Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

HID: uhid: update documentation

Remove legacy bits, refer people to hid-transport.txt and add descriptions
for all new features.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>

authored by

David Herrmann and committed by
Jiri Kosina
76c7c491 c2b2f16c

+90 -85
+90 -85
Documentation/hid/uhid.txt
··· 1 1 UHID - User-space I/O driver support for HID subsystem 2 2 ======================================================== 3 3 4 - The HID subsystem needs two kinds of drivers. In this document we call them: 4 + UHID allows user-space to implement HID transport drivers. Please see 5 + hid-transport.txt for an introduction into HID transport drivers. This document 6 + relies heavily on the definitions declared there. 5 7 6 - 1. The "HID I/O Driver" is the driver that performs raw data I/O to the 7 - low-level device. Internally, they register an hid_ll_driver structure with 8 - the HID core. They perform device setup, read raw data from the device and 9 - push it into the HID subsystem and they provide a callback so the HID 10 - subsystem can send data to the device. 11 - 12 - 2. The "HID Device Driver" is the driver that parses HID reports and reacts on 13 - them. There are generic drivers like "generic-usb" and "generic-bluetooth" 14 - which adhere to the HID specification and provide the standardizes features. 15 - But there may be special drivers and quirks for each non-standard device out 16 - there. Internally, they use the hid_driver structure. 17 - 18 - Historically, the USB stack was the first subsystem to provide an HID I/O 19 - Driver. However, other standards like Bluetooth have adopted the HID specs and 20 - may provide HID I/O Drivers, too. The UHID driver allows to implement HID I/O 21 - Drivers in user-space and feed the data into the kernel HID-subsystem. 22 - 23 - This allows user-space to operate on the same level as USB-HID, Bluetooth-HID 24 - and similar. It does not provide a way to write HID Device Drivers, though. Use 25 - hidraw for this purpose. 8 + With UHID, a user-space transport driver can create kernel hid-devices for each 9 + device connected to the user-space controlled bus. The UHID API defines the I/O 10 + events provided from the kernel to user-space and vice versa. 26 11 27 12 There is an example user-space application in ./samples/uhid/uhid-example.c 28 13 ··· 27 42 struct uhid_event { 28 43 __u32 type; 29 44 union { 30 - struct uhid_create_req create; 31 - struct uhid_data_req data; 45 + struct uhid_create2_req create2; 46 + struct uhid_output_req output; 47 + struct uhid_input2_req input2; 32 48 ... 33 49 } u; 34 50 }; ··· 40 54 only a single event can be sent per read() or write(). Pending data is ignored. 41 55 If you want to handle multiple events in a single syscall, then use vectored 42 56 I/O with readv()/writev(). 57 + The "type" field defines the payload. For each type, there is a 58 + payload-structure available in the union "u" (except for empty payloads). This 59 + payload contains management and/or device data. 43 60 44 - The first thing you should do is sending an UHID_CREATE event. This will 61 + The first thing you should do is sending an UHID_CREATE2 event. This will 45 62 register the device. UHID will respond with an UHID_START event. You can now 46 63 start sending data to and reading data from UHID. However, unless UHID sends the 47 64 UHID_OPEN event, the internally attached HID Device Driver has no user attached. ··· 58 69 You may decide to ignore UHID_OPEN/UHID_CLOSE, though. I/O is allowed even 59 70 though the device may have no users. 60 71 61 - If you want to send data to the HID subsystem, you send an HID_INPUT event with 62 - your raw data payload. If the kernel wants to send data to the device, you will 63 - read an UHID_OUTPUT or UHID_OUTPUT_EV event. 72 + If you want to send data on the interrupt channel to the HID subsystem, you send 73 + an HID_INPUT2 event with your raw data payload. If the kernel wants to send data 74 + on the interrupt channel to the device, you will read an UHID_OUTPUT event. 75 + Data requests on the control channel are currently limited to GET_REPORT and 76 + SET_REPORT (no other data reports on the control channel are defined so far). 77 + Those requests are always synchronous. That means, the kernel sends 78 + UHID_GET_REPORT and UHID_SET_REPORT events and requires you to forward them to 79 + the device on the control channel. Once the device responds, you must forward 80 + the response via UHID_GET_REPORT_REPLY and UHID_SET_REPORT_REPLY to the kernel. 81 + The kernel blocks internal driver-execution during such round-trips (times out 82 + after a hard-coded period). 64 83 65 84 If your device disconnects, you should send an UHID_DESTROY event. This will 66 - unregister the device. You can now send UHID_CREATE again to register a new 85 + unregister the device. You can now send UHID_CREATE2 again to register a new 67 86 device. 68 87 If you close() the fd, the device is automatically unregistered and destroyed 69 88 internally. ··· 79 82 write() 80 83 ------- 81 84 write() allows you to modify the state of the device and feed input data into 82 - the kernel. The following types are supported: UHID_CREATE, UHID_DESTROY and 83 - UHID_INPUT. The kernel will parse the event immediately and if the event ID is 85 + the kernel. The kernel will parse the event immediately and if the event ID is 84 86 not supported, it will return -EOPNOTSUPP. If the payload is invalid, then 85 87 -EINVAL is returned, otherwise, the amount of data that was read is returned and 86 - the request was handled successfully. 87 - 88 - UHID_CREATE: 89 - This creates the internal HID device. No I/O is possible until you send this 90 - event to the kernel. The payload is of type struct uhid_create_req and 91 - contains information about your device. You can start I/O now. 88 + the request was handled successfully. O_NONBLOCK does not affect write() as 89 + writes are always handled immediately in a non-blocking fashion. Future requests 90 + might make use of O_NONBLOCK, though. 92 91 93 92 UHID_CREATE2: 94 - Same as UHID_CREATE, but the HID report descriptor data (rd_data) is an array 95 - inside struct uhid_create2_req, instead of a pointer to a separate array. 96 - Enables use from languages that don't support pointers, e.g. Python. 93 + This creates the internal HID device. No I/O is possible until you send this 94 + event to the kernel. The payload is of type struct uhid_create2_req and 95 + contains information about your device. You can start I/O now. 97 96 98 97 UHID_DESTROY: 99 98 This destroys the internal HID device. No further I/O will be accepted. There 100 99 may still be pending messages that you can receive with read() but no further 101 100 UHID_INPUT events can be sent to the kernel. 102 - You can create a new device by sending UHID_CREATE again. There is no need to 101 + You can create a new device by sending UHID_CREATE2 again. There is no need to 103 102 reopen the character device. 104 103 105 - UHID_INPUT: 106 - You must send UHID_CREATE before sending input to the kernel! This event 107 - contains a data-payload. This is the raw data that you read from your device. 108 - The kernel will parse the HID reports and react on it. 109 - 110 104 UHID_INPUT2: 111 - Same as UHID_INPUT, but the data array is the last field of uhid_input2_req. 112 - Enables userspace to write only the required bytes to kernel (ev.type + 113 - ev.u.input2.size + the part of the data array that matters), instead of 114 - the entire struct uhid_input2_req. 105 + You must send UHID_CREATE2 before sending input to the kernel! This event 106 + contains a data-payload. This is the raw data that you read from your device 107 + on the interrupt channel. The kernel will parse the HID reports. 115 108 116 - UHID_FEATURE_ANSWER: 117 - If you receive a UHID_FEATURE request you must answer with this request. You 118 - must copy the "id" field from the request into the answer. Set the "err" field 119 - to 0 if no error occurred or to EIO if an I/O error occurred. 109 + UHID_GET_REPORT_REPLY: 110 + If you receive a UHID_GET_REPORT request you must answer with this request. 111 + You must copy the "id" field from the request into the answer. Set the "err" 112 + field to 0 if no error occurred or to EIO if an I/O error occurred. 120 113 If "err" is 0 then you should fill the buffer of the answer with the results 121 - of the feature request and set "size" correspondingly. 114 + of the GET_REPORT request and set "size" correspondingly. 115 + 116 + UHID_SET_REPORT_REPLY: 117 + This is the SET_REPORT equivalent of UHID_GET_REPORT_REPLY. Unlike GET_REPORT, 118 + SET_REPORT never returns a data buffer, therefore, it's sufficient to set the 119 + "id" and "err" fields correctly. 122 120 123 121 read() 124 122 ------ 125 - read() will return a queued output report. These output reports can be of type 126 - UHID_START, UHID_STOP, UHID_OPEN, UHID_CLOSE, UHID_OUTPUT or UHID_OUTPUT_EV. No 127 - reaction is required to any of them but you should handle them according to your 128 - needs. Only UHID_OUTPUT and UHID_OUTPUT_EV have payloads. 123 + read() will return a queued output report. No reaction is required to any of 124 + them but you should handle them according to your needs. 129 125 130 126 UHID_START: 131 127 This is sent when the HID device is started. Consider this as an answer to 132 - UHID_CREATE. This is always the first event that is sent. 128 + UHID_CREATE2. This is always the first event that is sent. Note that this 129 + event might not be available immediately after write(UHID_CREATE2) returns. 130 + Device drivers might required delayed setups. 131 + This event contains a payload of type uhid_start_req. The "dev_flags" field 132 + describes special behaviors of a device. The following flags are defined: 133 + UHID_DEV_NUMBERED_FEATURE_REPORTS: 134 + UHID_DEV_NUMBERED_OUTPUT_REPORTS: 135 + UHID_DEV_NUMBERED_INPUT_REPORTS: 136 + Each of these flags defines whether a given report-type uses numbered 137 + reports. If numbered reports are used for a type, all messages from 138 + the kernel already have the report-number as prefix. Otherwise, no 139 + prefix is added by the kernel. 140 + For messages sent by user-space to the kernel, you must adjust the 141 + prefixes according to these flags. 133 142 134 143 UHID_STOP: 135 144 This is sent when the HID device is stopped. Consider this as an answer to 136 145 UHID_DESTROY. 137 - If the kernel HID device driver closes the device manually (that is, you 138 - didn't send UHID_DESTROY) then you should consider this device closed and send 139 - an UHID_DESTROY event. You may want to reregister your device, though. This is 140 - always the last message that is sent to you unless you reopen the device with 141 - UHID_CREATE. 146 + If you didn't destroy your device via UHID_DESTROY, but the kernel sends an 147 + UHID_STOP event, this should usually be ignored. It means that the kernel 148 + reloaded/changed the device driver loaded on your HID device (or some other 149 + maintenance actions happened). 150 + You can usually ignored any UHID_STOP events safely. 142 151 143 152 UHID_OPEN: 144 153 This is sent when the HID device is opened. That is, the data that the HID 145 154 device provides is read by some other process. You may ignore this event but 146 155 it is useful for power-management. As long as you haven't received this event 147 156 there is actually no other process that reads your data so there is no need to 148 - send UHID_INPUT events to the kernel. 157 + send UHID_INPUT2 events to the kernel. 149 158 150 159 UHID_CLOSE: 151 160 This is sent when there are no more processes which read the HID data. It is ··· 159 156 160 157 UHID_OUTPUT: 161 158 This is sent if the HID device driver wants to send raw data to the I/O 162 - device. You should read the payload and forward it to the device. The payload 163 - is of type "struct uhid_data_req". 159 + device on the interrupt channel. You should read the payload and forward it to 160 + the device. The payload is of type "struct uhid_data_req". 164 161 This may be received even though you haven't received UHID_OPEN, yet. 165 162 166 - UHID_OUTPUT_EV (obsolete): 167 - Same as UHID_OUTPUT but this contains a "struct input_event" as payload. This 168 - is called for force-feedback, LED or similar events which are received through 169 - an input device by the HID subsystem. You should convert this into raw reports 170 - and send them to your device similar to events of type UHID_OUTPUT. 171 - This is no longer sent by newer kernels. Instead, HID core converts it into a 172 - raw output report and sends it via UHID_OUTPUT. 163 + UHID_GET_REPORT: 164 + This event is sent if the kernel driver wants to perform a GET_REPORT request 165 + on the control channeld as described in the HID specs. The report-type and 166 + report-number are available in the payload. 167 + The kernel serializes GET_REPORT requests so there will never be two in 168 + parallel. However, if you fail to respond with a UHID_GET_REPORT_REPLY, the 169 + request might silently time out. 170 + Once you read a GET_REPORT request, you shall forward it to the hid device and 171 + remember the "id" field in the payload. Once your hid device responds to the 172 + GET_REPORT (or if it fails), you must send a UHID_GET_REPORT_REPLY to the 173 + kernel with the exact same "id" as in the request. If the request already 174 + timed out, the kernel will ignore the response silently. The "id" field is 175 + never re-used, so conflicts cannot happen. 173 176 174 - UHID_FEATURE: 175 - This event is sent if the kernel driver wants to perform a feature request as 176 - described in the HID specs. The report-type and report-number are available in 177 - the payload. 178 - The kernel serializes feature requests so there will never be two in parallel. 179 - However, if you fail to respond with a UHID_FEATURE_ANSWER in a time-span of 5 180 - seconds, then the requests will be dropped and a new one might be sent. 181 - Therefore, the payload also contains an "id" field that identifies every 182 - request. 177 + UHID_SET_REPORT: 178 + This is the SET_REPORT equivalent of UHID_GET_REPORT. On receipt, you shall 179 + send a SET_REPORT request to your hid device. Once it replies, you must tell 180 + the kernel about it via UHID_SET_REPORT_REPLY. 181 + The same restrictions as for UHID_GET_REPORT apply. 183 182 184 - Document by: 185 - David Herrmann <dh.herrmann@googlemail.com> 183 + ---------------------------------------------------- 184 + Written 2012, David Herrmann <dh.herrmann@gmail.com>