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 static inline char *portspeed (int portstatus)
47 if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
49 else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
56 /* USB 2.0 spec Section 11.24.4.5 */
57 static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
59 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
60 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
61 USB_DT_HUB << 8, 0, data, size, HZ);
65 * USB 2.0 spec Section 11.24.2.1
67 static int usb_clear_hub_feature(struct usb_device *dev, int feature)
69 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
70 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, HZ);
74 * USB 2.0 spec Section 11.24.2.2
75 * BUG: doesn't handle port indicator selector in high byte of wIndex
77 static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
79 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
80 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port, NULL, 0, HZ);
84 * USB 2.0 spec Section 11.24.2.13
85 * BUG: doesn't handle port indicator selector in high byte of wIndex
87 static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
89 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
90 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port, NULL, 0, HZ);
94 * USB 2.0 spec Section 11.24.2.6
96 static int usb_get_hub_status(struct usb_device *dev, void *data)
98 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
99 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
100 data, sizeof(struct usb_hub_status), HZ);
104 * USB 2.0 spec Section 11.24.2.7
106 static int usb_get_port_status(struct usb_device *dev, int port, void *data)
108 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
109 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
110 data, sizeof(struct usb_hub_status), HZ);
113 static void hub_irq(struct urb *urb)
115 struct usb_hub *hub = (struct usb_hub *)urb->context;
118 /* Cause a hub reset after 10 consecutive errors */
120 if (urb->status == -ENOENT)
123 dbg("nonzero status in irq %d", urb->status);
125 if ((++hub->nerrors < 10) || hub->error)
128 hub->error = urb->status;
133 /* Something happened, let khubd figure it out */
134 spin_lock_irqsave(&hub_event_lock, flags);
135 if (list_empty(&hub->event_list)) {
136 list_add(&hub->event_list, &hub_event_list);
137 wake_up(&khubd_wait);
139 spin_unlock_irqrestore(&hub_event_lock, flags);
142 static void usb_hub_power_on(struct usb_hub *hub)
146 /* Enable power to the ports */
147 dbg("enabling power on all ports");
148 for (i = 0; i < hub->descriptor->bNbrPorts; i++)
149 usb_set_port_feature(hub->dev, i + 1, USB_PORT_FEAT_POWER);
151 /* Wait for power to be enabled */
152 wait_ms(hub->descriptor->bPwrOn2PwrGood * 2);
155 static int usb_hub_configure(struct usb_hub *hub, struct usb_endpoint_descriptor *endpoint)
157 struct usb_device *dev = hub->dev;
158 struct usb_hub_status *hubstatus;
159 char portstr[USB_MAXCHILDREN + 1];
163 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
164 if (!hub->descriptor) {
165 err("Unable to kmalloc %Zd bytes for hub descriptor", sizeof(*hub->descriptor));
169 /* Request the entire hub descriptor. */
170 ret = usb_get_hub_descriptor(dev, hub->descriptor, sizeof(*hub->descriptor));
171 /* <hub->descriptor> is large enough for a hub with 127 ports;
172 * the hub can/will return fewer bytes here. */
174 err("Unable to get hub descriptor (err = %d)", ret);
175 kfree(hub->descriptor);
179 dev->maxchild = hub->descriptor->bNbrPorts;
180 info("%d port%s detected", hub->descriptor->bNbrPorts, (hub->descriptor->bNbrPorts == 1) ? "" : "s");
182 le16_to_cpus(&hub->descriptor->wHubCharacteristics);
184 if (hub->descriptor->wHubCharacteristics & HUB_CHAR_COMPOUND)
185 dbg("part of a compound device");
187 dbg("standalone hub");
189 switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_LPSM) {
191 dbg("ganged power switching");
194 dbg("individual port power switching");
198 dbg("unknown reserved power switching mode");
202 switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_OCPM) {
204 dbg("global over-current protection");
207 dbg("individual port over-current protection");
211 dbg("no over-current protection");
215 switch (dev->descriptor.bDeviceProtocol) {
228 dbg("Unrecognized hub protocol %d",
229 dev->descriptor.bDeviceProtocol);
233 switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_TTTT) {
235 if (dev->descriptor.bDeviceProtocol != 0)
236 dbg("TT requires at most 8 FS bit times");
239 dbg("TT requires at most 16 FS bit times");
242 dbg("TT requires at most 24 FS bit times");
245 dbg("TT requires at most 32 FS bit times");
249 dbg("Port indicators are %s supported",
250 (hub->descriptor->wHubCharacteristics & HUB_CHAR_PORTIND) ? "" : "not");
252 dbg("power on to power good time: %dms", hub->descriptor->bPwrOn2PwrGood * 2);
253 dbg("hub controller current requirement: %dmA", hub->descriptor->bHubContrCurrent);
255 for (i = 0; i < dev->maxchild; i++)
256 portstr[i] = hub->descriptor->DeviceRemovable[((i + 1) / 8)] & (1 << ((i + 1) % 8)) ? 'F' : 'R';
257 portstr[dev->maxchild] = 0;
259 dbg("port removable status: %s", portstr);
261 hubstatus = kmalloc(sizeof *hubstatus, GFP_KERNEL);
263 err("Unable to allocate hubstatus");
264 kfree(hub->descriptor);
267 ret = usb_get_hub_status(dev, hubstatus);
269 err("Unable to get hub status (err = %d)", ret);
271 kfree(hub->descriptor);
275 le16_to_cpus(&hubstatus->wHubStatus);
277 dbg("local power source is %s",
278 (hubstatus->wHubStatus & HUB_STATUS_LOCAL_POWER) ? "lost (inactive)" : "good");
280 dbg("%sover-current condition exists",
281 (hubstatus->wHubStatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
285 /* Start the interrupt endpoint */
286 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
287 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
289 if (maxp > sizeof(hub->buffer))
290 maxp = sizeof(hub->buffer);
292 hub->urb = usb_alloc_urb(0);
294 err("couldn't allocate interrupt urb");
295 kfree(hub->descriptor);
299 FILL_INT_URB(hub->urb, dev, pipe, hub->buffer, maxp, hub_irq, hub,
300 /* NOTE: in 2.5 fill_int_urb() converts the encoding */
301 (dev->speed == USB_SPEED_HIGH)
302 ? 1 << (endpoint->bInterval - 1)
303 : endpoint->bInterval);
304 ret = usb_submit_urb(hub->urb);
306 err("usb_submit_urb failed (%d)", ret);
307 kfree(hub->descriptor);
312 wake_up(&khubd_wait);
314 usb_hub_power_on(hub);
319 static void *hub_probe(struct usb_device *dev, unsigned int i,
320 const struct usb_device_id *id)
322 struct usb_interface_descriptor *interface;
323 struct usb_endpoint_descriptor *endpoint;
327 interface = &dev->actconfig->interface[i].altsetting[0];
329 /* Some hubs have a subclass of 1, which AFAICT according to the */
330 /* specs is not defined, but it works */
331 if ((interface->bInterfaceSubClass != 0) &&
332 (interface->bInterfaceSubClass != 1)) {
333 err("invalid subclass (%d) for USB hub device #%d",
334 interface->bInterfaceSubClass, dev->devnum);
338 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
339 if (interface->bNumEndpoints != 1) {
340 err("invalid bNumEndpoints (%d) for USB hub device #%d",
341 interface->bNumEndpoints, dev->devnum);
345 endpoint = &interface->endpoint[0];
347 /* Output endpoint? Curiousier and curiousier.. */
348 if (!(endpoint->bEndpointAddress & USB_DIR_IN)) {
349 err("Device #%d is hub class, but has output endpoint?",
354 /* If it's not an interrupt endpoint, we'd better punt! */
355 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) {
356 err("Device #%d is hub class, but has endpoint other than interrupt?",
362 info("USB hub found");
364 hub = kmalloc(sizeof(*hub), GFP_KERNEL);
366 err("couldn't kmalloc hub struct");
370 memset(hub, 0, sizeof(*hub));
372 INIT_LIST_HEAD(&hub->event_list);
374 init_MUTEX(&hub->khubd_sem);
376 /* Record the new hub's existence */
377 spin_lock_irqsave(&hub_event_lock, flags);
378 INIT_LIST_HEAD(&hub->hub_list);
379 list_add(&hub->hub_list, &hub_list);
380 spin_unlock_irqrestore(&hub_event_lock, flags);
382 if (usb_hub_configure(hub, endpoint) >= 0)
385 err("hub configuration failed for device #%d", dev->devnum);
387 /* free hub, but first clean up its list. */
388 spin_lock_irqsave(&hub_event_lock, flags);
390 /* Delete it and then reset it */
391 list_del(&hub->event_list);
392 INIT_LIST_HEAD(&hub->event_list);
393 list_del(&hub->hub_list);
394 INIT_LIST_HEAD(&hub->hub_list);
396 spin_unlock_irqrestore(&hub_event_lock, flags);
403 static void hub_disconnect(struct usb_device *dev, void *ptr)
405 struct usb_hub *hub = (struct usb_hub *)ptr;
408 spin_lock_irqsave(&hub_event_lock, flags);
410 /* Delete it and then reset it */
411 list_del(&hub->event_list);
412 INIT_LIST_HEAD(&hub->event_list);
413 list_del(&hub->hub_list);
414 INIT_LIST_HEAD(&hub->hub_list);
416 spin_unlock_irqrestore(&hub_event_lock, flags);
418 down(&hub->khubd_sem); /* Wait for khubd to leave this hub alone. */
422 usb_unlink_urb(hub->urb);
423 usb_free_urb(hub->urb);
427 if (hub->descriptor) {
428 kfree(hub->descriptor);
429 hub->descriptor = NULL;
432 /* Free the memory */
436 static int hub_ioctl(struct usb_device *hub, unsigned int code, void *user_data)
438 /* assert ifno == 0 (part of hub spec) */
440 case USBDEVFS_HUB_PORTINFO: {
441 struct usbdevfs_hub_portinfo *info = user_data;
445 spin_lock_irqsave(&hub_event_lock, flags);
446 if (hub->devnum <= 0)
449 info->nports = hub->maxchild;
450 for (i = 0; i < info->nports; i++) {
451 if (hub->children[i] == NULL)
454 info->port[i] = hub->children[i]->devnum;
457 spin_unlock_irqrestore(&hub_event_lock, flags);
459 return info->nports + 1;
467 static int usb_hub_reset(struct usb_hub *hub)
469 struct usb_device *dev = hub->dev;
472 /* Disconnect any attached devices */
473 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
474 if (dev->children[i])
475 usb_disconnect(&dev->children[i]);
478 /* Attempt to reset the hub */
480 usb_unlink_urb(hub->urb);
484 if (usb_reset_device(dev))
488 if (usb_submit_urb(hub->urb))
491 usb_hub_power_on(hub);
496 static void usb_hub_disconnect(struct usb_device *dev)
498 struct usb_device *parent = dev->parent;
501 /* Find the device pointer to disconnect */
503 for (i = 0; i < parent->maxchild; i++) {
504 if (parent->children[i] == dev) {
505 usb_disconnect(&parent->children[i]);
511 err("cannot disconnect hub %d", dev->devnum);
514 static int usb_hub_port_status(struct usb_device *hub, int port,
515 u16 *status, u16 *change)
517 struct usb_port_status *portsts;
520 portsts = kmalloc(sizeof(*portsts), GFP_KERNEL);
522 ret = usb_get_port_status(hub, port + 1, portsts);
524 err("%s (%d) failed (err = %d)", __FUNCTION__, hub->devnum, ret);
526 *status = le16_to_cpu(portsts->wPortStatus);
527 *change = le16_to_cpu(portsts->wPortChange);
528 dbg("port %d, portstatus %x, change %x, %s", port + 1,
529 *status, *change, portspeed(*status));
537 #define HUB_RESET_TRIES 5
538 #define HUB_PROBE_TRIES 2
539 #define HUB_SHORT_RESET_TIME 10
540 #define HUB_LONG_RESET_TIME 200
541 #define HUB_RESET_TIMEOUT 500
543 /* return: -1 on error, 0 on success, 1 on disconnect. */
544 static int usb_hub_port_wait_reset(struct usb_device *hub, int port,
545 struct usb_device *dev, unsigned int delay)
551 for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT; delay_time += delay) {
552 /* wait to give the device a chance to reset */
555 /* read and decode port status */
556 ret = usb_hub_port_status(hub, port, &portstatus, &portchange);
561 /* Device went away? */
562 if (!(portstatus & USB_PORT_STAT_CONNECTION))
565 /* bomb out completely if something weird happened */
566 if ((portchange & USB_PORT_STAT_C_CONNECTION))
569 /* if we`ve finished resetting, then break out of the loop */
570 if (!(portstatus & USB_PORT_STAT_RESET) &&
571 (portstatus & USB_PORT_STAT_ENABLE)) {
572 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
573 dev->speed = USB_SPEED_HIGH;
574 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
575 dev->speed = USB_SPEED_LOW;
577 dev->speed = USB_SPEED_FULL;
581 /* switch to the long delay after two short delay failures */
582 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
583 delay = HUB_LONG_RESET_TIME;
585 dbg("port %d of hub %d not reset yet, waiting %dms", port + 1,
592 /* return: -1 on error, 0 on success, 1 on disconnect. */
593 static int usb_hub_port_reset(struct usb_device *hub, int port,
594 struct usb_device *dev, unsigned int delay)
599 for (i = 0; i < HUB_RESET_TRIES; i++) {
600 usb_set_port_feature(hub, port + 1, USB_PORT_FEAT_RESET);
602 /* return on disconnect or reset */
603 status = usb_hub_port_wait_reset(hub, port, dev, delay);
605 usb_clear_port_feature(hub, port + 1, USB_PORT_FEAT_C_RESET);
609 dbg("port %d of hub %d not enabled, trying reset again...",
610 port + 1, hub->devnum);
611 delay = HUB_LONG_RESET_TIME;
614 err("Cannot enable port %i of hub %d, disabling port.",
615 port + 1, hub->devnum);
616 err("Maybe the USB cable is bad?");
621 void usb_hub_port_disable(struct usb_device *hub, int port)
625 ret = usb_clear_port_feature(hub, port + 1, USB_PORT_FEAT_ENABLE);
627 err("cannot disable port %d of hub %d (err = %d)",
628 port + 1, hub->devnum, ret);
631 /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
633 * Between connect detection and reset signaling there must be a delay
634 * of 100ms at least for debounce and power-settling. The corresponding
635 * timer shall restart whenever the downstream port detects a disconnect.
637 * Apparently there are some bluetooth and irda-dongles and a number
638 * of low-speed devices which require longer delays of about 200-400ms.
639 * Not covered by the spec - but easy to deal with.
641 * This implementation uses 400ms minimum debounce timeout and checks
642 * every 100ms for transient disconnects to restart the delay.
645 #define HUB_DEBOUNCE_TIMEOUT 400
646 #define HUB_DEBOUNCE_STEP 100
648 /* return: -1 on error, 0 on success, 1 on disconnect. */
649 static int usb_hub_port_debounce(struct usb_device *hub, int port)
653 u16 portchange, portstatus;
655 for (delay_time = 0; delay_time < HUB_DEBOUNCE_TIMEOUT; /* empty */ ) {
657 /* wait debounce step increment */
658 wait_ms(HUB_DEBOUNCE_STEP);
660 ret = usb_hub_port_status(hub, port, &portstatus, &portchange);
664 if ((portchange & USB_PORT_STAT_C_CONNECTION)) {
665 usb_clear_port_feature(hub, port+1, USB_PORT_FEAT_C_CONNECTION);
669 delay_time += HUB_DEBOUNCE_STEP;
671 return ((portstatus&USB_PORT_STAT_CONNECTION)) ? 0 : 1;
674 static void usb_hub_port_connect_change(struct usb_hub *hubstate, int port,
675 u16 portstatus, u16 portchange)
677 struct usb_device *hub = hubstate->dev;
678 struct usb_device *dev;
679 unsigned int delay = HUB_SHORT_RESET_TIME;
682 dbg("port %d, portstatus %x, change %x, %s",
683 port + 1, portstatus, portchange, portspeed (portstatus));
685 /* Clear the connection change status */
686 usb_clear_port_feature(hub, port + 1, USB_PORT_FEAT_C_CONNECTION);
688 /* Disconnect any existing devices under this port */
689 if (hub->children[port])
690 usb_disconnect(&hub->children[port]);
692 /* Return now if nothing is connected */
693 if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
694 if (portstatus & USB_PORT_STAT_ENABLE)
695 usb_hub_port_disable(hub, port);
700 if (usb_hub_port_debounce(hub, port)) {
701 err("connect-debounce failed, port %d disabled", port+1);
702 usb_hub_port_disable(hub, port);
706 down(&usb_address0_sem);
708 for (i = 0; i < HUB_PROBE_TRIES; i++) {
709 struct usb_device *pdev;
712 /* Allocate a new device struct */
713 dev = usb_alloc_dev(hub, hub->bus);
715 err("couldn't allocate usb_device");
719 /* Reset the device */
720 if (usb_hub_port_reset(hub, port, dev, delay)) {
725 /* Find a new device ID for it */
728 /* Set up TT records, if needed */
731 dev->ttport = hub->ttport;
732 } else if (dev->speed != USB_SPEED_HIGH
733 && hub->speed == USB_SPEED_HIGH) {
734 dev->tt = &hubstate->tt;
735 dev->ttport = port + 1;
738 /* Save readable and stable topology id, distinguishing
739 * devices by location for diagnostics, tools, etc. The
740 * string is a path along hub ports, from the root. Each
741 * device's id will be stable until USB is re-cabled, and
742 * hubs are often labeled with these port numbers.
744 * Initial size: ".NN" times five hubs + NUL = 16 bytes max
745 * (quite rare, since most hubs have 4-6 ports).
748 if (pdev->devpath [0] != '0') /* parent not root? */
749 len = snprintf (dev->devpath, sizeof dev->devpath,
750 "%s.%d", pdev->devpath, port + 1);
751 /* root == "0", root port 2 == "2", port 3 that hub "2.3" */
753 len = snprintf (dev->devpath, sizeof dev->devpath,
755 if (len == sizeof dev->devpath)
756 warn ("devpath size! usb/%03d/%03d path %s",
757 dev->bus->busnum, dev->devnum, dev->devpath);
758 info("new USB device %s-%s, assigned address %d",
759 dev->bus->bus_name, dev->devpath, dev->devnum);
761 /* Run it through the hoops (find a driver, etc) */
762 if (!usb_new_device(dev)) {
763 hub->children[port] = dev;
767 /* Free the configuration if there was an error */
770 /* Switch to a long reset time */
771 delay = HUB_LONG_RESET_TIME;
774 usb_hub_port_disable(hub, port);
776 up(&usb_address0_sem);
779 static void usb_hub_events(void)
782 struct list_head *tmp;
783 struct usb_device *dev;
785 struct usb_hub_status *hubsts;
793 * We restart the list everytime to avoid a deadlock with
794 * deleting hubs downstream from this one. This should be
795 * safe since we delete the hub from the event list.
796 * Not the most efficient, but avoids deadlocks.
799 spin_lock_irqsave(&hub_event_lock, flags);
801 if (list_empty(&hub_event_list))
804 /* Grab the next entry from the beginning of the list */
805 tmp = hub_event_list.next;
807 hub = list_entry(tmp, struct usb_hub, event_list);
813 down(&hub->khubd_sem); /* never blocks, we were on list */
814 spin_unlock_irqrestore(&hub_event_lock, flags);
817 dbg("resetting hub %d for error %d", dev->devnum, hub->error);
819 if (usb_hub_reset(hub)) {
820 err("error resetting hub %d - disconnecting", dev->devnum);
822 usb_hub_disconnect(dev);
830 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
831 ret = usb_hub_port_status(dev, i, &portstatus, &portchange);
836 if (portchange & USB_PORT_STAT_C_CONNECTION) {
837 dbg("port %d connection change", i + 1);
839 usb_hub_port_connect_change(hub, i, portstatus, portchange);
840 } else if (portchange & USB_PORT_STAT_C_ENABLE) {
841 dbg("port %d enable change, status %x", i + 1, portstatus);
842 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_ENABLE);
845 * EM interference sometimes causes bad shielded USB devices to
846 * be shutdown by the hub, this hack enables them again.
847 * Works at least with mouse driver.
849 if (!(portstatus & USB_PORT_STAT_ENABLE) &&
850 (portstatus & USB_PORT_STAT_CONNECTION) && (dev->children[i])) {
851 err("already running port %i disabled by hub (EMI?), re-enabling...",
853 usb_hub_port_connect_change(hub, i, portstatus, portchange);
857 if (portchange & USB_PORT_STAT_C_SUSPEND) {
858 dbg("port %d suspend change", i + 1);
859 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_SUSPEND);
862 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
863 err("port %d over-current change", i + 1);
864 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_OVER_CURRENT);
865 usb_hub_power_on(hub);
868 if (portchange & USB_PORT_STAT_C_RESET) {
869 dbg("port %d reset change", i + 1);
870 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_RESET);
874 /* deal with hub status changes */
875 hubsts = kmalloc(sizeof *hubsts, GFP_KERNEL);
877 err("couldn't allocate hubsts");
879 if (usb_get_hub_status(dev, hubsts) < 0)
880 err("get_hub_status failed");
882 hubstatus = le16_to_cpup(&hubsts->wHubStatus);
883 hubchange = le16_to_cpup(&hubsts->wHubChange);
884 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
885 dbg("hub power change");
886 usb_clear_hub_feature(dev, C_HUB_LOCAL_POWER);
888 if (hubchange & HUB_CHANGE_OVERCURRENT) {
889 dbg("hub overcurrent change");
890 wait_ms(500); /* Cool down */
891 usb_clear_hub_feature(dev, C_HUB_OVER_CURRENT);
892 usb_hub_power_on(hub);
898 } /* end while (1) */
900 spin_unlock_irqrestore(&hub_event_lock, flags);
903 static int usb_hub_thread(void *__hub)
908 * This thread doesn't need any user-level access,
909 * so get rid of all our resources
915 /* Setup a nice name */
916 strcpy(current->comm, "khubd");
918 /* Send me a signal to get me die (for debugging) */
921 wait_event_interruptible(khubd_wait, !list_empty(&hub_event_list));
922 } while (!signal_pending(current));
924 dbg("usb_hub_thread exiting");
927 complete_and_exit(&khubd_exited, 0);
930 static struct usb_device_id hub_id_table [] = {
931 { match_flags: USB_DEVICE_ID_MATCH_INT_CLASS,
932 bInterfaceClass: USB_CLASS_HUB},
933 { } /* Terminating entry */
936 MODULE_DEVICE_TABLE (usb, hub_id_table);
938 static struct usb_driver hub_driver = {
942 disconnect: hub_disconnect,
943 id_table: hub_id_table,
947 * This should be a separate module.
949 int usb_hub_init(void)
953 if (usb_register(&hub_driver) < 0) {
954 err("Unable to register USB hub driver");
958 pid = kernel_thread(usb_hub_thread, NULL,
959 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
966 /* Fall through if kernel_thread failed */
967 usb_deregister(&hub_driver);
968 err("failed to start usb_hub_thread");
973 void usb_hub_cleanup(void)
977 /* Kill the thread */
978 ret = kill_proc(khubd_pid, SIGTERM, 1);
980 wait_for_completion(&khubd_exited);
983 * Hub resources are freed for us by usb_deregister. It calls
984 * usb_driver_purge on every device which in turn calls that
985 * devices disconnect function if it is using this driver.
986 * The hub_disconnect function takes care of releasing the
987 * individual hub resources. -greg
989 usb_deregister(&hub_driver);
990 } /* usb_hub_cleanup() */
993 * WARNING - If a driver calls usb_reset_device, you should simulate a
994 * disconnect() and probe() for other interfaces you doesn't claim. This
995 * is left up to the driver writer right now. This insures other drivers
996 * have a chance to re-setup their interface.
998 * Take a look at proc_resetdevice in devio.c for some sample code to
1001 int usb_reset_device(struct usb_device *dev)
1003 struct usb_device *parent = dev->parent;
1004 struct usb_device_descriptor *descriptor;
1005 int i, ret, port = -1;
1008 err("attempting to reset root hub!");
1012 for (i = 0; i < parent->maxchild; i++)
1013 if (parent->children[i] == dev) {
1021 down(&usb_address0_sem);
1023 /* Send a reset to the device */
1024 if (usb_hub_port_reset(parent, port, dev, HUB_SHORT_RESET_TIME)) {
1025 usb_hub_port_disable(parent, port);
1026 up(&usb_address0_sem);
1030 /* Reprogram the Address */
1031 ret = usb_set_address(dev);
1033 err("USB device not accepting new address (error=%d)", ret);
1034 usb_hub_port_disable(parent, port);
1035 up(&usb_address0_sem);
1039 /* Let the SET_ADDRESS settle */
1042 up(&usb_address0_sem);
1045 * Now we fetch the configuration descriptors for the device and
1046 * see if anything has changed. If it has, we dump the current
1047 * parsed descriptors and reparse from scratch. Then we leave
1048 * the device alone for the caller to finish setting up.
1050 * If nothing changed, we reprogram the configuration and then
1051 * the alternate settings.
1053 descriptor = kmalloc(sizeof *descriptor, GFP_NOIO);
1057 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, descriptor,
1058 sizeof(*descriptor));
1064 le16_to_cpus(&descriptor->bcdUSB);
1065 le16_to_cpus(&descriptor->idVendor);
1066 le16_to_cpus(&descriptor->idProduct);
1067 le16_to_cpus(&descriptor->bcdDevice);
1069 if (memcmp(&dev->descriptor, descriptor, sizeof(*descriptor))) {
1071 usb_destroy_configuration(dev);
1073 ret = usb_get_device_descriptor(dev);
1074 if (ret < sizeof(dev->descriptor)) {
1076 err("unable to get device descriptor (error=%d)", ret);
1078 err("USB device descriptor short read (expected %Zi, got %i)", sizeof(dev->descriptor), ret);
1080 clear_bit(dev->devnum, &dev->bus->devmap.devicemap);
1085 ret = usb_get_configuration(dev);
1087 err("unable to get configuration (error=%d)", ret);
1088 usb_destroy_configuration(dev);
1089 clear_bit(dev->devnum, &dev->bus->devmap.devicemap);
1094 dev->actconfig = dev->config;
1095 usb_set_maxpacket(dev);
1102 ret = usb_set_configuration(dev, dev->actconfig->bConfigurationValue);
1104 err("failed to set active configuration (error=%d)", ret);
1108 for (i = 0; i < dev->actconfig->bNumInterfaces; i++) {
1109 struct usb_interface *intf = &dev->actconfig->interface[i];
1110 struct usb_interface_descriptor *as = &intf->altsetting[intf->act_altsetting];
1112 ret = usb_set_interface(dev, as->bInterfaceNumber, as->bAlternateSetting);
1114 err("failed to set active alternate setting for interface %d (error=%d)", i, ret);