Skip to contentSkip to conversation

LIN Bus support for Linux

PATCH Superseded Christoph Fritz 5 participants 30 messages May 2, 2024 – May 8, 2024

Subsystems (8)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover LIN Bus support for Linux

This series is introducing basic Local Interconnect Network (LIN) (ISO 17987) [0] support to the Linux kernel, along with two drivers that make use of it: An advanced USB adapter and a lightweight serdev driver (for UARTs equipped with a LIN transceiver).

The LIN bus is common in the automotive industry for connecting low-level devices like side mirrors, seats, ambient lights, etc.

The LIN bus is a lower-cost bus system with a subset of features of CAN. Its earlier specification (before ISO) is publicly accessible [1].

This series of patches follows up on a discussion initiated by an RFC patch series [2].

The core of this series is the first patch, which implements the CAN_LIN glue driver. It basically utilizes the CAN interface on one side and for device drivers on the other side it creates a rx() function and several callbacks.

This approach is non-invasive, as LIN frames (nearly identical to CAN frames) are just treated as a special case of CAN frames. This approach eliminates the need for a separate API for LIN, allowing the use of existing CAN tools, including the CAN broadcast manager.

For the responder part of LIN, when a device responds to a controller request, it can reply on up to LIN its 64 possible IDs (0...63) with a maximum of 8 bytes payload. The response must be sent relatively quickly, so offloading is used (which is used by most devices anyway). Devices that do not support offloading (like the lightweight serdev) handle the list of responses in the driver on a best-effort basis.

The CAN broadcast manager (bcm) makes a good interface for the LIN userland interface, bcm is therefore enhanced to handle the configuration of these offload RX frames, so that the device can handle the response on its own. As a basic alternative, a sysfs file per LIN identifier gets also introduced.

The USB device driver for the hexLIN [3] adapter uses the HID protocol and is located in the drivers/hid directory. Which is a bit uncommon for a CAN device, but this is a LIN device and mainly a hid driver (and all hid drivers go into drivers/hid).

The other driver, the UART lin-serdev driver requires support for break detection, this is addressed by two serdev patches.

The lin-serdev driver has been tested on an ARM SoC, on its uart (uart-pl011) an adapter board (hexLIN-tty [4]) has been used. As a sidenote, in that tty serial driver (amba-pl011.c) it was necessary to disable DMA_ENGINE to accurately detect breaks [5].

The functions for generating LIN-Breaks and checksums, originally from a line discipline driver named sllin [6], have been adopted into the lin-serdev driver.

To make use of the LIN mode configuration (commander or responder) option, a patch for iproute2 [7] has been made.

The lin-utils [8] provide userland tools for reference, testing, and evaluation. These utilities are currently separate but may potentially be integrated into can-utils in the future.

[0]: https://en.wikipedia.org/wiki/Local_Interconnect_Network
[1]: https://www.lin-cia.org/fileadmin/microsites/lin-cia.org/resources/documents/LIN_2.2A.pdf
[2]: https://lwn.net/Articles/916049/
[3]: https://hexdev.de/hexlin
[4]: https://hexdev.de/hexlin#tty
[5]: https://github.com/raspberrypi/linux/issues/5985
[6]: https://github.com/lin-bus/linux-lin/blob/master/sllin/sllin.c
[7]: https://github.com/ch-f/iproute2/tree/lin-feature
[8]: https://github.com/ch-f/lin-utils

Changes in v2:
- add open/stop functions to also address teardown issues
- adapt dt-bindings description and add hexdev
- use 'unsigned int' instead of 'uint'
- add and adapt macros
- address review comments

Christoph Fritz (12):
can: Add LIN bus as CAN abstraction
HID: hexLIN: Add support for USB LIN bus adapter
tty: serdev: Add flag buffer aware receive_buf_fp()
tty: serdev: Add method to enable break flags
dt-bindings: vendor-prefixes: Add hexDEV
dt-bindings: net/can: Add serial (serdev) LIN adapter
can: Add support for serdev LIN adapters
can: lin: Add special frame id for rx offload config
can: bcm: Add LIN answer offloading for responder mode
can: lin: Handle rx offload config frames
can: lin: Support setting LIN mode
HID: hexLIN: Implement ability to update lin mode

 .../bindings/net/can/hexdev,lin-serdev.yaml   |  32 +
 .../devicetree/bindings/vendor-prefixes.yaml  |   2 +
 drivers/hid/Kconfig                           |  19 +
 drivers/hid/Makefile                          |   1 +
 drivers/hid/hid-hexdev-hexlin.c               | 641 ++++++++++++++++++
 drivers/hid/hid-ids.h                         |   1 +
 drivers/hid/hid-quirks.c                      |   3 +
 drivers/net/can/Kconfig                       |  26 +
 drivers/net/can/Makefile                      |   2 +
 drivers/net/can/lin-serdev.c                  | 514 ++++++++++++++
 drivers/net/can/lin.c                         | 562 +++++++++++++++
 drivers/tty/serdev/core.c                     |  11 +
 drivers/tty/serdev/serdev-ttyport.c           |  19 +-
 include/linux/serdev.h                        |  19 +-
 include/net/lin.h                             |  99 +++
 include/uapi/linux/can/bcm.h                  |   5 +-
 include/uapi/linux/can/netlink.h              |   2 +
 net/can/bcm.c                                 |  74 +-
 18 files changed, 2026 insertions(+), 6 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.yaml
 create mode 100644 drivers/hid/hid-hexdev-hexlin.c
 create mode 100644 drivers/net/can/lin-serdev.c
 create mode 100644 drivers/net/can/lin.c
 create mode 100644 include/net/lin.h
Signature · 1 line
2.39.2
#1 lore raw

PATCH v2 1/12 can: Add LIN bus as CAN abstraction

This patch adds a LIN (local interconnect network) bus abstraction on top of CAN. It is a glue driver adapting CAN on one side while offering LIN abstraction on the other side. So that upcoming LIN device drivers can make use of it.

---
 drivers/net/can/Kconfig          |  10 +
 drivers/net/can/Makefile         |   1 +
 drivers/net/can/lin.c            | 495 +++++++++++++++++++++++++++++++
 include/net/lin.h                |  91 ++++++
 include/uapi/linux/can/netlink.h |   1 +
 5 files changed, 598 insertions(+)
 create mode 100644 drivers/net/can/lin.c
 create mode 100644 include/net/lin.h
drivers/net/can/Kconfig +10 -0
index 2e31db55d9278..0934bbf8d03b2 100644
@@ -171,6 +171,16 @@ config CAN_KVASER_PCIEFD
 	    Kvaser M.2 PCIe 4xCAN
 	    Kvaser PCIe 8xCAN
 
+config CAN_LIN
+	tristate "LIN mode support"
+	help
+	  This is a glue driver for LIN-BUS support.
+
+	  The local interconnect (LIN) bus is a simple bus with a feature
+	  subset of CAN. It is often combined with CAN for simple controls.
+
+	  Actual device drivers need to be enabled too.
+
 config CAN_SLCAN
 	tristate "Serial / USB serial CAN Adaptors (slcan)"
 	depends on TTY
drivers/net/can/Makefile +1 -0
index 4669cd51e7bf5..0093ee9219ca8 100644
@@ -25,6 +25,7 @@ obj-$(CONFIG_CAN_GRCAN)		+= grcan.o
 obj-$(CONFIG_CAN_IFI_CANFD)	+= ifi_canfd/
 obj-$(CONFIG_CAN_JANZ_ICAN3)	+= janz-ican3.o
 obj-$(CONFIG_CAN_KVASER_PCIEFD)	+= kvaser_pciefd.o
+obj-$(CONFIG_CAN_LIN)		+= lin.o
 obj-$(CONFIG_CAN_MSCAN)		+= mscan/
 obj-$(CONFIG_CAN_M_CAN)		+= m_can/
 obj-$(CONFIG_CAN_PEAK_PCIEFD)	+= peak_canfd/
drivers/net/can/lin.c new file +495 -0

495 lines not on this page · raw message

include/net/lin.h new file +91 -0

91 lines not on this page · raw message

