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