aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/core/driver.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/core/driver.c')
-rw-r--r--drivers/usb/core/driver.c137
1 files changed, 69 insertions, 68 deletions
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index 73c49362cd4..8586817698a 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -29,13 +29,6 @@
29#include "hcd.h" 29#include "hcd.h"
30#include "usb.h" 30#include "usb.h"
31 31
32#define VERBOSE_DEBUG 0
33
34#if VERBOSE_DEBUG
35#define dev_vdbg dev_dbg
36#else
37#define dev_vdbg(dev, fmt, args...) do { } while (0)
38#endif
39 32
40#ifdef CONFIG_HOTPLUG 33#ifdef CONFIG_HOTPLUG
41 34
@@ -67,7 +60,7 @@ ssize_t usb_store_new_id(struct usb_dynids *dynids,
67 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE; 60 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
68 61
69 spin_lock(&dynids->lock); 62 spin_lock(&dynids->lock);
70 list_add_tail(&dynids->list, &dynid->node); 63 list_add_tail(&dynid->node, &dynids->list);
71 spin_unlock(&dynids->lock); 64 spin_unlock(&dynids->lock);
72 65
73 if (get_driver(driver)) { 66 if (get_driver(driver)) {
@@ -209,6 +202,11 @@ static int usb_probe_interface(struct device *dev)
209 intf = to_usb_interface(dev); 202 intf = to_usb_interface(dev);
210 udev = interface_to_usbdev(intf); 203 udev = interface_to_usbdev(intf);
211 204
205 if (udev->authorized == 0) {
206 dev_err(&intf->dev, "Device is not authorized for usage\n");
207 return -ENODEV;
208 }
209
212 id = usb_match_id(intf, driver->id_table); 210 id = usb_match_id(intf, driver->id_table);
213 if (!id) 211 if (!id)
214 id = usb_match_dynamic_id(intf, driver); 212 id = usb_match_dynamic_id(intf, driver);
@@ -583,12 +581,9 @@ static int usb_device_match(struct device *dev, struct device_driver *drv)
583} 581}
584 582
585#ifdef CONFIG_HOTPLUG 583#ifdef CONFIG_HOTPLUG
586static int usb_uevent(struct device *dev, char **envp, int num_envp, 584static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
587 char *buffer, int buffer_size)
588{ 585{
589 struct usb_device *usb_dev; 586 struct usb_device *usb_dev;
590 int i = 0;
591 int length = 0;
592 587
593 if (!dev) 588 if (!dev)
594 return -ENODEV; 589 return -ENODEV;
@@ -617,51 +612,39 @@ static int usb_uevent(struct device *dev, char **envp, int num_envp,
617 * all the device descriptors we don't tell them about. Or 612 * all the device descriptors we don't tell them about. Or
618 * act as usermode drivers. 613 * act as usermode drivers.
619 */ 614 */
620 if (add_uevent_var(envp, num_envp, &i, 615 if (add_uevent_var(env, "DEVICE=/proc/bus/usb/%03d/%03d",
621 buffer, buffer_size, &length,
622 "DEVICE=/proc/bus/usb/%03d/%03d",
623 usb_dev->bus->busnum, usb_dev->devnum)) 616 usb_dev->bus->busnum, usb_dev->devnum))
624 return -ENOMEM; 617 return -ENOMEM;
625#endif 618#endif
626 619
627 /* per-device configurations are common */ 620 /* per-device configurations are common */
628 if (add_uevent_var(envp, num_envp, &i, 621 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
629 buffer, buffer_size, &length,
630 "PRODUCT=%x/%x/%x",
631 le16_to_cpu(usb_dev->descriptor.idVendor), 622 le16_to_cpu(usb_dev->descriptor.idVendor),
632 le16_to_cpu(usb_dev->descriptor.idProduct), 623 le16_to_cpu(usb_dev->descriptor.idProduct),
633 le16_to_cpu(usb_dev->descriptor.bcdDevice))) 624 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
634 return -ENOMEM; 625 return -ENOMEM;
635 626
636 /* class-based driver binding models */ 627 /* class-based driver binding models */
637 if (add_uevent_var(envp, num_envp, &i, 628 if (add_uevent_var(env, "TYPE=%d/%d/%d",
638 buffer, buffer_size, &length,
639 "TYPE=%d/%d/%d",
640 usb_dev->descriptor.bDeviceClass, 629 usb_dev->descriptor.bDeviceClass,
641 usb_dev->descriptor.bDeviceSubClass, 630 usb_dev->descriptor.bDeviceSubClass,
642 usb_dev->descriptor.bDeviceProtocol)) 631 usb_dev->descriptor.bDeviceProtocol))
643 return -ENOMEM; 632 return -ENOMEM;
644 633
645 if (add_uevent_var(envp, num_envp, &i, 634 if (add_uevent_var(env, "BUSNUM=%03d",
646 buffer, buffer_size, &length,
647 "BUSNUM=%03d",
648 usb_dev->bus->busnum)) 635 usb_dev->bus->busnum))
649 return -ENOMEM; 636 return -ENOMEM;
650 637
651 if (add_uevent_var(envp, num_envp, &i, 638 if (add_uevent_var(env, "DEVNUM=%03d",
652 buffer, buffer_size, &length,
653 "DEVNUM=%03d",
654 usb_dev->devnum)) 639 usb_dev->devnum))
655 return -ENOMEM; 640 return -ENOMEM;
656 641
657 envp[i] = NULL;
658 return 0; 642 return 0;
659} 643}
660 644
661#else 645#else
662 646
663static int usb_uevent(struct device *dev, char **envp, 647static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
664 int num_envp, char *buffer, int buffer_size)
665{ 648{
666 return -ENODEV; 649 return -ENODEV;
667} 650}
@@ -952,11 +935,11 @@ done:
952#ifdef CONFIG_USB_SUSPEND 935#ifdef CONFIG_USB_SUSPEND
953 936
954/* Internal routine to check whether we may autosuspend a device. */ 937/* Internal routine to check whether we may autosuspend a device. */
955static int autosuspend_check(struct usb_device *udev) 938static int autosuspend_check(struct usb_device *udev, int reschedule)
956{ 939{
957 int i; 940 int i;
958 struct usb_interface *intf; 941 struct usb_interface *intf;
959 unsigned long suspend_time; 942 unsigned long suspend_time, j;
960 943
961 /* For autosuspend, fail fast if anything is in use or autosuspend 944 /* For autosuspend, fail fast if anything is in use or autosuspend
962 * is disabled. Also fail if any interfaces require remote wakeup 945 * is disabled. Also fail if any interfaces require remote wakeup
@@ -998,20 +981,20 @@ static int autosuspend_check(struct usb_device *udev)
998 } 981 }
999 982
1000 /* If everything is okay but the device hasn't been idle for long 983 /* If everything is okay but the device hasn't been idle for long
1001 * enough, queue a delayed autosuspend request. 984 * enough, queue a delayed autosuspend request. If the device
985 * _has_ been idle for long enough and the reschedule flag is set,
986 * likewise queue a delayed (1 second) autosuspend request.
1002 */ 987 */
1003 if (time_after(suspend_time, jiffies)) { 988 j = jiffies;
989 if (time_before(j, suspend_time))
990 reschedule = 1;
991 else
992 suspend_time = j + HZ;
993 if (reschedule) {
1004 if (!timer_pending(&udev->autosuspend.timer)) { 994 if (!timer_pending(&udev->autosuspend.timer)) {
1005
1006 /* The value of jiffies may change between the
1007 * time_after() comparison above and the subtraction
1008 * below. That's okay; the system behaves sanely
1009 * when a timer is registered for the present moment
1010 * or for the past.
1011 */
1012 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend, 995 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
1013 round_jiffies_relative(suspend_time - jiffies)); 996 round_jiffies_relative(suspend_time - j));
1014 } 997 }
1015 return -EAGAIN; 998 return -EAGAIN;
1016 } 999 }
1017 return 0; 1000 return 0;
@@ -1019,7 +1002,7 @@ static int autosuspend_check(struct usb_device *udev)
1019 1002
1020#else 1003#else
1021 1004
1022static inline int autosuspend_check(struct usb_device *udev) 1005static inline int autosuspend_check(struct usb_device *udev, int reschedule)
1023{ 1006{
1024 return 0; 1007 return 0;
1025} 1008}
@@ -1076,7 +1059,7 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1076 udev->do_remote_wakeup = device_may_wakeup(&udev->dev); 1059 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1077 1060
1078 if (udev->auto_pm) { 1061 if (udev->auto_pm) {
1079 status = autosuspend_check(udev); 1062 status = autosuspend_check(udev, 0);
1080 if (status < 0) 1063 if (status < 0)
1081 goto done; 1064 goto done;
1082 } 1065 }
@@ -1090,15 +1073,8 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1090 break; 1073 break;
1091 } 1074 }
1092 } 1075 }
1093 if (status == 0) { 1076 if (status == 0)
1094
1095 /* Non-root devices don't need to do anything for FREEZE
1096 * or PRETHAW. */
1097 if (udev->parent && (msg.event == PM_EVENT_FREEZE ||
1098 msg.event == PM_EVENT_PRETHAW))
1099 goto done;
1100 status = usb_suspend_device(udev, msg); 1077 status = usb_suspend_device(udev, msg);
1101 }
1102 1078
1103 /* If the suspend failed, resume interfaces that did get suspended */ 1079 /* If the suspend failed, resume interfaces that did get suspended */
1104 if (status != 0) { 1080 if (status != 0) {
@@ -1109,12 +1085,24 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1109 1085
1110 /* Try another autosuspend when the interfaces aren't busy */ 1086 /* Try another autosuspend when the interfaces aren't busy */
1111 if (udev->auto_pm) 1087 if (udev->auto_pm)
1112 autosuspend_check(udev); 1088 autosuspend_check(udev, status == -EBUSY);
1113 1089
1114 /* If the suspend succeeded, propagate it up the tree */ 1090 /* If the suspend succeeded then prevent any more URB submissions,
1091 * flush any outstanding URBs, and propagate the suspend up the tree.
1092 */
1115 } else { 1093 } else {
1116 cancel_delayed_work(&udev->autosuspend); 1094 cancel_delayed_work(&udev->autosuspend);
1117 if (parent) 1095 udev->can_submit = 0;
1096 for (i = 0; i < 16; ++i) {
1097 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1098 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1099 }
1100
1101 /* If this is just a FREEZE or a PRETHAW, udev might
1102 * not really be suspended. Only true suspends get
1103 * propagated up the device tree.
1104 */
1105 if (parent && udev->state == USB_STATE_SUSPENDED)
1118 usb_autosuspend_device(parent); 1106 usb_autosuspend_device(parent);
1119 } 1107 }
1120 1108
@@ -1163,6 +1151,7 @@ static int usb_resume_both(struct usb_device *udev)
1163 status = -ENODEV; 1151 status = -ENODEV;
1164 goto done; 1152 goto done;
1165 } 1153 }
1154 udev->can_submit = 1;
1166 1155
1167 /* Propagate the resume up the tree, if necessary */ 1156 /* Propagate the resume up the tree, if necessary */
1168 if (udev->state == USB_STATE_SUSPENDED) { 1157 if (udev->state == USB_STATE_SUSPENDED) {
@@ -1231,6 +1220,8 @@ static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt)
1231 udev->auto_pm = 1; 1220 udev->auto_pm = 1;
1232 udev->pm_usage_cnt += inc_usage_cnt; 1221 udev->pm_usage_cnt += inc_usage_cnt;
1233 WARN_ON(udev->pm_usage_cnt < 0); 1222 WARN_ON(udev->pm_usage_cnt < 0);
1223 if (inc_usage_cnt)
1224 udev->last_busy = jiffies;
1234 if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) { 1225 if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) {
1235 if (udev->state == USB_STATE_SUSPENDED) 1226 if (udev->state == USB_STATE_SUSPENDED)
1236 status = usb_resume_both(udev); 1227 status = usb_resume_both(udev);
@@ -1239,8 +1230,6 @@ static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt)
1239 else if (inc_usage_cnt) 1230 else if (inc_usage_cnt)
1240 udev->last_busy = jiffies; 1231 udev->last_busy = jiffies;
1241 } else if (inc_usage_cnt <= 0 && udev->pm_usage_cnt <= 0) { 1232 } else if (inc_usage_cnt <= 0 && udev->pm_usage_cnt <= 0) {
1242 if (inc_usage_cnt)
1243 udev->last_busy = jiffies;
1244 status = usb_suspend_both(udev, PMSG_SUSPEND); 1233 status = usb_suspend_both(udev, PMSG_SUSPEND);
1245 } 1234 }
1246 usb_pm_unlock(udev); 1235 usb_pm_unlock(udev);
@@ -1349,16 +1338,15 @@ static int usb_autopm_do_interface(struct usb_interface *intf,
1349 else { 1338 else {
1350 udev->auto_pm = 1; 1339 udev->auto_pm = 1;
1351 intf->pm_usage_cnt += inc_usage_cnt; 1340 intf->pm_usage_cnt += inc_usage_cnt;
1341 udev->last_busy = jiffies;
1352 if (inc_usage_cnt >= 0 && intf->pm_usage_cnt > 0) { 1342 if (inc_usage_cnt >= 0 && intf->pm_usage_cnt > 0) {
1353 if (udev->state == USB_STATE_SUSPENDED) 1343 if (udev->state == USB_STATE_SUSPENDED)
1354 status = usb_resume_both(udev); 1344 status = usb_resume_both(udev);
1355 if (status != 0) 1345 if (status != 0)
1356 intf->pm_usage_cnt -= inc_usage_cnt; 1346 intf->pm_usage_cnt -= inc_usage_cnt;
1357 else if (inc_usage_cnt) 1347 else
1358 udev->last_busy = jiffies; 1348 udev->last_busy = jiffies;
1359 } else if (inc_usage_cnt <= 0 && intf->pm_usage_cnt <= 0) { 1349 } else if (inc_usage_cnt <= 0 && intf->pm_usage_cnt <= 0) {
1360 if (inc_usage_cnt)
1361 udev->last_busy = jiffies;
1362 status = usb_suspend_both(udev, PMSG_SUSPEND); 1350 status = usb_suspend_both(udev, PMSG_SUSPEND);
1363 } 1351 }
1364 } 1352 }
@@ -1537,9 +1525,21 @@ int usb_external_resume_device(struct usb_device *udev)
1537 1525
1538static int usb_suspend(struct device *dev, pm_message_t message) 1526static int usb_suspend(struct device *dev, pm_message_t message)
1539{ 1527{
1528 struct usb_device *udev;
1529
1540 if (!is_usb_device(dev)) /* Ignore PM for interfaces */ 1530 if (!is_usb_device(dev)) /* Ignore PM for interfaces */
1541 return 0; 1531 return 0;
1542 return usb_external_suspend_device(to_usb_device(dev), message); 1532 udev = to_usb_device(dev);
1533
1534 /* If udev is already suspended, we can skip this suspend and
1535 * we should also skip the upcoming system resume. */
1536 if (udev->state == USB_STATE_SUSPENDED) {
1537 udev->skip_sys_resume = 1;
1538 return 0;
1539 }
1540
1541 udev->skip_sys_resume = 0;
1542 return usb_external_suspend_device(udev, message);
1543} 1543}
1544 1544
1545static int usb_resume(struct device *dev) 1545static int usb_resume(struct device *dev)
@@ -1550,13 +1550,14 @@ static int usb_resume(struct device *dev)
1550 return 0; 1550 return 0;
1551 udev = to_usb_device(dev); 1551 udev = to_usb_device(dev);
1552 1552
1553 /* If autoresume is disabled then we also want to prevent resume 1553 /* If udev->skip_sys_resume is set then udev was already suspended
1554 * during system wakeup. However, a "persistent-device" reset-resume 1554 * when the system suspend started, so we don't want to resume
1555 * after power loss counts as a wakeup event. So allow a 1555 * udev during this system wakeup. However a reset-resume counts
1556 * reset-resume to occur if remote wakeup is enabled. */ 1556 * as a wakeup event, so allow a reset-resume to occur if remote
1557 if (udev->autoresume_disabled) { 1557 * wakeup is enabled. */
1558 if (udev->skip_sys_resume) {
1558 if (!(udev->reset_resume && udev->do_remote_wakeup)) 1559 if (!(udev->reset_resume && udev->do_remote_wakeup))
1559 return -EPERM; 1560 return -EHOSTUNREACH;
1560 } 1561 }
1561 return usb_external_resume_device(udev); 1562 return usb_external_resume_device(udev);
1562} 1563}