linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/6] can: peak_usb: add sysfs interface to flashed value
@ 2021-11-17 15:01 Stephane Grosjean
  2021-11-17 15:01 ` [PATCH 1/6] can: peak_usb: rename a callback to a more explicit name Stephane Grosjean
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: Stephane Grosjean @ 2021-11-17 15:01 UTC (permalink / raw)
  To: linux-can Mailing List; +Cc: Stephane Grosjean

This series of patches offers the user the possibility to access in
read/write mode a flashed numerical value for each channel of a PEAK-System
USB - CAN/CANFD interface, through the sysfs interface, under the tree
/sys/class/net/canX and the new entry "dev_num" of the newly created group
"peak_usb". Thus the command :

$ echo /sys/class/net/can0/peak_usb/dev_num

will display the value stored in the internal memory, for the first channel
of the PEAK-System device.

Signed-off-by: Stephane Grosjean <s.grosjean@peak-system.com>

Stephane Grosjean (6):
  can: peak_usb: rename a callback to a more explicit name
  can: peak_usb: add callback to read user value of CANFD devices
  can: peak_usb: correction of a wrong field name
  can: peak_usb: allow flashing of user defined value
  can: peak_usb: replace unregister_netdev() with unregister_candev()
  can: peak_usb: add sysfs interface to internal device user value

 drivers/net/can/usb/peak_usb/pcan_usb.c      | 26 ++++++-
 drivers/net/can/usb/peak_usb/pcan_usb_core.c | 82 +++++++++++++++++++-
 drivers/net/can/usb/peak_usb/pcan_usb_core.h |  3 +-
 drivers/net/can/usb/peak_usb/pcan_usb_fd.c   | 48 ++++++++++++
 drivers/net/can/usb/peak_usb/pcan_usb_pro.c  | 23 +++++-
 drivers/net/can/usb/peak_usb/pcan_usb_pro.h  |  3 +-
 6 files changed, 173 insertions(+), 12 deletions(-)

-- 
2.25.1


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

* [PATCH 1/6] can: peak_usb: rename a callback to a more explicit name
  2021-11-17 15:01 [PATCH 0/6] can: peak_usb: add sysfs interface to flashed value Stephane Grosjean
@ 2021-11-17 15:01 ` Stephane Grosjean
  2021-11-17 15:01 ` [PATCH 2/6] can: peak_usb: add callback to read user value of CANFD devices Stephane Grosjean
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Stephane Grosjean @ 2021-11-17 15:01 UTC (permalink / raw)
  To: linux-can Mailing List; +Cc: Stephane Grosjean

The so-called "device id" is in fact a value defined by the user.
Therefore, in order not to confuse it with the device id used by the USB,
the functions for reading this user-defined value are renamed.

Signed-off-by: Stephane Grosjean <s.grosjean@peak-system.com>
---
 drivers/net/can/usb/peak_usb/pcan_usb.c      | 6 +++---
 drivers/net/can/usb/peak_usb/pcan_usb_core.c | 4 ++--
 drivers/net/can/usb/peak_usb/pcan_usb_core.h | 2 +-
 drivers/net/can/usb/peak_usb/pcan_usb_pro.c  | 6 +++---
 4 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/drivers/net/can/usb/peak_usb/pcan_usb.c b/drivers/net/can/usb/peak_usb/pcan_usb.c
index 837b3fecd71e..9c5ce9575d05 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb.c
@@ -380,9 +380,9 @@ static int pcan_usb_get_serial(struct peak_usb_device *dev, u32 *serial_number)
 }
 
 /*
- * read device id from device
+ * read user device id from device
  */
-static int pcan_usb_get_device_id(struct peak_usb_device *dev, u32 *device_id)
+static int pcan_usb_get_user_devid(struct peak_usb_device *dev, u32 *device_id)
 {
 	u8 args[PCAN_USB_CMD_ARGS_LEN];
 	int err;
@@ -1016,7 +1016,7 @@ const struct peak_usb_adapter pcan_usb = {
 	.dev_init = pcan_usb_init,
 	.dev_set_bus = pcan_usb_write_mode,
 	.dev_set_bittiming = pcan_usb_set_bittiming,
-	.dev_get_device_id = pcan_usb_get_device_id,
+	.dev_get_user_devid = pcan_usb_get_user_devid,
 	.dev_decode_buf = pcan_usb_decode_buf,
 	.dev_encode_msg = pcan_usb_encode_msg,
 	.dev_start = pcan_usb_start,
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 6107fef9f4a0..a83ac4672123 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
@@ -883,8 +883,8 @@ static int peak_usb_create_dev(const struct peak_usb_adapter *peak_usb_adapter,
 	}
 
 	/* get device number early */
-	if (dev->adapter->dev_get_device_id)
-		dev->adapter->dev_get_device_id(dev, &dev->device_number);
+	if (dev->adapter->dev_get_user_devid)
+		dev->adapter->dev_get_user_devid(dev, &dev->device_number);
 
 	netdev_info(netdev, "attached to %s channel %u (device %u)\n",
 			peak_usb_adapter->name, ctrl_idx, dev->device_number);
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.h b/drivers/net/can/usb/peak_usb/pcan_usb_core.h
index daa19f57e742..93f5112f1d14 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.h
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.h
@@ -60,7 +60,7 @@ struct peak_usb_adapter {
 	int (*dev_set_data_bittiming)(struct peak_usb_device *dev,
 				      struct can_bittiming *bt);
 	int (*dev_set_bus)(struct peak_usb_device *dev, u8 onoff);
-	int (*dev_get_device_id)(struct peak_usb_device *dev, u32 *device_id);
+	int (*dev_get_user_devid)(struct peak_usb_device *dev, u32 *device_id);
 	int (*dev_decode_buf)(struct peak_usb_device *dev, struct urb *urb);
 	int (*dev_encode_msg)(struct peak_usb_device *dev, struct sk_buff *skb,
 					u8 *obuf, size_t *size);
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
index 858ab22708fc..2716c9a23e51 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
@@ -419,8 +419,8 @@ static int pcan_usb_pro_set_led(struct peak_usb_device *dev, u8 mode,
 	return pcan_usb_pro_send_cmd(dev, &um);
 }
 
-static int pcan_usb_pro_get_device_id(struct peak_usb_device *dev,
-				      u32 *device_id)
+static int pcan_usb_pro_get_user_devid(struct peak_usb_device *dev,
+				       u32 *device_id)
 {
 	struct pcan_usb_pro_devid *pdn;
 	struct pcan_usb_pro_msg um;
@@ -1075,7 +1075,7 @@ const struct peak_usb_adapter pcan_usb_pro = {
 	.dev_free = pcan_usb_pro_free,
 	.dev_set_bus = pcan_usb_pro_set_bus,
 	.dev_set_bittiming = pcan_usb_pro_set_bittiming,
-	.dev_get_device_id = pcan_usb_pro_get_device_id,
+	.dev_get_user_devid = pcan_usb_pro_get_user_devid,
 	.dev_decode_buf = pcan_usb_pro_decode_buf,
 	.dev_encode_msg = pcan_usb_pro_encode_msg,
 	.dev_start = pcan_usb_pro_start,
-- 
2.25.1


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

* [PATCH 2/6] can: peak_usb: add callback to read user value of CANFD devices
  2021-11-17 15:01 [PATCH 0/6] can: peak_usb: add sysfs interface to flashed value Stephane Grosjean
  2021-11-17 15:01 ` [PATCH 1/6] can: peak_usb: rename a callback to a more explicit name Stephane Grosjean