include/uapi/linux/can/netlink.h +1 -0
index 02ec32d694742..51b0e2a7624e4 100644
@@ -103,6 +103,7 @@ struct can_ctrlmode {
 #define CAN_CTRLMODE_CC_LEN8_DLC	0x100	/* Classic CAN DLC option */
 #define CAN_CTRLMODE_TDC_AUTO		0x200	/* CAN transiver automatically calculates TDCV */
 #define CAN_CTRLMODE_TDC_MANUAL		0x400	/* TDCV is manually set up by user */
+#define CAN_CTRLMODE_LIN		0x800	/* LIN bus mode */
 
 /*
  * CAN device statistics
Signature · 1 line
2.39.2
reviewer
#2 lore raw

Re: [PATCH v2 01/12] can: Add LIN bus as CAN abstraction

Quoted 7 lines · Christoph Fritz

This patch adds a LIN (local interconnect network) bus abstraction on top of CAN. It is a glue driver adapting CAN on one side while offering LIN abstraction on the other side. So that upcoming LIN device drivers can make use of it.

...

Quoted 1 line
drivers/net/can/lin.c +0 -0

...

drivers/net/can/lin.c line 409

Quoted 10 lines
drivers/net/can/lin.c +10 -0
+struct lin_device *register_lin(struct device *dev,
+				const struct lin_device_ops *ldops)
+{
+	struct net_device *ndev;
+	struct lin_device *ldev;
+	int ret;
+
+	if (!ldops || !ldops->ldo_tx || !ldops->update_bitrate  ||
+	    !ldops->ldo_open || !ldops->ldo_stop) {
+		netdev_err(ndev, "missing mandatory lin_device_ops\n");

Hi Christoph,

The line above uses ndev, but ndev is not initialised until a few lines further down.

Flagged by Smatch.

Quoted 42 lines
drivers/net/can/lin.c +42 -0
+		return ERR_PTR(-EINVAL);
+	}
+
+	ndev = alloc_candev(sizeof(struct lin_device), 1);
+	if (!ndev)
+		return ERR_PTR(-ENOMEM);
+
+	ldev = netdev_priv(ndev);
+
+	ldev->ldev_ops = ldops;
+	ndev->netdev_ops = &lin_netdev_ops;
+	ndev->flags |= IFF_ECHO;
+	ndev->mtu = CANFD_MTU;
+	ldev->can.bittiming.bitrate = LIN_DEFAULT_BAUDRATE;
+	ldev->can.ctrlmode = CAN_CTRLMODE_LIN;
+	ldev->can.ctrlmode_supported = 0;
+	ldev->can.bitrate_const = lin_bitrate;
+	ldev->can.bitrate_const_cnt = ARRAY_SIZE(lin_bitrate);
+	ldev->can.do_set_bittiming = lin_set_bittiming;
+	ldev->ndev = ndev;
+	ldev->dev = dev;
+
+	SET_NETDEV_DEV(ndev, dev);
+
+	ret = lin_set_bittiming(ndev);
+	if (ret) {
+		netdev_err(ndev, "set bittiming failed\n");
+		goto exit_candev;
+	}
+
+	ret = register_candev(ndev);
+	if (ret)
+		goto exit_candev;
+
+	ldev->lin_ids_kobj = kobject_create_and_add("lin_ids", &ndev->dev.kobj);
+	if (!ldev->lin_ids_kobj) {
+		netdev_err(ndev, "Failed to create sysfs directory\n");
+		ret = -ENOMEM;
+		goto exit_unreg;
+	}
+
+	ret = lin_create_sysfs_id_files(ndev);

drivers/net/can/lin.c line 461

Quoted 10 lines
drivers/net/can/lin.c +10 -0
+	if (ret) {
+		netdev_err(ndev, "Failed to create sysfs entry: %d\n", ret);
+		goto exit_kobj_put;
+	}
+
+	/* Using workqueue as tx over USB/SPI/... may sleep */
+	ldev->wq = alloc_workqueue(dev_name(dev), WQ_FREEZABLE | WQ_MEM_RECLAIM,
+				   0);
+	if (!ldev->wq)
+		goto exit_rm_files;

The goto above will result in: return ERR_PTR(ret) But ret is 0 here. Should it be set to a negative error value?

Also flagged by Smatch.

Quoted 18 lines
drivers/net/can/lin.c +18 -0
+
+	INIT_WORK(&ldev->tx_work, lin_tx_work_handler);
+
+	netdev_info(ndev, "LIN initialized.\n");
+
+	return ldev;
+
+exit_rm_files:
+	lin_remove_sysfs_id_files(ndev);
+exit_kobj_put:
+	kobject_put(ldev->lin_ids_kobj);
+exit_unreg:
+	unregister_candev(ndev);
+exit_candev:
+	free_candev(ndev);
+	return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(register_lin);

...

#3 lore raw

Re: [PATCH v2 01/12] can: Add LIN bus as CAN abstraction

Quoted 15 lines · Simon Horman
Quoted 7 lines · Christoph Fritz

This patch adds a LIN (local interconnect network) bus abstraction on top of CAN. It is a glue driver adapting CAN on one side while offering LIN abstraction on the other side. So that upcoming LIN device drivers can make use of it.

...

Quoted 1 line
drivers/net/can/lin.c +0 -0

...

Quoted 12 lines

drivers/net/can/lin.c line 409

Quoted 10 lines
drivers/net/can/lin.c +10 -0
+struct lin_device *register_lin(struct device *dev,
+				const struct lin_device_ops *ldops)
+{
+	struct net_device *ndev;
+	struct lin_device *ldev;
+	int ret;
+
+	if (!ldops || !ldops->ldo_tx || !ldops->update_bitrate  ||
+	    !ldops->ldo_open || !ldops->ldo_stop) {
+		netdev_err(ndev, "missing mandatory lin_device_ops\n");

Hi Christoph,

Hi Simon

Quoted 4 lines

The line above uses ndev, but ndev is not initialised until a few lines further down.

Flagged by Smatch.

Despite netdev_err() checks validity of ndev, I agree with Smatch: In upcoming v4 I'll use dev_err() here instead.

Quoted 56 lines
Quoted 42 lines
drivers/net/can/lin.c +42 -0
+		return ERR_PTR(-EINVAL);
+	}
+
+	ndev = alloc_candev(sizeof(struct lin_device), 1);
+	if (!ndev)
+		return ERR_PTR(-ENOMEM);
+
+	ldev = netdev_priv(ndev);
+
+	ldev->ldev_ops = ldops;
+	ndev->netdev_ops = &lin_netdev_ops;
+	ndev->flags |= IFF_ECHO;
+	ndev->mtu = CANFD_MTU;
+	ldev->can.bittiming.bitrate = LIN_DEFAULT_BAUDRATE;
+	ldev->can.ctrlmode = CAN_CTRLMODE_LIN;
+	ldev->can.ctrlmode_supported = 0;
+	ldev->can.bitrate_const = lin_bitrate;
+	ldev->can.bitrate_const_cnt = ARRAY_SIZE(lin_bitrate);
+	ldev->can.do_set_bittiming = lin_set_bittiming;
+	ldev->ndev = ndev;
+	ldev->dev = dev;
+
+	SET_NETDEV_DEV(ndev, dev);
+
+	ret = lin_set_bittiming(ndev);
+	if (ret) {
+		netdev_err(ndev, "set bittiming failed\n");
+		goto exit_candev;
+	}
+
+	ret = register_candev(ndev);
+	if (ret)
+		goto exit_candev;
+
+	ldev->lin_ids_kobj = kobject_create_and_add("lin_ids", &ndev->dev.kobj);
+	if (!ldev->lin_ids_kobj) {
+		netdev_err(ndev, "Failed to create sysfs directory\n");
+		ret = -ENOMEM;
+		goto exit_unreg;
+	}
+
+	ret = lin_create_sysfs_id_files(ndev);

drivers/net/can/lin.c line 461

Quoted 10 lines
drivers/net/can/lin.c +10 -0
+	if (ret) {
+		netdev_err(ndev, "Failed to create sysfs entry: %d\n", ret);
+		goto exit_kobj_put;
+	}
+
+	/* Using workqueue as tx over USB/SPI/... may sleep */
+	ldev->wq = alloc_workqueue(dev_name(dev), WQ_FREEZABLE | WQ_MEM_RECLAIM,
+				   0);
+	if (!ldev->wq)
+		goto exit_rm_files;

The goto above will result in: return ERR_PTR(ret) But ret is 0 here. Should it be set to a negative error value?

Quoted 1 line

Also flagged by Smatch.

OK, will get an

ret = -ENOMEM;

Quoted 13 lines
Quoted 13 lines
drivers/net/can/lin.c +12 -0
+
+	INIT_WORK(&ldev->tx_work, lin_tx_work_handler);
+
+	netdev_info(ndev, "LIN initialized.\n");
+
+	return ldev;
+
+exit_rm_files:
+	lin_remove_sysfs_id_files(ndev);
+exit_kobj_put:
+	kobject_put(ldev->lin_ids_kobj);
+exit_unreg:
Quoted 8 lines
Quoted 6 lines
drivers/net/can/lin.c +6 -0
+	unregister_candev(ndev);
+exit_candev:
+	free_candev(ndev);
+	return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(register_lin);

...

Thanks
-- Christoph

#4 lore raw

PATCH v2 2/12 HID: hexLIN: Add support for USB LIN bus adapter

In reply to Christoph Fritz

This patch introduces driver support for the hexLIN USB LIN bus adapter, enabling LIN communication over USB for both controller and responder modes. The driver interfaces with the CAN_LIN framework for userland connectivity.

For more details on the adapter, visit: https://hexdev.de/hexlin/

---
 drivers/hid/Kconfig             |  19 +
 drivers/hid/Makefile            |   1 +
 drivers/hid/hid-hexdev-hexlin.c | 630 ++++++++++++++++++++++++++++++++
 drivers/hid/hid-ids.h           |   1 +
 drivers/hid/hid-quirks.c        |   3 +
 5 files changed, 654 insertions(+)
 create mode 100644 drivers/hid/hid-hexdev-hexlin.c
drivers/hid/Kconfig +19 -0
index 4c682c6507040..4caff63d9dcda 100644
@@ -496,6 +496,25 @@ config HID_GYRATION
 	help
 	Support for Gyration remote control.
 
+config HID_MCS_HEXDEV
+	tristate "hexDEV LIN-BUS adapter support"
+	depends on HID
+	select CAN_LIN
+	help
+	  Support for hexDEV its hexLIN USB LIN bus adapter.
+
+	  Local Interconnect Network (LIN) to USB adapter for controller and
+	  responder usage.
+	  This device driver is using CAN_LIN for a userland connection on
+	  one side and USB HID for the actual hardware adapter on the other
+	  side.
+
+	  If you have such an adapter, say Y here and see
+	  <https://hexdev.de/hexlin>.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called hid-hexlin.
+
 config HID_ICADE
 	tristate "ION iCade arcade controller"
 	help
drivers/hid/Makefile +1 -0
index 082a728eac600..f9b13e6117e60 100644
@@ -59,6 +59,7 @@ obj-$(CONFIG_HID_GOOGLE_STADIA_FF)	+= hid-google-stadiaff.o
 obj-$(CONFIG_HID_VIVALDI)	+= hid-vivaldi.o
 obj-$(CONFIG_HID_GT683R)	+= hid-gt683r.o
 obj-$(CONFIG_HID_GYRATION)	+= hid-gyration.o
+obj-$(CONFIG_HID_MCS_HEXDEV)	+= hid-hexdev-hexlin.o
 obj-$(CONFIG_HID_HOLTEK)	+= hid-holtek-kbd.o
 obj-$(CONFIG_HID_HOLTEK)	+= hid-holtek-mouse.o
 obj-$(CONFIG_HID_HOLTEK)	+= hid-holtekff.o
drivers/hid/hid-hexdev-hexlin.c new file +630 -0

630 lines not on this page · raw message

drivers/hid/hid-ids.h +1 -0
index 64164423b592b..8f46d37c2b499 100644
@@ -907,6 +907,7 @@
 
 #define USB_VENDOR_ID_MCS		0x16d0
 #define USB_DEVICE_ID_MCS_GAMEPADBLOCK	0x0bcc
+#define USB_DEVICE_ID_MCS_HEXDEV_HEXLIN	0x0648
 
 #define USB_VENDOR_MEGAWORLD		0x07b5
 #define USB_DEVICE_ID_MEGAWORLD_GAMEPAD	0x0312
drivers/hid/hid-quirks.c +3 -0
index 1d1949d62dfaf..a514449c50047 100644
@@ -438,6 +438,9 @@ static const struct hid_device_id hid_have_special_driver[] = {
 	{ HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_2) },
 	{ HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_3) },
 #endif
+#if IS_ENABLED(CONFIG_HID_MCS_HEXDEV)
+	{ HID_USB_DEVICE(USB_VENDOR_ID_MCS, USB_DEVICE_ID_MCS_HEXDEV_HEXLIN) },
+#endif
 #if IS_ENABLED(CONFIG_HID_HOLTEK)
 	{ HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK, USB_DEVICE_ID_HOLTEK_ON_LINE_GRIP) },
 	{ HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK_ALT, USB_DEVICE_ID_HOLTEK_ALT_KEYBOARD) },
Signature · 1 line
2.39.2
maintainer
#5 lore raw

Re: [PATCH v2 02/12] HID: hexLIN: Add support for USB LIN bus adapter

Quoted 9 lines · Christoph Fritz

This patch introduces driver support for the hexLIN USB LIN bus adapter, enabling LIN communication over USB for both controller and responder modes. The driver interfaces with the CAN_LIN framework for userland connectivity.

For more details on the adapter, visit: https://hexdev.de/hexlin/

...

Quoted 3 lines
drivers/hid/hid-hexdev-hexlin.c new file +0 -0
@@ -0,0 +1,630 @@

...

drivers/hid/hid-hexdev-hexlin.c line 318

Quoted 12 lines
drivers/hid/hid-hexdev-hexlin.c new file +12 -0
+static int hexlin_stop(struct lin_device *ldev)
+{
+	struct hid_device *hdev = to_hid_device(ldev->dev);
+	struct hexlin_priv_data *priv = hid_get_drvdata(hdev);
+
+	hid_hw_close(hdev);
+
+	priv->is_error = true;
+	complete(&priv->wait_in_report);
+
+	mutex_lock(&priv->tx_lock);
+	mutex_unlock(&priv->tx_lock);

This is a weird way to implement a completion. It looks like you need another one.

drivers/hid/hid-hexdev-hexlin.c

Quoted 2 lines
drivers/hid/hid-hexdev-hexlin.c new file +2 -0
+	return 0;
+}

