1 /* Driver for SanDisk SDDR-55 SmartMedia reader
7 * Current development and maintenance by:
8 * (c) 2002 Simon Munton
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2, or (at your option) any
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/jiffies.h>
26 #include <linux/errno.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_cmnd.h>
34 #include "transport.h"
39 #define DRV_NAME "ums-sddr55"
41 MODULE_DESCRIPTION("Driver for SanDisk SDDR-55 SmartMedia reader");
42 MODULE_AUTHOR("Simon Munton");
43 MODULE_LICENSE("GPL");
46 * The table of devices
48 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
49 vendorName, productName, useProtocol, useTransport, \
50 initFunction, flags) \
51 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
52 .driver_info = (flags) }
54 static struct usb_device_id sddr55_usb_ids
[] = {
55 # include "unusual_sddr55.h"
56 { } /* Terminating entry */
58 MODULE_DEVICE_TABLE(usb
, sddr55_usb_ids
);
65 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
66 vendor_name, product_name, use_protocol, use_transport, \
67 init_function, Flags) \
69 .vendorName = vendor_name, \
70 .productName = product_name, \
71 .useProtocol = use_protocol, \
72 .useTransport = use_transport, \
73 .initFunction = init_function, \
76 static struct us_unusual_dev sddr55_unusual_dev_list
[] = {
77 # include "unusual_sddr55.h"
78 { } /* Terminating entry */
84 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
85 #define LSB_of(s) ((s)&0xFF)
86 #define MSB_of(s) ((s)>>8)
89 #define set_sense_info(sk, asc, ascq) \
91 info->sense_data[2] = sk; \
92 info->sense_data[12] = asc; \
93 info->sense_data[13] = ascq; \
97 struct sddr55_card_info
{
98 unsigned long capacity
; /* Size of card in bytes */
99 int max_log_blks
; /* maximum number of logical blocks */
100 int pageshift
; /* log2 of pagesize */
101 int smallpageshift
; /* 1 if pagesize == 256 */
102 int blocksize
; /* Size of block in pages */
103 int blockshift
; /* log2 of blocksize */
104 int blockmask
; /* 2^blockshift - 1 */
105 int read_only
; /* non zero if card is write protected */
106 int force_read_only
; /* non zero if we find a map error*/
107 int *lba_to_pba
; /* logical to physical map */
108 int *pba_to_lba
; /* physical to logical map */
109 int fatal_error
; /* set if we detect something nasty */
110 unsigned long last_access
; /* number of jiffies since we last talked to device */
111 unsigned char sense_data
[18];
115 #define NOT_ALLOCATED 0xffffffff
116 #define BAD_BLOCK 0xffff
117 #define CIS_BLOCK 0x400
118 #define UNUSED_BLOCK 0x3ff
121 sddr55_bulk_transport(struct us_data
*us
, int direction
,
122 unsigned char *data
, unsigned int len
) {
123 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
124 unsigned int pipe
= (direction
== DMA_FROM_DEVICE
) ?
125 us
->recv_bulk_pipe
: us
->send_bulk_pipe
;
128 return USB_STOR_XFER_GOOD
;
129 info
->last_access
= jiffies
;
130 return usb_stor_bulk_transfer_buf(us
, pipe
, data
, len
, NULL
);
133 /* check if card inserted, if there is, update read_only status
134 * return non zero if no card
137 static int sddr55_status(struct us_data
*us
)
140 unsigned char *command
= us
->iobuf
;
141 unsigned char *status
= us
->iobuf
;
142 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
145 memset(command
, 0, 8);
148 result
= sddr55_bulk_transport(us
,
149 DMA_TO_DEVICE
, command
, 8);
151 usb_stor_dbg(us
, "Result for send_command in status %d\n", result
);
153 if (result
!= USB_STOR_XFER_GOOD
) {
154 set_sense_info (4, 0, 0); /* hardware error */
155 return USB_STOR_TRANSPORT_ERROR
;
158 result
= sddr55_bulk_transport(us
,
159 DMA_FROM_DEVICE
, status
, 4);
161 /* expect to get short transfer if no card fitted */
162 if (result
== USB_STOR_XFER_SHORT
|| result
== USB_STOR_XFER_STALLED
) {
163 /* had a short transfer, no card inserted, free map memory */
164 kfree(info
->lba_to_pba
);
165 kfree(info
->pba_to_lba
);
166 info
->lba_to_pba
= NULL
;
167 info
->pba_to_lba
= NULL
;
169 info
->fatal_error
= 0;
170 info
->force_read_only
= 0;
172 set_sense_info (2, 0x3a, 0); /* not ready, medium not present */
173 return USB_STOR_TRANSPORT_FAILED
;
176 if (result
!= USB_STOR_XFER_GOOD
) {
177 set_sense_info (4, 0, 0); /* hardware error */
178 return USB_STOR_TRANSPORT_FAILED
;
181 /* check write protect status */
182 info
->read_only
= (status
[0] & 0x20);
184 /* now read status */
185 result
= sddr55_bulk_transport(us
,
186 DMA_FROM_DEVICE
, status
, 2);
188 if (result
!= USB_STOR_XFER_GOOD
) {
189 set_sense_info (4, 0, 0); /* hardware error */
192 return (result
== USB_STOR_XFER_GOOD
?
193 USB_STOR_TRANSPORT_GOOD
: USB_STOR_TRANSPORT_FAILED
);
197 static int sddr55_read_data(struct us_data
*us
,
200 unsigned short sectors
) {
202 int result
= USB_STOR_TRANSPORT_GOOD
;
203 unsigned char *command
= us
->iobuf
;
204 unsigned char *status
= us
->iobuf
;
205 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
206 unsigned char *buffer
;
209 unsigned long address
;
211 unsigned short pages
;
212 unsigned int len
, offset
;
213 struct scatterlist
*sg
;
215 // Since we only read in one block at a time, we have to create
216 // a bounce buffer and move the data a piece at a time between the
217 // bounce buffer and the actual transfer buffer.
219 len
= min((unsigned int) sectors
, (unsigned int) info
->blocksize
>>
220 info
->smallpageshift
) * PAGESIZE
;
221 buffer
= kmalloc(len
, GFP_NOIO
);
223 return USB_STOR_TRANSPORT_ERROR
; /* out of memory */
229 /* have we got to end? */
230 if (lba
>= info
->max_log_blks
)
233 pba
= info
->lba_to_pba
[lba
];
235 // Read as many sectors as possible in this block
237 pages
= min((unsigned int) sectors
<< info
->smallpageshift
,
238 info
->blocksize
- page
);
239 len
= pages
<< info
->pageshift
;
241 usb_stor_dbg(us
, "Read %02X pages, from PBA %04X (LBA %04X) page %02X\n",
242 pages
, pba
, lba
, page
);
244 if (pba
== NOT_ALLOCATED
) {
245 /* no pba for this lba, fill with zeroes */
246 memset (buffer
, 0, len
);
249 address
= (pba
<< info
->blockshift
) + page
;
252 command
[1] = LSB_of(address
>>16);
253 command
[2] = LSB_of(address
>>8);
254 command
[3] = LSB_of(address
);
258 command
[6] = LSB_of(pages
<< (1 - info
->smallpageshift
));
262 result
= sddr55_bulk_transport(us
,
263 DMA_TO_DEVICE
, command
, 8);
265 usb_stor_dbg(us
, "Result for send_command in read_data %d\n",
268 if (result
!= USB_STOR_XFER_GOOD
) {
269 result
= USB_STOR_TRANSPORT_ERROR
;
274 result
= sddr55_bulk_transport(us
,
275 DMA_FROM_DEVICE
, buffer
, len
);
277 if (result
!= USB_STOR_XFER_GOOD
) {
278 result
= USB_STOR_TRANSPORT_ERROR
;
282 /* now read status */
283 result
= sddr55_bulk_transport(us
,
284 DMA_FROM_DEVICE
, status
, 2);
286 if (result
!= USB_STOR_XFER_GOOD
) {
287 result
= USB_STOR_TRANSPORT_ERROR
;
291 /* check status for error */
292 if (status
[0] == 0xff && status
[1] == 0x4) {
293 set_sense_info (3, 0x11, 0);
294 result
= USB_STOR_TRANSPORT_FAILED
;
299 // Store the data in the transfer buffer
300 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
301 &sg
, &offset
, TO_XFER_BUF
);
305 sectors
-= pages
>> info
->smallpageshift
;
308 result
= USB_STOR_TRANSPORT_GOOD
;
316 static int sddr55_write_data(struct us_data
*us
,
319 unsigned short sectors
) {
321 int result
= USB_STOR_TRANSPORT_GOOD
;
322 unsigned char *command
= us
->iobuf
;
323 unsigned char *status
= us
->iobuf
;
324 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
325 unsigned char *buffer
;
328 unsigned int new_pba
;
329 unsigned long address
;
331 unsigned short pages
;
333 unsigned int len
, offset
;
334 struct scatterlist
*sg
;
336 /* check if we are allowed to write */
337 if (info
->read_only
|| info
->force_read_only
) {
338 set_sense_info (7, 0x27, 0); /* read only */
339 return USB_STOR_TRANSPORT_FAILED
;
342 // Since we only write one block at a time, we have to create
343 // a bounce buffer and move the data a piece at a time between the
344 // bounce buffer and the actual transfer buffer.
346 len
= min((unsigned int) sectors
, (unsigned int) info
->blocksize
>>
347 info
->smallpageshift
) * PAGESIZE
;
348 buffer
= kmalloc(len
, GFP_NOIO
);
350 return USB_STOR_TRANSPORT_ERROR
;
354 while (sectors
> 0) {
356 /* have we got to end? */
357 if (lba
>= info
->max_log_blks
)
360 pba
= info
->lba_to_pba
[lba
];
362 // Write as many sectors as possible in this block
364 pages
= min((unsigned int) sectors
<< info
->smallpageshift
,
365 info
->blocksize
- page
);
366 len
= pages
<< info
->pageshift
;
368 // Get the data from the transfer buffer
369 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
370 &sg
, &offset
, FROM_XFER_BUF
);
372 usb_stor_dbg(us
, "Write %02X pages, to PBA %04X (LBA %04X) page %02X\n",
373 pages
, pba
, lba
, page
);
377 if (pba
== NOT_ALLOCATED
) {
378 /* no pba allocated for this lba, find a free pba to use */
380 int max_pba
= (info
->max_log_blks
/ 250 ) * 256;
384 /* set pba to first block in zone lba is in */
385 pba
= (lba
/ 1000) * 1024;
387 usb_stor_dbg(us
, "No PBA for LBA %04X\n", lba
);
393 * Scan through the map looking for an unused block
394 * leave 16 unused blocks at start (or as many as
395 * possible) since the sddr55 seems to reuse a used
396 * block when it shouldn't if we don't leave space.
398 for (i
= 0; i
< max_pba
; i
++, pba
++) {
399 if (info
->pba_to_lba
[pba
] == UNUSED_BLOCK
) {
401 if (found_count
++ > 16)
410 usb_stor_dbg(us
, "Couldn't find unallocated block\n");
412 set_sense_info (3, 0x31, 0); /* medium error */
413 result
= USB_STOR_TRANSPORT_FAILED
;
417 usb_stor_dbg(us
, "Allocating PBA %04X for LBA %04X\n",
420 /* set writing to unallocated block flag */
424 address
= (pba
<< info
->blockshift
) + page
;
426 command
[1] = LSB_of(address
>>16);
427 command
[2] = LSB_of(address
>>8);
428 command
[3] = LSB_of(address
);
430 /* set the lba into the command, modulo 1000 */
431 command
[0] = LSB_of(lba
% 1000);
432 command
[6] = MSB_of(lba
% 1000);
434 command
[4] |= LSB_of(pages
>> info
->smallpageshift
);
439 result
= sddr55_bulk_transport(us
,
440 DMA_TO_DEVICE
, command
, 8);
442 if (result
!= USB_STOR_XFER_GOOD
) {
443 usb_stor_dbg(us
, "Result for send_command in write_data %d\n",
446 /* set_sense_info is superfluous here? */
447 set_sense_info (3, 0x3, 0);/* peripheral write error */
448 result
= USB_STOR_TRANSPORT_FAILED
;
453 result
= sddr55_bulk_transport(us
,
454 DMA_TO_DEVICE
, buffer
, len
);
456 if (result
!= USB_STOR_XFER_GOOD
) {
457 usb_stor_dbg(us
, "Result for send_data in write_data %d\n",
460 /* set_sense_info is superfluous here? */
461 set_sense_info (3, 0x3, 0);/* peripheral write error */
462 result
= USB_STOR_TRANSPORT_FAILED
;
466 /* now read status */
467 result
= sddr55_bulk_transport(us
, DMA_FROM_DEVICE
, status
, 6);
469 if (result
!= USB_STOR_XFER_GOOD
) {
470 usb_stor_dbg(us
, "Result for get_status in write_data %d\n",
473 /* set_sense_info is superfluous here? */
474 set_sense_info (3, 0x3, 0);/* peripheral write error */
475 result
= USB_STOR_TRANSPORT_FAILED
;
479 new_pba
= (status
[3] + (status
[4] << 8) + (status
[5] << 16))
482 /* check status for error */
483 if (status
[0] == 0xff && status
[1] == 0x4) {
484 info
->pba_to_lba
[new_pba
] = BAD_BLOCK
;
486 set_sense_info (3, 0x0c, 0);
487 result
= USB_STOR_TRANSPORT_FAILED
;
491 usb_stor_dbg(us
, "Updating maps for LBA %04X: old PBA %04X, new PBA %04X\n",
494 /* update the lba<->pba maps, note new_pba might be the same as pba */
495 info
->lba_to_pba
[lba
] = new_pba
;
496 info
->pba_to_lba
[pba
] = UNUSED_BLOCK
;
498 /* check that new_pba wasn't already being used */
499 if (info
->pba_to_lba
[new_pba
] != UNUSED_BLOCK
) {
500 printk(KERN_ERR
"sddr55 error: new PBA %04X already in use for LBA %04X\n",
501 new_pba
, info
->pba_to_lba
[new_pba
]);
502 info
->fatal_error
= 1;
503 set_sense_info (3, 0x31, 0);
504 result
= USB_STOR_TRANSPORT_FAILED
;
508 /* update the pba<->lba maps for new_pba */
509 info
->pba_to_lba
[new_pba
] = lba
% 1000;
513 sectors
-= pages
>> info
->smallpageshift
;
515 result
= USB_STOR_TRANSPORT_GOOD
;
522 static int sddr55_read_deviceID(struct us_data
*us
,
523 unsigned char *manufacturerID
,
524 unsigned char *deviceID
) {
527 unsigned char *command
= us
->iobuf
;
528 unsigned char *content
= us
->iobuf
;
530 memset(command
, 0, 8);
533 result
= sddr55_bulk_transport(us
, DMA_TO_DEVICE
, command
, 8);
535 usb_stor_dbg(us
, "Result of send_control for device ID is %d\n",
538 if (result
!= USB_STOR_XFER_GOOD
)
539 return USB_STOR_TRANSPORT_ERROR
;
541 result
= sddr55_bulk_transport(us
,
542 DMA_FROM_DEVICE
, content
, 4);
544 if (result
!= USB_STOR_XFER_GOOD
)
545 return USB_STOR_TRANSPORT_ERROR
;
547 *manufacturerID
= content
[0];
548 *deviceID
= content
[1];
550 if (content
[0] != 0xff) {
551 result
= sddr55_bulk_transport(us
,
552 DMA_FROM_DEVICE
, content
, 2);
555 return USB_STOR_TRANSPORT_GOOD
;
559 static int sddr55_reset(struct us_data
*us
)
565 static unsigned long sddr55_get_capacity(struct us_data
*us
) {
567 unsigned char uninitialized_var(manufacturerID
);
568 unsigned char uninitialized_var(deviceID
);
570 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
572 usb_stor_dbg(us
, "Reading capacity...\n");
574 result
= sddr55_read_deviceID(us
,
578 usb_stor_dbg(us
, "Result of read_deviceID is %d\n", result
);
580 if (result
!= USB_STOR_XFER_GOOD
)
583 usb_stor_dbg(us
, "Device ID = %02X\n", deviceID
);
584 usb_stor_dbg(us
, "Manuf ID = %02X\n", manufacturerID
);
587 info
->smallpageshift
= 0;
588 info
->blocksize
= 16;
589 info
->blockshift
= 4;
590 info
->blockmask
= 15;
598 info
->smallpageshift
= 1;
604 info
->smallpageshift
= 1;
605 case 0x5d: // 5d is a ROM card with pagesize 512.
619 info
->blocksize
= 32;
620 info
->blockshift
= 5;
621 info
->blockmask
= 31;
625 info
->blocksize
= 32;
626 info
->blockshift
= 5;
627 info
->blockmask
= 31;
631 info
->blocksize
= 32;
632 info
->blockshift
= 5;
633 info
->blockmask
= 31;
637 info
->blocksize
= 32;
638 info
->blockshift
= 5;
639 info
->blockmask
= 31;
648 static int sddr55_read_map(struct us_data
*us
) {
650 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)(us
->extra
);
652 unsigned char *buffer
;
653 unsigned char *command
= us
->iobuf
;
656 unsigned short max_lba
;
662 numblocks
= info
->capacity
>> (info
->blockshift
+ info
->pageshift
);
664 buffer
= kmalloc( numblocks
* 2, GFP_NOIO
);
669 memset(command
, 0, 8);
671 command
[6] = numblocks
* 2 / 256;
674 result
= sddr55_bulk_transport(us
, DMA_TO_DEVICE
, command
, 8);
676 if ( result
!= USB_STOR_XFER_GOOD
) {
681 result
= sddr55_bulk_transport(us
, DMA_FROM_DEVICE
, buffer
, numblocks
* 2);
683 if ( result
!= USB_STOR_XFER_GOOD
) {
688 result
= sddr55_bulk_transport(us
, DMA_FROM_DEVICE
, command
, 2);
690 if ( result
!= USB_STOR_XFER_GOOD
) {
695 kfree(info
->lba_to_pba
);
696 kfree(info
->pba_to_lba
);
697 info
->lba_to_pba
= kmalloc(numblocks
*sizeof(int), GFP_NOIO
);
698 info
->pba_to_lba
= kmalloc(numblocks
*sizeof(int), GFP_NOIO
);
700 if (info
->lba_to_pba
== NULL
|| info
->pba_to_lba
== NULL
) {
701 kfree(info
->lba_to_pba
);
702 kfree(info
->pba_to_lba
);
703 info
->lba_to_pba
= NULL
;
704 info
->pba_to_lba
= NULL
;
709 memset(info
->lba_to_pba
, 0xff, numblocks
*sizeof(int));
710 memset(info
->pba_to_lba
, 0xff, numblocks
*sizeof(int));
712 /* set maximum lba */
713 max_lba
= info
->max_log_blks
;
717 // Each block is 64 bytes of control data, so block i is located in
718 // scatterlist block i*64/128k = i*(2^6)*(2^-17) = i*(2^-11)
720 for (i
=0; i
<numblocks
; i
++) {
723 lba
= short_pack(buffer
[i
* 2], buffer
[i
* 2 + 1]);
725 /* Every 1024 physical blocks ("zone"), the LBA numbers
726 * go back to zero, but are within a higher
727 * block of LBA's. Also, there is a maximum of
728 * 1000 LBA's per zone. In other words, in PBA
729 * 1024-2047 you will find LBA 0-999 which are
730 * really LBA 1000-1999. Yes, this wastes 24
731 * physical blocks per zone. Go figure.
732 * These devices can have blocks go bad, so there
733 * are 24 spare blocks to use when blocks do go bad.
736 /* SDDR55 returns 0xffff for a bad block, and 0x400 for the
737 * CIS block. (Is this true for cards 8MB or less??)
738 * Record these in the physical to logical map
741 info
->pba_to_lba
[i
] = lba
;
743 if (lba
>= max_lba
) {
747 if (info
->lba_to_pba
[lba
+ zone
* 1000] != NOT_ALLOCATED
&&
748 !info
->force_read_only
) {
750 "sddr55: map inconsistency at LBA %04X\n",
752 info
->force_read_only
= 1;
755 if (lba
<0x10 || (lba
>=0x3E0 && lba
<0x3EF))
756 usb_stor_dbg(us
, "LBA %04X <-> PBA %04X\n", lba
, i
);
758 info
->lba_to_pba
[lba
+ zone
* 1000] = i
;
766 static void sddr55_card_info_destructor(void *extra
) {
767 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)extra
;
772 kfree(info
->lba_to_pba
);
773 kfree(info
->pba_to_lba
);
778 * Transport for the Sandisk SDDR-55
780 static int sddr55_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
783 static unsigned char inquiry_response
[8] = {
784 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
786 // write-protected for now, no block descriptor support
787 static unsigned char mode_page_01
[20] = {
788 0x0, 0x12, 0x00, 0x80, 0x0, 0x0, 0x0, 0x0,
790 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
792 unsigned char *ptr
= us
->iobuf
;
793 unsigned long capacity
;
797 unsigned short pages
;
798 struct sddr55_card_info
*info
;
802 sizeof(struct sddr55_card_info
), GFP_NOIO
);
804 return USB_STOR_TRANSPORT_ERROR
;
805 us
->extra_destructor
= sddr55_card_info_destructor
;
808 info
= (struct sddr55_card_info
*)(us
->extra
);
810 if (srb
->cmnd
[0] == REQUEST_SENSE
) {
811 usb_stor_dbg(us
, "request sense %02x/%02x/%02x\n",
813 info
->sense_data
[12],
814 info
->sense_data
[13]);
816 memcpy (ptr
, info
->sense_data
, sizeof info
->sense_data
);
819 usb_stor_set_xfer_buf (ptr
, sizeof info
->sense_data
, srb
);
820 memset (info
->sense_data
, 0, sizeof info
->sense_data
);
822 return USB_STOR_TRANSPORT_GOOD
;
825 memset (info
->sense_data
, 0, sizeof info
->sense_data
);
827 /* Dummy up a response for INQUIRY since SDDR55 doesn't
828 respond to INQUIRY commands */
830 if (srb
->cmnd
[0] == INQUIRY
) {
831 memcpy(ptr
, inquiry_response
, 8);
832 fill_inquiry_response(us
, ptr
, 36);
833 return USB_STOR_TRANSPORT_GOOD
;
836 /* only check card status if the map isn't allocated, ie no card seen yet
837 * or if it's been over half a second since we last accessed it
839 if (info
->lba_to_pba
== NULL
|| time_after(jiffies
, info
->last_access
+ HZ
/2)) {
841 /* check to see if a card is fitted */
842 result
= sddr55_status (us
);
844 result
= sddr55_status (us
);
846 set_sense_info (6, 0x28, 0); /* new media, set unit attention, not ready to ready */
848 return USB_STOR_TRANSPORT_FAILED
;
852 /* if we detected a problem with the map when writing,
853 don't allow any more access */
854 if (info
->fatal_error
) {
856 set_sense_info (3, 0x31, 0);
857 return USB_STOR_TRANSPORT_FAILED
;
860 if (srb
->cmnd
[0] == READ_CAPACITY
) {
862 capacity
= sddr55_get_capacity(us
);
865 set_sense_info (3, 0x30, 0); /* incompatible medium */
866 return USB_STOR_TRANSPORT_FAILED
;
869 info
->capacity
= capacity
;
871 /* figure out the maximum logical block number, allowing for
872 * the fact that only 250 out of every 256 are used */
873 info
->max_log_blks
= ((info
->capacity
>> (info
->pageshift
+ info
->blockshift
)) / 256) * 250;
875 /* Last page in the card, adjust as we only use 250 out of
877 capacity
= (capacity
/ 256) * 250;
879 capacity
/= PAGESIZE
;
882 ((__be32
*) ptr
)[0] = cpu_to_be32(capacity
);
883 ((__be32
*) ptr
)[1] = cpu_to_be32(PAGESIZE
);
884 usb_stor_set_xfer_buf(ptr
, 8, srb
);
888 return USB_STOR_TRANSPORT_GOOD
;
891 if (srb
->cmnd
[0] == MODE_SENSE_10
) {
893 memcpy(ptr
, mode_page_01
, sizeof mode_page_01
);
894 ptr
[3] = (info
->read_only
|| info
->force_read_only
) ? 0x80 : 0;
895 usb_stor_set_xfer_buf(ptr
, sizeof(mode_page_01
), srb
);
897 if ( (srb
->cmnd
[2] & 0x3F) == 0x01 ) {
898 usb_stor_dbg(us
, "Dummy up request for mode page 1\n");
899 return USB_STOR_TRANSPORT_GOOD
;
901 } else if ( (srb
->cmnd
[2] & 0x3F) == 0x3F ) {
902 usb_stor_dbg(us
, "Dummy up request for all mode pages\n");
903 return USB_STOR_TRANSPORT_GOOD
;
906 set_sense_info (5, 0x24, 0); /* invalid field in command */
907 return USB_STOR_TRANSPORT_FAILED
;
910 if (srb
->cmnd
[0] == ALLOW_MEDIUM_REMOVAL
) {
912 usb_stor_dbg(us
, "%s medium removal. Not that I can do anything about it...\n",
913 (srb
->cmnd
[4]&0x03) ? "Prevent" : "Allow");
915 return USB_STOR_TRANSPORT_GOOD
;
919 if (srb
->cmnd
[0] == READ_10
|| srb
->cmnd
[0] == WRITE_10
) {
921 page
= short_pack(srb
->cmnd
[3], srb
->cmnd
[2]);
923 page
|= short_pack(srb
->cmnd
[5], srb
->cmnd
[4]);
924 pages
= short_pack(srb
->cmnd
[8], srb
->cmnd
[7]);
926 page
<<= info
->smallpageshift
;
928 // convert page to block and page-within-block
930 lba
= page
>> info
->blockshift
;
931 page
= page
& info
->blockmask
;
933 // locate physical block corresponding to logical block
935 if (lba
>= info
->max_log_blks
) {
937 usb_stor_dbg(us
, "Error: Requested LBA %04X exceeds maximum block %04X\n",
938 lba
, info
->max_log_blks
- 1);
940 set_sense_info (5, 0x24, 0); /* invalid field in command */
942 return USB_STOR_TRANSPORT_FAILED
;
945 pba
= info
->lba_to_pba
[lba
];
947 if (srb
->cmnd
[0] == WRITE_10
) {
948 usb_stor_dbg(us
, "WRITE_10: write block %04X (LBA %04X) page %01X pages %d\n",
949 pba
, lba
, page
, pages
);
951 return sddr55_write_data(us
, lba
, page
, pages
);
953 usb_stor_dbg(us
, "READ_10: read block %04X (LBA %04X) page %01X pages %d\n",
954 pba
, lba
, page
, pages
);
956 return sddr55_read_data(us
, lba
, page
, pages
);
961 if (srb
->cmnd
[0] == TEST_UNIT_READY
) {
962 return USB_STOR_TRANSPORT_GOOD
;
965 if (srb
->cmnd
[0] == START_STOP
) {
966 return USB_STOR_TRANSPORT_GOOD
;
969 set_sense_info (5, 0x20, 0); /* illegal command */
971 return USB_STOR_TRANSPORT_FAILED
; // FIXME: sense buffer?
974 static struct scsi_host_template sddr55_host_template
;
976 static int sddr55_probe(struct usb_interface
*intf
,
977 const struct usb_device_id
*id
)
982 result
= usb_stor_probe1(&us
, intf
, id
,
983 (id
- sddr55_usb_ids
) + sddr55_unusual_dev_list
,
984 &sddr55_host_template
);
988 us
->transport_name
= "SDDR55";
989 us
->transport
= sddr55_transport
;
990 us
->transport_reset
= sddr55_reset
;
993 result
= usb_stor_probe2(us
);
997 static struct usb_driver sddr55_driver
= {
999 .probe
= sddr55_probe
,
1000 .disconnect
= usb_stor_disconnect
,
1001 .suspend
= usb_stor_suspend
,
1002 .resume
= usb_stor_resume
,
1003 .reset_resume
= usb_stor_reset_resume
,
1004 .pre_reset
= usb_stor_pre_reset
,
1005 .post_reset
= usb_stor_post_reset
,
1006 .id_table
= sddr55_usb_ids
,
1011 module_usb_stor_driver(sddr55_driver
, sddr55_host_template
, DRV_NAME
);