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