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"
36 /* Enable the following define to export only the SD card slot. This
37 * is useful for USBCV MSC tests, as those are destructive.
38 * This won't work right if the device doesn't have a card slot.
40 //#define HIDE_FIRST_DRIVE
42 #ifdef USB_USE_RAMDISK
43 #define RAMDISK_SIZE 2048
47 #define SECTOR_SIZE 512
50 /* the ARC driver currently supports up to 64k USB transfers. This is
51 * enough for efficient mass storage support, as commonly host OSes
52 * don't do larger SCSI transfers anyway, so larger USB transfers
53 * wouldn't buy us anything.
54 * Due to being the double-buffering system used, using a smaller write buffer
55 * ends up being more efficient. Measurements have shown that 24k to 28k is
56 * optimal, except for sd devices that apparently don't gain anything from
59 #ifdef USB_READ_BUFFER_SIZE
60 #define READ_BUFFER_SIZE USB_READ_BUFFER_SIZE
62 #if CONFIG_CPU == AS3525
63 /* We'd need to implement multidescriptor dma for sizes >65535 */
64 #define READ_BUFFER_SIZE (1024*63)
66 #define READ_BUFFER_SIZE (1024*64)
67 #endif /* CONFIG_CPU == AS3525 */
68 #endif /* USB_READ_BUFFER_SIZE */
70 #define MAX_CBW_SIZE 1024
72 #ifdef USB_WRITE_BUFFER_SIZE
73 #define WRITE_BUFFER_SIZE USB_WRITE_BUFFER_SIZE
75 #if (CONFIG_STORAGE & STORAGE_SD)
76 #if CONFIG_CPU == AS3525
77 /* We'd need to implement multidescriptor dma for sizes >65535 */
78 #define WRITE_BUFFER_SIZE (1024*63)
80 #define WRITE_BUFFER_SIZE (1024*64)
81 #endif /* CONFIG_CPU == AS3525 */
83 #define WRITE_BUFFER_SIZE (1024*24)
84 #endif /* (CONFIG_STORAGE & STORAGE_SD) */
85 #endif /* USB_WRITE_BUFFER_SIZE */
87 #define ALLOCATE_BUFFER_SIZE (2*MAX(READ_BUFFER_SIZE,WRITE_BUFFER_SIZE))
89 /* bulk-only class specific requests */
90 #define USB_BULK_RESET_REQUEST 0xff
91 #define USB_BULK_GET_MAX_LUN 0xfe
93 #define DIRECT_ACCESS_DEVICE 0x00 /* disks */
94 #define DEVICE_REMOVABLE 0x80
96 #define CBW_SIGNATURE 0x43425355
97 #define CSW_SIGNATURE 0x53425355
99 #define SCSI_TEST_UNIT_READY 0x00
100 #define SCSI_INQUIRY 0x12
101 #define SCSI_MODE_SENSE_6 0x1a
102 #define SCSI_MODE_SENSE_10 0x5a
103 #define SCSI_REQUEST_SENSE 0x03
104 #define SCSI_ALLOW_MEDIUM_REMOVAL 0x1e
105 #define SCSI_READ_CAPACITY 0x25
106 #define SCSI_READ_FORMAT_CAPACITY 0x23
107 #define SCSI_READ_10 0x28
108 #define SCSI_WRITE_10 0x2a
109 #define SCSI_START_STOP_UNIT 0x1b
110 #define SCSI_REPORT_LUNS 0xa0
111 #define SCSI_WRITE_BUFFER 0x3b
113 #define UMS_STATUS_GOOD 0x00
114 #define UMS_STATUS_FAIL 0x01
116 #define SENSE_NOT_READY 0x02
117 #define SENSE_MEDIUM_ERROR 0x03
118 #define SENSE_ILLEGAL_REQUEST 0x05
119 #define SENSE_UNIT_ATTENTION 0x06
121 #define ASC_MEDIUM_NOT_PRESENT 0x3a
122 #define ASC_INVALID_FIELD_IN_CBD 0x24
123 #define ASC_LBA_OUT_OF_RANGE 0x21
124 #define ASC_WRITE_ERROR 0x0C
125 #define ASC_READ_ERROR 0x11
126 #define ASC_NOT_READY 0x04
127 #define ASC_INVALID_COMMAND 0x20
129 #define ASCQ_BECOMING_READY 0x01
131 #define SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA 0x02000000
133 /* storage interface */
135 #define USB_SC_SCSI 0x06 /* Transparent */
136 #define USB_PROT_BULK 0x50 /* bulk only */
138 static struct usb_interface_descriptor
__attribute__((aligned(2)))
139 interface_descriptor
=
141 .bLength
= sizeof(struct usb_interface_descriptor
),
142 .bDescriptorType
= USB_DT_INTERFACE
,
143 .bInterfaceNumber
= 0,
144 .bAlternateSetting
= 0,
146 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
147 .bInterfaceSubClass
= USB_SC_SCSI
,
148 .bInterfaceProtocol
= USB_PROT_BULK
,
152 static struct usb_endpoint_descriptor
__attribute__((aligned(2)))
153 endpoint_descriptor
=
155 .bLength
= sizeof(struct usb_endpoint_descriptor
),
156 .bDescriptorType
= USB_DT_ENDPOINT
,
157 .bEndpointAddress
= 0,
158 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
163 struct inquiry_data
{
164 unsigned char DeviceType
;
165 unsigned char DeviceTypeModifier
;
166 unsigned char Versions
;
167 unsigned char Format
;
168 unsigned char AdditionalLength
;
169 unsigned char Reserved
[2];
170 unsigned char Capability
;
171 unsigned char VendorId
[8];
172 unsigned char ProductId
[16];
173 unsigned char ProductRevisionLevel
[4];
174 } __attribute__ ((packed
));
176 struct report_lun_data
{
177 unsigned int lun_list_length
;
178 unsigned int reserved1
;
179 unsigned char luns
[NUM_DRIVES
][8];
180 } __attribute__ ((packed
));
183 unsigned char ResponseCode
;
184 unsigned char Obsolete
;
185 unsigned char fei_sensekey
;
186 unsigned int Information
;
187 unsigned char AdditionalSenseLength
;
188 unsigned int CommandSpecificInformation
;
189 unsigned char AdditionalSenseCode
;
190 unsigned char AdditionalSenseCodeQualifier
;
191 unsigned char FieldReplaceableUnitCode
;
193 unsigned short SenseKeySpecific
;
194 } __attribute__ ((packed
));
196 struct mode_sense_bdesc_longlba
{
197 unsigned char num_blocks
[8];
198 unsigned char reserved
[4];
199 unsigned char block_size
[4];
200 } __attribute__ ((packed
));
202 struct mode_sense_bdesc_shortlba
{
203 unsigned char density_code
;
204 unsigned char num_blocks
[3];
205 unsigned char reserved
;
206 unsigned char block_size
[3];
207 } __attribute__ ((packed
));
209 struct mode_sense_data_10
{
210 unsigned short mode_data_length
;
211 unsigned char medium_type
;
212 unsigned char device_specific
;
213 unsigned char longlba
;
214 unsigned char reserved
;
215 unsigned short block_descriptor_length
;
216 struct mode_sense_bdesc_longlba block_descriptor
;
217 } __attribute__ ((packed
));
219 struct mode_sense_data_6
{
220 unsigned char mode_data_length
;
221 unsigned char medium_type
;
222 unsigned char device_specific
;
223 unsigned char block_descriptor_length
;
224 struct mode_sense_bdesc_shortlba block_descriptor
;
225 } __attribute__ ((packed
));
227 struct command_block_wrapper
{
228 unsigned int signature
;
230 unsigned int data_transfer_length
;
233 unsigned char command_length
;
234 unsigned char command_block
[16];
235 } __attribute__ ((packed
));
237 struct command_status_wrapper
{
238 unsigned int signature
;
240 unsigned int data_residue
;
241 unsigned char status
;
242 } __attribute__ ((packed
));
245 unsigned int block_count
;
246 unsigned int block_size
;
247 } __attribute__ ((packed
));
249 struct format_capacity
{
250 unsigned int following_length
;
251 unsigned int block_count
;
252 unsigned int block_size
;
253 } __attribute__ ((packed
));
257 unsigned char* transfer_buffer
;
258 struct inquiry_data
* inquiry
;
259 struct capacity
* capacity_data
;
260 struct format_capacity
* format_capacity_data
;
261 struct sense_data
*sense_data
;
262 struct mode_sense_data_6
*ms_data_6
;
263 struct mode_sense_data_10
*ms_data_10
;
264 struct report_lun_data
*lun_data
;
265 struct command_status_wrapper
* csw
;
269 static char *cbw_buffer
;
274 unsigned int orig_count
;
275 unsigned int cur_cmd
;
278 unsigned char *data
[2];
279 unsigned char data_select
;
280 unsigned int last_result
;
284 unsigned char sense_key
;
285 unsigned char information
;
290 static void handle_scsi(struct command_block_wrapper
* cbw
);
291 static void send_csw(int status
);
292 static void send_command_result(void *data
,int size
);
293 static void send_command_failed_result(void);
294 static void send_block_data(void *data
,int size
);
295 static void receive_block_data(void *data
,int size
);
296 static void receive_time(void);
297 static void fill_inquiry(IF_MD_NONVOID(int lun
));
298 static void send_and_read_next(void);
299 static bool ejected
[NUM_DRIVES
];
300 static bool locked
[NUM_DRIVES
];
302 static int usb_interface
;
303 static int ep_in
, ep_out
;
305 #ifdef USB_USE_RAMDISK
306 static unsigned char* ramdisk_buffer
;
313 SENDING_FAILED_RESULT
,
316 WAITING_FOR_CSW_COMPLETION_OR_COMMAND
,
317 WAITING_FOR_CSW_COMPLETION
318 } state
= WAITING_FOR_COMMAND
;
320 static void yearday_to_daymonth(int yd
, int y
, int *d
, int *m
)
322 static const char tnl
[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
323 static const char tl
[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
327 if((y
%4 == 0 && y
%100 != 0) || y
%400 == 0)
336 while(yd
>= t
[i
] && i
<12)
345 #ifdef TOSHIBA_GIGABEAT_S
347 /* The Gigabeat S factory partition table contains invalid values for the
348 "active" flag in the MBR. This prevents at least the Linux kernel from
349 accepting the partition table, so we fix it on-the-fly. */
351 static void fix_mbr(unsigned char* mbr
)
353 unsigned char* p
= mbr
+ 0x1be;
362 static bool check_disk_present(IF_MD_NONVOID(int volume
))
364 #ifdef USB_USE_RAMDISK
367 unsigned char sector
[SECTOR_SIZE
];
368 return storage_read_sectors(IF_MD2(volume
,)0,1,sector
) == 0;
372 void usb_storage_try_release_storage(void)
374 /* Check if there is a connected drive left. If not,
375 release excusive access */
376 bool canrelease
=true;
378 for(i
=0;i
<storage_num_drives();i
++) {
379 if(!ejected
[i
] && locked
[i
]) {
385 logf("scsi release ata");
386 usb_release_exclusive_storage();
391 void usb_storage_notify_hotswap(int volume
,bool inserted
)
393 logf("notify %d",inserted
);
394 if(inserted
&& check_disk_present(IF_MD(volume
))) {
395 ejected
[volume
] = false;
398 ejected
[volume
] = true;
399 /* If this happens while the device is locked, weird things may happen.
400 At least try to keep our state consistent */
401 locked
[volume
]=false;
406 /* called by usb_core_init() */
407 void usb_storage_init(void)
409 logf("usb_storage_init done");
412 int usb_storage_request_endpoints(struct usb_class_driver
*drv
)
414 ep_in
= usb_core_request_endpoint(USB_ENDPOINT_XFER_BULK
, USB_DIR_IN
, drv
);
419 ep_out
= usb_core_request_endpoint(USB_ENDPOINT_XFER_BULK
, USB_DIR_OUT
,
423 usb_core_release_endpoint(ep_in
);
430 int usb_storage_set_first_interface(int interface
)
432 usb_interface
= interface
;
433 return interface
+ 1;
436 int usb_storage_get_config_descriptor(unsigned char *dest
,int max_packet_size
)
438 unsigned char *orig_dest
= dest
;
440 interface_descriptor
.bInterfaceNumber
= usb_interface
;
441 PACK_DATA(dest
, interface_descriptor
);
443 endpoint_descriptor
.wMaxPacketSize
= max_packet_size
;
445 endpoint_descriptor
.bEndpointAddress
= ep_in
;
446 PACK_DATA(dest
, endpoint_descriptor
);
448 endpoint_descriptor
.bEndpointAddress
= ep_out
;
449 PACK_DATA(dest
, endpoint_descriptor
);
451 return (dest
- orig_dest
);
454 void usb_storage_init_connection(void)
456 logf("ums: set config");
457 /* prime rx endpoint. We only need room for commands */
458 state
= WAITING_FOR_COMMAND
;
460 #if CONFIG_CPU == IMX31L || defined(CPU_TCC77X) || defined(CPU_TCC780X) || \
461 defined(BOOTLOADER) || CONFIG_CPU == DM320
462 static unsigned char _cbw_buffer
[MAX_CBW_SIZE
]
463 USB_DEVBSS_ATTR
__attribute__((aligned(32)));
464 cbw_buffer
= (void *)_cbw_buffer
;
466 static unsigned char _transfer_buffer
[ALLOCATE_BUFFER_SIZE
]
467 USB_DEVBSS_ATTR
__attribute__((aligned(32)));
468 tb
.transfer_buffer
= (void *)_transfer_buffer
;
469 #ifdef USB_USE_RAMDISK
470 static unsigned char _ramdisk_buffer
[RAMDISK_SIZE
*SECTOR_SIZE
];
471 ramdisk_buffer
= _ramdisk_buffer
;
474 /* TODO : check if bufsize is at least 32K ? */
476 unsigned char * audio_buffer
;
478 audio_buffer
= audio_get_buffer(false,&bufsize
);
479 #if defined(UNCACHED_ADDR) && CONFIG_CPU != AS3525
480 cbw_buffer
= (void *)UNCACHED_ADDR((unsigned int)(audio_buffer
+31) & 0xffffffe0);
482 cbw_buffer
= (void *)((unsigned int)(audio_buffer
+31) & 0xffffffe0);
484 tb
.transfer_buffer
= cbw_buffer
+ MAX_CBW_SIZE
;
485 cpucache_invalidate();
486 #ifdef USB_USE_RAMDISK
487 ramdisk_buffer
= tb
.transfer_buffer
+ ALLOCATE_BUFFER_SIZE
;
490 usb_drv_recv(ep_out
, cbw_buffer
, MAX_CBW_SIZE
);
493 for(i
=0;i
<storage_num_drives();i
++) {
494 #ifdef TOSHIBA_GIGABEAT_S
495 /* As long as the Gigabeat S is a non-removable device, we need
496 to mark the device as locked to avoid usb_storage_try_release_ata()
497 to leave MSC mode while the device is in use */
502 ejected
[i
] = !check_disk_present(IF_MD(i
));
503 queue_broadcast(SYS_USB_LUN_LOCKED
, (i
<<16)+0);
507 void usb_storage_disconnect(void)
512 /* called by usb_core_transfer_complete() */
513 void usb_storage_transfer_complete(int ep
,int dir
,int status
,int length
)
516 struct command_block_wrapper
* cbw
= (void*)cbw_buffer
;
519 logf("transfer result for ep %d/%d %X %d", ep
,dir
,status
, length
);
521 case RECEIVING_BLOCKS
:
522 if(dir
==USB_DIR_IN
) {
523 logf("IN received in RECEIVING");
525 logf("scsi write %d %d", cur_cmd
.sector
, cur_cmd
.count
);
527 if((unsigned int)length
!=(SECTOR_SIZE
* cur_cmd
.count
)
528 && (unsigned int)length
!=WRITE_BUFFER_SIZE
) {
529 logf("unexpected length :%d",length
);
533 unsigned int next_sector
= cur_cmd
.sector
+
534 (WRITE_BUFFER_SIZE
/SECTOR_SIZE
);
535 unsigned int next_count
= cur_cmd
.count
-
536 MIN(cur_cmd
.count
,WRITE_BUFFER_SIZE
/SECTOR_SIZE
);
537 int next_select
= !cur_cmd
.data_select
;
540 /* Ask the host to send more, to the other buffer */
541 receive_block_data(cur_cmd
.data
[next_select
],
542 MIN(WRITE_BUFFER_SIZE
,next_count
*SECTOR_SIZE
));
545 /* Now write the data that just came in, while the host is
546 sending the next bit */
547 #ifdef USB_USE_RAMDISK
548 memcpy(ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
549 cur_cmd
.data
[cur_cmd
.data_select
],
550 MIN(WRITE_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
)*SECTOR_SIZE
);
552 int result
= storage_write_sectors(IF_MD2(cur_cmd
.lun
,)
554 MIN(WRITE_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
),
555 cur_cmd
.data
[cur_cmd
.data_select
]);
557 send_csw(UMS_STATUS_FAIL
);
558 cur_sense_data
.sense_key
=SENSE_MEDIUM_ERROR
;
559 cur_sense_data
.asc
=ASC_WRITE_ERROR
;
560 cur_sense_data
.ascq
=0;
565 send_csw(UMS_STATUS_GOOD
);
568 /* Switch buffers for the next one */
569 cur_cmd
.data_select
=!cur_cmd
.data_select
;
571 cur_cmd
.sector
= next_sector
;
572 cur_cmd
.count
= next_count
;
575 logf("Transfer failed %X",status
);
576 send_csw(UMS_STATUS_FAIL
);
577 /* TODO fill in cur_sense_data */
578 cur_sense_data
.sense_key
=0;
579 cur_sense_data
.information
=0;
580 cur_sense_data
.asc
=0;
581 cur_sense_data
.ascq
=0;
584 case WAITING_FOR_CSW_COMPLETION_OR_COMMAND
:
585 if(dir
==USB_DIR_IN
) {
586 /* This was the CSW */
587 state
= WAITING_FOR_COMMAND
;
590 /* This was the command */
591 state
= WAITING_FOR_CSW_COMPLETION
;
592 /* We now have the CBW, but we won't execute it yet to avoid
593 * issues with the still-pending CSW */
596 case WAITING_FOR_COMMAND
:
597 if(dir
==USB_DIR_IN
) {
598 logf("IN received in WAITING_FOR_COMMAND");
602 case WAITING_FOR_CSW_COMPLETION
:
603 if(dir
==USB_DIR_OUT
) {
604 logf("OUT received in WAITING_FOR_CSW_COMPLETION");
609 if(cur_cmd
.cur_cmd
== SCSI_WRITE_10
)
611 queue_broadcast(SYS_USB_WRITE_DATA
, (cur_cmd
.lun
<<16)+cur_cmd
.orig_count
);
613 else if(cur_cmd
.cur_cmd
== SCSI_READ_10
)
615 queue_broadcast(SYS_USB_READ_DATA
, (cur_cmd
.lun
<<16)+cur_cmd
.orig_count
);
620 if(dir
==USB_DIR_OUT
) {
621 logf("OUT received in SENDING");
624 //logf("data sent, now send csw");
625 send_csw(UMS_STATUS_GOOD
);
628 logf("Transfer failed %X",status
);
629 send_csw(UMS_STATUS_FAIL
);
630 /* TODO fill in cur_sense_data */
631 cur_sense_data
.sense_key
=0;
632 cur_sense_data
.information
=0;
633 cur_sense_data
.asc
=0;
634 cur_sense_data
.ascq
=0;
637 case SENDING_FAILED_RESULT
:
638 if(dir
==USB_DIR_OUT
) {
639 logf("OUT received in SENDING");
641 send_csw(UMS_STATUS_FAIL
);
644 if(dir
==USB_DIR_OUT
) {
645 logf("OUT received in SENDING");
648 if(cur_cmd
.count
==0) {
649 //logf("data sent, now send csw");
650 if(cur_cmd
.last_result
!=0) {
651 /* The last read failed. */
652 send_csw(UMS_STATUS_FAIL
);
653 cur_sense_data
.sense_key
=SENSE_MEDIUM_ERROR
;
654 cur_sense_data
.asc
=ASC_READ_ERROR
;
655 cur_sense_data
.ascq
=0;
659 send_csw(UMS_STATUS_GOOD
);
662 send_and_read_next();
666 logf("Transfer failed %X",status
);
667 send_csw(UMS_STATUS_FAIL
);
668 /* TODO fill in cur_sense_data */
669 cur_sense_data
.sense_key
=0;
670 cur_sense_data
.information
=0;
671 cur_sense_data
.asc
=0;
672 cur_sense_data
.ascq
=0;
676 tm
.tm_year
=(tb
.transfer_buffer
[0]<<8)+tb
.transfer_buffer
[1] - 1900;
677 tm
.tm_yday
=(tb
.transfer_buffer
[2]<<8)+tb
.transfer_buffer
[3];
678 tm
.tm_hour
=tb
.transfer_buffer
[5];
679 tm
.tm_min
=tb
.transfer_buffer
[6];
680 tm
.tm_sec
=tb
.transfer_buffer
[7];
681 yearday_to_daymonth(tm
.tm_yday
,tm
.tm_year
+ 1900,&tm
.tm_mday
,&tm
.tm_mon
);
682 set_day_of_week(&tm
);
684 send_csw(UMS_STATUS_GOOD
);
689 /* called by usb_core_control_request() */
690 bool usb_storage_control_request(struct usb_ctrlrequest
* req
, unsigned char* dest
)
692 bool handled
= false;
695 switch (req
->bRequest
) {
696 case USB_BULK_GET_MAX_LUN
: {
697 *tb
.max_lun
= storage_num_drives() - 1;
698 #ifdef HIDE_FIRST_DRIVE
701 logf("ums: getmaxlun");
702 usb_drv_recv(EP_CONTROL
, NULL
, 0); /* ack */
703 usb_drv_send(EP_CONTROL
, tb
.max_lun
, 1);
708 case USB_BULK_RESET_REQUEST
:
709 logf("ums: bulk reset");
710 state
= WAITING_FOR_COMMAND
;
711 /* UMS BOT 3.1 says The device shall preserve the value of its bulk
712 data toggle bits and endpoint STALL conditions despite
713 the Bulk-Only Mass Storage Reset. */
715 usb_drv_reset_endpoint(ep_in
, false);
716 usb_drv_reset_endpoint(ep_out
, true);
718 usb_drv_send(EP_CONTROL
, NULL
, 0); /* ack */
726 static void send_and_read_next(void)
728 send_block_data(cur_cmd
.data
[cur_cmd
.data_select
],
729 MIN(READ_BUFFER_SIZE
,cur_cmd
.count
*SECTOR_SIZE
));
731 /* Switch buffers for the next one */
732 cur_cmd
.data_select
=!cur_cmd
.data_select
;
734 cur_cmd
.sector
+=(READ_BUFFER_SIZE
/SECTOR_SIZE
);
735 cur_cmd
.count
-=MIN(cur_cmd
.count
,READ_BUFFER_SIZE
/SECTOR_SIZE
);
737 if(cur_cmd
.count
!=0) {
738 /* already read the next bit, so we can send it out immediately when the
739 * current transfer completes. */
740 #ifdef USB_USE_RAMDISK
741 memcpy(cur_cmd
.data
[cur_cmd
.data_select
],
742 ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
743 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
)*SECTOR_SIZE
);
745 int result
= storage_read_sectors(IF_MD2(cur_cmd
.lun
,)
747 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
),
748 cur_cmd
.data
[cur_cmd
.data_select
]);
749 if(cur_cmd
.last_result
== 0)
750 cur_cmd
.last_result
= result
;
755 /****************************************************************************/
757 static void handle_scsi(struct command_block_wrapper
* cbw
)
759 /* USB Mass Storage assumes LBA capability.
760 TODO: support 48-bit LBA */
762 struct storage_info info
;
763 unsigned int length
= cbw
->data_transfer_length
;
764 unsigned int block_size
= 0;
765 unsigned int block_count
= 0;
766 bool lun_present
=true;
767 unsigned char lun
= cbw
->lun
;
768 unsigned int block_size_mult
= 1;
770 if(letoh32(cbw
->signature
) != CBW_SIGNATURE
) {
771 usb_drv_stall(ep_in
, true,true);
772 usb_drv_stall(ep_out
, true,false);
775 /* Clear the signature to prevent possible bugs elsewhere
776 * to trigger a second execution of the same command with
780 #ifdef HIDE_FIRST_DRIVE
784 storage_get_info(lun
,&info
);
785 #ifdef USB_USE_RAMDISK
786 block_size
= SECTOR_SIZE
;
787 block_count
= RAMDISK_SIZE
;
789 block_size
=info
.sector_size
;
790 block_count
=info
.num_sectors
;
794 if(storage_removable(lun
) && !storage_present(lun
)) {
802 #ifdef MAX_LOG_SECTOR_SIZE
803 block_size_mult
= disk_sector_multiplier
;
806 cur_cmd
.tag
= cbw
->tag
;
808 cur_cmd
.cur_cmd
= cbw
->command_block
[0];
810 switch (cbw
->command_block
[0]) {
811 case SCSI_TEST_UNIT_READY
:
812 logf("scsi test_unit_ready %d",lun
);
813 if(!usb_exclusive_storage()) {
814 send_csw(UMS_STATUS_FAIL
);
815 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
816 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
817 cur_sense_data
.ascq
=0;
821 send_csw(UMS_STATUS_GOOD
);
824 send_csw(UMS_STATUS_FAIL
);
825 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
826 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
827 cur_sense_data
.ascq
=0;
831 case SCSI_REPORT_LUNS
: {
832 logf("scsi report luns %d",lun
);
833 int allocation_length
=0;
835 unsigned int response_length
= 8+8*storage_num_drives();
836 allocation_length
|=(cbw
->command_block
[6]<<24);
837 allocation_length
|=(cbw
->command_block
[7]<<16);
838 allocation_length
|=(cbw
->command_block
[8]<<8);
839 allocation_length
|=(cbw
->command_block
[9]);
840 memset(tb
.lun_data
,0,sizeof(struct report_lun_data
));
841 tb
.lun_data
->lun_list_length
=htobe32(8*storage_num_drives());
842 for(i
=0;i
<storage_num_drives();i
++)
845 if(storage_removable(i
))
846 tb
.lun_data
->luns
[i
][1]=1;
849 tb
.lun_data
->luns
[i
][1]=0;
851 send_command_result(tb
.lun_data
,
852 MIN(response_length
, length
));
857 logf("scsi inquiry %d",lun
);
858 fill_inquiry(IF_MD(lun
));
859 length
= MIN(length
, cbw
->command_block
[4]);
860 send_command_result(tb
.inquiry
,
861 MIN(sizeof(struct inquiry_data
), length
));
864 case SCSI_REQUEST_SENSE
: {
865 tb
.sense_data
->ResponseCode
=0x70;/*current error*/
866 tb
.sense_data
->Obsolete
=0;
867 tb
.sense_data
->fei_sensekey
=cur_sense_data
.sense_key
&0x0f;
868 tb
.sense_data
->Information
=cur_sense_data
.information
;
869 tb
.sense_data
->AdditionalSenseLength
=10;
870 tb
.sense_data
->CommandSpecificInformation
=0;
871 tb
.sense_data
->AdditionalSenseCode
=cur_sense_data
.asc
;
872 tb
.sense_data
->AdditionalSenseCodeQualifier
=cur_sense_data
.ascq
;
873 tb
.sense_data
->FieldReplaceableUnitCode
=0;
874 tb
.sense_data
->SKSV
=0;
875 tb
.sense_data
->SenseKeySpecific
=0;
876 logf("scsi request_sense %d",lun
);
877 send_command_result(tb
.sense_data
,
878 MIN(sizeof(struct sense_data
), length
));
882 case SCSI_MODE_SENSE_10
: {
884 send_command_failed_result();
885 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
886 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
887 cur_sense_data
.ascq
=0;
890 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
891 unsigned char page_code
= cbw
->command_block
[2] & 0x3f;
892 logf("scsi mode_sense_10 %d %X",lun
,page_code
);
895 tb
.ms_data_10
->mode_data_length
=
896 htobe16(sizeof(struct mode_sense_data_10
)-2);
897 tb
.ms_data_10
->medium_type
= 0;
898 tb
.ms_data_10
->device_specific
= 0;
899 tb
.ms_data_10
->reserved
= 0;
900 tb
.ms_data_10
->longlba
= 1;
901 tb
.ms_data_10
->block_descriptor_length
=
902 htobe16(sizeof(struct mode_sense_bdesc_longlba
));
904 memset(tb
.ms_data_10
->block_descriptor
.reserved
,0,4);
905 memset(tb
.ms_data_10
->block_descriptor
.num_blocks
,0,8);
907 tb
.ms_data_10
->block_descriptor
.num_blocks
[4] =
908 ((block_count
/block_size_mult
) & 0xff000000)>>24;
909 tb
.ms_data_10
->block_descriptor
.num_blocks
[5] =
910 ((block_count
/block_size_mult
) & 0x00ff0000)>>16;
911 tb
.ms_data_10
->block_descriptor
.num_blocks
[6] =
912 ((block_count
/block_size_mult
) & 0x0000ff00)>>8;
913 tb
.ms_data_10
->block_descriptor
.num_blocks
[7] =
914 ((block_count
/block_size_mult
) & 0x000000ff);
916 tb
.ms_data_10
->block_descriptor
.block_size
[0] =
917 ((block_size
*block_size_mult
) & 0xff000000)>>24;
918 tb
.ms_data_10
->block_descriptor
.block_size
[1] =
919 ((block_size
*block_size_mult
) & 0x00ff0000)>>16;
920 tb
.ms_data_10
->block_descriptor
.block_size
[2] =
921 ((block_size
*block_size_mult
) & 0x0000ff00)>>8;
922 tb
.ms_data_10
->block_descriptor
.block_size
[3] =
923 ((block_size
*block_size_mult
) & 0x000000ff);
924 send_command_result(tb
.ms_data_10
,
925 MIN(sizeof(struct mode_sense_data_10
), length
));
928 send_command_failed_result();
929 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
930 cur_sense_data
.asc
=ASC_INVALID_FIELD_IN_CBD
;
931 cur_sense_data
.ascq
=0;
937 case SCSI_MODE_SENSE_6
: {
939 send_command_failed_result();
940 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
941 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
942 cur_sense_data
.ascq
=0;
945 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
946 unsigned char page_code
= cbw
->command_block
[2] & 0x3f;
947 logf("scsi mode_sense_6 %d %X",lun
,page_code
);
950 /* All supported pages. */
951 tb
.ms_data_6
->mode_data_length
=
952 sizeof(struct mode_sense_data_6
)-1;
953 tb
.ms_data_6
->medium_type
= 0;
954 tb
.ms_data_6
->device_specific
= 0;
955 tb
.ms_data_6
->block_descriptor_length
=
956 sizeof(struct mode_sense_bdesc_shortlba
);
957 tb
.ms_data_6
->block_descriptor
.density_code
= 0;
958 tb
.ms_data_6
->block_descriptor
.reserved
= 0;
959 if(block_count
/block_size_mult
> 0xffffff) {
960 tb
.ms_data_6
->block_descriptor
.num_blocks
[0] = 0xff;
961 tb
.ms_data_6
->block_descriptor
.num_blocks
[1] = 0xff;
962 tb
.ms_data_6
->block_descriptor
.num_blocks
[2] = 0xff;
965 tb
.ms_data_6
->block_descriptor
.num_blocks
[0] =
966 ((block_count
/block_size_mult
) & 0xff0000)>>16;
967 tb
.ms_data_6
->block_descriptor
.num_blocks
[1] =
968 ((block_count
/block_size_mult
) & 0x00ff00)>>8;
969 tb
.ms_data_6
->block_descriptor
.num_blocks
[2] =
970 ((block_count
/block_size_mult
) & 0x0000ff);
972 tb
.ms_data_6
->block_descriptor
.block_size
[0] =
973 ((block_size
*block_size_mult
) & 0xff0000)>>16;
974 tb
.ms_data_6
->block_descriptor
.block_size
[1] =
975 ((block_size
*block_size_mult
) & 0x00ff00)>>8;
976 tb
.ms_data_6
->block_descriptor
.block_size
[2] =
977 ((block_size
*block_size_mult
) & 0x0000ff);
978 send_command_result(tb
.ms_data_6
,
979 MIN(sizeof(struct mode_sense_data_6
), length
));
982 send_command_failed_result();
983 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
984 cur_sense_data
.asc
=ASC_INVALID_FIELD_IN_CBD
;
985 cur_sense_data
.ascq
=0;
991 case SCSI_START_STOP_UNIT
:
992 logf("scsi start_stop unit %d",lun
);
993 if((cbw
->command_block
[4] & 0xf0) == 0) /*load/eject bit is valid*/
994 { /* Process start and eject bits */
995 logf("scsi load/eject");
996 if((cbw
->command_block
[4] & 0x01) == 0) /* Don't start */
998 if((cbw
->command_block
[4] & 0x02) != 0) /* eject */
1005 send_csw(UMS_STATUS_GOOD
);
1008 case SCSI_ALLOW_MEDIUM_REMOVAL
:
1009 logf("scsi allow_medium_removal %d",lun
);
1010 if((cbw
->command_block
[4] & 0x03) == 0) {
1012 queue_broadcast(SYS_USB_LUN_LOCKED
, (lun
<<16)+0);
1016 queue_broadcast(SYS_USB_LUN_LOCKED
, (lun
<<16)+1);
1018 send_csw(UMS_STATUS_GOOD
);
1021 case SCSI_READ_FORMAT_CAPACITY
: {
1022 logf("scsi read_format_capacity %d",lun
);
1024 tb
.format_capacity_data
->following_length
=htobe32(8);
1025 /* "block count" actually means "number of last block" */
1026 tb
.format_capacity_data
->block_count
=
1027 htobe32(block_count
/block_size_mult
- 1);
1028 tb
.format_capacity_data
->block_size
=
1029 htobe32(block_size
*block_size_mult
);
1030 tb
.format_capacity_data
->block_size
|=
1031 htobe32(SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA
);
1033 send_command_result(tb
.format_capacity_data
,
1034 MIN(sizeof(struct format_capacity
), length
));
1037 send_command_failed_result();
1038 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1039 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1040 cur_sense_data
.ascq
=0;
1045 case SCSI_READ_CAPACITY
: {
1046 logf("scsi read_capacity %d",lun
);
1049 /* "block count" actually means "number of last block" */
1050 tb
.capacity_data
->block_count
=
1051 htobe32(block_count
/block_size_mult
- 1);
1052 tb
.capacity_data
->block_size
=
1053 htobe32(block_size
*block_size_mult
);
1055 send_command_result(tb
.capacity_data
,
1056 MIN(sizeof(struct capacity
), length
));
1059 send_command_failed_result();
1060 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1061 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1062 cur_sense_data
.ascq
=0;
1068 logf("scsi read10 %d",lun
);
1070 send_command_failed_result();
1071 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1072 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1073 cur_sense_data
.ascq
=0;
1076 cur_cmd
.data
[0] = tb
.transfer_buffer
;
1077 cur_cmd
.data
[1] = &tb
.transfer_buffer
[READ_BUFFER_SIZE
];
1078 cur_cmd
.data_select
=0;
1079 cur_cmd
.sector
= block_size_mult
*
1080 (cbw
->command_block
[2] << 24 |
1081 cbw
->command_block
[3] << 16 |
1082 cbw
->command_block
[4] << 8 |
1083 cbw
->command_block
[5] );
1084 cur_cmd
.count
= block_size_mult
*
1085 (cbw
->command_block
[7] << 8 |
1086 cbw
->command_block
[8]);
1087 cur_cmd
.orig_count
= cur_cmd
.count
;
1089 //logf("scsi read %d %d", cur_cmd.sector, cur_cmd.count);
1091 if((cur_cmd
.sector
+ cur_cmd
.count
) > block_count
) {
1092 send_csw(UMS_STATUS_FAIL
);
1093 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1094 cur_sense_data
.asc
=ASC_LBA_OUT_OF_RANGE
;
1095 cur_sense_data
.ascq
=0;
1098 #ifdef USB_USE_RAMDISK
1099 memcpy(cur_cmd
.data
[cur_cmd
.data_select
],
1100 ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
1101 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
,cur_cmd
.count
)*SECTOR_SIZE
);
1103 cur_cmd
.last_result
= storage_read_sectors(IF_MD2(cur_cmd
.lun
,)
1105 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
),
1106 cur_cmd
.data
[cur_cmd
.data_select
]);
1108 #ifdef TOSHIBA_GIGABEAT_S
1109 if(cur_cmd
.sector
== 0) {
1110 fix_mbr(cur_cmd
.data
[cur_cmd
.data_select
]);
1114 send_and_read_next();
1119 logf("scsi write10 %d",lun
);
1121 send_csw(UMS_STATUS_FAIL
);
1122 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1123 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1124 cur_sense_data
.ascq
=0;
1127 cur_cmd
.data
[0] = tb
.transfer_buffer
;
1128 cur_cmd
.data
[1] = &tb
.transfer_buffer
[WRITE_BUFFER_SIZE
];
1129 cur_cmd
.data_select
=0;
1130 cur_cmd
.sector
= block_size_mult
*
1131 (cbw
->command_block
[2] << 24 |
1132 cbw
->command_block
[3] << 16 |
1133 cbw
->command_block
[4] << 8 |
1134 cbw
->command_block
[5] );
1135 cur_cmd
.count
= block_size_mult
*
1136 (cbw
->command_block
[7] << 8 |
1137 cbw
->command_block
[8]);
1138 cur_cmd
.orig_count
= cur_cmd
.count
;
1141 if((cur_cmd
.sector
+ cur_cmd
.count
) > block_count
) {
1142 send_csw(UMS_STATUS_FAIL
);
1143 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1144 cur_sense_data
.asc
=ASC_LBA_OUT_OF_RANGE
;
1145 cur_sense_data
.ascq
=0;
1148 receive_block_data(cur_cmd
.data
[0],
1149 MIN(WRITE_BUFFER_SIZE
, cur_cmd
.count
*SECTOR_SIZE
));
1153 case SCSI_WRITE_BUFFER
:
1154 if(cbw
->command_block
[1]==1 /* mode = vendor specific */
1155 && cbw
->command_block
[2]==0 /* buffer id = 0 */
1157 && cbw
->command_block
[3]==0x0c /* offset (3 bytes) */
1158 && cbw
->command_block
[4]==0
1159 && cbw
->command_block
[5]==0
1161 /* Some versions of itunes set the parameter list length to 0.
1162 * Technically it should be 0x0c, which is what libgpod sends */
1163 && cbw
->command_block
[6]==0 /* parameter list (3 bytes) */
1164 && cbw
->command_block
[7]==0
1165 && (cbw
->command_block
[8]==0 || cbw
->command_block
[8]==0x0c)
1167 && cbw
->command_block
[9]==0)
1172 logf("scsi unknown cmd %x",cbw
->command_block
[0x0]);
1173 send_csw(UMS_STATUS_FAIL
);
1174 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1175 cur_sense_data
.asc
=ASC_INVALID_COMMAND
;
1176 cur_sense_data
.ascq
=0;
1181 static void send_block_data(void *data
,int size
)
1183 usb_drv_send_nonblocking(ep_in
, data
,size
);
1184 state
= SENDING_BLOCKS
;
1187 static void send_command_result(void *data
,int size
)
1189 usb_drv_send_nonblocking(ep_in
, data
,size
);
1190 state
= SENDING_RESULT
;
1193 static void send_command_failed_result(void)
1195 usb_drv_send_nonblocking(ep_in
, NULL
, 0);
1196 state
= SENDING_FAILED_RESULT
;
1199 static void receive_time(void)
1201 usb_drv_recv(ep_out
, tb
.transfer_buffer
, 12);
1202 state
= RECEIVING_TIME
;
1204 static void receive_block_data(void *data
,int size
)
1206 usb_drv_recv(ep_out
, data
, size
);
1207 state
= RECEIVING_BLOCKS
;
1210 static void send_csw(int status
)
1212 tb
.csw
->signature
= htole32(CSW_SIGNATURE
);
1213 tb
.csw
->tag
= cur_cmd
.tag
;
1214 tb
.csw
->data_residue
= 0;
1215 tb
.csw
->status
= status
;
1217 usb_drv_send_nonblocking(ep_in
, tb
.csw
,
1218 sizeof(struct command_status_wrapper
));
1219 state
= WAITING_FOR_CSW_COMPLETION_OR_COMMAND
;
1220 //logf("CSW: %X",status);
1221 /* Already start waiting for the next command */
1222 usb_drv_recv(ep_out
, cbw_buffer
, MAX_CBW_SIZE
);
1223 /* The next completed transfer will be either the CSW one
1224 * or the new command */
1226 if(status
== UMS_STATUS_GOOD
) {
1227 cur_sense_data
.sense_key
=0;
1228 cur_sense_data
.information
=0;
1229 cur_sense_data
.asc
=0;
1230 cur_sense_data
.ascq
=0;
1234 static void copy_padded(char *dest
, char *src
, int len
)
1237 while(src
[i
]!=0 && i
<len
)
1249 /* build SCSI INQUIRY */
1250 static void fill_inquiry(IF_MD_NONVOID(int lun
))
1252 struct storage_info info
;
1253 memset(tb
.inquiry
, 0, sizeof(struct inquiry_data
));
1254 storage_get_info(lun
,&info
);
1255 copy_padded(tb
.inquiry
->VendorId
,info
.vendor
,sizeof(tb
.inquiry
->VendorId
));
1256 copy_padded(tb
.inquiry
->ProductId
,info
.product
,sizeof(tb
.inquiry
->ProductId
));
1257 copy_padded(tb
.inquiry
->ProductRevisionLevel
,info
.revision
,sizeof(tb
.inquiry
->ProductRevisionLevel
));
1259 tb
.inquiry
->DeviceType
= DIRECT_ACCESS_DEVICE
;
1260 tb
.inquiry
->AdditionalLength
= 0x1f;
1261 memset(tb
.inquiry
->Reserved
, 0, 3);
1262 tb
.inquiry
->Versions
= 4; /* SPC-2 */
1263 tb
.inquiry
->Format
= 2; /* SPC-2/3 inquiry format */
1265 #ifdef TOSHIBA_GIGABEAT_S
1266 tb
.inquiry
->DeviceTypeModifier
= 0;
1268 tb
.inquiry
->DeviceTypeModifier
= DEVICE_REMOVABLE
;