@ 2021-11-17 15:01 ` Stephane Grosjean
  2021-11-17 15:01 ` [PATCH 3/6] can: peak_usb: correction of a wrong field name Stephane Grosjean
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Stephane Grosjean @ 2021-11-17 15:01 UTC (permalink / raw)
  To: linux-can Mailing List; +Cc: Stephane Grosjean

This patch adds the specific function that allows to read a user defined
value from the non volatile memory of the USB CANFD interfaces of
PEAK-System.

Signed-off-by: Stephane Grosjean <s.grosjean@peak-system.com>
---
 drivers/net/can/usb/peak_usb/pcan_usb_fd.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
index 6bd12549f101..76fe067f146b 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
@@ -420,6 +420,24 @@ static int pcan_usb_fd_set_bittiming_fast(struct peak_usb_device *dev,
 	return pcan_usb_fd_send_cmd(dev, ++cmd);
 }
 
+/* read user device id from device */
+static int pcan_usb_fd_get_user_devid(struct peak_usb_device *dev,
+				      u32 *device_id)
+{
+	struct pcan_usb_fd_device *pdev =
+		container_of(dev, struct pcan_usb_fd_device, dev);
+	struct pcan_ufd_fw_info *fw_info = &pdev->usb_if->fw_info;
+	int err;
+
+	err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
+				    PCAN_USBPRO_INFO_FW,
+				    fw_info, sizeof(*fw_info));
+	if (!err)
+		*device_id = le32_to_cpu(fw_info->dev_id[dev->ctrl_idx]);
+
+	return err;
+}
+
 /* handle restart but in asynchronously way
  * (uses PCAN-USB Pro code to complete asynchronous request)
  */
