hotplug.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. USB hotplugging
  2. ~~~~~~~~~~~~~~~
  3. Linux Hotplugging
  4. =================
  5. In hotpluggable busses like USB (and Cardbus PCI), end-users plug devices
  6. into the bus with power on. In most cases, users expect the devices to become
  7. immediately usable. That means the system must do many things, including:
  8. - Find a driver that can handle the device. That may involve
  9. loading a kernel module; newer drivers can use module-init-tools
  10. to publish their device (and class) support to user utilities.
  11. - Bind a driver to that device. Bus frameworks do that using a
  12. device driver's probe() routine.
  13. - Tell other subsystems to configure the new device. Print
  14. queues may need to be enabled, networks brought up, disk
  15. partitions mounted, and so on. In some cases these will
  16. be driver-specific actions.
  17. This involves a mix of kernel mode and user mode actions. Making devices
  18. be immediately usable means that any user mode actions can't wait for an
  19. administrator to do them: the kernel must trigger them, either passively
  20. (triggering some monitoring daemon to invoke a helper program) or
  21. actively (calling such a user mode helper program directly).
  22. Those triggered actions must support a system's administrative policies;
  23. such programs are called "policy agents" here. Typically they involve
  24. shell scripts that dispatch to more familiar administration tools.
  25. Because some of those actions rely on information about drivers (metadata)
  26. that is currently available only when the drivers are dynamically linked,
  27. you get the best hotplugging when you configure a highly modular system.
  28. Kernel Hotplug Helper (``/sbin/hotplug``)
  29. =========================================
  30. There is a kernel parameter: ``/proc/sys/kernel/hotplug``, which normally
  31. holds the pathname ``/sbin/hotplug``. That parameter names a program
  32. which the kernel may invoke at various times.
  33. The /sbin/hotplug program can be invoked by any subsystem as part of its
  34. reaction to a configuration change, from a thread in that subsystem.
  35. Only one parameter is required: the name of a subsystem being notified of
  36. some kernel event. That name is used as the first key for further event
  37. dispatch; any other argument and environment parameters are specified by
  38. the subsystem making that invocation.
  39. Hotplug software and other resources is available at:
  40. http://linux-hotplug.sourceforge.net
  41. Mailing list information is also available at that site.
  42. USB Policy Agent
  43. ================
  44. The USB subsystem currently invokes ``/sbin/hotplug`` when USB devices
  45. are added or removed from system. The invocation is done by the kernel
  46. hub workqueue [hub_wq], or else as part of root hub initialization
  47. (done by init, modprobe, kapmd, etc). Its single command line parameter
  48. is the string "usb", and it passes these environment variables:
  49. ========== ============================================
  50. ACTION ``add``, ``remove``
  51. PRODUCT USB vendor, product, and version codes (hex)
  52. TYPE device class codes (decimal)
  53. INTERFACE interface 0 class codes (decimal)
  54. ========== ============================================
  55. If "usbdevfs" is configured, DEVICE and DEVFS are also passed. DEVICE is
  56. the pathname of the device, and is useful for devices with multiple and/or
  57. alternate interfaces that complicate driver selection. By design, USB
  58. hotplugging is independent of ``usbdevfs``: you can do most essential parts
  59. of USB device setup without using that filesystem, and without running a
  60. user mode daemon to detect changes in system configuration.
  61. Currently available policy agent implementations can load drivers for
  62. modules, and can invoke driver-specific setup scripts. The newest ones
  63. leverage USB module-init-tools support. Later agents might unload drivers.
  64. USB Modutils Support
  65. ====================
  66. Current versions of module-init-tools will create a ``modules.usbmap`` file
  67. which contains the entries from each driver's ``MODULE_DEVICE_TABLE``. Such
  68. files can be used by various user mode policy agents to make sure all the
  69. right driver modules get loaded, either at boot time or later.
  70. See ``linux/usb.h`` for full information about such table entries; or look
  71. at existing drivers. Each table entry describes one or more criteria to
  72. be used when matching a driver to a device or class of devices. The
  73. specific criteria are identified by bits set in "match_flags", paired
  74. with field values. You can construct the criteria directly, or with
  75. macros such as these, and use driver_info to store more information::
  76. USB_DEVICE (vendorId, productId)
  77. ... matching devices with specified vendor and product ids
  78. USB_DEVICE_VER (vendorId, productId, lo, hi)
  79. ... like USB_DEVICE with lo <= productversion <= hi
  80. USB_INTERFACE_INFO (class, subclass, protocol)
  81. ... matching specified interface class info
  82. USB_DEVICE_INFO (class, subclass, protocol)
  83. ... matching specified device class info
  84. A short example, for a driver that supports several specific USB devices
  85. and their quirks, might have a MODULE_DEVICE_TABLE like this::
  86. static const struct usb_device_id mydriver_id_table[] = {
  87. { USB_DEVICE (0x9999, 0xaaaa), driver_info: QUIRK_X },
  88. { USB_DEVICE (0xbbbb, 0x8888), driver_info: QUIRK_Y|QUIRK_Z },
  89. ...
  90. { } /* end with an all-zeroes entry */
  91. };
  92. MODULE_DEVICE_TABLE(usb, mydriver_id_table);
  93. Most USB device drivers should pass these tables to the USB subsystem as
  94. well as to the module management subsystem. Not all, though: some driver
  95. frameworks connect using interfaces layered over USB, and so they won't
  96. need such a struct usb_driver.
  97. Drivers that connect directly to the USB subsystem should be declared
  98. something like this::
  99. static struct usb_driver mydriver = {
  100. .name = "mydriver",
  101. .id_table = mydriver_id_table,
  102. .probe = my_probe,
  103. .disconnect = my_disconnect,
  104. /*
  105. if using the usb chardev framework:
  106. .minor = MY_USB_MINOR_START,
  107. .fops = my_file_ops,
  108. if exposing any operations through usbdevfs:
  109. .ioctl = my_ioctl,
  110. */
  111. };
  112. When the USB subsystem knows about a driver's device ID table, it's used when
  113. choosing drivers to probe(). The thread doing new device processing checks
  114. drivers' device ID entries from the ``MODULE_DEVICE_TABLE`` against interface
  115. and device descriptors for the device. It will only call ``probe()`` if there
  116. is a match, and the third argument to ``probe()`` will be the entry that
  117. matched.
  118. If you don't provide an ``id_table`` for your driver, then your driver may get
  119. probed for each new device; the third parameter to ``probe()`` will be
  120. ``NULL``.