From patchwork Thu Jun 2 16:04:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 1991 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id F08383F06D for ; Thu, 2 Jun 2022 18:04:46 +0200 (CEST) Received: by mail-ed1-f71.google.com with SMTP id i20-20020a50fd14000000b0042dd305d0f7sf3770888eds.18 for ; Thu, 02 Jun 2022 09:04:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1654185886; cv=pass; d=google.com; s=arc-20160816; b=eKncyeCKBk528Jj92P8jQYwdjF3/Ei/KBgJ0aJGALDuXtL98j+3FOmZ8kWw1HG7IvI Jpn4Kx0VQyKinY+y4NyHZJ6FIDVLW2vL3TwvWi+9kppgLJN/QjIbDI5KjFyVYJOjXHfE zJGtv+eBKTwTfNciko79Ar9jyeV4DMaAigIePDUG5plJayL97fjlYgFUbw6TTG2Fczbb buBnx5la4fJOSRf8pQLQS2p9mRij3Q9t12GL5sF1iCH/57/akF0VuVQYqnL5JXhk7ui5 DcHcRZD9ENPmgS+xdOmgpGjQgLvmoPiMImWv36smsE3kgi+yx4sxKRSI1KLtYL7unfX6 au0w== 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=GYj2oFAVOId2ZMNw0h2vZHvNkNOHBgZPFfpOhF4ydYk=; b=PsP0D5Uo8gyBiC8EFQdBFKGzjKqkfSeQyUAv8A2iXkHxfS5kORSUFmcvrrG0E3JM4v xHmv/A10Ypapec4ljrF7X1e3wpS6UjPt9DW/NmuPsGJwOSb5smcM+/22Qc+LzLlbIm1g X/XjVQBNKvM75/1rV24bGdkqbZUiOFAvNb1zC/CJf1chbrZdaq4gA3mA5nk46T69J8W1 zjZBoPYiAMW8M2XNrVBWSk2JTw0t4zbD+DGDE3SCY7BVHEHOlQnepLGX9ZUXFL7fK+BQ +4cJdukmX8X7R2QI00Ehf7kl/YXS+S6PQs/TR9UJaLAo+7bfJTdn687Os67RuSVp+6rA Hkvg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=XI3Pyioz; 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=GYj2oFAVOId2ZMNw0h2vZHvNkNOHBgZPFfpOhF4ydYk=; b=UzDJZ2BUHaqGCpncGRe7udi9m2kM6sJ1dlJX8EyvPktP/DiEPmKCbk8bdRi+XpFls1 WNEDrquBz1h1sbNSRUc67kZQ2KjYjTY/qwF0sotFfFhBcCDcL/UM9DcSqRB+KJO17ijp muiJTHVTW6/IIXOO9nG25DSY4moG0suCDECZI= 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=GYj2oFAVOId2ZMNw0h2vZHvNkNOHBgZPFfpOhF4ydYk=; b=a3/FkVOuQSatkoIQlNd0JkxC4AuDs2Tvl1UWJLhTy8CdTEOzY+9q9cArlMuD6yr3KQ Q5yAscdLDtrBXSsfSI9khb3Uak/Zax6JANgwQ2VEbzP9+b3g5e57N5DF9zFK/Sp2eZ4T U3SGQKMyDSBVtHcg1+eoz8CTGTqevBXxDw/GXA5eT0flJAB2RmK2QuNeJizsEx9+Lwbv WzNDkUvIjHzCe4vnuvMjLDCvLD43I6gERAwtj50XNSQ2mN0AJIPvsXOyG6RIrp2t7+ox 5SejLIDZosW/XemUzsFxZsQmnrkHS6BUy+S/7h6+SzYesXdWTcTgeVgL7rZ8s+F/fkyf ZsrA== X-Gm-Message-State: AOAM531pFZ/mLP5Yo+Xpkn/SQiEzuK0iSIJ5uL16uLX5hl1T273R4S/5 T8z4OYouYT87gYxRq6ZruLGeyq7A X-Google-Smtp-Source: ABdhPJyR72fU4mdmT45it8BEYEaWiAgz53rRSvWxyOLT4Ph6lq+BkHN/tWKjwQIGqNixAphFnw9YmQ== X-Received: by 2002:a05:6402:2993:b0:42a:b25c:c9ce with SMTP id eq19-20020a056402299300b0042ab25cc9cemr6284323edb.0.1654185886812; Thu, 02 Jun 2022 09:04:46 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a05:6402:294a:b0:42b:31b9:2087 with SMTP id ed10-20020a056402294a00b0042b31b92087ls2362369edb.2.gmail; Thu, 02 Jun 2022 09:04:46 -0700 (PDT) X-Received: by 2002:a05:6402:2945:b0:41d:aad:c824 with SMTP id ed5-20020a056402294500b0041d0aadc824mr6154092edb.364.1654185885738; Thu, 02 Jun 2022 09:04:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654185885; cv=none; d=google.com; s=arc-20160816; b=PbbPqXgwNYLgM9VrIJaX0/XLwwRMo12KB9M5RYNCA9R8PfL5vyIKId7NrLaOQBdSkG d5DhcT9wn6uHTmByaUpiOzJN1Rnah+I7OgWkbiCSYMnekcY2ufPBwl65Ub4NHLgnM1vi Z3qsz1GGPYdB1eqCOgZTlJx+Sb49YUJldFyKsSL4DYixQ2QD2GTbBAiNuOku7g7QnOKo x0BtJiVcLu/r3l+ABGaS0OgYjZnmyYGjkVoZZoWxFk0urYPCgVFIWpPrRUmodRix2iGM FWidMvwE92wUC/VYSW6udB4e9nhjMylZ1PU670swj3cuOqc2ys04VOuv9Dble6BTyxhF l7/g== 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=R4QHW2dToAuu+K8yYEpwrujBtMKvPKYmSGZ7+9+CQsQ=; b=hbKUg2FoC4WPpYYkhepfDvNAgRmzQDOpsrXCOALkFbdCkJw0yUxYLxMqznTV+Yo0OP 2R3hNmHs3ZeLzZdbjuVYqxu9wmApWUfu8rFbkqHUE5hq9tUCy1Xtw08bMS7NLYKwmUeP r6QZUPvlCzz4k0CIMjLjpYpMY3JCyOUZLMY6f6eiNuNp0bqEy+seVtUqceXksdP4zfwL RiVmqTB6yR7jkt1k8C7HtgRjOQYo26gOMYsStmyQCHdqgQFTuzvJynTQJMziooWtxtVn 4CJOeg+7Fd2QkmacBVDBQH7+y+mbGBp1JCKkbtSS8i1VUqVmiXAuMmtxU1zX7pv16Hgq t7Sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=XI3Pyioz; 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 ce7-20020a170906b24700b006fee98076f8sor2745514ejb.23.2022.06.02.09.04.45 for (Google Transport Security); Thu, 02 Jun 2022 09:04:45 -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:906:f905:b0:6fe:9242:f9ed with SMTP id lc5-20020a170906f90500b006fe9242f9edmr5090101ejb.78.1654185885308; Thu, 02 Jun 2022 09:04:45 -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 f3-20020a056402150300b0042dd3bf1403sm2637095edw.54.2022.06.02.09.04.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Jun 2022 09:04:44 -0700 (PDT) From: Dario Binacchi To: linux-amarula@amarulasolutions.com Cc: dario.binacchi@amarulasolutions.com, michael@amarulasolutions.com, tommaso.merciai@amarulasolutions.com Subject: [RFC PATCH v2 07/13] can: slcan: set bitrate by CAN device driver API Date: Thu, 2 Jun 2022 18:04:28 +0200 Message-Id: <20220602160434.225417-8-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220602160434.225417-1-dario.binacchi@amarulasolutions.com> References: <20220602160434.225417-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=XI3Pyioz; 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 --- (no changes since v1) 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);