Click to See Complete Forum and Search --> : USB Devices


deathadder
12-04-2007, 10:19 AM
Hi guys,

I'm looking for some information, I'm trying to help out a friend with some uni work, about how USB devices are used within Linux specifically what happens when a device is plugged into a USB port. What process monitors for the change in hardware, how the required modules are determined etc. I've sent him in the direction of www.kernelnewbies.org but figured I'd ask here for him. :)

Thanks for any help!

saikee
12-04-2007, 11:50 AM
I think the modern kernels keep detecting the USB ports for devices.

The behaviour is nearly the same as a MS Windows that as soon as a device is inserted the kernel would mount it if it has a viable filing system. This seems to be the norm in the Ubuntu family now.

Generally by looking at the output of
fdisk -lbefore and after the USB device has been inserted one can see the detection of the device as a new entry would be added whenever a device is plugged even when the device has no filing system and therefore cannot be mounted. This is the same in a MS Windows when the device would appear in the "disk management" program but cannot be mounted if it has not been formatted.

As far as my experience goes a USB device works exactly like the block device of an internal hard disk in terms of formatting, partitioning and mounting.

The only major difference from an internal disk is in the booting. Many USB devices will not boot unless the USB lagacy support is enabled in the Bios.

Also many PC systems, like MS Windows, Solaris and most BSD systems, would not like to boot from a USB devices. Many Linux are readily made to be bootable from USB devices but the pattern seems to be distro specific. For example Debian family distros like Ubuntu, knoppix, Slackware family like Slax and Red Hat Family like Fedora have not hesitation to fire up from a USB device whereas an OpenSuse will need a new ram disk file compiled with the USB support before it does the same thing.

ph34r
12-04-2007, 12:25 PM
Take a look at udev and the various "rule sets" for it... there is also the hotplug daemon, and an associated blacklist, etc.

klackenfus
12-04-2007, 09:29 PM
You could also look at dmesg to see what actually happens when a new device is attached.

bwkaz
12-05-2007, 04:12 PM
how USB devices are used within Linux specifically what happens when a device is plugged into a USB port. At what level?

At the lowest electrical level, the device does some magic (that I don't fully understand, but whatever) with a couple of the lines on the USB bus, so the hub that's servicing that port picks it up. Some negotiation goes on between the hub and the device (negotiating power requirements, mostly), and eventually the hub hardware notifies the hub driver (in the OS) that there's a new device.

The hub driver then passes it up to the rest of the OS kernel, which does its own negotiation with the device, eventually loading drivers based on either the device's class, or its interfaces, or (less often in the USB world) its vendor/device IDs.

What process monitors for the change in hardware, None. The change is discovered by the hub's hardware, and the hub driver (in the kernel: no user-level process is involved here, and the only kernel "process" is khubd) eventually finds out that a new device is present. Userspace is notified (in several stages) by various uevents happening after the device is up and its capabilities have been parsed by the kernel's USB infrastructure.

These uevents are sent to udevd, which either loads a driver or creates a device node, or both, depending on properties of the uevent.

(I should note that hotplug has been deprecated for several years now, and I doubt it works properly in recent kernels.)

how the required modules are determined etc. That's done by the USB device's modalias (part of one of the uevents). In USB devices, the modalias contains the USB vendor and product IDs, and also the class/interface of the device. (There are several types of modaliases, but most of them work the same way as far as udev is concerned.)

Basically, whenever udevd sees a uevent that has a modalias, it tries to load the module that has that alias. (Which is where the name comes from.) Modules expose a list of arbitrary string aliases, which can be listed with modinfo; most these aliases are usually there to match all the devices that the module can drive.

(You can see all this udev-related stuff as it happens by running "udevadm monitor --env" and plugging in a USB device. I'd recommend redirecting it to a file, because there will be a lot of output.)

deathadder
12-05-2007, 07:59 PM
Thanks for the replies everyone!

The level would be from where the USB hub passes up to kernel, I'll pass the info along. You would know of any whitepapers/books/etc for futher reading would you bwkaz?

bwkaz
12-07-2007, 11:16 AM
There is a USB spec that might be helpful, but I don't know if it's freely-available or if you have to pay for access to it. (It might also be at a way-too-low level, depending on what this guy has for existing knowledge. Still, it might be worth a try.)

But that won't cover the udev/uevent stuff -- I'm not sure if there are any guides to all that that are still accurate (the implementation has been changing quite a bit over the last 6 years, since the hotplug package stopped getting released).

What I did was hang around on the linux-hotplug list for a while, and managed to hear most of this in the process. Not very school-friendly, but it worked...