linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] can: peak_usb: export PCAN device ID as sysfs device attribute
@ 2022-08-01  8:04 Lukas Magel
  2022-08-01 14:36 ` Vincent MAILHOL
                   ` (13 more replies)
  0 siblings, 14 replies; 48+ messages in thread
From: Lukas Magel @ 2022-08-01  8:04 UTC (permalink / raw)
  To: linux-can; +Cc: Lukas Magel, Vincent Mailhol

Peak USB devices support a configurable u8 / u32 device ID. In
multi-device setups, this device ID can be configured and used to
identify individual CAN interfaces independent of the order in which
they are plugged into the system. At the current time, the device ID
is already queried from the device and stored in the peak_usb_device
struct.

This patch exports the device ID (called device_number in the struct)
as a sysfs attribute. The attribute name was chosen to be device_id
instead of device_number because the latter has been deprecated by Peak
in their API.

Signed-off-by: Lukas Magel <lukas.magel@posteo.net>
Reviewed-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
---
For the moment, the patch only implements read support for the device
ID. My next goal is to also implement write access. However, this
will require a more significant modification of the driver since the
corresponding commands for ID retrieval and configuration are not
implemented for all device types.

 drivers/net/can/usb/peak_usb/pcan_usb_core.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.c b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
index 27b0a72fd885..43df178e9473 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
@@ -15,6 +15,8 @@
 #include <linux/netdevice.h>
 #include <linux/usb.h>
 #include <linux/ethtool.h>
+#include <linux/device.h>
+#include <linux/sysfs.h>
 
 #include <linux/can.h>
 #include <linux/can/dev.h>
@@ -53,6 +55,15 @@ static const struct usb_device_id peak_usb_table[] = {
 
 MODULE_DEVICE_TABLE(usb, peak_usb_table);
 
+static ssize_t device_id_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+	struct net_device *netdev = to_net_dev(dev);
+	struct peak_usb_device *peak_dev = netdev_priv(netdev);
+
+	return sysfs_emit(buf, "%08X\n", peak_dev->device_number);
+}
+static DEVICE_ATTR_RO(device_id);
+
 /*
  * dump memory
  */
