Don't do overlapping USB transactions and SD writes. This seems to avoid FS#8663
[kugel-rb.git] / firmware / usbstack / usb_storage.c
blobf233549a2bd748a9ecbcd9c503f49783f135e908
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include "string.h"
22 #include "system.h"
23 #include "usb_core.h"
24 #include "usb_drv.h"
25 //#define LOGF_ENABLE
26 #include "logf.h"
27 #include "ata.h"
28 #include "hotswap.h"
29 #include "disk.h"
30 /* Needed to get at the audio buffer */
31 #include "audio.h"
32 #include "usb_storage.h"
35 #ifdef USB_STORAGE
37 /* The SD card driver on Sansa c200 and e200 can cause write corruption,
38 * often triggered by simultaneous USB activity. This can be largely avoided
39 * by not overlapping ata_write_sector() with USB transfers. This does reduce
40 * write performance, so we only do it for the affected DAPs
42 #if defined(SANSA_C200) || defined(SANSA_E200)
43 #define SERIALIZE_WRITES
44 #endif
45 /* Enable the following define to export only the SD card slot. This
46 * is useful for USBCV MSC tests, as those are destructive.
47 * This won't work right if the device doesn't have a card slot.
49 //#define ONLY_EXPOSE_CARD_SLOT
51 #define SECTOR_SIZE 512
53 /* We can currently use up to 20k buffer size. More than that requires
54 * transfer chaining in the driver. Tests on sansa c200 show that the 16k
55 * limitation causes no more than 2% slowdown.
57 #define BUFFER_SIZE 16384
59 /* bulk-only class specific requests */
60 #define USB_BULK_RESET_REQUEST 0xff
61 #define USB_BULK_GET_MAX_LUN 0xfe
63 #define DIRECT_ACCESS_DEVICE 0x00 /* disks */
64 #define DEVICE_REMOVABLE 0x80
66 #define CBW_SIGNATURE 0x43425355
67 #define CSW_SIGNATURE 0x53425355
69 #define SCSI_TEST_UNIT_READY 0x00
70 #define SCSI_INQUIRY 0x12
71 #define SCSI_MODE_SENSE_6 0x1a
72 #define SCSI_MODE_SENSE_10 0x5a
73 #define SCSI_REQUEST_SENSE 0x03
74 #define SCSI_ALLOW_MEDIUM_REMOVAL 0x1e
75 #define SCSI_READ_CAPACITY 0x25
76 #define SCSI_READ_FORMAT_CAPACITY 0x23
77 #define SCSI_READ_10 0x28
78 #define SCSI_WRITE_10 0x2a
79 #define SCSI_START_STOP_UNIT 0x1b
80 #define SCSI_REPORT_LUNS 0xa0
82 #define UMS_STATUS_GOOD 0x00
83 #define UMS_STATUS_FAIL 0x01
85 #define SENSE_NOT_READY 0x02
86 #define SENSE_MEDIUM_ERROR 0x03
87 #define SENSE_ILLEGAL_REQUEST 0x05
88 #define SENSE_UNIT_ATTENTION 0x06
90 #define ASC_MEDIUM_NOT_PRESENT 0x3a
91 #define ASC_INVALID_FIELD_IN_CBD 0x24
92 #define ASC_LBA_OUT_OF_RANGE 0x21
93 #define ASC_WRITE_ERROR 0x0C
94 #define ASC_READ_ERROR 0x11
95 #define ASC_NOT_READY 0x04
96 #define ASCQ_BECOMING_READY 0x01
98 #define SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA 0x02000000
100 /* storage interface */
102 #define USB_SC_SCSI 0x06 /* Transparent */
103 #define USB_PROT_BULK 0x50 /* bulk only */
105 static struct usb_interface_descriptor __attribute__((aligned(2)))
106 interface_descriptor =
108 .bLength = sizeof(struct usb_interface_descriptor),
109 .bDescriptorType = USB_DT_INTERFACE,
110 .bInterfaceNumber = 0,
111 .bAlternateSetting = 0,
112 .bNumEndpoints = 2,
113 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
114 .bInterfaceSubClass = USB_SC_SCSI,
115 .bInterfaceProtocol = USB_PROT_BULK,
116 .iInterface = 0
119 static struct usb_endpoint_descriptor __attribute__((aligned(2)))
120 endpoint_descriptor =
122 .bLength = sizeof(struct usb_endpoint_descriptor),
123 .bDescriptorType = USB_DT_ENDPOINT,
124 .bEndpointAddress = 0,
125 .bmAttributes = USB_ENDPOINT_XFER_BULK,
126 .wMaxPacketSize = 0,
127 .bInterval = 0
130 struct inquiry_data {
131 unsigned char DeviceType;
132 unsigned char DeviceTypeModifier;
133 unsigned char Versions;
134 unsigned char Format;
135 unsigned char AdditionalLength;
136 unsigned char Reserved[2];
137 unsigned char Capability;
138 unsigned char VendorId[8];
139 unsigned char ProductId[16];
140 unsigned char ProductRevisionLevel[4];
141 } __attribute__ ((packed));
143 struct report_lun_data {
144 unsigned int lun_list_length;
145 unsigned int reserved1;
146 unsigned char lun0[8];
147 #ifdef HAVE_HOTSWAP
148 unsigned char lun1[8];
149 #endif
150 } __attribute__ ((packed));
152 struct sense_data {
153 unsigned char ResponseCode;
154 unsigned char Obsolete;
155 unsigned char fei_sensekey;
156 unsigned int Information;
157 unsigned char AdditionalSenseLength;
158 unsigned int CommandSpecificInformation;
159 unsigned char AdditionalSenseCode;
160 unsigned char AdditionalSenseCodeQualifier;
161 unsigned char FieldReplaceableUnitCode;
162 unsigned char SKSV;
163 unsigned short SenseKeySpecific;
164 } __attribute__ ((packed));
166 struct mode_sense_bdesc_longlba {
167 unsigned char num_blocks[8];
168 unsigned char reserved[4];
169 unsigned char block_size[4];
170 } __attribute__ ((packed));
172 struct mode_sense_bdesc_shortlba {
173 unsigned char density_code;
174 unsigned char num_blocks[3];
175 unsigned char reserved;
176 unsigned char block_size[3];
177 } __attribute__ ((packed));
179 struct mode_sense_data_10 {
180 unsigned short mode_data_length;
181 unsigned char medium_type;
182 unsigned char device_specific;
183 unsigned char longlba;
184 unsigned char reserved;
185 unsigned short block_descriptor_length;
186 struct mode_sense_bdesc_longlba block_descriptor;
187 } __attribute__ ((packed));
189 struct mode_sense_data_6 {
190 unsigned char mode_data_length;
191 unsigned char medium_type;
192 unsigned char device_specific;
193 unsigned char block_descriptor_length;
194 struct mode_sense_bdesc_shortlba block_descriptor;
195 } __attribute__ ((packed));
197 struct command_block_wrapper {
198 unsigned int signature;
199 unsigned int tag;
200 unsigned int data_transfer_length;
201 unsigned char flags;
202 unsigned char lun;
203 unsigned char command_length;
204 unsigned char command_block[16];
205 } __attribute__ ((packed));
207 struct command_status_wrapper {
208 unsigned int signature;
209 unsigned int tag;
210 unsigned int data_residue;
211 unsigned char status;
212 } __attribute__ ((packed));
214 struct capacity {
215 unsigned int block_count;
216 unsigned int block_size;
217 } __attribute__ ((packed));
219 struct format_capacity {
220 unsigned int following_length;
221 unsigned int block_count;
222 unsigned int block_size;
223 } __attribute__ ((packed));
226 static union {
227 unsigned char* transfer_buffer;
228 struct inquiry_data* inquiry;
229 struct capacity* capacity_data;
230 struct format_capacity* format_capacity_data;
231 struct sense_data *sense_data;
232 struct mode_sense_data_6 *ms_data_6;
233 struct mode_sense_data_10 *ms_data_10;
234 struct report_lun_data *lun_data;
235 struct command_status_wrapper* csw;
236 char *max_lun;
237 } tb;
239 static struct {
240 unsigned int sector;
241 unsigned int count;
242 unsigned int tag;
243 unsigned int lun;
244 unsigned char *data[2];
245 unsigned char data_select;
246 unsigned int last_result;
247 } cur_cmd;
249 static struct {
250 unsigned char sense_key;
251 unsigned char information;
252 unsigned char asc;
253 unsigned char ascq;
254 } cur_sense_data;
256 static void handle_scsi(struct command_block_wrapper* cbw);
257 static void send_csw(int status);
258 static void send_command_result(void *data,int size);
259 static void send_command_failed_result(void);
260 static void send_block_data(void *data,int size);
261 static void receive_block_data(void *data,int size);
262 static void identify2inquiry(int lun);
263 static void send_and_read_next(void);
264 static bool ejected[NUM_VOLUMES];
266 static int usb_endpoint;
267 static int usb_interface;
269 static enum {
270 WAITING_FOR_COMMAND,
271 SENDING_BLOCKS,
272 SENDING_RESULT,
273 SENDING_FAILED_RESULT,
274 RECEIVING_BLOCKS,
275 SENDING_CSW
276 } state = WAITING_FOR_COMMAND;
278 static bool check_disk_present(IF_MV_NONVOID(int volume))
280 unsigned char sector[512];
281 return ata_read_sectors(IF_MV2(volume,)0,1,sector) == 0;
284 static void try_release_ata(void)
286 /* Check if there is a connected drive left. If not,
287 release excusive access */
288 bool canrelease=true;
289 int i;
290 for(i=0;i<NUM_VOLUMES;i++) {
291 if(ejected[i]==false){
292 canrelease=false;
293 break;
296 if(canrelease) {
297 logf("scsi release ata");
298 usb_release_exclusive_ata();
302 #ifdef HAVE_HOTSWAP
303 void usb_storage_notify_hotswap(int volume,bool inserted)
305 logf("notify %d",inserted);
306 if(inserted && check_disk_present(IF_MV(volume))) {
307 ejected[volume] = false;
309 else {
310 ejected[volume] = true;
311 try_release_ata();
315 #endif
317 void usb_storage_reconnect(void)
319 int i;
320 if(usb_core_driver_enabled(USB_DRIVER_MASS_STORAGE)
321 && usb_inserted()) {
322 for(i=0;i<NUM_VOLUMES;i++)
323 ejected[i] = !check_disk_present(IF_MV(i));
325 usb_request_exclusive_ata();
329 /* called by usb_code_init() */
330 void usb_storage_init(void)
332 int i;
333 for(i=0;i<NUM_VOLUMES;i++) {
334 ejected[i] = !check_disk_present(IF_MV(i));
336 logf("usb_storage_init done");
340 int usb_storage_set_first_endpoint(int endpoint)
342 usb_endpoint = endpoint;
343 return endpoint + 1;
345 int usb_storage_set_first_interface(int interface)
347 usb_interface = interface;
348 return interface + 1;
351 int usb_storage_get_config_descriptor(unsigned char *dest,int max_packet_size)
353 endpoint_descriptor.wMaxPacketSize=max_packet_size;
354 interface_descriptor.bInterfaceNumber=usb_interface;
356 memcpy(dest,&interface_descriptor,
357 sizeof(struct usb_interface_descriptor));
358 dest+=sizeof(struct usb_interface_descriptor);
360 endpoint_descriptor.bEndpointAddress = usb_endpoint | USB_DIR_IN;
361 memcpy(dest,&endpoint_descriptor,
362 sizeof(struct usb_endpoint_descriptor));
363 dest+=sizeof(struct usb_endpoint_descriptor);
365 endpoint_descriptor.bEndpointAddress = usb_endpoint | USB_DIR_OUT;
366 memcpy(dest,&endpoint_descriptor,
367 sizeof(struct usb_endpoint_descriptor));
369 return sizeof(struct usb_interface_descriptor) +
370 2*sizeof(struct usb_endpoint_descriptor);
373 void usb_storage_init_connection(void)
375 logf("ums: set config");
376 /* prime rx endpoint. We only need room for commands */
377 state = WAITING_FOR_COMMAND;
379 #if CONFIG_CPU == IMX31L || CONFIG_USBOTG == USBOTG_ISP1583
380 static unsigned char _transfer_buffer[BUFFER_SIZE*2]
381 USBDEVBSS_ATTR __attribute__((aligned(32)));
382 tb.transfer_buffer = (void *)_transfer_buffer;
383 #else
384 /* TODO : check if bufsize is at least 32K ? */
385 size_t bufsize;
386 unsigned char * audio_buffer;
388 audio_buffer = audio_get_buffer(false,&bufsize);
389 tb.transfer_buffer =
390 (void *)UNCACHED_ADDR((unsigned int)(audio_buffer + 31) & 0xffffffe0);
391 invalidate_icache();
392 #endif
393 usb_drv_recv(usb_endpoint, tb.transfer_buffer, 1024);
396 /* called by usb_core_transfer_complete() */
397 void usb_storage_transfer_complete(int ep,bool in,int status,int length)
399 (void)ep;
400 struct command_block_wrapper* cbw = (void*)tb.transfer_buffer;
402 //logf("transfer result %X %d", status, length);
403 switch(state) {
404 case RECEIVING_BLOCKS:
405 if(in==true) {
406 logf("IN received in RECEIVING");
408 logf("scsi write %d %d", cur_cmd.sector, cur_cmd.count);
409 if(status==0) {
410 if((unsigned int)length!=(SECTOR_SIZE*cur_cmd.count)
411 && (unsigned int)length!=BUFFER_SIZE) {
412 logf("unexpected length :%d",length);
415 unsigned int next_sector = cur_cmd.sector +
416 (BUFFER_SIZE/SECTOR_SIZE);
417 unsigned int next_count = cur_cmd.count -
418 MIN(cur_cmd.count,BUFFER_SIZE/SECTOR_SIZE);
419 int next_select = !cur_cmd.data_select;
421 #ifndef SERIALIZE_WRITES
422 if(next_count!=0) {
423 /* Ask the host to send more, to the other buffer */
424 receive_block_data(cur_cmd.data[next_select],
425 MIN(BUFFER_SIZE,next_count*SECTOR_SIZE));
427 #endif
429 /* Now write the data that just came in, while the host is
430 sending the next bit */
431 int result = ata_write_sectors(IF_MV2(cur_cmd.lun,)
432 cur_cmd.sector,
433 MIN(BUFFER_SIZE/SECTOR_SIZE,
434 cur_cmd.count),
435 cur_cmd.data[cur_cmd.data_select]);
436 if(result != 0) {
437 send_csw(UMS_STATUS_FAIL);
438 cur_sense_data.sense_key=SENSE_MEDIUM_ERROR;
439 cur_sense_data.asc=ASC_WRITE_ERROR;
440 cur_sense_data.ascq=0;
441 break;
443 #ifdef SERIALIZE_WRITES
444 if(next_count!=0) {
445 /* Ask the host to send more, to the other buffer */
446 receive_block_data(cur_cmd.data[next_select],
447 MIN(BUFFER_SIZE,next_count*SECTOR_SIZE));
449 #endif
451 if(next_count==0) {
452 send_csw(UMS_STATUS_GOOD);
455 /* Switch buffers for the next one */
456 cur_cmd.data_select=!cur_cmd.data_select;
458 cur_cmd.sector = next_sector;
459 cur_cmd.count = next_count;
462 else {
463 logf("Transfer failed %X",status);
464 send_csw(UMS_STATUS_FAIL);
465 /* TODO fill in cur_sense_data */
466 cur_sense_data.sense_key=0;
467 cur_sense_data.information=0;
468 cur_sense_data.asc=0;
469 cur_sense_data.ascq=0;
471 break;
472 case WAITING_FOR_COMMAND:
473 if(in==true) {
474 logf("IN received in WAITING_FOR_COMMAND");
476 //logf("command received");
477 if(letoh32(cbw->signature) == CBW_SIGNATURE){
478 handle_scsi(cbw);
480 else {
481 usb_drv_stall(usb_endpoint, true,true);
482 usb_drv_stall(usb_endpoint, true,false);
484 break;
485 case SENDING_CSW:
486 if(in==false) {
487 logf("OUT received in SENDING_CSW");
489 //logf("csw sent, now go back to idle");
490 state = WAITING_FOR_COMMAND;
491 usb_drv_recv(usb_endpoint, tb.transfer_buffer, 1024);
492 break;
493 case SENDING_RESULT:
494 if(in==false) {
495 logf("OUT received in SENDING");
497 if(status==0) {
498 //logf("data sent, now send csw");
499 send_csw(UMS_STATUS_GOOD);
501 else {
502 logf("Transfer failed %X",status);
503 send_csw(UMS_STATUS_FAIL);
504 /* TODO fill in cur_sense_data */
505 cur_sense_data.sense_key=0;
506 cur_sense_data.information=0;
507 cur_sense_data.asc=0;
508 cur_sense_data.ascq=0;
510 break;
511 case SENDING_FAILED_RESULT:
512 if(in==false) {
513 logf("OUT received in SENDING");
515 send_csw(UMS_STATUS_FAIL);
516 break;
517 case SENDING_BLOCKS:
518 if(in==false) {
519 logf("OUT received in SENDING");
521 if(status==0) {
522 if(cur_cmd.count==0) {
523 //logf("data sent, now send csw");
524 send_csw(UMS_STATUS_GOOD);
526 else {
527 send_and_read_next();
530 else {
531 logf("Transfer failed %X",status);
532 send_csw(UMS_STATUS_FAIL);
533 /* TODO fill in cur_sense_data */
534 cur_sense_data.sense_key=0;
535 cur_sense_data.information=0;
536 cur_sense_data.asc=0;
537 cur_sense_data.ascq=0;
539 break;
543 /* called by usb_core_control_request() */
544 bool usb_storage_control_request(struct usb_ctrlrequest* req)
546 bool handled = false;
549 switch (req->bRequest) {
550 case USB_BULK_GET_MAX_LUN: {
551 #ifdef ONLY_EXPOSE_CARD_SLOT
552 *tb.max_lun = 0;
553 #else
554 *tb.max_lun = NUM_VOLUMES - 1;
555 #endif
556 logf("ums: getmaxlun");
557 usb_drv_send(EP_CONTROL, tb.max_lun, 1);
558 usb_drv_recv(EP_CONTROL, NULL, 0); /* ack */
559 handled = true;
560 break;
563 case USB_BULK_RESET_REQUEST:
564 logf("ums: bulk reset");
565 state = WAITING_FOR_COMMAND;
566 /* UMS BOT 3.1 says The device shall preserve the value of its bulk
567 data toggle bits and endpoint STALL conditions despite
568 the Bulk-Only Mass Storage Reset. */
569 #if 0
570 usb_drv_reset_endpoint(usb_endpoint, false);
571 usb_drv_reset_endpoint(usb_endpoint, true);
572 #endif
574 usb_drv_send(EP_CONTROL, NULL, 0); /* ack */
575 handled = true;
576 break;
579 return handled;
582 static void send_and_read_next(void)
584 if(cur_cmd.last_result!=0) {
585 /* The last read failed. */
586 send_csw(UMS_STATUS_FAIL);
587 cur_sense_data.sense_key=SENSE_MEDIUM_ERROR;
588 cur_sense_data.asc=ASC_READ_ERROR;
589 cur_sense_data.ascq=0;
590 return;
592 send_block_data(cur_cmd.data[cur_cmd.data_select],
593 MIN(BUFFER_SIZE,cur_cmd.count*SECTOR_SIZE));
595 /* Switch buffers for the next one */
596 cur_cmd.data_select=!cur_cmd.data_select;
598 cur_cmd.sector+=(BUFFER_SIZE/SECTOR_SIZE);
599 cur_cmd.count-=MIN(cur_cmd.count,BUFFER_SIZE/SECTOR_SIZE);
601 if(cur_cmd.count!=0){
602 /* already read the next bit, so we can send it out immediately when the
603 * current transfer completes. */
604 cur_cmd.last_result = ata_read_sectors(IF_MV2(cur_cmd.lun,)
605 cur_cmd.sector,
606 MIN(BUFFER_SIZE/SECTOR_SIZE,
607 cur_cmd.count),
608 cur_cmd.data[cur_cmd.data_select]);
611 /****************************************************************************/
613 static void handle_scsi(struct command_block_wrapper* cbw)
615 /* USB Mass Storage assumes LBA capability.
616 TODO: support 48-bit LBA */
618 unsigned int length = cbw->data_transfer_length;
619 unsigned int block_size = 0;
620 unsigned int block_count = 0;
621 bool lun_present=true;
622 #ifdef ONLY_EXPOSE_CARD_SLOT
623 unsigned char lun = cbw->lun+1;
624 #else
625 unsigned char lun = cbw->lun;
626 #endif
627 unsigned int block_size_mult = 1;
628 #if defined(HAVE_ATA_SD) || defined(HAVE_HOTSWAP)
629 tCardInfo* cinfo = card_get_info(lun);
630 if(cinfo->initialized==1 && cinfo->numblocks > 0) {
631 block_size = cinfo->blocksize;
632 block_count = cinfo->numblocks;
634 else {
635 ejected[lun] = true;
636 try_release_ata();
638 #else
639 unsigned short* identify = ata_get_identify();
640 block_size = SECTOR_SIZE;
641 block_count = (identify[61] << 16 | identify[60]);
642 #endif
644 if(ejected[lun])
645 lun_present = false;
647 #ifdef MAX_LOG_SECTOR_SIZE
648 block_size_mult = disk_sector_multiplier;
649 #endif
651 cur_cmd.tag = cbw->tag;
652 cur_cmd.lun = lun;
654 switch (cbw->command_block[0]) {
655 case SCSI_TEST_UNIT_READY:
656 logf("scsi test_unit_ready %d",lun);
657 if(!usb_exclusive_ata()) {
658 send_csw(UMS_STATUS_FAIL);
659 cur_sense_data.sense_key=SENSE_NOT_READY;
660 cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
661 cur_sense_data.ascq=0;
662 break;
664 if(lun_present) {
665 send_csw(UMS_STATUS_GOOD);
667 else {
668 send_csw(UMS_STATUS_FAIL);
669 cur_sense_data.sense_key=SENSE_NOT_READY;
670 cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
671 cur_sense_data.ascq=0;
673 break;
675 case SCSI_REPORT_LUNS: {
676 logf("scsi inquiry %d",lun);
677 int allocation_length=0;
678 allocation_length|=(cbw->command_block[6]<<24);
679 allocation_length|=(cbw->command_block[7]<<16);
680 allocation_length|=(cbw->command_block[8]<<8);
681 allocation_length|=(cbw->command_block[9]);
682 memset(tb.lun_data,0,sizeof(struct report_lun_data));
683 #ifdef HAVE_HOTSWAP
684 tb.lun_data->lun_list_length=htobe32(16);
685 tb.lun_data->lun1[1]=1;
686 #else
687 tb.lun_data->lun_list_length=htobe32(8);
688 #endif
689 tb.lun_data->lun0[1]=0;
691 send_command_result(tb.lun_data,
692 MIN(sizeof(struct report_lun_data), length));
693 break;
696 case SCSI_INQUIRY:
697 logf("scsi inquiry %d",lun);
698 identify2inquiry(lun);
699 length = MIN(length, cbw->command_block[4]);
700 send_command_result(tb.inquiry,
701 MIN(sizeof(struct inquiry_data), length));
702 break;
704 case SCSI_REQUEST_SENSE: {
705 tb.sense_data->ResponseCode=0x70;/*current error*/
706 tb.sense_data->Obsolete=0;
707 tb.sense_data->fei_sensekey=cur_sense_data.sense_key&0x0f;
708 tb.sense_data->Information=cur_sense_data.information;
709 tb.sense_data->AdditionalSenseLength=10;
710 tb.sense_data->CommandSpecificInformation=0;
711 tb.sense_data->AdditionalSenseCode=cur_sense_data.asc;
712 tb.sense_data->AdditionalSenseCodeQualifier=cur_sense_data.ascq;
713 tb.sense_data->FieldReplaceableUnitCode=0;
714 tb.sense_data->SKSV=0;
715 tb.sense_data->SenseKeySpecific=0;
716 logf("scsi request_sense %d",lun);
717 send_command_result(tb.sense_data, sizeof(struct sense_data));
718 break;
721 case SCSI_MODE_SENSE_10: {
722 if(! lun_present) {
723 send_command_failed_result();
724 cur_sense_data.sense_key=SENSE_NOT_READY;
725 cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
726 cur_sense_data.ascq=0;
727 break;
729 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
730 unsigned char page_code = cbw->command_block[2] & 0x3f;
731 logf("scsi mode_sense_10 %d %X",lun,page_code);
732 switch(page_code) {
733 case 0x3f:
734 tb.ms_data_10->mode_data_length =
735 htobe16(sizeof(struct mode_sense_data_10)-2);
736 tb.ms_data_10->medium_type = 0;
737 tb.ms_data_10->device_specific = 0;
738 tb.ms_data_10->reserved = 0;
739 tb.ms_data_10->longlba = 1;
740 tb.ms_data_10->block_descriptor_length =
741 htobe16(sizeof(struct mode_sense_bdesc_longlba));
743 memset(tb.ms_data_10->block_descriptor.reserved,0,4);
744 memset(tb.ms_data_10->block_descriptor.num_blocks,0,8);
746 tb.ms_data_10->block_descriptor.num_blocks[4] =
747 ((block_count/block_size_mult) & 0xff000000)>>24;
748 tb.ms_data_10->block_descriptor.num_blocks[5] =
749 ((block_count/block_size_mult) & 0x00ff0000)>>16;
750 tb.ms_data_10->block_descriptor.num_blocks[6] =
751 ((block_count/block_size_mult) & 0x0000ff00)>>8;
752 tb.ms_data_10->block_descriptor.num_blocks[7] =
753 ((block_count/block_size_mult) & 0x000000ff);
755 tb.ms_data_10->block_descriptor.block_size[0] =
756 ((block_size*block_size_mult) & 0xff000000)>>24;
757 tb.ms_data_10->block_descriptor.block_size[1] =
758 ((block_size*block_size_mult) & 0x00ff0000)>>16;
759 tb.ms_data_10->block_descriptor.block_size[2] =
760 ((block_size*block_size_mult) & 0x0000ff00)>>8;
761 tb.ms_data_10->block_descriptor.block_size[3] =
762 ((block_size*block_size_mult) & 0x000000ff);
763 send_command_result(tb.ms_data_10,
764 MIN(sizeof(struct mode_sense_data_10), length));
765 break;
766 default:
767 send_command_failed_result();
768 cur_sense_data.sense_key=SENSE_ILLEGAL_REQUEST;
769 cur_sense_data.asc=ASC_INVALID_FIELD_IN_CBD;
770 cur_sense_data.ascq=0;
771 break;
773 break;
775 case SCSI_MODE_SENSE_6: {
776 if(! lun_present) {
777 send_command_failed_result();
778 cur_sense_data.sense_key=SENSE_NOT_READY;
779 cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
780 cur_sense_data.ascq=0;
781 break;
783 /*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
784 unsigned char page_code = cbw->command_block[2] & 0x3f;
785 logf("scsi mode_sense_6 %d %X",lun,page_code);
786 switch(page_code) {
787 case 0x3f:
788 /* All supported pages. */
789 tb.ms_data_6->mode_data_length =
790 sizeof(struct mode_sense_data_6)-1;
791 tb.ms_data_6->medium_type = 0;
792 tb.ms_data_6->device_specific = 0;
793 tb.ms_data_6->block_descriptor_length =
794 sizeof(struct mode_sense_bdesc_shortlba);
795 tb.ms_data_6->block_descriptor.density_code = 0;
796 tb.ms_data_6->block_descriptor.reserved = 0;
797 if(block_count/block_size_mult > 0xffffff){
798 tb.ms_data_6->block_descriptor.num_blocks[0] = 0xff;
799 tb.ms_data_6->block_descriptor.num_blocks[1] = 0xff;
800 tb.ms_data_6->block_descriptor.num_blocks[2] = 0xff;
802 else {
803 tb.ms_data_6->block_descriptor.num_blocks[0] =
804 ((block_count/block_size_mult) & 0xff0000)>>16;
805 tb.ms_data_6->block_descriptor.num_blocks[1] =
806 ((block_count/block_size_mult) & 0x00ff00)>>8;
807 tb.ms_data_6->block_descriptor.num_blocks[2] =
808 ((block_count/block_size_mult) & 0x0000ff);
810 tb.ms_data_6->block_descriptor.block_size[0] =
811 ((block_size*block_size_mult) & 0xff0000)>>16;
812 tb.ms_data_6->block_descriptor.block_size[1] =
813 ((block_size*block_size_mult) & 0x00ff00)>>8;
814 tb.ms_data_6->block_descriptor.block_size[2] =
815 ((block_size*block_size_mult) & 0x0000ff);
816 send_command_result(tb.ms_data_6,
817 MIN(sizeof(struct mode_sense_data_6), length));
818 break;
819 default:
820 send_command_failed_result();
821 cur_sense_data.sense_key=SENSE_ILLEGAL_REQUEST;
822 cur_sense_data.asc=ASC_INVALID_FIELD_IN_CBD;
823 cur_sense_data.ascq=0;
824 break;
826 break;
829 case SCSI_START_STOP_UNIT:
830 logf("scsi start_stop unit %d",lun);
831 if((cbw->command_block[4] & 0xf0) == 0) /*load/eject bit is valid*/
832 { /* Process start and eject bits */
833 logf("scsi load/eject");
834 if((cbw->command_block[4] & 0x01) == 0) /* Don't start */
836 if((cbw->command_block[4] & 0x02) != 0) /* eject */
838 logf("scsi eject");
839 ejected[lun]=true;
840 try_release_ata();
844 send_csw(UMS_STATUS_GOOD);
845 break;
847 case SCSI_ALLOW_MEDIUM_REMOVAL:
848 logf("scsi allow_medium_removal %d",lun);
849 /* TODO: use this to show the connect screen ? */
850 send_csw(UMS_STATUS_GOOD);
851 break;
852 case SCSI_READ_FORMAT_CAPACITY: {
853 logf("scsi read_format_capacity %d",lun);
854 if(lun_present) {
855 tb.format_capacity_data->following_length=htobe32(8);
856 /* "block count" actually means "number of last block" */
857 tb.format_capacity_data->block_count =
858 htobe32(block_count/block_size_mult - 1);
859 tb.format_capacity_data->block_size =
860 htobe32(block_size*block_size_mult);
861 tb.format_capacity_data->block_size |=
862 htobe32(SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA);
864 send_command_result(tb.format_capacity_data,
865 MIN(sizeof(struct format_capacity), length));
867 else
869 send_command_failed_result();
870 cur_sense_data.sense_key=SENSE_NOT_READY;
871 cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
872 cur_sense_data.ascq=0;
874 break;
876 case SCSI_READ_CAPACITY: {
877 logf("scsi read_capacity %d",lun);
879 if(lun_present) {
880 /* "block count" actually means "number of last block" */
881 tb.capacity_data->block_count =
882 htobe32(block_count/block_size_mult - 1);
883 tb.capacity_data->block_size =
884 htobe32(block_size*block_size_mult);
886 send_command_result(tb.capacity_data,
887 MIN(sizeof(struct capacity), length));
889 else
891 send_command_failed_result();
892 cur_sense_data.sense_key=SENSE_NOT_READY;
893 cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
894 cur_sense_data.ascq=0;
896 break;
899 case SCSI_READ_10:
900 logf("scsi read10 %d",lun);
901 if(! lun_present) {
902 send_command_failed_result();
903 cur_sense_data.sense_key=SENSE_NOT_READY;
904 cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
905 cur_sense_data.ascq=0;
906 break;
908 cur_cmd.data[0] = tb.transfer_buffer;
909 cur_cmd.data[1] = &tb.transfer_buffer[BUFFER_SIZE];
910 cur_cmd.data_select=0;
911 cur_cmd.sector = block_size_mult *
912 (cbw->command_block[2] << 24 |
913 cbw->command_block[3] << 16 |
914 cbw->command_block[4] << 8 |
915 cbw->command_block[5] );
916 cur_cmd.count = block_size_mult *
917 (cbw->command_block[7] << 8 |
918 cbw->command_block[8]);
920 //logf("scsi read %d %d", cur_cmd.sector, cur_cmd.count);
922 if((cur_cmd.sector + cur_cmd.count) > block_count) {
923 send_csw(UMS_STATUS_FAIL);
924 cur_sense_data.sense_key=SENSE_ILLEGAL_REQUEST;
925 cur_sense_data.asc=ASC_LBA_OUT_OF_RANGE;
926 cur_sense_data.ascq=0;
928 else {
929 cur_cmd.last_result = ata_read_sectors(IF_MV2(cur_cmd.lun,)
930 cur_cmd.sector,
931 MIN(BUFFER_SIZE/SECTOR_SIZE,
932 cur_cmd.count),
933 cur_cmd.data[cur_cmd.data_select]);
934 send_and_read_next();
936 break;
938 case SCSI_WRITE_10:
939 logf("scsi write10 %d",lun);
940 if(! lun_present) {
941 send_csw(UMS_STATUS_FAIL);
942 cur_sense_data.sense_key=SENSE_NOT_READY;
943 cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
944 cur_sense_data.ascq=0;
945 break;
947 cur_cmd.data[0] = tb.transfer_buffer;
948 cur_cmd.data[1] = &tb.transfer_buffer[BUFFER_SIZE];
949 cur_cmd.data_select=0;
950 cur_cmd.sector = block_size_mult *
951 (cbw->command_block[2] << 24 |
952 cbw->command_block[3] << 16 |
953 cbw->command_block[4] << 8 |
954 cbw->command_block[5] );
955 cur_cmd.count = block_size_mult *
956 (cbw->command_block[7] << 8 |
957 cbw->command_block[8]);
958 /* expect data */
959 if((cur_cmd.sector + cur_cmd.count) > block_count) {
960 send_csw(UMS_STATUS_FAIL);
961 cur_sense_data.sense_key=SENSE_ILLEGAL_REQUEST;
962 cur_sense_data.asc=ASC_LBA_OUT_OF_RANGE;
963 cur_sense_data.ascq=0;
965 else {
966 receive_block_data(cur_cmd.data[0],
967 MIN(BUFFER_SIZE,
968 cur_cmd.count*SECTOR_SIZE));
971 break;
973 default:
974 logf("scsi unknown cmd %x",cbw->command_block[0x0]);
975 usb_drv_stall(usb_endpoint, true,true);
976 send_csw(UMS_STATUS_FAIL);
977 break;
981 static void send_block_data(void *data,int size)
983 usb_drv_send_nonblocking(usb_endpoint, data,size);
984 state = SENDING_BLOCKS;
987 static void send_command_result(void *data,int size)
989 usb_drv_send_nonblocking(usb_endpoint, data,size);
990 state = SENDING_RESULT;
993 static void send_command_failed_result(void)
995 usb_drv_send_nonblocking(usb_endpoint, NULL, 0);
996 state = SENDING_FAILED_RESULT;
999 static void receive_block_data(void *data,int size)
1001 usb_drv_recv(usb_endpoint, data, size);
1002 state = RECEIVING_BLOCKS;
1005 static void send_csw(int status)
1007 tb.csw->signature = htole32(CSW_SIGNATURE);
1008 tb.csw->tag = cur_cmd.tag;
1009 tb.csw->data_residue = 0;
1010 tb.csw->status = status;
1012 usb_drv_send_nonblocking(usb_endpoint, tb.csw,
1013 sizeof(struct command_status_wrapper));
1014 state = SENDING_CSW;
1015 //logf("CSW: %X",status);
1017 if(status == UMS_STATUS_GOOD) {
1018 cur_sense_data.sense_key=0;
1019 cur_sense_data.information=0;
1020 cur_sense_data.asc=0;
1021 cur_sense_data.ascq=0;
1025 /* convert ATA IDENTIFY to SCSI INQUIRY */
1026 static void identify2inquiry(int lun)
1028 #ifdef HAVE_FLASH_STORAGE
1029 if(lun==0) {
1030 memcpy(&tb.inquiry->VendorId,"Rockbox ",8);
1031 memcpy(&tb.inquiry->ProductId,"Internal Storage",16);
1032 memcpy(&tb.inquiry->ProductRevisionLevel,"0.00",4);
1034 else {
1035 memcpy(&tb.inquiry->VendorId,"Rockbox ",8);
1036 memcpy(&tb.inquiry->ProductId,"SD Card Slot ",16);
1037 memcpy(&tb.inquiry->ProductRevisionLevel,"0.00",4);
1039 #else
1040 unsigned int i;
1041 unsigned short* dest;
1042 unsigned short* src;
1043 unsigned short* identify = ata_get_identify();
1044 (void)lun;
1045 memset(tb.inquiry, 0, sizeof(struct inquiry_data));
1047 #if 0
1048 if (identify[82] & 4)
1049 tb.inquiry->DeviceTypeModifier = DEVICE_REMOVABLE;
1050 #endif
1052 /* ATA only has a 'model' field, so we copy the
1053 first 8 bytes to 'vendor' and the rest to 'product' (they are
1054 consecutive in the inquiry struct) */
1055 src = (unsigned short*)&identify[27];
1056 dest = (unsigned short*)&tb.inquiry->VendorId;
1057 for (i=0;i<12;i++)
1058 dest[i] = htobe16(src[i]);
1060 src = (unsigned short*)&identify[23];
1061 dest = (unsigned short*)&tb.inquiry->ProductRevisionLevel;
1062 for (i=0;i<2;i++)
1063 dest[i] = htobe16(src[i]);
1064 #endif
1066 tb.inquiry->DeviceType = DIRECT_ACCESS_DEVICE;
1067 tb.inquiry->AdditionalLength = 0x1f;
1068 memset(tb.inquiry->Reserved, 0, 3);
1069 tb.inquiry->Versions = 4; /* SPC-2 */
1070 tb.inquiry->Format = 2; /* SPC-2/3 inquiry format */
1072 #if 0
1073 #ifdef HAVE_HOTSWAP
1074 if(lun>0)
1075 tb.inquiry->DeviceTypeModifier = DEVICE_REMOVABLE;
1076 #endif
1077 #endif
1078 /* Mac OSX 10.5 doesn't like this driver if DEVICE_REMOVABLE is not set.
1079 TODO : this can probably be solved by providing caching mode page */
1080 #ifdef TOSHIBA_GIGABEAT_S
1081 tb.inquiry->DeviceTypeModifier = 0;
1082 #else
1083 tb.inquiry->DeviceTypeModifier = DEVICE_REMOVABLE;
1084 #endif
1087 #endif /* USB_STORAGE */