usbhotplug.txt 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. Linux specific installation information
  2. by Linus Walleij
  3. covering libusb 0.1.10
  4. ========================================
  5. Since libusb is accessing the raw device nodes exported by the kernel
  6. in order to identify connected USB busses, functions and such, it
  7. needs to find these nodes. During the history of the Linux kernel,
  8. the placement and the way of accessing these nodes have changed.
  9. This placement is closely related to hotplugging: i.e. addition and
  10. removal of USB functions at runtime.
  11. Most current solution: use udev
  12. ===============================
  13. The latest and greatest way of managing hotplugged (and cold-plugged)
  14. devices under Linux is called "udev". This is a development of the
  15. older "hotplug" system (see below).
  16. When a device is connected, the kernel will call the program
  17. /sbin/udev in order to create a device node in the /dev/ file
  18. hirerarchy. It will also remove devices from this hierarchy when
  19. they are unplugged.
  20. Traditionally, all devices plugged into a Linux system are expected
  21. to have a kernel device driver, or to load one on-the-fly when a
  22. new device is connected. Libusb cannot use these device drivers,
  23. instead it attempts to access the raw device nodes from user mode,
  24. not as a kernel module.
  25. In order for libusb to find the device node, it needs to locate it
  26. in the /dev filesystem. The recommended way to let udev create nodes
  27. in the /dev filesystem is to add a udev rule like the following into
  28. some foo.rules file inside the /etc/udev/rules.d/ directory:
  29. # usbfs-like devices
  30. SUBSYSTEM=="usb_device", PROGRAM="/bin/sh -c 'K=%k; K=$${K#usbdev}; \
  31. printf bus/usb/%%03i/%%03i $${K%%%%.*} $${K#*.}'", \
  32. NAME="%c"
  33. This layout is used by for example the Debian distribution and Fedora
  34. Core. This rule creates a device tree identical to the earlier
  35. /proc/bus/usb/ tree, but under /dev/bus/usb/ instead. If this device
  36. tree exists, libusb will default to use it. It will look like this:
  37. /dev
  38. /bus
  39. /usb
  40. /001
  41. /001
  42. /002
  43. /003
  44. /002
  45. /001
  46. /002
  47. ...
  48. However notice that the permissions on the nodes will be default
  49. permissions: often this means they are only accessible for writing
  50. by the root user, whereas non-root users often can access it
  51. read-only.
  52. The way of controlling access to a device node differs between
  53. systems, but a typical way of complementing udev rules with
  54. apropriate permissions is to use PAM (pluggable Authentication
  55. Modules), with some sort of configuration under /etc/security/.
  56. (For details on this, see below.)
  57. The use of /dev nodes is also different from the old usbfs
  58. solution in that it enables the use of ACL:s (Access Control
  59. Lists) to control access for the USB device nodes. ACL:s could
  60. not be used on the /proc filesystem.
  61. A less good alternative that may however be useful for debugging
  62. would be to supply the argument MODE="666" to the above udev
  63. rule, or, slightly better, to tag on:
  64. MODE="660", GROUP="foo"
  65. where "foo" is a group of users (e.g. desktop users) that need
  66. to access the device in read/write mode.
  67. If libusb cannot find a device hierarchy below /dev/bus/usb/
  68. (as is the case if you are not using udev, or not using it with
  69. the above rule), it will fall back on using /proc/bus/usb/
  70. instead.
  71. Additionally, you may want to trigger unique actions for your
  72. device at the same time. To do this, create a rules file
  73. /etc/udev/rules.d/bar.rules with these lines:
  74. SUBSYSTEM=="usb_device", ACTION=="add", SYSFS{idVendor}=="1234", \
  75. SYSFS{idProduct}=="4321"
  76. At the end of this line you can then tag on any device-specific
  77. actions for device 1234/4321, for example:
  78. MODE="660", GROUP="baz" to set mode and group
  79. RUN="/usr/local/bin/baz" to run a script on plug-in
  80. SYMLINK+="foo" to create a symlink device node with
  81. this name in /dev
  82. You can read more about udev in its own documentation.
  83. Permissions setting with PAM
  84. ============================
  85. In addition to the udev rule for creating the device node you
  86. will want to change the permissions on the new node, unless it
  87. defaults to something that is globally writeable and readable.
  88. Making anything that is plugged in on the USB bus writeable and
  89. readable by ALL users is typically a bad idea, because what you
  90. most typically want to do is to make it writeable and readable
  91. for the console user, i.e. the person that happens to sit behind
  92. the screen and keyboard of this very computer.
  93. Managing this by groups is a bit kludgy: it means you set up a
  94. group for all console users and add all users that may use the
  95. console to this group. This also mean that one user that is a
  96. member of this group could be at the console plugging his USB
  97. keydrive in, while another user of the same group is logged in
  98. remotely, and making a blank copy of the same keydrive at the
  99. same time, for example.
  100. Since Linux is used in a strict multi-user context, this has to
  101. be solved: give permissions to hotplugged devices only to the
  102. console user.
  103. Fedora Core 5 and later does this by using PAM. Whenever
  104. something happens in udev, PAM is called to modify the
  105. permissions on anything that appeared in the file system in
  106. accordance to a set of security rules.
  107. The trick is to create a symbolic link for your new device,
  108. then let PAM match the name of this link and change the
  109. permissions of it. For example, in /etc/udev/rules.d/foo.rules
  110. you write:
  111. SUBSYSTEM=="usb_device", ACTION=="add", SYSFS{idVendor}=="1234", \
  112. SYSFS{idProduct}=="4321", SYMLINK+="foo-%k"
  113. This will create a symlink named "/dev/foo-nn" where nn is some
  114. unique number for each added device matching this VID and PID.
  115. You then set up PAM console rules in accordance, by adding
  116. a /etc/security/console.perms.d/foo.perms containing:
  117. <foo>=/dev/foo*
  118. <console> 0600 <foo> 0600 root
  119. This instructs PAM to give the console user (and root) read and
  120. write permissions to the new symlink, whenever it appears. The
  121. permission change on the symlink will then fall through to the
  122. new device node.
  123. Previous solution: use hotplug
  124. ==============================
  125. Before udev another system, generally considered less elegant,
  126. known simply as "hotplug" was used. In this case the program
  127. /sbin/hotplug would be called whenever devices were connected
  128. or removed from the system, and the corresponding configuration
  129. lives in /etc/hotplug/.
  130. With hotplug not using udev at the same time, all devices are
  131. accessed using the usbfs hierarchy below /proc/bus/usb/. Again,
  132. this will be used by libusb, since libusb does not use any device
  133. drivers. The hierarchy will look like this:
  134. /proc
  135. /bus
  136. /usb
  137. /001
  138. /001
  139. /002
  140. /003
  141. /002
  142. /001
  143. /002
  144. ...
  145. When USB devices are plugged in, their corresponding device
  146. node is created in /proc/bus/usb/ by the kernel, without any
  147. external program intervention (as is the case with udev).
  148. However, to correct the permissions on these device nodes, if
  149. your device requires anything else than read access, you need
  150. to supply a script in /etc/hotplug/usb/ that detects your
  151. device and change its permissions, for example this
  152. /etc/hotplug/usb/foo.usermap
  153. # Foo device with VID=1234 and PID=4321
  154. bar 0x0003 0x1234 0x4321 0x0000 0x0000 0x00 0x00 0x00 0x00 0x00 0x00 0x00000000
  155. (All this need to be in one line.)
  156. The first string "bar" points out the name of a script placed
  157. in /etc/hotplug/usb/bar, with for example the following contents:
  158. #!/bin/bash
  159. if [ "${ACTION}" = "add" ] && [ -f "${DEVICE}" ]
  160. then
  161. chgrp baz "${DEVICE}"
  162. chmod 660 "${DEVICE}"
  163. fi
  164. to let users in the group "baz" access the device for reading
  165. and writing. There exist solutions similar to the PAM permission
  166. change for hotplug, but they are all kind of hackish.
  167. You can read more about hotplug and its usermaps in the
  168. hotplug documentation.