uhid.rst 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ======================================================
  2. UHID - User-space I/O driver support for HID subsystem
  3. ======================================================
  4. UHID allows user-space to implement HID transport drivers. Please see
  5. hid-transport.rst for an introduction into HID transport drivers. This document
  6. relies heavily on the definitions declared there.
  7. With UHID, a user-space transport driver can create kernel hid-devices for each
  8. device connected to the user-space controlled bus. The UHID API defines the I/O
  9. events provided from the kernel to user-space and vice versa.
  10. There is an example user-space application in ./samples/uhid/uhid-example.c
  11. The UHID API
  12. ------------
  13. UHID is accessed through a character misc-device. The minor number is allocated
  14. dynamically so you need to rely on udev (or similar) to create the device node.
  15. This is /dev/uhid by default.
  16. If a new device is detected by your HID I/O Driver and you want to register this
  17. device with the HID subsystem, then you need to open /dev/uhid once for each
  18. device you want to register. All further communication is done by read()'ing or
  19. write()'ing "struct uhid_event" objects. Non-blocking operations are supported
  20. by setting O_NONBLOCK::
  21. struct uhid_event {
  22. __u32 type;
  23. union {
  24. struct uhid_create2_req create2;
  25. struct uhid_output_req output;
  26. struct uhid_input2_req input2;
  27. ...
  28. } u;
  29. };
  30. The "type" field contains the ID of the event. Depending on the ID different
  31. payloads are sent. You must not split a single event across multiple read()'s or
  32. multiple write()'s. A single event must always be sent as a whole. Furthermore,
  33. only a single event can be sent per read() or write(). Pending data is ignored.
  34. If you want to handle multiple events in a single syscall, then use vectored
  35. I/O with readv()/writev().
  36. The "type" field defines the payload. For each type, there is a
  37. payload-structure available in the union "u" (except for empty payloads). This
  38. payload contains management and/or device data.
  39. The first thing you should do is send a UHID_CREATE2 event. This will
  40. register the device. UHID will respond with a UHID_START event. You can now
  41. start sending data to and reading data from UHID. However, unless UHID sends the
  42. UHID_OPEN event, the internally attached HID Device Driver has no user attached.
  43. That is, you might put your device asleep unless you receive the UHID_OPEN
  44. event. If you receive the UHID_OPEN event, you should start I/O. If the last
  45. user closes the HID device, you will receive a UHID_CLOSE event. This may be
  46. followed by a UHID_OPEN event again and so on. There is no need to perform
  47. reference-counting in user-space. That is, you will never receive multiple
  48. UHID_OPEN events without a UHID_CLOSE event. The HID subsystem performs
  49. ref-counting for you.
  50. You may decide to ignore UHID_OPEN/UHID_CLOSE, though. I/O is allowed even
  51. though the device may have no users.
  52. If you want to send data on the interrupt channel to the HID subsystem, you send
  53. a HID_INPUT2 event with your raw data payload. If the kernel wants to send data
  54. on the interrupt channel to the device, you will read a UHID_OUTPUT event.
  55. Data requests on the control channel are currently limited to GET_REPORT and
  56. SET_REPORT (no other data reports on the control channel are defined so far).
  57. Those requests are always synchronous. That means, the kernel sends
  58. UHID_GET_REPORT and UHID_SET_REPORT events and requires you to forward them to
  59. the device on the control channel. Once the device responds, you must forward
  60. the response via UHID_GET_REPORT_REPLY and UHID_SET_REPORT_REPLY to the kernel.
  61. The kernel blocks internal driver-execution during such round-trips (times out
  62. after a hard-coded period).
  63. If your device disconnects, you should send a UHID_DESTROY event. This will
  64. unregister the device. You can now send UHID_CREATE2 again to register a new
  65. device.
  66. If you close() the fd, the device is automatically unregistered and destroyed
  67. internally.
  68. write()
  69. -------
  70. write() allows you to modify the state of the device and feed input data into
  71. the kernel. 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. O_NONBLOCK does not affect write() as
  75. writes are always handled immediately in a non-blocking fashion. Future requests
  76. might make use of O_NONBLOCK, though.
  77. UHID_CREATE2:
  78. This creates the internal HID device. No I/O is possible until you send this
  79. event to the kernel. The payload is of type struct uhid_create2_req and
  80. contains information about your device. You can start I/O now.
  81. UHID_DESTROY:
  82. This destroys the internal HID device. No further I/O will be accepted. There
  83. may still be pending messages that you can receive with read() but no further
  84. UHID_INPUT events can be sent to the kernel.
  85. You can create a new device by sending UHID_CREATE2 again. There is no need to
  86. reopen the character device.
  87. UHID_INPUT2:
  88. You must send UHID_CREATE2 before sending input to the kernel! This event
  89. contains a data-payload. This is the raw data that you read from your device
  90. on the interrupt channel. The kernel will parse the HID reports.
  91. UHID_GET_REPORT_REPLY:
  92. If you receive a UHID_GET_REPORT request you must answer with this request.
  93. You must copy the "id" field from the request into the answer. Set the "err"
  94. field to 0 if no error occurred or to EIO if an I/O error occurred.
  95. If "err" is 0 then you should fill the buffer of the answer with the results
  96. of the GET_REPORT request and set "size" correspondingly.
  97. UHID_SET_REPORT_REPLY:
  98. This is the SET_REPORT equivalent of UHID_GET_REPORT_REPLY. Unlike GET_REPORT,
  99. SET_REPORT never returns a data buffer, therefore, it's sufficient to set the
  100. "id" and "err" fields correctly.
  101. read()
  102. ------
  103. read() will return a queued output report. No reaction is required to any of
  104. them but you should handle them according to your needs.
  105. UHID_START:
  106. This is sent when the HID device is started. Consider this as an answer to
  107. UHID_CREATE2. This is always the first event that is sent. Note that this
  108. event might not be available immediately after write(UHID_CREATE2) returns.
  109. Device drivers might require delayed setups.
  110. This event contains a payload of type uhid_start_req. The "dev_flags" field
  111. describes special behaviors of a device. The following flags are defined:
  112. - UHID_DEV_NUMBERED_FEATURE_REPORTS
  113. - UHID_DEV_NUMBERED_OUTPUT_REPORTS
  114. - UHID_DEV_NUMBERED_INPUT_REPORTS
  115. Each of these flags defines whether a given report-type uses numbered
  116. reports. If numbered reports are used for a type, all messages from
  117. the kernel already have the report-number as prefix. Otherwise, no
  118. prefix is added by the kernel.
  119. For messages sent by user-space to the kernel, you must adjust the
  120. prefixes according to these flags.
  121. UHID_STOP:
  122. This is sent when the HID device is stopped. Consider this as an answer to
  123. UHID_DESTROY.
  124. If you didn't destroy your device via UHID_DESTROY, but the kernel sends an
  125. UHID_STOP event, this should usually be ignored. It means that the kernel
  126. reloaded/changed the device driver loaded on your HID device (or some other
  127. maintenance actions happened).
  128. You can usually ignore any UHID_STOP events safely.
  129. UHID_OPEN:
  130. This is sent when the HID device is opened. That is, the data that the HID
  131. device provides is read by some other process. You may ignore this event but
  132. it is useful for power-management. As long as you haven't received this event
  133. there is actually no other process that reads your data so there is no need to
  134. send UHID_INPUT2 events to the kernel.
  135. UHID_CLOSE:
  136. This is sent when there are no more processes which read the HID data. It is
  137. the counterpart of UHID_OPEN and you may as well ignore this event.
  138. UHID_OUTPUT:
  139. This is sent if the HID device driver wants to send raw data to the I/O
  140. device on the interrupt channel. You should read the payload and forward it to
  141. the device. The payload is of type "struct uhid_output_req".
  142. This may be received even though you haven't received UHID_OPEN yet.
  143. UHID_GET_REPORT:
  144. This event is sent if the kernel driver wants to perform a GET_REPORT request
  145. on the control channel as described in the HID specs. The report-type and
  146. report-number are available in the payload.
  147. The kernel serializes GET_REPORT requests so there will never be two in
  148. parallel. However, if you fail to respond with a UHID_GET_REPORT_REPLY, the
  149. request might silently time out.
  150. Once you read a GET_REPORT request, you shall forward it to the HID device and
  151. remember the "id" field in the payload. Once your HID device responds to the
  152. GET_REPORT (or if it fails), you must send a UHID_GET_REPORT_REPLY to the
  153. kernel with the exact same "id" as in the request. If the request already
  154. timed out, the kernel will ignore the response silently. The "id" field is
  155. never re-used, so conflicts cannot happen.
  156. UHID_SET_REPORT:
  157. This is the SET_REPORT equivalent of UHID_GET_REPORT. On receipt, you shall
  158. send a SET_REPORT request to your HID device. Once it replies, you must tell
  159. the kernel about it via UHID_SET_REPORT_REPLY.
  160. The same restrictions as for UHID_GET_REPORT apply.
  161. ----------------------------------------------------
  162. Written 2012, David Herrmann <[email protected]>