Skip to contentSkip to conversation

can: kvaser_usb: Simplify struct kvaser_msg_cardinfo

PATCH Applied Jimmy Assarsson 4 participants 5 messages Feb 16, 2018 – Feb 17, 2018 CAN network drivers

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 can: kvaser_usb: Simplify struct kvaser_msg_cardinfo

serial_number_high can be removed from the struct since it is never used in the USBcan II firmware.

---
Changes in v4:
- Move change of ean to second patch.

Changes in v3:
- Change data type of ean, from u32 to __le32.

Changes in v2:
- Made this a separate patch.

 drivers/net/can/usb/kvaser_usb.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
drivers/net/can/usb/kvaser_usb.c +3 -11
index 63587b8e6825..ca808a1bc892 100644
@@ -196,16 +196,8 @@ struct kvaser_msg_simple {
 struct kvaser_msg_cardinfo {
 	u8 tid;
 	u8 nchannels;
-	union {
-		struct {
-			__le32 serial_number;
-			__le32 padding;
-		} __packed leaf0;
-		struct {
-			__le32 serial_number_low;
-			__le32 serial_number_high;
-		} __packed usbcan0;
-	} __packed;
+	__le32 serial_number;
+	__le32 padding0;
 	__le32 clock_resolution;
 	__le32 mfgdate;
 	u8 ean[8];
@@ -218,7 +210,7 @@ struct kvaser_msg_cardinfo {
 			u8 padding;
 		} __packed usbcan1;
 	} __packed;
-	__le16 padding;
+	__le16 padding1;
 } __packed;
 
 struct kvaser_msg_cardinfo2 {
Signature · 1 line
2.16.1
#1 lore raw

PATCH v4 2/2 can: kvaser_usb: Expose EAN and serial number via sysfs

The EAN (article number) combined with serial number form a unique identifier for any Kvaser device. This makes it possible to map netdevice with physical Kvaser device.

Example output for USBcan Light 2xHS:

$ cat /sys/class/net/can0/ean
7330130007147
$ cat /sys/class/net/can0/serial
10659

Tested with the following Kvaser devices:
Leaf Light HS, Leaf Light HS v2, USBcan Light 2xHS, USBcan II, Memorator Professional HS/HS and Memorator HS/HS.

---
Changes in v4:
- Change type of struct kvaser_msg_cardinfo member ean.

Changes in v3:
- Change data type of ean, from u32 to __le32.

Changes in v2:
- Made this a separate patch.

 .../ABI/testing/sysfs-class-net_kvaser_usb         | 15 ++++++++
 drivers/net/can/usb/kvaser_usb.c                   | 40 +++++++++++++++++++++-
 2 files changed, 54 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/sysfs-class-net_kvaser_usb
Documentation/ABI/testing/sysfs-class-net_kvaser_usb new file +15 -0
new file mode 100644
index 000000000000..922a8cc41e60
@@ -0,0 +1,15 @@
+What:		/sys/class/net/<ifname>/ean
+Date:		February 2018
+Contact:	Jimmy Assarsson <[email protected]>
+KernelVersion:	4.16
+Description:
+		Read-only value representing the article number (EAN) for the
+		Kvaser CAN product.
+
+What:		/sys/class/net/<ifname>/serial
+Date:		February 2018
+Contact:	Jimmy Assarsson <[email protected]>
+KernelVersion:	4.16
+Description:
+		Read-only value representing the serial number for the Kvaser
+		CAN product.
drivers/net/can/usb/kvaser_usb.c +39 -1
index ca808a1bc892..d3d19a7ff16f 100644
@@ -200,7 +200,7 @@ struct kvaser_msg_cardinfo {
 	__le32 padding0;
 	__le32 clock_resolution;
 	__le32 mfgdate;
-	u8 ean[8];
+	__le32 ean[2];
 	u8 hw_revision;
 	union {
 		struct {
@@ -457,6 +457,8 @@ struct kvaser_usb {
 	 * also used as a sentinel for marking free tx contexts.
 	 */
 	u32 fw_version;
+	u32 serial;
+	u64 ean;
 	unsigned int nchannels;
 	unsigned int max_tx_urbs;
 	enum kvaser_usb_family family;
@@ -692,6 +694,10 @@ static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
 	     dev->nchannels > MAX_USBCAN_NET_DEVICES))
 		return -EINVAL;
 
+	dev->serial = le32_to_cpu(msg.u.cardinfo.serial_number);
+	dev->ean = (u64)(le32_to_cpu(msg.u.cardinfo.ean[1])) << 32 |
+		   le32_to_cpu(msg.u.cardinfo.ean[0]);
+
 	return 0;
 }
 
@@ -1802,6 +1808,37 @@ static const struct can_bittiming_const kvaser_usb_bittiming_const = {
 	.brp_inc = KVASER_USB_BRP_INC,
 };
 
+static ssize_t serial_show(struct device *dev,
+			   struct device_attribute *attr, char *buf)
+{
+	struct kvaser_usb_net_priv *priv = netdev_priv(to_net_dev(dev));
+	struct kvaser_usb *kvaser_dev = priv->dev;
+
+	return sprintf(buf, "%u\n", kvaser_dev->serial);
+}
+
+static ssize_t ean_show(struct device *dev,
+			struct device_attribute *attr, char *buf)
+{
+	struct kvaser_usb_net_priv *priv = netdev_priv(to_net_dev(dev));
+	struct kvaser_usb *kvaser_dev = priv->dev;
+
+	return sprintf(buf, "%llx\n", kvaser_dev->ean);
+}
+
+static DEVICE_ATTR_RO(serial);
+static DEVICE_ATTR_RO(ean);
+
+static struct attribute *kvaser_usb_dev_attrs[] = {
+	&dev_attr_serial.attr,
+	&dev_attr_ean.attr,
+	NULL,
+};
+
+static const struct attribute_group kvaser_usb_dev_group = {
+	.attrs = kvaser_usb_dev_attrs,
+};
+
 static int kvaser_usb_set_bittiming(struct net_device *netdev)
 {
 	struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
@@ -1931,6 +1968,7 @@ static int kvaser_usb_init_one(struct usb_interface *intf,
 	netdev->flags |= IFF_ECHO;
 
 	netdev->netdev_ops = &kvaser_usb_netdev_ops;
+	netdev->sysfs_groups[0] = &kvaser_usb_dev_group;
 
 	SET_NETDEV_DEV(netdev, &intf->dev);
 	netdev->dev_id = channel;
Signature · 1 line
2.16.1
#2 lore raw

Re: [PATCH v4 2/2] can: kvaser_usb: Expose EAN and serial number via sysfs

Hi Jimmy, Marc,

IMO this patch does not solve the general requirement for exposing serial numbers.

Additionally the 'ean' attribute is obsolete due to the unique USB vendor and product id.

E.g. attaching a USB Pro FD adapter in my Laptop creates this dmesg output:

[ 5210.858742] usb 3-6: new high-speed USB device number 6 using xhci_hcd
[ 5211.007608] usb 3-6: config 1 interface 0 altsetting 0 bulk endpoint 0x83 has invalid maxpacket 64
[ 5211.007627] usb 3-6: config 1 interface 1 altsetting 0 bulk endpoint 0x84 has invalid maxpacket 64
[ 5211.007633] usb 3-6: config 1 interface 1 altsetting 0 bulk endpoint 0x4 has invalid maxpacket 64
[ 5211.007638] usb 3-6: config 1 interface 1 altsetting 0 bulk endpoint 0x85 has invalid maxpacket 64
[ 5211.007643] usb 3-6: config 1 interface 1 altsetting 0 bulk endpoint 0x5 has invalid maxpacket 64
[ 5211.007648] usb 3-6: config 1 interface 1 altsetting 0 bulk endpoint 0x86 has invalid maxpacket 64
[ 5211.007653] usb 3-6: config 1 interface 1 altsetting 0 bulk endpoint 0x6 has invalid maxpacket 64
[ 5211.008447] usb 3-6: New USB device found, idVendor=0c72, idProduct=0011
[ 5211.008454] usb 3-6: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 5211.008459] usb 3-6: Product: PCAN-USB Pro FD
[ 5211.008463] usb 3-6: Manufacturer: PEAK-System Technik GmbH
[ 5211.036944] CAN device driver interface
[ 5211.048600] peak_usb 3-6:1.0: PEAK-System PCAN-USB Pro FD v1 fw v2.1.0 (2 channels)
[ 5211.048916] peak_usb 3-6:1.0 can0: attached to PCAN-USB Pro FD channel 0 (device 1334)
[ 5211.050056] peak_usb 3-6:1.0 can1: attached to PCAN-USB Pro FD channel 1 (device 4294967295)
[ 5211.050247] usbcore: registered new interface driver peak_usb
$ cat /sys/class/net/can0/device/modalias
usb:v0C72p0011d0000dc00dsc00dp00ic00isc00ip00in00
    ^^^^^^^^^^

Vendor ID 0C72
Product ID 0011

-> this is as unique as the EAN, right?

For PCI devices this is similar:

$ cat /sys/class/net/can2/device/modalias
pci:v0000001Cd00000002sv0000001Csd00000004bc02sc80i00
    ^^^^^^^^^^^^^^^^^^

001C -> PEAK System
0002 -> PEAK_PCIEC_DEVICE_ID 0x0002 /* for ExpressCard slot cards */

So no need for an EAN ?!!

The other thing may be hardware or firmware revisions and the unique device ID (aka serial number)

For these I would suggest attibutes like

hw_rev
fw_rev
dev_uid

to be provided by drivers/net/can/dev.c and filled with content from the driver when calling alloc_candev() - e.g. by passing a pointer to a struct which contains the content.

e.g.

struct can_sysfs_info {
	u16 hw_rev_major;
	u16 hw_rev_minor;
	u16 fw_rev_major;
	u16 fw_rev_minor;
	u32 uid;
}

The code with

Quoted 14 lines · Jimmy Assarsson
drivers/net/can/usb/kvaser_usb.c +14 -0
+
+static DEVICE_ATTR_RO(serial);
+static DEVICE_ATTR_RO(ean);
+
+static struct attribute *kvaser_usb_dev_attrs[] = {
+	&dev_attr_serial.attr,
+	&dev_attr_ean.attr,
+	NULL,
+};
+
+static const struct attribute_group kvaser_usb_dev_group = {
+	.attrs = kvaser_usb_dev_attrs,
+};
+

(..)

drivers/net/can/usb/kvaser_usb.c static int kvaser_usb_init_one(struct usb_interface *intf, · line 1974

Quoted 4 lines
drivers/net/can/usb/kvaser_usb.c +1 -0
+	netdev->sysfs_groups[0] = &kvaser_usb_dev_group;
 
  	SET_NETDEV_DEV(netdev, &intf->dev);
  	netdev->dev_id = channel;

looks great - but needs to be moved to ../can/dev.c IMHO.

Best regards,
Oliver

ps. I would like to move the Softing driver and the PEAK USB to that common approach too - once it is available. I have to look for an old Softing PCMCIA card for testing then ;-)

maintainer Applied
#3 lore raw
In reply to Jimmy Assarsson
Quoted 4 lines · Jimmy Assarsson

serial_number_high can be removed from the struct since it is never used in the USBcan II firmware.

Applied to can.

tnx,
Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |
#4 lore raw
Quoted 10 lines · Marc Kleine-Budde
Quoted 4 lines · Jimmy Assarsson

serial_number_high can be removed from the struct since it is never used in the USBcan II firmware.

Applied to can.

tnx,
Marc

I really appreciate you taking the time to help me :) Thanks! Jimmy