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