Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp1408435iog; Tue, 14 Jun 2022 05:36:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyq1lHAvGrSJ7euA+MKibyaJnbf1Hll30SAGQENMmUQhrvW7TThT8rEj3hyMBSPCXBQqzrx X-Received: by 2002:aa7:dbd7:0:b0:433:55a6:e3c4 with SMTP id v23-20020aa7dbd7000000b0043355a6e3c4mr5940430edt.74.1655210207346; Tue, 14 Jun 2022 05:36:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655210207; cv=none; d=google.com; s=arc-20160816; b=fTW5A/GDRrlWsyImd3NdMQd7ldQFc4UfZLGrNfpRBVj7nz/vZIj5dSSvXmYXmq8q5U w8pq3yk5bb2qRBaVYCU0WvCzjqu00NmoQ4HGrkgE9JQy4nWuFc2Hag3MXurkoJ3iZP14 r79jVtsDX/hO01qhZLOF6P2jsz3sn6a61wFSObNIIA8TqJ9vRwtDdsbRqx+7z7OoqGcN l3vHEGpkwyytSSzlZFgAKfUU4k23ru5JmjCLZ0GxC/PVxgnquH//Wo/RT4GkD1wjr28Y 3ItaT2gmVwZkk3nOecOmllSy01gWDezvqnYyj8z6RoHhbDmrYKmND01W/sSe5U8V11sr kJVg== 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=J7cAHYzHeqO4eieQ7T7B38pR/xwLeH3ndQgGNnjLG/E=; b=v2d9Od/AanwRvsh0ORRkgsowonBIVd8gCKd+P3//OmZQz8UaxNcCMcBx8YKQ94GLn8 vwPnDzxIWuKO1EhVPPIS8yCBjteJvyKBbPyCw4kCStTMbbCnLNpj/LPXbncP32ZJhYuv Jt6d5+WA2tVtJKczwtGaFGPVR8KOAsyFbJ5ev4CXQBP/szQQFU2NrkOAWpK8cjVSdJmS 3TcoxsGnYi1tiqEHtmzSHZfWw2kvuzHEZpfrb99emjYbJQuwpALhws486JYxPzh2uLYN FwJ+seTYVvv8QGziJkQCB9iPkEca9p0qzqWcXILlUWYuLKF0jAa3qotYNnl4wWDaN45m QAnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=XSAUyZOW; 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 ds20-20020a170907725400b00715702f1e9esi10809321ejc.799.2022.06.14.05.36.21; Tue, 14 Jun 2022 05:36:47 -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=XSAUyZOW; 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 S238888AbiFNM36 (ORCPT + 99 others); Tue, 14 Jun 2022 08:29:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243564AbiFNM2i (ORCPT ); Tue, 14 Jun 2022 08:28:38 -0400 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0153C25EB8 for ; Tue, 14 Jun 2022 05:28:36 -0700 (PDT) Received: by mail-ej1-x62b.google.com with SMTP id o7so16868672eja.1 for ; Tue, 14 Jun 2022 05:28:35 -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=J7cAHYzHeqO4eieQ7T7B38pR/xwLeH3ndQgGNnjLG/E=; b=XSAUyZOWy5q2GWu3PLxoxE10G+1JlvepEjzvUqXBzxdLsTBIVBjyFLiLgEWy3AefaL qIDG5NM9+f+kytLuYuLs2tW2EsI8Oe9XkNrIgacbkXTyJzOsoPt9CE7Hzq9eDs+tiR0P oI4PRPXqGRbSBWCCi3Piai0AvOjG7IYUYM9KM= 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=J7cAHYzHeqO4eieQ7T7B38pR/xwLeH3ndQgGNnjLG/E=; b=WQ4dSrYRW8BMBX2I18oIdSbyygnop8wHdkUfO7lKXtgXKv1/l7wo+MKMypOOaerAbN 5XuY8fRT73eUevwvGHjBOfBGFrI3/4awpyPafKlnxTaw/1qn7mnOnm7dHipAn3jkDyDw o7gQvkLWo7WTNJHDDY/U9C3+VQdYjUquiKvFtsZ2W8XUWu2wLw7EJKuKX4sZbl7Xek+e xdQFiZ9XDVlQwy58Ahdwz413K7PJy7uNmIo+9OYphuVdoYzSnTYzmKBnMKSN4kqiD0+3 aFM6nlds1O+R5+IBBcowz8LV5MrIT1vEc66H/e4eYxyUJ35Hc5+NiN9q2lCn8Q3MtcHD BOtg== X-Gm-Message-State: AOAM533nV4lqW+UanuxW8ogGLeOMV5XNHfKRqV1E9lA29Iih6HfdN/6v cgUIwjzPiNQ0aKNQCk3sBga6Lw81NCqmHw== X-Received: by 2002:a17:906:7254:b0:6fe:5637:cbe6 with SMTP id n20-20020a170906725400b006fe5637cbe6mr4030190ejk.612.1655209714188; Tue, 14 Jun 2022 05:28:34 -0700 (PDT) Received: from dario-ThinkPad-T14s-Gen-2i.amarulasolutions.com (host-80-116-90-174.pool80116.interbusiness.it. [80.116.90.174]) by smtp.gmail.com with ESMTPSA id z22-20020a17090655d600b006f3ef214e2csm5087043ejp.146.2022.06.14.05.28.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Jun 2022 05:28:33 -0700 (PDT) From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: Amarula patchwork , Oliver Hartkopp , 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 v4 06/12] can: slcan: allow to send commands to the adapter Date: Tue, 14 Jun 2022 14:28:15 +0200 Message-Id: <20220614122821.3646071-7-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220614122821.3646071-1-dario.binacchi@amarulasolutions.com> References: <20220614122821.3646071-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 the close command (i. e. "C\r") will be sent in the ndo_stop() where netif_running() returns false, a new flag bit (i. e. SLF_XCMD) for serial transmission has to be added. Signed-off-by: Dario Binacchi --- Changes in v4: - Replace `sl->tty == NULL' with `!sl->tty'. Changes in v3: - Update the commit description. 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 c7ff11dd2278..2afddaf62586 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; @@ -315,12 +318,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++; @@ -384,6 +397,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) { + 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) { @@ -541,6 +584,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