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
42 #define USB_VENDOR_GENESYS_LOGIC 0x05e3
43 #define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01
45 static inline int hub_is_superspeed(struct usb_device
*hdev
)
47 return (hdev
->descriptor
.bDeviceProtocol
== USB_HUB_PR_SS
);
50 /* Protect struct usb_device->state and ->children members
51 * Note: Both are also protected by ->dev.sem, except that ->state can
52 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
53 static DEFINE_SPINLOCK(device_state_lock
);
55 /* khubd's worklist and its lock */
56 static DEFINE_SPINLOCK(hub_event_lock
);
57 static LIST_HEAD(hub_event_list
); /* List of hubs needing servicing */
60 static DECLARE_WAIT_QUEUE_HEAD(khubd_wait
);
62 static struct task_struct
*khubd_task
;
64 /* cycle leds on hubs that aren't blinking for attention */
65 static bool blinkenlights
= 0;
66 module_param (blinkenlights
, bool, S_IRUGO
);
67 MODULE_PARM_DESC (blinkenlights
, "true to cycle leds on hubs");
70 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
71 * 10 seconds to send reply for the initial 64-byte descriptor request.
73 /* define initial 64-byte descriptor request timeout in milliseconds */
74 static int initial_descriptor_timeout
= USB_CTRL_GET_TIMEOUT
;
75 module_param(initial_descriptor_timeout
, int, S_IRUGO
|S_IWUSR
);
76 MODULE_PARM_DESC(initial_descriptor_timeout
,
77 "initial 64-byte descriptor request timeout in milliseconds "
78 "(default 5000 - 5.0 seconds)");
81 * As of 2.6.10 we introduce a new USB device initialization scheme which
82 * closely resembles the way Windows works. Hopefully it will be compatible
83 * with a wider range of devices than the old scheme. However some previously
84 * working devices may start giving rise to "device not accepting address"
85 * errors; if that happens the user can try the old scheme by adjusting the
86 * following module parameters.
88 * For maximum flexibility there are two boolean parameters to control the
89 * hub driver's behavior. On the first initialization attempt, if the
90 * "old_scheme_first" parameter is set then the old scheme will be used,
91 * otherwise the new scheme is used. If that fails and "use_both_schemes"
92 * is set, then the driver will make another attempt, using the other scheme.
94 static bool old_scheme_first
= 0;
95 module_param(old_scheme_first
, bool, S_IRUGO
| S_IWUSR
);
96 MODULE_PARM_DESC(old_scheme_first
,
97 "start with the old device initialization scheme");
99 static bool use_both_schemes
= 1;
100 module_param(use_both_schemes
, bool, S_IRUGO
| S_IWUSR
);
101 MODULE_PARM_DESC(use_both_schemes
,
102 "try the other device initialization scheme if the "
105 /* Mutual exclusion for EHCI CF initialization. This interferes with
106 * port reset on some companion controllers.
108 DECLARE_RWSEM(ehci_cf_port_reset_rwsem
);
109 EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem
);
111 #define HUB_DEBOUNCE_TIMEOUT 1500
112 #define HUB_DEBOUNCE_STEP 25
113 #define HUB_DEBOUNCE_STABLE 100
115 static int usb_reset_and_verify_device(struct usb_device
*udev
);
117 static inline char *portspeed(struct usb_hub
*hub
, int portstatus
)
119 if (hub_is_superspeed(hub
->hdev
))
121 if (portstatus
& USB_PORT_STAT_HIGH_SPEED
)
123 else if (portstatus
& USB_PORT_STAT_LOW_SPEED
)
129 /* Note that hdev or one of its children must be locked! */
130 static struct usb_hub
*hdev_to_hub(struct usb_device
*hdev
)
132 if (!hdev
|| !hdev
->actconfig
|| !hdev
->maxchild
)
134 return usb_get_intfdata(hdev
->actconfig
->interface
[0]);
137 static int usb_device_supports_lpm(struct usb_device
*udev
)
139 /* USB 2.1 (and greater) devices indicate LPM support through
140 * their USB 2.0 Extended Capabilities BOS descriptor.
142 if (udev
->speed
== USB_SPEED_HIGH
) {
143 if (udev
->bos
->ext_cap
&&
145 le32_to_cpu(udev
->bos
->ext_cap
->bmAttributes
)))
150 /* All USB 3.0 must support LPM, but we need their max exit latency
151 * information from the SuperSpeed Extended Capabilities BOS descriptor.
153 if (!udev
->bos
->ss_cap
) {
154 dev_warn(&udev
->dev
, "No LPM exit latency info found. "
155 "Power management will be impacted.\n");
158 if (udev
->parent
->lpm_capable
)
161 dev_warn(&udev
->dev
, "Parent hub missing LPM exit latency info. "
162 "Power management will be impacted.\n");
167 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
170 static void usb_set_lpm_mel(struct usb_device
*udev
,
171 struct usb3_lpm_parameters
*udev_lpm_params
,
172 unsigned int udev_exit_latency
,
174 struct usb3_lpm_parameters
*hub_lpm_params
,
175 unsigned int hub_exit_latency
)
177 unsigned int total_mel
;
178 unsigned int device_mel
;
179 unsigned int hub_mel
;
182 * Calculate the time it takes to transition all links from the roothub
183 * to the parent hub into U0. The parent hub must then decode the
184 * packet (hub header decode latency) to figure out which port it was
187 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
188 * means 0.1us). Multiply that by 100 to get nanoseconds.
190 total_mel
= hub_lpm_params
->mel
+
191 (hub
->descriptor
->u
.ss
.bHubHdrDecLat
* 100);
194 * How long will it take to transition the downstream hub's port into
195 * U0? The greater of either the hub exit latency or the device exit
198 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
199 * Multiply that by 1000 to get nanoseconds.
201 device_mel
= udev_exit_latency
* 1000;
202 hub_mel
= hub_exit_latency
* 1000;
203 if (device_mel
> hub_mel
)
204 total_mel
+= device_mel
;
206 total_mel
+= hub_mel
;
208 udev_lpm_params
->mel
= total_mel
;
212 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
213 * a transition from either U1 or U2.
215 static void usb_set_lpm_pel(struct usb_device
*udev
,
216 struct usb3_lpm_parameters
*udev_lpm_params
,
217 unsigned int udev_exit_latency
,
219 struct usb3_lpm_parameters
*hub_lpm_params
,
220 unsigned int hub_exit_latency
,
221 unsigned int port_to_port_exit_latency
)
223 unsigned int first_link_pel
;
224 unsigned int hub_pel
;
227 * First, the device sends an LFPS to transition the link between the
228 * device and the parent hub into U0. The exit latency is the bigger of
229 * the device exit latency or the hub exit latency.
231 if (udev_exit_latency
> hub_exit_latency
)
232 first_link_pel
= udev_exit_latency
* 1000;
234 first_link_pel
= hub_exit_latency
* 1000;
237 * When the hub starts to receive the LFPS, there is a slight delay for
238 * it to figure out that one of the ports is sending an LFPS. Then it
239 * will forward the LFPS to its upstream link. The exit latency is the
240 * delay, plus the PEL that we calculated for this hub.
242 hub_pel
= port_to_port_exit_latency
* 1000 + hub_lpm_params
->pel
;
245 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
246 * is the greater of the two exit latencies.
248 if (first_link_pel
> hub_pel
)
249 udev_lpm_params
->pel
= first_link_pel
;
251 udev_lpm_params
->pel
= hub_pel
;
255 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
256 * when a device initiates a transition to U0, until when it will receive the
257 * first packet from the host controller.
259 * Section C.1.5.1 describes the four components to this:
261 * - t2: time for the ERDY to make it from the device to the host.
262 * - t3: a host-specific delay to process the ERDY.
263 * - t4: time for the packet to make it from the host to the device.
265 * t3 is specific to both the xHCI host and the platform the host is integrated
266 * into. The Intel HW folks have said it's negligible, FIXME if a different
267 * vendor says otherwise.
269 static void usb_set_lpm_sel(struct usb_device
*udev
,
270 struct usb3_lpm_parameters
*udev_lpm_params
)
272 struct usb_device
*parent
;
273 unsigned int num_hubs
;
274 unsigned int total_sel
;
276 /* t1 = device PEL */
277 total_sel
= udev_lpm_params
->pel
;
278 /* How many external hubs are in between the device & the root port. */
279 for (parent
= udev
->parent
, num_hubs
= 0; parent
->parent
;
280 parent
= parent
->parent
)
282 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
284 total_sel
+= 2100 + 250 * (num_hubs
- 1);
286 /* t4 = 250ns * num_hubs */
287 total_sel
+= 250 * num_hubs
;
289 udev_lpm_params
->sel
= total_sel
;
292 static void usb_set_lpm_parameters(struct usb_device
*udev
)
295 unsigned int port_to_port_delay
;
296 unsigned int udev_u1_del
;
297 unsigned int udev_u2_del
;
298 unsigned int hub_u1_del
;
299 unsigned int hub_u2_del
;
301 if (!udev
->lpm_capable
|| udev
->speed
!= USB_SPEED_SUPER
)
304 hub
= hdev_to_hub(udev
->parent
);
305 /* It doesn't take time to transition the roothub into U0, since it
306 * doesn't have an upstream link.
311 udev_u1_del
= udev
->bos
->ss_cap
->bU1devExitLat
;
312 udev_u2_del
= udev
->bos
->ss_cap
->bU2DevExitLat
;
313 hub_u1_del
= udev
->parent
->bos
->ss_cap
->bU1devExitLat
;
314 hub_u2_del
= udev
->parent
->bos
->ss_cap
->bU2DevExitLat
;
316 usb_set_lpm_mel(udev
, &udev
->u1_params
, udev_u1_del
,
317 hub
, &udev
->parent
->u1_params
, hub_u1_del
);
319 usb_set_lpm_mel(udev
, &udev
->u2_params
, udev_u2_del
,
320 hub
, &udev
->parent
->u2_params
, hub_u2_del
);
323 * Appendix C, section C.2.2.2, says that there is a slight delay from
324 * when the parent hub notices the downstream port is trying to
325 * transition to U0 to when the hub initiates a U0 transition on its
326 * upstream port. The section says the delays are tPort2PortU1EL and
327 * tPort2PortU2EL, but it doesn't define what they are.
329 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
330 * about the same delays. Use the maximum delay calculations from those
331 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
332 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
333 * assume the device exit latencies they are talking about are the hub
336 * What do we do if the U2 exit latency is less than the U1 exit
337 * latency? It's possible, although not likely...
339 port_to_port_delay
= 1;
341 usb_set_lpm_pel(udev
, &udev
->u1_params
, udev_u1_del
,
342 hub
, &udev
->parent
->u1_params
, hub_u1_del
,
345 if (hub_u2_del
> hub_u1_del
)
346 port_to_port_delay
= 1 + hub_u2_del
- hub_u1_del
;
348 port_to_port_delay
= 1 + hub_u1_del
;
350 usb_set_lpm_pel(udev
, &udev
->u2_params
, udev_u2_del
,
351 hub
, &udev
->parent
->u2_params
, hub_u2_del
,
354 /* Now that we've got PEL, calculate SEL. */
355 usb_set_lpm_sel(udev
, &udev
->u1_params
);
356 usb_set_lpm_sel(udev
, &udev
->u2_params
);
359 /* USB 2.0 spec Section 11.24.4.5 */
360 static int get_hub_descriptor(struct usb_device
*hdev
, void *data
)
365 if (hub_is_superspeed(hdev
)) {
366 dtype
= USB_DT_SS_HUB
;
367 size
= USB_DT_SS_HUB_SIZE
;
370 size
= sizeof(struct usb_hub_descriptor
);
373 for (i
= 0; i
< 3; i
++) {
374 ret
= usb_control_msg(hdev
, usb_rcvctrlpipe(hdev
, 0),
375 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
| USB_RT_HUB
,
376 dtype
<< 8, 0, data
, size
,
377 USB_CTRL_GET_TIMEOUT
);
378 if (ret
>= (USB_DT_HUB_NONVAR_SIZE
+ 2))
385 * USB 2.0 spec Section 11.24.2.1
387 static int clear_hub_feature(struct usb_device
*hdev
, int feature
)
389 return usb_control_msg(hdev
, usb_sndctrlpipe(hdev
, 0),
390 USB_REQ_CLEAR_FEATURE
, USB_RT_HUB
, feature
, 0, NULL
, 0, 1000);
394 * USB 2.0 spec Section 11.24.2.2
396 static int clear_port_feature(struct usb_device
*hdev
, int port1
, int feature
)
398 return usb_control_msg(hdev
, usb_sndctrlpipe(hdev
, 0),
399 USB_REQ_CLEAR_FEATURE
, USB_RT_PORT
, feature
, port1
,
404 * USB 2.0 spec Section 11.24.2.13
406 static int set_port_feature(struct usb_device
*hdev
, int port1
, int feature
)
408 return usb_control_msg(hdev
, usb_sndctrlpipe(hdev
, 0),
409 USB_REQ_SET_FEATURE
, USB_RT_PORT
, feature
, port1
,
414 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
415 * for info about using port indicators
417 static void set_port_led(
423 int status
= set_port_feature(hub
->hdev
, (selector
<< 8) | port1
,
424 USB_PORT_FEAT_INDICATOR
);
426 dev_dbg (hub
->intfdev
,
427 "port %d indicator %s status %d\n",
429 ({ char *s
; switch (selector
) {
430 case HUB_LED_AMBER
: s
= "amber"; break;
431 case HUB_LED_GREEN
: s
= "green"; break;
432 case HUB_LED_OFF
: s
= "off"; break;
433 case HUB_LED_AUTO
: s
= "auto"; break;
434 default: s
= "??"; break;
439 #define LED_CYCLE_PERIOD ((2*HZ)/3)
441 static void led_work (struct work_struct
*work
)
443 struct usb_hub
*hub
=
444 container_of(work
, struct usb_hub
, leds
.work
);
445 struct usb_device
*hdev
= hub
->hdev
;
447 unsigned changed
= 0;
450 if (hdev
->state
!= USB_STATE_CONFIGURED
|| hub
->quiescing
)
453 for (i
= 0; i
< hub
->descriptor
->bNbrPorts
; i
++) {
454 unsigned selector
, mode
;
456 /* 30%-50% duty cycle */
458 switch (hub
->indicator
[i
]) {
460 case INDICATOR_CYCLE
:
462 selector
= HUB_LED_AUTO
;
463 mode
= INDICATOR_AUTO
;
465 /* blinking green = sw attention */
466 case INDICATOR_GREEN_BLINK
:
467 selector
= HUB_LED_GREEN
;
468 mode
= INDICATOR_GREEN_BLINK_OFF
;
470 case INDICATOR_GREEN_BLINK_OFF
:
471 selector
= HUB_LED_OFF
;
472 mode
= INDICATOR_GREEN_BLINK
;
474 /* blinking amber = hw attention */
475 case INDICATOR_AMBER_BLINK
:
476 selector
= HUB_LED_AMBER
;
477 mode
= INDICATOR_AMBER_BLINK_OFF
;
479 case INDICATOR_AMBER_BLINK_OFF
:
480 selector
= HUB_LED_OFF
;
481 mode
= INDICATOR_AMBER_BLINK
;
483 /* blink green/amber = reserved */
484 case INDICATOR_ALT_BLINK
:
485 selector
= HUB_LED_GREEN
;
486 mode
= INDICATOR_ALT_BLINK_OFF
;
488 case INDICATOR_ALT_BLINK_OFF
:
489 selector
= HUB_LED_AMBER
;
490 mode
= INDICATOR_ALT_BLINK
;
495 if (selector
!= HUB_LED_AUTO
)
497 set_port_led(hub
, i
+ 1, selector
);
498 hub
->indicator
[i
] = mode
;
500 if (!changed
&& blinkenlights
) {
502 cursor
%= hub
->descriptor
->bNbrPorts
;
503 set_port_led(hub
, cursor
+ 1, HUB_LED_GREEN
);
504 hub
->indicator
[cursor
] = INDICATOR_CYCLE
;
508 schedule_delayed_work(&hub
->leds
, LED_CYCLE_PERIOD
);
511 /* use a short timeout for hub/port status fetches */
512 #define USB_STS_TIMEOUT 1000
513 #define USB_STS_RETRIES 5
516 * USB 2.0 spec Section 11.24.2.6
518 static int get_hub_status(struct usb_device
*hdev
,
519 struct usb_hub_status
*data
)
521 int i
, status
= -ETIMEDOUT
;
523 for (i
= 0; i
< USB_STS_RETRIES
&&
524 (status
== -ETIMEDOUT
|| status
== -EPIPE
); i
++) {
525 status
= usb_control_msg(hdev
, usb_rcvctrlpipe(hdev
, 0),
526 USB_REQ_GET_STATUS
, USB_DIR_IN
| USB_RT_HUB
, 0, 0,
527 data
, sizeof(*data
), USB_STS_TIMEOUT
);
533 * USB 2.0 spec Section 11.24.2.7
535 static int get_port_status(struct usb_device
*hdev
, int port1
,
536 struct usb_port_status
*data
)
538 int i
, status
= -ETIMEDOUT
;
540 for (i
= 0; i
< USB_STS_RETRIES
&&
541 (status
== -ETIMEDOUT
|| status
== -EPIPE
); i
++) {
542 status
= usb_control_msg(hdev
, usb_rcvctrlpipe(hdev
, 0),
543 USB_REQ_GET_STATUS
, USB_DIR_IN
| USB_RT_PORT
, 0, port1
,
544 data
, sizeof(*data
), USB_STS_TIMEOUT
);
549 static int hub_port_status(struct usb_hub
*hub
, int port1
,
550 u16
*status
, u16
*change
)
554 mutex_lock(&hub
->status_mutex
);
555 ret
= get_port_status(hub
->hdev
, port1
, &hub
->status
->port
);
557 dev_err(hub
->intfdev
,
558 "%s failed (err = %d)\n", __func__
, ret
);
562 *status
= le16_to_cpu(hub
->status
->port
.wPortStatus
);
563 *change
= le16_to_cpu(hub
->status
->port
.wPortChange
);
567 mutex_unlock(&hub
->status_mutex
);
571 static void kick_khubd(struct usb_hub
*hub
)
575 spin_lock_irqsave(&hub_event_lock
, flags
);
576 if (!hub
->disconnected
&& list_empty(&hub
->event_list
)) {
577 list_add_tail(&hub
->event_list
, &hub_event_list
);
579 /* Suppress autosuspend until khubd runs */
580 usb_autopm_get_interface_no_resume(
581 to_usb_interface(hub
->intfdev
));
582 wake_up(&khubd_wait
);
584 spin_unlock_irqrestore(&hub_event_lock
, flags
);
587 void usb_kick_khubd(struct usb_device
*hdev
)
589 struct usb_hub
*hub
= hdev_to_hub(hdev
);
596 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
597 * Notification, which indicates it had initiated remote wakeup.
599 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
600 * device initiates resume, so the USB core will not receive notice of the
601 * resume through the normal hub interrupt URB.
603 void usb_wakeup_notification(struct usb_device
*hdev
,
604 unsigned int portnum
)
611 hub
= hdev_to_hub(hdev
);
613 set_bit(portnum
, hub
->wakeup_bits
);
617 EXPORT_SYMBOL_GPL(usb_wakeup_notification
);
619 /* completion function, fires on port status changes and various faults */
620 static void hub_irq(struct urb
*urb
)
622 struct usb_hub
*hub
= urb
->context
;
623 int status
= urb
->status
;
628 case -ENOENT
: /* synchronous unlink */
629 case -ECONNRESET
: /* async unlink */
630 case -ESHUTDOWN
: /* hardware going away */
633 default: /* presumably an error */
634 /* Cause a hub reset after 10 consecutive errors */
635 dev_dbg (hub
->intfdev
, "transfer --> %d\n", status
);
636 if ((++hub
->nerrors
< 10) || hub
->error
)
641 /* let khubd handle things */
642 case 0: /* we got data: port status changed */
644 for (i
= 0; i
< urb
->actual_length
; ++i
)
645 bits
|= ((unsigned long) ((*hub
->buffer
)[i
]))
647 hub
->event_bits
[0] = bits
;
653 /* Something happened, let khubd figure it out */
660 if ((status
= usb_submit_urb (hub
->urb
, GFP_ATOMIC
)) != 0
661 && status
!= -ENODEV
&& status
!= -EPERM
)
662 dev_err (hub
->intfdev
, "resubmit --> %d\n", status
);
665 /* USB 2.0 spec Section 11.24.2.3 */
667 hub_clear_tt_buffer (struct usb_device
*hdev
, u16 devinfo
, u16 tt
)
669 return usb_control_msg(hdev
, usb_sndctrlpipe(hdev
, 0),
670 HUB_CLEAR_TT_BUFFER
, USB_RT_PORT
, devinfo
,
675 * enumeration blocks khubd for a long time. we use keventd instead, since
676 * long blocking there is the exception, not the rule. accordingly, HCDs
677 * talking to TTs must queue control transfers (not just bulk and iso), so
678 * both can talk to the same hub concurrently.
680 static void hub_tt_work(struct work_struct
*work
)
682 struct usb_hub
*hub
=
683 container_of(work
, struct usb_hub
, tt
.clear_work
);
686 spin_lock_irqsave (&hub
->tt
.lock
, flags
);
687 while (!list_empty(&hub
->tt
.clear_list
)) {
688 struct list_head
*next
;
689 struct usb_tt_clear
*clear
;
690 struct usb_device
*hdev
= hub
->hdev
;
691 const struct hc_driver
*drv
;
694 next
= hub
->tt
.clear_list
.next
;
695 clear
= list_entry (next
, struct usb_tt_clear
, clear_list
);
696 list_del (&clear
->clear_list
);
698 /* drop lock so HCD can concurrently report other TT errors */
699 spin_unlock_irqrestore (&hub
->tt
.lock
, flags
);
700 status
= hub_clear_tt_buffer (hdev
, clear
->devinfo
, clear
->tt
);
703 "clear tt %d (%04x) error %d\n",
704 clear
->tt
, clear
->devinfo
, status
);
706 /* Tell the HCD, even if the operation failed */
707 drv
= clear
->hcd
->driver
;
708 if (drv
->clear_tt_buffer_complete
)
709 (drv
->clear_tt_buffer_complete
)(clear
->hcd
, clear
->ep
);
712 spin_lock_irqsave(&hub
->tt
.lock
, flags
);
714 spin_unlock_irqrestore (&hub
->tt
.lock
, flags
);
718 * usb_hub_set_port_power - control hub port's power state
721 * @set: expected status
723 * call this function to control port's power via setting or
724 * clearing the port's PORT_POWER feature.
726 int usb_hub_set_port_power(struct usb_device
*hdev
, int port1
,
732 ret
= set_port_feature(hdev
, port1
, USB_PORT_FEAT_POWER
);
734 ret
= clear_port_feature(hdev
, port1
, USB_PORT_FEAT_POWER
);
739 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
740 * @urb: an URB associated with the failed or incomplete split transaction
742 * High speed HCDs use this to tell the hub driver that some split control or
743 * bulk transaction failed in a way that requires clearing internal state of
744 * a transaction translator. This is normally detected (and reported) from
747 * It may not be possible for that hub to handle additional full (or low)
748 * speed transactions until that state is fully cleared out.
750 int usb_hub_clear_tt_buffer(struct urb
*urb
)
752 struct usb_device
*udev
= urb
->dev
;
753 int pipe
= urb
->pipe
;
754 struct usb_tt
*tt
= udev
->tt
;
756 struct usb_tt_clear
*clear
;
758 /* we've got to cope with an arbitrary number of pending TT clears,
759 * since each TT has "at least two" buffers that can need it (and
760 * there can be many TTs per hub). even if they're uncommon.
762 if ((clear
= kmalloc (sizeof *clear
, GFP_ATOMIC
)) == NULL
) {
763 dev_err (&udev
->dev
, "can't save CLEAR_TT_BUFFER state\n");
764 /* FIXME recover somehow ... RESET_TT? */
768 /* info that CLEAR_TT_BUFFER needs */
769 clear
->tt
= tt
->multi
? udev
->ttport
: 1;
770 clear
->devinfo
= usb_pipeendpoint (pipe
);
771 clear
->devinfo
|= udev
->devnum
<< 4;
772 clear
->devinfo
|= usb_pipecontrol (pipe
)
773 ? (USB_ENDPOINT_XFER_CONTROL
<< 11)
774 : (USB_ENDPOINT_XFER_BULK
<< 11);
775 if (usb_pipein (pipe
))
776 clear
->devinfo
|= 1 << 15;
778 /* info for completion callback */
779 clear
->hcd
= bus_to_hcd(udev
->bus
);
782 /* tell keventd to clear state for this TT */
783 spin_lock_irqsave (&tt
->lock
, flags
);
784 list_add_tail (&clear
->clear_list
, &tt
->clear_list
);
785 schedule_work(&tt
->clear_work
);
786 spin_unlock_irqrestore (&tt
->lock
, flags
);
789 EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer
);
791 /* If do_delay is false, return the number of milliseconds the caller
794 static unsigned hub_power_on(struct usb_hub
*hub
, bool do_delay
)
797 unsigned pgood_delay
= hub
->descriptor
->bPwrOn2PwrGood
* 2;
799 u16 wHubCharacteristics
=
800 le16_to_cpu(hub
->descriptor
->wHubCharacteristics
);
802 /* Enable power on each port. Some hubs have reserved values
803 * of LPSM (> 2) in their descriptors, even though they are
804 * USB 2.0 hubs. Some hubs do not implement port-power switching
805 * but only emulate it. In all cases, the ports won't work
806 * unless we send these messages to the hub.
808 if ((wHubCharacteristics
& HUB_CHAR_LPSM
) < 2)
809 dev_dbg(hub
->intfdev
, "enabling power on all ports\n");
811 dev_dbg(hub
->intfdev
, "trying to enable port power on "
812 "non-switchable hub\n");
813 for (port1
= 1; port1
<= hub
->descriptor
->bNbrPorts
; port1
++)
814 set_port_feature(hub
->hdev
, port1
, USB_PORT_FEAT_POWER
);
816 /* Wait at least 100 msec for power to become stable */
817 delay
= max(pgood_delay
, (unsigned) 100);
823 static int hub_hub_status(struct usb_hub
*hub
,
824 u16
*status
, u16
*change
)
828 mutex_lock(&hub
->status_mutex
);
829 ret
= get_hub_status(hub
->hdev
, &hub
->status
->hub
);
831 dev_err (hub
->intfdev
,
832 "%s failed (err = %d)\n", __func__
, ret
);
834 *status
= le16_to_cpu(hub
->status
->hub
.wHubStatus
);
835 *change
= le16_to_cpu(hub
->status
->hub
.wHubChange
);
838 mutex_unlock(&hub
->status_mutex
);
842 static int hub_set_port_link_state(struct usb_hub
*hub
, int port1
,
843 unsigned int link_status
)
845 return set_port_feature(hub
->hdev
,
846 port1
| (link_status
<< 3),
847 USB_PORT_FEAT_LINK_STATE
);
851 * If USB 3.0 ports are placed into the Disabled state, they will no longer
852 * detect any device connects or disconnects. This is generally not what the
853 * USB core wants, since it expects a disabled port to produce a port status
854 * change event when a new device connects.
856 * Instead, set the link state to Disabled, wait for the link to settle into
857 * that state, clear any change bits, and then put the port into the RxDetect
860 static int hub_usb3_port_disable(struct usb_hub
*hub
, int port1
)
864 u16 portchange
, portstatus
;
866 if (!hub_is_superspeed(hub
->hdev
))
869 ret
= hub_set_port_link_state(hub
, port1
, USB_SS_PORT_LS_SS_DISABLED
);
871 dev_err(hub
->intfdev
, "cannot disable port %d (err = %d)\n",
876 /* Wait for the link to enter the disabled state. */
877 for (total_time
= 0; ; total_time
+= HUB_DEBOUNCE_STEP
) {
878 ret
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
882 if ((portstatus
& USB_PORT_STAT_LINK_STATE
) ==
883 USB_SS_PORT_LS_SS_DISABLED
)
885 if (total_time
>= HUB_DEBOUNCE_TIMEOUT
)
887 msleep(HUB_DEBOUNCE_STEP
);
889 if (total_time
>= HUB_DEBOUNCE_TIMEOUT
)
890 dev_warn(hub
->intfdev
, "Could not disable port %d after %d ms\n",
893 return hub_set_port_link_state(hub
, port1
, USB_SS_PORT_LS_RX_DETECT
);
896 static int hub_port_disable(struct usb_hub
*hub
, int port1
, int set_state
)
898 struct usb_device
*hdev
= hub
->hdev
;
901 if (hub
->ports
[port1
- 1]->child
&& set_state
)
902 usb_set_device_state(hub
->ports
[port1
- 1]->child
,
903 USB_STATE_NOTATTACHED
);
905 if (hub_is_superspeed(hub
->hdev
))
906 ret
= hub_usb3_port_disable(hub
, port1
);
908 ret
= clear_port_feature(hdev
, port1
,
909 USB_PORT_FEAT_ENABLE
);
912 dev_err(hub
->intfdev
, "cannot disable port %d (err = %d)\n",
918 * Disable a port and mark a logical connect-change event, so that some
919 * time later khubd will disconnect() any existing usb_device on the port
920 * and will re-enumerate if there actually is a device attached.
922 static void hub_port_logical_disconnect(struct usb_hub
*hub
, int port1
)
924 dev_dbg(hub
->intfdev
, "logical disconnect on port %d\n", port1
);
925 hub_port_disable(hub
, port1
, 1);
927 /* FIXME let caller ask to power down the port:
928 * - some devices won't enumerate without a VBUS power cycle
929 * - SRP saves power that way
930 * - ... new call, TBD ...
931 * That's easy if this hub can switch power per-port, and
932 * khubd reactivates the port later (timer, SRP, etc).
933 * Powerdown must be optional, because of reset/DFU.
936 set_bit(port1
, hub
->change_bits
);
941 * usb_remove_device - disable a device's port on its parent hub
942 * @udev: device to be disabled and removed
943 * Context: @udev locked, must be able to sleep.
945 * After @udev's port has been disabled, khubd is notified and it will
946 * see that the device has been disconnected. When the device is
947 * physically unplugged and something is plugged in, the events will
948 * be received and processed normally.
950 int usb_remove_device(struct usb_device
*udev
)
953 struct usb_interface
*intf
;
955 if (!udev
->parent
) /* Can't remove a root hub */
957 hub
= hdev_to_hub(udev
->parent
);
958 intf
= to_usb_interface(hub
->intfdev
);
960 usb_autopm_get_interface(intf
);
961 set_bit(udev
->portnum
, hub
->removed_bits
);
962 hub_port_logical_disconnect(hub
, udev
->portnum
);
963 usb_autopm_put_interface(intf
);
967 enum hub_activation_type
{
968 HUB_INIT
, HUB_INIT2
, HUB_INIT3
, /* INITs must come first */
969 HUB_POST_RESET
, HUB_RESUME
, HUB_RESET_RESUME
,
972 static void hub_init_func2(struct work_struct
*ws
);
973 static void hub_init_func3(struct work_struct
*ws
);
975 static void hub_activate(struct usb_hub
*hub
, enum hub_activation_type type
)
977 struct usb_device
*hdev
= hub
->hdev
;
982 bool need_debounce_delay
= false;
985 /* Continue a partial initialization */
986 if (type
== HUB_INIT2
)
988 if (type
== HUB_INIT3
)
991 /* The superspeed hub except for root hub has to use Hub Depth
992 * value as an offset into the route string to locate the bits
993 * it uses to determine the downstream port number. So hub driver
994 * should send a set hub depth request to superspeed hub after
995 * the superspeed hub is set configuration in initialization or
998 * After a resume, port power should still be on.
999 * For any other type of activation, turn it on.
1001 if (type
!= HUB_RESUME
) {
1002 if (hdev
->parent
&& hub_is_superspeed(hdev
)) {
1003 ret
= usb_control_msg(hdev
, usb_sndctrlpipe(hdev
, 0),
1004 HUB_SET_DEPTH
, USB_RT_HUB
,
1005 hdev
->level
- 1, 0, NULL
, 0,
1006 USB_CTRL_SET_TIMEOUT
);
1008 dev_err(hub
->intfdev
,
1009 "set hub depth failed\n");
1012 /* Speed up system boot by using a delayed_work for the
1013 * hub's initial power-up delays. This is pretty awkward
1014 * and the implementation looks like a home-brewed sort of
1015 * setjmp/longjmp, but it saves at least 100 ms for each
1016 * root hub (assuming usbcore is compiled into the kernel
1017 * rather than as a module). It adds up.
1019 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
1020 * because for those activation types the ports have to be
1021 * operational when we return. In theory this could be done
1022 * for HUB_POST_RESET, but it's easier not to.
1024 if (type
== HUB_INIT
) {
1025 delay
= hub_power_on(hub
, false);
1026 PREPARE_DELAYED_WORK(&hub
->init_work
, hub_init_func2
);
1027 schedule_delayed_work(&hub
->init_work
,
1028 msecs_to_jiffies(delay
));
1030 /* Suppress autosuspend until init is done */
1031 usb_autopm_get_interface_no_resume(
1032 to_usb_interface(hub
->intfdev
));
1033 return; /* Continues at init2: below */
1034 } else if (type
== HUB_RESET_RESUME
) {
1035 /* The internal host controller state for the hub device
1036 * may be gone after a host power loss on system resume.
1037 * Update the device's info so the HW knows it's a hub.
1039 hcd
= bus_to_hcd(hdev
->bus
);
1040 if (hcd
->driver
->update_hub_device
) {
1041 ret
= hcd
->driver
->update_hub_device(hcd
, hdev
,
1042 &hub
->tt
, GFP_NOIO
);
1044 dev_err(hub
->intfdev
, "Host not "
1045 "accepting hub info "
1047 dev_err(hub
->intfdev
, "LS/FS devices "
1048 "and hubs may not work "
1049 "under this hub\n.");
1052 hub_power_on(hub
, true);
1054 hub_power_on(hub
, true);
1059 /* Check each port and set hub->change_bits to let khubd know
1060 * which ports need attention.
1062 for (port1
= 1; port1
<= hdev
->maxchild
; ++port1
) {
1063 struct usb_device
*udev
= hub
->ports
[port1
- 1]->child
;
1064 u16 portstatus
, portchange
;
1066 portstatus
= portchange
= 0;
1067 status
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
1068 if (udev
|| (portstatus
& USB_PORT_STAT_CONNECTION
))
1069 dev_dbg(hub
->intfdev
,
1070 "port %d: status %04x change %04x\n",
1071 port1
, portstatus
, portchange
);
1073 /* After anything other than HUB_RESUME (i.e., initialization
1074 * or any sort of reset), every port should be disabled.
1075 * Unconnected ports should likewise be disabled (paranoia),
1076 * and so should ports for which we have no usb_device.
1078 if ((portstatus
& USB_PORT_STAT_ENABLE
) && (
1079 type
!= HUB_RESUME
||
1080 !(portstatus
& USB_PORT_STAT_CONNECTION
) ||
1082 udev
->state
== USB_STATE_NOTATTACHED
)) {
1084 * USB3 protocol ports will automatically transition
1085 * to Enabled state when detect an USB3.0 device attach.
1086 * Do not disable USB3 protocol ports.
1088 if (!hub_is_superspeed(hdev
)) {
1089 clear_port_feature(hdev
, port1
,
1090 USB_PORT_FEAT_ENABLE
);
1091 portstatus
&= ~USB_PORT_STAT_ENABLE
;
1093 /* Pretend that power was lost for USB3 devs */
1094 portstatus
&= ~USB_PORT_STAT_ENABLE
;
1098 /* Clear status-change flags; we'll debounce later */
1099 if (portchange
& USB_PORT_STAT_C_CONNECTION
) {
1100 need_debounce_delay
= true;
1101 clear_port_feature(hub
->hdev
, port1
,
1102 USB_PORT_FEAT_C_CONNECTION
);
1104 if (portchange
& USB_PORT_STAT_C_ENABLE
) {
1105 need_debounce_delay
= true;
1106 clear_port_feature(hub
->hdev
, port1
,
1107 USB_PORT_FEAT_C_ENABLE
);
1109 if ((portchange
& USB_PORT_STAT_C_BH_RESET
) &&
1110 hub_is_superspeed(hub
->hdev
)) {
1111 need_debounce_delay
= true;
1112 clear_port_feature(hub
->hdev
, port1
,
1113 USB_PORT_FEAT_C_BH_PORT_RESET
);
1115 /* We can forget about a "removed" device when there's a
1116 * physical disconnect or the connect status changes.
1118 if (!(portstatus
& USB_PORT_STAT_CONNECTION
) ||
1119 (portchange
& USB_PORT_STAT_C_CONNECTION
))
1120 clear_bit(port1
, hub
->removed_bits
);
1122 if (!udev
|| udev
->state
== USB_STATE_NOTATTACHED
) {
1123 /* Tell khubd to disconnect the device or
1124 * check for a new connection
1126 if (udev
|| (portstatus
& USB_PORT_STAT_CONNECTION
))
1127 set_bit(port1
, hub
->change_bits
);
1129 } else if (portstatus
& USB_PORT_STAT_ENABLE
) {
1130 bool port_resumed
= (portstatus
&
1131 USB_PORT_STAT_LINK_STATE
) ==
1133 /* The power session apparently survived the resume.
1134 * If there was an overcurrent or suspend change
1135 * (i.e., remote wakeup request), have khubd
1136 * take care of it. Look at the port link state
1137 * for USB 3.0 hubs, since they don't have a suspend
1138 * change bit, and they don't set the port link change
1139 * bit on device-initiated resume.
1141 if (portchange
|| (hub_is_superspeed(hub
->hdev
) &&
1143 set_bit(port1
, hub
->change_bits
);
1145 } else if (udev
->persist_enabled
) {
1147 udev
->reset_resume
= 1;
1149 set_bit(port1
, hub
->change_bits
);
1152 /* The power session is gone; tell khubd */
1153 usb_set_device_state(udev
, USB_STATE_NOTATTACHED
);
1154 set_bit(port1
, hub
->change_bits
);
1158 /* If no port-status-change flags were set, we don't need any
1159 * debouncing. If flags were set we can try to debounce the
1160 * ports all at once right now, instead of letting khubd do them
1161 * one at a time later on.
1163 * If any port-status changes do occur during this delay, khubd
1164 * will see them later and handle them normally.
1166 if (need_debounce_delay
) {
1167 delay
= HUB_DEBOUNCE_STABLE
;
1169 /* Don't do a long sleep inside a workqueue routine */
1170 if (type
== HUB_INIT2
) {
1171 PREPARE_DELAYED_WORK(&hub
->init_work
, hub_init_func3
);
1172 schedule_delayed_work(&hub
->init_work
,
1173 msecs_to_jiffies(delay
));
1174 return; /* Continues at init3: below */
1182 status
= usb_submit_urb(hub
->urb
, GFP_NOIO
);
1184 dev_err(hub
->intfdev
, "activate --> %d\n", status
);
1185 if (hub
->has_indicators
&& blinkenlights
)
1186 schedule_delayed_work(&hub
->leds
, LED_CYCLE_PERIOD
);
1188 /* Scan all ports that need attention */
1191 /* Allow autosuspend if it was suppressed */
1192 if (type
<= HUB_INIT3
)
1193 usb_autopm_put_interface_async(to_usb_interface(hub
->intfdev
));
1196 /* Implement the continuations for the delays above */
1197 static void hub_init_func2(struct work_struct
*ws
)
1199 struct usb_hub
*hub
= container_of(ws
, struct usb_hub
, init_work
.work
);
1201 hub_activate(hub
, HUB_INIT2
);
1204 static void hub_init_func3(struct work_struct
*ws
)
1206 struct usb_hub
*hub
= container_of(ws
, struct usb_hub
, init_work
.work
);
1208 hub_activate(hub
, HUB_INIT3
);
1211 enum hub_quiescing_type
{
1212 HUB_DISCONNECT
, HUB_PRE_RESET
, HUB_SUSPEND
1215 static void hub_quiesce(struct usb_hub
*hub
, enum hub_quiescing_type type
)
1217 struct usb_device
*hdev
= hub
->hdev
;
1220 cancel_delayed_work_sync(&hub
->init_work
);
1222 /* khubd and related activity won't re-trigger */
1225 if (type
!= HUB_SUSPEND
) {
1226 /* Disconnect all the children */
1227 for (i
= 0; i
< hdev
->maxchild
; ++i
) {
1228 if (hub
->ports
[i
]->child
)
1229 usb_disconnect(&hub
->ports
[i
]->child
);
1233 /* Stop khubd and related activity */
1234 usb_kill_urb(hub
->urb
);
1235 if (hub
->has_indicators
)
1236 cancel_delayed_work_sync(&hub
->leds
);
1238 flush_work(&hub
->tt
.clear_work
);
1241 /* caller has locked the hub device */
1242 static int hub_pre_reset(struct usb_interface
*intf
)
1244 struct usb_hub
*hub
= usb_get_intfdata(intf
);
1246 hub_quiesce(hub
, HUB_PRE_RESET
);
1250 /* caller has locked the hub device */
1251 static int hub_post_reset(struct usb_interface
*intf
)
1253 struct usb_hub
*hub
= usb_get_intfdata(intf
);
1255 hub_activate(hub
, HUB_POST_RESET
);
1259 static int hub_configure(struct usb_hub
*hub
,
1260 struct usb_endpoint_descriptor
*endpoint
)
1262 struct usb_hcd
*hcd
;
1263 struct usb_device
*hdev
= hub
->hdev
;
1264 struct device
*hub_dev
= hub
->intfdev
;
1265 u16 hubstatus
, hubchange
;
1266 u16 wHubCharacteristics
;
1269 char *message
= "out of memory";
1273 hub
->buffer
= kmalloc(sizeof(*hub
->buffer
), GFP_KERNEL
);
1279 hub
->status
= kmalloc(sizeof(*hub
->status
), GFP_KERNEL
);
1284 mutex_init(&hub
->status_mutex
);
1286 hub
->descriptor
= kmalloc(sizeof(*hub
->descriptor
), GFP_KERNEL
);
1287 if (!hub
->descriptor
) {
1292 /* Request the entire hub descriptor.
1293 * hub->descriptor can handle USB_MAXCHILDREN ports,
1294 * but the hub can/will return fewer bytes here.
1296 ret
= get_hub_descriptor(hdev
, hub
->descriptor
);
1298 message
= "can't read hub descriptor";
1300 } else if (hub
->descriptor
->bNbrPorts
> USB_MAXCHILDREN
) {
1301 message
= "hub has too many ports!";
1306 hdev
->maxchild
= hub
->descriptor
->bNbrPorts
;
1307 dev_info (hub_dev
, "%d port%s detected\n", hdev
->maxchild
,
1308 (hdev
->maxchild
== 1) ? "" : "s");
1310 hub
->ports
= kzalloc(hdev
->maxchild
* sizeof(struct usb_port
*),
1317 wHubCharacteristics
= le16_to_cpu(hub
->descriptor
->wHubCharacteristics
);
1318 if (hub_is_superspeed(hdev
)) {
1326 /* FIXME for USB 3.0, skip for now */
1327 if ((wHubCharacteristics
& HUB_CHAR_COMPOUND
) &&
1328 !(hub_is_superspeed(hdev
))) {
1330 char portstr
[USB_MAXCHILDREN
+ 1];
1332 for (i
= 0; i
< hdev
->maxchild
; i
++)
1333 portstr
[i
] = hub
->descriptor
->u
.hs
.DeviceRemovable
1334 [((i
+ 1) / 8)] & (1 << ((i
+ 1) % 8))
1336 portstr
[hdev
->maxchild
] = 0;
1337 dev_dbg(hub_dev
, "compound device; port removable status: %s\n", portstr
);
1339 dev_dbg(hub_dev
, "standalone hub\n");
1341 switch (wHubCharacteristics
& HUB_CHAR_LPSM
) {
1342 case HUB_CHAR_COMMON_LPSM
:
1343 dev_dbg(hub_dev
, "ganged power switching\n");
1345 case HUB_CHAR_INDV_PORT_LPSM
:
1346 dev_dbg(hub_dev
, "individual port power switching\n");
1348 case HUB_CHAR_NO_LPSM
:
1350 dev_dbg(hub_dev
, "no power switching (usb 1.0)\n");
1354 switch (wHubCharacteristics
& HUB_CHAR_OCPM
) {
1355 case HUB_CHAR_COMMON_OCPM
:
1356 dev_dbg(hub_dev
, "global over-current protection\n");
1358 case HUB_CHAR_INDV_PORT_OCPM
:
1359 dev_dbg(hub_dev
, "individual port over-current protection\n");
1361 case HUB_CHAR_NO_OCPM
:
1363 dev_dbg(hub_dev
, "no over-current protection\n");
1367 spin_lock_init (&hub
->tt
.lock
);
1368 INIT_LIST_HEAD (&hub
->tt
.clear_list
);
1369 INIT_WORK(&hub
->tt
.clear_work
, hub_tt_work
);
1370 switch (hdev
->descriptor
.bDeviceProtocol
) {
1373 case USB_HUB_PR_HS_SINGLE_TT
:
1374 dev_dbg(hub_dev
, "Single TT\n");
1377 case USB_HUB_PR_HS_MULTI_TT
:
1378 ret
= usb_set_interface(hdev
, 0, 1);
1380 dev_dbg(hub_dev
, "TT per port\n");
1383 dev_err(hub_dev
, "Using single TT (err %d)\n",
1388 /* USB 3.0 hubs don't have a TT */
1391 dev_dbg(hub_dev
, "Unrecognized hub protocol %d\n",
1392 hdev
->descriptor
.bDeviceProtocol
);
1396 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
1397 switch (wHubCharacteristics
& HUB_CHAR_TTTT
) {
1398 case HUB_TTTT_8_BITS
:
1399 if (hdev
->descriptor
.bDeviceProtocol
!= 0) {
1400 hub
->tt
.think_time
= 666;
1401 dev_dbg(hub_dev
, "TT requires at most %d "
1402 "FS bit times (%d ns)\n",
1403 8, hub
->tt
.think_time
);
1406 case HUB_TTTT_16_BITS
:
1407 hub
->tt
.think_time
= 666 * 2;
1408 dev_dbg(hub_dev
, "TT requires at most %d "
1409 "FS bit times (%d ns)\n",
1410 16, hub
->tt
.think_time
);
1412 case HUB_TTTT_24_BITS
:
1413 hub
->tt
.think_time
= 666 * 3;
1414 dev_dbg(hub_dev
, "TT requires at most %d "
1415 "FS bit times (%d ns)\n",
1416 24, hub
->tt
.think_time
);
1418 case HUB_TTTT_32_BITS
:
1419 hub
->tt
.think_time
= 666 * 4;
1420 dev_dbg(hub_dev
, "TT requires at most %d "
1421 "FS bit times (%d ns)\n",
1422 32, hub
->tt
.think_time
);
1426 /* probe() zeroes hub->indicator[] */
1427 if (wHubCharacteristics
& HUB_CHAR_PORTIND
) {
1428 hub
->has_indicators
= 1;
1429 dev_dbg(hub_dev
, "Port indicators are supported\n");
1432 dev_dbg(hub_dev
, "power on to power good time: %dms\n",
1433 hub
->descriptor
->bPwrOn2PwrGood
* 2);
1435 /* power budgeting mostly matters with bus-powered hubs,
1436 * and battery-powered root hubs (may provide just 8 mA).
1438 ret
= usb_get_status(hdev
, USB_RECIP_DEVICE
, 0, &hubstatus
);
1440 message
= "can't get hub status";
1443 le16_to_cpus(&hubstatus
);
1444 hcd
= bus_to_hcd(hdev
->bus
);
1445 if (hdev
== hdev
->bus
->root_hub
) {
1446 if (hcd
->power_budget
> 0)
1447 hdev
->bus_mA
= hcd
->power_budget
;
1449 hdev
->bus_mA
= full_load
* hdev
->maxchild
;
1450 if (hdev
->bus_mA
>= full_load
)
1451 hub
->mA_per_port
= full_load
;
1453 hub
->mA_per_port
= hdev
->bus_mA
;
1454 hub
->limited_power
= 1;
1456 } else if ((hubstatus
& (1 << USB_DEVICE_SELF_POWERED
)) == 0) {
1457 int remaining
= hdev
->bus_mA
-
1458 hub
->descriptor
->bHubContrCurrent
;
1460 dev_dbg(hub_dev
, "hub controller current requirement: %dmA\n",
1461 hub
->descriptor
->bHubContrCurrent
);
1462 hub
->limited_power
= 1;
1464 if (remaining
< hdev
->maxchild
* unit_load
)
1466 "insufficient power available "
1467 "to use all downstream ports\n");
1468 hub
->mA_per_port
= unit_load
; /* 7.2.1 */
1470 } else { /* Self-powered external hub */
1471 /* FIXME: What about battery-powered external hubs that
1472 * provide less current per port? */
1473 hub
->mA_per_port
= full_load
;
1475 if (hub
->mA_per_port
< full_load
)
1476 dev_dbg(hub_dev
, "%umA bus power budget for each child\n",
1479 /* Update the HCD's internal representation of this hub before khubd
1480 * starts getting port status changes for devices under the hub.
1482 if (hcd
->driver
->update_hub_device
) {
1483 ret
= hcd
->driver
->update_hub_device(hcd
, hdev
,
1484 &hub
->tt
, GFP_KERNEL
);
1486 message
= "can't update HCD hub info";
1491 ret
= hub_hub_status(hub
, &hubstatus
, &hubchange
);
1493 message
= "can't get hub status";
1497 /* local power status reports aren't always correct */
1498 if (hdev
->actconfig
->desc
.bmAttributes
& USB_CONFIG_ATT_SELFPOWER
)
1499 dev_dbg(hub_dev
, "local power source is %s\n",
1500 (hubstatus
& HUB_STATUS_LOCAL_POWER
)
1501 ? "lost (inactive)" : "good");
1503 if ((wHubCharacteristics
& HUB_CHAR_OCPM
) == 0)
1504 dev_dbg(hub_dev
, "%sover-current condition exists\n",
1505 (hubstatus
& HUB_STATUS_OVERCURRENT
) ? "" : "no ");
1507 /* set up the interrupt endpoint
1508 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1509 * bytes as USB2.0[11.12.3] says because some hubs are known
1510 * to send more data (and thus cause overflow). For root hubs,
1511 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1512 * to be big enough for at least USB_MAXCHILDREN ports. */
1513 pipe
= usb_rcvintpipe(hdev
, endpoint
->bEndpointAddress
);
1514 maxp
= usb_maxpacket(hdev
, pipe
, usb_pipeout(pipe
));
1516 if (maxp
> sizeof(*hub
->buffer
))
1517 maxp
= sizeof(*hub
->buffer
);
1519 hub
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
1525 usb_fill_int_urb(hub
->urb
, hdev
, pipe
, *hub
->buffer
, maxp
, hub_irq
,
1526 hub
, endpoint
->bInterval
);
1528 /* maybe cycle the hub leds */
1529 if (hub
->has_indicators
&& blinkenlights
)
1530 hub
->indicator
[0] = INDICATOR_CYCLE
;
1532 for (i
= 0; i
< hdev
->maxchild
; i
++)
1533 if (usb_hub_create_port_device(hub
, i
+ 1) < 0)
1534 dev_err(hub
->intfdev
,
1535 "couldn't create port%d device.\n", i
+ 1);
1537 usb_hub_adjust_deviceremovable(hdev
, hub
->descriptor
);
1539 hub_activate(hub
, HUB_INIT
);
1543 dev_err (hub_dev
, "config failed, %s (err %d)\n",
1545 /* hub_disconnect() frees urb and descriptor */
1549 static void hub_release(struct kref
*kref
)
1551 struct usb_hub
*hub
= container_of(kref
, struct usb_hub
, kref
);
1553 usb_put_intf(to_usb_interface(hub
->intfdev
));
1557 static unsigned highspeed_hubs
;
1559 static void hub_disconnect(struct usb_interface
*intf
)
1561 struct usb_hub
*hub
= usb_get_intfdata(intf
);
1562 struct usb_device
*hdev
= interface_to_usbdev(intf
);
1565 /* Take the hub off the event list and don't let it be added again */
1566 spin_lock_irq(&hub_event_lock
);
1567 if (!list_empty(&hub
->event_list
)) {
1568 list_del_init(&hub
->event_list
);
1569 usb_autopm_put_interface_no_suspend(intf
);
1571 hub
->disconnected
= 1;
1572 spin_unlock_irq(&hub_event_lock
);
1574 /* Disconnect all children and quiesce the hub */
1576 hub_quiesce(hub
, HUB_DISCONNECT
);
1578 usb_set_intfdata (intf
, NULL
);
1580 for (i
= 0; i
< hdev
->maxchild
; i
++)
1581 usb_hub_remove_port_device(hub
, i
+ 1);
1582 hub
->hdev
->maxchild
= 0;
1584 if (hub
->hdev
->speed
== USB_SPEED_HIGH
)
1587 usb_free_urb(hub
->urb
);
1589 kfree(hub
->descriptor
);
1593 pm_suspend_ignore_children(&intf
->dev
, false);
1594 kref_put(&hub
->kref
, hub_release
);
1597 static int hub_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1599 struct usb_host_interface
*desc
;
1600 struct usb_endpoint_descriptor
*endpoint
;
1601 struct usb_device
*hdev
;
1602 struct usb_hub
*hub
;
1604 desc
= intf
->cur_altsetting
;
1605 hdev
= interface_to_usbdev(intf
);
1608 * Set default autosuspend delay as 0 to speedup bus suspend,
1609 * based on the below considerations:
1611 * - Unlike other drivers, the hub driver does not rely on the
1612 * autosuspend delay to provide enough time to handle a wakeup
1613 * event, and the submitted status URB is just to check future
1614 * change on hub downstream ports, so it is safe to do it.
1616 * - The patch might cause one or more auto supend/resume for
1617 * below very rare devices when they are plugged into hub
1620 * devices having trouble initializing, and disconnect
1621 * themselves from the bus and then reconnect a second
1624 * devices just for downloading firmware, and disconnects
1625 * themselves after completing it
1627 * For these quite rare devices, their drivers may change the
1628 * autosuspend delay of their parent hub in the probe() to one
1629 * appropriate value to avoid the subtle problem if someone
1632 * - The patch may cause one or more auto suspend/resume on
1633 * hub during running 'lsusb', but it is probably too
1634 * infrequent to worry about.
1636 * - Change autosuspend delay of hub can avoid unnecessary auto
1637 * suspend timer for hub, also may decrease power consumption
1640 pm_runtime_set_autosuspend_delay(&hdev
->dev
, 0);
1642 /* Hubs have proper suspend/resume support. */
1643 usb_enable_autosuspend(hdev
);
1645 if (hdev
->level
== MAX_TOPO_LEVEL
) {
1647 "Unsupported bus topology: hub nested too deep\n");
1651 #ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1653 dev_warn(&intf
->dev
, "ignoring external hub\n");
1658 /* Some hubs have a subclass of 1, which AFAICT according to the */
1659 /* specs is not defined, but it works */
1660 if ((desc
->desc
.bInterfaceSubClass
!= 0) &&
1661 (desc
->desc
.bInterfaceSubClass
!= 1)) {
1663 dev_err (&intf
->dev
, "bad descriptor, ignoring hub\n");
1667 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1668 if (desc
->desc
.bNumEndpoints
!= 1)
1669 goto descriptor_error
;
1671 endpoint
= &desc
->endpoint
[0].desc
;
1673 /* If it's not an interrupt in endpoint, we'd better punt! */
1674 if (!usb_endpoint_is_int_in(endpoint
))
1675 goto descriptor_error
;
1677 /* We found a hub */
1678 dev_info (&intf
->dev
, "USB hub found\n");
1680 hub
= kzalloc(sizeof(*hub
), GFP_KERNEL
);
1682 dev_dbg (&intf
->dev
, "couldn't kmalloc hub struct\n");
1686 kref_init(&hub
->kref
);
1687 INIT_LIST_HEAD(&hub
->event_list
);
1688 hub
->intfdev
= &intf
->dev
;
1690 INIT_DELAYED_WORK(&hub
->leds
, led_work
);
1691 INIT_DELAYED_WORK(&hub
->init_work
, NULL
);
1694 usb_set_intfdata (intf
, hub
);
1695 intf
->needs_remote_wakeup
= 1;
1696 pm_suspend_ignore_children(&intf
->dev
, true);
1698 if (hdev
->speed
== USB_SPEED_HIGH
)
1701 if (id
->driver_info
& HUB_QUIRK_CHECK_PORT_AUTOSUSPEND
)
1702 hub
->quirk_check_port_auto_suspend
= 1;
1704 if (hub_configure(hub
, endpoint
) >= 0)
1707 hub_disconnect (intf
);
1712 hub_ioctl(struct usb_interface
*intf
, unsigned int code
, void *user_data
)
1714 struct usb_device
*hdev
= interface_to_usbdev (intf
);
1715 struct usb_hub
*hub
= hdev_to_hub(hdev
);
1717 /* assert ifno == 0 (part of hub spec) */
1719 case USBDEVFS_HUB_PORTINFO
: {
1720 struct usbdevfs_hub_portinfo
*info
= user_data
;
1723 spin_lock_irq(&device_state_lock
);
1724 if (hdev
->devnum
<= 0)
1727 info
->nports
= hdev
->maxchild
;
1728 for (i
= 0; i
< info
->nports
; i
++) {
1729 if (hub
->ports
[i
]->child
== NULL
)
1733 hub
->ports
[i
]->child
->devnum
;
1736 spin_unlock_irq(&device_state_lock
);
1738 return info
->nports
+ 1;
1747 * Allow user programs to claim ports on a hub. When a device is attached
1748 * to one of these "claimed" ports, the program will "own" the device.
1750 static int find_port_owner(struct usb_device
*hdev
, unsigned port1
,
1751 struct dev_state
***ppowner
)
1753 if (hdev
->state
== USB_STATE_NOTATTACHED
)
1755 if (port1
== 0 || port1
> hdev
->maxchild
)
1758 /* This assumes that devices not managed by the hub driver
1759 * will always have maxchild equal to 0.
1761 *ppowner
= &(hdev_to_hub(hdev
)->ports
[port1
- 1]->port_owner
);
1765 /* In the following three functions, the caller must hold hdev's lock */
1766 int usb_hub_claim_port(struct usb_device
*hdev
, unsigned port1
,
1767 struct dev_state
*owner
)
1770 struct dev_state
**powner
;
1772 rc
= find_port_owner(hdev
, port1
, &powner
);
1781 int usb_hub_release_port(struct usb_device
*hdev
, unsigned port1
,
1782 struct dev_state
*owner
)
1785 struct dev_state
**powner
;
1787 rc
= find_port_owner(hdev
, port1
, &powner
);
1790 if (*powner
!= owner
)
1796 void usb_hub_release_all_ports(struct usb_device
*hdev
, struct dev_state
*owner
)
1798 struct usb_hub
*hub
= hdev_to_hub(hdev
);
1801 for (n
= 0; n
< hdev
->maxchild
; n
++) {
1802 if (hub
->ports
[n
]->port_owner
== owner
)
1803 hub
->ports
[n
]->port_owner
= NULL
;
1808 /* The caller must hold udev's lock */
1809 bool usb_device_is_owned(struct usb_device
*udev
)
1811 struct usb_hub
*hub
;
1813 if (udev
->state
== USB_STATE_NOTATTACHED
|| !udev
->parent
)
1815 hub
= hdev_to_hub(udev
->parent
);
1816 return !!hub
->ports
[udev
->portnum
- 1]->port_owner
;
1819 static void recursively_mark_NOTATTACHED(struct usb_device
*udev
)
1821 struct usb_hub
*hub
= hdev_to_hub(udev
);
1824 for (i
= 0; i
< udev
->maxchild
; ++i
) {
1825 if (hub
->ports
[i
]->child
)
1826 recursively_mark_NOTATTACHED(hub
->ports
[i
]->child
);
1828 if (udev
->state
== USB_STATE_SUSPENDED
)
1829 udev
->active_duration
-= jiffies
;
1830 udev
->state
= USB_STATE_NOTATTACHED
;
1834 * usb_set_device_state - change a device's current state (usbcore, hcds)
1835 * @udev: pointer to device whose state should be changed
1836 * @new_state: new state value to be stored
1838 * udev->state is _not_ fully protected by the device lock. Although
1839 * most transitions are made only while holding the lock, the state can
1840 * can change to USB_STATE_NOTATTACHED at almost any time. This
1841 * is so that devices can be marked as disconnected as soon as possible,
1842 * without having to wait for any semaphores to be released. As a result,
1843 * all changes to any device's state must be protected by the
1844 * device_state_lock spinlock.
1846 * Once a device has been added to the device tree, all changes to its state
1847 * should be made using this routine. The state should _not_ be set directly.
1849 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1850 * Otherwise udev->state is set to new_state, and if new_state is
1851 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1852 * to USB_STATE_NOTATTACHED.
1854 void usb_set_device_state(struct usb_device
*udev
,
1855 enum usb_device_state new_state
)
1857 unsigned long flags
;
1860 spin_lock_irqsave(&device_state_lock
, flags
);
1861 if (udev
->state
== USB_STATE_NOTATTACHED
)
1863 else if (new_state
!= USB_STATE_NOTATTACHED
) {
1865 /* root hub wakeup capabilities are managed out-of-band
1866 * and may involve silicon errata ... ignore them here.
1869 if (udev
->state
== USB_STATE_SUSPENDED
1870 || new_state
== USB_STATE_SUSPENDED
)
1871 ; /* No change to wakeup settings */
1872 else if (new_state
== USB_STATE_CONFIGURED
)
1873 wakeup
= udev
->actconfig
->desc
.bmAttributes
1874 & USB_CONFIG_ATT_WAKEUP
;
1878 if (udev
->state
== USB_STATE_SUSPENDED
&&
1879 new_state
!= USB_STATE_SUSPENDED
)
1880 udev
->active_duration
-= jiffies
;
1881 else if (new_state
== USB_STATE_SUSPENDED
&&
1882 udev
->state
!= USB_STATE_SUSPENDED
)
1883 udev
->active_duration
+= jiffies
;
1884 udev
->state
= new_state
;
1886 recursively_mark_NOTATTACHED(udev
);
1887 spin_unlock_irqrestore(&device_state_lock
, flags
);
1889 device_set_wakeup_capable(&udev
->dev
, wakeup
);
1891 EXPORT_SYMBOL_GPL(usb_set_device_state
);
1894 * Choose a device number.
1896 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1897 * USB-2.0 buses they are also used as device addresses, however on
1898 * USB-3.0 buses the address is assigned by the controller hardware
1899 * and it usually is not the same as the device number.
1901 * WUSB devices are simple: they have no hubs behind, so the mapping
1902 * device <-> virtual port number becomes 1:1. Why? to simplify the
1903 * life of the device connection logic in
1904 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1905 * handshake we need to assign a temporary address in the unauthorized
1906 * space. For simplicity we use the first virtual port number found to
1907 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1908 * and that becomes it's address [X < 128] or its unauthorized address
1911 * We add 1 as an offset to the one-based USB-stack port number
1912 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1913 * 0 is reserved by USB for default address; (b) Linux's USB stack
1914 * uses always #1 for the root hub of the controller. So USB stack's
1915 * port #1, which is wusb virtual-port #0 has address #2.
1917 * Devices connected under xHCI are not as simple. The host controller
1918 * supports virtualization, so the hardware assigns device addresses and
1919 * the HCD must setup data structures before issuing a set address
1920 * command to the hardware.
1922 static void choose_devnum(struct usb_device
*udev
)
1925 struct usb_bus
*bus
= udev
->bus
;
1927 /* If khubd ever becomes multithreaded, this will need a lock */
1929 devnum
= udev
->portnum
+ 1;
1930 BUG_ON(test_bit(devnum
, bus
->devmap
.devicemap
));
1932 /* Try to allocate the next devnum beginning at
1933 * bus->devnum_next. */
1934 devnum
= find_next_zero_bit(bus
->devmap
.devicemap
, 128,
1937 devnum
= find_next_zero_bit(bus
->devmap
.devicemap
,
1939 bus
->devnum_next
= ( devnum
>= 127 ? 1 : devnum
+ 1);
1942 set_bit(devnum
, bus
->devmap
.devicemap
);
1943 udev
->devnum
= devnum
;
1947 static void release_devnum(struct usb_device
*udev
)
1949 if (udev
->devnum
> 0) {
1950 clear_bit(udev
->devnum
, udev
->bus
->devmap
.devicemap
);
1955 static void update_devnum(struct usb_device
*udev
, int devnum
)
1957 /* The address for a WUSB device is managed by wusbcore. */
1959 udev
->devnum
= devnum
;
1962 static void hub_free_dev(struct usb_device
*udev
)
1964 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
1966 /* Root hubs aren't real devices, so don't free HCD resources */
1967 if (hcd
->driver
->free_dev
&& udev
->parent
)
1968 hcd
->driver
->free_dev(hcd
, udev
);
1972 * usb_disconnect - disconnect a device (usbcore-internal)
1973 * @pdev: pointer to device being disconnected
1974 * Context: !in_interrupt ()
1976 * Something got disconnected. Get rid of it and all of its children.
1978 * If *pdev is a normal device then the parent hub must already be locked.
1979 * If *pdev is a root hub then this routine will acquire the
1980 * usb_bus_list_lock on behalf of the caller.
1982 * Only hub drivers (including virtual root hub drivers for host
1983 * controllers) should ever call this.
1985 * This call is synchronous, and may not be used in an interrupt context.
1987 void usb_disconnect(struct usb_device
**pdev
)
1989 struct usb_device
*udev
= *pdev
;
1990 struct usb_hub
*hub
= hdev_to_hub(udev
);
1993 /* mark the device as inactive, so any further urb submissions for
1994 * this device (and any of its children) will fail immediately.
1995 * this quiesces everything except pending urbs.
1997 usb_set_device_state(udev
, USB_STATE_NOTATTACHED
);
1998 dev_info(&udev
->dev
, "USB disconnect, device number %d\n",
2001 usb_lock_device(udev
);
2003 /* Free up all the children before we remove this device */
2004 for (i
= 0; i
< udev
->maxchild
; i
++) {
2005 if (hub
->ports
[i
]->child
)
2006 usb_disconnect(&hub
->ports
[i
]->child
);
2009 /* deallocate hcd/hardware state ... nuking all pending urbs and
2010 * cleaning up all state associated with the current configuration
2011 * so that the hardware is now fully quiesced.
2013 dev_dbg (&udev
->dev
, "unregistering device\n");
2014 usb_disable_device(udev
, 0);
2015 usb_hcd_synchronize_unlinks(udev
);
2018 struct usb_port
*port_dev
=
2019 hdev_to_hub(udev
->parent
)->ports
[udev
->portnum
- 1];
2021 sysfs_remove_link(&udev
->dev
.kobj
, "port");
2022 sysfs_remove_link(&port_dev
->dev
.kobj
, "device");
2024 pm_runtime_put(&port_dev
->dev
);
2027 usb_remove_ep_devs(&udev
->ep0
);
2028 usb_unlock_device(udev
);
2030 /* Unregister the device. The device driver is responsible
2031 * for de-configuring the device and invoking the remove-device
2032 * notifier chain (used by usbfs and possibly others).
2034 device_del(&udev
->dev
);
2036 /* Free the device number and delete the parent's children[]
2037 * (or root_hub) pointer.
2039 release_devnum(udev
);
2041 /* Avoid races with recursively_mark_NOTATTACHED() */
2042 spin_lock_irq(&device_state_lock
);
2044 spin_unlock_irq(&device_state_lock
);
2048 put_device(&udev
->dev
);
2051 #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
2052 static void show_string(struct usb_device
*udev
, char *id
, char *string
)
2056 dev_info(&udev
->dev
, "%s: %s\n", id
, string
);
2059 static void announce_device(struct usb_device
*udev
)
2061 dev_info(&udev
->dev
, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2062 le16_to_cpu(udev
->descriptor
.idVendor
),
2063 le16_to_cpu(udev
->descriptor
.idProduct
));
2064 dev_info(&udev
->dev
,
2065 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
2066 udev
->descriptor
.iManufacturer
,
2067 udev
->descriptor
.iProduct
,
2068 udev
->descriptor
.iSerialNumber
);
2069 show_string(udev
, "Product", udev
->product
);
2070 show_string(udev
, "Manufacturer", udev
->manufacturer
);
2071 show_string(udev
, "SerialNumber", udev
->serial
);
2074 static inline void announce_device(struct usb_device
*udev
) { }
2077 #ifdef CONFIG_USB_OTG
2078 #include "otg_whitelist.h"
2082 * usb_enumerate_device_otg - FIXME (usbcore-internal)
2083 * @udev: newly addressed device (in ADDRESS state)
2085 * Finish enumeration for On-The-Go devices
2087 static int usb_enumerate_device_otg(struct usb_device
*udev
)
2091 #ifdef CONFIG_USB_OTG
2093 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2094 * to wake us after we've powered off VBUS; and HNP, switching roles
2095 * "host" to "peripheral". The OTG descriptor helps figure this out.
2097 if (!udev
->bus
->is_b_host
2099 && udev
->parent
== udev
->bus
->root_hub
) {
2100 struct usb_otg_descriptor
*desc
= NULL
;
2101 struct usb_bus
*bus
= udev
->bus
;
2103 /* descriptor may appear anywhere in config */
2104 if (__usb_get_extra_descriptor (udev
->rawdescriptors
[0],
2105 le16_to_cpu(udev
->config
[0].desc
.wTotalLength
),
2106 USB_DT_OTG
, (void **) &desc
) == 0) {
2107 if (desc
->bmAttributes
& USB_OTG_HNP
) {
2108 unsigned port1
= udev
->portnum
;
2110 dev_info(&udev
->dev
,
2111 "Dual-Role OTG device on %sHNP port\n",
2112 (port1
== bus
->otg_port
)
2115 /* enable HNP before suspend, it's simpler */
2116 if (port1
== bus
->otg_port
)
2117 bus
->b_hnp_enable
= 1;
2118 err
= usb_control_msg(udev
,
2119 usb_sndctrlpipe(udev
, 0),
2120 USB_REQ_SET_FEATURE
, 0,
2122 ? USB_DEVICE_B_HNP_ENABLE
2123 : USB_DEVICE_A_ALT_HNP_SUPPORT
,
2124 0, NULL
, 0, USB_CTRL_SET_TIMEOUT
);
2126 /* OTG MESSAGE: report errors here,
2127 * customize to match your product.
2129 dev_info(&udev
->dev
,
2130 "can't set HNP mode: %d\n",
2132 bus
->b_hnp_enable
= 0;
2138 if (!is_targeted(udev
)) {
2140 /* Maybe it can talk to us, though we can't talk to it.
2141 * (Includes HNP test device.)
2143 if (udev
->bus
->b_hnp_enable
|| udev
->bus
->is_b_host
) {
2144 err
= usb_port_suspend(udev
, PMSG_SUSPEND
);
2146 dev_dbg(&udev
->dev
, "HNP fail, %d\n", err
);
2158 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
2159 * @udev: newly addressed device (in ADDRESS state)
2161 * This is only called by usb_new_device() and usb_authorize_device()
2162 * and FIXME -- all comments that apply to them apply here wrt to
2165 * If the device is WUSB and not authorized, we don't attempt to read
2166 * the string descriptors, as they will be errored out by the device
2167 * until it has been authorized.
2169 static int usb_enumerate_device(struct usb_device
*udev
)
2173 if (udev
->config
== NULL
) {
2174 err
= usb_get_configuration(udev
);
2176 dev_err(&udev
->dev
, "can't read configurations, error %d\n",
2181 if (udev
->wusb
== 1 && udev
->authorized
== 0) {
2182 udev
->product
= kstrdup("n/a (unauthorized)", GFP_KERNEL
);
2183 udev
->manufacturer
= kstrdup("n/a (unauthorized)", GFP_KERNEL
);
2184 udev
->serial
= kstrdup("n/a (unauthorized)", GFP_KERNEL
);
2187 /* read the standard strings and cache them if present */
2188 udev
->product
= usb_cache_string(udev
, udev
->descriptor
.iProduct
);
2189 udev
->manufacturer
= usb_cache_string(udev
,
2190 udev
->descriptor
.iManufacturer
);
2191 udev
->serial
= usb_cache_string(udev
, udev
->descriptor
.iSerialNumber
);
2193 err
= usb_enumerate_device_otg(udev
);
2197 usb_detect_interface_quirks(udev
);
2202 static void set_usb_port_removable(struct usb_device
*udev
)
2204 struct usb_device
*hdev
= udev
->parent
;
2205 struct usb_hub
*hub
;
2206 u8 port
= udev
->portnum
;
2207 u16 wHubCharacteristics
;
2208 bool removable
= true;
2213 hub
= hdev_to_hub(udev
->parent
);
2215 wHubCharacteristics
= le16_to_cpu(hub
->descriptor
->wHubCharacteristics
);
2217 if (!(wHubCharacteristics
& HUB_CHAR_COMPOUND
))
2220 if (hub_is_superspeed(hdev
)) {
2221 if (le16_to_cpu(hub
->descriptor
->u
.ss
.DeviceRemovable
)
2225 if (hub
->descriptor
->u
.hs
.DeviceRemovable
[port
/ 8] & (1 << (port
% 8)))
2230 udev
->removable
= USB_DEVICE_REMOVABLE
;
2232 udev
->removable
= USB_DEVICE_FIXED
;
2236 * usb_new_device - perform initial device setup (usbcore-internal)
2237 * @udev: newly addressed device (in ADDRESS state)
2239 * This is called with devices which have been detected but not fully
2240 * enumerated. The device descriptor is available, but not descriptors
2241 * for any device configuration. The caller must have locked either
2242 * the parent hub (if udev is a normal device) or else the
2243 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2244 * udev has already been installed, but udev is not yet visible through
2245 * sysfs or other filesystem code.
2247 * It will return if the device is configured properly or not. Zero if
2248 * the interface was registered with the driver core; else a negative
2251 * This call is synchronous, and may not be used in an interrupt context.
2253 * Only the hub driver or root-hub registrar should ever call this.
2255 int usb_new_device(struct usb_device
*udev
)
2260 /* Initialize non-root-hub device wakeup to disabled;
2261 * device (un)configuration controls wakeup capable
2262 * sysfs power/wakeup controls wakeup enabled/disabled
2264 device_init_wakeup(&udev
->dev
, 0);
2267 /* Tell the runtime-PM framework the device is active */
2268 pm_runtime_set_active(&udev
->dev
);
2269 pm_runtime_get_noresume(&udev
->dev
);
2270 pm_runtime_use_autosuspend(&udev
->dev
);
2271 pm_runtime_enable(&udev
->dev
);
2273 /* By default, forbid autosuspend for all devices. It will be
2274 * allowed for hubs during binding.
2276 usb_disable_autosuspend(udev
);
2278 err
= usb_enumerate_device(udev
); /* Read descriptors */
2281 dev_dbg(&udev
->dev
, "udev %d, busnum %d, minor = %d\n",
2282 udev
->devnum
, udev
->bus
->busnum
,
2283 (((udev
->bus
->busnum
-1) * 128) + (udev
->devnum
-1)));
2284 /* export the usbdev device-node for libusb */
2285 udev
->dev
.devt
= MKDEV(USB_DEVICE_MAJOR
,
2286 (((udev
->bus
->busnum
-1) * 128) + (udev
->devnum
-1)));
2288 /* Tell the world! */
2289 announce_device(udev
);
2292 add_device_randomness(udev
->serial
, strlen(udev
->serial
));
2294 add_device_randomness(udev
->product
, strlen(udev
->product
));
2295 if (udev
->manufacturer
)
2296 add_device_randomness(udev
->manufacturer
,
2297 strlen(udev
->manufacturer
));
2299 device_enable_async_suspend(&udev
->dev
);
2302 * check whether the hub marks this port as non-removable. Do it
2303 * now so that platform-specific data can override it in
2307 set_usb_port_removable(udev
);
2309 /* Register the device. The device driver is responsible
2310 * for configuring the device and invoking the add-device
2311 * notifier chain (used by usbfs and possibly others).
2313 err
= device_add(&udev
->dev
);
2315 dev_err(&udev
->dev
, "can't device_add, error %d\n", err
);
2319 /* Create link files between child device and usb port device. */
2321 struct usb_port
*port_dev
=
2322 hdev_to_hub(udev
->parent
)->ports
[udev
->portnum
- 1];
2324 err
= sysfs_create_link(&udev
->dev
.kobj
,
2325 &port_dev
->dev
.kobj
, "port");
2329 err
= sysfs_create_link(&port_dev
->dev
.kobj
,
2330 &udev
->dev
.kobj
, "device");
2332 sysfs_remove_link(&udev
->dev
.kobj
, "port");
2336 pm_runtime_get_sync(&port_dev
->dev
);
2339 (void) usb_create_ep_devs(&udev
->dev
, &udev
->ep0
, udev
);
2340 usb_mark_last_busy(udev
);
2341 pm_runtime_put_sync_autosuspend(&udev
->dev
);
2345 usb_set_device_state(udev
, USB_STATE_NOTATTACHED
);
2346 pm_runtime_disable(&udev
->dev
);
2347 pm_runtime_set_suspended(&udev
->dev
);
2353 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2354 * @usb_dev: USB device
2356 * Move the USB device to a very basic state where interfaces are disabled
2357 * and the device is in fact unconfigured and unusable.
2359 * We share a lock (that we have) with device_del(), so we need to
2362 int usb_deauthorize_device(struct usb_device
*usb_dev
)
2364 usb_lock_device(usb_dev
);
2365 if (usb_dev
->authorized
== 0)
2366 goto out_unauthorized
;
2368 usb_dev
->authorized
= 0;
2369 usb_set_configuration(usb_dev
, -1);
2371 kfree(usb_dev
->product
);
2372 usb_dev
->product
= kstrdup("n/a (unauthorized)", GFP_KERNEL
);
2373 kfree(usb_dev
->manufacturer
);
2374 usb_dev
->manufacturer
= kstrdup("n/a (unauthorized)", GFP_KERNEL
);
2375 kfree(usb_dev
->serial
);
2376 usb_dev
->serial
= kstrdup("n/a (unauthorized)", GFP_KERNEL
);
2378 usb_destroy_configuration(usb_dev
);
2379 usb_dev
->descriptor
.bNumConfigurations
= 0;
2382 usb_unlock_device(usb_dev
);
2387 int usb_authorize_device(struct usb_device
*usb_dev
)
2391 usb_lock_device(usb_dev
);
2392 if (usb_dev
->authorized
== 1)
2393 goto out_authorized
;
2395 result
= usb_autoresume_device(usb_dev
);
2397 dev_err(&usb_dev
->dev
,
2398 "can't autoresume for authorization: %d\n", result
);
2399 goto error_autoresume
;
2401 result
= usb_get_device_descriptor(usb_dev
, sizeof(usb_dev
->descriptor
));
2403 dev_err(&usb_dev
->dev
, "can't re-read device descriptor for "
2404 "authorization: %d\n", result
);
2405 goto error_device_descriptor
;
2408 kfree(usb_dev
->product
);
2409 usb_dev
->product
= NULL
;
2410 kfree(usb_dev
->manufacturer
);
2411 usb_dev
->manufacturer
= NULL
;
2412 kfree(usb_dev
->serial
);
2413 usb_dev
->serial
= NULL
;
2415 usb_dev
->authorized
= 1;
2416 result
= usb_enumerate_device(usb_dev
);
2418 goto error_enumerate
;
2419 /* Choose and set the configuration. This registers the interfaces
2420 * with the driver core and lets interface drivers bind to them.
2422 c
= usb_choose_configuration(usb_dev
);
2424 result
= usb_set_configuration(usb_dev
, c
);
2426 dev_err(&usb_dev
->dev
,
2427 "can't set config #%d, error %d\n", c
, result
);
2428 /* This need not be fatal. The user can try to
2429 * set other configurations. */
2432 dev_info(&usb_dev
->dev
, "authorized to connect\n");
2435 error_device_descriptor
:
2436 usb_autosuspend_device(usb_dev
);
2439 usb_unlock_device(usb_dev
); // complements locktree
2444 /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2445 static unsigned hub_is_wusb(struct usb_hub
*hub
)
2447 struct usb_hcd
*hcd
;
2448 if (hub
->hdev
->parent
!= NULL
) /* not a root hub? */
2450 hcd
= container_of(hub
->hdev
->bus
, struct usb_hcd
, self
);
2451 return hcd
->wireless
;
2455 #define PORT_RESET_TRIES 5
2456 #define SET_ADDRESS_TRIES 2
2457 #define GET_DESCRIPTOR_TRIES 2
2458 #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
2459 #define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
2461 #define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2462 #define HUB_SHORT_RESET_TIME 10
2463 #define HUB_BH_RESET_TIME 50
2464 #define HUB_LONG_RESET_TIME 200
2465 #define HUB_RESET_TIMEOUT 800
2467 static int hub_port_reset(struct usb_hub
*hub
, int port1
,
2468 struct usb_device
*udev
, unsigned int delay
, bool warm
);
2470 /* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2471 * Port worm reset is required to recover
2473 static bool hub_port_warm_reset_required(struct usb_hub
*hub
, u16 portstatus
)
2475 return hub_is_superspeed(hub
->hdev
) &&
2476 (((portstatus
& USB_PORT_STAT_LINK_STATE
) ==
2477 USB_SS_PORT_LS_SS_INACTIVE
) ||
2478 ((portstatus
& USB_PORT_STAT_LINK_STATE
) ==
2479 USB_SS_PORT_LS_COMP_MOD
)) ;
2482 static int hub_port_wait_reset(struct usb_hub
*hub
, int port1
,
2483 struct usb_device
*udev
, unsigned int delay
, bool warm
)
2485 int delay_time
, ret
;
2489 for (delay_time
= 0;
2490 delay_time
< HUB_RESET_TIMEOUT
;
2491 delay_time
+= delay
) {
2492 /* wait to give the device a chance to reset */
2495 /* read and decode port status */
2496 ret
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
2500 /* The port state is unknown until the reset completes. */
2501 if (!(portstatus
& USB_PORT_STAT_RESET
))
2504 /* switch to the long delay after two short delay failures */
2505 if (delay_time
>= 2 * HUB_SHORT_RESET_TIME
)
2506 delay
= HUB_LONG_RESET_TIME
;
2508 dev_dbg (hub
->intfdev
,
2509 "port %d not %sreset yet, waiting %dms\n",
2510 port1
, warm
? "warm " : "", delay
);
2513 if ((portstatus
& USB_PORT_STAT_RESET
))
2516 if (hub_port_warm_reset_required(hub
, portstatus
))
2519 /* Device went away? */
2520 if (!(portstatus
& USB_PORT_STAT_CONNECTION
))
2523 /* bomb out completely if the connection bounced. A USB 3.0
2524 * connection may bounce if multiple warm resets were issued,
2525 * but the device may have successfully re-connected. Ignore it.
2527 if (!hub_is_superspeed(hub
->hdev
) &&
2528 (portchange
& USB_PORT_STAT_C_CONNECTION
))
2531 if (!(portstatus
& USB_PORT_STAT_ENABLE
))
2537 if (hub_is_wusb(hub
))
2538 udev
->speed
= USB_SPEED_WIRELESS
;
2539 else if (hub_is_superspeed(hub
->hdev
))
2540 udev
->speed
= USB_SPEED_SUPER
;
2541 else if (portstatus
& USB_PORT_STAT_HIGH_SPEED
)
2542 udev
->speed
= USB_SPEED_HIGH
;
2543 else if (portstatus
& USB_PORT_STAT_LOW_SPEED
)
2544 udev
->speed
= USB_SPEED_LOW
;
2546 udev
->speed
= USB_SPEED_FULL
;
2550 static void hub_port_finish_reset(struct usb_hub
*hub
, int port1
,
2551 struct usb_device
*udev
, int *status
)
2555 /* TRSTRCY = 10 ms; plus some extra */
2558 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
2560 update_devnum(udev
, 0);
2561 /* The xHC may think the device is already reset,
2562 * so ignore the status.
2564 if (hcd
->driver
->reset_device
)
2565 hcd
->driver
->reset_device(hcd
, udev
);
2570 clear_port_feature(hub
->hdev
,
2571 port1
, USB_PORT_FEAT_C_RESET
);
2572 if (hub_is_superspeed(hub
->hdev
)) {
2573 clear_port_feature(hub
->hdev
, port1
,
2574 USB_PORT_FEAT_C_BH_PORT_RESET
);
2575 clear_port_feature(hub
->hdev
, port1
,
2576 USB_PORT_FEAT_C_PORT_LINK_STATE
);
2577 clear_port_feature(hub
->hdev
, port1
,
2578 USB_PORT_FEAT_C_CONNECTION
);
2581 usb_set_device_state(udev
, *status
2582 ? USB_STATE_NOTATTACHED
2583 : USB_STATE_DEFAULT
);
2588 /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
2589 static int hub_port_reset(struct usb_hub
*hub
, int port1
,
2590 struct usb_device
*udev
, unsigned int delay
, bool warm
)
2593 u16 portchange
, portstatus
;
2595 if (!hub_is_superspeed(hub
->hdev
)) {
2597 dev_err(hub
->intfdev
, "only USB3 hub support "
2601 /* Block EHCI CF initialization during the port reset.
2602 * Some companion controllers don't like it when they mix.
2604 down_read(&ehci_cf_port_reset_rwsem
);
2607 * If the caller hasn't explicitly requested a warm reset,
2608 * double check and see if one is needed.
2610 status
= hub_port_status(hub
, port1
,
2611 &portstatus
, &portchange
);
2615 if (hub_port_warm_reset_required(hub
, portstatus
))
2619 /* Reset the port */
2620 for (i
= 0; i
< PORT_RESET_TRIES
; i
++) {
2621 status
= set_port_feature(hub
->hdev
, port1
, (warm
?
2622 USB_PORT_FEAT_BH_PORT_RESET
:
2623 USB_PORT_FEAT_RESET
));
2625 dev_err(hub
->intfdev
,
2626 "cannot %sreset port %d (err = %d)\n",
2627 warm
? "warm " : "", port1
, status
);
2629 status
= hub_port_wait_reset(hub
, port1
, udev
, delay
,
2631 if (status
&& status
!= -ENOTCONN
)
2632 dev_dbg(hub
->intfdev
,
2633 "port_wait_reset: err = %d\n",
2637 /* Check for disconnect or reset */
2638 if (status
== 0 || status
== -ENOTCONN
|| status
== -ENODEV
) {
2639 hub_port_finish_reset(hub
, port1
, udev
, &status
);
2641 if (!hub_is_superspeed(hub
->hdev
))
2645 * If a USB 3.0 device migrates from reset to an error
2646 * state, re-issue the warm reset.
2648 if (hub_port_status(hub
, port1
,
2649 &portstatus
, &portchange
) < 0)
2652 if (!hub_port_warm_reset_required(hub
, portstatus
))
2656 * If the port is in SS.Inactive or Compliance Mode, the
2657 * hot or warm reset failed. Try another warm reset.
2660 dev_dbg(hub
->intfdev
, "hot reset failed, warm reset port %d\n",
2666 dev_dbg (hub
->intfdev
,
2667 "port %d not enabled, trying %sreset again...\n",
2668 port1
, warm
? "warm " : "");
2669 delay
= HUB_LONG_RESET_TIME
;
2672 dev_err (hub
->intfdev
,
2673 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2677 if (!hub_is_superspeed(hub
->hdev
))
2678 up_read(&ehci_cf_port_reset_rwsem
);
2683 /* Check if a port is power on */
2684 static int port_is_power_on(struct usb_hub
*hub
, unsigned portstatus
)
2688 if (hub_is_superspeed(hub
->hdev
)) {
2689 if (portstatus
& USB_SS_PORT_STAT_POWER
)
2692 if (portstatus
& USB_PORT_STAT_POWER
)
2701 /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2702 static int port_is_suspended(struct usb_hub
*hub
, unsigned portstatus
)
2706 if (hub_is_superspeed(hub
->hdev
)) {
2707 if ((portstatus
& USB_PORT_STAT_LINK_STATE
)
2708 == USB_SS_PORT_LS_U3
)
2711 if (portstatus
& USB_PORT_STAT_SUSPEND
)
2718 /* Determine whether the device on a port is ready for a normal resume,
2719 * is ready for a reset-resume, or should be disconnected.
2721 static int check_port_resume_type(struct usb_device
*udev
,
2722 struct usb_hub
*hub
, int port1
,
2723 int status
, unsigned portchange
, unsigned portstatus
)
2725 /* Is the device still present? */
2726 if (status
|| port_is_suspended(hub
, portstatus
) ||
2727 !port_is_power_on(hub
, portstatus
) ||
2728 !(portstatus
& USB_PORT_STAT_CONNECTION
)) {
2733 /* Can't do a normal resume if the port isn't enabled,
2734 * so try a reset-resume instead.
2736 else if (!(portstatus
& USB_PORT_STAT_ENABLE
) && !udev
->reset_resume
) {
2737 if (udev
->persist_enabled
)
2738 udev
->reset_resume
= 1;
2744 dev_dbg(hub
->intfdev
,
2745 "port %d status %04x.%04x after resume, %d\n",
2746 port1
, portchange
, portstatus
, status
);
2747 } else if (udev
->reset_resume
) {
2749 /* Late port handoff can set status-change bits */
2750 if (portchange
& USB_PORT_STAT_C_CONNECTION
)
2751 clear_port_feature(hub
->hdev
, port1
,
2752 USB_PORT_FEAT_C_CONNECTION
);
2753 if (portchange
& USB_PORT_STAT_C_ENABLE
)
2754 clear_port_feature(hub
->hdev
, port1
,
2755 USB_PORT_FEAT_C_ENABLE
);
2761 int usb_disable_ltm(struct usb_device
*udev
)
2763 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
2765 /* Check if the roothub and device supports LTM. */
2766 if (!usb_device_supports_ltm(hcd
->self
.root_hub
) ||
2767 !usb_device_supports_ltm(udev
))
2770 /* Clear Feature LTM Enable can only be sent if the device is
2773 if (!udev
->actconfig
)
2776 return usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
2777 USB_REQ_CLEAR_FEATURE
, USB_RECIP_DEVICE
,
2778 USB_DEVICE_LTM_ENABLE
, 0, NULL
, 0,
2779 USB_CTRL_SET_TIMEOUT
);
2781 EXPORT_SYMBOL_GPL(usb_disable_ltm
);
2783 void usb_enable_ltm(struct usb_device
*udev
)
2785 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
2787 /* Check if the roothub and device supports LTM. */
2788 if (!usb_device_supports_ltm(hcd
->self
.root_hub
) ||
2789 !usb_device_supports_ltm(udev
))
2792 /* Set Feature LTM Enable can only be sent if the device is
2795 if (!udev
->actconfig
)
2798 usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
2799 USB_REQ_SET_FEATURE
, USB_RECIP_DEVICE
,
2800 USB_DEVICE_LTM_ENABLE
, 0, NULL
, 0,
2801 USB_CTRL_SET_TIMEOUT
);
2803 EXPORT_SYMBOL_GPL(usb_enable_ltm
);
2805 #ifdef CONFIG_USB_SUSPEND
2808 * usb_port_suspend - suspend a usb device's upstream port
2809 * @udev: device that's no longer in active use, not a root hub
2810 * Context: must be able to sleep; device not locked; pm locks held
2812 * Suspends a USB device that isn't in active use, conserving power.
2813 * Devices may wake out of a suspend, if anything important happens,
2814 * using the remote wakeup mechanism. They may also be taken out of
2815 * suspend by the host, using usb_port_resume(). It's also routine
2816 * to disconnect devices while they are suspended.
2818 * This only affects the USB hardware for a device; its interfaces
2819 * (and, for hubs, child devices) must already have been suspended.
2821 * Selective port suspend reduces power; most suspended devices draw
2822 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2823 * All devices below the suspended port are also suspended.
2825 * Devices leave suspend state when the host wakes them up. Some devices
2826 * also support "remote wakeup", where the device can activate the USB
2827 * tree above them to deliver data, such as a keypress or packet. In
2828 * some cases, this wakes the USB host.
2830 * Suspending OTG devices may trigger HNP, if that's been enabled
2831 * between a pair of dual-role devices. That will change roles, such
2832 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2834 * Devices on USB hub ports have only one "suspend" state, corresponding
2835 * to ACPI D2, "may cause the device to lose some context".
2836 * State transitions include:
2838 * - suspend, resume ... when the VBUS power link stays live
2839 * - suspend, disconnect ... VBUS lost
2841 * Once VBUS drop breaks the circuit, the port it's using has to go through
2842 * normal re-enumeration procedures, starting with enabling VBUS power.
2843 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2844 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2845 * timer, no SRP, no requests through sysfs.
2847 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2848 * the root hub for their bus goes into global suspend ... so we don't
2849 * (falsely) update the device power state to say it suspended.
2851 * Returns 0 on success, else negative errno.
2853 int usb_port_suspend(struct usb_device
*udev
, pm_message_t msg
)
2855 struct usb_hub
*hub
= hdev_to_hub(udev
->parent
);
2856 int port1
= udev
->portnum
;
2859 /* enable remote wakeup when appropriate; this lets the device
2860 * wake up the upstream hub (including maybe the root hub).
2862 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2863 * we don't explicitly enable it here.
2865 if (udev
->do_remote_wakeup
) {
2866 if (!hub_is_superspeed(hub
->hdev
)) {
2867 status
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
2868 USB_REQ_SET_FEATURE
, USB_RECIP_DEVICE
,
2869 USB_DEVICE_REMOTE_WAKEUP
, 0,
2871 USB_CTRL_SET_TIMEOUT
);
2873 /* Assume there's only one function on the USB 3.0
2874 * device and enable remote wake for the first
2875 * interface. FIXME if the interface association
2876 * descriptor shows there's more than one function.
2878 status
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
2879 USB_REQ_SET_FEATURE
,
2880 USB_RECIP_INTERFACE
,
2881 USB_INTRF_FUNC_SUSPEND
,
2882 USB_INTRF_FUNC_SUSPEND_RW
|
2883 USB_INTRF_FUNC_SUSPEND_LP
,
2885 USB_CTRL_SET_TIMEOUT
);
2888 dev_dbg(&udev
->dev
, "won't remote wakeup, status %d\n",
2890 /* bail if autosuspend is requested */
2891 if (PMSG_IS_AUTO(msg
))
2896 /* disable USB2 hardware LPM */
2897 if (udev
->usb2_hw_lpm_enabled
== 1)
2898 usb_set_usb2_hardware_lpm(udev
, 0);
2900 if (usb_disable_ltm(udev
)) {
2901 dev_err(&udev
->dev
, "%s Failed to disable LTM before suspend\n.",
2905 if (usb_unlocked_disable_lpm(udev
)) {
2906 dev_err(&udev
->dev
, "%s Failed to disable LPM before suspend\n.",
2912 if (hub_is_superspeed(hub
->hdev
))
2913 status
= hub_set_port_link_state(hub
, port1
, USB_SS_PORT_LS_U3
);
2915 status
= set_port_feature(hub
->hdev
, port1
,
2916 USB_PORT_FEAT_SUSPEND
);
2918 dev_dbg(hub
->intfdev
, "can't suspend port %d, status %d\n",
2920 /* paranoia: "should not happen" */
2921 if (udev
->do_remote_wakeup
)
2922 (void) usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
2923 USB_REQ_CLEAR_FEATURE
, USB_RECIP_DEVICE
,
2924 USB_DEVICE_REMOTE_WAKEUP
, 0,
2926 USB_CTRL_SET_TIMEOUT
);
2928 /* Try to enable USB2 hardware LPM again */
2929 if (udev
->usb2_hw_lpm_capable
== 1)
2930 usb_set_usb2_hardware_lpm(udev
, 1);
2932 /* Try to enable USB3 LTM and LPM again */
2933 usb_enable_ltm(udev
);
2934 usb_unlocked_enable_lpm(udev
);
2936 /* System sleep transitions should never fail */
2937 if (!PMSG_IS_AUTO(msg
))
2940 /* device has up to 10 msec to fully suspend */
2941 dev_dbg(&udev
->dev
, "usb %ssuspend, wakeup %d\n",
2942 (PMSG_IS_AUTO(msg
) ? "auto-" : ""),
2943 udev
->do_remote_wakeup
);
2944 usb_set_device_state(udev
, USB_STATE_SUSPENDED
);
2945 udev
->port_is_suspended
= 1;
2948 usb_mark_last_busy(hub
->hdev
);
2953 * If the USB "suspend" state is in use (rather than "global suspend"),
2954 * many devices will be individually taken out of suspend state using
2955 * special "resume" signaling. This routine kicks in shortly after
2956 * hardware resume signaling is finished, either because of selective
2957 * resume (by host) or remote wakeup (by device) ... now see what changed
2958 * in the tree that's rooted at this device.
2960 * If @udev->reset_resume is set then the device is reset before the
2961 * status check is done.
2963 static int finish_port_resume(struct usb_device
*udev
)
2968 /* caller owns the udev device lock */
2969 dev_dbg(&udev
->dev
, "%s\n",
2970 udev
->reset_resume
? "finish reset-resume" : "finish resume");
2972 /* usb ch9 identifies four variants of SUSPENDED, based on what
2973 * state the device resumes to. Linux currently won't see the
2974 * first two on the host side; they'd be inside hub_port_init()
2975 * during many timeouts, but khubd can't suspend until later.
2977 usb_set_device_state(udev
, udev
->actconfig
2978 ? USB_STATE_CONFIGURED
2979 : USB_STATE_ADDRESS
);
2981 /* 10.5.4.5 says not to reset a suspended port if the attached
2982 * device is enabled for remote wakeup. Hence the reset
2983 * operation is carried out here, after the port has been
2986 if (udev
->reset_resume
)
2988 status
= usb_reset_and_verify_device(udev
);
2990 /* 10.5.4.5 says be sure devices in the tree are still there.
2991 * For now let's assume the device didn't go crazy on resume,
2992 * and device drivers will know about any resume quirks.
2996 status
= usb_get_status(udev
, USB_RECIP_DEVICE
, 0, &devstatus
);
2998 status
= (status
> 0 ? 0 : -ENODEV
);
3000 /* If a normal resume failed, try doing a reset-resume */
3001 if (status
&& !udev
->reset_resume
&& udev
->persist_enabled
) {
3002 dev_dbg(&udev
->dev
, "retry with reset-resume\n");
3003 udev
->reset_resume
= 1;
3004 goto retry_reset_resume
;
3009 dev_dbg(&udev
->dev
, "gone after usb resume? status %d\n",
3012 * There are a few quirky devices which violate the standard
3013 * by claiming to have remote wakeup enabled after a reset,
3014 * which crash if the feature is cleared, hence check for
3015 * udev->reset_resume
3017 } else if (udev
->actconfig
&& !udev
->reset_resume
) {
3018 le16_to_cpus(&devstatus
);
3019 if (devstatus
& (1 << USB_DEVICE_REMOTE_WAKEUP
)) {
3020 status
= usb_control_msg(udev
,
3021 usb_sndctrlpipe(udev
, 0),
3022 USB_REQ_CLEAR_FEATURE
,
3024 USB_DEVICE_REMOTE_WAKEUP
, 0,
3026 USB_CTRL_SET_TIMEOUT
);
3029 "disable remote wakeup, status %d\n",
3038 * usb_port_resume - re-activate a suspended usb device's upstream port
3039 * @udev: device to re-activate, not a root hub
3040 * Context: must be able to sleep; device not locked; pm locks held
3042 * This will re-activate the suspended device, increasing power usage
3043 * while letting drivers communicate again with its endpoints.
3044 * USB resume explicitly guarantees that the power session between
3045 * the host and the device is the same as it was when the device
3048 * If @udev->reset_resume is set then this routine won't check that the
3049 * port is still enabled. Furthermore, finish_port_resume() above will
3050 * reset @udev. The end result is that a broken power session can be
3051 * recovered and @udev will appear to persist across a loss of VBUS power.
3053 * For example, if a host controller doesn't maintain VBUS suspend current
3054 * during a system sleep or is reset when the system wakes up, all the USB
3055 * power sessions below it will be broken. This is especially troublesome
3056 * for mass-storage devices containing mounted filesystems, since the
3057 * device will appear to have disconnected and all the memory mappings
3058 * to it will be lost. Using the USB_PERSIST facility, the device can be
3059 * made to appear as if it had not disconnected.
3061 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
3062 * every effort to insure that the same device is present after the
3063 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3064 * quite possible for a device to remain unaltered but its media to be
3065 * changed. If the user replaces a flash memory card while the system is
3066 * asleep, he will have only himself to blame when the filesystem on the
3067 * new card is corrupted and the system crashes.
3069 * Returns 0 on success, else negative errno.
3071 int usb_port_resume(struct usb_device
*udev
, pm_message_t msg
)
3073 struct usb_hub
*hub
= hdev_to_hub(udev
->parent
);
3074 int port1
= udev
->portnum
;
3076 u16 portchange
, portstatus
;
3078 /* Skip the initial Clear-Suspend step for a remote wakeup */
3079 status
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
3080 if (status
== 0 && !port_is_suspended(hub
, portstatus
))
3081 goto SuspendCleared
;
3083 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
3085 set_bit(port1
, hub
->busy_bits
);
3087 /* see 7.1.7.7; affects power usage, but not budgeting */
3088 if (hub_is_superspeed(hub
->hdev
))
3089 status
= hub_set_port_link_state(hub
, port1
, USB_SS_PORT_LS_U0
);
3091 status
= clear_port_feature(hub
->hdev
,
3092 port1
, USB_PORT_FEAT_SUSPEND
);
3094 dev_dbg(hub
->intfdev
, "can't resume port %d, status %d\n",
3097 /* drive resume for at least 20 msec */
3098 dev_dbg(&udev
->dev
, "usb %sresume\n",
3099 (PMSG_IS_AUTO(msg
) ? "auto-" : ""));
3102 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3103 * stop resume signaling. Then finish the resume
3106 status
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
3108 /* TRSMRCY = 10 msec */
3114 udev
->port_is_suspended
= 0;
3115 if (hub_is_superspeed(hub
->hdev
)) {
3116 if (portchange
& USB_PORT_STAT_C_LINK_STATE
)
3117 clear_port_feature(hub
->hdev
, port1
,
3118 USB_PORT_FEAT_C_PORT_LINK_STATE
);
3120 if (portchange
& USB_PORT_STAT_C_SUSPEND
)
3121 clear_port_feature(hub
->hdev
, port1
,
3122 USB_PORT_FEAT_C_SUSPEND
);
3126 clear_bit(port1
, hub
->busy_bits
);
3128 status
= check_port_resume_type(udev
,
3129 hub
, port1
, status
, portchange
, portstatus
);
3131 status
= finish_port_resume(udev
);
3133 dev_dbg(&udev
->dev
, "can't resume, status %d\n", status
);
3134 hub_port_logical_disconnect(hub
, port1
);
3136 /* Try to enable USB2 hardware LPM */
3137 if (udev
->usb2_hw_lpm_capable
== 1)
3138 usb_set_usb2_hardware_lpm(udev
, 1);
3140 /* Try to enable USB3 LTM and LPM */
3141 usb_enable_ltm(udev
);
3142 usb_unlocked_enable_lpm(udev
);
3148 /* caller has locked udev */
3149 int usb_remote_wakeup(struct usb_device
*udev
)
3153 if (udev
->state
== USB_STATE_SUSPENDED
) {
3154 dev_dbg(&udev
->dev
, "usb %sresume\n", "wakeup-");
3155 status
= usb_autoresume_device(udev
);
3157 /* Let the drivers do their thing, then... */
3158 usb_autosuspend_device(udev
);
3164 #else /* CONFIG_USB_SUSPEND */
3166 /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3168 int usb_port_suspend(struct usb_device
*udev
, pm_message_t msg
)
3173 /* However we may need to do a reset-resume */
3175 int usb_port_resume(struct usb_device
*udev
, pm_message_t msg
)
3177 struct usb_hub
*hub
= hdev_to_hub(udev
->parent
);
3178 int port1
= udev
->portnum
;
3180 u16 portchange
, portstatus
;
3182 status
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
3183 status
= check_port_resume_type(udev
,
3184 hub
, port1
, status
, portchange
, portstatus
);
3187 dev_dbg(&udev
->dev
, "can't resume, status %d\n", status
);
3188 hub_port_logical_disconnect(hub
, port1
);
3189 } else if (udev
->reset_resume
) {
3190 dev_dbg(&udev
->dev
, "reset-resume\n");
3191 status
= usb_reset_and_verify_device(udev
);
3198 static int check_ports_changed(struct usb_hub
*hub
)
3202 for (port1
= 1; port1
<= hub
->hdev
->maxchild
; ++port1
) {
3203 u16 portstatus
, portchange
;
3206 status
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
3207 if (!status
&& portchange
)
3213 static int hub_suspend(struct usb_interface
*intf
, pm_message_t msg
)
3215 struct usb_hub
*hub
= usb_get_intfdata (intf
);
3216 struct usb_device
*hdev
= hub
->hdev
;
3220 /* Warn if children aren't already suspended */
3221 for (port1
= 1; port1
<= hdev
->maxchild
; port1
++) {
3222 struct usb_device
*udev
;
3224 udev
= hub
->ports
[port1
- 1]->child
;
3225 if (udev
&& udev
->can_submit
) {
3226 dev_warn(&intf
->dev
, "port %d nyet suspended\n", port1
);
3227 if (PMSG_IS_AUTO(msg
))
3232 if (hdev
->do_remote_wakeup
&& hub
->quirk_check_port_auto_suspend
) {
3233 /* check if there are changes pending on hub ports */
3234 if (check_ports_changed(hub
)) {
3235 if (PMSG_IS_AUTO(msg
))
3237 pm_wakeup_event(&hdev
->dev
, 2000);
3241 if (hub_is_superspeed(hdev
) && hdev
->do_remote_wakeup
) {
3242 /* Enable hub to send remote wakeup for all ports. */
3243 for (port1
= 1; port1
<= hdev
->maxchild
; port1
++) {
3244 status
= set_port_feature(hdev
,
3246 USB_PORT_FEAT_REMOTE_WAKE_CONNECT
|
3247 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT
|
3248 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT
,
3249 USB_PORT_FEAT_REMOTE_WAKE_MASK
);
3253 dev_dbg(&intf
->dev
, "%s\n", __func__
);
3255 /* stop khubd and related activity */
3256 hub_quiesce(hub
, HUB_SUSPEND
);
3260 static int hub_resume(struct usb_interface
*intf
)
3262 struct usb_hub
*hub
= usb_get_intfdata(intf
);
3264 dev_dbg(&intf
->dev
, "%s\n", __func__
);
3265 hub_activate(hub
, HUB_RESUME
);
3269 static int hub_reset_resume(struct usb_interface
*intf
)
3271 struct usb_hub
*hub
= usb_get_intfdata(intf
);
3273 dev_dbg(&intf
->dev
, "%s\n", __func__
);
3274 hub_activate(hub
, HUB_RESET_RESUME
);
3279 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3280 * @rhdev: struct usb_device for the root hub
3282 * The USB host controller driver calls this function when its root hub
3283 * is resumed and Vbus power has been interrupted or the controller
3284 * has been reset. The routine marks @rhdev as having lost power.
3285 * When the hub driver is resumed it will take notice and carry out
3286 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3287 * the others will be disconnected.
3289 void usb_root_hub_lost_power(struct usb_device
*rhdev
)
3291 dev_warn(&rhdev
->dev
, "root hub lost power or was reset\n");
3292 rhdev
->reset_resume
= 1;
3294 EXPORT_SYMBOL_GPL(usb_root_hub_lost_power
);
3296 static const char * const usb3_lpm_names
[] = {
3304 * Send a Set SEL control transfer to the device, prior to enabling
3305 * device-initiated U1 or U2. This lets the device know the exit latencies from
3306 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3307 * packet from the host.
3309 * This function will fail if the SEL or PEL values for udev are greater than
3310 * the maximum allowed values for the link state to be enabled.
3312 static int usb_req_set_sel(struct usb_device
*udev
, enum usb3_link_state state
)
3314 struct usb_set_sel_req
*sel_values
;
3315 unsigned long long u1_sel
;
3316 unsigned long long u1_pel
;
3317 unsigned long long u2_sel
;
3318 unsigned long long u2_pel
;
3321 /* Convert SEL and PEL stored in ns to us */
3322 u1_sel
= DIV_ROUND_UP(udev
->u1_params
.sel
, 1000);
3323 u1_pel
= DIV_ROUND_UP(udev
->u1_params
.pel
, 1000);
3324 u2_sel
= DIV_ROUND_UP(udev
->u2_params
.sel
, 1000);
3325 u2_pel
= DIV_ROUND_UP(udev
->u2_params
.pel
, 1000);
3328 * Make sure that the calculated SEL and PEL values for the link
3329 * state we're enabling aren't bigger than the max SEL/PEL
3330 * value that will fit in the SET SEL control transfer.
3331 * Otherwise the device would get an incorrect idea of the exit
3332 * latency for the link state, and could start a device-initiated
3333 * U1/U2 when the exit latencies are too high.
3335 if ((state
== USB3_LPM_U1
&&
3336 (u1_sel
> USB3_LPM_MAX_U1_SEL_PEL
||
3337 u1_pel
> USB3_LPM_MAX_U1_SEL_PEL
)) ||
3338 (state
== USB3_LPM_U2
&&
3339 (u2_sel
> USB3_LPM_MAX_U2_SEL_PEL
||
3340 u2_pel
> USB3_LPM_MAX_U2_SEL_PEL
))) {
3341 dev_dbg(&udev
->dev
, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n",
3342 usb3_lpm_names
[state
], u1_sel
, u1_pel
);
3347 * If we're enabling device-initiated LPM for one link state,
3348 * but the other link state has a too high SEL or PEL value,
3349 * just set those values to the max in the Set SEL request.
3351 if (u1_sel
> USB3_LPM_MAX_U1_SEL_PEL
)
3352 u1_sel
= USB3_LPM_MAX_U1_SEL_PEL
;
3354 if (u1_pel
> USB3_LPM_MAX_U1_SEL_PEL
)
3355 u1_pel
= USB3_LPM_MAX_U1_SEL_PEL
;
3357 if (u2_sel
> USB3_LPM_MAX_U2_SEL_PEL
)
3358 u2_sel
= USB3_LPM_MAX_U2_SEL_PEL
;
3360 if (u2_pel
> USB3_LPM_MAX_U2_SEL_PEL
)
3361 u2_pel
= USB3_LPM_MAX_U2_SEL_PEL
;
3364 * usb_enable_lpm() can be called as part of a failed device reset,
3365 * which may be initiated by an error path of a mass storage driver.
3366 * Therefore, use GFP_NOIO.
3368 sel_values
= kmalloc(sizeof *(sel_values
), GFP_NOIO
);
3372 sel_values
->u1_sel
= u1_sel
;
3373 sel_values
->u1_pel
= u1_pel
;
3374 sel_values
->u2_sel
= cpu_to_le16(u2_sel
);
3375 sel_values
->u2_pel
= cpu_to_le16(u2_pel
);
3377 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
3381 sel_values
, sizeof *(sel_values
),
3382 USB_CTRL_SET_TIMEOUT
);
3388 * Enable or disable device-initiated U1 or U2 transitions.
3390 static int usb_set_device_initiated_lpm(struct usb_device
*udev
,
3391 enum usb3_link_state state
, bool enable
)
3398 feature
= USB_DEVICE_U1_ENABLE
;
3401 feature
= USB_DEVICE_U2_ENABLE
;
3404 dev_warn(&udev
->dev
, "%s: Can't %s non-U1 or U2 state.\n",
3405 __func__
, enable
? "enable" : "disable");
3409 if (udev
->state
!= USB_STATE_CONFIGURED
) {
3410 dev_dbg(&udev
->dev
, "%s: Can't %s %s state "
3411 "for unconfigured device.\n",
3412 __func__
, enable
? "enable" : "disable",
3413 usb3_lpm_names
[state
]);
3419 * Now send the control transfer to enable device-initiated LPM
3420 * for either U1 or U2.
3422 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
3423 USB_REQ_SET_FEATURE
,
3427 USB_CTRL_SET_TIMEOUT
);
3429 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
3430 USB_REQ_CLEAR_FEATURE
,
3434 USB_CTRL_SET_TIMEOUT
);
3437 dev_warn(&udev
->dev
, "%s of device-initiated %s failed.\n",
3438 enable
? "Enable" : "Disable",
3439 usb3_lpm_names
[state
]);
3445 static int usb_set_lpm_timeout(struct usb_device
*udev
,
3446 enum usb3_link_state state
, int timeout
)
3453 feature
= USB_PORT_FEAT_U1_TIMEOUT
;
3456 feature
= USB_PORT_FEAT_U2_TIMEOUT
;
3459 dev_warn(&udev
->dev
, "%s: Can't set timeout for non-U1 or U2 state.\n",
3464 if (state
== USB3_LPM_U1
&& timeout
> USB3_LPM_U1_MAX_TIMEOUT
&&
3465 timeout
!= USB3_LPM_DEVICE_INITIATED
) {
3466 dev_warn(&udev
->dev
, "Failed to set %s timeout to 0x%x, "
3467 "which is a reserved value.\n",
3468 usb3_lpm_names
[state
], timeout
);
3472 ret
= set_port_feature(udev
->parent
,
3473 USB_PORT_LPM_TIMEOUT(timeout
) | udev
->portnum
,
3476 dev_warn(&udev
->dev
, "Failed to set %s timeout to 0x%x,"
3477 "error code %i\n", usb3_lpm_names
[state
],
3481 if (state
== USB3_LPM_U1
)
3482 udev
->u1_params
.timeout
= timeout
;
3484 udev
->u2_params
.timeout
= timeout
;
3489 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3492 * We will attempt to enable U1 or U2, but there are no guarantees that the
3493 * control transfers to set the hub timeout or enable device-initiated U1/U2
3494 * will be successful.
3496 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3497 * driver know about it. If that call fails, it should be harmless, and just
3498 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3500 static void usb_enable_link_state(struct usb_hcd
*hcd
, struct usb_device
*udev
,
3501 enum usb3_link_state state
)
3504 __u8 u1_mel
= udev
->bos
->ss_cap
->bU1devExitLat
;
3505 __le16 u2_mel
= udev
->bos
->ss_cap
->bU2DevExitLat
;
3507 /* If the device says it doesn't have *any* exit latency to come out of
3508 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3511 if ((state
== USB3_LPM_U1
&& u1_mel
== 0) ||
3512 (state
== USB3_LPM_U2
&& u2_mel
== 0))
3516 * First, let the device know about the exit latencies
3517 * associated with the link state we're about to enable.
3519 ret
= usb_req_set_sel(udev
, state
);
3521 dev_warn(&udev
->dev
, "Set SEL for device-initiated %s failed.\n",
3522 usb3_lpm_names
[state
]);
3526 /* We allow the host controller to set the U1/U2 timeout internally
3527 * first, so that it can change its schedule to account for the
3528 * additional latency to send data to a device in a lower power
3531 timeout
= hcd
->driver
->enable_usb3_lpm_timeout(hcd
, udev
, state
);
3533 /* xHCI host controller doesn't want to enable this LPM state. */
3538 dev_warn(&udev
->dev
, "Could not enable %s link state, "
3539 "xHCI error %i.\n", usb3_lpm_names
[state
],
3544 if (usb_set_lpm_timeout(udev
, state
, timeout
))
3545 /* If we can't set the parent hub U1/U2 timeout,
3546 * device-initiated LPM won't be allowed either, so let the xHCI
3547 * host know that this link state won't be enabled.
3549 hcd
->driver
->disable_usb3_lpm_timeout(hcd
, udev
, state
);
3551 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3552 else if (udev
->actconfig
)
3553 usb_set_device_initiated_lpm(udev
, state
, true);
3558 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3561 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3562 * If zero is returned, the parent will not allow the link to go into U1/U2.
3564 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3565 * it won't have an effect on the bus link state because the parent hub will
3566 * still disallow device-initiated U1/U2 entry.
3568 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3569 * possible. The result will be slightly more bus bandwidth will be taken up
3570 * (to account for U1/U2 exit latency), but it should be harmless.
3572 static int usb_disable_link_state(struct usb_hcd
*hcd
, struct usb_device
*udev
,
3573 enum usb3_link_state state
)
3579 feature
= USB_PORT_FEAT_U1_TIMEOUT
;
3582 feature
= USB_PORT_FEAT_U2_TIMEOUT
;
3585 dev_warn(&udev
->dev
, "%s: Can't disable non-U1 or U2 state.\n",
3590 if (usb_set_lpm_timeout(udev
, state
, 0))
3593 usb_set_device_initiated_lpm(udev
, state
, false);
3595 if (hcd
->driver
->disable_usb3_lpm_timeout(hcd
, udev
, state
))
3596 dev_warn(&udev
->dev
, "Could not disable xHCI %s timeout, "
3597 "bus schedule bandwidth may be impacted.\n",
3598 usb3_lpm_names
[state
]);
3603 * Disable hub-initiated and device-initiated U1 and U2 entry.
3604 * Caller must own the bandwidth_mutex.
3606 * This will call usb_enable_lpm() on failure, which will decrement
3607 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3609 int usb_disable_lpm(struct usb_device
*udev
)
3611 struct usb_hcd
*hcd
;
3613 if (!udev
|| !udev
->parent
||
3614 udev
->speed
!= USB_SPEED_SUPER
||
3618 hcd
= bus_to_hcd(udev
->bus
);
3619 if (!hcd
|| !hcd
->driver
->disable_usb3_lpm_timeout
)
3622 udev
->lpm_disable_count
++;
3623 if ((udev
->u1_params
.timeout
== 0 && udev
->u2_params
.timeout
== 0))
3626 /* If LPM is enabled, attempt to disable it. */
3627 if (usb_disable_link_state(hcd
, udev
, USB3_LPM_U1
))
3629 if (usb_disable_link_state(hcd
, udev
, USB3_LPM_U2
))
3635 usb_enable_lpm(udev
);
3638 EXPORT_SYMBOL_GPL(usb_disable_lpm
);
3640 /* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3641 int usb_unlocked_disable_lpm(struct usb_device
*udev
)
3643 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
3649 mutex_lock(hcd
->bandwidth_mutex
);
3650 ret
= usb_disable_lpm(udev
);
3651 mutex_unlock(hcd
->bandwidth_mutex
);
3655 EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm
);
3658 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3659 * xHCI host policy may prevent U1 or U2 from being enabled.
3661 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3662 * until the lpm_disable_count drops to zero. Caller must own the
3665 void usb_enable_lpm(struct usb_device
*udev
)
3667 struct usb_hcd
*hcd
;
3669 if (!udev
|| !udev
->parent
||
3670 udev
->speed
!= USB_SPEED_SUPER
||
3674 udev
->lpm_disable_count
--;
3675 hcd
= bus_to_hcd(udev
->bus
);
3676 /* Double check that we can both enable and disable LPM.
3677 * Device must be configured to accept set feature U1/U2 timeout.
3679 if (!hcd
|| !hcd
->driver
->enable_usb3_lpm_timeout
||
3680 !hcd
->driver
->disable_usb3_lpm_timeout
)
3683 if (udev
->lpm_disable_count
> 0)
3686 usb_enable_link_state(hcd
, udev
, USB3_LPM_U1
);
3687 usb_enable_link_state(hcd
, udev
, USB3_LPM_U2
);
3689 EXPORT_SYMBOL_GPL(usb_enable_lpm
);
3691 /* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3692 void usb_unlocked_enable_lpm(struct usb_device
*udev
)
3694 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
3699 mutex_lock(hcd
->bandwidth_mutex
);
3700 usb_enable_lpm(udev
);
3701 mutex_unlock(hcd
->bandwidth_mutex
);
3703 EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm
);
3706 #else /* CONFIG_PM */
3708 #define hub_suspend NULL
3709 #define hub_resume NULL
3710 #define hub_reset_resume NULL
3712 int usb_disable_lpm(struct usb_device
*udev
)
3716 EXPORT_SYMBOL_GPL(usb_disable_lpm
);
3718 void usb_enable_lpm(struct usb_device
*udev
) { }
3719 EXPORT_SYMBOL_GPL(usb_enable_lpm
);
3721 int usb_unlocked_disable_lpm(struct usb_device
*udev
)
3725 EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm
);
3727 void usb_unlocked_enable_lpm(struct usb_device
*udev
) { }
3728 EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm
);
3730 int usb_disable_ltm(struct usb_device
*udev
)
3734 EXPORT_SYMBOL_GPL(usb_disable_ltm
);
3736 void usb_enable_ltm(struct usb_device
*udev
) { }
3737 EXPORT_SYMBOL_GPL(usb_enable_ltm
);
3741 /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3743 * Between connect detection and reset signaling there must be a delay
3744 * of 100ms at least for debounce and power-settling. The corresponding
3745 * timer shall restart whenever the downstream port detects a disconnect.
3747 * Apparently there are some bluetooth and irda-dongles and a number of
3748 * low-speed devices for which this debounce period may last over a second.
3749 * Not covered by the spec - but easy to deal with.
3751 * This implementation uses a 1500ms total debounce timeout; if the
3752 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3753 * every 25ms for transient disconnects. When the port status has been
3754 * unchanged for 100ms it returns the port status.
3756 static int hub_port_debounce(struct usb_hub
*hub
, int port1
)
3759 int total_time
, stable_time
= 0;
3760 u16 portchange
, portstatus
;
3761 unsigned connection
= 0xffff;
3763 for (total_time
= 0; ; total_time
+= HUB_DEBOUNCE_STEP
) {
3764 ret
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
3768 if (!(portchange
& USB_PORT_STAT_C_CONNECTION
) &&
3769 (portstatus
& USB_PORT_STAT_CONNECTION
) == connection
) {
3770 stable_time
+= HUB_DEBOUNCE_STEP
;
3771 if (stable_time
>= HUB_DEBOUNCE_STABLE
)
3775 connection
= portstatus
& USB_PORT_STAT_CONNECTION
;
3778 if (portchange
& USB_PORT_STAT_C_CONNECTION
) {
3779 clear_port_feature(hub
->hdev
, port1
,
3780 USB_PORT_FEAT_C_CONNECTION
);
3783 if (total_time
>= HUB_DEBOUNCE_TIMEOUT
)
3785 msleep(HUB_DEBOUNCE_STEP
);
3788 dev_dbg (hub
->intfdev
,
3789 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3790 port1
, total_time
, stable_time
, portstatus
);
3792 if (stable_time
< HUB_DEBOUNCE_STABLE
)
3797 void usb_ep0_reinit(struct usb_device
*udev
)
3799 usb_disable_endpoint(udev
, 0 + USB_DIR_IN
, true);
3800 usb_disable_endpoint(udev
, 0 + USB_DIR_OUT
, true);
3801 usb_enable_endpoint(udev
, &udev
->ep0
, true);
3803 EXPORT_SYMBOL_GPL(usb_ep0_reinit
);
3805 #define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3806 #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3808 static int hub_set_address(struct usb_device
*udev
, int devnum
)
3811 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
3814 * The host controller will choose the device address,
3815 * instead of the core having chosen it earlier
3817 if (!hcd
->driver
->address_device
&& devnum
<= 1)
3819 if (udev
->state
== USB_STATE_ADDRESS
)
3821 if (udev
->state
!= USB_STATE_DEFAULT
)
3823 if (hcd
->driver
->address_device
)
3824 retval
= hcd
->driver
->address_device(hcd
, udev
);
3826 retval
= usb_control_msg(udev
, usb_sndaddr0pipe(),
3827 USB_REQ_SET_ADDRESS
, 0, devnum
, 0,
3828 NULL
, 0, USB_CTRL_SET_TIMEOUT
);
3830 update_devnum(udev
, devnum
);
3831 /* Device now using proper address. */
3832 usb_set_device_state(udev
, USB_STATE_ADDRESS
);
3833 usb_ep0_reinit(udev
);
3838 /* Reset device, (re)assign address, get device descriptor.
3839 * Device connection must be stable, no more debouncing needed.
3840 * Returns device in USB_STATE_ADDRESS, except on error.
3842 * If this is called for an already-existing device (as part of
3843 * usb_reset_and_verify_device), the caller must own the device lock. For a
3844 * newly detected device that is not accessible through any global
3845 * pointers, it's not necessary to lock the device.
3848 hub_port_init (struct usb_hub
*hub
, struct usb_device
*udev
, int port1
,
3851 static DEFINE_MUTEX(usb_address0_mutex
);
3853 struct usb_device
*hdev
= hub
->hdev
;
3854 struct usb_hcd
*hcd
= bus_to_hcd(hdev
->bus
);
3856 unsigned delay
= HUB_SHORT_RESET_TIME
;
3857 enum usb_device_speed oldspeed
= udev
->speed
;
3859 int devnum
= udev
->devnum
;
3861 /* root hub ports have a slightly longer reset period
3862 * (from USB 2.0 spec, section 7.1.7.5)
3864 if (!hdev
->parent
) {
3865 delay
= HUB_ROOT_RESET_TIME
;
3866 if (port1
== hdev
->bus
->otg_port
)
3867 hdev
->bus
->b_hnp_enable
= 0;
3870 /* Some low speed devices have problems with the quick delay, so */
3871 /* be a bit pessimistic with those devices. RHbug #23670 */
3872 if (oldspeed
== USB_SPEED_LOW
)
3873 delay
= HUB_LONG_RESET_TIME
;
3875 mutex_lock(&usb_address0_mutex
);
3877 /* Reset the device; full speed may morph to high speed */
3878 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
3879 retval
= hub_port_reset(hub
, port1
, udev
, delay
, false);
3880 if (retval
< 0) /* error or disconnect */
3882 /* success, speed is known */
3886 if (oldspeed
!= USB_SPEED_UNKNOWN
&& oldspeed
!= udev
->speed
) {
3887 dev_dbg(&udev
->dev
, "device reset changed speed!\n");
3890 oldspeed
= udev
->speed
;
3892 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3893 * it's fixed size except for full speed devices.
3894 * For Wireless USB devices, ep0 max packet is always 512 (tho
3895 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
3897 switch (udev
->speed
) {
3898 case USB_SPEED_SUPER
:
3899 case USB_SPEED_WIRELESS
: /* fixed at 512 */
3900 udev
->ep0
.desc
.wMaxPacketSize
= cpu_to_le16(512);
3902 case USB_SPEED_HIGH
: /* fixed at 64 */
3903 udev
->ep0
.desc
.wMaxPacketSize
= cpu_to_le16(64);
3905 case USB_SPEED_FULL
: /* 8, 16, 32, or 64 */
3906 /* to determine the ep0 maxpacket size, try to read
3907 * the device descriptor to get bMaxPacketSize0 and
3908 * then correct our initial guess.
3910 udev
->ep0
.desc
.wMaxPacketSize
= cpu_to_le16(64);
3912 case USB_SPEED_LOW
: /* fixed at 8 */
3913 udev
->ep0
.desc
.wMaxPacketSize
= cpu_to_le16(8);
3919 if (udev
->speed
== USB_SPEED_WIRELESS
)
3920 speed
= "variable speed Wireless";
3922 speed
= usb_speed_string(udev
->speed
);
3924 if (udev
->speed
!= USB_SPEED_SUPER
)
3925 dev_info(&udev
->dev
,
3926 "%s %s USB device number %d using %s\n",
3927 (udev
->config
) ? "reset" : "new", speed
,
3928 devnum
, udev
->bus
->controller
->driver
->name
);
3930 /* Set up TT records, if needed */
3932 udev
->tt
= hdev
->tt
;
3933 udev
->ttport
= hdev
->ttport
;
3934 } else if (udev
->speed
!= USB_SPEED_HIGH
3935 && hdev
->speed
== USB_SPEED_HIGH
) {
3937 dev_err(&udev
->dev
, "parent hub has no TT\n");
3941 udev
->tt
= &hub
->tt
;
3942 udev
->ttport
= port1
;
3945 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3946 * Because device hardware and firmware is sometimes buggy in
3947 * this area, and this is how Linux has done it for ages.
3948 * Change it cautiously.
3950 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3951 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3952 * so it may help with some non-standards-compliant devices.
3953 * Otherwise we start with SET_ADDRESS and then try to read the
3954 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3957 for (i
= 0; i
< GET_DESCRIPTOR_TRIES
; (++i
, msleep(100))) {
3958 if (USE_NEW_SCHEME(retry_counter
) && !(hcd
->driver
->flags
& HCD_USB3
)) {
3959 struct usb_device_descriptor
*buf
;
3962 #define GET_DESCRIPTOR_BUFSIZE 64
3963 buf
= kmalloc(GET_DESCRIPTOR_BUFSIZE
, GFP_NOIO
);
3969 /* Retry on all errors; some devices are flakey.
3970 * 255 is for WUSB devices, we actually need to use
3971 * 512 (WUSB1.0[4.8.1]).
3973 for (j
= 0; j
< 3; ++j
) {
3974 buf
->bMaxPacketSize0
= 0;
3975 r
= usb_control_msg(udev
, usb_rcvaddr0pipe(),
3976 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
,
3977 USB_DT_DEVICE
<< 8, 0,
3978 buf
, GET_DESCRIPTOR_BUFSIZE
,
3979 initial_descriptor_timeout
);
3980 switch (buf
->bMaxPacketSize0
) {
3981 case 8: case 16: case 32: case 64: case 255:
3982 if (buf
->bDescriptorType
==
3996 udev
->descriptor
.bMaxPacketSize0
=
3997 buf
->bMaxPacketSize0
;
4000 retval
= hub_port_reset(hub
, port1
, udev
, delay
, false);
4001 if (retval
< 0) /* error or disconnect */
4003 if (oldspeed
!= udev
->speed
) {
4005 "device reset changed speed!\n");
4011 "device descriptor read/64, error %d\n",
4016 #undef GET_DESCRIPTOR_BUFSIZE
4020 * If device is WUSB, we already assigned an
4021 * unauthorized address in the Connect Ack sequence;
4022 * authorization will assign the final address.
4024 if (udev
->wusb
== 0) {
4025 for (j
= 0; j
< SET_ADDRESS_TRIES
; ++j
) {
4026 retval
= hub_set_address(udev
, devnum
);
4033 "device not accepting address %d, error %d\n",
4037 if (udev
->speed
== USB_SPEED_SUPER
) {
4038 devnum
= udev
->devnum
;
4039 dev_info(&udev
->dev
,
4040 "%s SuperSpeed USB device number %d using %s\n",
4041 (udev
->config
) ? "reset" : "new",
4042 devnum
, udev
->bus
->controller
->driver
->name
);
4045 /* cope with hardware quirkiness:
4046 * - let SET_ADDRESS settle, some device hardware wants it
4047 * - read ep0 maxpacket even for high and low speed,
4050 if (USE_NEW_SCHEME(retry_counter
) && !(hcd
->driver
->flags
& HCD_USB3
))
4054 retval
= usb_get_device_descriptor(udev
, 8);
4057 "device descriptor read/8, error %d\n",
4069 if (hcd
->phy
&& !hdev
->parent
)
4070 usb_phy_notify_connect(hcd
->phy
, udev
->speed
);
4073 * Some superspeed devices have finished the link training process
4074 * and attached to a superspeed hub port, but the device descriptor
4075 * got from those devices show they aren't superspeed devices. Warm
4076 * reset the port attached by the devices can fix them.
4078 if ((udev
->speed
== USB_SPEED_SUPER
) &&
4079 (le16_to_cpu(udev
->descriptor
.bcdUSB
) < 0x0300)) {
4080 dev_err(&udev
->dev
, "got a wrong device descriptor, "
4081 "warm reset device\n");
4082 hub_port_reset(hub
, port1
, udev
,
4083 HUB_BH_RESET_TIME
, true);
4088 if (udev
->descriptor
.bMaxPacketSize0
== 0xff ||
4089 udev
->speed
== USB_SPEED_SUPER
)
4092 i
= udev
->descriptor
.bMaxPacketSize0
;
4093 if (usb_endpoint_maxp(&udev
->ep0
.desc
) != i
) {
4094 if (udev
->speed
== USB_SPEED_LOW
||
4095 !(i
== 8 || i
== 16 || i
== 32 || i
== 64)) {
4096 dev_err(&udev
->dev
, "Invalid ep0 maxpacket: %d\n", i
);
4100 if (udev
->speed
== USB_SPEED_FULL
)
4101 dev_dbg(&udev
->dev
, "ep0 maxpacket = %d\n", i
);
4103 dev_warn(&udev
->dev
, "Using ep0 maxpacket: %d\n", i
);
4104 udev
->ep0
.desc
.wMaxPacketSize
= cpu_to_le16(i
);
4105 usb_ep0_reinit(udev
);
4108 retval
= usb_get_device_descriptor(udev
, USB_DT_DEVICE_SIZE
);
4109 if (retval
< (signed)sizeof(udev
->descriptor
)) {
4110 dev_err(&udev
->dev
, "device descriptor read/all, error %d\n",
4117 if (udev
->wusb
== 0 && le16_to_cpu(udev
->descriptor
.bcdUSB
) >= 0x0201) {
4118 retval
= usb_get_bos_descriptor(udev
);
4120 udev
->lpm_capable
= usb_device_supports_lpm(udev
);
4121 usb_set_lpm_parameters(udev
);
4126 /* notify HCD that we have a device connected and addressed */
4127 if (hcd
->driver
->update_device
)
4128 hcd
->driver
->update_device(hcd
, udev
);
4131 hub_port_disable(hub
, port1
, 0);
4132 update_devnum(udev
, devnum
); /* for disconnect processing */
4134 mutex_unlock(&usb_address0_mutex
);
4139 check_highspeed (struct usb_hub
*hub
, struct usb_device
*udev
, int port1
)
4141 struct usb_qualifier_descriptor
*qual
;
4144 qual
= kmalloc (sizeof *qual
, GFP_KERNEL
);
4148 status
= usb_get_descriptor (udev
, USB_DT_DEVICE_QUALIFIER
, 0,
4149 qual
, sizeof *qual
);
4150 if (status
== sizeof *qual
) {
4151 dev_info(&udev
->dev
, "not running at top speed; "
4152 "connect to a high speed hub\n");
4153 /* hub LEDs are probably harder to miss than syslog */
4154 if (hub
->has_indicators
) {
4155 hub
->indicator
[port1
-1] = INDICATOR_GREEN_BLINK
;
4156 schedule_delayed_work (&hub
->leds
, 0);
4163 hub_power_remaining (struct usb_hub
*hub
)
4165 struct usb_device
*hdev
= hub
->hdev
;
4169 if (!hub
->limited_power
)
4172 remaining
= hdev
->bus_mA
- hub
->descriptor
->bHubContrCurrent
;
4173 for (port1
= 1; port1
<= hdev
->maxchild
; ++port1
) {
4174 struct usb_device
*udev
= hub
->ports
[port1
- 1]->child
;
4180 if (hub_is_superspeed(udev
))
4186 * Unconfigured devices may not use more than one unit load,
4187 * or 8mA for OTG ports
4189 if (udev
->actconfig
)
4190 delta
= usb_get_max_power(udev
, udev
->actconfig
);
4191 else if (port1
!= udev
->bus
->otg_port
|| hdev
->parent
)
4195 if (delta
> hub
->mA_per_port
)
4196 dev_warn(&udev
->dev
,
4197 "%dmA is over %umA budget for port %d!\n",
4198 delta
, hub
->mA_per_port
, port1
);
4201 if (remaining
< 0) {
4202 dev_warn(hub
->intfdev
, "%dmA over power budget!\n",
4209 /* Handle physical or logical connection change events.
4210 * This routine is called when:
4211 * a port connection-change occurs;
4212 * a port enable-change occurs (often caused by EMI);
4213 * usb_reset_and_verify_device() encounters changed descriptors (as from
4214 * a firmware download)
4215 * caller already locked the hub
4217 static void hub_port_connect_change(struct usb_hub
*hub
, int port1
,
4218 u16 portstatus
, u16 portchange
)
4220 struct usb_device
*hdev
= hub
->hdev
;
4221 struct device
*hub_dev
= hub
->intfdev
;
4222 struct usb_hcd
*hcd
= bus_to_hcd(hdev
->bus
);
4223 unsigned wHubCharacteristics
=
4224 le16_to_cpu(hub
->descriptor
->wHubCharacteristics
);
4225 struct usb_device
*udev
;
4230 "port %d, status %04x, change %04x, %s\n",
4231 port1
, portstatus
, portchange
, portspeed(hub
, portstatus
));
4233 if (hub
->has_indicators
) {
4234 set_port_led(hub
, port1
, HUB_LED_AUTO
);
4235 hub
->indicator
[port1
-1] = INDICATOR_AUTO
;
4238 #ifdef CONFIG_USB_OTG
4239 /* during HNP, don't repeat the debounce */
4240 if (hdev
->bus
->is_b_host
)
4241 portchange
&= ~(USB_PORT_STAT_C_CONNECTION
|
4242 USB_PORT_STAT_C_ENABLE
);
4245 /* Try to resuscitate an existing device */
4246 udev
= hub
->ports
[port1
- 1]->child
;
4247 if ((portstatus
& USB_PORT_STAT_CONNECTION
) && udev
&&
4248 udev
->state
!= USB_STATE_NOTATTACHED
) {
4249 usb_lock_device(udev
);
4250 if (portstatus
& USB_PORT_STAT_ENABLE
) {
4251 status
= 0; /* Nothing to do */
4253 #ifdef CONFIG_USB_SUSPEND
4254 } else if (udev
->state
== USB_STATE_SUSPENDED
&&
4255 udev
->persist_enabled
) {
4256 /* For a suspended device, treat this as a
4257 * remote wakeup event.
4259 status
= usb_remote_wakeup(udev
);
4263 status
= -ENODEV
; /* Don't resuscitate */
4265 usb_unlock_device(udev
);
4268 clear_bit(port1
, hub
->change_bits
);
4273 /* Disconnect any existing devices under this port */
4275 if (hcd
->phy
&& !hdev
->parent
&&
4276 !(portstatus
& USB_PORT_STAT_CONNECTION
))
4277 usb_phy_notify_disconnect(hcd
->phy
, udev
->speed
);
4278 usb_disconnect(&hub
->ports
[port1
- 1]->child
);
4280 clear_bit(port1
, hub
->change_bits
);
4282 /* We can forget about a "removed" device when there's a physical
4283 * disconnect or the connect status changes.
4285 if (!(portstatus
& USB_PORT_STAT_CONNECTION
) ||
4286 (portchange
& USB_PORT_STAT_C_CONNECTION
))
4287 clear_bit(port1
, hub
->removed_bits
);
4289 if (portchange
& (USB_PORT_STAT_C_CONNECTION
|
4290 USB_PORT_STAT_C_ENABLE
)) {
4291 status
= hub_port_debounce(hub
, port1
);
4293 if (printk_ratelimit())
4294 dev_err(hub_dev
, "connect-debounce failed, "
4295 "port %d disabled\n", port1
);
4296 portstatus
&= ~USB_PORT_STAT_CONNECTION
;
4298 portstatus
= status
;
4302 /* Return now if debouncing failed or nothing is connected or
4303 * the device was "removed".
4305 if (!(portstatus
& USB_PORT_STAT_CONNECTION
) ||
4306 test_bit(port1
, hub
->removed_bits
)) {
4308 /* maybe switch power back on (e.g. root hub was reset) */
4309 if ((wHubCharacteristics
& HUB_CHAR_LPSM
) < 2
4310 && !port_is_power_on(hub
, portstatus
))
4311 set_port_feature(hdev
, port1
, USB_PORT_FEAT_POWER
);
4313 if (portstatus
& USB_PORT_STAT_ENABLE
)
4317 if (hub_is_superspeed(hub
->hdev
))
4322 for (i
= 0; i
< SET_CONFIG_TRIES
; i
++) {
4324 /* reallocate for each attempt, since references
4325 * to the previous one can escape in various ways
4327 udev
= usb_alloc_dev(hdev
, hdev
->bus
, port1
);
4330 "couldn't allocate port %d usb_device\n",
4335 usb_set_device_state(udev
, USB_STATE_POWERED
);
4336 udev
->bus_mA
= hub
->mA_per_port
;
4337 udev
->level
= hdev
->level
+ 1;
4338 udev
->wusb
= hub_is_wusb(hub
);
4340 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4341 if (hub_is_superspeed(hub
->hdev
))
4342 udev
->speed
= USB_SPEED_SUPER
;
4344 udev
->speed
= USB_SPEED_UNKNOWN
;
4346 choose_devnum(udev
);
4347 if (udev
->devnum
<= 0) {
4348 status
= -ENOTCONN
; /* Don't retry */
4352 /* reset (non-USB 3.0 devices) and get descriptor */
4353 status
= hub_port_init(hub
, udev
, port1
, i
);
4357 usb_detect_quirks(udev
);
4358 if (udev
->quirks
& USB_QUIRK_DELAY_INIT
)
4361 /* consecutive bus-powered hubs aren't reliable; they can
4362 * violate the voltage drop budget. if the new child has
4363 * a "powered" LED, users should notice we didn't enable it
4364 * (without reading syslog), even without per-port LEDs
4367 if (udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
4368 && udev
->bus_mA
<= unit_load
) {
4371 status
= usb_get_status(udev
, USB_RECIP_DEVICE
, 0,
4374 dev_dbg(&udev
->dev
, "get status %d ?\n", status
);
4377 le16_to_cpus(&devstat
);
4378 if ((devstat
& (1 << USB_DEVICE_SELF_POWERED
)) == 0) {
4380 "can't connect bus-powered hub "
4382 if (hub
->has_indicators
) {
4383 hub
->indicator
[port1
-1] =
4384 INDICATOR_AMBER_BLINK
;
4385 schedule_delayed_work (&hub
->leds
, 0);
4387 status
= -ENOTCONN
; /* Don't retry */
4392 /* check for devices running slower than they could */
4393 if (le16_to_cpu(udev
->descriptor
.bcdUSB
) >= 0x0200
4394 && udev
->speed
== USB_SPEED_FULL
4395 && highspeed_hubs
!= 0)
4396 check_highspeed (hub
, udev
, port1
);
4398 /* Store the parent's children[] pointer. At this point
4399 * udev becomes globally accessible, although presumably
4400 * no one will look at it until hdev is unlocked.
4404 /* We mustn't add new devices if the parent hub has
4405 * been disconnected; we would race with the
4406 * recursively_mark_NOTATTACHED() routine.
4408 spin_lock_irq(&device_state_lock
);
4409 if (hdev
->state
== USB_STATE_NOTATTACHED
)
4412 hub
->ports
[port1
- 1]->child
= udev
;
4413 spin_unlock_irq(&device_state_lock
);
4415 /* Run it through the hoops (find a driver, etc) */
4417 status
= usb_new_device(udev
);
4419 spin_lock_irq(&device_state_lock
);
4420 hub
->ports
[port1
- 1]->child
= NULL
;
4421 spin_unlock_irq(&device_state_lock
);
4428 status
= hub_power_remaining(hub
);
4430 dev_dbg(hub_dev
, "%dmA power budget left\n", status
);
4435 hub_port_disable(hub
, port1
, 1);
4437 usb_ep0_reinit(udev
);
4438 release_devnum(udev
);
4441 if ((status
== -ENOTCONN
) || (status
== -ENOTSUPP
))
4444 if (hub
->hdev
->parent
||
4445 !hcd
->driver
->port_handed_over
||
4446 !(hcd
->driver
->port_handed_over
)(hcd
, port1
))
4447 dev_err(hub_dev
, "unable to enumerate USB device on port %d\n",
4451 hub_port_disable(hub
, port1
, 1);
4452 if (hcd
->driver
->relinquish_port
&& !hub
->hdev
->parent
)
4453 hcd
->driver
->relinquish_port(hcd
, port1
);
4456 /* Returns 1 if there was a remote wakeup and a connect status change. */
4457 static int hub_handle_remote_wakeup(struct usb_hub
*hub
, unsigned int port
,
4458 u16 portstatus
, u16 portchange
)
4460 struct usb_device
*hdev
;
4461 struct usb_device
*udev
;
4462 int connect_change
= 0;
4466 udev
= hub
->ports
[port
- 1]->child
;
4467 if (!hub_is_superspeed(hdev
)) {
4468 if (!(portchange
& USB_PORT_STAT_C_SUSPEND
))
4470 clear_port_feature(hdev
, port
, USB_PORT_FEAT_C_SUSPEND
);
4472 if (!udev
|| udev
->state
!= USB_STATE_SUSPENDED
||
4473 (portstatus
& USB_PORT_STAT_LINK_STATE
) !=
4479 /* TRSMRCY = 10 msec */
4482 usb_lock_device(udev
);
4483 ret
= usb_remote_wakeup(udev
);
4484 usb_unlock_device(udev
);
4489 hub_port_disable(hub
, port
, 1);
4491 dev_dbg(hub
->intfdev
, "resume on port %d, status %d\n",
4493 return connect_change
;
4496 static void hub_events(void)
4498 struct list_head
*tmp
;
4499 struct usb_device
*hdev
;
4500 struct usb_interface
*intf
;
4501 struct usb_hub
*hub
;
4502 struct device
*hub_dev
;
4508 int connect_change
, wakeup_change
;
4511 * We restart the list every time to avoid a deadlock with
4512 * deleting hubs downstream from this one. This should be
4513 * safe since we delete the hub from the event list.
4514 * Not the most efficient, but avoids deadlocks.
4518 /* Grab the first entry at the beginning of the list */
4519 spin_lock_irq(&hub_event_lock
);
4520 if (list_empty(&hub_event_list
)) {
4521 spin_unlock_irq(&hub_event_lock
);
4525 tmp
= hub_event_list
.next
;
4528 hub
= list_entry(tmp
, struct usb_hub
, event_list
);
4529 kref_get(&hub
->kref
);
4530 spin_unlock_irq(&hub_event_lock
);
4533 hub_dev
= hub
->intfdev
;
4534 intf
= to_usb_interface(hub_dev
);
4535 dev_dbg(hub_dev
, "state %d ports %d chg %04x evt %04x\n",
4536 hdev
->state
, hub
->descriptor
4537 ? hub
->descriptor
->bNbrPorts
4539 /* NOTE: expects max 15 ports... */
4540 (u16
) hub
->change_bits
[0],
4541 (u16
) hub
->event_bits
[0]);
4543 /* Lock the device, then check to see if we were
4544 * disconnected while waiting for the lock to succeed. */
4545 usb_lock_device(hdev
);
4546 if (unlikely(hub
->disconnected
))
4547 goto loop_disconnected
;
4549 /* If the hub has died, clean up after it */
4550 if (hdev
->state
== USB_STATE_NOTATTACHED
) {
4551 hub
->error
= -ENODEV
;
4552 hub_quiesce(hub
, HUB_DISCONNECT
);
4557 ret
= usb_autopm_get_interface(intf
);
4559 dev_dbg(hub_dev
, "Can't autoresume: %d\n", ret
);
4563 /* If this is an inactive hub, do nothing */
4568 dev_dbg (hub_dev
, "resetting for error %d\n",
4571 ret
= usb_reset_device(hdev
);
4574 "error resetting hub: %d\n", ret
);
4582 /* deal with port status changes */
4583 for (i
= 1; i
<= hub
->descriptor
->bNbrPorts
; i
++) {
4584 if (test_bit(i
, hub
->busy_bits
))
4586 connect_change
= test_bit(i
, hub
->change_bits
);
4587 wakeup_change
= test_and_clear_bit(i
, hub
->wakeup_bits
);
4588 if (!test_and_clear_bit(i
, hub
->event_bits
) &&
4589 !connect_change
&& !wakeup_change
)
4592 ret
= hub_port_status(hub
, i
,
4593 &portstatus
, &portchange
);
4597 if (portchange
& USB_PORT_STAT_C_CONNECTION
) {
4598 clear_port_feature(hdev
, i
,
4599 USB_PORT_FEAT_C_CONNECTION
);
4603 if (portchange
& USB_PORT_STAT_C_ENABLE
) {
4604 if (!connect_change
)
4606 "port %d enable change, "
4609 clear_port_feature(hdev
, i
,
4610 USB_PORT_FEAT_C_ENABLE
);
4613 * EM interference sometimes causes badly
4614 * shielded USB devices to be shutdown by
4615 * the hub, this hack enables them again.
4616 * Works at least with mouse driver.
4618 if (!(portstatus
& USB_PORT_STAT_ENABLE
)
4620 && hub
->ports
[i
- 1]->child
) {
4623 "disabled by hub (EMI?), "
4630 if (hub_handle_remote_wakeup(hub
, i
,
4631 portstatus
, portchange
))
4634 if (portchange
& USB_PORT_STAT_C_OVERCURRENT
) {
4638 dev_dbg(hub_dev
, "over-current change on port "
4640 clear_port_feature(hdev
, i
,
4641 USB_PORT_FEAT_C_OVER_CURRENT
);
4642 msleep(100); /* Cool down */
4643 hub_power_on(hub
, true);
4644 hub_port_status(hub
, i
, &status
, &unused
);
4645 if (status
& USB_PORT_STAT_OVERCURRENT
)
4646 dev_err(hub_dev
, "over-current "
4647 "condition on port %d\n", i
);
4650 if (portchange
& USB_PORT_STAT_C_RESET
) {
4652 "reset change on port %d\n",
4654 clear_port_feature(hdev
, i
,
4655 USB_PORT_FEAT_C_RESET
);
4657 if ((portchange
& USB_PORT_STAT_C_BH_RESET
) &&
4658 hub_is_superspeed(hub
->hdev
)) {
4660 "warm reset change on port %d\n",
4662 clear_port_feature(hdev
, i
,
4663 USB_PORT_FEAT_C_BH_PORT_RESET
);
4665 if (portchange
& USB_PORT_STAT_C_LINK_STATE
) {
4666 clear_port_feature(hub
->hdev
, i
,
4667 USB_PORT_FEAT_C_PORT_LINK_STATE
);
4669 if (portchange
& USB_PORT_STAT_C_CONFIG_ERROR
) {
4671 "config error on port %d\n",
4673 clear_port_feature(hub
->hdev
, i
,
4674 USB_PORT_FEAT_C_PORT_CONFIG_ERROR
);
4677 /* Warm reset a USB3 protocol port if it's in
4678 * SS.Inactive state.
4680 if (hub_port_warm_reset_required(hub
, portstatus
)) {
4682 struct usb_device
*udev
=
4683 hub
->ports
[i
- 1]->child
;
4685 dev_dbg(hub_dev
, "warm reset port %d\n", i
);
4687 status
= hub_port_reset(hub
, i
,
4688 NULL
, HUB_BH_RESET_TIME
,
4691 hub_port_disable(hub
, i
, 1);
4693 usb_lock_device(udev
);
4694 status
= usb_reset_device(udev
);
4695 usb_unlock_device(udev
);
4701 hub_port_connect_change(hub
, i
,
4702 portstatus
, portchange
);
4705 /* deal with hub status changes */
4706 if (test_and_clear_bit(0, hub
->event_bits
) == 0)
4708 else if (hub_hub_status(hub
, &hubstatus
, &hubchange
) < 0)
4709 dev_err (hub_dev
, "get_hub_status failed\n");
4711 if (hubchange
& HUB_CHANGE_LOCAL_POWER
) {
4712 dev_dbg (hub_dev
, "power change\n");
4713 clear_hub_feature(hdev
, C_HUB_LOCAL_POWER
);
4714 if (hubstatus
& HUB_STATUS_LOCAL_POWER
)
4715 /* FIXME: Is this always true? */
4716 hub
->limited_power
= 1;
4718 hub
->limited_power
= 0;
4720 if (hubchange
& HUB_CHANGE_OVERCURRENT
) {
4724 dev_dbg(hub_dev
, "over-current change\n");
4725 clear_hub_feature(hdev
, C_HUB_OVER_CURRENT
);
4726 msleep(500); /* Cool down */
4727 hub_power_on(hub
, true);
4728 hub_hub_status(hub
, &status
, &unused
);
4729 if (status
& HUB_STATUS_OVERCURRENT
)
4730 dev_err(hub_dev
, "over-current "
4736 /* Balance the usb_autopm_get_interface() above */
4737 usb_autopm_put_interface_no_suspend(intf
);
4739 /* Balance the usb_autopm_get_interface_no_resume() in
4740 * kick_khubd() and allow autosuspend.
4742 usb_autopm_put_interface(intf
);
4744 usb_unlock_device(hdev
);
4745 kref_put(&hub
->kref
, hub_release
);
4747 } /* end while (1) */
4750 static int hub_thread(void *__unused
)
4752 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4753 * port handover. Otherwise it might see that a full-speed device
4754 * was gone before the EHCI controller had handed its port over to
4755 * the companion full-speed controller.
4761 wait_event_freezable(khubd_wait
,
4762 !list_empty(&hub_event_list
) ||
4763 kthread_should_stop());
4764 } while (!kthread_should_stop() || !list_empty(&hub_event_list
));
4766 pr_debug("%s: khubd exiting\n", usbcore_name
);
4770 static const struct usb_device_id hub_id_table
[] = {
4771 { .match_flags
= USB_DEVICE_ID_MATCH_VENDOR
4772 | USB_DEVICE_ID_MATCH_INT_CLASS
,
4773 .idVendor
= USB_VENDOR_GENESYS_LOGIC
,
4774 .bInterfaceClass
= USB_CLASS_HUB
,
4775 .driver_info
= HUB_QUIRK_CHECK_PORT_AUTOSUSPEND
},
4776 { .match_flags
= USB_DEVICE_ID_MATCH_DEV_CLASS
,
4777 .bDeviceClass
= USB_CLASS_HUB
},
4778 { .match_flags
= USB_DEVICE_ID_MATCH_INT_CLASS
,
4779 .bInterfaceClass
= USB_CLASS_HUB
},
4780 { } /* Terminating entry */
4783 MODULE_DEVICE_TABLE (usb
, hub_id_table
);
4785 static struct usb_driver hub_driver
= {
4788 .disconnect
= hub_disconnect
,
4789 .suspend
= hub_suspend
,
4790 .resume
= hub_resume
,
4791 .reset_resume
= hub_reset_resume
,
4792 .pre_reset
= hub_pre_reset
,
4793 .post_reset
= hub_post_reset
,
4794 .unlocked_ioctl
= hub_ioctl
,
4795 .id_table
= hub_id_table
,
4796 .supports_autosuspend
= 1,
4799 int usb_hub_init(void)
4801 if (usb_register(&hub_driver
) < 0) {
4802 printk(KERN_ERR
"%s: can't register hub driver\n",
4807 khubd_task
= kthread_run(hub_thread
, NULL
, "khubd");
4808 if (!IS_ERR(khubd_task
))
4811 /* Fall through if kernel_thread failed */
4812 usb_deregister(&hub_driver
);
4813 printk(KERN_ERR
"%s: can't start khubd\n", usbcore_name
);
4818 void usb_hub_cleanup(void)
4820 kthread_stop(khubd_task
);
4823 * Hub resources are freed for us by usb_deregister. It calls
4824 * usb_driver_purge on every device which in turn calls that
4825 * devices disconnect function if it is using this driver.
4826 * The hub_disconnect function takes care of releasing the
4827 * individual hub resources. -greg
4829 usb_deregister(&hub_driver
);
4830 } /* usb_hub_cleanup() */
4832 static int descriptors_changed(struct usb_device
*udev
,
4833 struct usb_device_descriptor
*old_device_descriptor
)
4837 unsigned serial_len
= 0;
4839 unsigned old_length
;
4843 if (memcmp(&udev
->descriptor
, old_device_descriptor
,
4844 sizeof(*old_device_descriptor
)) != 0)
4847 /* Since the idVendor, idProduct, and bcdDevice values in the
4848 * device descriptor haven't changed, we will assume the
4849 * Manufacturer and Product strings haven't changed either.
4850 * But the SerialNumber string could be different (e.g., a
4851 * different flash card of the same brand).
4854 serial_len
= strlen(udev
->serial
) + 1;
4857 for (index
= 0; index
< udev
->descriptor
.bNumConfigurations
; index
++) {
4858 old_length
= le16_to_cpu(udev
->config
[index
].desc
.wTotalLength
);
4859 len
= max(len
, old_length
);
4862 buf
= kmalloc(len
, GFP_NOIO
);
4864 dev_err(&udev
->dev
, "no mem to re-read configs after reset\n");
4865 /* assume the worst */
4868 for (index
= 0; index
< udev
->descriptor
.bNumConfigurations
; index
++) {
4869 old_length
= le16_to_cpu(udev
->config
[index
].desc
.wTotalLength
);
4870 length
= usb_get_descriptor(udev
, USB_DT_CONFIG
, index
, buf
,
4872 if (length
!= old_length
) {
4873 dev_dbg(&udev
->dev
, "config index %d, error %d\n",
4878 if (memcmp (buf
, udev
->rawdescriptors
[index
], old_length
)
4880 dev_dbg(&udev
->dev
, "config index %d changed (#%d)\n",
4882 ((struct usb_config_descriptor
*) buf
)->
4883 bConfigurationValue
);
4889 if (!changed
&& serial_len
) {
4890 length
= usb_string(udev
, udev
->descriptor
.iSerialNumber
,
4892 if (length
+ 1 != serial_len
) {
4893 dev_dbg(&udev
->dev
, "serial string error %d\n",
4896 } else if (memcmp(buf
, udev
->serial
, length
) != 0) {
4897 dev_dbg(&udev
->dev
, "serial string changed\n");
4907 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
4908 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4910 * WARNING - don't use this routine to reset a composite device
4911 * (one with multiple interfaces owned by separate drivers)!
4912 * Use usb_reset_device() instead.
4914 * Do a port reset, reassign the device's address, and establish its
4915 * former operating configuration. If the reset fails, or the device's
4916 * descriptors change from their values before the reset, or the original
4917 * configuration and altsettings cannot be restored, a flag will be set
4918 * telling khubd to pretend the device has been disconnected and then
4919 * re-connected. All drivers will be unbound, and the device will be
4920 * re-enumerated and probed all over again.
4922 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4923 * flagged for logical disconnection, or some other negative error code
4924 * if the reset wasn't even attempted.
4926 * The caller must own the device lock. For example, it's safe to use
4927 * this from a driver probe() routine after downloading new firmware.
4928 * For calls that might not occur during probe(), drivers should lock
4929 * the device using usb_lock_device_for_reset().
4931 * Locking exception: This routine may also be called from within an
4932 * autoresume handler. Such usage won't conflict with other tasks
4933 * holding the device lock because these tasks should always call
4934 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
4936 static int usb_reset_and_verify_device(struct usb_device
*udev
)
4938 struct usb_device
*parent_hdev
= udev
->parent
;
4939 struct usb_hub
*parent_hub
;
4940 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
4941 struct usb_device_descriptor descriptor
= udev
->descriptor
;
4943 int port1
= udev
->portnum
;
4945 if (udev
->state
== USB_STATE_NOTATTACHED
||
4946 udev
->state
== USB_STATE_SUSPENDED
) {
4947 dev_dbg(&udev
->dev
, "device reset not allowed in state %d\n",
4953 /* this requires hcd-specific logic; see ohci_restart() */
4954 dev_dbg(&udev
->dev
, "%s for root hub!\n", __func__
);
4957 parent_hub
= hdev_to_hub(parent_hdev
);
4959 /* Disable LPM and LTM while we reset the device and reinstall the alt
4960 * settings. Device-initiated LPM settings, and system exit latency
4961 * settings are cleared when the device is reset, so we have to set
4964 ret
= usb_unlocked_disable_lpm(udev
);
4966 dev_err(&udev
->dev
, "%s Failed to disable LPM\n.", __func__
);
4969 ret
= usb_disable_ltm(udev
);
4971 dev_err(&udev
->dev
, "%s Failed to disable LTM\n.",
4976 set_bit(port1
, parent_hub
->busy_bits
);
4977 for (i
= 0; i
< SET_CONFIG_TRIES
; ++i
) {
4979 /* ep0 maxpacket size may change; let the HCD know about it.
4980 * Other endpoints will be handled by re-enumeration. */
4981 usb_ep0_reinit(udev
);
4982 ret
= hub_port_init(parent_hub
, udev
, port1
, i
);
4983 if (ret
>= 0 || ret
== -ENOTCONN
|| ret
== -ENODEV
)
4986 clear_bit(port1
, parent_hub
->busy_bits
);
4991 /* Device might have changed firmware (DFU or similar) */
4992 if (descriptors_changed(udev
, &descriptor
)) {
4993 dev_info(&udev
->dev
, "device firmware changed\n");
4994 udev
->descriptor
= descriptor
; /* for disconnect() calls */
4998 /* Restore the device's previous configuration */
4999 if (!udev
->actconfig
)
5002 mutex_lock(hcd
->bandwidth_mutex
);
5003 ret
= usb_hcd_alloc_bandwidth(udev
, udev
->actconfig
, NULL
, NULL
);
5005 dev_warn(&udev
->dev
,
5006 "Busted HC? Not enough HCD resources for "
5007 "old configuration.\n");
5008 mutex_unlock(hcd
->bandwidth_mutex
);
5011 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
5012 USB_REQ_SET_CONFIGURATION
, 0,
5013 udev
->actconfig
->desc
.bConfigurationValue
, 0,
5014 NULL
, 0, USB_CTRL_SET_TIMEOUT
);
5017 "can't restore configuration #%d (error=%d)\n",
5018 udev
->actconfig
->desc
.bConfigurationValue
, ret
);
5019 mutex_unlock(hcd
->bandwidth_mutex
);
5022 mutex_unlock(hcd
->bandwidth_mutex
);
5023 usb_set_device_state(udev
, USB_STATE_CONFIGURED
);
5025 /* Put interfaces back into the same altsettings as before.
5026 * Don't bother to send the Set-Interface request for interfaces
5027 * that were already in altsetting 0; besides being unnecessary,
5028 * many devices can't handle it. Instead just reset the host-side
5031 for (i
= 0; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
5032 struct usb_host_config
*config
= udev
->actconfig
;
5033 struct usb_interface
*intf
= config
->interface
[i
];
5034 struct usb_interface_descriptor
*desc
;
5036 desc
= &intf
->cur_altsetting
->desc
;
5037 if (desc
->bAlternateSetting
== 0) {
5038 usb_disable_interface(udev
, intf
, true);
5039 usb_enable_interface(udev
, intf
, true);
5042 /* Let the bandwidth allocation function know that this
5043 * device has been reset, and it will have to use
5044 * alternate setting 0 as the current alternate setting.
5046 intf
->resetting_device
= 1;
5047 ret
= usb_set_interface(udev
, desc
->bInterfaceNumber
,
5048 desc
->bAlternateSetting
);
5049 intf
->resetting_device
= 0;
5052 dev_err(&udev
->dev
, "failed to restore interface %d "
5053 "altsetting %d (error=%d)\n",
5054 desc
->bInterfaceNumber
,
5055 desc
->bAlternateSetting
,
5062 /* Now that the alt settings are re-installed, enable LTM and LPM. */
5063 usb_unlocked_enable_lpm(udev
);
5064 usb_enable_ltm(udev
);
5068 /* LPM state doesn't matter when we're about to destroy the device. */
5069 hub_port_logical_disconnect(parent_hub
, port1
);
5074 * usb_reset_device - warn interface drivers and perform a USB port reset
5075 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5077 * Warns all drivers bound to registered interfaces (using their pre_reset
5078 * method), performs the port reset, and then lets the drivers know that
5079 * the reset is over (using their post_reset method).
5081 * Return value is the same as for usb_reset_and_verify_device().
5083 * The caller must own the device lock. For example, it's safe to use
5084 * this from a driver probe() routine after downloading new firmware.
5085 * For calls that might not occur during probe(), drivers should lock
5086 * the device using usb_lock_device_for_reset().
5088 * If an interface is currently being probed or disconnected, we assume
5089 * its driver knows how to handle resets. For all other interfaces,
5090 * if the driver doesn't have pre_reset and post_reset methods then
5091 * we attempt to unbind it and rebind afterward.
5093 int usb_reset_device(struct usb_device
*udev
)
5097 struct usb_host_config
*config
= udev
->actconfig
;
5099 if (udev
->state
== USB_STATE_NOTATTACHED
||
5100 udev
->state
== USB_STATE_SUSPENDED
) {
5101 dev_dbg(&udev
->dev
, "device reset not allowed in state %d\n",
5106 /* Prevent autosuspend during the reset */
5107 usb_autoresume_device(udev
);
5110 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
5111 struct usb_interface
*cintf
= config
->interface
[i
];
5112 struct usb_driver
*drv
;
5115 if (cintf
->dev
.driver
) {
5116 drv
= to_usb_driver(cintf
->dev
.driver
);
5117 if (drv
->pre_reset
&& drv
->post_reset
)
5118 unbind
= (drv
->pre_reset
)(cintf
);
5119 else if (cintf
->condition
==
5120 USB_INTERFACE_BOUND
)
5123 usb_forced_unbind_intf(cintf
);
5128 ret
= usb_reset_and_verify_device(udev
);
5131 for (i
= config
->desc
.bNumInterfaces
- 1; i
>= 0; --i
) {
5132 struct usb_interface
*cintf
= config
->interface
[i
];
5133 struct usb_driver
*drv
;
5134 int rebind
= cintf
->needs_binding
;
5136 if (!rebind
&& cintf
->dev
.driver
) {
5137 drv
= to_usb_driver(cintf
->dev
.driver
);
5138 if (drv
->post_reset
)
5139 rebind
= (drv
->post_reset
)(cintf
);
5140 else if (cintf
->condition
==
5141 USB_INTERFACE_BOUND
)
5144 if (ret
== 0 && rebind
)
5145 usb_rebind_intf(cintf
);
5149 usb_autosuspend_device(udev
);
5152 EXPORT_SYMBOL_GPL(usb_reset_device
);
5156 * usb_queue_reset_device - Reset a USB device from an atomic context
5157 * @iface: USB interface belonging to the device to reset
5159 * This function can be used to reset a USB device from an atomic
5160 * context, where usb_reset_device() won't work (as it blocks).
5162 * Doing a reset via this method is functionally equivalent to calling
5163 * usb_reset_device(), except for the fact that it is delayed to a
5164 * workqueue. This means that any drivers bound to other interfaces
5165 * might be unbound, as well as users from usbfs in user space.
5169 * - Scheduling two resets at the same time from two different drivers
5170 * attached to two different interfaces of the same device is
5171 * possible; depending on how the driver attached to each interface
5172 * handles ->pre_reset(), the second reset might happen or not.
5174 * - If a driver is unbound and it had a pending reset, the reset will
5177 * - This function can be called during .probe() or .disconnect()
5178 * times. On return from .disconnect(), any pending resets will be
5181 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5184 * NOTE: We don't do any reference count tracking because it is not
5185 * needed. The lifecycle of the work_struct is tied to the
5186 * usb_interface. Before destroying the interface we cancel the
5187 * work_struct, so the fact that work_struct is queued and or
5188 * running means the interface (and thus, the device) exist and
5191 void usb_queue_reset_device(struct usb_interface
*iface
)
5193 schedule_work(&iface
->reset_ws
);
5195 EXPORT_SYMBOL_GPL(usb_queue_reset_device
);
5198 * usb_hub_find_child - Get the pointer of child device
5199 * attached to the port which is specified by @port1.
5200 * @hdev: USB device belonging to the usb hub
5201 * @port1: port num to indicate which port the child device
5204 * USB drivers call this function to get hub's child device
5207 * Return NULL if input param is invalid and
5208 * child's usb_device pointer if non-NULL.
5210 struct usb_device
*usb_hub_find_child(struct usb_device
*hdev
,
5213 struct usb_hub
*hub
= hdev_to_hub(hdev
);
5215 if (port1
< 1 || port1
> hdev
->maxchild
)
5217 return hub
->ports
[port1
- 1]->child
;
5219 EXPORT_SYMBOL_GPL(usb_hub_find_child
);
5222 * usb_set_hub_port_connect_type - set hub port connect type.
5223 * @hdev: USB device belonging to the usb hub
5224 * @port1: port num of the port
5225 * @type: connect type of the port
5227 void usb_set_hub_port_connect_type(struct usb_device
*hdev
, int port1
,
5228 enum usb_port_connect_type type
)
5230 struct usb_hub
*hub
= hdev_to_hub(hdev
);
5232 hub
->ports
[port1
- 1]->connect_type
= type
;
5236 * usb_get_hub_port_connect_type - Get the port's connect type
5237 * @hdev: USB device belonging to the usb hub
5238 * @port1: port num of the port
5240 * Return connect type of the port and if input params are
5241 * invalid, return USB_PORT_CONNECT_TYPE_UNKNOWN.
5243 enum usb_port_connect_type
5244 usb_get_hub_port_connect_type(struct usb_device
*hdev
, int port1
)
5246 struct usb_hub
*hub
= hdev_to_hub(hdev
);
5248 return hub
->ports
[port1
- 1]->connect_type
;
5251 void usb_hub_adjust_deviceremovable(struct usb_device
*hdev
,
5252 struct usb_hub_descriptor
*desc
)
5254 enum usb_port_connect_type connect_type
;
5257 if (!hub_is_superspeed(hdev
)) {
5258 for (i
= 1; i
<= hdev
->maxchild
; i
++) {
5259 connect_type
= usb_get_hub_port_connect_type(hdev
, i
);
5261 if (connect_type
== USB_PORT_CONNECT_TYPE_HARD_WIRED
) {
5262 u8 mask
= 1 << (i
%8);
5264 if (!(desc
->u
.hs
.DeviceRemovable
[i
/8] & mask
)) {
5265 dev_dbg(&hdev
->dev
, "usb port%d's DeviceRemovable is changed to 1 according to platform information.\n",
5267 desc
->u
.hs
.DeviceRemovable
[i
/8] |= mask
;
5272 u16 port_removable
= le16_to_cpu(desc
->u
.ss
.DeviceRemovable
);
5274 for (i
= 1; i
<= hdev
->maxchild
; i
++) {
5275 connect_type
= usb_get_hub_port_connect_type(hdev
, i
);
5277 if (connect_type
== USB_PORT_CONNECT_TYPE_HARD_WIRED
) {
5280 if (!(port_removable
& mask
)) {
5281 dev_dbg(&hdev
->dev
, "usb port%d's DeviceRemovable is changed to 1 according to platform information.\n",
5283 port_removable
|= mask
;
5288 desc
->u
.ss
.DeviceRemovable
= cpu_to_le16(port_removable
);
5294 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5295 * @hdev: USB device belonging to the usb hub
5296 * @port1: port num of the port
5298 * Return port's acpi handle if successful, NULL if params are
5301 acpi_handle
usb_get_hub_port_acpi_handle(struct usb_device
*hdev
,
5304 struct usb_hub
*hub
= hdev_to_hub(hdev
);
5306 return DEVICE_ACPI_HANDLE(&hub
->ports
[port1
- 1]->dev
);