Skip to contentSkip to conversation

Populating netdev::dev_id for udev discrimination

PATCH Christopher R. Baker 4 participants 13 messages Mar 8, 2014 – Mar 11, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi All,

I'm new to the list, and have poked around on gmane enough to convince myself that this hasn't been covered before, but if I've missed something in this vein, apologies in advance.

My objective is to be able to totally discriminate CAN ports on multi-port cards via udev so as to rename them to semantically interesting/unique names for my system (e.g., "ecuCAN" and "auxCAN" instead of "can0" and "can1").

udevadm info gives me the KERNELS=... incantation to match the pci bus address, but there are no other differences in the udev listing for my various CAN ports (at least for my peak_pci device)

Digging into the semantics of ATTRS{...}, "dev_id" stood out as having the intention of discriminating between physical ports that "share the same link layer" (from netdev.h) or "share the same MAC address" (from various other mailing lists).

The following patch assigns the dev_id field to match the channel number on all multi-channel cards I could identify in a fresh git pull under drivers/net/can. I can only test my two-port Peak PCI card, but it works as expected: ATTRS{dev_id} now expresses the port number and my udev rules now unambiguously pick out and rename my individual CAN ports.

The drivers for other cards I have touched at least compile, but should certainly be tested before deploying this patch.

-Chris

