Skip to contentSkip to conversation

usb hid: reset NumLock

PATCH Pete Zaitcev 6 participants 22 messages Mar 30, 2007 – Apr 5, 2007

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

This is a patch for comments only, please do not apply (at least not as-is). I haven't got the test results yet.

Dell people (Stuart and Charles) complained that on some USB keyboards, if BIOS enables NumLock, it stays on even after Linux has started. Since we always start with NumLock off, this confuses users. Quick double dab at NumLock fixes it, but it's not nice.

The keyboard driver tries to reset LEDs at start, but this never works. Since the bitmap dev->led is zero, the input_event() filters out any attempts to switch LEDs off. So, the code in kbd_start() does nothing.

The Dell's solution looks like this (for 2.6.9 code base):

linux-2.6.9-42.0.3/drivers/char/keyboard.c:
@@ -931,6 +931,19 @@ void kbd_refresh_leds(struct input_handl

tasklet_disable(&keyboard_tasklet);
if (leds != 0xff) {
+ /*
+ * We can't be sure of the state of the LEDs on keyboards
+ * (e.g., BIOS might have left LED_NUML on), so set dev->led
+ * opposite of ledstate, to make sure input_event() actually
+ * sends the command to the keyboard to set each LED.
+ */
+ if (test_bit(LED_SCROLLL, handle->dev->led) == !!(leds & 0x01))
+ change_bit(LED_SCROLLL, handle->dev->led);
+ if (test_bit(LED_NUML, handle->dev->led) == !!(leds & 0x02))
+ change_bit(LED_NUML, handle->dev->led);
+ if (test_bit(LED_CAPSL, handle->dev->led) == !!(leds & 0x04))
+ change_bit(LED_CAPSL, handle->dev->led);
+
input_event(handle->dev, EV_LED, LED_SCROLLL, !!(leds & 0x01));
input_event(handle->dev, EV_LED, LED_NUML, !!(leds & 0x02));
input_event(handle->dev, EV_LED, LED_CAPSL, !!(leds & 0x04));

I didn't like a) layering violation, and b) that they defeat filtering unconditionally. Why have any filtering then?

Instead, I propose for USB HID driver to reset NumLock on probe. Like this:

drivers/usb/input/hid-core.c +22 -1
@@ -458,6 +458,18 @@ static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, un
 	return 0;
 }
 
+static void usbhid_set_leds(struct hid_device *hid, unsigned int code, int val)
+{
+	struct hid_field *field;
+	int offset;
+
+	/* This is often called for the mouse half. */
+	if ((offset = hidinput_find_field(hid, EV_LED, code, &field)) != -1) {
+		hid_set_field(field, offset, val);
+		usbhid_submit_report(hid, field->report, USB_DIR_OUT);
+	}
+}
+
 int usbhid_wait_io(struct hid_device *hid)
 {
 	struct usbhid_device *usbhid = hid->driver_data;
@@ -1328,9 +1340,18 @@ static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
 		return -ENODEV;
 	}
 
-	if ((hid->claimed & HID_CLAIMED_INPUT))
+	if ((hid->claimed & HID_CLAIMED_INPUT)) {
 		hid_ff_init(hid);
 
+		/*
+		 * We do this only if input has claimed the device because
+		 * we can only find fields after they were configured in
+		 * hidinput_connect.
+		 */
+		/* if (hid->quirks & HID_QUIRK_RESET_LEDS) */
+		usbhid_set_leds(hid, LED_NUML, 0);
+	}
+
 	if (hid->quirks & HID_QUIRK_SONY_PS3_CONTROLLER)
 		hid_fixup_sony_ps3_controller(interface_to_usbdev(intf),
 			intf->cur_altsetting->desc.bInterfaceNumber);
include/linux/hid.h +1 -0
index d26b08f..f592f01 100644
@@ -267,6 +267,7 @@ struct hid_item {
 #define HID_QUIRK_SKIP_OUTPUT_REPORTS		0x00020000
 #define HID_QUIRK_IGNORE_MOUSE			0x00040000
 #define HID_QUIRK_SONY_PS3_CONTROLLER		0x00080000
+#define HID_QUIRK_RESET_LEDS			0x00100000
 
 /*
  * This is the global environment of the parser. This information is

URL with details, discussion, rejected patch to read BIOS byte at 0x417:
https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=228674

Jiri, Dmitry, any opinions please?

-- Pete

#1 lore raw

Hi Pete,

Quoted 14 lines · Pete Zaitcev

I didn't like a) layering violation, and b) that they defeat filtering unconditionally. Why have any filtering then?

Instead, I propose for USB HID driver to reset NumLock on probe. Like this:

drivers/usb/input/hid-core.c +2 -0
@@ -458,6 +458,18 @@ static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, un
       return 0;
 }
 
+static void usbhid_set_leds(struct hid_device *hid, unsigned int code, int val)
+{

drivers/usb/input/hid-core.c static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, un · line 472

Quoted 10 lines
drivers/usb/input/hid-core.c +10 -0
+       struct hid_field *field;
+       int offset;
+
+       /* This is often called for the mouse half. */
+       if ((offset = hidinput_find_field(hid, EV_LED, code, &field)) != -1) {
+               hid_set_field(field, offset, val);
+               usbhid_submit_report(hid, field->report, USB_DIR_OUT);
+       }
+}
+

This is fine and that's what we do in atkbd probe but maybe we should move that in input core and reset leds as part of input_register_device()?

Signature · 1 line
Dmitry
#2 lore raw
Quoted 10 lines · Dmitry Torokhov
Quoted 6 lines

I didn't like a) layering violation, and b) that they defeat filtering unconditionally. Why have any filtering then?

Instead, I propose for USB HID driver to reset NumLock on probe. Like this:

--- a/drivers/usb/input/hid-core.c

This is fine and that's what we do in atkbd probe but maybe we should move that in input core and reset leds as part of input_register_device()?

Sure, as long as it works. I think (as much as I understand), that we already attempt to do this indirectly. input_register_device invokes ->start handlers, and the kbd_start attempts to reset LEDs, but fails because of the state filtering.

-- Pete

#3 lore raw
Quoted 13 lines · Pete Zaitcev
Quoted 10 lines · Dmitry Torokhov
Quoted 6 lines

I didn't like a) layering violation, and b) that they defeat filtering unconditionally. Why have any filtering then?

Instead, I propose for USB HID driver to reset NumLock on probe. Like this:

--- a/drivers/usb/input/hid-core.c

This is fine and that's what we do in atkbd probe but maybe we should move that in input core and reset leds as part of input_register_device()?

Quoted 5 lines

Sure, as long as it works. I think (as much as I understand), that we already attempt to do this indirectly. input_register_device invokes ->start handlers, and the kbd_start attempts to reset LEDs, but fails because of the state filtering.

Not exactly... Keyboard handler's start method tries to bring state of new keyboard in sync with the state of the rest of keyboards. The purpose of start() is not to complete hardware initialization but to adjust logical state of the device according to handler's requirements...

But I am backpedaling on my statement about moving it into input core. The driver (HID in this case) should properly reset the device before registering it with input layer.

Signature · 1 line
Dmitry
#4 lore raw
In reply to Pete Zaitcev
Quoted 10 lines · Pete Zaitcev
drivers/usb/input/hid-core.c +3 -1
@@ -1328,9 +1340,18 @@ static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
 		return -ENODEV;
 	}
 
