1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
25 #include "usb_class_driver.h"
26 /*#define LOGF_ENABLE*/
31 /* Needed to get at the audio buffer */
33 #include "usb_storage.h"
35 #include "timefuncs.h"
38 #ifdef USB_USE_RAMDISK
39 #define RAMDISK_SIZE 2048
43 #define SECTOR_SIZE 512
46 /* These defaults allow the operation */
47 #ifndef USBSTOR_READ_SECTORS_FILTER
48 #define USBSTOR_READ_SECTORS_FILTER() ({ 0; })
51 #ifndef USBSTOR_WRITE_SECTORS_FILTER
52 #define USBSTOR_WRITE_SECTORS_FILTER() ({ 0; })
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
64 #ifdef USB_READ_BUFFER_SIZE
65 #define READ_BUFFER_SIZE USB_READ_BUFFER_SIZE
67 #if CONFIG_USBOTG == USBOTG_AS3525
68 /* We'd need to implement multidescriptor dma for sizes >65535 */
69 #define READ_BUFFER_SIZE (1024*63)
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
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)
85 #define WRITE_BUFFER_SIZE (1024*64)
86 #endif /* CONFIG_CPU == AS3525 */
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,
151 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
152 .bInterfaceSubClass
= USB_SC_SCSI
,
153 .bInterfaceProtocol
= USB_PROT_BULK
,
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
,
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
));
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
;
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
;
235 unsigned int data_transfer_length
;
238 unsigned char command_length
;
239 unsigned char command_block
[16];
240 } __attribute__ ((packed
));
242 struct command_status_wrapper
{
243 unsigned int signature
;
245 unsigned int data_residue
;
246 unsigned char status
;
247 } __attribute__ ((packed
));
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
));
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
;
274 static char *cbw_buffer
;
279 unsigned int orig_count
;
280 unsigned int cur_cmd
;
283 unsigned char *data
[2];
284 unsigned char data_select
;
285 unsigned int last_result
;
289 unsigned char sense_key
;
290 unsigned char information
;
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
);
302 static void receive_time(void);
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;
316 #ifdef USB_USE_RAMDISK
317 static unsigned char* ramdisk_buffer
;
324 SENDING_FAILED_RESULT
,
329 WAITING_FOR_CSW_COMPLETION_OR_COMMAND
,
330 WAITING_FOR_CSW_COMPLETION
331 } state
= WAITING_FOR_COMMAND
;
334 static void yearday_to_daymonth(int yd
, int y
, int *d
, int *m
)
336 static const char tnl
[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
337 static const char tl
[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
341 if((y
%4 == 0 && y
%100 != 0) || y
%400 == 0)
350 while(i
<12 && yd
>= t
[i
])
358 #endif /* CONFIG_RTC */
360 static bool check_disk_present(IF_MD_NONVOID(int volume
))
362 #ifdef USB_USE_RAMDISK
365 unsigned char* sector
= fat_get_sector_buffer();
366 bool success
= storage_read_sectors(IF_MD2(volume
,)0,1,sector
) == 0;
367 fat_release_sector_buffer();
373 void usb_storage_notify_hotswap(int volume
,bool inserted
)
375 logf("notify %d",inserted
);
376 if(inserted
&& check_disk_present(IF_MD(volume
))) {
377 ejected
[volume
] = false;
380 ejected
[volume
] = true;
381 /* If this happens while the device is locked, weird things may happen.
382 At least try to keep our state consistent */
383 locked
[volume
]=false;
388 #ifdef HAVE_MULTIDRIVE
389 void usb_set_skip_first_drive(bool skip
)
395 /* called by usb_core_init() */
396 void usb_storage_init(void)
398 logf("usb_storage_init done");
401 int usb_storage_request_endpoints(struct usb_class_driver
*drv
)
403 ep_in
= usb_core_request_endpoint(USB_ENDPOINT_XFER_BULK
, USB_DIR_IN
, drv
);
408 ep_out
= usb_core_request_endpoint(USB_ENDPOINT_XFER_BULK
, USB_DIR_OUT
,
412 usb_core_release_endpoint(ep_in
);
419 int usb_storage_set_first_interface(int interface
)
421 usb_interface
= interface
;
422 return interface
+ 1;
425 int usb_storage_get_config_descriptor(unsigned char *dest
,int max_packet_size
)
427 unsigned char *orig_dest
= dest
;
429 interface_descriptor
.bInterfaceNumber
= usb_interface
;
430 PACK_DATA(&dest
, interface_descriptor
);
432 endpoint_descriptor
.wMaxPacketSize
= max_packet_size
;
434 endpoint_descriptor
.bEndpointAddress
= ep_in
;
435 PACK_DATA(&dest
, endpoint_descriptor
);
437 endpoint_descriptor
.bEndpointAddress
= ep_out
;
438 PACK_DATA(&dest
, endpoint_descriptor
);
440 return (dest
- orig_dest
);
443 void usb_storage_init_connection(void)
445 logf("ums: set config");
446 /* prime rx endpoint. We only need room for commands */
447 state
= WAITING_FOR_COMMAND
;
449 #if (CONFIG_CPU == IMX31L || defined(CPU_TCC77X) || defined(CPU_TCC780X) || \
450 defined(BOOTLOADER) || CONFIG_CPU == DM320) && !defined(CPU_PP502x)
451 static unsigned char _cbw_buffer
[MAX_CBW_SIZE
]
452 USB_DEVBSS_ATTR
__attribute__((aligned(32)));
453 cbw_buffer
= (void *)_cbw_buffer
;
455 static unsigned char _transfer_buffer
[ALLOCATE_BUFFER_SIZE
]
456 USB_DEVBSS_ATTR
__attribute__((aligned(32)));
457 tb
.transfer_buffer
= (void *)_transfer_buffer
;
458 #ifdef USB_USE_RAMDISK
459 static unsigned char _ramdisk_buffer
[RAMDISK_SIZE
*SECTOR_SIZE
];
460 ramdisk_buffer
= _ramdisk_buffer
;
463 /* TODO : check if bufsize is at least 32K ? */
465 unsigned char * audio_buffer
;
467 audio_buffer
= audio_get_buffer(false,&bufsize
);
468 #if defined(UNCACHED_ADDR) && CONFIG_CPU != AS3525
469 cbw_buffer
= (void *)UNCACHED_ADDR((unsigned int)(audio_buffer
+31) & 0xffffffe0);
471 cbw_buffer
= (void *)((unsigned int)(audio_buffer
+31) & 0xffffffe0);
473 tb
.transfer_buffer
= cbw_buffer
+ MAX_CBW_SIZE
;
474 commit_discard_dcache();
475 #ifdef USB_USE_RAMDISK
476 ramdisk_buffer
= tb
.transfer_buffer
+ ALLOCATE_BUFFER_SIZE
;
479 usb_drv_recv(ep_out
, cbw_buffer
, MAX_CBW_SIZE
);
482 for(i
=0;i
<storage_num_drives();i
++) {
484 ejected
[i
] = !check_disk_present(IF_MD(i
));
485 queue_broadcast(SYS_USB_LUN_LOCKED
, (i
<<16)+0);
489 void usb_storage_disconnect(void)
494 /* called by usb_core_transfer_complete() */
495 void usb_storage_transfer_complete(int ep
,int dir
,int status
,int length
)
498 struct command_block_wrapper
* cbw
= (void*)cbw_buffer
;
503 logf("transfer result for ep %d/%d %X %d", ep
,dir
,status
, length
);
505 case RECEIVING_BLOCKS
:
506 if(dir
==USB_DIR_IN
) {
507 logf("IN received in RECEIVING");
509 logf("scsi write %d %d", cur_cmd
.sector
, cur_cmd
.count
);
511 if((unsigned int)length
!=(SECTOR_SIZE
* cur_cmd
.count
)
512 && (unsigned int)length
!=WRITE_BUFFER_SIZE
) {
513 logf("unexpected length :%d",length
);
517 unsigned int next_sector
= cur_cmd
.sector
+
518 (WRITE_BUFFER_SIZE
/SECTOR_SIZE
);
519 unsigned int next_count
= cur_cmd
.count
-
520 MIN(cur_cmd
.count
,WRITE_BUFFER_SIZE
/SECTOR_SIZE
);
521 int next_select
= !cur_cmd
.data_select
;
524 /* Ask the host to send more, to the other buffer */
525 receive_block_data(cur_cmd
.data
[next_select
],
526 MIN(WRITE_BUFFER_SIZE
,next_count
*SECTOR_SIZE
));
529 /* Now write the data that just came in, while the host is
530 sending the next bit */
531 #ifdef USB_USE_RAMDISK
532 memcpy(ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
533 cur_cmd
.data
[cur_cmd
.data_select
],
534 MIN(WRITE_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
)*SECTOR_SIZE
);
536 int result
= USBSTOR_WRITE_SECTORS_FILTER();
539 result
= storage_write_sectors(IF_MD2(cur_cmd
.lun
,)
541 MIN(WRITE_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
),
542 cur_cmd
.data
[cur_cmd
.data_select
]);
546 send_csw(UMS_STATUS_FAIL
);
547 cur_sense_data
.sense_key
=SENSE_MEDIUM_ERROR
;
548 cur_sense_data
.asc
=ASC_WRITE_ERROR
;
549 cur_sense_data
.ascq
=0;
554 send_csw(UMS_STATUS_GOOD
);
557 /* Switch buffers for the next one */
558 cur_cmd
.data_select
=!cur_cmd
.data_select
;
560 cur_cmd
.sector
= next_sector
;
561 cur_cmd
.count
= next_count
;
564 logf("Transfer failed %X",status
);
565 send_csw(UMS_STATUS_FAIL
);
566 /* TODO fill in cur_sense_data */
567 cur_sense_data
.sense_key
=0;
568 cur_sense_data
.information
=0;
569 cur_sense_data
.asc
=0;
570 cur_sense_data
.ascq
=0;
573 case WAITING_FOR_CSW_COMPLETION_OR_COMMAND
:
574 if(dir
==USB_DIR_IN
) {
575 /* This was the CSW */
576 state
= WAITING_FOR_COMMAND
;
579 /* This was the command */
580 state
= WAITING_FOR_CSW_COMPLETION
;
581 /* We now have the CBW, but we won't execute it yet to avoid
582 * issues with the still-pending CSW */
585 case WAITING_FOR_COMMAND
:
586 if(dir
==USB_DIR_IN
) {
587 logf("IN received in WAITING_FOR_COMMAND");
591 case WAITING_FOR_CSW_COMPLETION
:
592 if(dir
==USB_DIR_OUT
) {
593 logf("OUT received in WAITING_FOR_CSW_COMPLETION");
598 if(cur_cmd
.cur_cmd
== SCSI_WRITE_10
)
600 queue_broadcast(SYS_USB_WRITE_DATA
, (cur_cmd
.lun
<<16)+cur_cmd
.orig_count
);
602 else if(cur_cmd
.cur_cmd
== SCSI_READ_10
)
604 queue_broadcast(SYS_USB_READ_DATA
, (cur_cmd
.lun
<<16)+cur_cmd
.orig_count
);
609 if(dir
==USB_DIR_OUT
) {
610 logf("OUT received in SENDING");
613 //logf("data sent, now send csw");
614 send_csw(UMS_STATUS_GOOD
);
617 logf("Transfer failed %X",status
);
618 send_csw(UMS_STATUS_FAIL
);
619 /* TODO fill in cur_sense_data */
620 cur_sense_data
.sense_key
=0;
621 cur_sense_data
.information
=0;
622 cur_sense_data
.asc
=0;
623 cur_sense_data
.ascq
=0;
626 case SENDING_FAILED_RESULT
:
627 if(dir
==USB_DIR_OUT
) {
628 logf("OUT received in SENDING");
630 send_csw(UMS_STATUS_FAIL
);
633 if(dir
==USB_DIR_OUT
) {
634 logf("OUT received in SENDING");
637 if(cur_cmd
.count
==0) {
638 //logf("data sent, now send csw");
639 if(cur_cmd
.last_result
!=0) {
640 /* The last read failed. */
641 send_csw(UMS_STATUS_FAIL
);
642 cur_sense_data
.sense_key
=SENSE_MEDIUM_ERROR
;
643 cur_sense_data
.asc
=ASC_READ_ERROR
;
644 cur_sense_data
.ascq
=0;
648 send_csw(UMS_STATUS_GOOD
);
651 send_and_read_next();
655 logf("Transfer failed %X",status
);
656 send_csw(UMS_STATUS_FAIL
);
657 /* TODO fill in cur_sense_data */
658 cur_sense_data
.sense_key
=0;
659 cur_sense_data
.information
=0;
660 cur_sense_data
.asc
=0;
661 cur_sense_data
.ascq
=0;
666 tm
.tm_year
=(tb
.transfer_buffer
[0]<<8)+tb
.transfer_buffer
[1] - 1900;
667 tm
.tm_yday
=(tb
.transfer_buffer
[2]<<8)+tb
.transfer_buffer
[3];
668 tm
.tm_hour
=tb
.transfer_buffer
[5];
669 tm
.tm_min
=tb
.transfer_buffer
[6];
670 tm
.tm_sec
=tb
.transfer_buffer
[7];
671 yearday_to_daymonth(tm
.tm_yday
,tm
.tm_year
+ 1900,&tm
.tm_mday
,&tm
.tm_mon
);
672 set_day_of_week(&tm
);
674 send_csw(UMS_STATUS_GOOD
);
676 #endif /* CONFIG_RTC */
680 /* called by usb_core_control_request() */
681 bool usb_storage_control_request(struct usb_ctrlrequest
* req
, unsigned char* dest
)
683 bool handled
= false;
686 switch (req
->bRequest
) {
687 case USB_BULK_GET_MAX_LUN
: {
688 *tb
.max_lun
= storage_num_drives() - 1;
689 #if defined(HAVE_MULTIDRIVE)
690 if(skip_first
) (*tb
.max_lun
) --;
692 logf("ums: getmaxlun");
693 usb_drv_recv(EP_CONTROL
, NULL
, 0); /* ack */
694 usb_drv_send(EP_CONTROL
, tb
.max_lun
, 1);
699 case USB_BULK_RESET_REQUEST
:
700 logf("ums: bulk reset");
701 state
= WAITING_FOR_COMMAND
;
702 /* UMS BOT 3.1 says The device shall preserve the value of its bulk
703 data toggle bits and endpoint STALL conditions despite
704 the Bulk-Only Mass Storage Reset. */
706 usb_drv_reset_endpoint(ep_in
, false);
707 usb_drv_reset_endpoint(ep_out
, true);
709 usb_drv_send(EP_CONTROL
, NULL
, 0); /* ack */
717 static void send_and_read_next(void)
719 int result
= USBSTOR_READ_SECTORS_FILTER();
721 if(result
!= 0 && cur_cmd
.last_result
== 0)
722 cur_cmd
.last_result
= result
;
724 send_block_data(cur_cmd
.data
[cur_cmd
.data_select
],
725 MIN(READ_BUFFER_SIZE
,cur_cmd
.count
*SECTOR_SIZE
));
727 /* Switch buffers for the next one */
728 cur_cmd
.data_select
=!cur_cmd
.data_select
;
730 cur_cmd
.sector
+=(READ_BUFFER_SIZE
/SECTOR_SIZE
);
731 cur_cmd
.count
-=MIN(cur_cmd
.count
,READ_BUFFER_SIZE
/SECTOR_SIZE
);
733 if(cur_cmd
.count
!=0) {
734 /* already read the next bit, so we can send it out immediately when the
735 * current transfer completes. */
736 #ifdef USB_USE_RAMDISK
737 memcpy(cur_cmd
.data
[cur_cmd
.data_select
],
738 ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
739 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
)*SECTOR_SIZE
);
741 result
= storage_read_sectors(IF_MD2(cur_cmd
.lun
,)
743 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
),
744 cur_cmd
.data
[cur_cmd
.data_select
]);
745 if(cur_cmd
.last_result
== 0)
746 cur_cmd
.last_result
= result
;
751 /****************************************************************************/
753 static void handle_scsi(struct command_block_wrapper
* cbw
)
755 /* USB Mass Storage assumes LBA capability.
756 TODO: support 48-bit LBA */
758 struct storage_info info
;
759 unsigned int length
= cbw
->data_transfer_length
;
760 unsigned int block_size
= 0;
761 unsigned int block_count
= 0;
762 bool lun_present
=true;
763 unsigned char lun
= cbw
->lun
;
764 unsigned int block_size_mult
= 1;
766 if(letoh32(cbw
->signature
) != CBW_SIGNATURE
) {
767 usb_drv_stall(ep_in
, true,true);
768 usb_drv_stall(ep_out
, true,false);
771 /* Clear the signature to prevent possible bugs elsewhere
772 * to trigger a second execution of the same command with
776 #if defined(HAVE_MULTIDRIVE)
777 if(skip_first
) lun
++;
780 storage_get_info(lun
,&info
);
781 #ifdef USB_USE_RAMDISK
782 block_size
= SECTOR_SIZE
;
783 block_count
= RAMDISK_SIZE
;
785 block_size
=info
.sector_size
;
786 block_count
=info
.num_sectors
;
790 if(storage_removable(lun
) && !storage_present(lun
)) {
798 #ifdef MAX_LOG_SECTOR_SIZE
799 block_size_mult
= disk_get_sector_multiplier(IF_MD(lun
));
802 cur_cmd
.tag
= cbw
->tag
;
804 cur_cmd
.cur_cmd
= cbw
->command_block
[0];
806 switch (cbw
->command_block
[0]) {
807 case SCSI_TEST_UNIT_READY
:
808 logf("scsi test_unit_ready %d",lun
);
809 if(!usb_exclusive_storage()) {
810 send_csw(UMS_STATUS_FAIL
);
811 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
812 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
813 cur_sense_data
.ascq
=0;
817 send_csw(UMS_STATUS_GOOD
);
820 send_csw(UMS_STATUS_FAIL
);
821 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
822 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
823 cur_sense_data
.ascq
=0;
827 case SCSI_REPORT_LUNS
: {
828 logf("scsi report luns %d",lun
);
829 int allocation_length
=0;
831 unsigned int response_length
= 8+8*storage_num_drives();
832 allocation_length
|=(cbw
->command_block
[6]<<24);
833 allocation_length
|=(cbw
->command_block
[7]<<16);
834 allocation_length
|=(cbw
->command_block
[8]<<8);
835 allocation_length
|=(cbw
->command_block
[9]);
836 memset(tb
.lun_data
,0,sizeof(struct report_lun_data
));
837 tb
.lun_data
->lun_list_length
=htobe32(8*storage_num_drives());
838 for(i
=0;i
<storage_num_drives();i
++)
841 if(storage_removable(i
))
842 tb
.lun_data
->luns
[i
][1]=1;
845 tb
.lun_data
->luns
[i
][1]=0;
847 send_command_result(tb
.lun_data
,
848 MIN(response_length
, length
));
853 logf("scsi inquiry %d",lun
);
854 fill_inquiry(IF_MD(lun
));
855 length
= MIN(length
, cbw
->command_block
[4]);
856 send_command_result(tb
.inquiry
,
857 MIN(sizeof(struct inquiry_data
), length
));
860 case SCSI_REQUEST_SENSE
: {
861 tb
.sense_data
->ResponseCode
=0x70;/*current error*/
862 tb
.sense_data
->Obsolete
=0;
863 tb
.sense_data
->fei_sensekey
=cur_sense_data
.sense_key
&0x0f;
864 tb
.sense_data
->Information
=cur_sense_data
.information
;
865 tb
.sense_data
->AdditionalSenseLength
=10;
866 tb
.sense_data
->CommandSpecificInformation
=0;
867 tb
.sense_data
->AdditionalSenseCode
=cur_sense_data
.asc
;
868 tb
.sense_data
->AdditionalSenseCodeQualifier
=cur_sense_data
.ascq
;
869 tb
.sense_data
->FieldReplaceableUnitCode
=0;
870 tb
.sense_data
->SKSV
=0;
871 tb
.sense_data
->SenseKeySpecific
=0;
872 logf("scsi request_sense %d",lun
);
873 send_command_result(tb
.sense_data
,
874 MIN(sizeof(struct sense_data
), length
));
878 case SCSI_MODE_SENSE_10
: {
880 send_command_failed_result();
881 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
882 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
883 cur_sense_data
.ascq
=0;
886 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
887 unsigned char page_code
= cbw
->command_block
[2] & 0x3f;
888 logf("scsi mode_sense_10 %d %X",lun
,page_code
);
891 tb
.ms_data_10
->mode_data_length
=
892 htobe16(sizeof(struct mode_sense_data_10
)-2);
893 tb
.ms_data_10
->medium_type
= 0;
894 tb
.ms_data_10
->device_specific
= 0;
895 tb
.ms_data_10
->reserved
= 0;
896 tb
.ms_data_10
->longlba
= 1;
897 tb
.ms_data_10
->block_descriptor_length
=
898 htobe16(sizeof(struct mode_sense_bdesc_longlba
));
900 memset(tb
.ms_data_10
->block_descriptor
.reserved
,0,4);
901 memset(tb
.ms_data_10
->block_descriptor
.num_blocks
,0,8);
903 tb
.ms_data_10
->block_descriptor
.num_blocks
[4] =
904 ((block_count
/block_size_mult
) & 0xff000000)>>24;
905 tb
.ms_data_10
->block_descriptor
.num_blocks
[5] =
906 ((block_count
/block_size_mult
) & 0x00ff0000)>>16;
907 tb
.ms_data_10
->block_descriptor
.num_blocks
[6] =
908 ((block_count
/block_size_mult
) & 0x0000ff00)>>8;
909 tb
.ms_data_10
->block_descriptor
.num_blocks
[7] =
910 ((block_count
/block_size_mult
) & 0x000000ff);
912 tb
.ms_data_10
->block_descriptor
.block_size
[0] =
913 ((block_size
*block_size_mult
) & 0xff000000)>>24;
914 tb
.ms_data_10
->block_descriptor
.block_size
[1] =
915 ((block_size
*block_size_mult
) & 0x00ff0000)>>16;
916 tb
.ms_data_10
->block_descriptor
.block_size
[2] =
917 ((block_size
*block_size_mult
) & 0x0000ff00)>>8;
918 tb
.ms_data_10
->block_descriptor
.block_size
[3] =
919 ((block_size
*block_size_mult
) & 0x000000ff);
920 send_command_result(tb
.ms_data_10
,
921 MIN(sizeof(struct mode_sense_data_10
), length
));
924 send_command_failed_result();
925 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
926 cur_sense_data
.asc
=ASC_INVALID_FIELD_IN_CBD
;
927 cur_sense_data
.ascq
=0;
933 case SCSI_MODE_SENSE_6
: {
935 send_command_failed_result();
936 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
937 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
938 cur_sense_data
.ascq
=0;
941 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
942 unsigned char page_code
= cbw
->command_block
[2] & 0x3f;
943 logf("scsi mode_sense_6 %d %X",lun
,page_code
);
946 /* All supported pages. */
947 tb
.ms_data_6
->mode_data_length
=
948 sizeof(struct mode_sense_data_6
)-1;
949 tb
.ms_data_6
->medium_type
= 0;
950 tb
.ms_data_6
->device_specific
= 0;
951 tb
.ms_data_6
->block_descriptor_length
=
952 sizeof(struct mode_sense_bdesc_shortlba
);
953 tb
.ms_data_6
->block_descriptor
.density_code
= 0;
954 tb
.ms_data_6
->block_descriptor
.reserved
= 0;
955 if(block_count
/block_size_mult
> 0xffffff) {
956 tb
.ms_data_6
->block_descriptor
.num_blocks
[0] = 0xff;
957 tb
.ms_data_6
->block_descriptor
.num_blocks
[1] = 0xff;
958 tb
.ms_data_6
->block_descriptor
.num_blocks
[2] = 0xff;
961 tb
.ms_data_6
->block_descriptor
.num_blocks
[0] =
962 ((block_count
/block_size_mult
) & 0xff0000)>>16;
963 tb
.ms_data_6
->block_descriptor
.num_blocks
[1] =
964 ((block_count
/block_size_mult
) & 0x00ff00)>>8;
965 tb
.ms_data_6
->block_descriptor
.num_blocks
[2] =
966 ((block_count
/block_size_mult
) & 0x0000ff);
968 tb
.ms_data_6
->block_descriptor
.block_size
[0] =
969 ((block_size
*block_size_mult
) & 0xff0000)>>16;
970 tb
.ms_data_6
->block_descriptor
.block_size
[1] =
971 ((block_size
*block_size_mult
) & 0x00ff00)>>8;
972 tb
.ms_data_6
->block_descriptor
.block_size
[2] =
973 ((block_size
*block_size_mult
) & 0x0000ff);
974 send_command_result(tb
.ms_data_6
,
975 MIN(sizeof(struct mode_sense_data_6
), length
));
978 send_command_failed_result();
979 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
980 cur_sense_data
.asc
=ASC_INVALID_FIELD_IN_CBD
;
981 cur_sense_data
.ascq
=0;
987 case SCSI_START_STOP_UNIT
:
988 logf("scsi start_stop unit %d",lun
);
989 if((cbw
->command_block
[4] & 0xf0) == 0) /*load/eject bit is valid*/
990 { /* Process start and eject bits */
991 logf("scsi load/eject");
992 if((cbw
->command_block
[4] & 0x01) == 0) /* Don't start */
994 if((cbw
->command_block
[4] & 0x02) != 0) /* eject */
1001 send_csw(UMS_STATUS_GOOD
);
1004 case SCSI_ALLOW_MEDIUM_REMOVAL
:
1005 logf("scsi allow_medium_removal %d",lun
);
1006 if((cbw
->command_block
[4] & 0x03) == 0) {
1008 queue_broadcast(SYS_USB_LUN_LOCKED
, (lun
<<16)+0);
1012 queue_broadcast(SYS_USB_LUN_LOCKED
, (lun
<<16)+1);
1014 send_csw(UMS_STATUS_GOOD
);
1017 case SCSI_READ_FORMAT_CAPACITY
: {
1018 logf("scsi read_format_capacity %d",lun
);
1020 tb
.format_capacity_data
->following_length
=htobe32(8);
1021 /* "block count" actually means "number of last block" */
1022 tb
.format_capacity_data
->block_count
=
1023 htobe32(block_count
/block_size_mult
- 1);
1024 tb
.format_capacity_data
->block_size
=
1025 htobe32(block_size
*block_size_mult
);
1026 tb
.format_capacity_data
->block_size
|=
1027 htobe32(SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA
);
1029 send_command_result(tb
.format_capacity_data
,
1030 MIN(sizeof(struct format_capacity
), length
));
1033 send_command_failed_result();
1034 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1035 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1036 cur_sense_data
.ascq
=0;
1041 case SCSI_READ_CAPACITY
: {
1042 logf("scsi read_capacity %d",lun
);
1045 /* "block count" actually means "number of last block" */
1046 tb
.capacity_data
->block_count
=
1047 htobe32(block_count
/block_size_mult
- 1);
1048 tb
.capacity_data
->block_size
=
1049 htobe32(block_size
*block_size_mult
);
1051 send_command_result(tb
.capacity_data
,
1052 MIN(sizeof(struct capacity
), length
));
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;
1064 logf("scsi read10 %d",lun
);
1066 send_command_failed_result();
1067 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1068 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1069 cur_sense_data
.ascq
=0;
1072 cur_cmd
.data
[0] = tb
.transfer_buffer
;
1073 cur_cmd
.data
[1] = &tb
.transfer_buffer
[READ_BUFFER_SIZE
];
1074 cur_cmd
.data_select
=0;
1075 cur_cmd
.sector
= block_size_mult
*
1076 (cbw
->command_block
[2] << 24 |
1077 cbw
->command_block
[3] << 16 |
1078 cbw
->command_block
[4] << 8 |
1079 cbw
->command_block
[5] );
1080 cur_cmd
.count
= block_size_mult
*
1081 (cbw
->command_block
[7] << 8 |
1082 cbw
->command_block
[8]);
1083 cur_cmd
.orig_count
= cur_cmd
.count
;
1085 //logf("scsi read %d %d", cur_cmd.sector, cur_cmd.count);
1087 if((cur_cmd
.sector
+ cur_cmd
.count
) > block_count
) {
1088 send_csw(UMS_STATUS_FAIL
);
1089 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1090 cur_sense_data
.asc
=ASC_LBA_OUT_OF_RANGE
;
1091 cur_sense_data
.ascq
=0;
1094 #ifdef USB_USE_RAMDISK
1095 memcpy(cur_cmd
.data
[cur_cmd
.data_select
],
1096 ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
1097 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
,cur_cmd
.count
)*SECTOR_SIZE
);
1099 cur_cmd
.last_result
= storage_read_sectors(IF_MD2(cur_cmd
.lun
,)
1101 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
),
1102 cur_cmd
.data
[cur_cmd
.data_select
]);
1104 send_and_read_next();
1109 logf("scsi write10 %d",lun
);
1111 send_csw(UMS_STATUS_FAIL
);
1112 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1113 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1114 cur_sense_data
.ascq
=0;
1117 cur_cmd
.data
[0] = tb
.transfer_buffer
;
1118 cur_cmd
.data
[1] = &tb
.transfer_buffer
[WRITE_BUFFER_SIZE
];
1119 cur_cmd
.data_select
=0;
1120 cur_cmd
.sector
= block_size_mult
*
1121 (cbw
->command_block
[2] << 24 |
1122 cbw
->command_block
[3] << 16 |
1123 cbw
->command_block
[4] << 8 |
1124 cbw
->command_block
[5] );
1125 cur_cmd
.count
= block_size_mult
*
1126 (cbw
->command_block
[7] << 8 |
1127 cbw
->command_block
[8]);
1128 cur_cmd
.orig_count
= cur_cmd
.count
;
1131 if((cur_cmd
.sector
+ cur_cmd
.count
) > block_count
) {
1132 send_csw(UMS_STATUS_FAIL
);
1133 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1134 cur_sense_data
.asc
=ASC_LBA_OUT_OF_RANGE
;
1135 cur_sense_data
.ascq
=0;
1138 receive_block_data(cur_cmd
.data
[0],
1139 MIN(WRITE_BUFFER_SIZE
, cur_cmd
.count
*SECTOR_SIZE
));
1144 case SCSI_WRITE_BUFFER
:
1145 if(cbw
->command_block
[1]==1 /* mode = vendor specific */
1146 && cbw
->command_block
[2]==0 /* buffer id = 0 */
1148 && cbw
->command_block
[3]==0x0c /* offset (3 bytes) */
1149 && cbw
->command_block
[4]==0
1150 && cbw
->command_block
[5]==0
1152 /* Some versions of itunes set the parameter list length to 0.
1153 * Technically it should be 0x0c, which is what libgpod sends */
1154 && cbw
->command_block
[6]==0 /* parameter list (3 bytes) */
1155 && cbw
->command_block
[7]==0
1156 && (cbw
->command_block
[8]==0 || cbw
->command_block
[8]==0x0c)
1158 && cbw
->command_block
[9]==0)
1161 #endif /* CONFIG_RTC */
1164 logf("scsi unknown cmd %x",cbw
->command_block
[0x0]);
1165 send_csw(UMS_STATUS_FAIL
);
1166 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1167 cur_sense_data
.asc
=ASC_INVALID_COMMAND
;
1168 cur_sense_data
.ascq
=0;
1173 static void send_block_data(void *data
,int size
)
1175 usb_drv_send_nonblocking(ep_in
, data
,size
);
1176 state
= SENDING_BLOCKS
;
1179 static void send_command_result(void *data
,int size
)
1181 usb_drv_send_nonblocking(ep_in
, data
,size
);
1182 state
= SENDING_RESULT
;
1185 static void send_command_failed_result(void)
1187 usb_drv_send_nonblocking(ep_in
, NULL
, 0);
1188 state
= SENDING_FAILED_RESULT
;
1192 static void receive_time(void)
1194 usb_drv_recv(ep_out
, tb
.transfer_buffer
, 12);
1195 state
= RECEIVING_TIME
;
1197 #endif /* CONFIG_RTC */
1199 static void receive_block_data(void *data
,int size
)
1201 usb_drv_recv(ep_out
, data
, size
);
1202 state
= RECEIVING_BLOCKS
;
1205 static void send_csw(int status
)
1207 tb
.csw
->signature
= htole32(CSW_SIGNATURE
);
1208 tb
.csw
->tag
= cur_cmd
.tag
;
1209 tb
.csw
->data_residue
= 0;
1210 tb
.csw
->status
= status
;
1212 usb_drv_send_nonblocking(ep_in
, tb
.csw
,
1213 sizeof(struct command_status_wrapper
));
1214 state
= WAITING_FOR_CSW_COMPLETION_OR_COMMAND
;
1215 //logf("CSW: %X",status);
1216 /* Already start waiting for the next command */
1217 usb_drv_recv(ep_out
, cbw_buffer
, MAX_CBW_SIZE
);
1218 /* The next completed transfer will be either the CSW one
1219 * or the new command */
1221 if(status
== UMS_STATUS_GOOD
) {
1222 cur_sense_data
.sense_key
=0;
1223 cur_sense_data
.information
=0;
1224 cur_sense_data
.asc
=0;
1225 cur_sense_data
.ascq
=0;
1229 static void copy_padded(char *dest
, char *src
, int len
)
1232 while(i
<len
&& src
[i
]!=0)
1244 /* build SCSI INQUIRY */
1245 static void fill_inquiry(IF_MD_NONVOID(int lun
))
1247 struct storage_info info
;
1248 memset(tb
.inquiry
, 0, sizeof(struct inquiry_data
));
1249 storage_get_info(lun
,&info
);
1250 copy_padded(tb
.inquiry
->VendorId
,info
.vendor
,sizeof(tb
.inquiry
->VendorId
));
1251 copy_padded(tb
.inquiry
->ProductId
,info
.product
,sizeof(tb
.inquiry
->ProductId
));
1252 copy_padded(tb
.inquiry
->ProductRevisionLevel
,info
.revision
,sizeof(tb
.inquiry
->ProductRevisionLevel
));
1254 tb
.inquiry
->DeviceType
= DIRECT_ACCESS_DEVICE
;
1255 tb
.inquiry
->AdditionalLength
= 0x1f;
1256 memset(tb
.inquiry
->Reserved
, 0, 3);
1257 tb
.inquiry
->Versions
= 4; /* SPC-2 */
1258 tb
.inquiry
->Format
= 2; /* SPC-2/3 inquiry format */
1260 tb
.inquiry
->DeviceTypeModifier
= DEVICE_REMOVABLE
;