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