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