usb: replace wait_ms() with mdelay()
[profile/mobile/platform/kernel/u-boot-tm1.git] / common / usb.c
1 /*
2  *
3  * Most of this source has been derived from the Linux USB
4  * project:
5  * (C) Copyright Linus Torvalds 1999
6  * (C) Copyright Johannes Erdfelt 1999-2001
7  * (C) Copyright Andreas Gal 1999
8  * (C) Copyright Gregory P. Smith 1999
9  * (C) Copyright Deti Fliegl 1999 (new USB architecture)
10  * (C) Copyright Randy Dunlap 2000
11  * (C) Copyright David Brownell 2000 (kernel hotplug, usb_device_id)
12  * (C) Copyright Yggdrasil Computing, Inc. 2000
13  *     (usb_device_id matching changes by Adam J. Richter)
14  *
15  * Adapted for U-Boot:
16  * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
17  *
18  * See file CREDITS for list of people who contributed to this
19  * project.
20  *
21  * This program is free software; you can redistribute it and/or
22  * modify it under the terms of the GNU General Public License as
23  * published by the Free Software Foundation; either version 2 of
24  * the License, or (at your option) any later version.
25  *
26  * This program is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  * GNU General Public License for more details.
30  *
31  * You should have received a copy of the GNU General Public License
32  * along with this program; if not, write to the Free Software
33  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34  * MA 02111-1307 USA
35  *
36  */
37
38 /*
39  * How it works:
40  *
41  * Since this is a bootloader, the devices will not be automatic
42  * (re)configured on hotplug, but after a restart of the USB the
43  * device should work.
44  *
45  * For each transfer (except "Interrupt") we wait for completion.
46  */
47 #include <common.h>
48 #include <command.h>
49 #include <asm/processor.h>
50 #include <linux/ctype.h>
51 #include <asm/byteorder.h>
52
53 #include <usb.h>
54 #ifdef CONFIG_4xx
55 #include <asm/4xx_pci.h>
56 #endif
57
58 #undef USB_DEBUG
59
60 #ifdef  USB_DEBUG
61 #define USB_PRINTF(fmt, args...)        printf(fmt , ##args)
62 #else
63 #define USB_PRINTF(fmt, args...)
64 #endif
65
66 #define USB_BUFSIZ      512
67
68 static struct usb_device usb_dev[USB_MAX_DEVICE];
69 static int dev_index;
70 static int running;
71 static int asynch_allowed;
72 static struct devrequest setup_packet;
73
74 char usb_started; /* flag for the started/stopped USB status */
75
76 /**********************************************************************
77  * some forward declerations...
78  */
79 void usb_scan_devices(void);
80
81 int usb_hub_probe(struct usb_device *dev, int ifnum);
82 void usb_hub_reset(void);
83 static int hub_port_reset(struct usb_device *dev, int port,
84                           unsigned short *portstat);
85
86 /***************************************************************************
87  * Init USB Device
88  */
89
90 int usb_init(void)
91 {
92         int result;
93
94         running = 0;
95         dev_index = 0;
96         asynch_allowed = 1;
97         usb_hub_reset();
98         /* init low_level USB */
99         printf("USB:   ");
100         result = usb_lowlevel_init();
101         /* if lowlevel init is OK, scan the bus for devices
102          * i.e. search HUBs and configure them */
103         if (result == 0) {
104                 printf("scanning bus for devices... ");
105                 running = 1;
106                 usb_scan_devices();
107                 usb_started = 1;
108                 return 0;
109         } else {
110                 printf("Error, couldn't init Lowlevel part\n");
111                 usb_started = 0;
112                 return -1;
113         }
114 }
115
116 /******************************************************************************
117  * Stop USB this stops the LowLevel Part and deregisters USB devices.
118  */
119 int usb_stop(void)
120 {
121         int res = 0;
122
123         if (usb_started) {
124                 asynch_allowed = 1;
125                 usb_started = 0;
126                 usb_hub_reset();
127                 res = usb_lowlevel_stop();
128         }
129         return res;
130 }
131
132 /*
133  * disables the asynch behaviour of the control message. This is used for data
134  * transfers that uses the exclusiv access to the control and bulk messages.
135  */
136 void usb_disable_asynch(int disable)
137 {
138         asynch_allowed = !disable;
139 }
140
141
142 /*-------------------------------------------------------------------
143  * Message wrappers.
144  *
145  */
146
147 /*
148  * submits an Interrupt Message
149  */
150 int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe,
151                         void *buffer, int transfer_len, int interval)
152 {
153         return submit_int_msg(dev, pipe, buffer, transfer_len, interval);
154 }
155
156 /*
157  * submits a control message and waits for comletion (at least timeout * 1ms)
158  * If timeout is 0, we don't wait for completion (used as example to set and
159  * clear keyboards LEDs). For data transfers, (storage transfers) we don't
160  * allow control messages with 0 timeout, by previousely resetting the flag
161  * asynch_allowed (usb_disable_asynch(1)).
162  * returns the transfered length if OK or -1 if error. The transfered length
163  * and the current status are stored in the dev->act_len and dev->status.
164  */
165 int usb_control_msg(struct usb_device *dev, unsigned int pipe,
166                         unsigned char request, unsigned char requesttype,
167                         unsigned short value, unsigned short index,
168                         void *data, unsigned short size, int timeout)
169 {
170         if ((timeout == 0) && (!asynch_allowed)) {
171                 /* request for a asynch control pipe is not allowed */
172                 return -1;
173         }
174
175         /* set setup command */
176         setup_packet.requesttype = requesttype;
177         setup_packet.request = request;
178         setup_packet.value = cpu_to_le16(value);
179         setup_packet.index = cpu_to_le16(index);
180         setup_packet.length = cpu_to_le16(size);
181         USB_PRINTF("usb_control_msg: request: 0x%X, requesttype: 0x%X, " \
182                    "value 0x%X index 0x%X length 0x%X\n",
183                    request, requesttype, value, index, size);
184         dev->status = USB_ST_NOT_PROC; /*not yet processed */
185
186         submit_control_msg(dev, pipe, data, size, &setup_packet);
187         if (timeout == 0)
188                 return (int)size;
189
190         /*
191          * Wait for status to update until timeout expires, USB driver
192          * interrupt handler may set the status when the USB operation has
193          * been completed.
194          */
195         while (timeout--) {
196                 if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC))
197                         break;
198                 mdelay(1);
199         }
200         if (dev->status)
201                 return -1;
202
203         return dev->act_len;
204
205 }
206
207 /*-------------------------------------------------------------------
208  * submits bulk message, and waits for completion. returns 0 if Ok or
209  * -1 if Error.
210  * synchronous behavior
211  */
212 int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
213                         void *data, int len, int *actual_length, int timeout)
214 {
215         if (len < 0)
216                 return -1;
217         dev->status = USB_ST_NOT_PROC; /*not yet processed */
218         submit_bulk_msg(dev, pipe, data, len);
219         while (timeout--) {
220                 if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC))
221                         break;
222                 mdelay(1);
223         }
224         *actual_length = dev->act_len;
225         if (dev->status == 0)
226                 return 0;
227         else
228                 return -1;
229 }
230
231
232 /*-------------------------------------------------------------------
233  * Max Packet stuff
234  */
235
236 /*
237  * returns the max packet size, depending on the pipe direction and
238  * the configurations values
239  */
240 int usb_maxpacket(struct usb_device *dev, unsigned long pipe)
241 {
242         /* direction is out -> use emaxpacket out */
243         if ((pipe & USB_DIR_IN) == 0)
244                 return dev->epmaxpacketout[((pipe>>15) & 0xf)];
245         else
246                 return dev->epmaxpacketin[((pipe>>15) & 0xf)];
247 }
248
249 /* The routine usb_set_maxpacket_ep() is extracted from the loop of routine
250  * usb_set_maxpacket(), because the optimizer of GCC 4.x chokes on this routine
251  * when it is inlined in 1 single routine. What happens is that the register r3
252  * is used as loop-count 'i', but gets overwritten later on.
253  * This is clearly a compiler bug, but it is easier to workaround it here than
254  * to update the compiler (Occurs with at least several GCC 4.{1,2},x
255  * CodeSourcery compilers like e.g. 2007q3, 2008q1, 2008q3 lite editions on ARM)
256  */
257 static void  __attribute__((noinline))
258 usb_set_maxpacket_ep(struct usb_device *dev, struct usb_endpoint_descriptor *ep)
259 {
260         int b;
261
262         b = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
263
264         if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
265                                                 USB_ENDPOINT_XFER_CONTROL) {
266                 /* Control => bidirectional */
267                 dev->epmaxpacketout[b] = ep->wMaxPacketSize;
268                 dev->epmaxpacketin[b] = ep->wMaxPacketSize;
269                 USB_PRINTF("##Control EP epmaxpacketout/in[%d] = %d\n",
270                            b, dev->epmaxpacketin[b]);
271         } else {
272                 if ((ep->bEndpointAddress & 0x80) == 0) {
273                         /* OUT Endpoint */
274                         if (ep->wMaxPacketSize > dev->epmaxpacketout[b]) {
275                                 dev->epmaxpacketout[b] = ep->wMaxPacketSize;
276                                 USB_PRINTF("##EP epmaxpacketout[%d] = %d\n",
277                                            b, dev->epmaxpacketout[b]);
278                         }
279                 } else {
280                         /* IN Endpoint */
281                         if (ep->wMaxPacketSize > dev->epmaxpacketin[b]) {
282                                 dev->epmaxpacketin[b] = ep->wMaxPacketSize;
283                                 USB_PRINTF("##EP epmaxpacketin[%d] = %d\n",
284                                            b, dev->epmaxpacketin[b]);
285                         }
286                 } /* if out */
287         } /* if control */
288 }
289
290 /*
291  * set the max packed value of all endpoints in the given configuration
292  */
293 int usb_set_maxpacket(struct usb_device *dev)
294 {
295         int i, ii;
296
297         for (i = 0; i < dev->config.desc.bNumInterfaces; i++)
298                 for (ii = 0; ii < dev->config.if_desc[i].desc.bNumEndpoints; ii++)
299                         usb_set_maxpacket_ep(dev,
300                                           &dev->config.if_desc[i].ep_desc[ii]);
301
302         return 0;
303 }
304
305 /*******************************************************************************
306  * Parse the config, located in buffer, and fills the dev->config structure.
307  * Note that all little/big endian swapping are done automatically.
308  */
309 int usb_parse_config(struct usb_device *dev, unsigned char *buffer, int cfgno)
310 {
311         struct usb_descriptor_header *head;
312         int index, ifno, epno, curr_if_num;
313         int i;
314         unsigned char *ch;
315
316         ifno = -1;
317         epno = -1;
318         curr_if_num = -1;
319
320         dev->configno = cfgno;
321         head = (struct usb_descriptor_header *) &buffer[0];
322         if (head->bDescriptorType != USB_DT_CONFIG) {
323                 printf(" ERROR: NOT USB_CONFIG_DESC %x\n",
324                         head->bDescriptorType);
325                 return -1;
326         }
327         memcpy(&dev->config, buffer, buffer[0]);
328         le16_to_cpus(&(dev->config.desc.wTotalLength));
329         dev->config.no_of_if = 0;
330
331         index = dev->config.desc.bLength;
332         /* Ok the first entry must be a configuration entry,
333          * now process the others */
334         head = (struct usb_descriptor_header *) &buffer[index];
335         while (index + 1 < dev->config.desc.wTotalLength) {
336                 switch (head->bDescriptorType) {
337                 case USB_DT_INTERFACE:
338                         if (((struct usb_interface_descriptor *) \
339                              &buffer[index])->bInterfaceNumber != curr_if_num) {
340                                 /* this is a new interface, copy new desc */
341                                 ifno = dev->config.no_of_if;
342                                 dev->config.no_of_if++;
343                                 memcpy(&dev->config.if_desc[ifno],
344                                         &buffer[index], buffer[index]);
345                                 dev->config.if_desc[ifno].no_of_ep = 0;
346                                 dev->config.if_desc[ifno].num_altsetting = 1;
347                                 curr_if_num =
348                                      dev->config.if_desc[ifno].desc.bInterfaceNumber;
349                         } else {
350                                 /* found alternate setting for the interface */
351                                 dev->config.if_desc[ifno].num_altsetting++;
352                         }
353                         break;
354                 case USB_DT_ENDPOINT:
355                         epno = dev->config.if_desc[ifno].no_of_ep;
356                         /* found an endpoint */
357                         dev->config.if_desc[ifno].no_of_ep++;
358                         memcpy(&dev->config.if_desc[ifno].ep_desc[epno],
359                                 &buffer[index], buffer[index]);
360                         le16_to_cpus(&(dev->config.if_desc[ifno].ep_desc[epno].\
361                                                                wMaxPacketSize));
362                         USB_PRINTF("if %d, ep %d\n", ifno, epno);
363                         break;
364                 default:
365                         if (head->bLength == 0)
366                                 return 1;
367
368                         USB_PRINTF("unknown Description Type : %x\n",
369                                    head->bDescriptorType);
370
371                         {
372                                 ch = (unsigned char *)head;
373                                 for (i = 0; i < head->bLength; i++)
374                                         USB_PRINTF("%02X ", *ch++);
375                                 USB_PRINTF("\n\n\n");
376                         }
377                         break;
378                 }
379                 index += head->bLength;
380                 head = (struct usb_descriptor_header *)&buffer[index];
381         }
382         return 1;
383 }
384
385 /***********************************************************************
386  * Clears an endpoint
387  * endp: endpoint number in bits 0-3;
388  * direction flag in bit 7 (1 = IN, 0 = OUT)
389  */
390 int usb_clear_halt(struct usb_device *dev, int pipe)
391 {
392         int result;
393         int endp = usb_pipeendpoint(pipe)|(usb_pipein(pipe)<<7);
394
395         result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
396                                  USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0,
397                                  endp, NULL, 0, USB_CNTL_TIMEOUT * 3);
398
399         /* don't clear if failed */
400         if (result < 0)
401                 return result;
402
403         /*
404          * NOTE: we do not get status and verify reset was successful
405          * as some devices are reported to lock up upon this check..
406          */
407
408         usb_endpoint_running(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
409
410         /* toggle is reset on clear */
411         usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
412         return 0;
413 }
414
415
416 /**********************************************************************
417  * get_descriptor type
418  */
419 int usb_get_descriptor(struct usb_device *dev, unsigned char type,
420                         unsigned char index, void *buf, int size)
421 {
422         int res;
423         res = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
424                         USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
425                         (type << 8) + index, 0,
426                         buf, size, USB_CNTL_TIMEOUT);
427         return res;
428 }
429
430 /**********************************************************************
431  * gets configuration cfgno and store it in the buffer
432  */
433 int usb_get_configuration_no(struct usb_device *dev,
434                              unsigned char *buffer, int cfgno)
435 {
436         int result;
437         unsigned int tmp;
438         struct usb_configuration_descriptor *config;
439
440         config = (struct usb_configuration_descriptor *)&buffer[0];
441         result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, 9);
442         if (result < 9) {
443                 if (result < 0)
444                         printf("unable to get descriptor, error %lX\n",
445                                 dev->status);
446                 else
447                         printf("config descriptor too short " \
448                                 "(expected %i, got %i)\n", 9, result);
449                 return -1;
450         }
451         tmp = le16_to_cpu(config->wTotalLength);
452
453         if (tmp > USB_BUFSIZ) {
454                 USB_PRINTF("usb_get_configuration_no: failed to get " \
455                            "descriptor - too long: %d\n", tmp);
456                 return -1;
457         }
458
459         result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, tmp);
460         USB_PRINTF("get_conf_no %d Result %d, wLength %d\n",
461                    cfgno, result, tmp);
462         return result;
463 }
464
465 /********************************************************************
466  * set address of a device to the value in dev->devnum.
467  * This can only be done by addressing the device via the default address (0)
468  */
469 int usb_set_address(struct usb_device *dev)
470 {
471         int res;
472
473         USB_PRINTF("set address %d\n", dev->devnum);
474         res = usb_control_msg(dev, usb_snddefctrl(dev),
475                                 USB_REQ_SET_ADDRESS, 0,
476                                 (dev->devnum), 0,
477                                 NULL, 0, USB_CNTL_TIMEOUT);
478         return res;
479 }
480
481 /********************************************************************
482  * set interface number to interface
483  */
484 int usb_set_interface(struct usb_device *dev, int interface, int alternate)
485 {
486         struct usb_interface *if_face = NULL;
487         int ret, i;
488
489         for (i = 0; i < dev->config.desc.bNumInterfaces; i++) {
490                 if (dev->config.if_desc[i].desc.bInterfaceNumber == interface) {
491                         if_face = &dev->config.if_desc[i];
492                         break;
493                 }
494         }
495         if (!if_face) {
496                 printf("selecting invalid interface %d", interface);
497                 return -1;
498         }
499         /*
500          * We should return now for devices with only one alternate setting.
501          * According to 9.4.10 of the Universal Serial Bus Specification
502          * Revision 2.0 such devices can return with a STALL. This results in
503          * some USB sticks timeouting during initialization and then being
504          * unusable in U-Boot.
505          */
506         if (if_face->num_altsetting == 1)
507                 return 0;
508
509         ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
510                                 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
511                                 alternate, interface, NULL, 0,
512                                 USB_CNTL_TIMEOUT * 5);
513         if (ret < 0)
514                 return ret;
515
516         return 0;
517 }
518
519 /********************************************************************
520  * set configuration number to configuration
521  */
522 int usb_set_configuration(struct usb_device *dev, int configuration)
523 {
524         int res;
525         USB_PRINTF("set configuration %d\n", configuration);
526         /* set setup command */
527         res = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
528                                 USB_REQ_SET_CONFIGURATION, 0,
529                                 configuration, 0,
530                                 NULL, 0, USB_CNTL_TIMEOUT);
531         if (res == 0) {
532                 dev->toggle[0] = 0;
533                 dev->toggle[1] = 0;
534                 return 0;
535         } else
536                 return -1;
537 }
538
539 /********************************************************************
540  * set protocol to protocol
541  */
542 int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol)
543 {
544         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
545                 USB_REQ_SET_PROTOCOL, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
546                 protocol, ifnum, NULL, 0, USB_CNTL_TIMEOUT);
547 }
548
549 /********************************************************************
550  * set idle
551  */
552 int usb_set_idle(struct usb_device *dev, int ifnum, int duration, int report_id)
553 {
554         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
555                 USB_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
556                 (duration << 8) | report_id, ifnum, NULL, 0, USB_CNTL_TIMEOUT);
557 }
558
559 /********************************************************************
560  * get report
561  */
562 int usb_get_report(struct usb_device *dev, int ifnum, unsigned char type,
563                    unsigned char id, void *buf, int size)
564 {
565         return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
566                         USB_REQ_GET_REPORT,
567                         USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
568                         (type << 8) + id, ifnum, buf, size, USB_CNTL_TIMEOUT);
569 }
570
571 /********************************************************************
572  * get class descriptor
573  */
574 int usb_get_class_descriptor(struct usb_device *dev, int ifnum,
575                 unsigned char type, unsigned char id, void *buf, int size)
576 {
577         return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
578                 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
579                 (type << 8) + id, ifnum, buf, size, USB_CNTL_TIMEOUT);
580 }
581
582 /********************************************************************
583  * get string index in buffer
584  */
585 int usb_get_string(struct usb_device *dev, unsigned short langid,
586                    unsigned char index, void *buf, int size)
587 {
588         int i;
589         int result;
590
591         for (i = 0; i < 3; ++i) {
592                 /* some devices are flaky */
593                 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
594                         USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
595                         (USB_DT_STRING << 8) + index, langid, buf, size,
596                         USB_CNTL_TIMEOUT);
597
598                 if (result > 0)
599                         break;
600         }
601
602         return result;
603 }
604
605
606 static void usb_try_string_workarounds(unsigned char *buf, int *length)
607 {
608         int newlength, oldlength = *length;
609
610         for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
611                 if (!isprint(buf[newlength]) || buf[newlength + 1])
612                         break;
613
614         if (newlength > 2) {
615                 buf[0] = newlength;
616                 *length = newlength;
617         }
618 }
619
620
621 static int usb_string_sub(struct usb_device *dev, unsigned int langid,
622                 unsigned int index, unsigned char *buf)
623 {
624         int rc;
625
626         /* Try to read the string descriptor by asking for the maximum
627          * possible number of bytes */
628         rc = usb_get_string(dev, langid, index, buf, 255);
629
630         /* If that failed try to read the descriptor length, then
631          * ask for just that many bytes */
632         if (rc < 2) {
633                 rc = usb_get_string(dev, langid, index, buf, 2);
634                 if (rc == 2)
635                         rc = usb_get_string(dev, langid, index, buf, buf[0]);
636         }
637
638         if (rc >= 2) {
639                 if (!buf[0] && !buf[1])
640                         usb_try_string_workarounds(buf, &rc);
641
642                 /* There might be extra junk at the end of the descriptor */
643                 if (buf[0] < rc)
644                         rc = buf[0];
645
646                 rc = rc - (rc & 1); /* force a multiple of two */
647         }
648
649         if (rc < 2)
650                 rc = -1;
651
652         return rc;
653 }
654
655
656 /********************************************************************
657  * usb_string:
658  * Get string index and translate it to ascii.
659  * returns string length (> 0) or error (< 0)
660  */
661 int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
662 {
663         unsigned char mybuf[USB_BUFSIZ];
664         unsigned char *tbuf;
665         int err;
666         unsigned int u, idx;
667
668         if (size <= 0 || !buf || !index)
669                 return -1;
670         buf[0] = 0;
671         tbuf = &mybuf[0];
672
673         /* get langid for strings if it's not yet known */
674         if (!dev->have_langid) {
675                 err = usb_string_sub(dev, 0, 0, tbuf);
676                 if (err < 0) {
677                         USB_PRINTF("error getting string descriptor 0 " \
678                                    "(error=%lx)\n", dev->status);
679                         return -1;
680                 } else if (tbuf[0] < 4) {
681                         USB_PRINTF("string descriptor 0 too short\n");
682                         return -1;
683                 } else {
684                         dev->have_langid = -1;
685                         dev->string_langid = tbuf[2] | (tbuf[3] << 8);
686                                 /* always use the first langid listed */
687                         USB_PRINTF("USB device number %d default " \
688                                    "language ID 0x%x\n",
689                                    dev->devnum, dev->string_langid);
690                 }
691         }
692
693         err = usb_string_sub(dev, dev->string_langid, index, tbuf);
694         if (err < 0)
695                 return err;
696
697         size--;         /* leave room for trailing NULL char in output buffer */
698         for (idx = 0, u = 2; u < err; u += 2) {
699                 if (idx >= size)
700                         break;
701                 if (tbuf[u+1])                  /* high byte */
702                         buf[idx++] = '?';  /* non-ASCII character */
703                 else
704                         buf[idx++] = tbuf[u];
705         }
706         buf[idx] = 0;
707         err = idx;
708         return err;
709 }
710
711
712 /********************************************************************
713  * USB device handling:
714  * the USB device are static allocated [USB_MAX_DEVICE].
715  */
716
717
718 /* returns a pointer to the device with the index [index].
719  * if the device is not assigned (dev->devnum==-1) returns NULL
720  */
721 struct usb_device *usb_get_dev_index(int index)
722 {
723         if (usb_dev[index].devnum == -1)
724                 return NULL;
725         else
726                 return &usb_dev[index];
727 }
728
729
730 /* returns a pointer of a new device structure or NULL, if
731  * no device struct is available
732  */
733 struct usb_device *usb_alloc_new_device(void)
734 {
735         int i;
736         USB_PRINTF("New Device %d\n", dev_index);
737         if (dev_index == USB_MAX_DEVICE) {
738                 printf("ERROR, too many USB Devices, max=%d\n", USB_MAX_DEVICE);
739                 return NULL;
740         }
741         /* default Address is 0, real addresses start with 1 */
742         usb_dev[dev_index].devnum = dev_index + 1;
743         usb_dev[dev_index].maxchild = 0;
744         for (i = 0; i < USB_MAXCHILDREN; i++)
745                 usb_dev[dev_index].children[i] = NULL;
746         usb_dev[dev_index].parent = NULL;
747         dev_index++;
748         return &usb_dev[dev_index - 1];
749 }
750
751
752 /*
753  * By the time we get here, the device has gotten a new device ID
754  * and is in the default state. We need to identify the thing and
755  * get the ball rolling..
756  *
757  * Returns 0 for success, != 0 for error.
758  */
759 int usb_new_device(struct usb_device *dev)
760 {
761         int addr, err;
762         int tmp;
763         unsigned char tmpbuf[USB_BUFSIZ];
764
765         /* We still haven't set the Address yet */
766         addr = dev->devnum;
767         dev->devnum = 0;
768
769 #ifdef CONFIG_LEGACY_USB_INIT_SEQ
770         /* this is the old and known way of initializing devices, it is
771          * different than what Windows and Linux are doing. Windows and Linux
772          * both retrieve 64 bytes while reading the device descriptor
773          * Several USB stick devices report ERR: CTL_TIMEOUT, caused by an
774          * invalid header while reading 8 bytes as device descriptor. */
775         dev->descriptor.bMaxPacketSize0 = 8;        /* Start off at 8 bytes  */
776         dev->maxpacketsize = PACKET_SIZE_8;
777         dev->epmaxpacketin[0] = 8;
778         dev->epmaxpacketout[0] = 8;
779
780         err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, &dev->descriptor, 8);
781         if (err < 8) {
782                 printf("\n      USB device not responding, " \
783                        "giving up (status=%lX)\n", dev->status);
784                 return 1;
785         }
786 #else
787         /* This is a Windows scheme of initialization sequence, with double
788          * reset of the device (Linux uses the same sequence)
789          * Some equipment is said to work only with such init sequence; this
790          * patch is based on the work by Alan Stern:
791          * http://sourceforge.net/mailarchive/forum.php?
792          * thread_id=5729457&forum_id=5398
793          */
794         struct usb_device_descriptor *desc;
795         int port = -1;
796         struct usb_device *parent = dev->parent;
797         unsigned short portstatus;
798
799         /* send 64-byte GET-DEVICE-DESCRIPTOR request.  Since the descriptor is
800          * only 18 bytes long, this will terminate with a short packet.  But if
801          * the maxpacket size is 8 or 16 the device may be waiting to transmit
802          * some more, or keeps on retransmitting the 8 byte header. */
803
804         desc = (struct usb_device_descriptor *)tmpbuf;
805         dev->descriptor.bMaxPacketSize0 = 64;       /* Start off at 64 bytes  */
806         /* Default to 64 byte max packet size */
807         dev->maxpacketsize = PACKET_SIZE_64;
808         dev->epmaxpacketin[0] = 64;
809         dev->epmaxpacketout[0] = 64;
810
811         err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, 64);
812         if (err < 0) {
813                 USB_PRINTF("usb_new_device: usb_get_descriptor() failed\n");
814                 return 1;
815         }
816
817         dev->descriptor.bMaxPacketSize0 = desc->bMaxPacketSize0;
818
819         /* find the port number we're at */
820         if (parent) {
821                 int j;
822
823                 for (j = 0; j < parent->maxchild; j++) {
824                         if (parent->children[j] == dev) {
825                                 port = j;
826                                 break;
827                         }
828                 }
829                 if (port < 0) {
830                         printf("usb_new_device:cannot locate device's port.\n");
831                         return 1;
832                 }
833
834                 /* reset the port for the second time */
835                 err = hub_port_reset(dev->parent, port, &portstatus);
836                 if (err < 0) {
837                         printf("\n     Couldn't reset port %i\n", port);
838                         return 1;
839                 }
840         }
841 #endif
842
843         dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0;
844         dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
845         switch (dev->descriptor.bMaxPacketSize0) {
846         case 8:
847                 dev->maxpacketsize  = PACKET_SIZE_8;
848                 break;
849         case 16:
850                 dev->maxpacketsize = PACKET_SIZE_16;
851                 break;
852         case 32:
853                 dev->maxpacketsize = PACKET_SIZE_32;
854                 break;
855         case 64:
856                 dev->maxpacketsize = PACKET_SIZE_64;
857                 break;
858         }
859         dev->devnum = addr;
860
861         err = usb_set_address(dev); /* set address */
862
863         if (err < 0) {
864                 printf("\n      USB device not accepting new address " \
865                         "(error=%lX)\n", dev->status);
866                 return 1;
867         }
868
869         mdelay(10);     /* Let the SET_ADDRESS settle */
870
871         tmp = sizeof(dev->descriptor);
872
873         err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
874                                  &dev->descriptor, sizeof(dev->descriptor));
875         if (err < tmp) {
876                 if (err < 0)
877                         printf("unable to get device descriptor (error=%d)\n",
878                                err);
879                 else
880                         printf("USB device descriptor short read " \
881                                 "(expected %i, got %i)\n", tmp, err);
882                 return 1;
883         }
884         /* correct le values */
885         le16_to_cpus(&dev->descriptor.bcdUSB);
886         le16_to_cpus(&dev->descriptor.idVendor);
887         le16_to_cpus(&dev->descriptor.idProduct);
888         le16_to_cpus(&dev->descriptor.bcdDevice);
889         /* only support for one config for now */
890         usb_get_configuration_no(dev, &tmpbuf[0], 0);
891         usb_parse_config(dev, &tmpbuf[0], 0);
892         usb_set_maxpacket(dev);
893         /* we set the default configuration here */
894         if (usb_set_configuration(dev, dev->config.desc.bConfigurationValue)) {
895                 printf("failed to set default configuration " \
896                         "len %d, status %lX\n", dev->act_len, dev->status);
897                 return -1;
898         }
899         USB_PRINTF("new device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
900                    dev->descriptor.iManufacturer, dev->descriptor.iProduct,
901                    dev->descriptor.iSerialNumber);
902         memset(dev->mf, 0, sizeof(dev->mf));
903         memset(dev->prod, 0, sizeof(dev->prod));
904         memset(dev->serial, 0, sizeof(dev->serial));
905         if (dev->descriptor.iManufacturer)
906                 usb_string(dev, dev->descriptor.iManufacturer,
907                            dev->mf, sizeof(dev->mf));
908         if (dev->descriptor.iProduct)
909                 usb_string(dev, dev->descriptor.iProduct,
910                            dev->prod, sizeof(dev->prod));
911         if (dev->descriptor.iSerialNumber)
912                 usb_string(dev, dev->descriptor.iSerialNumber,
913                            dev->serial, sizeof(dev->serial));
914         USB_PRINTF("Manufacturer %s\n", dev->mf);
915         USB_PRINTF("Product      %s\n", dev->prod);
916         USB_PRINTF("SerialNumber %s\n", dev->serial);
917         /* now prode if the device is a hub */
918         usb_hub_probe(dev, 0);
919         return 0;
920 }
921
922 /* build device Tree  */
923 void usb_scan_devices(void)
924 {
925         int i;
926         struct usb_device *dev;
927
928         /* first make all devices unknown */
929         for (i = 0; i < USB_MAX_DEVICE; i++) {
930                 memset(&usb_dev[i], 0, sizeof(struct usb_device));
931                 usb_dev[i].devnum = -1;
932         }
933         dev_index = 0;
934         /* device 0 is always present (root hub, so let it analyze) */
935         dev = usb_alloc_new_device();
936         if (usb_new_device(dev))
937                 printf("No USB Device found\n");
938         else
939                 printf("%d USB Device(s) found\n", dev_index);
940         /* insert "driver" if possible */
941 #ifdef CONFIG_USB_KEYBOARD
942         drv_usb_kbd_init();
943         USB_PRINTF("scan end\n");
944 #endif
945 }
946
947
948 /****************************************************************************
949  * HUB "Driver"
950  * Probes device for being a hub and configurate it
951  */
952
953 #undef  USB_HUB_DEBUG
954
955 #ifdef  USB_HUB_DEBUG
956 #define USB_HUB_PRINTF(fmt, args...)    printf(fmt , ##args)
957 #else
958 #define USB_HUB_PRINTF(fmt, args...)
959 #endif
960
961
962 static struct usb_hub_device hub_dev[USB_MAX_HUB];
963 static int usb_hub_index;
964
965
966 int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
967 {
968         return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
969                 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
970                 USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
971 }
972
973 int usb_clear_hub_feature(struct usb_device *dev, int feature)
974 {
975         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
976                                 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature,
977                                 0, NULL, 0, USB_CNTL_TIMEOUT);
978 }
979
980 int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
981 {
982         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
983                                 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
984                                 port, NULL, 0, USB_CNTL_TIMEOUT);
985 }
986
987 int usb_set_port_feature(struct usb_device *dev, int port, int feature)
988 {
989         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
990                                 USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
991                                 port, NULL, 0, USB_CNTL_TIMEOUT);
992 }
993
994 int usb_get_hub_status(struct usb_device *dev, void *data)
995 {
996         return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
997                         USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
998                         data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
999 }
1000
1001 int usb_get_port_status(struct usb_device *dev, int port, void *data)
1002 {
1003         return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
1004                         USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
1005                         data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
1006 }
1007
1008
1009 static void usb_hub_power_on(struct usb_hub_device *hub)
1010 {
1011         int i;
1012         struct usb_device *dev;
1013
1014         dev = hub->pusb_dev;
1015         /* Enable power to the ports */
1016         USB_HUB_PRINTF("enabling power on all ports\n");
1017         for (i = 0; i < dev->maxchild; i++) {
1018                 usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
1019                 USB_HUB_PRINTF("port %d returns %lX\n", i + 1, dev->status);
1020                 mdelay(hub->desc.bPwrOn2PwrGood * 2);
1021         }
1022 }
1023
1024 void usb_hub_reset(void)
1025 {
1026         usb_hub_index = 0;
1027 }
1028
1029 struct usb_hub_device *usb_hub_allocate(void)
1030 {
1031         if (usb_hub_index < USB_MAX_HUB)
1032                 return &hub_dev[usb_hub_index++];
1033
1034         printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
1035         return NULL;
1036 }
1037
1038 #define MAX_TRIES 5
1039
1040 static inline char *portspeed(int portstatus)
1041 {
1042         if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
1043                 return "480 Mb/s";
1044         else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
1045                 return "1.5 Mb/s";
1046         else
1047                 return "12 Mb/s";
1048 }
1049
1050 static int hub_port_reset(struct usb_device *dev, int port,
1051                         unsigned short *portstat)
1052 {
1053         int tries;
1054         struct usb_port_status portsts;
1055         unsigned short portstatus, portchange;
1056
1057         USB_HUB_PRINTF("hub_port_reset: resetting port %d...\n", port);
1058         for (tries = 0; tries < MAX_TRIES; tries++) {
1059
1060                 usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
1061                 mdelay(200);
1062
1063                 if (usb_get_port_status(dev, port + 1, &portsts) < 0) {
1064                         USB_HUB_PRINTF("get_port_status failed status %lX\n",
1065                                         dev->status);
1066                         return -1;
1067                 }
1068                 portstatus = le16_to_cpu(portsts.wPortStatus);
1069                 portchange = le16_to_cpu(portsts.wPortChange);
1070
1071                 USB_HUB_PRINTF("portstatus %x, change %x, %s\n",
1072                                 portstatus, portchange,
1073                                 portspeed(portstatus));
1074
1075                 USB_HUB_PRINTF("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
1076                                "  USB_PORT_STAT_ENABLE %d\n",
1077                         (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
1078                         (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
1079                         (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
1080
1081                 if ((portchange & USB_PORT_STAT_C_CONNECTION) ||
1082                     !(portstatus & USB_PORT_STAT_CONNECTION))
1083                         return -1;
1084
1085                 if (portstatus & USB_PORT_STAT_ENABLE)
1086                         break;
1087
1088                 mdelay(200);
1089         }
1090
1091         if (tries == MAX_TRIES) {
1092                 USB_HUB_PRINTF("Cannot enable port %i after %i retries, " \
1093                                 "disabling port.\n", port + 1, MAX_TRIES);
1094                 USB_HUB_PRINTF("Maybe the USB cable is bad?\n");
1095                 return -1;
1096         }
1097
1098         usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
1099         *portstat = portstatus;
1100         return 0;
1101 }
1102
1103
1104 void usb_hub_port_connect_change(struct usb_device *dev, int port)
1105 {
1106         struct usb_device *usb;
1107         struct usb_port_status portsts;
1108         unsigned short portstatus, portchange;
1109
1110         /* Check status */
1111         if (usb_get_port_status(dev, port + 1, &portsts) < 0) {
1112                 USB_HUB_PRINTF("get_port_status failed\n");
1113                 return;
1114         }
1115
1116         portstatus = le16_to_cpu(portsts.wPortStatus);
1117         portchange = le16_to_cpu(portsts.wPortChange);
1118         USB_HUB_PRINTF("portstatus %x, change %x, %s\n",
1119                         portstatus, portchange, portspeed(portstatus));
1120
1121         /* Clear the connection change status */
1122         usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
1123
1124         /* Disconnect any existing devices under this port */
1125         if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
1126              (!(portstatus & USB_PORT_STAT_ENABLE))) || (dev->children[port])) {
1127                 USB_HUB_PRINTF("usb_disconnect(&hub->children[port]);\n");
1128                 /* Return now if nothing is connected */
1129                 if (!(portstatus & USB_PORT_STAT_CONNECTION))
1130                         return;
1131         }
1132         mdelay(200);
1133
1134         /* Reset the port */
1135         if (hub_port_reset(dev, port, &portstatus) < 0) {
1136                 printf("cannot reset port %i!?\n", port + 1);
1137                 return;
1138         }
1139
1140         mdelay(200);
1141
1142         /* Allocate a new device struct for it */
1143         usb = usb_alloc_new_device();
1144
1145         if (portstatus & USB_PORT_STAT_HIGH_SPEED)
1146                 usb->speed = USB_SPEED_HIGH;
1147         else if (portstatus & USB_PORT_STAT_LOW_SPEED)
1148                 usb->speed = USB_SPEED_LOW;
1149         else
1150                 usb->speed = USB_SPEED_FULL;
1151
1152         dev->children[port] = usb;
1153         usb->parent = dev;
1154         /* Run it through the hoops (find a driver, etc) */
1155         if (usb_new_device(usb)) {
1156                 /* Woops, disable the port */
1157                 USB_HUB_PRINTF("hub: disabling port %d\n", port + 1);
1158                 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
1159         }
1160 }
1161
1162
1163 int usb_hub_configure(struct usb_device *dev)
1164 {
1165         unsigned char buffer[USB_BUFSIZ], *bitmap;
1166         struct usb_hub_descriptor *descriptor;
1167         struct usb_hub_status *hubsts;
1168         int i;
1169         struct usb_hub_device *hub;
1170
1171         /* "allocate" Hub device */
1172         hub = usb_hub_allocate();
1173         if (hub == NULL)
1174                 return -1;
1175         hub->pusb_dev = dev;
1176         /* Get the the hub descriptor */
1177         if (usb_get_hub_descriptor(dev, buffer, 4) < 0) {
1178                 USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
1179                                    "descriptor, giving up %lX\n", dev->status);
1180                 return -1;
1181         }
1182         descriptor = (struct usb_hub_descriptor *)buffer;
1183
1184         /* silence compiler warning if USB_BUFSIZ is > 256 [= sizeof(char)] */
1185         i = descriptor->bLength;
1186         if (i > USB_BUFSIZ) {
1187                 USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
1188                                 "descriptor - too long: %d\n",
1189                                 descriptor->bLength);
1190                 return -1;
1191         }
1192
1193         if (usb_get_hub_descriptor(dev, buffer, descriptor->bLength) < 0) {
1194                 USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
1195                                 "descriptor 2nd giving up %lX\n", dev->status);
1196                 return -1;
1197         }
1198         memcpy((unsigned char *)&hub->desc, buffer, descriptor->bLength);
1199         /* adjust 16bit values */
1200         hub->desc.wHubCharacteristics =
1201                                 le16_to_cpu(descriptor->wHubCharacteristics);
1202         /* set the bitmap */
1203         bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
1204         /* devices not removable by default */
1205         memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
1206         bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
1207         memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
1208
1209         for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
1210                 hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
1211
1212         for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
1213                 hub->desc.DeviceRemovable[i] = descriptor->PortPowerCtrlMask[i];
1214
1215         dev->maxchild = descriptor->bNbrPorts;
1216         USB_HUB_PRINTF("%d ports detected\n", dev->maxchild);
1217
1218         switch (hub->desc.wHubCharacteristics & HUB_CHAR_LPSM) {
1219         case 0x00:
1220                 USB_HUB_PRINTF("ganged power switching\n");
1221                 break;
1222         case 0x01:
1223                 USB_HUB_PRINTF("individual port power switching\n");
1224                 break;
1225         case 0x02:
1226         case 0x03:
1227                 USB_HUB_PRINTF("unknown reserved power switching mode\n");
1228                 break;
1229         }
1230
1231         if (hub->desc.wHubCharacteristics & HUB_CHAR_COMPOUND)
1232                 USB_HUB_PRINTF("part of a compound device\n");
1233         else
1234                 USB_HUB_PRINTF("standalone hub\n");
1235
1236         switch (hub->desc.wHubCharacteristics & HUB_CHAR_OCPM) {
1237         case 0x00:
1238                 USB_HUB_PRINTF("global over-current protection\n");
1239                 break;
1240         case 0x08:
1241                 USB_HUB_PRINTF("individual port over-current protection\n");
1242                 break;
1243         case 0x10:
1244         case 0x18:
1245                 USB_HUB_PRINTF("no over-current protection\n");
1246                 break;
1247         }
1248
1249         USB_HUB_PRINTF("power on to power good time: %dms\n",
1250                         descriptor->bPwrOn2PwrGood * 2);
1251         USB_HUB_PRINTF("hub controller current requirement: %dmA\n",
1252                         descriptor->bHubContrCurrent);
1253
1254         for (i = 0; i < dev->maxchild; i++)
1255                 USB_HUB_PRINTF("port %d is%s removable\n", i + 1,
1256                         hub->desc.DeviceRemovable[(i + 1) / 8] & \
1257                                            (1 << ((i + 1) % 8)) ? " not" : "");
1258
1259         if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
1260                 USB_HUB_PRINTF("usb_hub_configure: failed to get Status - " \
1261                                 "too long: %d\n", descriptor->bLength);
1262                 return -1;
1263         }
1264
1265         if (usb_get_hub_status(dev, buffer) < 0) {
1266                 USB_HUB_PRINTF("usb_hub_configure: failed to get Status %lX\n",
1267                                 dev->status);
1268                 return -1;
1269         }
1270
1271         hubsts = (struct usb_hub_status *)buffer;
1272         USB_HUB_PRINTF("get_hub_status returned status %X, change %X\n",
1273                         le16_to_cpu(hubsts->wHubStatus),
1274                         le16_to_cpu(hubsts->wHubChange));
1275         USB_HUB_PRINTF("local power source is %s\n",
1276                 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
1277                 "lost (inactive)" : "good");
1278         USB_HUB_PRINTF("%sover-current condition exists\n",
1279                 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
1280                 "" : "no ");
1281         usb_hub_power_on(hub);
1282
1283         for (i = 0; i < dev->maxchild; i++) {
1284                 struct usb_port_status portsts;
1285                 unsigned short portstatus, portchange;
1286
1287                 if (usb_get_port_status(dev, i + 1, &portsts) < 0) {
1288                         USB_HUB_PRINTF("get_port_status failed\n");
1289                         continue;
1290                 }
1291
1292                 portstatus = le16_to_cpu(portsts.wPortStatus);
1293                 portchange = le16_to_cpu(portsts.wPortChange);
1294                 USB_HUB_PRINTF("Port %d Status %X Change %X\n",
1295                                 i + 1, portstatus, portchange);
1296
1297                 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1298                         USB_HUB_PRINTF("port %d connection change\n", i + 1);
1299                         usb_hub_port_connect_change(dev, i);
1300                 }
1301                 if (portchange & USB_PORT_STAT_C_ENABLE) {
1302                         USB_HUB_PRINTF("port %d enable change, status %x\n",
1303                                         i + 1, portstatus);
1304                         usb_clear_port_feature(dev, i + 1,
1305                                                 USB_PORT_FEAT_C_ENABLE);
1306
1307                         /* EM interference sometimes causes bad shielded USB
1308                          * devices to be shutdown by the hub, this hack enables
1309                          * them again. Works at least with mouse driver */
1310                         if (!(portstatus & USB_PORT_STAT_ENABLE) &&
1311                              (portstatus & USB_PORT_STAT_CONNECTION) &&
1312                              ((dev->children[i]))) {
1313                                 USB_HUB_PRINTF("already running port %i "  \
1314                                                 "disabled by hub (EMI?), " \
1315                                                 "re-enabling...\n", i + 1);
1316                                         usb_hub_port_connect_change(dev, i);
1317                         }
1318                 }
1319                 if (portstatus & USB_PORT_STAT_SUSPEND) {
1320                         USB_HUB_PRINTF("port %d suspend change\n", i + 1);
1321                         usb_clear_port_feature(dev, i + 1,
1322                                                 USB_PORT_FEAT_SUSPEND);
1323                 }
1324
1325                 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
1326                         USB_HUB_PRINTF("port %d over-current change\n", i + 1);
1327                         usb_clear_port_feature(dev, i + 1,
1328                                                 USB_PORT_FEAT_C_OVER_CURRENT);
1329                         usb_hub_power_on(hub);
1330                 }
1331
1332                 if (portchange & USB_PORT_STAT_C_RESET) {
1333                         USB_HUB_PRINTF("port %d reset change\n", i + 1);
1334                         usb_clear_port_feature(dev, i + 1,
1335                                                 USB_PORT_FEAT_C_RESET);
1336                 }
1337         } /* end for i all ports */
1338
1339         return 0;
1340 }
1341
1342 int usb_hub_probe(struct usb_device *dev, int ifnum)
1343 {
1344         struct usb_interface *iface;
1345         struct usb_endpoint_descriptor *ep;
1346         int ret;
1347
1348         iface = &dev->config.if_desc[ifnum];
1349         /* Is it a hub? */
1350         if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
1351                 return 0;
1352         /* Some hubs have a subclass of 1, which AFAICT according to the */
1353         /*  specs is not defined, but it works */
1354         if ((iface->desc.bInterfaceSubClass != 0) &&
1355             (iface->desc.bInterfaceSubClass != 1))
1356                 return 0;
1357         /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1358         if (iface->desc.bNumEndpoints != 1)
1359                 return 0;
1360         ep = &iface->ep_desc[0];
1361         /* Output endpoint? Curiousier and curiousier.. */
1362         if (!(ep->bEndpointAddress & USB_DIR_IN))
1363                 return 0;
1364         /* If it's not an interrupt endpoint, we'd better punt! */
1365         if ((ep->bmAttributes & 3) != 3)
1366                 return 0;
1367         /* We found a hub */
1368         USB_HUB_PRINTF("USB hub found\n");
1369         ret = usb_hub_configure(dev);
1370         return ret;
1371 }
1372
1373 /* EOF */