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