1 /* Driver for SanDisk SDDR-09 SmartMedia reader
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
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
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"
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
,
72 unsigned char request
,
73 unsigned char requesttype
,
76 unsigned char *xfer_data
,
77 unsigned int xfer_len
) {
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.
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
,
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.
105 if (command[0] & USB_DIR_IN)
106 memcpy(xfer_data, buffer, xfer_len);
110 // Check the return code for the command.
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
,
141 if (direction
== SCSI_DATA_READ
)
142 pipe
= usb_rcvbulkpipe(us
->pusb_dev
, us
->ep_in
);
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",
153 usb_clear_halt(us
->pusb_dev
, pipe
);
158 /* NAK - that means we've retried a few times already */
159 if (result
== -ETIMEDOUT
) {
160 US_DEBUGP("usbat_raw_bulk():"
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",
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
,
204 int result
= USB_STOR_TRANSPORT_GOOD
;
207 struct scatterlist
*sg
;
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 */
221 strcpy(string
, "wr: ");
222 for (i
=0; i
<len
&& i
<48; i
++) {
223 sprintf(string
+strlen(string
), "%02X ",
226 US_DEBUGP("%s\n", string
);
227 strcpy(string
, "wr: ");
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"),
241 result
= sddr09_raw_bulk(us
, direction
, data
, len
);
243 sg
= (struct scatterlist
*)data
;
244 for (i
=0; i
<use_sg
&& transferred
<len
; i
++) {
245 result
= sddr09_raw_bulk(us
, direction
,
247 len
-transferred
> sg
[i
].length
?
248 sg
[i
].length
: len
-transferred
);
249 if (result
!=US_BULK_TRANSFER_GOOD
)
251 transferred
+= sg
[i
].length
;
255 if (direction
== SCSI_DATA_READ
) {
257 /* Debug-print the first 48 bytes of the read transfer */
260 strcpy(string
, "rd: ");
261 for (i
=0; i
<len
&& i
<48; i
++) {
262 sprintf(string
+strlen(string
), "%02X ",
265 US_DEBUGP("%s\n", string
);
266 strcpy(string
, "rd: ");
270 US_DEBUGP("%s\n", string
);
277 int sddr09_read_data(struct us_data
*us
,
278 unsigned long address
,
279 unsigned short sectors
,
280 unsigned char *content
,
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
;
291 unsigned short pages
;
292 unsigned char *buffer
= NULL
;
294 struct scatterlist
*sg
= NULL
;
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
;
309 sg
= (struct scatterlist
*)content
;
310 buffer
= kmalloc(len
, GFP_KERNEL
);
312 return USB_STOR_TRANSPORT_ERROR
;
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.
328 pba
= info
->lba_to_pba
[lba
];
330 // Read as many sectors as possible in this block
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
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),
362 US_DEBUGP("Result for send_control in read_data %d\n",
365 if (result
!= USB_STOR_TRANSPORT_GOOD
) {
371 result
= sddr09_bulk_transport(us
,
373 pages
*info
->pagesize
, 0);
375 if (result
!= USB_STOR_TRANSPORT_GOOD
) {
384 ptr
+= pages
*info
->pagesize
;
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
;
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
,
407 // Unlike in the documentation, the last two bytes are the
408 // number of blocks, not sectors.
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",
421 result
= sddr09_send_control(us
,
422 usb_sndctrlpipe(us
->pusb_dev
,0),
430 US_DEBUGP("Result for send_control in read_control %d\n",
433 if (result
!= USB_STOR_TRANSPORT_GOOD
)
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",
446 int sddr09_read_deviceID(struct us_data
*us
,
447 unsigned char *manufacturerID
,
448 unsigned char *deviceID
) {
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),
465 US_DEBUGP("Result of send_control for device ID is %d\n",
468 if (result
!= USB_STOR_TRANSPORT_GOOD
)
471 result
= sddr09_bulk_transport(us
,
472 SCSI_DATA_READ
, content
,
475 *manufacturerID
= content
[0];
476 *deviceID
= content
[1];
481 int sddr09_read_status(struct us_data
*us
,
482 unsigned char *status
) {
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),
498 if (result
!= USB_STOR_TRANSPORT_GOOD
)
501 result
= sddr09_bulk_transport(us
,
502 SCSI_DATA_READ
, status
,
508 int sddr09_reset(struct us_data
*us
) {
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),
527 unsigned long sddr09_get_capacity(struct us_data
*us
,
528 unsigned int *pagesize
) {
530 unsigned char manufacturerID
;
531 unsigned char deviceID
;
534 US_DEBUGP("Reading capacity...\n");
536 result
= sddr09_read_deviceID(us
,
540 US_DEBUGP("Result of read_deviceID is %d\n",
543 if (result
!= USB_STOR_TRANSPORT_GOOD
)
546 US_DEBUGP("Device ID = %02X\n", deviceID
);
547 US_DEBUGP("Manuf ID = %02X\n", manufacturerID
);
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
);
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
606 /* read 64 (2^6) bytes for every block (8192 (2^13) bytes)
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
,
619 USB_STOR_TRANSPORT_GOOD
) {
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
++) {
637 if (ptr
[0]!=0xFF || ptr
[1]!=0xFF || ptr
[2]!=0xFF ||
638 ptr
[3]!=0xFF || ptr
[4]!=0xFF || ptr
[5]!=0xFF)
640 if ((ptr
[6]>>4)!=0x01)
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
);
656 lba
= (lba
&0x07FF)>>1;
658 if (lba
>=numblocks
) {
659 US_DEBUGP("Bad LBA %04X for block %04X\n", lba
, i
);
664 US_DEBUGP("LBA %04X <-> PBA %04X\n",
667 info
->pba_to_lba
[i
] = lba
;
668 info
->lba_to_pba
[lba
] = i
;
676 static int init_sddr09(struct us_data *us) {
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)
694 US_DEBUGP("SDDR09: %02X %02X\n", data[0], data[1]);
698 if ( (result = sddr09_send_control(us, command, data, 2)) !=
699 USB_STOR_TRANSPORT_GOOD)
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");
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");
717 US_DEBUGP("SDDR09: request sense worked\n");
723 void sddr09_card_info_destructor(void *extra
) {
724 struct sddr09_card_info
*info
= (struct sddr09_card_info
*)extra
;
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
)
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',
750 unsigned char mode_page_01
[12] = {
751 0x01, 0x0a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
754 unsigned long capacity
;
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");
765 us->flags &= ~US_FL_NEED_INIT;
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
;
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);
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);
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]);
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
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"
866 pba
, lba
, page
, pages
);
868 return sddr09_read_data(us
,
869 ((pba
<<4)+page
)*info
->pagesize
, pages
,
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;
886 sprintf(string
+strlen(string
), "%02X ", srb
->cmnd
[i
]);
888 US_DEBUGP("SDDR09: Send control for command %s\n",
891 if ( (result
= sddr09_send_control(us
,
892 usb_sndctrlpipe(us
->pusb_dev
,0),
898 12)) != USB_STOR_TRANSPORT_GOOD
)
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
,
917 srb
->request_bufflen
, srb
->use_sg
);
923 return USB_STOR_TRANSPORT_GOOD
;