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