4 * (C) Copyright 1999 Linus Torvalds
5 * (C) Copyright 1999 Johannes Erdfelt
6 * (C) Copyright 1999 Gregory P. Smith
7 * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/completion.h>
16 #include <linux/sched.h>
17 #include <linux/list.h>
18 #include <linux/slab.h>
19 #include <linux/ioctl.h>
20 #include <linux/usb.h>
21 #include <linux/usbdevice_fs.h>
22 #include <linux/usb/hcd.h>
23 #include <linux/usb/otg.h>
24 #include <linux/usb/quirks.h>
25 #include <linux/kthread.h>
26 #include <linux/mutex.h>
27 #include <linux/freezer.h>
28 #include <linux/random.h>
30 #include <asm/uaccess.h>
31 #include <asm/byteorder.h>
35 /* if we are in debug mode, always announce new devices */
37 #ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
38 #define CONFIG_USB_ANNOUNCE_NEW_DEVICES
43 struct usb_device *child;
45 struct dev_state *port_owner;
49 struct device *intfdev; /* the "interface" device */
50 struct usb_device *hdev;
52 struct urb *urb; /* for interrupt polling pipe */
54 /* buffer for urb ... with extra space in case of babble */
57 struct usb_hub_status hub;
58 struct usb_port_status port;
59 } *status; /* buffer for status reports */
60 struct mutex status_mutex; /* for the status buffer */
62 int error; /* last reported error */
63 int nerrors; /* track consecutive errors */
65 struct list_head event_list; /* hubs w/data or errs ready */
66 unsigned long event_bits[1]; /* status change bitmask */
67 unsigned long change_bits[1]; /* ports with logical connect
69 unsigned long busy_bits[1]; /* ports being reset or
71 unsigned long removed_bits[1]; /* ports with a "removed"
73 unsigned long wakeup_bits[1]; /* ports that have signaled
75 #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
76 #error event_bits[] is too short!
79 struct usb_hub_descriptor *descriptor; /* class descriptor */
80 struct usb_tt tt; /* Transaction Translator */
82 unsigned mA_per_port; /* current for each child */
84 unsigned limited_power:1;
86 unsigned disconnected:1;
88 unsigned has_indicators:1;
89 u8 indicator[USB_MAXCHILDREN];
90 struct delayed_work leds;
91 struct delayed_work init_work;
92 struct usb_port **ports;
95 static inline int hub_is_superspeed(struct usb_device *hdev)
97 return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
100 /* Protect struct usb_device->state and ->children members
101 * Note: Both are also protected by ->dev.sem, except that ->state can
102 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
103 static DEFINE_SPINLOCK(device_state_lock);
105 /* khubd's worklist and its lock */
106 static DEFINE_SPINLOCK(hub_event_lock);
107 static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
110 static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
112 static struct task_struct *khubd_task;
114 /* cycle leds on hubs that aren't blinking for attention */
115 static bool blinkenlights = 0;
116 module_param (blinkenlights, bool, S_IRUGO);
117 MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
120 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
121 * 10 seconds to send reply for the initial 64-byte descriptor request.
123 /* define initial 64-byte descriptor request timeout in milliseconds */
124 static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
125 module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
126 MODULE_PARM_DESC(initial_descriptor_timeout,
127 "initial 64-byte descriptor request timeout in milliseconds "
128 "(default 5000 - 5.0 seconds)");
131 * As of 2.6.10 we introduce a new USB device initialization scheme which
132 * closely resembles the way Windows works. Hopefully it will be compatible
133 * with a wider range of devices than the old scheme. However some previously
134 * working devices may start giving rise to "device not accepting address"
135 * errors; if that happens the user can try the old scheme by adjusting the
136 * following module parameters.
138 * For maximum flexibility there are two boolean parameters to control the
139 * hub driver's behavior. On the first initialization attempt, if the
140 * "old_scheme_first" parameter is set then the old scheme will be used,
141 * otherwise the new scheme is used. If that fails and "use_both_schemes"
142 * is set, then the driver will make another attempt, using the other scheme.
144 static bool old_scheme_first = 0;
145 module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
146 MODULE_PARM_DESC(old_scheme_first,
147 "start with the old device initialization scheme");
149 static bool use_both_schemes = 1;
150 module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
151 MODULE_PARM_DESC(use_both_schemes,
152 "try the other device initialization scheme if the "
155 /* Mutual exclusion for EHCI CF initialization. This interferes with
156 * port reset on some companion controllers.
158 DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
159 EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
161 #define HUB_DEBOUNCE_TIMEOUT 1500
162 #define HUB_DEBOUNCE_STEP 25
163 #define HUB_DEBOUNCE_STABLE 100
165 #define to_usb_port(_dev) \
166 container_of(_dev, struct usb_port, dev)
168 static int usb_reset_and_verify_device(struct usb_device *udev);
170 static inline char *portspeed(struct usb_hub *hub, int portstatus)
172 if (hub_is_superspeed(hub->hdev))
174 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
176 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
182 /* Note that hdev or one of its children must be locked! */
183 static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
185 if (!hdev || !hdev->actconfig || !hdev->maxchild)
187 return usb_get_intfdata(hdev->actconfig->interface[0]);
190 static int usb_device_supports_lpm(struct usb_device *udev)
192 /* USB 2.1 (and greater) devices indicate LPM support through
193 * their USB 2.0 Extended Capabilities BOS descriptor.
195 if (udev->speed == USB_SPEED_HIGH) {
196 if (udev->bos->ext_cap &&
198 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
203 /* All USB 3.0 must support LPM, but we need their max exit latency
204 * information from the SuperSpeed Extended Capabilities BOS descriptor.
206 if (!udev->bos->ss_cap) {
207 dev_warn(&udev->dev, "No LPM exit latency info found. "
208 "Power management will be impacted.\n");
211 if (udev->parent->lpm_capable)
214 dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. "
215 "Power management will be impacted.\n");
220 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
223 static void usb_set_lpm_mel(struct usb_device *udev,
224 struct usb3_lpm_parameters *udev_lpm_params,
225 unsigned int udev_exit_latency,
227 struct usb3_lpm_parameters *hub_lpm_params,
228 unsigned int hub_exit_latency)
230 unsigned int total_mel;
231 unsigned int device_mel;
232 unsigned int hub_mel;
235 * Calculate the time it takes to transition all links from the roothub
236 * to the parent hub into U0. The parent hub must then decode the
237 * packet (hub header decode latency) to figure out which port it was
240 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
241 * means 0.1us). Multiply that by 100 to get nanoseconds.
243 total_mel = hub_lpm_params->mel +
244 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
247 * How long will it take to transition the downstream hub's port into
248 * U0? The greater of either the hub exit latency or the device exit
251 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
252 * Multiply that by 1000 to get nanoseconds.
254 device_mel = udev_exit_latency * 1000;
255 hub_mel = hub_exit_latency * 1000;
256 if (device_mel > hub_mel)
257 total_mel += device_mel;
259 total_mel += hub_mel;
261 udev_lpm_params->mel = total_mel;
265 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
266 * a transition from either U1 or U2.
268 static void usb_set_lpm_pel(struct usb_device *udev,
269 struct usb3_lpm_parameters *udev_lpm_params,
270 unsigned int udev_exit_latency,
272 struct usb3_lpm_parameters *hub_lpm_params,
273 unsigned int hub_exit_latency,
274 unsigned int port_to_port_exit_latency)
276 unsigned int first_link_pel;
277 unsigned int hub_pel;
280 * First, the device sends an LFPS to transition the link between the
281 * device and the parent hub into U0. The exit latency is the bigger of
282 * the device exit latency or the hub exit latency.
284 if (udev_exit_latency > hub_exit_latency)
285 first_link_pel = udev_exit_latency * 1000;
287 first_link_pel = hub_exit_latency * 1000;
290 * When the hub starts to receive the LFPS, there is a slight delay for
291 * it to figure out that one of the ports is sending an LFPS. Then it
292 * will forward the LFPS to its upstream link. The exit latency is the
293 * delay, plus the PEL that we calculated for this hub.
295 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
298 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
299 * is the greater of the two exit latencies.
301 if (first_link_pel > hub_pel)
302 udev_lpm_params->pel = first_link_pel;
304 udev_lpm_params->pel = hub_pel;
308 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
309 * when a device initiates a transition to U0, until when it will receive the
310 * first packet from the host controller.
312 * Section C.1.5.1 describes the four components to this:
314 * - t2: time for the ERDY to make it from the device to the host.
315 * - t3: a host-specific delay to process the ERDY.
316 * - t4: time for the packet to make it from the host to the device.
318 * t3 is specific to both the xHCI host and the platform the host is integrated
319 * into. The Intel HW folks have said it's negligible, FIXME if a different
320 * vendor says otherwise.
322 static void usb_set_lpm_sel(struct usb_device *udev,
323 struct usb3_lpm_parameters *udev_lpm_params)
325 struct usb_device *parent;
326 unsigned int num_hubs;
327 unsigned int total_sel;
329 /* t1 = device PEL */
330 total_sel = udev_lpm_params->pel;
331 /* How many external hubs are in between the device & the root port. */
332 for (parent = udev->parent, num_hubs = 0; parent->parent;
333 parent = parent->parent)
335 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
337 total_sel += 2100 + 250 * (num_hubs - 1);
339 /* t4 = 250ns * num_hubs */
340 total_sel += 250 * num_hubs;
342 udev_lpm_params->sel = total_sel;
345 static void usb_set_lpm_parameters(struct usb_device *udev)
348 unsigned int port_to_port_delay;
349 unsigned int udev_u1_del;
350 unsigned int udev_u2_del;
351 unsigned int hub_u1_del;
352 unsigned int hub_u2_del;
354 if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
357 hub = hdev_to_hub(udev->parent);
358 /* It doesn't take time to transition the roothub into U0, since it
359 * doesn't have an upstream link.
364 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
365 udev_u2_del = udev->bos->ss_cap->bU2DevExitLat;
366 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
367 hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat;
369 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
370 hub, &udev->parent->u1_params, hub_u1_del);
372 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
373 hub, &udev->parent->u2_params, hub_u2_del);
376 * Appendix C, section C.2.2.2, says that there is a slight delay from
377 * when the parent hub notices the downstream port is trying to
378 * transition to U0 to when the hub initiates a U0 transition on its
379 * upstream port. The section says the delays are tPort2PortU1EL and
380 * tPort2PortU2EL, but it doesn't define what they are.
382 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
383 * about the same delays. Use the maximum delay calculations from those
384 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
385 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
386 * assume the device exit latencies they are talking about are the hub
389 * What do we do if the U2 exit latency is less than the U1 exit
390 * latency? It's possible, although not likely...
392 port_to_port_delay = 1;
394 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
395 hub, &udev->parent->u1_params, hub_u1_del,
398 if (hub_u2_del > hub_u1_del)
399 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
401 port_to_port_delay = 1 + hub_u1_del;
403 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
404 hub, &udev->parent->u2_params, hub_u2_del,
407 /* Now that we've got PEL, calculate SEL. */
408 usb_set_lpm_sel(udev, &udev->u1_params);
409 usb_set_lpm_sel(udev, &udev->u2_params);
412 /* USB 2.0 spec Section 11.24.4.5 */
413 static int get_hub_descriptor(struct usb_device *hdev, void *data)
418 if (hub_is_superspeed(hdev)) {
419 dtype = USB_DT_SS_HUB;
420 size = USB_DT_SS_HUB_SIZE;
423 size = sizeof(struct usb_hub_descriptor);
426 for (i = 0; i < 3; i++) {
427 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
428 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
429 dtype << 8, 0, data, size,
430 USB_CTRL_GET_TIMEOUT);
431 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
438 * USB 2.0 spec Section 11.24.2.1
440 static int clear_hub_feature(struct usb_device *hdev, int feature)
442 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
443 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
447 * USB 2.0 spec Section 11.24.2.2
449 static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
451 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
452 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
457 * USB 2.0 spec Section 11.24.2.13
459 static int set_port_feature(struct usb_device *hdev, int port1, int feature)
461 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
462 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
467 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
468 * for info about using port indicators
470 static void set_port_led(
476 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
477 USB_PORT_FEAT_INDICATOR);
479 dev_dbg (hub->intfdev,
480 "port %d indicator %s status %d\n",
482 ({ char *s; switch (selector) {
483 case HUB_LED_AMBER: s = "amber"; break;
484 case HUB_LED_GREEN: s = "green"; break;
485 case HUB_LED_OFF: s = "off"; break;
486 case HUB_LED_AUTO: s = "auto"; break;
487 default: s = "??"; break;
492 #define LED_CYCLE_PERIOD ((2*HZ)/3)
494 static void led_work (struct work_struct *work)
496 struct usb_hub *hub =
497 container_of(work, struct usb_hub, leds.work);
498 struct usb_device *hdev = hub->hdev;
500 unsigned changed = 0;
503 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
506 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
507 unsigned selector, mode;
509 /* 30%-50% duty cycle */
511 switch (hub->indicator[i]) {
513 case INDICATOR_CYCLE:
515 selector = HUB_LED_AUTO;
516 mode = INDICATOR_AUTO;
518 /* blinking green = sw attention */
519 case INDICATOR_GREEN_BLINK:
520 selector = HUB_LED_GREEN;
521 mode = INDICATOR_GREEN_BLINK_OFF;
523 case INDICATOR_GREEN_BLINK_OFF:
524 selector = HUB_LED_OFF;
525 mode = INDICATOR_GREEN_BLINK;
527 /* blinking amber = hw attention */
528 case INDICATOR_AMBER_BLINK:
529 selector = HUB_LED_AMBER;
530 mode = INDICATOR_AMBER_BLINK_OFF;
532 case INDICATOR_AMBER_BLINK_OFF:
533 selector = HUB_LED_OFF;
534 mode = INDICATOR_AMBER_BLINK;
536 /* blink green/amber = reserved */
537 case INDICATOR_ALT_BLINK:
538 selector = HUB_LED_GREEN;
539 mode = INDICATOR_ALT_BLINK_OFF;
541 case INDICATOR_ALT_BLINK_OFF:
542 selector = HUB_LED_AMBER;
543 mode = INDICATOR_ALT_BLINK;
548 if (selector != HUB_LED_AUTO)
550 set_port_led(hub, i + 1, selector);
551 hub->indicator[i] = mode;
553 if (!changed && blinkenlights) {
555 cursor %= hub->descriptor->bNbrPorts;
556 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
557 hub->indicator[cursor] = INDICATOR_CYCLE;
561 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
564 /* use a short timeout for hub/port status fetches */
565 #define USB_STS_TIMEOUT 1000
566 #define USB_STS_RETRIES 5
569 * USB 2.0 spec Section 11.24.2.6
571 static int get_hub_status(struct usb_device *hdev,
572 struct usb_hub_status *data)
574 int i, status = -ETIMEDOUT;
576 for (i = 0; i < USB_STS_RETRIES &&
577 (status == -ETIMEDOUT || status == -EPIPE); i++) {
578 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
579 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
580 data, sizeof(*data), USB_STS_TIMEOUT);
586 * USB 2.0 spec Section 11.24.2.7
588 static int get_port_status(struct usb_device *hdev, int port1,
589 struct usb_port_status *data)
591 int i, status = -ETIMEDOUT;
593 for (i = 0; i < USB_STS_RETRIES &&
594 (status == -ETIMEDOUT || status == -EPIPE); i++) {
595 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
596 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
597 data, sizeof(*data), USB_STS_TIMEOUT);
602 static int hub_port_status(struct usb_hub *hub, int port1,
603 u16 *status, u16 *change)
607 mutex_lock(&hub->status_mutex);
608 ret = get_port_status(hub->hdev, port1, &hub->status->port);
610 dev_err(hub->intfdev,
611 "%s failed (err = %d)\n", __func__, ret);
615 *status = le16_to_cpu(hub->status->port.wPortStatus);
616 *change = le16_to_cpu(hub->status->port.wPortChange);
620 mutex_unlock(&hub->status_mutex);
624 static void kick_khubd(struct usb_hub *hub)
628 spin_lock_irqsave(&hub_event_lock, flags);
629 if (!hub->disconnected && list_empty(&hub->event_list)) {
630 list_add_tail(&hub->event_list, &hub_event_list);
632 /* Suppress autosuspend until khubd runs */
633 usb_autopm_get_interface_no_resume(
634 to_usb_interface(hub->intfdev));
635 wake_up(&khubd_wait);
637 spin_unlock_irqrestore(&hub_event_lock, flags);
640 void usb_kick_khubd(struct usb_device *hdev)
642 struct usb_hub *hub = hdev_to_hub(hdev);
649 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
650 * Notification, which indicates it had initiated remote wakeup.
652 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
653 * device initiates resume, so the USB core will not receive notice of the
654 * resume through the normal hub interrupt URB.
656 void usb_wakeup_notification(struct usb_device *hdev,
657 unsigned int portnum)
664 hub = hdev_to_hub(hdev);
666 set_bit(portnum, hub->wakeup_bits);
670 EXPORT_SYMBOL_GPL(usb_wakeup_notification);
672 /* completion function, fires on port status changes and various faults */
673 static void hub_irq(struct urb *urb)
675 struct usb_hub *hub = urb->context;
676 int status = urb->status;
681 case -ENOENT: /* synchronous unlink */
682 case -ECONNRESET: /* async unlink */
683 case -ESHUTDOWN: /* hardware going away */
686 default: /* presumably an error */
687 /* Cause a hub reset after 10 consecutive errors */
688 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
689 if ((++hub->nerrors < 10) || hub->error)
694 /* let khubd handle things */
695 case 0: /* we got data: port status changed */
697 for (i = 0; i < urb->actual_length; ++i)
698 bits |= ((unsigned long) ((*hub->buffer)[i]))
700 hub->event_bits[0] = bits;
706 /* Something happened, let khubd figure it out */
713 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
714 && status != -ENODEV && status != -EPERM)
715 dev_err (hub->intfdev, "resubmit --> %d\n", status);
718 /* USB 2.0 spec Section 11.24.2.3 */
720 hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
722 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
723 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
728 * enumeration blocks khubd for a long time. we use keventd instead, since
729 * long blocking there is the exception, not the rule. accordingly, HCDs
730 * talking to TTs must queue control transfers (not just bulk and iso), so
731 * both can talk to the same hub concurrently.
733 static void hub_tt_work(struct work_struct *work)
735 struct usb_hub *hub =
736 container_of(work, struct usb_hub, tt.clear_work);
740 spin_lock_irqsave (&hub->tt.lock, flags);
741 while (--limit && !list_empty (&hub->tt.clear_list)) {
742 struct list_head *next;
743 struct usb_tt_clear *clear;
744 struct usb_device *hdev = hub->hdev;
745 const struct hc_driver *drv;
748 next = hub->tt.clear_list.next;
749 clear = list_entry (next, struct usb_tt_clear, clear_list);
750 list_del (&clear->clear_list);
752 /* drop lock so HCD can concurrently report other TT errors */
753 spin_unlock_irqrestore (&hub->tt.lock, flags);
754 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
757 "clear tt %d (%04x) error %d\n",
758 clear->tt, clear->devinfo, status);
760 /* Tell the HCD, even if the operation failed */
761 drv = clear->hcd->driver;
762 if (drv->clear_tt_buffer_complete)
763 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
766 spin_lock_irqsave(&hub->tt.lock, flags);
768 spin_unlock_irqrestore (&hub->tt.lock, flags);
772 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
773 * @urb: an URB associated with the failed or incomplete split transaction
775 * High speed HCDs use this to tell the hub driver that some split control or
776 * bulk transaction failed in a way that requires clearing internal state of
777 * a transaction translator. This is normally detected (and reported) from
780 * It may not be possible for that hub to handle additional full (or low)
781 * speed transactions until that state is fully cleared out.
783 int usb_hub_clear_tt_buffer(struct urb *urb)
785 struct usb_device *udev = urb->dev;
786 int pipe = urb->pipe;
787 struct usb_tt *tt = udev->tt;
789 struct usb_tt_clear *clear;
791 /* we've got to cope with an arbitrary number of pending TT clears,
792 * since each TT has "at least two" buffers that can need it (and
793 * there can be many TTs per hub). even if they're uncommon.
795 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
796 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
797 /* FIXME recover somehow ... RESET_TT? */
801 /* info that CLEAR_TT_BUFFER needs */
802 clear->tt = tt->multi ? udev->ttport : 1;
803 clear->devinfo = usb_pipeendpoint (pipe);
804 clear->devinfo |= udev->devnum << 4;
805 clear->devinfo |= usb_pipecontrol (pipe)
806 ? (USB_ENDPOINT_XFER_CONTROL << 11)
807 : (USB_ENDPOINT_XFER_BULK << 11);
808 if (usb_pipein (pipe))
809 clear->devinfo |= 1 << 15;
811 /* info for completion callback */
812 clear->hcd = bus_to_hcd(udev->bus);
815 /* tell keventd to clear state for this TT */
816 spin_lock_irqsave (&tt->lock, flags);
817 list_add_tail (&clear->clear_list, &tt->clear_list);
818 schedule_work(&tt->clear_work);
819 spin_unlock_irqrestore (&tt->lock, flags);
822 EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
824 /* If do_delay is false, return the number of milliseconds the caller
827 static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
830 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
832 u16 wHubCharacteristics =
833 le16_to_cpu(hub->descriptor->wHubCharacteristics);
835 /* Enable power on each port. Some hubs have reserved values
836 * of LPSM (> 2) in their descriptors, even though they are
837 * USB 2.0 hubs. Some hubs do not implement port-power switching
838 * but only emulate it. In all cases, the ports won't work
839 * unless we send these messages to the hub.
841 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
842 dev_dbg(hub->intfdev, "enabling power on all ports\n");
844 dev_dbg(hub->intfdev, "trying to enable port power on "
845 "non-switchable hub\n");
846 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
847 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
849 /* Wait at least 100 msec for power to become stable */
850 delay = max(pgood_delay, (unsigned) 100);
856 static int hub_hub_status(struct usb_hub *hub,
857 u16 *status, u16 *change)
861 mutex_lock(&hub->status_mutex);
862 ret = get_hub_status(hub->hdev, &hub->status->hub);
864 dev_err (hub->intfdev,
865 "%s failed (err = %d)\n", __func__, ret);
867 *status = le16_to_cpu(hub->status->hub.wHubStatus);
868 *change = le16_to_cpu(hub->status->hub.wHubChange);
871 mutex_unlock(&hub->status_mutex);
875 static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
877 struct usb_device *hdev = hub->hdev;
880 if (hub->ports[port1 - 1]->child && set_state)
881 usb_set_device_state(hub->ports[port1 - 1]->child,
882 USB_STATE_NOTATTACHED);
883 if (!hub->error && !hub_is_superspeed(hub->hdev))
884 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
886 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
892 * Disable a port and mark a logical connect-change event, so that some
893 * time later khubd will disconnect() any existing usb_device on the port
894 * and will re-enumerate if there actually is a device attached.
896 static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
898 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
899 hub_port_disable(hub, port1, 1);
901 /* FIXME let caller ask to power down the port:
902 * - some devices won't enumerate without a VBUS power cycle
903 * - SRP saves power that way
904 * - ... new call, TBD ...
905 * That's easy if this hub can switch power per-port, and
906 * khubd reactivates the port later (timer, SRP, etc).
907 * Powerdown must be optional, because of reset/DFU.
910 set_bit(port1, hub->change_bits);
915 * usb_remove_device - disable a device's port on its parent hub
916 * @udev: device to be disabled and removed
917 * Context: @udev locked, must be able to sleep.
919 * After @udev's port has been disabled, khubd is notified and it will
920 * see that the device has been disconnected. When the device is
921 * physically unplugged and something is plugged in, the events will
922 * be received and processed normally.
924 int usb_remove_device(struct usb_device *udev)
927 struct usb_interface *intf;
929 if (!udev->parent) /* Can't remove a root hub */
931 hub = hdev_to_hub(udev->parent);
932 intf = to_usb_interface(hub->intfdev);
934 usb_autopm_get_interface(intf);
935 set_bit(udev->portnum, hub->removed_bits);
936 hub_port_logical_disconnect(hub, udev->portnum);
937 usb_autopm_put_interface(intf);
941 enum hub_activation_type {
942 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
943 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
946 static void hub_init_func2(struct work_struct *ws);
947 static void hub_init_func3(struct work_struct *ws);
949 static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
951 struct usb_device *hdev = hub->hdev;
956 bool need_debounce_delay = false;
959 /* Continue a partial initialization */
960 if (type == HUB_INIT2)
962 if (type == HUB_INIT3)
965 /* The superspeed hub except for root hub has to use Hub Depth
966 * value as an offset into the route string to locate the bits
967 * it uses to determine the downstream port number. So hub driver
968 * should send a set hub depth request to superspeed hub after
969 * the superspeed hub is set configuration in initialization or
972 * After a resume, port power should still be on.
973 * For any other type of activation, turn it on.
975 if (type != HUB_RESUME) {
976 if (hdev->parent && hub_is_superspeed(hdev)) {
977 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
978 HUB_SET_DEPTH, USB_RT_HUB,
979 hdev->level - 1, 0, NULL, 0,
980 USB_CTRL_SET_TIMEOUT);
982 dev_err(hub->intfdev,
983 "set hub depth failed\n");
986 /* Speed up system boot by using a delayed_work for the
987 * hub's initial power-up delays. This is pretty awkward
988 * and the implementation looks like a home-brewed sort of
989 * setjmp/longjmp, but it saves at least 100 ms for each
990 * root hub (assuming usbcore is compiled into the kernel
991 * rather than as a module). It adds up.
993 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
994 * because for those activation types the ports have to be
995 * operational when we return. In theory this could be done
996 * for HUB_POST_RESET, but it's easier not to.
998 if (type == HUB_INIT) {
999 delay = hub_power_on(hub, false);
1000 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
1001 schedule_delayed_work(&hub->init_work,
1002 msecs_to_jiffies(delay));
1004 /* Suppress autosuspend until init is done */
1005 usb_autopm_get_interface_no_resume(
1006 to_usb_interface(hub->intfdev));
1007 return; /* Continues at init2: below */
1008 } else if (type == HUB_RESET_RESUME) {
1009 /* The internal host controller state for the hub device
1010 * may be gone after a host power loss on system resume.
1011 * Update the device's info so the HW knows it's a hub.
1013 hcd = bus_to_hcd(hdev->bus);
1014 if (hcd->driver->update_hub_device) {
1015 ret = hcd->driver->update_hub_device(hcd, hdev,
1016 &hub->tt, GFP_NOIO);
1018 dev_err(hub->intfdev, "Host not "
1019 "accepting hub info "
1021 dev_err(hub->intfdev, "LS/FS devices "
1022 "and hubs may not work "
1023 "under this hub\n.");
1026 hub_power_on(hub, true);
1028 hub_power_on(hub, true);
1033 /* Check each port and set hub->change_bits to let khubd know
1034 * which ports need attention.
1036 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
1037 struct usb_device *udev = hub->ports[port1 - 1]->child;
1038 u16 portstatus, portchange;
1040 portstatus = portchange = 0;
1041 status = hub_port_status(hub, port1, &portstatus, &portchange);
1042 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1043 dev_dbg(hub->intfdev,
1044 "port %d: status %04x change %04x\n",
1045 port1, portstatus, portchange);
1047 /* After anything other than HUB_RESUME (i.e., initialization
1048 * or any sort of reset), every port should be disabled.
1049 * Unconnected ports should likewise be disabled (paranoia),
1050 * and so should ports for which we have no usb_device.
1052 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1053 type != HUB_RESUME ||
1054 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1056 udev->state == USB_STATE_NOTATTACHED)) {
1058 * USB3 protocol ports will automatically transition
1059 * to Enabled state when detect an USB3.0 device attach.
1060 * Do not disable USB3 protocol ports.
1062 if (!hub_is_superspeed(hdev)) {
1063 clear_port_feature(hdev, port1,
1064 USB_PORT_FEAT_ENABLE);
1065 portstatus &= ~USB_PORT_STAT_ENABLE;
1067 /* Pretend that power was lost for USB3 devs */
1068 portstatus &= ~USB_PORT_STAT_ENABLE;
1072 /* Clear status-change flags; we'll debounce later */
1073 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1074 need_debounce_delay = true;
1075 clear_port_feature(hub->hdev, port1,
1076 USB_PORT_FEAT_C_CONNECTION);
1078 if (portchange & USB_PORT_STAT_C_ENABLE) {
1079 need_debounce_delay = true;
1080 clear_port_feature(hub->hdev, port1,
1081 USB_PORT_FEAT_C_ENABLE);
1083 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1084 hub_is_superspeed(hub->hdev)) {
1085 need_debounce_delay = true;
1086 clear_port_feature(hub->hdev, port1,
1087 USB_PORT_FEAT_C_BH_PORT_RESET);
1089 /* We can forget about a "removed" device when there's a
1090 * physical disconnect or the connect status changes.
1092 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1093 (portchange & USB_PORT_STAT_C_CONNECTION))
1094 clear_bit(port1, hub->removed_bits);
1096 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1097 /* Tell khubd to disconnect the device or
1098 * check for a new connection
1100 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1101 set_bit(port1, hub->change_bits);
1103 } else if (portstatus & USB_PORT_STAT_ENABLE) {
1104 bool port_resumed = (portstatus &
1105 USB_PORT_STAT_LINK_STATE) ==
1107 /* The power session apparently survived the resume.
1108 * If there was an overcurrent or suspend change
1109 * (i.e., remote wakeup request), have khubd
1110 * take care of it. Look at the port link state
1111 * for USB 3.0 hubs, since they don't have a suspend
1112 * change bit, and they don't set the port link change
1113 * bit on device-initiated resume.
1115 if (portchange || (hub_is_superspeed(hub->hdev) &&
1117 set_bit(port1, hub->change_bits);
1119 } else if (udev->persist_enabled) {
1121 udev->reset_resume = 1;
1123 set_bit(port1, hub->change_bits);
1126 /* The power session is gone; tell khubd */
1127 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1128 set_bit(port1, hub->change_bits);
1132 /* If no port-status-change flags were set, we don't need any
1133 * debouncing. If flags were set we can try to debounce the
1134 * ports all at once right now, instead of letting khubd do them
1135 * one at a time later on.
1137 * If any port-status changes do occur during this delay, khubd
1138 * will see them later and handle them normally.
1140 if (need_debounce_delay) {
1141 delay = HUB_DEBOUNCE_STABLE;
1143 /* Don't do a long sleep inside a workqueue routine */
1144 if (type == HUB_INIT2) {
1145 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
1146 schedule_delayed_work(&hub->init_work,
1147 msecs_to_jiffies(delay));
1148 return; /* Continues at init3: below */
1156 status = usb_submit_urb(hub->urb, GFP_NOIO);
1158 dev_err(hub->intfdev, "activate --> %d\n", status);
1159 if (hub->has_indicators && blinkenlights)
1160 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1162 /* Scan all ports that need attention */
1165 /* Allow autosuspend if it was suppressed */
1166 if (type <= HUB_INIT3)
1167 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
1170 /* Implement the continuations for the delays above */
1171 static void hub_init_func2(struct work_struct *ws)
1173 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1175 hub_activate(hub, HUB_INIT2);
1178 static void hub_init_func3(struct work_struct *ws)
1180 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1182 hub_activate(hub, HUB_INIT3);
1185 enum hub_quiescing_type {
1186 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1189 static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1191 struct usb_device *hdev = hub->hdev;
1194 cancel_delayed_work_sync(&hub->init_work);
1196 /* khubd and related activity won't re-trigger */
1199 if (type != HUB_SUSPEND) {
1200 /* Disconnect all the children */
1201 for (i = 0; i < hdev->maxchild; ++i) {
1202 if (hub->ports[i]->child)
1203 usb_disconnect(&hub->ports[i]->child);
1207 /* Stop khubd and related activity */
1208 usb_kill_urb(hub->urb);
1209 if (hub->has_indicators)
1210 cancel_delayed_work_sync(&hub->leds);
1212 cancel_work_sync(&hub->tt.clear_work);
1215 /* caller has locked the hub device */
1216 static int hub_pre_reset(struct usb_interface *intf)
1218 struct usb_hub *hub = usb_get_intfdata(intf);
1220 hub_quiesce(hub, HUB_PRE_RESET);
1224 /* caller has locked the hub device */
1225 static int hub_post_reset(struct usb_interface *intf)
1227 struct usb_hub *hub = usb_get_intfdata(intf);
1229 hub_activate(hub, HUB_POST_RESET);
1233 static void usb_port_device_release(struct device *dev)
1235 struct usb_port *port_dev = to_usb_port(dev);
1240 static void usb_hub_remove_port_device(struct usb_hub *hub,
1243 device_unregister(&hub->ports[port1 - 1]->dev);
1246 struct device_type usb_port_device_type = {
1248 .release = usb_port_device_release,
1251 static int usb_hub_create_port_device(struct usb_hub *hub,
1254 struct usb_port *port_dev = NULL;
1257 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
1263 hub->ports[port1 - 1] = port_dev;
1264 port_dev->dev.parent = hub->intfdev;
1265 port_dev->dev.type = &usb_port_device_type;
1266 dev_set_name(&port_dev->dev, "port%d", port1);
1268 retval = device_register(&port_dev->dev);
1270 goto error_register;
1274 put_device(&port_dev->dev);
1279 static int hub_configure(struct usb_hub *hub,
1280 struct usb_endpoint_descriptor *endpoint)
1282 struct usb_hcd *hcd;
1283 struct usb_device *hdev = hub->hdev;
1284 struct device *hub_dev = hub->intfdev;
1285 u16 hubstatus, hubchange;
1286 u16 wHubCharacteristics;
1289 char *message = "out of memory";
1291 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
1297 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1302 mutex_init(&hub->status_mutex);
1304 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1305 if (!hub->descriptor) {
1310 /* Request the entire hub descriptor.
1311 * hub->descriptor can handle USB_MAXCHILDREN ports,
1312 * but the hub can/will return fewer bytes here.
1314 ret = get_hub_descriptor(hdev, hub->descriptor);
1316 message = "can't read hub descriptor";
1318 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1319 message = "hub has too many ports!";
1324 hdev->maxchild = hub->descriptor->bNbrPorts;
1325 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
1326 (hdev->maxchild == 1) ? "" : "s");
1328 hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *),
1335 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
1337 /* FIXME for USB 3.0, skip for now */
1338 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1339 !(hub_is_superspeed(hdev))) {
1341 char portstr [USB_MAXCHILDREN + 1];
1343 for (i = 0; i < hdev->maxchild; i++)
1344 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
1345 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1347 portstr[hdev->maxchild] = 0;
1348 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1350 dev_dbg(hub_dev, "standalone hub\n");
1352 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
1353 case HUB_CHAR_COMMON_LPSM:
1354 dev_dbg(hub_dev, "ganged power switching\n");
1356 case HUB_CHAR_INDV_PORT_LPSM:
1357 dev_dbg(hub_dev, "individual port power switching\n");
1359 case HUB_CHAR_NO_LPSM:
1361 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1365 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
1366 case HUB_CHAR_COMMON_OCPM:
1367 dev_dbg(hub_dev, "global over-current protection\n");
1369 case HUB_CHAR_INDV_PORT_OCPM:
1370 dev_dbg(hub_dev, "individual port over-current protection\n");
1372 case HUB_CHAR_NO_OCPM:
1374 dev_dbg(hub_dev, "no over-current protection\n");
1378 spin_lock_init (&hub->tt.lock);
1379 INIT_LIST_HEAD (&hub->tt.clear_list);
1380 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
1381 switch (hdev->descriptor.bDeviceProtocol) {
1384 case USB_HUB_PR_HS_SINGLE_TT:
1385 dev_dbg(hub_dev, "Single TT\n");
1388 case USB_HUB_PR_HS_MULTI_TT:
1389 ret = usb_set_interface(hdev, 0, 1);
1391 dev_dbg(hub_dev, "TT per port\n");
1394 dev_err(hub_dev, "Using single TT (err %d)\n",
1399 /* USB 3.0 hubs don't have a TT */
1402 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1403 hdev->descriptor.bDeviceProtocol);
1407 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
1408 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
1409 case HUB_TTTT_8_BITS:
1410 if (hdev->descriptor.bDeviceProtocol != 0) {
1411 hub->tt.think_time = 666;
1412 dev_dbg(hub_dev, "TT requires at most %d "
1413 "FS bit times (%d ns)\n",
1414 8, hub->tt.think_time);
1417 case HUB_TTTT_16_BITS:
1418 hub->tt.think_time = 666 * 2;
1419 dev_dbg(hub_dev, "TT requires at most %d "
1420 "FS bit times (%d ns)\n",
1421 16, hub->tt.think_time);
1423 case HUB_TTTT_24_BITS:
1424 hub->tt.think_time = 666 * 3;
1425 dev_dbg(hub_dev, "TT requires at most %d "
1426 "FS bit times (%d ns)\n",
1427 24, hub->tt.think_time);
1429 case HUB_TTTT_32_BITS:
1430 hub->tt.think_time = 666 * 4;
1431 dev_dbg(hub_dev, "TT requires at most %d "
1432 "FS bit times (%d ns)\n",
1433 32, hub->tt.think_time);
1437 /* probe() zeroes hub->indicator[] */
1438 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
1439 hub->has_indicators = 1;
1440 dev_dbg(hub_dev, "Port indicators are supported\n");
1443 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1444 hub->descriptor->bPwrOn2PwrGood * 2);
1446 /* power budgeting mostly matters with bus-powered hubs,
1447 * and battery-powered root hubs (may provide just 8 mA).
1449 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
1451 message = "can't get hub status";
1454 le16_to_cpus(&hubstatus);
1455 if (hdev == hdev->bus->root_hub) {
1456 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1457 hub->mA_per_port = 500;
1459 hub->mA_per_port = hdev->bus_mA;
1460 hub->limited_power = 1;
1462 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
1463 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1464 hub->descriptor->bHubContrCurrent);
1465 hub->limited_power = 1;
1466 if (hdev->maxchild > 0) {
1467 int remaining = hdev->bus_mA -
1468 hub->descriptor->bHubContrCurrent;
1470 if (remaining < hdev->maxchild * 100)
1472 "insufficient power available "
1473 "to use all downstream ports\n");
1474 hub->mA_per_port = 100; /* 7.2.1.1 */
1476 } else { /* Self-powered external hub */
1477 /* FIXME: What about battery-powered external hubs that
1478 * provide less current per port? */
1479 hub->mA_per_port = 500;
1481 if (hub->mA_per_port < 500)
1482 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1485 /* Update the HCD's internal representation of this hub before khubd
1486 * starts getting port status changes for devices under the hub.
1488 hcd = bus_to_hcd(hdev->bus);
1489 if (hcd->driver->update_hub_device) {
1490 ret = hcd->driver->update_hub_device(hcd, hdev,
1491 &hub->tt, GFP_KERNEL);
1493 message = "can't update HCD hub info";
1498 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1500 message = "can't get hub status";
1504 /* local power status reports aren't always correct */
1505 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1506 dev_dbg(hub_dev, "local power source is %s\n",
1507 (hubstatus & HUB_STATUS_LOCAL_POWER)
1508 ? "lost (inactive)" : "good");
1510 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
1511 dev_dbg(hub_dev, "%sover-current condition exists\n",
1512 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1514 /* set up the interrupt endpoint
1515 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1516 * bytes as USB2.0[11.12.3] says because some hubs are known
1517 * to send more data (and thus cause overflow). For root hubs,
1518 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1519 * to be big enough for at least USB_MAXCHILDREN ports. */
1520 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1521 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1523 if (maxp > sizeof(*hub->buffer))
1524 maxp = sizeof(*hub->buffer);
1526 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1532 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1533 hub, endpoint->bInterval);
1535 /* maybe cycle the hub leds */
1536 if (hub->has_indicators && blinkenlights)
1537 hub->indicator [0] = INDICATOR_CYCLE;
1539 for (i = 0; i < hdev->maxchild; i++)
1540 if (usb_hub_create_port_device(hub, i + 1) < 0)
1541 dev_err(hub->intfdev,
1542 "couldn't create port%d device.\n", i + 1);
1544 hub_activate(hub, HUB_INIT);
1548 dev_err (hub_dev, "config failed, %s (err %d)\n",
1550 /* hub_disconnect() frees urb and descriptor */
1554 static void hub_release(struct kref *kref)
1556 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1558 usb_put_intf(to_usb_interface(hub->intfdev));
1562 static unsigned highspeed_hubs;
1564 static void hub_disconnect(struct usb_interface *intf)
1566 struct usb_hub *hub = usb_get_intfdata(intf);
1567 struct usb_device *hdev = interface_to_usbdev(intf);
1570 for (i = 0; i < hdev->maxchild; i++)
1571 usb_hub_remove_port_device(hub, i + 1);
1573 /* Take the hub off the event list and don't let it be added again */
1574 spin_lock_irq(&hub_event_lock);
1575 if (!list_empty(&hub->event_list)) {
1576 list_del_init(&hub->event_list);
1577 usb_autopm_put_interface_no_suspend(intf);
1579 hub->disconnected = 1;
1580 spin_unlock_irq(&hub_event_lock);
1582 /* Disconnect all children and quiesce the hub */
1584 hub_quiesce(hub, HUB_DISCONNECT);
1586 usb_set_intfdata (intf, NULL);
1587 hub->hdev->maxchild = 0;
1589 if (hub->hdev->speed == USB_SPEED_HIGH)
1592 usb_free_urb(hub->urb);
1594 kfree(hub->descriptor);
1598 kref_put(&hub->kref, hub_release);
1601 static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1603 struct usb_host_interface *desc;
1604 struct usb_endpoint_descriptor *endpoint;
1605 struct usb_device *hdev;
1606 struct usb_hub *hub;
1608 desc = intf->cur_altsetting;
1609 hdev = interface_to_usbdev(intf);
1611 /* Hubs have proper suspend/resume support. */
1612 usb_enable_autosuspend(hdev);
1614 if (hdev->level == MAX_TOPO_LEVEL) {
1616 "Unsupported bus topology: hub nested too deep\n");
1620 #ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1622 dev_warn(&intf->dev, "ignoring external hub\n");
1627 /* Some hubs have a subclass of 1, which AFAICT according to the */
1628 /* specs is not defined, but it works */
1629 if ((desc->desc.bInterfaceSubClass != 0) &&
1630 (desc->desc.bInterfaceSubClass != 1)) {
1632 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1636 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1637 if (desc->desc.bNumEndpoints != 1)
1638 goto descriptor_error;
1640 endpoint = &desc->endpoint[0].desc;
1642 /* If it's not an interrupt in endpoint, we'd better punt! */
1643 if (!usb_endpoint_is_int_in(endpoint))
1644 goto descriptor_error;
1646 /* We found a hub */
1647 dev_info (&intf->dev, "USB hub found\n");
1649 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
1651 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1655 kref_init(&hub->kref);
1656 INIT_LIST_HEAD(&hub->event_list);
1657 hub->intfdev = &intf->dev;
1659 INIT_DELAYED_WORK(&hub->leds, led_work);
1660 INIT_DELAYED_WORK(&hub->init_work, NULL);
1663 usb_set_intfdata (intf, hub);
1664 intf->needs_remote_wakeup = 1;
1666 if (hdev->speed == USB_SPEED_HIGH)
1669 if (hub_configure(hub, endpoint) >= 0)
1672 hub_disconnect (intf);
1677 hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1679 struct usb_device *hdev = interface_to_usbdev (intf);
1680 struct usb_hub *hub = hdev_to_hub(hdev);
1682 /* assert ifno == 0 (part of hub spec) */
1684 case USBDEVFS_HUB_PORTINFO: {
1685 struct usbdevfs_hub_portinfo *info = user_data;
1688 spin_lock_irq(&device_state_lock);
1689 if (hdev->devnum <= 0)
1692 info->nports = hdev->maxchild;
1693 for (i = 0; i < info->nports; i++) {
1694 if (hub->ports[i]->child == NULL)
1698 hub->ports[i]->child->devnum;
1701 spin_unlock_irq(&device_state_lock);
1703 return info->nports + 1;
1712 * Allow user programs to claim ports on a hub. When a device is attached
1713 * to one of these "claimed" ports, the program will "own" the device.
1715 static int find_port_owner(struct usb_device *hdev, unsigned port1,
1716 struct dev_state ***ppowner)
1718 if (hdev->state == USB_STATE_NOTATTACHED)
1720 if (port1 == 0 || port1 > hdev->maxchild)
1723 /* This assumes that devices not managed by the hub driver
1724 * will always have maxchild equal to 0.
1726 *ppowner = &(hdev_to_hub(hdev)->ports[port1 - 1]->port_owner);
1730 /* In the following three functions, the caller must hold hdev's lock */
1731 int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1732 struct dev_state *owner)
1735 struct dev_state **powner;
1737 rc = find_port_owner(hdev, port1, &powner);
1746 int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1747 struct dev_state *owner)
1750 struct dev_state **powner;
1752 rc = find_port_owner(hdev, port1, &powner);
1755 if (*powner != owner)
1761 void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
1763 struct usb_hub *hub = hdev_to_hub(hdev);
1766 for (n = 0; n < hdev->maxchild; n++) {
1767 if (hub->ports[n]->port_owner == owner)
1768 hub->ports[n]->port_owner = NULL;
1773 /* The caller must hold udev's lock */
1774 bool usb_device_is_owned(struct usb_device *udev)
1776 struct usb_hub *hub;
1778 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1780 hub = hdev_to_hub(udev->parent);
1781 return !!hub->ports[udev->portnum - 1]->port_owner;
1784 static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1786 struct usb_hub *hub = hdev_to_hub(udev);
1789 for (i = 0; i < udev->maxchild; ++i) {
1790 if (hub->ports[i]->child)
1791 recursively_mark_NOTATTACHED(hub->ports[i]->child);
1793 if (udev->state == USB_STATE_SUSPENDED)
1794 udev->active_duration -= jiffies;
1795 udev->state = USB_STATE_NOTATTACHED;
1799 * usb_set_device_state - change a device's current state (usbcore, hcds)
1800 * @udev: pointer to device whose state should be changed
1801 * @new_state: new state value to be stored
1803 * udev->state is _not_ fully protected by the device lock. Although
1804 * most transitions are made only while holding the lock, the state can
1805 * can change to USB_STATE_NOTATTACHED at almost any time. This
1806 * is so that devices can be marked as disconnected as soon as possible,
1807 * without having to wait for any semaphores to be released. As a result,
1808 * all changes to any device's state must be protected by the
1809 * device_state_lock spinlock.
1811 * Once a device has been added to the device tree, all changes to its state
1812 * should be made using this routine. The state should _not_ be set directly.
1814 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1815 * Otherwise udev->state is set to new_state, and if new_state is
1816 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1817 * to USB_STATE_NOTATTACHED.
1819 void usb_set_device_state(struct usb_device *udev,
1820 enum usb_device_state new_state)
1822 unsigned long flags;
1825 spin_lock_irqsave(&device_state_lock, flags);
1826 if (udev->state == USB_STATE_NOTATTACHED)
1828 else if (new_state != USB_STATE_NOTATTACHED) {
1830 /* root hub wakeup capabilities are managed out-of-band
1831 * and may involve silicon errata ... ignore them here.
1834 if (udev->state == USB_STATE_SUSPENDED
1835 || new_state == USB_STATE_SUSPENDED)
1836 ; /* No change to wakeup settings */
1837 else if (new_state == USB_STATE_CONFIGURED)
1838 wakeup = udev->actconfig->desc.bmAttributes
1839 & USB_CONFIG_ATT_WAKEUP;
1843 if (udev->state == USB_STATE_SUSPENDED &&
1844 new_state != USB_STATE_SUSPENDED)
1845 udev->active_duration -= jiffies;
1846 else if (new_state == USB_STATE_SUSPENDED &&
1847 udev->state != USB_STATE_SUSPENDED)
1848 udev->active_duration += jiffies;
1849 udev->state = new_state;
1851 recursively_mark_NOTATTACHED(udev);
1852 spin_unlock_irqrestore(&device_state_lock, flags);
1854 device_set_wakeup_capable(&udev->dev, wakeup);
1856 EXPORT_SYMBOL_GPL(usb_set_device_state);
1859 * Choose a device number.
1861 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1862 * USB-2.0 buses they are also used as device addresses, however on
1863 * USB-3.0 buses the address is assigned by the controller hardware
1864 * and it usually is not the same as the device number.
1866 * WUSB devices are simple: they have no hubs behind, so the mapping
1867 * device <-> virtual port number becomes 1:1. Why? to simplify the
1868 * life of the device connection logic in
1869 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1870 * handshake we need to assign a temporary address in the unauthorized
1871 * space. For simplicity we use the first virtual port number found to
1872 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1873 * and that becomes it's address [X < 128] or its unauthorized address
1876 * We add 1 as an offset to the one-based USB-stack port number
1877 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1878 * 0 is reserved by USB for default address; (b) Linux's USB stack
1879 * uses always #1 for the root hub of the controller. So USB stack's
1880 * port #1, which is wusb virtual-port #0 has address #2.
1882 * Devices connected under xHCI are not as simple. The host controller
1883 * supports virtualization, so the hardware assigns device addresses and
1884 * the HCD must setup data structures before issuing a set address
1885 * command to the hardware.
1887 static void choose_devnum(struct usb_device *udev)
1890 struct usb_bus *bus = udev->bus;
1892 /* If khubd ever becomes multithreaded, this will need a lock */
1894 devnum = udev->portnum + 1;
1895 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1897 /* Try to allocate the next devnum beginning at
1898 * bus->devnum_next. */
1899 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1902 devnum = find_next_zero_bit(bus->devmap.devicemap,
1904 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1907 set_bit(devnum, bus->devmap.devicemap);
1908 udev->devnum = devnum;
1912 static void release_devnum(struct usb_device *udev)
1914 if (udev->devnum > 0) {
1915 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1920 static void update_devnum(struct usb_device *udev, int devnum)
1922 /* The address for a WUSB device is managed by wusbcore. */
1924 udev->devnum = devnum;
1927 static void hub_free_dev(struct usb_device *udev)
1929 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1931 /* Root hubs aren't real devices, so don't free HCD resources */
1932 if (hcd->driver->free_dev && udev->parent)
1933 hcd->driver->free_dev(hcd, udev);
1937 * usb_disconnect - disconnect a device (usbcore-internal)
1938 * @pdev: pointer to device being disconnected
1939 * Context: !in_interrupt ()
1941 * Something got disconnected. Get rid of it and all of its children.
1943 * If *pdev is a normal device then the parent hub must already be locked.
1944 * If *pdev is a root hub then this routine will acquire the
1945 * usb_bus_list_lock on behalf of the caller.
1947 * Only hub drivers (including virtual root hub drivers for host
1948 * controllers) should ever call this.
1950 * This call is synchronous, and may not be used in an interrupt context.
1952 void usb_disconnect(struct usb_device **pdev)
1954 struct usb_device *udev = *pdev;
1955 struct usb_hub *hub = hdev_to_hub(udev);
1958 /* mark the device as inactive, so any further urb submissions for
1959 * this device (and any of its children) will fail immediately.
1960 * this quiesces everything except pending urbs.
1962 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1963 dev_info(&udev->dev, "USB disconnect, device number %d\n",
1966 usb_lock_device(udev);
1968 /* Free up all the children before we remove this device */
1969 for (i = 0; i < udev->maxchild; i++) {
1970 if (hub->ports[i]->child)
1971 usb_disconnect(&hub->ports[i]->child);
1974 /* deallocate hcd/hardware state ... nuking all pending urbs and
1975 * cleaning up all state associated with the current configuration
1976 * so that the hardware is now fully quiesced.
1978 dev_dbg (&udev->dev, "unregistering device\n");
1979 usb_disable_device(udev, 0);
1980 usb_hcd_synchronize_unlinks(udev);
1982 usb_remove_ep_devs(&udev->ep0);
1983 usb_unlock_device(udev);
1985 /* Unregister the device. The device driver is responsible
1986 * for de-configuring the device and invoking the remove-device
1987 * notifier chain (used by usbfs and possibly others).
1989 device_del(&udev->dev);
1991 /* Free the device number and delete the parent's children[]
1992 * (or root_hub) pointer.
1994 release_devnum(udev);
1996 /* Avoid races with recursively_mark_NOTATTACHED() */
1997 spin_lock_irq(&device_state_lock);
1999 spin_unlock_irq(&device_state_lock);
2003 put_device(&udev->dev);
2006 #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
2007 static void show_string(struct usb_device *udev, char *id, char *string)
2011 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
2014 static void announce_device(struct usb_device *udev)
2016 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2017 le16_to_cpu(udev->descriptor.idVendor),
2018 le16_to_cpu(udev->descriptor.idProduct));
2019 dev_info(&udev->dev,
2020 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
2021 udev->descriptor.iManufacturer,
2022 udev->descriptor.iProduct,
2023 udev->descriptor.iSerialNumber);
2024 show_string(udev, "Product", udev->product);
2025 show_string(udev, "Manufacturer", udev->manufacturer);
2026 show_string(udev, "SerialNumber", udev->serial);
2029 static inline void announce_device(struct usb_device *udev) { }
2032 #ifdef CONFIG_USB_OTG
2033 #include "otg_whitelist.h"
2037 * usb_enumerate_device_otg - FIXME (usbcore-internal)
2038 * @udev: newly addressed device (in ADDRESS state)
2040 * Finish enumeration for On-The-Go devices
2042 static int usb_enumerate_device_otg(struct usb_device *udev)
2046 #ifdef CONFIG_USB_OTG
2048 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2049 * to wake us after we've powered off VBUS; and HNP, switching roles
2050 * "host" to "peripheral". The OTG descriptor helps figure this out.
2052 if (!udev->bus->is_b_host
2054 && udev->parent == udev->bus->root_hub) {
2055 struct usb_otg_descriptor *desc = NULL;
2056 struct usb_bus *bus = udev->bus;
2058 /* descriptor may appear anywhere in config */
2059 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2060 le16_to_cpu(udev->config[0].desc.wTotalLength),
2061 USB_DT_OTG, (void **) &desc) == 0) {
2062 if (desc->bmAttributes & USB_OTG_HNP) {
2063 unsigned port1 = udev->portnum;
2065 dev_info(&udev->dev,
2066 "Dual-Role OTG device on %sHNP port\n",
2067 (port1 == bus->otg_port)
2070 /* enable HNP before suspend, it's simpler */
2071 if (port1 == bus->otg_port)
2072 bus->b_hnp_enable = 1;
2073 err = usb_control_msg(udev,
2074 usb_sndctrlpipe(udev, 0),
2075 USB_REQ_SET_FEATURE, 0,
2077 ? USB_DEVICE_B_HNP_ENABLE
2078 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2079 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2081 /* OTG MESSAGE: report errors here,
2082 * customize to match your product.
2084 dev_info(&udev->dev,
2085 "can't set HNP mode: %d\n",
2087 bus->b_hnp_enable = 0;
2093 if (!is_targeted(udev)) {
2095 /* Maybe it can talk to us, though we can't talk to it.
2096 * (Includes HNP test device.)
2098 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
2099 err = usb_port_suspend(udev, PMSG_SUSPEND);
2101 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2113 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
2114 * @udev: newly addressed device (in ADDRESS state)
2116 * This is only called by usb_new_device() and usb_authorize_device()
2117 * and FIXME -- all comments that apply to them apply here wrt to
2120 * If the device is WUSB and not authorized, we don't attempt to read
2121 * the string descriptors, as they will be errored out by the device
2122 * until it has been authorized.
2124 static int usb_enumerate_device(struct usb_device *udev)
2128 if (udev->config == NULL) {
2129 err = usb_get_configuration(udev);
2131 dev_err(&udev->dev, "can't read configurations, error %d\n",
2136 if (udev->wusb == 1 && udev->authorized == 0) {
2137 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2138 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2139 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2142 /* read the standard strings and cache them if present */
2143 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2144 udev->manufacturer = usb_cache_string(udev,
2145 udev->descriptor.iManufacturer);
2146 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2148 err = usb_enumerate_device_otg(udev);
2152 usb_detect_interface_quirks(udev);
2157 static void set_usb_port_removable(struct usb_device *udev)
2159 struct usb_device *hdev = udev->parent;
2160 struct usb_hub *hub;
2161 u8 port = udev->portnum;
2162 u16 wHubCharacteristics;
2163 bool removable = true;
2168 hub = hdev_to_hub(udev->parent);
2170 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2172 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2175 if (hub_is_superspeed(hdev)) {
2176 if (hub->descriptor->u.ss.DeviceRemovable & (1 << port))
2179 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2184 udev->removable = USB_DEVICE_REMOVABLE;
2186 udev->removable = USB_DEVICE_FIXED;
2190 * usb_new_device - perform initial device setup (usbcore-internal)
2191 * @udev: newly addressed device (in ADDRESS state)
2193 * This is called with devices which have been detected but not fully
2194 * enumerated. The device descriptor is available, but not descriptors
2195 * for any device configuration. The caller must have locked either
2196 * the parent hub (if udev is a normal device) or else the
2197 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2198 * udev has already been installed, but udev is not yet visible through
2199 * sysfs or other filesystem code.
2201 * It will return if the device is configured properly or not. Zero if
2202 * the interface was registered with the driver core; else a negative
2205 * This call is synchronous, and may not be used in an interrupt context.
2207 * Only the hub driver or root-hub registrar should ever call this.
2209 int usb_new_device(struct usb_device *udev)
2214 /* Initialize non-root-hub device wakeup to disabled;
2215 * device (un)configuration controls wakeup capable
2216 * sysfs power/wakeup controls wakeup enabled/disabled
2218 device_init_wakeup(&udev->dev, 0);
2221 /* Tell the runtime-PM framework the device is active */
2222 pm_runtime_set_active(&udev->dev);
2223 pm_runtime_get_noresume(&udev->dev);
2224 pm_runtime_use_autosuspend(&udev->dev);
2225 pm_runtime_enable(&udev->dev);
2227 /* By default, forbid autosuspend for all devices. It will be
2228 * allowed for hubs during binding.
2230 usb_disable_autosuspend(udev);
2232 err = usb_enumerate_device(udev); /* Read descriptors */
2235 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2236 udev->devnum, udev->bus->busnum,
2237 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2238 /* export the usbdev device-node for libusb */
2239 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2240 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2242 /* Tell the world! */
2243 announce_device(udev);
2246 add_device_randomness(udev->serial, strlen(udev->serial));
2248 add_device_randomness(udev->product, strlen(udev->product));
2249 if (udev->manufacturer)
2250 add_device_randomness(udev->manufacturer,
2251 strlen(udev->manufacturer));
2253 device_enable_async_suspend(&udev->dev);
2256 * check whether the hub marks this port as non-removable. Do it
2257 * now so that platform-specific data can override it in
2261 set_usb_port_removable(udev);
2263 /* Register the device. The device driver is responsible
2264 * for configuring the device and invoking the add-device
2265 * notifier chain (used by usbfs and possibly others).
2267 err = device_add(&udev->dev);
2269 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2273 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
2274 usb_mark_last_busy(udev);
2275 pm_runtime_put_sync_autosuspend(&udev->dev);
2279 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
2280 pm_runtime_disable(&udev->dev);
2281 pm_runtime_set_suspended(&udev->dev);
2287 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2288 * @usb_dev: USB device
2290 * Move the USB device to a very basic state where interfaces are disabled
2291 * and the device is in fact unconfigured and unusable.
2293 * We share a lock (that we have) with device_del(), so we need to
2296 int usb_deauthorize_device(struct usb_device *usb_dev)
2298 usb_lock_device(usb_dev);
2299 if (usb_dev->authorized == 0)
2300 goto out_unauthorized;
2302 usb_dev->authorized = 0;
2303 usb_set_configuration(usb_dev, -1);
2305 kfree(usb_dev->product);
2306 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2307 kfree(usb_dev->manufacturer);
2308 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2309 kfree(usb_dev->serial);
2310 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2312 usb_destroy_configuration(usb_dev);
2313 usb_dev->descriptor.bNumConfigurations = 0;
2316 usb_unlock_device(usb_dev);
2321 int usb_authorize_device(struct usb_device *usb_dev)
2325 usb_lock_device(usb_dev);
2326 if (usb_dev->authorized == 1)
2327 goto out_authorized;
2329 result = usb_autoresume_device(usb_dev);
2331 dev_err(&usb_dev->dev,
2332 "can't autoresume for authorization: %d\n", result);
2333 goto error_autoresume;
2335 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2337 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2338 "authorization: %d\n", result);
2339 goto error_device_descriptor;
2342 kfree(usb_dev->product);
2343 usb_dev->product = NULL;
2344 kfree(usb_dev->manufacturer);
2345 usb_dev->manufacturer = NULL;
2346 kfree(usb_dev->serial);
2347 usb_dev->serial = NULL;
2349 usb_dev->authorized = 1;
2350 result = usb_enumerate_device(usb_dev);
2352 goto error_enumerate;
2353 /* Choose and set the configuration. This registers the interfaces
2354 * with the driver core and lets interface drivers bind to them.
2356 c = usb_choose_configuration(usb_dev);
2358 result = usb_set_configuration(usb_dev, c);
2360 dev_err(&usb_dev->dev,
2361 "can't set config #%d, error %d\n", c, result);
2362 /* This need not be fatal. The user can try to
2363 * set other configurations. */
2366 dev_info(&usb_dev->dev, "authorized to connect\n");
2369 error_device_descriptor:
2370 usb_autosuspend_device(usb_dev);
2373 usb_unlock_device(usb_dev); // complements locktree
2378 /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2379 static unsigned hub_is_wusb(struct usb_hub *hub)
2381 struct usb_hcd *hcd;
2382 if (hub->hdev->parent != NULL) /* not a root hub? */
2384 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2385 return hcd->wireless;
2389 #define PORT_RESET_TRIES 5
2390 #define SET_ADDRESS_TRIES 2
2391 #define GET_DESCRIPTOR_TRIES 2
2392 #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
2393 #define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
2395 #define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2396 #define HUB_SHORT_RESET_TIME 10
2397 #define HUB_BH_RESET_TIME 50
2398 #define HUB_LONG_RESET_TIME 200
2399 #define HUB_RESET_TIMEOUT 500
2401 static int hub_port_reset(struct usb_hub *hub, int port1,
2402 struct usb_device *udev, unsigned int delay, bool warm);
2404 /* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2405 * Port worm reset is required to recover
2407 static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
2409 return hub_is_superspeed(hub->hdev) &&
2410 (((portstatus & USB_PORT_STAT_LINK_STATE) ==
2411 USB_SS_PORT_LS_SS_INACTIVE) ||
2412 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
2413 USB_SS_PORT_LS_COMP_MOD)) ;
2416 static int hub_port_wait_reset(struct usb_hub *hub, int port1,
2417 struct usb_device *udev, unsigned int delay, bool warm)
2419 int delay_time, ret;
2423 for (delay_time = 0;
2424 delay_time < HUB_RESET_TIMEOUT;
2425 delay_time += delay) {
2426 /* wait to give the device a chance to reset */
2429 /* read and decode port status */
2430 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2435 * Some buggy devices require a warm reset to be issued even
2436 * when the port appears not to be connected.
2440 * Some buggy devices can cause an NEC host controller
2441 * to transition to the "Error" state after a hot port
2442 * reset. This will show up as the port state in
2443 * "Inactive", and the port may also report a
2444 * disconnect. Forcing a warm port reset seems to make
2447 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2449 if (hub_port_warm_reset_required(hub, portstatus)) {
2452 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2453 clear_port_feature(hub->hdev, port1,
2454 USB_PORT_FEAT_C_CONNECTION);
2455 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2456 clear_port_feature(hub->hdev, port1,
2457 USB_PORT_FEAT_C_PORT_LINK_STATE);
2458 if (portchange & USB_PORT_STAT_C_RESET)
2459 clear_port_feature(hub->hdev, port1,
2460 USB_PORT_FEAT_C_RESET);
2461 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2463 ret = hub_port_reset(hub, port1,
2464 udev, HUB_BH_RESET_TIME,
2466 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2467 clear_port_feature(hub->hdev, port1,
2468 USB_PORT_FEAT_C_CONNECTION);
2471 /* Device went away? */
2472 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2475 /* bomb out completely if the connection bounced */
2476 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2479 /* if we`ve finished resetting, then break out of
2482 if (!(portstatus & USB_PORT_STAT_RESET) &&
2483 (portstatus & USB_PORT_STAT_ENABLE)) {
2484 if (hub_is_wusb(hub))
2485 udev->speed = USB_SPEED_WIRELESS;
2486 else if (hub_is_superspeed(hub->hdev))
2487 udev->speed = USB_SPEED_SUPER;
2488 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2489 udev->speed = USB_SPEED_HIGH;
2490 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2491 udev->speed = USB_SPEED_LOW;
2493 udev->speed = USB_SPEED_FULL;
2497 if (portchange & USB_PORT_STAT_C_BH_RESET)
2501 /* switch to the long delay after two short delay failures */
2502 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2503 delay = HUB_LONG_RESET_TIME;
2505 dev_dbg (hub->intfdev,
2506 "port %d not %sreset yet, waiting %dms\n",
2507 port1, warm ? "warm " : "", delay);
2513 static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2514 struct usb_device *udev, int *status, bool warm)
2519 struct usb_hcd *hcd;
2520 /* TRSTRCY = 10 ms; plus some extra */
2522 update_devnum(udev, 0);
2523 hcd = bus_to_hcd(udev->bus);
2524 if (hcd->driver->reset_device) {
2525 *status = hcd->driver->reset_device(hcd, udev);
2527 dev_err(&udev->dev, "Cannot reset "
2528 "HCD device state\n");
2536 clear_port_feature(hub->hdev,
2537 port1, USB_PORT_FEAT_C_RESET);
2538 /* FIXME need disconnect() for NOTATTACHED device */
2540 clear_port_feature(hub->hdev, port1,
2541 USB_PORT_FEAT_C_BH_PORT_RESET);
2542 clear_port_feature(hub->hdev, port1,
2543 USB_PORT_FEAT_C_PORT_LINK_STATE);
2545 usb_set_device_state(udev, *status
2546 ? USB_STATE_NOTATTACHED
2547 : USB_STATE_DEFAULT);
2553 /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
2554 static int hub_port_reset(struct usb_hub *hub, int port1,
2555 struct usb_device *udev, unsigned int delay, bool warm)
2560 /* Block EHCI CF initialization during the port reset.
2561 * Some companion controllers don't like it when they mix.
2563 down_read(&ehci_cf_port_reset_rwsem);
2565 if (!hub_is_superspeed(hub->hdev)) {
2566 dev_err(hub->intfdev, "only USB3 hub support "
2572 /* Reset the port */
2573 for (i = 0; i < PORT_RESET_TRIES; i++) {
2574 status = set_port_feature(hub->hdev, port1, (warm ?
2575 USB_PORT_FEAT_BH_PORT_RESET :
2576 USB_PORT_FEAT_RESET));
2578 dev_err(hub->intfdev,
2579 "cannot %sreset port %d (err = %d)\n",
2580 warm ? "warm " : "", port1, status);
2582 status = hub_port_wait_reset(hub, port1, udev, delay,
2584 if (status && status != -ENOTCONN)
2585 dev_dbg(hub->intfdev,
2586 "port_wait_reset: err = %d\n",
2590 /* return on disconnect or reset */
2591 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2592 hub_port_finish_reset(hub, port1, udev, &status, warm);
2596 dev_dbg (hub->intfdev,
2597 "port %d not enabled, trying %sreset again...\n",
2598 port1, warm ? "warm " : "");
2599 delay = HUB_LONG_RESET_TIME;
2602 dev_err (hub->intfdev,
2603 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2608 up_read(&ehci_cf_port_reset_rwsem);
2613 /* Check if a port is power on */
2614 static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2618 if (hub_is_superspeed(hub->hdev)) {
2619 if (portstatus & USB_SS_PORT_STAT_POWER)
2622 if (portstatus & USB_PORT_STAT_POWER)
2631 /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2632 static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2636 if (hub_is_superspeed(hub->hdev)) {
2637 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2638 == USB_SS_PORT_LS_U3)
2641 if (portstatus & USB_PORT_STAT_SUSPEND)
2648 /* Determine whether the device on a port is ready for a normal resume,
2649 * is ready for a reset-resume, or should be disconnected.
2651 static int check_port_resume_type(struct usb_device *udev,
2652 struct usb_hub *hub, int port1,
2653 int status, unsigned portchange, unsigned portstatus)
2655 /* Is the device still present? */
2656 if (status || port_is_suspended(hub, portstatus) ||
2657 !port_is_power_on(hub, portstatus) ||
2658 !(portstatus & USB_PORT_STAT_CONNECTION)) {
2663 /* Can't do a normal resume if the port isn't enabled,
2664 * so try a reset-resume instead.
2666 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2667 if (udev->persist_enabled)
2668 udev->reset_resume = 1;
2674 dev_dbg(hub->intfdev,
2675 "port %d status %04x.%04x after resume, %d\n",
2676 port1, portchange, portstatus, status);
2677 } else if (udev->reset_resume) {
2679 /* Late port handoff can set status-change bits */
2680 if (portchange & USB_PORT_STAT_C_CONNECTION)
2681 clear_port_feature(hub->hdev, port1,
2682 USB_PORT_FEAT_C_CONNECTION);
2683 if (portchange & USB_PORT_STAT_C_ENABLE)
2684 clear_port_feature(hub->hdev, port1,
2685 USB_PORT_FEAT_C_ENABLE);
2691 int usb_disable_ltm(struct usb_device *udev)
2693 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2695 /* Check if the roothub and device supports LTM. */
2696 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2697 !usb_device_supports_ltm(udev))
2700 /* Clear Feature LTM Enable can only be sent if the device is
2703 if (!udev->actconfig)
2706 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2707 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2708 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2709 USB_CTRL_SET_TIMEOUT);
2711 EXPORT_SYMBOL_GPL(usb_disable_ltm);
2713 void usb_enable_ltm(struct usb_device *udev)
2715 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2717 /* Check if the roothub and device supports LTM. */
2718 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2719 !usb_device_supports_ltm(udev))
2722 /* Set Feature LTM Enable can only be sent if the device is
2725 if (!udev->actconfig)
2728 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2729 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2730 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2731 USB_CTRL_SET_TIMEOUT);
2733 EXPORT_SYMBOL_GPL(usb_enable_ltm);
2735 #ifdef CONFIG_USB_SUSPEND
2738 * usb_port_suspend - suspend a usb device's upstream port
2739 * @udev: device that's no longer in active use, not a root hub
2740 * Context: must be able to sleep; device not locked; pm locks held
2742 * Suspends a USB device that isn't in active use, conserving power.
2743 * Devices may wake out of a suspend, if anything important happens,
2744 * using the remote wakeup mechanism. They may also be taken out of
2745 * suspend by the host, using usb_port_resume(). It's also routine
2746 * to disconnect devices while they are suspended.
2748 * This only affects the USB hardware for a device; its interfaces
2749 * (and, for hubs, child devices) must already have been suspended.
2751 * Selective port suspend reduces power; most suspended devices draw
2752 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2753 * All devices below the suspended port are also suspended.
2755 * Devices leave suspend state when the host wakes them up. Some devices
2756 * also support "remote wakeup", where the device can activate the USB
2757 * tree above them to deliver data, such as a keypress or packet. In
2758 * some cases, this wakes the USB host.
2760 * Suspending OTG devices may trigger HNP, if that's been enabled
2761 * between a pair of dual-role devices. That will change roles, such
2762 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2764 * Devices on USB hub ports have only one "suspend" state, corresponding
2765 * to ACPI D2, "may cause the device to lose some context".
2766 * State transitions include:
2768 * - suspend, resume ... when the VBUS power link stays live
2769 * - suspend, disconnect ... VBUS lost
2771 * Once VBUS drop breaks the circuit, the port it's using has to go through
2772 * normal re-enumeration procedures, starting with enabling VBUS power.
2773 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2774 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2775 * timer, no SRP, no requests through sysfs.
2777 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2778 * the root hub for their bus goes into global suspend ... so we don't
2779 * (falsely) update the device power state to say it suspended.
2781 * Returns 0 on success, else negative errno.
2783 int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
2785 struct usb_hub *hub = hdev_to_hub(udev->parent);
2786 int port1 = udev->portnum;
2789 /* enable remote wakeup when appropriate; this lets the device
2790 * wake up the upstream hub (including maybe the root hub).
2792 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2793 * we don't explicitly enable it here.
2795 if (udev->do_remote_wakeup) {
2796 if (!hub_is_superspeed(hub->hdev)) {
2797 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2798 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2799 USB_DEVICE_REMOTE_WAKEUP, 0,
2801 USB_CTRL_SET_TIMEOUT);
2803 /* Assume there's only one function on the USB 3.0
2804 * device and enable remote wake for the first
2805 * interface. FIXME if the interface association
2806 * descriptor shows there's more than one function.
2808 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2809 USB_REQ_SET_FEATURE,
2810 USB_RECIP_INTERFACE,
2811 USB_INTRF_FUNC_SUSPEND,
2812 USB_INTRF_FUNC_SUSPEND_RW |
2813 USB_INTRF_FUNC_SUSPEND_LP,
2815 USB_CTRL_SET_TIMEOUT);
2818 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2820 /* bail if autosuspend is requested */
2821 if (PMSG_IS_AUTO(msg))
2826 /* disable USB2 hardware LPM */
2827 if (udev->usb2_hw_lpm_enabled == 1)
2828 usb_set_usb2_hardware_lpm(udev, 0);
2830 if (usb_disable_ltm(udev)) {
2831 dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2835 if (usb_unlocked_disable_lpm(udev)) {
2836 dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
2842 if (hub_is_superspeed(hub->hdev))
2843 status = set_port_feature(hub->hdev,
2844 port1 | (USB_SS_PORT_LS_U3 << 3),
2845 USB_PORT_FEAT_LINK_STATE);
2847 status = set_port_feature(hub->hdev, port1,
2848 USB_PORT_FEAT_SUSPEND);
2850 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2852 /* paranoia: "should not happen" */
2853 if (udev->do_remote_wakeup)
2854 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2855 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2856 USB_DEVICE_REMOTE_WAKEUP, 0,
2858 USB_CTRL_SET_TIMEOUT);
2860 /* Try to enable USB2 hardware LPM again */
2861 if (udev->usb2_hw_lpm_capable == 1)
2862 usb_set_usb2_hardware_lpm(udev, 1);
2864 /* Try to enable USB3 LTM and LPM again */
2865 usb_enable_ltm(udev);
2866 usb_unlocked_enable_lpm(udev);
2868 /* System sleep transitions should never fail */
2869 if (!PMSG_IS_AUTO(msg))
2872 /* device has up to 10 msec to fully suspend */
2873 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2874 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2875 udev->do_remote_wakeup);
2876 usb_set_device_state(udev, USB_STATE_SUSPENDED);
2879 usb_mark_last_busy(hub->hdev);
2884 * If the USB "suspend" state is in use (rather than "global suspend"),
2885 * many devices will be individually taken out of suspend state using
2886 * special "resume" signaling. This routine kicks in shortly after
2887 * hardware resume signaling is finished, either because of selective
2888 * resume (by host) or remote wakeup (by device) ... now see what changed
2889 * in the tree that's rooted at this device.
2891 * If @udev->reset_resume is set then the device is reset before the
2892 * status check is done.
2894 static int finish_port_resume(struct usb_device *udev)
2899 /* caller owns the udev device lock */
2900 dev_dbg(&udev->dev, "%s\n",
2901 udev->reset_resume ? "finish reset-resume" : "finish resume");
2903 /* usb ch9 identifies four variants of SUSPENDED, based on what
2904 * state the device resumes to. Linux currently won't see the
2905 * first two on the host side; they'd be inside hub_port_init()
2906 * during many timeouts, but khubd can't suspend until later.
2908 usb_set_device_state(udev, udev->actconfig
2909 ? USB_STATE_CONFIGURED
2910 : USB_STATE_ADDRESS);
2912 /* 10.5.4.5 says not to reset a suspended port if the attached
2913 * device is enabled for remote wakeup. Hence the reset
2914 * operation is carried out here, after the port has been
2917 if (udev->reset_resume)
2919 status = usb_reset_and_verify_device(udev);
2921 /* 10.5.4.5 says be sure devices in the tree are still there.
2922 * For now let's assume the device didn't go crazy on resume,
2923 * and device drivers will know about any resume quirks.
2927 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2929 status = (status > 0 ? 0 : -ENODEV);
2931 /* If a normal resume failed, try doing a reset-resume */
2932 if (status && !udev->reset_resume && udev->persist_enabled) {
2933 dev_dbg(&udev->dev, "retry with reset-resume\n");
2934 udev->reset_resume = 1;
2935 goto retry_reset_resume;
2940 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
2942 } else if (udev->actconfig) {
2943 le16_to_cpus(&devstatus);
2944 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
2945 status = usb_control_msg(udev,
2946 usb_sndctrlpipe(udev, 0),
2947 USB_REQ_CLEAR_FEATURE,
2949 USB_DEVICE_REMOTE_WAKEUP, 0,
2951 USB_CTRL_SET_TIMEOUT);
2954 "disable remote wakeup, status %d\n",
2963 * usb_port_resume - re-activate a suspended usb device's upstream port
2964 * @udev: device to re-activate, not a root hub
2965 * Context: must be able to sleep; device not locked; pm locks held
2967 * This will re-activate the suspended device, increasing power usage
2968 * while letting drivers communicate again with its endpoints.
2969 * USB resume explicitly guarantees that the power session between
2970 * the host and the device is the same as it was when the device
2973 * If @udev->reset_resume is set then this routine won't check that the
2974 * port is still enabled. Furthermore, finish_port_resume() above will
2975 * reset @udev. The end result is that a broken power session can be
2976 * recovered and @udev will appear to persist across a loss of VBUS power.
2978 * For example, if a host controller doesn't maintain VBUS suspend current
2979 * during a system sleep or is reset when the system wakes up, all the USB
2980 * power sessions below it will be broken. This is especially troublesome
2981 * for mass-storage devices containing mounted filesystems, since the
2982 * device will appear to have disconnected and all the memory mappings
2983 * to it will be lost. Using the USB_PERSIST facility, the device can be
2984 * made to appear as if it had not disconnected.
2986 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
2987 * every effort to insure that the same device is present after the
2988 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
2989 * quite possible for a device to remain unaltered but its media to be
2990 * changed. If the user replaces a flash memory card while the system is
2991 * asleep, he will have only himself to blame when the filesystem on the
2992 * new card is corrupted and the system crashes.
2994 * Returns 0 on success, else negative errno.
2996 int usb_port_resume(struct usb_device *udev, pm_message_t msg)
2998 struct usb_hub *hub = hdev_to_hub(udev->parent);
2999 int port1 = udev->portnum;
3001 u16 portchange, portstatus;
3003 /* Skip the initial Clear-Suspend step for a remote wakeup */
3004 status = hub_port_status(hub, port1, &portstatus, &portchange);
3005 if (status == 0 && !port_is_suspended(hub, portstatus))
3006 goto SuspendCleared;
3008 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
3010 set_bit(port1, hub->busy_bits);
3012 /* see 7.1.7.7; affects power usage, but not budgeting */
3013 if (hub_is_superspeed(hub->hdev))
3014 status = set_port_feature(hub->hdev,
3015 port1 | (USB_SS_PORT_LS_U0 << 3),
3016 USB_PORT_FEAT_LINK_STATE);
3018 status = clear_port_feature(hub->hdev,
3019 port1, USB_PORT_FEAT_SUSPEND);
3021 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
3024 /* drive resume for at least 20 msec */
3025 dev_dbg(&udev->dev, "usb %sresume\n",
3026 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
3029 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3030 * stop resume signaling. Then finish the resume
3033 status = hub_port_status(hub, port1, &portstatus, &portchange);
3035 /* TRSMRCY = 10 msec */
3041 if (hub_is_superspeed(hub->hdev)) {
3042 if (portchange & USB_PORT_STAT_C_LINK_STATE)
3043 clear_port_feature(hub->hdev, port1,
3044 USB_PORT_FEAT_C_PORT_LINK_STATE);
3046 if (portchange & USB_PORT_STAT_C_SUSPEND)
3047 clear_port_feature(hub->hdev, port1,
3048 USB_PORT_FEAT_C_SUSPEND);
3052 clear_bit(port1, hub->busy_bits);
3054 status = check_port_resume_type(udev,
3055 hub, port1, status, portchange, portstatus);
3057 status = finish_port_resume(udev);
3059 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3060 hub_port_logical_disconnect(hub, port1);
3062 /* Try to enable USB2 hardware LPM */
3063 if (udev->usb2_hw_lpm_capable == 1)
3064 usb_set_usb2_hardware_lpm(udev, 1);
3066 /* Try to enable USB3 LTM and LPM */
3067 usb_enable_ltm(udev);
3068 usb_unlocked_enable_lpm(udev);
3074 /* caller has locked udev */
3075 int usb_remote_wakeup(struct usb_device *udev)
3079 if (udev->state == USB_STATE_SUSPENDED) {
3080 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
3081 status = usb_autoresume_device(udev);
3083 /* Let the drivers do their thing, then... */
3084 usb_autosuspend_device(udev);
3090 #else /* CONFIG_USB_SUSPEND */
3092 /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3094 int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
3099 /* However we may need to do a reset-resume */
3101 int usb_port_resume(struct usb_device *udev, pm_message_t msg)
3103 struct usb_hub *hub = hdev_to_hub(udev->parent);
3104 int port1 = udev->portnum;
3106 u16 portchange, portstatus;
3108 status = hub_port_status(hub, port1, &portstatus, &portchange);
3109 status = check_port_resume_type(udev,
3110 hub, port1, status, portchange, portstatus);
3113 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3114 hub_port_logical_disconnect(hub, port1);
3115 } else if (udev->reset_resume) {
3116 dev_dbg(&udev->dev, "reset-resume\n");
3117 status = usb_reset_and_verify_device(udev);
3124 static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
3126 struct usb_hub *hub = usb_get_intfdata (intf);
3127 struct usb_device *hdev = hub->hdev;
3131 /* Warn if children aren't already suspended */
3132 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3133 struct usb_device *udev;
3135 udev = hub->ports[port1 - 1]->child;
3136 if (udev && udev->can_submit) {
3137 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
3138 if (PMSG_IS_AUTO(msg))
3142 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3143 /* Enable hub to send remote wakeup for all ports. */
3144 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3145 status = set_port_feature(hdev,
3147 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3148 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3149 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3150 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3154 dev_dbg(&intf->dev, "%s\n", __func__);
3156 /* stop khubd and related activity */
3157 hub_quiesce(hub, HUB_SUSPEND);
3161 static int hub_resume(struct usb_interface *intf)
3163 struct usb_hub *hub = usb_get_intfdata(intf);
3165 dev_dbg(&intf->dev, "%s\n", __func__);
3166 hub_activate(hub, HUB_RESUME);
3170 static int hub_reset_resume(struct usb_interface *intf)
3172 struct usb_hub *hub = usb_get_intfdata(intf);
3174 dev_dbg(&intf->dev, "%s\n", __func__);
3175 hub_activate(hub, HUB_RESET_RESUME);
3180 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3181 * @rhdev: struct usb_device for the root hub
3183 * The USB host controller driver calls this function when its root hub
3184 * is resumed and Vbus power has been interrupted or the controller
3185 * has been reset. The routine marks @rhdev as having lost power.
3186 * When the hub driver is resumed it will take notice and carry out
3187 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3188 * the others will be disconnected.
3190 void usb_root_hub_lost_power(struct usb_device *rhdev)
3192 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3193 rhdev->reset_resume = 1;
3195 EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3197 static const char * const usb3_lpm_names[] = {
3205 * Send a Set SEL control transfer to the device, prior to enabling
3206 * device-initiated U1 or U2. This lets the device know the exit latencies from
3207 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3208 * packet from the host.
3210 * This function will fail if the SEL or PEL values for udev are greater than
3211 * the maximum allowed values for the link state to be enabled.
3213 static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3215 struct usb_set_sel_req *sel_values;
3216 unsigned long long u1_sel;
3217 unsigned long long u1_pel;
3218 unsigned long long u2_sel;
3219 unsigned long long u2_pel;
3222 /* Convert SEL and PEL stored in ns to us */
3223 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3224 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3225 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3226 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3229 * Make sure that the calculated SEL and PEL values for the link
3230 * state we're enabling aren't bigger than the max SEL/PEL
3231 * value that will fit in the SET SEL control transfer.
3232 * Otherwise the device would get an incorrect idea of the exit
3233 * latency for the link state, and could start a device-initiated
3234 * U1/U2 when the exit latencies are too high.
3236 if ((state == USB3_LPM_U1 &&
3237 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3238 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3239 (state == USB3_LPM_U2 &&
3240 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3241 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
3242 dev_dbg(&udev->dev, "Device-initiated %s disabled due "
3243 "to long SEL %llu ms or PEL %llu ms\n",
3244 usb3_lpm_names[state], u1_sel, u1_pel);
3249 * If we're enabling device-initiated LPM for one link state,
3250 * but the other link state has a too high SEL or PEL value,
3251 * just set those values to the max in the Set SEL request.
3253 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3254 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3256 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3257 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3259 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3260 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3262 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3263 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3266 * usb_enable_lpm() can be called as part of a failed device reset,
3267 * which may be initiated by an error path of a mass storage driver.
3268 * Therefore, use GFP_NOIO.
3270 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3274 sel_values->u1_sel = u1_sel;
3275 sel_values->u1_pel = u1_pel;
3276 sel_values->u2_sel = cpu_to_le16(u2_sel);
3277 sel_values->u2_pel = cpu_to_le16(u2_pel);
3279 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3283 sel_values, sizeof *(sel_values),
3284 USB_CTRL_SET_TIMEOUT);
3290 * Enable or disable device-initiated U1 or U2 transitions.
3292 static int usb_set_device_initiated_lpm(struct usb_device *udev,
3293 enum usb3_link_state state, bool enable)
3300 feature = USB_DEVICE_U1_ENABLE;
3303 feature = USB_DEVICE_U2_ENABLE;
3306 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3307 __func__, enable ? "enable" : "disable");
3311 if (udev->state != USB_STATE_CONFIGURED) {
3312 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3313 "for unconfigured device.\n",
3314 __func__, enable ? "enable" : "disable",
3315 usb3_lpm_names[state]);
3321 * First, let the device know about the exit latencies
3322 * associated with the link state we're about to enable.
3324 ret = usb_req_set_sel(udev, state);
3326 dev_warn(&udev->dev, "Set SEL for device-initiated "
3327 "%s failed.\n", usb3_lpm_names[state]);
3331 * Now send the control transfer to enable device-initiated LPM
3332 * for either U1 or U2.
3334 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3335 USB_REQ_SET_FEATURE,
3339 USB_CTRL_SET_TIMEOUT);
3341 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3342 USB_REQ_CLEAR_FEATURE,
3346 USB_CTRL_SET_TIMEOUT);
3349 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3350 enable ? "Enable" : "Disable",
3351 usb3_lpm_names[state]);
3357 static int usb_set_lpm_timeout(struct usb_device *udev,
3358 enum usb3_link_state state, int timeout)
3365 feature = USB_PORT_FEAT_U1_TIMEOUT;
3368 feature = USB_PORT_FEAT_U2_TIMEOUT;
3371 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3376 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3377 timeout != USB3_LPM_DEVICE_INITIATED) {
3378 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3379 "which is a reserved value.\n",
3380 usb3_lpm_names[state], timeout);
3384 ret = set_port_feature(udev->parent,
3385 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3388 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3389 "error code %i\n", usb3_lpm_names[state],
3393 if (state == USB3_LPM_U1)
3394 udev->u1_params.timeout = timeout;
3396 udev->u2_params.timeout = timeout;
3401 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3404 * We will attempt to enable U1 or U2, but there are no guarantees that the
3405 * control transfers to set the hub timeout or enable device-initiated U1/U2
3406 * will be successful.
3408 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3409 * driver know about it. If that call fails, it should be harmless, and just
3410 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3412 static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3413 enum usb3_link_state state)
3417 /* We allow the host controller to set the U1/U2 timeout internally
3418 * first, so that it can change its schedule to account for the
3419 * additional latency to send data to a device in a lower power
3422 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3424 /* xHCI host controller doesn't want to enable this LPM state. */
3429 dev_warn(&udev->dev, "Could not enable %s link state, "
3430 "xHCI error %i.\n", usb3_lpm_names[state],
3435 if (usb_set_lpm_timeout(udev, state, timeout))
3436 /* If we can't set the parent hub U1/U2 timeout,
3437 * device-initiated LPM won't be allowed either, so let the xHCI
3438 * host know that this link state won't be enabled.
3440 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3442 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3443 else if (udev->actconfig)
3444 usb_set_device_initiated_lpm(udev, state, true);
3449 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3452 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3453 * If zero is returned, the parent will not allow the link to go into U1/U2.
3455 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3456 * it won't have an effect on the bus link state because the parent hub will
3457 * still disallow device-initiated U1/U2 entry.
3459 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3460 * possible. The result will be slightly more bus bandwidth will be taken up
3461 * (to account for U1/U2 exit latency), but it should be harmless.
3463 static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3464 enum usb3_link_state state)
3470 feature = USB_PORT_FEAT_U1_TIMEOUT;
3473 feature = USB_PORT_FEAT_U2_TIMEOUT;
3476 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3481 if (usb_set_lpm_timeout(udev, state, 0))
3484 usb_set_device_initiated_lpm(udev, state, false);
3486 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3487 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3488 "bus schedule bandwidth may be impacted.\n",
3489 usb3_lpm_names[state]);
3494 * Disable hub-initiated and device-initiated U1 and U2 entry.
3495 * Caller must own the bandwidth_mutex.
3497 * This will call usb_enable_lpm() on failure, which will decrement
3498 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3500 int usb_disable_lpm(struct usb_device *udev)
3502 struct usb_hcd *hcd;
3504 if (!udev || !udev->parent ||
3505 udev->speed != USB_SPEED_SUPER ||
3509 hcd = bus_to_hcd(udev->bus);
3510 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3513 udev->lpm_disable_count++;
3514 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
3517 /* If LPM is enabled, attempt to disable it. */
3518 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3520 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3526 usb_enable_lpm(udev);
3529 EXPORT_SYMBOL_GPL(usb_disable_lpm);
3531 /* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3532 int usb_unlocked_disable_lpm(struct usb_device *udev)
3534 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3540 mutex_lock(hcd->bandwidth_mutex);
3541 ret = usb_disable_lpm(udev);
3542 mutex_unlock(hcd->bandwidth_mutex);
3546 EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3549 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3550 * xHCI host policy may prevent U1 or U2 from being enabled.
3552 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3553 * until the lpm_disable_count drops to zero. Caller must own the
3556 void usb_enable_lpm(struct usb_device *udev)
3558 struct usb_hcd *hcd;
3560 if (!udev || !udev->parent ||
3561 udev->speed != USB_SPEED_SUPER ||
3565 udev->lpm_disable_count--;
3566 hcd = bus_to_hcd(udev->bus);
3567 /* Double check that we can both enable and disable LPM.
3568 * Device must be configured to accept set feature U1/U2 timeout.
3570 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3571 !hcd->driver->disable_usb3_lpm_timeout)
3574 if (udev->lpm_disable_count > 0)
3577 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3578 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3580 EXPORT_SYMBOL_GPL(usb_enable_lpm);
3582 /* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3583 void usb_unlocked_enable_lpm(struct usb_device *udev)
3585 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3590 mutex_lock(hcd->bandwidth_mutex);
3591 usb_enable_lpm(udev);
3592 mutex_unlock(hcd->bandwidth_mutex);
3594 EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3597 #else /* CONFIG_PM */
3599 #define hub_suspend NULL
3600 #define hub_resume NULL
3601 #define hub_reset_resume NULL
3603 int usb_disable_lpm(struct usb_device *udev)
3607 EXPORT_SYMBOL_GPL(usb_disable_lpm);
3609 void usb_enable_lpm(struct usb_device *udev) { }
3610 EXPORT_SYMBOL_GPL(usb_enable_lpm);
3612 int usb_unlocked_disable_lpm(struct usb_device *udev)
3616 EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3618 void usb_unlocked_enable_lpm(struct usb_device *udev) { }
3619 EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3621 int usb_disable_ltm(struct usb_device *udev)
3625 EXPORT_SYMBOL_GPL(usb_disable_ltm);
3627 void usb_enable_ltm(struct usb_device *udev) { }
3628 EXPORT_SYMBOL_GPL(usb_enable_ltm);
3632 /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3634 * Between connect detection and reset signaling there must be a delay
3635 * of 100ms at least for debounce and power-settling. The corresponding
3636 * timer shall restart whenever the downstream port detects a disconnect.
3638 * Apparently there are some bluetooth and irda-dongles and a number of
3639 * low-speed devices for which this debounce period may last over a second.
3640 * Not covered by the spec - but easy to deal with.
3642 * This implementation uses a 1500ms total debounce timeout; if the
3643 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3644 * every 25ms for transient disconnects. When the port status has been
3645 * unchanged for 100ms it returns the port status.
3647 static int hub_port_debounce(struct usb_hub *hub, int port1)
3650 int total_time, stable_time = 0;
3651 u16 portchange, portstatus;
3652 unsigned connection = 0xffff;
3654 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3655 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3659 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3660 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3661 stable_time += HUB_DEBOUNCE_STEP;
3662 if (stable_time >= HUB_DEBOUNCE_STABLE)
3666 connection = portstatus & USB_PORT_STAT_CONNECTION;
3669 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3670 clear_port_feature(hub->hdev, port1,
3671 USB_PORT_FEAT_C_CONNECTION);
3674 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3676 msleep(HUB_DEBOUNCE_STEP);
3679 dev_dbg (hub->intfdev,
3680 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3681 port1, total_time, stable_time, portstatus);
3683 if (stable_time < HUB_DEBOUNCE_STABLE)
3688 void usb_ep0_reinit(struct usb_device *udev)
3690 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3691 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
3692 usb_enable_endpoint(udev, &udev->ep0, true);
3694 EXPORT_SYMBOL_GPL(usb_ep0_reinit);
3696 #define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3697 #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3699 static int hub_set_address(struct usb_device *udev, int devnum)
3702 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3705 * The host controller will choose the device address,
3706 * instead of the core having chosen it earlier
3708 if (!hcd->driver->address_device && devnum <= 1)
3710 if (udev->state == USB_STATE_ADDRESS)
3712 if (udev->state != USB_STATE_DEFAULT)
3714 if (hcd->driver->address_device)
3715 retval = hcd->driver->address_device(hcd, udev);
3717 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3718 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3719 NULL, 0, USB_CTRL_SET_TIMEOUT);
3721 update_devnum(udev, devnum);
3722 /* Device now using proper address. */
3723 usb_set_device_state(udev, USB_STATE_ADDRESS);
3724 usb_ep0_reinit(udev);
3729 /* Reset device, (re)assign address, get device descriptor.
3730 * Device connection must be stable, no more debouncing needed.
3731 * Returns device in USB_STATE_ADDRESS, except on error.
3733 * If this is called for an already-existing device (as part of
3734 * usb_reset_and_verify_device), the caller must own the device lock. For a
3735 * newly detected device that is not accessible through any global
3736 * pointers, it's not necessary to lock the device.
3739 hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3742 static DEFINE_MUTEX(usb_address0_mutex);
3744 struct usb_device *hdev = hub->hdev;
3745 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
3747 unsigned delay = HUB_SHORT_RESET_TIME;
3748 enum usb_device_speed oldspeed = udev->speed;
3750 int devnum = udev->devnum;
3752 /* root hub ports have a slightly longer reset period
3753 * (from USB 2.0 spec, section 7.1.7.5)
3755 if (!hdev->parent) {
3756 delay = HUB_ROOT_RESET_TIME;
3757 if (port1 == hdev->bus->otg_port)
3758 hdev->bus->b_hnp_enable = 0;
3761 /* Some low speed devices have problems with the quick delay, so */
3762 /* be a bit pessimistic with those devices. RHbug #23670 */
3763 if (oldspeed == USB_SPEED_LOW)
3764 delay = HUB_LONG_RESET_TIME;
3766 mutex_lock(&usb_address0_mutex);
3768 /* Reset the device; full speed may morph to high speed */
3769 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
3770 retval = hub_port_reset(hub, port1, udev, delay, false);
3771 if (retval < 0) /* error or disconnect */
3773 /* success, speed is known */
3777 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3778 dev_dbg(&udev->dev, "device reset changed speed!\n");
3781 oldspeed = udev->speed;
3783 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3784 * it's fixed size except for full speed devices.
3785 * For Wireless USB devices, ep0 max packet is always 512 (tho
3786 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
3788 switch (udev->speed) {
3789 case USB_SPEED_SUPER:
3790 case USB_SPEED_WIRELESS: /* fixed at 512 */
3791 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
3793 case USB_SPEED_HIGH: /* fixed at 64 */
3794 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
3796 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3797 /* to determine the ep0 maxpacket size, try to read
3798 * the device descriptor to get bMaxPacketSize0 and
3799 * then correct our initial guess.
3801 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
3803 case USB_SPEED_LOW: /* fixed at 8 */
3804 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
3810 if (udev->speed == USB_SPEED_WIRELESS)
3811 speed = "variable speed Wireless";
3813 speed = usb_speed_string(udev->speed);
3815 if (udev->speed != USB_SPEED_SUPER)
3816 dev_info(&udev->dev,
3817 "%s %s USB device number %d using %s\n",
3818 (udev->config) ? "reset" : "new", speed,
3819 devnum, udev->bus->controller->driver->name);
3821 /* Set up TT records, if needed */
3823 udev->tt = hdev->tt;
3824 udev->ttport = hdev->ttport;
3825 } else if (udev->speed != USB_SPEED_HIGH
3826 && hdev->speed == USB_SPEED_HIGH) {
3828 dev_err(&udev->dev, "parent hub has no TT\n");
3832 udev->tt = &hub->tt;
3833 udev->ttport = port1;
3836 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3837 * Because device hardware and firmware is sometimes buggy in
3838 * this area, and this is how Linux has done it for ages.
3839 * Change it cautiously.
3841 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3842 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3843 * so it may help with some non-standards-compliant devices.
3844 * Otherwise we start with SET_ADDRESS and then try to read the
3845 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3848 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
3849 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
3850 struct usb_device_descriptor *buf;
3853 #define GET_DESCRIPTOR_BUFSIZE 64
3854 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3860 /* Retry on all errors; some devices are flakey.
3861 * 255 is for WUSB devices, we actually need to use
3862 * 512 (WUSB1.0[4.8.1]).
3864 for (j = 0; j < 3; ++j) {
3865 buf->bMaxPacketSize0 = 0;
3866 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
3867 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
3868 USB_DT_DEVICE << 8, 0,
3869 buf, GET_DESCRIPTOR_BUFSIZE,
3870 initial_descriptor_timeout);
3871 switch (buf->bMaxPacketSize0) {
3872 case 8: case 16: case 32: case 64: case 255:
3873 if (buf->bDescriptorType ==
3887 udev->descriptor.bMaxPacketSize0 =
3888 buf->bMaxPacketSize0;
3891 retval = hub_port_reset(hub, port1, udev, delay, false);
3892 if (retval < 0) /* error or disconnect */
3894 if (oldspeed != udev->speed) {
3896 "device reset changed speed!\n");
3902 "device descriptor read/64, error %d\n",
3907 #undef GET_DESCRIPTOR_BUFSIZE
3911 * If device is WUSB, we already assigned an
3912 * unauthorized address in the Connect Ack sequence;
3913 * authorization will assign the final address.
3915 if (udev->wusb == 0) {
3916 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
3917 retval = hub_set_address(udev, devnum);
3924 "device not accepting address %d, error %d\n",
3928 if (udev->speed == USB_SPEED_SUPER) {
3929 devnum = udev->devnum;
3930 dev_info(&udev->dev,
3931 "%s SuperSpeed USB device number %d using %s\n",
3932 (udev->config) ? "reset" : "new",
3933 devnum, udev->bus->controller->driver->name);
3936 /* cope with hardware quirkiness:
3937 * - let SET_ADDRESS settle, some device hardware wants it
3938 * - read ep0 maxpacket even for high and low speed,
3941 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
3945 retval = usb_get_device_descriptor(udev, 8);
3948 "device descriptor read/8, error %d\n",
3961 * Some superspeed devices have finished the link training process
3962 * and attached to a superspeed hub port, but the device descriptor
3963 * got from those devices show they aren't superspeed devices. Warm
3964 * reset the port attached by the devices can fix them.
3966 if ((udev->speed == USB_SPEED_SUPER) &&
3967 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
3968 dev_err(&udev->dev, "got a wrong device descriptor, "
3969 "warm reset device\n");
3970 hub_port_reset(hub, port1, udev,
3971 HUB_BH_RESET_TIME, true);
3976 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
3977 udev->speed == USB_SPEED_SUPER)
3980 i = udev->descriptor.bMaxPacketSize0;
3981 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
3982 if (udev->speed == USB_SPEED_LOW ||
3983 !(i == 8 || i == 16 || i == 32 || i == 64)) {
3984 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
3988 if (udev->speed == USB_SPEED_FULL)
3989 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
3991 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
3992 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
3993 usb_ep0_reinit(udev);
3996 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
3997 if (retval < (signed)sizeof(udev->descriptor)) {
3998 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4005 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4006 retval = usb_get_bos_descriptor(udev);
4008 udev->lpm_capable = usb_device_supports_lpm(udev);
4009 usb_set_lpm_parameters(udev);
4014 /* notify HCD that we have a device connected and addressed */
4015 if (hcd->driver->update_device)
4016 hcd->driver->update_device(hcd, udev);
4019 hub_port_disable(hub, port1, 0);
4020 update_devnum(udev, devnum); /* for disconnect processing */
4022 mutex_unlock(&usb_address0_mutex);
4027 check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4029 struct usb_qualifier_descriptor *qual;
4032 qual = kmalloc (sizeof *qual, GFP_KERNEL);
4036 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4037 qual, sizeof *qual);
4038 if (status == sizeof *qual) {
4039 dev_info(&udev->dev, "not running at top speed; "
4040 "connect to a high speed hub\n");
4041 /* hub LEDs are probably harder to miss than syslog */
4042 if (hub->has_indicators) {
4043 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
4044 schedule_delayed_work (&hub->leds, 0);
4051 hub_power_remaining (struct usb_hub *hub)
4053 struct usb_device *hdev = hub->hdev;
4057 if (!hub->limited_power)
4060 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4061 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
4062 struct usb_device *udev = hub->ports[port1 - 1]->child;
4068 /* Unconfigured devices may not use more than 100mA,
4069 * or 8mA for OTG ports */
4070 if (udev->actconfig)
4071 delta = udev->actconfig->desc.bMaxPower * 2;
4072 else if (port1 != udev->bus->otg_port || hdev->parent)
4076 if (delta > hub->mA_per_port)
4077 dev_warn(&udev->dev,
4078 "%dmA is over %umA budget for port %d!\n",
4079 delta, hub->mA_per_port, port1);
4082 if (remaining < 0) {
4083 dev_warn(hub->intfdev, "%dmA over power budget!\n",
4090 /* Handle physical or logical connection change events.
4091 * This routine is called when:
4092 * a port connection-change occurs;
4093 * a port enable-change occurs (often caused by EMI);
4094 * usb_reset_and_verify_device() encounters changed descriptors (as from
4095 * a firmware download)
4096 * caller already locked the hub
4098 static void hub_port_connect_change(struct usb_hub *hub, int port1,
4099 u16 portstatus, u16 portchange)
4101 struct usb_device *hdev = hub->hdev;
4102 struct device *hub_dev = hub->intfdev;
4103 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
4104 unsigned wHubCharacteristics =
4105 le16_to_cpu(hub->descriptor->wHubCharacteristics);
4106 struct usb_device *udev;
4110 "port %d, status %04x, change %04x, %s\n",
4111 port1, portstatus, portchange, portspeed(hub, portstatus));
4113 if (hub->has_indicators) {
4114 set_port_led(hub, port1, HUB_LED_AUTO);
4115 hub->indicator[port1-1] = INDICATOR_AUTO;
4118 #ifdef CONFIG_USB_OTG
4119 /* during HNP, don't repeat the debounce */
4120 if (hdev->bus->is_b_host)
4121 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4122 USB_PORT_STAT_C_ENABLE);
4125 /* Try to resuscitate an existing device */
4126 udev = hub->ports[port1 - 1]->child;
4127 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4128 udev->state != USB_STATE_NOTATTACHED) {
4129 usb_lock_device(udev);
4130 if (portstatus & USB_PORT_STAT_ENABLE) {
4131 status = 0; /* Nothing to do */
4133 #ifdef CONFIG_USB_SUSPEND
4134 } else if (udev->state == USB_STATE_SUSPENDED &&
4135 udev->persist_enabled) {
4136 /* For a suspended device, treat this as a
4137 * remote wakeup event.
4139 status = usb_remote_wakeup(udev);
4143 status = -ENODEV; /* Don't resuscitate */
4145 usb_unlock_device(udev);
4148 clear_bit(port1, hub->change_bits);
4153 /* Disconnect any existing devices under this port */
4155 usb_disconnect(&hub->ports[port1 - 1]->child);
4156 clear_bit(port1, hub->change_bits);
4158 /* We can forget about a "removed" device when there's a physical
4159 * disconnect or the connect status changes.
4161 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4162 (portchange & USB_PORT_STAT_C_CONNECTION))
4163 clear_bit(port1, hub->removed_bits);
4165 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4166 USB_PORT_STAT_C_ENABLE)) {
4167 status = hub_port_debounce(hub, port1);
4169 if (printk_ratelimit())
4170 dev_err(hub_dev, "connect-debounce failed, "
4171 "port %d disabled\n", port1);
4172 portstatus &= ~USB_PORT_STAT_CONNECTION;
4174 portstatus = status;
4178 if (hcd->phy && !hdev->parent) {
4179 if (portstatus & USB_PORT_STAT_CONNECTION)
4180 usb_phy_notify_connect(hcd->phy, port1);
4182 usb_phy_notify_disconnect(hcd->phy, port1);
4185 /* Return now if debouncing failed or nothing is connected or
4186 * the device was "removed".
4188 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4189 test_bit(port1, hub->removed_bits)) {
4191 /* maybe switch power back on (e.g. root hub was reset) */
4192 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
4193 && !port_is_power_on(hub, portstatus))
4194 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
4196 if (portstatus & USB_PORT_STAT_ENABLE)
4201 for (i = 0; i < SET_CONFIG_TRIES; i++) {
4203 /* reallocate for each attempt, since references
4204 * to the previous one can escape in various ways
4206 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4209 "couldn't allocate port %d usb_device\n",
4214 usb_set_device_state(udev, USB_STATE_POWERED);
4215 udev->bus_mA = hub->mA_per_port;
4216 udev->level = hdev->level + 1;
4217 udev->wusb = hub_is_wusb(hub);
4219 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4220 if (hub_is_superspeed(hub->hdev))
4221 udev->speed = USB_SPEED_SUPER;
4223 udev->speed = USB_SPEED_UNKNOWN;
4225 choose_devnum(udev);
4226 if (udev->devnum <= 0) {
4227 status = -ENOTCONN; /* Don't retry */
4231 /* reset (non-USB 3.0 devices) and get descriptor */
4232 status = hub_port_init(hub, udev, port1, i);
4236 usb_detect_quirks(udev);
4237 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4240 /* consecutive bus-powered hubs aren't reliable; they can
4241 * violate the voltage drop budget. if the new child has
4242 * a "powered" LED, users should notice we didn't enable it
4243 * (without reading syslog), even without per-port LEDs
4246 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
4247 && udev->bus_mA <= 100) {
4250 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4253 dev_dbg(&udev->dev, "get status %d ?\n", status);
4256 le16_to_cpus(&devstat);
4257 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4259 "can't connect bus-powered hub "
4261 if (hub->has_indicators) {
4262 hub->indicator[port1-1] =
4263 INDICATOR_AMBER_BLINK;
4264 schedule_delayed_work (&hub->leds, 0);
4266 status = -ENOTCONN; /* Don't retry */
4271 /* check for devices running slower than they could */
4272 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4273 && udev->speed == USB_SPEED_FULL
4274 && highspeed_hubs != 0)
4275 check_highspeed (hub, udev, port1);
4277 /* Store the parent's children[] pointer. At this point
4278 * udev becomes globally accessible, although presumably
4279 * no one will look at it until hdev is unlocked.
4283 /* We mustn't add new devices if the parent hub has
4284 * been disconnected; we would race with the
4285 * recursively_mark_NOTATTACHED() routine.
4287 spin_lock_irq(&device_state_lock);
4288 if (hdev->state == USB_STATE_NOTATTACHED)
4291 hub->ports[port1 - 1]->child = udev;
4292 spin_unlock_irq(&device_state_lock);
4294 /* Run it through the hoops (find a driver, etc) */
4296 status = usb_new_device(udev);
4298 spin_lock_irq(&device_state_lock);
4299 hub->ports[port1 - 1]->child = NULL;
4300 spin_unlock_irq(&device_state_lock);
4307 status = hub_power_remaining(hub);
4309 dev_dbg(hub_dev, "%dmA power budget left\n", status);
4314 hub_port_disable(hub, port1, 1);
4316 usb_ep0_reinit(udev);
4317 release_devnum(udev);
4320 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
4323 if (hub->hdev->parent ||
4324 !hcd->driver->port_handed_over ||
4325 !(hcd->driver->port_handed_over)(hcd, port1))
4326 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4330 hub_port_disable(hub, port1, 1);
4331 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4332 hcd->driver->relinquish_port(hcd, port1);
4335 /* Returns 1 if there was a remote wakeup and a connect status change. */
4336 static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
4337 u16 portstatus, u16 portchange)
4339 struct usb_device *hdev;
4340 struct usb_device *udev;
4341 int connect_change = 0;
4345 udev = hub->ports[port - 1]->child;
4346 if (!hub_is_superspeed(hdev)) {
4347 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4349 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
4351 if (!udev || udev->state != USB_STATE_SUSPENDED ||
4352 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4358 /* TRSMRCY = 10 msec */
4361 usb_lock_device(udev);
4362 ret = usb_remote_wakeup(udev);
4363 usb_unlock_device(udev);
4368 hub_port_disable(hub, port, 1);
4370 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4372 return connect_change;
4375 static void hub_events(void)
4377 struct list_head *tmp;
4378 struct usb_device *hdev;
4379 struct usb_interface *intf;
4380 struct usb_hub *hub;
4381 struct device *hub_dev;
4387 int connect_change, wakeup_change;
4390 * We restart the list every time to avoid a deadlock with
4391 * deleting hubs downstream from this one. This should be
4392 * safe since we delete the hub from the event list.
4393 * Not the most efficient, but avoids deadlocks.
4397 /* Grab the first entry at the beginning of the list */
4398 spin_lock_irq(&hub_event_lock);
4399 if (list_empty(&hub_event_list)) {
4400 spin_unlock_irq(&hub_event_lock);
4404 tmp = hub_event_list.next;
4407 hub = list_entry(tmp, struct usb_hub, event_list);
4408 kref_get(&hub->kref);
4409 spin_unlock_irq(&hub_event_lock);
4412 hub_dev = hub->intfdev;
4413 intf = to_usb_interface(hub_dev);
4414 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
4415 hdev->state, hub->descriptor
4416 ? hub->descriptor->bNbrPorts
4418 /* NOTE: expects max 15 ports... */
4419 (u16) hub->change_bits[0],
4420 (u16) hub->event_bits[0]);
4422 /* Lock the device, then check to see if we were
4423 * disconnected while waiting for the lock to succeed. */
4424 usb_lock_device(hdev);
4425 if (unlikely(hub->disconnected))
4426 goto loop_disconnected;
4428 /* If the hub has died, clean up after it */
4429 if (hdev->state == USB_STATE_NOTATTACHED) {
4430 hub->error = -ENODEV;
4431 hub_quiesce(hub, HUB_DISCONNECT);
4436 ret = usb_autopm_get_interface(intf);
4438 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
4442 /* If this is an inactive hub, do nothing */
4447 dev_dbg (hub_dev, "resetting for error %d\n",
4450 ret = usb_reset_device(hdev);
4453 "error resetting hub: %d\n", ret);
4461 /* deal with port status changes */
4462 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4463 if (test_bit(i, hub->busy_bits))
4465 connect_change = test_bit(i, hub->change_bits);
4466 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
4467 if (!test_and_clear_bit(i, hub->event_bits) &&
4468 !connect_change && !wakeup_change)
4471 ret = hub_port_status(hub, i,
4472 &portstatus, &portchange);
4476 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4477 clear_port_feature(hdev, i,
4478 USB_PORT_FEAT_C_CONNECTION);
4482 if (portchange & USB_PORT_STAT_C_ENABLE) {
4483 if (!connect_change)
4485 "port %d enable change, "
4488 clear_port_feature(hdev, i,
4489 USB_PORT_FEAT_C_ENABLE);
4492 * EM interference sometimes causes badly
4493 * shielded USB devices to be shutdown by
4494 * the hub, this hack enables them again.
4495 * Works at least with mouse driver.
4497 if (!(portstatus & USB_PORT_STAT_ENABLE)
4499 && hub->ports[i - 1]->child) {
4502 "disabled by hub (EMI?), "
4509 if (hub_handle_remote_wakeup(hub, i,
4510 portstatus, portchange))
4513 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
4517 dev_dbg(hub_dev, "over-current change on port "
4519 clear_port_feature(hdev, i,
4520 USB_PORT_FEAT_C_OVER_CURRENT);
4521 msleep(100); /* Cool down */
4522 hub_power_on(hub, true);
4523 hub_port_status(hub, i, &status, &unused);
4524 if (status & USB_PORT_STAT_OVERCURRENT)
4525 dev_err(hub_dev, "over-current "
4526 "condition on port %d\n", i);
4529 if (portchange & USB_PORT_STAT_C_RESET) {
4531 "reset change on port %d\n",
4533 clear_port_feature(hdev, i,
4534 USB_PORT_FEAT_C_RESET);
4536 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4537 hub_is_superspeed(hub->hdev)) {
4539 "warm reset change on port %d\n",
4541 clear_port_feature(hdev, i,
4542 USB_PORT_FEAT_C_BH_PORT_RESET);
4544 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4545 clear_port_feature(hub->hdev, i,
4546 USB_PORT_FEAT_C_PORT_LINK_STATE);
4548 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4550 "config error on port %d\n",
4552 clear_port_feature(hub->hdev, i,
4553 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4556 /* Warm reset a USB3 protocol port if it's in
4557 * SS.Inactive state.
4559 if (hub_port_warm_reset_required(hub, portstatus)) {
4560 dev_dbg(hub_dev, "warm reset port %d\n", i);
4561 hub_port_reset(hub, i, NULL,
4562 HUB_BH_RESET_TIME, true);
4566 hub_port_connect_change(hub, i,
4567 portstatus, portchange);
4570 /* deal with hub status changes */
4571 if (test_and_clear_bit(0, hub->event_bits) == 0)
4573 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4574 dev_err (hub_dev, "get_hub_status failed\n");
4576 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4577 dev_dbg (hub_dev, "power change\n");
4578 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
4579 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4580 /* FIXME: Is this always true? */
4581 hub->limited_power = 1;
4583 hub->limited_power = 0;
4585 if (hubchange & HUB_CHANGE_OVERCURRENT) {
4589 dev_dbg(hub_dev, "over-current change\n");
4590 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
4591 msleep(500); /* Cool down */
4592 hub_power_on(hub, true);
4593 hub_hub_status(hub, &status, &unused);
4594 if (status & HUB_STATUS_OVERCURRENT)
4595 dev_err(hub_dev, "over-current "
4601 /* Balance the usb_autopm_get_interface() above */
4602 usb_autopm_put_interface_no_suspend(intf);
4604 /* Balance the usb_autopm_get_interface_no_resume() in
4605 * kick_khubd() and allow autosuspend.
4607 usb_autopm_put_interface(intf);
4609 usb_unlock_device(hdev);
4610 kref_put(&hub->kref, hub_release);
4612 } /* end while (1) */
4615 static int hub_thread(void *__unused)
4617 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4618 * port handover. Otherwise it might see that a full-speed device
4619 * was gone before the EHCI controller had handed its port over to
4620 * the companion full-speed controller.
4626 wait_event_freezable(khubd_wait,
4627 !list_empty(&hub_event_list) ||
4628 kthread_should_stop());
4629 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
4631 pr_debug("%s: khubd exiting\n", usbcore_name);
4635 static const struct usb_device_id hub_id_table[] = {
4636 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4637 .bDeviceClass = USB_CLASS_HUB},
4638 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4639 .bInterfaceClass = USB_CLASS_HUB},
4640 { } /* Terminating entry */
4643 MODULE_DEVICE_TABLE (usb, hub_id_table);
4645 static struct usb_driver hub_driver = {
4648 .disconnect = hub_disconnect,
4649 .suspend = hub_suspend,
4650 .resume = hub_resume,
4651 .reset_resume = hub_reset_resume,
4652 .pre_reset = hub_pre_reset,
4653 .post_reset = hub_post_reset,
4654 .unlocked_ioctl = hub_ioctl,
4655 .id_table = hub_id_table,
4656 .supports_autosuspend = 1,
4659 int usb_hub_init(void)
4661 if (usb_register(&hub_driver) < 0) {
4662 printk(KERN_ERR "%s: can't register hub driver\n",
4667 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4668 if (!IS_ERR(khubd_task))
4671 /* Fall through if kernel_thread failed */
4672 usb_deregister(&hub_driver);
4673 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4678 void usb_hub_cleanup(void)
4680 kthread_stop(khubd_task);
4683 * Hub resources are freed for us by usb_deregister. It calls
4684 * usb_driver_purge on every device which in turn calls that
4685 * devices disconnect function if it is using this driver.
4686 * The hub_disconnect function takes care of releasing the
4687 * individual hub resources. -greg
4689 usb_deregister(&hub_driver);
4690 } /* usb_hub_cleanup() */
4692 static int descriptors_changed(struct usb_device *udev,
4693 struct usb_device_descriptor *old_device_descriptor)
4697 unsigned serial_len = 0;
4699 unsigned old_length;
4703 if (memcmp(&udev->descriptor, old_device_descriptor,
4704 sizeof(*old_device_descriptor)) != 0)
4707 /* Since the idVendor, idProduct, and bcdDevice values in the
4708 * device descriptor haven't changed, we will assume the
4709 * Manufacturer and Product strings haven't changed either.
4710 * But the SerialNumber string could be different (e.g., a
4711 * different flash card of the same brand).
4714 serial_len = strlen(udev->serial) + 1;
4717 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
4718 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4719 len = max(len, old_length);
4722 buf = kmalloc(len, GFP_NOIO);
4724 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4725 /* assume the worst */
4728 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
4729 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4730 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4732 if (length != old_length) {
4733 dev_dbg(&udev->dev, "config index %d, error %d\n",
4738 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4740 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
4742 ((struct usb_config_descriptor *) buf)->
4743 bConfigurationValue);
4749 if (!changed && serial_len) {
4750 length = usb_string(udev, udev->descriptor.iSerialNumber,
4752 if (length + 1 != serial_len) {
4753 dev_dbg(&udev->dev, "serial string error %d\n",
4756 } else if (memcmp(buf, udev->serial, length) != 0) {
4757 dev_dbg(&udev->dev, "serial string changed\n");
4767 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
4768 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4770 * WARNING - don't use this routine to reset a composite device
4771 * (one with multiple interfaces owned by separate drivers)!
4772 * Use usb_reset_device() instead.
4774 * Do a port reset, reassign the device's address, and establish its
4775 * former operating configuration. If the reset fails, or the device's
4776 * descriptors change from their values before the reset, or the original
4777 * configuration and altsettings cannot be restored, a flag will be set
4778 * telling khubd to pretend the device has been disconnected and then
4779 * re-connected. All drivers will be unbound, and the device will be
4780 * re-enumerated and probed all over again.
4782 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4783 * flagged for logical disconnection, or some other negative error code
4784 * if the reset wasn't even attempted.
4786 * The caller must own the device lock. For example, it's safe to use
4787 * this from a driver probe() routine after downloading new firmware.
4788 * For calls that might not occur during probe(), drivers should lock
4789 * the device using usb_lock_device_for_reset().
4791 * Locking exception: This routine may also be called from within an
4792 * autoresume handler. Such usage won't conflict with other tasks
4793 * holding the device lock because these tasks should always call
4794 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
4796 static int usb_reset_and_verify_device(struct usb_device *udev)
4798 struct usb_device *parent_hdev = udev->parent;
4799 struct usb_hub *parent_hub;
4800 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4801 struct usb_device_descriptor descriptor = udev->descriptor;
4803 int port1 = udev->portnum;
4805 if (udev->state == USB_STATE_NOTATTACHED ||
4806 udev->state == USB_STATE_SUSPENDED) {
4807 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4813 /* this requires hcd-specific logic; see ohci_restart() */
4814 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
4817 parent_hub = hdev_to_hub(parent_hdev);
4819 /* Disable LPM and LTM while we reset the device and reinstall the alt
4820 * settings. Device-initiated LPM settings, and system exit latency
4821 * settings are cleared when the device is reset, so we have to set
4824 ret = usb_unlocked_disable_lpm(udev);
4826 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
4829 ret = usb_disable_ltm(udev);
4831 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
4836 set_bit(port1, parent_hub->busy_bits);
4837 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4839 /* ep0 maxpacket size may change; let the HCD know about it.
4840 * Other endpoints will be handled by re-enumeration. */
4841 usb_ep0_reinit(udev);
4842 ret = hub_port_init(parent_hub, udev, port1, i);
4843 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
4846 clear_bit(port1, parent_hub->busy_bits);
4851 /* Device might have changed firmware (DFU or similar) */
4852 if (descriptors_changed(udev, &descriptor)) {
4853 dev_info(&udev->dev, "device firmware changed\n");
4854 udev->descriptor = descriptor; /* for disconnect() calls */
4858 /* Restore the device's previous configuration */
4859 if (!udev->actconfig)
4862 mutex_lock(hcd->bandwidth_mutex);
4863 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
4865 dev_warn(&udev->dev,
4866 "Busted HC? Not enough HCD resources for "
4867 "old configuration.\n");
4868 mutex_unlock(hcd->bandwidth_mutex);
4871 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4872 USB_REQ_SET_CONFIGURATION, 0,
4873 udev->actconfig->desc.bConfigurationValue, 0,
4874 NULL, 0, USB_CTRL_SET_TIMEOUT);
4877 "can't restore configuration #%d (error=%d)\n",
4878 udev->actconfig->desc.bConfigurationValue, ret);
4879 mutex_unlock(hcd->bandwidth_mutex);
4882 mutex_unlock(hcd->bandwidth_mutex);
4883 usb_set_device_state(udev, USB_STATE_CONFIGURED);
4885 /* Put interfaces back into the same altsettings as before.
4886 * Don't bother to send the Set-Interface request for interfaces
4887 * that were already in altsetting 0; besides being unnecessary,
4888 * many devices can't handle it. Instead just reset the host-side
4891 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
4892 struct usb_host_config *config = udev->actconfig;
4893 struct usb_interface *intf = config->interface[i];
4894 struct usb_interface_descriptor *desc;
4896 desc = &intf->cur_altsetting->desc;
4897 if (desc->bAlternateSetting == 0) {
4898 usb_disable_interface(udev, intf, true);
4899 usb_enable_interface(udev, intf, true);
4902 /* Let the bandwidth allocation function know that this
4903 * device has been reset, and it will have to use
4904 * alternate setting 0 as the current alternate setting.
4906 intf->resetting_device = 1;
4907 ret = usb_set_interface(udev, desc->bInterfaceNumber,
4908 desc->bAlternateSetting);
4909 intf->resetting_device = 0;
4912 dev_err(&udev->dev, "failed to restore interface %d "
4913 "altsetting %d (error=%d)\n",
4914 desc->bInterfaceNumber,
4915 desc->bAlternateSetting,
4922 /* Now that the alt settings are re-installed, enable LTM and LPM. */
4923 usb_unlocked_enable_lpm(udev);
4924 usb_enable_ltm(udev);
4928 /* LPM state doesn't matter when we're about to destroy the device. */
4929 hub_port_logical_disconnect(parent_hub, port1);
4934 * usb_reset_device - warn interface drivers and perform a USB port reset
4935 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4937 * Warns all drivers bound to registered interfaces (using their pre_reset
4938 * method), performs the port reset, and then lets the drivers know that
4939 * the reset is over (using their post_reset method).
4941 * Return value is the same as for usb_reset_and_verify_device().
4943 * The caller must own the device lock. For example, it's safe to use
4944 * this from a driver probe() routine after downloading new firmware.
4945 * For calls that might not occur during probe(), drivers should lock
4946 * the device using usb_lock_device_for_reset().
4948 * If an interface is currently being probed or disconnected, we assume
4949 * its driver knows how to handle resets. For all other interfaces,
4950 * if the driver doesn't have pre_reset and post_reset methods then
4951 * we attempt to unbind it and rebind afterward.
4953 int usb_reset_device(struct usb_device *udev)
4957 struct usb_host_config *config = udev->actconfig;
4959 if (udev->state == USB_STATE_NOTATTACHED ||
4960 udev->state == USB_STATE_SUSPENDED) {
4961 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4966 /* Prevent autosuspend during the reset */
4967 usb_autoresume_device(udev);
4970 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
4971 struct usb_interface *cintf = config->interface[i];
4972 struct usb_driver *drv;
4975 if (cintf->dev.driver) {
4976 drv = to_usb_driver(cintf->dev.driver);
4977 if (drv->pre_reset && drv->post_reset)
4978 unbind = (drv->pre_reset)(cintf);
4979 else if (cintf->condition ==
4980 USB_INTERFACE_BOUND)
4983 usb_forced_unbind_intf(cintf);
4988 ret = usb_reset_and_verify_device(udev);
4991 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
4992 struct usb_interface *cintf = config->interface[i];
4993 struct usb_driver *drv;
4994 int rebind = cintf->needs_binding;
4996 if (!rebind && cintf->dev.driver) {
4997 drv = to_usb_driver(cintf->dev.driver);
4998 if (drv->post_reset)
4999 rebind = (drv->post_reset)(cintf);
5000 else if (cintf->condition ==
5001 USB_INTERFACE_BOUND)
5004 if (ret == 0 && rebind)
5005 usb_rebind_intf(cintf);
5009 usb_autosuspend_device(udev);
5012 EXPORT_SYMBOL_GPL(usb_reset_device);
5016 * usb_queue_reset_device - Reset a USB device from an atomic context
5017 * @iface: USB interface belonging to the device to reset
5019 * This function can be used to reset a USB device from an atomic
5020 * context, where usb_reset_device() won't work (as it blocks).
5022 * Doing a reset via this method is functionally equivalent to calling
5023 * usb_reset_device(), except for the fact that it is delayed to a
5024 * workqueue. This means that any drivers bound to other interfaces
5025 * might be unbound, as well as users from usbfs in user space.
5029 * - Scheduling two resets at the same time from two different drivers
5030 * attached to two different interfaces of the same device is
5031 * possible; depending on how the driver attached to each interface
5032 * handles ->pre_reset(), the second reset might happen or not.
5034 * - If a driver is unbound and it had a pending reset, the reset will
5037 * - This function can be called during .probe() or .disconnect()
5038 * times. On return from .disconnect(), any pending resets will be
5041 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5044 * NOTE: We don't do any reference count tracking because it is not
5045 * needed. The lifecycle of the work_struct is tied to the
5046 * usb_interface. Before destroying the interface we cancel the
5047 * work_struct, so the fact that work_struct is queued and or
5048 * running means the interface (and thus, the device) exist and
5051 void usb_queue_reset_device(struct usb_interface *iface)
5053 schedule_work(&iface->reset_ws);
5055 EXPORT_SYMBOL_GPL(usb_queue_reset_device);
5058 * usb_hub_find_child - Get the pointer of child device
5059 * attached to the port which is specified by @port1.
5060 * @hdev: USB device belonging to the usb hub
5061 * @port1: port num to indicate which port the child device
5064 * USB drivers call this function to get hub's child device
5067 * Return NULL if input param is invalid and
5068 * child's usb_device pointer if non-NULL.
5070 struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5073 struct usb_hub *hub = hdev_to_hub(hdev);
5075 if (port1 < 1 || port1 > hdev->maxchild)
5077 return hub->ports[port1 - 1]->child;
5079 EXPORT_SYMBOL_GPL(usb_hub_find_child);
5083 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5084 * @hdev: USB device belonging to the usb hub
5085 * @port1: port num of the port
5087 * Return port's acpi handle if successful, NULL if params are
5090 acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5093 struct usb_hub *hub = hdev_to_hub(hdev);
5095 return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev);