hotplug.txt 6.3 KB

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