Skip to contentSkip to conversation

HID: User-space HID I/O driver (UHID)

PATCH In review David Herrmann 3 participants 8 messages Apr 30, 2012 – May 23, 2012

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover HID: User-space HID I/O driver (UHID)

Hi

This implements a hid_ll_driver user-space interface similar to uinput for the input devices. It allows to implement the HID transport-layer in user-space as required by Bluetooth Low-Energy (HoG).

This is my "first final revision" for the module. Please review and test. Changes from the previous RFCs include:
- The feature-report mechanism is now implemented
- Several min_t() fixes
- Minor coding-style issues

I did not change __attribute__((__packed__)) to __packed as the latter one is not provided to user-space.

If there are any more issues, please tell me. And for the record, the HoG devs told me that they are working since several months with it now so we have a real user-space application that needs it ;)

If there are no more issues (which i doubt) I can also resend this splitted into multiple patches.

Regards
David

David Herrmann (1):
HID: User-space I/O driver support for HID subsystem

 Documentation/hid/uhid.txt  |  169 +++++++++++++
 drivers/hid/Kconfig         |   21 ++
 drivers/hid/Makefile        |    2 +-
 drivers/hid/uhid.c          |  572 +++++++++++++++++++++++++++++++++++++++++++
 include/linux/Kbuild        |    1 +
 include/linux/uhid.h        |  104 ++++++++
 samples/uhid/Makefile       |   10 +
 samples/uhid/uhid-example.c |  381 ++++++++++++++++++++++++++++
 8 files changed, 1259 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/hid/uhid.txt
 create mode 100644 drivers/hid/uhid.c
 create mode 100644 include/linux/uhid.h
 create mode 100644 samples/uhid/Makefile
 create mode 100644 samples/uhid/uhid-example.c
Signature · 1 line
1.7.10
#1 lore raw

PATCH 1/1 HID: User-space I/O driver support for HID subsystem

This driver allows to write I/O drivers in user-space and feed the input into the HID subsystem. It operates on the same level as USB-HID and Bluetooth-HID (HIDP). It does not provide support to write special HID device drivers but rather provides support for user-space I/O devices to feed their data into the kernel HID subsystem. The HID subsystem then loads the HID device drivers for the device and provides input-devices based on the user-space HID I/O device.

This driver register a new char-device (/dev/uhid). A user-space process has to open this file for each device that it wants to provide to the kernel. It can then use write/read to communicate with the UHID driver. Both input and output data is sent with a uhid_event structure. The "type" field of the structure specifies what kind of event is sent. There is a file in Documentation/hid/ explaining the ABI and an example user-space program that uses this interface in samples/uhid/.

---
 Documentation/hid/uhid.txt  |  169 +++++++++++++
 drivers/hid/Kconfig         |   21 ++
 drivers/hid/Makefile        |    2 +-
 drivers/hid/uhid.c          |  572 +++++++++++++++++++++++++++++++++++++++++++
 include/linux/Kbuild        |    1 +
 include/linux/uhid.h        |  104 ++++++++
 samples/uhid/Makefile       |   10 +
 samples/uhid/uhid-example.c |  381 ++++++++++++++++++++++++++++
 8 files changed, 1259 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/hid/uhid.txt
 create mode 100644 drivers/hid/uhid.c
 create mode 100644 include/linux/uhid.h
 create mode 100644 samples/uhid/Makefile
 create mode 100644 samples/uhid/uhid-example.c