@@ -1102,6 +1120,7 @@ const struct peak_usb_adapter pcan_usb_fd = {
 	.dev_set_bus = pcan_usb_fd_set_bus,
 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
+	.dev_get_user_devid = pcan_usb_fd_get_user_devid,
 	.dev_decode_buf = pcan_usb_fd_decode_buf,
 	.dev_start = pcan_usb_fd_start,
 	.dev_stop = pcan_usb_fd_stop,
@@ -1176,6 +1195,7 @@ const struct peak_usb_adapter pcan_usb_chip = {
 	.dev_set_bus = pcan_usb_fd_set_bus,
 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
+	.dev_get_user_devid = pcan_usb_fd_get_user_devid,
 	.dev_decode_buf = pcan_usb_fd_decode_buf,
 	.dev_start = pcan_usb_fd_start,
 	.dev_stop = pcan_usb_fd_stop,
@@ -1250,6 +1270,7 @@ const struct peak_usb_adapter pcan_usb_pro_fd = {
 	.dev_set_bus = pcan_usb_fd_set_bus,
 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
+	.dev_get_user_devid = pcan_usb_fd_get_user_devid,
 	.dev_decode_buf = pcan_usb_fd_decode_buf,
 	.dev_start = pcan_usb_fd_start,
 	.dev_stop = pcan_usb_fd_stop,
@@ -1324,6 +1345,7 @@ const struct peak_usb_adapter pcan_usb_x6 = {
 	.dev_set_bus = pcan_usb_fd_set_bus,
 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
+	.dev_get_user_devid = pcan_usb_fd_get_user_devid,
 	.dev_decode_buf = pcan_usb_fd_decode_buf,
 	.dev_start = pcan_usb_fd_start,
 	.dev_stop = pcan_usb_fd_stop,
-- 
2.25.1


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

* [PATCH 3/6] can: peak_usb: correction of a wrong field name
  2021-11-17 15:01 [PATCH 0/6] can: peak_usb: add sysfs interface to flashed value Stephane Grosjean
  2021-11-17 15:01 ` [PATCH 1/6] can: peak_usb: rename a callback to a more explicit name Stephane Grosjean
  2021-11-17 15:01 ` [PATCH 2/6] can: peak_usb: add callback to read user value of CANFD devices Stephane Grosjean
@ 2021-11-17 15:01 ` Stephane Grosjean
  2021-11-17 15:01 ` [PATCH 4/6] can: peak_usb: allow flashing of user defined value Stephane Grosjean
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Stephane Grosjean @ 2021-11-17 15:01 UTC (permalink / raw)
  To: linux-can Mailing List; +Cc: Stephane Grosjean

This field read from the PCAN-USB Pro device's flash memory contains a
user value rather than a serial number.

Signed-off-by: Stephane Grosjean <s.grosjean@peak-system.com>
---
 drivers/net/can/usb/peak_usb/pcan_usb_pro.c | 2 +-
 drivers/net/can/usb/peak_usb/pcan_usb_pro.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
index 2716c9a23e51..6c73eb9583bc 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
@@ -439,7 +439,7 @@ static int pcan_usb_pro_get_user_devid(struct peak_usb_device *dev,
 		return err;
 
 	pdn = (struct pcan_usb_pro_devid *)pc;
-	*device_id = le32_to_cpu(pdn->serial_num);
+	*device_id = le32_to_cpu(pdn->dev_num);
 
 	return err;
 }
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_pro.h b/drivers/net/can/usb/peak_usb/pcan_usb_pro.h
index 5d4cf14eb9d9..a34e0fc021c9 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_pro.h
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_pro.h
@@ -112,7 +112,7 @@ struct __packed pcan_usb_pro_devid {
 	u8 data_type;
 	u8 channel;
 	__le16 dummy;
-	__le32 serial_num;
+	__le32 dev_num;
 };
 
 #define PCAN_USBPRO_LED_DEVICE		0x00
-- 
2.25.1


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

* [PATCH 4/6] can: peak_usb: allow flashing of user defined value
  2021-11-17 15:01 [PATCH 0/6] can: peak_usb: add sysfs interface to flashed value Stephane Grosjean
                   ` (2 preceding siblings ...)
  2021-11-17 15:01 ` [PATCH 3/6] can: peak_usb: correction of a wrong field name Stephane Grosjean
@ 2021-11-17 15:01 ` Stephane Grosjean
  2021-11-17 15:01 ` [PATCH 5/6] can: peak_usb: replace unregister_netdev() with unregister_candev() Stephane Grosjean
  2021-11-17 15:01 ` [PATCH 6/6] can: peak_usb: add sysfs interface to internal device user value Stephane Grosjean
  5 siblings, 0 replies; 8+ messages in thread
From: Stephane Grosjean @ 2021-11-17 15:01 UTC (permalink / raw)
  To: linux-can Mailing List; +Cc: Stephane Grosjean

This series of patches adds a callback that allows the user to flash a
self-defined value to all USB - CAN/CANFD interfaces of PEAK-System managed
by this driver, namely:
- PCAN-USB
- PCAN-USB FD
- PCAN-USB Pro FD
- PCAN-USB X6
- PCAN-Chip USB
- PCAN-USB Pro

Signed-off-by: Stephane Grosjean <s.grosjean@peak-system.com>
---
 drivers/net/can/usb/peak_usb/pcan_usb.c      | 20 +++++++++++++++
 drivers/net/can/usb/peak_usb/pcan_usb_core.h |  1 +
 drivers/net/can/usb/peak_usb/pcan_usb_fd.c   | 26 ++++++++++++++++++++
 drivers/net/can/usb/peak_usb/pcan_usb_pro.c  | 15 +++++++++++
 drivers/net/can/usb/peak_usb/pcan_usb_pro.h  |  1 +
 5 files changed, 63 insertions(+)

diff --git a/drivers/net/can/usb/peak_usb/pcan_usb.c b/drivers/net/can/usb/peak_usb/pcan_usb.c
index 9c5ce9575d05..294b69eabda7 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb.c
@@ -397,6 +397,25 @@ static int pcan_usb_get_user_devid(struct peak_usb_device *dev, u32 *device_id)
 	return err;
 }
 
+/* set a new user device id in the flash memory of the device */
+static int pcan_usb_set_user_devid(struct peak_usb_device *dev, u32 devid)
+{
+	u8 args[PCAN_USB_CMD_ARGS_LEN];
+
+	/* this kind of device supports 8-bit values only */
+	if (devid > 255)
+		return -EINVAL;
+
+	/* during the flash process the device disconnects during ~1.25 s.:
+	 * prohibit access when interface is UP
+	 */
+	if (dev->netdev->flags & IFF_UP)
+		return -EBUSY;
+
+	args[0] = (u8)devid;
+	return pcan_usb_send_cmd(dev, PCAN_USB_CMD_DEVID, PCAN_USB_SET, args);
+}
+
 /*
  * update current time ref with received timestamp
  */
@@ -1017,6 +1036,7 @@ const struct peak_usb_adapter pcan_usb = {
 	.dev_set_bus = pcan_usb_write_mode,
 	.dev_set_bittiming = pcan_usb_set_bittiming,
 	.dev_get_user_devid = pcan_usb_get_user_devid,
+	.dev_set_user_devid = pcan_usb_set_user_devid,
 	.dev_decode_buf = pcan_usb_decode_buf,
 	.dev_encode_msg = pcan_usb_encode_msg,
 	.dev_start = pcan_usb_start,
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.h b/drivers/net/can/usb/peak_usb/pcan_usb_core.h
index 93f5112f1d14..8d978c4c3b92 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.h
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.h
@@ -61,6 +61,7 @@ struct peak_usb_adapter {
 				      struct can_bittiming *bt);
 	int (*dev_set_bus)(struct peak_usb_device *dev, u8 onoff);
 	int (*dev_get_user_devid)(struct peak_usb_device *dev, u32 *device_id);
+	int (*dev_set_user_devid)(struct peak_usb_device *dev, u32 device_id);
 	int (*dev_decode_buf)(struct peak_usb_device *dev, struct urb *urb);
 	int (*dev_encode_msg)(struct peak_usb_device *dev, struct sk_buff *skb,
 					u8 *obuf, size_t *size);
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
index 76fe067f146b..8b278e35ea45 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
@@ -136,6 +136,15 @@ struct __packed pcan_ufd_ovr_msg {
 	u8	unused[3];
 };
 
+#define PCAN_UFD_CMD_DEVID_SET		0x81
+
+struct __packed pcan_ufd_device_id {
+	__le16	opcode_channel;
+
+	u16	unused;
+	__le32	device_id;
+};
+
 static inline int pufd_omsg_get_channel(struct pcan_ufd_ovr_msg *om)
 {
 	return om->channel & 0xf;
@@ -438,6 +447,19 @@ static int pcan_usb_fd_get_user_devid(struct peak_usb_device *dev,
 	return err;
 }
 
+/* set a new user device id in the flash memory of the device */
+static int pcan_usb_fd_set_user_devid(struct peak_usb_device *dev, u32 devid)
+{
+	struct pcan_ufd_device_id *cmd = pcan_usb_fd_cmd_buffer(dev);
+
+	cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
+						       PCAN_UFD_CMD_DEVID_SET);
+	cmd->device_id = cpu_to_le32(devid);
+
+	/* send the command */
+	return pcan_usb_fd_send_cmd(dev, ++cmd);
+}
+
 /* handle restart but in asynchronously way
  * (uses PCAN-USB Pro code to complete asynchronous request)
  */
@@ -1121,6 +1143,7 @@ const struct peak_usb_adapter pcan_usb_fd = {
 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
 	.dev_get_user_devid = pcan_usb_fd_get_user_devid,
+	.dev_set_user_devid = pcan_usb_fd_set_user_devid,
 	.dev_decode_buf = pcan_usb_fd_decode_buf,
 	.dev_start = pcan_usb_fd_start,
 	.dev_stop = pcan_usb_fd_stop,
@@ -1196,6 +1219,7 @@ const struct peak_usb_adapter pcan_usb_chip = {
 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
 	.dev_get_user_devid = pcan_usb_fd_get_user_devid,
+	.dev_set_user_devid = pcan_usb_fd_set_user_devid,
 	.dev_decode_buf = pcan_usb_fd_decode_buf,
 	.dev_start = pcan_usb_fd_start,
 	.dev_stop = pcan_usb_fd_stop,
@@ -1271,6 +1295,7 @@ const struct peak_usb_adapter pcan_usb_pro_fd = {
 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
 	.dev_get_user_devid = pcan_usb_fd_get_user_devid,
+	.dev_set_user_devid = pcan_usb_fd_set_user_devid,
 	.dev_decode_buf = pcan_usb_fd_decode_buf,
 	.dev_start = pcan_usb_fd_start,
 	.dev_stop = pcan_usb_fd_stop,
@@ -1346,6 +1371,7 @@ const struct peak_usb_adapter pcan_usb_x6 = {
 	.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
 	.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
 	.dev_get_user_devid = pcan_usb_fd_get_user_devid,
+	.dev_set_user_devid = pcan_usb_fd_set_user_devid,
 	.dev_decode_buf = pcan_usb_fd_decode_buf,
 	.dev_start = pcan_usb_fd_start,
 	.dev_stop = pcan_usb_fd_stop,
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
index 6c73eb9583bc..143ed37e3956 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
@@ -76,6 +76,7 @@ static u16 pcan_usb_pro_sizeof_rec[256] = {
 	[PCAN_USBPRO_SETFILTR] = sizeof(struct pcan_usb_pro_filter),
 	[PCAN_USBPRO_SETTS] = sizeof(struct pcan_usb_pro_setts),
 	[PCAN_USBPRO_GETDEVID] = sizeof(struct pcan_usb_pro_devid),
+	[PCAN_USBPRO_SETDEVID] = sizeof(struct pcan_usb_pro_devid),
 	[PCAN_USBPRO_SETLED] = sizeof(struct pcan_usb_pro_setled),
 	[PCAN_USBPRO_RXMSG8] = sizeof(struct pcan_usb_pro_rxmsg),
 	[PCAN_USBPRO_RXMSG4] = sizeof(struct pcan_usb_pro_rxmsg) - 4,
@@ -149,6 +150,7 @@ static int pcan_msg_add_rec(struct pcan_usb_pro_msg *pm, int id, ...)
 
 	case PCAN_USBPRO_SETBTR:
 	case PCAN_USBPRO_GETDEVID:
+	case PCAN_USBPRO_SETDEVID:
 		*pc++ = va_arg(ap, int);
 		pc += 2;
 		*(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
@@ -444,6 +446,18 @@ static int pcan_usb_pro_get_user_devid(struct peak_usb_device *dev,
 	return err;
 }
 
+static int pcan_usb_pro_set_user_devid(struct peak_usb_device *dev,
+				       u32 device_id)
+{
+	struct pcan_usb_pro_msg um;
+
+	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
+	pcan_msg_add_rec(&um, PCAN_USBPRO_SETDEVID, dev->ctrl_idx,
+			 device_id);
+
+	return pcan_usb_pro_send_cmd(dev, &um);
+}
+
 static int pcan_usb_pro_set_bittiming(struct peak_usb_device *dev,
 				      struct can_bittiming *bt)
 {
@@ -1076,6 +1090,7 @@ const struct peak_usb_adapter pcan_usb_pro = {
 	.dev_set_bus = pcan_usb_pro_set_bus,
 	.dev_set_bittiming = pcan_usb_pro_set_bittiming,
 	.dev_get_user_devid = pcan_usb_pro_get_user_devid,
+	.dev_set_user_devid = pcan_usb_pro_set_user_devid,
 	.dev_decode_buf = pcan_usb_pro_decode_buf,
 	.dev_encode_msg = pcan_usb_pro_encode_msg,
 	.dev_start = pcan_usb_pro_start,
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_pro.h b/drivers/net/can/usb/peak_usb/pcan_usb_pro.h
index a34e0fc021c9..28e740af905d 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_pro.h
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_pro.h
@@ -62,6 +62,7 @@ struct __packed pcan_usb_pro_fwinfo {
 #define PCAN_USBPRO_SETBTR	0x02
 #define PCAN_USBPRO_SETBUSACT	0x04
 #define PCAN_USBPRO_SETSILENT	0x05
+#define PCAN_USBPRO_SETDEVID	0x06
 #define PCAN_USBPRO_SETFILTR	0x0a
 #define PCAN_USBPRO_SETTS	0x10
 #define PCAN_USBPRO_GETDEVID	0x12
-- 
2.25.1


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

* [PATCH 5/6] can: peak_usb: replace unregister_netdev() with unregister_candev()
  2021-11-17 15:01 [PATCH 0/6] can: peak_usb: add sysfs interface to flashed value Stephane Grosjean
                   ` (3 preceding siblings ...)
  2021-11-17 15:01 ` [PATCH 4/6] can: peak_usb: allow flashing of user defined value Stephane Grosjean
@ 2021-11-17 15:01 ` Stephane Grosjean
  2021-11-17 15:01 ` [PATCH 6/6] can: peak_usb: add sysfs interface to internal device user value Stephane Grosjean
  5 siblings, 0 replies; 8+ messages in thread
From: Stephane Grosjean @ 2021-11-17 15:01 UTC (permalink / raw)
  To: linux-can Mailing List; +Cc: Stephane Grosjean

Signed-off-by: Stephane Grosjean <s.grosjean@peak-system.com>
---
 drivers/net/can/usb/peak_usb/pcan_usb_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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 a83ac4672123..ad9f435fc57e 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
@@ -925,7 +925,7 @@ static void peak_usb_disconnect(struct usb_interface *intf)
 		dev->state &= ~PCAN_USB_STATE_CONNECTED;
 		strlcpy(name, netdev->name, IFNAMSIZ);
 
-		unregister_netdev(netdev);
+		unregister_candev(netdev);
 
 		kfree(dev->cmd_buf);
 		dev->next_siblings = NULL;
-- 
2.25.1


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

* [PATCH 6/6] can: peak_usb: add sysfs interface to internal device user value
  2021-11-17 15:01 [PATCH 0/6] can: peak_usb: add sysfs interface to flashed value Stephane Grosjean
                   ` (4 preceding siblings ...)
  2021-11-17 15:01 ` [PATCH 5/6] can: peak_usb: replace unregister_netdev() with unregister_candev() Stephane Grosjean
@ 2021-11-17 15:01 ` Stephane Grosjean
  2021-11-17 15:16   ` Marc Kleine-Budde
  5 siblings, 1 reply; 8+ messages in thread
From: Stephane Grosjean @ 2021-11-17 15:01 UTC (permalink / raw)
  To: linux-can Mailing List; +Cc: Stephane Grosjean

This patch adds under /sys/class/net/canX a new group named "peak_usb"
which contains a "dev_num" entry accessible for reading (display in
decimal of the numerical value stored in non-volatile memory of the USB
device) and also for writing (modification by the user of this value).

Signed-off-by: Stephane Grosjean <s.grosjean@peak-system.com>
---
 drivers/net/can/usb/peak_usb/pcan_usb_core.c | 76 ++++++++++++++++++++
 1 file changed, 76 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 ad9f435fc57e..08d1db988d81 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
@@ -53,6 +53,17 @@ static const struct usb_device_id peak_usb_table[] = {
 
 MODULE_DEVICE_TABLE(usb, peak_usb_table);
 
+/* sysfs shortcuts */
+#define PEAK_USB_DEVICE_ATTR_RW(_name) \
+	struct device_attribute peak_usb_attr_##_name = \
+		__ATTR(_name, 0644, \
+		       peak_usb_##_name##_show, peak_usb_##_name##_store)
+
+#define PEAK_USB_DEVICE_ATTR_RO(_name) \
+	struct device_attribute peak_usb_attr_##_name = \
+		__ATTR(_name, 0444, \
+		       peak_usb_##_name##_show, NULL)
+
 /*
  * dump memory
  */
@@ -784,6 +795,61 @@ static const struct net_device_ops peak_usb_netdev_ops = {
 	.ndo_change_mtu = can_change_mtu,
 };
 
+static inline struct peak_usb_device *to_peak_usb_device(struct device *dev)
+{
+	return netdev_priv(to_net_dev(dev));
+}
+
+static ssize_t peak_usb_dev_num_show(struct device *dev,
+				     struct device_attribute *attr, char *buf)
+{
+	struct peak_usb_device *pdev = to_peak_usb_device(dev);
+
+	if (!pdev->adapter->dev_get_user_devid)
+		return -EOPNOTSUPP;
+
+	pdev->adapter->dev_get_user_devid(pdev, &pdev->device_number);
+
+	return snprintf(buf, PAGE_SIZE, "%u\n", pdev->device_number);
+}
+
+static ssize_t peak_usb_dev_num_store(struct device *dev,
+				      struct device_attribute *attr,
+				      const char *buf, size_t count)
+{
+	struct peak_usb_device *pdev = to_peak_usb_device(dev);
+	unsigned long devid;
+	int err;
+
+	if (!pdev->adapter->dev_set_user_devid)
+		return -EOPNOTSUPP;
+
+	err = kstrtoul(buf, 0, &devid);
+	if (err)
+		return err;
+
+	err = pdev->adapter->dev_set_user_devid(pdev, devid);
+	if (err)
+		return err;
+
+	/* on success, update cached value */
+	pdev->device_number = devid;
+
+	return count;
+}
+
+static PEAK_USB_DEVICE_ATTR_RW(dev_num);
+
+static const struct attribute *const peak_usb_net_attrs[] = {
+	&peak_usb_attr_dev_num.attr,
+	NULL,
+};
+
+static const struct attribute_group peak_usb_net_attrs_group = {
+	.name = PCAN_USB_DRIVER_NAME,
+	.attrs = (struct attribute **)peak_usb_net_attrs,
+};
+
 /*
  * create one device which is attached to CAN controller #ctrl_idx of the
  * usb adapter.
@@ -886,6 +952,12 @@ static int peak_usb_create_dev(const struct peak_usb_adapter *peak_usb_adapter,
 	if (dev->adapter->dev_get_user_devid)
 		dev->adapter->dev_get_user_devid(dev, &dev->device_number);
 
+	/* add our own attrs to /sys/class/net/canX node */
+	err = sysfs_create_group(&netdev->dev.kobj, &peak_usb_net_attrs_group);
+	if (err)
+		netdev_warn(netdev,
+			    "can't add sysfs features (err %d)\n", err);
+
 	netdev_info(netdev, "attached to %s channel %u (device %u)\n",
 			peak_usb_adapter->name, ctrl_idx, dev->device_number);
 
@@ -925,6 +997,9 @@ static void peak_usb_disconnect(struct usb_interface *intf)
 		dev->state &= ~PCAN_USB_STATE_CONNECTED;
 		strlcpy(name, netdev->name, IFNAMSIZ);
 
+		sysfs_remove_group(&netdev->dev.kobj,
+				   &peak_usb_net_attrs_group);
+
 		unregister_candev(netdev);
 
 		kfree(dev->cmd_buf);
@@ -958,6 +1033,7 @@ static int peak_usb_probe(struct usb_interface *intf,
 			return err;
 	}
 
+	/* create one CAN device for each CAN controller */
 	for (i = 0; i < peak_usb_adapter->ctrl_count; i++) {
 		err = peak_usb_create_dev(peak_usb_adapter, intf, i);
 		if (err) {
-- 
2.25.1


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

* Re: [PATCH 6/6] can: peak_usb: add sysfs interface to internal device user value
  2021-11-17 15:01 ` [PATCH 6/6] can: peak_usb: add sysfs interface to internal device user value Stephane Grosjean
@ 2021-11-17 15:16   ` Marc Kleine-Budde
  0 siblings, 0 replies; 8+ messages in thread
From: Marc Kleine-Budde @ 2021-11-17 15:16 UTC (permalink / raw)
  To: Stephane Grosjean; +Cc: linux-can Mailing List

[-- Attachment #1: Type: text/plain, Size: 955 bytes --]

On 17.11.2021 16:01:32, Stephane Grosjean wrote:
> This patch adds under /sys/class/net/canX a new group named "peak_usb"
> which contains a "dev_num" entry accessible for reading (display in
> decimal of the numerical value stored in non-volatile memory of the USB
> device) and also for writing (modification by the user of this value).
> 
> Signed-off-by: Stephane Grosjean <s.grosjean@peak-system.com>

Please have a look at the ethtool eeprom interface:

| ethtool -e|--eeprom-dump devname [raw on|off] [offset N] [length N]
| ethtool -E|--change-eeprom devname [magic N] [offset N] [length N] [value N]

That looks better than adding a custom sysfs entry.

Marc

-- 
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

end of thread, other threads:[~2021-11-17 15:16 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-17 15:01 [PATCH 0/6] can: peak_usb: add sysfs interface to flashed value Stephane Grosjean
2021-11-17 15:01 ` [PATCH 1/6] can: peak_usb: rename a callback to a more explicit name Stephane Grosjean
2021-11-17 15:01 ` [PATCH 2/6] can: peak_usb: add callback to read user value of CANFD devices Stephane Grosjean
2021-11-17 15:01 ` [PATCH 3/6] can: peak_usb: correction of a wrong field name Stephane Grosjean
2021-11-17 15:01 ` [PATCH 4/6] can: peak_usb: allow flashing of user defined value Stephane Grosjean
2021-11-17 15:01 ` [PATCH 5/6] can: peak_usb: replace unregister_netdev() with unregister_candev() Stephane Grosjean
2021-11-17 15:01 ` [PATCH 6/6] can: peak_usb: add sysfs interface to internal device user value Stephane Grosjean
2021-11-17 15:16   ` 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).