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