USB Hotplug Parameters and A Simple Linux USB Hotplug Script

USB LogoUSB 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.

/sbin/hotplug Parameters
Linux kernels invoke the hotplug helper as/sbin/hotplug usb with extra environment variables as defined in the table below.

Variable Example Notes
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.
Variable Example Notes
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 3/1/1
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.
If “usbfs” (originally called “usbdevfs”) is enabled, the following
variables are also available:
Variable Example Notes
DEVFS /proc/bus/usb
Where the USB “drivers” list
lives. (Don’t confuse this with
the “devfs” filesystem type.)
Removed in Linux 2.5 kernels.
DEVICE
/proc/bus
/usb/002/008
The path to the usbdevfs node
for this device.
Before the Linux 2.6 kernels, usbmodules was used to scan the
Linux Hotplugging http://web.archive.org/web/20070716191956/http://linux-ho…
2 of 5 09/03/2009 14:44
other interfaces. In the 2.6 kernels, every interface is reported,
so usbmodules doesn’t help hotplugging.

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”]
then
# 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
fi

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.

Share this:

Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress

ROCK 5 ITX RK3588 mini-ITX motherboard
Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.
0 Comments
oldest
newest
Boardcon Rockchip and Allwinner SoM and SBC products