...> +static int hexlin_probe(struct hid_device *hdev,

Quoted 58 lines
drivers/hid/hid-hexdev-hexlin.c new file +58 -0
+			const struct hid_device_id *id)
+{
+	struct hexlin_priv_data *priv;
+	int ret;
+
+	priv = devm_kzalloc(&hdev->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	priv->hid_dev = hdev;
+	hid_set_drvdata(hdev, priv);
+
+	mutex_init(&priv->tx_lock);
+
+	ret = hid_parse(hdev);
+	if (ret) {
+		hid_err(hdev, "hid parse failed with %d\n", ret);
+		goto fail_and_free;
+	}
+
+	ret = hid_hw_start(hdev, HID_CONNECT_DRIVER);
+	if (ret) {
+		hid_err(hdev, "hid hw start failed with %d\n", ret);
+		goto fail_and_stop;
+	}
+
+	ret = hid_hw_open(hdev);
+	if (ret) {
+		hid_err(hdev, "hid hw open failed with %d\n", ret);
+		goto fail_and_close;
+	}
+
+	init_completion(&priv->wait_in_report);
+
+	hid_device_io_start(hdev);
+
+	ret = init_hw(priv);
+	if (ret)
+		goto fail_and_close;
+
+	priv->ldev = register_lin(&hdev->dev, &hexlin_ldo);
+	if (IS_ERR_OR_NULL(priv->ldev)) {
+		ret = PTR_ERR(priv->ldev);
+		goto fail_and_close;
+	}
+
+	hid_hw_close(hdev);
+
+	hid_info(hdev, "hexLIN (fw-version: %u) probed\n", priv->fw_version);
+
+	return 0;
+
+fail_and_close:
+	hid_hw_close(hdev);
+fail_and_stop:
+	hid_hw_stop(hdev);
+fail_and_free:
+	mutex_destroy(&priv->tx_lock);

drivers/hid/hid-hexdev-hexlin.c line 594

Quoted 10 lines
drivers/hid/hid-hexdev-hexlin.c new file +10 -0
+	return ret;
+}
+
+static void hexlin_remove(struct hid_device *hdev)
+{
+	struct hexlin_priv_data *priv = hid_get_drvdata(hdev);
+
+	unregister_lin(priv->ldev);
+	hid_hw_stop(hdev);
+	mutex_destroy(&priv->tx_lock);

It is unusual to destroy a mutex. Why do you do that?

Quoted 1 line

+}

...

Quoted 7 lines
drivers/hid/hid-hexdev-hexlin.c new file +7 -0
+static int __init hexlin_init(void)
+{
+	return hid_register_driver(&hexlin_driver);
+}
+
+static void __exit hexlin_exit(void)
+{

drivers/hid/hid-hexdev-hexlin.c line 625

Quoted 10 lines
drivers/hid/hid-hexdev-hexlin.c new file +7 -0
+	hid_unregister_driver(&hexlin_driver);
+}
 
 
 
+
+/*
+ * When compiled into the kernel, initialize after the hid bus.
+ */
+late_initcall(hexlin_init);

Hmm, why not module_init() then? (And module_hid_driver().)

drivers/hid/hid-hexdev-hexlin.c line 630

Quoted 5 lines
drivers/hid/hid-hexdev-hexlin.c new file +5 -0
+module_exit(hexlin_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Christoph Fritz <[email protected]>");
+MODULE_DESCRIPTION("LIN bus driver for hexLIN USB adapter");

thanks,

Signature · 2 lines
js
suse labs
#6 lore raw

Re: [PATCH v2 02/12] HID: hexLIN: Add support for USB LIN bus adapter

Quoted 15 lines · Jiri Slaby
Quoted 9 lines · Christoph Fritz

This patch introduces driver support for the hexLIN USB LIN bus adapter, enabling LIN communication over USB for both controller and responder modes. The driver interfaces with the CAN_LIN framework for userland connectivity.

For more details on the adapter, visit: https://hexdev.de/hexlin/

...

Quoted 3 lines
drivers/hid/hid-hexdev-hexlin.c new file +0 -0
@@ -0,0 +1,630 @@

...

Quoted 15 lines

drivers/hid/hid-hexdev-hexlin.c line 318

Quoted 12 lines
drivers/hid/hid-hexdev-hexlin.c new file +12 -0
+static int hexlin_stop(struct lin_device *ldev)
+{
+	struct hid_device *hdev = to_hid_device(ldev->dev);
+	struct hexlin_priv_data *priv = hid_get_drvdata(hdev);
+
+	hid_hw_close(hdev);
+
+	priv->is_error = true;
+	complete(&priv->wait_in_report);
+
+	mutex_lock(&priv->tx_lock);
+	mutex_unlock(&priv->tx_lock);

This is a weird way to implement a completion. It looks like you need another one.

They are not necessary, even more so when I can drop the mutex_destroy() below.

Quoted 66 lines

drivers/hid/hid-hexdev-hexlin.c

Quoted 3 lines
drivers/hid/hid-hexdev-hexlin.c new file +2 -0
+	return 0;
+}

...> +static int hexlin_probe(struct hid_device *hdev,

Quoted 62 lines
drivers/hid/hid-hexdev-hexlin.c new file +62 -0
+			const struct hid_device_id *id)
+{
+	struct hexlin_priv_data *priv;
+	int ret;
+
+	priv = devm_kzalloc(&hdev->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	priv->hid_dev = hdev;
+	hid_set_drvdata(hdev, priv);
+
+	mutex_init(&priv->tx_lock);
+
+	ret = hid_parse(hdev);
+	if (ret) {
+		hid_err(hdev, "hid parse failed with %d\n", ret);
+		goto fail_and_free;
+	}
+
+	ret = hid_hw_start(hdev, HID_CONNECT_DRIVER);
+	if (ret) {
+		hid_err(hdev, "hid hw start failed with %d\n", ret);
+		goto fail_and_stop;
+	}
+
+	ret = hid_hw_open(hdev);
+	if (ret) {
+		hid_err(hdev, "hid hw open failed with %d\n", ret);
+		goto fail_and_close;
+	}
+
+	init_completion(&priv->wait_in_report);
+
+	hid_device_io_start(hdev);
+
+	ret = init_hw(priv);
+	if (ret)
+		goto fail_and_close;
+
+	priv->ldev = register_lin(&hdev->dev, &hexlin_ldo);
+	if (IS_ERR_OR_NULL(priv->ldev)) {
+		ret = PTR_ERR(priv->ldev);
+		goto fail_and_close;
+	}
+
+	hid_hw_close(hdev);
+
+	hid_info(hdev, "hexLIN (fw-version: %u) probed\n", priv->fw_version);
+
+	return 0;
+
+fail_and_close:
+	hid_hw_close(hdev);
+fail_and_stop:
+	hid_hw_stop(hdev);
+fail_and_free:
+	mutex_destroy(&priv->tx_lock);
+	return ret;
+}
+
+static void hexlin_remove(struct hid_device *hdev)
Quoted 9 lines

drivers/hid/hid-hexdev-hexlin.c line 594

Quoted 6 lines
drivers/hid/hid-hexdev-hexlin.c new file +6 -0
+{
+	struct hexlin_priv_data *priv = hid_get_drvdata(hdev);
+
+	unregister_lin(priv->ldev);
+	hid_hw_stop(hdev);
+	mutex_destroy(&priv->tx_lock);

It is unusual to destroy a mutex. Why do you do that?

Just for code clarity and it should help if someone wants to use CONFIG_DEBUG_MUTEXES.

To be able to drop the lock/unlock from above, I could add the lock/unlock here or just drop the mutex_destroy() completely.

I'll just drop it in upcoming v3.

Quoted 14 lines
Quoted 1 line

+}

...

Quoted 9 lines
drivers/hid/hid-hexdev-hexlin.c new file +9 -0
+static int __init hexlin_init(void)
+{
+	return hid_register_driver(&hexlin_driver);
+}
+
+static void __exit hexlin_exit(void)
+{
+	hid_unregister_driver(&hexlin_driver);
+}
Quoted 7 lines

drivers/hid/hid-hexdev-hexlin.c line 625

Quoted 5 lines
drivers/hid/hid-hexdev-hexlin.c new file +5 -0
+
+/*
+ * When compiled into the kernel, initialize after the hid bus.
+ */
+late_initcall(hexlin_init);

Hmm, why not module_init() then? (And module_hid_driver().)

Looking at the other hid drivers and testing with just

module_hid_driver(hexlin_driver)

works here fine for compiled into the kernel and as a module.

Quoted 6 lines

drivers/hid/hid-hexdev-hexlin.c line 630

Quoted 6 lines
drivers/hid/hid-hexdev-hexlin.c new file +5 -0
+module_exit(hexlin_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Christoph Fritz <[email protected]>");
+MODULE_DESCRIPTION("LIN bus driver for hexLIN USB adapter");

Thanks
-- Christoph

reviewer
#7 lore raw

Re: [PATCH v2 02/12] HID: hexLIN: Add support for USB LIN bus adapter

In reply to Christoph Fritz
Quoted 9 lines · Christoph Fritz

This patch introduces driver support for the hexLIN USB LIN bus adapter, enabling LIN communication over USB for both controller and responder modes. The driver interfaces with the CAN_LIN framework for userland connectivity.

For more details on the adapter, visit: https://hexdev.de/hexlin/

Hi Christoph,

Some minor feedback from my side.

...

Quoted 1 line
drivers/hid/hid-hexdev-hexlin.c +0 -0

...

Quoted 12 lines
drivers/hid/hid-hexdev-hexlin.c +12 -0
+static int hexlin_queue_frames_insert(struct hexlin_priv_data *priv,
+				      const u8 *raw_data, int sz)
+{
+	struct hid_device *hdev = priv->hid_dev;
+	struct hexlin_frame hxf;
+	struct lin_frame lf;
+
+	if (sz != sizeof(struct hexlin_frame))
+		return -EREMOTEIO;
+
+	memcpy(&hxf, raw_data, sz);
+	le32_to_cpus(hxf.flags);

drivers/hid/hid-hexdev-hexlin.c line 205

Quoted 10 lines
drivers/hid/hid-hexdev-hexlin.c +10 -0
+
+	lf.len = hxf.len;
+	lf.lin_id = hxf.lin_id;
+	memcpy(lf.data, hxf.data, LIN_MAX_DLEN);
+	lf.checksum = hxf.checksum;
+	lf.checksum_mode = hxf.checksum_mode;
+
+	hid_dbg(hdev, "id:%02x, len:%u, data:%*ph, checksum:%02x (%s)\n",
+		   lf.lin_id, lf.len, lf.len, lf.data, lf.checksum,
+		   lf.checksum_mode ? "enhanced" : "classic");

nit: The indentation above is not quite right.
It should align to inside the opening '(' like this.

hid_dbg(hdev, "id:%02x, len:%u, data:%*ph, checksum:%02x (%s)\n",
lf.lin_id, lf.len, lf.len, lf.data, lf.checksum, lf.checksum_mode ? "enhanced" : "classic");

Flagged by checkpatch.

...

drivers/hid/hid-hexdev-hexlin.c line 242

Quoted 10 lines
drivers/hid/hid-hexdev-hexlin.c +10 -0
+static int hexlin_set_baudrate(struct hexlin_priv_data *priv, u16 baudrate)
+{
+	struct hexlin_baudrate_req req;
+	int ret;
+
+	if (baudrate < LIN_MIN_BAUDRATE || baudrate > LIN_MAX_BAUDRATE)
+		return -EINVAL;
+
+	req.cmd = HEXLIN_SET_BAUDRATE;
+	req.baudrate = cpu_to_le16(baudrate);

The type of req.baudrate is u16, a host byte-order type. But it is being assigned a little endian value. This does not seem right.

Flagged by Smatch.

Quoted 8 lines
drivers/hid/hid-hexdev-hexlin.c +8 -0
+
+	ret = hexlin_tx_req_status(priv, &req,
+				   sizeof(struct hexlin_baudrate_req));
+	if (ret)
+		hid_err(priv->hid_dev, "%s failed with %d\n", __func__, ret);
+
+	return ret;
+}

...

Quoted 35 lines
drivers/hid/hid-hexdev-hexlin.c +35 -0
+static int hexlin_probe(struct hid_device *hdev,
+			const struct hid_device_id *id)
+{
+	struct hexlin_priv_data *priv;
+	int ret;
+
+	priv = devm_kzalloc(&hdev->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	priv->hid_dev = hdev;
+	hid_set_drvdata(hdev, priv);
+
+	mutex_init(&priv->tx_lock);
+
+	ret = hid_parse(hdev);
+	if (ret) {
+		hid_err(hdev, "hid parse failed with %d\n", ret);
+		goto fail_and_free;
+	}
+
+	ret = hid_hw_start(hdev, HID_CONNECT_DRIVER);
+	if (ret) {
+		hid_err(hdev, "hid hw start failed with %d\n", ret);
+		goto fail_and_stop;
+	}
+
+	ret = hid_hw_open(hdev);
+	if (ret) {
+		hid_err(hdev, "hid hw open failed with %d\n", ret);
+		goto fail_and_close;
+	}
+
+	init_completion(&priv->wait_in_report);
+

drivers/hid/hid-hexdev-hexlin.c line 570

Quoted 10 lines
drivers/hid/hid-hexdev-hexlin.c +10 -0
+	hid_device_io_start(hdev);
+
+	ret = init_hw(priv);
+	if (ret)
+		goto fail_and_close;
+
+	priv->ldev = register_lin(&hdev->dev, &hexlin_ldo);
+	if (IS_ERR_OR_NULL(priv->ldev)) {
+		ret = PTR_ERR(priv->ldev);
+		goto fail_and_close;

Is it intentional that when priv->ldev is NULL here, the function will return 0?

Flagged by Smatch.

Quoted 16 lines
drivers/hid/hid-hexdev-hexlin.c +16 -0
+	}
+
+	hid_hw_close(hdev);
+
+	hid_info(hdev, "hexLIN (fw-version: %u) probed\n", priv->fw_version);
+
+	return 0;
+
+fail_and_close:
+	hid_hw_close(hdev);
+fail_and_stop:
+	hid_hw_stop(hdev);
+fail_and_free:
+	mutex_destroy(&priv->tx_lock);
+	return ret;
+}

...

#8 lore raw

Re: [PATCH v2 02/12] HID: hexLIN: Add support for USB LIN bus adapter

Quoted 47 lines · Simon Horman
Quoted 9 lines · Christoph Fritz

This patch introduces driver support for the hexLIN USB LIN bus adapter, enabling LIN communication over USB for both controller and responder modes. The driver interfaces with the CAN_LIN framework for userland connectivity.

For more details on the adapter, visit: https://hexdev.de/hexlin/

Hi Christoph,

Some minor feedback from my side.

...

Quoted 1 line
drivers/hid/hid-hexdev-hexlin.c +0 -0

...

Quoted 12 lines
drivers/hid/hid-hexdev-hexlin.c +12 -0
+static int hexlin_queue_frames_insert(struct hexlin_priv_data *priv,
+				      const u8 *raw_data, int sz)
+{
+	struct hid_device *hdev = priv->hid_dev;
+	struct hexlin_frame hxf;
+	struct lin_frame lf;
+
+	if (sz != sizeof(struct hexlin_frame))
+		return -EREMOTEIO;
+
+	memcpy(&hxf, raw_data, sz);
+	le32_to_cpus(hxf.flags);

drivers/hid/hid-hexdev-hexlin.c line 205

Quoted 10 lines
drivers/hid/hid-hexdev-hexlin.c +10 -0
+
+	lf.len = hxf.len;
+	lf.lin_id = hxf.lin_id;
+	memcpy(lf.data, hxf.data, LIN_MAX_DLEN);
+	lf.checksum = hxf.checksum;
+	lf.checksum_mode = hxf.checksum_mode;
+
+	hid_dbg(hdev, "id:%02x, len:%u, data:%*ph, checksum:%02x (%s)\n",
+		   lf.lin_id, lf.len, lf.len, lf.data, lf.checksum,
+		   lf.checksum_mode ? "enhanced" : "classic");

nit: The indentation above is not quite right.
It should align to inside the opening '(' like this.

Quoted 5 lines

hid_dbg(hdev, "id:%02x, len:%u, data:%*ph, checksum:%02x (%s)\n",
lf.lin_id, lf.len, lf.len, lf.data, lf.checksum, lf.checksum_mode ? "enhanced" : "classic");

Flagged by checkpatch.

OK

Quoted 18 lines

...

drivers/hid/hid-hexdev-hexlin.c line 242

Quoted 10 lines
drivers/hid/hid-hexdev-hexlin.c +10 -0
+static int hexlin_set_baudrate(struct hexlin_priv_data *priv, u16 baudrate)
+{
+	struct hexlin_baudrate_req req;
+	int ret;
+
+	if (baudrate < LIN_MIN_BAUDRATE || baudrate > LIN_MAX_BAUDRATE)
+		return -EINVAL;
+
+	req.cmd = HEXLIN_SET_BAUDRATE;
+	req.baudrate = cpu_to_le16(baudrate);

The type of req.baudrate is u16, a host byte-order type. But it is being assigned a little endian value. This does not seem right.

Quoted 1 line

Flagged by Smatch.

In upcoming v4 I'll use __le16

Quoted 61 lines
Quoted 9 lines
drivers/hid/hid-hexdev-hexlin.c +8 -0
+
+	ret = hexlin_tx_req_status(priv, &req,
+				   sizeof(struct hexlin_baudrate_req));
+	if (ret)
+		hid_err(priv->hid_dev, "%s failed with %d\n", __func__, ret);
+
+	return ret;
+}

...

Quoted 35 lines
drivers/hid/hid-hexdev-hexlin.c +35 -0
+static int hexlin_probe(struct hid_device *hdev,
+			const struct hid_device_id *id)
+{
+	struct hexlin_priv_data *priv;
+	int ret;
+
+	priv = devm_kzalloc(&hdev->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	priv->hid_dev = hdev;
+	hid_set_drvdata(hdev, priv);
+
+	mutex_init(&priv->tx_lock);
+
+	ret = hid_parse(hdev);
+	if (ret) {
+		hid_err(hdev, "hid parse failed with %d\n", ret);
+		goto fail_and_free;
+	}
+
+	ret = hid_hw_start(hdev, HID_CONNECT_DRIVER);
+	if (ret) {
+		hid_err(hdev, "hid hw start failed with %d\n", ret);
+		goto fail_and_stop;
+	}
+
+	ret = hid_hw_open(hdev);
+	if (ret) {
+		hid_err(hdev, "hid hw open failed with %d\n", ret);
+		goto fail_and_close;
+	}
+
+	init_completion(&priv->wait_in_report);
+

drivers/hid/hid-hexdev-hexlin.c line 570

Quoted 10 lines
drivers/hid/hid-hexdev-hexlin.c +10 -0
+	hid_device_io_start(hdev);
+
+	ret = init_hw(priv);
+	if (ret)
+		goto fail_and_close;
+
+	priv->ldev = register_lin(&hdev->dev, &hexlin_ldo);
+	if (IS_ERR_OR_NULL(priv->ldev)) {
+		ret = PTR_ERR(priv->ldev);
+		goto fail_and_close;

Is it intentional that when priv->ldev is NULL here, the function will return 0?

Quoted 1 line

Flagged by Smatch.

IS_ERR_OR_NULL() gets IS_ERR() in upcoming v4

Quoted 11 lines
Quoted 11 lines
drivers/hid/hid-hexdev-hexlin.c +10 -0
+	}
+
+	hid_hw_close(hdev);
+
+	hid_info(hdev, "hexLIN (fw-version: %u) probed\n", priv->fw_version);
+
+	return 0;
+
+fail_and_close:
+	hid_hw_close(hdev);
Quoted 8 lines
Quoted 6 lines
drivers/hid/hid-hexdev-hexlin.c +6 -0
+fail_and_stop:
+	hid_hw_stop(hdev);
+fail_and_free:
+	mutex_destroy(&priv->tx_lock);
+	return ret;
+}

...

Thanks
-- Christoph

#9 lore raw

PATCH v2 3/12 tty: serdev: Add flag buffer aware receive_buf_fp()

In reply to Christoph Fritz

This patch introduces an additional receive buffer callback variation besides the already existing receive_buf(). This new callback function also passes the flag buffer (TTY_NORMAL, TTY_BREAK, and friends).

If defined, this function gets prioritized and called instead of the standard receive_buf().

An alternative approach could have been to enhance the receive_buf() function and update all drivers that use it.

---
 drivers/tty/serdev/serdev-ttyport.c |  2 +-
 include/linux/serdev.h              | 17 ++++++++++++++---
 2 files changed, 15 insertions(+), 4 deletions(-)
drivers/tty/serdev/serdev-ttyport.c +1 -1
index 3d7ae7fa50186..bb47691afdb21 100644
@@ -32,7 +32,7 @@ static size_t ttyport_receive_buf(struct tty_port *port, const u8 *cp,
 	if (!test_bit(SERPORT_ACTIVE, &serport->flags))
 		return 0;
 
-	ret = serdev_controller_receive_buf(ctrl, cp, count);
+	ret = serdev_controller_receive_buf(ctrl, cp, fp, count);
 
 	dev_WARN_ONCE(&ctrl->dev, ret > count,
 				"receive_buf returns %zu (count = %zu)\n",
include/linux/serdev.h +14 -3
index ff78efc1f60df..c6ef5a8988e07 100644
@@ -23,11 +23,17 @@ struct serdev_device;
  * struct serdev_device_ops - Callback operations for a serdev device
  * @receive_buf:	Function called with data received from device;
  *			returns number of bytes accepted; may sleep.
+ * @receive_buf_fp:	Function called with data and flag buffer received
+ *			from device; If defined, this function gets called
+ *			instead of @receive_buf;
+ *			returns number of bytes accepted; may sleep.
  * @write_wakeup:	Function called when ready to transmit more data; must
  *			not sleep.
  */
 struct serdev_device_ops {
 	size_t (*receive_buf)(struct serdev_device *, const u8 *, size_t);
+	ssize_t (*receive_buf_fp)(struct serdev_device *, const u8 *,
+				  const u8 *, size_t);
 	void (*write_wakeup)(struct serdev_device *);
 };
 
@@ -186,15 +192,20 @@ static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl
 }
 
 static inline size_t serdev_controller_receive_buf(struct serdev_controller *ctrl,
-						   const u8 *data,
+						   const u8 *data, const u8 *fp,
 						   size_t count)
 {
 	struct serdev_device *serdev = ctrl->serdev;
 
-	if (!serdev || !serdev->ops->receive_buf)
+	if (!serdev || !serdev->ops)
 		return 0;
 
-	return serdev->ops->receive_buf(serdev, data, count);
+	if (serdev->ops->receive_buf_fp)
+		return serdev->ops->receive_buf_fp(serdev, data, fp, count);
+	else if (serdev->ops->receive_buf)
+		return serdev->ops->receive_buf(serdev, data, count);
+
+	return 0;
 }
 
 #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
Signature · 1 line
2.39.2
#10 lore raw

PATCH v2 4/12 tty: serdev: Add method to enable break flags

In reply to Christoph Fritz

The recently introduced callback function receive_buf_fp() brings flags buffer support. To allow signaling of TTY_BREAK flags, this patch introduces serdev_device_set_break_detection() and an implementation for ttyport. This enables serdev devices to configure their underlying tty port to signal or ignore break conditions.

---
 drivers/tty/serdev/core.c           | 11 +++++++++++
 drivers/tty/serdev/serdev-ttyport.c | 17 +++++++++++++++++
 include/linux/serdev.h              |  2 ++
 3 files changed, 30 insertions(+)
drivers/tty/serdev/core.c +11 -0
index 613cb356b918d..23a1e76cb553b 100644
@@ -339,6 +339,17 @@ unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned i
 }
 EXPORT_SYMBOL_GPL(serdev_device_set_baudrate);
 
+void serdev_device_set_break_detection(struct serdev_device *serdev, bool enable)
+{
+	struct serdev_controller *ctrl = serdev->ctrl;
+
+	if (!ctrl || !ctrl->ops->set_break_detection)
+		return;
+
+	ctrl->ops->set_break_detection(ctrl, enable);
+}
+EXPORT_SYMBOL_GPL(serdev_device_set_break_detection);
+
 void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable)
 {
 	struct serdev_controller *ctrl = serdev->ctrl;
drivers/tty/serdev/serdev-ttyport.c +17 -0
index bb47691afdb21..e928bf4175c6f 100644
@@ -192,6 +192,22 @@ static void ttyport_set_flow_control(struct serdev_controller *ctrl, bool enable
 	tty_set_termios(tty, &ktermios);
 }
 
+static void ttyport_set_break_detection(struct serdev_controller *ctrl, bool enable)
+{
+	struct serport *serport = serdev_controller_get_drvdata(ctrl);
+	struct tty_struct *tty = serport->tty;
+	struct ktermios ktermios = tty->termios;
+
+	ktermios.c_iflag &= ~(IGNBRK | BRKINT);
+
+	if (enable)
+		ktermios.c_iflag |= BRKINT;
+	else
+		ktermios.c_iflag |= IGNBRK;
+
+	tty_set_termios(tty, &ktermios);
+}
+
 static int ttyport_set_parity(struct serdev_controller *ctrl,
 			      enum serdev_parity parity)
 {
@@ -263,6 +279,7 @@ static const struct serdev_controller_ops ctrl_ops = {
 	.open = ttyport_open,
 	.close = ttyport_close,
 	.set_flow_control = ttyport_set_flow_control,
+	.set_break_detection = ttyport_set_break_detection,
 	.set_parity = ttyport_set_parity,
 	.set_baudrate = ttyport_set_baudrate,
 	.wait_until_sent = ttyport_wait_until_sent,
include/linux/serdev.h +2 -0
index c6ef5a8988e07..d08aa7e16f9f8 100644
@@ -94,6 +94,7 @@ struct serdev_controller_ops {
 	int (*open)(struct serdev_controller *);
 	void (*close)(struct serdev_controller *);
 	void (*set_flow_control)(struct serdev_controller *, bool);
+	void (*set_break_detection)(struct serdev_controller *, bool);
 	int (*set_parity)(struct serdev_controller *, enum serdev_parity);
 	unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int);
 	void (*wait_until_sent)(struct serdev_controller *, long);
@@ -215,6 +216,7 @@ void serdev_device_close(struct serdev_device *);
 int devm_serdev_device_open(struct device *, struct serdev_device *);
 unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
 void serdev_device_set_flow_control(struct serdev_device *, bool);
+void serdev_device_set_break_detection(struct serdev_device *, bool);
 int serdev_device_write_buf(struct serdev_device *, const u8 *, size_t);
 void serdev_device_wait_until_sent(struct serdev_device *, long);
 int serdev_device_get_tiocm(struct serdev_device *);
Signature · 1 line
2.39.2
#11 lore raw

PATCH v2 5/12 dt-bindings: vendor-prefixes: Add hexDEV

In reply to Christoph Fritz

Add vendor prefix for hexDEV GmbH. Website: https://hexdev.de

---
 Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++
 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/vendor-prefixes.yaml +2 -0
index 08b241825f8b6..1a8cfc5c7350d 100644
@@ -601,6 +601,8 @@ patternProperties:
     description: Hardkernel Co., Ltd
   "^hechuang,.*":
     description: Shenzhen Hechuang Intelligent Co.
+  "^hexdev,.*":
+    description: hexDEV GmbH
   "^hideep,.*":
     description: HiDeep Inc.
   "^himax,.*":
Signature · 1 line
2.39.2
Acked-by on v2 5/12
#12 lore raw

Re: [PATCH v2 05/12] dt-bindings: vendor-prefixes: Add hexDEV

Quoted 4 lines · Christoph Fritz

Add vendor prefix for hexDEV GmbH. Website: https://hexdev.de

---

---

This is an automated instruction, just in case, because many review tags are being ignored. If you know the process, you can skip it (please do not feel offended by me posting it here - no bad intentions intended). If you do not know the process, here is a short explanation:

Please add Acked-by/Reviewed-by/Tested-by tags when posting new versions, under or above your Signed-off-by tag. Tag is "received", when provided in a message replied to you on the mailing list. Tools like b4 can help here. However, there's no need to repost patches *only* to add the tags. The upstream maintainer will do that for tags received on the version they apply.

https://elixir.bootlin.com/linux/v6.5-rc3/source/Documentation/process/submitting-patches.rst#L577

Best regards,
Krzysztof

#13 lore raw

PATCH v2 6/12 dt-bindings: net/can: Add serial (serdev) LIN adapter

In reply to Christoph Fritz

This patch adds dt-bindings for serial LIN bus adapters. These adapters are basically just LIN transceivers that get hard-wired with serial devices.

---
 .../bindings/net/can/hexdev,lin-serdev.yaml   | 32 +++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.yaml
Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.yaml new file +32 -0
new file mode 100644
index 0000000000000..cbf9f63a2a49f
@@ -0,0 +1,32 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/net/can/hexdev,lin-serdev.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Serial LIN Adapter
+
+description:
+  LIN transceiver, mostly hard-wired to a serial device, used for communication
+  on a LIN bus.
+  For more details on an adapter, visit <https://hexdev.de/hexlin#tty>.
+
+maintainers:
+  - Christoph Fritz <[email protected]>
+
+properties:
+  compatible:
+    const: hexdev,lin-serdev
+
+required:
+  - compatible
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    serial {
+        linbus {
+            compatible = "linux,lin-serdev";
+        };
+    };
Signature · 1 line
2.39.2
#14 lore raw

Re: [PATCH v2 06/12] dt-bindings: net/can: Add serial (serdev) LIN adapter

Quoted 1 line · Christoph Fritz

This patch adds dt-bindings for serial LIN bus adapters. These adapters are

Please do not use "This commit/patch/change", but imperative mood. See longer explanation here:
https://elixir.bootlin.com/linux/v5.17.1/source/Documentation/process/submitting-patches.rst#L95

Quoted 3 lines

basically just LIN transceivers that get hard-wired with serial devices.

...

Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.yaml line 24

Quoted 11 lines
Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.yaml new file +11 -0
+maintainers:
+  - Christoph Fritz <[email protected]>
+
+properties:
+  compatible:
+    const: hexdev,lin-serdev
+
+required:
+  - compatible
+
+unevaluatedProperties: false

This must be additionalProperties: false.

Best regards,
Krzysztof

#15 lore raw

Re: [PATCH v2 06/12] dt-bindings: net/can: Add serial (serdev) LIN adapter

In reply to Christoph Fritz
Quoted 9 lines · Christoph Fritz

This patch adds dt-bindings for serial LIN bus adapters. These adapters are basically just LIN transceivers that get hard-wired with serial devices.

---
 .../bindings/net/can/hexdev,lin-serdev.yaml   | 32 +++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.yaml

My bot found errors running 'make dt_binding_check' on your patch:

yamllint warnings/errors:

dtschema/dtc warnings/errors:
Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.example.dtb: /example-0/serial/linbus: failed to match any schema with compatible: ['linux,lin-serdev']

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/[email protected]

The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.

#16 lore raw

Re: [PATCH v2 06/12] dt-bindings: net/can: Add serial (serdev) LIN adapter

Quoted 11 lines · Rob Herring (Arm)
Quoted 9 lines · Christoph Fritz

This patch adds dt-bindings for serial LIN bus adapters. These adapters are basically just LIN transceivers that get hard-wired with serial devices.

---
 .../bindings/net/can/hexdev,lin-serdev.yaml   | 32 +++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.yaml
Quoted 6 lines

My bot found errors running 'make dt_binding_check' on your patch:

yamllint warnings/errors:

dtschema/dtc warnings/errors:
Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.example.dtb: /example-0/serial/linbus: failed to match any schema with compatible: ['linux,lin-serdev']

Yes, that's obviously still false and will be fixed in v3.

Quoted 12 lines

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/[email protected]

The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

Quoted 6 lines

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.

I'm wondering why my local run of dt_binding_check does not catch this:

$ pip3 install dtschema --upgrade
Requirement already satisfied: dtschema in ./venv/lib/python3.11/site-packages (2024.4)
Requirement already satisfied: ruamel.yaml>0.15.69 in ./venv/lib/python3.11/site-packages (from dtschema) (0.18.6)
Requirement already satisfied: jsonschema<4.18,>=4.1.2 in ./venv/lib/python3.11/site-packages (from dtschema) (4.17.3)
Requirement already satisfied: rfc3987 in ./venv/lib/python3.11/site-packages (from dtschema) (1.3.8)
Requirement already satisfied: pylibfdt in ./venv/lib/python3.11/site-packages (from dtschema) (1.7.0.post1)
Requirement already satisfied: attrs>=17.4.0 in ./venv/lib/python3.11/site-packages (from jsonschema<4.18,>=4.1.2->dtschema) (23.2.0)
Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in ./venv/lib/python3.11/site-packages (from jsonschema<4.18,>=4.1.2->dtschema) (0.20.0)
Requirement already satisfied: ruamel.yaml.clib>=0.2.7 in ./venv/lib/python3.11/site-packages (from ruamel.yaml>0.15.69->dtschema) (0.2.8)
$ git diff
Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.yaml +1 -1
index c178eb9be1391..385cbe132258d 100644
@@ -27,6 +27,6 @@ examples:
   - |
     serial {
         linbus {
-            compatible = "hexdev,lin-serdev";
+            compatible = "linux,lin-serdev";
         };
     };
$ make dt_binding_check DT_SCHEMA_FILES=Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.yaml
  HOSTCC  scripts/basic/fixdep
  HOSTCC  scripts/dtc/dtc.o
  HOSTCC  scripts/dtc/flattree.o
  HOSTCC  scripts/dtc/fstree.o
  HOSTCC  scripts/dtc/data.o
  HOSTCC  scripts/dtc/livetree.o
  HOSTCC  scripts/dtc/treesource.o
  HOSTCC  scripts/dtc/srcpos.o
  HOSTCC  scripts/dtc/checks.o
  HOSTCC  scripts/dtc/util.o
  LEX     scripts/dtc/dtc-lexer.lex.c
  YACC    scripts/dtc/dtc-parser.tab.[ch]
  HOSTCC  scripts/dtc/dtc-lexer.lex.o
  HOSTCC  scripts/dtc/dtc-parser.tab.o
  HOSTLD  scripts/dtc/dtc
  HOSTCC  scripts/dtc/libfdt/fdt.o
  HOSTCC  scripts/dtc/libfdt/fdt_ro.o
  HOSTCC  scripts/dtc/libfdt/fdt_wip.o
  HOSTCC  scripts/dtc/libfdt/fdt_sw.o
  HOSTCC  scripts/dtc/libfdt/fdt_rw.o
  HOSTCC  scripts/dtc/libfdt/fdt_strerror.o
  HOSTCC  scripts/dtc/libfdt/fdt_empty_tree.o
  HOSTCC  scripts/dtc/libfdt/fdt_addresses.o
  HOSTCC  scripts/dtc/libfdt/fdt_overlay.o
  HOSTCC  scripts/dtc/fdtoverlay.o
  HOSTLD  scripts/dtc/fdtoverlay
  LINT    Documentation/devicetree/bindings
  CHKDT   Documentation/devicetree/bindings/processed-schema.json
  SCHEMA  Documentation/devicetree/bindings/processed-schema.json
/home/ch/linux/Documentation/devicetree/bindings/pci/brcm,stb-pcie.yaml: ignoring, error in schema: properties: brcm,tperst-clk-ms: type
/home/ch/linux/Documentation/devicetree/bindings/hwmon/microchip,emc2305.yaml: ignoring, error in schema: properties: emcs205,max-state: description
  DTEX    Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.example.dts
  DTC_CHK Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.example.dtb

Any ideas?

I'm using a python venv here, maybe this is related?

Thanks
-- Christoph

maintainer
#17 lore raw

Re: [PATCH v2 06/12] dt-bindings: net/can: Add serial (serdev) LIN adapter

Quoted 102 lines · Christoph Fritz
Quoted 11 lines · Rob Herring (Arm)
Quoted 9 lines · Christoph Fritz

This patch adds dt-bindings for serial LIN bus adapters. These adapters are basically just LIN transceivers that get hard-wired with serial devices.

---
 .../bindings/net/can/hexdev,lin-serdev.yaml   | 32 +++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.yaml
Quoted 6 lines

My bot found errors running 'make dt_binding_check' on your patch:

yamllint warnings/errors:

dtschema/dtc warnings/errors:
Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.example.dtb: /example-0/serial/linbus: failed to match any schema with compatible: ['linux,lin-serdev']

Yes, that's obviously still false and will be fixed in v3.

Quoted 12 lines

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/[email protected]

The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

Quoted 6 lines

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.

I'm wondering why my local run of dt_binding_check does not catch this:

$ pip3 install dtschema --upgrade
Requirement already satisfied: dtschema in ./venv/lib/python3.11/site-packages (2024.4)
Requirement already satisfied: ruamel.yaml>0.15.69 in ./venv/lib/python3.11/site-packages (from dtschema) (0.18.6)
Requirement already satisfied: jsonschema<4.18,>=4.1.2 in ./venv/lib/python3.11/site-packages (from dtschema) (4.17.3)
Requirement already satisfied: rfc3987 in ./venv/lib/python3.11/site-packages (from dtschema) (1.3.8)
Requirement already satisfied: pylibfdt in ./venv/lib/python3.11/site-packages (from dtschema) (1.7.0.post1)
Requirement already satisfied: attrs>=17.4.0 in ./venv/lib/python3.11/site-packages (from jsonschema<4.18,>=4.1.2->dtschema) (23.2.0)
Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in ./venv/lib/python3.11/site-packages (from jsonschema<4.18,>=4.1.2->dtschema) (0.20.0)
Requirement already satisfied: ruamel.yaml.clib>=0.2.7 in ./venv/lib/python3.11/site-packages (from ruamel.yaml>0.15.69->dtschema) (0.2.8)
$ git diff
Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.yaml +1 -1
index c178eb9be1391..385cbe132258d 100644
@@ -27,6 +27,6 @@ examples:
   - |
     serial {
         linbus {
-            compatible = "hexdev,lin-serdev";
+            compatible = "linux,lin-serdev";
         };
     };
$ make dt_binding_check DT_SCHEMA_FILES=Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.yaml
  HOSTCC  scripts/basic/fixdep
  HOSTCC  scripts/dtc/dtc.o
  HOSTCC  scripts/dtc/flattree.o
  HOSTCC  scripts/dtc/fstree.o
  HOSTCC  scripts/dtc/data.o
  HOSTCC  scripts/dtc/livetree.o
  HOSTCC  scripts/dtc/treesource.o
  HOSTCC  scripts/dtc/srcpos.o
  HOSTCC  scripts/dtc/checks.o
  HOSTCC  scripts/dtc/util.o
  LEX     scripts/dtc/dtc-lexer.lex.c
  YACC    scripts/dtc/dtc-parser.tab.[ch]
  HOSTCC  scripts/dtc/dtc-lexer.lex.o
  HOSTCC  scripts/dtc/dtc-parser.tab.o
  HOSTLD  scripts/dtc/dtc
  HOSTCC  scripts/dtc/libfdt/fdt.o
  HOSTCC  scripts/dtc/libfdt/fdt_ro.o
  HOSTCC  scripts/dtc/libfdt/fdt_wip.o
  HOSTCC  scripts/dtc/libfdt/fdt_sw.o
  HOSTCC  scripts/dtc/libfdt/fdt_rw.o
  HOSTCC  scripts/dtc/libfdt/fdt_strerror.o
  HOSTCC  scripts/dtc/libfdt/fdt_empty_tree.o
  HOSTCC  scripts/dtc/libfdt/fdt_addresses.o
  HOSTCC  scripts/dtc/libfdt/fdt_overlay.o
  HOSTCC  scripts/dtc/fdtoverlay.o
  HOSTLD  scripts/dtc/fdtoverlay
  LINT    Documentation/devicetree/bindings
  CHKDT   Documentation/devicetree/bindings/processed-schema.json
  SCHEMA  Documentation/devicetree/bindings/processed-schema.json
/home/ch/linux/Documentation/devicetree/bindings/pci/brcm,stb-pcie.yaml: ignoring, error in schema: properties: brcm,tperst-clk-ms: type
/home/ch/linux/Documentation/devicetree/bindings/hwmon/microchip,emc2305.yaml: ignoring, error in schema: properties: emcs205,max-state: description
  DTEX    Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.example.dts
  DTC_CHK Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.example.dtb
Quoted 3 lines

Any ideas?

I'm using a python venv here, maybe this is related?

No. There are 2 possibilities. What kernel version are you on? This check is enabled with the '-m' option on dt-validate which was only recently (6.9) enabled by default for the bindings. You can enable it with 'DT_CHECKER_FLAGS="-m"'. The other possibility is I noticed that the flag has an interaction with DT_SCHEMA_FILES in that we don't set the flag by default if DT_SCHEMA_FILES is set. (If you explicitly set DT_CHECKER_FLAGS in the newer kernels it should still give the warning.) I think we don't enable it because you would get false positives if your example has compatible strings not documented within the schema you are testing. I need to double check that as how the tools work in this regard has evolved. In any case, DT_SCHEMA_FILES is a shortcut and it is always possible your changes can introduce warnings in other examples, so ultimately "make dt_binding_check" has to be run without DT_SCHEMA_FILES set.

Rob

#18 lore raw

PATCH v2 7/12 can: Add support for serdev LIN adapters

In reply to Christoph Fritz

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

---
 drivers/net/can/Kconfig      |  16 ++
 drivers/net/can/Makefile     |   1 +
 drivers/net/can/lin-serdev.c | 514 +++++++++++++++++++++++++++++++++++
 3 files changed, 531 insertions(+)
 create mode 100644 drivers/net/can/lin-serdev.c
drivers/net/can/Kconfig +16 -0
index 0934bbf8d03b2..91c6cdeefe440 100644
@@ -181,6 +181,22 @@ config CAN_LIN
 
 	  Actual device drivers need to be enabled too.
 
+config CAN_LIN_SERDEV
+	tristate "Serial LIN Adaptors"
+	depends on CAN_LIN && SERIAL_DEV_BUS && OF
+	help
+	  LIN-Bus support for serial devices equipped with LIN transceievers.
+	  This device driver is using CAN_LIN for a userland connection on
+	  one side and the kernel its serial device bus (serdev) interface
+	  on the other side.
+
+	  If you have a hexlin tty adapter, say Y here and see
+	  <https://hexdev.de/hexlin#tty>.
+
+	  This driver can also be built as a module. If so, the module will be
+	  called lin-serdev.ko.
+
+
 config CAN_SLCAN
 	tristate "Serial / USB serial CAN Adaptors (slcan)"
 	depends on TTY
drivers/net/can/Makefile +1 -0
index 0093ee9219ca8..21ca514a42439 100644
@@ -26,6 +26,7 @@ obj-$(CONFIG_CAN_IFI_CANFD)	+= ifi_canfd/
 obj-$(CONFIG_CAN_JANZ_ICAN3)	+= janz-ican3.o
 obj-$(CONFIG_CAN_KVASER_PCIEFD)	+= kvaser_pciefd.o
 obj-$(CONFIG_CAN_LIN)		+= lin.o
+obj-$(CONFIG_CAN_LIN_SERDEV)	+= lin-serdev.o
 obj-$(CONFIG_CAN_MSCAN)		+= mscan/
 obj-$(CONFIG_CAN_M_CAN)		+= m_can/
 obj-$(CONFIG_CAN_PEAK_PCIEFD)	+= peak_canfd/
drivers/net/can/lin-serdev.c new file +514 -0

514 lines not on this page · raw message

Signature · 1 line
2.39.2
maintainer
#19 lore raw

Re: [PATCH v2 07/12] can: Add support for serdev LIN adapters

Quoted 4 lines · Christoph Fritz

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

...

drivers/net/can/lin-serdev.c line 9

Quoted 12 lines
drivers/net/can/lin-serdev.c new file +9 -0
@@ -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.

drivers/net/can/lin-serdev.c line 16

Quoted 7 lines
drivers/net/can/lin-serdev.c new file +7 -0
+#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.

Quoted 16 lines
drivers/net/can/lin-serdev.c new file +16 -0
+#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,
+};
+

drivers/net/can/lin-serdev.c line 43

Quoted 10 lines
drivers/net/can/lin-serdev.c new file +10 -0
+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 :)

Quoted 4 lines
drivers/net/can/lin-serdev.c new file +4 -0
+	struct lin_responder_answer respond_answ[LIN_NUM_IDS];
+	struct mutex resp_lock; /* protects respond_answ */
+	bool is_stopped;
+};

...

drivers/net/can/lin-serdev.c line 151

Quoted 5 lines
drivers/net/can/lin-serdev.c new file +5 -0
+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?

Quoted 9 lines
drivers/net/can/lin-serdev.c new file +9 -0
+	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);
+}

...

Quoted 16 lines
drivers/net/can/lin-serdev.c new file +16 -0
+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;
+

drivers/net/can/lin-serdev.c line 254

Quoted 10 lines
drivers/net/can/lin-serdev.c new file +10 -0
+		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?

Quoted 22 lines
drivers/net/can/lin-serdev.c new file +22 -0
+		}
+	} 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;

drivers/net/can/lin-serdev.c line 286

Quoted 10 lines
drivers/net/can/lin-serdev.c new file +10 -0
+}
+
+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.)

drivers/net/can/lin-serdev.c line 292

Quoted 6 lines
drivers/net/can/lin-serdev.c new file +6 -0
+		if (ret != 1) {
+			dev_err(&serdev->dev, "Failed to pop from FIFO\n");
+			break;
+		}
+	}
+}

