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