Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2689051iof; Wed, 8 Jun 2022 09:59:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwcLYNAiXui5gPQwgciIyrHcJEOI9FJWAJBMETyEEIjvkO6S63+cWbtdZay1WEOkVXVXI3R X-Received: by 2002:a17:90b:4b51:b0:1e8:71cb:4d18 with SMTP id mi17-20020a17090b4b5100b001e871cb4d18mr166126pjb.108.1654707548870; Wed, 08 Jun 2022 09:59:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654707548; cv=none; d=google.com; s=arc-20160816; b=GjV58lwEaaUK9136GlHwyhxYXu/xX7XifBBHM87VFt+P3iMCawIaMjjfAXHw77DuLx rz0psqSwNavPAOugy+ionSFbeqvQRrw8zUCiu6KcP0f2k0z+wacXKEBEHnepog2kk1cI dJ+mkohirBfKuW1idKcWKGUD6vTL1FE9VZCs8WKzk/Nowmk9Ji1RJhVsn2WImOzN1Ke1 Rh8Zpo48Bhwlp3Pntg+t4yJ4Ro9xaAnP+ozwHnPkIFiocF+lfnhlPyu5lVASDUuwBzDu cYAxBYB7mf7f4jk1crlJsZCb39TllpqXWpsjyP1I9I+gTwi1cFDTwWQBOwb9c4x11Z8/ kIvA== 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=5gQL+PBjKdFRbWGXqCko2W4l6SNrb34I5/OqJqVjfAw=; b=Pmwg6Kr3a4d5iILfjVaO3VYpBwFhJNE2I3v1A3VYz/QSzQ5VzXc2ZlMRrJ+n25Pp3S A/4jzWk6/uO7xMdnGmTu7HDQM8t0bkx0104pM2wkL2QPcdiEmO4rZoiUmE4aytj1UaP3 9JRyx8Z34vyQwLlNY3R1/Q/enQpnI1lDxJRSnShg4wG2iPhOvN+PduklryK6CDdu8SdI 6wR5Y1k+hbZAcFBDs+1SkNlMmv7YsJ0UJUDvxtClsNrCpXGIqeouwooCicbcM6TlzBmC rzMmdZnk5rzGp91YWaHsu6md+6iZ3lpFmoyhnDa1JAyZ6fxo+I83P5r1hnJe7zofODhN gXjQ== 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 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 ip4-20020a17090b314400b001e82bdf4718si24002402pjb.3.2022.06.08.09.58.33; Wed, 08 Jun 2022 09:59:08 -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=orhsCgXR; 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 S231357AbiFHQ4v (ORCPT + 99 others); Wed, 8 Jun 2022 12:56:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343805AbiFHQyM (ORCPT ); Wed, 8 Jun 2022 12:54:12 -0400 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B90A3C8810 for ; Wed, 8 Jun 2022 09:51:35 -0700 (PDT) Received: by mail-ej1-x62b.google.com with SMTP id u12so42644593eja.8 for ; Wed, 08 Jun 2022 09:51:35 -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=5gQL+PBjKdFRbWGXqCko2W4l6SNrb34I5/OqJqVjfAw=; b=orhsCgXR3r1BmMaiVIHHK+Nuz2maix0QOkibEQtFMj7JRULKjQ2TmKUbzmsWiWQ11f 21BhhDfSH1+my9PR/jKLCnwF+cYEaKsxeuQPyHr8hcVFqIz/2yMZeN3Es3ervF0Xs9D8 1LHxBlsXpg62lhf6PtkztHFd5z6kFnbmH94kM= 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=5gQL+PBjKdFRbWGXqCko2W4l6SNrb34I5/OqJqVjfAw=; b=HI3xPEtlB4mv2cZ62u3ijdfQIRu5MBnCwpEV6VQsAFbNMEbWVIdOiAkRTRh/23u2si jRAvDi4xohakD5nQEcPEkHdLDJuAhjoyCcyg4KbTsdunxCbXMesWqhSe9DFGiFftyCz/ 62RNT4u1N1uAtfMfm6Q/5pFV0OG5C3xJv8MgAXfRf60KR/DVxv63hfpIMICu4SwwQP9N z4KZIvm4or8BEUFhf6PvtqCJfeMPPTXEaTLvt7QwdPmecSvqBYtSLXuo6c0zPnIIuoby rUoYHiWGx60d4+XWO3hgW+463wfMBSlRY78hV9OlJ3PraknytqWYQs9QluBJkZsOcgPj actQ== X-Gm-Message-State: AOAM530H+WSoueHunZJU5F4oeXlLSeL1yrfn9nq+5YuyDQ07mYXSbSMf pzniwQZX+113QqGxcXl/Yp0AnwAdXmxf2g== 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 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 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); -- 2.32.0