-	if ((hid->claimed & HID_CLAIMED_INPUT))
+	if ((hid->claimed & HID_CLAIMED_INPUT)) {
 		hid_ff_init(hid);
 
+		/*
+		 * We do this only if input has claimed the device because

drivers/usb/input/hid-core.c static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id) · line 1357

Quoted 10 lines
drivers/usb/input/hid-core.c +7 -0
+		 * we can only find fields after they were configured in
+		 * hidinput_connect.
+		 */
+		/* if (hid->quirks & HID_QUIRK_RESET_LEDS) */
+		usbhid_set_leds(hid, LED_NUML, 0);
+	}
+
 	if (hid->quirks & HID_QUIRK_SONY_PS3_CONTROLLER)
 		hid_fixup_sony_ps3_controller(interface_to_usbdev(intf),
 			intf->cur_altsetting->desc.bInterfaceNumber);

Hi Pete,

I think I see an issue here. Imagine that you boot a system initially with one keyboard connected (usb, ps/2, doesn't matter), and after some time you connect second USB keyboard (the NumLock is 'on' on the first keyboard when you connect the second one).

Without your patch, the NumLock led will be OK on the second keyboard immediately. With your patch, the NumLock will be forced to 'off' and it will be out of sync with the first keyboard. The leds will get in sync later when any change occurs.

include/linux/hid.h struct hid_item { · line 270

Quoted 9 lines
include/linux/hid.h +1 -0
index d26b08f..f592f01 100644
@@ -267,6 +267,7 @@ struct hid_item {
 #define HID_QUIRK_SKIP_OUTPUT_REPORTS		0x00020000
 #define HID_QUIRK_IGNORE_MOUSE			0x00040000
 #define HID_QUIRK_SONY_PS3_CONTROLLER		0x00080000
+#define HID_QUIRK_RESET_LEDS			0x00100000

I think this is not worth a quirk - when we get it working properly, why not do it unconditionally for all keyboards?

Quoted 2 lines

URL with details, discussion, rejected patch to read BIOS byte at 0x417:
https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=228674

"You are not authorized to access bug #228674. To see this bug, you must first log in to an account with the appropriate permissions."

:)

Thanks,

Signature · 1 line
Jiri Kosina
#5 lore raw
Quoted 11 lines · Jiri Kosina

On Fri, 30 Mar 2007, Pete Zaitcev wrote:

I think I see an issue here. Imagine that you boot a system initially with one keyboard connected (usb, ps/2, doesn't matter), and after some time you connect second USB keyboard (the NumLock is 'on' on the first keyboard when you connect the second one).

Without your patch, the NumLock led will be OK on the second keyboard immediately. With your patch, the NumLock will be forced to 'off' and it will be out of sync with the first keyboard. The leds will get in sync later when any change occurs.

Oh, right, I missed it. The difficulty is how I rely on usages and fields being already mapped by hidinput_configure_usage(). Thanks for letting me know, I'll think about it.

-- Pete

#6 lore raw
In reply to Jiri Kosina
Quoted 4 lines · Jiri Kosina

I think I see an issue here. Imagine that you boot a system initially with one keyboard connected (usb, ps/2, doesn't matter), and after some time you connect second USB keyboard (the NumLock is 'on' on the first keyboard when you connect the second one).

OK. I toyed with a return to the Stuart's idea: defeat filtering somehow, but it wasn't working well, too much duplication. So, I created a clone of hidinput_find_field() instead. It's still an annoying duplication, but a lesser one, I think.

drivers/usb/input/hid-core.c +48 -0
index ef09952..7338e81 100644
@@ -548,6 +548,28 @@ void usbhid_init_reports(struct hid_device *hid)
 		warn("timeout initializing reports");
 }
 
+/*
+ * Reset LEDs which BIOS might have left on.
+ */
+static int hid_find_field_early(struct hid_device *hid, unsigned int page,
+    unsigned int hid_code, struct hid_field **field);
+
+static void usbhid_set_leds(struct hid_device *hid)
+{
+	struct hid_field *field;
+	int offset;
+
+	/*
+	 * Just reset Num Lock for now.
+	 * This is called for non-keyboard devices too, so no printk if field
+	 * is not found.
+	 */
+	if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
+		hid_set_field(field, offset, 0);
+		usbhid_submit_report(hid, field->report, USB_DIR_OUT);
+	}
+}
+
 #define USB_VENDOR_ID_GTCO		0x078c
 #define USB_DEVICE_ID_GTCO_90		0x0090
 #define USB_DEVICE_ID_GTCO_100		0x0100
@@ -971,6 +993,30 @@ static void hid_find_max_report(struct hid_device *hid, unsigned int type, int *
 	}
 }
 
+static int hid_find_field_early(struct hid_device *hid, unsigned int page,
+    unsigned int hid_code, struct hid_field **pfield)
+{
+	struct hid_report *report;
+	struct hid_field *field;
+	struct hid_usage *usage;
+	int i, j;
+
+	list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
+		for (i = 0; i < report->maxfield; i++) {
+			field = report->field[i];
+			for (j = 0; j < field->maxusage; j++) {
+				usage = &field->usage[j];
+				if ((usage->hid & HID_USAGE_PAGE) == page &&
+				    (usage->hid & 0xFFFF) == hid_code) {
+					*pfield = field;
+					return j;
+				}
+			}
+		}
+	}
+	return -1;
+}
+
 static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
 {
 	struct usbhid_device *usbhid = hid->driver_data;
@@ -1314,6 +1360,8 @@ static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
 
 	usbhid_init_reports(hid);
 	hid_dump_device(hid);
+	/* if (hid->quirks & HID_QUIRK_RESET_LEDS) */
+	usbhid_set_leds(hid);
 
 	if (!hidinput_connect(hid))
 		hid->claimed |= HID_CLAIMED_INPUT;
include/linux/hid.h +1 -0
index d26b08f..f592f01 100644
@@ -267,6 +267,7 @@ struct hid_item {
 #define HID_QUIRK_SKIP_OUTPUT_REPORTS		0x00020000
 #define HID_QUIRK_IGNORE_MOUSE			0x00040000
 #define HID_QUIRK_SONY_PS3_CONTROLLER		0x00080000
+#define HID_QUIRK_RESET_LEDS			0x00100000
 
 /*
  * This is the global environment of the parser. This information is
Quoted 9 lines

Review struct hid_item { · line 270

Quoted 6 lines

+++ b/include/linux/hid.h

(quoted diff) +1 -0
@@ -267,6 +267,7 @@ struct hid_item {
 #define HID_QUIRK_SKIP_OUTPUT_REPORTS		0x00020000
 #define HID_QUIRK_IGNORE_MOUSE			0x00040000
 #define HID_QUIRK_SONY_PS3_CONTROLLER		0x00080000
+#define HID_QUIRK_RESET_LEDS			0x00100000

I think this is not worth a quirk - when we get it working properly, why not do it unconditionally for all keyboards?

The main reason is, I have a USB-to-PS/2 adapter where early acces to LEDs does not work. Apparently, it is still initializing the PS/2 part when it reports to USB that it's ready, and needs a delay. So, I figured that I may be breaking some odd devices. Some may crash or whatnot. This is why I asked Stuart to get me VID/PID for involved keyboards.

Quoted 5 lines
Quoted 2 lines

URL with details, discussion, rejected patch to read BIOS byte at 0x417:
https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=228674

"You are not authorized to access bug #228674. To see this bug, you must first log in to an account with the appropriate permissions."

Sorry. I missed that the bug has access flags set...

-- Pete

#7 lore raw

drivers/usb/input/hid-core.c void usbhid_init_reports(struct hid_device *hid) · line 555

Quoted 13 lines · Pete Zaitcev
drivers/usb/input/hid-core.c +5 -0
index ef09952..7338e81 100644
@@ -548,6 +548,28 @@ void usbhid_init_reports(struct hid_device *hid)
 		warn("timeout initializing reports");
 }
 
+/*
+ * Reset LEDs which BIOS might have left on.
+ */
+static int hid_find_field_early(struct hid_device *hid, unsigned int page,
+    unsigned int hid_code, struct hid_field **field);

Hi Pete,

could you please change the order of the two functions, so that you don't have to put the forward declaration here?

drivers/usb/input/hid-core.c void usbhid_init_reports(struct hid_device *hid) · line 566

Quoted 5 lines
drivers/usb/input/hid-core.c +5 -0
+	/*
+	 * Just reset Num Lock for now.
+	 * This is called for non-keyboard devices too, so no printk if field
+	 * is not found.
+	 */

I'd say this is a little bit overcommented.

Quoted 5 lines

The main reason is, I have a USB-to-PS/2 adapter where early acces to LEDs does not work. Apparently, it is still initializing the PS/2 part when it reports to USB that it's ready, and needs a delay. So, I figured that I may be breaking some odd devices. Some may crash or whatnot. This is why I asked Stuart to get me VID/PID for involved keyboards.

Fair enough, thanks.

Besides the trivial nitpicks above, I think that this fixed version is fine. So as soon as you have the VIDs and PIDs of the hardware which requires this, could you please update the patch and send it to me again?

Thanks,

Signature · 1 line
Jiri Kosina
#8 lore raw
Quoted 9 lines · Jiri Kosina

On Sun, 1 Apr 2007, Pete Zaitcev wrote:

could you please change the order of the two functions, so that you don't have to put the forward declaration here?
[...]
I'd say this is a little bit overcommented.
[...]
So as soon as you have the VIDs and PIDs of the hardware which requires this, could you please update the patch and send it to me again?

How about this?

drivers/usb/input/hid-core.c +46 -0
index 827a75a..23b1e70 100644
@@ -545,6 +545,45 @@ void usbhid_init_reports(struct hid_device *hid)
 		warn("timeout initializing reports");
 }
 
+/*
+ * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
+ */
+
+static int hid_find_field_early(struct hid_device *hid, unsigned int page,
+    unsigned int hid_code, struct hid_field **pfield)
+{
+	struct hid_report *report;
+	struct hid_field *field;
+	struct hid_usage *usage;
+	int i, j;
+
+	list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
+		for (i = 0; i < report->maxfield; i++) {
+			field = report->field[i];
+			for (j = 0; j < field->maxusage; j++) {
+				usage = &field->usage[j];
+				if ((usage->hid & HID_USAGE_PAGE) == page &&
+				    (usage->hid & 0xFFFF) == hid_code) {
+					*pfield = field;
+					return j;
+				}
+			}
+		}
+	}
+	return -1;
+}
+
+static void usbhid_set_leds(struct hid_device *hid)
+{
+	struct hid_field *field;
+	int offset;
+
+	if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
+		hid_set_field(field, offset, 0);
+		usbhid_submit_report(hid, field->report, USB_DIR_OUT);
+	}
+}
+
 #define USB_VENDOR_ID_GTCO		0x078c
 #define USB_DEVICE_ID_GTCO_90		0x0090
 #define USB_DEVICE_ID_GTCO_100		0x0100