Documentation/hid/uhid.txt new file +169 -0
new file mode 100644
index 0000000..4627c42
@@ -0,0 +1,169 @@
+      UHID - User-space I/O driver support for HID subsystem
+     ========================================================
+
+The HID subsystem needs two kinds of drivers. In this document we call them:
+
+ 1. The "HID I/O Driver" is the driver that performs raw data I/O to the
+    low-level device. Internally, they register an hid_ll_driver structure with
+    the HID core. They perform device setup, read raw data from the device and
+    push it into the HID subsystem and they provide a callback so the HID
+    subsystem can send data to the device.
+
+ 2. The "HID Device Driver" is the driver that parses HID reports and reacts on
+    them. There are generic drivers like "generic-usb" and "generic-bluetooth"
+    which adhere to the HID specification and provide the standardizes features.
+    But there may be special drivers and quirks for each non-standard device out
+    there. Internally, they use the hid_driver structure.
+
+Historically, the USB stack was the first subsystem to provide an HID I/O
+Driver. However, other standards like Bluetooth have adopted the HID specs and
+may provide HID I/O Drivers, too. The UHID driver allows to implement HID I/O
+Drivers in user-space and feed the data into the kernel HID-subsystem.
+
+This allows user-space to operate on the same level as USB-HID, Bluetooth-HID
+and similar. It does not provide a way to write HID Device Drivers, though. Use
+hidraw for this purpose.
+
+There is an example user-space application in ./samples/uhid/uhid-example.c
+
+The UHID API
+------------
+
+UHID is accessed through a character misc-device. The minor-number is allocated
+dynamically so you need to rely on udev (or similar) to create the device node.
+This is /dev/uhid by default.
+
+If a new device is detected by your HID I/O Driver and you want to register this
+device with the HID subsystem, then you need to open /dev/uhid once for each
+device you want to register. All further communication is done by read()'ing or
+write()'ing "struct uhid_event" objects. Non-blocking operations are supported
+by setting O_NONBLOCK.
+
+struct uhid_event {
+        __u32 type;
+        union {
+                struct uhid_create_req create;
+                struct uhid_data_req data;
+                ...
+        } u;
+};
+
+The "type" field contains the ID of the event. Depending on the ID different
+payloads are sent. You must not split a single event across multiple read()'s or
+multiple write()'s. A single event must always be sent as a whole. Furthermore,
+only a single event can be sent per read() or write(). Pending data is ignored.
+If you want to handle multiple events in a single syscall, then use vectored
+I/O with readv()/writev().
+
+The first thing you should do is sending an UHID_CREATE event. This will
+register the device. UHID will respond with an UHID_START event. You can now
+start sending data to and reading data from UHID. However, unless UHID sends the
+UHID_OPEN event, the internally attached HID Device Driver has no user attached.
+That is, you might put your device asleep unless you receive the UHID_OPEN
+event. If you receive the UHID_OPEN event, you should start I/O. If the last
+user closes the HID device, you will receive an UHID_CLOSE event. This may be
+followed by an UHID_OPEN event again and so on. There is no need to perform
+reference-counting in user-space. That is, you will never receive multiple
+UHID_OPEN events without an UHID_CLOSE event. The HID subsystem performs
+ref-counting for you.
+You may decide to ignore UHID_OPEN/UHID_CLOSE, though. I/O is allowed even
+though the device may have no users.
+
+If you want to send data to the HID subsystem, you send an HID_INPUT event with
+your raw data payload. If the kernel wants to send data to the device, you will
+read an UHID_OUTPUT or UHID_OUTPUT_EV event.
+
+If your device disconnects, you should send an UHID_DESTROY event. This will
+unregister the device. You can now send UHID_CREATE again to register a new
+device.
+If you close() the fd, the device is automatically unregistered and destroyed
+internally.
+
+write()
+-------
+write() allows you to modify the state of the device and feed input data into
+the kernel. The following types are supported: UHID_CREATE, UHID_DESTROY and
+UHID_INPUT. The kernel will parse the event immediately and if the event ID is
+not supported, it will return -EOPNOTSUPP. If the payload is invalid, then
+-EINVAL is returned, otherwise, the amount of data that was read is returned and
+the request was handled successfully.
+
+  UHID_CREATE:
+  This creates the internal HID device. No I/O is possible until you send this
+  event to the kernel. The payload is of type struct uhid_create_req and
+  contains information about your device. You can start I/O now.
+
+  UHID_DESTROY:
+  This destroys the internal HID device. No further I/O will be accepted. There
+  may still be pending messages that you can receive with read() but no further
+  UHID_INPUT events can be sent to the kernel.
+  You can create a new device by sending UHID_CREATE again. There is no need to
+  reopen the character device.
+
+  UHID_INPUT:
+  You must send UHID_CREATE before sending input to the kernel! This event
+  contains a data-payload. This is the raw data that you read from your device.
+  The kernel will parse the HID reports and react on it.
+
+  UHID_FEATURE_ANSWER:
+  If you receive a UHID_FEATURE request you must answer with this request. You
+  must copy the "id" field from the request into the answer. Set the "err" field
+  to 0 if no error occured or to EIO if an I/O error occurred.
+  If "err" is 0 then you should fill the buffer of the answer with the results
+  of the feature request and set "size" correspondingly.
+
+read()
+------
+read() will return a queued ouput report. These output reports can be of type
+UHID_START, UHID_STOP, UHID_OPEN, UHID_CLOSE, UHID_OUTPUT or UHID_OUTPUT_EV. No
+reaction is required to any of them but you should handle them according to your
+needs. Only UHID_OUTPUT and UHID_OUTPUT_EV have payloads.
+
+  UHID_START:
+  This is sent when the HID device is started. Consider this as an answer to
+  UHID_CREATE. This is always the first event that is sent.
+
+  UHID_STOP:
+  This is sent when the HID device is stopped. Consider this as an answer to
+  UHID_DESTROY.
+  If the kernel HID device driver closes the device manually (that is, you
+  didn't send UHID_DESTROY) then you should consider this device closed and send
+  an UHID_DESTROY event. You may want to reregister your device, though. This is
+  always the last message that is sent to you unless you reopen the device with
+  UHID_CREATE.
+
+  UHID_OPEN:
+  This is sent when the HID device is opened. That is, the data that the HID
+  device provides is read by some other process. You may ignore this event but
+  it is useful for power-management. As long as you haven't received this event
+  there is actually no other process that reads your data so there is no need to
+  send UHID_INPUT events to the kernel.
+
+  UHID_CLOSE:
+  This is sent when there are no more processes which read the HID data. It is
+  the counterpart of UHID_OPEN and you may as well ignore this event.
+
+  UHID_OUTPUT:
+  This is sent if the HID device driver wants to send raw data to the I/O
+  device. You should read the payload and forward it to the device. The payload
+  is of type "struct uhid_data_req".
+  This may be received even though you haven't received UHID_OPEN, yet.
+
+  UHID_OUTPUT_EV:
+  Same as UHID_OUTPUT but this contains a "struct input_event" as payload. This
+  is called for force-feedback, LED or similar events which are received through
+  an input device by the HID subsystem. You should convert this into raw reports
+  and send them to your device similar to events of type UHID_OUTPUT.
+
+  UHID_FEATURE:
+  This event is sent if the kernel driver wants to perform a feature request as
+  described in the HID specs. The report-type and report-number are available in
+  the payload.
+  The kernel serializes feature requests so there will never be two in parallel.
+  However, if you fail to respond with a UHID_FEATURE_ANSWER in a time-span of 5
+  seconds, then the requests will be dropped and a new one might be sent.
+  Therefore, the payload also contains an "id" field that identifies every
+  request.
+
+Document by:
+  David Herrmann <[email protected]>
drivers/hid/Kconfig +21 -0
index 54cc92f..f6d47cd 100644
@@ -55,6 +55,27 @@ config HIDRAW
 
 	If unsure, say Y.
 
+config UHID
+	tristate "User-space I/O driver support for HID subsystem"
+	depends on HID
+	default n
+	---help---
+	Say Y here if you want to provide HID I/O Drivers from user-space.
+	This allows to write I/O drivers in user-space and feed the data from
+	the device into the kernel. The kernel parses the HID reports, loads the
+	corresponding HID Device Driver or provides input devices on top of your
+	user-space device.
+
+	This driver cannot be used to parse HID-reports in user-space and write
+	special HID-drivers. You should use hidraw for that.
+	Instead, this driver allows to write the transport-layer driver in
+	user-space like USB-HID and Bluetooth-HID do in kernel-space.
+
+	If unsure, say N.
+
+	To compile this driver as a module, choose M here: the
+	module will be called uhid.
+
 source "drivers/hid/usbhid/Kconfig"
 
 menu "Special HID drivers"
drivers/hid/Makefile +1 -1
index 22f1d16..cadb84f 100644
@@ -8,6 +8,7 @@ ifdef CONFIG_DEBUG_FS
 endif
 
 obj-$(CONFIG_HID)		+= hid.o
+obj-$(CONFIG_UHID)		+= uhid.o
 
 hid-$(CONFIG_HIDRAW)		+= hidraw.o
 
@@ -88,4 +89,3 @@ obj-$(CONFIG_HID_WIIMOTE)	+= hid-wiimote.o
 obj-$(CONFIG_USB_HID)		+= usbhid/
 obj-$(CONFIG_USB_MOUSE)		+= usbhid/
 obj-$(CONFIG_USB_KBD)		+= usbhid/
-
drivers/hid/uhid.c new file +572 -0

572 lines not on this page · raw message

include/linux/Kbuild +1 -0
index c94e717..b8d5ed0 100644
@@ -370,6 +370,7 @@ header-y += tty.h
 header-y += types.h
 header-y += udf_fs_i.h
 header-y += udp.h
+header-y += uhid.h
 header-y += uinput.h
 header-y += uio.h
 header-y += ultrasound.h
include/linux/uhid.h new file +104 -0

104 lines not on this page · raw message

samples/uhid/Makefile new file +10 -0
new file mode 100644
index 0000000..c95a696
@@ -0,0 +1,10 @@
+# kbuild trick to avoid linker error. Can be omitted if a module is built.
+obj- := dummy.o
+
+# List of programs to build
+hostprogs-y := uhid-example
+
+# Tell kbuild to always build the programs
+always := $(hostprogs-y)
+
+HOSTCFLAGS_uhid-example.o += -I$(objtree)/usr/include
samples/uhid/uhid-example.c new file +381 -0

381 lines not on this page · raw message

Signature · 1 line
1.7.10
#2 lore raw
In reply to David Herrmann
Quoted 3 lines · David Herrmann

Hi

This implements a hid_ll_driver user-space interface similar to uinput fo=

r the

Quoted 1 line

input devices. It allows to implement the HID transport-layer in user-spa=

ce as

Quoted 9 lines

required by Bluetooth Low-Energy (HoG).

This is my "first final revision" for the module. Please review and test. Changes from the previous RFCs include:
=C2=A0- The feature-report mechanism is now implemented =C2=A0- Several min_t() fixes
=C2=A0- Minor coding-style issues

I did not change __attribute__((__packed__)) to __packed as the latter on=

e is

Quoted 3 lines

not provided to user-space.

If there are any more issues, please tell me. And for the record, the HoG=

devs

Quoted 1 line

told me that they are working since several months with it now so we have=

a real

Quoted 3 lines

user-space application that needs it ;)

