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