wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 460c322 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 2 | * Most of this source has been derived from the Linux USB |
| 3 | * project: |
| 4 | * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net) |
| 5 | * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org) |
| 6 | * (c) 1999 Michael Gee (michael@linuxspecific.com) |
| 7 | * (c) 2000 Yggdrasil Computing, Inc. |
| 8 | * |
| 9 | * |
| 10 | * Adapted for U-Boot: |
| 11 | * (C) Copyright 2001 Denis Peter, MPL AG Switzerland |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 12 | * |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 13 | * For BBB support (C) Copyright 2003 |
Detlev Zundel | 792a09e | 2009-05-13 10:54:10 +0200 | [diff] [blame] | 14 | * Gary Jennejohn, DENX Software Engineering <garyj@denx.de> |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 15 | * |
Wolfgang Denk | 460c322 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 16 | * BBB support based on /sys/dev/usb/umass.c from |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 17 | * FreeBSD. |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 18 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 19 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | /* Note: |
| 23 | * Currently only the CBI transport protocoll has been implemented, and it |
| 24 | * is only tested with a TEAC USB Floppy. Other Massstorages with CBI or CB |
| 25 | * transport protocoll may work as well. |
| 26 | */ |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 27 | /* |
| 28 | * New Note: |
| 29 | * Support for USB Mass Storage Devices (BBB) has been added. It has |
| 30 | * only been tested with USB memory sticks. |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 31 | */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 32 | |
| 33 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 34 | #include <common.h> |
| 35 | #include <command.h> |
Simon Glass | 4fd074d | 2014-10-15 04:38:38 -0600 | [diff] [blame] | 36 | #include <inttypes.h> |
Christian Eggers | c918261 | 2008-05-21 22:12:00 +0200 | [diff] [blame] | 37 | #include <asm/byteorder.h> |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 38 | #include <asm/processor.h> |
| 39 | |
Grant Likely | 735dd97 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 40 | #include <part.h> |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 41 | #include <usb.h> |
| 42 | |
wdenk | 80885a9 | 2004-02-26 23:46:20 +0000 | [diff] [blame] | 43 | #undef BBB_COMDAT_TRACE |
| 44 | #undef BBB_XPORT_TRACE |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 45 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 46 | #include <scsi.h> |
| 47 | /* direction table -- this indicates the direction of the data |
| 48 | * transfer for each command code -- a 1 indicates input |
| 49 | */ |
Mike Frysinger | 2ff12285 | 2010-10-20 07:16:04 -0400 | [diff] [blame] | 50 | static const unsigned char us_direction[256/8] = { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 51 | 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77, |
| 52 | 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, |
| 53 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, |
| 54 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 55 | }; |
| 56 | #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1) |
| 57 | |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 58 | static ccb usb_ccb __attribute__((aligned(ARCH_DMA_MINALIGN))); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 59 | static __u32 CBWTag; |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 60 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 61 | #define USB_MAX_STOR_DEV 5 |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 62 | static int usb_max_devs; /* number of highest available usb device */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 63 | |
| 64 | static block_dev_desc_t usb_dev_desc[USB_MAX_STOR_DEV]; |
| 65 | |
| 66 | struct us_data; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 67 | typedef int (*trans_cmnd)(ccb *cb, struct us_data *data); |
| 68 | typedef int (*trans_reset)(struct us_data *data); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 69 | |
| 70 | struct us_data { |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 71 | struct usb_device *pusb_dev; /* this usb_device */ |
| 72 | |
| 73 | unsigned int flags; /* from filter initially */ |
Benoît Thébaudeau | 3e8581b | 2012-08-10 18:27:11 +0200 | [diff] [blame] | 74 | # define USB_READY (1 << 0) |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 75 | unsigned char ifnum; /* interface number */ |
| 76 | unsigned char ep_in; /* in endpoint */ |
| 77 | unsigned char ep_out; /* out ....... */ |
| 78 | unsigned char ep_int; /* interrupt . */ |
| 79 | unsigned char subclass; /* as in overview */ |
| 80 | unsigned char protocol; /* .............. */ |
| 81 | unsigned char attention_done; /* force attn on first cmd */ |
| 82 | unsigned short ip_data; /* interrupt data */ |
| 83 | int action; /* what to do */ |
| 84 | int ip_wanted; /* needed */ |
| 85 | int *irq_handle; /* for USB int requests */ |
| 86 | unsigned int irqpipe; /* pipe for release_irq */ |
| 87 | unsigned char irqmaxp; /* max packed for irq Pipe */ |
| 88 | unsigned char irqinterval; /* Intervall for IRQ Pipe */ |
| 89 | ccb *srb; /* current srb */ |
| 90 | trans_reset transport_reset; /* reset routine */ |
| 91 | trans_cmnd transport; /* transport routine */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
Benoît Thébaudeau | cffcc50 | 2012-08-10 18:26:50 +0200 | [diff] [blame] | 94 | #ifdef CONFIG_USB_EHCI |
Stefan Herbrechtsmeier | 1b4bd0e | 2012-07-09 09:52:29 +0000 | [diff] [blame] | 95 | /* |
Benoît Thébaudeau | 4bee5c8 | 2012-08-10 18:23:25 +0200 | [diff] [blame] | 96 | * The U-Boot EHCI driver can handle any transfer length as long as there is |
| 97 | * enough free heap space left, but the SCSI READ(10) and WRITE(10) commands are |
| 98 | * limited to 65535 blocks. |
Stefan Herbrechtsmeier | 1b4bd0e | 2012-07-09 09:52:29 +0000 | [diff] [blame] | 99 | */ |
Benoît Thébaudeau | 4bee5c8 | 2012-08-10 18:23:25 +0200 | [diff] [blame] | 100 | #define USB_MAX_XFER_BLK 65535 |
Benoît Thébaudeau | cffcc50 | 2012-08-10 18:26:50 +0200 | [diff] [blame] | 101 | #else |
Benoît Thébaudeau | 4bee5c8 | 2012-08-10 18:23:25 +0200 | [diff] [blame] | 102 | #define USB_MAX_XFER_BLK 20 |
Benoît Thébaudeau | cffcc50 | 2012-08-10 18:26:50 +0200 | [diff] [blame] | 103 | #endif |
Stefan Herbrechtsmeier | 1b4bd0e | 2012-07-09 09:52:29 +0000 | [diff] [blame] | 104 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 105 | static struct us_data usb_stor[USB_MAX_STOR_DEV]; |
| 106 | |
| 107 | |
wdenk | 80885a9 | 2004-02-26 23:46:20 +0000 | [diff] [blame] | 108 | #define USB_STOR_TRANSPORT_GOOD 0 |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 109 | #define USB_STOR_TRANSPORT_FAILED -1 |
| 110 | #define USB_STOR_TRANSPORT_ERROR -2 |
| 111 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 112 | int usb_stor_get_info(struct usb_device *dev, struct us_data *us, |
| 113 | block_dev_desc_t *dev_desc); |
| 114 | int usb_storage_probe(struct usb_device *dev, unsigned int ifnum, |
| 115 | struct us_data *ss); |
Sascha Silbe | ff8fef5 | 2013-06-14 13:07:25 +0200 | [diff] [blame] | 116 | unsigned long usb_stor_read(int device, lbaint_t blknr, |
Gabe Black | e81e79e | 2012-10-12 14:26:07 +0000 | [diff] [blame] | 117 | lbaint_t blkcnt, void *buffer); |
Sascha Silbe | ff8fef5 | 2013-06-14 13:07:25 +0200 | [diff] [blame] | 118 | unsigned long usb_stor_write(int device, lbaint_t blknr, |
Gabe Black | e81e79e | 2012-10-12 14:26:07 +0000 | [diff] [blame] | 119 | lbaint_t blkcnt, const void *buffer); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 120 | struct usb_device * usb_get_dev_index(int index); |
| 121 | void uhci_show_temp_int_td(void); |
| 122 | |
Matthew McClintock | df3fc52 | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 123 | #ifdef CONFIG_PARTITIONS |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 124 | block_dev_desc_t *usb_stor_get_dev(int index) |
| 125 | { |
Kim B. Heino | aaad108 | 2010-03-12 15:46:56 +0200 | [diff] [blame] | 126 | return (index < usb_max_devs) ? &usb_dev_desc[index] : NULL; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 127 | } |
Matthew McClintock | df3fc52 | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 128 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 129 | |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 130 | static void usb_show_progress(void) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 131 | { |
Wolfgang Denk | 226fa9b | 2010-07-19 11:36:59 +0200 | [diff] [blame] | 132 | debug("."); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 135 | /******************************************************************************* |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 136 | * show info on storage devices; 'usb start/init' must be invoked earlier |
| 137 | * as we only retrieve structures populated during devices initialization |
| 138 | */ |
Aras Vaichas | f6b44e0 | 2008-03-25 12:09:07 +1100 | [diff] [blame] | 139 | int usb_stor_info(void) |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 140 | { |
| 141 | int i; |
| 142 | |
Aras Vaichas | f6b44e0 | 2008-03-25 12:09:07 +1100 | [diff] [blame] | 143 | if (usb_max_devs > 0) { |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 144 | for (i = 0; i < usb_max_devs; i++) { |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 145 | printf(" Device %d: ", i); |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 146 | dev_print(&usb_dev_desc[i]); |
| 147 | } |
Markus Klotzbuecher | b9e749e | 2008-03-26 18:26:43 +0100 | [diff] [blame] | 148 | return 0; |
Aras Vaichas | f6b44e0 | 2008-03-25 12:09:07 +1100 | [diff] [blame] | 149 | } |
Wolfgang Denk | 1aeed8d | 2008-04-13 09:59:26 -0700 | [diff] [blame] | 150 | |
Markus Klotzbuecher | b9e749e | 2008-03-26 18:26:43 +0100 | [diff] [blame] | 151 | printf("No storage devices, perhaps not 'usb start'ed..?\n"); |
| 152 | return 1; |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Ludovic Courtès | 99e9ed1 | 2010-10-05 22:04:26 +0200 | [diff] [blame] | 155 | static unsigned int usb_get_max_lun(struct us_data *us) |
| 156 | { |
| 157 | int len; |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 158 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, result, 1); |
Ludovic Courtès | 99e9ed1 | 2010-10-05 22:04:26 +0200 | [diff] [blame] | 159 | len = usb_control_msg(us->pusb_dev, |
| 160 | usb_rcvctrlpipe(us->pusb_dev, 0), |
| 161 | US_BBB_GET_MAX_LUN, |
| 162 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, |
| 163 | 0, us->ifnum, |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 164 | result, sizeof(char), |
Ludovic Courtès | 99e9ed1 | 2010-10-05 22:04:26 +0200 | [diff] [blame] | 165 | USB_CNTL_TIMEOUT * 5); |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 166 | debug("Get Max LUN -> len = %i, result = %i\n", len, (int) *result); |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 167 | return (len > 0) ? *result : 0; |
Ludovic Courtès | 99e9ed1 | 2010-10-05 22:04:26 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Franck Jullien | cd74965 | 2015-03-04 21:07:00 +0100 | [diff] [blame] | 170 | static int usb_storage_register(struct usb_device *dev, unsigned char iface) |
| 171 | { |
| 172 | int lun, max_lun, start = usb_max_devs; |
| 173 | int nb_dev = 0; |
| 174 | |
| 175 | if (!usb_storage_probe(dev, iface, &usb_stor[usb_max_devs])) |
| 176 | return nb_dev; |
| 177 | |
| 178 | /* |
| 179 | * OK, it's a storage device. Iterate over its LUNs |
| 180 | * and populate `usb_dev_desc'. |
| 181 | */ |
| 182 | max_lun = usb_get_max_lun(&usb_stor[usb_max_devs]); |
| 183 | for (lun = 0; lun <= max_lun && usb_max_devs < USB_MAX_STOR_DEV; lun++) { |
| 184 | usb_dev_desc[usb_max_devs].lun = lun; |
| 185 | if (usb_stor_get_info(dev, &usb_stor[start], |
| 186 | &usb_dev_desc[usb_max_devs]) == 1) { |
| 187 | nb_dev++; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return nb_dev; |
| 192 | } |
| 193 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 194 | /******************************************************************************* |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 195 | * scan the usb and reports device info |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 196 | * to the user if mode = 1 |
| 197 | * returns current device or -1 if no |
| 198 | */ |
| 199 | int usb_stor_scan(int mode) |
| 200 | { |
Franck Jullien | cd74965 | 2015-03-04 21:07:00 +0100 | [diff] [blame] | 201 | unsigned char i, iface; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 202 | struct usb_device *dev; |
| 203 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 204 | if (mode == 1) |
Lucas Stach | 93c2582 | 2012-09-26 00:14:36 +0200 | [diff] [blame] | 205 | printf(" scanning usb for storage devices... "); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 206 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 207 | usb_disable_asynch(1); /* asynch transfer not allowed */ |
| 208 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 209 | for (i = 0; i < USB_MAX_STOR_DEV; i++) { |
| 210 | memset(&usb_dev_desc[i], 0, sizeof(block_dev_desc_t)); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 211 | usb_dev_desc[i].if_type = IF_TYPE_USB; |
| 212 | usb_dev_desc[i].dev = i; |
| 213 | usb_dev_desc[i].part_type = PART_TYPE_UNKNOWN; |
Wolfgang Denk | a17c548 | 2010-07-19 11:36:56 +0200 | [diff] [blame] | 214 | usb_dev_desc[i].target = 0xff; |
| 215 | usb_dev_desc[i].type = DEV_TYPE_UNKNOWN; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 216 | usb_dev_desc[i].block_read = usb_stor_read; |
Mahavir Jain | 127e108 | 2009-11-03 12:22:10 +0530 | [diff] [blame] | 217 | usb_dev_desc[i].block_write = usb_stor_write; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 218 | } |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 219 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 220 | usb_max_devs = 0; |
| 221 | for (i = 0; i < USB_MAX_DEVICE; i++) { |
| 222 | dev = usb_get_dev_index(i); /* get device */ |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 223 | debug("i=%d\n", i); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 224 | if (dev == NULL) |
Loïc Minier | 6052cba | 2011-02-03 22:04:26 +0100 | [diff] [blame] | 225 | break; /* no more devices available */ |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 226 | |
Franck Jullien | cd74965 | 2015-03-04 21:07:00 +0100 | [diff] [blame] | 227 | for (iface = 0; iface < dev->config.no_of_if; iface++) { |
| 228 | usb_max_devs += usb_storage_register(dev, iface); |
Ludovic Courtès | 99e9ed1 | 2010-10-05 22:04:26 +0200 | [diff] [blame] | 229 | } |
Franck Jullien | cd74965 | 2015-03-04 21:07:00 +0100 | [diff] [blame] | 230 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 231 | /* if storage device */ |
| 232 | if (usb_max_devs == USB_MAX_STOR_DEV) { |
| 233 | printf("max USB Storage Device reached: %d stopping\n", |
| 234 | usb_max_devs); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 235 | break; |
| 236 | } |
| 237 | } /* for */ |
Wolfgang Denk | 095b8a3 | 2005-08-02 17:06:17 +0200 | [diff] [blame] | 238 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 239 | usb_disable_asynch(0); /* asynch transfer allowed */ |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 240 | printf("%d Storage Device(s) found\n", usb_max_devs); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 241 | if (usb_max_devs > 0) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 242 | return 0; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 243 | return -1; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static int usb_stor_irq(struct usb_device *dev) |
| 247 | { |
| 248 | struct us_data *us; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 249 | us = (struct us_data *)dev->privptr; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 250 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 251 | if (us->ip_wanted) |
| 252 | us->ip_wanted = 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 257 | #ifdef DEBUG |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 258 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 259 | static void usb_show_srb(ccb *pccb) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 260 | { |
| 261 | int i; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 262 | printf("SRB: len %d datalen 0x%lX\n ", pccb->cmdlen, pccb->datalen); |
| 263 | for (i = 0; i < 12; i++) |
| 264 | printf("%02X ", pccb->cmd[i]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 265 | printf("\n"); |
| 266 | } |
| 267 | |
| 268 | static void display_int_status(unsigned long tmp) |
| 269 | { |
| 270 | printf("Status: %s %s %s %s %s %s %s\n", |
| 271 | (tmp & USB_ST_ACTIVE) ? "Active" : "", |
| 272 | (tmp & USB_ST_STALLED) ? "Stalled" : "", |
| 273 | (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "", |
| 274 | (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "", |
| 275 | (tmp & USB_ST_NAK_REC) ? "NAKed" : "", |
| 276 | (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "", |
| 277 | (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : ""); |
| 278 | } |
| 279 | #endif |
| 280 | /*********************************************************************** |
| 281 | * Data transfer routines |
| 282 | ***********************************************************************/ |
| 283 | |
| 284 | static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length) |
| 285 | { |
| 286 | int max_size; |
| 287 | int this_xfer; |
| 288 | int result; |
| 289 | int partial; |
| 290 | int maxtry; |
| 291 | int stat; |
| 292 | |
| 293 | /* determine the maximum packet size for these transfers */ |
| 294 | max_size = usb_maxpacket(us->pusb_dev, pipe) * 16; |
| 295 | |
| 296 | /* while we have data left to transfer */ |
| 297 | while (length) { |
| 298 | |
| 299 | /* calculate how long this will be -- maximum or a remainder */ |
| 300 | this_xfer = length > max_size ? max_size : length; |
| 301 | length -= this_xfer; |
| 302 | |
| 303 | /* setup the retry counter */ |
| 304 | maxtry = 10; |
| 305 | |
| 306 | /* set up the transfer loop */ |
| 307 | do { |
| 308 | /* transfer the data */ |
Sergey Temerkhanov | f657087 | 2015-04-01 17:18:46 +0300 | [diff] [blame] | 309 | debug("Bulk xfer %p(%d) try #%d\n", |
| 310 | buf, this_xfer, 11 - maxtry); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 311 | result = usb_bulk_msg(us->pusb_dev, pipe, buf, |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 312 | this_xfer, &partial, |
| 313 | USB_CNTL_TIMEOUT * 5); |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 314 | debug("bulk_msg returned %d xferred %d/%d\n", |
| 315 | result, partial, this_xfer); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 316 | if (us->pusb_dev->status != 0) { |
| 317 | /* if we stall, we need to clear it before |
| 318 | * we go on |
| 319 | */ |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 320 | #ifdef DEBUG |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 321 | display_int_status(us->pusb_dev->status); |
| 322 | #endif |
| 323 | if (us->pusb_dev->status & USB_ST_STALLED) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 324 | debug("stalled ->clearing endpoint" \ |
| 325 | "halt for pipe 0x%x\n", pipe); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 326 | stat = us->pusb_dev->status; |
| 327 | usb_clear_halt(us->pusb_dev, pipe); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 328 | us->pusb_dev->status = stat; |
| 329 | if (this_xfer == partial) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 330 | debug("bulk transferred" \ |
| 331 | "with error %lX," \ |
| 332 | " but data ok\n", |
| 333 | us->pusb_dev->status); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | else |
| 337 | return result; |
| 338 | } |
| 339 | if (us->pusb_dev->status & USB_ST_NAK_REC) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 340 | debug("Device NAKed bulk_msg\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 341 | return result; |
| 342 | } |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 343 | debug("bulk transferred with error"); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 344 | if (this_xfer == partial) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 345 | debug(" %ld, but data ok\n", |
| 346 | us->pusb_dev->status); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | /* if our try counter reaches 0, bail out */ |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 350 | debug(" %ld, data %d\n", |
| 351 | us->pusb_dev->status, partial); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 352 | if (!maxtry--) |
| 353 | return result; |
| 354 | } |
| 355 | /* update to show what data was transferred */ |
| 356 | this_xfer -= partial; |
| 357 | buf += partial; |
| 358 | /* continue until this transfer is done */ |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 359 | } while (this_xfer); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* if we get here, we're done and successful */ |
| 363 | return 0; |
| 364 | } |
| 365 | |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 366 | static int usb_stor_BBB_reset(struct us_data *us) |
| 367 | { |
| 368 | int result; |
| 369 | unsigned int pipe; |
| 370 | |
| 371 | /* |
| 372 | * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class) |
| 373 | * |
| 374 | * For Reset Recovery the host shall issue in the following order: |
| 375 | * a) a Bulk-Only Mass Storage Reset |
| 376 | * b) a Clear Feature HALT to the Bulk-In endpoint |
| 377 | * c) a Clear Feature HALT to the Bulk-Out endpoint |
| 378 | * |
| 379 | * This is done in 3 steps. |
| 380 | * |
| 381 | * If the reset doesn't succeed, the device should be port reset. |
| 382 | * |
| 383 | * This comment stolen from FreeBSD's /sys/dev/usb/umass.c. |
| 384 | */ |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 385 | debug("BBB_reset\n"); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 386 | result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0), |
| 387 | US_BBB_RESET, |
| 388 | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 389 | 0, us->ifnum, NULL, 0, USB_CNTL_TIMEOUT * 5); |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 390 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 391 | if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 392 | debug("RESET:stall\n"); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 393 | return -1; |
| 394 | } |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 395 | |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 396 | /* long wait for reset */ |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 397 | mdelay(150); |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 398 | debug("BBB_reset result %d: status %lX reset\n", |
| 399 | result, us->pusb_dev->status); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 400 | pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in); |
| 401 | result = usb_clear_halt(us->pusb_dev, pipe); |
| 402 | /* long wait for reset */ |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 403 | mdelay(150); |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 404 | debug("BBB_reset result %d: status %lX clearing IN endpoint\n", |
| 405 | result, us->pusb_dev->status); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 406 | /* long wait for reset */ |
| 407 | pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out); |
| 408 | result = usb_clear_halt(us->pusb_dev, pipe); |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 409 | mdelay(150); |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 410 | debug("BBB_reset result %d: status %lX clearing OUT endpoint\n", |
| 411 | result, us->pusb_dev->status); |
| 412 | debug("BBB_reset done\n"); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 416 | /* FIXME: this reset function doesn't really reset the port, and it |
| 417 | * should. Actually it should probably do what it's doing here, and |
| 418 | * reset the port physically |
| 419 | */ |
| 420 | static int usb_stor_CB_reset(struct us_data *us) |
| 421 | { |
| 422 | unsigned char cmd[12]; |
| 423 | int result; |
| 424 | |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 425 | debug("CB_reset\n"); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 426 | memset(cmd, 0xff, sizeof(cmd)); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 427 | cmd[0] = SCSI_SEND_DIAG; |
| 428 | cmd[1] = 4; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 429 | result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0), |
| 430 | US_CBI_ADSC, |
| 431 | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 432 | 0, us->ifnum, cmd, sizeof(cmd), |
| 433 | USB_CNTL_TIMEOUT * 5); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 434 | |
| 435 | /* long wait for reset */ |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 436 | mdelay(1500); |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 437 | debug("CB_reset result %d: status %lX clearing endpoint halt\n", |
| 438 | result, us->pusb_dev->status); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 439 | usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in)); |
| 440 | usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out)); |
| 441 | |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 442 | debug("CB_reset done\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 443 | return 0; |
| 444 | } |
| 445 | |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 446 | /* |
| 447 | * Set up the command for a BBB device. Note that the actual SCSI |
| 448 | * command is copied into cbw.CBWCDB. |
| 449 | */ |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 450 | static int usb_stor_BBB_comdat(ccb *srb, struct us_data *us) |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 451 | { |
| 452 | int result; |
| 453 | int actlen; |
| 454 | int dir_in; |
| 455 | unsigned int pipe; |
Simon Glass | 2e17c87 | 2015-03-25 12:22:11 -0600 | [diff] [blame^] | 456 | ALLOC_CACHE_ALIGN_BUFFER(struct umass_bbb_cbw, cbw, 1); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 457 | |
| 458 | dir_in = US_DIRECTION(srb->cmd[0]); |
| 459 | |
| 460 | #ifdef BBB_COMDAT_TRACE |
Vivek Gautam | 605bd75 | 2013-04-12 16:34:34 +0530 | [diff] [blame] | 461 | printf("dir %d lun %d cmdlen %d cmd %p datalen %lu pdata %p\n", |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 462 | dir_in, srb->lun, srb->cmdlen, srb->cmd, srb->datalen, |
| 463 | srb->pdata); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 464 | if (srb->cmdlen) { |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 465 | for (result = 0; result < srb->cmdlen; result++) |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 466 | printf("cmd[%d] %#x ", result, srb->cmd[result]); |
| 467 | printf("\n"); |
| 468 | } |
| 469 | #endif |
| 470 | /* sanity checks */ |
| 471 | if (!(srb->cmdlen <= CBWCDBLENGTH)) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 472 | debug("usb_stor_BBB_comdat:cmdlen too large\n"); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 473 | return -1; |
| 474 | } |
| 475 | |
| 476 | /* always OUT to the ep */ |
| 477 | pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out); |
| 478 | |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 479 | cbw->dCBWSignature = cpu_to_le32(CBWSIGNATURE); |
| 480 | cbw->dCBWTag = cpu_to_le32(CBWTag++); |
| 481 | cbw->dCBWDataTransferLength = cpu_to_le32(srb->datalen); |
| 482 | cbw->bCBWFlags = (dir_in ? CBWFLAGS_IN : CBWFLAGS_OUT); |
| 483 | cbw->bCBWLUN = srb->lun; |
| 484 | cbw->bCDBLength = srb->cmdlen; |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 485 | /* copy the command data into the CBW command data buffer */ |
| 486 | /* DST SRC LEN!!! */ |
Sergey Temerkhanov | f657087 | 2015-04-01 17:18:46 +0300 | [diff] [blame] | 487 | |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 488 | memcpy(cbw->CBWCDB, srb->cmd, srb->cmdlen); |
| 489 | result = usb_bulk_msg(us->pusb_dev, pipe, cbw, UMASS_BBB_CBW_SIZE, |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 490 | &actlen, USB_CNTL_TIMEOUT * 5); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 491 | if (result < 0) |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 492 | debug("usb_stor_BBB_comdat:usb_bulk_msg error\n"); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 493 | return result; |
| 494 | } |
| 495 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 496 | /* FIXME: we also need a CBI_command which sets up the completion |
| 497 | * interrupt, and waits for it |
| 498 | */ |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 499 | static int usb_stor_CB_comdat(ccb *srb, struct us_data *us) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 500 | { |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 501 | int result = 0; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 502 | int dir_in, retry; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 503 | unsigned int pipe; |
| 504 | unsigned long status; |
| 505 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 506 | retry = 5; |
| 507 | dir_in = US_DIRECTION(srb->cmd[0]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 508 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 509 | if (dir_in) |
| 510 | pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in); |
| 511 | else |
| 512 | pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out); |
| 513 | |
| 514 | while (retry--) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 515 | debug("CBI gets a command: Try %d\n", 5 - retry); |
| 516 | #ifdef DEBUG |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 517 | usb_show_srb(srb); |
| 518 | #endif |
| 519 | /* let's send the command via the control pipe */ |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 520 | result = usb_control_msg(us->pusb_dev, |
| 521 | usb_sndctrlpipe(us->pusb_dev , 0), |
| 522 | US_CBI_ADSC, |
| 523 | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 524 | 0, us->ifnum, |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 525 | srb->cmd, srb->cmdlen, |
| 526 | USB_CNTL_TIMEOUT * 5); |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 527 | debug("CB_transport: control msg returned %d, status %lX\n", |
| 528 | result, us->pusb_dev->status); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 529 | /* check the return code for the command */ |
| 530 | if (result < 0) { |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 531 | if (us->pusb_dev->status & USB_ST_STALLED) { |
| 532 | status = us->pusb_dev->status; |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 533 | debug(" stall during command found," \ |
| 534 | " clear pipe\n"); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 535 | usb_clear_halt(us->pusb_dev, |
| 536 | usb_sndctrlpipe(us->pusb_dev, 0)); |
| 537 | us->pusb_dev->status = status; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 538 | } |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 539 | debug(" error during command %02X" \ |
| 540 | " Stat = %lX\n", srb->cmd[0], |
| 541 | us->pusb_dev->status); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 542 | return result; |
| 543 | } |
| 544 | /* transfer the data payload for this command, if one exists*/ |
| 545 | |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 546 | debug("CB_transport: control msg returned %d," \ |
| 547 | " direction is %s to go 0x%lx\n", result, |
| 548 | dir_in ? "IN" : "OUT", srb->datalen); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 549 | if (srb->datalen) { |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 550 | result = us_one_transfer(us, pipe, (char *)srb->pdata, |
| 551 | srb->datalen); |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 552 | debug("CBI attempted to transfer data," \ |
| 553 | " result is %d status %lX, len %d\n", |
| 554 | result, us->pusb_dev->status, |
| 555 | us->pusb_dev->act_len); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 556 | if (!(us->pusb_dev->status & USB_ST_NAK_REC)) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 557 | break; |
| 558 | } /* if (srb->datalen) */ |
| 559 | else |
| 560 | break; |
| 561 | } |
| 562 | /* return result */ |
| 563 | |
| 564 | return result; |
| 565 | } |
| 566 | |
| 567 | |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 568 | static int usb_stor_CBI_get_status(ccb *srb, struct us_data *us) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 569 | { |
| 570 | int timeout; |
| 571 | |
wdenk | 80885a9 | 2004-02-26 23:46:20 +0000 | [diff] [blame] | 572 | us->ip_wanted = 1; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 573 | submit_int_msg(us->pusb_dev, us->irqpipe, |
wdenk | 80885a9 | 2004-02-26 23:46:20 +0000 | [diff] [blame] | 574 | (void *) &us->ip_data, us->irqmaxp, us->irqinterval); |
| 575 | timeout = 1000; |
| 576 | while (timeout--) { |
Sergey Temerkhanov | f657087 | 2015-04-01 17:18:46 +0300 | [diff] [blame] | 577 | if (us->ip_wanted == 0) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 578 | break; |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 579 | mdelay(10); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 580 | } |
| 581 | if (us->ip_wanted) { |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 582 | printf(" Did not get interrupt on CBI\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 583 | us->ip_wanted = 0; |
| 584 | return USB_STOR_TRANSPORT_ERROR; |
| 585 | } |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 586 | debug("Got interrupt data 0x%x, transfered %d status 0x%lX\n", |
| 587 | us->ip_data, us->pusb_dev->irq_act_len, |
| 588 | us->pusb_dev->irq_status); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 589 | /* UFI gives us ASC and ASCQ, like a request sense */ |
| 590 | if (us->subclass == US_SC_UFI) { |
| 591 | if (srb->cmd[0] == SCSI_REQ_SENSE || |
| 592 | srb->cmd[0] == SCSI_INQUIRY) |
| 593 | return USB_STOR_TRANSPORT_GOOD; /* Good */ |
wdenk | 80885a9 | 2004-02-26 23:46:20 +0000 | [diff] [blame] | 594 | else if (us->ip_data) |
| 595 | return USB_STOR_TRANSPORT_FAILED; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 596 | else |
wdenk | 80885a9 | 2004-02-26 23:46:20 +0000 | [diff] [blame] | 597 | return USB_STOR_TRANSPORT_GOOD; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 598 | } |
| 599 | /* otherwise, we interpret the data normally */ |
| 600 | switch (us->ip_data) { |
wdenk | 80885a9 | 2004-02-26 23:46:20 +0000 | [diff] [blame] | 601 | case 0x0001: |
| 602 | return USB_STOR_TRANSPORT_GOOD; |
| 603 | case 0x0002: |
| 604 | return USB_STOR_TRANSPORT_FAILED; |
| 605 | default: |
| 606 | return USB_STOR_TRANSPORT_ERROR; |
| 607 | } /* switch */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 608 | return USB_STOR_TRANSPORT_ERROR; |
| 609 | } |
| 610 | |
| 611 | #define USB_TRANSPORT_UNKNOWN_RETRY 5 |
| 612 | #define USB_TRANSPORT_NOT_READY_RETRY 10 |
| 613 | |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 614 | /* clear a stall on an endpoint - special for BBB devices */ |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 615 | static int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt) |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 616 | { |
| 617 | int result; |
| 618 | |
| 619 | /* ENDPOINT_HALT = 0, so set value to 0 */ |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 620 | result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0), |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 621 | USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 622 | 0, endpt, NULL, 0, USB_CNTL_TIMEOUT * 5); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 623 | return result; |
| 624 | } |
| 625 | |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 626 | static int usb_stor_BBB_transport(ccb *srb, struct us_data *us) |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 627 | { |
| 628 | int result, retry; |
| 629 | int dir_in; |
| 630 | int actlen, data_actlen; |
| 631 | unsigned int pipe, pipein, pipeout; |
Simon Glass | 2e17c87 | 2015-03-25 12:22:11 -0600 | [diff] [blame^] | 632 | ALLOC_CACHE_ALIGN_BUFFER(struct umass_bbb_csw, csw, 1); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 633 | #ifdef BBB_XPORT_TRACE |
| 634 | unsigned char *ptr; |
| 635 | int index; |
| 636 | #endif |
| 637 | |
| 638 | dir_in = US_DIRECTION(srb->cmd[0]); |
| 639 | |
| 640 | /* COMMAND phase */ |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 641 | debug("COMMAND phase\n"); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 642 | result = usb_stor_BBB_comdat(srb, us); |
| 643 | if (result < 0) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 644 | debug("failed to send CBW status %ld\n", |
| 645 | us->pusb_dev->status); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 646 | usb_stor_BBB_reset(us); |
| 647 | return USB_STOR_TRANSPORT_FAILED; |
| 648 | } |
Benoît Thébaudeau | 3e8581b | 2012-08-10 18:27:11 +0200 | [diff] [blame] | 649 | if (!(us->flags & USB_READY)) |
| 650 | mdelay(5); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 651 | pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in); |
| 652 | pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out); |
| 653 | /* DATA phase + error handling */ |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 654 | data_actlen = 0; |
| 655 | /* no data, go immediately to the STATUS phase */ |
| 656 | if (srb->datalen == 0) |
| 657 | goto st; |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 658 | debug("DATA phase\n"); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 659 | if (dir_in) |
| 660 | pipe = pipein; |
| 661 | else |
| 662 | pipe = pipeout; |
Sergey Temerkhanov | f657087 | 2015-04-01 17:18:46 +0300 | [diff] [blame] | 663 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 664 | result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen, |
| 665 | &data_actlen, USB_CNTL_TIMEOUT * 5); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 666 | /* special handling of STALL in DATA phase */ |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 667 | if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 668 | debug("DATA:stall\n"); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 669 | /* clear the STALL on the endpoint */ |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 670 | result = usb_stor_BBB_clear_endpt_stall(us, |
| 671 | dir_in ? us->ep_in : us->ep_out); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 672 | if (result >= 0) |
| 673 | /* continue on to STATUS phase */ |
| 674 | goto st; |
| 675 | } |
| 676 | if (result < 0) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 677 | debug("usb_bulk_msg error status %ld\n", |
| 678 | us->pusb_dev->status); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 679 | usb_stor_BBB_reset(us); |
| 680 | return USB_STOR_TRANSPORT_FAILED; |
| 681 | } |
| 682 | #ifdef BBB_XPORT_TRACE |
| 683 | for (index = 0; index < data_actlen; index++) |
| 684 | printf("pdata[%d] %#x ", index, srb->pdata[index]); |
| 685 | printf("\n"); |
| 686 | #endif |
| 687 | /* STATUS phase + error handling */ |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 688 | st: |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 689 | retry = 0; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 690 | again: |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 691 | debug("STATUS phase\n"); |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 692 | result = usb_bulk_msg(us->pusb_dev, pipein, csw, UMASS_BBB_CSW_SIZE, |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 693 | &actlen, USB_CNTL_TIMEOUT*5); |
| 694 | |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 695 | /* special handling of STALL in STATUS phase */ |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 696 | if ((result < 0) && (retry < 1) && |
| 697 | (us->pusb_dev->status & USB_ST_STALLED)) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 698 | debug("STATUS:stall\n"); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 699 | /* clear the STALL on the endpoint */ |
| 700 | result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in); |
| 701 | if (result >= 0 && (retry++ < 1)) |
| 702 | /* do a retry */ |
| 703 | goto again; |
| 704 | } |
| 705 | if (result < 0) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 706 | debug("usb_bulk_msg error status %ld\n", |
| 707 | us->pusb_dev->status); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 708 | usb_stor_BBB_reset(us); |
| 709 | return USB_STOR_TRANSPORT_FAILED; |
| 710 | } |
| 711 | #ifdef BBB_XPORT_TRACE |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 712 | ptr = (unsigned char *)csw; |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 713 | for (index = 0; index < UMASS_BBB_CSW_SIZE; index++) |
| 714 | printf("ptr[%d] %#x ", index, ptr[index]); |
| 715 | printf("\n"); |
| 716 | #endif |
| 717 | /* misuse pipe to get the residue */ |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 718 | pipe = le32_to_cpu(csw->dCSWDataResidue); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 719 | if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0) |
| 720 | pipe = srb->datalen - data_actlen; |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 721 | if (CSWSIGNATURE != le32_to_cpu(csw->dCSWSignature)) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 722 | debug("!CSWSIGNATURE\n"); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 723 | usb_stor_BBB_reset(us); |
| 724 | return USB_STOR_TRANSPORT_FAILED; |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 725 | } else if ((CBWTag - 1) != le32_to_cpu(csw->dCSWTag)) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 726 | debug("!Tag\n"); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 727 | usb_stor_BBB_reset(us); |
| 728 | return USB_STOR_TRANSPORT_FAILED; |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 729 | } else if (csw->bCSWStatus > CSWSTATUS_PHASE) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 730 | debug(">PHASE\n"); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 731 | usb_stor_BBB_reset(us); |
| 732 | return USB_STOR_TRANSPORT_FAILED; |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 733 | } else if (csw->bCSWStatus == CSWSTATUS_PHASE) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 734 | debug("=PHASE\n"); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 735 | usb_stor_BBB_reset(us); |
| 736 | return USB_STOR_TRANSPORT_FAILED; |
| 737 | } else if (data_actlen > srb->datalen) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 738 | debug("transferred %dB instead of %ldB\n", |
| 739 | data_actlen, srb->datalen); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 740 | return USB_STOR_TRANSPORT_FAILED; |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 741 | } else if (csw->bCSWStatus == CSWSTATUS_FAILED) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 742 | debug("FAILED\n"); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 743 | return USB_STOR_TRANSPORT_FAILED; |
| 744 | } |
| 745 | |
| 746 | return result; |
| 747 | } |
| 748 | |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 749 | static int usb_stor_CB_transport(ccb *srb, struct us_data *us) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 750 | { |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 751 | int result, status; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 752 | ccb *psrb; |
| 753 | ccb reqsrb; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 754 | int retry, notready; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 755 | |
Wolfgang Denk | d0ff51b | 2008-07-14 15:19:07 +0200 | [diff] [blame] | 756 | psrb = &reqsrb; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 757 | status = USB_STOR_TRANSPORT_GOOD; |
| 758 | retry = 0; |
| 759 | notready = 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 760 | /* issue the command */ |
| 761 | do_retry: |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 762 | result = usb_stor_CB_comdat(srb, us); |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 763 | debug("command / Data returned %d, status %lX\n", |
| 764 | result, us->pusb_dev->status); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 765 | /* if this is an CBI Protocol, get IRQ */ |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 766 | if (us->protocol == US_PR_CBI) { |
| 767 | status = usb_stor_CBI_get_status(srb, us); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 768 | /* if the status is error, report it */ |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 769 | if (status == USB_STOR_TRANSPORT_ERROR) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 770 | debug(" USB CBI Command Error\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 771 | return status; |
| 772 | } |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 773 | srb->sense_buf[12] = (unsigned char)(us->ip_data >> 8); |
| 774 | srb->sense_buf[13] = (unsigned char)(us->ip_data & 0xff); |
| 775 | if (!us->ip_data) { |
| 776 | /* if the status is good, report it */ |
| 777 | if (status == USB_STOR_TRANSPORT_GOOD) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 778 | debug(" USB CBI Command Good\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 779 | return status; |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | /* do we have to issue an auto request? */ |
| 784 | /* HERE we have to check the result */ |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 785 | if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 786 | debug("ERROR %lX\n", us->pusb_dev->status); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 787 | us->transport_reset(us); |
| 788 | return USB_STOR_TRANSPORT_ERROR; |
| 789 | } |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 790 | if ((us->protocol == US_PR_CBI) && |
| 791 | ((srb->cmd[0] == SCSI_REQ_SENSE) || |
| 792 | (srb->cmd[0] == SCSI_INQUIRY))) { |
| 793 | /* do not issue an autorequest after request sense */ |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 794 | debug("No auto request and good\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 795 | return USB_STOR_TRANSPORT_GOOD; |
| 796 | } |
| 797 | /* issue an request_sense */ |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 798 | memset(&psrb->cmd[0], 0, 12); |
| 799 | psrb->cmd[0] = SCSI_REQ_SENSE; |
| 800 | psrb->cmd[1] = srb->lun << 5; |
| 801 | psrb->cmd[4] = 18; |
| 802 | psrb->datalen = 18; |
Wolfgang Denk | d0ff51b | 2008-07-14 15:19:07 +0200 | [diff] [blame] | 803 | psrb->pdata = &srb->sense_buf[0]; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 804 | psrb->cmdlen = 12; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 805 | /* issue the command */ |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 806 | result = usb_stor_CB_comdat(psrb, us); |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 807 | debug("auto request returned %d\n", result); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 808 | /* if this is an CBI Protocol, get IRQ */ |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 809 | if (us->protocol == US_PR_CBI) |
| 810 | status = usb_stor_CBI_get_status(psrb, us); |
| 811 | |
| 812 | if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 813 | debug(" AUTO REQUEST ERROR %ld\n", |
| 814 | us->pusb_dev->status); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 815 | return USB_STOR_TRANSPORT_ERROR; |
| 816 | } |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 817 | debug("autorequest returned 0x%02X 0x%02X 0x%02X 0x%02X\n", |
| 818 | srb->sense_buf[0], srb->sense_buf[2], |
| 819 | srb->sense_buf[12], srb->sense_buf[13]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 820 | /* Check the auto request result */ |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 821 | if ((srb->sense_buf[2] == 0) && |
| 822 | (srb->sense_buf[12] == 0) && |
| 823 | (srb->sense_buf[13] == 0)) { |
| 824 | /* ok, no sense */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 825 | return USB_STOR_TRANSPORT_GOOD; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 826 | } |
| 827 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 828 | /* Check the auto request result */ |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 829 | switch (srb->sense_buf[2]) { |
| 830 | case 0x01: |
| 831 | /* Recovered Error */ |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 832 | return USB_STOR_TRANSPORT_GOOD; |
wdenk | 80885a9 | 2004-02-26 23:46:20 +0000 | [diff] [blame] | 833 | break; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 834 | case 0x02: |
| 835 | /* Not Ready */ |
| 836 | if (notready++ > USB_TRANSPORT_NOT_READY_RETRY) { |
| 837 | printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X" |
| 838 | " 0x%02X (NOT READY)\n", srb->cmd[0], |
| 839 | srb->sense_buf[0], srb->sense_buf[2], |
| 840 | srb->sense_buf[12], srb->sense_buf[13]); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 841 | return USB_STOR_TRANSPORT_FAILED; |
| 842 | } else { |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 843 | mdelay(100); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 844 | goto do_retry; |
| 845 | } |
| 846 | break; |
| 847 | default: |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 848 | if (retry++ > USB_TRANSPORT_UNKNOWN_RETRY) { |
| 849 | printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X" |
| 850 | " 0x%02X\n", srb->cmd[0], srb->sense_buf[0], |
| 851 | srb->sense_buf[2], srb->sense_buf[12], |
| 852 | srb->sense_buf[13]); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 853 | return USB_STOR_TRANSPORT_FAILED; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 854 | } else |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 855 | goto do_retry; |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 856 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 857 | } |
| 858 | return USB_STOR_TRANSPORT_FAILED; |
| 859 | } |
| 860 | |
| 861 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 862 | static int usb_inquiry(ccb *srb, struct us_data *ss) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 863 | { |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 864 | int retry, i; |
| 865 | retry = 5; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 866 | do { |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 867 | memset(&srb->cmd[0], 0, 12); |
| 868 | srb->cmd[0] = SCSI_INQUIRY; |
Ludovic Courtès | 99e9ed1 | 2010-10-05 22:04:26 +0200 | [diff] [blame] | 869 | srb->cmd[1] = srb->lun << 5; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 870 | srb->cmd[4] = 36; |
| 871 | srb->datalen = 36; |
| 872 | srb->cmdlen = 12; |
| 873 | i = ss->transport(srb, ss); |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 874 | debug("inquiry returns %d\n", i); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 875 | if (i == 0) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 876 | break; |
Kim B. Heino | fac71cc | 2010-03-12 10:07:00 +0200 | [diff] [blame] | 877 | } while (--retry); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 878 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 879 | if (!retry) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 880 | printf("error in inquiry\n"); |
| 881 | return -1; |
| 882 | } |
| 883 | return 0; |
| 884 | } |
| 885 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 886 | static int usb_request_sense(ccb *srb, struct us_data *ss) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 887 | { |
| 888 | char *ptr; |
wdenk | 80885a9 | 2004-02-26 23:46:20 +0000 | [diff] [blame] | 889 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 890 | ptr = (char *)srb->pdata; |
| 891 | memset(&srb->cmd[0], 0, 12); |
| 892 | srb->cmd[0] = SCSI_REQ_SENSE; |
Ludovic Courtès | 99e9ed1 | 2010-10-05 22:04:26 +0200 | [diff] [blame] | 893 | srb->cmd[1] = srb->lun << 5; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 894 | srb->cmd[4] = 18; |
| 895 | srb->datalen = 18; |
Wolfgang Denk | d0ff51b | 2008-07-14 15:19:07 +0200 | [diff] [blame] | 896 | srb->pdata = &srb->sense_buf[0]; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 897 | srb->cmdlen = 12; |
| 898 | ss->transport(srb, ss); |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 899 | debug("Request Sense returned %02X %02X %02X\n", |
| 900 | srb->sense_buf[2], srb->sense_buf[12], |
| 901 | srb->sense_buf[13]); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 902 | srb->pdata = (uchar *)ptr; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 903 | return 0; |
| 904 | } |
| 905 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 906 | static int usb_test_unit_ready(ccb *srb, struct us_data *ss) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 907 | { |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 908 | int retries = 10; |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 909 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 910 | do { |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 911 | memset(&srb->cmd[0], 0, 12); |
| 912 | srb->cmd[0] = SCSI_TST_U_RDY; |
Ludovic Courtès | 99e9ed1 | 2010-10-05 22:04:26 +0200 | [diff] [blame] | 913 | srb->cmd[1] = srb->lun << 5; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 914 | srb->datalen = 0; |
| 915 | srb->cmdlen = 12; |
Benoît Thébaudeau | 3e8581b | 2012-08-10 18:27:11 +0200 | [diff] [blame] | 916 | if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD) { |
| 917 | ss->flags |= USB_READY; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 918 | return 0; |
Benoît Thébaudeau | 3e8581b | 2012-08-10 18:27:11 +0200 | [diff] [blame] | 919 | } |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 920 | usb_request_sense(srb, ss); |
Vincent Palatin | 8b57e2f | 2012-12-12 17:55:29 -0800 | [diff] [blame] | 921 | /* |
| 922 | * Check the Key Code Qualifier, if it matches |
| 923 | * "Not Ready - medium not present" |
| 924 | * (the sense Key equals 0x2 and the ASC is 0x3a) |
| 925 | * return immediately as the medium being absent won't change |
| 926 | * unless there is a user action. |
| 927 | */ |
| 928 | if ((srb->sense_buf[2] == 0x02) && |
| 929 | (srb->sense_buf[12] == 0x3a)) |
| 930 | return -1; |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 931 | mdelay(100); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 932 | } while (retries--); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 933 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 934 | return -1; |
| 935 | } |
| 936 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 937 | static int usb_read_capacity(ccb *srb, struct us_data *ss) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 938 | { |
| 939 | int retry; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 940 | /* XXX retries */ |
| 941 | retry = 3; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 942 | do { |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 943 | memset(&srb->cmd[0], 0, 12); |
| 944 | srb->cmd[0] = SCSI_RD_CAPAC; |
Ludovic Courtès | 99e9ed1 | 2010-10-05 22:04:26 +0200 | [diff] [blame] | 945 | srb->cmd[1] = srb->lun << 5; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 946 | srb->datalen = 8; |
| 947 | srb->cmdlen = 12; |
| 948 | if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 949 | return 0; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 950 | } while (retry--); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 951 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 952 | return -1; |
| 953 | } |
| 954 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 955 | static int usb_read_10(ccb *srb, struct us_data *ss, unsigned long start, |
| 956 | unsigned short blocks) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 957 | { |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 958 | memset(&srb->cmd[0], 0, 12); |
| 959 | srb->cmd[0] = SCSI_READ10; |
Ludovic Courtès | 99e9ed1 | 2010-10-05 22:04:26 +0200 | [diff] [blame] | 960 | srb->cmd[1] = srb->lun << 5; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 961 | srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff; |
| 962 | srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff; |
| 963 | srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff; |
| 964 | srb->cmd[5] = ((unsigned char) (start)) & 0xff; |
| 965 | srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff; |
| 966 | srb->cmd[8] = (unsigned char) blocks & 0xff; |
| 967 | srb->cmdlen = 12; |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 968 | debug("read10: start %lx blocks %x\n", start, blocks); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 969 | return ss->transport(srb, ss); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Mahavir Jain | 127e108 | 2009-11-03 12:22:10 +0530 | [diff] [blame] | 972 | static int usb_write_10(ccb *srb, struct us_data *ss, unsigned long start, |
| 973 | unsigned short blocks) |
| 974 | { |
| 975 | memset(&srb->cmd[0], 0, 12); |
| 976 | srb->cmd[0] = SCSI_WRITE10; |
Ludovic Courtès | 99e9ed1 | 2010-10-05 22:04:26 +0200 | [diff] [blame] | 977 | srb->cmd[1] = srb->lun << 5; |
Mahavir Jain | 127e108 | 2009-11-03 12:22:10 +0530 | [diff] [blame] | 978 | srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff; |
| 979 | srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff; |
| 980 | srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff; |
| 981 | srb->cmd[5] = ((unsigned char) (start)) & 0xff; |
| 982 | srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff; |
| 983 | srb->cmd[8] = (unsigned char) blocks & 0xff; |
| 984 | srb->cmdlen = 12; |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 985 | debug("write10: start %lx blocks %x\n", start, blocks); |
Mahavir Jain | 127e108 | 2009-11-03 12:22:10 +0530 | [diff] [blame] | 986 | return ss->transport(srb, ss); |
| 987 | } |
| 988 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 989 | |
Bartlomiej Sieka | ddde6b7 | 2006-08-22 10:38:18 +0200 | [diff] [blame] | 990 | #ifdef CONFIG_USB_BIN_FIXUP |
| 991 | /* |
| 992 | * Some USB storage devices queried for SCSI identification data respond with |
| 993 | * binary strings, which if output to the console freeze the terminal. The |
| 994 | * workaround is to modify the vendor and product strings read from such |
| 995 | * device with proper values (as reported by 'usb info'). |
| 996 | * |
| 997 | * Vendor and product length limits are taken from the definition of |
| 998 | * block_dev_desc_t in include/part.h. |
| 999 | */ |
| 1000 | static void usb_bin_fixup(struct usb_device_descriptor descriptor, |
| 1001 | unsigned char vendor[], |
| 1002 | unsigned char product[]) { |
| 1003 | const unsigned char max_vendor_len = 40; |
| 1004 | const unsigned char max_product_len = 20; |
| 1005 | if (descriptor.idVendor == 0x0424 && descriptor.idProduct == 0x223a) { |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1006 | strncpy((char *)vendor, "SMSC", max_vendor_len); |
| 1007 | strncpy((char *)product, "Flash Media Cntrller", |
| 1008 | max_product_len); |
Bartlomiej Sieka | ddde6b7 | 2006-08-22 10:38:18 +0200 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | #endif /* CONFIG_USB_BIN_FIXUP */ |
| 1012 | |
Sascha Silbe | ff8fef5 | 2013-06-14 13:07:25 +0200 | [diff] [blame] | 1013 | unsigned long usb_stor_read(int device, lbaint_t blknr, |
Gabe Black | e81e79e | 2012-10-12 14:26:07 +0000 | [diff] [blame] | 1014 | lbaint_t blkcnt, void *buffer) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1015 | { |
Gabe Black | e81e79e | 2012-10-12 14:26:07 +0000 | [diff] [blame] | 1016 | lbaint_t start, blks; |
| 1017 | uintptr_t buf_addr; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1018 | unsigned short smallblks; |
| 1019 | struct usb_device *dev; |
Kyle Moffett | 5dd95cf | 2011-12-21 07:08:12 +0000 | [diff] [blame] | 1020 | struct us_data *ss; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1021 | int retry, i; |
wdenk | f8d813e | 2004-03-02 14:05:39 +0000 | [diff] [blame] | 1022 | ccb *srb = &usb_ccb; |
| 1023 | |
| 1024 | if (blkcnt == 0) |
| 1025 | return 0; |
| 1026 | |
| 1027 | device &= 0xff; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1028 | /* Setup device */ |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1029 | debug("\nusb_read: dev %d \n", device); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1030 | dev = NULL; |
| 1031 | for (i = 0; i < USB_MAX_DEVICE; i++) { |
| 1032 | dev = usb_get_dev_index(i); |
| 1033 | if (dev == NULL) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1034 | return 0; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1035 | if (dev->devnum == usb_dev_desc[device].target) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1036 | break; |
| 1037 | } |
Kyle Moffett | 5dd95cf | 2011-12-21 07:08:12 +0000 | [diff] [blame] | 1038 | ss = (struct us_data *)dev->privptr; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1039 | |
| 1040 | usb_disable_asynch(1); /* asynch transfer not allowed */ |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1041 | srb->lun = usb_dev_desc[device].lun; |
Sergey Temerkhanov | f657087 | 2015-04-01 17:18:46 +0300 | [diff] [blame] | 1042 | buf_addr = (uintptr_t)buffer; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1043 | start = blknr; |
| 1044 | blks = blkcnt; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1045 | |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1046 | debug("\nusb_read: dev %d startblk " LBAF ", blccnt " LBAF |
Simon Glass | 4fd074d | 2014-10-15 04:38:38 -0600 | [diff] [blame] | 1047 | " buffer %" PRIxPTR "\n", device, start, blks, buf_addr); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1048 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1049 | do { |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1050 | /* XXX need some comment here */ |
| 1051 | retry = 2; |
| 1052 | srb->pdata = (unsigned char *)buf_addr; |
Benoît Thébaudeau | 4bee5c8 | 2012-08-10 18:23:25 +0200 | [diff] [blame] | 1053 | if (blks > USB_MAX_XFER_BLK) |
| 1054 | smallblks = USB_MAX_XFER_BLK; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1055 | else |
| 1056 | smallblks = (unsigned short) blks; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1057 | retry_it: |
Benoît Thébaudeau | 4bee5c8 | 2012-08-10 18:23:25 +0200 | [diff] [blame] | 1058 | if (smallblks == USB_MAX_XFER_BLK) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1059 | usb_show_progress(); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1060 | srb->datalen = usb_dev_desc[device].blksz * smallblks; |
| 1061 | srb->pdata = (unsigned char *)buf_addr; |
Kyle Moffett | 5dd95cf | 2011-12-21 07:08:12 +0000 | [diff] [blame] | 1062 | if (usb_read_10(srb, ss, start, smallblks)) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1063 | debug("Read ERROR\n"); |
Kyle Moffett | 5dd95cf | 2011-12-21 07:08:12 +0000 | [diff] [blame] | 1064 | usb_request_sense(srb, ss); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1065 | if (retry--) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1066 | goto retry_it; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1067 | blkcnt -= blks; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1068 | break; |
| 1069 | } |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1070 | start += smallblks; |
| 1071 | blks -= smallblks; |
| 1072 | buf_addr += srb->datalen; |
| 1073 | } while (blks != 0); |
Benoît Thébaudeau | 3e8581b | 2012-08-10 18:27:11 +0200 | [diff] [blame] | 1074 | ss->flags &= ~USB_READY; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1075 | |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1076 | debug("usb_read: end startblk " LBAF |
Simon Glass | 4fd074d | 2014-10-15 04:38:38 -0600 | [diff] [blame] | 1077 | ", blccnt %x buffer %" PRIxPTR "\n", |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1078 | start, smallblks, buf_addr); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1079 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1080 | usb_disable_asynch(0); /* asynch transfer allowed */ |
Benoît Thébaudeau | 4bee5c8 | 2012-08-10 18:23:25 +0200 | [diff] [blame] | 1081 | if (blkcnt >= USB_MAX_XFER_BLK) |
Wolfgang Denk | 226fa9b | 2010-07-19 11:36:59 +0200 | [diff] [blame] | 1082 | debug("\n"); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1083 | return blkcnt; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Sascha Silbe | ff8fef5 | 2013-06-14 13:07:25 +0200 | [diff] [blame] | 1086 | unsigned long usb_stor_write(int device, lbaint_t blknr, |
Gabe Black | e81e79e | 2012-10-12 14:26:07 +0000 | [diff] [blame] | 1087 | lbaint_t blkcnt, const void *buffer) |
Mahavir Jain | 127e108 | 2009-11-03 12:22:10 +0530 | [diff] [blame] | 1088 | { |
Gabe Black | e81e79e | 2012-10-12 14:26:07 +0000 | [diff] [blame] | 1089 | lbaint_t start, blks; |
| 1090 | uintptr_t buf_addr; |
Mahavir Jain | 127e108 | 2009-11-03 12:22:10 +0530 | [diff] [blame] | 1091 | unsigned short smallblks; |
| 1092 | struct usb_device *dev; |
Kyle Moffett | 5dd95cf | 2011-12-21 07:08:12 +0000 | [diff] [blame] | 1093 | struct us_data *ss; |
Mahavir Jain | 127e108 | 2009-11-03 12:22:10 +0530 | [diff] [blame] | 1094 | int retry, i; |
| 1095 | ccb *srb = &usb_ccb; |
| 1096 | |
| 1097 | if (blkcnt == 0) |
| 1098 | return 0; |
| 1099 | |
| 1100 | device &= 0xff; |
| 1101 | /* Setup device */ |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1102 | debug("\nusb_write: dev %d \n", device); |
Mahavir Jain | 127e108 | 2009-11-03 12:22:10 +0530 | [diff] [blame] | 1103 | dev = NULL; |
| 1104 | for (i = 0; i < USB_MAX_DEVICE; i++) { |
| 1105 | dev = usb_get_dev_index(i); |
| 1106 | if (dev == NULL) |
| 1107 | return 0; |
| 1108 | if (dev->devnum == usb_dev_desc[device].target) |
| 1109 | break; |
| 1110 | } |
Kyle Moffett | 5dd95cf | 2011-12-21 07:08:12 +0000 | [diff] [blame] | 1111 | ss = (struct us_data *)dev->privptr; |
Mahavir Jain | 127e108 | 2009-11-03 12:22:10 +0530 | [diff] [blame] | 1112 | |
| 1113 | usb_disable_asynch(1); /* asynch transfer not allowed */ |
| 1114 | |
| 1115 | srb->lun = usb_dev_desc[device].lun; |
Sergey Temerkhanov | f657087 | 2015-04-01 17:18:46 +0300 | [diff] [blame] | 1116 | buf_addr = (uintptr_t)buffer; |
Mahavir Jain | 127e108 | 2009-11-03 12:22:10 +0530 | [diff] [blame] | 1117 | start = blknr; |
| 1118 | blks = blkcnt; |
Mahavir Jain | 127e108 | 2009-11-03 12:22:10 +0530 | [diff] [blame] | 1119 | |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1120 | debug("\nusb_write: dev %d startblk " LBAF ", blccnt " LBAF |
Simon Glass | 4fd074d | 2014-10-15 04:38:38 -0600 | [diff] [blame] | 1121 | " buffer %" PRIxPTR "\n", device, start, blks, buf_addr); |
Mahavir Jain | 127e108 | 2009-11-03 12:22:10 +0530 | [diff] [blame] | 1122 | |
| 1123 | do { |
| 1124 | /* If write fails retry for max retry count else |
| 1125 | * return with number of blocks written successfully. |
| 1126 | */ |
| 1127 | retry = 2; |
| 1128 | srb->pdata = (unsigned char *)buf_addr; |
Benoît Thébaudeau | 4bee5c8 | 2012-08-10 18:23:25 +0200 | [diff] [blame] | 1129 | if (blks > USB_MAX_XFER_BLK) |
| 1130 | smallblks = USB_MAX_XFER_BLK; |
Mahavir Jain | 127e108 | 2009-11-03 12:22:10 +0530 | [diff] [blame] | 1131 | else |
| 1132 | smallblks = (unsigned short) blks; |
| 1133 | retry_it: |
Benoît Thébaudeau | 4bee5c8 | 2012-08-10 18:23:25 +0200 | [diff] [blame] | 1134 | if (smallblks == USB_MAX_XFER_BLK) |
Mahavir Jain | 127e108 | 2009-11-03 12:22:10 +0530 | [diff] [blame] | 1135 | usb_show_progress(); |
| 1136 | srb->datalen = usb_dev_desc[device].blksz * smallblks; |
| 1137 | srb->pdata = (unsigned char *)buf_addr; |
Kyle Moffett | 5dd95cf | 2011-12-21 07:08:12 +0000 | [diff] [blame] | 1138 | if (usb_write_10(srb, ss, start, smallblks)) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1139 | debug("Write ERROR\n"); |
Kyle Moffett | 5dd95cf | 2011-12-21 07:08:12 +0000 | [diff] [blame] | 1140 | usb_request_sense(srb, ss); |
Mahavir Jain | 127e108 | 2009-11-03 12:22:10 +0530 | [diff] [blame] | 1141 | if (retry--) |
| 1142 | goto retry_it; |
| 1143 | blkcnt -= blks; |
| 1144 | break; |
| 1145 | } |
| 1146 | start += smallblks; |
| 1147 | blks -= smallblks; |
| 1148 | buf_addr += srb->datalen; |
| 1149 | } while (blks != 0); |
Benoît Thébaudeau | 3e8581b | 2012-08-10 18:27:11 +0200 | [diff] [blame] | 1150 | ss->flags &= ~USB_READY; |
Mahavir Jain | 127e108 | 2009-11-03 12:22:10 +0530 | [diff] [blame] | 1151 | |
Simon Glass | 4fd074d | 2014-10-15 04:38:38 -0600 | [diff] [blame] | 1152 | debug("usb_write: end startblk " LBAF ", blccnt %x buffer %" |
| 1153 | PRIxPTR "\n", start, smallblks, buf_addr); |
Mahavir Jain | 127e108 | 2009-11-03 12:22:10 +0530 | [diff] [blame] | 1154 | |
| 1155 | usb_disable_asynch(0); /* asynch transfer allowed */ |
Benoît Thébaudeau | 4bee5c8 | 2012-08-10 18:23:25 +0200 | [diff] [blame] | 1156 | if (blkcnt >= USB_MAX_XFER_BLK) |
Wolfgang Denk | 226fa9b | 2010-07-19 11:36:59 +0200 | [diff] [blame] | 1157 | debug("\n"); |
Mahavir Jain | 127e108 | 2009-11-03 12:22:10 +0530 | [diff] [blame] | 1158 | return blkcnt; |
| 1159 | |
| 1160 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1161 | |
| 1162 | /* Probe to see if a new device is actually a Storage device */ |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1163 | int usb_storage_probe(struct usb_device *dev, unsigned int ifnum, |
| 1164 | struct us_data *ss) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1165 | { |
Tom Rix | 8f8bd56 | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 1166 | struct usb_interface *iface; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1167 | int i; |
Vivek Gautam | 605bd75 | 2013-04-12 16:34:34 +0530 | [diff] [blame] | 1168 | struct usb_endpoint_descriptor *ep_desc; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1169 | unsigned int flags = 0; |
| 1170 | |
| 1171 | int protocol = 0; |
| 1172 | int subclass = 0; |
| 1173 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1174 | /* let's examine the device now */ |
| 1175 | iface = &dev->config.if_desc[ifnum]; |
| 1176 | |
| 1177 | #if 0 |
| 1178 | /* this is the place to patch some storage devices */ |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1179 | debug("iVendor %X iProduct %X\n", dev->descriptor.idVendor, |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1180 | dev->descriptor.idProduct); |
| 1181 | |
| 1182 | if ((dev->descriptor.idVendor) == 0x066b && |
| 1183 | (dev->descriptor.idProduct) == 0x0103) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1184 | debug("patched for E-USB\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1185 | protocol = US_PR_CB; |
| 1186 | subclass = US_SC_UFI; /* an assumption */ |
| 1187 | } |
| 1188 | #endif |
| 1189 | |
| 1190 | if (dev->descriptor.bDeviceClass != 0 || |
Tom Rix | 8f8bd56 | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 1191 | iface->desc.bInterfaceClass != USB_CLASS_MASS_STORAGE || |
| 1192 | iface->desc.bInterfaceSubClass < US_SC_MIN || |
| 1193 | iface->desc.bInterfaceSubClass > US_SC_MAX) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1194 | /* if it's not a mass storage, we go no further */ |
| 1195 | return 0; |
| 1196 | } |
| 1197 | |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 1198 | memset(ss, 0, sizeof(struct us_data)); |
| 1199 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1200 | /* At this point, we know we've got a live one */ |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1201 | debug("\n\nUSB Mass Storage device detected\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1202 | |
| 1203 | /* Initialize the us_data structure with some useful info */ |
| 1204 | ss->flags = flags; |
| 1205 | ss->ifnum = ifnum; |
| 1206 | ss->pusb_dev = dev; |
| 1207 | ss->attention_done = 0; |
| 1208 | |
| 1209 | /* If the device has subclass and protocol, then use that. Otherwise, |
| 1210 | * take data from the specific interface. |
| 1211 | */ |
| 1212 | if (subclass) { |
| 1213 | ss->subclass = subclass; |
| 1214 | ss->protocol = protocol; |
| 1215 | } else { |
Tom Rix | 8f8bd56 | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 1216 | ss->subclass = iface->desc.bInterfaceSubClass; |
| 1217 | ss->protocol = iface->desc.bInterfaceProtocol; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | /* set the handler pointers based on the protocol */ |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1221 | debug("Transport: "); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1222 | switch (ss->protocol) { |
| 1223 | case US_PR_CB: |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1224 | debug("Control/Bulk\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1225 | ss->transport = usb_stor_CB_transport; |
| 1226 | ss->transport_reset = usb_stor_CB_reset; |
| 1227 | break; |
| 1228 | |
| 1229 | case US_PR_CBI: |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1230 | debug("Control/Bulk/Interrupt\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1231 | ss->transport = usb_stor_CB_transport; |
| 1232 | ss->transport_reset = usb_stor_CB_reset; |
| 1233 | break; |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 1234 | case US_PR_BULK: |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1235 | debug("Bulk/Bulk/Bulk\n"); |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 1236 | ss->transport = usb_stor_BBB_transport; |
| 1237 | ss->transport_reset = usb_stor_BBB_reset; |
| 1238 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1239 | default: |
wdenk | 80885a9 | 2004-02-26 23:46:20 +0000 | [diff] [blame] | 1240 | printf("USB Storage Transport unknown / not yet implemented\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1241 | return 0; |
| 1242 | break; |
| 1243 | } |
| 1244 | |
| 1245 | /* |
| 1246 | * We are expecting a minimum of 2 endpoints - in and out (bulk). |
| 1247 | * An optional interrupt is OK (necessary for CBI protocol). |
| 1248 | * We will ignore any others. |
| 1249 | */ |
Tom Rix | 8f8bd56 | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 1250 | for (i = 0; i < iface->desc.bNumEndpoints; i++) { |
Vivek Gautam | 605bd75 | 2013-04-12 16:34:34 +0530 | [diff] [blame] | 1251 | ep_desc = &iface->ep_desc[i]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1252 | /* is it an BULK endpoint? */ |
Vivek Gautam | 605bd75 | 2013-04-12 16:34:34 +0530 | [diff] [blame] | 1253 | if ((ep_desc->bmAttributes & |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1254 | USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) { |
Vivek Gautam | 605bd75 | 2013-04-12 16:34:34 +0530 | [diff] [blame] | 1255 | if (ep_desc->bEndpointAddress & USB_DIR_IN) |
| 1256 | ss->ep_in = ep_desc->bEndpointAddress & |
| 1257 | USB_ENDPOINT_NUMBER_MASK; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1258 | else |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1259 | ss->ep_out = |
Vivek Gautam | 605bd75 | 2013-04-12 16:34:34 +0530 | [diff] [blame] | 1260 | ep_desc->bEndpointAddress & |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1261 | USB_ENDPOINT_NUMBER_MASK; |
| 1262 | } |
| 1263 | |
| 1264 | /* is it an interrupt endpoint? */ |
Vivek Gautam | 605bd75 | 2013-04-12 16:34:34 +0530 | [diff] [blame] | 1265 | if ((ep_desc->bmAttributes & |
| 1266 | USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) { |
| 1267 | ss->ep_int = ep_desc->bEndpointAddress & |
| 1268 | USB_ENDPOINT_NUMBER_MASK; |
| 1269 | ss->irqinterval = ep_desc->bInterval; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1270 | } |
| 1271 | } |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1272 | debug("Endpoints In %d Out %d Int %d\n", |
| 1273 | ss->ep_in, ss->ep_out, ss->ep_int); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1274 | |
| 1275 | /* Do some basic sanity checks, and bail if we find a problem */ |
Tom Rix | 8f8bd56 | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 1276 | if (usb_set_interface(dev, iface->desc.bInterfaceNumber, 0) || |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1277 | !ss->ep_in || !ss->ep_out || |
| 1278 | (ss->protocol == US_PR_CBI && ss->ep_int == 0)) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1279 | debug("Problems with device\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1280 | return 0; |
| 1281 | } |
| 1282 | /* set class specific stuff */ |
wdenk | 149dded | 2003-09-10 18:20:28 +0000 | [diff] [blame] | 1283 | /* We only handle certain protocols. Currently, these are |
| 1284 | * the only ones. |
wdenk | 80885a9 | 2004-02-26 23:46:20 +0000 | [diff] [blame] | 1285 | * The SFF8070 accepts the requests used in u-boot |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1286 | */ |
wdenk | 80885a9 | 2004-02-26 23:46:20 +0000 | [diff] [blame] | 1287 | if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI && |
| 1288 | ss->subclass != US_SC_8070) { |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1289 | printf("Sorry, protocol %d not yet supported.\n", ss->subclass); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1290 | return 0; |
| 1291 | } |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1292 | if (ss->ep_int) { |
| 1293 | /* we had found an interrupt endpoint, prepare irq pipe |
| 1294 | * set up the IRQ pipe and handler |
| 1295 | */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1296 | ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255; |
| 1297 | ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int); |
| 1298 | ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1299 | dev->irq_handle = usb_stor_irq; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1300 | } |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1301 | dev->privptr = (void *)ss; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1302 | return 1; |
| 1303 | } |
| 1304 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1305 | int usb_stor_get_info(struct usb_device *dev, struct us_data *ss, |
| 1306 | block_dev_desc_t *dev_desc) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1307 | { |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1308 | unsigned char perq, modi; |
Sergey Temerkhanov | f657087 | 2015-04-01 17:18:46 +0300 | [diff] [blame] | 1309 | ALLOC_CACHE_ALIGN_BUFFER(u32, cap, 2); |
| 1310 | ALLOC_CACHE_ALIGN_BUFFER(u8, usb_stor_buf, 36); |
| 1311 | u32 capacity, blksz; |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 1312 | ccb *pccb = &usb_ccb; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1313 | |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 1314 | pccb->pdata = usb_stor_buf; |
| 1315 | |
| 1316 | dev_desc->target = dev->devnum; |
| 1317 | pccb->lun = dev_desc->lun; |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1318 | debug(" address %d\n", dev_desc->target); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1319 | |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1320 | if (usb_inquiry(pccb, ss)) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1321 | return -1; |
Wolfgang Denk | 095b8a3 | 2005-08-02 17:06:17 +0200 | [diff] [blame] | 1322 | |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 1323 | perq = usb_stor_buf[0]; |
| 1324 | modi = usb_stor_buf[1]; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1325 | |
Soeren Moch | 6a559bb | 2014-11-08 07:02:14 +0100 | [diff] [blame] | 1326 | /* |
| 1327 | * Skip unknown devices (0x1f) and enclosure service devices (0x0d), |
| 1328 | * they would not respond to test_unit_ready . |
| 1329 | */ |
| 1330 | if (((perq & 0x1f) == 0x1f) || ((perq & 0x1f) == 0x0d)) { |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1331 | return 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1332 | } |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1333 | if ((modi&0x80) == 0x80) { |
| 1334 | /* drive is removable */ |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 1335 | dev_desc->removable = 1; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1336 | } |
Sergey Temerkhanov | f657087 | 2015-04-01 17:18:46 +0300 | [diff] [blame] | 1337 | memcpy(dev_desc->vendor, (const void *)&usb_stor_buf[8], 8); |
| 1338 | memcpy(dev_desc->product, (const void *)&usb_stor_buf[16], 16); |
| 1339 | memcpy(dev_desc->revision, (const void *)&usb_stor_buf[32], 4); |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 1340 | dev_desc->vendor[8] = 0; |
| 1341 | dev_desc->product[16] = 0; |
| 1342 | dev_desc->revision[4] = 0; |
Bartlomiej Sieka | ddde6b7 | 2006-08-22 10:38:18 +0200 | [diff] [blame] | 1343 | #ifdef CONFIG_USB_BIN_FIXUP |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1344 | usb_bin_fixup(dev->descriptor, (uchar *)dev_desc->vendor, |
| 1345 | (uchar *)dev_desc->product); |
Bartlomiej Sieka | ddde6b7 | 2006-08-22 10:38:18 +0200 | [diff] [blame] | 1346 | #endif /* CONFIG_USB_BIN_FIXUP */ |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1347 | debug("ISO Vers %X, Response Data %X\n", usb_stor_buf[2], |
| 1348 | usb_stor_buf[3]); |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1349 | if (usb_test_unit_ready(pccb, ss)) { |
| 1350 | printf("Device NOT ready\n" |
| 1351 | " Request Sense returned %02X %02X %02X\n", |
| 1352 | pccb->sense_buf[2], pccb->sense_buf[12], |
| 1353 | pccb->sense_buf[13]); |
| 1354 | if (dev_desc->removable == 1) { |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 1355 | dev_desc->type = perq; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1356 | return 1; |
| 1357 | } |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1358 | return 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1359 | } |
Sergey Temerkhanov | f657087 | 2015-04-01 17:18:46 +0300 | [diff] [blame] | 1360 | pccb->pdata = (unsigned char *)cap; |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1361 | memset(pccb->pdata, 0, 8); |
| 1362 | if (usb_read_capacity(pccb, ss) != 0) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1363 | printf("READ_CAP ERROR\n"); |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 1364 | cap[0] = 2880; |
| 1365 | cap[1] = 0x200; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1366 | } |
Benoît Thébaudeau | 3e8581b | 2012-08-10 18:27:11 +0200 | [diff] [blame] | 1367 | ss->flags &= ~USB_READY; |
Sergey Temerkhanov | f657087 | 2015-04-01 17:18:46 +0300 | [diff] [blame] | 1368 | debug("Read Capacity returns: 0x%08x, 0x%08x\n", cap[0], cap[1]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1369 | #if 0 |
Michael Trimarchi | a0cb3fc | 2008-12-10 15:52:06 +0100 | [diff] [blame] | 1370 | if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */ |
| 1371 | cap[0] >>= 16; |
Sergey Temerkhanov | f657087 | 2015-04-01 17:18:46 +0300 | [diff] [blame] | 1372 | |
Christian Eggers | c918261 | 2008-05-21 22:12:00 +0200 | [diff] [blame] | 1373 | cap[0] = cpu_to_be32(cap[0]); |
| 1374 | cap[1] = cpu_to_be32(cap[1]); |
Sergey Temerkhanov | f657087 | 2015-04-01 17:18:46 +0300 | [diff] [blame] | 1375 | #endif |
Christian Eggers | c918261 | 2008-05-21 22:12:00 +0200 | [diff] [blame] | 1376 | |
Sergey Temerkhanov | f657087 | 2015-04-01 17:18:46 +0300 | [diff] [blame] | 1377 | capacity = be32_to_cpu(cap[0]) + 1; |
| 1378 | blksz = be32_to_cpu(cap[1]); |
| 1379 | |
| 1380 | debug("Capacity = 0x%08x, blocksz = 0x%08x\n", capacity, blksz); |
| 1381 | dev_desc->lba = capacity; |
| 1382 | dev_desc->blksz = blksz; |
Egbert Eich | 0472fbf | 2013-04-09 21:11:56 +0000 | [diff] [blame] | 1383 | dev_desc->log2blksz = LOG2(dev_desc->blksz); |
Wolfgang Denk | 9c998aa | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 1384 | dev_desc->type = perq; |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1385 | debug(" address %d\n", dev_desc->target); |
| 1386 | debug("partype: %d\n", dev_desc->part_type); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1387 | |
| 1388 | init_part(dev_desc); |
| 1389 | |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1390 | debug("partype: %d\n", dev_desc->part_type); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1391 | return 1; |
| 1392 | } |