hidraw.txt 5.3 KB

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