Skip to contentSkip to conversation

HID: hid-input: avoid splitting keyboard, system and consumer controls

PATCH Applied Dmitry Torokhov 4 participants 5 messages Jan 14, 2021 – Feb 2, 2021 HID

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 HID: hid-input: avoid splitting keyboard, system and consumer controls

A typical USB keyboard usually splits its keys into several reports:

- one for the basic alphanumeric keys, modifier keys, F<n> keys, six pack keys and keypad. This report's application is normally listed as GenericDesktop.Keyboard
- a GenericDesktop.SystemControl report for the system control keys, such as power and sleep
- Consumer.ConsumerControl report for multimedia (forward, rewind, play/pause, mute, etc) and other extended keys.
- additional output, vendor specific, and feature reports

Splitting each report into a separate input device is wasteful and even hurts userspace as it makes it harder to determine the true capabilities (set of available keys) of a keyboard, so let's adjust application matching to merge system control and consumer control reports with keyboard report, if one has already been processed.

---
 drivers/hid/hid-input.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/hid/hid-input.c +10 -0
index f797659cb9d9..df45d8d07dc2 100644
@@ -1851,6 +1851,16 @@ static struct hid_input *hidinput_match_application(struct hid_report *report)
 	list_for_each_entry(hidinput, &hid->inputs, list) {
 		if (hidinput->application == report->application)
 			return hidinput;
+
+		/*
+		 * Keep SystemControl and ConsumerControl applications together
+		 * with the main keyboard, if present.
+		 */
+		if ((report->application == HID_GD_SYSTEM_CONTROL ||
+		     report->application == HID_CP_CONSUMER_CONTROL) &&
+		    hidinput->application == HID_GD_KEYBOARD) {
+			return hidinput;
+		}
 	}
 
 	return NULL;
Signature · 5 lines
2.30.0.284.gd98b1dd5eaa7-goog


-- 
Dmitry
#1 lore raw

Hi Dmitry,

Quoted 29 lines · Dmitry Torokhov

A typical USB keyboard usually splits its keys into several reports:

- one for the basic alphanumeric keys, modifier keys, F<n> keys, six pack keys and keypad. This report's application is normally listed as GenericDesktop.Keyboard
- a GenericDesktop.SystemControl report for the system control keys, such as power and sleep
- Consumer.ConsumerControl report for multimedia (forward, rewind, play/pause, mute, etc) and other extended keys.
- additional output, vendor specific, and feature reports

Splitting each report into a separate input device is wasteful and even hurts userspace as it makes it harder to determine the true capabilities (set of available keys) of a keyboard, so let's adjust application matching to merge system control and consumer control reports with keyboard report, if one has already been processed.

---
 drivers/hid/hid-input.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/hid/hid-input.c +0 -0
index f797659cb9d9..df45d8d07dc2 100644
@@ -1851,6 +1851,16 @@ static struct hid_input *hidinput_match_application(struct hid_report *report)
        list_for_each_entry(hidinput, &hid->inputs, list) {

drivers/hid/hid-input.c static struct hid_input *hidinput_match_application(struct hid_report *report) · line 1861

Quoted 10 lines
drivers/hid/hid-input.c +8 -0
                if (hidinput->application == report->application)
                        return hidinput;
+
+               /*
+                * Keep SystemControl and ConsumerControl applications together
+                * with the main keyboard, if present.
+                */
+               if ((report->application == HID_GD_SYSTEM_CONTROL ||
+                    report->application == HID_CP_CONSUMER_CONTROL) &&
+                   hidinput->application == HID_GD_KEYBOARD) {

I am not fundamentally against the patch, but I think that if the device exposes first a HID_CP_CONSUMER_CONTROL and then a HID_GD_KEYBOARD we will end up with 2 different input nodes. We likely need to "convert" HID_GD_SYSTEM_CONTROL and HID_CP_CONSUMER_CONTROL to HID_GD_KEYBOARD when creating the hidinput.

Cheers,
Benjamin

Quoted 12 lines
drivers/hid/hid-input.c +2 -1
+                       return hidinput;
+               }
        }
 
        return NULL;
--

2.30.0.284.gd98b1dd5eaa7-goog

--
Dmitry

#2 lore raw

Hi Benjamin,

Quoted 41 lines · Benjamin Tissoires

Hi Dmitry,

Quoted 37 lines · Dmitry Torokhov

