Clean up the way empty transfers are sent in error cases.
[kugel-rb.git] / firmware / usbstack / usb_storage.c
blob1f7069dfe2f344038d0fbd7701e31e81c530bca1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: $
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 ****************************************************************************/
19 #include "string.h"
20 #include "system.h"
21 #include "usb_core.h"
22 #include "usb_drv.h"
23 //#define LOGF_ENABLE
24 #include "logf.h"
25 #include "ata.h"
26 #include "hotswap.h"
27 #include "disk.h"
28 /* Needed to get at the audio buffer */
29 #include "audio.h"
32 #ifdef USB_STORAGE
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,
101 .bNumEndpoints = 2,
102 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
103 .bInterfaceSubClass = USB_SC_SCSI,
104 .bInterfaceProtocol = USB_PROT_BULK,
105 .iInterface = 0
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,
115 .wMaxPacketSize = 0,
116 .bInterval = 0
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];
136 #ifdef HAVE_HOTSWAP
137 unsigned char lun1[8];
138 #endif
139 } __attribute__ ((packed));
141 struct sense_data {
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;
151 unsigned char SKSV;
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;
188 unsigned int tag;
189 unsigned int data_transfer_length;
190 unsigned char flags;
191 unsigned char lun;
192 unsigned char command_length;
193 unsigned char command_block[16];
194 } __attribute__ ((packed));
196 struct command_status_wrapper {
197 unsigned int signature;
198 unsigned int tag;
199 unsigned int data_residue;
200 unsigned char status;
201 } __attribute__ ((packed));
203 struct capacity {
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));
215 static union {
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;
225 char *max_lun;
226 } tb;
228 static struct {
229 unsigned int sector;
230 unsigned int count;
231 unsigned int tag;
232 unsigned int lun;
233 unsigned char *data[2];
234 unsigned char data_select;
235 unsigned int last_result;
236 } cur_cmd;
238 static struct {
239 unsigned char sense_key;
240 unsigned char information;
241 unsigned char asc;
242 unsigned char ascq;
243 } cur_sense_data;
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;
258 static enum {
259 WAITING_FOR_COMMAND,
260 SENDING_BLOCKS,
261 SENDING_RESULT,
262 SENDING_FAILED_RESULT,
263 RECEIVING_BLOCKS,
264 SENDING_CSW
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;
278 int i;
279 for(i=0;i<NUM_VOLUMES;i++) {
280 if(ejected[i]==false){
281 canrelease=false;
282 break;
285 if(canrelease) {
286 logf("scsi release ata");
287 usb_release_exclusive_ata();
291 #ifdef HAVE_HOTSWAP
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;
298 else {
299 ejected[volume] = true;
300 try_release_ata();
304 #endif
306 void usb_storage_reconnect(void)
308 int i;
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)
318 int i;
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)
352 size_t bufsize;
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);
364 tb.transfer_buffer =
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);
375 switch(state) {
376 case RECEIVING_BLOCKS:
377 if(in==true) {
378 logf("IN received in RECEIVING");
380 logf("scsi write %d %d", cur_cmd.sector, cur_cmd.count);
381 if(status==0) {
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);
392 if(next_count!=0) {
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,)
401 cur_cmd.sector,
402 MIN(BUFFER_SIZE/SECTOR_SIZE,
403 cur_cmd.count),
404 cur_cmd.data[cur_cmd.data_select]);
405 if(result != 0) {
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;
410 break;
413 if(next_count==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;
424 else {
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;
433 break;
434 case WAITING_FOR_COMMAND:
435 if(in==true) {
436 logf("IN received in WAITING_FOR_COMMAND");
438 //logf("command received");
439 if(letoh32(cbw->signature) == CBW_SIGNATURE){
440 handle_scsi(cbw);
442 else {
443 usb_drv_stall(usb_endpoint, true,true);
444 usb_drv_stall(usb_endpoint, true,false);
446 break;
447 case SENDING_CSW:
448 if(in==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);
454 break;
455 case SENDING_RESULT:
456 if(in==false) {
457 logf("OUT received in SENDING");
459 if(status==0) {
460 //logf("data sent, now send csw");
461 send_csw(UMS_STATUS_GOOD);
463 else {
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;
472 break;
473 case SENDING_FAILED_RESULT:
474 if(in==false) {
475 logf("OUT received in SENDING");
477 send_csw(UMS_STATUS_FAIL);
478 break;
479 case SENDING_BLOCKS:
480 if(in==false) {
481 logf("OUT received in SENDING");
483 if(status==0) {
484 if(cur_cmd.count==0) {
485 //logf("data sent, now send csw");
486 send_csw(UMS_STATUS_GOOD);
488 else {
489 send_and_read_next();
492 else {
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;
501 break;
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
514 *tb.max_lun = 0;
515 #else
516 *tb.max_lun = NUM_VOLUMES - 1;
517 #endif
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 */
521 handled = true;
522 break;
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. */
531 #if 0
532 usb_drv_reset_endpoint(usb_endpoint, false);
533 usb_drv_reset_endpoint(usb_endpoint, true);
534 #endif
536 usb_drv_send(EP_CONTROL, NULL, 0); /* ack */
537 handled = true;
538 break;
541 return handled;
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;
552 return;
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,)
567 cur_cmd.sector,
568 MIN(BUFFER_SIZE/SECTOR_SIZE,
569 cur_cmd.count),
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;
586 #else
587 unsigned char lun = cbw->lun;
588 #endif
589 unsigned int block_size_mult = 1;
590 #ifdef HAVE_HOTSWAP
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;
596 else {
597 ejected[lun] = true;
598 try_release_ata();
600 #else
601 unsigned short* identify = ata_get_identify();
602 block_size = SECTOR_SIZE;
603 block_count = (identify[61] << 16 | identify[60]);
604 #endif
606 if(ejected[lun])
607 lun_present = false;
609 #ifdef MAX_LOG_SECTOR_SIZE
610 block_size_mult = disk_sector_multiplier;
611 #endif
613 cur_cmd.tag = cbw->tag;
614 cur_cmd.lun = lun;
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;
624 break;
626 if(lun_present) {
627 send_csw(UMS_STATUS_GOOD);
629 else {
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;
635 break;
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));
645 #ifdef HAVE_HOTSWAP
646 tb.lun_data->lun_list_length=htobe32(16);
647 tb.lun_data->lun1[1]=1;
648 #else
649 tb.lun_data->lun_list_length=htobe32(8);
650 #endif
651 tb.lun_data->lun0[1]=0;
653 send_command_result(tb.lun_data,
654 MIN(sizeof(struct report_lun_data), length));
655 break;
658 case SCSI_INQUIRY:
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));
664 break;
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));
680 break;
683 case SCSI_MODE_SENSE_10: {
684 if(! lun_present) {
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;
689 break;
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);
694 switch(page_code) {
695 case 0x3f:
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));
727 break;
728 default:
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;
733 break;
735 break;
737 case SCSI_MODE_SENSE_6: {
738 if(! lun_present) {
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;
743 break;
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);
748 switch(page_code) {
749 case 0x3f:
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;
764 else {
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));
780 break;
781 default:
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;
786 break;
788 break;
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 */
800 logf("scsi eject");
801 ejected[lun]=true;
802 try_release_ata();
806 send_csw(UMS_STATUS_GOOD);
807 break;
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);
813 break;
814 case SCSI_READ_FORMAT_CAPACITY: {
815 logf("scsi read_format_capacity %d",lun);
816 if(lun_present) {
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));
829 else
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;
836 break;
838 case SCSI_READ_CAPACITY: {
839 logf("scsi read_capacity %d",lun);
841 if(lun_present) {
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));
851 else
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;
858 break;
861 case SCSI_READ_10:
862 logf("scsi read10 %d",lun);
863 if(! lun_present) {
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;
868 break;
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;
890 else {
891 cur_cmd.last_result = ata_read_sectors(IF_MV2(cur_cmd.lun,)
892 cur_cmd.sector,
893 MIN(BUFFER_SIZE/SECTOR_SIZE,
894 cur_cmd.count),
895 cur_cmd.data[cur_cmd.data_select]);
896 send_and_read_next();
898 break;
900 case SCSI_WRITE_10:
901 logf("scsi write10 %d",lun);
902 if(! lun_present) {
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;
907 break;
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]);
920 /* expect data */
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;
927 else {
928 receive_block_data(cur_cmd.data[0],
929 MIN(BUFFER_SIZE,
930 cur_cmd.count*SECTOR_SIZE));
933 break;
935 default:
936 logf("scsi unknown cmd %x",cbw->command_block[0x0]);
937 usb_drv_stall(usb_endpoint, true,true);
938 send_csw(UMS_STATUS_FAIL);
939 break;
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));
976 state = SENDING_CSW;
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
991 if(lun==0) {
992 memcpy(&tb.inquiry->VendorId,"Rockbox ",8);
993 memcpy(&tb.inquiry->ProductId,"Internal Storage",16);
994 memcpy(&tb.inquiry->ProductRevisionLevel,"0.00",4);
996 else {
997 memcpy(&tb.inquiry->VendorId,"Rockbox ",8);
998 memcpy(&tb.inquiry->ProductId,"SD Card Slot ",16);
999 memcpy(&tb.inquiry->ProductRevisionLevel,"0.00",4);
1001 #else
1002 unsigned int i;
1003 unsigned short* dest;
1004 unsigned short* src;
1005 unsigned short* identify = ata_get_identify();
1006 (void)lun;
1007 memset(tb.inquiry, 0, sizeof(struct inquiry_data));
1009 #if 0
1010 if (identify[82] & 4)
1011 tb.inquiry->DeviceTypeModifier = DEVICE_REMOVABLE;
1012 #endif
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;
1019 for (i=0;i<12;i++)
1020 dest[i] = htobe16(src[i]);
1022 src = (unsigned short*)&identify[23];
1023 dest = (unsigned short*)&tb.inquiry->ProductRevisionLevel;
1024 for (i=0;i<2;i++)
1025 dest[i] = htobe16(src[i]);
1026 #endif
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 */
1034 #if 0
1035 #ifdef HAVE_HOTSWAP
1036 if(lun>0)
1037 tb.inquiry->DeviceTypeModifier = DEVICE_REMOVABLE;
1038 #endif
1039 #endif
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 */