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 ONLY_EXPOSE_CARD_SLOT
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 #define READ_BUFFER_SIZE (1024*64)
65 #define MAX_CBW_SIZE 1024
67 #ifdef USB_WRITE_BUFFER_SIZE
68 #define WRITE_BUFFER_SIZE USB_WRITE_BUFFER_SIZE
70 #if (CONFIG_STORAGE & STORAGE_SD)
71 #define WRITE_BUFFER_SIZE (1024*64)
73 #define WRITE_BUFFER_SIZE (1024*24)
77 #define ALLOCATE_BUFFER_SIZE (2*MAX(READ_BUFFER_SIZE,WRITE_BUFFER_SIZE))
79 /* bulk-only class specific requests */
80 #define USB_BULK_RESET_REQUEST 0xff
81 #define USB_BULK_GET_MAX_LUN 0xfe
83 #define DIRECT_ACCESS_DEVICE 0x00 /* disks */
84 #define DEVICE_REMOVABLE 0x80
86 #define CBW_SIGNATURE 0x43425355
87 #define CSW_SIGNATURE 0x53425355
89 #define SCSI_TEST_UNIT_READY 0x00
90 #define SCSI_INQUIRY 0x12
91 #define SCSI_MODE_SENSE_6 0x1a
92 #define SCSI_MODE_SENSE_10 0x5a
93 #define SCSI_REQUEST_SENSE 0x03
94 #define SCSI_ALLOW_MEDIUM_REMOVAL 0x1e
95 #define SCSI_READ_CAPACITY 0x25
96 #define SCSI_READ_FORMAT_CAPACITY 0x23
97 #define SCSI_READ_10 0x28
98 #define SCSI_WRITE_10 0x2a
99 #define SCSI_START_STOP_UNIT 0x1b
100 #define SCSI_REPORT_LUNS 0xa0
101 #define SCSI_WRITE_BUFFER 0x3b
103 #define UMS_STATUS_GOOD 0x00
104 #define UMS_STATUS_FAIL 0x01
106 #define SENSE_NOT_READY 0x02
107 #define SENSE_MEDIUM_ERROR 0x03
108 #define SENSE_ILLEGAL_REQUEST 0x05
109 #define SENSE_UNIT_ATTENTION 0x06
111 #define ASC_MEDIUM_NOT_PRESENT 0x3a
112 #define ASC_INVALID_FIELD_IN_CBD 0x24
113 #define ASC_LBA_OUT_OF_RANGE 0x21
114 #define ASC_WRITE_ERROR 0x0C
115 #define ASC_READ_ERROR 0x11
116 #define ASC_NOT_READY 0x04
117 #define ASC_INVALID_COMMAND 0x20
119 #define ASCQ_BECOMING_READY 0x01
121 #define SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA 0x02000000
123 /* storage interface */
125 #define USB_SC_SCSI 0x06 /* Transparent */
126 #define USB_PROT_BULK 0x50 /* bulk only */
128 static struct usb_interface_descriptor
__attribute__((aligned(2)))
129 interface_descriptor
=
131 .bLength
= sizeof(struct usb_interface_descriptor
),
132 .bDescriptorType
= USB_DT_INTERFACE
,
133 .bInterfaceNumber
= 0,
134 .bAlternateSetting
= 0,
136 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
137 .bInterfaceSubClass
= USB_SC_SCSI
,
138 .bInterfaceProtocol
= USB_PROT_BULK
,
142 static struct usb_endpoint_descriptor
__attribute__((aligned(2)))
143 endpoint_descriptor
=
145 .bLength
= sizeof(struct usb_endpoint_descriptor
),
146 .bDescriptorType
= USB_DT_ENDPOINT
,
147 .bEndpointAddress
= 0,
148 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
153 struct inquiry_data
{
154 unsigned char DeviceType
;
155 unsigned char DeviceTypeModifier
;
156 unsigned char Versions
;
157 unsigned char Format
;
158 unsigned char AdditionalLength
;
159 unsigned char Reserved
[2];
160 unsigned char Capability
;
161 unsigned char VendorId
[8];
162 unsigned char ProductId
[16];
163 unsigned char ProductRevisionLevel
[4];
164 } __attribute__ ((packed
));
166 struct report_lun_data
{
167 unsigned int lun_list_length
;
168 unsigned int reserved1
;
169 unsigned char luns
[NUM_DRIVES
][8];
170 } __attribute__ ((packed
));
173 unsigned char ResponseCode
;
174 unsigned char Obsolete
;
175 unsigned char fei_sensekey
;
176 unsigned int Information
;
177 unsigned char AdditionalSenseLength
;
178 unsigned int CommandSpecificInformation
;
179 unsigned char AdditionalSenseCode
;
180 unsigned char AdditionalSenseCodeQualifier
;
181 unsigned char FieldReplaceableUnitCode
;
183 unsigned short SenseKeySpecific
;
184 } __attribute__ ((packed
));
186 struct mode_sense_bdesc_longlba
{
187 unsigned char num_blocks
[8];
188 unsigned char reserved
[4];
189 unsigned char block_size
[4];
190 } __attribute__ ((packed
));
192 struct mode_sense_bdesc_shortlba
{
193 unsigned char density_code
;
194 unsigned char num_blocks
[3];
195 unsigned char reserved
;
196 unsigned char block_size
[3];
197 } __attribute__ ((packed
));
199 struct mode_sense_data_10
{
200 unsigned short mode_data_length
;
201 unsigned char medium_type
;
202 unsigned char device_specific
;
203 unsigned char longlba
;
204 unsigned char reserved
;
205 unsigned short block_descriptor_length
;
206 struct mode_sense_bdesc_longlba block_descriptor
;
207 } __attribute__ ((packed
));
209 struct mode_sense_data_6
{
210 unsigned char mode_data_length
;
211 unsigned char medium_type
;
212 unsigned char device_specific
;
213 unsigned char block_descriptor_length
;
214 struct mode_sense_bdesc_shortlba block_descriptor
;
215 } __attribute__ ((packed
));
217 struct command_block_wrapper
{
218 unsigned int signature
;
220 unsigned int data_transfer_length
;
223 unsigned char command_length
;
224 unsigned char command_block
[16];
225 } __attribute__ ((packed
));
227 struct command_status_wrapper
{
228 unsigned int signature
;
230 unsigned int data_residue
;
231 unsigned char status
;
232 } __attribute__ ((packed
));
235 unsigned int block_count
;
236 unsigned int block_size
;
237 } __attribute__ ((packed
));
239 struct format_capacity
{
240 unsigned int following_length
;
241 unsigned int block_count
;
242 unsigned int block_size
;
243 } __attribute__ ((packed
));
247 unsigned char* transfer_buffer
;
248 struct inquiry_data
* inquiry
;
249 struct capacity
* capacity_data
;
250 struct format_capacity
* format_capacity_data
;
251 struct sense_data
*sense_data
;
252 struct mode_sense_data_6
*ms_data_6
;
253 struct mode_sense_data_10
*ms_data_10
;
254 struct report_lun_data
*lun_data
;
255 struct command_status_wrapper
* csw
;
259 static char *cbw_buffer
;
264 unsigned int orig_count
;
265 unsigned int cur_cmd
;
268 unsigned char *data
[2];
269 unsigned char data_select
;
270 unsigned int last_result
;
274 unsigned char sense_key
;
275 unsigned char information
;
280 static void handle_scsi(struct command_block_wrapper
* cbw
);
281 static void send_csw(int status
);
282 static void send_command_result(void *data
,int size
);
283 static void send_command_failed_result(void);
284 static void send_block_data(void *data
,int size
);
285 static void receive_block_data(void *data
,int size
);
286 static void receive_time(void);
287 static void fill_inquiry(IF_MD_NONVOID(int lun
));
288 static void send_and_read_next(void);
289 static bool ejected
[NUM_DRIVES
];
290 static bool locked
[NUM_DRIVES
];
292 static int usb_interface
;
293 static int ep_in
, ep_out
;
295 #ifdef USB_USE_RAMDISK
296 static unsigned char* ramdisk_buffer
;
303 SENDING_FAILED_RESULT
,
306 WAITING_FOR_CSW_COMPLETION_OR_COMMAND
,
307 WAITING_FOR_CSW_COMPLETION
308 } state
= WAITING_FOR_COMMAND
;
310 static void yearday_to_daymonth(int yd
, int y
, int *d
, int *m
)
312 static const char tnl
[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
313 static const char tl
[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
317 if((y
%4 == 0 && y
%100 != 0) || y
%400 == 0)
326 while(yd
>= t
[i
] && i
<12)
335 #ifdef TOSHIBA_GIGABEAT_S
337 /* The Gigabeat S factory partition table contains invalid values for the
338 "active" flag in the MBR. This prevents at least the Linux kernel from
339 accepting the partition table, so we fix it on-the-fly. */
341 static void fix_mbr(unsigned char* mbr
)
343 unsigned char* p
= mbr
+ 0x1be;
352 static bool check_disk_present(IF_MD_NONVOID(int volume
))
354 #ifdef USB_USE_RAMDISK
357 unsigned char sector
[SECTOR_SIZE
];
358 return storage_read_sectors(IF_MD2(volume
,)0,1,sector
) == 0;
362 void usb_storage_try_release_storage(void)
364 /* Check if there is a connected drive left. If not,
365 release excusive access */
366 bool canrelease
=true;
368 for(i
=0;i
<storage_num_drives();i
++) {
369 if(!ejected
[i
] && locked
[i
]) {
375 logf("scsi release ata");
376 usb_release_exclusive_storage();
381 void usb_storage_notify_hotswap(int volume
,bool inserted
)
383 logf("notify %d",inserted
);
384 if(inserted
&& check_disk_present(IF_MD(volume
))) {
385 ejected
[volume
] = false;
388 ejected
[volume
] = true;
389 /* If this happens while the device is locked, weird things may happen.
390 At least try to keep our state consistent */
391 locked
[volume
]=false;
396 /* called by usb_core_init() */
397 void usb_storage_init(void)
399 logf("usb_storage_init done");
402 int usb_storage_request_endpoints(struct usb_class_driver
*drv
)
404 ep_in
= usb_core_request_endpoint(USB_ENDPOINT_XFER_BULK
, USB_DIR_IN
, drv
);
409 ep_out
= usb_core_request_endpoint(USB_ENDPOINT_XFER_BULK
, USB_DIR_OUT
,
413 usb_core_release_endpoint(ep_in
);
420 int usb_storage_set_first_interface(int interface
)
422 usb_interface
= interface
;
423 return interface
+ 1;
426 int usb_storage_get_config_descriptor(unsigned char *dest
,int max_packet_size
)
428 unsigned char *orig_dest
= dest
;
430 interface_descriptor
.bInterfaceNumber
= usb_interface
;
431 PACK_DATA(dest
, interface_descriptor
);
433 endpoint_descriptor
.wMaxPacketSize
= max_packet_size
;
435 endpoint_descriptor
.bEndpointAddress
= ep_in
;
436 PACK_DATA(dest
, endpoint_descriptor
);
438 endpoint_descriptor
.bEndpointAddress
= ep_out
;
439 PACK_DATA(dest
, endpoint_descriptor
);
441 return (dest
- orig_dest
);
444 void usb_storage_init_connection(void)
446 logf("ums: set config");
447 /* prime rx endpoint. We only need room for commands */
448 state
= WAITING_FOR_COMMAND
;
450 #if CONFIG_CPU == IMX31L || defined(CPU_TCC77X) || defined(CPU_TCC780X) || \
451 defined(BOOTLOADER) || CONFIG_CPU == DM320
452 static unsigned char _cbw_buffer
[MAX_CBW_SIZE
]
453 USB_DEVBSS_ATTR
__attribute__((aligned(32)));
454 cbw_buffer
= (void *)_cbw_buffer
;
456 static unsigned char _transfer_buffer
[ALLOCATE_BUFFER_SIZE
]
457 USB_DEVBSS_ATTR
__attribute__((aligned(32)));
458 tb
.transfer_buffer
= (void *)_transfer_buffer
;
459 #ifdef USB_USE_RAMDISK
460 static unsigned char _ramdisk_buffer
[RAMDISK_SIZE
*SECTOR_SIZE
];
461 ramdisk_buffer
= _ramdisk_buffer
;
464 /* TODO : check if bufsize is at least 32K ? */
466 unsigned char * audio_buffer
;
468 audio_buffer
= audio_get_buffer(false,&bufsize
);
470 cbw_buffer
= (void *)UNCACHED_ADDR((unsigned int)(audio_buffer
+31) & 0xffffffe0);
472 cbw_buffer
= (void *)((unsigned int)(audio_buffer
+31) & 0xffffffe0);
474 tb
.transfer_buffer
= cbw_buffer
+ MAX_CBW_SIZE
;
475 cpucache_invalidate();
476 #ifdef USB_USE_RAMDISK
477 ramdisk_buffer
= tb
.transfer_buffer
+ ALLOCATE_BUFFER_SIZE
;
480 usb_drv_recv(ep_out
, cbw_buffer
, MAX_CBW_SIZE
);
483 for(i
=0;i
<storage_num_drives();i
++) {
484 #ifdef TOSHIBA_GIGABEAT_S
485 /* As long as the Gigabeat S is a non-removable device, we need
486 to mark the device as locked to avoid usb_storage_try_release_ata()
487 to leave MSC mode while the device is in use */
492 ejected
[i
] = !check_disk_present(IF_MD(i
));
493 queue_broadcast(SYS_USB_LUN_LOCKED
, (i
<<16)+0);
497 void usb_storage_disconnect(void)
502 /* called by usb_core_transfer_complete() */
503 void usb_storage_transfer_complete(int ep
,int dir
,int status
,int length
)
506 struct command_block_wrapper
* cbw
= (void*)cbw_buffer
;
509 logf("transfer result for ep %d/%d %X %d", ep
,dir
,status
, length
);
511 case RECEIVING_BLOCKS
:
512 if(dir
==USB_DIR_IN
) {
513 logf("IN received in RECEIVING");
515 logf("scsi write %d %d", cur_cmd
.sector
, cur_cmd
.count
);
517 if((unsigned int)length
!=(SECTOR_SIZE
* cur_cmd
.count
)
518 && (unsigned int)length
!=WRITE_BUFFER_SIZE
) {
519 logf("unexpected length :%d",length
);
523 unsigned int next_sector
= cur_cmd
.sector
+
524 (WRITE_BUFFER_SIZE
/SECTOR_SIZE
);
525 unsigned int next_count
= cur_cmd
.count
-
526 MIN(cur_cmd
.count
,WRITE_BUFFER_SIZE
/SECTOR_SIZE
);
527 int next_select
= !cur_cmd
.data_select
;
530 /* Ask the host to send more, to the other buffer */
531 receive_block_data(cur_cmd
.data
[next_select
],
532 MIN(WRITE_BUFFER_SIZE
,next_count
*SECTOR_SIZE
));
535 /* Now write the data that just came in, while the host is
536 sending the next bit */
537 #ifdef USB_USE_RAMDISK
538 memcpy(ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
539 cur_cmd
.data
[cur_cmd
.data_select
],
540 MIN(WRITE_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
)*SECTOR_SIZE
);
542 int 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
]);
547 send_csw(UMS_STATUS_FAIL
);
548 cur_sense_data
.sense_key
=SENSE_MEDIUM_ERROR
;
549 cur_sense_data
.asc
=ASC_WRITE_ERROR
;
550 cur_sense_data
.ascq
=0;
555 send_csw(UMS_STATUS_GOOD
);
558 /* Switch buffers for the next one */
559 cur_cmd
.data_select
=!cur_cmd
.data_select
;
561 cur_cmd
.sector
= next_sector
;
562 cur_cmd
.count
= next_count
;
565 logf("Transfer failed %X",status
);
566 send_csw(UMS_STATUS_FAIL
);
567 /* TODO fill in cur_sense_data */
568 cur_sense_data
.sense_key
=0;
569 cur_sense_data
.information
=0;
570 cur_sense_data
.asc
=0;
571 cur_sense_data
.ascq
=0;
574 case WAITING_FOR_CSW_COMPLETION_OR_COMMAND
:
575 if(dir
==USB_DIR_IN
) {
576 /* This was the CSW */
577 state
= WAITING_FOR_COMMAND
;
580 /* This was the command */
581 state
= WAITING_FOR_CSW_COMPLETION
;
582 /* We now have the CBW, but we won't execute it yet to avoid
583 * issues with the still-pending CSW */
586 case WAITING_FOR_COMMAND
:
587 if(dir
==USB_DIR_IN
) {
588 logf("IN received in WAITING_FOR_COMMAND");
592 case WAITING_FOR_CSW_COMPLETION
:
593 if(dir
==USB_DIR_OUT
) {
594 logf("OUT received in WAITING_FOR_CSW_COMPLETION");
599 if(cur_cmd
.cur_cmd
== SCSI_WRITE_10
)
601 queue_broadcast(SYS_USB_WRITE_DATA
, (cur_cmd
.lun
<<16)+cur_cmd
.orig_count
);
603 else if(cur_cmd
.cur_cmd
== SCSI_READ_10
)
605 queue_broadcast(SYS_USB_READ_DATA
, (cur_cmd
.lun
<<16)+cur_cmd
.orig_count
);
610 if(dir
==USB_DIR_OUT
) {
611 logf("OUT received in SENDING");
614 //logf("data sent, now send csw");
615 send_csw(UMS_STATUS_GOOD
);
618 logf("Transfer failed %X",status
);
619 send_csw(UMS_STATUS_FAIL
);
620 /* TODO fill in cur_sense_data */
621 cur_sense_data
.sense_key
=0;
622 cur_sense_data
.information
=0;
623 cur_sense_data
.asc
=0;
624 cur_sense_data
.ascq
=0;
627 case SENDING_FAILED_RESULT
:
628 if(dir
==USB_DIR_OUT
) {
629 logf("OUT received in SENDING");
631 send_csw(UMS_STATUS_FAIL
);
634 if(dir
==USB_DIR_OUT
) {
635 logf("OUT received in SENDING");
638 if(cur_cmd
.count
==0) {
639 //logf("data sent, now send csw");
640 send_csw(UMS_STATUS_GOOD
);
643 send_and_read_next();
647 logf("Transfer failed %X",status
);
648 send_csw(UMS_STATUS_FAIL
);
649 /* TODO fill in cur_sense_data */
650 cur_sense_data
.sense_key
=0;
651 cur_sense_data
.information
=0;
652 cur_sense_data
.asc
=0;
653 cur_sense_data
.ascq
=0;
657 tm
.tm_year
=(tb
.transfer_buffer
[0]<<8)+tb
.transfer_buffer
[1] - 1900;
658 tm
.tm_yday
=(tb
.transfer_buffer
[2]<<8)+tb
.transfer_buffer
[3];
659 tm
.tm_hour
=tb
.transfer_buffer
[5];
660 tm
.tm_min
=tb
.transfer_buffer
[6];
661 tm
.tm_sec
=tb
.transfer_buffer
[7];
662 yearday_to_daymonth(tm
.tm_yday
,tm
.tm_year
+ 1900,&tm
.tm_mday
,&tm
.tm_mon
);
663 set_day_of_week(&tm
);
665 send_csw(UMS_STATUS_GOOD
);
670 /* called by usb_core_control_request() */
671 bool usb_storage_control_request(struct usb_ctrlrequest
* req
, unsigned char* dest
)
673 bool handled
= false;
676 switch (req
->bRequest
) {
677 case USB_BULK_GET_MAX_LUN
: {
678 *tb
.max_lun
= storage_num_drives() - 1;
679 #ifdef HIDE_FIRST_DRIVE
682 logf("ums: getmaxlun");
683 usb_drv_recv(EP_CONTROL
, NULL
, 0); /* ack */
684 usb_drv_send(EP_CONTROL
, tb
.max_lun
, 1);
689 case USB_BULK_RESET_REQUEST
:
690 logf("ums: bulk reset");
691 state
= WAITING_FOR_COMMAND
;
692 /* UMS BOT 3.1 says The device shall preserve the value of its bulk
693 data toggle bits and endpoint STALL conditions despite
694 the Bulk-Only Mass Storage Reset. */
696 usb_drv_reset_endpoint(ep_in
, false);
697 usb_drv_reset_endpoint(ep_out
, true);
699 usb_drv_send(EP_CONTROL
, NULL
, 0); /* ack */
707 static void send_and_read_next(void)
709 if(cur_cmd
.last_result
!=0) {
710 /* The last read failed. */
711 send_csw(UMS_STATUS_FAIL
);
712 cur_sense_data
.sense_key
=SENSE_MEDIUM_ERROR
;
713 cur_sense_data
.asc
=ASC_READ_ERROR
;
714 cur_sense_data
.ascq
=0;
717 send_block_data(cur_cmd
.data
[cur_cmd
.data_select
],
718 MIN(READ_BUFFER_SIZE
,cur_cmd
.count
*SECTOR_SIZE
));
720 /* Switch buffers for the next one */
721 cur_cmd
.data_select
=!cur_cmd
.data_select
;
723 cur_cmd
.sector
+=(READ_BUFFER_SIZE
/SECTOR_SIZE
);
724 cur_cmd
.count
-=MIN(cur_cmd
.count
,READ_BUFFER_SIZE
/SECTOR_SIZE
);
726 if(cur_cmd
.count
!=0) {
727 /* already read the next bit, so we can send it out immediately when the
728 * current transfer completes. */
729 #ifdef USB_USE_RAMDISK
730 memcpy(cur_cmd
.data
[cur_cmd
.data_select
],
731 ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
732 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
)*SECTOR_SIZE
);
734 cur_cmd
.last_result
= storage_read_sectors(IF_MD2(cur_cmd
.lun
,)
736 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
),
737 cur_cmd
.data
[cur_cmd
.data_select
]);
741 /****************************************************************************/
743 static void handle_scsi(struct command_block_wrapper
* cbw
)
745 /* USB Mass Storage assumes LBA capability.
746 TODO: support 48-bit LBA */
748 struct storage_info info
;
749 unsigned int length
= cbw
->data_transfer_length
;
750 unsigned int block_size
= 0;
751 unsigned int block_count
= 0;
752 bool lun_present
=true;
753 unsigned char lun
= cbw
->lun
;
754 unsigned int block_size_mult
= 1;
756 if(letoh32(cbw
->signature
) != CBW_SIGNATURE
) {
757 usb_drv_stall(ep_in
, true,true);
758 usb_drv_stall(ep_out
, true,false);
761 /* Clear the signature to prevent possible bugs elsewhere
762 * to trigger a second execution of the same command with
766 #ifdef HIDE_FIRST_DRIVE
770 storage_get_info(lun
,&info
);
771 #ifdef USB_USE_RAMDISK
772 block_size
= SECTOR_SIZE
;
773 block_count
= RAMDISK_SIZE
;
775 block_size
=info
.sector_size
;
776 block_count
=info
.num_sectors
;
780 if(storage_removable(lun
) && !storage_present(lun
)) {
788 #ifdef MAX_LOG_SECTOR_SIZE
789 block_size_mult
= disk_sector_multiplier
;
792 cur_cmd
.tag
= cbw
->tag
;
794 cur_cmd
.cur_cmd
= cbw
->command_block
[0];
796 switch (cbw
->command_block
[0]) {
797 case SCSI_TEST_UNIT_READY
:
798 logf("scsi test_unit_ready %d",lun
);
799 if(!usb_exclusive_storage()) {
800 send_csw(UMS_STATUS_FAIL
);
801 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
802 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
803 cur_sense_data
.ascq
=0;
807 send_csw(UMS_STATUS_GOOD
);
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 case SCSI_REPORT_LUNS
: {
818 logf("scsi report luns %d",lun
);
819 int allocation_length
=0;
821 unsigned int response_length
= 8+8*storage_num_drives();
822 allocation_length
|=(cbw
->command_block
[6]<<24);
823 allocation_length
|=(cbw
->command_block
[7]<<16);
824 allocation_length
|=(cbw
->command_block
[8]<<8);
825 allocation_length
|=(cbw
->command_block
[9]);
826 memset(tb
.lun_data
,0,sizeof(struct report_lun_data
));
827 tb
.lun_data
->lun_list_length
=htobe32(8*storage_num_drives());
828 for(i
=0;i
<storage_num_drives();i
++)
831 if(storage_removable(i
))
832 tb
.lun_data
->luns
[i
][1]=1;
835 tb
.lun_data
->luns
[i
][1]=0;
837 send_command_result(tb
.lun_data
,
838 MIN(response_length
, length
));
843 logf("scsi inquiry %d",lun
);
844 fill_inquiry(IF_MD(lun
));
845 length
= MIN(length
, cbw
->command_block
[4]);
846 send_command_result(tb
.inquiry
,
847 MIN(sizeof(struct inquiry_data
), length
));
850 case SCSI_REQUEST_SENSE
: {
851 tb
.sense_data
->ResponseCode
=0x70;/*current error*/
852 tb
.sense_data
->Obsolete
=0;
853 tb
.sense_data
->fei_sensekey
=cur_sense_data
.sense_key
&0x0f;
854 tb
.sense_data
->Information
=cur_sense_data
.information
;
855 tb
.sense_data
->AdditionalSenseLength
=10;
856 tb
.sense_data
->CommandSpecificInformation
=0;
857 tb
.sense_data
->AdditionalSenseCode
=cur_sense_data
.asc
;
858 tb
.sense_data
->AdditionalSenseCodeQualifier
=cur_sense_data
.ascq
;
859 tb
.sense_data
->FieldReplaceableUnitCode
=0;
860 tb
.sense_data
->SKSV
=0;
861 tb
.sense_data
->SenseKeySpecific
=0;
862 logf("scsi request_sense %d",lun
);
863 send_command_result(tb
.sense_data
,
864 MIN(sizeof(struct sense_data
), length
));
868 case SCSI_MODE_SENSE_10
: {
870 send_command_failed_result();
871 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
872 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
873 cur_sense_data
.ascq
=0;
876 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
877 unsigned char page_code
= cbw
->command_block
[2] & 0x3f;
878 logf("scsi mode_sense_10 %d %X",lun
,page_code
);
881 tb
.ms_data_10
->mode_data_length
=
882 htobe16(sizeof(struct mode_sense_data_10
)-2);
883 tb
.ms_data_10
->medium_type
= 0;
884 tb
.ms_data_10
->device_specific
= 0;
885 tb
.ms_data_10
->reserved
= 0;
886 tb
.ms_data_10
->longlba
= 1;
887 tb
.ms_data_10
->block_descriptor_length
=
888 htobe16(sizeof(struct mode_sense_bdesc_longlba
));
890 memset(tb
.ms_data_10
->block_descriptor
.reserved
,0,4);
891 memset(tb
.ms_data_10
->block_descriptor
.num_blocks
,0,8);
893 tb
.ms_data_10
->block_descriptor
.num_blocks
[4] =
894 ((block_count
/block_size_mult
) & 0xff000000)>>24;
895 tb
.ms_data_10
->block_descriptor
.num_blocks
[5] =
896 ((block_count
/block_size_mult
) & 0x00ff0000)>>16;
897 tb
.ms_data_10
->block_descriptor
.num_blocks
[6] =
898 ((block_count
/block_size_mult
) & 0x0000ff00)>>8;
899 tb
.ms_data_10
->block_descriptor
.num_blocks
[7] =
900 ((block_count
/block_size_mult
) & 0x000000ff);
902 tb
.ms_data_10
->block_descriptor
.block_size
[0] =
903 ((block_size
*block_size_mult
) & 0xff000000)>>24;
904 tb
.ms_data_10
->block_descriptor
.block_size
[1] =
905 ((block_size
*block_size_mult
) & 0x00ff0000)>>16;
906 tb
.ms_data_10
->block_descriptor
.block_size
[2] =
907 ((block_size
*block_size_mult
) & 0x0000ff00)>>8;
908 tb
.ms_data_10
->block_descriptor
.block_size
[3] =
909 ((block_size
*block_size_mult
) & 0x000000ff);
910 send_command_result(tb
.ms_data_10
,
911 MIN(sizeof(struct mode_sense_data_10
), length
));
914 send_command_failed_result();
915 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
916 cur_sense_data
.asc
=ASC_INVALID_FIELD_IN_CBD
;
917 cur_sense_data
.ascq
=0;
923 case SCSI_MODE_SENSE_6
: {
925 send_command_failed_result();
926 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
927 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
928 cur_sense_data
.ascq
=0;
931 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
932 unsigned char page_code
= cbw
->command_block
[2] & 0x3f;
933 logf("scsi mode_sense_6 %d %X",lun
,page_code
);
936 /* All supported pages. */
937 tb
.ms_data_6
->mode_data_length
=
938 sizeof(struct mode_sense_data_6
)-1;
939 tb
.ms_data_6
->medium_type
= 0;
940 tb
.ms_data_6
->device_specific
= 0;
941 tb
.ms_data_6
->block_descriptor_length
=
942 sizeof(struct mode_sense_bdesc_shortlba
);
943 tb
.ms_data_6
->block_descriptor
.density_code
= 0;
944 tb
.ms_data_6
->block_descriptor
.reserved
= 0;
945 if(block_count
/block_size_mult
> 0xffffff) {
946 tb
.ms_data_6
->block_descriptor
.num_blocks
[0] = 0xff;
947 tb
.ms_data_6
->block_descriptor
.num_blocks
[1] = 0xff;
948 tb
.ms_data_6
->block_descriptor
.num_blocks
[2] = 0xff;
951 tb
.ms_data_6
->block_descriptor
.num_blocks
[0] =
952 ((block_count
/block_size_mult
) & 0xff0000)>>16;
953 tb
.ms_data_6
->block_descriptor
.num_blocks
[1] =
954 ((block_count
/block_size_mult
) & 0x00ff00)>>8;
955 tb
.ms_data_6
->block_descriptor
.num_blocks
[2] =
956 ((block_count
/block_size_mult
) & 0x0000ff);
958 tb
.ms_data_6
->block_descriptor
.block_size
[0] =
959 ((block_size
*block_size_mult
) & 0xff0000)>>16;
960 tb
.ms_data_6
->block_descriptor
.block_size
[1] =
961 ((block_size
*block_size_mult
) & 0x00ff00)>>8;
962 tb
.ms_data_6
->block_descriptor
.block_size
[2] =
963 ((block_size
*block_size_mult
) & 0x0000ff);
964 send_command_result(tb
.ms_data_6
,
965 MIN(sizeof(struct mode_sense_data_6
), length
));
968 send_command_failed_result();
969 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
970 cur_sense_data
.asc
=ASC_INVALID_FIELD_IN_CBD
;
971 cur_sense_data
.ascq
=0;
977 case SCSI_START_STOP_UNIT
:
978 logf("scsi start_stop unit %d",lun
);
979 if((cbw
->command_block
[4] & 0xf0) == 0) /*load/eject bit is valid*/
980 { /* Process start and eject bits */
981 logf("scsi load/eject");
982 if((cbw
->command_block
[4] & 0x01) == 0) /* Don't start */
984 if((cbw
->command_block
[4] & 0x02) != 0) /* eject */
991 send_csw(UMS_STATUS_GOOD
);
994 case SCSI_ALLOW_MEDIUM_REMOVAL
:
995 logf("scsi allow_medium_removal %d",lun
);
996 if((cbw
->command_block
[4] & 0x03) == 0) {
998 queue_broadcast(SYS_USB_LUN_LOCKED
, (lun
<<16)+0);
1002 queue_broadcast(SYS_USB_LUN_LOCKED
, (lun
<<16)+1);
1004 send_csw(UMS_STATUS_GOOD
);
1007 case SCSI_READ_FORMAT_CAPACITY
: {
1008 logf("scsi read_format_capacity %d",lun
);
1010 tb
.format_capacity_data
->following_length
=htobe32(8);
1011 /* "block count" actually means "number of last block" */
1012 tb
.format_capacity_data
->block_count
=
1013 htobe32(block_count
/block_size_mult
- 1);
1014 tb
.format_capacity_data
->block_size
=
1015 htobe32(block_size
*block_size_mult
);
1016 tb
.format_capacity_data
->block_size
|=
1017 htobe32(SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA
);
1019 send_command_result(tb
.format_capacity_data
,
1020 MIN(sizeof(struct format_capacity
), length
));
1023 send_command_failed_result();
1024 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1025 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1026 cur_sense_data
.ascq
=0;
1031 case SCSI_READ_CAPACITY
: {
1032 logf("scsi read_capacity %d",lun
);
1035 /* "block count" actually means "number of last block" */
1036 tb
.capacity_data
->block_count
=
1037 htobe32(block_count
/block_size_mult
- 1);
1038 tb
.capacity_data
->block_size
=
1039 htobe32(block_size
*block_size_mult
);
1041 send_command_result(tb
.capacity_data
,
1042 MIN(sizeof(struct capacity
), length
));
1045 send_command_failed_result();
1046 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1047 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1048 cur_sense_data
.ascq
=0;
1054 logf("scsi read10 %d",lun
);
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;
1062 cur_cmd
.data
[0] = tb
.transfer_buffer
;
1063 cur_cmd
.data
[1] = &tb
.transfer_buffer
[READ_BUFFER_SIZE
];
1064 cur_cmd
.data_select
=0;
1065 cur_cmd
.sector
= block_size_mult
*
1066 (cbw
->command_block
[2] << 24 |
1067 cbw
->command_block
[3] << 16 |
1068 cbw
->command_block
[4] << 8 |
1069 cbw
->command_block
[5] );
1070 cur_cmd
.count
= block_size_mult
*
1071 (cbw
->command_block
[7] << 8 |
1072 cbw
->command_block
[8]);
1073 cur_cmd
.orig_count
= cur_cmd
.count
;
1075 //logf("scsi read %d %d", cur_cmd.sector, cur_cmd.count);
1077 if((cur_cmd
.sector
+ cur_cmd
.count
) > block_count
) {
1078 send_csw(UMS_STATUS_FAIL
);
1079 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1080 cur_sense_data
.asc
=ASC_LBA_OUT_OF_RANGE
;
1081 cur_sense_data
.ascq
=0;
1084 #ifdef USB_USE_RAMDISK
1085 memcpy(cur_cmd
.data
[cur_cmd
.data_select
],
1086 ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
1087 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
,cur_cmd
.count
)*SECTOR_SIZE
);
1089 cur_cmd
.last_result
= storage_read_sectors(IF_MD2(cur_cmd
.lun
,)
1091 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
),
1092 cur_cmd
.data
[cur_cmd
.data_select
]);
1094 #ifdef TOSHIBA_GIGABEAT_S
1095 if(cur_cmd
.sector
== 0) {
1096 fix_mbr(cur_cmd
.data
[cur_cmd
.data_select
]);
1100 send_and_read_next();
1105 logf("scsi write10 %d",lun
);
1107 send_csw(UMS_STATUS_FAIL
);
1108 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1109 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1110 cur_sense_data
.ascq
=0;
1113 cur_cmd
.data
[0] = tb
.transfer_buffer
;
1114 cur_cmd
.data
[1] = &tb
.transfer_buffer
[WRITE_BUFFER_SIZE
];
1115 cur_cmd
.data_select
=0;
1116 cur_cmd
.sector
= block_size_mult
*
1117 (cbw
->command_block
[2] << 24 |
1118 cbw
->command_block
[3] << 16 |
1119 cbw
->command_block
[4] << 8 |
1120 cbw
->command_block
[5] );
1121 cur_cmd
.count
= block_size_mult
*
1122 (cbw
->command_block
[7] << 8 |
1123 cbw
->command_block
[8]);
1124 cur_cmd
.orig_count
= cur_cmd
.count
;
1127 if((cur_cmd
.sector
+ cur_cmd
.count
) > block_count
) {
1128 send_csw(UMS_STATUS_FAIL
);
1129 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1130 cur_sense_data
.asc
=ASC_LBA_OUT_OF_RANGE
;
1131 cur_sense_data
.ascq
=0;
1134 receive_block_data(cur_cmd
.data
[0],
1135 MIN(WRITE_BUFFER_SIZE
, cur_cmd
.count
*SECTOR_SIZE
));
1139 case SCSI_WRITE_BUFFER
:
1140 if(cbw
->command_block
[1]==1 /* mode = vendor specific */
1141 && cbw
->command_block
[2]==0 /* buffer id = 0 */
1143 && cbw
->command_block
[3]==0x0c /* offset (3 bytes) */
1144 && cbw
->command_block
[4]==0
1145 && cbw
->command_block
[5]==0
1147 /* Some versions of itunes set the parameter list length to 0.
1148 * Technically it should be 0x0c, which is what libgpod sends */
1149 && cbw
->command_block
[6]==0 /* parameter list (3 bytes) */
1150 && cbw
->command_block
[7]==0
1151 && (cbw
->command_block
[8]==0 || cbw
->command_block
[8]==0x0c)
1153 && cbw
->command_block
[9]==0)
1158 logf("scsi unknown cmd %x",cbw
->command_block
[0x0]);
1159 send_csw(UMS_STATUS_FAIL
);
1160 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1161 cur_sense_data
.asc
=ASC_INVALID_COMMAND
;
1162 cur_sense_data
.ascq
=0;
1167 static void send_block_data(void *data
,int size
)
1169 usb_drv_send_nonblocking(ep_in
, data
,size
);
1170 state
= SENDING_BLOCKS
;
1173 static void send_command_result(void *data
,int size
)
1175 usb_drv_send_nonblocking(ep_in
, data
,size
);
1176 state
= SENDING_RESULT
;
1179 static void send_command_failed_result(void)
1181 usb_drv_send_nonblocking(ep_in
, NULL
, 0);
1182 state
= SENDING_FAILED_RESULT
;
1185 static void receive_time(void)
1187 usb_drv_recv(ep_out
, tb
.transfer_buffer
, 12);
1188 state
= RECEIVING_TIME
;
1190 static void receive_block_data(void *data
,int size
)
1192 usb_drv_recv(ep_out
, data
, size
);
1193 state
= RECEIVING_BLOCKS
;
1196 static void send_csw(int status
)
1198 tb
.csw
->signature
= htole32(CSW_SIGNATURE
);
1199 tb
.csw
->tag
= cur_cmd
.tag
;
1200 tb
.csw
->data_residue
= 0;
1201 tb
.csw
->status
= status
;
1203 usb_drv_send_nonblocking(ep_in
, tb
.csw
,
1204 sizeof(struct command_status_wrapper
));
1205 state
= WAITING_FOR_CSW_COMPLETION_OR_COMMAND
;
1206 //logf("CSW: %X",status);
1207 /* Already start waiting for the next command */
1208 usb_drv_recv(ep_out
, cbw_buffer
, MAX_CBW_SIZE
);
1209 /* The next completed transfer will be either the CSW one
1210 * or the new command */
1212 if(status
== UMS_STATUS_GOOD
) {
1213 cur_sense_data
.sense_key
=0;
1214 cur_sense_data
.information
=0;
1215 cur_sense_data
.asc
=0;
1216 cur_sense_data
.ascq
=0;
1220 static void copy_padded(char *dest
, char *src
, int len
)
1223 while(src
[i
]!=0 && i
<len
)
1235 /* build SCSI INQUIRY */
1236 static void fill_inquiry(IF_MD_NONVOID(int lun
))
1238 struct storage_info info
;
1239 memset(tb
.inquiry
, 0, sizeof(struct inquiry_data
));
1240 storage_get_info(lun
,&info
);
1241 copy_padded(tb
.inquiry
->VendorId
,info
.vendor
,sizeof(tb
.inquiry
->VendorId
));
1242 copy_padded(tb
.inquiry
->ProductId
,info
.product
,sizeof(tb
.inquiry
->ProductId
));
1243 copy_padded(tb
.inquiry
->ProductRevisionLevel
,info
.revision
,sizeof(tb
.inquiry
->ProductRevisionLevel
));
1245 tb
.inquiry
->DeviceType
= DIRECT_ACCESS_DEVICE
;
1246 tb
.inquiry
->AdditionalLength
= 0x1f;
1247 memset(tb
.inquiry
->Reserved
, 0, 3);
1248 tb
.inquiry
->Versions
= 4; /* SPC-2 */
1249 tb
.inquiry
->Format
= 2; /* SPC-2/3 inquiry format */
1251 #ifdef TOSHIBA_GIGABEAT_S
1252 tb
.inquiry
->DeviceTypeModifier
= 0;
1254 tb
.inquiry
->DeviceTypeModifier
= DEVICE_REMOVABLE
;