Merge 3.7-rc3 into usb-next.
[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 743 unsigned long flags;
55e5fdfa 744 int limit = 100;
1da177e4
LT
745
746 spin_lock_irqsave (&hub->tt.lock, flags);
3b6054da 747 while (!list_empty(&hub->tt.clear_list)) {
d0f830d3 748 struct list_head *next;
1da177e4
LT
749 struct usb_tt_clear *clear;
750 struct usb_device *hdev = hub->hdev;
cb88a1b8 751 const struct hc_driver *drv;
1da177e4
LT
752 int status;
753
3b6054da
OP
754 if (!hub->quiescing && --limit < 0)
755 break;
756
d0f830d3
HS
757 next = hub->tt.clear_list.next;
758 clear = list_entry (next, struct usb_tt_clear, clear_list);
1da177e4
LT
759 list_del (&clear->clear_list);
760
761 /* drop lock so HCD can concurrently report other TT errors */
762 spin_unlock_irqrestore (&hub->tt.lock, flags);
763 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
1da177e4
LT
764 if (status)
765 dev_err (&hdev->dev,
766 "clear tt %d (%04x) error %d\n",
767 clear->tt, clear->devinfo, status);
cb88a1b8
AS
768
769 /* Tell the HCD, even if the operation failed */
770 drv = clear->hcd->driver;
771 if (drv->clear_tt_buffer_complete)
772 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
773
1bc3c9e1 774 kfree(clear);
cb88a1b8 775 spin_lock_irqsave(&hub->tt.lock, flags);
1da177e4
LT
776 }
777 spin_unlock_irqrestore (&hub->tt.lock, flags);
778}
779
780/**
cb88a1b8
AS
781 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
782 * @urb: an URB associated with the failed or incomplete split transaction
1da177e4
LT
783 *
784 * High speed HCDs use this to tell the hub driver that some split control or
785 * bulk transaction failed in a way that requires clearing internal state of
786 * a transaction translator. This is normally detected (and reported) from
787 * interrupt context.
788 *
789 * It may not be possible for that hub to handle additional full (or low)
790 * speed transactions until that state is fully cleared out.
791 */
cb88a1b8 792int usb_hub_clear_tt_buffer(struct urb *urb)
1da177e4 793{
cb88a1b8
AS
794 struct usb_device *udev = urb->dev;
795 int pipe = urb->pipe;
1da177e4
LT
796 struct usb_tt *tt = udev->tt;
797 unsigned long flags;
798 struct usb_tt_clear *clear;
799
800 /* we've got to cope with an arbitrary number of pending TT clears,
801 * since each TT has "at least two" buffers that can need it (and
802 * there can be many TTs per hub). even if they're uncommon.
803 */
54e6ecb2 804 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
1da177e4
LT
805 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
806 /* FIXME recover somehow ... RESET_TT? */
cb88a1b8 807 return -ENOMEM;
1da177e4
LT
808 }
809
810 /* info that CLEAR_TT_BUFFER needs */
811 clear->tt = tt->multi ? udev->ttport : 1;
812 clear->devinfo = usb_pipeendpoint (pipe);
813 clear->devinfo |= udev->devnum << 4;
814 clear->devinfo |= usb_pipecontrol (pipe)
815 ? (USB_ENDPOINT_XFER_CONTROL << 11)
816 : (USB_ENDPOINT_XFER_BULK << 11);
817 if (usb_pipein (pipe))
818 clear->devinfo |= 1 << 15;
cb88a1b8
AS
819
820 /* info for completion callback */
821 clear->hcd = bus_to_hcd(udev->bus);
822 clear->ep = urb->ep;
823
1da177e4
LT
824 /* tell keventd to clear state for this TT */
825 spin_lock_irqsave (&tt->lock, flags);
826 list_add_tail (&clear->clear_list, &tt->clear_list);
cb88a1b8 827 schedule_work(&tt->clear_work);
1da177e4 828 spin_unlock_irqrestore (&tt->lock, flags);
cb88a1b8 829 return 0;
1da177e4 830}
cb88a1b8 831EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
1da177e4 832
8520f380
AS
833/* If do_delay is false, return the number of milliseconds the caller
834 * needs to delay.
835 */
836static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
1da177e4
LT
837{
838 int port1;
b789696a 839 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
8520f380 840 unsigned delay;
4489a571
AS
841 u16 wHubCharacteristics =
842 le16_to_cpu(hub->descriptor->wHubCharacteristics);
843
844 /* Enable power on each port. Some hubs have reserved values
845 * of LPSM (> 2) in their descriptors, even though they are
846 * USB 2.0 hubs. Some hubs do not implement port-power switching
847 * but only emulate it. In all cases, the ports won't work
848 * unless we send these messages to the hub.
849 */
850 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
1da177e4 851 dev_dbg(hub->intfdev, "enabling power on all ports\n");
4489a571
AS
852 else
853 dev_dbg(hub->intfdev, "trying to enable port power on "
854 "non-switchable hub\n");
855 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
a0693bd0 856 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
1da177e4 857
b789696a 858 /* Wait at least 100 msec for power to become stable */
8520f380
AS
859 delay = max(pgood_delay, (unsigned) 100);
860 if (do_delay)
861 msleep(delay);
862 return delay;
1da177e4
LT
863}
864
1da177e4
LT
865static int hub_hub_status(struct usb_hub *hub,
866 u16 *status, u16 *change)
867{
868 int ret;
869
db90e7a1 870 mutex_lock(&hub->status_mutex);
1da177e4
LT
871 ret = get_hub_status(hub->hdev, &hub->status->hub);
872 if (ret < 0)
873 dev_err (hub->intfdev,
441b62c1 874 "%s failed (err = %d)\n", __func__, ret);
1da177e4
LT
875 else {
876 *status = le16_to_cpu(hub->status->hub.wHubStatus);
877 *change = le16_to_cpu(hub->status->hub.wHubChange);
878 ret = 0;
879 }
db90e7a1 880 mutex_unlock(&hub->status_mutex);
1da177e4
LT
881 return ret;
882}
883
8b28c752
AS
884static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
885{
886 struct usb_device *hdev = hub->hdev;
0458d5b4 887 int ret = 0;
8b28c752 888
ff823c79
LT
889 if (hub->ports[port1 - 1]->child && set_state)
890 usb_set_device_state(hub->ports[port1 - 1]->child,
8b28c752 891 USB_STATE_NOTATTACHED);
dbe79bbe 892 if (!hub->error && !hub_is_superspeed(hub->hdev))
0458d5b4 893 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
8b28c752
AS
894 if (ret)
895 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
0458d5b4 896 port1, ret);
8b28c752
AS
897 return ret;
898}
899
0458d5b4 900/*
6d42fcdb 901 * Disable a port and mark a logical connect-change event, so that some
0458d5b4
AS
902 * time later khubd will disconnect() any existing usb_device on the port
903 * and will re-enumerate if there actually is a device attached.
904 */
905static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
906{
907 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
908 hub_port_disable(hub, port1, 1);
7d069b7d 909
0458d5b4
AS
910 /* FIXME let caller ask to power down the port:
911 * - some devices won't enumerate without a VBUS power cycle
912 * - SRP saves power that way
913 * - ... new call, TBD ...
914 * That's easy if this hub can switch power per-port, and
915 * khubd reactivates the port later (timer, SRP, etc).
916 * Powerdown must be optional, because of reset/DFU.
917 */
918
919 set_bit(port1, hub->change_bits);
920 kick_khubd(hub);
921}
922
253e0572
AS
923/**
924 * usb_remove_device - disable a device's port on its parent hub
925 * @udev: device to be disabled and removed
926 * Context: @udev locked, must be able to sleep.
927 *
928 * After @udev's port has been disabled, khubd is notified and it will
929 * see that the device has been disconnected. When the device is
930 * physically unplugged and something is plugged in, the events will
931 * be received and processed normally.
932 */
933int usb_remove_device(struct usb_device *udev)
934{
935 struct usb_hub *hub;
936 struct usb_interface *intf;
937
938 if (!udev->parent) /* Can't remove a root hub */
939 return -EINVAL;
940 hub = hdev_to_hub(udev->parent);
941 intf = to_usb_interface(hub->intfdev);
942
943 usb_autopm_get_interface(intf);
944 set_bit(udev->portnum, hub->removed_bits);
945 hub_port_logical_disconnect(hub, udev->portnum);
946 usb_autopm_put_interface(intf);
947 return 0;
948}
949
6ee0b270 950enum hub_activation_type {
8e4ceb38 951 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
8520f380 952 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
6ee0b270 953};
5e6effae 954
8520f380
AS
955static void hub_init_func2(struct work_struct *ws);
956static void hub_init_func3(struct work_struct *ws);
957
f2835219 958static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
5e6effae
AS
959{
960 struct usb_device *hdev = hub->hdev;
653a39d1
SS
961 struct usb_hcd *hcd;
962 int ret;
5e6effae 963 int port1;
f2835219 964 int status;
948fea37 965 bool need_debounce_delay = false;
8520f380
AS
966 unsigned delay;
967
968 /* Continue a partial initialization */
969 if (type == HUB_INIT2)
970 goto init2;
971 if (type == HUB_INIT3)
972 goto init3;
5e6effae 973
a45aa3b3
EF
974 /* The superspeed hub except for root hub has to use Hub Depth
975 * value as an offset into the route string to locate the bits
976 * it uses to determine the downstream port number. So hub driver
977 * should send a set hub depth request to superspeed hub after
978 * the superspeed hub is set configuration in initialization or
979 * reset procedure.
980 *
981 * After a resume, port power should still be on.
f2835219
AS
982 * For any other type of activation, turn it on.
983 */
8520f380 984 if (type != HUB_RESUME) {
a45aa3b3
EF
985 if (hdev->parent && hub_is_superspeed(hdev)) {
986 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
987 HUB_SET_DEPTH, USB_RT_HUB,
988 hdev->level - 1, 0, NULL, 0,
989 USB_CTRL_SET_TIMEOUT);
990 if (ret < 0)
991 dev_err(hub->intfdev,
992 "set hub depth failed\n");
993 }
8520f380
AS
994
995 /* Speed up system boot by using a delayed_work for the
996 * hub's initial power-up delays. This is pretty awkward
997 * and the implementation looks like a home-brewed sort of
998 * setjmp/longjmp, but it saves at least 100 ms for each
999 * root hub (assuming usbcore is compiled into the kernel
1000 * rather than as a module). It adds up.
1001 *
1002 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
1003 * because for those activation types the ports have to be
1004 * operational when we return. In theory this could be done
1005 * for HUB_POST_RESET, but it's easier not to.
1006 */
1007 if (type == HUB_INIT) {
1008 delay = hub_power_on(hub, false);
1009 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
1010 schedule_delayed_work(&hub->init_work,
1011 msecs_to_jiffies(delay));
61fbeba1
AS
1012
1013 /* Suppress autosuspend until init is done */
8e4ceb38
AS
1014 usb_autopm_get_interface_no_resume(
1015 to_usb_interface(hub->intfdev));
8520f380 1016 return; /* Continues at init2: below */
653a39d1
SS
1017 } else if (type == HUB_RESET_RESUME) {
1018 /* The internal host controller state for the hub device
1019 * may be gone after a host power loss on system resume.
1020 * Update the device's info so the HW knows it's a hub.
1021 */
1022 hcd = bus_to_hcd(hdev->bus);
1023 if (hcd->driver->update_hub_device) {
1024 ret = hcd->driver->update_hub_device(hcd, hdev,
1025 &hub->tt, GFP_NOIO);
1026 if (ret < 0) {
1027 dev_err(hub->intfdev, "Host not "
1028 "accepting hub info "
1029 "update.\n");
1030 dev_err(hub->intfdev, "LS/FS devices "
1031 "and hubs may not work "
1032 "under this hub\n.");
1033 }
1034 }
1035 hub_power_on(hub, true);
8520f380
AS
1036 } else {
1037 hub_power_on(hub, true);
1038 }
1039 }
1040 init2:
f2835219 1041
6ee0b270
AS
1042 /* Check each port and set hub->change_bits to let khubd know
1043 * which ports need attention.
5e6effae
AS
1044 */
1045 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
ff823c79 1046 struct usb_device *udev = hub->ports[port1 - 1]->child;
5e6effae
AS
1047 u16 portstatus, portchange;
1048
6ee0b270
AS
1049 portstatus = portchange = 0;
1050 status = hub_port_status(hub, port1, &portstatus, &portchange);
1051 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1052 dev_dbg(hub->intfdev,
1053 "port %d: status %04x change %04x\n",
1054 port1, portstatus, portchange);
1055
1056 /* After anything other than HUB_RESUME (i.e., initialization
1057 * or any sort of reset), every port should be disabled.
1058 * Unconnected ports should likewise be disabled (paranoia),
1059 * and so should ports for which we have no usb_device.
1060 */
1061 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1062 type != HUB_RESUME ||
1063 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1064 !udev ||
1065 udev->state == USB_STATE_NOTATTACHED)) {
9f0a6cd3
AX
1066 /*
1067 * USB3 protocol ports will automatically transition
1068 * to Enabled state when detect an USB3.0 device attach.
1069 * Do not disable USB3 protocol ports.
9f0a6cd3 1070 */
131dec34 1071 if (!hub_is_superspeed(hdev)) {
9f0a6cd3
AX
1072 clear_port_feature(hdev, port1,
1073 USB_PORT_FEAT_ENABLE);
1074 portstatus &= ~USB_PORT_STAT_ENABLE;
85f0ff46
SS
1075 } else {
1076 /* Pretend that power was lost for USB3 devs */
1077 portstatus &= ~USB_PORT_STAT_ENABLE;
9f0a6cd3 1078 }
5e6effae
AS
1079 }
1080
948fea37
AS
1081 /* Clear status-change flags; we'll debounce later */
1082 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1083 need_debounce_delay = true;
1084 clear_port_feature(hub->hdev, port1,
1085 USB_PORT_FEAT_C_CONNECTION);
1086 }
1087 if (portchange & USB_PORT_STAT_C_ENABLE) {
1088 need_debounce_delay = true;
1089 clear_port_feature(hub->hdev, port1,
1090 USB_PORT_FEAT_C_ENABLE);
1091 }
79c3dd81
DZ
1092 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1093 hub_is_superspeed(hub->hdev)) {
1094 need_debounce_delay = true;
1095 clear_port_feature(hub->hdev, port1,
1096 USB_PORT_FEAT_C_BH_PORT_RESET);
1097 }
253e0572
AS
1098 /* We can forget about a "removed" device when there's a
1099 * physical disconnect or the connect status changes.
1100 */
1101 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1102 (portchange & USB_PORT_STAT_C_CONNECTION))
1103 clear_bit(port1, hub->removed_bits);
1104
6ee0b270
AS
1105 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1106 /* Tell khubd to disconnect the device or
1107 * check for a new connection
1108 */
1109 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1110 set_bit(port1, hub->change_bits);
1111
1112 } else if (portstatus & USB_PORT_STAT_ENABLE) {
72937e1e
SS
1113 bool port_resumed = (portstatus &
1114 USB_PORT_STAT_LINK_STATE) ==
1115 USB_SS_PORT_LS_U0;
6ee0b270
AS
1116 /* The power session apparently survived the resume.
1117 * If there was an overcurrent or suspend change
1118 * (i.e., remote wakeup request), have khubd
72937e1e
SS
1119 * take care of it. Look at the port link state
1120 * for USB 3.0 hubs, since they don't have a suspend
1121 * change bit, and they don't set the port link change
1122 * bit on device-initiated resume.
6ee0b270 1123 */
72937e1e
SS
1124 if (portchange || (hub_is_superspeed(hub->hdev) &&
1125 port_resumed))
6ee0b270 1126 set_bit(port1, hub->change_bits);
5e6effae 1127
6ee0b270 1128 } else if (udev->persist_enabled) {
6ee0b270 1129#ifdef CONFIG_PM
5e6effae 1130 udev->reset_resume = 1;
6ee0b270 1131#endif
8808f00c
AS
1132 set_bit(port1, hub->change_bits);
1133
6ee0b270
AS
1134 } else {
1135 /* The power session is gone; tell khubd */
1136 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1137 set_bit(port1, hub->change_bits);
5e6effae 1138 }
5e6effae
AS
1139 }
1140
948fea37
AS
1141 /* If no port-status-change flags were set, we don't need any
1142 * debouncing. If flags were set we can try to debounce the
1143 * ports all at once right now, instead of letting khubd do them
1144 * one at a time later on.
1145 *
1146 * If any port-status changes do occur during this delay, khubd
1147 * will see them later and handle them normally.
1148 */
8520f380
AS
1149 if (need_debounce_delay) {
1150 delay = HUB_DEBOUNCE_STABLE;
1151
1152 /* Don't do a long sleep inside a workqueue routine */
1153 if (type == HUB_INIT2) {
1154 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
1155 schedule_delayed_work(&hub->init_work,
1156 msecs_to_jiffies(delay));
1157 return; /* Continues at init3: below */
1158 } else {
1159 msleep(delay);
1160 }
1161 }
1162 init3:
f2835219
AS
1163 hub->quiescing = 0;
1164
1165 status = usb_submit_urb(hub->urb, GFP_NOIO);
1166 if (status < 0)
1167 dev_err(hub->intfdev, "activate --> %d\n", status);
1168 if (hub->has_indicators && blinkenlights)
1169 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1170
1171 /* Scan all ports that need attention */
1172 kick_khubd(hub);
8e4ceb38
AS
1173
1174 /* Allow autosuspend if it was suppressed */
1175 if (type <= HUB_INIT3)
1176 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
5e6effae
AS
1177}
1178
8520f380
AS
1179/* Implement the continuations for the delays above */
1180static void hub_init_func2(struct work_struct *ws)
1181{
1182 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1183
1184 hub_activate(hub, HUB_INIT2);
1185}
1186
1187static void hub_init_func3(struct work_struct *ws)
1188{
1189 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1190
1191 hub_activate(hub, HUB_INIT3);
1192}
1193
4330354f
AS
1194enum hub_quiescing_type {
1195 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1196};
1197
1198static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1199{
1200 struct usb_device *hdev = hub->hdev;
1201 int i;
1202
8520f380
AS
1203 cancel_delayed_work_sync(&hub->init_work);
1204
4330354f
AS
1205 /* khubd and related activity won't re-trigger */
1206 hub->quiescing = 1;
1207
1208 if (type != HUB_SUSPEND) {
1209 /* Disconnect all the children */
1210 for (i = 0; i < hdev->maxchild; ++i) {
ff823c79
LT
1211 if (hub->ports[i]->child)
1212 usb_disconnect(&hub->ports[i]->child);
4330354f
AS
1213 }
1214 }
1215
1216 /* Stop khubd and related activity */
1217 usb_kill_urb(hub->urb);
1218 if (hub->has_indicators)
1219 cancel_delayed_work_sync(&hub->leds);
1220 if (hub->tt.hub)
036546bf 1221 flush_work(&hub->tt.clear_work);
4330354f
AS
1222}
1223
3eb14915
AS
1224/* caller has locked the hub device */
1225static int hub_pre_reset(struct usb_interface *intf)
1226{
1227 struct usb_hub *hub = usb_get_intfdata(intf);
1228
4330354f 1229 hub_quiesce(hub, HUB_PRE_RESET);
f07600cf 1230 return 0;
7d069b7d
AS
1231}
1232
1233/* caller has locked the hub device */
f07600cf 1234static int hub_post_reset(struct usb_interface *intf)
7d069b7d 1235{
7de18d8b
AS
1236 struct usb_hub *hub = usb_get_intfdata(intf);
1237
f2835219 1238 hub_activate(hub, HUB_POST_RESET);
f07600cf 1239 return 0;
7d069b7d
AS
1240}
1241
fa2a9566
LT
1242static void usb_port_device_release(struct device *dev)
1243{
1244 struct usb_port *port_dev = to_usb_port(dev);
1245
1246 kfree(port_dev);
1247}
1248
1249static void usb_hub_remove_port_device(struct usb_hub *hub,
1250 int port1)
1251{
1252 device_unregister(&hub->ports[port1 - 1]->dev);
1253}
1254
1255struct device_type usb_port_device_type = {
1256 .name = "usb_port",
1257 .release = usb_port_device_release,
1258};
1259
1260static int usb_hub_create_port_device(struct usb_hub *hub,
1261 int port1)
1262{
1263 struct usb_port *port_dev = NULL;
1264 int retval;
1265
1266 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
1267 if (!port_dev) {
1268 retval = -ENOMEM;
1269 goto exit;
1270 }
1271
1272 hub->ports[port1 - 1] = port_dev;
1273 port_dev->dev.parent = hub->intfdev;
1274 port_dev->dev.type = &usb_port_device_type;
1275 dev_set_name(&port_dev->dev, "port%d", port1);
1276
1277 retval = device_register(&port_dev->dev);
1278 if (retval)
1279 goto error_register;
1280 return 0;
1281
1282error_register:
1283 put_device(&port_dev->dev);
1284exit:
1285 return retval;
1286}
1287
1da177e4
LT
1288static int hub_configure(struct usb_hub *hub,
1289 struct usb_endpoint_descriptor *endpoint)
1290{
b356b7c7 1291 struct usb_hcd *hcd;
1da177e4
LT
1292 struct usb_device *hdev = hub->hdev;
1293 struct device *hub_dev = hub->intfdev;
1294 u16 hubstatus, hubchange;
74ad9bd2 1295 u16 wHubCharacteristics;
1da177e4 1296 unsigned int pipe;
fa2a9566 1297 int maxp, ret, i;
7cbe5dca 1298 char *message = "out of memory";
1da177e4 1299
d697cdda 1300 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
1da177e4 1301 if (!hub->buffer) {
1da177e4
LT
1302 ret = -ENOMEM;
1303 goto fail;
1304 }
1305
1306 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1307 if (!hub->status) {
1da177e4
LT
1308 ret = -ENOMEM;
1309 goto fail;
1310 }
db90e7a1 1311 mutex_init(&hub->status_mutex);
1da177e4
LT
1312
1313 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1314 if (!hub->descriptor) {
1da177e4
LT
1315 ret = -ENOMEM;
1316 goto fail;
1317 }
1318
1319 /* Request the entire hub descriptor.
1320 * hub->descriptor can handle USB_MAXCHILDREN ports,
1321 * but the hub can/will return fewer bytes here.
1322 */
dbe79bbe 1323 ret = get_hub_descriptor(hdev, hub->descriptor);
1da177e4
LT
1324 if (ret < 0) {
1325 message = "can't read hub descriptor";
1326 goto fail;
1327 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1328 message = "hub has too many ports!";
1329 ret = -ENODEV;
1330 goto fail;
1331 }
1332
1333 hdev->maxchild = hub->descriptor->bNbrPorts;
1334 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
1335 (hdev->maxchild == 1) ? "" : "s");
1336
fa2a9566
LT
1337 hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *),
1338 GFP_KERNEL);
ff823c79 1339 if (!hub->ports) {
7cbe5dca
AS
1340 ret = -ENOMEM;
1341 goto fail;
1342 }
1343
74ad9bd2 1344 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
1da177e4 1345
dbe79bbe
JY
1346 /* FIXME for USB 3.0, skip for now */
1347 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1348 !(hub_is_superspeed(hdev))) {
1da177e4
LT
1349 int i;
1350 char portstr [USB_MAXCHILDREN + 1];
1351
1352 for (i = 0; i < hdev->maxchild; i++)
dbe79bbe 1353 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
1da177e4
LT
1354 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1355 ? 'F' : 'R';
1356 portstr[hdev->maxchild] = 0;
1357 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1358 } else
1359 dev_dbg(hub_dev, "standalone hub\n");
1360
74ad9bd2 1361 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
7bf01185
AD
1362 case HUB_CHAR_COMMON_LPSM:
1363 dev_dbg(hub_dev, "ganged power switching\n");
1364 break;
1365 case HUB_CHAR_INDV_PORT_LPSM:
1366 dev_dbg(hub_dev, "individual port power switching\n");
1367 break;
1368 case HUB_CHAR_NO_LPSM:
1369 case HUB_CHAR_LPSM:
1370 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1371 break;
1da177e4
LT
1372 }
1373
74ad9bd2 1374 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
7bf01185
AD
1375 case HUB_CHAR_COMMON_OCPM:
1376 dev_dbg(hub_dev, "global over-current protection\n");
1377 break;
1378 case HUB_CHAR_INDV_PORT_OCPM:
1379 dev_dbg(hub_dev, "individual port over-current protection\n");
1380 break;
1381 case HUB_CHAR_NO_OCPM:
1382 case HUB_CHAR_OCPM:
1383 dev_dbg(hub_dev, "no over-current protection\n");
1384 break;
1da177e4
LT
1385 }
1386
1387 spin_lock_init (&hub->tt.lock);
1388 INIT_LIST_HEAD (&hub->tt.clear_list);
cb88a1b8 1389 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
1da177e4 1390 switch (hdev->descriptor.bDeviceProtocol) {
7bf01185
AD
1391 case USB_HUB_PR_FS:
1392 break;
1393 case USB_HUB_PR_HS_SINGLE_TT:
1394 dev_dbg(hub_dev, "Single TT\n");
1395 hub->tt.hub = hdev;
1396 break;
1397 case USB_HUB_PR_HS_MULTI_TT:
1398 ret = usb_set_interface(hdev, 0, 1);
1399 if (ret == 0) {
1400 dev_dbg(hub_dev, "TT per port\n");
1401 hub->tt.multi = 1;
1402 } else
1403 dev_err(hub_dev, "Using single TT (err %d)\n",
1404 ret);
1405 hub->tt.hub = hdev;
1406 break;
1407 case USB_HUB_PR_SS:
1408 /* USB 3.0 hubs don't have a TT */
1409 break;
1410 default:
1411 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1412 hdev->descriptor.bDeviceProtocol);
1413 break;
1da177e4
LT
1414 }
1415
e09711ae 1416 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
74ad9bd2 1417 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
e09711ae
DB
1418 case HUB_TTTT_8_BITS:
1419 if (hdev->descriptor.bDeviceProtocol != 0) {
1420 hub->tt.think_time = 666;
1421 dev_dbg(hub_dev, "TT requires at most %d "
1422 "FS bit times (%d ns)\n",
1423 8, hub->tt.think_time);
1424 }
1da177e4 1425 break;
e09711ae
DB
1426 case HUB_TTTT_16_BITS:
1427 hub->tt.think_time = 666 * 2;
1428 dev_dbg(hub_dev, "TT requires at most %d "
1429 "FS bit times (%d ns)\n",
1430 16, hub->tt.think_time);
1da177e4 1431 break;
e09711ae
DB
1432 case HUB_TTTT_24_BITS:
1433 hub->tt.think_time = 666 * 3;
1434 dev_dbg(hub_dev, "TT requires at most %d "
1435 "FS bit times (%d ns)\n",
1436 24, hub->tt.think_time);
1da177e4 1437 break;
e09711ae
DB
1438 case HUB_TTTT_32_BITS:
1439 hub->tt.think_time = 666 * 4;
1440 dev_dbg(hub_dev, "TT requires at most %d "
1441 "FS bit times (%d ns)\n",
1442 32, hub->tt.think_time);
1da177e4
LT
1443 break;
1444 }
1445
1446 /* probe() zeroes hub->indicator[] */
74ad9bd2 1447 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
1da177e4
LT
1448 hub->has_indicators = 1;
1449 dev_dbg(hub_dev, "Port indicators are supported\n");
1450 }
1451
1452 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1453 hub->descriptor->bPwrOn2PwrGood * 2);
1454
1455 /* power budgeting mostly matters with bus-powered hubs,
1456 * and battery-powered root hubs (may provide just 8 mA).
1457 */
1458 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
55c52718 1459 if (ret < 2) {
1da177e4
LT
1460 message = "can't get hub status";
1461 goto fail;
1462 }
7d35b929
AS
1463 le16_to_cpus(&hubstatus);
1464 if (hdev == hdev->bus->root_hub) {
55c52718
AS
1465 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1466 hub->mA_per_port = 500;
1467 else {
1468 hub->mA_per_port = hdev->bus_mA;
1469 hub->limited_power = 1;
1470 }
7d35b929 1471 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
1da177e4
LT
1472 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1473 hub->descriptor->bHubContrCurrent);
55c52718
AS
1474 hub->limited_power = 1;
1475 if (hdev->maxchild > 0) {
1476 int remaining = hdev->bus_mA -
1477 hub->descriptor->bHubContrCurrent;
1478
1479 if (remaining < hdev->maxchild * 100)
1480 dev_warn(hub_dev,
1481 "insufficient power available "
1482 "to use all downstream ports\n");
1483 hub->mA_per_port = 100; /* 7.2.1.1 */
1484 }
1485 } else { /* Self-powered external hub */
1486 /* FIXME: What about battery-powered external hubs that
1487 * provide less current per port? */
1488 hub->mA_per_port = 500;
7d35b929 1489 }
55c52718
AS
1490 if (hub->mA_per_port < 500)
1491 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1492 hub->mA_per_port);
1da177e4 1493
b356b7c7
SS
1494 /* Update the HCD's internal representation of this hub before khubd
1495 * starts getting port status changes for devices under the hub.
1496 */
1497 hcd = bus_to_hcd(hdev->bus);
1498 if (hcd->driver->update_hub_device) {
1499 ret = hcd->driver->update_hub_device(hcd, hdev,
1500 &hub->tt, GFP_KERNEL);
1501 if (ret < 0) {
1502 message = "can't update HCD hub info";
1503 goto fail;
1504 }
1505 }
1506
1da177e4
LT
1507 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1508 if (ret < 0) {
1509 message = "can't get hub status";
1510 goto fail;
1511 }
1512
1513 /* local power status reports aren't always correct */
1514 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1515 dev_dbg(hub_dev, "local power source is %s\n",
1516 (hubstatus & HUB_STATUS_LOCAL_POWER)
1517 ? "lost (inactive)" : "good");
1518
74ad9bd2 1519 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
1da177e4
LT
1520 dev_dbg(hub_dev, "%sover-current condition exists\n",
1521 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1522
88fafff9 1523 /* set up the interrupt endpoint
1524 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1525 * bytes as USB2.0[11.12.3] says because some hubs are known
1526 * to send more data (and thus cause overflow). For root hubs,
1527 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1528 * to be big enough for at least USB_MAXCHILDREN ports. */
1da177e4
LT
1529 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1530 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1531
1532 if (maxp > sizeof(*hub->buffer))
1533 maxp = sizeof(*hub->buffer);
1534
1535 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1536 if (!hub->urb) {
1da177e4
LT
1537 ret = -ENOMEM;
1538 goto fail;
1539 }
1540
1541 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1542 hub, endpoint->bInterval);
1da177e4
LT
1543
1544 /* maybe cycle the hub leds */
1545 if (hub->has_indicators && blinkenlights)
1546 hub->indicator [0] = INDICATOR_CYCLE;
1547
fa2a9566
LT
1548 for (i = 0; i < hdev->maxchild; i++)
1549 if (usb_hub_create_port_device(hub, i + 1) < 0)
1550 dev_err(hub->intfdev,
1551 "couldn't create port%d device.\n", i + 1);
1552
f2835219 1553 hub_activate(hub, HUB_INIT);
1da177e4
LT
1554 return 0;
1555
1556fail:
1557 dev_err (hub_dev, "config failed, %s (err %d)\n",
1558 message, ret);
1559 /* hub_disconnect() frees urb and descriptor */
1560 return ret;
1561}
1562
e8054854
AS
1563static void hub_release(struct kref *kref)
1564{
1565 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1566
1567 usb_put_intf(to_usb_interface(hub->intfdev));
1568 kfree(hub);
1569}
1570
1da177e4
LT
1571static unsigned highspeed_hubs;
1572
1573static void hub_disconnect(struct usb_interface *intf)
1574{
8816230e 1575 struct usb_hub *hub = usb_get_intfdata(intf);
fa286188 1576 struct usb_device *hdev = interface_to_usbdev(intf);
fa2a9566
LT
1577 int i;
1578
e8054854
AS
1579 /* Take the hub off the event list and don't let it be added again */
1580 spin_lock_irq(&hub_event_lock);
8e4ceb38
AS
1581 if (!list_empty(&hub->event_list)) {
1582 list_del_init(&hub->event_list);
1583 usb_autopm_put_interface_no_suspend(intf);
1584 }
e8054854
AS
1585 hub->disconnected = 1;
1586 spin_unlock_irq(&hub_event_lock);
1da177e4 1587
7de18d8b
AS
1588 /* Disconnect all children and quiesce the hub */
1589 hub->error = 0;
4330354f 1590 hub_quiesce(hub, HUB_DISCONNECT);
7de18d8b 1591
8b28c752 1592 usb_set_intfdata (intf, NULL);
1f2235b8
AS
1593
1594 for (i = 0; i < hdev->maxchild; i++)
1595 usb_hub_remove_port_device(hub, i + 1);
7cbe5dca 1596 hub->hdev->maxchild = 0;
1da177e4 1597
e8054854 1598 if (hub->hdev->speed == USB_SPEED_HIGH)
1da177e4
LT
1599 highspeed_hubs--;
1600
1da177e4 1601 usb_free_urb(hub->urb);
fa2a9566 1602 kfree(hub->ports);
1bc3c9e1 1603 kfree(hub->descriptor);
1bc3c9e1 1604 kfree(hub->status);
d697cdda 1605 kfree(hub->buffer);
1da177e4 1606
e8054854 1607 kref_put(&hub->kref, hub_release);
1da177e4
LT
1608}
1609
1610static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1611{
1612 struct usb_host_interface *desc;
1613 struct usb_endpoint_descriptor *endpoint;
1614 struct usb_device *hdev;
1615 struct usb_hub *hub;
1616
1617 desc = intf->cur_altsetting;
1618 hdev = interface_to_usbdev(intf);
1619
596d789a
ML
1620 /*
1621 * Set default autosuspend delay as 0 to speedup bus suspend,
1622 * based on the below considerations:
1623 *
1624 * - Unlike other drivers, the hub driver does not rely on the
1625 * autosuspend delay to provide enough time to handle a wakeup
1626 * event, and the submitted status URB is just to check future
1627 * change on hub downstream ports, so it is safe to do it.
1628 *
1629 * - The patch might cause one or more auto supend/resume for
1630 * below very rare devices when they are plugged into hub
1631 * first time:
1632 *
1633 * devices having trouble initializing, and disconnect
1634 * themselves from the bus and then reconnect a second
1635 * or so later
1636 *
1637 * devices just for downloading firmware, and disconnects
1638 * themselves after completing it
1639 *
1640 * For these quite rare devices, their drivers may change the
1641 * autosuspend delay of their parent hub in the probe() to one
1642 * appropriate value to avoid the subtle problem if someone
1643 * does care it.
1644 *
1645 * - The patch may cause one or more auto suspend/resume on
1646 * hub during running 'lsusb', but it is probably too
1647 * infrequent to worry about.
1648 *
1649 * - Change autosuspend delay of hub can avoid unnecessary auto
1650 * suspend timer for hub, also may decrease power consumption
1651 * of USB bus.
1652 */
1653 pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
1654
2839f5bc
SS
1655 /* Hubs have proper suspend/resume support. */
1656 usb_enable_autosuspend(hdev);
088f7fec 1657
38f3ad5e 1658 if (hdev->level == MAX_TOPO_LEVEL) {
b9cef6c3
WF
1659 dev_err(&intf->dev,
1660 "Unsupported bus topology: hub nested too deep\n");
38f3ad5e
FB
1661 return -E2BIG;
1662 }
1663
89ccbdc9
DB
1664#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1665 if (hdev->parent) {
1666 dev_warn(&intf->dev, "ignoring external hub\n");
1667 return -ENODEV;
1668 }
1669#endif
1670
1da177e4
LT
1671 /* Some hubs have a subclass of 1, which AFAICT according to the */
1672 /* specs is not defined, but it works */
1673 if ((desc->desc.bInterfaceSubClass != 0) &&
1674 (desc->desc.bInterfaceSubClass != 1)) {
1675descriptor_error:
1676 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1677 return -EIO;
1678 }
1679
1680 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1681 if (desc->desc.bNumEndpoints != 1)
1682 goto descriptor_error;
1683
1684 endpoint = &desc->endpoint[0].desc;
1685
fbf81c29
LFC
1686 /* If it's not an interrupt in endpoint, we'd better punt! */
1687 if (!usb_endpoint_is_int_in(endpoint))
1da177e4
LT
1688 goto descriptor_error;
1689
1690 /* We found a hub */
1691 dev_info (&intf->dev, "USB hub found\n");
1692
0a1ef3b5 1693 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
1da177e4
LT
1694 if (!hub) {
1695 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1696 return -ENOMEM;
1697 }
1698
e8054854 1699 kref_init(&hub->kref);
1da177e4
LT
1700 INIT_LIST_HEAD(&hub->event_list);
1701 hub->intfdev = &intf->dev;
1702 hub->hdev = hdev;
c4028958 1703 INIT_DELAYED_WORK(&hub->leds, led_work);
8520f380 1704 INIT_DELAYED_WORK(&hub->init_work, NULL);
e8054854 1705 usb_get_intf(intf);
1da177e4
LT
1706
1707 usb_set_intfdata (intf, hub);
40f122f3 1708 intf->needs_remote_wakeup = 1;
1da177e4
LT
1709
1710 if (hdev->speed == USB_SPEED_HIGH)
1711 highspeed_hubs++;
1712
e6f30dea
ML
1713 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1714 hub->quirk_check_port_auto_suspend = 1;
1715
1da177e4
LT
1716 if (hub_configure(hub, endpoint) >= 0)
1717 return 0;
1718
1719 hub_disconnect (intf);
1720 return -ENODEV;
1721}
1722
1723static int
1724hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1725{
1726 struct usb_device *hdev = interface_to_usbdev (intf);
ff823c79 1727 struct usb_hub *hub = hdev_to_hub(hdev);
1da177e4
LT
1728
1729 /* assert ifno == 0 (part of hub spec) */
1730 switch (code) {
1731 case USBDEVFS_HUB_PORTINFO: {
1732 struct usbdevfs_hub_portinfo *info = user_data;
1733 int i;
1734
1735 spin_lock_irq(&device_state_lock);
1736 if (hdev->devnum <= 0)
1737 info->nports = 0;
1738 else {
1739 info->nports = hdev->maxchild;
1740 for (i = 0; i < info->nports; i++) {
ff823c79 1741 if (hub->ports[i]->child == NULL)
1da177e4
LT
1742 info->port[i] = 0;
1743 else
1744 info->port[i] =
ff823c79 1745 hub->ports[i]->child->devnum;
1da177e4
LT
1746 }
1747 }
1748 spin_unlock_irq(&device_state_lock);
1749
1750 return info->nports + 1;
1751 }
1752
1753 default:
1754 return -ENOSYS;
1755 }
1756}
1757
7cbe5dca
AS
1758/*
1759 * Allow user programs to claim ports on a hub. When a device is attached
1760 * to one of these "claimed" ports, the program will "own" the device.
1761 */
1762static int find_port_owner(struct usb_device *hdev, unsigned port1,
336c5c31 1763 struct dev_state ***ppowner)
7cbe5dca
AS
1764{
1765 if (hdev->state == USB_STATE_NOTATTACHED)
1766 return -ENODEV;
1767 if (port1 == 0 || port1 > hdev->maxchild)
1768 return -EINVAL;
1769
1770 /* This assumes that devices not managed by the hub driver
1771 * will always have maxchild equal to 0.
1772 */
fa2a9566 1773 *ppowner = &(hdev_to_hub(hdev)->ports[port1 - 1]->port_owner);
7cbe5dca
AS
1774 return 0;
1775}
1776
1777/* In the following three functions, the caller must hold hdev's lock */
336c5c31
LT
1778int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1779 struct dev_state *owner)
7cbe5dca
AS
1780{
1781 int rc;
336c5c31 1782 struct dev_state **powner;
7cbe5dca
AS
1783
1784 rc = find_port_owner(hdev, port1, &powner);
1785 if (rc)
1786 return rc;
1787 if (*powner)
1788 return -EBUSY;
1789 *powner = owner;
1790 return rc;
1791}
1792
336c5c31
LT
1793int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1794 struct dev_state *owner)
7cbe5dca
AS
1795{
1796 int rc;
336c5c31 1797 struct dev_state **powner;
7cbe5dca
AS
1798
1799 rc = find_port_owner(hdev, port1, &powner);
1800 if (rc)
1801 return rc;
1802 if (*powner != owner)
1803 return -ENOENT;
1804 *powner = NULL;
1805 return rc;
1806}
1807
336c5c31 1808void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
7cbe5dca 1809{
fa2a9566 1810 struct usb_hub *hub = hdev_to_hub(hdev);
7cbe5dca 1811 int n;
7cbe5dca 1812
fa2a9566
LT
1813 for (n = 0; n < hdev->maxchild; n++) {
1814 if (hub->ports[n]->port_owner == owner)
1815 hub->ports[n]->port_owner = NULL;
7cbe5dca 1816 }
fa2a9566 1817
7cbe5dca
AS
1818}
1819
1820/* The caller must hold udev's lock */
1821bool usb_device_is_owned(struct usb_device *udev)
1822{
1823 struct usb_hub *hub;
1824
1825 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1826 return false;
1827 hub = hdev_to_hub(udev->parent);
fa2a9566 1828 return !!hub->ports[udev->portnum - 1]->port_owner;
7cbe5dca
AS
1829}
1830
1da177e4
LT
1831static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1832{
ff823c79 1833 struct usb_hub *hub = hdev_to_hub(udev);
1da177e4
LT
1834 int i;
1835
1836 for (i = 0; i < udev->maxchild; ++i) {
ff823c79
LT
1837 if (hub->ports[i]->child)
1838 recursively_mark_NOTATTACHED(hub->ports[i]->child);
1da177e4 1839 }
9bbdf1e0 1840 if (udev->state == USB_STATE_SUSPENDED)
15123006 1841 udev->active_duration -= jiffies;
1da177e4
LT
1842 udev->state = USB_STATE_NOTATTACHED;
1843}
1844
1845/**
1846 * usb_set_device_state - change a device's current state (usbcore, hcds)
1847 * @udev: pointer to device whose state should be changed
1848 * @new_state: new state value to be stored
1849 *
1850 * udev->state is _not_ fully protected by the device lock. Although
1851 * most transitions are made only while holding the lock, the state can
1852 * can change to USB_STATE_NOTATTACHED at almost any time. This
1853 * is so that devices can be marked as disconnected as soon as possible,
1854 * without having to wait for any semaphores to be released. As a result,
1855 * all changes to any device's state must be protected by the
1856 * device_state_lock spinlock.
1857 *
1858 * Once a device has been added to the device tree, all changes to its state
1859 * should be made using this routine. The state should _not_ be set directly.
1860 *
1861 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1862 * Otherwise udev->state is set to new_state, and if new_state is
1863 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1864 * to USB_STATE_NOTATTACHED.
1865 */
1866void usb_set_device_state(struct usb_device *udev,
1867 enum usb_device_state new_state)
1868{
1869 unsigned long flags;
4681b171 1870 int wakeup = -1;
1da177e4
LT
1871
1872 spin_lock_irqsave(&device_state_lock, flags);
1873 if (udev->state == USB_STATE_NOTATTACHED)
1874 ; /* do nothing */
b94dc6b5 1875 else if (new_state != USB_STATE_NOTATTACHED) {
fb669cc0
DB
1876
1877 /* root hub wakeup capabilities are managed out-of-band
1878 * and may involve silicon errata ... ignore them here.
1879 */
1880 if (udev->parent) {
645daaab
AS
1881 if (udev->state == USB_STATE_SUSPENDED
1882 || new_state == USB_STATE_SUSPENDED)
1883 ; /* No change to wakeup settings */
1884 else if (new_state == USB_STATE_CONFIGURED)
4681b171
RW
1885 wakeup = udev->actconfig->desc.bmAttributes
1886 & USB_CONFIG_ATT_WAKEUP;
645daaab 1887 else
4681b171 1888 wakeup = 0;
fb669cc0 1889 }
15123006
SS
1890 if (udev->state == USB_STATE_SUSPENDED &&
1891 new_state != USB_STATE_SUSPENDED)
1892 udev->active_duration -= jiffies;
1893 else if (new_state == USB_STATE_SUSPENDED &&
1894 udev->state != USB_STATE_SUSPENDED)
1895 udev->active_duration += jiffies;
645daaab 1896 udev->state = new_state;
b94dc6b5 1897 } else
1da177e4
LT
1898 recursively_mark_NOTATTACHED(udev);
1899 spin_unlock_irqrestore(&device_state_lock, flags);
4681b171
RW
1900 if (wakeup >= 0)
1901 device_set_wakeup_capable(&udev->dev, wakeup);
1da177e4 1902}
6da9c990 1903EXPORT_SYMBOL_GPL(usb_set_device_state);
1da177e4 1904
8af548dc 1905/*
3b29b68b
AS
1906 * Choose a device number.
1907 *
1908 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1909 * USB-2.0 buses they are also used as device addresses, however on
1910 * USB-3.0 buses the address is assigned by the controller hardware
1911 * and it usually is not the same as the device number.
1912 *
8af548dc
IPG
1913 * WUSB devices are simple: they have no hubs behind, so the mapping
1914 * device <-> virtual port number becomes 1:1. Why? to simplify the
1915 * life of the device connection logic in
1916 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1917 * handshake we need to assign a temporary address in the unauthorized
1918 * space. For simplicity we use the first virtual port number found to
1919 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1920 * and that becomes it's address [X < 128] or its unauthorized address
1921 * [X | 0x80].
1922 *
1923 * We add 1 as an offset to the one-based USB-stack port number
1924 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1925 * 0 is reserved by USB for default address; (b) Linux's USB stack
1926 * uses always #1 for the root hub of the controller. So USB stack's
1927 * port #1, which is wusb virtual-port #0 has address #2.
c6515272
SS
1928 *
1929 * Devices connected under xHCI are not as simple. The host controller
1930 * supports virtualization, so the hardware assigns device addresses and
1931 * the HCD must setup data structures before issuing a set address
1932 * command to the hardware.
8af548dc 1933 */
3b29b68b 1934static void choose_devnum(struct usb_device *udev)
1da177e4
LT
1935{
1936 int devnum;
1937 struct usb_bus *bus = udev->bus;
1938
1939 /* If khubd ever becomes multithreaded, this will need a lock */
8af548dc
IPG
1940 if (udev->wusb) {
1941 devnum = udev->portnum + 1;
1942 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1943 } else {
1944 /* Try to allocate the next devnum beginning at
1945 * bus->devnum_next. */
1946 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1947 bus->devnum_next);
1948 if (devnum >= 128)
1949 devnum = find_next_zero_bit(bus->devmap.devicemap,
1950 128, 1);
1951 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1952 }
1da177e4
LT
1953 if (devnum < 128) {
1954 set_bit(devnum, bus->devmap.devicemap);
1955 udev->devnum = devnum;
1956 }
1957}
1958
3b29b68b 1959static void release_devnum(struct usb_device *udev)
1da177e4
LT
1960{
1961 if (udev->devnum > 0) {
1962 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1963 udev->devnum = -1;
1964 }
1965}
1966
3b29b68b 1967static void update_devnum(struct usb_device *udev, int devnum)
4953d141
DV
1968{
1969 /* The address for a WUSB device is managed by wusbcore. */
1970 if (!udev->wusb)
1971 udev->devnum = devnum;
1972}
1973
f7410ced
HX
1974static void hub_free_dev(struct usb_device *udev)
1975{
1976 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1977
1978 /* Root hubs aren't real devices, so don't free HCD resources */
1979 if (hcd->driver->free_dev && udev->parent)
1980 hcd->driver->free_dev(hcd, udev);
1981}
1982
1da177e4
LT
1983/**
1984 * usb_disconnect - disconnect a device (usbcore-internal)
1985 * @pdev: pointer to device being disconnected
1986 * Context: !in_interrupt ()
1987 *
1988 * Something got disconnected. Get rid of it and all of its children.
1989 *
1990 * If *pdev is a normal device then the parent hub must already be locked.
1991 * If *pdev is a root hub then this routine will acquire the
1992 * usb_bus_list_lock on behalf of the caller.
1993 *
1994 * Only hub drivers (including virtual root hub drivers for host
1995 * controllers) should ever call this.
1996 *
1997 * This call is synchronous, and may not be used in an interrupt context.
1998 */
1999void usb_disconnect(struct usb_device **pdev)
2000{
2001 struct usb_device *udev = *pdev;
ff823c79 2002 struct usb_hub *hub = hdev_to_hub(udev);
1da177e4
LT
2003 int i;
2004
1da177e4
LT
2005 /* mark the device as inactive, so any further urb submissions for
2006 * this device (and any of its children) will fail immediately.
25985edc 2007 * this quiesces everything except pending urbs.
1da177e4
LT
2008 */
2009 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
3b29b68b
AS
2010 dev_info(&udev->dev, "USB disconnect, device number %d\n",
2011 udev->devnum);
1da177e4 2012
9ad3d6cc
AS
2013 usb_lock_device(udev);
2014
1da177e4 2015 /* Free up all the children before we remove this device */
8816230e 2016 for (i = 0; i < udev->maxchild; i++) {
ff823c79
LT
2017 if (hub->ports[i]->child)
2018 usb_disconnect(&hub->ports[i]->child);
1da177e4
LT
2019 }
2020
2021 /* deallocate hcd/hardware state ... nuking all pending urbs and
2022 * cleaning up all state associated with the current configuration
2023 * so that the hardware is now fully quiesced.
2024 */
782da727 2025 dev_dbg (&udev->dev, "unregistering device\n");
1da177e4 2026 usb_disable_device(udev, 0);
cde217a5 2027 usb_hcd_synchronize_unlinks(udev);
1da177e4 2028
3b23dd6f 2029 usb_remove_ep_devs(&udev->ep0);
782da727
AS
2030 usb_unlock_device(udev);
2031
2032 /* Unregister the device. The device driver is responsible
3b23dd6f
AS
2033 * for de-configuring the device and invoking the remove-device
2034 * notifier chain (used by usbfs and possibly others).
782da727
AS
2035 */
2036 device_del(&udev->dev);
3099e75a 2037
782da727 2038 /* Free the device number and delete the parent's children[]
1da177e4
LT
2039 * (or root_hub) pointer.
2040 */
3b29b68b 2041 release_devnum(udev);
1da177e4
LT
2042
2043 /* Avoid races with recursively_mark_NOTATTACHED() */
2044 spin_lock_irq(&device_state_lock);
2045 *pdev = NULL;
2046 spin_unlock_irq(&device_state_lock);
2047
f7410ced
HX
2048 hub_free_dev(udev);
2049
782da727 2050 put_device(&udev->dev);
1da177e4
LT
2051}
2052
f2a383e4 2053#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
1da177e4
LT
2054static void show_string(struct usb_device *udev, char *id, char *string)
2055{
2056 if (!string)
2057 return;
2058 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
2059}
2060
f2a383e4
GKH
2061static void announce_device(struct usb_device *udev)
2062{
2063 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2064 le16_to_cpu(udev->descriptor.idVendor),
2065 le16_to_cpu(udev->descriptor.idProduct));
b9cef6c3
WF
2066 dev_info(&udev->dev,
2067 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
f2a383e4
GKH
2068 udev->descriptor.iManufacturer,
2069 udev->descriptor.iProduct,
2070 udev->descriptor.iSerialNumber);
2071 show_string(udev, "Product", udev->product);
2072 show_string(udev, "Manufacturer", udev->manufacturer);
2073 show_string(udev, "SerialNumber", udev->serial);
2074}
1da177e4 2075#else
f2a383e4 2076static inline void announce_device(struct usb_device *udev) { }
1da177e4
LT
2077#endif
2078
1da177e4
LT
2079#ifdef CONFIG_USB_OTG
2080#include "otg_whitelist.h"
2081#endif
2082
3ede760f 2083/**
8d8558d1 2084 * usb_enumerate_device_otg - FIXME (usbcore-internal)
3ede760f
ON
2085 * @udev: newly addressed device (in ADDRESS state)
2086 *
8d8558d1 2087 * Finish enumeration for On-The-Go devices
3ede760f 2088 */
8d8558d1 2089static int usb_enumerate_device_otg(struct usb_device *udev)
1da177e4 2090{
d9d16e8a 2091 int err = 0;
1da177e4
LT
2092
2093#ifdef CONFIG_USB_OTG
2094 /*
2095 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2096 * to wake us after we've powered off VBUS; and HNP, switching roles
2097 * "host" to "peripheral". The OTG descriptor helps figure this out.
2098 */
2099 if (!udev->bus->is_b_host
2100 && udev->config
2101 && udev->parent == udev->bus->root_hub) {
2eb5052e 2102 struct usb_otg_descriptor *desc = NULL;
1da177e4
LT
2103 struct usb_bus *bus = udev->bus;
2104
2105 /* descriptor may appear anywhere in config */
2106 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2107 le16_to_cpu(udev->config[0].desc.wTotalLength),
2108 USB_DT_OTG, (void **) &desc) == 0) {
2109 if (desc->bmAttributes & USB_OTG_HNP) {
12c3da34 2110 unsigned port1 = udev->portnum;
fc849b85 2111
1da177e4
LT
2112 dev_info(&udev->dev,
2113 "Dual-Role OTG device on %sHNP port\n",
2114 (port1 == bus->otg_port)
2115 ? "" : "non-");
2116
2117 /* enable HNP before suspend, it's simpler */
2118 if (port1 == bus->otg_port)
2119 bus->b_hnp_enable = 1;
2120 err = usb_control_msg(udev,
2121 usb_sndctrlpipe(udev, 0),
2122 USB_REQ_SET_FEATURE, 0,
2123 bus->b_hnp_enable
2124 ? USB_DEVICE_B_HNP_ENABLE
2125 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2126 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2127 if (err < 0) {
2128 /* OTG MESSAGE: report errors here,
2129 * customize to match your product.
2130 */
2131 dev_info(&udev->dev,
b9cef6c3 2132 "can't set HNP mode: %d\n",
1da177e4
LT
2133 err);
2134 bus->b_hnp_enable = 0;
2135 }
2136 }
2137 }
2138 }
2139
2140 if (!is_targeted(udev)) {
2141
2142 /* Maybe it can talk to us, though we can't talk to it.
2143 * (Includes HNP test device.)
2144 */
2145 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
634a84f8 2146 err = usb_port_suspend(udev, PMSG_SUSPEND);
1da177e4
LT
2147 if (err < 0)
2148 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2149 }
ffcdc18d 2150 err = -ENOTSUPP;
1da177e4
LT
2151 goto fail;
2152 }
d9d16e8a 2153fail:
1da177e4 2154#endif
d9d16e8a
IPG
2155 return err;
2156}
2157
2158
2159/**
8d8558d1 2160 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
d9d16e8a
IPG
2161 * @udev: newly addressed device (in ADDRESS state)
2162 *
2163 * This is only called by usb_new_device() and usb_authorize_device()
2164 * and FIXME -- all comments that apply to them apply here wrt to
2165 * environment.
2166 *
2167 * If the device is WUSB and not authorized, we don't attempt to read
2168 * the string descriptors, as they will be errored out by the device
2169 * until it has been authorized.
2170 */
8d8558d1 2171static int usb_enumerate_device(struct usb_device *udev)
d9d16e8a
IPG
2172{
2173 int err;
1da177e4 2174
d9d16e8a
IPG
2175 if (udev->config == NULL) {
2176 err = usb_get_configuration(udev);
2177 if (err < 0) {
2178 dev_err(&udev->dev, "can't read configurations, error %d\n",
2179 err);
80da2e0d 2180 return err;
d9d16e8a
IPG
2181 }
2182 }
2183 if (udev->wusb == 1 && udev->authorized == 0) {
2184 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2185 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2186 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2187 }
2188 else {
2189 /* read the standard strings and cache them if present */
2190 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2191 udev->manufacturer = usb_cache_string(udev,
2192 udev->descriptor.iManufacturer);
2193 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2194 }
8d8558d1 2195 err = usb_enumerate_device_otg(udev);
80da2e0d
LP
2196 if (err < 0)
2197 return err;
2198
2199 usb_detect_interface_quirks(udev);
2200
2201 return 0;
d9d16e8a
IPG
2202}
2203
d35e70d5
MG
2204static void set_usb_port_removable(struct usb_device *udev)
2205{
2206 struct usb_device *hdev = udev->parent;
2207 struct usb_hub *hub;
2208 u8 port = udev->portnum;
2209 u16 wHubCharacteristics;
2210 bool removable = true;
2211
2212 if (!hdev)
2213 return;
2214
2215 hub = hdev_to_hub(udev->parent);
2216
2217 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2218
2219 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2220 return;
2221
2222 if (hub_is_superspeed(hdev)) {
ca3c1539
LT
2223 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2224 & (1 << port))
d35e70d5
MG
2225 removable = false;
2226 } else {
2227 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2228 removable = false;
2229 }
2230
2231 if (removable)
2232 udev->removable = USB_DEVICE_REMOVABLE;
2233 else
2234 udev->removable = USB_DEVICE_FIXED;
2235}
d9d16e8a
IPG
2236
2237/**
2238 * usb_new_device - perform initial device setup (usbcore-internal)
2239 * @udev: newly addressed device (in ADDRESS state)
2240 *
8d8558d1
AS
2241 * This is called with devices which have been detected but not fully
2242 * enumerated. The device descriptor is available, but not descriptors
d9d16e8a
IPG
2243 * for any device configuration. The caller must have locked either
2244 * the parent hub (if udev is a normal device) or else the
2245 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2246 * udev has already been installed, but udev is not yet visible through
2247 * sysfs or other filesystem code.
2248 *
2249 * It will return if the device is configured properly or not. Zero if
2250 * the interface was registered with the driver core; else a negative
2251 * errno value.
2252 *
2253 * This call is synchronous, and may not be used in an interrupt context.
2254 *
2255 * Only the hub driver or root-hub registrar should ever call this.
2256 */
2257int usb_new_device(struct usb_device *udev)
2258{
2259 int err;
2260
16985408 2261 if (udev->parent) {
16985408
DS
2262 /* Initialize non-root-hub device wakeup to disabled;
2263 * device (un)configuration controls wakeup capable
2264 * sysfs power/wakeup controls wakeup enabled/disabled
2265 */
2266 device_init_wakeup(&udev->dev, 0);
16985408
DS
2267 }
2268
9bbdf1e0
AS
2269 /* Tell the runtime-PM framework the device is active */
2270 pm_runtime_set_active(&udev->dev);
c08512c7 2271 pm_runtime_get_noresume(&udev->dev);
fcc4a01e 2272 pm_runtime_use_autosuspend(&udev->dev);
9bbdf1e0
AS
2273 pm_runtime_enable(&udev->dev);
2274
c08512c7
AS
2275 /* By default, forbid autosuspend for all devices. It will be
2276 * allowed for hubs during binding.
2277 */
2278 usb_disable_autosuspend(udev);
2279
8d8558d1 2280 err = usb_enumerate_device(udev); /* Read descriptors */
d9d16e8a
IPG
2281 if (err < 0)
2282 goto fail;
c6515272
SS
2283 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2284 udev->devnum, udev->bus->busnum,
2285 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
9f8b17e6
KS
2286 /* export the usbdev device-node for libusb */
2287 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2288 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2289
6cd13201
AS
2290 /* Tell the world! */
2291 announce_device(udev);
195af2cc 2292
b04b3156
TT
2293 if (udev->serial)
2294 add_device_randomness(udev->serial, strlen(udev->serial));
2295 if (udev->product)
2296 add_device_randomness(udev->product, strlen(udev->product));
2297 if (udev->manufacturer)
2298 add_device_randomness(udev->manufacturer,
2299 strlen(udev->manufacturer));
2300
927bc916 2301 device_enable_async_suspend(&udev->dev);
d35e70d5
MG
2302
2303 /*
2304 * check whether the hub marks this port as non-removable. Do it
2305 * now so that platform-specific data can override it in
2306 * device_add()
2307 */
2308 if (udev->parent)
2309 set_usb_port_removable(udev);
2310
782da727 2311 /* Register the device. The device driver is responsible
3b23dd6f
AS
2312 * for configuring the device and invoking the add-device
2313 * notifier chain (used by usbfs and possibly others).
782da727 2314 */
9f8b17e6 2315 err = device_add(&udev->dev);
1da177e4
LT
2316 if (err) {
2317 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2318 goto fail;
2319 }
9ad3d6cc 2320
3b23dd6f 2321 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
c08512c7
AS
2322 usb_mark_last_busy(udev);
2323 pm_runtime_put_sync_autosuspend(&udev->dev);
c066475e 2324 return err;
1da177e4
LT
2325
2326fail:
2327 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
9bbdf1e0
AS
2328 pm_runtime_disable(&udev->dev);
2329 pm_runtime_set_suspended(&udev->dev);
d9d16e8a 2330 return err;
1da177e4
LT
2331}
2332
93993a0a
IPG
2333
2334/**
fd39c86b
RD
2335 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2336 * @usb_dev: USB device
2337 *
2338 * Move the USB device to a very basic state where interfaces are disabled
2339 * and the device is in fact unconfigured and unusable.
93993a0a
IPG
2340 *
2341 * We share a lock (that we have) with device_del(), so we need to
2342 * defer its call.
2343 */
2344int usb_deauthorize_device(struct usb_device *usb_dev)
2345{
93993a0a
IPG
2346 usb_lock_device(usb_dev);
2347 if (usb_dev->authorized == 0)
2348 goto out_unauthorized;
da307123 2349
93993a0a
IPG
2350 usb_dev->authorized = 0;
2351 usb_set_configuration(usb_dev, -1);
da307123
AS
2352
2353 kfree(usb_dev->product);
93993a0a 2354 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
da307123 2355 kfree(usb_dev->manufacturer);
93993a0a 2356 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
da307123 2357 kfree(usb_dev->serial);
93993a0a 2358 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
da307123
AS
2359
2360 usb_destroy_configuration(usb_dev);
93993a0a 2361 usb_dev->descriptor.bNumConfigurations = 0;
da307123 2362
93993a0a
IPG
2363out_unauthorized:
2364 usb_unlock_device(usb_dev);
2365 return 0;
2366}
2367
2368
2369int usb_authorize_device(struct usb_device *usb_dev)
2370{
2371 int result = 0, c;
da307123 2372
93993a0a
IPG
2373 usb_lock_device(usb_dev);
2374 if (usb_dev->authorized == 1)
2375 goto out_authorized;
da307123 2376
93993a0a
IPG
2377 result = usb_autoresume_device(usb_dev);
2378 if (result < 0) {
2379 dev_err(&usb_dev->dev,
2380 "can't autoresume for authorization: %d\n", result);
2381 goto error_autoresume;
2382 }
2383 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2384 if (result < 0) {
2385 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2386 "authorization: %d\n", result);
2387 goto error_device_descriptor;
2388 }
da307123
AS
2389
2390 kfree(usb_dev->product);
2391 usb_dev->product = NULL;
2392 kfree(usb_dev->manufacturer);
2393 usb_dev->manufacturer = NULL;
2394 kfree(usb_dev->serial);
2395 usb_dev->serial = NULL;
2396
93993a0a 2397 usb_dev->authorized = 1;
8d8558d1 2398 result = usb_enumerate_device(usb_dev);
93993a0a 2399 if (result < 0)
8d8558d1 2400 goto error_enumerate;
93993a0a
IPG
2401 /* Choose and set the configuration. This registers the interfaces
2402 * with the driver core and lets interface drivers bind to them.
2403 */
b5ea060f 2404 c = usb_choose_configuration(usb_dev);
93993a0a
IPG
2405 if (c >= 0) {
2406 result = usb_set_configuration(usb_dev, c);
2407 if (result) {
2408 dev_err(&usb_dev->dev,
2409 "can't set config #%d, error %d\n", c, result);
2410 /* This need not be fatal. The user can try to
2411 * set other configurations. */
2412 }
2413 }
2414 dev_info(&usb_dev->dev, "authorized to connect\n");
da307123 2415
8d8558d1 2416error_enumerate:
93993a0a 2417error_device_descriptor:
da307123 2418 usb_autosuspend_device(usb_dev);
93993a0a
IPG
2419error_autoresume:
2420out_authorized:
2421 usb_unlock_device(usb_dev); // complements locktree
2422 return result;
2423}
2424
2425
0165de09
IPG
2426/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2427static unsigned hub_is_wusb(struct usb_hub *hub)
2428{
2429 struct usb_hcd *hcd;
2430 if (hub->hdev->parent != NULL) /* not a root hub? */
2431 return 0;
2432 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2433 return hcd->wireless;
2434}
2435
2436
1da177e4
LT
2437#define PORT_RESET_TRIES 5
2438#define SET_ADDRESS_TRIES 2
2439#define GET_DESCRIPTOR_TRIES 2
2440#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
90ab5ee9 2441#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
1da177e4
LT
2442
2443#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2444#define HUB_SHORT_RESET_TIME 10
75d7cf72 2445#define HUB_BH_RESET_TIME 50
1da177e4
LT
2446#define HUB_LONG_RESET_TIME 200
2447#define HUB_RESET_TIMEOUT 500
2448
10d674a8
SS
2449static int hub_port_reset(struct usb_hub *hub, int port1,
2450 struct usb_device *udev, unsigned int delay, bool warm);
2451
8bea2bd3
SL
2452/* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2453 * Port worm reset is required to recover
2454 */
2455static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
10d674a8
SS
2456{
2457 return hub_is_superspeed(hub->hdev) &&
8bea2bd3
SL
2458 (((portstatus & USB_PORT_STAT_LINK_STATE) ==
2459 USB_SS_PORT_LS_SS_INACTIVE) ||
2460 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
2461 USB_SS_PORT_LS_COMP_MOD)) ;
10d674a8
SS
2462}
2463
1da177e4 2464static int hub_port_wait_reset(struct usb_hub *hub, int port1,
75d7cf72 2465 struct usb_device *udev, unsigned int delay, bool warm)
1da177e4
LT
2466{
2467 int delay_time, ret;
2468 u16 portstatus;
2469 u16 portchange;
2470
2471 for (delay_time = 0;
2472 delay_time < HUB_RESET_TIMEOUT;
2473 delay_time += delay) {
2474 /* wait to give the device a chance to reset */
2475 msleep(delay);
2476
2477 /* read and decode port status */
2478 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2479 if (ret < 0)
2480 return ret;
2481
75d7cf72
AX
2482 /*
2483 * Some buggy devices require a warm reset to be issued even
2484 * when the port appears not to be connected.
2485 */
2486 if (!warm) {
10d674a8
SS
2487 /*
2488 * Some buggy devices can cause an NEC host controller
2489 * to transition to the "Error" state after a hot port
2490 * reset. This will show up as the port state in
2491 * "Inactive", and the port may also report a
2492 * disconnect. Forcing a warm port reset seems to make
2493 * the device work.
2494 *
2495 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2496 */
8bea2bd3 2497 if (hub_port_warm_reset_required(hub, portstatus)) {
10d674a8
SS
2498 int ret;
2499
2500 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2501 clear_port_feature(hub->hdev, port1,
2502 USB_PORT_FEAT_C_CONNECTION);
2503 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2504 clear_port_feature(hub->hdev, port1,
2505 USB_PORT_FEAT_C_PORT_LINK_STATE);
2506 if (portchange & USB_PORT_STAT_C_RESET)
2507 clear_port_feature(hub->hdev, port1,
2508 USB_PORT_FEAT_C_RESET);
2509 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2510 port1);
2511 ret = hub_port_reset(hub, port1,
2512 udev, HUB_BH_RESET_TIME,
2513 true);
2514 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2515 clear_port_feature(hub->hdev, port1,
2516 USB_PORT_FEAT_C_CONNECTION);
2517 return ret;
2518 }
75d7cf72
AX
2519 /* Device went away? */
2520 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2521 return -ENOTCONN;
2522
2523 /* bomb out completely if the connection bounced */
2524 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2525 return -ENOTCONN;
2526
2527 /* if we`ve finished resetting, then break out of
2528 * the loop
2529 */
2530 if (!(portstatus & USB_PORT_STAT_RESET) &&
2531 (portstatus & USB_PORT_STAT_ENABLE)) {
2532 if (hub_is_wusb(hub))
2533 udev->speed = USB_SPEED_WIRELESS;
2534 else if (hub_is_superspeed(hub->hdev))
2535 udev->speed = USB_SPEED_SUPER;
2536 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2537 udev->speed = USB_SPEED_HIGH;
2538 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2539 udev->speed = USB_SPEED_LOW;
2540 else
2541 udev->speed = USB_SPEED_FULL;
2542 return 0;
2543 }
2544 } else {
2545 if (portchange & USB_PORT_STAT_C_BH_RESET)
2546 return 0;
1da177e4
LT
2547 }
2548
2549 /* switch to the long delay after two short delay failures */
2550 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2551 delay = HUB_LONG_RESET_TIME;
2552
2553 dev_dbg (hub->intfdev,
75d7cf72
AX
2554 "port %d not %sreset yet, waiting %dms\n",
2555 port1, warm ? "warm " : "", delay);
1da177e4
LT
2556 }
2557
2558 return -EBUSY;
2559}
2560
75d7cf72
AX
2561static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2562 struct usb_device *udev, int *status, bool warm)
2563{
2564 switch (*status) {
2565 case 0:
2566 if (!warm) {
2567 struct usb_hcd *hcd;
2568 /* TRSTRCY = 10 ms; plus some extra */
2569 msleep(10 + 40);
2570 update_devnum(udev, 0);
2571 hcd = bus_to_hcd(udev->bus);
2572 if (hcd->driver->reset_device) {
2573 *status = hcd->driver->reset_device(hcd, udev);
2574 if (*status < 0) {
2575 dev_err(&udev->dev, "Cannot reset "
2576 "HCD device state\n");
2577 break;
2578 }
2579 }
2580 }
2581 /* FALL THROUGH */
2582 case -ENOTCONN:
2583 case -ENODEV:
2584 clear_port_feature(hub->hdev,
2585 port1, USB_PORT_FEAT_C_RESET);
2586 /* FIXME need disconnect() for NOTATTACHED device */
2587 if (warm) {
2588 clear_port_feature(hub->hdev, port1,
2589 USB_PORT_FEAT_C_BH_PORT_RESET);
2590 clear_port_feature(hub->hdev, port1,
2591 USB_PORT_FEAT_C_PORT_LINK_STATE);
2592 } else {
2593 usb_set_device_state(udev, *status
2594 ? USB_STATE_NOTATTACHED
2595 : USB_STATE_DEFAULT);
2596 }
2597 break;
2598 }
2599}
2600
2601/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
1da177e4 2602static int hub_port_reset(struct usb_hub *hub, int port1,
75d7cf72 2603 struct usb_device *udev, unsigned int delay, bool warm)
1da177e4
LT
2604{
2605 int i, status;
2606
75d7cf72
AX
2607 if (!warm) {
2608 /* Block EHCI CF initialization during the port reset.
2609 * Some companion controllers don't like it when they mix.
2610 */
2611 down_read(&ehci_cf_port_reset_rwsem);
2612 } else {
2613 if (!hub_is_superspeed(hub->hdev)) {
2614 dev_err(hub->intfdev, "only USB3 hub support "
2615 "warm reset\n");
2616 return -EINVAL;
2617 }
2618 }
32fe0198 2619
1da177e4
LT
2620 /* Reset the port */
2621 for (i = 0; i < PORT_RESET_TRIES; i++) {
75d7cf72
AX
2622 status = set_port_feature(hub->hdev, port1, (warm ?
2623 USB_PORT_FEAT_BH_PORT_RESET :
2624 USB_PORT_FEAT_RESET));
2625 if (status) {
1da177e4 2626 dev_err(hub->intfdev,
75d7cf72
AX
2627 "cannot %sreset port %d (err = %d)\n",
2628 warm ? "warm " : "", port1, status);
2629 } else {
2630 status = hub_port_wait_reset(hub, port1, udev, delay,
2631 warm);
dd16525b 2632 if (status && status != -ENOTCONN)
1da177e4
LT
2633 dev_dbg(hub->intfdev,
2634 "port_wait_reset: err = %d\n",
2635 status);
2636 }
2637
2638 /* return on disconnect or reset */
75d7cf72
AX
2639 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2640 hub_port_finish_reset(hub, port1, udev, &status, warm);
32fe0198 2641 goto done;
1da177e4
LT
2642 }
2643
2644 dev_dbg (hub->intfdev,
75d7cf72
AX
2645 "port %d not enabled, trying %sreset again...\n",
2646 port1, warm ? "warm " : "");
1da177e4
LT
2647 delay = HUB_LONG_RESET_TIME;
2648 }
2649
2650 dev_err (hub->intfdev,
2651 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2652 port1);
2653
75d7cf72
AX
2654done:
2655 if (!warm)
2656 up_read(&ehci_cf_port_reset_rwsem);
5e467f6e 2657
75d7cf72 2658 return status;
5e467f6e
AX
2659}
2660
0ed9a57e
AX
2661/* Check if a port is power on */
2662static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2663{
2664 int ret = 0;
2665
2666 if (hub_is_superspeed(hub->hdev)) {
2667 if (portstatus & USB_SS_PORT_STAT_POWER)
2668 ret = 1;
2669 } else {
2670 if (portstatus & USB_PORT_STAT_POWER)
2671 ret = 1;
2672 }
2673
2674 return ret;
2675}
2676
d388dab7 2677#ifdef CONFIG_PM
1da177e4 2678
0ed9a57e
AX
2679/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2680static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2681{
2682 int ret = 0;
2683
2684 if (hub_is_superspeed(hub->hdev)) {
2685 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2686 == USB_SS_PORT_LS_U3)
2687 ret = 1;
2688 } else {
2689 if (portstatus & USB_PORT_STAT_SUSPEND)
2690 ret = 1;
2691 }
2692
2693 return ret;
2694}
b01b03f3
AS
2695
2696/* Determine whether the device on a port is ready for a normal resume,
2697 * is ready for a reset-resume, or should be disconnected.
2698 */
2699static int check_port_resume_type(struct usb_device *udev,
2700 struct usb_hub *hub, int port1,
2701 int status, unsigned portchange, unsigned portstatus)
2702{
2703 /* Is the device still present? */
0ed9a57e
AX
2704 if (status || port_is_suspended(hub, portstatus) ||
2705 !port_is_power_on(hub, portstatus) ||
2706 !(portstatus & USB_PORT_STAT_CONNECTION)) {
b01b03f3
AS
2707 if (status >= 0)
2708 status = -ENODEV;
2709 }
2710
86c57edf
AS
2711 /* Can't do a normal resume if the port isn't enabled,
2712 * so try a reset-resume instead.
2713 */
2714 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2715 if (udev->persist_enabled)
2716 udev->reset_resume = 1;
2717 else
2718 status = -ENODEV;
2719 }
b01b03f3
AS
2720
2721 if (status) {
2722 dev_dbg(hub->intfdev,
2723 "port %d status %04x.%04x after resume, %d\n",
2724 port1, portchange, portstatus, status);
2725 } else if (udev->reset_resume) {
2726
2727 /* Late port handoff can set status-change bits */
2728 if (portchange & USB_PORT_STAT_C_CONNECTION)
2729 clear_port_feature(hub->hdev, port1,
2730 USB_PORT_FEAT_C_CONNECTION);
2731 if (portchange & USB_PORT_STAT_C_ENABLE)
2732 clear_port_feature(hub->hdev, port1,
2733 USB_PORT_FEAT_C_ENABLE);
2734 }
2735
2736 return status;
2737}
2738
f74631e3
SS
2739int usb_disable_ltm(struct usb_device *udev)
2740{
2741 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2742
2743 /* Check if the roothub and device supports LTM. */
2744 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2745 !usb_device_supports_ltm(udev))
2746 return 0;
2747
2748 /* Clear Feature LTM Enable can only be sent if the device is
2749 * configured.
2750 */
2751 if (!udev->actconfig)
2752 return 0;
2753
2754 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2755 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2756 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2757 USB_CTRL_SET_TIMEOUT);
2758}
2759EXPORT_SYMBOL_GPL(usb_disable_ltm);
2760
2761void usb_enable_ltm(struct usb_device *udev)
2762{
2763 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2764
2765 /* Check if the roothub and device supports LTM. */
2766 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2767 !usb_device_supports_ltm(udev))
2768 return;
2769
2770 /* Set Feature LTM Enable can only be sent if the device is
2771 * configured.
2772 */
2773 if (!udev->actconfig)
2774 return;
2775
2776 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2777 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2778 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2779 USB_CTRL_SET_TIMEOUT);
2780}
2781EXPORT_SYMBOL_GPL(usb_enable_ltm);
2782
1da177e4
LT
2783#ifdef CONFIG_USB_SUSPEND
2784
2785/*
624d6c07
AS
2786 * usb_port_suspend - suspend a usb device's upstream port
2787 * @udev: device that's no longer in active use, not a root hub
2788 * Context: must be able to sleep; device not locked; pm locks held
2789 *
2790 * Suspends a USB device that isn't in active use, conserving power.
2791 * Devices may wake out of a suspend, if anything important happens,
2792 * using the remote wakeup mechanism. They may also be taken out of
2793 * suspend by the host, using usb_port_resume(). It's also routine
2794 * to disconnect devices while they are suspended.
2795 *
2796 * This only affects the USB hardware for a device; its interfaces
2797 * (and, for hubs, child devices) must already have been suspended.
2798 *
1da177e4
LT
2799 * Selective port suspend reduces power; most suspended devices draw
2800 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2801 * All devices below the suspended port are also suspended.
2802 *
2803 * Devices leave suspend state when the host wakes them up. Some devices
2804 * also support "remote wakeup", where the device can activate the USB
2805 * tree above them to deliver data, such as a keypress or packet. In
2806 * some cases, this wakes the USB host.
624d6c07
AS
2807 *
2808 * Suspending OTG devices may trigger HNP, if that's been enabled
2809 * between a pair of dual-role devices. That will change roles, such
2810 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2811 *
2812 * Devices on USB hub ports have only one "suspend" state, corresponding
2813 * to ACPI D2, "may cause the device to lose some context".
2814 * State transitions include:
2815 *
2816 * - suspend, resume ... when the VBUS power link stays live
2817 * - suspend, disconnect ... VBUS lost
2818 *
2819 * Once VBUS drop breaks the circuit, the port it's using has to go through
2820 * normal re-enumeration procedures, starting with enabling VBUS power.
2821 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2822 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2823 * timer, no SRP, no requests through sysfs.
2824 *
2825 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2826 * the root hub for their bus goes into global suspend ... so we don't
2827 * (falsely) update the device power state to say it suspended.
2828 *
2829 * Returns 0 on success, else negative errno.
1da177e4 2830 */
65bfd296 2831int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
1da177e4 2832{
624d6c07
AS
2833 struct usb_hub *hub = hdev_to_hub(udev->parent);
2834 int port1 = udev->portnum;
2835 int status;
1da177e4 2836
1da177e4
LT
2837 /* enable remote wakeup when appropriate; this lets the device
2838 * wake up the upstream hub (including maybe the root hub).
2839 *
2840 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2841 * we don't explicitly enable it here.
2842 */
645daaab 2843 if (udev->do_remote_wakeup) {
623bef9e
SS
2844 if (!hub_is_superspeed(hub->hdev)) {
2845 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2846 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2847 USB_DEVICE_REMOTE_WAKEUP, 0,
2848 NULL, 0,
2849 USB_CTRL_SET_TIMEOUT);
2850 } else {
2851 /* Assume there's only one function on the USB 3.0
2852 * device and enable remote wake for the first
2853 * interface. FIXME if the interface association
2854 * descriptor shows there's more than one function.
2855 */
2856 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2857 USB_REQ_SET_FEATURE,
2858 USB_RECIP_INTERFACE,
2859 USB_INTRF_FUNC_SUSPEND,
3b9b6acd
SS
2860 USB_INTRF_FUNC_SUSPEND_RW |
2861 USB_INTRF_FUNC_SUSPEND_LP,
623bef9e
SS
2862 NULL, 0,
2863 USB_CTRL_SET_TIMEOUT);
2864 }
0c487206 2865 if (status) {
624d6c07
AS
2866 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2867 status);
0c487206 2868 /* bail if autosuspend is requested */
5b1b0b81 2869 if (PMSG_IS_AUTO(msg))
0c487206
ON
2870 return status;
2871 }
1da177e4
LT
2872 }
2873
65580b43
AX
2874 /* disable USB2 hardware LPM */
2875 if (udev->usb2_hw_lpm_enabled == 1)
2876 usb_set_usb2_hardware_lpm(udev, 0);
2877
f74631e3
SS
2878 if (usb_disable_ltm(udev)) {
2879 dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2880 __func__);
2881 return -ENOMEM;
2882 }
8306095f
SS
2883 if (usb_unlocked_disable_lpm(udev)) {
2884 dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
2885 __func__);
2886 return -ENOMEM;
2887 }
2888
1da177e4 2889 /* see 7.1.7.6 */
a7114230
AX
2890 if (hub_is_superspeed(hub->hdev))
2891 status = set_port_feature(hub->hdev,
2892 port1 | (USB_SS_PORT_LS_U3 << 3),
2893 USB_PORT_FEAT_LINK_STATE);
a8f08d86
AX
2894 else
2895 status = set_port_feature(hub->hdev, port1,
2896 USB_PORT_FEAT_SUSPEND);
1da177e4 2897 if (status) {
624d6c07
AS
2898 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2899 port1, status);
1da177e4 2900 /* paranoia: "should not happen" */
0c487206
ON
2901 if (udev->do_remote_wakeup)
2902 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1da177e4
LT
2903 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2904 USB_DEVICE_REMOTE_WAKEUP, 0,
2905 NULL, 0,
2906 USB_CTRL_SET_TIMEOUT);
cbb33004 2907
c3e751e4
AX
2908 /* Try to enable USB2 hardware LPM again */
2909 if (udev->usb2_hw_lpm_capable == 1)
2910 usb_set_usb2_hardware_lpm(udev, 1);
2911
f74631e3
SS
2912 /* Try to enable USB3 LTM and LPM again */
2913 usb_enable_ltm(udev);
8306095f
SS
2914 usb_unlocked_enable_lpm(udev);
2915
cbb33004 2916 /* System sleep transitions should never fail */
5b1b0b81 2917 if (!PMSG_IS_AUTO(msg))
cbb33004 2918 status = 0;
1da177e4
LT
2919 } else {
2920 /* device has up to 10 msec to fully suspend */
30b1a7a3
AS
2921 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2922 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2923 udev->do_remote_wakeup);
1da177e4 2924 usb_set_device_state(udev, USB_STATE_SUSPENDED);
bfd1e910 2925 udev->port_is_suspended = 1;
1da177e4
LT
2926 msleep(10);
2927 }
c08512c7 2928 usb_mark_last_busy(hub->hdev);
1da177e4
LT
2929 return status;
2930}
2931
1da177e4 2932/*
390a8c34
DB
2933 * If the USB "suspend" state is in use (rather than "global suspend"),
2934 * many devices will be individually taken out of suspend state using
54515fe5 2935 * special "resume" signaling. This routine kicks in shortly after
1da177e4
LT
2936 * hardware resume signaling is finished, either because of selective
2937 * resume (by host) or remote wakeup (by device) ... now see what changed
2938 * in the tree that's rooted at this device.
54515fe5
AS
2939 *
2940 * If @udev->reset_resume is set then the device is reset before the
2941 * status check is done.
1da177e4 2942 */
140d8f68 2943static int finish_port_resume(struct usb_device *udev)
1da177e4 2944{
54515fe5 2945 int status = 0;
1da177e4
LT
2946 u16 devstatus;
2947
2948 /* caller owns the udev device lock */
b9cef6c3
WF
2949 dev_dbg(&udev->dev, "%s\n",
2950 udev->reset_resume ? "finish reset-resume" : "finish resume");
1da177e4
LT
2951
2952 /* usb ch9 identifies four variants of SUSPENDED, based on what
2953 * state the device resumes to. Linux currently won't see the
2954 * first two on the host side; they'd be inside hub_port_init()
2955 * during many timeouts, but khubd can't suspend until later.
2956 */
2957 usb_set_device_state(udev, udev->actconfig
2958 ? USB_STATE_CONFIGURED
2959 : USB_STATE_ADDRESS);
1da177e4 2960
54515fe5
AS
2961 /* 10.5.4.5 says not to reset a suspended port if the attached
2962 * device is enabled for remote wakeup. Hence the reset
2963 * operation is carried out here, after the port has been
2964 * resumed.
2965 */
2966 if (udev->reset_resume)
86c57edf 2967 retry_reset_resume:
742120c6 2968 status = usb_reset_and_verify_device(udev);
54515fe5 2969
1da177e4
LT
2970 /* 10.5.4.5 says be sure devices in the tree are still there.
2971 * For now let's assume the device didn't go crazy on resume,
2972 * and device drivers will know about any resume quirks.
2973 */
54515fe5 2974 if (status == 0) {
46dede46 2975 devstatus = 0;
54515fe5
AS
2976 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2977 if (status >= 0)
46dede46 2978 status = (status > 0 ? 0 : -ENODEV);
86c57edf
AS
2979
2980 /* If a normal resume failed, try doing a reset-resume */
2981 if (status && !udev->reset_resume && udev->persist_enabled) {
2982 dev_dbg(&udev->dev, "retry with reset-resume\n");
2983 udev->reset_resume = 1;
2984 goto retry_reset_resume;
2985 }
54515fe5 2986 }
b40b7a90 2987
624d6c07
AS
2988 if (status) {
2989 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
2990 status);
2991 } else if (udev->actconfig) {
1da177e4 2992 le16_to_cpus(&devstatus);
686314cf 2993 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
1da177e4
LT
2994 status = usb_control_msg(udev,
2995 usb_sndctrlpipe(udev, 0),
2996 USB_REQ_CLEAR_FEATURE,
2997 USB_RECIP_DEVICE,
2998 USB_DEVICE_REMOTE_WAKEUP, 0,
2999 NULL, 0,
3000 USB_CTRL_SET_TIMEOUT);
a8e7c565 3001 if (status)
b9cef6c3
WF
3002 dev_dbg(&udev->dev,
3003 "disable remote wakeup, status %d\n",
3004 status);
4bf0ba86 3005 }
1da177e4 3006 status = 0;
1da177e4
LT
3007 }
3008 return status;
3009}
3010
624d6c07
AS
3011/*
3012 * usb_port_resume - re-activate a suspended usb device's upstream port
3013 * @udev: device to re-activate, not a root hub
3014 * Context: must be able to sleep; device not locked; pm locks held
3015 *
3016 * This will re-activate the suspended device, increasing power usage
3017 * while letting drivers communicate again with its endpoints.
3018 * USB resume explicitly guarantees that the power session between
3019 * the host and the device is the same as it was when the device
3020 * suspended.
3021 *
feccc30d
AS
3022 * If @udev->reset_resume is set then this routine won't check that the
3023 * port is still enabled. Furthermore, finish_port_resume() above will
54515fe5
AS
3024 * reset @udev. The end result is that a broken power session can be
3025 * recovered and @udev will appear to persist across a loss of VBUS power.
3026 *
3027 * For example, if a host controller doesn't maintain VBUS suspend current
3028 * during a system sleep or is reset when the system wakes up, all the USB
3029 * power sessions below it will be broken. This is especially troublesome
3030 * for mass-storage devices containing mounted filesystems, since the
3031 * device will appear to have disconnected and all the memory mappings
3032 * to it will be lost. Using the USB_PERSIST facility, the device can be
3033 * made to appear as if it had not disconnected.
3034 *
742120c6 3035 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
feccc30d 3036 * every effort to insure that the same device is present after the
54515fe5
AS
3037 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3038 * quite possible for a device to remain unaltered but its media to be
3039 * changed. If the user replaces a flash memory card while the system is
3040 * asleep, he will have only himself to blame when the filesystem on the
3041 * new card is corrupted and the system crashes.
3042 *
624d6c07
AS
3043 * Returns 0 on success, else negative errno.
3044 */
65bfd296 3045int usb_port_resume(struct usb_device *udev, pm_message_t msg)
1da177e4 3046{
624d6c07
AS
3047 struct usb_hub *hub = hdev_to_hub(udev->parent);
3048 int port1 = udev->portnum;
3049 int status;
3050 u16 portchange, portstatus;
d25450c6
AS
3051
3052 /* Skip the initial Clear-Suspend step for a remote wakeup */
3053 status = hub_port_status(hub, port1, &portstatus, &portchange);
0ed9a57e 3054 if (status == 0 && !port_is_suspended(hub, portstatus))
d25450c6 3055 goto SuspendCleared;
1da177e4
LT
3056
3057 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
3058
d5cbad4b
AS
3059 set_bit(port1, hub->busy_bits);
3060
1da177e4 3061 /* see 7.1.7.7; affects power usage, but not budgeting */
a7114230
AX
3062 if (hub_is_superspeed(hub->hdev))
3063 status = set_port_feature(hub->hdev,
3064 port1 | (USB_SS_PORT_LS_U0 << 3),
3065 USB_PORT_FEAT_LINK_STATE);
3066 else
3067 status = clear_port_feature(hub->hdev,
3068 port1, USB_PORT_FEAT_SUSPEND);
1da177e4 3069 if (status) {
624d6c07
AS
3070 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
3071 port1, status);
1da177e4 3072 } else {
1da177e4 3073 /* drive resume for at least 20 msec */
686314cf 3074 dev_dbg(&udev->dev, "usb %sresume\n",
5b1b0b81 3075 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
1da177e4
LT
3076 msleep(25);
3077
1da177e4
LT
3078 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3079 * stop resume signaling. Then finish the resume
3080 * sequence.
3081 */
d25450c6 3082 status = hub_port_status(hub, port1, &portstatus, &portchange);
54515fe5 3083
b01b03f3
AS
3084 /* TRSMRCY = 10 msec */
3085 msleep(10);
3086 }
3087
54515fe5 3088 SuspendCleared:
b01b03f3 3089 if (status == 0) {
bfd1e910 3090 udev->port_is_suspended = 0;
a7114230
AX
3091 if (hub_is_superspeed(hub->hdev)) {
3092 if (portchange & USB_PORT_STAT_C_LINK_STATE)
3093 clear_port_feature(hub->hdev, port1,
3094 USB_PORT_FEAT_C_PORT_LINK_STATE);
3095 } else {
3096 if (portchange & USB_PORT_STAT_C_SUSPEND)
3097 clear_port_feature(hub->hdev, port1,
3098 USB_PORT_FEAT_C_SUSPEND);
3099 }
1da177e4 3100 }
1da177e4 3101
d5cbad4b 3102 clear_bit(port1, hub->busy_bits);
d5cbad4b 3103
b01b03f3
AS
3104 status = check_port_resume_type(udev,
3105 hub, port1, status, portchange, portstatus);
54515fe5
AS
3106 if (status == 0)
3107 status = finish_port_resume(udev);
3108 if (status < 0) {
3109 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3110 hub_port_logical_disconnect(hub, port1);
65580b43
AX
3111 } else {
3112 /* Try to enable USB2 hardware LPM */
3113 if (udev->usb2_hw_lpm_capable == 1)
3114 usb_set_usb2_hardware_lpm(udev, 1);
8306095f 3115
f74631e3
SS
3116 /* Try to enable USB3 LTM and LPM */
3117 usb_enable_ltm(udev);
8306095f 3118 usb_unlocked_enable_lpm(udev);
54515fe5 3119 }
65580b43 3120
1da177e4
LT
3121 return status;
3122}
3123
8808f00c 3124/* caller has locked udev */
0534d468 3125int usb_remote_wakeup(struct usb_device *udev)
1da177e4
LT
3126{
3127 int status = 0;
3128
1da177e4 3129 if (udev->state == USB_STATE_SUSPENDED) {
645daaab 3130 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
9bbdf1e0
AS
3131 status = usb_autoresume_device(udev);
3132 if (status == 0) {
3133 /* Let the drivers do their thing, then... */
3134 usb_autosuspend_device(udev);
3135 }
1da177e4 3136 }
1da177e4
LT
3137 return status;
3138}
3139
d388dab7
AS
3140#else /* CONFIG_USB_SUSPEND */
3141
3142/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3143
65bfd296 3144int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
d388dab7
AS
3145{
3146 return 0;
3147}
3148
b01b03f3
AS
3149/* However we may need to do a reset-resume */
3150
65bfd296 3151int usb_port_resume(struct usb_device *udev, pm_message_t msg)
d388dab7 3152{
b01b03f3
AS
3153 struct usb_hub *hub = hdev_to_hub(udev->parent);
3154 int port1 = udev->portnum;
3155 int status;
3156 u16 portchange, portstatus;
3157
3158 status = hub_port_status(hub, port1, &portstatus, &portchange);
3159 status = check_port_resume_type(udev,
3160 hub, port1, status, portchange, portstatus);
54515fe5 3161
b01b03f3
AS
3162 if (status) {
3163 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3164 hub_port_logical_disconnect(hub, port1);
3165 } else if (udev->reset_resume) {
54515fe5 3166 dev_dbg(&udev->dev, "reset-resume\n");
742120c6 3167 status = usb_reset_and_verify_device(udev);
54515fe5
AS
3168 }
3169 return status;
d388dab7
AS
3170}
3171
d388dab7
AS
3172#endif
3173
e6f30dea
ML
3174static int check_ports_changed(struct usb_hub *hub)
3175{
3176 int port1;
3177
3178 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3179 u16 portstatus, portchange;
3180 int status;
3181
3182 status = hub_port_status(hub, port1, &portstatus, &portchange);
3183 if (!status && portchange)
3184 return 1;
3185 }
3186 return 0;
3187}
3188
db690874 3189static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
1da177e4
LT
3190{
3191 struct usb_hub *hub = usb_get_intfdata (intf);
3192 struct usb_device *hdev = hub->hdev;
3193 unsigned port1;
4296c70a 3194 int status;
1da177e4 3195
cbb33004 3196 /* Warn if children aren't already suspended */
1da177e4
LT
3197 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3198 struct usb_device *udev;
3199
ff823c79 3200 udev = hub->ports[port1 - 1]->child;
6840d255 3201 if (udev && udev->can_submit) {
cbb33004 3202 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
5b1b0b81 3203 if (PMSG_IS_AUTO(msg))
cbb33004 3204 return -EBUSY;
c9f89fa4 3205 }
1da177e4 3206 }
e6f30dea
ML
3207
3208 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3209 /* check if there are changes pending on hub ports */
3210 if (check_ports_changed(hub)) {
3211 if (PMSG_IS_AUTO(msg))
3212 return -EBUSY;
3213 pm_wakeup_event(&hdev->dev, 2000);
3214 }
3215 }
3216
4296c70a
SS
3217 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3218 /* Enable hub to send remote wakeup for all ports. */
3219 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3220 status = set_port_feature(hdev,
3221 port1 |
3222 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3223 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3224 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3225 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3226 }
3227 }
1da177e4 3228
441b62c1 3229 dev_dbg(&intf->dev, "%s\n", __func__);
40f122f3 3230
12f1ff83 3231 /* stop khubd and related activity */
4330354f 3232 hub_quiesce(hub, HUB_SUSPEND);
b6f6436d 3233 return 0;
1da177e4
LT
3234}
3235
3236static int hub_resume(struct usb_interface *intf)
3237{
5e6effae 3238 struct usb_hub *hub = usb_get_intfdata(intf);
40f122f3 3239
5e6effae 3240 dev_dbg(&intf->dev, "%s\n", __func__);
f2835219 3241 hub_activate(hub, HUB_RESUME);
1da177e4
LT
3242 return 0;
3243}
3244
b41a60ec 3245static int hub_reset_resume(struct usb_interface *intf)
f07600cf 3246{
b41a60ec 3247 struct usb_hub *hub = usb_get_intfdata(intf);
f07600cf 3248
5e6effae 3249 dev_dbg(&intf->dev, "%s\n", __func__);
f2835219 3250 hub_activate(hub, HUB_RESET_RESUME);
f07600cf
AS
3251 return 0;
3252}
3253
54515fe5
AS
3254/**
3255 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3256 * @rhdev: struct usb_device for the root hub
3257 *
3258 * The USB host controller driver calls this function when its root hub
3259 * is resumed and Vbus power has been interrupted or the controller
feccc30d
AS
3260 * has been reset. The routine marks @rhdev as having lost power.
3261 * When the hub driver is resumed it will take notice and carry out
3262 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3263 * the others will be disconnected.
54515fe5
AS
3264 */
3265void usb_root_hub_lost_power(struct usb_device *rhdev)
3266{
3267 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3268 rhdev->reset_resume = 1;
3269}
3270EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3271
1ea7e0e8
SS
3272static const char * const usb3_lpm_names[] = {
3273 "U0",
3274 "U1",
3275 "U2",
3276 "U3",
3277};
3278
3279/*
3280 * Send a Set SEL control transfer to the device, prior to enabling
3281 * device-initiated U1 or U2. This lets the device know the exit latencies from
3282 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3283 * packet from the host.
3284 *
3285 * This function will fail if the SEL or PEL values for udev are greater than
3286 * the maximum allowed values for the link state to be enabled.
3287 */
3288static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3289{
3290 struct usb_set_sel_req *sel_values;
3291 unsigned long long u1_sel;
3292 unsigned long long u1_pel;
3293 unsigned long long u2_sel;
3294 unsigned long long u2_pel;
3295 int ret;
3296
3297 /* Convert SEL and PEL stored in ns to us */
3298 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3299 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3300 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3301 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3302
3303 /*
3304 * Make sure that the calculated SEL and PEL values for the link
3305 * state we're enabling aren't bigger than the max SEL/PEL
3306 * value that will fit in the SET SEL control transfer.
3307 * Otherwise the device would get an incorrect idea of the exit
3308 * latency for the link state, and could start a device-initiated
3309 * U1/U2 when the exit latencies are too high.
3310 */
3311 if ((state == USB3_LPM_U1 &&
3312 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3313 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3314 (state == USB3_LPM_U2 &&
3315 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3316 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
1510a1a2 3317 dev_dbg(&udev->dev, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n",
1ea7e0e8
SS
3318 usb3_lpm_names[state], u1_sel, u1_pel);
3319 return -EINVAL;
3320 }
3321
3322 /*
3323 * If we're enabling device-initiated LPM for one link state,
3324 * but the other link state has a too high SEL or PEL value,
3325 * just set those values to the max in the Set SEL request.
3326 */
3327 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3328 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3329
3330 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3331 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3332
3333 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3334 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3335
3336 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3337 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3338
3339 /*
3340 * usb_enable_lpm() can be called as part of a failed device reset,
3341 * which may be initiated by an error path of a mass storage driver.
3342 * Therefore, use GFP_NOIO.
3343 */
3344 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3345 if (!sel_values)
3346 return -ENOMEM;
3347
3348 sel_values->u1_sel = u1_sel;
3349 sel_values->u1_pel = u1_pel;
3350 sel_values->u2_sel = cpu_to_le16(u2_sel);
3351 sel_values->u2_pel = cpu_to_le16(u2_pel);
3352
3353 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3354 USB_REQ_SET_SEL,
3355 USB_RECIP_DEVICE,
3356 0, 0,
3357 sel_values, sizeof *(sel_values),
3358 USB_CTRL_SET_TIMEOUT);
3359 kfree(sel_values);
3360 return ret;
3361}
3362
3363/*
3364 * Enable or disable device-initiated U1 or U2 transitions.
3365 */
3366static int usb_set_device_initiated_lpm(struct usb_device *udev,
3367 enum usb3_link_state state, bool enable)
3368{
3369 int ret;
3370 int feature;
3371
3372 switch (state) {
3373 case USB3_LPM_U1:
3374 feature = USB_DEVICE_U1_ENABLE;
3375 break;
3376 case USB3_LPM_U2:
3377 feature = USB_DEVICE_U2_ENABLE;
3378 break;
3379 default:
3380 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3381 __func__, enable ? "enable" : "disable");
3382 return -EINVAL;
3383 }
3384
3385 if (udev->state != USB_STATE_CONFIGURED) {
3386 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3387 "for unconfigured device.\n",
3388 __func__, enable ? "enable" : "disable",
3389 usb3_lpm_names[state]);
3390 return 0;
3391 }
3392
3393 if (enable) {
1ea7e0e8
SS
3394 /*
3395 * Now send the control transfer to enable device-initiated LPM
3396 * for either U1 or U2.
3397 */
3398 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3399 USB_REQ_SET_FEATURE,
3400 USB_RECIP_DEVICE,
3401 feature,
3402 0, NULL, 0,
3403 USB_CTRL_SET_TIMEOUT);
3404 } else {
3405 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3406 USB_REQ_CLEAR_FEATURE,
3407 USB_RECIP_DEVICE,
3408 feature,
3409 0, NULL, 0,
3410 USB_CTRL_SET_TIMEOUT);
3411 }
3412 if (ret < 0) {
3413 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3414 enable ? "Enable" : "Disable",
3415 usb3_lpm_names[state]);
3416 return -EBUSY;
3417 }
3418 return 0;
3419}
3420
3421static int usb_set_lpm_timeout(struct usb_device *udev,
3422 enum usb3_link_state state, int timeout)
3423{
3424 int ret;
3425 int feature;
3426
3427 switch (state) {
3428 case USB3_LPM_U1:
3429 feature = USB_PORT_FEAT_U1_TIMEOUT;
3430 break;
3431 case USB3_LPM_U2:
3432 feature = USB_PORT_FEAT_U2_TIMEOUT;
3433 break;
3434 default:
3435 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3436 __func__);
3437 return -EINVAL;
3438 }
3439
3440 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3441 timeout != USB3_LPM_DEVICE_INITIATED) {
3442 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3443 "which is a reserved value.\n",
3444 usb3_lpm_names[state], timeout);
3445 return -EINVAL;
3446 }
3447
3448 ret = set_port_feature(udev->parent,
3449 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3450 feature);
3451 if (ret < 0) {
3452 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3453 "error code %i\n", usb3_lpm_names[state],
3454 timeout, ret);
3455 return -EBUSY;
3456 }
3457 if (state == USB3_LPM_U1)
3458 udev->u1_params.timeout = timeout;
3459 else
3460 udev->u2_params.timeout = timeout;
3461 return 0;
3462}
3463
3464/*
3465 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3466 * U1/U2 entry.
3467 *
3468 * We will attempt to enable U1 or U2, but there are no guarantees that the
3469 * control transfers to set the hub timeout or enable device-initiated U1/U2
3470 * will be successful.
3471 *
3472 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3473 * driver know about it. If that call fails, it should be harmless, and just
3474 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3475 */
3476static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3477 enum usb3_link_state state)
3478{
65a95b75 3479 int timeout, ret;
ae8963ad
SS
3480 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3481 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3482
3483 /* If the device says it doesn't have *any* exit latency to come out of
3484 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3485 * state.
3486 */
3487 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3488 (state == USB3_LPM_U2 && u2_mel == 0))
3489 return;
1ea7e0e8 3490
65a95b75
SS
3491 /*
3492 * First, let the device know about the exit latencies
3493 * associated with the link state we're about to enable.
3494 */
3495 ret = usb_req_set_sel(udev, state);
3496 if (ret < 0) {
3497 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3498 usb3_lpm_names[state]);
3499 return;
3500 }
3501
1ea7e0e8
SS
3502 /* We allow the host controller to set the U1/U2 timeout internally
3503 * first, so that it can change its schedule to account for the
3504 * additional latency to send data to a device in a lower power
3505 * link state.
3506 */
3507 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3508
3509 /* xHCI host controller doesn't want to enable this LPM state. */
3510 if (timeout == 0)
3511 return;
3512
3513 if (timeout < 0) {
3514 dev_warn(&udev->dev, "Could not enable %s link state, "
3515 "xHCI error %i.\n", usb3_lpm_names[state],
3516 timeout);
3517 return;
3518 }
3519
3520 if (usb_set_lpm_timeout(udev, state, timeout))
3521 /* If we can't set the parent hub U1/U2 timeout,
3522 * device-initiated LPM won't be allowed either, so let the xHCI
3523 * host know that this link state won't be enabled.
3524 */
3525 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3526
3527 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3528 else if (udev->actconfig)
3529 usb_set_device_initiated_lpm(udev, state, true);
3530
3531}
3532
3533/*
3534 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3535 * U1/U2 entry.
3536 *
3537 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3538 * If zero is returned, the parent will not allow the link to go into U1/U2.
3539 *
3540 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3541 * it won't have an effect on the bus link state because the parent hub will
3542 * still disallow device-initiated U1/U2 entry.
3543 *
3544 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3545 * possible. The result will be slightly more bus bandwidth will be taken up
3546 * (to account for U1/U2 exit latency), but it should be harmless.
3547 */
3548static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3549 enum usb3_link_state state)
3550{
3551 int feature;
3552
3553 switch (state) {
3554 case USB3_LPM_U1:
3555 feature = USB_PORT_FEAT_U1_TIMEOUT;
3556 break;
3557 case USB3_LPM_U2:
3558 feature = USB_PORT_FEAT_U2_TIMEOUT;
3559 break;
3560 default:
3561 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3562 __func__);
3563 return -EINVAL;
3564 }
3565
3566 if (usb_set_lpm_timeout(udev, state, 0))
3567 return -EBUSY;
3568
3569 usb_set_device_initiated_lpm(udev, state, false);
3570
3571 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3572 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3573 "bus schedule bandwidth may be impacted.\n",
3574 usb3_lpm_names[state]);
3575 return 0;
3576}
3577
3578/*
3579 * Disable hub-initiated and device-initiated U1 and U2 entry.
3580 * Caller must own the bandwidth_mutex.
3581 *
3582 * This will call usb_enable_lpm() on failure, which will decrement
3583 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3584 */
3585int usb_disable_lpm(struct usb_device *udev)
3586{
3587 struct usb_hcd *hcd;
3588
3589 if (!udev || !udev->parent ||
3590 udev->speed != USB_SPEED_SUPER ||
3591 !udev->lpm_capable)
3592 return 0;
3593
3594 hcd = bus_to_hcd(udev->bus);
3595 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3596 return 0;
3597
3598 udev->lpm_disable_count++;
55558c33 3599 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
1ea7e0e8
SS
3600 return 0;
3601
3602 /* If LPM is enabled, attempt to disable it. */
3603 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3604 goto enable_lpm;
3605 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3606 goto enable_lpm;
3607
3608 return 0;
3609
3610enable_lpm:
3611 usb_enable_lpm(udev);
3612 return -EBUSY;
3613}
3614EXPORT_SYMBOL_GPL(usb_disable_lpm);
3615
3616/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3617int usb_unlocked_disable_lpm(struct usb_device *udev)
3618{
3619 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3620 int ret;
3621
3622 if (!hcd)
3623 return -EINVAL;
3624
3625 mutex_lock(hcd->bandwidth_mutex);
3626 ret = usb_disable_lpm(udev);
3627 mutex_unlock(hcd->bandwidth_mutex);
3628
3629 return ret;
3630}
3631EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3632
3633/*
3634 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3635 * xHCI host policy may prevent U1 or U2 from being enabled.
3636 *
3637 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3638 * until the lpm_disable_count drops to zero. Caller must own the
3639 * bandwidth_mutex.
3640 */
3641void usb_enable_lpm(struct usb_device *udev)
3642{
3643 struct usb_hcd *hcd;
3644
3645 if (!udev || !udev->parent ||
3646 udev->speed != USB_SPEED_SUPER ||
3647 !udev->lpm_capable)
3648 return;
3649
3650 udev->lpm_disable_count--;
3651 hcd = bus_to_hcd(udev->bus);
3652 /* Double check that we can both enable and disable LPM.
3653 * Device must be configured to accept set feature U1/U2 timeout.
3654 */
3655 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3656 !hcd->driver->disable_usb3_lpm_timeout)
3657 return;
3658
3659 if (udev->lpm_disable_count > 0)
3660 return;
3661
3662 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3663 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3664}
3665EXPORT_SYMBOL_GPL(usb_enable_lpm);
3666
3667/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3668void usb_unlocked_enable_lpm(struct usb_device *udev)
3669{
3670 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3671
3672 if (!hcd)
3673 return;
3674
3675 mutex_lock(hcd->bandwidth_mutex);
3676 usb_enable_lpm(udev);
3677 mutex_unlock(hcd->bandwidth_mutex);
3678}
3679EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3680
3681
d388dab7
AS
3682#else /* CONFIG_PM */
3683
f07600cf
AS
3684#define hub_suspend NULL
3685#define hub_resume NULL
3686#define hub_reset_resume NULL
1ea7e0e8
SS
3687
3688int usb_disable_lpm(struct usb_device *udev)
3689{
3690 return 0;
3691}
e9261fb6 3692EXPORT_SYMBOL_GPL(usb_disable_lpm);
1ea7e0e8
SS
3693
3694void usb_enable_lpm(struct usb_device *udev) { }
e9261fb6 3695EXPORT_SYMBOL_GPL(usb_enable_lpm);
1ea7e0e8
SS
3696
3697int usb_unlocked_disable_lpm(struct usb_device *udev)
3698{
3699 return 0;
3700}
e9261fb6 3701EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
1ea7e0e8
SS
3702
3703void usb_unlocked_enable_lpm(struct usb_device *udev) { }
e9261fb6 3704EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
f74631e3
SS
3705
3706int usb_disable_ltm(struct usb_device *udev)
3707{
3708 return 0;
3709}
3710EXPORT_SYMBOL_GPL(usb_disable_ltm);
3711
3712void usb_enable_ltm(struct usb_device *udev) { }
3713EXPORT_SYMBOL_GPL(usb_enable_ltm);
d388dab7
AS
3714#endif
3715
1da177e4
LT
3716
3717/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3718 *
3719 * Between connect detection and reset signaling there must be a delay
3720 * of 100ms at least for debounce and power-settling. The corresponding
3721 * timer shall restart whenever the downstream port detects a disconnect.
3722 *
3723 * Apparently there are some bluetooth and irda-dongles and a number of
3724 * low-speed devices for which this debounce period may last over a second.
3725 * Not covered by the spec - but easy to deal with.
3726 *
3727 * This implementation uses a 1500ms total debounce timeout; if the
3728 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3729 * every 25ms for transient disconnects. When the port status has been
3730 * unchanged for 100ms it returns the port status.
3731 */
1da177e4
LT
3732static int hub_port_debounce(struct usb_hub *hub, int port1)
3733{
3734 int ret;
3735 int total_time, stable_time = 0;
3736 u16 portchange, portstatus;
3737 unsigned connection = 0xffff;
3738
3739 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3740 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3741 if (ret < 0)
3742 return ret;
3743
3744 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3745 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3746 stable_time += HUB_DEBOUNCE_STEP;
3747 if (stable_time >= HUB_DEBOUNCE_STABLE)
3748 break;
3749 } else {
3750 stable_time = 0;
3751 connection = portstatus & USB_PORT_STAT_CONNECTION;
3752 }
3753
3754 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3755 clear_port_feature(hub->hdev, port1,
3756 USB_PORT_FEAT_C_CONNECTION);
3757 }
3758
3759 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3760 break;
3761 msleep(HUB_DEBOUNCE_STEP);
3762 }
3763
3764 dev_dbg (hub->intfdev,
3765 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3766 port1, total_time, stable_time, portstatus);
3767
3768 if (stable_time < HUB_DEBOUNCE_STABLE)
3769 return -ETIMEDOUT;
3770 return portstatus;
3771}
3772
fc721f51 3773void usb_ep0_reinit(struct usb_device *udev)
1da177e4 3774{
ddeac4e7
AS
3775 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3776 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
2caf7fcd 3777 usb_enable_endpoint(udev, &udev->ep0, true);
1da177e4 3778}
fc721f51 3779EXPORT_SYMBOL_GPL(usb_ep0_reinit);
1da177e4
LT
3780
3781#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3782#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3783
4326ed0b 3784static int hub_set_address(struct usb_device *udev, int devnum)
1da177e4
LT
3785{
3786 int retval;
c6515272 3787 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1da177e4 3788
c6515272
SS
3789 /*
3790 * The host controller will choose the device address,
3791 * instead of the core having chosen it earlier
3792 */
3793 if (!hcd->driver->address_device && devnum <= 1)
1da177e4
LT
3794 return -EINVAL;
3795 if (udev->state == USB_STATE_ADDRESS)
3796 return 0;
3797 if (udev->state != USB_STATE_DEFAULT)
3798 return -EINVAL;
c8d4af8e 3799 if (hcd->driver->address_device)
c6515272 3800 retval = hcd->driver->address_device(hcd, udev);
c8d4af8e 3801 else
c6515272
SS
3802 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3803 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3804 NULL, 0, USB_CTRL_SET_TIMEOUT);
1da177e4 3805 if (retval == 0) {
3b29b68b 3806 update_devnum(udev, devnum);
4953d141 3807 /* Device now using proper address. */
1da177e4 3808 usb_set_device_state(udev, USB_STATE_ADDRESS);
fc721f51 3809 usb_ep0_reinit(udev);
1da177e4
LT
3810 }
3811 return retval;
3812}
3813
3814/* Reset device, (re)assign address, get device descriptor.
3815 * Device connection must be stable, no more debouncing needed.
3816 * Returns device in USB_STATE_ADDRESS, except on error.
3817 *
3818 * If this is called for an already-existing device (as part of
742120c6 3819 * usb_reset_and_verify_device), the caller must own the device lock. For a
1da177e4
LT
3820 * newly detected device that is not accessible through any global
3821 * pointers, it's not necessary to lock the device.
3822 */
3823static int
3824hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3825 int retry_counter)
3826{
4186ecf8 3827 static DEFINE_MUTEX(usb_address0_mutex);
1da177e4
LT
3828
3829 struct usb_device *hdev = hub->hdev;
e7b77172 3830 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
1da177e4
LT
3831 int i, j, retval;
3832 unsigned delay = HUB_SHORT_RESET_TIME;
3833 enum usb_device_speed oldspeed = udev->speed;
e538dfda 3834 const char *speed;
4326ed0b 3835 int devnum = udev->devnum;
1da177e4
LT
3836
3837 /* root hub ports have a slightly longer reset period
3838 * (from USB 2.0 spec, section 7.1.7.5)
3839 */
3840 if (!hdev->parent) {
3841 delay = HUB_ROOT_RESET_TIME;
3842 if (port1 == hdev->bus->otg_port)
3843 hdev->bus->b_hnp_enable = 0;
3844 }
3845
3846 /* Some low speed devices have problems with the quick delay, so */
3847 /* be a bit pessimistic with those devices. RHbug #23670 */
3848 if (oldspeed == USB_SPEED_LOW)
3849 delay = HUB_LONG_RESET_TIME;
3850
4186ecf8 3851 mutex_lock(&usb_address0_mutex);
1da177e4 3852
07194ab7
LT
3853 /* Reset the device; full speed may morph to high speed */
3854 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
75d7cf72 3855 retval = hub_port_reset(hub, port1, udev, delay, false);
07194ab7
LT
3856 if (retval < 0) /* error or disconnect */
3857 goto fail;
3858 /* success, speed is known */
3859
1da177e4
LT
3860 retval = -ENODEV;
3861
3862 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3863 dev_dbg(&udev->dev, "device reset changed speed!\n");
3864 goto fail;
3865 }
3866 oldspeed = udev->speed;
4326ed0b 3867
1da177e4
LT
3868 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3869 * it's fixed size except for full speed devices.
5bb6e0ae
IPG
3870 * For Wireless USB devices, ep0 max packet is always 512 (tho
3871 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
1da177e4
LT
3872 */
3873 switch (udev->speed) {
6b403b02 3874 case USB_SPEED_SUPER:
551cdbbe 3875 case USB_SPEED_WIRELESS: /* fixed at 512 */
551509d2 3876 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
5bb6e0ae 3877 break;
1da177e4 3878 case USB_SPEED_HIGH: /* fixed at 64 */
551509d2 3879 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
1da177e4
LT
3880 break;
3881 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3882 /* to determine the ep0 maxpacket size, try to read
3883 * the device descriptor to get bMaxPacketSize0 and
3884 * then correct our initial guess.
3885 */
551509d2 3886 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
1da177e4
LT
3887 break;
3888 case USB_SPEED_LOW: /* fixed at 8 */
551509d2 3889 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
1da177e4
LT
3890 break;
3891 default:
3892 goto fail;
3893 }
e538dfda
MN
3894
3895 if (udev->speed == USB_SPEED_WIRELESS)
3896 speed = "variable speed Wireless";
3897 else
3898 speed = usb_speed_string(udev->speed);
3899
c6515272
SS
3900 if (udev->speed != USB_SPEED_SUPER)
3901 dev_info(&udev->dev,
e538dfda
MN
3902 "%s %s USB device number %d using %s\n",
3903 (udev->config) ? "reset" : "new", speed,
3b29b68b 3904 devnum, udev->bus->controller->driver->name);
1da177e4
LT
3905
3906 /* Set up TT records, if needed */
3907 if (hdev->tt) {
3908 udev->tt = hdev->tt;
3909 udev->ttport = hdev->ttport;
3910 } else if (udev->speed != USB_SPEED_HIGH
3911 && hdev->speed == USB_SPEED_HIGH) {
d199c96d
AS
3912 if (!hub->tt.hub) {
3913 dev_err(&udev->dev, "parent hub has no TT\n");
3914 retval = -EINVAL;
3915 goto fail;
3916 }
1da177e4
LT
3917 udev->tt = &hub->tt;
3918 udev->ttport = port1;
3919 }
3920
3921 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3922 * Because device hardware and firmware is sometimes buggy in
3923 * this area, and this is how Linux has done it for ages.
3924 * Change it cautiously.
3925 *
3926 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3927 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3928 * so it may help with some non-standards-compliant devices.
3929 * Otherwise we start with SET_ADDRESS and then try to read the
3930 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3931 * value.
3932 */
3933 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
c6515272 3934 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
1da177e4
LT
3935 struct usb_device_descriptor *buf;
3936 int r = 0;
3937
3938#define GET_DESCRIPTOR_BUFSIZE 64
3939 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3940 if (!buf) {
3941 retval = -ENOMEM;
3942 continue;
3943 }
3944
b89ee19a
AS
3945 /* Retry on all errors; some devices are flakey.
3946 * 255 is for WUSB devices, we actually need to use
3947 * 512 (WUSB1.0[4.8.1]).
1da177e4
LT
3948 */
3949 for (j = 0; j < 3; ++j) {
3950 buf->bMaxPacketSize0 = 0;
3951 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
3952 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
3953 USB_DT_DEVICE << 8, 0,
3954 buf, GET_DESCRIPTOR_BUFSIZE,
fd7c519d 3955 initial_descriptor_timeout);
1da177e4 3956 switch (buf->bMaxPacketSize0) {
5bb6e0ae 3957 case 8: case 16: case 32: case 64: case 255:
1da177e4
LT
3958 if (buf->bDescriptorType ==
3959 USB_DT_DEVICE) {
3960 r = 0;
3961 break;
3962 }
3963 /* FALL THROUGH */
3964 default:
3965 if (r == 0)
3966 r = -EPROTO;
3967 break;
3968 }
3969 if (r == 0)
3970 break;
3971 }
3972 udev->descriptor.bMaxPacketSize0 =
3973 buf->bMaxPacketSize0;
3974 kfree(buf);
3975
75d7cf72 3976 retval = hub_port_reset(hub, port1, udev, delay, false);
1da177e4
LT
3977 if (retval < 0) /* error or disconnect */
3978 goto fail;
3979 if (oldspeed != udev->speed) {
3980 dev_dbg(&udev->dev,
3981 "device reset changed speed!\n");
3982 retval = -ENODEV;
3983 goto fail;
3984 }
3985 if (r) {
b9cef6c3
WF
3986 dev_err(&udev->dev,
3987 "device descriptor read/64, error %d\n",
3988 r);
1da177e4
LT
3989 retval = -EMSGSIZE;
3990 continue;
3991 }
3992#undef GET_DESCRIPTOR_BUFSIZE
3993 }
3994
6c529cdc
IPG
3995 /*
3996 * If device is WUSB, we already assigned an
3997 * unauthorized address in the Connect Ack sequence;
3998 * authorization will assign the final address.
3999 */
c6515272 4000 if (udev->wusb == 0) {
6c529cdc
IPG
4001 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
4002 retval = hub_set_address(udev, devnum);
4003 if (retval >= 0)
4004 break;
4005 msleep(200);
4006 }
4007 if (retval < 0) {
4008 dev_err(&udev->dev,
4009 "device not accepting address %d, error %d\n",
4010 devnum, retval);
4011 goto fail;
4012 }
c6515272
SS
4013 if (udev->speed == USB_SPEED_SUPER) {
4014 devnum = udev->devnum;
4015 dev_info(&udev->dev,
3b29b68b 4016 "%s SuperSpeed USB device number %d using %s\n",
c6515272 4017 (udev->config) ? "reset" : "new",
3b29b68b 4018 devnum, udev->bus->controller->driver->name);
c6515272 4019 }
6c529cdc
IPG
4020
4021 /* cope with hardware quirkiness:
4022 * - let SET_ADDRESS settle, some device hardware wants it
4023 * - read ep0 maxpacket even for high and low speed,
4024 */
4025 msleep(10);
c6515272 4026 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
1da177e4 4027 break;
6c529cdc 4028 }
1da177e4
LT
4029
4030 retval = usb_get_device_descriptor(udev, 8);
4031 if (retval < 8) {
b9cef6c3
WF
4032 dev_err(&udev->dev,
4033 "device descriptor read/8, error %d\n",
4034 retval);
1da177e4
LT
4035 if (retval >= 0)
4036 retval = -EMSGSIZE;
4037 } else {
4038 retval = 0;
4039 break;
4040 }
4041 }
4042 if (retval)
4043 goto fail;
4044
d8aec3db
EF
4045 /*
4046 * Some superspeed devices have finished the link training process
4047 * and attached to a superspeed hub port, but the device descriptor
4048 * got from those devices show they aren't superspeed devices. Warm
4049 * reset the port attached by the devices can fix them.
4050 */
4051 if ((udev->speed == USB_SPEED_SUPER) &&
4052 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4053 dev_err(&udev->dev, "got a wrong device descriptor, "
4054 "warm reset device\n");
4055 hub_port_reset(hub, port1, udev,
4056 HUB_BH_RESET_TIME, true);
4057 retval = -EINVAL;
4058 goto fail;
4059 }
4060
6b403b02
SS
4061 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
4062 udev->speed == USB_SPEED_SUPER)
4063 i = 512;
4064 else
4065 i = udev->descriptor.bMaxPacketSize0;
29cc8897 4066 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
56626a72 4067 if (udev->speed == USB_SPEED_LOW ||
1da177e4 4068 !(i == 8 || i == 16 || i == 32 || i == 64)) {
56626a72 4069 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
1da177e4
LT
4070 retval = -EMSGSIZE;
4071 goto fail;
4072 }
56626a72
AS
4073 if (udev->speed == USB_SPEED_FULL)
4074 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4075 else
4076 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
1da177e4 4077 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
fc721f51 4078 usb_ep0_reinit(udev);
1da177e4
LT
4079 }
4080
4081 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4082 if (retval < (signed)sizeof(udev->descriptor)) {
b9cef6c3
WF
4083 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4084 retval);
1da177e4
LT
4085 if (retval >= 0)
4086 retval = -ENOMSG;
4087 goto fail;
4088 }
4089
1ff4df56
AX
4090 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4091 retval = usb_get_bos_descriptor(udev);
51e0a012 4092 if (!retval) {
d9b2099c 4093 udev->lpm_capable = usb_device_supports_lpm(udev);
51e0a012
SS
4094 usb_set_lpm_parameters(udev);
4095 }
1ff4df56 4096 }
3148bf04 4097
1da177e4 4098 retval = 0;
48f24970
AD
4099 /* notify HCD that we have a device connected and addressed */
4100 if (hcd->driver->update_device)
4101 hcd->driver->update_device(hcd, udev);
1da177e4 4102fail:
4326ed0b 4103 if (retval) {
1da177e4 4104 hub_port_disable(hub, port1, 0);
3b29b68b 4105 update_devnum(udev, devnum); /* for disconnect processing */
4326ed0b 4106 }
4186ecf8 4107 mutex_unlock(&usb_address0_mutex);
1da177e4
LT
4108 return retval;
4109}
4110
4111static void
4112check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4113{
4114 struct usb_qualifier_descriptor *qual;
4115 int status;
4116
e94b1766 4117 qual = kmalloc (sizeof *qual, GFP_KERNEL);
1da177e4
LT
4118 if (qual == NULL)
4119 return;
4120
4121 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4122 qual, sizeof *qual);
4123 if (status == sizeof *qual) {
4124 dev_info(&udev->dev, "not running at top speed; "
4125 "connect to a high speed hub\n");
4126 /* hub LEDs are probably harder to miss than syslog */
4127 if (hub->has_indicators) {
4128 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
c4028958 4129 schedule_delayed_work (&hub->leds, 0);
1da177e4
LT
4130 }
4131 }
1bc3c9e1 4132 kfree(qual);
1da177e4
LT
4133}
4134
4135static unsigned
4136hub_power_remaining (struct usb_hub *hub)
4137{
4138 struct usb_device *hdev = hub->hdev;
4139 int remaining;
55c52718 4140 int port1;
1da177e4 4141
55c52718 4142 if (!hub->limited_power)
1da177e4
LT
4143 return 0;
4144
55c52718
AS
4145 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4146 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
ff823c79 4147 struct usb_device *udev = hub->ports[port1 - 1]->child;
55c52718 4148 int delta;
1da177e4
LT
4149
4150 if (!udev)
4151 continue;
4152
55c52718
AS
4153 /* Unconfigured devices may not use more than 100mA,
4154 * or 8mA for OTG ports */
1da177e4 4155 if (udev->actconfig)
55c52718
AS
4156 delta = udev->actconfig->desc.bMaxPower * 2;
4157 else if (port1 != udev->bus->otg_port || hdev->parent)
4158 delta = 100;
1da177e4 4159 else
55c52718
AS
4160 delta = 8;
4161 if (delta > hub->mA_per_port)
b9cef6c3
WF
4162 dev_warn(&udev->dev,
4163 "%dmA is over %umA budget for port %d!\n",
4164 delta, hub->mA_per_port, port1);
1da177e4
LT
4165 remaining -= delta;
4166 }
4167 if (remaining < 0) {
55c52718
AS
4168 dev_warn(hub->intfdev, "%dmA over power budget!\n",
4169 - remaining);
1da177e4
LT
4170 remaining = 0;
4171 }
4172 return remaining;
4173}
4174
4175/* Handle physical or logical connection change events.
4176 * This routine is called when:
4177 * a port connection-change occurs;
4178 * a port enable-change occurs (often caused by EMI);
742120c6 4179 * usb_reset_and_verify_device() encounters changed descriptors (as from
1da177e4
LT
4180 * a firmware download)
4181 * caller already locked the hub
4182 */
4183static void hub_port_connect_change(struct usb_hub *hub, int port1,
4184 u16 portstatus, u16 portchange)
4185{
4186 struct usb_device *hdev = hub->hdev;
4187 struct device *hub_dev = hub->intfdev;
90da096e 4188 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
24618b0c
AS
4189 unsigned wHubCharacteristics =
4190 le16_to_cpu(hub->descriptor->wHubCharacteristics);
8808f00c 4191 struct usb_device *udev;
1da177e4 4192 int status, i;
24618b0c 4193
1da177e4
LT
4194 dev_dbg (hub_dev,
4195 "port %d, status %04x, change %04x, %s\n",
131dec34 4196 port1, portstatus, portchange, portspeed(hub, portstatus));
1da177e4
LT
4197
4198 if (hub->has_indicators) {
4199 set_port_led(hub, port1, HUB_LED_AUTO);
4200 hub->indicator[port1-1] = INDICATOR_AUTO;
4201 }
1da177e4
LT
4202
4203#ifdef CONFIG_USB_OTG
4204 /* during HNP, don't repeat the debounce */
4205 if (hdev->bus->is_b_host)
24618b0c
AS
4206 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4207 USB_PORT_STAT_C_ENABLE);
1da177e4
LT
4208#endif
4209
8808f00c 4210 /* Try to resuscitate an existing device */
ff823c79 4211 udev = hub->ports[port1 - 1]->child;
8808f00c
AS
4212 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4213 udev->state != USB_STATE_NOTATTACHED) {
8808f00c
AS
4214 usb_lock_device(udev);
4215 if (portstatus & USB_PORT_STAT_ENABLE) {
4216 status = 0; /* Nothing to do */
8808f00c
AS
4217
4218#ifdef CONFIG_USB_SUSPEND
5257d97a
AS
4219 } else if (udev->state == USB_STATE_SUSPENDED &&
4220 udev->persist_enabled) {
8808f00c
AS
4221 /* For a suspended device, treat this as a
4222 * remote wakeup event.
4223 */
0534d468 4224 status = usb_remote_wakeup(udev);
8808f00c
AS
4225#endif
4226
4227 } else {
5257d97a 4228 status = -ENODEV; /* Don't resuscitate */
8808f00c
AS
4229 }
4230 usb_unlock_device(udev);
4231
4232 if (status == 0) {
4233 clear_bit(port1, hub->change_bits);
4234 return;
4235 }
4236 }
4237
24618b0c 4238 /* Disconnect any existing devices under this port */
8808f00c 4239 if (udev)
ff823c79 4240 usb_disconnect(&hub->ports[port1 - 1]->child);
24618b0c
AS
4241 clear_bit(port1, hub->change_bits);
4242
253e0572
AS
4243 /* We can forget about a "removed" device when there's a physical
4244 * disconnect or the connect status changes.
4245 */
4246 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4247 (portchange & USB_PORT_STAT_C_CONNECTION))
4248 clear_bit(port1, hub->removed_bits);
4249
5257d97a
AS
4250 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4251 USB_PORT_STAT_C_ENABLE)) {
4252 status = hub_port_debounce(hub, port1);
4253 if (status < 0) {
4254 if (printk_ratelimit())
4255 dev_err(hub_dev, "connect-debounce failed, "
4256 "port %d disabled\n", port1);
4257 portstatus &= ~USB_PORT_STAT_CONNECTION;
4258 } else {
4259 portstatus = status;
4260 }
4261 }
4262
925aa46b
RZ
4263 if (hcd->phy && !hdev->parent) {
4264 if (portstatus & USB_PORT_STAT_CONNECTION)
4265 usb_phy_notify_connect(hcd->phy, port1);
4266 else
4267 usb_phy_notify_disconnect(hcd->phy, port1);
4268 }
4269
253e0572
AS
4270 /* Return now if debouncing failed or nothing is connected or
4271 * the device was "removed".
4272 */
4273 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4274 test_bit(port1, hub->removed_bits)) {
1da177e4
LT
4275
4276 /* maybe switch power back on (e.g. root hub was reset) */
74ad9bd2 4277 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
0ed9a57e 4278 && !port_is_power_on(hub, portstatus))
1da177e4 4279 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
5257d97a 4280
1da177e4
LT
4281 if (portstatus & USB_PORT_STAT_ENABLE)
4282 goto done;
4283 return;
4284 }
4285
1da177e4 4286 for (i = 0; i < SET_CONFIG_TRIES; i++) {
1da177e4
LT
4287
4288 /* reallocate for each attempt, since references
4289 * to the previous one can escape in various ways
4290 */
4291 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4292 if (!udev) {
4293 dev_err (hub_dev,
4294 "couldn't allocate port %d usb_device\n",
4295 port1);
4296 goto done;
4297 }
4298
4299 usb_set_device_state(udev, USB_STATE_POWERED);
55c52718 4300 udev->bus_mA = hub->mA_per_port;
b6956ffa 4301 udev->level = hdev->level + 1;
8af548dc 4302 udev->wusb = hub_is_wusb(hub);
55c52718 4303
131dec34
SS
4304 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4305 if (hub_is_superspeed(hub->hdev))
e7b77172
SS
4306 udev->speed = USB_SPEED_SUPER;
4307 else
4308 udev->speed = USB_SPEED_UNKNOWN;
4309
3b29b68b 4310 choose_devnum(udev);
c8d4af8e
AX
4311 if (udev->devnum <= 0) {
4312 status = -ENOTCONN; /* Don't retry */
4313 goto loop;
c6515272
SS
4314 }
4315
e7b77172 4316 /* reset (non-USB 3.0 devices) and get descriptor */
1da177e4
LT
4317 status = hub_port_init(hub, udev, port1, i);
4318 if (status < 0)
4319 goto loop;
4320
93362a87
PD
4321 usb_detect_quirks(udev);
4322 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4323 msleep(1000);
4324
1da177e4
LT
4325 /* consecutive bus-powered hubs aren't reliable; they can
4326 * violate the voltage drop budget. if the new child has
4327 * a "powered" LED, users should notice we didn't enable it
4328 * (without reading syslog), even without per-port LEDs
4329 * on the parent.
4330 */
4331 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
55c52718 4332 && udev->bus_mA <= 100) {
1da177e4
LT
4333 u16 devstat;
4334
4335 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4336 &devstat);
55c52718 4337 if (status < 2) {
1da177e4
LT
4338 dev_dbg(&udev->dev, "get status %d ?\n", status);
4339 goto loop_disable;
4340 }
55c52718 4341 le16_to_cpus(&devstat);
1da177e4
LT
4342 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4343 dev_err(&udev->dev,
4344 "can't connect bus-powered hub "
4345 "to this port\n");
4346 if (hub->has_indicators) {
4347 hub->indicator[port1-1] =
4348 INDICATOR_AMBER_BLINK;
c4028958 4349 schedule_delayed_work (&hub->leds, 0);
1da177e4
LT
4350 }
4351 status = -ENOTCONN; /* Don't retry */
4352 goto loop_disable;
4353 }
4354 }
4355
4356 /* check for devices running slower than they could */
4357 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4358 && udev->speed == USB_SPEED_FULL
4359 && highspeed_hubs != 0)
4360 check_highspeed (hub, udev, port1);
4361
fa286188 4362 /* Store the parent's children[] pointer. At this point
1da177e4
LT
4363 * udev becomes globally accessible, although presumably
4364 * no one will look at it until hdev is unlocked.
4365 */
1da177e4
LT
4366 status = 0;
4367
4368 /* We mustn't add new devices if the parent hub has
4369 * been disconnected; we would race with the
4370 * recursively_mark_NOTATTACHED() routine.
4371 */
4372 spin_lock_irq(&device_state_lock);
4373 if (hdev->state == USB_STATE_NOTATTACHED)
4374 status = -ENOTCONN;
4375 else
ff823c79 4376 hub->ports[port1 - 1]->child = udev;
1da177e4
LT
4377 spin_unlock_irq(&device_state_lock);
4378
4379 /* Run it through the hoops (find a driver, etc) */
4380 if (!status) {
4381 status = usb_new_device(udev);
4382 if (status) {
4383 spin_lock_irq(&device_state_lock);
ff823c79 4384 hub->ports[port1 - 1]->child = NULL;
1da177e4
LT
4385 spin_unlock_irq(&device_state_lock);
4386 }
4387 }
4388
1da177e4
LT
4389 if (status)
4390 goto loop_disable;
4391
4392 status = hub_power_remaining(hub);
4393 if (status)
55c52718 4394 dev_dbg(hub_dev, "%dmA power budget left\n", status);
1da177e4
LT
4395
4396 return;
4397
4398loop_disable:
4399 hub_port_disable(hub, port1, 1);
4400loop:
fc721f51 4401 usb_ep0_reinit(udev);
3b29b68b 4402 release_devnum(udev);
f7410ced 4403 hub_free_dev(udev);
1da177e4 4404 usb_put_dev(udev);
ffcdc18d 4405 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
1da177e4
LT
4406 break;
4407 }
3a31155c
AS
4408 if (hub->hdev->parent ||
4409 !hcd->driver->port_handed_over ||
4410 !(hcd->driver->port_handed_over)(hcd, port1))
4411 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4412 port1);
1da177e4
LT
4413
4414done:
4415 hub_port_disable(hub, port1, 1);
90da096e
BR
4416 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4417 hcd->driver->relinquish_port(hcd, port1);
1da177e4
LT
4418}
4419
714b07be
SS
4420/* Returns 1 if there was a remote wakeup and a connect status change. */
4421static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
72937e1e 4422 u16 portstatus, u16 portchange)
714b07be
SS
4423{
4424 struct usb_device *hdev;
4425 struct usb_device *udev;
4426 int connect_change = 0;
4427 int ret;
4428
4429 hdev = hub->hdev;
ff823c79 4430 udev = hub->ports[port - 1]->child;
4ee823b8
SS
4431 if (!hub_is_superspeed(hdev)) {
4432 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4433 return 0;
4434 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
4435 } else {
4436 if (!udev || udev->state != USB_STATE_SUSPENDED ||
72937e1e
SS
4437 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4438 USB_SS_PORT_LS_U0)
4ee823b8
SS
4439 return 0;
4440 }
4441
714b07be
SS
4442 if (udev) {
4443 /* TRSMRCY = 10 msec */
4444 msleep(10);
4445
4446 usb_lock_device(udev);
4447 ret = usb_remote_wakeup(udev);
4448 usb_unlock_device(udev);
4449 if (ret < 0)
4450 connect_change = 1;
4451 } else {
4452 ret = -ENODEV;
4453 hub_port_disable(hub, port, 1);
4454 }
4455 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4456 port, ret);
4457 return connect_change;
4458}
4459
1da177e4
LT
4460static void hub_events(void)
4461{
4462 struct list_head *tmp;
4463 struct usb_device *hdev;
4464 struct usb_interface *intf;
4465 struct usb_hub *hub;
4466 struct device *hub_dev;
4467 u16 hubstatus;
4468 u16 hubchange;
4469 u16 portstatus;
4470 u16 portchange;
4471 int i, ret;
4ee823b8 4472 int connect_change, wakeup_change;
1da177e4
LT
4473
4474 /*
4475 * We restart the list every time to avoid a deadlock with
4476 * deleting hubs downstream from this one. This should be
4477 * safe since we delete the hub from the event list.
4478 * Not the most efficient, but avoids deadlocks.
4479 */
4480 while (1) {
4481
4482 /* Grab the first entry at the beginning of the list */
4483 spin_lock_irq(&hub_event_lock);
4484 if (list_empty(&hub_event_list)) {
4485 spin_unlock_irq(&hub_event_lock);
4486 break;
4487 }
4488
4489 tmp = hub_event_list.next;
4490 list_del_init(tmp);
4491
4492 hub = list_entry(tmp, struct usb_hub, event_list);
e8054854
AS
4493 kref_get(&hub->kref);
4494 spin_unlock_irq(&hub_event_lock);
1da177e4 4495
e8054854
AS
4496 hdev = hub->hdev;
4497 hub_dev = hub->intfdev;
4498 intf = to_usb_interface(hub_dev);
40f122f3 4499 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
1da177e4
LT
4500 hdev->state, hub->descriptor
4501 ? hub->descriptor->bNbrPorts
4502 : 0,
4503 /* NOTE: expects max 15 ports... */
4504 (u16) hub->change_bits[0],
40f122f3 4505 (u16) hub->event_bits[0]);
1da177e4 4506
1da177e4
LT
4507 /* Lock the device, then check to see if we were
4508 * disconnected while waiting for the lock to succeed. */
06b84e8a 4509 usb_lock_device(hdev);
e8054854 4510 if (unlikely(hub->disconnected))
9bbdf1e0 4511 goto loop_disconnected;
1da177e4
LT
4512
4513 /* If the hub has died, clean up after it */
4514 if (hdev->state == USB_STATE_NOTATTACHED) {
7de18d8b 4515 hub->error = -ENODEV;
4330354f 4516 hub_quiesce(hub, HUB_DISCONNECT);
1da177e4
LT
4517 goto loop;
4518 }
4519
40f122f3
AS
4520 /* Autoresume */
4521 ret = usb_autopm_get_interface(intf);
4522 if (ret) {
4523 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
4524 goto loop;
4525 }
a8e7c565 4526
40f122f3 4527 /* If this is an inactive hub, do nothing */
1da177e4 4528 if (hub->quiescing)
40f122f3 4529 goto loop_autopm;
1da177e4
LT
4530
4531 if (hub->error) {
4532 dev_dbg (hub_dev, "resetting for error %d\n",
4533 hub->error);
4534
742120c6 4535 ret = usb_reset_device(hdev);
1da177e4
LT
4536 if (ret) {
4537 dev_dbg (hub_dev,
4538 "error resetting hub: %d\n", ret);
40f122f3 4539 goto loop_autopm;
1da177e4
LT
4540 }
4541
4542 hub->nerrors = 0;
4543 hub->error = 0;
4544 }
4545
4546 /* deal with port status changes */
4547 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4548 if (test_bit(i, hub->busy_bits))
4549 continue;
4550 connect_change = test_bit(i, hub->change_bits);
72937e1e 4551 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
1da177e4 4552 if (!test_and_clear_bit(i, hub->event_bits) &&
4ee823b8 4553 !connect_change && !wakeup_change)
1da177e4
LT
4554 continue;
4555
4556 ret = hub_port_status(hub, i,
4557 &portstatus, &portchange);
4558 if (ret < 0)
4559 continue;
4560
1da177e4
LT
4561 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4562 clear_port_feature(hdev, i,
4563 USB_PORT_FEAT_C_CONNECTION);
4564 connect_change = 1;
4565 }
4566
4567 if (portchange & USB_PORT_STAT_C_ENABLE) {
4568 if (!connect_change)
4569 dev_dbg (hub_dev,
4570 "port %d enable change, "
4571 "status %08x\n",
4572 i, portstatus);
4573 clear_port_feature(hdev, i,
4574 USB_PORT_FEAT_C_ENABLE);
4575
4576 /*
4577 * EM interference sometimes causes badly
4578 * shielded USB devices to be shutdown by
4579 * the hub, this hack enables them again.
4580 * Works at least with mouse driver.
4581 */
4582 if (!(portstatus & USB_PORT_STAT_ENABLE)
4583 && !connect_change
ff823c79 4584 && hub->ports[i - 1]->child) {
1da177e4
LT
4585 dev_err (hub_dev,
4586 "port %i "
4587 "disabled by hub (EMI?), "
4588 "re-enabling...\n",
4589 i);
4590 connect_change = 1;
4591 }
4592 }
4593
72937e1e
SS
4594 if (hub_handle_remote_wakeup(hub, i,
4595 portstatus, portchange))
714b07be 4596 connect_change = 1;
8808f00c 4597
1da177e4 4598 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
752d57a8
PB
4599 u16 status = 0;
4600 u16 unused;
4601
4602 dev_dbg(hub_dev, "over-current change on port "
4603 "%d\n", i);
1da177e4
LT
4604 clear_port_feature(hdev, i,
4605 USB_PORT_FEAT_C_OVER_CURRENT);
752d57a8 4606 msleep(100); /* Cool down */
8520f380 4607 hub_power_on(hub, true);
752d57a8
PB
4608 hub_port_status(hub, i, &status, &unused);
4609 if (status & USB_PORT_STAT_OVERCURRENT)
4610 dev_err(hub_dev, "over-current "
4611 "condition on port %d\n", i);
1da177e4
LT
4612 }
4613
4614 if (portchange & USB_PORT_STAT_C_RESET) {
4615 dev_dbg (hub_dev,
4616 "reset change on port %d\n",
4617 i);
4618 clear_port_feature(hdev, i,
4619 USB_PORT_FEAT_C_RESET);
4620 }
c7061574
SS
4621 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4622 hub_is_superspeed(hub->hdev)) {
4623 dev_dbg(hub_dev,
4624 "warm reset change on port %d\n",
4625 i);
4626 clear_port_feature(hdev, i,
4627 USB_PORT_FEAT_C_BH_PORT_RESET);
4628 }
dbe79bbe
JY
4629 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4630 clear_port_feature(hub->hdev, i,
4631 USB_PORT_FEAT_C_PORT_LINK_STATE);
4632 }
4633 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4634 dev_warn(hub_dev,
4635 "config error on port %d\n",
4636 i);
4637 clear_port_feature(hub->hdev, i,
4638 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4639 }
1da177e4 4640
5e467f6e
AX
4641 /* Warm reset a USB3 protocol port if it's in
4642 * SS.Inactive state.
4643 */
8bea2bd3 4644 if (hub_port_warm_reset_required(hub, portstatus)) {
5e467f6e 4645 dev_dbg(hub_dev, "warm reset port %d\n", i);
75d7cf72
AX
4646 hub_port_reset(hub, i, NULL,
4647 HUB_BH_RESET_TIME, true);
5e467f6e
AX
4648 }
4649
1da177e4
LT
4650 if (connect_change)
4651 hub_port_connect_change(hub, i,
4652 portstatus, portchange);
4653 } /* end for i */
4654
4655 /* deal with hub status changes */
4656 if (test_and_clear_bit(0, hub->event_bits) == 0)
4657 ; /* do nothing */
4658 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4659 dev_err (hub_dev, "get_hub_status failed\n");
4660 else {
4661 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4662 dev_dbg (hub_dev, "power change\n");
4663 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
55c52718
AS
4664 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4665 /* FIXME: Is this always true? */
55c52718 4666 hub->limited_power = 1;
403fae78 4667 else
4668 hub->limited_power = 0;
1da177e4
LT
4669 }
4670 if (hubchange & HUB_CHANGE_OVERCURRENT) {
752d57a8
PB
4671 u16 status = 0;
4672 u16 unused;
4673
4674 dev_dbg(hub_dev, "over-current change\n");
1da177e4 4675 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
752d57a8 4676 msleep(500); /* Cool down */
8520f380 4677 hub_power_on(hub, true);
752d57a8
PB
4678 hub_hub_status(hub, &status, &unused);
4679 if (status & HUB_STATUS_OVERCURRENT)
4680 dev_err(hub_dev, "over-current "
4681 "condition\n");
1da177e4
LT
4682 }
4683 }
4684
8e4ceb38
AS
4685 loop_autopm:
4686 /* Balance the usb_autopm_get_interface() above */
4687 usb_autopm_put_interface_no_suspend(intf);
4688 loop:
4689 /* Balance the usb_autopm_get_interface_no_resume() in
4690 * kick_khubd() and allow autosuspend.
4691 */
4692 usb_autopm_put_interface(intf);
9bbdf1e0 4693 loop_disconnected:
1da177e4 4694 usb_unlock_device(hdev);
e8054854 4695 kref_put(&hub->kref, hub_release);
1da177e4
LT
4696
4697 } /* end while (1) */
4698}
4699
4700static int hub_thread(void *__unused)
4701{
3bb1af52
AS
4702 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4703 * port handover. Otherwise it might see that a full-speed device
4704 * was gone before the EHCI controller had handed its port over to
4705 * the companion full-speed controller.
4706 */
83144186 4707 set_freezable();
3bb1af52 4708
1da177e4
LT
4709 do {
4710 hub_events();
e42837bc 4711 wait_event_freezable(khubd_wait,
9c8d6178
AM
4712 !list_empty(&hub_event_list) ||
4713 kthread_should_stop());
9c8d6178 4714 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
1da177e4 4715
9c8d6178
AM
4716 pr_debug("%s: khubd exiting\n", usbcore_name);
4717 return 0;
1da177e4
LT
4718}
4719
1e927d96 4720static const struct usb_device_id hub_id_table[] = {
e6f30dea
ML
4721 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
4722 | USB_DEVICE_ID_MATCH_INT_CLASS,
4723 .idVendor = USB_VENDOR_GENESYS_LOGIC,
4724 .bInterfaceClass = USB_CLASS_HUB,
4725 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
1da177e4
LT
4726 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4727 .bDeviceClass = USB_CLASS_HUB},
4728 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4729 .bInterfaceClass = USB_CLASS_HUB},
4730 { } /* Terminating entry */
4731};
4732
4733MODULE_DEVICE_TABLE (usb, hub_id_table);
4734
4735static struct usb_driver hub_driver = {
1da177e4
LT
4736 .name = "hub",
4737 .probe = hub_probe,
4738 .disconnect = hub_disconnect,
4739 .suspend = hub_suspend,
4740 .resume = hub_resume,
f07600cf 4741 .reset_resume = hub_reset_resume,
7de18d8b
AS
4742 .pre_reset = hub_pre_reset,
4743 .post_reset = hub_post_reset,
c532b29a 4744 .unlocked_ioctl = hub_ioctl,
1da177e4 4745 .id_table = hub_id_table,
40f122f3 4746 .supports_autosuspend = 1,
1da177e4
LT
4747};
4748
4749int usb_hub_init(void)
4750{
1da177e4
LT
4751 if (usb_register(&hub_driver) < 0) {
4752 printk(KERN_ERR "%s: can't register hub driver\n",
4753 usbcore_name);
4754 return -1;
4755 }
4756
9c8d6178
AM
4757 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4758 if (!IS_ERR(khubd_task))
1da177e4 4759 return 0;
1da177e4
LT
4760
4761 /* Fall through if kernel_thread failed */
4762 usb_deregister(&hub_driver);
4763 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4764
4765 return -1;
4766}
4767
4768void usb_hub_cleanup(void)
4769{
9c8d6178 4770 kthread_stop(khubd_task);
1da177e4
LT
4771
4772 /*
4773 * Hub resources are freed for us by usb_deregister. It calls
4774 * usb_driver_purge on every device which in turn calls that
4775 * devices disconnect function if it is using this driver.
4776 * The hub_disconnect function takes care of releasing the
4777 * individual hub resources. -greg
4778 */
4779 usb_deregister(&hub_driver);
4780} /* usb_hub_cleanup() */
4781
eb764c4b
AS
4782static int descriptors_changed(struct usb_device *udev,
4783 struct usb_device_descriptor *old_device_descriptor)
4784{
4785 int changed = 0;
4786 unsigned index;
4787 unsigned serial_len = 0;
4788 unsigned len;
4789 unsigned old_length;
4790 int length;
4791 char *buf;
4792
4793 if (memcmp(&udev->descriptor, old_device_descriptor,
4794 sizeof(*old_device_descriptor)) != 0)
4795 return 1;
4796
4797 /* Since the idVendor, idProduct, and bcdDevice values in the
4798 * device descriptor haven't changed, we will assume the
4799 * Manufacturer and Product strings haven't changed either.
4800 * But the SerialNumber string could be different (e.g., a
4801 * different flash card of the same brand).
4802 */
4803 if (udev->serial)
4804 serial_len = strlen(udev->serial) + 1;
1da177e4 4805
eb764c4b 4806 len = serial_len;
1da177e4 4807 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
eb764c4b
AS
4808 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4809 len = max(len, old_length);
1da177e4 4810 }
eb764c4b 4811
0cc1a51f 4812 buf = kmalloc(len, GFP_NOIO);
1da177e4
LT
4813 if (buf == NULL) {
4814 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4815 /* assume the worst */
4816 return 1;
4817 }
4818 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
eb764c4b 4819 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
1da177e4
LT
4820 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4821 old_length);
eb764c4b 4822 if (length != old_length) {
1da177e4
LT
4823 dev_dbg(&udev->dev, "config index %d, error %d\n",
4824 index, length);
eb764c4b 4825 changed = 1;
1da177e4
LT
4826 break;
4827 }
4828 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4829 != 0) {
4830 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
eb764c4b
AS
4831 index,
4832 ((struct usb_config_descriptor *) buf)->
4833 bConfigurationValue);
4834 changed = 1;
1da177e4
LT
4835 break;
4836 }
4837 }
eb764c4b
AS
4838
4839 if (!changed && serial_len) {
4840 length = usb_string(udev, udev->descriptor.iSerialNumber,
4841 buf, serial_len);
4842 if (length + 1 != serial_len) {
4843 dev_dbg(&udev->dev, "serial string error %d\n",
4844 length);
4845 changed = 1;
4846 } else if (memcmp(buf, udev->serial, length) != 0) {
4847 dev_dbg(&udev->dev, "serial string changed\n");
4848 changed = 1;
4849 }
4850 }
4851
1da177e4 4852 kfree(buf);
eb764c4b 4853 return changed;
1da177e4
LT
4854}
4855
4856/**
742120c6 4857 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
1da177e4
LT
4858 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4859 *
79efa097
AS
4860 * WARNING - don't use this routine to reset a composite device
4861 * (one with multiple interfaces owned by separate drivers)!
742120c6 4862 * Use usb_reset_device() instead.
1da177e4
LT
4863 *
4864 * Do a port reset, reassign the device's address, and establish its
4865 * former operating configuration. If the reset fails, or the device's
4866 * descriptors change from their values before the reset, or the original
4867 * configuration and altsettings cannot be restored, a flag will be set
4868 * telling khubd to pretend the device has been disconnected and then
4869 * re-connected. All drivers will be unbound, and the device will be
4870 * re-enumerated and probed all over again.
4871 *
4872 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4873 * flagged for logical disconnection, or some other negative error code
4874 * if the reset wasn't even attempted.
4875 *
4876 * The caller must own the device lock. For example, it's safe to use
4877 * this from a driver probe() routine after downloading new firmware.
4878 * For calls that might not occur during probe(), drivers should lock
4879 * the device using usb_lock_device_for_reset().
6bc6cff5
AS
4880 *
4881 * Locking exception: This routine may also be called from within an
4882 * autoresume handler. Such usage won't conflict with other tasks
4883 * holding the device lock because these tasks should always call
4884 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
1da177e4 4885 */
742120c6 4886static int usb_reset_and_verify_device(struct usb_device *udev)
1da177e4
LT
4887{
4888 struct usb_device *parent_hdev = udev->parent;
4889 struct usb_hub *parent_hub;
3f0479e0 4890 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1da177e4 4891 struct usb_device_descriptor descriptor = udev->descriptor;
12c3da34
AS
4892 int i, ret = 0;
4893 int port1 = udev->portnum;
1da177e4
LT
4894
4895 if (udev->state == USB_STATE_NOTATTACHED ||
4896 udev->state == USB_STATE_SUSPENDED) {
4897 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4898 udev->state);
4899 return -EINVAL;
4900 }
4901
4902 if (!parent_hdev) {
4bec9917 4903 /* this requires hcd-specific logic; see ohci_restart() */
441b62c1 4904 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
1da177e4
LT
4905 return -EISDIR;
4906 }
1da177e4
LT
4907 parent_hub = hdev_to_hub(parent_hdev);
4908
f74631e3
SS
4909 /* Disable LPM and LTM while we reset the device and reinstall the alt
4910 * settings. Device-initiated LPM settings, and system exit latency
4911 * settings are cleared when the device is reset, so we have to set
4912 * them up again.
6d1d0513
SS
4913 */
4914 ret = usb_unlocked_disable_lpm(udev);
4915 if (ret) {
4916 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
4917 goto re_enumerate;
4918 }
f74631e3
SS
4919 ret = usb_disable_ltm(udev);
4920 if (ret) {
4921 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
4922 __func__);
4923 goto re_enumerate;
4924 }
6d1d0513 4925
1da177e4
LT
4926 set_bit(port1, parent_hub->busy_bits);
4927 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4928
4929 /* ep0 maxpacket size may change; let the HCD know about it.
4930 * Other endpoints will be handled by re-enumeration. */
fc721f51 4931 usb_ep0_reinit(udev);
1da177e4 4932 ret = hub_port_init(parent_hub, udev, port1, i);
dd4dd19e 4933 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
1da177e4
LT
4934 break;
4935 }
4936 clear_bit(port1, parent_hub->busy_bits);
d5cbad4b 4937
1da177e4
LT
4938 if (ret < 0)
4939 goto re_enumerate;
4940
4941 /* Device might have changed firmware (DFU or similar) */
eb764c4b 4942 if (descriptors_changed(udev, &descriptor)) {
1da177e4
LT
4943 dev_info(&udev->dev, "device firmware changed\n");
4944 udev->descriptor = descriptor; /* for disconnect() calls */
4945 goto re_enumerate;
4946 }
4fe0387a
AS
4947
4948 /* Restore the device's previous configuration */
1da177e4
LT
4949 if (!udev->actconfig)
4950 goto done;
3f0479e0 4951
d673bfcb 4952 mutex_lock(hcd->bandwidth_mutex);
3f0479e0
SS
4953 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
4954 if (ret < 0) {
4955 dev_warn(&udev->dev,
4956 "Busted HC? Not enough HCD resources for "
4957 "old configuration.\n");
d673bfcb 4958 mutex_unlock(hcd->bandwidth_mutex);
3f0479e0
SS
4959 goto re_enumerate;
4960 }
1da177e4
LT
4961 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4962 USB_REQ_SET_CONFIGURATION, 0,
4963 udev->actconfig->desc.bConfigurationValue, 0,
4964 NULL, 0, USB_CTRL_SET_TIMEOUT);
4965 if (ret < 0) {
4966 dev_err(&udev->dev,
4967 "can't restore configuration #%d (error=%d)\n",
4968 udev->actconfig->desc.bConfigurationValue, ret);
d673bfcb 4969 mutex_unlock(hcd->bandwidth_mutex);
1da177e4
LT
4970 goto re_enumerate;
4971 }
d673bfcb 4972 mutex_unlock(hcd->bandwidth_mutex);
1da177e4
LT
4973 usb_set_device_state(udev, USB_STATE_CONFIGURED);
4974
4fe0387a
AS
4975 /* Put interfaces back into the same altsettings as before.
4976 * Don't bother to send the Set-Interface request for interfaces
4977 * that were already in altsetting 0; besides being unnecessary,
4978 * many devices can't handle it. Instead just reset the host-side
4979 * endpoint state.
4980 */
1da177e4 4981 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
3f0479e0
SS
4982 struct usb_host_config *config = udev->actconfig;
4983 struct usb_interface *intf = config->interface[i];
1da177e4
LT
4984 struct usb_interface_descriptor *desc;
4985
1da177e4 4986 desc = &intf->cur_altsetting->desc;
4fe0387a
AS
4987 if (desc->bAlternateSetting == 0) {
4988 usb_disable_interface(udev, intf, true);
4989 usb_enable_interface(udev, intf, true);
4990 ret = 0;
4991 } else {
04a723ea
SS
4992 /* Let the bandwidth allocation function know that this
4993 * device has been reset, and it will have to use
4994 * alternate setting 0 as the current alternate setting.
3f0479e0 4995 */
04a723ea 4996 intf->resetting_device = 1;
4fe0387a
AS
4997 ret = usb_set_interface(udev, desc->bInterfaceNumber,
4998 desc->bAlternateSetting);
04a723ea 4999 intf->resetting_device = 0;
4fe0387a 5000 }
1da177e4
LT
5001 if (ret < 0) {
5002 dev_err(&udev->dev, "failed to restore interface %d "
5003 "altsetting %d (error=%d)\n",
5004 desc->bInterfaceNumber,
5005 desc->bAlternateSetting,
5006 ret);
5007 goto re_enumerate;
5008 }
5009 }
5010
5011done:
f74631e3 5012 /* Now that the alt settings are re-installed, enable LTM and LPM. */
8306095f 5013 usb_unlocked_enable_lpm(udev);
f74631e3 5014 usb_enable_ltm(udev);
1da177e4
LT
5015 return 0;
5016
5017re_enumerate:
6d1d0513 5018 /* LPM state doesn't matter when we're about to destroy the device. */
1da177e4
LT
5019 hub_port_logical_disconnect(parent_hub, port1);
5020 return -ENODEV;
5021}
79efa097
AS
5022
5023/**
742120c6 5024 * usb_reset_device - warn interface drivers and perform a USB port reset
79efa097 5025 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
79efa097
AS
5026 *
5027 * Warns all drivers bound to registered interfaces (using their pre_reset
5028 * method), performs the port reset, and then lets the drivers know that
5029 * the reset is over (using their post_reset method).
5030 *
742120c6 5031 * Return value is the same as for usb_reset_and_verify_device().
79efa097
AS
5032 *
5033 * The caller must own the device lock. For example, it's safe to use
5034 * this from a driver probe() routine after downloading new firmware.
5035 * For calls that might not occur during probe(), drivers should lock
5036 * the device using usb_lock_device_for_reset().
78d9a487
AS
5037 *
5038 * If an interface is currently being probed or disconnected, we assume
5039 * its driver knows how to handle resets. For all other interfaces,
5040 * if the driver doesn't have pre_reset and post_reset methods then
5041 * we attempt to unbind it and rebind afterward.
79efa097 5042 */
742120c6 5043int usb_reset_device(struct usb_device *udev)
79efa097
AS
5044{
5045 int ret;
852c4b43 5046 int i;
79efa097
AS
5047 struct usb_host_config *config = udev->actconfig;
5048
5049 if (udev->state == USB_STATE_NOTATTACHED ||
5050 udev->state == USB_STATE_SUSPENDED) {
5051 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5052 udev->state);
5053 return -EINVAL;
5054 }
5055
645daaab 5056 /* Prevent autosuspend during the reset */
94fcda1f 5057 usb_autoresume_device(udev);
645daaab 5058
79efa097 5059 if (config) {
79efa097 5060 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
852c4b43
AS
5061 struct usb_interface *cintf = config->interface[i];
5062 struct usb_driver *drv;
78d9a487 5063 int unbind = 0;
852c4b43
AS
5064
5065 if (cintf->dev.driver) {
79efa097 5066 drv = to_usb_driver(cintf->dev.driver);
78d9a487
AS
5067 if (drv->pre_reset && drv->post_reset)
5068 unbind = (drv->pre_reset)(cintf);
5069 else if (cintf->condition ==
5070 USB_INTERFACE_BOUND)
5071 unbind = 1;
5072 if (unbind)
5073 usb_forced_unbind_intf(cintf);
79efa097
AS
5074 }
5075 }
5076 }
5077
742120c6 5078 ret = usb_reset_and_verify_device(udev);
79efa097
AS
5079
5080 if (config) {
79efa097 5081 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
852c4b43
AS
5082 struct usb_interface *cintf = config->interface[i];
5083 struct usb_driver *drv;
78d9a487 5084 int rebind = cintf->needs_binding;
852c4b43 5085
78d9a487 5086 if (!rebind && cintf->dev.driver) {
79efa097
AS
5087 drv = to_usb_driver(cintf->dev.driver);
5088 if (drv->post_reset)
78d9a487
AS
5089 rebind = (drv->post_reset)(cintf);
5090 else if (cintf->condition ==
5091 USB_INTERFACE_BOUND)
5092 rebind = 1;
79efa097 5093 }
6c640945 5094 if (ret == 0 && rebind)
78d9a487 5095 usb_rebind_intf(cintf);
79efa097
AS
5096 }
5097 }
5098
94fcda1f 5099 usb_autosuspend_device(udev);
79efa097
AS
5100 return ret;
5101}
742120c6 5102EXPORT_SYMBOL_GPL(usb_reset_device);
dc023dce
IPG
5103
5104
5105/**
5106 * usb_queue_reset_device - Reset a USB device from an atomic context
5107 * @iface: USB interface belonging to the device to reset
5108 *
5109 * This function can be used to reset a USB device from an atomic
5110 * context, where usb_reset_device() won't work (as it blocks).
5111 *
5112 * Doing a reset via this method is functionally equivalent to calling
5113 * usb_reset_device(), except for the fact that it is delayed to a
5114 * workqueue. This means that any drivers bound to other interfaces
5115 * might be unbound, as well as users from usbfs in user space.
5116 *
5117 * Corner cases:
5118 *
5119 * - Scheduling two resets at the same time from two different drivers
5120 * attached to two different interfaces of the same device is
5121 * possible; depending on how the driver attached to each interface
5122 * handles ->pre_reset(), the second reset might happen or not.
5123 *
5124 * - If a driver is unbound and it had a pending reset, the reset will
5125 * be cancelled.
5126 *
5127 * - This function can be called during .probe() or .disconnect()
5128 * times. On return from .disconnect(), any pending resets will be
5129 * cancelled.
5130 *
5131 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5132 * does its own.
5133 *
5134 * NOTE: We don't do any reference count tracking because it is not
5135 * needed. The lifecycle of the work_struct is tied to the
5136 * usb_interface. Before destroying the interface we cancel the
5137 * work_struct, so the fact that work_struct is queued and or
5138 * running means the interface (and thus, the device) exist and
5139 * are referenced.
5140 */
5141void usb_queue_reset_device(struct usb_interface *iface)
5142{
5143 schedule_work(&iface->reset_ws);
5144}
5145EXPORT_SYMBOL_GPL(usb_queue_reset_device);
ff823c79
LT
5146
5147/**
5148 * usb_hub_find_child - Get the pointer of child device
5149 * attached to the port which is specified by @port1.
5150 * @hdev: USB device belonging to the usb hub
5151 * @port1: port num to indicate which port the child device
5152 * is attached to.
5153 *
5154 * USB drivers call this function to get hub's child device
5155 * pointer.
5156 *
5157 * Return NULL if input param is invalid and
5158 * child's usb_device pointer if non-NULL.
5159 */
5160struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5161 int port1)
5162{
5163 struct usb_hub *hub = hdev_to_hub(hdev);
5164
5165 if (port1 < 1 || port1 > hdev->maxchild)
5166 return NULL;
5167 return hub->ports[port1 - 1]->child;
5168}
5169EXPORT_SYMBOL_GPL(usb_hub_find_child);
d5575424 5170
05f91689
LT
5171/**
5172 * usb_set_hub_port_connect_type - set hub port connect type.
5173 * @hdev: USB device belonging to the usb hub
5174 * @port1: port num of the port
5175 * @type: connect type of the port
5176 */
5177void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1,
5178 enum usb_port_connect_type type)
5179{
5180 struct usb_hub *hub = hdev_to_hub(hdev);
5181
5182 hub->ports[port1 - 1]->connect_type = type;
5183}
5184
5185/**
5186 * usb_get_hub_port_connect_type - Get the port's connect type
5187 * @hdev: USB device belonging to the usb hub
5188 * @port1: port num of the port
5189 *
5190 * Return connect type of the port and if input params are
5191 * invalid, return USB_PORT_CONNECT_TYPE_UNKNOWN.
5192 */
5193enum usb_port_connect_type
5194usb_get_hub_port_connect_type(struct usb_device *hdev, int port1)
5195{
5196 struct usb_hub *hub = hdev_to_hub(hdev);
5197
5198 return hub->ports[port1 - 1]->connect_type;
5199}
5200
d5575424
LT
5201#ifdef CONFIG_ACPI
5202/**
5203 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5204 * @hdev: USB device belonging to the usb hub
5205 * @port1: port num of the port
5206 *
5207 * Return port's acpi handle if successful, NULL if params are
5208 * invaild.
5209 */
5210acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5211 int port1)
5212{
5213 struct usb_hub *hub = hdev_to_hub(hdev);
5214
5215 return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
5216}
5217#endif