rk27xx: Disable cache while invalidating it.
[maemo-rb.git] / firmware / usbstack / usb_storage.c
blobb7fdeac7e2906cf9ca532c050d64a4d0928ba46e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 by Björn Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "string.h"
22 #include "system.h"
23 #include "usb_core.h"
24 #include "usb_drv.h"
25 #include "usb_class_driver.h"
26 /*#define LOGF_ENABLE*/
27 #include "logf.h"
28 #include "storage.h"
29 #include "disk.h"
30 #include "fat.h"
31 /* Needed to get at the audio buffer */
32 #include "audio.h"
33 #include "usb_storage.h"
34 #if CONFIG_RTC
35 #include "timefuncs.h"
36 #endif
38 #ifdef USB_USE_RAMDISK
39 #define RAMDISK_SIZE 2048
40 #endif
42 #ifndef SECTOR_SIZE
43 #define SECTOR_SIZE 512
44 #endif
46 /* These defaults allow the operation */
47 #ifndef USBSTOR_READ_SECTORS_FILTER
48 #define USBSTOR_READ_SECTORS_FILTER() ({ 0; })
49 #endif
51 #ifndef USBSTOR_WRITE_SECTORS_FILTER
52 #define USBSTOR_WRITE_SECTORS_FILTER() ({ 0; })
53 #endif
55 /* the ARC driver currently supports up to 64k USB transfers. This is
56 * enough for efficient mass storage support, as commonly host OSes
57 * don't do larger SCSI transfers anyway, so larger USB transfers
58 * wouldn't buy us anything.
59 * Due to being the double-buffering system used, using a smaller write buffer
60 * ends up being more efficient. Measurements have shown that 24k to 28k is
61 * optimal, except for sd devices that apparently don't gain anything from
62 * double-buffering
64 #ifdef USB_READ_BUFFER_SIZE
65 #define READ_BUFFER_SIZE USB_READ_BUFFER_SIZE
66 #else
67 #if CONFIG_USBOTG == USBOTG_AS3525
68 /* We'd need to implement multidescriptor dma for sizes >65535 */
69 #define READ_BUFFER_SIZE (1024*63)
70 #else
71 #define READ_BUFFER_SIZE (1024*64)
72 #endif /* CONFIG_CPU == AS3525 */
73 #endif /* USB_READ_BUFFER_SIZE */
75 #define MAX_CBW_SIZE 1024
77 #ifdef USB_WRITE_BUFFER_SIZE
78 #define WRITE_BUFFER_SIZE USB_WRITE_BUFFER_SIZE
79 #else
80 #if (CONFIG_STORAGE & STORAGE_SD)
81 #if CONFIG_USBOTG == USBOTG_AS3525
82 /* We'd need to implement multidescriptor dma for sizes >65535 */
83 #define WRITE_BUFFER_SIZE (1024*63)
84 #else
85 #define WRITE_BUFFER_SIZE (1024*64)
86 #endif /* CONFIG_CPU == AS3525 */
87 #else
88 #define WRITE_BUFFER_SIZE (1024*24)
89 #endif /* (CONFIG_STORAGE & STORAGE_SD) */
90 #endif /* USB_WRITE_BUFFER_SIZE */
92 #define ALLOCATE_BUFFER_SIZE (2*MAX(READ_BUFFER_SIZE,WRITE_BUFFER_SIZE))
94 /* bulk-only class specific requests */
95 #define USB_BULK_RESET_REQUEST 0xff
96 #define USB_BULK_GET_MAX_LUN 0xfe
98 #define DIRECT_ACCESS_DEVICE 0x00 /* disks */
99 #define DEVICE_REMOVABLE 0x80
101 #define CBW_SIGNATURE 0x43425355
102 #define CSW_SIGNATURE 0x53425355
104 #define SCSI_TEST_UNIT_READY 0x00
105 #define SCSI_INQUIRY 0x12
106 #define SCSI_MODE_SENSE_6 0x1a
107 #define SCSI_MODE_SENSE_10 0x5a
108 #define SCSI_REQUEST_SENSE 0x03
109 #define SCSI_ALLOW_MEDIUM_REMOVAL 0x1e
110 #define SCSI_READ_CAPACITY 0x25
111 #define SCSI_READ_FORMAT_CAPACITY 0x23
112 #define SCSI_READ_10 0x28
113 #define SCSI_WRITE_10 0x2a
114 #define SCSI_START_STOP_UNIT 0x1b
115 #define SCSI_REPORT_LUNS 0xa0
116 #define SCSI_WRITE_BUFFER 0x3b
118 #define UMS_STATUS_GOOD 0x00
119 #define UMS_STATUS_FAIL 0x01
121 #define SENSE_NOT_READY 0x02
122 #define SENSE_MEDIUM_ERROR 0x03
123 #define SENSE_ILLEGAL_REQUEST 0x05
124 #define SENSE_UNIT_ATTENTION 0x06
126 #define ASC_MEDIUM_NOT_PRESENT 0x3a
127 #define ASC_INVALID_FIELD_IN_CBD 0x24
128 #define ASC_LBA_OUT_OF_RANGE 0x21
129 #define ASC_WRITE_ERROR 0x0C
130 #define ASC_READ_ERROR 0x11
131 #define ASC_NOT_READY 0x04
132 #define ASC_INVALID_COMMAND 0x20
134 #define ASCQ_BECOMING_READY 0x01
136 #define SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA 0x02000000
138 /* storage interface */
140 #define USB_SC_SCSI 0x06 /* Transparent */
141 #define USB_PROT_BULK 0x50 /* bulk only */
143 static struct usb_interface_descriptor __attribute__((aligned(2)))
144 interface_descriptor =
146 .bLength = sizeof(struct usb_interface_descriptor),
147 .bDescriptorType = USB_DT_INTERFACE,
148 .bInterfaceNumber = 0,
149 .bAlternateSetting = 0,
150 .bNumEndpoints = 2,
151 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
152 .bInterfaceSubClass = USB_SC_SCSI,
153 .bInterfaceProtocol = USB_PROT_BULK,
154 .iInterface = 0
157 static struct usb_endpoint_descriptor __attribute__((aligned(2)))
158 endpoint_descriptor =
160 .bLength = sizeof(struct usb_endpoint_descriptor),
161 .bDescriptorType = USB_DT_ENDPOINT,
162 .bEndpointAddress = 0,
163 .bmAttributes = USB_ENDPOINT_XFER_BULK,
164 .wMaxPacketSize = 0,
165 .bInterval = 0
168 struct inquiry_data {
169 unsigned char DeviceType;
170 unsigned char DeviceTypeModifier;
171 unsigned char Versions;
172 unsigned char Format;
173 unsigned char AdditionalLength;
174 unsigned char Reserved[2];
175 unsigned char Capability;
176 unsigned char VendorId[8];
177 unsigned char ProductId[16];
178 unsigned char ProductRevisionLevel[4];
179 } __attribute__ ((packed));
181 struct report_lun_data {
182 unsigned int lun_list_length;
183 unsigned int reserved1;
184 unsigned char luns[NUM_DRIVES][8];
185 } __attribute__ ((packed));
187 struct sense_data {
188 unsigned char ResponseCode;
189 unsigned char Obsolete;
190 unsigned char fei_sensekey;
191 unsigned int Information;
192 unsigned char AdditionalSenseLength;
193 unsigned int CommandSpecificInformation;
194 unsigned char AdditionalSenseCode;
195 unsigned char AdditionalSenseCodeQualifier;
196 unsigned char FieldReplaceableUnitCode;
197 unsigned char SKSV;
198 unsigned short SenseKeySpecific;
199 } __attribute__ ((packed));
201 struct mode_sense_bdesc_longlba {
202 unsigned char num_blocks[8];
203 unsigned char reserved[4];
204 unsigned char block_size[4];
205 } __attribute__ ((packed));
207 struct mode_sense_bdesc_shortlba {
208 unsigned char density_code;
209 unsigned char num_blocks[3];
210 unsigned char reserved;
211 unsigned char block_size[3];
212 } __attribute__ ((packed));
214 struct mode_sense_data_10 {
215 unsigned short mode_data_length;
216 unsigned char medium_type;
217 unsigned char device_specific;
218 unsigned char longlba;
219 unsigned char reserved;
220 unsigned short block_descriptor_length;
221 struct mode_sense_bdesc_longlba block_descriptor;
222 } __attribute__ ((packed));
224 struct mode_sense_data_6 {
225 unsigned char mode_data_length;
226 unsigned char medium_type;
227 unsigned char device_specific;
228 unsigned char block_descriptor_length;
229 struct mode_sense_bdesc_shortlba block_descriptor;
230 } __attribute__ ((packed));
232 struct command_block_wrapper {
233 unsigned int signature;
234 unsigned int tag;
235 unsigned int data_transfer_length;
236 unsigned char flags;
237 unsigned char lun;
238 unsigned char command_length;
239 unsigned char command_block[16];
240 } __attribute__ ((packed));
242 struct command_status_wrapper {
243 unsigned int signature;
244 unsigned int tag;
245 unsigned int data_residue;
246 unsigned char status;
247 } __attribute__ ((packed));
249 struct capacity {
250 unsigned int block_count;
251 unsigned int block_size;
252 } __attribute__ ((packed));
254 struct format_capacity {
255 unsigned int following_length;
256 unsigned int block_count;
257 unsigned int block_size;
258 } __attribute__ ((packed));
261 static union {
262 unsigned char* transfer_buffer;
263 struct inquiry_data* inquiry;
264 struct capacity* capacity_data;
265 struct format_capacity* format_capacity_data;
266 struct sense_data *sense_data;
267 struct mode_sense_data_6 *ms_data_6;
268 struct mode_sense_data_10 *ms_data_10;
269 struct report_lun_data *lun_data;
270 struct command_status_wrapper* csw;
271 char *max_lun;
272 } tb;
274 static char *cbw_buffer;
276 static struct {
277 unsigned int sector;
278 unsigned int count;
279 unsigned int orig_count;
280 unsigned int cur_cmd;
281 unsigned int tag;
282 unsigned int lun;
283 unsigned char *data[2];
284 unsigned char data_select;
285 unsigned int last_result;
286 } cur_cmd;
288 static struct {
289 unsigned char sense_key;
290 unsigned char information;
291 unsigned char asc;
292 unsigned char ascq;
293 } cur_sense_data;
295 static void handle_scsi(struct command_block_wrapper* cbw);
296 static void send_csw(int status);
297 static void send_command_result(void *data,int size);
298 static void send_command_failed_result(void);
299 static void send_block_data(void *data,int size);
300 static void receive_block_data(void *data,int size);
301 #if CONFIG_RTC
302 static void receive_time(void);
303 #endif
304 static void fill_inquiry(IF_MD_NONVOID(int lun));
305 static void send_and_read_next(void);
306 static bool ejected[NUM_DRIVES];
307 static bool locked[NUM_DRIVES];
309 static int usb_interface;
310 static int ep_in, ep_out;
312 #if defined(HAVE_MULTIDRIVE)
313 static bool skip_first = 0;
314 #endif
316 #ifdef USB_USE_RAMDISK
317 static unsigned char* ramdisk_buffer;
318 #endif
320 static enum {
321 WAITING_FOR_COMMAND,
322 SENDING_BLOCKS,
323 SENDING_RESULT,
324 SENDING_FAILED_RESULT,
325 RECEIVING_BLOCKS,
326 #if CONFIG_RTC
327 RECEIVING_TIME,
328 #endif
329 WAITING_FOR_CSW_COMPLETION_OR_COMMAND,
330 WAITING_FOR_CSW_COMPLETION
331 } state = WAITING_FOR_COMMAND;
333 #if CONFIG_RTC
334 static void yearday_to_daymonth(int yd, int y, int *d, int *m)
336 static char t[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
338 bool leap = (y%4 == 0 && y%100 != 0) || y%400 == 0;
339 t[2] = leap ? 29 : 28;
341 int i;
342 for (i = 0; i < 12 && yd >= t[i]; i++)
343 yd -= t[i];
345 *d = yd+1;
346 *m = i;
348 #endif /* CONFIG_RTC */
350 static bool check_disk_present(IF_MD_NONVOID(int volume))
352 #ifdef USB_USE_RAMDISK
353 return true;
354 #else
355 unsigned char* sector = fat_get_sector_buffer();
356 bool success = storage_read_sectors(IF_MD2(volume,)0,1,sector) == 0;
357 fat_release_sector_buffer();
358 return success;
359 #endif
362 #ifdef HAVE_HOTSWAP
363 void usb_storage_notify_hotswap(int volume,bool inserted)
365 logf("notify %d",inserted);
366 if(inserted && check_disk_present(IF_MD(volume))) {
367 ejected[volume] = false;
369 else {
370 ejected[volume] = true;
371 /* If this happens while the device is locked, weird things may happen.
372 At least try to keep our state consistent */
373 locked[volume]=false;
376 #endif
378 #ifdef HAVE_MULTIDRIVE
379 void usb_set_skip_first_drive(bool skip)
381 skip_first = skip;
383 #endif
385 /* called by usb_core_init() */
386 void usb_storage_init(void)
388 logf("usb_storage_init done");
391 int usb_storage_request_endpoints(struct usb_class_driver *drv)
393 ep_in = usb_core_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_IN, drv);
395 if(ep_in<0)
396 return -1;
398 ep_out = usb_core_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_OUT,
399 drv);
401 if(ep_out<0) {
402 usb_core_release_endpoint(ep_in);
403 return -1;
406 return 0;
409 int usb_storage_set_first_interface(int interface)
411 usb_interface = interface;
412 return interface + 1;
415 int usb_storage_get_config_descriptor(unsigned char *dest,int max_packet_size)
417 unsigned char *orig_dest = dest;
419 interface_descriptor.bInterfaceNumber = usb_interface;
420 PACK_DATA(&dest, interface_descriptor);
422 endpoint_descriptor.wMaxPacketSize = max_packet_size;
424 endpoint_descriptor.bEndpointAddress = ep_in;
425 PACK_DATA(&dest, endpoint_descriptor);
427 endpoint_descriptor.bEndpointAddress = ep_out;
428 PACK_DATA(&dest, endpoint_descriptor);
430 return (dest - orig_dest);
433 void usb_storage_init_connection(void)
435 logf("ums: set config");
436 /* prime rx endpoint. We only need room for commands */
437 state = WAITING_FOR_COMMAND;
439 #if (CONFIG_CPU == IMX31L || defined(CPU_TCC77X) || defined(CPU_TCC780X) || \
440 defined(BOOTLOADER) || CONFIG_CPU == DM320) && !defined(CPU_PP502x)
441 static unsigned char _cbw_buffer[MAX_CBW_SIZE]
442 USB_DEVBSS_ATTR __attribute__((aligned(32)));
443 cbw_buffer = (void *)_cbw_buffer;
445 static unsigned char _transfer_buffer[ALLOCATE_BUFFER_SIZE]
446 USB_DEVBSS_ATTR __attribute__((aligned(32)));
447 tb.transfer_buffer = (void *)_transfer_buffer;
448 #ifdef USB_USE_RAMDISK
449 static unsigned char _ramdisk_buffer[RAMDISK_SIZE*SECTOR_SIZE];
450 ramdisk_buffer = _ramdisk_buffer;
451 #endif
452 #else
453 /* TODO : check if bufsize is at least 32K ? */
454 size_t bufsize;
455 unsigned char * audio_buffer;
457 audio_buffer = audio_get_buffer(false,&bufsize);
458 #if defined(UNCACHED_ADDR) && CONFIG_CPU != AS3525
459 cbw_buffer = (void *)UNCACHED_ADDR((unsigned int)(audio_buffer+31) & 0xffffffe0);
460 #else
461 cbw_buffer = (void *)((unsigned int)(audio_buffer+31) & 0xffffffe0);
462 #endif
463 tb.transfer_buffer = cbw_buffer + MAX_CBW_SIZE;
464 commit_discard_dcache();
465 #ifdef USB_USE_RAMDISK
466 ramdisk_buffer = tb.transfer_buffer + ALLOCATE_BUFFER_SIZE;
467 #endif
468 #endif
469 usb_drv_recv(ep_out, cbw_buffer, MAX_CBW_SIZE);
471 int i;
472 for(i=0;i<storage_num_drives();i++) {
473 locked[i] = false;
474 ejected[i] = !check_disk_present(IF_MD(i));
475 queue_broadcast(SYS_USB_LUN_LOCKED, (i<<16)+0);
479 void usb_storage_disconnect(void)
481 /* Empty for now */
484 /* called by usb_core_transfer_complete() */
485 void usb_storage_transfer_complete(int ep,int dir,int status,int length)
487 (void)ep;
488 struct command_block_wrapper* cbw = (void*)cbw_buffer;
489 #if CONFIG_RTC
490 struct tm tm;
491 #endif
493 logf("transfer result for ep %d/%d %X %d", ep,dir,status, length);
494 switch(state) {
495 case RECEIVING_BLOCKS:
496 if(dir==USB_DIR_IN) {
497 logf("IN received in RECEIVING");
499 logf("scsi write %d %d", cur_cmd.sector, cur_cmd.count);
500 if(status==0) {
501 if((unsigned int)length!=(SECTOR_SIZE* cur_cmd.count)
502 && (unsigned int)length!=WRITE_BUFFER_SIZE) {
503 logf("unexpected length :%d",length);
504 break;
507 unsigned int next_sector = cur_cmd.sector +
508 (WRITE_BUFFER_SIZE/SECTOR_SIZE);
509 unsigned int next_count = cur_cmd.count -
510 MIN(cur_cmd.count,WRITE_BUFFER_SIZE/SECTOR_SIZE);
511 int next_select = !cur_cmd.data_select;
513 if(next_count!=0) {
514 /* Ask the host to send more, to the other buffer */
515 receive_block_data(cur_cmd.data[next_select],
516 MIN(WRITE_BUFFER_SIZE,next_count*SECTOR_SIZE));
519 /* Now write the data that just came in, while the host is
520 sending the next bit */
521 #ifdef USB_USE_RAMDISK
522 memcpy(ramdisk_buffer + cur_cmd.sector*SECTOR_SIZE,
523 cur_cmd.data[cur_cmd.data_select],
524 MIN(WRITE_BUFFER_SIZE/SECTOR_SIZE, cur_cmd.count)*SECTOR_SIZE);
525 #else
526 int result = USBSTOR_WRITE_SECTORS_FILTER();
528 if (result == 0) {
529 result = storage_write_sectors(IF_MD2(cur_cmd.lun,)
530 cur_cmd.sector,
531 MIN(WRITE_BUFFER_SIZE/SECTOR_SIZE, cur_cmd.count),
532 cur_cmd.data[cur_cmd.data_select]);
535 if(result != 0) {
536 send_csw(UMS_STATUS_FAIL);
537 cur_sense_data.sense_key=SENSE_MEDIUM_ERROR;
538 cur_sense_data.asc=ASC_WRITE_ERROR;
539 cur_sense_data.ascq=0;
540 break;
542 #endif
543 if(next_count==0) {
544 send_csw(UMS_STATUS_GOOD);
547 /* Switch buffers for the next one */
548 cur_cmd.data_select=!cur_cmd.data_select;
550 cur_cmd.sector = next_sector;
551 cur_cmd.count = next_count;
553 else {
554 logf("Transfer failed %X",status);
555 send_csw(UMS_STATUS_FAIL);
556 /* TODO fill in cur_sense_data */
557 cur_sense_data.sense_key=0;
558 cur_sense_data.information=0;
559 cur_sense_data.asc=0;
560 cur_sense_data.ascq=0;
562 break;
563 case WAITING_FOR_CSW_COMPLETION_OR_COMMAND:
564 if(dir==USB_DIR_IN) {
565 /* This was the CSW */
566 state = WAITING_FOR_COMMAND;
568 else {
569 /* This was the command */
570 state = WAITING_FOR_CSW_COMPLETION;
571 /* We now have the CBW, but we won't execute it yet to avoid
572 * issues with the still-pending CSW */
574 break;
575 case WAITING_FOR_COMMAND:
576 if(dir==USB_DIR_IN) {
577 logf("IN received in WAITING_FOR_COMMAND");
579 handle_scsi(cbw);
580 break;
581 case WAITING_FOR_CSW_COMPLETION:
582 if(dir==USB_DIR_OUT) {
583 logf("OUT received in WAITING_FOR_CSW_COMPLETION");
585 handle_scsi(cbw);
586 break;
587 #if 0
588 if(cur_cmd.cur_cmd == SCSI_WRITE_10)
590 queue_broadcast(SYS_USB_WRITE_DATA, (cur_cmd.lun<<16)+cur_cmd.orig_count);
592 else if(cur_cmd.cur_cmd == SCSI_READ_10)
594 queue_broadcast(SYS_USB_READ_DATA, (cur_cmd.lun<<16)+cur_cmd.orig_count);
596 #endif
597 break;
598 case SENDING_RESULT:
599 if(dir==USB_DIR_OUT) {
600 logf("OUT received in SENDING");
602 if(status==0) {
603 //logf("data sent, now send csw");
604 send_csw(UMS_STATUS_GOOD);
606 else {
607 logf("Transfer failed %X",status);
608 send_csw(UMS_STATUS_FAIL);
609 /* TODO fill in cur_sense_data */
610 cur_sense_data.sense_key=0;
611 cur_sense_data.information=0;
612 cur_sense_data.asc=0;
613 cur_sense_data.ascq=0;
615 break;
616 case SENDING_FAILED_RESULT:
617 if(dir==USB_DIR_OUT) {
618 logf("OUT received in SENDING");
620 send_csw(UMS_STATUS_FAIL);
621 break;
622 case SENDING_BLOCKS:
623 if(dir==USB_DIR_OUT) {
624 logf("OUT received in SENDING");
626 if(status==0) {
627 if(cur_cmd.count==0) {
628 //logf("data sent, now send csw");
629 if(cur_cmd.last_result!=0) {
630 /* The last read failed. */
631 send_csw(UMS_STATUS_FAIL);
632 cur_sense_data.sense_key=SENSE_MEDIUM_ERROR;
633 cur_sense_data.asc=ASC_READ_ERROR;
634 cur_sense_data.ascq=0;
635 return;
637 else
638 send_csw(UMS_STATUS_GOOD);
640 else {
641 send_and_read_next();
644 else {
645 logf("Transfer failed %X",status);
646 send_csw(UMS_STATUS_FAIL);
647 /* TODO fill in cur_sense_data */
648 cur_sense_data.sense_key=0;
649 cur_sense_data.information=0;
650 cur_sense_data.asc=0;
651 cur_sense_data.ascq=0;
653 break;
654 #if CONFIG_RTC
655 case RECEIVING_TIME:
656 tm.tm_year=(tb.transfer_buffer[0]<<8)+tb.transfer_buffer[1] - 1900;
657 tm.tm_yday=(tb.transfer_buffer[2]<<8)+tb.transfer_buffer[3];
658 tm.tm_hour=tb.transfer_buffer[5];
659 tm.tm_min=tb.transfer_buffer[6];
660 tm.tm_sec=tb.transfer_buffer[7];
661 yearday_to_daymonth(tm.tm_yday,tm.tm_year + 1900,&tm.tm_mday,&tm.tm_mon);
662 set_day_of_week(&tm);
663 set_time(&tm);
664 send_csw(UMS_STATUS_GOOD);
665 break;
666 #endif /* CONFIG_RTC */
670 /* called by usb_core_control_request() */
671 bool usb_storage_control_request(struct usb_ctrlrequest* req, unsigned char* dest)
673 bool handled = false;
675 (void)dest;
676 switch (req->bRequest) {
677 case USB_BULK_GET_MAX_LUN: {
678 *tb.max_lun = storage_num_drives() - 1;
679 #if defined(HAVE_MULTIDRIVE)
680 if(skip_first) (*tb.max_lun) --;
681 #endif
682 logf("ums: getmaxlun");
683 usb_drv_recv(EP_CONTROL, NULL, 0); /* ack */
684 usb_drv_send(EP_CONTROL, tb.max_lun, 1);
685 handled = true;
686 break;
689 case USB_BULK_RESET_REQUEST:
690 logf("ums: bulk reset");
691 state = WAITING_FOR_COMMAND;
692 /* UMS BOT 3.1 says The device shall preserve the value of its bulk
693 data toggle bits and endpoint STALL conditions despite
694 the Bulk-Only Mass Storage Reset. */
695 #if 0
696 usb_drv_reset_endpoint(ep_in, false);
697 usb_drv_reset_endpoint(ep_out, true);
698 #endif
699 usb_drv_send(EP_CONTROL, NULL, 0); /* ack */
700 handled = true;
701 break;
704 return handled;
707 static void send_and_read_next(void)
709 int result = USBSTOR_READ_SECTORS_FILTER();
711 if(result != 0 && cur_cmd.last_result == 0)
712 cur_cmd.last_result = result;
714 send_block_data(cur_cmd.data[cur_cmd.data_select],
715 MIN(READ_BUFFER_SIZE,cur_cmd.count*SECTOR_SIZE));
717 /* Switch buffers for the next one */
718 cur_cmd.data_select=!cur_cmd.data_select;
720 cur_cmd.sector+=(READ_BUFFER_SIZE/SECTOR_SIZE);
721 cur_cmd.count-=MIN(cur_cmd.count,READ_BUFFER_SIZE/SECTOR_SIZE);
723 if(cur_cmd.count!=0) {
724 /* already read the next bit, so we can send it out immediately when the
725 * current transfer completes. */
726 #ifdef USB_USE_RAMDISK
727 memcpy(cur_cmd.data[cur_cmd.data_select],
728 ramdisk_buffer + cur_cmd.sector*SECTOR_SIZE,
729 MIN(READ_BUFFER_SIZE/SECTOR_SIZE, cur_cmd.count)*SECTOR_SIZE);
730 #else
731 result = storage_read_sectors(IF_MD2(cur_cmd.lun,)
732 cur_cmd.sector,
733 MIN(READ_BUFFER_SIZE/SECTOR_SIZE, cur_cmd.count),
734 cur_cmd.data[cur_cmd.data_select]);
735 if(cur_cmd.last_result == 0)
736 cur_cmd.last_result = result;
738 #endif
741 /****************************************************************************/
743 static void handle_scsi(struct command_block_wrapper* cbw)
745 /* USB Mass Storage assumes LBA capability.
746 TODO: support 48-bit LBA */
748 struct storage_info info;
749 unsigned int length = cbw->data_transfer_length;
750 unsigned int block_size, block_count;
751 bool lun_present=true;
752 unsigned char lun = cbw->lun;
753 unsigned int block_size_mult = 1;
755 if(letoh32(cbw->signature) != CBW_SIGNATURE) {
756 usb_drv_stall(ep_in, true,true);
757 usb_drv_stall(ep_out, true,false);
758 return;
760 /* Clear the signature to prevent possible bugs elsewhere
761 * to trigger a second execution of the same command with
762 * bogus data */
763 cbw->signature=0;
765 #if defined(HAVE_MULTIDRIVE)
766 if(skip_first) lun++;
767 #endif
769 storage_get_info(lun,&info);
770 #ifdef USB_USE_RAMDISK
771 block_size = SECTOR_SIZE;
772 block_count = RAMDISK_SIZE;
773 #else
774 block_size=info.sector_size;
775 block_count=info.num_sectors;
776 #endif
778 #ifdef HAVE_HOTSWAP
779 if(storage_removable(lun) && !storage_present(lun)) {
780 ejected[lun] = true;
782 #endif
784 if(ejected[lun])
785 lun_present = false;
787 #ifdef MAX_LOG_SECTOR_SIZE
788 block_size_mult = disk_get_sector_multiplier(IF_MD(lun));
789 #endif
791 cur_cmd.tag = cbw->tag;
792 cur_cmd.lun = lun;
793 cur_cmd.cur_cmd = cbw->command_block[0];
795 switch (cbw->command_block[0]) {
796 case SCSI_TEST_UNIT_READY:
797 logf("scsi test_unit_ready %d",lun);
798 if(!usb_exclusive_storage()) {
799 send_csw(UMS_STATUS_FAIL);
800 cur_sense_data.sense_key=SENSE_NOT_READY;
801 cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
802 cur_sense_data.ascq=0;
803 break;
805 if(lun_present) {
806 send_csw(UMS_STATUS_GOOD);
808 else {
809 send_csw(UMS_STATUS_FAIL);
810 cur_sense_data.sense_key=SENSE_NOT_READY;
811 cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
812 cur_sense_data.ascq=0;
814 break;
816 case SCSI_REPORT_LUNS: {
817 logf("scsi report luns %d",lun);
818 int allocation_length=0;
819 int i;
820 unsigned int response_length = 8+8*storage_num_drives();
821 allocation_length|=(cbw->command_block[6]<<24);
822 allocation_length|=(cbw->command_block[7]<<16);
823 allocation_length|=(cbw->command_block[8]<<8);
824 allocation_length|=(cbw->command_block[9]);
825 memset(tb.lun_data,0,sizeof(struct report_lun_data));
826 tb.lun_data->lun_list_length=htobe32(8*storage_num_drives());
827 for(i=0;i<storage_num_drives();i++)
829 #ifdef HAVE_HOTSWAP
830 if(storage_removable(i))
831 tb.lun_data->luns[i][1]=1;
832 else
833 #endif
834 tb.lun_data->luns[i][1]=0;
836 send_command_result(tb.lun_data,
837 MIN(response_length, length));
838 break;
841 case SCSI_INQUIRY:
842 logf("scsi inquiry %d",lun);
843 fill_inquiry(IF_MD(lun));
844 length = MIN(length, cbw->command_block[4]);
845 send_command_result(tb.inquiry,
846 MIN(sizeof(struct inquiry_data), length));
847 break;
849 case SCSI_REQUEST_SENSE: {
850 tb.sense_data->ResponseCode=0x70;/*current error*/
851 tb.sense_data->Obsolete=0;
852 tb.sense_data->fei_sensekey=cur_sense_data.sense_key&0x0f;
853 tb.sense_data->Information=cur_sense_data.information;
854 tb.sense_data->AdditionalSenseLength=10;
855 tb.sense_data->CommandSpecificInformation=0;
856 tb.sense_data->AdditionalSenseCode=cur_sense_data.asc;
857 tb.sense_data->AdditionalSenseCodeQualifier=cur_sense_data.ascq;
858 tb.sense_data->FieldReplaceableUnitCode=0;
859 tb.sense_data->SKSV=0;
860 tb.sense_data->SenseKeySpecific=0;
861 logf("scsi request_sense %d",lun);
862 send_command_result(tb.sense_data,
863 MIN(sizeof(struct sense_data), length));
864 break;
867 case SCSI_MODE_SENSE_10: {
868 if(!lun_present) {
869 send_command_failed_result();
870 cur_sense_data.sense_key=SENSE_NOT_READY;
871 cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
872 cur_sense_data.ascq=0;
873 break;
875 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
876 unsigned char page_code = cbw->command_block[2] & 0x3f;
877 logf("scsi mode_sense_10 %d %X",lun,page_code);
878 switch(page_code) {
879 case 0x3f:
880 tb.ms_data_10->mode_data_length =
881 htobe16(sizeof(struct mode_sense_data_10)-2);
882 tb.ms_data_10->medium_type = 0;
883 tb.ms_data_10->device_specific = 0;
884 tb.ms_data_10->reserved = 0;
885 tb.ms_data_10->longlba = 1;
886 tb.ms_data_10->block_descriptor_length =
887 htobe16(sizeof(struct mode_sense_bdesc_longlba));
889 memset(tb.ms_data_10->block_descriptor.reserved,0,4);
890 memset(tb.ms_data_10->block_descriptor.num_blocks,0,8);
892 tb.ms_data_10->block_descriptor.num_blocks[4] =
893 ((block_count/block_size_mult) & 0xff000000)>>24;
894 tb.ms_data_10->block_descriptor.num_blocks[5] =
895 ((block_count/block_size_mult) & 0x00ff0000)>>16;
896 tb.ms_data_10->block_descriptor.num_blocks[6] =
897 ((block_count/block_size_mult) & 0x0000ff00)>>8;
898 tb.ms_data_10->block_descriptor.num_blocks[7] =
899 ((block_count/block_size_mult) & 0x000000ff);
901 tb.ms_data_10->block_descriptor.block_size[0] =
902 ((block_size*block_size_mult) & 0xff000000)>>24;
903 tb.ms_data_10->block_descriptor.block_size[1] =
904 ((block_size*block_size_mult) & 0x00ff0000)>>16;
905 tb.ms_data_10->block_descriptor.block_size[2] =
906 ((block_size*block_size_mult) & 0x0000ff00)>>8;
907 tb.ms_data_10->block_descriptor.block_size[3] =
908 ((block_size*block_size_mult) & 0x000000ff);
909 send_command_result(tb.ms_data_10,
910 MIN(sizeof(struct mode_sense_data_10), length));
911 break;
912 default:
913 send_command_failed_result();
914 cur_sense_data.sense_key=SENSE_ILLEGAL_REQUEST;
915 cur_sense_data.asc=ASC_INVALID_FIELD_IN_CBD;
916 cur_sense_data.ascq=0;
917 break;
919 break;
922 case SCSI_MODE_SENSE_6: {
923 if(!lun_present) {
924 send_command_failed_result();
925 cur_sense_data.sense_key=SENSE_NOT_READY;
926 cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
927 cur_sense_data.ascq=0;
928 break;
930 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
931 unsigned char page_code = cbw->command_block[2] & 0x3f;
932 logf("scsi mode_sense_6 %d %X",lun,page_code);
933 switch(page_code) {
934 case 0x3f:
935 /* All supported pages. */
936 tb.ms_data_6->mode_data_length =
937 sizeof(struct mode_sense_data_6)-1;
938 tb.ms_data_6->medium_type = 0;
939 tb.ms_data_6->device_specific = 0;
940 tb.ms_data_6->block_descriptor_length =
941 sizeof(struct mode_sense_bdesc_shortlba);
942 tb.ms_data_6->block_descriptor.density_code = 0;
943 tb.ms_data_6->block_descriptor.reserved = 0;
944 if(block_count/block_size_mult > 0xffffff) {
945 tb.ms_data_6->block_descriptor.num_blocks[0] = 0xff;
946 tb.ms_data_6->block_descriptor.num_blocks[1] = 0xff;
947 tb.ms_data_6->block_descriptor.num_blocks[2] = 0xff;
949 else {
950 tb.ms_data_6->block_descriptor.num_blocks[0] =
951 ((block_count/block_size_mult) & 0xff0000)>>16;
952 tb.ms_data_6->block_descriptor.num_blocks[1] =
953 ((block_count/block_size_mult) & 0x00ff00)>>8;
954 tb.ms_data_6->block_descriptor.num_blocks[2] =
955 ((block_count/block_size_mult) & 0x0000ff);
957 tb.ms_data_6->block_descriptor.block_size[0] =
958 ((block_size*block_size_mult) & 0xff0000)>>16;
959 tb.ms_data_6->block_descriptor.block_size[1] =
960 ((block_size*block_size_mult) & 0x00ff00)>>8;
961 tb.ms_data_6->block_descriptor.block_size[2] =
962 ((block_size*block_size_mult) & 0x0000ff);
963 send_command_result(tb.ms_data_6,
964 MIN(sizeof(struct mode_sense_data_6), length));
965 break;
966 default:
967 send_command_failed_result();
968 cur_sense_data.sense_key=SENSE_ILLEGAL_REQUEST;
969 cur_sense_data.asc=ASC_INVALID_FIELD_IN_CBD;
970 cur_sense_data.ascq=0;
971 break;
973 break;
976 case SCSI_START_STOP_UNIT:
977 logf("scsi start_stop unit %d",lun);
978 if((cbw->command_block[4] & 0xf0) == 0) /*load/eject bit is valid*/
979 { /* Process start and eject bits */
980 logf("scsi load/eject");
981 if((cbw->command_block[4] & 0x01) == 0) /* Don't start */
983 if((cbw->command_block[4] & 0x02) != 0) /* eject */
985 logf("scsi eject");
986 ejected[lun]=true;
990 send_csw(UMS_STATUS_GOOD);
991 break;
993 case SCSI_ALLOW_MEDIUM_REMOVAL:
994 logf("scsi allow_medium_removal %d",lun);
995 if((cbw->command_block[4] & 0x03) == 0) {
996 locked[lun]=false;
997 queue_broadcast(SYS_USB_LUN_LOCKED, (lun<<16)+0);
999 else {
1000 locked[lun]=true;
1001 queue_broadcast(SYS_USB_LUN_LOCKED, (lun<<16)+1);
1003 send_csw(UMS_STATUS_GOOD);
1004 break;
1006 case SCSI_READ_FORMAT_CAPACITY: {
1007 logf("scsi read_format_capacity %d",lun);
1008 if(lun_present) {
1009 tb.format_capacity_data->following_length=htobe32(8);
1010 /* "block count" actually means "number of last block" */
1011 tb.format_capacity_data->block_count =
1012 htobe32(block_count/block_size_mult - 1);
1013 tb.format_capacity_data->block_size =
1014 htobe32(block_size*block_size_mult);
1015 tb.format_capacity_data->block_size |=
1016 htobe32(SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA);
1018 send_command_result(tb.format_capacity_data,
1019 MIN(sizeof(struct format_capacity), length));
1021 else {
1022 send_command_failed_result();
1023 cur_sense_data.sense_key=SENSE_NOT_READY;
1024 cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
1025 cur_sense_data.ascq=0;
1027 break;
1030 case SCSI_READ_CAPACITY: {
1031 logf("scsi read_capacity %d",lun);
1033 if(lun_present) {
1034 /* "block count" actually means "number of last block" */
1035 tb.capacity_data->block_count =
1036 htobe32(block_count/block_size_mult - 1);
1037 tb.capacity_data->block_size =
1038 htobe32(block_size*block_size_mult);
1040 send_command_result(tb.capacity_data,
1041 MIN(sizeof(struct capacity), length));
1043 else {
1044 send_command_failed_result();
1045 cur_sense_data.sense_key=SENSE_NOT_READY;
1046 cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
1047 cur_sense_data.ascq=0;
1049 break;
1052 case SCSI_READ_10:
1053 logf("scsi read10 %d",lun);
1054 if(!lun_present) {
1055 send_command_failed_result();
1056 cur_sense_data.sense_key=SENSE_NOT_READY;
1057 cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
1058 cur_sense_data.ascq=0;
1059 break;
1061 cur_cmd.data[0] = tb.transfer_buffer;
1062 cur_cmd.data[1] = &tb.transfer_buffer[READ_BUFFER_SIZE];
1063 cur_cmd.data_select=0;
1064 cur_cmd.sector = block_size_mult *
1065 (cbw->command_block[2] << 24 |
1066 cbw->command_block[3] << 16 |
1067 cbw->command_block[4] << 8 |
1068 cbw->command_block[5] );
1069 cur_cmd.count = block_size_mult *
1070 (cbw->command_block[7] << 8 |
1071 cbw->command_block[8]);
1072 cur_cmd.orig_count = cur_cmd.count;
1074 //logf("scsi read %d %d", cur_cmd.sector, cur_cmd.count);
1076 if((cur_cmd.sector + cur_cmd.count) > block_count) {
1077 send_csw(UMS_STATUS_FAIL);
1078 cur_sense_data.sense_key=SENSE_ILLEGAL_REQUEST;
1079 cur_sense_data.asc=ASC_LBA_OUT_OF_RANGE;
1080 cur_sense_data.ascq=0;
1082 else {
1083 #ifdef USB_USE_RAMDISK
1084 memcpy(cur_cmd.data[cur_cmd.data_select],
1085 ramdisk_buffer + cur_cmd.sector*SECTOR_SIZE,
1086 MIN(READ_BUFFER_SIZE/SECTOR_SIZE,cur_cmd.count)*SECTOR_SIZE);
1087 #else
1088 cur_cmd.last_result = storage_read_sectors(IF_MD2(cur_cmd.lun,)
1089 cur_cmd.sector,
1090 MIN(READ_BUFFER_SIZE/SECTOR_SIZE, cur_cmd.count),
1091 cur_cmd.data[cur_cmd.data_select]);
1092 #endif
1093 send_and_read_next();
1095 break;
1097 case SCSI_WRITE_10:
1098 logf("scsi write10 %d",lun);
1099 if(!lun_present) {
1100 send_csw(UMS_STATUS_FAIL);
1101 cur_sense_data.sense_key=SENSE_NOT_READY;
1102 cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
1103 cur_sense_data.ascq=0;
1104 break;
1106 cur_cmd.data[0] = tb.transfer_buffer;
1107 cur_cmd.data[1] = &tb.transfer_buffer[WRITE_BUFFER_SIZE];
1108 cur_cmd.data_select=0;
1109 cur_cmd.sector = block_size_mult *
1110 (cbw->command_block[2] << 24 |
1111 cbw->command_block[3] << 16 |
1112 cbw->command_block[4] << 8 |
1113 cbw->command_block[5] );
1114 cur_cmd.count = block_size_mult *
1115 (cbw->command_block[7] << 8 |
1116 cbw->command_block[8]);
1117 cur_cmd.orig_count = cur_cmd.count;
1119 /* expect data */
1120 if((cur_cmd.sector + cur_cmd.count) > block_count) {
1121 send_csw(UMS_STATUS_FAIL);
1122 cur_sense_data.sense_key=SENSE_ILLEGAL_REQUEST;
1123 cur_sense_data.asc=ASC_LBA_OUT_OF_RANGE;
1124 cur_sense_data.ascq=0;
1126 else {
1127 receive_block_data(cur_cmd.data[0],
1128 MIN(WRITE_BUFFER_SIZE, cur_cmd.count*SECTOR_SIZE));
1130 break;
1132 #if CONFIG_RTC
1133 case SCSI_WRITE_BUFFER:
1134 if(cbw->command_block[1]==1 /* mode = vendor specific */
1135 && cbw->command_block[2]==0 /* buffer id = 0 */
1137 && cbw->command_block[3]==0x0c /* offset (3 bytes) */
1138 && cbw->command_block[4]==0
1139 && cbw->command_block[5]==0
1141 /* Some versions of itunes set the parameter list length to 0.
1142 * Technically it should be 0x0c, which is what libgpod sends */
1143 && cbw->command_block[6]==0 /* parameter list (3 bytes) */
1144 && cbw->command_block[7]==0
1145 && (cbw->command_block[8]==0 || cbw->command_block[8]==0x0c)
1147 && cbw->command_block[9]==0)
1148 receive_time();
1149 break;
1150 #endif /* CONFIG_RTC */
1152 default:
1153 logf("scsi unknown cmd %x",cbw->command_block[0x0]);
1154 send_csw(UMS_STATUS_FAIL);
1155 cur_sense_data.sense_key=SENSE_ILLEGAL_REQUEST;
1156 cur_sense_data.asc=ASC_INVALID_COMMAND;
1157 cur_sense_data.ascq=0;
1158 break;
1162 static void send_block_data(void *data,int size)
1164 usb_drv_send_nonblocking(ep_in, data,size);
1165 state = SENDING_BLOCKS;
1168 static void send_command_result(void *data,int size)
1170 usb_drv_send_nonblocking(ep_in, data,size);
1171 state = SENDING_RESULT;
1174 static void send_command_failed_result(void)
1176 usb_drv_send_nonblocking(ep_in, NULL, 0);
1177 state = SENDING_FAILED_RESULT;
1180 #if CONFIG_RTC
1181 static void receive_time(void)
1183 usb_drv_recv(ep_out, tb.transfer_buffer, 12);
1184 state = RECEIVING_TIME;
1186 #endif /* CONFIG_RTC */
1188 static void receive_block_data(void *data,int size)
1190 usb_drv_recv(ep_out, data, size);
1191 state = RECEIVING_BLOCKS;
1194 static void send_csw(int status)
1196 tb.csw->signature = htole32(CSW_SIGNATURE);
1197 tb.csw->tag = cur_cmd.tag;
1198 tb.csw->data_residue = 0;
1199 tb.csw->status = status;
1201 usb_drv_send_nonblocking(ep_in, tb.csw,
1202 sizeof(struct command_status_wrapper));
1203 state = WAITING_FOR_CSW_COMPLETION_OR_COMMAND;
1204 //logf("CSW: %X",status);
1205 /* Already start waiting for the next command */
1206 usb_drv_recv(ep_out, cbw_buffer, MAX_CBW_SIZE);
1207 /* The next completed transfer will be either the CSW one
1208 * or the new command */
1210 if(status == UMS_STATUS_GOOD) {
1211 cur_sense_data.sense_key=0;
1212 cur_sense_data.information=0;
1213 cur_sense_data.asc=0;
1214 cur_sense_data.ascq=0;
1218 static void copy_padded(char *dest, char *src, int len)
1220 for (int i = 0; i < len; i++) {
1221 if (src[i] == '\0') {
1222 memset(&dest[i], ' ', len - i);
1223 return;
1225 dest[i] = src[i];
1229 /* build SCSI INQUIRY */
1230 static void fill_inquiry(IF_MD_NONVOID(int lun))
1232 struct storage_info info;
1233 memset(tb.inquiry, 0, sizeof(struct inquiry_data));
1234 storage_get_info(lun,&info);
1235 copy_padded(tb.inquiry->VendorId,info.vendor,sizeof(tb.inquiry->VendorId));
1236 copy_padded(tb.inquiry->ProductId,info.product,sizeof(tb.inquiry->ProductId));
1237 copy_padded(tb.inquiry->ProductRevisionLevel,info.revision,sizeof(tb.inquiry->ProductRevisionLevel));
1239 tb.inquiry->DeviceType = DIRECT_ACCESS_DEVICE;
1240 tb.inquiry->AdditionalLength = 0x1f;
1241 memset(tb.inquiry->Reserved, 0, 3);
1242 tb.inquiry->Versions = 4; /* SPC-2 */
1243 tb.inquiry->Format = 2; /* SPC-2/3 inquiry format */
1245 tb.inquiry->DeviceTypeModifier = DEVICE_REMOVABLE;