xref: /linux/Documentation/hid/uhid.rst (revision d0034a7a4ac7fae708146ac0059b9c47a1543f0d)
1cca47861SMauro Carvalho Chehab======================================================
2d99b8badSDavid HerrmannUHID - User-space I/O driver support for HID subsystem
3cca47861SMauro Carvalho Chehab======================================================
4d99b8badSDavid Herrmann
576c7c491SDavid HerrmannUHID allows user-space to implement HID transport drivers. Please see
6356006a6SRandy Dunlaphid-transport.rst for an introduction into HID transport drivers. This document
776c7c491SDavid Herrmannrelies heavily on the definitions declared there.
8d99b8badSDavid Herrmann
976c7c491SDavid HerrmannWith UHID, a user-space transport driver can create kernel hid-devices for each
1076c7c491SDavid Herrmanndevice connected to the user-space controlled bus. The UHID API defines the I/O
1176c7c491SDavid Herrmannevents provided from the kernel to user-space and vice versa.
12d99b8badSDavid Herrmann
13d99b8badSDavid HerrmannThere is an example user-space application in ./samples/uhid/uhid-example.c
14d99b8badSDavid Herrmann
15d99b8badSDavid HerrmannThe UHID API
16d99b8badSDavid Herrmann------------
17d99b8badSDavid Herrmann
18356006a6SRandy DunlapUHID is accessed through a character misc-device. The minor number is allocated
19d99b8badSDavid Herrmanndynamically so you need to rely on udev (or similar) to create the device node.
20d99b8badSDavid HerrmannThis is /dev/uhid by default.
21d99b8badSDavid Herrmann
22d99b8badSDavid HerrmannIf a new device is detected by your HID I/O Driver and you want to register this
23d99b8badSDavid Herrmanndevice with the HID subsystem, then you need to open /dev/uhid once for each
24d99b8badSDavid Herrmanndevice you want to register. All further communication is done by read()'ing or
25d99b8badSDavid Herrmannwrite()'ing "struct uhid_event" objects. Non-blocking operations are supported
26cca47861SMauro Carvalho Chehabby setting O_NONBLOCK::
27d99b8badSDavid Herrmann
28d99b8badSDavid Herrmann  struct uhid_event {
29d99b8badSDavid Herrmann        __u32 type;
30d99b8badSDavid Herrmann        union {
3176c7c491SDavid Herrmann                struct uhid_create2_req create2;
3276c7c491SDavid Herrmann                struct uhid_output_req output;
3376c7c491SDavid Herrmann                struct uhid_input2_req input2;
34d99b8badSDavid Herrmann                ...
35d99b8badSDavid Herrmann        } u;
36d99b8badSDavid Herrmann  };
37d99b8badSDavid Herrmann
38d99b8badSDavid HerrmannThe "type" field contains the ID of the event. Depending on the ID different
39d99b8badSDavid Herrmannpayloads are sent. You must not split a single event across multiple read()'s or
40d99b8badSDavid Herrmannmultiple write()'s. A single event must always be sent as a whole. Furthermore,
41d99b8badSDavid Herrmannonly a single event can be sent per read() or write(). Pending data is ignored.
42d99b8badSDavid HerrmannIf you want to handle multiple events in a single syscall, then use vectored
43d99b8badSDavid HerrmannI/O with readv()/writev().
4476c7c491SDavid HerrmannThe "type" field defines the payload. For each type, there is a
4576c7c491SDavid Herrmannpayload-structure available in the union "u" (except for empty payloads). This
4676c7c491SDavid Herrmannpayload contains management and/or device data.
47d99b8badSDavid Herrmann
48356006a6SRandy DunlapThe first thing you should do is send a UHID_CREATE2 event. This will
49356006a6SRandy Dunlapregister the device. UHID will respond with a UHID_START event. You can now
50d99b8badSDavid Herrmannstart sending data to and reading data from UHID. However, unless UHID sends the
51d99b8badSDavid HerrmannUHID_OPEN event, the internally attached HID Device Driver has no user attached.
52d99b8badSDavid HerrmannThat is, you might put your device asleep unless you receive the UHID_OPEN
53d99b8badSDavid Herrmannevent. If you receive the UHID_OPEN event, you should start I/O. If the last
54356006a6SRandy Dunlapuser closes the HID device, you will receive a UHID_CLOSE event. This may be
55356006a6SRandy Dunlapfollowed by a UHID_OPEN event again and so on. There is no need to perform
56d99b8badSDavid Herrmannreference-counting in user-space. That is, you will never receive multiple
57356006a6SRandy DunlapUHID_OPEN events without a UHID_CLOSE event. The HID subsystem performs
58d99b8badSDavid Herrmannref-counting for you.
59d99b8badSDavid HerrmannYou may decide to ignore UHID_OPEN/UHID_CLOSE, though. I/O is allowed even
60d99b8badSDavid Herrmannthough the device may have no users.
61d99b8badSDavid Herrmann
6276c7c491SDavid HerrmannIf you want to send data on the interrupt channel to the HID subsystem, you send
63356006a6SRandy Dunlapa HID_INPUT2 event with your raw data payload. If the kernel wants to send data
64356006a6SRandy Dunlapon the interrupt channel to the device, you will read a UHID_OUTPUT event.
6576c7c491SDavid HerrmannData requests on the control channel are currently limited to GET_REPORT and
6676c7c491SDavid HerrmannSET_REPORT (no other data reports on the control channel are defined so far).
6776c7c491SDavid HerrmannThose requests are always synchronous. That means, the kernel sends
6876c7c491SDavid HerrmannUHID_GET_REPORT and UHID_SET_REPORT events and requires you to forward them to
6976c7c491SDavid Herrmannthe device on the control channel. Once the device responds, you must forward
7076c7c491SDavid Herrmannthe response via UHID_GET_REPORT_REPLY and UHID_SET_REPORT_REPLY to the kernel.
7176c7c491SDavid HerrmannThe kernel blocks internal driver-execution during such round-trips (times out
7276c7c491SDavid Herrmannafter a hard-coded period).
73d99b8badSDavid Herrmann
74356006a6SRandy DunlapIf your device disconnects, you should send a UHID_DESTROY event. This will
7576c7c491SDavid Herrmannunregister the device. You can now send UHID_CREATE2 again to register a new
76d99b8badSDavid Herrmanndevice.
77d99b8badSDavid HerrmannIf you close() the fd, the device is automatically unregistered and destroyed
78d99b8badSDavid Herrmanninternally.
79d99b8badSDavid Herrmann
80d99b8badSDavid Herrmannwrite()
81d99b8badSDavid Herrmann-------
82d99b8badSDavid Herrmannwrite() allows you to modify the state of the device and feed input data into
8376c7c491SDavid Herrmannthe kernel. The kernel will parse the event immediately and if the event ID is
84d99b8badSDavid Herrmannnot supported, it will return -EOPNOTSUPP. If the payload is invalid, then
85d99b8badSDavid Herrmann-EINVAL is returned, otherwise, the amount of data that was read is returned and
8676c7c491SDavid Herrmannthe request was handled successfully. O_NONBLOCK does not affect write() as
8776c7c491SDavid Herrmannwrites are always handled immediately in a non-blocking fashion. Future requests
8876c7c491SDavid Herrmannmight make use of O_NONBLOCK, though.
89d99b8badSDavid Herrmann
904522643aSPetri GyntherUHID_CREATE2:
9176c7c491SDavid Herrmann  This creates the internal HID device. No I/O is possible until you send this
9276c7c491SDavid Herrmann  event to the kernel. The payload is of type struct uhid_create2_req and
9376c7c491SDavid Herrmann  contains information about your device. You can start I/O now.
944522643aSPetri Gynther
95d99b8badSDavid HerrmannUHID_DESTROY:
96d99b8badSDavid Herrmann  This destroys the internal HID device. No further I/O will be accepted. There
97d99b8badSDavid Herrmann  may still be pending messages that you can receive with read() but no further
98d99b8badSDavid Herrmann  UHID_INPUT events can be sent to the kernel.
9976c7c491SDavid Herrmann  You can create a new device by sending UHID_CREATE2 again. There is no need to
100d99b8badSDavid Herrmann  reopen the character device.
101d99b8badSDavid Herrmann
1024522643aSPetri GyntherUHID_INPUT2:
10376c7c491SDavid Herrmann  You must send UHID_CREATE2 before sending input to the kernel! This event
10476c7c491SDavid Herrmann  contains a data-payload. This is the raw data that you read from your device
10576c7c491SDavid Herrmann  on the interrupt channel. The kernel will parse the HID reports.
1064522643aSPetri Gynther
10776c7c491SDavid HerrmannUHID_GET_REPORT_REPLY:
10876c7c491SDavid Herrmann  If you receive a UHID_GET_REPORT request you must answer with this request.
10976c7c491SDavid Herrmann  You  must copy the "id" field from the request into the answer. Set the "err"
11076c7c491SDavid Herrmann  field to 0 if no error occurred or to EIO if an I/O error occurred.
111d99b8badSDavid Herrmann  If "err" is 0 then you should fill the buffer of the answer with the results
11276c7c491SDavid Herrmann  of the GET_REPORT request and set "size" correspondingly.
11376c7c491SDavid Herrmann
11476c7c491SDavid HerrmannUHID_SET_REPORT_REPLY:
11576c7c491SDavid Herrmann  This is the SET_REPORT equivalent of UHID_GET_REPORT_REPLY. Unlike GET_REPORT,
11676c7c491SDavid Herrmann  SET_REPORT never returns a data buffer, therefore, it's sufficient to set the
11776c7c491SDavid Herrmann  "id" and "err" fields correctly.
118d99b8badSDavid Herrmann
119d99b8badSDavid Herrmannread()
120d99b8badSDavid Herrmann------
12176c7c491SDavid Herrmannread() will return a queued output report. No reaction is required to any of
12276c7c491SDavid Herrmannthem but you should handle them according to your needs.
123d99b8badSDavid Herrmann
124d99b8badSDavid HerrmannUHID_START:
125d99b8badSDavid Herrmann  This is sent when the HID device is started. Consider this as an answer to
12676c7c491SDavid Herrmann  UHID_CREATE2. This is always the first event that is sent. Note that this
12776c7c491SDavid Herrmann  event might not be available immediately after write(UHID_CREATE2) returns.
128356006a6SRandy Dunlap  Device drivers might require delayed setups.
12976c7c491SDavid Herrmann  This event contains a payload of type uhid_start_req. The "dev_flags" field
13076c7c491SDavid Herrmann  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
13676c7c491SDavid Herrmann          Each of these flags defines whether a given report-type uses numbered
13776c7c491SDavid Herrmann          reports. If numbered reports are used for a type, all messages from
13876c7c491SDavid Herrmann          the kernel already have the report-number as prefix. Otherwise, no
13976c7c491SDavid Herrmann          prefix is added by the kernel.
14076c7c491SDavid Herrmann          For messages sent by user-space to the kernel, you must adjust the
14176c7c491SDavid Herrmann          prefixes according to these flags.
142d99b8badSDavid Herrmann
143d99b8badSDavid HerrmannUHID_STOP:
144d99b8badSDavid Herrmann  This is sent when the HID device is stopped. Consider this as an answer to
145d99b8badSDavid Herrmann  UHID_DESTROY.
146cca47861SMauro Carvalho Chehab
14776c7c491SDavid Herrmann  If you didn't destroy your device via UHID_DESTROY, but the kernel sends an
14876c7c491SDavid Herrmann  UHID_STOP event, this should usually be ignored. It means that the kernel
14976c7c491SDavid Herrmann  reloaded/changed the device driver loaded on your HID device (or some other
15076c7c491SDavid Herrmann  maintenance actions happened).
151cca47861SMauro Carvalho Chehab
152356006a6SRandy Dunlap  You can usually ignore any UHID_STOP events safely.
153d99b8badSDavid Herrmann
154d99b8badSDavid HerrmannUHID_OPEN:
155d99b8badSDavid Herrmann  This is sent when the HID device is opened. That is, the data that the HID
156d99b8badSDavid Herrmann  device provides is read by some other process. You may ignore this event but
157d99b8badSDavid Herrmann  it is useful for power-management. As long as you haven't received this event
158d99b8badSDavid Herrmann  there is actually no other process that reads your data so there is no need to
15976c7c491SDavid Herrmann  send UHID_INPUT2 events to the kernel.
160d99b8badSDavid Herrmann
161d99b8badSDavid HerrmannUHID_CLOSE:
162d99b8badSDavid Herrmann  This is sent when there are no more processes which read the HID data. It is
163d99b8badSDavid Herrmann  the counterpart of UHID_OPEN and you may as well ignore this event.
164d99b8badSDavid Herrmann
165d99b8badSDavid HerrmannUHID_OUTPUT:
166d99b8badSDavid Herrmann  This is sent if the HID device driver wants to send raw data to the I/O
16776c7c491SDavid Herrmann  device on the interrupt channel. You should read the payload and forward it to
16846b14eefSPeter Hutterer  the device. The payload is of type "struct uhid_output_req".
169356006a6SRandy Dunlap  This may be received even though you haven't received UHID_OPEN yet.
170d99b8badSDavid Herrmann
17176c7c491SDavid HerrmannUHID_GET_REPORT:
17276c7c491SDavid Herrmann  This event is sent if the kernel driver wants to perform a GET_REPORT request
173356006a6SRandy Dunlap  on the control channel as described in the HID specs. The report-type and
17476c7c491SDavid Herrmann  report-number are available in the payload.
17576c7c491SDavid Herrmann  The kernel serializes GET_REPORT requests so there will never be two in
17676c7c491SDavid Herrmann  parallel. However, if you fail to respond with a UHID_GET_REPORT_REPLY, the
17776c7c491SDavid Herrmann  request might silently time out.
178356006a6SRandy Dunlap  Once you read a GET_REPORT request, you shall forward it to the HID device and
179356006a6SRandy Dunlap  remember the "id" field in the payload. Once your HID device responds to the
18076c7c491SDavid Herrmann  GET_REPORT (or if it fails), you must send a UHID_GET_REPORT_REPLY to the
18176c7c491SDavid Herrmann  kernel with the exact same "id" as in the request. If the request already
18276c7c491SDavid Herrmann  timed out, the kernel will ignore the response silently. The "id" field is
18376c7c491SDavid Herrmann  never re-used, so conflicts cannot happen.
184d99b8badSDavid Herrmann
18576c7c491SDavid HerrmannUHID_SET_REPORT:
18676c7c491SDavid Herrmann  This is the SET_REPORT equivalent of UHID_GET_REPORT. On receipt, you shall
187356006a6SRandy Dunlap  send a SET_REPORT request to your HID device. Once it replies, you must tell
18876c7c491SDavid Herrmann  the kernel about it via UHID_SET_REPORT_REPLY.
18976c7c491SDavid Herrmann  The same restrictions as for UHID_GET_REPORT apply.
190d99b8badSDavid Herrmann
19176c7c491SDavid Herrmann----------------------------------------------------
192cca47861SMauro Carvalho Chehab
19376c7c491SDavid HerrmannWritten 2012, David Herrmann <dh.herrmann@gmail.com>
194