@@ -887,6 +898,11 @@ static int peak_usb_create_dev(const struct peak_usb_adapter *peak_usb_adapter,
 	netdev_info(netdev, "attached to %s channel %u (device %u)\n",
 			peak_usb_adapter->name, ctrl_idx, dev->device_number);
 
+	err = device_create_file(&netdev->dev, &dev_attr_device_id);
+	/* Do not error out since device was configured successfully */
+	if (err)
+		netdev_warn(netdev, "unable to expose device_id via sysfs %d\n", err);
+
 	return 0;
 
 adap_dev_free:
@@ -923,6 +939,8 @@ static void peak_usb_disconnect(struct usb_interface *intf)
 		dev->state &= ~PCAN_USB_STATE_CONNECTED;
 		strlcpy(name, netdev->name, IFNAMSIZ);
 
+		device_remove_file(&netdev->dev, &dev_attr_device_id);
+
 		unregister_netdev(netdev);
 
 		kfree(dev->cmd_buf);
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

end of thread, other threads:[~2023-02-02 16:45 UTC | newest]

Thread overview: 48+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-01  8:04 [PATCH v1] can: peak_usb: export PCAN device ID as sysfs device attribute Lukas Magel
2022-08-01 14:36 ` Vincent MAILHOL
2022-08-02 19:26   ` [PATCH v2] " Lukas Magel
2022-09-15  9:54 ` [RESEND PATCH " Lukas Magel
2022-09-28 20:43   ` Lukas Magel
2022-09-30 12:06     ` Marc Kleine-Budde
2022-09-30 14:20       ` Marc Kleine-Budde
2022-10-22 21:57         ` Lukas Magel
2022-10-22 21:35 ` [PATCH 0/7] can: peak_usb: Introduce configurable user dev id Lukas Magel
2022-10-22 21:35   ` [PATCH 1/7] can: peak_usb: rename device_id to a more explicit name Lukas Magel
2022-10-22 21:35   ` [PATCH 2/7] can: peak_usb: add callback to read user value of CANFD devices Lukas Magel
2022-10-22 21:35   ` [PATCH 3/7] can: peak_usb: allow flashing of the user device id Lukas Magel
2022-10-22 21:35   ` [PATCH 4/7] can: peak_usb: replace unregister_netdev() with unregister_candev() Lukas Magel
2022-10-25 13:30     ` Marc Kleine-Budde
2022-10-22 21:35   ` [PATCH 5/7] can: peak_usb: add ethtool interface to user defined flashed device number Lukas Magel
2022-10-25 14:54     ` Marc Kleine-Budde
2022-10-22 21:35   ` [PATCH 6/7] can: peak_usb: export PCAN user device ID as sysfs device attribute Lukas Magel
2022-10-25 13:30     ` Marc Kleine-Budde
2022-10-22 21:35   ` [PATCH 7/7] can: peak_usb: align user device id format in log with sysfs attribute Lukas Magel
2022-10-25 11:28     ` Marc Kleine-Budde
2022-10-25 11:44       ` Marc Kleine-Budde
2022-10-30 10:59 ` [PATCH v2 0/7] can: peak_usb: Introduce configurable user dev id Lukas Magel
2022-10-30 10:59   ` [PATCH v2 1/7] can: peak_usb: rename device_id to a more explicit name Lukas Magel
2022-10-30 10:59   ` [PATCH v2 2/7] can: peak_usb: add callback to read user value of CANFD devices Lukas Magel
2022-10-30 10:59   ` [PATCH v2 3/7] can: peak_usb: allow flashing of the user device id Lukas Magel
2022-10-30 10:59   ` [PATCH v2 4/7] can: peak_usb: replace unregister_netdev() with unregister_candev() Lukas Magel
2022-10-30 10:59   ` [PATCH v2 5/7] can: peak_usb: add ethtool interface to user defined flashed device number Lukas Magel
2022-10-30 10:59   ` [PATCH v2 6/7] can: peak_usb: export PCAN user device ID as sysfs device attribute Lukas Magel
2022-10-30 10:59   ` [PATCH v2 7/7] can: peak_usb: align user device id format in log with sysfs attribute Lukas Magel
2022-12-13  8:03 ` [PATCH v3 0/7] can: peak_usb: Introduce configurable CAN channel ID Lukas Magel
2022-12-13  8:03 ` [PATCH v3 1/8] can: peak_usb: rename device_id to " Lukas Magel
2022-12-13  8:03 ` [PATCH v3 2/8] can: peak_usb: add callback to read CAN channel ID of PEAK CAN-FD devices Lukas Magel
2022-12-13  8:03 ` [PATCH v3 3/8] can: peak_usb: allow flashing of the CAN channel ID Lukas Magel
2022-12-13  8:03 ` [PATCH v3 4/8] can: peak_usb: replace unregister_netdev() with unregister_candev() Lukas Magel
2022-12-13  8:03 ` [PATCH v3 5/8] can: peak_usb: add ethtool interface to user-configurable CAN channel identifier Lukas Magel
2022-12-13  8:03 ` [PATCH v3 6/8] can: peak_usb: export PCAN CAN channel ID as sysfs device attribute Lukas Magel
2022-12-13  8:03 ` [PATCH v3 7/8] can: peak_usb: align CAN channel ID format in log with sysfs attribute Lukas Magel
2022-12-13  8:03 ` [PATCH v3 8/8] can: peak_usb: Reorder include directives alphabetically Lukas Magel
2023-01-16 20:09 ` [RESEND PATCH v3 0/8] can: peak_usb: Introduce configurable CAN channel ID Lukas Magel
2023-01-16 20:09   ` [PATCH v3 1/8] can: peak_usb: rename device_id to " Lukas Magel
2023-01-16 20:09   ` [PATCH v3 2/8] can: peak_usb: add callback to read CAN channel ID of PEAK CAN-FD devices Lukas Magel
2023-01-16 20:09   ` [PATCH v3 3/8] can: peak_usb: allow flashing of the CAN channel ID Lukas Magel
2023-01-16 20:09   ` [PATCH v3 4/8] can: peak_usb: replace unregister_netdev() with unregister_candev() Lukas Magel
2023-01-16 20:09   ` [PATCH v3 5/8] can: peak_usb: add ethtool interface to user-configurable CAN channel identifier Lukas Magel
2023-01-16 20:09   ` [PATCH v3 6/8] can: peak_usb: export PCAN CAN channel ID as sysfs device attribute Lukas Magel
2023-01-16 20:09   ` [PATCH v3 7/8] can: peak_usb: align CAN channel ID format in log with sysfs attribute Lukas Magel
2023-01-16 20:09   ` [PATCH v3 8/8] can: peak_usb: Reorder include directives alphabetically Lukas Magel
2023-02-02 16:45   ` [RESEND PATCH v3 0/8] can: peak_usb: Introduce configurable CAN channel ID Marc Kleine-Budde

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).