xhci: Avoid "dead ports", add roothub port polling.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / usb / core / hub.c
CommitLineData
1da177e4
LT
1/*
2 * USB hub driver.
3 *
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)
8 *
9 */
10
1da177e4
LT
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>
1da177e4
LT
19#include <linux/ioctl.h>
20#include <linux/usb.h>
21#include <linux/usbdevice_fs.h>
27729aad 22#include <linux/usb/hcd.h>
925aa46b 23#include <linux/usb/otg.h>
93362a87 24#include <linux/usb/quirks.h>
9c8d6178 25#include <linux/kthread.h>
4186ecf8 26#include <linux/mutex.h>
7dfb7103 27#include <linux/freezer.h>
b04b3156 28#include <linux/random.h>
1da177e4 29
1da177e4
LT
30#include <asm/uaccess.h>
31#include <asm/byteorder.h>
32
33#include "usb.h"
1da177e4 34
f2a383e4
GKH
35/* if we are in debug mode, always announce new devices */
36#ifdef DEBUG
37#ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
38#define CONFIG_USB_ANNOUNCE_NEW_DEVICES
39#endif
40#endif
41
e6f30dea
ML
42#define USB_VENDOR_GENESYS_LOGIC 0x05e3
43#define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01
44
fa2a9566 45struct usb_port {
ff823c79 46 struct usb_device *child;
fa2a9566
LT
47 struct device dev;
48 struct dev_state *port_owner;
05f91689 49 enum usb_port_connect_type connect_type;
fa2a9566
LT
50};
51
1bb5f66b
AS
52struct usb_hub {
53 struct device *intfdev; /* the "interface" device */
54 struct usb_device *hdev;
e8054854 55 struct kref kref;
1bb5f66b
AS
56 struct urb *urb; /* for interrupt polling pipe */
57
58 /* buffer for urb ... with extra space in case of babble */
59 char (*buffer)[8];
1bb5f66b
AS
60 union {
61 struct usb_hub_status hub;
62 struct usb_port_status port;
63 } *status; /* buffer for status reports */
db90e7a1 64 struct mutex status_mutex; /* for the status buffer */
1bb5f66b
AS
65
66 int error; /* last reported error */
67 int nerrors; /* track consecutive errors */
68
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
72 status change */
73 unsigned long busy_bits[1]; /* ports being reset or
74 resumed */
253e0572
AS
75 unsigned long removed_bits[1]; /* ports with a "removed"
76 device present */
4ee823b8
SS
77 unsigned long wakeup_bits[1]; /* ports that have signaled
78 remote wakeup */
1bb5f66b
AS
79#if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
80#error event_bits[] is too short!
81#endif
82
83 struct usb_hub_descriptor *descriptor; /* class descriptor */
84 struct usb_tt tt; /* Transaction Translator */
85
86 unsigned mA_per_port; /* current for each child */
87
88 unsigned limited_power:1;
89 unsigned quiescing:1;
e8054854 90 unsigned disconnected:1;
1bb5f66b 91
e6f30dea
ML
92 unsigned quirk_check_port_auto_suspend:1;
93
1bb5f66b
AS
94 unsigned has_indicators:1;
95 u8 indicator[USB_MAXCHILDREN];
6d5aefb8 96 struct delayed_work leds;
8520f380 97 struct delayed_work init_work;
fa2a9566 98 struct usb_port **ports;
1bb5f66b
AS
99};
100
dbe79bbe
JY
101static inline int hub_is_superspeed(struct usb_device *hdev)
102{
7bf01185 103 return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
dbe79bbe 104}
1bb5f66b 105
fa286188 106/* Protect struct usb_device->state and ->children members
9ad3d6cc 107 * Note: Both are also protected by ->dev.sem, except that ->state can
1da177e4
LT
108 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
109static DEFINE_SPINLOCK(device_state_lock);
110
111/* khubd's worklist and its lock */
112static DEFINE_SPINLOCK(hub_event_lock);
113static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
114
115/* Wakes up khubd */
116static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
117
9c8d6178 118static struct task_struct *khubd_task;
1da177e4
LT
119
120/* cycle leds on hubs that aren't blinking for attention */
90ab5ee9 121static bool blinkenlights = 0;
1da177e4
LT
122module_param (blinkenlights, bool, S_IRUGO);
123MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
124
fd7c519d
JK
125/*
126 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
127 * 10 seconds to send reply for the initial 64-byte descriptor request.
128 */
129/* define initial 64-byte descriptor request timeout in milliseconds */
130static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
131module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
b9cef6c3
WF
132MODULE_PARM_DESC(initial_descriptor_timeout,
133 "initial 64-byte descriptor request timeout in milliseconds "
134 "(default 5000 - 5.0 seconds)");
fd7c519d 135
1da177e4
LT
136/*
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.
143 *
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.
149 */
90ab5ee9 150static bool old_scheme_first = 0;
1da177e4
LT
151module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
152MODULE_PARM_DESC(old_scheme_first,
153 "start with the old device initialization scheme");
154
90ab5ee9 155static bool use_both_schemes = 1;
1da177e4
LT
156module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
157MODULE_PARM_DESC(use_both_schemes,
158 "try the other device initialization scheme if the "
159 "first one fails");
160
32fe0198
AS
161/* Mutual exclusion for EHCI CF initialization. This interferes with
162 * port reset on some companion controllers.
163 */
164DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
165EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
166
948fea37
AS
167#define HUB_DEBOUNCE_TIMEOUT 1500
168#define HUB_DEBOUNCE_STEP 25
169#define HUB_DEBOUNCE_STABLE 100
170
fa2a9566
LT
171#define to_usb_port(_dev) \
172 container_of(_dev, struct usb_port, dev)
1da177e4 173
742120c6
ML
174static int usb_reset_and_verify_device(struct usb_device *udev);
175
131dec34 176static inline char *portspeed(struct usb_hub *hub, int portstatus)
1da177e4 177{
131dec34
SS
178 if (hub_is_superspeed(hub->hdev))
179 return "5.0 Gb/s";
288ead45 180 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
1da177e4 181 return "480 Mb/s";
288ead45 182 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
1da177e4
LT
183 return "1.5 Mb/s";
184 else
185 return "12 Mb/s";
186}
1da177e4
LT
187
188/* Note that hdev or one of its children must be locked! */
25118084 189static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
1da177e4 190{
ff823c79 191 if (!hdev || !hdev->actconfig || !hdev->maxchild)
25118084 192 return NULL;
1da177e4
LT
193 return usb_get_intfdata(hdev->actconfig->interface[0]);
194}
195
d9b2099c
SS
196static int usb_device_supports_lpm(struct usb_device *udev)
197{
198 /* USB 2.1 (and greater) devices indicate LPM support through
199 * their USB 2.0 Extended Capabilities BOS descriptor.
200 */
201 if (udev->speed == USB_SPEED_HIGH) {
202 if (udev->bos->ext_cap &&
203 (USB_LPM_SUPPORT &
204 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
205 return 1;
206 return 0;
207 }
51e0a012
SS
208
209 /* All USB 3.0 must support LPM, but we need their max exit latency
210 * information from the SuperSpeed Extended Capabilities BOS descriptor.
211 */
212 if (!udev->bos->ss_cap) {
213 dev_warn(&udev->dev, "No LPM exit latency info found. "
214 "Power management will be impacted.\n");
215 return 0;
216 }
217 if (udev->parent->lpm_capable)
218 return 1;
219
220 dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. "
221 "Power management will be impacted.\n");
d9b2099c
SS
222 return 0;
223}
224
51e0a012
SS
225/*
226 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
227 * either U1 or U2.
228 */
229static void usb_set_lpm_mel(struct usb_device *udev,
230 struct usb3_lpm_parameters *udev_lpm_params,
231 unsigned int udev_exit_latency,
232 struct usb_hub *hub,
233 struct usb3_lpm_parameters *hub_lpm_params,
234 unsigned int hub_exit_latency)
235{
236 unsigned int total_mel;
237 unsigned int device_mel;
238 unsigned int hub_mel;
239
240 /*
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
244 * bound for.
245 *
246 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
247 * means 0.1us). Multiply that by 100 to get nanoseconds.
248 */
249 total_mel = hub_lpm_params->mel +
250 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
251
252 /*
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
255 * latency.
256 *
257 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
258 * Multiply that by 1000 to get nanoseconds.
259 */
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;
264 else
265 total_mel += hub_mel;
266
267 udev_lpm_params->mel = total_mel;
268}
269
270/*
271 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
272 * a transition from either U1 or U2.
273 */
274static void usb_set_lpm_pel(struct usb_device *udev,
275 struct usb3_lpm_parameters *udev_lpm_params,
276 unsigned int udev_exit_latency,
277 struct usb_hub *hub,
278 struct usb3_lpm_parameters *hub_lpm_params,
279 unsigned int hub_exit_latency,
280 unsigned int port_to_port_exit_latency)
281{
282 unsigned int first_link_pel;
283 unsigned int hub_pel;
284
285 /*
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.
289 */
290 if (udev_exit_latency > hub_exit_latency)
291 first_link_pel = udev_exit_latency * 1000;
292 else
293 first_link_pel = hub_exit_latency * 1000;
294
295 /*
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.
300 */
301 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
302
303 /*
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.
306 */
307 if (first_link_pel > hub_pel)
308 udev_lpm_params->pel = first_link_pel;
309 else
310 udev_lpm_params->pel = hub_pel;
311}
312
313/*
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.
317 *
318 * Section C.1.5.1 describes the four components to this:
319 * - t1: device PEL
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.
323 *
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.
327 */
328static void usb_set_lpm_sel(struct usb_device *udev,
329 struct usb3_lpm_parameters *udev_lpm_params)
330{
331 struct usb_device *parent;
332 unsigned int num_hubs;
333 unsigned int total_sel;
334
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)
340 num_hubs++;
341 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
342 if (num_hubs > 0)
343 total_sel += 2100 + 250 * (num_hubs - 1);
344
345 /* t4 = 250ns * num_hubs */
346 total_sel += 250 * num_hubs;
347
348 udev_lpm_params->sel = total_sel;
349}
350
351static void usb_set_lpm_parameters(struct usb_device *udev)
352{
353 struct usb_hub *hub;
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;
359
360 if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
361 return;
362
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.
366 */
367 if (!hub)
368 return;
369
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;
374
375 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
376 hub, &udev->parent->u1_params, hub_u1_del);
377
378 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
379 hub, &udev->parent->u2_params, hub_u2_del);
380
381 /*
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.
387 *
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
393 * exit latencies.
394 *
395 * What do we do if the U2 exit latency is less than the U1 exit
396 * latency? It's possible, although not likely...
397 */
398 port_to_port_delay = 1;
399
400 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
401 hub, &udev->parent->u1_params, hub_u1_del,
402 port_to_port_delay);
403
404 if (hub_u2_del > hub_u1_del)
405 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
406 else
407 port_to_port_delay = 1 + hub_u1_del;
408
409 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
410 hub, &udev->parent->u2_params, hub_u2_del,
411 port_to_port_delay);
412
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);
416}
417
1da177e4 418/* USB 2.0 spec Section 11.24.4.5 */
dbe79bbe 419static int get_hub_descriptor(struct usb_device *hdev, void *data)
1da177e4 420{
dbe79bbe
JY
421 int i, ret, size;
422 unsigned dtype;
423
424 if (hub_is_superspeed(hdev)) {
425 dtype = USB_DT_SS_HUB;
426 size = USB_DT_SS_HUB_SIZE;
427 } else {
428 dtype = USB_DT_HUB;
429 size = sizeof(struct usb_hub_descriptor);
430 }
1da177e4
LT
431
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,
dbe79bbe 435 dtype << 8, 0, data, size,
1da177e4
LT
436 USB_CTRL_GET_TIMEOUT);
437 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
438 return ret;
439 }
440 return -EINVAL;
441}
442
443/*
444 * USB 2.0 spec Section 11.24.2.1
445 */
446static int clear_hub_feature(struct usb_device *hdev, int feature)
447{
448 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
449 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
450}
451
452/*
453 * USB 2.0 spec Section 11.24.2.2
454 */
455static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
456{
457 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
458 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
459 NULL, 0, 1000);
460}
461
462/*
463 * USB 2.0 spec Section 11.24.2.13
464 */
465static int set_port_feature(struct usb_device *hdev, int port1, int feature)
466{
467 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
468 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
469 NULL, 0, 1000);
470}
471
472/*
473 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
474 * for info about using port indicators
475 */
476static void set_port_led(
477 struct usb_hub *hub,
478 int port1,
479 int selector
480)
481{
482 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
483 USB_PORT_FEAT_INDICATOR);
484 if (status < 0)
485 dev_dbg (hub->intfdev,
486 "port %d indicator %s status %d\n",
487 port1,
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;
494 }; s; }),
495 status);
496}
497
498#define LED_CYCLE_PERIOD ((2*HZ)/3)
499
c4028958 500static void led_work (struct work_struct *work)
1da177e4 501{
c4028958
DH
502 struct usb_hub *hub =
503 container_of(work, struct usb_hub, leds.work);
1da177e4
LT
504 struct usb_device *hdev = hub->hdev;
505 unsigned i;
506 unsigned changed = 0;
507 int cursor = -1;
508
509 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
510 return;
511
512 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
513 unsigned selector, mode;
514
515 /* 30%-50% duty cycle */
516
517 switch (hub->indicator[i]) {
518 /* cycle marker */
519 case INDICATOR_CYCLE:
520 cursor = i;
521 selector = HUB_LED_AUTO;
522 mode = INDICATOR_AUTO;
523 break;
524 /* blinking green = sw attention */
525 case INDICATOR_GREEN_BLINK:
526 selector = HUB_LED_GREEN;
527 mode = INDICATOR_GREEN_BLINK_OFF;
528 break;
529 case INDICATOR_GREEN_BLINK_OFF:
530 selector = HUB_LED_OFF;
531 mode = INDICATOR_GREEN_BLINK;
532 break;
533 /* blinking amber = hw attention */
534 case INDICATOR_AMBER_BLINK:
535 selector = HUB_LED_AMBER;
536 mode = INDICATOR_AMBER_BLINK_OFF;
537 break;
538 case INDICATOR_AMBER_BLINK_OFF:
539 selector = HUB_LED_OFF;
540 mode = INDICATOR_AMBER_BLINK;
541 break;
542 /* blink green/amber = reserved */
543 case INDICATOR_ALT_BLINK:
544 selector = HUB_LED_GREEN;
545 mode = INDICATOR_ALT_BLINK_OFF;
546 break;
547 case INDICATOR_ALT_BLINK_OFF:
548 selector = HUB_LED_AMBER;
549 mode = INDICATOR_ALT_BLINK;
550 break;
551 default:
552 continue;
553 }
554 if (selector != HUB_LED_AUTO)
555 changed = 1;
556 set_port_led(hub, i + 1, selector);
557 hub->indicator[i] = mode;
558 }
559 if (!changed && blinkenlights) {
560 cursor++;
561 cursor %= hub->descriptor->bNbrPorts;
562 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
563 hub->indicator[cursor] = INDICATOR_CYCLE;
564 changed++;
565 }
566 if (changed)
567 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
568}
569
570/* use a short timeout for hub/port status fetches */
571#define USB_STS_TIMEOUT 1000
572#define USB_STS_RETRIES 5
573
574/*
575 * USB 2.0 spec Section 11.24.2.6
576 */
577static int get_hub_status(struct usb_device *hdev,
578 struct usb_hub_status *data)
579{
580 int i, status = -ETIMEDOUT;
581
3824c1dd
LP
582 for (i = 0; i < USB_STS_RETRIES &&
583 (status == -ETIMEDOUT || status == -EPIPE); i++) {
1da177e4
LT
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);
587 }
588 return status;
589}
590
591/*
592 * USB 2.0 spec Section 11.24.2.7
593 */
594static int get_port_status(struct usb_device *hdev, int port1,
595 struct usb_port_status *data)
596{
597 int i, status = -ETIMEDOUT;
598
3824c1dd
LP
599 for (i = 0; i < USB_STS_RETRIES &&
600 (status == -ETIMEDOUT || status == -EPIPE); i++) {
1da177e4
LT
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);
604 }
605 return status;
606}
607
3eb14915
AS
608static int hub_port_status(struct usb_hub *hub, int port1,
609 u16 *status, u16 *change)
610{
611 int ret;
612
613 mutex_lock(&hub->status_mutex);
614 ret = get_port_status(hub->hdev, port1, &hub->status->port);
615 if (ret < 4) {
616 dev_err(hub->intfdev,
617 "%s failed (err = %d)\n", __func__, ret);
618 if (ret >= 0)
619 ret = -EIO;
620 } else {
621 *status = le16_to_cpu(hub->status->port.wPortStatus);
622 *change = le16_to_cpu(hub->status->port.wPortChange);
dbe79bbe 623
3eb14915
AS
624 ret = 0;
625 }
626 mutex_unlock(&hub->status_mutex);
627 return ret;
628}
629
1da177e4
LT
630static void kick_khubd(struct usb_hub *hub)
631{
632 unsigned long flags;
633
634 spin_lock_irqsave(&hub_event_lock, flags);
2e2c5eea 635 if (!hub->disconnected && list_empty(&hub->event_list)) {
1da177e4 636 list_add_tail(&hub->event_list, &hub_event_list);
8e4ceb38
AS
637
638 /* Suppress autosuspend until khubd runs */
639 usb_autopm_get_interface_no_resume(
640 to_usb_interface(hub->intfdev));
1da177e4
LT
641 wake_up(&khubd_wait);
642 }
643 spin_unlock_irqrestore(&hub_event_lock, flags);
644}
645
646void usb_kick_khubd(struct usb_device *hdev)
647{
25118084
AS
648 struct usb_hub *hub = hdev_to_hub(hdev);
649
650 if (hub)
651 kick_khubd(hub);
1da177e4
LT
652}
653
4ee823b8
SS
654/*
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.
657 *
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.
661 */
662void usb_wakeup_notification(struct usb_device *hdev,
663 unsigned int portnum)
664{
665 struct usb_hub *hub;
666
667 if (!hdev)
668 return;
669
670 hub = hdev_to_hub(hdev);
671 if (hub) {
672 set_bit(portnum, hub->wakeup_bits);
673 kick_khubd(hub);
674 }
675}
676EXPORT_SYMBOL_GPL(usb_wakeup_notification);
1da177e4
LT
677
678/* completion function, fires on port status changes and various faults */
7d12e780 679static void hub_irq(struct urb *urb)
1da177e4 680{
ec17cf1c 681 struct usb_hub *hub = urb->context;
e015268d 682 int status = urb->status;
71d2718f 683 unsigned i;
1da177e4
LT
684 unsigned long bits;
685
e015268d 686 switch (status) {
1da177e4
LT
687 case -ENOENT: /* synchronous unlink */
688 case -ECONNRESET: /* async unlink */
689 case -ESHUTDOWN: /* hardware going away */
690 return;
691
692 default: /* presumably an error */
693 /* Cause a hub reset after 10 consecutive errors */
e015268d 694 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
1da177e4
LT
695 if ((++hub->nerrors < 10) || hub->error)
696 goto resubmit;
e015268d 697 hub->error = status;
1da177e4 698 /* FALL THROUGH */
ec17cf1c 699
1da177e4
LT
700 /* let khubd handle things */
701 case 0: /* we got data: port status changed */
702 bits = 0;
703 for (i = 0; i < urb->actual_length; ++i)
704 bits |= ((unsigned long) ((*hub->buffer)[i]))
705 << (i*8);
706 hub->event_bits[0] = bits;
707 break;
708 }
709
710 hub->nerrors = 0;
711
712 /* Something happened, let khubd figure it out */
713 kick_khubd(hub);
714
715resubmit:
716 if (hub->quiescing)
717 return;
718
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);
722}
723
724/* USB 2.0 spec Section 11.24.2.3 */
725static inline int
726hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
727{
c2f6595f 728 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1da177e4
LT
729 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
730 tt, NULL, 0, 1000);
731}
732
733/*
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.
738 */
cb88a1b8 739static void hub_tt_work(struct work_struct *work)
1da177e4 740{
c4028958 741 struct usb_hub *hub =
cb88a1b8 742 container_of(work, struct usb_hub, tt.clear_work);
1da177e4
LT
743 unsigned long flags;
744
745 spin_lock_irqsave (&hub->tt.lock, flags);
3b6054da 746 while (!list_empty(&hub->tt.clear_list)) {
d0f830d3 747 struct list_head *next;
1da177e4
LT
748 struct usb_tt_clear *clear;
749 struct usb_device *hdev = hub->hdev;
cb88a1b8 750 const struct hc_driver *drv;
1da177e4
LT
751 int status;
752
d0f830d3
HS
753 next = hub->tt.clear_list.next;
754 clear = list_entry (next, struct usb_tt_clear, clear_list);
1da177e4
LT
755 list_del (&clear->clear_list);
756
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);
1da177e4
LT
760 if (status)
761 dev_err (&hdev->dev,
762 "clear tt %d (%04x) error %d\n",
763 clear->tt, clear->devinfo, status);
cb88a1b8
AS
764
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);
769
1bc3c9e1 770 kfree(clear);
cb88a1b8 771 spin_lock_irqsave(&hub->tt.lock, flags);
1da177e4
LT
772 }
773 spin_unlock_irqrestore (&hub->tt.lock, flags);
774}
775
776/**
cb88a1b8
AS
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
1da177e4
LT
779 *
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
783 * interrupt context.
784 *
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.
787 */
cb88a1b8 788int usb_hub_clear_tt_buffer(struct urb *urb)
1da177e4 789{
cb88a1b8
AS
790 struct usb_device *udev = urb->dev;
791 int pipe = urb->pipe;
1da177e4
LT
792 struct usb_tt *tt = udev->tt;
793 unsigned long flags;
794 struct usb_tt_clear *clear;
795
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.
799 */
54e6ecb2 800 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
1da177e4
LT
801 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
802 /* FIXME recover somehow ... RESET_TT? */
cb88a1b8 803 return -ENOMEM;
1da177e4
LT
804 }
805
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;
cb88a1b8
AS
815
816 /* info for completion callback */
817 clear->hcd = bus_to_hcd(udev->bus);
818 clear->ep = urb->ep;
819
1da177e4
LT
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);
cb88a1b8 823 schedule_work(&tt->clear_work);
1da177e4 824 spin_unlock_irqrestore (&tt->lock, flags);
cb88a1b8 825 return 0;
1da177e4 826}
cb88a1b8 827EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
1da177e4 828
8520f380
AS
829/* If do_delay is false, return the number of milliseconds the caller
830 * needs to delay.
831 */
832static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
1da177e4
LT
833{
834 int port1;
b789696a 835 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
8520f380 836 unsigned delay;
4489a571
AS
837 u16 wHubCharacteristics =
838 le16_to_cpu(hub->descriptor->wHubCharacteristics);
839
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.
845 */
846 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
1da177e4 847 dev_dbg(hub->intfdev, "enabling power on all ports\n");
4489a571
AS
848 else
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++)
a0693bd0 852 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
1da177e4 853
b789696a 854 /* Wait at least 100 msec for power to become stable */
8520f380
AS
855 delay = max(pgood_delay, (unsigned) 100);
856 if (do_delay)
857 msleep(delay);
858 return delay;
1da177e4
LT
859}
860
1da177e4
LT
861static int hub_hub_status(struct usb_hub *hub,
862 u16 *status, u16 *change)
863{
864 int ret;
865
db90e7a1 866 mutex_lock(&hub->status_mutex);
1da177e4
LT
867 ret = get_hub_status(hub->hdev, &hub->status->hub);
868 if (ret < 0)
869 dev_err (hub->intfdev,
441b62c1 870 "%s failed (err = %d)\n", __func__, ret);
1da177e4
LT
871 else {
872 *status = le16_to_cpu(hub->status->hub.wHubStatus);
873 *change = le16_to_cpu(hub->status->hub.wHubChange);
874 ret = 0;
875 }
db90e7a1 876 mutex_unlock(&hub->status_mutex);
1da177e4
LT
877 return ret;
878}
879
41e7e056
SS
880static int hub_set_port_link_state(struct usb_hub *hub, int port1,
881 unsigned int link_status)
882{
883 return set_port_feature(hub->hdev,
884 port1 | (link_status << 3),
885 USB_PORT_FEAT_LINK_STATE);
886}
887
888/*
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.
893 *
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
896 * state.
897 */
898static int hub_usb3_port_disable(struct usb_hub *hub, int port1)
899{
900 int ret;
901 int total_time;
902 u16 portchange, portstatus;
903
904 if (!hub_is_superspeed(hub->hdev))
905 return -EINVAL;
906
907 ret = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_SS_DISABLED);
908 if (ret) {
909 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
910 port1, ret);
911 return ret;
912 }
913
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);
917 if (ret < 0)
918 return ret;
919
920 if ((portstatus & USB_PORT_STAT_LINK_STATE) ==
921 USB_SS_PORT_LS_SS_DISABLED)
922 break;
923 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
924 break;
925 msleep(HUB_DEBOUNCE_STEP);
926 }
927 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
928 dev_warn(hub->intfdev, "Could not disable port %d after %d ms\n",
929 port1, total_time);
930
931 return hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_RX_DETECT);
932}
933
8b28c752
AS
934static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
935{
936 struct usb_device *hdev = hub->hdev;
0458d5b4 937 int ret = 0;
8b28c752 938
ff823c79
LT
939 if (hub->ports[port1 - 1]->child && set_state)
940 usb_set_device_state(hub->ports[port1 - 1]->child,
8b28c752 941 USB_STATE_NOTATTACHED);
41e7e056
SS
942 if (!hub->error) {
943 if (hub_is_superspeed(hub->hdev))
944 ret = hub_usb3_port_disable(hub, port1);
945 else
946 ret = clear_port_feature(hdev, port1,
947 USB_PORT_FEAT_ENABLE);
948 }
8b28c752
AS
949 if (ret)
950 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
0458d5b4 951 port1, ret);
8b28c752
AS
952 return ret;
953}
954
0458d5b4 955/*
6d42fcdb 956 * Disable a port and mark a logical connect-change event, so that some
0458d5b4
AS
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.
959 */
960static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
961{
962 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
963 hub_port_disable(hub, port1, 1);
7d069b7d 964
0458d5b4
AS
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.
972 */
973
974 set_bit(port1, hub->change_bits);
975 kick_khubd(hub);
976}
977
253e0572
AS
978/**
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.
982 *
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.
987 */
988int usb_remove_device(struct usb_device *udev)
989{
990 struct usb_hub *hub;
991 struct usb_interface *intf;
992
993 if (!udev->parent) /* Can't remove a root hub */
994 return -EINVAL;
995 hub = hdev_to_hub(udev->parent);
996 intf = to_usb_interface(hub->intfdev);
997
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);
1002 return 0;
1003}
1004
6ee0b270 1005enum hub_activation_type {
8e4ceb38 1006 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
8520f380 1007 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
6ee0b270 1008};
5e6effae 1009
8520f380
AS
1010static void hub_init_func2(struct work_struct *ws);
1011static void hub_init_func3(struct work_struct *ws);
1012
f2835219 1013static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
5e6effae
AS
1014{
1015 struct usb_device *hdev = hub->hdev;
653a39d1
SS
1016 struct usb_hcd *hcd;
1017 int ret;
5e6effae 1018 int port1;
f2835219 1019 int status;
948fea37 1020 bool need_debounce_delay = false;
8520f380
AS
1021 unsigned delay;
1022
1023 /* Continue a partial initialization */
1024 if (type == HUB_INIT2)
1025 goto init2;
1026 if (type == HUB_INIT3)
1027 goto init3;
5e6effae 1028
a45aa3b3
EF
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
1034 * reset procedure.
1035 *
1036 * After a resume, port power should still be on.
f2835219
AS
1037 * For any other type of activation, turn it on.
1038 */
8520f380 1039 if (type != HUB_RESUME) {
a45aa3b3
EF
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);
1045 if (ret < 0)
1046 dev_err(hub->intfdev,
1047 "set hub depth failed\n");
1048 }
8520f380
AS
1049
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.
1056 *
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.
1061 */
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));
61fbeba1
AS
1067
1068 /* Suppress autosuspend until init is done */
8e4ceb38
AS
1069 usb_autopm_get_interface_no_resume(
1070 to_usb_interface(hub->intfdev));
8520f380 1071 return; /* Continues at init2: below */
653a39d1
SS
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.
1076 */
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);
1081 if (ret < 0) {
1082 dev_err(hub->intfdev, "Host not "
1083 "accepting hub info "
1084 "update.\n");
1085 dev_err(hub->intfdev, "LS/FS devices "
1086 "and hubs may not work "
1087 "under this hub\n.");
1088 }
1089 }
1090 hub_power_on(hub, true);
8520f380
AS
1091 } else {
1092 hub_power_on(hub, true);
1093 }
1094 }
1095 init2:
f2835219 1096
6ee0b270
AS
1097 /* Check each port and set hub->change_bits to let khubd know
1098 * which ports need attention.
5e6effae
AS
1099 */
1100 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
ff823c79 1101 struct usb_device *udev = hub->ports[port1 - 1]->child;
5e6effae
AS
1102 u16 portstatus, portchange;
1103
6ee0b270
AS
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);
1110
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.
1115 */
1116 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1117 type != HUB_RESUME ||
1118 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1119 !udev ||
1120 udev->state == USB_STATE_NOTATTACHED)) {
9f0a6cd3
AX
1121 /*
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.
9f0a6cd3 1125 */
131dec34 1126 if (!hub_is_superspeed(hdev)) {
9f0a6cd3
AX
1127 clear_port_feature(hdev, port1,
1128 USB_PORT_FEAT_ENABLE);
1129 portstatus &= ~USB_PORT_STAT_ENABLE;
85f0ff46
SS
1130 } else {
1131 /* Pretend that power was lost for USB3 devs */
1132 portstatus &= ~USB_PORT_STAT_ENABLE;
9f0a6cd3 1133 }
5e6effae
AS
1134 }
1135
948fea37
AS
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);
1141 }
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);
1146 }
79c3dd81
DZ
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);
1152 }
253e0572
AS
1153 /* We can forget about a "removed" device when there's a
1154 * physical disconnect or the connect status changes.
1155 */
1156 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1157 (portchange & USB_PORT_STAT_C_CONNECTION))
1158 clear_bit(port1, hub->removed_bits);
1159
6ee0b270
AS
1160 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1161 /* Tell khubd to disconnect the device or
1162 * check for a new connection
1163 */
1164 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1165 set_bit(port1, hub->change_bits);
1166
1167 } else if (portstatus & USB_PORT_STAT_ENABLE) {
72937e1e
SS
1168 bool port_resumed = (portstatus &
1169 USB_PORT_STAT_LINK_STATE) ==
1170 USB_SS_PORT_LS_U0;
6ee0b270
AS
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
72937e1e
SS
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.
6ee0b270 1178 */
72937e1e
SS
1179 if (portchange || (hub_is_superspeed(hub->hdev) &&
1180 port_resumed))
6ee0b270 1181 set_bit(port1, hub->change_bits);
5e6effae 1182
6ee0b270 1183 } else if (udev->persist_enabled) {
6ee0b270 1184#ifdef CONFIG_PM
5e6effae 1185 udev->reset_resume = 1;
6ee0b270 1186#endif
8808f00c
AS
1187 set_bit(port1, hub->change_bits);
1188
6ee0b270
AS
1189 } else {
1190 /* The power session is gone; tell khubd */
1191 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1192 set_bit(port1, hub->change_bits);
5e6effae 1193 }
5e6effae
AS
1194 }
1195
948fea37
AS
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.
1200 *
1201 * If any port-status changes do occur during this delay, khubd
1202 * will see them later and handle them normally.
1203 */
8520f380
AS
1204 if (need_debounce_delay) {
1205 delay = HUB_DEBOUNCE_STABLE;
1206
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 */
1213 } else {
1214 msleep(delay);
1215 }
1216 }
1217 init3:
f2835219
AS
1218 hub->quiescing = 0;
1219
1220 status = usb_submit_urb(hub->urb, GFP_NOIO);
1221 if (status < 0)
1222 dev_err(hub->intfdev, "activate --> %d\n", status);
1223 if (hub->has_indicators && blinkenlights)
1224 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1225
1226 /* Scan all ports that need attention */
1227 kick_khubd(hub);
8e4ceb38
AS
1228
1229 /* Allow autosuspend if it was suppressed */
1230 if (type <= HUB_INIT3)
1231 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
5e6effae
AS
1232}
1233
8520f380
AS
1234/* Implement the continuations for the delays above */
1235static void hub_init_func2(struct work_struct *ws)
1236{
1237 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1238
1239 hub_activate(hub, HUB_INIT2);
1240}
1241
1242static void hub_init_func3(struct work_struct *ws)
1243{
1244 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1245
1246 hub_activate(hub, HUB_INIT3);
1247}
1248
4330354f
AS
1249enum hub_quiescing_type {
1250 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1251};
1252
1253static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1254{
1255 struct usb_device *hdev = hub->hdev;
1256 int i;
1257
8520f380
AS
1258 cancel_delayed_work_sync(&hub->init_work);
1259
4330354f
AS
1260 /* khubd and related activity won't re-trigger */
1261 hub->quiescing = 1;
1262
1263 if (type != HUB_SUSPEND) {
1264 /* Disconnect all the children */
1265 for (i = 0; i < hdev->maxchild; ++i) {
ff823c79
LT
1266 if (hub->ports[i]->child)
1267 usb_disconnect(&hub->ports[i]->child);
4330354f
AS
1268 }
1269 }
1270
1271 /* Stop khubd and related activity */
1272 usb_kill_urb(hub->urb);
1273 if (hub->has_indicators)
1274 cancel_delayed_work_sync(&hub->leds);
1275 if (hub->tt.hub)
036546bf 1276 flush_work(&hub->tt.clear_work);
4330354f
AS
1277}
1278
3eb14915
AS
1279/* caller has locked the hub device */
1280static int hub_pre_reset(struct usb_interface *intf)
1281{
1282 struct usb_hub *hub = usb_get_intfdata(intf);
1283
4330354f 1284 hub_quiesce(hub, HUB_PRE_RESET);
f07600cf 1285 return 0;
7d069b7d
AS
1286}
1287
1288/* caller has locked the hub device */
f07600cf 1289static int hub_post_reset(struct usb_interface *intf)
7d069b7d 1290{
7de18d8b
AS
1291 struct usb_hub *hub = usb_get_intfdata(intf);
1292
f2835219 1293 hub_activate(hub, HUB_POST_RESET);
f07600cf 1294 return 0;
7d069b7d
AS
1295}
1296
fa2a9566
LT
1297static void usb_port_device_release(struct device *dev)
1298{
1299 struct usb_port *port_dev = to_usb_port(dev);
1300
1301 kfree(port_dev);
1302}
1303
1304static void usb_hub_remove_port_device(struct usb_hub *hub,
1305 int port1)
1306{
1307 device_unregister(&hub->ports[port1 - 1]->dev);
1308}
1309
1310struct device_type usb_port_device_type = {
1311 .name = "usb_port",
1312 .release = usb_port_device_release,
1313};
1314
1315static int usb_hub_create_port_device(struct usb_hub *hub,
1316 int port1)
1317{
1318 struct usb_port *port_dev = NULL;
1319 int retval;
1320
1321 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
1322 if (!port_dev) {
1323 retval = -ENOMEM;
1324 goto exit;
1325 }
1326
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);
1331
1332 retval = device_register(&port_dev->dev);
1333 if (retval)
1334 goto error_register;
1335 return 0;
1336
1337error_register:
1338 put_device(&port_dev->dev);
1339exit:
1340 return retval;
1341}
1342
1da177e4
LT
1343static int hub_configure(struct usb_hub *hub,
1344 struct usb_endpoint_descriptor *endpoint)
1345{
b356b7c7 1346 struct usb_hcd *hcd;
1da177e4
LT
1347 struct usb_device *hdev = hub->hdev;
1348 struct device *hub_dev = hub->intfdev;
1349 u16 hubstatus, hubchange;
74ad9bd2 1350 u16 wHubCharacteristics;
1da177e4 1351 unsigned int pipe;
fa2a9566 1352 int maxp, ret, i;
7cbe5dca 1353 char *message = "out of memory";
1da177e4 1354
d697cdda 1355 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
1da177e4 1356 if (!hub->buffer) {
1da177e4
LT
1357 ret = -ENOMEM;
1358 goto fail;
1359 }
1360
1361 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1362 if (!hub->status) {
1da177e4
LT
1363 ret = -ENOMEM;
1364 goto fail;
1365 }
db90e7a1 1366 mutex_init(&hub->status_mutex);
1da177e4
LT
1367
1368 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1369 if (!hub->descriptor) {
1da177e4
LT
1370 ret = -ENOMEM;
1371 goto fail;
1372 }
1373
1374 /* Request the entire hub descriptor.
1375 * hub->descriptor can handle USB_MAXCHILDREN ports,
1376 * but the hub can/will return fewer bytes here.
1377 */
dbe79bbe 1378 ret = get_hub_descriptor(hdev, hub->descriptor);
1da177e4
LT
1379 if (ret < 0) {
1380 message = "can't read hub descriptor";
1381 goto fail;
1382 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1383 message = "hub has too many ports!";
1384 ret = -ENODEV;
1385 goto fail;
1386 }
1387
1388 hdev->maxchild = hub->descriptor->bNbrPorts;
1389 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
1390 (hdev->maxchild == 1) ? "" : "s");
1391
fa2a9566
LT
1392 hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *),
1393 GFP_KERNEL);
ff823c79 1394 if (!hub->ports) {
7cbe5dca
AS
1395 ret = -ENOMEM;
1396 goto fail;
1397 }
1398
74ad9bd2 1399 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
1da177e4 1400
dbe79bbe
JY
1401 /* FIXME for USB 3.0, skip for now */
1402 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1403 !(hub_is_superspeed(hdev))) {
1da177e4
LT
1404 int i;
1405 char portstr [USB_MAXCHILDREN + 1];
1406
1407 for (i = 0; i < hdev->maxchild; i++)
dbe79bbe 1408 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
1da177e4
LT
1409 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1410 ? 'F' : 'R';
1411 portstr[hdev->maxchild] = 0;
1412 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1413 } else
1414 dev_dbg(hub_dev, "standalone hub\n");
1415
74ad9bd2 1416 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
7bf01185
AD
1417 case HUB_CHAR_COMMON_LPSM:
1418 dev_dbg(hub_dev, "ganged power switching\n");
1419 break;
1420 case HUB_CHAR_INDV_PORT_LPSM:
1421 dev_dbg(hub_dev, "individual port power switching\n");
1422 break;
1423 case HUB_CHAR_NO_LPSM:
1424 case HUB_CHAR_LPSM:
1425 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1426 break;
1da177e4
LT
1427 }
1428
74ad9bd2 1429 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
7bf01185
AD
1430 case HUB_CHAR_COMMON_OCPM:
1431 dev_dbg(hub_dev, "global over-current protection\n");
1432 break;
1433 case HUB_CHAR_INDV_PORT_OCPM:
1434 dev_dbg(hub_dev, "individual port over-current protection\n");
1435 break;
1436 case HUB_CHAR_NO_OCPM:
1437 case HUB_CHAR_OCPM:
1438 dev_dbg(hub_dev, "no over-current protection\n");
1439 break;
1da177e4
LT
1440 }
1441
1442 spin_lock_init (&hub->tt.lock);
1443 INIT_LIST_HEAD (&hub->tt.clear_list);
cb88a1b8 1444 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
1da177e4 1445 switch (hdev->descriptor.bDeviceProtocol) {
7bf01185
AD
1446 case USB_HUB_PR_FS:
1447 break;
1448 case USB_HUB_PR_HS_SINGLE_TT:
1449 dev_dbg(hub_dev, "Single TT\n");
1450 hub->tt.hub = hdev;
1451 break;
1452 case USB_HUB_PR_HS_MULTI_TT:
1453 ret = usb_set_interface(hdev, 0, 1);
1454 if (ret == 0) {
1455 dev_dbg(hub_dev, "TT per port\n");
1456 hub->tt.multi = 1;
1457 } else
1458 dev_err(hub_dev, "Using single TT (err %d)\n",
1459 ret);
1460 hub->tt.hub = hdev;
1461 break;
1462 case USB_HUB_PR_SS:
1463 /* USB 3.0 hubs don't have a TT */
1464 break;
1465 default:
1466 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1467 hdev->descriptor.bDeviceProtocol);
1468 break;
1da177e4
LT
1469 }
1470
e09711ae 1471 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
74ad9bd2 1472 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
e09711ae
DB
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);
1479 }
1da177e4 1480 break;
e09711ae
DB
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);
1da177e4 1486 break;
e09711ae
DB
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);
1da177e4 1492 break;
e09711ae
DB
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);
1da177e4
LT
1498 break;
1499 }
1500
1501 /* probe() zeroes hub->indicator[] */
74ad9bd2 1502 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
1da177e4
LT
1503 hub->has_indicators = 1;
1504 dev_dbg(hub_dev, "Port indicators are supported\n");
1505 }
1506
1507 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1508 hub->descriptor->bPwrOn2PwrGood * 2);
1509
1510 /* power budgeting mostly matters with bus-powered hubs,
1511 * and battery-powered root hubs (may provide just 8 mA).
1512 */
1513 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
55c52718 1514 if (ret < 2) {
1da177e4
LT
1515 message = "can't get hub status";
1516 goto fail;
1517 }
7d35b929
AS
1518 le16_to_cpus(&hubstatus);
1519 if (hdev == hdev->bus->root_hub) {
55c52718
AS
1520 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1521 hub->mA_per_port = 500;
1522 else {
1523 hub->mA_per_port = hdev->bus_mA;
1524 hub->limited_power = 1;
1525 }
7d35b929 1526 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
1da177e4
LT
1527 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1528 hub->descriptor->bHubContrCurrent);
55c52718
AS
1529 hub->limited_power = 1;
1530 if (hdev->maxchild > 0) {
1531 int remaining = hdev->bus_mA -
1532 hub->descriptor->bHubContrCurrent;
1533
1534 if (remaining < hdev->maxchild * 100)
1535 dev_warn(hub_dev,
1536 "insufficient power available "
1537 "to use all downstream ports\n");
1538 hub->mA_per_port = 100; /* 7.2.1.1 */
1539 }
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;
7d35b929 1544 }
55c52718
AS
1545 if (hub->mA_per_port < 500)
1546 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1547 hub->mA_per_port);
1da177e4 1548
b356b7c7
SS
1549 /* Update the HCD's internal representation of this hub before khubd
1550 * starts getting port status changes for devices under the hub.
1551 */
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);
1556 if (ret < 0) {
1557 message = "can't update HCD hub info";
1558 goto fail;
1559 }
1560 }
1561
1da177e4
LT
1562 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1563 if (ret < 0) {
1564 message = "can't get hub status";
1565 goto fail;
1566 }
1567
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");
1573
74ad9bd2 1574 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
1da177e4
LT
1575 dev_dbg(hub_dev, "%sover-current condition exists\n",
1576 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1577
88fafff9 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. */
1da177e4
LT
1584 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1585 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1586
1587 if (maxp > sizeof(*hub->buffer))
1588 maxp = sizeof(*hub->buffer);
1589
1590 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1591 if (!hub->urb) {
1da177e4
LT
1592 ret = -ENOMEM;
1593 goto fail;
1594 }
1595
1596 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1597 hub, endpoint->bInterval);
1da177e4
LT
1598
1599 /* maybe cycle the hub leds */
1600 if (hub->has_indicators && blinkenlights)
1601 hub->indicator [0] = INDICATOR_CYCLE;
1602
fa2a9566
LT
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);
1607
f2835219 1608 hub_activate(hub, HUB_INIT);
1da177e4
LT
1609 return 0;
1610
1611fail:
1612 dev_err (hub_dev, "config failed, %s (err %d)\n",
1613 message, ret);
1614 /* hub_disconnect() frees urb and descriptor */
1615 return ret;
1616}
1617
e8054854
AS
1618static void hub_release(struct kref *kref)
1619{
1620 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1621
1622 usb_put_intf(to_usb_interface(hub->intfdev));
1623 kfree(hub);
1624}
1625
1da177e4
LT
1626static unsigned highspeed_hubs;
1627
1628static void hub_disconnect(struct usb_interface *intf)
1629{
8816230e 1630 struct usb_hub *hub = usb_get_intfdata(intf);
fa286188 1631 struct usb_device *hdev = interface_to_usbdev(intf);
fa2a9566
LT
1632 int i;
1633
e8054854
AS
1634 /* Take the hub off the event list and don't let it be added again */
1635 spin_lock_irq(&hub_event_lock);
8e4ceb38
AS
1636 if (!list_empty(&hub->event_list)) {
1637 list_del_init(&hub->event_list);
1638 usb_autopm_put_interface_no_suspend(intf);
1639 }
e8054854
AS
1640 hub->disconnected = 1;
1641 spin_unlock_irq(&hub_event_lock);
1da177e4 1642
7de18d8b
AS
1643 /* Disconnect all children and quiesce the hub */
1644 hub->error = 0;
4330354f 1645 hub_quiesce(hub, HUB_DISCONNECT);
7de18d8b 1646
8b28c752 1647 usb_set_intfdata (intf, NULL);
1f2235b8
AS
1648
1649 for (i = 0; i < hdev->maxchild; i++)
1650 usb_hub_remove_port_device(hub, i + 1);
7cbe5dca 1651 hub->hdev->maxchild = 0;
1da177e4 1652
e8054854 1653 if (hub->hdev->speed == USB_SPEED_HIGH)
1da177e4
LT
1654 highspeed_hubs--;
1655
1da177e4 1656 usb_free_urb(hub->urb);
fa2a9566 1657 kfree(hub->ports);
1bc3c9e1 1658 kfree(hub->descriptor);
1bc3c9e1 1659 kfree(hub->status);
d697cdda 1660 kfree(hub->buffer);
1da177e4 1661
e8054854 1662 kref_put(&hub->kref, hub_release);
1da177e4
LT
1663}
1664
1665static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1666{
1667 struct usb_host_interface *desc;
1668 struct usb_endpoint_descriptor *endpoint;
1669 struct usb_device *hdev;
1670 struct usb_hub *hub;
1671
1672 desc = intf->cur_altsetting;
1673 hdev = interface_to_usbdev(intf);
1674
596d789a
ML
1675 /*
1676 * Set default autosuspend delay as 0 to speedup bus suspend,
1677 * based on the below considerations:
1678 *
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.
1683 *
1684 * - The patch might cause one or more auto supend/resume for
1685 * below very rare devices when they are plugged into hub
1686 * first time:
1687 *
1688 * devices having trouble initializing, and disconnect
1689 * themselves from the bus and then reconnect a second
1690 * or so later
1691 *
1692 * devices just for downloading firmware, and disconnects
1693 * themselves after completing it
1694 *
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
1698 * does care it.
1699 *
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.
1703 *
1704 * - Change autosuspend delay of hub can avoid unnecessary auto
1705 * suspend timer for hub, also may decrease power consumption
1706 * of USB bus.
1707 */
1708 pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
1709
2839f5bc
SS
1710 /* Hubs have proper suspend/resume support. */
1711 usb_enable_autosuspend(hdev);
088f7fec 1712
38f3ad5e 1713 if (hdev->level == MAX_TOPO_LEVEL) {
b9cef6c3
WF
1714 dev_err(&intf->dev,
1715 "Unsupported bus topology: hub nested too deep\n");
38f3ad5e
FB
1716 return -E2BIG;
1717 }
1718
89ccbdc9
DB
1719#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1720 if (hdev->parent) {
1721 dev_warn(&intf->dev, "ignoring external hub\n");
1722 return -ENODEV;
1723 }
1724#endif
1725
1da177e4
LT
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)) {
1730descriptor_error:
1731 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1732 return -EIO;
1733 }
1734
1735 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1736 if (desc->desc.bNumEndpoints != 1)
1737 goto descriptor_error;
1738
1739 endpoint = &desc->endpoint[0].desc;
1740
fbf81c29
LFC
1741 /* If it's not an interrupt in endpoint, we'd better punt! */
1742 if (!usb_endpoint_is_int_in(endpoint))
1da177e4
LT
1743 goto descriptor_error;
1744
1745 /* We found a hub */
1746 dev_info (&intf->dev, "USB hub found\n");
1747
0a1ef3b5 1748 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
1da177e4
LT
1749 if (!hub) {
1750 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1751 return -ENOMEM;
1752 }
1753
e8054854 1754 kref_init(&hub->kref);
1da177e4
LT
1755 INIT_LIST_HEAD(&hub->event_list);
1756 hub->intfdev = &intf->dev;
1757 hub->hdev = hdev;
c4028958 1758 INIT_DELAYED_WORK(&hub->leds, led_work);
8520f380 1759 INIT_DELAYED_WORK(&hub->init_work, NULL);
e8054854 1760 usb_get_intf(intf);
1da177e4
LT
1761
1762 usb_set_intfdata (intf, hub);
40f122f3 1763 intf->needs_remote_wakeup = 1;
1da177e4
LT
1764
1765 if (hdev->speed == USB_SPEED_HIGH)
1766 highspeed_hubs++;
1767
e6f30dea
ML
1768 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1769 hub->quirk_check_port_auto_suspend = 1;
1770
1da177e4
LT
1771 if (hub_configure(hub, endpoint) >= 0)
1772 return 0;
1773
1774 hub_disconnect (intf);
1775 return -ENODEV;
1776}
1777
1778static int
1779hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1780{
1781 struct usb_device *hdev = interface_to_usbdev (intf);
ff823c79 1782 struct usb_hub *hub = hdev_to_hub(hdev);
1da177e4
LT
1783
1784 /* assert ifno == 0 (part of hub spec) */
1785 switch (code) {
1786 case USBDEVFS_HUB_PORTINFO: {
1787 struct usbdevfs_hub_portinfo *info = user_data;
1788 int i;
1789
1790 spin_lock_irq(&device_state_lock);
1791 if (hdev->devnum <= 0)
1792 info->nports = 0;
1793 else {
1794 info->nports = hdev->maxchild;
1795 for (i = 0; i < info->nports; i++) {
ff823c79 1796 if (hub->ports[i]->child == NULL)
1da177e4
LT
1797 info->port[i] = 0;
1798 else
1799 info->port[i] =
ff823c79 1800 hub->ports[i]->child->devnum;
1da177e4
LT
1801 }
1802 }
1803 spin_unlock_irq(&device_state_lock);
1804
1805 return info->nports + 1;
1806 }
1807
1808 default:
1809 return -ENOSYS;
1810 }
1811}
1812
7cbe5dca
AS
1813/*
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.
1816 */
1817static int find_port_owner(struct usb_device *hdev, unsigned port1,
336c5c31 1818 struct dev_state ***ppowner)
7cbe5dca
AS
1819{
1820 if (hdev->state == USB_STATE_NOTATTACHED)
1821 return -ENODEV;
1822 if (port1 == 0 || port1 > hdev->maxchild)
1823 return -EINVAL;
1824
1825 /* This assumes that devices not managed by the hub driver
1826 * will always have maxchild equal to 0.
1827 */
fa2a9566 1828 *ppowner = &(hdev_to_hub(hdev)->ports[port1 - 1]->port_owner);
7cbe5dca
AS
1829 return 0;
1830}
1831
1832/* In the following three functions, the caller must hold hdev's lock */
336c5c31
LT
1833int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1834 struct dev_state *owner)
7cbe5dca
AS
1835{
1836 int rc;
336c5c31 1837 struct dev_state **powner;
7cbe5dca
AS
1838
1839 rc = find_port_owner(hdev, port1, &powner);
1840 if (rc)
1841 return rc;
1842 if (*powner)
1843 return -EBUSY;
1844 *powner = owner;
1845 return rc;
1846}
1847
336c5c31
LT
1848int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1849 struct dev_state *owner)
7cbe5dca
AS
1850{
1851 int rc;
336c5c31 1852 struct dev_state **powner;
7cbe5dca
AS
1853
1854 rc = find_port_owner(hdev, port1, &powner);
1855 if (rc)
1856 return rc;
1857 if (*powner != owner)
1858 return -ENOENT;
1859 *powner = NULL;
1860 return rc;
1861}
1862
336c5c31 1863void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
7cbe5dca 1864{
fa2a9566 1865 struct usb_hub *hub = hdev_to_hub(hdev);
7cbe5dca 1866 int n;
7cbe5dca 1867
fa2a9566
LT
1868 for (n = 0; n < hdev->maxchild; n++) {
1869 if (hub->ports[n]->port_owner == owner)
1870 hub->ports[n]->port_owner = NULL;
7cbe5dca 1871 }
fa2a9566 1872
7cbe5dca
AS
1873}
1874
1875/* The caller must hold udev's lock */
1876bool usb_device_is_owned(struct usb_device *udev)
1877{
1878 struct usb_hub *hub;
1879
1880 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1881 return false;
1882 hub = hdev_to_hub(udev->parent);
fa2a9566 1883 return !!hub->ports[udev->portnum - 1]->port_owner;
7cbe5dca
AS
1884}
1885
1da177e4
LT
1886static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1887{
ff823c79 1888 struct usb_hub *hub = hdev_to_hub(udev);
1da177e4
LT
1889 int i;
1890
1891 for (i = 0; i < udev->maxchild; ++i) {
ff823c79
LT
1892 if (hub->ports[i]->child)
1893 recursively_mark_NOTATTACHED(hub->ports[i]->child);
1da177e4 1894 }
9bbdf1e0 1895 if (udev->state == USB_STATE_SUSPENDED)
15123006 1896 udev->active_duration -= jiffies;
1da177e4
LT
1897 udev->state = USB_STATE_NOTATTACHED;
1898}
1899
1900/**
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
1904 *
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.
1912 *
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.
1915 *
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.
1920 */
1921void usb_set_device_state(struct usb_device *udev,
1922 enum usb_device_state new_state)
1923{
1924 unsigned long flags;
4681b171 1925 int wakeup = -1;
1da177e4
LT
1926
1927 spin_lock_irqsave(&device_state_lock, flags);
1928 if (udev->state == USB_STATE_NOTATTACHED)
1929 ; /* do nothing */
b94dc6b5 1930 else if (new_state != USB_STATE_NOTATTACHED) {
fb669cc0
DB
1931
1932 /* root hub wakeup capabilities are managed out-of-band
1933 * and may involve silicon errata ... ignore them here.
1934 */
1935 if (udev->parent) {
645daaab
AS
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)
4681b171
RW
1940 wakeup = udev->actconfig->desc.bmAttributes
1941 & USB_CONFIG_ATT_WAKEUP;
645daaab 1942 else
4681b171 1943 wakeup = 0;
fb669cc0 1944 }
15123006
SS
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;
645daaab 1951 udev->state = new_state;
b94dc6b5 1952 } else
1da177e4
LT
1953 recursively_mark_NOTATTACHED(udev);
1954 spin_unlock_irqrestore(&device_state_lock, flags);
4681b171
RW
1955 if (wakeup >= 0)
1956 device_set_wakeup_capable(&udev->dev, wakeup);
1da177e4 1957}
6da9c990 1958EXPORT_SYMBOL_GPL(usb_set_device_state);
1da177e4 1959
8af548dc 1960/*
3b29b68b
AS
1961 * Choose a device number.
1962 *
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.
1967 *
8af548dc
IPG
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
1976 * [X | 0x80].
1977 *
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.
c6515272
SS
1983 *
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.
8af548dc 1988 */
3b29b68b 1989static void choose_devnum(struct usb_device *udev)
1da177e4
LT
1990{
1991 int devnum;
1992 struct usb_bus *bus = udev->bus;
1993
1994 /* If khubd ever becomes multithreaded, this will need a lock */
8af548dc
IPG
1995 if (udev->wusb) {
1996 devnum = udev->portnum + 1;
1997 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1998 } else {
1999 /* Try to allocate the next devnum beginning at
2000 * bus->devnum_next. */
2001 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
2002 bus->devnum_next);
2003 if (devnum >= 128)
2004 devnum = find_next_zero_bit(bus->devmap.devicemap,
2005 128, 1);
2006 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
2007 }
1da177e4
LT
2008 if (devnum < 128) {
2009 set_bit(devnum, bus->devmap.devicemap);
2010 udev->devnum = devnum;
2011 }
2012}
2013
3b29b68b 2014static void release_devnum(struct usb_device *udev)
1da177e4
LT
2015{
2016 if (udev->devnum > 0) {
2017 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
2018 udev->devnum = -1;
2019 }
2020}
2021
3b29b68b 2022static void update_devnum(struct usb_device *udev, int devnum)
4953d141
DV
2023{
2024 /* The address for a WUSB device is managed by wusbcore. */
2025 if (!udev->wusb)
2026 udev->devnum = devnum;
2027}
2028
f7410ced
HX
2029static void hub_free_dev(struct usb_device *udev)
2030{
2031 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2032
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);
2036}
2037
1da177e4
LT
2038/**
2039 * usb_disconnect - disconnect a device (usbcore-internal)
2040 * @pdev: pointer to device being disconnected
2041 * Context: !in_interrupt ()
2042 *
2043 * Something got disconnected. Get rid of it and all of its children.
2044 *
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.
2048 *
2049 * Only hub drivers (including virtual root hub drivers for host
2050 * controllers) should ever call this.
2051 *
2052 * This call is synchronous, and may not be used in an interrupt context.
2053 */
2054void usb_disconnect(struct usb_device **pdev)
2055{
2056 struct usb_device *udev = *pdev;
ff823c79 2057 struct usb_hub *hub = hdev_to_hub(udev);
1da177e4
LT
2058 int i;
2059
1da177e4
LT
2060 /* mark the device as inactive, so any further urb submissions for
2061 * this device (and any of its children) will fail immediately.
25985edc 2062 * this quiesces everything except pending urbs.
1da177e4
LT
2063 */
2064 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
3b29b68b
AS
2065 dev_info(&udev->dev, "USB disconnect, device number %d\n",
2066 udev->devnum);
1da177e4 2067
9ad3d6cc
AS
2068 usb_lock_device(udev);
2069
1da177e4 2070 /* Free up all the children before we remove this device */
8816230e 2071 for (i = 0; i < udev->maxchild; i++) {
ff823c79
LT
2072 if (hub->ports[i]->child)
2073 usb_disconnect(&hub->ports[i]->child);
1da177e4
LT
2074 }
2075
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.
2079 */
782da727 2080 dev_dbg (&udev->dev, "unregistering device\n");
1da177e4 2081 usb_disable_device(udev, 0);
cde217a5 2082 usb_hcd_synchronize_unlinks(udev);
1da177e4 2083
3b23dd6f 2084 usb_remove_ep_devs(&udev->ep0);
782da727
AS
2085 usb_unlock_device(udev);
2086
2087 /* Unregister the device. The device driver is responsible
3b23dd6f
AS
2088 * for de-configuring the device and invoking the remove-device
2089 * notifier chain (used by usbfs and possibly others).
782da727
AS
2090 */
2091 device_del(&udev->dev);
3099e75a 2092
782da727 2093 /* Free the device number and delete the parent's children[]
1da177e4
LT
2094 * (or root_hub) pointer.
2095 */
3b29b68b 2096 release_devnum(udev);
1da177e4
LT
2097
2098 /* Avoid races with recursively_mark_NOTATTACHED() */
2099 spin_lock_irq(&device_state_lock);
2100 *pdev = NULL;
2101 spin_unlock_irq(&device_state_lock);
2102
f7410ced
HX
2103 hub_free_dev(udev);
2104
782da727 2105 put_device(&udev->dev);
1da177e4
LT
2106}
2107
f2a383e4 2108#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
1da177e4
LT
2109static void show_string(struct usb_device *udev, char *id, char *string)
2110{
2111 if (!string)
2112 return;
f2ec522e 2113 dev_info(&udev->dev, "%s: %s\n", id, string);
1da177e4
LT
2114}
2115
f2a383e4
GKH
2116static void announce_device(struct usb_device *udev)
2117{
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));
b9cef6c3
WF
2121 dev_info(&udev->dev,
2122 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
f2a383e4
GKH
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);
2129}
1da177e4 2130#else
f2a383e4 2131static inline void announce_device(struct usb_device *udev) { }
1da177e4
LT
2132#endif
2133
1da177e4
LT
2134#ifdef CONFIG_USB_OTG
2135#include "otg_whitelist.h"
2136#endif
2137
3ede760f 2138/**
8d8558d1 2139 * usb_enumerate_device_otg - FIXME (usbcore-internal)
3ede760f
ON
2140 * @udev: newly addressed device (in ADDRESS state)
2141 *
8d8558d1 2142 * Finish enumeration for On-The-Go devices
3ede760f 2143 */
8d8558d1 2144static int usb_enumerate_device_otg(struct usb_device *udev)
1da177e4 2145{
d9d16e8a 2146 int err = 0;
1da177e4
LT
2147
2148#ifdef CONFIG_USB_OTG
2149 /*
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.
2153 */
2154 if (!udev->bus->is_b_host
2155 && udev->config
2156 && udev->parent == udev->bus->root_hub) {
2eb5052e 2157 struct usb_otg_descriptor *desc = NULL;
1da177e4
LT
2158 struct usb_bus *bus = udev->bus;
2159
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) {
12c3da34 2165 unsigned port1 = udev->portnum;
fc849b85 2166
1da177e4
LT
2167 dev_info(&udev->dev,
2168 "Dual-Role OTG device on %sHNP port\n",
2169 (port1 == bus->otg_port)
2170 ? "" : "non-");
2171
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,
2178 bus->b_hnp_enable
2179 ? USB_DEVICE_B_HNP_ENABLE
2180 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2181 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2182 if (err < 0) {
2183 /* OTG MESSAGE: report errors here,
2184 * customize to match your product.
2185 */
2186 dev_info(&udev->dev,
b9cef6c3 2187 "can't set HNP mode: %d\n",
1da177e4
LT
2188 err);
2189 bus->b_hnp_enable = 0;
2190 }
2191 }
2192 }
2193 }
2194
2195 if (!is_targeted(udev)) {
2196
2197 /* Maybe it can talk to us, though we can't talk to it.
2198 * (Includes HNP test device.)
2199 */
2200 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
634a84f8 2201 err = usb_port_suspend(udev, PMSG_SUSPEND);
1da177e4
LT
2202 if (err < 0)
2203 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2204 }
ffcdc18d 2205 err = -ENOTSUPP;
1da177e4
LT
2206 goto fail;
2207 }
d9d16e8a 2208fail:
1da177e4 2209#endif
d9d16e8a
IPG
2210 return err;
2211}
2212
2213
2214/**
8d8558d1 2215 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
d9d16e8a
IPG
2216 * @udev: newly addressed device (in ADDRESS state)
2217 *
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
2220 * environment.
2221 *
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.
2225 */
8d8558d1 2226static int usb_enumerate_device(struct usb_device *udev)
d9d16e8a
IPG
2227{
2228 int err;
1da177e4 2229
d9d16e8a
IPG
2230 if (udev->config == NULL) {
2231 err = usb_get_configuration(udev);
2232 if (err < 0) {
2233 dev_err(&udev->dev, "can't read configurations, error %d\n",
2234 err);
80da2e0d 2235 return err;
d9d16e8a
IPG
2236 }
2237 }
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);
2242 }
2243 else {
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);
2249 }
8d8558d1 2250 err = usb_enumerate_device_otg(udev);
80da2e0d
LP
2251 if (err < 0)
2252 return err;
2253
2254 usb_detect_interface_quirks(udev);
2255
2256 return 0;
d9d16e8a
IPG
2257}
2258
d35e70d5
MG
2259static void set_usb_port_removable(struct usb_device *udev)
2260{
2261 struct usb_device *hdev = udev->parent;
2262 struct usb_hub *hub;
2263 u8 port = udev->portnum;
2264 u16 wHubCharacteristics;
2265 bool removable = true;
2266
2267 if (!hdev)
2268 return;
2269
2270 hub = hdev_to_hub(udev->parent);
2271
2272 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2273
2274 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2275 return;
2276
2277 if (hub_is_superspeed(hdev)) {
ca3c1539
LT
2278 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2279 & (1 << port))
d35e70d5
MG
2280 removable = false;
2281 } else {
2282 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2283 removable = false;
2284 }
2285
2286 if (removable)
2287 udev->removable = USB_DEVICE_REMOVABLE;
2288 else
2289 udev->removable = USB_DEVICE_FIXED;
2290}
d9d16e8a
IPG
2291
2292/**
2293 * usb_new_device - perform initial device setup (usbcore-internal)
2294 * @udev: newly addressed device (in ADDRESS state)
2295 *
8d8558d1
AS
2296 * This is called with devices which have been detected but not fully
2297 * enumerated. The device descriptor is available, but not descriptors
d9d16e8a
IPG
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.
2303 *
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
2306 * errno value.
2307 *
2308 * This call is synchronous, and may not be used in an interrupt context.
2309 *
2310 * Only the hub driver or root-hub registrar should ever call this.
2311 */
2312int usb_new_device(struct usb_device *udev)
2313{
2314 int err;
2315
16985408 2316 if (udev->parent) {
16985408
DS
2317 /* Initialize non-root-hub device wakeup to disabled;
2318 * device (un)configuration controls wakeup capable
2319 * sysfs power/wakeup controls wakeup enabled/disabled
2320 */
2321 device_init_wakeup(&udev->dev, 0);
16985408
DS
2322 }
2323
9bbdf1e0
AS
2324 /* Tell the runtime-PM framework the device is active */
2325 pm_runtime_set_active(&udev->dev);
c08512c7 2326 pm_runtime_get_noresume(&udev->dev);
fcc4a01e 2327 pm_runtime_use_autosuspend(&udev->dev);
9bbdf1e0
AS
2328 pm_runtime_enable(&udev->dev);
2329
c08512c7
AS
2330 /* By default, forbid autosuspend for all devices. It will be
2331 * allowed for hubs during binding.
2332 */
2333 usb_disable_autosuspend(udev);
2334
8d8558d1 2335 err = usb_enumerate_device(udev); /* Read descriptors */
d9d16e8a
IPG
2336 if (err < 0)
2337 goto fail;
c6515272
SS
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)));
9f8b17e6
KS
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)));
2344
6cd13201
AS
2345 /* Tell the world! */
2346 announce_device(udev);
195af2cc 2347
b04b3156
TT
2348 if (udev->serial)
2349 add_device_randomness(udev->serial, strlen(udev->serial));
2350 if (udev->product)
2351 add_device_randomness(udev->product, strlen(udev->product));
2352 if (udev->manufacturer)
2353 add_device_randomness(udev->manufacturer,
2354 strlen(udev->manufacturer));
2355
927bc916 2356 device_enable_async_suspend(&udev->dev);
d35e70d5
MG
2357
2358 /*
2359 * check whether the hub marks this port as non-removable. Do it
2360 * now so that platform-specific data can override it in
2361 * device_add()
2362 */
2363 if (udev->parent)
2364 set_usb_port_removable(udev);
2365
782da727 2366 /* Register the device. The device driver is responsible
3b23dd6f
AS
2367 * for configuring the device and invoking the add-device
2368 * notifier chain (used by usbfs and possibly others).
782da727 2369 */
9f8b17e6 2370 err = device_add(&udev->dev);
1da177e4
LT
2371 if (err) {
2372 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2373 goto fail;
2374 }
9ad3d6cc 2375
3b23dd6f 2376 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
c08512c7
AS
2377 usb_mark_last_busy(udev);
2378 pm_runtime_put_sync_autosuspend(&udev->dev);
c066475e 2379 return err;
1da177e4
LT
2380
2381fail:
2382 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
9bbdf1e0
AS
2383 pm_runtime_disable(&udev->dev);
2384 pm_runtime_set_suspended(&udev->dev);
d9d16e8a 2385 return err;
1da177e4
LT
2386}
2387
93993a0a
IPG
2388
2389/**
fd39c86b
RD
2390 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2391 * @usb_dev: USB device
2392 *
2393 * Move the USB device to a very basic state where interfaces are disabled
2394 * and the device is in fact unconfigured and unusable.
93993a0a
IPG
2395 *
2396 * We share a lock (that we have) with device_del(), so we need to
2397 * defer its call.
2398 */
2399int usb_deauthorize_device(struct usb_device *usb_dev)
2400{
93993a0a
IPG
2401 usb_lock_device(usb_dev);
2402 if (usb_dev->authorized == 0)
2403 goto out_unauthorized;
da307123 2404
93993a0a
IPG
2405 usb_dev->authorized = 0;
2406 usb_set_configuration(usb_dev, -1);
da307123
AS
2407
2408 kfree(usb_dev->product);
93993a0a 2409 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
da307123 2410 kfree(usb_dev->manufacturer);
93993a0a 2411 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
da307123 2412 kfree(usb_dev->serial);
93993a0a 2413 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
da307123
AS
2414
2415 usb_destroy_configuration(usb_dev);
93993a0a 2416 usb_dev->descriptor.bNumConfigurations = 0;
da307123 2417
93993a0a
IPG
2418out_unauthorized:
2419 usb_unlock_device(usb_dev);
2420 return 0;
2421}
2422
2423
2424int usb_authorize_device(struct usb_device *usb_dev)
2425{
2426 int result = 0, c;
da307123 2427
93993a0a
IPG
2428 usb_lock_device(usb_dev);
2429 if (usb_dev->authorized == 1)
2430 goto out_authorized;
da307123 2431
93993a0a
IPG
2432 result = usb_autoresume_device(usb_dev);
2433 if (result < 0) {
2434 dev_err(&usb_dev->dev,
2435 "can't autoresume for authorization: %d\n", result);
2436 goto error_autoresume;
2437 }
2438 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2439 if (result < 0) {
2440 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2441 "authorization: %d\n", result);
2442 goto error_device_descriptor;
2443 }
da307123
AS
2444
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;
2451
93993a0a 2452 usb_dev->authorized = 1;
8d8558d1 2453 result = usb_enumerate_device(usb_dev);
93993a0a 2454 if (result < 0)
8d8558d1 2455 goto error_enumerate;
93993a0a
IPG
2456 /* Choose and set the configuration. This registers the interfaces
2457 * with the driver core and lets interface drivers bind to them.
2458 */
b5ea060f 2459 c = usb_choose_configuration(usb_dev);
93993a0a
IPG
2460 if (c >= 0) {
2461 result = usb_set_configuration(usb_dev, c);
2462 if (result) {
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. */
2467 }
2468 }
2469 dev_info(&usb_dev->dev, "authorized to connect\n");
da307123 2470
8d8558d1 2471error_enumerate:
93993a0a 2472error_device_descriptor:
da307123 2473 usb_autosuspend_device(usb_dev);
93993a0a
IPG
2474error_autoresume:
2475out_authorized:
2476 usb_unlock_device(usb_dev); // complements locktree
2477 return result;
2478}
2479
2480
0165de09
IPG
2481/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2482static unsigned hub_is_wusb(struct usb_hub *hub)
2483{
2484 struct usb_hcd *hcd;
2485 if (hub->hdev->parent != NULL) /* not a root hub? */
2486 return 0;
2487 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2488 return hcd->wireless;
2489}
2490
2491
1da177e4
LT
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))
90ab5ee9 2496#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
1da177e4
LT
2497
2498#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2499#define HUB_SHORT_RESET_TIME 10
75d7cf72 2500#define HUB_BH_RESET_TIME 50
1da177e4 2501#define HUB_LONG_RESET_TIME 200
77c7f072 2502#define HUB_RESET_TIMEOUT 800
1da177e4 2503
10d674a8
SS
2504static int hub_port_reset(struct usb_hub *hub, int port1,
2505 struct usb_device *udev, unsigned int delay, bool warm);
2506
8bea2bd3
SL
2507/* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2508 * Port worm reset is required to recover
2509 */
2510static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
10d674a8
SS
2511{
2512 return hub_is_superspeed(hub->hdev) &&
8bea2bd3
SL
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)) ;
10d674a8
SS
2517}
2518
1da177e4 2519static int hub_port_wait_reset(struct usb_hub *hub, int port1,
75d7cf72 2520 struct usb_device *udev, unsigned int delay, bool warm)
1da177e4
LT
2521{
2522 int delay_time, ret;
2523 u16 portstatus;
2524 u16 portchange;
2525
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 */
2530 msleep(delay);
2531
2532 /* read and decode port status */
2533 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2534 if (ret < 0)
2535 return ret;
2536
4f43447e
SS
2537 /* The port state is unknown until the reset completes. */
2538 if ((portstatus & USB_PORT_STAT_RESET))
2539 goto delay;
2540
75d7cf72
AX
2541 /*
2542 * Some buggy devices require a warm reset to be issued even
2543 * when the port appears not to be connected.
2544 */
2545 if (!warm) {
10d674a8
SS
2546 /*
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
2552 * the device work.
2553 *
2554 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2555 */
8bea2bd3 2556 if (hub_port_warm_reset_required(hub, portstatus)) {
10d674a8
SS
2557 int ret;
2558
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",
2569 port1);
2570 ret = hub_port_reset(hub, port1,
2571 udev, HUB_BH_RESET_TIME,
2572 true);
2573 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2574 clear_port_feature(hub->hdev, port1,
2575 USB_PORT_FEAT_C_CONNECTION);
2576 return ret;
2577 }
75d7cf72
AX
2578 /* Device went away? */
2579 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2580 return -ENOTCONN;
2581
2582 /* bomb out completely if the connection bounced */
2583 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2584 return -ENOTCONN;
2585
4f43447e 2586 if ((portstatus & USB_PORT_STAT_ENABLE)) {
75d7cf72
AX
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;
2595 else
2596 udev->speed = USB_SPEED_FULL;
2597 return 0;
2598 }
2599 } else {
65bdac5e
SS
2600 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
2601 hub_port_warm_reset_required(hub,
2602 portstatus))
2603 return -ENOTCONN;
2604
4f43447e 2605 return 0;
1da177e4
LT
2606 }
2607
4f43447e 2608delay:
1da177e4
LT
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;
2612
2613 dev_dbg (hub->intfdev,
75d7cf72
AX
2614 "port %d not %sreset yet, waiting %dms\n",
2615 port1, warm ? "warm " : "", delay);
1da177e4
LT
2616 }
2617
2618 return -EBUSY;
2619}
2620
75d7cf72
AX
2621static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2622 struct usb_device *udev, int *status, bool warm)
2623{
2624 switch (*status) {
2625 case 0:
2626 if (!warm) {
2627 struct usb_hcd *hcd;
2628 /* TRSTRCY = 10 ms; plus some extra */
2629 msleep(10 + 40);
2630 update_devnum(udev, 0);
2631 hcd = bus_to_hcd(udev->bus);
8b8132bc
SS
2632 /* The xHC may think the device is already reset,
2633 * so ignore the status.
2634 */
2635 if (hcd->driver->reset_device)
2636 hcd->driver->reset_device(hcd, udev);
75d7cf72
AX
2637 }
2638 /* FALL THROUGH */
2639 case -ENOTCONN:
2640 case -ENODEV:
2641 clear_port_feature(hub->hdev,
2642 port1, USB_PORT_FEAT_C_RESET);
2643 /* FIXME need disconnect() for NOTATTACHED device */
1c7439c6 2644 if (hub_is_superspeed(hub->hdev)) {
75d7cf72
AX
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);
1c7439c6
SS
2649 }
2650 if (!warm)
75d7cf72
AX
2651 usb_set_device_state(udev, *status
2652 ? USB_STATE_NOTATTACHED
2653 : USB_STATE_DEFAULT);
75d7cf72
AX
2654 break;
2655 }
2656}
2657
2658/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
1da177e4 2659static int hub_port_reset(struct usb_hub *hub, int port1,
75d7cf72 2660 struct usb_device *udev, unsigned int delay, bool warm)
1da177e4
LT
2661{
2662 int i, status;
2663
75d7cf72
AX
2664 if (!warm) {
2665 /* Block EHCI CF initialization during the port reset.
2666 * Some companion controllers don't like it when they mix.
2667 */
2668 down_read(&ehci_cf_port_reset_rwsem);
2669 } else {
2670 if (!hub_is_superspeed(hub->hdev)) {
2671 dev_err(hub->intfdev, "only USB3 hub support "
2672 "warm reset\n");
2673 return -EINVAL;
2674 }
2675 }
32fe0198 2676
1da177e4
LT
2677 /* Reset the port */
2678 for (i = 0; i < PORT_RESET_TRIES; i++) {
75d7cf72
AX
2679 status = set_port_feature(hub->hdev, port1, (warm ?
2680 USB_PORT_FEAT_BH_PORT_RESET :
2681 USB_PORT_FEAT_RESET));
2682 if (status) {
1da177e4 2683 dev_err(hub->intfdev,
75d7cf72
AX
2684 "cannot %sreset port %d (err = %d)\n",
2685 warm ? "warm " : "", port1, status);
2686 } else {
2687 status = hub_port_wait_reset(hub, port1, udev, delay,
2688 warm);
dd16525b 2689 if (status && status != -ENOTCONN)
1da177e4
LT
2690 dev_dbg(hub->intfdev,
2691 "port_wait_reset: err = %d\n",
2692 status);
2693 }
2694
2695 /* return on disconnect or reset */
75d7cf72
AX
2696 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2697 hub_port_finish_reset(hub, port1, udev, &status, warm);
32fe0198 2698 goto done;
1da177e4
LT
2699 }
2700
2701 dev_dbg (hub->intfdev,
75d7cf72
AX
2702 "port %d not enabled, trying %sreset again...\n",
2703 port1, warm ? "warm " : "");
1da177e4
LT
2704 delay = HUB_LONG_RESET_TIME;
2705 }
2706
2707 dev_err (hub->intfdev,
2708 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2709 port1);
2710
75d7cf72
AX
2711done:
2712 if (!warm)
2713 up_read(&ehci_cf_port_reset_rwsem);
5e467f6e 2714
75d7cf72 2715 return status;
5e467f6e
AX
2716}
2717
0ed9a57e
AX
2718/* Check if a port is power on */
2719static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2720{
2721 int ret = 0;
2722
2723 if (hub_is_superspeed(hub->hdev)) {
2724 if (portstatus & USB_SS_PORT_STAT_POWER)
2725 ret = 1;
2726 } else {
2727 if (portstatus & USB_PORT_STAT_POWER)
2728 ret = 1;
2729 }
2730
2731 return ret;
2732}
2733
d388dab7 2734#ifdef CONFIG_PM
1da177e4 2735
0ed9a57e
AX
2736/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2737static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2738{
2739 int ret = 0;
2740
2741 if (hub_is_superspeed(hub->hdev)) {
2742 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2743 == USB_SS_PORT_LS_U3)
2744 ret = 1;
2745 } else {
2746 if (portstatus & USB_PORT_STAT_SUSPEND)
2747 ret = 1;
2748 }
2749
2750 return ret;
2751}
b01b03f3
AS
2752
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.
2755 */
2756static int check_port_resume_type(struct usb_device *udev,
2757 struct usb_hub *hub, int port1,
2758 int status, unsigned portchange, unsigned portstatus)
2759{
2760 /* Is the device still present? */
0ed9a57e
AX
2761 if (status || port_is_suspended(hub, portstatus) ||
2762 !port_is_power_on(hub, portstatus) ||
2763 !(portstatus & USB_PORT_STAT_CONNECTION)) {
b01b03f3
AS
2764 if (status >= 0)
2765 status = -ENODEV;
2766 }
2767
86c57edf
AS
2768 /* Can't do a normal resume if the port isn't enabled,
2769 * so try a reset-resume instead.
2770 */
2771 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2772 if (udev->persist_enabled)
2773 udev->reset_resume = 1;
2774 else
2775 status = -ENODEV;
2776 }
b01b03f3
AS
2777
2778 if (status) {
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) {
2783
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);
2791 }
2792
2793 return status;
2794}
2795
f74631e3
SS
2796int usb_disable_ltm(struct usb_device *udev)
2797{
2798 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2799
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))
2803 return 0;
2804
2805 /* Clear Feature LTM Enable can only be sent if the device is
2806 * configured.
2807 */
2808 if (!udev->actconfig)
2809 return 0;
2810
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);
2815}
2816EXPORT_SYMBOL_GPL(usb_disable_ltm);
2817
2818void usb_enable_ltm(struct usb_device *udev)
2819{
2820 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2821
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))
2825 return;
2826
2827 /* Set Feature LTM Enable can only be sent if the device is
2828 * configured.
2829 */
2830 if (!udev->actconfig)
2831 return;
2832
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);
2837}
2838EXPORT_SYMBOL_GPL(usb_enable_ltm);
2839
1da177e4
LT
2840#ifdef CONFIG_USB_SUSPEND
2841
2842/*
624d6c07
AS
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
2846 *
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.
2852 *
2853 * This only affects the USB hardware for a device; its interfaces
2854 * (and, for hubs, child devices) must already have been suspended.
2855 *
1da177e4
LT
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.
2859 *
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.
624d6c07
AS
2864 *
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.
2868 *
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:
2872 *
2873 * - suspend, resume ... when the VBUS power link stays live
2874 * - suspend, disconnect ... VBUS lost
2875 *
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.
2881 *
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.
2885 *
2886 * Returns 0 on success, else negative errno.
1da177e4 2887 */
65bfd296 2888int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
1da177e4 2889{
624d6c07
AS
2890 struct usb_hub *hub = hdev_to_hub(udev->parent);
2891 int port1 = udev->portnum;
2892 int status;
1da177e4 2893
1da177e4
LT
2894 /* enable remote wakeup when appropriate; this lets the device
2895 * wake up the upstream hub (including maybe the root hub).
2896 *
2897 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2898 * we don't explicitly enable it here.
2899 */
645daaab 2900 if (udev->do_remote_wakeup) {
623bef9e
SS
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,
2905 NULL, 0,
2906 USB_CTRL_SET_TIMEOUT);
2907 } else {
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.
2912 */
2913 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2914 USB_REQ_SET_FEATURE,
2915 USB_RECIP_INTERFACE,
2916 USB_INTRF_FUNC_SUSPEND,
3b9b6acd
SS
2917 USB_INTRF_FUNC_SUSPEND_RW |
2918 USB_INTRF_FUNC_SUSPEND_LP,
623bef9e
SS
2919 NULL, 0,
2920 USB_CTRL_SET_TIMEOUT);
2921 }
0c487206 2922 if (status) {
624d6c07
AS
2923 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2924 status);
0c487206 2925 /* bail if autosuspend is requested */
5b1b0b81 2926 if (PMSG_IS_AUTO(msg))
0c487206
ON
2927 return status;
2928 }
1da177e4
LT
2929 }
2930
65580b43
AX
2931 /* disable USB2 hardware LPM */
2932 if (udev->usb2_hw_lpm_enabled == 1)
2933 usb_set_usb2_hardware_lpm(udev, 0);
2934
f74631e3
SS
2935 if (usb_disable_ltm(udev)) {
2936 dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2937 __func__);
2938 return -ENOMEM;
2939 }
8306095f
SS
2940 if (usb_unlocked_disable_lpm(udev)) {
2941 dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
2942 __func__);
2943 return -ENOMEM;
2944 }
2945
1da177e4 2946 /* see 7.1.7.6 */
a7114230
AX
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);
a8f08d86
AX
2951 else
2952 status = set_port_feature(hub->hdev, port1,
2953 USB_PORT_FEAT_SUSPEND);
1da177e4 2954 if (status) {
624d6c07
AS
2955 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2956 port1, status);
1da177e4 2957 /* paranoia: "should not happen" */
0c487206
ON
2958 if (udev->do_remote_wakeup)
2959 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1da177e4
LT
2960 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2961 USB_DEVICE_REMOTE_WAKEUP, 0,
2962 NULL, 0,
2963 USB_CTRL_SET_TIMEOUT);
cbb33004 2964
c3e751e4
AX
2965 /* Try to enable USB2 hardware LPM again */
2966 if (udev->usb2_hw_lpm_capable == 1)
2967 usb_set_usb2_hardware_lpm(udev, 1);
2968
f74631e3
SS
2969 /* Try to enable USB3 LTM and LPM again */
2970 usb_enable_ltm(udev);
8306095f
SS
2971 usb_unlocked_enable_lpm(udev);
2972
cbb33004 2973 /* System sleep transitions should never fail */
5b1b0b81 2974 if (!PMSG_IS_AUTO(msg))
cbb33004 2975 status = 0;
1da177e4
LT
2976 } else {
2977 /* device has up to 10 msec to fully suspend */
30b1a7a3
AS
2978 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2979 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2980 udev->do_remote_wakeup);
1da177e4 2981 usb_set_device_state(udev, USB_STATE_SUSPENDED);
bfd1e910 2982 udev->port_is_suspended = 1;
1da177e4
LT
2983 msleep(10);
2984 }
c08512c7 2985 usb_mark_last_busy(hub->hdev);
1da177e4
LT
2986 return status;
2987}
2988
1da177e4 2989/*
390a8c34
DB
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
54515fe5 2992 * special "resume" signaling. This routine kicks in shortly after
1da177e4
LT
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.
54515fe5
AS
2996 *
2997 * If @udev->reset_resume is set then the device is reset before the
2998 * status check is done.
1da177e4 2999 */
140d8f68 3000static int finish_port_resume(struct usb_device *udev)
1da177e4 3001{
54515fe5 3002 int status = 0;
1da177e4
LT
3003 u16 devstatus;
3004
3005 /* caller owns the udev device lock */
b9cef6c3
WF
3006 dev_dbg(&udev->dev, "%s\n",
3007 udev->reset_resume ? "finish reset-resume" : "finish resume");
1da177e4
LT
3008
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.
3013 */
3014 usb_set_device_state(udev, udev->actconfig
3015 ? USB_STATE_CONFIGURED
3016 : USB_STATE_ADDRESS);
1da177e4 3017
54515fe5
AS
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
3021 * resumed.
3022 */
3023 if (udev->reset_resume)
86c57edf 3024 retry_reset_resume:
742120c6 3025 status = usb_reset_and_verify_device(udev);
54515fe5 3026
1da177e4
LT
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.
3030 */
54515fe5 3031 if (status == 0) {
46dede46 3032 devstatus = 0;
54515fe5
AS
3033 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
3034 if (status >= 0)
46dede46 3035 status = (status > 0 ? 0 : -ENODEV);
86c57edf
AS
3036
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;
3042 }
54515fe5 3043 }
b40b7a90 3044
624d6c07
AS
3045 if (status) {
3046 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
3047 status);
3048 } else if (udev->actconfig) {
1da177e4 3049 le16_to_cpus(&devstatus);
686314cf 3050 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
1da177e4
LT
3051 status = usb_control_msg(udev,
3052 usb_sndctrlpipe(udev, 0),
3053 USB_REQ_CLEAR_FEATURE,
3054 USB_RECIP_DEVICE,
3055 USB_DEVICE_REMOTE_WAKEUP, 0,
3056 NULL, 0,
3057 USB_CTRL_SET_TIMEOUT);
a8e7c565 3058 if (status)
b9cef6c3
WF
3059 dev_dbg(&udev->dev,
3060 "disable remote wakeup, status %d\n",
3061 status);
4bf0ba86 3062 }
1da177e4 3063 status = 0;
1da177e4
LT
3064 }
3065 return status;
3066}
3067
624d6c07
AS
3068/*
3069 * usb_port_resume - re-activate a suspended usb device's upstream port
3070 * @udev: device to re-activate, not a root hub
3071 * Context: must be able to sleep; device not locked; pm locks held
3072 *
3073 * This will re-activate the suspended device, increasing power usage
3074 * while letting drivers communicate again with its endpoints.
3075 * USB resume explicitly guarantees that the power session between
3076 * the host and the device is the same as it was when the device
3077 * suspended.
3078 *
feccc30d
AS
3079 * If @udev->reset_resume is set then this routine won't check that the
3080 * port is still enabled. Furthermore, finish_port_resume() above will
54515fe5
AS
3081 * reset @udev. The end result is that a broken power session can be
3082 * recovered and @udev will appear to persist across a loss of VBUS power.
3083 *
3084 * For example, if a host controller doesn't maintain VBUS suspend current
3085 * during a system sleep or is reset when the system wakes up, all the USB
3086 * power sessions below it will be broken. This is especially troublesome
3087 * for mass-storage devices containing mounted filesystems, since the
3088 * device will appear to have disconnected and all the memory mappings
3089 * to it will be lost. Using the USB_PERSIST facility, the device can be
3090 * made to appear as if it had not disconnected.
3091 *
742120c6 3092 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
feccc30d 3093 * every effort to insure that the same device is present after the
54515fe5
AS
3094 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3095 * quite possible for a device to remain unaltered but its media to be
3096 * changed. If the user replaces a flash memory card while the system is
3097 * asleep, he will have only himself to blame when the filesystem on the
3098 * new card is corrupted and the system crashes.
3099 *
624d6c07
AS
3100 * Returns 0 on success, else negative errno.
3101 */
65bfd296 3102int usb_port_resume(struct usb_device *udev, pm_message_t msg)
1da177e4 3103{
624d6c07
AS
3104 struct usb_hub *hub = hdev_to_hub(udev->parent);
3105 int port1 = udev->portnum;
3106 int status;
3107 u16 portchange, portstatus;
d25450c6
AS
3108
3109 /* Skip the initial Clear-Suspend step for a remote wakeup */
3110 status = hub_port_status(hub, port1, &portstatus, &portchange);
0ed9a57e 3111 if (status == 0 && !port_is_suspended(hub, portstatus))
d25450c6 3112 goto SuspendCleared;
1da177e4
LT
3113
3114 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
3115
d5cbad4b
AS
3116 set_bit(port1, hub->busy_bits);
3117
1da177e4 3118 /* see 7.1.7.7; affects power usage, but not budgeting */
a7114230
AX
3119 if (hub_is_superspeed(hub->hdev))
3120 status = set_port_feature(hub->hdev,
3121 port1 | (USB_SS_PORT_LS_U0 << 3),
3122 USB_PORT_FEAT_LINK_STATE);
3123 else
3124 status = clear_port_feature(hub->hdev,
3125 port1, USB_PORT_FEAT_SUSPEND);
1da177e4 3126 if (status) {
624d6c07
AS
3127 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
3128 port1, status);
1da177e4 3129 } else {
1da177e4 3130 /* drive resume for at least 20 msec */
686314cf 3131 dev_dbg(&udev->dev, "usb %sresume\n",
5b1b0b81 3132 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
1da177e4
LT
3133 msleep(25);
3134
1da177e4
LT
3135 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3136 * stop resume signaling. Then finish the resume
3137 * sequence.
3138 */
d25450c6 3139 status = hub_port_status(hub, port1, &portstatus, &portchange);
54515fe5 3140
b01b03f3
AS
3141 /* TRSMRCY = 10 msec */
3142 msleep(10);
3143 }
3144
54515fe5 3145 SuspendCleared:
b01b03f3 3146 if (status == 0) {
bfd1e910 3147 udev->port_is_suspended = 0;
a7114230
AX
3148 if (hub_is_superspeed(hub->hdev)) {
3149 if (portchange & USB_PORT_STAT_C_LINK_STATE)
3150 clear_port_feature(hub->hdev, port1,
3151 USB_PORT_FEAT_C_PORT_LINK_STATE);
3152 } else {
3153 if (portchange & USB_PORT_STAT_C_SUSPEND)
3154 clear_port_feature(hub->hdev, port1,
3155 USB_PORT_FEAT_C_SUSPEND);
3156 }
1da177e4 3157 }
1da177e4 3158
d5cbad4b 3159 clear_bit(port1, hub->busy_bits);
d5cbad4b 3160
b01b03f3
AS
3161 status = check_port_resume_type(udev,
3162 hub, port1, status, portchange, portstatus);
54515fe5
AS
3163 if (status == 0)
3164 status = finish_port_resume(udev);
3165 if (status < 0) {
3166 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3167 hub_port_logical_disconnect(hub, port1);
65580b43
AX
3168 } else {
3169 /* Try to enable USB2 hardware LPM */
3170 if (udev->usb2_hw_lpm_capable == 1)
3171 usb_set_usb2_hardware_lpm(udev, 1);
8306095f 3172
f74631e3
SS
3173 /* Try to enable USB3 LTM and LPM */
3174 usb_enable_ltm(udev);
8306095f 3175 usb_unlocked_enable_lpm(udev);
54515fe5 3176 }
65580b43 3177
1da177e4
LT
3178 return status;
3179}
3180
8808f00c 3181/* caller has locked udev */
0534d468 3182int usb_remote_wakeup(struct usb_device *udev)
1da177e4
LT
3183{
3184 int status = 0;
3185
1da177e4 3186 if (udev->state == USB_STATE_SUSPENDED) {
645daaab 3187 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
9bbdf1e0
AS
3188 status = usb_autoresume_device(udev);
3189 if (status == 0) {
3190 /* Let the drivers do their thing, then... */
3191 usb_autosuspend_device(udev);
3192 }
1da177e4 3193 }
1da177e4
LT
3194 return status;
3195}
3196
d388dab7
AS
3197#else /* CONFIG_USB_SUSPEND */
3198
3199/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3200
65bfd296 3201int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
d388dab7
AS
3202{
3203 return 0;
3204}
3205
b01b03f3
AS
3206/* However we may need to do a reset-resume */
3207
65bfd296 3208int usb_port_resume(struct usb_device *udev, pm_message_t msg)
d388dab7 3209{
b01b03f3
AS
3210 struct usb_hub *hub = hdev_to_hub(udev->parent);
3211 int port1 = udev->portnum;
3212 int status;
3213 u16 portchange, portstatus;
3214
3215 status = hub_port_status(hub, port1, &portstatus, &portchange);
3216 status = check_port_resume_type(udev,
3217 hub, port1, status, portchange, portstatus);
54515fe5 3218
b01b03f3
AS
3219 if (status) {
3220 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3221 hub_port_logical_disconnect(hub, port1);
3222 } else if (udev->reset_resume) {
54515fe5 3223 dev_dbg(&udev->dev, "reset-resume\n");
742120c6 3224 status = usb_reset_and_verify_device(udev);
54515fe5
AS
3225 }
3226 return status;
d388dab7
AS
3227}
3228
d388dab7
AS
3229#endif
3230
e6f30dea
ML
3231static int check_ports_changed(struct usb_hub *hub)
3232{
3233 int port1;
3234
3235 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3236 u16 portstatus, portchange;
3237 int status;
3238
3239 status = hub_port_status(hub, port1, &portstatus, &portchange);
3240 if (!status && portchange)
3241 return 1;
3242 }
3243 return 0;
3244}
3245
db690874 3246static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
1da177e4
LT
3247{
3248 struct usb_hub *hub = usb_get_intfdata (intf);
3249 struct usb_device *hdev = hub->hdev;
3250 unsigned port1;
4296c70a 3251 int status;
1da177e4 3252
cbb33004 3253 /* Warn if children aren't already suspended */
1da177e4
LT
3254 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3255 struct usb_device *udev;
3256
ff823c79 3257 udev = hub->ports[port1 - 1]->child;
6840d255 3258 if (udev && udev->can_submit) {
cbb33004 3259 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
5b1b0b81 3260 if (PMSG_IS_AUTO(msg))
cbb33004 3261 return -EBUSY;
c9f89fa4 3262 }
1da177e4 3263 }
e6f30dea
ML
3264
3265 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3266 /* check if there are changes pending on hub ports */
3267 if (check_ports_changed(hub)) {
3268 if (PMSG_IS_AUTO(msg))
3269 return -EBUSY;
3270 pm_wakeup_event(&hdev->dev, 2000);
3271 }
3272 }
3273
4296c70a
SS
3274 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3275 /* Enable hub to send remote wakeup for all ports. */
3276 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3277 status = set_port_feature(hdev,
3278 port1 |
3279 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3280 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3281 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3282 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3283 }
3284 }
1da177e4 3285
441b62c1 3286 dev_dbg(&intf->dev, "%s\n", __func__);
40f122f3 3287
12f1ff83 3288 /* stop khubd and related activity */
4330354f 3289 hub_quiesce(hub, HUB_SUSPEND);
b6f6436d 3290 return 0;
1da177e4
LT
3291}
3292
3293static int hub_resume(struct usb_interface *intf)
3294{
5e6effae 3295 struct usb_hub *hub = usb_get_intfdata(intf);
40f122f3 3296
5e6effae 3297 dev_dbg(&intf->dev, "%s\n", __func__);
f2835219 3298 hub_activate(hub, HUB_RESUME);
1da177e4
LT
3299 return 0;
3300}
3301
b41a60ec 3302static int hub_reset_resume(struct usb_interface *intf)
f07600cf 3303{
b41a60ec 3304 struct usb_hub *hub = usb_get_intfdata(intf);
f07600cf 3305
5e6effae 3306 dev_dbg(&intf->dev, "%s\n", __func__);
f2835219 3307 hub_activate(hub, HUB_RESET_RESUME);
f07600cf
AS
3308 return 0;
3309}
3310
54515fe5
AS
3311/**
3312 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3313 * @rhdev: struct usb_device for the root hub
3314 *
3315 * The USB host controller driver calls this function when its root hub
3316 * is resumed and Vbus power has been interrupted or the controller
feccc30d
AS
3317 * has been reset. The routine marks @rhdev as having lost power.
3318 * When the hub driver is resumed it will take notice and carry out
3319 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3320 * the others will be disconnected.
54515fe5
AS
3321 */
3322void usb_root_hub_lost_power(struct usb_device *rhdev)
3323{
3324 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3325 rhdev->reset_resume = 1;
3326}
3327EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3328
1ea7e0e8
SS
3329static const char * const usb3_lpm_names[] = {
3330 "U0",
3331 "U1",
3332 "U2",
3333 "U3",
3334};
3335
3336/*
3337 * Send a Set SEL control transfer to the device, prior to enabling
3338 * device-initiated U1 or U2. This lets the device know the exit latencies from
3339 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3340 * packet from the host.
3341 *
3342 * This function will fail if the SEL or PEL values for udev are greater than
3343 * the maximum allowed values for the link state to be enabled.
3344 */
3345static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3346{
3347 struct usb_set_sel_req *sel_values;
3348 unsigned long long u1_sel;
3349 unsigned long long u1_pel;
3350 unsigned long long u2_sel;
3351 unsigned long long u2_pel;
3352 int ret;
3353
3354 /* Convert SEL and PEL stored in ns to us */
3355 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3356 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3357 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3358 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3359
3360 /*
3361 * Make sure that the calculated SEL and PEL values for the link
3362 * state we're enabling aren't bigger than the max SEL/PEL
3363 * value that will fit in the SET SEL control transfer.
3364 * Otherwise the device would get an incorrect idea of the exit
3365 * latency for the link state, and could start a device-initiated
3366 * U1/U2 when the exit latencies are too high.
3367 */
3368 if ((state == USB3_LPM_U1 &&
3369 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3370 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3371 (state == USB3_LPM_U2 &&
3372 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3373 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
1510a1a2 3374 dev_dbg(&udev->dev, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n",
1ea7e0e8
SS
3375 usb3_lpm_names[state], u1_sel, u1_pel);
3376 return -EINVAL;
3377 }
3378
3379 /*
3380 * If we're enabling device-initiated LPM for one link state,
3381 * but the other link state has a too high SEL or PEL value,
3382 * just set those values to the max in the Set SEL request.
3383 */
3384 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3385 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3386
3387 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3388 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3389
3390 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3391 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3392
3393 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3394 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3395
3396 /*
3397 * usb_enable_lpm() can be called as part of a failed device reset,
3398 * which may be initiated by an error path of a mass storage driver.
3399 * Therefore, use GFP_NOIO.
3400 */
3401 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3402 if (!sel_values)
3403 return -ENOMEM;
3404
3405 sel_values->u1_sel = u1_sel;
3406 sel_values->u1_pel = u1_pel;
3407 sel_values->u2_sel = cpu_to_le16(u2_sel);
3408 sel_values->u2_pel = cpu_to_le16(u2_pel);
3409
3410 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3411 USB_REQ_SET_SEL,
3412 USB_RECIP_DEVICE,
3413 0, 0,
3414 sel_values, sizeof *(sel_values),
3415 USB_CTRL_SET_TIMEOUT);
3416 kfree(sel_values);
3417 return ret;
3418}
3419
3420/*
3421 * Enable or disable device-initiated U1 or U2 transitions.
3422 */
3423static int usb_set_device_initiated_lpm(struct usb_device *udev,
3424 enum usb3_link_state state, bool enable)
3425{
3426 int ret;
3427 int feature;
3428
3429 switch (state) {
3430 case USB3_LPM_U1:
3431 feature = USB_DEVICE_U1_ENABLE;
3432 break;
3433 case USB3_LPM_U2:
3434 feature = USB_DEVICE_U2_ENABLE;
3435 break;
3436 default:
3437 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3438 __func__, enable ? "enable" : "disable");
3439 return -EINVAL;
3440 }
3441
3442 if (udev->state != USB_STATE_CONFIGURED) {
3443 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3444 "for unconfigured device.\n",
3445 __func__, enable ? "enable" : "disable",
3446 usb3_lpm_names[state]);
3447 return 0;
3448 }
3449
3450 if (enable) {
1ea7e0e8
SS
3451 /*
3452 * Now send the control transfer to enable device-initiated LPM
3453 * for either U1 or U2.
3454 */
3455 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3456 USB_REQ_SET_FEATURE,
3457 USB_RECIP_DEVICE,
3458 feature,
3459 0, NULL, 0,
3460 USB_CTRL_SET_TIMEOUT);
3461 } else {
3462 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3463 USB_REQ_CLEAR_FEATURE,
3464 USB_RECIP_DEVICE,
3465 feature,
3466 0, NULL, 0,
3467 USB_CTRL_SET_TIMEOUT);
3468 }
3469 if (ret < 0) {
3470 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3471 enable ? "Enable" : "Disable",
3472 usb3_lpm_names[state]);
3473 return -EBUSY;
3474 }
3475 return 0;
3476}
3477
3478static int usb_set_lpm_timeout(struct usb_device *udev,
3479 enum usb3_link_state state, int timeout)
3480{
3481 int ret;
3482 int feature;
3483
3484 switch (state) {
3485 case USB3_LPM_U1:
3486 feature = USB_PORT_FEAT_U1_TIMEOUT;
3487 break;
3488 case USB3_LPM_U2:
3489 feature = USB_PORT_FEAT_U2_TIMEOUT;
3490 break;
3491 default:
3492 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3493 __func__);
3494 return -EINVAL;
3495 }
3496
3497 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3498 timeout != USB3_LPM_DEVICE_INITIATED) {
3499 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3500 "which is a reserved value.\n",
3501 usb3_lpm_names[state], timeout);
3502 return -EINVAL;
3503 }
3504
3505 ret = set_port_feature(udev->parent,
3506 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3507 feature);
3508 if (ret < 0) {
3509 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3510 "error code %i\n", usb3_lpm_names[state],
3511 timeout, ret);
3512 return -EBUSY;
3513 }
3514 if (state == USB3_LPM_U1)
3515 udev->u1_params.timeout = timeout;
3516 else
3517 udev->u2_params.timeout = timeout;
3518 return 0;
3519}
3520
3521/*
3522 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3523 * U1/U2 entry.
3524 *
3525 * We will attempt to enable U1 or U2, but there are no guarantees that the
3526 * control transfers to set the hub timeout or enable device-initiated U1/U2
3527 * will be successful.
3528 *
3529 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3530 * driver know about it. If that call fails, it should be harmless, and just
3531 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3532 */
3533static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3534 enum usb3_link_state state)
3535{
65a95b75 3536 int timeout, ret;
ae8963ad
SS
3537 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3538 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3539
3540 /* If the device says it doesn't have *any* exit latency to come out of
3541 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3542 * state.
3543 */
3544 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3545 (state == USB3_LPM_U2 && u2_mel == 0))
3546 return;
1ea7e0e8 3547
65a95b75
SS
3548 /*
3549 * First, let the device know about the exit latencies
3550 * associated with the link state we're about to enable.
3551 */
3552 ret = usb_req_set_sel(udev, state);
3553 if (ret < 0) {
3554 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3555 usb3_lpm_names[state]);
3556 return;
3557 }
3558
1ea7e0e8
SS
3559 /* We allow the host controller to set the U1/U2 timeout internally
3560 * first, so that it can change its schedule to account for the
3561 * additional latency to send data to a device in a lower power
3562 * link state.
3563 */
3564 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3565
3566 /* xHCI host controller doesn't want to enable this LPM state. */
3567 if (timeout == 0)
3568 return;
3569
3570 if (timeout < 0) {
3571 dev_warn(&udev->dev, "Could not enable %s link state, "
3572 "xHCI error %i.\n", usb3_lpm_names[state],
3573 timeout);
3574 return;
3575 }
3576
3577 if (usb_set_lpm_timeout(udev, state, timeout))
3578 /* If we can't set the parent hub U1/U2 timeout,
3579 * device-initiated LPM won't be allowed either, so let the xHCI
3580 * host know that this link state won't be enabled.
3581 */
3582 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3583
3584 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3585 else if (udev->actconfig)
3586 usb_set_device_initiated_lpm(udev, state, true);
3587
3588}
3589
3590/*
3591 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3592 * U1/U2 entry.
3593 *
3594 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3595 * If zero is returned, the parent will not allow the link to go into U1/U2.
3596 *
3597 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3598 * it won't have an effect on the bus link state because the parent hub will
3599 * still disallow device-initiated U1/U2 entry.
3600 *
3601 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3602 * possible. The result will be slightly more bus bandwidth will be taken up
3603 * (to account for U1/U2 exit latency), but it should be harmless.
3604 */
3605static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3606 enum usb3_link_state state)
3607{
3608 int feature;
3609
3610 switch (state) {
3611 case USB3_LPM_U1:
3612 feature = USB_PORT_FEAT_U1_TIMEOUT;
3613 break;
3614 case USB3_LPM_U2:
3615 feature = USB_PORT_FEAT_U2_TIMEOUT;
3616 break;
3617 default:
3618 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3619 __func__);
3620 return -EINVAL;
3621 }
3622
3623 if (usb_set_lpm_timeout(udev, state, 0))
3624 return -EBUSY;
3625
3626 usb_set_device_initiated_lpm(udev, state, false);
3627
3628 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3629 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3630 "bus schedule bandwidth may be impacted.\n",
3631 usb3_lpm_names[state]);
3632 return 0;
3633}
3634
3635/*
3636 * Disable hub-initiated and device-initiated U1 and U2 entry.
3637 * Caller must own the bandwidth_mutex.
3638 *
3639 * This will call usb_enable_lpm() on failure, which will decrement
3640 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3641 */
3642int usb_disable_lpm(struct usb_device *udev)
3643{
3644 struct usb_hcd *hcd;
3645
3646 if (!udev || !udev->parent ||
3647 udev->speed != USB_SPEED_SUPER ||
3648 !udev->lpm_capable)
3649 return 0;
3650
3651 hcd = bus_to_hcd(udev->bus);
3652 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3653 return 0;
3654
3655 udev->lpm_disable_count++;
55558c33 3656 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
1ea7e0e8
SS
3657 return 0;
3658
3659 /* If LPM is enabled, attempt to disable it. */
3660 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3661 goto enable_lpm;
3662 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3663 goto enable_lpm;
3664
3665 return 0;
3666
3667enable_lpm:
3668 usb_enable_lpm(udev);
3669 return -EBUSY;
3670}
3671EXPORT_SYMBOL_GPL(usb_disable_lpm);
3672
3673/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3674int usb_unlocked_disable_lpm(struct usb_device *udev)
3675{
3676 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3677 int ret;
3678
3679 if (!hcd)
3680 return -EINVAL;
3681
3682 mutex_lock(hcd->bandwidth_mutex);
3683 ret = usb_disable_lpm(udev);
3684 mutex_unlock(hcd->bandwidth_mutex);
3685
3686 return ret;
3687}
3688EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3689
3690/*
3691 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3692 * xHCI host policy may prevent U1 or U2 from being enabled.
3693 *
3694 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3695 * until the lpm_disable_count drops to zero. Caller must own the
3696 * bandwidth_mutex.
3697 */
3698void usb_enable_lpm(struct usb_device *udev)
3699{
3700 struct usb_hcd *hcd;
3701
3702 if (!udev || !udev->parent ||
3703 udev->speed != USB_SPEED_SUPER ||
3704 !udev->lpm_capable)
3705 return;
3706
3707 udev->lpm_disable_count--;
3708 hcd = bus_to_hcd(udev->bus);
3709 /* Double check that we can both enable and disable LPM.
3710 * Device must be configured to accept set feature U1/U2 timeout.
3711 */
3712 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3713 !hcd->driver->disable_usb3_lpm_timeout)
3714 return;
3715
3716 if (udev->lpm_disable_count > 0)
3717 return;
3718
3719 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3720 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3721}
3722EXPORT_SYMBOL_GPL(usb_enable_lpm);
3723
3724/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3725void usb_unlocked_enable_lpm(struct usb_device *udev)
3726{
3727 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3728
3729 if (!hcd)
3730 return;
3731
3732 mutex_lock(hcd->bandwidth_mutex);
3733 usb_enable_lpm(udev);
3734 mutex_unlock(hcd->bandwidth_mutex);
3735}
3736EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3737
3738
d388dab7
AS
3739#else /* CONFIG_PM */
3740
f07600cf
AS
3741#define hub_suspend NULL
3742#define hub_resume NULL
3743#define hub_reset_resume NULL
1ea7e0e8
SS
3744
3745int usb_disable_lpm(struct usb_device *udev)
3746{
3747 return 0;
3748}
e9261fb6 3749EXPORT_SYMBOL_GPL(usb_disable_lpm);
1ea7e0e8
SS
3750
3751void usb_enable_lpm(struct usb_device *udev) { }
e9261fb6 3752EXPORT_SYMBOL_GPL(usb_enable_lpm);
1ea7e0e8
SS
3753
3754int usb_unlocked_disable_lpm(struct usb_device *udev)
3755{
3756 return 0;
3757}
e9261fb6 3758EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
1ea7e0e8
SS
3759
3760void usb_unlocked_enable_lpm(struct usb_device *udev) { }
e9261fb6 3761EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
f74631e3
SS
3762
3763int usb_disable_ltm(struct usb_device *udev)
3764{
3765 return 0;
3766}
3767EXPORT_SYMBOL_GPL(usb_disable_ltm);
3768
3769void usb_enable_ltm(struct usb_device *udev) { }
3770EXPORT_SYMBOL_GPL(usb_enable_ltm);
d388dab7
AS
3771#endif
3772
1da177e4
LT
3773
3774/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3775 *
3776 * Between connect detection and reset signaling there must be a delay
3777 * of 100ms at least for debounce and power-settling. The corresponding
3778 * timer shall restart whenever the downstream port detects a disconnect.
3779 *
3780 * Apparently there are some bluetooth and irda-dongles and a number of
3781 * low-speed devices for which this debounce period may last over a second.
3782 * Not covered by the spec - but easy to deal with.
3783 *
3784 * This implementation uses a 1500ms total debounce timeout; if the
3785 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3786 * every 25ms for transient disconnects. When the port status has been
3787 * unchanged for 100ms it returns the port status.
3788 */
1da177e4
LT
3789static int hub_port_debounce(struct usb_hub *hub, int port1)
3790{
3791 int ret;
3792 int total_time, stable_time = 0;
3793 u16 portchange, portstatus;
3794 unsigned connection = 0xffff;
3795
3796 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3797 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3798 if (ret < 0)
3799 return ret;
3800
3801 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3802 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3803 stable_time += HUB_DEBOUNCE_STEP;
3804 if (stable_time >= HUB_DEBOUNCE_STABLE)
3805 break;
3806 } else {
3807 stable_time = 0;
3808 connection = portstatus & USB_PORT_STAT_CONNECTION;
3809 }
3810
3811 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3812 clear_port_feature(hub->hdev, port1,
3813 USB_PORT_FEAT_C_CONNECTION);
3814 }
3815
3816 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3817 break;
3818 msleep(HUB_DEBOUNCE_STEP);
3819 }
3820
3821 dev_dbg (hub->intfdev,
3822 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3823 port1, total_time, stable_time, portstatus);
3824
3825 if (stable_time < HUB_DEBOUNCE_STABLE)
3826 return -ETIMEDOUT;
3827 return portstatus;
3828}
3829
fc721f51 3830void usb_ep0_reinit(struct usb_device *udev)
1da177e4 3831{
ddeac4e7
AS
3832 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3833 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
2caf7fcd 3834 usb_enable_endpoint(udev, &udev->ep0, true);
1da177e4 3835}
fc721f51 3836EXPORT_SYMBOL_GPL(usb_ep0_reinit);
1da177e4
LT
3837
3838#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3839#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3840
4326ed0b 3841static int hub_set_address(struct usb_device *udev, int devnum)
1da177e4
LT
3842{
3843 int retval;
c6515272 3844 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1da177e4 3845
c6515272
SS
3846 /*
3847 * The host controller will choose the device address,
3848 * instead of the core having chosen it earlier
3849 */
3850 if (!hcd->driver->address_device && devnum <= 1)
1da177e4
LT
3851 return -EINVAL;
3852 if (udev->state == USB_STATE_ADDRESS)
3853 return 0;
3854 if (udev->state != USB_STATE_DEFAULT)
3855 return -EINVAL;
c8d4af8e 3856 if (hcd->driver->address_device)
c6515272 3857 retval = hcd->driver->address_device(hcd, udev);
c8d4af8e 3858 else
c6515272
SS
3859 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3860 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3861 NULL, 0, USB_CTRL_SET_TIMEOUT);
1da177e4 3862 if (retval == 0) {
3b29b68b 3863 update_devnum(udev, devnum);
4953d141 3864 /* Device now using proper address. */
1da177e4 3865 usb_set_device_state(udev, USB_STATE_ADDRESS);
fc721f51 3866 usb_ep0_reinit(udev);
1da177e4
LT
3867 }
3868 return retval;
3869}
3870
3871/* Reset device, (re)assign address, get device descriptor.
3872 * Device connection must be stable, no more debouncing needed.
3873 * Returns device in USB_STATE_ADDRESS, except on error.
3874 *
3875 * If this is called for an already-existing device (as part of
742120c6 3876 * usb_reset_and_verify_device), the caller must own the device lock. For a
1da177e4
LT
3877 * newly detected device that is not accessible through any global
3878 * pointers, it's not necessary to lock the device.
3879 */
3880static int
3881hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3882 int retry_counter)
3883{
4186ecf8 3884 static DEFINE_MUTEX(usb_address0_mutex);
1da177e4
LT
3885
3886 struct usb_device *hdev = hub->hdev;
e7b77172 3887 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
1da177e4
LT
3888 int i, j, retval;
3889 unsigned delay = HUB_SHORT_RESET_TIME;
3890 enum usb_device_speed oldspeed = udev->speed;
e538dfda 3891 const char *speed;
4326ed0b 3892 int devnum = udev->devnum;
1da177e4
LT
3893
3894 /* root hub ports have a slightly longer reset period
3895 * (from USB 2.0 spec, section 7.1.7.5)
3896 */
3897 if (!hdev->parent) {
3898 delay = HUB_ROOT_RESET_TIME;
3899 if (port1 == hdev->bus->otg_port)
3900 hdev->bus->b_hnp_enable = 0;
3901 }
3902
3903 /* Some low speed devices have problems with the quick delay, so */
3904 /* be a bit pessimistic with those devices. RHbug #23670 */
3905 if (oldspeed == USB_SPEED_LOW)
3906 delay = HUB_LONG_RESET_TIME;
3907
4186ecf8 3908 mutex_lock(&usb_address0_mutex);
1da177e4 3909
07194ab7
LT
3910 /* Reset the device; full speed may morph to high speed */
3911 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
75d7cf72 3912 retval = hub_port_reset(hub, port1, udev, delay, false);
07194ab7
LT
3913 if (retval < 0) /* error or disconnect */
3914 goto fail;
3915 /* success, speed is known */
3916
1da177e4
LT
3917 retval = -ENODEV;
3918
3919 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3920 dev_dbg(&udev->dev, "device reset changed speed!\n");
3921 goto fail;
3922 }
3923 oldspeed = udev->speed;
4326ed0b 3924
1da177e4
LT
3925 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3926 * it's fixed size except for full speed devices.
5bb6e0ae
IPG
3927 * For Wireless USB devices, ep0 max packet is always 512 (tho
3928 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
1da177e4
LT
3929 */
3930 switch (udev->speed) {
6b403b02 3931 case USB_SPEED_SUPER:
551cdbbe 3932 case USB_SPEED_WIRELESS: /* fixed at 512 */
551509d2 3933 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
5bb6e0ae 3934 break;
1da177e4 3935 case USB_SPEED_HIGH: /* fixed at 64 */
551509d2 3936 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
1da177e4
LT
3937 break;
3938 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3939 /* to determine the ep0 maxpacket size, try to read
3940 * the device descriptor to get bMaxPacketSize0 and
3941 * then correct our initial guess.
3942 */
551509d2 3943 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
1da177e4
LT
3944 break;
3945 case USB_SPEED_LOW: /* fixed at 8 */
551509d2 3946 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
1da177e4
LT
3947 break;
3948 default:
3949 goto fail;
3950 }
e538dfda
MN
3951
3952 if (udev->speed == USB_SPEED_WIRELESS)
3953 speed = "variable speed Wireless";
3954 else
3955 speed = usb_speed_string(udev->speed);
3956
c6515272
SS
3957 if (udev->speed != USB_SPEED_SUPER)
3958 dev_info(&udev->dev,
e538dfda
MN
3959 "%s %s USB device number %d using %s\n",
3960 (udev->config) ? "reset" : "new", speed,
3b29b68b 3961 devnum, udev->bus->controller->driver->name);
1da177e4
LT
3962
3963 /* Set up TT records, if needed */
3964 if (hdev->tt) {
3965 udev->tt = hdev->tt;
3966 udev->ttport = hdev->ttport;
3967 } else if (udev->speed != USB_SPEED_HIGH
3968 && hdev->speed == USB_SPEED_HIGH) {
d199c96d
AS
3969 if (!hub->tt.hub) {
3970 dev_err(&udev->dev, "parent hub has no TT\n");
3971 retval = -EINVAL;
3972 goto fail;
3973 }
1da177e4
LT
3974 udev->tt = &hub->tt;
3975 udev->ttport = port1;
3976 }
3977
3978 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3979 * Because device hardware and firmware is sometimes buggy in
3980 * this area, and this is how Linux has done it for ages.
3981 * Change it cautiously.
3982 *
3983 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3984 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3985 * so it may help with some non-standards-compliant devices.
3986 * Otherwise we start with SET_ADDRESS and then try to read the
3987 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3988 * value.
3989 */
3990 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
c6515272 3991 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
1da177e4
LT
3992 struct usb_device_descriptor *buf;
3993 int r = 0;
3994
3995#define GET_DESCRIPTOR_BUFSIZE 64
3996 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3997 if (!buf) {
3998 retval = -ENOMEM;
3999 continue;
4000 }
4001
b89ee19a
AS
4002 /* Retry on all errors; some devices are flakey.
4003 * 255 is for WUSB devices, we actually need to use
4004 * 512 (WUSB1.0[4.8.1]).
1da177e4
LT
4005 */
4006 for (j = 0; j < 3; ++j) {
4007 buf->bMaxPacketSize0 = 0;
4008 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
4009 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
4010 USB_DT_DEVICE << 8, 0,
4011 buf, GET_DESCRIPTOR_BUFSIZE,
fd7c519d 4012 initial_descriptor_timeout);
1da177e4 4013 switch (buf->bMaxPacketSize0) {
5bb6e0ae 4014 case 8: case 16: case 32: case 64: case 255:
1da177e4
LT
4015 if (buf->bDescriptorType ==
4016 USB_DT_DEVICE) {
4017 r = 0;
4018 break;
4019 }
4020 /* FALL THROUGH */
4021 default:
4022 if (r == 0)
4023 r = -EPROTO;
4024 break;
4025 }
4026 if (r == 0)
4027 break;
4028 }
4029 udev->descriptor.bMaxPacketSize0 =
4030 buf->bMaxPacketSize0;
4031 kfree(buf);
4032
75d7cf72 4033 retval = hub_port_reset(hub, port1, udev, delay, false);
1da177e4
LT
4034 if (retval < 0) /* error or disconnect */
4035 goto fail;
4036 if (oldspeed != udev->speed) {
4037 dev_dbg(&udev->dev,
4038 "device reset changed speed!\n");
4039 retval = -ENODEV;
4040 goto fail;
4041 }
4042 if (r) {
b9cef6c3
WF
4043 dev_err(&udev->dev,
4044 "device descriptor read/64, error %d\n",
4045 r);
1da177e4
LT
4046 retval = -EMSGSIZE;
4047 continue;
4048 }
4049#undef GET_DESCRIPTOR_BUFSIZE
4050 }
4051
6c529cdc
IPG
4052 /*
4053 * If device is WUSB, we already assigned an
4054 * unauthorized address in the Connect Ack sequence;
4055 * authorization will assign the final address.
4056 */
c6515272 4057 if (udev->wusb == 0) {
6c529cdc
IPG
4058 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
4059 retval = hub_set_address(udev, devnum);
4060 if (retval >= 0)
4061 break;
4062 msleep(200);
4063 }
4064 if (retval < 0) {
4065 dev_err(&udev->dev,
4066 "device not accepting address %d, error %d\n",
4067 devnum, retval);
4068 goto fail;
4069 }
c6515272
SS
4070 if (udev->speed == USB_SPEED_SUPER) {
4071 devnum = udev->devnum;
4072 dev_info(&udev->dev,
3b29b68b 4073 "%s SuperSpeed USB device number %d using %s\n",
c6515272 4074 (udev->config) ? "reset" : "new",
3b29b68b 4075 devnum, udev->bus->controller->driver->name);
c6515272 4076 }
6c529cdc
IPG
4077
4078 /* cope with hardware quirkiness:
4079 * - let SET_ADDRESS settle, some device hardware wants it
4080 * - read ep0 maxpacket even for high and low speed,
4081 */
4082 msleep(10);
c6515272 4083 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
1da177e4 4084 break;
6c529cdc 4085 }
1da177e4
LT
4086
4087 retval = usb_get_device_descriptor(udev, 8);
4088 if (retval < 8) {
b9cef6c3
WF
4089 dev_err(&udev->dev,
4090 "device descriptor read/8, error %d\n",
4091 retval);
1da177e4
LT
4092 if (retval >= 0)
4093 retval = -EMSGSIZE;
4094 } else {
4095 retval = 0;
4096 break;
4097 }
4098 }
4099 if (retval)
4100 goto fail;
4101
b76baa81 4102 if (hcd->phy && !hdev->parent)
ac96511b 4103 usb_phy_notify_connect(hcd->phy, udev->speed);
b76baa81 4104
d8aec3db
EF
4105 /*
4106 * Some superspeed devices have finished the link training process
4107 * and attached to a superspeed hub port, but the device descriptor
4108 * got from those devices show they aren't superspeed devices. Warm
4109 * reset the port attached by the devices can fix them.
4110 */
4111 if ((udev->speed == USB_SPEED_SUPER) &&
4112 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4113 dev_err(&udev->dev, "got a wrong device descriptor, "
4114 "warm reset device\n");
4115 hub_port_reset(hub, port1, udev,
4116 HUB_BH_RESET_TIME, true);
4117 retval = -EINVAL;
4118 goto fail;
4119 }
4120
6b403b02
SS
4121 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
4122 udev->speed == USB_SPEED_SUPER)
4123 i = 512;
4124 else
4125 i = udev->descriptor.bMaxPacketSize0;
29cc8897 4126 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
56626a72 4127 if (udev->speed == USB_SPEED_LOW ||
1da177e4 4128 !(i == 8 || i == 16 || i == 32 || i == 64)) {
56626a72 4129 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
1da177e4
LT
4130 retval = -EMSGSIZE;
4131 goto fail;
4132 }
56626a72
AS
4133 if (udev->speed == USB_SPEED_FULL)
4134 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4135 else
4136 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
1da177e4 4137 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
fc721f51 4138 usb_ep0_reinit(udev);
1da177e4
LT
4139 }
4140
4141 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4142 if (retval < (signed)sizeof(udev->descriptor)) {
b9cef6c3
WF
4143 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4144 retval);
1da177e4
LT
4145 if (retval >= 0)
4146 retval = -ENOMSG;
4147 goto fail;
4148 }
4149
1ff4df56
AX
4150 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4151 retval = usb_get_bos_descriptor(udev);
51e0a012 4152 if (!retval) {
d9b2099c 4153 udev->lpm_capable = usb_device_supports_lpm(udev);
51e0a012
SS
4154 usb_set_lpm_parameters(udev);
4155 }
1ff4df56 4156 }
3148bf04 4157
1da177e4 4158 retval = 0;
48f24970
AD
4159 /* notify HCD that we have a device connected and addressed */
4160 if (hcd->driver->update_device)
4161 hcd->driver->update_device(hcd, udev);
1da177e4 4162fail:
4326ed0b 4163 if (retval) {
1da177e4 4164 hub_port_disable(hub, port1, 0);
3b29b68b 4165 update_devnum(udev, devnum); /* for disconnect processing */
4326ed0b 4166 }
4186ecf8 4167 mutex_unlock(&usb_address0_mutex);
1da177e4
LT
4168 return retval;
4169}
4170
4171static void
4172check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4173{
4174 struct usb_qualifier_descriptor *qual;
4175 int status;
4176
e94b1766 4177 qual = kmalloc (sizeof *qual, GFP_KERNEL);
1da177e4
LT
4178 if (qual == NULL)
4179 return;
4180
4181 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4182 qual, sizeof *qual);
4183 if (status == sizeof *qual) {
4184 dev_info(&udev->dev, "not running at top speed; "
4185 "connect to a high speed hub\n");
4186 /* hub LEDs are probably harder to miss than syslog */
4187 if (hub->has_indicators) {
4188 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
c4028958 4189 schedule_delayed_work (&hub->leds, 0);
1da177e4
LT
4190 }
4191 }
1bc3c9e1 4192 kfree(qual);
1da177e4
LT
4193}
4194
4195static unsigned
4196hub_power_remaining (struct usb_hub *hub)
4197{
4198 struct usb_device *hdev = hub->hdev;
4199 int remaining;
55c52718 4200 int port1;
1da177e4 4201
55c52718 4202 if (!hub->limited_power)
1da177e4
LT
4203 return 0;
4204
55c52718
AS
4205 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4206 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
ff823c79 4207 struct usb_device *udev = hub->ports[port1 - 1]->child;
55c52718 4208 int delta;
1da177e4
LT
4209
4210 if (!udev)
4211 continue;
4212
55c52718
AS
4213 /* Unconfigured devices may not use more than 100mA,
4214 * or 8mA for OTG ports */
1da177e4 4215 if (udev->actconfig)
55c52718
AS
4216 delta = udev->actconfig->desc.bMaxPower * 2;
4217 else if (port1 != udev->bus->otg_port || hdev->parent)
4218 delta = 100;
1da177e4 4219 else
55c52718
AS
4220 delta = 8;
4221 if (delta > hub->mA_per_port)
b9cef6c3
WF
4222 dev_warn(&udev->dev,
4223 "%dmA is over %umA budget for port %d!\n",
4224 delta, hub->mA_per_port, port1);
1da177e4
LT
4225 remaining -= delta;
4226 }
4227 if (remaining < 0) {
55c52718
AS
4228 dev_warn(hub->intfdev, "%dmA over power budget!\n",
4229 - remaining);
1da177e4
LT
4230 remaining = 0;
4231 }
4232 return remaining;
4233}
4234
4235/* Handle physical or logical connection change events.
4236 * This routine is called when:
4237 * a port connection-change occurs;
4238 * a port enable-change occurs (often caused by EMI);
742120c6 4239 * usb_reset_and_verify_device() encounters changed descriptors (as from
1da177e4
LT
4240 * a firmware download)
4241 * caller already locked the hub
4242 */
4243static void hub_port_connect_change(struct usb_hub *hub, int port1,
4244 u16 portstatus, u16 portchange)
4245{
4246 struct usb_device *hdev = hub->hdev;
4247 struct device *hub_dev = hub->intfdev;
90da096e 4248 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
24618b0c
AS
4249 unsigned wHubCharacteristics =
4250 le16_to_cpu(hub->descriptor->wHubCharacteristics);
8808f00c 4251 struct usb_device *udev;
1da177e4 4252 int status, i;
24618b0c 4253
1da177e4
LT
4254 dev_dbg (hub_dev,
4255 "port %d, status %04x, change %04x, %s\n",
131dec34 4256 port1, portstatus, portchange, portspeed(hub, portstatus));
1da177e4
LT
4257
4258 if (hub->has_indicators) {
4259 set_port_led(hub, port1, HUB_LED_AUTO);
4260 hub->indicator[port1-1] = INDICATOR_AUTO;
4261 }
1da177e4
LT
4262
4263#ifdef CONFIG_USB_OTG
4264 /* during HNP, don't repeat the debounce */
4265 if (hdev->bus->is_b_host)
24618b0c
AS
4266 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4267 USB_PORT_STAT_C_ENABLE);
1da177e4
LT
4268#endif
4269
8808f00c 4270 /* Try to resuscitate an existing device */
ff823c79 4271 udev = hub->ports[port1 - 1]->child;
8808f00c
AS
4272 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4273 udev->state != USB_STATE_NOTATTACHED) {
8808f00c
AS
4274 usb_lock_device(udev);
4275 if (portstatus & USB_PORT_STAT_ENABLE) {
4276 status = 0; /* Nothing to do */
8808f00c
AS
4277
4278#ifdef CONFIG_USB_SUSPEND
5257d97a
AS
4279 } else if (udev->state == USB_STATE_SUSPENDED &&
4280 udev->persist_enabled) {
8808f00c
AS
4281 /* For a suspended device, treat this as a
4282 * remote wakeup event.
4283 */
0534d468 4284 status = usb_remote_wakeup(udev);
8808f00c
AS
4285#endif
4286
4287 } else {
5257d97a 4288 status = -ENODEV; /* Don't resuscitate */
8808f00c
AS
4289 }
4290 usb_unlock_device(udev);
4291
4292 if (status == 0) {
4293 clear_bit(port1, hub->change_bits);
4294 return;
4295 }
4296 }
4297
24618b0c 4298 /* Disconnect any existing devices under this port */
b76baa81
PC
4299 if (udev) {
4300 if (hcd->phy && !hdev->parent &&
4301 !(portstatus & USB_PORT_STAT_CONNECTION))
ac96511b 4302 usb_phy_notify_disconnect(hcd->phy, udev->speed);
ff823c79 4303 usb_disconnect(&hub->ports[port1 - 1]->child);
b76baa81 4304 }
24618b0c
AS
4305 clear_bit(port1, hub->change_bits);
4306
253e0572
AS
4307 /* We can forget about a "removed" device when there's a physical
4308 * disconnect or the connect status changes.
4309 */
4310 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4311 (portchange & USB_PORT_STAT_C_CONNECTION))
4312 clear_bit(port1, hub->removed_bits);
4313
5257d97a
AS
4314 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4315 USB_PORT_STAT_C_ENABLE)) {
4316 status = hub_port_debounce(hub, port1);
4317 if (status < 0) {
4318 if (printk_ratelimit())
4319 dev_err(hub_dev, "connect-debounce failed, "
4320 "port %d disabled\n", port1);
4321 portstatus &= ~USB_PORT_STAT_CONNECTION;
4322 } else {
4323 portstatus = status;
4324 }
4325 }
4326
253e0572
AS
4327 /* Return now if debouncing failed or nothing is connected or
4328 * the device was "removed".
4329 */
4330 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4331 test_bit(port1, hub->removed_bits)) {
1da177e4
LT
4332
4333 /* maybe switch power back on (e.g. root hub was reset) */
74ad9bd2 4334 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
0ed9a57e 4335 && !port_is_power_on(hub, portstatus))
1da177e4 4336 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
5257d97a 4337
1da177e4
LT
4338 if (portstatus & USB_PORT_STAT_ENABLE)
4339 goto done;
4340 return;
4341 }
4342
1da177e4 4343 for (i = 0; i < SET_CONFIG_TRIES; i++) {
1da177e4
LT
4344
4345 /* reallocate for each attempt, since references
4346 * to the previous one can escape in various ways
4347 */
4348 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4349 if (!udev) {
4350 dev_err (hub_dev,
4351 "couldn't allocate port %d usb_device\n",
4352 port1);
4353 goto done;
4354 }
4355
4356 usb_set_device_state(udev, USB_STATE_POWERED);
55c52718 4357 udev->bus_mA = hub->mA_per_port;
b6956ffa 4358 udev->level = hdev->level + 1;
8af548dc 4359 udev->wusb = hub_is_wusb(hub);
55c52718 4360
131dec34
SS
4361 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4362 if (hub_is_superspeed(hub->hdev))
e7b77172
SS
4363 udev->speed = USB_SPEED_SUPER;
4364 else
4365 udev->speed = USB_SPEED_UNKNOWN;
4366
3b29b68b 4367 choose_devnum(udev);
c8d4af8e
AX
4368 if (udev->devnum <= 0) {
4369 status = -ENOTCONN; /* Don't retry */
4370 goto loop;
c6515272
SS
4371 }
4372
e7b77172 4373 /* reset (non-USB 3.0 devices) and get descriptor */
1da177e4
LT
4374 status = hub_port_init(hub, udev, port1, i);
4375 if (status < 0)
4376 goto loop;
4377
93362a87
PD
4378 usb_detect_quirks(udev);
4379 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4380 msleep(1000);
4381
1da177e4
LT
4382 /* consecutive bus-powered hubs aren't reliable; they can
4383 * violate the voltage drop budget. if the new child has
4384 * a "powered" LED, users should notice we didn't enable it
4385 * (without reading syslog), even without per-port LEDs
4386 * on the parent.
4387 */
4388 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
55c52718 4389 && udev->bus_mA <= 100) {
1da177e4
LT
4390 u16 devstat;
4391
4392 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4393 &devstat);
55c52718 4394 if (status < 2) {
1da177e4
LT
4395 dev_dbg(&udev->dev, "get status %d ?\n", status);
4396 goto loop_disable;
4397 }
55c52718 4398 le16_to_cpus(&devstat);
1da177e4
LT
4399 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4400 dev_err(&udev->dev,
4401 "can't connect bus-powered hub "
4402 "to this port\n");
4403 if (hub->has_indicators) {
4404 hub->indicator[port1-1] =
4405 INDICATOR_AMBER_BLINK;
c4028958 4406 schedule_delayed_work (&hub->leds, 0);
1da177e4
LT
4407 }
4408 status = -ENOTCONN; /* Don't retry */
4409 goto loop_disable;
4410 }
4411 }
4412
4413 /* check for devices running slower than they could */
4414 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4415 && udev->speed == USB_SPEED_FULL
4416 && highspeed_hubs != 0)
4417 check_highspeed (hub, udev, port1);
4418
fa286188 4419 /* Store the parent's children[] pointer. At this point
1da177e4
LT
4420 * udev becomes globally accessible, although presumably
4421 * no one will look at it until hdev is unlocked.
4422 */
1da177e4
LT
4423 status = 0;
4424
4425 /* We mustn't add new devices if the parent hub has
4426 * been disconnected; we would race with the
4427 * recursively_mark_NOTATTACHED() routine.
4428 */
4429 spin_lock_irq(&device_state_lock);
4430 if (hdev->state == USB_STATE_NOTATTACHED)
4431 status = -ENOTCONN;
4432 else
ff823c79 4433 hub->ports[port1 - 1]->child = udev;
1da177e4
LT
4434 spin_unlock_irq(&device_state_lock);
4435
4436 /* Run it through the hoops (find a driver, etc) */
4437 if (!status) {
4438 status = usb_new_device(udev);
4439 if (status) {
4440 spin_lock_irq(&device_state_lock);
ff823c79 4441 hub->ports[port1 - 1]->child = NULL;
1da177e4
LT
4442 spin_unlock_irq(&device_state_lock);
4443 }
4444 }
4445
1da177e4
LT
4446 if (status)
4447 goto loop_disable;
4448
4449 status = hub_power_remaining(hub);
4450 if (status)
55c52718 4451 dev_dbg(hub_dev, "%dmA power budget left\n", status);
1da177e4
LT
4452
4453 return;
4454
4455loop_disable:
4456 hub_port_disable(hub, port1, 1);
4457loop:
fc721f51 4458 usb_ep0_reinit(udev);
3b29b68b 4459 release_devnum(udev);
f7410ced 4460 hub_free_dev(udev);
1da177e4 4461 usb_put_dev(udev);
ffcdc18d 4462 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
1da177e4
LT
4463 break;
4464 }
3a31155c
AS
4465 if (hub->hdev->parent ||
4466 !hcd->driver->port_handed_over ||
4467 !(hcd->driver->port_handed_over)(hcd, port1))
4468 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4469 port1);
1da177e4
LT
4470
4471done:
4472 hub_port_disable(hub, port1, 1);
90da096e
BR
4473 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4474 hcd->driver->relinquish_port(hcd, port1);
1da177e4
LT
4475}
4476
714b07be
SS
4477/* Returns 1 if there was a remote wakeup and a connect status change. */
4478static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
72937e1e 4479 u16 portstatus, u16 portchange)
714b07be
SS
4480{
4481 struct usb_device *hdev;
4482 struct usb_device *udev;
4483 int connect_change = 0;
4484 int ret;
4485
4486 hdev = hub->hdev;
ff823c79 4487 udev = hub->ports[port - 1]->child;
4ee823b8
SS
4488 if (!hub_is_superspeed(hdev)) {
4489 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4490 return 0;
4491 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
4492 } else {
4493 if (!udev || udev->state != USB_STATE_SUSPENDED ||
72937e1e
SS
4494 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4495 USB_SS_PORT_LS_U0)
4ee823b8
SS
4496 return 0;
4497 }
4498
714b07be
SS
4499 if (udev) {
4500 /* TRSMRCY = 10 msec */
4501 msleep(10);
4502
4503 usb_lock_device(udev);
4504 ret = usb_remote_wakeup(udev);
4505 usb_unlock_device(udev);
4506 if (ret < 0)
4507 connect_change = 1;
4508 } else {
4509 ret = -ENODEV;
4510 hub_port_disable(hub, port, 1);
4511 }
4512 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4513 port, ret);
4514 return connect_change;
4515}
4516
1da177e4
LT
4517static void hub_events(void)
4518{
4519 struct list_head *tmp;
4520 struct usb_device *hdev;
4521 struct usb_interface *intf;
4522 struct usb_hub *hub;
4523 struct device *hub_dev;
4524 u16 hubstatus;
4525 u16 hubchange;
4526 u16 portstatus;
4527 u16 portchange;
4528 int i, ret;
4ee823b8 4529 int connect_change, wakeup_change;
1da177e4
LT
4530
4531 /*
4532 * We restart the list every time to avoid a deadlock with
4533 * deleting hubs downstream from this one. This should be
4534 * safe since we delete the hub from the event list.
4535 * Not the most efficient, but avoids deadlocks.
4536 */
4537 while (1) {
4538
4539 /* Grab the first entry at the beginning of the list */
4540 spin_lock_irq(&hub_event_lock);
4541 if (list_empty(&hub_event_list)) {
4542 spin_unlock_irq(&hub_event_lock);
4543 break;
4544 }
4545
4546 tmp = hub_event_list.next;
4547 list_del_init(tmp);
4548
4549 hub = list_entry(tmp, struct usb_hub, event_list);
e8054854
AS
4550 kref_get(&hub->kref);
4551 spin_unlock_irq(&hub_event_lock);
1da177e4 4552
e8054854
AS
4553 hdev = hub->hdev;
4554 hub_dev = hub->intfdev;
4555 intf = to_usb_interface(hub_dev);
40f122f3 4556 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
1da177e4
LT
4557 hdev->state, hub->descriptor
4558 ? hub->descriptor->bNbrPorts
4559 : 0,
4560 /* NOTE: expects max 15 ports... */
4561 (u16) hub->change_bits[0],
40f122f3 4562 (u16) hub->event_bits[0]);
1da177e4 4563
1da177e4
LT
4564 /* Lock the device, then check to see if we were
4565 * disconnected while waiting for the lock to succeed. */
06b84e8a 4566 usb_lock_device(hdev);
e8054854 4567 if (unlikely(hub->disconnected))
9bbdf1e0 4568 goto loop_disconnected;
1da177e4
LT
4569
4570 /* If the hub has died, clean up after it */
4571 if (hdev->state == USB_STATE_NOTATTACHED) {
7de18d8b 4572 hub->error = -ENODEV;
4330354f 4573 hub_quiesce(hub, HUB_DISCONNECT);
1da177e4
LT
4574 goto loop;
4575 }
4576
40f122f3
AS
4577 /* Autoresume */
4578 ret = usb_autopm_get_interface(intf);
4579 if (ret) {
4580 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
4581 goto loop;
4582 }
a8e7c565 4583
40f122f3 4584 /* If this is an inactive hub, do nothing */
1da177e4 4585 if (hub->quiescing)
40f122f3 4586 goto loop_autopm;
1da177e4
LT
4587
4588 if (hub->error) {
4589 dev_dbg (hub_dev, "resetting for error %d\n",
4590 hub->error);
4591
742120c6 4592 ret = usb_reset_device(hdev);
1da177e4
LT
4593 if (ret) {
4594 dev_dbg (hub_dev,
4595 "error resetting hub: %d\n", ret);
40f122f3 4596 goto loop_autopm;
1da177e4
LT
4597 }
4598
4599 hub->nerrors = 0;
4600 hub->error = 0;
4601 }
4602
4603 /* deal with port status changes */
4604 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4605 if (test_bit(i, hub->busy_bits))
4606 continue;
4607 connect_change = test_bit(i, hub->change_bits);
72937e1e 4608 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
1da177e4 4609 if (!test_and_clear_bit(i, hub->event_bits) &&
4ee823b8 4610 !connect_change && !wakeup_change)
1da177e4
LT
4611 continue;
4612
4613 ret = hub_port_status(hub, i,
4614 &portstatus, &portchange);
4615 if (ret < 0)
4616 continue;
4617
1da177e4
LT
4618 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4619 clear_port_feature(hdev, i,
4620 USB_PORT_FEAT_C_CONNECTION);
4621 connect_change = 1;
4622 }
4623
4624 if (portchange & USB_PORT_STAT_C_ENABLE) {
4625 if (!connect_change)
4626 dev_dbg (hub_dev,
4627 "port %d enable change, "
4628 "status %08x\n",
4629 i, portstatus);
4630 clear_port_feature(hdev, i,
4631 USB_PORT_FEAT_C_ENABLE);
4632
4633 /*
4634 * EM interference sometimes causes badly
4635 * shielded USB devices to be shutdown by
4636 * the hub, this hack enables them again.
4637 * Works at least with mouse driver.
4638 */
4639 if (!(portstatus & USB_PORT_STAT_ENABLE)
4640 && !connect_change
ff823c79 4641 && hub->ports[i - 1]->child) {
1da177e4
LT
4642 dev_err (hub_dev,
4643 "port %i "
4644 "disabled by hub (EMI?), "
4645 "re-enabling...\n",
4646 i);
4647 connect_change = 1;
4648 }
4649 }
4650
72937e1e
SS
4651 if (hub_handle_remote_wakeup(hub, i,
4652 portstatus, portchange))
714b07be 4653 connect_change = 1;
8808f00c 4654
1da177e4 4655 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
752d57a8
PB
4656 u16 status = 0;
4657 u16 unused;
4658
4659 dev_dbg(hub_dev, "over-current change on port "
4660 "%d\n", i);
1da177e4
LT
4661 clear_port_feature(hdev, i,
4662 USB_PORT_FEAT_C_OVER_CURRENT);
752d57a8 4663 msleep(100); /* Cool down */
8520f380 4664 hub_power_on(hub, true);
752d57a8
PB
4665 hub_port_status(hub, i, &status, &unused);
4666 if (status & USB_PORT_STAT_OVERCURRENT)
4667 dev_err(hub_dev, "over-current "
4668 "condition on port %d\n", i);
1da177e4
LT
4669 }
4670
4671 if (portchange & USB_PORT_STAT_C_RESET) {
4672 dev_dbg (hub_dev,
4673 "reset change on port %d\n",
4674 i);
4675 clear_port_feature(hdev, i,
4676 USB_PORT_FEAT_C_RESET);
4677 }
c7061574
SS
4678 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4679 hub_is_superspeed(hub->hdev)) {
4680 dev_dbg(hub_dev,
4681 "warm reset change on port %d\n",
4682 i);
4683 clear_port_feature(hdev, i,
4684 USB_PORT_FEAT_C_BH_PORT_RESET);
4685 }
dbe79bbe
JY
4686 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4687 clear_port_feature(hub->hdev, i,
4688 USB_PORT_FEAT_C_PORT_LINK_STATE);
4689 }
4690 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4691 dev_warn(hub_dev,
4692 "config error on port %d\n",
4693 i);
4694 clear_port_feature(hub->hdev, i,
4695 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4696 }
1da177e4 4697
5e467f6e
AX
4698 /* Warm reset a USB3 protocol port if it's in
4699 * SS.Inactive state.
4700 */
8bea2bd3 4701 if (hub_port_warm_reset_required(hub, portstatus)) {
65bdac5e
SS
4702 int status;
4703
5e467f6e 4704 dev_dbg(hub_dev, "warm reset port %d\n", i);
65bdac5e 4705 status = hub_port_reset(hub, i, NULL,
75d7cf72 4706 HUB_BH_RESET_TIME, true);
65bdac5e
SS
4707 if (status < 0)
4708 hub_port_disable(hub, i, 1);
4709 connect_change = 0;
5e467f6e
AX
4710 }
4711
1da177e4
LT
4712 if (connect_change)
4713 hub_port_connect_change(hub, i,
4714 portstatus, portchange);
4715 } /* end for i */
4716
4717 /* deal with hub status changes */
4718 if (test_and_clear_bit(0, hub->event_bits) == 0)
4719 ; /* do nothing */
4720 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4721 dev_err (hub_dev, "get_hub_status failed\n");
4722 else {
4723 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4724 dev_dbg (hub_dev, "power change\n");
4725 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
55c52718
AS
4726 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4727 /* FIXME: Is this always true? */
55c52718 4728 hub->limited_power = 1;
403fae78 4729 else
4730 hub->limited_power = 0;
1da177e4
LT
4731 }
4732 if (hubchange & HUB_CHANGE_OVERCURRENT) {
752d57a8
PB
4733 u16 status = 0;
4734 u16 unused;
4735
4736 dev_dbg(hub_dev, "over-current change\n");
1da177e4 4737 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
752d57a8 4738 msleep(500); /* Cool down */
8520f380 4739 hub_power_on(hub, true);
752d57a8
PB
4740 hub_hub_status(hub, &status, &unused);
4741 if (status & HUB_STATUS_OVERCURRENT)
4742 dev_err(hub_dev, "over-current "
4743 "condition\n");
1da177e4
LT
4744 }
4745 }
4746
8e4ceb38
AS
4747 loop_autopm:
4748 /* Balance the usb_autopm_get_interface() above */
4749 usb_autopm_put_interface_no_suspend(intf);
4750 loop:
4751 /* Balance the usb_autopm_get_interface_no_resume() in
4752 * kick_khubd() and allow autosuspend.
4753 */
4754 usb_autopm_put_interface(intf);
9bbdf1e0 4755 loop_disconnected:
1da177e4 4756 usb_unlock_device(hdev);
e8054854 4757 kref_put(&hub->kref, hub_release);
1da177e4
LT
4758
4759 } /* end while (1) */
4760}
4761
4762static int hub_thread(void *__unused)
4763{
3bb1af52
AS
4764 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4765 * port handover. Otherwise it might see that a full-speed device
4766 * was gone before the EHCI controller had handed its port over to
4767 * the companion full-speed controller.
4768 */
83144186 4769 set_freezable();
3bb1af52 4770
1da177e4
LT
4771 do {
4772 hub_events();
e42837bc 4773 wait_event_freezable(khubd_wait,
9c8d6178
AM
4774 !list_empty(&hub_event_list) ||
4775 kthread_should_stop());
9c8d6178 4776 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
1da177e4 4777
9c8d6178
AM
4778 pr_debug("%s: khubd exiting\n", usbcore_name);
4779 return 0;
1da177e4
LT
4780}
4781
1e927d96 4782static const struct usb_device_id hub_id_table[] = {
e6f30dea
ML
4783 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
4784 | USB_DEVICE_ID_MATCH_INT_CLASS,
4785 .idVendor = USB_VENDOR_GENESYS_LOGIC,
4786 .bInterfaceClass = USB_CLASS_HUB,
4787 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
1da177e4
LT
4788 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4789 .bDeviceClass = USB_CLASS_HUB},
4790 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4791 .bInterfaceClass = USB_CLASS_HUB},
4792 { } /* Terminating entry */
4793};
4794
4795MODULE_DEVICE_TABLE (usb, hub_id_table);
4796
4797static struct usb_driver hub_driver = {
1da177e4
LT
4798 .name = "hub",
4799 .probe = hub_probe,
4800 .disconnect = hub_disconnect,
4801 .suspend = hub_suspend,
4802 .resume = hub_resume,
f07600cf 4803 .reset_resume = hub_reset_resume,
7de18d8b
AS
4804 .pre_reset = hub_pre_reset,
4805 .post_reset = hub_post_reset,
c532b29a 4806 .unlocked_ioctl = hub_ioctl,
1da177e4 4807 .id_table = hub_id_table,
40f122f3 4808 .supports_autosuspend = 1,
1da177e4
LT
4809};
4810
4811int usb_hub_init(void)
4812{
1da177e4
LT
4813 if (usb_register(&hub_driver) < 0) {
4814 printk(KERN_ERR "%s: can't register hub driver\n",
4815 usbcore_name);
4816 return -1;
4817 }
4818
9c8d6178
AM
4819 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4820 if (!IS_ERR(khubd_task))
1da177e4 4821 return 0;
1da177e4
LT
4822
4823 /* Fall through if kernel_thread failed */
4824 usb_deregister(&hub_driver);
4825 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4826
4827 return -1;
4828}
4829
4830void usb_hub_cleanup(void)
4831{
9c8d6178 4832 kthread_stop(khubd_task);
1da177e4
LT
4833
4834 /*
4835 * Hub resources are freed for us by usb_deregister. It calls
4836 * usb_driver_purge on every device which in turn calls that
4837 * devices disconnect function if it is using this driver.
4838 * The hub_disconnect function takes care of releasing the
4839 * individual hub resources. -greg
4840 */
4841 usb_deregister(&hub_driver);
4842} /* usb_hub_cleanup() */
4843
eb764c4b
AS
4844static int descriptors_changed(struct usb_device *udev,
4845 struct usb_device_descriptor *old_device_descriptor)
4846{
4847 int changed = 0;
4848 unsigned index;
4849 unsigned serial_len = 0;
4850 unsigned len;
4851 unsigned old_length;
4852 int length;
4853 char *buf;
4854
4855 if (memcmp(&udev->descriptor, old_device_descriptor,
4856 sizeof(*old_device_descriptor)) != 0)
4857 return 1;
4858
4859 /* Since the idVendor, idProduct, and bcdDevice values in the
4860 * device descriptor haven't changed, we will assume the
4861 * Manufacturer and Product strings haven't changed either.
4862 * But the SerialNumber string could be different (e.g., a
4863 * different flash card of the same brand).
4864 */
4865 if (udev->serial)
4866 serial_len = strlen(udev->serial) + 1;
1da177e4 4867
eb764c4b 4868 len = serial_len;
1da177e4 4869 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
eb764c4b
AS
4870 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4871 len = max(len, old_length);
1da177e4 4872 }
eb764c4b 4873
0cc1a51f 4874 buf = kmalloc(len, GFP_NOIO);
1da177e4
LT
4875 if (buf == NULL) {
4876 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4877 /* assume the worst */
4878 return 1;
4879 }
4880 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
eb764c4b 4881 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
1da177e4
LT
4882 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4883 old_length);
eb764c4b 4884 if (length != old_length) {
1da177e4
LT
4885 dev_dbg(&udev->dev, "config index %d, error %d\n",
4886 index, length);
eb764c4b 4887 changed = 1;
1da177e4
LT
4888 break;
4889 }
4890 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4891 != 0) {
4892 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
eb764c4b
AS
4893 index,
4894 ((struct usb_config_descriptor *) buf)->
4895 bConfigurationValue);
4896 changed = 1;
1da177e4
LT
4897 break;
4898 }
4899 }
eb764c4b
AS
4900
4901 if (!changed && serial_len) {
4902 length = usb_string(udev, udev->descriptor.iSerialNumber,
4903 buf, serial_len);
4904 if (length + 1 != serial_len) {
4905 dev_dbg(&udev->dev, "serial string error %d\n",
4906 length);
4907 changed = 1;
4908 } else if (memcmp(buf, udev->serial, length) != 0) {
4909 dev_dbg(&udev->dev, "serial string changed\n");
4910 changed = 1;
4911 }
4912 }
4913
1da177e4 4914 kfree(buf);
eb764c4b 4915 return changed;
1da177e4
LT
4916}
4917
4918/**
742120c6 4919 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
1da177e4
LT
4920 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4921 *
79efa097
AS
4922 * WARNING - don't use this routine to reset a composite device
4923 * (one with multiple interfaces owned by separate drivers)!
742120c6 4924 * Use usb_reset_device() instead.
1da177e4
LT
4925 *
4926 * Do a port reset, reassign the device's address, and establish its
4927 * former operating configuration. If the reset fails, or the device's
4928 * descriptors change from their values before the reset, or the original
4929 * configuration and altsettings cannot be restored, a flag will be set
4930 * telling khubd to pretend the device has been disconnected and then
4931 * re-connected. All drivers will be unbound, and the device will be
4932 * re-enumerated and probed all over again.
4933 *
4934 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4935 * flagged for logical disconnection, or some other negative error code
4936 * if the reset wasn't even attempted.
4937 *
4938 * The caller must own the device lock. For example, it's safe to use
4939 * this from a driver probe() routine after downloading new firmware.
4940 * For calls that might not occur during probe(), drivers should lock
4941 * the device using usb_lock_device_for_reset().
6bc6cff5
AS
4942 *
4943 * Locking exception: This routine may also be called from within an
4944 * autoresume handler. Such usage won't conflict with other tasks
4945 * holding the device lock because these tasks should always call
4946 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
1da177e4 4947 */
742120c6 4948static int usb_reset_and_verify_device(struct usb_device *udev)
1da177e4
LT
4949{
4950 struct usb_device *parent_hdev = udev->parent;
4951 struct usb_hub *parent_hub;
3f0479e0 4952 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1da177e4 4953 struct usb_device_descriptor descriptor = udev->descriptor;
12c3da34
AS
4954 int i, ret = 0;
4955 int port1 = udev->portnum;
1da177e4
LT
4956
4957 if (udev->state == USB_STATE_NOTATTACHED ||
4958 udev->state == USB_STATE_SUSPENDED) {
4959 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4960 udev->state);
4961 return -EINVAL;
4962 }
4963
4964 if (!parent_hdev) {
4bec9917 4965 /* this requires hcd-specific logic; see ohci_restart() */
441b62c1 4966 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
1da177e4
LT
4967 return -EISDIR;
4968 }
1da177e4
LT
4969 parent_hub = hdev_to_hub(parent_hdev);
4970
f74631e3
SS
4971 /* Disable LPM and LTM while we reset the device and reinstall the alt
4972 * settings. Device-initiated LPM settings, and system exit latency
4973 * settings are cleared when the device is reset, so we have to set
4974 * them up again.
6d1d0513
SS
4975 */
4976 ret = usb_unlocked_disable_lpm(udev);
4977 if (ret) {
4978 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
4979 goto re_enumerate;
4980 }
f74631e3
SS
4981 ret = usb_disable_ltm(udev);
4982 if (ret) {
4983 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
4984 __func__);
4985 goto re_enumerate;
4986 }
6d1d0513 4987
1da177e4
LT
4988 set_bit(port1, parent_hub->busy_bits);
4989 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4990
4991 /* ep0 maxpacket size may change; let the HCD know about it.
4992 * Other endpoints will be handled by re-enumeration. */
fc721f51 4993 usb_ep0_reinit(udev);
1da177e4 4994 ret = hub_port_init(parent_hub, udev, port1, i);
dd4dd19e 4995 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
1da177e4
LT
4996 break;
4997 }
4998 clear_bit(port1, parent_hub->busy_bits);
d5cbad4b 4999
1da177e4
LT
5000 if (ret < 0)
5001 goto re_enumerate;
5002
5003 /* Device might have changed firmware (DFU or similar) */
eb764c4b 5004 if (descriptors_changed(udev, &descriptor)) {
1da177e4
LT
5005 dev_info(&udev->dev, "device firmware changed\n");
5006 udev->descriptor = descriptor; /* for disconnect() calls */
5007 goto re_enumerate;
5008 }
4fe0387a
AS
5009
5010 /* Restore the device's previous configuration */
1da177e4
LT
5011 if (!udev->actconfig)
5012 goto done;
3f0479e0 5013
d673bfcb 5014 mutex_lock(hcd->bandwidth_mutex);
3f0479e0
SS
5015 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
5016 if (ret < 0) {
5017 dev_warn(&udev->dev,
5018 "Busted HC? Not enough HCD resources for "
5019 "old configuration.\n");
d673bfcb 5020 mutex_unlock(hcd->bandwidth_mutex);
3f0479e0
SS
5021 goto re_enumerate;
5022 }
1da177e4
LT
5023 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
5024 USB_REQ_SET_CONFIGURATION, 0,
5025 udev->actconfig->desc.bConfigurationValue, 0,
5026 NULL, 0, USB_CTRL_SET_TIMEOUT);
5027 if (ret < 0) {
5028 dev_err(&udev->dev,
5029 "can't restore configuration #%d (error=%d)\n",
5030 udev->actconfig->desc.bConfigurationValue, ret);
d673bfcb 5031 mutex_unlock(hcd->bandwidth_mutex);
1da177e4
LT
5032 goto re_enumerate;
5033 }
d673bfcb 5034 mutex_unlock(hcd->bandwidth_mutex);
1da177e4
LT
5035 usb_set_device_state(udev, USB_STATE_CONFIGURED);
5036
4fe0387a
AS
5037 /* Put interfaces back into the same altsettings as before.
5038 * Don't bother to send the Set-Interface request for interfaces
5039 * that were already in altsetting 0; besides being unnecessary,
5040 * many devices can't handle it. Instead just reset the host-side
5041 * endpoint state.
5042 */
1da177e4 5043 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
3f0479e0
SS
5044 struct usb_host_config *config = udev->actconfig;
5045 struct usb_interface *intf = config->interface[i];
1da177e4
LT
5046 struct usb_interface_descriptor *desc;
5047
1da177e4 5048 desc = &intf->cur_altsetting->desc;
4fe0387a
AS
5049 if (desc->bAlternateSetting == 0) {
5050 usb_disable_interface(udev, intf, true);
5051 usb_enable_interface(udev, intf, true);
5052 ret = 0;
5053 } else {
04a723ea
SS
5054 /* Let the bandwidth allocation function know that this
5055 * device has been reset, and it will have to use
5056 * alternate setting 0 as the current alternate setting.
3f0479e0 5057 */
04a723ea 5058 intf->resetting_device = 1;
4fe0387a
AS
5059 ret = usb_set_interface(udev, desc->bInterfaceNumber,
5060 desc->bAlternateSetting);
04a723ea 5061 intf->resetting_device = 0;
4fe0387a 5062 }
1da177e4
LT
5063 if (ret < 0) {
5064 dev_err(&udev->dev, "failed to restore interface %d "
5065 "altsetting %d (error=%d)\n",
5066 desc->bInterfaceNumber,
5067 desc->bAlternateSetting,
5068 ret);
5069 goto re_enumerate;
5070 }
5071 }
5072
5073done:
f74631e3 5074 /* Now that the alt settings are re-installed, enable LTM and LPM. */
8306095f 5075 usb_unlocked_enable_lpm(udev);
f74631e3 5076 usb_enable_ltm(udev);
1da177e4
LT
5077 return 0;
5078
5079re_enumerate:
6d1d0513 5080 /* LPM state doesn't matter when we're about to destroy the device. */
1da177e4
LT
5081 hub_port_logical_disconnect(parent_hub, port1);
5082 return -ENODEV;
5083}
79efa097
AS
5084
5085/**
742120c6 5086 * usb_reset_device - warn interface drivers and perform a USB port reset
79efa097 5087 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
79efa097
AS
5088 *
5089 * Warns all drivers bound to registered interfaces (using their pre_reset
5090 * method), performs the port reset, and then lets the drivers know that
5091 * the reset is over (using their post_reset method).
5092 *
742120c6 5093 * Return value is the same as for usb_reset_and_verify_device().
79efa097
AS
5094 *
5095 * The caller must own the device lock. For example, it's safe to use
5096 * this from a driver probe() routine after downloading new firmware.
5097 * For calls that might not occur during probe(), drivers should lock
5098 * the device using usb_lock_device_for_reset().
78d9a487
AS
5099 *
5100 * If an interface is currently being probed or disconnected, we assume
5101 * its driver knows how to handle resets. For all other interfaces,
5102 * if the driver doesn't have pre_reset and post_reset methods then
5103 * we attempt to unbind it and rebind afterward.
79efa097 5104 */
742120c6 5105int usb_reset_device(struct usb_device *udev)
79efa097
AS
5106{
5107 int ret;
852c4b43 5108 int i;
79efa097
AS
5109 struct usb_host_config *config = udev->actconfig;
5110
5111 if (udev->state == USB_STATE_NOTATTACHED ||
5112 udev->state == USB_STATE_SUSPENDED) {
5113 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5114 udev->state);
5115 return -EINVAL;
5116 }
5117
645daaab 5118 /* Prevent autosuspend during the reset */
94fcda1f 5119 usb_autoresume_device(udev);
645daaab 5120
79efa097 5121 if (config) {
79efa097 5122 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
852c4b43
AS
5123 struct usb_interface *cintf = config->interface[i];
5124 struct usb_driver *drv;
78d9a487 5125 int unbind = 0;
852c4b43
AS
5126
5127 if (cintf->dev.driver) {
79efa097 5128 drv = to_usb_driver(cintf->dev.driver);
78d9a487
AS
5129 if (drv->pre_reset && drv->post_reset)
5130 unbind = (drv->pre_reset)(cintf);
5131 else if (cintf->condition ==
5132 USB_INTERFACE_BOUND)
5133 unbind = 1;
5134 if (unbind)
5135 usb_forced_unbind_intf(cintf);
79efa097
AS
5136 }
5137 }
5138 }
5139
742120c6 5140 ret = usb_reset_and_verify_device(udev);
79efa097
AS
5141
5142 if (config) {
79efa097 5143 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
852c4b43
AS
5144 struct usb_interface *cintf = config->interface[i];
5145 struct usb_driver *drv;
78d9a487 5146 int rebind = cintf->needs_binding;
852c4b43 5147
78d9a487 5148 if (!rebind && cintf->dev.driver) {
79efa097
AS
5149 drv = to_usb_driver(cintf->dev.driver);
5150 if (drv->post_reset)
78d9a487
AS
5151 rebind = (drv->post_reset)(cintf);
5152 else if (cintf->condition ==
5153 USB_INTERFACE_BOUND)
5154 rebind = 1;
79efa097 5155 }
6c640945 5156 if (ret == 0 && rebind)
78d9a487 5157 usb_rebind_intf(cintf);
79efa097
AS
5158 }
5159 }
5160
94fcda1f 5161 usb_autosuspend_device(udev);
79efa097
AS
5162 return ret;
5163}
742120c6 5164EXPORT_SYMBOL_GPL(usb_reset_device);
dc023dce
IPG
5165
5166
5167/**
5168 * usb_queue_reset_device - Reset a USB device from an atomic context
5169 * @iface: USB interface belonging to the device to reset
5170 *
5171 * This function can be used to reset a USB device from an atomic
5172 * context, where usb_reset_device() won't work (as it blocks).
5173 *
5174 * Doing a reset via this method is functionally equivalent to calling
5175 * usb_reset_device(), except for the fact that it is delayed to a
5176 * workqueue. This means that any drivers bound to other interfaces
5177 * might be unbound, as well as users from usbfs in user space.
5178 *
5179 * Corner cases:
5180 *
5181 * - Scheduling two resets at the same time from two different drivers
5182 * attached to two different interfaces of the same device is
5183 * possible; depending on how the driver attached to each interface
5184 * handles ->pre_reset(), the second reset might happen or not.
5185 *
5186 * - If a driver is unbound and it had a pending reset, the reset will
5187 * be cancelled.
5188 *
5189 * - This function can be called during .probe() or .disconnect()
5190 * times. On return from .disconnect(), any pending resets will be
5191 * cancelled.
5192 *
5193 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5194 * does its own.
5195 *
5196 * NOTE: We don't do any reference count tracking because it is not
5197 * needed. The lifecycle of the work_struct is tied to the
5198 * usb_interface. Before destroying the interface we cancel the
5199 * work_struct, so the fact that work_struct is queued and or
5200 * running means the interface (and thus, the device) exist and
5201 * are referenced.
5202 */
5203void usb_queue_reset_device(struct usb_interface *iface)
5204{
5205 schedule_work(&iface->reset_ws);
5206}
5207EXPORT_SYMBOL_GPL(usb_queue_reset_device);
ff823c79
LT
5208
5209/**
5210 * usb_hub_find_child - Get the pointer of child device
5211 * attached to the port which is specified by @port1.
5212 * @hdev: USB device belonging to the usb hub
5213 * @port1: port num to indicate which port the child device
5214 * is attached to.
5215 *
5216 * USB drivers call this function to get hub's child device
5217 * pointer.
5218 *
5219 * Return NULL if input param is invalid and
5220 * child's usb_device pointer if non-NULL.
5221 */
5222struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5223 int port1)
5224{
5225 struct usb_hub *hub = hdev_to_hub(hdev);
5226
5227 if (port1 < 1 || port1 > hdev->maxchild)
5228 return NULL;
5229 return hub->ports[port1 - 1]->child;
5230}
5231EXPORT_SYMBOL_GPL(usb_hub_find_child);
d5575424 5232
05f91689
LT
5233/**
5234 * usb_set_hub_port_connect_type - set hub port connect type.
5235 * @hdev: USB device belonging to the usb hub
5236 * @port1: port num of the port
5237 * @type: connect type of the port
5238 */
5239void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1,
5240 enum usb_port_connect_type type)
5241{
5242 struct usb_hub *hub = hdev_to_hub(hdev);
5243
5244 hub->ports[port1 - 1]->connect_type = type;
5245}
5246
5247/**
5248 * usb_get_hub_port_connect_type - Get the port's connect type
5249 * @hdev: USB device belonging to the usb hub
5250 * @port1: port num of the port
5251 *
5252 * Return connect type of the port and if input params are
5253 * invalid, return USB_PORT_CONNECT_TYPE_UNKNOWN.
5254 */
5255enum usb_port_connect_type
5256usb_get_hub_port_connect_type(struct usb_device *hdev, int port1)
5257{
5258 struct usb_hub *hub = hdev_to_hub(hdev);
5259
5260 return hub->ports[port1 - 1]->connect_type;
5261}
5262
d5575424
LT
5263#ifdef CONFIG_ACPI
5264/**
5265 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5266 * @hdev: USB device belonging to the usb hub
5267 * @port1: port num of the port
5268 *
5269 * Return port's acpi handle if successful, NULL if params are
5270 * invaild.
5271 */
5272acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5273 int port1)
5274{
5275 struct usb_hub *hub = hdev_to_hub(hdev);
5276
5277 return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
5278}
5279#endif