blob: 9f54e8330f7839d2010801fcaa56ca742bb47613 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/config.h>
12#ifdef CONFIG_USB_DEBUG
13 #define DEBUG
14#else
15 #undef DEBUG
16#endif
17#include <linux/kernel.h>
18#include <linux/errno.h>
19#include <linux/module.h>
20#include <linux/moduleparam.h>
21#include <linux/completion.h>
22#include <linux/sched.h>
23#include <linux/list.h>
24#include <linux/slab.h>
25#include <linux/smp_lock.h>
26#include <linux/ioctl.h>
27#include <linux/usb.h>
28#include <linux/usbdevice_fs.h>
akpm@osdl.org9c8d6172005-06-20 14:29:58 -070029#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <asm/semaphore.h>
32#include <asm/uaccess.h>
33#include <asm/byteorder.h>
34
35#include "usb.h"
36#include "hcd.h"
37#include "hub.h"
38
39/* Protect struct usb_device->state and ->children members
40 * Note: Both are also protected by ->serialize, except that ->state can
41 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
42static DEFINE_SPINLOCK(device_state_lock);
43
44/* khubd's worklist and its lock */
45static DEFINE_SPINLOCK(hub_event_lock);
46static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
47
48/* Wakes up khubd */
49static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
50
akpm@osdl.org9c8d6172005-06-20 14:29:58 -070051static struct task_struct *khubd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/* cycle leds on hubs that aren't blinking for attention */
54static int blinkenlights = 0;
55module_param (blinkenlights, bool, S_IRUGO);
56MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
57
58/*
59 * As of 2.6.10 we introduce a new USB device initialization scheme which
60 * closely resembles the way Windows works. Hopefully it will be compatible
61 * with a wider range of devices than the old scheme. However some previously
62 * working devices may start giving rise to "device not accepting address"
63 * errors; if that happens the user can try the old scheme by adjusting the
64 * following module parameters.
65 *
66 * For maximum flexibility there are two boolean parameters to control the
67 * hub driver's behavior. On the first initialization attempt, if the
68 * "old_scheme_first" parameter is set then the old scheme will be used,
69 * otherwise the new scheme is used. If that fails and "use_both_schemes"
70 * is set, then the driver will make another attempt, using the other scheme.
71 */
72static int old_scheme_first = 0;
73module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
74MODULE_PARM_DESC(old_scheme_first,
75 "start with the old device initialization scheme");
76
77static int use_both_schemes = 1;
78module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
79MODULE_PARM_DESC(use_both_schemes,
80 "try the other device initialization scheme if the "
81 "first one fails");
82
83
84#ifdef DEBUG
85static inline char *portspeed (int portstatus)
86{
87 if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
88 return "480 Mb/s";
89 else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
90 return "1.5 Mb/s";
91 else
92 return "12 Mb/s";
93}
94#endif
95
96/* Note that hdev or one of its children must be locked! */
97static inline struct usb_hub *hdev_to_hub(struct usb_device *hdev)
98{
99 return usb_get_intfdata(hdev->actconfig->interface[0]);
100}
101
102/* USB 2.0 spec Section 11.24.4.5 */
103static int get_hub_descriptor(struct usb_device *hdev, void *data, int size)
104{
105 int i, ret;
106
107 for (i = 0; i < 3; i++) {
108 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
109 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
110 USB_DT_HUB << 8, 0, data, size,
111 USB_CTRL_GET_TIMEOUT);
112 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
113 return ret;
114 }
115 return -EINVAL;
116}
117
118/*
119 * USB 2.0 spec Section 11.24.2.1
120 */
121static int clear_hub_feature(struct usb_device *hdev, int feature)
122{
123 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
124 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
125}
126
127/*
128 * USB 2.0 spec Section 11.24.2.2
129 */
130static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
131{
132 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
133 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
134 NULL, 0, 1000);
135}
136
137/*
138 * USB 2.0 spec Section 11.24.2.13
139 */
140static int set_port_feature(struct usb_device *hdev, int port1, int feature)
141{
142 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
143 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
144 NULL, 0, 1000);
145}
146
147/*
148 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
149 * for info about using port indicators
150 */
151static void set_port_led(
152 struct usb_hub *hub,
153 int port1,
154 int selector
155)
156{
157 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
158 USB_PORT_FEAT_INDICATOR);
159 if (status < 0)
160 dev_dbg (hub->intfdev,
161 "port %d indicator %s status %d\n",
162 port1,
163 ({ char *s; switch (selector) {
164 case HUB_LED_AMBER: s = "amber"; break;
165 case HUB_LED_GREEN: s = "green"; break;
166 case HUB_LED_OFF: s = "off"; break;
167 case HUB_LED_AUTO: s = "auto"; break;
168 default: s = "??"; break;
169 }; s; }),
170 status);
171}
172
173#define LED_CYCLE_PERIOD ((2*HZ)/3)
174
175static void led_work (void *__hub)
176{
177 struct usb_hub *hub = __hub;
178 struct usb_device *hdev = hub->hdev;
179 unsigned i;
180 unsigned changed = 0;
181 int cursor = -1;
182
183 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
184 return;
185
186 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
187 unsigned selector, mode;
188
189 /* 30%-50% duty cycle */
190
191 switch (hub->indicator[i]) {
192 /* cycle marker */
193 case INDICATOR_CYCLE:
194 cursor = i;
195 selector = HUB_LED_AUTO;
196 mode = INDICATOR_AUTO;
197 break;
198 /* blinking green = sw attention */
199 case INDICATOR_GREEN_BLINK:
200 selector = HUB_LED_GREEN;
201 mode = INDICATOR_GREEN_BLINK_OFF;
202 break;
203 case INDICATOR_GREEN_BLINK_OFF:
204 selector = HUB_LED_OFF;
205 mode = INDICATOR_GREEN_BLINK;
206 break;
207 /* blinking amber = hw attention */
208 case INDICATOR_AMBER_BLINK:
209 selector = HUB_LED_AMBER;
210 mode = INDICATOR_AMBER_BLINK_OFF;
211 break;
212 case INDICATOR_AMBER_BLINK_OFF:
213 selector = HUB_LED_OFF;
214 mode = INDICATOR_AMBER_BLINK;
215 break;
216 /* blink green/amber = reserved */
217 case INDICATOR_ALT_BLINK:
218 selector = HUB_LED_GREEN;
219 mode = INDICATOR_ALT_BLINK_OFF;
220 break;
221 case INDICATOR_ALT_BLINK_OFF:
222 selector = HUB_LED_AMBER;
223 mode = INDICATOR_ALT_BLINK;
224 break;
225 default:
226 continue;
227 }
228 if (selector != HUB_LED_AUTO)
229 changed = 1;
230 set_port_led(hub, i + 1, selector);
231 hub->indicator[i] = mode;
232 }
233 if (!changed && blinkenlights) {
234 cursor++;
235 cursor %= hub->descriptor->bNbrPorts;
236 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
237 hub->indicator[cursor] = INDICATOR_CYCLE;
238 changed++;
239 }
240 if (changed)
241 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
242}
243
244/* use a short timeout for hub/port status fetches */
245#define USB_STS_TIMEOUT 1000
246#define USB_STS_RETRIES 5
247
248/*
249 * USB 2.0 spec Section 11.24.2.6
250 */
251static int get_hub_status(struct usb_device *hdev,
252 struct usb_hub_status *data)
253{
254 int i, status = -ETIMEDOUT;
255
256 for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
257 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
258 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
259 data, sizeof(*data), USB_STS_TIMEOUT);
260 }
261 return status;
262}
263
264/*
265 * USB 2.0 spec Section 11.24.2.7
266 */
267static int get_port_status(struct usb_device *hdev, int port1,
268 struct usb_port_status *data)
269{
270 int i, status = -ETIMEDOUT;
271
272 for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
273 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
274 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
275 data, sizeof(*data), USB_STS_TIMEOUT);
276 }
277 return status;
278}
279
280static void kick_khubd(struct usb_hub *hub)
281{
282 unsigned long flags;
283
284 spin_lock_irqsave(&hub_event_lock, flags);
285 if (list_empty(&hub->event_list)) {
286 list_add_tail(&hub->event_list, &hub_event_list);
287 wake_up(&khubd_wait);
288 }
289 spin_unlock_irqrestore(&hub_event_lock, flags);
290}
291
292void usb_kick_khubd(struct usb_device *hdev)
293{
294 kick_khubd(hdev_to_hub(hdev));
295}
296
297
298/* completion function, fires on port status changes and various faults */
299static void hub_irq(struct urb *urb, struct pt_regs *regs)
300{
301 struct usb_hub *hub = (struct usb_hub *)urb->context;
302 int status;
303 int i;
304 unsigned long bits;
305
306 switch (urb->status) {
307 case -ENOENT: /* synchronous unlink */
308 case -ECONNRESET: /* async unlink */
309 case -ESHUTDOWN: /* hardware going away */
310 return;
311
312 default: /* presumably an error */
313 /* Cause a hub reset after 10 consecutive errors */
314 dev_dbg (hub->intfdev, "transfer --> %d\n", urb->status);
315 if ((++hub->nerrors < 10) || hub->error)
316 goto resubmit;
317 hub->error = urb->status;
318 /* FALL THROUGH */
319
320 /* let khubd handle things */
321 case 0: /* we got data: port status changed */
322 bits = 0;
323 for (i = 0; i < urb->actual_length; ++i)
324 bits |= ((unsigned long) ((*hub->buffer)[i]))
325 << (i*8);
326 hub->event_bits[0] = bits;
327 break;
328 }
329
330 hub->nerrors = 0;
331
332 /* Something happened, let khubd figure it out */
333 kick_khubd(hub);
334
335resubmit:
336 if (hub->quiescing)
337 return;
338
339 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
340 && status != -ENODEV && status != -EPERM)
341 dev_err (hub->intfdev, "resubmit --> %d\n", status);
342}
343
344/* USB 2.0 spec Section 11.24.2.3 */
345static inline int
346hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
347{
348 return usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
349 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
350 tt, NULL, 0, 1000);
351}
352
353/*
354 * enumeration blocks khubd for a long time. we use keventd instead, since
355 * long blocking there is the exception, not the rule. accordingly, HCDs
356 * talking to TTs must queue control transfers (not just bulk and iso), so
357 * both can talk to the same hub concurrently.
358 */
359static void hub_tt_kevent (void *arg)
360{
361 struct usb_hub *hub = arg;
362 unsigned long flags;
363
364 spin_lock_irqsave (&hub->tt.lock, flags);
365 while (!list_empty (&hub->tt.clear_list)) {
366 struct list_head *temp;
367 struct usb_tt_clear *clear;
368 struct usb_device *hdev = hub->hdev;
369 int status;
370
371 temp = hub->tt.clear_list.next;
372 clear = list_entry (temp, struct usb_tt_clear, clear_list);
373 list_del (&clear->clear_list);
374
375 /* drop lock so HCD can concurrently report other TT errors */
376 spin_unlock_irqrestore (&hub->tt.lock, flags);
377 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
378 spin_lock_irqsave (&hub->tt.lock, flags);
379
380 if (status)
381 dev_err (&hdev->dev,
382 "clear tt %d (%04x) error %d\n",
383 clear->tt, clear->devinfo, status);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700384 kfree(clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386 spin_unlock_irqrestore (&hub->tt.lock, flags);
387}
388
389/**
390 * usb_hub_tt_clear_buffer - clear control/bulk TT state in high speed hub
391 * @udev: the device whose split transaction failed
392 * @pipe: identifies the endpoint of the failed transaction
393 *
394 * High speed HCDs use this to tell the hub driver that some split control or
395 * bulk transaction failed in a way that requires clearing internal state of
396 * a transaction translator. This is normally detected (and reported) from
397 * interrupt context.
398 *
399 * It may not be possible for that hub to handle additional full (or low)
400 * speed transactions until that state is fully cleared out.
401 */
402void usb_hub_tt_clear_buffer (struct usb_device *udev, int pipe)
403{
404 struct usb_tt *tt = udev->tt;
405 unsigned long flags;
406 struct usb_tt_clear *clear;
407
408 /* we've got to cope with an arbitrary number of pending TT clears,
409 * since each TT has "at least two" buffers that can need it (and
410 * there can be many TTs per hub). even if they're uncommon.
411 */
412 if ((clear = kmalloc (sizeof *clear, SLAB_ATOMIC)) == NULL) {
413 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
414 /* FIXME recover somehow ... RESET_TT? */
415 return;
416 }
417
418 /* info that CLEAR_TT_BUFFER needs */
419 clear->tt = tt->multi ? udev->ttport : 1;
420 clear->devinfo = usb_pipeendpoint (pipe);
421 clear->devinfo |= udev->devnum << 4;
422 clear->devinfo |= usb_pipecontrol (pipe)
423 ? (USB_ENDPOINT_XFER_CONTROL << 11)
424 : (USB_ENDPOINT_XFER_BULK << 11);
425 if (usb_pipein (pipe))
426 clear->devinfo |= 1 << 15;
427
428 /* tell keventd to clear state for this TT */
429 spin_lock_irqsave (&tt->lock, flags);
430 list_add_tail (&clear->clear_list, &tt->clear_list);
431 schedule_work (&tt->kevent);
432 spin_unlock_irqrestore (&tt->lock, flags);
433}
434
435static void hub_power_on(struct usb_hub *hub)
436{
437 int port1;
438
439 /* if hub supports power switching, enable power on each port */
440 if ((hub->descriptor->wHubCharacteristics & HUB_CHAR_LPSM) < 2) {
441 dev_dbg(hub->intfdev, "enabling power on all ports\n");
442 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
443 set_port_feature(hub->hdev, port1,
444 USB_PORT_FEAT_POWER);
445 }
446
447 /* Wait for power to be enabled */
448 msleep(hub->descriptor->bPwrOn2PwrGood * 2);
449}
450
451static void hub_quiesce(struct usb_hub *hub)
452{
453 /* stop khubd and related activity */
454 hub->quiescing = 1;
455 usb_kill_urb(hub->urb);
456 if (hub->has_indicators)
457 cancel_delayed_work(&hub->leds);
458 if (hub->has_indicators || hub->tt.hub)
459 flush_scheduled_work();
460}
461
462static void hub_activate(struct usb_hub *hub)
463{
464 int status;
465
466 hub->quiescing = 0;
467 hub->activating = 1;
468 status = usb_submit_urb(hub->urb, GFP_NOIO);
469 if (status < 0)
470 dev_err(hub->intfdev, "activate --> %d\n", status);
471 if (hub->has_indicators && blinkenlights)
472 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
473
474 /* scan all ports ASAP */
475 kick_khubd(hub);
476}
477
478static int hub_hub_status(struct usb_hub *hub,
479 u16 *status, u16 *change)
480{
481 int ret;
482
483 ret = get_hub_status(hub->hdev, &hub->status->hub);
484 if (ret < 0)
485 dev_err (hub->intfdev,
486 "%s failed (err = %d)\n", __FUNCTION__, ret);
487 else {
488 *status = le16_to_cpu(hub->status->hub.wHubStatus);
489 *change = le16_to_cpu(hub->status->hub.wHubChange);
490 ret = 0;
491 }
492 return ret;
493}
494
Alan Stern8b28c752005-08-10 17:04:13 -0400495static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
496{
497 struct usb_device *hdev = hub->hdev;
498 int ret;
499
500 if (hdev->children[port1-1] && set_state) {
501 usb_set_device_state(hdev->children[port1-1],
502 USB_STATE_NOTATTACHED);
503 }
504 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
505 if (ret)
506 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
507 port1, ret);
508
509 return ret;
510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512static int hub_configure(struct usb_hub *hub,
513 struct usb_endpoint_descriptor *endpoint)
514{
515 struct usb_device *hdev = hub->hdev;
516 struct device *hub_dev = hub->intfdev;
517 u16 hubstatus, hubchange;
518 unsigned int pipe;
519 int maxp, ret;
520 char *message;
521
522 hub->buffer = usb_buffer_alloc(hdev, sizeof(*hub->buffer), GFP_KERNEL,
523 &hub->buffer_dma);
524 if (!hub->buffer) {
525 message = "can't allocate hub irq buffer";
526 ret = -ENOMEM;
527 goto fail;
528 }
529
530 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
531 if (!hub->status) {
532 message = "can't kmalloc hub status buffer";
533 ret = -ENOMEM;
534 goto fail;
535 }
536
537 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
538 if (!hub->descriptor) {
539 message = "can't kmalloc hub descriptor";
540 ret = -ENOMEM;
541 goto fail;
542 }
543
544 /* Request the entire hub descriptor.
545 * hub->descriptor can handle USB_MAXCHILDREN ports,
546 * but the hub can/will return fewer bytes here.
547 */
548 ret = get_hub_descriptor(hdev, hub->descriptor,
549 sizeof(*hub->descriptor));
550 if (ret < 0) {
551 message = "can't read hub descriptor";
552 goto fail;
553 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
554 message = "hub has too many ports!";
555 ret = -ENODEV;
556 goto fail;
557 }
558
559 hdev->maxchild = hub->descriptor->bNbrPorts;
560 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
561 (hdev->maxchild == 1) ? "" : "s");
562
563 le16_to_cpus(&hub->descriptor->wHubCharacteristics);
564
565 if (hub->descriptor->wHubCharacteristics & HUB_CHAR_COMPOUND) {
566 int i;
567 char portstr [USB_MAXCHILDREN + 1];
568
569 for (i = 0; i < hdev->maxchild; i++)
570 portstr[i] = hub->descriptor->DeviceRemovable
571 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
572 ? 'F' : 'R';
573 portstr[hdev->maxchild] = 0;
574 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
575 } else
576 dev_dbg(hub_dev, "standalone hub\n");
577
578 switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_LPSM) {
579 case 0x00:
580 dev_dbg(hub_dev, "ganged power switching\n");
581 break;
582 case 0x01:
583 dev_dbg(hub_dev, "individual port power switching\n");
584 break;
585 case 0x02:
586 case 0x03:
587 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
588 break;
589 }
590
591 switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_OCPM) {
592 case 0x00:
593 dev_dbg(hub_dev, "global over-current protection\n");
594 break;
595 case 0x08:
596 dev_dbg(hub_dev, "individual port over-current protection\n");
597 break;
598 case 0x10:
599 case 0x18:
600 dev_dbg(hub_dev, "no over-current protection\n");
601 break;
602 }
603
604 spin_lock_init (&hub->tt.lock);
605 INIT_LIST_HEAD (&hub->tt.clear_list);
606 INIT_WORK (&hub->tt.kevent, hub_tt_kevent, hub);
607 switch (hdev->descriptor.bDeviceProtocol) {
608 case 0:
609 break;
610 case 1:
611 dev_dbg(hub_dev, "Single TT\n");
612 hub->tt.hub = hdev;
613 break;
614 case 2:
615 ret = usb_set_interface(hdev, 0, 1);
616 if (ret == 0) {
617 dev_dbg(hub_dev, "TT per port\n");
618 hub->tt.multi = 1;
619 } else
620 dev_err(hub_dev, "Using single TT (err %d)\n",
621 ret);
622 hub->tt.hub = hdev;
623 break;
624 default:
625 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
626 hdev->descriptor.bDeviceProtocol);
627 break;
628 }
629
630 switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_TTTT) {
631 case 0x00:
632 if (hdev->descriptor.bDeviceProtocol != 0)
633 dev_dbg(hub_dev, "TT requires at most 8 FS bit times\n");
634 break;
635 case 0x20:
636 dev_dbg(hub_dev, "TT requires at most 16 FS bit times\n");
637 break;
638 case 0x40:
639 dev_dbg(hub_dev, "TT requires at most 24 FS bit times\n");
640 break;
641 case 0x60:
642 dev_dbg(hub_dev, "TT requires at most 32 FS bit times\n");
643 break;
644 }
645
646 /* probe() zeroes hub->indicator[] */
647 if (hub->descriptor->wHubCharacteristics & HUB_CHAR_PORTIND) {
648 hub->has_indicators = 1;
649 dev_dbg(hub_dev, "Port indicators are supported\n");
650 }
651
652 dev_dbg(hub_dev, "power on to power good time: %dms\n",
653 hub->descriptor->bPwrOn2PwrGood * 2);
654
655 /* power budgeting mostly matters with bus-powered hubs,
656 * and battery-powered root hubs (may provide just 8 mA).
657 */
658 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
659 if (ret < 0) {
660 message = "can't get hub status";
661 goto fail;
662 }
Alan Stern7d35b922005-04-25 11:18:32 -0400663 le16_to_cpus(&hubstatus);
664 if (hdev == hdev->bus->root_hub) {
665 struct usb_hcd *hcd =
666 container_of(hdev->bus, struct usb_hcd, self);
667
668 hub->power_budget = min(500u, hcd->power_budget) / 2;
669 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
671 hub->descriptor->bHubContrCurrent);
672 hub->power_budget = (501 - hub->descriptor->bHubContrCurrent)
673 / 2;
Alan Stern7d35b922005-04-25 11:18:32 -0400674 }
675 if (hub->power_budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 dev_dbg(hub_dev, "%dmA bus power budget for children\n",
677 hub->power_budget * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679
680 ret = hub_hub_status(hub, &hubstatus, &hubchange);
681 if (ret < 0) {
682 message = "can't get hub status";
683 goto fail;
684 }
685
686 /* local power status reports aren't always correct */
687 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
688 dev_dbg(hub_dev, "local power source is %s\n",
689 (hubstatus & HUB_STATUS_LOCAL_POWER)
690 ? "lost (inactive)" : "good");
691
692 if ((hub->descriptor->wHubCharacteristics & HUB_CHAR_OCPM) == 0)
693 dev_dbg(hub_dev, "%sover-current condition exists\n",
694 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
695
696 /* set up the interrupt endpoint */
697 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
698 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
699
700 if (maxp > sizeof(*hub->buffer))
701 maxp = sizeof(*hub->buffer);
702
703 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
704 if (!hub->urb) {
705 message = "couldn't allocate interrupt urb";
706 ret = -ENOMEM;
707 goto fail;
708 }
709
710 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
711 hub, endpoint->bInterval);
712 hub->urb->transfer_dma = hub->buffer_dma;
713 hub->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
714
715 /* maybe cycle the hub leds */
716 if (hub->has_indicators && blinkenlights)
717 hub->indicator [0] = INDICATOR_CYCLE;
718
719 hub_power_on(hub);
720 hub_activate(hub);
721 return 0;
722
723fail:
724 dev_err (hub_dev, "config failed, %s (err %d)\n",
725 message, ret);
726 /* hub_disconnect() frees urb and descriptor */
727 return ret;
728}
729
730static unsigned highspeed_hubs;
731
Alan Sternbf193d32005-08-10 17:12:31 -0400732/* Called after the hub driver is unbound from a hub with children */
733static void hub_remove_children_work(void *__hub)
734{
735 struct usb_hub *hub = __hub;
736 struct usb_device *hdev = hub->hdev;
737 int i;
738
739 kfree(hub);
740
741 usb_lock_device(hdev);
742 for (i = 0; i < hdev->maxchild; ++i) {
743 if (hdev->children[i])
744 usb_disconnect(&hdev->children[i]);
745 }
746 usb_unlock_device(hdev);
747 usb_put_dev(hdev);
748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750static void hub_disconnect(struct usb_interface *intf)
751{
752 struct usb_hub *hub = usb_get_intfdata (intf);
753 struct usb_device *hdev;
Alan Sternbf193d32005-08-10 17:12:31 -0400754 int n, port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Alan Stern8b28c752005-08-10 17:04:13 -0400756 usb_set_intfdata (intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 hdev = hub->hdev;
758
759 if (hdev->speed == USB_SPEED_HIGH)
760 highspeed_hubs--;
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 hub_quiesce(hub);
763 usb_free_urb(hub->urb);
764 hub->urb = NULL;
765
766 spin_lock_irq(&hub_event_lock);
767 list_del_init(&hub->event_list);
768 spin_unlock_irq(&hub_event_lock);
769
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700770 kfree(hub->descriptor);
771 hub->descriptor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700773 kfree(hub->status);
774 hub->status = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 if (hub->buffer) {
777 usb_buffer_free(hdev, sizeof(*hub->buffer), hub->buffer,
778 hub->buffer_dma);
779 hub->buffer = NULL;
780 }
781
Alan Sternbf193d32005-08-10 17:12:31 -0400782 /* If there are any children then this is an unbind only, not a
783 * physical disconnection. The active ports must be disabled
784 * and later on we must call usb_disconnect(). We can't call
785 * it now because we may not hold the hub's device lock.
786 */
787 n = 0;
788 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
789 if (hdev->children[port1 - 1]) {
790 ++n;
791 hub_port_disable(hub, port1, 1);
792 }
793 }
794
795 if (n == 0)
796 kfree(hub);
797 else {
798 /* Reuse the hub->leds work_struct for our own purposes */
799 INIT_WORK(&hub->leds, hub_remove_children_work, hub);
800 schedule_work(&hub->leds);
801 usb_get_dev(hdev);
802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
805static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
806{
807 struct usb_host_interface *desc;
808 struct usb_endpoint_descriptor *endpoint;
809 struct usb_device *hdev;
810 struct usb_hub *hub;
811
812 desc = intf->cur_altsetting;
813 hdev = interface_to_usbdev(intf);
814
815 /* Some hubs have a subclass of 1, which AFAICT according to the */
816 /* specs is not defined, but it works */
817 if ((desc->desc.bInterfaceSubClass != 0) &&
818 (desc->desc.bInterfaceSubClass != 1)) {
819descriptor_error:
820 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
821 return -EIO;
822 }
823
824 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
825 if (desc->desc.bNumEndpoints != 1)
826 goto descriptor_error;
827
828 endpoint = &desc->endpoint[0].desc;
829
830 /* Output endpoint? Curiouser and curiouser.. */
831 if (!(endpoint->bEndpointAddress & USB_DIR_IN))
832 goto descriptor_error;
833
834 /* If it's not an interrupt endpoint, we'd better punt! */
835 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
836 != USB_ENDPOINT_XFER_INT)
837 goto descriptor_error;
838
839 /* We found a hub */
840 dev_info (&intf->dev, "USB hub found\n");
841
842 hub = kmalloc(sizeof(*hub), GFP_KERNEL);
843 if (!hub) {
844 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
845 return -ENOMEM;
846 }
847
848 memset(hub, 0, sizeof(*hub));
849
850 INIT_LIST_HEAD(&hub->event_list);
851 hub->intfdev = &intf->dev;
852 hub->hdev = hdev;
853 INIT_WORK(&hub->leds, led_work, hub);
854
855 usb_set_intfdata (intf, hub);
856
857 if (hdev->speed == USB_SPEED_HIGH)
858 highspeed_hubs++;
859
860 if (hub_configure(hub, endpoint) >= 0)
861 return 0;
862
863 hub_disconnect (intf);
864 return -ENODEV;
865}
866
867static int
868hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
869{
870 struct usb_device *hdev = interface_to_usbdev (intf);
871
872 /* assert ifno == 0 (part of hub spec) */
873 switch (code) {
874 case USBDEVFS_HUB_PORTINFO: {
875 struct usbdevfs_hub_portinfo *info = user_data;
876 int i;
877
878 spin_lock_irq(&device_state_lock);
879 if (hdev->devnum <= 0)
880 info->nports = 0;
881 else {
882 info->nports = hdev->maxchild;
883 for (i = 0; i < info->nports; i++) {
884 if (hdev->children[i] == NULL)
885 info->port[i] = 0;
886 else
887 info->port[i] =
888 hdev->children[i]->devnum;
889 }
890 }
891 spin_unlock_irq(&device_state_lock);
892
893 return info->nports + 1;
894 }
895
896 default:
897 return -ENOSYS;
898 }
899}
900
901/* caller has locked the hub device */
902static void hub_pre_reset(struct usb_hub *hub)
903{
904 struct usb_device *hdev = hub->hdev;
905 int i;
906
907 for (i = 0; i < hdev->maxchild; ++i) {
908 if (hdev->children[i])
909 usb_disconnect(&hdev->children[i]);
910 }
911 hub_quiesce(hub);
912}
913
914/* caller has locked the hub device */
915static void hub_post_reset(struct usb_hub *hub)
916{
917 hub_activate(hub);
918 hub_power_on(hub);
919}
920
921
922/* grab device/port lock, returning index of that port (zero based).
923 * protects the upstream link used by this device from concurrent
924 * tree operations like suspend, resume, reset, and disconnect, which
925 * apply to everything downstream of a given port.
926 */
927static int locktree(struct usb_device *udev)
928{
929 int t;
930 struct usb_device *hdev;
931
932 if (!udev)
933 return -ENODEV;
934
935 /* root hub is always the first lock in the series */
936 hdev = udev->parent;
937 if (!hdev) {
938 usb_lock_device(udev);
939 return 0;
940 }
941
942 /* on the path from root to us, lock everything from
943 * top down, dropping parent locks when not needed
944 */
945 t = locktree(hdev);
946 if (t < 0)
947 return t;
948 for (t = 0; t < hdev->maxchild; t++) {
949 if (hdev->children[t] == udev) {
950 /* everything is fail-fast once disconnect
951 * processing starts
952 */
953 if (udev->state == USB_STATE_NOTATTACHED)
954 break;
955
956 /* when everyone grabs locks top->bottom,
957 * non-overlapping work may be concurrent
958 */
959 down(&udev->serialize);
960 up(&hdev->serialize);
961 return t + 1;
962 }
963 }
964 usb_unlock_device(hdev);
965 return -ENODEV;
966}
967
968static void recursively_mark_NOTATTACHED(struct usb_device *udev)
969{
970 int i;
971
972 for (i = 0; i < udev->maxchild; ++i) {
973 if (udev->children[i])
974 recursively_mark_NOTATTACHED(udev->children[i]);
975 }
976 udev->state = USB_STATE_NOTATTACHED;
977}
978
979/**
980 * usb_set_device_state - change a device's current state (usbcore, hcds)
981 * @udev: pointer to device whose state should be changed
982 * @new_state: new state value to be stored
983 *
984 * udev->state is _not_ fully protected by the device lock. Although
985 * most transitions are made only while holding the lock, the state can
986 * can change to USB_STATE_NOTATTACHED at almost any time. This
987 * is so that devices can be marked as disconnected as soon as possible,
988 * without having to wait for any semaphores to be released. As a result,
989 * all changes to any device's state must be protected by the
990 * device_state_lock spinlock.
991 *
992 * Once a device has been added to the device tree, all changes to its state
993 * should be made using this routine. The state should _not_ be set directly.
994 *
995 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
996 * Otherwise udev->state is set to new_state, and if new_state is
997 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
998 * to USB_STATE_NOTATTACHED.
999 */
1000void usb_set_device_state(struct usb_device *udev,
1001 enum usb_device_state new_state)
1002{
1003 unsigned long flags;
1004
1005 spin_lock_irqsave(&device_state_lock, flags);
1006 if (udev->state == USB_STATE_NOTATTACHED)
1007 ; /* do nothing */
1008 else if (new_state != USB_STATE_NOTATTACHED)
1009 udev->state = new_state;
1010 else
1011 recursively_mark_NOTATTACHED(udev);
1012 spin_unlock_irqrestore(&device_state_lock, flags);
1013}
1014EXPORT_SYMBOL(usb_set_device_state);
1015
1016
1017static void choose_address(struct usb_device *udev)
1018{
1019 int devnum;
1020 struct usb_bus *bus = udev->bus;
1021
1022 /* If khubd ever becomes multithreaded, this will need a lock */
1023
1024 /* Try to allocate the next devnum beginning at bus->devnum_next. */
1025 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1026 bus->devnum_next);
1027 if (devnum >= 128)
1028 devnum = find_next_zero_bit(bus->devmap.devicemap, 128, 1);
1029
1030 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1031
1032 if (devnum < 128) {
1033 set_bit(devnum, bus->devmap.devicemap);
1034 udev->devnum = devnum;
1035 }
1036}
1037
1038static void release_address(struct usb_device *udev)
1039{
1040 if (udev->devnum > 0) {
1041 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1042 udev->devnum = -1;
1043 }
1044}
1045
1046/**
1047 * usb_disconnect - disconnect a device (usbcore-internal)
1048 * @pdev: pointer to device being disconnected
1049 * Context: !in_interrupt ()
1050 *
1051 * Something got disconnected. Get rid of it and all of its children.
1052 *
1053 * If *pdev is a normal device then the parent hub must already be locked.
1054 * If *pdev is a root hub then this routine will acquire the
1055 * usb_bus_list_lock on behalf of the caller.
1056 *
1057 * Only hub drivers (including virtual root hub drivers for host
1058 * controllers) should ever call this.
1059 *
1060 * This call is synchronous, and may not be used in an interrupt context.
1061 */
1062void usb_disconnect(struct usb_device **pdev)
1063{
1064 struct usb_device *udev = *pdev;
1065 int i;
1066
1067 if (!udev) {
1068 pr_debug ("%s nodev\n", __FUNCTION__);
1069 return;
1070 }
1071
1072 /* mark the device as inactive, so any further urb submissions for
1073 * this device (and any of its children) will fail immediately.
1074 * this quiesces everyting except pending urbs.
1075 */
1076 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1077
1078 /* lock the bus list on behalf of HCDs unregistering their root hubs */
1079 if (!udev->parent) {
1080 down(&usb_bus_list_lock);
1081 usb_lock_device(udev);
1082 } else
1083 down(&udev->serialize);
1084
1085 dev_info (&udev->dev, "USB disconnect, address %d\n", udev->devnum);
1086
1087 /* Free up all the children before we remove this device */
1088 for (i = 0; i < USB_MAXCHILDREN; i++) {
1089 if (udev->children[i])
1090 usb_disconnect(&udev->children[i]);
1091 }
1092
1093 /* deallocate hcd/hardware state ... nuking all pending urbs and
1094 * cleaning up all state associated with the current configuration
1095 * so that the hardware is now fully quiesced.
1096 */
1097 usb_disable_device(udev, 0);
1098
1099 /* Free the device number, remove the /proc/bus/usb entry and
1100 * the sysfs attributes, and delete the parent's children[]
1101 * (or root_hub) pointer.
1102 */
1103 dev_dbg (&udev->dev, "unregistering device\n");
1104 release_address(udev);
1105 usbfs_remove_device(udev);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001106 usbdev_remove(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 usb_remove_sysfs_dev_files(udev);
1108
1109 /* Avoid races with recursively_mark_NOTATTACHED() */
1110 spin_lock_irq(&device_state_lock);
1111 *pdev = NULL;
1112 spin_unlock_irq(&device_state_lock);
1113
1114 if (!udev->parent) {
1115 usb_unlock_device(udev);
1116 up(&usb_bus_list_lock);
1117 } else
1118 up(&udev->serialize);
1119
1120 device_unregister(&udev->dev);
1121}
1122
1123static int choose_configuration(struct usb_device *udev)
1124{
1125 int c, i;
1126
1127 /* NOTE: this should interact with hub power budgeting */
1128
1129 c = udev->config[0].desc.bConfigurationValue;
1130 if (udev->descriptor.bNumConfigurations != 1) {
1131 for (i = 0; i < udev->descriptor.bNumConfigurations; i++) {
1132 struct usb_interface_descriptor *desc;
1133
1134 /* heuristic: Linux is more likely to have class
1135 * drivers, so avoid vendor-specific interfaces.
1136 */
1137 desc = &udev->config[i].intf_cache[0]
1138 ->altsetting->desc;
1139 if (desc->bInterfaceClass == USB_CLASS_VENDOR_SPEC)
1140 continue;
1141 /* COMM/2/all is CDC ACM, except 0xff is MSFT RNDIS.
1142 * MSFT needs this to be the first config; never use
1143 * it as the default unless Linux has host-side RNDIS.
1144 * A second config would ideally be CDC-Ethernet, but
1145 * may instead be the "vendor specific" CDC subset
1146 * long used by ARM Linux for sa1100 or pxa255.
1147 */
1148 if (desc->bInterfaceClass == USB_CLASS_COMM
1149 && desc->bInterfaceSubClass == 2
1150 && desc->bInterfaceProtocol == 0xff) {
1151 c = udev->config[1].desc.bConfigurationValue;
1152 continue;
1153 }
1154 c = udev->config[i].desc.bConfigurationValue;
1155 break;
1156 }
1157 dev_info(&udev->dev,
1158 "configuration #%d chosen from %d choices\n",
1159 c, udev->descriptor.bNumConfigurations);
1160 }
1161 return c;
1162}
1163
1164#ifdef DEBUG
1165static void show_string(struct usb_device *udev, char *id, char *string)
1166{
1167 if (!string)
1168 return;
1169 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
1170}
1171
1172#else
1173static inline void show_string(struct usb_device *udev, char *id, char *string)
1174{}
1175#endif
1176
1177static void get_string(struct usb_device *udev, char **string, int index)
1178{
1179 char *buf;
1180
1181 if (!index)
1182 return;
1183 buf = kmalloc(256, GFP_KERNEL);
1184 if (!buf)
1185 return;
1186 if (usb_string(udev, index, buf, 256) > 0)
1187 *string = buf;
1188 else
1189 kfree(buf);
1190}
1191
1192
1193#ifdef CONFIG_USB_OTG
1194#include "otg_whitelist.h"
1195#endif
1196
1197/**
1198 * usb_new_device - perform initial device setup (usbcore-internal)
1199 * @udev: newly addressed device (in ADDRESS state)
1200 *
1201 * This is called with devices which have been enumerated, but not yet
1202 * configured. The device descriptor is available, but not descriptors
1203 * for any device configuration. The caller must have locked udev and
1204 * either the parent hub (if udev is a normal device) or else the
1205 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
1206 * udev has already been installed, but udev is not yet visible through
1207 * sysfs or other filesystem code.
1208 *
1209 * Returns 0 for success (device is configured and listed, with its
1210 * interfaces, in sysfs); else a negative errno value.
1211 *
1212 * This call is synchronous, and may not be used in an interrupt context.
1213 *
1214 * Only the hub driver should ever call this; root hub registration
1215 * uses it indirectly.
1216 */
1217int usb_new_device(struct usb_device *udev)
1218{
1219 int err;
1220 int c;
1221
1222 err = usb_get_configuration(udev);
1223 if (err < 0) {
1224 dev_err(&udev->dev, "can't read configurations, error %d\n",
1225 err);
1226 goto fail;
1227 }
1228
1229 /* read the standard strings and cache them if present */
1230 get_string(udev, &udev->product, udev->descriptor.iProduct);
1231 get_string(udev, &udev->manufacturer, udev->descriptor.iManufacturer);
1232 get_string(udev, &udev->serial, udev->descriptor.iSerialNumber);
1233
1234 /* Tell the world! */
1235 dev_dbg(&udev->dev, "new device strings: Mfr=%d, Product=%d, "
1236 "SerialNumber=%d\n",
1237 udev->descriptor.iManufacturer,
1238 udev->descriptor.iProduct,
1239 udev->descriptor.iSerialNumber);
1240 show_string(udev, "Product", udev->product);
1241 show_string(udev, "Manufacturer", udev->manufacturer);
1242 show_string(udev, "SerialNumber", udev->serial);
1243
1244#ifdef CONFIG_USB_OTG
1245 /*
1246 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
1247 * to wake us after we've powered off VBUS; and HNP, switching roles
1248 * "host" to "peripheral". The OTG descriptor helps figure this out.
1249 */
1250 if (!udev->bus->is_b_host
1251 && udev->config
1252 && udev->parent == udev->bus->root_hub) {
1253 struct usb_otg_descriptor *desc = 0;
1254 struct usb_bus *bus = udev->bus;
1255
1256 /* descriptor may appear anywhere in config */
1257 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
1258 le16_to_cpu(udev->config[0].desc.wTotalLength),
1259 USB_DT_OTG, (void **) &desc) == 0) {
1260 if (desc->bmAttributes & USB_OTG_HNP) {
1261 unsigned port1;
1262 struct usb_device *root = udev->parent;
1263
1264 for (port1 = 1; port1 <= root->maxchild;
1265 port1++) {
1266 if (root->children[port1-1] == udev)
1267 break;
1268 }
1269
1270 dev_info(&udev->dev,
1271 "Dual-Role OTG device on %sHNP port\n",
1272 (port1 == bus->otg_port)
1273 ? "" : "non-");
1274
1275 /* enable HNP before suspend, it's simpler */
1276 if (port1 == bus->otg_port)
1277 bus->b_hnp_enable = 1;
1278 err = usb_control_msg(udev,
1279 usb_sndctrlpipe(udev, 0),
1280 USB_REQ_SET_FEATURE, 0,
1281 bus->b_hnp_enable
1282 ? USB_DEVICE_B_HNP_ENABLE
1283 : USB_DEVICE_A_ALT_HNP_SUPPORT,
1284 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
1285 if (err < 0) {
1286 /* OTG MESSAGE: report errors here,
1287 * customize to match your product.
1288 */
1289 dev_info(&udev->dev,
1290 "can't set HNP mode; %d\n",
1291 err);
1292 bus->b_hnp_enable = 0;
1293 }
1294 }
1295 }
1296 }
1297
1298 if (!is_targeted(udev)) {
1299
1300 /* Maybe it can talk to us, though we can't talk to it.
1301 * (Includes HNP test device.)
1302 */
1303 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
1304 static int __usb_suspend_device (struct usb_device *,
1305 int port1, pm_message_t state);
1306 err = __usb_suspend_device(udev,
1307 udev->bus->otg_port,
1308 PMSG_SUSPEND);
1309 if (err < 0)
1310 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
1311 }
1312 err = -ENODEV;
1313 goto fail;
1314 }
1315#endif
1316
1317 /* put device-specific files into sysfs */
1318 err = device_add (&udev->dev);
1319 if (err) {
1320 dev_err(&udev->dev, "can't device_add, error %d\n", err);
1321 goto fail;
1322 }
1323 usb_create_sysfs_dev_files (udev);
1324
1325 /* choose and set the configuration. that registers the interfaces
1326 * with the driver core, and lets usb device drivers bind to them.
1327 */
1328 c = choose_configuration(udev);
1329 if (c < 0)
1330 dev_warn(&udev->dev,
1331 "can't choose an initial configuration\n");
1332 else {
1333 err = usb_set_configuration(udev, c);
1334 if (err) {
1335 dev_err(&udev->dev, "can't set config #%d, error %d\n",
1336 c, err);
1337 usb_remove_sysfs_dev_files(udev);
1338 device_del(&udev->dev);
1339 goto fail;
1340 }
1341 }
1342
1343 /* USB device state == configured ... usable */
1344
1345 /* add a /proc/bus/usb entry */
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001346 usbdev_add(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 usbfs_add_device(udev);
1348 return 0;
1349
1350fail:
1351 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1352 return err;
1353}
1354
1355
1356static int hub_port_status(struct usb_hub *hub, int port1,
1357 u16 *status, u16 *change)
1358{
1359 int ret;
1360
1361 ret = get_port_status(hub->hdev, port1, &hub->status->port);
1362 if (ret < 0)
1363 dev_err (hub->intfdev,
1364 "%s failed (err = %d)\n", __FUNCTION__, ret);
1365 else {
1366 *status = le16_to_cpu(hub->status->port.wPortStatus);
1367 *change = le16_to_cpu(hub->status->port.wPortChange);
1368 ret = 0;
1369 }
1370 return ret;
1371}
1372
1373#define PORT_RESET_TRIES 5
1374#define SET_ADDRESS_TRIES 2
1375#define GET_DESCRIPTOR_TRIES 2
1376#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
1377#define USE_NEW_SCHEME(i) ((i) / 2 == old_scheme_first)
1378
1379#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
1380#define HUB_SHORT_RESET_TIME 10
1381#define HUB_LONG_RESET_TIME 200
1382#define HUB_RESET_TIMEOUT 500
1383
1384static int hub_port_wait_reset(struct usb_hub *hub, int port1,
1385 struct usb_device *udev, unsigned int delay)
1386{
1387 int delay_time, ret;
1388 u16 portstatus;
1389 u16 portchange;
1390
1391 for (delay_time = 0;
1392 delay_time < HUB_RESET_TIMEOUT;
1393 delay_time += delay) {
1394 /* wait to give the device a chance to reset */
1395 msleep(delay);
1396
1397 /* read and decode port status */
1398 ret = hub_port_status(hub, port1, &portstatus, &portchange);
1399 if (ret < 0)
1400 return ret;
1401
1402 /* Device went away? */
1403 if (!(portstatus & USB_PORT_STAT_CONNECTION))
1404 return -ENOTCONN;
1405
1406 /* bomb out completely if something weird happened */
1407 if ((portchange & USB_PORT_STAT_C_CONNECTION))
1408 return -EINVAL;
1409
1410 /* if we`ve finished resetting, then break out of the loop */
1411 if (!(portstatus & USB_PORT_STAT_RESET) &&
1412 (portstatus & USB_PORT_STAT_ENABLE)) {
1413 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
1414 udev->speed = USB_SPEED_HIGH;
1415 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
1416 udev->speed = USB_SPEED_LOW;
1417 else
1418 udev->speed = USB_SPEED_FULL;
1419 return 0;
1420 }
1421
1422 /* switch to the long delay after two short delay failures */
1423 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
1424 delay = HUB_LONG_RESET_TIME;
1425
1426 dev_dbg (hub->intfdev,
1427 "port %d not reset yet, waiting %dms\n",
1428 port1, delay);
1429 }
1430
1431 return -EBUSY;
1432}
1433
1434static int hub_port_reset(struct usb_hub *hub, int port1,
1435 struct usb_device *udev, unsigned int delay)
1436{
1437 int i, status;
1438
1439 /* Reset the port */
1440 for (i = 0; i < PORT_RESET_TRIES; i++) {
1441 status = set_port_feature(hub->hdev,
1442 port1, USB_PORT_FEAT_RESET);
1443 if (status)
1444 dev_err(hub->intfdev,
1445 "cannot reset port %d (err = %d)\n",
1446 port1, status);
1447 else {
1448 status = hub_port_wait_reset(hub, port1, udev, delay);
1449 if (status)
1450 dev_dbg(hub->intfdev,
1451 "port_wait_reset: err = %d\n",
1452 status);
1453 }
1454
1455 /* return on disconnect or reset */
1456 switch (status) {
1457 case 0:
1458 /* TRSTRCY = 10 ms */
1459 msleep(10);
1460 /* FALL THROUGH */
1461 case -ENOTCONN:
1462 case -ENODEV:
1463 clear_port_feature(hub->hdev,
1464 port1, USB_PORT_FEAT_C_RESET);
1465 /* FIXME need disconnect() for NOTATTACHED device */
1466 usb_set_device_state(udev, status
1467 ? USB_STATE_NOTATTACHED
1468 : USB_STATE_DEFAULT);
1469 return status;
1470 }
1471
1472 dev_dbg (hub->intfdev,
1473 "port %d not enabled, trying reset again...\n",
1474 port1);
1475 delay = HUB_LONG_RESET_TIME;
1476 }
1477
1478 dev_err (hub->intfdev,
1479 "Cannot enable port %i. Maybe the USB cable is bad?\n",
1480 port1);
1481
1482 return status;
1483}
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485/*
1486 * Disable a port and mark a logical connnect-change event, so that some
1487 * time later khubd will disconnect() any existing usb_device on the port
1488 * and will re-enumerate if there actually is a device attached.
1489 */
1490static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
1491{
1492 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
1493 hub_port_disable(hub, port1, 1);
1494
1495 /* FIXME let caller ask to power down the port:
1496 * - some devices won't enumerate without a VBUS power cycle
1497 * - SRP saves power that way
Pavel Machekba9d35f2005-04-18 17:39:24 -07001498 * - usb_suspend_device(dev, PMSG_SUSPEND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 * That's easy if this hub can switch power per-port, and
1500 * khubd reactivates the port later (timer, SRP, etc).
1501 * Powerdown must be optional, because of reset/DFU.
1502 */
1503
1504 set_bit(port1, hub->change_bits);
1505 kick_khubd(hub);
1506}
1507
1508
1509#ifdef CONFIG_USB_SUSPEND
1510
1511/*
1512 * Selective port suspend reduces power; most suspended devices draw
1513 * less than 500 uA. It's also used in OTG, along with remote wakeup.
1514 * All devices below the suspended port are also suspended.
1515 *
1516 * Devices leave suspend state when the host wakes them up. Some devices
1517 * also support "remote wakeup", where the device can activate the USB
1518 * tree above them to deliver data, such as a keypress or packet. In
1519 * some cases, this wakes the USB host.
1520 */
1521static int hub_port_suspend(struct usb_hub *hub, int port1,
1522 struct usb_device *udev)
1523{
1524 int status;
1525
1526 // dev_dbg(hub->intfdev, "suspend port %d\n", port1);
1527
1528 /* enable remote wakeup when appropriate; this lets the device
1529 * wake up the upstream hub (including maybe the root hub).
1530 *
1531 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
1532 * we don't explicitly enable it here.
1533 */
1534 if (udev->actconfig
1535 // && FIXME (remote wakeup enabled on this bus)
1536 // ... currently assuming it's always appropriate
1537 && (udev->actconfig->desc.bmAttributes
1538 & USB_CONFIG_ATT_WAKEUP) != 0) {
1539 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1540 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
1541 USB_DEVICE_REMOTE_WAKEUP, 0,
1542 NULL, 0,
1543 USB_CTRL_SET_TIMEOUT);
1544 if (status)
1545 dev_dbg(&udev->dev,
1546 "won't remote wakeup, status %d\n",
1547 status);
1548 }
1549
1550 /* see 7.1.7.6 */
1551 status = set_port_feature(hub->hdev, port1, USB_PORT_FEAT_SUSPEND);
1552 if (status) {
1553 dev_dbg(hub->intfdev,
1554 "can't suspend port %d, status %d\n",
1555 port1, status);
1556 /* paranoia: "should not happen" */
1557 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1558 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
1559 USB_DEVICE_REMOTE_WAKEUP, 0,
1560 NULL, 0,
1561 USB_CTRL_SET_TIMEOUT);
1562 } else {
1563 /* device has up to 10 msec to fully suspend */
1564 dev_dbg(&udev->dev, "usb suspend\n");
1565 usb_set_device_state(udev, USB_STATE_SUSPENDED);
1566 msleep(10);
1567 }
1568 return status;
1569}
1570
1571/*
1572 * Devices on USB hub ports have only one "suspend" state, corresponding
Pavel Machekba9d35f2005-04-18 17:39:24 -07001573 * to ACPI D2, "may cause the device to lose some context".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 * State transitions include:
1575 *
1576 * - suspend, resume ... when the VBUS power link stays live
1577 * - suspend, disconnect ... VBUS lost
1578 *
1579 * Once VBUS drop breaks the circuit, the port it's using has to go through
1580 * normal re-enumeration procedures, starting with enabling VBUS power.
1581 * Other than re-initializing the hub (plug/unplug, except for root hubs),
1582 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
1583 * timer, no SRP, no requests through sysfs.
1584 */
1585static int __usb_suspend_device (struct usb_device *udev, int port1,
1586 pm_message_t state)
1587{
1588 int status;
1589
1590 /* caller owns the udev device lock */
1591 if (port1 < 0)
1592 return port1;
1593
1594 if (udev->state == USB_STATE_SUSPENDED
1595 || udev->state == USB_STATE_NOTATTACHED) {
1596 return 0;
1597 }
1598
1599 /* suspend interface drivers; if this is a hub, it
1600 * suspends the child devices
1601 */
1602 if (udev->actconfig) {
1603 int i;
1604
1605 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1606 struct usb_interface *intf;
1607 struct usb_driver *driver;
1608
1609 intf = udev->actconfig->interface[i];
Pavel Machekca078ba2005-09-03 15:56:57 -07001610 if (state.event <= intf->dev.power.power_state.event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 continue;
1612 if (!intf->dev.driver)
1613 continue;
1614 driver = to_usb_driver(intf->dev.driver);
1615
1616 if (driver->suspend) {
1617 status = driver->suspend(intf, state);
Pavel Machekca078ba2005-09-03 15:56:57 -07001618 if (intf->dev.power.power_state.event != state.event
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 || status)
1620 dev_err(&intf->dev,
1621 "suspend %d fail, code %d\n",
Pavel Machekca078ba2005-09-03 15:56:57 -07001622 state.event, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624
1625 /* only drivers with suspend() can ever resume();
1626 * and after power loss, even they won't.
1627 * bus_rescan_devices() can rebind drivers later.
1628 *
1629 * FIXME the PM core self-deadlocks when unbinding
1630 * drivers during suspend/resume ... everything grabs
1631 * dpm_sem (not a spinlock, ugh). we want to unbind,
1632 * since we know every driver's probe/disconnect works
1633 * even for drivers that can't suspend.
1634 */
Pavel Machekca078ba2005-09-03 15:56:57 -07001635 if (!driver->suspend || state.event > PM_EVENT_FREEZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636#if 1
1637 dev_warn(&intf->dev, "resume is unsafe!\n");
1638#else
1639 down_write(&usb_bus_type.rwsem);
1640 device_release_driver(&intf->dev);
1641 up_write(&usb_bus_type.rwsem);
1642#endif
1643 }
1644 }
1645 }
1646
1647 /*
1648 * FIXME this needs port power off call paths too, to help force
1649 * USB into the "generic" PM model. At least for devices on
1650 * ports that aren't using ganged switching (usually root hubs).
1651 *
1652 * NOTE: SRP-capable links should adopt more aggressive poweroff
1653 * policies (when HNP doesn't apply) once we have mechanisms to
1654 * turn power back on! (Likely not before 2.7...)
1655 */
Pavel Machekca078ba2005-09-03 15:56:57 -07001656 if (state.event > PM_EVENT_FREEZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 dev_warn(&udev->dev, "no poweroff yet, suspending instead\n");
1658 }
1659
1660 /* "global suspend" of the HC-to-USB interface (root hub), or
1661 * "selective suspend" of just one hub-device link.
1662 */
1663 if (!udev->parent) {
1664 struct usb_bus *bus = udev->bus;
1665 if (bus && bus->op->hub_suspend) {
1666 status = bus->op->hub_suspend (bus);
1667 if (status == 0) {
1668 dev_dbg(&udev->dev, "usb suspend\n");
1669 usb_set_device_state(udev,
1670 USB_STATE_SUSPENDED);
1671 }
1672 } else
1673 status = -EOPNOTSUPP;
1674 } else
1675 status = hub_port_suspend(hdev_to_hub(udev->parent), port1,
1676 udev);
1677
1678 if (status == 0)
1679 udev->dev.power.power_state = state;
1680 return status;
1681}
1682
1683/**
1684 * usb_suspend_device - suspend a usb device
1685 * @udev: device that's no longer in active use
1686 * @state: PMSG_SUSPEND to suspend
1687 * Context: must be able to sleep; device not locked
1688 *
1689 * Suspends a USB device that isn't in active use, conserving power.
1690 * Devices may wake out of a suspend, if anything important happens,
1691 * using the remote wakeup mechanism. They may also be taken out of
1692 * suspend by the host, using usb_resume_device(). It's also routine
1693 * to disconnect devices while they are suspended.
1694 *
1695 * Suspending OTG devices may trigger HNP, if that's been enabled
1696 * between a pair of dual-role devices. That will change roles, such
1697 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
1698 *
1699 * Returns 0 on success, else negative errno.
1700 */
1701int usb_suspend_device(struct usb_device *udev, pm_message_t state)
1702{
1703 int port1, status;
1704
1705 port1 = locktree(udev);
1706 if (port1 < 0)
1707 return port1;
1708
1709 status = __usb_suspend_device(udev, port1, state);
1710 usb_unlock_device(udev);
1711 return status;
1712}
1713
1714/*
1715 * hardware resume signaling is finished, either because of selective
1716 * resume (by host) or remote wakeup (by device) ... now see what changed
1717 * in the tree that's rooted at this device.
1718 */
1719static int finish_port_resume(struct usb_device *udev)
1720{
1721 int status;
1722 u16 devstatus;
1723
1724 /* caller owns the udev device lock */
1725 dev_dbg(&udev->dev, "usb resume\n");
1726
1727 /* usb ch9 identifies four variants of SUSPENDED, based on what
1728 * state the device resumes to. Linux currently won't see the
1729 * first two on the host side; they'd be inside hub_port_init()
1730 * during many timeouts, but khubd can't suspend until later.
1731 */
1732 usb_set_device_state(udev, udev->actconfig
1733 ? USB_STATE_CONFIGURED
1734 : USB_STATE_ADDRESS);
1735 udev->dev.power.power_state = PMSG_ON;
1736
1737 /* 10.5.4.5 says be sure devices in the tree are still there.
1738 * For now let's assume the device didn't go crazy on resume,
1739 * and device drivers will know about any resume quirks.
1740 */
1741 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
1742 if (status < 0)
1743 dev_dbg(&udev->dev,
1744 "gone after usb resume? status %d\n",
1745 status);
1746 else if (udev->actconfig) {
1747 unsigned i;
1748
1749 le16_to_cpus(&devstatus);
1750 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
1751 status = usb_control_msg(udev,
1752 usb_sndctrlpipe(udev, 0),
1753 USB_REQ_CLEAR_FEATURE,
1754 USB_RECIP_DEVICE,
1755 USB_DEVICE_REMOTE_WAKEUP, 0,
1756 NULL, 0,
1757 USB_CTRL_SET_TIMEOUT);
1758 if (status) {
1759 dev_dbg(&udev->dev, "disable remote "
1760 "wakeup, status %d\n", status);
1761 status = 0;
1762 }
1763 }
1764
1765 /* resume interface drivers; if this is a hub, it
1766 * resumes the child devices
1767 */
1768 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1769 struct usb_interface *intf;
1770 struct usb_driver *driver;
1771
1772 intf = udev->actconfig->interface[i];
Pavel Machekca078ba2005-09-03 15:56:57 -07001773 if (intf->dev.power.power_state.event == PM_EVENT_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 continue;
1775 if (!intf->dev.driver) {
1776 /* FIXME maybe force to alt 0 */
1777 continue;
1778 }
1779 driver = to_usb_driver(intf->dev.driver);
1780
1781 /* bus_rescan_devices() may rebind drivers */
1782 if (!driver->resume)
1783 continue;
1784
1785 /* can we do better than just logging errors? */
1786 status = driver->resume(intf);
Pavel Machekca078ba2005-09-03 15:56:57 -07001787 if (intf->dev.power.power_state.event != PM_EVENT_ON
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 || status)
1789 dev_dbg(&intf->dev,
1790 "resume fail, state %d code %d\n",
Pavel Machekca078ba2005-09-03 15:56:57 -07001791 intf->dev.power.power_state.event, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793 status = 0;
1794
1795 } else if (udev->devnum <= 0) {
1796 dev_dbg(&udev->dev, "bogus resume!\n");
1797 status = -EINVAL;
1798 }
1799 return status;
1800}
1801
1802static int
1803hub_port_resume(struct usb_hub *hub, int port1, struct usb_device *udev)
1804{
1805 int status;
1806
1807 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
1808
1809 /* see 7.1.7.7; affects power usage, but not budgeting */
1810 status = clear_port_feature(hub->hdev,
1811 port1, USB_PORT_FEAT_SUSPEND);
1812 if (status) {
1813 dev_dbg(hub->intfdev,
1814 "can't resume port %d, status %d\n",
1815 port1, status);
1816 } else {
1817 u16 devstatus;
1818 u16 portchange;
1819
1820 /* drive resume for at least 20 msec */
1821 if (udev)
1822 dev_dbg(&udev->dev, "RESUME\n");
1823 msleep(25);
1824
1825#define LIVE_FLAGS ( USB_PORT_STAT_POWER \
1826 | USB_PORT_STAT_ENABLE \
1827 | USB_PORT_STAT_CONNECTION)
1828
1829 /* Virtual root hubs can trigger on GET_PORT_STATUS to
1830 * stop resume signaling. Then finish the resume
1831 * sequence.
1832 */
1833 devstatus = portchange = 0;
1834 status = hub_port_status(hub, port1,
1835 &devstatus, &portchange);
1836 if (status < 0
1837 || (devstatus & LIVE_FLAGS) != LIVE_FLAGS
1838 || (devstatus & USB_PORT_STAT_SUSPEND) != 0
1839 ) {
1840 dev_dbg(hub->intfdev,
1841 "port %d status %04x.%04x after resume, %d\n",
1842 port1, portchange, devstatus, status);
1843 } else {
1844 /* TRSMRCY = 10 msec */
1845 msleep(10);
1846 if (udev)
1847 status = finish_port_resume(udev);
1848 }
1849 }
1850 if (status < 0)
1851 hub_port_logical_disconnect(hub, port1);
1852
1853 return status;
1854}
1855
1856static int hub_resume (struct usb_interface *intf);
1857
1858/**
1859 * usb_resume_device - re-activate a suspended usb device
1860 * @udev: device to re-activate
1861 * Context: must be able to sleep; device not locked
1862 *
1863 * This will re-activate the suspended device, increasing power usage
1864 * while letting drivers communicate again with its endpoints.
1865 * USB resume explicitly guarantees that the power session between
1866 * the host and the device is the same as it was when the device
1867 * suspended.
1868 *
1869 * Returns 0 on success, else negative errno.
1870 */
1871int usb_resume_device(struct usb_device *udev)
1872{
1873 int port1, status;
1874
1875 port1 = locktree(udev);
1876 if (port1 < 0)
1877 return port1;
1878
1879 /* "global resume" of the HC-to-USB interface (root hub), or
1880 * selective resume of one hub-to-device port
1881 */
1882 if (!udev->parent) {
1883 struct usb_bus *bus = udev->bus;
1884 if (bus && bus->op->hub_resume) {
1885 status = bus->op->hub_resume (bus);
1886 } else
1887 status = -EOPNOTSUPP;
1888 if (status == 0) {
1889 dev_dbg(&udev->dev, "usb resume\n");
1890 /* TRSMRCY = 10 msec */
1891 msleep(10);
1892 usb_set_device_state (udev, USB_STATE_CONFIGURED);
1893 udev->dev.power.power_state = PMSG_ON;
1894 status = hub_resume (udev
1895 ->actconfig->interface[0]);
1896 }
1897 } else if (udev->state == USB_STATE_SUSPENDED) {
1898 // NOTE this fails if parent is also suspended...
1899 status = hub_port_resume(hdev_to_hub(udev->parent),
1900 port1, udev);
1901 } else {
1902 status = 0;
1903 }
1904 if (status < 0) {
1905 dev_dbg(&udev->dev, "can't resume, status %d\n",
1906 status);
1907 }
1908
1909 usb_unlock_device(udev);
1910
1911 /* rebind drivers that had no suspend() */
1912 if (status == 0) {
1913 usb_lock_all_devices();
1914 bus_rescan_devices(&usb_bus_type);
1915 usb_unlock_all_devices();
1916 }
1917 return status;
1918}
1919
1920static int remote_wakeup(struct usb_device *udev)
1921{
1922 int status = 0;
1923
1924 /* don't repeat RESUME sequence if this device
1925 * was already woken up by some other task
1926 */
1927 down(&udev->serialize);
1928 if (udev->state == USB_STATE_SUSPENDED) {
1929 dev_dbg(&udev->dev, "RESUME (wakeup)\n");
1930 /* TRSMRCY = 10 msec */
1931 msleep(10);
1932 status = finish_port_resume(udev);
1933 }
1934 up(&udev->serialize);
1935 return status;
1936}
1937
1938static int hub_suspend(struct usb_interface *intf, pm_message_t state)
1939{
1940 struct usb_hub *hub = usb_get_intfdata (intf);
1941 struct usb_device *hdev = hub->hdev;
1942 unsigned port1;
1943 int status;
1944
1945 /* stop khubd and related activity */
1946 hub_quiesce(hub);
1947
1948 /* then suspend every port */
1949 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
1950 struct usb_device *udev;
1951
1952 udev = hdev->children [port1-1];
1953 if (!udev)
1954 continue;
1955 down(&udev->serialize);
1956 status = __usb_suspend_device(udev, port1, state);
1957 up(&udev->serialize);
1958 if (status < 0)
1959 dev_dbg(&intf->dev, "suspend port %d --> %d\n",
1960 port1, status);
1961 }
1962
1963 intf->dev.power.power_state = state;
1964 return 0;
1965}
1966
1967static int hub_resume(struct usb_interface *intf)
1968{
1969 struct usb_device *hdev = interface_to_usbdev(intf);
1970 struct usb_hub *hub = usb_get_intfdata (intf);
1971 unsigned port1;
1972 int status;
1973
Pavel Machekca078ba2005-09-03 15:56:57 -07001974 if (intf->dev.power.power_state.event == PM_EVENT_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return 0;
1976
1977 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
1978 struct usb_device *udev;
1979 u16 portstat, portchange;
1980
1981 udev = hdev->children [port1-1];
1982 status = hub_port_status(hub, port1, &portstat, &portchange);
1983 if (status == 0) {
1984 if (portchange & USB_PORT_STAT_C_SUSPEND) {
1985 clear_port_feature(hdev, port1,
1986 USB_PORT_FEAT_C_SUSPEND);
1987 portchange &= ~USB_PORT_STAT_C_SUSPEND;
1988 }
1989
1990 /* let khubd handle disconnects etc */
1991 if (portchange)
1992 continue;
1993 }
1994
1995 if (!udev || status < 0)
1996 continue;
1997 down (&udev->serialize);
1998 if (portstat & USB_PORT_STAT_SUSPEND)
1999 status = hub_port_resume(hub, port1, udev);
2000 else {
2001 status = finish_port_resume(udev);
2002 if (status < 0) {
2003 dev_dbg(&intf->dev, "resume port %d --> %d\n",
2004 port1, status);
2005 hub_port_logical_disconnect(hub, port1);
2006 }
2007 }
2008 up(&udev->serialize);
2009 }
2010 intf->dev.power.power_state = PMSG_ON;
2011
2012 hub->resume_root_hub = 0;
2013 hub_activate(hub);
2014 return 0;
2015}
2016
2017void usb_resume_root_hub(struct usb_device *hdev)
2018{
2019 struct usb_hub *hub = hdev_to_hub(hdev);
2020
2021 hub->resume_root_hub = 1;
2022 kick_khubd(hub);
2023}
2024
2025#else /* !CONFIG_USB_SUSPEND */
2026
2027int usb_suspend_device(struct usb_device *udev, pm_message_t state)
2028{
2029 return 0;
2030}
2031
2032int usb_resume_device(struct usb_device *udev)
2033{
2034 return 0;
2035}
2036
2037#define hub_suspend NULL
2038#define hub_resume NULL
2039#define remote_wakeup(x) 0
2040
2041#endif /* CONFIG_USB_SUSPEND */
2042
2043EXPORT_SYMBOL(usb_suspend_device);
2044EXPORT_SYMBOL(usb_resume_device);
2045
2046
2047
2048/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
2049 *
2050 * Between connect detection and reset signaling there must be a delay
2051 * of 100ms at least for debounce and power-settling. The corresponding
2052 * timer shall restart whenever the downstream port detects a disconnect.
2053 *
2054 * Apparently there are some bluetooth and irda-dongles and a number of
2055 * low-speed devices for which this debounce period may last over a second.
2056 * Not covered by the spec - but easy to deal with.
2057 *
2058 * This implementation uses a 1500ms total debounce timeout; if the
2059 * connection isn't stable by then it returns -ETIMEDOUT. It checks
2060 * every 25ms for transient disconnects. When the port status has been
2061 * unchanged for 100ms it returns the port status.
2062 */
2063
2064#define HUB_DEBOUNCE_TIMEOUT 1500
2065#define HUB_DEBOUNCE_STEP 25
2066#define HUB_DEBOUNCE_STABLE 100
2067
2068static int hub_port_debounce(struct usb_hub *hub, int port1)
2069{
2070 int ret;
2071 int total_time, stable_time = 0;
2072 u16 portchange, portstatus;
2073 unsigned connection = 0xffff;
2074
2075 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
2076 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2077 if (ret < 0)
2078 return ret;
2079
2080 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
2081 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
2082 stable_time += HUB_DEBOUNCE_STEP;
2083 if (stable_time >= HUB_DEBOUNCE_STABLE)
2084 break;
2085 } else {
2086 stable_time = 0;
2087 connection = portstatus & USB_PORT_STAT_CONNECTION;
2088 }
2089
2090 if (portchange & USB_PORT_STAT_C_CONNECTION) {
2091 clear_port_feature(hub->hdev, port1,
2092 USB_PORT_FEAT_C_CONNECTION);
2093 }
2094
2095 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
2096 break;
2097 msleep(HUB_DEBOUNCE_STEP);
2098 }
2099
2100 dev_dbg (hub->intfdev,
2101 "debounce: port %d: total %dms stable %dms status 0x%x\n",
2102 port1, total_time, stable_time, portstatus);
2103
2104 if (stable_time < HUB_DEBOUNCE_STABLE)
2105 return -ETIMEDOUT;
2106 return portstatus;
2107}
2108
2109static void ep0_reinit(struct usb_device *udev)
2110{
2111 usb_disable_endpoint(udev, 0 + USB_DIR_IN);
2112 usb_disable_endpoint(udev, 0 + USB_DIR_OUT);
2113 udev->ep_in[0] = udev->ep_out[0] = &udev->ep0;
2114}
2115
2116#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
2117#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
2118
2119static int hub_set_address(struct usb_device *udev)
2120{
2121 int retval;
2122
2123 if (udev->devnum == 0)
2124 return -EINVAL;
2125 if (udev->state == USB_STATE_ADDRESS)
2126 return 0;
2127 if (udev->state != USB_STATE_DEFAULT)
2128 return -EINVAL;
2129 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
2130 USB_REQ_SET_ADDRESS, 0, udev->devnum, 0,
2131 NULL, 0, USB_CTRL_SET_TIMEOUT);
2132 if (retval == 0) {
2133 usb_set_device_state(udev, USB_STATE_ADDRESS);
2134 ep0_reinit(udev);
2135 }
2136 return retval;
2137}
2138
2139/* Reset device, (re)assign address, get device descriptor.
2140 * Device connection must be stable, no more debouncing needed.
2141 * Returns device in USB_STATE_ADDRESS, except on error.
2142 *
2143 * If this is called for an already-existing device (as part of
2144 * usb_reset_device), the caller must own the device lock. For a
2145 * newly detected device that is not accessible through any global
2146 * pointers, it's not necessary to lock the device.
2147 */
2148static int
2149hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2150 int retry_counter)
2151{
2152 static DECLARE_MUTEX(usb_address0_sem);
2153
2154 struct usb_device *hdev = hub->hdev;
2155 int i, j, retval;
2156 unsigned delay = HUB_SHORT_RESET_TIME;
2157 enum usb_device_speed oldspeed = udev->speed;
2158
2159 /* root hub ports have a slightly longer reset period
2160 * (from USB 2.0 spec, section 7.1.7.5)
2161 */
2162 if (!hdev->parent) {
2163 delay = HUB_ROOT_RESET_TIME;
2164 if (port1 == hdev->bus->otg_port)
2165 hdev->bus->b_hnp_enable = 0;
2166 }
2167
2168 /* Some low speed devices have problems with the quick delay, so */
2169 /* be a bit pessimistic with those devices. RHbug #23670 */
2170 if (oldspeed == USB_SPEED_LOW)
2171 delay = HUB_LONG_RESET_TIME;
2172
2173 down(&usb_address0_sem);
2174
2175 /* Reset the device; full speed may morph to high speed */
2176 retval = hub_port_reset(hub, port1, udev, delay);
2177 if (retval < 0) /* error or disconnect */
2178 goto fail;
2179 /* success, speed is known */
2180 retval = -ENODEV;
2181
2182 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
2183 dev_dbg(&udev->dev, "device reset changed speed!\n");
2184 goto fail;
2185 }
2186 oldspeed = udev->speed;
2187
2188 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
2189 * it's fixed size except for full speed devices.
2190 */
2191 switch (udev->speed) {
2192 case USB_SPEED_HIGH: /* fixed at 64 */
2193 udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
2194 break;
2195 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
2196 /* to determine the ep0 maxpacket size, try to read
2197 * the device descriptor to get bMaxPacketSize0 and
2198 * then correct our initial guess.
2199 */
2200 udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
2201 break;
2202 case USB_SPEED_LOW: /* fixed at 8 */
2203 udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(8);
2204 break;
2205 default:
2206 goto fail;
2207 }
2208
2209 dev_info (&udev->dev,
2210 "%s %s speed USB device using %s and address %d\n",
2211 (udev->config) ? "reset" : "new",
2212 ({ char *speed; switch (udev->speed) {
2213 case USB_SPEED_LOW: speed = "low"; break;
2214 case USB_SPEED_FULL: speed = "full"; break;
2215 case USB_SPEED_HIGH: speed = "high"; break;
2216 default: speed = "?"; break;
2217 }; speed;}),
2218 udev->bus->controller->driver->name,
2219 udev->devnum);
2220
2221 /* Set up TT records, if needed */
2222 if (hdev->tt) {
2223 udev->tt = hdev->tt;
2224 udev->ttport = hdev->ttport;
2225 } else if (udev->speed != USB_SPEED_HIGH
2226 && hdev->speed == USB_SPEED_HIGH) {
2227 udev->tt = &hub->tt;
2228 udev->ttport = port1;
2229 }
2230
2231 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
2232 * Because device hardware and firmware is sometimes buggy in
2233 * this area, and this is how Linux has done it for ages.
2234 * Change it cautiously.
2235 *
2236 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
2237 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
2238 * so it may help with some non-standards-compliant devices.
2239 * Otherwise we start with SET_ADDRESS and then try to read the
2240 * first 8 bytes of the device descriptor to get the ep0 maxpacket
2241 * value.
2242 */
2243 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
2244 if (USE_NEW_SCHEME(retry_counter)) {
2245 struct usb_device_descriptor *buf;
2246 int r = 0;
2247
2248#define GET_DESCRIPTOR_BUFSIZE 64
2249 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
2250 if (!buf) {
2251 retval = -ENOMEM;
2252 continue;
2253 }
2254
2255 /* Use a short timeout the first time through,
2256 * so that recalcitrant full-speed devices with
2257 * 8- or 16-byte ep0-maxpackets won't slow things
2258 * down tremendously by NAKing the unexpectedly
2259 * early status stage. Also, retry on all errors;
2260 * some devices are flakey.
2261 */
2262 for (j = 0; j < 3; ++j) {
2263 buf->bMaxPacketSize0 = 0;
2264 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
2265 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
2266 USB_DT_DEVICE << 8, 0,
2267 buf, GET_DESCRIPTOR_BUFSIZE,
2268 (i ? USB_CTRL_GET_TIMEOUT : 1000));
2269 switch (buf->bMaxPacketSize0) {
2270 case 8: case 16: case 32: case 64:
2271 if (buf->bDescriptorType ==
2272 USB_DT_DEVICE) {
2273 r = 0;
2274 break;
2275 }
2276 /* FALL THROUGH */
2277 default:
2278 if (r == 0)
2279 r = -EPROTO;
2280 break;
2281 }
2282 if (r == 0)
2283 break;
2284 }
2285 udev->descriptor.bMaxPacketSize0 =
2286 buf->bMaxPacketSize0;
2287 kfree(buf);
2288
2289 retval = hub_port_reset(hub, port1, udev, delay);
2290 if (retval < 0) /* error or disconnect */
2291 goto fail;
2292 if (oldspeed != udev->speed) {
2293 dev_dbg(&udev->dev,
2294 "device reset changed speed!\n");
2295 retval = -ENODEV;
2296 goto fail;
2297 }
2298 if (r) {
2299 dev_err(&udev->dev, "device descriptor "
2300 "read/%s, error %d\n",
2301 "64", r);
2302 retval = -EMSGSIZE;
2303 continue;
2304 }
2305#undef GET_DESCRIPTOR_BUFSIZE
2306 }
2307
2308 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
2309 retval = hub_set_address(udev);
2310 if (retval >= 0)
2311 break;
2312 msleep(200);
2313 }
2314 if (retval < 0) {
2315 dev_err(&udev->dev,
2316 "device not accepting address %d, error %d\n",
2317 udev->devnum, retval);
2318 goto fail;
2319 }
2320
2321 /* cope with hardware quirkiness:
2322 * - let SET_ADDRESS settle, some device hardware wants it
2323 * - read ep0 maxpacket even for high and low speed,
2324 */
2325 msleep(10);
2326 if (USE_NEW_SCHEME(retry_counter))
2327 break;
2328
2329 retval = usb_get_device_descriptor(udev, 8);
2330 if (retval < 8) {
2331 dev_err(&udev->dev, "device descriptor "
2332 "read/%s, error %d\n",
2333 "8", retval);
2334 if (retval >= 0)
2335 retval = -EMSGSIZE;
2336 } else {
2337 retval = 0;
2338 break;
2339 }
2340 }
2341 if (retval)
2342 goto fail;
2343
2344 i = udev->descriptor.bMaxPacketSize0;
2345 if (le16_to_cpu(udev->ep0.desc.wMaxPacketSize) != i) {
2346 if (udev->speed != USB_SPEED_FULL ||
2347 !(i == 8 || i == 16 || i == 32 || i == 64)) {
2348 dev_err(&udev->dev, "ep0 maxpacket = %d\n", i);
2349 retval = -EMSGSIZE;
2350 goto fail;
2351 }
2352 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
2353 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
2354 ep0_reinit(udev);
2355 }
2356
2357 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
2358 if (retval < (signed)sizeof(udev->descriptor)) {
2359 dev_err(&udev->dev, "device descriptor read/%s, error %d\n",
2360 "all", retval);
2361 if (retval >= 0)
2362 retval = -ENOMSG;
2363 goto fail;
2364 }
2365
2366 retval = 0;
2367
2368fail:
2369 if (retval)
2370 hub_port_disable(hub, port1, 0);
2371 up(&usb_address0_sem);
2372 return retval;
2373}
2374
2375static void
2376check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
2377{
2378 struct usb_qualifier_descriptor *qual;
2379 int status;
2380
2381 qual = kmalloc (sizeof *qual, SLAB_KERNEL);
2382 if (qual == NULL)
2383 return;
2384
2385 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
2386 qual, sizeof *qual);
2387 if (status == sizeof *qual) {
2388 dev_info(&udev->dev, "not running at top speed; "
2389 "connect to a high speed hub\n");
2390 /* hub LEDs are probably harder to miss than syslog */
2391 if (hub->has_indicators) {
2392 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
2393 schedule_work (&hub->leds);
2394 }
2395 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07002396 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
2399static unsigned
2400hub_power_remaining (struct usb_hub *hub)
2401{
2402 struct usb_device *hdev = hub->hdev;
2403 int remaining;
2404 unsigned i;
2405
2406 remaining = hub->power_budget;
2407 if (!remaining) /* self-powered */
2408 return 0;
2409
2410 for (i = 0; i < hdev->maxchild; i++) {
2411 struct usb_device *udev = hdev->children[i];
2412 int delta, ceiling;
2413
2414 if (!udev)
2415 continue;
2416
2417 /* 100mA per-port ceiling, or 8mA for OTG ports */
2418 if (i != (udev->bus->otg_port - 1) || hdev->parent)
2419 ceiling = 50;
2420 else
2421 ceiling = 4;
2422
2423 if (udev->actconfig)
2424 delta = udev->actconfig->desc.bMaxPower;
2425 else
2426 delta = ceiling;
2427 // dev_dbg(&udev->dev, "budgeted %dmA\n", 2 * delta);
2428 if (delta > ceiling)
2429 dev_warn(&udev->dev, "%dmA over %dmA budget!\n",
2430 2 * (delta - ceiling), 2 * ceiling);
2431 remaining -= delta;
2432 }
2433 if (remaining < 0) {
2434 dev_warn(hub->intfdev,
2435 "%dmA over power budget!\n",
2436 -2 * remaining);
2437 remaining = 0;
2438 }
2439 return remaining;
2440}
2441
2442/* Handle physical or logical connection change events.
2443 * This routine is called when:
2444 * a port connection-change occurs;
2445 * a port enable-change occurs (often caused by EMI);
2446 * usb_reset_device() encounters changed descriptors (as from
2447 * a firmware download)
2448 * caller already locked the hub
2449 */
2450static void hub_port_connect_change(struct usb_hub *hub, int port1,
2451 u16 portstatus, u16 portchange)
2452{
2453 struct usb_device *hdev = hub->hdev;
2454 struct device *hub_dev = hub->intfdev;
2455 int status, i;
2456
2457 dev_dbg (hub_dev,
2458 "port %d, status %04x, change %04x, %s\n",
2459 port1, portstatus, portchange, portspeed (portstatus));
2460
2461 if (hub->has_indicators) {
2462 set_port_led(hub, port1, HUB_LED_AUTO);
2463 hub->indicator[port1-1] = INDICATOR_AUTO;
2464 }
2465
2466 /* Disconnect any existing devices under this port */
2467 if (hdev->children[port1-1])
2468 usb_disconnect(&hdev->children[port1-1]);
2469 clear_bit(port1, hub->change_bits);
2470
2471#ifdef CONFIG_USB_OTG
2472 /* during HNP, don't repeat the debounce */
2473 if (hdev->bus->is_b_host)
2474 portchange &= ~USB_PORT_STAT_C_CONNECTION;
2475#endif
2476
2477 if (portchange & USB_PORT_STAT_C_CONNECTION) {
2478 status = hub_port_debounce(hub, port1);
2479 if (status < 0) {
2480 dev_err (hub_dev,
2481 "connect-debounce failed, port %d disabled\n",
2482 port1);
2483 goto done;
2484 }
2485 portstatus = status;
2486 }
2487
2488 /* Return now if nothing is connected */
2489 if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
2490
2491 /* maybe switch power back on (e.g. root hub was reset) */
2492 if ((hub->descriptor->wHubCharacteristics
2493 & HUB_CHAR_LPSM) < 2
2494 && !(portstatus & (1 << USB_PORT_FEAT_POWER)))
2495 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
2496
2497 if (portstatus & USB_PORT_STAT_ENABLE)
2498 goto done;
2499 return;
2500 }
2501
2502#ifdef CONFIG_USB_SUSPEND
2503 /* If something is connected, but the port is suspended, wake it up. */
2504 if (portstatus & USB_PORT_STAT_SUSPEND) {
2505 status = hub_port_resume(hub, port1, NULL);
2506 if (status < 0) {
2507 dev_dbg(hub_dev,
2508 "can't clear suspend on port %d; %d\n",
2509 port1, status);
2510 goto done;
2511 }
2512 }
2513#endif
2514
2515 for (i = 0; i < SET_CONFIG_TRIES; i++) {
2516 struct usb_device *udev;
2517
2518 /* reallocate for each attempt, since references
2519 * to the previous one can escape in various ways
2520 */
2521 udev = usb_alloc_dev(hdev, hdev->bus, port1);
2522 if (!udev) {
2523 dev_err (hub_dev,
2524 "couldn't allocate port %d usb_device\n",
2525 port1);
2526 goto done;
2527 }
2528
2529 usb_set_device_state(udev, USB_STATE_POWERED);
2530 udev->speed = USB_SPEED_UNKNOWN;
2531
2532 /* set the address */
2533 choose_address(udev);
2534 if (udev->devnum <= 0) {
2535 status = -ENOTCONN; /* Don't retry */
2536 goto loop;
2537 }
2538
2539 /* reset and get descriptor */
2540 status = hub_port_init(hub, udev, port1, i);
2541 if (status < 0)
2542 goto loop;
2543
2544 /* consecutive bus-powered hubs aren't reliable; they can
2545 * violate the voltage drop budget. if the new child has
2546 * a "powered" LED, users should notice we didn't enable it
2547 * (without reading syslog), even without per-port LEDs
2548 * on the parent.
2549 */
2550 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
2551 && hub->power_budget) {
2552 u16 devstat;
2553
2554 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
2555 &devstat);
2556 if (status < 0) {
2557 dev_dbg(&udev->dev, "get status %d ?\n", status);
2558 goto loop_disable;
2559 }
2560 cpu_to_le16s(&devstat);
2561 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
2562 dev_err(&udev->dev,
2563 "can't connect bus-powered hub "
2564 "to this port\n");
2565 if (hub->has_indicators) {
2566 hub->indicator[port1-1] =
2567 INDICATOR_AMBER_BLINK;
2568 schedule_work (&hub->leds);
2569 }
2570 status = -ENOTCONN; /* Don't retry */
2571 goto loop_disable;
2572 }
2573 }
2574
2575 /* check for devices running slower than they could */
2576 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
2577 && udev->speed == USB_SPEED_FULL
2578 && highspeed_hubs != 0)
2579 check_highspeed (hub, udev, port1);
2580
2581 /* Store the parent's children[] pointer. At this point
2582 * udev becomes globally accessible, although presumably
2583 * no one will look at it until hdev is unlocked.
2584 */
2585 down (&udev->serialize);
2586 status = 0;
2587
2588 /* We mustn't add new devices if the parent hub has
2589 * been disconnected; we would race with the
2590 * recursively_mark_NOTATTACHED() routine.
2591 */
2592 spin_lock_irq(&device_state_lock);
2593 if (hdev->state == USB_STATE_NOTATTACHED)
2594 status = -ENOTCONN;
2595 else
2596 hdev->children[port1-1] = udev;
2597 spin_unlock_irq(&device_state_lock);
2598
2599 /* Run it through the hoops (find a driver, etc) */
2600 if (!status) {
2601 status = usb_new_device(udev);
2602 if (status) {
2603 spin_lock_irq(&device_state_lock);
2604 hdev->children[port1-1] = NULL;
2605 spin_unlock_irq(&device_state_lock);
2606 }
2607 }
2608
2609 up (&udev->serialize);
2610 if (status)
2611 goto loop_disable;
2612
2613 status = hub_power_remaining(hub);
2614 if (status)
2615 dev_dbg(hub_dev,
2616 "%dmA power budget left\n",
2617 2 * status);
2618
2619 return;
2620
2621loop_disable:
2622 hub_port_disable(hub, port1, 1);
2623loop:
2624 ep0_reinit(udev);
2625 release_address(udev);
2626 usb_put_dev(udev);
2627 if (status == -ENOTCONN)
2628 break;
2629 }
2630
2631done:
2632 hub_port_disable(hub, port1, 1);
2633}
2634
2635static void hub_events(void)
2636{
2637 struct list_head *tmp;
2638 struct usb_device *hdev;
2639 struct usb_interface *intf;
2640 struct usb_hub *hub;
2641 struct device *hub_dev;
2642 u16 hubstatus;
2643 u16 hubchange;
2644 u16 portstatus;
2645 u16 portchange;
2646 int i, ret;
2647 int connect_change;
2648
2649 /*
2650 * We restart the list every time to avoid a deadlock with
2651 * deleting hubs downstream from this one. This should be
2652 * safe since we delete the hub from the event list.
2653 * Not the most efficient, but avoids deadlocks.
2654 */
2655 while (1) {
2656
2657 /* Grab the first entry at the beginning of the list */
2658 spin_lock_irq(&hub_event_lock);
2659 if (list_empty(&hub_event_list)) {
2660 spin_unlock_irq(&hub_event_lock);
2661 break;
2662 }
2663
2664 tmp = hub_event_list.next;
2665 list_del_init(tmp);
2666
2667 hub = list_entry(tmp, struct usb_hub, event_list);
2668 hdev = hub->hdev;
2669 intf = to_usb_interface(hub->intfdev);
2670 hub_dev = &intf->dev;
2671
2672 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
2673 hdev->state, hub->descriptor
2674 ? hub->descriptor->bNbrPorts
2675 : 0,
2676 /* NOTE: expects max 15 ports... */
2677 (u16) hub->change_bits[0],
2678 (u16) hub->event_bits[0]);
2679
2680 usb_get_intf(intf);
2681 i = hub->resume_root_hub;
2682 spin_unlock_irq(&hub_event_lock);
2683
2684 /* Is this is a root hub wanting to be resumed? */
2685 if (i)
2686 usb_resume_device(hdev);
2687
2688 /* Lock the device, then check to see if we were
2689 * disconnected while waiting for the lock to succeed. */
2690 if (locktree(hdev) < 0) {
2691 usb_put_intf(intf);
2692 continue;
2693 }
2694 if (hub != usb_get_intfdata(intf))
2695 goto loop;
2696
2697 /* If the hub has died, clean up after it */
2698 if (hdev->state == USB_STATE_NOTATTACHED) {
2699 hub_pre_reset(hub);
2700 goto loop;
2701 }
2702
2703 /* If this is an inactive or suspended hub, do nothing */
2704 if (hub->quiescing)
2705 goto loop;
2706
2707 if (hub->error) {
2708 dev_dbg (hub_dev, "resetting for error %d\n",
2709 hub->error);
2710
2711 ret = usb_reset_device(hdev);
2712 if (ret) {
2713 dev_dbg (hub_dev,
2714 "error resetting hub: %d\n", ret);
2715 goto loop;
2716 }
2717
2718 hub->nerrors = 0;
2719 hub->error = 0;
2720 }
2721
2722 /* deal with port status changes */
2723 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
2724 if (test_bit(i, hub->busy_bits))
2725 continue;
2726 connect_change = test_bit(i, hub->change_bits);
2727 if (!test_and_clear_bit(i, hub->event_bits) &&
2728 !connect_change && !hub->activating)
2729 continue;
2730
2731 ret = hub_port_status(hub, i,
2732 &portstatus, &portchange);
2733 if (ret < 0)
2734 continue;
2735
2736 if (hub->activating && !hdev->children[i-1] &&
2737 (portstatus &
2738 USB_PORT_STAT_CONNECTION))
2739 connect_change = 1;
2740
2741 if (portchange & USB_PORT_STAT_C_CONNECTION) {
2742 clear_port_feature(hdev, i,
2743 USB_PORT_FEAT_C_CONNECTION);
2744 connect_change = 1;
2745 }
2746
2747 if (portchange & USB_PORT_STAT_C_ENABLE) {
2748 if (!connect_change)
2749 dev_dbg (hub_dev,
2750 "port %d enable change, "
2751 "status %08x\n",
2752 i, portstatus);
2753 clear_port_feature(hdev, i,
2754 USB_PORT_FEAT_C_ENABLE);
2755
2756 /*
2757 * EM interference sometimes causes badly
2758 * shielded USB devices to be shutdown by
2759 * the hub, this hack enables them again.
2760 * Works at least with mouse driver.
2761 */
2762 if (!(portstatus & USB_PORT_STAT_ENABLE)
2763 && !connect_change
2764 && hdev->children[i-1]) {
2765 dev_err (hub_dev,
2766 "port %i "
2767 "disabled by hub (EMI?), "
2768 "re-enabling...\n",
2769 i);
2770 connect_change = 1;
2771 }
2772 }
2773
2774 if (portchange & USB_PORT_STAT_C_SUSPEND) {
2775 clear_port_feature(hdev, i,
2776 USB_PORT_FEAT_C_SUSPEND);
2777 if (hdev->children[i-1]) {
2778 ret = remote_wakeup(hdev->
2779 children[i-1]);
2780 if (ret < 0)
2781 connect_change = 1;
2782 } else {
2783 ret = -ENODEV;
2784 hub_port_disable(hub, i, 1);
2785 }
2786 dev_dbg (hub_dev,
2787 "resume on port %d, status %d\n",
2788 i, ret);
2789 }
2790
2791 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
2792 dev_err (hub_dev,
2793 "over-current change on port %d\n",
2794 i);
2795 clear_port_feature(hdev, i,
2796 USB_PORT_FEAT_C_OVER_CURRENT);
2797 hub_power_on(hub);
2798 }
2799
2800 if (portchange & USB_PORT_STAT_C_RESET) {
2801 dev_dbg (hub_dev,
2802 "reset change on port %d\n",
2803 i);
2804 clear_port_feature(hdev, i,
2805 USB_PORT_FEAT_C_RESET);
2806 }
2807
2808 if (connect_change)
2809 hub_port_connect_change(hub, i,
2810 portstatus, portchange);
2811 } /* end for i */
2812
2813 /* deal with hub status changes */
2814 if (test_and_clear_bit(0, hub->event_bits) == 0)
2815 ; /* do nothing */
2816 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
2817 dev_err (hub_dev, "get_hub_status failed\n");
2818 else {
2819 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
2820 dev_dbg (hub_dev, "power change\n");
2821 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
2822 }
2823 if (hubchange & HUB_CHANGE_OVERCURRENT) {
2824 dev_dbg (hub_dev, "overcurrent change\n");
2825 msleep(500); /* Cool down */
2826 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
2827 hub_power_on(hub);
2828 }
2829 }
2830
2831 hub->activating = 0;
2832
Alan Sternd5926ae72005-04-21 15:56:37 -04002833 /* If this is a root hub, tell the HCD it's okay to
2834 * re-enable port-change interrupts now. */
2835 if (!hdev->parent)
2836 usb_enable_root_hub_irq(hdev->bus);
2837
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838loop:
2839 usb_unlock_device(hdev);
2840 usb_put_intf(intf);
2841
2842 } /* end while (1) */
2843}
2844
2845static int hub_thread(void *__unused)
2846{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 do {
2848 hub_events();
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07002849 wait_event_interruptible(khubd_wait,
2850 !list_empty(&hub_event_list) ||
2851 kthread_should_stop());
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07002852 try_to_freeze();
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07002853 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07002855 pr_debug("%s: khubd exiting\n", usbcore_name);
2856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857}
2858
2859static struct usb_device_id hub_id_table [] = {
2860 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
2861 .bDeviceClass = USB_CLASS_HUB},
2862 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
2863 .bInterfaceClass = USB_CLASS_HUB},
2864 { } /* Terminating entry */
2865};
2866
2867MODULE_DEVICE_TABLE (usb, hub_id_table);
2868
2869static struct usb_driver hub_driver = {
2870 .owner = THIS_MODULE,
2871 .name = "hub",
2872 .probe = hub_probe,
2873 .disconnect = hub_disconnect,
2874 .suspend = hub_suspend,
2875 .resume = hub_resume,
2876 .ioctl = hub_ioctl,
2877 .id_table = hub_id_table,
2878};
2879
2880int usb_hub_init(void)
2881{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 if (usb_register(&hub_driver) < 0) {
2883 printk(KERN_ERR "%s: can't register hub driver\n",
2884 usbcore_name);
2885 return -1;
2886 }
2887
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07002888 khubd_task = kthread_run(hub_thread, NULL, "khubd");
2889 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
2892 /* Fall through if kernel_thread failed */
2893 usb_deregister(&hub_driver);
2894 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
2895
2896 return -1;
2897}
2898
2899void usb_hub_cleanup(void)
2900{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07002901 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
2903 /*
2904 * Hub resources are freed for us by usb_deregister. It calls
2905 * usb_driver_purge on every device which in turn calls that
2906 * devices disconnect function if it is using this driver.
2907 * The hub_disconnect function takes care of releasing the
2908 * individual hub resources. -greg
2909 */
2910 usb_deregister(&hub_driver);
2911} /* usb_hub_cleanup() */
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913static int config_descriptors_changed(struct usb_device *udev)
2914{
2915 unsigned index;
2916 unsigned len = 0;
2917 struct usb_config_descriptor *buf;
2918
2919 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
2920 if (len < le16_to_cpu(udev->config[index].desc.wTotalLength))
2921 len = le16_to_cpu(udev->config[index].desc.wTotalLength);
2922 }
2923 buf = kmalloc (len, SLAB_KERNEL);
2924 if (buf == NULL) {
2925 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
2926 /* assume the worst */
2927 return 1;
2928 }
2929 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
2930 int length;
2931 int old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
2932
2933 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
2934 old_length);
2935 if (length < old_length) {
2936 dev_dbg(&udev->dev, "config index %d, error %d\n",
2937 index, length);
2938 break;
2939 }
2940 if (memcmp (buf, udev->rawdescriptors[index], old_length)
2941 != 0) {
2942 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
2943 index, buf->bConfigurationValue);
2944 break;
2945 }
2946 }
2947 kfree(buf);
2948 return index != udev->descriptor.bNumConfigurations;
2949}
2950
2951/**
2952 * usb_reset_device - perform a USB port reset to reinitialize a device
2953 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
2954 *
2955 * WARNING - don't reset any device unless drivers for all of its
2956 * interfaces are expecting that reset! Maybe some driver->reset()
2957 * method should eventually help ensure sufficient cooperation.
2958 *
2959 * Do a port reset, reassign the device's address, and establish its
2960 * former operating configuration. If the reset fails, or the device's
2961 * descriptors change from their values before the reset, or the original
2962 * configuration and altsettings cannot be restored, a flag will be set
2963 * telling khubd to pretend the device has been disconnected and then
2964 * re-connected. All drivers will be unbound, and the device will be
2965 * re-enumerated and probed all over again.
2966 *
2967 * Returns 0 if the reset succeeded, -ENODEV if the device has been
2968 * flagged for logical disconnection, or some other negative error code
2969 * if the reset wasn't even attempted.
2970 *
2971 * The caller must own the device lock. For example, it's safe to use
2972 * this from a driver probe() routine after downloading new firmware.
2973 * For calls that might not occur during probe(), drivers should lock
2974 * the device using usb_lock_device_for_reset().
2975 */
2976int usb_reset_device(struct usb_device *udev)
2977{
2978 struct usb_device *parent_hdev = udev->parent;
2979 struct usb_hub *parent_hub;
2980 struct usb_device_descriptor descriptor = udev->descriptor;
2981 struct usb_hub *hub = NULL;
2982 int i, ret = 0, port1 = -1;
2983
2984 if (udev->state == USB_STATE_NOTATTACHED ||
2985 udev->state == USB_STATE_SUSPENDED) {
2986 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
2987 udev->state);
2988 return -EINVAL;
2989 }
2990
2991 if (!parent_hdev) {
2992 /* this requires hcd-specific logic; see OHCI hc_restart() */
2993 dev_dbg(&udev->dev, "%s for root hub!\n", __FUNCTION__);
2994 return -EISDIR;
2995 }
2996
2997 for (i = 0; i < parent_hdev->maxchild; i++)
2998 if (parent_hdev->children[i] == udev) {
2999 port1 = i + 1;
3000 break;
3001 }
3002
3003 if (port1 < 0) {
3004 /* If this ever happens, it's very bad */
3005 dev_err(&udev->dev, "Can't locate device's port!\n");
3006 return -ENOENT;
3007 }
3008 parent_hub = hdev_to_hub(parent_hdev);
3009
3010 /* If we're resetting an active hub, take some special actions */
3011 if (udev->actconfig &&
3012 udev->actconfig->interface[0]->dev.driver ==
3013 &hub_driver.driver &&
3014 (hub = hdev_to_hub(udev)) != NULL) {
3015 hub_pre_reset(hub);
3016 }
3017
3018 set_bit(port1, parent_hub->busy_bits);
3019 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
3020
3021 /* ep0 maxpacket size may change; let the HCD know about it.
3022 * Other endpoints will be handled by re-enumeration. */
3023 ep0_reinit(udev);
3024 ret = hub_port_init(parent_hub, udev, port1, i);
3025 if (ret >= 0)
3026 break;
3027 }
3028 clear_bit(port1, parent_hub->busy_bits);
3029 if (ret < 0)
3030 goto re_enumerate;
3031
3032 /* Device might have changed firmware (DFU or similar) */
3033 if (memcmp(&udev->descriptor, &descriptor, sizeof descriptor)
3034 || config_descriptors_changed (udev)) {
3035 dev_info(&udev->dev, "device firmware changed\n");
3036 udev->descriptor = descriptor; /* for disconnect() calls */
3037 goto re_enumerate;
3038 }
3039
3040 if (!udev->actconfig)
3041 goto done;
3042
3043 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3044 USB_REQ_SET_CONFIGURATION, 0,
3045 udev->actconfig->desc.bConfigurationValue, 0,
3046 NULL, 0, USB_CTRL_SET_TIMEOUT);
3047 if (ret < 0) {
3048 dev_err(&udev->dev,
3049 "can't restore configuration #%d (error=%d)\n",
3050 udev->actconfig->desc.bConfigurationValue, ret);
3051 goto re_enumerate;
3052 }
3053 usb_set_device_state(udev, USB_STATE_CONFIGURED);
3054
3055 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
3056 struct usb_interface *intf = udev->actconfig->interface[i];
3057 struct usb_interface_descriptor *desc;
3058
3059 /* set_interface resets host side toggle even
3060 * for altsetting zero. the interface may have no driver.
3061 */
3062 desc = &intf->cur_altsetting->desc;
3063 ret = usb_set_interface(udev, desc->bInterfaceNumber,
3064 desc->bAlternateSetting);
3065 if (ret < 0) {
3066 dev_err(&udev->dev, "failed to restore interface %d "
3067 "altsetting %d (error=%d)\n",
3068 desc->bInterfaceNumber,
3069 desc->bAlternateSetting,
3070 ret);
3071 goto re_enumerate;
3072 }
3073 }
3074
3075done:
3076 if (hub)
3077 hub_post_reset(hub);
3078 return 0;
3079
3080re_enumerate:
3081 hub_port_logical_disconnect(parent_hub, port1);
3082 return -ENODEV;
3083}