Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp1404121iog; Tue, 14 Jun 2022 05:32:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyftakDTbYCGCny0adcQb4/sPLKWe49W2JFV3XZ/n8YeP3SNeV81GJLkAjwHoUIHhuXk/iw X-Received: by 2002:aa7:c508:0:b0:42d:cc6b:df80 with SMTP id o8-20020aa7c508000000b0042dcc6bdf80mr5805239edq.393.1655209969348; Tue, 14 Jun 2022 05:32:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655209969; cv=none; d=google.com; s=arc-20160816; b=y++417Tdsffb8ozia3xAaAIG0XVlEoWIxCrE0zv3/t5xxhIhbLQz0R3HmQLNZZALVH im+XBBpfEjJTrb42mQXcMHEttX0sifTWVnIINce4p3ahb5E5dddhyZw6ouhB6lMSrxf5 2m8QYD2WWoqL7gZu3WCX1bioFg94xwvNVFEU3ySS4Rxx5ieXm0FNhoknfMqBjazfYfD3 go0M+eCgq72bIXU2dZY5Hu4KEIUv6BLnII5XveGCUiS1uHY4eVaNhD5gcM2dYTNNvSh6 vc/qfkqiEMG6KkT5CZP/EHs7TTV5NYtIXU90aLHCsLywojIJ8ddnqCKW7/cRsJNk7ujj p0UQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=AbbDKExzTnDRI7gbQZlSxolycHVQn9x05Wxw1nhoVAE=; b=xF9NrPqM4qpsrVHh0JfHFgSfMoj0lu5XL9/xzVT//swcTInhTUPwh+p0jCdYO5BYGn vadGED7ESpE5/uV1AJrNKty6u8r5U6l5u9xZP9XayeBhXka60ErgFp1HSE172ShzL4vn E0fAcEFRh3QkMm12arSsbjw1rUd+YjMNio5+bJ43AvKyBp/IQ+QiAiStI7HfwG64j88u FfzvcTXhqGJxqc1WlOif1EkmhinOsl35mIYCCeNTZNQhExz6h67bpNAfudXa3CyF1khV Z/2y5d0SEwhj5vPLH+JMVZKE4RP3BDgD0Da7lbhjKUiKxpUpoMOmSpMQ+JPlAHe10bdv +bEQ== 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 linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o18-20020a056402439200b0042dd16020dasi12644727edc.216.2022.06.14.05.32.20; Tue, 14 Jun 2022 05:32:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b="Z/JIYpot"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356766AbiFNM3T (ORCPT + 99 others); Tue, 14 Jun 2022 08:29:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243679AbiFNM2j (ORCPT ); Tue, 14 Jun 2022 08:28:39 -0400 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 611F122BC5 for ; Tue, 14 Jun 2022 05:28:37 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id z7so11367057edm.13 for ; Tue, 14 Jun 2022 05:28:37 -0700 (PDT) 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; bh=AbbDKExzTnDRI7gbQZlSxolycHVQn9x05Wxw1nhoVAE=; b=Z/JIYpotFnOQ5zmb9SqzHVEaaztD0jqPK321kGQhBwM1TdUYaoJdCTkGfdGSfFJYZa frDZL3JcKyG8xgmE9rhb9P8I4rS5WPXeob6WzBVImIZ+gJyTtsCdBFxHgw6dJJo9fNTF qXCjtV+3R/aeZlAviiKig+1e0QRoK6hBQOzyE= 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; bh=AbbDKExzTnDRI7gbQZlSxolycHVQn9x05Wxw1nhoVAE=; b=Msmye378VVT1pwbMuo2tXr+FFU+SsLJ7VjqGwH1I1d+p9R3EwEbQGsWEfUjcFcT0H1 COTer7j+bQcn4bEstcfckH0MXNsaVVP5wDw+NqeOb2ZSUiSJd2ehh8qcObfCP756asl0 VSZFOLIgLDwadfXl7Jb4NYC6hnKsZC2zLp22OVL5lwlH6mXJNuzQ8lIn24UjaI3uKSTU jgB64Ge3BSq+4WjQDqOU0bD8CVJcCqVSgPoGUh58VtP2gDV3HwFV//3j6dYsYkJGSD5a zLdPjrrworDmgHFzhON7+102BJLg5qf2LoPZbRT8aEZJWIYlrZ3n+bpLP0G4b8MNFUDp wt9A== X-Gm-Message-State: AOAM532rqurXDh+IYiolWl24+w9QIEODDT6MEidMumgSM1Kiyd+uFKpo ZGu5xNZoHETcU5MR96lvld0Mvii9cIIasA== 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 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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); -- 2.32.0