aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/core
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2010-03-03 11:48:58 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2010-03-03 11:48:58 -0500
commit7f5b09c15ab989ed5ce4adda0be42c1302df70b7 (patch)
tree9695b00983d1bd077ff91c463abcb136330cf344 /drivers/usb/core
parent94468080220162f74dc6ce5c3e95e5fec8022902 (diff)
parentcedf8a78421943441b9011ce7bcdab55f07d2ea6 (diff)
Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb-2.6
* git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb-2.6: (220 commits) USB: backlight, appledisplay: fix incomplete registration failure handling USB: pl2303: remove unnecessary reset of usb_device in urbs USB: ftdi_sio: remove obsolete check in unthrottle USB: ftdi_sio: remove unused tx_bytes counter USB: qcaux: driver for auxiliary serial ports on Qualcomm devices USB: pl2303: initial TIOCGSERIAL support USB: option: add Longcheer/Longsung vendor ID USB: fix I2C API usage in ohci-pnx4008. USB: usbmon: mask seconds properly in text API USB: sisusbvga: no unnecessary GFP_ATOMIC USB: storage: onetouch: unnecessary GFP_ATOMIC USB: serial: ftdi: add CONTEC vendor and product id USB: remove references to port->port.count from the serial drivers USB: tty: Prune uses of tty_request_room in the USB layer USB: tty: Add a function to insert a string of characters with the same flag USB: don't read past config->interface[] if usb_control_msg() fails in usb_reset_configuration() USB: tty: kill request_room for USB ACM class USB: tty: sort out the request_room handling for whiteheat USB: storage: fix misplaced parenthesis USB: vstusb.c: removal of driver for Vernier Software & Technology, Inc., devices and spectrometers ...
Diffstat (limited to 'drivers/usb/core')
-rw-r--r--drivers/usb/core/Kconfig4
-rw-r--r--drivers/usb/core/devices.c32
-rw-r--r--drivers/usb/core/devio.c127
-rw-r--r--drivers/usb/core/driver.c918
-rw-r--r--drivers/usb/core/file.c2
-rw-r--r--drivers/usb/core/hcd.c27
-rw-r--r--drivers/usb/core/hcd.h13
-rw-r--r--drivers/usb/core/hub.c120
-rw-r--r--drivers/usb/core/message.c5
-rw-r--r--drivers/usb/core/quirks.c18
-rw-r--r--drivers/usb/core/sysfs.c85
-rw-r--r--drivers/usb/core/urb.c13
-rw-r--r--drivers/usb/core/usb.c38
-rw-r--r--drivers/usb/core/usb.h43
14 files changed, 693 insertions, 752 deletions
diff --git a/drivers/usb/core/Kconfig b/drivers/usb/core/Kconfig
index ad925946f869..97a819c23ef3 100644
--- a/drivers/usb/core/Kconfig
+++ b/drivers/usb/core/Kconfig
@@ -91,8 +91,8 @@ config USB_DYNAMIC_MINORS
91 If you are unsure about this, say N here. 91 If you are unsure about this, say N here.
92 92
93config USB_SUSPEND 93config USB_SUSPEND
94 bool "USB selective suspend/resume and wakeup" 94 bool "USB runtime power management (suspend/resume and wakeup)"
95 depends on USB && PM 95 depends on USB && PM_RUNTIME
96 help 96 help
97 If you say Y here, you can use driver calls or the sysfs 97 If you say Y here, you can use driver calls or the sysfs
98 "power/level" file to suspend or resume individual USB 98 "power/level" file to suspend or resume individual USB
diff --git a/drivers/usb/core/devices.c b/drivers/usb/core/devices.c
index 355dffcc23b0..c83c975152a6 100644
--- a/drivers/usb/core/devices.c
+++ b/drivers/usb/core/devices.c
@@ -118,6 +118,7 @@ static const char *format_endpt =
118 */ 118 */
119 119
120static DECLARE_WAIT_QUEUE_HEAD(deviceconndiscwq); 120static DECLARE_WAIT_QUEUE_HEAD(deviceconndiscwq);
121/* guarded by usbfs_mutex */
121static unsigned int conndiscevcnt; 122static unsigned int conndiscevcnt;
122 123
123/* this struct stores the poll state for <mountpoint>/devices pollers */ 124/* this struct stores the poll state for <mountpoint>/devices pollers */
@@ -156,7 +157,9 @@ static const struct class_info clas_info[] =
156 157
157void usbfs_conn_disc_event(void) 158void usbfs_conn_disc_event(void)
158{ 159{
160 mutex_lock(&usbfs_mutex);
159 conndiscevcnt++; 161 conndiscevcnt++;
162 mutex_unlock(&usbfs_mutex);
160 wake_up(&deviceconndiscwq); 163 wake_up(&deviceconndiscwq);
161} 164}
162 165
@@ -629,42 +632,29 @@ static ssize_t usb_device_read(struct file *file, char __user *buf,
629static unsigned int usb_device_poll(struct file *file, 632static unsigned int usb_device_poll(struct file *file,
630 struct poll_table_struct *wait) 633 struct poll_table_struct *wait)
631{ 634{
632 struct usb_device_status *st = file->private_data; 635 struct usb_device_status *st;
633 unsigned int mask = 0; 636 unsigned int mask = 0;
634 637
635 lock_kernel(); 638 mutex_lock(&usbfs_mutex);
639 st = file->private_data;
636 if (!st) { 640 if (!st) {
637 st = kmalloc(sizeof(struct usb_device_status), GFP_KERNEL); 641 st = kmalloc(sizeof(struct usb_device_status), GFP_KERNEL);
638
639 /* we may have dropped BKL -
640 * need to check for having lost the race */
641 if (file->private_data) {
642 kfree(st);
643 st = file->private_data;
644 goto lost_race;
645 }
646 /* we haven't lost - check for allocation failure now */
647 if (!st) { 642 if (!st) {
648 unlock_kernel(); 643 mutex_unlock(&usbfs_mutex);
649 return POLLIN; 644 return POLLIN;
650 } 645 }
651 646
652 /*
653 * need to prevent the module from being unloaded, since
654 * proc_unregister does not call the release method and
655 * we would have a memory leak
656 */
657 st->lastev = conndiscevcnt; 647 st->lastev = conndiscevcnt;
658 file->private_data = st; 648 file->private_data = st;
659 mask = POLLIN; 649 mask = POLLIN;
660 } 650 }
661lost_race: 651
662 if (file->f_mode & FMODE_READ) 652 if (file->f_mode & FMODE_READ)
663 poll_wait(file, &deviceconndiscwq, wait); 653 poll_wait(file, &deviceconndiscwq, wait);
664 if (st->lastev != conndiscevcnt) 654 if (st->lastev != conndiscevcnt)
665 mask |= POLLIN; 655 mask |= POLLIN;
666 st->lastev = conndiscevcnt; 656 st->lastev = conndiscevcnt;
667 unlock_kernel(); 657 mutex_unlock(&usbfs_mutex);
668 return mask; 658 return mask;
669} 659}
670 660
@@ -685,7 +675,7 @@ static loff_t usb_device_lseek(struct file *file, loff_t offset, int orig)
685{ 675{
686 loff_t ret; 676 loff_t ret;
687 677
688 lock_kernel(); 678 mutex_lock(&file->f_dentry->d_inode->i_mutex);
689 679
690 switch (orig) { 680 switch (orig) {
691 case 0: 681 case 0:
@@ -701,7 +691,7 @@ static loff_t usb_device_lseek(struct file *file, loff_t offset, int orig)
701 ret = -EINVAL; 691 ret = -EINVAL;
702 } 692 }
703 693
704 unlock_kernel(); 694 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
705 return ret; 695 return ret;
706} 696}
707 697
diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index a678186f218f..e909ff7b9094 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -122,7 +122,7 @@ static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
122{ 122{
123 loff_t ret; 123 loff_t ret;
124 124
125 lock_kernel(); 125 mutex_lock(&file->f_dentry->d_inode->i_mutex);
126 126
127 switch (orig) { 127 switch (orig) {
128 case 0: 128 case 0:
@@ -138,7 +138,7 @@ static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
138 ret = -EINVAL; 138 ret = -EINVAL;
139 } 139 }
140 140
141 unlock_kernel(); 141 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
142 return ret; 142 return ret;
143} 143}
144 144
@@ -310,7 +310,8 @@ static struct async *async_getpending(struct dev_state *ps,
310 310
311static void snoop_urb(struct usb_device *udev, 311static void snoop_urb(struct usb_device *udev,
312 void __user *userurb, int pipe, unsigned length, 312 void __user *userurb, int pipe, unsigned length,
313 int timeout_or_status, enum snoop_when when) 313 int timeout_or_status, enum snoop_when when,
314 unsigned char *data, unsigned data_len)
314{ 315{
315 static const char *types[] = {"isoc", "int", "ctrl", "bulk"}; 316 static const char *types[] = {"isoc", "int", "ctrl", "bulk"};
316 static const char *dirs[] = {"out", "in"}; 317 static const char *dirs[] = {"out", "in"};
@@ -344,6 +345,11 @@ static void snoop_urb(struct usb_device *udev,
344 "status %d\n", 345 "status %d\n",
345 ep, t, d, length, timeout_or_status); 346 ep, t, d, length, timeout_or_status);
346 } 347 }
348
349 if (data && data_len > 0) {
350 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
351 data, data_len, 1);
352 }
347} 353}
348 354
349#define AS_CONTINUATION 1 355#define AS_CONTINUATION 1
@@ -410,7 +416,9 @@ static void async_completed(struct urb *urb)
410 } 416 }
411 snoop(&urb->dev->dev, "urb complete\n"); 417 snoop(&urb->dev->dev, "urb complete\n");
412 snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length, 418 snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length,
413 as->status, COMPLETE); 419 as->status, COMPLETE,
420 ((urb->transfer_flags & URB_DIR_MASK) == USB_DIR_OUT) ?
421 NULL : urb->transfer_buffer, urb->actual_length);
414 if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET && 422 if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET &&
415 as->status != -ENOENT) 423 as->status != -ENOENT)
416 cancel_bulk_urbs(ps, as->bulk_addr); 424 cancel_bulk_urbs(ps, as->bulk_addr);
@@ -653,20 +661,20 @@ static int usbdev_open(struct inode *inode, struct file *file)
653 const struct cred *cred = current_cred(); 661 const struct cred *cred = current_cred();
654 int ret; 662 int ret;
655 663
656 lock_kernel();
657 /* Protect against simultaneous removal or release */
658 mutex_lock(&usbfs_mutex);
659
660 ret = -ENOMEM; 664 ret = -ENOMEM;
661 ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL); 665 ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
662 if (!ps) 666 if (!ps)
663 goto out; 667 goto out_free_ps;
664 668
665 ret = -ENODEV; 669 ret = -ENODEV;
666 670
671 /* Protect against simultaneous removal or release */
672 mutex_lock(&usbfs_mutex);
673
667 /* usbdev device-node */ 674 /* usbdev device-node */
668 if (imajor(inode) == USB_DEVICE_MAJOR) 675 if (imajor(inode) == USB_DEVICE_MAJOR)
669 dev = usbdev_lookup_by_devt(inode->i_rdev); 676 dev = usbdev_lookup_by_devt(inode->i_rdev);
677
670#ifdef CONFIG_USB_DEVICEFS 678#ifdef CONFIG_USB_DEVICEFS
671 /* procfs file */ 679 /* procfs file */
672 if (!dev) { 680 if (!dev) {
@@ -678,13 +686,19 @@ static int usbdev_open(struct inode *inode, struct file *file)
678 dev = NULL; 686 dev = NULL;
679 } 687 }
680#endif 688#endif
681 if (!dev || dev->state == USB_STATE_NOTATTACHED) 689 mutex_unlock(&usbfs_mutex);
682 goto out; 690
691 if (!dev)
692 goto out_free_ps;
693
694 usb_lock_device(dev);
695 if (dev->state == USB_STATE_NOTATTACHED)
696 goto out_unlock_device;
697
683 ret = usb_autoresume_device(dev); 698 ret = usb_autoresume_device(dev);
684 if (ret) 699 if (ret)
685 goto out; 700 goto out_unlock_device;
686 701
687 ret = 0;
688 ps->dev = dev; 702 ps->dev = dev;
689 ps->file = file; 703 ps->file = file;
690 spin_lock_init(&ps->lock); 704 spin_lock_init(&ps->lock);
@@ -702,15 +716,16 @@ static int usbdev_open(struct inode *inode, struct file *file)
702 smp_wmb(); 716 smp_wmb();
703 list_add_tail(&ps->list, &dev->filelist); 717 list_add_tail(&ps->list, &dev->filelist);
704 file->private_data = ps; 718 file->private_data = ps;
719 usb_unlock_device(dev);
705 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current), 720 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
706 current->comm); 721 current->comm);
707 out: 722 return ret;
708 if (ret) { 723
709 kfree(ps); 724 out_unlock_device:
710 usb_put_dev(dev); 725 usb_unlock_device(dev);
711 } 726 usb_put_dev(dev);
712 mutex_unlock(&usbfs_mutex); 727 out_free_ps:
713 unlock_kernel(); 728 kfree(ps);
714 return ret; 729 return ret;
715} 730}
716 731
@@ -724,10 +739,7 @@ static int usbdev_release(struct inode *inode, struct file *file)
724 usb_lock_device(dev); 739 usb_lock_device(dev);
725 usb_hub_release_all_ports(dev, ps); 740 usb_hub_release_all_ports(dev, ps);
726 741
727 /* Protect against simultaneous open */
728 mutex_lock(&usbfs_mutex);
729 list_del_init(&ps->list); 742 list_del_init(&ps->list);
730 mutex_unlock(&usbfs_mutex);
731 743
732 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed); 744 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
733 ifnum++) { 745 ifnum++) {
@@ -770,6 +782,13 @@ static int proc_control(struct dev_state *ps, void __user *arg)
770 if (!tbuf) 782 if (!tbuf)
771 return -ENOMEM; 783 return -ENOMEM;
772 tmo = ctrl.timeout; 784 tmo = ctrl.timeout;
785 snoop(&dev->dev, "control urb: bRequestType=%02x "
786 "bRequest=%02x wValue=%04x "
787 "wIndex=%04x wLength=%04x\n",
788 ctrl.bRequestType, ctrl.bRequest,
789 __le16_to_cpup(&ctrl.wValue),
790 __le16_to_cpup(&ctrl.wIndex),
791 __le16_to_cpup(&ctrl.wLength));
773 if (ctrl.bRequestType & 0x80) { 792 if (ctrl.bRequestType & 0x80) {
774 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data, 793 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
775 ctrl.wLength)) { 794 ctrl.wLength)) {
@@ -777,15 +796,15 @@ static int proc_control(struct dev_state *ps, void __user *arg)
777 return -EINVAL; 796 return -EINVAL;
778 } 797 }
779 pipe = usb_rcvctrlpipe(dev, 0); 798 pipe = usb_rcvctrlpipe(dev, 0);
780 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT); 799 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT, NULL, 0);
781 800
782 usb_unlock_device(dev); 801 usb_unlock_device(dev);
783 i = usb_control_msg(dev, pipe, ctrl.bRequest, 802 i = usb_control_msg(dev, pipe, ctrl.bRequest,
784 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex, 803 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
785 tbuf, ctrl.wLength, tmo); 804 tbuf, ctrl.wLength, tmo);
786 usb_lock_device(dev); 805 usb_lock_device(dev);
787 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE); 806 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE,
788 807 tbuf, i);
789 if ((i > 0) && ctrl.wLength) { 808 if ((i > 0) && ctrl.wLength) {
790 if (copy_to_user(ctrl.data, tbuf, i)) { 809 if (copy_to_user(ctrl.data, tbuf, i)) {
791 free_page((unsigned long)tbuf); 810 free_page((unsigned long)tbuf);
@@ -800,14 +819,15 @@ static int proc_control(struct dev_state *ps, void __user *arg)
800 } 819 }
801 } 820 }
802 pipe = usb_sndctrlpipe(dev, 0); 821 pipe = usb_sndctrlpipe(dev, 0);
803 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT); 822 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT,
823 tbuf, ctrl.wLength);
804 824
805 usb_unlock_device(dev); 825 usb_unlock_device(dev);
806 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest, 826 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
807 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex, 827 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
808 tbuf, ctrl.wLength, tmo); 828 tbuf, ctrl.wLength, tmo);
809 usb_lock_device(dev); 829 usb_lock_device(dev);
810 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE); 830 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE, NULL, 0);
811 } 831 }
812 free_page((unsigned long)tbuf); 832 free_page((unsigned long)tbuf);
813 if (i < 0 && i != -EPIPE) { 833 if (i < 0 && i != -EPIPE) {
@@ -853,12 +873,12 @@ static int proc_bulk(struct dev_state *ps, void __user *arg)
853 kfree(tbuf); 873 kfree(tbuf);
854 return -EINVAL; 874 return -EINVAL;
855 } 875 }
856 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT); 876 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, NULL, 0);
857 877
858 usb_unlock_device(dev); 878 usb_unlock_device(dev);
859 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo); 879 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
860 usb_lock_device(dev); 880 usb_lock_device(dev);
861 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE); 881 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, tbuf, len2);
862 882
863 if (!i && len2) { 883 if (!i && len2) {
864 if (copy_to_user(bulk.data, tbuf, len2)) { 884 if (copy_to_user(bulk.data, tbuf, len2)) {
@@ -873,12 +893,12 @@ static int proc_bulk(struct dev_state *ps, void __user *arg)
873 return -EFAULT; 893 return -EFAULT;
874 } 894 }
875 } 895 }
876 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT); 896 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, tbuf, len1);
877 897
878 usb_unlock_device(dev); 898 usb_unlock_device(dev);
879 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo); 899 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
880 usb_lock_device(dev); 900 usb_lock_device(dev);
881 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE); 901 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, NULL, 0);
882 } 902 }
883 kfree(tbuf); 903 kfree(tbuf);
884 if (i < 0) 904 if (i < 0)
@@ -1097,6 +1117,13 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1097 is_in = 0; 1117 is_in = 0;
1098 uurb->endpoint &= ~USB_DIR_IN; 1118 uurb->endpoint &= ~USB_DIR_IN;
1099 } 1119 }
1120 snoop(&ps->dev->dev, "control urb: bRequestType=%02x "
1121 "bRequest=%02x wValue=%04x "
1122 "wIndex=%04x wLength=%04x\n",
1123 dr->bRequestType, dr->bRequest,
1124 __le16_to_cpup(&dr->wValue),
1125 __le16_to_cpup(&dr->wIndex),
1126 __le16_to_cpup(&dr->wLength));
1100 break; 1127 break;
1101 1128
1102 case USBDEVFS_URB_TYPE_BULK: 1129 case USBDEVFS_URB_TYPE_BULK:
@@ -1104,13 +1131,25 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1104 case USB_ENDPOINT_XFER_CONTROL: 1131 case USB_ENDPOINT_XFER_CONTROL:
1105 case USB_ENDPOINT_XFER_ISOC: 1132 case USB_ENDPOINT_XFER_ISOC:
1106 return -EINVAL; 1133 return -EINVAL;
1107 /* allow single-shot interrupt transfers, at bogus rates */ 1134 case USB_ENDPOINT_XFER_INT:
1135 /* allow single-shot interrupt transfers */
1136 uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
1137 goto interrupt_urb;
1108 } 1138 }
1109 uurb->number_of_packets = 0; 1139 uurb->number_of_packets = 0;
1110 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE) 1140 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1111 return -EINVAL; 1141 return -EINVAL;
1112 break; 1142 break;
1113 1143
1144 case USBDEVFS_URB_TYPE_INTERRUPT:
1145 if (!usb_endpoint_xfer_int(&ep->desc))
1146 return -EINVAL;
1147 interrupt_urb:
1148 uurb->number_of_packets = 0;
1149 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1150 return -EINVAL;
1151 break;
1152
1114 case USBDEVFS_URB_TYPE_ISO: 1153 case USBDEVFS_URB_TYPE_ISO:
1115 /* arbitrary limit */ 1154 /* arbitrary limit */
1116 if (uurb->number_of_packets < 1 || 1155 if (uurb->number_of_packets < 1 ||
@@ -1143,14 +1182,6 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1143 uurb->buffer_length = totlen; 1182 uurb->buffer_length = totlen;
1144 break; 1183 break;
1145 1184
1146 case USBDEVFS_URB_TYPE_INTERRUPT:
1147 uurb->number_of_packets = 0;
1148 if (!usb_endpoint_xfer_int(&ep->desc))
1149 return -EINVAL;
1150 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1151 return -EINVAL;
1152 break;
1153
1154 default: 1185 default:
1155 return -EINVAL; 1186 return -EINVAL;
1156 } 1187 }
@@ -1236,7 +1267,9 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1236 } 1267 }
1237 } 1268 }
1238 snoop_urb(ps->dev, as->userurb, as->urb->pipe, 1269 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1239 as->urb->transfer_buffer_length, 0, SUBMIT); 1270 as->urb->transfer_buffer_length, 0, SUBMIT,
1271 is_in ? NULL : as->urb->transfer_buffer,
1272 uurb->buffer_length);
1240 async_newpending(as); 1273 async_newpending(as);
1241 1274
1242 if (usb_endpoint_xfer_bulk(&ep->desc)) { 1275 if (usb_endpoint_xfer_bulk(&ep->desc)) {
@@ -1274,7 +1307,7 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1274 dev_printk(KERN_DEBUG, &ps->dev->dev, 1307 dev_printk(KERN_DEBUG, &ps->dev->dev,
1275 "usbfs: usb_submit_urb returned %d\n", ret); 1308 "usbfs: usb_submit_urb returned %d\n", ret);
1276 snoop_urb(ps->dev, as->userurb, as->urb->pipe, 1309 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1277 0, ret, COMPLETE); 1310 0, ret, COMPLETE, NULL, 0);
1278 async_removepending(as); 1311 async_removepending(as);
1279 free_async(as); 1312 free_async(as);
1280 return ret; 1313 return ret;
@@ -1628,7 +1661,10 @@ static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
1628 if (driver == NULL || driver->ioctl == NULL) { 1661 if (driver == NULL || driver->ioctl == NULL) {
1629 retval = -ENOTTY; 1662 retval = -ENOTTY;
1630 } else { 1663 } else {
1664 /* keep API that guarantees BKL */
1665 lock_kernel();
1631 retval = driver->ioctl(intf, ctl->ioctl_code, buf); 1666 retval = driver->ioctl(intf, ctl->ioctl_code, buf);
1667 unlock_kernel();
1632 if (retval == -ENOIOCTLCMD) 1668 if (retval == -ENOIOCTLCMD)
1633 retval = -ENOTTY; 1669 retval = -ENOTTY;
1634 } 1670 }
@@ -1711,6 +1747,7 @@ static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
1711 1747
1712 if (!(file->f_mode & FMODE_WRITE)) 1748 if (!(file->f_mode & FMODE_WRITE))
1713 return -EPERM; 1749 return -EPERM;
1750
1714 usb_lock_device(dev); 1751 usb_lock_device(dev);
1715 if (!connected(ps)) { 1752 if (!connected(ps)) {
1716 usb_unlock_device(dev); 1753 usb_unlock_device(dev);
@@ -1877,9 +1914,7 @@ static long usbdev_ioctl(struct file *file, unsigned int cmd,
1877{ 1914{
1878 int ret; 1915 int ret;
1879 1916
1880 lock_kernel();
1881 ret = usbdev_do_ioctl(file, cmd, (void __user *)arg); 1917 ret = usbdev_do_ioctl(file, cmd, (void __user *)arg);
1882 unlock_kernel();
1883 1918
1884 return ret; 1919 return ret;
1885} 1920}
@@ -1890,9 +1925,7 @@ static long usbdev_compat_ioctl(struct file *file, unsigned int cmd,
1890{ 1925{
1891 int ret; 1926 int ret;
1892 1927
1893 lock_kernel();
1894 ret = usbdev_do_ioctl(file, cmd, compat_ptr(arg)); 1928 ret = usbdev_do_ioctl(file, cmd, compat_ptr(arg));
1895 unlock_kernel();
1896 1929
1897 return ret; 1930 return ret;
1898} 1931}
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index f2f055eb6831..a7037bf81688 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -25,7 +25,7 @@
25#include <linux/device.h> 25#include <linux/device.h>
26#include <linux/usb.h> 26#include <linux/usb.h>
27#include <linux/usb/quirks.h> 27#include <linux/usb/quirks.h>
28#include <linux/workqueue.h> 28#include <linux/pm_runtime.h>
29#include "hcd.h" 29#include "hcd.h"
30#include "usb.h" 30#include "usb.h"
31 31
@@ -221,7 +221,7 @@ static int usb_probe_device(struct device *dev)
221{ 221{
222 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); 222 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
223 struct usb_device *udev = to_usb_device(dev); 223 struct usb_device *udev = to_usb_device(dev);
224 int error = -ENODEV; 224 int error = 0;
225 225
226 dev_dbg(dev, "%s\n", __func__); 226 dev_dbg(dev, "%s\n", __func__);
227 227
@@ -230,18 +230,23 @@ static int usb_probe_device(struct device *dev)
230 /* The device should always appear to be in use 230 /* The device should always appear to be in use
231 * unless the driver suports autosuspend. 231 * unless the driver suports autosuspend.
232 */ 232 */
233 udev->pm_usage_cnt = !(udriver->supports_autosuspend); 233 if (!udriver->supports_autosuspend)
234 error = usb_autoresume_device(udev);
234 235
235 error = udriver->probe(udev); 236 if (!error)
237 error = udriver->probe(udev);
236 return error; 238 return error;
237} 239}
238 240
239/* called from driver core with dev locked */ 241/* called from driver core with dev locked */
240static int usb_unbind_device(struct device *dev) 242static int usb_unbind_device(struct device *dev)
241{ 243{
244 struct usb_device *udev = to_usb_device(dev);
242 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); 245 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
243 246
244 udriver->disconnect(to_usb_device(dev)); 247 udriver->disconnect(udev);
248 if (!udriver->supports_autosuspend)
249 usb_autosuspend_device(udev);
245 return 0; 250 return 0;
246} 251}
247 252
@@ -274,60 +279,62 @@ static int usb_probe_interface(struct device *dev)
274 intf->needs_binding = 0; 279 intf->needs_binding = 0;
275 280
276 if (usb_device_is_owned(udev)) 281 if (usb_device_is_owned(udev))
277 return -ENODEV; 282 return error;
278 283
279 if (udev->authorized == 0) { 284 if (udev->authorized == 0) {
280 dev_err(&intf->dev, "Device is not authorized for usage\n"); 285 dev_err(&intf->dev, "Device is not authorized for usage\n");
281 return -ENODEV; 286 return error;
282 } 287 }
283 288
284 id = usb_match_id(intf, driver->id_table); 289 id = usb_match_id(intf, driver->id_table);
285 if (!id) 290 if (!id)
286 id = usb_match_dynamic_id(intf, driver); 291 id = usb_match_dynamic_id(intf, driver);
287 if (id) { 292 if (!id)
288 dev_dbg(dev, "%s - got id\n", __func__); 293 return error;
289
290 error = usb_autoresume_device(udev);
291 if (error)
292 return error;
293 294
294 /* Interface "power state" doesn't correspond to any hardware 295 dev_dbg(dev, "%s - got id\n", __func__);
295 * state whatsoever. We use it to record when it's bound to
296 * a driver that may start I/0: it's not frozen/quiesced.
297 */
298 mark_active(intf);
299 intf->condition = USB_INTERFACE_BINDING;
300 296
301 /* The interface should always appear to be in use 297 error = usb_autoresume_device(udev);
302 * unless the driver suports autosuspend. 298 if (error)
303 */ 299 return error;
304 atomic_set(&intf->pm_usage_cnt, !driver->supports_autosuspend);
305
306 /* Carry out a deferred switch to altsetting 0 */
307 if (intf->needs_altsetting0) {
308 error = usb_set_interface(udev, intf->altsetting[0].
309 desc.bInterfaceNumber, 0);
310 if (error < 0)
311 goto err;
312 300
313 intf->needs_altsetting0 = 0; 301 intf->condition = USB_INTERFACE_BINDING;
314 }
315 302
316 error = driver->probe(intf, id); 303 /* Bound interfaces are initially active. They are
317 if (error) 304 * runtime-PM-enabled only if the driver has autosuspend support.
305 * They are sensitive to their children's power states.
306 */
307 pm_runtime_set_active(dev);
308 pm_suspend_ignore_children(dev, false);
309 if (driver->supports_autosuspend)
310 pm_runtime_enable(dev);
311
312 /* Carry out a deferred switch to altsetting 0 */
313 if (intf->needs_altsetting0) {
314 error = usb_set_interface(udev, intf->altsetting[0].
315 desc.bInterfaceNumber, 0);
316 if (error < 0)
318 goto err; 317 goto err;
319 318 intf->needs_altsetting0 = 0;
320 intf->condition = USB_INTERFACE_BOUND;
321 usb_autosuspend_device(udev);
322 } 319 }
323 320
321 error = driver->probe(intf, id);
322 if (error)
323 goto err;
324
325 intf->condition = USB_INTERFACE_BOUND;
326 usb_autosuspend_device(udev);
324 return error; 327 return error;
325 328
326err: 329 err:
327 mark_quiesced(intf);
328 intf->needs_remote_wakeup = 0; 330 intf->needs_remote_wakeup = 0;
329 intf->condition = USB_INTERFACE_UNBOUND; 331 intf->condition = USB_INTERFACE_UNBOUND;
330 usb_cancel_queued_reset(intf); 332 usb_cancel_queued_reset(intf);
333
334 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
335 pm_runtime_disable(dev);
336 pm_runtime_set_suspended(dev);
337
331 usb_autosuspend_device(udev); 338 usb_autosuspend_device(udev);
332 return error; 339 return error;
333} 340}
@@ -377,9 +384,17 @@ static int usb_unbind_interface(struct device *dev)
377 usb_set_intfdata(intf, NULL); 384 usb_set_intfdata(intf, NULL);
378 385
379 intf->condition = USB_INTERFACE_UNBOUND; 386 intf->condition = USB_INTERFACE_UNBOUND;
380 mark_quiesced(intf);
381 intf->needs_remote_wakeup = 0; 387 intf->needs_remote_wakeup = 0;
382 388
389 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
390 pm_runtime_disable(dev);
391 pm_runtime_set_suspended(dev);
392
393 /* Undo any residual pm_autopm_get_interface_* calls */
394 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
395 usb_autopm_put_interface_no_suspend(intf);
396 atomic_set(&intf->pm_usage_cnt, 0);
397
383 if (!error) 398 if (!error)
384 usb_autosuspend_device(udev); 399 usb_autosuspend_device(udev);
385 400
@@ -410,7 +425,6 @@ int usb_driver_claim_interface(struct usb_driver *driver,
410 struct usb_interface *iface, void *priv) 425 struct usb_interface *iface, void *priv)
411{ 426{
412 struct device *dev = &iface->dev; 427 struct device *dev = &iface->dev;
413 struct usb_device *udev = interface_to_usbdev(iface);
414 int retval = 0; 428 int retval = 0;
415 429
416 if (dev->driver) 430 if (dev->driver)
@@ -420,11 +434,16 @@ int usb_driver_claim_interface(struct usb_driver *driver,
420 usb_set_intfdata(iface, priv); 434 usb_set_intfdata(iface, priv);
421 iface->needs_binding = 0; 435 iface->needs_binding = 0;
422 436
423 usb_pm_lock(udev);
424 iface->condition = USB_INTERFACE_BOUND; 437 iface->condition = USB_INTERFACE_BOUND;
425 mark_active(iface); 438
426 atomic_set(&iface->pm_usage_cnt, !driver->supports_autosuspend); 439 /* Bound interfaces are initially active. They are
427 usb_pm_unlock(udev); 440 * runtime-PM-enabled only if the driver has autosuspend support.
441 * They are sensitive to their children's power states.
442 */
443 pm_runtime_set_active(dev);
444 pm_suspend_ignore_children(dev, false);
445 if (driver->supports_autosuspend)
446 pm_runtime_enable(dev);
428 447
429 /* if interface was already added, bind now; else let 448 /* if interface was already added, bind now; else let
430 * the future device_add() bind it, bypassing probe() 449 * the future device_add() bind it, bypassing probe()
@@ -691,9 +710,6 @@ static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
691{ 710{
692 struct usb_device *usb_dev; 711 struct usb_device *usb_dev;
693 712
694 /* driver is often null here; dev_dbg() would oops */
695 pr_debug("usb %s: uevent\n", dev_name(dev));
696
697 if (is_usb_device(dev)) { 713 if (is_usb_device(dev)) {
698 usb_dev = to_usb_device(dev); 714 usb_dev = to_usb_device(dev);
699 } else if (is_usb_interface(dev)) { 715 } else if (is_usb_interface(dev)) {
@@ -705,6 +721,7 @@ static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
705 } 721 }
706 722
707 if (usb_dev->devnum < 0) { 723 if (usb_dev->devnum < 0) {
724 /* driver is often null here; dev_dbg() would oops */
708 pr_debug("usb %s: already deleted?\n", dev_name(dev)); 725 pr_debug("usb %s: already deleted?\n", dev_name(dev));
709 return -ENODEV; 726 return -ENODEV;
710 } 727 }
@@ -983,7 +1000,6 @@ static void do_unbind_rebind(struct usb_device *udev, int action)
983 } 1000 }
984} 1001}
985 1002
986/* Caller has locked udev's pm_mutex */
987static int usb_suspend_device(struct usb_device *udev, pm_message_t msg) 1003static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
988{ 1004{
989 struct usb_device_driver *udriver; 1005 struct usb_device_driver *udriver;
@@ -1007,7 +1023,6 @@ static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
1007 return status; 1023 return status;
1008} 1024}
1009 1025
1010/* Caller has locked udev's pm_mutex */
1011static int usb_resume_device(struct usb_device *udev, pm_message_t msg) 1026static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
1012{ 1027{
1013 struct usb_device_driver *udriver; 1028 struct usb_device_driver *udriver;
@@ -1041,27 +1056,20 @@ static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
1041 return status; 1056 return status;
1042} 1057}
1043 1058
1044/* Caller has locked intf's usb_device's pm mutex */
1045static int usb_suspend_interface(struct usb_device *udev, 1059static int usb_suspend_interface(struct usb_device *udev,
1046 struct usb_interface *intf, pm_message_t msg) 1060 struct usb_interface *intf, pm_message_t msg)
1047{ 1061{
1048 struct usb_driver *driver; 1062 struct usb_driver *driver;
1049 int status = 0; 1063 int status = 0;
1050 1064
1051 /* with no hardware, USB interfaces only use FREEZE and ON states */ 1065 if (udev->state == USB_STATE_NOTATTACHED ||
1052 if (udev->state == USB_STATE_NOTATTACHED || !is_active(intf)) 1066 intf->condition == USB_INTERFACE_UNBOUND)
1053 goto done;
1054
1055 /* This can happen; see usb_driver_release_interface() */
1056 if (intf->condition == USB_INTERFACE_UNBOUND)
1057 goto done; 1067 goto done;
1058 driver = to_usb_driver(intf->dev.driver); 1068 driver = to_usb_driver(intf->dev.driver);
1059 1069
1060 if (driver->suspend) { 1070 if (driver->suspend) {
1061 status = driver->suspend(intf, msg); 1071 status = driver->suspend(intf, msg);
1062 if (status == 0) 1072 if (status && !(msg.event & PM_EVENT_AUTO))
1063 mark_quiesced(intf);
1064 else if (!(msg.event & PM_EVENT_AUTO))
1065 dev_err(&intf->dev, "%s error %d\n", 1073 dev_err(&intf->dev, "%s error %d\n",
1066 "suspend", status); 1074 "suspend", status);
1067 } else { 1075 } else {
@@ -1069,7 +1077,6 @@ static int usb_suspend_interface(struct usb_device *udev,
1069 intf->needs_binding = 1; 1077 intf->needs_binding = 1;
1070 dev_warn(&intf->dev, "no %s for driver %s?\n", 1078 dev_warn(&intf->dev, "no %s for driver %s?\n",
1071 "suspend", driver->name); 1079 "suspend", driver->name);
1072 mark_quiesced(intf);
1073 } 1080 }
1074 1081
1075 done: 1082 done:
@@ -1077,14 +1084,13 @@ static int usb_suspend_interface(struct usb_device *udev,
1077 return status; 1084 return status;
1078} 1085}
1079 1086
1080/* Caller has locked intf's usb_device's pm_mutex */
1081static int usb_resume_interface(struct usb_device *udev, 1087static int usb_resume_interface(struct usb_device *udev,
1082 struct usb_interface *intf, pm_message_t msg, int reset_resume) 1088 struct usb_interface *intf, pm_message_t msg, int reset_resume)
1083{ 1089{
1084 struct usb_driver *driver; 1090 struct usb_driver *driver;
1085 int status = 0; 1091 int status = 0;
1086 1092
1087 if (udev->state == USB_STATE_NOTATTACHED || is_active(intf)) 1093 if (udev->state == USB_STATE_NOTATTACHED)
1088 goto done; 1094 goto done;
1089 1095
1090 /* Don't let autoresume interfere with unbinding */ 1096 /* Don't let autoresume interfere with unbinding */
@@ -1135,90 +1141,11 @@ static int usb_resume_interface(struct usb_device *udev,
1135 1141
1136done: 1142done:
1137 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status); 1143 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1138 if (status == 0 && intf->condition == USB_INTERFACE_BOUND)
1139 mark_active(intf);
1140 1144
1141 /* Later we will unbind the driver and/or reprobe, if necessary */ 1145 /* Later we will unbind the driver and/or reprobe, if necessary */
1142 return status; 1146 return status;
1143} 1147}
1144 1148
1145#ifdef CONFIG_USB_SUSPEND
1146
1147/* Internal routine to check whether we may autosuspend a device. */
1148static int autosuspend_check(struct usb_device *udev, int reschedule)
1149{
1150 int i;
1151 struct usb_interface *intf;
1152 unsigned long suspend_time, j;
1153
1154 /* For autosuspend, fail fast if anything is in use or autosuspend
1155 * is disabled. Also fail if any interfaces require remote wakeup
1156 * but it isn't available.
1157 */
1158 if (udev->pm_usage_cnt > 0)
1159 return -EBUSY;
1160 if (udev->autosuspend_delay < 0 || udev->autosuspend_disabled)
1161 return -EPERM;
1162
1163 suspend_time = udev->last_busy + udev->autosuspend_delay;
1164 if (udev->actconfig) {
1165 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1166 intf = udev->actconfig->interface[i];
1167 if (!is_active(intf))
1168 continue;
1169 if (atomic_read(&intf->pm_usage_cnt) > 0)
1170 return -EBUSY;
1171 if (intf->needs_remote_wakeup &&
1172 !udev->do_remote_wakeup) {
1173 dev_dbg(&udev->dev, "remote wakeup needed "
1174 "for autosuspend\n");
1175 return -EOPNOTSUPP;
1176 }
1177
1178 /* Don't allow autosuspend if the device will need
1179 * a reset-resume and any of its interface drivers
1180 * doesn't include support.
1181 */
1182 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1183 struct usb_driver *driver;
1184
1185 driver = to_usb_driver(intf->dev.driver);
1186 if (!driver->reset_resume ||
1187 intf->needs_remote_wakeup)
1188 return -EOPNOTSUPP;
1189 }
1190 }
1191 }
1192
1193 /* If everything is okay but the device hasn't been idle for long
1194 * enough, queue a delayed autosuspend request. If the device
1195 * _has_ been idle for long enough and the reschedule flag is set,
1196 * likewise queue a delayed (1 second) autosuspend request.
1197 */
1198 j = jiffies;
1199 if (time_before(j, suspend_time))
1200 reschedule = 1;
1201 else
1202 suspend_time = j + HZ;
1203 if (reschedule) {
1204 if (!timer_pending(&udev->autosuspend.timer)) {
1205 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
1206 round_jiffies_up_relative(suspend_time - j));
1207 }
1208 return -EAGAIN;
1209 }
1210 return 0;
1211}
1212
1213#else
1214
1215static inline int autosuspend_check(struct usb_device *udev, int reschedule)
1216{
1217 return 0;
1218}
1219
1220#endif /* CONFIG_USB_SUSPEND */
1221
1222/** 1149/**
1223 * usb_suspend_both - suspend a USB device and its interfaces 1150 * usb_suspend_both - suspend a USB device and its interfaces
1224 * @udev: the usb_device to suspend 1151 * @udev: the usb_device to suspend
@@ -1230,27 +1157,12 @@ static inline int autosuspend_check(struct usb_device *udev, int reschedule)
1230 * all the interfaces which were suspended are resumed so that they remain 1157 * all the interfaces which were suspended are resumed so that they remain
1231 * in the same state as the device. 1158 * in the same state as the device.
1232 * 1159 *
1233 * If an autosuspend is in progress the routine checks first to make sure 1160 * Autosuspend requests originating from a child device or an interface
1234 * that neither the device itself or any of its active interfaces is in use 1161 * driver may be made without the protection of @udev's device lock, but
1235 * (pm_usage_cnt is greater than 0). If they are, the autosuspend fails. 1162 * all other suspend calls will hold the lock. Usbcore will insure that
1236 * 1163 * method calls do not arrive during bind, unbind, or reset operations.
1237 * If the suspend succeeds, the routine recursively queues an autosuspend 1164 * However drivers must be prepared to handle suspend calls arriving at
1238 * request for @udev's parent device, thereby propagating the change up 1165 * unpredictable times.
1239 * the device tree. If all of the parent's children are now suspended,
1240 * the parent will autosuspend in turn.
1241 *
1242 * The suspend method calls are subject to mutual exclusion under control
1243 * of @udev's pm_mutex. Many of these calls are also under the protection
1244 * of @udev's device lock (including all requests originating outside the
1245 * USB subsystem), but autosuspend requests generated by a child device or
1246 * interface driver may not be. Usbcore will insure that the method calls
1247 * do not arrive during bind, unbind, or reset operations. However, drivers
1248 * must be prepared to handle suspend calls arriving at unpredictable times.
1249 * The only way to block such calls is to do an autoresume (preventing
1250 * autosuspends) while holding @udev's device lock (preventing outside
1251 * suspends).
1252 *
1253 * The caller must hold @udev->pm_mutex.
1254 * 1166 *
1255 * This routine can run only in process context. 1167 * This routine can run only in process context.
1256 */ 1168 */
@@ -1259,20 +1171,11 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1259 int status = 0; 1171 int status = 0;
1260 int i = 0; 1172 int i = 0;
1261 struct usb_interface *intf; 1173 struct usb_interface *intf;
1262 struct usb_device *parent = udev->parent;
1263 1174
1264 if (udev->state == USB_STATE_NOTATTACHED || 1175 if (udev->state == USB_STATE_NOTATTACHED ||
1265 udev->state == USB_STATE_SUSPENDED) 1176 udev->state == USB_STATE_SUSPENDED)
1266 goto done; 1177 goto done;
1267 1178
1268 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1269
1270 if (msg.event & PM_EVENT_AUTO) {
1271 status = autosuspend_check(udev, 0);
1272 if (status < 0)
1273 goto done;
1274 }
1275
1276 /* Suspend all the interfaces and then udev itself */ 1179 /* Suspend all the interfaces and then udev itself */
1277 if (udev->actconfig) { 1180 if (udev->actconfig) {
1278 for (; i < udev->actconfig->desc.bNumInterfaces; i++) { 1181 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
@@ -1287,35 +1190,21 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1287 1190
1288 /* If the suspend failed, resume interfaces that did get suspended */ 1191 /* If the suspend failed, resume interfaces that did get suspended */
1289 if (status != 0) { 1192 if (status != 0) {
1290 pm_message_t msg2; 1193 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1291
1292 msg2.event = msg.event ^ (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1293 while (--i >= 0) { 1194 while (--i >= 0) {
1294 intf = udev->actconfig->interface[i]; 1195 intf = udev->actconfig->interface[i];
1295 usb_resume_interface(udev, intf, msg2, 0); 1196 usb_resume_interface(udev, intf, msg, 0);
1296 } 1197 }
1297 1198
1298 /* Try another autosuspend when the interfaces aren't busy */ 1199 /* If the suspend succeeded then prevent any more URB submissions
1299 if (msg.event & PM_EVENT_AUTO) 1200 * and flush any outstanding URBs.
1300 autosuspend_check(udev, status == -EBUSY);
1301
1302 /* If the suspend succeeded then prevent any more URB submissions,
1303 * flush any outstanding URBs, and propagate the suspend up the tree.
1304 */ 1201 */
1305 } else { 1202 } else {
1306 cancel_delayed_work(&udev->autosuspend);
1307 udev->can_submit = 0; 1203 udev->can_submit = 0;
1308 for (i = 0; i < 16; ++i) { 1204 for (i = 0; i < 16; ++i) {
1309 usb_hcd_flush_endpoint(udev, udev->ep_out[i]); 1205 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1310 usb_hcd_flush_endpoint(udev, udev->ep_in[i]); 1206 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1311 } 1207 }
1312
1313 /* If this is just a FREEZE or a PRETHAW, udev might
1314 * not really be suspended. Only true suspends get
1315 * propagated up the device tree.
1316 */
1317 if (parent && udev->state == USB_STATE_SUSPENDED)
1318 usb_autosuspend_device(parent);
1319 } 1208 }
1320 1209
1321 done: 1210 done:
@@ -1332,23 +1221,12 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1332 * the resume method for @udev and then calls the resume methods for all 1221 * the resume method for @udev and then calls the resume methods for all
1333 * the interface drivers in @udev. 1222 * the interface drivers in @udev.
1334 * 1223 *
1335 * Before starting the resume, the routine calls itself recursively for 1224 * Autoresume requests originating from a child device or an interface
1336 * the parent device of @udev, thereby propagating the change up the device 1225 * driver may be made without the protection of @udev's device lock, but
1337 * tree and assuring that @udev will be able to resume. If the parent is 1226 * all other resume calls will hold the lock. Usbcore will insure that
1338 * unable to resume successfully, the routine fails. 1227 * method calls do not arrive during bind, unbind, or reset operations.
1339 * 1228 * However drivers must be prepared to handle resume calls arriving at
1340 * The resume method calls are subject to mutual exclusion under control 1229 * unpredictable times.
1341 * of @udev's pm_mutex. Many of these calls are also under the protection
1342 * of @udev's device lock (including all requests originating outside the
1343 * USB subsystem), but autoresume requests generated by a child device or
1344 * interface driver may not be. Usbcore will insure that the method calls
1345 * do not arrive during bind, unbind, or reset operations. However, drivers
1346 * must be prepared to handle resume calls arriving at unpredictable times.
1347 * The only way to block such calls is to do an autoresume (preventing
1348 * other autoresumes) while holding @udev's device lock (preventing outside
1349 * resumes).
1350 *
1351 * The caller must hold @udev->pm_mutex.
1352 * 1230 *
1353 * This routine can run only in process context. 1231 * This routine can run only in process context.
1354 */ 1232 */
@@ -1357,48 +1235,18 @@ static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
1357 int status = 0; 1235 int status = 0;
1358 int i; 1236 int i;
1359 struct usb_interface *intf; 1237 struct usb_interface *intf;
1360 struct usb_device *parent = udev->parent;
1361 1238
1362 cancel_delayed_work(&udev->autosuspend);
1363 if (udev->state == USB_STATE_NOTATTACHED) { 1239 if (udev->state == USB_STATE_NOTATTACHED) {
1364 status = -ENODEV; 1240 status = -ENODEV;
1365 goto done; 1241 goto done;
1366 } 1242 }
1367 udev->can_submit = 1; 1243 udev->can_submit = 1;
1368 1244
1369 /* Propagate the resume up the tree, if necessary */ 1245 /* Resume the device */
1370 if (udev->state == USB_STATE_SUSPENDED) { 1246 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
1371 if (parent) {
1372 status = usb_autoresume_device(parent);
1373 if (status == 0) {
1374 status = usb_resume_device(udev, msg);
1375 if (status || udev->state ==
1376 USB_STATE_NOTATTACHED) {
1377 usb_autosuspend_device(parent);
1378
1379 /* It's possible usb_resume_device()
1380 * failed after the port was
1381 * unsuspended, causing udev to be
1382 * logically disconnected. We don't
1383 * want usb_disconnect() to autosuspend
1384 * the parent again, so tell it that
1385 * udev disconnected while still
1386 * suspended. */
1387 if (udev->state ==
1388 USB_STATE_NOTATTACHED)
1389 udev->discon_suspended = 1;
1390 }
1391 }
1392 } else {
1393
1394 /* We can't progagate beyond the USB subsystem,
1395 * so if a root hub's controller is suspended
1396 * then we're stuck. */
1397 status = usb_resume_device(udev, msg);
1398 }
1399 } else if (udev->reset_resume)
1400 status = usb_resume_device(udev, msg); 1247 status = usb_resume_device(udev, msg);
1401 1248
1249 /* Resume the interfaces */
1402 if (status == 0 && udev->actconfig) { 1250 if (status == 0 && udev->actconfig) {
1403 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 1251 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1404 intf = udev->actconfig->interface[i]; 1252 intf = udev->actconfig->interface[i];
@@ -1414,55 +1262,94 @@ static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
1414 return status; 1262 return status;
1415} 1263}
1416 1264
1417#ifdef CONFIG_USB_SUSPEND 1265/* The device lock is held by the PM core */
1266int usb_suspend(struct device *dev, pm_message_t msg)
1267{
1268 struct usb_device *udev = to_usb_device(dev);
1418 1269
1419/* Internal routine to adjust a device's usage counter and change 1270 do_unbind_rebind(udev, DO_UNBIND);
1420 * its autosuspend state. 1271 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1421 */ 1272 return usb_suspend_both(udev, msg);
1422static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt) 1273}
1274
1275/* The device lock is held by the PM core */
1276int usb_resume(struct device *dev, pm_message_t msg)
1423{ 1277{
1424 int status = 0; 1278 struct usb_device *udev = to_usb_device(dev);
1279 int status;
1425 1280
1426 usb_pm_lock(udev); 1281 /* For PM complete calls, all we do is rebind interfaces */
1427 udev->pm_usage_cnt += inc_usage_cnt; 1282 if (msg.event == PM_EVENT_ON) {
1428 WARN_ON(udev->pm_usage_cnt < 0); 1283 if (udev->state != USB_STATE_NOTATTACHED)
1429 if (inc_usage_cnt) 1284 do_unbind_rebind(udev, DO_REBIND);
1430 udev->last_busy = jiffies; 1285 status = 0;
1431 if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) { 1286
1432 if (udev->state == USB_STATE_SUSPENDED) 1287 /* For all other calls, take the device back to full power and
1433 status = usb_resume_both(udev, PMSG_AUTO_RESUME); 1288 * tell the PM core in case it was autosuspended previously.
1434 if (status != 0) 1289 */
1435 udev->pm_usage_cnt -= inc_usage_cnt; 1290 } else {
1436 else if (inc_usage_cnt) 1291 status = usb_resume_both(udev, msg);
1292 if (status == 0) {
1293 pm_runtime_disable(dev);
1294 pm_runtime_set_active(dev);
1295 pm_runtime_enable(dev);
1437 udev->last_busy = jiffies; 1296 udev->last_busy = jiffies;
1438 } else if (inc_usage_cnt <= 0 && udev->pm_usage_cnt <= 0) { 1297 }
1439 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1440 } 1298 }
1441 usb_pm_unlock(udev); 1299
1300 /* Avoid PM error messages for devices disconnected while suspended
1301 * as we'll display regular disconnect messages just a bit later.
1302 */
1303 if (status == -ENODEV)
1304 status = 0;
1442 return status; 1305 return status;
1443} 1306}
1444 1307
1445/* usb_autosuspend_work - callback routine to autosuspend a USB device */ 1308#endif /* CONFIG_PM */
1446void usb_autosuspend_work(struct work_struct *work) 1309
1447{ 1310#ifdef CONFIG_USB_SUSPEND
1448 struct usb_device *udev =
1449 container_of(work, struct usb_device, autosuspend.work);
1450 1311
1451 usb_autopm_do_device(udev, 0); 1312/**
1313 * usb_enable_autosuspend - allow a USB device to be autosuspended
1314 * @udev: the USB device which may be autosuspended
1315 *
1316 * This routine allows @udev to be autosuspended. An autosuspend won't
1317 * take place until the autosuspend_delay has elapsed and all the other
1318 * necessary conditions are satisfied.
1319 *
1320 * The caller must hold @udev's device lock.
1321 */
1322int usb_enable_autosuspend(struct usb_device *udev)
1323{
1324 if (udev->autosuspend_disabled) {
1325 udev->autosuspend_disabled = 0;
1326 usb_autosuspend_device(udev);
1327 }
1328 return 0;
1452} 1329}
1330EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1453 1331
1454/* usb_autoresume_work - callback routine to autoresume a USB device */ 1332/**
1455void usb_autoresume_work(struct work_struct *work) 1333 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1334 * @udev: the USB device which may not be autosuspended
1335 *
1336 * This routine prevents @udev from being autosuspended and wakes it up
1337 * if it is already autosuspended.
1338 *
1339 * The caller must hold @udev's device lock.
1340 */
1341int usb_disable_autosuspend(struct usb_device *udev)
1456{ 1342{
1457 struct usb_device *udev = 1343 int rc = 0;
1458 container_of(work, struct usb_device, autoresume);
1459 1344
1460 /* Wake it up, let the drivers do their thing, and then put it 1345 if (!udev->autosuspend_disabled) {
1461 * back to sleep. 1346 rc = usb_autoresume_device(udev);
1462 */ 1347 if (rc == 0)
1463 if (usb_autopm_do_device(udev, 1) == 0) 1348 udev->autosuspend_disabled = 1;
1464 usb_autopm_do_device(udev, -1); 1349 }
1350 return rc;
1465} 1351}
1352EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1466 1353
1467/** 1354/**
1468 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces 1355 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
@@ -1472,15 +1359,11 @@ void usb_autoresume_work(struct work_struct *work)
1472 * @udev and wants to allow it to autosuspend. Examples would be when 1359 * @udev and wants to allow it to autosuspend. Examples would be when
1473 * @udev's device file in usbfs is closed or after a configuration change. 1360 * @udev's device file in usbfs is closed or after a configuration change.
1474 * 1361 *
1475 * @udev's usage counter is decremented. If it or any of the usage counters 1362 * @udev's usage counter is decremented; if it drops to 0 and all the
1476 * for an active interface is greater than 0, no autosuspend request will be 1363 * interfaces are inactive then a delayed autosuspend will be attempted.
1477 * queued. (If an interface driver does not support autosuspend then its 1364 * The attempt may fail (see autosuspend_check()).
1478 * usage counter is permanently positive.) Furthermore, if an interface
1479 * driver requires remote-wakeup capability during autosuspend but remote
1480 * wakeup is disabled, the autosuspend will fail.
1481 * 1365 *
1482 * Often the caller will hold @udev's device lock, but this is not 1366 * The caller must hold @udev's device lock.
1483 * necessary.
1484 * 1367 *
1485 * This routine can run only in process context. 1368 * This routine can run only in process context.
1486 */ 1369 */
@@ -1488,9 +1371,11 @@ void usb_autosuspend_device(struct usb_device *udev)
1488{ 1371{
1489 int status; 1372 int status;
1490 1373
1491 status = usb_autopm_do_device(udev, -1); 1374 udev->last_busy = jiffies;
1492 dev_vdbg(&udev->dev, "%s: cnt %d\n", 1375 status = pm_runtime_put_sync(&udev->dev);
1493 __func__, udev->pm_usage_cnt); 1376 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1377 __func__, atomic_read(&udev->dev.power.usage_count),
1378 status);
1494} 1379}
1495 1380
1496/** 1381/**
@@ -1500,17 +1385,22 @@ void usb_autosuspend_device(struct usb_device *udev)
1500 * This routine should be called when a core subsystem thinks @udev may 1385 * This routine should be called when a core subsystem thinks @udev may
1501 * be ready to autosuspend. 1386 * be ready to autosuspend.
1502 * 1387 *
1503 * @udev's usage counter left unchanged. If it or any of the usage counters 1388 * @udev's usage counter left unchanged. If it is 0 and all the interfaces
1504 * for an active interface is greater than 0, or autosuspend is not allowed 1389 * are inactive then an autosuspend will be attempted. The attempt may
1505 * for any other reason, no autosuspend request will be queued. 1390 * fail or be delayed.
1391 *
1392 * The caller must hold @udev's device lock.
1506 * 1393 *
1507 * This routine can run only in process context. 1394 * This routine can run only in process context.
1508 */ 1395 */
1509void usb_try_autosuspend_device(struct usb_device *udev) 1396void usb_try_autosuspend_device(struct usb_device *udev)
1510{ 1397{
1511 usb_autopm_do_device(udev, 0); 1398 int status;
1512 dev_vdbg(&udev->dev, "%s: cnt %d\n", 1399
1513 __func__, udev->pm_usage_cnt); 1400 status = pm_runtime_idle(&udev->dev);
1401 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1402 __func__, atomic_read(&udev->dev.power.usage_count),
1403 status);
1514} 1404}
1515 1405
1516/** 1406/**
@@ -1519,16 +1409,15 @@ void usb_try_autosuspend_device(struct usb_device *udev)
1519 * 1409 *
1520 * This routine should be called when a core subsystem wants to use @udev 1410 * This routine should be called when a core subsystem wants to use @udev
1521 * and needs to guarantee that it is not suspended. No autosuspend will 1411 * and needs to guarantee that it is not suspended. No autosuspend will
1522 * occur until usb_autosuspend_device is called. (Note that this will not 1412 * occur until usb_autosuspend_device() is called. (Note that this will
1523 * prevent suspend events originating in the PM core.) Examples would be 1413 * not prevent suspend events originating in the PM core.) Examples would
1524 * when @udev's device file in usbfs is opened or when a remote-wakeup 1414 * be when @udev's device file in usbfs is opened or when a remote-wakeup
1525 * request is received. 1415 * request is received.
1526 * 1416 *
1527 * @udev's usage counter is incremented to prevent subsequent autosuspends. 1417 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1528 * However if the autoresume fails then the usage counter is re-decremented. 1418 * However if the autoresume fails then the usage counter is re-decremented.
1529 * 1419 *
1530 * Often the caller will hold @udev's device lock, but this is not 1420 * The caller must hold @udev's device lock.
1531 * necessary (and attempting it might cause deadlock).
1532 * 1421 *
1533 * This routine can run only in process context. 1422 * This routine can run only in process context.
1534 */ 1423 */
@@ -1536,42 +1425,14 @@ int usb_autoresume_device(struct usb_device *udev)
1536{ 1425{
1537 int status; 1426 int status;
1538 1427
1539 status = usb_autopm_do_device(udev, 1); 1428 status = pm_runtime_get_sync(&udev->dev);
1540 dev_vdbg(&udev->dev, "%s: status %d cnt %d\n", 1429 if (status < 0)
1541 __func__, status, udev->pm_usage_cnt); 1430 pm_runtime_put_sync(&udev->dev);
1542 return status; 1431 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1543} 1432 __func__, atomic_read(&udev->dev.power.usage_count),
1544 1433 status);
1545/* Internal routine to adjust an interface's usage counter and change 1434 if (status > 0)
1546 * its device's autosuspend state. 1435 status = 0;
1547 */
1548static int usb_autopm_do_interface(struct usb_interface *intf,
1549 int inc_usage_cnt)
1550{
1551 struct usb_device *udev = interface_to_usbdev(intf);
1552 int status = 0;
1553
1554 usb_pm_lock(udev);
1555 if (intf->condition == USB_INTERFACE_UNBOUND)
1556 status = -ENODEV;
1557 else {
1558 atomic_add(inc_usage_cnt, &intf->pm_usage_cnt);
1559 udev->last_busy = jiffies;
1560 if (inc_usage_cnt >= 0 &&
1561 atomic_read(&intf->pm_usage_cnt) > 0) {
1562 if (udev->state == USB_STATE_SUSPENDED)
1563 status = usb_resume_both(udev,
1564 PMSG_AUTO_RESUME);
1565 if (status != 0)
1566 atomic_sub(inc_usage_cnt, &intf->pm_usage_cnt);
1567 else
1568 udev->last_busy = jiffies;
1569 } else if (inc_usage_cnt <= 0 &&
1570 atomic_read(&intf->pm_usage_cnt) <= 0) {
1571 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1572 }
1573 }
1574 usb_pm_unlock(udev);
1575 return status; 1436 return status;
1576} 1437}
1577 1438
@@ -1585,34 +1446,25 @@ static int usb_autopm_do_interface(struct usb_interface *intf,
1585 * closed. 1446 * closed.
1586 * 1447 *
1587 * The routine decrements @intf's usage counter. When the counter reaches 1448 * The routine decrements @intf's usage counter. When the counter reaches
1588 * 0, a delayed autosuspend request for @intf's device is queued. When 1449 * 0, a delayed autosuspend request for @intf's device is attempted. The
1589 * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all 1450 * attempt may fail (see autosuspend_check()).
1590 * the other usage counters for the sibling interfaces and @intf's
1591 * usb_device, the device and all its interfaces will be autosuspended.
1592 *
1593 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1594 * core will not change its value other than the increment and decrement
1595 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1596 * may use this simple counter-oriented discipline or may set the value
1597 * any way it likes.
1598 * 1451 *
1599 * If the driver has set @intf->needs_remote_wakeup then autosuspend will 1452 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1600 * take place only if the device's remote-wakeup facility is enabled. 1453 * take place only if the device's remote-wakeup facility is enabled.
1601 * 1454 *
1602 * Suspend method calls queued by this routine can arrive at any time
1603 * while @intf is resumed and its usage counter is equal to 0. They are
1604 * not protected by the usb_device's lock but only by its pm_mutex.
1605 * Drivers must provide their own synchronization.
1606 *
1607 * This routine can run only in process context. 1455 * This routine can run only in process context.
1608 */ 1456 */
1609void usb_autopm_put_interface(struct usb_interface *intf) 1457void usb_autopm_put_interface(struct usb_interface *intf)
1610{ 1458{
1611 int status; 1459 struct usb_device *udev = interface_to_usbdev(intf);
1460 int status;
1612 1461
1613 status = usb_autopm_do_interface(intf, -1); 1462 udev->last_busy = jiffies;
1614 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n", 1463 atomic_dec(&intf->pm_usage_cnt);
1615 __func__, status, atomic_read(&intf->pm_usage_cnt)); 1464 status = pm_runtime_put_sync(&intf->dev);
1465 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1466 __func__, atomic_read(&intf->dev.power.usage_count),
1467 status);
1616} 1468}
1617EXPORT_SYMBOL_GPL(usb_autopm_put_interface); 1469EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1618 1470
@@ -1620,11 +1472,11 @@ EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1620 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter 1472 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1621 * @intf: the usb_interface whose counter should be decremented 1473 * @intf: the usb_interface whose counter should be decremented
1622 * 1474 *
1623 * This routine does essentially the same thing as 1475 * This routine does much the same thing as usb_autopm_put_interface():
1624 * usb_autopm_put_interface(): it decrements @intf's usage counter and 1476 * It decrements @intf's usage counter and schedules a delayed
1625 * queues a delayed autosuspend request if the counter is <= 0. The 1477 * autosuspend request if the counter is <= 0. The difference is that it
1626 * difference is that it does not acquire the device's pm_mutex; 1478 * does not perform any synchronization; callers should hold a private
1627 * callers must handle all synchronization issues themselves. 1479 * lock and handle all synchronization issues themselves.
1628 * 1480 *
1629 * Typically a driver would call this routine during an URB's completion 1481 * Typically a driver would call this routine during an URB's completion
1630 * handler, if no more URBs were pending. 1482 * handler, if no more URBs were pending.
@@ -1634,28 +1486,58 @@ EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1634void usb_autopm_put_interface_async(struct usb_interface *intf) 1486void usb_autopm_put_interface_async(struct usb_interface *intf)
1635{ 1487{
1636 struct usb_device *udev = interface_to_usbdev(intf); 1488 struct usb_device *udev = interface_to_usbdev(intf);
1489 unsigned long last_busy;
1637 int status = 0; 1490 int status = 0;
1638 1491
1639 if (intf->condition == USB_INTERFACE_UNBOUND) { 1492 last_busy = udev->last_busy;
1640 status = -ENODEV; 1493 udev->last_busy = jiffies;
1641 } else { 1494 atomic_dec(&intf->pm_usage_cnt);
1642 udev->last_busy = jiffies; 1495 pm_runtime_put_noidle(&intf->dev);
1643 atomic_dec(&intf->pm_usage_cnt); 1496
1644 if (udev->autosuspend_disabled || udev->autosuspend_delay < 0) 1497 if (!udev->autosuspend_disabled) {
1645 status = -EPERM; 1498 /* Optimization: Don't schedule a delayed autosuspend if
1646 else if (atomic_read(&intf->pm_usage_cnt) <= 0 && 1499 * the timer is already running and the expiration time
1647 !timer_pending(&udev->autosuspend.timer)) { 1500 * wouldn't change.
1648 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend, 1501 *
1502 * We have to use the interface's timer. Attempts to
1503 * schedule a suspend for the device would fail because
1504 * the interface is still active.
1505 */
1506 if (intf->dev.power.timer_expires == 0 ||
1507 round_jiffies_up(last_busy) !=
1508 round_jiffies_up(jiffies)) {
1509 status = pm_schedule_suspend(&intf->dev,
1510 jiffies_to_msecs(
1649 round_jiffies_up_relative( 1511 round_jiffies_up_relative(
1650 udev->autosuspend_delay)); 1512 udev->autosuspend_delay)));
1651 } 1513 }
1652 } 1514 }
1653 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n", 1515 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1654 __func__, status, atomic_read(&intf->pm_usage_cnt)); 1516 __func__, atomic_read(&intf->dev.power.usage_count),
1517 status);
1655} 1518}
1656EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async); 1519EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1657 1520
1658/** 1521/**
1522 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1523 * @intf: the usb_interface whose counter should be decremented
1524 *
1525 * This routine decrements @intf's usage counter but does not carry out an
1526 * autosuspend.
1527 *
1528 * This routine can run in atomic context.
1529 */
1530void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1531{
1532 struct usb_device *udev = interface_to_usbdev(intf);
1533
1534 udev->last_busy = jiffies;
1535 atomic_dec(&intf->pm_usage_cnt);
1536 pm_runtime_put_noidle(&intf->dev);
1537}
1538EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1539
1540/**
1659 * usb_autopm_get_interface - increment a USB interface's PM-usage counter 1541 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1660 * @intf: the usb_interface whose counter should be incremented 1542 * @intf: the usb_interface whose counter should be incremented
1661 * 1543 *
@@ -1667,25 +1549,8 @@ EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1667 * or @intf is unbound. A typical example would be a character-device 1549 * or @intf is unbound. A typical example would be a character-device
1668 * driver when its device file is opened. 1550 * driver when its device file is opened.
1669 * 1551 *
1670 * 1552 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1671 * The routine increments @intf's usage counter. (However if the 1553 * However if the autoresume fails then the counter is re-decremented.
1672 * autoresume fails then the counter is re-decremented.) So long as the
1673 * counter is greater than 0, autosuspend will not be allowed for @intf
1674 * or its usb_device. When the driver is finished using @intf it should
1675 * call usb_autopm_put_interface() to decrement the usage counter and
1676 * queue a delayed autosuspend request (if the counter is <= 0).
1677 *
1678 *
1679 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1680 * core will not change its value other than the increment and decrement
1681 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1682 * may use this simple counter-oriented discipline or may set the value
1683 * any way it likes.
1684 *
1685 * Resume method calls generated by this routine can arrive at any time
1686 * while @intf is suspended. They are not protected by the usb_device's
1687 * lock but only by its pm_mutex. Drivers must provide their own
1688 * synchronization.
1689 * 1554 *
1690 * This routine can run only in process context. 1555 * This routine can run only in process context.
1691 */ 1556 */
@@ -1693,9 +1558,16 @@ int usb_autopm_get_interface(struct usb_interface *intf)
1693{ 1558{
1694 int status; 1559 int status;
1695 1560
1696 status = usb_autopm_do_interface(intf, 1); 1561 status = pm_runtime_get_sync(&intf->dev);
1697 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n", 1562 if (status < 0)
1698 __func__, status, atomic_read(&intf->pm_usage_cnt)); 1563 pm_runtime_put_sync(&intf->dev);
1564 else
1565 atomic_inc(&intf->pm_usage_cnt);
1566 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1567 __func__, atomic_read(&intf->dev.power.usage_count),
1568 status);
1569 if (status > 0)
1570 status = 0;
1699 return status; 1571 return status;
1700} 1572}
1701EXPORT_SYMBOL_GPL(usb_autopm_get_interface); 1573EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
@@ -1705,149 +1577,207 @@ EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1705 * @intf: the usb_interface whose counter should be incremented 1577 * @intf: the usb_interface whose counter should be incremented
1706 * 1578 *
1707 * This routine does much the same thing as 1579 * This routine does much the same thing as
1708 * usb_autopm_get_interface(): it increments @intf's usage counter and 1580 * usb_autopm_get_interface(): It increments @intf's usage counter and
1709 * queues an autoresume request if the result is > 0. The differences 1581 * queues an autoresume request if the device is suspended. The
1710 * are that it does not acquire the device's pm_mutex (callers must 1582 * differences are that it does not perform any synchronization (callers
1711 * handle all synchronization issues themselves), and it does not 1583 * should hold a private lock and handle all synchronization issues
1712 * autoresume the device directly (it only queues a request). After a 1584 * themselves), and it does not autoresume the device directly (it only
1713 * successful call, the device will generally not yet be resumed. 1585 * queues a request). After a successful call, the device may not yet be
1586 * resumed.
1714 * 1587 *
1715 * This routine can run in atomic context. 1588 * This routine can run in atomic context.
1716 */ 1589 */
1717int usb_autopm_get_interface_async(struct usb_interface *intf) 1590int usb_autopm_get_interface_async(struct usb_interface *intf)
1718{ 1591{
1719 struct usb_device *udev = interface_to_usbdev(intf); 1592 int status = 0;
1720 int status = 0; 1593 enum rpm_status s;
1721 1594
1722 if (intf->condition == USB_INTERFACE_UNBOUND) 1595 /* Don't request a resume unless the interface is already suspending
1723 status = -ENODEV; 1596 * or suspended. Doing so would force a running suspend timer to be
1724 else { 1597 * cancelled.
1598 */
1599 pm_runtime_get_noresume(&intf->dev);
1600 s = ACCESS_ONCE(intf->dev.power.runtime_status);
1601 if (s == RPM_SUSPENDING || s == RPM_SUSPENDED)
1602 status = pm_request_resume(&intf->dev);
1603
1604 if (status < 0 && status != -EINPROGRESS)
1605 pm_runtime_put_noidle(&intf->dev);
1606 else
1725 atomic_inc(&intf->pm_usage_cnt); 1607 atomic_inc(&intf->pm_usage_cnt);
1726 if (atomic_read(&intf->pm_usage_cnt) > 0 && 1608 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1727 udev->state == USB_STATE_SUSPENDED) 1609 __func__, atomic_read(&intf->dev.power.usage_count),
1728 queue_work(ksuspend_usb_wq, &udev->autoresume); 1610 status);
1729 } 1611 if (status > 0)
1730 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n", 1612 status = 0;
1731 __func__, status, atomic_read(&intf->pm_usage_cnt));
1732 return status; 1613 return status;
1733} 1614}
1734EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async); 1615EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1735 1616
1736#else
1737
1738void usb_autosuspend_work(struct work_struct *work)
1739{}
1740
1741void usb_autoresume_work(struct work_struct *work)
1742{}
1743
1744#endif /* CONFIG_USB_SUSPEND */
1745
1746/** 1617/**
1747 * usb_external_suspend_device - external suspend of a USB device and its interfaces 1618 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1748 * @udev: the usb_device to suspend 1619 * @intf: the usb_interface whose counter should be incremented
1749 * @msg: Power Management message describing this state transition
1750 * 1620 *
1751 * This routine handles external suspend requests: ones not generated 1621 * This routine increments @intf's usage counter but does not carry out an
1752 * internally by a USB driver (autosuspend) but rather coming from the user 1622 * autoresume.
1753 * (via sysfs) or the PM core (system sleep). The suspend will be carried
1754 * out regardless of @udev's usage counter or those of its interfaces,
1755 * and regardless of whether or not remote wakeup is enabled. Of course,
1756 * interface drivers still have the option of failing the suspend (if
1757 * there are unsuspended children, for example).
1758 * 1623 *
1759 * The caller must hold @udev's device lock. 1624 * This routine can run in atomic context.
1760 */ 1625 */
1761int usb_external_suspend_device(struct usb_device *udev, pm_message_t msg) 1626void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1762{ 1627{
1763 int status; 1628 struct usb_device *udev = interface_to_usbdev(intf);
1764 1629
1765 do_unbind_rebind(udev, DO_UNBIND); 1630 udev->last_busy = jiffies;
1766 usb_pm_lock(udev); 1631 atomic_inc(&intf->pm_usage_cnt);
1767 status = usb_suspend_both(udev, msg); 1632 pm_runtime_get_noresume(&intf->dev);
1768 usb_pm_unlock(udev);
1769 return status;
1770} 1633}
1634EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1771 1635
1772/** 1636/* Internal routine to check whether we may autosuspend a device. */
1773 * usb_external_resume_device - external resume of a USB device and its interfaces 1637static int autosuspend_check(struct usb_device *udev)
1774 * @udev: the usb_device to resume
1775 * @msg: Power Management message describing this state transition
1776 *
1777 * This routine handles external resume requests: ones not generated
1778 * internally by a USB driver (autoresume) but rather coming from the user
1779 * (via sysfs), the PM core (system resume), or the device itself (remote
1780 * wakeup). @udev's usage counter is unaffected.
1781 *
1782 * The caller must hold @udev's device lock.
1783 */
1784int usb_external_resume_device(struct usb_device *udev, pm_message_t msg)
1785{ 1638{
1786 int status; 1639 int i;
1640 struct usb_interface *intf;
1641 unsigned long suspend_time, j;
1787 1642
1788 usb_pm_lock(udev); 1643 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1789 status = usb_resume_both(udev, msg); 1644 * any interface drivers require remote wakeup but it isn't available.
1790 udev->last_busy = jiffies; 1645 */
1791 usb_pm_unlock(udev); 1646 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1792 if (status == 0) 1647 if (udev->actconfig) {
1793 do_unbind_rebind(udev, DO_REBIND); 1648 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1649 intf = udev->actconfig->interface[i];
1794 1650
1795 /* Now that the device is awake, we can start trying to autosuspend 1651 /* We don't need to check interfaces that are
1796 * it again. */ 1652 * disabled for runtime PM. Either they are unbound
1797 if (status == 0) 1653 * or else their drivers don't support autosuspend
1798 usb_try_autosuspend_device(udev); 1654 * and so they are permanently active.
1799 return status; 1655 */
1656 if (intf->dev.power.disable_depth)
1657 continue;
1658 if (atomic_read(&intf->dev.power.usage_count) > 0)
1659 return -EBUSY;
1660 if (intf->needs_remote_wakeup &&
1661 !udev->do_remote_wakeup) {
1662 dev_dbg(&udev->dev, "remote wakeup needed "
1663 "for autosuspend\n");
1664 return -EOPNOTSUPP;
1665 }
1666
1667 /* Don't allow autosuspend if the device will need
1668 * a reset-resume and any of its interface drivers
1669 * doesn't include support or needs remote wakeup.
1670 */
1671 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1672 struct usb_driver *driver;
1673
1674 driver = to_usb_driver(intf->dev.driver);
1675 if (!driver->reset_resume ||
1676 intf->needs_remote_wakeup)
1677 return -EOPNOTSUPP;
1678 }
1679 }
1680 }
1681
1682 /* If everything is okay but the device hasn't been idle for long
1683 * enough, queue a delayed autosuspend request.
1684 */
1685 j = ACCESS_ONCE(jiffies);
1686 suspend_time = udev->last_busy + udev->autosuspend_delay;
1687 if (time_before(j, suspend_time)) {
1688 pm_schedule_suspend(&udev->dev, jiffies_to_msecs(
1689 round_jiffies_up_relative(suspend_time - j)));
1690 return -EAGAIN;
1691 }
1692 return 0;
1800} 1693}
1801 1694
1802int usb_suspend(struct device *dev, pm_message_t msg) 1695static int usb_runtime_suspend(struct device *dev)
1803{ 1696{
1804 struct usb_device *udev; 1697 int status = 0;
1805
1806 udev = to_usb_device(dev);
1807 1698
1808 /* If udev is already suspended, we can skip this suspend and 1699 /* A USB device can be suspended if it passes the various autosuspend
1809 * we should also skip the upcoming system resume. High-speed 1700 * checks. Runtime suspend for a USB device means suspending all the
1810 * root hubs are an exception; they need to resume whenever the 1701 * interfaces and then the device itself.
1811 * system wakes up in order for USB-PERSIST port handover to work
1812 * properly.
1813 */ 1702 */
1814 if (udev->state == USB_STATE_SUSPENDED) { 1703 if (is_usb_device(dev)) {
1815 if (udev->parent || udev->speed != USB_SPEED_HIGH) 1704 struct usb_device *udev = to_usb_device(dev);
1816 udev->skip_sys_resume = 1; 1705
1817 return 0; 1706 if (autosuspend_check(udev) != 0)
1707 return -EAGAIN;
1708
1709 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1710
1711 /* If an interface fails the suspend, adjust the last_busy
1712 * time so that we don't get another suspend attempt right
1713 * away.
1714 */
1715 if (status) {
1716 udev->last_busy = jiffies +
1717 (udev->autosuspend_delay == 0 ?
1718 HZ/2 : 0);
1719 }
1720
1721 /* Prevent the parent from suspending immediately after */
1722 else if (udev->parent) {
1723 udev->parent->last_busy = jiffies;
1724 }
1818 } 1725 }
1819 1726
1820 udev->skip_sys_resume = 0; 1727 /* Runtime suspend for a USB interface doesn't mean anything. */
1821 return usb_external_suspend_device(udev, msg); 1728 return status;
1822} 1729}
1823 1730
1824int usb_resume(struct device *dev, pm_message_t msg) 1731static int usb_runtime_resume(struct device *dev)
1825{ 1732{
1826 struct usb_device *udev; 1733 /* Runtime resume for a USB device means resuming both the device
1827 int status; 1734 * and all its interfaces.
1735 */
1736 if (is_usb_device(dev)) {
1737 struct usb_device *udev = to_usb_device(dev);
1738 int status;
1828 1739
1829 udev = to_usb_device(dev); 1740 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
1741 udev->last_busy = jiffies;
1742 return status;
1743 }
1830 1744
1831 /* If udev->skip_sys_resume is set then udev was already suspended 1745 /* Runtime resume for a USB interface doesn't mean anything. */
1832 * when the system sleep started, so we don't want to resume it 1746 return 0;
1833 * during this system wakeup. 1747}
1834 */
1835 if (udev->skip_sys_resume)
1836 return 0;
1837 status = usb_external_resume_device(udev, msg);
1838 1748
1839 /* Avoid PM error messages for devices disconnected while suspended 1749static int usb_runtime_idle(struct device *dev)
1840 * as we'll display regular disconnect messages just a bit later. 1750{
1751 /* An idle USB device can be suspended if it passes the various
1752 * autosuspend checks. An idle interface can be suspended at
1753 * any time.
1841 */ 1754 */
1842 if (status == -ENODEV) 1755 if (is_usb_device(dev)) {
1843 return 0; 1756 struct usb_device *udev = to_usb_device(dev);
1844 return status; 1757
1758 if (autosuspend_check(udev) != 0)
1759 return 0;
1760 }
1761
1762 pm_runtime_suspend(dev);
1763 return 0;
1845} 1764}
1846 1765
1847#endif /* CONFIG_PM */ 1766static struct dev_pm_ops usb_bus_pm_ops = {
1767 .runtime_suspend = usb_runtime_suspend,
1768 .runtime_resume = usb_runtime_resume,
1769 .runtime_idle = usb_runtime_idle,
1770};
1771
1772#else
1773
1774#define usb_bus_pm_ops (*(struct dev_pm_ops *) NULL)
1775
1776#endif /* CONFIG_USB_SUSPEND */
1848 1777
1849struct bus_type usb_bus_type = { 1778struct bus_type usb_bus_type = {
1850 .name = "usb", 1779 .name = "usb",
1851 .match = usb_device_match, 1780 .match = usb_device_match,
1852 .uevent = usb_uevent, 1781 .uevent = usb_uevent,
1782 .pm = &usb_bus_pm_ops,
1853}; 1783};
diff --git a/drivers/usb/core/file.c b/drivers/usb/core/file.c
index bfc6c2eea647..c3536f151f02 100644
--- a/drivers/usb/core/file.c
+++ b/drivers/usb/core/file.c
@@ -34,7 +34,6 @@ static int usb_open(struct inode * inode, struct file * file)
34 int err = -ENODEV; 34 int err = -ENODEV;
35 const struct file_operations *old_fops, *new_fops = NULL; 35 const struct file_operations *old_fops, *new_fops = NULL;
36 36
37 lock_kernel();
38 down_read(&minor_rwsem); 37 down_read(&minor_rwsem);
39 c = usb_minors[minor]; 38 c = usb_minors[minor];
40 39
@@ -53,7 +52,6 @@ static int usb_open(struct inode * inode, struct file * file)
53 fops_put(old_fops); 52 fops_put(old_fops);
54 done: 53 done:
55 up_read(&minor_rwsem); 54 up_read(&minor_rwsem);
56 unlock_kernel();
57 return err; 55 return err;
58} 56}
59 57
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 80995ef0868c..2f8cedda8007 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -39,6 +39,7 @@
39#include <linux/platform_device.h> 39#include <linux/platform_device.h>
40#include <linux/workqueue.h> 40#include <linux/workqueue.h>
41#include <linux/mutex.h> 41#include <linux/mutex.h>
42#include <linux/pm_runtime.h>
42 43
43#include <linux/usb.h> 44#include <linux/usb.h>
44 45
@@ -141,7 +142,7 @@ static const u8 usb3_rh_dev_descriptor[18] = {
141 0x09, /* __u8 bMaxPacketSize0; 2^9 = 512 Bytes */ 142 0x09, /* __u8 bMaxPacketSize0; 2^9 = 512 Bytes */
142 143
143 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */ 144 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */
144 0x02, 0x00, /* __le16 idProduct; device 0x0002 */ 145 0x03, 0x00, /* __le16 idProduct; device 0x0003 */
145 KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ 146 KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
146 147
147 0x03, /* __u8 iManufacturer; */ 148 0x03, /* __u8 iManufacturer; */
@@ -1670,11 +1671,16 @@ int usb_hcd_alloc_bandwidth(struct usb_device *udev,
1670 } 1671 }
1671 } 1672 }
1672 for (i = 0; i < num_intfs; ++i) { 1673 for (i = 0; i < num_intfs; ++i) {
1674 struct usb_host_interface *first_alt;
1675 int iface_num;
1676
1677 first_alt = &new_config->intf_cache[i]->altsetting[0];
1678 iface_num = first_alt->desc.bInterfaceNumber;
1673 /* Set up endpoints for alternate interface setting 0 */ 1679 /* Set up endpoints for alternate interface setting 0 */
1674 alt = usb_find_alt_setting(new_config, i, 0); 1680 alt = usb_find_alt_setting(new_config, iface_num, 0);
1675 if (!alt) 1681 if (!alt)
1676 /* No alt setting 0? Pick the first setting. */ 1682 /* No alt setting 0? Pick the first setting. */
1677 alt = &new_config->intf_cache[i]->altsetting[0]; 1683 alt = first_alt;
1678 1684
1679 for (j = 0; j < alt->desc.bNumEndpoints; j++) { 1685 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
1680 ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]); 1686 ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]);
@@ -1853,6 +1859,10 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
1853 return status; 1859 return status;
1854} 1860}
1855 1861
1862#endif /* CONFIG_PM */
1863
1864#ifdef CONFIG_USB_SUSPEND
1865
1856/* Workqueue routine for root-hub remote wakeup */ 1866/* Workqueue routine for root-hub remote wakeup */
1857static void hcd_resume_work(struct work_struct *work) 1867static void hcd_resume_work(struct work_struct *work)
1858{ 1868{
@@ -1860,8 +1870,7 @@ static void hcd_resume_work(struct work_struct *work)
1860 struct usb_device *udev = hcd->self.root_hub; 1870 struct usb_device *udev = hcd->self.root_hub;
1861 1871
1862 usb_lock_device(udev); 1872 usb_lock_device(udev);
1863 usb_mark_last_busy(udev); 1873 usb_remote_wakeup(udev);
1864 usb_external_resume_device(udev, PMSG_REMOTE_RESUME);
1865 usb_unlock_device(udev); 1874 usb_unlock_device(udev);
1866} 1875}
1867 1876
@@ -1880,12 +1889,12 @@ void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
1880 1889
1881 spin_lock_irqsave (&hcd_root_hub_lock, flags); 1890 spin_lock_irqsave (&hcd_root_hub_lock, flags);
1882 if (hcd->rh_registered) 1891 if (hcd->rh_registered)
1883 queue_work(ksuspend_usb_wq, &hcd->wakeup_work); 1892 queue_work(pm_wq, &hcd->wakeup_work);
1884 spin_unlock_irqrestore (&hcd_root_hub_lock, flags); 1893 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
1885} 1894}
1886EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub); 1895EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
1887 1896
1888#endif 1897#endif /* CONFIG_USB_SUSPEND */
1889 1898
1890/*-------------------------------------------------------------------------*/ 1899/*-------------------------------------------------------------------------*/
1891 1900
@@ -2030,7 +2039,7 @@ struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
2030 init_timer(&hcd->rh_timer); 2039 init_timer(&hcd->rh_timer);
2031 hcd->rh_timer.function = rh_timer_func; 2040 hcd->rh_timer.function = rh_timer_func;
2032 hcd->rh_timer.data = (unsigned long) hcd; 2041 hcd->rh_timer.data = (unsigned long) hcd;
2033#ifdef CONFIG_PM 2042#ifdef CONFIG_USB_SUSPEND
2034 INIT_WORK(&hcd->wakeup_work, hcd_resume_work); 2043 INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
2035#endif 2044#endif
2036 mutex_init(&hcd->bandwidth_mutex); 2045 mutex_init(&hcd->bandwidth_mutex);
@@ -2230,7 +2239,7 @@ void usb_remove_hcd(struct usb_hcd *hcd)
2230 hcd->rh_registered = 0; 2239 hcd->rh_registered = 0;
2231 spin_unlock_irq (&hcd_root_hub_lock); 2240 spin_unlock_irq (&hcd_root_hub_lock);
2232 2241
2233#ifdef CONFIG_PM 2242#ifdef CONFIG_USB_SUSPEND
2234 cancel_work_sync(&hcd->wakeup_work); 2243 cancel_work_sync(&hcd->wakeup_work);
2235#endif 2244#endif
2236 2245
diff --git a/drivers/usb/core/hcd.h b/drivers/usb/core/hcd.h
index bbe2b924aae8..a3cdb09734ab 100644
--- a/drivers/usb/core/hcd.h
+++ b/drivers/usb/core/hcd.h
@@ -80,7 +80,7 @@ struct usb_hcd {
80 80
81 struct timer_list rh_timer; /* drives root-hub polling */ 81 struct timer_list rh_timer; /* drives root-hub polling */
82 struct urb *status_urb; /* the current status urb */ 82 struct urb *status_urb; /* the current status urb */
83#ifdef CONFIG_PM 83#ifdef CONFIG_USB_SUSPEND
84 struct work_struct wakeup_work; /* for remote wakeup */ 84 struct work_struct wakeup_work; /* for remote wakeup */
85#endif 85#endif
86 86
@@ -248,7 +248,7 @@ struct hc_driver {
248 /* xHCI specific functions */ 248 /* xHCI specific functions */
249 /* Called by usb_alloc_dev to alloc HC device structures */ 249 /* Called by usb_alloc_dev to alloc HC device structures */
250 int (*alloc_dev)(struct usb_hcd *, struct usb_device *); 250 int (*alloc_dev)(struct usb_hcd *, struct usb_device *);
251 /* Called by usb_release_dev to free HC device structures */ 251 /* Called by usb_disconnect to free HC device structures */
252 void (*free_dev)(struct usb_hcd *, struct usb_device *); 252 void (*free_dev)(struct usb_hcd *, struct usb_device *);
253 253
254 /* Bandwidth computation functions */ 254 /* Bandwidth computation functions */
@@ -286,6 +286,7 @@ struct hc_driver {
286 */ 286 */
287 int (*update_hub_device)(struct usb_hcd *, struct usb_device *hdev, 287 int (*update_hub_device)(struct usb_hcd *, struct usb_device *hdev,
288 struct usb_tt *tt, gfp_t mem_flags); 288 struct usb_tt *tt, gfp_t mem_flags);
289 int (*reset_device)(struct usb_hcd *, struct usb_device *);
289}; 290};
290 291
291extern int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb); 292extern int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb);
@@ -463,16 +464,20 @@ extern int usb_find_interface_driver(struct usb_device *dev,
463#define usb_endpoint_out(ep_dir) (!((ep_dir) & USB_DIR_IN)) 464#define usb_endpoint_out(ep_dir) (!((ep_dir) & USB_DIR_IN))
464 465
465#ifdef CONFIG_PM 466#ifdef CONFIG_PM
466extern void usb_hcd_resume_root_hub(struct usb_hcd *hcd);
467extern void usb_root_hub_lost_power(struct usb_device *rhdev); 467extern void usb_root_hub_lost_power(struct usb_device *rhdev);
468extern int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg); 468extern int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg);
469extern int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg); 469extern int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg);
470#endif /* CONFIG_PM */
471
472#ifdef CONFIG_USB_SUSPEND
473extern void usb_hcd_resume_root_hub(struct usb_hcd *hcd);
470#else 474#else
471static inline void usb_hcd_resume_root_hub(struct usb_hcd *hcd) 475static inline void usb_hcd_resume_root_hub(struct usb_hcd *hcd)
472{ 476{
473 return; 477 return;
474} 478}
475#endif /* CONFIG_PM */ 479#endif /* CONFIG_USB_SUSPEND */
480
476 481
477/* 482/*
478 * USB device fs stuff 483 * USB device fs stuff
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index 20ecb4cec8de..0940ccd6f4f4 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -22,6 +22,7 @@
22#include <linux/kthread.h> 22#include <linux/kthread.h>
23#include <linux/mutex.h> 23#include <linux/mutex.h>
24#include <linux/freezer.h> 24#include <linux/freezer.h>
25#include <linux/pm_runtime.h>
25 26
26#include <asm/uaccess.h> 27#include <asm/uaccess.h>
27#include <asm/byteorder.h> 28#include <asm/byteorder.h>
@@ -71,7 +72,6 @@ struct usb_hub {
71 72
72 unsigned mA_per_port; /* current for each child */ 73 unsigned mA_per_port; /* current for each child */
73 74
74 unsigned init_done:1;
75 unsigned limited_power:1; 75 unsigned limited_power:1;
76 unsigned quiescing:1; 76 unsigned quiescing:1;
77 unsigned disconnected:1; 77 unsigned disconnected:1;
@@ -820,7 +820,6 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
820 } 820 }
821 init3: 821 init3:
822 hub->quiescing = 0; 822 hub->quiescing = 0;
823 hub->init_done = 1;
824 823
825 status = usb_submit_urb(hub->urb, GFP_NOIO); 824 status = usb_submit_urb(hub->urb, GFP_NOIO);
826 if (status < 0) 825 if (status < 0)
@@ -861,11 +860,6 @@ static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
861 int i; 860 int i;
862 861
863 cancel_delayed_work_sync(&hub->init_work); 862 cancel_delayed_work_sync(&hub->init_work);
864 if (!hub->init_done) {
865 hub->init_done = 1;
866 usb_autopm_put_interface_no_suspend(
867 to_usb_interface(hub->intfdev));
868 }
869 863
870 /* khubd and related activity won't re-trigger */ 864 /* khubd and related activity won't re-trigger */
871 hub->quiescing = 1; 865 hub->quiescing = 1;
@@ -1224,6 +1218,9 @@ static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1224 desc = intf->cur_altsetting; 1218 desc = intf->cur_altsetting;
1225 hdev = interface_to_usbdev(intf); 1219 hdev = interface_to_usbdev(intf);
1226 1220
1221 /* Hubs have proper suspend/resume support */
1222 usb_enable_autosuspend(hdev);
1223
1227 if (hdev->level == MAX_TOPO_LEVEL) { 1224 if (hdev->level == MAX_TOPO_LEVEL) {
1228 dev_err(&intf->dev, 1225 dev_err(&intf->dev,
1229 "Unsupported bus topology: hub nested too deep\n"); 1226 "Unsupported bus topology: hub nested too deep\n");
@@ -1402,10 +1399,8 @@ static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1402 if (udev->children[i]) 1399 if (udev->children[i])
1403 recursively_mark_NOTATTACHED(udev->children[i]); 1400 recursively_mark_NOTATTACHED(udev->children[i]);
1404 } 1401 }
1405 if (udev->state == USB_STATE_SUSPENDED) { 1402 if (udev->state == USB_STATE_SUSPENDED)
1406 udev->discon_suspended = 1;
1407 udev->active_duration -= jiffies; 1403 udev->active_duration -= jiffies;
1408 }
1409 udev->state = USB_STATE_NOTATTACHED; 1404 udev->state = USB_STATE_NOTATTACHED;
1410} 1405}
1411 1406
@@ -1448,11 +1443,11 @@ void usb_set_device_state(struct usb_device *udev,
1448 || new_state == USB_STATE_SUSPENDED) 1443 || new_state == USB_STATE_SUSPENDED)
1449 ; /* No change to wakeup settings */ 1444 ; /* No change to wakeup settings */
1450 else if (new_state == USB_STATE_CONFIGURED) 1445 else if (new_state == USB_STATE_CONFIGURED)
1451 device_init_wakeup(&udev->dev, 1446 device_set_wakeup_capable(&udev->dev,
1452 (udev->actconfig->desc.bmAttributes 1447 (udev->actconfig->desc.bmAttributes
1453 & USB_CONFIG_ATT_WAKEUP)); 1448 & USB_CONFIG_ATT_WAKEUP));
1454 else 1449 else
1455 device_init_wakeup(&udev->dev, 0); 1450 device_set_wakeup_capable(&udev->dev, 0);
1456 } 1451 }
1457 if (udev->state == USB_STATE_SUSPENDED && 1452 if (udev->state == USB_STATE_SUSPENDED &&
1458 new_state != USB_STATE_SUSPENDED) 1453 new_state != USB_STATE_SUSPENDED)
@@ -1529,31 +1524,15 @@ static void update_address(struct usb_device *udev, int devnum)
1529 udev->devnum = devnum; 1524 udev->devnum = devnum;
1530} 1525}
1531 1526
1532#ifdef CONFIG_USB_SUSPEND 1527static void hub_free_dev(struct usb_device *udev)
1533
1534static void usb_stop_pm(struct usb_device *udev)
1535{ 1528{
1536 /* Synchronize with the ksuspend thread to prevent any more 1529 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1537 * autosuspend requests from being submitted, and decrement
1538 * the parent's count of unsuspended children.
1539 */
1540 usb_pm_lock(udev);
1541 if (udev->parent && !udev->discon_suspended)
1542 usb_autosuspend_device(udev->parent);
1543 usb_pm_unlock(udev);
1544 1530
1545 /* Stop any autosuspend or autoresume requests already submitted */ 1531 /* Root hubs aren't real devices, so don't free HCD resources */
1546 cancel_delayed_work_sync(&udev->autosuspend); 1532 if (hcd->driver->free_dev && udev->parent)
1547 cancel_work_sync(&udev->autoresume); 1533 hcd->driver->free_dev(hcd, udev);
1548} 1534}
1549 1535
1550#else
1551
1552static inline void usb_stop_pm(struct usb_device *udev)
1553{ }
1554
1555#endif
1556
1557/** 1536/**
1558 * usb_disconnect - disconnect a device (usbcore-internal) 1537 * usb_disconnect - disconnect a device (usbcore-internal)
1559 * @pdev: pointer to device being disconnected 1538 * @pdev: pointer to device being disconnected
@@ -1622,7 +1601,7 @@ void usb_disconnect(struct usb_device **pdev)
1622 *pdev = NULL; 1601 *pdev = NULL;
1623 spin_unlock_irq(&device_state_lock); 1602 spin_unlock_irq(&device_state_lock);
1624 1603
1625 usb_stop_pm(udev); 1604 hub_free_dev(udev);
1626 1605
1627 put_device(&udev->dev); 1606 put_device(&udev->dev);
1628} 1607}
@@ -1799,9 +1778,18 @@ int usb_new_device(struct usb_device *udev)
1799{ 1778{
1800 int err; 1779 int err;
1801 1780
1802 /* Increment the parent's count of unsuspended children */ 1781 if (udev->parent) {
1803 if (udev->parent) 1782 /* Initialize non-root-hub device wakeup to disabled;
1804 usb_autoresume_device(udev->parent); 1783 * device (un)configuration controls wakeup capable
1784 * sysfs power/wakeup controls wakeup enabled/disabled
1785 */
1786 device_init_wakeup(&udev->dev, 0);
1787 device_set_wakeup_enable(&udev->dev, 1);
1788 }
1789
1790 /* Tell the runtime-PM framework the device is active */
1791 pm_runtime_set_active(&udev->dev);
1792 pm_runtime_enable(&udev->dev);
1805 1793
1806 usb_detect_quirks(udev); 1794 usb_detect_quirks(udev);
1807 err = usb_enumerate_device(udev); /* Read descriptors */ 1795 err = usb_enumerate_device(udev); /* Read descriptors */
@@ -1833,7 +1821,8 @@ int usb_new_device(struct usb_device *udev)
1833 1821
1834fail: 1822fail:
1835 usb_set_device_state(udev, USB_STATE_NOTATTACHED); 1823 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1836 usb_stop_pm(udev); 1824 pm_runtime_disable(&udev->dev);
1825 pm_runtime_set_suspended(&udev->dev);
1837 return err; 1826 return err;
1838} 1827}
1839 1828
@@ -1982,7 +1971,7 @@ static int hub_port_wait_reset(struct usb_hub *hub, int port1,
1982 if (!(portstatus & USB_PORT_STAT_RESET) && 1971 if (!(portstatus & USB_PORT_STAT_RESET) &&
1983 (portstatus & USB_PORT_STAT_ENABLE)) { 1972 (portstatus & USB_PORT_STAT_ENABLE)) {
1984 if (hub_is_wusb(hub)) 1973 if (hub_is_wusb(hub))
1985 udev->speed = USB_SPEED_VARIABLE; 1974 udev->speed = USB_SPEED_WIRELESS;
1986 else if (portstatus & USB_PORT_STAT_HIGH_SPEED) 1975 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
1987 udev->speed = USB_SPEED_HIGH; 1976 udev->speed = USB_SPEED_HIGH;
1988 else if (portstatus & USB_PORT_STAT_LOW_SPEED) 1977 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
@@ -2008,7 +1997,9 @@ static int hub_port_reset(struct usb_hub *hub, int port1,
2008 struct usb_device *udev, unsigned int delay) 1997 struct usb_device *udev, unsigned int delay)
2009{ 1998{
2010 int i, status; 1999 int i, status;
2000 struct usb_hcd *hcd;
2011 2001
2002 hcd = bus_to_hcd(udev->bus);
2012 /* Block EHCI CF initialization during the port reset. 2003 /* Block EHCI CF initialization during the port reset.
2013 * Some companion controllers don't like it when they mix. 2004 * Some companion controllers don't like it when they mix.
2014 */ 2005 */
@@ -2036,6 +2027,14 @@ static int hub_port_reset(struct usb_hub *hub, int port1,
2036 /* TRSTRCY = 10 ms; plus some extra */ 2027 /* TRSTRCY = 10 ms; plus some extra */
2037 msleep(10 + 40); 2028 msleep(10 + 40);
2038 update_address(udev, 0); 2029 update_address(udev, 0);
2030 if (hcd->driver->reset_device) {
2031 status = hcd->driver->reset_device(hcd, udev);
2032 if (status < 0) {
2033 dev_err(&udev->dev, "Cannot reset "
2034 "HCD device state\n");
2035 break;
2036 }
2037 }
2039 /* FALL THROUGH */ 2038 /* FALL THROUGH */
2040 case -ENOTCONN: 2039 case -ENOTCONN:
2041 case -ENODEV: 2040 case -ENODEV:
@@ -2381,14 +2380,17 @@ int usb_port_resume(struct usb_device *udev, pm_message_t msg)
2381} 2380}
2382 2381
2383/* caller has locked udev */ 2382/* caller has locked udev */
2384static int remote_wakeup(struct usb_device *udev) 2383int usb_remote_wakeup(struct usb_device *udev)
2385{ 2384{
2386 int status = 0; 2385 int status = 0;
2387 2386
2388 if (udev->state == USB_STATE_SUSPENDED) { 2387 if (udev->state == USB_STATE_SUSPENDED) {
2389 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); 2388 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
2390 usb_mark_last_busy(udev); 2389 status = usb_autoresume_device(udev);
2391 status = usb_external_resume_device(udev, PMSG_REMOTE_RESUME); 2390 if (status == 0) {
2391 /* Let the drivers do their thing, then... */
2392 usb_autosuspend_device(udev);
2393 }
2392 } 2394 }
2393 return status; 2395 return status;
2394} 2396}
@@ -2425,11 +2427,6 @@ int usb_port_resume(struct usb_device *udev, pm_message_t msg)
2425 return status; 2427 return status;
2426} 2428}
2427 2429
2428static inline int remote_wakeup(struct usb_device *udev)
2429{
2430 return 0;
2431}
2432
2433#endif 2430#endif
2434 2431
2435static int hub_suspend(struct usb_interface *intf, pm_message_t msg) 2432static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
@@ -2496,11 +2493,6 @@ EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
2496 2493
2497#else /* CONFIG_PM */ 2494#else /* CONFIG_PM */
2498 2495
2499static inline int remote_wakeup(struct usb_device *udev)
2500{
2501 return 0;
2502}
2503
2504#define hub_suspend NULL 2496#define hub_suspend NULL
2505#define hub_resume NULL 2497#define hub_resume NULL
2506#define hub_reset_resume NULL 2498#define hub_reset_resume NULL
@@ -2645,14 +2637,7 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2645 2637
2646 mutex_lock(&usb_address0_mutex); 2638 mutex_lock(&usb_address0_mutex);
2647 2639
2648 if ((hcd->driver->flags & HCD_USB3) && udev->config) { 2640 if (!udev->config && oldspeed == USB_SPEED_SUPER) {
2649 /* FIXME this will need special handling by the xHCI driver. */
2650 dev_dbg(&udev->dev,
2651 "xHCI reset of configured device "
2652 "not supported yet.\n");
2653 retval = -EINVAL;
2654 goto fail;
2655 } else if (!udev->config && oldspeed == USB_SPEED_SUPER) {
2656 /* Don't reset USB 3.0 devices during an initial setup */ 2641 /* Don't reset USB 3.0 devices during an initial setup */
2657 usb_set_device_state(udev, USB_STATE_DEFAULT); 2642 usb_set_device_state(udev, USB_STATE_DEFAULT);
2658 } else { 2643 } else {
@@ -2678,7 +2663,7 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2678 */ 2663 */
2679 switch (udev->speed) { 2664 switch (udev->speed) {
2680 case USB_SPEED_SUPER: 2665 case USB_SPEED_SUPER:
2681 case USB_SPEED_VARIABLE: /* fixed at 512 */ 2666 case USB_SPEED_WIRELESS: /* fixed at 512 */
2682 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512); 2667 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
2683 break; 2668 break;
2684 case USB_SPEED_HIGH: /* fixed at 64 */ 2669 case USB_SPEED_HIGH: /* fixed at 64 */
@@ -2706,7 +2691,7 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2706 case USB_SPEED_SUPER: 2691 case USB_SPEED_SUPER:
2707 speed = "super"; 2692 speed = "super";
2708 break; 2693 break;
2709 case USB_SPEED_VARIABLE: 2694 case USB_SPEED_WIRELESS:
2710 speed = "variable"; 2695 speed = "variable";
2711 type = "Wireless "; 2696 type = "Wireless ";
2712 break; 2697 break;
@@ -3006,7 +2991,7 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
3006 /* For a suspended device, treat this as a 2991 /* For a suspended device, treat this as a
3007 * remote wakeup event. 2992 * remote wakeup event.
3008 */ 2993 */
3009 status = remote_wakeup(udev); 2994 status = usb_remote_wakeup(udev);
3010#endif 2995#endif
3011 2996
3012 } else { 2997 } else {
@@ -3192,6 +3177,7 @@ loop_disable:
3192loop: 3177loop:
3193 usb_ep0_reinit(udev); 3178 usb_ep0_reinit(udev);
3194 release_address(udev); 3179 release_address(udev);
3180 hub_free_dev(udev);
3195 usb_put_dev(udev); 3181 usb_put_dev(udev);
3196 if ((status == -ENOTCONN) || (status == -ENOTSUPP)) 3182 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
3197 break; 3183 break;
@@ -3259,7 +3245,7 @@ static void hub_events(void)
3259 * disconnected while waiting for the lock to succeed. */ 3245 * disconnected while waiting for the lock to succeed. */
3260 usb_lock_device(hdev); 3246 usb_lock_device(hdev);
3261 if (unlikely(hub->disconnected)) 3247 if (unlikely(hub->disconnected))
3262 goto loop2; 3248 goto loop_disconnected;
3263 3249
3264 /* If the hub has died, clean up after it */ 3250 /* If the hub has died, clean up after it */
3265 if (hdev->state == USB_STATE_NOTATTACHED) { 3251 if (hdev->state == USB_STATE_NOTATTACHED) {
@@ -3352,7 +3338,7 @@ static void hub_events(void)
3352 msleep(10); 3338 msleep(10);
3353 3339
3354 usb_lock_device(udev); 3340 usb_lock_device(udev);
3355 ret = remote_wakeup(hdev-> 3341 ret = usb_remote_wakeup(hdev->
3356 children[i-1]); 3342 children[i-1]);
3357 usb_unlock_device(udev); 3343 usb_unlock_device(udev);
3358 if (ret < 0) 3344 if (ret < 0)
@@ -3419,7 +3405,7 @@ static void hub_events(void)
3419 * kick_khubd() and allow autosuspend. 3405 * kick_khubd() and allow autosuspend.
3420 */ 3406 */
3421 usb_autopm_put_interface(intf); 3407 usb_autopm_put_interface(intf);
3422 loop2: 3408 loop_disconnected:
3423 usb_unlock_device(hdev); 3409 usb_unlock_device(hdev);
3424 kref_put(&hub->kref, hub_release); 3410 kref_put(&hub->kref, hub_release);
3425 3411
@@ -3446,7 +3432,7 @@ static int hub_thread(void *__unused)
3446 return 0; 3432 return 0;
3447} 3433}
3448 3434
3449static struct usb_device_id hub_id_table [] = { 3435static const struct usb_device_id hub_id_table[] = {
3450 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS, 3436 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
3451 .bDeviceClass = USB_CLASS_HUB}, 3437 .bDeviceClass = USB_CLASS_HUB},
3452 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, 3438 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index df73574a9cc9..cd220277c6c3 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -1316,7 +1316,7 @@ int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1316 1316
1317 alt = usb_altnum_to_altsetting(iface, alternate); 1317 alt = usb_altnum_to_altsetting(iface, alternate);
1318 if (!alt) { 1318 if (!alt) {
1319 dev_warn(&dev->dev, "selecting invalid altsetting %d", 1319 dev_warn(&dev->dev, "selecting invalid altsetting %d\n",
1320 alternate); 1320 alternate);
1321 return -EINVAL; 1321 return -EINVAL;
1322 } 1322 }
@@ -1471,7 +1471,7 @@ int usb_reset_configuration(struct usb_device *dev)
1471 /* If not, reinstate the old alternate settings */ 1471 /* If not, reinstate the old alternate settings */
1472 if (retval < 0) { 1472 if (retval < 0) {
1473reset_old_alts: 1473reset_old_alts:
1474 for (; i >= 0; i--) { 1474 for (i--; i >= 0; i--) {
1475 struct usb_interface *intf = config->interface[i]; 1475 struct usb_interface *intf = config->interface[i];
1476 struct usb_host_interface *alt; 1476 struct usb_host_interface *alt;
1477 1477
@@ -1843,7 +1843,6 @@ free_interfaces:
1843 intf->dev.dma_mask = dev->dev.dma_mask; 1843 intf->dev.dma_mask = dev->dev.dma_mask;
1844 INIT_WORK(&intf->reset_ws, __usb_queue_reset_device); 1844 INIT_WORK(&intf->reset_ws, __usb_queue_reset_device);
1845 device_initialize(&intf->dev); 1845 device_initialize(&intf->dev);
1846 mark_quiesced(intf);
1847 dev_set_name(&intf->dev, "%d-%s:%d.%d", 1846 dev_set_name(&intf->dev, "%d-%s:%d.%d",
1848 dev->bus->busnum, dev->devpath, 1847 dev->bus->busnum, dev->devpath,
1849 configuration, alt->desc.bInterfaceNumber); 1848 configuration, alt->desc.bInterfaceNumber);
diff --git a/drivers/usb/core/quirks.c b/drivers/usb/core/quirks.c
index ab93918d9207..f073c5cb4e7b 100644
--- a/drivers/usb/core/quirks.c
+++ b/drivers/usb/core/quirks.c
@@ -103,10 +103,19 @@ void usb_detect_quirks(struct usb_device *udev)
103 dev_dbg(&udev->dev, "USB quirks for this device: %x\n", 103 dev_dbg(&udev->dev, "USB quirks for this device: %x\n",
104 udev->quirks); 104 udev->quirks);
105 105
106 /* By default, disable autosuspend for all non-hubs */
107#ifdef CONFIG_USB_SUSPEND 106#ifdef CONFIG_USB_SUSPEND
108 if (udev->descriptor.bDeviceClass != USB_CLASS_HUB) 107
109 udev->autosuspend_disabled = 1; 108 /* By default, disable autosuspend for all devices. The hub driver
109 * will enable it for hubs.
110 */
111 usb_disable_autosuspend(udev);
112
113 /* Autosuspend can also be disabled if the initial autosuspend_delay
114 * is negative.
115 */
116 if (udev->autosuspend_delay < 0)
117 usb_autoresume_device(udev);
118
110#endif 119#endif
111 120
112 /* For the present, all devices default to USB-PERSIST enabled */ 121 /* For the present, all devices default to USB-PERSIST enabled */
@@ -120,6 +129,7 @@ void usb_detect_quirks(struct usb_device *udev)
120 * for all devices. It will affect things like hub resets 129 * for all devices. It will affect things like hub resets
121 * and EMF-related port disables. 130 * and EMF-related port disables.
122 */ 131 */
123 udev->persist_enabled = 1; 132 if (!(udev->quirks & USB_QUIRK_RESET_MORPHS))
133 udev->persist_enabled = 1;
124#endif /* CONFIG_PM */ 134#endif /* CONFIG_PM */
125} 135}
diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c
index 5f3908f6e2dc..43c002e3a9aa 100644
--- a/drivers/usb/core/sysfs.c
+++ b/drivers/usb/core/sysfs.c
@@ -115,7 +115,7 @@ show_speed(struct device *dev, struct device_attribute *attr, char *buf)
115 case USB_SPEED_HIGH: 115 case USB_SPEED_HIGH:
116 speed = "480"; 116 speed = "480";
117 break; 117 break;
118 case USB_SPEED_VARIABLE: 118 case USB_SPEED_WIRELESS:
119 speed = "480"; 119 speed = "480";
120 break; 120 break;
121 case USB_SPEED_SUPER: 121 case USB_SPEED_SUPER:
@@ -191,6 +191,36 @@ show_quirks(struct device *dev, struct device_attribute *attr, char *buf)
191static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL); 191static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL);
192 192
193static ssize_t 193static ssize_t
194show_avoid_reset_quirk(struct device *dev, struct device_attribute *attr, char *buf)
195{
196 struct usb_device *udev;
197
198 udev = to_usb_device(dev);
199 return sprintf(buf, "%d\n", !!(udev->quirks & USB_QUIRK_RESET_MORPHS));
200}
201
202static ssize_t
203set_avoid_reset_quirk(struct device *dev, struct device_attribute *attr,
204 const char *buf, size_t count)
205{
206 struct usb_device *udev = to_usb_device(dev);
207 int config;
208
209 if (sscanf(buf, "%d", &config) != 1 || config < 0 || config > 1)
210 return -EINVAL;
211 usb_lock_device(udev);
212 if (config)
213 udev->quirks |= USB_QUIRK_RESET_MORPHS;
214 else
215 udev->quirks &= ~USB_QUIRK_RESET_MORPHS;
216 usb_unlock_device(udev);
217 return count;
218}
219
220static DEVICE_ATTR(avoid_reset_quirk, S_IRUGO | S_IWUSR,
221 show_avoid_reset_quirk, set_avoid_reset_quirk);
222
223static ssize_t
194show_urbnum(struct device *dev, struct device_attribute *attr, char *buf) 224show_urbnum(struct device *dev, struct device_attribute *attr, char *buf)
195{ 225{
196 struct usb_device *udev; 226 struct usb_device *udev;
@@ -226,9 +256,10 @@ set_persist(struct device *dev, struct device_attribute *attr,
226 256
227 if (sscanf(buf, "%d", &value) != 1) 257 if (sscanf(buf, "%d", &value) != 1)
228 return -EINVAL; 258 return -EINVAL;
229 usb_pm_lock(udev); 259
260 usb_lock_device(udev);
230 udev->persist_enabled = !!value; 261 udev->persist_enabled = !!value;
231 usb_pm_unlock(udev); 262 usb_unlock_device(udev);
232 return count; 263 return count;
233} 264}
234 265
@@ -315,20 +346,34 @@ set_autosuspend(struct device *dev, struct device_attribute *attr,
315 const char *buf, size_t count) 346 const char *buf, size_t count)
316{ 347{
317 struct usb_device *udev = to_usb_device(dev); 348 struct usb_device *udev = to_usb_device(dev);
318 int value; 349 int value, old_delay;
350 int rc;
319 351
320 if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ || 352 if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ ||
321 value <= - INT_MAX/HZ) 353 value <= - INT_MAX/HZ)
322 return -EINVAL; 354 return -EINVAL;
323 value *= HZ; 355 value *= HZ;
324 356
357 usb_lock_device(udev);
358 old_delay = udev->autosuspend_delay;
325 udev->autosuspend_delay = value; 359 udev->autosuspend_delay = value;
326 if (value >= 0) 360
327 usb_try_autosuspend_device(udev); 361 if (old_delay < 0) { /* Autosuspend wasn't allowed */
328 else { 362 if (value >= 0)
329 if (usb_autoresume_device(udev) == 0)
330 usb_autosuspend_device(udev); 363 usb_autosuspend_device(udev);
364 } else { /* Autosuspend was allowed */
365 if (value < 0) {
366 rc = usb_autoresume_device(udev);
367 if (rc < 0) {
368 count = rc;
369 udev->autosuspend_delay = old_delay;
370 }
371 } else {
372 usb_try_autosuspend_device(udev);
373 }
331 } 374 }
375
376 usb_unlock_device(udev);
332 return count; 377 return count;
333} 378}
334 379
@@ -356,34 +401,25 @@ set_level(struct device *dev, struct device_attribute *attr,
356 struct usb_device *udev = to_usb_device(dev); 401 struct usb_device *udev = to_usb_device(dev);
357 int len = count; 402 int len = count;
358 char *cp; 403 char *cp;
359 int rc = 0; 404 int rc;
360 int old_autosuspend_disabled;
361 405
362 cp = memchr(buf, '\n', count); 406 cp = memchr(buf, '\n', count);
363 if (cp) 407 if (cp)
364 len = cp - buf; 408 len = cp - buf;
365 409
366 usb_lock_device(udev); 410 usb_lock_device(udev);
367 old_autosuspend_disabled = udev->autosuspend_disabled;
368 411
369 /* Setting the flags without calling usb_pm_lock is a subject to
370 * races, but who cares...
371 */
372 if (len == sizeof on_string - 1 && 412 if (len == sizeof on_string - 1 &&
373 strncmp(buf, on_string, len) == 0) { 413 strncmp(buf, on_string, len) == 0)
374 udev->autosuspend_disabled = 1; 414 rc = usb_disable_autosuspend(udev);
375 rc = usb_external_resume_device(udev, PMSG_USER_RESUME);
376 415
377 } else if (len == sizeof auto_string - 1 && 416 else if (len == sizeof auto_string - 1 &&
378 strncmp(buf, auto_string, len) == 0) { 417 strncmp(buf, auto_string, len) == 0)
379 udev->autosuspend_disabled = 0; 418 rc = usb_enable_autosuspend(udev);
380 rc = usb_external_resume_device(udev, PMSG_USER_RESUME);
381 419
382 } else 420 else
383 rc = -EINVAL; 421 rc = -EINVAL;
384 422
385 if (rc)
386 udev->autosuspend_disabled = old_autosuspend_disabled;
387 usb_unlock_device(udev); 423 usb_unlock_device(udev);
388 return (rc < 0 ? rc : count); 424 return (rc < 0 ? rc : count);
389} 425}
@@ -558,6 +594,7 @@ static struct attribute *dev_attrs[] = {
558 &dev_attr_version.attr, 594 &dev_attr_version.attr,
559 &dev_attr_maxchild.attr, 595 &dev_attr_maxchild.attr,
560 &dev_attr_quirks.attr, 596 &dev_attr_quirks.attr,
597 &dev_attr_avoid_reset_quirk.attr,
561 &dev_attr_authorized.attr, 598 &dev_attr_authorized.attr,
562 &dev_attr_remove.attr, 599 &dev_attr_remove.attr,
563 NULL, 600 NULL,
diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c
index e7cae1334693..27080561a1c2 100644
--- a/drivers/usb/core/urb.c
+++ b/drivers/usb/core/urb.c
@@ -387,6 +387,13 @@ int usb_submit_urb(struct urb *urb, gfp_t mem_flags)
387 { 387 {
388 unsigned int orig_flags = urb->transfer_flags; 388 unsigned int orig_flags = urb->transfer_flags;
389 unsigned int allowed; 389 unsigned int allowed;
390 static int pipetypes[4] = {
391 PIPE_CONTROL, PIPE_ISOCHRONOUS, PIPE_BULK, PIPE_INTERRUPT
392 };
393
394 /* Check that the pipe's type matches the endpoint's type */
395 if (usb_pipetype(urb->pipe) != pipetypes[xfertype])
396 return -EPIPE; /* The most suitable error code :-) */
390 397
391 /* enforce simple/standard policy */ 398 /* enforce simple/standard policy */
392 allowed = (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP | 399 allowed = (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP |
@@ -430,7 +437,7 @@ int usb_submit_urb(struct urb *urb, gfp_t mem_flags)
430 case USB_ENDPOINT_XFER_INT: 437 case USB_ENDPOINT_XFER_INT:
431 /* too small? */ 438 /* too small? */
432 switch (dev->speed) { 439 switch (dev->speed) {
433 case USB_SPEED_VARIABLE: 440 case USB_SPEED_WIRELESS:
434 if (urb->interval < 6) 441 if (urb->interval < 6)
435 return -EINVAL; 442 return -EINVAL;
436 break; 443 break;
@@ -446,7 +453,7 @@ int usb_submit_urb(struct urb *urb, gfp_t mem_flags)
446 if (urb->interval > (1 << 15)) 453 if (urb->interval > (1 << 15))
447 return -EINVAL; 454 return -EINVAL;
448 max = 1 << 15; 455 max = 1 << 15;
449 case USB_SPEED_VARIABLE: 456 case USB_SPEED_WIRELESS:
450 if (urb->interval > 16) 457 if (urb->interval > 16)
451 return -EINVAL; 458 return -EINVAL;
452 break; 459 break;
@@ -473,7 +480,7 @@ int usb_submit_urb(struct urb *urb, gfp_t mem_flags)
473 default: 480 default:
474 return -EINVAL; 481 return -EINVAL;
475 } 482 }
476 if (dev->speed != USB_SPEED_VARIABLE) { 483 if (dev->speed != USB_SPEED_WIRELESS) {
477 /* Round down to a power of 2, no more than max */ 484 /* Round down to a power of 2, no more than max */
478 urb->interval = min(max, 1 << ilog2(urb->interval)); 485 urb->interval = min(max, 1 << ilog2(urb->interval));
479 } 486 }
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index 0daff0d968ba..1297e9b16a51 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -49,9 +49,6 @@ const char *usbcore_name = "usbcore";
49 49
50static int nousb; /* Disable USB when built into kernel image */ 50static int nousb; /* Disable USB when built into kernel image */
51 51
52/* Workqueue for autosuspend and for remote wakeup of root hubs */
53struct workqueue_struct *ksuspend_usb_wq;
54
55#ifdef CONFIG_USB_SUSPEND 52#ifdef CONFIG_USB_SUSPEND
56static int usb_autosuspend_delay = 2; /* Default delay value, 53static int usb_autosuspend_delay = 2; /* Default delay value,
57 * in seconds */ 54 * in seconds */
@@ -228,9 +225,6 @@ static void usb_release_dev(struct device *dev)
228 hcd = bus_to_hcd(udev->bus); 225 hcd = bus_to_hcd(udev->bus);
229 226
230 usb_destroy_configuration(udev); 227 usb_destroy_configuration(udev);
231 /* Root hubs aren't real devices, so don't free HCD resources */
232 if (hcd->driver->free_dev && udev->parent)
233 hcd->driver->free_dev(hcd, udev);
234 usb_put_hcd(hcd); 228 usb_put_hcd(hcd);
235 kfree(udev->product); 229 kfree(udev->product);
236 kfree(udev->manufacturer); 230 kfree(udev->manufacturer);
@@ -264,23 +258,6 @@ static int usb_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
264 258
265#ifdef CONFIG_PM 259#ifdef CONFIG_PM
266 260
267static int ksuspend_usb_init(void)
268{
269 /* This workqueue is supposed to be both freezable and
270 * singlethreaded. Its job doesn't justify running on more
271 * than one CPU.
272 */
273 ksuspend_usb_wq = create_freezeable_workqueue("ksuspend_usbd");
274 if (!ksuspend_usb_wq)
275 return -ENOMEM;
276 return 0;
277}
278
279static void ksuspend_usb_cleanup(void)
280{
281 destroy_workqueue(ksuspend_usb_wq);
282}
283
284/* USB device Power-Management thunks. 261/* USB device Power-Management thunks.
285 * There's no need to distinguish here between quiescing a USB device 262 * There's no need to distinguish here between quiescing a USB device
286 * and powering it down; the generic_suspend() routine takes care of 263 * and powering it down; the generic_suspend() routine takes care of
@@ -296,7 +273,7 @@ static int usb_dev_prepare(struct device *dev)
296static void usb_dev_complete(struct device *dev) 273static void usb_dev_complete(struct device *dev)
297{ 274{
298 /* Currently used only for rebinding interfaces */ 275 /* Currently used only for rebinding interfaces */
299 usb_resume(dev, PMSG_RESUME); /* Message event is meaningless */ 276 usb_resume(dev, PMSG_ON); /* FIXME: change to PMSG_COMPLETE */
300} 277}
301 278
302static int usb_dev_suspend(struct device *dev) 279static int usb_dev_suspend(struct device *dev)
@@ -342,9 +319,7 @@ static const struct dev_pm_ops usb_device_pm_ops = {
342 319
343#else 320#else
344 321
345#define ksuspend_usb_init() 0 322#define usb_device_pm_ops (*(struct dev_pm_ops *) NULL)
346#define ksuspend_usb_cleanup() do {} while (0)
347#define usb_device_pm_ops (*(struct dev_pm_ops *)0)
348 323
349#endif /* CONFIG_PM */ 324#endif /* CONFIG_PM */
350 325
@@ -472,9 +447,6 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent,
472 INIT_LIST_HEAD(&dev->filelist); 447 INIT_LIST_HEAD(&dev->filelist);
473 448
474#ifdef CONFIG_PM 449#ifdef CONFIG_PM
475 mutex_init(&dev->pm_mutex);
476 INIT_DELAYED_WORK(&dev->autosuspend, usb_autosuspend_work);
477 INIT_WORK(&dev->autoresume, usb_autoresume_work);
478 dev->autosuspend_delay = usb_autosuspend_delay * HZ; 450 dev->autosuspend_delay = usb_autosuspend_delay * HZ;
479 dev->connect_time = jiffies; 451 dev->connect_time = jiffies;
480 dev->active_duration = -jiffies; 452 dev->active_duration = -jiffies;
@@ -1117,9 +1089,6 @@ static int __init usb_init(void)
1117 if (retval) 1089 if (retval)
1118 goto out; 1090 goto out;
1119 1091
1120 retval = ksuspend_usb_init();
1121 if (retval)
1122 goto out;
1123 retval = bus_register(&usb_bus_type); 1092 retval = bus_register(&usb_bus_type);
1124 if (retval) 1093 if (retval)
1125 goto bus_register_failed; 1094 goto bus_register_failed;
@@ -1159,7 +1128,7 @@ major_init_failed:
1159bus_notifier_failed: 1128bus_notifier_failed:
1160 bus_unregister(&usb_bus_type); 1129 bus_unregister(&usb_bus_type);
1161bus_register_failed: 1130bus_register_failed:
1162 ksuspend_usb_cleanup(); 1131 usb_debugfs_cleanup();
1163out: 1132out:
1164 return retval; 1133 return retval;
1165} 1134}
@@ -1181,7 +1150,6 @@ static void __exit usb_exit(void)
1181 usb_hub_cleanup(); 1150 usb_hub_cleanup();
1182 bus_unregister_notifier(&usb_bus_type, &usb_bus_nb); 1151 bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
1183 bus_unregister(&usb_bus_type); 1152 bus_unregister(&usb_bus_type);
1184 ksuspend_usb_cleanup();
1185 usb_debugfs_cleanup(); 1153 usb_debugfs_cleanup();
1186} 1154}
1187 1155
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h
index 4c36c7f512a0..cd882203ad34 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -55,24 +55,8 @@ extern void usb_major_cleanup(void);
55extern int usb_suspend(struct device *dev, pm_message_t msg); 55extern int usb_suspend(struct device *dev, pm_message_t msg);
56extern int usb_resume(struct device *dev, pm_message_t msg); 56extern int usb_resume(struct device *dev, pm_message_t msg);
57 57
58extern void usb_autosuspend_work(struct work_struct *work);
59extern void usb_autoresume_work(struct work_struct *work);
60extern int usb_port_suspend(struct usb_device *dev, pm_message_t msg); 58extern int usb_port_suspend(struct usb_device *dev, pm_message_t msg);
61extern int usb_port_resume(struct usb_device *dev, pm_message_t msg); 59extern int usb_port_resume(struct usb_device *dev, pm_message_t msg);
62extern int usb_external_suspend_device(struct usb_device *udev,
63 pm_message_t msg);
64extern int usb_external_resume_device(struct usb_device *udev,
65 pm_message_t msg);
66
67static inline void usb_pm_lock(struct usb_device *udev)
68{
69 mutex_lock_nested(&udev->pm_mutex, udev->level);
70}
71
72static inline void usb_pm_unlock(struct usb_device *udev)
73{
74 mutex_unlock(&udev->pm_mutex);
75}
76 60
77#else 61#else
78 62
@@ -86,9 +70,6 @@ static inline int usb_port_resume(struct usb_device *udev, pm_message_t msg)
86 return 0; 70 return 0;
87} 71}
88 72
89static inline void usb_pm_lock(struct usb_device *udev) {}
90static inline void usb_pm_unlock(struct usb_device *udev) {}
91
92#endif 73#endif
93 74
94#ifdef CONFIG_USB_SUSPEND 75#ifdef CONFIG_USB_SUSPEND
@@ -96,6 +77,7 @@ static inline void usb_pm_unlock(struct usb_device *udev) {}
96extern void usb_autosuspend_device(struct usb_device *udev); 77extern void usb_autosuspend_device(struct usb_device *udev);
97extern void usb_try_autosuspend_device(struct usb_device *udev); 78extern void usb_try_autosuspend_device(struct usb_device *udev);
98extern int usb_autoresume_device(struct usb_device *udev); 79extern int usb_autoresume_device(struct usb_device *udev);
80extern int usb_remote_wakeup(struct usb_device *dev);
99 81
100#else 82#else
101 83
@@ -106,9 +88,13 @@ static inline int usb_autoresume_device(struct usb_device *udev)
106 return 0; 88 return 0;
107} 89}
108 90
91static inline int usb_remote_wakeup(struct usb_device *udev)
92{
93 return 0;
94}
95
109#endif 96#endif
110 97
111extern struct workqueue_struct *ksuspend_usb_wq;
112extern struct bus_type usb_bus_type; 98extern struct bus_type usb_bus_type;
113extern struct device_type usb_device_type; 99extern struct device_type usb_device_type;
114extern struct device_type usb_if_device_type; 100extern struct device_type usb_if_device_type;
@@ -138,23 +124,6 @@ static inline int is_usb_device_driver(struct device_driver *drv)
138 for_devices; 124 for_devices;
139} 125}
140 126
141/* Interfaces and their "power state" are owned by usbcore */
142
143static inline void mark_active(struct usb_interface *f)
144{
145 f->is_active = 1;
146}
147
148static inline void mark_quiesced(struct usb_interface *f)
149{
150 f->is_active = 0;
151}
152
153static inline int is_active(const struct usb_interface *f)
154{
155 return f->is_active;
156}
157
158 127
159/* for labeling diagnostics */ 128/* for labeling diagnostics */
160extern const char *usbcore_name; 129extern const char *usbcore_name;