hiddev.txt 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. Care and feeding of your Human Interface Devices
  2. INTRODUCTION
  3. In addition to the normal input type HID devices, USB also uses the
  4. human interface device protocols for things that are not really human
  5. interfaces, but have similar sorts of communication needs. The two big
  6. examples for this are power devices (especially uninterruptable power
  7. supplies) and monitor control on higher end monitors.
  8. To support these disparate requirements, the Linux USB system provides
  9. HID events to two separate interfaces:
  10. * the input subsystem, which converts HID events into normal input
  11. device interfaces (such as keyboard, mouse and joystick) and a
  12. normalised event interface - see Documentation/input/input.txt
  13. * the hiddev interface, which provides fairly raw HID events
  14. The data flow for a HID event produced by a device is something like
  15. the following :
  16. usb.c ---> hid-core.c ----> hid-input.c ----> [keyboard/mouse/joystick/event]
  17. |
  18. |
  19. --> hiddev.c ----> POWER / MONITOR CONTROL
  20. In addition, other subsystems (apart from USB) can potentially feed
  21. events into the input subsystem, but these have no effect on the hid
  22. device interface.
  23. USING THE HID DEVICE INTERFACE
  24. The hiddev interface is a char interface using the normal USB major,
  25. with the minor numbers starting at 96 and finishing at 111. Therefore,
  26. you need the following commands:
  27. mknod /dev/usb/hiddev0 c 180 96
  28. mknod /dev/usb/hiddev1 c 180 97
  29. mknod /dev/usb/hiddev2 c 180 98
  30. mknod /dev/usb/hiddev3 c 180 99
  31. mknod /dev/usb/hiddev4 c 180 100
  32. mknod /dev/usb/hiddev5 c 180 101
  33. mknod /dev/usb/hiddev6 c 180 102
  34. mknod /dev/usb/hiddev7 c 180 103
  35. mknod /dev/usb/hiddev8 c 180 104
  36. mknod /dev/usb/hiddev9 c 180 105
  37. mknod /dev/usb/hiddev10 c 180 106
  38. mknod /dev/usb/hiddev11 c 180 107
  39. mknod /dev/usb/hiddev12 c 180 108
  40. mknod /dev/usb/hiddev13 c 180 109
  41. mknod /dev/usb/hiddev14 c 180 110
  42. mknod /dev/usb/hiddev15 c 180 111
  43. So you point your hiddev compliant user-space program at the correct
  44. interface for your device, and it all just works.
  45. Assuming that you have a hiddev compliant user-space program, of
  46. course. If you need to write one, read on.
  47. THE HIDDEV API
  48. This description should be read in conjunction with the HID
  49. specification, freely available from http://www.usb.org, and
  50. conveniently linked of http://www.linux-usb.org.
  51. The hiddev API uses a read() interface, and a set of ioctl() calls.
  52. HID devices exchange data with the host computer using data
  53. bundles called "reports". Each report is divided into "fields",
  54. each of which can have one or more "usages". In the hid-core,
  55. each one of these usages has a single signed 32 bit value.
  56. read():
  57. This is the event interface. When the HID device's state changes,
  58. it performs an interrupt transfer containing a report which contains
  59. the changed value. The hid-core.c module parses the report, and
  60. returns to hiddev.c the individual usages that have changed within
  61. the report. In its basic mode, the hiddev will make these individual
  62. usage changes available to the reader using a struct hiddev_event:
  63. struct hiddev_event {
  64. unsigned hid;
  65. signed int value;
  66. };
  67. containing the HID usage identifier for the status that changed, and
  68. the value that it was changed to. Note that the structure is defined
  69. within <linux/hiddev.h>, along with some other useful #defines and
  70. structures. The HID usage identifier is a composite of the HID usage
  71. page shifted to the 16 high order bits ORed with the usage code. The
  72. behavior of the read() function can be modified using the HIDIOCSFLAG
  73. ioctl() described below.
  74. ioctl():
  75. This is the control interface. There are a number of controls:
  76. HIDIOCGVERSION - int (read)
  77. Gets the version code out of the hiddev driver.
  78. HIDIOCAPPLICATION - (none)
  79. This ioctl call returns the HID application usage associated with the
  80. hid device. The third argument to ioctl() specifies which application
  81. index to get. This is useful when the device has more than one
  82. application collection. If the index is invalid (greater or equal to
  83. the number of application collections this device has) the ioctl
  84. returns -1. You can find out beforehand how many application
  85. collections the device has from the num_applications field from the
  86. hiddev_devinfo structure.
  87. HIDIOCGCOLLECTIONINFO - struct hiddev_collection_info (read/write)
  88. This returns a superset of the information above, providing not only
  89. application collections, but all the collections the device has. It
  90. also returns the level the collection lives in the hierarchy.
  91. The user passes in a hiddev_collection_info struct with the index
  92. field set to the index that should be returned. The ioctl fills in
  93. the other fields. If the index is larger than the last collection
  94. index, the ioctl returns -1 and sets errno to -EINVAL.
  95. HIDIOCGDEVINFO - struct hiddev_devinfo (read)
  96. Gets a hiddev_devinfo structure which describes the device.
  97. HIDIOCGSTRING - struct hiddev_string_descriptor (read/write)
  98. Gets a string descriptor from the device. The caller must fill in the
  99. "index" field to indicate which descriptor should be returned.
  100. HIDIOCINITREPORT - (none)
  101. Instructs the kernel to retrieve all input and feature report values
  102. from the device. At this point, all the usage structures will contain
  103. current values for the device, and will maintain it as the device
  104. changes. Note that the use of this ioctl is unnecessary in general,
  105. since later kernels automatically initialize the reports from the
  106. device at attach time.
  107. HIDIOCGNAME - string (variable length)
  108. Gets the device name
  109. HIDIOCGREPORT - struct hiddev_report_info (write)
  110. Instructs the kernel to get a feature or input report from the device,
  111. in order to selectively update the usage structures (in contrast to
  112. INITREPORT).
  113. HIDIOCSREPORT - struct hiddev_report_info (write)
  114. Instructs the kernel to send a report to the device. This report can
  115. be filled in by the user through HIDIOCSUSAGE calls (below) to fill in
  116. individual usage values in the report before sending the report in full
  117. to the device.
  118. HIDIOCGREPORTINFO - struct hiddev_report_info (read/write)
  119. Fills in a hiddev_report_info structure for the user. The report is
  120. looked up by type (input, output or feature) and id, so these fields
  121. must be filled in by the user. The ID can be absolute -- the actual
  122. report id as reported by the device -- or relative --
  123. HID_REPORT_ID_FIRST for the first report, and (HID_REPORT_ID_NEXT |
  124. report_id) for the next report after report_id. Without a-priori
  125. information about report ids, the right way to use this ioctl is to
  126. use the relative IDs above to enumerate the valid IDs. The ioctl
  127. returns non-zero when there is no more next ID. The real report ID is
  128. filled into the returned hiddev_report_info structure.
  129. HIDIOCGFIELDINFO - struct hiddev_field_info (read/write)
  130. Returns the field information associated with a report in a
  131. hiddev_field_info structure. The user must fill in report_id and
  132. report_type in this structure, as above. The field_index should also
  133. be filled in, which should be a number from 0 and maxfield-1, as
  134. returned from a previous HIDIOCGREPORTINFO call.
  135. HIDIOCGUCODE - struct hiddev_usage_ref (read/write)
  136. Returns the usage_code in a hiddev_usage_ref structure, given that
  137. given its report type, report id, field index, and index within the
  138. field have already been filled into the structure.
  139. HIDIOCGUSAGE - struct hiddev_usage_ref (read/write)
  140. Returns the value of a usage in a hiddev_usage_ref structure. The
  141. usage to be retrieved can be specified as above, or the user can
  142. choose to fill in the report_type field and specify the report_id as
  143. HID_REPORT_ID_UNKNOWN. In this case, the hiddev_usage_ref will be
  144. filled in with the report and field information associated with this
  145. usage if it is found.
  146. HIDIOCSUSAGE - struct hiddev_usage_ref (write)
  147. Sets the value of a usage in an output report. The user fills in
  148. the hiddev_usage_ref structure as above, but additionally fills in
  149. the value field.
  150. HIDIOGCOLLECTIONINDEX - struct hiddev_usage_ref (write)
  151. Returns the collection index associated with this usage. This
  152. indicates where in the collection hierarchy this usage sits.
  153. HIDIOCGFLAG - int (read)
  154. HIDIOCSFLAG - int (write)
  155. These operations respectively inspect and replace the mode flags
  156. that influence the read() call above. The flags are as follows:
  157. HIDDEV_FLAG_UREF - read() calls will now return
  158. struct hiddev_usage_ref instead of struct hiddev_event.
  159. This is a larger structure, but in situations where the
  160. device has more than one usage in its reports with the
  161. same usage code, this mode serves to resolve such
  162. ambiguity.
  163. HIDDEV_FLAG_REPORT - This flag can only be used in conjunction
  164. with HIDDEV_FLAG_UREF. With this flag set, when the device
  165. sends a report, a struct hiddev_usage_ref will be returned
  166. to read() filled in with the report_type and report_id, but
  167. with field_index set to FIELD_INDEX_NONE. This serves as
  168. additional notification when the device has sent a report.