drivers/net/can/sja1000/ems_pci.c +1 -0
index d790b87..fd13dbf 100644
@@ -323,6 +323,7 @@ static int ems_pci_add_card(struct pci_dev *pdev,
 			priv->cdr = EMS_PCI_CDR;
 
 			SET_NETDEV_DEV(dev, &pdev->dev);
+			dev->dev_id = i;
 
 			if (card->version == 1)
 				/* reset int flag of pita */
drivers/net/can/sja1000/ems_pcmcia.c +1 -0
index 9e535f2..381de99 100644
@@ -211,6 +211,7 @@ static int ems_pcmcia_add_card(struct pcmcia_device *pdev, unsigned long base)
 		priv = netdev_priv(dev);
 		priv->priv = card;
 		SET_NETDEV_DEV(dev, &pdev->dev);
+		dev->dev_id = i;
 
 		priv->irq_flags = IRQF_SHARED;
 		dev->irq = pdev->irq;
drivers/net/can/sja1000/kvaser_pci.c +1 -0
index c96eb14..23b8e13 100644
@@ -270,6 +270,7 @@ static int kvaser_pci_add_chan(struct pci_dev *pdev, int channel,
 		 priv->reg_base, board->conf_addr, dev->irq);
 
 	SET_NETDEV_DEV(dev, &pdev->dev);
+	dev->dev_id = channel;
 
 	/* Register SJA1000 device */
 	err = register_sja1000dev(dev);
drivers/net/can/sja1000/peak_pci.c +1 -0
index 065ca49..c540e3d 100644
@@ -642,6 +642,7 @@ static int peak_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 		icr |= chan->icr_mask;
 
 		SET_NETDEV_DEV(dev, &pdev->dev);
+		dev->dev_id = i;
 
 		/* Create chain of SJA1000 devices */
 		chan->prev_dev = pci_get_drvdata(pdev);
drivers/net/can/sja1000/peak_pcmcia.c +1 -0
index f7ad754..dd56133 100644
@@ -550,6 +550,7 @@ static int pcan_add_channels(struct pcan_pccard *card)
 		priv = netdev_priv(netdev);
 		priv->priv = card;
 		SET_NETDEV_DEV(netdev, &pdev->dev);
+		netdev->dev_id = i;
 
 		priv->irq_flags = IRQF_SHARED;
 		netdev->irq = pdev->irq;
drivers/net/can/sja1000/plx_pci.c +1 -0
index fbb61a0..ec39b7c 100644
@@ -587,6 +587,7 @@ static int plx_pci_add_card(struct pci_dev *pdev,
 			priv->cdr = ci->cdr;
 
 			SET_NETDEV_DEV(dev, &pdev->dev);
+			dev->dev_id = i;
 
 			/* Register SJA1000 device */
 			err = register_sja1000dev(dev);
drivers/net/can/softing/softing_main.c +1 -0
index 9ea0dcd..c79dad1 100644
@@ -832,6 +832,7 @@ static int softing_pdev_probe(struct platform_device *pdev)
 			ret = -ENOMEM;
 			goto netdev_failed;
 		}
+		netdev->dev_id = j;
 		priv = netdev_priv(card->net[j]);
 		priv->index = j;
 		ret = softing_netdev_register(netdev);
drivers/net/can/usb/esd_usb2.c +1 -0
index 7fbe859..65ea1058 100644
@@ -1024,6 +1024,7 @@ static int esd_usb2_probe_one_net(struct usb_interface *intf, int index)
 	netdev->netdev_ops = &esd_usb2_netdev_ops;
 
 	SET_NETDEV_DEV(netdev, &intf->dev);
+	netdev->dev_id = index;
 
 	err = register_candev(netdev);
 	if (err) {
drivers/net/can/usb/kvaser_usb.c +1 -0
index e77d110..880c0257 100644
@@ -1529,6 +1529,7 @@ static int kvaser_usb_init_one(struct usb_interface *intf,
 	netdev->netdev_ops = &kvaser_usb_netdev_ops;
 
 	SET_NETDEV_DEV(netdev, &intf->dev);
+	netdev->dev_id = channel;
 
 	dev->nets[channel] = priv;
 
drivers/net/can/usb/peak_usb/pcan_usb_core.c +1 -0
index 0b7a4c3..89f560b 100644
@@ -769,6 +769,7 @@ static int peak_usb_create_dev(struct peak_usb_adapter *peak_usb_adapter,
 	usb_set_intfdata(intf, dev);
 
 	SET_NETDEV_DEV(netdev, &intf->dev);
+	netdev->dev_id = ctrl_idx;
 
 	err = register_candev(netdev);
 	if (err) {
#1 lore raw
Quoted 3 lines · Christopher R. Baker

I'm new to the list, and have poked around on gmane enough to convince myself that this hasn't been covered before, but if I've missed something in this vein, apologies in advance.

You are the first one to address this issue. \o/

Quoted 21 lines

My objective is to be able to totally discriminate CAN ports on multi-port cards via udev so as to rename them to semantically interesting/unique names for my system (e.g., "ecuCAN" and "auxCAN" instead of "can0" and "can1").

udevadm info gives me the KERNELS=... incantation to match the pci bus address, but there are no other differences in the udev listing for my various CAN ports (at least for my peak_pci device)

Digging into the semantics of ATTRS{...}, "dev_id" stood out as having the intention of discriminating between physical ports that "share the same link layer" (from netdev.h) or "share the same MAC address" (from various other mailing lists).

The following patch assigns the dev_id field to match the channel number on all multi-channel cards I could identify in a fresh git pull under drivers/net/can. I can only test my two-port Peak PCI card, but it works as expected: ATTRS{dev_id} now expresses the port number and my udev rules now unambiguously pick out and rename my individual CAN ports.

Quoted 2 lines

The drivers for other cards I have touched at least compile, but should certainly be tested before deploying this patch.

Thanks for the patch, looks good. Can I add your Signed-off-by[1]?

Marc

[1]
http://lxr.free-electrons.com/source/Documentation/SubmittingPatches#L307

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   |
#2 lore raw

Absolutely, and thanks for the link (without undue noob-roasting ;) )

-Chris

Quoted 37 lines · Marc Kleine-Budde
Quoted 3 lines · Christopher R. Baker

I'm new to the list, and have poked around on gmane enough to convince myself that this hasn't been covered before, but if I've missed something in this vein, apologies in advance.

You are the first one to address this issue. \o/

Quoted 21 lines

My objective is to be able to totally discriminate CAN ports on multi-port cards via udev so as to rename them to semantically interesting/unique names for my system (e.g., "ecuCAN" and "auxCAN" instead of "can0" and "can1").

udevadm info gives me the KERNELS=... incantation to match the pci bus address, but there are no other differences in the udev listing for my various CAN ports (at least for my peak_pci device)

Digging into the semantics of ATTRS{...}, "dev_id" stood out as having the intention of discriminating between physical ports that "share the same link layer" (from netdev.h) or "share the same MAC address" (from various other mailing lists).

The following patch assigns the dev_id field to match the channel number on all multi-channel cards I could identify in a fresh git pull under drivers/net/can. I can only test my two-port Peak PCI card, but it works as expected: ATTRS{dev_id} now expresses the port number and my udev rules now unambiguously pick out and rename my individual CAN ports.

Quoted 2 lines

The drivers for other cards I have touched at least compile, but should certainly be tested before deploying this patch.

Thanks for the patch, looks good. Can I add your Signed-off-by[1]?

Marc

[1]
http://lxr.free-electrons.com/source/Documentation/SubmittingPatches#L307

#3 lore raw
In reply to Marc Kleine-Budde

Hello Christopher,

Quoted 3 lines · Marc Kleine-Budde

On 03/08/2014 05:00 PM, Christopher R. Baker wrote:

You are the first one to address this issue. \o/

I remember we discussed this issue years ago - but it was not really brought to an end. So I really appreciate Christophers attempt :-)

Quoted 6 lines
Quoted 6 lines

udevadm info gives me the KERNELS=... incantation to match the pci bus address, but there are no other differences in the udev listing for my various CAN ports (at least for my peak_pci device)

Digging into the semantics of ATTRS{...}, "dev_id" stood out as having the intention of discriminating between physical ports

I assume dev->if_port has to be assigned to different CAN transceiver types for a specific CAN controller then ...

We should keep that in mind if someone comes up with that question.

Quoted 6 lines
Quoted 6 lines

that "share the
same link layer" (from netdev.h) or "share the same MAC address" (from various other mailing lists).

The following patch assigns the dev_id field to match the channel number on all multi-channel cards

Ok. That looks reasonable (and therefore the patch fits excellent).

As you stated above you can identify the card via the PCI bus address too.

Do you have an idea how to proceed with USB or other devices that bring their own identification (like a serial number / device id) ??

E.g. the Softing Card provides some serial number through sysfs:

http://lxr.free-electrons.com/source/drivers/net/can/softing/softing_main.c#L719

and e.g. the PEAK USB have a so called "device number" that is currently just printed in kernel log:

http://lxr.free-electrons.com/source/drivers/net/can/usb/peak_usb/pcan_usb_core.c#L799

Especially to assign USB devices that can be attached in a wild manner each time a good idea would be needed for udev to identify the adapter.

Do you also have an idea for that? E.g. use the HW MAC address field (we do not need for CAN) for this kind of "serial number / device id" ??

Regards,
Oliver

#4 lore raw

Hey Christopher & Oliver,

Quoted 16 lines · Oliver Hartkopp

Hello Christopher,

Quoted 3 lines · Marc Kleine-Budde

On 03/08/2014 05:00 PM, Christopher R. Baker wrote:

You are the first one to address this issue. \o/

I remember we discussed this issue years ago - but it was not really brought to an end. So I really appreciate Christophers attempt :-)

Quoted 6 lines
Quoted 6 lines

udevadm info gives me the KERNELS=... incantation to match the pci bus address, but there are no other differences in the udev listing for my various CAN ports (at least for my peak_pci device)

Digging into the semantics of ATTRS{...}, "dev_id" stood out as having the intention of discriminating between physical ports

I find this attempt attractive, because it moves multichannel device identification to generic netdev infrastructure.

The patch obsoletes the 'channel' sysfs property of the softing device driver. That property did exactly what can now be done with 'dev_id'.
I'll prepare a patch when this gets accepted.
That patch would not only remove 1 property.
The 'channel' property required the attr_group to be present at device creation. Whithout 'channel', I could use the regular sysfs_create_group which makes the code even simpler.

[...]

Quoted 4 lines

Ok. That looks reasonable (and therefore the patch fits excellent).

As you stated above you can identify the card via the PCI bus address too.

I contest this. If 2 CAN ports appear in 1 single PCI device probe, then they share all PCI attributes, via their 'device' link.

Quoted 7 lines

Do you have an idea how to proceed with USB or other devices that bring their own identification (like a serial number / device id) ??

E.g. the Softing Card provides some serial number through sysfs:

http://lxr.free-electrons.com/source/drivers/net/can/softing/softing_main.c#L719

I still think this is the correct way.
udev identifies the device based on anyting device related (serial number, driver, ...) and identifies the channel of the device based on the netdev (now through dev_id). The netdev links to the device, so there is be no problem with identification.

Quoted 11 lines

and e.g. the PEAK USB have a so called "device number" that is currently just printed in kernel log:

http://lxr.free-electrons.com/source/drivers/net/can/usb/peak_usb/pcan_usb_core.c#L799

Especially to assign USB devices that can be attached in a wild manner each time a good idea would be needed for udev to identify the adapter.

Do you also have an idea for that? E.g. use the HW MAC address field (we do not need for CAN) for this kind of "serial number / device id" ??

Putting somehing else into the MAC address field is not right. What problem do you try to solve here?

Kind regards,
Kurt Van Dijck

#5 lore raw
Quoted 23 lines · Kurt Van Dijck

Hey Christopher & Oliver,

Quoted 16 lines · Oliver Hartkopp

Hello Christopher,

Quoted 3 lines · Marc Kleine-Budde

On 03/08/2014 05:00 PM, Christopher R. Baker wrote:

You are the first one to address this issue. \o/

I remember we discussed this issue years ago - but it was not really brought to an end. So I really appreciate Christophers attempt :-)

Quoted 6 lines
Quoted 6 lines

udevadm info gives me the KERNELS=... incantation to match the pci bus address, but there are no other differences in the udev listing for my various CAN ports (at least for my peak_pci device)

Digging into the semantics of ATTRS{...}, "dev_id" stood out as having the intention of discriminating between physical ports

I find this attempt attractive, because it moves multichannel device identification to generic netdev infrastructure.

Quoted 7 lines

The patch obsoletes the 'channel' sysfs property of the softing device driver. That property did exactly what can now be done with 'dev_id'.
I'll prepare a patch when this gets accepted.
That patch would not only remove 1 property.
The 'channel' property required the attr_group to be present at device creation. Whithout 'channel', I could use the regular sysfs_create_group which makes the code even simpler.

You can create a patch against the testing branch of linux-can-next[1]

Marc

[1]
https://gitorious.org/linux-can/linux-can-next/source/e7bf719dbbad19857040ef50ed8b2cfefd81d78c:

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   |
#6 lore raw
Quoted 3 lines · Marc Kleine-Budde

You can create a patch against the testing branch of linux-can-next[1]

FYI:

Tested with PEAK PCAN USB pro and EMS PCMCIA (both with two channels) and the dev_id works like expected (first channel dev_id = 0x0, second channel 0x1).

Tnx
Oliver

#7 lore raw
Quoted 6 lines · Oliver Hartkopp
Quoted 1 line

You can create a patch against the testing branch of linux-can-next[1]

FYI:

Tested with PEAK PCAN USB pro and EMS PCMCIA (both with two channels) and the dev_id works like expected (first channel dev_id = 0x0, second channel 0x1).

Can I add your Tested-by for the Peak and the EMS?

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   |
#8 lore raw
Quoted 10 lines · Marc Kleine-Budde
Quoted 6 lines · Oliver Hartkopp
Quoted 1 line

You can create a patch against the testing branch of linux-can-next[1]

FYI:

Tested with PEAK PCAN USB pro and EMS PCMCIA (both with two channels) and the dev_id works like expected (first channel dev_id = 0x0, second channel 0x1).

Can I add your Tested-by for the Peak and the EMS?

Oh, yes.

At least for these two adapters I checked it myself.

Thanks,
Oliver

maintainer Tested-by Oliver Hartkopp
#9 lore raw
Quoted 11 lines · Oliver Hartkopp
Quoted 7 lines
Quoted 4 lines

FYI:

Tested with PEAK PCAN USB pro and EMS PCMCIA (both with two channels) and the dev_id works like expected (first channel dev_id = 0x0, second channel 0x1).

Can I add your Tested-by for the Peak and the EMS?

Oh, yes.

At least for these two adapters I checked it myself.

I've added:

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   |
#11 lore raw
In reply to Kurt Van Dijck

Hey Kurt!

Quoted 8 lines · Kurt Van Dijck
Quoted 6 lines

Do you have an idea how to proceed with USB or other devices that bring their own identification (like a serial number / device id) ??

E.g. the Softing Card provides some serial number through sysfs:

http://lxr.free-electrons.com/source/drivers/net/can/softing/softing_main.c#L719

I still think this is the correct way.

Yes. You are right.

Quoted 3 lines

udev identifies the device based on anyting device related (serial number, driver, ...) and identifies the channel of the device based on the netdev (now through dev_id). The netdev links to the device, so there is be no problem with identification.

Looking into your code IMO a similar thing has to be done for the other CAN interfaces providing HW/SW versions and unique IDs like e.g. a serial number.

E.g. the PEAK USB adapters or the USB8DEV adapter, etc.

It already looks pretty good for a general usage.

Do you know what your attributes "frequency" and "tx_pending" have been used for in the softing driver? Are they still needed?

Quoted 1 line

Putting somehing else into the MAC address field is not right.

Ack. It was just a suggestion. But after looking into your sysfs code you convinced me :-)

