HID: Add USI support
Subsystems (3)
v5 cover HID: Add USI support
Hi,
These two patches add the missing pieces for HID USI support. First one adds the HID core changes to support the new Misc events for pen ID, line color and line style. The second patch adds a BPF program on top of the HID-BPF driver which adds support for writing the Pen parameters from userspace, and to add filtering of HID low level events for ELAN USI controller. The BPF programs are not built by the kernel as of now (there are no Makefile changes), as there is a plan to most likely integrate these to a kernel external repository. I have tested these in my own external build setup though, and I can provide the makefile for that if needed. Also a sample client program is provided for communicating with the D-BUS server.
I have also a kernel testing branch available at [1], which contains a few fix patches on top of Benjamin's HID-BPF driver work, and is rebased on top of latest hid/for-next. The HID-BPF fixes have been cleaned up a bit compared to previous setup. There are also a couple of new patches for adding support for a delayed_work BPF program on top of the hid-bpf driver; this is used to execute the raw_requests in non-irq context.
-Tero
RFC v5 1/2 HID: core: Add support for USI style events
Add support for Universal Stylus Interface (USI) style events to the HID core and input layers.
--- drivers/hid/hid-debug.c | 9 +++++- drivers/hid/hid-input.c | 40 ++++++++++++++++++++++++++ include/linux/mod_devicetable.h | 2 +- include/uapi/linux/input-event-codes.h | 26 +++++++++++------ 4 files changed, 67 insertions(+), 10 deletions(-)
drivers/hid/hid-debug.c +8 -1
@@ -1025,7 +1025,14 @@ static const char *absolutes[ABS_CNT] = {static const char *misc[MSC_MAX +1 ] = { [MSC_SERIAL ] ="Serial" , [MSC_PULSELED ] ="Pulseled" , - [MSC_GESTURE ] ="Gesture" , [MSC_RAW ] ="RawData" + [MSC_GESTURE ] ="Gesture" , [MSC_RAW ] ="RawData" , + [MSC_PEN_ID] = "PenID", [MSC_PEN_COLOR] "PenColor", + [MSC_PEN_LINE_STYLE_INK ] ="PenLineStyleInk" , + [MSC_PEN_LINE_STYLE_PENCIL ] ="PenLineStylePencil" , + [MSC_PEN_LINE_STYLE_HIGHLIGHTER ] ="PenLineStyleHighlighter" , + [MSC_PEN_LINE_STYLE_CHISEL_MARKER ] ="PenLineStyleChiselMarker" , + [MSC_PEN_LINE_STYLE_BRUSH ] ="PenLineStyleBrush" , + [MSC_PEN_LINE_STYLE_NO_PREFERENCE ] ="PenLineStyleNoPreference" , };static const char *leds[LED_MAX +1 ] = {
drivers/hid/hid-input.c +40 -0
@@ -833,6 +833,10 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel }break ; +case 0x38 :/* Transducer Index */ +map_msc (MSC_PEN_ID ); + goto ignore; +case 0x3b :/* Battery Strength */ hidinput_setup_battery (device,HID_INPUT_REPORT , field, false); usage->type =EV_PWR ;
@@ -880,6 +884,42 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fielmap_msc (MSC_SERIAL );break ; +case 0x5c :/* Digitizer Preferred Color */ +map_msc (MSC_PEN_COLOR ); +break ; + +case 0x5e :/* Digitizer Preferred Line Width */ +map_abs_clear (ABS_TOOL_WIDTH ); +break ; + +case 0x70 :/* Preferred Line Style -> not an input usage */ +case 0x71 :/* Preferred Line Style is Locked */ + goto ignore; + +case 0x72 :/* Ink */ +map_msc (MSC_PEN_LINE_STYLE_INK ); +break ; + +case 0x73 :/* Pencil */ +map_msc (MSC_PEN_LINE_STYLE_PENCIL ); +break ; + +case 0x74 :/* Highlighter */ +map_msc (MSC_PEN_LINE_STYLE_HIGHLIGHTER ); +break ; + +case 0x75 :/* Chisel Marker */ +map_msc (MSC_PEN_LINE_STYLE_CHISEL_MARKER ); +break ; + +case 0x76 :/* Brush */ +map_msc (MSC_PEN_LINE_STYLE_BRUSH ); +break ; + +case 0x77 :/* No Preference */ +map_msc (MSC_PEN_LINE_STYLE_NO_PREFERENCE ); +break ; +default : goto unknown; }break ;
include/linux/mod_devicetable.h +1 -1
@@ -322,7 +322,7 @@ struct pcmcia_device_id {#define INPUT_DEVICE_ID_KEY_MAX 0x2ff#define INPUT_DEVICE_ID_REL_MAX 0x0f#define INPUT_DEVICE_ID_ABS_MAX 0x3f -#define INPUT_DEVICE_ID_MSC_MAX 0x07 +#define INPUT_DEVICE_ID_MSC_MAX 0x0d#define INPUT_DEVICE_ID_LED_MAX 0x0f#define INPUT_DEVICE_ID_SND_MAX 0x07#define INPUT_DEVICE_ID_FF_MAX 0x7f
include/uapi/linux/input-event-codes.h +18 -8
@@ -901,14 +901,24 @@* Misc events */ -#define MSC_SERIAL 0x00 -#define MSC_PULSELED 0x01 -#define MSC_GESTURE 0x02 -#define MSC_RAW 0x03 -#define MSC_SCAN 0x04 -#define MSC_TIMESTAMP 0x05 -#define MSC_MAX 0x07 -#define MSC_CNT (MSC_MAX+1) +#define MSC_SERIAL 0x00 +#define MSC_PULSELED 0x01 +#define MSC_GESTURE 0x02 +#define MSC_RAW 0x03 +#define MSC_SCAN 0x04 +#define MSC_TIMESTAMP 0x05 +/* USI Pen events */ +#define MSC_PEN_ID 0x06 +#define MSC_PEN_COLOR 0x07 +#define MSC_PEN_LINE_STYLE_INK 0x08 +#define MSC_PEN_LINE_STYLE_PENCIL 0x09 +#define MSC_PEN_LINE_STYLE_HIGHLIGHTER 0x0a +#define MSC_PEN_LINE_STYLE_CHISEL_MARKER 0x0b +#define MSC_PEN_LINE_STYLE_BRUSH 0x0c +#define MSC_PEN_LINE_STYLE_NO_PREFERENCE 0x0d +/* TODO: Add USI diagnostic & battery events too */ +#define MSC_MAX 0x0d +#define MSC_CNT (MSC_MAX + 1)/* * LEDs
Signature · 1 line
2.25.1
RFC v5 2/2 samples: hid-bpf: add HID USI samples
In reply to Tero KristoThese samples add HID USI client and server code. These are not compiled within kernel tree and are provided strictly as RFC only; the target for these is to build them external to the kernel tree. The provided samples run HID USI client-server communication over D-BUS.
--- samples/bpf/hid_usi.h | 21 ++ samples/bpf/hid_usi_client.c | 154 +++++++++++ samples/bpf/hid_usi_server.c | 438 ++++++++++++++++++++++++++++++ samples/bpf/hid_usi_server_kern.c | 267 ++++++++++++++++++ 4 files changed, 880 insertions(+) create mode 100644 samples/bpf/hid_usi.h create mode 100644 samples/bpf/hid_usi_client.c create mode 100644 samples/bpf/hid_usi_server.c create mode 100644 samples/bpf/hid_usi_server_kern.c
samples/bpf/hid_usi.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 +* +* Copyright(c) 2021 Intel Corporation. +*/ + +#ifndef HID_USI_H_ +#define HID_USI_H_ + +#include <linux/bits.h> + +enum { +USI_PEN_ID =0 , +USI_PEN_IN_RANGE , +USI_PEN_TOUCHING , +USI_PEN_COLOR , +USI_PEN_LINE_WIDTH , +USI_PEN_LINE_STYLE , +USI_NUM_PARAMS +}; + +#endif /* HID_USI_H */
samples/bpf/hid_usi_client.c new file +154 -0
154 lines not on this page · raw message
samples/bpf/hid_usi_server.c new file +438 -0
438 lines not on this page · raw message
samples/bpf/hid_usi_server_kern.c new file +267 -0
267 lines not on this page · raw message
Signature · 1 line
2.25.1
Hi Tero,
Quoted 14 lines · Tero Kristo
Hi,
These two patches add the missing pieces for HID USI support. First one adds the HID core changes to support the new Misc events for pen ID, line color and line style. The second patch adds a BPF program on top of the HID-BPF driver which adds support for writing the Pen parameters from userspace, and to add filtering of HID low level events for ELAN USI controller. The BPF programs are not built by the kernel as of now (there are no
Makefilechanges), as there is a plan to most likely integrate these to a kernel external repository. I have tested these in my own external build setup though, and I can provide the makefile for that if needed. Also a sample client program is provided for communicating with the D-BUS server.
I had a deeper look at the recordings, and I am very worried in what I am seeing:
- the USI parameters seems to be transmitted only after the touch
- the USI parameters takes *a lot* of time to be transmitted (2 bytes every 2 reports)
- the recording of the goodix one starts with a stylus touch without hovering
- the only "reliable" information we get when hovering seems to be the transducer index
So I am wondering a few things:
- what happens when you switch between pens?
* Do we immediately get a different transducer index?
* Are the values right there or do they also take time to be updated?
- on the goodix one, do you still need to issue a get_report on the feature to get the USI parameters, even when you change the pen?
Could you give me the following recording (with an updated hid-tools master branch):
- on the Elan:
* start the recording from a fresh boot (no BPF loaded)
* hover for a few secs the first USI pen you have
* touch it for a few secs
* release, then out of proximity
* approach the other pen
* touch
* release, out of prox
* then once again with the first pen
* then once again with the second pen
- on the goodix: same thing
- on the goodix: same thing but with a BPF program to trigger the GET_REPORT if you can cook one quickly (not a big issue if you can not).
The reason I am asking about those recordings is because with the 2 logs you kindly provided, there is no way we can forward the raw information to userspace. So I am slightly tempted to only rely on a USI manager, in the form of the BPF program in 2/2 to transmit that information to userspace.
If this is bulky just for the first event, then the input events might be OK, we can assume when the application needs those events they will be there.
Quoted 8 lines
I have also a kernel testing branch available at [1], which contains a few fix patches on top of Benjamin's HID-BPF driver work, and is rebased on top of latest hid/for-next. The HID-BPF fixes have been cleaned up a bit compared to previous setup. There are also a couple of new patches for adding support for a
delayed_workBPF program on top of the hid-bpf driver; this is used to execute theraw_requestsin non-irq context.
Thanks for that. I had a very quick look. I thought we could directly use the bpf_timer_* functions instead of having to cook another API. I'll play around with this, but thanks for pushing forward :)
IIRC you asked me when I was counting on submitting the HID BPF work.
So my answer is that I wanted to submit it by the end of 2021, but it looks like I have only one week to finalize this :/
The current missing points are:
- add selftests for all the new API introduced
- review the entire API to not have to deal with a mistake forever
- rebase against bpf-next
One part of the API I am wondering is whether it is good or not to have bpf_hid_foreach_rdesc_item(). This function is complex and we could in theory parse the report descriptor in userspace, even before we load the program. So all the parameters you need in the various raw_event functions could be computed in user space, leading to a much smaller API. The other benefit would be that the API would only deal with arrays of bytes, which is a small enough and versatile enough API :)
Cheers,
Benjamin
Quoted 6 lines
-Tero
Hi Benjamin,
Quoted 16 lines · Benjamin Tissoires
Hi Tero,
Quoted 13 lines · Tero Kristo
Hi,
These two patches add the missing pieces for HID USI support. First one adds the HID core changes to support the new Misc events for pen ID, line color and line style. The second patch adds a BPF program on top of the HID-BPF driver which adds support for writing the Pen parameters from userspace, and to add filtering of HID low level events for ELAN USI controller. The BPF programs are not built by the kernel as of now (there are no
Makefilechanges), as there is a plan to most likely integrate these to a kernel external repository. I have tested these in my own external build setup though, and I can provide the makefile for that if needed. Also a sample client program is provided for communicating with the D-BUS server.
Quoted 3 lines
I had a deeper look at the recordings, and I am very worried in what I am seeing:
- the USI parameters seems to be transmitted only after the touch
Yes, they don't get updated before the pen touches the screen.
Quoted 2 lines
- the USI parameters takes *a lot* of time to be transmitted (2 bytes every 2 reports)
Yes, there appears to be a very large latency when they are transmitted from the pen to controller. Apparently the controller queries each parameter from the pen individually and caches the received values internally, and then passes these out via input reports.
Quoted 1 line
- the recording of the goodix one starts with a stylus touch without hovering
This is a glitch I think, I'll capture a new log.
Quoted 2 lines
- the only "reliable" information we get when hovering seems to be the transducer index
Yes, however even this one is a static constant of "1" for the current controllers.
Quoted 4 lines
So I am wondering a few things:
- what happens when you switch between pens?
* Do we immediately get a different transducer index?
No, the transducer index gets re-allocated, effectively the first pen is going to use transducer index 1, and the second pen is going to re-use that and still report it as 1. This is a highly annoying feature.
Quoted 1 line
* Are the values right there or do they also take time to be updated?
They take time to update. If you are using the same pen, the values are retained in the cache and report correct values immediately, but if you switch pens, they start updating with the same latency as initial pen touch.
Quoted 2 lines
- on the goodix one, do you still need to issue a
get_reporton the feature to get the USI parameters, even when you change the pen?
Yes, goodix reports 0xff,0xff,0x77 for the parameters until a GET_REPORT is issued, after which it notices the parameter that was queried has changed and updates its cached value. Also, see how it reports 0x77, instead of an index to the usages array.
Quoted 26 lines
Could you give me the following recording (with an updated hid-tools master branch):
- on the Elan:
* start the recording from a fresh boot (no BPF loaded)
* hover for a few secs the first USI pen you have
* touch it for a few secs
* release, then out of proximity
* approach the other pen
* touch
* release, out of prox
* then once again with the first pen
* then once again with the second pen- on the goodix: same thing
- on the goodix: same thing but with a BPF program to trigger the
GET_REPORTif you can cook one quickly (not a big issue if you can not).The reason I am asking about those recordings is because with the 2 logs you kindly provided, there is no way we can forward the raw information to userspace. So I am slightly tempted to only rely on a USI manager, in the form of the BPF program in 2/2 to transmit that information to userspace.
Quoted 3 lines
If this is bulky just for the first event, then the input events might be OK, we can assume when the application needs those events they will be there.
See attached tarball. All three logs are in it, I crafted a BPF program for Goodix controller also. Wasn't that bad actually, a few line change so it might be possible to use the same program at least for these two controllers.
Quoted 11 lines
Quoted 8 lines
I have also a kernel testing branch available at [1], which contains a few fix patches on top of Benjamin's HID-BPF driver work, and is rebased on top of latest hid/for-next. The HID-BPF fixes have been cleaned up a bit compared to previous setup. There are also a couple of new patches for adding support for a
delayed_workBPF program on top of the hid-bpf driver; this is used to execute theraw_requestsin non-irq context.Thanks for that. I had a very quick look. I thought we could directly use the bpf_timer_* functions instead of having to cook another API. I'll play around with this, but thanks for pushing forward :)
Yeah, I am kicked by someone on this so want to proceed. ;)
According to documentation, bpf_timer callbacks are executed in soft-irq context, so we can't use it at least for sending the raw_requests which is needed for USI.
Quoted 5 lines
IIRC you asked me when I was counting on submitting the HID BPF work.
So my answer is that I wanted to submit it by the end of 2021, but it looks like I have only one week to finalize this :/
Ok, sounds like you want to submit it probably early next year then. I just wanted to have some sort of ballpark estimate, and this is good enough for me, thanks.
Quoted 5 lines
The current missing points are:
- add selftests for all the new API introduced
- review the entire API to not have to deal with a mistake forever
- rebase against bpf-next
If you need my help on any of these items, let me know.
Quoted 9 lines
One part of the API I am wondering is whether it is good or not to have
bpf_hid_foreach_rdesc_item(). This function is complex and we could in theory parse the report descriptor in userspace, even before we load the program. So all the parameters you need in the variousraw_eventfunctions could be computed in user space, leading to a much smaller API. The other benefit would be that the API would only deal with arrays of bytes, which is a small enough and versatile enough API :)
This might be a good idea. Assuming the BPF programs end up in the hid-tools repo, there could be a generic library for parsing the rdescs also which the userspace programs could use.
-Tero
Quoted 9 lines
Cheers,
BenjaminQuoted 5 lines
-Tero
Quoted 20 lines · Tero Kristo
Hi Benjamin,
Quoted 16 lines · Benjamin Tissoires
Hi Tero,
Quoted 13 lines · Tero Kristo
Hi,
These two patches add the missing pieces for HID USI support. First one adds the HID core changes to support the new Misc events for pen ID, line color and line style. The second patch adds a BPF program on top of the HID-BPF driver which adds support for writing the Pen parameters from userspace, and to add filtering of HID low level events for ELAN USI controller. The BPF programs are not built by the kernel as of now (there are no
Makefilechanges), as there is a plan to most likely integrate these to a kernel external repository. I have tested these in my own external build setup though, and I can provide the makefile for that if needed. Also a sample client program is provided for communicating with the D-BUS server.
Quoted 4 lines
Quoted 3 lines
I had a deeper look at the recordings, and I am very worried in what I am seeing:
- the USI parameters seems to be transmitted only after the touchYes, they don't get updated before the pen touches the screen.
Looking at the logs you provided, and when you switch pens, it seems the serial number in the Elan case is updated while hovering (but it takes 3 seconds to get to that point).
Also, the first time you hovered the pen, the inrange slot was not long enough to be 3 seconds, so maybe that's the time the controller needs to manually update the information.
OTOH, on the Goodix one, if you initiated the GET_REPORT after the first touch, the information came in a few ms.
So I wonder, what if you take the BPF program from the goodix and start querying the information as soon as the pen is hovering in the Elan case. Would that accelerate the time it fetches them?
Quoted 6 lines
Quoted 2 lines
- the USI parameters takes *a lot* of time to be transmitted (2 bytes every 2 reports)
Yes, there appears to be a very large latency when they are transmitted from the pen to controller. Apparently the controller queries each parameter from the pen individually and caches the received values internally, and then passes these out via input reports.
And of course, Goodix doesn't even bother with the serial number.
Quoted 7 lines
Quoted 1 line
- the recording of the goodix one starts with a stylus touch without hovering
This is a glitch I think, I'll capture a new log.
Quoted 2 lines
- the only "reliable" information we get when hovering seems to be the transducer index
Yes, however even this one is a static constant of "1" for the current controllers.
Ouch. Well, that also means that this is used as an index and if the controller were to support more than a pen, the index would be 2 for the second pen when the first is touching...
So this is not reliable for now, but is consistent with a multi-pen scenario.
Quoted 9 lines
Quoted 6 lines
So I am wondering a few things:
- what happens when you switch between pens?
* Do we immediately get a different transducer index?No, the transducer index gets re-allocated, effectively the first pen is going to use transducer index 1, and the second pen is going to re-use that and still report it as 1. This is a highly annoying feature.
Well, that also means that we can ignore this field until we see controllers capable of handling more than one pen.
Quoted 4 lines
Quoted 1 line
* Are the values right there or do they also take time to be updated?
They take time to update. If you are using the same pen, the values are retained in the cache and report correct values immediately, but if you switch pens, they start updating with the same latency as initial pen touch.
That is worrying. The serial number is also cached and we don't know if we are using the same pen or not way too long after the pen touched the surface.
Quoted 7 lines
Quoted 2 lines
- on the goodix one, do you still need to issue a
get_reporton the feature to get the USI parameters, even when you change the pen?Yes, goodix reports 0xff,0xff,0x77 for the parameters until a
GET_REPORTis issued, after which it notices the parameter that was queried has changed and updates its cached value. Also, see how it reports0x77, instead of an index to the usages array.
The 0x77 doesn't bother me too much. This should not trigger an input event because we are out of the usage range.
Quoted 28 lines
Quoted 28 lines
Could you give me the following recording (with an updated hid-tools master branch):
- on the Elan:
* start the recording from a fresh boot (no BPF loaded)
* hover for a few secs the first USI pen you have
* touch it for a few secs
* release, then out of proximity
* approach the other pen
* touch
* release, out of prox
* then once again with the first pen
* then once again with the second pen- on the goodix: same thing
- on the goodix: same thing but with a BPF program to trigger the
GET_REPORTif you can cook one quickly (not a big issue if you can not).The reason I am asking about those recordings is because with the 2 logs you kindly provided, there is no way we can forward the raw information to userspace. So I am slightly tempted to only rely on a USI manager, in the form of the BPF program in 2/2 to transmit that information to userspace.
Quoted 8 lines
Quoted 3 lines
If this is bulky just for the first event, then the input events might be OK, we can assume when the application needs those events they will be there.
See attached tarball. All three logs are in it, I crafted a BPF program for Goodix controller also. Wasn't that bad actually, a few line change so it might be possible to use the same program at least for these two controllers.
\o/
Quoted 14 lines
Quoted 10 lines
Quoted 10 lines
I have also a kernel testing branch available at [1], which contains a few fix patches on top of Benjamin's HID-BPF driver work, and is rebased on top of latest hid/for-next. The HID-BPF fixes have been cleaned up a bit compared to previous setup. There are also a couple of new patches for adding support for a
delayed_workBPF program on top of the hid-bpf driver; this is used to execute theraw_requestsin non-irq context.Quoted 3 lines
Thanks for that. I had a very quick look. I thought we could directly use the bpf_timer_* functions instead of having to cook another API. I'll play around with this, but thanks for pushing forward :)
Quoted 5 lines
Yeah, I am kicked by someone on this so want to proceed. ;)
According to documentation,
bpf_timercallbacks are executed in soft-irq context, so we can't use it at least for sending theraw_requestswhich is needed for USI.
I might be wrong, but I am not sure if soft IRQs are incompatible with hid_hw_raw_request.
Anyway, I gave it a shot today and I am hitting the fact that we lose the BPF context in the timer callback. So we might actually need a new API :/
Quoted 9 lines
Quoted 6 lines
IIRC you asked me when I was counting on submitting the HID BPF work.
So my answer is that I wanted to submit it by the end of 2021, but it looks like I have only one week to finalize this :/
Ok, sounds like you want to submit it probably early next year then. I just wanted to have some sort of ballpark estimate, and this is good enough for me, thanks.
Yeah, sorry for the delay.
Quoted 16 lines
Quoted 5 lines
The current missing points are:
- add selftests for all the new API introduced
- review the entire API to not have to deal with a mistake forever
- rebase against bpf-nextIf you need my help on any of these items, let me know.
Quoted 9 lines
One part of the API I am wondering is whether it is good or not to have
bpf_hid_foreach_rdesc_item(). This function is complex and we could in theory parse the report descriptor in userspace, even before we load the program. So all the parameters you need in the variousraw_eventfunctions could be computed in user space, leading to a much smaller API. The other benefit would be that the API would only deal with arrays of bytes, which is a small enough and versatile enough API :)
Quoted 3 lines
This might be a good idea. Assuming the BPF programs end up in the hid-tools repo, there could be a generic library for parsing the rdescs also which the userspace programs could use.
Oh. Glad to see that this is something you find interesting. Peter Hutterer mentioned that idea and I am still not 100% sure about it, but if you also think it'll make things easier, then why not :)
Cheers,
Benjamin
Quoted 12 lines
-Tero
Quoted 9 lines
Cheers,
BenjaminQuoted 5 lines
-Tero