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.c956
1 files changed, 511 insertions, 445 deletions
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index 229a73f64304..879b66e13370 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -36,11 +36,6 @@
36#define USB_VENDOR_GENESYS_LOGIC 0x05e3 36#define USB_VENDOR_GENESYS_LOGIC 0x05e3
37#define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01 37#define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01
38 38
39static inline int hub_is_superspeed(struct usb_device *hdev)
40{
41 return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
42}
43
44/* Protect struct usb_device->state and ->children members 39/* Protect struct usb_device->state and ->children members
45 * Note: Both are also protected by ->dev.sem, except that ->state can 40 * Note: Both are also protected by ->dev.sem, except that ->state can
46 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */ 41 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
@@ -55,6 +50,9 @@ static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
55 50
56static struct task_struct *khubd_task; 51static struct task_struct *khubd_task;
57 52
53/* synchronize hub-port add/remove and peering operations */
54DEFINE_MUTEX(usb_port_peer_mutex);
55
58/* cycle leds on hubs that aren't blinking for attention */ 56/* cycle leds on hubs that aren't blinking for attention */
59static bool blinkenlights = 0; 57static bool blinkenlights = 0;
60module_param (blinkenlights, bool, S_IRUGO); 58module_param (blinkenlights, bool, S_IRUGO);
@@ -412,30 +410,35 @@ static int set_port_feature(struct usb_device *hdev, int port1, int feature)
412 NULL, 0, 1000); 410 NULL, 0, 1000);
413} 411}
414 412
413static char *to_led_name(int selector)
414{
415 switch (selector) {
416 case HUB_LED_AMBER:
417 return "amber";
418 case HUB_LED_GREEN:
419 return "green";
420 case HUB_LED_OFF:
421 return "off";
422 case HUB_LED_AUTO:
423 return "auto";
424 default:
425 return "??";
426 }
427}
428
415/* 429/*
416 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7 430 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
417 * for info about using port indicators 431 * for info about using port indicators
418 */ 432 */
419static void set_port_led( 433static void set_port_led(struct usb_hub *hub, int port1, int selector)
420 struct usb_hub *hub,
421 int port1,
422 int selector
423)
424{ 434{
425 int status = set_port_feature(hub->hdev, (selector << 8) | port1, 435 struct usb_port *port_dev = hub->ports[port1 - 1];
436 int status;
437
438 status = set_port_feature(hub->hdev, (selector << 8) | port1,
426 USB_PORT_FEAT_INDICATOR); 439 USB_PORT_FEAT_INDICATOR);
427 if (status < 0) 440 dev_dbg(&port_dev->dev, "indicator %s status %d\n",
428 dev_dbg (hub->intfdev, 441 to_led_name(selector), status);
429 "port %d indicator %s status %d\n",
430 port1,
431 ({ char *s; switch (selector) {
432 case HUB_LED_AMBER: s = "amber"; break;
433 case HUB_LED_GREEN: s = "green"; break;
434 case HUB_LED_OFF: s = "off"; break;
435 case HUB_LED_AUTO: s = "auto"; break;
436 default: s = "??"; break;
437 } s; }),
438 status);
439} 442}
440 443
441#define LED_CYCLE_PERIOD ((2*HZ)/3) 444#define LED_CYCLE_PERIOD ((2*HZ)/3)
@@ -743,16 +746,20 @@ int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub,
743 int port1, bool set) 746 int port1, bool set)
744{ 747{
745 int ret; 748 int ret;
746 struct usb_port *port_dev = hub->ports[port1 - 1];
747 749
748 if (set) 750 if (set)
749 ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 751 ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
750 else 752 else
751 ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 753 ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
752 754
753 if (!ret) 755 if (ret)
754 port_dev->power_is_on = set; 756 return ret;
755 return ret; 757
758 if (set)
759 set_bit(port1, hub->power_bits);
760 else
761 clear_bit(port1, hub->power_bits);
762 return 0;
756} 763}
757 764
758/** 765/**
@@ -810,16 +817,9 @@ int usb_hub_clear_tt_buffer(struct urb *urb)
810} 817}
811EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer); 818EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
812 819
813/* If do_delay is false, return the number of milliseconds the caller 820static void hub_power_on(struct usb_hub *hub, bool do_delay)
814 * needs to delay.
815 */
816static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
817{ 821{
818 int port1; 822 int port1;
819 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
820 unsigned delay;
821 u16 wHubCharacteristics =
822 le16_to_cpu(hub->descriptor->wHubCharacteristics);
823 823
824 /* Enable power on each port. Some hubs have reserved values 824 /* Enable power on each port. Some hubs have reserved values
825 * of LPSM (> 2) in their descriptors, even though they are 825 * of LPSM (> 2) in their descriptors, even though they are
@@ -827,23 +827,19 @@ static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
827 * but only emulate it. In all cases, the ports won't work 827 * but only emulate it. In all cases, the ports won't work
828 * unless we send these messages to the hub. 828 * unless we send these messages to the hub.
829 */ 829 */
830 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2) 830 if (hub_is_port_power_switchable(hub))
831 dev_dbg(hub->intfdev, "enabling power on all ports\n"); 831 dev_dbg(hub->intfdev, "enabling power on all ports\n");
832 else 832 else
833 dev_dbg(hub->intfdev, "trying to enable port power on " 833 dev_dbg(hub->intfdev, "trying to enable port power on "
834 "non-switchable hub\n"); 834 "non-switchable hub\n");
835 for (port1 = 1; port1 <= hub->hdev->maxchild; port1++) 835 for (port1 = 1; port1 <= hub->hdev->maxchild; port1++)
836 if (hub->ports[port1 - 1]->power_is_on) 836 if (test_bit(port1, hub->power_bits))
837 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER); 837 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
838 else 838 else
839 usb_clear_port_feature(hub->hdev, port1, 839 usb_clear_port_feature(hub->hdev, port1,
840 USB_PORT_FEAT_POWER); 840 USB_PORT_FEAT_POWER);
841
842 /* Wait at least 100 msec for power to become stable */
843 delay = max(pgood_delay, (unsigned) 100);
844 if (do_delay) 841 if (do_delay)
845 msleep(delay); 842 msleep(hub_power_on_good_delay(hub));
846 return delay;
847} 843}
848 844
849static int hub_hub_status(struct usb_hub *hub, 845static int hub_hub_status(struct usb_hub *hub,
@@ -911,20 +907,20 @@ static int hub_usb3_port_disable(struct usb_hub *hub, int port1)
911 msleep(HUB_DEBOUNCE_STEP); 907 msleep(HUB_DEBOUNCE_STEP);
912 } 908 }
913 if (total_time >= HUB_DEBOUNCE_TIMEOUT) 909 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
914 dev_warn(hub->intfdev, "Could not disable port %d after %d ms\n", 910 dev_warn(&hub->ports[port1 - 1]->dev,
915 port1, total_time); 911 "Could not disable after %d ms\n", total_time);
916 912
917 return hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_RX_DETECT); 913 return hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_RX_DETECT);
918} 914}
919 915
920static int hub_port_disable(struct usb_hub *hub, int port1, int set_state) 916static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
921{ 917{
918 struct usb_port *port_dev = hub->ports[port1 - 1];
922 struct usb_device *hdev = hub->hdev; 919 struct usb_device *hdev = hub->hdev;
923 int ret = 0; 920 int ret = 0;
924 921
925 if (hub->ports[port1 - 1]->child && set_state) 922 if (port_dev->child && set_state)
926 usb_set_device_state(hub->ports[port1 - 1]->child, 923 usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED);
927 USB_STATE_NOTATTACHED);
928 if (!hub->error) { 924 if (!hub->error) {
929 if (hub_is_superspeed(hub->hdev)) 925 if (hub_is_superspeed(hub->hdev))
930 ret = hub_usb3_port_disable(hub, port1); 926 ret = hub_usb3_port_disable(hub, port1);
@@ -933,8 +929,7 @@ static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
933 USB_PORT_FEAT_ENABLE); 929 USB_PORT_FEAT_ENABLE);
934 } 930 }
935 if (ret && ret != -ENODEV) 931 if (ret && ret != -ENODEV)
936 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n", 932 dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret);
937 port1, ret);
938 return ret; 933 return ret;
939} 934}
940 935
@@ -945,7 +940,7 @@ static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
945 */ 940 */
946static void hub_port_logical_disconnect(struct usb_hub *hub, int port1) 941static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
947{ 942{
948 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1); 943 dev_dbg(&hub->ports[port1 - 1]->dev, "logical disconnect\n");
949 hub_port_disable(hub, port1, 1); 944 hub_port_disable(hub, port1, 1);
950 945
951 /* FIXME let caller ask to power down the port: 946 /* FIXME let caller ask to power down the port:
@@ -1048,7 +1043,9 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
1048 * for HUB_POST_RESET, but it's easier not to. 1043 * for HUB_POST_RESET, but it's easier not to.
1049 */ 1044 */
1050 if (type == HUB_INIT) { 1045 if (type == HUB_INIT) {
1051 delay = hub_power_on(hub, false); 1046 unsigned delay = hub_power_on_good_delay(hub);
1047
1048 hub_power_on(hub, false);
1052 INIT_DELAYED_WORK(&hub->init_work, hub_init_func2); 1049 INIT_DELAYED_WORK(&hub->init_work, hub_init_func2);
1053 queue_delayed_work(system_power_efficient_wq, 1050 queue_delayed_work(system_power_efficient_wq,
1054 &hub->init_work, 1051 &hub->init_work,
@@ -1083,21 +1080,23 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
1083 } 1080 }
1084 init2: 1081 init2:
1085 1082
1086 /* Check each port and set hub->change_bits to let khubd know 1083 /*
1084 * Check each port and set hub->change_bits to let khubd know
1087 * which ports need attention. 1085 * which ports need attention.
1088 */ 1086 */
1089 for (port1 = 1; port1 <= hdev->maxchild; ++port1) { 1087 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
1090 struct usb_device *udev = hub->ports[port1 - 1]->child; 1088 struct usb_port *port_dev = hub->ports[port1 - 1];
1089 struct usb_device *udev = port_dev->child;
1091 u16 portstatus, portchange; 1090 u16 portstatus, portchange;
1092 1091
1093 portstatus = portchange = 0; 1092 portstatus = portchange = 0;
1094 status = hub_port_status(hub, port1, &portstatus, &portchange); 1093 status = hub_port_status(hub, port1, &portstatus, &portchange);
1095 if (udev || (portstatus & USB_PORT_STAT_CONNECTION)) 1094 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1096 dev_dbg(hub->intfdev, 1095 dev_dbg(&port_dev->dev, "status %04x change %04x\n",
1097 "port %d: status %04x change %04x\n", 1096 portstatus, portchange);
1098 port1, portstatus, portchange);
1099 1097
1100 /* After anything other than HUB_RESUME (i.e., initialization 1098 /*
1099 * After anything other than HUB_RESUME (i.e., initialization
1101 * or any sort of reset), every port should be disabled. 1100 * or any sort of reset), every port should be disabled.
1102 * Unconnected ports should likewise be disabled (paranoia), 1101 * Unconnected ports should likewise be disabled (paranoia),
1103 * and so should ports for which we have no usb_device. 1102 * and so should ports for which we have no usb_device.
@@ -1173,15 +1172,13 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
1173 set_bit(port1, hub->change_bits); 1172 set_bit(port1, hub->change_bits);
1174 1173
1175 } else if (udev->persist_enabled) { 1174 } else if (udev->persist_enabled) {
1176 struct usb_port *port_dev = hub->ports[port1 - 1];
1177
1178#ifdef CONFIG_PM 1175#ifdef CONFIG_PM
1179 udev->reset_resume = 1; 1176 udev->reset_resume = 1;
1180#endif 1177#endif
1181 /* Don't set the change_bits when the device 1178 /* Don't set the change_bits when the device
1182 * was powered off. 1179 * was powered off.
1183 */ 1180 */
1184 if (port_dev->power_is_on) 1181 if (test_bit(port1, hub->power_bits))
1185 set_bit(port1, hub->change_bits); 1182 set_bit(port1, hub->change_bits);
1186 1183
1187 } else { 1184 } else {
@@ -1276,12 +1273,22 @@ static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1276 flush_work(&hub->tt.clear_work); 1273 flush_work(&hub->tt.clear_work);
1277} 1274}
1278 1275
1276static void hub_pm_barrier_for_all_ports(struct usb_hub *hub)
1277{
1278 int i;
1279
1280 for (i = 0; i < hub->hdev->maxchild; ++i)
1281 pm_runtime_barrier(&hub->ports[i]->dev);
1282}
1283
1279/* caller has locked the hub device */ 1284/* caller has locked the hub device */
1280static int hub_pre_reset(struct usb_interface *intf) 1285static int hub_pre_reset(struct usb_interface *intf)
1281{ 1286{
1282 struct usb_hub *hub = usb_get_intfdata(intf); 1287 struct usb_hub *hub = usb_get_intfdata(intf);
1283 1288
1284 hub_quiesce(hub, HUB_PRE_RESET); 1289 hub_quiesce(hub, HUB_PRE_RESET);
1290 hub->in_reset = 1;
1291 hub_pm_barrier_for_all_ports(hub);
1285 return 0; 1292 return 0;
1286} 1293}
1287 1294
@@ -1290,6 +1297,8 @@ static int hub_post_reset(struct usb_interface *intf)
1290{ 1297{
1291 struct usb_hub *hub = usb_get_intfdata(intf); 1298 struct usb_hub *hub = usb_get_intfdata(intf);
1292 1299
1300 hub->in_reset = 0;
1301 hub_pm_barrier_for_all_ports(hub);
1293 hub_activate(hub, HUB_POST_RESET); 1302 hub_activate(hub, HUB_POST_RESET);
1294 return 0; 1303 return 0;
1295} 1304}
@@ -1307,6 +1316,7 @@ static int hub_configure(struct usb_hub *hub,
1307 char *message = "out of memory"; 1316 char *message = "out of memory";
1308 unsigned unit_load; 1317 unsigned unit_load;
1309 unsigned full_load; 1318 unsigned full_load;
1319 unsigned maxchild;
1310 1320
1311 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL); 1321 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
1312 if (!hub->buffer) { 1322 if (!hub->buffer) {
@@ -1345,12 +1355,11 @@ static int hub_configure(struct usb_hub *hub,
1345 goto fail; 1355 goto fail;
1346 } 1356 }
1347 1357
1348 hdev->maxchild = hub->descriptor->bNbrPorts; 1358 maxchild = hub->descriptor->bNbrPorts;
1349 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild, 1359 dev_info(hub_dev, "%d port%s detected\n", maxchild,
1350 (hdev->maxchild == 1) ? "" : "s"); 1360 (maxchild == 1) ? "" : "s");
1351 1361
1352 hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *), 1362 hub->ports = kzalloc(maxchild * sizeof(struct usb_port *), GFP_KERNEL);
1353 GFP_KERNEL);
1354 if (!hub->ports) { 1363 if (!hub->ports) {
1355 ret = -ENOMEM; 1364 ret = -ENOMEM;
1356 goto fail; 1365 goto fail;
@@ -1371,11 +1380,11 @@ static int hub_configure(struct usb_hub *hub,
1371 int i; 1380 int i;
1372 char portstr[USB_MAXCHILDREN + 1]; 1381 char portstr[USB_MAXCHILDREN + 1];
1373 1382
1374 for (i = 0; i < hdev->maxchild; i++) 1383 for (i = 0; i < maxchild; i++)
1375 portstr[i] = hub->descriptor->u.hs.DeviceRemovable 1384 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
1376 [((i + 1) / 8)] & (1 << ((i + 1) % 8)) 1385 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1377 ? 'F' : 'R'; 1386 ? 'F' : 'R';
1378 portstr[hdev->maxchild] = 0; 1387 portstr[maxchild] = 0;
1379 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr); 1388 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1380 } else 1389 } else
1381 dev_dbg(hub_dev, "standalone hub\n"); 1390 dev_dbg(hub_dev, "standalone hub\n");
@@ -1487,7 +1496,7 @@ static int hub_configure(struct usb_hub *hub,
1487 if (hcd->power_budget > 0) 1496 if (hcd->power_budget > 0)
1488 hdev->bus_mA = hcd->power_budget; 1497 hdev->bus_mA = hcd->power_budget;
1489 else 1498 else
1490 hdev->bus_mA = full_load * hdev->maxchild; 1499 hdev->bus_mA = full_load * maxchild;
1491 if (hdev->bus_mA >= full_load) 1500 if (hdev->bus_mA >= full_load)
1492 hub->mA_per_port = full_load; 1501 hub->mA_per_port = full_load;
1493 else { 1502 else {
@@ -1502,7 +1511,7 @@ static int hub_configure(struct usb_hub *hub,
1502 hub->descriptor->bHubContrCurrent); 1511 hub->descriptor->bHubContrCurrent);
1503 hub->limited_power = 1; 1512 hub->limited_power = 1;
1504 1513
1505 if (remaining < hdev->maxchild * unit_load) 1514 if (remaining < maxchild * unit_load)
1506 dev_warn(hub_dev, 1515 dev_warn(hub_dev,
1507 "insufficient power available " 1516 "insufficient power available "
1508 "to use all downstream ports\n"); 1517 "to use all downstream ports\n");
@@ -1570,15 +1579,19 @@ static int hub_configure(struct usb_hub *hub,
1570 if (hub->has_indicators && blinkenlights) 1579 if (hub->has_indicators && blinkenlights)
1571 hub->indicator[0] = INDICATOR_CYCLE; 1580 hub->indicator[0] = INDICATOR_CYCLE;
1572 1581
1573 for (i = 0; i < hdev->maxchild; i++) { 1582 mutex_lock(&usb_port_peer_mutex);
1583 for (i = 0; i < maxchild; i++) {
1574 ret = usb_hub_create_port_device(hub, i + 1); 1584 ret = usb_hub_create_port_device(hub, i + 1);
1575 if (ret < 0) { 1585 if (ret < 0) {
1576 dev_err(hub->intfdev, 1586 dev_err(hub->intfdev,
1577 "couldn't create port%d device.\n", i + 1); 1587 "couldn't create port%d device.\n", i + 1);
1578 hdev->maxchild = i; 1588 break;
1579 goto fail_keep_maxchild;
1580 } 1589 }
1581 } 1590 }
1591 hdev->maxchild = i;
1592 mutex_unlock(&usb_port_peer_mutex);
1593 if (ret < 0)
1594 goto fail;
1582 1595
1583 usb_hub_adjust_deviceremovable(hdev, hub->descriptor); 1596 usb_hub_adjust_deviceremovable(hdev, hub->descriptor);
1584 1597
@@ -1586,8 +1599,6 @@ static int hub_configure(struct usb_hub *hub,
1586 return 0; 1599 return 0;
1587 1600
1588fail: 1601fail:
1589 hdev->maxchild = 0;
1590fail_keep_maxchild:
1591 dev_err (hub_dev, "config failed, %s (err %d)\n", 1602 dev_err (hub_dev, "config failed, %s (err %d)\n",
1592 message, ret); 1603 message, ret);
1593 /* hub_disconnect() frees urb and descriptor */ 1604 /* hub_disconnect() frees urb and descriptor */
@@ -1623,6 +1634,8 @@ static void hub_disconnect(struct usb_interface *intf)
1623 hub->error = 0; 1634 hub->error = 0;
1624 hub_quiesce(hub, HUB_DISCONNECT); 1635 hub_quiesce(hub, HUB_DISCONNECT);
1625 1636
1637 mutex_lock(&usb_port_peer_mutex);
1638
1626 /* Avoid races with recursively_mark_NOTATTACHED() */ 1639 /* Avoid races with recursively_mark_NOTATTACHED() */
1627 spin_lock_irq(&device_state_lock); 1640 spin_lock_irq(&device_state_lock);
1628 port1 = hdev->maxchild; 1641 port1 = hdev->maxchild;
@@ -1633,6 +1646,8 @@ static void hub_disconnect(struct usb_interface *intf)
1633 for (; port1 > 0; --port1) 1646 for (; port1 > 0; --port1)
1634 usb_hub_remove_port_device(hub, port1); 1647 usb_hub_remove_port_device(hub, port1);
1635 1648
1649 mutex_unlock(&usb_port_peer_mutex);
1650
1636 if (hub->hdev->speed == USB_SPEED_HIGH) 1651 if (hub->hdev->speed == USB_SPEED_HIGH)
1637 highspeed_hubs--; 1652 highspeed_hubs--;
1638 1653
@@ -2035,6 +2050,18 @@ static void hub_free_dev(struct usb_device *udev)
2035 hcd->driver->free_dev(hcd, udev); 2050 hcd->driver->free_dev(hcd, udev);
2036} 2051}
2037 2052
2053static void hub_disconnect_children(struct usb_device *udev)
2054{
2055 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
2056 int i;
2057
2058 /* Free up all the children before we remove this device */
2059 for (i = 0; i < udev->maxchild; i++) {
2060 if (hub->ports[i]->child)
2061 usb_disconnect(&hub->ports[i]->child);
2062 }
2063}
2064
2038/** 2065/**
2039 * usb_disconnect - disconnect a device (usbcore-internal) 2066 * usb_disconnect - disconnect a device (usbcore-internal)
2040 * @pdev: pointer to device being disconnected 2067 * @pdev: pointer to device being disconnected
@@ -2053,9 +2080,10 @@ static void hub_free_dev(struct usb_device *udev)
2053 */ 2080 */
2054void usb_disconnect(struct usb_device **pdev) 2081void usb_disconnect(struct usb_device **pdev)
2055{ 2082{
2056 struct usb_device *udev = *pdev; 2083 struct usb_port *port_dev = NULL;
2057 struct usb_hub *hub = usb_hub_to_struct_hub(udev); 2084 struct usb_device *udev = *pdev;
2058 int i; 2085 struct usb_hub *hub;
2086 int port1;
2059 2087
2060 /* mark the device as inactive, so any further urb submissions for 2088 /* mark the device as inactive, so any further urb submissions for
2061 * this device (and any of its children) will fail immediately. 2089 * this device (and any of its children) will fail immediately.
@@ -2067,11 +2095,7 @@ void usb_disconnect(struct usb_device **pdev)
2067 2095
2068 usb_lock_device(udev); 2096 usb_lock_device(udev);
2069 2097
2070 /* Free up all the children before we remove this device */ 2098 hub_disconnect_children(udev);
2071 for (i = 0; i < udev->maxchild; i++) {
2072 if (hub->ports[i]->child)
2073 usb_disconnect(&hub->ports[i]->child);
2074 }
2075 2099
2076 /* deallocate hcd/hardware state ... nuking all pending urbs and 2100 /* deallocate hcd/hardware state ... nuking all pending urbs and
2077 * cleaning up all state associated with the current configuration 2101 * cleaning up all state associated with the current configuration
@@ -2082,16 +2106,19 @@ void usb_disconnect(struct usb_device **pdev)
2082 usb_hcd_synchronize_unlinks(udev); 2106 usb_hcd_synchronize_unlinks(udev);
2083 2107
2084 if (udev->parent) { 2108 if (udev->parent) {
2085 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 2109 port1 = udev->portnum;
2086 struct usb_port *port_dev = hub->ports[udev->portnum - 1]; 2110 hub = usb_hub_to_struct_hub(udev->parent);
2111 port_dev = hub->ports[port1 - 1];
2087 2112
2088 sysfs_remove_link(&udev->dev.kobj, "port"); 2113 sysfs_remove_link(&udev->dev.kobj, "port");
2089 sysfs_remove_link(&port_dev->dev.kobj, "device"); 2114 sysfs_remove_link(&port_dev->dev.kobj, "device");
2090 2115
2091 if (!port_dev->did_runtime_put) 2116 /*
2092 pm_runtime_put(&port_dev->dev); 2117 * As usb_port_runtime_resume() de-references udev, make
2093 else 2118 * sure no resumes occur during removal
2094 port_dev->did_runtime_put = false; 2119 */
2120 if (!test_and_set_bit(port1, hub->child_usage_bits))
2121 pm_runtime_get_sync(&port_dev->dev);
2095 } 2122 }
2096 2123
2097 usb_remove_ep_devs(&udev->ep0); 2124 usb_remove_ep_devs(&udev->ep0);
@@ -2113,6 +2140,9 @@ void usb_disconnect(struct usb_device **pdev)
2113 *pdev = NULL; 2140 *pdev = NULL;
2114 spin_unlock_irq(&device_state_lock); 2141 spin_unlock_irq(&device_state_lock);
2115 2142
2143 if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits))
2144 pm_runtime_put(&port_dev->dev);
2145
2116 hub_free_dev(udev); 2146 hub_free_dev(udev);
2117 2147
2118 put_device(&udev->dev); 2148 put_device(&udev->dev);
@@ -2300,6 +2330,22 @@ static void set_usb_port_removable(struct usb_device *udev)
2300 udev->removable = USB_DEVICE_REMOVABLE; 2330 udev->removable = USB_DEVICE_REMOVABLE;
2301 else 2331 else
2302 udev->removable = USB_DEVICE_FIXED; 2332 udev->removable = USB_DEVICE_FIXED;
2333
2334 /*
2335 * Platform firmware may have populated an alternative value for
2336 * removable. If the parent port has a known connect_type use
2337 * that instead.
2338 */
2339 switch (hub->ports[udev->portnum - 1]->connect_type) {
2340 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
2341 udev->removable = USB_DEVICE_REMOVABLE;
2342 break;
2343 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
2344 udev->removable = USB_DEVICE_FIXED;
2345 break;
2346 default: /* use what was set above */
2347 break;
2348 }
2303} 2349}
2304 2350
2305/** 2351/**
@@ -2369,11 +2415,7 @@ int usb_new_device(struct usb_device *udev)
2369 2415
2370 device_enable_async_suspend(&udev->dev); 2416 device_enable_async_suspend(&udev->dev);
2371 2417
2372 /* 2418 /* check whether the hub or firmware marks this port as non-removable */
2373 * check whether the hub marks this port as non-removable. Do it
2374 * now so that platform-specific data can override it in
2375 * device_add()
2376 */
2377 if (udev->parent) 2419 if (udev->parent)
2378 set_usb_port_removable(udev); 2420 set_usb_port_removable(udev);
2379 2421
@@ -2390,7 +2432,8 @@ int usb_new_device(struct usb_device *udev)
2390 /* Create link files between child device and usb port device. */ 2432 /* Create link files between child device and usb port device. */
2391 if (udev->parent) { 2433 if (udev->parent) {
2392 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 2434 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
2393 struct usb_port *port_dev = hub->ports[udev->portnum - 1]; 2435 int port1 = udev->portnum;
2436 struct usb_port *port_dev = hub->ports[port1 - 1];
2394 2437
2395 err = sysfs_create_link(&udev->dev.kobj, 2438 err = sysfs_create_link(&udev->dev.kobj,
2396 &port_dev->dev.kobj, "port"); 2439 &port_dev->dev.kobj, "port");
@@ -2404,7 +2447,8 @@ int usb_new_device(struct usb_device *udev)
2404 goto fail; 2447 goto fail;
2405 } 2448 }
2406 2449
2407 pm_runtime_get_sync(&port_dev->dev); 2450 if (!test_and_set_bit(port1, hub->child_usage_bits))
2451 pm_runtime_get_sync(&port_dev->dev);
2408 } 2452 }
2409 2453
2410 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev); 2454 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
@@ -2572,9 +2616,9 @@ static int hub_port_wait_reset(struct usb_hub *hub, int port1,
2572 if (delay_time >= 2 * HUB_SHORT_RESET_TIME) 2616 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2573 delay = HUB_LONG_RESET_TIME; 2617 delay = HUB_LONG_RESET_TIME;
2574 2618
2575 dev_dbg (hub->intfdev, 2619 dev_dbg(&hub->ports[port1 - 1]->dev,
2576 "port %d not %sreset yet, waiting %dms\n", 2620 "not %sreset yet, waiting %dms\n",
2577 port1, warm ? "warm " : "", delay); 2621 warm ? "warm " : "", delay);
2578 } 2622 }
2579 2623
2580 if ((portstatus & USB_PORT_STAT_RESET)) 2624 if ((portstatus & USB_PORT_STAT_RESET))
@@ -2658,6 +2702,7 @@ static int hub_port_reset(struct usb_hub *hub, int port1,
2658{ 2702{
2659 int i, status; 2703 int i, status;
2660 u16 portchange, portstatus; 2704 u16 portchange, portstatus;
2705 struct usb_port *port_dev = hub->ports[port1 - 1];
2661 2706
2662 if (!hub_is_superspeed(hub->hdev)) { 2707 if (!hub_is_superspeed(hub->hdev)) {
2663 if (warm) { 2708 if (warm) {
@@ -2691,9 +2736,9 @@ static int hub_port_reset(struct usb_hub *hub, int port1,
2691 if (status == -ENODEV) { 2736 if (status == -ENODEV) {
2692 ; /* The hub is gone */ 2737 ; /* The hub is gone */
2693 } else if (status) { 2738 } else if (status) {
2694 dev_err(hub->intfdev, 2739 dev_err(&port_dev->dev,
2695 "cannot %sreset port %d (err = %d)\n", 2740 "cannot %sreset (err = %d)\n",
2696 warm ? "warm " : "", port1, status); 2741 warm ? "warm " : "", status);
2697 } else { 2742 } else {
2698 status = hub_port_wait_reset(hub, port1, udev, delay, 2743 status = hub_port_wait_reset(hub, port1, udev, delay,
2699 warm); 2744 warm);
@@ -2726,21 +2771,19 @@ static int hub_port_reset(struct usb_hub *hub, int port1,
2726 * hot or warm reset failed. Try another warm reset. 2771 * hot or warm reset failed. Try another warm reset.
2727 */ 2772 */
2728 if (!warm) { 2773 if (!warm) {
2729 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n", 2774 dev_dbg(&port_dev->dev,
2730 port1); 2775 "hot reset failed, warm reset\n");
2731 warm = true; 2776 warm = true;
2732 } 2777 }
2733 } 2778 }
2734 2779
2735 dev_dbg (hub->intfdev, 2780 dev_dbg(&port_dev->dev,
2736 "port %d not enabled, trying %sreset again...\n", 2781 "not enabled, trying %sreset again...\n",
2737 port1, warm ? "warm " : ""); 2782 warm ? "warm " : "");
2738 delay = HUB_LONG_RESET_TIME; 2783 delay = HUB_LONG_RESET_TIME;
2739 } 2784 }
2740 2785
2741 dev_err (hub->intfdev, 2786 dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n");
2742 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2743 port1);
2744 2787
2745done: 2788done:
2746 if (!hub_is_superspeed(hub->hdev)) 2789 if (!hub_is_superspeed(hub->hdev))
@@ -2765,6 +2808,20 @@ static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2765 return ret; 2808 return ret;
2766} 2809}
2767 2810
2811static void usb_lock_port(struct usb_port *port_dev)
2812 __acquires(&port_dev->status_lock)
2813{
2814 mutex_lock(&port_dev->status_lock);
2815 __acquire(&port_dev->status_lock);
2816}
2817
2818static void usb_unlock_port(struct usb_port *port_dev)
2819 __releases(&port_dev->status_lock)
2820{
2821 mutex_unlock(&port_dev->status_lock);
2822 __release(&port_dev->status_lock);
2823}
2824
2768#ifdef CONFIG_PM 2825#ifdef CONFIG_PM
2769 2826
2770/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */ 2827/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
@@ -2791,6 +2848,8 @@ static int check_port_resume_type(struct usb_device *udev,
2791 struct usb_hub *hub, int port1, 2848 struct usb_hub *hub, int port1,
2792 int status, unsigned portchange, unsigned portstatus) 2849 int status, unsigned portchange, unsigned portstatus)
2793{ 2850{
2851 struct usb_port *port_dev = hub->ports[port1 - 1];
2852
2794 /* Is the device still present? */ 2853 /* Is the device still present? */
2795 if (status || port_is_suspended(hub, portstatus) || 2854 if (status || port_is_suspended(hub, portstatus) ||
2796 !port_is_power_on(hub, portstatus) || 2855 !port_is_power_on(hub, portstatus) ||
@@ -2810,9 +2869,8 @@ static int check_port_resume_type(struct usb_device *udev,
2810 } 2869 }
2811 2870
2812 if (status) { 2871 if (status) {
2813 dev_dbg(hub->intfdev, 2872 dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n",
2814 "port %d status %04x.%04x after resume, %d\n", 2873 portchange, portstatus, status);
2815 port1, portchange, portstatus, status);
2816 } else if (udev->reset_resume) { 2874 } else if (udev->reset_resume) {
2817 2875
2818 /* Late port handoff can set status-change bits */ 2876 /* Late port handoff can set status-change bits */
@@ -2986,6 +3044,8 @@ int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
2986 int status; 3044 int status;
2987 bool really_suspend = true; 3045 bool really_suspend = true;
2988 3046
3047 usb_lock_port(port_dev);
3048
2989 /* enable remote wakeup when appropriate; this lets the device 3049 /* enable remote wakeup when appropriate; this lets the device
2990 * wake up the upstream hub (including maybe the root hub). 3050 * wake up the upstream hub (including maybe the root hub).
2991 * 3051 *
@@ -3043,8 +3103,7 @@ int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
3043 status = 0; 3103 status = 0;
3044 } 3104 }
3045 if (status) { 3105 if (status) {
3046 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n", 3106 dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status);
3047 port1, status);
3048 3107
3049 /* Try to enable USB3 LPM and LTM again */ 3108 /* Try to enable USB3 LPM and LTM again */
3050 usb_unlocked_enable_lpm(udev); 3109 usb_unlocked_enable_lpm(udev);
@@ -3075,12 +3134,13 @@ int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
3075 usb_set_device_state(udev, USB_STATE_SUSPENDED); 3134 usb_set_device_state(udev, USB_STATE_SUSPENDED);
3076 } 3135 }
3077 3136
3078 if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled) { 3137 if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled
3138 && test_and_clear_bit(port1, hub->child_usage_bits))
3079 pm_runtime_put_sync(&port_dev->dev); 3139 pm_runtime_put_sync(&port_dev->dev);
3080 port_dev->did_runtime_put = true;
3081 }
3082 3140
3083 usb_mark_last_busy(hub->hdev); 3141 usb_mark_last_busy(hub->hdev);
3142
3143 usb_unlock_port(port_dev);
3084 return status; 3144 return status;
3085} 3145}
3086 3146
@@ -3220,9 +3280,8 @@ int usb_port_resume(struct usb_device *udev, pm_message_t msg)
3220 int status; 3280 int status;
3221 u16 portchange, portstatus; 3281 u16 portchange, portstatus;
3222 3282
3223 if (port_dev->did_runtime_put) { 3283 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
3224 status = pm_runtime_get_sync(&port_dev->dev); 3284 status = pm_runtime_get_sync(&port_dev->dev);
3225 port_dev->did_runtime_put = false;
3226 if (status < 0) { 3285 if (status < 0) {
3227 dev_dbg(&udev->dev, "can't resume usb port, status %d\n", 3286 dev_dbg(&udev->dev, "can't resume usb port, status %d\n",
3228 status); 3287 status);
@@ -3230,15 +3289,13 @@ int usb_port_resume(struct usb_device *udev, pm_message_t msg)
3230 } 3289 }
3231 } 3290 }
3232 3291
3292 usb_lock_port(port_dev);
3293
3233 /* Skip the initial Clear-Suspend step for a remote wakeup */ 3294 /* Skip the initial Clear-Suspend step for a remote wakeup */
3234 status = hub_port_status(hub, port1, &portstatus, &portchange); 3295 status = hub_port_status(hub, port1, &portstatus, &portchange);
3235 if (status == 0 && !port_is_suspended(hub, portstatus)) 3296 if (status == 0 && !port_is_suspended(hub, portstatus))
3236 goto SuspendCleared; 3297 goto SuspendCleared;
3237 3298
3238 /* dev_dbg(hub->intfdev, "resume port %d\n", port1); */
3239
3240 set_bit(port1, hub->busy_bits);
3241
3242 /* see 7.1.7.7; affects power usage, but not budgeting */ 3299 /* see 7.1.7.7; affects power usage, but not budgeting */
3243 if (hub_is_superspeed(hub->hdev)) 3300 if (hub_is_superspeed(hub->hdev))
3244 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0); 3301 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
@@ -3246,8 +3303,7 @@ int usb_port_resume(struct usb_device *udev, pm_message_t msg)
3246 status = usb_clear_port_feature(hub->hdev, 3303 status = usb_clear_port_feature(hub->hdev,
3247 port1, USB_PORT_FEAT_SUSPEND); 3304 port1, USB_PORT_FEAT_SUSPEND);
3248 if (status) { 3305 if (status) {
3249 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n", 3306 dev_dbg(&port_dev->dev, "can't resume, status %d\n", status);
3250 port1, status);
3251 } else { 3307 } else {
3252 /* drive resume for at least 20 msec */ 3308 /* drive resume for at least 20 msec */
3253 dev_dbg(&udev->dev, "usb %sresume\n", 3309 dev_dbg(&udev->dev, "usb %sresume\n",
@@ -3278,8 +3334,6 @@ int usb_port_resume(struct usb_device *udev, pm_message_t msg)
3278 } 3334 }
3279 } 3335 }
3280 3336
3281 clear_bit(port1, hub->busy_bits);
3282
3283 status = check_port_resume_type(udev, 3337 status = check_port_resume_type(udev,
3284 hub, port1, status, portchange, portstatus); 3338 hub, port1, status, portchange, portstatus);
3285 if (status == 0) 3339 if (status == 0)
@@ -3297,16 +3351,18 @@ int usb_port_resume(struct usb_device *udev, pm_message_t msg)
3297 usb_unlocked_enable_lpm(udev); 3351 usb_unlocked_enable_lpm(udev);
3298 } 3352 }
3299 3353
3354 usb_unlock_port(port_dev);
3355
3300 return status; 3356 return status;
3301} 3357}
3302 3358
3303#ifdef CONFIG_PM_RUNTIME 3359#ifdef CONFIG_PM_RUNTIME
3304 3360
3305/* caller has locked udev */
3306int usb_remote_wakeup(struct usb_device *udev) 3361int usb_remote_wakeup(struct usb_device *udev)
3307{ 3362{
3308 int status = 0; 3363 int status = 0;
3309 3364
3365 usb_lock_device(udev);
3310 if (udev->state == USB_STATE_SUSPENDED) { 3366 if (udev->state == USB_STATE_SUSPENDED) {
3311 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); 3367 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
3312 status = usb_autoresume_device(udev); 3368 status = usb_autoresume_device(udev);
@@ -3315,9 +3371,59 @@ int usb_remote_wakeup(struct usb_device *udev)
3315 usb_autosuspend_device(udev); 3371 usb_autosuspend_device(udev);
3316 } 3372 }
3317 } 3373 }
3374 usb_unlock_device(udev);
3318 return status; 3375 return status;
3319} 3376}
3320 3377
3378/* Returns 1 if there was a remote wakeup and a connect status change. */
3379static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
3380 u16 portstatus, u16 portchange)
3381 __must_hold(&port_dev->status_lock)
3382{
3383 struct usb_port *port_dev = hub->ports[port - 1];
3384 struct usb_device *hdev;
3385 struct usb_device *udev;
3386 int connect_change = 0;
3387 int ret;
3388
3389 hdev = hub->hdev;
3390 udev = port_dev->child;
3391 if (!hub_is_superspeed(hdev)) {
3392 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
3393 return 0;
3394 usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
3395 } else {
3396 if (!udev || udev->state != USB_STATE_SUSPENDED ||
3397 (portstatus & USB_PORT_STAT_LINK_STATE) !=
3398 USB_SS_PORT_LS_U0)
3399 return 0;
3400 }
3401
3402 if (udev) {
3403 /* TRSMRCY = 10 msec */
3404 msleep(10);
3405
3406 usb_unlock_port(port_dev);
3407 ret = usb_remote_wakeup(udev);
3408 usb_lock_port(port_dev);
3409 if (ret < 0)
3410 connect_change = 1;
3411 } else {
3412 ret = -ENODEV;
3413 hub_port_disable(hub, port, 1);
3414 }
3415 dev_dbg(&port_dev->dev, "resume, status %d\n", ret);
3416 return connect_change;
3417}
3418
3419#else
3420
3421static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
3422 u16 portstatus, u16 portchange)
3423{
3424 return 0;
3425}
3426
3321#endif 3427#endif
3322 3428
3323static int check_ports_changed(struct usb_hub *hub) 3429static int check_ports_changed(struct usb_hub *hub)
@@ -3348,12 +3454,11 @@ static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
3348 */ 3454 */
3349 hub->wakeup_enabled_descendants = 0; 3455 hub->wakeup_enabled_descendants = 0;
3350 for (port1 = 1; port1 <= hdev->maxchild; port1++) { 3456 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3351 struct usb_device *udev; 3457 struct usb_port *port_dev = hub->ports[port1 - 1];
3458 struct usb_device *udev = port_dev->child;
3352 3459
3353 udev = hub->ports[port1 - 1]->child;
3354 if (udev && udev->can_submit) { 3460 if (udev && udev->can_submit) {
3355 dev_warn(&intf->dev, "port %d not suspended yet\n", 3461 dev_warn(&port_dev->dev, "not suspended yet\n");
3356 port1);
3357 if (PMSG_IS_AUTO(msg)) 3462 if (PMSG_IS_AUTO(msg))
3358 return -EBUSY; 3463 return -EBUSY;
3359 } 3464 }
@@ -3872,6 +3977,12 @@ EXPORT_SYMBOL_GPL(usb_disable_ltm);
3872void usb_enable_ltm(struct usb_device *udev) { } 3977void usb_enable_ltm(struct usb_device *udev) { }
3873EXPORT_SYMBOL_GPL(usb_enable_ltm); 3978EXPORT_SYMBOL_GPL(usb_enable_ltm);
3874 3979
3980static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
3981 u16 portstatus, u16 portchange)
3982{
3983 return 0;
3984}
3985
3875#endif /* CONFIG_PM */ 3986#endif /* CONFIG_PM */
3876 3987
3877 3988
@@ -3893,9 +4004,10 @@ EXPORT_SYMBOL_GPL(usb_enable_ltm);
3893int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected) 4004int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected)
3894{ 4005{
3895 int ret; 4006 int ret;
3896 int total_time, stable_time = 0;
3897 u16 portchange, portstatus; 4007 u16 portchange, portstatus;
3898 unsigned connection = 0xffff; 4008 unsigned connection = 0xffff;
4009 int total_time, stable_time = 0;
4010 struct usb_port *port_dev = hub->ports[port1 - 1];
3899 4011
3900 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) { 4012 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3901 ret = hub_port_status(hub, port1, &portstatus, &portchange); 4013 ret = hub_port_status(hub, port1, &portstatus, &portchange);
@@ -3924,9 +4036,8 @@ int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected)
3924 msleep(HUB_DEBOUNCE_STEP); 4036 msleep(HUB_DEBOUNCE_STEP);
3925 } 4037 }
3926 4038
3927 dev_dbg (hub->intfdev, 4039 dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n",
3928 "debounce: port %d: total %dms stable %dms status 0x%x\n", 4040 total_time, stable_time, portstatus);
3929 port1, total_time, stable_time, portstatus);
3930 4041
3931 if (stable_time < HUB_DEBOUNCE_STABLE) 4042 if (stable_time < HUB_DEBOUNCE_STABLE)
3932 return -ETIMEDOUT; 4043 return -ETIMEDOUT;
@@ -3985,13 +4096,14 @@ static int hub_set_address(struct usb_device *udev, int devnum)
3985 */ 4096 */
3986static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev) 4097static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev)
3987{ 4098{
3988 int connect_type; 4099 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
4100 int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN;
3989 4101
3990 if (!udev->usb2_hw_lpm_capable) 4102 if (!udev->usb2_hw_lpm_capable)
3991 return; 4103 return;
3992 4104
3993 connect_type = usb_get_hub_port_connect_type(udev->parent, 4105 if (hub)
3994 udev->portnum); 4106 connect_type = hub->ports[udev->portnum - 1]->connect_type;
3995 4107
3996 if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) || 4108 if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) ||
3997 connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 4109 connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
@@ -4019,16 +4131,15 @@ static int hub_enable_device(struct usb_device *udev)
4019 * Returns device in USB_STATE_ADDRESS, except on error. 4131 * Returns device in USB_STATE_ADDRESS, except on error.
4020 * 4132 *
4021 * If this is called for an already-existing device (as part of 4133 * If this is called for an already-existing device (as part of
4022 * usb_reset_and_verify_device), the caller must own the device lock. For a 4134 * usb_reset_and_verify_device), the caller must own the device lock and
4023 * newly detected device that is not accessible through any global 4135 * the port lock. For a newly detected device that is not accessible
4024 * pointers, it's not necessary to lock the device. 4136 * through any global pointers, it's not necessary to lock the device,
4137 * but it is still necessary to lock the port.
4025 */ 4138 */
4026static int 4139static int
4027hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1, 4140hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
4028 int retry_counter) 4141 int retry_counter)
4029{ 4142{
4030 static DEFINE_MUTEX(usb_address0_mutex);
4031
4032 struct usb_device *hdev = hub->hdev; 4143 struct usb_device *hdev = hub->hdev;
4033 struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 4144 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
4034 int i, j, retval; 4145 int i, j, retval;
@@ -4051,7 +4162,7 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
4051 if (oldspeed == USB_SPEED_LOW) 4162 if (oldspeed == USB_SPEED_LOW)
4052 delay = HUB_LONG_RESET_TIME; 4163 delay = HUB_LONG_RESET_TIME;
4053 4164
4054 mutex_lock(&usb_address0_mutex); 4165 mutex_lock(&hdev->bus->usb_address0_mutex);
4055 4166
4056 /* Reset the device; full speed may morph to high speed */ 4167 /* Reset the device; full speed may morph to high speed */
4057 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */ 4168 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
@@ -4328,7 +4439,7 @@ fail:
4328 hub_port_disable(hub, port1, 0); 4439 hub_port_disable(hub, port1, 0);
4329 update_devnum(udev, devnum); /* for disconnect processing */ 4440 update_devnum(udev, devnum); /* for disconnect processing */
4330 } 4441 }
4331 mutex_unlock(&usb_address0_mutex); 4442 mutex_unlock(&hdev->bus->usb_address0_mutex);
4332 return retval; 4443 return retval;
4333} 4444}
4334 4445
@@ -4369,9 +4480,10 @@ hub_power_remaining (struct usb_hub *hub)
4369 4480
4370 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent; 4481 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4371 for (port1 = 1; port1 <= hdev->maxchild; ++port1) { 4482 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
4372 struct usb_device *udev = hub->ports[port1 - 1]->child; 4483 struct usb_port *port_dev = hub->ports[port1 - 1];
4373 int delta; 4484 struct usb_device *udev = port_dev->child;
4374 unsigned unit_load; 4485 unsigned unit_load;
4486 int delta;
4375 4487
4376 if (!udev) 4488 if (!udev)
4377 continue; 4489 continue;
@@ -4391,9 +4503,8 @@ hub_power_remaining (struct usb_hub *hub)
4391 else 4503 else
4392 delta = 8; 4504 delta = 8;
4393 if (delta > hub->mA_per_port) 4505 if (delta > hub->mA_per_port)
4394 dev_warn(&udev->dev, 4506 dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n",
4395 "%dmA is over %umA budget for port %d!\n", 4507 delta, hub->mA_per_port);
4396 delta, hub->mA_per_port, port1);
4397 remaining -= delta; 4508 remaining -= delta;
4398 } 4509 }
4399 if (remaining < 0) { 4510 if (remaining < 0) {
@@ -4404,78 +4515,23 @@ hub_power_remaining (struct usb_hub *hub)
4404 return remaining; 4515 return remaining;
4405} 4516}
4406 4517
4407/* Handle physical or logical connection change events. 4518static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
4408 * This routine is called when: 4519 u16 portchange)
4409 * a port connection-change occurs;
4410 * a port enable-change occurs (often caused by EMI);
4411 * usb_reset_and_verify_device() encounters changed descriptors (as from
4412 * a firmware download)
4413 * caller already locked the hub
4414 */
4415static void hub_port_connect_change(struct usb_hub *hub, int port1,
4416 u16 portstatus, u16 portchange)
4417{ 4520{
4418 struct usb_device *hdev = hub->hdev;
4419 struct device *hub_dev = hub->intfdev;
4420 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
4421 unsigned wHubCharacteristics =
4422 le16_to_cpu(hub->descriptor->wHubCharacteristics);
4423 struct usb_device *udev;
4424 int status, i; 4521 int status, i;
4425 unsigned unit_load; 4522 unsigned unit_load;
4426 4523 struct usb_device *hdev = hub->hdev;
4427 dev_dbg (hub_dev, 4524 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
4428 "port %d, status %04x, change %04x, %s\n", 4525 struct usb_port *port_dev = hub->ports[port1 - 1];
4429 port1, portstatus, portchange, portspeed(hub, portstatus)); 4526 struct usb_device *udev = port_dev->child;
4430
4431 if (hub->has_indicators) {
4432 set_port_led(hub, port1, HUB_LED_AUTO);
4433 hub->indicator[port1-1] = INDICATOR_AUTO;
4434 }
4435
4436#ifdef CONFIG_USB_OTG
4437 /* during HNP, don't repeat the debounce */
4438 if (hdev->bus->is_b_host)
4439 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4440 USB_PORT_STAT_C_ENABLE);
4441#endif
4442
4443 /* Try to resuscitate an existing device */
4444 udev = hub->ports[port1 - 1]->child;
4445 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4446 udev->state != USB_STATE_NOTATTACHED) {
4447 usb_lock_device(udev);
4448 if (portstatus & USB_PORT_STAT_ENABLE) {
4449 status = 0; /* Nothing to do */
4450
4451#ifdef CONFIG_PM_RUNTIME
4452 } else if (udev->state == USB_STATE_SUSPENDED &&
4453 udev->persist_enabled) {
4454 /* For a suspended device, treat this as a
4455 * remote wakeup event.
4456 */
4457 status = usb_remote_wakeup(udev);
4458#endif
4459
4460 } else {
4461 status = -ENODEV; /* Don't resuscitate */
4462 }
4463 usb_unlock_device(udev);
4464
4465 if (status == 0) {
4466 clear_bit(port1, hub->change_bits);
4467 return;
4468 }
4469 }
4470 4527
4471 /* Disconnect any existing devices under this port */ 4528 /* Disconnect any existing devices under this port */
4472 if (udev) { 4529 if (udev) {
4473 if (hcd->phy && !hdev->parent && 4530 if (hcd->phy && !hdev->parent &&
4474 !(portstatus & USB_PORT_STAT_CONNECTION)) 4531 !(portstatus & USB_PORT_STAT_CONNECTION))
4475 usb_phy_notify_disconnect(hcd->phy, udev->speed); 4532 usb_phy_notify_disconnect(hcd->phy, udev->speed);
4476 usb_disconnect(&hub->ports[port1 - 1]->child); 4533 usb_disconnect(&port_dev->child);
4477 } 4534 }
4478 clear_bit(port1, hub->change_bits);
4479 4535
4480 /* We can forget about a "removed" device when there's a physical 4536 /* We can forget about a "removed" device when there's a physical
4481 * disconnect or the connect status changes. 4537 * disconnect or the connect status changes.
@@ -4489,8 +4545,8 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
4489 status = hub_port_debounce_be_stable(hub, port1); 4545 status = hub_port_debounce_be_stable(hub, port1);
4490 if (status < 0) { 4546 if (status < 0) {
4491 if (status != -ENODEV && printk_ratelimit()) 4547 if (status != -ENODEV && printk_ratelimit())
4492 dev_err(hub_dev, "connect-debounce failed, " 4548 dev_err(&port_dev->dev,
4493 "port %d disabled\n", port1); 4549 "connect-debounce failed\n");
4494 portstatus &= ~USB_PORT_STAT_CONNECTION; 4550 portstatus &= ~USB_PORT_STAT_CONNECTION;
4495 } else { 4551 } else {
4496 portstatus = status; 4552 portstatus = status;
@@ -4504,7 +4560,7 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
4504 test_bit(port1, hub->removed_bits)) { 4560 test_bit(port1, hub->removed_bits)) {
4505 4561
4506 /* maybe switch power back on (e.g. root hub was reset) */ 4562 /* maybe switch power back on (e.g. root hub was reset) */
4507 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2 4563 if (hub_is_port_power_switchable(hub)
4508 && !port_is_power_on(hub, portstatus)) 4564 && !port_is_power_on(hub, portstatus))
4509 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 4565 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
4510 4566
@@ -4525,9 +4581,8 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
4525 */ 4581 */
4526 udev = usb_alloc_dev(hdev, hdev->bus, port1); 4582 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4527 if (!udev) { 4583 if (!udev) {
4528 dev_err (hub_dev, 4584 dev_err(&port_dev->dev,
4529 "couldn't allocate port %d usb_device\n", 4585 "couldn't allocate usb_device\n");
4530 port1);
4531 goto done; 4586 goto done;
4532 } 4587 }
4533 4588
@@ -4549,7 +4604,9 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
4549 } 4604 }
4550 4605
4551 /* reset (non-USB 3.0 devices) and get descriptor */ 4606 /* reset (non-USB 3.0 devices) and get descriptor */
4607 usb_lock_port(port_dev);
4552 status = hub_port_init(hub, udev, port1, i); 4608 status = hub_port_init(hub, udev, port1, i);
4609 usb_unlock_port(port_dev);
4553 if (status < 0) 4610 if (status < 0)
4554 goto loop; 4611 goto loop;
4555 4612
@@ -4601,6 +4658,8 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
4601 */ 4658 */
4602 status = 0; 4659 status = 0;
4603 4660
4661 mutex_lock(&usb_port_peer_mutex);
4662
4604 /* We mustn't add new devices if the parent hub has 4663 /* We mustn't add new devices if the parent hub has
4605 * been disconnected; we would race with the 4664 * been disconnected; we would race with the
4606 * recursively_mark_NOTATTACHED() routine. 4665 * recursively_mark_NOTATTACHED() routine.
@@ -4609,16 +4668,19 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
4609 if (hdev->state == USB_STATE_NOTATTACHED) 4668 if (hdev->state == USB_STATE_NOTATTACHED)
4610 status = -ENOTCONN; 4669 status = -ENOTCONN;
4611 else 4670 else
4612 hub->ports[port1 - 1]->child = udev; 4671 port_dev->child = udev;
4613 spin_unlock_irq(&device_state_lock); 4672 spin_unlock_irq(&device_state_lock);
4673 mutex_unlock(&usb_port_peer_mutex);
4614 4674
4615 /* Run it through the hoops (find a driver, etc) */ 4675 /* Run it through the hoops (find a driver, etc) */
4616 if (!status) { 4676 if (!status) {
4617 status = usb_new_device(udev); 4677 status = usb_new_device(udev);
4618 if (status) { 4678 if (status) {
4679 mutex_lock(&usb_port_peer_mutex);
4619 spin_lock_irq(&device_state_lock); 4680 spin_lock_irq(&device_state_lock);
4620 hub->ports[port1 - 1]->child = NULL; 4681 port_dev->child = NULL;
4621 spin_unlock_irq(&device_state_lock); 4682 spin_unlock_irq(&device_state_lock);
4683 mutex_unlock(&usb_port_peer_mutex);
4622 } 4684 }
4623 } 4685 }
4624 4686
@@ -4627,7 +4689,7 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
4627 4689
4628 status = hub_power_remaining(hub); 4690 status = hub_power_remaining(hub);
4629 if (status) 4691 if (status)
4630 dev_dbg(hub_dev, "%dmA power budget left\n", status); 4692 dev_dbg(hub->intfdev, "%dmA power budget left\n", status);
4631 4693
4632 return; 4694 return;
4633 4695
@@ -4645,56 +4707,200 @@ loop:
4645 !hcd->driver->port_handed_over || 4707 !hcd->driver->port_handed_over ||
4646 !(hcd->driver->port_handed_over)(hcd, port1)) { 4708 !(hcd->driver->port_handed_over)(hcd, port1)) {
4647 if (status != -ENOTCONN && status != -ENODEV) 4709 if (status != -ENOTCONN && status != -ENODEV)
4648 dev_err(hub_dev, "unable to enumerate USB device on port %d\n", 4710 dev_err(&port_dev->dev,
4649 port1); 4711 "unable to enumerate USB device\n");
4650 } 4712 }
4651 4713
4652done: 4714done:
4653 hub_port_disable(hub, port1, 1); 4715 hub_port_disable(hub, port1, 1);
4654 if (hcd->driver->relinquish_port && !hub->hdev->parent) 4716 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4655 hcd->driver->relinquish_port(hcd, port1); 4717 hcd->driver->relinquish_port(hcd, port1);
4718
4656} 4719}
4657 4720
4658/* Returns 1 if there was a remote wakeup and a connect status change. */ 4721/* Handle physical or logical connection change events.
4659static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, 4722 * This routine is called when:
4660 u16 portstatus, u16 portchange) 4723 * a port connection-change occurs;
4724 * a port enable-change occurs (often caused by EMI);
4725 * usb_reset_and_verify_device() encounters changed descriptors (as from
4726 * a firmware download)
4727 * caller already locked the hub
4728 */
4729static void hub_port_connect_change(struct usb_hub *hub, int port1,
4730 u16 portstatus, u16 portchange)
4731 __must_hold(&port_dev->status_lock)
4661{ 4732{
4662 struct usb_device *hdev; 4733 struct usb_port *port_dev = hub->ports[port1 - 1];
4663 struct usb_device *udev; 4734 struct usb_device *udev = port_dev->child;
4664 int connect_change = 0; 4735 int status = -ENODEV;
4665 int ret;
4666 4736
4667 hdev = hub->hdev; 4737 dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus,
4668 udev = hub->ports[port - 1]->child; 4738 portchange, portspeed(hub, portstatus));
4669 if (!hub_is_superspeed(hdev)) { 4739
4670 if (!(portchange & USB_PORT_STAT_C_SUSPEND)) 4740 if (hub->has_indicators) {
4671 return 0; 4741 set_port_led(hub, port1, HUB_LED_AUTO);
4672 usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND); 4742 hub->indicator[port1-1] = INDICATOR_AUTO;
4673 } else {
4674 if (!udev || udev->state != USB_STATE_SUSPENDED ||
4675 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4676 USB_SS_PORT_LS_U0)
4677 return 0;
4678 } 4743 }
4679 4744
4680 if (udev) { 4745#ifdef CONFIG_USB_OTG
4681 /* TRSMRCY = 10 msec */ 4746 /* during HNP, don't repeat the debounce */
4682 msleep(10); 4747 if (hub->hdev->bus->is_b_host)
4748 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4749 USB_PORT_STAT_C_ENABLE);
4750#endif
4751
4752 /* Try to resuscitate an existing device */
4753 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4754 udev->state != USB_STATE_NOTATTACHED) {
4755 if (portstatus & USB_PORT_STAT_ENABLE) {
4756 status = 0; /* Nothing to do */
4757#ifdef CONFIG_PM_RUNTIME
4758 } else if (udev->state == USB_STATE_SUSPENDED &&
4759 udev->persist_enabled) {
4760 /* For a suspended device, treat this as a
4761 * remote wakeup event.
4762 */
4763 usb_unlock_port(port_dev);
4764 status = usb_remote_wakeup(udev);
4765 usb_lock_port(port_dev);
4766#endif
4767 } else {
4768 /* Don't resuscitate */;
4769 }
4770 }
4771 clear_bit(port1, hub->change_bits);
4772
4773 /* successfully revalidated the connection */
4774 if (status == 0)
4775 return;
4776
4777 usb_unlock_port(port_dev);
4778 hub_port_connect(hub, port1, portstatus, portchange);
4779 usb_lock_port(port_dev);
4780}
4781
4782static void port_event(struct usb_hub *hub, int port1)
4783 __must_hold(&port_dev->status_lock)
4784{
4785 int connect_change, reset_device = 0;
4786 struct usb_port *port_dev = hub->ports[port1 - 1];
4787 struct usb_device *udev = port_dev->child;
4788 struct usb_device *hdev = hub->hdev;
4789 u16 portstatus, portchange;
4790
4791 connect_change = test_bit(port1, hub->change_bits);
4792 clear_bit(port1, hub->event_bits);
4793 clear_bit(port1, hub->wakeup_bits);
4794
4795 if (hub_port_status(hub, port1, &portstatus, &portchange) < 0)
4796 return;
4797
4798 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4799 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
4800 connect_change = 1;
4801 }
4802
4803 if (portchange & USB_PORT_STAT_C_ENABLE) {
4804 if (!connect_change)
4805 dev_dbg(&port_dev->dev, "enable change, status %08x\n",
4806 portstatus);
4807 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
4808
4809 /*
4810 * EM interference sometimes causes badly shielded USB devices
4811 * to be shutdown by the hub, this hack enables them again.
4812 * Works at least with mouse driver.
4813 */
4814 if (!(portstatus & USB_PORT_STAT_ENABLE)
4815 && !connect_change && udev) {
4816 dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n");
4817 connect_change = 1;
4818 }
4819 }
4820
4821 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
4822 u16 status = 0, unused;
4823
4824 dev_dbg(&port_dev->dev, "over-current change\n");
4825 usb_clear_port_feature(hdev, port1,
4826 USB_PORT_FEAT_C_OVER_CURRENT);
4827 msleep(100); /* Cool down */
4828 hub_power_on(hub, true);
4829 hub_port_status(hub, port1, &status, &unused);
4830 if (status & USB_PORT_STAT_OVERCURRENT)
4831 dev_err(&port_dev->dev, "over-current condition\n");
4832 }
4833
4834 if (portchange & USB_PORT_STAT_C_RESET) {
4835 dev_dbg(&port_dev->dev, "reset change\n");
4836 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET);
4837 }
4838 if ((portchange & USB_PORT_STAT_C_BH_RESET)
4839 && hub_is_superspeed(hdev)) {
4840 dev_dbg(&port_dev->dev, "warm reset change\n");
4841 usb_clear_port_feature(hdev, port1,
4842 USB_PORT_FEAT_C_BH_PORT_RESET);
4843 }
4844 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4845 dev_dbg(&port_dev->dev, "link state change\n");
4846 usb_clear_port_feature(hdev, port1,
4847 USB_PORT_FEAT_C_PORT_LINK_STATE);
4848 }
4849 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4850 dev_warn(&port_dev->dev, "config error\n");
4851 usb_clear_port_feature(hdev, port1,
4852 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4853 }
4854
4855 /* skip port actions that require the port to be powered on */
4856 if (!pm_runtime_active(&port_dev->dev))
4857 return;
4683 4858
4859 if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange))
4860 connect_change = 1;
4861
4862 /*
4863 * Warm reset a USB3 protocol port if it's in
4864 * SS.Inactive state.
4865 */
4866 if (hub_port_warm_reset_required(hub, portstatus)) {
4867 dev_dbg(&port_dev->dev, "do warm reset\n");
4868 if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION)
4869 || udev->state == USB_STATE_NOTATTACHED) {
4870 if (hub_port_reset(hub, port1, NULL,
4871 HUB_BH_RESET_TIME, true) < 0)
4872 hub_port_disable(hub, port1, 1);
4873 } else
4874 reset_device = 1;
4875 }
4876
4877 /*
4878 * On disconnect USB3 protocol ports transit from U0 to
4879 * SS.Inactive to Rx.Detect. If this happens a warm-
4880 * reset is not needed, but a (re)connect may happen
4881 * before khubd runs and sees the disconnect, and the
4882 * device may be an unknown state.
4883 *
4884 * If the port went through SS.Inactive without khubd
4885 * seeing it the C_LINK_STATE change flag will be set,
4886 * and we reset the dev to put it in a known state.
4887 */
4888 if (reset_device || (udev && hub_is_superspeed(hub->hdev)
4889 && (portchange & USB_PORT_STAT_C_LINK_STATE)
4890 && (portstatus & USB_PORT_STAT_CONNECTION))) {
4891 usb_unlock_port(port_dev);
4684 usb_lock_device(udev); 4892 usb_lock_device(udev);
4685 ret = usb_remote_wakeup(udev); 4893 usb_reset_device(udev);
4686 usb_unlock_device(udev); 4894 usb_unlock_device(udev);
4687 if (ret < 0) 4895 usb_lock_port(port_dev);
4688 connect_change = 1; 4896 connect_change = 0;
4689 } else {
4690 ret = -ENODEV;
4691 hub_port_disable(hub, port, 1);
4692 } 4897 }
4693 dev_dbg(hub->intfdev, "resume on port %d, status %d\n", 4898
4694 port, ret); 4899 if (connect_change)
4695 return connect_change; 4900 hub_port_connect_change(hub, port1, portstatus, portchange);
4696} 4901}
4697 4902
4903
4698static void hub_events(void) 4904static void hub_events(void)
4699{ 4905{
4700 struct list_head *tmp; 4906 struct list_head *tmp;
@@ -4704,10 +4910,7 @@ static void hub_events(void)
4704 struct device *hub_dev; 4910 struct device *hub_dev;
4705 u16 hubstatus; 4911 u16 hubstatus;
4706 u16 hubchange; 4912 u16 hubchange;
4707 u16 portstatus;
4708 u16 portchange;
4709 int i, ret; 4913 int i, ret;
4710 int connect_change, wakeup_change;
4711 4914
4712 /* 4915 /*
4713 * We restart the list every time to avoid a deadlock with 4916 * We restart the list every time to avoid a deadlock with
@@ -4781,146 +4984,28 @@ static void hub_events(void)
4781 4984
4782 /* deal with port status changes */ 4985 /* deal with port status changes */
4783 for (i = 1; i <= hdev->maxchild; i++) { 4986 for (i = 1; i <= hdev->maxchild; i++) {
4784 struct usb_device *udev = hub->ports[i - 1]->child; 4987 struct usb_port *port_dev = hub->ports[i - 1];
4785
4786 if (test_bit(i, hub->busy_bits))
4787 continue;
4788 connect_change = test_bit(i, hub->change_bits);
4789 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
4790 if (!test_and_clear_bit(i, hub->event_bits) &&
4791 !connect_change && !wakeup_change)
4792 continue;
4793
4794 ret = hub_port_status(hub, i,
4795 &portstatus, &portchange);
4796 if (ret < 0)
4797 continue;
4798
4799 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4800 usb_clear_port_feature(hdev, i,
4801 USB_PORT_FEAT_C_CONNECTION);
4802 connect_change = 1;
4803 }
4804
4805 if (portchange & USB_PORT_STAT_C_ENABLE) {
4806 if (!connect_change)
4807 dev_dbg (hub_dev,
4808 "port %d enable change, "
4809 "status %08x\n",
4810 i, portstatus);
4811 usb_clear_port_feature(hdev, i,
4812 USB_PORT_FEAT_C_ENABLE);
4813 4988
4989 if (test_bit(i, hub->event_bits)
4990 || test_bit(i, hub->change_bits)
4991 || test_bit(i, hub->wakeup_bits)) {
4814 /* 4992 /*
4815 * EM interference sometimes causes badly 4993 * The get_noresume and barrier ensure that if
4816 * shielded USB devices to be shutdown by 4994 * the port was in the process of resuming, we
4817 * the hub, this hack enables them again. 4995 * flush that work and keep the port active for
4818 * Works at least with mouse driver. 4996 * the duration of the port_event(). However,
4997 * if the port is runtime pm suspended
4998 * (powered-off), we leave it in that state, run
4999 * an abbreviated port_event(), and move on.
4819 */ 5000 */
4820 if (!(portstatus & USB_PORT_STAT_ENABLE) 5001 pm_runtime_get_noresume(&port_dev->dev);
4821 && !connect_change 5002 pm_runtime_barrier(&port_dev->dev);
4822 && hub->ports[i - 1]->child) { 5003 usb_lock_port(port_dev);
4823 dev_err (hub_dev, 5004 port_event(hub, i);
4824 "port %i " 5005 usb_unlock_port(port_dev);
4825 "disabled by hub (EMI?), " 5006 pm_runtime_put_sync(&port_dev->dev);
4826 "re-enabling...\n",
4827 i);
4828 connect_change = 1;
4829 }
4830 }
4831
4832 if (hub_handle_remote_wakeup(hub, i,
4833 portstatus, portchange))
4834 connect_change = 1;
4835
4836 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
4837 u16 status = 0;
4838 u16 unused;
4839
4840 dev_dbg(hub_dev, "over-current change on port "
4841 "%d\n", i);
4842 usb_clear_port_feature(hdev, i,
4843 USB_PORT_FEAT_C_OVER_CURRENT);
4844 msleep(100); /* Cool down */
4845 hub_power_on(hub, true);
4846 hub_port_status(hub, i, &status, &unused);
4847 if (status & USB_PORT_STAT_OVERCURRENT)
4848 dev_err(hub_dev, "over-current "
4849 "condition on port %d\n", i);
4850 }
4851
4852 if (portchange & USB_PORT_STAT_C_RESET) {
4853 dev_dbg (hub_dev,
4854 "reset change on port %d\n",
4855 i);
4856 usb_clear_port_feature(hdev, i,
4857 USB_PORT_FEAT_C_RESET);
4858 }
4859 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4860 hub_is_superspeed(hub->hdev)) {
4861 dev_dbg(hub_dev,
4862 "warm reset change on port %d\n",
4863 i);
4864 usb_clear_port_feature(hdev, i,
4865 USB_PORT_FEAT_C_BH_PORT_RESET);
4866 }
4867 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4868 usb_clear_port_feature(hub->hdev, i,
4869 USB_PORT_FEAT_C_PORT_LINK_STATE);
4870 }
4871 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4872 dev_warn(hub_dev,
4873 "config error on port %d\n",
4874 i);
4875 usb_clear_port_feature(hub->hdev, i,
4876 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4877 } 5007 }
4878 5008 }
4879 /* Warm reset a USB3 protocol port if it's in
4880 * SS.Inactive state.
4881 */
4882 if (hub_port_warm_reset_required(hub, portstatus)) {
4883 int status;
4884
4885 dev_dbg(hub_dev, "warm reset port %d\n", i);
4886 if (!udev ||
4887 !(portstatus & USB_PORT_STAT_CONNECTION) ||
4888 udev->state == USB_STATE_NOTATTACHED) {
4889 status = hub_port_reset(hub, i,
4890 NULL, HUB_BH_RESET_TIME,
4891 true);
4892 if (status < 0)
4893 hub_port_disable(hub, i, 1);
4894 } else {
4895 usb_lock_device(udev);
4896 status = usb_reset_device(udev);
4897 usb_unlock_device(udev);
4898 connect_change = 0;
4899 }
4900 /*
4901 * On disconnect USB3 protocol ports transit from U0 to
4902 * SS.Inactive to Rx.Detect. If this happens a warm-
4903 * reset is not needed, but a (re)connect may happen
4904 * before khubd runs and sees the disconnect, and the
4905 * device may be an unknown state.
4906 *
4907 * If the port went through SS.Inactive without khubd
4908 * seeing it the C_LINK_STATE change flag will be set,
4909 * and we reset the dev to put it in a known state.
4910 */
4911 } else if (udev && hub_is_superspeed(hub->hdev) &&
4912 (portchange & USB_PORT_STAT_C_LINK_STATE) &&
4913 (portstatus & USB_PORT_STAT_CONNECTION)) {
4914 usb_lock_device(udev);
4915 usb_reset_device(udev);
4916 usb_unlock_device(udev);
4917 connect_change = 0;
4918 }
4919
4920 if (connect_change)
4921 hub_port_connect_change(hub, i,
4922 portstatus, portchange);
4923 } /* end for i */
4924 5009
4925 /* deal with hub status changes */ 5010 /* deal with hub status changes */
4926 if (test_and_clear_bit(0, hub->event_bits) == 0) 5011 if (test_and_clear_bit(0, hub->event_bits) == 0)
@@ -5155,15 +5240,18 @@ static int descriptors_changed(struct usb_device *udev,
5155 * if the reset wasn't even attempted. 5240 * if the reset wasn't even attempted.
5156 * 5241 *
5157 * Note: 5242 * Note:
5158 * The caller must own the device lock. For example, it's safe to use 5243 * The caller must own the device lock and the port lock, the latter is
5159 * this from a driver probe() routine after downloading new firmware. 5244 * taken by usb_reset_device(). For example, it's safe to use
5160 * For calls that might not occur during probe(), drivers should lock 5245 * usb_reset_device() from a driver probe() routine after downloading
5161 * the device using usb_lock_device_for_reset(). 5246 * new firmware. For calls that might not occur during probe(), drivers
5247 * should lock the device using usb_lock_device_for_reset().
5162 * 5248 *
5163 * Locking exception: This routine may also be called from within an 5249 * Locking exception: This routine may also be called from within an
5164 * autoresume handler. Such usage won't conflict with other tasks 5250 * autoresume handler. Such usage won't conflict with other tasks
5165 * holding the device lock because these tasks should always call 5251 * holding the device lock because these tasks should always call
5166 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume. 5252 * usb_autopm_resume_device(), thereby preventing any unwanted
5253 * autoresume. The autoresume handler is expected to have already
5254 * acquired the port lock before calling this routine.
5167 */ 5255 */
5168static int usb_reset_and_verify_device(struct usb_device *udev) 5256static int usb_reset_and_verify_device(struct usb_device *udev)
5169{ 5257{
@@ -5182,11 +5270,9 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
5182 return -EINVAL; 5270 return -EINVAL;
5183 } 5271 }
5184 5272
5185 if (!parent_hdev) { 5273 if (!parent_hdev)
5186 /* this requires hcd-specific logic; see ohci_restart() */
5187 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
5188 return -EISDIR; 5274 return -EISDIR;
5189 } 5275
5190 parent_hub = usb_hub_to_struct_hub(parent_hdev); 5276 parent_hub = usb_hub_to_struct_hub(parent_hdev);
5191 5277
5192 /* Disable USB2 hardware LPM. 5278 /* Disable USB2 hardware LPM.
@@ -5215,7 +5301,6 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
5215 goto re_enumerate; 5301 goto re_enumerate;
5216 } 5302 }
5217 5303
5218 set_bit(port1, parent_hub->busy_bits);
5219 for (i = 0; i < SET_CONFIG_TRIES; ++i) { 5304 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
5220 5305
5221 /* ep0 maxpacket size may change; let the HCD know about it. 5306 /* ep0 maxpacket size may change; let the HCD know about it.
@@ -5225,7 +5310,6 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
5225 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV) 5310 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
5226 break; 5311 break;
5227 } 5312 }
5228 clear_bit(port1, parent_hub->busy_bits);
5229 5313
5230 if (ret < 0) 5314 if (ret < 0)
5231 goto re_enumerate; 5315 goto re_enumerate;
@@ -5346,7 +5430,9 @@ int usb_reset_device(struct usb_device *udev)
5346 int ret; 5430 int ret;
5347 int i; 5431 int i;
5348 unsigned int noio_flag; 5432 unsigned int noio_flag;
5433 struct usb_port *port_dev;
5349 struct usb_host_config *config = udev->actconfig; 5434 struct usb_host_config *config = udev->actconfig;
5435 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
5350 5436
5351 if (udev->state == USB_STATE_NOTATTACHED || 5437 if (udev->state == USB_STATE_NOTATTACHED ||
5352 udev->state == USB_STATE_SUSPENDED) { 5438 udev->state == USB_STATE_SUSPENDED) {
@@ -5355,6 +5441,14 @@ int usb_reset_device(struct usb_device *udev)
5355 return -EINVAL; 5441 return -EINVAL;
5356 } 5442 }
5357 5443
5444 if (!udev->parent) {
5445 /* this requires hcd-specific logic; see ohci_restart() */
5446 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
5447 return -EISDIR;
5448 }
5449
5450 port_dev = hub->ports[udev->portnum - 1];
5451
5358 /* 5452 /*
5359 * Don't allocate memory with GFP_KERNEL in current 5453 * Don't allocate memory with GFP_KERNEL in current
5360 * context to avoid possible deadlock if usb mass 5454 * context to avoid possible deadlock if usb mass
@@ -5388,7 +5482,9 @@ int usb_reset_device(struct usb_device *udev)
5388 } 5482 }
5389 } 5483 }
5390 5484
5485 usb_lock_port(port_dev);
5391 ret = usb_reset_and_verify_device(udev); 5486 ret = usb_reset_and_verify_device(udev);
5487 usb_unlock_port(port_dev);
5392 5488
5393 if (config) { 5489 if (config) {
5394 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) { 5490 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
@@ -5483,56 +5579,26 @@ struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5483} 5579}
5484EXPORT_SYMBOL_GPL(usb_hub_find_child); 5580EXPORT_SYMBOL_GPL(usb_hub_find_child);
5485 5581
5486/**
5487 * usb_set_hub_port_connect_type - set hub port connect type.
5488 * @hdev: USB device belonging to the usb hub
5489 * @port1: port num of the port
5490 * @type: connect type of the port
5491 */
5492void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1,
5493 enum usb_port_connect_type type)
5494{
5495 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
5496
5497 if (hub)
5498 hub->ports[port1 - 1]->connect_type = type;
5499}
5500
5501/**
5502 * usb_get_hub_port_connect_type - Get the port's connect type
5503 * @hdev: USB device belonging to the usb hub
5504 * @port1: port num of the port
5505 *
5506 * Return: The connect type of the port if successful. Or
5507 * USB_PORT_CONNECT_TYPE_UNKNOWN if input params are invalid.
5508 */
5509enum usb_port_connect_type
5510usb_get_hub_port_connect_type(struct usb_device *hdev, int port1)
5511{
5512 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
5513
5514 if (!hub)
5515 return USB_PORT_CONNECT_TYPE_UNKNOWN;
5516
5517 return hub->ports[port1 - 1]->connect_type;
5518}
5519
5520void usb_hub_adjust_deviceremovable(struct usb_device *hdev, 5582void usb_hub_adjust_deviceremovable(struct usb_device *hdev,
5521 struct usb_hub_descriptor *desc) 5583 struct usb_hub_descriptor *desc)
5522{ 5584{
5585 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
5523 enum usb_port_connect_type connect_type; 5586 enum usb_port_connect_type connect_type;
5524 int i; 5587 int i;
5525 5588
5589 if (!hub)
5590 return;
5591
5526 if (!hub_is_superspeed(hdev)) { 5592 if (!hub_is_superspeed(hdev)) {
5527 for (i = 1; i <= hdev->maxchild; i++) { 5593 for (i = 1; i <= hdev->maxchild; i++) {
5528 connect_type = usb_get_hub_port_connect_type(hdev, i); 5594 struct usb_port *port_dev = hub->ports[i - 1];
5529 5595
5596 connect_type = port_dev->connect_type;
5530 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 5597 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5531 u8 mask = 1 << (i%8); 5598 u8 mask = 1 << (i%8);
5532 5599
5533 if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) { 5600 if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) {
5534 dev_dbg(&hdev->dev, "usb port%d's DeviceRemovable is changed to 1 according to platform information.\n", 5601 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
5535 i);
5536 desc->u.hs.DeviceRemovable[i/8] |= mask; 5602 desc->u.hs.DeviceRemovable[i/8] |= mask;
5537 } 5603 }
5538 } 5604 }
@@ -5541,14 +5607,14 @@ void usb_hub_adjust_deviceremovable(struct usb_device *hdev,
5541 u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable); 5607 u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable);
5542 5608
5543 for (i = 1; i <= hdev->maxchild; i++) { 5609 for (i = 1; i <= hdev->maxchild; i++) {
5544 connect_type = usb_get_hub_port_connect_type(hdev, i); 5610 struct usb_port *port_dev = hub->ports[i - 1];
5545 5611
5612 connect_type = port_dev->connect_type;
5546 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 5613 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5547 u16 mask = 1 << i; 5614 u16 mask = 1 << i;
5548 5615
5549 if (!(port_removable & mask)) { 5616 if (!(port_removable & mask)) {
5550 dev_dbg(&hdev->dev, "usb port%d's DeviceRemovable is changed to 1 according to platform information.\n", 5617 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
5551 i);
5552 port_removable |= mask; 5618 port_removable |= mask;
5553 } 5619 }
5554 } 5620 }