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