]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - common/usb_storage.c
dm: usb: Split hub detection into its own function
[karo-tx-uboot.git] / common / usb_storage.c
index 4599d03c3ad6a3d6e6b36d25d14e6d2efce71c42..a4ca7a59577ac5df307b40f99ca64ad793d17057 100644 (file)
  * BBB support based on /sys/dev/usb/umass.c from
  * FreeBSD.
  *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
- *
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 /* Note:
@@ -50,6 +33,7 @@
 
 #include <common.h>
 #include <command.h>
+#include <inttypes.h>
 #include <asm/byteorder.h>
 #include <asm/processor.h>
 
@@ -224,6 +208,30 @@ static unsigned int usb_get_max_lun(struct us_data *us)
        return (len > 0) ? *result : 0;
 }
 
+static int usb_storage_register(struct usb_device *dev, unsigned char iface)
+{
+       int lun, max_lun, start = usb_max_devs;
+       int nb_dev = 0;
+
+       if (!usb_storage_probe(dev, iface, &usb_stor[usb_max_devs]))
+               return nb_dev;
+
+       /*
+        * OK, it's a storage device.  Iterate over its LUNs
+        * and populate `usb_dev_desc'.
+        */
+       max_lun = usb_get_max_lun(&usb_stor[usb_max_devs]);
+       for (lun = 0; lun <= max_lun && usb_max_devs < USB_MAX_STOR_DEV; lun++) {
+               usb_dev_desc[usb_max_devs].lun = lun;
+               if (usb_stor_get_info(dev, &usb_stor[start],
+                   &usb_dev_desc[usb_max_devs]) == 1) {
+                       nb_dev++;
+               }
+       }
+
+       return nb_dev;
+}
+
 /*******************************************************************************
  * scan the usb and reports device info
  * to the user if mode = 1
@@ -231,7 +239,7 @@ static unsigned int usb_get_max_lun(struct us_data *us)
  */
 int usb_stor_scan(int mode)
 {
-       unsigned char i;
+       unsigned char i, iface;
        struct usb_device *dev;
 
        if (mode == 1)
@@ -257,23 +265,10 @@ int usb_stor_scan(int mode)
                if (dev == NULL)
                        break; /* no more devices available */
 
-               if (usb_storage_probe(dev, 0, &usb_stor[usb_max_devs])) {
-                       /* OK, it's a storage device.  Iterate over its LUNs
-                        * and populate `usb_dev_desc'.
-                        */
-                       int lun, max_lun, start = usb_max_devs;
-
-                       max_lun = usb_get_max_lun(&usb_stor[usb_max_devs]);
-                       for (lun = 0;
-                            lun <= max_lun && usb_max_devs < USB_MAX_STOR_DEV;
-                            lun++) {
-                               usb_dev_desc[usb_max_devs].lun = lun;
-                               if (usb_stor_get_info(dev, &usb_stor[start],
-                                   &usb_dev_desc[usb_max_devs]) == 1) {
-                                       usb_max_devs++;
-                               }
-                       }
+               for (iface = 0; iface < dev->config.no_of_if; iface++) {
+                       usb_max_devs += usb_storage_register(dev, iface);
                }
+
                /* if storage device */
                if (usb_max_devs == USB_MAX_STOR_DEV) {
                        printf("max USB Storage Device reached: %d stopping\n",
@@ -352,8 +347,8 @@ static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
                /* set up the transfer loop */
                do {
                        /* transfer the data */
-                       debug("Bulk xfer 0x%x(%d) try #%d\n",
-                             (unsigned int)buf, this_xfer, 11 - maxtry);
+                       debug("Bulk xfer %p(%d) try #%d\n",
+                             buf, this_xfer, 11 - maxtry);
                        result = usb_bulk_msg(us->pusb_dev, pipe, buf,
                                              this_xfer, &partial,
                                              USB_CNTL_TIMEOUT * 5);
@@ -530,6 +525,7 @@ static int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
        cbw->bCDBLength = srb->cmdlen;
        /* copy the command data into the CBW command data buffer */
        /* DST SRC LEN!!! */
+
        memcpy(cbw->CBWCDB, srb->cmd, srb->cmdlen);
        result = usb_bulk_msg(us->pusb_dev, pipe, cbw, UMASS_BBB_CBW_SIZE,
                              &actlen, USB_CNTL_TIMEOUT * 5);
@@ -619,7 +615,7 @@ static int usb_stor_CBI_get_status(ccb *srb, struct us_data *us)
                        (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
        timeout = 1000;
        while (timeout--) {
-               if ((volatile int *) us->ip_wanted == NULL)
+               if (us->ip_wanted == 0)
                        break;
                mdelay(10);
        }
@@ -705,6 +701,7 @@ static int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
                pipe = pipein;
        else
                pipe = pipeout;
+
        result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen,
                              &data_actlen, USB_CNTL_TIMEOUT * 5);
        /* special handling of STALL in DATA phase */
@@ -1083,12 +1080,12 @@ unsigned long usb_stor_read(int device, lbaint_t blknr,
 
        usb_disable_asynch(1); /* asynch transfer not allowed */
        srb->lun = usb_dev_desc[device].lun;
-       buf_addr = (unsigned long)buffer;
+       buf_addr = (uintptr_t)buffer;
        start = blknr;
        blks = blkcnt;
 
        debug("\nusb_read: dev %d startblk " LBAF ", blccnt " LBAF
-             " buffer %lx\n", device, start, blks, buf_addr);
+             " buffer %" PRIxPTR "\n", device, start, blks, buf_addr);
 
        do {
                /* XXX need some comment here */
@@ -1118,7 +1115,7 @@ retry_it:
        ss->flags &= ~USB_READY;
 
        debug("usb_read: end startblk " LBAF
-             ", blccnt %x buffer %lx\n",
+             ", blccnt %x buffer %" PRIxPTR "\n",
              start, smallblks, buf_addr);
 
        usb_disable_asynch(0); /* asynch transfer allowed */
@@ -1157,12 +1154,12 @@ unsigned long usb_stor_write(int device, lbaint_t blknr,
        usb_disable_asynch(1); /* asynch transfer not allowed */
 
        srb->lun = usb_dev_desc[device].lun;
-       buf_addr = (unsigned long)buffer;
+       buf_addr = (uintptr_t)buffer;
        start = blknr;
        blks = blkcnt;
 
        debug("\nusb_write: dev %d startblk " LBAF ", blccnt " LBAF
-             " buffer %lx\n", device, start, blks, buf_addr);
+             " buffer %" PRIxPTR "\n", device, start, blks, buf_addr);
 
        do {
                /* If write fails retry for max retry count else
@@ -1193,8 +1190,8 @@ retry_it:
        } while (blks != 0);
        ss->flags &= ~USB_READY;
 
-       debug("usb_write: end startblk " LBAF ", blccnt %x buffer %lx\n",
-             start, smallblks, buf_addr);
+       debug("usb_write: end startblk " LBAF ", blccnt %x buffer %"
+             PRIxPTR "\n", start, smallblks, buf_addr);
 
        usb_disable_asynch(0); /* asynch transfer allowed */
        if (blkcnt >= USB_MAX_XFER_BLK)
@@ -1350,9 +1347,9 @@ int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
                      block_dev_desc_t *dev_desc)
 {
        unsigned char perq, modi;
-       ALLOC_CACHE_ALIGN_BUFFER(unsigned long, cap, 2);
-       ALLOC_CACHE_ALIGN_BUFFER(unsigned char, usb_stor_buf, 36);
-       unsigned long *capacity, *blksz;
+       ALLOC_CACHE_ALIGN_BUFFER(u32, cap, 2);
+       ALLOC_CACHE_ALIGN_BUFFER(u8, usb_stor_buf, 36);
+       u32 capacity, blksz;
        ccb *pccb = &usb_ccb;
 
        pccb->pdata = usb_stor_buf;
@@ -1367,17 +1364,20 @@ int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
        perq = usb_stor_buf[0];
        modi = usb_stor_buf[1];
 
-       if ((perq & 0x1f) == 0x1f) {
-               /* skip unknown devices */
+       /*
+        * Skip unknown devices (0x1f) and enclosure service devices (0x0d),
+        * they would not respond to test_unit_ready .
+        */
+       if (((perq & 0x1f) == 0x1f) || ((perq & 0x1f) == 0x0d)) {
                return 0;
        }
        if ((modi&0x80) == 0x80) {
                /* drive is removable */
                dev_desc->removable = 1;
        }
-       memcpy(&dev_desc->vendor[0], (const void *) &usb_stor_buf[8], 8);
-       memcpy(&dev_desc->product[0], (const void *) &usb_stor_buf[16], 16);
-       memcpy(&dev_desc->revision[0], (const void *) &usb_stor_buf[32], 4);
+       memcpy(dev_desc->vendor, (const void *)&usb_stor_buf[8], 8);
+       memcpy(dev_desc->product, (const void *)&usb_stor_buf[16], 16);
+       memcpy(dev_desc->revision, (const void *)&usb_stor_buf[32], 4);
        dev_desc->vendor[8] = 0;
        dev_desc->product[16] = 0;
        dev_desc->revision[4] = 0;
@@ -1398,7 +1398,7 @@ int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
                }
                return 0;
        }
-       pccb->pdata = (unsigned char *)&cap[0];
+       pccb->pdata = (unsigned char *)cap;
        memset(pccb->pdata, 0, 8);
        if (usb_read_capacity(pccb, ss) != 0) {
                printf("READ_CAP ERROR\n");
@@ -1406,21 +1406,21 @@ int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
                cap[1] = 0x200;
        }
        ss->flags &= ~USB_READY;
-       debug("Read Capacity returns: 0x%lx, 0x%lx\n", cap[0], cap[1]);
+       debug("Read Capacity returns: 0x%08x, 0x%08x\n", cap[0], cap[1]);
 #if 0
        if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */
                cap[0] >>= 16;
-#endif
+
        cap[0] = cpu_to_be32(cap[0]);
        cap[1] = cpu_to_be32(cap[1]);
+#endif
+
+       capacity = be32_to_cpu(cap[0]) + 1;
+       blksz = be32_to_cpu(cap[1]);
 
-       /* this assumes bigendian! */
-       cap[0] += 1;
-       capacity = &cap[0];
-       blksz = &cap[1];
-       debug("Capacity = 0x%lx, blocksz = 0x%lx\n", *capacity, *blksz);
-       dev_desc->lba = *capacity;
-       dev_desc->blksz = *blksz;
+       debug("Capacity = 0x%08x, blocksz = 0x%08x\n", capacity, blksz);
+       dev_desc->lba = capacity;
+       dev_desc->blksz = blksz;
        dev_desc->log2blksz = LOG2(dev_desc->blksz);
        dev_desc->type = perq;
        debug(" address %d\n", dev_desc->target);