If there are no more issues (which i doubt) I can also resend this splitt=

ed into

Quoted 2 lines

multiple patches.

Thanks for your work, David. I'll test and review this new version. Did you find out the what is the problem when passing BUS_BLUETOOTH on the 'bus' field of the create request?

Also, how does the HID subsystem differentiate from HID protocol versions? We've just found out that HoG exposes what HID protocol version the device implements, is it the case of having an extra field on the uhid_create_req structure to pass this information, or is it already present on the HID descriptor (sorry for my limited knowledge on the HID protocol itself)?

--=20
Jo=C3=A3o Paulo Rechi Vita
Openbossa Labs - INdT

#3 lore raw

Hi Joao

Quoted 5 lines · Joao Paulo Rechi Vita
Quoted 3 lines · David Herrmann

Hi

This implements a hid_ll_driver user-space interface similar to uinput f=

or the

Quoted 1 line
Quoted 1 line

input devices. It allows to implement the HID transport-layer in user-sp=

ace as

Quoted 3 lines
Quoted 3 lines

required by Bluetooth Low-Energy (HoG).

This is my "first final revision" for the module. Please review and test=

.

Quoted 6 lines
Quoted 6 lines

Changes from the previous RFCs include:
=A0- The feature-report mechanism is now implemented =A0- Several min_t() fixes
=A0- Minor coding-style issues

