hotplug.txt
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:6k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

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