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