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/quirks.h>
24 #include <linux/kthread.h>
25 #include <linux/mutex.h>
26 #include <linux/freezer.h>
28 #include <asm/uaccess.h>
29 #include <asm/byteorder.h>
33 /* if we are in debug mode, always announce new devices */
35 #ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
36 #define CONFIG_USB_ANNOUNCE_NEW_DEVICES
41 struct device
*intfdev
; /* the "interface" device */
42 struct usb_device
*hdev
;
44 struct urb
*urb
; /* for interrupt polling pipe */
46 /* buffer for urb ... with extra space in case of babble */
49 struct usb_hub_status hub
;
50 struct usb_port_status port
;
51 } *status
; /* buffer for status reports */
52 struct mutex status_mutex
; /* for the status buffer */
54 int error
; /* last reported error */
55 int nerrors
; /* track consecutive errors */
57 struct list_head event_list
; /* hubs w/data or errs ready */
58 unsigned long event_bits
[1]; /* status change bitmask */
59 unsigned long change_bits
[1]; /* ports with logical connect
61 unsigned long busy_bits
[1]; /* ports being reset or
63 unsigned long removed_bits
[1]; /* ports with a "removed"
65 unsigned long wakeup_bits
[1]; /* ports that have signaled
67 #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
68 #error event_bits[] is too short!
71 struct usb_hub_descriptor
*descriptor
; /* class descriptor */
72 struct usb_tt tt
; /* Transaction Translator */
74 unsigned mA_per_port
; /* current for each child */
76 unsigned limited_power
:1;
78 unsigned disconnected
:1;
80 unsigned has_indicators
:1;
81 u8 indicator
[USB_MAXCHILDREN
];
82 struct delayed_work leds
;
83 struct delayed_work init_work
;
87 static inline int hub_is_superspeed(struct usb_device
*hdev
)
89 return (hdev
->descriptor
.bDeviceProtocol
== USB_HUB_PR_SS
);
92 /* Protect struct usb_device->state and ->children members
93 * Note: Both are also protected by ->dev.sem, except that ->state can
94 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
95 static DEFINE_SPINLOCK(device_state_lock
);
97 /* khubd's worklist and its lock */
98 static DEFINE_SPINLOCK(hub_event_lock
);
99 static LIST_HEAD(hub_event_list
); /* List of hubs needing servicing */
102 static DECLARE_WAIT_QUEUE_HEAD(khubd_wait
);
104 static struct task_struct
*khubd_task
;
106 /* cycle leds on hubs that aren't blinking for attention */
107 static bool blinkenlights
= 0;
108 module_param (blinkenlights
, bool, S_IRUGO
);
109 MODULE_PARM_DESC (blinkenlights
, "true to cycle leds on hubs");
112 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
113 * 10 seconds to send reply for the initial 64-byte descriptor request.
115 /* define initial 64-byte descriptor request timeout in milliseconds */
116 static int initial_descriptor_timeout
= USB_CTRL_GET_TIMEOUT
;
117 module_param(initial_descriptor_timeout
, int, S_IRUGO
|S_IWUSR
);
118 MODULE_PARM_DESC(initial_descriptor_timeout
,
119 "initial 64-byte descriptor request timeout in milliseconds "
120 "(default 5000 - 5.0 seconds)");
123 * As of 2.6.10 we introduce a new USB device initialization scheme which
124 * closely resembles the way Windows works. Hopefully it will be compatible
125 * with a wider range of devices than the old scheme. However some previously
126 * working devices may start giving rise to "device not accepting address"
127 * errors; if that happens the user can try the old scheme by adjusting the
128 * following module parameters.
130 * For maximum flexibility there are two boolean parameters to control the
131 * hub driver's behavior. On the first initialization attempt, if the
132 * "old_scheme_first" parameter is set then the old scheme will be used,
133 * otherwise the new scheme is used. If that fails and "use_both_schemes"
134 * is set, then the driver will make another attempt, using the other scheme.
136 static bool old_scheme_first
= 0;
137 module_param(old_scheme_first
, bool, S_IRUGO
| S_IWUSR
);
138 MODULE_PARM_DESC(old_scheme_first
,
139 "start with the old device initialization scheme");
141 static bool use_both_schemes
= 1;
142 module_param(use_both_schemes
, bool, S_IRUGO
| S_IWUSR
);
143 MODULE_PARM_DESC(use_both_schemes
,
144 "try the other device initialization scheme if the "
147 /* Mutual exclusion for EHCI CF initialization. This interferes with
148 * port reset on some companion controllers.
150 DECLARE_RWSEM(ehci_cf_port_reset_rwsem
);
151 EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem
);
153 #define HUB_DEBOUNCE_TIMEOUT 1500
154 #define HUB_DEBOUNCE_STEP 25
155 #define HUB_DEBOUNCE_STABLE 100
158 static int usb_reset_and_verify_device(struct usb_device
*udev
);
160 static inline char *portspeed(struct usb_hub
*hub
, int portstatus
)
162 if (hub_is_superspeed(hub
->hdev
))
164 if (portstatus
& USB_PORT_STAT_HIGH_SPEED
)
166 else if (portstatus
& USB_PORT_STAT_LOW_SPEED
)
172 /* Note that hdev or one of its children must be locked! */
173 static struct usb_hub
*hdev_to_hub(struct usb_device
*hdev
)
175 if (!hdev
|| !hdev
->actconfig
)
177 return usb_get_intfdata(hdev
->actconfig
->interface
[0]);
180 static int usb_device_supports_lpm(struct usb_device
*udev
)
182 /* USB 2.1 (and greater) devices indicate LPM support through
183 * their USB 2.0 Extended Capabilities BOS descriptor.
185 if (udev
->speed
== USB_SPEED_HIGH
) {
186 if (udev
->bos
->ext_cap
&&
188 le32_to_cpu(udev
->bos
->ext_cap
->bmAttributes
)))
193 /* All USB 3.0 must support LPM, but we need their max exit latency
194 * information from the SuperSpeed Extended Capabilities BOS descriptor.
196 if (!udev
->bos
->ss_cap
) {
197 dev_warn(&udev
->dev
, "No LPM exit latency info found. "
198 "Power management will be impacted.\n");
201 if (udev
->parent
->lpm_capable
)
204 dev_warn(&udev
->dev
, "Parent hub missing LPM exit latency info. "
205 "Power management will be impacted.\n");
210 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
213 static void usb_set_lpm_mel(struct usb_device
*udev
,
214 struct usb3_lpm_parameters
*udev_lpm_params
,
215 unsigned int udev_exit_latency
,
217 struct usb3_lpm_parameters
*hub_lpm_params
,
218 unsigned int hub_exit_latency
)
220 unsigned int total_mel
;
221 unsigned int device_mel
;
222 unsigned int hub_mel
;
225 * Calculate the time it takes to transition all links from the roothub
226 * to the parent hub into U0. The parent hub must then decode the
227 * packet (hub header decode latency) to figure out which port it was
230 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
231 * means 0.1us). Multiply that by 100 to get nanoseconds.
233 total_mel
= hub_lpm_params
->mel
+
234 (hub
->descriptor
->u
.ss
.bHubHdrDecLat
* 100);
237 * How long will it take to transition the downstream hub's port into
238 * U0? The greater of either the hub exit latency or the device exit
241 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
242 * Multiply that by 1000 to get nanoseconds.
244 device_mel
= udev_exit_latency
* 1000;
245 hub_mel
= hub_exit_latency
* 1000;
246 if (device_mel
> hub_mel
)
247 total_mel
+= device_mel
;
249 total_mel
+= hub_mel
;
251 udev_lpm_params
->mel
= total_mel
;
255 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
256 * a transition from either U1 or U2.
258 static void usb_set_lpm_pel(struct usb_device
*udev
,
259 struct usb3_lpm_parameters
*udev_lpm_params
,
260 unsigned int udev_exit_latency
,
262 struct usb3_lpm_parameters
*hub_lpm_params
,
263 unsigned int hub_exit_latency
,
264 unsigned int port_to_port_exit_latency
)
266 unsigned int first_link_pel
;
267 unsigned int hub_pel
;
270 * First, the device sends an LFPS to transition the link between the
271 * device and the parent hub into U0. The exit latency is the bigger of
272 * the device exit latency or the hub exit latency.
274 if (udev_exit_latency
> hub_exit_latency
)
275 first_link_pel
= udev_exit_latency
* 1000;
277 first_link_pel
= hub_exit_latency
* 1000;
280 * When the hub starts to receive the LFPS, there is a slight delay for
281 * it to figure out that one of the ports is sending an LFPS. Then it
282 * will forward the LFPS to its upstream link. The exit latency is the
283 * delay, plus the PEL that we calculated for this hub.
285 hub_pel
= port_to_port_exit_latency
* 1000 + hub_lpm_params
->pel
;
288 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
289 * is the greater of the two exit latencies.
291 if (first_link_pel
> hub_pel
)
292 udev_lpm_params
->pel
= first_link_pel
;
294 udev_lpm_params
->pel
= hub_pel
;
298 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
299 * when a device initiates a transition to U0, until when it will receive the
300 * first packet from the host controller.
302 * Section C.1.5.1 describes the four components to this:
304 * - t2: time for the ERDY to make it from the device to the host.
305 * - t3: a host-specific delay to process the ERDY.
306 * - t4: time for the packet to make it from the host to the device.
308 * t3 is specific to both the xHCI host and the platform the host is integrated
309 * into. The Intel HW folks have said it's negligible, FIXME if a different
310 * vendor says otherwise.
312 static void usb_set_lpm_sel(struct usb_device
*udev
,
313 struct usb3_lpm_parameters
*udev_lpm_params
)
315 struct usb_device
*parent
;
316 unsigned int num_hubs
;
317 unsigned int total_sel
;
319 /* t1 = device PEL */
320 total_sel
= udev_lpm_params
->pel
;
321 /* How many external hubs are in between the device & the root port. */
322 for (parent
= udev
->parent
, num_hubs
= 0; parent
->parent
;
323 parent
= parent
->parent
)
325 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
327 total_sel
+= 2100 + 250 * (num_hubs
- 1);
329 /* t4 = 250ns * num_hubs */
330 total_sel
+= 250 * num_hubs
;
332 udev_lpm_params
->sel
= total_sel
;
335 static void usb_set_lpm_parameters(struct usb_device
*udev
)
338 unsigned int port_to_port_delay
;
339 unsigned int udev_u1_del
;
340 unsigned int udev_u2_del
;
341 unsigned int hub_u1_del
;
342 unsigned int hub_u2_del
;
344 if (!udev
->lpm_capable
|| udev
->speed
!= USB_SPEED_SUPER
)
347 hub
= hdev_to_hub(udev
->parent
);
348 /* It doesn't take time to transition the roothub into U0, since it
349 * doesn't have an upstream link.
354 udev_u1_del
= udev
->bos
->ss_cap
->bU1devExitLat
;
355 udev_u2_del
= udev
->bos
->ss_cap
->bU2DevExitLat
;
356 hub_u1_del
= udev
->parent
->bos
->ss_cap
->bU1devExitLat
;
357 hub_u2_del
= udev
->parent
->bos
->ss_cap
->bU2DevExitLat
;
359 usb_set_lpm_mel(udev
, &udev
->u1_params
, udev_u1_del
,
360 hub
, &udev
->parent
->u1_params
, hub_u1_del
);
362 usb_set_lpm_mel(udev
, &udev
->u2_params
, udev_u2_del
,
363 hub
, &udev
->parent
->u2_params
, hub_u2_del
);
366 * Appendix C, section C.2.2.2, says that there is a slight delay from
367 * when the parent hub notices the downstream port is trying to
368 * transition to U0 to when the hub initiates a U0 transition on its
369 * upstream port. The section says the delays are tPort2PortU1EL and
370 * tPort2PortU2EL, but it doesn't define what they are.
372 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
373 * about the same delays. Use the maximum delay calculations from those
374 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
375 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
376 * assume the device exit latencies they are talking about are the hub
379 * What do we do if the U2 exit latency is less than the U1 exit
380 * latency? It's possible, although not likely...
382 port_to_port_delay
= 1;
384 usb_set_lpm_pel(udev
, &udev
->u1_params
, udev_u1_del
,
385 hub
, &udev
->parent
->u1_params
, hub_u1_del
,
388 if (hub_u2_del
> hub_u1_del
)
389 port_to_port_delay
= 1 + hub_u2_del
- hub_u1_del
;
391 port_to_port_delay
= 1 + hub_u1_del
;
393 usb_set_lpm_pel(udev
, &udev
->u2_params
, udev_u2_del
,
394 hub
, &udev
->parent
->u2_params
, hub_u2_del
,
397 /* Now that we've got PEL, calculate SEL. */
398 usb_set_lpm_sel(udev
, &udev
->u1_params
);
399 usb_set_lpm_sel(udev
, &udev
->u2_params
);
402 /* USB 2.0 spec Section 11.24.4.5 */
403 static int get_hub_descriptor(struct usb_device
*hdev
, void *data
)
408 if (hub_is_superspeed(hdev
)) {
409 dtype
= USB_DT_SS_HUB
;
410 size
= USB_DT_SS_HUB_SIZE
;
413 size
= sizeof(struct usb_hub_descriptor
);
416 for (i
= 0; i
< 3; i
++) {
417 ret
= usb_control_msg(hdev
, usb_rcvctrlpipe(hdev
, 0),
418 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
| USB_RT_HUB
,
419 dtype
<< 8, 0, data
, size
,
420 USB_CTRL_GET_TIMEOUT
);
421 if (ret
>= (USB_DT_HUB_NONVAR_SIZE
+ 2))
428 * USB 2.0 spec Section 11.24.2.1
430 static int clear_hub_feature(struct usb_device
*hdev
, int feature
)
432 return usb_control_msg(hdev
, usb_sndctrlpipe(hdev
, 0),
433 USB_REQ_CLEAR_FEATURE
, USB_RT_HUB
, feature
, 0, NULL
, 0, 1000);
437 * USB 2.0 spec Section 11.24.2.2
439 static int clear_port_feature(struct usb_device
*hdev
, int port1
, int feature
)
441 return usb_control_msg(hdev
, usb_sndctrlpipe(hdev
, 0),
442 USB_REQ_CLEAR_FEATURE
, USB_RT_PORT
, feature
, port1
,
447 * USB 2.0 spec Section 11.24.2.13
449 static int set_port_feature(struct usb_device
*hdev
, int port1
, int feature
)
451 return usb_control_msg(hdev
, usb_sndctrlpipe(hdev
, 0),
452 USB_REQ_SET_FEATURE
, USB_RT_PORT
, feature
, port1
,
457 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
458 * for info about using port indicators
460 static void set_port_led(
466 int status
= set_port_feature(hub
->hdev
, (selector
<< 8) | port1
,
467 USB_PORT_FEAT_INDICATOR
);
469 dev_dbg (hub
->intfdev
,
470 "port %d indicator %s status %d\n",
472 ({ char *s
; switch (selector
) {
473 case HUB_LED_AMBER
: s
= "amber"; break;
474 case HUB_LED_GREEN
: s
= "green"; break;
475 case HUB_LED_OFF
: s
= "off"; break;
476 case HUB_LED_AUTO
: s
= "auto"; break;
477 default: s
= "??"; break;
482 #define LED_CYCLE_PERIOD ((2*HZ)/3)
484 static void led_work (struct work_struct
*work
)
486 struct usb_hub
*hub
=
487 container_of(work
, struct usb_hub
, leds
.work
);
488 struct usb_device
*hdev
= hub
->hdev
;
490 unsigned changed
= 0;
493 if (hdev
->state
!= USB_STATE_CONFIGURED
|| hub
->quiescing
)
496 for (i
= 0; i
< hub
->descriptor
->bNbrPorts
; i
++) {
497 unsigned selector
, mode
;
499 /* 30%-50% duty cycle */
501 switch (hub
->indicator
[i
]) {
503 case INDICATOR_CYCLE
:
505 selector
= HUB_LED_AUTO
;
506 mode
= INDICATOR_AUTO
;
508 /* blinking green = sw attention */
509 case INDICATOR_GREEN_BLINK
:
510 selector
= HUB_LED_GREEN
;
511 mode
= INDICATOR_GREEN_BLINK_OFF
;
513 case INDICATOR_GREEN_BLINK_OFF
:
514 selector
= HUB_LED_OFF
;
515 mode
= INDICATOR_GREEN_BLINK
;
517 /* blinking amber = hw attention */
518 case INDICATOR_AMBER_BLINK
:
519 selector
= HUB_LED_AMBER
;
520 mode
= INDICATOR_AMBER_BLINK_OFF
;
522 case INDICATOR_AMBER_BLINK_OFF
:
523 selector
= HUB_LED_OFF
;
524 mode
= INDICATOR_AMBER_BLINK
;
526 /* blink green/amber = reserved */
527 case INDICATOR_ALT_BLINK
:
528 selector
= HUB_LED_GREEN
;
529 mode
= INDICATOR_ALT_BLINK_OFF
;
531 case INDICATOR_ALT_BLINK_OFF
:
532 selector
= HUB_LED_AMBER
;
533 mode
= INDICATOR_ALT_BLINK
;
538 if (selector
!= HUB_LED_AUTO
)
540 set_port_led(hub
, i
+ 1, selector
);
541 hub
->indicator
[i
] = mode
;
543 if (!changed
&& blinkenlights
) {
545 cursor
%= hub
->descriptor
->bNbrPorts
;
546 set_port_led(hub
, cursor
+ 1, HUB_LED_GREEN
);
547 hub
->indicator
[cursor
] = INDICATOR_CYCLE
;
551 schedule_delayed_work(&hub
->leds
, LED_CYCLE_PERIOD
);
554 /* use a short timeout for hub/port status fetches */
555 #define USB_STS_TIMEOUT 1000
556 #define USB_STS_RETRIES 5
559 * USB 2.0 spec Section 11.24.2.6
561 static int get_hub_status(struct usb_device
*hdev
,
562 struct usb_hub_status
*data
)
564 int i
, status
= -ETIMEDOUT
;
566 for (i
= 0; i
< USB_STS_RETRIES
&&
567 (status
== -ETIMEDOUT
|| status
== -EPIPE
); i
++) {
568 status
= usb_control_msg(hdev
, usb_rcvctrlpipe(hdev
, 0),
569 USB_REQ_GET_STATUS
, USB_DIR_IN
| USB_RT_HUB
, 0, 0,
570 data
, sizeof(*data
), USB_STS_TIMEOUT
);
576 * USB 2.0 spec Section 11.24.2.7
578 static int get_port_status(struct usb_device
*hdev
, int port1
,
579 struct usb_port_status
*data
)
581 int i
, status
= -ETIMEDOUT
;
583 for (i
= 0; i
< USB_STS_RETRIES
&&
584 (status
== -ETIMEDOUT
|| status
== -EPIPE
); i
++) {
585 status
= usb_control_msg(hdev
, usb_rcvctrlpipe(hdev
, 0),
586 USB_REQ_GET_STATUS
, USB_DIR_IN
| USB_RT_PORT
, 0, port1
,
587 data
, sizeof(*data
), USB_STS_TIMEOUT
);
592 static int hub_port_status(struct usb_hub
*hub
, int port1
,
593 u16
*status
, u16
*change
)
597 mutex_lock(&hub
->status_mutex
);
598 ret
= get_port_status(hub
->hdev
, port1
, &hub
->status
->port
);
600 dev_err(hub
->intfdev
,
601 "%s failed (err = %d)\n", __func__
, ret
);
605 *status
= le16_to_cpu(hub
->status
->port
.wPortStatus
);
606 *change
= le16_to_cpu(hub
->status
->port
.wPortChange
);
610 mutex_unlock(&hub
->status_mutex
);
614 static void kick_khubd(struct usb_hub
*hub
)
618 spin_lock_irqsave(&hub_event_lock
, flags
);
619 if (!hub
->disconnected
&& list_empty(&hub
->event_list
)) {
620 list_add_tail(&hub
->event_list
, &hub_event_list
);
622 /* Suppress autosuspend until khubd runs */
623 usb_autopm_get_interface_no_resume(
624 to_usb_interface(hub
->intfdev
));
625 wake_up(&khubd_wait
);
627 spin_unlock_irqrestore(&hub_event_lock
, flags
);
630 void usb_kick_khubd(struct usb_device
*hdev
)
632 struct usb_hub
*hub
= hdev_to_hub(hdev
);
639 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
640 * Notification, which indicates it had initiated remote wakeup.
642 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
643 * device initiates resume, so the USB core will not receive notice of the
644 * resume through the normal hub interrupt URB.
646 void usb_wakeup_notification(struct usb_device
*hdev
,
647 unsigned int portnum
)
654 hub
= hdev_to_hub(hdev
);
656 set_bit(portnum
, hub
->wakeup_bits
);
660 EXPORT_SYMBOL_GPL(usb_wakeup_notification
);
662 /* completion function, fires on port status changes and various faults */
663 static void hub_irq(struct urb
*urb
)
665 struct usb_hub
*hub
= urb
->context
;
666 int status
= urb
->status
;
671 case -ENOENT
: /* synchronous unlink */
672 case -ECONNRESET
: /* async unlink */
673 case -ESHUTDOWN
: /* hardware going away */
676 default: /* presumably an error */
677 /* Cause a hub reset after 10 consecutive errors */
678 dev_dbg (hub
->intfdev
, "transfer --> %d\n", status
);
679 if ((++hub
->nerrors
< 10) || hub
->error
)
684 /* let khubd handle things */
685 case 0: /* we got data: port status changed */
687 for (i
= 0; i
< urb
->actual_length
; ++i
)
688 bits
|= ((unsigned long) ((*hub
->buffer
)[i
]))
690 hub
->event_bits
[0] = bits
;
696 /* Something happened, let khubd figure it out */
703 if ((status
= usb_submit_urb (hub
->urb
, GFP_ATOMIC
)) != 0
704 && status
!= -ENODEV
&& status
!= -EPERM
)
705 dev_err (hub
->intfdev
, "resubmit --> %d\n", status
);
708 /* USB 2.0 spec Section 11.24.2.3 */
710 hub_clear_tt_buffer (struct usb_device
*hdev
, u16 devinfo
, u16 tt
)
712 return usb_control_msg(hdev
, usb_sndctrlpipe(hdev
, 0),
713 HUB_CLEAR_TT_BUFFER
, USB_RT_PORT
, devinfo
,
718 * enumeration blocks khubd for a long time. we use keventd instead, since
719 * long blocking there is the exception, not the rule. accordingly, HCDs
720 * talking to TTs must queue control transfers (not just bulk and iso), so
721 * both can talk to the same hub concurrently.
723 static void hub_tt_work(struct work_struct
*work
)
725 struct usb_hub
*hub
=
726 container_of(work
, struct usb_hub
, tt
.clear_work
);
730 spin_lock_irqsave (&hub
->tt
.lock
, flags
);
731 while (--limit
&& !list_empty (&hub
->tt
.clear_list
)) {
732 struct list_head
*next
;
733 struct usb_tt_clear
*clear
;
734 struct usb_device
*hdev
= hub
->hdev
;
735 const struct hc_driver
*drv
;
738 next
= hub
->tt
.clear_list
.next
;
739 clear
= list_entry (next
, struct usb_tt_clear
, clear_list
);
740 list_del (&clear
->clear_list
);
742 /* drop lock so HCD can concurrently report other TT errors */
743 spin_unlock_irqrestore (&hub
->tt
.lock
, flags
);
744 status
= hub_clear_tt_buffer (hdev
, clear
->devinfo
, clear
->tt
);
747 "clear tt %d (%04x) error %d\n",
748 clear
->tt
, clear
->devinfo
, status
);
750 /* Tell the HCD, even if the operation failed */
751 drv
= clear
->hcd
->driver
;
752 if (drv
->clear_tt_buffer_complete
)
753 (drv
->clear_tt_buffer_complete
)(clear
->hcd
, clear
->ep
);
756 spin_lock_irqsave(&hub
->tt
.lock
, flags
);
758 spin_unlock_irqrestore (&hub
->tt
.lock
, flags
);
762 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
763 * @urb: an URB associated with the failed or incomplete split transaction
765 * High speed HCDs use this to tell the hub driver that some split control or
766 * bulk transaction failed in a way that requires clearing internal state of
767 * a transaction translator. This is normally detected (and reported) from
770 * It may not be possible for that hub to handle additional full (or low)
771 * speed transactions until that state is fully cleared out.
773 int usb_hub_clear_tt_buffer(struct urb
*urb
)
775 struct usb_device
*udev
= urb
->dev
;
776 int pipe
= urb
->pipe
;
777 struct usb_tt
*tt
= udev
->tt
;
779 struct usb_tt_clear
*clear
;
781 /* we've got to cope with an arbitrary number of pending TT clears,
782 * since each TT has "at least two" buffers that can need it (and
783 * there can be many TTs per hub). even if they're uncommon.
785 if ((clear
= kmalloc (sizeof *clear
, GFP_ATOMIC
)) == NULL
) {
786 dev_err (&udev
->dev
, "can't save CLEAR_TT_BUFFER state\n");
787 /* FIXME recover somehow ... RESET_TT? */
791 /* info that CLEAR_TT_BUFFER needs */
792 clear
->tt
= tt
->multi
? udev
->ttport
: 1;
793 clear
->devinfo
= usb_pipeendpoint (pipe
);
794 clear
->devinfo
|= udev
->devnum
<< 4;
795 clear
->devinfo
|= usb_pipecontrol (pipe
)
796 ? (USB_ENDPOINT_XFER_CONTROL
<< 11)
797 : (USB_ENDPOINT_XFER_BULK
<< 11);
798 if (usb_pipein (pipe
))
799 clear
->devinfo
|= 1 << 15;
801 /* info for completion callback */
802 clear
->hcd
= bus_to_hcd(udev
->bus
);
805 /* tell keventd to clear state for this TT */
806 spin_lock_irqsave (&tt
->lock
, flags
);
807 list_add_tail (&clear
->clear_list
, &tt
->clear_list
);
808 schedule_work(&tt
->clear_work
);
809 spin_unlock_irqrestore (&tt
->lock
, flags
);
812 EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer
);
814 /* If do_delay is false, return the number of milliseconds the caller
817 static unsigned hub_power_on(struct usb_hub
*hub
, bool do_delay
)
820 unsigned pgood_delay
= hub
->descriptor
->bPwrOn2PwrGood
* 2;
822 u16 wHubCharacteristics
=
823 le16_to_cpu(hub
->descriptor
->wHubCharacteristics
);
825 /* Enable power on each port. Some hubs have reserved values
826 * of LPSM (> 2) in their descriptors, even though they are
827 * USB 2.0 hubs. Some hubs do not implement port-power switching
828 * but only emulate it. In all cases, the ports won't work
829 * unless we send these messages to the hub.
831 if ((wHubCharacteristics
& HUB_CHAR_LPSM
) < 2)
832 dev_dbg(hub
->intfdev
, "enabling power on all ports\n");
834 dev_dbg(hub
->intfdev
, "trying to enable port power on "
835 "non-switchable hub\n");
836 for (port1
= 1; port1
<= hub
->descriptor
->bNbrPorts
; port1
++)
837 set_port_feature(hub
->hdev
, port1
, USB_PORT_FEAT_POWER
);
839 /* Wait at least 100 msec for power to become stable */
840 delay
= max(pgood_delay
, (unsigned) 100);
846 static int hub_hub_status(struct usb_hub
*hub
,
847 u16
*status
, u16
*change
)
851 mutex_lock(&hub
->status_mutex
);
852 ret
= get_hub_status(hub
->hdev
, &hub
->status
->hub
);
854 dev_err (hub
->intfdev
,
855 "%s failed (err = %d)\n", __func__
, ret
);
857 *status
= le16_to_cpu(hub
->status
->hub
.wHubStatus
);
858 *change
= le16_to_cpu(hub
->status
->hub
.wHubChange
);
861 mutex_unlock(&hub
->status_mutex
);
865 static int hub_port_disable(struct usb_hub
*hub
, int port1
, int set_state
)
867 struct usb_device
*hdev
= hub
->hdev
;
870 if (hdev
->children
[port1
-1] && set_state
)
871 usb_set_device_state(hdev
->children
[port1
-1],
872 USB_STATE_NOTATTACHED
);
873 if (!hub
->error
&& !hub_is_superspeed(hub
->hdev
))
874 ret
= clear_port_feature(hdev
, port1
, USB_PORT_FEAT_ENABLE
);
876 dev_err(hub
->intfdev
, "cannot disable port %d (err = %d)\n",
882 * Disable a port and mark a logical connect-change event, so that some
883 * time later khubd will disconnect() any existing usb_device on the port
884 * and will re-enumerate if there actually is a device attached.
886 static void hub_port_logical_disconnect(struct usb_hub
*hub
, int port1
)
888 dev_dbg(hub
->intfdev
, "logical disconnect on port %d\n", port1
);
889 hub_port_disable(hub
, port1
, 1);
891 /* FIXME let caller ask to power down the port:
892 * - some devices won't enumerate without a VBUS power cycle
893 * - SRP saves power that way
894 * - ... new call, TBD ...
895 * That's easy if this hub can switch power per-port, and
896 * khubd reactivates the port later (timer, SRP, etc).
897 * Powerdown must be optional, because of reset/DFU.
900 set_bit(port1
, hub
->change_bits
);
905 * usb_remove_device - disable a device's port on its parent hub
906 * @udev: device to be disabled and removed
907 * Context: @udev locked, must be able to sleep.
909 * After @udev's port has been disabled, khubd is notified and it will
910 * see that the device has been disconnected. When the device is
911 * physically unplugged and something is plugged in, the events will
912 * be received and processed normally.
914 int usb_remove_device(struct usb_device
*udev
)
917 struct usb_interface
*intf
;
919 if (!udev
->parent
) /* Can't remove a root hub */
921 hub
= hdev_to_hub(udev
->parent
);
922 intf
= to_usb_interface(hub
->intfdev
);
924 usb_autopm_get_interface(intf
);
925 set_bit(udev
->portnum
, hub
->removed_bits
);
926 hub_port_logical_disconnect(hub
, udev
->portnum
);
927 usb_autopm_put_interface(intf
);
931 enum hub_activation_type
{
932 HUB_INIT
, HUB_INIT2
, HUB_INIT3
, /* INITs must come first */
933 HUB_POST_RESET
, HUB_RESUME
, HUB_RESET_RESUME
,
936 static void hub_init_func2(struct work_struct
*ws
);
937 static void hub_init_func3(struct work_struct
*ws
);
939 static void hub_activate(struct usb_hub
*hub
, enum hub_activation_type type
)
941 struct usb_device
*hdev
= hub
->hdev
;
946 bool need_debounce_delay
= false;
949 /* Continue a partial initialization */
950 if (type
== HUB_INIT2
)
952 if (type
== HUB_INIT3
)
955 /* The superspeed hub except for root hub has to use Hub Depth
956 * value as an offset into the route string to locate the bits
957 * it uses to determine the downstream port number. So hub driver
958 * should send a set hub depth request to superspeed hub after
959 * the superspeed hub is set configuration in initialization or
962 * After a resume, port power should still be on.
963 * For any other type of activation, turn it on.
965 if (type
!= HUB_RESUME
) {
966 if (hdev
->parent
&& hub_is_superspeed(hdev
)) {
967 ret
= usb_control_msg(hdev
, usb_sndctrlpipe(hdev
, 0),
968 HUB_SET_DEPTH
, USB_RT_HUB
,
969 hdev
->level
- 1, 0, NULL
, 0,
970 USB_CTRL_SET_TIMEOUT
);
972 dev_err(hub
->intfdev
,
973 "set hub depth failed\n");
976 /* Speed up system boot by using a delayed_work for the
977 * hub's initial power-up delays. This is pretty awkward
978 * and the implementation looks like a home-brewed sort of
979 * setjmp/longjmp, but it saves at least 100 ms for each
980 * root hub (assuming usbcore is compiled into the kernel
981 * rather than as a module). It adds up.
983 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
984 * because for those activation types the ports have to be
985 * operational when we return. In theory this could be done
986 * for HUB_POST_RESET, but it's easier not to.
988 if (type
== HUB_INIT
) {
989 delay
= hub_power_on(hub
, false);
990 PREPARE_DELAYED_WORK(&hub
->init_work
, hub_init_func2
);
991 schedule_delayed_work(&hub
->init_work
,
992 msecs_to_jiffies(delay
));
994 /* Suppress autosuspend until init is done */
995 usb_autopm_get_interface_no_resume(
996 to_usb_interface(hub
->intfdev
));
997 return; /* Continues at init2: below */
998 } else if (type
== HUB_RESET_RESUME
) {
999 /* The internal host controller state for the hub device
1000 * may be gone after a host power loss on system resume.
1001 * Update the device's info so the HW knows it's a hub.
1003 hcd
= bus_to_hcd(hdev
->bus
);
1004 if (hcd
->driver
->update_hub_device
) {
1005 ret
= hcd
->driver
->update_hub_device(hcd
, hdev
,
1006 &hub
->tt
, GFP_NOIO
);
1008 dev_err(hub
->intfdev
, "Host not "
1009 "accepting hub info "
1011 dev_err(hub
->intfdev
, "LS/FS devices "
1012 "and hubs may not work "
1013 "under this hub\n.");
1016 hub_power_on(hub
, true);
1018 hub_power_on(hub
, true);
1023 /* Check each port and set hub->change_bits to let khubd know
1024 * which ports need attention.
1026 for (port1
= 1; port1
<= hdev
->maxchild
; ++port1
) {
1027 struct usb_device
*udev
= hdev
->children
[port1
-1];
1028 u16 portstatus
, portchange
;
1030 portstatus
= portchange
= 0;
1031 status
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
1032 if (udev
|| (portstatus
& USB_PORT_STAT_CONNECTION
))
1033 dev_dbg(hub
->intfdev
,
1034 "port %d: status %04x change %04x\n",
1035 port1
, portstatus
, portchange
);
1037 /* After anything other than HUB_RESUME (i.e., initialization
1038 * or any sort of reset), every port should be disabled.
1039 * Unconnected ports should likewise be disabled (paranoia),
1040 * and so should ports for which we have no usb_device.
1042 if ((portstatus
& USB_PORT_STAT_ENABLE
) && (
1043 type
!= HUB_RESUME
||
1044 !(portstatus
& USB_PORT_STAT_CONNECTION
) ||
1046 udev
->state
== USB_STATE_NOTATTACHED
)) {
1048 * USB3 protocol ports will automatically transition
1049 * to Enabled state when detect an USB3.0 device attach.
1050 * Do not disable USB3 protocol ports.
1052 if (!hub_is_superspeed(hdev
)) {
1053 clear_port_feature(hdev
, port1
,
1054 USB_PORT_FEAT_ENABLE
);
1055 portstatus
&= ~USB_PORT_STAT_ENABLE
;
1057 /* Pretend that power was lost for USB3 devs */
1058 portstatus
&= ~USB_PORT_STAT_ENABLE
;
1062 /* Clear status-change flags; we'll debounce later */
1063 if (portchange
& USB_PORT_STAT_C_CONNECTION
) {
1064 need_debounce_delay
= true;
1065 clear_port_feature(hub
->hdev
, port1
,
1066 USB_PORT_FEAT_C_CONNECTION
);
1068 if (portchange
& USB_PORT_STAT_C_ENABLE
) {
1069 need_debounce_delay
= true;
1070 clear_port_feature(hub
->hdev
, port1
,
1071 USB_PORT_FEAT_C_ENABLE
);
1073 if ((portchange
& USB_PORT_STAT_C_BH_RESET
) &&
1074 hub_is_superspeed(hub
->hdev
)) {
1075 need_debounce_delay
= true;
1076 clear_port_feature(hub
->hdev
, port1
,
1077 USB_PORT_FEAT_C_BH_PORT_RESET
);
1079 /* We can forget about a "removed" device when there's a
1080 * physical disconnect or the connect status changes.
1082 if (!(portstatus
& USB_PORT_STAT_CONNECTION
) ||
1083 (portchange
& USB_PORT_STAT_C_CONNECTION
))
1084 clear_bit(port1
, hub
->removed_bits
);
1086 if (!udev
|| udev
->state
== USB_STATE_NOTATTACHED
) {
1087 /* Tell khubd to disconnect the device or
1088 * check for a new connection
1090 if (udev
|| (portstatus
& USB_PORT_STAT_CONNECTION
))
1091 set_bit(port1
, hub
->change_bits
);
1093 } else if (portstatus
& USB_PORT_STAT_ENABLE
) {
1094 bool port_resumed
= (portstatus
&
1095 USB_PORT_STAT_LINK_STATE
) ==
1097 /* The power session apparently survived the resume.
1098 * If there was an overcurrent or suspend change
1099 * (i.e., remote wakeup request), have khubd
1100 * take care of it. Look at the port link state
1101 * for USB 3.0 hubs, since they don't have a suspend
1102 * change bit, and they don't set the port link change
1103 * bit on device-initiated resume.
1105 if (portchange
|| (hub_is_superspeed(hub
->hdev
) &&
1107 set_bit(port1
, hub
->change_bits
);
1109 } else if (udev
->persist_enabled
) {
1111 udev
->reset_resume
= 1;
1113 set_bit(port1
, hub
->change_bits
);
1116 /* The power session is gone; tell khubd */
1117 usb_set_device_state(udev
, USB_STATE_NOTATTACHED
);
1118 set_bit(port1
, hub
->change_bits
);
1122 /* If no port-status-change flags were set, we don't need any
1123 * debouncing. If flags were set we can try to debounce the
1124 * ports all at once right now, instead of letting khubd do them
1125 * one at a time later on.
1127 * If any port-status changes do occur during this delay, khubd
1128 * will see them later and handle them normally.
1130 if (need_debounce_delay
) {
1131 delay
= HUB_DEBOUNCE_STABLE
;
1133 /* Don't do a long sleep inside a workqueue routine */
1134 if (type
== HUB_INIT2
) {
1135 PREPARE_DELAYED_WORK(&hub
->init_work
, hub_init_func3
);
1136 schedule_delayed_work(&hub
->init_work
,
1137 msecs_to_jiffies(delay
));
1138 return; /* Continues at init3: below */
1146 status
= usb_submit_urb(hub
->urb
, GFP_NOIO
);
1148 dev_err(hub
->intfdev
, "activate --> %d\n", status
);
1149 if (hub
->has_indicators
&& blinkenlights
)
1150 schedule_delayed_work(&hub
->leds
, LED_CYCLE_PERIOD
);
1152 /* Scan all ports that need attention */
1155 /* Allow autosuspend if it was suppressed */
1156 if (type
<= HUB_INIT3
)
1157 usb_autopm_put_interface_async(to_usb_interface(hub
->intfdev
));
1160 /* Implement the continuations for the delays above */
1161 static void hub_init_func2(struct work_struct
*ws
)
1163 struct usb_hub
*hub
= container_of(ws
, struct usb_hub
, init_work
.work
);
1165 hub_activate(hub
, HUB_INIT2
);
1168 static void hub_init_func3(struct work_struct
*ws
)
1170 struct usb_hub
*hub
= container_of(ws
, struct usb_hub
, init_work
.work
);
1172 hub_activate(hub
, HUB_INIT3
);
1175 enum hub_quiescing_type
{
1176 HUB_DISCONNECT
, HUB_PRE_RESET
, HUB_SUSPEND
1179 static void hub_quiesce(struct usb_hub
*hub
, enum hub_quiescing_type type
)
1181 struct usb_device
*hdev
= hub
->hdev
;
1184 cancel_delayed_work_sync(&hub
->init_work
);
1186 /* khubd and related activity won't re-trigger */
1189 if (type
!= HUB_SUSPEND
) {
1190 /* Disconnect all the children */
1191 for (i
= 0; i
< hdev
->maxchild
; ++i
) {
1192 if (hdev
->children
[i
])
1193 usb_disconnect(&hdev
->children
[i
]);
1197 /* Stop khubd and related activity */
1198 usb_kill_urb(hub
->urb
);
1199 if (hub
->has_indicators
)
1200 cancel_delayed_work_sync(&hub
->leds
);
1202 cancel_work_sync(&hub
->tt
.clear_work
);
1205 /* caller has locked the hub device */
1206 static int hub_pre_reset(struct usb_interface
*intf
)
1208 struct usb_hub
*hub
= usb_get_intfdata(intf
);
1210 hub_quiesce(hub
, HUB_PRE_RESET
);
1214 /* caller has locked the hub device */
1215 static int hub_post_reset(struct usb_interface
*intf
)
1217 struct usb_hub
*hub
= usb_get_intfdata(intf
);
1219 hub_activate(hub
, HUB_POST_RESET
);
1223 static int hub_configure(struct usb_hub
*hub
,
1224 struct usb_endpoint_descriptor
*endpoint
)
1226 struct usb_hcd
*hcd
;
1227 struct usb_device
*hdev
= hub
->hdev
;
1228 struct device
*hub_dev
= hub
->intfdev
;
1229 u16 hubstatus
, hubchange
;
1230 u16 wHubCharacteristics
;
1233 char *message
= "out of memory";
1235 hub
->buffer
= kmalloc(sizeof(*hub
->buffer
), GFP_KERNEL
);
1241 hub
->status
= kmalloc(sizeof(*hub
->status
), GFP_KERNEL
);
1246 mutex_init(&hub
->status_mutex
);
1248 hub
->descriptor
= kmalloc(sizeof(*hub
->descriptor
), GFP_KERNEL
);
1249 if (!hub
->descriptor
) {
1254 /* Request the entire hub descriptor.
1255 * hub->descriptor can handle USB_MAXCHILDREN ports,
1256 * but the hub can/will return fewer bytes here.
1258 ret
= get_hub_descriptor(hdev
, hub
->descriptor
);
1260 message
= "can't read hub descriptor";
1262 } else if (hub
->descriptor
->bNbrPorts
> USB_MAXCHILDREN
) {
1263 message
= "hub has too many ports!";
1268 hdev
->maxchild
= hub
->descriptor
->bNbrPorts
;
1269 dev_info (hub_dev
, "%d port%s detected\n", hdev
->maxchild
,
1270 (hdev
->maxchild
== 1) ? "" : "s");
1272 hdev
->children
= kzalloc(hdev
->maxchild
*
1273 sizeof(struct usb_device
*), GFP_KERNEL
);
1274 hub
->port_owners
= kzalloc(hdev
->maxchild
* sizeof(void *), GFP_KERNEL
);
1275 if (!hdev
->children
|| !hub
->port_owners
) {
1280 wHubCharacteristics
= le16_to_cpu(hub
->descriptor
->wHubCharacteristics
);
1282 /* FIXME for USB 3.0, skip for now */
1283 if ((wHubCharacteristics
& HUB_CHAR_COMPOUND
) &&
1284 !(hub_is_superspeed(hdev
))) {
1286 char portstr
[USB_MAXCHILDREN
+ 1];
1288 for (i
= 0; i
< hdev
->maxchild
; i
++)
1289 portstr
[i
] = hub
->descriptor
->u
.hs
.DeviceRemovable
1290 [((i
+ 1) / 8)] & (1 << ((i
+ 1) % 8))
1292 portstr
[hdev
->maxchild
] = 0;
1293 dev_dbg(hub_dev
, "compound device; port removable status: %s\n", portstr
);
1295 dev_dbg(hub_dev
, "standalone hub\n");
1297 switch (wHubCharacteristics
& HUB_CHAR_LPSM
) {
1298 case HUB_CHAR_COMMON_LPSM
:
1299 dev_dbg(hub_dev
, "ganged power switching\n");
1301 case HUB_CHAR_INDV_PORT_LPSM
:
1302 dev_dbg(hub_dev
, "individual port power switching\n");
1304 case HUB_CHAR_NO_LPSM
:
1306 dev_dbg(hub_dev
, "no power switching (usb 1.0)\n");
1310 switch (wHubCharacteristics
& HUB_CHAR_OCPM
) {
1311 case HUB_CHAR_COMMON_OCPM
:
1312 dev_dbg(hub_dev
, "global over-current protection\n");
1314 case HUB_CHAR_INDV_PORT_OCPM
:
1315 dev_dbg(hub_dev
, "individual port over-current protection\n");
1317 case HUB_CHAR_NO_OCPM
:
1319 dev_dbg(hub_dev
, "no over-current protection\n");
1323 spin_lock_init (&hub
->tt
.lock
);
1324 INIT_LIST_HEAD (&hub
->tt
.clear_list
);
1325 INIT_WORK(&hub
->tt
.clear_work
, hub_tt_work
);
1326 switch (hdev
->descriptor
.bDeviceProtocol
) {
1329 case USB_HUB_PR_HS_SINGLE_TT
:
1330 dev_dbg(hub_dev
, "Single TT\n");
1333 case USB_HUB_PR_HS_MULTI_TT
:
1334 ret
= usb_set_interface(hdev
, 0, 1);
1336 dev_dbg(hub_dev
, "TT per port\n");
1339 dev_err(hub_dev
, "Using single TT (err %d)\n",
1344 /* USB 3.0 hubs don't have a TT */
1347 dev_dbg(hub_dev
, "Unrecognized hub protocol %d\n",
1348 hdev
->descriptor
.bDeviceProtocol
);
1352 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
1353 switch (wHubCharacteristics
& HUB_CHAR_TTTT
) {
1354 case HUB_TTTT_8_BITS
:
1355 if (hdev
->descriptor
.bDeviceProtocol
!= 0) {
1356 hub
->tt
.think_time
= 666;
1357 dev_dbg(hub_dev
, "TT requires at most %d "
1358 "FS bit times (%d ns)\n",
1359 8, hub
->tt
.think_time
);
1362 case HUB_TTTT_16_BITS
:
1363 hub
->tt
.think_time
= 666 * 2;
1364 dev_dbg(hub_dev
, "TT requires at most %d "
1365 "FS bit times (%d ns)\n",
1366 16, hub
->tt
.think_time
);
1368 case HUB_TTTT_24_BITS
:
1369 hub
->tt
.think_time
= 666 * 3;
1370 dev_dbg(hub_dev
, "TT requires at most %d "
1371 "FS bit times (%d ns)\n",
1372 24, hub
->tt
.think_time
);
1374 case HUB_TTTT_32_BITS
:
1375 hub
->tt
.think_time
= 666 * 4;
1376 dev_dbg(hub_dev
, "TT requires at most %d "
1377 "FS bit times (%d ns)\n",
1378 32, hub
->tt
.think_time
);
1382 /* probe() zeroes hub->indicator[] */
1383 if (wHubCharacteristics
& HUB_CHAR_PORTIND
) {
1384 hub
->has_indicators
= 1;
1385 dev_dbg(hub_dev
, "Port indicators are supported\n");
1388 dev_dbg(hub_dev
, "power on to power good time: %dms\n",
1389 hub
->descriptor
->bPwrOn2PwrGood
* 2);
1391 /* power budgeting mostly matters with bus-powered hubs,
1392 * and battery-powered root hubs (may provide just 8 mA).
1394 ret
= usb_get_status(hdev
, USB_RECIP_DEVICE
, 0, &hubstatus
);
1396 message
= "can't get hub status";
1399 le16_to_cpus(&hubstatus
);
1400 if (hdev
== hdev
->bus
->root_hub
) {
1401 if (hdev
->bus_mA
== 0 || hdev
->bus_mA
>= 500)
1402 hub
->mA_per_port
= 500;
1404 hub
->mA_per_port
= hdev
->bus_mA
;
1405 hub
->limited_power
= 1;
1407 } else if ((hubstatus
& (1 << USB_DEVICE_SELF_POWERED
)) == 0) {
1408 dev_dbg(hub_dev
, "hub controller current requirement: %dmA\n",
1409 hub
->descriptor
->bHubContrCurrent
);
1410 hub
->limited_power
= 1;
1411 if (hdev
->maxchild
> 0) {
1412 int remaining
= hdev
->bus_mA
-
1413 hub
->descriptor
->bHubContrCurrent
;
1415 if (remaining
< hdev
->maxchild
* 100)
1417 "insufficient power available "
1418 "to use all downstream ports\n");
1419 hub
->mA_per_port
= 100; /* 7.2.1.1 */
1421 } else { /* Self-powered external hub */
1422 /* FIXME: What about battery-powered external hubs that
1423 * provide less current per port? */
1424 hub
->mA_per_port
= 500;
1426 if (hub
->mA_per_port
< 500)
1427 dev_dbg(hub_dev
, "%umA bus power budget for each child\n",
1430 /* Update the HCD's internal representation of this hub before khubd
1431 * starts getting port status changes for devices under the hub.
1433 hcd
= bus_to_hcd(hdev
->bus
);
1434 if (hcd
->driver
->update_hub_device
) {
1435 ret
= hcd
->driver
->update_hub_device(hcd
, hdev
,
1436 &hub
->tt
, GFP_KERNEL
);
1438 message
= "can't update HCD hub info";
1443 ret
= hub_hub_status(hub
, &hubstatus
, &hubchange
);
1445 message
= "can't get hub status";
1449 /* local power status reports aren't always correct */
1450 if (hdev
->actconfig
->desc
.bmAttributes
& USB_CONFIG_ATT_SELFPOWER
)
1451 dev_dbg(hub_dev
, "local power source is %s\n",
1452 (hubstatus
& HUB_STATUS_LOCAL_POWER
)
1453 ? "lost (inactive)" : "good");
1455 if ((wHubCharacteristics
& HUB_CHAR_OCPM
) == 0)
1456 dev_dbg(hub_dev
, "%sover-current condition exists\n",
1457 (hubstatus
& HUB_STATUS_OVERCURRENT
) ? "" : "no ");
1459 /* set up the interrupt endpoint
1460 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1461 * bytes as USB2.0[11.12.3] says because some hubs are known
1462 * to send more data (and thus cause overflow). For root hubs,
1463 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1464 * to be big enough for at least USB_MAXCHILDREN ports. */
1465 pipe
= usb_rcvintpipe(hdev
, endpoint
->bEndpointAddress
);
1466 maxp
= usb_maxpacket(hdev
, pipe
, usb_pipeout(pipe
));
1468 if (maxp
> sizeof(*hub
->buffer
))
1469 maxp
= sizeof(*hub
->buffer
);
1471 hub
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
1477 usb_fill_int_urb(hub
->urb
, hdev
, pipe
, *hub
->buffer
, maxp
, hub_irq
,
1478 hub
, endpoint
->bInterval
);
1480 /* maybe cycle the hub leds */
1481 if (hub
->has_indicators
&& blinkenlights
)
1482 hub
->indicator
[0] = INDICATOR_CYCLE
;
1484 hub_activate(hub
, HUB_INIT
);
1488 dev_err (hub_dev
, "config failed, %s (err %d)\n",
1490 /* hub_disconnect() frees urb and descriptor */
1494 static void hub_release(struct kref
*kref
)
1496 struct usb_hub
*hub
= container_of(kref
, struct usb_hub
, kref
);
1498 usb_put_intf(to_usb_interface(hub
->intfdev
));
1502 static unsigned highspeed_hubs
;
1504 static void hub_disconnect(struct usb_interface
*intf
)
1506 struct usb_hub
*hub
= usb_get_intfdata(intf
);
1507 struct usb_device
*hdev
= interface_to_usbdev(intf
);
1509 /* Take the hub off the event list and don't let it be added again */
1510 spin_lock_irq(&hub_event_lock
);
1511 if (!list_empty(&hub
->event_list
)) {
1512 list_del_init(&hub
->event_list
);
1513 usb_autopm_put_interface_no_suspend(intf
);
1515 hub
->disconnected
= 1;
1516 spin_unlock_irq(&hub_event_lock
);
1518 /* Disconnect all children and quiesce the hub */
1520 hub_quiesce(hub
, HUB_DISCONNECT
);
1522 usb_set_intfdata (intf
, NULL
);
1523 hub
->hdev
->maxchild
= 0;
1525 if (hub
->hdev
->speed
== USB_SPEED_HIGH
)
1528 usb_free_urb(hub
->urb
);
1529 kfree(hdev
->children
);
1530 kfree(hub
->port_owners
);
1531 kfree(hub
->descriptor
);
1535 kref_put(&hub
->kref
, hub_release
);
1538 static int hub_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1540 struct usb_host_interface
*desc
;
1541 struct usb_endpoint_descriptor
*endpoint
;
1542 struct usb_device
*hdev
;
1543 struct usb_hub
*hub
;
1545 desc
= intf
->cur_altsetting
;
1546 hdev
= interface_to_usbdev(intf
);
1548 /* Hubs have proper suspend/resume support. */
1549 usb_enable_autosuspend(hdev
);
1551 if (hdev
->level
== MAX_TOPO_LEVEL
) {
1553 "Unsupported bus topology: hub nested too deep\n");
1557 #ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1559 dev_warn(&intf
->dev
, "ignoring external hub\n");
1564 /* Some hubs have a subclass of 1, which AFAICT according to the */
1565 /* specs is not defined, but it works */
1566 if ((desc
->desc
.bInterfaceSubClass
!= 0) &&
1567 (desc
->desc
.bInterfaceSubClass
!= 1)) {
1569 dev_err (&intf
->dev
, "bad descriptor, ignoring hub\n");
1573 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1574 if (desc
->desc
.bNumEndpoints
!= 1)
1575 goto descriptor_error
;
1577 endpoint
= &desc
->endpoint
[0].desc
;
1579 /* If it's not an interrupt in endpoint, we'd better punt! */
1580 if (!usb_endpoint_is_int_in(endpoint
))
1581 goto descriptor_error
;
1583 /* We found a hub */
1584 dev_info (&intf
->dev
, "USB hub found\n");
1586 hub
= kzalloc(sizeof(*hub
), GFP_KERNEL
);
1588 dev_dbg (&intf
->dev
, "couldn't kmalloc hub struct\n");
1592 kref_init(&hub
->kref
);
1593 INIT_LIST_HEAD(&hub
->event_list
);
1594 hub
->intfdev
= &intf
->dev
;
1596 INIT_DELAYED_WORK(&hub
->leds
, led_work
);
1597 INIT_DELAYED_WORK(&hub
->init_work
, NULL
);
1600 usb_set_intfdata (intf
, hub
);
1601 intf
->needs_remote_wakeup
= 1;
1603 if (hdev
->speed
== USB_SPEED_HIGH
)
1606 if (hub_configure(hub
, endpoint
) >= 0)
1609 hub_disconnect (intf
);
1614 hub_ioctl(struct usb_interface
*intf
, unsigned int code
, void *user_data
)
1616 struct usb_device
*hdev
= interface_to_usbdev (intf
);
1618 /* assert ifno == 0 (part of hub spec) */
1620 case USBDEVFS_HUB_PORTINFO
: {
1621 struct usbdevfs_hub_portinfo
*info
= user_data
;
1624 spin_lock_irq(&device_state_lock
);
1625 if (hdev
->devnum
<= 0)
1628 info
->nports
= hdev
->maxchild
;
1629 for (i
= 0; i
< info
->nports
; i
++) {
1630 if (hdev
->children
[i
] == NULL
)
1634 hdev
->children
[i
]->devnum
;
1637 spin_unlock_irq(&device_state_lock
);
1639 return info
->nports
+ 1;
1648 * Allow user programs to claim ports on a hub. When a device is attached
1649 * to one of these "claimed" ports, the program will "own" the device.
1651 static int find_port_owner(struct usb_device
*hdev
, unsigned port1
,
1654 if (hdev
->state
== USB_STATE_NOTATTACHED
)
1656 if (port1
== 0 || port1
> hdev
->maxchild
)
1659 /* This assumes that devices not managed by the hub driver
1660 * will always have maxchild equal to 0.
1662 *ppowner
= &(hdev_to_hub(hdev
)->port_owners
[port1
- 1]);
1666 /* In the following three functions, the caller must hold hdev's lock */
1667 int usb_hub_claim_port(struct usb_device
*hdev
, unsigned port1
, void *owner
)
1672 rc
= find_port_owner(hdev
, port1
, &powner
);
1681 int usb_hub_release_port(struct usb_device
*hdev
, unsigned port1
, void *owner
)
1686 rc
= find_port_owner(hdev
, port1
, &powner
);
1689 if (*powner
!= owner
)
1695 void usb_hub_release_all_ports(struct usb_device
*hdev
, void *owner
)
1700 n
= find_port_owner(hdev
, 1, &powner
);
1702 for (; n
< hdev
->maxchild
; (++n
, ++powner
)) {
1703 if (*powner
== owner
)
1709 /* The caller must hold udev's lock */
1710 bool usb_device_is_owned(struct usb_device
*udev
)
1712 struct usb_hub
*hub
;
1714 if (udev
->state
== USB_STATE_NOTATTACHED
|| !udev
->parent
)
1716 hub
= hdev_to_hub(udev
->parent
);
1717 return !!hub
->port_owners
[udev
->portnum
- 1];
1721 static void recursively_mark_NOTATTACHED(struct usb_device
*udev
)
1725 for (i
= 0; i
< udev
->maxchild
; ++i
) {
1726 if (udev
->children
[i
])
1727 recursively_mark_NOTATTACHED(udev
->children
[i
]);
1729 if (udev
->state
== USB_STATE_SUSPENDED
)
1730 udev
->active_duration
-= jiffies
;
1731 udev
->state
= USB_STATE_NOTATTACHED
;
1735 * usb_set_device_state - change a device's current state (usbcore, hcds)
1736 * @udev: pointer to device whose state should be changed
1737 * @new_state: new state value to be stored
1739 * udev->state is _not_ fully protected by the device lock. Although
1740 * most transitions are made only while holding the lock, the state can
1741 * can change to USB_STATE_NOTATTACHED at almost any time. This
1742 * is so that devices can be marked as disconnected as soon as possible,
1743 * without having to wait for any semaphores to be released. As a result,
1744 * all changes to any device's state must be protected by the
1745 * device_state_lock spinlock.
1747 * Once a device has been added to the device tree, all changes to its state
1748 * should be made using this routine. The state should _not_ be set directly.
1750 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1751 * Otherwise udev->state is set to new_state, and if new_state is
1752 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1753 * to USB_STATE_NOTATTACHED.
1755 void usb_set_device_state(struct usb_device
*udev
,
1756 enum usb_device_state new_state
)
1758 unsigned long flags
;
1761 spin_lock_irqsave(&device_state_lock
, flags
);
1762 if (udev
->state
== USB_STATE_NOTATTACHED
)
1764 else if (new_state
!= USB_STATE_NOTATTACHED
) {
1766 /* root hub wakeup capabilities are managed out-of-band
1767 * and may involve silicon errata ... ignore them here.
1770 if (udev
->state
== USB_STATE_SUSPENDED
1771 || new_state
== USB_STATE_SUSPENDED
)
1772 ; /* No change to wakeup settings */
1773 else if (new_state
== USB_STATE_CONFIGURED
)
1774 wakeup
= udev
->actconfig
->desc
.bmAttributes
1775 & USB_CONFIG_ATT_WAKEUP
;
1779 if (udev
->state
== USB_STATE_SUSPENDED
&&
1780 new_state
!= USB_STATE_SUSPENDED
)
1781 udev
->active_duration
-= jiffies
;
1782 else if (new_state
== USB_STATE_SUSPENDED
&&
1783 udev
->state
!= USB_STATE_SUSPENDED
)
1784 udev
->active_duration
+= jiffies
;
1785 udev
->state
= new_state
;
1787 recursively_mark_NOTATTACHED(udev
);
1788 spin_unlock_irqrestore(&device_state_lock
, flags
);
1790 device_set_wakeup_capable(&udev
->dev
, wakeup
);
1792 EXPORT_SYMBOL_GPL(usb_set_device_state
);
1795 * Choose a device number.
1797 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1798 * USB-2.0 buses they are also used as device addresses, however on
1799 * USB-3.0 buses the address is assigned by the controller hardware
1800 * and it usually is not the same as the device number.
1802 * WUSB devices are simple: they have no hubs behind, so the mapping
1803 * device <-> virtual port number becomes 1:1. Why? to simplify the
1804 * life of the device connection logic in
1805 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1806 * handshake we need to assign a temporary address in the unauthorized
1807 * space. For simplicity we use the first virtual port number found to
1808 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1809 * and that becomes it's address [X < 128] or its unauthorized address
1812 * We add 1 as an offset to the one-based USB-stack port number
1813 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1814 * 0 is reserved by USB for default address; (b) Linux's USB stack
1815 * uses always #1 for the root hub of the controller. So USB stack's
1816 * port #1, which is wusb virtual-port #0 has address #2.
1818 * Devices connected under xHCI are not as simple. The host controller
1819 * supports virtualization, so the hardware assigns device addresses and
1820 * the HCD must setup data structures before issuing a set address
1821 * command to the hardware.
1823 static void choose_devnum(struct usb_device
*udev
)
1826 struct usb_bus
*bus
= udev
->bus
;
1828 /* If khubd ever becomes multithreaded, this will need a lock */
1830 devnum
= udev
->portnum
+ 1;
1831 BUG_ON(test_bit(devnum
, bus
->devmap
.devicemap
));
1833 /* Try to allocate the next devnum beginning at
1834 * bus->devnum_next. */
1835 devnum
= find_next_zero_bit(bus
->devmap
.devicemap
, 128,
1838 devnum
= find_next_zero_bit(bus
->devmap
.devicemap
,
1840 bus
->devnum_next
= ( devnum
>= 127 ? 1 : devnum
+ 1);
1843 set_bit(devnum
, bus
->devmap
.devicemap
);
1844 udev
->devnum
= devnum
;
1848 static void release_devnum(struct usb_device
*udev
)
1850 if (udev
->devnum
> 0) {
1851 clear_bit(udev
->devnum
, udev
->bus
->devmap
.devicemap
);
1856 static void update_devnum(struct usb_device
*udev
, int devnum
)
1858 /* The address for a WUSB device is managed by wusbcore. */
1860 udev
->devnum
= devnum
;
1863 static void hub_free_dev(struct usb_device
*udev
)
1865 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
1867 /* Root hubs aren't real devices, so don't free HCD resources */
1868 if (hcd
->driver
->free_dev
&& udev
->parent
)
1869 hcd
->driver
->free_dev(hcd
, udev
);
1873 * usb_disconnect - disconnect a device (usbcore-internal)
1874 * @pdev: pointer to device being disconnected
1875 * Context: !in_interrupt ()
1877 * Something got disconnected. Get rid of it and all of its children.
1879 * If *pdev is a normal device then the parent hub must already be locked.
1880 * If *pdev is a root hub then this routine will acquire the
1881 * usb_bus_list_lock on behalf of the caller.
1883 * Only hub drivers (including virtual root hub drivers for host
1884 * controllers) should ever call this.
1886 * This call is synchronous, and may not be used in an interrupt context.
1888 void usb_disconnect(struct usb_device
**pdev
)
1890 struct usb_device
*udev
= *pdev
;
1893 /* mark the device as inactive, so any further urb submissions for
1894 * this device (and any of its children) will fail immediately.
1895 * this quiesces everything except pending urbs.
1897 usb_set_device_state(udev
, USB_STATE_NOTATTACHED
);
1898 dev_info(&udev
->dev
, "USB disconnect, device number %d\n",
1901 usb_lock_device(udev
);
1903 /* Free up all the children before we remove this device */
1904 for (i
= 0; i
< udev
->maxchild
; i
++) {
1905 if (udev
->children
[i
])
1906 usb_disconnect(&udev
->children
[i
]);
1909 /* deallocate hcd/hardware state ... nuking all pending urbs and
1910 * cleaning up all state associated with the current configuration
1911 * so that the hardware is now fully quiesced.
1913 dev_dbg (&udev
->dev
, "unregistering device\n");
1914 usb_disable_device(udev
, 0);
1915 usb_hcd_synchronize_unlinks(udev
);
1917 usb_remove_ep_devs(&udev
->ep0
);
1918 usb_unlock_device(udev
);
1920 /* Unregister the device. The device driver is responsible
1921 * for de-configuring the device and invoking the remove-device
1922 * notifier chain (used by usbfs and possibly others).
1924 device_del(&udev
->dev
);
1926 /* Free the device number and delete the parent's children[]
1927 * (or root_hub) pointer.
1929 release_devnum(udev
);
1931 /* Avoid races with recursively_mark_NOTATTACHED() */
1932 spin_lock_irq(&device_state_lock
);
1934 spin_unlock_irq(&device_state_lock
);
1938 put_device(&udev
->dev
);
1941 #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
1942 static void show_string(struct usb_device
*udev
, char *id
, char *string
)
1946 dev_printk(KERN_INFO
, &udev
->dev
, "%s: %s\n", id
, string
);
1949 static void announce_device(struct usb_device
*udev
)
1951 dev_info(&udev
->dev
, "New USB device found, idVendor=%04x, idProduct=%04x\n",
1952 le16_to_cpu(udev
->descriptor
.idVendor
),
1953 le16_to_cpu(udev
->descriptor
.idProduct
));
1954 dev_info(&udev
->dev
,
1955 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
1956 udev
->descriptor
.iManufacturer
,
1957 udev
->descriptor
.iProduct
,
1958 udev
->descriptor
.iSerialNumber
);
1959 show_string(udev
, "Product", udev
->product
);
1960 show_string(udev
, "Manufacturer", udev
->manufacturer
);
1961 show_string(udev
, "SerialNumber", udev
->serial
);
1964 static inline void announce_device(struct usb_device
*udev
) { }
1967 #ifdef CONFIG_USB_OTG
1968 #include "otg_whitelist.h"
1972 * usb_enumerate_device_otg - FIXME (usbcore-internal)
1973 * @udev: newly addressed device (in ADDRESS state)
1975 * Finish enumeration for On-The-Go devices
1977 static int usb_enumerate_device_otg(struct usb_device
*udev
)
1981 #ifdef CONFIG_USB_OTG
1983 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
1984 * to wake us after we've powered off VBUS; and HNP, switching roles
1985 * "host" to "peripheral". The OTG descriptor helps figure this out.
1987 if (!udev
->bus
->is_b_host
1989 && udev
->parent
== udev
->bus
->root_hub
) {
1990 struct usb_otg_descriptor
*desc
= NULL
;
1991 struct usb_bus
*bus
= udev
->bus
;
1993 /* descriptor may appear anywhere in config */
1994 if (__usb_get_extra_descriptor (udev
->rawdescriptors
[0],
1995 le16_to_cpu(udev
->config
[0].desc
.wTotalLength
),
1996 USB_DT_OTG
, (void **) &desc
) == 0) {
1997 if (desc
->bmAttributes
& USB_OTG_HNP
) {
1998 unsigned port1
= udev
->portnum
;
2000 dev_info(&udev
->dev
,
2001 "Dual-Role OTG device on %sHNP port\n",
2002 (port1
== bus
->otg_port
)
2005 /* enable HNP before suspend, it's simpler */
2006 if (port1
== bus
->otg_port
)
2007 bus
->b_hnp_enable
= 1;
2008 err
= usb_control_msg(udev
,
2009 usb_sndctrlpipe(udev
, 0),
2010 USB_REQ_SET_FEATURE
, 0,
2012 ? USB_DEVICE_B_HNP_ENABLE
2013 : USB_DEVICE_A_ALT_HNP_SUPPORT
,
2014 0, NULL
, 0, USB_CTRL_SET_TIMEOUT
);
2016 /* OTG MESSAGE: report errors here,
2017 * customize to match your product.
2019 dev_info(&udev
->dev
,
2020 "can't set HNP mode: %d\n",
2022 bus
->b_hnp_enable
= 0;
2028 if (!is_targeted(udev
)) {
2030 /* Maybe it can talk to us, though we can't talk to it.
2031 * (Includes HNP test device.)
2033 if (udev
->bus
->b_hnp_enable
|| udev
->bus
->is_b_host
) {
2034 err
= usb_port_suspend(udev
, PMSG_SUSPEND
);
2036 dev_dbg(&udev
->dev
, "HNP fail, %d\n", err
);
2048 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
2049 * @udev: newly addressed device (in ADDRESS state)
2051 * This is only called by usb_new_device() and usb_authorize_device()
2052 * and FIXME -- all comments that apply to them apply here wrt to
2055 * If the device is WUSB and not authorized, we don't attempt to read
2056 * the string descriptors, as they will be errored out by the device
2057 * until it has been authorized.
2059 static int usb_enumerate_device(struct usb_device
*udev
)
2063 if (udev
->config
== NULL
) {
2064 err
= usb_get_configuration(udev
);
2066 dev_err(&udev
->dev
, "can't read configurations, error %d\n",
2071 if (udev
->wusb
== 1 && udev
->authorized
== 0) {
2072 udev
->product
= kstrdup("n/a (unauthorized)", GFP_KERNEL
);
2073 udev
->manufacturer
= kstrdup("n/a (unauthorized)", GFP_KERNEL
);
2074 udev
->serial
= kstrdup("n/a (unauthorized)", GFP_KERNEL
);
2077 /* read the standard strings and cache them if present */
2078 udev
->product
= usb_cache_string(udev
, udev
->descriptor
.iProduct
);
2079 udev
->manufacturer
= usb_cache_string(udev
,
2080 udev
->descriptor
.iManufacturer
);
2081 udev
->serial
= usb_cache_string(udev
, udev
->descriptor
.iSerialNumber
);
2083 err
= usb_enumerate_device_otg(udev
);
2088 static void set_usb_port_removable(struct usb_device
*udev
)
2090 struct usb_device
*hdev
= udev
->parent
;
2091 struct usb_hub
*hub
;
2092 u8 port
= udev
->portnum
;
2093 u16 wHubCharacteristics
;
2094 bool removable
= true;
2099 hub
= hdev_to_hub(udev
->parent
);
2101 wHubCharacteristics
= le16_to_cpu(hub
->descriptor
->wHubCharacteristics
);
2103 if (!(wHubCharacteristics
& HUB_CHAR_COMPOUND
))
2106 if (hub_is_superspeed(hdev
)) {
2107 if (hub
->descriptor
->u
.ss
.DeviceRemovable
& (1 << port
))
2110 if (hub
->descriptor
->u
.hs
.DeviceRemovable
[port
/ 8] & (1 << (port
% 8)))
2115 udev
->removable
= USB_DEVICE_REMOVABLE
;
2117 udev
->removable
= USB_DEVICE_FIXED
;
2121 * usb_new_device - perform initial device setup (usbcore-internal)
2122 * @udev: newly addressed device (in ADDRESS state)
2124 * This is called with devices which have been detected but not fully
2125 * enumerated. The device descriptor is available, but not descriptors
2126 * for any device configuration. The caller must have locked either
2127 * the parent hub (if udev is a normal device) or else the
2128 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2129 * udev has already been installed, but udev is not yet visible through
2130 * sysfs or other filesystem code.
2132 * It will return if the device is configured properly or not. Zero if
2133 * the interface was registered with the driver core; else a negative
2136 * This call is synchronous, and may not be used in an interrupt context.
2138 * Only the hub driver or root-hub registrar should ever call this.
2140 int usb_new_device(struct usb_device
*udev
)
2145 /* Initialize non-root-hub device wakeup to disabled;
2146 * device (un)configuration controls wakeup capable
2147 * sysfs power/wakeup controls wakeup enabled/disabled
2149 device_init_wakeup(&udev
->dev
, 0);
2152 /* Tell the runtime-PM framework the device is active */
2153 pm_runtime_set_active(&udev
->dev
);
2154 pm_runtime_get_noresume(&udev
->dev
);
2155 pm_runtime_use_autosuspend(&udev
->dev
);
2156 pm_runtime_enable(&udev
->dev
);
2158 /* By default, forbid autosuspend for all devices. It will be
2159 * allowed for hubs during binding.
2161 usb_disable_autosuspend(udev
);
2163 err
= usb_enumerate_device(udev
); /* Read descriptors */
2166 dev_dbg(&udev
->dev
, "udev %d, busnum %d, minor = %d\n",
2167 udev
->devnum
, udev
->bus
->busnum
,
2168 (((udev
->bus
->busnum
-1) * 128) + (udev
->devnum
-1)));
2169 /* export the usbdev device-node for libusb */
2170 udev
->dev
.devt
= MKDEV(USB_DEVICE_MAJOR
,
2171 (((udev
->bus
->busnum
-1) * 128) + (udev
->devnum
-1)));
2173 /* Tell the world! */
2174 announce_device(udev
);
2176 device_enable_async_suspend(&udev
->dev
);
2179 * check whether the hub marks this port as non-removable. Do it
2180 * now so that platform-specific data can override it in
2184 set_usb_port_removable(udev
);
2186 /* Register the device. The device driver is responsible
2187 * for configuring the device and invoking the add-device
2188 * notifier chain (used by usbfs and possibly others).
2190 err
= device_add(&udev
->dev
);
2192 dev_err(&udev
->dev
, "can't device_add, error %d\n", err
);
2196 (void) usb_create_ep_devs(&udev
->dev
, &udev
->ep0
, udev
);
2197 usb_mark_last_busy(udev
);
2198 pm_runtime_put_sync_autosuspend(&udev
->dev
);
2202 usb_set_device_state(udev
, USB_STATE_NOTATTACHED
);
2203 pm_runtime_disable(&udev
->dev
);
2204 pm_runtime_set_suspended(&udev
->dev
);
2210 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2211 * @usb_dev: USB device
2213 * Move the USB device to a very basic state where interfaces are disabled
2214 * and the device is in fact unconfigured and unusable.
2216 * We share a lock (that we have) with device_del(), so we need to
2219 int usb_deauthorize_device(struct usb_device
*usb_dev
)
2221 usb_lock_device(usb_dev
);
2222 if (usb_dev
->authorized
== 0)
2223 goto out_unauthorized
;
2225 usb_dev
->authorized
= 0;
2226 usb_set_configuration(usb_dev
, -1);
2228 kfree(usb_dev
->product
);
2229 usb_dev
->product
= kstrdup("n/a (unauthorized)", GFP_KERNEL
);
2230 kfree(usb_dev
->manufacturer
);
2231 usb_dev
->manufacturer
= kstrdup("n/a (unauthorized)", GFP_KERNEL
);
2232 kfree(usb_dev
->serial
);
2233 usb_dev
->serial
= kstrdup("n/a (unauthorized)", GFP_KERNEL
);
2235 usb_destroy_configuration(usb_dev
);
2236 usb_dev
->descriptor
.bNumConfigurations
= 0;
2239 usb_unlock_device(usb_dev
);
2244 int usb_authorize_device(struct usb_device
*usb_dev
)
2248 usb_lock_device(usb_dev
);
2249 if (usb_dev
->authorized
== 1)
2250 goto out_authorized
;
2252 result
= usb_autoresume_device(usb_dev
);
2254 dev_err(&usb_dev
->dev
,
2255 "can't autoresume for authorization: %d\n", result
);
2256 goto error_autoresume
;
2258 result
= usb_get_device_descriptor(usb_dev
, sizeof(usb_dev
->descriptor
));
2260 dev_err(&usb_dev
->dev
, "can't re-read device descriptor for "
2261 "authorization: %d\n", result
);
2262 goto error_device_descriptor
;
2265 kfree(usb_dev
->product
);
2266 usb_dev
->product
= NULL
;
2267 kfree(usb_dev
->manufacturer
);
2268 usb_dev
->manufacturer
= NULL
;
2269 kfree(usb_dev
->serial
);
2270 usb_dev
->serial
= NULL
;
2272 usb_dev
->authorized
= 1;
2273 result
= usb_enumerate_device(usb_dev
);
2275 goto error_enumerate
;
2276 /* Choose and set the configuration. This registers the interfaces
2277 * with the driver core and lets interface drivers bind to them.
2279 c
= usb_choose_configuration(usb_dev
);
2281 result
= usb_set_configuration(usb_dev
, c
);
2283 dev_err(&usb_dev
->dev
,
2284 "can't set config #%d, error %d\n", c
, result
);
2285 /* This need not be fatal. The user can try to
2286 * set other configurations. */
2289 dev_info(&usb_dev
->dev
, "authorized to connect\n");
2292 error_device_descriptor
:
2293 usb_autosuspend_device(usb_dev
);
2296 usb_unlock_device(usb_dev
); // complements locktree
2301 /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2302 static unsigned hub_is_wusb(struct usb_hub
*hub
)
2304 struct usb_hcd
*hcd
;
2305 if (hub
->hdev
->parent
!= NULL
) /* not a root hub? */
2307 hcd
= container_of(hub
->hdev
->bus
, struct usb_hcd
, self
);
2308 return hcd
->wireless
;
2312 #define PORT_RESET_TRIES 5
2313 #define SET_ADDRESS_TRIES 2
2314 #define GET_DESCRIPTOR_TRIES 2
2315 #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
2316 #define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
2318 #define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2319 #define HUB_SHORT_RESET_TIME 10
2320 #define HUB_BH_RESET_TIME 50
2321 #define HUB_LONG_RESET_TIME 200
2322 #define HUB_RESET_TIMEOUT 500
2324 static int hub_port_reset(struct usb_hub
*hub
, int port1
,
2325 struct usb_device
*udev
, unsigned int delay
, bool warm
);
2327 /* Is a USB 3.0 port in the Inactive state? */
2328 static bool hub_port_inactive(struct usb_hub
*hub
, u16 portstatus
)
2330 return hub_is_superspeed(hub
->hdev
) &&
2331 (portstatus
& USB_PORT_STAT_LINK_STATE
) ==
2332 USB_SS_PORT_LS_SS_INACTIVE
;
2335 static int hub_port_wait_reset(struct usb_hub
*hub
, int port1
,
2336 struct usb_device
*udev
, unsigned int delay
, bool warm
)
2338 int delay_time
, ret
;
2342 for (delay_time
= 0;
2343 delay_time
< HUB_RESET_TIMEOUT
;
2344 delay_time
+= delay
) {
2345 /* wait to give the device a chance to reset */
2348 /* read and decode port status */
2349 ret
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
2354 * Some buggy devices require a warm reset to be issued even
2355 * when the port appears not to be connected.
2359 * Some buggy devices can cause an NEC host controller
2360 * to transition to the "Error" state after a hot port
2361 * reset. This will show up as the port state in
2362 * "Inactive", and the port may also report a
2363 * disconnect. Forcing a warm port reset seems to make
2366 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2368 if (hub_port_inactive(hub
, portstatus
)) {
2371 if ((portchange
& USB_PORT_STAT_C_CONNECTION
))
2372 clear_port_feature(hub
->hdev
, port1
,
2373 USB_PORT_FEAT_C_CONNECTION
);
2374 if (portchange
& USB_PORT_STAT_C_LINK_STATE
)
2375 clear_port_feature(hub
->hdev
, port1
,
2376 USB_PORT_FEAT_C_PORT_LINK_STATE
);
2377 if (portchange
& USB_PORT_STAT_C_RESET
)
2378 clear_port_feature(hub
->hdev
, port1
,
2379 USB_PORT_FEAT_C_RESET
);
2380 dev_dbg(hub
->intfdev
, "hot reset failed, warm reset port %d\n",
2382 ret
= hub_port_reset(hub
, port1
,
2383 udev
, HUB_BH_RESET_TIME
,
2385 if ((portchange
& USB_PORT_STAT_C_CONNECTION
))
2386 clear_port_feature(hub
->hdev
, port1
,
2387 USB_PORT_FEAT_C_CONNECTION
);
2390 /* Device went away? */
2391 if (!(portstatus
& USB_PORT_STAT_CONNECTION
))
2394 /* bomb out completely if the connection bounced */
2395 if ((portchange
& USB_PORT_STAT_C_CONNECTION
))
2398 /* if we`ve finished resetting, then break out of
2401 if (!(portstatus
& USB_PORT_STAT_RESET
) &&
2402 (portstatus
& USB_PORT_STAT_ENABLE
)) {
2403 if (hub_is_wusb(hub
))
2404 udev
->speed
= USB_SPEED_WIRELESS
;
2405 else if (hub_is_superspeed(hub
->hdev
))
2406 udev
->speed
= USB_SPEED_SUPER
;
2407 else if (portstatus
& USB_PORT_STAT_HIGH_SPEED
)
2408 udev
->speed
= USB_SPEED_HIGH
;
2409 else if (portstatus
& USB_PORT_STAT_LOW_SPEED
)
2410 udev
->speed
= USB_SPEED_LOW
;
2412 udev
->speed
= USB_SPEED_FULL
;
2416 if (portchange
& USB_PORT_STAT_C_BH_RESET
)
2420 /* switch to the long delay after two short delay failures */
2421 if (delay_time
>= 2 * HUB_SHORT_RESET_TIME
)
2422 delay
= HUB_LONG_RESET_TIME
;
2424 dev_dbg (hub
->intfdev
,
2425 "port %d not %sreset yet, waiting %dms\n",
2426 port1
, warm
? "warm " : "", delay
);
2432 static void hub_port_finish_reset(struct usb_hub
*hub
, int port1
,
2433 struct usb_device
*udev
, int *status
, bool warm
)
2438 struct usb_hcd
*hcd
;
2439 /* TRSTRCY = 10 ms; plus some extra */
2441 update_devnum(udev
, 0);
2442 hcd
= bus_to_hcd(udev
->bus
);
2443 if (hcd
->driver
->reset_device
) {
2444 *status
= hcd
->driver
->reset_device(hcd
, udev
);
2446 dev_err(&udev
->dev
, "Cannot reset "
2447 "HCD device state\n");
2455 clear_port_feature(hub
->hdev
,
2456 port1
, USB_PORT_FEAT_C_RESET
);
2457 /* FIXME need disconnect() for NOTATTACHED device */
2459 clear_port_feature(hub
->hdev
, port1
,
2460 USB_PORT_FEAT_C_BH_PORT_RESET
);
2461 clear_port_feature(hub
->hdev
, port1
,
2462 USB_PORT_FEAT_C_PORT_LINK_STATE
);
2464 usb_set_device_state(udev
, *status
2465 ? USB_STATE_NOTATTACHED
2466 : USB_STATE_DEFAULT
);
2472 /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
2473 static int hub_port_reset(struct usb_hub
*hub
, int port1
,
2474 struct usb_device
*udev
, unsigned int delay
, bool warm
)
2479 /* Block EHCI CF initialization during the port reset.
2480 * Some companion controllers don't like it when they mix.
2482 down_read(&ehci_cf_port_reset_rwsem
);
2484 if (!hub_is_superspeed(hub
->hdev
)) {
2485 dev_err(hub
->intfdev
, "only USB3 hub support "
2491 /* Reset the port */
2492 for (i
= 0; i
< PORT_RESET_TRIES
; i
++) {
2493 status
= set_port_feature(hub
->hdev
, port1
, (warm
?
2494 USB_PORT_FEAT_BH_PORT_RESET
:
2495 USB_PORT_FEAT_RESET
));
2497 dev_err(hub
->intfdev
,
2498 "cannot %sreset port %d (err = %d)\n",
2499 warm
? "warm " : "", port1
, status
);
2501 status
= hub_port_wait_reset(hub
, port1
, udev
, delay
,
2503 if (status
&& status
!= -ENOTCONN
)
2504 dev_dbg(hub
->intfdev
,
2505 "port_wait_reset: err = %d\n",
2509 /* return on disconnect or reset */
2510 if (status
== 0 || status
== -ENOTCONN
|| status
== -ENODEV
) {
2511 hub_port_finish_reset(hub
, port1
, udev
, &status
, warm
);
2515 dev_dbg (hub
->intfdev
,
2516 "port %d not enabled, trying %sreset again...\n",
2517 port1
, warm
? "warm " : "");
2518 delay
= HUB_LONG_RESET_TIME
;
2521 dev_err (hub
->intfdev
,
2522 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2527 up_read(&ehci_cf_port_reset_rwsem
);
2532 /* Check if a port is power on */
2533 static int port_is_power_on(struct usb_hub
*hub
, unsigned portstatus
)
2537 if (hub_is_superspeed(hub
->hdev
)) {
2538 if (portstatus
& USB_SS_PORT_STAT_POWER
)
2541 if (portstatus
& USB_PORT_STAT_POWER
)
2550 /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2551 static int port_is_suspended(struct usb_hub
*hub
, unsigned portstatus
)
2555 if (hub_is_superspeed(hub
->hdev
)) {
2556 if ((portstatus
& USB_PORT_STAT_LINK_STATE
)
2557 == USB_SS_PORT_LS_U3
)
2560 if (portstatus
& USB_PORT_STAT_SUSPEND
)
2567 /* Determine whether the device on a port is ready for a normal resume,
2568 * is ready for a reset-resume, or should be disconnected.
2570 static int check_port_resume_type(struct usb_device
*udev
,
2571 struct usb_hub
*hub
, int port1
,
2572 int status
, unsigned portchange
, unsigned portstatus
)
2574 /* Is the device still present? */
2575 if (status
|| port_is_suspended(hub
, portstatus
) ||
2576 !port_is_power_on(hub
, portstatus
) ||
2577 !(portstatus
& USB_PORT_STAT_CONNECTION
)) {
2582 /* Can't do a normal resume if the port isn't enabled,
2583 * so try a reset-resume instead.
2585 else if (!(portstatus
& USB_PORT_STAT_ENABLE
) && !udev
->reset_resume
) {
2586 if (udev
->persist_enabled
)
2587 udev
->reset_resume
= 1;
2593 dev_dbg(hub
->intfdev
,
2594 "port %d status %04x.%04x after resume, %d\n",
2595 port1
, portchange
, portstatus
, status
);
2596 } else if (udev
->reset_resume
) {
2598 /* Late port handoff can set status-change bits */
2599 if (portchange
& USB_PORT_STAT_C_CONNECTION
)
2600 clear_port_feature(hub
->hdev
, port1
,
2601 USB_PORT_FEAT_C_CONNECTION
);
2602 if (portchange
& USB_PORT_STAT_C_ENABLE
)
2603 clear_port_feature(hub
->hdev
, port1
,
2604 USB_PORT_FEAT_C_ENABLE
);
2610 #ifdef CONFIG_USB_SUSPEND
2613 * usb_port_suspend - suspend a usb device's upstream port
2614 * @udev: device that's no longer in active use, not a root hub
2615 * Context: must be able to sleep; device not locked; pm locks held
2617 * Suspends a USB device that isn't in active use, conserving power.
2618 * Devices may wake out of a suspend, if anything important happens,
2619 * using the remote wakeup mechanism. They may also be taken out of
2620 * suspend by the host, using usb_port_resume(). It's also routine
2621 * to disconnect devices while they are suspended.
2623 * This only affects the USB hardware for a device; its interfaces
2624 * (and, for hubs, child devices) must already have been suspended.
2626 * Selective port suspend reduces power; most suspended devices draw
2627 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2628 * All devices below the suspended port are also suspended.
2630 * Devices leave suspend state when the host wakes them up. Some devices
2631 * also support "remote wakeup", where the device can activate the USB
2632 * tree above them to deliver data, such as a keypress or packet. In
2633 * some cases, this wakes the USB host.
2635 * Suspending OTG devices may trigger HNP, if that's been enabled
2636 * between a pair of dual-role devices. That will change roles, such
2637 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2639 * Devices on USB hub ports have only one "suspend" state, corresponding
2640 * to ACPI D2, "may cause the device to lose some context".
2641 * State transitions include:
2643 * - suspend, resume ... when the VBUS power link stays live
2644 * - suspend, disconnect ... VBUS lost
2646 * Once VBUS drop breaks the circuit, the port it's using has to go through
2647 * normal re-enumeration procedures, starting with enabling VBUS power.
2648 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2649 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2650 * timer, no SRP, no requests through sysfs.
2652 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2653 * the root hub for their bus goes into global suspend ... so we don't
2654 * (falsely) update the device power state to say it suspended.
2656 * Returns 0 on success, else negative errno.
2658 int usb_port_suspend(struct usb_device
*udev
, pm_message_t msg
)
2660 struct usb_hub
*hub
= hdev_to_hub(udev
->parent
);
2661 int port1
= udev
->portnum
;
2664 /* enable remote wakeup when appropriate; this lets the device
2665 * wake up the upstream hub (including maybe the root hub).
2667 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2668 * we don't explicitly enable it here.
2670 if (udev
->do_remote_wakeup
) {
2671 if (!hub_is_superspeed(hub
->hdev
)) {
2672 status
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
2673 USB_REQ_SET_FEATURE
, USB_RECIP_DEVICE
,
2674 USB_DEVICE_REMOTE_WAKEUP
, 0,
2676 USB_CTRL_SET_TIMEOUT
);
2678 /* Assume there's only one function on the USB 3.0
2679 * device and enable remote wake for the first
2680 * interface. FIXME if the interface association
2681 * descriptor shows there's more than one function.
2683 status
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
2684 USB_REQ_SET_FEATURE
,
2685 USB_RECIP_INTERFACE
,
2686 USB_INTRF_FUNC_SUSPEND
,
2687 USB_INTRF_FUNC_SUSPEND_RW
|
2688 USB_INTRF_FUNC_SUSPEND_LP
,
2690 USB_CTRL_SET_TIMEOUT
);
2693 dev_dbg(&udev
->dev
, "won't remote wakeup, status %d\n",
2695 /* bail if autosuspend is requested */
2696 if (PMSG_IS_AUTO(msg
))
2701 /* disable USB2 hardware LPM */
2702 if (udev
->usb2_hw_lpm_enabled
== 1)
2703 usb_set_usb2_hardware_lpm(udev
, 0);
2705 if (usb_unlocked_disable_lpm(udev
)) {
2706 dev_err(&udev
->dev
, "%s Failed to disable LPM before suspend\n.",
2712 if (hub_is_superspeed(hub
->hdev
))
2713 status
= set_port_feature(hub
->hdev
,
2714 port1
| (USB_SS_PORT_LS_U3
<< 3),
2715 USB_PORT_FEAT_LINK_STATE
);
2717 status
= set_port_feature(hub
->hdev
, port1
,
2718 USB_PORT_FEAT_SUSPEND
);
2720 dev_dbg(hub
->intfdev
, "can't suspend port %d, status %d\n",
2722 /* paranoia: "should not happen" */
2723 if (udev
->do_remote_wakeup
)
2724 (void) usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
2725 USB_REQ_CLEAR_FEATURE
, USB_RECIP_DEVICE
,
2726 USB_DEVICE_REMOTE_WAKEUP
, 0,
2728 USB_CTRL_SET_TIMEOUT
);
2730 /* Try to enable USB2 hardware LPM again */
2731 if (udev
->usb2_hw_lpm_capable
== 1)
2732 usb_set_usb2_hardware_lpm(udev
, 1);
2734 /* Try to enable USB3 LPM again */
2735 usb_unlocked_enable_lpm(udev
);
2737 /* System sleep transitions should never fail */
2738 if (!PMSG_IS_AUTO(msg
))
2741 /* device has up to 10 msec to fully suspend */
2742 dev_dbg(&udev
->dev
, "usb %ssuspend, wakeup %d\n",
2743 (PMSG_IS_AUTO(msg
) ? "auto-" : ""),
2744 udev
->do_remote_wakeup
);
2745 usb_set_device_state(udev
, USB_STATE_SUSPENDED
);
2748 usb_mark_last_busy(hub
->hdev
);
2753 * If the USB "suspend" state is in use (rather than "global suspend"),
2754 * many devices will be individually taken out of suspend state using
2755 * special "resume" signaling. This routine kicks in shortly after
2756 * hardware resume signaling is finished, either because of selective
2757 * resume (by host) or remote wakeup (by device) ... now see what changed
2758 * in the tree that's rooted at this device.
2760 * If @udev->reset_resume is set then the device is reset before the
2761 * status check is done.
2763 static int finish_port_resume(struct usb_device
*udev
)
2768 /* caller owns the udev device lock */
2769 dev_dbg(&udev
->dev
, "%s\n",
2770 udev
->reset_resume
? "finish reset-resume" : "finish resume");
2772 /* usb ch9 identifies four variants of SUSPENDED, based on what
2773 * state the device resumes to. Linux currently won't see the
2774 * first two on the host side; they'd be inside hub_port_init()
2775 * during many timeouts, but khubd can't suspend until later.
2777 usb_set_device_state(udev
, udev
->actconfig
2778 ? USB_STATE_CONFIGURED
2779 : USB_STATE_ADDRESS
);
2781 /* 10.5.4.5 says not to reset a suspended port if the attached
2782 * device is enabled for remote wakeup. Hence the reset
2783 * operation is carried out here, after the port has been
2786 if (udev
->reset_resume
)
2788 status
= usb_reset_and_verify_device(udev
);
2790 /* 10.5.4.5 says be sure devices in the tree are still there.
2791 * For now let's assume the device didn't go crazy on resume,
2792 * and device drivers will know about any resume quirks.
2796 status
= usb_get_status(udev
, USB_RECIP_DEVICE
, 0, &devstatus
);
2798 status
= (status
> 0 ? 0 : -ENODEV
);
2800 /* If a normal resume failed, try doing a reset-resume */
2801 if (status
&& !udev
->reset_resume
&& udev
->persist_enabled
) {
2802 dev_dbg(&udev
->dev
, "retry with reset-resume\n");
2803 udev
->reset_resume
= 1;
2804 goto retry_reset_resume
;
2809 dev_dbg(&udev
->dev
, "gone after usb resume? status %d\n",
2811 } else if (udev
->actconfig
) {
2812 le16_to_cpus(&devstatus
);
2813 if (devstatus
& (1 << USB_DEVICE_REMOTE_WAKEUP
)) {
2814 status
= usb_control_msg(udev
,
2815 usb_sndctrlpipe(udev
, 0),
2816 USB_REQ_CLEAR_FEATURE
,
2818 USB_DEVICE_REMOTE_WAKEUP
, 0,
2820 USB_CTRL_SET_TIMEOUT
);
2823 "disable remote wakeup, status %d\n",
2832 * usb_port_resume - re-activate a suspended usb device's upstream port
2833 * @udev: device to re-activate, not a root hub
2834 * Context: must be able to sleep; device not locked; pm locks held
2836 * This will re-activate the suspended device, increasing power usage
2837 * while letting drivers communicate again with its endpoints.
2838 * USB resume explicitly guarantees that the power session between
2839 * the host and the device is the same as it was when the device
2842 * If @udev->reset_resume is set then this routine won't check that the
2843 * port is still enabled. Furthermore, finish_port_resume() above will
2844 * reset @udev. The end result is that a broken power session can be
2845 * recovered and @udev will appear to persist across a loss of VBUS power.
2847 * For example, if a host controller doesn't maintain VBUS suspend current
2848 * during a system sleep or is reset when the system wakes up, all the USB
2849 * power sessions below it will be broken. This is especially troublesome
2850 * for mass-storage devices containing mounted filesystems, since the
2851 * device will appear to have disconnected and all the memory mappings
2852 * to it will be lost. Using the USB_PERSIST facility, the device can be
2853 * made to appear as if it had not disconnected.
2855 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
2856 * every effort to insure that the same device is present after the
2857 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
2858 * quite possible for a device to remain unaltered but its media to be
2859 * changed. If the user replaces a flash memory card while the system is
2860 * asleep, he will have only himself to blame when the filesystem on the
2861 * new card is corrupted and the system crashes.
2863 * Returns 0 on success, else negative errno.
2865 int usb_port_resume(struct usb_device
*udev
, pm_message_t msg
)
2867 struct usb_hub
*hub
= hdev_to_hub(udev
->parent
);
2868 int port1
= udev
->portnum
;
2870 u16 portchange
, portstatus
;
2872 /* Skip the initial Clear-Suspend step for a remote wakeup */
2873 status
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
2874 if (status
== 0 && !port_is_suspended(hub
, portstatus
))
2875 goto SuspendCleared
;
2877 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
2879 set_bit(port1
, hub
->busy_bits
);
2881 /* see 7.1.7.7; affects power usage, but not budgeting */
2882 if (hub_is_superspeed(hub
->hdev
))
2883 status
= set_port_feature(hub
->hdev
,
2884 port1
| (USB_SS_PORT_LS_U0
<< 3),
2885 USB_PORT_FEAT_LINK_STATE
);
2887 status
= clear_port_feature(hub
->hdev
,
2888 port1
, USB_PORT_FEAT_SUSPEND
);
2890 dev_dbg(hub
->intfdev
, "can't resume port %d, status %d\n",
2893 /* drive resume for at least 20 msec */
2894 dev_dbg(&udev
->dev
, "usb %sresume\n",
2895 (PMSG_IS_AUTO(msg
) ? "auto-" : ""));
2898 /* Virtual root hubs can trigger on GET_PORT_STATUS to
2899 * stop resume signaling. Then finish the resume
2902 status
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
2904 /* TRSMRCY = 10 msec */
2910 if (hub_is_superspeed(hub
->hdev
)) {
2911 if (portchange
& USB_PORT_STAT_C_LINK_STATE
)
2912 clear_port_feature(hub
->hdev
, port1
,
2913 USB_PORT_FEAT_C_PORT_LINK_STATE
);
2915 if (portchange
& USB_PORT_STAT_C_SUSPEND
)
2916 clear_port_feature(hub
->hdev
, port1
,
2917 USB_PORT_FEAT_C_SUSPEND
);
2921 clear_bit(port1
, hub
->busy_bits
);
2923 status
= check_port_resume_type(udev
,
2924 hub
, port1
, status
, portchange
, portstatus
);
2926 status
= finish_port_resume(udev
);
2928 dev_dbg(&udev
->dev
, "can't resume, status %d\n", status
);
2929 hub_port_logical_disconnect(hub
, port1
);
2931 /* Try to enable USB2 hardware LPM */
2932 if (udev
->usb2_hw_lpm_capable
== 1)
2933 usb_set_usb2_hardware_lpm(udev
, 1);
2935 /* Try to enable USB3 LPM */
2936 usb_unlocked_enable_lpm(udev
);
2942 /* caller has locked udev */
2943 int usb_remote_wakeup(struct usb_device
*udev
)
2947 if (udev
->state
== USB_STATE_SUSPENDED
) {
2948 dev_dbg(&udev
->dev
, "usb %sresume\n", "wakeup-");
2949 status
= usb_autoresume_device(udev
);
2951 /* Let the drivers do their thing, then... */
2952 usb_autosuspend_device(udev
);
2958 #else /* CONFIG_USB_SUSPEND */
2960 /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
2962 int usb_port_suspend(struct usb_device
*udev
, pm_message_t msg
)
2967 /* However we may need to do a reset-resume */
2969 int usb_port_resume(struct usb_device
*udev
, pm_message_t msg
)
2971 struct usb_hub
*hub
= hdev_to_hub(udev
->parent
);
2972 int port1
= udev
->portnum
;
2974 u16 portchange
, portstatus
;
2976 status
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
2977 status
= check_port_resume_type(udev
,
2978 hub
, port1
, status
, portchange
, portstatus
);
2981 dev_dbg(&udev
->dev
, "can't resume, status %d\n", status
);
2982 hub_port_logical_disconnect(hub
, port1
);
2983 } else if (udev
->reset_resume
) {
2984 dev_dbg(&udev
->dev
, "reset-resume\n");
2985 status
= usb_reset_and_verify_device(udev
);
2992 static int hub_suspend(struct usb_interface
*intf
, pm_message_t msg
)
2994 struct usb_hub
*hub
= usb_get_intfdata (intf
);
2995 struct usb_device
*hdev
= hub
->hdev
;
2999 /* Warn if children aren't already suspended */
3000 for (port1
= 1; port1
<= hdev
->maxchild
; port1
++) {
3001 struct usb_device
*udev
;
3003 udev
= hdev
->children
[port1
-1];
3004 if (udev
&& udev
->can_submit
) {
3005 dev_warn(&intf
->dev
, "port %d nyet suspended\n", port1
);
3006 if (PMSG_IS_AUTO(msg
))
3010 if (hub_is_superspeed(hdev
) && hdev
->do_remote_wakeup
) {
3011 /* Enable hub to send remote wakeup for all ports. */
3012 for (port1
= 1; port1
<= hdev
->maxchild
; port1
++) {
3013 status
= set_port_feature(hdev
,
3015 USB_PORT_FEAT_REMOTE_WAKE_CONNECT
|
3016 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT
|
3017 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT
,
3018 USB_PORT_FEAT_REMOTE_WAKE_MASK
);
3022 dev_dbg(&intf
->dev
, "%s\n", __func__
);
3024 /* stop khubd and related activity */
3025 hub_quiesce(hub
, HUB_SUSPEND
);
3029 static int hub_resume(struct usb_interface
*intf
)
3031 struct usb_hub
*hub
= usb_get_intfdata(intf
);
3033 dev_dbg(&intf
->dev
, "%s\n", __func__
);
3034 hub_activate(hub
, HUB_RESUME
);
3038 static int hub_reset_resume(struct usb_interface
*intf
)
3040 struct usb_hub
*hub
= usb_get_intfdata(intf
);
3042 dev_dbg(&intf
->dev
, "%s\n", __func__
);
3043 hub_activate(hub
, HUB_RESET_RESUME
);
3048 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3049 * @rhdev: struct usb_device for the root hub
3051 * The USB host controller driver calls this function when its root hub
3052 * is resumed and Vbus power has been interrupted or the controller
3053 * has been reset. The routine marks @rhdev as having lost power.
3054 * When the hub driver is resumed it will take notice and carry out
3055 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3056 * the others will be disconnected.
3058 void usb_root_hub_lost_power(struct usb_device
*rhdev
)
3060 dev_warn(&rhdev
->dev
, "root hub lost power or was reset\n");
3061 rhdev
->reset_resume
= 1;
3063 EXPORT_SYMBOL_GPL(usb_root_hub_lost_power
);
3065 static const char * const usb3_lpm_names
[] = {
3073 * Send a Set SEL control transfer to the device, prior to enabling
3074 * device-initiated U1 or U2. This lets the device know the exit latencies from
3075 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3076 * packet from the host.
3078 * This function will fail if the SEL or PEL values for udev are greater than
3079 * the maximum allowed values for the link state to be enabled.
3081 static int usb_req_set_sel(struct usb_device
*udev
, enum usb3_link_state state
)
3083 struct usb_set_sel_req
*sel_values
;
3084 unsigned long long u1_sel
;
3085 unsigned long long u1_pel
;
3086 unsigned long long u2_sel
;
3087 unsigned long long u2_pel
;
3090 /* Convert SEL and PEL stored in ns to us */
3091 u1_sel
= DIV_ROUND_UP(udev
->u1_params
.sel
, 1000);
3092 u1_pel
= DIV_ROUND_UP(udev
->u1_params
.pel
, 1000);
3093 u2_sel
= DIV_ROUND_UP(udev
->u2_params
.sel
, 1000);
3094 u2_pel
= DIV_ROUND_UP(udev
->u2_params
.pel
, 1000);
3097 * Make sure that the calculated SEL and PEL values for the link
3098 * state we're enabling aren't bigger than the max SEL/PEL
3099 * value that will fit in the SET SEL control transfer.
3100 * Otherwise the device would get an incorrect idea of the exit
3101 * latency for the link state, and could start a device-initiated
3102 * U1/U2 when the exit latencies are too high.
3104 if ((state
== USB3_LPM_U1
&&
3105 (u1_sel
> USB3_LPM_MAX_U1_SEL_PEL
||
3106 u1_pel
> USB3_LPM_MAX_U1_SEL_PEL
)) ||
3107 (state
== USB3_LPM_U2
&&
3108 (u2_sel
> USB3_LPM_MAX_U2_SEL_PEL
||
3109 u2_pel
> USB3_LPM_MAX_U2_SEL_PEL
))) {
3110 dev_dbg(&udev
->dev
, "Device-initiated %s disabled due "
3111 "to long SEL %llu ms or PEL %llu ms\n",
3112 usb3_lpm_names
[state
], u1_sel
, u1_pel
);
3117 * If we're enabling device-initiated LPM for one link state,
3118 * but the other link state has a too high SEL or PEL value,
3119 * just set those values to the max in the Set SEL request.
3121 if (u1_sel
> USB3_LPM_MAX_U1_SEL_PEL
)
3122 u1_sel
= USB3_LPM_MAX_U1_SEL_PEL
;
3124 if (u1_pel
> USB3_LPM_MAX_U1_SEL_PEL
)
3125 u1_pel
= USB3_LPM_MAX_U1_SEL_PEL
;
3127 if (u2_sel
> USB3_LPM_MAX_U2_SEL_PEL
)
3128 u2_sel
= USB3_LPM_MAX_U2_SEL_PEL
;
3130 if (u2_pel
> USB3_LPM_MAX_U2_SEL_PEL
)
3131 u2_pel
= USB3_LPM_MAX_U2_SEL_PEL
;
3134 * usb_enable_lpm() can be called as part of a failed device reset,
3135 * which may be initiated by an error path of a mass storage driver.
3136 * Therefore, use GFP_NOIO.
3138 sel_values
= kmalloc(sizeof *(sel_values
), GFP_NOIO
);
3142 sel_values
->u1_sel
= u1_sel
;
3143 sel_values
->u1_pel
= u1_pel
;
3144 sel_values
->u2_sel
= cpu_to_le16(u2_sel
);
3145 sel_values
->u2_pel
= cpu_to_le16(u2_pel
);
3147 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
3151 sel_values
, sizeof *(sel_values
),
3152 USB_CTRL_SET_TIMEOUT
);
3158 * Enable or disable device-initiated U1 or U2 transitions.
3160 static int usb_set_device_initiated_lpm(struct usb_device
*udev
,
3161 enum usb3_link_state state
, bool enable
)
3168 feature
= USB_DEVICE_U1_ENABLE
;
3171 feature
= USB_DEVICE_U2_ENABLE
;
3174 dev_warn(&udev
->dev
, "%s: Can't %s non-U1 or U2 state.\n",
3175 __func__
, enable
? "enable" : "disable");
3179 if (udev
->state
!= USB_STATE_CONFIGURED
) {
3180 dev_dbg(&udev
->dev
, "%s: Can't %s %s state "
3181 "for unconfigured device.\n",
3182 __func__
, enable
? "enable" : "disable",
3183 usb3_lpm_names
[state
]);
3189 * First, let the device know about the exit latencies
3190 * associated with the link state we're about to enable.
3192 ret
= usb_req_set_sel(udev
, state
);
3194 dev_warn(&udev
->dev
, "Set SEL for device-initiated "
3195 "%s failed.\n", usb3_lpm_names
[state
]);
3199 * Now send the control transfer to enable device-initiated LPM
3200 * for either U1 or U2.
3202 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
3203 USB_REQ_SET_FEATURE
,
3207 USB_CTRL_SET_TIMEOUT
);
3209 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
3210 USB_REQ_CLEAR_FEATURE
,
3214 USB_CTRL_SET_TIMEOUT
);
3217 dev_warn(&udev
->dev
, "%s of device-initiated %s failed.\n",
3218 enable
? "Enable" : "Disable",
3219 usb3_lpm_names
[state
]);
3225 static int usb_set_lpm_timeout(struct usb_device
*udev
,
3226 enum usb3_link_state state
, int timeout
)
3233 feature
= USB_PORT_FEAT_U1_TIMEOUT
;
3236 feature
= USB_PORT_FEAT_U2_TIMEOUT
;
3239 dev_warn(&udev
->dev
, "%s: Can't set timeout for non-U1 or U2 state.\n",
3244 if (state
== USB3_LPM_U1
&& timeout
> USB3_LPM_U1_MAX_TIMEOUT
&&
3245 timeout
!= USB3_LPM_DEVICE_INITIATED
) {
3246 dev_warn(&udev
->dev
, "Failed to set %s timeout to 0x%x, "
3247 "which is a reserved value.\n",
3248 usb3_lpm_names
[state
], timeout
);
3252 ret
= set_port_feature(udev
->parent
,
3253 USB_PORT_LPM_TIMEOUT(timeout
) | udev
->portnum
,
3256 dev_warn(&udev
->dev
, "Failed to set %s timeout to 0x%x,"
3257 "error code %i\n", usb3_lpm_names
[state
],
3261 if (state
== USB3_LPM_U1
)
3262 udev
->u1_params
.timeout
= timeout
;
3264 udev
->u2_params
.timeout
= timeout
;
3269 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3272 * We will attempt to enable U1 or U2, but there are no guarantees that the
3273 * control transfers to set the hub timeout or enable device-initiated U1/U2
3274 * will be successful.
3276 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3277 * driver know about it. If that call fails, it should be harmless, and just
3278 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3280 static void usb_enable_link_state(struct usb_hcd
*hcd
, struct usb_device
*udev
,
3281 enum usb3_link_state state
)
3285 /* We allow the host controller to set the U1/U2 timeout internally
3286 * first, so that it can change its schedule to account for the
3287 * additional latency to send data to a device in a lower power
3290 timeout
= hcd
->driver
->enable_usb3_lpm_timeout(hcd
, udev
, state
);
3292 /* xHCI host controller doesn't want to enable this LPM state. */
3297 dev_warn(&udev
->dev
, "Could not enable %s link state, "
3298 "xHCI error %i.\n", usb3_lpm_names
[state
],
3303 if (usb_set_lpm_timeout(udev
, state
, timeout
))
3304 /* If we can't set the parent hub U1/U2 timeout,
3305 * device-initiated LPM won't be allowed either, so let the xHCI
3306 * host know that this link state won't be enabled.
3308 hcd
->driver
->disable_usb3_lpm_timeout(hcd
, udev
, state
);
3310 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3311 else if (udev
->actconfig
)
3312 usb_set_device_initiated_lpm(udev
, state
, true);
3317 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3320 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3321 * If zero is returned, the parent will not allow the link to go into U1/U2.
3323 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3324 * it won't have an effect on the bus link state because the parent hub will
3325 * still disallow device-initiated U1/U2 entry.
3327 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3328 * possible. The result will be slightly more bus bandwidth will be taken up
3329 * (to account for U1/U2 exit latency), but it should be harmless.
3331 static int usb_disable_link_state(struct usb_hcd
*hcd
, struct usb_device
*udev
,
3332 enum usb3_link_state state
)
3338 feature
= USB_PORT_FEAT_U1_TIMEOUT
;
3341 feature
= USB_PORT_FEAT_U2_TIMEOUT
;
3344 dev_warn(&udev
->dev
, "%s: Can't disable non-U1 or U2 state.\n",
3349 if (usb_set_lpm_timeout(udev
, state
, 0))
3352 usb_set_device_initiated_lpm(udev
, state
, false);
3354 if (hcd
->driver
->disable_usb3_lpm_timeout(hcd
, udev
, state
))
3355 dev_warn(&udev
->dev
, "Could not disable xHCI %s timeout, "
3356 "bus schedule bandwidth may be impacted.\n",
3357 usb3_lpm_names
[state
]);
3362 * Disable hub-initiated and device-initiated U1 and U2 entry.
3363 * Caller must own the bandwidth_mutex.
3365 * This will call usb_enable_lpm() on failure, which will decrement
3366 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3368 int usb_disable_lpm(struct usb_device
*udev
)
3370 struct usb_hcd
*hcd
;
3372 if (!udev
|| !udev
->parent
||
3373 udev
->speed
!= USB_SPEED_SUPER
||
3377 hcd
= bus_to_hcd(udev
->bus
);
3378 if (!hcd
|| !hcd
->driver
->disable_usb3_lpm_timeout
)
3381 udev
->lpm_disable_count
++;
3382 if ((udev
->u1_params
.timeout
== 0 && udev
->u2_params
.timeout
== 0))
3385 /* If LPM is enabled, attempt to disable it. */
3386 if (usb_disable_link_state(hcd
, udev
, USB3_LPM_U1
))
3388 if (usb_disable_link_state(hcd
, udev
, USB3_LPM_U2
))
3394 usb_enable_lpm(udev
);
3397 EXPORT_SYMBOL_GPL(usb_disable_lpm
);
3399 /* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3400 int usb_unlocked_disable_lpm(struct usb_device
*udev
)
3402 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
3408 mutex_lock(hcd
->bandwidth_mutex
);
3409 ret
= usb_disable_lpm(udev
);
3410 mutex_unlock(hcd
->bandwidth_mutex
);
3414 EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm
);
3417 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3418 * xHCI host policy may prevent U1 or U2 from being enabled.
3420 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3421 * until the lpm_disable_count drops to zero. Caller must own the
3424 void usb_enable_lpm(struct usb_device
*udev
)
3426 struct usb_hcd
*hcd
;
3428 if (!udev
|| !udev
->parent
||
3429 udev
->speed
!= USB_SPEED_SUPER
||
3433 udev
->lpm_disable_count
--;
3434 hcd
= bus_to_hcd(udev
->bus
);
3435 /* Double check that we can both enable and disable LPM.
3436 * Device must be configured to accept set feature U1/U2 timeout.
3438 if (!hcd
|| !hcd
->driver
->enable_usb3_lpm_timeout
||
3439 !hcd
->driver
->disable_usb3_lpm_timeout
)
3442 if (udev
->lpm_disable_count
> 0)
3445 usb_enable_link_state(hcd
, udev
, USB3_LPM_U1
);
3446 usb_enable_link_state(hcd
, udev
, USB3_LPM_U2
);
3448 EXPORT_SYMBOL_GPL(usb_enable_lpm
);
3450 /* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3451 void usb_unlocked_enable_lpm(struct usb_device
*udev
)
3453 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
3458 mutex_lock(hcd
->bandwidth_mutex
);
3459 usb_enable_lpm(udev
);
3460 mutex_unlock(hcd
->bandwidth_mutex
);
3462 EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm
);
3465 #else /* CONFIG_PM */
3467 #define hub_suspend NULL
3468 #define hub_resume NULL
3469 #define hub_reset_resume NULL
3471 int usb_disable_lpm(struct usb_device
*udev
)
3475 EXPORT_SYMBOL_GPL(usb_disable_lpm
);
3477 void usb_enable_lpm(struct usb_device
*udev
) { }
3478 EXPORT_SYMBOL_GPL(usb_enable_lpm
);
3480 int usb_unlocked_disable_lpm(struct usb_device
*udev
)
3484 EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm
);
3486 void usb_unlocked_enable_lpm(struct usb_device
*udev
) { }
3487 EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm
);
3491 /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3493 * Between connect detection and reset signaling there must be a delay
3494 * of 100ms at least for debounce and power-settling. The corresponding
3495 * timer shall restart whenever the downstream port detects a disconnect.
3497 * Apparently there are some bluetooth and irda-dongles and a number of
3498 * low-speed devices for which this debounce period may last over a second.
3499 * Not covered by the spec - but easy to deal with.
3501 * This implementation uses a 1500ms total debounce timeout; if the
3502 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3503 * every 25ms for transient disconnects. When the port status has been
3504 * unchanged for 100ms it returns the port status.
3506 static int hub_port_debounce(struct usb_hub
*hub
, int port1
)
3509 int total_time
, stable_time
= 0;
3510 u16 portchange
, portstatus
;
3511 unsigned connection
= 0xffff;
3513 for (total_time
= 0; ; total_time
+= HUB_DEBOUNCE_STEP
) {
3514 ret
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
3518 if (!(portchange
& USB_PORT_STAT_C_CONNECTION
) &&
3519 (portstatus
& USB_PORT_STAT_CONNECTION
) == connection
) {
3520 stable_time
+= HUB_DEBOUNCE_STEP
;
3521 if (stable_time
>= HUB_DEBOUNCE_STABLE
)
3525 connection
= portstatus
& USB_PORT_STAT_CONNECTION
;
3528 if (portchange
& USB_PORT_STAT_C_CONNECTION
) {
3529 clear_port_feature(hub
->hdev
, port1
,
3530 USB_PORT_FEAT_C_CONNECTION
);
3533 if (total_time
>= HUB_DEBOUNCE_TIMEOUT
)
3535 msleep(HUB_DEBOUNCE_STEP
);
3538 dev_dbg (hub
->intfdev
,
3539 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3540 port1
, total_time
, stable_time
, portstatus
);
3542 if (stable_time
< HUB_DEBOUNCE_STABLE
)
3547 void usb_ep0_reinit(struct usb_device
*udev
)
3549 usb_disable_endpoint(udev
, 0 + USB_DIR_IN
, true);
3550 usb_disable_endpoint(udev
, 0 + USB_DIR_OUT
, true);
3551 usb_enable_endpoint(udev
, &udev
->ep0
, true);
3553 EXPORT_SYMBOL_GPL(usb_ep0_reinit
);
3555 #define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3556 #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3558 static int hub_set_address(struct usb_device
*udev
, int devnum
)
3561 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
3564 * The host controller will choose the device address,
3565 * instead of the core having chosen it earlier
3567 if (!hcd
->driver
->address_device
&& devnum
<= 1)
3569 if (udev
->state
== USB_STATE_ADDRESS
)
3571 if (udev
->state
!= USB_STATE_DEFAULT
)
3573 if (hcd
->driver
->address_device
)
3574 retval
= hcd
->driver
->address_device(hcd
, udev
);
3576 retval
= usb_control_msg(udev
, usb_sndaddr0pipe(),
3577 USB_REQ_SET_ADDRESS
, 0, devnum
, 0,
3578 NULL
, 0, USB_CTRL_SET_TIMEOUT
);
3580 update_devnum(udev
, devnum
);
3581 /* Device now using proper address. */
3582 usb_set_device_state(udev
, USB_STATE_ADDRESS
);
3583 usb_ep0_reinit(udev
);
3588 /* Reset device, (re)assign address, get device descriptor.
3589 * Device connection must be stable, no more debouncing needed.
3590 * Returns device in USB_STATE_ADDRESS, except on error.
3592 * If this is called for an already-existing device (as part of
3593 * usb_reset_and_verify_device), the caller must own the device lock. For a
3594 * newly detected device that is not accessible through any global
3595 * pointers, it's not necessary to lock the device.
3598 hub_port_init (struct usb_hub
*hub
, struct usb_device
*udev
, int port1
,
3601 static DEFINE_MUTEX(usb_address0_mutex
);
3603 struct usb_device
*hdev
= hub
->hdev
;
3604 struct usb_hcd
*hcd
= bus_to_hcd(hdev
->bus
);
3606 unsigned delay
= HUB_SHORT_RESET_TIME
;
3607 enum usb_device_speed oldspeed
= udev
->speed
;
3609 int devnum
= udev
->devnum
;
3611 /* root hub ports have a slightly longer reset period
3612 * (from USB 2.0 spec, section 7.1.7.5)
3614 if (!hdev
->parent
) {
3615 delay
= HUB_ROOT_RESET_TIME
;
3616 if (port1
== hdev
->bus
->otg_port
)
3617 hdev
->bus
->b_hnp_enable
= 0;
3620 /* Some low speed devices have problems with the quick delay, so */
3621 /* be a bit pessimistic with those devices. RHbug #23670 */
3622 if (oldspeed
== USB_SPEED_LOW
)
3623 delay
= HUB_LONG_RESET_TIME
;
3625 mutex_lock(&usb_address0_mutex
);
3627 /* Reset the device; full speed may morph to high speed */
3628 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
3629 retval
= hub_port_reset(hub
, port1
, udev
, delay
, false);
3630 if (retval
< 0) /* error or disconnect */
3632 /* success, speed is known */
3636 if (oldspeed
!= USB_SPEED_UNKNOWN
&& oldspeed
!= udev
->speed
) {
3637 dev_dbg(&udev
->dev
, "device reset changed speed!\n");
3640 oldspeed
= udev
->speed
;
3642 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3643 * it's fixed size except for full speed devices.
3644 * For Wireless USB devices, ep0 max packet is always 512 (tho
3645 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
3647 switch (udev
->speed
) {
3648 case USB_SPEED_SUPER
:
3649 case USB_SPEED_WIRELESS
: /* fixed at 512 */
3650 udev
->ep0
.desc
.wMaxPacketSize
= cpu_to_le16(512);
3652 case USB_SPEED_HIGH
: /* fixed at 64 */
3653 udev
->ep0
.desc
.wMaxPacketSize
= cpu_to_le16(64);
3655 case USB_SPEED_FULL
: /* 8, 16, 32, or 64 */
3656 /* to determine the ep0 maxpacket size, try to read
3657 * the device descriptor to get bMaxPacketSize0 and
3658 * then correct our initial guess.
3660 udev
->ep0
.desc
.wMaxPacketSize
= cpu_to_le16(64);
3662 case USB_SPEED_LOW
: /* fixed at 8 */
3663 udev
->ep0
.desc
.wMaxPacketSize
= cpu_to_le16(8);
3669 if (udev
->speed
== USB_SPEED_WIRELESS
)
3670 speed
= "variable speed Wireless";
3672 speed
= usb_speed_string(udev
->speed
);
3674 if (udev
->speed
!= USB_SPEED_SUPER
)
3675 dev_info(&udev
->dev
,
3676 "%s %s USB device number %d using %s\n",
3677 (udev
->config
) ? "reset" : "new", speed
,
3678 devnum
, udev
->bus
->controller
->driver
->name
);
3680 /* Set up TT records, if needed */
3682 udev
->tt
= hdev
->tt
;
3683 udev
->ttport
= hdev
->ttport
;
3684 } else if (udev
->speed
!= USB_SPEED_HIGH
3685 && hdev
->speed
== USB_SPEED_HIGH
) {
3687 dev_err(&udev
->dev
, "parent hub has no TT\n");
3691 udev
->tt
= &hub
->tt
;
3692 udev
->ttport
= port1
;
3695 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3696 * Because device hardware and firmware is sometimes buggy in
3697 * this area, and this is how Linux has done it for ages.
3698 * Change it cautiously.
3700 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3701 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3702 * so it may help with some non-standards-compliant devices.
3703 * Otherwise we start with SET_ADDRESS and then try to read the
3704 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3707 for (i
= 0; i
< GET_DESCRIPTOR_TRIES
; (++i
, msleep(100))) {
3708 if (USE_NEW_SCHEME(retry_counter
) && !(hcd
->driver
->flags
& HCD_USB3
)) {
3709 struct usb_device_descriptor
*buf
;
3712 #define GET_DESCRIPTOR_BUFSIZE 64
3713 buf
= kmalloc(GET_DESCRIPTOR_BUFSIZE
, GFP_NOIO
);
3719 /* Retry on all errors; some devices are flakey.
3720 * 255 is for WUSB devices, we actually need to use
3721 * 512 (WUSB1.0[4.8.1]).
3723 for (j
= 0; j
< 3; ++j
) {
3724 buf
->bMaxPacketSize0
= 0;
3725 r
= usb_control_msg(udev
, usb_rcvaddr0pipe(),
3726 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
,
3727 USB_DT_DEVICE
<< 8, 0,
3728 buf
, GET_DESCRIPTOR_BUFSIZE
,
3729 initial_descriptor_timeout
);
3730 switch (buf
->bMaxPacketSize0
) {
3731 case 8: case 16: case 32: case 64: case 255:
3732 if (buf
->bDescriptorType
==
3746 udev
->descriptor
.bMaxPacketSize0
=
3747 buf
->bMaxPacketSize0
;
3750 retval
= hub_port_reset(hub
, port1
, udev
, delay
, false);
3751 if (retval
< 0) /* error or disconnect */
3753 if (oldspeed
!= udev
->speed
) {
3755 "device reset changed speed!\n");
3761 "device descriptor read/64, error %d\n",
3766 #undef GET_DESCRIPTOR_BUFSIZE
3770 * If device is WUSB, we already assigned an
3771 * unauthorized address in the Connect Ack sequence;
3772 * authorization will assign the final address.
3774 if (udev
->wusb
== 0) {
3775 for (j
= 0; j
< SET_ADDRESS_TRIES
; ++j
) {
3776 retval
= hub_set_address(udev
, devnum
);
3783 "device not accepting address %d, error %d\n",
3787 if (udev
->speed
== USB_SPEED_SUPER
) {
3788 devnum
= udev
->devnum
;
3789 dev_info(&udev
->dev
,
3790 "%s SuperSpeed USB device number %d using %s\n",
3791 (udev
->config
) ? "reset" : "new",
3792 devnum
, udev
->bus
->controller
->driver
->name
);
3795 /* cope with hardware quirkiness:
3796 * - let SET_ADDRESS settle, some device hardware wants it
3797 * - read ep0 maxpacket even for high and low speed,
3800 if (USE_NEW_SCHEME(retry_counter
) && !(hcd
->driver
->flags
& HCD_USB3
))
3804 retval
= usb_get_device_descriptor(udev
, 8);
3807 "device descriptor read/8, error %d\n",
3820 * Some superspeed devices have finished the link training process
3821 * and attached to a superspeed hub port, but the device descriptor
3822 * got from those devices show they aren't superspeed devices. Warm
3823 * reset the port attached by the devices can fix them.
3825 if ((udev
->speed
== USB_SPEED_SUPER
) &&
3826 (le16_to_cpu(udev
->descriptor
.bcdUSB
) < 0x0300)) {
3827 dev_err(&udev
->dev
, "got a wrong device descriptor, "
3828 "warm reset device\n");
3829 hub_port_reset(hub
, port1
, udev
,
3830 HUB_BH_RESET_TIME
, true);
3835 if (udev
->descriptor
.bMaxPacketSize0
== 0xff ||
3836 udev
->speed
== USB_SPEED_SUPER
)
3839 i
= udev
->descriptor
.bMaxPacketSize0
;
3840 if (usb_endpoint_maxp(&udev
->ep0
.desc
) != i
) {
3841 if (udev
->speed
== USB_SPEED_LOW
||
3842 !(i
== 8 || i
== 16 || i
== 32 || i
== 64)) {
3843 dev_err(&udev
->dev
, "Invalid ep0 maxpacket: %d\n", i
);
3847 if (udev
->speed
== USB_SPEED_FULL
)
3848 dev_dbg(&udev
->dev
, "ep0 maxpacket = %d\n", i
);
3850 dev_warn(&udev
->dev
, "Using ep0 maxpacket: %d\n", i
);
3851 udev
->ep0
.desc
.wMaxPacketSize
= cpu_to_le16(i
);
3852 usb_ep0_reinit(udev
);
3855 retval
= usb_get_device_descriptor(udev
, USB_DT_DEVICE_SIZE
);
3856 if (retval
< (signed)sizeof(udev
->descriptor
)) {
3857 dev_err(&udev
->dev
, "device descriptor read/all, error %d\n",
3864 if (udev
->wusb
== 0 && le16_to_cpu(udev
->descriptor
.bcdUSB
) >= 0x0201) {
3865 retval
= usb_get_bos_descriptor(udev
);
3867 udev
->lpm_capable
= usb_device_supports_lpm(udev
);
3868 usb_set_lpm_parameters(udev
);
3873 /* notify HCD that we have a device connected and addressed */
3874 if (hcd
->driver
->update_device
)
3875 hcd
->driver
->update_device(hcd
, udev
);
3878 hub_port_disable(hub
, port1
, 0);
3879 update_devnum(udev
, devnum
); /* for disconnect processing */
3881 mutex_unlock(&usb_address0_mutex
);
3886 check_highspeed (struct usb_hub
*hub
, struct usb_device
*udev
, int port1
)
3888 struct usb_qualifier_descriptor
*qual
;
3891 qual
= kmalloc (sizeof *qual
, GFP_KERNEL
);
3895 status
= usb_get_descriptor (udev
, USB_DT_DEVICE_QUALIFIER
, 0,
3896 qual
, sizeof *qual
);
3897 if (status
== sizeof *qual
) {
3898 dev_info(&udev
->dev
, "not running at top speed; "
3899 "connect to a high speed hub\n");
3900 /* hub LEDs are probably harder to miss than syslog */
3901 if (hub
->has_indicators
) {
3902 hub
->indicator
[port1
-1] = INDICATOR_GREEN_BLINK
;
3903 schedule_delayed_work (&hub
->leds
, 0);
3910 hub_power_remaining (struct usb_hub
*hub
)
3912 struct usb_device
*hdev
= hub
->hdev
;
3916 if (!hub
->limited_power
)
3919 remaining
= hdev
->bus_mA
- hub
->descriptor
->bHubContrCurrent
;
3920 for (port1
= 1; port1
<= hdev
->maxchild
; ++port1
) {
3921 struct usb_device
*udev
= hdev
->children
[port1
- 1];
3927 /* Unconfigured devices may not use more than 100mA,
3928 * or 8mA for OTG ports */
3929 if (udev
->actconfig
)
3930 delta
= udev
->actconfig
->desc
.bMaxPower
* 2;
3931 else if (port1
!= udev
->bus
->otg_port
|| hdev
->parent
)
3935 if (delta
> hub
->mA_per_port
)
3936 dev_warn(&udev
->dev
,
3937 "%dmA is over %umA budget for port %d!\n",
3938 delta
, hub
->mA_per_port
, port1
);
3941 if (remaining
< 0) {
3942 dev_warn(hub
->intfdev
, "%dmA over power budget!\n",
3949 /* Handle physical or logical connection change events.
3950 * This routine is called when:
3951 * a port connection-change occurs;
3952 * a port enable-change occurs (often caused by EMI);
3953 * usb_reset_and_verify_device() encounters changed descriptors (as from
3954 * a firmware download)
3955 * caller already locked the hub
3957 static void hub_port_connect_change(struct usb_hub
*hub
, int port1
,
3958 u16 portstatus
, u16 portchange
)
3960 struct usb_device
*hdev
= hub
->hdev
;
3961 struct device
*hub_dev
= hub
->intfdev
;
3962 struct usb_hcd
*hcd
= bus_to_hcd(hdev
->bus
);
3963 unsigned wHubCharacteristics
=
3964 le16_to_cpu(hub
->descriptor
->wHubCharacteristics
);
3965 struct usb_device
*udev
;
3969 "port %d, status %04x, change %04x, %s\n",
3970 port1
, portstatus
, portchange
, portspeed(hub
, portstatus
));
3972 if (hub
->has_indicators
) {
3973 set_port_led(hub
, port1
, HUB_LED_AUTO
);
3974 hub
->indicator
[port1
-1] = INDICATOR_AUTO
;
3977 #ifdef CONFIG_USB_OTG
3978 /* during HNP, don't repeat the debounce */
3979 if (hdev
->bus
->is_b_host
)
3980 portchange
&= ~(USB_PORT_STAT_C_CONNECTION
|
3981 USB_PORT_STAT_C_ENABLE
);
3984 /* Try to resuscitate an existing device */
3985 udev
= hdev
->children
[port1
-1];
3986 if ((portstatus
& USB_PORT_STAT_CONNECTION
) && udev
&&
3987 udev
->state
!= USB_STATE_NOTATTACHED
) {
3988 usb_lock_device(udev
);
3989 if (portstatus
& USB_PORT_STAT_ENABLE
) {
3990 status
= 0; /* Nothing to do */
3992 #ifdef CONFIG_USB_SUSPEND
3993 } else if (udev
->state
== USB_STATE_SUSPENDED
&&
3994 udev
->persist_enabled
) {
3995 /* For a suspended device, treat this as a
3996 * remote wakeup event.
3998 status
= usb_remote_wakeup(udev
);
4002 status
= -ENODEV
; /* Don't resuscitate */
4004 usb_unlock_device(udev
);
4007 clear_bit(port1
, hub
->change_bits
);
4012 /* Disconnect any existing devices under this port */
4014 usb_disconnect(&hdev
->children
[port1
-1]);
4015 clear_bit(port1
, hub
->change_bits
);
4017 /* We can forget about a "removed" device when there's a physical
4018 * disconnect or the connect status changes.
4020 if (!(portstatus
& USB_PORT_STAT_CONNECTION
) ||
4021 (portchange
& USB_PORT_STAT_C_CONNECTION
))
4022 clear_bit(port1
, hub
->removed_bits
);
4024 if (portchange
& (USB_PORT_STAT_C_CONNECTION
|
4025 USB_PORT_STAT_C_ENABLE
)) {
4026 status
= hub_port_debounce(hub
, port1
);
4028 if (printk_ratelimit())
4029 dev_err(hub_dev
, "connect-debounce failed, "
4030 "port %d disabled\n", port1
);
4031 portstatus
&= ~USB_PORT_STAT_CONNECTION
;
4033 portstatus
= status
;
4037 /* Return now if debouncing failed or nothing is connected or
4038 * the device was "removed".
4040 if (!(portstatus
& USB_PORT_STAT_CONNECTION
) ||
4041 test_bit(port1
, hub
->removed_bits
)) {
4043 /* maybe switch power back on (e.g. root hub was reset) */
4044 if ((wHubCharacteristics
& HUB_CHAR_LPSM
) < 2
4045 && !port_is_power_on(hub
, portstatus
))
4046 set_port_feature(hdev
, port1
, USB_PORT_FEAT_POWER
);
4048 if (portstatus
& USB_PORT_STAT_ENABLE
)
4053 for (i
= 0; i
< SET_CONFIG_TRIES
; i
++) {
4055 /* reallocate for each attempt, since references
4056 * to the previous one can escape in various ways
4058 udev
= usb_alloc_dev(hdev
, hdev
->bus
, port1
);
4061 "couldn't allocate port %d usb_device\n",
4066 usb_set_device_state(udev
, USB_STATE_POWERED
);
4067 udev
->bus_mA
= hub
->mA_per_port
;
4068 udev
->level
= hdev
->level
+ 1;
4069 udev
->wusb
= hub_is_wusb(hub
);
4071 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4072 if (hub_is_superspeed(hub
->hdev
))
4073 udev
->speed
= USB_SPEED_SUPER
;
4075 udev
->speed
= USB_SPEED_UNKNOWN
;
4077 choose_devnum(udev
);
4078 if (udev
->devnum
<= 0) {
4079 status
= -ENOTCONN
; /* Don't retry */
4083 /* reset (non-USB 3.0 devices) and get descriptor */
4084 status
= hub_port_init(hub
, udev
, port1
, i
);
4088 usb_detect_quirks(udev
);
4089 if (udev
->quirks
& USB_QUIRK_DELAY_INIT
)
4092 /* consecutive bus-powered hubs aren't reliable; they can
4093 * violate the voltage drop budget. if the new child has
4094 * a "powered" LED, users should notice we didn't enable it
4095 * (without reading syslog), even without per-port LEDs
4098 if (udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
4099 && udev
->bus_mA
<= 100) {
4102 status
= usb_get_status(udev
, USB_RECIP_DEVICE
, 0,
4105 dev_dbg(&udev
->dev
, "get status %d ?\n", status
);
4108 le16_to_cpus(&devstat
);
4109 if ((devstat
& (1 << USB_DEVICE_SELF_POWERED
)) == 0) {
4111 "can't connect bus-powered hub "
4113 if (hub
->has_indicators
) {
4114 hub
->indicator
[port1
-1] =
4115 INDICATOR_AMBER_BLINK
;
4116 schedule_delayed_work (&hub
->leds
, 0);
4118 status
= -ENOTCONN
; /* Don't retry */
4123 /* check for devices running slower than they could */
4124 if (le16_to_cpu(udev
->descriptor
.bcdUSB
) >= 0x0200
4125 && udev
->speed
== USB_SPEED_FULL
4126 && highspeed_hubs
!= 0)
4127 check_highspeed (hub
, udev
, port1
);
4129 /* Store the parent's children[] pointer. At this point
4130 * udev becomes globally accessible, although presumably
4131 * no one will look at it until hdev is unlocked.
4135 /* We mustn't add new devices if the parent hub has
4136 * been disconnected; we would race with the
4137 * recursively_mark_NOTATTACHED() routine.
4139 spin_lock_irq(&device_state_lock
);
4140 if (hdev
->state
== USB_STATE_NOTATTACHED
)
4143 hdev
->children
[port1
-1] = udev
;
4144 spin_unlock_irq(&device_state_lock
);
4146 /* Run it through the hoops (find a driver, etc) */
4148 status
= usb_new_device(udev
);
4150 spin_lock_irq(&device_state_lock
);
4151 hdev
->children
[port1
-1] = NULL
;
4152 spin_unlock_irq(&device_state_lock
);
4159 status
= hub_power_remaining(hub
);
4161 dev_dbg(hub_dev
, "%dmA power budget left\n", status
);
4166 hub_port_disable(hub
, port1
, 1);
4168 usb_ep0_reinit(udev
);
4169 release_devnum(udev
);
4172 if ((status
== -ENOTCONN
) || (status
== -ENOTSUPP
))
4175 if (hub
->hdev
->parent
||
4176 !hcd
->driver
->port_handed_over
||
4177 !(hcd
->driver
->port_handed_over
)(hcd
, port1
))
4178 dev_err(hub_dev
, "unable to enumerate USB device on port %d\n",
4182 hub_port_disable(hub
, port1
, 1);
4183 if (hcd
->driver
->relinquish_port
&& !hub
->hdev
->parent
)
4184 hcd
->driver
->relinquish_port(hcd
, port1
);
4187 /* Returns 1 if there was a remote wakeup and a connect status change. */
4188 static int hub_handle_remote_wakeup(struct usb_hub
*hub
, unsigned int port
,
4189 u16 portstatus
, u16 portchange
)
4191 struct usb_device
*hdev
;
4192 struct usb_device
*udev
;
4193 int connect_change
= 0;
4197 udev
= hdev
->children
[port
-1];
4198 if (!hub_is_superspeed(hdev
)) {
4199 if (!(portchange
& USB_PORT_STAT_C_SUSPEND
))
4201 clear_port_feature(hdev
, port
, USB_PORT_FEAT_C_SUSPEND
);
4203 if (!udev
|| udev
->state
!= USB_STATE_SUSPENDED
||
4204 (portstatus
& USB_PORT_STAT_LINK_STATE
) !=
4210 /* TRSMRCY = 10 msec */
4213 usb_lock_device(udev
);
4214 ret
= usb_remote_wakeup(udev
);
4215 usb_unlock_device(udev
);
4220 hub_port_disable(hub
, port
, 1);
4222 dev_dbg(hub
->intfdev
, "resume on port %d, status %d\n",
4224 return connect_change
;
4227 static void hub_events(void)
4229 struct list_head
*tmp
;
4230 struct usb_device
*hdev
;
4231 struct usb_interface
*intf
;
4232 struct usb_hub
*hub
;
4233 struct device
*hub_dev
;
4239 int connect_change
, wakeup_change
;
4242 * We restart the list every time to avoid a deadlock with
4243 * deleting hubs downstream from this one. This should be
4244 * safe since we delete the hub from the event list.
4245 * Not the most efficient, but avoids deadlocks.
4249 /* Grab the first entry at the beginning of the list */
4250 spin_lock_irq(&hub_event_lock
);
4251 if (list_empty(&hub_event_list
)) {
4252 spin_unlock_irq(&hub_event_lock
);
4256 tmp
= hub_event_list
.next
;
4259 hub
= list_entry(tmp
, struct usb_hub
, event_list
);
4260 kref_get(&hub
->kref
);
4261 spin_unlock_irq(&hub_event_lock
);
4264 hub_dev
= hub
->intfdev
;
4265 intf
= to_usb_interface(hub_dev
);
4266 dev_dbg(hub_dev
, "state %d ports %d chg %04x evt %04x\n",
4267 hdev
->state
, hub
->descriptor
4268 ? hub
->descriptor
->bNbrPorts
4270 /* NOTE: expects max 15 ports... */
4271 (u16
) hub
->change_bits
[0],
4272 (u16
) hub
->event_bits
[0]);
4274 /* Lock the device, then check to see if we were
4275 * disconnected while waiting for the lock to succeed. */
4276 usb_lock_device(hdev
);
4277 if (unlikely(hub
->disconnected
))
4278 goto loop_disconnected
;
4280 /* If the hub has died, clean up after it */
4281 if (hdev
->state
== USB_STATE_NOTATTACHED
) {
4282 hub
->error
= -ENODEV
;
4283 hub_quiesce(hub
, HUB_DISCONNECT
);
4288 ret
= usb_autopm_get_interface(intf
);
4290 dev_dbg(hub_dev
, "Can't autoresume: %d\n", ret
);
4294 /* If this is an inactive hub, do nothing */
4299 dev_dbg (hub_dev
, "resetting for error %d\n",
4302 ret
= usb_reset_device(hdev
);
4305 "error resetting hub: %d\n", ret
);
4313 /* deal with port status changes */
4314 for (i
= 1; i
<= hub
->descriptor
->bNbrPorts
; i
++) {
4315 if (test_bit(i
, hub
->busy_bits
))
4317 connect_change
= test_bit(i
, hub
->change_bits
);
4318 wakeup_change
= test_and_clear_bit(i
, hub
->wakeup_bits
);
4319 if (!test_and_clear_bit(i
, hub
->event_bits
) &&
4320 !connect_change
&& !wakeup_change
)
4323 ret
= hub_port_status(hub
, i
,
4324 &portstatus
, &portchange
);
4328 if (portchange
& USB_PORT_STAT_C_CONNECTION
) {
4329 clear_port_feature(hdev
, i
,
4330 USB_PORT_FEAT_C_CONNECTION
);
4334 if (portchange
& USB_PORT_STAT_C_ENABLE
) {
4335 if (!connect_change
)
4337 "port %d enable change, "
4340 clear_port_feature(hdev
, i
,
4341 USB_PORT_FEAT_C_ENABLE
);
4344 * EM interference sometimes causes badly
4345 * shielded USB devices to be shutdown by
4346 * the hub, this hack enables them again.
4347 * Works at least with mouse driver.
4349 if (!(portstatus
& USB_PORT_STAT_ENABLE
)
4351 && hdev
->children
[i
-1]) {
4354 "disabled by hub (EMI?), "
4361 if (hub_handle_remote_wakeup(hub
, i
,
4362 portstatus
, portchange
))
4365 if (portchange
& USB_PORT_STAT_C_OVERCURRENT
) {
4369 dev_dbg(hub_dev
, "over-current change on port "
4371 clear_port_feature(hdev
, i
,
4372 USB_PORT_FEAT_C_OVER_CURRENT
);
4373 msleep(100); /* Cool down */
4374 hub_power_on(hub
, true);
4375 hub_port_status(hub
, i
, &status
, &unused
);
4376 if (status
& USB_PORT_STAT_OVERCURRENT
)
4377 dev_err(hub_dev
, "over-current "
4378 "condition on port %d\n", i
);
4381 if (portchange
& USB_PORT_STAT_C_RESET
) {
4383 "reset change on port %d\n",
4385 clear_port_feature(hdev
, i
,
4386 USB_PORT_FEAT_C_RESET
);
4388 if ((portchange
& USB_PORT_STAT_C_BH_RESET
) &&
4389 hub_is_superspeed(hub
->hdev
)) {
4391 "warm reset change on port %d\n",
4393 clear_port_feature(hdev
, i
,
4394 USB_PORT_FEAT_C_BH_PORT_RESET
);
4396 if (portchange
& USB_PORT_STAT_C_LINK_STATE
) {
4397 clear_port_feature(hub
->hdev
, i
,
4398 USB_PORT_FEAT_C_PORT_LINK_STATE
);
4400 if (portchange
& USB_PORT_STAT_C_CONFIG_ERROR
) {
4402 "config error on port %d\n",
4404 clear_port_feature(hub
->hdev
, i
,
4405 USB_PORT_FEAT_C_PORT_CONFIG_ERROR
);
4408 /* Warm reset a USB3 protocol port if it's in
4409 * SS.Inactive state.
4411 if (hub_is_superspeed(hub
->hdev
) &&
4412 (portstatus
& USB_PORT_STAT_LINK_STATE
)
4413 == USB_SS_PORT_LS_SS_INACTIVE
) {
4414 dev_dbg(hub_dev
, "warm reset port %d\n", i
);
4415 hub_port_reset(hub
, i
, NULL
,
4416 HUB_BH_RESET_TIME
, true);
4420 hub_port_connect_change(hub
, i
,
4421 portstatus
, portchange
);
4424 /* deal with hub status changes */
4425 if (test_and_clear_bit(0, hub
->event_bits
) == 0)
4427 else if (hub_hub_status(hub
, &hubstatus
, &hubchange
) < 0)
4428 dev_err (hub_dev
, "get_hub_status failed\n");
4430 if (hubchange
& HUB_CHANGE_LOCAL_POWER
) {
4431 dev_dbg (hub_dev
, "power change\n");
4432 clear_hub_feature(hdev
, C_HUB_LOCAL_POWER
);
4433 if (hubstatus
& HUB_STATUS_LOCAL_POWER
)
4434 /* FIXME: Is this always true? */
4435 hub
->limited_power
= 1;
4437 hub
->limited_power
= 0;
4439 if (hubchange
& HUB_CHANGE_OVERCURRENT
) {
4443 dev_dbg(hub_dev
, "over-current change\n");
4444 clear_hub_feature(hdev
, C_HUB_OVER_CURRENT
);
4445 msleep(500); /* Cool down */
4446 hub_power_on(hub
, true);
4447 hub_hub_status(hub
, &status
, &unused
);
4448 if (status
& HUB_STATUS_OVERCURRENT
)
4449 dev_err(hub_dev
, "over-current "
4455 /* Balance the usb_autopm_get_interface() above */
4456 usb_autopm_put_interface_no_suspend(intf
);
4458 /* Balance the usb_autopm_get_interface_no_resume() in
4459 * kick_khubd() and allow autosuspend.
4461 usb_autopm_put_interface(intf
);
4463 usb_unlock_device(hdev
);
4464 kref_put(&hub
->kref
, hub_release
);
4466 } /* end while (1) */
4469 static int hub_thread(void *__unused
)
4471 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4472 * port handover. Otherwise it might see that a full-speed device
4473 * was gone before the EHCI controller had handed its port over to
4474 * the companion full-speed controller.
4480 wait_event_freezable(khubd_wait
,
4481 !list_empty(&hub_event_list
) ||
4482 kthread_should_stop());
4483 } while (!kthread_should_stop() || !list_empty(&hub_event_list
));
4485 pr_debug("%s: khubd exiting\n", usbcore_name
);
4489 static const struct usb_device_id hub_id_table
[] = {
4490 { .match_flags
= USB_DEVICE_ID_MATCH_DEV_CLASS
,
4491 .bDeviceClass
= USB_CLASS_HUB
},
4492 { .match_flags
= USB_DEVICE_ID_MATCH_INT_CLASS
,
4493 .bInterfaceClass
= USB_CLASS_HUB
},
4494 { } /* Terminating entry */
4497 MODULE_DEVICE_TABLE (usb
, hub_id_table
);
4499 static struct usb_driver hub_driver
= {
4502 .disconnect
= hub_disconnect
,
4503 .suspend
= hub_suspend
,
4504 .resume
= hub_resume
,
4505 .reset_resume
= hub_reset_resume
,
4506 .pre_reset
= hub_pre_reset
,
4507 .post_reset
= hub_post_reset
,
4508 .unlocked_ioctl
= hub_ioctl
,
4509 .id_table
= hub_id_table
,
4510 .supports_autosuspend
= 1,
4513 int usb_hub_init(void)
4515 if (usb_register(&hub_driver
) < 0) {
4516 printk(KERN_ERR
"%s: can't register hub driver\n",
4521 khubd_task
= kthread_run(hub_thread
, NULL
, "khubd");
4522 if (!IS_ERR(khubd_task
))
4525 /* Fall through if kernel_thread failed */
4526 usb_deregister(&hub_driver
);
4527 printk(KERN_ERR
"%s: can't start khubd\n", usbcore_name
);
4532 void usb_hub_cleanup(void)
4534 kthread_stop(khubd_task
);
4537 * Hub resources are freed for us by usb_deregister. It calls
4538 * usb_driver_purge on every device which in turn calls that
4539 * devices disconnect function if it is using this driver.
4540 * The hub_disconnect function takes care of releasing the
4541 * individual hub resources. -greg
4543 usb_deregister(&hub_driver
);
4544 } /* usb_hub_cleanup() */
4546 static int descriptors_changed(struct usb_device
*udev
,
4547 struct usb_device_descriptor
*old_device_descriptor
)
4551 unsigned serial_len
= 0;
4553 unsigned old_length
;
4557 if (memcmp(&udev
->descriptor
, old_device_descriptor
,
4558 sizeof(*old_device_descriptor
)) != 0)
4561 /* Since the idVendor, idProduct, and bcdDevice values in the
4562 * device descriptor haven't changed, we will assume the
4563 * Manufacturer and Product strings haven't changed either.
4564 * But the SerialNumber string could be different (e.g., a
4565 * different flash card of the same brand).
4568 serial_len
= strlen(udev
->serial
) + 1;
4571 for (index
= 0; index
< udev
->descriptor
.bNumConfigurations
; index
++) {
4572 old_length
= le16_to_cpu(udev
->config
[index
].desc
.wTotalLength
);
4573 len
= max(len
, old_length
);
4576 buf
= kmalloc(len
, GFP_NOIO
);
4578 dev_err(&udev
->dev
, "no mem to re-read configs after reset\n");
4579 /* assume the worst */
4582 for (index
= 0; index
< udev
->descriptor
.bNumConfigurations
; index
++) {
4583 old_length
= le16_to_cpu(udev
->config
[index
].desc
.wTotalLength
);
4584 length
= usb_get_descriptor(udev
, USB_DT_CONFIG
, index
, buf
,
4586 if (length
!= old_length
) {
4587 dev_dbg(&udev
->dev
, "config index %d, error %d\n",
4592 if (memcmp (buf
, udev
->rawdescriptors
[index
], old_length
)
4594 dev_dbg(&udev
->dev
, "config index %d changed (#%d)\n",
4596 ((struct usb_config_descriptor
*) buf
)->
4597 bConfigurationValue
);
4603 if (!changed
&& serial_len
) {
4604 length
= usb_string(udev
, udev
->descriptor
.iSerialNumber
,
4606 if (length
+ 1 != serial_len
) {
4607 dev_dbg(&udev
->dev
, "serial string error %d\n",
4610 } else if (memcmp(buf
, udev
->serial
, length
) != 0) {
4611 dev_dbg(&udev
->dev
, "serial string changed\n");
4621 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
4622 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4624 * WARNING - don't use this routine to reset a composite device
4625 * (one with multiple interfaces owned by separate drivers)!
4626 * Use usb_reset_device() instead.
4628 * Do a port reset, reassign the device's address, and establish its
4629 * former operating configuration. If the reset fails, or the device's
4630 * descriptors change from their values before the reset, or the original
4631 * configuration and altsettings cannot be restored, a flag will be set
4632 * telling khubd to pretend the device has been disconnected and then
4633 * re-connected. All drivers will be unbound, and the device will be
4634 * re-enumerated and probed all over again.
4636 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4637 * flagged for logical disconnection, or some other negative error code
4638 * if the reset wasn't even attempted.
4640 * The caller must own the device lock. For example, it's safe to use
4641 * this from a driver probe() routine after downloading new firmware.
4642 * For calls that might not occur during probe(), drivers should lock
4643 * the device using usb_lock_device_for_reset().
4645 * Locking exception: This routine may also be called from within an
4646 * autoresume handler. Such usage won't conflict with other tasks
4647 * holding the device lock because these tasks should always call
4648 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
4650 static int usb_reset_and_verify_device(struct usb_device
*udev
)
4652 struct usb_device
*parent_hdev
= udev
->parent
;
4653 struct usb_hub
*parent_hub
;
4654 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
4655 struct usb_device_descriptor descriptor
= udev
->descriptor
;
4657 int port1
= udev
->portnum
;
4659 if (udev
->state
== USB_STATE_NOTATTACHED
||
4660 udev
->state
== USB_STATE_SUSPENDED
) {
4661 dev_dbg(&udev
->dev
, "device reset not allowed in state %d\n",
4667 /* this requires hcd-specific logic; see ohci_restart() */
4668 dev_dbg(&udev
->dev
, "%s for root hub!\n", __func__
);
4671 parent_hub
= hdev_to_hub(parent_hdev
);
4673 set_bit(port1
, parent_hub
->busy_bits
);
4674 for (i
= 0; i
< SET_CONFIG_TRIES
; ++i
) {
4676 /* ep0 maxpacket size may change; let the HCD know about it.
4677 * Other endpoints will be handled by re-enumeration. */
4678 usb_ep0_reinit(udev
);
4679 ret
= hub_port_init(parent_hub
, udev
, port1
, i
);
4680 if (ret
>= 0 || ret
== -ENOTCONN
|| ret
== -ENODEV
)
4683 clear_bit(port1
, parent_hub
->busy_bits
);
4688 /* Device might have changed firmware (DFU or similar) */
4689 if (descriptors_changed(udev
, &descriptor
)) {
4690 dev_info(&udev
->dev
, "device firmware changed\n");
4691 udev
->descriptor
= descriptor
; /* for disconnect() calls */
4695 /* Restore the device's previous configuration */
4696 if (!udev
->actconfig
)
4699 mutex_lock(hcd
->bandwidth_mutex
);
4700 /* Disable LPM while we reset the device and reinstall the alt settings.
4701 * Device-initiated LPM settings, and system exit latency settings are
4702 * cleared when the device is reset, so we have to set them up again.
4704 ret
= usb_disable_lpm(udev
);
4706 dev_err(&udev
->dev
, "%s Failed to disable LPM\n.", __func__
);
4707 mutex_unlock(hcd
->bandwidth_mutex
);
4710 ret
= usb_hcd_alloc_bandwidth(udev
, udev
->actconfig
, NULL
, NULL
);
4712 dev_warn(&udev
->dev
,
4713 "Busted HC? Not enough HCD resources for "
4714 "old configuration.\n");
4715 usb_enable_lpm(udev
);
4716 mutex_unlock(hcd
->bandwidth_mutex
);
4719 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
4720 USB_REQ_SET_CONFIGURATION
, 0,
4721 udev
->actconfig
->desc
.bConfigurationValue
, 0,
4722 NULL
, 0, USB_CTRL_SET_TIMEOUT
);
4725 "can't restore configuration #%d (error=%d)\n",
4726 udev
->actconfig
->desc
.bConfigurationValue
, ret
);
4727 usb_enable_lpm(udev
);
4728 mutex_unlock(hcd
->bandwidth_mutex
);
4731 mutex_unlock(hcd
->bandwidth_mutex
);
4732 usb_set_device_state(udev
, USB_STATE_CONFIGURED
);
4734 /* Put interfaces back into the same altsettings as before.
4735 * Don't bother to send the Set-Interface request for interfaces
4736 * that were already in altsetting 0; besides being unnecessary,
4737 * many devices can't handle it. Instead just reset the host-side
4740 for (i
= 0; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
4741 struct usb_host_config
*config
= udev
->actconfig
;
4742 struct usb_interface
*intf
= config
->interface
[i
];
4743 struct usb_interface_descriptor
*desc
;
4745 desc
= &intf
->cur_altsetting
->desc
;
4746 if (desc
->bAlternateSetting
== 0) {
4747 usb_disable_interface(udev
, intf
, true);
4748 usb_enable_interface(udev
, intf
, true);
4751 /* Let the bandwidth allocation function know that this
4752 * device has been reset, and it will have to use
4753 * alternate setting 0 as the current alternate setting.
4755 intf
->resetting_device
= 1;
4756 ret
= usb_set_interface(udev
, desc
->bInterfaceNumber
,
4757 desc
->bAlternateSetting
);
4758 intf
->resetting_device
= 0;
4761 dev_err(&udev
->dev
, "failed to restore interface %d "
4762 "altsetting %d (error=%d)\n",
4763 desc
->bInterfaceNumber
,
4764 desc
->bAlternateSetting
,
4766 usb_unlocked_enable_lpm(udev
);
4771 /* Now that the alt settings are re-installed, enable LPM. */
4772 usb_unlocked_enable_lpm(udev
);
4777 hub_port_logical_disconnect(parent_hub
, port1
);
4782 * usb_reset_device - warn interface drivers and perform a USB port reset
4783 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4785 * Warns all drivers bound to registered interfaces (using their pre_reset
4786 * method), performs the port reset, and then lets the drivers know that
4787 * the reset is over (using their post_reset method).
4789 * Return value is the same as for usb_reset_and_verify_device().
4791 * The caller must own the device lock. For example, it's safe to use
4792 * this from a driver probe() routine after downloading new firmware.
4793 * For calls that might not occur during probe(), drivers should lock
4794 * the device using usb_lock_device_for_reset().
4796 * If an interface is currently being probed or disconnected, we assume
4797 * its driver knows how to handle resets. For all other interfaces,
4798 * if the driver doesn't have pre_reset and post_reset methods then
4799 * we attempt to unbind it and rebind afterward.
4801 int usb_reset_device(struct usb_device
*udev
)
4805 struct usb_host_config
*config
= udev
->actconfig
;
4807 if (udev
->state
== USB_STATE_NOTATTACHED
||
4808 udev
->state
== USB_STATE_SUSPENDED
) {
4809 dev_dbg(&udev
->dev
, "device reset not allowed in state %d\n",
4814 /* Prevent autosuspend during the reset */
4815 usb_autoresume_device(udev
);
4818 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
4819 struct usb_interface
*cintf
= config
->interface
[i
];
4820 struct usb_driver
*drv
;
4823 if (cintf
->dev
.driver
) {
4824 drv
= to_usb_driver(cintf
->dev
.driver
);
4825 if (drv
->pre_reset
&& drv
->post_reset
)
4826 unbind
= (drv
->pre_reset
)(cintf
);
4827 else if (cintf
->condition
==
4828 USB_INTERFACE_BOUND
)
4831 usb_forced_unbind_intf(cintf
);
4836 ret
= usb_reset_and_verify_device(udev
);
4839 for (i
= config
->desc
.bNumInterfaces
- 1; i
>= 0; --i
) {
4840 struct usb_interface
*cintf
= config
->interface
[i
];
4841 struct usb_driver
*drv
;
4842 int rebind
= cintf
->needs_binding
;
4844 if (!rebind
&& cintf
->dev
.driver
) {
4845 drv
= to_usb_driver(cintf
->dev
.driver
);
4846 if (drv
->post_reset
)
4847 rebind
= (drv
->post_reset
)(cintf
);
4848 else if (cintf
->condition
==
4849 USB_INTERFACE_BOUND
)
4852 if (ret
== 0 && rebind
)
4853 usb_rebind_intf(cintf
);
4857 usb_autosuspend_device(udev
);
4860 EXPORT_SYMBOL_GPL(usb_reset_device
);
4864 * usb_queue_reset_device - Reset a USB device from an atomic context
4865 * @iface: USB interface belonging to the device to reset
4867 * This function can be used to reset a USB device from an atomic
4868 * context, where usb_reset_device() won't work (as it blocks).
4870 * Doing a reset via this method is functionally equivalent to calling
4871 * usb_reset_device(), except for the fact that it is delayed to a
4872 * workqueue. This means that any drivers bound to other interfaces
4873 * might be unbound, as well as users from usbfs in user space.
4877 * - Scheduling two resets at the same time from two different drivers
4878 * attached to two different interfaces of the same device is
4879 * possible; depending on how the driver attached to each interface
4880 * handles ->pre_reset(), the second reset might happen or not.
4882 * - If a driver is unbound and it had a pending reset, the reset will
4885 * - This function can be called during .probe() or .disconnect()
4886 * times. On return from .disconnect(), any pending resets will be
4889 * There is no no need to lock/unlock the @reset_ws as schedule_work()
4892 * NOTE: We don't do any reference count tracking because it is not
4893 * needed. The lifecycle of the work_struct is tied to the
4894 * usb_interface. Before destroying the interface we cancel the
4895 * work_struct, so the fact that work_struct is queued and or
4896 * running means the interface (and thus, the device) exist and
4899 void usb_queue_reset_device(struct usb_interface
*iface
)
4901 schedule_work(&iface
->reset_ws
);
4903 EXPORT_SYMBOL_GPL(usb_queue_reset_device
);