From patchwork Sun Jun 12 21:39:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 2054 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id 55F843F1DD for ; Sun, 12 Jun 2022 23:40:08 +0200 (CEST) Received: by mail-ed1-f69.google.com with SMTP id a4-20020a056402168400b0042dc5b94da6sf2947373edv.10 for ; Sun, 12 Jun 2022 14:40:08 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1655070008; cv=pass; d=google.com; s=arc-20160816; b=PH89L38BAzqggnpjw8FspxcgOOZuZSpoizrmuMX2ynLhwi1ID0HnDRkg1kN2ZW65T5 /lUti5sjg6+ExtCytfSgcuh4IYikfS2IsbwuXE686XIUSBTR017LwF3idL5W6rtfaWMa 30K/E1AQf1Lt5WTmku7Fc6zRHxqvtDiD8uCWqStzeUgjobhqopOToWZrb2usnwC9FmS+ z4W7Avl/tl1F8K/VeXTuT85xJ1IQQkC48S8E6UkWgMEmOeWOoBFtUV83+86OYsGxq3PY V16icwIoEBAClBhtMoCf61zK18UTUpCvnH9BgX0Iy9UJZ0z5AeWlisIzT9EmNn6xkpgi YZTw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=dOJUY+m3CETPRn4+2lIDxKcqcjE2dJofYG50D3QlEWU=; b=VlPiuLLLIvBoabnzT9ybDkWI/VeiNzD9wtz9Rj1HNvzAl+VvCI7I7qBbEpQvVKUawc s2vgP8gMI5JSBQKTm1T5eWDe354mt9YbPHgAPgYJtWTnVgh3nRhozrchA+Hmvp3HheB7 L/9Er19GaPNklbewrVAdvBOQSdyx/sNkIdPZ02bWUXH0Pg5nGM7sAILHFJICwrHsM+05 1TyteKiZggLQ/bWh3nCMF6InC2O9BKSII1kgUq77kANYsiiuLfRnf49UXabT6fi+uz29 NGj31GYKm9a6VJE/CnDIkoXlBEGBZH5ZSKeZgX2p5uBJllUshUSeHzKwleMFX/XSeaeI 1XOw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=ekzn2Eu7; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com 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:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-unsubscribe; bh=dOJUY+m3CETPRn4+2lIDxKcqcjE2dJofYG50D3QlEWU=; b=Y2kQu7hTjnRpP8ly7b/bQl8jfi9OEQUoaldRfugszmyuWWv+VCyInj0Bde9fi4w8wY Ifn58KkuMk419n+mIknfGgPmZQ7CX40Yp57iCfnULYMY7YUWRZQ2896BaVkO6QzxLDHf +lPUjxBPBhXeeXbeybY/+c3J5y3+w8Un85JEA= 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:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive :list-unsubscribe; bh=dOJUY+m3CETPRn4+2lIDxKcqcjE2dJofYG50D3QlEWU=; b=JdS9UmQQrAomZEB7cChilIslnfIe0di8c+xHXVyzbQVDY6yHXRPy9Zdc+TQsPZVvzY 8SOtLHfUqWvzmU3W6w/XOJogAev3JDWKyRFPJnejjDrKMcohukBLzm64SbiU5lJoG+pu Ie3lu23QrgPrvd6Mxfsn70A2t5IsGBfWaZvql0DJy9ramXxqJRL/z0c+G8xJyIO8fv6V JBwma8X7zUXZpzOKd6lrs/VmIUyZaX7s3vJKRh1VfobqDGEs15aYtxtmZe3I6CtJzT9F b+RTVp7V55/xnpFtSSffQztxITaRa/L/uHngBzGboHLeWqekZB/0I+ltIFtWZi9paM45 3+aA== X-Gm-Message-State: AOAM531Zek0BA/pv3jHmsxTF/6xfMK3YWJ0i35/8dZQM3zompx5hylYG sJfm6jS6bgWo1/S7ybWCGLaB+meL X-Google-Smtp-Source: ABdhPJz7dPVQsd2o1Q59ApUwF/Izt0OZt/BL0OsWEPnW936h9BQMz3p4NGpkO7bLitLmn3/JCzqa4g== X-Received: by 2002:a17:906:d82:b0:70d:84d3:b6df with SMTP id m2-20020a1709060d8200b0070d84d3b6dfmr46190506eji.464.1655070008165; Sun, 12 Jun 2022 14:40:08 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a17:907:c19:b0:704:582e:9858 with SMTP id ga25-20020a1709070c1900b00704582e9858ls1010627ejc.1.gmail; Sun, 12 Jun 2022 14:40:07 -0700 (PDT) X-Received: by 2002:a17:906:2dd:b0:712:1293:3dd8 with SMTP id 29-20020a17090602dd00b0071212933dd8mr15463307ejk.448.1655070007110; Sun, 12 Jun 2022 14:40:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655070007; cv=none; d=google.com; s=arc-20160816; b=aJuRxlDRQgLv6UpC4SpiPdFuKGUNC3Fzq4V0Jix6aC7sgzaSZBfMD1s8aw7hJ1IVNj DDgjAwL33Oeh6MAfidg9Ttte3fBsNT+iHV/XntAmUG3rKrsHYx1S+S/AhWd9ovahebQB wkD6yg+hgfZC5NO9PniUH3MgAqTazJGuTzovmXmj13n0r8uLT2CCJEP67DSbro9MQFna 7GtC5TU6Gn24xh9D1Pucb5gQlgIidSDH3rBegJWqgGkI3IO4tddHHMNpnYahKkMoJoi0 UsD8qF2QwkFoOWUgTlU8wNiBh+XEGkhW9n8p1Tr7sLXAR7+vchzTnPNdM1W0RxlGxj3a LG2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=+67b1tOoWpv+tJyn4Sl0UQj6SwsqWBdWszhIl7nE8ec=; b=cMypCVnWcIuSn1o4XHn+OkKrzFjI0Z+YzRkxeSo6mfOE0x+mu0zGSpR86Nyxxez5J0 YqNeEJbfqcjZ/kS5yZDp6wHnA1tdTh3tuN7H4j10DFCrJQEunCLC7biWOWqLxsgt6sLH X9rUjxO9Di9aXA7qu+eFaqyuy9G8yyq+1Lmk09GhqZdLV5Tj6YztdKbaq/X7tJdYM2oJ VcuIW9yfiKFFeffIxmF6cQI+bVYuu/7lvVV5Z5o8LDShVoUX8FCqFh2wdR9GGePa5XUs ORAZgQEgcyXtE1wdfeLS3hfRIBCZWstk1M0elCqr3g9WBiKkz3tc750j9PeiKVt1ssoa V5Ag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=ekzn2Eu7; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id z4-20020a1709063a0400b00712134cb9d5sor1286944eje.69.2022.06.12.14.40.07 for (Google Transport Security); Sun, 12 Jun 2022 14:40:07 -0700 (PDT) Received-SPF: pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; X-Received: by 2002:a17:907:8a1d:b0:711:d86e:cc5 with SMTP id sc29-20020a1709078a1d00b00711d86e0cc5mr31921307ejc.237.1655070006888; Sun, 12 Jun 2022 14:40:06 -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 u10-20020a1709061daa00b00711d546f8a8sm2909398ejh.139.2022.06.12.14.40.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jun 2022 14:40:06 -0700 (PDT) From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: michael@amarulasolutions.com, Amarula patchwork , Oliver Hartkopp , 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 v3 07/13] can: slcan: set bitrate by CAN device driver API Date: Sun, 12 Jun 2022 23:39:21 +0200 Message-Id: <20220612213927.3004444-8-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220612213927.3004444-1-dario.binacchi@amarulasolutions.com> References: <20220612213927.3004444-1-dario.binacchi@amarulasolutions.com> MIME-Version: 1.0 X-Original-Sender: dario.binacchi@amarulasolutions.com X-Original-Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=ekzn2Eu7; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Content-Type: text/plain; charset="UTF-8" Precedence: list Mailing-list: list linux-amarula@amarulasolutions.com; contact linux-amarula+owners@amarulasolutions.com List-ID: X-Spam-Checked-In-Group: linux-amarula@amarulasolutions.com X-Google-Group-Id: 476853432473 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , It allows to set the bitrate via ip tool, as it happens for the other CAN device drivers. It still remains possible to set the bitrate via slcand or slcan_attach utilities. In case the ip tool is used, the driver will send the serial command to the adapter. Signed-off-by: Dario Binacchi --- Changes in v3: - Remove the slc_do_set_bittiming(). - Set the bitrate in the ndo_open(). - Replace -1UL with -1U in setting a fake value for the bitrate. Changes in v2: - Use the CAN framework support for setting fixed bit rates. drivers/net/can/slcan.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/drivers/net/can/slcan.c b/drivers/net/can/slcan.c index 4639a63c3af8..be3f7e5c685b 100644 --- a/drivers/net/can/slcan.c +++ b/drivers/net/can/slcan.c @@ -105,6 +105,11 @@ struct slcan { static struct net_device **slcan_devs; static DEFINE_SPINLOCK(slcan_lock); +static const u32 slcan_bitrate_const[] = { + 10000, 20000, 50000, 100000, 125000, + 250000, 500000, 800000, 1000000 +}; + /************************************************************************ * SLCAN ENCAPSULATION FORMAT * ************************************************************************/ @@ -440,6 +445,7 @@ static int slc_close(struct net_device *dev) netif_stop_queue(dev); close_candev(dev); sl->can.state = CAN_STATE_STOPPED; + sl->can.bittiming.bitrate = 0; sl->rcount = 0; sl->xleft = 0; spin_unlock_bh(&sl->lock); @@ -451,7 +457,8 @@ static int slc_close(struct net_device *dev) static int slc_open(struct net_device *dev) { struct slcan *sl = netdev_priv(dev); - int err; + unsigned char cmd[SLC_MTU]; + int err, s; if (sl->tty == NULL) return -ENODEV; @@ -461,15 +468,39 @@ static int slc_open(struct net_device *dev) * can.bittiming.bitrate is 0, causing open_candev() to fail. * So let's set to a fake value. */ - sl->can.bittiming.bitrate = -1; + if (sl->can.bittiming.bitrate == 0) + sl->can.bittiming.bitrate = -1U; + err = open_candev(dev); if (err) { netdev_err(dev, "failed to open can device\n"); return err; } - sl->can.state = CAN_STATE_ERROR_ACTIVE; sl->flags &= BIT(SLF_INUSE); + + if (sl->can.bittiming.bitrate != -1U) { + for (s = 0; s < ARRAY_SIZE(slcan_bitrate_const); s++) { + if (sl->can.bittiming.bitrate == slcan_bitrate_const[s]) + break; + } + + /* The CAN framework has already validate the bitrate value, + * so we can avoid to check if `s' has been properly set. + */ + + snprintf(cmd, sizeof(cmd), "C\rS%d\r", s); + err = slcan_transmit_cmd(sl, cmd); + if (err) { + netdev_err(dev, + "failed to send bitrate command 'C\\rS%d\\r'\n", + s); + close_candev(dev); + return err; + } + } + + sl->can.state = CAN_STATE_ERROR_ACTIVE; netif_start_queue(dev); return 0; } @@ -583,6 +614,8 @@ static struct slcan *slc_alloc(void) /* Initialize channel control data */ sl->magic = SLCAN_MAGIC; sl->dev = dev; + sl->can.bitrate_const = slcan_bitrate_const; + sl->can.bitrate_const_cnt = ARRAY_SIZE(slcan_bitrate_const); spin_lock_init(&sl->lock); INIT_WORK(&sl->tx_work, slcan_transmit); init_waitqueue_head(&sl->xcmd_wait);