4 * (C) Copyright 1999 Linus Torvalds
5 * (C) Copyright 1999 Johannes Erdfelt
6 * (C) Copyright 1999 Gregory P. Smith
7 * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
11 #include <linux/config.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/module.h>
15 #include <linux/completion.h>
16 #include <linux/sched.h>
17 #include <linux/list.h>
18 #include <linux/slab.h>
19 #include <linux/smp_lock.h>
20 #include <linux/ioctl.h>
21 #ifdef CONFIG_USB_DEBUG
26 #include <linux/usb.h>
27 #include <linux/usbdevice_fs.h>
28 #include <linux/suspend.h>
30 #include <asm/semaphore.h>
31 #include <asm/uaccess.h>
32 #include <asm/byteorder.h>
39 #include "../miniport/usb_wrapper.h"
46 static spinlock_t hub_event_lock
;
47 //static DECLARE_MUTEX(usb_address0_sem);
49 static LIST_HEAD(hub_event_list
); /* List of hubs needing servicing */
50 static LIST_HEAD(hub_list
); /* List of all hubs (for cleanup) */
52 static DECLARE_WAIT_QUEUE_HEAD(khubd_wait
);
53 static pid_t khubd_pid
= 0; /* PID of khubd */
54 static DECLARE_COMPLETION(khubd_exited
);
57 static inline char *portspeed (int portstatus
)
59 if (portstatus
& (1 << USB_PORT_FEAT_HIGHSPEED
))
61 else if (portstatus
& (1 << USB_PORT_FEAT_LOWSPEED
))
68 /* for dev_info, dev_dbg, etc */
69 static inline struct device
*hubdev (struct usb_device
*dev
)
71 return &dev
->actconfig
->interface
[0].dev
;
74 /* USB 2.0 spec Section 11.24.4.5 */
75 static int get_hub_descriptor(struct usb_device
*dev
, void *data
, int size
)
77 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
78 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
| USB_RT_HUB
,
79 USB_DT_HUB
<< 8, 0, data
, size
, HZ
* USB_CTRL_GET_TIMEOUT
);
83 * USB 2.0 spec Section 11.24.2.1
85 static int clear_hub_feature(struct usb_device
*dev
, int feature
)
87 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
88 USB_REQ_CLEAR_FEATURE
, USB_RT_HUB
, feature
, 0, NULL
, 0, HZ
);
92 * USB 2.0 spec Section 11.24.2.2
93 * BUG: doesn't handle port indicator selector in high byte of wIndex
95 static int clear_port_feature(struct usb_device
*dev
, int port
, int feature
)
97 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
98 USB_REQ_CLEAR_FEATURE
, USB_RT_PORT
, feature
, port
, NULL
, 0, HZ
);
102 * USB 2.0 spec Section 11.24.2.13
103 * BUG: doesn't handle port indicator selector in high byte of wIndex
105 static int set_port_feature(struct usb_device
*dev
, int port
, int feature
)
107 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
108 USB_REQ_SET_FEATURE
, USB_RT_PORT
, feature
, port
, NULL
, 0, HZ
);
112 * USB 2.0 spec Section 11.24.2.6
114 static int get_hub_status(struct usb_device
*dev
,
115 struct usb_hub_status
*data
)
117 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
118 USB_REQ_GET_STATUS
, USB_DIR_IN
| USB_RT_HUB
, 0, 0,
119 data
, sizeof(*data
), HZ
* USB_CTRL_GET_TIMEOUT
);
123 * USB 2.0 spec Section 11.24.2.7
125 static int get_port_status(struct usb_device
*dev
, int port
,
126 struct usb_port_status
*data
)
128 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
129 USB_REQ_GET_STATUS
, USB_DIR_IN
| USB_RT_PORT
, 0, port
,
130 data
, sizeof(*data
), HZ
* USB_CTRL_GET_TIMEOUT
);
133 /* completion function, fires on port status changes and various faults */
134 static void hub_irq(struct urb
*urb
, struct pt_regs
*regs
)
136 struct usb_hub
*hub
= (struct usb_hub
*)urb
->context
;
140 switch (urb
->status
) {
141 case -ENOENT
: /* synchronous unlink */
142 case -ECONNRESET
: /* async unlink */
143 case -ESHUTDOWN
: /* hardware going away */
146 if (hub
->RestCounter
>0) {
147 // we already resetted one time ...
154 default: /* presumably an error */
155 /* Cause a hub reset after 10 consecutive errors */
156 printk("hub_irq got ...: error %d URB: %d",hub
->error
,urb
->status
);
158 dev_dbg (&hub
->intf
->dev
, "transfer --> %d\n", urb
->status
);
159 if ((++hub
->nerrors
< 10) || hub
->error
)
161 hub
->error
= urb
->status
;
165 /* let khubd handle things */
166 case 0: /* we got data: port status changed */
173 /* Something happened, let khubd figure it out */
174 spin_lock_irqsave(&hub_event_lock
, flags
);
175 if (list_empty(&hub
->event_list
)) {
176 list_add(&hub
->event_list
, &hub_event_list
);
177 wake_up(&khubd_wait
);
179 spin_unlock_irqrestore(&hub_event_lock
, flags
);
182 if ((status
= usb_submit_urb (hub
->urb
, GFP_ATOMIC
)) != 0
183 /* ENODEV means we raced disconnect() */
184 && status
!= -ENODEV
)
185 dev_err (&hub
->intf
->dev
, "resubmit --> %d\n", urb
->status
);
188 /* USB 2.0 spec Section 11.24.2.3 */
190 hub_clear_tt_buffer (struct usb_device
*hub
, u16 devinfo
, u16 tt
)
192 return usb_control_msg (hub
, usb_rcvctrlpipe (hub
, 0),
193 HUB_CLEAR_TT_BUFFER
, USB_DIR_IN
| USB_RECIP_OTHER
,
194 devinfo
, tt
, 0, 0, HZ
);
198 * enumeration blocks khubd for a long time. we use keventd instead, since
199 * long blocking there is the exception, not the rule. accordingly, HCDs
200 * talking to TTs must queue control transfers (not just bulk and iso), so
201 * both can talk to the same hub concurrently.
203 static void hub_tt_kevent (void *arg
)
205 struct usb_hub
*hub
= arg
;
208 spin_lock_irqsave (&hub
->tt
.lock
, flags
);
209 while (!list_empty (&hub
->tt
.clear_list
)) {
210 struct list_head
*temp
;
211 struct usb_tt_clear
*clear
;
212 struct usb_device
*dev
;
215 temp
= hub
->tt
.clear_list
.next
;
216 clear
= list_entry (temp
, struct usb_tt_clear
, clear_list
);
217 list_del (&clear
->clear_list
);
219 /* drop lock so HCD can concurrently report other TT errors */
220 spin_unlock_irqrestore (&hub
->tt
.lock
, flags
);
221 dev
= interface_to_usbdev (hub
->intf
);
222 status
= hub_clear_tt_buffer (dev
, clear
->devinfo
, clear
->tt
);
223 spin_lock_irqsave (&hub
->tt
.lock
, flags
);
226 err ("usb-%s-%s clear tt %d (%04x) error %d",
227 dev
->bus
->bus_name
, dev
->devpath
,
228 clear
->tt
, clear
->devinfo
, status
);
231 spin_unlock_irqrestore (&hub
->tt
.lock
, flags
);
235 * usb_hub_tt_clear_buffer - clear control/bulk TT state in high speed hub
236 * @dev: the device whose split transaction failed
237 * @pipe: identifies the endpoint of the failed transaction
239 * High speed HCDs use this to tell the hub driver that some split control or
240 * bulk transaction failed in a way that requires clearing internal state of
241 * a transaction translator. This is normally detected (and reported) from
244 * It may not be possible for that hub to handle additional full (or low)
245 * speed transactions until that state is fully cleared out.
247 void usb_hub_tt_clear_buffer (struct usb_device
*dev
, int pipe
)
249 struct usb_tt
*tt
= dev
->tt
;
251 struct usb_tt_clear
*clear
;
253 /* we've got to cope with an arbitrary number of pending TT clears,
254 * since each TT has "at least two" buffers that can need it (and
255 * there can be many TTs per hub). even if they're uncommon.
257 if ((clear
= kmalloc (sizeof *clear
, SLAB_ATOMIC
)) == 0) {
258 err ("can't save CLEAR_TT_BUFFER state for hub at usb-%s-%s",
259 dev
->bus
->bus_name
, tt
->hub
->devpath
);
260 /* FIXME recover somehow ... RESET_TT? */
264 /* info that CLEAR_TT_BUFFER needs */
265 clear
->tt
= tt
->multi
? dev
->ttport
: 1;
266 clear
->devinfo
= usb_pipeendpoint (pipe
);
267 clear
->devinfo
|= dev
->devnum
<< 4;
268 clear
->devinfo
|= usb_pipecontrol (pipe
)
269 ? (USB_ENDPOINT_XFER_CONTROL
<< 11)
270 : (USB_ENDPOINT_XFER_BULK
<< 11);
271 if (usb_pipein (pipe
))
272 clear
->devinfo
|= 1 << 15;
274 /* tell keventd to clear state for this TT */
275 spin_lock_irqsave (&tt
->lock
, flags
);
276 list_add_tail (&clear
->clear_list
, &tt
->clear_list
);
277 schedule_work (&tt
->kevent
);
278 spin_unlock_irqrestore (&tt
->lock
, flags
);
281 static void hub_power_on(struct usb_hub
*hub
)
283 struct usb_device
*dev
;
288 DelayAfterPort
= hub
->descriptor
->bPwrOn2PwrGood
* 2;
291 switch (hub
->descriptor
->wHubCharacteristics
& HUB_CHAR_LPSM
) {
293 DelayAfterPort
= hub
->descriptor
->bPwrOn2PwrGood
* 2;
297 DelayAfterPort
= hub
->descriptor
->bPwrOn2PwrGood
;
298 DelayPerPort
= hub
->descriptor
->bPwrOn2PwrGood
/4;
302 //dev_dbg(hub_dev, "unknown reserved power switching mode\n");
307 /* Enable power to the ports */
308 dev_dbg(hubdev(interface_to_usbdev(hub
->intf
)),
309 "enabling power on all ports\n");
310 dev
= interface_to_usbdev(hub
->intf
);
312 for (i
= 0; i
< hub
->descriptor
->bNbrPorts
; i
++) {
313 set_port_feature(dev
, i
+ 1, USB_PORT_FEAT_POWER
);
314 wait_ms(DelayPerPort
);
317 /* Wait for power to be enabled */
318 wait_ms(DelayAfterPort
);
321 static int hub_hub_status(struct usb_hub
*hub
,
322 u16
*status
, u16
*change
)
324 struct usb_device
*dev
= interface_to_usbdev (hub
->intf
);
327 ret
= get_hub_status(dev
, &hub
->status
->hub
);
329 dev_err (hubdev (dev
),
330 "%s failed (err = %d)\n", __FUNCTION__
, ret
);
333 *status
= le16_to_cpu(hub
->status
->hub
.wHubStatus
);
334 *change
= le16_to_cpu(hub
->status
->hub
.wHubChange
);
340 static int hub_configure(struct usb_hub
*hub
,
341 struct usb_endpoint_descriptor
*endpoint
)
343 struct usb_device
*dev
= interface_to_usbdev (hub
->intf
);
344 struct device
*hub_dev
;
345 u16 hubstatus
, hubchange
;
350 hub
->buffer
= usb_buffer_alloc(dev
, sizeof(*hub
->buffer
), GFP_KERNEL
,
353 message
= "can't allocate hub irq buffer";
358 hub
->status
= kmalloc(sizeof(*hub
->status
), GFP_KERNEL
);
360 message
= "can't kmalloc hub status buffer";
365 hub
->descriptor
= kmalloc(sizeof(*hub
->descriptor
), GFP_KERNEL
);
366 if (!hub
->descriptor
) {
367 message
= "can't kmalloc hub descriptor";
372 /* Request the entire hub descriptor.
373 * hub->descriptor can handle USB_MAXCHILDREN ports,
374 * but the hub can/will return fewer bytes here.
376 ret
= get_hub_descriptor(dev
, hub
->descriptor
,
377 sizeof(*hub
->descriptor
));
379 message
= "can't read hub descriptor";
381 } else if (hub
->descriptor
->bNbrPorts
> USB_MAXCHILDREN
) {
382 message
= "hub has too many ports!";
383 // XBOX PATCH hub->descriptor->bNbrPorts = 4; //ret = -ENODEV;
387 hub_dev
= hubdev(dev
);
388 dev
->maxchild
= hub
->descriptor
->bNbrPorts
;
389 dev_info (hub_dev
, "%d port%s detected\n", dev
->maxchild
,
390 (dev
->maxchild
== 1) ? "" : "s");
392 le16_to_cpus(&hub
->descriptor
->wHubCharacteristics
);
394 if (hub
->descriptor
->wHubCharacteristics
& HUB_CHAR_COMPOUND
) {
396 char portstr
[USB_MAXCHILDREN
+ 1];
398 for (i
= 0; i
< dev
->maxchild
; i
++)
399 portstr
[i
] = hub
->descriptor
->DeviceRemovable
400 [((i
+ 1) / 8)] & (1 << ((i
+ 1) % 8))
402 portstr
[dev
->maxchild
] = 0;
403 dev_dbg(hub_dev
, "compound device; port removable status: %s\n", portstr
);
405 dev_dbg(hub_dev
, "standalone hub\n");
407 switch (hub
->descriptor
->wHubCharacteristics
& HUB_CHAR_LPSM
) {
409 dev_dbg(hub_dev
, "ganged power switching\n");
412 dev_dbg(hub_dev
, "individual port power switching\n");
416 dev_dbg(hub_dev
, "unknown reserved power switching mode\n");
420 switch (hub
->descriptor
->wHubCharacteristics
& HUB_CHAR_OCPM
) {
422 dev_dbg(hub_dev
, "global over-current protection\n");
425 dev_dbg(hub_dev
, "individual port over-current protection\n");
429 dev_dbg(hub_dev
, "no over-current protection\n");
433 spin_lock_init (&hub
->tt
.lock
);
434 INIT_LIST_HEAD (&hub
->tt
.clear_list
);
435 INIT_WORK (&hub
->tt
.kevent
, hub_tt_kevent
, hub
);
436 switch (dev
->descriptor
.bDeviceProtocol
) {
440 dev_dbg(hub_dev
, "Single TT\n");
444 dev_dbg(hub_dev
, "TT per port\n");
449 dev_dbg(hub_dev
, "Unrecognized hub protocol %d\n",
450 dev
->descriptor
.bDeviceProtocol
);
454 switch (hub
->descriptor
->wHubCharacteristics
& HUB_CHAR_TTTT
) {
456 if (dev
->descriptor
.bDeviceProtocol
!= 0)
457 dev_dbg(hub_dev
, "TT requires at most 8 FS bit times\n");
460 dev_dbg(hub_dev
, "TT requires at most 16 FS bit times\n");
463 dev_dbg(hub_dev
, "TT requires at most 24 FS bit times\n");
466 dev_dbg(hub_dev
, "TT requires at most 32 FS bit times\n");
470 dev_dbg(hub_dev
, "Port indicators are %s supported\n",
471 (hub
->descriptor
->wHubCharacteristics
& HUB_CHAR_PORTIND
)
474 if (hub
->descriptor
->bPwrOn2PwrGood
<3) hub
->descriptor
->bPwrOn2PwrGood
= 3;
475 if (hub
->descriptor
->bPwrOn2PwrGood
>20) hub
->descriptor
->bPwrOn2PwrGood
= 20;
477 dev_dbg(hub_dev
, "power on to power good time: %dms\n",
478 hub
->descriptor
->bPwrOn2PwrGood
* 2);
479 dev_dbg(hub_dev
, "hub controller current requirement: %dmA\n",
480 hub
->descriptor
->bHubContrCurrent
);
482 ret
= hub_hub_status(hub
, &hubstatus
, &hubchange
);
484 message
= "can't get hub status";
488 dev_dbg(hub_dev
, "local power source is %s\n",
489 (hubstatus
& HUB_STATUS_LOCAL_POWER
)
490 ? "lost (inactive)" : "good");
492 dev_dbg(hub_dev
, "%sover-current condition exists\n",
493 (hubstatus
& HUB_STATUS_OVERCURRENT
) ? "" : "no ");
495 /* Start the interrupt endpoint */
496 pipe
= usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
);
497 maxp
= usb_maxpacket(dev
, pipe
, usb_pipeout(pipe
));
499 if (maxp
> sizeof(*hub
->buffer
))
500 maxp
= sizeof(*hub
->buffer
);
502 hub
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
504 message
= "couldn't allocate interrupt urb";
509 usb_fill_int_urb(hub
->urb
, dev
, pipe
, *hub
->buffer
, maxp
, hub_irq
,
510 hub
, endpoint
->bInterval
);
511 hub
->urb
->transfer_dma
= hub
->buffer_dma
;
512 hub
->urb
->transfer_flags
|= URB_NO_DMA_MAP
;
513 ret
= usb_submit_urb(hub
->urb
, GFP_KERNEL
);
515 message
= "couldn't submit status urb";
520 wake_up(&khubd_wait
);
521 printk("hub_thread should woke up\n");
528 dev_err (&hub
->intf
->dev
, "config failed, %s (err %d)\n",
530 /* hub_disconnect() frees urb and descriptor */
534 static void hub_disconnect(struct usb_interface
*intf
)
536 struct usb_hub
*hub
= usb_get_intfdata (intf
);
542 usb_set_intfdata (intf
, NULL
);
543 spin_lock_irqsave(&hub_event_lock
, flags
);
545 /* Delete it and then reset it */
546 list_del(&hub
->event_list
);
547 INIT_LIST_HEAD(&hub
->event_list
);
548 list_del(&hub
->hub_list
);
549 INIT_LIST_HEAD(&hub
->hub_list
);
551 spin_unlock_irqrestore(&hub_event_lock
, flags
);
553 down(&hub
->khubd_sem
); /* Wait for khubd to leave this hub alone. */
556 /* assuming we used keventd, it must quiesce too */
558 flush_scheduled_work ();
561 usb_unlink_urb(hub
->urb
);
562 usb_free_urb(hub
->urb
);
566 if (hub
->descriptor
) {
567 kfree(hub
->descriptor
);
568 hub
->descriptor
= NULL
;
577 usb_buffer_free(interface_to_usbdev(intf
),
578 sizeof(*hub
->buffer
), hub
->buffer
,
583 /* Free the memory */
587 static int hub_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
589 struct usb_host_interface
*desc
;
590 struct usb_endpoint_descriptor
*endpoint
;
591 struct usb_device
*dev
;
595 desc
= intf
->altsetting
+ intf
->act_altsetting
;
596 dev
= interface_to_usbdev(intf
);
598 /* Some hubs have a subclass of 1, which AFAICT according to the */
599 /* specs is not defined, but it works */
600 if ((desc
->desc
.bInterfaceSubClass
!= 0) &&
601 (desc
->desc
.bInterfaceSubClass
!= 1)) {
603 desc
->desc
.bInterfaceSubClass
=0;
604 dev_err (&intf
->dev
, "bad descriptor, ignoring hub\n");
608 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
609 if (desc
->desc
.bNumEndpoints
!= 1) {
610 desc
->desc
.bNumEndpoints
= 1;
611 //goto descriptor_error;
614 endpoint
= &desc
->endpoint
[0].desc
;
616 /* Output endpoint? Curiouser and curiouser.. */
617 if (!(endpoint
->bEndpointAddress
& USB_DIR_IN
)) {
618 //goto descriptor_error;
619 endpoint
->bEndpointAddress
|= USB_DIR_IN
;
622 /* If it's not an interrupt endpoint, we'd better punt! */
623 if ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
624 != USB_ENDPOINT_XFER_INT
) {
625 endpoint
->bmAttributes
|= USB_ENDPOINT_XFER_INT
;
626 //goto descriptor_error;
631 dev_info (hubdev (dev
), "USB hub found\n");
633 hub
= kmalloc(sizeof(*hub
), GFP_KERNEL
);
635 err("couldn't kmalloc hub struct");
639 memset(hub
, 0, sizeof(*hub
));
641 hub
->RestCounter
= 0;
643 INIT_LIST_HEAD(&hub
->event_list
);
645 init_MUTEX(&hub
->khubd_sem
);
647 /* Record the new hub's existence */
648 spin_lock_irqsave(&hub_event_lock
, flags
);
649 INIT_LIST_HEAD(&hub
->hub_list
);
650 list_add(&hub
->hub_list
, &hub_list
);
651 spin_unlock_irqrestore(&hub_event_lock
, flags
);
653 usb_set_intfdata (intf
, hub
);
655 if (hub_configure(hub
, endpoint
) >= 0)
657 strcpy (intf
->dev
.name
, "Hub");
661 //hub_disconnect (intf);
666 hub_ioctl(struct usb_interface
*intf
, unsigned int code
, void *user_data
)
668 struct usb_device
*hub
= interface_to_usbdev (intf
);
670 /* assert ifno == 0 (part of hub spec) */
672 case USBDEVFS_HUB_PORTINFO
: {
673 struct usbdevfs_hub_portinfo
*info
= user_data
;
677 spin_lock_irqsave(&hub_event_lock
, flags
);
678 if (hub
->devnum
<= 0)
681 info
->nports
= hub
->maxchild
;
682 for (i
= 0; i
< info
->nports
; i
++) {
683 if (hub
->children
[i
] == NULL
)
687 hub
->children
[i
]->devnum
;
690 spin_unlock_irqrestore(&hub_event_lock
, flags
);
692 return info
->nports
+ 1;
700 static int hub_reset(struct usb_hub
*hub
)
702 struct usb_device
*dev
= interface_to_usbdev(hub
->intf
);
705 /* Disconnect any attached devices */
706 for (i
= 0; i
< hub
->descriptor
->bNbrPorts
; i
++) {
707 if (dev
->children
[i
])
708 usb_disconnect(&dev
->children
[i
]);
711 /* Attempt to reset the hub */
713 usb_unlink_urb(hub
->urb
);
717 if (usb_reset_device(dev
))
721 if (usb_submit_urb(hub
->urb
, GFP_KERNEL
))
729 static void hub_start_disconnect(struct usb_device
*dev
)
731 struct usb_device
*parent
= dev
->parent
;
734 /* Find the device pointer to disconnect */
736 for (i
= 0; i
< parent
->maxchild
; i
++) {
737 if (parent
->children
[i
] == dev
) {
738 usb_disconnect(&parent
->children
[i
]);
744 err("cannot disconnect hub %s", dev
->devpath
);
747 static int hub_port_status(struct usb_device
*dev
, int port
,
748 u16
*status
, u16
*change
)
750 struct usb_hub
*hub
= usb_get_intfdata (dev
->actconfig
->interface
);
756 ret
= get_port_status(dev
, port
+ 1, &hub
->status
->port
);
758 dev_err (hubdev (dev
),
759 "%s failed (err = %d)\n", __FUNCTION__
, ret
);
762 *status
= le16_to_cpu(hub
->status
->port
.wPortStatus
);
763 *change
= le16_to_cpu(hub
->status
->port
.wPortChange
);
769 #define HUB_RESET_TRIES 5
770 #define HUB_PROBE_TRIES 5
771 #define HUB_ROOT_RESET_TIME 40
772 #define HUB_SHORT_RESET_TIME 10
773 #define HUB_LONG_RESET_TIME 70
774 #define HUB_RESET_TIMEOUT 500
776 /* return: -1 on error, 0 on success, 1 on disconnect. */
777 static int hub_port_wait_reset(struct usb_device
*hub
, int port
,
778 struct usb_device
*dev
, unsigned int delay
)
785 delay_time
< HUB_RESET_TIMEOUT
;
786 delay_time
+= delay
) {
787 /* wait to give the device a chance to reset */
790 /* read and decode port status */
791 ret
= hub_port_status(hub
, port
, &portstatus
, &portchange
);
796 /* Device went away? */
797 if (!(portstatus
& USB_PORT_STAT_CONNECTION
))
800 /* bomb out completely if something weird happened */
801 if ((portchange
& USB_PORT_STAT_C_CONNECTION
))
804 /* if we`ve finished resetting, then break out of the loop */
805 if (!(portstatus
& USB_PORT_STAT_RESET
) &&
806 (portstatus
& USB_PORT_STAT_ENABLE
)) {
807 if (portstatus
& USB_PORT_STAT_HIGH_SPEED
)
808 dev
->speed
= USB_SPEED_HIGH
;
809 else if (portstatus
& USB_PORT_STAT_LOW_SPEED
)
810 dev
->speed
= USB_SPEED_LOW
;
812 dev
->speed
= USB_SPEED_FULL
;
816 /* switch to the long delay after two short delay failures */
817 if (delay_time
>= 2 * HUB_SHORT_RESET_TIME
)
818 delay
= HUB_LONG_RESET_TIME
;
820 dev_dbg (hubdev (hub
),
821 "port %d not reset yet, waiting %dms\n",
828 /* return: -1 on error, 0 on success, 1 on disconnect. */
829 static int hub_port_reset(struct usb_device
*hub
, int port
,
830 struct usb_device
*dev
, unsigned int delay
)
835 for (i
= 0; i
< HUB_RESET_TRIES
; i
++) {
836 set_port_feature(hub
, port
+ 1, USB_PORT_FEAT_RESET
);
838 /* return on disconnect or reset */
839 status
= hub_port_wait_reset(hub
, port
, dev
, delay
);
841 clear_port_feature(hub
,
842 port
+ 1, USB_PORT_FEAT_C_RESET
);
844 ? USB_STATE_NOTATTACHED
849 dev_dbg (hubdev (hub
),
850 "port %d not enabled, trying reset again...\n",
852 delay
= HUB_LONG_RESET_TIME
;
855 dev_err (hubdev (hub
),
856 "Cannot enable port %i. Maybe the USB cable is bad?\n",
862 int hub_port_disable(struct usb_device
*hub
, int port
)
866 ret
= clear_port_feature(hub
, port
+ 1, USB_PORT_FEAT_ENABLE
);
868 dev_err(hubdev(hub
), "cannot disable port %d (err = %d)\n",
874 /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
876 * Between connect detection and reset signaling there must be a delay
877 * of 100ms at least for debounce and power-settling. The corresponding
878 * timer shall restart whenever the downstream port detects a disconnect.
880 * Apparently there are some bluetooth and irda-dongles and a number
881 * of low-speed devices which require longer delays of about 200-400ms.
882 * Not covered by the spec - but easy to deal with.
884 * This implementation uses 400ms minimum debounce timeout and checks
885 * every 25ms for transient disconnects to restart the delay.
888 #define HUB_DEBOUNCE_TIMEOUT 400
889 #define HUB_DEBOUNCE_STEP 5
890 #define HUB_DEBOUNCE_STABLE 3
892 /* return: -1 on error, 0 on success, 1 on disconnect. */
893 static int hub_port_debounce(struct usb_device
*hub
, int port
)
896 int delay_time
, stable_count
;
897 u16 portchange
, portstatus
;
902 for (delay_time
= 0; delay_time
< HUB_DEBOUNCE_TIMEOUT
; delay_time
+= HUB_DEBOUNCE_STEP
) {
903 wait_ms(HUB_DEBOUNCE_STEP
);
905 ret
= hub_port_status(hub
, port
, &portstatus
, &portchange
);
909 if ((portstatus
& USB_PORT_STAT_CONNECTION
) == connection
) {
911 if (++stable_count
== HUB_DEBOUNCE_STABLE
)
917 connection
= portstatus
& USB_PORT_STAT_CONNECTION
;
919 if ((portchange
& USB_PORT_STAT_C_CONNECTION
)) {
920 clear_port_feature(hub
, port
+1, USB_PORT_FEAT_C_CONNECTION
);
924 /* XXX Replace this with dbg() when 2.6 is about to ship. */
925 dev_dbg (hubdev (hub
),
926 "debounce: port %d: delay %dms stable %d status 0x%x\n",
927 port
+ 1, delay_time
, stable_count
, portstatus
);
929 return ((portstatus
&USB_PORT_STAT_CONNECTION
)) ? 0 : 1;
932 static void hub_port_connect_change(struct usb_hub
*hubstate
, int port
,
933 u16 portstatus
, u16 portchange
)
935 struct usb_device
*hub
= interface_to_usbdev(hubstate
->intf
);
936 struct usb_device
*dev
;
937 unsigned int delay
= HUB_SHORT_RESET_TIME
;
939 int DevcoosenAdress
= 0;
941 //printe("port %d, status %x, change %x,\n",port + 1, portstatus, portchange);
944 dev_dbg (&hubstate
->intf
->dev
,
945 "port %d, status %x, change %x, %s\n",
946 port
+ 1, portstatus
, portchange
, portspeed (portstatus
));
948 /* Clear the connection change status */
949 clear_port_feature(hub
, port
+ 1, USB_PORT_FEAT_C_CONNECTION
);
951 /* Disconnect any existing devices under this port */
952 if (hub
->children
[port
])
953 usb_disconnect(&hub
->children
[port
]);
955 /* Return now if nothing is connected */
956 if (!(portstatus
& USB_PORT_STAT_CONNECTION
)) {
957 if (portstatus
& USB_PORT_STAT_ENABLE
)
958 hub_port_disable(hub
, port
);
963 if (hub_port_debounce(hub
, port
)) {
964 dev_err (&hubstate
->intf
->dev
,
965 "connect-debounce failed, port %d disabled\n",
967 //printe("connect-debounce failed, port %d disabled\n", port+1);
968 hub_port_disable(hub
, port
);
972 /* root hub ports have a slightly longer reset period
973 * (from USB 2.0 spec, section 7.1.7.5)
976 delay
= HUB_ROOT_RESET_TIME
;
978 /* Some low speed devices have problems with the quick delay, so */
979 /* be a bit pessimistic with those devices. RHbug #23670 */
980 if (portstatus
& USB_PORT_STAT_LOW_SPEED
)
981 delay
= HUB_LONG_RESET_TIME
;
983 down(&usb_address0_sem
);
985 for (i
= 0; i
< HUB_PROBE_TRIES
; i
++) {
986 struct usb_device
*pdev
;
989 /* Allocate a new device struct */
990 dev
= usb_alloc_dev(hub
, hub
->bus
);
992 dev_err (&hubstate
->intf
->dev
,
993 "couldn't allocate usb_device\n");
998 dev
->state
= USB_STATE_POWERED
;
1000 /* Reset the device, and detect its speed */
1001 if (hub_port_reset(hub
, port
, dev
, delay
)) {
1006 /* Find a new address for it */
1007 if (DevcoosenAdress
==0) {
1008 usb_choose_address(dev
);
1009 DevcoosenAdress
= dev
->devnum
;
1011 dev
->devnum
= DevcoosenAdress
;
1013 /* Set up TT records, if needed */
1016 dev
->ttport
= hub
->ttport
;
1017 } else if (dev
->speed
!= USB_SPEED_HIGH
1018 && hub
->speed
== USB_SPEED_HIGH
) {
1019 dev
->tt
= &hubstate
->tt
;
1020 dev
->ttport
= port
+ 1;
1023 /* Save readable and stable topology id, distinguishing
1024 * devices by location for diagnostics, tools, etc. The
1025 * string is a path along hub ports, from the root. Each
1026 * device's id will be stable until USB is re-cabled, and
1027 * hubs are often labeled with these port numbers.
1029 * Initial size: ".NN" times five hubs + NUL = 16 bytes max
1030 * (quite rare, since most hubs have 4-6 ports).
1033 if (pdev
->devpath
[0] != '0') /* parent not root? */
1034 len
= snprintf (dev
->devpath
, sizeof dev
->devpath
,
1035 "%s.%d", pdev
->devpath
, port
+ 1);
1036 /* root == "0", root port 2 == "2", port 3 that hub "2.3" */
1038 len
= snprintf (dev
->devpath
, sizeof dev
->devpath
,
1040 if (len
== sizeof dev
->devpath
)
1041 dev_err (&hubstate
->intf
->dev
,
1042 "devpath size! usb/%03d/%03d path %s\n",
1043 dev
->bus
->busnum
, dev
->devnum
, dev
->devpath
);
1044 dev_info (&hubstate
->intf
->dev
,
1045 "new USB device on port %d, assigned address %d\n",
1046 port
+ 1, dev
->devnum
);
1048 /* put the device in the global device tree. the hub port
1049 * is the "bus_id"; hubs show in hierarchy like bridges
1051 dev
->dev
.parent
= dev
->parent
->dev
.parent
->parent
;
1053 /* Run it through the hoops (find a driver, etc) */
1054 if (!usb_new_device(dev
, &hub
->dev
)) {
1055 hub
->children
[port
] = dev
;
1059 /* Free the configuration if there was an error */
1062 /* Switch to a long reset time */
1063 delay
= HUB_LONG_RESET_TIME
;
1067 hub_port_disable(hub
, port
);
1069 up(&usb_address0_sem
);
1072 static void hub_events(void)
1074 unsigned long flags
;
1075 struct list_head
*tmp
;
1076 struct usb_device
*dev
;
1077 struct usb_hub
*hub
;
1085 * We restart the list every time to avoid a deadlock with
1086 * deleting hubs downstream from this one. This should be
1087 * safe since we delete the hub from the event list.
1088 * Not the most efficient, but avoids deadlocks.
1090 //DPRINT1("hub_events() called\n");
1094 spin_lock_irqsave(&hub_event_lock
, flags
);
1096 if (list_empty(&hub_event_list
))
1099 /* Grab the next entry from the beginning of the list */
1100 tmp
= hub_event_list
.next
;
1102 hub
= list_entry(tmp
, struct usb_hub
, event_list
);
1103 dev
= interface_to_usbdev(hub
->intf
);
1107 if (unlikely(down_trylock(&hub
->khubd_sem
)))
1108 BUG(); /* never blocks, we were on list */
1110 spin_unlock_irqrestore(&hub_event_lock
, flags
);
1113 dev_dbg (&hub
->intf
->dev
, "resetting for error %d\n",
1116 if (hub_reset(hub
)) {
1117 dev_dbg (&hub
->intf
->dev
,
1118 "can't reset; disconnecting\n");
1119 up(&hub
->khubd_sem
);
1120 hub_start_disconnect(dev
);
1128 for (i
= 0; i
< hub
->descriptor
->bNbrPorts
; i
++) {
1129 ret
= hub_port_status(dev
, i
, &portstatus
, &portchange
);
1134 if (portchange
& USB_PORT_STAT_C_CONNECTION
) {
1135 hub_port_connect_change(hub
, i
, portstatus
, portchange
);
1136 } else if (portchange
& USB_PORT_STAT_C_ENABLE
) {
1137 dev_dbg (hubdev (dev
),
1138 "port %d enable change, status %x\n",
1140 clear_port_feature(dev
,
1141 i
+ 1, USB_PORT_FEAT_C_ENABLE
);
1144 * EM interference sometimes causes badly
1145 * shielded USB devices to be shutdown by
1146 * the hub, this hack enables them again.
1147 * Works at least with mouse driver.
1149 if (!(portstatus
& USB_PORT_STAT_ENABLE
)
1150 && (portstatus
& USB_PORT_STAT_CONNECTION
)
1151 && (dev
->children
[i
])) {
1152 dev_err (&hub
->intf
->dev
,
1154 "disabled by hub (EMI?), "
1157 hub_port_connect_change(hub
,
1158 i
, portstatus
, portchange
);
1162 if (portchange
& USB_PORT_STAT_C_SUSPEND
) {
1163 dev_dbg (&hub
->intf
->dev
,
1164 "suspend change on port %d\n",
1166 clear_port_feature(dev
,
1167 i
+ 1, USB_PORT_FEAT_C_SUSPEND
);
1170 if (portchange
& USB_PORT_STAT_C_OVERCURRENT
) {
1171 dev_err (&hub
->intf
->dev
,
1172 "over-current change on port %d\n",
1174 clear_port_feature(dev
,
1175 i
+ 1, USB_PORT_FEAT_C_OVER_CURRENT
);
1179 if (portchange
& USB_PORT_STAT_C_RESET
) {
1180 dev_dbg (&hub
->intf
->dev
,
1181 "reset change on port %d\n",
1183 clear_port_feature(dev
,
1184 i
+ 1, USB_PORT_FEAT_C_RESET
);
1188 /* deal with hub status changes */
1189 if (hub_hub_status(hub
, &hubstatus
, &hubchange
) < 0) {
1190 dev_err (&hub
->intf
->dev
, "get_hub_status failed\n");
1193 if (hubchange
& HUB_CHANGE_LOCAL_POWER
) {
1194 dev_dbg (&hub
->intf
->dev
, "power change\n");
1195 clear_hub_feature(dev
, C_HUB_LOCAL_POWER
);
1197 if (hubchange
& HUB_CHANGE_OVERCURRENT
) {
1198 dev_dbg (&hub
->intf
->dev
, "overcurrent change\n");
1199 wait_ms(500); /* Cool down */
1200 clear_hub_feature(dev
, C_HUB_OVER_CURRENT
);
1204 up(&hub
->khubd_sem
);
1205 } /* end while (1) */
1207 spin_unlock_irqrestore(&hub_event_lock
, flags
);
1210 // ReactOS: STDCALL is needed here
1211 static int STDCALL
hub_thread(void *__hub
)
1213 //LARGE_INTEGER delay;
1216 * This thread doesn't need any user-level access,
1217 * so get rid of all our resources
1221 allow_signal(SIGKILL
);
1223 // Initialize khubd spinlock
1224 KeInitializeSpinLock((PKSPIN_LOCK
)&hub_event_lock
);
1226 //delay.QuadPart = -10000000*5; // wait 5 seconds before powering up
1227 //KeDelayExecutionThread(KernelMode, FALSE, &delay); //wait_us(1);
1229 printk("hub_thread starting");
1231 /* Send me a signal to get me die (for debugging) */
1233 LARGE_INTEGER delay
;
1235 /* The following is just for debug */
1239 /* End of debug hack*/
1241 /* The following is just for debug */
1243 /* End of debug hack*/
1245 //FIXME: Correct this
1246 //wait_event_interruptible(khubd_wait, !list_empty(&hub_event_list)); // interruptable_sleep_on analog - below
1247 //while (!list_empty(&hub_event_list)) {
1248 // interruptible_sleep_on(&khubd_wait);
1251 delay
.QuadPart
= -10000*100; // convert to 100ns units
1252 KeDelayExecutionThread(KernelMode
, FALSE
, &delay
); //wait_us(1);
1254 if (current
->flags
& PF_FREEZE
)
1255 refrigerator(PF_IOTHREAD
);
1257 } while (!signal_pending(current
));
1259 dbg("hub_thread exiting");
1260 complete_and_exit(&khubd_exited
, 0);
1263 static struct usb_device_id hub_id_table
[] = {
1264 { .match_flags
= USB_DEVICE_ID_MATCH_DEV_CLASS
,
1265 .bDeviceClass
= USB_CLASS_HUB
},
1266 { .match_flags
= USB_DEVICE_ID_MATCH_INT_CLASS
,
1267 .bInterfaceClass
= USB_CLASS_HUB
},
1268 { } /* Terminating entry */
1271 MODULE_DEVICE_TABLE (usb
, hub_id_table
);
1273 static struct usb_driver hub_driver
= {
1274 .owner
= THIS_MODULE
,
1277 .disconnect
= hub_disconnect
,
1279 .id_table
= hub_id_table
,
1283 * This should be a separate module.
1285 int usb_hub_init(void)
1290 // Create Event object, initialize other sync events
1291 KeInitializeEvent(&khubd_wait
, NotificationEvent
, TRUE
); // signalled state
1293 if (usb_register(&hub_driver
) < 0) {
1294 err("Unable to register USB hub driver");
1298 pid
= kernel_thread((void*)hub_thread
, NULL
,
1299 (CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
));
1305 /* Fall through if kernel_thread failed */
1306 usb_deregister(&hub_driver
);
1307 err("failed to start hub_thread");
1312 void usb_hub_cleanup(void)
1316 /* Kill the thread */
1317 ret
= kill_proc(khubd_pid
, SIGKILL
, 1);
1319 wait_for_completion(&khubd_exited
);
1322 * Hub resources are freed for us by usb_deregister. It calls
1323 * usb_driver_purge on every device which in turn calls that
1324 * devices disconnect function if it is using this driver.
1325 * The hub_disconnect function takes care of releasing the
1326 * individual hub resources. -greg
1328 usb_deregister(&hub_driver
);
1329 } /* usb_hub_cleanup() */
1332 * WARNING - If a driver calls usb_reset_device, you should simulate a
1333 * disconnect() and probe() for other interfaces you doesn't claim. This
1334 * is left up to the driver writer right now. This insures other drivers
1335 * have a chance to re-setup their interface.
1337 * Take a look at proc_resetdevice in devio.c for some sample code to
1339 * Use this only from within your probe function, otherwise use
1340 * usb_reset_device() below, which ensure proper locking
1342 int usb_physical_reset_device(struct usb_device
*dev
)
1344 struct usb_device
*parent
= dev
->parent
;
1345 struct usb_device_descriptor
*descriptor
;
1346 int i
, ret
, port
= -1;
1349 err("attempting to reset root hub!");
1353 for (i
= 0; i
< parent
->maxchild
; i
++)
1354 if (parent
->children
[i
] == dev
) {
1362 descriptor
= kmalloc(sizeof *descriptor
, GFP_NOIO
);
1367 down(&usb_address0_sem
);
1369 /* Send a reset to the device */
1370 if (hub_port_reset(parent
, port
, dev
, HUB_SHORT_RESET_TIME
)) {
1371 hub_port_disable(parent
, port
);
1372 up(&usb_address0_sem
);
1377 /* Reprogram the Address */
1378 ret
= usb_set_address(dev
);
1380 err("USB device not accepting new address (error=%d)", ret
);
1381 hub_port_disable(parent
, port
);
1382 up(&usb_address0_sem
);
1387 /* Let the SET_ADDRESS settle */
1390 up(&usb_address0_sem
);
1393 * Now we fetch the configuration descriptors for the device and
1394 * see if anything has changed. If it has, we dump the current
1395 * parsed descriptors and reparse from scratch. Then we leave
1396 * the device alone for the caller to finish setting up.
1398 * If nothing changed, we reprogram the configuration and then
1399 * the alternate settings.
1402 ret
= usb_get_descriptor(dev
, USB_DT_DEVICE
, 0, descriptor
,
1403 sizeof(*descriptor
));
1409 le16_to_cpus(&descriptor
->bcdUSB
);
1410 le16_to_cpus(&descriptor
->idVendor
);
1411 le16_to_cpus(&descriptor
->idProduct
);
1412 le16_to_cpus(&descriptor
->bcdDevice
);
1414 if (RtlCompareMemory(&dev
->descriptor
, descriptor
, sizeof(*descriptor
))) {
1416 usb_destroy_configuration(dev
);
1418 ret
= usb_get_device_descriptor(dev
);
1419 if (ret
< sizeof(dev
->descriptor
)) {
1421 err("unable to get device %s descriptor "
1422 "(error=%d)", dev
->devpath
, ret
);
1425 err("USB device %s descriptor short read "
1426 "(expected %Zi, got %i)",
1428 sizeof(dev
->descriptor
), ret
);
1430 clear_bit(dev
->devnum
, dev
->bus
->devmap
.devicemap
);
1435 ret
= usb_get_configuration(dev
);
1437 err("unable to get configuration (error=%d)", ret
);
1438 usb_destroy_configuration(dev
);
1439 clear_bit(dev
->devnum
, dev
->bus
->devmap
.devicemap
);
1444 usb_set_configuration(dev
, dev
->config
[0].desc
.bConfigurationValue
);
1452 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
1453 USB_REQ_SET_CONFIGURATION
, 0,
1454 dev
->actconfig
->desc
.bConfigurationValue
, 0,
1455 NULL
, 0, HZ
* USB_CTRL_SET_TIMEOUT
);
1457 err("failed to set dev %s active configuration (error=%d)",
1461 dev
->state
= USB_STATE_CONFIGURED
;
1463 for (i
= 0; i
< dev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1464 struct usb_interface
*intf
= &dev
->actconfig
->interface
[i
];
1465 struct usb_interface_descriptor
*as
;
1467 as
= &intf
->altsetting
[intf
->act_altsetting
].desc
;
1468 ret
= usb_set_interface(dev
, as
->bInterfaceNumber
,
1469 as
->bAlternateSetting
);
1471 err("failed to set active alternate setting "
1472 "for dev %s interface %d (error=%d)",
1473 dev
->devpath
, i
, ret
);
1481 int usb_reset_device(struct usb_device
*udev
)
1483 //struct device *gdev = &udev->dev;
1486 down_read(&gdev
->bus
->subsys
.rwsem
);
1487 r
= usb_physical_reset_device(udev
);
1488 up_read(&gdev
->bus
->subsys
.rwsem
);