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*/
30 /* Needed to get at the audio buffer */
32 #include "usb_storage.h"
33 #include "timefuncs.h"
35 /* For sector filter macro definitions */
36 #include "usb-target.h"
38 /* Enable the following define to export only the SD card slot. This
39 * is useful for USBCV MSC tests, as those are destructive.
40 * This won't work right if the device doesn't have a card slot.
42 //#define HIDE_FIRST_DRIVE
44 #ifdef USB_USE_RAMDISK
45 #define RAMDISK_SIZE 2048
49 #define SECTOR_SIZE 512
52 /* These defaults allow the operation */
53 #ifndef USBSTOR_READ_SECTORS_FILTER
54 #define USBSTOR_READ_SECTORS_FILTER() ({ 0; })
57 #ifndef USBSTOR_WRITE_SECTORS_FILTER
58 #define USBSTOR_WRITE_SECTORS_FILTER() ({ 0; })
61 /* the ARC driver currently supports up to 64k USB transfers. This is
62 * enough for efficient mass storage support, as commonly host OSes
63 * don't do larger SCSI transfers anyway, so larger USB transfers
64 * wouldn't buy us anything.
65 * Due to being the double-buffering system used, using a smaller write buffer
66 * ends up being more efficient. Measurements have shown that 24k to 28k is
67 * optimal, except for sd devices that apparently don't gain anything from
70 #ifdef USB_READ_BUFFER_SIZE
71 #define READ_BUFFER_SIZE USB_READ_BUFFER_SIZE
73 #if CONFIG_CPU == AS3525
74 /* We'd need to implement multidescriptor dma for sizes >65535 */
75 #define READ_BUFFER_SIZE (1024*63)
77 #define READ_BUFFER_SIZE (1024*64)
78 #endif /* CONFIG_CPU == AS3525 */
79 #endif /* USB_READ_BUFFER_SIZE */
81 #define MAX_CBW_SIZE 1024
83 #ifdef USB_WRITE_BUFFER_SIZE
84 #define WRITE_BUFFER_SIZE USB_WRITE_BUFFER_SIZE
86 #if (CONFIG_STORAGE & STORAGE_SD)
87 #if CONFIG_CPU == AS3525
88 /* We'd need to implement multidescriptor dma for sizes >65535 */
89 #define WRITE_BUFFER_SIZE (1024*63)
91 #define WRITE_BUFFER_SIZE (1024*64)
92 #endif /* CONFIG_CPU == AS3525 */
94 #define WRITE_BUFFER_SIZE (1024*24)
95 #endif /* (CONFIG_STORAGE & STORAGE_SD) */
96 #endif /* USB_WRITE_BUFFER_SIZE */
98 #define ALLOCATE_BUFFER_SIZE (2*MAX(READ_BUFFER_SIZE,WRITE_BUFFER_SIZE))
100 /* bulk-only class specific requests */
101 #define USB_BULK_RESET_REQUEST 0xff
102 #define USB_BULK_GET_MAX_LUN 0xfe
104 #define DIRECT_ACCESS_DEVICE 0x00 /* disks */
105 #define DEVICE_REMOVABLE 0x80
107 #define CBW_SIGNATURE 0x43425355
108 #define CSW_SIGNATURE 0x53425355
110 #define SCSI_TEST_UNIT_READY 0x00
111 #define SCSI_INQUIRY 0x12
112 #define SCSI_MODE_SENSE_6 0x1a
113 #define SCSI_MODE_SENSE_10 0x5a
114 #define SCSI_REQUEST_SENSE 0x03
115 #define SCSI_ALLOW_MEDIUM_REMOVAL 0x1e
116 #define SCSI_READ_CAPACITY 0x25
117 #define SCSI_READ_FORMAT_CAPACITY 0x23
118 #define SCSI_READ_10 0x28
119 #define SCSI_WRITE_10 0x2a
120 #define SCSI_START_STOP_UNIT 0x1b
121 #define SCSI_REPORT_LUNS 0xa0
122 #define SCSI_WRITE_BUFFER 0x3b
124 #define UMS_STATUS_GOOD 0x00
125 #define UMS_STATUS_FAIL 0x01
127 #define SENSE_NOT_READY 0x02
128 #define SENSE_MEDIUM_ERROR 0x03
129 #define SENSE_ILLEGAL_REQUEST 0x05
130 #define SENSE_UNIT_ATTENTION 0x06
132 #define ASC_MEDIUM_NOT_PRESENT 0x3a
133 #define ASC_INVALID_FIELD_IN_CBD 0x24
134 #define ASC_LBA_OUT_OF_RANGE 0x21
135 #define ASC_WRITE_ERROR 0x0C
136 #define ASC_READ_ERROR 0x11
137 #define ASC_NOT_READY 0x04
138 #define ASC_INVALID_COMMAND 0x20
140 #define ASCQ_BECOMING_READY 0x01
142 #define SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA 0x02000000
144 /* storage interface */
146 #define USB_SC_SCSI 0x06 /* Transparent */
147 #define USB_PROT_BULK 0x50 /* bulk only */
149 static struct usb_interface_descriptor
__attribute__((aligned(2)))
150 interface_descriptor
=
152 .bLength
= sizeof(struct usb_interface_descriptor
),
153 .bDescriptorType
= USB_DT_INTERFACE
,
154 .bInterfaceNumber
= 0,
155 .bAlternateSetting
= 0,
157 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
158 .bInterfaceSubClass
= USB_SC_SCSI
,
159 .bInterfaceProtocol
= USB_PROT_BULK
,
163 static struct usb_endpoint_descriptor
__attribute__((aligned(2)))
164 endpoint_descriptor
=
166 .bLength
= sizeof(struct usb_endpoint_descriptor
),
167 .bDescriptorType
= USB_DT_ENDPOINT
,
168 .bEndpointAddress
= 0,
169 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
174 struct inquiry_data
{
175 unsigned char DeviceType
;
176 unsigned char DeviceTypeModifier
;
177 unsigned char Versions
;
178 unsigned char Format
;
179 unsigned char AdditionalLength
;
180 unsigned char Reserved
[2];
181 unsigned char Capability
;
182 unsigned char VendorId
[8];
183 unsigned char ProductId
[16];
184 unsigned char ProductRevisionLevel
[4];
185 } __attribute__ ((packed
));
187 struct report_lun_data
{
188 unsigned int lun_list_length
;
189 unsigned int reserved1
;
190 unsigned char luns
[NUM_DRIVES
][8];
191 } __attribute__ ((packed
));
194 unsigned char ResponseCode
;
195 unsigned char Obsolete
;
196 unsigned char fei_sensekey
;
197 unsigned int Information
;
198 unsigned char AdditionalSenseLength
;
199 unsigned int CommandSpecificInformation
;
200 unsigned char AdditionalSenseCode
;
201 unsigned char AdditionalSenseCodeQualifier
;
202 unsigned char FieldReplaceableUnitCode
;
204 unsigned short SenseKeySpecific
;
205 } __attribute__ ((packed
));
207 struct mode_sense_bdesc_longlba
{
208 unsigned char num_blocks
[8];
209 unsigned char reserved
[4];
210 unsigned char block_size
[4];
211 } __attribute__ ((packed
));
213 struct mode_sense_bdesc_shortlba
{
214 unsigned char density_code
;
215 unsigned char num_blocks
[3];
216 unsigned char reserved
;
217 unsigned char block_size
[3];
218 } __attribute__ ((packed
));
220 struct mode_sense_data_10
{
221 unsigned short mode_data_length
;
222 unsigned char medium_type
;
223 unsigned char device_specific
;
224 unsigned char longlba
;
225 unsigned char reserved
;
226 unsigned short block_descriptor_length
;
227 struct mode_sense_bdesc_longlba block_descriptor
;
228 } __attribute__ ((packed
));
230 struct mode_sense_data_6
{
231 unsigned char mode_data_length
;
232 unsigned char medium_type
;
233 unsigned char device_specific
;
234 unsigned char block_descriptor_length
;
235 struct mode_sense_bdesc_shortlba block_descriptor
;
236 } __attribute__ ((packed
));
238 struct command_block_wrapper
{
239 unsigned int signature
;
241 unsigned int data_transfer_length
;
244 unsigned char command_length
;
245 unsigned char command_block
[16];
246 } __attribute__ ((packed
));
248 struct command_status_wrapper
{
249 unsigned int signature
;
251 unsigned int data_residue
;
252 unsigned char status
;
253 } __attribute__ ((packed
));
256 unsigned int block_count
;
257 unsigned int block_size
;
258 } __attribute__ ((packed
));
260 struct format_capacity
{
261 unsigned int following_length
;
262 unsigned int block_count
;
263 unsigned int block_size
;
264 } __attribute__ ((packed
));
268 unsigned char* transfer_buffer
;
269 struct inquiry_data
* inquiry
;
270 struct capacity
* capacity_data
;
271 struct format_capacity
* format_capacity_data
;
272 struct sense_data
*sense_data
;
273 struct mode_sense_data_6
*ms_data_6
;
274 struct mode_sense_data_10
*ms_data_10
;
275 struct report_lun_data
*lun_data
;
276 struct command_status_wrapper
* csw
;
280 static char *cbw_buffer
;
285 unsigned int orig_count
;
286 unsigned int cur_cmd
;
289 unsigned char *data
[2];
290 unsigned char data_select
;
291 unsigned int last_result
;
295 unsigned char sense_key
;
296 unsigned char information
;
301 static void handle_scsi(struct command_block_wrapper
* cbw
);
302 static void send_csw(int status
);
303 static void send_command_result(void *data
,int size
);
304 static void send_command_failed_result(void);
305 static void send_block_data(void *data
,int size
);
306 static void receive_block_data(void *data
,int size
);
307 static void receive_time(void);
308 static void fill_inquiry(IF_MD_NONVOID(int lun
));
309 static void send_and_read_next(void);
310 static bool ejected
[NUM_DRIVES
];
311 static bool locked
[NUM_DRIVES
];
313 static int usb_interface
;
314 static int ep_in
, ep_out
;
316 #ifdef USB_USE_RAMDISK
317 static unsigned char* ramdisk_buffer
;
324 SENDING_FAILED_RESULT
,
327 WAITING_FOR_CSW_COMPLETION_OR_COMMAND
,
328 WAITING_FOR_CSW_COMPLETION
329 } state
= WAITING_FOR_COMMAND
;
331 static void yearday_to_daymonth(int yd
, int y
, int *d
, int *m
)
333 static const char tnl
[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
334 static const char tl
[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
338 if((y
%4 == 0 && y
%100 != 0) || y
%400 == 0)
347 while(yd
>= t
[i
] && i
<12)
356 static bool check_disk_present(IF_MD_NONVOID(int volume
))
358 #ifdef USB_USE_RAMDISK
361 unsigned char sector
[SECTOR_SIZE
];
362 return storage_read_sectors(IF_MD2(volume
,)0,1,sector
) == 0;
366 void usb_storage_try_release_storage(void)
368 /* Check if there is a connected drive left. If not,
369 release excusive access */
370 bool canrelease
=true;
372 for(i
=0;i
<storage_num_drives();i
++) {
373 if(!ejected
[i
] && locked
[i
]) {
379 logf("scsi release ata");
380 usb_release_exclusive_storage();
385 void usb_storage_notify_hotswap(int volume
,bool inserted
)
387 logf("notify %d",inserted
);
388 if(inserted
&& check_disk_present(IF_MD(volume
))) {
389 ejected
[volume
] = false;
392 ejected
[volume
] = true;
393 /* If this happens while the device is locked, weird things may happen.
394 At least try to keep our state consistent */
395 locked
[volume
]=false;
400 /* called by usb_core_init() */
401 void usb_storage_init(void)
403 logf("usb_storage_init done");
406 int usb_storage_request_endpoints(struct usb_class_driver
*drv
)
408 ep_in
= usb_core_request_endpoint(USB_ENDPOINT_XFER_BULK
, USB_DIR_IN
, drv
);
413 ep_out
= usb_core_request_endpoint(USB_ENDPOINT_XFER_BULK
, USB_DIR_OUT
,
417 usb_core_release_endpoint(ep_in
);
424 int usb_storage_set_first_interface(int interface
)
426 usb_interface
= interface
;
427 return interface
+ 1;
430 int usb_storage_get_config_descriptor(unsigned char *dest
,int max_packet_size
)
432 unsigned char *orig_dest
= dest
;
434 interface_descriptor
.bInterfaceNumber
= usb_interface
;
435 PACK_DATA(dest
, interface_descriptor
);
437 endpoint_descriptor
.wMaxPacketSize
= max_packet_size
;
439 endpoint_descriptor
.bEndpointAddress
= ep_in
;
440 PACK_DATA(dest
, endpoint_descriptor
);
442 endpoint_descriptor
.bEndpointAddress
= ep_out
;
443 PACK_DATA(dest
, endpoint_descriptor
);
445 return (dest
- orig_dest
);
448 void usb_storage_init_connection(void)
450 logf("ums: set config");
451 /* prime rx endpoint. We only need room for commands */
452 state
= WAITING_FOR_COMMAND
;
454 #if (CONFIG_CPU == IMX31L || defined(CPU_TCC77X) || defined(CPU_TCC780X) || \
455 defined(BOOTLOADER) || CONFIG_CPU == DM320) && !defined(CPU_PP502x)
456 static unsigned char _cbw_buffer
[MAX_CBW_SIZE
]
457 USB_DEVBSS_ATTR
__attribute__((aligned(32)));
458 cbw_buffer
= (void *)_cbw_buffer
;
460 static unsigned char _transfer_buffer
[ALLOCATE_BUFFER_SIZE
]
461 USB_DEVBSS_ATTR
__attribute__((aligned(32)));
462 tb
.transfer_buffer
= (void *)_transfer_buffer
;
463 #ifdef USB_USE_RAMDISK
464 static unsigned char _ramdisk_buffer
[RAMDISK_SIZE
*SECTOR_SIZE
];
465 ramdisk_buffer
= _ramdisk_buffer
;
468 /* TODO : check if bufsize is at least 32K ? */
470 unsigned char * audio_buffer
;
472 audio_buffer
= audio_get_buffer(false,&bufsize
);
473 #if defined(UNCACHED_ADDR) && CONFIG_CPU != AS3525
474 cbw_buffer
= (void *)UNCACHED_ADDR((unsigned int)(audio_buffer
+31) & 0xffffffe0);
476 cbw_buffer
= (void *)((unsigned int)(audio_buffer
+31) & 0xffffffe0);
478 tb
.transfer_buffer
= cbw_buffer
+ MAX_CBW_SIZE
;
479 cpucache_invalidate();
480 #ifdef USB_USE_RAMDISK
481 ramdisk_buffer
= tb
.transfer_buffer
+ ALLOCATE_BUFFER_SIZE
;
484 usb_drv_recv(ep_out
, cbw_buffer
, MAX_CBW_SIZE
);
487 for(i
=0;i
<storage_num_drives();i
++) {
489 ejected
[i
] = !check_disk_present(IF_MD(i
));
490 queue_broadcast(SYS_USB_LUN_LOCKED
, (i
<<16)+0);
494 void usb_storage_disconnect(void)
499 /* called by usb_core_transfer_complete() */
500 void usb_storage_transfer_complete(int ep
,int dir
,int status
,int length
)
503 struct command_block_wrapper
* cbw
= (void*)cbw_buffer
;
506 logf("transfer result for ep %d/%d %X %d", ep
,dir
,status
, length
);
508 case RECEIVING_BLOCKS
:
509 if(dir
==USB_DIR_IN
) {
510 logf("IN received in RECEIVING");
512 logf("scsi write %d %d", cur_cmd
.sector
, cur_cmd
.count
);
514 if((unsigned int)length
!=(SECTOR_SIZE
* cur_cmd
.count
)
515 && (unsigned int)length
!=WRITE_BUFFER_SIZE
) {
516 logf("unexpected length :%d",length
);
520 unsigned int next_sector
= cur_cmd
.sector
+
521 (WRITE_BUFFER_SIZE
/SECTOR_SIZE
);
522 unsigned int next_count
= cur_cmd
.count
-
523 MIN(cur_cmd
.count
,WRITE_BUFFER_SIZE
/SECTOR_SIZE
);
524 int next_select
= !cur_cmd
.data_select
;
527 /* Ask the host to send more, to the other buffer */
528 receive_block_data(cur_cmd
.data
[next_select
],
529 MIN(WRITE_BUFFER_SIZE
,next_count
*SECTOR_SIZE
));
532 /* Now write the data that just came in, while the host is
533 sending the next bit */
534 #ifdef USB_USE_RAMDISK
535 memcpy(ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
536 cur_cmd
.data
[cur_cmd
.data_select
],
537 MIN(WRITE_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
)*SECTOR_SIZE
);
539 int result
= USBSTOR_WRITE_SECTORS_FILTER();
542 result
= storage_write_sectors(IF_MD2(cur_cmd
.lun
,)
544 MIN(WRITE_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
),
545 cur_cmd
.data
[cur_cmd
.data_select
]);
549 send_csw(UMS_STATUS_FAIL
);
550 cur_sense_data
.sense_key
=SENSE_MEDIUM_ERROR
;
551 cur_sense_data
.asc
=ASC_WRITE_ERROR
;
552 cur_sense_data
.ascq
=0;
557 send_csw(UMS_STATUS_GOOD
);
560 /* Switch buffers for the next one */
561 cur_cmd
.data_select
=!cur_cmd
.data_select
;
563 cur_cmd
.sector
= next_sector
;
564 cur_cmd
.count
= next_count
;
567 logf("Transfer failed %X",status
);
568 send_csw(UMS_STATUS_FAIL
);
569 /* TODO fill in cur_sense_data */
570 cur_sense_data
.sense_key
=0;
571 cur_sense_data
.information
=0;
572 cur_sense_data
.asc
=0;
573 cur_sense_data
.ascq
=0;
576 case WAITING_FOR_CSW_COMPLETION_OR_COMMAND
:
577 if(dir
==USB_DIR_IN
) {
578 /* This was the CSW */
579 state
= WAITING_FOR_COMMAND
;
582 /* This was the command */
583 state
= WAITING_FOR_CSW_COMPLETION
;
584 /* We now have the CBW, but we won't execute it yet to avoid
585 * issues with the still-pending CSW */
588 case WAITING_FOR_COMMAND
:
589 if(dir
==USB_DIR_IN
) {
590 logf("IN received in WAITING_FOR_COMMAND");
594 case WAITING_FOR_CSW_COMPLETION
:
595 if(dir
==USB_DIR_OUT
) {
596 logf("OUT received in WAITING_FOR_CSW_COMPLETION");
601 if(cur_cmd
.cur_cmd
== SCSI_WRITE_10
)
603 queue_broadcast(SYS_USB_WRITE_DATA
, (cur_cmd
.lun
<<16)+cur_cmd
.orig_count
);
605 else if(cur_cmd
.cur_cmd
== SCSI_READ_10
)
607 queue_broadcast(SYS_USB_READ_DATA
, (cur_cmd
.lun
<<16)+cur_cmd
.orig_count
);
612 if(dir
==USB_DIR_OUT
) {
613 logf("OUT received in SENDING");
616 //logf("data sent, now send csw");
617 send_csw(UMS_STATUS_GOOD
);
620 logf("Transfer failed %X",status
);
621 send_csw(UMS_STATUS_FAIL
);
622 /* TODO fill in cur_sense_data */
623 cur_sense_data
.sense_key
=0;
624 cur_sense_data
.information
=0;
625 cur_sense_data
.asc
=0;
626 cur_sense_data
.ascq
=0;
629 case SENDING_FAILED_RESULT
:
630 if(dir
==USB_DIR_OUT
) {
631 logf("OUT received in SENDING");
633 send_csw(UMS_STATUS_FAIL
);
636 if(dir
==USB_DIR_OUT
) {
637 logf("OUT received in SENDING");
640 if(cur_cmd
.count
==0) {
641 //logf("data sent, now send csw");
642 if(cur_cmd
.last_result
!=0) {
643 /* The last read failed. */
644 send_csw(UMS_STATUS_FAIL
);
645 cur_sense_data
.sense_key
=SENSE_MEDIUM_ERROR
;
646 cur_sense_data
.asc
=ASC_READ_ERROR
;
647 cur_sense_data
.ascq
=0;
651 send_csw(UMS_STATUS_GOOD
);
654 send_and_read_next();
658 logf("Transfer failed %X",status
);
659 send_csw(UMS_STATUS_FAIL
);
660 /* TODO fill in cur_sense_data */
661 cur_sense_data
.sense_key
=0;
662 cur_sense_data
.information
=0;
663 cur_sense_data
.asc
=0;
664 cur_sense_data
.ascq
=0;
668 tm
.tm_year
=(tb
.transfer_buffer
[0]<<8)+tb
.transfer_buffer
[1] - 1900;
669 tm
.tm_yday
=(tb
.transfer_buffer
[2]<<8)+tb
.transfer_buffer
[3];
670 tm
.tm_hour
=tb
.transfer_buffer
[5];
671 tm
.tm_min
=tb
.transfer_buffer
[6];
672 tm
.tm_sec
=tb
.transfer_buffer
[7];
673 yearday_to_daymonth(tm
.tm_yday
,tm
.tm_year
+ 1900,&tm
.tm_mday
,&tm
.tm_mon
);
674 set_day_of_week(&tm
);
676 send_csw(UMS_STATUS_GOOD
);
681 /* called by usb_core_control_request() */
682 bool usb_storage_control_request(struct usb_ctrlrequest
* req
, unsigned char* dest
)
684 bool handled
= false;
687 switch (req
->bRequest
) {
688 case USB_BULK_GET_MAX_LUN
: {
689 *tb
.max_lun
= storage_num_drives() - 1;
690 #ifdef HIDE_FIRST_DRIVE
693 logf("ums: getmaxlun");
694 usb_drv_recv(EP_CONTROL
, NULL
, 0); /* ack */
695 usb_drv_send(EP_CONTROL
, tb
.max_lun
, 1);
700 case USB_BULK_RESET_REQUEST
:
701 logf("ums: bulk reset");
702 state
= WAITING_FOR_COMMAND
;
703 /* UMS BOT 3.1 says The device shall preserve the value of its bulk
704 data toggle bits and endpoint STALL conditions despite
705 the Bulk-Only Mass Storage Reset. */
707 usb_drv_reset_endpoint(ep_in
, false);
708 usb_drv_reset_endpoint(ep_out
, true);
710 usb_drv_send(EP_CONTROL
, NULL
, 0); /* ack */
718 static void send_and_read_next(void)
720 int result
= USBSTOR_READ_SECTORS_FILTER();
722 if(result
!= 0 && cur_cmd
.last_result
== 0)
723 cur_cmd
.last_result
= result
;
725 send_block_data(cur_cmd
.data
[cur_cmd
.data_select
],
726 MIN(READ_BUFFER_SIZE
,cur_cmd
.count
*SECTOR_SIZE
));
728 /* Switch buffers for the next one */
729 cur_cmd
.data_select
=!cur_cmd
.data_select
;
731 cur_cmd
.sector
+=(READ_BUFFER_SIZE
/SECTOR_SIZE
);
732 cur_cmd
.count
-=MIN(cur_cmd
.count
,READ_BUFFER_SIZE
/SECTOR_SIZE
);
734 if(cur_cmd
.count
!=0) {
735 /* already read the next bit, so we can send it out immediately when the
736 * current transfer completes. */
737 #ifdef USB_USE_RAMDISK
738 memcpy(cur_cmd
.data
[cur_cmd
.data_select
],
739 ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
740 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
)*SECTOR_SIZE
);
742 result
= storage_read_sectors(IF_MD2(cur_cmd
.lun
,)
744 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
),
745 cur_cmd
.data
[cur_cmd
.data_select
]);
746 if(cur_cmd
.last_result
== 0)
747 cur_cmd
.last_result
= result
;
752 /****************************************************************************/
754 static void handle_scsi(struct command_block_wrapper
* cbw
)
756 /* USB Mass Storage assumes LBA capability.
757 TODO: support 48-bit LBA */
759 struct storage_info info
;
760 unsigned int length
= cbw
->data_transfer_length
;
761 unsigned int block_size
= 0;
762 unsigned int block_count
= 0;
763 bool lun_present
=true;
764 unsigned char lun
= cbw
->lun
;
765 unsigned int block_size_mult
= 1;
767 if(letoh32(cbw
->signature
) != CBW_SIGNATURE
) {
768 usb_drv_stall(ep_in
, true,true);
769 usb_drv_stall(ep_out
, true,false);
772 /* Clear the signature to prevent possible bugs elsewhere
773 * to trigger a second execution of the same command with
777 #ifdef HIDE_FIRST_DRIVE
781 storage_get_info(lun
,&info
);
782 #ifdef USB_USE_RAMDISK
783 block_size
= SECTOR_SIZE
;
784 block_count
= RAMDISK_SIZE
;
786 block_size
=info
.sector_size
;
787 block_count
=info
.num_sectors
;
791 if(storage_removable(lun
) && !storage_present(lun
)) {
799 #ifdef MAX_LOG_SECTOR_SIZE
800 block_size_mult
= disk_sector_multiplier
;
803 cur_cmd
.tag
= cbw
->tag
;
805 cur_cmd
.cur_cmd
= cbw
->command_block
[0];
807 switch (cbw
->command_block
[0]) {
808 case SCSI_TEST_UNIT_READY
:
809 logf("scsi test_unit_ready %d",lun
);
810 if(!usb_exclusive_storage()) {
811 send_csw(UMS_STATUS_FAIL
);
812 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
813 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
814 cur_sense_data
.ascq
=0;
818 send_csw(UMS_STATUS_GOOD
);
821 send_csw(UMS_STATUS_FAIL
);
822 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
823 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
824 cur_sense_data
.ascq
=0;
828 case SCSI_REPORT_LUNS
: {
829 logf("scsi report luns %d",lun
);
830 int allocation_length
=0;
832 unsigned int response_length
= 8+8*storage_num_drives();
833 allocation_length
|=(cbw
->command_block
[6]<<24);
834 allocation_length
|=(cbw
->command_block
[7]<<16);
835 allocation_length
|=(cbw
->command_block
[8]<<8);
836 allocation_length
|=(cbw
->command_block
[9]);
837 memset(tb
.lun_data
,0,sizeof(struct report_lun_data
));
838 tb
.lun_data
->lun_list_length
=htobe32(8*storage_num_drives());
839 for(i
=0;i
<storage_num_drives();i
++)
842 if(storage_removable(i
))
843 tb
.lun_data
->luns
[i
][1]=1;
846 tb
.lun_data
->luns
[i
][1]=0;
848 send_command_result(tb
.lun_data
,
849 MIN(response_length
, length
));
854 logf("scsi inquiry %d",lun
);
855 fill_inquiry(IF_MD(lun
));
856 length
= MIN(length
, cbw
->command_block
[4]);
857 send_command_result(tb
.inquiry
,
858 MIN(sizeof(struct inquiry_data
), length
));
861 case SCSI_REQUEST_SENSE
: {
862 tb
.sense_data
->ResponseCode
=0x70;/*current error*/
863 tb
.sense_data
->Obsolete
=0;
864 tb
.sense_data
->fei_sensekey
=cur_sense_data
.sense_key
&0x0f;
865 tb
.sense_data
->Information
=cur_sense_data
.information
;
866 tb
.sense_data
->AdditionalSenseLength
=10;
867 tb
.sense_data
->CommandSpecificInformation
=0;
868 tb
.sense_data
->AdditionalSenseCode
=cur_sense_data
.asc
;
869 tb
.sense_data
->AdditionalSenseCodeQualifier
=cur_sense_data
.ascq
;
870 tb
.sense_data
->FieldReplaceableUnitCode
=0;
871 tb
.sense_data
->SKSV
=0;
872 tb
.sense_data
->SenseKeySpecific
=0;
873 logf("scsi request_sense %d",lun
);
874 send_command_result(tb
.sense_data
,
875 MIN(sizeof(struct sense_data
), length
));
879 case SCSI_MODE_SENSE_10
: {
881 send_command_failed_result();
882 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
883 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
884 cur_sense_data
.ascq
=0;
887 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
888 unsigned char page_code
= cbw
->command_block
[2] & 0x3f;
889 logf("scsi mode_sense_10 %d %X",lun
,page_code
);
892 tb
.ms_data_10
->mode_data_length
=
893 htobe16(sizeof(struct mode_sense_data_10
)-2);
894 tb
.ms_data_10
->medium_type
= 0;
895 tb
.ms_data_10
->device_specific
= 0;
896 tb
.ms_data_10
->reserved
= 0;
897 tb
.ms_data_10
->longlba
= 1;
898 tb
.ms_data_10
->block_descriptor_length
=
899 htobe16(sizeof(struct mode_sense_bdesc_longlba
));
901 memset(tb
.ms_data_10
->block_descriptor
.reserved
,0,4);
902 memset(tb
.ms_data_10
->block_descriptor
.num_blocks
,0,8);
904 tb
.ms_data_10
->block_descriptor
.num_blocks
[4] =
905 ((block_count
/block_size_mult
) & 0xff000000)>>24;
906 tb
.ms_data_10
->block_descriptor
.num_blocks
[5] =
907 ((block_count
/block_size_mult
) & 0x00ff0000)>>16;
908 tb
.ms_data_10
->block_descriptor
.num_blocks
[6] =
909 ((block_count
/block_size_mult
) & 0x0000ff00)>>8;
910 tb
.ms_data_10
->block_descriptor
.num_blocks
[7] =
911 ((block_count
/block_size_mult
) & 0x000000ff);
913 tb
.ms_data_10
->block_descriptor
.block_size
[0] =
914 ((block_size
*block_size_mult
) & 0xff000000)>>24;
915 tb
.ms_data_10
->block_descriptor
.block_size
[1] =
916 ((block_size
*block_size_mult
) & 0x00ff0000)>>16;
917 tb
.ms_data_10
->block_descriptor
.block_size
[2] =
918 ((block_size
*block_size_mult
) & 0x0000ff00)>>8;
919 tb
.ms_data_10
->block_descriptor
.block_size
[3] =
920 ((block_size
*block_size_mult
) & 0x000000ff);
921 send_command_result(tb
.ms_data_10
,
922 MIN(sizeof(struct mode_sense_data_10
), length
));
925 send_command_failed_result();
926 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
927 cur_sense_data
.asc
=ASC_INVALID_FIELD_IN_CBD
;
928 cur_sense_data
.ascq
=0;
934 case SCSI_MODE_SENSE_6
: {
936 send_command_failed_result();
937 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
938 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
939 cur_sense_data
.ascq
=0;
942 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
943 unsigned char page_code
= cbw
->command_block
[2] & 0x3f;
944 logf("scsi mode_sense_6 %d %X",lun
,page_code
);
947 /* All supported pages. */
948 tb
.ms_data_6
->mode_data_length
=
949 sizeof(struct mode_sense_data_6
)-1;
950 tb
.ms_data_6
->medium_type
= 0;
951 tb
.ms_data_6
->device_specific
= 0;
952 tb
.ms_data_6
->block_descriptor_length
=
953 sizeof(struct mode_sense_bdesc_shortlba
);
954 tb
.ms_data_6
->block_descriptor
.density_code
= 0;
955 tb
.ms_data_6
->block_descriptor
.reserved
= 0;
956 if(block_count
/block_size_mult
> 0xffffff) {
957 tb
.ms_data_6
->block_descriptor
.num_blocks
[0] = 0xff;
958 tb
.ms_data_6
->block_descriptor
.num_blocks
[1] = 0xff;
959 tb
.ms_data_6
->block_descriptor
.num_blocks
[2] = 0xff;
962 tb
.ms_data_6
->block_descriptor
.num_blocks
[0] =
963 ((block_count
/block_size_mult
) & 0xff0000)>>16;
964 tb
.ms_data_6
->block_descriptor
.num_blocks
[1] =
965 ((block_count
/block_size_mult
) & 0x00ff00)>>8;
966 tb
.ms_data_6
->block_descriptor
.num_blocks
[2] =
967 ((block_count
/block_size_mult
) & 0x0000ff);
969 tb
.ms_data_6
->block_descriptor
.block_size
[0] =
970 ((block_size
*block_size_mult
) & 0xff0000)>>16;
971 tb
.ms_data_6
->block_descriptor
.block_size
[1] =
972 ((block_size
*block_size_mult
) & 0x00ff00)>>8;
973 tb
.ms_data_6
->block_descriptor
.block_size
[2] =
974 ((block_size
*block_size_mult
) & 0x0000ff);
975 send_command_result(tb
.ms_data_6
,
976 MIN(sizeof(struct mode_sense_data_6
), length
));
979 send_command_failed_result();
980 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
981 cur_sense_data
.asc
=ASC_INVALID_FIELD_IN_CBD
;
982 cur_sense_data
.ascq
=0;
988 case SCSI_START_STOP_UNIT
:
989 logf("scsi start_stop unit %d",lun
);
990 if((cbw
->command_block
[4] & 0xf0) == 0) /*load/eject bit is valid*/
991 { /* Process start and eject bits */
992 logf("scsi load/eject");
993 if((cbw
->command_block
[4] & 0x01) == 0) /* Don't start */
995 if((cbw
->command_block
[4] & 0x02) != 0) /* eject */
1002 send_csw(UMS_STATUS_GOOD
);
1005 case SCSI_ALLOW_MEDIUM_REMOVAL
:
1006 logf("scsi allow_medium_removal %d",lun
);
1007 if((cbw
->command_block
[4] & 0x03) == 0) {
1009 queue_broadcast(SYS_USB_LUN_LOCKED
, (lun
<<16)+0);
1013 queue_broadcast(SYS_USB_LUN_LOCKED
, (lun
<<16)+1);
1015 send_csw(UMS_STATUS_GOOD
);
1018 case SCSI_READ_FORMAT_CAPACITY
: {
1019 logf("scsi read_format_capacity %d",lun
);
1021 tb
.format_capacity_data
->following_length
=htobe32(8);
1022 /* "block count" actually means "number of last block" */
1023 tb
.format_capacity_data
->block_count
=
1024 htobe32(block_count
/block_size_mult
- 1);
1025 tb
.format_capacity_data
->block_size
=
1026 htobe32(block_size
*block_size_mult
);
1027 tb
.format_capacity_data
->block_size
|=
1028 htobe32(SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA
);
1030 send_command_result(tb
.format_capacity_data
,
1031 MIN(sizeof(struct format_capacity
), length
));
1034 send_command_failed_result();
1035 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1036 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1037 cur_sense_data
.ascq
=0;
1042 case SCSI_READ_CAPACITY
: {
1043 logf("scsi read_capacity %d",lun
);
1046 /* "block count" actually means "number of last block" */
1047 tb
.capacity_data
->block_count
=
1048 htobe32(block_count
/block_size_mult
- 1);
1049 tb
.capacity_data
->block_size
=
1050 htobe32(block_size
*block_size_mult
);
1052 send_command_result(tb
.capacity_data
,
1053 MIN(sizeof(struct capacity
), length
));
1056 send_command_failed_result();
1057 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1058 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1059 cur_sense_data
.ascq
=0;
1065 logf("scsi read10 %d",lun
);
1067 send_command_failed_result();
1068 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1069 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1070 cur_sense_data
.ascq
=0;
1073 cur_cmd
.data
[0] = tb
.transfer_buffer
;
1074 cur_cmd
.data
[1] = &tb
.transfer_buffer
[READ_BUFFER_SIZE
];
1075 cur_cmd
.data_select
=0;
1076 cur_cmd
.sector
= block_size_mult
*
1077 (cbw
->command_block
[2] << 24 |
1078 cbw
->command_block
[3] << 16 |
1079 cbw
->command_block
[4] << 8 |
1080 cbw
->command_block
[5] );
1081 cur_cmd
.count
= block_size_mult
*
1082 (cbw
->command_block
[7] << 8 |
1083 cbw
->command_block
[8]);
1084 cur_cmd
.orig_count
= cur_cmd
.count
;
1086 //logf("scsi read %d %d", cur_cmd.sector, cur_cmd.count);
1088 if((cur_cmd
.sector
+ cur_cmd
.count
) > block_count
) {
1089 send_csw(UMS_STATUS_FAIL
);
1090 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1091 cur_sense_data
.asc
=ASC_LBA_OUT_OF_RANGE
;
1092 cur_sense_data
.ascq
=0;
1095 #ifdef USB_USE_RAMDISK
1096 memcpy(cur_cmd
.data
[cur_cmd
.data_select
],
1097 ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
1098 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
,cur_cmd
.count
)*SECTOR_SIZE
);
1100 cur_cmd
.last_result
= storage_read_sectors(IF_MD2(cur_cmd
.lun
,)
1102 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
),
1103 cur_cmd
.data
[cur_cmd
.data_select
]);
1105 send_and_read_next();
1110 logf("scsi write10 %d",lun
);
1112 send_csw(UMS_STATUS_FAIL
);
1113 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1114 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1115 cur_sense_data
.ascq
=0;
1118 cur_cmd
.data
[0] = tb
.transfer_buffer
;
1119 cur_cmd
.data
[1] = &tb
.transfer_buffer
[WRITE_BUFFER_SIZE
];
1120 cur_cmd
.data_select
=0;
1121 cur_cmd
.sector
= block_size_mult
*
1122 (cbw
->command_block
[2] << 24 |
1123 cbw
->command_block
[3] << 16 |
1124 cbw
->command_block
[4] << 8 |
1125 cbw
->command_block
[5] );
1126 cur_cmd
.count
= block_size_mult
*
1127 (cbw
->command_block
[7] << 8 |
1128 cbw
->command_block
[8]);
1129 cur_cmd
.orig_count
= cur_cmd
.count
;
1132 if((cur_cmd
.sector
+ cur_cmd
.count
) > block_count
) {
1133 send_csw(UMS_STATUS_FAIL
);
1134 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1135 cur_sense_data
.asc
=ASC_LBA_OUT_OF_RANGE
;
1136 cur_sense_data
.ascq
=0;
1139 receive_block_data(cur_cmd
.data
[0],
1140 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)
1163 logf("scsi unknown cmd %x",cbw
->command_block
[0x0]);
1164 send_csw(UMS_STATUS_FAIL
);
1165 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1166 cur_sense_data
.asc
=ASC_INVALID_COMMAND
;
1167 cur_sense_data
.ascq
=0;
1172 static void send_block_data(void *data
,int size
)
1174 usb_drv_send_nonblocking(ep_in
, data
,size
);
1175 state
= SENDING_BLOCKS
;
1178 static void send_command_result(void *data
,int size
)
1180 usb_drv_send_nonblocking(ep_in
, data
,size
);
1181 state
= SENDING_RESULT
;
1184 static void send_command_failed_result(void)
1186 usb_drv_send_nonblocking(ep_in
, NULL
, 0);
1187 state
= SENDING_FAILED_RESULT
;
1190 static void receive_time(void)
1192 usb_drv_recv(ep_out
, tb
.transfer_buffer
, 12);
1193 state
= RECEIVING_TIME
;
1195 static void receive_block_data(void *data
,int size
)
1197 usb_drv_recv(ep_out
, data
, size
);
1198 state
= RECEIVING_BLOCKS
;
1201 static void send_csw(int status
)
1203 tb
.csw
->signature
= htole32(CSW_SIGNATURE
);
1204 tb
.csw
->tag
= cur_cmd
.tag
;
1205 tb
.csw
->data_residue
= 0;
1206 tb
.csw
->status
= status
;
1208 usb_drv_send_nonblocking(ep_in
, tb
.csw
,
1209 sizeof(struct command_status_wrapper
));
1210 state
= WAITING_FOR_CSW_COMPLETION_OR_COMMAND
;
1211 //logf("CSW: %X",status);
1212 /* Already start waiting for the next command */
1213 usb_drv_recv(ep_out
, cbw_buffer
, MAX_CBW_SIZE
);
1214 /* The next completed transfer will be either the CSW one
1215 * or the new command */
1217 if(status
== UMS_STATUS_GOOD
) {
1218 cur_sense_data
.sense_key
=0;
1219 cur_sense_data
.information
=0;
1220 cur_sense_data
.asc
=0;
1221 cur_sense_data
.ascq
=0;
1225 static void copy_padded(char *dest
, char *src
, int len
)
1228 while(src
[i
]!=0 && i
<len
)
1240 /* build SCSI INQUIRY */
1241 static void fill_inquiry(IF_MD_NONVOID(int lun
))
1243 struct storage_info info
;
1244 memset(tb
.inquiry
, 0, sizeof(struct inquiry_data
));
1245 storage_get_info(lun
,&info
);
1246 copy_padded(tb
.inquiry
->VendorId
,info
.vendor
,sizeof(tb
.inquiry
->VendorId
));
1247 copy_padded(tb
.inquiry
->ProductId
,info
.product
,sizeof(tb
.inquiry
->ProductId
));
1248 copy_padded(tb
.inquiry
->ProductRevisionLevel
,info
.revision
,sizeof(tb
.inquiry
->ProductRevisionLevel
));
1250 tb
.inquiry
->DeviceType
= DIRECT_ACCESS_DEVICE
;
1251 tb
.inquiry
->AdditionalLength
= 0x1f;
1252 memset(tb
.inquiry
->Reserved
, 0, 3);
1253 tb
.inquiry
->Versions
= 4; /* SPC-2 */
1254 tb
.inquiry
->Format
= 2; /* SPC-2/3 inquiry format */
1256 tb
.inquiry
->DeviceTypeModifier
= DEVICE_REMOVABLE
;