USB Hotplug allows your system to detect a USB device has been inserted or removed and perform certain actions upon the USB device detection.
The article below is largely inspired by http://linux-hotplug.sourceforge.net/?selected=hotplug which does not seem to work anymore.
USB hotplugging involves:
- MODULE_DEVICE_TABLE support in the USB Device Driver API, so that a driver’s probe() routine is called only when it’s very likely to want to bind to a particular interface. USB always uses this.
- Support for invoking the hotplug helper (/sbin/hotplug by default) when devices are added or removed. This functionality is enabled only by CONFIG_HOTPLUG. That hotplug helper usually contacts a policy agent which handles the USB-level configuration or activation tasks appropriate to this device and the current system configuration.
- Boot-time configuration (“cold-plugging”) can be managed by the /etc/rc.d/init.d/hotplug startup script, if the “usbmodules” command line utility is available. (There is also a “diet hotplug” tool for use during “initrd” processing, which is much less flexible but works without BASH to allow boot images to work over USB devices.)
In the kernel tree, /usr/src/linux/Documentation/usb/hotplug.txt has basic information about USB Device Driver API support for hotplugging.
Linux kernels invoke the hotplug helper as/sbin/hotplug usb with extra environment variables as defined in the table below.
|ACTION||add||a USB device has been added or removed;|
|DEVPATH||only in 2.6 and later kernels; /sys/$DEVPATH is the sysfs directory for this device or interface|
|PRODUCT||46d/c281/108||idVendor/idProduct/bcdDevice, from device descriptor. Numbers are hexadecimal, without leading ‘0x’ or zeros.|
|TYPE||9/0/0||bDeviceClass/bDeviceSubClass/bDeviceProtocol, from device descriptor … when 0/*/* is seen, a variable of type INTERFACE is also provided. Numbers are decimal.|
|INTERFACE||03/01/2001||bInterfaceClass/bInterfaceSubClass/bInterfaceProtocol, only for device class zero. Linux 2.6 gives each interface its own hotplug event, and /sys/$DEVPATH/bInterfaceNumber tells them apart. Earlier kernels only reported the first interface. Numbers are decimal.|
Here’s a simple USB hotplug script (/sbin/hotplug) to get the parameters described above and save it in a file in /tmp/hotplug.log (/tmp is part of the ramdisk in most embedded systems) . hotplug.log would be written to each time a USB device is connected or unplugged.
#Filter USB device only
if [ $1 == “usb”]
# Display USB hotplug parameters
echo $ACTION >> /tmp/hotplug.log
echo $DEVPATH >> /tmp/hotplug.log
echo “VID/PID/BCD: $PRODUCT” >> /tmp/hotplug.log
echo “Type: $TYPE” >> /tmp/hotplug.log
echo “Interface: $INTERFACE” >> /tmp/hotplug.log
So you can detect the device model by checking the product Vid (Vendor ID) and Pid (Product ID) and check the type (e.g. Mass Storage: Interface class 8 ) with $TYPE or $INTERFACE.
Jean-Luc started CNX Software in 2010 as a part-time endeavor, before quitting his job as a software engineering manager, and starting to write daily news, and reviews full time later in 2011.