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"
38 MODULE_DESCRIPTION("Driver for SanDisk SDDR-55 SmartMedia reader");
39 MODULE_AUTHOR("Simon Munton");
40 MODULE_LICENSE("GPL");
43 * The table of devices
45 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
46 vendorName, productName, useProtocol, useTransport, \
47 initFunction, flags) \
48 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
49 .driver_info = (flags) }
51 static struct usb_device_id sddr55_usb_ids
[] = {
52 # include "unusual_sddr55.h"
53 { } /* Terminating entry */
55 MODULE_DEVICE_TABLE(usb
, sddr55_usb_ids
);
62 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
63 vendor_name, product_name, use_protocol, use_transport, \
64 init_function, Flags) \
66 .vendorName = vendor_name, \
67 .productName = product_name, \
68 .useProtocol = use_protocol, \
69 .useTransport = use_transport, \
70 .initFunction = init_function, \
73 static struct us_unusual_dev sddr55_unusual_dev_list
[] = {
74 # include "unusual_sddr55.h"
75 { } /* Terminating entry */
81 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
82 #define LSB_of(s) ((s)&0xFF)
83 #define MSB_of(s) ((s)>>8)
86 #define set_sense_info(sk, asc, ascq) \
88 info->sense_data[2] = sk; \
89 info->sense_data[12] = asc; \
90 info->sense_data[13] = ascq; \
94 struct sddr55_card_info
{
95 unsigned long capacity
; /* Size of card in bytes */
96 int max_log_blks
; /* maximum number of logical blocks */
97 int pageshift
; /* log2 of pagesize */
98 int smallpageshift
; /* 1 if pagesize == 256 */
99 int blocksize
; /* Size of block in pages */
100 int blockshift
; /* log2 of blocksize */
101 int blockmask
; /* 2^blockshift - 1 */
102 int read_only
; /* non zero if card is write protected */
103 int force_read_only
; /* non zero if we find a map error*/
104 int *lba_to_pba
; /* logical to physical map */
105 int *pba_to_lba
; /* physical to logical map */
106 int fatal_error
; /* set if we detect something nasty */
107 unsigned long last_access
; /* number of jiffies since we last talked to device */
108 unsigned char sense_data
[18];
112 #define NOT_ALLOCATED 0xffffffff
113 #define BAD_BLOCK 0xffff
114 #define CIS_BLOCK 0x400
115 #define UNUSED_BLOCK 0x3ff
118 sddr55_bulk_transport(struct us_data
*us
, int direction
,
119 unsigned char *data
, unsigned int len
) {
120 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
121 unsigned int pipe
= (direction
== DMA_FROM_DEVICE
) ?
122 us
->recv_bulk_pipe
: us
->send_bulk_pipe
;
125 return USB_STOR_XFER_GOOD
;
126 info
->last_access
= jiffies
;
127 return usb_stor_bulk_transfer_buf(us
, pipe
, data
, len
, NULL
);
130 /* check if card inserted, if there is, update read_only status
131 * return non zero if no card
134 static int sddr55_status(struct us_data
*us
)
137 unsigned char *command
= us
->iobuf
;
138 unsigned char *status
= us
->iobuf
;
139 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
142 memset(command
, 0, 8);
145 result
= sddr55_bulk_transport(us
,
146 DMA_TO_DEVICE
, command
, 8);
148 usb_stor_dbg(us
, "Result for send_command in status %d\n", result
);
150 if (result
!= USB_STOR_XFER_GOOD
) {
151 set_sense_info (4, 0, 0); /* hardware error */
152 return USB_STOR_TRANSPORT_ERROR
;
155 result
= sddr55_bulk_transport(us
,
156 DMA_FROM_DEVICE
, status
, 4);
158 /* expect to get short transfer if no card fitted */
159 if (result
== USB_STOR_XFER_SHORT
|| result
== USB_STOR_XFER_STALLED
) {
160 /* had a short transfer, no card inserted, free map memory */
161 kfree(info
->lba_to_pba
);
162 kfree(info
->pba_to_lba
);
163 info
->lba_to_pba
= NULL
;
164 info
->pba_to_lba
= NULL
;
166 info
->fatal_error
= 0;
167 info
->force_read_only
= 0;
169 set_sense_info (2, 0x3a, 0); /* not ready, medium not present */
170 return USB_STOR_TRANSPORT_FAILED
;
173 if (result
!= USB_STOR_XFER_GOOD
) {
174 set_sense_info (4, 0, 0); /* hardware error */
175 return USB_STOR_TRANSPORT_FAILED
;
178 /* check write protect status */
179 info
->read_only
= (status
[0] & 0x20);
181 /* now read status */
182 result
= sddr55_bulk_transport(us
,
183 DMA_FROM_DEVICE
, status
, 2);
185 if (result
!= USB_STOR_XFER_GOOD
) {
186 set_sense_info (4, 0, 0); /* hardware error */
189 return (result
== USB_STOR_XFER_GOOD
?
190 USB_STOR_TRANSPORT_GOOD
: USB_STOR_TRANSPORT_FAILED
);
194 static int sddr55_read_data(struct us_data
*us
,
197 unsigned short sectors
) {
199 int result
= USB_STOR_TRANSPORT_GOOD
;
200 unsigned char *command
= us
->iobuf
;
201 unsigned char *status
= us
->iobuf
;
202 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
203 unsigned char *buffer
;
206 unsigned long address
;
208 unsigned short pages
;
209 unsigned int len
, offset
;
210 struct scatterlist
*sg
;
212 // Since we only read in one block at a time, we have to create
213 // a bounce buffer and move the data a piece at a time between the
214 // bounce buffer and the actual transfer buffer.
216 len
= min((unsigned int) sectors
, (unsigned int) info
->blocksize
>>
217 info
->smallpageshift
) * PAGESIZE
;
218 buffer
= kmalloc(len
, GFP_NOIO
);
220 return USB_STOR_TRANSPORT_ERROR
; /* out of memory */
226 /* have we got to end? */
227 if (lba
>= info
->max_log_blks
)
230 pba
= info
->lba_to_pba
[lba
];
232 // Read as many sectors as possible in this block
234 pages
= min((unsigned int) sectors
<< info
->smallpageshift
,
235 info
->blocksize
- page
);
236 len
= pages
<< info
->pageshift
;
238 usb_stor_dbg(us
, "Read %02X pages, from PBA %04X (LBA %04X) page %02X\n",
239 pages
, pba
, lba
, page
);
241 if (pba
== NOT_ALLOCATED
) {
242 /* no pba for this lba, fill with zeroes */
243 memset (buffer
, 0, len
);
246 address
= (pba
<< info
->blockshift
) + page
;
249 command
[1] = LSB_of(address
>>16);
250 command
[2] = LSB_of(address
>>8);
251 command
[3] = LSB_of(address
);
255 command
[6] = LSB_of(pages
<< (1 - info
->smallpageshift
));
259 result
= sddr55_bulk_transport(us
,
260 DMA_TO_DEVICE
, command
, 8);
262 usb_stor_dbg(us
, "Result for send_command in read_data %d\n",
265 if (result
!= USB_STOR_XFER_GOOD
) {
266 result
= USB_STOR_TRANSPORT_ERROR
;
271 result
= sddr55_bulk_transport(us
,
272 DMA_FROM_DEVICE
, buffer
, len
);
274 if (result
!= USB_STOR_XFER_GOOD
) {
275 result
= USB_STOR_TRANSPORT_ERROR
;
279 /* now read status */
280 result
= sddr55_bulk_transport(us
,
281 DMA_FROM_DEVICE
, status
, 2);
283 if (result
!= USB_STOR_XFER_GOOD
) {
284 result
= USB_STOR_TRANSPORT_ERROR
;
288 /* check status for error */
289 if (status
[0] == 0xff && status
[1] == 0x4) {
290 set_sense_info (3, 0x11, 0);
291 result
= USB_STOR_TRANSPORT_FAILED
;
296 // Store the data in the transfer buffer
297 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
298 &sg
, &offset
, TO_XFER_BUF
);
302 sectors
-= pages
>> info
->smallpageshift
;
305 result
= USB_STOR_TRANSPORT_GOOD
;
313 static int sddr55_write_data(struct us_data
*us
,
316 unsigned short sectors
) {
318 int result
= USB_STOR_TRANSPORT_GOOD
;
319 unsigned char *command
= us
->iobuf
;
320 unsigned char *status
= us
->iobuf
;
321 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
322 unsigned char *buffer
;
325 unsigned int new_pba
;
326 unsigned long address
;
328 unsigned short pages
;
330 unsigned int len
, offset
;
331 struct scatterlist
*sg
;
333 /* check if we are allowed to write */
334 if (info
->read_only
|| info
->force_read_only
) {
335 set_sense_info (7, 0x27, 0); /* read only */
336 return USB_STOR_TRANSPORT_FAILED
;
339 // Since we only write one block at a time, we have to create
340 // a bounce buffer and move the data a piece at a time between the
341 // bounce buffer and the actual transfer buffer.
343 len
= min((unsigned int) sectors
, (unsigned int) info
->blocksize
>>
344 info
->smallpageshift
) * PAGESIZE
;
345 buffer
= kmalloc(len
, GFP_NOIO
);
347 return USB_STOR_TRANSPORT_ERROR
;
351 while (sectors
> 0) {
353 /* have we got to end? */
354 if (lba
>= info
->max_log_blks
)
357 pba
= info
->lba_to_pba
[lba
];
359 // Write as many sectors as possible in this block
361 pages
= min((unsigned int) sectors
<< info
->smallpageshift
,
362 info
->blocksize
- page
);
363 len
= pages
<< info
->pageshift
;
365 // Get the data from the transfer buffer
366 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
367 &sg
, &offset
, FROM_XFER_BUF
);
369 usb_stor_dbg(us
, "Write %02X pages, to PBA %04X (LBA %04X) page %02X\n",
370 pages
, pba
, lba
, page
);
374 if (pba
== NOT_ALLOCATED
) {
375 /* no pba allocated for this lba, find a free pba to use */
377 int max_pba
= (info
->max_log_blks
/ 250 ) * 256;
381 /* set pba to first block in zone lba is in */
382 pba
= (lba
/ 1000) * 1024;
384 usb_stor_dbg(us
, "No PBA for LBA %04X\n", lba
);
390 * Scan through the map looking for an unused block
391 * leave 16 unused blocks at start (or as many as
392 * possible) since the sddr55 seems to reuse a used
393 * block when it shouldn't if we don't leave space.
395 for (i
= 0; i
< max_pba
; i
++, pba
++) {
396 if (info
->pba_to_lba
[pba
] == UNUSED_BLOCK
) {
398 if (found_count
++ > 16)
407 usb_stor_dbg(us
, "Couldn't find unallocated block\n");
409 set_sense_info (3, 0x31, 0); /* medium error */
410 result
= USB_STOR_TRANSPORT_FAILED
;
414 usb_stor_dbg(us
, "Allocating PBA %04X for LBA %04X\n",
417 /* set writing to unallocated block flag */
421 address
= (pba
<< info
->blockshift
) + page
;
423 command
[1] = LSB_of(address
>>16);
424 command
[2] = LSB_of(address
>>8);
425 command
[3] = LSB_of(address
);
427 /* set the lba into the command, modulo 1000 */
428 command
[0] = LSB_of(lba
% 1000);
429 command
[6] = MSB_of(lba
% 1000);
431 command
[4] |= LSB_of(pages
>> info
->smallpageshift
);
436 result
= sddr55_bulk_transport(us
,
437 DMA_TO_DEVICE
, command
, 8);
439 if (result
!= USB_STOR_XFER_GOOD
) {
440 usb_stor_dbg(us
, "Result for send_command in write_data %d\n",
443 /* set_sense_info is superfluous here? */
444 set_sense_info (3, 0x3, 0);/* peripheral write error */
445 result
= USB_STOR_TRANSPORT_FAILED
;
450 result
= sddr55_bulk_transport(us
,
451 DMA_TO_DEVICE
, buffer
, len
);
453 if (result
!= USB_STOR_XFER_GOOD
) {
454 usb_stor_dbg(us
, "Result for send_data in write_data %d\n",
457 /* set_sense_info is superfluous here? */
458 set_sense_info (3, 0x3, 0);/* peripheral write error */
459 result
= USB_STOR_TRANSPORT_FAILED
;
463 /* now read status */
464 result
= sddr55_bulk_transport(us
, DMA_FROM_DEVICE
, status
, 6);
466 if (result
!= USB_STOR_XFER_GOOD
) {
467 usb_stor_dbg(us
, "Result for get_status in write_data %d\n",
470 /* set_sense_info is superfluous here? */
471 set_sense_info (3, 0x3, 0);/* peripheral write error */
472 result
= USB_STOR_TRANSPORT_FAILED
;
476 new_pba
= (status
[3] + (status
[4] << 8) + (status
[5] << 16))
479 /* check status for error */
480 if (status
[0] == 0xff && status
[1] == 0x4) {
481 info
->pba_to_lba
[new_pba
] = BAD_BLOCK
;
483 set_sense_info (3, 0x0c, 0);
484 result
= USB_STOR_TRANSPORT_FAILED
;
488 usb_stor_dbg(us
, "Updating maps for LBA %04X: old PBA %04X, new PBA %04X\n",
491 /* update the lba<->pba maps, note new_pba might be the same as pba */
492 info
->lba_to_pba
[lba
] = new_pba
;
493 info
->pba_to_lba
[pba
] = UNUSED_BLOCK
;
495 /* check that new_pba wasn't already being used */
496 if (info
->pba_to_lba
[new_pba
] != UNUSED_BLOCK
) {
497 printk(KERN_ERR
"sddr55 error: new PBA %04X already in use for LBA %04X\n",
498 new_pba
, info
->pba_to_lba
[new_pba
]);
499 info
->fatal_error
= 1;
500 set_sense_info (3, 0x31, 0);
501 result
= USB_STOR_TRANSPORT_FAILED
;
505 /* update the pba<->lba maps for new_pba */
506 info
->pba_to_lba
[new_pba
] = lba
% 1000;
510 sectors
-= pages
>> info
->smallpageshift
;
512 result
= USB_STOR_TRANSPORT_GOOD
;
519 static int sddr55_read_deviceID(struct us_data
*us
,
520 unsigned char *manufacturerID
,
521 unsigned char *deviceID
) {
524 unsigned char *command
= us
->iobuf
;
525 unsigned char *content
= us
->iobuf
;
527 memset(command
, 0, 8);
530 result
= sddr55_bulk_transport(us
, DMA_TO_DEVICE
, command
, 8);
532 usb_stor_dbg(us
, "Result of send_control for device ID is %d\n",
535 if (result
!= USB_STOR_XFER_GOOD
)
536 return USB_STOR_TRANSPORT_ERROR
;
538 result
= sddr55_bulk_transport(us
,
539 DMA_FROM_DEVICE
, content
, 4);
541 if (result
!= USB_STOR_XFER_GOOD
)
542 return USB_STOR_TRANSPORT_ERROR
;
544 *manufacturerID
= content
[0];
545 *deviceID
= content
[1];
547 if (content
[0] != 0xff) {
548 result
= sddr55_bulk_transport(us
,
549 DMA_FROM_DEVICE
, content
, 2);
552 return USB_STOR_TRANSPORT_GOOD
;
556 static int sddr55_reset(struct us_data
*us
)
562 static unsigned long sddr55_get_capacity(struct us_data
*us
) {
564 unsigned char uninitialized_var(manufacturerID
);
565 unsigned char uninitialized_var(deviceID
);
567 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
569 usb_stor_dbg(us
, "Reading capacity...\n");
571 result
= sddr55_read_deviceID(us
,
575 usb_stor_dbg(us
, "Result of read_deviceID is %d\n", result
);
577 if (result
!= USB_STOR_XFER_GOOD
)
580 usb_stor_dbg(us
, "Device ID = %02X\n", deviceID
);
581 usb_stor_dbg(us
, "Manuf ID = %02X\n", manufacturerID
);
584 info
->smallpageshift
= 0;
585 info
->blocksize
= 16;
586 info
->blockshift
= 4;
587 info
->blockmask
= 15;
595 info
->smallpageshift
= 1;
601 info
->smallpageshift
= 1;
602 case 0x5d: // 5d is a ROM card with pagesize 512.
616 info
->blocksize
= 32;
617 info
->blockshift
= 5;
618 info
->blockmask
= 31;
622 info
->blocksize
= 32;
623 info
->blockshift
= 5;
624 info
->blockmask
= 31;
628 info
->blocksize
= 32;
629 info
->blockshift
= 5;
630 info
->blockmask
= 31;
634 info
->blocksize
= 32;
635 info
->blockshift
= 5;
636 info
->blockmask
= 31;
645 static int sddr55_read_map(struct us_data
*us
) {
647 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)(us
->extra
);
649 unsigned char *buffer
;
650 unsigned char *command
= us
->iobuf
;
653 unsigned short max_lba
;
659 numblocks
= info
->capacity
>> (info
->blockshift
+ info
->pageshift
);
661 buffer
= kmalloc( numblocks
* 2, GFP_NOIO
);
666 memset(command
, 0, 8);
668 command
[6] = numblocks
* 2 / 256;
671 result
= sddr55_bulk_transport(us
, DMA_TO_DEVICE
, command
, 8);
673 if ( result
!= USB_STOR_XFER_GOOD
) {
678 result
= sddr55_bulk_transport(us
, DMA_FROM_DEVICE
, buffer
, numblocks
* 2);
680 if ( result
!= USB_STOR_XFER_GOOD
) {
685 result
= sddr55_bulk_transport(us
, DMA_FROM_DEVICE
, command
, 2);
687 if ( result
!= USB_STOR_XFER_GOOD
) {
692 kfree(info
->lba_to_pba
);
693 kfree(info
->pba_to_lba
);
694 info
->lba_to_pba
= kmalloc(numblocks
*sizeof(int), GFP_NOIO
);
695 info
->pba_to_lba
= kmalloc(numblocks
*sizeof(int), GFP_NOIO
);
697 if (info
->lba_to_pba
== NULL
|| info
->pba_to_lba
== NULL
) {
698 kfree(info
->lba_to_pba
);
699 kfree(info
->pba_to_lba
);
700 info
->lba_to_pba
= NULL
;
701 info
->pba_to_lba
= NULL
;
706 memset(info
->lba_to_pba
, 0xff, numblocks
*sizeof(int));
707 memset(info
->pba_to_lba
, 0xff, numblocks
*sizeof(int));
709 /* set maximum lba */
710 max_lba
= info
->max_log_blks
;
714 // Each block is 64 bytes of control data, so block i is located in
715 // scatterlist block i*64/128k = i*(2^6)*(2^-17) = i*(2^-11)
717 for (i
=0; i
<numblocks
; i
++) {
720 lba
= short_pack(buffer
[i
* 2], buffer
[i
* 2 + 1]);
722 /* Every 1024 physical blocks ("zone"), the LBA numbers
723 * go back to zero, but are within a higher
724 * block of LBA's. Also, there is a maximum of
725 * 1000 LBA's per zone. In other words, in PBA
726 * 1024-2047 you will find LBA 0-999 which are
727 * really LBA 1000-1999. Yes, this wastes 24
728 * physical blocks per zone. Go figure.
729 * These devices can have blocks go bad, so there
730 * are 24 spare blocks to use when blocks do go bad.
733 /* SDDR55 returns 0xffff for a bad block, and 0x400 for the
734 * CIS block. (Is this true for cards 8MB or less??)
735 * Record these in the physical to logical map
738 info
->pba_to_lba
[i
] = lba
;
740 if (lba
>= max_lba
) {
744 if (info
->lba_to_pba
[lba
+ zone
* 1000] != NOT_ALLOCATED
&&
745 !info
->force_read_only
) {
747 "sddr55: map inconsistency at LBA %04X\n",
749 info
->force_read_only
= 1;
752 if (lba
<0x10 || (lba
>=0x3E0 && lba
<0x3EF))
753 usb_stor_dbg(us
, "LBA %04X <-> PBA %04X\n", lba
, i
);
755 info
->lba_to_pba
[lba
+ zone
* 1000] = i
;
763 static void sddr55_card_info_destructor(void *extra
) {
764 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)extra
;
769 kfree(info
->lba_to_pba
);
770 kfree(info
->pba_to_lba
);
775 * Transport for the Sandisk SDDR-55
777 static int sddr55_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
780 static unsigned char inquiry_response
[8] = {
781 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
783 // write-protected for now, no block descriptor support
784 static unsigned char mode_page_01
[20] = {
785 0x0, 0x12, 0x00, 0x80, 0x0, 0x0, 0x0, 0x0,
787 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
789 unsigned char *ptr
= us
->iobuf
;
790 unsigned long capacity
;
794 unsigned short pages
;
795 struct sddr55_card_info
*info
;
799 sizeof(struct sddr55_card_info
), GFP_NOIO
);
801 return USB_STOR_TRANSPORT_ERROR
;
802 us
->extra_destructor
= sddr55_card_info_destructor
;
805 info
= (struct sddr55_card_info
*)(us
->extra
);
807 if (srb
->cmnd
[0] == REQUEST_SENSE
) {
808 usb_stor_dbg(us
, "request sense %02x/%02x/%02x\n",
810 info
->sense_data
[12],
811 info
->sense_data
[13]);
813 memcpy (ptr
, info
->sense_data
, sizeof info
->sense_data
);
816 usb_stor_set_xfer_buf (ptr
, sizeof info
->sense_data
, srb
);
817 memset (info
->sense_data
, 0, sizeof info
->sense_data
);
819 return USB_STOR_TRANSPORT_GOOD
;
822 memset (info
->sense_data
, 0, sizeof info
->sense_data
);
824 /* Dummy up a response for INQUIRY since SDDR55 doesn't
825 respond to INQUIRY commands */
827 if (srb
->cmnd
[0] == INQUIRY
) {
828 memcpy(ptr
, inquiry_response
, 8);
829 fill_inquiry_response(us
, ptr
, 36);
830 return USB_STOR_TRANSPORT_GOOD
;
833 /* only check card status if the map isn't allocated, ie no card seen yet
834 * or if it's been over half a second since we last accessed it
836 if (info
->lba_to_pba
== NULL
|| time_after(jiffies
, info
->last_access
+ HZ
/2)) {
838 /* check to see if a card is fitted */
839 result
= sddr55_status (us
);
841 result
= sddr55_status (us
);
843 set_sense_info (6, 0x28, 0); /* new media, set unit attention, not ready to ready */
845 return USB_STOR_TRANSPORT_FAILED
;
849 /* if we detected a problem with the map when writing,
850 don't allow any more access */
851 if (info
->fatal_error
) {
853 set_sense_info (3, 0x31, 0);
854 return USB_STOR_TRANSPORT_FAILED
;
857 if (srb
->cmnd
[0] == READ_CAPACITY
) {
859 capacity
= sddr55_get_capacity(us
);
862 set_sense_info (3, 0x30, 0); /* incompatible medium */
863 return USB_STOR_TRANSPORT_FAILED
;
866 info
->capacity
= capacity
;
868 /* figure out the maximum logical block number, allowing for
869 * the fact that only 250 out of every 256 are used */
870 info
->max_log_blks
= ((info
->capacity
>> (info
->pageshift
+ info
->blockshift
)) / 256) * 250;
872 /* Last page in the card, adjust as we only use 250 out of
874 capacity
= (capacity
/ 256) * 250;
876 capacity
/= PAGESIZE
;
879 ((__be32
*) ptr
)[0] = cpu_to_be32(capacity
);
880 ((__be32
*) ptr
)[1] = cpu_to_be32(PAGESIZE
);
881 usb_stor_set_xfer_buf(ptr
, 8, srb
);
885 return USB_STOR_TRANSPORT_GOOD
;
888 if (srb
->cmnd
[0] == MODE_SENSE_10
) {
890 memcpy(ptr
, mode_page_01
, sizeof mode_page_01
);
891 ptr
[3] = (info
->read_only
|| info
->force_read_only
) ? 0x80 : 0;
892 usb_stor_set_xfer_buf(ptr
, sizeof(mode_page_01
), srb
);
894 if ( (srb
->cmnd
[2] & 0x3F) == 0x01 ) {
895 usb_stor_dbg(us
, "Dummy up request for mode page 1\n");
896 return USB_STOR_TRANSPORT_GOOD
;
898 } else if ( (srb
->cmnd
[2] & 0x3F) == 0x3F ) {
899 usb_stor_dbg(us
, "Dummy up request for all mode pages\n");
900 return USB_STOR_TRANSPORT_GOOD
;
903 set_sense_info (5, 0x24, 0); /* invalid field in command */
904 return USB_STOR_TRANSPORT_FAILED
;
907 if (srb
->cmnd
[0] == ALLOW_MEDIUM_REMOVAL
) {
909 usb_stor_dbg(us
, "%s medium removal. Not that I can do anything about it...\n",
910 (srb
->cmnd
[4]&0x03) ? "Prevent" : "Allow");
912 return USB_STOR_TRANSPORT_GOOD
;
916 if (srb
->cmnd
[0] == READ_10
|| srb
->cmnd
[0] == WRITE_10
) {
918 page
= short_pack(srb
->cmnd
[3], srb
->cmnd
[2]);
920 page
|= short_pack(srb
->cmnd
[5], srb
->cmnd
[4]);
921 pages
= short_pack(srb
->cmnd
[8], srb
->cmnd
[7]);
923 page
<<= info
->smallpageshift
;
925 // convert page to block and page-within-block
927 lba
= page
>> info
->blockshift
;
928 page
= page
& info
->blockmask
;
930 // locate physical block corresponding to logical block
932 if (lba
>= info
->max_log_blks
) {
934 usb_stor_dbg(us
, "Error: Requested LBA %04X exceeds maximum block %04X\n",
935 lba
, info
->max_log_blks
- 1);
937 set_sense_info (5, 0x24, 0); /* invalid field in command */
939 return USB_STOR_TRANSPORT_FAILED
;
942 pba
= info
->lba_to_pba
[lba
];
944 if (srb
->cmnd
[0] == WRITE_10
) {
945 usb_stor_dbg(us
, "WRITE_10: write block %04X (LBA %04X) page %01X pages %d\n",
946 pba
, lba
, page
, pages
);
948 return sddr55_write_data(us
, lba
, page
, pages
);
950 usb_stor_dbg(us
, "READ_10: read block %04X (LBA %04X) page %01X pages %d\n",
951 pba
, lba
, page
, pages
);
953 return sddr55_read_data(us
, lba
, page
, pages
);
958 if (srb
->cmnd
[0] == TEST_UNIT_READY
) {
959 return USB_STOR_TRANSPORT_GOOD
;
962 if (srb
->cmnd
[0] == START_STOP
) {
963 return USB_STOR_TRANSPORT_GOOD
;
966 set_sense_info (5, 0x20, 0); /* illegal command */
968 return USB_STOR_TRANSPORT_FAILED
; // FIXME: sense buffer?
972 static int sddr55_probe(struct usb_interface
*intf
,
973 const struct usb_device_id
*id
)
978 result
= usb_stor_probe1(&us
, intf
, id
,
979 (id
- sddr55_usb_ids
) + sddr55_unusual_dev_list
);
983 us
->transport_name
= "SDDR55";
984 us
->transport
= sddr55_transport
;
985 us
->transport_reset
= sddr55_reset
;
988 result
= usb_stor_probe2(us
);
992 static struct usb_driver sddr55_driver
= {
993 .name
= "ums-sddr55",
994 .probe
= sddr55_probe
,
995 .disconnect
= usb_stor_disconnect
,
996 .suspend
= usb_stor_suspend
,
997 .resume
= usb_stor_resume
,
998 .reset_resume
= usb_stor_reset_resume
,
999 .pre_reset
= usb_stor_pre_reset
,
1000 .post_reset
= usb_stor_post_reset
,
1001 .id_table
= sddr55_usb_ids
,
1006 module_usb_driver(sddr55_driver
);