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 ****************************************************************************/
30 /* Needed to get at the audio buffer */
32 #include "usb_storage.h"
37 /* The SD card driver on Sansa c200 and e200 can cause write corruption,
38 * often triggered by simultaneous USB activity. This can be largely avoided
39 * by not overlapping storage_write_sector() with USB transfers. This does reduce
40 * write performance, so we only do it for the affected DAPs
42 #if (CONFIG_STORAGE & STORAGE_SD)
43 #define SERIALIZE_WRITES
45 /* Enable the following define to export only the SD card slot. This
46 * is useful for USBCV MSC tests, as those are destructive.
47 * This won't work right if the device doesn't have a card slot.
49 //#define ONLY_EXPOSE_CARD_SLOT
51 #ifdef USB_USE_RAMDISK
52 #define RAMDISK_SIZE 2048
55 #define SECTOR_SIZE 512
57 /* We can currently use up to 20k buffer size. More than that requires
58 * transfer chaining in the driver. Tests on sansa c200 show that the 16k
59 * limitation causes no more than 2% slowdown.
61 #define BUFFER_SIZE 16384
63 /* bulk-only class specific requests */
64 #define USB_BULK_RESET_REQUEST 0xff
65 #define USB_BULK_GET_MAX_LUN 0xfe
67 #define DIRECT_ACCESS_DEVICE 0x00 /* disks */
68 #define DEVICE_REMOVABLE 0x80
70 #define CBW_SIGNATURE 0x43425355
71 #define CSW_SIGNATURE 0x53425355
73 #define SCSI_TEST_UNIT_READY 0x00
74 #define SCSI_INQUIRY 0x12
75 #define SCSI_MODE_SENSE_6 0x1a
76 #define SCSI_MODE_SENSE_10 0x5a
77 #define SCSI_REQUEST_SENSE 0x03
78 #define SCSI_ALLOW_MEDIUM_REMOVAL 0x1e
79 #define SCSI_READ_CAPACITY 0x25
80 #define SCSI_READ_FORMAT_CAPACITY 0x23
81 #define SCSI_READ_10 0x28
82 #define SCSI_WRITE_10 0x2a
83 #define SCSI_START_STOP_UNIT 0x1b
84 #define SCSI_REPORT_LUNS 0xa0
86 #define UMS_STATUS_GOOD 0x00
87 #define UMS_STATUS_FAIL 0x01
89 #define SENSE_NOT_READY 0x02
90 #define SENSE_MEDIUM_ERROR 0x03
91 #define SENSE_ILLEGAL_REQUEST 0x05
92 #define SENSE_UNIT_ATTENTION 0x06
94 #define ASC_MEDIUM_NOT_PRESENT 0x3a
95 #define ASC_INVALID_FIELD_IN_CBD 0x24
96 #define ASC_LBA_OUT_OF_RANGE 0x21
97 #define ASC_WRITE_ERROR 0x0C
98 #define ASC_READ_ERROR 0x11
99 #define ASC_NOT_READY 0x04
100 #define ASC_INVALID_COMMAND 0x20
102 #define ASCQ_BECOMING_READY 0x01
104 #define SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA 0x02000000
106 /* storage interface */
108 #define USB_SC_SCSI 0x06 /* Transparent */
109 #define USB_PROT_BULK 0x50 /* bulk only */
111 static struct usb_interface_descriptor
__attribute__((aligned(2)))
112 interface_descriptor
=
114 .bLength
= sizeof(struct usb_interface_descriptor
),
115 .bDescriptorType
= USB_DT_INTERFACE
,
116 .bInterfaceNumber
= 0,
117 .bAlternateSetting
= 0,
119 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
120 .bInterfaceSubClass
= USB_SC_SCSI
,
121 .bInterfaceProtocol
= USB_PROT_BULK
,
125 static struct usb_endpoint_descriptor
__attribute__((aligned(2)))
126 endpoint_descriptor
=
128 .bLength
= sizeof(struct usb_endpoint_descriptor
),
129 .bDescriptorType
= USB_DT_ENDPOINT
,
130 .bEndpointAddress
= 0,
131 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
136 struct inquiry_data
{
137 unsigned char DeviceType
;
138 unsigned char DeviceTypeModifier
;
139 unsigned char Versions
;
140 unsigned char Format
;
141 unsigned char AdditionalLength
;
142 unsigned char Reserved
[2];
143 unsigned char Capability
;
144 unsigned char VendorId
[8];
145 unsigned char ProductId
[16];
146 unsigned char ProductRevisionLevel
[4];
147 } __attribute__ ((packed
));
149 struct report_lun_data
{
150 unsigned int lun_list_length
;
151 unsigned int reserved1
;
152 // TODO this should be cleaned up with the VOLUMES vs DRIVES mess
153 unsigned char luns
[NUM_VOLUMES
][8];
154 } __attribute__ ((packed
));
157 unsigned char ResponseCode
;
158 unsigned char Obsolete
;
159 unsigned char fei_sensekey
;
160 unsigned int Information
;
161 unsigned char AdditionalSenseLength
;
162 unsigned int CommandSpecificInformation
;
163 unsigned char AdditionalSenseCode
;
164 unsigned char AdditionalSenseCodeQualifier
;
165 unsigned char FieldReplaceableUnitCode
;
167 unsigned short SenseKeySpecific
;
168 } __attribute__ ((packed
));
170 struct mode_sense_bdesc_longlba
{
171 unsigned char num_blocks
[8];
172 unsigned char reserved
[4];
173 unsigned char block_size
[4];
174 } __attribute__ ((packed
));
176 struct mode_sense_bdesc_shortlba
{
177 unsigned char density_code
;
178 unsigned char num_blocks
[3];
179 unsigned char reserved
;
180 unsigned char block_size
[3];
181 } __attribute__ ((packed
));
183 struct mode_sense_data_10
{
184 unsigned short mode_data_length
;
185 unsigned char medium_type
;
186 unsigned char device_specific
;
187 unsigned char longlba
;
188 unsigned char reserved
;
189 unsigned short block_descriptor_length
;
190 struct mode_sense_bdesc_longlba block_descriptor
;
191 } __attribute__ ((packed
));
193 struct mode_sense_data_6
{
194 unsigned char mode_data_length
;
195 unsigned char medium_type
;
196 unsigned char device_specific
;
197 unsigned char block_descriptor_length
;
198 struct mode_sense_bdesc_shortlba block_descriptor
;
199 } __attribute__ ((packed
));
201 struct command_block_wrapper
{
202 unsigned int signature
;
204 unsigned int data_transfer_length
;
207 unsigned char command_length
;
208 unsigned char command_block
[16];
209 } __attribute__ ((packed
));
211 struct command_status_wrapper
{
212 unsigned int signature
;
214 unsigned int data_residue
;
215 unsigned char status
;
216 } __attribute__ ((packed
));
219 unsigned int block_count
;
220 unsigned int block_size
;
221 } __attribute__ ((packed
));
223 struct format_capacity
{
224 unsigned int following_length
;
225 unsigned int block_count
;
226 unsigned int block_size
;
227 } __attribute__ ((packed
));
231 unsigned char* transfer_buffer
;
232 struct inquiry_data
* inquiry
;
233 struct capacity
* capacity_data
;
234 struct format_capacity
* format_capacity_data
;
235 struct sense_data
*sense_data
;
236 struct mode_sense_data_6
*ms_data_6
;
237 struct mode_sense_data_10
*ms_data_10
;
238 struct report_lun_data
*lun_data
;
239 struct command_status_wrapper
* csw
;
248 unsigned char *data
[2];
249 unsigned char data_select
;
250 unsigned int last_result
;
254 unsigned char sense_key
;
255 unsigned char information
;
260 static void handle_scsi(struct command_block_wrapper
* cbw
);
261 static void send_csw(int status
);
262 static void send_command_result(void *data
,int size
);
263 static void send_command_failed_result(void);
264 static void send_block_data(void *data
,int size
);
265 static void receive_block_data(void *data
,int size
);
266 static void fill_inquiry(IF_MV_NONVOID(int lun
));
267 static void send_and_read_next(void);
268 static bool ejected
[NUM_VOLUMES
];
270 static int usb_interface
;
271 static int ep_in
, ep_out
;
273 #ifdef USB_USE_RAMDISK
274 static unsigned char* ramdisk_buffer
;
281 SENDING_FAILED_RESULT
,
284 } state
= WAITING_FOR_COMMAND
;
286 static bool check_disk_present(IF_MV_NONVOID(int volume
))
288 #ifdef USB_USE_RAMDISK
291 unsigned char sector
[512];
292 return storage_read_sectors(volume
,0,1,sector
) == 0;
297 static void try_release_ata(void)
299 /* Check if there is a connected drive left. If not,
300 release excusive access */
301 bool canrelease
=true;
303 for(i
=0;i
<NUM_VOLUMES
;i
++) {
304 if(ejected
[i
]==false){
310 logf("scsi release ata");
311 usb_release_exclusive_ata();
317 void usb_storage_notify_hotswap(int volume
,bool inserted
)
319 logf("notify %d",inserted
);
320 if(inserted
&& check_disk_present(IF_MV(volume
))) {
321 ejected
[volume
] = false;
324 ejected
[volume
] = true;
329 void usb_storage_reconnect(void)
332 if(usb_core_driver_enabled(USB_DRIVER_MASS_STORAGE
)
334 for(i
=0;i
<NUM_VOLUMES
;i
++)
335 ejected
[i
] = !check_disk_present(IF_MV(i
));
336 logf("%s", __func__
);
340 /* called by usb_code_init() */
341 void usb_storage_init(void)
344 for(i
=0;i
<NUM_VOLUMES
;i
++) {
345 ejected
[i
] = !check_disk_present(IF_MV(i
));
347 logf("usb_storage_init done");
350 int usb_storage_request_endpoints(struct usb_class_driver
*drv
)
352 ep_in
= usb_core_request_endpoint(USB_DIR_IN
, drv
);
357 ep_out
= usb_core_request_endpoint(USB_DIR_OUT
, drv
);
360 usb_core_release_endpoint(ep_in
);
367 int usb_storage_set_first_interface(int interface
)
369 usb_interface
= interface
;
370 return interface
+ 1;
373 int usb_storage_get_config_descriptor(unsigned char *dest
,int max_packet_size
)
375 endpoint_descriptor
.wMaxPacketSize
=max_packet_size
;
376 interface_descriptor
.bInterfaceNumber
=usb_interface
;
378 memcpy(dest
,&interface_descriptor
,
379 sizeof(struct usb_interface_descriptor
));
380 dest
+=sizeof(struct usb_interface_descriptor
);
382 endpoint_descriptor
.bEndpointAddress
= ep_in
;
383 memcpy(dest
,&endpoint_descriptor
,
384 sizeof(struct usb_endpoint_descriptor
));
385 dest
+=sizeof(struct usb_endpoint_descriptor
);
387 endpoint_descriptor
.bEndpointAddress
= ep_out
;
388 memcpy(dest
,&endpoint_descriptor
,
389 sizeof(struct usb_endpoint_descriptor
));
391 return sizeof(struct usb_interface_descriptor
) +
392 2*sizeof(struct usb_endpoint_descriptor
);
395 void usb_storage_init_connection(void)
397 logf("ums: set config");
398 /* prime rx endpoint. We only need room for commands */
399 state
= WAITING_FOR_COMMAND
;
401 #if CONFIG_CPU == IMX31L || CONFIG_USBOTG == USBOTG_ISP1583 || \
402 defined(CPU_TCC77X) || defined(CPU_TCC780X) || defined(BOOTLOADER)
403 static unsigned char _transfer_buffer
[BUFFER_SIZE
*2]
404 USB_DEVBSS_ATTR
__attribute__((aligned(32)));
405 tb
.transfer_buffer
= (void *)_transfer_buffer
;
406 #ifdef USB_USE_RAMDISK
407 static unsigned char _ramdisk_buffer
[RAMDISK_SIZE
*SECTOR_SIZE
];
408 ramdisk_buffer
= _ramdisk_buffer
;
411 /* TODO : check if bufsize is at least 32K ? */
413 unsigned char * audio_buffer
;
415 audio_buffer
= audio_get_buffer(false,&bufsize
);
417 (void *)UNCACHED_ADDR((unsigned int)(audio_buffer
+ 31) & 0xffffffe0);
419 #ifdef USB_USE_RAMDISK
420 ramdisk_buffer
= tb
.transfer_buffer
+ BUFFER_SIZE
*2;
423 usb_drv_recv(ep_out
, tb
.transfer_buffer
, 1024);
426 void usb_storage_disconnect(void)
431 /* called by usb_core_transfer_complete() */
432 void usb_storage_transfer_complete(int ep
,int dir
,int status
,int length
)
435 struct command_block_wrapper
* cbw
= (void*)tb
.transfer_buffer
;
437 //logf("transfer result %X %d", status, length);
439 case RECEIVING_BLOCKS
:
440 if(dir
==USB_DIR_IN
) {
441 logf("IN received in RECEIVING");
443 logf("scsi write %d %d", cur_cmd
.sector
, cur_cmd
.count
);
445 if((unsigned int)length
!=(SECTOR_SIZE
*cur_cmd
.count
)
446 && (unsigned int)length
!=BUFFER_SIZE
) {
447 logf("unexpected length :%d",length
);
450 unsigned int next_sector
= cur_cmd
.sector
+
451 (BUFFER_SIZE
/SECTOR_SIZE
);
452 unsigned int next_count
= cur_cmd
.count
-
453 MIN(cur_cmd
.count
,BUFFER_SIZE
/SECTOR_SIZE
);
454 int next_select
= !cur_cmd
.data_select
;
456 #ifndef SERIALIZE_WRITES
458 /* Ask the host to send more, to the other buffer */
459 receive_block_data(cur_cmd
.data
[next_select
],
460 MIN(BUFFER_SIZE
,next_count
*SECTOR_SIZE
));
464 /* Now write the data that just came in, while the host is
465 sending the next bit */
466 #ifdef USB_USE_RAMDISK
467 memcpy(ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
468 cur_cmd
.data
[cur_cmd
.data_select
],
469 MIN(BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
)*SECTOR_SIZE
);
471 int result
= storage_write_sectors(cur_cmd
.lun
,
473 MIN(BUFFER_SIZE
/SECTOR_SIZE
,
475 cur_cmd
.data
[cur_cmd
.data_select
]);
477 send_csw(UMS_STATUS_FAIL
);
478 cur_sense_data
.sense_key
=SENSE_MEDIUM_ERROR
;
479 cur_sense_data
.asc
=ASC_WRITE_ERROR
;
480 cur_sense_data
.ascq
=0;
484 #ifdef SERIALIZE_WRITES
486 /* Ask the host to send more, to the other buffer */
487 receive_block_data(cur_cmd
.data
[next_select
],
488 MIN(BUFFER_SIZE
,next_count
*SECTOR_SIZE
));
493 send_csw(UMS_STATUS_GOOD
);
496 /* Switch buffers for the next one */
497 cur_cmd
.data_select
=!cur_cmd
.data_select
;
499 cur_cmd
.sector
= next_sector
;
500 cur_cmd
.count
= next_count
;
504 logf("Transfer failed %X",status
);
505 send_csw(UMS_STATUS_FAIL
);
506 /* TODO fill in cur_sense_data */
507 cur_sense_data
.sense_key
=0;
508 cur_sense_data
.information
=0;
509 cur_sense_data
.asc
=0;
510 cur_sense_data
.ascq
=0;
513 case WAITING_FOR_COMMAND
:
514 if(dir
==USB_DIR_IN
) {
515 logf("IN received in WAITING_FOR_COMMAND");
517 //logf("command received");
518 if(letoh32(cbw
->signature
) == CBW_SIGNATURE
){
522 usb_drv_stall(ep_in
, true,true);
523 usb_drv_stall(ep_out
, true,false);
527 if(dir
==USB_DIR_OUT
) {
528 logf("OUT received in SENDING_CSW");
530 //logf("csw sent, now go back to idle");
531 state
= WAITING_FOR_COMMAND
;
532 usb_drv_recv(ep_out
, tb
.transfer_buffer
, 1024);
535 if(dir
==USB_DIR_OUT
) {
536 logf("OUT received in SENDING");
539 //logf("data sent, now send csw");
540 send_csw(UMS_STATUS_GOOD
);
543 logf("Transfer failed %X",status
);
544 send_csw(UMS_STATUS_FAIL
);
545 /* TODO fill in cur_sense_data */
546 cur_sense_data
.sense_key
=0;
547 cur_sense_data
.information
=0;
548 cur_sense_data
.asc
=0;
549 cur_sense_data
.ascq
=0;
552 case SENDING_FAILED_RESULT
:
553 if(dir
==USB_DIR_OUT
) {
554 logf("OUT received in SENDING");
556 send_csw(UMS_STATUS_FAIL
);
559 if(dir
==USB_DIR_OUT
) {
560 logf("OUT received in SENDING");
563 if(cur_cmd
.count
==0) {
564 //logf("data sent, now send csw");
565 send_csw(UMS_STATUS_GOOD
);
568 send_and_read_next();
572 logf("Transfer failed %X",status
);
573 send_csw(UMS_STATUS_FAIL
);
574 /* TODO fill in cur_sense_data */
575 cur_sense_data
.sense_key
=0;
576 cur_sense_data
.information
=0;
577 cur_sense_data
.asc
=0;
578 cur_sense_data
.ascq
=0;
584 /* called by usb_core_control_request() */
585 bool usb_storage_control_request(struct usb_ctrlrequest
* req
)
587 bool handled
= false;
590 switch (req
->bRequest
) {
591 case USB_BULK_GET_MAX_LUN
: {
592 #ifdef ONLY_EXPOSE_CARD_SLOT
595 *tb
.max_lun
= NUM_VOLUMES
- 1;
597 logf("ums: getmaxlun");
598 usb_drv_send(EP_CONTROL
, tb
.max_lun
, 1);
599 usb_drv_recv(EP_CONTROL
, NULL
, 0); /* ack */
604 case USB_BULK_RESET_REQUEST
:
605 logf("ums: bulk reset");
606 state
= WAITING_FOR_COMMAND
;
607 /* UMS BOT 3.1 says The device shall preserve the value of its bulk
608 data toggle bits and endpoint STALL conditions despite
609 the Bulk-Only Mass Storage Reset. */
611 usb_drv_reset_endpoint(ep_in
, false);
612 usb_drv_reset_endpoint(ep_out
, true);
615 usb_drv_send(EP_CONTROL
, NULL
, 0); /* ack */
623 static void send_and_read_next(void)
625 if(cur_cmd
.last_result
!=0) {
626 /* The last read failed. */
627 send_csw(UMS_STATUS_FAIL
);
628 cur_sense_data
.sense_key
=SENSE_MEDIUM_ERROR
;
629 cur_sense_data
.asc
=ASC_READ_ERROR
;
630 cur_sense_data
.ascq
=0;
633 send_block_data(cur_cmd
.data
[cur_cmd
.data_select
],
634 MIN(BUFFER_SIZE
,cur_cmd
.count
*SECTOR_SIZE
));
636 /* Switch buffers for the next one */
637 cur_cmd
.data_select
=!cur_cmd
.data_select
;
639 cur_cmd
.sector
+=(BUFFER_SIZE
/SECTOR_SIZE
);
640 cur_cmd
.count
-=MIN(cur_cmd
.count
,BUFFER_SIZE
/SECTOR_SIZE
);
642 if(cur_cmd
.count
!=0){
643 /* already read the next bit, so we can send it out immediately when the
644 * current transfer completes. */
645 #ifdef USB_USE_RAMDISK
646 memcpy(cur_cmd
.data
[cur_cmd
.data_select
],
647 ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
648 MIN(BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
)*SECTOR_SIZE
);
650 cur_cmd
.last_result
= storage_read_sectors(cur_cmd
.lun
,
652 MIN(BUFFER_SIZE
/SECTOR_SIZE
,
654 cur_cmd
.data
[cur_cmd
.data_select
]);
658 /****************************************************************************/
660 static void handle_scsi(struct command_block_wrapper
* cbw
)
662 /* USB Mass Storage assumes LBA capability.
663 TODO: support 48-bit LBA */
665 struct storage_info info
;
666 unsigned int length
= cbw
->data_transfer_length
;
667 unsigned int block_size
= 0;
668 unsigned int block_count
= 0;
669 bool lun_present
=true;
670 #ifdef ONLY_EXPOSE_CARD_SLOT
671 unsigned char lun
= cbw
->lun
+1;
673 unsigned char lun
= cbw
->lun
;
675 unsigned int block_size_mult
= 1;
676 storage_get_info(lun
,&info
);
677 #ifdef USB_USE_RAMDISK
678 block_size
= SECTOR_SIZE
;
679 block_count
= RAMDISK_SIZE
;
681 block_size
=info
.sector_size
;
682 block_count
=info
.num_sectors
;
686 if(storage_removable(lun
) && !storage_present(lun
)) {
694 #ifdef MAX_LOG_SECTOR_SIZE
695 block_size_mult
= disk_sector_multiplier
;
698 cur_cmd
.tag
= cbw
->tag
;
701 switch (cbw
->command_block
[0]) {
702 case SCSI_TEST_UNIT_READY
:
703 logf("scsi test_unit_ready %d",lun
);
704 if(!usb_exclusive_storage()) {
705 send_csw(UMS_STATUS_FAIL
);
706 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
707 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
708 cur_sense_data
.ascq
=0;
712 send_csw(UMS_STATUS_GOOD
);
715 send_csw(UMS_STATUS_FAIL
);
716 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
717 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
718 cur_sense_data
.ascq
=0;
722 case SCSI_REPORT_LUNS
: {
723 logf("scsi report luns %d",lun
);
724 int allocation_length
=0;
726 allocation_length
|=(cbw
->command_block
[6]<<24);
727 allocation_length
|=(cbw
->command_block
[7]<<16);
728 allocation_length
|=(cbw
->command_block
[8]<<8);
729 allocation_length
|=(cbw
->command_block
[9]);
730 memset(tb
.lun_data
,0,sizeof(struct report_lun_data
));
731 tb
.lun_data
->lun_list_length
=htobe32(8*NUM_VOLUMES
);
732 for(i
=0;i
<NUM_VOLUMES
;i
++)
735 if(storage_removable(i
))
736 tb
.lun_data
->luns
[i
][1]=1;
739 tb
.lun_data
->luns
[i
][1]=0;
741 send_command_result(tb
.lun_data
,
742 MIN(sizeof(struct report_lun_data
), length
));
747 logf("scsi inquiry %d",lun
);
748 fill_inquiry(IF_MV(lun
));
749 length
= MIN(length
, cbw
->command_block
[4]);
750 send_command_result(tb
.inquiry
,
751 MIN(sizeof(struct inquiry_data
), length
));
754 case SCSI_REQUEST_SENSE
: {
755 tb
.sense_data
->ResponseCode
=0x70;/*current error*/
756 tb
.sense_data
->Obsolete
=0;
757 tb
.sense_data
->fei_sensekey
=cur_sense_data
.sense_key
&0x0f;
758 tb
.sense_data
->Information
=cur_sense_data
.information
;
759 tb
.sense_data
->AdditionalSenseLength
=10;
760 tb
.sense_data
->CommandSpecificInformation
=0;
761 tb
.sense_data
->AdditionalSenseCode
=cur_sense_data
.asc
;
762 tb
.sense_data
->AdditionalSenseCodeQualifier
=cur_sense_data
.ascq
;
763 tb
.sense_data
->FieldReplaceableUnitCode
=0;
764 tb
.sense_data
->SKSV
=0;
765 tb
.sense_data
->SenseKeySpecific
=0;
766 logf("scsi request_sense %d",lun
);
767 send_command_result(tb
.sense_data
, sizeof(struct sense_data
));
771 case SCSI_MODE_SENSE_10
: {
773 send_command_failed_result();
774 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
775 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
776 cur_sense_data
.ascq
=0;
779 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
780 unsigned char page_code
= cbw
->command_block
[2] & 0x3f;
781 logf("scsi mode_sense_10 %d %X",lun
,page_code
);
784 tb
.ms_data_10
->mode_data_length
=
785 htobe16(sizeof(struct mode_sense_data_10
)-2);
786 tb
.ms_data_10
->medium_type
= 0;
787 tb
.ms_data_10
->device_specific
= 0;
788 tb
.ms_data_10
->reserved
= 0;
789 tb
.ms_data_10
->longlba
= 1;
790 tb
.ms_data_10
->block_descriptor_length
=
791 htobe16(sizeof(struct mode_sense_bdesc_longlba
));
793 memset(tb
.ms_data_10
->block_descriptor
.reserved
,0,4);
794 memset(tb
.ms_data_10
->block_descriptor
.num_blocks
,0,8);
796 tb
.ms_data_10
->block_descriptor
.num_blocks
[4] =
797 ((block_count
/block_size_mult
) & 0xff000000)>>24;
798 tb
.ms_data_10
->block_descriptor
.num_blocks
[5] =
799 ((block_count
/block_size_mult
) & 0x00ff0000)>>16;
800 tb
.ms_data_10
->block_descriptor
.num_blocks
[6] =
801 ((block_count
/block_size_mult
) & 0x0000ff00)>>8;
802 tb
.ms_data_10
->block_descriptor
.num_blocks
[7] =
803 ((block_count
/block_size_mult
) & 0x000000ff);
805 tb
.ms_data_10
->block_descriptor
.block_size
[0] =
806 ((block_size
*block_size_mult
) & 0xff000000)>>24;
807 tb
.ms_data_10
->block_descriptor
.block_size
[1] =
808 ((block_size
*block_size_mult
) & 0x00ff0000)>>16;
809 tb
.ms_data_10
->block_descriptor
.block_size
[2] =
810 ((block_size
*block_size_mult
) & 0x0000ff00)>>8;
811 tb
.ms_data_10
->block_descriptor
.block_size
[3] =
812 ((block_size
*block_size_mult
) & 0x000000ff);
813 send_command_result(tb
.ms_data_10
,
814 MIN(sizeof(struct mode_sense_data_10
), length
));
817 send_command_failed_result();
818 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
819 cur_sense_data
.asc
=ASC_INVALID_FIELD_IN_CBD
;
820 cur_sense_data
.ascq
=0;
825 case SCSI_MODE_SENSE_6
: {
827 send_command_failed_result();
828 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
829 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
830 cur_sense_data
.ascq
=0;
833 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
834 unsigned char page_code
= cbw
->command_block
[2] & 0x3f;
835 logf("scsi mode_sense_6 %d %X",lun
,page_code
);
838 /* All supported pages. */
839 tb
.ms_data_6
->mode_data_length
=
840 sizeof(struct mode_sense_data_6
)-1;
841 tb
.ms_data_6
->medium_type
= 0;
842 tb
.ms_data_6
->device_specific
= 0;
843 tb
.ms_data_6
->block_descriptor_length
=
844 sizeof(struct mode_sense_bdesc_shortlba
);
845 tb
.ms_data_6
->block_descriptor
.density_code
= 0;
846 tb
.ms_data_6
->block_descriptor
.reserved
= 0;
847 if(block_count
/block_size_mult
> 0xffffff){
848 tb
.ms_data_6
->block_descriptor
.num_blocks
[0] = 0xff;
849 tb
.ms_data_6
->block_descriptor
.num_blocks
[1] = 0xff;
850 tb
.ms_data_6
->block_descriptor
.num_blocks
[2] = 0xff;
853 tb
.ms_data_6
->block_descriptor
.num_blocks
[0] =
854 ((block_count
/block_size_mult
) & 0xff0000)>>16;
855 tb
.ms_data_6
->block_descriptor
.num_blocks
[1] =
856 ((block_count
/block_size_mult
) & 0x00ff00)>>8;
857 tb
.ms_data_6
->block_descriptor
.num_blocks
[2] =
858 ((block_count
/block_size_mult
) & 0x0000ff);
860 tb
.ms_data_6
->block_descriptor
.block_size
[0] =
861 ((block_size
*block_size_mult
) & 0xff0000)>>16;
862 tb
.ms_data_6
->block_descriptor
.block_size
[1] =
863 ((block_size
*block_size_mult
) & 0x00ff00)>>8;
864 tb
.ms_data_6
->block_descriptor
.block_size
[2] =
865 ((block_size
*block_size_mult
) & 0x0000ff);
866 send_command_result(tb
.ms_data_6
,
867 MIN(sizeof(struct mode_sense_data_6
), length
));
870 send_command_failed_result();
871 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
872 cur_sense_data
.asc
=ASC_INVALID_FIELD_IN_CBD
;
873 cur_sense_data
.ascq
=0;
879 case SCSI_START_STOP_UNIT
:
880 logf("scsi start_stop unit %d",lun
);
881 if((cbw
->command_block
[4] & 0xf0) == 0) /*load/eject bit is valid*/
882 { /* Process start and eject bits */
883 logf("scsi load/eject");
884 if((cbw
->command_block
[4] & 0x01) == 0) /* Don't start */
886 if((cbw
->command_block
[4] & 0x02) != 0) /* eject */
893 send_csw(UMS_STATUS_GOOD
);
896 case SCSI_ALLOW_MEDIUM_REMOVAL
:
897 logf("scsi allow_medium_removal %d",lun
);
898 /* TODO: use this to show the connect screen ? */
899 send_csw(UMS_STATUS_GOOD
);
901 case SCSI_READ_FORMAT_CAPACITY
: {
902 logf("scsi read_format_capacity %d",lun
);
904 tb
.format_capacity_data
->following_length
=htobe32(8);
905 /* "block count" actually means "number of last block" */
906 tb
.format_capacity_data
->block_count
=
907 htobe32(block_count
/block_size_mult
- 1);
908 tb
.format_capacity_data
->block_size
=
909 htobe32(block_size
*block_size_mult
);
910 tb
.format_capacity_data
->block_size
|=
911 htobe32(SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA
);
913 send_command_result(tb
.format_capacity_data
,
914 MIN(sizeof(struct format_capacity
), length
));
918 send_command_failed_result();
919 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
920 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
921 cur_sense_data
.ascq
=0;
925 case SCSI_READ_CAPACITY
: {
926 logf("scsi read_capacity %d",lun
);
929 /* "block count" actually means "number of last block" */
930 tb
.capacity_data
->block_count
=
931 htobe32(block_count
/block_size_mult
- 1);
932 tb
.capacity_data
->block_size
=
933 htobe32(block_size
*block_size_mult
);
935 send_command_result(tb
.capacity_data
,
936 MIN(sizeof(struct capacity
), length
));
940 send_command_failed_result();
941 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
942 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
943 cur_sense_data
.ascq
=0;
949 logf("scsi read10 %d",lun
);
951 send_command_failed_result();
952 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
953 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
954 cur_sense_data
.ascq
=0;
957 cur_cmd
.data
[0] = tb
.transfer_buffer
;
958 cur_cmd
.data
[1] = &tb
.transfer_buffer
[BUFFER_SIZE
];
959 cur_cmd
.data_select
=0;
960 cur_cmd
.sector
= block_size_mult
*
961 (cbw
->command_block
[2] << 24 |
962 cbw
->command_block
[3] << 16 |
963 cbw
->command_block
[4] << 8 |
964 cbw
->command_block
[5] );
965 cur_cmd
.count
= block_size_mult
*
966 (cbw
->command_block
[7] << 8 |
967 cbw
->command_block
[8]);
969 //logf("scsi read %d %d", cur_cmd.sector, cur_cmd.count);
971 if((cur_cmd
.sector
+ cur_cmd
.count
) > block_count
) {
972 send_csw(UMS_STATUS_FAIL
);
973 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
974 cur_sense_data
.asc
=ASC_LBA_OUT_OF_RANGE
;
975 cur_sense_data
.ascq
=0;
978 #ifdef USB_USE_RAMDISK
979 memcpy(cur_cmd
.data
[cur_cmd
.data_select
],
980 ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
981 MIN(BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
)*SECTOR_SIZE
);
983 cur_cmd
.last_result
= storage_read_sectors(cur_cmd
.lun
,
985 MIN(BUFFER_SIZE
/SECTOR_SIZE
,
987 cur_cmd
.data
[cur_cmd
.data_select
]);
989 send_and_read_next();
994 logf("scsi write10 %d",lun
);
996 send_csw(UMS_STATUS_FAIL
);
997 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
998 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
999 cur_sense_data
.ascq
=0;
1002 cur_cmd
.data
[0] = tb
.transfer_buffer
;
1003 cur_cmd
.data
[1] = &tb
.transfer_buffer
[BUFFER_SIZE
];
1004 cur_cmd
.data_select
=0;
1005 cur_cmd
.sector
= block_size_mult
*
1006 (cbw
->command_block
[2] << 24 |
1007 cbw
->command_block
[3] << 16 |
1008 cbw
->command_block
[4] << 8 |
1009 cbw
->command_block
[5] );
1010 cur_cmd
.count
= block_size_mult
*
1011 (cbw
->command_block
[7] << 8 |
1012 cbw
->command_block
[8]);
1014 if((cur_cmd
.sector
+ cur_cmd
.count
) > block_count
) {
1015 send_csw(UMS_STATUS_FAIL
);
1016 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1017 cur_sense_data
.asc
=ASC_LBA_OUT_OF_RANGE
;
1018 cur_sense_data
.ascq
=0;
1021 receive_block_data(cur_cmd
.data
[0],
1023 cur_cmd
.count
*SECTOR_SIZE
));
1029 logf("scsi unknown cmd %x",cbw
->command_block
[0x0]);
1030 send_csw(UMS_STATUS_FAIL
);
1031 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1032 cur_sense_data
.asc
=ASC_INVALID_COMMAND
;
1033 cur_sense_data
.ascq
=0;
1038 static void send_block_data(void *data
,int size
)
1040 usb_drv_send_nonblocking(ep_in
, data
,size
);
1041 state
= SENDING_BLOCKS
;
1044 static void send_command_result(void *data
,int size
)
1046 usb_drv_send_nonblocking(ep_in
, data
,size
);
1047 state
= SENDING_RESULT
;
1050 static void send_command_failed_result(void)
1052 usb_drv_send_nonblocking(ep_in
, NULL
, 0);
1053 state
= SENDING_FAILED_RESULT
;
1056 static void receive_block_data(void *data
,int size
)
1058 usb_drv_recv(ep_out
, data
, size
);
1059 state
= RECEIVING_BLOCKS
;
1062 static void send_csw(int status
)
1064 tb
.csw
->signature
= htole32(CSW_SIGNATURE
);
1065 tb
.csw
->tag
= cur_cmd
.tag
;
1066 tb
.csw
->data_residue
= 0;
1067 tb
.csw
->status
= status
;
1069 usb_drv_send_nonblocking(ep_in
, tb
.csw
,
1070 sizeof(struct command_status_wrapper
));
1071 state
= SENDING_CSW
;
1072 //logf("CSW: %X",status);
1074 if(status
== UMS_STATUS_GOOD
) {
1075 cur_sense_data
.sense_key
=0;
1076 cur_sense_data
.information
=0;
1077 cur_sense_data
.asc
=0;
1078 cur_sense_data
.ascq
=0;
1082 static void copy_padded(char *dest
, char *src
, int len
)
1085 while(src
[i
]!=0 && i
<len
)
1097 /* build SCSI INQUIRY */
1098 static void fill_inquiry(IF_MV_NONVOID(int lun
))
1100 memset(tb
.inquiry
, 0, sizeof(struct inquiry_data
));
1101 struct storage_info info
;
1102 storage_get_info(lun
,&info
);
1103 copy_padded(tb
.inquiry
->VendorId
,info
.vendor
,sizeof(tb
.inquiry
->VendorId
));
1104 copy_padded(tb
.inquiry
->ProductId
,info
.product
,sizeof(tb
.inquiry
->ProductId
));
1105 copy_padded(tb
.inquiry
->ProductRevisionLevel
,info
.revision
,sizeof(tb
.inquiry
->ProductRevisionLevel
));
1107 tb
.inquiry
->DeviceType
= DIRECT_ACCESS_DEVICE
;
1108 tb
.inquiry
->AdditionalLength
= 0x1f;
1109 memset(tb
.inquiry
->Reserved
, 0, 3);
1110 tb
.inquiry
->Versions
= 4; /* SPC-2 */
1111 tb
.inquiry
->Format
= 2; /* SPC-2/3 inquiry format */
1113 #ifdef TOSHIBA_GIGABEAT_S
1114 tb
.inquiry
->DeviceTypeModifier
= 0;
1116 tb
.inquiry
->DeviceTypeModifier
= DEVICE_REMOVABLE
;
1120 #endif /* USB_STORAGE */