From patchwork Wed Jun 8 16:51:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 2034 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 31E893F1F7 for ; Wed, 8 Jun 2022 18:51:35 +0200 (CEST) Received: by mail-ej1-f69.google.com with SMTP id v13-20020a170906b00d00b006f51e289f7csf9613150ejy.19 for ; Wed, 08 Jun 2022 09:51:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1654707095; cv=pass; d=google.com; s=arc-20160816; b=0BRAf402UGC/2imTAQoyE/wffUotN67oMlfP3hnb/mgSy14R4H2e/2JX28zZ3fD2CV g+uLtx5VL0axAZ/hfscmJMTVmcvjs0SLCDEZQm3ge+XozCE+/INm7arlrMqARdKTJ+zj m62iarKYkdu9Cmh1RUaSvXsWbjxcFtLjTTF/lTaLihuDBt1HWeaweMkx8ylEGMU/zHyn wq5HOGdNgjFmVmF0CgsIvTupg89Mf0N4xfffVKXCdd4DMWKX9DM7GJhQUqnRjvumNQnn DLEwqEDLiHKfld/w32J5JkJCJvluMep/lkKw/lLG2P2KrGVPJgg/7HShqQ3KKAkyVx6W 7SeQ== 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=msNajhLG122r6fMwXF5Zs4lvJK/uokjV5NpX/8Te3jo=; b=siNZX9+/o7uN7KwhHnNrX90Cnw4XCeDQpUDy7sBPRTejkiPbzI0cAmAlefZmEsVnHF 7lpO1NEOFqOSBG/YB6l+J3XRc2JQG18ozyoi4WfMfO0C9NcVpzfJTUBPgKSIK8HPHTNx 0BC9HbVDeN2bjshg9ph3QHFMYrRjHLtvZkUAeyXfGkBxmONn4zVIE5ohzMXhYPlMwGqI ZVlWMhvvJF9mTzWRIj7/9qJ+PF+KRgLClNz2ewQUv9fDFwEVEDMeqF9GOe+TPFGJkKpQ d2uvgfEphC6wY2rNSKjqj+nqn2hxuAR45X6EVLgR2H3DR/I1F3aFKEiH0CFfnrazJSgt UAgw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=orhsCgXR; 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=msNajhLG122r6fMwXF5Zs4lvJK/uokjV5NpX/8Te3jo=; b=mvo5yZllA+d+6XHMp7VIk/6Acouztm0PaaZSMp7k8lrRsgAahPTR5DRY2PDEsHHOBi ROhDOoSx7oGhMURkexrb8KwTMXyWmWSJh+w4J/cdpHLnms1/Lqr7hwzFoUmQ7JWPe6SU fQGH8mcpl/NOTuOkAhIFoPCoYiQlB0xRpfmM4= 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=msNajhLG122r6fMwXF5Zs4lvJK/uokjV5NpX/8Te3jo=; b=2fozzpQa+QxBxCLZEEiX9NvdaxgAYZMqtSgSD4H7FlbUAt8jtt5A1wTdqz/6mGsQmk /wkMK6MC73B+g7wrIGrewxCYeHyaQRXLafmzHhgVrK01zOIE6TysOF4uBOld8RFv/M5D jPYspJgTOz7/zSPcg5eKVaf4r/pu52Ljd95SEhLcyzLIjpUWQ0QbJD/hrxNPN9ZlDUau amcuI10NR+VpWRXf5VLyg6U314rRjk5CaEdHn49lsQuMe96C/+lF9LeF1gJBlhgYtZBQ WeE1Lyq9H4T6bzZGsp5jRT1tIyZ7m9s6KX86yCqVcHDPNDXyuQYgEAxzezNucfvNqNYt kN/A== X-Gm-Message-State: AOAM532c9ZmVL6SYtVvd1a0JIe4igOesRuo+S7dCRXuf9/gY5VFy7G5a HEK9OK6xL2Hij5rditsvbsDCVX4j X-Google-Smtp-Source: ABdhPJyesX0KA7NvuOSJjzyOG6b2lk7eJ3hdriKNqXnK2oCJQ6wnw3jlQNAxMsXEWYyIisdaB0rxeg== X-Received: by 2002:a17:907:3e1b:b0:6fe:e1a4:a331 with SMTP id hp27-20020a1709073e1b00b006fee1a4a331mr31957957ejc.72.1654707095010; Wed, 08 Jun 2022 09:51:35 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a05:6402:2741:b0:41f:7eee:e393 with SMTP id z1-20020a056402274100b0041f7eeee393ls188932edd.3.gmail; Wed, 08 Jun 2022 09:51:34 -0700 (PDT) X-Received: by 2002:a05:6402:c1:b0:42a:b8a5:8d5e with SMTP id i1-20020a05640200c100b0042ab8a58d5emr40657030edu.266.1654707093866; Wed, 08 Jun 2022 09:51:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654707093; cv=none; d=google.com; s=arc-20160816; b=Ji73G0Olb9MgLkyk6sXHzezBNLKIxkaEK6vF4xgqMVq/dZhXTRgcW3zKXwCcPt+9q/ O+DX2wphLUfj8Xy+MYM4AhEmWzL0/MYNOw1SyU+08ao5fwFG0OFdaYbQUBW3GU7Zsbtx QvKRnVIqolHVB+xFUVRtIEmWq8FLHrtjhpGEqW+1YiZWybd14AhMbwLtvMdH/1SE04Gj Z1H/muMXnnjbvoyJzmnfGjXA51QKGvsifYwNlgu5RiITGEOhlh22X8N1vc0xms9PUwse cK7Xc33HokZEnZyBiAbbR2kRMIQHGBpULa3QKIgrni8xUJrclqMyqo15HQvxstrA7RDF xyNQ== 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=5gQL+PBjKdFRbWGXqCko2W4l6SNrb34I5/OqJqVjfAw=; b=mTWEYiws28QbLxWKMpd7xVnNYvRzIL6gfwWUGftn4wrOJPf0dpBenZft+Dq1j4Zc99 BEEEZ4dGKC6O/Y//7FMr9gV7aQDJKoQOvElnttDx/8R3uPnbWqj9XkHiBSMoB6EyqmYh Hi9q8t9zk991LRtqsmfIIcG121WrktS6xEXCSEdVnNDDMXfE6AB2KW2YXUVLj3Z/Znbx OhdUq7B8Ds92OtX2AwUhn9o3KlvVeYmRfPm6PZmZUKhZJKdUyI5BdswEzV8BqcO2QWCa FB/vKFBYrNRlmkbJOneEj46s8Dt1KNChq8QPfF0HPsqmbHrc8ehl0izqYcsG/mz0xX02 i0jw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=orhsCgXR; 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 f14-20020a1709067f8e00b006fa8e1e0684sor9725462ejr.37.2022.06.08.09.51.33 for (Google Transport Security); Wed, 08 Jun 2022 09:51:33 -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:6d9b:b0:711:d26b:f5ba with SMTP id sb27-20020a1709076d9b00b00711d26bf5bamr13923231ejc.135.1654707093625; Wed, 08 Jun 2022 09:51:33 -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 c22-20020a17090654d600b0070587f81bcfsm9569071ejp.19.2022.06.08.09.51.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 09:51:33 -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: [PATCH v2 07/13] can: slcan: set bitrate by CAN device driver API Date: Wed, 8 Jun 2022 18:51:10 +0200 Message-Id: <20220608165116.1575390-8-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220608165116.1575390-1-dario.binacchi@amarulasolutions.com> References: <20220608165116.1575390-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=orhsCgXR; 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 v2: - Use the CAN framework support for setting fixed bit rates. drivers/net/can/slcan.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/drivers/net/can/slcan.c b/drivers/net/can/slcan.c index cab0a2a8c84c..8561bcee81ba 100644 --- a/drivers/net/can/slcan.c +++ b/drivers/net/can/slcan.c @@ -439,6 +439,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); @@ -460,7 +461,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"); @@ -558,6 +561,37 @@ static void slc_sync(void) } } +static const u32 slcan_bitrate_const[] = { + 10000, 20000, 50000, 100000, 125000, + 250000, 500000, 800000, 1000000 +}; + +static int slc_do_set_bittiming(struct net_device *dev) +{ + struct slcan *sl = netdev_priv(dev); + unsigned char cmd[SLC_MTU]; + int s, err; + + 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) { + 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) { @@ -587,6 +621,9 @@ 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); + 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);