Quoted 1 line

What problem do you try to solve here?

Just to have a unique ID to identify CAN USB adapters to set them to the right bitrate and netdevice name via udev when they are attached.

Best regards,
Oliver

#12 lore raw
Quoted 13 lines · Oliver Hartkopp

Hey Kurt!

Quoted 8 lines · Kurt Van Dijck
Quoted 6 lines

Do you have an idea how to proceed with USB or other devices that bring their own identification (like a serial number / device id) ??

E.g. the Softing Card provides some serial number through sysfs:

http://lxr.free-electrons.com/source/drivers/net/can/softing/softing_main.c#L719

I still think this is the correct way.

Quoted 8 lines

Yes. You are right.

Quoted 3 lines

udev identifies the device based on anyting device related (serial number, driver, ...) and identifies the channel of the device based on the netdev (now through dev_id). The netdev links to the device, so there is be no problem with identification.

Looking into your code IMO a similar thing has to be done for the other CAN interfaces providing HW/SW versions and unique IDs like e.g. a serial number.

Yes, I think too.
Now that (later this week) the netdev attrs have been integrated in a regular way, the sysfs code will be much more readable and easier to follow than today. I had to use special handlings for the 'channel', now dev_id.

Quoted 7 lines

E.g. the PEAK USB adapters or the USB8DEV adapter, etc.

It already looks pretty good for a general usage.

Do you know what your attributes "frequency" and "tx_pending" have been used for in the softing driver? Are they still needed?

tx_pending can go. I'll address that too later this week. Frequency is an indication of the embedded cpu's frequency. It's exported, but with no immediate user.

Kurt