]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - common/usb.c
Merge branch 'master' of git://git.denx.de/u-boot-usb
[karo-tx-uboot.git] / common / usb.c
index 3c9ede4b8e5cfd57c26775ec3cb1cf21d7bcbeda..6fc0fc1c0ec75fe34f100cbfb03b207628b32885 100644 (file)
@@ -47,6 +47,7 @@
 #include <common.h>
 #include <command.h>
 #include <asm/processor.h>
+#include <linux/compiler.h>
 #include <linux/ctype.h>
 #include <asm/byteorder.h>
 #include <asm/unaligned.h>
 
 static struct usb_device usb_dev[USB_MAX_DEVICE];
 static int dev_index;
-static int running;
 static int asynch_allowed;
 
 char usb_started; /* flag for the started/stopped USB status */
 
-/**********************************************************************
- * some forward declerations...
- */
-static void usb_scan_devices(void);
-
-/***********************************************************************
- * wait_ms
- */
-
-inline void wait_ms(unsigned long ms)
-{
-       while (ms-- > 0)
-               udelay(1000);
-}
+#ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
+#define CONFIG_USB_MAX_CONTROLLER_COUNT 1
+#endif
 
 /***************************************************************************
  * Init USB Device
  */
-
 int usb_init(void)
 {
-       int result;
+       void *ctrl;
+       struct usb_device *dev;
+       int i, start_index = 0;
 
-       running = 0;
        dev_index = 0;
        asynch_allowed = 1;
        usb_hub_reset();
+
+       /* first make all devices unknown */
+       for (i = 0; i < USB_MAX_DEVICE; i++) {
+               memset(&usb_dev[i], 0, sizeof(struct usb_device));
+               usb_dev[i].devnum = -1;
+       }
+
        /* init low_level USB */
-       printf("USB:   ");
-       result = usb_lowlevel_init();
-       /* if lowlevel init is OK, scan the bus for devices
-        * i.e. search HUBs and configure them */
-       if (result == 0) {
-               printf("scanning bus for devices... ");
-               running = 1;
-               usb_scan_devices();
+       for (i = 0; i < CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
+               /* init low_level USB */
+               printf("USB%d:   ", i);
+               if (usb_lowlevel_init(i, &ctrl)) {
+                       puts("lowlevel init failed\n");
+                       continue;
+               }
+               /*
+                * lowlevel init is OK, now scan the bus for devices
+                * i.e. search HUBs and configure them
+                */
+               start_index = dev_index;
+               printf("scanning bus %d for devices... ", i);
+               dev = usb_alloc_new_device(ctrl);
+               /*
+                * device 0 is always present
+                * (root hub, so let it analyze)
+                */
+               if (dev)
+                       usb_new_device(dev);
+
+               if (start_index == dev_index)
+                       puts("No USB Device found\n");
+               else
+                       printf("%d USB Device(s) found\n",
+                               dev_index - start_index);
+
                usb_started = 1;
-               return 0;
-       } else {
-               printf("Error, couldn't init Lowlevel part\n");
-               usb_started = 0;
+       }
+
+       USB_PRINTF("scan end\n");
+       /* if we were not able to find at least one working bus, bail out */
+       if (!usb_started) {
+               puts("USB error: all controllers failed lowlevel init\n");
                return -1;
        }
+
+       return 0;
 }
 
 /******************************************************************************
@@ -126,15 +145,20 @@ int usb_init(void)
  */
 int usb_stop(void)
 {
-       int res = 0;
+       int i;
 
        if (usb_started) {
                asynch_allowed = 1;
                usb_started = 0;
                usb_hub_reset();
-               res = usb_lowlevel_stop();
+
+               for (i = 0; i < CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
+                       if (usb_lowlevel_stop(i))
+                               printf("failed to stop USB controller %d\n", i);
+               }
        }
-       return res;
+
+       return 0;
 }
 
 /*
@@ -179,7 +203,7 @@ int usb_control_msg(struct usb_device *dev, unsigned int pipe,
                        unsigned short value, unsigned short index,
                        void *data, unsigned short size, int timeout)
 {
-       struct devrequest setup_packet;
+       ALLOC_CACHE_ALIGN_BUFFER(struct devrequest, setup_packet, 1);
 
        if ((timeout == 0) && (!asynch_allowed)) {
                /* request for a asynch control pipe is not allowed */
@@ -187,17 +211,18 @@ int usb_control_msg(struct usb_device *dev, unsigned int pipe,
        }
 
        /* set setup command */
-       setup_packet.requesttype = requesttype;
-       setup_packet.request = request;
-       setup_packet.value = cpu_to_le16(value);
-       setup_packet.index = cpu_to_le16(index);
-       setup_packet.length = cpu_to_le16(size);
+       setup_packet->requesttype = requesttype;
+       setup_packet->request = request;
+       setup_packet->value = cpu_to_le16(value);
+       setup_packet->index = cpu_to_le16(index);
+       setup_packet->length = cpu_to_le16(size);
        USB_PRINTF("usb_control_msg: request: 0x%X, requesttype: 0x%X, " \
                   "value 0x%X index 0x%X length 0x%X\n",
                   request, requesttype, value, index, size);
        dev->status = USB_ST_NOT_PROC; /*not yet processed */
 
-       submit_control_msg(dev, pipe, data, size, &setup_packet);
+       if (submit_control_msg(dev, pipe, data, size, setup_packet) < 0)
+               return -1;
        if (timeout == 0)
                return (int)size;
 
@@ -209,7 +234,7 @@ int usb_control_msg(struct usb_device *dev, unsigned int pipe,
        while (timeout--) {
                if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC))
                        break;
-               wait_ms(1);
+               mdelay(1);
        }
        if (dev->status)
                return -1;
