xref: /OK3568_Linux_fs/kernel/Documentation/driver-api/usb/hotplug.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593SmuzhiyunUSB hotplugging
2*4882a593Smuzhiyun~~~~~~~~~~~~~~~
3*4882a593Smuzhiyun
4*4882a593SmuzhiyunLinux Hotplugging
5*4882a593Smuzhiyun=================
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun
8*4882a593SmuzhiyunIn hotpluggable busses like USB (and Cardbus PCI), end-users plug devices
9*4882a593Smuzhiyuninto the bus with power on.  In most cases, users expect the devices to become
10*4882a593Smuzhiyunimmediately usable.  That means the system must do many things, including:
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun    - Find a driver that can handle the device.  That may involve
13*4882a593Smuzhiyun      loading a kernel module; newer drivers can use module-init-tools
14*4882a593Smuzhiyun      to publish their device (and class) support to user utilities.
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun    - Bind a driver to that device.  Bus frameworks do that using a
17*4882a593Smuzhiyun      device driver's probe() routine.
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun    - Tell other subsystems to configure the new device.  Print
20*4882a593Smuzhiyun      queues may need to be enabled, networks brought up, disk
21*4882a593Smuzhiyun      partitions mounted, and so on.  In some cases these will
22*4882a593Smuzhiyun      be driver-specific actions.
23*4882a593Smuzhiyun
24*4882a593SmuzhiyunThis involves a mix of kernel mode and user mode actions.  Making devices
25*4882a593Smuzhiyunbe immediately usable means that any user mode actions can't wait for an
26*4882a593Smuzhiyunadministrator to do them:  the kernel must trigger them, either passively
27*4882a593Smuzhiyun(triggering some monitoring daemon to invoke a helper program) or
28*4882a593Smuzhiyunactively (calling such a user mode helper program directly).
29*4882a593Smuzhiyun
30*4882a593SmuzhiyunThose triggered actions must support a system's administrative policies;
31*4882a593Smuzhiyunsuch programs are called "policy agents" here.  Typically they involve
32*4882a593Smuzhiyunshell scripts that dispatch to more familiar administration tools.
33*4882a593Smuzhiyun
34*4882a593SmuzhiyunBecause some of those actions rely on information about drivers (metadata)
35*4882a593Smuzhiyunthat is currently available only when the drivers are dynamically linked,
36*4882a593Smuzhiyunyou get the best hotplugging when you configure a highly modular system.
37*4882a593Smuzhiyun
38*4882a593SmuzhiyunKernel Hotplug Helper (``/sbin/hotplug``)
39*4882a593Smuzhiyun=========================================
40*4882a593Smuzhiyun
41*4882a593SmuzhiyunThere is a kernel parameter: ``/proc/sys/kernel/hotplug``, which normally
42*4882a593Smuzhiyunholds the pathname ``/sbin/hotplug``.  That parameter names a program
43*4882a593Smuzhiyunwhich the kernel may invoke at various times.
44*4882a593Smuzhiyun
45*4882a593SmuzhiyunThe /sbin/hotplug program can be invoked by any subsystem as part of its
46*4882a593Smuzhiyunreaction to a configuration change, from a thread in that subsystem.
47*4882a593SmuzhiyunOnly one parameter is required: the name of a subsystem being notified of
48*4882a593Smuzhiyunsome kernel event.  That name is used as the first key for further event
49*4882a593Smuzhiyundispatch; any other argument and environment parameters are specified by
50*4882a593Smuzhiyunthe subsystem making that invocation.
51*4882a593Smuzhiyun
52*4882a593SmuzhiyunHotplug software and other resources is available at:
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun	http://linux-hotplug.sourceforge.net
55*4882a593Smuzhiyun
56*4882a593SmuzhiyunMailing list information is also available at that site.
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun
59*4882a593SmuzhiyunUSB Policy Agent
60*4882a593Smuzhiyun================
61*4882a593Smuzhiyun
62*4882a593SmuzhiyunThe USB subsystem currently invokes ``/sbin/hotplug`` when USB devices
63*4882a593Smuzhiyunare added or removed from system.  The invocation is done by the kernel
64*4882a593Smuzhiyunhub workqueue [hub_wq], or else as part of root hub initialization
65*4882a593Smuzhiyun(done by init, modprobe, kapmd, etc).  Its single command line parameter
66*4882a593Smuzhiyunis the string "usb", and it passes these environment variables:
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun========== ============================================
69*4882a593SmuzhiyunACTION     ``add``, ``remove``
70*4882a593SmuzhiyunPRODUCT    USB vendor, product, and version codes (hex)
71*4882a593SmuzhiyunTYPE       device class codes (decimal)
72*4882a593SmuzhiyunINTERFACE  interface 0 class codes (decimal)
73*4882a593Smuzhiyun========== ============================================
74*4882a593Smuzhiyun
75*4882a593SmuzhiyunIf "usbdevfs" is configured, DEVICE and DEVFS are also passed.  DEVICE is
76*4882a593Smuzhiyunthe pathname of the device, and is useful for devices with multiple and/or
77*4882a593Smuzhiyunalternate interfaces that complicate driver selection.  By design, USB
78*4882a593Smuzhiyunhotplugging is independent of ``usbdevfs``:  you can do most essential parts
79*4882a593Smuzhiyunof USB device setup without using that filesystem, and without running a
80*4882a593Smuzhiyunuser mode daemon to detect changes in system configuration.
81*4882a593Smuzhiyun
82*4882a593SmuzhiyunCurrently available policy agent implementations can load drivers for
83*4882a593Smuzhiyunmodules, and can invoke driver-specific setup scripts.  The newest ones
84*4882a593Smuzhiyunleverage USB module-init-tools support.  Later agents might unload drivers.
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun
87*4882a593SmuzhiyunUSB Modutils Support
88*4882a593Smuzhiyun====================
89*4882a593Smuzhiyun
90*4882a593SmuzhiyunCurrent versions of module-init-tools will create a ``modules.usbmap`` file
91*4882a593Smuzhiyunwhich contains the entries from each driver's ``MODULE_DEVICE_TABLE``.  Such
92*4882a593Smuzhiyunfiles can be used by various user mode policy agents to make sure all the
93*4882a593Smuzhiyunright driver modules get loaded, either at boot time or later.
94*4882a593Smuzhiyun
95*4882a593SmuzhiyunSee ``linux/usb.h`` for full information about such table entries; or look
96*4882a593Smuzhiyunat existing drivers.  Each table entry describes one or more criteria to
97*4882a593Smuzhiyunbe used when matching a driver to a device or class of devices.  The
98*4882a593Smuzhiyunspecific criteria are identified by bits set in "match_flags", paired
99*4882a593Smuzhiyunwith field values.  You can construct the criteria directly, or with
100*4882a593Smuzhiyunmacros such as these, and use driver_info to store more information::
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun    USB_DEVICE (vendorId, productId)
103*4882a593Smuzhiyun	... matching devices with specified vendor and product ids
104*4882a593Smuzhiyun    USB_DEVICE_VER (vendorId, productId, lo, hi)
105*4882a593Smuzhiyun	... like USB_DEVICE with lo <= productversion <= hi
106*4882a593Smuzhiyun    USB_INTERFACE_INFO (class, subclass, protocol)
107*4882a593Smuzhiyun	... matching specified interface class info
108*4882a593Smuzhiyun    USB_DEVICE_INFO (class, subclass, protocol)
109*4882a593Smuzhiyun	... matching specified device class info
110*4882a593Smuzhiyun
111*4882a593SmuzhiyunA short example, for a driver that supports several specific USB devices
112*4882a593Smuzhiyunand their quirks, might have a MODULE_DEVICE_TABLE like this::
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun    static const struct usb_device_id mydriver_id_table[] = {
115*4882a593Smuzhiyun	{ USB_DEVICE (0x9999, 0xaaaa), driver_info: QUIRK_X },
116*4882a593Smuzhiyun	{ USB_DEVICE (0xbbbb, 0x8888), driver_info: QUIRK_Y|QUIRK_Z },
117*4882a593Smuzhiyun	...
118*4882a593Smuzhiyun	{ } /* end with an all-zeroes entry */
119*4882a593Smuzhiyun    };
120*4882a593Smuzhiyun    MODULE_DEVICE_TABLE(usb, mydriver_id_table);
121*4882a593Smuzhiyun
122*4882a593SmuzhiyunMost USB device drivers should pass these tables to the USB subsystem as
123*4882a593Smuzhiyunwell as to the module management subsystem.  Not all, though: some driver
124*4882a593Smuzhiyunframeworks connect using interfaces layered over USB, and so they won't
125*4882a593Smuzhiyunneed such a struct usb_driver.
126*4882a593Smuzhiyun
127*4882a593SmuzhiyunDrivers that connect directly to the USB subsystem should be declared
128*4882a593Smuzhiyunsomething like this::
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun    static struct usb_driver mydriver = {
131*4882a593Smuzhiyun	.name		= "mydriver",
132*4882a593Smuzhiyun	.id_table	= mydriver_id_table,
133*4882a593Smuzhiyun	.probe		= my_probe,
134*4882a593Smuzhiyun	.disconnect	= my_disconnect,
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun	/*
137*4882a593Smuzhiyun	if using the usb chardev framework:
138*4882a593Smuzhiyun	    .minor		= MY_USB_MINOR_START,
139*4882a593Smuzhiyun	    .fops		= my_file_ops,
140*4882a593Smuzhiyun	if exposing any operations through usbdevfs:
141*4882a593Smuzhiyun	    .ioctl		= my_ioctl,
142*4882a593Smuzhiyun	*/
143*4882a593Smuzhiyun    };
144*4882a593Smuzhiyun
145*4882a593SmuzhiyunWhen the USB subsystem knows about a driver's device ID table, it's used when
146*4882a593Smuzhiyunchoosing drivers to probe().  The thread doing new device processing checks
147*4882a593Smuzhiyundrivers' device ID entries from the ``MODULE_DEVICE_TABLE`` against interface
148*4882a593Smuzhiyunand device descriptors for the device.  It will only call ``probe()`` if there
149*4882a593Smuzhiyunis a match, and the third argument to ``probe()`` will be the entry that
150*4882a593Smuzhiyunmatched.
151*4882a593Smuzhiyun
152*4882a593SmuzhiyunIf you don't provide an ``id_table`` for your driver, then your driver may get
153*4882a593Smuzhiyunprobed for each new device; the third parameter to ``probe()`` will be
154*4882a593Smuzhiyun``NULL``.
155