162306a36Sopenharmony_ciUSB hotplugging
262306a36Sopenharmony_ci~~~~~~~~~~~~~~~
362306a36Sopenharmony_ci
462306a36Sopenharmony_ciLinux Hotplugging
562306a36Sopenharmony_ci=================
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci
862306a36Sopenharmony_ciIn hotpluggable busses like USB (and Cardbus PCI), end-users plug devices
962306a36Sopenharmony_ciinto the bus with power on.  In most cases, users expect the devices to become
1062306a36Sopenharmony_ciimmediately usable.  That means the system must do many things, including:
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci    - Find a driver that can handle the device.  That may involve
1362306a36Sopenharmony_ci      loading a kernel module; newer drivers can use module-init-tools
1462306a36Sopenharmony_ci      to publish their device (and class) support to user utilities.
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci    - Bind a driver to that device.  Bus frameworks do that using a
1762306a36Sopenharmony_ci      device driver's probe() routine.
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci    - Tell other subsystems to configure the new device.  Print
2062306a36Sopenharmony_ci      queues may need to be enabled, networks brought up, disk
2162306a36Sopenharmony_ci      partitions mounted, and so on.  In some cases these will
2262306a36Sopenharmony_ci      be driver-specific actions.
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ciThis involves a mix of kernel mode and user mode actions.  Making devices
2562306a36Sopenharmony_cibe immediately usable means that any user mode actions can't wait for an
2662306a36Sopenharmony_ciadministrator to do them:  the kernel must trigger them, either passively
2762306a36Sopenharmony_ci(triggering some monitoring daemon to invoke a helper program) or
2862306a36Sopenharmony_ciactively (calling such a user mode helper program directly).
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ciThose triggered actions must support a system's administrative policies;
3162306a36Sopenharmony_cisuch programs are called "policy agents" here.  Typically they involve
3262306a36Sopenharmony_cishell scripts that dispatch to more familiar administration tools.
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ciBecause some of those actions rely on information about drivers (metadata)
3562306a36Sopenharmony_cithat is currently available only when the drivers are dynamically linked,
3662306a36Sopenharmony_ciyou get the best hotplugging when you configure a highly modular system.
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ciKernel Hotplug Helper (``/sbin/hotplug``)
3962306a36Sopenharmony_ci=========================================
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ciThere is a kernel parameter: ``/proc/sys/kernel/hotplug``, which normally
4262306a36Sopenharmony_ciholds the pathname ``/sbin/hotplug``.  That parameter names a program
4362306a36Sopenharmony_ciwhich the kernel may invoke at various times.
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ciThe /sbin/hotplug program can be invoked by any subsystem as part of its
4662306a36Sopenharmony_cireaction to a configuration change, from a thread in that subsystem.
4762306a36Sopenharmony_ciOnly one parameter is required: the name of a subsystem being notified of
4862306a36Sopenharmony_cisome kernel event.  That name is used as the first key for further event
4962306a36Sopenharmony_cidispatch; any other argument and environment parameters are specified by
5062306a36Sopenharmony_cithe subsystem making that invocation.
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ciHotplug software and other resources is available at:
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	http://linux-hotplug.sourceforge.net
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ciMailing list information is also available at that site.
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ciUSB Policy Agent
6062306a36Sopenharmony_ci================
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ciThe USB subsystem currently invokes ``/sbin/hotplug`` when USB devices
6362306a36Sopenharmony_ciare added or removed from system.  The invocation is done by the kernel
6462306a36Sopenharmony_cihub workqueue [hub_wq], or else as part of root hub initialization
6562306a36Sopenharmony_ci(done by init, modprobe, kapmd, etc).  Its single command line parameter
6662306a36Sopenharmony_ciis the string "usb", and it passes these environment variables:
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci========== ============================================
6962306a36Sopenharmony_ciACTION     ``add``, ``remove``
7062306a36Sopenharmony_ciPRODUCT    USB vendor, product, and version codes (hex)
7162306a36Sopenharmony_ciTYPE       device class codes (decimal)
7262306a36Sopenharmony_ciINTERFACE  interface 0 class codes (decimal)
7362306a36Sopenharmony_ci========== ============================================
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ciIf "usbdevfs" is configured, DEVICE and DEVFS are also passed.  DEVICE is
7662306a36Sopenharmony_cithe pathname of the device, and is useful for devices with multiple and/or
7762306a36Sopenharmony_cialternate interfaces that complicate driver selection.  By design, USB
7862306a36Sopenharmony_cihotplugging is independent of ``usbdevfs``:  you can do most essential parts
7962306a36Sopenharmony_ciof USB device setup without using that filesystem, and without running a
8062306a36Sopenharmony_ciuser mode daemon to detect changes in system configuration.
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ciCurrently available policy agent implementations can load drivers for
8362306a36Sopenharmony_cimodules, and can invoke driver-specific setup scripts.  The newest ones
8462306a36Sopenharmony_cileverage USB module-init-tools support.  Later agents might unload drivers.
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ciUSB Modutils Support
8862306a36Sopenharmony_ci====================
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ciCurrent versions of module-init-tools will create a ``modules.usbmap`` file
9162306a36Sopenharmony_ciwhich contains the entries from each driver's ``MODULE_DEVICE_TABLE``.  Such
9262306a36Sopenharmony_cifiles can be used by various user mode policy agents to make sure all the
9362306a36Sopenharmony_ciright driver modules get loaded, either at boot time or later.
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ciSee ``linux/usb.h`` for full information about such table entries; or look
9662306a36Sopenharmony_ciat existing drivers.  Each table entry describes one or more criteria to
9762306a36Sopenharmony_cibe used when matching a driver to a device or class of devices.  The
9862306a36Sopenharmony_cispecific criteria are identified by bits set in "match_flags", paired
9962306a36Sopenharmony_ciwith field values.  You can construct the criteria directly, or with
10062306a36Sopenharmony_cimacros such as these, and use driver_info to store more information::
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci    USB_DEVICE (vendorId, productId)
10362306a36Sopenharmony_ci	... matching devices with specified vendor and product ids
10462306a36Sopenharmony_ci    USB_DEVICE_VER (vendorId, productId, lo, hi)
10562306a36Sopenharmony_ci	... like USB_DEVICE with lo <= productversion <= hi
10662306a36Sopenharmony_ci    USB_INTERFACE_INFO (class, subclass, protocol)
10762306a36Sopenharmony_ci	... matching specified interface class info
10862306a36Sopenharmony_ci    USB_DEVICE_INFO (class, subclass, protocol)
10962306a36Sopenharmony_ci	... matching specified device class info
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ciA short example, for a driver that supports several specific USB devices
11262306a36Sopenharmony_ciand their quirks, might have a MODULE_DEVICE_TABLE like this::
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci    static const struct usb_device_id mydriver_id_table[] = {
11562306a36Sopenharmony_ci	{ USB_DEVICE (0x9999, 0xaaaa), driver_info: QUIRK_X },
11662306a36Sopenharmony_ci	{ USB_DEVICE (0xbbbb, 0x8888), driver_info: QUIRK_Y|QUIRK_Z },
11762306a36Sopenharmony_ci	...
11862306a36Sopenharmony_ci	{ } /* end with an all-zeroes entry */
11962306a36Sopenharmony_ci    };
12062306a36Sopenharmony_ci    MODULE_DEVICE_TABLE(usb, mydriver_id_table);
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ciMost USB device drivers should pass these tables to the USB subsystem as
12362306a36Sopenharmony_ciwell as to the module management subsystem.  Not all, though: some driver
12462306a36Sopenharmony_ciframeworks connect using interfaces layered over USB, and so they won't
12562306a36Sopenharmony_cineed such a struct usb_driver.
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ciDrivers that connect directly to the USB subsystem should be declared
12862306a36Sopenharmony_cisomething like this::
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci    static struct usb_driver mydriver = {
13162306a36Sopenharmony_ci	.name		= "mydriver",
13262306a36Sopenharmony_ci	.id_table	= mydriver_id_table,
13362306a36Sopenharmony_ci	.probe		= my_probe,
13462306a36Sopenharmony_ci	.disconnect	= my_disconnect,
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci	/*
13762306a36Sopenharmony_ci	if using the usb chardev framework:
13862306a36Sopenharmony_ci	    .minor		= MY_USB_MINOR_START,
13962306a36Sopenharmony_ci	    .fops		= my_file_ops,
14062306a36Sopenharmony_ci	if exposing any operations through usbdevfs:
14162306a36Sopenharmony_ci	    .ioctl		= my_ioctl,
14262306a36Sopenharmony_ci	*/
14362306a36Sopenharmony_ci    };
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ciWhen the USB subsystem knows about a driver's device ID table, it's used when
14662306a36Sopenharmony_cichoosing drivers to probe().  The thread doing new device processing checks
14762306a36Sopenharmony_cidrivers' device ID entries from the ``MODULE_DEVICE_TABLE`` against interface
14862306a36Sopenharmony_ciand device descriptors for the device.  It will only call ``probe()`` if there
14962306a36Sopenharmony_ciis a match, and the third argument to ``probe()`` will be the entry that
15062306a36Sopenharmony_cimatched.
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ciIf you don't provide an ``id_table`` for your driver, then your driver may get
15362306a36Sopenharmony_ciprobed for each new device; the third parameter to ``probe()`` will be
15462306a36Sopenharmony_ci``NULL``.
155