@@ -229,11 +254,12 @@ int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
        if (len < 0)
                return -1;
        dev->status = USB_ST_NOT_PROC; /*not yet processed */
-       submit_bulk_msg(dev, pipe, data, len);
+       if (submit_bulk_msg(dev, pipe, data, len) < 0)
+               return -1;
        while (timeout--) {
                if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC))
                        break;
-               wait_ms(1);
+               mdelay(1);
        }
        *actual_length = dev->act_len;
        if (dev->status == 0)
@@ -271,7 +297,7 @@ int usb_maxpacket(struct usb_device *dev, unsigned long pipe)
  *
  * NOTE: Similar behaviour was observed with GCC4.6 on ARMv5.
  */
-static void  __attribute__((noinline))
+static void noinline
 usb_set_maxpacket_ep(struct usb_device *dev, int if_idx, int ep_idx)
 {
        int b;
@@ -466,9 +492,9 @@ int usb_get_configuration_no(struct usb_device *dev,
 {
        int result;
        unsigned int tmp;
-       struct usb_configuration_descriptor *config;
+       struct usb_config_descriptor *config;
 
-       config = (struct usb_configuration_descriptor *)&buffer[0];
+       config = (struct usb_config_descriptor *)&buffer[0];
        result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, 9);
        if (result < 9) {
                if (result < 0)
@@ -482,8 +508,8 @@ int usb_get_configuration_no(struct usb_device *dev,
        tmp = le16_to_cpu(config->wTotalLength);
 
        if (tmp > USB_BUFSIZ) {
-               USB_PRINTF("usb_get_configuration_no: failed to get " \
-                          "descriptor - too long: %d\n", tmp);
+               printf("usb_get_configuration_no: failed to get " \
+                      "descriptor - too long: %d\n", tmp);
                return -1;
        }
 
@@ -691,7 +717,7 @@ static int usb_string_sub(struct usb_device *dev, unsigned int langid,
  */
 int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
 {
-       unsigned char mybuf[USB_BUFSIZ];
+       ALLOC_CACHE_ALIGN_BUFFER(unsigned char, mybuf, USB_BUFSIZ);
        unsigned char *tbuf;
        int err;
        unsigned int u, idx;
@@ -757,11 +783,10 @@ struct usb_device *usb_get_dev_index(int index)
                return &usb_dev[index];
 }
 
-
 /* returns a pointer of a new device structure or NULL, if
  * no device struct is available
  */
-struct usb_device *usb_alloc_new_device(void)
+struct usb_device *usb_alloc_new_device(void *controller)
 {
        int i;
        USB_PRINTF("New Device %d\n", dev_index);
@@ -775,10 +800,23 @@ struct usb_device *usb_alloc_new_device(void)
        for (i = 0; i < USB_MAXCHILDREN; i++)
                usb_dev[dev_index].children[i] = NULL;
        usb_dev[dev_index].parent = NULL;
+       usb_dev[dev_index].controller = controller;
        dev_index++;
        return &usb_dev[dev_index - 1];
 }
 
+/*
+ * Free the newly created device node.
+ * Called in error cases where configuring a newly attached
+ * device fails for some reason.
+ */
+void usb_free_device(void)
+{
+       dev_index--;
+       USB_PRINTF("Freeing device node: %d\n", dev_index);
+       memset(&usb_dev[dev_index], 0, sizeof(struct usb_device));
+       usb_dev[dev_index].devnum = -1;
+}
 
 /*
  * By the time we get here, the device has gotten a new device ID
@@ -791,7 +829,7 @@ int usb_new_device(struct usb_device *dev)
 {
        int addr, err;
        int tmp;
-       unsigned char tmpbuf[USB_BUFSIZ];
+       ALLOC_CACHE_ALIGN_BUFFER(unsigned char, tmpbuf, USB_BUFSIZ);
 
        /* We still haven't set the Address yet */
        addr = dev->devnum;
@@ -808,12 +846,13 @@ int usb_new_device(struct usb_device *dev)
        dev->epmaxpacketin[0] = 8;
        dev->epmaxpacketout[0] = 8;
 
-       err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, &dev->descriptor, 8);
+       err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, tmpbuf, 8);
        if (err < 8) {
                printf("\n      USB device not responding, " \
                       "giving up (status=%lX)\n", dev->status);
                return 1;
        }
+       memcpy(&dev->descriptor, tmpbuf, 8);
 #else
        /* This is a Windows scheme of initialization sequence, with double
         * reset of the device (Linux uses the same sequence)
@@ -897,12 +936,12 @@ int usb_new_device(struct usb_device *dev)
                return 1;
        }
 
-       wait_ms(10);    /* Let the SET_ADDRESS settle */
+       mdelay(10);     /* Let the SET_ADDRESS settle */
 
        tmp = sizeof(dev->descriptor);
 
        err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
-                                &dev->descriptor, sizeof(dev->descriptor));
+                                tmpbuf, sizeof(dev->descriptor));
        if (err < tmp) {
                if (err < 0)
                        printf("unable to get device descriptor (error=%d)\n",
@@ -912,14 +951,21 @@ int usb_new_device(struct usb_device *dev)
                                "(expected %i, got %i)\n", tmp, err);
                return 1;
        }
+       memcpy(&dev->descriptor, tmpbuf, sizeof(dev->descriptor));
        /* correct le values */
        le16_to_cpus(&dev->descriptor.bcdUSB);
        le16_to_cpus(&dev->descriptor.idVendor);
        le16_to_cpus(&dev->descriptor.idProduct);
        le16_to_cpus(&dev->descriptor.bcdDevice);
        /* only support for one config for now */
-       usb_get_configuration_no(dev, &tmpbuf[0], 0);
-       usb_parse_config(dev, &tmpbuf[0], 0);
+       err = usb_get_configuration_no(dev, tmpbuf, 0);
+       if (err < 0) {
+               printf("usb_new_device: Cannot read configuration, " \
+                      "skipping device %04x:%04x\n",
+                      dev->descriptor.idVendor, dev->descriptor.idProduct);
+               return -1;
+       }
+       usb_parse_config(dev, tmpbuf, 0);
        usb_set_maxpacket(dev);
        /* we set the default configuration here */
        if (usb_set_configuration(dev, dev->config.desc.bConfigurationValue)) {
@@ -950,29 +996,4 @@ int usb_new_device(struct usb_device *dev)
        return 0;
 }
 
-/* build device Tree  */
-static void usb_scan_devices(void)
-{
-       int i;
-       struct usb_device *dev;
-
-       /* first make all devices unknown */
-       for (i = 0; i < USB_MAX_DEVICE; i++) {
-               memset(&usb_dev[i], 0, sizeof(struct usb_device));
-               usb_dev[i].devnum = -1;
-       }
-       dev_index = 0;
-       /* device 0 is always present (root hub, so let it analyze) */
-       dev = usb_alloc_new_device();
-       if (usb_new_device(dev))
-               printf("No USB Device found\n");
-       else
-               printf("%d USB Device(s) found\n", dev_index);
-       /* insert "driver" if possible */
-#ifdef CONFIG_USB_KEYBOARD
-       drv_usb_kbd_init();
-#endif
-       USB_PRINTF("scan end\n");
-}
-
 /* EOF */