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/module.h>
14 #include <linux/completion.h>
15 #include <linux/sched.h>
16 #include <linux/list.h>
17 #include <linux/slab.h>
18 #include <linux/smp_lock.h>
19 #ifdef CONFIG_USB_DEBUG
24 #include <linux/usb.h>
25 #include <linux/usbdevice_fs.h>
27 #include <asm/semaphore.h>
28 #include <asm/uaccess.h>
29 #include <asm/byteorder.h>
34 static spinlock_t hub_event_lock = SPIN_LOCK_UNLOCKED;
35 static DECLARE_MUTEX(usb_address0_sem);
37 static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
38 static LIST_HEAD(hub_list); /* List containing all of the hubs (for cleanup) */
40 static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
41 static pid_t khubd_pid = 0; /* PID of khubd */
42 static DECLARE_COMPLETION(khubd_exited);
45 extern void led_status(int);
54 static inline char *portspeed (int portstatus)
56 if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
58 else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
65 /* USB 2.0 spec Section 11.24.4.5 */
66 static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
68 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
69 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
70 USB_DT_HUB << 8, 0, data, size, HZ);
74 * USB 2.0 spec Section 11.24.2.1
76 static int usb_clear_hub_feature(struct usb_device *dev, int feature)
78 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
79 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, HZ);
83 * USB 2.0 spec Section 11.24.2.2
84 * BUG: doesn't handle port indicator selector in high byte of wIndex
86 static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
88 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
89 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port, NULL, 0, HZ);
93 * USB 2.0 spec Section 11.24.2.13
94 * BUG: doesn't handle port indicator selector in high byte of wIndex
96 static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
98 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
99 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port, NULL, 0, HZ);
103 * USB 2.0 spec Section 11.24.2.6
105 static int usb_get_hub_status(struct usb_device *dev, void *data)
107 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
108 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
109 data, sizeof(struct usb_hub_status), HZ);
113 * USB 2.0 spec Section 11.24.2.7
115 static int usb_get_port_status(struct usb_device *dev, int port, void *data)
117 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
118 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
119 data, sizeof(struct usb_hub_status), HZ);
122 static void hub_irq(struct urb *urb)
124 struct usb_hub *hub = (struct usb_hub *)urb->context;
127 /* Cause a hub reset after 10 consecutive errors */
129 if (urb->status == -ENOENT)
132 dbg("nonzero status in irq %d", urb->status);
134 if ((++hub->nerrors < 10) || hub->error)
137 hub->error = urb->status;
142 /* Something happened, let khubd figure it out */
143 spin_lock_irqsave(&hub_event_lock, flags);
144 if (list_empty(&hub->event_list)) {
145 list_add(&hub->event_list, &hub_event_list);
146 wake_up(&khubd_wait);
148 spin_unlock_irqrestore(&hub_event_lock, flags);
151 static void usb_hub_power_on(struct usb_hub *hub)
155 /* Enable power to the ports */
156 dbg("enabling power on all ports");
157 for (i = 0; i < hub->descriptor->bNbrPorts; i++)
158 usb_set_port_feature(hub->dev, i + 1, USB_PORT_FEAT_POWER);
160 /* Wait for power to be enabled */
161 wait_ms(hub->descriptor->bPwrOn2PwrGood * 2);
164 static int usb_hub_configure(struct usb_hub *hub, struct usb_endpoint_descriptor *endpoint)
166 struct usb_device *dev = hub->dev;
167 struct usb_hub_status *hubstatus;
168 char portstr[USB_MAXCHILDREN + 1];
172 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
173 if (!hub->descriptor) {
174 err("Unable to kmalloc %Zd bytes for hub descriptor", sizeof(*hub->descriptor));
178 /* Request the entire hub descriptor. */
179 ret = usb_get_hub_descriptor(dev, hub->descriptor, sizeof(*hub->descriptor));
180 /* <hub->descriptor> is large enough for a hub with 127 ports;
181 * the hub can/will return fewer bytes here. */
183 err("Unable to get hub descriptor (err = %d)", ret);
184 kfree(hub->descriptor);
188 dev->maxchild = hub->descriptor->bNbrPorts;
189 info("%d port%s detected", hub->descriptor->bNbrPorts, (hub->descriptor->bNbrPorts == 1) ? "" : "s");
192 /* Indicates that a usb hub is being detected, so the LED_RED of the led status is invalid now */
193 //led_status(LED_HUB);
197 le16_to_cpus(&hub->descriptor->wHubCharacteristics);
199 if (hub->descriptor->wHubCharacteristics & HUB_CHAR_COMPOUND)
200 dbg("part of a compound device");
202 dbg("standalone hub");
204 switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_LPSM) {
206 dbg("ganged power switching");
209 dbg("individual port power switching");
213 dbg("unknown reserved power switching mode");
217 switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_OCPM) {
219 dbg("global over-current protection");
222 dbg("individual port over-current protection");
226 dbg("no over-current protection");
230 switch (dev->descriptor.bDeviceProtocol) {
243 dbg("Unrecognized hub protocol %d",
244 dev->descriptor.bDeviceProtocol);
248 switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_TTTT) {
250 if (dev->descriptor.bDeviceProtocol != 0)
251 dbg("TT requires at most 8 FS bit times");
254 dbg("TT requires at most 16 FS bit times");
257 dbg("TT requires at most 24 FS bit times");
260 dbg("TT requires at most 32 FS bit times");
264 dbg("Port indicators are %s supported",
265 (hub->descriptor->wHubCharacteristics & HUB_CHAR_PORTIND) ? "" : "not");
267 dbg("power on to power good time: %dms", hub->descriptor->bPwrOn2PwrGood * 2);
268 dbg("hub controller current requirement: %dmA", hub->descriptor->bHubContrCurrent);
270 for (i = 0; i < dev->maxchild; i++)
271 portstr[i] = hub->descriptor->DeviceRemovable[((i + 1) / 8)] & (1 << ((i + 1) % 8)) ? 'F' : 'R';
272 portstr[dev->maxchild] = 0;
274 dbg("port removable status: %s", portstr);
276 hubstatus = kmalloc(sizeof *hubstatus, GFP_KERNEL);
278 err("Unable to allocate hubstatus");
279 kfree(hub->descriptor);
282 ret = usb_get_hub_status(dev, hubstatus);
284 err("Unable to get hub status (err = %d)", ret);
286 kfree(hub->descriptor);
290 le16_to_cpus(&hubstatus->wHubStatus);
292 dbg("local power source is %s",
293 (hubstatus->wHubStatus & HUB_STATUS_LOCAL_POWER) ? "lost (inactive)" : "good");
295 dbg("%sover-current condition exists",
296 (hubstatus->wHubStatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
300 /* Start the interrupt endpoint */
301 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
302 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
304 if (maxp > sizeof(hub->buffer))
305 maxp = sizeof(hub->buffer);
307 hub->urb = usb_alloc_urb(0);
309 err("couldn't allocate interrupt urb");
310 kfree(hub->descriptor);
314 FILL_INT_URB(hub->urb, dev, pipe, hub->buffer, maxp, hub_irq, hub,
315 /* NOTE: in 2.5 fill_int_urb() converts the encoding */
316 (dev->speed == USB_SPEED_HIGH)
317 ? 1 << (endpoint->bInterval - 1)
318 : endpoint->bInterval);
319 ret = usb_submit_urb(hub->urb);
321 err("usb_submit_urb failed (%d)", ret);
322 kfree(hub->descriptor);
327 wake_up(&khubd_wait);
329 usb_hub_power_on(hub);
334 static void *hub_probe(struct usb_device *dev, unsigned int i,
335 const struct usb_device_id *id)
337 struct usb_interface_descriptor *interface;
338 struct usb_endpoint_descriptor *endpoint;
342 interface = &dev->actconfig->interface[i].altsetting[0];
344 /* Some hubs have a subclass of 1, which AFAICT according to the */
345 /* specs is not defined, but it works */
346 if ((interface->bInterfaceSubClass != 0) &&
347 (interface->bInterfaceSubClass != 1)) {
348 err("invalid subclass (%d) for USB hub device #%d",
349 interface->bInterfaceSubClass, dev->devnum);
353 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
354 if (interface->bNumEndpoints != 1) {
355 err("invalid bNumEndpoints (%d) for USB hub device #%d",
356 interface->bNumEndpoints, dev->devnum);
360 endpoint = &interface->endpoint[0];
362 /* Output endpoint? Curiousier and curiousier.. */
363 if (!(endpoint->bEndpointAddress & USB_DIR_IN)) {
364 err("Device #%d is hub class, but has output endpoint?",
369 /* If it's not an interrupt endpoint, we'd better punt! */
370 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) {
371 err("Device #%d is hub class, but has endpoint other than interrupt?",
377 info("USB hub found");
379 hub = kmalloc(sizeof(*hub), GFP_KERNEL);
381 err("couldn't kmalloc hub struct");
385 memset(hub, 0, sizeof(*hub));
387 INIT_LIST_HEAD(&hub->event_list);
389 init_MUTEX(&hub->khubd_sem);
391 /* Record the new hub's existence */
392 spin_lock_irqsave(&hub_event_lock, flags);
393 INIT_LIST_HEAD(&hub->hub_list);
394 list_add(&hub->hub_list, &hub_list);
395 spin_unlock_irqrestore(&hub_event_lock, flags);
397 if (usb_hub_configure(hub, endpoint) >= 0)
400 err("hub configuration failed for device #%d", dev->devnum);
402 /* free hub, but first clean up its list. */
403 spin_lock_irqsave(&hub_event_lock, flags);
405 /* Delete it and then reset it */
406 list_del(&hub->event_list);
407 INIT_LIST_HEAD(&hub->event_list);
408 list_del(&hub->hub_list);
409 INIT_LIST_HEAD(&hub->hub_list);
411 spin_unlock_irqrestore(&hub_event_lock, flags);
418 static void hub_disconnect(struct usb_device *dev, void *ptr)
420 struct usb_hub *hub = (struct usb_hub *)ptr;
423 spin_lock_irqsave(&hub_event_lock, flags);
425 /* Delete it and then reset it */
426 list_del(&hub->event_list);
427 INIT_LIST_HEAD(&hub->event_list);
428 list_del(&hub->hub_list);
429 INIT_LIST_HEAD(&hub->hub_list);
431 spin_unlock_irqrestore(&hub_event_lock, flags);
433 down(&hub->khubd_sem); /* Wait for khubd to leave this hub alone. */
437 usb_unlink_urb(hub->urb);
438 usb_free_urb(hub->urb);
442 if (hub->descriptor) {
443 kfree(hub->descriptor);
444 hub->descriptor = NULL;
447 /* Free the memory */
451 static int hub_ioctl(struct usb_device *hub, unsigned int code, void *user_data)
453 /* assert ifno == 0 (part of hub spec) */
455 case USBDEVFS_HUB_PORTINFO: {
456 struct usbdevfs_hub_portinfo *info = user_data;
460 spin_lock_irqsave(&hub_event_lock, flags);
461 if (hub->devnum <= 0)
464 info->nports = hub->maxchild;
465 for (i = 0; i < info->nports; i++) {
466 if (hub->children[i] == NULL)
469 info->port[i] = hub->children[i]->devnum;
472 spin_unlock_irqrestore(&hub_event_lock, flags);
474 return info->nports + 1;
482 static int usb_hub_reset(struct usb_hub *hub)
484 struct usb_device *dev = hub->dev;
487 /* Disconnect any attached devices */
488 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
489 if (dev->children[i])
490 usb_disconnect(&dev->children[i]);
493 /* Attempt to reset the hub */
495 usb_unlink_urb(hub->urb);
499 if (usb_reset_device(dev))
503 if (usb_submit_urb(hub->urb))
506 usb_hub_power_on(hub);
511 static void usb_hub_disconnect(struct usb_device *dev)
513 struct usb_device *parent = dev->parent;
516 /* Find the device pointer to disconnect */
518 for (i = 0; i < parent->maxchild; i++) {
519 if (parent->children[i] == dev) {
520 usb_disconnect(&parent->children[i]);
526 err("cannot disconnect hub %d", dev->devnum);
529 static int usb_hub_port_status(struct usb_device *hub, int port,
530 u16 *status, u16 *change)
532 struct usb_port_status *portsts;
535 portsts = kmalloc(sizeof(*portsts), GFP_KERNEL);
537 ret = usb_get_port_status(hub, port + 1, portsts);
539 err("%s (%d) failed (err = %d)", __FUNCTION__, hub->devnum, ret);
541 *status = le16_to_cpu(portsts->wPortStatus);
542 *change = le16_to_cpu(portsts->wPortChange);
543 dbg("port %d, portstatus %x, change %x, %s", port + 1,
544 *status, *change, portspeed(*status));
552 #define HUB_RESET_TRIES 5
553 #define HUB_PROBE_TRIES 2
554 #define HUB_SHORT_RESET_TIME 10
555 #define HUB_LONG_RESET_TIME 200
556 #define HUB_RESET_TIMEOUT 500
558 /* return: -1 on error, 0 on success, 1 on disconnect. */
559 static int usb_hub_port_wait_reset(struct usb_device *hub, int port,
560 struct usb_device *dev, unsigned int delay)
566 for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT; delay_time += delay) {
567 /* wait to give the device a chance to reset */
570 /* read and decode port status */
571 ret = usb_hub_port_status(hub, port, &portstatus, &portchange);
576 /* Device went away? */
577 if (!(portstatus & USB_PORT_STAT_CONNECTION))
580 /* bomb out completely if something weird happened */
581 if ((portchange & USB_PORT_STAT_C_CONNECTION))
584 /* if we`ve finished resetting, then break out of the loop */
585 if (!(portstatus & USB_PORT_STAT_RESET) &&
586 (portstatus & USB_PORT_STAT_ENABLE)) {
587 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
588 dev->speed = USB_SPEED_HIGH;
589 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
590 dev->speed = USB_SPEED_LOW;
592 dev->speed = USB_SPEED_FULL;
596 /* switch to the long delay after two short delay failures */
597 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
598 delay = HUB_LONG_RESET_TIME;
600 dbg("port %d of hub %d not reset yet, waiting %dms", port + 1,
607 /* return: -1 on error, 0 on success, 1 on disconnect. */
608 static int usb_hub_port_reset(struct usb_device *hub, int port,
609 struct usb_device *dev, unsigned int delay)
614 for (i = 0; i < HUB_RESET_TRIES; i++) {
615 usb_set_port_feature(hub, port + 1, USB_PORT_FEAT_RESET);
617 /* return on disconnect or reset */
618 status = usb_hub_port_wait_reset(hub, port, dev, delay);
620 usb_clear_port_feature(hub, port + 1, USB_PORT_FEAT_C_RESET);
624 dbg("port %d of hub %d not enabled, trying reset again...",
625 port + 1, hub->devnum);
626 delay = HUB_LONG_RESET_TIME;
629 err("Cannot enable port %i of hub %d, disabling port.",
630 port + 1, hub->devnum);
631 err("Maybe the USB cable is bad?");
636 void usb_hub_port_disable(struct usb_device *hub, int port)
640 ret = usb_clear_port_feature(hub, port + 1, USB_PORT_FEAT_ENABLE);
642 err("cannot disable port %d of hub %d (err = %d)",
643 port + 1, hub->devnum, ret);
646 /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
648 * Between connect detection and reset signaling there must be a delay
649 * of 100ms at least for debounce and power-settling. The corresponding
650 * timer shall restart whenever the downstream port detects a disconnect.
652 * Apparently there are some bluetooth and irda-dongles and a number
653 * of low-speed devices which require longer delays of about 200-400ms.
654 * Not covered by the spec - but easy to deal with.
656 * This implementation uses 400ms minimum debounce timeout and checks
657 * every 100ms for transient disconnects to restart the delay.
660 #define HUB_DEBOUNCE_TIMEOUT 400
661 #define HUB_DEBOUNCE_STEP 100
663 /* return: -1 on error, 0 on success, 1 on disconnect. */
664 static int usb_hub_port_debounce(struct usb_device *hub, int port)
668 u16 portchange, portstatus;
670 for (delay_time = 0; delay_time < HUB_DEBOUNCE_TIMEOUT; /* empty */ ) {
672 /* wait debounce step increment */
673 wait_ms(HUB_DEBOUNCE_STEP);
675 ret = usb_hub_port_status(hub, port, &portstatus, &portchange);
679 if ((portchange & USB_PORT_STAT_C_CONNECTION)) {
680 usb_clear_port_feature(hub, port+1, USB_PORT_FEAT_C_CONNECTION);
684 delay_time += HUB_DEBOUNCE_STEP;
686 return ((portstatus&USB_PORT_STAT_CONNECTION)) ? 0 : 1;
689 static void usb_hub_port_connect_change(struct usb_hub *hubstate, int port,
690 u16 portstatus, u16 portchange)
692 struct usb_device *hub = hubstate->dev;
693 struct usb_device *dev;
694 unsigned int delay = HUB_SHORT_RESET_TIME;
697 dbg("port %d, portstatus %x, change %x, %s",
698 port + 1, portstatus, portchange, portspeed (portstatus));
700 /* Clear the connection change status */
701 usb_clear_port_feature(hub, port + 1, USB_PORT_FEAT_C_CONNECTION);
703 /* Disconnect any existing devices under this port */
704 if (hub->children[port])
705 usb_disconnect(&hub->children[port]);
707 /* Return now if nothing is connected */
708 if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
709 if (portstatus & USB_PORT_STAT_ENABLE)
710 usb_hub_port_disable(hub, port);
715 if (usb_hub_port_debounce(hub, port)) {
716 err("connect-debounce failed, port %d disabled", port+1);
717 usb_hub_port_disable(hub, port);
721 down(&usb_address0_sem);
723 for (i = 0; i < HUB_PROBE_TRIES; i++) {
724 struct usb_device *pdev;
727 /* Allocate a new device struct */
728 dev = usb_alloc_dev(hub, hub->bus);
730 err("couldn't allocate usb_device");
734 /* Reset the device */
735 if (usb_hub_port_reset(hub, port, dev, delay)) {
740 /* Find a new device ID for it */
743 /* Set up TT records, if needed */
746 dev->ttport = hub->ttport;
747 } else if (dev->speed != USB_SPEED_HIGH
748 && hub->speed == USB_SPEED_HIGH) {
749 dev->tt = &hubstate->tt;
750 dev->ttport = port + 1;
753 /* Save readable and stable topology id, distinguishing
754 * devices by location for diagnostics, tools, etc. The
755 * string is a path along hub ports, from the root. Each
756 * device's id will be stable until USB is re-cabled, and
757 * hubs are often labeled with these port numbers.
759 * Initial size: ".NN" times five hubs + NUL = 16 bytes max
760 * (quite rare, since most hubs have 4-6 ports).
763 if (pdev->devpath [0] != '0') /* parent not root? */
764 len = snprintf (dev->devpath, sizeof dev->devpath,
765 "%s.%d", pdev->devpath, port + 1);
766 /* root == "0", root port 2 == "2", port 3 that hub "2.3" */
768 len = snprintf (dev->devpath, sizeof dev->devpath,
770 if (len == sizeof dev->devpath)
771 warn ("devpath size! usb/%03d/%03d path %s",
772 dev->bus->busnum, dev->devnum, dev->devpath);
773 info("new USB device %s-%s, assigned address %d",
774 dev->bus->bus_name, dev->devpath, dev->devnum);
776 /* Run it through the hoops (find a driver, etc) */
777 if (!usb_new_device(dev)) {
778 hub->children[port] = dev;
782 /* Free the configuration if there was an error */
785 /* Switch to a long reset time */
786 delay = HUB_LONG_RESET_TIME;
789 usb_hub_port_disable(hub, port);
791 up(&usb_address0_sem);
794 static void usb_hub_events(void)
797 struct list_head *tmp;
798 struct usb_device *dev;
800 struct usb_hub_status *hubsts;
808 * We restart the list everytime to avoid a deadlock with
809 * deleting hubs downstream from this one. This should be
810 * safe since we delete the hub from the event list.
811 * Not the most efficient, but avoids deadlocks.
814 spin_lock_irqsave(&hub_event_lock, flags);
816 if (list_empty(&hub_event_list))
819 /* Grab the next entry from the beginning of the list */
820 tmp = hub_event_list.next;
822 hub = list_entry(tmp, struct usb_hub, event_list);
828 down(&hub->khubd_sem); /* never blocks, we were on list */
829 spin_unlock_irqrestore(&hub_event_lock, flags);
832 dbg("resetting hub %d for error %d", dev->devnum, hub->error);
834 if (usb_hub_reset(hub)) {
835 err("error resetting hub %d - disconnecting", dev->devnum);
837 usb_hub_disconnect(dev);
845 //for (i = 0; i < hub->descriptor->bNbrPorts; i++) { //+Wilson04272004
846 for (i = (hub->descriptor->bNbrPorts - 1); i >= 0; i--) { //+Wilson04272004, Reversed the get port sequence
847 ret = usb_hub_port_status(dev, i, &portstatus, &portchange);
852 if (portchange & USB_PORT_STAT_C_CONNECTION) {
853 dbg("port %d connection change", i + 1);
855 usb_hub_port_connect_change(hub, i, portstatus, portchange);
856 } else if (portchange & USB_PORT_STAT_C_ENABLE) {
857 dbg("port %d enable change, status %x", i + 1, portstatus);
858 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_ENABLE);
861 * EM interference sometimes causes bad shielded USB devices to
862 * be shutdown by the hub, this hack enables them again.
863 * Works at least with mouse driver.
865 if (!(portstatus & USB_PORT_STAT_ENABLE) &&
866 (portstatus & USB_PORT_STAT_CONNECTION) && (dev->children[i])) {
867 err("already running port %i disabled by hub (EMI?), re-enabling...",
869 usb_hub_port_connect_change(hub, i, portstatus, portchange);
873 if (portchange & USB_PORT_STAT_C_SUSPEND) {
874 dbg("port %d suspend change", i + 1);
875 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_SUSPEND);
878 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
879 err("port %d over-current change", i + 1);
880 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_OVER_CURRENT);
881 usb_hub_power_on(hub);
884 if (portchange & USB_PORT_STAT_C_RESET) {
885 dbg("port %d reset change", i + 1);
886 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_RESET);
890 /* deal with hub status changes */
891 hubsts = kmalloc(sizeof *hubsts, GFP_KERNEL);
893 err("couldn't allocate hubsts");
895 if (usb_get_hub_status(dev, hubsts) < 0)
896 err("get_hub_status failed");
898 hubstatus = le16_to_cpup(&hubsts->wHubStatus);
899 hubchange = le16_to_cpup(&hubsts->wHubChange);
900 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
901 dbg("hub power change");
902 usb_clear_hub_feature(dev, C_HUB_LOCAL_POWER);
904 if (hubchange & HUB_CHANGE_OVERCURRENT) {
905 dbg("hub overcurrent change");
906 wait_ms(500); /* Cool down */
907 usb_clear_hub_feature(dev, C_HUB_OVER_CURRENT);
908 usb_hub_power_on(hub);
914 } /* end while (1) */
916 spin_unlock_irqrestore(&hub_event_lock, flags);
919 static int usb_hub_thread(void *__hub)
924 * This thread doesn't need any user-level access,
925 * so get rid of all our resources
931 /* Setup a nice name */
932 strcpy(current->comm, "khubd");
934 /* Send me a signal to get me die (for debugging) */
937 wait_event_interruptible(khubd_wait, !list_empty(&hub_event_list));
938 } while (!signal_pending(current));
940 dbg("usb_hub_thread exiting");
943 complete_and_exit(&khubd_exited, 0);
946 static struct usb_device_id hub_id_table [] = {
947 { match_flags: USB_DEVICE_ID_MATCH_INT_CLASS,
948 bInterfaceClass: USB_CLASS_HUB},
949 { } /* Terminating entry */
952 MODULE_DEVICE_TABLE (usb, hub_id_table);
954 static struct usb_driver hub_driver = {
958 disconnect: hub_disconnect,
959 id_table: hub_id_table,
963 * This should be a separate module.
965 int usb_hub_init(void)
969 if (usb_register(&hub_driver) < 0) {
970 err("Unable to register USB hub driver");
974 pid = kernel_thread(usb_hub_thread, NULL,
975 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
982 /* Fall through if kernel_thread failed */
983 usb_deregister(&hub_driver);
984 err("failed to start usb_hub_thread");
989 void usb_hub_cleanup(void)
993 /* Kill the thread */
994 ret = kill_proc(khubd_pid, SIGTERM, 1);
996 wait_for_completion(&khubd_exited);
999 * Hub resources are freed for us by usb_deregister. It calls
1000 * usb_driver_purge on every device which in turn calls that
1001 * devices disconnect function if it is using this driver.
1002 * The hub_disconnect function takes care of releasing the
1003 * individual hub resources. -greg
1005 usb_deregister(&hub_driver);
1006 } /* usb_hub_cleanup() */
1009 * WARNING - If a driver calls usb_reset_device, you should simulate a
1010 * disconnect() and probe() for other interfaces you doesn't claim. This
1011 * is left up to the driver writer right now. This insures other drivers
1012 * have a chance to re-setup their interface.
1014 * Take a look at proc_resetdevice in devio.c for some sample code to
1017 int usb_reset_device(struct usb_device *dev)
1019 struct usb_device *parent = dev->parent;
1020 struct usb_device_descriptor *descriptor;
1021 int i, ret, port = -1;
1024 err("attempting to reset root hub!");
1028 for (i = 0; i < parent->maxchild; i++)
1029 if (parent->children[i] == dev) {
1037 down(&usb_address0_sem);
1039 /* Send a reset to the device */
1040 if (usb_hub_port_reset(parent, port, dev, HUB_SHORT_RESET_TIME)) {
1041 usb_hub_port_disable(parent, port);
1042 up(&usb_address0_sem);
1046 /* Reprogram the Address */
1047 ret = usb_set_address(dev);
1049 err("USB device not accepting new address (error=%d)", ret);
1050 usb_hub_port_disable(parent, port);
1051 up(&usb_address0_sem);
1055 /* Let the SET_ADDRESS settle */
1058 up(&usb_address0_sem);
1061 * Now we fetch the configuration descriptors for the device and
1062 * see if anything has changed. If it has, we dump the current
1063 * parsed descriptors and reparse from scratch. Then we leave
1064 * the device alone for the caller to finish setting up.
1066 * If nothing changed, we reprogram the configuration and then
1067 * the alternate settings.
1069 descriptor = kmalloc(sizeof *descriptor, GFP_NOIO);
1073 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, descriptor,
1074 sizeof(*descriptor));
1080 le16_to_cpus(&descriptor->bcdUSB);
1081 le16_to_cpus(&descriptor->idVendor);
1082 le16_to_cpus(&descriptor->idProduct);
1083 le16_to_cpus(&descriptor->bcdDevice);
1085 if (memcmp(&dev->descriptor, descriptor, sizeof(*descriptor))) {
1087 usb_destroy_configuration(dev);
1089 ret = usb_get_device_descriptor(dev);
1090 if (ret < sizeof(dev->descriptor)) {
1092 err("unable to get device descriptor (error=%d)", ret);
1094 err("USB device descriptor short read (expected %Zi, got %i)", sizeof(dev->descriptor), ret);
1096 clear_bit(dev->devnum, &dev->bus->devmap.devicemap);
1101 ret = usb_get_configuration(dev);
1103 err("unable to get configuration (error=%d)", ret);
1104 usb_destroy_configuration(dev);
1105 clear_bit(dev->devnum, &dev->bus->devmap.devicemap);
1110 dev->actconfig = dev->config;
1111 usb_set_maxpacket(dev);
1118 ret = usb_set_configuration(dev, dev->actconfig->bConfigurationValue);
1120 err("failed to set active configuration (error=%d)", ret);
1124 for (i = 0; i < dev->actconfig->bNumInterfaces; i++) {
1125 struct usb_interface *intf = &dev->actconfig->interface[i];
1126 struct usb_interface_descriptor *as = &intf->altsetting[intf->act_altsetting];
1128 ret = usb_set_interface(dev, as->bInterfaceNumber, as->bAlternateSetting);
1130 err("failed to set active alternate setting for interface %d (error=%d)", i, ret);