4 * (C) Copyright 1999 Linus Torvalds
5 * (C) Copyright 1999 Johannes Erdfelt
6 * (C) Copyright 1999 Gregory P. Smith
7 * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/completion.h>
16 #include <linux/sched.h>
17 #include <linux/list.h>
18 #include <linux/slab.h>
19 #include <linux/ioctl.h>
20 #include <linux/usb.h>
21 #include <linux/usbdevice_fs.h>
22 #include <linux/usb/hcd.h>
23 #include <linux/usb/otg.h>
24 #include <linux/usb/quirks.h>
25 #include <linux/kthread.h>
26 #include <linux/mutex.h>
27 #include <linux/freezer.h>
28 #include <linux/random.h>
30 #include <asm/uaccess.h>
31 #include <asm/byteorder.h>
35 /* if we are in debug mode, always announce new devices */
37 #ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
38 #define CONFIG_USB_ANNOUNCE_NEW_DEVICES
42 #define USB_VENDOR_GENESYS_LOGIC 0x05e3
43 #define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01
46 struct usb_device
*child
;
48 struct dev_state
*port_owner
;
49 enum usb_port_connect_type connect_type
;
53 struct device
*intfdev
; /* the "interface" device */
54 struct usb_device
*hdev
;
56 struct urb
*urb
; /* for interrupt polling pipe */
58 /* buffer for urb ... with extra space in case of babble */
61 struct usb_hub_status hub
;
62 struct usb_port_status port
;
63 } *status
; /* buffer for status reports */
64 struct mutex status_mutex
; /* for the status buffer */
66 int error
; /* last reported error */
67 int nerrors
; /* track consecutive errors */
69 struct list_head event_list
; /* hubs w/data or errs ready */
70 unsigned long event_bits
[1]; /* status change bitmask */
71 unsigned long change_bits
[1]; /* ports with logical connect
73 unsigned long busy_bits
[1]; /* ports being reset or
75 unsigned long removed_bits
[1]; /* ports with a "removed"
77 unsigned long wakeup_bits
[1]; /* ports that have signaled
79 #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
80 #error event_bits[] is too short!
83 struct usb_hub_descriptor
*descriptor
; /* class descriptor */
84 struct usb_tt tt
; /* Transaction Translator */
86 unsigned mA_per_port
; /* current for each child */
88 unsigned limited_power
:1;
90 unsigned disconnected
:1;
92 unsigned quirk_check_port_auto_suspend
:1;
94 unsigned has_indicators
:1;
95 u8 indicator
[USB_MAXCHILDREN
];
96 struct delayed_work leds
;
97 struct delayed_work init_work
;
98 struct usb_port
**ports
;
101 static inline int hub_is_superspeed(struct usb_device
*hdev
)
103 return (hdev
->descriptor
.bDeviceProtocol
== USB_HUB_PR_SS
);
106 /* Protect struct usb_device->state and ->children members
107 * Note: Both are also protected by ->dev.sem, except that ->state can
108 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
109 static DEFINE_SPINLOCK(device_state_lock
);
111 /* khubd's worklist and its lock */
112 static DEFINE_SPINLOCK(hub_event_lock
);
113 static LIST_HEAD(hub_event_list
); /* List of hubs needing servicing */
116 static DECLARE_WAIT_QUEUE_HEAD(khubd_wait
);
118 static struct task_struct
*khubd_task
;
120 /* cycle leds on hubs that aren't blinking for attention */
121 static bool blinkenlights
= 0;
122 module_param (blinkenlights
, bool, S_IRUGO
);
123 MODULE_PARM_DESC (blinkenlights
, "true to cycle leds on hubs");
126 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
127 * 10 seconds to send reply for the initial 64-byte descriptor request.
129 /* define initial 64-byte descriptor request timeout in milliseconds */
130 static int initial_descriptor_timeout
= USB_CTRL_GET_TIMEOUT
;
131 module_param(initial_descriptor_timeout
, int, S_IRUGO
|S_IWUSR
);
132 MODULE_PARM_DESC(initial_descriptor_timeout
,
133 "initial 64-byte descriptor request timeout in milliseconds "
134 "(default 5000 - 5.0 seconds)");
137 * As of 2.6.10 we introduce a new USB device initialization scheme which
138 * closely resembles the way Windows works. Hopefully it will be compatible
139 * with a wider range of devices than the old scheme. However some previously
140 * working devices may start giving rise to "device not accepting address"
141 * errors; if that happens the user can try the old scheme by adjusting the
142 * following module parameters.
144 * For maximum flexibility there are two boolean parameters to control the
145 * hub driver's behavior. On the first initialization attempt, if the
146 * "old_scheme_first" parameter is set then the old scheme will be used,
147 * otherwise the new scheme is used. If that fails and "use_both_schemes"
148 * is set, then the driver will make another attempt, using the other scheme.
150 static bool old_scheme_first
= 0;
151 module_param(old_scheme_first
, bool, S_IRUGO
| S_IWUSR
);
152 MODULE_PARM_DESC(old_scheme_first
,
153 "start with the old device initialization scheme");
155 static bool use_both_schemes
= 1;
156 module_param(use_both_schemes
, bool, S_IRUGO
| S_IWUSR
);
157 MODULE_PARM_DESC(use_both_schemes
,
158 "try the other device initialization scheme if the "
161 /* Mutual exclusion for EHCI CF initialization. This interferes with
162 * port reset on some companion controllers.
164 DECLARE_RWSEM(ehci_cf_port_reset_rwsem
);
165 EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem
);
167 #define HUB_DEBOUNCE_TIMEOUT 1500
168 #define HUB_DEBOUNCE_STEP 25
169 #define HUB_DEBOUNCE_STABLE 100
171 #define to_usb_port(_dev) \
172 container_of(_dev, struct usb_port, dev)
174 static int usb_reset_and_verify_device(struct usb_device
*udev
);
176 static inline char *portspeed(struct usb_hub
*hub
, int portstatus
)
178 if (hub_is_superspeed(hub
->hdev
))
180 if (portstatus
& USB_PORT_STAT_HIGH_SPEED
)
182 else if (portstatus
& USB_PORT_STAT_LOW_SPEED
)
188 /* Note that hdev or one of its children must be locked! */
189 static struct usb_hub
*hdev_to_hub(struct usb_device
*hdev
)
191 if (!hdev
|| !hdev
->actconfig
|| !hdev
->maxchild
)
193 return usb_get_intfdata(hdev
->actconfig
->interface
[0]);
196 static int usb_device_supports_lpm(struct usb_device
*udev
)
198 /* USB 2.1 (and greater) devices indicate LPM support through
199 * their USB 2.0 Extended Capabilities BOS descriptor.
201 if (udev
->speed
== USB_SPEED_HIGH
) {
202 if (udev
->bos
->ext_cap
&&
204 le32_to_cpu(udev
->bos
->ext_cap
->bmAttributes
)))
209 /* All USB 3.0 must support LPM, but we need their max exit latency
210 * information from the SuperSpeed Extended Capabilities BOS descriptor.
212 if (!udev
->bos
->ss_cap
) {
213 dev_warn(&udev
->dev
, "No LPM exit latency info found. "
214 "Power management will be impacted.\n");
217 if (udev
->parent
->lpm_capable
)
220 dev_warn(&udev
->dev
, "Parent hub missing LPM exit latency info. "
221 "Power management will be impacted.\n");
226 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
229 static void usb_set_lpm_mel(struct usb_device
*udev
,
230 struct usb3_lpm_parameters
*udev_lpm_params
,
231 unsigned int udev_exit_latency
,
233 struct usb3_lpm_parameters
*hub_lpm_params
,
234 unsigned int hub_exit_latency
)
236 unsigned int total_mel
;
237 unsigned int device_mel
;
238 unsigned int hub_mel
;
241 * Calculate the time it takes to transition all links from the roothub
242 * to the parent hub into U0. The parent hub must then decode the
243 * packet (hub header decode latency) to figure out which port it was
246 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
247 * means 0.1us). Multiply that by 100 to get nanoseconds.
249 total_mel
= hub_lpm_params
->mel
+
250 (hub
->descriptor
->u
.ss
.bHubHdrDecLat
* 100);
253 * How long will it take to transition the downstream hub's port into
254 * U0? The greater of either the hub exit latency or the device exit
257 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
258 * Multiply that by 1000 to get nanoseconds.
260 device_mel
= udev_exit_latency
* 1000;
261 hub_mel
= hub_exit_latency
* 1000;
262 if (device_mel
> hub_mel
)
263 total_mel
+= device_mel
;
265 total_mel
+= hub_mel
;
267 udev_lpm_params
->mel
= total_mel
;
271 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
272 * a transition from either U1 or U2.
274 static void usb_set_lpm_pel(struct usb_device
*udev
,
275 struct usb3_lpm_parameters
*udev_lpm_params
,
276 unsigned int udev_exit_latency
,
278 struct usb3_lpm_parameters
*hub_lpm_params
,
279 unsigned int hub_exit_latency
,
280 unsigned int port_to_port_exit_latency
)
282 unsigned int first_link_pel
;
283 unsigned int hub_pel
;
286 * First, the device sends an LFPS to transition the link between the
287 * device and the parent hub into U0. The exit latency is the bigger of
288 * the device exit latency or the hub exit latency.
290 if (udev_exit_latency
> hub_exit_latency
)
291 first_link_pel
= udev_exit_latency
* 1000;
293 first_link_pel
= hub_exit_latency
* 1000;
296 * When the hub starts to receive the LFPS, there is a slight delay for
297 * it to figure out that one of the ports is sending an LFPS. Then it
298 * will forward the LFPS to its upstream link. The exit latency is the
299 * delay, plus the PEL that we calculated for this hub.
301 hub_pel
= port_to_port_exit_latency
* 1000 + hub_lpm_params
->pel
;
304 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
305 * is the greater of the two exit latencies.
307 if (first_link_pel
> hub_pel
)
308 udev_lpm_params
->pel
= first_link_pel
;
310 udev_lpm_params
->pel
= hub_pel
;
314 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
315 * when a device initiates a transition to U0, until when it will receive the
316 * first packet from the host controller.
318 * Section C.1.5.1 describes the four components to this:
320 * - t2: time for the ERDY to make it from the device to the host.
321 * - t3: a host-specific delay to process the ERDY.
322 * - t4: time for the packet to make it from the host to the device.
324 * t3 is specific to both the xHCI host and the platform the host is integrated
325 * into. The Intel HW folks have said it's negligible, FIXME if a different
326 * vendor says otherwise.
328 static void usb_set_lpm_sel(struct usb_device
*udev
,
329 struct usb3_lpm_parameters
*udev_lpm_params
)
331 struct usb_device
*parent
;
332 unsigned int num_hubs
;
333 unsigned int total_sel
;
335 /* t1 = device PEL */
336 total_sel
= udev_lpm_params
->pel
;
337 /* How many external hubs are in between the device & the root port. */
338 for (parent
= udev
->parent
, num_hubs
= 0; parent
->parent
;
339 parent
= parent
->parent
)
341 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
343 total_sel
+= 2100 + 250 * (num_hubs
- 1);
345 /* t4 = 250ns * num_hubs */
346 total_sel
+= 250 * num_hubs
;
348 udev_lpm_params
->sel
= total_sel
;
351 static void usb_set_lpm_parameters(struct usb_device
*udev
)
354 unsigned int port_to_port_delay
;
355 unsigned int udev_u1_del
;
356 unsigned int udev_u2_del
;
357 unsigned int hub_u1_del
;
358 unsigned int hub_u2_del
;
360 if (!udev
->lpm_capable
|| udev
->speed
!= USB_SPEED_SUPER
)
363 hub
= hdev_to_hub(udev
->parent
);
364 /* It doesn't take time to transition the roothub into U0, since it
365 * doesn't have an upstream link.
370 udev_u1_del
= udev
->bos
->ss_cap
->bU1devExitLat
;
371 udev_u2_del
= udev
->bos
->ss_cap
->bU2DevExitLat
;
372 hub_u1_del
= udev
->parent
->bos
->ss_cap
->bU1devExitLat
;
373 hub_u2_del
= udev
->parent
->bos
->ss_cap
->bU2DevExitLat
;
375 usb_set_lpm_mel(udev
, &udev
->u1_params
, udev_u1_del
,
376 hub
, &udev
->parent
->u1_params
, hub_u1_del
);
378 usb_set_lpm_mel(udev
, &udev
->u2_params
, udev_u2_del
,
379 hub
, &udev
->parent
->u2_params
, hub_u2_del
);
382 * Appendix C, section C.2.2.2, says that there is a slight delay from
383 * when the parent hub notices the downstream port is trying to
384 * transition to U0 to when the hub initiates a U0 transition on its
385 * upstream port. The section says the delays are tPort2PortU1EL and
386 * tPort2PortU2EL, but it doesn't define what they are.
388 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
389 * about the same delays. Use the maximum delay calculations from those
390 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
391 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
392 * assume the device exit latencies they are talking about are the hub
395 * What do we do if the U2 exit latency is less than the U1 exit
396 * latency? It's possible, although not likely...
398 port_to_port_delay
= 1;
400 usb_set_lpm_pel(udev
, &udev
->u1_params
, udev_u1_del
,
401 hub
, &udev
->parent
->u1_params
, hub_u1_del
,
404 if (hub_u2_del
> hub_u1_del
)
405 port_to_port_delay
= 1 + hub_u2_del
- hub_u1_del
;
407 port_to_port_delay
= 1 + hub_u1_del
;
409 usb_set_lpm_pel(udev
, &udev
->u2_params
, udev_u2_del
,
410 hub
, &udev
->parent
->u2_params
, hub_u2_del
,
413 /* Now that we've got PEL, calculate SEL. */
414 usb_set_lpm_sel(udev
, &udev
->u1_params
);
415 usb_set_lpm_sel(udev
, &udev
->u2_params
);
418 /* USB 2.0 spec Section 11.24.4.5 */
419 static int get_hub_descriptor(struct usb_device
*hdev
, void *data
)
424 if (hub_is_superspeed(hdev
)) {
425 dtype
= USB_DT_SS_HUB
;
426 size
= USB_DT_SS_HUB_SIZE
;
429 size
= sizeof(struct usb_hub_descriptor
);
432 for (i
= 0; i
< 3; i
++) {
433 ret
= usb_control_msg(hdev
, usb_rcvctrlpipe(hdev
, 0),
434 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
| USB_RT_HUB
,
435 dtype
<< 8, 0, data
, size
,
436 USB_CTRL_GET_TIMEOUT
);
437 if (ret
>= (USB_DT_HUB_NONVAR_SIZE
+ 2))
444 * USB 2.0 spec Section 11.24.2.1
446 static int clear_hub_feature(struct usb_device
*hdev
, int feature
)
448 return usb_control_msg(hdev
, usb_sndctrlpipe(hdev
, 0),
449 USB_REQ_CLEAR_FEATURE
, USB_RT_HUB
, feature
, 0, NULL
, 0, 1000);
453 * USB 2.0 spec Section 11.24.2.2
455 static int clear_port_feature(struct usb_device
*hdev
, int port1
, int feature
)
457 return usb_control_msg(hdev
, usb_sndctrlpipe(hdev
, 0),
458 USB_REQ_CLEAR_FEATURE
, USB_RT_PORT
, feature
, port1
,
463 * USB 2.0 spec Section 11.24.2.13
465 static int set_port_feature(struct usb_device
*hdev
, int port1
, int feature
)
467 return usb_control_msg(hdev
, usb_sndctrlpipe(hdev
, 0),
468 USB_REQ_SET_FEATURE
, USB_RT_PORT
, feature
, port1
,
473 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
474 * for info about using port indicators
476 static void set_port_led(
482 int status
= set_port_feature(hub
->hdev
, (selector
<< 8) | port1
,
483 USB_PORT_FEAT_INDICATOR
);
485 dev_dbg (hub
->intfdev
,
486 "port %d indicator %s status %d\n",
488 ({ char *s
; switch (selector
) {
489 case HUB_LED_AMBER
: s
= "amber"; break;
490 case HUB_LED_GREEN
: s
= "green"; break;
491 case HUB_LED_OFF
: s
= "off"; break;
492 case HUB_LED_AUTO
: s
= "auto"; break;
493 default: s
= "??"; break;
498 #define LED_CYCLE_PERIOD ((2*HZ)/3)
500 static void led_work (struct work_struct
*work
)
502 struct usb_hub
*hub
=
503 container_of(work
, struct usb_hub
, leds
.work
);
504 struct usb_device
*hdev
= hub
->hdev
;
506 unsigned changed
= 0;
509 if (hdev
->state
!= USB_STATE_CONFIGURED
|| hub
->quiescing
)
512 for (i
= 0; i
< hub
->descriptor
->bNbrPorts
; i
++) {
513 unsigned selector
, mode
;
515 /* 30%-50% duty cycle */
517 switch (hub
->indicator
[i
]) {
519 case INDICATOR_CYCLE
:
521 selector
= HUB_LED_AUTO
;
522 mode
= INDICATOR_AUTO
;
524 /* blinking green = sw attention */
525 case INDICATOR_GREEN_BLINK
:
526 selector
= HUB_LED_GREEN
;
527 mode
= INDICATOR_GREEN_BLINK_OFF
;
529 case INDICATOR_GREEN_BLINK_OFF
:
530 selector
= HUB_LED_OFF
;
531 mode
= INDICATOR_GREEN_BLINK
;
533 /* blinking amber = hw attention */
534 case INDICATOR_AMBER_BLINK
:
535 selector
= HUB_LED_AMBER
;
536 mode
= INDICATOR_AMBER_BLINK_OFF
;
538 case INDICATOR_AMBER_BLINK_OFF
:
539 selector
= HUB_LED_OFF
;
540 mode
= INDICATOR_AMBER_BLINK
;
542 /* blink green/amber = reserved */
543 case INDICATOR_ALT_BLINK
:
544 selector
= HUB_LED_GREEN
;
545 mode
= INDICATOR_ALT_BLINK_OFF
;
547 case INDICATOR_ALT_BLINK_OFF
:
548 selector
= HUB_LED_AMBER
;
549 mode
= INDICATOR_ALT_BLINK
;
554 if (selector
!= HUB_LED_AUTO
)
556 set_port_led(hub
, i
+ 1, selector
);
557 hub
->indicator
[i
] = mode
;
559 if (!changed
&& blinkenlights
) {
561 cursor
%= hub
->descriptor
->bNbrPorts
;
562 set_port_led(hub
, cursor
+ 1, HUB_LED_GREEN
);
563 hub
->indicator
[cursor
] = INDICATOR_CYCLE
;
567 schedule_delayed_work(&hub
->leds
, LED_CYCLE_PERIOD
);
570 /* use a short timeout for hub/port status fetches */
571 #define USB_STS_TIMEOUT 1000
572 #define USB_STS_RETRIES 5
575 * USB 2.0 spec Section 11.24.2.6
577 static int get_hub_status(struct usb_device
*hdev
,
578 struct usb_hub_status
*data
)
580 int i
, status
= -ETIMEDOUT
;
582 for (i
= 0; i
< USB_STS_RETRIES
&&
583 (status
== -ETIMEDOUT
|| status
== -EPIPE
); i
++) {
584 status
= usb_control_msg(hdev
, usb_rcvctrlpipe(hdev
, 0),
585 USB_REQ_GET_STATUS
, USB_DIR_IN
| USB_RT_HUB
, 0, 0,
586 data
, sizeof(*data
), USB_STS_TIMEOUT
);
592 * USB 2.0 spec Section 11.24.2.7
594 static int get_port_status(struct usb_device
*hdev
, int port1
,
595 struct usb_port_status
*data
)
597 int i
, status
= -ETIMEDOUT
;
599 for (i
= 0; i
< USB_STS_RETRIES
&&
600 (status
== -ETIMEDOUT
|| status
== -EPIPE
); i
++) {
601 status
= usb_control_msg(hdev
, usb_rcvctrlpipe(hdev
, 0),
602 USB_REQ_GET_STATUS
, USB_DIR_IN
| USB_RT_PORT
, 0, port1
,
603 data
, sizeof(*data
), USB_STS_TIMEOUT
);
608 static int hub_port_status(struct usb_hub
*hub
, int port1
,
609 u16
*status
, u16
*change
)
613 mutex_lock(&hub
->status_mutex
);
614 ret
= get_port_status(hub
->hdev
, port1
, &hub
->status
->port
);
616 dev_err(hub
->intfdev
,
617 "%s failed (err = %d)\n", __func__
, ret
);
621 *status
= le16_to_cpu(hub
->status
->port
.wPortStatus
);
622 *change
= le16_to_cpu(hub
->status
->port
.wPortChange
);
626 mutex_unlock(&hub
->status_mutex
);
630 static void kick_khubd(struct usb_hub
*hub
)
634 spin_lock_irqsave(&hub_event_lock
, flags
);
635 if (!hub
->disconnected
&& list_empty(&hub
->event_list
)) {
636 list_add_tail(&hub
->event_list
, &hub_event_list
);
638 /* Suppress autosuspend until khubd runs */
639 usb_autopm_get_interface_no_resume(
640 to_usb_interface(hub
->intfdev
));
641 wake_up(&khubd_wait
);
643 spin_unlock_irqrestore(&hub_event_lock
, flags
);
646 void usb_kick_khubd(struct usb_device
*hdev
)
648 struct usb_hub
*hub
= hdev_to_hub(hdev
);
655 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
656 * Notification, which indicates it had initiated remote wakeup.
658 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
659 * device initiates resume, so the USB core will not receive notice of the
660 * resume through the normal hub interrupt URB.
662 void usb_wakeup_notification(struct usb_device
*hdev
,
663 unsigned int portnum
)
670 hub
= hdev_to_hub(hdev
);
672 set_bit(portnum
, hub
->wakeup_bits
);
676 EXPORT_SYMBOL_GPL(usb_wakeup_notification
);
678 /* completion function, fires on port status changes and various faults */
679 static void hub_irq(struct urb
*urb
)
681 struct usb_hub
*hub
= urb
->context
;
682 int status
= urb
->status
;
687 case -ENOENT
: /* synchronous unlink */
688 case -ECONNRESET
: /* async unlink */
689 case -ESHUTDOWN
: /* hardware going away */
692 default: /* presumably an error */
693 /* Cause a hub reset after 10 consecutive errors */
694 dev_dbg (hub
->intfdev
, "transfer --> %d\n", status
);
695 if ((++hub
->nerrors
< 10) || hub
->error
)
700 /* let khubd handle things */
701 case 0: /* we got data: port status changed */
703 for (i
= 0; i
< urb
->actual_length
; ++i
)
704 bits
|= ((unsigned long) ((*hub
->buffer
)[i
]))
706 hub
->event_bits
[0] = bits
;
712 /* Something happened, let khubd figure it out */
719 if ((status
= usb_submit_urb (hub
->urb
, GFP_ATOMIC
)) != 0
720 && status
!= -ENODEV
&& status
!= -EPERM
)
721 dev_err (hub
->intfdev
, "resubmit --> %d\n", status
);
724 /* USB 2.0 spec Section 11.24.2.3 */
726 hub_clear_tt_buffer (struct usb_device
*hdev
, u16 devinfo
, u16 tt
)
728 return usb_control_msg(hdev
, usb_sndctrlpipe(hdev
, 0),
729 HUB_CLEAR_TT_BUFFER
, USB_RT_PORT
, devinfo
,
734 * enumeration blocks khubd for a long time. we use keventd instead, since
735 * long blocking there is the exception, not the rule. accordingly, HCDs
736 * talking to TTs must queue control transfers (not just bulk and iso), so
737 * both can talk to the same hub concurrently.
739 static void hub_tt_work(struct work_struct
*work
)
741 struct usb_hub
*hub
=
742 container_of(work
, struct usb_hub
, tt
.clear_work
);
745 spin_lock_irqsave (&hub
->tt
.lock
, flags
);
746 while (!list_empty(&hub
->tt
.clear_list
)) {
747 struct list_head
*next
;
748 struct usb_tt_clear
*clear
;
749 struct usb_device
*hdev
= hub
->hdev
;
750 const struct hc_driver
*drv
;
753 next
= hub
->tt
.clear_list
.next
;
754 clear
= list_entry (next
, struct usb_tt_clear
, clear_list
);
755 list_del (&clear
->clear_list
);
757 /* drop lock so HCD can concurrently report other TT errors */
758 spin_unlock_irqrestore (&hub
->tt
.lock
, flags
);
759 status
= hub_clear_tt_buffer (hdev
, clear
->devinfo
, clear
->tt
);
762 "clear tt %d (%04x) error %d\n",
763 clear
->tt
, clear
->devinfo
, status
);
765 /* Tell the HCD, even if the operation failed */
766 drv
= clear
->hcd
->driver
;
767 if (drv
->clear_tt_buffer_complete
)
768 (drv
->clear_tt_buffer_complete
)(clear
->hcd
, clear
->ep
);
771 spin_lock_irqsave(&hub
->tt
.lock
, flags
);
773 spin_unlock_irqrestore (&hub
->tt
.lock
, flags
);
777 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
778 * @urb: an URB associated with the failed or incomplete split transaction
780 * High speed HCDs use this to tell the hub driver that some split control or
781 * bulk transaction failed in a way that requires clearing internal state of
782 * a transaction translator. This is normally detected (and reported) from
785 * It may not be possible for that hub to handle additional full (or low)
786 * speed transactions until that state is fully cleared out.
788 int usb_hub_clear_tt_buffer(struct urb
*urb
)
790 struct usb_device
*udev
= urb
->dev
;
791 int pipe
= urb
->pipe
;
792 struct usb_tt
*tt
= udev
->tt
;
794 struct usb_tt_clear
*clear
;
796 /* we've got to cope with an arbitrary number of pending TT clears,
797 * since each TT has "at least two" buffers that can need it (and
798 * there can be many TTs per hub). even if they're uncommon.
800 if ((clear
= kmalloc (sizeof *clear
, GFP_ATOMIC
)) == NULL
) {
801 dev_err (&udev
->dev
, "can't save CLEAR_TT_BUFFER state\n");
802 /* FIXME recover somehow ... RESET_TT? */
806 /* info that CLEAR_TT_BUFFER needs */
807 clear
->tt
= tt
->multi
? udev
->ttport
: 1;
808 clear
->devinfo
= usb_pipeendpoint (pipe
);
809 clear
->devinfo
|= udev
->devnum
<< 4;
810 clear
->devinfo
|= usb_pipecontrol (pipe
)
811 ? (USB_ENDPOINT_XFER_CONTROL
<< 11)
812 : (USB_ENDPOINT_XFER_BULK
<< 11);
813 if (usb_pipein (pipe
))
814 clear
->devinfo
|= 1 << 15;
816 /* info for completion callback */
817 clear
->hcd
= bus_to_hcd(udev
->bus
);
820 /* tell keventd to clear state for this TT */
821 spin_lock_irqsave (&tt
->lock
, flags
);
822 list_add_tail (&clear
->clear_list
, &tt
->clear_list
);
823 schedule_work(&tt
->clear_work
);
824 spin_unlock_irqrestore (&tt
->lock
, flags
);
827 EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer
);
829 /* If do_delay is false, return the number of milliseconds the caller
832 static unsigned hub_power_on(struct usb_hub
*hub
, bool do_delay
)
835 unsigned pgood_delay
= hub
->descriptor
->bPwrOn2PwrGood
* 2;
837 u16 wHubCharacteristics
=
838 le16_to_cpu(hub
->descriptor
->wHubCharacteristics
);
840 /* Enable power on each port. Some hubs have reserved values
841 * of LPSM (> 2) in their descriptors, even though they are
842 * USB 2.0 hubs. Some hubs do not implement port-power switching
843 * but only emulate it. In all cases, the ports won't work
844 * unless we send these messages to the hub.
846 if ((wHubCharacteristics
& HUB_CHAR_LPSM
) < 2)
847 dev_dbg(hub
->intfdev
, "enabling power on all ports\n");
849 dev_dbg(hub
->intfdev
, "trying to enable port power on "
850 "non-switchable hub\n");
851 for (port1
= 1; port1
<= hub
->descriptor
->bNbrPorts
; port1
++)
852 set_port_feature(hub
->hdev
, port1
, USB_PORT_FEAT_POWER
);
854 /* Wait at least 100 msec for power to become stable */
855 delay
= max(pgood_delay
, (unsigned) 100);
861 static int hub_hub_status(struct usb_hub
*hub
,
862 u16
*status
, u16
*change
)
866 mutex_lock(&hub
->status_mutex
);
867 ret
= get_hub_status(hub
->hdev
, &hub
->status
->hub
);
869 dev_err (hub
->intfdev
,
870 "%s failed (err = %d)\n", __func__
, ret
);
872 *status
= le16_to_cpu(hub
->status
->hub
.wHubStatus
);
873 *change
= le16_to_cpu(hub
->status
->hub
.wHubChange
);
876 mutex_unlock(&hub
->status_mutex
);
880 static int hub_set_port_link_state(struct usb_hub
*hub
, int port1
,
881 unsigned int link_status
)
883 return set_port_feature(hub
->hdev
,
884 port1
| (link_status
<< 3),
885 USB_PORT_FEAT_LINK_STATE
);
889 * If USB 3.0 ports are placed into the Disabled state, they will no longer
890 * detect any device connects or disconnects. This is generally not what the
891 * USB core wants, since it expects a disabled port to produce a port status
892 * change event when a new device connects.
894 * Instead, set the link state to Disabled, wait for the link to settle into
895 * that state, clear any change bits, and then put the port into the RxDetect
898 static int hub_usb3_port_disable(struct usb_hub
*hub
, int port1
)
902 u16 portchange
, portstatus
;
904 if (!hub_is_superspeed(hub
->hdev
))
907 ret
= hub_set_port_link_state(hub
, port1
, USB_SS_PORT_LS_SS_DISABLED
);
909 dev_err(hub
->intfdev
, "cannot disable port %d (err = %d)\n",
914 /* Wait for the link to enter the disabled state. */
915 for (total_time
= 0; ; total_time
+= HUB_DEBOUNCE_STEP
) {
916 ret
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
920 if ((portstatus
& USB_PORT_STAT_LINK_STATE
) ==
921 USB_SS_PORT_LS_SS_DISABLED
)
923 if (total_time
>= HUB_DEBOUNCE_TIMEOUT
)
925 msleep(HUB_DEBOUNCE_STEP
);
927 if (total_time
>= HUB_DEBOUNCE_TIMEOUT
)
928 dev_warn(hub
->intfdev
, "Could not disable port %d after %d ms\n",
931 return hub_set_port_link_state(hub
, port1
, USB_SS_PORT_LS_RX_DETECT
);
934 static int hub_port_disable(struct usb_hub
*hub
, int port1
, int set_state
)
936 struct usb_device
*hdev
= hub
->hdev
;
939 if (hub
->ports
[port1
- 1]->child
&& set_state
)
940 usb_set_device_state(hub
->ports
[port1
- 1]->child
,
941 USB_STATE_NOTATTACHED
);
943 if (hub_is_superspeed(hub
->hdev
))
944 ret
= hub_usb3_port_disable(hub
, port1
);
946 ret
= clear_port_feature(hdev
, port1
,
947 USB_PORT_FEAT_ENABLE
);
950 dev_err(hub
->intfdev
, "cannot disable port %d (err = %d)\n",
956 * Disable a port and mark a logical connect-change event, so that some
957 * time later khubd will disconnect() any existing usb_device on the port
958 * and will re-enumerate if there actually is a device attached.
960 static void hub_port_logical_disconnect(struct usb_hub
*hub
, int port1
)
962 dev_dbg(hub
->intfdev
, "logical disconnect on port %d\n", port1
);
963 hub_port_disable(hub
, port1
, 1);
965 /* FIXME let caller ask to power down the port:
966 * - some devices won't enumerate without a VBUS power cycle
967 * - SRP saves power that way
968 * - ... new call, TBD ...
969 * That's easy if this hub can switch power per-port, and
970 * khubd reactivates the port later (timer, SRP, etc).
971 * Powerdown must be optional, because of reset/DFU.
974 set_bit(port1
, hub
->change_bits
);
979 * usb_remove_device - disable a device's port on its parent hub
980 * @udev: device to be disabled and removed
981 * Context: @udev locked, must be able to sleep.
983 * After @udev's port has been disabled, khubd is notified and it will
984 * see that the device has been disconnected. When the device is
985 * physically unplugged and something is plugged in, the events will
986 * be received and processed normally.
988 int usb_remove_device(struct usb_device
*udev
)
991 struct usb_interface
*intf
;
993 if (!udev
->parent
) /* Can't remove a root hub */
995 hub
= hdev_to_hub(udev
->parent
);
996 intf
= to_usb_interface(hub
->intfdev
);
998 usb_autopm_get_interface(intf
);
999 set_bit(udev
->portnum
, hub
->removed_bits
);
1000 hub_port_logical_disconnect(hub
, udev
->portnum
);
1001 usb_autopm_put_interface(intf
);
1005 enum hub_activation_type
{
1006 HUB_INIT
, HUB_INIT2
, HUB_INIT3
, /* INITs must come first */
1007 HUB_POST_RESET
, HUB_RESUME
, HUB_RESET_RESUME
,
1010 static void hub_init_func2(struct work_struct
*ws
);
1011 static void hub_init_func3(struct work_struct
*ws
);
1013 static void hub_activate(struct usb_hub
*hub
, enum hub_activation_type type
)
1015 struct usb_device
*hdev
= hub
->hdev
;
1016 struct usb_hcd
*hcd
;
1020 bool need_debounce_delay
= false;
1023 /* Continue a partial initialization */
1024 if (type
== HUB_INIT2
)
1026 if (type
== HUB_INIT3
)
1029 /* The superspeed hub except for root hub has to use Hub Depth
1030 * value as an offset into the route string to locate the bits
1031 * it uses to determine the downstream port number. So hub driver
1032 * should send a set hub depth request to superspeed hub after
1033 * the superspeed hub is set configuration in initialization or
1036 * After a resume, port power should still be on.
1037 * For any other type of activation, turn it on.
1039 if (type
!= HUB_RESUME
) {
1040 if (hdev
->parent
&& hub_is_superspeed(hdev
)) {
1041 ret
= usb_control_msg(hdev
, usb_sndctrlpipe(hdev
, 0),
1042 HUB_SET_DEPTH
, USB_RT_HUB
,
1043 hdev
->level
- 1, 0, NULL
, 0,
1044 USB_CTRL_SET_TIMEOUT
);
1046 dev_err(hub
->intfdev
,
1047 "set hub depth failed\n");
1050 /* Speed up system boot by using a delayed_work for the
1051 * hub's initial power-up delays. This is pretty awkward
1052 * and the implementation looks like a home-brewed sort of
1053 * setjmp/longjmp, but it saves at least 100 ms for each
1054 * root hub (assuming usbcore is compiled into the kernel
1055 * rather than as a module). It adds up.
1057 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
1058 * because for those activation types the ports have to be
1059 * operational when we return. In theory this could be done
1060 * for HUB_POST_RESET, but it's easier not to.
1062 if (type
== HUB_INIT
) {
1063 delay
= hub_power_on(hub
, false);
1064 PREPARE_DELAYED_WORK(&hub
->init_work
, hub_init_func2
);
1065 schedule_delayed_work(&hub
->init_work
,
1066 msecs_to_jiffies(delay
));
1068 /* Suppress autosuspend until init is done */
1069 usb_autopm_get_interface_no_resume(
1070 to_usb_interface(hub
->intfdev
));
1071 return; /* Continues at init2: below */
1072 } else if (type
== HUB_RESET_RESUME
) {
1073 /* The internal host controller state for the hub device
1074 * may be gone after a host power loss on system resume.
1075 * Update the device's info so the HW knows it's a hub.
1077 hcd
= bus_to_hcd(hdev
->bus
);
1078 if (hcd
->driver
->update_hub_device
) {
1079 ret
= hcd
->driver
->update_hub_device(hcd
, hdev
,
1080 &hub
->tt
, GFP_NOIO
);
1082 dev_err(hub
->intfdev
, "Host not "
1083 "accepting hub info "
1085 dev_err(hub
->intfdev
, "LS/FS devices "
1086 "and hubs may not work "
1087 "under this hub\n.");
1090 hub_power_on(hub
, true);
1092 hub_power_on(hub
, true);
1097 /* Check each port and set hub->change_bits to let khubd know
1098 * which ports need attention.
1100 for (port1
= 1; port1
<= hdev
->maxchild
; ++port1
) {
1101 struct usb_device
*udev
= hub
->ports
[port1
- 1]->child
;
1102 u16 portstatus
, portchange
;
1104 portstatus
= portchange
= 0;
1105 status
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
1106 if (udev
|| (portstatus
& USB_PORT_STAT_CONNECTION
))
1107 dev_dbg(hub
->intfdev
,
1108 "port %d: status %04x change %04x\n",
1109 port1
, portstatus
, portchange
);
1111 /* After anything other than HUB_RESUME (i.e., initialization
1112 * or any sort of reset), every port should be disabled.
1113 * Unconnected ports should likewise be disabled (paranoia),
1114 * and so should ports for which we have no usb_device.
1116 if ((portstatus
& USB_PORT_STAT_ENABLE
) && (
1117 type
!= HUB_RESUME
||
1118 !(portstatus
& USB_PORT_STAT_CONNECTION
) ||
1120 udev
->state
== USB_STATE_NOTATTACHED
)) {
1122 * USB3 protocol ports will automatically transition
1123 * to Enabled state when detect an USB3.0 device attach.
1124 * Do not disable USB3 protocol ports.
1126 if (!hub_is_superspeed(hdev
)) {
1127 clear_port_feature(hdev
, port1
,
1128 USB_PORT_FEAT_ENABLE
);
1129 portstatus
&= ~USB_PORT_STAT_ENABLE
;
1131 /* Pretend that power was lost for USB3 devs */
1132 portstatus
&= ~USB_PORT_STAT_ENABLE
;
1136 /* Clear status-change flags; we'll debounce later */
1137 if (portchange
& USB_PORT_STAT_C_CONNECTION
) {
1138 need_debounce_delay
= true;
1139 clear_port_feature(hub
->hdev
, port1
,
1140 USB_PORT_FEAT_C_CONNECTION
);
1142 if (portchange
& USB_PORT_STAT_C_ENABLE
) {
1143 need_debounce_delay
= true;
1144 clear_port_feature(hub
->hdev
, port1
,
1145 USB_PORT_FEAT_C_ENABLE
);
1147 if ((portchange
& USB_PORT_STAT_C_BH_RESET
) &&
1148 hub_is_superspeed(hub
->hdev
)) {
1149 need_debounce_delay
= true;
1150 clear_port_feature(hub
->hdev
, port1
,
1151 USB_PORT_FEAT_C_BH_PORT_RESET
);
1153 /* We can forget about a "removed" device when there's a
1154 * physical disconnect or the connect status changes.
1156 if (!(portstatus
& USB_PORT_STAT_CONNECTION
) ||
1157 (portchange
& USB_PORT_STAT_C_CONNECTION
))
1158 clear_bit(port1
, hub
->removed_bits
);
1160 if (!udev
|| udev
->state
== USB_STATE_NOTATTACHED
) {
1161 /* Tell khubd to disconnect the device or
1162 * check for a new connection
1164 if (udev
|| (portstatus
& USB_PORT_STAT_CONNECTION
))
1165 set_bit(port1
, hub
->change_bits
);
1167 } else if (portstatus
& USB_PORT_STAT_ENABLE
) {
1168 bool port_resumed
= (portstatus
&
1169 USB_PORT_STAT_LINK_STATE
) ==
1171 /* The power session apparently survived the resume.
1172 * If there was an overcurrent or suspend change
1173 * (i.e., remote wakeup request), have khubd
1174 * take care of it. Look at the port link state
1175 * for USB 3.0 hubs, since they don't have a suspend
1176 * change bit, and they don't set the port link change
1177 * bit on device-initiated resume.
1179 if (portchange
|| (hub_is_superspeed(hub
->hdev
) &&
1181 set_bit(port1
, hub
->change_bits
);
1183 } else if (udev
->persist_enabled
) {
1185 udev
->reset_resume
= 1;
1187 set_bit(port1
, hub
->change_bits
);
1190 /* The power session is gone; tell khubd */
1191 usb_set_device_state(udev
, USB_STATE_NOTATTACHED
);
1192 set_bit(port1
, hub
->change_bits
);
1196 /* If no port-status-change flags were set, we don't need any
1197 * debouncing. If flags were set we can try to debounce the
1198 * ports all at once right now, instead of letting khubd do them
1199 * one at a time later on.
1201 * If any port-status changes do occur during this delay, khubd
1202 * will see them later and handle them normally.
1204 if (need_debounce_delay
) {
1205 delay
= HUB_DEBOUNCE_STABLE
;
1207 /* Don't do a long sleep inside a workqueue routine */
1208 if (type
== HUB_INIT2
) {
1209 PREPARE_DELAYED_WORK(&hub
->init_work
, hub_init_func3
);
1210 schedule_delayed_work(&hub
->init_work
,
1211 msecs_to_jiffies(delay
));
1212 return; /* Continues at init3: below */
1220 status
= usb_submit_urb(hub
->urb
, GFP_NOIO
);
1222 dev_err(hub
->intfdev
, "activate --> %d\n", status
);
1223 if (hub
->has_indicators
&& blinkenlights
)
1224 schedule_delayed_work(&hub
->leds
, LED_CYCLE_PERIOD
);
1226 /* Scan all ports that need attention */
1229 /* Allow autosuspend if it was suppressed */
1230 if (type
<= HUB_INIT3
)
1231 usb_autopm_put_interface_async(to_usb_interface(hub
->intfdev
));
1234 /* Implement the continuations for the delays above */
1235 static void hub_init_func2(struct work_struct
*ws
)
1237 struct usb_hub
*hub
= container_of(ws
, struct usb_hub
, init_work
.work
);
1239 hub_activate(hub
, HUB_INIT2
);
1242 static void hub_init_func3(struct work_struct
*ws
)
1244 struct usb_hub
*hub
= container_of(ws
, struct usb_hub
, init_work
.work
);
1246 hub_activate(hub
, HUB_INIT3
);
1249 enum hub_quiescing_type
{
1250 HUB_DISCONNECT
, HUB_PRE_RESET
, HUB_SUSPEND
1253 static void hub_quiesce(struct usb_hub
*hub
, enum hub_quiescing_type type
)
1255 struct usb_device
*hdev
= hub
->hdev
;
1258 cancel_delayed_work_sync(&hub
->init_work
);
1260 /* khubd and related activity won't re-trigger */
1263 if (type
!= HUB_SUSPEND
) {
1264 /* Disconnect all the children */
1265 for (i
= 0; i
< hdev
->maxchild
; ++i
) {
1266 if (hub
->ports
[i
]->child
)
1267 usb_disconnect(&hub
->ports
[i
]->child
);
1271 /* Stop khubd and related activity */
1272 usb_kill_urb(hub
->urb
);
1273 if (hub
->has_indicators
)
1274 cancel_delayed_work_sync(&hub
->leds
);
1276 flush_work(&hub
->tt
.clear_work
);
1279 /* caller has locked the hub device */
1280 static int hub_pre_reset(struct usb_interface
*intf
)
1282 struct usb_hub
*hub
= usb_get_intfdata(intf
);
1284 hub_quiesce(hub
, HUB_PRE_RESET
);
1288 /* caller has locked the hub device */
1289 static int hub_post_reset(struct usb_interface
*intf
)
1291 struct usb_hub
*hub
= usb_get_intfdata(intf
);
1293 hub_activate(hub
, HUB_POST_RESET
);
1297 static void usb_port_device_release(struct device
*dev
)
1299 struct usb_port
*port_dev
= to_usb_port(dev
);
1304 static void usb_hub_remove_port_device(struct usb_hub
*hub
,
1307 device_unregister(&hub
->ports
[port1
- 1]->dev
);
1310 struct device_type usb_port_device_type
= {
1312 .release
= usb_port_device_release
,
1315 static int usb_hub_create_port_device(struct usb_hub
*hub
,
1318 struct usb_port
*port_dev
= NULL
;
1321 port_dev
= kzalloc(sizeof(*port_dev
), GFP_KERNEL
);
1327 hub
->ports
[port1
- 1] = port_dev
;
1328 port_dev
->dev
.parent
= hub
->intfdev
;
1329 port_dev
->dev
.type
= &usb_port_device_type
;
1330 dev_set_name(&port_dev
->dev
, "port%d", port1
);
1332 retval
= device_register(&port_dev
->dev
);
1334 goto error_register
;
1338 put_device(&port_dev
->dev
);
1343 static int hub_configure(struct usb_hub
*hub
,
1344 struct usb_endpoint_descriptor
*endpoint
)
1346 struct usb_hcd
*hcd
;
1347 struct usb_device
*hdev
= hub
->hdev
;
1348 struct device
*hub_dev
= hub
->intfdev
;
1349 u16 hubstatus
, hubchange
;
1350 u16 wHubCharacteristics
;
1353 char *message
= "out of memory";
1355 hub
->buffer
= kmalloc(sizeof(*hub
->buffer
), GFP_KERNEL
);
1361 hub
->status
= kmalloc(sizeof(*hub
->status
), GFP_KERNEL
);
1366 mutex_init(&hub
->status_mutex
);
1368 hub
->descriptor
= kmalloc(sizeof(*hub
->descriptor
), GFP_KERNEL
);
1369 if (!hub
->descriptor
) {
1374 /* Request the entire hub descriptor.
1375 * hub->descriptor can handle USB_MAXCHILDREN ports,
1376 * but the hub can/will return fewer bytes here.
1378 ret
= get_hub_descriptor(hdev
, hub
->descriptor
);
1380 message
= "can't read hub descriptor";
1382 } else if (hub
->descriptor
->bNbrPorts
> USB_MAXCHILDREN
) {
1383 message
= "hub has too many ports!";
1388 hdev
->maxchild
= hub
->descriptor
->bNbrPorts
;
1389 dev_info (hub_dev
, "%d port%s detected\n", hdev
->maxchild
,
1390 (hdev
->maxchild
== 1) ? "" : "s");
1392 hub
->ports
= kzalloc(hdev
->maxchild
* sizeof(struct usb_port
*),
1399 wHubCharacteristics
= le16_to_cpu(hub
->descriptor
->wHubCharacteristics
);
1401 /* FIXME for USB 3.0, skip for now */
1402 if ((wHubCharacteristics
& HUB_CHAR_COMPOUND
) &&
1403 !(hub_is_superspeed(hdev
))) {
1405 char portstr
[USB_MAXCHILDREN
+ 1];
1407 for (i
= 0; i
< hdev
->maxchild
; i
++)
1408 portstr
[i
] = hub
->descriptor
->u
.hs
.DeviceRemovable
1409 [((i
+ 1) / 8)] & (1 << ((i
+ 1) % 8))
1411 portstr
[hdev
->maxchild
] = 0;
1412 dev_dbg(hub_dev
, "compound device; port removable status: %s\n", portstr
);
1414 dev_dbg(hub_dev
, "standalone hub\n");
1416 switch (wHubCharacteristics
& HUB_CHAR_LPSM
) {
1417 case HUB_CHAR_COMMON_LPSM
:
1418 dev_dbg(hub_dev
, "ganged power switching\n");
1420 case HUB_CHAR_INDV_PORT_LPSM
:
1421 dev_dbg(hub_dev
, "individual port power switching\n");
1423 case HUB_CHAR_NO_LPSM
:
1425 dev_dbg(hub_dev
, "no power switching (usb 1.0)\n");
1429 switch (wHubCharacteristics
& HUB_CHAR_OCPM
) {
1430 case HUB_CHAR_COMMON_OCPM
:
1431 dev_dbg(hub_dev
, "global over-current protection\n");
1433 case HUB_CHAR_INDV_PORT_OCPM
:
1434 dev_dbg(hub_dev
, "individual port over-current protection\n");
1436 case HUB_CHAR_NO_OCPM
:
1438 dev_dbg(hub_dev
, "no over-current protection\n");
1442 spin_lock_init (&hub
->tt
.lock
);
1443 INIT_LIST_HEAD (&hub
->tt
.clear_list
);
1444 INIT_WORK(&hub
->tt
.clear_work
, hub_tt_work
);
1445 switch (hdev
->descriptor
.bDeviceProtocol
) {
1448 case USB_HUB_PR_HS_SINGLE_TT
:
1449 dev_dbg(hub_dev
, "Single TT\n");
1452 case USB_HUB_PR_HS_MULTI_TT
:
1453 ret
= usb_set_interface(hdev
, 0, 1);
1455 dev_dbg(hub_dev
, "TT per port\n");
1458 dev_err(hub_dev
, "Using single TT (err %d)\n",
1463 /* USB 3.0 hubs don't have a TT */
1466 dev_dbg(hub_dev
, "Unrecognized hub protocol %d\n",
1467 hdev
->descriptor
.bDeviceProtocol
);
1471 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
1472 switch (wHubCharacteristics
& HUB_CHAR_TTTT
) {
1473 case HUB_TTTT_8_BITS
:
1474 if (hdev
->descriptor
.bDeviceProtocol
!= 0) {
1475 hub
->tt
.think_time
= 666;
1476 dev_dbg(hub_dev
, "TT requires at most %d "
1477 "FS bit times (%d ns)\n",
1478 8, hub
->tt
.think_time
);
1481 case HUB_TTTT_16_BITS
:
1482 hub
->tt
.think_time
= 666 * 2;
1483 dev_dbg(hub_dev
, "TT requires at most %d "
1484 "FS bit times (%d ns)\n",
1485 16, hub
->tt
.think_time
);
1487 case HUB_TTTT_24_BITS
:
1488 hub
->tt
.think_time
= 666 * 3;
1489 dev_dbg(hub_dev
, "TT requires at most %d "
1490 "FS bit times (%d ns)\n",
1491 24, hub
->tt
.think_time
);
1493 case HUB_TTTT_32_BITS
:
1494 hub
->tt
.think_time
= 666 * 4;
1495 dev_dbg(hub_dev
, "TT requires at most %d "
1496 "FS bit times (%d ns)\n",
1497 32, hub
->tt
.think_time
);
1501 /* probe() zeroes hub->indicator[] */
1502 if (wHubCharacteristics
& HUB_CHAR_PORTIND
) {
1503 hub
->has_indicators
= 1;
1504 dev_dbg(hub_dev
, "Port indicators are supported\n");
1507 dev_dbg(hub_dev
, "power on to power good time: %dms\n",
1508 hub
->descriptor
->bPwrOn2PwrGood
* 2);
1510 /* power budgeting mostly matters with bus-powered hubs,
1511 * and battery-powered root hubs (may provide just 8 mA).
1513 ret
= usb_get_status(hdev
, USB_RECIP_DEVICE
, 0, &hubstatus
);
1515 message
= "can't get hub status";
1518 le16_to_cpus(&hubstatus
);
1519 if (hdev
== hdev
->bus
->root_hub
) {
1520 if (hdev
->bus_mA
== 0 || hdev
->bus_mA
>= 500)
1521 hub
->mA_per_port
= 500;
1523 hub
->mA_per_port
= hdev
->bus_mA
;
1524 hub
->limited_power
= 1;
1526 } else if ((hubstatus
& (1 << USB_DEVICE_SELF_POWERED
)) == 0) {
1527 dev_dbg(hub_dev
, "hub controller current requirement: %dmA\n",
1528 hub
->descriptor
->bHubContrCurrent
);
1529 hub
->limited_power
= 1;
1530 if (hdev
->maxchild
> 0) {
1531 int remaining
= hdev
->bus_mA
-
1532 hub
->descriptor
->bHubContrCurrent
;
1534 if (remaining
< hdev
->maxchild
* 100)
1536 "insufficient power available "
1537 "to use all downstream ports\n");
1538 hub
->mA_per_port
= 100; /* 7.2.1.1 */
1540 } else { /* Self-powered external hub */
1541 /* FIXME: What about battery-powered external hubs that
1542 * provide less current per port? */
1543 hub
->mA_per_port
= 500;
1545 if (hub
->mA_per_port
< 500)
1546 dev_dbg(hub_dev
, "%umA bus power budget for each child\n",
1549 /* Update the HCD's internal representation of this hub before khubd
1550 * starts getting port status changes for devices under the hub.
1552 hcd
= bus_to_hcd(hdev
->bus
);
1553 if (hcd
->driver
->update_hub_device
) {
1554 ret
= hcd
->driver
->update_hub_device(hcd
, hdev
,
1555 &hub
->tt
, GFP_KERNEL
);
1557 message
= "can't update HCD hub info";
1562 ret
= hub_hub_status(hub
, &hubstatus
, &hubchange
);
1564 message
= "can't get hub status";
1568 /* local power status reports aren't always correct */
1569 if (hdev
->actconfig
->desc
.bmAttributes
& USB_CONFIG_ATT_SELFPOWER
)
1570 dev_dbg(hub_dev
, "local power source is %s\n",
1571 (hubstatus
& HUB_STATUS_LOCAL_POWER
)
1572 ? "lost (inactive)" : "good");
1574 if ((wHubCharacteristics
& HUB_CHAR_OCPM
) == 0)
1575 dev_dbg(hub_dev
, "%sover-current condition exists\n",
1576 (hubstatus
& HUB_STATUS_OVERCURRENT
) ? "" : "no ");
1578 /* set up the interrupt endpoint
1579 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1580 * bytes as USB2.0[11.12.3] says because some hubs are known
1581 * to send more data (and thus cause overflow). For root hubs,
1582 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1583 * to be big enough for at least USB_MAXCHILDREN ports. */
1584 pipe
= usb_rcvintpipe(hdev
, endpoint
->bEndpointAddress
);
1585 maxp
= usb_maxpacket(hdev
, pipe
, usb_pipeout(pipe
));
1587 if (maxp
> sizeof(*hub
->buffer
))
1588 maxp
= sizeof(*hub
->buffer
);
1590 hub
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
1596 usb_fill_int_urb(hub
->urb
, hdev
, pipe
, *hub
->buffer
, maxp
, hub_irq
,
1597 hub
, endpoint
->bInterval
);
1599 /* maybe cycle the hub leds */
1600 if (hub
->has_indicators
&& blinkenlights
)
1601 hub
->indicator
[0] = INDICATOR_CYCLE
;
1603 for (i
= 0; i
< hdev
->maxchild
; i
++)
1604 if (usb_hub_create_port_device(hub
, i
+ 1) < 0)
1605 dev_err(hub
->intfdev
,
1606 "couldn't create port%d device.\n", i
+ 1);
1608 hub_activate(hub
, HUB_INIT
);
1612 dev_err (hub_dev
, "config failed, %s (err %d)\n",
1614 /* hub_disconnect() frees urb and descriptor */
1618 static void hub_release(struct kref
*kref
)
1620 struct usb_hub
*hub
= container_of(kref
, struct usb_hub
, kref
);
1622 usb_put_intf(to_usb_interface(hub
->intfdev
));
1626 static unsigned highspeed_hubs
;
1628 static void hub_disconnect(struct usb_interface
*intf
)
1630 struct usb_hub
*hub
= usb_get_intfdata(intf
);
1631 struct usb_device
*hdev
= interface_to_usbdev(intf
);
1634 /* Take the hub off the event list and don't let it be added again */
1635 spin_lock_irq(&hub_event_lock
);
1636 if (!list_empty(&hub
->event_list
)) {
1637 list_del_init(&hub
->event_list
);
1638 usb_autopm_put_interface_no_suspend(intf
);
1640 hub
->disconnected
= 1;
1641 spin_unlock_irq(&hub_event_lock
);
1643 /* Disconnect all children and quiesce the hub */
1645 hub_quiesce(hub
, HUB_DISCONNECT
);
1647 usb_set_intfdata (intf
, NULL
);
1649 for (i
= 0; i
< hdev
->maxchild
; i
++)
1650 usb_hub_remove_port_device(hub
, i
+ 1);
1651 hub
->hdev
->maxchild
= 0;
1653 if (hub
->hdev
->speed
== USB_SPEED_HIGH
)
1656 usb_free_urb(hub
->urb
);
1658 kfree(hub
->descriptor
);
1662 kref_put(&hub
->kref
, hub_release
);
1665 static int hub_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1667 struct usb_host_interface
*desc
;
1668 struct usb_endpoint_descriptor
*endpoint
;
1669 struct usb_device
*hdev
;
1670 struct usb_hub
*hub
;
1672 desc
= intf
->cur_altsetting
;
1673 hdev
= interface_to_usbdev(intf
);
1676 * Set default autosuspend delay as 0 to speedup bus suspend,
1677 * based on the below considerations:
1679 * - Unlike other drivers, the hub driver does not rely on the
1680 * autosuspend delay to provide enough time to handle a wakeup
1681 * event, and the submitted status URB is just to check future
1682 * change on hub downstream ports, so it is safe to do it.
1684 * - The patch might cause one or more auto supend/resume for
1685 * below very rare devices when they are plugged into hub
1688 * devices having trouble initializing, and disconnect
1689 * themselves from the bus and then reconnect a second
1692 * devices just for downloading firmware, and disconnects
1693 * themselves after completing it
1695 * For these quite rare devices, their drivers may change the
1696 * autosuspend delay of their parent hub in the probe() to one
1697 * appropriate value to avoid the subtle problem if someone
1700 * - The patch may cause one or more auto suspend/resume on
1701 * hub during running 'lsusb', but it is probably too
1702 * infrequent to worry about.
1704 * - Change autosuspend delay of hub can avoid unnecessary auto
1705 * suspend timer for hub, also may decrease power consumption
1708 pm_runtime_set_autosuspend_delay(&hdev
->dev
, 0);
1710 /* Hubs have proper suspend/resume support. */
1711 usb_enable_autosuspend(hdev
);
1713 if (hdev
->level
== MAX_TOPO_LEVEL
) {
1715 "Unsupported bus topology: hub nested too deep\n");
1719 #ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1721 dev_warn(&intf
->dev
, "ignoring external hub\n");
1726 /* Some hubs have a subclass of 1, which AFAICT according to the */
1727 /* specs is not defined, but it works */
1728 if ((desc
->desc
.bInterfaceSubClass
!= 0) &&
1729 (desc
->desc
.bInterfaceSubClass
!= 1)) {
1731 dev_err (&intf
->dev
, "bad descriptor, ignoring hub\n");
1735 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1736 if (desc
->desc
.bNumEndpoints
!= 1)
1737 goto descriptor_error
;
1739 endpoint
= &desc
->endpoint
[0].desc
;
1741 /* If it's not an interrupt in endpoint, we'd better punt! */
1742 if (!usb_endpoint_is_int_in(endpoint
))
1743 goto descriptor_error
;
1745 /* We found a hub */
1746 dev_info (&intf
->dev
, "USB hub found\n");
1748 hub
= kzalloc(sizeof(*hub
), GFP_KERNEL
);
1750 dev_dbg (&intf
->dev
, "couldn't kmalloc hub struct\n");
1754 kref_init(&hub
->kref
);
1755 INIT_LIST_HEAD(&hub
->event_list
);
1756 hub
->intfdev
= &intf
->dev
;
1758 INIT_DELAYED_WORK(&hub
->leds
, led_work
);
1759 INIT_DELAYED_WORK(&hub
->init_work
, NULL
);
1762 usb_set_intfdata (intf
, hub
);
1763 intf
->needs_remote_wakeup
= 1;
1765 if (hdev
->speed
== USB_SPEED_HIGH
)
1768 if (id
->driver_info
& HUB_QUIRK_CHECK_PORT_AUTOSUSPEND
)
1769 hub
->quirk_check_port_auto_suspend
= 1;
1771 if (hub_configure(hub
, endpoint
) >= 0)
1774 hub_disconnect (intf
);
1779 hub_ioctl(struct usb_interface
*intf
, unsigned int code
, void *user_data
)
1781 struct usb_device
*hdev
= interface_to_usbdev (intf
);
1782 struct usb_hub
*hub
= hdev_to_hub(hdev
);
1784 /* assert ifno == 0 (part of hub spec) */
1786 case USBDEVFS_HUB_PORTINFO
: {
1787 struct usbdevfs_hub_portinfo
*info
= user_data
;
1790 spin_lock_irq(&device_state_lock
);
1791 if (hdev
->devnum
<= 0)
1794 info
->nports
= hdev
->maxchild
;
1795 for (i
= 0; i
< info
->nports
; i
++) {
1796 if (hub
->ports
[i
]->child
== NULL
)
1800 hub
->ports
[i
]->child
->devnum
;
1803 spin_unlock_irq(&device_state_lock
);
1805 return info
->nports
+ 1;
1814 * Allow user programs to claim ports on a hub. When a device is attached
1815 * to one of these "claimed" ports, the program will "own" the device.
1817 static int find_port_owner(struct usb_device
*hdev
, unsigned port1
,
1818 struct dev_state
***ppowner
)
1820 if (hdev
->state
== USB_STATE_NOTATTACHED
)
1822 if (port1
== 0 || port1
> hdev
->maxchild
)
1825 /* This assumes that devices not managed by the hub driver
1826 * will always have maxchild equal to 0.
1828 *ppowner
= &(hdev_to_hub(hdev
)->ports
[port1
- 1]->port_owner
);
1832 /* In the following three functions, the caller must hold hdev's lock */
1833 int usb_hub_claim_port(struct usb_device
*hdev
, unsigned port1
,
1834 struct dev_state
*owner
)
1837 struct dev_state
**powner
;
1839 rc
= find_port_owner(hdev
, port1
, &powner
);
1848 int usb_hub_release_port(struct usb_device
*hdev
, unsigned port1
,
1849 struct dev_state
*owner
)
1852 struct dev_state
**powner
;
1854 rc
= find_port_owner(hdev
, port1
, &powner
);
1857 if (*powner
!= owner
)
1863 void usb_hub_release_all_ports(struct usb_device
*hdev
, struct dev_state
*owner
)
1865 struct usb_hub
*hub
= hdev_to_hub(hdev
);
1868 for (n
= 0; n
< hdev
->maxchild
; n
++) {
1869 if (hub
->ports
[n
]->port_owner
== owner
)
1870 hub
->ports
[n
]->port_owner
= NULL
;
1875 /* The caller must hold udev's lock */
1876 bool usb_device_is_owned(struct usb_device
*udev
)
1878 struct usb_hub
*hub
;
1880 if (udev
->state
== USB_STATE_NOTATTACHED
|| !udev
->parent
)
1882 hub
= hdev_to_hub(udev
->parent
);
1883 return !!hub
->ports
[udev
->portnum
- 1]->port_owner
;
1886 static void recursively_mark_NOTATTACHED(struct usb_device
*udev
)
1888 struct usb_hub
*hub
= hdev_to_hub(udev
);
1891 for (i
= 0; i
< udev
->maxchild
; ++i
) {
1892 if (hub
->ports
[i
]->child
)
1893 recursively_mark_NOTATTACHED(hub
->ports
[i
]->child
);
1895 if (udev
->state
== USB_STATE_SUSPENDED
)
1896 udev
->active_duration
-= jiffies
;
1897 udev
->state
= USB_STATE_NOTATTACHED
;
1901 * usb_set_device_state - change a device's current state (usbcore, hcds)
1902 * @udev: pointer to device whose state should be changed
1903 * @new_state: new state value to be stored
1905 * udev->state is _not_ fully protected by the device lock. Although
1906 * most transitions are made only while holding the lock, the state can
1907 * can change to USB_STATE_NOTATTACHED at almost any time. This
1908 * is so that devices can be marked as disconnected as soon as possible,
1909 * without having to wait for any semaphores to be released. As a result,
1910 * all changes to any device's state must be protected by the
1911 * device_state_lock spinlock.
1913 * Once a device has been added to the device tree, all changes to its state
1914 * should be made using this routine. The state should _not_ be set directly.
1916 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1917 * Otherwise udev->state is set to new_state, and if new_state is
1918 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1919 * to USB_STATE_NOTATTACHED.
1921 void usb_set_device_state(struct usb_device
*udev
,
1922 enum usb_device_state new_state
)
1924 unsigned long flags
;
1927 spin_lock_irqsave(&device_state_lock
, flags
);
1928 if (udev
->state
== USB_STATE_NOTATTACHED
)
1930 else if (new_state
!= USB_STATE_NOTATTACHED
) {
1932 /* root hub wakeup capabilities are managed out-of-band
1933 * and may involve silicon errata ... ignore them here.
1936 if (udev
->state
== USB_STATE_SUSPENDED
1937 || new_state
== USB_STATE_SUSPENDED
)
1938 ; /* No change to wakeup settings */
1939 else if (new_state
== USB_STATE_CONFIGURED
)
1940 wakeup
= udev
->actconfig
->desc
.bmAttributes
1941 & USB_CONFIG_ATT_WAKEUP
;
1945 if (udev
->state
== USB_STATE_SUSPENDED
&&
1946 new_state
!= USB_STATE_SUSPENDED
)
1947 udev
->active_duration
-= jiffies
;
1948 else if (new_state
== USB_STATE_SUSPENDED
&&
1949 udev
->state
!= USB_STATE_SUSPENDED
)
1950 udev
->active_duration
+= jiffies
;
1951 udev
->state
= new_state
;
1953 recursively_mark_NOTATTACHED(udev
);
1954 spin_unlock_irqrestore(&device_state_lock
, flags
);
1956 device_set_wakeup_capable(&udev
->dev
, wakeup
);
1958 EXPORT_SYMBOL_GPL(usb_set_device_state
);
1961 * Choose a device number.
1963 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1964 * USB-2.0 buses they are also used as device addresses, however on
1965 * USB-3.0 buses the address is assigned by the controller hardware
1966 * and it usually is not the same as the device number.
1968 * WUSB devices are simple: they have no hubs behind, so the mapping
1969 * device <-> virtual port number becomes 1:1. Why? to simplify the
1970 * life of the device connection logic in
1971 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1972 * handshake we need to assign a temporary address in the unauthorized
1973 * space. For simplicity we use the first virtual port number found to
1974 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1975 * and that becomes it's address [X < 128] or its unauthorized address
1978 * We add 1 as an offset to the one-based USB-stack port number
1979 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1980 * 0 is reserved by USB for default address; (b) Linux's USB stack
1981 * uses always #1 for the root hub of the controller. So USB stack's
1982 * port #1, which is wusb virtual-port #0 has address #2.
1984 * Devices connected under xHCI are not as simple. The host controller
1985 * supports virtualization, so the hardware assigns device addresses and
1986 * the HCD must setup data structures before issuing a set address
1987 * command to the hardware.
1989 static void choose_devnum(struct usb_device
*udev
)
1992 struct usb_bus
*bus
= udev
->bus
;
1994 /* If khubd ever becomes multithreaded, this will need a lock */
1996 devnum
= udev
->portnum
+ 1;
1997 BUG_ON(test_bit(devnum
, bus
->devmap
.devicemap
));
1999 /* Try to allocate the next devnum beginning at
2000 * bus->devnum_next. */
2001 devnum
= find_next_zero_bit(bus
->devmap
.devicemap
, 128,
2004 devnum
= find_next_zero_bit(bus
->devmap
.devicemap
,
2006 bus
->devnum_next
= ( devnum
>= 127 ? 1 : devnum
+ 1);
2009 set_bit(devnum
, bus
->devmap
.devicemap
);
2010 udev
->devnum
= devnum
;
2014 static void release_devnum(struct usb_device
*udev
)
2016 if (udev
->devnum
> 0) {
2017 clear_bit(udev
->devnum
, udev
->bus
->devmap
.devicemap
);
2022 static void update_devnum(struct usb_device
*udev
, int devnum
)
2024 /* The address for a WUSB device is managed by wusbcore. */
2026 udev
->devnum
= devnum
;
2029 static void hub_free_dev(struct usb_device
*udev
)
2031 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
2033 /* Root hubs aren't real devices, so don't free HCD resources */
2034 if (hcd
->driver
->free_dev
&& udev
->parent
)
2035 hcd
->driver
->free_dev(hcd
, udev
);
2039 * usb_disconnect - disconnect a device (usbcore-internal)
2040 * @pdev: pointer to device being disconnected
2041 * Context: !in_interrupt ()
2043 * Something got disconnected. Get rid of it and all of its children.
2045 * If *pdev is a normal device then the parent hub must already be locked.
2046 * If *pdev is a root hub then this routine will acquire the
2047 * usb_bus_list_lock on behalf of the caller.
2049 * Only hub drivers (including virtual root hub drivers for host
2050 * controllers) should ever call this.
2052 * This call is synchronous, and may not be used in an interrupt context.
2054 void usb_disconnect(struct usb_device
**pdev
)
2056 struct usb_device
*udev
= *pdev
;
2057 struct usb_hub
*hub
= hdev_to_hub(udev
);
2060 /* mark the device as inactive, so any further urb submissions for
2061 * this device (and any of its children) will fail immediately.
2062 * this quiesces everything except pending urbs.
2064 usb_set_device_state(udev
, USB_STATE_NOTATTACHED
);
2065 dev_info(&udev
->dev
, "USB disconnect, device number %d\n",
2068 usb_lock_device(udev
);
2070 /* Free up all the children before we remove this device */
2071 for (i
= 0; i
< udev
->maxchild
; i
++) {
2072 if (hub
->ports
[i
]->child
)
2073 usb_disconnect(&hub
->ports
[i
]->child
);
2076 /* deallocate hcd/hardware state ... nuking all pending urbs and
2077 * cleaning up all state associated with the current configuration
2078 * so that the hardware is now fully quiesced.
2080 dev_dbg (&udev
->dev
, "unregistering device\n");
2081 usb_disable_device(udev
, 0);
2082 usb_hcd_synchronize_unlinks(udev
);
2084 usb_remove_ep_devs(&udev
->ep0
);
2085 usb_unlock_device(udev
);
2087 /* Unregister the device. The device driver is responsible
2088 * for de-configuring the device and invoking the remove-device
2089 * notifier chain (used by usbfs and possibly others).
2091 device_del(&udev
->dev
);
2093 /* Free the device number and delete the parent's children[]
2094 * (or root_hub) pointer.
2096 release_devnum(udev
);
2098 /* Avoid races with recursively_mark_NOTATTACHED() */
2099 spin_lock_irq(&device_state_lock
);
2101 spin_unlock_irq(&device_state_lock
);
2105 put_device(&udev
->dev
);
2108 #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
2109 static void show_string(struct usb_device
*udev
, char *id
, char *string
)
2113 dev_info(&udev
->dev
, "%s: %s\n", id
, string
);
2116 static void announce_device(struct usb_device
*udev
)
2118 dev_info(&udev
->dev
, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2119 le16_to_cpu(udev
->descriptor
.idVendor
),
2120 le16_to_cpu(udev
->descriptor
.idProduct
));
2121 dev_info(&udev
->dev
,
2122 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
2123 udev
->descriptor
.iManufacturer
,
2124 udev
->descriptor
.iProduct
,
2125 udev
->descriptor
.iSerialNumber
);
2126 show_string(udev
, "Product", udev
->product
);
2127 show_string(udev
, "Manufacturer", udev
->manufacturer
);
2128 show_string(udev
, "SerialNumber", udev
->serial
);
2131 static inline void announce_device(struct usb_device
*udev
) { }
2134 #ifdef CONFIG_USB_OTG
2135 #include "otg_whitelist.h"
2139 * usb_enumerate_device_otg - FIXME (usbcore-internal)
2140 * @udev: newly addressed device (in ADDRESS state)
2142 * Finish enumeration for On-The-Go devices
2144 static int usb_enumerate_device_otg(struct usb_device
*udev
)
2148 #ifdef CONFIG_USB_OTG
2150 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2151 * to wake us after we've powered off VBUS; and HNP, switching roles
2152 * "host" to "peripheral". The OTG descriptor helps figure this out.
2154 if (!udev
->bus
->is_b_host
2156 && udev
->parent
== udev
->bus
->root_hub
) {
2157 struct usb_otg_descriptor
*desc
= NULL
;
2158 struct usb_bus
*bus
= udev
->bus
;
2160 /* descriptor may appear anywhere in config */
2161 if (__usb_get_extra_descriptor (udev
->rawdescriptors
[0],
2162 le16_to_cpu(udev
->config
[0].desc
.wTotalLength
),
2163 USB_DT_OTG
, (void **) &desc
) == 0) {
2164 if (desc
->bmAttributes
& USB_OTG_HNP
) {
2165 unsigned port1
= udev
->portnum
;
2167 dev_info(&udev
->dev
,
2168 "Dual-Role OTG device on %sHNP port\n",
2169 (port1
== bus
->otg_port
)
2172 /* enable HNP before suspend, it's simpler */
2173 if (port1
== bus
->otg_port
)
2174 bus
->b_hnp_enable
= 1;
2175 err
= usb_control_msg(udev
,
2176 usb_sndctrlpipe(udev
, 0),
2177 USB_REQ_SET_FEATURE
, 0,
2179 ? USB_DEVICE_B_HNP_ENABLE
2180 : USB_DEVICE_A_ALT_HNP_SUPPORT
,
2181 0, NULL
, 0, USB_CTRL_SET_TIMEOUT
);
2183 /* OTG MESSAGE: report errors here,
2184 * customize to match your product.
2186 dev_info(&udev
->dev
,
2187 "can't set HNP mode: %d\n",
2189 bus
->b_hnp_enable
= 0;
2195 if (!is_targeted(udev
)) {
2197 /* Maybe it can talk to us, though we can't talk to it.
2198 * (Includes HNP test device.)
2200 if (udev
->bus
->b_hnp_enable
|| udev
->bus
->is_b_host
) {
2201 err
= usb_port_suspend(udev
, PMSG_SUSPEND
);
2203 dev_dbg(&udev
->dev
, "HNP fail, %d\n", err
);
2215 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
2216 * @udev: newly addressed device (in ADDRESS state)
2218 * This is only called by usb_new_device() and usb_authorize_device()
2219 * and FIXME -- all comments that apply to them apply here wrt to
2222 * If the device is WUSB and not authorized, we don't attempt to read
2223 * the string descriptors, as they will be errored out by the device
2224 * until it has been authorized.
2226 static int usb_enumerate_device(struct usb_device
*udev
)
2230 if (udev
->config
== NULL
) {
2231 err
= usb_get_configuration(udev
);
2233 dev_err(&udev
->dev
, "can't read configurations, error %d\n",
2238 if (udev
->wusb
== 1 && udev
->authorized
== 0) {
2239 udev
->product
= kstrdup("n/a (unauthorized)", GFP_KERNEL
);
2240 udev
->manufacturer
= kstrdup("n/a (unauthorized)", GFP_KERNEL
);
2241 udev
->serial
= kstrdup("n/a (unauthorized)", GFP_KERNEL
);
2244 /* read the standard strings and cache them if present */
2245 udev
->product
= usb_cache_string(udev
, udev
->descriptor
.iProduct
);
2246 udev
->manufacturer
= usb_cache_string(udev
,
2247 udev
->descriptor
.iManufacturer
);
2248 udev
->serial
= usb_cache_string(udev
, udev
->descriptor
.iSerialNumber
);
2250 err
= usb_enumerate_device_otg(udev
);
2254 usb_detect_interface_quirks(udev
);
2259 static void set_usb_port_removable(struct usb_device
*udev
)
2261 struct usb_device
*hdev
= udev
->parent
;
2262 struct usb_hub
*hub
;
2263 u8 port
= udev
->portnum
;
2264 u16 wHubCharacteristics
;
2265 bool removable
= true;
2270 hub
= hdev_to_hub(udev
->parent
);
2272 wHubCharacteristics
= le16_to_cpu(hub
->descriptor
->wHubCharacteristics
);
2274 if (!(wHubCharacteristics
& HUB_CHAR_COMPOUND
))
2277 if (hub_is_superspeed(hdev
)) {
2278 if (le16_to_cpu(hub
->descriptor
->u
.ss
.DeviceRemovable
)
2282 if (hub
->descriptor
->u
.hs
.DeviceRemovable
[port
/ 8] & (1 << (port
% 8)))
2287 udev
->removable
= USB_DEVICE_REMOVABLE
;
2289 udev
->removable
= USB_DEVICE_FIXED
;
2293 * usb_new_device - perform initial device setup (usbcore-internal)
2294 * @udev: newly addressed device (in ADDRESS state)
2296 * This is called with devices which have been detected but not fully
2297 * enumerated. The device descriptor is available, but not descriptors
2298 * for any device configuration. The caller must have locked either
2299 * the parent hub (if udev is a normal device) or else the
2300 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2301 * udev has already been installed, but udev is not yet visible through
2302 * sysfs or other filesystem code.
2304 * It will return if the device is configured properly or not. Zero if
2305 * the interface was registered with the driver core; else a negative
2308 * This call is synchronous, and may not be used in an interrupt context.
2310 * Only the hub driver or root-hub registrar should ever call this.
2312 int usb_new_device(struct usb_device
*udev
)
2317 /* Initialize non-root-hub device wakeup to disabled;
2318 * device (un)configuration controls wakeup capable
2319 * sysfs power/wakeup controls wakeup enabled/disabled
2321 device_init_wakeup(&udev
->dev
, 0);
2324 /* Tell the runtime-PM framework the device is active */
2325 pm_runtime_set_active(&udev
->dev
);
2326 pm_runtime_get_noresume(&udev
->dev
);
2327 pm_runtime_use_autosuspend(&udev
->dev
);
2328 pm_runtime_enable(&udev
->dev
);
2330 /* By default, forbid autosuspend for all devices. It will be
2331 * allowed for hubs during binding.
2333 usb_disable_autosuspend(udev
);
2335 err
= usb_enumerate_device(udev
); /* Read descriptors */
2338 dev_dbg(&udev
->dev
, "udev %d, busnum %d, minor = %d\n",
2339 udev
->devnum
, udev
->bus
->busnum
,
2340 (((udev
->bus
->busnum
-1) * 128) + (udev
->devnum
-1)));
2341 /* export the usbdev device-node for libusb */
2342 udev
->dev
.devt
= MKDEV(USB_DEVICE_MAJOR
,
2343 (((udev
->bus
->busnum
-1) * 128) + (udev
->devnum
-1)));
2345 /* Tell the world! */
2346 announce_device(udev
);
2349 add_device_randomness(udev
->serial
, strlen(udev
->serial
));
2351 add_device_randomness(udev
->product
, strlen(udev
->product
));
2352 if (udev
->manufacturer
)
2353 add_device_randomness(udev
->manufacturer
,
2354 strlen(udev
->manufacturer
));
2356 device_enable_async_suspend(&udev
->dev
);
2359 * check whether the hub marks this port as non-removable. Do it
2360 * now so that platform-specific data can override it in
2364 set_usb_port_removable(udev
);
2366 /* Register the device. The device driver is responsible
2367 * for configuring the device and invoking the add-device
2368 * notifier chain (used by usbfs and possibly others).
2370 err
= device_add(&udev
->dev
);
2372 dev_err(&udev
->dev
, "can't device_add, error %d\n", err
);
2376 (void) usb_create_ep_devs(&udev
->dev
, &udev
->ep0
, udev
);
2377 usb_mark_last_busy(udev
);
2378 pm_runtime_put_sync_autosuspend(&udev
->dev
);
2382 usb_set_device_state(udev
, USB_STATE_NOTATTACHED
);
2383 pm_runtime_disable(&udev
->dev
);
2384 pm_runtime_set_suspended(&udev
->dev
);
2390 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2391 * @usb_dev: USB device
2393 * Move the USB device to a very basic state where interfaces are disabled
2394 * and the device is in fact unconfigured and unusable.
2396 * We share a lock (that we have) with device_del(), so we need to
2399 int usb_deauthorize_device(struct usb_device
*usb_dev
)
2401 usb_lock_device(usb_dev
);
2402 if (usb_dev
->authorized
== 0)
2403 goto out_unauthorized
;
2405 usb_dev
->authorized
= 0;
2406 usb_set_configuration(usb_dev
, -1);
2408 kfree(usb_dev
->product
);
2409 usb_dev
->product
= kstrdup("n/a (unauthorized)", GFP_KERNEL
);
2410 kfree(usb_dev
->manufacturer
);
2411 usb_dev
->manufacturer
= kstrdup("n/a (unauthorized)", GFP_KERNEL
);
2412 kfree(usb_dev
->serial
);
2413 usb_dev
->serial
= kstrdup("n/a (unauthorized)", GFP_KERNEL
);
2415 usb_destroy_configuration(usb_dev
);
2416 usb_dev
->descriptor
.bNumConfigurations
= 0;
2419 usb_unlock_device(usb_dev
);
2424 int usb_authorize_device(struct usb_device
*usb_dev
)
2428 usb_lock_device(usb_dev
);
2429 if (usb_dev
->authorized
== 1)
2430 goto out_authorized
;
2432 result
= usb_autoresume_device(usb_dev
);
2434 dev_err(&usb_dev
->dev
,
2435 "can't autoresume for authorization: %d\n", result
);
2436 goto error_autoresume
;
2438 result
= usb_get_device_descriptor(usb_dev
, sizeof(usb_dev
->descriptor
));
2440 dev_err(&usb_dev
->dev
, "can't re-read device descriptor for "
2441 "authorization: %d\n", result
);
2442 goto error_device_descriptor
;
2445 kfree(usb_dev
->product
);
2446 usb_dev
->product
= NULL
;
2447 kfree(usb_dev
->manufacturer
);
2448 usb_dev
->manufacturer
= NULL
;
2449 kfree(usb_dev
->serial
);
2450 usb_dev
->serial
= NULL
;
2452 usb_dev
->authorized
= 1;
2453 result
= usb_enumerate_device(usb_dev
);
2455 goto error_enumerate
;
2456 /* Choose and set the configuration. This registers the interfaces
2457 * with the driver core and lets interface drivers bind to them.
2459 c
= usb_choose_configuration(usb_dev
);
2461 result
= usb_set_configuration(usb_dev
, c
);
2463 dev_err(&usb_dev
->dev
,
2464 "can't set config #%d, error %d\n", c
, result
);
2465 /* This need not be fatal. The user can try to
2466 * set other configurations. */
2469 dev_info(&usb_dev
->dev
, "authorized to connect\n");
2472 error_device_descriptor
:
2473 usb_autosuspend_device(usb_dev
);
2476 usb_unlock_device(usb_dev
); // complements locktree
2481 /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2482 static unsigned hub_is_wusb(struct usb_hub
*hub
)
2484 struct usb_hcd
*hcd
;
2485 if (hub
->hdev
->parent
!= NULL
) /* not a root hub? */
2487 hcd
= container_of(hub
->hdev
->bus
, struct usb_hcd
, self
);
2488 return hcd
->wireless
;
2492 #define PORT_RESET_TRIES 5
2493 #define SET_ADDRESS_TRIES 2
2494 #define GET_DESCRIPTOR_TRIES 2
2495 #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
2496 #define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
2498 #define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2499 #define HUB_SHORT_RESET_TIME 10
2500 #define HUB_BH_RESET_TIME 50
2501 #define HUB_LONG_RESET_TIME 200
2502 #define HUB_RESET_TIMEOUT 800
2504 static int hub_port_reset(struct usb_hub
*hub
, int port1
,
2505 struct usb_device
*udev
, unsigned int delay
, bool warm
);
2507 /* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2508 * Port worm reset is required to recover
2510 static bool hub_port_warm_reset_required(struct usb_hub
*hub
, u16 portstatus
)
2512 return hub_is_superspeed(hub
->hdev
) &&
2513 (((portstatus
& USB_PORT_STAT_LINK_STATE
) ==
2514 USB_SS_PORT_LS_SS_INACTIVE
) ||
2515 ((portstatus
& USB_PORT_STAT_LINK_STATE
) ==
2516 USB_SS_PORT_LS_COMP_MOD
)) ;
2519 static int hub_port_wait_reset(struct usb_hub
*hub
, int port1
,
2520 struct usb_device
*udev
, unsigned int delay
, bool warm
)
2522 int delay_time
, ret
;
2526 for (delay_time
= 0;
2527 delay_time
< HUB_RESET_TIMEOUT
;
2528 delay_time
+= delay
) {
2529 /* wait to give the device a chance to reset */
2532 /* read and decode port status */
2533 ret
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
2537 /* The port state is unknown until the reset completes. */
2538 if ((portstatus
& USB_PORT_STAT_RESET
))
2542 * Some buggy devices require a warm reset to be issued even
2543 * when the port appears not to be connected.
2547 * Some buggy devices can cause an NEC host controller
2548 * to transition to the "Error" state after a hot port
2549 * reset. This will show up as the port state in
2550 * "Inactive", and the port may also report a
2551 * disconnect. Forcing a warm port reset seems to make
2554 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2556 if (hub_port_warm_reset_required(hub
, portstatus
)) {
2559 if ((portchange
& USB_PORT_STAT_C_CONNECTION
))
2560 clear_port_feature(hub
->hdev
, port1
,
2561 USB_PORT_FEAT_C_CONNECTION
);
2562 if (portchange
& USB_PORT_STAT_C_LINK_STATE
)
2563 clear_port_feature(hub
->hdev
, port1
,
2564 USB_PORT_FEAT_C_PORT_LINK_STATE
);
2565 if (portchange
& USB_PORT_STAT_C_RESET
)
2566 clear_port_feature(hub
->hdev
, port1
,
2567 USB_PORT_FEAT_C_RESET
);
2568 dev_dbg(hub
->intfdev
, "hot reset failed, warm reset port %d\n",
2570 ret
= hub_port_reset(hub
, port1
,
2571 udev
, HUB_BH_RESET_TIME
,
2573 if ((portchange
& USB_PORT_STAT_C_CONNECTION
))
2574 clear_port_feature(hub
->hdev
, port1
,
2575 USB_PORT_FEAT_C_CONNECTION
);
2578 /* Device went away? */
2579 if (!(portstatus
& USB_PORT_STAT_CONNECTION
))
2582 /* bomb out completely if the connection bounced */
2583 if ((portchange
& USB_PORT_STAT_C_CONNECTION
))
2586 if ((portstatus
& USB_PORT_STAT_ENABLE
)) {
2587 if (hub_is_wusb(hub
))
2588 udev
->speed
= USB_SPEED_WIRELESS
;
2589 else if (hub_is_superspeed(hub
->hdev
))
2590 udev
->speed
= USB_SPEED_SUPER
;
2591 else if (portstatus
& USB_PORT_STAT_HIGH_SPEED
)
2592 udev
->speed
= USB_SPEED_HIGH
;
2593 else if (portstatus
& USB_PORT_STAT_LOW_SPEED
)
2594 udev
->speed
= USB_SPEED_LOW
;
2596 udev
->speed
= USB_SPEED_FULL
;
2600 if (!(portstatus
& USB_PORT_STAT_CONNECTION
) ||
2601 hub_port_warm_reset_required(hub
,
2609 /* switch to the long delay after two short delay failures */
2610 if (delay_time
>= 2 * HUB_SHORT_RESET_TIME
)
2611 delay
= HUB_LONG_RESET_TIME
;
2613 dev_dbg (hub
->intfdev
,
2614 "port %d not %sreset yet, waiting %dms\n",
2615 port1
, warm
? "warm " : "", delay
);
2621 static void hub_port_finish_reset(struct usb_hub
*hub
, int port1
,
2622 struct usb_device
*udev
, int *status
, bool warm
)
2627 struct usb_hcd
*hcd
;
2628 /* TRSTRCY = 10 ms; plus some extra */
2630 update_devnum(udev
, 0);
2631 hcd
= bus_to_hcd(udev
->bus
);
2632 /* The xHC may think the device is already reset,
2633 * so ignore the status.
2635 if (hcd
->driver
->reset_device
)
2636 hcd
->driver
->reset_device(hcd
, udev
);
2641 clear_port_feature(hub
->hdev
,
2642 port1
, USB_PORT_FEAT_C_RESET
);
2643 /* FIXME need disconnect() for NOTATTACHED device */
2644 if (hub_is_superspeed(hub
->hdev
)) {
2645 clear_port_feature(hub
->hdev
, port1
,
2646 USB_PORT_FEAT_C_BH_PORT_RESET
);
2647 clear_port_feature(hub
->hdev
, port1
,
2648 USB_PORT_FEAT_C_PORT_LINK_STATE
);
2651 usb_set_device_state(udev
, *status
2652 ? USB_STATE_NOTATTACHED
2653 : USB_STATE_DEFAULT
);
2658 /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
2659 static int hub_port_reset(struct usb_hub
*hub
, int port1
,
2660 struct usb_device
*udev
, unsigned int delay
, bool warm
)
2665 /* Block EHCI CF initialization during the port reset.
2666 * Some companion controllers don't like it when they mix.
2668 down_read(&ehci_cf_port_reset_rwsem
);
2670 if (!hub_is_superspeed(hub
->hdev
)) {
2671 dev_err(hub
->intfdev
, "only USB3 hub support "
2677 /* Reset the port */
2678 for (i
= 0; i
< PORT_RESET_TRIES
; i
++) {
2679 status
= set_port_feature(hub
->hdev
, port1
, (warm
?
2680 USB_PORT_FEAT_BH_PORT_RESET
:
2681 USB_PORT_FEAT_RESET
));
2683 dev_err(hub
->intfdev
,
2684 "cannot %sreset port %d (err = %d)\n",
2685 warm
? "warm " : "", port1
, status
);
2687 status
= hub_port_wait_reset(hub
, port1
, udev
, delay
,
2689 if (status
&& status
!= -ENOTCONN
)
2690 dev_dbg(hub
->intfdev
,
2691 "port_wait_reset: err = %d\n",
2695 /* return on disconnect or reset */
2696 if (status
== 0 || status
== -ENOTCONN
|| status
== -ENODEV
) {
2697 hub_port_finish_reset(hub
, port1
, udev
, &status
, warm
);
2701 dev_dbg (hub
->intfdev
,
2702 "port %d not enabled, trying %sreset again...\n",
2703 port1
, warm
? "warm " : "");
2704 delay
= HUB_LONG_RESET_TIME
;
2707 dev_err (hub
->intfdev
,
2708 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2713 up_read(&ehci_cf_port_reset_rwsem
);
2718 /* Check if a port is power on */
2719 static int port_is_power_on(struct usb_hub
*hub
, unsigned portstatus
)
2723 if (hub_is_superspeed(hub
->hdev
)) {
2724 if (portstatus
& USB_SS_PORT_STAT_POWER
)
2727 if (portstatus
& USB_PORT_STAT_POWER
)
2736 /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2737 static int port_is_suspended(struct usb_hub
*hub
, unsigned portstatus
)
2741 if (hub_is_superspeed(hub
->hdev
)) {
2742 if ((portstatus
& USB_PORT_STAT_LINK_STATE
)
2743 == USB_SS_PORT_LS_U3
)
2746 if (portstatus
& USB_PORT_STAT_SUSPEND
)
2753 /* Determine whether the device on a port is ready for a normal resume,
2754 * is ready for a reset-resume, or should be disconnected.
2756 static int check_port_resume_type(struct usb_device
*udev
,
2757 struct usb_hub
*hub
, int port1
,
2758 int status
, unsigned portchange
, unsigned portstatus
)
2760 /* Is the device still present? */
2761 if (status
|| port_is_suspended(hub
, portstatus
) ||
2762 !port_is_power_on(hub
, portstatus
) ||
2763 !(portstatus
& USB_PORT_STAT_CONNECTION
)) {
2768 /* Can't do a normal resume if the port isn't enabled,
2769 * so try a reset-resume instead.
2771 else if (!(portstatus
& USB_PORT_STAT_ENABLE
) && !udev
->reset_resume
) {
2772 if (udev
->persist_enabled
)
2773 udev
->reset_resume
= 1;
2779 dev_dbg(hub
->intfdev
,
2780 "port %d status %04x.%04x after resume, %d\n",
2781 port1
, portchange
, portstatus
, status
);
2782 } else if (udev
->reset_resume
) {
2784 /* Late port handoff can set status-change bits */
2785 if (portchange
& USB_PORT_STAT_C_CONNECTION
)
2786 clear_port_feature(hub
->hdev
, port1
,
2787 USB_PORT_FEAT_C_CONNECTION
);
2788 if (portchange
& USB_PORT_STAT_C_ENABLE
)
2789 clear_port_feature(hub
->hdev
, port1
,
2790 USB_PORT_FEAT_C_ENABLE
);
2796 int usb_disable_ltm(struct usb_device
*udev
)
2798 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
2800 /* Check if the roothub and device supports LTM. */
2801 if (!usb_device_supports_ltm(hcd
->self
.root_hub
) ||
2802 !usb_device_supports_ltm(udev
))
2805 /* Clear Feature LTM Enable can only be sent if the device is
2808 if (!udev
->actconfig
)
2811 return usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
2812 USB_REQ_CLEAR_FEATURE
, USB_RECIP_DEVICE
,
2813 USB_DEVICE_LTM_ENABLE
, 0, NULL
, 0,
2814 USB_CTRL_SET_TIMEOUT
);
2816 EXPORT_SYMBOL_GPL(usb_disable_ltm
);
2818 void usb_enable_ltm(struct usb_device
*udev
)
2820 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
2822 /* Check if the roothub and device supports LTM. */
2823 if (!usb_device_supports_ltm(hcd
->self
.root_hub
) ||
2824 !usb_device_supports_ltm(udev
))
2827 /* Set Feature LTM Enable can only be sent if the device is
2830 if (!udev
->actconfig
)
2833 usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
2834 USB_REQ_SET_FEATURE
, USB_RECIP_DEVICE
,
2835 USB_DEVICE_LTM_ENABLE
, 0, NULL
, 0,
2836 USB_CTRL_SET_TIMEOUT
);
2838 EXPORT_SYMBOL_GPL(usb_enable_ltm
);
2840 #ifdef CONFIG_USB_SUSPEND
2843 * usb_port_suspend - suspend a usb device's upstream port
2844 * @udev: device that's no longer in active use, not a root hub
2845 * Context: must be able to sleep; device not locked; pm locks held
2847 * Suspends a USB device that isn't in active use, conserving power.
2848 * Devices may wake out of a suspend, if anything important happens,
2849 * using the remote wakeup mechanism. They may also be taken out of
2850 * suspend by the host, using usb_port_resume(). It's also routine
2851 * to disconnect devices while they are suspended.
2853 * This only affects the USB hardware for a device; its interfaces
2854 * (and, for hubs, child devices) must already have been suspended.
2856 * Selective port suspend reduces power; most suspended devices draw
2857 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2858 * All devices below the suspended port are also suspended.
2860 * Devices leave suspend state when the host wakes them up. Some devices
2861 * also support "remote wakeup", where the device can activate the USB
2862 * tree above them to deliver data, such as a keypress or packet. In
2863 * some cases, this wakes the USB host.
2865 * Suspending OTG devices may trigger HNP, if that's been enabled
2866 * between a pair of dual-role devices. That will change roles, such
2867 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2869 * Devices on USB hub ports have only one "suspend" state, corresponding
2870 * to ACPI D2, "may cause the device to lose some context".
2871 * State transitions include:
2873 * - suspend, resume ... when the VBUS power link stays live
2874 * - suspend, disconnect ... VBUS lost
2876 * Once VBUS drop breaks the circuit, the port it's using has to go through
2877 * normal re-enumeration procedures, starting with enabling VBUS power.
2878 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2879 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2880 * timer, no SRP, no requests through sysfs.
2882 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2883 * the root hub for their bus goes into global suspend ... so we don't
2884 * (falsely) update the device power state to say it suspended.
2886 * Returns 0 on success, else negative errno.
2888 int usb_port_suspend(struct usb_device
*udev
, pm_message_t msg
)
2890 struct usb_hub
*hub
= hdev_to_hub(udev
->parent
);
2891 int port1
= udev
->portnum
;
2894 /* enable remote wakeup when appropriate; this lets the device
2895 * wake up the upstream hub (including maybe the root hub).
2897 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2898 * we don't explicitly enable it here.
2900 if (udev
->do_remote_wakeup
) {
2901 if (!hub_is_superspeed(hub
->hdev
)) {
2902 status
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
2903 USB_REQ_SET_FEATURE
, USB_RECIP_DEVICE
,
2904 USB_DEVICE_REMOTE_WAKEUP
, 0,
2906 USB_CTRL_SET_TIMEOUT
);
2908 /* Assume there's only one function on the USB 3.0
2909 * device and enable remote wake for the first
2910 * interface. FIXME if the interface association
2911 * descriptor shows there's more than one function.
2913 status
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
2914 USB_REQ_SET_FEATURE
,
2915 USB_RECIP_INTERFACE
,
2916 USB_INTRF_FUNC_SUSPEND
,
2917 USB_INTRF_FUNC_SUSPEND_RW
|
2918 USB_INTRF_FUNC_SUSPEND_LP
,
2920 USB_CTRL_SET_TIMEOUT
);
2923 dev_dbg(&udev
->dev
, "won't remote wakeup, status %d\n",
2925 /* bail if autosuspend is requested */
2926 if (PMSG_IS_AUTO(msg
))
2931 /* disable USB2 hardware LPM */
2932 if (udev
->usb2_hw_lpm_enabled
== 1)
2933 usb_set_usb2_hardware_lpm(udev
, 0);
2935 if (usb_disable_ltm(udev
)) {
2936 dev_err(&udev
->dev
, "%s Failed to disable LTM before suspend\n.",
2940 if (usb_unlocked_disable_lpm(udev
)) {
2941 dev_err(&udev
->dev
, "%s Failed to disable LPM before suspend\n.",
2947 if (hub_is_superspeed(hub
->hdev
))
2948 status
= set_port_feature(hub
->hdev
,
2949 port1
| (USB_SS_PORT_LS_U3
<< 3),
2950 USB_PORT_FEAT_LINK_STATE
);
2952 status
= set_port_feature(hub
->hdev
, port1
,
2953 USB_PORT_FEAT_SUSPEND
);
2955 dev_dbg(hub
->intfdev
, "can't suspend port %d, status %d\n",
2957 /* paranoia: "should not happen" */
2958 if (udev
->do_remote_wakeup
)
2959 (void) usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
2960 USB_REQ_CLEAR_FEATURE
, USB_RECIP_DEVICE
,
2961 USB_DEVICE_REMOTE_WAKEUP
, 0,
2963 USB_CTRL_SET_TIMEOUT
);
2965 /* Try to enable USB2 hardware LPM again */
2966 if (udev
->usb2_hw_lpm_capable
== 1)
2967 usb_set_usb2_hardware_lpm(udev
, 1);
2969 /* Try to enable USB3 LTM and LPM again */
2970 usb_enable_ltm(udev
);
2971 usb_unlocked_enable_lpm(udev
);
2973 /* System sleep transitions should never fail */
2974 if (!PMSG_IS_AUTO(msg
))
2977 /* device has up to 10 msec to fully suspend */
2978 dev_dbg(&udev
->dev
, "usb %ssuspend, wakeup %d\n",
2979 (PMSG_IS_AUTO(msg
) ? "auto-" : ""),
2980 udev
->do_remote_wakeup
);
2981 usb_set_device_state(udev
, USB_STATE_SUSPENDED
);
2982 udev
->port_is_suspended
= 1;
2985 usb_mark_last_busy(hub
->hdev
);
2990 * If the USB "suspend" state is in use (rather than "global suspend"),
2991 * many devices will be individually taken out of suspend state using
2992 * special "resume" signaling. This routine kicks in shortly after
2993 * hardware resume signaling is finished, either because of selective
2994 * resume (by host) or remote wakeup (by device) ... now see what changed
2995 * in the tree that's rooted at this device.
2997 * If @udev->reset_resume is set then the device is reset before the
2998 * status check is done.
3000 static int finish_port_resume(struct usb_device
*udev
)
3005 /* caller owns the udev device lock */
3006 dev_dbg(&udev
->dev
, "%s\n",
3007 udev
->reset_resume
? "finish reset-resume" : "finish resume");
3009 /* usb ch9 identifies four variants of SUSPENDED, based on what
3010 * state the device resumes to. Linux currently won't see the
3011 * first two on the host side; they'd be inside hub_port_init()
3012 * during many timeouts, but khubd can't suspend until later.
3014 usb_set_device_state(udev
, udev
->actconfig
3015 ? USB_STATE_CONFIGURED
3016 : USB_STATE_ADDRESS
);
3018 /* 10.5.4.5 says not to reset a suspended port if the attached
3019 * device is enabled for remote wakeup. Hence the reset
3020 * operation is carried out here, after the port has been
3023 if (udev
->reset_resume
)
3025 status
= usb_reset_and_verify_device(udev
);
3027 /* 10.5.4.5 says be sure devices in the tree are still there.
3028 * For now let's assume the device didn't go crazy on resume,
3029 * and device drivers will know about any resume quirks.
3033 status
= usb_get_status(udev
, USB_RECIP_DEVICE
, 0, &devstatus
);
3035 status
= (status
> 0 ? 0 : -ENODEV
);
3037 /* If a normal resume failed, try doing a reset-resume */
3038 if (status
&& !udev
->reset_resume
&& udev
->persist_enabled
) {
3039 dev_dbg(&udev
->dev
, "retry with reset-resume\n");
3040 udev
->reset_resume
= 1;
3041 goto retry_reset_resume
;
3046 dev_dbg(&udev
->dev
, "gone after usb resume? status %d\n",
3049 * There are a few quirky devices which violate the standard
3050 * by claiming to have remote wakeup enabled after a reset,
3051 * which crash if the feature is cleared, hence check for
3052 * udev->reset_resume
3054 } else if (udev
->actconfig
&& !udev
->reset_resume
) {
3055 le16_to_cpus(&devstatus
);
3056 if (devstatus
& (1 << USB_DEVICE_REMOTE_WAKEUP
)) {
3057 status
= usb_control_msg(udev
,
3058 usb_sndctrlpipe(udev
, 0),
3059 USB_REQ_CLEAR_FEATURE
,
3061 USB_DEVICE_REMOTE_WAKEUP
, 0,
3063 USB_CTRL_SET_TIMEOUT
);
3066 "disable remote wakeup, status %d\n",
3075 * usb_port_resume - re-activate a suspended usb device's upstream port
3076 * @udev: device to re-activate, not a root hub
3077 * Context: must be able to sleep; device not locked; pm locks held
3079 * This will re-activate the suspended device, increasing power usage
3080 * while letting drivers communicate again with its endpoints.
3081 * USB resume explicitly guarantees that the power session between
3082 * the host and the device is the same as it was when the device
3085 * If @udev->reset_resume is set then this routine won't check that the
3086 * port is still enabled. Furthermore, finish_port_resume() above will
3087 * reset @udev. The end result is that a broken power session can be
3088 * recovered and @udev will appear to persist across a loss of VBUS power.
3090 * For example, if a host controller doesn't maintain VBUS suspend current
3091 * during a system sleep or is reset when the system wakes up, all the USB
3092 * power sessions below it will be broken. This is especially troublesome
3093 * for mass-storage devices containing mounted filesystems, since the
3094 * device will appear to have disconnected and all the memory mappings
3095 * to it will be lost. Using the USB_PERSIST facility, the device can be
3096 * made to appear as if it had not disconnected.
3098 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
3099 * every effort to insure that the same device is present after the
3100 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3101 * quite possible for a device to remain unaltered but its media to be
3102 * changed. If the user replaces a flash memory card while the system is
3103 * asleep, he will have only himself to blame when the filesystem on the
3104 * new card is corrupted and the system crashes.
3106 * Returns 0 on success, else negative errno.
3108 int usb_port_resume(struct usb_device
*udev
, pm_message_t msg
)
3110 struct usb_hub
*hub
= hdev_to_hub(udev
->parent
);
3111 int port1
= udev
->portnum
;
3113 u16 portchange
, portstatus
;
3115 /* Skip the initial Clear-Suspend step for a remote wakeup */
3116 status
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
3117 if (status
== 0 && !port_is_suspended(hub
, portstatus
))
3118 goto SuspendCleared
;
3120 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
3122 set_bit(port1
, hub
->busy_bits
);
3124 /* see 7.1.7.7; affects power usage, but not budgeting */
3125 if (hub_is_superspeed(hub
->hdev
))
3126 status
= set_port_feature(hub
->hdev
,
3127 port1
| (USB_SS_PORT_LS_U0
<< 3),
3128 USB_PORT_FEAT_LINK_STATE
);
3130 status
= clear_port_feature(hub
->hdev
,
3131 port1
, USB_PORT_FEAT_SUSPEND
);
3133 dev_dbg(hub
->intfdev
, "can't resume port %d, status %d\n",
3136 /* drive resume for at least 20 msec */
3137 dev_dbg(&udev
->dev
, "usb %sresume\n",
3138 (PMSG_IS_AUTO(msg
) ? "auto-" : ""));
3141 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3142 * stop resume signaling. Then finish the resume
3145 status
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
3147 /* TRSMRCY = 10 msec */
3153 udev
->port_is_suspended
= 0;
3154 if (hub_is_superspeed(hub
->hdev
)) {
3155 if (portchange
& USB_PORT_STAT_C_LINK_STATE
)
3156 clear_port_feature(hub
->hdev
, port1
,
3157 USB_PORT_FEAT_C_PORT_LINK_STATE
);
3159 if (portchange
& USB_PORT_STAT_C_SUSPEND
)
3160 clear_port_feature(hub
->hdev
, port1
,
3161 USB_PORT_FEAT_C_SUSPEND
);
3165 clear_bit(port1
, hub
->busy_bits
);
3167 status
= check_port_resume_type(udev
,
3168 hub
, port1
, status
, portchange
, portstatus
);
3170 status
= finish_port_resume(udev
);
3172 dev_dbg(&udev
->dev
, "can't resume, status %d\n", status
);
3173 hub_port_logical_disconnect(hub
, port1
);
3175 /* Try to enable USB2 hardware LPM */
3176 if (udev
->usb2_hw_lpm_capable
== 1)
3177 usb_set_usb2_hardware_lpm(udev
, 1);
3179 /* Try to enable USB3 LTM and LPM */
3180 usb_enable_ltm(udev
);
3181 usb_unlocked_enable_lpm(udev
);
3187 /* caller has locked udev */
3188 int usb_remote_wakeup(struct usb_device
*udev
)
3192 if (udev
->state
== USB_STATE_SUSPENDED
) {
3193 dev_dbg(&udev
->dev
, "usb %sresume\n", "wakeup-");
3194 status
= usb_autoresume_device(udev
);
3196 /* Let the drivers do their thing, then... */
3197 usb_autosuspend_device(udev
);
3203 #else /* CONFIG_USB_SUSPEND */
3205 /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3207 int usb_port_suspend(struct usb_device
*udev
, pm_message_t msg
)
3212 /* However we may need to do a reset-resume */
3214 int usb_port_resume(struct usb_device
*udev
, pm_message_t msg
)
3216 struct usb_hub
*hub
= hdev_to_hub(udev
->parent
);
3217 int port1
= udev
->portnum
;
3219 u16 portchange
, portstatus
;
3221 status
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
3222 status
= check_port_resume_type(udev
,
3223 hub
, port1
, status
, portchange
, portstatus
);
3226 dev_dbg(&udev
->dev
, "can't resume, status %d\n", status
);
3227 hub_port_logical_disconnect(hub
, port1
);
3228 } else if (udev
->reset_resume
) {
3229 dev_dbg(&udev
->dev
, "reset-resume\n");
3230 status
= usb_reset_and_verify_device(udev
);
3237 static int check_ports_changed(struct usb_hub
*hub
)
3241 for (port1
= 1; port1
<= hub
->hdev
->maxchild
; ++port1
) {
3242 u16 portstatus
, portchange
;
3245 status
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
3246 if (!status
&& portchange
)
3252 static int hub_suspend(struct usb_interface
*intf
, pm_message_t msg
)
3254 struct usb_hub
*hub
= usb_get_intfdata (intf
);
3255 struct usb_device
*hdev
= hub
->hdev
;
3259 /* Warn if children aren't already suspended */
3260 for (port1
= 1; port1
<= hdev
->maxchild
; port1
++) {
3261 struct usb_device
*udev
;
3263 udev
= hub
->ports
[port1
- 1]->child
;
3264 if (udev
&& udev
->can_submit
) {
3265 dev_warn(&intf
->dev
, "port %d nyet suspended\n", port1
);
3266 if (PMSG_IS_AUTO(msg
))
3271 if (hdev
->do_remote_wakeup
&& hub
->quirk_check_port_auto_suspend
) {
3272 /* check if there are changes pending on hub ports */
3273 if (check_ports_changed(hub
)) {
3274 if (PMSG_IS_AUTO(msg
))
3276 pm_wakeup_event(&hdev
->dev
, 2000);
3280 if (hub_is_superspeed(hdev
) && hdev
->do_remote_wakeup
) {
3281 /* Enable hub to send remote wakeup for all ports. */
3282 for (port1
= 1; port1
<= hdev
->maxchild
; port1
++) {
3283 status
= set_port_feature(hdev
,
3285 USB_PORT_FEAT_REMOTE_WAKE_CONNECT
|
3286 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT
|
3287 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT
,
3288 USB_PORT_FEAT_REMOTE_WAKE_MASK
);
3292 dev_dbg(&intf
->dev
, "%s\n", __func__
);
3294 /* stop khubd and related activity */
3295 hub_quiesce(hub
, HUB_SUSPEND
);
3299 static int hub_resume(struct usb_interface
*intf
)
3301 struct usb_hub
*hub
= usb_get_intfdata(intf
);
3303 dev_dbg(&intf
->dev
, "%s\n", __func__
);
3304 hub_activate(hub
, HUB_RESUME
);
3308 static int hub_reset_resume(struct usb_interface
*intf
)
3310 struct usb_hub
*hub
= usb_get_intfdata(intf
);
3312 dev_dbg(&intf
->dev
, "%s\n", __func__
);
3313 hub_activate(hub
, HUB_RESET_RESUME
);
3318 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3319 * @rhdev: struct usb_device for the root hub
3321 * The USB host controller driver calls this function when its root hub
3322 * is resumed and Vbus power has been interrupted or the controller
3323 * has been reset. The routine marks @rhdev as having lost power.
3324 * When the hub driver is resumed it will take notice and carry out
3325 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3326 * the others will be disconnected.
3328 void usb_root_hub_lost_power(struct usb_device
*rhdev
)
3330 dev_warn(&rhdev
->dev
, "root hub lost power or was reset\n");
3331 rhdev
->reset_resume
= 1;
3333 EXPORT_SYMBOL_GPL(usb_root_hub_lost_power
);
3335 static const char * const usb3_lpm_names
[] = {
3343 * Send a Set SEL control transfer to the device, prior to enabling
3344 * device-initiated U1 or U2. This lets the device know the exit latencies from
3345 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3346 * packet from the host.
3348 * This function will fail if the SEL or PEL values for udev are greater than
3349 * the maximum allowed values for the link state to be enabled.
3351 static int usb_req_set_sel(struct usb_device
*udev
, enum usb3_link_state state
)
3353 struct usb_set_sel_req
*sel_values
;
3354 unsigned long long u1_sel
;
3355 unsigned long long u1_pel
;
3356 unsigned long long u2_sel
;
3357 unsigned long long u2_pel
;
3360 /* Convert SEL and PEL stored in ns to us */
3361 u1_sel
= DIV_ROUND_UP(udev
->u1_params
.sel
, 1000);
3362 u1_pel
= DIV_ROUND_UP(udev
->u1_params
.pel
, 1000);
3363 u2_sel
= DIV_ROUND_UP(udev
->u2_params
.sel
, 1000);
3364 u2_pel
= DIV_ROUND_UP(udev
->u2_params
.pel
, 1000);
3367 * Make sure that the calculated SEL and PEL values for the link
3368 * state we're enabling aren't bigger than the max SEL/PEL
3369 * value that will fit in the SET SEL control transfer.
3370 * Otherwise the device would get an incorrect idea of the exit
3371 * latency for the link state, and could start a device-initiated
3372 * U1/U2 when the exit latencies are too high.
3374 if ((state
== USB3_LPM_U1
&&
3375 (u1_sel
> USB3_LPM_MAX_U1_SEL_PEL
||
3376 u1_pel
> USB3_LPM_MAX_U1_SEL_PEL
)) ||
3377 (state
== USB3_LPM_U2
&&
3378 (u2_sel
> USB3_LPM_MAX_U2_SEL_PEL
||
3379 u2_pel
> USB3_LPM_MAX_U2_SEL_PEL
))) {
3380 dev_dbg(&udev
->dev
, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n",
3381 usb3_lpm_names
[state
], u1_sel
, u1_pel
);
3386 * If we're enabling device-initiated LPM for one link state,
3387 * but the other link state has a too high SEL or PEL value,
3388 * just set those values to the max in the Set SEL request.
3390 if (u1_sel
> USB3_LPM_MAX_U1_SEL_PEL
)
3391 u1_sel
= USB3_LPM_MAX_U1_SEL_PEL
;
3393 if (u1_pel
> USB3_LPM_MAX_U1_SEL_PEL
)
3394 u1_pel
= USB3_LPM_MAX_U1_SEL_PEL
;
3396 if (u2_sel
> USB3_LPM_MAX_U2_SEL_PEL
)
3397 u2_sel
= USB3_LPM_MAX_U2_SEL_PEL
;
3399 if (u2_pel
> USB3_LPM_MAX_U2_SEL_PEL
)
3400 u2_pel
= USB3_LPM_MAX_U2_SEL_PEL
;
3403 * usb_enable_lpm() can be called as part of a failed device reset,
3404 * which may be initiated by an error path of a mass storage driver.
3405 * Therefore, use GFP_NOIO.
3407 sel_values
= kmalloc(sizeof *(sel_values
), GFP_NOIO
);
3411 sel_values
->u1_sel
= u1_sel
;
3412 sel_values
->u1_pel
= u1_pel
;
3413 sel_values
->u2_sel
= cpu_to_le16(u2_sel
);
3414 sel_values
->u2_pel
= cpu_to_le16(u2_pel
);
3416 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
3420 sel_values
, sizeof *(sel_values
),
3421 USB_CTRL_SET_TIMEOUT
);
3427 * Enable or disable device-initiated U1 or U2 transitions.
3429 static int usb_set_device_initiated_lpm(struct usb_device
*udev
,
3430 enum usb3_link_state state
, bool enable
)
3437 feature
= USB_DEVICE_U1_ENABLE
;
3440 feature
= USB_DEVICE_U2_ENABLE
;
3443 dev_warn(&udev
->dev
, "%s: Can't %s non-U1 or U2 state.\n",
3444 __func__
, enable
? "enable" : "disable");
3448 if (udev
->state
!= USB_STATE_CONFIGURED
) {
3449 dev_dbg(&udev
->dev
, "%s: Can't %s %s state "
3450 "for unconfigured device.\n",
3451 __func__
, enable
? "enable" : "disable",
3452 usb3_lpm_names
[state
]);
3458 * Now send the control transfer to enable device-initiated LPM
3459 * for either U1 or U2.
3461 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
3462 USB_REQ_SET_FEATURE
,
3466 USB_CTRL_SET_TIMEOUT
);
3468 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
3469 USB_REQ_CLEAR_FEATURE
,
3473 USB_CTRL_SET_TIMEOUT
);
3476 dev_warn(&udev
->dev
, "%s of device-initiated %s failed.\n",
3477 enable
? "Enable" : "Disable",
3478 usb3_lpm_names
[state
]);
3484 static int usb_set_lpm_timeout(struct usb_device
*udev
,
3485 enum usb3_link_state state
, int timeout
)
3492 feature
= USB_PORT_FEAT_U1_TIMEOUT
;
3495 feature
= USB_PORT_FEAT_U2_TIMEOUT
;
3498 dev_warn(&udev
->dev
, "%s: Can't set timeout for non-U1 or U2 state.\n",
3503 if (state
== USB3_LPM_U1
&& timeout
> USB3_LPM_U1_MAX_TIMEOUT
&&
3504 timeout
!= USB3_LPM_DEVICE_INITIATED
) {
3505 dev_warn(&udev
->dev
, "Failed to set %s timeout to 0x%x, "
3506 "which is a reserved value.\n",
3507 usb3_lpm_names
[state
], timeout
);
3511 ret
= set_port_feature(udev
->parent
,
3512 USB_PORT_LPM_TIMEOUT(timeout
) | udev
->portnum
,
3515 dev_warn(&udev
->dev
, "Failed to set %s timeout to 0x%x,"
3516 "error code %i\n", usb3_lpm_names
[state
],
3520 if (state
== USB3_LPM_U1
)
3521 udev
->u1_params
.timeout
= timeout
;
3523 udev
->u2_params
.timeout
= timeout
;
3528 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3531 * We will attempt to enable U1 or U2, but there are no guarantees that the
3532 * control transfers to set the hub timeout or enable device-initiated U1/U2
3533 * will be successful.
3535 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3536 * driver know about it. If that call fails, it should be harmless, and just
3537 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3539 static void usb_enable_link_state(struct usb_hcd
*hcd
, struct usb_device
*udev
,
3540 enum usb3_link_state state
)
3543 __u8 u1_mel
= udev
->bos
->ss_cap
->bU1devExitLat
;
3544 __le16 u2_mel
= udev
->bos
->ss_cap
->bU2DevExitLat
;
3546 /* If the device says it doesn't have *any* exit latency to come out of
3547 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3550 if ((state
== USB3_LPM_U1
&& u1_mel
== 0) ||
3551 (state
== USB3_LPM_U2
&& u2_mel
== 0))
3555 * First, let the device know about the exit latencies
3556 * associated with the link state we're about to enable.
3558 ret
= usb_req_set_sel(udev
, state
);
3560 dev_warn(&udev
->dev
, "Set SEL for device-initiated %s failed.\n",
3561 usb3_lpm_names
[state
]);
3565 /* We allow the host controller to set the U1/U2 timeout internally
3566 * first, so that it can change its schedule to account for the
3567 * additional latency to send data to a device in a lower power
3570 timeout
= hcd
->driver
->enable_usb3_lpm_timeout(hcd
, udev
, state
);
3572 /* xHCI host controller doesn't want to enable this LPM state. */
3577 dev_warn(&udev
->dev
, "Could not enable %s link state, "
3578 "xHCI error %i.\n", usb3_lpm_names
[state
],
3583 if (usb_set_lpm_timeout(udev
, state
, timeout
))
3584 /* If we can't set the parent hub U1/U2 timeout,
3585 * device-initiated LPM won't be allowed either, so let the xHCI
3586 * host know that this link state won't be enabled.
3588 hcd
->driver
->disable_usb3_lpm_timeout(hcd
, udev
, state
);
3590 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3591 else if (udev
->actconfig
)
3592 usb_set_device_initiated_lpm(udev
, state
, true);
3597 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3600 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3601 * If zero is returned, the parent will not allow the link to go into U1/U2.
3603 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3604 * it won't have an effect on the bus link state because the parent hub will
3605 * still disallow device-initiated U1/U2 entry.
3607 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3608 * possible. The result will be slightly more bus bandwidth will be taken up
3609 * (to account for U1/U2 exit latency), but it should be harmless.
3611 static int usb_disable_link_state(struct usb_hcd
*hcd
, struct usb_device
*udev
,
3612 enum usb3_link_state state
)
3618 feature
= USB_PORT_FEAT_U1_TIMEOUT
;
3621 feature
= USB_PORT_FEAT_U2_TIMEOUT
;
3624 dev_warn(&udev
->dev
, "%s: Can't disable non-U1 or U2 state.\n",
3629 if (usb_set_lpm_timeout(udev
, state
, 0))
3632 usb_set_device_initiated_lpm(udev
, state
, false);
3634 if (hcd
->driver
->disable_usb3_lpm_timeout(hcd
, udev
, state
))
3635 dev_warn(&udev
->dev
, "Could not disable xHCI %s timeout, "
3636 "bus schedule bandwidth may be impacted.\n",
3637 usb3_lpm_names
[state
]);
3642 * Disable hub-initiated and device-initiated U1 and U2 entry.
3643 * Caller must own the bandwidth_mutex.
3645 * This will call usb_enable_lpm() on failure, which will decrement
3646 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3648 int usb_disable_lpm(struct usb_device
*udev
)
3650 struct usb_hcd
*hcd
;
3652 if (!udev
|| !udev
->parent
||
3653 udev
->speed
!= USB_SPEED_SUPER
||
3657 hcd
= bus_to_hcd(udev
->bus
);
3658 if (!hcd
|| !hcd
->driver
->disable_usb3_lpm_timeout
)
3661 udev
->lpm_disable_count
++;
3662 if ((udev
->u1_params
.timeout
== 0 && udev
->u2_params
.timeout
== 0))
3665 /* If LPM is enabled, attempt to disable it. */
3666 if (usb_disable_link_state(hcd
, udev
, USB3_LPM_U1
))
3668 if (usb_disable_link_state(hcd
, udev
, USB3_LPM_U2
))
3674 usb_enable_lpm(udev
);
3677 EXPORT_SYMBOL_GPL(usb_disable_lpm
);
3679 /* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3680 int usb_unlocked_disable_lpm(struct usb_device
*udev
)
3682 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
3688 mutex_lock(hcd
->bandwidth_mutex
);
3689 ret
= usb_disable_lpm(udev
);
3690 mutex_unlock(hcd
->bandwidth_mutex
);
3694 EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm
);
3697 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3698 * xHCI host policy may prevent U1 or U2 from being enabled.
3700 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3701 * until the lpm_disable_count drops to zero. Caller must own the
3704 void usb_enable_lpm(struct usb_device
*udev
)
3706 struct usb_hcd
*hcd
;
3708 if (!udev
|| !udev
->parent
||
3709 udev
->speed
!= USB_SPEED_SUPER
||
3713 udev
->lpm_disable_count
--;
3714 hcd
= bus_to_hcd(udev
->bus
);
3715 /* Double check that we can both enable and disable LPM.
3716 * Device must be configured to accept set feature U1/U2 timeout.
3718 if (!hcd
|| !hcd
->driver
->enable_usb3_lpm_timeout
||
3719 !hcd
->driver
->disable_usb3_lpm_timeout
)
3722 if (udev
->lpm_disable_count
> 0)
3725 usb_enable_link_state(hcd
, udev
, USB3_LPM_U1
);
3726 usb_enable_link_state(hcd
, udev
, USB3_LPM_U2
);
3728 EXPORT_SYMBOL_GPL(usb_enable_lpm
);
3730 /* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3731 void usb_unlocked_enable_lpm(struct usb_device
*udev
)
3733 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
3738 mutex_lock(hcd
->bandwidth_mutex
);
3739 usb_enable_lpm(udev
);
3740 mutex_unlock(hcd
->bandwidth_mutex
);
3742 EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm
);
3745 #else /* CONFIG_PM */
3747 #define hub_suspend NULL
3748 #define hub_resume NULL
3749 #define hub_reset_resume NULL
3751 int usb_disable_lpm(struct usb_device
*udev
)
3755 EXPORT_SYMBOL_GPL(usb_disable_lpm
);
3757 void usb_enable_lpm(struct usb_device
*udev
) { }
3758 EXPORT_SYMBOL_GPL(usb_enable_lpm
);
3760 int usb_unlocked_disable_lpm(struct usb_device
*udev
)
3764 EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm
);
3766 void usb_unlocked_enable_lpm(struct usb_device
*udev
) { }
3767 EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm
);
3769 int usb_disable_ltm(struct usb_device
*udev
)
3773 EXPORT_SYMBOL_GPL(usb_disable_ltm
);
3775 void usb_enable_ltm(struct usb_device
*udev
) { }
3776 EXPORT_SYMBOL_GPL(usb_enable_ltm
);
3780 /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3782 * Between connect detection and reset signaling there must be a delay
3783 * of 100ms at least for debounce and power-settling. The corresponding
3784 * timer shall restart whenever the downstream port detects a disconnect.
3786 * Apparently there are some bluetooth and irda-dongles and a number of
3787 * low-speed devices for which this debounce period may last over a second.
3788 * Not covered by the spec - but easy to deal with.
3790 * This implementation uses a 1500ms total debounce timeout; if the
3791 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3792 * every 25ms for transient disconnects. When the port status has been
3793 * unchanged for 100ms it returns the port status.
3795 static int hub_port_debounce(struct usb_hub
*hub
, int port1
)
3798 int total_time
, stable_time
= 0;
3799 u16 portchange
, portstatus
;
3800 unsigned connection
= 0xffff;
3802 for (total_time
= 0; ; total_time
+= HUB_DEBOUNCE_STEP
) {
3803 ret
= hub_port_status(hub
, port1
, &portstatus
, &portchange
);
3807 if (!(portchange
& USB_PORT_STAT_C_CONNECTION
) &&
3808 (portstatus
& USB_PORT_STAT_CONNECTION
) == connection
) {
3809 stable_time
+= HUB_DEBOUNCE_STEP
;
3810 if (stable_time
>= HUB_DEBOUNCE_STABLE
)
3814 connection
= portstatus
& USB_PORT_STAT_CONNECTION
;
3817 if (portchange
& USB_PORT_STAT_C_CONNECTION
) {
3818 clear_port_feature(hub
->hdev
, port1
,
3819 USB_PORT_FEAT_C_CONNECTION
);
3822 if (total_time
>= HUB_DEBOUNCE_TIMEOUT
)
3824 msleep(HUB_DEBOUNCE_STEP
);
3827 dev_dbg (hub
->intfdev
,
3828 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3829 port1
, total_time
, stable_time
, portstatus
);
3831 if (stable_time
< HUB_DEBOUNCE_STABLE
)
3836 void usb_ep0_reinit(struct usb_device
*udev
)
3838 usb_disable_endpoint(udev
, 0 + USB_DIR_IN
, true);
3839 usb_disable_endpoint(udev
, 0 + USB_DIR_OUT
, true);
3840 usb_enable_endpoint(udev
, &udev
->ep0
, true);
3842 EXPORT_SYMBOL_GPL(usb_ep0_reinit
);
3844 #define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3845 #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3847 static int hub_set_address(struct usb_device
*udev
, int devnum
)
3850 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
3853 * The host controller will choose the device address,
3854 * instead of the core having chosen it earlier
3856 if (!hcd
->driver
->address_device
&& devnum
<= 1)
3858 if (udev
->state
== USB_STATE_ADDRESS
)
3860 if (udev
->state
!= USB_STATE_DEFAULT
)
3862 if (hcd
->driver
->address_device
)
3863 retval
= hcd
->driver
->address_device(hcd
, udev
);
3865 retval
= usb_control_msg(udev
, usb_sndaddr0pipe(),
3866 USB_REQ_SET_ADDRESS
, 0, devnum
, 0,
3867 NULL
, 0, USB_CTRL_SET_TIMEOUT
);
3869 update_devnum(udev
, devnum
);
3870 /* Device now using proper address. */
3871 usb_set_device_state(udev
, USB_STATE_ADDRESS
);
3872 usb_ep0_reinit(udev
);
3877 /* Reset device, (re)assign address, get device descriptor.
3878 * Device connection must be stable, no more debouncing needed.
3879 * Returns device in USB_STATE_ADDRESS, except on error.
3881 * If this is called for an already-existing device (as part of
3882 * usb_reset_and_verify_device), the caller must own the device lock. For a
3883 * newly detected device that is not accessible through any global
3884 * pointers, it's not necessary to lock the device.
3887 hub_port_init (struct usb_hub
*hub
, struct usb_device
*udev
, int port1
,
3890 static DEFINE_MUTEX(usb_address0_mutex
);
3892 struct usb_device
*hdev
= hub
->hdev
;
3893 struct usb_hcd
*hcd
= bus_to_hcd(hdev
->bus
);
3895 unsigned delay
= HUB_SHORT_RESET_TIME
;
3896 enum usb_device_speed oldspeed
= udev
->speed
;
3898 int devnum
= udev
->devnum
;
3900 /* root hub ports have a slightly longer reset period
3901 * (from USB 2.0 spec, section 7.1.7.5)
3903 if (!hdev
->parent
) {
3904 delay
= HUB_ROOT_RESET_TIME
;
3905 if (port1
== hdev
->bus
->otg_port
)
3906 hdev
->bus
->b_hnp_enable
= 0;
3909 /* Some low speed devices have problems with the quick delay, so */
3910 /* be a bit pessimistic with those devices. RHbug #23670 */
3911 if (oldspeed
== USB_SPEED_LOW
)
3912 delay
= HUB_LONG_RESET_TIME
;
3914 mutex_lock(&usb_address0_mutex
);
3916 /* Reset the device; full speed may morph to high speed */
3917 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
3918 retval
= hub_port_reset(hub
, port1
, udev
, delay
, false);
3919 if (retval
< 0) /* error or disconnect */
3921 /* success, speed is known */
3925 if (oldspeed
!= USB_SPEED_UNKNOWN
&& oldspeed
!= udev
->speed
) {
3926 dev_dbg(&udev
->dev
, "device reset changed speed!\n");
3929 oldspeed
= udev
->speed
;
3931 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3932 * it's fixed size except for full speed devices.
3933 * For Wireless USB devices, ep0 max packet is always 512 (tho
3934 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
3936 switch (udev
->speed
) {
3937 case USB_SPEED_SUPER
:
3938 case USB_SPEED_WIRELESS
: /* fixed at 512 */
3939 udev
->ep0
.desc
.wMaxPacketSize
= cpu_to_le16(512);
3941 case USB_SPEED_HIGH
: /* fixed at 64 */
3942 udev
->ep0
.desc
.wMaxPacketSize
= cpu_to_le16(64);
3944 case USB_SPEED_FULL
: /* 8, 16, 32, or 64 */
3945 /* to determine the ep0 maxpacket size, try to read
3946 * the device descriptor to get bMaxPacketSize0 and
3947 * then correct our initial guess.
3949 udev
->ep0
.desc
.wMaxPacketSize
= cpu_to_le16(64);
3951 case USB_SPEED_LOW
: /* fixed at 8 */
3952 udev
->ep0
.desc
.wMaxPacketSize
= cpu_to_le16(8);
3958 if (udev
->speed
== USB_SPEED_WIRELESS
)
3959 speed
= "variable speed Wireless";
3961 speed
= usb_speed_string(udev
->speed
);
3963 if (udev
->speed
!= USB_SPEED_SUPER
)
3964 dev_info(&udev
->dev
,
3965 "%s %s USB device number %d using %s\n",
3966 (udev
->config
) ? "reset" : "new", speed
,
3967 devnum
, udev
->bus
->controller
->driver
->name
);
3969 /* Set up TT records, if needed */
3971 udev
->tt
= hdev
->tt
;
3972 udev
->ttport
= hdev
->ttport
;
3973 } else if (udev
->speed
!= USB_SPEED_HIGH
3974 && hdev
->speed
== USB_SPEED_HIGH
) {
3976 dev_err(&udev
->dev
, "parent hub has no TT\n");
3980 udev
->tt
= &hub
->tt
;
3981 udev
->ttport
= port1
;
3984 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3985 * Because device hardware and firmware is sometimes buggy in
3986 * this area, and this is how Linux has done it for ages.
3987 * Change it cautiously.
3989 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3990 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3991 * so it may help with some non-standards-compliant devices.
3992 * Otherwise we start with SET_ADDRESS and then try to read the
3993 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3996 for (i
= 0; i
< GET_DESCRIPTOR_TRIES
; (++i
, msleep(100))) {
3997 if (USE_NEW_SCHEME(retry_counter
) && !(hcd
->driver
->flags
& HCD_USB3
)) {
3998 struct usb_device_descriptor
*buf
;
4001 #define GET_DESCRIPTOR_BUFSIZE 64
4002 buf
= kmalloc(GET_DESCRIPTOR_BUFSIZE
, GFP_NOIO
);
4008 /* Retry on all errors; some devices are flakey.
4009 * 255 is for WUSB devices, we actually need to use
4010 * 512 (WUSB1.0[4.8.1]).
4012 for (j
= 0; j
< 3; ++j
) {
4013 buf
->bMaxPacketSize0
= 0;
4014 r
= usb_control_msg(udev
, usb_rcvaddr0pipe(),
4015 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
,
4016 USB_DT_DEVICE
<< 8, 0,
4017 buf
, GET_DESCRIPTOR_BUFSIZE
,
4018 initial_descriptor_timeout
);
4019 switch (buf
->bMaxPacketSize0
) {
4020 case 8: case 16: case 32: case 64: case 255:
4021 if (buf
->bDescriptorType
==
4035 udev
->descriptor
.bMaxPacketSize0
=
4036 buf
->bMaxPacketSize0
;
4039 retval
= hub_port_reset(hub
, port1
, udev
, delay
, false);
4040 if (retval
< 0) /* error or disconnect */
4042 if (oldspeed
!= udev
->speed
) {
4044 "device reset changed speed!\n");
4050 "device descriptor read/64, error %d\n",
4055 #undef GET_DESCRIPTOR_BUFSIZE
4059 * If device is WUSB, we already assigned an
4060 * unauthorized address in the Connect Ack sequence;
4061 * authorization will assign the final address.
4063 if (udev
->wusb
== 0) {
4064 for (j
= 0; j
< SET_ADDRESS_TRIES
; ++j
) {
4065 retval
= hub_set_address(udev
, devnum
);
4072 "device not accepting address %d, error %d\n",
4076 if (udev
->speed
== USB_SPEED_SUPER
) {
4077 devnum
= udev
->devnum
;
4078 dev_info(&udev
->dev
,
4079 "%s SuperSpeed USB device number %d using %s\n",
4080 (udev
->config
) ? "reset" : "new",
4081 devnum
, udev
->bus
->controller
->driver
->name
);
4084 /* cope with hardware quirkiness:
4085 * - let SET_ADDRESS settle, some device hardware wants it
4086 * - read ep0 maxpacket even for high and low speed,
4089 if (USE_NEW_SCHEME(retry_counter
) && !(hcd
->driver
->flags
& HCD_USB3
))
4093 retval
= usb_get_device_descriptor(udev
, 8);
4096 "device descriptor read/8, error %d\n",
4108 if (hcd
->phy
&& !hdev
->parent
)
4109 usb_phy_notify_connect(hcd
->phy
, udev
->speed
);
4112 * Some superspeed devices have finished the link training process
4113 * and attached to a superspeed hub port, but the device descriptor
4114 * got from those devices show they aren't superspeed devices. Warm
4115 * reset the port attached by the devices can fix them.
4117 if ((udev
->speed
== USB_SPEED_SUPER
) &&
4118 (le16_to_cpu(udev
->descriptor
.bcdUSB
) < 0x0300)) {
4119 dev_err(&udev
->dev
, "got a wrong device descriptor, "
4120 "warm reset device\n");
4121 hub_port_reset(hub
, port1
, udev
,
4122 HUB_BH_RESET_TIME
, true);
4127 if (udev
->descriptor
.bMaxPacketSize0
== 0xff ||
4128 udev
->speed
== USB_SPEED_SUPER
)
4131 i
= udev
->descriptor
.bMaxPacketSize0
;
4132 if (usb_endpoint_maxp(&udev
->ep0
.desc
) != i
) {
4133 if (udev
->speed
== USB_SPEED_LOW
||
4134 !(i
== 8 || i
== 16 || i
== 32 || i
== 64)) {
4135 dev_err(&udev
->dev
, "Invalid ep0 maxpacket: %d\n", i
);
4139 if (udev
->speed
== USB_SPEED_FULL
)
4140 dev_dbg(&udev
->dev
, "ep0 maxpacket = %d\n", i
);
4142 dev_warn(&udev
->dev
, "Using ep0 maxpacket: %d\n", i
);
4143 udev
->ep0
.desc
.wMaxPacketSize
= cpu_to_le16(i
);
4144 usb_ep0_reinit(udev
);
4147 retval
= usb_get_device_descriptor(udev
, USB_DT_DEVICE_SIZE
);
4148 if (retval
< (signed)sizeof(udev
->descriptor
)) {
4149 dev_err(&udev
->dev
, "device descriptor read/all, error %d\n",
4156 if (udev
->wusb
== 0 && le16_to_cpu(udev
->descriptor
.bcdUSB
) >= 0x0201) {
4157 retval
= usb_get_bos_descriptor(udev
);
4159 udev
->lpm_capable
= usb_device_supports_lpm(udev
);
4160 usb_set_lpm_parameters(udev
);
4165 /* notify HCD that we have a device connected and addressed */
4166 if (hcd
->driver
->update_device
)
4167 hcd
->driver
->update_device(hcd
, udev
);
4170 hub_port_disable(hub
, port1
, 0);
4171 update_devnum(udev
, devnum
); /* for disconnect processing */
4173 mutex_unlock(&usb_address0_mutex
);
4178 check_highspeed (struct usb_hub
*hub
, struct usb_device
*udev
, int port1
)
4180 struct usb_qualifier_descriptor
*qual
;
4183 qual
= kmalloc (sizeof *qual
, GFP_KERNEL
);
4187 status
= usb_get_descriptor (udev
, USB_DT_DEVICE_QUALIFIER
, 0,
4188 qual
, sizeof *qual
);
4189 if (status
== sizeof *qual
) {
4190 dev_info(&udev
->dev
, "not running at top speed; "
4191 "connect to a high speed hub\n");
4192 /* hub LEDs are probably harder to miss than syslog */
4193 if (hub
->has_indicators
) {
4194 hub
->indicator
[port1
-1] = INDICATOR_GREEN_BLINK
;
4195 schedule_delayed_work (&hub
->leds
, 0);
4202 hub_power_remaining (struct usb_hub
*hub
)
4204 struct usb_device
*hdev
= hub
->hdev
;
4208 if (!hub
->limited_power
)
4211 remaining
= hdev
->bus_mA
- hub
->descriptor
->bHubContrCurrent
;
4212 for (port1
= 1; port1
<= hdev
->maxchild
; ++port1
) {
4213 struct usb_device
*udev
= hub
->ports
[port1
- 1]->child
;
4219 /* Unconfigured devices may not use more than 100mA,
4220 * or 8mA for OTG ports */
4221 if (udev
->actconfig
)
4222 delta
= udev
->actconfig
->desc
.bMaxPower
* 2;
4223 else if (port1
!= udev
->bus
->otg_port
|| hdev
->parent
)
4227 if (delta
> hub
->mA_per_port
)
4228 dev_warn(&udev
->dev
,
4229 "%dmA is over %umA budget for port %d!\n",
4230 delta
, hub
->mA_per_port
, port1
);
4233 if (remaining
< 0) {
4234 dev_warn(hub
->intfdev
, "%dmA over power budget!\n",
4241 /* Handle physical or logical connection change events.
4242 * This routine is called when:
4243 * a port connection-change occurs;
4244 * a port enable-change occurs (often caused by EMI);
4245 * usb_reset_and_verify_device() encounters changed descriptors (as from
4246 * a firmware download)
4247 * caller already locked the hub
4249 static void hub_port_connect_change(struct usb_hub
*hub
, int port1
,
4250 u16 portstatus
, u16 portchange
)
4252 struct usb_device
*hdev
= hub
->hdev
;
4253 struct device
*hub_dev
= hub
->intfdev
;
4254 struct usb_hcd
*hcd
= bus_to_hcd(hdev
->bus
);
4255 unsigned wHubCharacteristics
=
4256 le16_to_cpu(hub
->descriptor
->wHubCharacteristics
);
4257 struct usb_device
*udev
;
4261 "port %d, status %04x, change %04x, %s\n",
4262 port1
, portstatus
, portchange
, portspeed(hub
, portstatus
));
4264 if (hub
->has_indicators
) {
4265 set_port_led(hub
, port1
, HUB_LED_AUTO
);
4266 hub
->indicator
[port1
-1] = INDICATOR_AUTO
;
4269 #ifdef CONFIG_USB_OTG
4270 /* during HNP, don't repeat the debounce */
4271 if (hdev
->bus
->is_b_host
)
4272 portchange
&= ~(USB_PORT_STAT_C_CONNECTION
|
4273 USB_PORT_STAT_C_ENABLE
);
4276 /* Try to resuscitate an existing device */
4277 udev
= hub
->ports
[port1
- 1]->child
;
4278 if ((portstatus
& USB_PORT_STAT_CONNECTION
) && udev
&&
4279 udev
->state
!= USB_STATE_NOTATTACHED
) {
4280 usb_lock_device(udev
);
4281 if (portstatus
& USB_PORT_STAT_ENABLE
) {
4282 status
= 0; /* Nothing to do */
4284 #ifdef CONFIG_USB_SUSPEND
4285 } else if (udev
->state
== USB_STATE_SUSPENDED
&&
4286 udev
->persist_enabled
) {
4287 /* For a suspended device, treat this as a
4288 * remote wakeup event.
4290 status
= usb_remote_wakeup(udev
);
4294 status
= -ENODEV
; /* Don't resuscitate */
4296 usb_unlock_device(udev
);
4299 clear_bit(port1
, hub
->change_bits
);
4304 /* Disconnect any existing devices under this port */
4306 if (hcd
->phy
&& !hdev
->parent
&&
4307 !(portstatus
& USB_PORT_STAT_CONNECTION
))
4308 usb_phy_notify_disconnect(hcd
->phy
, udev
->speed
);
4309 usb_disconnect(&hub
->ports
[port1
- 1]->child
);
4311 clear_bit(port1
, hub
->change_bits
);
4313 /* We can forget about a "removed" device when there's a physical
4314 * disconnect or the connect status changes.
4316 if (!(portstatus
& USB_PORT_STAT_CONNECTION
) ||
4317 (portchange
& USB_PORT_STAT_C_CONNECTION
))
4318 clear_bit(port1
, hub
->removed_bits
);
4320 if (portchange
& (USB_PORT_STAT_C_CONNECTION
|
4321 USB_PORT_STAT_C_ENABLE
)) {
4322 status
= hub_port_debounce(hub
, port1
);
4324 if (printk_ratelimit())
4325 dev_err(hub_dev
, "connect-debounce failed, "
4326 "port %d disabled\n", port1
);
4327 portstatus
&= ~USB_PORT_STAT_CONNECTION
;
4329 portstatus
= status
;
4333 /* Return now if debouncing failed or nothing is connected or
4334 * the device was "removed".
4336 if (!(portstatus
& USB_PORT_STAT_CONNECTION
) ||
4337 test_bit(port1
, hub
->removed_bits
)) {
4339 /* maybe switch power back on (e.g. root hub was reset) */
4340 if ((wHubCharacteristics
& HUB_CHAR_LPSM
) < 2
4341 && !port_is_power_on(hub
, portstatus
))
4342 set_port_feature(hdev
, port1
, USB_PORT_FEAT_POWER
);
4344 if (portstatus
& USB_PORT_STAT_ENABLE
)
4349 for (i
= 0; i
< SET_CONFIG_TRIES
; i
++) {
4351 /* reallocate for each attempt, since references
4352 * to the previous one can escape in various ways
4354 udev
= usb_alloc_dev(hdev
, hdev
->bus
, port1
);
4357 "couldn't allocate port %d usb_device\n",
4362 usb_set_device_state(udev
, USB_STATE_POWERED
);
4363 udev
->bus_mA
= hub
->mA_per_port
;
4364 udev
->level
= hdev
->level
+ 1;
4365 udev
->wusb
= hub_is_wusb(hub
);
4367 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4368 if (hub_is_superspeed(hub
->hdev
))
4369 udev
->speed
= USB_SPEED_SUPER
;
4371 udev
->speed
= USB_SPEED_UNKNOWN
;
4373 choose_devnum(udev
);
4374 if (udev
->devnum
<= 0) {
4375 status
= -ENOTCONN
; /* Don't retry */
4379 /* reset (non-USB 3.0 devices) and get descriptor */
4380 status
= hub_port_init(hub
, udev
, port1
, i
);
4384 usb_detect_quirks(udev
);
4385 if (udev
->quirks
& USB_QUIRK_DELAY_INIT
)
4388 /* consecutive bus-powered hubs aren't reliable; they can
4389 * violate the voltage drop budget. if the new child has
4390 * a "powered" LED, users should notice we didn't enable it
4391 * (without reading syslog), even without per-port LEDs
4394 if (udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
4395 && udev
->bus_mA
<= 100) {
4398 status
= usb_get_status(udev
, USB_RECIP_DEVICE
, 0,
4401 dev_dbg(&udev
->dev
, "get status %d ?\n", status
);
4404 le16_to_cpus(&devstat
);
4405 if ((devstat
& (1 << USB_DEVICE_SELF_POWERED
)) == 0) {
4407 "can't connect bus-powered hub "
4409 if (hub
->has_indicators
) {
4410 hub
->indicator
[port1
-1] =
4411 INDICATOR_AMBER_BLINK
;
4412 schedule_delayed_work (&hub
->leds
, 0);
4414 status
= -ENOTCONN
; /* Don't retry */
4419 /* check for devices running slower than they could */
4420 if (le16_to_cpu(udev
->descriptor
.bcdUSB
) >= 0x0200
4421 && udev
->speed
== USB_SPEED_FULL
4422 && highspeed_hubs
!= 0)
4423 check_highspeed (hub
, udev
, port1
);
4425 /* Store the parent's children[] pointer. At this point
4426 * udev becomes globally accessible, although presumably
4427 * no one will look at it until hdev is unlocked.
4431 /* We mustn't add new devices if the parent hub has
4432 * been disconnected; we would race with the
4433 * recursively_mark_NOTATTACHED() routine.
4435 spin_lock_irq(&device_state_lock
);
4436 if (hdev
->state
== USB_STATE_NOTATTACHED
)
4439 hub
->ports
[port1
- 1]->child
= udev
;
4440 spin_unlock_irq(&device_state_lock
);
4442 /* Run it through the hoops (find a driver, etc) */
4444 status
= usb_new_device(udev
);
4446 spin_lock_irq(&device_state_lock
);
4447 hub
->ports
[port1
- 1]->child
= NULL
;
4448 spin_unlock_irq(&device_state_lock
);
4455 status
= hub_power_remaining(hub
);
4457 dev_dbg(hub_dev
, "%dmA power budget left\n", status
);
4462 hub_port_disable(hub
, port1
, 1);
4464 usb_ep0_reinit(udev
);
4465 release_devnum(udev
);
4468 if ((status
== -ENOTCONN
) || (status
== -ENOTSUPP
))
4471 if (hub
->hdev
->parent
||
4472 !hcd
->driver
->port_handed_over
||
4473 !(hcd
->driver
->port_handed_over
)(hcd
, port1
))
4474 dev_err(hub_dev
, "unable to enumerate USB device on port %d\n",
4478 hub_port_disable(hub
, port1
, 1);
4479 if (hcd
->driver
->relinquish_port
&& !hub
->hdev
->parent
)
4480 hcd
->driver
->relinquish_port(hcd
, port1
);
4483 /* Returns 1 if there was a remote wakeup and a connect status change. */
4484 static int hub_handle_remote_wakeup(struct usb_hub
*hub
, unsigned int port
,
4485 u16 portstatus
, u16 portchange
)
4487 struct usb_device
*hdev
;
4488 struct usb_device
*udev
;
4489 int connect_change
= 0;
4493 udev
= hub
->ports
[port
- 1]->child
;
4494 if (!hub_is_superspeed(hdev
)) {
4495 if (!(portchange
& USB_PORT_STAT_C_SUSPEND
))
4497 clear_port_feature(hdev
, port
, USB_PORT_FEAT_C_SUSPEND
);
4499 if (!udev
|| udev
->state
!= USB_STATE_SUSPENDED
||
4500 (portstatus
& USB_PORT_STAT_LINK_STATE
) !=
4506 /* TRSMRCY = 10 msec */
4509 usb_lock_device(udev
);
4510 ret
= usb_remote_wakeup(udev
);
4511 usb_unlock_device(udev
);
4516 hub_port_disable(hub
, port
, 1);
4518 dev_dbg(hub
->intfdev
, "resume on port %d, status %d\n",
4520 return connect_change
;
4523 static void hub_events(void)
4525 struct list_head
*tmp
;
4526 struct usb_device
*hdev
;
4527 struct usb_interface
*intf
;
4528 struct usb_hub
*hub
;
4529 struct device
*hub_dev
;
4535 int connect_change
, wakeup_change
;
4538 * We restart the list every time to avoid a deadlock with
4539 * deleting hubs downstream from this one. This should be
4540 * safe since we delete the hub from the event list.
4541 * Not the most efficient, but avoids deadlocks.
4545 /* Grab the first entry at the beginning of the list */
4546 spin_lock_irq(&hub_event_lock
);
4547 if (list_empty(&hub_event_list
)) {
4548 spin_unlock_irq(&hub_event_lock
);
4552 tmp
= hub_event_list
.next
;
4555 hub
= list_entry(tmp
, struct usb_hub
, event_list
);
4556 kref_get(&hub
->kref
);
4557 spin_unlock_irq(&hub_event_lock
);
4560 hub_dev
= hub
->intfdev
;
4561 intf
= to_usb_interface(hub_dev
);
4562 dev_dbg(hub_dev
, "state %d ports %d chg %04x evt %04x\n",
4563 hdev
->state
, hub
->descriptor
4564 ? hub
->descriptor
->bNbrPorts
4566 /* NOTE: expects max 15 ports... */
4567 (u16
) hub
->change_bits
[0],
4568 (u16
) hub
->event_bits
[0]);
4570 /* Lock the device, then check to see if we were
4571 * disconnected while waiting for the lock to succeed. */
4572 usb_lock_device(hdev
);
4573 if (unlikely(hub
->disconnected
))
4574 goto loop_disconnected
;
4576 /* If the hub has died, clean up after it */
4577 if (hdev
->state
== USB_STATE_NOTATTACHED
) {
4578 hub
->error
= -ENODEV
;
4579 hub_quiesce(hub
, HUB_DISCONNECT
);
4584 ret
= usb_autopm_get_interface(intf
);
4586 dev_dbg(hub_dev
, "Can't autoresume: %d\n", ret
);
4590 /* If this is an inactive hub, do nothing */
4595 dev_dbg (hub_dev
, "resetting for error %d\n",
4598 ret
= usb_reset_device(hdev
);
4601 "error resetting hub: %d\n", ret
);
4609 /* deal with port status changes */
4610 for (i
= 1; i
<= hub
->descriptor
->bNbrPorts
; i
++) {
4611 if (test_bit(i
, hub
->busy_bits
))
4613 connect_change
= test_bit(i
, hub
->change_bits
);
4614 wakeup_change
= test_and_clear_bit(i
, hub
->wakeup_bits
);
4615 if (!test_and_clear_bit(i
, hub
->event_bits
) &&
4616 !connect_change
&& !wakeup_change
)
4619 ret
= hub_port_status(hub
, i
,
4620 &portstatus
, &portchange
);
4624 if (portchange
& USB_PORT_STAT_C_CONNECTION
) {
4625 clear_port_feature(hdev
, i
,
4626 USB_PORT_FEAT_C_CONNECTION
);
4630 if (portchange
& USB_PORT_STAT_C_ENABLE
) {
4631 if (!connect_change
)
4633 "port %d enable change, "
4636 clear_port_feature(hdev
, i
,
4637 USB_PORT_FEAT_C_ENABLE
);
4640 * EM interference sometimes causes badly
4641 * shielded USB devices to be shutdown by
4642 * the hub, this hack enables them again.
4643 * Works at least with mouse driver.
4645 if (!(portstatus
& USB_PORT_STAT_ENABLE
)
4647 && hub
->ports
[i
- 1]->child
) {
4650 "disabled by hub (EMI?), "
4657 if (hub_handle_remote_wakeup(hub
, i
,
4658 portstatus
, portchange
))
4661 if (portchange
& USB_PORT_STAT_C_OVERCURRENT
) {
4665 dev_dbg(hub_dev
, "over-current change on port "
4667 clear_port_feature(hdev
, i
,
4668 USB_PORT_FEAT_C_OVER_CURRENT
);
4669 msleep(100); /* Cool down */
4670 hub_power_on(hub
, true);
4671 hub_port_status(hub
, i
, &status
, &unused
);
4672 if (status
& USB_PORT_STAT_OVERCURRENT
)
4673 dev_err(hub_dev
, "over-current "
4674 "condition on port %d\n", i
);
4677 if (portchange
& USB_PORT_STAT_C_RESET
) {
4679 "reset change on port %d\n",
4681 clear_port_feature(hdev
, i
,
4682 USB_PORT_FEAT_C_RESET
);
4684 if ((portchange
& USB_PORT_STAT_C_BH_RESET
) &&
4685 hub_is_superspeed(hub
->hdev
)) {
4687 "warm reset change on port %d\n",
4689 clear_port_feature(hdev
, i
,
4690 USB_PORT_FEAT_C_BH_PORT_RESET
);
4692 if (portchange
& USB_PORT_STAT_C_LINK_STATE
) {
4693 clear_port_feature(hub
->hdev
, i
,
4694 USB_PORT_FEAT_C_PORT_LINK_STATE
);
4696 if (portchange
& USB_PORT_STAT_C_CONFIG_ERROR
) {
4698 "config error on port %d\n",
4700 clear_port_feature(hub
->hdev
, i
,
4701 USB_PORT_FEAT_C_PORT_CONFIG_ERROR
);
4704 /* Warm reset a USB3 protocol port if it's in
4705 * SS.Inactive state.
4707 if (hub_port_warm_reset_required(hub
, portstatus
)) {
4710 dev_dbg(hub_dev
, "warm reset port %d\n", i
);
4711 status
= hub_port_reset(hub
, i
, NULL
,
4712 HUB_BH_RESET_TIME
, true);
4714 hub_port_disable(hub
, i
, 1);
4719 hub_port_connect_change(hub
, i
,
4720 portstatus
, portchange
);
4723 /* deal with hub status changes */
4724 if (test_and_clear_bit(0, hub
->event_bits
) == 0)
4726 else if (hub_hub_status(hub
, &hubstatus
, &hubchange
) < 0)
4727 dev_err (hub_dev
, "get_hub_status failed\n");
4729 if (hubchange
& HUB_CHANGE_LOCAL_POWER
) {
4730 dev_dbg (hub_dev
, "power change\n");
4731 clear_hub_feature(hdev
, C_HUB_LOCAL_POWER
);
4732 if (hubstatus
& HUB_STATUS_LOCAL_POWER
)
4733 /* FIXME: Is this always true? */
4734 hub
->limited_power
= 1;
4736 hub
->limited_power
= 0;
4738 if (hubchange
& HUB_CHANGE_OVERCURRENT
) {
4742 dev_dbg(hub_dev
, "over-current change\n");
4743 clear_hub_feature(hdev
, C_HUB_OVER_CURRENT
);
4744 msleep(500); /* Cool down */
4745 hub_power_on(hub
, true);
4746 hub_hub_status(hub
, &status
, &unused
);
4747 if (status
& HUB_STATUS_OVERCURRENT
)
4748 dev_err(hub_dev
, "over-current "
4754 /* Balance the usb_autopm_get_interface() above */
4755 usb_autopm_put_interface_no_suspend(intf
);
4757 /* Balance the usb_autopm_get_interface_no_resume() in
4758 * kick_khubd() and allow autosuspend.
4760 usb_autopm_put_interface(intf
);
4762 usb_unlock_device(hdev
);
4763 kref_put(&hub
->kref
, hub_release
);
4765 } /* end while (1) */
4768 static int hub_thread(void *__unused
)
4770 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4771 * port handover. Otherwise it might see that a full-speed device
4772 * was gone before the EHCI controller had handed its port over to
4773 * the companion full-speed controller.
4779 wait_event_freezable(khubd_wait
,
4780 !list_empty(&hub_event_list
) ||
4781 kthread_should_stop());
4782 } while (!kthread_should_stop() || !list_empty(&hub_event_list
));
4784 pr_debug("%s: khubd exiting\n", usbcore_name
);
4788 static const struct usb_device_id hub_id_table
[] = {
4789 { .match_flags
= USB_DEVICE_ID_MATCH_VENDOR
4790 | USB_DEVICE_ID_MATCH_INT_CLASS
,
4791 .idVendor
= USB_VENDOR_GENESYS_LOGIC
,
4792 .bInterfaceClass
= USB_CLASS_HUB
,
4793 .driver_info
= HUB_QUIRK_CHECK_PORT_AUTOSUSPEND
},
4794 { .match_flags
= USB_DEVICE_ID_MATCH_DEV_CLASS
,
4795 .bDeviceClass
= USB_CLASS_HUB
},
4796 { .match_flags
= USB_DEVICE_ID_MATCH_INT_CLASS
,
4797 .bInterfaceClass
= USB_CLASS_HUB
},
4798 { } /* Terminating entry */
4801 MODULE_DEVICE_TABLE (usb
, hub_id_table
);
4803 static struct usb_driver hub_driver
= {
4806 .disconnect
= hub_disconnect
,
4807 .suspend
= hub_suspend
,
4808 .resume
= hub_resume
,
4809 .reset_resume
= hub_reset_resume
,
4810 .pre_reset
= hub_pre_reset
,
4811 .post_reset
= hub_post_reset
,
4812 .unlocked_ioctl
= hub_ioctl
,
4813 .id_table
= hub_id_table
,
4814 .supports_autosuspend
= 1,
4817 int usb_hub_init(void)
4819 if (usb_register(&hub_driver
) < 0) {
4820 printk(KERN_ERR
"%s: can't register hub driver\n",
4825 khubd_task
= kthread_run(hub_thread
, NULL
, "khubd");
4826 if (!IS_ERR(khubd_task
))
4829 /* Fall through if kernel_thread failed */
4830 usb_deregister(&hub_driver
);
4831 printk(KERN_ERR
"%s: can't start khubd\n", usbcore_name
);
4836 void usb_hub_cleanup(void)
4838 kthread_stop(khubd_task
);
4841 * Hub resources are freed for us by usb_deregister. It calls
4842 * usb_driver_purge on every device which in turn calls that
4843 * devices disconnect function if it is using this driver.
4844 * The hub_disconnect function takes care of releasing the
4845 * individual hub resources. -greg
4847 usb_deregister(&hub_driver
);
4848 } /* usb_hub_cleanup() */
4850 static int descriptors_changed(struct usb_device
*udev
,
4851 struct usb_device_descriptor
*old_device_descriptor
)
4855 unsigned serial_len
= 0;
4857 unsigned old_length
;
4861 if (memcmp(&udev
->descriptor
, old_device_descriptor
,
4862 sizeof(*old_device_descriptor
)) != 0)
4865 /* Since the idVendor, idProduct, and bcdDevice values in the
4866 * device descriptor haven't changed, we will assume the
4867 * Manufacturer and Product strings haven't changed either.
4868 * But the SerialNumber string could be different (e.g., a
4869 * different flash card of the same brand).
4872 serial_len
= strlen(udev
->serial
) + 1;
4875 for (index
= 0; index
< udev
->descriptor
.bNumConfigurations
; index
++) {
4876 old_length
= le16_to_cpu(udev
->config
[index
].desc
.wTotalLength
);
4877 len
= max(len
, old_length
);
4880 buf
= kmalloc(len
, GFP_NOIO
);
4882 dev_err(&udev
->dev
, "no mem to re-read configs after reset\n");
4883 /* assume the worst */
4886 for (index
= 0; index
< udev
->descriptor
.bNumConfigurations
; index
++) {
4887 old_length
= le16_to_cpu(udev
->config
[index
].desc
.wTotalLength
);
4888 length
= usb_get_descriptor(udev
, USB_DT_CONFIG
, index
, buf
,
4890 if (length
!= old_length
) {
4891 dev_dbg(&udev
->dev
, "config index %d, error %d\n",
4896 if (memcmp (buf
, udev
->rawdescriptors
[index
], old_length
)
4898 dev_dbg(&udev
->dev
, "config index %d changed (#%d)\n",
4900 ((struct usb_config_descriptor
*) buf
)->
4901 bConfigurationValue
);
4907 if (!changed
&& serial_len
) {
4908 length
= usb_string(udev
, udev
->descriptor
.iSerialNumber
,
4910 if (length
+ 1 != serial_len
) {
4911 dev_dbg(&udev
->dev
, "serial string error %d\n",
4914 } else if (memcmp(buf
, udev
->serial
, length
) != 0) {
4915 dev_dbg(&udev
->dev
, "serial string changed\n");
4925 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
4926 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4928 * WARNING - don't use this routine to reset a composite device
4929 * (one with multiple interfaces owned by separate drivers)!
4930 * Use usb_reset_device() instead.
4932 * Do a port reset, reassign the device's address, and establish its
4933 * former operating configuration. If the reset fails, or the device's
4934 * descriptors change from their values before the reset, or the original
4935 * configuration and altsettings cannot be restored, a flag will be set
4936 * telling khubd to pretend the device has been disconnected and then
4937 * re-connected. All drivers will be unbound, and the device will be
4938 * re-enumerated and probed all over again.
4940 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4941 * flagged for logical disconnection, or some other negative error code
4942 * if the reset wasn't even attempted.
4944 * The caller must own the device lock. For example, it's safe to use
4945 * this from a driver probe() routine after downloading new firmware.
4946 * For calls that might not occur during probe(), drivers should lock
4947 * the device using usb_lock_device_for_reset().
4949 * Locking exception: This routine may also be called from within an
4950 * autoresume handler. Such usage won't conflict with other tasks
4951 * holding the device lock because these tasks should always call
4952 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
4954 static int usb_reset_and_verify_device(struct usb_device
*udev
)
4956 struct usb_device
*parent_hdev
= udev
->parent
;
4957 struct usb_hub
*parent_hub
;
4958 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
4959 struct usb_device_descriptor descriptor
= udev
->descriptor
;
4961 int port1
= udev
->portnum
;
4963 if (udev
->state
== USB_STATE_NOTATTACHED
||
4964 udev
->state
== USB_STATE_SUSPENDED
) {
4965 dev_dbg(&udev
->dev
, "device reset not allowed in state %d\n",
4971 /* this requires hcd-specific logic; see ohci_restart() */
4972 dev_dbg(&udev
->dev
, "%s for root hub!\n", __func__
);
4975 parent_hub
= hdev_to_hub(parent_hdev
);
4977 /* Disable LPM and LTM while we reset the device and reinstall the alt
4978 * settings. Device-initiated LPM settings, and system exit latency
4979 * settings are cleared when the device is reset, so we have to set
4982 ret
= usb_unlocked_disable_lpm(udev
);
4984 dev_err(&udev
->dev
, "%s Failed to disable LPM\n.", __func__
);
4987 ret
= usb_disable_ltm(udev
);
4989 dev_err(&udev
->dev
, "%s Failed to disable LTM\n.",
4994 set_bit(port1
, parent_hub
->busy_bits
);
4995 for (i
= 0; i
< SET_CONFIG_TRIES
; ++i
) {
4997 /* ep0 maxpacket size may change; let the HCD know about it.
4998 * Other endpoints will be handled by re-enumeration. */
4999 usb_ep0_reinit(udev
);
5000 ret
= hub_port_init(parent_hub
, udev
, port1
, i
);
5001 if (ret
>= 0 || ret
== -ENOTCONN
|| ret
== -ENODEV
)
5004 clear_bit(port1
, parent_hub
->busy_bits
);
5009 /* Device might have changed firmware (DFU or similar) */
5010 if (descriptors_changed(udev
, &descriptor
)) {
5011 dev_info(&udev
->dev
, "device firmware changed\n");
5012 udev
->descriptor
= descriptor
; /* for disconnect() calls */
5016 /* Restore the device's previous configuration */
5017 if (!udev
->actconfig
)
5020 mutex_lock(hcd
->bandwidth_mutex
);
5021 ret
= usb_hcd_alloc_bandwidth(udev
, udev
->actconfig
, NULL
, NULL
);
5023 dev_warn(&udev
->dev
,
5024 "Busted HC? Not enough HCD resources for "
5025 "old configuration.\n");
5026 mutex_unlock(hcd
->bandwidth_mutex
);
5029 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
5030 USB_REQ_SET_CONFIGURATION
, 0,
5031 udev
->actconfig
->desc
.bConfigurationValue
, 0,
5032 NULL
, 0, USB_CTRL_SET_TIMEOUT
);
5035 "can't restore configuration #%d (error=%d)\n",
5036 udev
->actconfig
->desc
.bConfigurationValue
, ret
);
5037 mutex_unlock(hcd
->bandwidth_mutex
);
5040 mutex_unlock(hcd
->bandwidth_mutex
);
5041 usb_set_device_state(udev
, USB_STATE_CONFIGURED
);
5043 /* Put interfaces back into the same altsettings as before.
5044 * Don't bother to send the Set-Interface request for interfaces
5045 * that were already in altsetting 0; besides being unnecessary,
5046 * many devices can't handle it. Instead just reset the host-side
5049 for (i
= 0; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
5050 struct usb_host_config
*config
= udev
->actconfig
;
5051 struct usb_interface
*intf
= config
->interface
[i
];
5052 struct usb_interface_descriptor
*desc
;
5054 desc
= &intf
->cur_altsetting
->desc
;
5055 if (desc
->bAlternateSetting
== 0) {
5056 usb_disable_interface(udev
, intf
, true);
5057 usb_enable_interface(udev
, intf
, true);
5060 /* Let the bandwidth allocation function know that this
5061 * device has been reset, and it will have to use
5062 * alternate setting 0 as the current alternate setting.
5064 intf
->resetting_device
= 1;
5065 ret
= usb_set_interface(udev
, desc
->bInterfaceNumber
,
5066 desc
->bAlternateSetting
);
5067 intf
->resetting_device
= 0;
5070 dev_err(&udev
->dev
, "failed to restore interface %d "
5071 "altsetting %d (error=%d)\n",
5072 desc
->bInterfaceNumber
,
5073 desc
->bAlternateSetting
,
5080 /* Now that the alt settings are re-installed, enable LTM and LPM. */
5081 usb_unlocked_enable_lpm(udev
);
5082 usb_enable_ltm(udev
);
5086 /* LPM state doesn't matter when we're about to destroy the device. */
5087 hub_port_logical_disconnect(parent_hub
, port1
);
5092 * usb_reset_device - warn interface drivers and perform a USB port reset
5093 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5095 * Warns all drivers bound to registered interfaces (using their pre_reset
5096 * method), performs the port reset, and then lets the drivers know that
5097 * the reset is over (using their post_reset method).
5099 * Return value is the same as for usb_reset_and_verify_device().
5101 * The caller must own the device lock. For example, it's safe to use
5102 * this from a driver probe() routine after downloading new firmware.
5103 * For calls that might not occur during probe(), drivers should lock
5104 * the device using usb_lock_device_for_reset().
5106 * If an interface is currently being probed or disconnected, we assume
5107 * its driver knows how to handle resets. For all other interfaces,
5108 * if the driver doesn't have pre_reset and post_reset methods then
5109 * we attempt to unbind it and rebind afterward.
5111 int usb_reset_device(struct usb_device
*udev
)
5115 struct usb_host_config
*config
= udev
->actconfig
;
5117 if (udev
->state
== USB_STATE_NOTATTACHED
||
5118 udev
->state
== USB_STATE_SUSPENDED
) {
5119 dev_dbg(&udev
->dev
, "device reset not allowed in state %d\n",
5124 /* Prevent autosuspend during the reset */
5125 usb_autoresume_device(udev
);
5128 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
5129 struct usb_interface
*cintf
= config
->interface
[i
];
5130 struct usb_driver
*drv
;
5133 if (cintf
->dev
.driver
) {
5134 drv
= to_usb_driver(cintf
->dev
.driver
);
5135 if (drv
->pre_reset
&& drv
->post_reset
)
5136 unbind
= (drv
->pre_reset
)(cintf
);
5137 else if (cintf
->condition
==
5138 USB_INTERFACE_BOUND
)
5141 usb_forced_unbind_intf(cintf
);
5146 ret
= usb_reset_and_verify_device(udev
);
5149 for (i
= config
->desc
.bNumInterfaces
- 1; i
>= 0; --i
) {
5150 struct usb_interface
*cintf
= config
->interface
[i
];
5151 struct usb_driver
*drv
;
5152 int rebind
= cintf
->needs_binding
;
5154 if (!rebind
&& cintf
->dev
.driver
) {
5155 drv
= to_usb_driver(cintf
->dev
.driver
);
5156 if (drv
->post_reset
)
5157 rebind
= (drv
->post_reset
)(cintf
);
5158 else if (cintf
->condition
==
5159 USB_INTERFACE_BOUND
)
5162 if (ret
== 0 && rebind
)
5163 usb_rebind_intf(cintf
);
5167 usb_autosuspend_device(udev
);
5170 EXPORT_SYMBOL_GPL(usb_reset_device
);
5174 * usb_queue_reset_device - Reset a USB device from an atomic context
5175 * @iface: USB interface belonging to the device to reset
5177 * This function can be used to reset a USB device from an atomic
5178 * context, where usb_reset_device() won't work (as it blocks).
5180 * Doing a reset via this method is functionally equivalent to calling
5181 * usb_reset_device(), except for the fact that it is delayed to a
5182 * workqueue. This means that any drivers bound to other interfaces
5183 * might be unbound, as well as users from usbfs in user space.
5187 * - Scheduling two resets at the same time from two different drivers
5188 * attached to two different interfaces of the same device is
5189 * possible; depending on how the driver attached to each interface
5190 * handles ->pre_reset(), the second reset might happen or not.
5192 * - If a driver is unbound and it had a pending reset, the reset will
5195 * - This function can be called during .probe() or .disconnect()
5196 * times. On return from .disconnect(), any pending resets will be
5199 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5202 * NOTE: We don't do any reference count tracking because it is not
5203 * needed. The lifecycle of the work_struct is tied to the
5204 * usb_interface. Before destroying the interface we cancel the
5205 * work_struct, so the fact that work_struct is queued and or
5206 * running means the interface (and thus, the device) exist and
5209 void usb_queue_reset_device(struct usb_interface
*iface
)
5211 schedule_work(&iface
->reset_ws
);
5213 EXPORT_SYMBOL_GPL(usb_queue_reset_device
);
5216 * usb_hub_find_child - Get the pointer of child device
5217 * attached to the port which is specified by @port1.
5218 * @hdev: USB device belonging to the usb hub
5219 * @port1: port num to indicate which port the child device
5222 * USB drivers call this function to get hub's child device
5225 * Return NULL if input param is invalid and
5226 * child's usb_device pointer if non-NULL.
5228 struct usb_device
*usb_hub_find_child(struct usb_device
*hdev
,
5231 struct usb_hub
*hub
= hdev_to_hub(hdev
);
5233 if (port1
< 1 || port1
> hdev
->maxchild
)
5235 return hub
->ports
[port1
- 1]->child
;
5237 EXPORT_SYMBOL_GPL(usb_hub_find_child
);
5240 * usb_set_hub_port_connect_type - set hub port connect type.
5241 * @hdev: USB device belonging to the usb hub
5242 * @port1: port num of the port
5243 * @type: connect type of the port
5245 void usb_set_hub_port_connect_type(struct usb_device
*hdev
, int port1
,
5246 enum usb_port_connect_type type
)
5248 struct usb_hub
*hub
= hdev_to_hub(hdev
);
5250 hub
->ports
[port1
- 1]->connect_type
= type
;
5254 * usb_get_hub_port_connect_type - Get the port's connect type
5255 * @hdev: USB device belonging to the usb hub
5256 * @port1: port num of the port
5258 * Return connect type of the port and if input params are
5259 * invalid, return USB_PORT_CONNECT_TYPE_UNKNOWN.
5261 enum usb_port_connect_type
5262 usb_get_hub_port_connect_type(struct usb_device
*hdev
, int port1
)
5264 struct usb_hub
*hub
= hdev_to_hub(hdev
);
5266 return hub
->ports
[port1
- 1]->connect_type
;
5271 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5272 * @hdev: USB device belonging to the usb hub
5273 * @port1: port num of the port
5275 * Return port's acpi handle if successful, NULL if params are
5278 acpi_handle
usb_get_hub_port_acpi_handle(struct usb_device
*hdev
,
5281 struct usb_hub
*hub
= hdev_to_hub(hdev
);
5283 return DEVICE_ACPI_HANDLE(&hub
->ports
[port1
- 1]->dev
);