ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / usb / core / 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/errno.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/completion.h>
17 #include <linux/sched.h>
18 #include <linux/list.h>
19 #include <linux/slab.h>
20 #include <linux/smp_lock.h>
21 #include <linux/ioctl.h>
22 #ifdef CONFIG_USB_DEBUG
23         #define DEBUG
24 #else
25         #undef DEBUG
26 #endif
27 #include <linux/usb.h>
28 #include <linux/usbdevice_fs.h>
29 #include <linux/suspend.h>
30
31 #include <asm/semaphore.h>
32 #include <asm/uaccess.h>
33 #include <asm/byteorder.h>
34
35 #include "usb.h"
36 #include "hcd.h"
37 #include "hub.h"
38
39 /* Wakes up khubd */
40 static spinlock_t hub_event_lock = SPIN_LOCK_UNLOCKED;
41 static DECLARE_MUTEX(usb_address0_sem);
42
43 static LIST_HEAD(hub_event_list);       /* List of hubs needing servicing */
44 static LIST_HEAD(hub_list);             /* List of all hubs (for cleanup) */
45
46 static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
47 static pid_t khubd_pid = 0;                     /* PID of khubd */
48 static DECLARE_COMPLETION(khubd_exited);
49
50 /* cycle leds on hubs that aren't blinking for attention */
51 static int blinkenlights = 0;
52 module_param (blinkenlights, bool, S_IRUGO);
53 MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
54
55
56 #ifdef  DEBUG
57 static inline char *portspeed (int portstatus)
58 {
59         if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
60                 return "480 Mb/s";
61         else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
62                 return "1.5 Mb/s";
63         else
64                 return "12 Mb/s";
65 }
66 #endif
67
68 /* for dev_info, dev_dbg, etc */
69 static inline struct device *hubdev (struct usb_device *dev)
70 {
71         return &dev->actconfig->interface[0]->dev;
72 }
73
74 /* USB 2.0 spec Section 11.24.4.5 */
75 static int get_hub_descriptor(struct usb_device *dev, void *data, int size)
76 {
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);
80 }
81
82 /*
83  * USB 2.0 spec Section 11.24.2.1
84  */
85 static int clear_hub_feature(struct usb_device *dev, int feature)
86 {
87         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
88                 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, HZ);
89 }
90
91 /*
92  * USB 2.0 spec Section 11.24.2.2
93  */
94 static int clear_port_feature(struct usb_device *dev, int port, int feature)
95 {
96         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
97                 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port, NULL, 0, HZ);
98 }
99
100 /*
101  * USB 2.0 spec Section 11.24.2.13
102  */
103 static int set_port_feature(struct usb_device *dev, int port, int feature)
104 {
105         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
106                 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port, NULL, 0, HZ);
107 }
108
109 /*
110  * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
111  * for info about using port indicators
112  */
113 static void set_port_led(
114         struct usb_device *dev,
115         struct usb_hub *hub,
116         int port,
117         int selector
118 )
119 {
120         int status = set_port_feature(dev, (selector << 8) | port,
121                         USB_PORT_FEAT_INDICATOR);
122         if (status < 0)
123                 dev_dbg (&hub->intf->dev,
124                         "port %d indicator %s status %d\n",
125                         port,
126                         ({ char *s; switch (selector) {
127                         case HUB_LED_AMBER: s = "amber"; break;
128                         case HUB_LED_GREEN: s = "green"; break;
129                         case HUB_LED_OFF: s = "off"; break;
130                         case HUB_LED_AUTO: s = "auto"; break;
131                         default: s = "??"; break;
132                         }; s; }),
133                         status);
134 }
135
136 #define LED_CYCLE_PERIOD        ((2*HZ)/3)
137
138 static void led_work (void *__hub)
139 {
140         struct usb_hub          *hub = __hub;
141         struct usb_device       *dev = interface_to_usbdev (hub->intf);
142         unsigned                i;
143         unsigned                changed = 0;
144         int                     cursor = -1;
145
146         if (dev->state != USB_STATE_CONFIGURED)
147                 return;
148
149         for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
150                 unsigned        selector, mode;
151
152                 /* 30%-50% duty cycle */
153
154                 switch (hub->indicator[i]) {
155                 /* cycle marker */
156                 case INDICATOR_CYCLE:
157                         cursor = i;
158                         selector = HUB_LED_AUTO;
159                         mode = INDICATOR_AUTO;
160                         break;
161                 /* blinking green = sw attention */
162                 case INDICATOR_GREEN_BLINK:
163                         selector = HUB_LED_GREEN;
164                         mode = INDICATOR_GREEN_BLINK_OFF;
165                         break;
166                 case INDICATOR_GREEN_BLINK_OFF:
167                         selector = HUB_LED_OFF;
168                         mode = INDICATOR_GREEN_BLINK;
169                         break;
170                 /* blinking amber = hw attention */
171                 case INDICATOR_AMBER_BLINK:
172                         selector = HUB_LED_AMBER;
173                         mode = INDICATOR_AMBER_BLINK_OFF;
174                         break;
175                 case INDICATOR_AMBER_BLINK_OFF:
176                         selector = HUB_LED_OFF;
177                         mode = INDICATOR_AMBER_BLINK;
178                         break;
179                 /* blink green/amber = reserved */
180                 case INDICATOR_ALT_BLINK:
181                         selector = HUB_LED_GREEN;
182                         mode = INDICATOR_ALT_BLINK_OFF;
183                         break;
184                 case INDICATOR_ALT_BLINK_OFF:
185                         selector = HUB_LED_AMBER;
186                         mode = INDICATOR_ALT_BLINK;
187                         break;
188                 default:
189                         continue;
190                 }
191                 if (selector != HUB_LED_AUTO)
192                         changed = 1;
193                 set_port_led(dev, hub, i + 1, selector);
194                 hub->indicator[i] = mode;
195         }
196         if (!changed && blinkenlights) {
197                 cursor++;
198                 cursor %= hub->descriptor->bNbrPorts;
199                 set_port_led(dev, hub, cursor + 1, HUB_LED_GREEN);
200                 hub->indicator[cursor] = INDICATOR_CYCLE;
201                 changed++;
202         }
203         if (changed)
204                 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
205 }
206
207 /*
208  * USB 2.0 spec Section 11.24.2.6
209  */
210 static int get_hub_status(struct usb_device *dev,
211                 struct usb_hub_status *data)
212 {
213         return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
214                 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
215                 data, sizeof(*data), HZ * USB_CTRL_GET_TIMEOUT);
216 }
217
218 /*
219  * USB 2.0 spec Section 11.24.2.7
220  */
221 static int get_port_status(struct usb_device *dev, int port,
222                 struct usb_port_status *data)
223 {
224         return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
225                 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
226                 data, sizeof(*data), HZ * USB_CTRL_GET_TIMEOUT);
227 }
228
229 /* completion function, fires on port status changes and various faults */
230 static void hub_irq(struct urb *urb, struct pt_regs *regs)
231 {
232         struct usb_hub *hub = (struct usb_hub *)urb->context;
233         int status;
234
235         spin_lock(&hub_event_lock);
236         hub->urb_active = 0;
237         if (hub->urb_complete) {        /* disconnect or rmmod */
238                 complete(hub->urb_complete);
239                 goto done;
240         }
241
242         switch (urb->status) {
243         case -ENOENT:           /* synchronous unlink */
244         case -ECONNRESET:       /* async unlink */
245         case -ESHUTDOWN:        /* hardware going away */
246                 goto done;
247
248         default:                /* presumably an error */
249                 /* Cause a hub reset after 10 consecutive errors */
250                 dev_dbg (&hub->intf->dev, "transfer --> %d\n", urb->status);
251                 if ((++hub->nerrors < 10) || hub->error)
252                         goto resubmit;
253                 hub->error = urb->status;
254                 /* FALL THROUGH */
255         
256         /* let khubd handle things */
257         case 0:                 /* we got data:  port status changed */
258                 break;
259         }
260
261         hub->nerrors = 0;
262
263         /* Something happened, let khubd figure it out */
264         if (list_empty(&hub->event_list)) {
265                 list_add(&hub->event_list, &hub_event_list);
266                 wake_up(&khubd_wait);
267         }
268
269 resubmit:
270         if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
271                         /* ENODEV means we raced disconnect() */
272                         && status != -ENODEV)
273                 dev_err (&hub->intf->dev, "resubmit --> %d\n", urb->status);
274         if (status == 0)
275                 hub->urb_active = 1;
276 done:
277         spin_unlock(&hub_event_lock);
278 }
279
280 /* USB 2.0 spec Section 11.24.2.3 */
281 static inline int
282 hub_clear_tt_buffer (struct usb_device *hub, u16 devinfo, u16 tt)
283 {
284         return usb_control_msg (hub, usb_rcvctrlpipe (hub, 0),
285                 HUB_CLEAR_TT_BUFFER, USB_RT_PORT,
286                 devinfo, tt, 0, 0, HZ);
287 }
288
289 /*
290  * enumeration blocks khubd for a long time. we use keventd instead, since
291  * long blocking there is the exception, not the rule.  accordingly, HCDs
292  * talking to TTs must queue control transfers (not just bulk and iso), so
293  * both can talk to the same hub concurrently.
294  */
295 static void hub_tt_kevent (void *arg)
296 {
297         struct usb_hub          *hub = arg;
298         unsigned long           flags;
299
300         spin_lock_irqsave (&hub->tt.lock, flags);
301         while (!list_empty (&hub->tt.clear_list)) {
302                 struct list_head        *temp;
303                 struct usb_tt_clear     *clear;
304                 struct usb_device       *dev;
305                 int                     status;
306
307                 temp = hub->tt.clear_list.next;
308                 clear = list_entry (temp, struct usb_tt_clear, clear_list);
309                 list_del (&clear->clear_list);
310
311                 /* drop lock so HCD can concurrently report other TT errors */
312                 spin_unlock_irqrestore (&hub->tt.lock, flags);
313                 dev = interface_to_usbdev (hub->intf);
314                 status = hub_clear_tt_buffer (dev, clear->devinfo, clear->tt);
315                 spin_lock_irqsave (&hub->tt.lock, flags);
316
317                 if (status)
318                         dev_err (&dev->dev, "clear tt %d (%04x) error %d\n",
319                                 clear->tt, clear->devinfo, status);
320                 kfree (clear);
321         }
322         spin_unlock_irqrestore (&hub->tt.lock, flags);
323 }
324
325 /**
326  * usb_hub_tt_clear_buffer - clear control/bulk TT state in high speed hub
327  * @dev: the device whose split transaction failed
328  * @pipe: identifies the endpoint of the failed transaction
329  *
330  * High speed HCDs use this to tell the hub driver that some split control or
331  * bulk transaction failed in a way that requires clearing internal state of
332  * a transaction translator.  This is normally detected (and reported) from
333  * interrupt context.
334  *
335  * It may not be possible for that hub to handle additional full (or low)
336  * speed transactions until that state is fully cleared out.
337  */
338 void usb_hub_tt_clear_buffer (struct usb_device *dev, int pipe)
339 {
340         struct usb_tt           *tt = dev->tt;
341         unsigned long           flags;
342         struct usb_tt_clear     *clear;
343
344         /* we've got to cope with an arbitrary number of pending TT clears,
345          * since each TT has "at least two" buffers that can need it (and
346          * there can be many TTs per hub).  even if they're uncommon.
347          */
348         if ((clear = kmalloc (sizeof *clear, SLAB_ATOMIC)) == 0) {
349                 dev_err (&dev->dev, "can't save CLEAR_TT_BUFFER state\n");
350                 /* FIXME recover somehow ... RESET_TT? */
351                 return;
352         }
353
354         /* info that CLEAR_TT_BUFFER needs */
355         clear->tt = tt->multi ? dev->ttport : 1;
356         clear->devinfo = usb_pipeendpoint (pipe);
357         clear->devinfo |= dev->devnum << 4;
358         clear->devinfo |= usb_pipecontrol (pipe)
359                         ? (USB_ENDPOINT_XFER_CONTROL << 11)
360                         : (USB_ENDPOINT_XFER_BULK << 11);
361         if (usb_pipein (pipe))
362                 clear->devinfo |= 1 << 15;
363         
364         /* tell keventd to clear state for this TT */
365         spin_lock_irqsave (&tt->lock, flags);
366         list_add_tail (&clear->clear_list, &tt->clear_list);
367         schedule_work (&tt->kevent);
368         spin_unlock_irqrestore (&tt->lock, flags);
369 }
370
371 static void hub_power_on(struct usb_hub *hub)
372 {
373         struct usb_device *dev;
374         int i;
375
376         /* Enable power to the ports */
377         dev_dbg(hubdev(interface_to_usbdev(hub->intf)),
378                 "enabling power on all ports\n");
379         dev = interface_to_usbdev(hub->intf);
380         for (i = 0; i < hub->descriptor->bNbrPorts; i++)
381                 set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
382
383         /* Wait for power to be enabled */
384         wait_ms(hub->descriptor->bPwrOn2PwrGood * 2);
385 }
386
387 static int hub_hub_status(struct usb_hub *hub,
388                 u16 *status, u16 *change)
389 {
390         struct usb_device *dev = interface_to_usbdev (hub->intf);
391         int ret;
392
393         ret = get_hub_status(dev, &hub->status->hub);
394         if (ret < 0)
395                 dev_err (hubdev (dev),
396                         "%s failed (err = %d)\n", __FUNCTION__, ret);
397         else {
398                 *status = le16_to_cpu(hub->status->hub.wHubStatus);
399                 *change = le16_to_cpu(hub->status->hub.wHubChange); 
400                 ret = 0;
401         }
402         return ret;
403 }
404
405 static int hub_configure(struct usb_hub *hub,
406         struct usb_endpoint_descriptor *endpoint)
407 {
408         struct usb_device *dev = interface_to_usbdev (hub->intf);
409         struct device *hub_dev;
410         u16 hubstatus, hubchange;
411         unsigned int pipe;
412         int maxp, ret;
413         char *message;
414
415         hub->buffer = usb_buffer_alloc(dev, sizeof(*hub->buffer), GFP_KERNEL,
416                         &hub->buffer_dma);
417         if (!hub->buffer) {
418                 message = "can't allocate hub irq buffer";
419                 ret = -ENOMEM;
420                 goto fail;
421         }
422
423         hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
424         if (!hub->status) {
425                 message = "can't kmalloc hub status buffer";
426                 ret = -ENOMEM;
427                 goto fail;
428         }
429
430         hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
431         if (!hub->descriptor) {
432                 message = "can't kmalloc hub descriptor";
433                 ret = -ENOMEM;
434                 goto fail;
435         }
436
437         /* Request the entire hub descriptor.
438          * hub->descriptor can handle USB_MAXCHILDREN ports,
439          * but the hub can/will return fewer bytes here.
440          */
441         ret = get_hub_descriptor(dev, hub->descriptor,
442                         sizeof(*hub->descriptor));
443         if (ret < 0) {
444                 message = "can't read hub descriptor";
445                 goto fail;
446         } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
447                 message = "hub has too many ports!";
448                 ret = -ENODEV;
449                 goto fail;
450         }
451
452         hub_dev = hubdev(dev);
453         dev->maxchild = hub->descriptor->bNbrPorts;
454         dev_info (hub_dev, "%d port%s detected\n", dev->maxchild,
455                 (dev->maxchild == 1) ? "" : "s");
456
457         le16_to_cpus(&hub->descriptor->wHubCharacteristics);
458
459         if (hub->descriptor->wHubCharacteristics & HUB_CHAR_COMPOUND) {
460                 int     i;
461                 char    portstr [USB_MAXCHILDREN + 1];
462
463                 for (i = 0; i < dev->maxchild; i++)
464                         portstr[i] = hub->descriptor->DeviceRemovable
465                                     [((i + 1) / 8)] & (1 << ((i + 1) % 8))
466                                 ? 'F' : 'R';
467                 portstr[dev->maxchild] = 0;
468                 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
469         } else
470                 dev_dbg(hub_dev, "standalone hub\n");
471
472         switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_LPSM) {
473                 case 0x00:
474                         dev_dbg(hub_dev, "ganged power switching\n");
475                         break;
476                 case 0x01:
477                         dev_dbg(hub_dev, "individual port power switching\n");
478                         break;
479                 case 0x02:
480                 case 0x03:
481                         dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
482                         break;
483         }
484
485         switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_OCPM) {
486                 case 0x00:
487                         dev_dbg(hub_dev, "global over-current protection\n");
488                         break;
489                 case 0x08:
490                         dev_dbg(hub_dev, "individual port over-current protection\n");
491                         break;
492                 case 0x10:
493                 case 0x18:
494                         dev_dbg(hub_dev, "no over-current protection\n");
495                         break;
496         }
497
498         spin_lock_init (&hub->tt.lock);
499         INIT_LIST_HEAD (&hub->tt.clear_list);
500         INIT_WORK (&hub->tt.kevent, hub_tt_kevent, hub);
501         switch (dev->descriptor.bDeviceProtocol) {
502                 case 0:
503                         break;
504                 case 1:
505                         dev_dbg(hub_dev, "Single TT\n");
506                         hub->tt.hub = dev;
507                         break;
508                 case 2:
509                         ret = usb_set_interface(dev, 0, 1);
510                         if (ret == 0) {
511                                 dev_dbg(hub_dev, "TT per port\n");
512                                 hub->tt.multi = 1;
513                         } else
514                                 dev_err(hub_dev, "Using single TT (err %d)\n",
515                                         ret);
516                         hub->tt.hub = dev;
517                         break;
518                 default:
519                         dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
520                                 dev->descriptor.bDeviceProtocol);
521                         break;
522         }
523
524         switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_TTTT) {
525                 case 0x00:
526                         if (dev->descriptor.bDeviceProtocol != 0)
527                                 dev_dbg(hub_dev, "TT requires at most 8 FS bit times\n");
528                         break;
529                 case 0x20:
530                         dev_dbg(hub_dev, "TT requires at most 16 FS bit times\n");
531                         break;
532                 case 0x40:
533                         dev_dbg(hub_dev, "TT requires at most 24 FS bit times\n");
534                         break;
535                 case 0x60:
536                         dev_dbg(hub_dev, "TT requires at most 32 FS bit times\n");
537                         break;
538         }
539
540         /* probe() zeroes hub->indicator[] */
541         if (hub->descriptor->wHubCharacteristics & HUB_CHAR_PORTIND) {
542                 hub->has_indicators = 1;
543                 dev_dbg(hub_dev, "Port indicators are supported\n");
544         }
545
546         dev_dbg(hub_dev, "power on to power good time: %dms\n",
547                 hub->descriptor->bPwrOn2PwrGood * 2);
548         dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
549                 hub->descriptor->bHubContrCurrent);
550
551         ret = hub_hub_status(hub, &hubstatus, &hubchange);
552         if (ret < 0) {
553                 message = "can't get hub status";
554                 goto fail;
555         }
556
557         /* FIXME implement per-port power budgeting;
558          * enable it for bus-powered hubs.
559          */
560         dev_dbg(hub_dev, "local power source is %s\n",
561                 (hubstatus & HUB_STATUS_LOCAL_POWER)
562                 ? "lost (inactive)" : "good");
563
564         if ((hub->descriptor->wHubCharacteristics & HUB_CHAR_OCPM) == 0)
565                 dev_dbg(hub_dev, "%sover-current condition exists\n",
566                         (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
567
568         /* Start the interrupt endpoint */
569         pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
570         maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
571
572         if (maxp > sizeof(*hub->buffer))
573                 maxp = sizeof(*hub->buffer);
574
575         hub->urb = usb_alloc_urb(0, GFP_KERNEL);
576         if (!hub->urb) {
577                 message = "couldn't allocate interrupt urb";
578                 ret = -ENOMEM;
579                 goto fail;
580         }
581
582         usb_fill_int_urb(hub->urb, dev, pipe, *hub->buffer, maxp, hub_irq,
583                 hub, endpoint->bInterval);
584         hub->urb->transfer_dma = hub->buffer_dma;
585         hub->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
586         ret = usb_submit_urb(hub->urb, GFP_KERNEL);
587         if (ret) {
588                 message = "couldn't submit status urb";
589                 goto fail;
590         }
591         hub->urb_active = 1;
592
593         /* Wake up khubd */
594         wake_up(&khubd_wait);
595
596         /* maybe start cycling the hub leds */
597         if (hub->has_indicators && blinkenlights) {
598                 set_port_led(dev, hub, 1, HUB_LED_GREEN);
599                 hub->indicator [0] = INDICATOR_CYCLE;
600                 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
601         }
602
603         hub_power_on(hub);
604
605         return 0;
606
607 fail:
608         dev_err (&hub->intf->dev, "config failed, %s (err %d)\n",
609                         message, ret);
610         /* hub_disconnect() frees urb and descriptor */
611         return ret;
612 }
613
614 static void hub_disconnect(struct usb_interface *intf)
615 {
616         struct usb_hub *hub = usb_get_intfdata (intf);
617         DECLARE_COMPLETION(urb_complete);
618         unsigned long flags;
619
620         if (!hub)
621                 return;
622
623         usb_set_intfdata (intf, NULL);
624         spin_lock_irqsave(&hub_event_lock, flags);
625         hub->urb_complete = &urb_complete;
626
627         /* Delete it and then reset it */
628         list_del_init(&hub->event_list);
629         list_del_init(&hub->hub_list);
630
631         spin_unlock_irqrestore(&hub_event_lock, flags);
632
633         down(&hub->khubd_sem); /* Wait for khubd to leave this hub alone. */
634         up(&hub->khubd_sem);
635
636         /* assuming we used keventd, it must quiesce too */
637         if (hub->has_indicators)
638                 cancel_delayed_work (&hub->leds);
639         if (hub->has_indicators || hub->tt.hub)
640                 flush_scheduled_work ();
641
642         if (hub->urb) {
643                 usb_unlink_urb(hub->urb);
644                 if (hub->urb_active)
645                         wait_for_completion(&urb_complete);
646                 usb_free_urb(hub->urb);
647                 hub->urb = NULL;
648         }
649
650         if (hub->descriptor) {
651                 kfree(hub->descriptor);
652                 hub->descriptor = NULL;
653         }
654
655         if (hub->status) {
656                 kfree(hub->status);
657                 hub->status = NULL;
658         }
659
660         if (hub->buffer) {
661                 usb_buffer_free(interface_to_usbdev(intf),
662                                 sizeof(*hub->buffer), hub->buffer,
663                                 hub->buffer_dma);
664                 hub->buffer = NULL;
665         }
666
667         /* Free the memory */
668         kfree(hub);
669 }
670
671 static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
672 {
673         struct usb_host_interface *desc;
674         struct usb_endpoint_descriptor *endpoint;
675         struct usb_device *dev;
676         struct usb_hub *hub;
677         unsigned long flags;
678
679         desc = intf->cur_altsetting;
680         dev = interface_to_usbdev(intf);
681
682         /* Some hubs have a subclass of 1, which AFAICT according to the */
683         /*  specs is not defined, but it works */
684         if ((desc->desc.bInterfaceSubClass != 0) &&
685             (desc->desc.bInterfaceSubClass != 1)) {
686 descriptor_error:
687                 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
688                 return -EIO;
689         }
690
691         /* Multiple endpoints? What kind of mutant ninja-hub is this? */
692         if (desc->desc.bNumEndpoints != 1) {
693                 goto descriptor_error;
694         }
695
696         endpoint = &desc->endpoint[0].desc;
697
698         /* Output endpoint? Curiouser and curiouser.. */
699         if (!(endpoint->bEndpointAddress & USB_DIR_IN)) {
700                 goto descriptor_error;
701         }
702
703         /* If it's not an interrupt endpoint, we'd better punt! */
704         if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
705                         != USB_ENDPOINT_XFER_INT) {
706                 goto descriptor_error;
707                 return -EIO;
708         }
709
710         /* We found a hub */
711         dev_info (hubdev (dev), "USB hub found\n");
712
713         hub = kmalloc(sizeof(*hub), GFP_KERNEL);
714         if (!hub) {
715                 dev_dbg (hubdev(dev), "couldn't kmalloc hub struct\n");
716                 return -ENOMEM;
717         }
718
719         memset(hub, 0, sizeof(*hub));
720
721         INIT_LIST_HEAD(&hub->event_list);
722         hub->intf = intf;
723         init_MUTEX(&hub->khubd_sem);
724         INIT_WORK(&hub->leds, led_work, hub);
725
726         /* Record the new hub's existence */
727         spin_lock_irqsave(&hub_event_lock, flags);
728         INIT_LIST_HEAD(&hub->hub_list);
729         list_add(&hub->hub_list, &hub_list);
730         spin_unlock_irqrestore(&hub_event_lock, flags);
731
732         usb_set_intfdata (intf, hub);
733
734         if (hub_configure(hub, endpoint) >= 0)
735                 return 0;
736
737         hub_disconnect (intf);
738         return -ENODEV;
739 }
740
741 static int
742 hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
743 {
744         struct usb_device *hub = interface_to_usbdev (intf);
745
746         /* assert ifno == 0 (part of hub spec) */
747         switch (code) {
748         case USBDEVFS_HUB_PORTINFO: {
749                 struct usbdevfs_hub_portinfo *info = user_data;
750                 unsigned long flags;
751                 int i;
752
753                 spin_lock_irqsave(&hub_event_lock, flags);
754                 if (hub->devnum <= 0)
755                         info->nports = 0;
756                 else {
757                         info->nports = hub->maxchild;
758                         for (i = 0; i < info->nports; i++) {
759                                 if (hub->children[i] == NULL)
760                                         info->port[i] = 0;
761                                 else
762                                         info->port[i] =
763                                                 hub->children[i]->devnum;
764                         }
765                 }
766                 spin_unlock_irqrestore(&hub_event_lock, flags);
767
768                 return info->nports + 1;
769                 }
770
771         default:
772                 return -ENOSYS;
773         }
774 }
775
776 static int hub_reset(struct usb_hub *hub)
777 {
778         struct usb_device *dev = interface_to_usbdev(hub->intf);
779         int i;
780
781         /* Disconnect any attached devices */
782         for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
783                 if (dev->children[i])
784                         usb_disconnect(&dev->children[i]);
785         }
786
787         /* Attempt to reset the hub */
788         if (hub->urb)
789                 usb_unlink_urb(hub->urb);
790         else
791                 return -1;
792
793         if (usb_reset_device(dev))
794                 return -1;
795
796         hub->urb->dev = dev;                                                    
797         if (usb_submit_urb(hub->urb, GFP_KERNEL))
798                 return -1;
799
800         hub_power_on(hub);
801
802         return 0;
803 }
804
805 static void hub_start_disconnect(struct usb_device *dev)
806 {
807         struct usb_device *parent = dev->parent;
808         int i;
809
810         /* Find the device pointer to disconnect */
811         if (parent) {
812                 for (i = 0; i < parent->maxchild; i++) {
813                         if (parent->children[i] == dev) {
814                                 usb_disconnect(&parent->children[i]);
815                                 return;
816                         }
817                 }
818         }
819
820         dev_err(&dev->dev, "cannot disconnect hub!\n");
821 }
822
823 static int hub_port_status(struct usb_device *dev, int port,
824                                u16 *status, u16 *change)
825 {
826         struct usb_hub *hub = usb_get_intfdata(dev->actconfig->interface[0]);
827         int ret;
828
829         if (!hub)
830                 return -ENODEV;
831
832         ret = get_port_status(dev, port + 1, &hub->status->port);
833         if (ret < 0)
834                 dev_err (hubdev (dev),
835                         "%s failed (err = %d)\n", __FUNCTION__, ret);
836         else {
837                 *status = le16_to_cpu(hub->status->port.wPortStatus);
838                 *change = le16_to_cpu(hub->status->port.wPortChange); 
839                 ret = 0;
840         }
841         return ret;
842 }
843
844 #define HUB_RESET_TRIES         5
845 #define HUB_PROBE_TRIES         2
846 #define HUB_ROOT_RESET_TIME     50      /* times are in msec */
847 #define HUB_SHORT_RESET_TIME    10
848 #define HUB_LONG_RESET_TIME     200
849 #define HUB_RESET_TIMEOUT       500
850
851 /* return: -1 on error, 0 on success, 1 on disconnect.  */
852 static int hub_port_wait_reset(struct usb_device *hub, int port,
853                                 struct usb_device *dev, unsigned int delay)
854 {
855         int delay_time, ret;
856         u16 portstatus;
857         u16 portchange;
858
859         for (delay_time = 0;
860                         delay_time < HUB_RESET_TIMEOUT;
861                         delay_time += delay) {
862                 /* wait to give the device a chance to reset */
863                 wait_ms(delay);
864
865                 /* read and decode port status */
866                 ret = hub_port_status(hub, port, &portstatus, &portchange);
867                 if (ret < 0) {
868                         return -1;
869                 }
870
871                 /* Device went away? */
872                 if (!(portstatus & USB_PORT_STAT_CONNECTION))
873                         return 1;
874
875                 /* bomb out completely if something weird happened */
876                 if ((portchange & USB_PORT_STAT_C_CONNECTION))
877                         return -1;
878
879                 /* if we`ve finished resetting, then break out of the loop */
880                 if (!(portstatus & USB_PORT_STAT_RESET) &&
881                     (portstatus & USB_PORT_STAT_ENABLE)) {
882                         if (portstatus & USB_PORT_STAT_HIGH_SPEED)
883                                 dev->speed = USB_SPEED_HIGH;
884                         else if (portstatus & USB_PORT_STAT_LOW_SPEED)
885                                 dev->speed = USB_SPEED_LOW;
886                         else
887                                 dev->speed = USB_SPEED_FULL;
888                         return 0;
889                 }
890
891                 /* switch to the long delay after two short delay failures */
892                 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
893                         delay = HUB_LONG_RESET_TIME;
894
895                 dev_dbg (hubdev (hub),
896                         "port %d not reset yet, waiting %dms\n",
897                         port + 1, delay);
898         }
899
900         return -1;
901 }
902
903 /* return: -1 on error, 0 on success, 1 on disconnect.  */
904 static int hub_port_reset(struct usb_device *hub, int port,
905                                 struct usb_device *dev, unsigned int delay)
906 {
907         int i, status;
908
909         /* Reset the port */
910         for (i = 0; i < HUB_RESET_TRIES; i++) {
911                 set_port_feature(hub, port + 1, USB_PORT_FEAT_RESET);
912
913                 /* return on disconnect or reset */
914                 status = hub_port_wait_reset(hub, port, dev, delay);
915                 if (status != -1) {
916                         clear_port_feature(hub,
917                                 port + 1, USB_PORT_FEAT_C_RESET);
918                         dev->state = status
919                                         ? USB_STATE_NOTATTACHED
920                                         : USB_STATE_DEFAULT;
921                         return status;
922                 }
923
924                 dev_dbg (hubdev (hub),
925                         "port %d not enabled, trying reset again...\n",
926                         port + 1);
927                 delay = HUB_LONG_RESET_TIME;
928         }
929
930         dev_err (hubdev (hub),
931                 "Cannot enable port %i.  Maybe the USB cable is bad?\n",
932                 port + 1);
933
934         return -1;
935 }
936
937 int hub_port_disable(struct usb_device *hub, int port)
938 {
939         int ret;
940
941         ret = clear_port_feature(hub, port + 1, USB_PORT_FEAT_ENABLE);
942         if (ret)
943                 dev_err(hubdev(hub), "cannot disable port %d (err = %d)\n",
944                         port + 1, ret);
945
946         return ret;
947 }
948
949 /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
950  *
951  * Between connect detection and reset signaling there must be a delay
952  * of 100ms at least for debounce and power-settling. The corresponding
953  * timer shall restart whenever the downstream port detects a disconnect.
954  * 
955  * Apparently there are some bluetooth and irda-dongles and a number
956  * of low-speed devices which require longer delays of about 200-400ms.
957  * Not covered by the spec - but easy to deal with.
958  *
959  * This implementation uses 400ms minimum debounce timeout and checks
960  * every 25ms for transient disconnects to restart the delay.
961  */
962
963 #define HUB_DEBOUNCE_TIMEOUT    400
964 #define HUB_DEBOUNCE_STEP        25
965 #define HUB_DEBOUNCE_STABLE       4
966
967 /* return: -1 on error, 0 on success, 1 on disconnect.  */
968 static int hub_port_debounce(struct usb_device *hub, int port)
969 {
970         int ret;
971         int delay_time, stable_count;
972         u16 portchange, portstatus;
973         unsigned connection;
974
975         connection = 0;
976         stable_count = 0;
977         for (delay_time = 0; delay_time < HUB_DEBOUNCE_TIMEOUT; delay_time += HUB_DEBOUNCE_STEP) {
978                 wait_ms(HUB_DEBOUNCE_STEP);
979
980                 ret = hub_port_status(hub, port, &portstatus, &portchange);
981                 if (ret < 0)
982                         return -1;
983
984                 if ((portstatus & USB_PORT_STAT_CONNECTION) == connection) {
985                         if (connection) {
986                                 if (++stable_count == HUB_DEBOUNCE_STABLE)
987                                         break;
988                         }
989                 } else {
990                         stable_count = 0;
991                 }
992                 connection = portstatus & USB_PORT_STAT_CONNECTION;
993
994                 if ((portchange & USB_PORT_STAT_C_CONNECTION)) {
995                         clear_port_feature(hub, port+1, USB_PORT_FEAT_C_CONNECTION);
996                 }
997         }
998
999         dev_dbg (hubdev (hub),
1000                 "debounce: port %d: delay %dms stable %d status 0x%x\n",
1001                 port + 1, delay_time, stable_count, portstatus);
1002
1003         return ((portstatus&USB_PORT_STAT_CONNECTION)) ? 0 : 1;
1004 }
1005
1006 static void hub_port_connect_change(struct usb_hub *hubstate, int port,
1007                                         u16 portstatus, u16 portchange)
1008 {
1009         struct usb_device *hub = interface_to_usbdev(hubstate->intf);
1010         struct usb_device *dev;
1011         unsigned int delay = HUB_SHORT_RESET_TIME;
1012         int i;
1013
1014         dev_dbg (&hubstate->intf->dev,
1015                 "port %d, status %x, change %x, %s\n",
1016                 port + 1, portstatus, portchange, portspeed (portstatus));
1017
1018         /* Clear the connection change status */
1019         clear_port_feature(hub, port + 1, USB_PORT_FEAT_C_CONNECTION);
1020
1021         /* Disconnect any existing devices under this port */
1022         if (hub->children[port])
1023                 usb_disconnect(&hub->children[port]);
1024
1025         /* Return now if nothing is connected */
1026         if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
1027                 if (portstatus & USB_PORT_STAT_ENABLE)
1028                         hub_port_disable(hub, port);
1029
1030                 return;
1031         }
1032
1033         if (hub_port_debounce(hub, port)) {
1034                 dev_err (&hubstate->intf->dev,
1035                         "connect-debounce failed, port %d disabled\n",
1036                         port+1);
1037                 hub_port_disable(hub, port);
1038                 return;
1039         }
1040
1041         /* root hub ports have a slightly longer reset period
1042          * (from USB 2.0 spec, section 7.1.7.5)
1043          */
1044         if (!hub->parent)
1045                 delay = HUB_ROOT_RESET_TIME;
1046
1047         /* Some low speed devices have problems with the quick delay, so */
1048         /*  be a bit pessimistic with those devices. RHbug #23670 */
1049         if (portstatus & USB_PORT_STAT_LOW_SPEED)
1050                 delay = HUB_LONG_RESET_TIME;
1051
1052         down(&usb_address0_sem);
1053
1054         for (i = 0; i < HUB_PROBE_TRIES; i++) {
1055
1056                 /* Allocate a new device struct */
1057                 dev = usb_alloc_dev(hub, hub->bus, port);
1058                 if (!dev) {
1059                         dev_err (&hubstate->intf->dev,
1060                                 "couldn't allocate usb_device\n");
1061                         break;
1062                 }
1063
1064                 dev->state = USB_STATE_POWERED;
1065
1066                 /* Reset the device, and detect its speed */
1067                 if (hub_port_reset(hub, port, dev, delay)) {
1068                         usb_put_dev(dev);
1069                         break;
1070                 }
1071
1072                 /* Find a new address for it */
1073                 usb_choose_address(dev);
1074
1075                 /* Set up TT records, if needed  */
1076                 if (hub->tt) {
1077                         dev->tt = hub->tt;
1078                         dev->ttport = hub->ttport;
1079                 } else if (dev->speed != USB_SPEED_HIGH
1080                                 && hub->speed == USB_SPEED_HIGH) {
1081                         dev->tt = &hubstate->tt;
1082                         dev->ttport = port + 1;
1083                 }
1084
1085                 dev_info (&dev->dev,
1086                         "new %s speed USB device using address %d\n",
1087                         ({ char *speed; switch (dev->speed) {
1088                         case USB_SPEED_LOW:     speed = "low";  break;
1089                         case USB_SPEED_FULL:    speed = "full"; break;
1090                         case USB_SPEED_HIGH:    speed = "high"; break;
1091                         default:                speed = "?";    break;
1092                         }; speed;}),
1093                         dev->devnum);
1094
1095                 /* Run it through the hoops (find a driver, etc) */
1096                 if (usb_new_device(dev) == 0) {
1097                         hub->children[port] = dev;
1098                         goto done;
1099                 }
1100
1101                 /* Free the configuration if there was an error */
1102                 usb_put_dev(dev);
1103
1104                 /* Switch to a long reset time */
1105                 delay = HUB_LONG_RESET_TIME;
1106         }
1107
1108         hub_port_disable(hub, port);
1109 done:
1110         up(&usb_address0_sem);
1111 }
1112
1113 static void hub_events(void)
1114 {
1115         unsigned long flags;
1116         struct list_head *tmp;
1117         struct usb_device *dev;
1118         struct usb_hub *hub;
1119         u16 hubstatus;
1120         u16 hubchange;
1121         u16 portstatus;
1122         u16 portchange;
1123         int i, ret;
1124
1125         /*
1126          *  We restart the list every time to avoid a deadlock with
1127          * deleting hubs downstream from this one. This should be
1128          * safe since we delete the hub from the event list.
1129          * Not the most efficient, but avoids deadlocks.
1130          */
1131         while (1) {
1132                 spin_lock_irqsave(&hub_event_lock, flags);
1133
1134                 if (list_empty(&hub_event_list))
1135                         break;
1136
1137                 /* Grab the next entry from the beginning of the list */
1138                 tmp = hub_event_list.next;
1139
1140                 hub = list_entry(tmp, struct usb_hub, event_list);
1141                 dev = interface_to_usbdev(hub->intf);
1142
1143                 list_del_init(tmp);
1144
1145                 if (unlikely(down_trylock(&hub->khubd_sem)))
1146                         BUG();  /* never blocks, we were on list */
1147
1148                 spin_unlock_irqrestore(&hub_event_lock, flags);
1149
1150                 if (hub->error) {
1151                         dev_dbg (&hub->intf->dev, "resetting for error %d\n",
1152                                 hub->error);
1153
1154                         if (hub_reset(hub)) {
1155                                 dev_dbg (&hub->intf->dev,
1156                                         "can't reset; disconnecting\n");
1157                                 up(&hub->khubd_sem);
1158                                 hub_start_disconnect(dev);
1159                                 continue;
1160                         }
1161
1162                         hub->nerrors = 0;
1163                         hub->error = 0;
1164                 }
1165
1166                 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
1167                         ret = hub_port_status(dev, i, &portstatus, &portchange);
1168                         if (ret < 0) {
1169                                 continue;
1170                         }
1171
1172                         if (portchange & USB_PORT_STAT_C_CONNECTION) {
1173                                 hub_port_connect_change(hub, i, portstatus, portchange);
1174                         } else if (portchange & USB_PORT_STAT_C_ENABLE) {
1175                                 dev_dbg (hubdev (dev),
1176                                         "port %d enable change, status %x\n",
1177                                         i + 1, portstatus);
1178                                 clear_port_feature(dev,
1179                                         i + 1, USB_PORT_FEAT_C_ENABLE);
1180
1181                                 /*
1182                                  * EM interference sometimes causes badly
1183                                  * shielded USB devices to be shutdown by
1184                                  * the hub, this hack enables them again.
1185                                  * Works at least with mouse driver. 
1186                                  */
1187                                 if (!(portstatus & USB_PORT_STAT_ENABLE)
1188                                     && (portstatus & USB_PORT_STAT_CONNECTION)
1189                                     && (dev->children[i])) {
1190                                         dev_err (&hub->intf->dev,
1191                                             "port %i "
1192                                             "disabled by hub (EMI?), "
1193                                             "re-enabling...",
1194                                                 i + 1);
1195                                         hub_port_connect_change(hub,
1196                                                 i, portstatus, portchange);
1197                                 }
1198                         }
1199
1200                         if (portchange & USB_PORT_STAT_C_SUSPEND) {
1201                                 dev_dbg (&hub->intf->dev,
1202                                         "suspend change on port %d\n",
1203                                         i + 1);
1204                                 clear_port_feature(dev,
1205                                         i + 1,  USB_PORT_FEAT_C_SUSPEND);
1206                         }
1207                         
1208                         if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
1209                                 dev_err (&hub->intf->dev,
1210                                         "over-current change on port %d\n",
1211                                         i + 1);
1212                                 clear_port_feature(dev,
1213                                         i + 1, USB_PORT_FEAT_C_OVER_CURRENT);
1214                                 hub_power_on(hub);
1215                         }
1216
1217                         if (portchange & USB_PORT_STAT_C_RESET) {
1218                                 dev_dbg (&hub->intf->dev,
1219                                         "reset change on port %d\n",
1220                                         i + 1);
1221                                 clear_port_feature(dev,
1222                                         i + 1, USB_PORT_FEAT_C_RESET);
1223                         }
1224                 } /* end for i */
1225
1226                 /* deal with hub status changes */
1227                 if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
1228                         dev_err (&hub->intf->dev, "get_hub_status failed\n");
1229                 else {
1230                         if (hubchange & HUB_CHANGE_LOCAL_POWER) {
1231                                 dev_dbg (&hub->intf->dev, "power change\n");
1232                                 clear_hub_feature(dev, C_HUB_LOCAL_POWER);
1233                         }
1234                         if (hubchange & HUB_CHANGE_OVERCURRENT) {
1235                                 dev_dbg (&hub->intf->dev, "overcurrent change\n");
1236                                 wait_ms(500);   /* Cool down */
1237                                 clear_hub_feature(dev, C_HUB_OVER_CURRENT);
1238                                 hub_power_on(hub);
1239                         }
1240                 }
1241                 up(&hub->khubd_sem);
1242         } /* end while (1) */
1243
1244         spin_unlock_irqrestore(&hub_event_lock, flags);
1245 }
1246
1247 static int hub_thread(void *__hub)
1248 {
1249         /*
1250          * This thread doesn't need any user-level access,
1251          * so get rid of all our resources
1252          */
1253
1254         daemonize("khubd");
1255         allow_signal(SIGKILL);
1256
1257         /* Send me a signal to get me die (for debugging) */
1258         do {
1259                 hub_events();
1260                 wait_event_interruptible(khubd_wait, !list_empty(&hub_event_list)); 
1261                 if (current->flags & PF_FREEZE)
1262                         refrigerator(PF_FREEZE);
1263         } while (!signal_pending(current));
1264
1265         pr_debug ("%s: khubd exiting\n", usbcore_name);
1266         complete_and_exit(&khubd_exited, 0);
1267 }
1268
1269 static struct usb_device_id hub_id_table [] = {
1270     { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
1271       .bDeviceClass = USB_CLASS_HUB},
1272     { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1273       .bInterfaceClass = USB_CLASS_HUB},
1274     { }                                         /* Terminating entry */
1275 };
1276
1277 MODULE_DEVICE_TABLE (usb, hub_id_table);
1278
1279 static struct usb_driver hub_driver = {
1280         .owner =        THIS_MODULE,
1281         .name =         "hub",
1282         .probe =        hub_probe,
1283         .disconnect =   hub_disconnect,
1284         .ioctl =        hub_ioctl,
1285         .id_table =     hub_id_table,
1286 };
1287
1288 /*
1289  * This should be a separate module.
1290  */
1291 int usb_hub_init(void)
1292 {
1293         pid_t pid;
1294
1295         if (usb_register(&hub_driver) < 0) {
1296                 printk(KERN_ERR "%s: can't register hub driver\n",
1297                         usbcore_name);
1298                 return -1;
1299         }
1300
1301         pid = kernel_thread(hub_thread, NULL, CLONE_KERNEL);
1302         if (pid >= 0) {
1303                 khubd_pid = pid;
1304
1305                 return 0;
1306         }
1307
1308         /* Fall through if kernel_thread failed */
1309         usb_deregister(&hub_driver);
1310         printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
1311
1312         return -1;
1313 }
1314
1315 void usb_hub_cleanup(void)
1316 {
1317         int ret;
1318
1319         /* Kill the thread */
1320         ret = kill_proc(khubd_pid, SIGKILL, 1);
1321
1322         wait_for_completion(&khubd_exited);
1323
1324         /*
1325          * Hub resources are freed for us by usb_deregister. It calls
1326          * usb_driver_purge on every device which in turn calls that
1327          * devices disconnect function if it is using this driver.
1328          * The hub_disconnect function takes care of releasing the
1329          * individual hub resources. -greg
1330          */
1331         usb_deregister(&hub_driver);
1332 } /* usb_hub_cleanup() */
1333
1334 /*
1335  * WARNING - If a driver calls usb_reset_device, you should simulate a
1336  * disconnect() and probe() for other interfaces you doesn't claim. This
1337  * is left up to the driver writer right now. This insures other drivers
1338  * have a chance to re-setup their interface.
1339  *
1340  * Take a look at proc_resetdevice in devio.c for some sample code to
1341  * do this.
1342  * Use this only from within your probe function, otherwise use
1343  * usb_reset_device() below, which ensure proper locking
1344  */
1345 int usb_physical_reset_device(struct usb_device *dev)
1346 {
1347         struct usb_device *parent = dev->parent;
1348         struct usb_device_descriptor *descriptor;
1349         int i, ret, port = -1;
1350
1351         if (!parent) {
1352                 err("attempting to reset root hub!");
1353                 return -EINVAL;
1354         }
1355
1356         for (i = 0; i < parent->maxchild; i++)
1357                 if (parent->children[i] == dev) {
1358                         port = i;
1359                         break;
1360                 }
1361
1362         if (port < 0)
1363                 return -ENOENT;
1364
1365         descriptor = kmalloc(sizeof *descriptor, GFP_NOIO);
1366         if (!descriptor) {
1367                 return -ENOMEM;
1368         }
1369
1370         down(&usb_address0_sem);
1371
1372         /* Send a reset to the device */
1373         if (hub_port_reset(parent, port, dev, HUB_SHORT_RESET_TIME)) {
1374                 hub_port_disable(parent, port);
1375                 up(&usb_address0_sem);
1376                 kfree(descriptor);
1377                 return(-ENODEV);
1378         }
1379
1380         /* Reprogram the Address */
1381         ret = usb_set_address(dev);
1382         if (ret < 0) {
1383                 err("USB device not accepting new address (error=%d)", ret);
1384                 hub_port_disable(parent, port);
1385                 up(&usb_address0_sem);
1386                 kfree(descriptor);
1387                 return ret;
1388         }
1389
1390         /* Let the SET_ADDRESS settle */
1391         wait_ms(10);
1392
1393         up(&usb_address0_sem);
1394
1395         /*
1396          * Now we fetch the configuration descriptors for the device and
1397          * see if anything has changed. If it has, we dump the current
1398          * parsed descriptors and reparse from scratch. Then we leave
1399          * the device alone for the caller to finish setting up.
1400          *
1401          * If nothing changed, we reprogram the configuration and then
1402          * the alternate settings.
1403          */
1404
1405         ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, descriptor,
1406                         sizeof(*descriptor));
1407         if (ret < 0) {
1408                 kfree(descriptor);
1409                 return ret;
1410         }
1411
1412         le16_to_cpus(&descriptor->bcdUSB);
1413         le16_to_cpus(&descriptor->idVendor);
1414         le16_to_cpus(&descriptor->idProduct);
1415         le16_to_cpus(&descriptor->bcdDevice);
1416
1417         if (memcmp(&dev->descriptor, descriptor, sizeof(*descriptor))) {
1418                 kfree(descriptor);
1419                 usb_destroy_configuration(dev);
1420
1421                 /* FIXME Linux doesn't yet handle these "device morphed"
1422                  * paths.  DFU variants need this to work ... and they
1423                  * include the "config descriptors changed" case this
1424                  * doesn't yet detect!
1425                  */
1426                 dev->state = USB_STATE_NOTATTACHED;
1427                 dev_err(&dev->dev, "device morphed (DFU?), nyet supported\n");
1428
1429                 return -ENODEV;
1430         }
1431
1432         kfree(descriptor);
1433
1434         ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1435                         USB_REQ_SET_CONFIGURATION, 0,
1436                         dev->actconfig->desc.bConfigurationValue, 0,
1437                         NULL, 0, HZ * USB_CTRL_SET_TIMEOUT);
1438         if (ret < 0) {
1439                 err("failed to set dev %s active configuration (error=%d)",
1440                         dev->devpath, ret);
1441                 return ret;
1442         }
1443         dev->state = USB_STATE_CONFIGURED;
1444
1445         for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1446                 struct usb_interface *intf = dev->actconfig->interface[i];
1447                 struct usb_interface_descriptor *desc;
1448
1449                 desc = &intf->cur_altsetting->desc;
1450                 ret = usb_set_interface(dev, desc->bInterfaceNumber,
1451                         desc->bAlternateSetting);
1452                 if (ret < 0) {
1453                         err("failed to set active alternate setting "
1454                                 "for dev %s interface %d (error=%d)",
1455                                 dev->devpath, desc->bInterfaceNumber, ret);
1456                         return ret;
1457                 }
1458         }
1459
1460         return 0;
1461 }
1462
1463 int usb_reset_device(struct usb_device *udev)
1464 {
1465         struct device *gdev = &udev->dev;
1466         int r;
1467         
1468         down_read(&gdev->bus->subsys.rwsem);
1469         r = usb_physical_reset_device(udev);
1470         up_read(&gdev->bus->subsys.rwsem);
1471
1472         return r;
1473 }
1474
1475