blob: 9ed629708a8f233e3e2adb04ba872059e3c95812 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
Wolfgang Denk460c3222005-08-04 01:14:12 +02002 * 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
wdenkaffae2b2002-08-17 09:36:01 +000012 *
wdenk149dded2003-09-10 18:20:28 +000013 * For BBB support (C) Copyright 2003
14 * Gary Jennejohn, DENX Software Engineering <gj@denx.de>
15 *
Wolfgang Denk460c3222005-08-04 01:14:12 +020016 * BBB support based on /sys/dev/usb/umass.c from
wdenk149dded2003-09-10 18:20:28 +000017 * FreeBSD.
wdenkaffae2b2002-08-17 09:36:01 +000018 *
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
wdenk80885a92004-02-26 23:46:20 +000029 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenkaffae2b2002-08-17 09:36:01 +000030 * 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 */
wdenk149dded2003-09-10 18:20:28 +000044/*
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 */
wdenkaffae2b2002-08-17 09:36:01 +000051
52
wdenkaffae2b2002-08-17 09:36:01 +000053#include <common.h>
54#include <command.h>
Christian Eggersc9182612008-05-21 22:12:00 +020055#include <asm/byteorder.h>
wdenkaffae2b2002-08-17 09:36:01 +000056#include <asm/processor.h>
57
Grant Likely735dd972007-02-20 09:04:34 +010058#include <part.h>
wdenkaffae2b2002-08-17 09:36:01 +000059#include <usb.h>
60
wdenk80885a92004-02-26 23:46:20 +000061#undef USB_STOR_DEBUG
62#undef BBB_COMDAT_TRACE
63#undef BBB_XPORT_TRACE
wdenkaffae2b2002-08-17 09:36:01 +000064
65#ifdef USB_STOR_DEBUG
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +010066#define USB_STOR_PRINTF(fmt, args...) printf(fmt , ##args)
wdenkaffae2b2002-08-17 09:36:01 +000067#else
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +010068#define USB_STOR_PRINTF(fmt, args...)
wdenkaffae2b2002-08-17 09:36:01 +000069#endif
70
71#include <scsi.h>
72/* direction table -- this indicates the direction of the data
73 * transfer for each command code -- a 1 indicates input
74 */
75unsigned char us_direction[256/8] = {
76 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
77 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
80};
81#define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
82
83static unsigned char usb_stor_buf[512];
84static ccb usb_ccb;
85
86/*
87 * CBI style
88 */
89
90#define US_CBI_ADSC 0
91
wdenk149dded2003-09-10 18:20:28 +000092/*
93 * BULK only
94 */
95#define US_BBB_RESET 0xff
96#define US_BBB_GET_MAX_LUN 0xfe
97
98/* Command Block Wrapper */
99typedef struct {
100 __u32 dCBWSignature;
101# define CBWSIGNATURE 0x43425355
102 __u32 dCBWTag;
103 __u32 dCBWDataTransferLength;
104 __u8 bCBWFlags;
105# define CBWFLAGS_OUT 0x00
106# define CBWFLAGS_IN 0x80
107 __u8 bCBWLUN;
108 __u8 bCDBLength;
109# define CBWCDBLENGTH 16
110 __u8 CBWCDB[CBWCDBLENGTH];
111} umass_bbb_cbw_t;
wdenk80885a92004-02-26 23:46:20 +0000112#define UMASS_BBB_CBW_SIZE 31
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100113static __u32 CBWTag;
wdenk149dded2003-09-10 18:20:28 +0000114
115/* Command Status Wrapper */
116typedef struct {
117 __u32 dCSWSignature;
118# define CSWSIGNATURE 0x53425355
119 __u32 dCSWTag;
120 __u32 dCSWDataResidue;
121 __u8 bCSWStatus;
122# define CSWSTATUS_GOOD 0x0
wdenk80885a92004-02-26 23:46:20 +0000123# define CSWSTATUS_FAILED 0x1
wdenk149dded2003-09-10 18:20:28 +0000124# define CSWSTATUS_PHASE 0x2
125} umass_bbb_csw_t;
wdenk80885a92004-02-26 23:46:20 +0000126#define UMASS_BBB_CSW_SIZE 13
wdenkaffae2b2002-08-17 09:36:01 +0000127
128#define USB_MAX_STOR_DEV 5
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100129static int usb_max_devs; /* number of highest available usb device */
wdenkaffae2b2002-08-17 09:36:01 +0000130
131static block_dev_desc_t usb_dev_desc[USB_MAX_STOR_DEV];
132
133struct us_data;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100134typedef int (*trans_cmnd)(ccb *cb, struct us_data *data);
135typedef int (*trans_reset)(struct us_data *data);
wdenkaffae2b2002-08-17 09:36:01 +0000136
137struct us_data {
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100138 struct usb_device *pusb_dev; /* this usb_device */
139
140 unsigned int flags; /* from filter initially */
141 unsigned char ifnum; /* interface number */
142 unsigned char ep_in; /* in endpoint */
143 unsigned char ep_out; /* out ....... */
144 unsigned char ep_int; /* interrupt . */
145 unsigned char subclass; /* as in overview */
146 unsigned char protocol; /* .............. */
147 unsigned char attention_done; /* force attn on first cmd */
148 unsigned short ip_data; /* interrupt data */
149 int action; /* what to do */
150 int ip_wanted; /* needed */
151 int *irq_handle; /* for USB int requests */
152 unsigned int irqpipe; /* pipe for release_irq */
153 unsigned char irqmaxp; /* max packed for irq Pipe */
154 unsigned char irqinterval; /* Intervall for IRQ Pipe */
155 ccb *srb; /* current srb */
156 trans_reset transport_reset; /* reset routine */
157 trans_cmnd transport; /* transport routine */
wdenkaffae2b2002-08-17 09:36:01 +0000158};
159
160static struct us_data usb_stor[USB_MAX_STOR_DEV];
161
162
wdenk80885a92004-02-26 23:46:20 +0000163#define USB_STOR_TRANSPORT_GOOD 0
wdenkaffae2b2002-08-17 09:36:01 +0000164#define USB_STOR_TRANSPORT_FAILED -1
165#define USB_STOR_TRANSPORT_ERROR -2
166
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100167int usb_stor_get_info(struct usb_device *dev, struct us_data *us,
168 block_dev_desc_t *dev_desc);
169int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
170 struct us_data *ss);
171unsigned long usb_stor_read(int device, unsigned long blknr,
172 unsigned long blkcnt, void *buffer);
wdenkaffae2b2002-08-17 09:36:01 +0000173struct usb_device * usb_get_dev_index(int index);
174void uhci_show_temp_int_td(void);
175
176block_dev_desc_t *usb_stor_get_dev(int index)
177{
Grant Likely735dd972007-02-20 09:04:34 +0100178 return (index < USB_MAX_STOR_DEV) ? &usb_dev_desc[index] : NULL;
wdenkaffae2b2002-08-17 09:36:01 +0000179}
180
181
182void usb_show_progress(void)
183{
184 printf(".");
185}
186
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100187/*******************************************************************************
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200188 * show info on storage devices; 'usb start/init' must be invoked earlier
189 * as we only retrieve structures populated during devices initialization
190 */
Aras Vaichasf6b44e02008-03-25 12:09:07 +1100191int usb_stor_info(void)
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200192{
193 int i;
194
Aras Vaichasf6b44e02008-03-25 12:09:07 +1100195 if (usb_max_devs > 0) {
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200196 for (i = 0; i < usb_max_devs; i++) {
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100197 printf(" Device %d: ", i);
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200198 dev_print(&usb_dev_desc[i]);
199 }
Markus Klotzbuecherb9e749e2008-03-26 18:26:43 +0100200 return 0;
Aras Vaichasf6b44e02008-03-25 12:09:07 +1100201 }
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700202
Markus Klotzbuecherb9e749e2008-03-26 18:26:43 +0100203 printf("No storage devices, perhaps not 'usb start'ed..?\n");
204 return 1;
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200205}
206
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100207/*******************************************************************************
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200208 * scan the usb and reports device info
wdenkaffae2b2002-08-17 09:36:01 +0000209 * to the user if mode = 1
210 * returns current device or -1 if no
211 */
212int usb_stor_scan(int mode)
213{
214 unsigned char i;
215 struct usb_device *dev;
216
wdenk149dded2003-09-10 18:20:28 +0000217 /* GJ */
218 memset(usb_stor_buf, 0, sizeof(usb_stor_buf));
219
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100220 if (mode == 1)
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200221 printf(" scanning bus for storage devices... ");
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100222
wdenkaffae2b2002-08-17 09:36:01 +0000223 usb_disable_asynch(1); /* asynch transfer not allowed */
224
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100225 for (i = 0; i < USB_MAX_STOR_DEV; i++) {
226 memset(&usb_dev_desc[i], 0, sizeof(block_dev_desc_t));
227 usb_dev_desc[i].target = 0xff;
228 usb_dev_desc[i].if_type = IF_TYPE_USB;
229 usb_dev_desc[i].dev = i;
230 usb_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
231 usb_dev_desc[i].block_read = usb_stor_read;
wdenkaffae2b2002-08-17 09:36:01 +0000232 }
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200233
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100234 usb_max_devs = 0;
235 for (i = 0; i < USB_MAX_DEVICE; i++) {
236 dev = usb_get_dev_index(i); /* get device */
237 USB_STOR_PRINTF("i=%d\n", i);
238 if (dev == NULL)
wdenkaffae2b2002-08-17 09:36:01 +0000239 break; /* no more devices avaiable */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100240
241 if (usb_storage_probe(dev, 0, &usb_stor[usb_max_devs])) {
242 /* ok, it is a storage devices
243 * get info and fill it in
244 */
245 if (usb_stor_get_info(dev, &usb_stor[usb_max_devs],
246 &usb_dev_desc[usb_max_devs]))
wdenkaffae2b2002-08-17 09:36:01 +0000247 usb_max_devs++;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100248 }
249 /* if storage device */
250 if (usb_max_devs == USB_MAX_STOR_DEV) {
251 printf("max USB Storage Device reached: %d stopping\n",
252 usb_max_devs);
wdenkaffae2b2002-08-17 09:36:01 +0000253 break;
254 }
255 } /* for */
Wolfgang Denk095b8a32005-08-02 17:06:17 +0200256
wdenkaffae2b2002-08-17 09:36:01 +0000257 usb_disable_asynch(0); /* asynch transfer allowed */
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200258 printf("%d Storage Device(s) found\n", usb_max_devs);
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100259 if (usb_max_devs > 0)
wdenkaffae2b2002-08-17 09:36:01 +0000260 return 0;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100261 return -1;
wdenkaffae2b2002-08-17 09:36:01 +0000262}
263
264static int usb_stor_irq(struct usb_device *dev)
265{
266 struct us_data *us;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100267 us = (struct us_data *)dev->privptr;
wdenkaffae2b2002-08-17 09:36:01 +0000268
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100269 if (us->ip_wanted)
270 us->ip_wanted = 0;
wdenkaffae2b2002-08-17 09:36:01 +0000271 return 0;
272}
273
274
275#ifdef USB_STOR_DEBUG
276
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100277static void usb_show_srb(ccb *pccb)
wdenkaffae2b2002-08-17 09:36:01 +0000278{
279 int i;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100280 printf("SRB: len %d datalen 0x%lX\n ", pccb->cmdlen, pccb->datalen);
281 for (i = 0; i < 12; i++)
282 printf("%02X ", pccb->cmd[i]);
wdenkaffae2b2002-08-17 09:36:01 +0000283 printf("\n");
284}
285
286static void display_int_status(unsigned long tmp)
287{
288 printf("Status: %s %s %s %s %s %s %s\n",
289 (tmp & USB_ST_ACTIVE) ? "Active" : "",
290 (tmp & USB_ST_STALLED) ? "Stalled" : "",
291 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
292 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
293 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
294 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
295 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
296}
297#endif
298/***********************************************************************
299 * Data transfer routines
300 ***********************************************************************/
301
302static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
303{
304 int max_size;
305 int this_xfer;
306 int result;
307 int partial;
308 int maxtry;
309 int stat;
310
311 /* determine the maximum packet size for these transfers */
312 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
313
314 /* while we have data left to transfer */
315 while (length) {
316
317 /* calculate how long this will be -- maximum or a remainder */
318 this_xfer = length > max_size ? max_size : length;
319 length -= this_xfer;
320
321 /* setup the retry counter */
322 maxtry = 10;
323
324 /* set up the transfer loop */
325 do {
326 /* transfer the data */
327 USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
328 (unsigned int)buf, this_xfer, 11 - maxtry);
329 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100330 this_xfer, &partial,
331 USB_CNTL_TIMEOUT * 5);
wdenkaffae2b2002-08-17 09:36:01 +0000332 USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
333 result, partial, this_xfer);
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100334 if (us->pusb_dev->status != 0) {
335 /* if we stall, we need to clear it before
336 * we go on
337 */
wdenkaffae2b2002-08-17 09:36:01 +0000338#ifdef USB_STOR_DEBUG
339 display_int_status(us->pusb_dev->status);
340#endif
341 if (us->pusb_dev->status & USB_ST_STALLED) {
342 USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe);
343 stat = us->pusb_dev->status;
344 usb_clear_halt(us->pusb_dev, pipe);
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100345 us->pusb_dev->status = stat;
346 if (this_xfer == partial) {
347 USB_STOR_PRINTF("bulk transferred with error %X, but data ok\n", us->pusb_dev->status);
wdenkaffae2b2002-08-17 09:36:01 +0000348 return 0;
349 }
350 else
351 return result;
352 }
353 if (us->pusb_dev->status & USB_ST_NAK_REC) {
354 USB_STOR_PRINTF("Device NAKed bulk_msg\n");
355 return result;
356 }
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100357 USB_STOR_PRINTF("bulk transferred with error");
358 if (this_xfer == partial) {
359 USB_STOR_PRINTF(" %d, but data ok\n",
360 us->pusb_dev->status);
wdenkaffae2b2002-08-17 09:36:01 +0000361 return 0;
362 }
363 /* if our try counter reaches 0, bail out */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100364 USB_STOR_PRINTF(" %d, data %d\n",
365 us->pusb_dev->status, partial);
wdenkaffae2b2002-08-17 09:36:01 +0000366 if (!maxtry--)
367 return result;
368 }
369 /* update to show what data was transferred */
370 this_xfer -= partial;
371 buf += partial;
372 /* continue until this transfer is done */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100373 } while (this_xfer);
wdenkaffae2b2002-08-17 09:36:01 +0000374 }
375
376 /* if we get here, we're done and successful */
377 return 0;
378}
379
wdenk149dded2003-09-10 18:20:28 +0000380static int usb_stor_BBB_reset(struct us_data *us)
381{
382 int result;
383 unsigned int pipe;
384
385 /*
386 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
387 *
388 * For Reset Recovery the host shall issue in the following order:
389 * a) a Bulk-Only Mass Storage Reset
390 * b) a Clear Feature HALT to the Bulk-In endpoint
391 * c) a Clear Feature HALT to the Bulk-Out endpoint
392 *
393 * This is done in 3 steps.
394 *
395 * If the reset doesn't succeed, the device should be port reset.
396 *
397 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
398 */
399 USB_STOR_PRINTF("BBB_reset\n");
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100400 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
401 US_BBB_RESET,
402 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
403 0, us->ifnum, 0, 0, USB_CNTL_TIMEOUT * 5);
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200404
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100405 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
wdenk149dded2003-09-10 18:20:28 +0000406 USB_STOR_PRINTF("RESET:stall\n");
407 return -1;
408 }
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200409
wdenk149dded2003-09-10 18:20:28 +0000410 /* long wait for reset */
411 wait_ms(150);
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100412 USB_STOR_PRINTF("BBB_reset result %d: status %X reset\n", result,
413 us->pusb_dev->status);
wdenk149dded2003-09-10 18:20:28 +0000414 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
415 result = usb_clear_halt(us->pusb_dev, pipe);
416 /* long wait for reset */
417 wait_ms(150);
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100418 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing IN endpoint\n",
419 result, us->pusb_dev->status);
wdenk149dded2003-09-10 18:20:28 +0000420 /* long wait for reset */
421 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
422 result = usb_clear_halt(us->pusb_dev, pipe);
423 wait_ms(150);
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100424 USB_STOR_PRINTF("BBB_reset result %d: status %X"
425 " clearing OUT endpoint\n", result,
426 us->pusb_dev->status);
wdenk149dded2003-09-10 18:20:28 +0000427 USB_STOR_PRINTF("BBB_reset done\n");
428 return 0;
429}
430
wdenkaffae2b2002-08-17 09:36:01 +0000431/* FIXME: this reset function doesn't really reset the port, and it
432 * should. Actually it should probably do what it's doing here, and
433 * reset the port physically
434 */
435static int usb_stor_CB_reset(struct us_data *us)
436{
437 unsigned char cmd[12];
438 int result;
439
440 USB_STOR_PRINTF("CB_reset\n");
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100441 memset(cmd, 0xff, sizeof(cmd));
wdenkaffae2b2002-08-17 09:36:01 +0000442 cmd[0] = SCSI_SEND_DIAG;
443 cmd[1] = 4;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100444 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
445 US_CBI_ADSC,
446 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
447 0, us->ifnum, cmd, sizeof(cmd),
448 USB_CNTL_TIMEOUT * 5);
wdenkaffae2b2002-08-17 09:36:01 +0000449
450 /* long wait for reset */
451 wait_ms(1500);
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100452 USB_STOR_PRINTF("CB_reset result %d: status %X"
453 " clearing endpoint halt\n", result,
454 us->pusb_dev->status);
wdenkaffae2b2002-08-17 09:36:01 +0000455 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
456 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
457
458 USB_STOR_PRINTF("CB_reset done\n");
459 return 0;
460}
461
wdenk149dded2003-09-10 18:20:28 +0000462/*
463 * Set up the command for a BBB device. Note that the actual SCSI
464 * command is copied into cbw.CBWCDB.
465 */
466int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
467{
468 int result;
469 int actlen;
470 int dir_in;
471 unsigned int pipe;
472 umass_bbb_cbw_t cbw;
473
474 dir_in = US_DIRECTION(srb->cmd[0]);
475
476#ifdef BBB_COMDAT_TRACE
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100477 printf("dir %d lun %d cmdlen %d cmd %p datalen %d pdata %p\n",
478 dir_in, srb->lun, srb->cmdlen, srb->cmd, srb->datalen,
479 srb->pdata);
wdenk149dded2003-09-10 18:20:28 +0000480 if (srb->cmdlen) {
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100481 for (result = 0; result < srb->cmdlen; result++)
wdenk149dded2003-09-10 18:20:28 +0000482 printf("cmd[%d] %#x ", result, srb->cmd[result]);
483 printf("\n");
484 }
485#endif
486 /* sanity checks */
487 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
488 USB_STOR_PRINTF("usb_stor_BBB_comdat:cmdlen too large\n");
489 return -1;
490 }
491
492 /* always OUT to the ep */
493 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
494
Christian Eggersc9182612008-05-21 22:12:00 +0200495 cbw.dCBWSignature = cpu_to_le32(CBWSIGNATURE);
496 cbw.dCBWTag = cpu_to_le32(CBWTag++);
497 cbw.dCBWDataTransferLength = cpu_to_le32(srb->datalen);
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100498 cbw.bCBWFlags = (dir_in ? CBWFLAGS_IN : CBWFLAGS_OUT);
wdenk149dded2003-09-10 18:20:28 +0000499 cbw.bCBWLUN = srb->lun;
500 cbw.bCDBLength = srb->cmdlen;
501 /* copy the command data into the CBW command data buffer */
502 /* DST SRC LEN!!! */
503 memcpy(cbw.CBWCDB, srb->cmd, srb->cmdlen);
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100504 result = usb_bulk_msg(us->pusb_dev, pipe, &cbw, UMASS_BBB_CBW_SIZE,
505 &actlen, USB_CNTL_TIMEOUT * 5);
wdenk149dded2003-09-10 18:20:28 +0000506 if (result < 0)
507 USB_STOR_PRINTF("usb_stor_BBB_comdat:usb_bulk_msg error\n");
508 return result;
509}
510
wdenkaffae2b2002-08-17 09:36:01 +0000511/* FIXME: we also need a CBI_command which sets up the completion
512 * interrupt, and waits for it
513 */
514int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
515{
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200516 int result = 0;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100517 int dir_in, retry;
wdenkaffae2b2002-08-17 09:36:01 +0000518 unsigned int pipe;
519 unsigned long status;
520
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100521 retry = 5;
522 dir_in = US_DIRECTION(srb->cmd[0]);
wdenkaffae2b2002-08-17 09:36:01 +0000523
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100524 if (dir_in)
525 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
526 else
527 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
528
529 while (retry--) {
530 USB_STOR_PRINTF("CBI gets a command: Try %d\n", 5 - retry);
wdenkaffae2b2002-08-17 09:36:01 +0000531#ifdef USB_STOR_DEBUG
532 usb_show_srb(srb);
533#endif
534 /* let's send the command via the control pipe */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100535 result = usb_control_msg(us->pusb_dev,
536 usb_sndctrlpipe(us->pusb_dev , 0),
537 US_CBI_ADSC,
538 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
wdenkaffae2b2002-08-17 09:36:01 +0000539 0, us->ifnum,
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100540 srb->cmd, srb->cmdlen,
541 USB_CNTL_TIMEOUT * 5);
542 USB_STOR_PRINTF("CB_transport: control msg returned %d,"
543 " status %X\n", result, us->pusb_dev->status);
wdenkaffae2b2002-08-17 09:36:01 +0000544 /* check the return code for the command */
545 if (result < 0) {
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100546 if (us->pusb_dev->status & USB_ST_STALLED) {
547 status = us->pusb_dev->status;
548 USB_STOR_PRINTF(" stall during command found,"
549 " clear pipe\n");
550 usb_clear_halt(us->pusb_dev,
551 usb_sndctrlpipe(us->pusb_dev, 0));
552 us->pusb_dev->status = status;
wdenkaffae2b2002-08-17 09:36:01 +0000553 }
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100554 USB_STOR_PRINTF(" error during command %02X"
555 " Stat = %X\n", srb->cmd[0],
556 us->pusb_dev->status);
wdenkaffae2b2002-08-17 09:36:01 +0000557 return result;
558 }
559 /* transfer the data payload for this command, if one exists*/
560
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100561 USB_STOR_PRINTF("CB_transport: control msg returned %d,"
562 " direction is %s to go 0x%lx\n", result,
563 dir_in ? "IN" : "OUT", srb->datalen);
wdenkaffae2b2002-08-17 09:36:01 +0000564 if (srb->datalen) {
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100565 result = us_one_transfer(us, pipe, (char *)srb->pdata,
566 srb->datalen);
567 USB_STOR_PRINTF("CBI attempted to transfer data,"
568 " result is %d status %lX, len %d\n",
569 result, us->pusb_dev->status,
570 us->pusb_dev->act_len);
571 if (!(us->pusb_dev->status & USB_ST_NAK_REC))
wdenkaffae2b2002-08-17 09:36:01 +0000572 break;
573 } /* if (srb->datalen) */
574 else
575 break;
576 }
577 /* return result */
578
579 return result;
580}
581
582
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100583int usb_stor_CBI_get_status(ccb *srb, struct us_data *us)
wdenkaffae2b2002-08-17 09:36:01 +0000584{
585 int timeout;
586
wdenk80885a92004-02-26 23:46:20 +0000587 us->ip_wanted = 1;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100588 submit_int_msg(us->pusb_dev, us->irqpipe,
wdenk80885a92004-02-26 23:46:20 +0000589 (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
590 timeout = 1000;
591 while (timeout--) {
592 if ((volatile int *) us->ip_wanted == 0)
wdenkaffae2b2002-08-17 09:36:01 +0000593 break;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100594 wait_ms(10);
wdenkaffae2b2002-08-17 09:36:01 +0000595 }
596 if (us->ip_wanted) {
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100597 printf(" Did not get interrupt on CBI\n");
wdenkaffae2b2002-08-17 09:36:01 +0000598 us->ip_wanted = 0;
599 return USB_STOR_TRANSPORT_ERROR;
600 }
wdenk80885a92004-02-26 23:46:20 +0000601 USB_STOR_PRINTF
602 ("Got interrupt data 0x%x, transfered %d status 0x%lX\n",
603 us->ip_data, us->pusb_dev->irq_act_len,
604 us->pusb_dev->irq_status);
wdenkaffae2b2002-08-17 09:36:01 +0000605 /* UFI gives us ASC and ASCQ, like a request sense */
606 if (us->subclass == US_SC_UFI) {
607 if (srb->cmd[0] == SCSI_REQ_SENSE ||
608 srb->cmd[0] == SCSI_INQUIRY)
609 return USB_STOR_TRANSPORT_GOOD; /* Good */
wdenk80885a92004-02-26 23:46:20 +0000610 else if (us->ip_data)
611 return USB_STOR_TRANSPORT_FAILED;
wdenkaffae2b2002-08-17 09:36:01 +0000612 else
wdenk80885a92004-02-26 23:46:20 +0000613 return USB_STOR_TRANSPORT_GOOD;
wdenkaffae2b2002-08-17 09:36:01 +0000614 }
615 /* otherwise, we interpret the data normally */
616 switch (us->ip_data) {
wdenk80885a92004-02-26 23:46:20 +0000617 case 0x0001:
618 return USB_STOR_TRANSPORT_GOOD;
619 case 0x0002:
620 return USB_STOR_TRANSPORT_FAILED;
621 default:
622 return USB_STOR_TRANSPORT_ERROR;
623 } /* switch */
wdenkaffae2b2002-08-17 09:36:01 +0000624 return USB_STOR_TRANSPORT_ERROR;
625}
626
627#define USB_TRANSPORT_UNKNOWN_RETRY 5
628#define USB_TRANSPORT_NOT_READY_RETRY 10
629
wdenk149dded2003-09-10 18:20:28 +0000630/* clear a stall on an endpoint - special for BBB devices */
631int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
632{
633 int result;
634
635 /* ENDPOINT_HALT = 0, so set value to 0 */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100636 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
wdenk149dded2003-09-10 18:20:28 +0000637 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100638 0, endpt, 0, 0, USB_CNTL_TIMEOUT * 5);
wdenk149dded2003-09-10 18:20:28 +0000639 return result;
640}
641
642int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
643{
644 int result, retry;
645 int dir_in;
646 int actlen, data_actlen;
647 unsigned int pipe, pipein, pipeout;
648 umass_bbb_csw_t csw;
649#ifdef BBB_XPORT_TRACE
650 unsigned char *ptr;
651 int index;
652#endif
653
654 dir_in = US_DIRECTION(srb->cmd[0]);
655
656 /* COMMAND phase */
657 USB_STOR_PRINTF("COMMAND phase\n");
658 result = usb_stor_BBB_comdat(srb, us);
659 if (result < 0) {
660 USB_STOR_PRINTF("failed to send CBW status %ld\n",
661 us->pusb_dev->status);
662 usb_stor_BBB_reset(us);
663 return USB_STOR_TRANSPORT_FAILED;
664 }
665 wait_ms(5);
666 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
667 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
668 /* DATA phase + error handling */
wdenk149dded2003-09-10 18:20:28 +0000669 data_actlen = 0;
670 /* no data, go immediately to the STATUS phase */
671 if (srb->datalen == 0)
672 goto st;
wdenk80885a92004-02-26 23:46:20 +0000673 USB_STOR_PRINTF("DATA phase\n");
wdenk149dded2003-09-10 18:20:28 +0000674 if (dir_in)
675 pipe = pipein;
676 else
677 pipe = pipeout;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100678 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen,
679 &data_actlen, USB_CNTL_TIMEOUT * 5);
wdenk149dded2003-09-10 18:20:28 +0000680 /* special handling of STALL in DATA phase */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100681 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
wdenka43278a2003-09-11 19:48:06 +0000682 USB_STOR_PRINTF("DATA:stall\n");
wdenk149dded2003-09-10 18:20:28 +0000683 /* clear the STALL on the endpoint */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100684 result = usb_stor_BBB_clear_endpt_stall(us,
685 dir_in ? us->ep_in : us->ep_out);
wdenk149dded2003-09-10 18:20:28 +0000686 if (result >= 0)
687 /* continue on to STATUS phase */
688 goto st;
689 }
690 if (result < 0) {
691 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
692 us->pusb_dev->status);
693 usb_stor_BBB_reset(us);
694 return USB_STOR_TRANSPORT_FAILED;
695 }
696#ifdef BBB_XPORT_TRACE
697 for (index = 0; index < data_actlen; index++)
698 printf("pdata[%d] %#x ", index, srb->pdata[index]);
699 printf("\n");
700#endif
701 /* STATUS phase + error handling */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100702st:
wdenk149dded2003-09-10 18:20:28 +0000703 retry = 0;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100704again:
wdenk149dded2003-09-10 18:20:28 +0000705 USB_STOR_PRINTF("STATUS phase\n");
Wolfgang Denk095b8a32005-08-02 17:06:17 +0200706 result = usb_bulk_msg(us->pusb_dev, pipein, &csw, UMASS_BBB_CSW_SIZE,
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200707 &actlen, USB_CNTL_TIMEOUT*5);
708
wdenk149dded2003-09-10 18:20:28 +0000709 /* special handling of STALL in STATUS phase */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100710 if ((result < 0) && (retry < 1) &&
711 (us->pusb_dev->status & USB_ST_STALLED)) {
wdenk149dded2003-09-10 18:20:28 +0000712 USB_STOR_PRINTF("STATUS:stall\n");
713 /* clear the STALL on the endpoint */
714 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
715 if (result >= 0 && (retry++ < 1))
716 /* do a retry */
717 goto again;
718 }
719 if (result < 0) {
720 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
721 us->pusb_dev->status);
722 usb_stor_BBB_reset(us);
723 return USB_STOR_TRANSPORT_FAILED;
724 }
725#ifdef BBB_XPORT_TRACE
726 ptr = (unsigned char *)&csw;
727 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
728 printf("ptr[%d] %#x ", index, ptr[index]);
729 printf("\n");
730#endif
731 /* misuse pipe to get the residue */
Christian Eggersc9182612008-05-21 22:12:00 +0200732 pipe = le32_to_cpu(csw.dCSWDataResidue);
wdenk149dded2003-09-10 18:20:28 +0000733 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
734 pipe = srb->datalen - data_actlen;
Christian Eggersc9182612008-05-21 22:12:00 +0200735 if (CSWSIGNATURE != le32_to_cpu(csw.dCSWSignature)) {
wdenk149dded2003-09-10 18:20:28 +0000736 USB_STOR_PRINTF("!CSWSIGNATURE\n");
737 usb_stor_BBB_reset(us);
738 return USB_STOR_TRANSPORT_FAILED;
Christian Eggersc9182612008-05-21 22:12:00 +0200739 } else if ((CBWTag - 1) != le32_to_cpu(csw.dCSWTag)) {
wdenk149dded2003-09-10 18:20:28 +0000740 USB_STOR_PRINTF("!Tag\n");
741 usb_stor_BBB_reset(us);
742 return USB_STOR_TRANSPORT_FAILED;
743 } else if (csw.bCSWStatus > CSWSTATUS_PHASE) {
744 USB_STOR_PRINTF(">PHASE\n");
745 usb_stor_BBB_reset(us);
746 return USB_STOR_TRANSPORT_FAILED;
747 } else if (csw.bCSWStatus == CSWSTATUS_PHASE) {
748 USB_STOR_PRINTF("=PHASE\n");
749 usb_stor_BBB_reset(us);
750 return USB_STOR_TRANSPORT_FAILED;
751 } else if (data_actlen > srb->datalen) {
752 USB_STOR_PRINTF("transferred %dB instead of %dB\n",
753 data_actlen, srb->datalen);
754 return USB_STOR_TRANSPORT_FAILED;
755 } else if (csw.bCSWStatus == CSWSTATUS_FAILED) {
756 USB_STOR_PRINTF("FAILED\n");
757 return USB_STOR_TRANSPORT_FAILED;
758 }
759
760 return result;
761}
762
wdenkaffae2b2002-08-17 09:36:01 +0000763int usb_stor_CB_transport(ccb *srb, struct us_data *us)
764{
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100765 int result, status;
wdenkaffae2b2002-08-17 09:36:01 +0000766 ccb *psrb;
767 ccb reqsrb;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100768 int retry, notready;
wdenkaffae2b2002-08-17 09:36:01 +0000769
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +0200770 psrb = &reqsrb;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100771 status = USB_STOR_TRANSPORT_GOOD;
772 retry = 0;
773 notready = 0;
wdenkaffae2b2002-08-17 09:36:01 +0000774 /* issue the command */
775do_retry:
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100776 result = usb_stor_CB_comdat(srb, us);
777 USB_STOR_PRINTF("command / Data returned %d, status %X\n",
778 result, us->pusb_dev->status);
wdenkaffae2b2002-08-17 09:36:01 +0000779 /* if this is an CBI Protocol, get IRQ */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100780 if (us->protocol == US_PR_CBI) {
781 status = usb_stor_CBI_get_status(srb, us);
wdenkaffae2b2002-08-17 09:36:01 +0000782 /* if the status is error, report it */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100783 if (status == USB_STOR_TRANSPORT_ERROR) {
wdenkaffae2b2002-08-17 09:36:01 +0000784 USB_STOR_PRINTF(" USB CBI Command Error\n");
785 return status;
786 }
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100787 srb->sense_buf[12] = (unsigned char)(us->ip_data >> 8);
788 srb->sense_buf[13] = (unsigned char)(us->ip_data & 0xff);
789 if (!us->ip_data) {
790 /* if the status is good, report it */
791 if (status == USB_STOR_TRANSPORT_GOOD) {
wdenkaffae2b2002-08-17 09:36:01 +0000792 USB_STOR_PRINTF(" USB CBI Command Good\n");
793 return status;
794 }
795 }
796 }
797 /* do we have to issue an auto request? */
798 /* HERE we have to check the result */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100799 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
800 USB_STOR_PRINTF("ERROR %X\n", us->pusb_dev->status);
wdenkaffae2b2002-08-17 09:36:01 +0000801 us->transport_reset(us);
802 return USB_STOR_TRANSPORT_ERROR;
803 }
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100804 if ((us->protocol == US_PR_CBI) &&
805 ((srb->cmd[0] == SCSI_REQ_SENSE) ||
806 (srb->cmd[0] == SCSI_INQUIRY))) {
807 /* do not issue an autorequest after request sense */
wdenkaffae2b2002-08-17 09:36:01 +0000808 USB_STOR_PRINTF("No auto request and good\n");
809 return USB_STOR_TRANSPORT_GOOD;
810 }
811 /* issue an request_sense */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100812 memset(&psrb->cmd[0], 0, 12);
813 psrb->cmd[0] = SCSI_REQ_SENSE;
814 psrb->cmd[1] = srb->lun << 5;
815 psrb->cmd[4] = 18;
816 psrb->datalen = 18;
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +0200817 psrb->pdata = &srb->sense_buf[0];
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100818 psrb->cmdlen = 12;
wdenkaffae2b2002-08-17 09:36:01 +0000819 /* issue the command */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100820 result = usb_stor_CB_comdat(psrb, us);
821 USB_STOR_PRINTF("auto request returned %d\n", result);
wdenkaffae2b2002-08-17 09:36:01 +0000822 /* if this is an CBI Protocol, get IRQ */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100823 if (us->protocol == US_PR_CBI)
824 status = usb_stor_CBI_get_status(psrb, us);
825
826 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
827 USB_STOR_PRINTF(" AUTO REQUEST ERROR %d\n",
828 us->pusb_dev->status);
wdenkaffae2b2002-08-17 09:36:01 +0000829 return USB_STOR_TRANSPORT_ERROR;
830 }
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100831 USB_STOR_PRINTF("autorequest returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
832 srb->sense_buf[0], srb->sense_buf[2],
833 srb->sense_buf[12], srb->sense_buf[13]);
wdenkaffae2b2002-08-17 09:36:01 +0000834 /* Check the auto request result */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100835 if ((srb->sense_buf[2] == 0) &&
836 (srb->sense_buf[12] == 0) &&
837 (srb->sense_buf[13] == 0)) {
838 /* ok, no sense */
wdenkaffae2b2002-08-17 09:36:01 +0000839 return USB_STOR_TRANSPORT_GOOD;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100840 }
841
wdenkaffae2b2002-08-17 09:36:01 +0000842 /* Check the auto request result */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100843 switch (srb->sense_buf[2]) {
844 case 0x01:
845 /* Recovered Error */
wdenk149dded2003-09-10 18:20:28 +0000846 return USB_STOR_TRANSPORT_GOOD;
wdenk80885a92004-02-26 23:46:20 +0000847 break;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100848 case 0x02:
849 /* Not Ready */
850 if (notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
851 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
852 " 0x%02X (NOT READY)\n", srb->cmd[0],
853 srb->sense_buf[0], srb->sense_buf[2],
854 srb->sense_buf[12], srb->sense_buf[13]);
wdenk149dded2003-09-10 18:20:28 +0000855 return USB_STOR_TRANSPORT_FAILED;
856 } else {
857 wait_ms(100);
858 goto do_retry;
859 }
860 break;
861 default:
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100862 if (retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
863 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
864 " 0x%02X\n", srb->cmd[0], srb->sense_buf[0],
865 srb->sense_buf[2], srb->sense_buf[12],
866 srb->sense_buf[13]);
wdenk149dded2003-09-10 18:20:28 +0000867 return USB_STOR_TRANSPORT_FAILED;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100868 } else
wdenk149dded2003-09-10 18:20:28 +0000869 goto do_retry;
wdenk149dded2003-09-10 18:20:28 +0000870 break;
wdenkaffae2b2002-08-17 09:36:01 +0000871 }
872 return USB_STOR_TRANSPORT_FAILED;
873}
874
875
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100876static int usb_inquiry(ccb *srb, struct us_data *ss)
wdenkaffae2b2002-08-17 09:36:01 +0000877{
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100878 int retry, i;
879 retry = 5;
wdenkaffae2b2002-08-17 09:36:01 +0000880 do {
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100881 memset(&srb->cmd[0], 0, 12);
882 srb->cmd[0] = SCSI_INQUIRY;
883 srb->cmd[1] = srb->lun<<5;
884 srb->cmd[4] = 36;
885 srb->datalen = 36;
886 srb->cmdlen = 12;
887 i = ss->transport(srb, ss);
888 USB_STOR_PRINTF("inquiry returns %d\n", i);
889 if (i == 0)
wdenkaffae2b2002-08-17 09:36:01 +0000890 break;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100891 } while (retry--);
wdenk149dded2003-09-10 18:20:28 +0000892
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100893 if (!retry) {
wdenkaffae2b2002-08-17 09:36:01 +0000894 printf("error in inquiry\n");
895 return -1;
896 }
897 return 0;
898}
899
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100900static int usb_request_sense(ccb *srb, struct us_data *ss)
wdenkaffae2b2002-08-17 09:36:01 +0000901{
902 char *ptr;
wdenk80885a92004-02-26 23:46:20 +0000903
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100904 ptr = (char *)srb->pdata;
905 memset(&srb->cmd[0], 0, 12);
906 srb->cmd[0] = SCSI_REQ_SENSE;
907 srb->cmd[1] = srb->lun << 5;
908 srb->cmd[4] = 18;
909 srb->datalen = 18;
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +0200910 srb->pdata = &srb->sense_buf[0];
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100911 srb->cmdlen = 12;
912 ss->transport(srb, ss);
913 USB_STOR_PRINTF("Request Sense returned %02X %02X %02X\n",
914 srb->sense_buf[2], srb->sense_buf[12],
915 srb->sense_buf[13]);
916 srb->pdata = (uchar *)ptr;
wdenkaffae2b2002-08-17 09:36:01 +0000917 return 0;
918}
919
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100920static int usb_test_unit_ready(ccb *srb, struct us_data *ss)
wdenkaffae2b2002-08-17 09:36:01 +0000921{
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200922 int retries = 10;
wdenk149dded2003-09-10 18:20:28 +0000923
wdenkaffae2b2002-08-17 09:36:01 +0000924 do {
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100925 memset(&srb->cmd[0], 0, 12);
926 srb->cmd[0] = SCSI_TST_U_RDY;
927 srb->cmd[1] = srb->lun << 5;
928 srb->datalen = 0;
929 srb->cmdlen = 12;
930 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
wdenkaffae2b2002-08-17 09:36:01 +0000931 return 0;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100932 usb_request_sense(srb, ss);
933 wait_ms(100);
934 } while (retries--);
wdenk149dded2003-09-10 18:20:28 +0000935
wdenkaffae2b2002-08-17 09:36:01 +0000936 return -1;
937}
938
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100939static int usb_read_capacity(ccb *srb, struct us_data *ss)
wdenkaffae2b2002-08-17 09:36:01 +0000940{
941 int retry;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100942 /* XXX retries */
943 retry = 3;
wdenkaffae2b2002-08-17 09:36:01 +0000944 do {
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100945 memset(&srb->cmd[0], 0, 12);
946 srb->cmd[0] = SCSI_RD_CAPAC;
947 srb->cmd[1] = srb->lun << 5;
948 srb->datalen = 8;
949 srb->cmdlen = 12;
950 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
wdenkaffae2b2002-08-17 09:36:01 +0000951 return 0;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100952 } while (retry--);
wdenk149dded2003-09-10 18:20:28 +0000953
wdenkaffae2b2002-08-17 09:36:01 +0000954 return -1;
955}
956
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100957static int usb_read_10(ccb *srb, struct us_data *ss, unsigned long start,
958 unsigned short blocks)
wdenkaffae2b2002-08-17 09:36:01 +0000959{
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100960 memset(&srb->cmd[0], 0, 12);
961 srb->cmd[0] = SCSI_READ10;
962 srb->cmd[1] = srb->lun << 5;
963 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
964 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
965 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
966 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
967 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
968 srb->cmd[8] = (unsigned char) blocks & 0xff;
969 srb->cmdlen = 12;
970 USB_STOR_PRINTF("read10: start %lx blocks %x\n", start, blocks);
971 return ss->transport(srb, ss);
wdenkaffae2b2002-08-17 09:36:01 +0000972}
973
974
Bartlomiej Siekaddde6b72006-08-22 10:38:18 +0200975#ifdef CONFIG_USB_BIN_FIXUP
976/*
977 * Some USB storage devices queried for SCSI identification data respond with
978 * binary strings, which if output to the console freeze the terminal. The
979 * workaround is to modify the vendor and product strings read from such
980 * device with proper values (as reported by 'usb info').
981 *
982 * Vendor and product length limits are taken from the definition of
983 * block_dev_desc_t in include/part.h.
984 */
985static void usb_bin_fixup(struct usb_device_descriptor descriptor,
986 unsigned char vendor[],
987 unsigned char product[]) {
988 const unsigned char max_vendor_len = 40;
989 const unsigned char max_product_len = 20;
990 if (descriptor.idVendor == 0x0424 && descriptor.idProduct == 0x223a) {
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +0100991 strncpy((char *)vendor, "SMSC", max_vendor_len);
992 strncpy((char *)product, "Flash Media Cntrller",
993 max_product_len);
Bartlomiej Siekaddde6b72006-08-22 10:38:18 +0200994 }
995}
996#endif /* CONFIG_USB_BIN_FIXUP */
997
wdenkaffae2b2002-08-17 09:36:01 +0000998#define USB_MAX_READ_BLK 20
999
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001000unsigned long usb_stor_read(int device, unsigned long blknr,
1001 unsigned long blkcnt, void *buffer)
wdenkaffae2b2002-08-17 09:36:01 +00001002{
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001003 unsigned long start, blks, buf_addr;
wdenkaffae2b2002-08-17 09:36:01 +00001004 unsigned short smallblks;
1005 struct usb_device *dev;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001006 int retry, i;
wdenkf8d813e2004-03-02 14:05:39 +00001007 ccb *srb = &usb_ccb;
1008
1009 if (blkcnt == 0)
1010 return 0;
1011
1012 device &= 0xff;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001013 /* Setup device */
1014 USB_STOR_PRINTF("\nusb_read: dev %d \n", device);
1015 dev = NULL;
1016 for (i = 0; i < USB_MAX_DEVICE; i++) {
1017 dev = usb_get_dev_index(i);
1018 if (dev == NULL)
wdenkaffae2b2002-08-17 09:36:01 +00001019 return 0;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001020 if (dev->devnum == usb_dev_desc[device].target)
wdenkaffae2b2002-08-17 09:36:01 +00001021 break;
1022 }
1023
1024 usb_disable_asynch(1); /* asynch transfer not allowed */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001025 srb->lun = usb_dev_desc[device].lun;
1026 buf_addr = (unsigned long)buffer;
1027 start = blknr;
1028 blks = blkcnt;
1029 if (usb_test_unit_ready(srb, (struct us_data *)dev->privptr)) {
1030 printf("Device NOT ready\n Request Sense returned %02X %02X"
1031 " %02X\n", srb->sense_buf[2], srb->sense_buf[12],
1032 srb->sense_buf[13]);
wdenkaffae2b2002-08-17 09:36:01 +00001033 return 0;
1034 }
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001035
1036 USB_STOR_PRINTF("\nusb_read: dev %d startblk %lx, blccnt %lx"
1037 " buffer %lx\n", device, start, blks, buf_addr);
1038
wdenkaffae2b2002-08-17 09:36:01 +00001039 do {
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001040 /* XXX need some comment here */
1041 retry = 2;
1042 srb->pdata = (unsigned char *)buf_addr;
1043 if (blks > USB_MAX_READ_BLK)
1044 smallblks = USB_MAX_READ_BLK;
1045 else
1046 smallblks = (unsigned short) blks;
wdenkaffae2b2002-08-17 09:36:01 +00001047retry_it:
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001048 if (smallblks == USB_MAX_READ_BLK)
wdenkaffae2b2002-08-17 09:36:01 +00001049 usb_show_progress();
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001050 srb->datalen = usb_dev_desc[device].blksz * smallblks;
1051 srb->pdata = (unsigned char *)buf_addr;
1052 if (usb_read_10(srb, (struct us_data *)dev->privptr, start,
1053 smallblks)) {
wdenkaffae2b2002-08-17 09:36:01 +00001054 USB_STOR_PRINTF("Read ERROR\n");
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001055 usb_request_sense(srb, (struct us_data *)dev->privptr);
1056 if (retry--)
wdenkaffae2b2002-08-17 09:36:01 +00001057 goto retry_it;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001058 blkcnt -= blks;
wdenkaffae2b2002-08-17 09:36:01 +00001059 break;
1060 }
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001061 start += smallblks;
1062 blks -= smallblks;
1063 buf_addr += srb->datalen;
1064 } while (blks != 0);
1065
1066 USB_STOR_PRINTF("usb_read: end startblk %lx, blccnt %x buffer %lx\n",
1067 start, smallblks, buf_addr);
1068
wdenkaffae2b2002-08-17 09:36:01 +00001069 usb_disable_asynch(0); /* asynch transfer allowed */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001070 if (blkcnt >= USB_MAX_READ_BLK)
wdenkaffae2b2002-08-17 09:36:01 +00001071 printf("\n");
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001072 return blkcnt;
wdenkaffae2b2002-08-17 09:36:01 +00001073}
1074
1075
1076/* Probe to see if a new device is actually a Storage device */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001077int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
1078 struct us_data *ss)
wdenkaffae2b2002-08-17 09:36:01 +00001079{
1080 struct usb_interface_descriptor *iface;
1081 int i;
1082 unsigned int flags = 0;
1083
1084 int protocol = 0;
1085 int subclass = 0;
1086
wdenkaffae2b2002-08-17 09:36:01 +00001087 /* let's examine the device now */
1088 iface = &dev->config.if_desc[ifnum];
1089
1090#if 0
1091 /* this is the place to patch some storage devices */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001092 USB_STOR_PRINTF("iVendor %X iProduct %X\n", dev->descriptor.idVendor,
1093 dev->descriptor.idProduct);
1094
1095 if ((dev->descriptor.idVendor) == 0x066b &&
1096 (dev->descriptor.idProduct) == 0x0103) {
wdenkaffae2b2002-08-17 09:36:01 +00001097 USB_STOR_PRINTF("patched for E-USB\n");
1098 protocol = US_PR_CB;
1099 subclass = US_SC_UFI; /* an assumption */
1100 }
1101#endif
1102
1103 if (dev->descriptor.bDeviceClass != 0 ||
1104 iface->bInterfaceClass != USB_CLASS_MASS_STORAGE ||
1105 iface->bInterfaceSubClass < US_SC_MIN ||
1106 iface->bInterfaceSubClass > US_SC_MAX) {
1107 /* if it's not a mass storage, we go no further */
1108 return 0;
1109 }
1110
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001111 memset(ss, 0, sizeof(struct us_data));
1112
wdenkaffae2b2002-08-17 09:36:01 +00001113 /* At this point, we know we've got a live one */
1114 USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
1115
1116 /* Initialize the us_data structure with some useful info */
1117 ss->flags = flags;
1118 ss->ifnum = ifnum;
1119 ss->pusb_dev = dev;
1120 ss->attention_done = 0;
1121
1122 /* If the device has subclass and protocol, then use that. Otherwise,
1123 * take data from the specific interface.
1124 */
1125 if (subclass) {
1126 ss->subclass = subclass;
1127 ss->protocol = protocol;
1128 } else {
1129 ss->subclass = iface->bInterfaceSubClass;
1130 ss->protocol = iface->bInterfaceProtocol;
1131 }
1132
1133 /* set the handler pointers based on the protocol */
1134 USB_STOR_PRINTF("Transport: ");
1135 switch (ss->protocol) {
1136 case US_PR_CB:
1137 USB_STOR_PRINTF("Control/Bulk\n");
1138 ss->transport = usb_stor_CB_transport;
1139 ss->transport_reset = usb_stor_CB_reset;
1140 break;
1141
1142 case US_PR_CBI:
1143 USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
1144 ss->transport = usb_stor_CB_transport;
1145 ss->transport_reset = usb_stor_CB_reset;
1146 break;
wdenk149dded2003-09-10 18:20:28 +00001147 case US_PR_BULK:
1148 USB_STOR_PRINTF("Bulk/Bulk/Bulk\n");
1149 ss->transport = usb_stor_BBB_transport;
1150 ss->transport_reset = usb_stor_BBB_reset;
1151 break;
wdenkaffae2b2002-08-17 09:36:01 +00001152 default:
wdenk80885a92004-02-26 23:46:20 +00001153 printf("USB Storage Transport unknown / not yet implemented\n");
wdenkaffae2b2002-08-17 09:36:01 +00001154 return 0;
1155 break;
1156 }
1157
1158 /*
1159 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1160 * An optional interrupt is OK (necessary for CBI protocol).
1161 * We will ignore any others.
1162 */
1163 for (i = 0; i < iface->bNumEndpoints; i++) {
1164 /* is it an BULK endpoint? */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001165 if ((iface->ep_desc[i].bmAttributes &
1166 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
wdenkaffae2b2002-08-17 09:36:01 +00001167 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
1168 ss->ep_in = iface->ep_desc[i].bEndpointAddress &
1169 USB_ENDPOINT_NUMBER_MASK;
1170 else
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001171 ss->ep_out =
1172 iface->ep_desc[i].bEndpointAddress &
wdenkaffae2b2002-08-17 09:36:01 +00001173 USB_ENDPOINT_NUMBER_MASK;
1174 }
1175
1176 /* is it an interrupt endpoint? */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001177 if ((iface->ep_desc[i].bmAttributes &
1178 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
wdenkaffae2b2002-08-17 09:36:01 +00001179 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
1180 USB_ENDPOINT_NUMBER_MASK;
1181 ss->irqinterval = iface->ep_desc[i].bInterval;
1182 }
1183 }
1184 USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
1185 ss->ep_in, ss->ep_out, ss->ep_int);
1186
1187 /* Do some basic sanity checks, and bail if we find a problem */
1188 if (usb_set_interface(dev, iface->bInterfaceNumber, 0) ||
1189 !ss->ep_in || !ss->ep_out ||
1190 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1191 USB_STOR_PRINTF("Problems with device\n");
1192 return 0;
1193 }
1194 /* set class specific stuff */
wdenk149dded2003-09-10 18:20:28 +00001195 /* We only handle certain protocols. Currently, these are
1196 * the only ones.
wdenk80885a92004-02-26 23:46:20 +00001197 * The SFF8070 accepts the requests used in u-boot
wdenkaffae2b2002-08-17 09:36:01 +00001198 */
wdenk80885a92004-02-26 23:46:20 +00001199 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
1200 ss->subclass != US_SC_8070) {
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001201 printf("Sorry, protocol %d not yet supported.\n", ss->subclass);
wdenkaffae2b2002-08-17 09:36:01 +00001202 return 0;
1203 }
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001204 if (ss->ep_int) {
1205 /* we had found an interrupt endpoint, prepare irq pipe
1206 * set up the IRQ pipe and handler
1207 */
wdenkaffae2b2002-08-17 09:36:01 +00001208 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1209 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1210 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001211 dev->irq_handle = usb_stor_irq;
wdenkaffae2b2002-08-17 09:36:01 +00001212 }
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001213 dev->privptr = (void *)ss;
wdenkaffae2b2002-08-17 09:36:01 +00001214 return 1;
1215}
1216
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001217int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
1218 block_dev_desc_t *dev_desc)
wdenkaffae2b2002-08-17 09:36:01 +00001219{
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001220 unsigned char perq, modi;
wdenkaffae2b2002-08-17 09:36:01 +00001221 unsigned long cap[2];
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001222 unsigned long *capacity, *blksz;
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001223 ccb *pccb = &usb_ccb;
wdenkaffae2b2002-08-17 09:36:01 +00001224
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001225 /* for some reasons a couple of devices would not survive this reset */
1226 if (
1227 /* Sony USM256E */
1228 (dev->descriptor.idVendor == 0x054c &&
1229 dev->descriptor.idProduct == 0x019e)
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001230 ||
1231 /* USB007 Mini-USB2 Flash Drive */
1232 (dev->descriptor.idVendor == 0x066f &&
1233 dev->descriptor.idProduct == 0x2010)
Bartlomiej Siekaf88a0ae2006-07-13 15:32:16 +02001234 ||
1235 /* SanDisk Corporation Cruzer Micro 20044318410546613953 */
1236 (dev->descriptor.idVendor == 0x0781 &&
1237 dev->descriptor.idProduct == 0x5151)
Bryan Wu14e41112009-01-01 19:48:07 -05001238 ||
1239 /*
1240 * SanDisk Corporation U3 Cruzer Micro 1/4GB
1241 * Flash Drive 000016244373FFB4
1242 */
1243 (dev->descriptor.idVendor == 0x0781 &&
1244 dev->descriptor.idProduct == 0x5406)
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001245 )
1246 USB_STOR_PRINTF("usb_stor_get_info: skipping RESET..\n");
Wolfgang Denk095b8a32005-08-02 17:06:17 +02001247 else
wdenk2729af92004-05-03 20:45:30 +00001248 ss->transport_reset(ss);
wdenkaffae2b2002-08-17 09:36:01 +00001249
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001250 pccb->pdata = usb_stor_buf;
1251
1252 dev_desc->target = dev->devnum;
1253 pccb->lun = dev_desc->lun;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001254 USB_STOR_PRINTF(" address %d\n", dev_desc->target);
wdenkaffae2b2002-08-17 09:36:01 +00001255
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001256 if (usb_inquiry(pccb, ss))
wdenkaffae2b2002-08-17 09:36:01 +00001257 return -1;
Wolfgang Denk095b8a32005-08-02 17:06:17 +02001258
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001259 perq = usb_stor_buf[0];
1260 modi = usb_stor_buf[1];
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001261
1262 if ((perq & 0x1f) == 0x1f) {
1263 /* skip unknown devices */
1264 return 0;
wdenkaffae2b2002-08-17 09:36:01 +00001265 }
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001266 if ((modi&0x80) == 0x80) {
1267 /* drive is removable */
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001268 dev_desc->removable = 1;
wdenkaffae2b2002-08-17 09:36:01 +00001269 }
1270 memcpy(&dev_desc->vendor[0], &usb_stor_buf[8], 8);
1271 memcpy(&dev_desc->product[0], &usb_stor_buf[16], 16);
1272 memcpy(&dev_desc->revision[0], &usb_stor_buf[32], 4);
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001273 dev_desc->vendor[8] = 0;
1274 dev_desc->product[16] = 0;
1275 dev_desc->revision[4] = 0;
Bartlomiej Siekaddde6b72006-08-22 10:38:18 +02001276#ifdef CONFIG_USB_BIN_FIXUP
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001277 usb_bin_fixup(dev->descriptor, (uchar *)dev_desc->vendor,
1278 (uchar *)dev_desc->product);
Bartlomiej Siekaddde6b72006-08-22 10:38:18 +02001279#endif /* CONFIG_USB_BIN_FIXUP */
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001280 USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n", usb_stor_buf[2],
1281 usb_stor_buf[3]);
1282 if (usb_test_unit_ready(pccb, ss)) {
1283 printf("Device NOT ready\n"
1284 " Request Sense returned %02X %02X %02X\n",
1285 pccb->sense_buf[2], pccb->sense_buf[12],
1286 pccb->sense_buf[13]);
1287 if (dev_desc->removable == 1) {
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001288 dev_desc->type = perq;
wdenkaffae2b2002-08-17 09:36:01 +00001289 return 1;
1290 }
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001291 return 0;
wdenkaffae2b2002-08-17 09:36:01 +00001292 }
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001293 pccb->pdata = (unsigned char *)&cap[0];
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001294 memset(pccb->pdata, 0, 8);
1295 if (usb_read_capacity(pccb, ss) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +00001296 printf("READ_CAP ERROR\n");
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001297 cap[0] = 2880;
1298 cap[1] = 0x200;
wdenkaffae2b2002-08-17 09:36:01 +00001299 }
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001300 USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n", cap[0],
1301 cap[1]);
wdenkaffae2b2002-08-17 09:36:01 +00001302#if 0
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001303 if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */
1304 cap[0] >>= 16;
wdenkaffae2b2002-08-17 09:36:01 +00001305#endif
Christian Eggersc9182612008-05-21 22:12:00 +02001306 cap[0] = cpu_to_be32(cap[0]);
1307 cap[1] = cpu_to_be32(cap[1]);
1308
wdenk149dded2003-09-10 18:20:28 +00001309 /* this assumes bigendian! */
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001310 cap[0] += 1;
1311 capacity = &cap[0];
1312 blksz = &cap[1];
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001313 USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",
1314 *capacity, *blksz);
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001315 dev_desc->lba = *capacity;
1316 dev_desc->blksz = *blksz;
1317 dev_desc->type = perq;
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001318 USB_STOR_PRINTF(" address %d\n", dev_desc->target);
1319 USB_STOR_PRINTF("partype: %d\n", dev_desc->part_type);
wdenkaffae2b2002-08-17 09:36:01 +00001320
1321 init_part(dev_desc);
1322
Michael Trimarchia0cb3fc2008-12-10 15:52:06 +01001323 USB_STOR_PRINTF("partype: %d\n", dev_desc->part_type);
wdenkaffae2b2002-08-17 09:36:01 +00001324 return 1;
1325}