aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/core/hub.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/core/hub.c')
-rw-r--r--drivers/usb/core/hub.c426
1 files changed, 191 insertions, 235 deletions
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index a12cab5314e9..256d9f698715 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -436,9 +436,10 @@ static void hub_power_on(struct usb_hub *hub)
436{ 436{
437 int port1; 437 int port1;
438 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2; 438 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
439 u16 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
439 440
440 /* if hub supports power switching, enable power on each port */ 441 /* if hub supports power switching, enable power on each port */
441 if ((hub->descriptor->wHubCharacteristics & HUB_CHAR_LPSM) < 2) { 442 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2) {
442 dev_dbg(hub->intfdev, "enabling power on all ports\n"); 443 dev_dbg(hub->intfdev, "enabling power on all ports\n");
443 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++) 444 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
444 set_port_feature(hub->hdev, port1, 445 set_port_feature(hub->hdev, port1,
@@ -449,10 +450,18 @@ static void hub_power_on(struct usb_hub *hub)
449 msleep(max(pgood_delay, (unsigned) 100)); 450 msleep(max(pgood_delay, (unsigned) 100));
450} 451}
451 452
452static void hub_quiesce(struct usb_hub *hub) 453static inline void __hub_quiesce(struct usb_hub *hub)
453{ 454{
454 /* stop khubd and related activity */ 455 /* (nonblocking) khubd and related activity won't re-trigger */
455 hub->quiescing = 1; 456 hub->quiescing = 1;
457 hub->activating = 0;
458 hub->resume_root_hub = 0;
459}
460
461static void hub_quiesce(struct usb_hub *hub)
462{
463 /* (blocking) stop khubd and related activity */
464 __hub_quiesce(hub);
456 usb_kill_urb(hub->urb); 465 usb_kill_urb(hub->urb);
457 if (hub->has_indicators) 466 if (hub->has_indicators)
458 cancel_delayed_work(&hub->leds); 467 cancel_delayed_work(&hub->leds);
@@ -466,6 +475,7 @@ static void hub_activate(struct usb_hub *hub)
466 475
467 hub->quiescing = 0; 476 hub->quiescing = 0;
468 hub->activating = 1; 477 hub->activating = 1;
478 hub->resume_root_hub = 0;
469 status = usb_submit_urb(hub->urb, GFP_NOIO); 479 status = usb_submit_urb(hub->urb, GFP_NOIO);
470 if (status < 0) 480 if (status < 0)
471 dev_err(hub->intfdev, "activate --> %d\n", status); 481 dev_err(hub->intfdev, "activate --> %d\n", status);
@@ -516,6 +526,7 @@ static int hub_configure(struct usb_hub *hub,
516 struct usb_device *hdev = hub->hdev; 526 struct usb_device *hdev = hub->hdev;
517 struct device *hub_dev = hub->intfdev; 527 struct device *hub_dev = hub->intfdev;
518 u16 hubstatus, hubchange; 528 u16 hubstatus, hubchange;
529 u16 wHubCharacteristics;
519 unsigned int pipe; 530 unsigned int pipe;
520 int maxp, ret; 531 int maxp, ret;
521 char *message; 532 char *message;
@@ -561,9 +572,9 @@ static int hub_configure(struct usb_hub *hub,
561 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild, 572 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
562 (hdev->maxchild == 1) ? "" : "s"); 573 (hdev->maxchild == 1) ? "" : "s");
563 574
564 le16_to_cpus(&hub->descriptor->wHubCharacteristics); 575 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
565 576
566 if (hub->descriptor->wHubCharacteristics & HUB_CHAR_COMPOUND) { 577 if (wHubCharacteristics & HUB_CHAR_COMPOUND) {
567 int i; 578 int i;
568 char portstr [USB_MAXCHILDREN + 1]; 579 char portstr [USB_MAXCHILDREN + 1];
569 580
@@ -576,7 +587,7 @@ static int hub_configure(struct usb_hub *hub,
576 } else 587 } else
577 dev_dbg(hub_dev, "standalone hub\n"); 588 dev_dbg(hub_dev, "standalone hub\n");
578 589
579 switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_LPSM) { 590 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
580 case 0x00: 591 case 0x00:
581 dev_dbg(hub_dev, "ganged power switching\n"); 592 dev_dbg(hub_dev, "ganged power switching\n");
582 break; 593 break;
@@ -589,7 +600,7 @@ static int hub_configure(struct usb_hub *hub,
589 break; 600 break;
590 } 601 }
591 602
592 switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_OCPM) { 603 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
593 case 0x00: 604 case 0x00:
594 dev_dbg(hub_dev, "global over-current protection\n"); 605 dev_dbg(hub_dev, "global over-current protection\n");
595 break; 606 break;
@@ -629,7 +640,7 @@ static int hub_configure(struct usb_hub *hub,
629 } 640 }
630 641
631 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */ 642 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
632 switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_TTTT) { 643 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
633 case HUB_TTTT_8_BITS: 644 case HUB_TTTT_8_BITS:
634 if (hdev->descriptor.bDeviceProtocol != 0) { 645 if (hdev->descriptor.bDeviceProtocol != 0) {
635 hub->tt.think_time = 666; 646 hub->tt.think_time = 666;
@@ -659,7 +670,7 @@ static int hub_configure(struct usb_hub *hub,
659 } 670 }
660 671
661 /* probe() zeroes hub->indicator[] */ 672 /* probe() zeroes hub->indicator[] */
662 if (hub->descriptor->wHubCharacteristics & HUB_CHAR_PORTIND) { 673 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
663 hub->has_indicators = 1; 674 hub->has_indicators = 1;
664 dev_dbg(hub_dev, "Port indicators are supported\n"); 675 dev_dbg(hub_dev, "Port indicators are supported\n");
665 } 676 }
@@ -704,7 +715,7 @@ static int hub_configure(struct usb_hub *hub,
704 (hubstatus & HUB_STATUS_LOCAL_POWER) 715 (hubstatus & HUB_STATUS_LOCAL_POWER)
705 ? "lost (inactive)" : "good"); 716 ? "lost (inactive)" : "good");
706 717
707 if ((hub->descriptor->wHubCharacteristics & HUB_CHAR_OCPM) == 0) 718 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
708 dev_dbg(hub_dev, "%sover-current condition exists\n", 719 dev_dbg(hub_dev, "%sover-current condition exists\n",
709 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no "); 720 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
710 721
@@ -854,14 +865,12 @@ descriptor_error:
854 /* We found a hub */ 865 /* We found a hub */
855 dev_info (&intf->dev, "USB hub found\n"); 866 dev_info (&intf->dev, "USB hub found\n");
856 867
857 hub = kmalloc(sizeof(*hub), GFP_KERNEL); 868 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
858 if (!hub) { 869 if (!hub) {
859 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n"); 870 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
860 return -ENOMEM; 871 return -ENOMEM;
861 } 872 }
862 873
863 memset(hub, 0, sizeof(*hub));
864
865 INIT_LIST_HEAD(&hub->event_list); 874 INIT_LIST_HEAD(&hub->event_list);
866 hub->intfdev = &intf->dev; 875 hub->intfdev = &intf->dev;
867 hub->hdev = hdev; 876 hub->hdev = hdev;
@@ -1020,9 +1029,15 @@ void usb_set_device_state(struct usb_device *udev,
1020 spin_lock_irqsave(&device_state_lock, flags); 1029 spin_lock_irqsave(&device_state_lock, flags);
1021 if (udev->state == USB_STATE_NOTATTACHED) 1030 if (udev->state == USB_STATE_NOTATTACHED)
1022 ; /* do nothing */ 1031 ; /* do nothing */
1023 else if (new_state != USB_STATE_NOTATTACHED) 1032 else if (new_state != USB_STATE_NOTATTACHED) {
1024 udev->state = new_state; 1033 udev->state = new_state;
1025 else 1034 if (new_state == USB_STATE_CONFIGURED)
1035 device_init_wakeup(&udev->dev,
1036 (udev->actconfig->desc.bmAttributes
1037 & USB_CONFIG_ATT_WAKEUP));
1038 else if (new_state != USB_STATE_SUSPENDED)
1039 device_init_wakeup(&udev->dev, 0);
1040 } else
1026 recursively_mark_NOTATTACHED(udev); 1041 recursively_mark_NOTATTACHED(udev);
1027 spin_unlock_irqrestore(&device_state_lock, flags); 1042 spin_unlock_irqrestore(&device_state_lock, flags);
1028} 1043}
@@ -1111,14 +1126,14 @@ void usb_disconnect(struct usb_device **pdev)
1111 */ 1126 */
1112 usb_disable_device(udev, 0); 1127 usb_disable_device(udev, 0);
1113 1128
1129 usb_notify_remove_device(udev);
1130
1114 /* Free the device number, remove the /proc/bus/usb entry and 1131 /* Free the device number, remove the /proc/bus/usb entry and
1115 * the sysfs attributes, and delete the parent's children[] 1132 * the sysfs attributes, and delete the parent's children[]
1116 * (or root_hub) pointer. 1133 * (or root_hub) pointer.
1117 */ 1134 */
1118 dev_dbg (&udev->dev, "unregistering device\n"); 1135 dev_dbg (&udev->dev, "unregistering device\n");
1119 release_address(udev); 1136 release_address(udev);
1120 usbfs_remove_device(udev);
1121 usbdev_remove(udev);
1122 usb_remove_sysfs_dev_files(udev); 1137 usb_remove_sysfs_dev_files(udev);
1123 1138
1124 /* Avoid races with recursively_mark_NOTATTACHED() */ 1139 /* Avoid races with recursively_mark_NOTATTACHED() */
@@ -1189,21 +1204,6 @@ static inline void show_string(struct usb_device *udev, char *id, char *string)
1189{} 1204{}
1190#endif 1205#endif
1191 1206
1192static void get_string(struct usb_device *udev, char **string, int index)
1193{
1194 char *buf;
1195
1196 if (!index)
1197 return;
1198 buf = kmalloc(256, GFP_KERNEL);
1199 if (!buf)
1200 return;
1201 if (usb_string(udev, index, buf, 256) > 0)
1202 *string = buf;
1203 else
1204 kfree(buf);
1205}
1206
1207 1207
1208#ifdef CONFIG_USB_OTG 1208#ifdef CONFIG_USB_OTG
1209#include "otg_whitelist.h" 1209#include "otg_whitelist.h"
@@ -1242,9 +1242,10 @@ int usb_new_device(struct usb_device *udev)
1242 } 1242 }
1243 1243
1244 /* read the standard strings and cache them if present */ 1244 /* read the standard strings and cache them if present */
1245 get_string(udev, &udev->product, udev->descriptor.iProduct); 1245 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
1246 get_string(udev, &udev->manufacturer, udev->descriptor.iManufacturer); 1246 udev->manufacturer = usb_cache_string(udev,
1247 get_string(udev, &udev->serial, udev->descriptor.iSerialNumber); 1247 udev->descriptor.iManufacturer);
1248 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
1248 1249
1249 /* Tell the world! */ 1250 /* Tell the world! */
1250 dev_dbg(&udev->dev, "new device strings: Mfr=%d, Product=%d, " 1251 dev_dbg(&udev->dev, "new device strings: Mfr=%d, Product=%d, "
@@ -1316,11 +1317,9 @@ int usb_new_device(struct usb_device *udev)
1316 * (Includes HNP test device.) 1317 * (Includes HNP test device.)
1317 */ 1318 */
1318 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) { 1319 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
1319 static int __usb_suspend_device (struct usb_device *, 1320 static int __usb_suspend_device(struct usb_device *,
1320 int port1, pm_message_t state); 1321 int port1);
1321 err = __usb_suspend_device(udev, 1322 err = __usb_suspend_device(udev, udev->bus->otg_port);
1322 udev->bus->otg_port,
1323 PMSG_SUSPEND);
1324 if (err < 0) 1323 if (err < 0)
1325 dev_dbg(&udev->dev, "HNP fail, %d\n", err); 1324 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
1326 } 1325 }
@@ -1356,10 +1355,8 @@ int usb_new_device(struct usb_device *udev)
1356 } 1355 }
1357 1356
1358 /* USB device state == configured ... usable */ 1357 /* USB device state == configured ... usable */
1358 usb_notify_add_device(udev);
1359 1359
1360 /* add a /proc/bus/usb entry */
1361 usbdev_add(udev);
1362 usbfs_add_device(udev);
1363 return 0; 1360 return 0;
1364 1361
1365fail: 1362fail:
@@ -1510,7 +1507,7 @@ static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
1510 /* FIXME let caller ask to power down the port: 1507 /* FIXME let caller ask to power down the port:
1511 * - some devices won't enumerate without a VBUS power cycle 1508 * - some devices won't enumerate without a VBUS power cycle
1512 * - SRP saves power that way 1509 * - SRP saves power that way
1513 * - usb_suspend_device(dev, PMSG_SUSPEND) 1510 * - ... new call, TBD ...
1514 * That's easy if this hub can switch power per-port, and 1511 * That's easy if this hub can switch power per-port, and
1515 * khubd reactivates the port later (timer, SRP, etc). 1512 * khubd reactivates the port later (timer, SRP, etc).
1516 * Powerdown must be optional, because of reset/DFU. 1513 * Powerdown must be optional, because of reset/DFU.
@@ -1546,11 +1543,7 @@ static int hub_port_suspend(struct usb_hub *hub, int port1,
1546 * NOTE: OTG devices may issue remote wakeup (or SRP) even when 1543 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
1547 * we don't explicitly enable it here. 1544 * we don't explicitly enable it here.
1548 */ 1545 */
1549 if (udev->actconfig 1546 if (device_may_wakeup(&udev->dev)) {
1550 // && FIXME (remote wakeup enabled on this bus)
1551 // ... currently assuming it's always appropriate
1552 && (udev->actconfig->desc.bmAttributes
1553 & USB_CONFIG_ATT_WAKEUP) != 0) {
1554 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 1547 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1555 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, 1548 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
1556 USB_DEVICE_REMOTE_WAKEUP, 0, 1549 USB_DEVICE_REMOTE_WAKEUP, 0,
@@ -1596,11 +1589,14 @@ static int hub_port_suspend(struct usb_hub *hub, int port1,
1596 * Other than re-initializing the hub (plug/unplug, except for root hubs), 1589 * Other than re-initializing the hub (plug/unplug, except for root hubs),
1597 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd 1590 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
1598 * timer, no SRP, no requests through sysfs. 1591 * timer, no SRP, no requests through sysfs.
1592 *
1593 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
1594 * the root hub for their bus goes into global suspend ... so we don't
1595 * (falsely) update the device power state to say it suspended.
1599 */ 1596 */
1600static int __usb_suspend_device (struct usb_device *udev, int port1, 1597static int __usb_suspend_device (struct usb_device *udev, int port1)
1601 pm_message_t state)
1602{ 1598{
1603 int status; 1599 int status = 0;
1604 1600
1605 /* caller owns the udev device lock */ 1601 /* caller owns the udev device lock */
1606 if (port1 < 0) 1602 if (port1 < 0)
@@ -1611,95 +1607,39 @@ static int __usb_suspend_device (struct usb_device *udev, int port1,
1611 return 0; 1607 return 0;
1612 } 1608 }
1613 1609
1614 /* suspend interface drivers; if this is a hub, it 1610 /* all interfaces must already be suspended */
1615 * suspends the child devices
1616 */
1617 if (udev->actconfig) { 1611 if (udev->actconfig) {
1618 int i; 1612 int i;
1619 1613
1620 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 1614 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1621 struct usb_interface *intf; 1615 struct usb_interface *intf;
1622 struct usb_driver *driver;
1623 1616
1624 intf = udev->actconfig->interface[i]; 1617 intf = udev->actconfig->interface[i];
1625 if (state.event <= intf->dev.power.power_state.event) 1618 if (is_active(intf)) {
1626 continue; 1619 dev_dbg(&intf->dev, "nyet suspended\n");
1627 if (!intf->dev.driver) 1620 return -EBUSY;
1628 continue;
1629 driver = to_usb_driver(intf->dev.driver);
1630
1631 if (driver->suspend) {
1632 status = driver->suspend(intf, state);
1633 if (intf->dev.power.power_state.event != state.event
1634 || status)
1635 dev_err(&intf->dev,
1636 "suspend %d fail, code %d\n",
1637 state.event, status);
1638 }
1639
1640 /* only drivers with suspend() can ever resume();
1641 * and after power loss, even they won't.
1642 * bus_rescan_devices() can rebind drivers later.
1643 *
1644 * FIXME the PM core self-deadlocks when unbinding
1645 * drivers during suspend/resume ... everything grabs
1646 * dpm_sem (not a spinlock, ugh). we want to unbind,
1647 * since we know every driver's probe/disconnect works
1648 * even for drivers that can't suspend.
1649 */
1650 if (!driver->suspend || state.event > PM_EVENT_FREEZE) {
1651#if 1
1652 dev_warn(&intf->dev, "resume is unsafe!\n");
1653#else
1654 down_write(&usb_bus_type.rwsem);
1655 device_release_driver(&intf->dev);
1656 up_write(&usb_bus_type.rwsem);
1657#endif
1658 } 1621 }
1659 } 1622 }
1660 } 1623 }
1661 1624
1662 /* 1625 /* we only change a device's upstream USB link.
1663 * FIXME this needs port power off call paths too, to help force 1626 * root hubs have no upstream USB link.
1664 * USB into the "generic" PM model. At least for devices on
1665 * ports that aren't using ganged switching (usually root hubs).
1666 *
1667 * NOTE: SRP-capable links should adopt more aggressive poweroff
1668 * policies (when HNP doesn't apply) once we have mechanisms to
1669 * turn power back on! (Likely not before 2.7...)
1670 */ 1627 */
1671 if (state.event > PM_EVENT_FREEZE) { 1628 if (udev->parent)
1672 dev_warn(&udev->dev, "no poweroff yet, suspending instead\n");
1673 }
1674
1675 /* "global suspend" of the HC-to-USB interface (root hub), or
1676 * "selective suspend" of just one hub-device link.
1677 */
1678 if (!udev->parent) {
1679 struct usb_bus *bus = udev->bus;
1680 if (bus && bus->op->hub_suspend) {
1681 status = bus->op->hub_suspend (bus);
1682 if (status == 0) {
1683 dev_dbg(&udev->dev, "usb suspend\n");
1684 usb_set_device_state(udev,
1685 USB_STATE_SUSPENDED);
1686 }
1687 } else
1688 status = -EOPNOTSUPP;
1689 } else
1690 status = hub_port_suspend(hdev_to_hub(udev->parent), port1, 1629 status = hub_port_suspend(hdev_to_hub(udev->parent), port1,
1691 udev); 1630 udev);
1692 1631
1693 if (status == 0) 1632 if (status == 0)
1694 udev->dev.power.power_state = state; 1633 udev->dev.power.power_state = PMSG_SUSPEND;
1695 return status; 1634 return status;
1696} 1635}
1697 1636
1698/** 1637#endif
1638
1639/*
1699 * usb_suspend_device - suspend a usb device 1640 * usb_suspend_device - suspend a usb device
1700 * @udev: device that's no longer in active use 1641 * @udev: device that's no longer in active use
1701 * @state: PMSG_SUSPEND to suspend 1642 * Context: must be able to sleep; device not locked; pm locks held
1702 * Context: must be able to sleep; device not locked
1703 * 1643 *
1704 * Suspends a USB device that isn't in active use, conserving power. 1644 * Suspends a USB device that isn't in active use, conserving power.
1705 * Devices may wake out of a suspend, if anything important happens, 1645 * Devices may wake out of a suspend, if anything important happens,
@@ -1707,37 +1647,50 @@ static int __usb_suspend_device (struct usb_device *udev, int port1,
1707 * suspend by the host, using usb_resume_device(). It's also routine 1647 * suspend by the host, using usb_resume_device(). It's also routine
1708 * to disconnect devices while they are suspended. 1648 * to disconnect devices while they are suspended.
1709 * 1649 *
1650 * This only affects the USB hardware for a device; its interfaces
1651 * (and, for hubs, child devices) must already have been suspended.
1652 *
1710 * Suspending OTG devices may trigger HNP, if that's been enabled 1653 * Suspending OTG devices may trigger HNP, if that's been enabled
1711 * between a pair of dual-role devices. That will change roles, such 1654 * between a pair of dual-role devices. That will change roles, such
1712 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral. 1655 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
1713 * 1656 *
1714 * Returns 0 on success, else negative errno. 1657 * Returns 0 on success, else negative errno.
1715 */ 1658 */
1716int usb_suspend_device(struct usb_device *udev, pm_message_t state) 1659int usb_suspend_device(struct usb_device *udev)
1717{ 1660{
1661#ifdef CONFIG_USB_SUSPEND
1718 int port1, status; 1662 int port1, status;
1719 1663
1720 port1 = locktree(udev); 1664 port1 = locktree(udev);
1721 if (port1 < 0) 1665 if (port1 < 0)
1722 return port1; 1666 return port1;
1723 1667
1724 status = __usb_suspend_device(udev, port1, state); 1668 status = __usb_suspend_device(udev, port1);
1725 usb_unlock_device(udev); 1669 usb_unlock_device(udev);
1726 return status; 1670 return status;
1671#else
1672 /* NOTE: udev->state unchanged, it's not lying ... */
1673 udev->dev.power.power_state = PMSG_SUSPEND;
1674 return 0;
1675#endif
1727} 1676}
1677EXPORT_SYMBOL_GPL(usb_suspend_device);
1728 1678
1729/* 1679/*
1680 * If the USB "suspend" state is in use (rather than "global suspend"),
1681 * many devices will be individually taken out of suspend state using
1682 * special" resume" signaling. These routines kick in shortly after
1730 * hardware resume signaling is finished, either because of selective 1683 * hardware resume signaling is finished, either because of selective
1731 * resume (by host) or remote wakeup (by device) ... now see what changed 1684 * resume (by host) or remote wakeup (by device) ... now see what changed
1732 * in the tree that's rooted at this device. 1685 * in the tree that's rooted at this device.
1733 */ 1686 */
1734static int finish_port_resume(struct usb_device *udev) 1687static int finish_device_resume(struct usb_device *udev)
1735{ 1688{
1736 int status; 1689 int status;
1737 u16 devstatus; 1690 u16 devstatus;
1738 1691
1739 /* caller owns the udev device lock */ 1692 /* caller owns the udev device lock */
1740 dev_dbg(&udev->dev, "usb resume\n"); 1693 dev_dbg(&udev->dev, "finish resume\n");
1741 1694
1742 /* usb ch9 identifies four variants of SUSPENDED, based on what 1695 /* usb ch9 identifies four variants of SUSPENDED, based on what
1743 * state the device resumes to. Linux currently won't see the 1696 * state the device resumes to. Linux currently won't see the
@@ -1747,7 +1700,6 @@ static int finish_port_resume(struct usb_device *udev)
1747 usb_set_device_state(udev, udev->actconfig 1700 usb_set_device_state(udev, udev->actconfig
1748 ? USB_STATE_CONFIGURED 1701 ? USB_STATE_CONFIGURED
1749 : USB_STATE_ADDRESS); 1702 : USB_STATE_ADDRESS);
1750 udev->dev.power.power_state = PMSG_ON;
1751 1703
1752 /* 10.5.4.5 says be sure devices in the tree are still there. 1704 /* 10.5.4.5 says be sure devices in the tree are still there.
1753 * For now let's assume the device didn't go crazy on resume, 1705 * For now let's assume the device didn't go crazy on resume,
@@ -1760,9 +1712,11 @@ static int finish_port_resume(struct usb_device *udev)
1760 status); 1712 status);
1761 else if (udev->actconfig) { 1713 else if (udev->actconfig) {
1762 unsigned i; 1714 unsigned i;
1715 int (*resume)(struct device *);
1763 1716
1764 le16_to_cpus(&devstatus); 1717 le16_to_cpus(&devstatus);
1765 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) { 1718 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)
1719 && udev->parent) {
1766 status = usb_control_msg(udev, 1720 status = usb_control_msg(udev,
1767 usb_sndctrlpipe(udev, 0), 1721 usb_sndctrlpipe(udev, 0),
1768 USB_REQ_CLEAR_FEATURE, 1722 USB_REQ_CLEAR_FEATURE,
@@ -1778,33 +1732,11 @@ static int finish_port_resume(struct usb_device *udev)
1778 } 1732 }
1779 1733
1780 /* resume interface drivers; if this is a hub, it 1734 /* resume interface drivers; if this is a hub, it
1781 * resumes the child devices 1735 * may have a child resume event to deal with soon
1782 */ 1736 */
1783 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 1737 resume = udev->dev.bus->resume;
1784 struct usb_interface *intf; 1738 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++)
1785 struct usb_driver *driver; 1739 (void) resume(&udev->actconfig->interface[i]->dev);
1786
1787 intf = udev->actconfig->interface[i];
1788 if (intf->dev.power.power_state.event == PM_EVENT_ON)
1789 continue;
1790 if (!intf->dev.driver) {
1791 /* FIXME maybe force to alt 0 */
1792 continue;
1793 }
1794 driver = to_usb_driver(intf->dev.driver);
1795
1796 /* bus_rescan_devices() may rebind drivers */
1797 if (!driver->resume)
1798 continue;
1799
1800 /* can we do better than just logging errors? */
1801 status = driver->resume(intf);
1802 if (intf->dev.power.power_state.event != PM_EVENT_ON
1803 || status)
1804 dev_dbg(&intf->dev,
1805 "resume fail, state %d code %d\n",
1806 intf->dev.power.power_state.event, status);
1807 }
1808 status = 0; 1740 status = 0;
1809 1741
1810 } else if (udev->devnum <= 0) { 1742 } else if (udev->devnum <= 0) {
@@ -1814,6 +1746,8 @@ static int finish_port_resume(struct usb_device *udev)
1814 return status; 1746 return status;
1815} 1747}
1816 1748
1749#ifdef CONFIG_USB_SUSPEND
1750
1817static int 1751static int
1818hub_port_resume(struct usb_hub *hub, int port1, struct usb_device *udev) 1752hub_port_resume(struct usb_hub *hub, int port1, struct usb_device *udev)
1819{ 1753{
@@ -1859,7 +1793,7 @@ hub_port_resume(struct usb_hub *hub, int port1, struct usb_device *udev)
1859 /* TRSMRCY = 10 msec */ 1793 /* TRSMRCY = 10 msec */
1860 msleep(10); 1794 msleep(10);
1861 if (udev) 1795 if (udev)
1862 status = finish_port_resume(udev); 1796 status = finish_device_resume(udev);
1863 } 1797 }
1864 } 1798 }
1865 if (status < 0) 1799 if (status < 0)
@@ -1868,12 +1802,12 @@ hub_port_resume(struct usb_hub *hub, int port1, struct usb_device *udev)
1868 return status; 1802 return status;
1869} 1803}
1870 1804
1871static int hub_resume (struct usb_interface *intf); 1805#endif
1872 1806
1873/** 1807/*
1874 * usb_resume_device - re-activate a suspended usb device 1808 * usb_resume_device - re-activate a suspended usb device
1875 * @udev: device to re-activate 1809 * @udev: device to re-activate
1876 * Context: must be able to sleep; device not locked 1810 * Context: must be able to sleep; device not locked; pm locks held
1877 * 1811 *
1878 * This will re-activate the suspended device, increasing power usage 1812 * This will re-activate the suspended device, increasing power usage
1879 * while letting drivers communicate again with its endpoints. 1813 * while letting drivers communicate again with its endpoints.
@@ -1891,35 +1825,22 @@ int usb_resume_device(struct usb_device *udev)
1891 if (port1 < 0) 1825 if (port1 < 0)
1892 return port1; 1826 return port1;
1893 1827
1894 /* "global resume" of the HC-to-USB interface (root hub), or 1828#ifdef CONFIG_USB_SUSPEND
1895 * selective resume of one hub-to-device port 1829 /* selective resume of one downstream hub-to-device port */
1896 */ 1830 if (udev->parent) {
1897 if (!udev->parent) { 1831 if (udev->state == USB_STATE_SUSPENDED) {
1898 struct usb_bus *bus = udev->bus; 1832 // NOTE swsusp may bork us, device state being wrong...
1899 if (bus && bus->op->hub_resume) { 1833 // NOTE this fails if parent is also suspended...
1900 status = bus->op->hub_resume (bus); 1834 status = hub_port_resume(hdev_to_hub(udev->parent),
1835 port1, udev);
1901 } else 1836 } else
1902 status = -EOPNOTSUPP; 1837 status = 0;
1903 if (status == 0) { 1838 } else
1904 dev_dbg(&udev->dev, "usb resume\n"); 1839#endif
1905 /* TRSMRCY = 10 msec */ 1840 status = finish_device_resume(udev);
1906 msleep(10); 1841 if (status < 0)
1907 usb_set_device_state (udev, USB_STATE_CONFIGURED);
1908 udev->dev.power.power_state = PMSG_ON;
1909 status = hub_resume (udev
1910 ->actconfig->interface[0]);
1911 }
1912 } else if (udev->state == USB_STATE_SUSPENDED) {
1913 // NOTE this fails if parent is also suspended...
1914 status = hub_port_resume(hdev_to_hub(udev->parent),
1915 port1, udev);
1916 } else {
1917 status = 0;
1918 }
1919 if (status < 0) {
1920 dev_dbg(&udev->dev, "can't resume, status %d\n", 1842 dev_dbg(&udev->dev, "can't resume, status %d\n",
1921 status); 1843 status);
1922 }
1923 1844
1924 usb_unlock_device(udev); 1845 usb_unlock_device(udev);
1925 1846
@@ -1936,6 +1857,8 @@ static int remote_wakeup(struct usb_device *udev)
1936{ 1857{
1937 int status = 0; 1858 int status = 0;
1938 1859
1860#ifdef CONFIG_USB_SUSPEND
1861
1939 /* don't repeat RESUME sequence if this device 1862 /* don't repeat RESUME sequence if this device
1940 * was already woken up by some other task 1863 * was already woken up by some other task
1941 */ 1864 */
@@ -1944,38 +1867,52 @@ static int remote_wakeup(struct usb_device *udev)
1944 dev_dbg(&udev->dev, "RESUME (wakeup)\n"); 1867 dev_dbg(&udev->dev, "RESUME (wakeup)\n");
1945 /* TRSMRCY = 10 msec */ 1868 /* TRSMRCY = 10 msec */
1946 msleep(10); 1869 msleep(10);
1947 status = finish_port_resume(udev); 1870 status = finish_device_resume(udev);
1948 } 1871 }
1949 up(&udev->serialize); 1872 up(&udev->serialize);
1873#endif
1950 return status; 1874 return status;
1951} 1875}
1952 1876
1953static int hub_suspend(struct usb_interface *intf, pm_message_t state) 1877static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
1954{ 1878{
1955 struct usb_hub *hub = usb_get_intfdata (intf); 1879 struct usb_hub *hub = usb_get_intfdata (intf);
1956 struct usb_device *hdev = hub->hdev; 1880 struct usb_device *hdev = hub->hdev;
1957 unsigned port1; 1881 unsigned port1;
1958 int status;
1959 1882
1960 /* stop khubd and related activity */ 1883 /* fail if children aren't already suspended */
1961 hub_quiesce(hub);
1962
1963 /* then suspend every port */
1964 for (port1 = 1; port1 <= hdev->maxchild; port1++) { 1884 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
1965 struct usb_device *udev; 1885 struct usb_device *udev;
1966 1886
1967 udev = hdev->children [port1-1]; 1887 udev = hdev->children [port1-1];
1968 if (!udev) 1888 if (udev && (udev->dev.power.power_state.event
1969 continue; 1889 == PM_EVENT_ON
1970 down(&udev->serialize); 1890#ifdef CONFIG_USB_SUSPEND
1971 status = __usb_suspend_device(udev, port1, state); 1891 || udev->state != USB_STATE_SUSPENDED
1972 up(&udev->serialize); 1892#endif
1973 if (status < 0) 1893 )) {
1974 dev_dbg(&intf->dev, "suspend port %d --> %d\n", 1894 dev_dbg(&intf->dev, "port %d nyet suspended\n", port1);
1975 port1, status); 1895 return -EBUSY;
1896 }
1976 } 1897 }
1977 1898
1978 intf->dev.power.power_state = state; 1899 /* "global suspend" of the downstream HC-to-USB interface */
1900 if (!hdev->parent) {
1901 struct usb_bus *bus = hdev->bus;
1902 if (bus) {
1903 int status = hcd_bus_suspend (bus);
1904
1905 if (status != 0) {
1906 dev_dbg(&hdev->dev, "'global' suspend %d\n",
1907 status);
1908 return status;
1909 }
1910 } else
1911 return -EOPNOTSUPP;
1912 }
1913
1914 /* stop khubd and related activity */
1915 hub_quiesce(hub);
1979 return 0; 1916 return 0;
1980} 1917}
1981 1918
@@ -1983,11 +1920,35 @@ static int hub_resume(struct usb_interface *intf)
1983{ 1920{
1984 struct usb_device *hdev = interface_to_usbdev(intf); 1921 struct usb_device *hdev = interface_to_usbdev(intf);
1985 struct usb_hub *hub = usb_get_intfdata (intf); 1922 struct usb_hub *hub = usb_get_intfdata (intf);
1986 unsigned port1;
1987 int status; 1923 int status;
1988 1924
1989 if (intf->dev.power.power_state.event == PM_EVENT_ON) 1925 /* "global resume" of the downstream HC-to-USB interface */
1990 return 0; 1926 if (!hdev->parent) {
1927 struct usb_bus *bus = hdev->bus;
1928 if (bus) {
1929 status = hcd_bus_resume (bus);
1930 if (status) {
1931 dev_dbg(&intf->dev, "'global' resume %d\n",
1932 status);
1933 return status;
1934 }
1935 } else
1936 return -EOPNOTSUPP;
1937 if (status == 0) {
1938 /* TRSMRCY = 10 msec */
1939 msleep(10);
1940 }
1941 }
1942
1943 hub_activate(hub);
1944
1945 /* REVISIT: this recursion probably shouldn't exist. Remove
1946 * this code sometime, after retesting with different root and
1947 * external hubs.
1948 */
1949#ifdef CONFIG_USB_SUSPEND
1950 {
1951 unsigned port1;
1991 1952
1992 for (port1 = 1; port1 <= hdev->maxchild; port1++) { 1953 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
1993 struct usb_device *udev; 1954 struct usb_device *udev;
@@ -2013,7 +1974,7 @@ static int hub_resume(struct usb_interface *intf)
2013 if (portstat & USB_PORT_STAT_SUSPEND) 1974 if (portstat & USB_PORT_STAT_SUSPEND)
2014 status = hub_port_resume(hub, port1, udev); 1975 status = hub_port_resume(hub, port1, udev);
2015 else { 1976 else {
2016 status = finish_port_resume(udev); 1977 status = finish_device_resume(udev);
2017 if (status < 0) { 1978 if (status < 0) {
2018 dev_dbg(&intf->dev, "resume port %d --> %d\n", 1979 dev_dbg(&intf->dev, "resume port %d --> %d\n",
2019 port1, status); 1980 port1, status);
@@ -2022,43 +1983,31 @@ static int hub_resume(struct usb_interface *intf)
2022 } 1983 }
2023 up(&udev->serialize); 1984 up(&udev->serialize);
2024 } 1985 }
2025 intf->dev.power.power_state = PMSG_ON; 1986 }
2026 1987#endif
2027 hub->resume_root_hub = 0;
2028 hub_activate(hub);
2029 return 0; 1988 return 0;
2030} 1989}
2031 1990
2032void usb_resume_root_hub(struct usb_device *hdev) 1991void usb_suspend_root_hub(struct usb_device *hdev)
2033{ 1992{
2034 struct usb_hub *hub = hdev_to_hub(hdev); 1993 struct usb_hub *hub = hdev_to_hub(hdev);
2035 1994
2036 hub->resume_root_hub = 1; 1995 /* This also makes any led blinker stop retriggering. We're called
2037 kick_khubd(hub); 1996 * from irq, so the blinker might still be scheduled. Caller promises
1997 * that the root hub status URB will be canceled.
1998 */
1999 __hub_quiesce(hub);
2000 mark_quiesced(to_usb_interface(hub->intfdev));
2038} 2001}
2039 2002
2040#else /* !CONFIG_USB_SUSPEND */ 2003void usb_resume_root_hub(struct usb_device *hdev)
2041
2042int usb_suspend_device(struct usb_device *udev, pm_message_t state)
2043{ 2004{
2044 return 0; 2005 struct usb_hub *hub = hdev_to_hub(hdev);
2045}
2046 2006
2047int usb_resume_device(struct usb_device *udev) 2007 hub->resume_root_hub = 1;
2048{ 2008 kick_khubd(hub);
2049 return 0;
2050} 2009}
2051 2010
2052#define hub_suspend NULL
2053#define hub_resume NULL
2054#define remote_wakeup(x) 0
2055
2056#endif /* CONFIG_USB_SUSPEND */
2057
2058EXPORT_SYMBOL(usb_suspend_device);
2059EXPORT_SYMBOL(usb_resume_device);
2060
2061
2062 2011
2063/* USB 2.0 spec, 7.1.7.3 / fig 7-29: 2012/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
2064 * 2013 *
@@ -2467,6 +2416,7 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
2467{ 2416{
2468 struct usb_device *hdev = hub->hdev; 2417 struct usb_device *hdev = hub->hdev;
2469 struct device *hub_dev = hub->intfdev; 2418 struct device *hub_dev = hub->intfdev;
2419 u16 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2470 int status, i; 2420 int status, i;
2471 2421
2472 dev_dbg (hub_dev, 2422 dev_dbg (hub_dev,
@@ -2504,8 +2454,7 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
2504 if (!(portstatus & USB_PORT_STAT_CONNECTION)) { 2454 if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
2505 2455
2506 /* maybe switch power back on (e.g. root hub was reset) */ 2456 /* maybe switch power back on (e.g. root hub was reset) */
2507 if ((hub->descriptor->wHubCharacteristics 2457 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
2508 & HUB_CHAR_LPSM) < 2
2509 && !(portstatus & (1 << USB_PORT_FEAT_POWER))) 2458 && !(portstatus & (1 << USB_PORT_FEAT_POWER)))
2510 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 2459 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
2511 2460
@@ -2684,21 +2633,28 @@ static void hub_events(void)
2684 intf = to_usb_interface(hub->intfdev); 2633 intf = to_usb_interface(hub->intfdev);
2685 hub_dev = &intf->dev; 2634 hub_dev = &intf->dev;
2686 2635
2687 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n", 2636 i = hub->resume_root_hub;
2637
2638 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x%s\n",
2688 hdev->state, hub->descriptor 2639 hdev->state, hub->descriptor
2689 ? hub->descriptor->bNbrPorts 2640 ? hub->descriptor->bNbrPorts
2690 : 0, 2641 : 0,
2691 /* NOTE: expects max 15 ports... */ 2642 /* NOTE: expects max 15 ports... */
2692 (u16) hub->change_bits[0], 2643 (u16) hub->change_bits[0],
2693 (u16) hub->event_bits[0]); 2644 (u16) hub->event_bits[0],
2645 i ? ", resume root" : "");
2694 2646
2695 usb_get_intf(intf); 2647 usb_get_intf(intf);
2696 i = hub->resume_root_hub;
2697 spin_unlock_irq(&hub_event_lock); 2648 spin_unlock_irq(&hub_event_lock);
2698 2649
2699 /* Is this is a root hub wanting to be resumed? */ 2650 /* Is this is a root hub wanting to reactivate the downstream
2700 if (i) 2651 * ports? If so, be sure the interface resumes even if its
2701 usb_resume_device(hdev); 2652 * stub "device" node was never suspended.
2653 */
2654 if (i) {
2655 dpm_runtime_resume(&hdev->dev);
2656 dpm_runtime_resume(&intf->dev);
2657 }
2702 2658
2703 /* Lock the device, then check to see if we were 2659 /* Lock the device, then check to see if we were
2704 * disconnected while waiting for the lock to succeed. */ 2660 * disconnected while waiting for the lock to succeed. */