uhid.txt 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. UHID - User-space I/O driver support for HID subsystem
  2. ========================================================
  3. The HID subsystem needs two kinds of drivers. In this document we call them:
  4. 1. The "HID I/O Driver" is the driver that performs raw data I/O to the
  5. low-level device. Internally, they register an hid_ll_driver structure with
  6. the HID core. They perform device setup, read raw data from the device and
  7. push it into the HID subsystem and they provide a callback so the HID
  8. subsystem can send data to the device.
  9. 2. The "HID Device Driver" is the driver that parses HID reports and reacts on
  10. them. There are generic drivers like "generic-usb" and "generic-bluetooth"
  11. which adhere to the HID specification and provide the standardizes features.
  12. But there may be special drivers and quirks for each non-standard device out
  13. there. Internally, they use the hid_driver structure.
  14. Historically, the USB stack was the first subsystem to provide an HID I/O
  15. Driver. However, other standards like Bluetooth have adopted the HID specs and
  16. may provide HID I/O Drivers, too. The UHID driver allows to implement HID I/O
  17. Drivers in user-space and feed the data into the kernel HID-subsystem.
  18. This allows user-space to operate on the same level as USB-HID, Bluetooth-HID
  19. and similar. It does not provide a way to write HID Device Drivers, though. Use
  20. hidraw for this purpose.
  21. There is an example user-space application in ./samples/uhid/uhid-example.c
  22. The UHID API
  23. ------------
  24. UHID is accessed through a character misc-device. The minor-number is allocated
  25. dynamically so you need to rely on udev (or similar) to create the device node.
  26. This is /dev/uhid by default.
  27. If a new device is detected by your HID I/O Driver and you want to register this
  28. device with the HID subsystem, then you need to open /dev/uhid once for each
  29. device you want to register. All further communication is done by read()'ing or
  30. write()'ing "struct uhid_event" objects. Non-blocking operations are supported
  31. by setting O_NONBLOCK.
  32. struct uhid_event {
  33. __u32 type;
  34. union {
  35. struct uhid_create_req create;
  36. struct uhid_data_req data;
  37. ...
  38. } u;
  39. };
  40. The "type" field contains the ID of the event. Depending on the ID different
  41. payloads are sent. You must not split a single event across multiple read()'s or
  42. multiple write()'s. A single event must always be sent as a whole. Furthermore,
  43. only a single event can be sent per read() or write(). Pending data is ignored.
  44. If you want to handle multiple events in a single syscall, then use vectored
  45. I/O with readv()/writev().
  46. The first thing you should do is sending an UHID_CREATE event. This will
  47. register the device. UHID will respond with an UHID_START event. You can now
  48. start sending data to and reading data from UHID. However, unless UHID sends the
  49. UHID_OPEN event, the internally attached HID Device Driver has no user attached.
  50. That is, you might put your device asleep unless you receive the UHID_OPEN
  51. event. If you receive the UHID_OPEN event, you should start I/O. If the last
  52. user closes the HID device, you will receive an UHID_CLOSE event. This may be
  53. followed by an UHID_OPEN event again and so on. There is no need to perform
  54. reference-counting in user-space. That is, you will never receive multiple
  55. UHID_OPEN events without an UHID_CLOSE event. The HID subsystem performs
  56. ref-counting for you.
  57. You may decide to ignore UHID_OPEN/UHID_CLOSE, though. I/O is allowed even
  58. though the device may have no users.
  59. If you want to send data to the HID subsystem, you send an HID_INPUT event with
  60. your raw data payload. If the kernel wants to send data to the device, you will
  61. read an UHID_OUTPUT or UHID_OUTPUT_EV event.
  62. If your device disconnects, you should send an UHID_DESTROY event. This will
  63. unregister the device. You can now send UHID_CREATE again to register a new
  64. device.
  65. If you close() the fd, the device is automatically unregistered and destroyed
  66. internally.
  67. write()
  68. -------
  69. write() allows you to modify the state of the device and feed input data into
  70. the kernel. The following types are supported: UHID_CREATE, UHID_DESTROY and
  71. UHID_INPUT. The kernel will parse the event immediately and if the event ID is
  72. not supported, it will return -EOPNOTSUPP. If the payload is invalid, then
  73. -EINVAL is returned, otherwise, the amount of data that was read is returned and
  74. the request was handled successfully.
  75. UHID_CREATE:
  76. This creates the internal HID device. No I/O is possible until you send this
  77. event to the kernel. The payload is of type struct uhid_create_req and
  78. contains information about your device. You can start I/O now.
  79. UHID_DESTROY:
  80. This destroys the internal HID device. No further I/O will be accepted. There
  81. may still be pending messages that you can receive with read() but no further
  82. UHID_INPUT events can be sent to the kernel.
  83. You can create a new device by sending UHID_CREATE again. There is no need to
  84. reopen the character device.
  85. UHID_INPUT:
  86. You must send UHID_CREATE before sending input to the kernel! This event
  87. contains a data-payload. This is the raw data that you read from your device.
  88. The kernel will parse the HID reports and react on it.
  89. UHID_FEATURE_ANSWER:
  90. If you receive a UHID_FEATURE request you must answer with this request. You
  91. must copy the "id" field from the request into the answer. Set the "err" field
  92. to 0 if no error occurred or to EIO if an I/O error occurred.
  93. If "err" is 0 then you should fill the buffer of the answer with the results
  94. of the feature request and set "size" correspondingly.
  95. read()
  96. ------
  97. read() will return a queued ouput report. These output reports can be of type
  98. UHID_START, UHID_STOP, UHID_OPEN, UHID_CLOSE, UHID_OUTPUT or UHID_OUTPUT_EV. No
  99. reaction is required to any of them but you should handle them according to your
  100. needs. Only UHID_OUTPUT and UHID_OUTPUT_EV have payloads.
  101. UHID_START:
  102. This is sent when the HID device is started. Consider this as an answer to
  103. UHID_CREATE. This is always the first event that is sent.
  104. UHID_STOP:
  105. This is sent when the HID device is stopped. Consider this as an answer to
  106. UHID_DESTROY.
  107. If the kernel HID device driver closes the device manually (that is, you
  108. didn't send UHID_DESTROY) then you should consider this device closed and send
  109. an UHID_DESTROY event. You may want to reregister your device, though. This is
  110. always the last message that is sent to you unless you reopen the device with
  111. UHID_CREATE.
  112. UHID_OPEN:
  113. This is sent when the HID device is opened. That is, the data that the HID
  114. device provides is read by some other process. You may ignore this event but
  115. it is useful for power-management. As long as you haven't received this event
  116. there is actually no other process that reads your data so there is no need to
  117. send UHID_INPUT events to the kernel.
  118. UHID_CLOSE:
  119. This is sent when there are no more processes which read the HID data. It is
  120. the counterpart of UHID_OPEN and you may as well ignore this event.
  121. UHID_OUTPUT:
  122. This is sent if the HID device driver wants to send raw data to the I/O
  123. device. You should read the payload and forward it to the device. The payload
  124. is of type "struct uhid_data_req".
  125. This may be received even though you haven't received UHID_OPEN, yet.
  126. UHID_OUTPUT_EV:
  127. Same as UHID_OUTPUT but this contains a "struct input_event" as payload. This
  128. is called for force-feedback, LED or similar events which are received through
  129. an input device by the HID subsystem. You should convert this into raw reports
  130. and send them to your device similar to events of type UHID_OUTPUT.
  131. UHID_FEATURE:
  132. This event is sent if the kernel driver wants to perform a feature request as
  133. described in the HID specs. The report-type and report-number are available in
  134. the payload.
  135. The kernel serializes feature requests so there will never be two in parallel.
  136. However, if you fail to respond with a UHID_FEATURE_ANSWER in a time-span of 5
  137. seconds, then the requests will be dropped and a new one might be sent.
  138. Therefore, the payload also contains an "id" field that identifies every
  139. request.
  140. Document by:
  141. David Herrmann <dh.herrmann@googlemail.com>