thanks,

Signature · 2 lines
js
suse labs
#20 lore raw

Re: [PATCH v2 07/12] can: Add support for serdev LIN adapters

Quoted 6 lines · Jiri Slaby
Quoted 4 lines · Christoph Fritz

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

...

Quoted 15 lines

drivers/net/can/lin-serdev.c line 9

Quoted 12 lines
drivers/net/can/lin-serdev.c new file +9 -0
@@ -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.

OK

Quoted 10 lines

drivers/net/can/lin-serdev.c line 16

Quoted 8 lines
drivers/net/can/lin-serdev.c new file +7 -0
+#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.

OK, hid driver gets also sorted

Quoted 21 lines
Quoted 21 lines
drivers/net/can/lin-serdev.c new file +20 -0
+#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);
Quoted 8 lines

drivers/net/can/lin-serdev.c line 43

Quoted 6 lines
drivers/net/can/lin-serdev.c new file +6 -0
+	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 :)

OK

Quoted 6 lines
Quoted 5 lines
drivers/net/can/lin-serdev.c new file +4 -0
+	struct lin_responder_answer respond_answ[LIN_NUM_IDS];
+	struct mutex resp_lock; /* protects respond_answ */
+	bool is_stopped;
+};

...

Quoted 7 lines

drivers/net/can/lin-serdev.c line 151

