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