4 * (C) Copyright 1999 Linus Torvalds
5 * (C) Copyright 1999 Johannes Erdfelt
6 * (C) Copyright 1999 Gregory P. Smith
9 #include <linux/config.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/list.h>
13 #include <linux/malloc.h>
14 #include <linux/smp_lock.h>
15 #ifdef CONFIG_USB_DEBUG
20 #include <linux/usb.h>
21 #include <linux/usbdevice_fs.h>
23 #include <asm/semaphore.h>
24 #include <asm/uaccess.h>
25 #include <asm/byteorder.h>
30 static spinlock_t hub_event_lock
= SPIN_LOCK_UNLOCKED
;
31 static DECLARE_MUTEX(usb_address0_sem
);
33 static LIST_HEAD(hub_event_list
); /* List of hubs needing servicing */
34 static LIST_HEAD(hub_list
); /* List containing all of the hubs (for cleanup) */
36 static DECLARE_WAIT_QUEUE_HEAD(khubd_wait
);
37 static int khubd_pid
= 0; /* PID of khubd */
38 static int khubd_running
= 0;
40 static int usb_get_hub_descriptor(struct usb_device
*dev
, void *data
, int size
)
42 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
43 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
| USB_RT_HUB
,
44 USB_DT_HUB
<< 8, 0, data
, size
, HZ
);
47 static int usb_clear_hub_feature(struct usb_device
*dev
, int feature
)
49 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
50 USB_REQ_CLEAR_FEATURE
, USB_RT_HUB
, feature
, 0, NULL
, 0, HZ
);
53 static int usb_clear_port_feature(struct usb_device
*dev
, int port
, int feature
)
55 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
56 USB_REQ_CLEAR_FEATURE
, USB_RT_PORT
, feature
, port
, NULL
, 0, HZ
);
59 static int usb_set_port_feature(struct usb_device
*dev
, int port
, int feature
)
61 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
62 USB_REQ_SET_FEATURE
, USB_RT_PORT
, feature
, port
, NULL
, 0, HZ
);
65 static int usb_get_hub_status(struct usb_device
*dev
, void *data
)
67 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
68 USB_REQ_GET_STATUS
, USB_DIR_IN
| USB_RT_HUB
, 0, 0,
69 data
, sizeof(struct usb_hub_status
), HZ
);
72 static int usb_get_port_status(struct usb_device
*dev
, int port
, void *data
)
74 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
75 USB_REQ_GET_STATUS
, USB_DIR_IN
| USB_RT_PORT
, 0, port
,
76 data
, sizeof(struct usb_hub_status
), HZ
);
80 * A irq handler returns non-zero to indicate to
81 * the low-level driver that it wants to be re-activated,
82 * or zero to say "I'm done".
84 static void hub_irq(struct urb
*urb
)
86 struct usb_hub
*hub
= (struct usb_hub
*)urb
->context
;
90 if (urb
->status
!= -ENOENT
)
91 dbg("nonzero status in irq %d", urb
->status
);
96 /* Something happened, let khubd figure it out */
97 if (waitqueue_active(&khubd_wait
)) {
98 /* Add the hub to the event queue */
99 spin_lock_irqsave(&hub_event_lock
, flags
);
100 if (hub
->event_list
.next
== &hub
->event_list
) {
101 list_add(&hub
->event_list
, &hub_event_list
);
103 wake_up(&khubd_wait
);
105 spin_unlock_irqrestore(&hub_event_lock
, flags
);
109 static void usb_hub_power_on(struct usb_hub
*hub
)
113 /* Enable power to the ports */
114 dbg("enabling power on all ports");
115 for (i
= 0; i
< hub
->nports
; i
++)
116 usb_set_port_feature(hub
->dev
, i
+ 1, USB_PORT_FEAT_POWER
);
119 static int usb_hub_configure(struct usb_hub
*hub
)
121 struct usb_device
*dev
= hub
->dev
;
122 unsigned char buffer
[HUB_DESCRIPTOR_MAX_SIZE
], *bitmap
;
123 struct usb_hub_descriptor
*descriptor
;
124 struct usb_descriptor_header
*header
;
125 struct usb_hub_status
*hubsts
;
128 /* Request the entire hub descriptor. */
129 header
= (struct usb_descriptor_header
*)buffer
;
130 ret
= usb_get_hub_descriptor(dev
, buffer
, sizeof(buffer
));
131 /* <buffer> is large enough for a hub with 127 ports;
132 * the hub can/will return fewer bytes here. */
134 err("Unable to get hub descriptor (err = %d)", ret
);
138 bitmap
= kmalloc(header
->bLength
, GFP_KERNEL
);
140 err("Unable to kmalloc %d bytes for bitmap", header
->bLength
);
144 memcpy (bitmap
, buffer
, header
->bLength
);
145 descriptor
= (struct usb_hub_descriptor
*)bitmap
;
147 hub
->nports
= dev
->maxchild
= descriptor
->bNbrPorts
;
148 info("%d port%s detected", hub
->nports
, (hub
->nports
== 1) ? "" : "s");
150 switch (descriptor
->wHubCharacteristics
& HUB_CHAR_LPSM
) {
152 dbg("ganged power switching");
155 dbg("individual port power switching");
159 dbg("unknown reserved power switching mode");
163 if (descriptor
->wHubCharacteristics
& HUB_CHAR_COMPOUND
)
164 dbg("part of a compound device");
166 dbg("standalone hub");
168 switch (descriptor
->wHubCharacteristics
& HUB_CHAR_OCPM
) {
170 dbg("global over-current protection");
173 dbg("individual port over-current protection");
177 dbg("no over-current protection");
181 dbg("power on to power good time: %dms", descriptor
->bPwrOn2PwrGood
* 2);
182 dbg("hub controller current requirement: %dmA", descriptor
->bHubContrCurrent
);
184 for (i
= 0; i
< dev
->maxchild
; i
++)
185 dbg("port %d is%s removable", i
+ 1,
186 bitmap
[7 + ((i
+ 1)/8)] & (1 << ((i
+ 1) % 8))
191 ret
= usb_get_hub_status(dev
, buffer
);
193 err("Unable to get hub status (err = %d)", ret
);
197 hubsts
= (struct usb_hub_status
*)buffer
;
198 dbg("local power source is %s",
199 (le16_to_cpu(hubsts
->wHubStatus
) & HUB_STATUS_LOCAL_POWER
) ? "lost (inactive)" : "good");
201 dbg("%sover-current condition exists",
202 (le16_to_cpu(hubsts
->wHubStatus
) & HUB_STATUS_OVERCURRENT
) ? "" : "no ");
204 usb_hub_power_on(hub
);
209 static void *hub_probe(struct usb_device
*dev
, unsigned int i
)
211 struct usb_interface_descriptor
*interface
;
212 struct usb_endpoint_descriptor
*endpoint
;
218 interface
= &dev
->actconfig
->interface
[i
].altsetting
[0];
221 if (interface
->bInterfaceClass
!= USB_CLASS_HUB
)
224 /* Some hubs have a subclass of 1, which AFAICT according to the */
225 /* specs is not defined, but it works */
226 if ((interface
->bInterfaceSubClass
!= 0) &&
227 (interface
->bInterfaceSubClass
!= 1))
230 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
231 if (interface
->bNumEndpoints
!= 1)
234 endpoint
= &interface
->endpoint
[0];
236 /* Output endpoint? Curiousier and curiousier.. */
237 if (!(endpoint
->bEndpointAddress
& USB_DIR_IN
)) {
238 err("Device is hub class, but has output endpoint?");
242 /* If it's not an interrupt endpoint, we'd better punt! */
243 if ((endpoint
->bmAttributes
& 3) != 3) {
244 err("Device is hub class, but has endpoint other than interrupt?");
249 info("USB hub found");
251 hub
= kmalloc(sizeof(*hub
), GFP_KERNEL
);
253 err("couldn't kmalloc hub struct");
257 memset(hub
, 0, sizeof(*hub
));
259 INIT_LIST_HEAD(&hub
->event_list
);
262 /* Record the new hub's existence */
263 spin_lock_irqsave(&hub_event_lock
, flags
);
264 INIT_LIST_HEAD(&hub
->hub_list
);
265 list_add(&hub
->hub_list
, &hub_list
);
266 spin_unlock_irqrestore(&hub_event_lock
, flags
);
268 if (usb_hub_configure(hub
) >= 0) {
269 pipe
= usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
);
270 maxp
= usb_maxpacket(dev
, pipe
, usb_pipeout(pipe
));
272 if (maxp
> sizeof(hub
->buffer
))
273 maxp
= sizeof(hub
->buffer
);
275 hub
->urb
= usb_alloc_urb(0);
277 err("couldn't allocate interrupt urb");
281 FILL_INT_URB(hub
->urb
, dev
, pipe
, hub
->buffer
, maxp
, hub_irq
,
282 hub
, endpoint
->bInterval
);
283 ret
= usb_submit_urb(hub
->urb
);
285 err("usb_submit_urb failed (%d)", ret
);
290 wake_up(&khubd_wait
);
296 /* free hub, but first clean up its list. */
297 spin_lock_irqsave(&hub_event_lock
, flags
);
299 /* Delete it and then reset it */
300 list_del(&hub
->event_list
);
301 INIT_LIST_HEAD(&hub
->event_list
);
302 list_del(&hub
->hub_list
);
303 INIT_LIST_HEAD(&hub
->hub_list
);
305 spin_unlock_irqrestore(&hub_event_lock
, flags
);
312 static void hub_disconnect(struct usb_device
*dev
, void *ptr
)
314 struct usb_hub
*hub
= (struct usb_hub
*)ptr
;
317 spin_lock_irqsave(&hub_event_lock
, flags
);
319 /* Delete it and then reset it */
320 list_del(&hub
->event_list
);
321 INIT_LIST_HEAD(&hub
->event_list
);
322 list_del(&hub
->hub_list
);
323 INIT_LIST_HEAD(&hub
->hub_list
);
325 spin_unlock_irqrestore(&hub_event_lock
, flags
);
328 usb_unlink_urb(hub
->urb
);
329 usb_free_urb(hub
->urb
);
333 /* Free the memory */
337 static int hub_ioctl (struct usb_device
*hub
, unsigned int code
, void *user_data
)
339 /* assert ifno == 0 (part of hub spec) */
341 case USBDEVFS_HUB_PORTINFO
: {
342 struct usbdevfs_hub_portinfo
*info
= user_data
;
346 spin_lock_irqsave (&hub_event_lock
, flags
);
347 if (hub
->devnum
<= 0)
350 info
->nports
= hub
->maxchild
;
351 for (i
= 0; i
< info
->nports
; i
++) {
352 if (hub
->children
[i
] == NULL
)
355 info
->port
[i
] = hub
->children
[i
]->devnum
;
358 spin_unlock_irqrestore (&hub_event_lock
, flags
);
360 return info
->nports
+ 1;
368 static void usb_hub_port_connect_change(struct usb_device
*hub
, int port
)
370 struct usb_device
*usb
;
371 struct usb_port_status portsts
;
372 unsigned short portstatus
, portchange
;
377 ret
= usb_get_port_status(hub
, port
+ 1, &portsts
);
379 err("get_port_status(%d) failed (err = %d)", port
+ 1, ret
);
383 portstatus
= le16_to_cpu(portsts
.wPortStatus
);
384 portchange
= le16_to_cpu(portsts
.wPortChange
);
385 dbg("portstatus %x, change %x, %s", portstatus
, portchange
,
386 portstatus
&(1<<USB_PORT_FEAT_LOWSPEED
) ? "1.5 Mb/s" : "12 Mb/s");
388 /* Clear the connection change status */
389 usb_clear_port_feature(hub
, port
+ 1, USB_PORT_FEAT_C_CONNECTION
);
391 /* Disconnect any existing devices under this port */
392 if (((!(portstatus
& USB_PORT_STAT_CONNECTION
)) &&
393 (!(portstatus
& USB_PORT_STAT_ENABLE
)))|| (hub
->children
[port
])) {
394 usb_disconnect(&hub
->children
[port
]);
395 /* Return now if nothing is connected */
396 if (!(portstatus
& USB_PORT_STAT_CONNECTION
))
401 down(&usb_address0_sem
);
405 for (tries
= 0; tries
< MAX_TRIES
; tries
++) {
406 usb_set_port_feature(hub
, port
+ 1, USB_PORT_FEAT_RESET
);
409 ret
= usb_get_port_status(hub
, port
+ 1, &portsts
);
411 err("get_port_status(%d) failed (err = %d)", port
+ 1, ret
);
415 portstatus
= le16_to_cpu(portsts
.wPortStatus
);
416 portchange
= le16_to_cpu(portsts
.wPortChange
);
417 dbg("portstatus %x, change %x, %s", portstatus
,portchange
,
418 portstatus
&(1<<USB_PORT_FEAT_LOWSPEED
) ? "1.5 Mb/s" : "12 Mb/s");
420 if ((portchange
& USB_PORT_STAT_C_CONNECTION
) ||
421 !(portstatus
& USB_PORT_STAT_CONNECTION
))
424 if (portstatus
& USB_PORT_STAT_ENABLE
)
430 if (tries
>= MAX_TRIES
) {
431 err("Cannot enable port %i after %i retries, disabling port.", port
+1, MAX_TRIES
);
432 err("Maybe the USB cable is bad?");
436 usb_clear_port_feature(hub
, port
+ 1, USB_PORT_FEAT_C_RESET
);
438 /* Allocate a new device struct for it */
439 usb
= usb_alloc_dev(hub
, hub
->bus
);
441 err("couldn't allocate usb_device");
445 usb
->slow
= (portstatus
& USB_PORT_STAT_LOW_SPEED
) ? 1 : 0;
447 hub
->children
[port
] = usb
;
449 /* Find a new device ID for it */
452 /* Run it through the hoops (find a driver, etc) */
453 ret
= usb_new_device(usb
);
455 /* Try resetting the device. Windows does this and it */
456 /* gets some devices working correctly */
457 usb_set_port_feature(hub
, port
+ 1, USB_PORT_FEAT_RESET
);
459 ret
= usb_new_device(usb
);
461 usb_disconnect(&hub
->children
[port
]);
463 /* Woops, disable the port */
464 dbg("hub: disabling port %d", port
+ 1);
465 usb_clear_port_feature(hub
, port
+ 1,
466 USB_PORT_FEAT_ENABLE
);
471 up(&usb_address0_sem
);
474 static void usb_hub_events(void)
478 struct list_head
*tmp
;
479 struct usb_device
*dev
;
481 struct usb_hub_status hubsts
;
482 unsigned short hubstatus
, hubchange
;
485 * We restart the list everytime to avoid a deadlock with
486 * deleting hubs downstream from this one. This should be
487 * safe since we delete the hub from the event list.
488 * Not the most efficient, but avoids deadlocks.
491 spin_lock_irqsave(&hub_event_lock
, flags
);
493 if (list_empty(&hub_event_list
))
496 /* Grab the next entry from the beginning of the list */
497 tmp
= hub_event_list
.next
;
499 hub
= list_entry(tmp
, struct usb_hub
, event_list
);
505 spin_unlock_irqrestore(&hub_event_lock
, flags
);
507 for (i
= 0; i
< hub
->nports
; i
++) {
508 struct usb_port_status portsts
;
509 unsigned short portstatus
, portchange
;
511 if (usb_get_port_status(dev
, i
+ 1, &portsts
) < 0) {
512 err("get_port_status failed");
516 portstatus
= le16_to_cpu(portsts
.wPortStatus
);
517 portchange
= le16_to_cpu(portsts
.wPortChange
);
519 if (portchange
& USB_PORT_STAT_C_CONNECTION
) {
520 dbg("port %d connection change", i
+ 1);
522 usb_hub_port_connect_change(dev
, i
);
525 if (portchange
& USB_PORT_STAT_C_ENABLE
) {
526 dbg("port %d enable change, status %x", i
+ 1, portstatus
);
527 usb_clear_port_feature(dev
, i
+ 1, USB_PORT_FEAT_C_ENABLE
);
529 // EM interference sometimes causes bad shielded USB devices to
530 // be shutdown by the hub, this hack enables them again.
531 // Works at least with mouse driver.
532 if (!(portstatus
& USB_PORT_STAT_ENABLE
) &&
533 (portstatus
& USB_PORT_STAT_CONNECTION
) && (dev
->children
[i
])) {
534 err("already running port %i disabled by hub (EMI?), re-enabling...",
536 usb_hub_port_connect_change(dev
, i
);
540 if (portstatus
& USB_PORT_STAT_SUSPEND
) {
541 dbg("port %d suspend change", i
+ 1);
542 usb_clear_port_feature(dev
, i
+ 1, USB_PORT_FEAT_SUSPEND
);
545 if (portchange
& USB_PORT_STAT_C_OVERCURRENT
) {
546 err("port %d over-current change", i
+ 1);
547 usb_clear_port_feature(dev
, i
+ 1, USB_PORT_FEAT_C_OVER_CURRENT
);
548 usb_hub_power_on(hub
);
551 if (portchange
& USB_PORT_STAT_C_RESET
) {
552 dbg("port %d reset change", i
+ 1);
553 usb_clear_port_feature(dev
, i
+ 1, USB_PORT_FEAT_C_RESET
);
557 /* deal with hub status changes */
558 if (usb_get_hub_status(dev
, &hubsts
) < 0) {
559 err("get_hub_status failed");
561 hubstatus
= le16_to_cpup(&hubsts
.wHubStatus
);
562 hubchange
= le16_to_cpup(&hubsts
.wHubChange
);
563 if (hubchange
& HUB_CHANGE_LOCAL_POWER
) {
564 dbg("hub power change");
565 usb_clear_hub_feature(dev
, C_HUB_LOCAL_POWER
);
567 if (hubchange
& HUB_CHANGE_OVERCURRENT
) {
568 dbg("hub overcurrent change");
569 wait_ms(500); //Cool down
570 usb_clear_hub_feature(dev
, C_HUB_OVER_CURRENT
);
571 usb_hub_power_on(hub
);
574 } /* end while (1) */
577 spin_unlock_irqrestore(&hub_event_lock
, flags
);
580 static int usb_hub_thread(void *__hub
)
587 * This thread doesn't need any user-level access,
588 * so get rid of all our resources
590 exit_files(current
); /* daemonize doesn't do exit_files */
591 current
->files
= init_task
.files
;
592 atomic_inc(¤t
->files
->count
);
595 /* Setup a nice name */
596 strcpy(current
->comm
, "khubd");
598 /* Send me a signal to get me die (for debugging) */
601 interruptible_sleep_on(&khubd_wait
);
602 } while (!signal_pending(current
));
604 dbg("usb_hub_thread exiting");
610 static struct usb_driver hub_driver
= {
614 disconnect
: hub_disconnect
618 * This should be a separate module.
620 int usb_hub_init(void)
624 if (usb_register(&hub_driver
) < 0) {
625 err("Unable to register USB hub driver");
629 pid
= kernel_thread(usb_hub_thread
, NULL
,
630 CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
);
637 /* Fall through if kernel_thread failed */
638 usb_deregister(&hub_driver
);
643 void usb_hub_cleanup(void)
647 /* Kill the thread */
648 ret
= kill_proc(khubd_pid
, SIGTERM
, 1);
650 /* Wait 10 seconds */
651 int count
= 10 * 100;
653 while (khubd_running
&& --count
) {
654 current
->state
= TASK_INTERRUPTIBLE
;
659 err("giving up on killing khubd");
663 * Hub resources are freed for us by usb_deregister. It
664 * usb_driver_purge on every device which in turn calls that
665 * devices disconnect function if it is using this driver.
666 * The hub_disconnect function takes care of releasing the
667 * individual hub resources. -greg
669 usb_deregister(&hub_driver
);
670 } /* usb_hub_cleanup() */
673 * WARNING - If a driver calls usb_reset_device, you should simulate a
674 * disconnect() and probe() for other interfaces you doesn't claim. This
675 * is left up to the driver writer right now. This insures other drivers
676 * have a chance to re-setup their interface.
678 * Take a look at proc_resetdevice in devio.c for some sample code to
681 int usb_reset_device(struct usb_device
*dev
)
683 struct usb_device
*parent
= dev
->parent
;
684 struct usb_device_descriptor descriptor
;
685 int i
, ret
, port
= -1;
688 err("attempting to reset root hub!");
692 for (i
= 0; i
< parent
->maxchild
; i
++)
693 if (parent
->children
[i
] == dev
) {
701 down(&usb_address0_sem
);
703 /* Send a reset to the device */
704 usb_set_port_feature(parent
, port
+ 1, USB_PORT_FEAT_RESET
);
708 usb_clear_port_feature(parent
, port
+ 1, USB_PORT_FEAT_C_RESET
);
710 /* Reprogram the Address */
711 ret
= usb_set_address(dev
);
713 err("USB device not accepting new address (error=%d)", ret
);
714 clear_bit(dev
->devnum
, &dev
->bus
->devmap
.devicemap
);
716 up(&usb_address0_sem
);
720 wait_ms(10); /* Let the SET_ADDRESS settle */
722 up(&usb_address0_sem
);
725 * Now we fetch the configuration descriptors for the device and
726 * see if anything has changed. If it has, we dump the current
727 * parsed descriptors and reparse from scratch. Then we leave
728 * the device alone for the caller to finish setting up.
730 * If nothing changed, we reprogram the configuration and then
731 * the alternate settings.
733 ret
= usb_get_descriptor(dev
, USB_DT_DEVICE
, 0, &descriptor
,
738 le16_to_cpus(&descriptor
.bcdUSB
);
739 le16_to_cpus(&descriptor
.idVendor
);
740 le16_to_cpus(&descriptor
.idProduct
);
741 le16_to_cpus(&descriptor
.bcdDevice
);
743 if (memcmp(&dev
->descriptor
, &descriptor
, sizeof(descriptor
))) {
744 usb_destroy_configuration(dev
);
746 ret
= usb_get_device_descriptor(dev
);
747 if (ret
< sizeof(dev
->descriptor
)) {
749 err("unable to get device descriptor (error=%d)", ret
);
751 err("USB device descriptor short read (expected %i, got %i)", sizeof(dev
->descriptor
), ret
);
753 clear_bit(dev
->devnum
, &dev
->bus
->devmap
.devicemap
);
758 ret
= usb_get_configuration(dev
);
760 err("unable to get configuration (error=%d)", ret
);
761 clear_bit(dev
->devnum
, &dev
->bus
->devmap
.devicemap
);
766 dev
->actconfig
= dev
->config
;
767 usb_set_maxpacket(dev
);
771 ret
= usb_set_configuration(dev
,
772 dev
->actconfig
->bConfigurationValue
);
774 err("failed to set active configuration (error=%d)",
779 for (i
= 0; i
< dev
->actconfig
->bNumInterfaces
; i
++) {
780 struct usb_interface
*intf
=
781 &dev
->actconfig
->interface
[i
];
782 struct usb_interface_descriptor
*as
=
783 &intf
->altsetting
[intf
->act_altsetting
];
785 ret
= usb_set_interface(dev
, as
->bInterfaceNumber
,
786 as
->bAlternateSetting
);
788 err("failed to set active alternate setting for interface %d (error=%d)", i
, ret
);