11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/list.h>
18 #include <linux/slab.h>
21 #include <linux/usbdevice_fs.h>
28 #include <linux/random.h>
30 #include <asm/uaccess.h>
31 #include <asm/byteorder.h>
37 #ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
38 #define CONFIG_USB_ANNOUNCE_NEW_DEVICES
76 #if USB_MAXCHILDREN > 31
77 #error event_bits[] is too short!
96 static inline int hub_is_superspeed(
struct usb_device *hdev)
116 static bool blinkenlights = 0;
125 static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
128 "initial 64-byte descriptor request timeout in milliseconds "
129 "(default 5000 - 5.0 seconds)");
145 static bool old_scheme_first = 0;
148 "start with the old device initialization scheme");
150 static bool use_both_schemes = 1;
153 "try the other device initialization scheme if the "
162 #define HUB_DEBOUNCE_TIMEOUT 1500
163 #define HUB_DEBOUNCE_STEP 25
164 #define HUB_DEBOUNCE_STABLE 100
166 #define to_usb_port(_dev) \
167 container_of(_dev, struct usb_port, dev)
169 static int usb_reset_and_verify_device(
struct usb_device *
udev);
171 static inline char *portspeed(
struct usb_hub *hub,
int portstatus)
173 if (hub_is_superspeed(hub->
hdev))
184 static struct usb_hub *hdev_to_hub(
struct usb_device *
hdev)
186 if (!hdev || !hdev->actconfig || !hdev->maxchild)
188 return usb_get_intfdata(hdev->actconfig->interface[0]);
191 static int usb_device_supports_lpm(
struct usb_device *
udev)
197 if (udev->bos->ext_cap &&
207 if (!udev->bos->ss_cap) {
208 dev_warn(&udev->dev,
"No LPM exit latency info found. "
209 "Power management will be impacted.\n");
212 if (udev->parent->lpm_capable)
215 dev_warn(&udev->dev,
"Parent hub missing LPM exit latency info. "
216 "Power management will be impacted.\n");
224 static void usb_set_lpm_mel(
struct usb_device *udev,
225 struct usb3_lpm_parameters *udev_lpm_params,
226 unsigned int udev_exit_latency,
228 struct usb3_lpm_parameters *hub_lpm_params,
229 unsigned int hub_exit_latency)
231 unsigned int total_mel;
232 unsigned int device_mel;
233 unsigned int hub_mel;
244 total_mel = hub_lpm_params->mel +
255 device_mel = udev_exit_latency * 1000;
256 hub_mel = hub_exit_latency * 1000;
257 if (device_mel > hub_mel)
258 total_mel += device_mel;
260 total_mel += hub_mel;
262 udev_lpm_params->mel = total_mel;
269 static void usb_set_lpm_pel(
struct usb_device *udev,
270 struct usb3_lpm_parameters *udev_lpm_params,
271 unsigned int udev_exit_latency,
273 struct usb3_lpm_parameters *hub_lpm_params,
274 unsigned int hub_exit_latency,
275 unsigned int port_to_port_exit_latency)
277 unsigned int first_link_pel;
278 unsigned int hub_pel;
285 if (udev_exit_latency > hub_exit_latency)
286 first_link_pel = udev_exit_latency * 1000;
288 first_link_pel = hub_exit_latency * 1000;
296 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
302 if (first_link_pel > hub_pel)
303 udev_lpm_params->pel = first_link_pel;
305 udev_lpm_params->pel = hub_pel;
323 static void usb_set_lpm_sel(
struct usb_device *udev,
324 struct usb3_lpm_parameters *udev_lpm_params)
326 struct usb_device *
parent;
327 unsigned int num_hubs;
328 unsigned int total_sel;
331 total_sel = udev_lpm_params->pel;
333 for (parent = udev->parent, num_hubs = 0; parent->parent;
334 parent = parent->parent)
338 total_sel += 2100 + 250 * (num_hubs - 1);
341 total_sel += 250 * num_hubs;
343 udev_lpm_params->sel = total_sel;
346 static void usb_set_lpm_parameters(
struct usb_device *udev)
349 unsigned int port_to_port_delay;
350 unsigned int udev_u1_del;
351 unsigned int udev_u2_del;
352 unsigned int hub_u1_del;
353 unsigned int hub_u2_del;
358 hub = hdev_to_hub(udev->parent);
365 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
366 udev_u2_del = udev->bos->ss_cap->bU2DevExitLat;
367 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
368 hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat;
370 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
371 hub, &udev->parent->u1_params, hub_u1_del);
373 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
374 hub, &udev->parent->u2_params, hub_u2_del);
393 port_to_port_delay = 1;
395 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
396 hub, &udev->parent->u1_params, hub_u1_del,
399 if (hub_u2_del > hub_u1_del)
400 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
402 port_to_port_delay = 1 + hub_u1_del;
404 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
405 hub, &udev->parent->u2_params, hub_u2_del,
409 usb_set_lpm_sel(udev, &udev->u1_params);
410 usb_set_lpm_sel(udev, &udev->u2_params);
414 static int get_hub_descriptor(
struct usb_device *hdev,
void *
data)
419 if (hub_is_superspeed(hdev)) {
427 for (i = 0; i < 3; i++) {
430 dtype << 8, 0, data, size,
431 USB_CTRL_GET_TIMEOUT);
441 static int clear_hub_feature(
struct usb_device *hdev,
int feature)
450 static int clear_port_feature(
struct usb_device *hdev,
int port1,
int feature)
460 static int set_port_feature(
struct usb_device *hdev,
int port1,
int feature)
471 static void set_port_led(
477 int status = set_port_feature(hub->
hdev, (selector << 8) | port1,
481 "port %d indicator %s status %d\n",
483 ({
char *
s; switch (selector) {
488 default:
s =
"??";
break;
493 #define LED_CYCLE_PERIOD ((2*HZ)/3)
499 struct usb_device *hdev = hub->
hdev;
507 for (i = 0; i < hub->
descriptor->bNbrPorts; i++) {
508 unsigned selector,
mode;
551 set_port_led(hub, i + 1, selector);
554 if (!changed && blinkenlights) {
566 #define USB_STS_TIMEOUT 1000
567 #define USB_STS_RETRIES 5
572 static int get_hub_status(
struct usb_device *hdev,
589 static int get_port_status(
struct usb_device *hdev,
int port1,
603 static int hub_port_status(
struct usb_hub *hub,
int port1,
612 "%s failed (err = %d)\n", __func__, ret);
625 static void kick_khubd(
struct usb_hub *hub)
634 usb_autopm_get_interface_no_resume(
635 to_usb_interface(hub->
intfdev));
638 spin_unlock_irqrestore(&hub_event_lock, flags);
643 struct usb_hub *hub = hdev_to_hub(hdev);
658 unsigned int portnum)
665 hub = hdev_to_hub(hdev);
674 static void hub_irq(
struct urb *
urb)
676 struct usb_hub *hub = urb->context;
677 int status = urb->status;
698 for (i = 0; i < urb->actual_length; ++
i)
699 bits |= ((
unsigned long) ((*hub->
buffer)[i]))
721 hub_clear_tt_buffer (
struct usb_device *hdev,
u16 devinfo,
u16 tt)
742 while (!list_empty(&hub->
tt.clear_list)) {
744 struct usb_tt_clear *
clear;
745 struct usb_device *hdev = hub->
hdev;
746 const struct hc_driver *drv;
752 next = hub->
tt.clear_list.next;
753 clear =
list_entry (next,
struct usb_tt_clear, clear_list);
757 spin_unlock_irqrestore (&hub->
tt.lock, flags);
758 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
761 "clear tt %d (%04x) error %d\n",
762 clear->tt, clear->devinfo, status);
765 drv = clear->hcd->driver;
766 if (drv->clear_tt_buffer_complete)
767 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
772 spin_unlock_irqrestore (&hub->
tt.lock, flags);
789 struct usb_device *udev = urb->dev;
790 int pipe = urb->pipe;
791 struct usb_tt *tt = udev->tt;
793 struct usb_tt_clear *
clear;
800 dev_err (&udev->dev,
"can't save CLEAR_TT_BUFFER state\n");
806 clear->tt = tt->multi ? udev->ttport : 1;
807 clear->devinfo = usb_pipeendpoint (pipe);
808 clear->devinfo |= udev->devnum << 4;
809 clear->devinfo |= usb_pipecontrol (pipe)
812 if (usb_pipein (pipe))
813 clear->devinfo |= 1 << 15;
816 clear->hcd = bus_to_hcd(udev->bus);
823 spin_unlock_irqrestore (&tt->lock, flags);
831 static unsigned hub_power_on(
struct usb_hub *hub,
bool do_delay)
834 unsigned pgood_delay = hub->
descriptor->bPwrOn2PwrGood * 2;
849 "non-switchable hub\n");
850 for (port1 = 1; port1 <= hub->
descriptor->bNbrPorts; port1++)
854 delay =
max(pgood_delay, (
unsigned) 100);
860 static int hub_hub_status(
struct usb_hub *hub,
869 "%s failed (err = %d)\n", __func__, ret);
879 static int hub_port_disable(
struct usb_hub *hub,
int port1,
int set_state)
881 struct usb_device *hdev = hub->
hdev;
884 if (hub->
ports[port1 - 1]->child && set_state)
887 if (!hub->
error && !hub_is_superspeed(hub->
hdev))
900 static void hub_port_logical_disconnect(
struct usb_hub *hub,
int port1)
903 hub_port_disable(hub, port1, 1);
935 hub = hdev_to_hub(udev->parent);
936 intf = to_usb_interface(hub->
intfdev);
938 usb_autopm_get_interface(intf);
940 hub_port_logical_disconnect(hub, udev->portnum);
941 usb_autopm_put_interface(intf);
955 struct usb_device *hdev = hub->
hdev;
960 bool need_debounce_delay =
false;
980 if (hdev->parent && hub_is_superspeed(hdev)) {
983 hdev->level - 1, 0,
NULL, 0,
984 USB_CTRL_SET_TIMEOUT);
987 "set hub depth failed\n");
1003 delay = hub_power_on(hub,
false);
1009 usb_autopm_get_interface_no_resume(
1010 to_usb_interface(hub->
intfdev));
1017 hcd = bus_to_hcd(hdev->bus);
1018 if (hcd->driver->update_hub_device) {
1019 ret = hcd->driver->update_hub_device(hcd, hdev,
1023 "accepting hub info "
1026 "and hubs may not work "
1027 "under this hub\n.");
1030 hub_power_on(hub,
true);
1032 hub_power_on(hub,
true);
1040 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
1041 struct usb_device *udev = hub->
ports[port1 - 1]->child;
1042 u16 portstatus, portchange;
1044 portstatus = portchange = 0;
1045 status = hub_port_status(hub, port1, &portstatus, &portchange);
1048 "port %d: status %04x change %04x\n",
1049 port1, portstatus, portchange);
1058 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1066 if (!hub_is_superspeed(hdev)) {
1067 clear_port_feature(hdev, port1,
1069 portstatus &= ~USB_PORT_STAT_ENABLE;
1072 portstatus &= ~USB_PORT_STAT_ENABLE;
1078 need_debounce_delay =
true;
1079 clear_port_feature(hub->
hdev, port1,
1083 need_debounce_delay =
true;
1084 clear_port_feature(hub->
hdev, port1,
1088 hub_is_superspeed(hub->
hdev)) {
1089 need_debounce_delay =
true;
1090 clear_port_feature(hub->
hdev, port1,
1096 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1097 (portchange & USB_PORT_STAT_C_CONNECTION))
1104 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1107 }
else if (portstatus & USB_PORT_STAT_ENABLE) {
1108 bool port_resumed = (portstatus &
1119 if (portchange || (hub_is_superspeed(hub->
hdev) &&
1123 }
else if (udev->persist_enabled) {
1125 udev->reset_resume = 1;
1144 if (need_debounce_delay) {
1171 usb_autopm_put_interface_async(to_usb_interface(hub->
intfdev));
1195 struct usb_device *hdev = hub->
hdev;
1205 for (i = 0; i < hdev->maxchild; ++
i) {
1206 if (hub->
ports[i]->child)
1222 struct usb_hub *hub = usb_get_intfdata(intf);
1231 struct usb_hub *hub = usb_get_intfdata(intf);
1237 static void usb_port_device_release(
struct device *
dev)
1244 static void usb_hub_remove_port_device(
struct usb_hub *hub,
1252 .release = usb_port_device_release,
1255 static int usb_hub_create_port_device(
struct usb_hub *hub,
1261 port_dev = kzalloc(
sizeof(*port_dev),
GFP_KERNEL);
1267 hub->
ports[port1 - 1] = port_dev;
1274 goto error_register;
1283 static int hub_configure(
struct usb_hub *hub,
1286 struct usb_hcd *hcd;
1287 struct usb_device *hdev = hub->
hdev;
1289 u16 hubstatus, hubchange;
1293 char *
message =
"out of memory";
1318 ret = get_hub_descriptor(hdev, hub->
descriptor);
1320 message =
"can't read hub descriptor";
1322 }
else if (hub->
descriptor->bNbrPorts > USB_MAXCHILDREN) {
1323 message =
"hub has too many ports!";
1329 dev_info (hub_dev,
"%d port%s detected\n", hdev->maxchild,
1330 (hdev->maxchild == 1) ?
"" :
"s");
1332 hub->
ports = kzalloc(hdev->maxchild *
sizeof(
struct usb_port *),
1343 !(hub_is_superspeed(hdev))) {
1345 char portstr [USB_MAXCHILDREN + 1];
1347 for (i = 0; i < hdev->maxchild; i++)
1348 portstr[i] = hub->
descriptor->u.hs.DeviceRemovable
1349 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1351 portstr[hdev->maxchild] = 0;
1352 dev_dbg(hub_dev,
"compound device; port removable status: %s\n", portstr);
1354 dev_dbg(hub_dev,
"standalone hub\n");
1356 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
1358 dev_dbg(hub_dev,
"ganged power switching\n");
1361 dev_dbg(hub_dev,
"individual port power switching\n");
1365 dev_dbg(hub_dev,
"no power switching (usb 1.0)\n");
1371 dev_dbg(hub_dev,
"global over-current protection\n");
1374 dev_dbg(hub_dev,
"individual port over-current protection\n");
1378 dev_dbg(hub_dev,
"no over-current protection\n");
1383 INIT_LIST_HEAD (&hub->
tt.clear_list);
1385 switch (hdev->descriptor.bDeviceProtocol) {
1389 dev_dbg(hub_dev,
"Single TT\n");
1395 dev_dbg(hub_dev,
"TT per port\n");
1398 dev_err(hub_dev,
"Using single TT (err %d)\n",
1406 dev_dbg(hub_dev,
"Unrecognized hub protocol %d\n",
1407 hdev->descriptor.bDeviceProtocol);
1414 if (hdev->descriptor.bDeviceProtocol != 0) {
1415 hub->
tt.think_time = 666;
1416 dev_dbg(hub_dev,
"TT requires at most %d "
1417 "FS bit times (%d ns)\n",
1418 8, hub->
tt.think_time);
1422 hub->
tt.think_time = 666 * 2;
1423 dev_dbg(hub_dev,
"TT requires at most %d "
1424 "FS bit times (%d ns)\n",
1425 16, hub->
tt.think_time);
1428 hub->
tt.think_time = 666 * 3;
1429 dev_dbg(hub_dev,
"TT requires at most %d "
1430 "FS bit times (%d ns)\n",
1431 24, hub->
tt.think_time);
1434 hub->
tt.think_time = 666 * 4;
1435 dev_dbg(hub_dev,
"TT requires at most %d "
1436 "FS bit times (%d ns)\n",
1437 32, hub->
tt.think_time);
1444 dev_dbg(hub_dev,
"Port indicators are supported\n");
1447 dev_dbg(hub_dev,
"power on to power good time: %dms\n",
1455 message =
"can't get hub status";
1459 if (hdev == hdev->bus->root_hub) {
1460 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1467 dev_dbg(hub_dev,
"hub controller current requirement: %dmA\n",
1470 if (hdev->maxchild > 0) {
1471 int remaining = hdev->bus_mA -
1474 if (remaining < hdev->maxchild * 100)
1476 "insufficient power available "
1477 "to use all downstream ports\n");
1486 dev_dbg(hub_dev,
"%umA bus power budget for each child\n",
1492 hcd = bus_to_hcd(hdev->bus);
1493 if (hcd->driver->update_hub_device) {
1494 ret = hcd->driver->update_hub_device(hcd, hdev,
1497 message =
"can't update HCD hub info";
1502 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1504 message =
"can't get hub status";
1510 dev_dbg(hub_dev,
"local power source is %s\n",
1512 ?
"lost (inactive)" :
"good");
1514 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
1515 dev_dbg(hub_dev,
"%sover-current condition exists\n",
1525 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1527 if (maxp >
sizeof(*hub->
buffer))
1528 maxp =
sizeof(*hub->
buffer);
1536 usb_fill_int_urb(hub->
urb, hdev, pipe, *hub->
buffer, maxp, hub_irq,
1543 for (i = 0; i < hdev->maxchild; i++)
1544 if (usb_hub_create_port_device(hub, i + 1) < 0)
1546 "couldn't create port%d device.\n", i + 1);
1552 dev_err (hub_dev,
"config failed, %s (err %d)\n",
1558 static void hub_release(
struct kref *
kref)
1566 static unsigned highspeed_hubs;
1570 struct usb_hub *hub = usb_get_intfdata(intf);
1571 struct usb_device *hdev = interface_to_usbdev(intf);
1575 spin_lock_irq(&hub_event_lock);
1578 usb_autopm_put_interface_no_suspend(intf);
1581 spin_unlock_irq(&hub_event_lock);
1587 usb_set_intfdata (intf,
NULL);
1589 for (i = 0; i < hdev->maxchild; i++)
1590 usb_hub_remove_port_device(hub, i + 1);
1591 hub->
hdev->maxchild = 0;
1602 kref_put(&hub->
kref, hub_release);
1607 struct usb_host_interface *
desc;
1609 struct usb_device *hdev;
1612 desc = intf->cur_altsetting;
1613 hdev = interface_to_usbdev(intf);
1616 usb_enable_autosuspend(hdev);
1618 if (hdev->level == MAX_TOPO_LEVEL) {
1620 "Unsupported bus topology: hub nested too deep\n");
1624 #ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1626 dev_warn(&intf->dev,
"ignoring external hub\n");
1633 if ((desc->desc.bInterfaceSubClass != 0) &&
1634 (desc->desc.bInterfaceSubClass != 1)) {
1636 dev_err (&intf->dev,
"bad descriptor, ignoring hub\n");
1641 if (desc->desc.bNumEndpoints != 1)
1642 goto descriptor_error;
1644 endpoint = &desc->endpoint[0].desc;
1647 if (!usb_endpoint_is_int_in(endpoint))
1648 goto descriptor_error;
1651 dev_info (&intf->dev,
"USB hub found\n");
1655 dev_dbg (&intf->dev,
"couldn't kmalloc hub struct\n");
1659 kref_init(&hub->
kref);
1667 usb_set_intfdata (intf, hub);
1668 intf->needs_remote_wakeup = 1;
1673 if (hub_configure(hub, endpoint) >= 0)
1676 hub_disconnect (intf);
1683 struct usb_device *hdev = interface_to_usbdev (intf);
1684 struct usb_hub *hub = hdev_to_hub(hdev);
1692 spin_lock_irq(&device_state_lock);
1693 if (hdev->devnum <= 0)
1696 info->
nports = hdev->maxchild;
1697 for (i = 0; i < info->
nports; i++) {
1702 hub->
ports[
i]->child->devnum;
1705 spin_unlock_irq(&device_state_lock);
1719 static int find_port_owner(
struct usb_device *hdev,
unsigned port1,
1724 if (port1 == 0 || port1 > hdev->maxchild)
1730 *ppowner = &(hdev_to_hub(hdev)->ports[port1 - 1]->port_owner);
1741 rc = find_port_owner(hdev, port1, &powner);
1756 rc = find_port_owner(hdev, port1, &powner);
1759 if (*powner != owner)
1767 struct usb_hub *hub = hdev_to_hub(hdev);
1770 for (n = 0; n < hdev->maxchild; n++) {
1771 if (hub->
ports[n]->port_owner == owner)
1784 hub = hdev_to_hub(udev->parent);
1785 return !!hub->
ports[udev->portnum - 1]->port_owner;
1788 static void recursively_mark_NOTATTACHED(
struct usb_device *udev)
1790 struct usb_hub *hub = hdev_to_hub(udev);
1793 for (i = 0; i < udev->maxchild; ++
i) {
1794 if (hub->
ports[i]->child)
1795 recursively_mark_NOTATTACHED(hub->
ports[i]->child);
1798 udev->active_duration -=
jiffies;
1826 unsigned long flags;
1842 wakeup = udev->actconfig->desc.bmAttributes
1849 udev->active_duration -=
jiffies;
1852 udev->active_duration +=
jiffies;
1855 recursively_mark_NOTATTACHED(udev);
1856 spin_unlock_irqrestore(&device_state_lock, flags);
1891 static void choose_devnum(
struct usb_device *udev)
1894 struct usb_bus *
bus = udev->bus;
1898 devnum = udev->portnum + 1;
1908 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1911 set_bit(devnum, bus->devmap.devicemap);
1916 static void release_devnum(
struct usb_device *udev)
1918 if (udev->devnum > 0) {
1919 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1924 static void update_devnum(
struct usb_device *udev,
int devnum)
1931 static void hub_free_dev(
struct usb_device *udev)
1933 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1936 if (hcd->driver->free_dev && udev->parent)
1937 hcd->driver->free_dev(hcd, udev);
1958 struct usb_device *udev = *pdev;
1959 struct usb_hub *hub = hdev_to_hub(udev);
1967 dev_info(&udev->dev,
"USB disconnect, device number %d\n",
1970 usb_lock_device(udev);
1973 for (i = 0; i < udev->maxchild; i++) {
1974 if (hub->
ports[i]->child)
1982 dev_dbg (&udev->dev,
"unregistering device\n");
1987 usb_unlock_device(udev);
1998 release_devnum(udev);
2001 spin_lock_irq(&device_state_lock);
2003 spin_unlock_irq(&device_state_lock);
2010 #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
2011 static void show_string(
struct usb_device *udev,
char *
id,
char *
string)
2015 dev_printk(
KERN_INFO, &udev->dev,
"%s: %s\n",
id,
string);
2018 static void announce_device(
struct usb_device *udev)
2020 dev_info(&udev->dev,
"New USB device found, idVendor=%04x, idProduct=%04x\n",
2024 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
2025 udev->descriptor.iManufacturer,
2026 udev->descriptor.iProduct,
2027 udev->descriptor.iSerialNumber);
2028 show_string(udev,
"Product", udev->product);
2029 show_string(udev,
"Manufacturer", udev->manufacturer);
2030 show_string(udev,
"SerialNumber", udev->serial);
2033 static inline void announce_device(
struct usb_device *udev) { }
2036 #ifdef CONFIG_USB_OTG
2046 static int usb_enumerate_device_otg(
struct usb_device *udev)
2050 #ifdef CONFIG_USB_OTG
2056 if (!udev->bus->is_b_host
2058 && udev->parent == udev->bus->root_hub) {
2060 struct usb_bus *bus = udev->bus;
2067 unsigned port1 = udev->portnum;
2070 "Dual-Role OTG device on %sHNP port\n",
2071 (port1 == bus->otg_port)
2075 if (port1 == bus->otg_port)
2076 bus->b_hnp_enable = 1;
2078 usb_sndctrlpipe(udev, 0),
2083 0,
NULL, 0, USB_CTRL_SET_TIMEOUT);
2089 "can't set HNP mode: %d\n",
2091 bus->b_hnp_enable = 0;
2097 if (!is_targeted(udev)) {
2102 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
2105 dev_dbg(&udev->dev,
"HNP fail, %d\n", err);
2128 static int usb_enumerate_device(
struct usb_device *udev)
2132 if (udev->config ==
NULL) {
2135 dev_err(&udev->dev,
"can't read configurations, error %d\n",
2140 if (udev->wusb == 1 && udev->authorized == 0) {
2149 udev->descriptor.iManufacturer);
2152 err = usb_enumerate_device_otg(udev);
2161 static void set_usb_port_removable(
struct usb_device *udev)
2163 struct usb_device *hdev = udev->parent;
2172 hub = hdev_to_hub(udev->parent);
2176 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2179 if (hub_is_superspeed(hdev)) {
2184 if (hub->
descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2189 udev->removable = USB_DEVICE_REMOVABLE;
2191 udev->removable = USB_DEVICE_FIXED;
2227 pm_runtime_set_active(&udev->dev);
2228 pm_runtime_get_noresume(&udev->dev);
2229 pm_runtime_use_autosuspend(&udev->dev);
2235 usb_disable_autosuspend(udev);
2237 err = usb_enumerate_device(udev);
2240 dev_dbg(&udev->dev,
"udev %d, busnum %d, minor = %d\n",
2241 udev->devnum, udev->bus->busnum,
2242 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2245 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2248 announce_device(udev);
2254 if (udev->manufacturer)
2256 strlen(udev->manufacturer));
2258 device_enable_async_suspend(&udev->dev);
2266 set_usb_port_removable(udev);
2274 dev_err(&udev->dev,
"can't device_add, error %d\n", err);
2279 usb_mark_last_busy(udev);
2280 pm_runtime_put_sync_autosuspend(&udev->dev);
2285 pm_runtime_disable(&udev->dev);
2286 pm_runtime_set_suspended(&udev->dev);
2303 usb_lock_device(usb_dev);
2304 if (usb_dev->authorized == 0)
2305 goto out_unauthorized;
2307 usb_dev->authorized = 0;
2310 kfree(usb_dev->product);
2312 kfree(usb_dev->manufacturer);
2314 kfree(usb_dev->serial);
2318 usb_dev->descriptor.bNumConfigurations = 0;
2321 usb_unlock_device(usb_dev);
2330 usb_lock_device(usb_dev);
2331 if (usb_dev->authorized == 1)
2332 goto out_authorized;
2334 result = usb_autoresume_device(usb_dev);
2337 "can't autoresume for authorization: %d\n", result);
2338 goto error_autoresume;
2342 dev_err(&usb_dev->dev,
"can't re-read device descriptor for "
2343 "authorization: %d\n", result);
2344 goto error_device_descriptor;
2347 kfree(usb_dev->product);
2348 usb_dev->product =
NULL;
2349 kfree(usb_dev->manufacturer);
2350 usb_dev->manufacturer =
NULL;
2351 kfree(usb_dev->serial);
2352 usb_dev->serial =
NULL;
2354 usb_dev->authorized = 1;
2355 result = usb_enumerate_device(usb_dev);
2357 goto error_enumerate;
2366 "can't set config #%d, error %d\n",
c, result);
2371 dev_info(&usb_dev->dev,
"authorized to connect\n");
2374 error_device_descriptor:
2378 usb_unlock_device(usb_dev);
2384 static unsigned hub_is_wusb(
struct usb_hub *hub)
2386 struct usb_hcd *hcd;
2390 return hcd->wireless;
2394 #define PORT_RESET_TRIES 5
2395 #define SET_ADDRESS_TRIES 2
2396 #define GET_DESCRIPTOR_TRIES 2
2397 #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
2398 #define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
2400 #define HUB_ROOT_RESET_TIME 50
2401 #define HUB_SHORT_RESET_TIME 10
2402 #define HUB_BH_RESET_TIME 50
2403 #define HUB_LONG_RESET_TIME 200
2404 #define HUB_RESET_TIMEOUT 500
2406 static int hub_port_reset(
struct usb_hub *hub,
int port1,
2407 struct usb_device *udev,
unsigned int delay,
bool warm);
2412 static bool hub_port_warm_reset_required(
struct usb_hub *hub,
u16 portstatus)
2414 return hub_is_superspeed(hub->
hdev) &&
2421 static int hub_port_wait_reset(
struct usb_hub *hub,
int port1,
2422 struct usb_device *udev,
unsigned int delay,
bool warm)
2424 int delay_time,
ret;
2428 for (delay_time = 0;
2430 delay_time +=
delay) {
2435 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2454 if (hub_port_warm_reset_required(hub, portstatus)) {
2457 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2458 clear_port_feature(hub->
hdev, port1,
2461 clear_port_feature(hub->
hdev, port1,
2464 clear_port_feature(hub->
hdev, port1,
2468 ret = hub_port_reset(hub, port1,
2471 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2472 clear_port_feature(hub->
hdev, port1,
2477 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2481 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2488 (portstatus & USB_PORT_STAT_ENABLE)) {
2489 if (hub_is_wusb(hub))
2491 else if (hub_is_superspeed(hub->
hdev))
2493 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2502 if (portchange & USB_PORT_STAT_C_BH_RESET)
2511 "port %d not %sreset yet, waiting %dms\n",
2512 port1, warm ?
"warm " :
"", delay);
2518 static void hub_port_finish_reset(
struct usb_hub *hub,
int port1,
2519 struct usb_device *udev,
int *status,
bool warm)
2524 struct usb_hcd *hcd;
2527 update_devnum(udev, 0);
2528 hcd = bus_to_hcd(udev->bus);
2529 if (hcd->driver->reset_device) {
2530 *status = hcd->driver->reset_device(hcd, udev);
2532 dev_err(&udev->dev,
"Cannot reset "
2533 "HCD device state\n");
2541 clear_port_feature(hub->
hdev,
2545 clear_port_feature(hub->
hdev, port1,
2547 clear_port_feature(hub->
hdev, port1,
2559 static int hub_port_reset(
struct usb_hub *hub,
int port1,
2560 struct usb_device *udev,
unsigned int delay,
bool warm)
2570 if (!hub_is_superspeed(hub->
hdev)) {
2579 status = set_port_feature(hub->
hdev, port1, (warm ?
2584 "cannot %sreset port %d (err = %d)\n",
2585 warm ?
"warm " :
"", port1, status);
2587 status = hub_port_wait_reset(hub, port1, udev, delay,
2591 "port_wait_reset: err = %d\n",
2597 hub_port_finish_reset(hub, port1, udev, &status, warm);
2602 "port %d not enabled, trying %sreset again...\n",
2603 port1, warm ?
"warm " :
"");
2608 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2613 up_read(&ehci_cf_port_reset_rwsem);
2619 static int port_is_power_on(
struct usb_hub *hub,
unsigned portstatus)
2623 if (hub_is_superspeed(hub->
hdev)) {
2637 static int port_is_suspended(
struct usb_hub *hub,
unsigned portstatus)
2641 if (hub_is_superspeed(hub->
hdev)) {
2656 static int check_port_resume_type(
struct usb_device *udev,
2657 struct usb_hub *hub,
int port1,
2658 int status,
unsigned portchange,
unsigned portstatus)
2661 if (status || port_is_suspended(hub, portstatus) ||
2662 !port_is_power_on(hub, portstatus) ||
2663 !(portstatus & USB_PORT_STAT_CONNECTION)) {
2671 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2672 if (udev->persist_enabled)
2673 udev->reset_resume = 1;
2680 "port %d status %04x.%04x after resume, %d\n",
2681 port1, portchange, portstatus, status);
2682 }
else if (udev->reset_resume) {
2685 if (portchange & USB_PORT_STAT_C_CONNECTION)
2686 clear_port_feature(hub->
hdev, port1,
2688 if (portchange & USB_PORT_STAT_C_ENABLE)
2689 clear_port_feature(hub->
hdev, port1,
2698 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2701 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2702 !usb_device_supports_ltm(udev))
2708 if (!udev->actconfig)
2714 USB_CTRL_SET_TIMEOUT);
2720 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2723 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2724 !usb_device_supports_ltm(udev))
2730 if (!udev->actconfig)
2736 USB_CTRL_SET_TIMEOUT);
2740 #ifdef CONFIG_USB_SUSPEND
2790 struct usb_hub *hub = hdev_to_hub(udev->parent);
2791 int port1 = udev->portnum;
2800 if (udev->do_remote_wakeup) {
2801 if (!hub_is_superspeed(hub->
hdev)) {
2806 USB_CTRL_SET_TIMEOUT);
2820 USB_CTRL_SET_TIMEOUT);
2823 dev_dbg(&udev->dev,
"won't remote wakeup, status %d\n",
2832 if (udev->usb2_hw_lpm_enabled == 1)
2833 usb_set_usb2_hardware_lpm(udev, 0);
2836 dev_err(&udev->dev,
"%s Failed to disable LTM before suspend\n.",
2841 dev_err(&udev->dev,
"%s Failed to disable LPM before suspend\n.",
2847 if (hub_is_superspeed(hub->
hdev))
2848 status = set_port_feature(hub->
hdev,
2852 status = set_port_feature(hub->
hdev, port1,
2858 if (udev->do_remote_wakeup)
2863 USB_CTRL_SET_TIMEOUT);
2866 if (udev->usb2_hw_lpm_capable == 1)
2867 usb_set_usb2_hardware_lpm(udev, 1);
2878 dev_dbg(&udev->dev,
"usb %ssuspend, wakeup %d\n",
2880 udev->do_remote_wakeup);
2884 usb_mark_last_busy(hub->
hdev);
2899 static int finish_port_resume(
struct usb_device *udev)
2906 udev->reset_resume ?
"finish reset-resume" :
"finish resume");
2922 if (udev->reset_resume)
2924 status = usb_reset_and_verify_device(udev);
2934 status = (status > 0 ? 0 : -
ENODEV);
2937 if (status && !udev->reset_resume && udev->persist_enabled) {
2938 dev_dbg(&udev->dev,
"retry with reset-resume\n");
2939 udev->reset_resume = 1;
2940 goto retry_reset_resume;
2945 dev_dbg(&udev->dev,
"gone after usb resume? status %d\n",
2947 }
else if (udev->actconfig) {
2949 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
2951 usb_sndctrlpipe(udev, 0),
2954 USB_DEVICE_REMOTE_WAKEUP, 0,
2956 USB_CTRL_SET_TIMEOUT);
2959 "disable remote wakeup, status %d\n",
3001 int usb_port_resume(
struct usb_device *udev,
pm_message_t msg)
3003 struct usb_hub *hub = hdev_to_hub(udev->parent);
3004 int port1 = udev->portnum;
3006 u16 portchange, portstatus;
3009 status = hub_port_status(hub, port1, &portstatus, &portchange);
3010 if (status == 0 && !port_is_suspended(hub, portstatus))
3011 goto SuspendCleared;
3018 if (hub_is_superspeed(hub->
hdev))
3019 status = set_port_feature(hub->
hdev,
3023 status = clear_port_feature(hub->
hdev,
3030 dev_dbg(&udev->dev,
"usb %sresume\n",
3038 status = hub_port_status(hub, port1, &portstatus, &portchange);
3046 if (hub_is_superspeed(hub->
hdev)) {
3047 if (portchange & USB_PORT_STAT_C_LINK_STATE)
3048 clear_port_feature(hub->
hdev, port1,
3052 clear_port_feature(hub->
hdev, port1,
3059 status = check_port_resume_type(udev,
3060 hub, port1, status, portchange, portstatus);
3062 status = finish_port_resume(udev);
3064 dev_dbg(&udev->dev,
"can't resume, status %d\n", status);
3065 hub_port_logical_disconnect(hub, port1);
3068 if (udev->usb2_hw_lpm_capable == 1)
3069 usb_set_usb2_hardware_lpm(udev, 1);
3080 int usb_remote_wakeup(
struct usb_device *udev)
3085 dev_dbg(&udev->dev,
"usb %sresume\n",
"wakeup-");
3086 status = usb_autoresume_device(udev);
3099 int usb_port_suspend(
struct usb_device *udev,
pm_message_t msg)
3106 int usb_port_resume(
struct usb_device *udev,
pm_message_t msg)
3108 struct usb_hub *hub = hdev_to_hub(udev->parent);
3109 int port1 = udev->portnum;
3111 u16 portchange, portstatus;
3113 status = hub_port_status(hub, port1, &portstatus, &portchange);
3114 status = check_port_resume_type(udev,
3115 hub, port1, status, portchange, portstatus);
3118 dev_dbg(&udev->dev,
"can't resume, status %d\n", status);
3119 hub_port_logical_disconnect(hub, port1);
3120 }
else if (udev->reset_resume) {
3121 dev_dbg(&udev->dev,
"reset-resume\n");
3122 status = usb_reset_and_verify_device(udev);
3131 struct usb_hub *hub = usb_get_intfdata (intf);
3132 struct usb_device *hdev = hub->
hdev;
3137 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3138 struct usb_device *
udev;
3140 udev = hub->
ports[port1 - 1]->child;
3141 if (udev && udev->can_submit) {
3142 dev_warn(&intf->dev,
"port %d nyet suspended\n", port1);
3147 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3149 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3150 status = set_port_feature(hdev,
3159 dev_dbg(&intf->dev,
"%s\n", __func__);
3168 struct usb_hub *hub = usb_get_intfdata(intf);
3170 dev_dbg(&intf->dev,
"%s\n", __func__);
3177 struct usb_hub *hub = usb_get_intfdata(intf);
3179 dev_dbg(&intf->dev,
"%s\n", __func__);
3195 void usb_root_hub_lost_power(
struct usb_device *rhdev)
3197 dev_warn(&rhdev->dev,
"root hub lost power or was reset\n");
3198 rhdev->reset_resume = 1;
3202 static const char *
const usb3_lpm_names[] = {
3221 unsigned long long u1_sel;
3222 unsigned long long u1_pel;
3223 unsigned long long u2_sel;
3224 unsigned long long u2_pel;
3247 dev_dbg(&udev->dev,
"Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n",
3248 usb3_lpm_names[state], u1_sel, u1_pel);
3287 sel_values,
sizeof *(sel_values),
3288 USB_CTRL_SET_TIMEOUT);
3296 static int usb_set_device_initiated_lpm(
struct usb_device *udev,
3310 dev_warn(&udev->dev,
"%s: Can't %s non-U1 or U2 state.\n",
3311 __func__, enable ?
"enable" :
"disable");
3316 dev_dbg(&udev->dev,
"%s: Can't %s %s state "
3317 "for unconfigured device.\n",
3318 __func__, enable ?
"enable" :
"disable",
3319 usb3_lpm_names[state]);
3333 USB_CTRL_SET_TIMEOUT);
3340 USB_CTRL_SET_TIMEOUT);
3343 dev_warn(&udev->dev,
"%s of device-initiated %s failed.\n",
3344 enable ?
"Enable" :
"Disable",
3345 usb3_lpm_names[state]);
3351 static int usb_set_lpm_timeout(
struct usb_device *udev,
3365 dev_warn(&udev->dev,
"%s: Can't set timeout for non-U1 or U2 state.\n",
3372 dev_warn(&udev->dev,
"Failed to set %s timeout to 0x%x, "
3373 "which is a reserved value.\n",
3374 usb3_lpm_names[state], timeout);
3378 ret = set_port_feature(udev->parent,
3382 dev_warn(&udev->dev,
"Failed to set %s timeout to 0x%x,"
3383 "error code %i\n", usb3_lpm_names[state],
3388 udev->u1_params.timeout = timeout;
3390 udev->u2_params.timeout = timeout;
3406 static void usb_enable_link_state(
struct usb_hcd *hcd,
struct usb_device *udev,
3410 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3411 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3425 ret = usb_req_set_sel(udev, state);
3427 dev_warn(&udev->dev,
"Set SEL for device-initiated %s failed.\n",
3428 usb3_lpm_names[state]);
3437 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3444 dev_warn(&udev->dev,
"Could not enable %s link state, "
3445 "xHCI error %i.\n", usb3_lpm_names[state],
3450 if (usb_set_lpm_timeout(udev, state, timeout))
3455 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3458 else if (udev->actconfig)
3459 usb_set_device_initiated_lpm(udev, state,
true);
3478 static int usb_disable_link_state(
struct usb_hcd *hcd,
struct usb_device *udev,
3491 dev_warn(&udev->dev,
"%s: Can't disable non-U1 or U2 state.\n",
3496 if (usb_set_lpm_timeout(udev, state, 0))
3499 usb_set_device_initiated_lpm(udev, state,
false);
3501 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3502 dev_warn(&udev->dev,
"Could not disable xHCI %s timeout, "
3503 "bus schedule bandwidth may be impacted.\n",
3504 usb3_lpm_names[state]);
3517 struct usb_hcd *hcd;
3519 if (!udev || !udev->parent ||
3524 hcd = bus_to_hcd(udev->bus);
3525 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3528 udev->lpm_disable_count++;
3529 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
3533 if (usb_disable_link_state(hcd, udev,
USB3_LPM_U1))
3535 if (usb_disable_link_state(hcd, udev,
USB3_LPM_U2))
3549 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3573 struct usb_hcd *hcd;
3575 if (!udev || !udev->parent ||
3580 udev->lpm_disable_count--;
3581 hcd = bus_to_hcd(udev->bus);
3585 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3586 !hcd->driver->disable_usb3_lpm_timeout)
3589 if (udev->lpm_disable_count > 0)
3600 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3614 #define hub_suspend NULL
3615 #define hub_resume NULL
3616 #define hub_reset_resume NULL
3662 static int hub_port_debounce(
struct usb_hub *hub,
int port1)
3665 int total_time, stable_time = 0;
3666 u16 portchange, portstatus;
3670 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3674 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3675 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3684 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3685 clear_port_feature(hub->
hdev, port1,
3695 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3696 port1, total_time, stable_time, portstatus);
3711 #define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3712 #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3714 static int hub_set_address(
struct usb_device *udev,
int devnum)
3717 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3723 if (!hcd->driver->address_device && devnum <= 1)
3729 if (hcd->driver->address_device)
3730 retval = hcd->driver->address_device(hcd, udev);
3734 NULL, 0, USB_CTRL_SET_TIMEOUT);
3736 update_devnum(udev, devnum);
3754 hub_port_init (
struct usb_hub *hub,
struct usb_device *udev,
int port1,
3759 struct usb_device *hdev = hub->
hdev;
3760 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
3765 int devnum = udev->devnum;
3770 if (!hdev->parent) {
3772 if (port1 == hdev->bus->otg_port)
3773 hdev->bus->b_hnp_enable = 0;
3785 retval = hub_port_reset(hub, port1, udev, delay,
false);
3793 dev_dbg(&udev->dev,
"device reset changed speed!\n");
3796 oldspeed = udev->speed;
3803 switch (udev->speed) {
3826 speed =
"variable speed Wireless";
3832 "%s %s USB device number %d using %s\n",
3833 (udev->config) ?
"reset" :
"new", speed,
3834 devnum, udev->bus->controller->driver->name);
3838 udev->tt = hdev->tt;
3839 udev->ttport = hdev->ttport;
3843 dev_err(&udev->dev,
"parent hub has no TT\n");
3847 udev->tt = &hub->
tt;
3848 udev->ttport = port1;
3864 if (
USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
3868 #define GET_DESCRIPTOR_BUFSIZE 64
3879 for (j = 0; j < 3; ++
j) {
3884 buf, GET_DESCRIPTOR_BUFSIZE,
3885 initial_descriptor_timeout);
3887 case 8:
case 16:
case 32:
case 64:
case 255:
3902 udev->descriptor.bMaxPacketSize0 =
3906 retval = hub_port_reset(hub, port1, udev, delay,
false);
3909 if (oldspeed != udev->speed) {
3911 "device reset changed speed!\n");
3917 "device descriptor read/64, error %d\n",
3922 #undef GET_DESCRIPTOR_BUFSIZE
3930 if (udev->wusb == 0) {
3932 retval = hub_set_address(udev, devnum);
3939 "device not accepting address %d, error %d\n",
3944 devnum = udev->devnum;
3946 "%s SuperSpeed USB device number %d using %s\n",
3947 (udev->config) ?
"reset" :
"new",
3948 devnum, udev->bus->controller->driver->name);
3956 if (
USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
3963 "device descriptor read/8, error %d\n",
3982 (
le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
3983 dev_err(&udev->dev,
"got a wrong device descriptor, "
3984 "warm reset device\n");
3985 hub_port_reset(hub, port1, udev,
3991 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
3995 i = udev->descriptor.bMaxPacketSize0;
3996 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
3998 !(i == 8 || i == 16 || i == 32 || i == 64)) {
3999 dev_err(&udev->dev,
"Invalid ep0 maxpacket: %d\n", i);
4004 dev_dbg(&udev->dev,
"ep0 maxpacket = %d\n", i);
4006 dev_warn(&udev->dev,
"Using ep0 maxpacket: %d\n", i);
4012 if (retval < (
signed)
sizeof(udev->descriptor)) {
4013 dev_err(&udev->dev,
"device descriptor read/all, error %d\n",
4020 if (udev->wusb == 0 &&
le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4023 udev->lpm_capable = usb_device_supports_lpm(udev);
4024 usb_set_lpm_parameters(udev);
4030 if (hcd->driver->update_device)
4031 hcd->driver->update_device(hcd, udev);
4034 hub_port_disable(hub, port1, 0);
4035 update_devnum(udev, devnum);
4042 check_highspeed (
struct usb_hub *hub,
struct usb_device *udev,
int port1)
4052 qual,
sizeof *qual);
4053 if (status ==
sizeof *qual) {
4054 dev_info(&udev->dev,
"not running at top speed; "
4055 "connect to a high speed hub\n");
4066 hub_power_remaining (
struct usb_hub *hub)
4068 struct usb_device *hdev = hub->
hdev;
4075 remaining = hdev->bus_mA - hub->
descriptor->bHubContrCurrent;
4076 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
4077 struct usb_device *udev = hub->
ports[port1 - 1]->child;
4085 if (udev->actconfig)
4086 delta = udev->actconfig->desc.bMaxPower * 2;
4087 else if (port1 != udev->bus->otg_port || hdev->parent)
4093 "%dmA is over %umA budget for port %d!\n",
4097 if (remaining < 0) {
4113 static void hub_port_connect_change(
struct usb_hub *hub,
int port1,
4114 u16 portstatus,
u16 portchange)
4116 struct usb_device *hdev = hub->
hdev;
4118 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
4119 unsigned wHubCharacteristics =
4121 struct usb_device *
udev;
4125 "port %d, status %04x, change %04x, %s\n",
4126 port1, portstatus, portchange, portspeed(hub, portstatus));
4133 #ifdef CONFIG_USB_OTG
4135 if (hdev->bus->is_b_host)
4136 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4141 udev = hub->
ports[port1 - 1]->child;
4142 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4144 usb_lock_device(udev);
4145 if (portstatus & USB_PORT_STAT_ENABLE) {
4148 #ifdef CONFIG_USB_SUSPEND
4150 udev->persist_enabled) {
4154 status = usb_remote_wakeup(udev);
4160 usb_unlock_device(udev);
4176 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4177 (portchange & USB_PORT_STAT_C_CONNECTION))
4180 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4181 USB_PORT_STAT_C_ENABLE)) {
4182 status = hub_port_debounce(hub, port1);
4184 if (printk_ratelimit())
4185 dev_err(hub_dev,
"connect-debounce failed, "
4186 "port %d disabled\n", port1);
4187 portstatus &= ~USB_PORT_STAT_CONNECTION;
4193 if (hcd->phy && !hdev->parent) {
4194 if (portstatus & USB_PORT_STAT_CONNECTION)
4195 usb_phy_notify_connect(hcd->phy, port1);
4197 usb_phy_notify_disconnect(hcd->phy, port1);
4203 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4207 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
4208 && !port_is_power_on(hub, portstatus))
4211 if (portstatus & USB_PORT_STAT_ENABLE)
4224 "couldn't allocate port %d usb_device\n",
4231 udev->level = hdev->level + 1;
4232 udev->wusb = hub_is_wusb(hub);
4235 if (hub_is_superspeed(hub->
hdev))
4240 choose_devnum(udev);
4241 if (udev->devnum <= 0) {
4247 status = hub_port_init(hub, udev, port1, i);
4262 && udev->bus_mA <= 100) {
4268 dev_dbg(&udev->dev,
"get status %d ?\n", status);
4274 "can't connect bus-powered hub "
4287 if (
le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4289 && highspeed_hubs != 0)
4290 check_highspeed (hub, udev, port1);
4302 spin_lock_irq(&device_state_lock);
4307 spin_unlock_irq(&device_state_lock);
4313 spin_lock_irq(&device_state_lock);
4315 spin_unlock_irq(&device_state_lock);
4322 status = hub_power_remaining(hub);
4324 dev_dbg(hub_dev,
"%dmA power budget left\n", status);
4329 hub_port_disable(hub, port1, 1);
4332 release_devnum(udev);
4338 if (hub->
hdev->parent ||
4339 !hcd->driver->port_handed_over ||
4340 !(hcd->driver->port_handed_over)(hcd, port1))
4341 dev_err(hub_dev,
"unable to enumerate USB device on port %d\n",
4345 hub_port_disable(hub, port1, 1);
4346 if (hcd->driver->relinquish_port && !hub->
hdev->parent)
4347 hcd->driver->relinquish_port(hcd, port1);
4351 static int hub_handle_remote_wakeup(
struct usb_hub *hub,
unsigned int port,
4352 u16 portstatus,
u16 portchange)
4354 struct usb_device *hdev;
4355 struct usb_device *
udev;
4356 int connect_change = 0;
4360 udev = hub->
ports[port - 1]->child;
4361 if (!hub_is_superspeed(hdev)) {
4376 usb_lock_device(udev);
4377 ret = usb_remote_wakeup(udev);
4378 usb_unlock_device(udev);
4383 hub_port_disable(hub, port, 1);
4387 return connect_change;
4390 static void hub_events(
void)
4393 struct usb_device *hdev;
4402 int connect_change, wakeup_change;
4413 spin_lock_irq(&hub_event_lock);
4414 if (list_empty(&hub_event_list)) {
4415 spin_unlock_irq(&hub_event_lock);
4419 tmp = hub_event_list.
next;
4423 kref_get(&hub->
kref);
4424 spin_unlock_irq(&hub_event_lock);
4428 intf = to_usb_interface(hub_dev);
4429 dev_dbg(hub_dev,
"state %d ports %d chg %04x evt %04x\n",
4439 usb_lock_device(hdev);
4441 goto loop_disconnected;
4451 ret = usb_autopm_get_interface(intf);
4453 dev_dbg(hub_dev,
"Can't autoresume: %d\n", ret);
4462 dev_dbg (hub_dev,
"resetting for error %d\n",
4468 "error resetting hub: %d\n", ret);
4477 for (i = 1; i <= hub->
descriptor->bNbrPorts; i++) {
4483 !connect_change && !wakeup_change)
4486 ret = hub_port_status(hub, i,
4487 &portstatus, &portchange);
4491 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4492 clear_port_feature(hdev, i,
4497 if (portchange & USB_PORT_STAT_C_ENABLE) {
4498 if (!connect_change)
4500 "port %d enable change, "
4503 clear_port_feature(hdev, i,
4512 if (!(portstatus & USB_PORT_STAT_ENABLE)
4514 && hub->
ports[i - 1]->child) {
4517 "disabled by hub (EMI?), "
4524 if (hub_handle_remote_wakeup(hub, i,
4525 portstatus, portchange))
4532 dev_dbg(hub_dev,
"over-current change on port "
4534 clear_port_feature(hdev, i,
4537 hub_power_on(hub,
true);
4538 hub_port_status(hub, i, &status, &unused);
4540 dev_err(hub_dev,
"over-current "
4541 "condition on port %d\n", i);
4544 if (portchange & USB_PORT_STAT_C_RESET) {
4546 "reset change on port %d\n",
4548 clear_port_feature(hdev, i,
4551 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4552 hub_is_superspeed(hub->
hdev)) {
4554 "warm reset change on port %d\n",
4556 clear_port_feature(hdev, i,
4559 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4560 clear_port_feature(hub->
hdev, i,
4565 "config error on port %d\n",
4567 clear_port_feature(hub->
hdev, i,
4574 if (hub_port_warm_reset_required(hub, portstatus)) {
4575 dev_dbg(hub_dev,
"warm reset port %d\n", i);
4576 hub_port_reset(hub, i, NULL,
4581 hub_port_connect_change(hub, i,
4582 portstatus, portchange);
4588 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4589 dev_err (hub_dev,
"get_hub_status failed\n");
4592 dev_dbg (hub_dev,
"power change\n");
4604 dev_dbg(hub_dev,
"over-current change\n");
4607 hub_power_on(hub,
true);
4608 hub_hub_status(hub, &status, &unused);
4610 dev_err(hub_dev,
"over-current "
4617 usb_autopm_put_interface_no_suspend(intf);
4622 usb_autopm_put_interface(intf);
4624 usb_unlock_device(hdev);
4625 kref_put(&hub->
kref, hub_release);
4630 static int hub_thread(
void *
__unused)
4642 !list_empty(&hub_event_list) ||
4660 static struct usb_driver hub_driver = {
4663 .disconnect = hub_disconnect,
4667 .pre_reset = hub_pre_reset,
4668 .post_reset = hub_post_reset,
4669 .unlocked_ioctl = hub_ioctl,
4670 .id_table = hub_id_table,
4671 .supports_autosuspend = 1,
4676 if (usb_register(&hub_driver) < 0) {
4682 khubd_task =
kthread_run(hub_thread, NULL,
"khubd");
4683 if (!IS_ERR(khubd_task))
4707 static int descriptors_changed(
struct usb_device *udev,
4712 unsigned serial_len = 0;
4714 unsigned old_length;
4718 if (
memcmp(&udev->descriptor, old_device_descriptor,
4719 sizeof(*old_device_descriptor)) != 0)
4729 serial_len =
strlen(udev->serial) + 1;
4732 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
4733 old_length =
le16_to_cpu(udev->config[index].desc.wTotalLength);
4734 len =
max(len, old_length);
4739 dev_err(&udev->dev,
"no mem to re-read configs after reset\n");
4743 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
4744 old_length =
le16_to_cpu(udev->config[index].desc.wTotalLength);
4747 if (length != old_length) {
4748 dev_dbg(&udev->dev,
"config index %d, error %d\n",
4753 if (
memcmp (buf, udev->rawdescriptors[index], old_length)
4755 dev_dbg(&udev->dev,
"config index %d changed (#%d)\n",
4764 if (!changed && serial_len) {
4765 length =
usb_string(udev, udev->descriptor.iSerialNumber,
4767 if (length + 1 != serial_len) {
4768 dev_dbg(&udev->dev,
"serial string error %d\n",
4771 }
else if (
memcmp(buf, udev->serial, length) != 0) {
4772 dev_dbg(&udev->dev,
"serial string changed\n");
4811 static int usb_reset_and_verify_device(
struct usb_device *udev)
4813 struct usb_device *parent_hdev = udev->parent;
4815 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4818 int port1 = udev->portnum;
4822 dev_dbg(&udev->dev,
"device reset not allowed in state %d\n",
4829 dev_dbg(&udev->dev,
"%s for root hub!\n", __func__);
4832 parent_hub = hdev_to_hub(parent_hdev);
4841 dev_err(&udev->dev,
"%s Failed to disable LPM\n.", __func__);
4846 dev_err(&udev->dev,
"%s Failed to disable LTM\n.",
4857 ret = hub_port_init(parent_hub, udev, port1, i);
4867 if (descriptors_changed(udev, &descriptor)) {
4868 dev_info(&udev->dev,
"device firmware changed\n");
4874 if (!udev->actconfig)
4881 "Busted HC? Not enough HCD resources for "
4882 "old configuration.\n");
4888 udev->actconfig->desc.bConfigurationValue, 0,
4889 NULL, 0, USB_CTRL_SET_TIMEOUT);
4892 "can't restore configuration #%d (error=%d)\n",
4893 udev->actconfig->desc.bConfigurationValue, ret);
4906 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
4907 struct usb_host_config *
config = udev->actconfig;
4911 desc = &intf->cur_altsetting->desc;
4921 intf->resetting_device = 1;
4924 intf->resetting_device = 0;
4927 dev_err(&udev->dev,
"failed to restore interface %d "
4928 "altsetting %d (error=%d)\n",
4944 hub_port_logical_disconnect(parent_hub, port1);
4972 struct usb_host_config *config = udev->actconfig;
4976 dev_dbg(&udev->dev,
"device reset not allowed in state %d\n",
4982 usb_autoresume_device(udev);
4985 for (i = 0; i < config->desc.bNumInterfaces; ++
i) {
4987 struct usb_driver *drv;
4990 if (cintf->dev.driver) {
4991 drv = to_usb_driver(cintf->dev.driver);
4992 if (drv->pre_reset && drv->post_reset)
4993 unbind = (drv->pre_reset)(cintf);
4994 else if (cintf->condition ==
4995 USB_INTERFACE_BOUND)
5003 ret = usb_reset_and_verify_device(udev);
5006 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
5008 struct usb_driver *drv;
5009 int rebind = cintf->needs_binding;
5011 if (!rebind && cintf->dev.driver) {
5012 drv = to_usb_driver(cintf->dev.driver);
5013 if (drv->post_reset)
5014 rebind = (drv->post_reset)(cintf);
5015 else if (cintf->condition ==
5016 USB_INTERFACE_BOUND)
5019 if (ret == 0 && rebind)
5088 struct usb_hub *hub = hdev_to_hub(hdev);
5090 if (port1 < 1 || port1 > hdev->maxchild)
5092 return hub->
ports[port1 - 1]->child;
5103 enum usb_port_connect_type type)
5105 struct usb_hub *hub = hdev_to_hub(hdev);
5107 hub->
ports[port1 - 1]->connect_type =
type;
5118 enum usb_port_connect_type
5121 struct usb_hub *hub = hdev_to_hub(hdev);
5123 return hub->
ports[port1 - 1]->connect_type;
5135 acpi_handle usb_get_hub_port_acpi_handle(
struct usb_device *hdev,
5138 struct usb_hub *hub = hdev_to_hub(hdev);
5140 return DEVICE_ACPI_HANDLE(&hub->
ports[port1 - 1]->dev);