From: Jiri Slaby <jirislaby@kernel.org>
To: Christoph Fritz <christoph.fritz@hexdev.de>,
Oliver Hartkopp <socketcan@hartkopp.net>,
Marc Kleine-Budde <mkl@pengutronix.de>,
Vincent Mailhol <mailhol.vincent@wanadoo.fr>,
"David S . Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
Jiri Kosina <jikos@kernel.org>,
Benjamin Tissoires <bentiss@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Sebastian Reichel <sre@kernel.org>,
Linus Walleij <linus.walleij@linaro.org>
Cc: Andreas Lauser <andreas.lauser@mercedes-benz.com>,
Jonathan Corbet <corbet@lwn.net>,
Pavel Pisa <pisa@cmp.felk.cvut.cz>,
linux-can@vger.kernel.org, netdev@vger.kernel.org,
devicetree@vger.kernel.org, linux-input@vger.kernel.org,
linux-serial@vger.kernel.org
Subject: Re: [PATCH v2 07/12] can: Add support for serdev LIN adapters
Date: Thu, 2 May 2024 10:44:46 +0200 [thread overview]
Message-ID: <6ae3c1af-4368-4a3e-bfb5-366080048dac@kernel.org> (raw)
In-Reply-To: <20240502075534.882628-8-christoph.fritz@hexdev.de>
On 02. 05. 24, 9:55, Christoph Fritz wrote:
> This commit introduces LIN-Bus support for UART devices equipped with
> LIN transceivers, utilizing the Serial Device Bus (serdev) interface.
>
> For more details on an adapter, visit: https://hexdev.de/hexlin#tty
...
> --- /dev/null
> +++ b/drivers/net/can/lin-serdev.c
> @@ -0,0 +1,514 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/* Copyright (C) 2024 hexDEV GmbH - https://hexdev.de */
> +
> +#include <linux/module.h>
> +#include <linux/wait.h>
> +#include <linux/init.h>
> +#include <linux/errno.h>
> +#include <linux/string.h>
> +#include <linux/kernel.h>
What do you need kernel.h for? You should explicitly require what you
need (you apparently do), so kernel.h should not be needed.
> +#include <net/lin.h>
> +#include <linux/of.h>
> +#include <linux/serdev.h>
> +#include <linux/slab.h>
> +#include <linux/kfifo.h>
> +#include <linux/workqueue.h>
> +#include <linux/tty.h>
Might be eaier to maintain if you sort them.
> +#define LINSER_SAMPLES_PER_CHAR 10
> +#define LINSER_TX_BUFFER_SIZE 11
> +#define LINSER_RX_FIFO_SIZE 256
> +#define LINSER_PARSE_BUFFER 24
> +
> +struct linser_rx {
> + u8 data;
> + u8 flag;
> +};
> +
> +enum linser_rx_status {
> + NEED_MORE = -1,
> + MODE_OK = 0,
> + NEED_FORCE,
> +};
> +
> +struct linser_priv {
> + struct lin_device *lin_dev;
> + struct serdev_device *serdev;
> + DECLARE_KFIFO_PTR(rx_fifo, struct linser_rx);
> + struct delayed_work rx_work;
> + ulong break_usleep_min;
> + ulong break_usleep_max;
> + ulong post_break_usleep_min;
> + ulong post_break_usleep_max;
> + ulong force_timeout_jfs;
The same as for uint :)
> + struct lin_responder_answer respond_answ[LIN_NUM_IDS];
> + struct mutex resp_lock; /* protects respond_answ */
> + bool is_stopped;
> +};
...
> +static void linser_derive_timings(struct linser_priv *priv, u16 bitrate)
> +{
> + unsigned long break_baud = (bitrate * 2) / 3;
> + unsigned long timeout_us;
> +
Are those 1000000UL USEC_PER_SEC?
> + priv->break_usleep_min = (1000000UL * LINSER_SAMPLES_PER_CHAR) /
> + break_baud;
> + priv->break_usleep_max = priv->break_usleep_min + 50;
> + priv->post_break_usleep_min = (1000000UL * 1 /* 1 bit */) / break_baud;
> + priv->post_break_usleep_max = priv->post_break_usleep_min + 30;
> +
> + timeout_us = DIV_ROUND_CLOSEST(1000000UL * 256 /* bit */, bitrate);
> + priv->force_timeout_jfs = usecs_to_jiffies(timeout_us);
> +}
...
> +static bool linser_tx_frame_as_responder(struct linser_priv *priv, u8 id)
> +{
> + struct lin_responder_answer *answ = &priv->respond_answ[id];
> + struct serdev_device *serdev = priv->serdev;
> + u8 buf[LINSER_TX_BUFFER_SIZE];
> + u8 checksum, count, n;
> + ssize_t write_len;
> +
> + mutex_lock(&priv->resp_lock);
> +
> + if (!answ->is_active)
> + goto unlock_and_exit_false;
> +
> + if (answ->is_event_frame) {
> + struct lin_responder_answer *e_answ;
> +
> + e_answ = &priv->respond_answ[answ->event_associated_id];
> + n = min(e_answ->lf.len, LIN_MAX_DLEN);
> + if (memcmp(answ->lf.data, e_answ->lf.data, n) != 0) {
> + memcpy(answ->lf.data, e_answ->lf.data, n);
> + checksum = lin_get_checksum(LIN_FORM_PID(answ->lf.lin_id),
> + n, e_answ->lf.data,
> + answ->lf.checksum_mode);
> + answ = e_answ;
> + } else {
> + goto unlock_and_exit_false;
Can't you simply use guard(mutex) above and avoid the error-prone
gotos/cleanup completely?
> + }
> + } else {
> + checksum = answ->lf.checksum;
> + }
> +
> + count = min(answ->lf.len, LIN_MAX_DLEN);
> + memcpy(&buf[0], answ->lf.data, count);
> + buf[count] = checksum;
> +
> + mutex_unlock(&priv->resp_lock);
> +
> + write_len = serdev_device_write(serdev, buf, count + 1, 0);
> + if (write_len < count + 1)
> + return false;
> +
> + serdev_device_wait_until_sent(serdev, 0);
> +
> + return true;
> +
> +unlock_and_exit_false:
> + mutex_unlock(&priv->resp_lock);
> + return false;
> +}
> +
> +static void linser_pop_fifo(struct linser_priv *priv, size_t n)
> +{
> + struct serdev_device *serdev = priv->serdev;
> + struct linser_rx dummy;
> + size_t ret, i;
> +
> + for (i = 0; i < n; i++) {
> + ret = kfifo_out(&priv->rx_fifo, &dummy, 1);
Does kfifo_skip() not work for records? (I added it recently for serial.)
> + if (ret != 1) {
> + dev_err(&serdev->dev, "Failed to pop from FIFO\n");
> + break;
> + }
> + }
> +}
thanks,
--
js
suse labs
next prev parent reply other threads:[~2024-05-02 8:44 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-02 7:55 [PATCH v2 00/12] LIN Bus support for Linux Christoph Fritz
2024-05-02 7:55 ` [PATCH v2 01/12] can: Add LIN bus as CAN abstraction Christoph Fritz
2024-05-04 12:49 ` Simon Horman
2024-05-08 8:37 ` Christoph Fritz
2024-05-02 7:55 ` [PATCH v2 02/12] HID: hexLIN: Add support for USB LIN bus adapter Christoph Fritz
2024-05-02 8:30 ` Jiri Slaby
2024-05-02 10:41 ` Christoph Fritz
2024-05-04 12:58 ` Simon Horman
2024-05-08 8:37 ` Christoph Fritz
2024-05-02 7:55 ` [PATCH v2 03/12] tty: serdev: Add flag buffer aware receive_buf_fp() Christoph Fritz
2024-05-02 7:55 ` [PATCH v2 04/12] tty: serdev: Add method to enable break flags Christoph Fritz
2024-05-02 7:55 ` [PATCH v2 05/12] dt-bindings: vendor-prefixes: Add hexDEV Christoph Fritz
2024-05-02 8:56 ` Krzysztof Kozlowski
2024-05-02 7:55 ` [PATCH v2 06/12] dt-bindings: net/can: Add serial (serdev) LIN adapter Christoph Fritz
2024-05-02 9:01 ` Krzysztof Kozlowski
2024-05-02 9:31 ` Rob Herring (Arm)
2024-05-02 11:03 ` Christoph Fritz
2024-05-02 15:03 ` Rob Herring
2024-05-02 7:55 ` [PATCH v2 07/12] can: Add support for serdev LIN adapters Christoph Fritz
2024-05-02 8:44 ` Jiri Slaby [this message]
2024-05-02 18:19 ` Christoph Fritz
2024-05-04 13:13 ` Simon Horman
2024-05-08 8:37 ` Christoph Fritz
2024-05-02 7:55 ` [PATCH v2 08/12] can: lin: Add special frame id for rx offload config Christoph Fritz
2024-05-02 10:27 ` Krzysztof Kozlowski
2024-05-02 17:58 ` Christoph Fritz
2024-05-02 7:55 ` [PATCH v2 09/12] can: bcm: Add LIN answer offloading for responder mode Christoph Fritz
2024-05-02 7:55 ` [PATCH v2 10/12] can: lin: Handle rx offload config frames Christoph Fritz
2024-05-02 7:55 ` [PATCH v2 11/12] can: lin: Support setting LIN mode Christoph Fritz
2024-05-02 7:55 ` [PATCH v2 12/12] HID: hexLIN: Implement ability to update lin mode Christoph Fritz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6ae3c1af-4368-4a3e-bfb5-366080048dac@kernel.org \
--to=jirislaby@kernel.org \
--cc=andreas.lauser@mercedes-benz.com \
--cc=bentiss@kernel.org \
--cc=christoph.fritz@hexdev.de \
--cc=conor+dt@kernel.org \
--cc=corbet@lwn.net \
--cc=davem@davemloft.net \
--cc=devicetree@vger.kernel.org \
--cc=edumazet@google.com \
--cc=gregkh@linuxfoundation.org \
--cc=jikos@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=kuba@kernel.org \
--cc=linus.walleij@linaro.org \
--cc=linux-can@vger.kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=mailhol.vincent@wanadoo.fr \
--cc=mkl@pengutronix.de \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=pisa@cmp.felk.cvut.cz \
--cc=robh@kernel.org \
--cc=socketcan@hartkopp.net \
--cc=sre@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox