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/module.h>
12 #include <linux/sched.h>
13 #include <linux/list.h>
14 #include <linux/malloc.h>
15 #include <linux/smp_lock.h>
16 #ifdef CONFIG_USB_DEBUG
21 #include <linux/usb.h>
22 #include <linux/usbdevice_fs.h>
24 #include <asm/semaphore.h>
25 #include <asm/uaccess.h>
26 #include <asm/byteorder.h>
31 static spinlock_t hub_event_lock
= SPIN_LOCK_UNLOCKED
;
32 static DECLARE_MUTEX(usb_address0_sem
);
34 static LIST_HEAD(hub_event_list
); /* List of hubs needing servicing */
35 static LIST_HEAD(hub_list
); /* List containing all of the hubs (for cleanup) */
37 static DECLARE_WAIT_QUEUE_HEAD(khubd_wait
);
38 static int khubd_pid
= 0; /* PID of khubd */
39 static DECLARE_MUTEX_LOCKED(khubd_exited
);
41 static int usb_get_hub_descriptor(struct usb_device
*dev
, void *data
, int size
)
43 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
44 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
| USB_RT_HUB
,
45 USB_DT_HUB
<< 8, 0, data
, size
, HZ
);
48 static int usb_clear_hub_feature(struct usb_device
*dev
, int feature
)
50 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
51 USB_REQ_CLEAR_FEATURE
, USB_RT_HUB
, feature
, 0, NULL
, 0, HZ
);
54 static int usb_clear_port_feature(struct usb_device
*dev
, int port
, int feature
)
56 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
57 USB_REQ_CLEAR_FEATURE
, USB_RT_PORT
, feature
, port
, NULL
, 0, HZ
);
60 static int usb_set_port_feature(struct usb_device
*dev
, int port
, int feature
)
62 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
63 USB_REQ_SET_FEATURE
, USB_RT_PORT
, feature
, port
, NULL
, 0, HZ
);
66 static int usb_get_hub_status(struct usb_device
*dev
, void *data
)
68 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
69 USB_REQ_GET_STATUS
, USB_DIR_IN
| USB_RT_HUB
, 0, 0,
70 data
, sizeof(struct usb_hub_status
), HZ
);
73 static int usb_get_port_status(struct usb_device
*dev
, int port
, void *data
)
75 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
76 USB_REQ_GET_STATUS
, USB_DIR_IN
| USB_RT_PORT
, 0, port
,
77 data
, sizeof(struct usb_hub_status
), HZ
);
80 static void hub_irq(struct urb
*urb
)
82 struct usb_hub
*hub
= (struct usb_hub
*)urb
->context
;
85 /* Cause a hub reset after 10 consecutive errors */
87 if (urb
->status
== -ENOENT
)
90 dbg("nonzero status in irq %d", urb
->status
);
92 if ((++hub
->nerrors
< 10) || hub
->error
)
95 hub
->error
= urb
->status
;
100 /* Something happened, let khubd figure it out */
101 if (waitqueue_active(&khubd_wait
)) {
102 /* Add the hub to the event queue */
103 spin_lock_irqsave(&hub_event_lock
, flags
);
104 if (list_empty(&hub
->event_list
)) {
105 list_add(&hub
->event_list
, &hub_event_list
);
106 wake_up(&khubd_wait
);
108 spin_unlock_irqrestore(&hub_event_lock
, flags
);
112 static void usb_hub_power_on(struct usb_hub
*hub
)
116 /* Enable power to the ports */
117 dbg("enabling power on all ports");
118 for (i
= 0; i
< hub
->nports
; i
++)
119 usb_set_port_feature(hub
->dev
, i
+ 1, USB_PORT_FEAT_POWER
);
121 /* Wait for power to be enabled */
122 wait_ms(hub
->descriptor
->bPwrOn2PwrGood
* 2);
125 static int usb_hub_configure(struct usb_hub
*hub
, struct usb_endpoint_descriptor
*endpoint
)
127 struct usb_device
*dev
= hub
->dev
;
128 struct usb_hub_status hubstatus
;
129 char portstr
[USB_MAXCHILDREN
+ 1];
133 hub
->descriptor
= kmalloc(HUB_DESCRIPTOR_MAX_SIZE
, GFP_KERNEL
);
134 if (!hub
->descriptor
) {
135 err("Unable to kmalloc %d bytes for hub descriptor", HUB_DESCRIPTOR_MAX_SIZE
);
139 /* Request the entire hub descriptor. */
140 ret
= usb_get_hub_descriptor(dev
, hub
->descriptor
, HUB_DESCRIPTOR_MAX_SIZE
);
141 /* <hub->descriptor> is large enough for a hub with 127 ports;
142 * the hub can/will return fewer bytes here. */
144 err("Unable to get hub descriptor (err = %d)", ret
);
145 kfree(hub
->descriptor
);
149 hub
->nports
= dev
->maxchild
= hub
->descriptor
->bNbrPorts
;
150 info("%d port%s detected", hub
->nports
, (hub
->nports
== 1) ? "" : "s");
152 if (hub
->descriptor
->wHubCharacteristics
& HUB_CHAR_COMPOUND
)
153 dbg("part of a compound device");
155 dbg("standalone hub");
157 switch (hub
->descriptor
->wHubCharacteristics
& HUB_CHAR_LPSM
) {
159 dbg("ganged power switching");
162 dbg("individual port power switching");
166 dbg("unknown reserved power switching mode");
170 switch (hub
->descriptor
->wHubCharacteristics
& HUB_CHAR_OCPM
) {
172 dbg("global over-current protection");
175 dbg("individual port over-current protection");
179 dbg("no over-current protection");
183 dbg("power on to power good time: %dms", hub
->descriptor
->bPwrOn2PwrGood
* 2);
184 dbg("hub controller current requirement: %dmA", hub
->descriptor
->bHubContrCurrent
);
186 for (i
= 0; i
< dev
->maxchild
; i
++)
187 portstr
[i
] = hub
->descriptor
->bitmap
[((i
+ 1) / 8)] & (1 << ((i
+ 1) % 8)) ? 'F' : 'R';
188 portstr
[dev
->maxchild
] = 0;
190 dbg("port removable status: %s", portstr
);
192 ret
= usb_get_hub_status(dev
, &hubstatus
);
194 err("Unable to get hub status (err = %d)", ret
);
195 kfree(hub
->descriptor
);
199 le16_to_cpus(&hubstatus
.wHubStatus
);
201 dbg("local power source is %s",
202 (hubstatus
.wHubStatus
& HUB_STATUS_LOCAL_POWER
) ? "lost (inactive)" : "good");
204 dbg("%sover-current condition exists",
205 (hubstatus
.wHubStatus
& HUB_STATUS_OVERCURRENT
) ? "" : "no ");
207 /* Start the interrupt endpoint */
208 pipe
= usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
);
209 maxp
= usb_maxpacket(dev
, pipe
, usb_pipeout(pipe
));
211 if (maxp
> sizeof(hub
->buffer
))
212 maxp
= sizeof(hub
->buffer
);
214 hub
->urb
= usb_alloc_urb(0);
216 err("couldn't allocate interrupt urb");
217 kfree(hub
->descriptor
);
221 FILL_INT_URB(hub
->urb
, dev
, pipe
, hub
->buffer
, maxp
, hub_irq
,
222 hub
, endpoint
->bInterval
);
223 ret
= usb_submit_urb(hub
->urb
);
225 err("usb_submit_urb failed (%d)", ret
);
226 kfree(hub
->descriptor
);
231 wake_up(&khubd_wait
);
233 usb_hub_power_on(hub
);
238 static void *hub_probe(struct usb_device
*dev
, unsigned int i
,
239 const struct usb_device_id
*id
)
242 struct usb_interface_descriptor
*interface
;
243 struct usb_endpoint_descriptor
*endpoint
;
247 interface
= &dev
->actconfig
->interface
[i
].altsetting
[0];
249 /* Some hubs have a subclass of 1, which AFAICT according to the */
250 /* specs is not defined, but it works */
251 if ((interface
->bInterfaceSubClass
!= 0) &&
252 (interface
->bInterfaceSubClass
!= 1)) {
253 err("invalid subclass (%d) for USB hub device #%d",
254 interface
->bInterfaceSubClass
, dev
->devnum
);
258 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
259 if (interface
->bNumEndpoints
!= 1) {
260 err("invalid bNumEndpoints (%d) for USB hub device #%d",
261 interface
->bNumEndpoints
, dev
->devnum
);
265 endpoint
= &interface
->endpoint
[0];
267 /* Output endpoint? Curiousier and curiousier.. */
268 if (!(endpoint
->bEndpointAddress
& USB_DIR_IN
)) {
269 err("Device #%d is hub class, but has output endpoint?",
274 /* If it's not an interrupt endpoint, we'd better punt! */
275 if ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_INT
) {
276 err("Device #%d is hub class, but has endpoint other than interrupt?",
282 info("USB hub found");
284 hub
= kmalloc(sizeof(*hub
), GFP_KERNEL
);
286 err("couldn't kmalloc hub struct");
290 memset(hub
, 0, sizeof(*hub
));
292 INIT_LIST_HEAD(&hub
->event_list
);
295 /* Record the new hub's existence */
296 spin_lock_irqsave(&hub_event_lock
, flags
);
297 INIT_LIST_HEAD(&hub
->hub_list
);
298 list_add(&hub
->hub_list
, &hub_list
);
299 spin_unlock_irqrestore(&hub_event_lock
, flags
);
301 if (usb_hub_configure(hub
, endpoint
) >= 0)
304 err("hub configuration failed for device #%d", dev
->devnum
);
306 /* free hub, but first clean up its list. */
307 spin_lock_irqsave(&hub_event_lock
, flags
);
309 /* Delete it and then reset it */
310 list_del(&hub
->event_list
);
311 INIT_LIST_HEAD(&hub
->event_list
);
312 list_del(&hub
->hub_list
);
313 INIT_LIST_HEAD(&hub
->hub_list
);
315 spin_unlock_irqrestore(&hub_event_lock
, flags
);
322 static void hub_disconnect(struct usb_device
*dev
, void *ptr
)
324 struct usb_hub
*hub
= (struct usb_hub
*)ptr
;
327 spin_lock_irqsave(&hub_event_lock
, flags
);
329 /* Delete it and then reset it */
330 list_del(&hub
->event_list
);
331 INIT_LIST_HEAD(&hub
->event_list
);
332 list_del(&hub
->hub_list
);
333 INIT_LIST_HEAD(&hub
->hub_list
);
335 spin_unlock_irqrestore(&hub_event_lock
, flags
);
338 usb_unlink_urb(hub
->urb
);
339 usb_free_urb(hub
->urb
);
343 if (hub
->descriptor
) {
344 kfree(hub
->descriptor
);
345 hub
->descriptor
= NULL
;
348 /* Free the memory */
352 static int hub_ioctl(struct usb_device
*hub
, unsigned int code
, void *user_data
)
354 /* assert ifno == 0 (part of hub spec) */
356 case USBDEVFS_HUB_PORTINFO
: {
357 struct usbdevfs_hub_portinfo
*info
= user_data
;
361 spin_lock_irqsave(&hub_event_lock
, flags
);
362 if (hub
->devnum
<= 0)
365 info
->nports
= hub
->maxchild
;
366 for (i
= 0; i
< info
->nports
; i
++) {
367 if (hub
->children
[i
] == NULL
)
370 info
->port
[i
] = hub
->children
[i
]->devnum
;
373 spin_unlock_irqrestore(&hub_event_lock
, flags
);
375 return info
->nports
+ 1;
383 static int usb_hub_reset(struct usb_hub
*hub
)
385 struct usb_device
*dev
= hub
->dev
;
388 /* Disconnect any attached devices */
389 for (i
= 0; i
< hub
->nports
; i
++) {
390 if (dev
->children
[i
])
391 usb_disconnect(&dev
->children
[i
]);
394 /* Attempt to reset the hub */
396 usb_unlink_urb(hub
->urb
);
400 if (usb_reset_device(dev
))
403 if (usb_submit_urb(hub
->urb
))
406 usb_hub_power_on(hub
);
411 static void usb_hub_disconnect(struct usb_device
*dev
)
413 struct usb_device
*parent
= dev
->parent
;
416 /* Find the device pointer to disconnect */
418 for (i
= 0; i
< parent
->maxchild
; i
++) {
419 if (parent
->children
[i
] == dev
) {
420 usb_disconnect(&parent
->children
[i
]);
426 err("cannot disconnect hub %d", dev
->devnum
);
429 #define HUB_RESET_TRIES 5
430 #define HUB_PROBE_TRIES 2
431 #define HUB_SHORT_RESET_TIME 10
432 #define HUB_LONG_RESET_TIME 200
433 #define HUB_RESET_TIMEOUT 500
435 static int usb_hub_port_wait_reset(struct usb_device
*hub
, int port
,
436 struct usb_device
*dev
, unsigned int delay
)
439 struct usb_port_status portsts
;
440 unsigned short portchange
, portstatus
;
442 for (delay_time
= 0; delay_time
< HUB_RESET_TIMEOUT
; delay_time
+= delay
) {
443 /* wait to give the device a chance to reset */
446 /* read and decode port status */
447 ret
= usb_get_port_status(hub
, port
+ 1, &portsts
);
449 err("get_port_status(%d) failed (err = %d)", port
+ 1, ret
);
453 portstatus
= le16_to_cpu(portsts
.wPortStatus
);
454 portchange
= le16_to_cpu(portsts
.wPortChange
);
455 dbg("port %d, portstatus %x, change %x, %s", port
+ 1,
456 portstatus
, portchange
,
457 portstatus
& (1 << USB_PORT_FEAT_LOWSPEED
) ? "1.5 Mb/s" : "12 Mb/s");
459 /* bomb out completely if something weird happened */
460 if ((portchange
& USB_PORT_STAT_C_CONNECTION
) ||
461 !(portstatus
& USB_PORT_STAT_CONNECTION
))
464 /* if we`ve finished resetting, then break out of the loop */
465 if (!(portstatus
& USB_PORT_STAT_RESET
) &&
466 (portstatus
& USB_PORT_STAT_ENABLE
)) {
467 dev
->slow
= (portstatus
& USB_PORT_STAT_LOW_SPEED
) ? 1 : 0;
471 /* switch to the long delay after two short delay failures */
472 if (delay_time
>= 2 * HUB_SHORT_RESET_TIME
)
473 delay
= HUB_LONG_RESET_TIME
;
475 dbg("port %d of hub %d not reset yet, waiting %dms", port
+ 1,
482 static int usb_hub_port_reset(struct usb_device
*hub
, int port
,
483 struct usb_device
*dev
, unsigned int delay
)
488 for (i
= 0; i
< HUB_RESET_TRIES
; i
++) {
489 usb_set_port_feature(hub
, port
+ 1, USB_PORT_FEAT_RESET
);
491 /* return success if the port reset OK */
492 if (!usb_hub_port_wait_reset(hub
, port
, dev
, delay
)) {
493 usb_clear_port_feature(hub
, port
+ 1, USB_PORT_FEAT_C_RESET
);
497 dbg("port %d of hub %d not enabled, trying reset again...",
498 port
+ 1, hub
->devnum
);
499 delay
= HUB_LONG_RESET_TIME
;
502 err("Cannot enable port %i of hub %d, disabling port.",
503 port
+ 1, hub
->devnum
);
504 err("Maybe the USB cable is bad?");
509 void usb_hub_port_disable(struct usb_device
*hub
, int port
)
513 ret
= usb_clear_port_feature(hub
, port
+ 1, USB_PORT_FEAT_ENABLE
);
515 err("cannot disable port %d of hub %d (err = %d)",
516 port
+ 1, hub
->devnum
, ret
);
519 static void usb_hub_port_connect_change(struct usb_device
*hub
, int port
,
520 struct usb_port_status
*portsts
)
522 struct usb_device
*dev
;
523 unsigned short portstatus
, portchange
;
524 unsigned int delay
= HUB_SHORT_RESET_TIME
;
526 char *portstr
, *tempstr
;
528 portstatus
= le16_to_cpu(portsts
->wPortStatus
);
529 portchange
= le16_to_cpu(portsts
->wPortChange
);
530 dbg("port %d, portstatus %x, change %x, %s", port
+ 1, portstatus
,
531 portchange
, portstatus
& (1 << USB_PORT_FEAT_LOWSPEED
) ? "1.5 Mb/s" : "12 Mb/s");
533 /* Clear the connection change status */
534 usb_clear_port_feature(hub
, port
+ 1, USB_PORT_FEAT_C_CONNECTION
);
536 /* Disconnect any existing devices under this port */
537 if (hub
->children
[port
])
538 usb_disconnect(&hub
->children
[port
]);
540 /* Return now if nothing is connected */
541 if (!(portstatus
& USB_PORT_STAT_CONNECTION
)) {
542 if (portstatus
& USB_PORT_STAT_ENABLE
)
543 usb_hub_port_disable(hub
, port
);
548 down(&usb_address0_sem
);
550 tempstr
= kmalloc(1024, GFP_KERNEL
);
551 portstr
= kmalloc(1024, GFP_KERNEL
);
553 for (i
= 0; i
< HUB_PROBE_TRIES
; i
++) {
554 struct usb_device
*pdev
, *cdev
;
556 /* Allocate a new device struct */
557 dev
= usb_alloc_dev(hub
, hub
->bus
);
559 err("couldn't allocate usb_device");
563 hub
->children
[port
] = dev
;
565 /* Reset the device */
566 if (usb_hub_port_reset(hub
, port
, dev
, delay
)) {
571 /* Find a new device ID for it */
574 /* Create a readable topology string */
577 if (portstr
&& tempstr
) {
582 for (port
= 0; port
< pdev
->maxchild
; port
++)
583 if (pdev
->children
[port
] == cdev
)
586 strcpy(tempstr
, portstr
);
587 if (!strlen(tempstr
))
588 sprintf(portstr
, "%d", port
+ 1);
590 sprintf(portstr
, "%d/%s", port
+ 1, tempstr
);
595 info("USB new device connect on bus%d/%s, assigned device number %d",
596 dev
->bus
->busnum
, portstr
, dev
->devnum
);
598 info("USB new device connect on bus%d, assigned device number %d",
599 dev
->bus
->busnum
, dev
->devnum
);
601 /* Run it through the hoops (find a driver, etc) */
602 if (!usb_new_device(dev
))
605 /* Free the configuration if there was an error */
608 /* Switch to a long reset time */
609 delay
= HUB_LONG_RESET_TIME
;
612 hub
->children
[port
] = NULL
;
613 usb_hub_port_disable(hub
, port
);
615 up(&usb_address0_sem
);
622 static void usb_hub_events(void)
625 struct list_head
*tmp
;
626 struct usb_device
*dev
;
628 struct usb_hub_status hubsts
;
629 unsigned short hubstatus
, hubchange
;
633 * We restart the list everytime to avoid a deadlock with
634 * deleting hubs downstream from this one. This should be
635 * safe since we delete the hub from the event list.
636 * Not the most efficient, but avoids deadlocks.
639 spin_lock_irqsave(&hub_event_lock
, flags
);
641 if (list_empty(&hub_event_list
))
644 /* Grab the next entry from the beginning of the list */
645 tmp
= hub_event_list
.next
;
647 hub
= list_entry(tmp
, struct usb_hub
, event_list
);
653 spin_unlock_irqrestore(&hub_event_lock
, flags
);
656 dbg("resetting hub %d for error %d", dev
->devnum
, hub
->error
);
658 if (usb_hub_reset(hub
)) {
659 err("error resetting hub %d - disconnecting", dev
->devnum
);
660 usb_hub_disconnect(dev
);
668 for (i
= 0; i
< hub
->nports
; i
++) {
669 struct usb_port_status portsts
;
670 unsigned short portstatus
, portchange
;
672 ret
= usb_get_port_status(dev
, i
+ 1, &portsts
);
674 err("get_port_status failed (err = %d)", ret
);
678 portstatus
= le16_to_cpu(portsts
.wPortStatus
);
679 portchange
= le16_to_cpu(portsts
.wPortChange
);
681 if (portchange
& USB_PORT_STAT_C_CONNECTION
) {
682 dbg("port %d connection change", i
+ 1);
684 usb_hub_port_connect_change(dev
, i
, &portsts
);
685 } else if (portchange
& USB_PORT_STAT_C_ENABLE
) {
686 dbg("port %d enable change, status %x", i
+ 1, portstatus
);
687 usb_clear_port_feature(dev
, i
+ 1, USB_PORT_FEAT_C_ENABLE
);
690 * EM interference sometimes causes bad shielded USB devices to
691 * be shutdown by the hub, this hack enables them again.
692 * Works at least with mouse driver.
694 if (!(portstatus
& USB_PORT_STAT_ENABLE
) &&
695 (portstatus
& USB_PORT_STAT_CONNECTION
) && (dev
->children
[i
])) {
696 err("already running port %i disabled by hub (EMI?), re-enabling...",
698 usb_hub_port_connect_change(dev
, i
, &portsts
);
702 if (portchange
& USB_PORT_STAT_C_SUSPEND
) {
703 dbg("port %d suspend change", i
+ 1);
704 usb_clear_port_feature(dev
, i
+ 1, USB_PORT_FEAT_C_SUSPEND
);
707 if (portchange
& USB_PORT_STAT_C_OVERCURRENT
) {
708 err("port %d over-current change", i
+ 1);
709 usb_clear_port_feature(dev
, i
+ 1, USB_PORT_FEAT_C_OVER_CURRENT
);
710 usb_hub_power_on(hub
);
713 if (portchange
& USB_PORT_STAT_C_RESET
) {
714 dbg("port %d reset change", i
+ 1);
715 usb_clear_port_feature(dev
, i
+ 1, USB_PORT_FEAT_C_RESET
);
719 /* deal with hub status changes */
720 if (usb_get_hub_status(dev
, &hubsts
) < 0)
721 err("get_hub_status failed");
723 hubstatus
= le16_to_cpup(&hubsts
.wHubStatus
);
724 hubchange
= le16_to_cpup(&hubsts
.wHubChange
);
725 if (hubchange
& HUB_CHANGE_LOCAL_POWER
) {
726 dbg("hub power change");
727 usb_clear_hub_feature(dev
, C_HUB_LOCAL_POWER
);
729 if (hubchange
& HUB_CHANGE_OVERCURRENT
) {
730 dbg("hub overcurrent change");
731 wait_ms(500); /* Cool down */
732 usb_clear_hub_feature(dev
, C_HUB_OVER_CURRENT
);
733 usb_hub_power_on(hub
);
736 } /* end while (1) */
739 spin_unlock_irqrestore(&hub_event_lock
, flags
);
742 static int usb_hub_thread(void *__hub
)
747 * This thread doesn't need any user-level access,
748 * so get rid of all our resources
753 /* Setup a nice name */
754 strcpy(current
->comm
, "khubd");
756 /* Send me a signal to get me die (for debugging) */
759 interruptible_sleep_on(&khubd_wait
);
760 } while (!signal_pending(current
));
762 dbg("usb_hub_thread exiting");
764 up_and_exit(&khubd_exited
, 0);
767 static struct usb_device_id hub_id_table
[] = {
768 { bInterfaceClass
: USB_CLASS_HUB
},
769 { } /* Terminating entry */
772 MODULE_DEVICE_TABLE (usb
, hub_id_table
);
774 static struct usb_driver hub_driver
= {
778 disconnect
: hub_disconnect
,
779 id_table
: hub_id_table
,
783 * This should be a separate module.
785 int usb_hub_init(void)
789 if (usb_register(&hub_driver
) < 0) {
790 err("Unable to register USB hub driver");
794 pid
= kernel_thread(usb_hub_thread
, NULL
,
795 CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
);
802 /* Fall through if kernel_thread failed */
803 usb_deregister(&hub_driver
);
804 err("failed to start usb_hub_thread");
809 void usb_hub_cleanup(void)
813 /* Kill the thread */
814 ret
= kill_proc(khubd_pid
, SIGTERM
, 1);
819 * Hub resources are freed for us by usb_deregister. It calls
820 * usb_driver_purge on every device which in turn calls that
821 * devices disconnect function if it is using this driver.
822 * The hub_disconnect function takes care of releasing the
823 * individual hub resources. -greg
825 usb_deregister(&hub_driver
);
826 } /* usb_hub_cleanup() */
829 * WARNING - If a driver calls usb_reset_device, you should simulate a
830 * disconnect() and probe() for other interfaces you doesn't claim. This
831 * is left up to the driver writer right now. This insures other drivers
832 * have a chance to re-setup their interface.
834 * Take a look at proc_resetdevice in devio.c for some sample code to
837 int usb_reset_device(struct usb_device
*dev
)
839 struct usb_device
*parent
= dev
->parent
;
840 struct usb_device_descriptor descriptor
;
841 int i
, ret
, port
= -1;
844 err("attempting to reset root hub!");
848 for (i
= 0; i
< parent
->maxchild
; i
++)
849 if (parent
->children
[i
] == dev
) {
857 down(&usb_address0_sem
);
859 /* Send a reset to the device */
860 if (usb_hub_port_reset(parent
, port
, dev
, HUB_SHORT_RESET_TIME
)) {
861 usb_hub_port_disable(parent
, port
);
862 up(&usb_address0_sem
);
866 /* Reprogram the Address */
867 ret
= usb_set_address(dev
);
869 err("USB device not accepting new address (error=%d)", ret
);
870 usb_hub_port_disable(parent
, port
);
871 up(&usb_address0_sem
);
875 /* Let the SET_ADDRESS settle */
878 up(&usb_address0_sem
);
881 * Now we fetch the configuration descriptors for the device and
882 * see if anything has changed. If it has, we dump the current
883 * parsed descriptors and reparse from scratch. Then we leave
884 * the device alone for the caller to finish setting up.
886 * If nothing changed, we reprogram the configuration and then
887 * the alternate settings.
889 ret
= usb_get_descriptor(dev
, USB_DT_DEVICE
, 0, &descriptor
,
894 le16_to_cpus(&descriptor
.bcdUSB
);
895 le16_to_cpus(&descriptor
.idVendor
);
896 le16_to_cpus(&descriptor
.idProduct
);
897 le16_to_cpus(&descriptor
.bcdDevice
);
899 if (memcmp(&dev
->descriptor
, &descriptor
, sizeof(descriptor
))) {
900 usb_destroy_configuration(dev
);
902 ret
= usb_get_device_descriptor(dev
);
903 if (ret
< sizeof(dev
->descriptor
)) {
905 err("unable to get device descriptor (error=%d)", ret
);
907 err("USB device descriptor short read (expected %i, got %i)", sizeof(dev
->descriptor
), ret
);
909 clear_bit(dev
->devnum
, &dev
->bus
->devmap
.devicemap
);
914 ret
= usb_get_configuration(dev
);
916 err("unable to get configuration (error=%d)", ret
);
917 usb_destroy_configuration(dev
);
918 clear_bit(dev
->devnum
, &dev
->bus
->devmap
.devicemap
);
923 dev
->actconfig
= dev
->config
;
924 usb_set_maxpacket(dev
);
929 ret
= usb_set_configuration(dev
, dev
->actconfig
->bConfigurationValue
);
931 err("failed to set active configuration (error=%d)", ret
);
935 for (i
= 0; i
< dev
->actconfig
->bNumInterfaces
; i
++) {
936 struct usb_interface
*intf
= &dev
->actconfig
->interface
[i
];
937 struct usb_interface_descriptor
*as
= &intf
->altsetting
[intf
->act_altsetting
];
939 ret
= usb_set_interface(dev
, as
->bInterfaceNumber
, as
->bAlternateSetting
);
941 err("failed to set active alternate setting for interface %d (error=%d)", i
, ret
);