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