I did not change __attribute__((__packed__)) to __packed as the latter o=

ne is

Quoted 3 lines
Quoted 3 lines

not provided to user-space.

If there are any more issues, please tell me. And for the record, the Ho=

G devs

Quoted 1 line
Quoted 1 line

told me that they are working since several months with it now so we hav=

e a real

Quoted 3 lines
Quoted 3 lines

user-space application that needs it ;)

If there are no more issues (which i doubt) I can also resend this split=

ted into

Quoted 6 lines
Quoted 2 lines

multiple patches.

Thanks for your work, David. I'll test and review this new version. Did you find out the what is the problem when passing BUS_BLUETOOTH on the 'bus' field of the create request?

I've spent some time looking into that but I haven't found the problem, yet. Maybe Jiri can comment here.

Quoted 6 lines

Also, how does the HID subsystem differentiate from HID protocol versions? We've just found out that HoG exposes what HID protocol version the device implements, is it the case of having an extra field on the uhid_create_req structure to pass this information, or is it already present on the HID descriptor (sorry for my limited knowledge on the HID protocol itself)?

There is already a "version" field in UHID_CREATE. It isn't mandatory, though, so setting it to 0 works. I even think the HID core doesn't even look into that value except for printk messages.

Quoted 3 lines

--
Jo=E3o Paulo Rechi Vita
Openbossa Labs - INdT

Thanks
David

#4 lore raw

Hi Joao

Quoted 9 lines · David Herrmann

Hi Joao

Quoted 5 lines · Joao Paulo Rechi Vita
Quoted 3 lines · David Herrmann

Hi

This implements a hid_ll_driver user-space interface similar to uinput =

for the

Quoted 1 line
Quoted 1 line
Quoted 1 line

