hidraw.rst 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. ================================================================
  2. HIDRAW - Raw Access to USB and Bluetooth Human Interface Devices
  3. ================================================================
  4. The hidraw driver provides a raw interface to USB and Bluetooth Human
  5. Interface Devices (HIDs). It differs from hiddev in that reports sent and
  6. received are not parsed by the HID parser, but are sent to and received from
  7. the device unmodified.
  8. Hidraw should be used if the userspace application knows exactly how to
  9. communicate with the hardware device, and is able to construct the HID
  10. reports manually. This is often the case when making userspace drivers for
  11. custom HID devices.
  12. Hidraw is also useful for communicating with non-conformant HID devices
  13. which send and receive data in a way that is inconsistent with their report
  14. descriptors. Because hiddev parses reports which are sent and received
  15. through it, checking them against the device's report descriptor, such
  16. communication with these non-conformant devices is impossible using hiddev.
  17. Hidraw is the only alternative, short of writing a custom kernel driver, for
  18. these non-conformant devices.
  19. A benefit of hidraw is that its use by userspace applications is independent
  20. of the underlying hardware type. Currently, hidraw is implemented for USB
  21. and Bluetooth. In the future, as new hardware bus types are developed which
  22. use the HID specification, hidraw will be expanded to add support for these
  23. new bus types.
  24. Hidraw uses a dynamic major number, meaning that udev should be relied on to
  25. create hidraw device nodes. Udev will typically create the device nodes
  26. directly under /dev (eg: /dev/hidraw0). As this location is distribution-
  27. and udev rule-dependent, applications should use libudev to locate hidraw
  28. devices attached to the system. There is a tutorial on libudev with a
  29. working example at::
  30. http://www.signal11.us/oss/udev/
  31. https://web.archive.org/web/2019*/www.signal11.us
  32. The HIDRAW API
  33. ---------------
  34. read()
  35. -------
  36. read() will read a queued report received from the HID device. On USB
  37. devices, the reports read using read() are the reports sent from the device
  38. on the INTERRUPT IN endpoint. By default, read() will block until there is
  39. a report available to be read. read() can be made non-blocking, by passing
  40. the O_NONBLOCK flag to open(), or by setting the O_NONBLOCK flag using
  41. fcntl().
  42. On a device which uses numbered reports, the first byte of the returned data
  43. will be the report number; the report data follows, beginning in the second
  44. byte. For devices which do not use numbered reports, the report data
  45. will begin at the first byte.
  46. write()
  47. -------
  48. The write() function will write a report to the device. For USB devices, if
  49. the device has an INTERRUPT OUT endpoint, the report will be sent on that
  50. endpoint. If it does not, the report will be sent over the control endpoint,
  51. using a SET_REPORT transfer.
  52. The first byte of the buffer passed to write() should be set to the report
  53. number. If the device does not use numbered reports, the first byte should
  54. be set to 0. The report data itself should begin at the second byte.
  55. ioctl()
  56. -------
  57. Hidraw supports the following ioctls:
  58. HIDIOCGRDESCSIZE:
  59. Get Report Descriptor Size
  60. This ioctl will get the size of the device's report descriptor.
  61. HIDIOCGRDESC:
  62. Get Report Descriptor
  63. This ioctl returns the device's report descriptor using a
  64. hidraw_report_descriptor struct. Make sure to set the size field of the
  65. hidraw_report_descriptor struct to the size returned from HIDIOCGRDESCSIZE.
  66. HIDIOCGRAWINFO:
  67. Get Raw Info
  68. This ioctl will return a hidraw_devinfo struct containing the bus type, the
  69. vendor ID (VID), and product ID (PID) of the device. The bus type can be one
  70. of::
  71. - BUS_USB
  72. - BUS_HIL
  73. - BUS_BLUETOOTH
  74. - BUS_VIRTUAL
  75. which are defined in uapi/linux/input.h.
  76. HIDIOCGRAWNAME(len):
  77. Get Raw Name
  78. This ioctl returns a string containing the vendor and product strings of
  79. the device. The returned string is Unicode, UTF-8 encoded.
  80. HIDIOCGRAWPHYS(len):
  81. Get Physical Address
  82. This ioctl returns a string representing the physical address of the device.
  83. For USB devices, the string contains the physical path to the device (the
  84. USB controller, hubs, ports, etc). For Bluetooth devices, the string
  85. contains the hardware (MAC) address of the device.
  86. HIDIOCSFEATURE(len):
  87. Send a Feature Report
  88. This ioctl will send a feature report to the device. Per the HID
  89. specification, feature reports are always sent using the control endpoint.
  90. Set the first byte of the supplied buffer to the report number. For devices
  91. which do not use numbered reports, set the first byte to 0. The report data
  92. begins in the second byte. Make sure to set len accordingly, to one more
  93. than the length of the report (to account for the report number).
  94. HIDIOCGFEATURE(len):
  95. Get a Feature Report
  96. This ioctl will request a feature report from the device using the control
  97. endpoint. The first byte of the supplied buffer should be set to the report
  98. number of the requested report. For devices which do not use numbered
  99. reports, set the first byte to 0. The returned report buffer will contain the
  100. report number in the first byte, followed by the report data read from the
  101. device. For devices which do not use numbered reports, the report data will
  102. begin at the first byte of the returned buffer.
  103. HIDIOCSINPUT(len):
  104. Send an Input Report
  105. This ioctl will send an input report to the device, using the control endpoint.
  106. In most cases, setting an input HID report on a device is meaningless and has
  107. no effect, but some devices may choose to use this to set or reset an initial
  108. state of a report. The format of the buffer issued with this report is identical
  109. to that of HIDIOCSFEATURE.
  110. HIDIOCGINPUT(len):
  111. Get an Input Report
  112. This ioctl will request an input report from the device using the control
  113. endpoint. This is slower on most devices where a dedicated In endpoint exists
  114. for regular input reports, but allows the host to request the value of a
  115. specific report number. Typically, this is used to request the initial states of
  116. an input report of a device, before an application listens for normal reports via
  117. the regular device read() interface. The format of the buffer issued with this report
  118. is identical to that of HIDIOCGFEATURE.
  119. HIDIOCSOUTPUT(len):
  120. Send an Output Report
  121. This ioctl will send an output report to the device, using the control endpoint.
  122. This is slower on most devices where a dedicated Out endpoint exists for regular
  123. output reports, but is added for completeness. Typically, this is used to set
  124. the initial states of an output report of a device, before an application sends
  125. updates via the regular device write() interface. The format of the buffer issued
  126. with this report is identical to that of HIDIOCSFEATURE.
  127. HIDIOCGOUTPUT(len):
  128. Get an Output Report
  129. This ioctl will request an output report from the device using the control
  130. endpoint. Typically, this is used to retrive the initial state of
  131. an output report of a device, before an application updates it as necessary either
  132. via a HIDIOCSOUTPUT request, or the regular device write() interface. The format
  133. of the buffer issued with this report is identical to that of HIDIOCGFEATURE.
  134. Example
  135. -------
  136. In samples/, find hid-example.c, which shows examples of read(), write(),
  137. and all the ioctls for hidraw. The code may be used by anyone for any
  138. purpose, and can serve as a starting point for developing applications using
  139. hidraw.
  140. Document by:
  141. Alan Ott <[email protected]>, Signal 11 Software