@@ -765,6 +804,9 @@ void usbhid_init_reports(struct hid_device *hid)
 #define USB_VENDOR_ID_SONY			0x054c
 #define USB_DEVICE_ID_SONY_PS3_CONTROLLER	0x0268
 
+#define USB_VENDOR_ID_DELL		0x413c
+#define USB_DEVICE_ID_DELL_W7658	0x2005
+
 /*
  * Alphabetically sorted blacklist by quirk type.
  */
@@ -947,6 +989,8 @@ static const struct hid_blacklist {
 
 	{ USB_VENDOR_ID_CIDC, 0x0103, HID_QUIRK_IGNORE },
 
+	{ USB_VENDOR_ID_DELL, USB_DEVICE_ID_DELL_W7658, HID_QUIRK_RESET_LEDS },
+
 	{ 0, 0 }
 };
 
@@ -1334,6 +1378,8 @@ static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
 
 	usbhid_init_reports(hid);
 	hid_dump_device(hid);
+	if (hid->quirks & HID_QUIRK_RESET_LEDS)
+		usbhid_set_leds(hid);
 
 	if (!hidinput_connect(hid))
 		hid->claimed |= HID_CLAIMED_INPUT;
include/linux/hid.h +1 -0
index 8c97d4d..3e8dcb0 100644
@@ -269,6 +269,7 @@ struct hid_item {
 #define HID_QUIRK_SONY_PS3_CONTROLLER		0x00080000
 #define HID_QUIRK_LOGITECH_S510_DESCRIPTOR	0x00100000
 #define HID_QUIRK_DUPLICATE_USAGES		0x00200000
+#define HID_QUIRK_RESET_LEDS			0x00400000
 
 /*
  * This is the global environment of the parser. This information is

I wasn't sure where to place the function, so I just put it above its user, to signify that it's special-casing. Also, it's unclear where to put the quirk entry and defines. There's a comment saying that they are sorted alphabetically by quirk, but apparently the order was violated with more recent additions.

-- Pete

#9 lore raw
Quoted 11 lines · Pete Zaitcev
Quoted 9 lines · Jiri Kosina

On Sun, 1 Apr 2007, Pete Zaitcev wrote:

could you please change the order of the two functions, so that you don't have to put the forward declaration here?
[...]
I'd say this is a little bit overcommented.
[...]
So as soon as you have the VIDs and PIDs of the hardware which requires this, could you please update the patch and send it to me again?

Quoted 1 line

How about this?

Actually I think I will be adding the patch below, but it has to wait till 2.6.22 as it requires input core to struct device conversion patch.

What do you think?

Signature · 117 lines
Dmitry

Input: add generic suspend and resume for uinput devices

Automatically turn off leds and sound effects as part of suspend
process and restore led state, sounds and repeat rate at resume.

Also synchronize hardware state with logical state at device
registration.

Signed-off-by: Dmitry Torokhov <[email protected]>
---

 drivers/input/input.c |   80 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 80 insertions(+)

Index: work/drivers/input/input.c
===================================================================
--- work.orig/drivers/input/input.c
+++ work/drivers/input/input.c
@@ -997,10 +997,88 @@ static int input_dev_uevent(struct devic
 	return 0;
 }
 
+static void input_dev_toggle(struct input_dev *dev,
+			     unsigned int type, unsigned int code,
+			     unsigned long *cap_bits, unsigned long *bits,
+			     int force_off)
+{
+	if (test_bit(code, cap_bits)) {
+		if (!force_off)
+			dev->event(dev, type, code, test_bit(code, bits));
+		else if (test_bit(code, bits))
+			dev->event(dev, type, code, 0);
+	}
+}
+
+static void input_dev_reset(struct input_dev *dev, int force_off)
+{
+	int i;
+
+	if (!dev->event)
+		return;
+
+	/* synchronize led state */
+	if (test_bit(EV_LED, dev->evbit))
+		for (i = 0; i <= LED_MAX; i++)
+			input_dev_toggle(dev, EV_LED, i,
+					 dev->ledbit, dev->led, force_off);
+
+	/* restore sound */
+	if (test_bit(EV_SND, dev->evbit))
+		for (i = 0; i <= SND_MAX; i++)
+			input_dev_toggle(dev, EV_SND, i,
+					 dev->sndbit, dev->snd, force_off);
+
+	if (!force_off && test_bit(EV_REP, dev->evbit)) {
+		dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
+		dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
+	}
+}
+
+#ifdef CONFIG_PM
+static int input_dev_suspend(struct device *dev, pm_message_t state)
+{
+	struct input_dev *input_dev = to_input_dev(dev);
+
+	mutex_lock(&input_dev->mutex);
+
+	if (dev->power.power_state.event != state.event) {
+		if (state.event == PM_EVENT_SUSPEND)
+			input_dev_reset(input_dev, 1);
+
+		dev->power.power_state = state;
+	}
+
+	mutex_unlock(&input_dev->mutex);
+
+	return 0;
+}
+
+static int input_dev_resume(struct device *dev)
+{
+	struct input_dev *input_dev = to_input_dev(dev);
+
+	mutex_lock(&input_dev->mutex);
+
+	if (dev->power.power_state.event != PM_EVENT_ON)
+		input_dev_reset(to_input_dev(dev), 0);
+
+	dev->power.power_state = PMSG_ON;
+
+	mutex_unlock(&input_dev->mutex);
+
+	return 0;
+}
+#endif /* CONFIG_PM */
+
 static struct device_type input_dev_type = {
 	.groups		= input_dev_attr_groups,
 	.release	= input_dev_release,
 	.uevent		= input_dev_uevent,
+#ifdef CONFIG_PM
+	.suspend	= input_dev_suspend,
+	.resume		= input_dev_resume,
+#endif
 };
 
 struct class input_class = {
@@ -1080,6 +1158,8 @@ int input_register_device(struct input_d
 		dev->rep[REP_PERIOD] = 33;
 	}
 
+	input_dev_reset(dev, 0);
+
 	if (!dev->getkeycode)
 		dev->getkeycode = input_default_getkeycode;
#10 lore raw
Quoted 1 line · Dmitry Torokhov

What do you think?

The patch looks sane, although I haven't yet tested it. I'll live with it until next quarterly update, then consider if I should take it or use my patch for RHEL 5 and RHEL 4.

-- Pete

#11 lore raw
In reply to Pete Zaitcev
Quoted 1 line · Pete Zaitcev

How about this?

Looks quite fine to me.

But in case that Dmitry's patch "Input: add generic suspend and resume for uinput devices" fixes your issue too, I wouldn't merge it as it won't be needed. Could you please let me know?

drivers/usb/input/hid-core.c static const struct hid_blacklist { · line 993

Quoted 6 lines
drivers/usb/input/hid-core.c +2 -0
@@ -947,6 +989,8 @@ static const struct hid_blacklist {
 
 	{ USB_VENDOR_ID_CIDC, 0x0103, HID_QUIRK_IGNORE },
 
+	{ USB_VENDOR_ID_DELL, USB_DEVICE_ID_DELL_W7658, HID_QUIRK_RESET_LEDS },
+

As this quirk is only needed once in the initialization method, we could probably get rid of it and just put and explicit test for PID and VID there (with apropriate comment). We can't afford wasting quirk entries, as we are slowly running out of them.

Depends on whether this is going to be needed for > 1 PID.

Quoted 5 lines

I wasn't sure where to place the function, so I just put it above its user, to signify that it's special-casing. Also, it's unclear where to put the quirk entry and defines. There's a comment saying that they are sorted alphabetically by quirk, but apparently the order was violated with more recent additions.

Yep, the blacklist is ugly. I have a patch moving it to the proper place and rearranging it to be sorted again, queued for 2.6.22.

Thanks,

Signature · 1 line
Jiri Kosina
#12 lore raw
Quoted 1 line · Jiri Kosina

As this quirk is only needed once in the initialization method, we could probably get rid of it and just put and explicit test for PID and VID there (with apropriate comment). We can't afford wasting quirk entries, as we are slowly running out of them.

Is it a problem of table size or number of table entries? For the number of entries a simple change to PID ranges should help. It would also allow to move Wacom to the blacklist.
I have sent a patch for that already, but it was rejected by Greg.

#13 lore raw
Quoted 8 lines · Robert Marquardt
Quoted 4 lines

As this quirk is only needed once in the initialization method, we could probably get rid of it and just put and explicit test for PID and VID there (with apropriate comment). We can't afford wasting quirk entries, as we are slowly running out of them.

Is it a problem of table size or number of table entries? For the number of entries a simple change to PID ranges should help. It would also allow to move Wacom to the blacklist. I have sent a patch for that already, but it was rejected by Greg.

The issue is that 32 bits of the quirk bitmask are going to be taken by the quirk entries (so no, it's not related to the size of the table).

Thanks,

Signature · 1 line
Jiri Kosina
#14 lore raw
In reply to Jiri Kosina
Quoted 9 lines · Jiri Kosina
Quoted 1 line · Pete Zaitcev

How about this?

Looks quite fine to me.

But in case that Dmitry's patch "Input: add generic suspend and resume for uinput devices" fixes your issue too, I wouldn't merge it as it won't be needed. Could you please let me know?

Unfortunately my patch is crap. We should not be sending events down dev->event() until dev->open() has been called because many drivers start hardware from there and not ready until then.

So it is HID driver responsibility to properly reset leds after all.

Signature · 1 line
Dmitry
#15 lore raw
Quoted 4 lines · Dmitry Torokhov

Unfortunately my patch is crap. We should not be sending events down dev->event() until dev->open() has been called because many drivers start hardware from there and not ready until then. So it is HID driver responsibility to properly reset leds after all.

Pete, could you please resend your patch, with proper metadata, we need to merge your one then.

Thanks,

Signature · 1 line
Jiri Kosina
#16 lore raw

On a certain keyboard, when BIOS sets NumLock LED on, it survives the takeover by Linux and thus confuses users.

Eating of an increasibly scarce quirk bit is unfortunate. We do it for safety, given the history of nervous input devices which crash if anything unusual happens.

---

drivers/usb/input/hid-core.c +46 -0
index 827a75a..23b1e70 100644
@@ -545,6 +545,45 @@ void usbhid_init_reports(struct hid_device *hid)
 		warn("timeout initializing reports");
 }
 
+/*
+ * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
+ */
+
+static int hid_find_field_early(struct hid_device *hid, unsigned int page,
+    unsigned int hid_code, struct hid_field **pfield)
+{
+	struct hid_report *report;
+	struct hid_field *field;
+	struct hid_usage *usage;
+	int i, j;
+
+	list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
+		for (i = 0; i < report->maxfield; i++) {
+			field = report->field[i];
+			for (j = 0; j < field->maxusage; j++) {
+				usage = &field->usage[j];
+				if ((usage->hid & HID_USAGE_PAGE) == page &&
+				    (usage->hid & 0xFFFF) == hid_code) {
+					*pfield = field;
+					return j;
+				}
+			}
+		}
+	}
+	return -1;
+}
+
+static void usbhid_set_leds(struct hid_device *hid)
+{
+	struct hid_field *field;
+	int offset;
+
+	if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
+		hid_set_field(field, offset, 0);
+		usbhid_submit_report(hid, field->report, USB_DIR_OUT);
+	}
+}
+
 #define USB_VENDOR_ID_GTCO		0x078c
 #define USB_DEVICE_ID_GTCO_90		0x0090
 #define USB_DEVICE_ID_GTCO_100		0x0100
@@ -765,6 +804,9 @@ void usbhid_init_reports(struct hid_device *hid)
 #define USB_VENDOR_ID_SONY			0x054c
 #define USB_DEVICE_ID_SONY_PS3_CONTROLLER	0x0268
 
+#define USB_VENDOR_ID_DELL		0x413c
+#define USB_DEVICE_ID_DELL_W7658	0x2005
+
 /*
  * Alphabetically sorted blacklist by quirk type.
  */
@@ -947,6 +989,8 @@ static const struct hid_blacklist {
 
 	{ USB_VENDOR_ID_CIDC, 0x0103, HID_QUIRK_IGNORE },
 
+	{ USB_VENDOR_ID_DELL, USB_DEVICE_ID_DELL_W7658, HID_QUIRK_RESET_LEDS },
+
 	{ 0, 0 }
 };
 
@@ -1334,6 +1378,8 @@ static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
 
 	usbhid_init_reports(hid);
 	hid_dump_device(hid);
+	if (hid->quirks & HID_QUIRK_RESET_LEDS)
+		usbhid_set_leds(hid);
 
 	if (!hidinput_connect(hid))
 		hid->claimed |= HID_CLAIMED_INPUT;
include/linux/hid.h +1 -0
index 8c97d4d..3e8dcb0 100644
@@ -269,6 +269,7 @@ struct hid_item {
 #define HID_QUIRK_SONY_PS3_CONTROLLER		0x00080000
 #define HID_QUIRK_LOGITECH_S510_DESCRIPTOR	0x00100000
 #define HID_QUIRK_DUPLICATE_USAGES		0x00200000
+#define HID_QUIRK_RESET_LEDS			0x00400000
 
 /*
  * This is the global environment of the parser. This information is
#17 lore raw
Quoted 7 lines · Pete Zaitcev

On a certain keyboard, when BIOS sets NumLock LED on, it survives the takeover by Linux and thus confuses users.

Eating of an increasibly scarce quirk bit is unfortunate. We do it for safety, given the history of nervous input devices which crash if anything unusual happens.

You know, I would not call turning leds on an off an unusual operation for a keyboard.

Signature · 1 line
Dmitry
#18 lore raw
Quoted 9 lines · Dmitry Torokhov
Quoted 6 lines

On a certain keyboard, when BIOS sets NumLock LED on, it survives the takeover by Linux and thus confuses users.

Eating of an increasibly scarce quirk bit is unfortunate. We do it for safety, given the history of nervous input devices which crash if anything unusual happens.

You know, I would not call turning leds on an off an unusual operation for a keyboard.

Right, but monkeying with them immediately upon initialization may be "unusual", meaning "device was tested with one version of Windows and shipped, everything else is unusual".

-- Pete

#19 lore raw
In reply to Pete Zaitcev
Quoted 4 lines · Pete Zaitcev

Dell people (Stuart and Charles) complained that on some USB keyboards, if BIOS enables NumLock, it stays on even after Linux has started. Since we always start with NumLock off, this confuses users. Quick double dab at NumLock fixes it, but it's not nice.

What I am seeing on my Thinkpad is that when I boot _without_ an USB keyboard NumLock is enabled. Switching to virtual console and back to X fixes it which is why I have never bothered to debug it further. Perhaps this is related? Should I give your patch a spin to see if it fixes the problem?

#20 lore raw
Quoted 7 lines · Pekka Enberg
Quoted 4 lines · Pete Zaitcev

Dell people (Stuart and Charles) complained that on some USB keyboards, if BIOS enables NumLock, it stays on even after Linux has started. Since we always start with NumLock off, this confuses users. Quick double dab at NumLock fixes it, but it's not nice.

Quoted 5 lines

What I am seeing on my Thinkpad is that when I boot _without_ an USB keyboard NumLock is enabled. Switching to virtual console and back to X fixes it which is why I have never bothered to debug it further. Perhaps this is related? Should I give your patch a spin to see if it fixes the problem?

It's related, but not the same problem. Perhaps the initialization in atkbd which Dmitry mentioned is not complete. Jiri found a fatal bug in my patch, but even assuming that it worked, it still would do nothing to you.

-- Pete

#21 lore raw
In reply to Pekka Enberg

Hi Pekka,

Quoted 6 lines · Pekka Enberg
Quoted 4 lines · Pete Zaitcev

Dell people (Stuart and Charles) complained that on some USB keyboards, if BIOS enables NumLock, it stays on even after Linux has started. Since we always start with NumLock off, this confuses users. Quick double dab at NumLock fixes it, but it's not nice.

Quoted 6 lines

What I am seeing on my Thinkpad is that when I boot _without_ an USB keyboard NumLock is enabled. Switching to virtual console and back to X fixes it which is why I have never bothered to debug it further. Perhaps this is related? Should I give your patch a spin to see if it fixes the problem?

Are you saying that NumLock LED is lit or that keyboard is in NumLock state? Does the same happen if you boot with init=/bin/bash?

Signature · 1 line
Dmitry