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