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"
31 /* Needed to get at the audio buffer */
33 #include "usb_storage.h"
34 #include "timefuncs.h"
37 /* Enable the following define to export only the SD card slot. This
38 * is useful for USBCV MSC tests, as those are destructive.
39 * This won't work right if the device doesn't have a card slot.
41 //#define ONLY_EXPOSE_CARD_SLOT
43 #ifdef USB_USE_RAMDISK
44 #define RAMDISK_SIZE 2048
47 #define SECTOR_SIZE 512
49 /* the ARC driver currently supports up to 64k USB transfers. This is
50 * enough for efficient mass storage support, as commonly host OSes
51 * don't do larger SCSI transfers anyway, so larger USB transfers
52 * wouldn't buy us anything.
53 * Due to being the double-buffering system used, using a smaller write buffer
54 * ends up being more efficient. Measurements have shown that 24k to 28k is
55 * optimal, except for sd devices that apparently don't gain anything from
58 #define READ_BUFFER_SIZE (1024*64)
60 #if (CONFIG_STORAGE & STORAGE_SD)
61 #define WRITE_BUFFER_SIZE (1024*64)
63 #define WRITE_BUFFER_SIZE (1024*24)
66 #define ALLOCATE_BUFFER_SIZE (2*MAX(READ_BUFFER_SIZE,WRITE_BUFFER_SIZE))
68 /* bulk-only class specific requests */
69 #define USB_BULK_RESET_REQUEST 0xff
70 #define USB_BULK_GET_MAX_LUN 0xfe
72 #define DIRECT_ACCESS_DEVICE 0x00 /* disks */
73 #define DEVICE_REMOVABLE 0x80
75 #define CBW_SIGNATURE 0x43425355
76 #define CSW_SIGNATURE 0x53425355
78 #define SCSI_TEST_UNIT_READY 0x00
79 #define SCSI_INQUIRY 0x12
80 #define SCSI_MODE_SENSE_6 0x1a
81 #define SCSI_MODE_SENSE_10 0x5a
82 #define SCSI_REQUEST_SENSE 0x03
83 #define SCSI_ALLOW_MEDIUM_REMOVAL 0x1e
84 #define SCSI_READ_CAPACITY 0x25
85 #define SCSI_READ_FORMAT_CAPACITY 0x23
86 #define SCSI_READ_10 0x28
87 #define SCSI_WRITE_10 0x2a
88 #define SCSI_START_STOP_UNIT 0x1b
89 #define SCSI_REPORT_LUNS 0xa0
90 #define SCSI_WRITE_BUFFER 0x3b
92 #define UMS_STATUS_GOOD 0x00
93 #define UMS_STATUS_FAIL 0x01
95 #define SENSE_NOT_READY 0x02
96 #define SENSE_MEDIUM_ERROR 0x03
97 #define SENSE_ILLEGAL_REQUEST 0x05
98 #define SENSE_UNIT_ATTENTION 0x06
100 #define ASC_MEDIUM_NOT_PRESENT 0x3a
101 #define ASC_INVALID_FIELD_IN_CBD 0x24
102 #define ASC_LBA_OUT_OF_RANGE 0x21
103 #define ASC_WRITE_ERROR 0x0C
104 #define ASC_READ_ERROR 0x11
105 #define ASC_NOT_READY 0x04
106 #define ASC_INVALID_COMMAND 0x20
108 #define ASCQ_BECOMING_READY 0x01
110 #define SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA 0x02000000
112 /* storage interface */
114 #define USB_SC_SCSI 0x06 /* Transparent */
115 #define USB_PROT_BULK 0x50 /* bulk only */
117 static struct usb_interface_descriptor
__attribute__((aligned(2)))
118 interface_descriptor
=
120 .bLength
= sizeof(struct usb_interface_descriptor
),
121 .bDescriptorType
= USB_DT_INTERFACE
,
122 .bInterfaceNumber
= 0,
123 .bAlternateSetting
= 0,
125 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
126 .bInterfaceSubClass
= USB_SC_SCSI
,
127 .bInterfaceProtocol
= USB_PROT_BULK
,
131 static struct usb_endpoint_descriptor
__attribute__((aligned(2)))
132 endpoint_descriptor
=
134 .bLength
= sizeof(struct usb_endpoint_descriptor
),
135 .bDescriptorType
= USB_DT_ENDPOINT
,
136 .bEndpointAddress
= 0,
137 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
142 struct inquiry_data
{
143 unsigned char DeviceType
;
144 unsigned char DeviceTypeModifier
;
145 unsigned char Versions
;
146 unsigned char Format
;
147 unsigned char AdditionalLength
;
148 unsigned char Reserved
[2];
149 unsigned char Capability
;
150 unsigned char VendorId
[8];
151 unsigned char ProductId
[16];
152 unsigned char ProductRevisionLevel
[4];
153 } __attribute__ ((packed
));
155 struct report_lun_data
{
156 unsigned int lun_list_length
;
157 unsigned int reserved1
;
158 unsigned char luns
[NUM_DRIVES
][8];
159 } __attribute__ ((packed
));
162 unsigned char ResponseCode
;
163 unsigned char Obsolete
;
164 unsigned char fei_sensekey
;
165 unsigned int Information
;
166 unsigned char AdditionalSenseLength
;
167 unsigned int CommandSpecificInformation
;
168 unsigned char AdditionalSenseCode
;
169 unsigned char AdditionalSenseCodeQualifier
;
170 unsigned char FieldReplaceableUnitCode
;
172 unsigned short SenseKeySpecific
;
173 } __attribute__ ((packed
));
175 struct mode_sense_bdesc_longlba
{
176 unsigned char num_blocks
[8];
177 unsigned char reserved
[4];
178 unsigned char block_size
[4];
179 } __attribute__ ((packed
));
181 struct mode_sense_bdesc_shortlba
{
182 unsigned char density_code
;
183 unsigned char num_blocks
[3];
184 unsigned char reserved
;
185 unsigned char block_size
[3];
186 } __attribute__ ((packed
));
188 struct mode_sense_data_10
{
189 unsigned short mode_data_length
;
190 unsigned char medium_type
;
191 unsigned char device_specific
;
192 unsigned char longlba
;
193 unsigned char reserved
;
194 unsigned short block_descriptor_length
;
195 struct mode_sense_bdesc_longlba block_descriptor
;
196 } __attribute__ ((packed
));
198 struct mode_sense_data_6
{
199 unsigned char mode_data_length
;
200 unsigned char medium_type
;
201 unsigned char device_specific
;
202 unsigned char block_descriptor_length
;
203 struct mode_sense_bdesc_shortlba block_descriptor
;
204 } __attribute__ ((packed
));
206 struct command_block_wrapper
{
207 unsigned int signature
;
209 unsigned int data_transfer_length
;
212 unsigned char command_length
;
213 unsigned char command_block
[16];
214 } __attribute__ ((packed
));
216 struct command_status_wrapper
{
217 unsigned int signature
;
219 unsigned int data_residue
;
220 unsigned char status
;
221 } __attribute__ ((packed
));
224 unsigned int block_count
;
225 unsigned int block_size
;
226 } __attribute__ ((packed
));
228 struct format_capacity
{
229 unsigned int following_length
;
230 unsigned int block_count
;
231 unsigned int block_size
;
232 } __attribute__ ((packed
));
236 unsigned char* transfer_buffer
;
237 struct inquiry_data
* inquiry
;
238 struct capacity
* capacity_data
;
239 struct format_capacity
* format_capacity_data
;
240 struct sense_data
*sense_data
;
241 struct mode_sense_data_6
*ms_data_6
;
242 struct mode_sense_data_10
*ms_data_10
;
243 struct report_lun_data
*lun_data
;
244 struct command_status_wrapper
* csw
;
248 static char *cbw_buffer
;
253 unsigned int orig_count
;
254 unsigned int cur_cmd
;
257 unsigned char *data
[2];
258 unsigned char data_select
;
259 unsigned int last_result
;
263 unsigned char sense_key
;
264 unsigned char information
;
269 static void handle_scsi(struct command_block_wrapper
* cbw
);
270 static void send_csw(int status
);
271 static void send_command_result(void *data
,int size
);
272 static void send_command_failed_result(void);
273 static void send_block_data(void *data
,int size
);
274 static void receive_block_data(void *data
,int size
);
275 static void receive_time(void);
276 static void fill_inquiry(IF_MD_NONVOID(int lun
));
277 static void send_and_read_next(void);
278 static bool ejected
[NUM_DRIVES
];
279 static bool locked
[NUM_DRIVES
];
281 static int usb_interface
;
282 static int ep_in
, ep_out
;
284 #ifdef USB_USE_RAMDISK
285 static unsigned char* ramdisk_buffer
;
292 SENDING_FAILED_RESULT
,
296 } state
= WAITING_FOR_COMMAND
;
298 static void yearday_to_daymonth(int yd
, int y
, int *d
, int *m
)
300 static const char tnl
[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
301 static const char tl
[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
305 if((y
%4 == 0 && y
%100 != 0) || y
%400 == 0)
314 while(yd
>= t
[i
] && i
<12)
323 #ifdef TOSHIBA_GIGABEAT_S
325 /* The Gigabeat S factory partition table contains invalid values for the
326 "active" flag in the MBR. This prevents at least the Linux kernel from
327 accepting the partition table, so we fix it on-the-fly. */
329 static void fix_mbr(unsigned char* mbr
)
331 unsigned char* p
= mbr
+ 0x1be;
340 static bool check_disk_present(IF_MD_NONVOID(int volume
))
342 #ifdef USB_USE_RAMDISK
345 unsigned char sector
[512];
346 return storage_read_sectors(volume
,0,1,sector
) == 0;
350 void usb_storage_try_release_storage(void)
352 /* Check if there is a connected drive left. If not,
353 release excusive access */
354 bool canrelease
=true;
356 for(i
=0;i
<storage_num_drives();i
++) {
357 if(ejected
[i
]==false && locked
[i
]==true) {
363 logf("scsi release ata");
364 usb_release_exclusive_storage();
369 void usb_storage_notify_hotswap(int volume
,bool inserted
)
371 logf("notify %d",inserted
);
372 if(inserted
&& check_disk_present(IF_MD(volume
))) {
373 ejected
[volume
] = false;
376 ejected
[volume
] = true;
377 /* If this happens while the device is locked, weird things may happen.
378 At least try to keep our state consistent */
379 locked
[volume
]=false;
384 /* called by usb_core_init() */
385 void usb_storage_init(void)
387 logf("usb_storage_init done");
390 int usb_storage_request_endpoints(struct usb_class_driver
*drv
)
392 ep_in
= usb_core_request_endpoint(USB_ENDPOINT_XFER_BULK
, USB_DIR_IN
, drv
);
397 ep_out
= usb_core_request_endpoint(USB_ENDPOINT_XFER_BULK
, USB_DIR_OUT
,
401 usb_core_release_endpoint(ep_in
);
408 int usb_storage_set_first_interface(int interface
)
410 usb_interface
= interface
;
411 return interface
+ 1;
414 int usb_storage_get_config_descriptor(unsigned char *dest
,int max_packet_size
)
416 unsigned char *orig_dest
= dest
;
418 interface_descriptor
.bInterfaceNumber
= usb_interface
;
419 PACK_DATA(dest
, interface_descriptor
);
421 endpoint_descriptor
.wMaxPacketSize
= max_packet_size
;
423 endpoint_descriptor
.bEndpointAddress
= ep_in
;
424 PACK_DATA(dest
, endpoint_descriptor
);
426 endpoint_descriptor
.bEndpointAddress
= ep_out
;
427 PACK_DATA(dest
, endpoint_descriptor
);
429 return (dest
- orig_dest
);
432 void usb_storage_init_connection(void)
434 logf("ums: set config");
435 /* prime rx endpoint. We only need room for commands */
436 state
= WAITING_FOR_COMMAND
;
438 #if CONFIG_CPU == IMX31L || defined(CPU_TCC77X) || defined(CPU_TCC780X) || \
439 defined(BOOTLOADER) || CONFIG_CPU == DM320
440 static unsigned char _cbw_buffer
[ALLOCATE_BUFFER_SIZE
]
441 USB_DEVBSS_ATTR
__attribute__((aligned(32)));
442 cbw_buffer
= (void *)_cbw_buffer
;
444 static unsigned char _transfer_buffer
[ALLOCATE_BUFFER_SIZE
]
445 USB_DEVBSS_ATTR
__attribute__((aligned(32)));
446 tb
.transfer_buffer
= (void *)_transfer_buffer
;
447 #ifdef USB_USE_RAMDISK
448 static unsigned char _ramdisk_buffer
[RAMDISK_SIZE
*SECTOR_SIZE
];
449 ramdisk_buffer
= _ramdisk_buffer
;
452 /* TODO : check if bufsize is at least 32K ? */
454 unsigned char * audio_buffer
;
456 audio_buffer
= audio_get_buffer(false,&bufsize
);
457 cbw_buffer
= (void *)UNCACHED_ADDR((unsigned int)(audio_buffer
+31) & 0xffffffe0);
458 tb
.transfer_buffer
= cbw_buffer
+ 1024;
459 cpucache_invalidate();
460 #ifdef USB_USE_RAMDISK
461 ramdisk_buffer
= tb
.transfer_buffer
+ ALLOCATE_BUFFER_SIZE
;
464 usb_drv_recv(ep_out
, cbw_buffer
, 1024);
467 for(i
=0;i
<storage_num_drives();i
++) {
468 #ifdef TOSHIBA_GIGABEAT_S
469 /* As long as the Gigabeat S is a non-removable device, we need
470 to mark the device as locked to avoid usb_storage_try_release_ata()
471 to leave MSC mode while the device is in use */
476 ejected
[i
] = !check_disk_present(IF_MD(i
));
477 queue_broadcast(SYS_USB_LUN_LOCKED
, (i
<<16)+0);
481 void usb_storage_disconnect(void)
486 /* called by usb_core_transfer_complete() */
487 void usb_storage_transfer_complete(int ep
,int dir
,int status
,int length
)
490 struct command_block_wrapper
* cbw
= (void*)cbw_buffer
;
493 //logf("transfer result %X %d", status, length);
495 case RECEIVING_BLOCKS
:
496 if(dir
==USB_DIR_IN
) {
497 logf("IN received in RECEIVING");
499 logf("scsi write %d %d", cur_cmd
.sector
, cur_cmd
.count
);
501 if((unsigned int)length
!=(SECTOR_SIZE
* cur_cmd
.count
)
502 && (unsigned int)length
!=WRITE_BUFFER_SIZE
) {
503 logf("unexpected length :%d",length
);
507 unsigned int next_sector
= cur_cmd
.sector
+
508 (WRITE_BUFFER_SIZE
/SECTOR_SIZE
);
509 unsigned int next_count
= cur_cmd
.count
-
510 MIN(cur_cmd
.count
,WRITE_BUFFER_SIZE
/SECTOR_SIZE
);
511 int next_select
= !cur_cmd
.data_select
;
514 /* Ask the host to send more, to the other buffer */
515 receive_block_data(cur_cmd
.data
[next_select
],
516 MIN(WRITE_BUFFER_SIZE
,next_count
*SECTOR_SIZE
));
519 /* Now write the data that just came in, while the host is
520 sending the next bit */
521 #ifdef USB_USE_RAMDISK
522 memcpy(ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
523 cur_cmd
.data
[cur_cmd
.data_select
],
524 MIN(WRITE_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
)*SECTOR_SIZE
);
526 int result
= storage_write_sectors(cur_cmd
.lun
,
528 MIN(WRITE_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
),
529 cur_cmd
.data
[cur_cmd
.data_select
]);
531 send_csw(UMS_STATUS_FAIL
);
532 cur_sense_data
.sense_key
=SENSE_MEDIUM_ERROR
;
533 cur_sense_data
.asc
=ASC_WRITE_ERROR
;
534 cur_sense_data
.ascq
=0;
539 send_csw(UMS_STATUS_GOOD
);
542 /* Switch buffers for the next one */
543 cur_cmd
.data_select
=!cur_cmd
.data_select
;
545 cur_cmd
.sector
= next_sector
;
546 cur_cmd
.count
= next_count
;
549 logf("Transfer failed %X",status
);
550 send_csw(UMS_STATUS_FAIL
);
551 /* TODO fill in cur_sense_data */
552 cur_sense_data
.sense_key
=0;
553 cur_sense_data
.information
=0;
554 cur_sense_data
.asc
=0;
555 cur_sense_data
.ascq
=0;
558 case WAITING_FOR_COMMAND
:
559 if(dir
==USB_DIR_IN
) {
560 logf("IN received in WAITING_FOR_COMMAND");
562 //logf("command received");
563 if(letoh32(cbw
->signature
) == CBW_SIGNATURE
) {
567 usb_drv_stall(ep_in
, true,true);
568 usb_drv_stall(ep_out
, true,false);
572 if(dir
==USB_DIR_OUT
) {
573 logf("OUT received in SENDING_CSW");
575 //logf("csw sent, now go back to idle");
576 state
= WAITING_FOR_COMMAND
;
578 if(cur_cmd
.cur_cmd
== SCSI_WRITE_10
)
580 queue_broadcast(SYS_USB_WRITE_DATA
, (cur_cmd
.lun
<<16)+cur_cmd
.orig_count
);
582 else if(cur_cmd
.cur_cmd
== SCSI_READ_10
)
584 queue_broadcast(SYS_USB_READ_DATA
, (cur_cmd
.lun
<<16)+cur_cmd
.orig_count
);
589 if(dir
==USB_DIR_OUT
) {
590 logf("OUT received in SENDING");
593 //logf("data sent, now send csw");
594 send_csw(UMS_STATUS_GOOD
);
597 logf("Transfer failed %X",status
);
598 send_csw(UMS_STATUS_FAIL
);
599 /* TODO fill in cur_sense_data */
600 cur_sense_data
.sense_key
=0;
601 cur_sense_data
.information
=0;
602 cur_sense_data
.asc
=0;
603 cur_sense_data
.ascq
=0;
606 case SENDING_FAILED_RESULT
:
607 if(dir
==USB_DIR_OUT
) {
608 logf("OUT received in SENDING");
610 send_csw(UMS_STATUS_FAIL
);
613 if(dir
==USB_DIR_OUT
) {
614 logf("OUT received in SENDING");
617 if(cur_cmd
.count
==0) {
618 //logf("data sent, now send csw");
619 send_csw(UMS_STATUS_GOOD
);
622 send_and_read_next();
626 logf("Transfer failed %X",status
);
627 send_csw(UMS_STATUS_FAIL
);
628 /* TODO fill in cur_sense_data */
629 cur_sense_data
.sense_key
=0;
630 cur_sense_data
.information
=0;
631 cur_sense_data
.asc
=0;
632 cur_sense_data
.ascq
=0;
636 tm
.tm_year
=(tb
.transfer_buffer
[0]<<8)+tb
.transfer_buffer
[1] - 1900;
637 tm
.tm_yday
=(tb
.transfer_buffer
[2]<<8)+tb
.transfer_buffer
[3];
638 tm
.tm_hour
=tb
.transfer_buffer
[5];
639 tm
.tm_min
=tb
.transfer_buffer
[6];
640 tm
.tm_sec
=tb
.transfer_buffer
[7];
641 yearday_to_daymonth(tm
.tm_yday
,tm
.tm_year
+ 1900,&tm
.tm_mday
,&tm
.tm_mon
);
642 set_day_of_week(&tm
);
644 send_csw(UMS_STATUS_GOOD
);
649 /* called by usb_core_control_request() */
650 bool usb_storage_control_request(struct usb_ctrlrequest
* req
, unsigned char* dest
)
652 bool handled
= false;
655 switch (req
->bRequest
) {
656 case USB_BULK_GET_MAX_LUN
: {
657 *tb
.max_lun
= storage_num_drives() - 1;
658 #ifdef HIDE_FIRST_DRIVE
661 logf("ums: getmaxlun");
662 usb_drv_send(EP_CONTROL
, tb
.max_lun
, 1);
663 usb_drv_recv(EP_CONTROL
, NULL
, 0); /* ack */
668 case USB_BULK_RESET_REQUEST
:
669 logf("ums: bulk reset");
670 state
= WAITING_FOR_COMMAND
;
671 /* UMS BOT 3.1 says The device shall preserve the value of its bulk
672 data toggle bits and endpoint STALL conditions despite
673 the Bulk-Only Mass Storage Reset. */
675 usb_drv_reset_endpoint(ep_in
, false);
676 usb_drv_reset_endpoint(ep_out
, true);
678 usb_drv_send(EP_CONTROL
, NULL
, 0); /* ack */
686 static void send_and_read_next(void)
688 if(cur_cmd
.last_result
!=0) {
689 /* The last read failed. */
690 send_csw(UMS_STATUS_FAIL
);
691 cur_sense_data
.sense_key
=SENSE_MEDIUM_ERROR
;
692 cur_sense_data
.asc
=ASC_READ_ERROR
;
693 cur_sense_data
.ascq
=0;
696 send_block_data(cur_cmd
.data
[cur_cmd
.data_select
],
697 MIN(READ_BUFFER_SIZE
,cur_cmd
.count
*SECTOR_SIZE
));
699 /* Switch buffers for the next one */
700 cur_cmd
.data_select
=!cur_cmd
.data_select
;
702 cur_cmd
.sector
+=(READ_BUFFER_SIZE
/SECTOR_SIZE
);
703 cur_cmd
.count
-=MIN(cur_cmd
.count
,READ_BUFFER_SIZE
/SECTOR_SIZE
);
705 if(cur_cmd
.count
!=0) {
706 /* already read the next bit, so we can send it out immediately when the
707 * current transfer completes. */
708 #ifdef USB_USE_RAMDISK
709 memcpy(cur_cmd
.data
[cur_cmd
.data_select
],
710 ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
711 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
)*SECTOR_SIZE
);
713 cur_cmd
.last_result
= storage_read_sectors(cur_cmd
.lun
,
715 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
),
716 cur_cmd
.data
[cur_cmd
.data_select
]);
720 /****************************************************************************/
722 static void handle_scsi(struct command_block_wrapper
* cbw
)
724 /* USB Mass Storage assumes LBA capability.
725 TODO: support 48-bit LBA */
727 struct storage_info info
;
728 unsigned int length
= cbw
->data_transfer_length
;
729 unsigned int block_size
= 0;
730 unsigned int block_count
= 0;
731 bool lun_present
=true;
732 unsigned char lun
= cbw
->lun
;
733 unsigned int block_size_mult
= 1;
734 #ifdef HIDE_FIRST_DRIVE
738 storage_get_info(lun
,&info
);
739 #ifdef USB_USE_RAMDISK
740 block_size
= SECTOR_SIZE
;
741 block_count
= RAMDISK_SIZE
;
743 block_size
=info
.sector_size
;
744 block_count
=info
.num_sectors
;
748 if(storage_removable(lun
) && !storage_present(lun
)) {
756 #ifdef MAX_LOG_SECTOR_SIZE
757 block_size_mult
= disk_sector_multiplier
;
760 cur_cmd
.tag
= cbw
->tag
;
762 cur_cmd
.cur_cmd
= cbw
->command_block
[0];
764 switch (cbw
->command_block
[0]) {
765 case SCSI_TEST_UNIT_READY
:
766 logf("scsi test_unit_ready %d",lun
);
767 if(!usb_exclusive_storage()) {
768 send_csw(UMS_STATUS_FAIL
);
769 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
770 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
771 cur_sense_data
.ascq
=0;
775 send_csw(UMS_STATUS_GOOD
);
778 send_csw(UMS_STATUS_FAIL
);
779 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
780 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
781 cur_sense_data
.ascq
=0;
785 case SCSI_REPORT_LUNS
: {
786 logf("scsi report luns %d",lun
);
787 int allocation_length
=0;
789 unsigned int response_length
= 8+8*storage_num_drives();
790 allocation_length
|=(cbw
->command_block
[6]<<24);
791 allocation_length
|=(cbw
->command_block
[7]<<16);
792 allocation_length
|=(cbw
->command_block
[8]<<8);
793 allocation_length
|=(cbw
->command_block
[9]);
794 memset(tb
.lun_data
,0,sizeof(struct report_lun_data
));
795 tb
.lun_data
->lun_list_length
=htobe32(8*storage_num_drives());
796 for(i
=0;i
<storage_num_drives();i
++)
799 if(storage_removable(i
))
800 tb
.lun_data
->luns
[i
][1]=1;
803 tb
.lun_data
->luns
[i
][1]=0;
805 send_command_result(tb
.lun_data
,
806 MIN(response_length
, length
));
811 logf("scsi inquiry %d",lun
);
812 fill_inquiry(IF_MD(lun
));
813 length
= MIN(length
, cbw
->command_block
[4]);
814 send_command_result(tb
.inquiry
,
815 MIN(sizeof(struct inquiry_data
), length
));
818 case SCSI_REQUEST_SENSE
: {
819 tb
.sense_data
->ResponseCode
=0x70;/*current error*/
820 tb
.sense_data
->Obsolete
=0;
821 tb
.sense_data
->fei_sensekey
=cur_sense_data
.sense_key
&0x0f;
822 tb
.sense_data
->Information
=cur_sense_data
.information
;
823 tb
.sense_data
->AdditionalSenseLength
=10;
824 tb
.sense_data
->CommandSpecificInformation
=0;
825 tb
.sense_data
->AdditionalSenseCode
=cur_sense_data
.asc
;
826 tb
.sense_data
->AdditionalSenseCodeQualifier
=cur_sense_data
.ascq
;
827 tb
.sense_data
->FieldReplaceableUnitCode
=0;
828 tb
.sense_data
->SKSV
=0;
829 tb
.sense_data
->SenseKeySpecific
=0;
830 logf("scsi request_sense %d",lun
);
831 send_command_result(tb
.sense_data
,
832 MIN(sizeof(struct sense_data
), length
));
836 case SCSI_MODE_SENSE_10
: {
838 send_command_failed_result();
839 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
840 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
841 cur_sense_data
.ascq
=0;
844 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
845 unsigned char page_code
= cbw
->command_block
[2] & 0x3f;
846 logf("scsi mode_sense_10 %d %X",lun
,page_code
);
849 tb
.ms_data_10
->mode_data_length
=
850 htobe16(sizeof(struct mode_sense_data_10
)-2);
851 tb
.ms_data_10
->medium_type
= 0;
852 tb
.ms_data_10
->device_specific
= 0;
853 tb
.ms_data_10
->reserved
= 0;
854 tb
.ms_data_10
->longlba
= 1;
855 tb
.ms_data_10
->block_descriptor_length
=
856 htobe16(sizeof(struct mode_sense_bdesc_longlba
));
858 memset(tb
.ms_data_10
->block_descriptor
.reserved
,0,4);
859 memset(tb
.ms_data_10
->block_descriptor
.num_blocks
,0,8);
861 tb
.ms_data_10
->block_descriptor
.num_blocks
[4] =
862 ((block_count
/block_size_mult
) & 0xff000000)>>24;
863 tb
.ms_data_10
->block_descriptor
.num_blocks
[5] =
864 ((block_count
/block_size_mult
) & 0x00ff0000)>>16;
865 tb
.ms_data_10
->block_descriptor
.num_blocks
[6] =
866 ((block_count
/block_size_mult
) & 0x0000ff00)>>8;
867 tb
.ms_data_10
->block_descriptor
.num_blocks
[7] =
868 ((block_count
/block_size_mult
) & 0x000000ff);
870 tb
.ms_data_10
->block_descriptor
.block_size
[0] =
871 ((block_size
*block_size_mult
) & 0xff000000)>>24;
872 tb
.ms_data_10
->block_descriptor
.block_size
[1] =
873 ((block_size
*block_size_mult
) & 0x00ff0000)>>16;
874 tb
.ms_data_10
->block_descriptor
.block_size
[2] =
875 ((block_size
*block_size_mult
) & 0x0000ff00)>>8;
876 tb
.ms_data_10
->block_descriptor
.block_size
[3] =
877 ((block_size
*block_size_mult
) & 0x000000ff);
878 send_command_result(tb
.ms_data_10
,
879 MIN(sizeof(struct mode_sense_data_10
), length
));
882 send_command_failed_result();
883 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
884 cur_sense_data
.asc
=ASC_INVALID_FIELD_IN_CBD
;
885 cur_sense_data
.ascq
=0;
891 case SCSI_MODE_SENSE_6
: {
893 send_command_failed_result();
894 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
895 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
896 cur_sense_data
.ascq
=0;
899 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
900 unsigned char page_code
= cbw
->command_block
[2] & 0x3f;
901 logf("scsi mode_sense_6 %d %X",lun
,page_code
);
904 /* All supported pages. */
905 tb
.ms_data_6
->mode_data_length
=
906 sizeof(struct mode_sense_data_6
)-1;
907 tb
.ms_data_6
->medium_type
= 0;
908 tb
.ms_data_6
->device_specific
= 0;
909 tb
.ms_data_6
->block_descriptor_length
=
910 sizeof(struct mode_sense_bdesc_shortlba
);
911 tb
.ms_data_6
->block_descriptor
.density_code
= 0;
912 tb
.ms_data_6
->block_descriptor
.reserved
= 0;
913 if(block_count
/block_size_mult
> 0xffffff) {
914 tb
.ms_data_6
->block_descriptor
.num_blocks
[0] = 0xff;
915 tb
.ms_data_6
->block_descriptor
.num_blocks
[1] = 0xff;
916 tb
.ms_data_6
->block_descriptor
.num_blocks
[2] = 0xff;
919 tb
.ms_data_6
->block_descriptor
.num_blocks
[0] =
920 ((block_count
/block_size_mult
) & 0xff0000)>>16;
921 tb
.ms_data_6
->block_descriptor
.num_blocks
[1] =
922 ((block_count
/block_size_mult
) & 0x00ff00)>>8;
923 tb
.ms_data_6
->block_descriptor
.num_blocks
[2] =
924 ((block_count
/block_size_mult
) & 0x0000ff);
926 tb
.ms_data_6
->block_descriptor
.block_size
[0] =
927 ((block_size
*block_size_mult
) & 0xff0000)>>16;
928 tb
.ms_data_6
->block_descriptor
.block_size
[1] =
929 ((block_size
*block_size_mult
) & 0x00ff00)>>8;
930 tb
.ms_data_6
->block_descriptor
.block_size
[2] =
931 ((block_size
*block_size_mult
) & 0x0000ff);
932 send_command_result(tb
.ms_data_6
,
933 MIN(sizeof(struct mode_sense_data_6
), length
));
936 send_command_failed_result();
937 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
938 cur_sense_data
.asc
=ASC_INVALID_FIELD_IN_CBD
;
939 cur_sense_data
.ascq
=0;
945 case SCSI_START_STOP_UNIT
:
946 logf("scsi start_stop unit %d",lun
);
947 if((cbw
->command_block
[4] & 0xf0) == 0) /*load/eject bit is valid*/
948 { /* Process start and eject bits */
949 logf("scsi load/eject");
950 if((cbw
->command_block
[4] & 0x01) == 0) /* Don't start */
952 if((cbw
->command_block
[4] & 0x02) != 0) /* eject */
959 send_csw(UMS_STATUS_GOOD
);
962 case SCSI_ALLOW_MEDIUM_REMOVAL
:
963 logf("scsi allow_medium_removal %d",lun
);
964 if((cbw
->command_block
[4] & 0x03) == 0) {
966 queue_broadcast(SYS_USB_LUN_LOCKED
, (lun
<<16)+0);
970 queue_broadcast(SYS_USB_LUN_LOCKED
, (lun
<<16)+1);
972 send_csw(UMS_STATUS_GOOD
);
975 case SCSI_READ_FORMAT_CAPACITY
: {
976 logf("scsi read_format_capacity %d",lun
);
978 tb
.format_capacity_data
->following_length
=htobe32(8);
979 /* "block count" actually means "number of last block" */
980 tb
.format_capacity_data
->block_count
=
981 htobe32(block_count
/block_size_mult
- 1);
982 tb
.format_capacity_data
->block_size
=
983 htobe32(block_size
*block_size_mult
);
984 tb
.format_capacity_data
->block_size
|=
985 htobe32(SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA
);
987 send_command_result(tb
.format_capacity_data
,
988 MIN(sizeof(struct format_capacity
), length
));
991 send_command_failed_result();
992 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
993 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
994 cur_sense_data
.ascq
=0;
999 case SCSI_READ_CAPACITY
: {
1000 logf("scsi read_capacity %d",lun
);
1003 /* "block count" actually means "number of last block" */
1004 tb
.capacity_data
->block_count
=
1005 htobe32(block_count
/block_size_mult
- 1);
1006 tb
.capacity_data
->block_size
=
1007 htobe32(block_size
*block_size_mult
);
1009 send_command_result(tb
.capacity_data
,
1010 MIN(sizeof(struct capacity
), length
));
1013 send_command_failed_result();
1014 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1015 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1016 cur_sense_data
.ascq
=0;
1022 logf("scsi read10 %d",lun
);
1024 send_command_failed_result();
1025 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1026 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1027 cur_sense_data
.ascq
=0;
1030 cur_cmd
.data
[0] = tb
.transfer_buffer
;
1031 cur_cmd
.data
[1] = &tb
.transfer_buffer
[READ_BUFFER_SIZE
];
1032 cur_cmd
.data_select
=0;
1033 cur_cmd
.sector
= block_size_mult
*
1034 (cbw
->command_block
[2] << 24 |
1035 cbw
->command_block
[3] << 16 |
1036 cbw
->command_block
[4] << 8 |
1037 cbw
->command_block
[5] );
1038 cur_cmd
.count
= block_size_mult
*
1039 (cbw
->command_block
[7] << 8 |
1040 cbw
->command_block
[8]);
1041 cur_cmd
.orig_count
= cur_cmd
.count
;
1043 //logf("scsi read %d %d", cur_cmd.sector, cur_cmd.count);
1045 if((cur_cmd
.sector
+ cur_cmd
.count
) > block_count
) {
1046 send_csw(UMS_STATUS_FAIL
);
1047 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1048 cur_sense_data
.asc
=ASC_LBA_OUT_OF_RANGE
;
1049 cur_sense_data
.ascq
=0;
1052 #ifdef USB_USE_RAMDISK
1053 memcpy(cur_cmd
.data
[cur_cmd
.data_select
],
1054 ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
1055 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
,cur_cmd
.count
)*SECTOR_SIZE
);
1057 cur_cmd
.last_result
= storage_read_sectors(cur_cmd
.lun
,
1059 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
),
1060 cur_cmd
.data
[cur_cmd
.data_select
]);
1062 #ifdef TOSHIBA_GIGABEAT_S
1063 if(cur_cmd
.sector
== 0) {
1064 fix_mbr(cur_cmd
.data
[cur_cmd
.data_select
]);
1068 send_and_read_next();
1073 logf("scsi write10 %d",lun
);
1075 send_csw(UMS_STATUS_FAIL
);
1076 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1077 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1078 cur_sense_data
.ascq
=0;
1081 cur_cmd
.data
[0] = tb
.transfer_buffer
;
1082 cur_cmd
.data
[1] = &tb
.transfer_buffer
[WRITE_BUFFER_SIZE
];
1083 cur_cmd
.data_select
=0;
1084 cur_cmd
.sector
= block_size_mult
*
1085 (cbw
->command_block
[2] << 24 |
1086 cbw
->command_block
[3] << 16 |
1087 cbw
->command_block
[4] << 8 |
1088 cbw
->command_block
[5] );
1089 cur_cmd
.count
= block_size_mult
*
1090 (cbw
->command_block
[7] << 8 |
1091 cbw
->command_block
[8]);
1092 cur_cmd
.orig_count
= cur_cmd
.count
;
1095 if((cur_cmd
.sector
+ cur_cmd
.count
) > block_count
) {
1096 send_csw(UMS_STATUS_FAIL
);
1097 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1098 cur_sense_data
.asc
=ASC_LBA_OUT_OF_RANGE
;
1099 cur_sense_data
.ascq
=0;
1102 receive_block_data(cur_cmd
.data
[0],
1103 MIN(WRITE_BUFFER_SIZE
, cur_cmd
.count
*SECTOR_SIZE
));
1107 case SCSI_WRITE_BUFFER
:
1108 if(cbw
->command_block
[1]==1 /* mode = vendor specific */
1109 && cbw
->command_block
[2]==0 /* buffer id = 0 */
1111 && cbw
->command_block
[3]==0x0c /* offset (3 bytes) */
1112 && cbw
->command_block
[4]==0
1113 && cbw
->command_block
[5]==0
1115 /* Some versions of itunes set the parameter list length to 0.
1116 * Technically it should be 0x0c, which is what libgpod sends */
1117 && cbw
->command_block
[6]==0 /* parameter list (3 bytes) */
1118 && cbw
->command_block
[7]==0
1119 && (cbw
->command_block
[8]==0 || cbw
->command_block
[8]==0x0c)
1121 && cbw
->command_block
[9]==0)
1126 logf("scsi unknown cmd %x",cbw
->command_block
[0x0]);
1127 send_csw(UMS_STATUS_FAIL
);
1128 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1129 cur_sense_data
.asc
=ASC_INVALID_COMMAND
;
1130 cur_sense_data
.ascq
=0;
1135 static void send_block_data(void *data
,int size
)
1137 usb_drv_send_nonblocking(ep_in
, data
,size
);
1138 state
= SENDING_BLOCKS
;
1141 static void send_command_result(void *data
,int size
)
1143 usb_drv_send_nonblocking(ep_in
, data
,size
);
1144 state
= SENDING_RESULT
;
1147 static void send_command_failed_result(void)
1149 usb_drv_send_nonblocking(ep_in
, NULL
, 0);
1150 state
= SENDING_FAILED_RESULT
;
1153 static void receive_time(void)
1155 usb_drv_recv(ep_out
, tb
.transfer_buffer
, 12);
1156 state
= RECEIVING_TIME
;
1158 static void receive_block_data(void *data
,int size
)
1160 usb_drv_recv(ep_out
, data
, size
);
1161 state
= RECEIVING_BLOCKS
;
1164 static void send_csw(int status
)
1166 tb
.csw
->signature
= htole32(CSW_SIGNATURE
);
1167 tb
.csw
->tag
= cur_cmd
.tag
;
1168 tb
.csw
->data_residue
= 0;
1169 tb
.csw
->status
= status
;
1171 usb_drv_send_nonblocking(ep_in
, tb
.csw
,
1172 sizeof(struct command_status_wrapper
));
1173 state
= SENDING_CSW
;
1174 //logf("CSW: %X",status);
1175 /* Already start waiting for the next command */
1176 usb_drv_recv(ep_out
, cbw_buffer
, 1024);
1178 if(status
== UMS_STATUS_GOOD
) {
1179 cur_sense_data
.sense_key
=0;
1180 cur_sense_data
.information
=0;
1181 cur_sense_data
.asc
=0;
1182 cur_sense_data
.ascq
=0;
1186 static void copy_padded(char *dest
, char *src
, int len
)
1189 while(src
[i
]!=0 && i
<len
)
1201 /* build SCSI INQUIRY */
1202 static void fill_inquiry(IF_MD_NONVOID(int lun
))
1204 struct storage_info info
;
1205 memset(tb
.inquiry
, 0, sizeof(struct inquiry_data
));
1206 storage_get_info(lun
,&info
);
1207 copy_padded(tb
.inquiry
->VendorId
,info
.vendor
,sizeof(tb
.inquiry
->VendorId
));
1208 copy_padded(tb
.inquiry
->ProductId
,info
.product
,sizeof(tb
.inquiry
->ProductId
));
1209 copy_padded(tb
.inquiry
->ProductRevisionLevel
,info
.revision
,sizeof(tb
.inquiry
->ProductRevisionLevel
));
1211 tb
.inquiry
->DeviceType
= DIRECT_ACCESS_DEVICE
;
1212 tb
.inquiry
->AdditionalLength
= 0x1f;
1213 memset(tb
.inquiry
->Reserved
, 0, 3);
1214 tb
.inquiry
->Versions
= 4; /* SPC-2 */
1215 tb
.inquiry
->Format
= 2; /* SPC-2/3 inquiry format */
1217 #ifdef TOSHIBA_GIGABEAT_S
1218 tb
.inquiry
->DeviceTypeModifier
= 0;
1220 tb
.inquiry
->DeviceTypeModifier
= DEVICE_REMOVABLE
;