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*/
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
48 #define SECTOR_SIZE 512
51 /* the ARC driver currently supports up to 64k USB transfers. This is
52 * enough for efficient mass storage support, as commonly host OSes
53 * don't do larger SCSI transfers anyway, so larger USB transfers
54 * wouldn't buy us anything.
55 * Due to being the double-buffering system used, using a smaller write buffer
56 * ends up being more efficient. Measurements have shown that 24k to 28k is
57 * optimal, except for sd devices that apparently don't gain anything from
60 #define READ_BUFFER_SIZE (1024*64)
62 #if (CONFIG_STORAGE & STORAGE_SD)
63 #define WRITE_BUFFER_SIZE (1024*64)
65 #define WRITE_BUFFER_SIZE (1024*24)
68 #define ALLOCATE_BUFFER_SIZE (2*MAX(READ_BUFFER_SIZE,WRITE_BUFFER_SIZE))
70 /* bulk-only class specific requests */
71 #define USB_BULK_RESET_REQUEST 0xff
72 #define USB_BULK_GET_MAX_LUN 0xfe
74 #define DIRECT_ACCESS_DEVICE 0x00 /* disks */
75 #define DEVICE_REMOVABLE 0x80
77 #define CBW_SIGNATURE 0x43425355
78 #define CSW_SIGNATURE 0x53425355
80 #define SCSI_TEST_UNIT_READY 0x00
81 #define SCSI_INQUIRY 0x12
82 #define SCSI_MODE_SENSE_6 0x1a
83 #define SCSI_MODE_SENSE_10 0x5a
84 #define SCSI_REQUEST_SENSE 0x03
85 #define SCSI_ALLOW_MEDIUM_REMOVAL 0x1e
86 #define SCSI_READ_CAPACITY 0x25
87 #define SCSI_READ_FORMAT_CAPACITY 0x23
88 #define SCSI_READ_10 0x28
89 #define SCSI_WRITE_10 0x2a
90 #define SCSI_START_STOP_UNIT 0x1b
91 #define SCSI_REPORT_LUNS 0xa0
92 #define SCSI_WRITE_BUFFER 0x3b
94 #define UMS_STATUS_GOOD 0x00
95 #define UMS_STATUS_FAIL 0x01
97 #define SENSE_NOT_READY 0x02
98 #define SENSE_MEDIUM_ERROR 0x03
99 #define SENSE_ILLEGAL_REQUEST 0x05
100 #define SENSE_UNIT_ATTENTION 0x06
102 #define ASC_MEDIUM_NOT_PRESENT 0x3a
103 #define ASC_INVALID_FIELD_IN_CBD 0x24
104 #define ASC_LBA_OUT_OF_RANGE 0x21
105 #define ASC_WRITE_ERROR 0x0C
106 #define ASC_READ_ERROR 0x11
107 #define ASC_NOT_READY 0x04
108 #define ASC_INVALID_COMMAND 0x20
110 #define ASCQ_BECOMING_READY 0x01
112 #define SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA 0x02000000
114 /* storage interface */
116 #define USB_SC_SCSI 0x06 /* Transparent */
117 #define USB_PROT_BULK 0x50 /* bulk only */
119 static struct usb_interface_descriptor
__attribute__((aligned(2)))
120 interface_descriptor
=
122 .bLength
= sizeof(struct usb_interface_descriptor
),
123 .bDescriptorType
= USB_DT_INTERFACE
,
124 .bInterfaceNumber
= 0,
125 .bAlternateSetting
= 0,
127 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
128 .bInterfaceSubClass
= USB_SC_SCSI
,
129 .bInterfaceProtocol
= USB_PROT_BULK
,
133 static struct usb_endpoint_descriptor
__attribute__((aligned(2)))
134 endpoint_descriptor
=
136 .bLength
= sizeof(struct usb_endpoint_descriptor
),
137 .bDescriptorType
= USB_DT_ENDPOINT
,
138 .bEndpointAddress
= 0,
139 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
144 struct inquiry_data
{
145 unsigned char DeviceType
;
146 unsigned char DeviceTypeModifier
;
147 unsigned char Versions
;
148 unsigned char Format
;
149 unsigned char AdditionalLength
;
150 unsigned char Reserved
[2];
151 unsigned char Capability
;
152 unsigned char VendorId
[8];
153 unsigned char ProductId
[16];
154 unsigned char ProductRevisionLevel
[4];
155 } __attribute__ ((packed
));
157 struct report_lun_data
{
158 unsigned int lun_list_length
;
159 unsigned int reserved1
;
160 unsigned char luns
[NUM_DRIVES
][8];
161 } __attribute__ ((packed
));
164 unsigned char ResponseCode
;
165 unsigned char Obsolete
;
166 unsigned char fei_sensekey
;
167 unsigned int Information
;
168 unsigned char AdditionalSenseLength
;
169 unsigned int CommandSpecificInformation
;
170 unsigned char AdditionalSenseCode
;
171 unsigned char AdditionalSenseCodeQualifier
;
172 unsigned char FieldReplaceableUnitCode
;
174 unsigned short SenseKeySpecific
;
175 } __attribute__ ((packed
));
177 struct mode_sense_bdesc_longlba
{
178 unsigned char num_blocks
[8];
179 unsigned char reserved
[4];
180 unsigned char block_size
[4];
181 } __attribute__ ((packed
));
183 struct mode_sense_bdesc_shortlba
{
184 unsigned char density_code
;
185 unsigned char num_blocks
[3];
186 unsigned char reserved
;
187 unsigned char block_size
[3];
188 } __attribute__ ((packed
));
190 struct mode_sense_data_10
{
191 unsigned short mode_data_length
;
192 unsigned char medium_type
;
193 unsigned char device_specific
;
194 unsigned char longlba
;
195 unsigned char reserved
;
196 unsigned short block_descriptor_length
;
197 struct mode_sense_bdesc_longlba block_descriptor
;
198 } __attribute__ ((packed
));
200 struct mode_sense_data_6
{
201 unsigned char mode_data_length
;
202 unsigned char medium_type
;
203 unsigned char device_specific
;
204 unsigned char block_descriptor_length
;
205 struct mode_sense_bdesc_shortlba block_descriptor
;
206 } __attribute__ ((packed
));
208 struct command_block_wrapper
{
209 unsigned int signature
;
211 unsigned int data_transfer_length
;
214 unsigned char command_length
;
215 unsigned char command_block
[16];
216 } __attribute__ ((packed
));
218 struct command_status_wrapper
{
219 unsigned int signature
;
221 unsigned int data_residue
;
222 unsigned char status
;
223 } __attribute__ ((packed
));
226 unsigned int block_count
;
227 unsigned int block_size
;
228 } __attribute__ ((packed
));
230 struct format_capacity
{
231 unsigned int following_length
;
232 unsigned int block_count
;
233 unsigned int block_size
;
234 } __attribute__ ((packed
));
238 unsigned char* transfer_buffer
;
239 struct inquiry_data
* inquiry
;
240 struct capacity
* capacity_data
;
241 struct format_capacity
* format_capacity_data
;
242 struct sense_data
*sense_data
;
243 struct mode_sense_data_6
*ms_data_6
;
244 struct mode_sense_data_10
*ms_data_10
;
245 struct report_lun_data
*lun_data
;
246 struct command_status_wrapper
* csw
;
250 static char *cbw_buffer
;
255 unsigned int orig_count
;
256 unsigned int cur_cmd
;
259 unsigned char *data
[2];
260 unsigned char data_select
;
261 unsigned int last_result
;
265 unsigned char sense_key
;
266 unsigned char information
;
271 static void handle_scsi(struct command_block_wrapper
* cbw
);
272 static void send_csw(int status
);
273 static void send_command_result(void *data
,int size
);
274 static void send_command_failed_result(void);
275 static void send_block_data(void *data
,int size
);
276 static void receive_block_data(void *data
,int size
);
277 static void receive_time(void);
278 static void fill_inquiry(IF_MD_NONVOID(int lun
));
279 static void send_and_read_next(void);
280 static bool ejected
[NUM_DRIVES
];
281 static bool locked
[NUM_DRIVES
];
283 static int usb_interface
;
284 static int ep_in
, ep_out
;
286 #ifdef USB_USE_RAMDISK
287 static unsigned char* ramdisk_buffer
;
294 SENDING_FAILED_RESULT
,
298 } state
= WAITING_FOR_COMMAND
;
300 static void yearday_to_daymonth(int yd
, int y
, int *d
, int *m
)
302 static const char tnl
[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
303 static const char tl
[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
307 if((y
%4 == 0 && y
%100 != 0) || y
%400 == 0)
316 while(yd
>= t
[i
] && i
<12)
325 #ifdef TOSHIBA_GIGABEAT_S
327 /* The Gigabeat S factory partition table contains invalid values for the
328 "active" flag in the MBR. This prevents at least the Linux kernel from
329 accepting the partition table, so we fix it on-the-fly. */
331 static void fix_mbr(unsigned char* mbr
)
333 unsigned char* p
= mbr
+ 0x1be;
342 static bool check_disk_present(IF_MD_NONVOID(int volume
))
344 #ifdef USB_USE_RAMDISK
347 unsigned char sector
[SECTOR_SIZE
];
348 return storage_read_sectors(volume
,0,1,sector
) == 0;
352 void usb_storage_try_release_storage(void)
354 /* Check if there is a connected drive left. If not,
355 release excusive access */
356 bool canrelease
=true;
358 for(i
=0;i
<storage_num_drives();i
++) {
359 if(ejected
[i
]==false && locked
[i
]==true) {
365 logf("scsi release ata");
366 usb_release_exclusive_storage();
371 void usb_storage_notify_hotswap(int volume
,bool inserted
)
373 logf("notify %d",inserted
);
374 if(inserted
&& check_disk_present(IF_MD(volume
))) {
375 ejected
[volume
] = false;
378 ejected
[volume
] = true;
379 /* If this happens while the device is locked, weird things may happen.
380 At least try to keep our state consistent */
381 locked
[volume
]=false;
386 /* called by usb_core_init() */
387 void usb_storage_init(void)
389 logf("usb_storage_init done");
392 int usb_storage_request_endpoints(struct usb_class_driver
*drv
)
394 ep_in
= usb_core_request_endpoint(USB_ENDPOINT_XFER_BULK
, USB_DIR_IN
, drv
);
399 ep_out
= usb_core_request_endpoint(USB_ENDPOINT_XFER_BULK
, USB_DIR_OUT
,
403 usb_core_release_endpoint(ep_in
);
410 int usb_storage_set_first_interface(int interface
)
412 usb_interface
= interface
;
413 return interface
+ 1;
416 int usb_storage_get_config_descriptor(unsigned char *dest
,int max_packet_size
)
418 unsigned char *orig_dest
= dest
;
420 interface_descriptor
.bInterfaceNumber
= usb_interface
;
421 PACK_DATA(dest
, interface_descriptor
);
423 endpoint_descriptor
.wMaxPacketSize
= max_packet_size
;
425 endpoint_descriptor
.bEndpointAddress
= ep_in
;
426 PACK_DATA(dest
, endpoint_descriptor
);
428 endpoint_descriptor
.bEndpointAddress
= ep_out
;
429 PACK_DATA(dest
, endpoint_descriptor
);
431 return (dest
- orig_dest
);
434 void usb_storage_init_connection(void)
436 logf("ums: set config");
437 /* prime rx endpoint. We only need room for commands */
438 state
= WAITING_FOR_COMMAND
;
440 #if CONFIG_CPU == IMX31L || defined(CPU_TCC77X) || defined(CPU_TCC780X) || \
441 defined(BOOTLOADER) || CONFIG_CPU == DM320
442 static unsigned char _cbw_buffer
[ALLOCATE_BUFFER_SIZE
]
443 USB_DEVBSS_ATTR
__attribute__((aligned(32)));
444 cbw_buffer
= (void *)_cbw_buffer
;
446 static unsigned char _transfer_buffer
[ALLOCATE_BUFFER_SIZE
]
447 USB_DEVBSS_ATTR
__attribute__((aligned(32)));
448 tb
.transfer_buffer
= (void *)_transfer_buffer
;
449 #ifdef USB_USE_RAMDISK
450 static unsigned char _ramdisk_buffer
[RAMDISK_SIZE
*SECTOR_SIZE
];
451 ramdisk_buffer
= _ramdisk_buffer
;
454 /* TODO : check if bufsize is at least 32K ? */
456 unsigned char * audio_buffer
;
458 audio_buffer
= audio_get_buffer(false,&bufsize
);
460 cbw_buffer
= (void *)UNCACHED_ADDR((unsigned int)(audio_buffer
+31) & 0xffffffe0);
462 cbw_buffer
= (void *)((unsigned int)(audio_buffer
+31) & 0xffffffe0);
464 tb
.transfer_buffer
= cbw_buffer
+ 1024;
465 cpucache_invalidate();
466 #ifdef USB_USE_RAMDISK
467 ramdisk_buffer
= tb
.transfer_buffer
+ ALLOCATE_BUFFER_SIZE
;
470 usb_drv_recv(ep_out
, cbw_buffer
, 1024);
473 for(i
=0;i
<storage_num_drives();i
++) {
474 #ifdef TOSHIBA_GIGABEAT_S
475 /* As long as the Gigabeat S is a non-removable device, we need
476 to mark the device as locked to avoid usb_storage_try_release_ata()
477 to leave MSC mode while the device is in use */
482 ejected
[i
] = !check_disk_present(IF_MD(i
));
483 queue_broadcast(SYS_USB_LUN_LOCKED
, (i
<<16)+0);
487 void usb_storage_disconnect(void)
492 /* called by usb_core_transfer_complete() */
493 void usb_storage_transfer_complete(int ep
,int dir
,int status
,int length
)
496 struct command_block_wrapper
* cbw
= (void*)cbw_buffer
;
499 //logf("transfer result %X %d", status, length);
501 case RECEIVING_BLOCKS
:
502 if(dir
==USB_DIR_IN
) {
503 logf("IN received in RECEIVING");
505 logf("scsi write %d %d", cur_cmd
.sector
, cur_cmd
.count
);
507 if((unsigned int)length
!=(SECTOR_SIZE
* cur_cmd
.count
)
508 && (unsigned int)length
!=WRITE_BUFFER_SIZE
) {
509 logf("unexpected length :%d",length
);
513 unsigned int next_sector
= cur_cmd
.sector
+
514 (WRITE_BUFFER_SIZE
/SECTOR_SIZE
);
515 unsigned int next_count
= cur_cmd
.count
-
516 MIN(cur_cmd
.count
,WRITE_BUFFER_SIZE
/SECTOR_SIZE
);
517 int next_select
= !cur_cmd
.data_select
;
520 /* Ask the host to send more, to the other buffer */
521 receive_block_data(cur_cmd
.data
[next_select
],
522 MIN(WRITE_BUFFER_SIZE
,next_count
*SECTOR_SIZE
));
525 /* Now write the data that just came in, while the host is
526 sending the next bit */
527 #ifdef USB_USE_RAMDISK
528 memcpy(ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
529 cur_cmd
.data
[cur_cmd
.data_select
],
530 MIN(WRITE_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
)*SECTOR_SIZE
);
532 int result
= storage_write_sectors(cur_cmd
.lun
,
534 MIN(WRITE_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
),
535 cur_cmd
.data
[cur_cmd
.data_select
]);
537 send_csw(UMS_STATUS_FAIL
);
538 cur_sense_data
.sense_key
=SENSE_MEDIUM_ERROR
;
539 cur_sense_data
.asc
=ASC_WRITE_ERROR
;
540 cur_sense_data
.ascq
=0;
545 send_csw(UMS_STATUS_GOOD
);
548 /* Switch buffers for the next one */
549 cur_cmd
.data_select
=!cur_cmd
.data_select
;
551 cur_cmd
.sector
= next_sector
;
552 cur_cmd
.count
= next_count
;
555 logf("Transfer failed %X",status
);
556 send_csw(UMS_STATUS_FAIL
);
557 /* TODO fill in cur_sense_data */
558 cur_sense_data
.sense_key
=0;
559 cur_sense_data
.information
=0;
560 cur_sense_data
.asc
=0;
561 cur_sense_data
.ascq
=0;
564 case WAITING_FOR_COMMAND
:
565 if(dir
==USB_DIR_IN
) {
566 logf("IN received in WAITING_FOR_COMMAND");
568 //logf("command received");
569 if(letoh32(cbw
->signature
) == CBW_SIGNATURE
) {
573 usb_drv_stall(ep_in
, true,true);
574 usb_drv_stall(ep_out
, true,false);
578 if(dir
==USB_DIR_OUT
) {
579 logf("OUT received in SENDING_CSW");
581 //logf("csw sent, now go back to idle");
582 state
= WAITING_FOR_COMMAND
;
584 if(cur_cmd
.cur_cmd
== SCSI_WRITE_10
)
586 queue_broadcast(SYS_USB_WRITE_DATA
, (cur_cmd
.lun
<<16)+cur_cmd
.orig_count
);
588 else if(cur_cmd
.cur_cmd
== SCSI_READ_10
)
590 queue_broadcast(SYS_USB_READ_DATA
, (cur_cmd
.lun
<<16)+cur_cmd
.orig_count
);
595 if(dir
==USB_DIR_OUT
) {
596 logf("OUT received in SENDING");
599 //logf("data sent, now send csw");
600 send_csw(UMS_STATUS_GOOD
);
603 logf("Transfer failed %X",status
);
604 send_csw(UMS_STATUS_FAIL
);
605 /* TODO fill in cur_sense_data */
606 cur_sense_data
.sense_key
=0;
607 cur_sense_data
.information
=0;
608 cur_sense_data
.asc
=0;
609 cur_sense_data
.ascq
=0;
612 case SENDING_FAILED_RESULT
:
613 if(dir
==USB_DIR_OUT
) {
614 logf("OUT received in SENDING");
616 send_csw(UMS_STATUS_FAIL
);
619 if(dir
==USB_DIR_OUT
) {
620 logf("OUT received in SENDING");
623 if(cur_cmd
.count
==0) {
624 //logf("data sent, now send csw");
625 send_csw(UMS_STATUS_GOOD
);
628 send_and_read_next();
632 logf("Transfer failed %X",status
);
633 send_csw(UMS_STATUS_FAIL
);
634 /* TODO fill in cur_sense_data */
635 cur_sense_data
.sense_key
=0;
636 cur_sense_data
.information
=0;
637 cur_sense_data
.asc
=0;
638 cur_sense_data
.ascq
=0;
642 tm
.tm_year
=(tb
.transfer_buffer
[0]<<8)+tb
.transfer_buffer
[1] - 1900;
643 tm
.tm_yday
=(tb
.transfer_buffer
[2]<<8)+tb
.transfer_buffer
[3];
644 tm
.tm_hour
=tb
.transfer_buffer
[5];
645 tm
.tm_min
=tb
.transfer_buffer
[6];
646 tm
.tm_sec
=tb
.transfer_buffer
[7];
647 yearday_to_daymonth(tm
.tm_yday
,tm
.tm_year
+ 1900,&tm
.tm_mday
,&tm
.tm_mon
);
648 set_day_of_week(&tm
);
650 send_csw(UMS_STATUS_GOOD
);
655 /* called by usb_core_control_request() */
656 bool usb_storage_control_request(struct usb_ctrlrequest
* req
, unsigned char* dest
)
658 bool handled
= false;
661 switch (req
->bRequest
) {
662 case USB_BULK_GET_MAX_LUN
: {
663 *tb
.max_lun
= storage_num_drives() - 1;
664 #ifdef HIDE_FIRST_DRIVE
667 logf("ums: getmaxlun");
668 usb_drv_recv(EP_CONTROL
, NULL
, 0); /* ack */
669 usb_drv_send(EP_CONTROL
, tb
.max_lun
, 1);
674 case USB_BULK_RESET_REQUEST
:
675 logf("ums: bulk reset");
676 state
= WAITING_FOR_COMMAND
;
677 /* UMS BOT 3.1 says The device shall preserve the value of its bulk
678 data toggle bits and endpoint STALL conditions despite
679 the Bulk-Only Mass Storage Reset. */
681 usb_drv_reset_endpoint(ep_in
, false);
682 usb_drv_reset_endpoint(ep_out
, true);
684 usb_drv_send(EP_CONTROL
, NULL
, 0); /* ack */
692 static void send_and_read_next(void)
694 if(cur_cmd
.last_result
!=0) {
695 /* The last read failed. */
696 send_csw(UMS_STATUS_FAIL
);
697 cur_sense_data
.sense_key
=SENSE_MEDIUM_ERROR
;
698 cur_sense_data
.asc
=ASC_READ_ERROR
;
699 cur_sense_data
.ascq
=0;
702 send_block_data(cur_cmd
.data
[cur_cmd
.data_select
],
703 MIN(READ_BUFFER_SIZE
,cur_cmd
.count
*SECTOR_SIZE
));
705 /* Switch buffers for the next one */
706 cur_cmd
.data_select
=!cur_cmd
.data_select
;
708 cur_cmd
.sector
+=(READ_BUFFER_SIZE
/SECTOR_SIZE
);
709 cur_cmd
.count
-=MIN(cur_cmd
.count
,READ_BUFFER_SIZE
/SECTOR_SIZE
);
711 if(cur_cmd
.count
!=0) {
712 /* already read the next bit, so we can send it out immediately when the
713 * current transfer completes. */
714 #ifdef USB_USE_RAMDISK
715 memcpy(cur_cmd
.data
[cur_cmd
.data_select
],
716 ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
717 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
)*SECTOR_SIZE
);
719 cur_cmd
.last_result
= storage_read_sectors(cur_cmd
.lun
,
721 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
),
722 cur_cmd
.data
[cur_cmd
.data_select
]);
726 /****************************************************************************/
728 static void handle_scsi(struct command_block_wrapper
* cbw
)
730 /* USB Mass Storage assumes LBA capability.
731 TODO: support 48-bit LBA */
733 struct storage_info info
;
734 unsigned int length
= cbw
->data_transfer_length
;
735 unsigned int block_size
= 0;
736 unsigned int block_count
= 0;
737 bool lun_present
=true;
738 unsigned char lun
= cbw
->lun
;
739 unsigned int block_size_mult
= 1;
740 #ifdef HIDE_FIRST_DRIVE
744 storage_get_info(lun
,&info
);
745 #ifdef USB_USE_RAMDISK
746 block_size
= SECTOR_SIZE
;
747 block_count
= RAMDISK_SIZE
;
749 block_size
=info
.sector_size
;
750 block_count
=info
.num_sectors
;
754 if(storage_removable(lun
) && !storage_present(lun
)) {
762 #ifdef MAX_LOG_SECTOR_SIZE
763 block_size_mult
= disk_sector_multiplier
;
766 cur_cmd
.tag
= cbw
->tag
;
768 cur_cmd
.cur_cmd
= cbw
->command_block
[0];
770 switch (cbw
->command_block
[0]) {
771 case SCSI_TEST_UNIT_READY
:
772 logf("scsi test_unit_ready %d",lun
);
773 if(!usb_exclusive_storage()) {
774 send_csw(UMS_STATUS_FAIL
);
775 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
776 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
777 cur_sense_data
.ascq
=0;
781 send_csw(UMS_STATUS_GOOD
);
784 send_csw(UMS_STATUS_FAIL
);
785 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
786 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
787 cur_sense_data
.ascq
=0;
791 case SCSI_REPORT_LUNS
: {
792 logf("scsi report luns %d",lun
);
793 int allocation_length
=0;
795 unsigned int response_length
= 8+8*storage_num_drives();
796 allocation_length
|=(cbw
->command_block
[6]<<24);
797 allocation_length
|=(cbw
->command_block
[7]<<16);
798 allocation_length
|=(cbw
->command_block
[8]<<8);
799 allocation_length
|=(cbw
->command_block
[9]);
800 memset(tb
.lun_data
,0,sizeof(struct report_lun_data
));
801 tb
.lun_data
->lun_list_length
=htobe32(8*storage_num_drives());
802 for(i
=0;i
<storage_num_drives();i
++)
805 if(storage_removable(i
))
806 tb
.lun_data
->luns
[i
][1]=1;
809 tb
.lun_data
->luns
[i
][1]=0;
811 send_command_result(tb
.lun_data
,
812 MIN(response_length
, length
));
817 logf("scsi inquiry %d",lun
);
818 fill_inquiry(IF_MD(lun
));
819 length
= MIN(length
, cbw
->command_block
[4]);
820 send_command_result(tb
.inquiry
,
821 MIN(sizeof(struct inquiry_data
), length
));
824 case SCSI_REQUEST_SENSE
: {
825 tb
.sense_data
->ResponseCode
=0x70;/*current error*/
826 tb
.sense_data
->Obsolete
=0;
827 tb
.sense_data
->fei_sensekey
=cur_sense_data
.sense_key
&0x0f;
828 tb
.sense_data
->Information
=cur_sense_data
.information
;
829 tb
.sense_data
->AdditionalSenseLength
=10;
830 tb
.sense_data
->CommandSpecificInformation
=0;
831 tb
.sense_data
->AdditionalSenseCode
=cur_sense_data
.asc
;
832 tb
.sense_data
->AdditionalSenseCodeQualifier
=cur_sense_data
.ascq
;
833 tb
.sense_data
->FieldReplaceableUnitCode
=0;
834 tb
.sense_data
->SKSV
=0;
835 tb
.sense_data
->SenseKeySpecific
=0;
836 logf("scsi request_sense %d",lun
);
837 send_command_result(tb
.sense_data
,
838 MIN(sizeof(struct sense_data
), length
));
842 case SCSI_MODE_SENSE_10
: {
844 send_command_failed_result();
845 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
846 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
847 cur_sense_data
.ascq
=0;
850 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
851 unsigned char page_code
= cbw
->command_block
[2] & 0x3f;
852 logf("scsi mode_sense_10 %d %X",lun
,page_code
);
855 tb
.ms_data_10
->mode_data_length
=
856 htobe16(sizeof(struct mode_sense_data_10
)-2);
857 tb
.ms_data_10
->medium_type
= 0;
858 tb
.ms_data_10
->device_specific
= 0;
859 tb
.ms_data_10
->reserved
= 0;
860 tb
.ms_data_10
->longlba
= 1;
861 tb
.ms_data_10
->block_descriptor_length
=
862 htobe16(sizeof(struct mode_sense_bdesc_longlba
));
864 memset(tb
.ms_data_10
->block_descriptor
.reserved
,0,4);
865 memset(tb
.ms_data_10
->block_descriptor
.num_blocks
,0,8);
867 tb
.ms_data_10
->block_descriptor
.num_blocks
[4] =
868 ((block_count
/block_size_mult
) & 0xff000000)>>24;
869 tb
.ms_data_10
->block_descriptor
.num_blocks
[5] =
870 ((block_count
/block_size_mult
) & 0x00ff0000)>>16;
871 tb
.ms_data_10
->block_descriptor
.num_blocks
[6] =
872 ((block_count
/block_size_mult
) & 0x0000ff00)>>8;
873 tb
.ms_data_10
->block_descriptor
.num_blocks
[7] =
874 ((block_count
/block_size_mult
) & 0x000000ff);
876 tb
.ms_data_10
->block_descriptor
.block_size
[0] =
877 ((block_size
*block_size_mult
) & 0xff000000)>>24;
878 tb
.ms_data_10
->block_descriptor
.block_size
[1] =
879 ((block_size
*block_size_mult
) & 0x00ff0000)>>16;
880 tb
.ms_data_10
->block_descriptor
.block_size
[2] =
881 ((block_size
*block_size_mult
) & 0x0000ff00)>>8;
882 tb
.ms_data_10
->block_descriptor
.block_size
[3] =
883 ((block_size
*block_size_mult
) & 0x000000ff);
884 send_command_result(tb
.ms_data_10
,
885 MIN(sizeof(struct mode_sense_data_10
), length
));
888 send_command_failed_result();
889 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
890 cur_sense_data
.asc
=ASC_INVALID_FIELD_IN_CBD
;
891 cur_sense_data
.ascq
=0;
897 case SCSI_MODE_SENSE_6
: {
899 send_command_failed_result();
900 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
901 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
902 cur_sense_data
.ascq
=0;
905 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
906 unsigned char page_code
= cbw
->command_block
[2] & 0x3f;
907 logf("scsi mode_sense_6 %d %X",lun
,page_code
);
910 /* All supported pages. */
911 tb
.ms_data_6
->mode_data_length
=
912 sizeof(struct mode_sense_data_6
)-1;
913 tb
.ms_data_6
->medium_type
= 0;
914 tb
.ms_data_6
->device_specific
= 0;
915 tb
.ms_data_6
->block_descriptor_length
=
916 sizeof(struct mode_sense_bdesc_shortlba
);
917 tb
.ms_data_6
->block_descriptor
.density_code
= 0;
918 tb
.ms_data_6
->block_descriptor
.reserved
= 0;
919 if(block_count
/block_size_mult
> 0xffffff) {
920 tb
.ms_data_6
->block_descriptor
.num_blocks
[0] = 0xff;
921 tb
.ms_data_6
->block_descriptor
.num_blocks
[1] = 0xff;
922 tb
.ms_data_6
->block_descriptor
.num_blocks
[2] = 0xff;
925 tb
.ms_data_6
->block_descriptor
.num_blocks
[0] =
926 ((block_count
/block_size_mult
) & 0xff0000)>>16;
927 tb
.ms_data_6
->block_descriptor
.num_blocks
[1] =
928 ((block_count
/block_size_mult
) & 0x00ff00)>>8;
929 tb
.ms_data_6
->block_descriptor
.num_blocks
[2] =
930 ((block_count
/block_size_mult
) & 0x0000ff);
932 tb
.ms_data_6
->block_descriptor
.block_size
[0] =
933 ((block_size
*block_size_mult
) & 0xff0000)>>16;
934 tb
.ms_data_6
->block_descriptor
.block_size
[1] =
935 ((block_size
*block_size_mult
) & 0x00ff00)>>8;
936 tb
.ms_data_6
->block_descriptor
.block_size
[2] =
937 ((block_size
*block_size_mult
) & 0x0000ff);
938 send_command_result(tb
.ms_data_6
,
939 MIN(sizeof(struct mode_sense_data_6
), length
));
942 send_command_failed_result();
943 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
944 cur_sense_data
.asc
=ASC_INVALID_FIELD_IN_CBD
;
945 cur_sense_data
.ascq
=0;
951 case SCSI_START_STOP_UNIT
:
952 logf("scsi start_stop unit %d",lun
);
953 if((cbw
->command_block
[4] & 0xf0) == 0) /*load/eject bit is valid*/
954 { /* Process start and eject bits */
955 logf("scsi load/eject");
956 if((cbw
->command_block
[4] & 0x01) == 0) /* Don't start */
958 if((cbw
->command_block
[4] & 0x02) != 0) /* eject */
965 send_csw(UMS_STATUS_GOOD
);
968 case SCSI_ALLOW_MEDIUM_REMOVAL
:
969 logf("scsi allow_medium_removal %d",lun
);
970 if((cbw
->command_block
[4] & 0x03) == 0) {
972 queue_broadcast(SYS_USB_LUN_LOCKED
, (lun
<<16)+0);
976 queue_broadcast(SYS_USB_LUN_LOCKED
, (lun
<<16)+1);
978 send_csw(UMS_STATUS_GOOD
);
981 case SCSI_READ_FORMAT_CAPACITY
: {
982 logf("scsi read_format_capacity %d",lun
);
984 tb
.format_capacity_data
->following_length
=htobe32(8);
985 /* "block count" actually means "number of last block" */
986 tb
.format_capacity_data
->block_count
=
987 htobe32(block_count
/block_size_mult
- 1);
988 tb
.format_capacity_data
->block_size
=
989 htobe32(block_size
*block_size_mult
);
990 tb
.format_capacity_data
->block_size
|=
991 htobe32(SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA
);
993 send_command_result(tb
.format_capacity_data
,
994 MIN(sizeof(struct format_capacity
), length
));
997 send_command_failed_result();
998 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
999 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1000 cur_sense_data
.ascq
=0;
1005 case SCSI_READ_CAPACITY
: {
1006 logf("scsi read_capacity %d",lun
);
1009 /* "block count" actually means "number of last block" */
1010 tb
.capacity_data
->block_count
=
1011 htobe32(block_count
/block_size_mult
- 1);
1012 tb
.capacity_data
->block_size
=
1013 htobe32(block_size
*block_size_mult
);
1015 send_command_result(tb
.capacity_data
,
1016 MIN(sizeof(struct capacity
), length
));
1019 send_command_failed_result();
1020 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1021 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1022 cur_sense_data
.ascq
=0;
1028 logf("scsi read10 %d",lun
);
1030 send_command_failed_result();
1031 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1032 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1033 cur_sense_data
.ascq
=0;
1036 cur_cmd
.data
[0] = tb
.transfer_buffer
;
1037 cur_cmd
.data
[1] = &tb
.transfer_buffer
[READ_BUFFER_SIZE
];
1038 cur_cmd
.data_select
=0;
1039 cur_cmd
.sector
= block_size_mult
*
1040 (cbw
->command_block
[2] << 24 |
1041 cbw
->command_block
[3] << 16 |
1042 cbw
->command_block
[4] << 8 |
1043 cbw
->command_block
[5] );
1044 cur_cmd
.count
= block_size_mult
*
1045 (cbw
->command_block
[7] << 8 |
1046 cbw
->command_block
[8]);
1047 cur_cmd
.orig_count
= cur_cmd
.count
;
1049 //logf("scsi read %d %d", cur_cmd.sector, cur_cmd.count);
1051 if((cur_cmd
.sector
+ cur_cmd
.count
) > block_count
) {
1052 send_csw(UMS_STATUS_FAIL
);
1053 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1054 cur_sense_data
.asc
=ASC_LBA_OUT_OF_RANGE
;
1055 cur_sense_data
.ascq
=0;
1058 #ifdef USB_USE_RAMDISK
1059 memcpy(cur_cmd
.data
[cur_cmd
.data_select
],
1060 ramdisk_buffer
+ cur_cmd
.sector
*SECTOR_SIZE
,
1061 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
,cur_cmd
.count
)*SECTOR_SIZE
);
1063 cur_cmd
.last_result
= storage_read_sectors(cur_cmd
.lun
,
1065 MIN(READ_BUFFER_SIZE
/SECTOR_SIZE
, cur_cmd
.count
),
1066 cur_cmd
.data
[cur_cmd
.data_select
]);
1068 #ifdef TOSHIBA_GIGABEAT_S
1069 if(cur_cmd
.sector
== 0) {
1070 fix_mbr(cur_cmd
.data
[cur_cmd
.data_select
]);
1074 send_and_read_next();
1079 logf("scsi write10 %d",lun
);
1081 send_csw(UMS_STATUS_FAIL
);
1082 cur_sense_data
.sense_key
=SENSE_NOT_READY
;
1083 cur_sense_data
.asc
=ASC_MEDIUM_NOT_PRESENT
;
1084 cur_sense_data
.ascq
=0;
1087 cur_cmd
.data
[0] = tb
.transfer_buffer
;
1088 cur_cmd
.data
[1] = &tb
.transfer_buffer
[WRITE_BUFFER_SIZE
];
1089 cur_cmd
.data_select
=0;
1090 cur_cmd
.sector
= block_size_mult
*
1091 (cbw
->command_block
[2] << 24 |
1092 cbw
->command_block
[3] << 16 |
1093 cbw
->command_block
[4] << 8 |
1094 cbw
->command_block
[5] );
1095 cur_cmd
.count
= block_size_mult
*
1096 (cbw
->command_block
[7] << 8 |
1097 cbw
->command_block
[8]);
1098 cur_cmd
.orig_count
= cur_cmd
.count
;
1101 if((cur_cmd
.sector
+ cur_cmd
.count
) > block_count
) {
1102 send_csw(UMS_STATUS_FAIL
);
1103 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1104 cur_sense_data
.asc
=ASC_LBA_OUT_OF_RANGE
;
1105 cur_sense_data
.ascq
=0;
1108 receive_block_data(cur_cmd
.data
[0],
1109 MIN(WRITE_BUFFER_SIZE
, cur_cmd
.count
*SECTOR_SIZE
));
1113 case SCSI_WRITE_BUFFER
:
1114 if(cbw
->command_block
[1]==1 /* mode = vendor specific */
1115 && cbw
->command_block
[2]==0 /* buffer id = 0 */
1117 && cbw
->command_block
[3]==0x0c /* offset (3 bytes) */
1118 && cbw
->command_block
[4]==0
1119 && cbw
->command_block
[5]==0
1121 /* Some versions of itunes set the parameter list length to 0.
1122 * Technically it should be 0x0c, which is what libgpod sends */
1123 && cbw
->command_block
[6]==0 /* parameter list (3 bytes) */
1124 && cbw
->command_block
[7]==0
1125 && (cbw
->command_block
[8]==0 || cbw
->command_block
[8]==0x0c)
1127 && cbw
->command_block
[9]==0)
1132 logf("scsi unknown cmd %x",cbw
->command_block
[0x0]);
1133 send_csw(UMS_STATUS_FAIL
);
1134 cur_sense_data
.sense_key
=SENSE_ILLEGAL_REQUEST
;
1135 cur_sense_data
.asc
=ASC_INVALID_COMMAND
;
1136 cur_sense_data
.ascq
=0;
1141 static void send_block_data(void *data
,int size
)
1143 usb_drv_send_nonblocking(ep_in
, data
,size
);
1144 state
= SENDING_BLOCKS
;
1147 static void send_command_result(void *data
,int size
)
1149 usb_drv_send_nonblocking(ep_in
, data
,size
);
1150 state
= SENDING_RESULT
;
1153 static void send_command_failed_result(void)
1155 usb_drv_send_nonblocking(ep_in
, NULL
, 0);
1156 state
= SENDING_FAILED_RESULT
;
1159 static void receive_time(void)
1161 usb_drv_recv(ep_out
, tb
.transfer_buffer
, 12);
1162 state
= RECEIVING_TIME
;
1164 static void receive_block_data(void *data
,int size
)
1166 usb_drv_recv(ep_out
, data
, size
);
1167 state
= RECEIVING_BLOCKS
;
1170 static void send_csw(int status
)
1172 tb
.csw
->signature
= htole32(CSW_SIGNATURE
);
1173 tb
.csw
->tag
= cur_cmd
.tag
;
1174 tb
.csw
->data_residue
= 0;
1175 tb
.csw
->status
= status
;
1177 usb_drv_send_nonblocking(ep_in
, tb
.csw
,
1178 sizeof(struct command_status_wrapper
));
1179 state
= SENDING_CSW
;
1180 //logf("CSW: %X",status);
1181 /* Already start waiting for the next command */
1182 usb_drv_recv(ep_out
, cbw_buffer
, 1024);
1184 if(status
== UMS_STATUS_GOOD
) {
1185 cur_sense_data
.sense_key
=0;
1186 cur_sense_data
.information
=0;
1187 cur_sense_data
.asc
=0;
1188 cur_sense_data
.ascq
=0;
1192 static void copy_padded(char *dest
, char *src
, int len
)
1195 while(src
[i
]!=0 && i
<len
)
1207 /* build SCSI INQUIRY */
1208 static void fill_inquiry(IF_MD_NONVOID(int lun
))
1210 struct storage_info info
;
1211 memset(tb
.inquiry
, 0, sizeof(struct inquiry_data
));
1212 storage_get_info(lun
,&info
);
1213 copy_padded(tb
.inquiry
->VendorId
,info
.vendor
,sizeof(tb
.inquiry
->VendorId
));
1214 copy_padded(tb
.inquiry
->ProductId
,info
.product
,sizeof(tb
.inquiry
->ProductId
));
1215 copy_padded(tb
.inquiry
->ProductRevisionLevel
,info
.revision
,sizeof(tb
.inquiry
->ProductRevisionLevel
));
1217 tb
.inquiry
->DeviceType
= DIRECT_ACCESS_DEVICE
;
1218 tb
.inquiry
->AdditionalLength
= 0x1f;
1219 memset(tb
.inquiry
->Reserved
, 0, 3);
1220 tb
.inquiry
->Versions
= 4; /* SPC-2 */
1221 tb
.inquiry
->Format
= 2; /* SPC-2/3 inquiry format */
1223 #ifdef TOSHIBA_GIGABEAT_S
1224 tb
.inquiry
->DeviceTypeModifier
= 0;
1226 tb
.inquiry
->DeviceTypeModifier
= DEVICE_REMOVABLE
;