1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2007 by Björn Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
28 /* Needed to get at the audio buffer */
34 /* Enable the following define to export only the SD card slot. This
35 * is useful for USBCV MSC tests, as those are destructive.
36 * This won't work right if the device doesn't have a card slot.
38 //#define ONLY_EXPOSE_CARD_SLOT
40 #define SECTOR_SIZE 512
42 /* We can currently use up to 20k buffer size. More than that requires
43 * transfer chaining in the driver. Tests on sansa c200 show that the 16k
44 * limitation causes no more than 2% slowdown.
46 #define BUFFER_SIZE 16384
48 /* bulk-only class specific requests */
49 #define USB_BULK_RESET_REQUEST 0xff
50 #define USB_BULK_GET_MAX_LUN 0xfe
52 #define DIRECT_ACCESS_DEVICE 0x00 /* disks */
53 #define DEVICE_REMOVABLE 0x80
55 #define CBW_SIGNATURE 0x43425355
56 #define CSW_SIGNATURE 0x53425355
58 #define SCSI_TEST_UNIT_READY 0x00
59 #define SCSI_INQUIRY 0x12
60 #define SCSI_MODE_SENSE_6 0x1a
61 #define SCSI_MODE_SENSE_10 0x5a
62 #define SCSI_REQUEST_SENSE 0x03
63 #define SCSI_ALLOW_MEDIUM_REMOVAL 0x1e
64 #define SCSI_READ_CAPACITY 0x25
65 #define SCSI_READ_FORMAT_CAPACITY 0x23
66 #define SCSI_READ_10 0x28
67 #define SCSI_WRITE_10 0x2a
68 #define SCSI_START_STOP_UNIT 0x1b
69 #define SCSI_REPORT_LUNS 0xa0
71 #define UMS_STATUS_GOOD 0x00
72 #define UMS_STATUS_FAIL 0x01
74 #define SENSE_NOT_READY 0x02
75 #define SENSE_MEDIUM_ERROR 0x03
76 #define SENSE_ILLEGAL_REQUEST 0x05
77 #define SENSE_UNIT_ATTENTION 0x06
79 #define ASC_MEDIUM_NOT_PRESENT 0x3a
80 #define ASC_INVALID_FIELD_IN_CBD 0x24
81 #define ASC_LBA_OUT_OF_RANGE 0x21
82 #define ASC_WRITE_ERROR 0x0C
83 #define ASC_READ_ERROR 0x11
84 #define ASC_NOT_READY 0x04
85 #define ASCQ_BECOMING_READY 0x01
87 #define SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA 0x02000000
89 /* storage interface */
91 #define USB_SC_SCSI 0x06 /* Transparent */
92 #define USB_PROT_BULK 0x50 /* bulk only */
94 static struct usb_interface_descriptor
__attribute__((aligned(2)))
95 interface_descriptor
=
97 .bLength
= sizeof(struct usb_interface_descriptor
),
98 .bDescriptorType
= USB_DT_INTERFACE
,
99 .bInterfaceNumber
= 0,
100 .bAlternateSetting
= 0,
102 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
103 .bInterfaceSubClass
= USB_SC_SCSI
,
104 .bInterfaceProtocol
= USB_PROT_BULK
,
108 static struct usb_endpoint_descriptor
__attribute__((aligned(2)))
109 endpoint_descriptor
=
111 .bLength
= sizeof(struct usb_endpoint_descriptor
),
112 .bDescriptorType
= USB_DT_ENDPOINT
,
113 .bEndpointAddress
= 0,
114 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
119 struct inquiry_data
{
120 unsigned char DeviceType
;
121 unsigned char DeviceTypeModifier
;
122 unsigned char Versions
;
123 unsigned char Format
;
124 unsigned char AdditionalLength
;
125 unsigned char Reserved
[2];
126 unsigned char Capability
;
127 unsigned char VendorId
[8];
128 unsigned char ProductId
[16];
129 unsigned char ProductRevisionLevel
[4];
130 } __attribute__ ((packed
));
132 struct report_lun_data
{
133 unsigned int lun_list_length
;
134 unsigned int reserved1
;
135 unsigned char lun0
[8];
137 unsigned char lun1
[8];
139 } __attribute__ ((packed
));
142 unsigned char ResponseCode
;
143 unsigned char Obsolete
;
144 unsigned char fei_sensekey
;
145 unsigned int Information
;
146 unsigned char AdditionalSenseLength
;
147 unsigned int CommandSpecificInformation
;
148 unsigned char AdditionalSenseCode
;
149 unsigned char AdditionalSenseCodeQualifier
;
150 unsigned char FieldReplaceableUnitCode
;
152 unsigned short SenseKeySpecific
;
153 } __attribute__ ((packed
));
155 struct mode_sense_bdesc_longlba
{
156 unsigned char num_blocks
[8];
157 unsigned char reserved
[4];
158 unsigned char block_size
[4];
159 } __attribute__ ((packed
));
161 struct mode_sense_bdesc_shortlba
{
162 unsigned char density_code
;
163 unsigned char num_blocks
[3];
164 unsigned char reserved
;
165 unsigned char block_size
[3];
166 } __attribute__ ((packed
));
168 struct mode_sense_data_10
{
169 unsigned short mode_data_length
;
170 unsigned char medium_type
;
171 unsigned char device_specific
;
172 unsigned char longlba
;
173 unsigned char reserved
;
174 unsigned short block_descriptor_length
;
175 struct mode_sense_bdesc_longlba block_descriptor
;
176 } __attribute__ ((packed
));
178 struct mode_sense_data_6
{
179 unsigned char mode_data_length
;
180 unsigned char medium_type
;
181 unsigned char device_specific
;
182 unsigned char block_descriptor_length
;
183 struct mode_sense_bdesc_shortlba block_descriptor
;
184 } __attribute__ ((packed
));
186 struct command_block_wrapper
{
187 unsigned int signature
;
189 unsigned int data_transfer_length
;
192 unsigned char command_length
;
193 unsigned char command_block
[16];
194 } __attribute__ ((packed
));
196 struct command_status_wrapper
{
197 unsigned int signature
;
199 unsigned int data_residue
;
200 unsigned char status
;
201 } __attribute__ ((packed
));
204 unsigned int block_count
;
205 unsigned int block_size
;
206 } __attribute__ ((packed
));
208 struct format_capacity
{
209 unsigned int following_length
;
210 unsigned int block_count
;
211 unsigned int block_size
;
212 } __attribute__ ((packed
));
216 unsigned char* transfer_buffer
;
217 struct inquiry_data
* inquiry
;
218 struct capacity
* capacity_data
;
219 struct format_capacity
* format_capacity_data
;
220 struct sense_data
*sense_data
;
221 struct mode_sense_data_6
*ms_data_6
;
222 struct mode_sense_data_10
*ms_data_10
;
223 struct report_lun_data
*lun_data
;
224 struct command_status_wrapper
* csw
;
233 unsigned char *data
[2];
234 unsigned char data_select
;
235 unsigned int last_result
;
239 unsigned char sense_key
;
240 unsigned char information
;
245 static void handle_scsi(struct command_block_wrapper
* cbw
);
246 static void send_csw(int status
);
247 static void send_command_result(void *data
,int size
);
248 static void send_command_failed_result(void);
249 static void send_block_data(void *data
,int size
);
250 static void receive_block_data(void *data
,int size
);
251 static void identify2inquiry(int lun
);
252 static void send_and_read_next(void);
253 static bool ejected
[NUM_VOLUMES
];
255 static int usb_endpoint
;
256 static int usb_interface
;
262 SENDING_FAILED_RESULT
,
265 } state
= WAITING_FOR_COMMAND
;
267 static bool check_disk_present(IF_MV_NONVOID(int volume
))
269 unsigned char sector
[512];
270 return ata_read_sectors(IF_MV2(volume
,)0,1,sector
) == 0;
273 static void try_release_ata(void)
275 /* Check if there is a connected drive left. If not,
276 release excusive access */
277 bool canrelease
=true;
279 for(i
=0;i
<NUM_VOLUMES
;i
++) {
280 if(ejected
[i
]==false){
286 logf("scsi release ata");
287 usb_release_exclusive_ata();
292 void usb_storage_notify_hotswap(int volume
,bool inserted
)
294 logf("notify %d",inserted
);
295 if(inserted
&& check_disk_present(IF_MV(volume
))) {
296 ejected
[volume
] = false;
299 ejected
[volume
] = true;
306 void usb_storage_reconnect(void)
309 for(i
=0;i
<NUM_VOLUMES
;i
++)
310 ejected
[i
] = !check_disk_present(IF_MV(i
));
312 usb_request_exclusive_ata();
315 /* called by usb_code_init() */
316 void usb_storage_init(void)
319 for(i
=0;i
<NUM_VOLUMES
;i
++) {
320 ejected
[i
] = !check_disk_present(IF_MV(i
));
322 logf("usb_storage_init done");
326 int usb_storage_get_config_descriptor(unsigned char *dest
,int max_packet_size
,
327 int interface_number
,int endpoint
)
329 endpoint_descriptor
.wMaxPacketSize
=max_packet_size
;
330 interface_descriptor
.bInterfaceNumber
=interface_number
;
333 memcpy(dest
,&interface_descriptor
,
334 sizeof(struct usb_interface_descriptor
));
335 dest
+=sizeof(struct usb_interface_descriptor
);
337 endpoint_descriptor
.bEndpointAddress
= endpoint
| USB_DIR_IN
,
338 memcpy(dest
,&endpoint_descriptor
,
339 sizeof(struct usb_endpoint_descriptor
));
340 dest
+=sizeof(struct usb_endpoint_descriptor
);
342 endpoint_descriptor
.bEndpointAddress
= endpoint
| USB_DIR_OUT
,
343 memcpy(dest
,&endpoint_descriptor
,
344 sizeof(struct usb_endpoint_descriptor
));
346 return sizeof(struct usb_interface_descriptor
) +
347 2*sizeof(struct usb_endpoint_descriptor
);
350 void usb_storage_init_connection(int interface
,int endpoint
)
353 unsigned char * audio_buffer
;
355 usb_interface
= interface
;
356 usb_endpoint
= endpoint
;
358 logf("ums: set config");
359 /* prime rx endpoint. We only need room for commands */
360 state
= WAITING_FOR_COMMAND
;
362 /* TODO : check if bufsize is at least 32K ? */
363 audio_buffer
= audio_get_buffer(false,&bufsize
);
365 (void *)UNCACHED_ADDR((unsigned int)(audio_buffer
+ 31) & 0xffffffe0);
366 usb_drv_recv(usb_endpoint
, tb
.transfer_buffer
, 1024);
369 /* called by usb_core_transfer_complete() */
370 void usb_storage_transfer_complete(bool in
,int status
,int length
)
372 struct command_block_wrapper
* cbw
= (void*)tb
.transfer_buffer
;
374 //logf("transfer result %X %d", status, length);
376 case RECEIVING_BLOCKS
:
378 logf("IN received in RECEIVING");
380 logf("scsi write %d %d", cur_cmd
.sector
, cur_cmd
.count
);
382 if((unsigned int)length
!=(SECTOR_SIZE
*cur_cmd
.count
)
383 && (unsigned int)length
!=BUFFER_SIZE
) {
384 logf("unexpected length :%d",length
);
387 unsigned int next_sector
= cur_cmd
.sector
+
388 (BUFFER_SIZE
/SECTOR_SIZE
);
389 unsigned int next_count
= cur_cmd
.count
-
390 MIN(cur_cmd
.count
,BUFFER_SIZE
/SECTOR_SIZE
);
393 /* Ask the host to send more, to the other buffer */
394 receive_block_data(cur_cmd
.data
[!cur_cmd
.data_select
],
395 MIN(BUFFER_SIZE
,next_count
*SECTOR_SIZE
));
398 /* Now write the data that just came in, while the host is
399 sending the next bit */
400 int result
= ata_write_sectors(IF_MV2(cur_cmd
.lun
,)
402 MIN(BUFFER_SIZE
/SECTOR_SIZE
,
404 cur_cmd
.data
[cur_cmd
.data_select
]);
406 send_csw(UMS_STATUS_FAIL
);
407 cur_sense_data
.sense_key
=SENSE_MEDIUM_ERROR
;
408 cur_sense_data
.asc
=ASC_WRITE_ERROR
;
409 cur_sense_data
.ascq
=0;
414 send_csw(UMS_STATUS_GOOD
);
417 /* Switch buffers for the next one */
418 cur_cmd
.data_select
=!cur_cmd
.data_select
;
420 cur_cmd
.sector
= next_sector
;
421 cur_cmd
.count
= next_count
;
425 logf("Transfer failed %X",status
);
426 send_csw(UMS_STATUS_FAIL
);
427 /* TODO fill in cur_sense_data */
428 cur_sense_data
.sense_key
=0;
429 cur_sense_data
.information
=0;
430 cur_sense_data
.asc
=0;
431 cur_sense_data
.ascq
=0;
434 case WAITING_FOR_COMMAND
:
436 logf("IN received in WAITING_FOR_COMMAND");
438 //logf("command received");
439 if(letoh32(cbw
->signature
) == CBW_SIGNATURE
){
443 usb_drv_stall(usb_endpoint
, true,true);
444 usb_drv_stall(usb_endpoint
, true,false);
449 logf("OUT received in SENDING_CSW");
451 //logf("csw sent, now go back to idle");
452 state
= WAITING_FOR_COMMAND
;
453 usb_drv_recv(usb_endpoint
, tb
.transfer_buffer
, 1024);
457 logf("OUT received in SENDING");
460 //logf("data sent, now send csw");
461 send_csw(UMS_STATUS_GOOD
);
464 logf("Transfer failed %X",status
);
465 send_csw(UMS_STATUS_FAIL
);
466 /* TODO fill in cur_sense_data */
467 cur_sense_data
.sense_key
=0;
468 cur_sense_data
.information
=0;
469 cur_sense_data
.asc
=0;
470 cur_sense_data
.ascq
=0;
473 case SENDING_FAILED_RESULT
:
475 logf("OUT received in SENDING");
477 send_csw(UMS_STATUS_FAIL
);
481 logf("OUT received in SENDING");
484 if(cur_cmd
.count
==0) {
485 //logf("data sent, now send csw");
486 send_csw(UMS_STATUS_GOOD
);
489 send_and_read_next();
493 logf("Transfer failed %X",status
);
494 send_csw(UMS_STATUS_FAIL
);
495 /* TODO fill in cur_sense_data */
496 cur_sense_data
.sense_key
=0;
497 cur_sense_data
.information
=0;
498 cur_sense_data
.asc
=0;
499 cur_sense_data
.ascq
=0;
505 /* called by usb_core_control_request() */
506 bool usb_storage_control_request(struct usb_ctrlrequest
* req
)
508 bool handled
= false;
511 switch (req
->bRequest
) {
512 case USB_BULK_GET_MAX_LUN
: {
513 #ifdef ONLY_EXPOSE_CARD_SLOT
516 *tb
.max_lun
= NUM_VOLUMES
- 1;
518 logf("ums: getmaxlun");
519 usb_drv_send(EP_CONTROL
, UNCACHED_ADDR(tb
.max_lun
), 1);
520 usb_drv_recv(EP_CONTROL
, NULL
, 0); /* ack */
525 case USB_BULK_RESET_REQUEST
:
526 logf("ums: bulk reset");
527 state
= WAITING_FOR_COMMAND
;
528 /* UMS BOT 3.1 says The device shall preserve the value of its bulk
529 data toggle bits and endpoint STALL conditions despite
530 the Bulk-Only Mass Storage Reset. */
532 usb_drv_reset_endpoint(usb_endpoint
, false);
533 usb_drv_reset_endpoint(usb_endpoint
, true);
536 usb_drv_send(EP_CONTROL
, NULL
, 0); /* ack */
544 static void send_and_read_next(void)
546 if(cur_cmd
.last_result
!=0) {
547 /* The last read failed. */
548 send_csw(UMS_STATUS_FAIL
);
549 cur_sense_data
.sense_key
=SENSE_MEDIUM_ERROR
;
550 cur_sense_data
.asc
=ASC_READ_ERROR
;
551 cur_sense_data
.ascq
=0;
554 send_block_data(cur_cmd
.data
[cur_cmd
.data_select
],
555 MIN(BUFFER_SIZE
,cur_cmd
.count
*SECTOR_SIZE
));
557 /* Switch buffers for the next one */
558 cur_cmd
.data_select
=!cur_cmd
.data_select
;
560 cur_cmd
.sector
+=(BUFFER_SIZE
/SECTOR_SIZE
);
561 cur_cmd
.count
-=MIN(cur_cmd
.count
,BUFFER_SIZE
/SECTOR_SIZE
);
563 if(cur_cmd
.count
!=0){
564 /* already read the next bit, so we can send it out immediately when the
565 * current transfer completes. */
566 cur_cmd
.last_result
= ata_read_sectors(IF_MV2(cur_cmd
.lun
,)
568 MIN(BUFFER_SIZE
/SECTOR_SIZE
,
570 cur_cmd
.data
[cur_cmd
.data_select
]);
573 /****************************************************************************/
575 static void handle_scsi(struct command_block_wrapper
* cbw
)
577 /* USB Mass Storage assumes LBA capability.
578 TODO: support 48-bit LBA */
580 unsigned int length
= cbw
->data_transfer_length
;
581 unsigned int block_size
= 0;
582 unsigned int block_count
= 0;
583 bool lun_present
=true;
584 #ifdef ONLY_EXPOSE_CARD_SLOT
585 unsigned char lun
= cbw
->lun
+1;
587 unsigned char lun
= cbw
->lun
;
589 unsigned int block_size_mult
= 1;
591 tCardInfo
* cinfo
= card_get_info(lun
);
592 if(cinfo
->initialized
==1 && cinfo
->numblocks
> 0) {
593 block_size
= cinfo
->blocksize
;
594 block_count
= cinfo
->numblocks
;
601 unsigned short* identify
= ata_get_identify();
602 block_size
= SECTOR_SIZE
;
603 block_count
= (identify
[61] << 16 | identify
[60]);
609 #ifdef MAX_LOG_SECTOR_SIZE
610 block_size_mult
= disk_sector_multiplier
;
613 cur_cmd
.tag
= cbw
->tag
;
616 switch (cbw
->command_block
[0]) {
617 case SCSI_TEST_UNIT_READY
:
618 logf("scsi test_unit_ready %d",lun
);
619 if(!usb_exclusive_ata()) {
620 send_csw(UMS_STATUS_FAIL
);
621 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
622 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
623 cur_sense_data
.ascq
=0;
627 send_csw(UMS_STATUS_GOOD
);
630 send_csw(UMS_STATUS_FAIL
);
631 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
632 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
633 cur_sense_data
.ascq
=0;
637 case SCSI_REPORT_LUNS
: {
638 logf("scsi inquiry %d",lun
);
639 int allocation_length
=0;
640 allocation_length
|=(cbw
->command_block
[6]<<24);
641 allocation_length
|=(cbw
->command_block
[7]<<16);
642 allocation_length
|=(cbw
->command_block
[8]<<8);
643 allocation_length
|=(cbw
->command_block
[9]);
644 memset(tb
.lun_data
,0,sizeof(struct report_lun_data
));
646 tb
.lun_data
->lun_list_length
=htobe32(16);
647 tb
.lun_data
->lun1
[1]=1;
649 tb
.lun_data
->lun_list_length
=htobe32(8);
651 tb
.lun_data
->lun0
[1]=0;
653 send_command_result(tb
.lun_data
,
654 MIN(sizeof(struct report_lun_data
), length
));
659 logf("scsi inquiry %d",lun
);
660 identify2inquiry(lun
);
661 length
= MIN(length
, cbw
->command_block
[4]);
662 send_command_result(tb
.inquiry
,
663 MIN(sizeof(struct inquiry_data
), length
));
666 case SCSI_REQUEST_SENSE
: {
667 tb
.sense_data
->ResponseCode
=0x70;/*current error*/
668 tb
.sense_data
->Obsolete
=0;
669 tb
.sense_data
->fei_sensekey
=cur_sense_data
.sense_key
&0x0f;
670 tb
.sense_data
->Information
=cur_sense_data
.information
;
671 tb
.sense_data
->AdditionalSenseLength
=10;
672 tb
.sense_data
->CommandSpecificInformation
=0;
673 tb
.sense_data
->AdditionalSenseCode
=cur_sense_data
.asc
;
674 tb
.sense_data
->AdditionalSenseCodeQualifier
=cur_sense_data
.ascq
;
675 tb
.sense_data
->FieldReplaceableUnitCode
=0;
676 tb
.sense_data
->SKSV
=0;
677 tb
.sense_data
->SenseKeySpecific
=0;
678 logf("scsi request_sense %d",lun
);
679 send_command_result(tb
.sense_data
, sizeof(struct sense_data
));
683 case SCSI_MODE_SENSE_10
: {
685 send_command_failed_result();
686 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
687 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
688 cur_sense_data
.ascq
=0;
691 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
692 unsigned char page_code
= cbw
->command_block
[2] & 0x3f;
693 logf("scsi mode_sense_10 %d %X",lun
,page_code
);
696 tb
.ms_data_10
->mode_data_length
=
697 htobe16(sizeof(struct mode_sense_data_10
)-2);
698 tb
.ms_data_10
->medium_type
= 0;
699 tb
.ms_data_10
->device_specific
= 0;
700 tb
.ms_data_10
->reserved
= 0;
701 tb
.ms_data_10
->longlba
= 1;
702 tb
.ms_data_10
->block_descriptor_length
=
703 htobe16(sizeof(struct mode_sense_bdesc_longlba
));
705 memset(tb
.ms_data_10
->block_descriptor
.reserved
,0,4);
706 memset(tb
.ms_data_10
->block_descriptor
.num_blocks
,0,8);
708 tb
.ms_data_10
->block_descriptor
.num_blocks
[4] =
709 ((block_count
/block_size_mult
) & 0xff000000)>>24;
710 tb
.ms_data_10
->block_descriptor
.num_blocks
[5] =
711 ((block_count
/block_size_mult
) & 0x00ff0000)>>16;
712 tb
.ms_data_10
->block_descriptor
.num_blocks
[6] =
713 ((block_count
/block_size_mult
) & 0x0000ff00)>>8;
714 tb
.ms_data_10
->block_descriptor
.num_blocks
[7] =
715 ((block_count
/block_size_mult
) & 0x000000ff);
717 tb
.ms_data_10
->block_descriptor
.block_size
[0] =
718 ((block_size
*block_size_mult
) & 0xff000000)>>24;
719 tb
.ms_data_10
->block_descriptor
.block_size
[1] =
720 ((block_size
*block_size_mult
) & 0x00ff0000)>>16;
721 tb
.ms_data_10
->block_descriptor
.block_size
[2] =
722 ((block_size
*block_size_mult
) & 0x0000ff00)>>8;
723 tb
.ms_data_10
->block_descriptor
.block_size
[3] =
724 ((block_size
*block_size_mult
) & 0x000000ff);
725 send_command_result(tb
.ms_data_10
,
726 MIN(sizeof(struct mode_sense_data_10
), length
));
729 send_command_failed_result();
730 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
731 cur_sense_data
.asc
=ASC_INVALID_FIELD_IN_CBD
;
732 cur_sense_data
.ascq
=0;
737 case SCSI_MODE_SENSE_6
: {
739 send_command_failed_result();
740 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
741 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
742 cur_sense_data
.ascq
=0;
745 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
746 unsigned char page_code
= cbw
->command_block
[2] & 0x3f;
747 logf("scsi mode_sense_6 %d %X",lun
,page_code
);
750 /* All supported pages. */
751 tb
.ms_data_6
->mode_data_length
=
752 sizeof(struct mode_sense_data_6
)-1;
753 tb
.ms_data_6
->medium_type
= 0;
754 tb
.ms_data_6
->device_specific
= 0;
755 tb
.ms_data_6
->block_descriptor_length
=
756 sizeof(struct mode_sense_bdesc_shortlba
);
757 tb
.ms_data_6
->block_descriptor
.density_code
= 0;
758 tb
.ms_data_6
->block_descriptor
.reserved
= 0;
759 if(block_count
/block_size_mult
> 0xffffff){
760 tb
.ms_data_6
->block_descriptor
.num_blocks
[0] = 0xff;
761 tb
.ms_data_6
->block_descriptor
.num_blocks
[1] = 0xff;
762 tb
.ms_data_6
->block_descriptor
.num_blocks
[2] = 0xff;
765 tb
.ms_data_6
->block_descriptor
.num_blocks
[0] =
766 ((block_count
/block_size_mult
) & 0xff0000)>>16;
767 tb
.ms_data_6
->block_descriptor
.num_blocks
[1] =
768 ((block_count
/block_size_mult
) & 0x00ff00)>>8;
769 tb
.ms_data_6
->block_descriptor
.num_blocks
[2] =
770 ((block_count
/block_size_mult
) & 0x0000ff);
772 tb
.ms_data_6
->block_descriptor
.block_size
[0] =
773 ((block_size
*block_size_mult
) & 0xff0000)>>16;
774 tb
.ms_data_6
->block_descriptor
.block_size
[1] =
775 ((block_size
*block_size_mult
) & 0x00ff00)>>8;
776 tb
.ms_data_6
->block_descriptor
.block_size
[2] =
777 ((block_size
*block_size_mult
) & 0x0000ff);
778 send_command_result(tb
.ms_data_6
,
779 MIN(sizeof(struct mode_sense_data_6
), length
));
782 send_command_failed_result();
783 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
784 cur_sense_data
.asc
=ASC_INVALID_FIELD_IN_CBD
;
785 cur_sense_data
.ascq
=0;
791 case SCSI_START_STOP_UNIT
:
792 logf("scsi start_stop unit %d",lun
);
793 if((cbw
->command_block
[4] & 0xf0) == 0) /*load/eject bit is valid*/
794 { /* Process start and eject bits */
795 logf("scsi load/eject");
796 if((cbw
->command_block
[4] & 0x01) == 0) /* Don't start */
798 if((cbw
->command_block
[4] & 0x02) != 0) /* eject */
806 send_csw(UMS_STATUS_GOOD
);
809 case SCSI_ALLOW_MEDIUM_REMOVAL
:
810 logf("scsi allow_medium_removal %d",lun
);
811 /* TODO: use this to show the connect screen ? */
812 send_csw(UMS_STATUS_GOOD
);
814 case SCSI_READ_FORMAT_CAPACITY
: {
815 logf("scsi read_format_capacity %d",lun
);
817 tb
.format_capacity_data
->following_length
=htobe32(8);
818 /* "block count" actually means "number of last block" */
819 tb
.format_capacity_data
->block_count
=
820 htobe32(block_count
/block_size_mult
- 1);
821 tb
.format_capacity_data
->block_size
=
822 htobe32(block_size
*block_size_mult
);
823 tb
.format_capacity_data
->block_size
|=
824 htobe32(SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA
);
826 send_command_result(tb
.format_capacity_data
,
827 MIN(sizeof(struct format_capacity
), length
));
831 send_command_failed_result();
832 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
833 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
834 cur_sense_data
.ascq
=0;
838 case SCSI_READ_CAPACITY
: {
839 logf("scsi read_capacity %d",lun
);
842 /* "block count" actually means "number of last block" */
843 tb
.capacity_data
->block_count
=
844 htobe32(block_count
/block_size_mult
- 1);
845 tb
.capacity_data
->block_size
=
846 htobe32(block_size
*block_size_mult
);
848 send_command_result(tb
.capacity_data
,
849 MIN(sizeof(struct capacity
), length
));
853 send_command_failed_result();
854 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
855 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
856 cur_sense_data
.ascq
=0;
862 logf("scsi read10 %d",lun
);
864 send_command_failed_result();
865 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
866 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
867 cur_sense_data
.ascq
=0;
870 cur_cmd
.data
[0] = tb
.transfer_buffer
;
871 cur_cmd
.data
[1] = &tb
.transfer_buffer
[BUFFER_SIZE
];
872 cur_cmd
.data_select
=0;
873 cur_cmd
.sector
= block_size_mult
*
874 (cbw
->command_block
[2] << 24 |
875 cbw
->command_block
[3] << 16 |
876 cbw
->command_block
[4] << 8 |
877 cbw
->command_block
[5] );
878 cur_cmd
.count
= block_size_mult
*
879 (cbw
->command_block
[7] << 8 |
880 cbw
->command_block
[8]);
882 //logf("scsi read %d %d", cur_cmd.sector, cur_cmd.count);
884 if((cur_cmd
.sector
+ cur_cmd
.count
) > block_count
) {
885 send_csw(UMS_STATUS_FAIL
);
886 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
887 cur_sense_data
.asc
=ASC_LBA_OUT_OF_RANGE
;
888 cur_sense_data
.ascq
=0;
891 cur_cmd
.last_result
= ata_read_sectors(IF_MV2(cur_cmd
.lun
,)
893 MIN(BUFFER_SIZE
/SECTOR_SIZE
,
895 cur_cmd
.data
[cur_cmd
.data_select
]);
896 send_and_read_next();
901 logf("scsi write10 %d",lun
);
903 send_csw(UMS_STATUS_FAIL
);
904 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
905 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
906 cur_sense_data
.ascq
=0;
909 cur_cmd
.data
[0] = tb
.transfer_buffer
;
910 cur_cmd
.data
[1] = &tb
.transfer_buffer
[BUFFER_SIZE
];
911 cur_cmd
.data_select
=0;
912 cur_cmd
.sector
= block_size_mult
*
913 (cbw
->command_block
[2] << 24 |
914 cbw
->command_block
[3] << 16 |
915 cbw
->command_block
[4] << 8 |
916 cbw
->command_block
[5] );
917 cur_cmd
.count
= block_size_mult
*
918 (cbw
->command_block
[7] << 8 |
919 cbw
->command_block
[8]);
921 if((cur_cmd
.sector
+ cur_cmd
.count
) > block_count
) {
922 send_csw(UMS_STATUS_FAIL
);
923 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
924 cur_sense_data
.asc
=ASC_LBA_OUT_OF_RANGE
;
925 cur_sense_data
.ascq
=0;
928 receive_block_data(cur_cmd
.data
[0],
930 cur_cmd
.count
*SECTOR_SIZE
));
936 logf("scsi unknown cmd %x",cbw
->command_block
[0x0]);
937 usb_drv_stall(usb_endpoint
, true,true);
938 send_csw(UMS_STATUS_FAIL
);
943 static void send_block_data(void *data
,int size
)
945 usb_drv_send_nonblocking(usb_endpoint
, data
,size
);
946 state
= SENDING_BLOCKS
;
949 static void send_command_result(void *data
,int size
)
951 usb_drv_send_nonblocking(usb_endpoint
, data
,size
);
952 state
= SENDING_RESULT
;
955 static void send_command_failed_result(void)
957 usb_drv_send_nonblocking(usb_endpoint
, NULL
, 0);
958 state
= SENDING_FAILED_RESULT
;
961 static void receive_block_data(void *data
,int size
)
963 usb_drv_recv(usb_endpoint
, data
, size
);
964 state
= RECEIVING_BLOCKS
;
967 static void send_csw(int status
)
969 tb
.csw
->signature
= htole32(CSW_SIGNATURE
);
970 tb
.csw
->tag
= cur_cmd
.tag
;
971 tb
.csw
->data_residue
= 0;
972 tb
.csw
->status
= status
;
974 usb_drv_send_nonblocking(usb_endpoint
, tb
.csw
,
975 sizeof(struct command_status_wrapper
));
977 //logf("CSW: %X",status);
979 if(status
== UMS_STATUS_GOOD
) {
980 cur_sense_data
.sense_key
=0;
981 cur_sense_data
.information
=0;
982 cur_sense_data
.asc
=0;
983 cur_sense_data
.ascq
=0;
987 /* convert ATA IDENTIFY to SCSI INQUIRY */
988 static void identify2inquiry(int lun
)
990 #ifdef HAVE_FLASH_STORAGE
992 memcpy(&tb
.inquiry
->VendorId
,"Rockbox ",8);
993 memcpy(&tb
.inquiry
->ProductId
,"Internal Storage",16);
994 memcpy(&tb
.inquiry
->ProductRevisionLevel
,"0.00",4);
997 memcpy(&tb
.inquiry
->VendorId
,"Rockbox ",8);
998 memcpy(&tb
.inquiry
->ProductId
,"SD Card Slot ",16);
999 memcpy(&tb
.inquiry
->ProductRevisionLevel
,"0.00",4);
1003 unsigned short* dest
;
1004 unsigned short* src
;
1005 unsigned short* identify
= ata_get_identify();
1007 memset(tb
.inquiry
, 0, sizeof(struct inquiry_data
));
1010 if (identify
[82] & 4)
1011 tb
.inquiry
->DeviceTypeModifier
= DEVICE_REMOVABLE
;
1014 /* ATA only has a 'model' field, so we copy the
1015 first 8 bytes to 'vendor' and the rest to 'product' (they are
1016 consecutive in the inquiry struct) */
1017 src
= (unsigned short*)&identify
[27];
1018 dest
= (unsigned short*)&tb
.inquiry
->VendorId
;
1020 dest
[i
] = htobe16(src
[i
]);
1022 src
= (unsigned short*)&identify
[23];
1023 dest
= (unsigned short*)&tb
.inquiry
->ProductRevisionLevel
;
1025 dest
[i
] = htobe16(src
[i
]);
1028 tb
.inquiry
->DeviceType
= DIRECT_ACCESS_DEVICE
;
1029 tb
.inquiry
->AdditionalLength
= 0x1f;
1030 memset(tb
.inquiry
->Reserved
, 0, 3);
1031 tb
.inquiry
->Versions
= 4; /* SPC-2 */
1032 tb
.inquiry
->Format
= 2; /* SPC-2/3 inquiry format */
1037 tb
.inquiry
->DeviceTypeModifier
= DEVICE_REMOVABLE
;
1040 /* Mac OSX 10.5 doesn't like this driver if DEVICE_REMOVABLE is not set.
1041 TODO : this can probably be solved by providing caching mode page */
1042 tb
.inquiry
->DeviceTypeModifier
= DEVICE_REMOVABLE
;
1045 #endif /* USB_STORAGE */