input devices. It allows to implement the HID transport-layer in user-s=

pace as

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

required by Bluetooth Low-Energy (HoG).

This is my "first final revision" for the module. Please review and tes=

t.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Changes from the previous RFCs include:
=A0- The feature-report mechanism is now implemented =A0- Several min_t() fixes
=A0- Minor coding-style issues

I did not change __attribute__((__packed__)) to __packed as the latter =

one is

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

not provided to user-space.

If there are any more issues, please tell me. And for the record, the H=

oG devs

Quoted 1 line
Quoted 1 line
Quoted 1 line

told me that they are working since several months with it now so we ha=

ve a real

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

user-space application that needs it ;)

If there are no more issues (which i doubt) I can also resend this spli=

tted into

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

multiple patches.

Thanks for your work, David. I'll test and review this new version. Did you find out the what is the problem when passing BUS_BLUETOOTH on the 'bus' field of the create request?

I've spent some time looking into that but I haven't found the problem, yet. Maybe Jiri can comment here.

hid-next should fix this:
http://git.kernel.org/?p=3Dlinux/kernel/git/jikos/hid.git;a=3Dcommitdiff;h=
=3D8215d557e5f3a70e50e07c857d35c250fee62a73

I actually haven't tested UHID with hid-next. Some core interfaces are changed and I will update UHID as soon as Linus pulls the HID updates.

The BUS_USB problems should be gone, though.

Quoted 11 lines
Quoted 6 lines

Also, how does the HID subsystem differentiate from HID protocol versions? We've just found out that HoG exposes what HID protocol version the device implements, is it the case of having an extra field on the uhid_create_req structure to pass this information, or is it already present on the HID descriptor (sorry for my limited knowledge on the HID protocol itself)?

There is already a "version" field in UHID_CREATE. It isn't mandatory, though, so setting it to 0 works. I even think the HID core doesn't even look into that value except for printk messages.

Quoted 6 lines
Quoted 3 lines

--
Jo=E3o Paulo Rechi Vita
Openbossa Labs - INdT

Thanks
David

Regards
David

#5 lore raw
In reply to David Herrmann
Quoted 6 lines · David Herrmann
Quoted 3 lines

Thanks for your work, David. I'll test and review this new version. Did you find out the what is the problem when passing BUS_BLUETOOTH on the 'bus' field of the create request?

I've spent some time looking into that but I haven't found the problem, yet. Maybe Jiri can comment here.

This should now all be handled correctly using the hid-generic mechanism. If you still see any problems with this (currently in linux-next, more precisely 'device-groups' branch in hid.git), please let me know.

Signature · 2 lines
Jiri Kosina
SUSE Labs
#6 lore raw

Hi Jiri

Quoted 6 lines · Jiri Kosina
Quoted 4 lines · David Herrmann
Quoted 3 lines

Thanks for your work, David. I'll test and review this new version. Did you find out the what is the problem when passing BUS_BLUETOOTH on the 'bus' field of the create request?

Quoted 6 lines
Quoted 2 lines

I've spent some time looking into that but I haven't found the problem, yet. Maybe Jiri can comment here.

This should now all be handled correctly using the hid-generic mechanism. If you still see any problems with this (currently in linux-next, more precisely 'device-groups' branch in hid.git), please let me know.

That is working correctly now. Do you have any update on this patch? There is no time pressure in merging it but I would like to get some feedback what you are thinking about it so I can continue working on it.

If you like I can resend it split into multiple patches. Let me know of any problems with it.

Quoted 3 lines

--
Jiri Kosina
SUSE Labs

Regards
David

#7 lore raw
Quoted 4 lines · David Herrmann
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

Thanks for your work, David. I'll test and review this new version. Did you find out the what is the problem when passing BUS_BLUETOOTH on the 'bus' field of the create request?

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

I've spent some time looking into that but I haven't found the problem, yet. Maybe Jiri can comment here.

This should now all be handled correctly using the hid-generic mechanism. If you still see any problems with this (currently in linux-next, more precisely 'device-groups' branch in hid.git), please let me know.

That is working correctly now.

Thanks for checking.

Quoted 6 lines

Do you have any update on this patch? There is no time pressure in merging it but I would like to get some feedback what you are thinking about it so I can continue working on it.

If you like I can resend it split into multiple patches. Let me know of any problems with it.

Yes, that would be very much preferred on my side -- huge condensed patches are much harder to review (if the split is done properly). Thanks!

Signature · 2 lines
Jiri Kosina
SUSE Labs