From patchwork Tue Jun 14 12:28:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 2072 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id 3B9323F03E for ; Tue, 14 Jun 2022 14:28:37 +0200 (CEST) Received: by mail-ej1-f69.google.com with SMTP id mh24-20020a170906eb9800b0070947edf692sf2799124ejb.10 for ; Tue, 14 Jun 2022 05:28:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1655209717; cv=pass; d=google.com; s=arc-20160816; b=bVMVyH9NFs6D1LFU91tATkCiyeMkc9u5fAxCoryk8wdw76QxD4U4OcEdXAlANWRU5v r+8SP8HcyREPkMOCEBbJtDaWMC0aYNqTW0UmPYfuUs5yEtaNuBDhUNwaJEF4fZnKKc9O v7iSBouSN013raXXwLp0xqLGHum/fwJGIqlJIpj3snvyRiT6s0Cs26ZLgcNs3DEOTR4D jcmrzME6o0oYk7Hc99sKYcov8Lag0+Bf/FtDcuWrKuYZvYCPNZ2GitJ84umvgK2Uh0tz yh+f1Pxf01CxYO4UfPl0hJkKAuGaiBhioMnNcUh/vShs0yihmVqY6RVVAJv4FhIuUAoQ Hqtw== 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=t9RT6ixxSf0NDAxLWRPJgx2sCubF8xu0QISo5KlAUg8=; b=ZCGXVnsUGM8cy0n2Kf/oI30DdFF3bM/PbqwkSwaHPILt8vjjhtXC26MxkR3GXNX+tA J6nm3JC4qxPo3bqAQ9ZrCypqWDO7q9HGXbsrZCNt3BZ6fkOiPfF2Z9MkdeDIIsrnTvSX OwZSd7Cpd1zjVQA6snZMTpLPGOusaRWNDnELTCwCKxiaCAt+9YZtoEP0qpmAW/ftTqH5 GQ3RHKvWe83JEK8+bjYPUIw2a6AErJZW5HJpdmPG05kTnKQEZEHgsZD8PzyDZQEbHPPy 4HKp4mMh3zAQHqkgc5ssEshjL8qk3hKFxV5VRVWharGCy794rpXXmVnvzQCah4Y8vxw/ MPYg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b="Z/JIYpot"; 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=t9RT6ixxSf0NDAxLWRPJgx2sCubF8xu0QISo5KlAUg8=; b=K3cbZF+I46KaziBRGzSLo2jsrw7/2EzdaWFAVVp+10tbRGhvF3hTtxqXR2iA1dTcDT 73n/QzTaOk+NETY46yBAiCEMXWaZlzLGeXDDLljk1zq0086qUaRlrhqrTwCbdWXiHEew ccgekHEQZ4Pcb11Zraad4GrsGC8YWFZ2GOe5U= 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=t9RT6ixxSf0NDAxLWRPJgx2sCubF8xu0QISo5KlAUg8=; b=rmh4TnBMWJhj8nEoRVpfBwKhQSoYTx0bsU2IFnqis9i+j3PEgd0EcXPbkPICfhCY/Y xVvKJYtUDeHB496AgqWeEiNCwDYoLToTGVew9uKt/AyMCZvueu35faUzuSOh78eklS+f Svz2KtxlBjpnLfWCisiN5s0myzmLOtQemT2h9U73axdOw6/uMnTSXWvBZCbtgAqSf1X0 /RH6Com86NAmn/f9QO6qFowRPjjDW8m4O8HV9MmBiP7zeamJ82t8h6UeGdgEpcUOg58r ESIUziT4OuNsiSWF5a1jS764NDj+PUHt81uxq/KwB+5laow+f80Gyy00p/1WOxV8Ncgg bTLA== X-Gm-Message-State: AOAM531I1Yb1w+dIlcTkdr4nHGvfv5rKnZiI11h/D1hXIix7YYnqos2l AeBvsupikHrrBJAlUNtzUp+cBK+1 X-Google-Smtp-Source: ABdhPJwgsGJz3o0/AXTX9+QG7ds31k8XGC3m7FN/RGP23IgQ58yOO0kEszqXnLRKIILMGB8wWga2zg== X-Received: by 2002:a17:907:8a1f:b0:711:da32:8410 with SMTP id sc31-20020a1709078a1f00b00711da328410mr4358936ejc.298.1655209717006; Tue, 14 Jun 2022 05:28:37 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a05:6402:709:b0:431:3a5e:75b3 with SMTP id w9-20020a056402070900b004313a5e75b3ls2366507edx.1.gmail; Tue, 14 Jun 2022 05:28:36 -0700 (PDT) X-Received: by 2002:a05:6402:25c2:b0:431:932e:eb6f with SMTP id x2-20020a05640225c200b00431932eeb6fmr5725395edb.296.1655209715952; Tue, 14 Jun 2022 05:28:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655209715; cv=none; d=google.com; s=arc-20160816; b=HrJhbPjXnKUM8rgsG7IVDNHsgFwHQN2YsCgPCPaxvAmJHB1MZSTsLH3BnTcY+klzhX H+9L3Oc/Ggb6ThwC0XzEE3W/XR/gDHVH8s2m39BOimfet0KthLc3WAr93NRo2UJ3pAH4 uJ34tTek24cvBOITMecmrtsoitKrkbl1M1QGWjpMsjk1CpVO4ZeRC1M8JC/dS+Bdukk0 gAMx6Ab42GLYQWHOt80j4GE0dfFEp3JQMVDJ8VJiZsuGObYB9ExoD1K1b5yivSSNHL1s W+Wav/V/14NyV1dHFfztNAdIHLlAvMBZkU+zm6iq/RGgUmPdfevCLGYvdmgTLvI2TKOp QI3Q== 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=AbbDKExzTnDRI7gbQZlSxolycHVQn9x05Wxw1nhoVAE=; b=Ki1DhcycUJ6eSCEWu2TTgJq7HJ1eAIZCjBxe84pf8pSbWMoidc99xuBBo46HOkIP2b jsEEUCGfZYQnjXU83mq4lJmNLGzmfeItl81Vt+y88w3deZ37BRnpjXS9xwCh/IB5YbTM N9mA1sLcDXQjI0vIRI/KHKV8+7kUnGws4k50Gm6Zmtk9TNRN51T78FOSBbKJw5zuXLiX BcIl28DP6DQsJW7dfVKG9j0vDPTycU6mr1OvN7bbYlbdPzvYI7dPauoFv51+reaR6bSP qhe1VflbkNyqT+FJGb+M4iwYhWZJxmii5DSPuSmWGU781EJHskwTS0//aMNw71j2XUbX YXxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b="Z/JIYpot"; 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 c11-20020aa7c74b000000b00425d72fcb73sor4218530eds.19.2022.06.14.05.28.35 for (Google Transport Security); Tue, 14 Jun 2022 05:28:35 -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:a05:6402:5193:b0:42e:2569:652c with SMTP id q19-20020a056402519300b0042e2569652cmr5733602edd.73.1655209715758; Tue, 14 Jun 2022 05:28:35 -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.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Jun 2022 05:28:35 -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 07/12] can: slcan: set bitrate by CAN device driver API Date: Tue, 14 Jun 2022 14:28:16 +0200 Message-Id: <20220614122821.3646071-8-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 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="Z/JIYpot"; 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 v4: - Use CAN_BITRATE_UNSET (0) and CAN_BITRATE_UNKNOWN (-1U) macros. - Don't reset the bitrate in ndo_stop() if it has been configured. 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 | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/drivers/net/can/slcan.c b/drivers/net/can/slcan.c index 2afddaf62586..bd3cf53246c7 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,9 @@ static int slc_close(struct net_device *dev) netif_stop_queue(dev); close_candev(dev); sl->can.state = CAN_STATE_STOPPED; + if (sl->can.bittiming.bitrate == CAN_BITRATE_UNKNOWN) + sl->can.bittiming.bitrate = CAN_BITRATE_UNSET; + sl->rcount = 0; sl->xleft = 0; spin_unlock_bh(&sl->lock); @@ -451,7 +459,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 +470,39 @@ static int slc_open(struct net_device *dev) * can.bittiming.bitrate is CAN_BITRATE_UNSET (0), causing * open_candev() to fail. So let's set to a fake value. */ - sl->can.bittiming.bitrate = CAN_BITRATE_UNKNOWN; + if (sl->can.bittiming.bitrate == CAN_BITRATE_UNSET) + sl->can.bittiming.bitrate = CAN_BITRATE_UNKNOWN; + 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 != CAN_BITRATE_UNKNOWN) { + 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; } @@ -582,6 +615,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);