Quoted 5 lines
drivers/net/can/lin-serdev.c new file +5 -0
+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?

yes

Quoted 32 lines
Quoted 10 lines
drivers/net/can/lin-serdev.c new file +9 -0
+	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);
+}

...

Quoted 21 lines
drivers/net/can/lin-serdev.c new file +21 -0
+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),
Quoted 8 lines

drivers/net/can/lin-serdev.c line 254

Quoted 5 lines
drivers/net/can/lin-serdev.c new file +5 -0
+						    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?

OK

Quoted 27 lines
Quoted 27 lines
drivers/net/can/lin-serdev.c new file +26 -0
+		}
+	} 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)
+{
Quoted 8 lines

drivers/net/can/lin-serdev.c line 286

Quoted 6 lines
drivers/net/can/lin-serdev.c new file +6 -0
+	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.)

Using kfifo_skip() greatly simplifies this function and it works for records (uses __kfifo_skip_r), tests are successful.

Maybe the comment in kfifo.h could be made more clear from:

"kfifo_skip - skip output data"
to
"kfifo_skip - skip the next fifo record"

Quoted 8 lines

drivers/net/can/lin-serdev.c line 292

Quoted 7 lines
drivers/net/can/lin-serdev.c new file +6 -0
+		if (ret != 1) {
+			dev_err(&serdev->dev, "Failed to pop from FIFO\n");
+			break;
+		}
+	}
+}

