3 * Most of this source has been derived from the Linux USB
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)
16 * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
18 * See file CREDITS for list of people who contributed to this
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.
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.
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,
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
45 * For each transfer (except "Interrupt") we wait for completion.
49 #include <asm/processor.h>
50 #include <linux/ctype.h>
51 #include <asm/byteorder.h>
55 #include <asm/4xx_pci.h>
61 #define USB_PRINTF(fmt, args...) printf(fmt , ##args)
63 #define USB_PRINTF(fmt, args...)
66 #define USB_BUFSIZ 512
68 static struct usb_device usb_dev[USB_MAX_DEVICE];
71 static int asynch_allowed;
72 static struct devrequest setup_packet;
74 char usb_started; /* flag for the started/stopped USB status */
76 /**********************************************************************
77 * some forward declerations...
79 void usb_scan_devices(void);
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);
86 /***************************************************************************
98 /* init low_level 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 */
104 printf("scanning bus for devices... ");
110 printf("Error, couldn't init Lowlevel part\n");
116 /******************************************************************************
117 * Stop USB this stops the LowLevel Part and deregisters USB devices.
127 res = usb_lowlevel_stop();
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.
136 void usb_disable_asynch(int disable)
138 asynch_allowed = !disable;
142 /*-------------------------------------------------------------------
148 * submits an Interrupt Message
150 int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe,
151 void *buffer, int transfer_len, int interval)
153 return submit_int_msg(dev, pipe, buffer, transfer_len, interval);
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.
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)
170 if ((timeout == 0) && (!asynch_allowed)) {
171 /* request for a asynch control pipe is not allowed */
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 */
186 submit_control_msg(dev, pipe, data, size, &setup_packet);
191 * Wait for status to update until timeout expires, USB driver
192 * interrupt handler may set the status when the USB operation has
196 if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC))
207 /*-------------------------------------------------------------------
208 * submits bulk message, and waits for completion. returns 0 if Ok or
210 * synchronous behavior
212 int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
213 void *data, int len, int *actual_length, int timeout)
217 dev->status = USB_ST_NOT_PROC; /*not yet processed */
218 submit_bulk_msg(dev, pipe, data, len);
220 if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC))
224 *actual_length = dev->act_len;
225 if (dev->status == 0)
232 /*-------------------------------------------------------------------
237 * returns the max packet size, depending on the pipe direction and
238 * the configurations values
240 int usb_maxpacket(struct usb_device *dev, unsigned long pipe)
242 /* direction is out -> use emaxpacket out */
243 if ((pipe & USB_DIR_IN) == 0)
244 return dev->epmaxpacketout[((pipe>>15) & 0xf)];
246 return dev->epmaxpacketin[((pipe>>15) & 0xf)];
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)
257 static void __attribute__((noinline))
258 usb_set_maxpacket_ep(struct usb_device *dev, struct usb_endpoint_descriptor *ep)
262 b = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
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]);
272 if ((ep->bEndpointAddress & 0x80) == 0) {
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]);
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]);
291 * set the max packed value of all endpoints in the given configuration
293 int usb_set_maxpacket(struct usb_device *dev)
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]);
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.
309 int usb_parse_config(struct usb_device *dev, unsigned char *buffer, int cfgno)
311 struct usb_descriptor_header *head;
312 int index, ifno, epno, curr_if_num;
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);
327 memcpy(&dev->config, buffer, buffer[0]);
328 le16_to_cpus(&(dev->config.desc.wTotalLength));
329 dev->config.no_of_if = 0;
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;
348 dev->config.if_desc[ifno].desc.bInterfaceNumber;
350 /* found alternate setting for the interface */
351 dev->config.if_desc[ifno].num_altsetting++;
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].\
362 USB_PRINTF("if %d, ep %d\n", ifno, epno);
365 if (head->bLength == 0)
368 USB_PRINTF("unknown Description Type : %x\n",
369 head->bDescriptorType);
372 ch = (unsigned char *)head;
373 for (i = 0; i < head->bLength; i++)
374 USB_PRINTF("%02X ", *ch++);
375 USB_PRINTF("\n\n\n");
379 index += head->bLength;
380 head = (struct usb_descriptor_header *)&buffer[index];
385 /***********************************************************************
387 * endp: endpoint number in bits 0-3;
388 * direction flag in bit 7 (1 = IN, 0 = OUT)
390 int usb_clear_halt(struct usb_device *dev, int pipe)
393 int endp = usb_pipeendpoint(pipe)|(usb_pipein(pipe)<<7);
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);
399 /* don't clear if failed */
404 * NOTE: we do not get status and verify reset was successful
405 * as some devices are reported to lock up upon this check..
408 usb_endpoint_running(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
410 /* toggle is reset on clear */
411 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
416 /**********************************************************************
417 * get_descriptor type
419 int usb_get_descriptor(struct usb_device *dev, unsigned char type,
420 unsigned char index, void *buf, int size)
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);
430 /**********************************************************************
431 * gets configuration cfgno and store it in the buffer
433 int usb_get_configuration_no(struct usb_device *dev,
434 unsigned char *buffer, int cfgno)
438 struct usb_configuration_descriptor *config;
440 config = (struct usb_configuration_descriptor *)&buffer[0];
441 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, 9);
444 printf("unable to get descriptor, error %lX\n",
447 printf("config descriptor too short " \
448 "(expected %i, got %i)\n", 9, result);
451 tmp = le16_to_cpu(config->wTotalLength);
453 if (tmp > USB_BUFSIZ) {
454 USB_PRINTF("usb_get_configuration_no: failed to get " \
455 "descriptor - too long: %d\n", tmp);
459 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, tmp);
460 USB_PRINTF("get_conf_no %d Result %d, wLength %d\n",
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)
469 int usb_set_address(struct usb_device *dev)
473 USB_PRINTF("set address %d\n", dev->devnum);
474 res = usb_control_msg(dev, usb_snddefctrl(dev),
475 USB_REQ_SET_ADDRESS, 0,
477 NULL, 0, USB_CNTL_TIMEOUT);
481 /********************************************************************
482 * set interface number to interface
484 int usb_set_interface(struct usb_device *dev, int interface, int alternate)
486 struct usb_interface *if_face = NULL;
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];
496 printf("selecting invalid interface %d", interface);
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.
506 if (if_face->num_altsetting == 1)
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);
519 /********************************************************************
520 * set configuration number to configuration
522 int usb_set_configuration(struct usb_device *dev, int configuration)
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,
530 NULL, 0, USB_CNTL_TIMEOUT);
539 /********************************************************************
540 * set protocol to protocol
542 int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol)
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);
549 /********************************************************************
552 int usb_set_idle(struct usb_device *dev, int ifnum, int duration, int report_id)
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);
559 /********************************************************************
562 int usb_get_report(struct usb_device *dev, int ifnum, unsigned char type,
563 unsigned char id, void *buf, int size)
565 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
567 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
568 (type << 8) + id, ifnum, buf, size, USB_CNTL_TIMEOUT);
571 /********************************************************************
572 * get class descriptor
574 int usb_get_class_descriptor(struct usb_device *dev, int ifnum,
575 unsigned char type, unsigned char id, void *buf, int size)
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);
582 /********************************************************************
583 * get string index in buffer
585 int usb_get_string(struct usb_device *dev, unsigned short langid,
586 unsigned char index, void *buf, int size)
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,
606 static void usb_try_string_workarounds(unsigned char *buf, int *length)
608 int newlength, oldlength = *length;
610 for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
611 if (!isprint(buf[newlength]) || buf[newlength + 1])
621 static int usb_string_sub(struct usb_device *dev, unsigned int langid,
622 unsigned int index, unsigned char *buf)
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);
630 /* If that failed try to read the descriptor length, then
631 * ask for just that many bytes */
633 rc = usb_get_string(dev, langid, index, buf, 2);
635 rc = usb_get_string(dev, langid, index, buf, buf[0]);
639 if (!buf[0] && !buf[1])
640 usb_try_string_workarounds(buf, &rc);
642 /* There might be extra junk at the end of the descriptor */
646 rc = rc - (rc & 1); /* force a multiple of two */
656 /********************************************************************
658 * Get string index and translate it to ascii.
659 * returns string length (> 0) or error (< 0)
661 int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
663 unsigned char mybuf[USB_BUFSIZ];
668 if (size <= 0 || !buf || !index)
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);
677 USB_PRINTF("error getting string descriptor 0 " \
678 "(error=%lx)\n", dev->status);
680 } else if (tbuf[0] < 4) {
681 USB_PRINTF("string descriptor 0 too short\n");
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);
693 err = usb_string_sub(dev, dev->string_langid, index, tbuf);
697 size--; /* leave room for trailing NULL char in output buffer */
698 for (idx = 0, u = 2; u < err; u += 2) {
701 if (tbuf[u+1]) /* high byte */
702 buf[idx++] = '?'; /* non-ASCII character */
704 buf[idx++] = tbuf[u];
712 /********************************************************************
713 * USB device handling:
714 * the USB device are static allocated [USB_MAX_DEVICE].
718 /* returns a pointer to the device with the index [index].
719 * if the device is not assigned (dev->devnum==-1) returns NULL
721 struct usb_device *usb_get_dev_index(int index)
723 if (usb_dev[index].devnum == -1)
726 return &usb_dev[index];
730 /* returns a pointer of a new device structure or NULL, if
731 * no device struct is available
733 struct usb_device *usb_alloc_new_device(void)
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);
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;
748 return &usb_dev[dev_index - 1];
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..
757 * Returns 0 for success, != 0 for error.
759 int usb_new_device(struct usb_device *dev)
763 unsigned char tmpbuf[USB_BUFSIZ];
765 /* We still haven't set the Address yet */
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;
780 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, &dev->descriptor, 8);
782 printf("\n USB device not responding, " \
783 "giving up (status=%lX)\n", dev->status);
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
794 struct usb_device_descriptor *desc;
796 struct usb_device *parent = dev->parent;
797 unsigned short portstatus;
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. */
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;
811 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, 64);
813 USB_PRINTF("usb_new_device: usb_get_descriptor() failed\n");
817 dev->descriptor.bMaxPacketSize0 = desc->bMaxPacketSize0;
819 /* find the port number we're at */
823 for (j = 0; j < parent->maxchild; j++) {
824 if (parent->children[j] == dev) {
830 printf("usb_new_device:cannot locate device's port.\n");
834 /* reset the port for the second time */
835 err = hub_port_reset(dev->parent, port, &portstatus);
837 printf("\n Couldn't reset port %i\n", port);
843 dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0;
844 dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
845 switch (dev->descriptor.bMaxPacketSize0) {
847 dev->maxpacketsize = PACKET_SIZE_8;
850 dev->maxpacketsize = PACKET_SIZE_16;
853 dev->maxpacketsize = PACKET_SIZE_32;
856 dev->maxpacketsize = PACKET_SIZE_64;
861 err = usb_set_address(dev); /* set address */
864 printf("\n USB device not accepting new address " \
865 "(error=%lX)\n", dev->status);
869 mdelay(10); /* Let the SET_ADDRESS settle */
871 tmp = sizeof(dev->descriptor);
873 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
874 &dev->descriptor, sizeof(dev->descriptor));
877 printf("unable to get device descriptor (error=%d)\n",
880 printf("USB device descriptor short read " \
881 "(expected %i, got %i)\n", tmp, err);
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);
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);
922 /* build device Tree */
923 void usb_scan_devices(void)
926 struct usb_device *dev;
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;
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");
939 printf("%d USB Device(s) found\n", dev_index);
940 /* insert "driver" if possible */
941 #ifdef CONFIG_USB_KEYBOARD
943 USB_PRINTF("scan end\n");
948 /****************************************************************************
950 * Probes device for being a hub and configurate it
956 #define USB_HUB_PRINTF(fmt, args...) printf(fmt , ##args)
958 #define USB_HUB_PRINTF(fmt, args...)
962 static struct usb_hub_device hub_dev[USB_MAX_HUB];
963 static int usb_hub_index;
966 int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
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);
973 int usb_clear_hub_feature(struct usb_device *dev, int feature)
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);
980 int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
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);
987 int usb_set_port_feature(struct usb_device *dev, int port, int feature)
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);
994 int usb_get_hub_status(struct usb_device *dev, void *data)
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);
1001 int usb_get_port_status(struct usb_device *dev, int port, void *data)
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);
1009 static void usb_hub_power_on(struct usb_hub_device *hub)
1012 struct usb_device *dev;
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);
1024 void usb_hub_reset(void)
1029 struct usb_hub_device *usb_hub_allocate(void)
1031 if (usb_hub_index < USB_MAX_HUB)
1032 return &hub_dev[usb_hub_index++];
1034 printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
1040 static inline char *portspeed(int portstatus)
1042 if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
1044 else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
1050 static int hub_port_reset(struct usb_device *dev, int port,
1051 unsigned short *portstat)
1054 struct usb_port_status portsts;
1055 unsigned short portstatus, portchange;
1057 USB_HUB_PRINTF("hub_port_reset: resetting port %d...\n", port);
1058 for (tries = 0; tries < MAX_TRIES; tries++) {
1060 usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
1063 if (usb_get_port_status(dev, port + 1, &portsts) < 0) {
1064 USB_HUB_PRINTF("get_port_status failed status %lX\n",
1068 portstatus = le16_to_cpu(portsts.wPortStatus);
1069 portchange = le16_to_cpu(portsts.wPortChange);
1071 USB_HUB_PRINTF("portstatus %x, change %x, %s\n",
1072 portstatus, portchange,
1073 portspeed(portstatus));
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);
1081 if ((portchange & USB_PORT_STAT_C_CONNECTION) ||
1082 !(portstatus & USB_PORT_STAT_CONNECTION))
1085 if (portstatus & USB_PORT_STAT_ENABLE)
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");
1098 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
1099 *portstat = portstatus;
1104 void usb_hub_port_connect_change(struct usb_device *dev, int port)
1106 struct usb_device *usb;
1107 struct usb_port_status portsts;
1108 unsigned short portstatus, portchange;
1111 if (usb_get_port_status(dev, port + 1, &portsts) < 0) {
1112 USB_HUB_PRINTF("get_port_status failed\n");
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));
1121 /* Clear the connection change status */
1122 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
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))
1134 /* Reset the port */
1135 if (hub_port_reset(dev, port, &portstatus) < 0) {
1136 printf("cannot reset port %i!?\n", port + 1);
1142 /* Allocate a new device struct for it */
1143 usb = usb_alloc_new_device();
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;
1150 usb->speed = USB_SPEED_FULL;
1152 dev->children[port] = usb;
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);
1163 int usb_hub_configure(struct usb_device *dev)
1165 unsigned char buffer[USB_BUFSIZ], *bitmap;
1166 struct usb_hub_descriptor *descriptor;
1167 struct usb_hub_status *hubsts;
1169 struct usb_hub_device *hub;
1171 /* "allocate" Hub device */
1172 hub = usb_hub_allocate();
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);
1182 descriptor = (struct usb_hub_descriptor *)buffer;
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);
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);
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 */
1209 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
1210 hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
1212 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
1213 hub->desc.DeviceRemovable[i] = descriptor->PortPowerCtrlMask[i];
1215 dev->maxchild = descriptor->bNbrPorts;
1216 USB_HUB_PRINTF("%d ports detected\n", dev->maxchild);
1218 switch (hub->desc.wHubCharacteristics & HUB_CHAR_LPSM) {
1220 USB_HUB_PRINTF("ganged power switching\n");
1223 USB_HUB_PRINTF("individual port power switching\n");
1227 USB_HUB_PRINTF("unknown reserved power switching mode\n");
1231 if (hub->desc.wHubCharacteristics & HUB_CHAR_COMPOUND)
1232 USB_HUB_PRINTF("part of a compound device\n");
1234 USB_HUB_PRINTF("standalone hub\n");
1236 switch (hub->desc.wHubCharacteristics & HUB_CHAR_OCPM) {
1238 USB_HUB_PRINTF("global over-current protection\n");
1241 USB_HUB_PRINTF("individual port over-current protection\n");
1245 USB_HUB_PRINTF("no over-current protection\n");
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);
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" : "");
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);
1265 if (usb_get_hub_status(dev, buffer) < 0) {
1266 USB_HUB_PRINTF("usb_hub_configure: failed to get Status %lX\n",
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) ? \
1281 usb_hub_power_on(hub);
1283 for (i = 0; i < dev->maxchild; i++) {
1284 struct usb_port_status portsts;
1285 unsigned short portstatus, portchange;
1287 if (usb_get_port_status(dev, i + 1, &portsts) < 0) {
1288 USB_HUB_PRINTF("get_port_status failed\n");
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);
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);
1301 if (portchange & USB_PORT_STAT_C_ENABLE) {
1302 USB_HUB_PRINTF("port %d enable change, status %x\n",
1304 usb_clear_port_feature(dev, i + 1,
1305 USB_PORT_FEAT_C_ENABLE);
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);
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);
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);
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);
1337 } /* end for i all ports */
1342 int usb_hub_probe(struct usb_device *dev, int ifnum)
1344 struct usb_interface *iface;
1345 struct usb_endpoint_descriptor *ep;
1348 iface = &dev->config.if_desc[ifnum];
1350 if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
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))
1357 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1358 if (iface->desc.bNumEndpoints != 1)
1360 ep = &iface->ep_desc[0];
1361 /* Output endpoint? Curiousier and curiousier.. */
1362 if (!(ep->bEndpointAddress & USB_DIR_IN))
1364 /* If it's not an interrupt endpoint, we'd better punt! */
1365 if ((ep->bmAttributes & 3) != 3)
1367 /* We found a hub */
1368 USB_HUB_PRINTF("USB hub found\n");
1369 ret = usb_hub_configure(dev);