1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for SanDisk SDDR-55 SmartMedia reader
9 * Current development and maintenance by:
10 * (c) 2002 Simon Munton
13 #include <linux/jiffies.h>
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
18 #include <scsi/scsi.h>
19 #include <scsi/scsi_cmnd.h>
22 #include "transport.h"
27 #define DRV_NAME "ums-sddr55"
29 MODULE_DESCRIPTION("Driver for SanDisk SDDR-55 SmartMedia reader");
30 MODULE_AUTHOR("Simon Munton");
31 MODULE_LICENSE("GPL");
32 MODULE_IMPORT_NS(USB_STORAGE
);
35 * The table of devices
37 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
38 vendorName, productName, useProtocol, useTransport, \
39 initFunction, flags) \
40 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
41 .driver_info = (flags) }
43 static struct usb_device_id sddr55_usb_ids
[] = {
44 # include "unusual_sddr55.h"
45 { } /* Terminating entry */
47 MODULE_DEVICE_TABLE(usb
, sddr55_usb_ids
);
54 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
55 vendor_name, product_name, use_protocol, use_transport, \
56 init_function, Flags) \
58 .vendorName = vendor_name, \
59 .productName = product_name, \
60 .useProtocol = use_protocol, \
61 .useTransport = use_transport, \
62 .initFunction = init_function, \
65 static struct us_unusual_dev sddr55_unusual_dev_list
[] = {
66 # include "unusual_sddr55.h"
67 { } /* Terminating entry */
73 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
74 #define LSB_of(s) ((s)&0xFF)
75 #define MSB_of(s) ((s)>>8)
78 #define set_sense_info(sk, asc, ascq) \
80 info->sense_data[2] = sk; \
81 info->sense_data[12] = asc; \
82 info->sense_data[13] = ascq; \
86 struct sddr55_card_info
{
87 unsigned long capacity
; /* Size of card in bytes */
88 int max_log_blks
; /* maximum number of logical blocks */
89 int pageshift
; /* log2 of pagesize */
90 int smallpageshift
; /* 1 if pagesize == 256 */
91 int blocksize
; /* Size of block in pages */
92 int blockshift
; /* log2 of blocksize */
93 int blockmask
; /* 2^blockshift - 1 */
94 int read_only
; /* non zero if card is write protected */
95 int force_read_only
; /* non zero if we find a map error*/
96 int *lba_to_pba
; /* logical to physical map */
97 int *pba_to_lba
; /* physical to logical map */
98 int fatal_error
; /* set if we detect something nasty */
99 unsigned long last_access
; /* number of jiffies since we last talked to device */
100 unsigned char sense_data
[18];
104 #define NOT_ALLOCATED 0xffffffff
105 #define BAD_BLOCK 0xffff
106 #define CIS_BLOCK 0x400
107 #define UNUSED_BLOCK 0x3ff
110 sddr55_bulk_transport(struct us_data
*us
, int direction
,
111 unsigned char *data
, unsigned int len
) {
112 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
113 unsigned int pipe
= (direction
== DMA_FROM_DEVICE
) ?
114 us
->recv_bulk_pipe
: us
->send_bulk_pipe
;
117 return USB_STOR_XFER_GOOD
;
118 info
->last_access
= jiffies
;
119 return usb_stor_bulk_transfer_buf(us
, pipe
, data
, len
, NULL
);
123 * check if card inserted, if there is, update read_only status
124 * return non zero if no card
127 static int sddr55_status(struct us_data
*us
)
130 unsigned char *command
= us
->iobuf
;
131 unsigned char *status
= us
->iobuf
;
132 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
135 memset(command
, 0, 8);
138 result
= sddr55_bulk_transport(us
,
139 DMA_TO_DEVICE
, command
, 8);
141 usb_stor_dbg(us
, "Result for send_command in status %d\n", result
);
143 if (result
!= USB_STOR_XFER_GOOD
) {
144 set_sense_info (4, 0, 0); /* hardware error */
145 return USB_STOR_TRANSPORT_ERROR
;
148 result
= sddr55_bulk_transport(us
,
149 DMA_FROM_DEVICE
, status
, 4);
151 /* expect to get short transfer if no card fitted */
152 if (result
== USB_STOR_XFER_SHORT
|| result
== USB_STOR_XFER_STALLED
) {
153 /* had a short transfer, no card inserted, free map memory */
154 kfree(info
->lba_to_pba
);
155 kfree(info
->pba_to_lba
);
156 info
->lba_to_pba
= NULL
;
157 info
->pba_to_lba
= NULL
;
159 info
->fatal_error
= 0;
160 info
->force_read_only
= 0;
162 set_sense_info (2, 0x3a, 0); /* not ready, medium not present */
163 return USB_STOR_TRANSPORT_FAILED
;
166 if (result
!= USB_STOR_XFER_GOOD
) {
167 set_sense_info (4, 0, 0); /* hardware error */
168 return USB_STOR_TRANSPORT_FAILED
;
171 /* check write protect status */
172 info
->read_only
= (status
[0] & 0x20);
174 /* now read status */
175 result
= sddr55_bulk_transport(us
,
176 DMA_FROM_DEVICE
, status
, 2);
178 if (result
!= USB_STOR_XFER_GOOD
) {
179 set_sense_info (4, 0, 0); /* hardware error */
182 return (result
== USB_STOR_XFER_GOOD
?
183 USB_STOR_TRANSPORT_GOOD
: USB_STOR_TRANSPORT_FAILED
);
187 static int sddr55_read_data(struct us_data
*us
,
190 unsigned short sectors
) {
192 int result
= USB_STOR_TRANSPORT_GOOD
;
193 unsigned char *command
= us
->iobuf
;
194 unsigned char *status
= us
->iobuf
;
195 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
196 unsigned char *buffer
;
199 unsigned long address
;
201 unsigned short pages
;
202 unsigned int len
, offset
;
203 struct scatterlist
*sg
;
205 // Since we only read in one block at a time, we have to create
206 // a bounce buffer and move the data a piece at a time between the
207 // bounce buffer and the actual transfer buffer.
209 len
= min((unsigned int) sectors
, (unsigned int) info
->blocksize
>>
210 info
->smallpageshift
) * PAGESIZE
;
211 buffer
= kmalloc(len
, GFP_NOIO
);
213 return USB_STOR_TRANSPORT_ERROR
; /* out of memory */
219 /* have we got to end? */
220 if (lba
>= info
->max_log_blks
)
223 pba
= info
->lba_to_pba
[lba
];
225 // Read as many sectors as possible in this block
227 pages
= min((unsigned int) sectors
<< info
->smallpageshift
,
228 info
->blocksize
- page
);
229 len
= pages
<< info
->pageshift
;
231 usb_stor_dbg(us
, "Read %02X pages, from PBA %04X (LBA %04X) page %02X\n",
232 pages
, pba
, lba
, page
);
234 if (pba
== NOT_ALLOCATED
) {
235 /* no pba for this lba, fill with zeroes */
236 memset (buffer
, 0, len
);
239 address
= (pba
<< info
->blockshift
) + page
;
242 command
[1] = LSB_of(address
>>16);
243 command
[2] = LSB_of(address
>>8);
244 command
[3] = LSB_of(address
);
248 command
[6] = LSB_of(pages
<< (1 - info
->smallpageshift
));
252 result
= sddr55_bulk_transport(us
,
253 DMA_TO_DEVICE
, command
, 8);
255 usb_stor_dbg(us
, "Result for send_command in read_data %d\n",
258 if (result
!= USB_STOR_XFER_GOOD
) {
259 result
= USB_STOR_TRANSPORT_ERROR
;
264 result
= sddr55_bulk_transport(us
,
265 DMA_FROM_DEVICE
, buffer
, len
);
267 if (result
!= USB_STOR_XFER_GOOD
) {
268 result
= USB_STOR_TRANSPORT_ERROR
;
272 /* now read status */
273 result
= sddr55_bulk_transport(us
,
274 DMA_FROM_DEVICE
, status
, 2);
276 if (result
!= USB_STOR_XFER_GOOD
) {
277 result
= USB_STOR_TRANSPORT_ERROR
;
281 /* check status for error */
282 if (status
[0] == 0xff && status
[1] == 0x4) {
283 set_sense_info (3, 0x11, 0);
284 result
= USB_STOR_TRANSPORT_FAILED
;
289 // Store the data in the transfer buffer
290 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
291 &sg
, &offset
, TO_XFER_BUF
);
295 sectors
-= pages
>> info
->smallpageshift
;
298 result
= USB_STOR_TRANSPORT_GOOD
;
306 static int sddr55_write_data(struct us_data
*us
,
309 unsigned short sectors
) {
311 int result
= USB_STOR_TRANSPORT_GOOD
;
312 unsigned char *command
= us
->iobuf
;
313 unsigned char *status
= us
->iobuf
;
314 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
315 unsigned char *buffer
;
318 unsigned int new_pba
;
319 unsigned long address
;
321 unsigned short pages
;
323 unsigned int len
, offset
;
324 struct scatterlist
*sg
;
326 /* check if we are allowed to write */
327 if (info
->read_only
|| info
->force_read_only
) {
328 set_sense_info (7, 0x27, 0); /* read only */
329 return USB_STOR_TRANSPORT_FAILED
;
332 // Since we only write one block at a time, we have to create
333 // a bounce buffer and move the data a piece at a time between the
334 // bounce buffer and the actual transfer buffer.
336 len
= min((unsigned int) sectors
, (unsigned int) info
->blocksize
>>
337 info
->smallpageshift
) * PAGESIZE
;
338 buffer
= kmalloc(len
, GFP_NOIO
);
340 return USB_STOR_TRANSPORT_ERROR
;
344 while (sectors
> 0) {
346 /* have we got to end? */
347 if (lba
>= info
->max_log_blks
)
350 pba
= info
->lba_to_pba
[lba
];
352 // Write as many sectors as possible in this block
354 pages
= min((unsigned int) sectors
<< info
->smallpageshift
,
355 info
->blocksize
- page
);
356 len
= pages
<< info
->pageshift
;
358 // Get the data from the transfer buffer
359 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
360 &sg
, &offset
, FROM_XFER_BUF
);
362 usb_stor_dbg(us
, "Write %02X pages, to PBA %04X (LBA %04X) page %02X\n",
363 pages
, pba
, lba
, page
);
367 if (pba
== NOT_ALLOCATED
) {
368 /* no pba allocated for this lba, find a free pba to use */
370 int max_pba
= (info
->max_log_blks
/ 250 ) * 256;
374 /* set pba to first block in zone lba is in */
375 pba
= (lba
/ 1000) * 1024;
377 usb_stor_dbg(us
, "No PBA for LBA %04X\n", lba
);
383 * Scan through the map looking for an unused block
384 * leave 16 unused blocks at start (or as many as
385 * possible) since the sddr55 seems to reuse a used
386 * block when it shouldn't if we don't leave space.
388 for (i
= 0; i
< max_pba
; i
++, pba
++) {
389 if (info
->pba_to_lba
[pba
] == UNUSED_BLOCK
) {
391 if (found_count
++ > 16)
400 usb_stor_dbg(us
, "Couldn't find unallocated block\n");
402 set_sense_info (3, 0x31, 0); /* medium error */
403 result
= USB_STOR_TRANSPORT_FAILED
;
407 usb_stor_dbg(us
, "Allocating PBA %04X for LBA %04X\n",
410 /* set writing to unallocated block flag */
414 address
= (pba
<< info
->blockshift
) + page
;
416 command
[1] = LSB_of(address
>>16);
417 command
[2] = LSB_of(address
>>8);
418 command
[3] = LSB_of(address
);
420 /* set the lba into the command, modulo 1000 */
421 command
[0] = LSB_of(lba
% 1000);
422 command
[6] = MSB_of(lba
% 1000);
424 command
[4] |= LSB_of(pages
>> info
->smallpageshift
);
429 result
= sddr55_bulk_transport(us
,
430 DMA_TO_DEVICE
, command
, 8);
432 if (result
!= USB_STOR_XFER_GOOD
) {
433 usb_stor_dbg(us
, "Result for send_command in write_data %d\n",
436 /* set_sense_info is superfluous here? */
437 set_sense_info (3, 0x3, 0);/* peripheral write error */
438 result
= USB_STOR_TRANSPORT_FAILED
;
443 result
= sddr55_bulk_transport(us
,
444 DMA_TO_DEVICE
, buffer
, len
);
446 if (result
!= USB_STOR_XFER_GOOD
) {
447 usb_stor_dbg(us
, "Result for send_data in write_data %d\n",
450 /* set_sense_info is superfluous here? */
451 set_sense_info (3, 0x3, 0);/* peripheral write error */
452 result
= USB_STOR_TRANSPORT_FAILED
;
456 /* now read status */
457 result
= sddr55_bulk_transport(us
, DMA_FROM_DEVICE
, status
, 6);
459 if (result
!= USB_STOR_XFER_GOOD
) {
460 usb_stor_dbg(us
, "Result for get_status in write_data %d\n",
463 /* set_sense_info is superfluous here? */
464 set_sense_info (3, 0x3, 0);/* peripheral write error */
465 result
= USB_STOR_TRANSPORT_FAILED
;
469 new_pba
= (status
[3] + (status
[4] << 8) + (status
[5] << 16))
472 /* check status for error */
473 if (status
[0] == 0xff && status
[1] == 0x4) {
474 info
->pba_to_lba
[new_pba
] = BAD_BLOCK
;
476 set_sense_info (3, 0x0c, 0);
477 result
= USB_STOR_TRANSPORT_FAILED
;
481 usb_stor_dbg(us
, "Updating maps for LBA %04X: old PBA %04X, new PBA %04X\n",
484 /* update the lba<->pba maps, note new_pba might be the same as pba */
485 info
->lba_to_pba
[lba
] = new_pba
;
486 info
->pba_to_lba
[pba
] = UNUSED_BLOCK
;
488 /* check that new_pba wasn't already being used */
489 if (info
->pba_to_lba
[new_pba
] != UNUSED_BLOCK
) {
490 printk(KERN_ERR
"sddr55 error: new PBA %04X already in use for LBA %04X\n",
491 new_pba
, info
->pba_to_lba
[new_pba
]);
492 info
->fatal_error
= 1;
493 set_sense_info (3, 0x31, 0);
494 result
= USB_STOR_TRANSPORT_FAILED
;
498 /* update the pba<->lba maps for new_pba */
499 info
->pba_to_lba
[new_pba
] = lba
% 1000;
503 sectors
-= pages
>> info
->smallpageshift
;
505 result
= USB_STOR_TRANSPORT_GOOD
;
512 static int sddr55_read_deviceID(struct us_data
*us
,
513 unsigned char *manufacturerID
,
514 unsigned char *deviceID
) {
517 unsigned char *command
= us
->iobuf
;
518 unsigned char *content
= us
->iobuf
;
520 memset(command
, 0, 8);
523 result
= sddr55_bulk_transport(us
, DMA_TO_DEVICE
, command
, 8);
525 usb_stor_dbg(us
, "Result of send_control for device ID is %d\n",
528 if (result
!= USB_STOR_XFER_GOOD
)
529 return USB_STOR_TRANSPORT_ERROR
;
531 result
= sddr55_bulk_transport(us
,
532 DMA_FROM_DEVICE
, content
, 4);
534 if (result
!= USB_STOR_XFER_GOOD
)
535 return USB_STOR_TRANSPORT_ERROR
;
537 *manufacturerID
= content
[0];
538 *deviceID
= content
[1];
540 if (content
[0] != 0xff) {
541 result
= sddr55_bulk_transport(us
,
542 DMA_FROM_DEVICE
, content
, 2);
545 return USB_STOR_TRANSPORT_GOOD
;
549 static int sddr55_reset(struct us_data
*us
)
555 static unsigned long sddr55_get_capacity(struct us_data
*us
) {
557 unsigned char uninitialized_var(manufacturerID
);
558 unsigned char uninitialized_var(deviceID
);
560 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
562 usb_stor_dbg(us
, "Reading capacity...\n");
564 result
= sddr55_read_deviceID(us
,
568 usb_stor_dbg(us
, "Result of read_deviceID is %d\n", result
);
570 if (result
!= USB_STOR_XFER_GOOD
)
573 usb_stor_dbg(us
, "Device ID = %02X\n", deviceID
);
574 usb_stor_dbg(us
, "Manuf ID = %02X\n", manufacturerID
);
577 info
->smallpageshift
= 0;
578 info
->blocksize
= 16;
579 info
->blockshift
= 4;
580 info
->blockmask
= 15;
588 info
->smallpageshift
= 1;
594 info
->smallpageshift
= 1;
596 case 0x5d: // 5d is a ROM card with pagesize 512.
610 info
->blocksize
= 32;
611 info
->blockshift
= 5;
612 info
->blockmask
= 31;
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;
639 static int sddr55_read_map(struct us_data
*us
) {
641 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)(us
->extra
);
643 unsigned char *buffer
;
644 unsigned char *command
= us
->iobuf
;
647 unsigned short max_lba
;
653 numblocks
= info
->capacity
>> (info
->blockshift
+ info
->pageshift
);
655 buffer
= kmalloc_array(numblocks
, 2, GFP_NOIO
);
660 memset(command
, 0, 8);
662 command
[6] = numblocks
* 2 / 256;
665 result
= sddr55_bulk_transport(us
, DMA_TO_DEVICE
, command
, 8);
667 if ( result
!= USB_STOR_XFER_GOOD
) {
672 result
= sddr55_bulk_transport(us
, DMA_FROM_DEVICE
, buffer
, numblocks
* 2);
674 if ( result
!= USB_STOR_XFER_GOOD
) {
679 result
= sddr55_bulk_transport(us
, DMA_FROM_DEVICE
, command
, 2);
681 if ( result
!= USB_STOR_XFER_GOOD
) {
686 kfree(info
->lba_to_pba
);
687 kfree(info
->pba_to_lba
);
688 info
->lba_to_pba
= kmalloc_array(numblocks
, sizeof(int), GFP_NOIO
);
689 info
->pba_to_lba
= kmalloc_array(numblocks
, sizeof(int), GFP_NOIO
);
691 if (info
->lba_to_pba
== NULL
|| info
->pba_to_lba
== NULL
) {
692 kfree(info
->lba_to_pba
);
693 kfree(info
->pba_to_lba
);
694 info
->lba_to_pba
= NULL
;
695 info
->pba_to_lba
= NULL
;
700 memset(info
->lba_to_pba
, 0xff, numblocks
*sizeof(int));
701 memset(info
->pba_to_lba
, 0xff, numblocks
*sizeof(int));
703 /* set maximum lba */
704 max_lba
= info
->max_log_blks
;
709 * Each block is 64 bytes of control data, so block i is located in
710 * scatterlist block i*64/128k = i*(2^6)*(2^-17) = i*(2^-11)
713 for (i
=0; i
<numblocks
; i
++) {
716 lba
= short_pack(buffer
[i
* 2], buffer
[i
* 2 + 1]);
719 * Every 1024 physical blocks ("zone"), the LBA numbers
720 * go back to zero, but are within a higher
721 * block of LBA's. Also, there is a maximum of
722 * 1000 LBA's per zone. In other words, in PBA
723 * 1024-2047 you will find LBA 0-999 which are
724 * really LBA 1000-1999. Yes, this wastes 24
725 * physical blocks per zone. Go figure.
726 * These devices can have blocks go bad, so there
727 * are 24 spare blocks to use when blocks do go bad.
731 * SDDR55 returns 0xffff for a bad block, and 0x400 for the
732 * CIS block. (Is this true for cards 8MB or less??)
733 * Record these in the physical to logical map
736 info
->pba_to_lba
[i
] = lba
;
738 if (lba
>= max_lba
) {
742 if (info
->lba_to_pba
[lba
+ zone
* 1000] != NOT_ALLOCATED
&&
743 !info
->force_read_only
) {
745 "sddr55: map inconsistency at LBA %04X\n",
747 info
->force_read_only
= 1;
750 if (lba
<0x10 || (lba
>=0x3E0 && lba
<0x3EF))
751 usb_stor_dbg(us
, "LBA %04X <-> PBA %04X\n", lba
, i
);
753 info
->lba_to_pba
[lba
+ zone
* 1000] = i
;
761 static void sddr55_card_info_destructor(void *extra
) {
762 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)extra
;
767 kfree(info
->lba_to_pba
);
768 kfree(info
->pba_to_lba
);
773 * Transport for the Sandisk SDDR-55
775 static int sddr55_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
778 static unsigned char inquiry_response
[8] = {
779 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
781 // write-protected for now, no block descriptor support
782 static unsigned char mode_page_01
[20] = {
783 0x0, 0x12, 0x00, 0x80, 0x0, 0x0, 0x0, 0x0,
785 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
787 unsigned char *ptr
= us
->iobuf
;
788 unsigned long capacity
;
792 unsigned short pages
;
793 struct sddr55_card_info
*info
;
797 sizeof(struct sddr55_card_info
), GFP_NOIO
);
799 return USB_STOR_TRANSPORT_ERROR
;
800 us
->extra_destructor
= sddr55_card_info_destructor
;
803 info
= (struct sddr55_card_info
*)(us
->extra
);
805 if (srb
->cmnd
[0] == REQUEST_SENSE
) {
806 usb_stor_dbg(us
, "request sense %02x/%02x/%02x\n",
808 info
->sense_data
[12],
809 info
->sense_data
[13]);
811 memcpy (ptr
, info
->sense_data
, sizeof info
->sense_data
);
814 usb_stor_set_xfer_buf (ptr
, sizeof info
->sense_data
, srb
);
815 memset (info
->sense_data
, 0, sizeof info
->sense_data
);
817 return USB_STOR_TRANSPORT_GOOD
;
820 memset (info
->sense_data
, 0, sizeof info
->sense_data
);
823 * Dummy up a response for INQUIRY since SDDR55 doesn't
824 * 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
;
834 * only check card status if the map isn't allocated, ie no card seen yet
835 * or if it's been over half a second since we last accessed it
837 if (info
->lba_to_pba
== NULL
|| time_after(jiffies
, info
->last_access
+ HZ
/2)) {
839 /* check to see if a card is fitted */
840 result
= sddr55_status (us
);
842 result
= sddr55_status (us
);
844 set_sense_info (6, 0x28, 0); /* new media, set unit attention, not ready to ready */
846 return USB_STOR_TRANSPORT_FAILED
;
851 * if we detected a problem with the map when writing,
852 * 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
;
872 * figure out the maximum logical block number, allowing for
873 * the fact that only 250 out of every 256 are used
875 info
->max_log_blks
= ((info
->capacity
>> (info
->pageshift
+ info
->blockshift
)) / 256) * 250;
878 * Last page in the card, adjust as we only use 250 out of
881 capacity
= (capacity
/ 256) * 250;
883 capacity
/= PAGESIZE
;
886 ((__be32
*) ptr
)[0] = cpu_to_be32(capacity
);
887 ((__be32
*) ptr
)[1] = cpu_to_be32(PAGESIZE
);
888 usb_stor_set_xfer_buf(ptr
, 8, srb
);
892 return USB_STOR_TRANSPORT_GOOD
;
895 if (srb
->cmnd
[0] == MODE_SENSE_10
) {
897 memcpy(ptr
, mode_page_01
, sizeof mode_page_01
);
898 ptr
[3] = (info
->read_only
|| info
->force_read_only
) ? 0x80 : 0;
899 usb_stor_set_xfer_buf(ptr
, sizeof(mode_page_01
), srb
);
901 if ( (srb
->cmnd
[2] & 0x3F) == 0x01 ) {
902 usb_stor_dbg(us
, "Dummy up request for mode page 1\n");
903 return USB_STOR_TRANSPORT_GOOD
;
905 } else if ( (srb
->cmnd
[2] & 0x3F) == 0x3F ) {
906 usb_stor_dbg(us
, "Dummy up request for all mode pages\n");
907 return USB_STOR_TRANSPORT_GOOD
;
910 set_sense_info (5, 0x24, 0); /* invalid field in command */
911 return USB_STOR_TRANSPORT_FAILED
;
914 if (srb
->cmnd
[0] == ALLOW_MEDIUM_REMOVAL
) {
916 usb_stor_dbg(us
, "%s medium removal. Not that I can do anything about it...\n",
917 (srb
->cmnd
[4]&0x03) ? "Prevent" : "Allow");
919 return USB_STOR_TRANSPORT_GOOD
;
923 if (srb
->cmnd
[0] == READ_10
|| srb
->cmnd
[0] == WRITE_10
) {
925 page
= short_pack(srb
->cmnd
[3], srb
->cmnd
[2]);
927 page
|= short_pack(srb
->cmnd
[5], srb
->cmnd
[4]);
928 pages
= short_pack(srb
->cmnd
[8], srb
->cmnd
[7]);
930 page
<<= info
->smallpageshift
;
932 // convert page to block and page-within-block
934 lba
= page
>> info
->blockshift
;
935 page
= page
& info
->blockmask
;
937 // locate physical block corresponding to logical block
939 if (lba
>= info
->max_log_blks
) {
941 usb_stor_dbg(us
, "Error: Requested LBA %04X exceeds maximum block %04X\n",
942 lba
, info
->max_log_blks
- 1);
944 set_sense_info (5, 0x24, 0); /* invalid field in command */
946 return USB_STOR_TRANSPORT_FAILED
;
949 pba
= info
->lba_to_pba
[lba
];
951 if (srb
->cmnd
[0] == WRITE_10
) {
952 usb_stor_dbg(us
, "WRITE_10: write block %04X (LBA %04X) page %01X pages %d\n",
953 pba
, lba
, page
, pages
);
955 return sddr55_write_data(us
, lba
, page
, pages
);
957 usb_stor_dbg(us
, "READ_10: read block %04X (LBA %04X) page %01X pages %d\n",
958 pba
, lba
, page
, pages
);
960 return sddr55_read_data(us
, lba
, page
, pages
);
965 if (srb
->cmnd
[0] == TEST_UNIT_READY
) {
966 return USB_STOR_TRANSPORT_GOOD
;
969 if (srb
->cmnd
[0] == START_STOP
) {
970 return USB_STOR_TRANSPORT_GOOD
;
973 set_sense_info (5, 0x20, 0); /* illegal command */
975 return USB_STOR_TRANSPORT_FAILED
; // FIXME: sense buffer?
978 static struct scsi_host_template sddr55_host_template
;
980 static int sddr55_probe(struct usb_interface
*intf
,
981 const struct usb_device_id
*id
)
986 result
= usb_stor_probe1(&us
, intf
, id
,
987 (id
- sddr55_usb_ids
) + sddr55_unusual_dev_list
,
988 &sddr55_host_template
);
992 us
->transport_name
= "SDDR55";
993 us
->transport
= sddr55_transport
;
994 us
->transport_reset
= sddr55_reset
;
997 result
= usb_stor_probe2(us
);
1001 static struct usb_driver sddr55_driver
= {
1003 .probe
= sddr55_probe
,
1004 .disconnect
= usb_stor_disconnect
,
1005 .suspend
= usb_stor_suspend
,
1006 .resume
= usb_stor_resume
,
1007 .reset_resume
= usb_stor_reset_resume
,
1008 .pre_reset
= usb_stor_pre_reset
,
1009 .post_reset
= usb_stor_post_reset
,
1010 .id_table
= sddr55_usb_ids
,
1015 module_usb_stor_driver(sddr55_driver
, sddr55_host_template
, DRV_NAME
);