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