Let me address these points and reroll in v3.

Thanks
-- Christoph

reviewer
#21 lore raw

Re: [PATCH v2 07/12] can: Add support for serdev LIN adapters

In reply to Christoph Fritz
Quoted 6 lines · Christoph Fritz

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

...

Quoted 1 line
drivers/net/can/lin-serdev.c +0 -0

...

Quoted 24 lines
drivers/net/can/lin-serdev.c +24 -0
+static int linser_probe(struct serdev_device *serdev)
+{
+	struct linser_priv *priv;
+	int ret;
+
+	priv = devm_kzalloc(&serdev->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	ret = kfifo_alloc(&priv->rx_fifo, LINSER_RX_FIFO_SIZE, GFP_KERNEL);
+	if (ret)
+		return ret;
+
+	INIT_DELAYED_WORK(&priv->rx_work, linser_process_delayed);
+
+	priv->serdev = serdev;
+	serdev_device_set_drvdata(serdev, priv);
+	serdev_device_set_client_ops(serdev, &linser_ops);
+
+	ret = serdev_device_open(serdev);
+	if (ret) {
+		dev_err(&serdev->dev, "Unable to open device\n");
+		goto err_open;
+	}

drivers/net/can/lin-serdev.c line 470

Quoted 10 lines
drivers/net/can/lin-serdev.c +10 -0
+
+	serdev_device_set_flow_control(serdev, false);
+	serdev_device_set_break_detection(serdev, true);
+	linser_derive_timings(priv, LIN_DEFAULT_BAUDRATE);
+
+	mutex_init(&priv->resp_lock);
+
+	priv->lin_dev = register_lin(&serdev->dev, &linser_lindev_ops);
+	if (IS_ERR_OR_NULL(priv->lin_dev)) {
+		ret = PTR_ERR(priv->lin_dev);

As per my feedback on an earlier patch in the series, in the case where priv->lin_dev is NULL,
this will result in the function returning 0. Is that ok?

Flagged by Smatch

Quoted 14 lines
drivers/net/can/lin-serdev.c +14 -0
+		goto err_register_lin;
+	}
+
+	serdev_device_close(serdev);
+	priv->is_stopped = true;
+
+	return 0;
+
+err_register_lin:
+	serdev_device_close(serdev);
+err_open:
+	kfifo_free(&priv->rx_fifo);
+	return ret;
+}

...

#22 lore raw

Re: [PATCH v2 07/12] can: Add support for serdev LIN adapters

Quoted 54 lines · Simon Horman
Quoted 6 lines · Christoph Fritz

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

...

Quoted 1 line
drivers/net/can/lin-serdev.c +0 -0

...

Quoted 24 lines
drivers/net/can/lin-serdev.c +24 -0
+static int linser_probe(struct serdev_device *serdev)
+{
+	struct linser_priv *priv;
+	int ret;
+
+	priv = devm_kzalloc(&serdev->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	ret = kfifo_alloc(&priv->rx_fifo, LINSER_RX_FIFO_SIZE, GFP_KERNEL);
+	if (ret)
+		return ret;
+
+	INIT_DELAYED_WORK(&priv->rx_work, linser_process_delayed);
+
+	priv->serdev = serdev;
+	serdev_device_set_drvdata(serdev, priv);
+	serdev_device_set_client_ops(serdev, &linser_ops);
+
+	ret = serdev_device_open(serdev);
+	if (ret) {
+		dev_err(&serdev->dev, "Unable to open device\n");
+		goto err_open;
+	}

drivers/net/can/lin-serdev.c line 470

Quoted 10 lines
drivers/net/can/lin-serdev.c +10 -0
+
+	serdev_device_set_flow_control(serdev, false);
+	serdev_device_set_break_detection(serdev, true);
+	linser_derive_timings(priv, LIN_DEFAULT_BAUDRATE);
+
+	mutex_init(&priv->resp_lock);
+
+	priv->lin_dev = register_lin(&serdev->dev, &linser_lindev_ops);
+	if (IS_ERR_OR_NULL(priv->lin_dev)) {
+		ret = PTR_ERR(priv->lin_dev);

As per my feedback on an earlier patch in the series, in the case where priv->lin_dev is NULL,
this will result in the function returning 0. Is that ok?

Quoted 1 line

Flagged by Smatch

IS_ERR_OR_NULL() gets IS_ERR() in upcoming v4

Thanks
-- Christoph

Quoted 17 lines
Quoted 15 lines
drivers/net/can/lin-serdev.c +14 -0
+		goto err_register_lin;
+	}
+
+	serdev_device_close(serdev);
+	priv->is_stopped = true;
+
+	return 0;
+
+err_register_lin:
+	serdev_device_close(serdev);
+err_open:
+	kfifo_free(&priv->rx_fifo);
+	return ret;
+}

...

#23 lore raw

PATCH v2 8/12 can: lin: Add special frame id for rx offload config

In reply to Christoph Fritz

A LIN bus supports up to 64 identifiers in one byte. This commit adds a special frame ID, beyond the actual LIN identifiers, for signaling RX offload configuration requests. This ID will be utilized in future LIN enhancements to the CAN broadcast manager.

---
 include/net/lin.h | 1 +
 1 file changed, 1 insertion(+)
include/net/lin.h +1 -0
index 944e775c40e79..e7c7c820a6e18 100644
@@ -19,6 +19,7 @@
 
 #define LIN_ID_MASK		GENMASK(5, 0)
 /* special ID descriptions for LIN */
+#define LIN_RXOFFLOAD_DATA_FLAG	0x00000200U
 #define LIN_ENHANCED_CKSUM_FLAG	0x00000100U
 
 extern u8 lin_get_id_parity(u8 id);
Signature · 1 line
2.39.2
#24 lore raw

Re: [PATCH v2 08/12] can: lin: Add special frame id for rx offload config

Quoted 8 lines · Christoph Fritz

A LIN bus supports up to 64 identifiers in one byte. This commit adds a special frame ID, beyond the actual LIN identifiers, for signaling RX offload configuration requests. This ID will be utilized in future LIN enhancements to the CAN broadcast manager.

---
 include/net/lin.h | 1 +

You just added this file in other patch. What is the point of splitting line-per-line additions? There is no user of this in this patch. Squash it with the patch adding the file.

Best regards,
Krzysztof

#25 lore raw

Re: [PATCH v2 08/12] can: lin: Add special frame id for rx offload config

Quoted 6 lines · Krzysztof Kozlowski
Quoted 5 lines · Christoph Fritz

A LIN bus supports up to 64 identifiers in one byte. This commit adds a special frame ID, beyond the actual LIN identifiers, for signaling RX offload configuration requests. This ID will be utilized in future LIN enhancements to the CAN broadcast manager.

Quoted 6 lines
Quoted 3 lines
---
 include/net/lin.h | 1 +

You just added this file in other patch. What is the point of splitting line-per-line additions?

My intention was to make the review process easier by separating the BCM (Broadcast Manager) logic from the basic driver implementation.

Quoted 2 lines

There is no user of this in this patch. Squash it with the patch adding the file.

OK

v3 is coming up

Thanks
-- Christoph

#26 lore raw

PATCH v2 9/12 can: bcm: Add LIN answer offloading for responder mode

In reply to Christoph Fritz

Enhance CAN broadcast manager with RX_LIN_SETUP and RX_LIN_DELETE operations to setup automatic LIN frame responses in responder mode.

Additionally, the patch introduces the LIN_EVENT_FRAME flag to setup event-triggered LIN frames.

---
 include/uapi/linux/can/bcm.h |  5 ++-
 net/can/bcm.c                | 74 +++++++++++++++++++++++++++++++++++-
 2 files changed, 77 insertions(+), 2 deletions(-)
include/uapi/linux/can/bcm.h +4 -1
index f1e45f533a72c..c46268a114078 100644
@@ -86,7 +86,9 @@ enum {
 	TX_EXPIRED,	/* notification on performed transmissions (count=0) */
 	RX_STATUS,	/* reply to RX_READ request */
 	RX_TIMEOUT,	/* cyclic message is absent */
-	RX_CHANGED	/* updated CAN frame (detected content change) */
+	RX_CHANGED,	/* updated CAN frame (detected content change) */
+	RX_LIN_SETUP,	/* create auto-response for LIN frame */
+	RX_LIN_DELETE,  /* remove auto-response for LIN frame */
 };
 
 #define SETTIMER            0x0001
@@ -101,5 +103,6 @@ enum {
 #define TX_RESET_MULTI_IDX  0x0200
 #define RX_RTR_FRAME        0x0400
 #define CAN_FD_FRAME        0x0800
+#define LIN_EVENT_FRAME     0x1000
 
 #endif /* !_UAPI_CAN_BCM_H */
net/can/bcm.c +73 -1

97 lines not on this page · raw message

Signature · 1 line
2.39.2
#27 lore raw

PATCH v2 10/12 can: lin: Handle rx offload config frames

In reply to Christoph Fritz

The CAN Broadcast Manager now has the capability to dispatch CANFD frames marked with the id LINBUS_RXOFFLOAD_ID. This patch introduces functionality to interpret these specific frames, enabling the configuration of RX offloading within the LIN driver.

---
 drivers/net/can/lin.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
drivers/net/can/lin.c +29 -0
index 95906003666fb..ee2ebea2c865f 100644
@@ -194,6 +194,27 @@ static void lin_remove_sysfs_id_files(struct net_device *ndev)
 	}
 }
 
+static int lin_setup_rxoffload(struct lin_device *ldev,
+			       struct canfd_frame *cfd)
+{
+	struct lin_responder_answer answ;
+
+	if (!(cfd->flags & CANFD_FDF))
+		return -EMSGSIZE;
+
+	BUILD_BUG_ON(sizeof(struct lin_responder_answer) > sizeof(cfd->data));
+	memcpy(&answ, cfd->data, sizeof(struct lin_responder_answer));
+
+	answ.lf.checksum_mode = (cfd->can_id & LIN_ENHANCED_CKSUM_FLAG) ?
+			LINBUS_ENHANCED : LINBUS_CLASSIC;
+
+	if (answ.lf.lin_id > LIN_ID_MASK ||
+	    answ.event_associated_id > LIN_ID_MASK)
+		return -EINVAL;
+
+	return ldev->ldev_ops->update_responder_answer(ldev, &answ);
+}
+
 static void lin_tx_work_handler(struct work_struct *ws)
 {
 	struct lin_device *ldev = container_of(ws, struct lin_device,
@@ -206,6 +227,14 @@ static void lin_tx_work_handler(struct work_struct *ws)
 	ldev->tx_busy = true;
 
 	cfd = (struct canfd_frame *)ldev->tx_skb->data;
+
+	if (cfd->can_id & LIN_RXOFFLOAD_DATA_FLAG) {
+		ret = lin_setup_rxoffload(ldev, cfd);
+		if (ret < 0)
+			netdev_err(ndev, "setting up rx failed %d\n", ret);
+		goto lin_tx_out;
+	}
+
 	lf.checksum_mode = (cfd->can_id & LIN_ENHANCED_CKSUM_FLAG) ?
 			   LINBUS_ENHANCED : LINBUS_CLASSIC;
 	lf.lin_id = cfd->can_id & LIN_ID_MASK;
Signature · 1 line
2.39.2
#28 lore raw

PATCH v2 11/12 can: lin: Support setting LIN mode

In reply to Christoph Fritz

A LIN node can work as commander or responder. This patch is introducing a new control mode (CAN_CTRLMODE_LIN_COMMANDER), so that e.g. the ip tool from iproute2 can turn on commander mode when the device is being brought up.

---
 drivers/net/can/lin.c            | 40 +++++++++++++++++++++++++++++++-
 include/net/lin.h                |  7 ++++++
 include/uapi/linux/can/netlink.h |  1 +
 3 files changed, 47 insertions(+), 1 deletion(-)
drivers/net/can/lin.c +39 -1
index ee2ebea2c865f..96cd016228fea 100644
@@ -271,11 +271,40 @@ static netdev_tx_t lin_start_xmit(struct sk_buff *skb,
 	return NETDEV_TX_OK;
 }
 
+static int lin_update_mode(struct net_device *ndev)
+{
+	struct lin_device *ldev = netdev_priv(ndev);
+	u32 ctrlmode = ldev->can.ctrlmode;
+	enum lin_mode lm;
+	int ret = 0;
+
+	lm = (ctrlmode & CAN_CTRLMODE_LIN_COMMANDER) ? LINBUS_COMMANDER :
+						       LINBUS_RESPONDER;
+	if (ldev->lmode != lm) {
+		if (!ldev->ldev_ops->update_lin_mode) {
+			netdev_err(ndev, "setting lin mode unsupported\n");
+			return -EINVAL;
+		}
+		ret = ldev->ldev_ops->update_lin_mode(ldev, lm);
+		if (ret) {
+			netdev_err(ndev, "Failed to set lin mode: %d\n", ret);
+			return ret;
+		}
+		ldev->lmode = lm;
+	}
+
+	return ret;
+}
+
 static int lin_open(struct net_device *ndev)
 {
 	struct lin_device *ldev = netdev_priv(ndev);
 	int ret;
 
+	ret = lin_update_mode(ndev);
+	if (ret)
+		return ret;
+
 	ldev->tx_busy = false;
 
 	ret = open_candev(ndev);
@@ -451,7 +480,7 @@ struct lin_device *register_lin(struct device *dev,
 	ndev->mtu = CANFD_MTU;
 	ldev->can.bittiming.bitrate = LIN_DEFAULT_BAUDRATE;
 	ldev->can.ctrlmode = CAN_CTRLMODE_LIN;
-	ldev->can.ctrlmode_supported = 0;
+	ldev->can.ctrlmode_supported = CAN_CTRLMODE_LIN_COMMANDER;
 	ldev->can.bitrate_const = lin_bitrate;
 	ldev->can.bitrate_const_cnt = ARRAY_SIZE(lin_bitrate);
 	ldev->can.do_set_bittiming = lin_set_bittiming;
@@ -466,6 +495,15 @@ struct lin_device *register_lin(struct device *dev,
 		goto exit_candev;
 	}
 
+	ldev->lmode = LINBUS_RESPONDER;
+	if (ldev->ldev_ops->update_lin_mode) {
+		ret = ldev->ldev_ops->update_lin_mode(ldev, ldev->lmode);
+		if (ret) {
+			netdev_err(ndev, "updating lin mode failed\n");
+			goto exit_candev;
+		}
+	}
+
 	ret = register_candev(ndev);
 	if (ret)
 		goto exit_candev;
include/net/lin.h +7 -0
index e7c7c820a6e18..e80a4509b7a8c 100644
@@ -36,6 +36,11 @@ struct lin_attr {
 	struct lin_device *ldev;
 };
 
+enum lin_mode {
+	LINBUS_RESPONDER = 0,
+	LINBUS_COMMANDER,
+};
+
 struct lin_device {
 	struct can_priv can;  /* must be the first member */
 	struct net_device *ndev;
@@ -47,6 +52,7 @@ struct lin_device {
 	struct sk_buff *tx_skb;
 	struct kobject *lin_ids_kobj;
 	struct lin_attr sysfs_entries[LIN_NUM_IDS];
+	enum lin_mode lmode;
 };
 
 enum lin_checksum_mode {
@@ -73,6 +79,7 @@ struct lin_device_ops {
 	int (*ldo_open)(struct lin_device *ldev);
 	int (*ldo_stop)(struct lin_device *ldev);
 	int (*ldo_tx)(struct lin_device *ldev, const struct lin_frame *frame);
+	int (*update_lin_mode)(struct lin_device *ldev, enum lin_mode lm);
 	int (*update_bitrate)(struct lin_device *ldev, u16 bitrate);
 	int (*update_responder_answer)(struct lin_device *ldev,
 				       const struct lin_responder_answer *answ);
include/uapi/linux/can/netlink.h +1 -0
index 51b0e2a7624e4..6c84a7666c646 100644
@@ -104,6 +104,7 @@ struct can_ctrlmode {
 #define CAN_CTRLMODE_TDC_AUTO		0x200	/* CAN transiver automatically calculates TDCV */
 #define CAN_CTRLMODE_TDC_MANUAL		0x400	/* TDCV is manually set up by user */
 #define CAN_CTRLMODE_LIN		0x800	/* LIN bus mode */
+#define CAN_CTRLMODE_LIN_COMMANDER	0x1000	/* LIN bus specific commander mode */
 
 /*
  * CAN device statistics
Signature · 1 line
2.39.2
#29 lore raw

PATCH v2 12/12 HID: hexLIN: Implement ability to update lin mode

In reply to Christoph Fritz

This patch enhances the hexLIN driver by implementing the newly introduced update_lin_mode() callback. So that either commander or responder mode can be configured on this hardware.

---
 drivers/hid/hid-hexdev-hexlin.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/hid/hid-hexdev-hexlin.c +11 -0
index 34019043f329e..64c1afcba0550 100644
@@ -180,6 +180,8 @@ HEXLIN_GET_CMD(get_baudrate, HEXLIN_GET_BAUDRATE)
 	}
 
 HEXLIN_VAL_CMD(send_break, HEXLIN_SEND_BREAK, hexlin_val8_req, u8)
+HEXLIN_VAL_CMD(set_mode_controller, HEXLIN_SET_MODE_CONTROLLER, hexlin_val8_req,
+	       bool)
 
 static int hexlin_queue_frames_insert(struct hexlin_priv_data *priv,
 				      const u8 *raw_data, int sz)
@@ -349,6 +351,14 @@ static int hexlin_ldo_tx(struct lin_device *ldev,
 	return ret;
 }
 
+static int  hexlin_update_lin_mode(struct lin_device *ldev, enum lin_mode lm)
+{
+	struct hid_device *hdev = to_hid_device(ldev->dev);
+	struct hexlin_priv_data *priv = hid_get_drvdata(hdev);
+
+	return hexlin_set_mode_controller(priv, lm == LINBUS_COMMANDER);
+}
+
 static int hexlin_update_bitrate(struct lin_device *ldev, u16 bitrate)
 {
 	struct hid_device *hdev = to_hid_device(ldev->dev);
@@ -420,6 +430,7 @@ static const struct lin_device_ops hexlin_ldo = {
 	.ldo_open = hexlin_open,
 	.ldo_stop = hexlin_stop,
 	.ldo_tx = hexlin_ldo_tx,
+	.update_lin_mode = hexlin_update_lin_mode,
 	.update_bitrate = hexlin_update_bitrate,
 	.get_responder_answer = hexlin_get_responder_answer,
 	.update_responder_answer = hexlin_update_resp_answer,
Signature · 1 line
2.39.2