Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2702457iof; Wed, 8 Jun 2022 10:13:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzUwihzm5cHetd9oAdZsufOIExAd9i5KTkeA8Vx45U7lQVvHdSZINJCEwwdOIQZ9/j9IxaZ X-Received: by 2002:a17:902:8f8f:b0:167:7fa1:60e2 with SMTP id z15-20020a1709028f8f00b001677fa160e2mr15581759plo.121.1654708425830; Wed, 08 Jun 2022 10:13:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654708425; cv=none; d=google.com; s=arc-20160816; b=edqXsqjTorcjrK0ptZI3qVZfWP9x/iHBegXwu9AMg6e3rE7aNUSu4p+W2v804SXYtf KPgKX2PwxlwYBpLMUsA5EtR1VDSPto9s+RnZg4dF/dUcbU3brXhpVuAX+MdSlKPgUjwX bmVP5kzQzl/jcWYAPf0fbzggtJQF+Il8a/2cV1rB5Ori2qeiyiNM7shjiCZXKRH4v3n4 7sNuRDIxmjDnwKKPhEOTFtZTPs+4Iz5W+3goqgYBnRYIAjEjsZ2TF3Lq18F5M56PLzU0 5qXE4GCTul9Vv7UPwtAXvJekJ5alxR6lt67EGzh080X9qzcELN/rLGBAuenTC22RmEDR wn/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=MTTwzX/aiA/HRTscWPld7sV0y4pSfK/bYOnDrdN63VI=; b=OsFV9knap5ks2lIVnXeRQ9P4C4YrOBlylrUj/Zmiv3v+5raxsYEw/5lwIKMB+UFovu Rg7YM34820Kgy8LjcNq7KUKOKwZ8+g+8Xb1lsP6IExxLTFrG/FqTRpYEyedcdsaRpZWR WNp7alIzRWtqls3wpIkvBPWfxzPxhgM2/5n6tau/ulNAnc/hkfbsu4Ir0cdmhJPm+trS JIyO5y0GCkPEUEKbkw8SfzCfjZA9gDQgBAcl0h/D+KZT+JOxgHo3+x3I+7MuKnUL+aNh 6bTagr7OoKjRDqXh7gJxga0Nc/gEk47kl76gt/Va1MghlECMtatuBnosx/0pbUfimmSF rzqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=OSyMEpVo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b13-20020a63cf4d000000b003fbfe888d78si27436726pgj.469.2022.06.08.10.13.32; Wed, 08 Jun 2022 10:13:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=OSyMEpVo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242322AbiFHQ7N (ORCPT + 99 others); Wed, 8 Jun 2022 12:59:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236479AbiFHQzc (ORCPT ); Wed, 8 Jun 2022 12:55:32 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF9A73CAF43 for ; Wed, 8 Jun 2022 09:51:44 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id d14so20141877wra.10 for ; Wed, 08 Jun 2022 09:51:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=MTTwzX/aiA/HRTscWPld7sV0y4pSfK/bYOnDrdN63VI=; b=OSyMEpVoLY2gHejWkMk9SCxrTmHFgHLaZDyBg2ojZQlfFiKUmG6vVhu2pb0EN62d4y XWAcHLQr18NR0yAc/EVfYRCOzh73nKlABEU9WIfbyoXNMp3D40jTiZd/7DcR3gXz3idz 8rByJjSs0u69EFn5sr/zghXbyFofFSOrFApak= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=MTTwzX/aiA/HRTscWPld7sV0y4pSfK/bYOnDrdN63VI=; b=q8sxHQCKOTzcgrJErGjtr01/HMy61DW7ogHxXobkDN8hcZAcxd9+lJ5mC8kRnRDAvW ysQWsZFVqNW0UWbUJX7oDwBNRvYSblK1gqWfjCNkgUdC/Heyvik6SZUbkhCGX6fyq9Ul opnmUuxTryEF4HUlJRk4lqKh9ZMXiMOALlvux72K0GftXE3lOdIzPwp2oUMi8lbyx8i3 gAkV5kRNb+zRBGAjo7IGoFu1kxtqffM3+pyXha0oMimopdW+Uo9jk6Opsx39PHQQSbhS vDeBYU7iqo0xjuI66G3AgCmaJNFUcjHx+QcSDYZxzy6NIHiwjtRvd2qazgVuQFZ27zAd qQOQ== X-Gm-Message-State: AOAM530pYtuOgUjC3Kbi9tFFXB0oWFzlsctYejNHGHYMWF46RPEFk2Lj sUwVRwiveKh4bhhfPRUEdBt3V7zs4RhZKQ== X-Received: by 2002:a05:6402:f97:b0:431:8d1d:397d with SMTP id eh23-20020a0564020f9700b004318d1d397dmr11248102edb.423.1654707092277; Wed, 08 Jun 2022 09:51:32 -0700 (PDT) Received: from dario-ThinkPad-T14s-Gen-2i.homenet.telecomitalia.it (host-80-116-90-174.pool80116.interbusiness.it. [80.116.90.174]) by smtp.gmail.com with ESMTPSA id c22-20020a17090654d600b0070587f81bcfsm9569071ejp.19.2022.06.08.09.51.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 09:51:31 -0700 (PDT) From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: Amarula patchwork , michael@amarulasolutions.com, Dario Binacchi , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Marc Kleine-Budde , Paolo Abeni , Wolfgang Grandegger , linux-can@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v2 06/13] can: slcan: allow to send commands to the adapter Date: Wed, 8 Jun 2022 18:51:09 +0200 Message-Id: <20220608165116.1575390-7-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220608165116.1575390-1-dario.binacchi@amarulasolutions.com> References: <20220608165116.1575390-1-dario.binacchi@amarulasolutions.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is a preparation patch for the upcoming support to change the bitrate via ip tool, reset the adapter error states via the ethtool API and, more generally, send commands to the adapter. Since some commands (e. g. setting the bitrate) will be sent before calling the open_candev(), the netif_running() will return false and so a new flag bit (i. e. SLF_XCMD) for serial transmission has to be added. Signed-off-by: Dario Binacchi --- (no changes since v1) drivers/net/can/slcan.c | 46 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/drivers/net/can/slcan.c b/drivers/net/can/slcan.c index cf05c30b8da5..cab0a2a8c84c 100644 --- a/drivers/net/can/slcan.c +++ b/drivers/net/can/slcan.c @@ -97,6 +97,9 @@ struct slcan { unsigned long flags; /* Flag values/ mode etc */ #define SLF_INUSE 0 /* Channel in use */ #define SLF_ERROR 1 /* Parity, etc. error */ +#define SLF_XCMD 2 /* Command transmission */ + wait_queue_head_t xcmd_wait; /* Wait queue for commands */ + /* transmission */ }; static struct net_device **slcan_devs; @@ -314,12 +317,22 @@ static void slcan_transmit(struct work_struct *work) spin_lock_bh(&sl->lock); /* First make sure we're connected. */ - if (!sl->tty || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev)) { + if (!sl->tty || sl->magic != SLCAN_MAGIC || + (unlikely(!netif_running(sl->dev)) && + likely(!test_bit(SLF_XCMD, &sl->flags)))) { spin_unlock_bh(&sl->lock); return; } if (sl->xleft <= 0) { + if (unlikely(test_bit(SLF_XCMD, &sl->flags))) { + clear_bit(SLF_XCMD, &sl->flags); + clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags); + spin_unlock_bh(&sl->lock); + wake_up(&sl->xcmd_wait); + return; + } + /* Now serial buffer is almost free & we can start * transmission of another packet */ sl->dev->stats.tx_packets++; @@ -383,6 +396,36 @@ static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev) * Routines looking at netdevice side. ******************************************/ +static int slcan_transmit_cmd(struct slcan *sl, const unsigned char *cmd) +{ + int ret, actual, n; + + spin_lock(&sl->lock); + if (sl->tty == NULL) { + spin_unlock(&sl->lock); + return -ENODEV; + } + + n = snprintf(sl->xbuff, sizeof(sl->xbuff), "%s", cmd); + set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags); + actual = sl->tty->ops->write(sl->tty, sl->xbuff, n); + sl->xleft = n - actual; + sl->xhead = sl->xbuff + actual; + set_bit(SLF_XCMD, &sl->flags); + spin_unlock(&sl->lock); + ret = wait_event_interruptible_timeout(sl->xcmd_wait, + !test_bit(SLF_XCMD, &sl->flags), + HZ); + clear_bit(SLF_XCMD, &sl->flags); + if (ret == -ERESTARTSYS) + return ret; + + if (ret == 0) + return -ETIMEDOUT; + + return 0; +} + /* Netdevice UP -> DOWN routine */ static int slc_close(struct net_device *dev) { @@ -546,6 +589,7 @@ static struct slcan *slc_alloc(void) sl->dev = dev; spin_lock_init(&sl->lock); INIT_WORK(&sl->tx_work, slcan_transmit); + init_waitqueue_head(&sl->xcmd_wait); slcan_devs[i] = dev; return sl; -- 2.32.0