full usbhid autosuspend
Hi,
this is a full implementation of autosuspend for USB HID devices. Contrary to the current implementation it tries to safe power even for open devices, like keyboards and mice. Please test.
It is against 2.6.25-rc7 plus the patch closing the disconnect/open race which I've attached for your convinience.
Regards
Oliver
---
drivers/hid/hid-core.c +18 -0
@@ -1001,6 +1001,24 @@ int hid_input_report(struct hid_device * }EXPORT_SYMBOL_GPL (hid_input_report); +#define NBITS (x) ((((x)-1)/BITS_PER_LONG)+1) + +int hid_check_keys_pressed (struct hid_device *hid) +{ +struct hid_input *hidinput; +int i; + +list_for_each_entry (hidinput, &hid->inputs, list) { +for (i =0 ; i <NBITS (KEY_MAX ); i++) +if (hidinput->input->key[i]) +return 1 ; + } + +return 0 ; +} + +EXPORT_SYMBOL_GPL (hid_check_keys_pressed); +static int __init hid_init (void ) {return hidraw_init ();
drivers/hid/usbhid/hid-core.c +301 -71
614 lines not on this page · raw message
drivers/hid/usbhid/hiddev.c +12 -4
@@ -249,10 +249,12 @@ static int hiddev_release(struct inode *spin_unlock_irqrestore (&list->hiddev->list_lock, flags);if (!--list->hiddev->open) { -if (list->hiddev->exist) +if (list->hiddev->exist) {usbhid_close (list->hiddev->hid); -else +usbhid_put_power (list->hiddev->hid); + }else {kfree (list->hiddev); + } }kfree (list);
@@ -267,6 +269,7 @@ static int hiddev_open(struct inode *ino {struct hiddev_list *list;unsigned long flags; +int res;int i =iminor (inode) -HIDDEV_MINOR_BASE ;
@@ -285,8 +288,13 @@ static int hiddev_open(struct inode *ino file->private_data = list;if (!list->hiddev->open++) -if (list->hiddev->exist) -usbhid_open (hiddev_table[i]->hid); +if (list->hiddev->exist) { +struct hid_device *hid = hiddev_table[i]->hid; + res =usbhid_get_power (hid); +if (res <0 ) +return -EIO ; +usbhid_open (hid); + }return 0 ; }
drivers/hid/usbhid/usbhid.h +6 -3
@@ -36,7 +36,10 @@ int usbhid_wait_io(struct hid_device* hivoid usbhid_close (struct hid_device *hid);int usbhid_open (struct hid_device *hid);void usbhid_init_reports (struct hid_device *hid); -void usbhid_submit_report (struct hid_device *hid,struct hid_report *report,unsigned char dir); +void usbhid_submit_report +(struct hid_device *hid,struct hid_report *report,unsigned char dir); +int usbhid_get_power (struct hid_device *hid); +void usbhid_put_power (struct hid_device *hid);/* * USB-specific HID struct, to be pointed to
@@ -70,14 +73,14 @@ struct usbhid_device {unsigned char outhead, outtail;/* Output pipe fifo head & tail */ char *outbuf;/* Output buffer */ dma_addr_t outbuf_dma;/* Output buffer dma */ -spinlock_t outlock;/* Output fifo spinlock */ +spinlock_t outputlock;/* Output fifo spinlock */ unsigned long iofl;/* I/O flags (CTRL_RUNNING, OUT_RUNNING) */ struct timer_list io_retry;/* Retry timer */ unsigned long stop_retry;/* Time to give up, in jiffies */ unsigned int retry_delay;/* Delay length in ms */ struct work_struct reset_work;/* Task context for resets */ - +struct work_struct restart_work;/* waking up for output to be done in a task */ };#define hid_to_usb_dev (hid_dev) \
include/linux/hid.h +3 -0
@@ -422,6 +422,8 @@ struct hid_control_fifo {#define HID_SUSPENDED 5#define HID_CLEAR_HALT 6#define HID_DISCONNECTED 7 +#define HID_REPORTED_IDLE 8 +#define HID_KEYS_PRESSED 9struct hid_input {struct list_head list;
@@ -535,6 +537,7 @@ void hid_input_field(struct hid_device *void hid_output_report (struct hid_report *report,__u8 *data);void hid_free_device (struct hid_device *device);struct hid_device *hid_parse_report (__u8 *start,unsigned size); +int hid_check_keys_pressed (struct hid_device *hid);/* HID quirks API */ u32 usbhid_lookup_quirk (const u16 idVendor,const u16 idProduct);
drivers/hid/usbhid/hid-core.c +3 -1
@@ -82,6 +82,7 @@spin_lock_irqsave (&usbhid->inlock, flags);if (hid->open >0 && !test_bit (HID_SUSPENDED , &usbhid->iofl) && + !test_bit (HID_DISCONNECTED , &usbhid->iofl) && !test_and_set_bit (HID_IN_RUNNING , &usbhid->iofl)) { rc =usb_submit_urb (usbhid->urbin,GFP_ATOMIC );if (rc !=0 )
@@ -155,7 +156,7 @@spin_lock_irqsave (&usbhid->inlock, flags);/* Stop when disconnected */ -if (usb_get_intfdata (usbhid->intf) ==NULL ) +if (test_bit (HID_DISCONNECTED , &usbhid->iofl)) goto done;/* If it has been a while since the last error, we'll assume
@@ -932,6 +933,7 @@spin_lock_irq (&usbhid->inlock);/* Sync with error handler */ usb_set_intfdata (intf,NULL ); +set_bit (HID_DISCONNECTED , &usbhid->iofl);spin_unlock_irq (&usbhid->inlock);usb_kill_urb (usbhid->urbin);usb_kill_urb (usbhid->urbout);
include/linux/hid.h +1 -0
@@ -421,6 +421,7 @@#define HID_RESET_PENDING 4#define HID_SUSPENDED 5#define HID_CLEAR_HALT 6 +#define HID_DISCONNECTED 7struct hid_input {struct list_head list;
A message that is not in the archive (sent off-list or not yet received): 200804011603.05998.oliver-GvhC2dPhHPQdnm+yROfE0A@public.gmane.org
Quoted 3 lines · Oliver Neukum
this is a full implementation of autosuspend for USB HID devices. Contrary to the current implementation it tries to safe power even for open devices, like keyboards and mice. Please test.
Hi Oliver,
there is some deadlock in IRQ path, could you please look at it? I have triggered it on my test system trivially by just attaching one PS2 and one USB keyboard to the system, fiddling a bit with leds on PS2 keyboard and then tried to perform remote-wakeup on the suspended USB keyboard. Unfortunately I won't probably have time to debug this today.
Other than that, some trivial comments below.
BUG: spinlock cpu recursion on CPU#0, ksoftirqd/0/4 lock: ffff81001d8796a0, .magic: dead4ead, .owner: swapper/0, .owner_cpu:
0
Pid: 4, comm: ksoftirqd/0 Not tainted
2.6.25-rc7-default-00002-g9e50656-dirty #24
Call Trace: [<ffffffff802fd0ca>] spin_bug+0xa2/0xaa [<ffffffff802fd25d>] _raw_spin_lock+0x5d/0x125 [<ffffffff80426bdb>] _spin_lock_irqsave+0x34/0x3c [<ffffffff8038e601>] input_event+0x3b/0x77 [<ffffffff880fc838>] :hid:hidinput_hid_event+0x2da/0x30a [<ffffffff880f880e>] :hid:hid_process_event+0x44/0x78 [<ffffffff880f8a99>] :hid:hid_input_field+0x257/0x268 [<ffffffff880f8c94>] :hid:hid_input_report+0x1ea/0x223 [<ffffffff8810e309>] :usbhid:hid_irq_in+0xb9/0x1e4 [<ffffffff80370693>] usb_hcd_giveback_urb+0x84/0xb7 [<ffffffff80386f0c>] uhci_giveback_urb+0x10a/0x19e [<ffffffff8038763f>] uhci_scan_schedule+0x58a/0x83f [<ffffffff80389586>] uhci_irq+0x128/0x144 [<ffffffff803709be>] usb_hcd_irq+0x2b/0x5b [<ffffffff8025e2bf>] handle_IRQ_event+0x20/0x55 [<ffffffff8025f673>] handle_fasteoi_irq+0x9d/0xdd [<ffffffff8020ecb2>] do_IRQ+0x6d/0xd7 [<ffffffff8020c556>] ret_from_intr+0x0/0xf [<ffffffff80231e6f>] ? __do_softirq+0x65/0xf1 [<ffffffff80231e67>] ? __do_softirq+0x5d/0xf1 [<ffffffff80231aa9>] ? ksoftirqd+0x0/0xa5 [<ffffffff8020d25c>] ? call_softirq+0x1c/0x28 [<ffffffff8020ebca>] ? do_softirq+0x39/0x8a [<ffffffff80231aec>] ? ksoftirqd+0x43/0xa5 [<ffffffff8023fe10>] ? kthread+0x49/0x79 [<ffffffff8020cee8>] ? child_rip+0xa/0x12 [<ffffffff8020c5ff>] ? restore_args+0x0/0x30 [<ffffffff8023fc7b>] ? kthreadd+0x17b/0x1a0 [<ffffffff8023fdc7>] ? kthread+0x0/0x79 [<ffffffff8020cede>] ? child_rip+0x0/0x12
drivers/hid/hid-core.c int hid_input_report(struct hid_device * · line 1003
Quoted 6 lines
drivers/hid/hid-core.c +1
-0@@ -1001,6 +1001,24 @@ int hid_input_report(struct hid_device * }EXPORT_SYMBOL_GPL (hid_input_report); +#define NBITS (x) ((((x)-1)/BITS_PER_LONG)+1)
Why is this needed (and it is definitely not nice to have it in the middle of the file). We already have the same macro in input.h, and we include it from this source anyway, don't we?
drivers/hid/usbhid/hid-core.c line 8
Quoted 7 lines
drivers/hid/usbhid/hid-core.c +1
-0@@ -5,6 +5,7 @@* Copyright (c) 2000-2005 Vojtech Pavlik <[email protected]> * Copyright (c) 2005 Michael Haboustak <mike--vDbLwGUA7lNWk0Htik3J/[email protected]> for Concept2, Inc * Copyright (c) 2006-2007 Jiri Kosina +* Copyright (c) 2007 Oliver Neukum
It's 2008 these days already :)
drivers/hid/usbhid/hid-core.c static void hid_irq_in(struct urb *urb) · line 261
Quoted 5 lines
drivers/hid/usbhid/hid-core.c +5
-0+/* autosuspend refused while keys are pressed*/ +if (hid_check_keys_pressed (hid)) +set_bit (HID_KEYS_PRESSED , &usbhid->iofl); +else +clear_bit (HID_KEYS_PRESSED , &usbhid->iofl);
Maybe we will have to make this more general one day? I think it makes more sense to disable autosuspend whenever any event having EV_REP set is happening. But as this currently equals to keys, that should be probably OK for now.
drivers/hid/usbhid/hid-core.c static int usb_hidinput_input_event(stru · line 561
Quoted 5 lines
drivers/hid/usbhid/hid-core.c +5
-0+spin_lock_irqsave (&usbhid->outputlock, flags); +/* suspension will switch off LEDs, so count them */ +if (value) { +/* increase pm counter */ + }else {
Some code missing here?
drivers/hid/usbhid/hid-core.c static int hid_probe(struct usb_interfac · line 1224
Quoted 3 lines
drivers/hid/usbhid/hid-core.c +3
-0+if (udev->auto_pm &&test_bit (HID_KEYS_PRESSED , &usbhid->iofl)) { +/* lost race against keypresses */ +printk (KERN_ERR "lost race against keypresses.\n" );
I guess this printk() should go away, right? I don't think anyone is interested to know whether this happened, right?
drivers/hid/usbhid/hid-core.c struct usbhid_device { · line 76
Quoted 6 lines
drivers/hid/usbhid/hid-core.c +1
-1@@ -70,14 +73,14 @@ struct usbhid_device {unsigned char outhead, outtail;/* Output pipe fifo head & tail */ char *outbuf;/* Output buffer */ dma_addr_t outbuf_dma;/* Output buffer dma */ -spinlock_t outlock;/* Output fifo spinlock */ +spinlock_t outputlock;/* Output fifo spinlock */
Shouldn't ctrllock be removed as well?
Also, I think that a little bit more verbose changelog would be helpful (i.e. short description what is changed and how), as the patch is not completely trivial.
Thanks,
Signature · 6 lines
Jiri Kosina SUSE Labs -- To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): Pine.LNX.4.64.0804041437430.32130-YCXOAqNspd+N3ZZ/[email protected]
Am Freitag, 4. April 2008 16:24:30 schrieb Jiri Kosina:
Quoted 9 lines · Oliver Neukum
Quoted 4 lines
this is a full implementation of autosuspend for USB HID devices. Contrary to the current implementation it tries to safe power even for open devices, like keyboards and mice. Please test.
Hi Oliver,
there is some deadlock in IRQ path, could you please look at it? I have
Sure.
Quoted 3 lines
triggered it on my test system trivially by just attaching one PS2 and one USB keyboard to the system, fiddling a bit with leds on PS2 keyboard and then tried to perform remote-wakeup on the suspended USB keyboard.
That is you hit a key on the USB keyboard?
Regards
Oliver
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): 200804041628.14793.oliver-GvhC2dPhHPQdnm+yROfE0A@public.gmane.org
Quoted 2 lines · Oliver Neukum
Quoted 1 line
there is some deadlock in IRQ path, could you please look at it? I have
Sure.
Thanks.
Quoted 4 lines
Quoted 3 lines
triggered it on my test system trivially by just attaching one PS2 and one USB keyboard to the system, fiddling a bit with leds on PS2 keyboard and then tried to perform remote-wakeup on the suspended USB keyboard.
That is you hit a key on the USB keyboard?
Yes. Just letting the USB keyboard go auto-suspend, then fiddling with LEDs on PS2 keyboard and then hitting key on USB keyboard triggers it for me quite reliably (not 100% though, so you may have to retry).
Thanks,
Signature · 6 lines
Jiri Kosina SUSE Labs -- To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Am Freitag, 4. April 2008 16:38:18 schrieb Jiri Kosina:
Quoted 8 lines
Quoted 4 lines
Quoted 3 lines
triggered it on my test system trivially by just attaching one PS2 and one USB keyboard to the system, fiddling a bit with leds on PS2 keyboard and then tried to perform remote-wakeup on the suspended USB keyboard.
That is you hit a key on the USB keyboard?
Yes. Just letting the USB keyboard go auto-suspend, then fiddling with LEDs on PS2 keyboard and then hitting key on USB keyboard triggers it for me quite reliably (not 100% though, so you may have to retry).
I mean did you hit e.g. CAPS LOCK or any key that should have switched an LED or just any other key?
Regards
Oliver
Quoted 5 lines · Oliver Neukum
Quoted 3 lines
Yes. Just letting the USB keyboard go auto-suspend, then fiddling with LEDs on PS2 keyboard and then hitting key on USB keyboard triggers it for me quite reliably (not 100% though, so you may have to retry).
I mean did you hit e.g. CAPS LOCK or any key that should have switched an LED or just any other key?
OK, so the quite reliable sequence seems to be
- let the USB keyboard go to sleep
- press CAPS on PS2 keyboard
- let the USB keyboard go to sleep again
- press 'a' on USB keyboard
- let the USB keyboard go to sleep again
- press CAPS on PS2 keyboard
- let the USB keyboard go to sleep again
- press 'a' on the USB keyboard
=> deadlock.
Signature · 2 lines
Jiri Kosina SUSE Labs
A message that is not in the archive (sent off-list or not yet received): Pine.LNX.4.64.0804041708100.32130-YCXOAqNspd+N3ZZ/[email protected]
Am Freitag, 4. April 2008 17:11:24 schrieb Jiri Kosina:
Quoted 19 lines · Oliver Neukum
Quoted 6 lines
Quoted 4 lines
Yes. Just letting the USB keyboard go auto-suspend, then fiddling with LEDs on PS2 keyboard and then hitting key on USB keyboard triggers it for me quite reliably (not 100% though, so you may have to retry).
I mean did you hit e.g. CAPS LOCK or any key that should have switched an LED or just any other key?
OK, so the quite reliable sequence seems to be
- let the USB keyboard go to sleep
- press CAPS on PS2 keyboard
- let the USB keyboard go to sleep again
- press 'a' on USB keyboard
- let the USB keyboard go to sleep again
- press CAPS on PS2 keyboard
- let the USB keyboard go to sleep again
- press 'a' on the USB keyboard
Quoted 1 line
=> deadlock.
Good. I am recompiling with spinlock debug. This should be fixable.
Regards
Oliver
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): 200804041708.48296.oliver-GvhC2dPhHPQdnm+yROfE0A@public.gmane.org
Quoted 11 lines · Oliver Neukum
Quoted 10 lines
OK, so the quite reliable sequence seems to be
- let the USB keyboard go to sleep
- press CAPS on PS2 keyboard
- let the USB keyboard go to sleep again
- press 'a' on USB keyboard
- let the USB keyboard go to sleep again
- press CAPS on PS2 keyboard
- let the USB keyboard go to sleep again
- press 'a' on the USB keyboard
=> deadlock.Good. I am recompiling with spinlock debug. This should be fixable.
Hi Oliver,
have you succeeded debugging this please?
Thanks,
Signature · 7 lines
Jiri Kosina SUSE Labs -- To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Am Dienstag, 22. April 2008 02:07:30 schrieb Jiri Kosina:
Quoted 3 lines
Hi Oliver,
have you succeeded debugging this please?
I have been somewhat busy with hso patches and I cannot replicate it. I have gotten another keyboard to test on.
Regards
Oliver