hid-transport.rst 16 KB

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