From patchwork Tue Jun 7 09:47:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 2020 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id E3B2C3F08A for ; Tue, 7 Jun 2022 11:48:24 +0200 (CEST) Received: by mail-wm1-f69.google.com with SMTP id j31-20020a05600c1c1f00b0039c481c4664sf1483091wms.7 for ; Tue, 07 Jun 2022 02:48:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1654595304; cv=pass; d=google.com; s=arc-20160816; b=DNtpoYz9hmkaB+UQlnVJ4IUQhgbvIZJ9OLPjHTLJiMYRJ0vl9tuQHV3EIPYoIFe+0D MNrm4CNqwYqC09XN61bg2cxF48x9rHZ6XfDAN+4pPjyOUOI4GjxJpI0ra30tHNDLLQ7I J+Mi8JuWSpXnBsYHOaj/WbGRUqACfP8ro822kA+cxf06toxcfD99zFa6SFRACMYVeVgS 3V33WUxziXHe49I9AwbiyuN2W2GNbKsgiaVI2fI0W5ZYQ8Hj8TSPIA34GQHqpGFyMi8v 665xo11cBDqmXaKeZqrS+Q/FrEUrpC7Eho24zlffpvIAwpF822pmLsCwcx0y24PyQofY XCUg== 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:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=9JGVbnYSDZK+psOWeb9T010b6Ilfpvi7nldKDR2QC6s=; b=m0henVhKdES1OhKmp9IFXR5f1yz2pl3umC/i1m8G1bJv8QOJO4IAlL0HbdRaFF9LoG sBPlpcGChDQAock2iw1nqj75pZyg3yDcjMd+5BfOeqehhntOInXMD0t9upB11Or/zIkh jivsgzELTc9OzvNZuhMSSSPNGyNgxvjkcIYHBm+blyNFo0WWmrq7xRBT7R/bEY4+gsuo QXMvIiPAPDEfAVvs7dcFpLj5nVIQNt5IFIaH2Wkjf6CrnWw5J3Dul4zXi3maPNVQM0uH Q9eWLQQPPP5eAAcvi6UlYeYBoprkliHQ9jE/Qdr12WL7LEAZ5DdFrRFlLsMq/Sfq7w4K 3oqw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=cRCWGgSN; 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:content-transfer-encoding:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=9JGVbnYSDZK+psOWeb9T010b6Ilfpvi7nldKDR2QC6s=; b=SPphjCwWgm7doF5ffhe+PvvcbRNhjv4Qx9W3CmRM3rUyPd7ana8tgfTqxh3Blj/Qep PouAV8QwnloxBhFgK6J0ZX1JeDIcT1SuLUd4bZV+QAsylLiO15Sjlp74Vr+64m1wqEQu zMYC57GzGUaIfnbJjFZM9l44RC7d+JXdoYrDg= 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: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=9JGVbnYSDZK+psOWeb9T010b6Ilfpvi7nldKDR2QC6s=; b=hioLjj4TPRFuqS4GwqRI6JVdVEAtAt2rl/Fn59oHG4e7BTiDrlDqQq/BkH2Uqd/Bkf haKY6k0xSlRrzwvHj+TdvzMmTS25ZQnBRzGysipwS9CHLa121W64dz7TQ94cp4cTYNw9 pkL1tXOR7gj9+6Y51oBtRxy0dSF57iCvGdxES9NJcMu9wtDZbEErJCtdc045ms4JChE1 vBHytO3+ISGCqDnvoipxRrTtazkbtToZDyk4pdLojiBLY7G8Zs9J34bYj2U4ihy6OzWE XsGKGpuN1ecNOEwxl//n2e279pvT5cpIVyOqsSpqtyv460pi/mlzVwyz6dLp8eq5AsgM Fdyw== X-Gm-Message-State: AOAM530A0nRhVOJYSARLIEkTsE1NN2o9qgn6nxscVVAMpYJyVgRL5E7Q NngoeE4r+n3Flfy+s6vXGNnMAuFR X-Google-Smtp-Source: ABdhPJyV2ZcOzsbGj3OuARX9JcBgQrhC/iPNxLhrRUWIAGs0BQvBooDgJqEI89AHx0Xs2sJ7fK5GzQ== X-Received: by 2002:a5d:4206:0:b0:213:bb1f:b81f with SMTP id n6-20020a5d4206000000b00213bb1fb81fmr20798144wrq.363.1654595304787; Tue, 07 Jun 2022 02:48:24 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a5d:6dab:0:b0:212:d9db:a98 with SMTP id u11-20020a5d6dab000000b00212d9db0a98ls4153528wrs.3.gmail; Tue, 07 Jun 2022 02:48:24 -0700 (PDT) X-Received: by 2002:a5d:430d:0:b0:210:2ce0:e2a9 with SMTP id h13-20020a5d430d000000b002102ce0e2a9mr27131477wrq.627.1654595303799; Tue, 07 Jun 2022 02:48:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654595303; cv=none; d=google.com; s=arc-20160816; b=KULYKrlpiOSlbcruQsJZk16CeFvsI8vLadj4Rp/g8rfhkxx//1rjB4srZkyfoEz4Eo NQu6i+Pb/TYllNBOzAShLq3vwuN273eCUKJuHTB1TbuXPphmRTWprfk65eGRsUx7vwjP lsc8SE4l387DFpX4CAImEtaY23IoA3+2l0jrwVeFzLsbXWoD+2UEQpYyfoZ7mSEUTfCH w9Y92C4kyZ+7lpvpmwlY79cm/AKmq6n++7S11OIydjseL65YM7aMfbh50pAmm9D2b5Q/ rcqPZhpxgJKWGqGWbrA3m7lk4E3XserMV/DeXEnP8DN883mqYlMoJ76w5dmzpx2XqO+w tcEA== 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=onYK8jf0XQfJNXTnZtrlaJoF/ojpUqhJIjTYy4RWPWI=; b=plOOdloQmDVkCUhPkSjEWm7TOOSJPlMNCviX+Hxm94uX2veK2/bCiU3VHRSrXhgn1b XhdF54EA/i9IttUvsJfBpV8RF0Cr0+Bv88GNQYrBOnI61KoHDtKNM43JcESNSQlrSURm X8ioRQByeuU1lX8IeO30abKplB2tyGIIUda4lPf1XOxwxiurs+4CfQxhadAusWUiBJux YwBVB/cVcst2s1hjBBxBoPyw1YDfm2qhdTdKiG3v3Ho1Vdz5LuF3QcqEXCY7dOmYh4bK FWgVNVNoQ7qb+GPhEszO+XsoFaSh4P6pvYQUlPQk1oIw8njF91RQXPF/FzKxIHtL5CTR Ya6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=cRCWGgSN; 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 k9-20020adff289000000b0020d08586312sor9819756wro.40.2022.06.07.02.48.23 for (Google Transport Security); Tue, 07 Jun 2022 02:48:23 -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:a5d:4649:0:b0:218:4d6c:3f3f with SMTP id j9-20020a5d4649000000b002184d6c3f3fmr3510839wrs.148.1654595303518; Tue, 07 Jun 2022 02:48:23 -0700 (PDT) Received: from dario-ThinkPad-T14s-Gen-2i.pdxnet.pdxeng.ch (mob-5-90-137-51.net.vodafone.it. [5.90.137.51]) by smtp.gmail.com with ESMTPSA id o4-20020a05600c510400b0039748be12dbsm23200547wms.47.2022.06.07.02.48.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 02:48:23 -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: [RFC PATCH 07/13] can: slcan: set bitrate by CAN device driver API Date: Tue, 7 Jun 2022 11:47:46 +0200 Message-Id: <20220607094752.1029295-8-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220607094752.1029295-1-dario.binacchi@amarulasolutions.com> References: <20220607094752.1029295-1-dario.binacchi@amarulasolutions.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" 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=cRCWGgSN; 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 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. The struct can_bittiming_const and struct can_priv::clock.freq has been set with empirical values ​​that allow you to get a correct bit timing, so that the slc_do_set_bittiming() can be called. Signed-off-by: Dario Binacchi --- DTS properties could be used to set the can.clock.freq and the can.bittiming_const variables. This way the parameters could be changed based on the type of the adapter. drivers/net/can/slcan.c | 54 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/drivers/net/can/slcan.c b/drivers/net/can/slcan.c index dbd4ebdfa024..f1bf32b70c4d 100644 --- a/drivers/net/can/slcan.c +++ b/drivers/net/can/slcan.c @@ -105,6 +105,18 @@ struct slcan { static struct net_device **slcan_devs; static DEFINE_SPINLOCK(slcan_lock); +static const struct can_bittiming_const slcan_bittiming_const = { + .name = KBUILD_MODNAME, + .tseg1_min = 2, + .tseg1_max = 256, + .tseg2_min = 1, + .tseg2_max = 128, + .sjw_max = 128, + .brp_min = 1, + .brp_max = 256, + .brp_inc = 1, +}; + /************************************************************************ * SLCAN ENCAPSULATION FORMAT * ************************************************************************/ @@ -435,6 +447,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); @@ -456,7 +469,9 @@ 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 = -1UL; + err = open_candev(dev); if (err) { netdev_err(dev, "failed to open can device\n"); @@ -554,6 +569,40 @@ static void slc_sync(void) } } +static int slc_do_set_bittiming(struct net_device *dev) +{ + struct slcan *sl = netdev_priv(dev); + unsigned char cmd[SLC_MTU]; + int i, s = -1, err; + unsigned int bitrates[] = { + 10000, 20000, 50000, 100000, + 125000, 250000, 500000, 800000, + 1000000, + }; + + for (i = 0; i < ARRAY_SIZE(bitrates); i++) { + if (sl->can.bittiming.bitrate == bitrates[i]) { + s = i; + break; + } + } + + if (s < 0) { + netdev_err(dev, "invalid bitrate\n"); + return -EINVAL; + } + + snprintf(cmd, sizeof(cmd), "C\rS%d\r", s); + err = slcan_transmit_cmd(sl, cmd); + if (err) { + sl->can.bittiming.bitrate = 0; + netdev_err(sl->dev, + "failed to send bitrate command 'C\\rS%d\\r'\n", s); + } + + return err; +} + /* Find a free SLCAN channel, and link in this `tty' line. */ static struct slcan *slc_alloc(void) { @@ -583,6 +632,9 @@ static struct slcan *slc_alloc(void) /* Initialize channel control data */ sl->magic = SLCAN_MAGIC; sl->dev = dev; + sl->can.clock.freq = 24 * 1000 * 1000; + sl->can.bittiming_const = &slcan_bittiming_const; + sl->can.do_set_bittiming = slc_do_set_bittiming; spin_lock_init(&sl->lock); INIT_WORK(&sl->tx_work, slcan_transmit); init_waitqueue_head(&sl->xcmd_wait);