Merge with Linux 2.4.0-test6-pre2.
[linux-2.6/linux-mips.git] / drivers / usb / storage / sddr09.c
blob6bf9f147778823f1337de6fb096c442c9c7c6838
1 /* Driver for SanDisk SDDR-09 SmartMedia reader
3 * SDDR09 driver v0.1:
5 * First release
7 * Current development and maintainance by:
8 * (c) 2000 Robert Baruch (autophile@dol.net)
10 * The SanDisk SDDR-09 SmartMedia reader uses the Shuttle EUSB-01 chip.
11 * This chip is a programmable USB controller. In the SDDR-09, it has
12 * been programmed to obey a certain limited set of SCSI commands. This
13 * driver translates the "real" SCSI commands to the SDDR-09 SCSI
14 * commands.
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 2, or (at your option) any
19 * later version.
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include "transport.h"
32 #include "protocol.h"
33 #include "usb.h"
34 #include "debug.h"
35 #include "sddr09.h"
37 #include <linux/sched.h>
38 #include <linux/errno.h>
39 #include <linux/malloc.h>
41 extern int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
42 u8 request, u8 requesttype, u16 value, u16 index,
43 void *data, u16 size);
44 extern int usb_stor_bulk_msg(struct us_data *us, void *data, int pipe,
45 unsigned int len, unsigned int *act_len);
47 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
48 #define LSB_of(s) ((s)&0xFF)
49 #define MSB_of(s) ((s)>>8)
52 * Send a control message and wait for the response.
54 * us - the pointer to the us_data structure for the device to use
56 * request - the URB Setup Packet's first 6 bytes. The first byte always
57 * corresponds to the request type, and the second byte always corresponds
58 * to the request. The other 4 bytes do not correspond to value and index,
59 * since they are used in a custom way by the SCM protocol.
61 * xfer_data - a buffer from which to get, or to which to store, any data
62 * that gets send or received, respectively, with the URB. Even though
63 * it looks like we allocate a buffer in this code for the data, xfer_data
64 * must contain enough allocated space.
66 * xfer_len - the number of bytes to send or receive with the URB.
70 static int sddr09_send_control(struct us_data *us,
71 int pipe,
72 unsigned char request,
73 unsigned char requesttype,
74 unsigned short value,
75 unsigned short index,
76 unsigned char *xfer_data,
77 unsigned int xfer_len) {
79 int result;
81 // If data is going to be sent or received with the URB,
82 // then allocate a buffer for it. If data is to be sent,
83 // copy the data into the buffer.
85 if (xfer_len > 0) {
86 buffer = kmalloc(xfer_len, GFP_KERNEL);
87 if (!(command[0] & USB_DIR_IN))
88 memcpy(buffer, xfer_data, xfer_len);
91 // Send the URB to the device and wait for a response.
93 /* Why are request and request type reversed in this call? */
95 result = usb_stor_control_msg(us, pipe,
96 request, requesttype, value, index,
97 xfer_data, xfer_len);
100 // If data was sent or received with the URB, free the buffer we
101 // allocated earlier, but not before reading the data out of the
102 // buffer if we wanted to receive data.
104 if (xfer_len > 0) {
105 if (command[0] & USB_DIR_IN)
106 memcpy(xfer_data, buffer, xfer_len);
107 kfree(buffer);
110 // Check the return code for the command.
112 if (result < 0) {
113 /* if the command was aborted, indicate that */
114 if (result == -ENOENT)
115 return USB_STOR_TRANSPORT_ABORTED;
117 /* a stall is a fatal condition from the device */
118 if (result == -EPIPE) {
119 US_DEBUGP("-- Stall on control pipe. Clearing\n");
120 result = usb_clear_halt(us->pusb_dev, pipe);
121 US_DEBUGP("-- usb_clear_halt() returns %d\n", result);
122 return USB_STOR_TRANSPORT_FAILED;
125 /* Uh oh... serious problem here */
126 return USB_STOR_TRANSPORT_ERROR;
129 return USB_STOR_TRANSPORT_GOOD;
132 static int sddr09_raw_bulk(struct us_data *us,
133 int direction,
134 unsigned char *data,
135 unsigned int len) {
137 int result;
138 int act_len;
139 int pipe;
141 if (direction == SCSI_DATA_READ)
142 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
143 else
144 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
146 result = usb_stor_bulk_msg(us, data, pipe, len, &act_len);
148 /* if we stall, we need to clear it before we go on */
149 if (result == -EPIPE) {
150 US_DEBUGP("EPIPE: clearing endpoint halt for"
151 " pipe 0x%x, stalled at %d bytes\n",
152 pipe, act_len);
153 usb_clear_halt(us->pusb_dev, pipe);
156 if (result) {
158 /* NAK - that means we've retried a few times already */
159 if (result == -ETIMEDOUT) {
160 US_DEBUGP("usbat_raw_bulk():"
161 " device NAKed\n");
162 return US_BULK_TRANSFER_FAILED;
165 /* -ENOENT -- we canceled this transfer */
166 if (result == -ENOENT) {
167 US_DEBUGP("usbat_raw_bulk():"
168 " transfer aborted\n");
169 return US_BULK_TRANSFER_ABORTED;
172 if (result == -EPIPE) {
173 US_DEBUGP("usbat_raw_bulk():"
174 " output pipe stalled\n");
175 return USB_STOR_TRANSPORT_FAILED;
178 /* the catch-all case */
179 US_DEBUGP("us_transfer_partial(): unknown error\n");
180 return US_BULK_TRANSFER_FAILED;
183 if (act_len != len) {
184 US_DEBUGP("Warning: Transferred only %d bytes\n",
185 act_len);
186 return US_BULK_TRANSFER_SHORT;
189 US_DEBUGP("Transfered %d of %d bytes\n", act_len, len);
191 return US_BULK_TRANSFER_GOOD;
195 * Note: direction must be set if command_len == 0.
198 static int sddr09_bulk_transport(struct us_data *us,
199 int direction,
200 unsigned char *data,
201 unsigned int len,
202 int use_sg) {
204 int result = USB_STOR_TRANSPORT_GOOD;
205 int transferred = 0;
206 int i;
207 struct scatterlist *sg;
208 char string[64];
210 if (len==0)
211 return USB_STOR_TRANSPORT_GOOD;
214 /* transfer the data */
216 if (direction == SCSI_DATA_WRITE) {
218 /* Debug-print the first 48 bytes of the write transfer */
220 if (!use_sg) {
221 strcpy(string, "wr: ");
222 for (i=0; i<len && i<48; i++) {
223 sprintf(string+strlen(string), "%02X ",
224 data[i]);
225 if ((i%16)==15) {
226 US_DEBUGP("%s\n", string);
227 strcpy(string, "wr: ");
230 if ((i%16)!=0)
231 US_DEBUGP("%s\n", string);
236 US_DEBUGP("SCM data %s transfer %d sg buffers %d\n",
237 ( direction==SCSI_DATA_READ ? "in" : "out"),
238 len, use_sg);
240 if (!use_sg)
241 result = sddr09_raw_bulk(us, direction, data, len);
242 else {
243 sg = (struct scatterlist *)data;
244 for (i=0; i<use_sg && transferred<len; i++) {
245 result = sddr09_raw_bulk(us, direction,
246 sg[i].address,
247 len-transferred > sg[i].length ?
248 sg[i].length : len-transferred);
249 if (result!=US_BULK_TRANSFER_GOOD)
250 break;
251 transferred += sg[i].length;
255 if (direction == SCSI_DATA_READ) {
257 /* Debug-print the first 48 bytes of the read transfer */
259 if (!use_sg) {
260 strcpy(string, "rd: ");
261 for (i=0; i<len && i<48; i++) {
262 sprintf(string+strlen(string), "%02X ",
263 data[i]);
264 if ((i%16)==15) {
265 US_DEBUGP("%s\n", string);
266 strcpy(string, "rd: ");
269 if ((i%16)!=0)
270 US_DEBUGP("%s\n", string);
274 return result;
277 int sddr09_read_data(struct us_data *us,
278 unsigned long address,
279 unsigned short sectors,
280 unsigned char *content,
281 int use_sg) {
283 int result;
284 unsigned char command[12] = {
285 0xe8, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
287 struct sddr09_card_info *info = (struct sddr09_card_info *)us->extra;
288 unsigned int lba;
289 unsigned int pba;
290 unsigned short page;
291 unsigned short pages;
292 unsigned char *buffer = NULL;
293 unsigned char *ptr;
294 struct scatterlist *sg = NULL;
295 int i;
296 int len;
297 int transferred;
299 // If we're using scatter-gather, we have to create a new
300 // buffer to read all of the data in first, since a
301 // scatter-gather buffer could in theory start in the middle
302 // of a page, which would be bad. A developer who wants a
303 // challenge might want to write a limited-buffer
304 // version of this code.
306 len = sectors*info->pagesize;
308 if (use_sg) {
309 sg = (struct scatterlist *)content;
310 buffer = kmalloc(len, GFP_KERNEL);
311 if (buffer == NULL)
312 return USB_STOR_TRANSPORT_ERROR;
313 ptr = buffer;
314 } else
315 ptr = content;
317 // Figure out the initial LBA and page
319 pba = (address/info->pagesize)>>4;
320 lba = info->pba_to_lba[pba];
321 page = (address/info->pagesize)&0x0F;
323 // This could be made much more efficient by checking for
324 // contiguous LBA's. Another exercise left to the student.
326 while (sectors>0) {
328 pba = info->lba_to_pba[lba];
330 // Read as many sectors as possible in this block
332 pages = 0x10-page;
333 if (pages > sectors)
334 pages = sectors;
336 US_DEBUGP("Read %01X pages, from PBA %04X"
337 " (LBA %04X) page %01X\n",
338 pages, pba, lba, page);
340 address = ((pba<<4)+page)*info->pagesize;
342 // Unlike in the documentation, the address is in
343 // words of 2 bytes.
345 command[2] = MSB_of(address>>17);
346 command[3] = LSB_of(address>>17);
347 command[4] = MSB_of((address>>1)&0xFFFF);
348 command[5] = LSB_of((address>>1)&0xFFFF);
350 command[10] = MSB_of(pages);
351 command[11] = LSB_of(pages);
353 result = sddr09_send_control(us,
354 usb_sndctrlpipe(us->pusb_dev,0),
356 0x41,
359 command,
360 12);
362 US_DEBUGP("Result for send_control in read_data %d\n",
363 result);
365 if (result != USB_STOR_TRANSPORT_GOOD) {
366 if (use_sg)
367 kfree(buffer);
368 return result;
371 result = sddr09_bulk_transport(us,
372 SCSI_DATA_READ, ptr,
373 pages*info->pagesize, 0);
375 if (result != USB_STOR_TRANSPORT_GOOD) {
376 if (use_sg)
377 kfree(buffer);
378 return result;
381 page = 0;
382 lba++;
383 sectors -= pages;
384 ptr += pages*info->pagesize;
387 if (use_sg) {
388 transferred = 0;
389 for (i=0; i<use_sg && transferred<len; i++) {
390 memcpy(sg[i].address, buffer+transferred,
391 len-transferred > sg[i].length ?
392 sg[i].length : len-transferred);
393 transferred += sg[i].length;
395 kfree(buffer);
398 return USB_STOR_TRANSPORT_GOOD;
401 int sddr09_read_control(struct us_data *us,
402 unsigned long address,
403 unsigned short blocks,
404 unsigned char *content,
405 int use_sg) {
407 // Unlike in the documentation, the last two bytes are the
408 // number of blocks, not sectors.
410 int result;
411 unsigned char command[12] = {
412 0xe8, 0x21, MSB_of(address>>16),
413 LSB_of(address>>16), MSB_of(address&0xFFFF),
414 LSB_of(address&0xFFFF), 0, 0, 0, 0,
415 MSB_of(blocks), LSB_of(blocks)
418 US_DEBUGP("Read control address %08X blocks %04X\n",
419 address, blocks);
421 result = sddr09_send_control(us,
422 usb_sndctrlpipe(us->pusb_dev,0),
424 0x41,
427 command,
428 12);
430 US_DEBUGP("Result for send_control in read_control %d\n",
431 result);
433 if (result != USB_STOR_TRANSPORT_GOOD)
434 return result;
436 result = sddr09_bulk_transport(us,
437 SCSI_DATA_READ, content,
438 blocks*0x40, use_sg);
440 US_DEBUGP("Result for bulk read in read_control %d\n",
441 result);
443 return result;
446 int sddr09_read_deviceID(struct us_data *us,
447 unsigned char *manufacturerID,
448 unsigned char *deviceID) {
450 int result;
451 unsigned char command[12] = {
452 0xed, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
454 unsigned char content[64];
456 result = sddr09_send_control(us,
457 usb_sndctrlpipe(us->pusb_dev,0),
459 0x41,
462 command,
463 12);
465 US_DEBUGP("Result of send_control for device ID is %d\n",
466 result);
468 if (result != USB_STOR_TRANSPORT_GOOD)
469 return result;
471 result = sddr09_bulk_transport(us,
472 SCSI_DATA_READ, content,
473 64, 0);
475 *manufacturerID = content[0];
476 *deviceID = content[1];
478 return result;
481 int sddr09_read_status(struct us_data *us,
482 unsigned char *status) {
484 int result;
485 unsigned char command[12] = {
486 0xec, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
489 result = sddr09_send_control(us,
490 usb_sndctrlpipe(us->pusb_dev,0),
492 0x41,
495 command,
496 12);
498 if (result != USB_STOR_TRANSPORT_GOOD)
499 return result;
501 result = sddr09_bulk_transport(us,
502 SCSI_DATA_READ, status,
503 1, 0);
505 return result;
508 int sddr09_reset(struct us_data *us) {
510 int result;
511 unsigned char command[12] = {
512 0xeb, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
515 result = sddr09_send_control(us,
516 usb_sndctrlpipe(us->pusb_dev,0),
518 0x41,
521 command,
522 12);
524 return result;
527 unsigned long sddr09_get_capacity(struct us_data *us,
528 unsigned int *pagesize) {
530 unsigned char manufacturerID;
531 unsigned char deviceID;
532 int result;
534 US_DEBUGP("Reading capacity...\n");
536 result = sddr09_read_deviceID(us,
537 &manufacturerID,
538 &deviceID);
540 US_DEBUGP("Result of read_deviceID is %d\n",
541 result);
543 if (result != USB_STOR_TRANSPORT_GOOD)
544 return 0;
546 US_DEBUGP("Device ID = %02X\n", deviceID);
547 US_DEBUGP("Manuf ID = %02X\n", manufacturerID);
549 *pagesize = 512;
551 switch (deviceID) {
553 case 0x6e: // 1MB
554 case 0xe8:
555 case 0xec:
556 *pagesize = 256;
557 return 0x00100000;
559 case 0x5d: // 2MB
560 case 0xea:
561 case 0x64:
562 if (deviceID!=0x5D)
563 *pagesize = 256;
564 return 0x00200000;
566 case 0xe3: // 4MB
567 case 0xe5:
568 case 0x6b:
569 case 0xd5:
570 return 0x00400000;
572 case 0xe6: // 8MB
573 case 0xd6:
574 return 0x00800000;
576 case 0x73: // 16MB
577 return 0x01000000;
579 case 0x75: // 32MB
580 return 0x02000000;
582 default: // unknown
583 return 0;
588 int sddr09_read_map(struct us_data *us) {
590 unsigned char *control;
591 struct sddr09_card_info *info = (struct sddr09_card_info *)(us->extra);
592 int numblocks;
593 int i;
594 unsigned char *ptr;
595 unsigned short lba;
596 unsigned char parity;
597 unsigned char fast_parity[16] = {
598 0, 1, 1, 0, 1, 0, 0, 1,
599 1, 0, 0, 1, 0, 1, 1, 0
601 int result;
603 if (!info->capacity)
604 return -1;
606 /* read 64 (2^6) bytes for every block (8192 (2^13) bytes)
607 of capacity:
608 64*(capacity/8192) = capacity*(2^6)*(2^-13) =
609 capacity*2^(6-13) = capacity*(2^-7)
612 control = kmalloc(info->capacity>>7, GFP_KERNEL);
615 numblocks = info->capacity>>13;
617 if ( (result = sddr09_read_control(us, 0, numblocks,
618 control, 0)) !=
619 USB_STOR_TRANSPORT_GOOD) {
620 kfree(control);
621 return -1;
626 if (info->lba_to_pba)
627 kfree(info->lba_to_pba);
628 if (info->pba_to_lba)
629 kfree(info->pba_to_lba);
630 info->lba_to_pba = kmalloc(numblocks*sizeof(int), GFP_KERNEL);
631 info->pba_to_lba = kmalloc(numblocks*sizeof(int), GFP_KERNEL);
632 memset(info->lba_to_pba, 0, numblocks*sizeof(int));
633 memset(info->pba_to_lba, 0, numblocks*sizeof(int));
635 for (i=0; i<numblocks; i++) {
636 ptr = control+64*i;
637 if (ptr[0]!=0xFF || ptr[1]!=0xFF || ptr[2]!=0xFF ||
638 ptr[3]!=0xFF || ptr[4]!=0xFF || ptr[5]!=0xFF)
639 continue;
640 if ((ptr[6]>>4)!=0x01)
641 continue;
643 /* ensure even parity */
645 lba = short_pack(ptr[7], ptr[6]);
646 parity = 1; // the parity of 0x1000
647 parity ^= fast_parity[lba & 0x000F];
648 parity ^= fast_parity[(lba>>4) & 0x000F];
649 parity ^= fast_parity[(lba>>8) & 0x000F];
651 if (parity) { /* bad parity bit */
652 US_DEBUGP("Bad parity in LBA for block %04X\n", i);
653 continue;
656 lba = (lba&0x07FF)>>1;
658 if (lba>=numblocks) {
659 US_DEBUGP("Bad LBA %04X for block %04X\n", lba, i);
660 continue;
663 if (i<0x10)
664 US_DEBUGP("LBA %04X <-> PBA %04X\n",
665 lba, i);
667 info->pba_to_lba[i] = lba;
668 info->lba_to_pba[lba] = i;
671 kfree(control);
672 return 0;
676 static int init_sddr09(struct us_data *us) {
678 int result;
679 unsigned char data[14];
680 unsigned char command[8] = {
681 0xc1, 0x01, 0, 0, 0, 0, 0, 0
683 unsigned char command2[8] = {
684 0x41, 0, 0, 0, 0, 0, 0, 0
686 unsigned char tur[12] = {
687 0x03, 0x20, 0, 0, 0x0e, 0, 0, 0, 0, 0, 0, 0
690 if ( (result = sddr09_send_control(us, command, data, 2)) !=
691 USB_STOR_TRANSPORT_GOOD)
692 return result;
694 US_DEBUGP("SDDR09: %02X %02X\n", data[0], data[1]);
696 command[1] = 0x08;
698 if ( (result = sddr09_send_control(us, command, data, 2)) !=
699 USB_STOR_TRANSPORT_GOOD)
700 return result;
702 US_DEBUGP("SDDR09: %02X %02X\n", data[0], data[1]);
704 if ( (result = sddr09_send_control(us, command2, tur, 12)) !=
705 USB_STOR_TRANSPORT_GOOD) {
706 US_DEBUGP("SDDR09: request sense failed\n");
707 return result;
710 if ( (result = sddr09_raw_bulk(
711 us, SCSI_DATA_READ, data, 14)) !=
712 USB_STOR_TRANSPORT_GOOD) {
713 US_DEBUGP("SDDR09: request sense bulk in failed\n");
714 return result;
717 US_DEBUGP("SDDR09: request sense worked\n");
719 return result;
723 void sddr09_card_info_destructor(void *extra) {
724 struct sddr09_card_info *info = (struct sddr09_card_info *)extra;
726 if (!extra)
727 return;
729 if (info->lba_to_pba)
730 kfree(info->lba_to_pba);
731 if (info->pba_to_lba)
732 kfree(info->pba_to_lba);
736 * Transport for the Sandisk SDDR-09
738 int sddr09_transport(Scsi_Cmnd *srb, struct us_data *us)
740 int result;
741 int i;
742 char string[64];
743 unsigned char inquiry_response[36] = {
744 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00,
745 'S', 'a', 'n', 'D', 'i', 's', 'k', ' ',
746 'I', 'm', 'a', 'g', 'e', 'M', 'a', 't',
747 'e', ' ', 'S', 'D', 'D', 'R', '0', '9',
748 ' ', ' ', ' ', ' '
750 unsigned char mode_page_01[12] = {
751 0x01, 0x0a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
753 unsigned char *ptr;
754 unsigned long capacity;
755 unsigned int lba;
756 unsigned int pba;
757 unsigned int page;
758 unsigned short pages;
759 struct sddr09_card_info *info = (struct sddr09_card_info *)(us->extra);
762 if (us->flags & US_FL_NEED_INIT) {
763 US_DEBUGP("SDDR-09: initializing\n");
764 init_sddr09(us);
765 us->flags &= ~US_FL_NEED_INIT;
769 if (!us->extra) {
770 us->extra = kmalloc(
771 sizeof(struct sddr09_card_info), GFP_KERNEL);
772 memset(us->extra, 0, sizeof(struct sddr09_card_info));
773 us->extra_destructor = sddr09_card_info_destructor;
776 ptr = (unsigned char *)srb->request_buffer;
778 /* Dummy up a response for INQUIRY since SDDR09 doesn't
779 respond to INQUIRY commands */
781 if (srb->cmnd[0] == INQUIRY) {
782 memcpy(ptr, inquiry_response, 36);
783 return USB_STOR_TRANSPORT_GOOD;
786 if (srb->cmnd[0] == READ_CAPACITY) {
788 capacity = sddr09_get_capacity(us, &info->pagesize);
789 info->capacity = capacity;
791 // Last page in the card
793 capacity /= info->pagesize;
794 capacity--;
796 ptr[0] = MSB_of(capacity>>16);
797 ptr[1] = LSB_of(capacity>>16);
798 ptr[2] = MSB_of(capacity&0xFFFF);
799 ptr[3] = LSB_of(capacity&0xFFFF);
801 // The page size
803 ptr[4] = MSB_of(info->pagesize>>16);
804 ptr[5] = LSB_of(info->pagesize>>16);
805 ptr[6] = MSB_of(info->pagesize&0xFFFF);
806 ptr[7] = LSB_of(info->pagesize&0xFFFF);
808 sddr09_read_map(us);
810 return USB_STOR_TRANSPORT_GOOD;
813 if (srb->cmnd[0] == MODE_SENSE) {
815 // Read-write error recovery page: there needs to
816 // be a check for write-protect here
818 if ( (srb->cmnd[2] & 0x3F) == 0x01 ) {
819 if (ptr==NULL || srb->request_bufflen<12)
820 return USB_STOR_TRANSPORT_ERROR;
821 memcpy(ptr, mode_page_01, 12);
822 return USB_STOR_TRANSPORT_GOOD;
825 // FIXME: sense buffer?
827 return USB_STOR_TRANSPORT_ERROR;
830 if (srb->cmnd[0] == READ_10) {
832 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
833 page <<= 16;
834 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
835 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
837 // convert page to block and page-within-block
839 lba = page>>4;
840 page = page&0x0F;
842 // locate physical block corresponding to logical block
844 if (lba>=(info->capacity>>13)) {
846 // FIXME: sense buffer?
848 return USB_STOR_TRANSPORT_ERROR;
851 pba = info->lba_to_pba[lba];
853 // if pba is 0, either it's really 0, in which case
854 // the pba-to-lba map for pba 0 will be the lba,
855 // or that lba doesn't exist.
857 if (pba==0 && info->pba_to_lba[0] != lba) {
859 // FIXME: sense buffer?
861 return USB_STOR_TRANSPORT_ERROR;
864 US_DEBUGP("READ_10: read block %04X (LBA %04X) page %01X"
865 " pages %d\n",
866 pba, lba, page, pages);
868 return sddr09_read_data(us,
869 ((pba<<4)+page)*info->pagesize, pages,
870 ptr, srb->use_sg);
873 // Pass TEST_UNIT_READY and REQUEST_SENSE through
875 if (srb->cmnd[0] != TEST_UNIT_READY &&
876 srb->cmnd[0] != REQUEST_SENSE)
877 return USB_STOR_TRANSPORT_ERROR; // FIXME: sense buffer?
879 for (; srb->cmd_len<12; srb->cmd_len++)
880 srb->cmnd[srb->cmd_len] = 0;
882 srb->cmnd[1] = 0x20;
884 string[0] = 0;
885 for (i=0; i<12; i++)
886 sprintf(string+strlen(string), "%02X ", srb->cmnd[i]);
888 US_DEBUGP("SDDR09: Send control for command %s\n",
889 string);
891 if ( (result = sddr09_send_control(us,
892 usb_sndctrlpipe(us->pusb_dev,0),
894 0x41,
897 srb->cmnd,
898 12)) != USB_STOR_TRANSPORT_GOOD)
899 return result;
901 US_DEBUGP("SDDR09: Control for command OK\n");
903 if (srb->request_bufflen == 0)
904 return USB_STOR_TRANSPORT_GOOD;
906 if (srb->sc_data_direction == SCSI_DATA_WRITE ||
907 srb->sc_data_direction == SCSI_DATA_READ) {
909 US_DEBUGP("SDDR09: %s %d bytes\n",
910 srb->sc_data_direction==SCSI_DATA_WRITE ?
911 "sending" : "receiving",
912 srb->request_bufflen);
914 result = sddr09_bulk_transport(us,
915 srb->sc_data_direction,
916 srb->request_buffer,
917 srb->request_bufflen, srb->use_sg);
919 return result;
923 return USB_STOR_TRANSPORT_GOOD;