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