hid-transport.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. HID I/O Transport Drivers
  2. ===========================
  3. The HID subsystem is independent of the underlying transport driver. Initially,
  4. only USB was supported, but other specifications adopted the HID design and
  5. provided new transport drivers. The kernel includes at least support for USB,
  6. Bluetooth, I2C and user-space I/O drivers.
  7. 1) HID Bus
  8. ==========
  9. The HID subsystem is designed as a bus. Any I/O subsystem may provide HID
  10. devices and register them with the HID bus. HID core then loads generic device
  11. drivers on top of it. The transport drivers are responsible of raw data
  12. transport and device setup/management. HID core is responsible of
  13. report-parsing, report interpretation and the user-space API. Device specifics
  14. and quirks are handled by all layers depending on the quirk.
  15. +-----------+ +-----------+ +-----------+ +-----------+
  16. | Device #1 | | Device #i | | Device #j | | Device #k |
  17. +-----------+ +-----------+ +-----------+ +-----------+
  18. \\ // \\ //
  19. +------------+ +------------+
  20. | I/O Driver | | I/O Driver |
  21. +------------+ +------------+
  22. || ||
  23. +------------------+ +------------------+
  24. | Transport Driver | | Transport Driver |
  25. +------------------+ +------------------+
  26. \___ ___/
  27. \ /
  28. +----------------+
  29. | HID Core |
  30. +----------------+
  31. / | | \
  32. / | | \
  33. ____________/ | | \_________________
  34. / | | \
  35. / | | \
  36. +----------------+ +-----------+ +------------------+ +------------------+
  37. | Generic Driver | | MT Driver | | Custom Driver #1 | | Custom Driver #2 |
  38. +----------------+ +-----------+ +------------------+ +------------------+
  39. Example Drivers:
  40. I/O: USB, I2C, Bluetooth-l2cap
  41. Transport: USB-HID, I2C-HID, BT-HIDP
  42. Everything below "HID Core" is simplified in this graph as it is only of
  43. interest to HID device drivers. Transport drivers do not need to know the
  44. specifics.
  45. 1.1) Device Setup
  46. -----------------
  47. I/O drivers normally provide hotplug detection or device enumeration APIs to the
  48. transport drivers. Transport drivers use this to find any suitable HID device.
  49. They allocate HID device objects and register them with HID core. Transport
  50. drivers are not required to register themselves with HID core. HID core is never
  51. aware of which transport drivers are available and is not interested in it. It
  52. is only interested in devices.
  53. Transport drivers attach a constant "struct hid_ll_driver" object with each
  54. device. Once a device is registered with HID core, the callbacks provided via
  55. this struct are used by HID core to communicate with the device.
  56. Transport drivers are responsible of detecting device failures and unplugging.
  57. HID core will operate a device as long as it is registered regardless of any
  58. device failures. Once transport drivers detect unplug or failure events, they
  59. must unregister the device from HID core and HID core will stop using the
  60. provided callbacks.
  61. 1.2) Transport Driver Requirements
  62. ----------------------------------
  63. The terms "asynchronous" and "synchronous" in this document describe the
  64. transmission behavior regarding acknowledgements. An asynchronous channel must
  65. not perform any synchronous operations like waiting for acknowledgements or
  66. verifications. Generally, HID calls operating on asynchronous channels must be
  67. running in atomic-context just fine.
  68. On the other hand, synchronous channels can be implemented by the transport
  69. driver in whatever way they like. They might just be the same as asynchronous
  70. channels, but they can also provide acknowledgement reports, automatic
  71. retransmission on failure, etc. in a blocking manner. If such functionality is
  72. required on asynchronous channels, a transport-driver must implement that via
  73. its own worker threads.
  74. HID core requires transport drivers to follow a given design. A Transport
  75. driver must provide two bi-directional I/O channels to each HID device. These
  76. channels must not necessarily be bi-directional in the hardware itself. A
  77. transport driver might just provide 4 uni-directional channels. Or it might
  78. multiplex all four on a single physical channel. However, in this document we
  79. will describe them as two bi-directional channels as they have several
  80. properties in common.
  81. - Interrupt Channel (intr): The intr channel is used for asynchronous data
  82. reports. No management commands or data acknowledgements are sent on this
  83. channel. Any unrequested incoming or outgoing data report must be sent on
  84. this channel and is never acknowledged by the remote side. Devices usually
  85. send their input events on this channel. Outgoing events are normally
  86. not send via intr, except if high throughput is required.
  87. - Control Channel (ctrl): The ctrl channel is used for synchronous requests and
  88. device management. Unrequested data input events must not be sent on this
  89. channel and are normally ignored. Instead, devices only send management
  90. events or answers to host requests on this channel.
  91. The control-channel is used for direct blocking queries to the device
  92. independent of any events on the intr-channel.
  93. Outgoing reports are usually sent on the ctrl channel via synchronous
  94. SET_REPORT requests.
  95. Communication between devices and HID core is mostly done via HID reports. A
  96. report can be of one of three types:
  97. - INPUT Report: Input reports provide data from device to host. This
  98. data may include button events, axis events, battery status or more. This
  99. data is generated by the device and sent to the host with or without
  100. requiring explicit requests. Devices can choose to send data continuously or
  101. only on change.
  102. - OUTPUT Report: Output reports change device states. They are sent from host
  103. to device and may include LED requests, rumble requests or more. Output
  104. reports are never sent from device to host, but a host can retrieve their
  105. current state.
  106. Hosts may choose to send output reports either continuously or only on
  107. change.
  108. - FEATURE Report: Feature reports are used for specific static device features
  109. and never reported spontaneously. A host can read and/or write them to access
  110. data like battery-state or device-settings.
  111. Feature reports are never sent without requests. A host must explicitly set
  112. or retrieve a feature report. This also means, feature reports are never sent
  113. on the intr channel as this channel is asynchronous.
  114. INPUT and OUTPUT reports can be sent as pure data reports on the intr channel.
  115. For INPUT reports this is the usual operational mode. But for OUTPUT reports,
  116. this is rarely done as OUTPUT reports are normally quite scarce. But devices are
  117. free to make excessive use of asynchronous OUTPUT reports (for instance, custom
  118. HID audio speakers make great use of it).
  119. Plain reports must not be sent on the ctrl channel, though. Instead, the ctrl
  120. channel provides synchronous GET/SET_REPORT requests. Plain reports are only
  121. allowed on the intr channel and are the only means of data there.
  122. - GET_REPORT: A GET_REPORT request has a report ID as payload and is sent
  123. from host to device. The device must answer with a data report for the
  124. requested report ID on the ctrl channel as a synchronous acknowledgement.
  125. Only one GET_REPORT request can be pending for each device. This restriction
  126. is enforced by HID core as several transport drivers don't allow multiple
  127. simultaneous GET_REPORT requests.
  128. Note that data reports which are sent as answer to a GET_REPORT request are
  129. not handled as generic device events. That is, if a device does not operate
  130. in continuous data reporting mode, an answer to GET_REPORT does not replace
  131. the raw data report on the intr channel on state change.
  132. GET_REPORT is only used by custom HID device drivers to query device state.
  133. Normally, HID core caches any device state so this request is not necessary
  134. on devices that follow the HID specs except during device initialization to
  135. retrieve the current state.
  136. GET_REPORT requests can be sent for any of the 3 report types and shall
  137. return the current report state of the device. However, OUTPUT reports as
  138. payload may be blocked by the underlying transport driver if the
  139. specification does not allow them.
  140. - SET_REPORT: A SET_REPORT request has a report ID plus data as payload. It is
  141. sent from host to device and a device must update it's current report state
  142. according to the given data. Any of the 3 report types can be used. However,
  143. INPUT reports as payload might be blocked by the underlying transport driver
  144. if the specification does not allow them.
  145. A device must answer with a synchronous acknowledgement. However, HID core
  146. does not require transport drivers to forward this acknowledgement to HID
  147. core.
  148. Same as for GET_REPORT, only one SET_REPORT can be pending at a time. This
  149. restriction is enforced by HID core as some transport drivers do not support
  150. multiple synchronous SET_REPORT requests.
  151. Other ctrl-channel requests are supported by USB-HID but are not available
  152. (or deprecated) in most other transport level specifications:
  153. - GET/SET_IDLE: Only used by USB-HID and I2C-HID.
  154. - GET/SET_PROTOCOL: Not used by HID core.
  155. - RESET: Used by I2C-HID, not hooked up in HID core.
  156. - SET_POWER: Used by I2C-HID, not hooked up in HID core.
  157. 2) HID API
  158. ==========
  159. 2.1) Initialization
  160. -------------------
  161. Transport drivers normally use the following procedure to register a new device
  162. with HID core:
  163. struct hid_device *hid;
  164. int ret;
  165. hid = hid_allocate_device();
  166. if (IS_ERR(hid)) {
  167. ret = PTR_ERR(hid);
  168. goto err_<...>;
  169. }
  170. strlcpy(hid->name, <device-name-src>, 127);
  171. strlcpy(hid->phys, <device-phys-src>, 63);
  172. strlcpy(hid->uniq, <device-uniq-src>, 63);
  173. hid->ll_driver = &custom_ll_driver;
  174. hid->bus = <device-bus>;
  175. hid->vendor = <device-vendor>;
  176. hid->product = <device-product>;
  177. hid->version = <device-version>;
  178. hid->country = <device-country>;
  179. hid->dev.parent = <pointer-to-parent-device>;
  180. hid->driver_data = <transport-driver-data-field>;
  181. ret = hid_add_device(hid);
  182. if (ret)
  183. goto err_<...>;
  184. Once hid_add_device() is entered, HID core might use the callbacks provided in
  185. "custom_ll_driver". Note that fields like "country" can be ignored by underlying
  186. transport-drivers if not supported.
  187. To unregister a device, use:
  188. hid_destroy_device(hid);
  189. Once hid_destroy_device() returns, HID core will no longer make use of any
  190. driver callbacks.
  191. 2.2) hid_ll_driver operations
  192. -----------------------------
  193. The available HID callbacks are:
  194. - int (*start) (struct hid_device *hdev)
  195. Called from HID device drivers once they want to use the device. Transport
  196. drivers can choose to setup their device in this callback. However, normally
  197. devices are already set up before transport drivers register them to HID core
  198. so this is mostly only used by USB-HID.
  199. - void (*stop) (struct hid_device *hdev)
  200. Called from HID device drivers once they are done with a device. Transport
  201. drivers can free any buffers and deinitialize the device. But note that
  202. ->start() might be called again if another HID device driver is loaded on the
  203. device.
  204. Transport drivers are free to ignore it and deinitialize devices after they
  205. destroyed them via hid_destroy_device().
  206. - int (*open) (struct hid_device *hdev)
  207. Called from HID device drivers once they are interested in data reports.
  208. Usually, while user-space didn't open any input API/etc., device drivers are
  209. not interested in device data and transport drivers can put devices asleep.
  210. However, once ->open() is called, transport drivers must be ready for I/O.
  211. ->open() calls are nested for each client that opens the HID device.
  212. - void (*close) (struct hid_device *hdev)
  213. Called from HID device drivers after ->open() was called but they are no
  214. longer interested in device reports. (Usually if user-space closed any input
  215. devices of the driver).
  216. Transport drivers can put devices asleep and terminate any I/O of all
  217. ->open() calls have been followed by a ->close() call. However, ->start() may
  218. be called again if the device driver is interested in input reports again.
  219. - int (*parse) (struct hid_device *hdev)
  220. Called once during device setup after ->start() has been called. Transport
  221. drivers must read the HID report-descriptor from the device and tell HID core
  222. about it via hid_parse_report().
  223. - int (*power) (struct hid_device *hdev, int level)
  224. Called by HID core to give PM hints to transport drivers. Usually this is
  225. analogical to the ->open() and ->close() hints and redundant.
  226. - void (*request) (struct hid_device *hdev, struct hid_report *report,
  227. int reqtype)
  228. Send an HID request on the ctrl channel. "report" contains the report that
  229. should be sent and "reqtype" the request type. Request-type can be
  230. HID_REQ_SET_REPORT or HID_REQ_GET_REPORT.
  231. This callback is optional. If not provided, HID core will assemble a raw
  232. report following the HID specs and send it via the ->raw_request() callback.
  233. The transport driver is free to implement this asynchronously.
  234. - int (*wait) (struct hid_device *hdev)
  235. Used by HID core before calling ->request() again. A transport driver can use
  236. it to wait for any pending requests to complete if only one request is
  237. allowed at a time.
  238. - int (*raw_request) (struct hid_device *hdev, unsigned char reportnum,
  239. __u8 *buf, size_t count, unsigned char rtype,
  240. int reqtype)
  241. Same as ->request() but provides the report as raw buffer. This request shall
  242. be synchronous. A transport driver must not use ->wait() to complete such
  243. requests. This request is mandatory and hid core will reject the device if
  244. it is missing.
  245. - int (*output_report) (struct hid_device *hdev, __u8 *buf, size_t len)
  246. Send raw output report via intr channel. Used by some HID device drivers
  247. which require high throughput for outgoing requests on the intr channel. This
  248. must not cause SET_REPORT calls! This must be implemented as asynchronous
  249. output report on the intr channel!
  250. - int (*idle) (struct hid_device *hdev, int report, int idle, int reqtype)
  251. Perform SET/GET_IDLE request. Only used by USB-HID, do not implement!
  252. 2.3) Data Path
  253. --------------
  254. Transport drivers are responsible of reading data from I/O devices. They must
  255. handle any I/O-related state-tracking themselves. HID core does not implement
  256. protocol handshakes or other management commands which can be required by the
  257. given HID transport specification.
  258. Every raw data packet read from a device must be fed into HID core via
  259. hid_input_report(). You must specify the channel-type (intr or ctrl) and report
  260. type (input/output/feature). Under normal conditions, only input reports are
  261. provided via this API.
  262. Responses to GET_REPORT requests via ->request() must also be provided via this
  263. API. Responses to ->raw_request() are synchronous and must be intercepted by the
  264. transport driver and not passed to hid_input_report().
  265. Acknowledgements to SET_REPORT requests are not of interest to HID core.
  266. ----------------------------------------------------
  267. Written 2013, David Herrmann <dh.herrmann@gmail.com>