3

I have one of these and I am trying to write a Bash script for finding the device path in /sys.

My approach goes something like this:

  • start in /sys/modules/usbled since this is the name of the kernel module loaded when the device is plugged in
  • cd to drivers/usb:usbled, which appears to be the bus and name of the driver for the device (according to usbled.c:229)
  • ???

I get stuck on the last step since the directory contains:

$ ls
1-1.2:1.0  bind  module  new_id  remove_id  uevent  unbind

Now, I happen to know in this case that the 1-1.2:1.0 directory contains the char devices necessary to control the LEDs. However, how does my script know this? Is there any particular naming convention behind the directory? What if there are multiple devices of the same type plugged in?

2

OK I think your question title is a bit unintentionally misleading, since in the text you're saying you've already found the device and are asking how to programmatically do so. One question that comes to mind is how you manually found it and why you couldn't just script around it. I'll answer the question as best I can in its current form, apologies in advance if I miss the mark.

Stuff like this is incredibly variable and device-specific. It's considerations like this that require drivers/software adapters even in higher level software. So you should probably get used to the idea that this script will do this one particular thing rather than abstracting out to some generic process (which is what I'm guessing you're trying to do).

As a bit of background: Each bus-like system (USB, SCSI, PCI, etc) needs some form of addressing devices. With lspci these are the values you see on the leftmost of each line in the default output.

Abbreviated example:

[root@hypervisor pyadmin]# lspci
00:00.0 Host bridge: Intel Corporation 5400 Chipset Memory Controller Hub (rev 20)
00:1d.1 USB controller: Intel Corporation 631xESB/632xESB/3100 Chipset UHCI USB Controller #2 (rev 09)
00:1d.2 USB controller: Intel Corporation 631xESB/632xESB/3100 Chipset UHCI USB Controller #3 (rev 09)
04:00.0 PCI bridge: Intel Corporation 6311ESB/6321ESB PCI Express Downstream Port E1 (rev 01)
04:01.0 PCI bridge: Intel Corporation 6311ESB/6321ESB PCI Express Downstream Port E2 (rev 01)
07:05.0 Fibre Channel: QLogic Corp. ISP2422-based 4Gb Fibre Channel to PCI-X HBA (rev 02)
07:06.0 Fibre Channel: QLogic Corp. ISP2422-based 4Gb Fibre Channel to PCI-X HBA (rev 02)
08:00.0 Ethernet controller: Broadcom Corporation NetXtreme BCM5754 Gigabit Ethernet PCI Express (rev 02)

The PCI addresses are the 08:00.0, 07:06.0, etc.

As you said, the sysfs directory you're looking at is for one of the modules (usbled) so you're looking at the information sysfs has on that module, which includes devices that use the module (or vice versa, if you like). The 1-1.2:1.0 you're looking at represents the device and is being referenced by it's USB Address (in USB terminology the "endpoint").

So if you already know the module, I would basically filter out the known values and only search for dentry's that contain both colons and periods, as there's very little chance of another dentry in that particular sysfs directory ever being created with a name like that if it's not a connected device.

I know that's a little all over the place, but I'm pretty sure your answer's up there somewhere.

  • 1
    Thanks! I also dug this up which seems to explain the bus addressing scheme for USB devices. (Fair warning: the page is slightly dated - it was last modified in 2007 and refers to 2.4.x kernels.) – Nathan Osman Oct 28 '13 at 4:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.