2 * This file is part of the libpayload project.
4 * Copyright (C) 2008 coresystems GmbH
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <arch/endian.h>
32 #include <usb/usbmsc.h>
33 #include <usb/usbdisk.h>
36 msc_subclass_rbc = 0x1,
37 msc_subclass_mmc2 = 0x2,
38 msc_subclass_qic157 = 0x3,
39 msc_subclass_ufi = 0x4,
40 msc_subclass_sff8070i = 0x5,
41 msc_subclass_scsitrans = 0x6
44 static const char *msc_subclass_strings[7] = {
54 msc_proto_cbi_wcomp = 0x0,
55 msc_proto_cbi_wocomp = 0x1,
56 msc_proto_bulk_only = 0x50
58 static const char *msc_protocol_strings[0x51] = {
59 "Control/Bulk/Interrupt protocol (with command completion interrupt)",
60 "Control/Bulk/Interrupt protocol (with no command completion interrupt)",
61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 usb_msc_destroy (usbdev_t *dev)
80 usb_msc_poll (usbdev_t *dev)
84 const int DEV_RESET = 0xff;
85 const int GET_MAX_LUN = 0xfe;
87 const unsigned int cbw_signature = 0x43425355;
88 const unsigned int csw_signature = 0x53425355;
91 unsigned int dCBWSignature;
93 unsigned int dCBWDataTransferLength;
94 unsigned char bmCBWFlags;
95 unsigned long bCBWLUN:4;
97 unsigned long bCBWCBLength:5;
99 unsigned char CBWCB[31 - 15];
100 } __attribute__ ((packed)) cbw_t;
103 unsigned int dCSWSignature;
104 unsigned int dCSWTag;
105 unsigned int dCSWDataResidue;
106 unsigned char bCSWStatus;
107 } __attribute__ ((packed)) csw_t;
110 request_sense (usbdev_t *dev);
113 reset_transport (usbdev_t *dev)
116 memset (&dr, 0, sizeof (dr));
117 dr.bmRequestType = 0;
118 dr.data_dir = host_to_device;
120 dr.req_type = class_type;
121 dr.req_recp = iface_recp;
123 dr.bRequest = DEV_RESET;
127 dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0);
128 clear_stall (MSC_INST (dev)->bulk_in);
129 clear_stall (MSC_INST (dev)->bulk_out);
132 /* device may stall this command, so beware! */
134 get_max_luns (usbdev_t *dev)
136 unsigned char luns = 75;
138 dr.bmRequestType = 0;
139 dr.data_dir = device_to_host;
141 dr.req_type = class_type;
142 dr.req_recp = iface_recp;
144 dr.bRequest = GET_MAX_LUN;
148 if (dev->controller->control (dev, IN, sizeof (dr), &dr, 1, &luns)) {
149 luns = 0; // assume only 1 lun if req fails
158 wrap_cbw (cbw_t *cbw, int datalen, cbw_direction dir, const u8 *cmd,
161 memset (cbw, 0, sizeof (cbw_t));
163 cbw->dCBWSignature = cbw_signature;
164 cbw->dCBWTag = tag++;
165 cbw->bCBWLUN = lun; // static value per device
167 cbw->dCBWDataTransferLength = datalen;
168 cbw->bmCBWFlags = dir;
169 memcpy (cbw->CBWCB, cmd, sizeof (cbw->CBWCB));
170 cbw->bCBWCBLength = cmdlen;
174 get_csw (endpoint_t *ep, csw_t *csw)
176 if (ep->dev->controller->bulk (ep, sizeof (csw_t), (u8 *) csw, 1))
181 execute_command (usbdev_t *dev, cbw_direction dir, const u8 *cb, int cblen,
187 int always_succeed = 0;
188 if ((cb[0] == 0x1b) && (cb[4] == 1)) { //start command, always succeed
191 wrap_cbw (&cbw, buflen, dir, cb, cblen);
192 if (dev->controller->
193 bulk (MSC_INST (dev)->bulk_out, sizeof (cbw), (u8 *) &cbw, 0)) {
194 reset_transport (dev);
199 if (dir == cbw_direction_data_in) {
200 if (dev->controller->
201 bulk (MSC_INST (dev)->bulk_in, buflen, buf, 0)) {
202 clear_stall (MSC_INST (dev)->bulk_in);
206 if (dev->controller->
207 bulk (MSC_INST (dev)->bulk_out, buflen, buf, 0)) {
208 clear_stall (MSC_INST (dev)->bulk_out);
213 get_csw (MSC_INST (dev)->bulk_in, &csw);
214 if (always_succeed == 1) {
215 // return success, regardless of message
218 if (csw.bCSWStatus == 2) {
219 // phase error, reset transport
220 reset_transport (dev);
223 if (csw.bCSWStatus == 0) {
227 // error "check condition" or reserved error
233 unsigned char command; //0
234 unsigned char res1; //1
235 unsigned int block; //2-5
236 unsigned char res2; //6
237 unsigned short numblocks; //7-8
238 unsigned char res3; //9 - the block is 10 bytes long
239 } __attribute__ ((packed)) cmdblock_t;
242 unsigned char command; //0
243 unsigned char res1; //1
244 unsigned char res2; //2
245 unsigned char res3; //3
246 unsigned char lun; //4
247 unsigned char res4; //5
248 } __attribute__ ((packed)) cmdblock6_t;
251 * Like readwrite_blocks, but for soft-sectors of 512b size. Converts the
252 * start and count from 512b units.
253 * Start and count must be aligned so that they match the native
256 * @param dev device to access
257 * @param start first sector to access
258 * @param n number of sectors to access
259 * @param dir direction of access: cbw_direction_data_in == read, cbw_direction_data_out == write
260 * @param buf buffer to read into or write from. Must be at least n*512 bytes
261 * @return 0 on success, 1 on failure
264 readwrite_blocks_512 (usbdev_t *dev, int start, int n,
265 cbw_direction dir, u8 *buf)
267 int blocksize_divider = MSC_INST(dev)->blocksize / 512;
268 return readwrite_blocks (dev, start / blocksize_divider,
269 n / blocksize_divider, dir, buf);
273 * Reads or writes a number of sequential blocks on a USB storage device.
274 * As it uses the READ(10) SCSI-2 command, it's limited to storage devices
275 * of at most 2TB. It assumes sectors of 512 bytes.
277 * @param dev device to access
278 * @param start first sector to access
279 * @param n number of sectors to access
280 * @param dir direction of access: cbw_direction_data_in == read, cbw_direction_data_out == write
281 * @param buf buffer to read into or write from. Must be at least n*sectorsize bytes
282 * @return 0 on success, 1 on failure
285 readwrite_blocks (usbdev_t *dev, int start, int n, cbw_direction dir, u8 *buf)
288 memset (&cb, 0, sizeof (cb));
289 if (dir == cbw_direction_data_in) {
296 cb.block = htonl (start);
297 cb.numblocks = htonw (n);
299 return execute_command (dev, dir, (u8 *) &cb, sizeof (cb), buf,
300 n * MSC_INST(dev)->blocksize);
303 /* Only request it, we don't interpret it.
304 On certain errors, that's necessary to get devices out of
305 a special state called "Contingent Allegiance Condition" */
307 request_sense (usbdev_t *dev)
311 memset (&cb, 0, sizeof (cb));
314 return execute_command (dev, cbw_direction_data_in, (u8 *) &cb,
315 sizeof (cb), buf, 19);
319 test_unit_ready (usbdev_t *dev)
322 memset (&cb, 0, sizeof (cb)); // full initialization for T-U-R
323 return execute_command (dev, cbw_direction_data_out, (u8 *) &cb,
328 spin_up (usbdev_t *dev)
331 memset (&cb, 0, sizeof (cb));
334 return execute_command (dev, cbw_direction_data_out, (u8 *) &cb,
339 read_capacity (usbdev_t *dev)
342 memset (&cb, 0, sizeof (cb));
343 cb.command = 0x25; // read capacity
346 debug ("Reading capacity of mass storage device.\n");
348 while ((count++ < 20)
351 (dev, cbw_direction_data_in, (u8 *) &cb, sizeof (cb), buf,
354 // still not successful, assume 2tb in 512byte sectors, which is just the same garbage as any other number, but probably more usable.
355 printf (" assuming 2 TB with 512-byte sectors as READ CAPACITY didn't answer.\n");
356 MSC_INST (dev)->numblocks = 0xffffffff;
357 MSC_INST (dev)->blocksize = 512;
359 MSC_INST (dev)->numblocks = ntohl (*(u32 *) buf) + 1;
360 MSC_INST (dev)->blocksize = ntohl (*(u32 *) (buf + 4));
362 printf (" %d %d-byte sectors (%d MB)\n", MSC_INST (dev)->numblocks,
363 MSC_INST (dev)->blocksize,
364 MSC_INST (dev)->numblocks * MSC_INST (dev)->blocksize / 1000 / 1000);
368 usb_msc_init (usbdev_t *dev)
372 dev->destroy = usb_msc_destroy;
373 dev->poll = usb_msc_poll;
375 configuration_descriptor_t *cd =
376 (configuration_descriptor_t *) dev->configuration;
377 interface_descriptor_t *interface =
378 (interface_descriptor_t *) (((char *) cd) + cd->bLength);
380 debug (" it uses %s command set\n",
381 msc_subclass_strings[interface->bInterfaceSubClass]);
382 debug (" it uses %s protocol\n",
383 msc_protocol_strings[interface->bInterfaceProtocol]);
386 if (interface->bInterfaceProtocol != 0x50) {
387 printf (" Protocol not supported.\n");
391 if ((interface->bInterfaceSubClass != 2) && // ATAPI 8020
392 (interface->bInterfaceSubClass != 5) && // ATAPI 8070
393 (interface->bInterfaceSubClass != 6)) { // SCSI
394 /* Other protocols, such as ATAPI don't seem to be very popular. looks like ATAPI would be really easy to add, if necessary. */
395 printf (" Interface SubClass not supported.\n");
399 dev->data = malloc (sizeof (usbmsc_inst_t));
401 usb_fatal ("Not enough memory for USB MSC device.\n");
403 MSC_INST (dev)->protocol = interface->bInterfaceSubClass;
404 MSC_INST (dev)->bulk_in = 0;
405 MSC_INST (dev)->bulk_out = 0;
407 for (i = 1; i <= dev->num_endp; i++) {
408 if (dev->endpoints[i].endpoint == 0)
410 if (dev->endpoints[i].type != BULK)
412 if ((dev->endpoints[i].direction == IN)
413 && (MSC_INST (dev)->bulk_in == 0))
414 MSC_INST (dev)->bulk_in = &dev->endpoints[i];
415 if ((dev->endpoints[i].direction == OUT)
416 && (MSC_INST (dev)->bulk_out == 0))
417 MSC_INST (dev)->bulk_out = &dev->endpoints[i];
420 if (MSC_INST (dev)->bulk_in == 0)
421 fatal ("couldn't find bulk-in endpoint");
422 if (MSC_INST (dev)->bulk_out == 0)
423 fatal ("couldn't find bulk-out endpoint");
424 debug (" using endpoint %x as in, %x as out\n",
425 MSC_INST (dev)->bulk_in->endpoint,
426 MSC_INST (dev)->bulk_out->endpoint);
428 debug (" has %d luns\n", get_max_luns (dev) + 1);
430 printf (" Waiting for device to become ready...");
431 timeout = 30 * 10; /* SCSI/ATA specs say we have to wait up to 30s. Ugh */
432 while (test_unit_ready (dev) && --timeout) {
437 if (test_unit_ready (dev)) {
438 printf ("timeout. Device not ready. Still trying...\n");
444 for (i = 0; i < 30; i++) {
446 if (!spin_up (dev)) {
456 usbdisk_create (dev);