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)
11 #include <linux/config.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/completion.h>
17 #include <linux/sched.h>
18 #include <linux/list.h>
19 #include <linux/slab.h>
20 #include <linux/smp_lock.h>
21 #include <linux/ioctl.h>
22 #ifdef CONFIG_USB_DEBUG
27 #include <linux/usb.h>
28 #include <linux/usbdevice_fs.h>
29 #include <linux/suspend.h>
31 #include <asm/semaphore.h>
32 #include <asm/uaccess.h>
33 #include <asm/byteorder.h>
40 static spinlock_t hub_event_lock = SPIN_LOCK_UNLOCKED;
41 static DECLARE_MUTEX(usb_address0_sem);
43 static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
44 static LIST_HEAD(hub_list); /* List of all hubs (for cleanup) */
46 static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
47 static pid_t khubd_pid = 0; /* PID of khubd */
48 static DECLARE_COMPLETION(khubd_exited);
50 /* cycle leds on hubs that aren't blinking for attention */
51 static int blinkenlights = 0;
52 module_param (blinkenlights, bool, S_IRUGO);
53 MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
57 static inline char *portspeed (int portstatus)
59 if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
61 else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
68 /* for dev_info, dev_dbg, etc */
69 static inline struct device *hubdev (struct usb_device *dev)
71 return &dev->actconfig->interface[0]->dev;
74 /* USB 2.0 spec Section 11.24.4.5 */
75 static int get_hub_descriptor(struct usb_device *dev, void *data, int size)
77 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
78 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
79 USB_DT_HUB << 8, 0, data, size, HZ * USB_CTRL_GET_TIMEOUT);
83 * USB 2.0 spec Section 11.24.2.1
85 static int clear_hub_feature(struct usb_device *dev, int feature)
87 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
88 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, HZ);
92 * USB 2.0 spec Section 11.24.2.2
94 static int clear_port_feature(struct usb_device *dev, int port, int feature)
96 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
97 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port, NULL, 0, HZ);
101 * USB 2.0 spec Section 11.24.2.13
103 static int set_port_feature(struct usb_device *dev, int port, int feature)
105 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
106 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port, NULL, 0, HZ);
110 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
111 * for info about using port indicators
113 static void set_port_led(
114 struct usb_device *dev,
120 int status = set_port_feature(dev, (selector << 8) | port,
121 USB_PORT_FEAT_INDICATOR);
123 dev_dbg (&hub->intf->dev,
124 "port %d indicator %s status %d\n",
126 ({ char *s; switch (selector) {
127 case HUB_LED_AMBER: s = "amber"; break;
128 case HUB_LED_GREEN: s = "green"; break;
129 case HUB_LED_OFF: s = "off"; break;
130 case HUB_LED_AUTO: s = "auto"; break;
131 default: s = "??"; break;
136 #define LED_CYCLE_PERIOD ((2*HZ)/3)
138 static void led_work (void *__hub)
140 struct usb_hub *hub = __hub;
141 struct usb_device *dev = interface_to_usbdev (hub->intf);
143 unsigned changed = 0;
146 if (dev->state != USB_STATE_CONFIGURED)
149 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
150 unsigned selector, mode;
152 /* 30%-50% duty cycle */
154 switch (hub->indicator[i]) {
156 case INDICATOR_CYCLE:
158 selector = HUB_LED_AUTO;
159 mode = INDICATOR_AUTO;
161 /* blinking green = sw attention */
162 case INDICATOR_GREEN_BLINK:
163 selector = HUB_LED_GREEN;
164 mode = INDICATOR_GREEN_BLINK_OFF;
166 case INDICATOR_GREEN_BLINK_OFF:
167 selector = HUB_LED_OFF;
168 mode = INDICATOR_GREEN_BLINK;
170 /* blinking amber = hw attention */
171 case INDICATOR_AMBER_BLINK:
172 selector = HUB_LED_AMBER;
173 mode = INDICATOR_AMBER_BLINK_OFF;
175 case INDICATOR_AMBER_BLINK_OFF:
176 selector = HUB_LED_OFF;
177 mode = INDICATOR_AMBER_BLINK;
179 /* blink green/amber = reserved */
180 case INDICATOR_ALT_BLINK:
181 selector = HUB_LED_GREEN;
182 mode = INDICATOR_ALT_BLINK_OFF;
184 case INDICATOR_ALT_BLINK_OFF:
185 selector = HUB_LED_AMBER;
186 mode = INDICATOR_ALT_BLINK;
191 if (selector != HUB_LED_AUTO)
193 set_port_led(dev, hub, i + 1, selector);
194 hub->indicator[i] = mode;
196 if (!changed && blinkenlights) {
198 cursor %= hub->descriptor->bNbrPorts;
199 set_port_led(dev, hub, cursor + 1, HUB_LED_GREEN);
200 hub->indicator[cursor] = INDICATOR_CYCLE;
204 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
208 * USB 2.0 spec Section 11.24.2.6
210 static int get_hub_status(struct usb_device *dev,
211 struct usb_hub_status *data)
213 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
214 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
215 data, sizeof(*data), HZ * USB_CTRL_GET_TIMEOUT);
219 * USB 2.0 spec Section 11.24.2.7
221 static int get_port_status(struct usb_device *dev, int port,
222 struct usb_port_status *data)
224 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
225 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
226 data, sizeof(*data), HZ * USB_CTRL_GET_TIMEOUT);
229 /* completion function, fires on port status changes and various faults */
230 static void hub_irq(struct urb *urb, struct pt_regs *regs)
232 struct usb_hub *hub = (struct usb_hub *)urb->context;
235 spin_lock(&hub_event_lock);
237 if (hub->urb_complete) { /* disconnect or rmmod */
238 complete(hub->urb_complete);
242 switch (urb->status) {
243 case -ENOENT: /* synchronous unlink */
244 case -ECONNRESET: /* async unlink */
245 case -ESHUTDOWN: /* hardware going away */
248 default: /* presumably an error */
249 /* Cause a hub reset after 10 consecutive errors */
250 dev_dbg (&hub->intf->dev, "transfer --> %d\n", urb->status);
251 if ((++hub->nerrors < 10) || hub->error)
253 hub->error = urb->status;
256 /* let khubd handle things */
257 case 0: /* we got data: port status changed */
263 /* Something happened, let khubd figure it out */
264 if (list_empty(&hub->event_list)) {
265 list_add(&hub->event_list, &hub_event_list);
266 wake_up(&khubd_wait);
270 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
271 /* ENODEV means we raced disconnect() */
272 && status != -ENODEV)
273 dev_err (&hub->intf->dev, "resubmit --> %d\n", urb->status);
277 spin_unlock(&hub_event_lock);
280 /* USB 2.0 spec Section 11.24.2.3 */
282 hub_clear_tt_buffer (struct usb_device *hub, u16 devinfo, u16 tt)
284 return usb_control_msg (hub, usb_rcvctrlpipe (hub, 0),
285 HUB_CLEAR_TT_BUFFER, USB_RT_PORT,
286 devinfo, tt, 0, 0, HZ);
290 * enumeration blocks khubd for a long time. we use keventd instead, since
291 * long blocking there is the exception, not the rule. accordingly, HCDs
292 * talking to TTs must queue control transfers (not just bulk and iso), so
293 * both can talk to the same hub concurrently.
295 static void hub_tt_kevent (void *arg)
297 struct usb_hub *hub = arg;
300 spin_lock_irqsave (&hub->tt.lock, flags);
301 while (!list_empty (&hub->tt.clear_list)) {
302 struct list_head *temp;
303 struct usb_tt_clear *clear;
304 struct usb_device *dev;
307 temp = hub->tt.clear_list.next;
308 clear = list_entry (temp, struct usb_tt_clear, clear_list);
309 list_del (&clear->clear_list);
311 /* drop lock so HCD can concurrently report other TT errors */
312 spin_unlock_irqrestore (&hub->tt.lock, flags);
313 dev = interface_to_usbdev (hub->intf);
314 status = hub_clear_tt_buffer (dev, clear->devinfo, clear->tt);
315 spin_lock_irqsave (&hub->tt.lock, flags);
318 dev_err (&dev->dev, "clear tt %d (%04x) error %d\n",
319 clear->tt, clear->devinfo, status);
322 spin_unlock_irqrestore (&hub->tt.lock, flags);
326 * usb_hub_tt_clear_buffer - clear control/bulk TT state in high speed hub
327 * @dev: the device whose split transaction failed
328 * @pipe: identifies the endpoint of the failed transaction
330 * High speed HCDs use this to tell the hub driver that some split control or
331 * bulk transaction failed in a way that requires clearing internal state of
332 * a transaction translator. This is normally detected (and reported) from
335 * It may not be possible for that hub to handle additional full (or low)
336 * speed transactions until that state is fully cleared out.
338 void usb_hub_tt_clear_buffer (struct usb_device *dev, int pipe)
340 struct usb_tt *tt = dev->tt;
342 struct usb_tt_clear *clear;
344 /* we've got to cope with an arbitrary number of pending TT clears,
345 * since each TT has "at least two" buffers that can need it (and
346 * there can be many TTs per hub). even if they're uncommon.
348 if ((clear = kmalloc (sizeof *clear, SLAB_ATOMIC)) == 0) {
349 dev_err (&dev->dev, "can't save CLEAR_TT_BUFFER state\n");
350 /* FIXME recover somehow ... RESET_TT? */
354 /* info that CLEAR_TT_BUFFER needs */
355 clear->tt = tt->multi ? dev->ttport : 1;
356 clear->devinfo = usb_pipeendpoint (pipe);
357 clear->devinfo |= dev->devnum << 4;
358 clear->devinfo |= usb_pipecontrol (pipe)
359 ? (USB_ENDPOINT_XFER_CONTROL << 11)
360 : (USB_ENDPOINT_XFER_BULK << 11);
361 if (usb_pipein (pipe))
362 clear->devinfo |= 1 << 15;
364 /* tell keventd to clear state for this TT */
365 spin_lock_irqsave (&tt->lock, flags);
366 list_add_tail (&clear->clear_list, &tt->clear_list);
367 schedule_work (&tt->kevent);
368 spin_unlock_irqrestore (&tt->lock, flags);
371 static void hub_power_on(struct usb_hub *hub)
373 struct usb_device *dev;
376 /* Enable power to the ports */
377 dev_dbg(hubdev(interface_to_usbdev(hub->intf)),
378 "enabling power on all ports\n");
379 dev = interface_to_usbdev(hub->intf);
380 for (i = 0; i < hub->descriptor->bNbrPorts; i++)
381 set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
383 /* Wait for power to be enabled */
384 wait_ms(hub->descriptor->bPwrOn2PwrGood * 2);
387 static int hub_hub_status(struct usb_hub *hub,
388 u16 *status, u16 *change)
390 struct usb_device *dev = interface_to_usbdev (hub->intf);
393 ret = get_hub_status(dev, &hub->status->hub);
395 dev_err (hubdev (dev),
396 "%s failed (err = %d)\n", __FUNCTION__, ret);
398 *status = le16_to_cpu(hub->status->hub.wHubStatus);
399 *change = le16_to_cpu(hub->status->hub.wHubChange);
405 static int hub_configure(struct usb_hub *hub,
406 struct usb_endpoint_descriptor *endpoint)
408 struct usb_device *dev = interface_to_usbdev (hub->intf);
409 struct device *hub_dev;
410 u16 hubstatus, hubchange;
415 hub->buffer = usb_buffer_alloc(dev, sizeof(*hub->buffer), GFP_KERNEL,
418 message = "can't allocate hub irq buffer";
423 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
425 message = "can't kmalloc hub status buffer";
430 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
431 if (!hub->descriptor) {
432 message = "can't kmalloc hub descriptor";
437 /* Request the entire hub descriptor.
438 * hub->descriptor can handle USB_MAXCHILDREN ports,
439 * but the hub can/will return fewer bytes here.
441 ret = get_hub_descriptor(dev, hub->descriptor,
442 sizeof(*hub->descriptor));
444 message = "can't read hub descriptor";
446 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
447 message = "hub has too many ports!";
452 hub_dev = hubdev(dev);
453 dev->maxchild = hub->descriptor->bNbrPorts;
454 dev_info (hub_dev, "%d port%s detected\n", dev->maxchild,
455 (dev->maxchild == 1) ? "" : "s");
457 le16_to_cpus(&hub->descriptor->wHubCharacteristics);
459 if (hub->descriptor->wHubCharacteristics & HUB_CHAR_COMPOUND) {
461 char portstr [USB_MAXCHILDREN + 1];
463 for (i = 0; i < dev->maxchild; i++)
464 portstr[i] = hub->descriptor->DeviceRemovable
465 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
467 portstr[dev->maxchild] = 0;
468 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
470 dev_dbg(hub_dev, "standalone hub\n");
472 switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_LPSM) {
474 dev_dbg(hub_dev, "ganged power switching\n");
477 dev_dbg(hub_dev, "individual port power switching\n");
481 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
485 switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_OCPM) {
487 dev_dbg(hub_dev, "global over-current protection\n");
490 dev_dbg(hub_dev, "individual port over-current protection\n");
494 dev_dbg(hub_dev, "no over-current protection\n");
498 spin_lock_init (&hub->tt.lock);
499 INIT_LIST_HEAD (&hub->tt.clear_list);
500 INIT_WORK (&hub->tt.kevent, hub_tt_kevent, hub);
501 switch (dev->descriptor.bDeviceProtocol) {
505 dev_dbg(hub_dev, "Single TT\n");
509 ret = usb_set_interface(dev, 0, 1);
511 dev_dbg(hub_dev, "TT per port\n");
514 dev_err(hub_dev, "Using single TT (err %d)\n",
519 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
520 dev->descriptor.bDeviceProtocol);
524 switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_TTTT) {
526 if (dev->descriptor.bDeviceProtocol != 0)
527 dev_dbg(hub_dev, "TT requires at most 8 FS bit times\n");
530 dev_dbg(hub_dev, "TT requires at most 16 FS bit times\n");
533 dev_dbg(hub_dev, "TT requires at most 24 FS bit times\n");
536 dev_dbg(hub_dev, "TT requires at most 32 FS bit times\n");
540 /* probe() zeroes hub->indicator[] */
541 if (hub->descriptor->wHubCharacteristics & HUB_CHAR_PORTIND) {
542 hub->has_indicators = 1;
543 dev_dbg(hub_dev, "Port indicators are supported\n");
546 dev_dbg(hub_dev, "power on to power good time: %dms\n",
547 hub->descriptor->bPwrOn2PwrGood * 2);
548 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
549 hub->descriptor->bHubContrCurrent);
551 ret = hub_hub_status(hub, &hubstatus, &hubchange);
553 message = "can't get hub status";
557 /* FIXME implement per-port power budgeting;
558 * enable it for bus-powered hubs.
560 dev_dbg(hub_dev, "local power source is %s\n",
561 (hubstatus & HUB_STATUS_LOCAL_POWER)
562 ? "lost (inactive)" : "good");
564 if ((hub->descriptor->wHubCharacteristics & HUB_CHAR_OCPM) == 0)
565 dev_dbg(hub_dev, "%sover-current condition exists\n",
566 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
568 /* Start the interrupt endpoint */
569 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
570 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
572 if (maxp > sizeof(*hub->buffer))
573 maxp = sizeof(*hub->buffer);
575 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
577 message = "couldn't allocate interrupt urb";
582 usb_fill_int_urb(hub->urb, dev, pipe, *hub->buffer, maxp, hub_irq,
583 hub, endpoint->bInterval);
584 hub->urb->transfer_dma = hub->buffer_dma;
585 hub->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
586 ret = usb_submit_urb(hub->urb, GFP_KERNEL);
588 message = "couldn't submit status urb";
594 wake_up(&khubd_wait);
596 /* maybe start cycling the hub leds */
597 if (hub->has_indicators && blinkenlights) {
598 set_port_led(dev, hub, 1, HUB_LED_GREEN);
599 hub->indicator [0] = INDICATOR_CYCLE;
600 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
608 dev_err (&hub->intf->dev, "config failed, %s (err %d)\n",
610 /* hub_disconnect() frees urb and descriptor */
614 static void hub_disconnect(struct usb_interface *intf)
616 struct usb_hub *hub = usb_get_intfdata (intf);
617 DECLARE_COMPLETION(urb_complete);
623 usb_set_intfdata (intf, NULL);
624 spin_lock_irqsave(&hub_event_lock, flags);
625 hub->urb_complete = &urb_complete;
627 /* Delete it and then reset it */
628 list_del_init(&hub->event_list);
629 list_del_init(&hub->hub_list);
631 spin_unlock_irqrestore(&hub_event_lock, flags);
633 down(&hub->khubd_sem); /* Wait for khubd to leave this hub alone. */
636 /* assuming we used keventd, it must quiesce too */
637 if (hub->has_indicators)
638 cancel_delayed_work (&hub->leds);
639 if (hub->has_indicators || hub->tt.hub)
640 flush_scheduled_work ();
643 usb_unlink_urb(hub->urb);
645 wait_for_completion(&urb_complete);
646 usb_free_urb(hub->urb);
650 if (hub->descriptor) {
651 kfree(hub->descriptor);
652 hub->descriptor = NULL;
661 usb_buffer_free(interface_to_usbdev(intf),
662 sizeof(*hub->buffer), hub->buffer,
667 /* Free the memory */
671 static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
673 struct usb_host_interface *desc;
674 struct usb_endpoint_descriptor *endpoint;
675 struct usb_device *dev;
679 desc = intf->cur_altsetting;
680 dev = interface_to_usbdev(intf);
682 /* Some hubs have a subclass of 1, which AFAICT according to the */
683 /* specs is not defined, but it works */
684 if ((desc->desc.bInterfaceSubClass != 0) &&
685 (desc->desc.bInterfaceSubClass != 1)) {
687 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
691 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
692 if (desc->desc.bNumEndpoints != 1) {
693 goto descriptor_error;
696 endpoint = &desc->endpoint[0].desc;
698 /* Output endpoint? Curiouser and curiouser.. */
699 if (!(endpoint->bEndpointAddress & USB_DIR_IN)) {
700 goto descriptor_error;
703 /* If it's not an interrupt endpoint, we'd better punt! */
704 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
705 != USB_ENDPOINT_XFER_INT) {
706 goto descriptor_error;
711 dev_info (hubdev (dev), "USB hub found\n");
713 hub = kmalloc(sizeof(*hub), GFP_KERNEL);
715 dev_dbg (hubdev(dev), "couldn't kmalloc hub struct\n");
719 memset(hub, 0, sizeof(*hub));
721 INIT_LIST_HEAD(&hub->event_list);
723 init_MUTEX(&hub->khubd_sem);
724 INIT_WORK(&hub->leds, led_work, hub);
726 /* Record the new hub's existence */
727 spin_lock_irqsave(&hub_event_lock, flags);
728 INIT_LIST_HEAD(&hub->hub_list);
729 list_add(&hub->hub_list, &hub_list);
730 spin_unlock_irqrestore(&hub_event_lock, flags);
732 usb_set_intfdata (intf, hub);
734 if (hub_configure(hub, endpoint) >= 0)
737 hub_disconnect (intf);
742 hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
744 struct usb_device *hub = interface_to_usbdev (intf);
746 /* assert ifno == 0 (part of hub spec) */
748 case USBDEVFS_HUB_PORTINFO: {
749 struct usbdevfs_hub_portinfo *info = user_data;
753 spin_lock_irqsave(&hub_event_lock, flags);
754 if (hub->devnum <= 0)
757 info->nports = hub->maxchild;
758 for (i = 0; i < info->nports; i++) {
759 if (hub->children[i] == NULL)
763 hub->children[i]->devnum;
766 spin_unlock_irqrestore(&hub_event_lock, flags);
768 return info->nports + 1;
776 static int hub_reset(struct usb_hub *hub)
778 struct usb_device *dev = interface_to_usbdev(hub->intf);
781 /* Disconnect any attached devices */
782 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
783 if (dev->children[i])
784 usb_disconnect(&dev->children[i]);
787 /* Attempt to reset the hub */
789 usb_unlink_urb(hub->urb);
793 if (usb_reset_device(dev))
797 if (usb_submit_urb(hub->urb, GFP_KERNEL))
805 static void hub_start_disconnect(struct usb_device *dev)
807 struct usb_device *parent = dev->parent;
810 /* Find the device pointer to disconnect */
812 for (i = 0; i < parent->maxchild; i++) {
813 if (parent->children[i] == dev) {
814 usb_disconnect(&parent->children[i]);
820 dev_err(&dev->dev, "cannot disconnect hub!\n");
823 static int hub_port_status(struct usb_device *dev, int port,
824 u16 *status, u16 *change)
826 struct usb_hub *hub = usb_get_intfdata(dev->actconfig->interface[0]);
832 ret = get_port_status(dev, port + 1, &hub->status->port);
834 dev_err (hubdev (dev),
835 "%s failed (err = %d)\n", __FUNCTION__, ret);
837 *status = le16_to_cpu(hub->status->port.wPortStatus);
838 *change = le16_to_cpu(hub->status->port.wPortChange);
844 #define HUB_RESET_TRIES 5
845 #define HUB_PROBE_TRIES 2
846 #define HUB_ROOT_RESET_TIME 50 /* times are in msec */
847 #define HUB_SHORT_RESET_TIME 10
848 #define HUB_LONG_RESET_TIME 200
849 #define HUB_RESET_TIMEOUT 500
851 /* return: -1 on error, 0 on success, 1 on disconnect. */
852 static int hub_port_wait_reset(struct usb_device *hub, int port,
853 struct usb_device *dev, unsigned int delay)
860 delay_time < HUB_RESET_TIMEOUT;
861 delay_time += delay) {
862 /* wait to give the device a chance to reset */
865 /* read and decode port status */
866 ret = hub_port_status(hub, port, &portstatus, &portchange);
871 /* Device went away? */
872 if (!(portstatus & USB_PORT_STAT_CONNECTION))
875 /* bomb out completely if something weird happened */
876 if ((portchange & USB_PORT_STAT_C_CONNECTION))
879 /* if we`ve finished resetting, then break out of the loop */
880 if (!(portstatus & USB_PORT_STAT_RESET) &&
881 (portstatus & USB_PORT_STAT_ENABLE)) {
882 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
883 dev->speed = USB_SPEED_HIGH;
884 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
885 dev->speed = USB_SPEED_LOW;
887 dev->speed = USB_SPEED_FULL;
891 /* switch to the long delay after two short delay failures */
892 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
893 delay = HUB_LONG_RESET_TIME;
895 dev_dbg (hubdev (hub),
896 "port %d not reset yet, waiting %dms\n",
903 /* return: -1 on error, 0 on success, 1 on disconnect. */
904 static int hub_port_reset(struct usb_device *hub, int port,
905 struct usb_device *dev, unsigned int delay)
910 for (i = 0; i < HUB_RESET_TRIES; i++) {
911 set_port_feature(hub, port + 1, USB_PORT_FEAT_RESET);
913 /* return on disconnect or reset */
914 status = hub_port_wait_reset(hub, port, dev, delay);
916 clear_port_feature(hub,
917 port + 1, USB_PORT_FEAT_C_RESET);
919 ? USB_STATE_NOTATTACHED
924 dev_dbg (hubdev (hub),
925 "port %d not enabled, trying reset again...\n",
927 delay = HUB_LONG_RESET_TIME;
930 dev_err (hubdev (hub),
931 "Cannot enable port %i. Maybe the USB cable is bad?\n",
937 int hub_port_disable(struct usb_device *hub, int port)
941 ret = clear_port_feature(hub, port + 1, USB_PORT_FEAT_ENABLE);
943 dev_err(hubdev(hub), "cannot disable port %d (err = %d)\n",
949 /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
951 * Between connect detection and reset signaling there must be a delay
952 * of 100ms at least for debounce and power-settling. The corresponding
953 * timer shall restart whenever the downstream port detects a disconnect.
955 * Apparently there are some bluetooth and irda-dongles and a number
956 * of low-speed devices which require longer delays of about 200-400ms.
957 * Not covered by the spec - but easy to deal with.
959 * This implementation uses 400ms minimum debounce timeout and checks
960 * every 25ms for transient disconnects to restart the delay.
963 #define HUB_DEBOUNCE_TIMEOUT 400
964 #define HUB_DEBOUNCE_STEP 25
965 #define HUB_DEBOUNCE_STABLE 4
967 /* return: -1 on error, 0 on success, 1 on disconnect. */
968 static int hub_port_debounce(struct usb_device *hub, int port)
971 int delay_time, stable_count;
972 u16 portchange, portstatus;
977 for (delay_time = 0; delay_time < HUB_DEBOUNCE_TIMEOUT; delay_time += HUB_DEBOUNCE_STEP) {
978 wait_ms(HUB_DEBOUNCE_STEP);
980 ret = hub_port_status(hub, port, &portstatus, &portchange);
984 if ((portstatus & USB_PORT_STAT_CONNECTION) == connection) {
986 if (++stable_count == HUB_DEBOUNCE_STABLE)
992 connection = portstatus & USB_PORT_STAT_CONNECTION;
994 if ((portchange & USB_PORT_STAT_C_CONNECTION)) {
995 clear_port_feature(hub, port+1, USB_PORT_FEAT_C_CONNECTION);
999 dev_dbg (hubdev (hub),
1000 "debounce: port %d: delay %dms stable %d status 0x%x\n",
1001 port + 1, delay_time, stable_count, portstatus);
1003 return ((portstatus&USB_PORT_STAT_CONNECTION)) ? 0 : 1;
1006 static void hub_port_connect_change(struct usb_hub *hubstate, int port,
1007 u16 portstatus, u16 portchange)
1009 struct usb_device *hub = interface_to_usbdev(hubstate->intf);
1010 struct usb_device *dev;
1011 unsigned int delay = HUB_SHORT_RESET_TIME;
1014 dev_dbg (&hubstate->intf->dev,
1015 "port %d, status %x, change %x, %s\n",
1016 port + 1, portstatus, portchange, portspeed (portstatus));
1018 /* Clear the connection change status */
1019 clear_port_feature(hub, port + 1, USB_PORT_FEAT_C_CONNECTION);
1021 /* Disconnect any existing devices under this port */
1022 if (hub->children[port])
1023 usb_disconnect(&hub->children[port]);
1025 /* Return now if nothing is connected */
1026 if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
1027 if (portstatus & USB_PORT_STAT_ENABLE)
1028 hub_port_disable(hub, port);
1033 if (hub_port_debounce(hub, port)) {
1034 dev_err (&hubstate->intf->dev,
1035 "connect-debounce failed, port %d disabled\n",
1037 hub_port_disable(hub, port);
1041 /* root hub ports have a slightly longer reset period
1042 * (from USB 2.0 spec, section 7.1.7.5)
1045 delay = HUB_ROOT_RESET_TIME;
1047 /* Some low speed devices have problems with the quick delay, so */
1048 /* be a bit pessimistic with those devices. RHbug #23670 */
1049 if (portstatus & USB_PORT_STAT_LOW_SPEED)
1050 delay = HUB_LONG_RESET_TIME;
1052 down(&usb_address0_sem);
1054 for (i = 0; i < HUB_PROBE_TRIES; i++) {
1056 /* Allocate a new device struct */
1057 dev = usb_alloc_dev(hub, hub->bus, port);
1059 dev_err (&hubstate->intf->dev,
1060 "couldn't allocate usb_device\n");
1064 dev->state = USB_STATE_POWERED;
1066 /* Reset the device, and detect its speed */
1067 if (hub_port_reset(hub, port, dev, delay)) {
1072 /* Find a new address for it */
1073 usb_choose_address(dev);
1075 /* Set up TT records, if needed */
1078 dev->ttport = hub->ttport;
1079 } else if (dev->speed != USB_SPEED_HIGH
1080 && hub->speed == USB_SPEED_HIGH) {
1081 dev->tt = &hubstate->tt;
1082 dev->ttport = port + 1;
1085 dev_info (&dev->dev,
1086 "new %s speed USB device using address %d\n",
1087 ({ char *speed; switch (dev->speed) {
1088 case USB_SPEED_LOW: speed = "low"; break;
1089 case USB_SPEED_FULL: speed = "full"; break;
1090 case USB_SPEED_HIGH: speed = "high"; break;
1091 default: speed = "?"; break;
1095 /* Run it through the hoops (find a driver, etc) */
1096 if (usb_new_device(dev) == 0) {
1097 hub->children[port] = dev;
1101 /* Free the configuration if there was an error */
1104 /* Switch to a long reset time */
1105 delay = HUB_LONG_RESET_TIME;
1108 hub_port_disable(hub, port);
1110 up(&usb_address0_sem);
1113 static void hub_events(void)
1115 unsigned long flags;
1116 struct list_head *tmp;
1117 struct usb_device *dev;
1118 struct usb_hub *hub;
1126 * We restart the list every time to avoid a deadlock with
1127 * deleting hubs downstream from this one. This should be
1128 * safe since we delete the hub from the event list.
1129 * Not the most efficient, but avoids deadlocks.
1132 spin_lock_irqsave(&hub_event_lock, flags);
1134 if (list_empty(&hub_event_list))
1137 /* Grab the next entry from the beginning of the list */
1138 tmp = hub_event_list.next;
1140 hub = list_entry(tmp, struct usb_hub, event_list);
1141 dev = interface_to_usbdev(hub->intf);
1145 if (unlikely(down_trylock(&hub->khubd_sem)))
1146 BUG(); /* never blocks, we were on list */
1148 spin_unlock_irqrestore(&hub_event_lock, flags);
1151 dev_dbg (&hub->intf->dev, "resetting for error %d\n",
1154 if (hub_reset(hub)) {
1155 dev_dbg (&hub->intf->dev,
1156 "can't reset; disconnecting\n");
1157 up(&hub->khubd_sem);
1158 hub_start_disconnect(dev);
1166 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
1167 ret = hub_port_status(dev, i, &portstatus, &portchange);
1172 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1173 hub_port_connect_change(hub, i, portstatus, portchange);
1174 } else if (portchange & USB_PORT_STAT_C_ENABLE) {
1175 dev_dbg (hubdev (dev),
1176 "port %d enable change, status %x\n",
1178 clear_port_feature(dev,
1179 i + 1, USB_PORT_FEAT_C_ENABLE);
1182 * EM interference sometimes causes badly
1183 * shielded USB devices to be shutdown by
1184 * the hub, this hack enables them again.
1185 * Works at least with mouse driver.
1187 if (!(portstatus & USB_PORT_STAT_ENABLE)
1188 && (portstatus & USB_PORT_STAT_CONNECTION)
1189 && (dev->children[i])) {
1190 dev_err (&hub->intf->dev,
1192 "disabled by hub (EMI?), "
1195 hub_port_connect_change(hub,
1196 i, portstatus, portchange);
1200 if (portchange & USB_PORT_STAT_C_SUSPEND) {
1201 dev_dbg (&hub->intf->dev,
1202 "suspend change on port %d\n",
1204 clear_port_feature(dev,
1205 i + 1, USB_PORT_FEAT_C_SUSPEND);
1208 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
1209 dev_err (&hub->intf->dev,
1210 "over-current change on port %d\n",
1212 clear_port_feature(dev,
1213 i + 1, USB_PORT_FEAT_C_OVER_CURRENT);
1217 if (portchange & USB_PORT_STAT_C_RESET) {
1218 dev_dbg (&hub->intf->dev,
1219 "reset change on port %d\n",
1221 clear_port_feature(dev,
1222 i + 1, USB_PORT_FEAT_C_RESET);
1226 /* deal with hub status changes */
1227 if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
1228 dev_err (&hub->intf->dev, "get_hub_status failed\n");
1230 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
1231 dev_dbg (&hub->intf->dev, "power change\n");
1232 clear_hub_feature(dev, C_HUB_LOCAL_POWER);
1234 if (hubchange & HUB_CHANGE_OVERCURRENT) {
1235 dev_dbg (&hub->intf->dev, "overcurrent change\n");
1236 wait_ms(500); /* Cool down */
1237 clear_hub_feature(dev, C_HUB_OVER_CURRENT);
1241 up(&hub->khubd_sem);
1242 } /* end while (1) */
1244 spin_unlock_irqrestore(&hub_event_lock, flags);
1247 static int hub_thread(void *__hub)
1250 * This thread doesn't need any user-level access,
1251 * so get rid of all our resources
1255 allow_signal(SIGKILL);
1257 /* Send me a signal to get me die (for debugging) */
1260 wait_event_interruptible(khubd_wait, !list_empty(&hub_event_list));
1261 if (current->flags & PF_FREEZE)
1262 refrigerator(PF_FREEZE);
1263 } while (!signal_pending(current));
1265 pr_debug ("%s: khubd exiting\n", usbcore_name);
1266 complete_and_exit(&khubd_exited, 0);
1269 static struct usb_device_id hub_id_table [] = {
1270 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
1271 .bDeviceClass = USB_CLASS_HUB},
1272 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1273 .bInterfaceClass = USB_CLASS_HUB},
1274 { } /* Terminating entry */
1277 MODULE_DEVICE_TABLE (usb, hub_id_table);
1279 static struct usb_driver hub_driver = {
1280 .owner = THIS_MODULE,
1283 .disconnect = hub_disconnect,
1285 .id_table = hub_id_table,
1289 * This should be a separate module.
1291 int usb_hub_init(void)
1295 if (usb_register(&hub_driver) < 0) {
1296 printk(KERN_ERR "%s: can't register hub driver\n",
1301 pid = kernel_thread(hub_thread, NULL, CLONE_KERNEL);
1308 /* Fall through if kernel_thread failed */
1309 usb_deregister(&hub_driver);
1310 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
1315 void usb_hub_cleanup(void)
1319 /* Kill the thread */
1320 ret = kill_proc(khubd_pid, SIGKILL, 1);
1322 wait_for_completion(&khubd_exited);
1325 * Hub resources are freed for us by usb_deregister. It calls
1326 * usb_driver_purge on every device which in turn calls that
1327 * devices disconnect function if it is using this driver.
1328 * The hub_disconnect function takes care of releasing the
1329 * individual hub resources. -greg
1331 usb_deregister(&hub_driver);
1332 } /* usb_hub_cleanup() */
1335 * WARNING - If a driver calls usb_reset_device, you should simulate a
1336 * disconnect() and probe() for other interfaces you doesn't claim. This
1337 * is left up to the driver writer right now. This insures other drivers
1338 * have a chance to re-setup their interface.
1340 * Take a look at proc_resetdevice in devio.c for some sample code to
1342 * Use this only from within your probe function, otherwise use
1343 * usb_reset_device() below, which ensure proper locking
1345 int usb_physical_reset_device(struct usb_device *dev)
1347 struct usb_device *parent = dev->parent;
1348 struct usb_device_descriptor *descriptor;
1349 int i, ret, port = -1;
1352 err("attempting to reset root hub!");
1356 for (i = 0; i < parent->maxchild; i++)
1357 if (parent->children[i] == dev) {
1365 descriptor = kmalloc(sizeof *descriptor, GFP_NOIO);
1370 down(&usb_address0_sem);
1372 /* Send a reset to the device */
1373 if (hub_port_reset(parent, port, dev, HUB_SHORT_RESET_TIME)) {
1374 hub_port_disable(parent, port);
1375 up(&usb_address0_sem);
1380 /* Reprogram the Address */
1381 ret = usb_set_address(dev);
1383 err("USB device not accepting new address (error=%d)", ret);
1384 hub_port_disable(parent, port);
1385 up(&usb_address0_sem);
1390 /* Let the SET_ADDRESS settle */
1393 up(&usb_address0_sem);
1396 * Now we fetch the configuration descriptors for the device and
1397 * see if anything has changed. If it has, we dump the current
1398 * parsed descriptors and reparse from scratch. Then we leave
1399 * the device alone for the caller to finish setting up.
1401 * If nothing changed, we reprogram the configuration and then
1402 * the alternate settings.
1405 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, descriptor,
1406 sizeof(*descriptor));
1412 le16_to_cpus(&descriptor->bcdUSB);
1413 le16_to_cpus(&descriptor->idVendor);
1414 le16_to_cpus(&descriptor->idProduct);
1415 le16_to_cpus(&descriptor->bcdDevice);
1417 if (memcmp(&dev->descriptor, descriptor, sizeof(*descriptor))) {
1419 usb_destroy_configuration(dev);
1421 /* FIXME Linux doesn't yet handle these "device morphed"
1422 * paths. DFU variants need this to work ... and they
1423 * include the "config descriptors changed" case this
1424 * doesn't yet detect!
1426 dev->state = USB_STATE_NOTATTACHED;
1427 dev_err(&dev->dev, "device morphed (DFU?), nyet supported\n");
1434 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1435 USB_REQ_SET_CONFIGURATION, 0,
1436 dev->actconfig->desc.bConfigurationValue, 0,
1437 NULL, 0, HZ * USB_CTRL_SET_TIMEOUT);
1439 err("failed to set dev %s active configuration (error=%d)",
1443 dev->state = USB_STATE_CONFIGURED;
1445 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1446 struct usb_interface *intf = dev->actconfig->interface[i];
1447 struct usb_interface_descriptor *desc;
1449 desc = &intf->cur_altsetting->desc;
1450 ret = usb_set_interface(dev, desc->bInterfaceNumber,
1451 desc->bAlternateSetting);
1453 err("failed to set active alternate setting "
1454 "for dev %s interface %d (error=%d)",
1455 dev->devpath, desc->bInterfaceNumber, ret);
1463 int usb_reset_device(struct usb_device *udev)
1465 struct device *gdev = &udev->dev;
1468 down_read(&gdev->bus->subsys.rwsem);
1469 r = usb_physical_reset_device(udev);
1470 up_read(&gdev->bus->subsys.rwsem);