more changes on original files
[linux-2.4.git] / drivers / usb / hub.c
1 /*
2  * USB hub driver.
3  *
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)
8  *
9  */
10
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
20         #define DEBUG
21 #else
22         #undef DEBUG
23 #endif
24 #include <linux/usb.h>
25 #include <linux/usbdevice_fs.h>
26
27 #include <asm/semaphore.h>
28 #include <asm/uaccess.h>
29 #include <asm/byteorder.h>
30
31 #include "hub.h"
32
33 /* Wakes up khubd */
34 static spinlock_t hub_event_lock = SPIN_LOCK_UNLOCKED;
35 static DECLARE_MUTEX(usb_address0_sem);
36
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) */
39
40 static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
41 static pid_t khubd_pid = 0;                     /* PID of khubd */
42 static DECLARE_COMPLETION(khubd_exited);
43
44 //+Wilson12032003
45 extern void led_status(int);
46 //int HubFlag = 0;      
47 #define LED_OFF         0
48 #define LED_GREEN       1
49 #define LED_RED         2
50 #define LED_HUB         3
51 //Wilson12032003+
52
53 #ifdef  DEBUG
54 static inline char *portspeed (int portstatus)
55 {
56         if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
57                 return "480 Mb/s";
58         else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
59                 return "1.5 Mb/s";
60         else
61                 return "12 Mb/s";
62 }
63 #endif
64
65 /* USB 2.0 spec Section 11.24.4.5 */
66 static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
67 {
68         return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
69                 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
70                 USB_DT_HUB << 8, 0, data, size, HZ);
71 }
72
73 /*
74  * USB 2.0 spec Section 11.24.2.1
75  */
76 static int usb_clear_hub_feature(struct usb_device *dev, int feature)
77 {
78         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
79                 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, HZ);
80 }
81
82 /*
83  * USB 2.0 spec Section 11.24.2.2
84  * BUG: doesn't handle port indicator selector in high byte of wIndex
85  */
86 static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
87 {
88         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
89                 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port, NULL, 0, HZ);
90 }
91
92 /*
93  * USB 2.0 spec Section 11.24.2.13
94  * BUG: doesn't handle port indicator selector in high byte of wIndex
95  */
96 static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
97 {
98         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
99                 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port, NULL, 0, HZ);
100 }
101
102 /*
103  * USB 2.0 spec Section 11.24.2.6
104  */
105 static int usb_get_hub_status(struct usb_device *dev, void *data)
106 {
107         return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
108                 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
109                 data, sizeof(struct usb_hub_status), HZ);
110 }
111
112 /*
113  * USB 2.0 spec Section 11.24.2.7
114  */
115 static int usb_get_port_status(struct usb_device *dev, int port, void *data)
116 {
117         return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
118                 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
119                 data, sizeof(struct usb_hub_status), HZ);
120 }
121
122 static void hub_irq(struct urb *urb)
123 {
124         struct usb_hub *hub = (struct usb_hub *)urb->context;
125         unsigned long flags;
126
127         /* Cause a hub reset after 10 consecutive errors */
128         if (urb->status) {
129                 if (urb->status == -ENOENT)
130                         return;
131
132                 dbg("nonzero status in irq %d", urb->status);
133
134                 if ((++hub->nerrors < 10) || hub->error)
135                         return;
136
137                 hub->error = urb->status;
138         }
139
140         hub->nerrors = 0;
141
142         /* Something happened, let khubd figure it out */
143         spin_lock_irqsave(&hub_event_lock, flags);
144         if (list_empty(&hub->event_list)) {
145                 list_add(&hub->event_list, &hub_event_list);
146                 wake_up(&khubd_wait);
147         }
148         spin_unlock_irqrestore(&hub_event_lock, flags);
149 }
150
151 static void usb_hub_power_on(struct usb_hub *hub)
152 {
153         int i;
154
155         /* Enable power to the ports */
156         dbg("enabling power on all ports");
157         for (i = 0; i < hub->descriptor->bNbrPorts; i++)
158                 usb_set_port_feature(hub->dev, i + 1, USB_PORT_FEAT_POWER);
159
160         /* Wait for power to be enabled */
161         wait_ms(hub->descriptor->bPwrOn2PwrGood * 2);
162 }
163
164 static int usb_hub_configure(struct usb_hub *hub, struct usb_endpoint_descriptor *endpoint)
165 {
166         struct usb_device *dev = hub->dev;
167         struct usb_hub_status *hubstatus;
168         char portstr[USB_MAXCHILDREN + 1];
169         unsigned int pipe;
170         int i, maxp, ret;
171
172         hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
173         if (!hub->descriptor) {
174                 err("Unable to kmalloc %Zd bytes for hub descriptor", sizeof(*hub->descriptor));
175                 return -1;
176         }
177
178         /* Request the entire hub descriptor. */
179         ret = usb_get_hub_descriptor(dev, hub->descriptor, sizeof(*hub->descriptor));
180                 /* <hub->descriptor> is large enough for a hub with 127 ports;
181                  * the hub can/will return fewer bytes here. */
182         if (ret < 0) {
183                 err("Unable to get hub descriptor (err = %d)", ret);
184                 kfree(hub->descriptor);
185                 return -1;
186         }
187
188         dev->maxchild = hub->descriptor->bNbrPorts;
189         info("%d port%s detected", hub->descriptor->bNbrPorts, (hub->descriptor->bNbrPorts == 1) ? "" : "s");
190
191         //+Wilson12032003
192         /* Indicates that a usb hub is being detected, so the LED_RED of the led status is invalid now */
193         //led_status(LED_HUB);  
194         dev->HubFlag = 1;       
195         //Wilson12032003+
196
197         le16_to_cpus(&hub->descriptor->wHubCharacteristics);
198
199         if (hub->descriptor->wHubCharacteristics & HUB_CHAR_COMPOUND)
200                 dbg("part of a compound device");
201         else
202                 dbg("standalone hub");
203
204         switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_LPSM) {
205                 case 0x00:
206                         dbg("ganged power switching");
207                         break;
208                 case 0x01:
209                         dbg("individual port power switching");
210                         break;
211                 case 0x02:
212                 case 0x03:
213                         dbg("unknown reserved power switching mode");
214                         break;
215         }
216
217         switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_OCPM) {
218                 case 0x00:
219                         dbg("global over-current protection");
220                         break;
221                 case 0x08:
222                         dbg("individual port over-current protection");
223                         break;
224                 case 0x10:
225                 case 0x18:
226                         dbg("no over-current protection");
227                         break;
228         }
229
230         switch (dev->descriptor.bDeviceProtocol) {
231                 case 0:
232                         break;
233                 case 1:
234                         dbg("Single TT");
235                         hub->tt.hub = dev;
236                         break;
237                 case 2:
238                         dbg("TT per port");
239                         hub->tt.hub = dev;
240                         hub->tt.multi = 1;
241                         break;
242                 default:
243                         dbg("Unrecognized hub protocol %d",
244                                 dev->descriptor.bDeviceProtocol);
245                         break;
246         }
247
248         switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_TTTT) {
249                 case 0x00:
250                         if (dev->descriptor.bDeviceProtocol != 0)
251                                 dbg("TT requires at most 8 FS bit times");
252                         break;
253                 case 0x20:
254                         dbg("TT requires at most 16 FS bit times");
255                         break;
256                 case 0x40:
257                         dbg("TT requires at most 24 FS bit times");
258                         break;
259                 case 0x60:
260                         dbg("TT requires at most 32 FS bit times");
261                         break;
262         }
263
264         dbg("Port indicators are %s supported", 
265             (hub->descriptor->wHubCharacteristics & HUB_CHAR_PORTIND) ? "" : "not");
266
267         dbg("power on to power good time: %dms", hub->descriptor->bPwrOn2PwrGood * 2);
268         dbg("hub controller current requirement: %dmA", hub->descriptor->bHubContrCurrent);
269
270         for (i = 0; i < dev->maxchild; i++)
271                 portstr[i] = hub->descriptor->DeviceRemovable[((i + 1) / 8)] & (1 << ((i + 1) % 8)) ? 'F' : 'R';
272         portstr[dev->maxchild] = 0;
273
274         dbg("port removable status: %s", portstr);
275
276         hubstatus = kmalloc(sizeof *hubstatus, GFP_KERNEL);
277         if (!hubstatus) {
278                 err("Unable to allocate hubstatus");
279                 kfree(hub->descriptor);
280                 return -1;
281         }
282         ret = usb_get_hub_status(dev, hubstatus);
283         if (ret < 0) {
284                 err("Unable to get hub status (err = %d)", ret);
285                 kfree(hubstatus);
286                 kfree(hub->descriptor);
287                 return -1;
288         }
289
290         le16_to_cpus(&hubstatus->wHubStatus);
291
292         dbg("local power source is %s",
293                 (hubstatus->wHubStatus & HUB_STATUS_LOCAL_POWER) ? "lost (inactive)" : "good");
294
295         dbg("%sover-current condition exists",
296                 (hubstatus->wHubStatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
297
298         kfree(hubstatus);
299
300         /* Start the interrupt endpoint */
301         pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
302         maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
303
304         if (maxp > sizeof(hub->buffer))
305                 maxp = sizeof(hub->buffer);
306
307         hub->urb = usb_alloc_urb(0);
308         if (!hub->urb) {
309                 err("couldn't allocate interrupt urb");
310                 kfree(hub->descriptor);
311                 return -1;
312         }
313
314         FILL_INT_URB(hub->urb, dev, pipe, hub->buffer, maxp, hub_irq, hub,
315                 /* NOTE:  in 2.5 fill_int_urb() converts the encoding */
316                 (dev->speed == USB_SPEED_HIGH)
317                         ? 1 << (endpoint->bInterval - 1)
318                         : endpoint->bInterval);
319         ret = usb_submit_urb(hub->urb);
320         if (ret) {
321                 err("usb_submit_urb failed (%d)", ret);
322                 kfree(hub->descriptor);
323                 return -1;
324         }
325                 
326         /* Wake up khubd */
327         wake_up(&khubd_wait);
328
329         usb_hub_power_on(hub);
330
331         return 0;
332 }
333
334 static void *hub_probe(struct usb_device *dev, unsigned int i,
335                        const struct usb_device_id *id)
336 {
337         struct usb_interface_descriptor *interface;
338         struct usb_endpoint_descriptor *endpoint;
339         struct usb_hub *hub;
340         unsigned long flags;
341
342         interface = &dev->actconfig->interface[i].altsetting[0];
343
344         /* Some hubs have a subclass of 1, which AFAICT according to the */
345         /*  specs is not defined, but it works */
346         if ((interface->bInterfaceSubClass != 0) &&
347             (interface->bInterfaceSubClass != 1)) {
348                 err("invalid subclass (%d) for USB hub device #%d",
349                         interface->bInterfaceSubClass, dev->devnum);
350                 return NULL;
351         }
352
353         /* Multiple endpoints? What kind of mutant ninja-hub is this? */
354         if (interface->bNumEndpoints != 1) {
355                 err("invalid bNumEndpoints (%d) for USB hub device #%d",
356                         interface->bNumEndpoints, dev->devnum);
357                 return NULL;
358         }
359
360         endpoint = &interface->endpoint[0];
361
362         /* Output endpoint? Curiousier and curiousier.. */
363         if (!(endpoint->bEndpointAddress & USB_DIR_IN)) {
364                 err("Device #%d is hub class, but has output endpoint?",
365                         dev->devnum);
366                 return NULL;
367         }
368
369         /* If it's not an interrupt endpoint, we'd better punt! */
370         if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) {
371                 err("Device #%d is hub class, but has endpoint other than interrupt?",
372                         dev->devnum);
373                 return NULL;
374         }
375
376         /* We found a hub */
377         info("USB hub found");
378
379         hub = kmalloc(sizeof(*hub), GFP_KERNEL);
380         if (!hub) {
381                 err("couldn't kmalloc hub struct");
382                 return NULL;
383         }
384
385         memset(hub, 0, sizeof(*hub));
386
387         INIT_LIST_HEAD(&hub->event_list);
388         hub->dev = dev;
389         init_MUTEX(&hub->khubd_sem);
390
391         /* Record the new hub's existence */
392         spin_lock_irqsave(&hub_event_lock, flags);
393         INIT_LIST_HEAD(&hub->hub_list);
394         list_add(&hub->hub_list, &hub_list);
395         spin_unlock_irqrestore(&hub_event_lock, flags);
396
397         if (usb_hub_configure(hub, endpoint) >= 0)
398                 return hub;
399
400         err("hub configuration failed for device #%d", dev->devnum);
401
402         /* free hub, but first clean up its list. */
403         spin_lock_irqsave(&hub_event_lock, flags);
404
405         /* Delete it and then reset it */
406         list_del(&hub->event_list);
407         INIT_LIST_HEAD(&hub->event_list);
408         list_del(&hub->hub_list);
409         INIT_LIST_HEAD(&hub->hub_list);
410
411         spin_unlock_irqrestore(&hub_event_lock, flags);
412
413         kfree(hub);
414
415         return NULL;
416 }
417
418 static void hub_disconnect(struct usb_device *dev, void *ptr)
419 {
420         struct usb_hub *hub = (struct usb_hub *)ptr;
421         unsigned long flags;
422
423         spin_lock_irqsave(&hub_event_lock, flags);
424
425         /* Delete it and then reset it */
426         list_del(&hub->event_list);
427         INIT_LIST_HEAD(&hub->event_list);
428         list_del(&hub->hub_list);
429         INIT_LIST_HEAD(&hub->hub_list);
430
431         spin_unlock_irqrestore(&hub_event_lock, flags);
432
433         down(&hub->khubd_sem); /* Wait for khubd to leave this hub alone. */
434         up(&hub->khubd_sem);
435
436         if (hub->urb) {
437                 usb_unlink_urb(hub->urb);
438                 usb_free_urb(hub->urb);
439                 hub->urb = NULL;
440         }
441
442         if (hub->descriptor) {
443                 kfree(hub->descriptor);
444                 hub->descriptor = NULL;
445         }
446
447         /* Free the memory */
448         kfree(hub);
449 }
450
451 static int hub_ioctl(struct usb_device *hub, unsigned int code, void *user_data)
452 {
453         /* assert ifno == 0 (part of hub spec) */
454         switch (code) {
455         case USBDEVFS_HUB_PORTINFO: {
456                 struct usbdevfs_hub_portinfo *info = user_data;
457                 unsigned long flags;
458                 int i;
459
460                 spin_lock_irqsave(&hub_event_lock, flags);
461                 if (hub->devnum <= 0)
462                         info->nports = 0;
463                 else {
464                         info->nports = hub->maxchild;
465                         for (i = 0; i < info->nports; i++) {
466                                 if (hub->children[i] == NULL)
467                                         info->port[i] = 0;
468                                 else
469                                         info->port[i] = hub->children[i]->devnum;
470                         }
471                 }
472                 spin_unlock_irqrestore(&hub_event_lock, flags);
473
474                 return info->nports + 1;
475                 }
476
477         default:
478                 return -ENOSYS;
479         }
480 }
481
482 static int usb_hub_reset(struct usb_hub *hub)
483 {
484         struct usb_device *dev = hub->dev;
485         int i;
486
487         /* Disconnect any attached devices */
488         for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
489                 if (dev->children[i])
490                         usb_disconnect(&dev->children[i]);
491         }
492
493         /* Attempt to reset the hub */
494         if (hub->urb)
495                 usb_unlink_urb(hub->urb);
496         else
497                 return -1;
498
499         if (usb_reset_device(dev))
500                 return -1;
501
502         hub->urb->dev = dev;                                                    
503         if (usb_submit_urb(hub->urb))
504                 return -1;
505
506         usb_hub_power_on(hub);
507
508         return 0;
509 }
510
511 static void usb_hub_disconnect(struct usb_device *dev)
512 {
513         struct usb_device *parent = dev->parent;
514         int i;
515
516         /* Find the device pointer to disconnect */
517         if (parent) {
518                 for (i = 0; i < parent->maxchild; i++) {
519                         if (parent->children[i] == dev) {
520                                 usb_disconnect(&parent->children[i]);
521                                 return;
522                         }
523                 }
524         }
525
526         err("cannot disconnect hub %d", dev->devnum);
527 }
528
529 static int usb_hub_port_status(struct usb_device *hub, int port,
530                                u16 *status, u16 *change)
531 {
532         struct usb_port_status *portsts;
533         int ret = -ENOMEM;
534
535         portsts = kmalloc(sizeof(*portsts), GFP_KERNEL);
536         if (portsts) {
537                 ret = usb_get_port_status(hub, port + 1, portsts);
538                 if (ret < 0)
539                         err("%s (%d) failed (err = %d)", __FUNCTION__, hub->devnum, ret);
540                 else {
541                         *status = le16_to_cpu(portsts->wPortStatus);
542                         *change = le16_to_cpu(portsts->wPortChange); 
543                         dbg("port %d, portstatus %x, change %x, %s", port + 1,
544                                 *status, *change, portspeed(*status));
545                         ret = 0;
546                 }
547                 kfree(portsts);
548         }
549         return ret;
550 }
551
552 #define HUB_RESET_TRIES         5
553 #define HUB_PROBE_TRIES         2
554 #define HUB_SHORT_RESET_TIME    10
555 #define HUB_LONG_RESET_TIME     200
556 #define HUB_RESET_TIMEOUT       500
557
558 /* return: -1 on error, 0 on success, 1 on disconnect.  */
559 static int usb_hub_port_wait_reset(struct usb_device *hub, int port,
560                                 struct usb_device *dev, unsigned int delay)
561 {
562         int delay_time, ret;
563         u16 portstatus;
564         u16 portchange;
565
566         for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT; delay_time += delay) {
567                 /* wait to give the device a chance to reset */
568                 wait_ms(delay);
569
570                 /* read and decode port status */
571                 ret = usb_hub_port_status(hub, port, &portstatus, &portchange);
572                 if (ret < 0) {
573                         return -1;
574                 }
575
576                 /* Device went away? */
577                 if (!(portstatus & USB_PORT_STAT_CONNECTION))
578                         return 1;
579
580                 /* bomb out completely if something weird happened */
581                 if ((portchange & USB_PORT_STAT_C_CONNECTION))
582                         return -1;
583
584                 /* if we`ve finished resetting, then break out of the loop */
585                 if (!(portstatus & USB_PORT_STAT_RESET) &&
586                     (portstatus & USB_PORT_STAT_ENABLE)) {
587                         if (portstatus & USB_PORT_STAT_HIGH_SPEED)
588                                 dev->speed = USB_SPEED_HIGH;
589                         else if (portstatus & USB_PORT_STAT_LOW_SPEED)
590                                 dev->speed = USB_SPEED_LOW;
591                         else
592                                 dev->speed = USB_SPEED_FULL;
593                         return 0;
594                 }
595
596                 /* switch to the long delay after two short delay failures */
597                 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
598                         delay = HUB_LONG_RESET_TIME;
599
600                 dbg("port %d of hub %d not reset yet, waiting %dms", port + 1,
601                         hub->devnum, delay);
602         }
603
604         return -1;
605 }
606
607 /* return: -1 on error, 0 on success, 1 on disconnect.  */
608 static int usb_hub_port_reset(struct usb_device *hub, int port,
609                                 struct usb_device *dev, unsigned int delay)
610 {
611         int i, status;
612
613         /* Reset the port */
614         for (i = 0; i < HUB_RESET_TRIES; i++) {
615                 usb_set_port_feature(hub, port + 1, USB_PORT_FEAT_RESET);
616
617                 /* return on disconnect or reset */
618                 status = usb_hub_port_wait_reset(hub, port, dev, delay);
619                 if (status != -1) {
620                         usb_clear_port_feature(hub, port + 1, USB_PORT_FEAT_C_RESET);
621                         return status;
622                 }
623
624                 dbg("port %d of hub %d not enabled, trying reset again...",
625                         port + 1, hub->devnum);
626                 delay = HUB_LONG_RESET_TIME;
627         }
628
629         err("Cannot enable port %i of hub %d, disabling port.",
630                 port + 1, hub->devnum);
631         err("Maybe the USB cable is bad?");
632
633         return -1;
634 }
635
636 void usb_hub_port_disable(struct usb_device *hub, int port)
637 {
638         int ret;
639
640         ret = usb_clear_port_feature(hub, port + 1, USB_PORT_FEAT_ENABLE);
641         if (ret)
642                 err("cannot disable port %d of hub %d (err = %d)",
643                         port + 1, hub->devnum, ret);
644 }
645
646 /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
647  *
648  * Between connect detection and reset signaling there must be a delay
649  * of 100ms at least for debounce and power-settling. The corresponding
650  * timer shall restart whenever the downstream port detects a disconnect.
651  * 
652  * Apparently there are some bluetooth and irda-dongles and a number
653  * of low-speed devices which require longer delays of about 200-400ms.
654  * Not covered by the spec - but easy to deal with.
655  *
656  * This implementation uses 400ms minimum debounce timeout and checks
657  * every 100ms for transient disconnects to restart the delay.
658  */
659
660 #define HUB_DEBOUNCE_TIMEOUT    400
661 #define HUB_DEBOUNCE_STEP       100
662
663 /* return: -1 on error, 0 on success, 1 on disconnect.  */
664 static int usb_hub_port_debounce(struct usb_device *hub, int port)
665 {
666         int ret;
667         unsigned delay_time;
668         u16 portchange, portstatus;
669
670         for (delay_time = 0; delay_time < HUB_DEBOUNCE_TIMEOUT; /* empty */ ) {
671
672                 /* wait debounce step increment */
673                 wait_ms(HUB_DEBOUNCE_STEP);
674
675                 ret = usb_hub_port_status(hub, port, &portstatus, &portchange);
676                 if (ret < 0)
677                         return -1;
678
679                 if ((portchange & USB_PORT_STAT_C_CONNECTION)) {
680                         usb_clear_port_feature(hub, port+1, USB_PORT_FEAT_C_CONNECTION);
681                         delay_time = 0;
682                 }
683                 else
684                         delay_time += HUB_DEBOUNCE_STEP;
685         }
686         return ((portstatus&USB_PORT_STAT_CONNECTION)) ? 0 : 1;
687 }
688
689 static void usb_hub_port_connect_change(struct usb_hub *hubstate, int port,
690                                         u16 portstatus, u16 portchange)
691 {
692         struct usb_device *hub = hubstate->dev;
693         struct usb_device *dev;
694         unsigned int delay = HUB_SHORT_RESET_TIME;
695         int i;
696
697         dbg("port %d, portstatus %x, change %x, %s",
698                 port + 1, portstatus, portchange, portspeed (portstatus));
699
700         /* Clear the connection change status */
701         usb_clear_port_feature(hub, port + 1, USB_PORT_FEAT_C_CONNECTION);
702
703         /* Disconnect any existing devices under this port */
704         if (hub->children[port])
705                 usb_disconnect(&hub->children[port]);
706
707         /* Return now if nothing is connected */
708         if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
709                 if (portstatus & USB_PORT_STAT_ENABLE)
710                         usb_hub_port_disable(hub, port);
711
712                 return;
713         }
714
715         if (usb_hub_port_debounce(hub, port)) {
716                 err("connect-debounce failed, port %d disabled", port+1);
717                 usb_hub_port_disable(hub, port);
718                 return;
719         }
720
721         down(&usb_address0_sem);
722
723         for (i = 0; i < HUB_PROBE_TRIES; i++) {
724                 struct usb_device *pdev;
725                 int len;
726
727                 /* Allocate a new device struct */
728                 dev = usb_alloc_dev(hub, hub->bus);
729                 if (!dev) {
730                         err("couldn't allocate usb_device");
731                         break;
732                 }
733
734                 /* Reset the device */
735                 if (usb_hub_port_reset(hub, port, dev, delay)) {
736                         usb_free_dev(dev);
737                         break;
738                 }
739
740                 /* Find a new device ID for it */
741                 usb_connect(dev);
742
743                 /* Set up TT records, if needed  */
744                 if (hub->tt) {
745                         dev->tt = hub->tt;
746                         dev->ttport = hub->ttport;
747                 } else if (dev->speed != USB_SPEED_HIGH
748                                 && hub->speed == USB_SPEED_HIGH) {
749                         dev->tt = &hubstate->tt;
750                         dev->ttport = port + 1;
751                 }
752
753                 /* Save readable and stable topology id, distinguishing
754                  * devices by location for diagnostics, tools, etc.  The
755                  * string is a path along hub ports, from the root.  Each
756                  * device's id will be stable until USB is re-cabled, and
757                  * hubs are often labeled with these port numbers.
758                  *
759                  * Initial size: ".NN" times five hubs + NUL = 16 bytes max
760                  * (quite rare, since most hubs have 4-6 ports).
761                  */
762                 pdev = dev->parent;
763                 if (pdev->devpath [0] != '0')   /* parent not root? */
764                         len = snprintf (dev->devpath, sizeof dev->devpath,
765                                 "%s.%d", pdev->devpath, port + 1);
766                 /* root == "0", root port 2 == "2", port 3 that hub "2.3" */
767                 else
768                         len = snprintf (dev->devpath, sizeof dev->devpath,
769                                 "%d", port + 1);
770                 if (len == sizeof dev->devpath)
771                         warn ("devpath size! usb/%03d/%03d path %s",
772                                 dev->bus->busnum, dev->devnum, dev->devpath);
773                 info("new USB device %s-%s, assigned address %d",
774                         dev->bus->bus_name, dev->devpath, dev->devnum);
775
776                 /* Run it through the hoops (find a driver, etc) */
777                 if (!usb_new_device(dev)) {
778                         hub->children[port] = dev;
779                         goto done;
780                 }
781
782                 /* Free the configuration if there was an error */
783                 usb_free_dev(dev);
784
785                 /* Switch to a long reset time */
786                 delay = HUB_LONG_RESET_TIME;
787         }
788
789         usb_hub_port_disable(hub, port);
790 done:
791         up(&usb_address0_sem);
792 }
793
794 static void usb_hub_events(void)
795 {
796         unsigned long flags;
797         struct list_head *tmp;
798         struct usb_device *dev;
799         struct usb_hub *hub;
800         struct usb_hub_status *hubsts;
801         u16 hubstatus;
802         u16 hubchange;
803         u16 portstatus;
804         u16 portchange;
805         int i, ret;
806
807         /*
808          *  We restart the list everytime to avoid a deadlock with
809          * deleting hubs downstream from this one. This should be
810          * safe since we delete the hub from the event list.
811          * Not the most efficient, but avoids deadlocks.
812          */
813         while (1) {
814                 spin_lock_irqsave(&hub_event_lock, flags);
815
816                 if (list_empty(&hub_event_list))
817                         break;
818
819                 /* Grab the next entry from the beginning of the list */
820                 tmp = hub_event_list.next;
821
822                 hub = list_entry(tmp, struct usb_hub, event_list);
823                 dev = hub->dev;
824
825                 list_del(tmp);
826                 INIT_LIST_HEAD(tmp);
827
828                 down(&hub->khubd_sem); /* never blocks, we were on list */
829                 spin_unlock_irqrestore(&hub_event_lock, flags);
830
831                 if (hub->error) {
832                         dbg("resetting hub %d for error %d", dev->devnum, hub->error);
833
834                         if (usb_hub_reset(hub)) {
835                                 err("error resetting hub %d - disconnecting", dev->devnum);
836                                 up(&hub->khubd_sem);
837                                 usb_hub_disconnect(dev);
838                                 continue;
839                         }
840
841                         hub->nerrors = 0;
842                         hub->error = 0;
843                 }
844
845                 //for (i = 0; i < hub->descriptor->bNbrPorts; i++) { //+Wilson04272004
846                 for (i = (hub->descriptor->bNbrPorts - 1); i >= 0; i--) { //+Wilson04272004, Reversed the get port sequence
847                         ret = usb_hub_port_status(dev, i, &portstatus, &portchange);
848                         if (ret < 0) {
849                                 continue;
850                         }
851
852                         if (portchange & USB_PORT_STAT_C_CONNECTION) {
853                                 dbg("port %d connection change", i + 1);
854
855                                 usb_hub_port_connect_change(hub, i, portstatus, portchange);
856                         } else if (portchange & USB_PORT_STAT_C_ENABLE) {
857                                 dbg("port %d enable change, status %x", i + 1, portstatus);
858                                 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_ENABLE);
859
860                                 /*
861                                  * EM interference sometimes causes bad shielded USB devices to 
862                                  * be shutdown by the hub, this hack enables them again.
863                                  * Works at least with mouse driver. 
864                                  */
865                                 if (!(portstatus & USB_PORT_STAT_ENABLE) && 
866                                     (portstatus & USB_PORT_STAT_CONNECTION) && (dev->children[i])) {
867                                         err("already running port %i disabled by hub (EMI?), re-enabling...",
868                                                 i + 1);
869                                         usb_hub_port_connect_change(hub, i, portstatus, portchange);
870                                 }
871                         }
872
873                         if (portchange & USB_PORT_STAT_C_SUSPEND) {
874                                 dbg("port %d suspend change", i + 1);
875                                 usb_clear_port_feature(dev, i + 1,  USB_PORT_FEAT_C_SUSPEND);
876                         }
877                         
878                         if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
879                                 err("port %d over-current change", i + 1);
880                                 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_OVER_CURRENT);
881                                 usb_hub_power_on(hub);
882                         }
883
884                         if (portchange & USB_PORT_STAT_C_RESET) {
885                                 dbg("port %d reset change", i + 1);
886                                 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_RESET);
887                         }
888                 } /* end for i */
889
890                 /* deal with hub status changes */
891                 hubsts = kmalloc(sizeof *hubsts, GFP_KERNEL);
892                 if (!hubsts) {
893                         err("couldn't allocate hubsts");
894                 } else {
895                         if (usb_get_hub_status(dev, hubsts) < 0)
896                                 err("get_hub_status failed");
897                         else {
898                                 hubstatus = le16_to_cpup(&hubsts->wHubStatus);
899                                 hubchange = le16_to_cpup(&hubsts->wHubChange);
900                                 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
901                                         dbg("hub power change");
902                                         usb_clear_hub_feature(dev, C_HUB_LOCAL_POWER);
903                                 }
904                                 if (hubchange & HUB_CHANGE_OVERCURRENT) {
905                                         dbg("hub overcurrent change");
906                                         wait_ms(500);   /* Cool down */
907                                         usb_clear_hub_feature(dev, C_HUB_OVER_CURRENT);
908                                         usb_hub_power_on(hub);
909                                 }
910                         }
911                         kfree(hubsts);
912                 }
913                 up(&hub->khubd_sem);
914         } /* end while (1) */
915
916         spin_unlock_irqrestore(&hub_event_lock, flags);
917 }
918
919 static int usb_hub_thread(void *__hub)
920 {
921         lock_kernel();
922
923         /*
924          * This thread doesn't need any user-level access,
925          * so get rid of all our resources
926          */
927
928         daemonize();
929         reparent_to_init();
930
931         /* Setup a nice name */
932         strcpy(current->comm, "khubd");
933
934         /* Send me a signal to get me die (for debugging) */
935         do {
936                 usb_hub_events();
937                 wait_event_interruptible(khubd_wait, !list_empty(&hub_event_list)); 
938         } while (!signal_pending(current));
939
940         dbg("usb_hub_thread exiting");
941
942         unlock_kernel();
943         complete_and_exit(&khubd_exited, 0);
944 }
945
946 static struct usb_device_id hub_id_table [] = {
947     { match_flags: USB_DEVICE_ID_MATCH_INT_CLASS,
948       bInterfaceClass: USB_CLASS_HUB},
949     { }                                         /* Terminating entry */
950 };
951
952 MODULE_DEVICE_TABLE (usb, hub_id_table);
953
954 static struct usb_driver hub_driver = {
955         name:           "hub",
956         probe:          hub_probe,
957         ioctl:          hub_ioctl,
958         disconnect:     hub_disconnect,
959         id_table:       hub_id_table,
960 };
961
962 /*
963  * This should be a separate module.
964  */
965 int usb_hub_init(void)
966 {
967         pid_t pid;
968
969         if (usb_register(&hub_driver) < 0) {
970                 err("Unable to register USB hub driver");
971                 return -1;
972         }
973
974         pid = kernel_thread(usb_hub_thread, NULL,
975                 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
976         if (pid >= 0) {
977                 khubd_pid = pid;
978
979                 return 0;
980         }
981
982         /* Fall through if kernel_thread failed */
983         usb_deregister(&hub_driver);
984         err("failed to start usb_hub_thread");
985
986         return -1;
987 }
988
989 void usb_hub_cleanup(void)
990 {
991         int ret;
992
993         /* Kill the thread */
994         ret = kill_proc(khubd_pid, SIGTERM, 1);
995
996         wait_for_completion(&khubd_exited);
997
998         /*
999          * Hub resources are freed for us by usb_deregister. It calls
1000          * usb_driver_purge on every device which in turn calls that
1001          * devices disconnect function if it is using this driver.
1002          * The hub_disconnect function takes care of releasing the
1003          * individual hub resources. -greg
1004          */
1005         usb_deregister(&hub_driver);
1006 } /* usb_hub_cleanup() */
1007
1008 /*
1009  * WARNING - If a driver calls usb_reset_device, you should simulate a
1010  * disconnect() and probe() for other interfaces you doesn't claim. This
1011  * is left up to the driver writer right now. This insures other drivers
1012  * have a chance to re-setup their interface.
1013  *
1014  * Take a look at proc_resetdevice in devio.c for some sample code to
1015  * do this.
1016  */
1017 int usb_reset_device(struct usb_device *dev)
1018 {
1019         struct usb_device *parent = dev->parent;
1020         struct usb_device_descriptor *descriptor;
1021         int i, ret, port = -1;
1022
1023         if (!parent) {
1024                 err("attempting to reset root hub!");
1025                 return -EINVAL;
1026         }
1027
1028         for (i = 0; i < parent->maxchild; i++)
1029                 if (parent->children[i] == dev) {
1030                         port = i;
1031                         break;
1032                 }
1033
1034         if (port < 0)
1035                 return -ENOENT;
1036
1037         down(&usb_address0_sem);
1038
1039         /* Send a reset to the device */
1040         if (usb_hub_port_reset(parent, port, dev, HUB_SHORT_RESET_TIME)) {
1041                 usb_hub_port_disable(parent, port);
1042                 up(&usb_address0_sem);
1043                 return(-ENODEV);
1044         }
1045
1046         /* Reprogram the Address */
1047         ret = usb_set_address(dev);
1048         if (ret < 0) {
1049                 err("USB device not accepting new address (error=%d)", ret);
1050                 usb_hub_port_disable(parent, port);
1051                 up(&usb_address0_sem);
1052                 return ret;
1053         }
1054
1055         /* Let the SET_ADDRESS settle */
1056         wait_ms(10);
1057
1058         up(&usb_address0_sem);
1059
1060         /*
1061          * Now we fetch the configuration descriptors for the device and
1062          * see if anything has changed. If it has, we dump the current
1063          * parsed descriptors and reparse from scratch. Then we leave
1064          * the device alone for the caller to finish setting up.
1065          *
1066          * If nothing changed, we reprogram the configuration and then
1067          * the alternate settings.
1068          */
1069         descriptor = kmalloc(sizeof *descriptor, GFP_NOIO);
1070         if (!descriptor) {
1071                 return -ENOMEM;
1072         }
1073         ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, descriptor,
1074                         sizeof(*descriptor));
1075         if (ret < 0) {
1076                 kfree(descriptor);
1077                 return ret;
1078         }
1079
1080         le16_to_cpus(&descriptor->bcdUSB);
1081         le16_to_cpus(&descriptor->idVendor);
1082         le16_to_cpus(&descriptor->idProduct);
1083         le16_to_cpus(&descriptor->bcdDevice);
1084
1085         if (memcmp(&dev->descriptor, descriptor, sizeof(*descriptor))) {
1086                 kfree(descriptor);
1087                 usb_destroy_configuration(dev);
1088
1089                 ret = usb_get_device_descriptor(dev);
1090                 if (ret < sizeof(dev->descriptor)) {
1091                         if (ret < 0)
1092                                 err("unable to get device descriptor (error=%d)", ret);
1093                         else
1094                                 err("USB device descriptor short read (expected %Zi, got %i)", sizeof(dev->descriptor), ret);
1095         
1096                         clear_bit(dev->devnum, &dev->bus->devmap.devicemap);
1097                         dev->devnum = -1;
1098                         return -EIO;
1099                 }
1100
1101                 ret = usb_get_configuration(dev);
1102                 if (ret < 0) {
1103                         err("unable to get configuration (error=%d)", ret);
1104                         usb_destroy_configuration(dev);
1105                         clear_bit(dev->devnum, &dev->bus->devmap.devicemap);
1106                         dev->devnum = -1;
1107                         return 1;
1108                 }
1109
1110                 dev->actconfig = dev->config;
1111                 usb_set_maxpacket(dev);
1112
1113                 return 1;
1114         }
1115
1116         kfree(descriptor);
1117
1118         ret = usb_set_configuration(dev, dev->actconfig->bConfigurationValue);
1119         if (ret < 0) {
1120                 err("failed to set active configuration (error=%d)", ret);
1121                 return ret;
1122         }
1123
1124         for (i = 0; i < dev->actconfig->bNumInterfaces; i++) {
1125                 struct usb_interface *intf = &dev->actconfig->interface[i];
1126                 struct usb_interface_descriptor *as = &intf->altsetting[intf->act_altsetting];
1127
1128                 ret = usb_set_interface(dev, as->bInterfaceNumber, as->bAlternateSetting);
1129                 if (ret < 0) {
1130                         err("failed to set active alternate setting for interface %d (error=%d)", i, ret);
1131                         return ret;
1132                 }
1133         }
1134
1135         return 0;
1136 }
1137