Udev

From Organic Design wiki
Revision as of 10:11, 27 May 2005 by Rob (talk | contribs) (USB)

Thanks to HotPlug and /proc it has never been easier for software to determine the state of devices plugged into the system.

USB

USB devices live at /proc/bus/usb. When a device is plugged this file is updated. Here is an example of the file:

T:  Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#=  1 Spd=12  MxCh= 2
B:  Alloc=  0/900 us ( 0%), #Int=  0, #Iso=  0
D:  Ver= 1.10 Cls=09(hub  ) Sub=00 Prot=00 MxPS= 8 #Cfgs=  1
P:  Vendor=0000 ProdID=0000 Rev= 2.06
S:  Manufacturer=Linux 2.6.8-2-386 uhci_hcd
S:  Product=Intel Corp. 82801AA USB
S:  SerialNumber=0000:00:1f.2
C:* #Ifs= 1 Cfg#= 1 Atr=c0 MxPwr=  0mA
I:  If#= 0 Alt= 0 #EPs= 1 Cls=09(hub  ) Sub=00 Prot=00 Driver=hub
E:  Ad=81(I) Atr=03(Int.) MxPS=   2 Ivl=255ms

T:  Bus=01 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#=  6 Spd=12  MxCh= 0
D:  Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs=  1
P:  Vendor=0d7d ProdID=1300 Rev= 0.50
S:  Product=USB2.0 FlashDisk
S:  SerialNumber=1D4402000054
C:* #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=200mA
I:  If#= 0 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storage
E:  Ad=81(I) Atr=02(Bulk) MxPS=  64 Ivl=0ms
E:  Ad=02(O) Atr=02(Bulk) MxPS=  64 Ivl=0ms

This computer has only one external USB device plugged in (USB2.0 FlashDisk). The other entry is the host controler (Intel Corp. 82801AA USB).

Firewire and others

These all work in a similar way. It is a simple matter to parse the /proc files to extract whatever relevant data is needed to feed into the UI.

Events

Hotpug also includes a very useful feature that allows you to bind a program to a specific hotplug event. This means the OS can be notified when something happens (eg you plug in your USB memory stick).

In the directory /etc/hotplug.d is a buch of directories that represent all the kinds of devices that may be hotplugged.

drwxr-xr-x  2 root root 4096 2004-09-04 06:15 chandev
drwxr-xr-x  2 root root 4096 2005-05-11 04:39 default
drwxr-xr-x  2 root root 4096 2004-09-04 06:15 dock
drwxr-xr-x  2 root root 4096 2004-09-04 06:15 ieee1394
drwxr-xr-x  2 root root 4096 2004-09-04 06:15 input
drwxr-xr-x  2 root root 4096 2004-09-04 06:15 net
drwxr-xr-x  2 root root 4096 2004-09-04 06:15 pci
drwxr-xr-x  2 root root 4096 2004-09-04 06:15 scsi
drwxr-xr-x  2 root root 4096 2005-05-27 05:59 usb

Example: I want to run a script when the USB device is plugged in. So I put some kind of executable in the usb directory and suffix it with .hotplug.

Resources

Linux Hotplugging