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