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