A typical USB keyboard usually splits its keys into several reports:

- one for the basic alphanumeric keys, modifier keys, F<n> keys, six pack keys and keypad. This report's application is normally listed as GenericDesktop.Keyboard
- a GenericDesktop.SystemControl report for the system control keys, such as power and sleep
- Consumer.ConsumerControl report for multimedia (forward, rewind, play/pause, mute, etc) and other extended keys.
- additional output, vendor specific, and feature reports

Splitting each report into a separate input device is wasteful and even hurts userspace as it makes it harder to determine the true capabilities (set of available keys) of a keyboard, so let's adjust application matching to merge system control and consumer control reports with keyboard report, if one has already been processed.

---
 drivers/hid/hid-input.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/hid/hid-input.c +6 -0
index f797659cb9d9..df45d8d07dc2 100644
@@ -1851,6 +1851,16 @@ static struct hid_input *hidinput_match_application(struct hid_report *report)
        list_for_each_entry(hidinput, &hid->inputs, list) {
                if (hidinput->application == report->application)
                        return hidinput;
+
+               /*
+                * Keep SystemControl and ConsumerControl applications together
+                * with the main keyboard, if present.
+                */
+               if ((report->application == HID_GD_SYSTEM_CONTROL ||
Quoted 8 lines

drivers/hid/hid-input.c static struct hid_input *hidinput_match_application(struct hid_report *report) · line 1861

Quoted 2 lines
drivers/hid/hid-input.c +2 -0
+                    report->application == HID_CP_CONSUMER_CONTROL) &&
+                   hidinput->application == HID_GD_KEYBOARD) {

I am not fundamentally against the patch, but I think that if the device exposes first a HID_CP_CONSUMER_CONTROL and then a HID_GD_KEYBOARD we will end up with 2 different input nodes. We likely need to "convert" HID_GD_SYSTEM_CONTROL and HID_CP_CONSUMER_CONTROL to HID_GD_KEYBOARD when creating the hidinput.

While it is technically possible that consumer control or system control comes first, before main keyboard report, in reality all keyboards that I have seen so far have the main keyboard report first, so I opted not to handle the uncommon case to keep the code simple.

I could add the above as a comment, and we could wait to see if there are devices that are exceptions to the common practice, or I can go and try to implement the conversion if you feel it is required. Please let me know.

Note that we will still end up with 2+ input nodes if a device uses several USB interfaces, but we can't really do much about such cases (well, short of having a specialized driver claiming additional interfaces while probing the first one, but that is really outside of scope of this patch).

Thanks!

Signature · 1 line
Dmitry
Acked-by on v1 1/1
#3 lore raw
In reply to Dmitry Torokhov
Quoted 11 lines · Dmitry Torokhov

A typical USB keyboard usually splits its keys into several reports:

- one for the basic alphanumeric keys, modifier keys, F<n> keys, six pack keys and keypad. This report's application is normally listed as GenericDesktop.Keyboard
- a GenericDesktop.SystemControl report for the system control keys, such as power and sleep
- Consumer.ConsumerControl report for multimedia (forward, rewind, play/pause, mute, etc) and other extended keys.
- additional output, vendor specific, and feature reports

Quoted 7 lines

Splitting each report into a separate input device is wasteful and even hurts userspace as it makes it harder to determine the true capabilities (set of available keys) of a keyboard, so let's adjust application matching to merge system control and consumer control reports with keyboard report, if one has already been processed.

I think, let's see if there's any fallout from that :)

Cheers,
Peter

maintainer Applied
#4 lore raw
In reply to Dmitry Torokhov
Quoted 11 lines · Dmitry Torokhov

A typical USB keyboard usually splits its keys into several reports:

- one for the basic alphanumeric keys, modifier keys, F<n> keys, six pack keys and keypad. This report's application is normally listed as GenericDesktop.Keyboard
- a GenericDesktop.SystemControl report for the system control keys, such as power and sleep
- Consumer.ConsumerControl report for multimedia (forward, rewind, play/pause, mute, etc) and other extended keys.
- additional output, vendor specific, and feature reports

Quoted 8 lines

Splitting each report into a separate input device is wasteful and even hurts userspace as it makes it harder to determine the true capabilities (set of available keys) of a keyboard, so let's adjust application matching to merge system control and consumer control reports with keyboard report, if one has already been processed.

---

Queued in for-5.12/core. Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs