hotplug.rst 6.4 KB
Newer Older
1 2 3 4 5 6
USB hotplugging
~~~~~~~~~~~~~~~

Linux Hotplugging
=================

L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15 16 17

In hotpluggable busses like USB (and Cardbus PCI), end-users plug devices
into the bus with power on.  In most cases, users expect the devices to become
immediately usable.  That means the system must do many things, including:

    - Find a driver that can handle the device.  That may involve
      loading a kernel module; newer drivers can use module-init-tools
      to publish their device (and class) support to user utilities.

    - Bind a driver to that device.  Bus frameworks do that using a
      device driver's probe() routine.
18

L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    - Tell other subsystems to configure the new device.  Print
      queues may need to be enabled, networks brought up, disk
      partitions mounted, and so on.  In some cases these will
      be driver-specific actions.

This involves a mix of kernel mode and user mode actions.  Making devices
be immediately usable means that any user mode actions can't wait for an
administrator to do them:  the kernel must trigger them, either passively
(triggering some monitoring daemon to invoke a helper program) or
actively (calling such a user mode helper program directly).

Those triggered actions must support a system's administrative policies;
such programs are called "policy agents" here.  Typically they involve
shell scripts that dispatch to more familiar administration tools.

Because some of those actions rely on information about drivers (metadata)
that is currently available only when the drivers are dynamically linked,
you get the best hotplugging when you configure a highly modular system.

38 39
Kernel Hotplug Helper (``/sbin/hotplug``)
=========================================
L
Linus Torvalds 已提交
40

41 42
There is a kernel parameter: ``/proc/sys/kernel/hotplug``, which normally
holds the pathname ``/sbin/hotplug``.  That parameter names a program
43
which the kernel may invoke at various times.
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

The /sbin/hotplug program can be invoked by any subsystem as part of its
reaction to a configuration change, from a thread in that subsystem.
Only one parameter is required: the name of a subsystem being notified of
some kernel event.  That name is used as the first key for further event
dispatch; any other argument and environment parameters are specified by
the subsystem making that invocation.

Hotplug software and other resources is available at:

	http://linux-hotplug.sourceforge.net

Mailing list information is also available at that site.


59 60
USB Policy Agent
================
L
Linus Torvalds 已提交
61

62
The USB subsystem currently invokes ``/sbin/hotplug`` when USB devices
L
Linus Torvalds 已提交
63
are added or removed from system.  The invocation is done by the kernel
64
hub workqueue [hub_wq], or else as part of root hub initialization
L
Linus Torvalds 已提交
65 66 67
(done by init, modprobe, kapmd, etc).  Its single command line parameter
is the string "usb", and it passes these environment variables:

68 69 70 71 72 73
========== ============================================
ACTION     ``add``, ``remove``
PRODUCT    USB vendor, product, and version codes (hex)
TYPE       device class codes (decimal)
INTERFACE  interface 0 class codes (decimal)
========== ============================================
L
Linus Torvalds 已提交
74 75 76 77

If "usbdevfs" is configured, DEVICE and DEVFS are also passed.  DEVICE is
the pathname of the device, and is useful for devices with multiple and/or
alternate interfaces that complicate driver selection.  By design, USB
78
hotplugging is independent of ``usbdevfs``:  you can do most essential parts
L
Linus Torvalds 已提交
79 80 81 82 83 84 85 86
of USB device setup without using that filesystem, and without running a
user mode daemon to detect changes in system configuration.

Currently available policy agent implementations can load drivers for
modules, and can invoke driver-specific setup scripts.  The newest ones
leverage USB module-init-tools support.  Later agents might unload drivers.


87 88
USB Modutils Support
====================
L
Linus Torvalds 已提交
89

90 91
Current versions of module-init-tools will create a ``modules.usbmap`` file
which contains the entries from each driver's ``MODULE_DEVICE_TABLE``.  Such
L
Linus Torvalds 已提交
92
files can be used by various user mode policy agents to make sure all the
93
right driver modules get loaded, either at boot time or later.
L
Linus Torvalds 已提交
94

95
See ``linux/usb.h`` for full information about such table entries; or look
L
Linus Torvalds 已提交
96 97 98 99
at existing drivers.  Each table entry describes one or more criteria to
be used when matching a driver to a device or class of devices.  The
specific criteria are identified by bits set in "match_flags", paired
with field values.  You can construct the criteria directly, or with
100
macros such as these, and use driver_info to store more information::
L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108 109 110 111

    USB_DEVICE (vendorId, productId)
	... matching devices with specified vendor and product ids
    USB_DEVICE_VER (vendorId, productId, lo, hi)
	... like USB_DEVICE with lo <= productversion <= hi
    USB_INTERFACE_INFO (class, subclass, protocol)
	... matching specified interface class info
    USB_DEVICE_INFO (class, subclass, protocol)
	... matching specified device class info

A short example, for a driver that supports several specific USB devices
112
and their quirks, might have a MODULE_DEVICE_TABLE like this::
L
Linus Torvalds 已提交
113

114
    static const struct usb_device_id mydriver_id_table[] = {
L
Linus Torvalds 已提交
115 116 117 118
	{ USB_DEVICE (0x9999, 0xaaaa), driver_info: QUIRK_X },
	{ USB_DEVICE (0xbbbb, 0x8888), driver_info: QUIRK_Y|QUIRK_Z },
	...
	{ } /* end with an all-zeroes entry */
119 120
    };
    MODULE_DEVICE_TABLE(usb, mydriver_id_table);
L
Linus Torvalds 已提交
121 122 123 124

Most USB device drivers should pass these tables to the USB subsystem as
well as to the module management subsystem.  Not all, though: some driver
frameworks connect using interfaces layered over USB, and so they won't
125
need such a struct :c:type:`usb_driver`.
L
Linus Torvalds 已提交
126 127

Drivers that connect directly to the USB subsystem should be declared
128
something like this::
L
Linus Torvalds 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142

    static struct usb_driver mydriver = {
	.name		= "mydriver",
	.id_table	= mydriver_id_table,
	.probe		= my_probe,
	.disconnect	= my_disconnect,

	/*
	if using the usb chardev framework:
	    .minor		= MY_USB_MINOR_START,
	    .fops		= my_file_ops,
	if exposing any operations through usbdevfs:
	    .ioctl		= my_ioctl,
	*/
143
    };
L
Linus Torvalds 已提交
144 145 146

When the USB subsystem knows about a driver's device ID table, it's used when
choosing drivers to probe().  The thread doing new device processing checks
147 148 149 150 151 152 153 154
drivers' device ID entries from the ``MODULE_DEVICE_TABLE`` against interface
and device descriptors for the device.  It will only call ``probe()`` if there
is a match, and the third argument to ``probe()`` will be the entry that
matched.

If you don't provide an ``id_table`` for your driver, then your driver may get
probed for each new device; the third parameter to ``probe()`` will be
``NULL``.