2 * Driver for SanDisk SDDR-55 SmartMedia reader
8 * Current development and maintenance by:
9 * (c) 2002 Simon Munton
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2, or (at your option) any
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/jiffies.h>
27 #include <linux/errno.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
35 #include "transport.h"
40 #define DRV_NAME "ums-sddr55"
42 MODULE_DESCRIPTION("Driver for SanDisk SDDR-55 SmartMedia reader");
43 MODULE_AUTHOR("Simon Munton");
44 MODULE_LICENSE("GPL");
47 * The table of devices
49 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
50 vendorName, productName, useProtocol, useTransport, \
51 initFunction, flags) \
52 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
53 .driver_info = (flags) }
55 static struct usb_device_id sddr55_usb_ids
[] = {
56 # include "unusual_sddr55.h"
57 { } /* Terminating entry */
59 MODULE_DEVICE_TABLE(usb
, sddr55_usb_ids
);
66 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
67 vendor_name, product_name, use_protocol, use_transport, \
68 init_function, Flags) \
70 .vendorName = vendor_name, \
71 .productName = product_name, \
72 .useProtocol = use_protocol, \
73 .useTransport = use_transport, \
74 .initFunction = init_function, \
77 static struct us_unusual_dev sddr55_unusual_dev_list
[] = {
78 # include "unusual_sddr55.h"
79 { } /* Terminating entry */
85 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
86 #define LSB_of(s) ((s)&0xFF)
87 #define MSB_of(s) ((s)>>8)
90 #define set_sense_info(sk, asc, ascq) \
92 info->sense_data[2] = sk; \
93 info->sense_data[12] = asc; \
94 info->sense_data[13] = ascq; \
98 struct sddr55_card_info
{
99 unsigned long capacity
; /* Size of card in bytes */
100 int max_log_blks
; /* maximum number of logical blocks */
101 int pageshift
; /* log2 of pagesize */
102 int smallpageshift
; /* 1 if pagesize == 256 */
103 int blocksize
; /* Size of block in pages */
104 int blockshift
; /* log2 of blocksize */
105 int blockmask
; /* 2^blockshift - 1 */
106 int read_only
; /* non zero if card is write protected */
107 int force_read_only
; /* non zero if we find a map error*/
108 int *lba_to_pba
; /* logical to physical map */
109 int *pba_to_lba
; /* physical to logical map */
110 int fatal_error
; /* set if we detect something nasty */
111 unsigned long last_access
; /* number of jiffies since we last talked to device */
112 unsigned char sense_data
[18];
116 #define NOT_ALLOCATED 0xffffffff
117 #define BAD_BLOCK 0xffff
118 #define CIS_BLOCK 0x400
119 #define UNUSED_BLOCK 0x3ff
122 sddr55_bulk_transport(struct us_data
*us
, int direction
,
123 unsigned char *data
, unsigned int len
) {
124 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
125 unsigned int pipe
= (direction
== DMA_FROM_DEVICE
) ?
126 us
->recv_bulk_pipe
: us
->send_bulk_pipe
;
129 return USB_STOR_XFER_GOOD
;
130 info
->last_access
= jiffies
;
131 return usb_stor_bulk_transfer_buf(us
, pipe
, data
, len
, NULL
);
135 * check if card inserted, if there is, update read_only status
136 * return non zero if no card
139 static int sddr55_status(struct us_data
*us
)
142 unsigned char *command
= us
->iobuf
;
143 unsigned char *status
= us
->iobuf
;
144 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
147 memset(command
, 0, 8);
150 result
= sddr55_bulk_transport(us
,
151 DMA_TO_DEVICE
, command
, 8);
153 usb_stor_dbg(us
, "Result for send_command in status %d\n", result
);
155 if (result
!= USB_STOR_XFER_GOOD
) {
156 set_sense_info (4, 0, 0); /* hardware error */
157 return USB_STOR_TRANSPORT_ERROR
;
160 result
= sddr55_bulk_transport(us
,
161 DMA_FROM_DEVICE
, status
, 4);
163 /* expect to get short transfer if no card fitted */
164 if (result
== USB_STOR_XFER_SHORT
|| result
== USB_STOR_XFER_STALLED
) {
165 /* had a short transfer, no card inserted, free map memory */
166 kfree(info
->lba_to_pba
);
167 kfree(info
->pba_to_lba
);
168 info
->lba_to_pba
= NULL
;
169 info
->pba_to_lba
= NULL
;
171 info
->fatal_error
= 0;
172 info
->force_read_only
= 0;
174 set_sense_info (2, 0x3a, 0); /* not ready, medium not present */
175 return USB_STOR_TRANSPORT_FAILED
;
178 if (result
!= USB_STOR_XFER_GOOD
) {
179 set_sense_info (4, 0, 0); /* hardware error */
180 return USB_STOR_TRANSPORT_FAILED
;
183 /* check write protect status */
184 info
->read_only
= (status
[0] & 0x20);
186 /* now read status */
187 result
= sddr55_bulk_transport(us
,
188 DMA_FROM_DEVICE
, status
, 2);
190 if (result
!= USB_STOR_XFER_GOOD
) {
191 set_sense_info (4, 0, 0); /* hardware error */
194 return (result
== USB_STOR_XFER_GOOD
?
195 USB_STOR_TRANSPORT_GOOD
: USB_STOR_TRANSPORT_FAILED
);
199 static int sddr55_read_data(struct us_data
*us
,
202 unsigned short sectors
) {
204 int result
= USB_STOR_TRANSPORT_GOOD
;
205 unsigned char *command
= us
->iobuf
;
206 unsigned char *status
= us
->iobuf
;
207 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
208 unsigned char *buffer
;
211 unsigned long address
;
213 unsigned short pages
;
214 unsigned int len
, offset
;
215 struct scatterlist
*sg
;
217 // Since we only read in one block at a time, we have to create
218 // a bounce buffer and move the data a piece at a time between the
219 // bounce buffer and the actual transfer buffer.
221 len
= min((unsigned int) sectors
, (unsigned int) info
->blocksize
>>
222 info
->smallpageshift
) * PAGESIZE
;
223 buffer
= kmalloc(len
, GFP_NOIO
);
225 return USB_STOR_TRANSPORT_ERROR
; /* out of memory */
231 /* have we got to end? */
232 if (lba
>= info
->max_log_blks
)
235 pba
= info
->lba_to_pba
[lba
];
237 // Read as many sectors as possible in this block
239 pages
= min((unsigned int) sectors
<< info
->smallpageshift
,
240 info
->blocksize
- page
);
241 len
= pages
<< info
->pageshift
;
243 usb_stor_dbg(us
, "Read %02X pages, from PBA %04X (LBA %04X) page %02X\n",
244 pages
, pba
, lba
, page
);
246 if (pba
== NOT_ALLOCATED
) {
247 /* no pba for this lba, fill with zeroes */
248 memset (buffer
, 0, len
);
251 address
= (pba
<< info
->blockshift
) + page
;
254 command
[1] = LSB_of(address
>>16);
255 command
[2] = LSB_of(address
>>8);
256 command
[3] = LSB_of(address
);
260 command
[6] = LSB_of(pages
<< (1 - info
->smallpageshift
));
264 result
= sddr55_bulk_transport(us
,
265 DMA_TO_DEVICE
, command
, 8);
267 usb_stor_dbg(us
, "Result for send_command in read_data %d\n",
270 if (result
!= USB_STOR_XFER_GOOD
) {
271 result
= USB_STOR_TRANSPORT_ERROR
;
276 result
= sddr55_bulk_transport(us
,
277 DMA_FROM_DEVICE
, buffer
, len
);
279 if (result
!= USB_STOR_XFER_GOOD
) {
280 result
= USB_STOR_TRANSPORT_ERROR
;
284 /* now read status */
285 result
= sddr55_bulk_transport(us
,
286 DMA_FROM_DEVICE
, status
, 2);
288 if (result
!= USB_STOR_XFER_GOOD
) {
289 result
= USB_STOR_TRANSPORT_ERROR
;
293 /* check status for error */
294 if (status
[0] == 0xff && status
[1] == 0x4) {
295 set_sense_info (3, 0x11, 0);
296 result
= USB_STOR_TRANSPORT_FAILED
;
301 // Store the data in the transfer buffer
302 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
303 &sg
, &offset
, TO_XFER_BUF
);
307 sectors
-= pages
>> info
->smallpageshift
;
310 result
= USB_STOR_TRANSPORT_GOOD
;
318 static int sddr55_write_data(struct us_data
*us
,
321 unsigned short sectors
) {
323 int result
= USB_STOR_TRANSPORT_GOOD
;
324 unsigned char *command
= us
->iobuf
;
325 unsigned char *status
= us
->iobuf
;
326 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
327 unsigned char *buffer
;
330 unsigned int new_pba
;
331 unsigned long address
;
333 unsigned short pages
;
335 unsigned int len
, offset
;
336 struct scatterlist
*sg
;
338 /* check if we are allowed to write */
339 if (info
->read_only
|| info
->force_read_only
) {
340 set_sense_info (7, 0x27, 0); /* read only */
341 return USB_STOR_TRANSPORT_FAILED
;
344 // Since we only write one block at a time, we have to create
345 // a bounce buffer and move the data a piece at a time between the
346 // bounce buffer and the actual transfer buffer.
348 len
= min((unsigned int) sectors
, (unsigned int) info
->blocksize
>>
349 info
->smallpageshift
) * PAGESIZE
;
350 buffer
= kmalloc(len
, GFP_NOIO
);
352 return USB_STOR_TRANSPORT_ERROR
;
356 while (sectors
> 0) {
358 /* have we got to end? */
359 if (lba
>= info
->max_log_blks
)
362 pba
= info
->lba_to_pba
[lba
];
364 // Write as many sectors as possible in this block
366 pages
= min((unsigned int) sectors
<< info
->smallpageshift
,
367 info
->blocksize
- page
);
368 len
= pages
<< info
->pageshift
;
370 // Get the data from the transfer buffer
371 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
372 &sg
, &offset
, FROM_XFER_BUF
);
374 usb_stor_dbg(us
, "Write %02X pages, to PBA %04X (LBA %04X) page %02X\n",
375 pages
, pba
, lba
, page
);
379 if (pba
== NOT_ALLOCATED
) {
380 /* no pba allocated for this lba, find a free pba to use */
382 int max_pba
= (info
->max_log_blks
/ 250 ) * 256;
386 /* set pba to first block in zone lba is in */
387 pba
= (lba
/ 1000) * 1024;
389 usb_stor_dbg(us
, "No PBA for LBA %04X\n", lba
);
395 * Scan through the map looking for an unused block
396 * leave 16 unused blocks at start (or as many as
397 * possible) since the sddr55 seems to reuse a used
398 * block when it shouldn't if we don't leave space.
400 for (i
= 0; i
< max_pba
; i
++, pba
++) {
401 if (info
->pba_to_lba
[pba
] == UNUSED_BLOCK
) {
403 if (found_count
++ > 16)
412 usb_stor_dbg(us
, "Couldn't find unallocated block\n");
414 set_sense_info (3, 0x31, 0); /* medium error */
415 result
= USB_STOR_TRANSPORT_FAILED
;
419 usb_stor_dbg(us
, "Allocating PBA %04X for LBA %04X\n",
422 /* set writing to unallocated block flag */
426 address
= (pba
<< info
->blockshift
) + page
;
428 command
[1] = LSB_of(address
>>16);
429 command
[2] = LSB_of(address
>>8);
430 command
[3] = LSB_of(address
);
432 /* set the lba into the command, modulo 1000 */
433 command
[0] = LSB_of(lba
% 1000);
434 command
[6] = MSB_of(lba
% 1000);
436 command
[4] |= LSB_of(pages
>> info
->smallpageshift
);
441 result
= sddr55_bulk_transport(us
,
442 DMA_TO_DEVICE
, command
, 8);
444 if (result
!= USB_STOR_XFER_GOOD
) {
445 usb_stor_dbg(us
, "Result for send_command in write_data %d\n",
448 /* set_sense_info is superfluous here? */
449 set_sense_info (3, 0x3, 0);/* peripheral write error */
450 result
= USB_STOR_TRANSPORT_FAILED
;
455 result
= sddr55_bulk_transport(us
,
456 DMA_TO_DEVICE
, buffer
, len
);
458 if (result
!= USB_STOR_XFER_GOOD
) {
459 usb_stor_dbg(us
, "Result for send_data in write_data %d\n",
462 /* set_sense_info is superfluous here? */
463 set_sense_info (3, 0x3, 0);/* peripheral write error */
464 result
= USB_STOR_TRANSPORT_FAILED
;
468 /* now read status */
469 result
= sddr55_bulk_transport(us
, DMA_FROM_DEVICE
, status
, 6);
471 if (result
!= USB_STOR_XFER_GOOD
) {
472 usb_stor_dbg(us
, "Result for get_status in write_data %d\n",
475 /* set_sense_info is superfluous here? */
476 set_sense_info (3, 0x3, 0);/* peripheral write error */
477 result
= USB_STOR_TRANSPORT_FAILED
;
481 new_pba
= (status
[3] + (status
[4] << 8) + (status
[5] << 16))
484 /* check status for error */
485 if (status
[0] == 0xff && status
[1] == 0x4) {
486 info
->pba_to_lba
[new_pba
] = BAD_BLOCK
;
488 set_sense_info (3, 0x0c, 0);
489 result
= USB_STOR_TRANSPORT_FAILED
;
493 usb_stor_dbg(us
, "Updating maps for LBA %04X: old PBA %04X, new PBA %04X\n",
496 /* update the lba<->pba maps, note new_pba might be the same as pba */
497 info
->lba_to_pba
[lba
] = new_pba
;
498 info
->pba_to_lba
[pba
] = UNUSED_BLOCK
;
500 /* check that new_pba wasn't already being used */
501 if (info
->pba_to_lba
[new_pba
] != UNUSED_BLOCK
) {
502 printk(KERN_ERR
"sddr55 error: new PBA %04X already in use for LBA %04X\n",
503 new_pba
, info
->pba_to_lba
[new_pba
]);
504 info
->fatal_error
= 1;
505 set_sense_info (3, 0x31, 0);
506 result
= USB_STOR_TRANSPORT_FAILED
;
510 /* update the pba<->lba maps for new_pba */
511 info
->pba_to_lba
[new_pba
] = lba
% 1000;
515 sectors
-= pages
>> info
->smallpageshift
;
517 result
= USB_STOR_TRANSPORT_GOOD
;
524 static int sddr55_read_deviceID(struct us_data
*us
,
525 unsigned char *manufacturerID
,
526 unsigned char *deviceID
) {
529 unsigned char *command
= us
->iobuf
;
530 unsigned char *content
= us
->iobuf
;
532 memset(command
, 0, 8);
535 result
= sddr55_bulk_transport(us
, DMA_TO_DEVICE
, command
, 8);
537 usb_stor_dbg(us
, "Result of send_control for device ID is %d\n",
540 if (result
!= USB_STOR_XFER_GOOD
)
541 return USB_STOR_TRANSPORT_ERROR
;
543 result
= sddr55_bulk_transport(us
,
544 DMA_FROM_DEVICE
, content
, 4);
546 if (result
!= USB_STOR_XFER_GOOD
)
547 return USB_STOR_TRANSPORT_ERROR
;
549 *manufacturerID
= content
[0];
550 *deviceID
= content
[1];
552 if (content
[0] != 0xff) {
553 result
= sddr55_bulk_transport(us
,
554 DMA_FROM_DEVICE
, content
, 2);
557 return USB_STOR_TRANSPORT_GOOD
;
561 static int sddr55_reset(struct us_data
*us
)
567 static unsigned long sddr55_get_capacity(struct us_data
*us
) {
569 unsigned char uninitialized_var(manufacturerID
);
570 unsigned char uninitialized_var(deviceID
);
572 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)us
->extra
;
574 usb_stor_dbg(us
, "Reading capacity...\n");
576 result
= sddr55_read_deviceID(us
,
580 usb_stor_dbg(us
, "Result of read_deviceID is %d\n", result
);
582 if (result
!= USB_STOR_XFER_GOOD
)
585 usb_stor_dbg(us
, "Device ID = %02X\n", deviceID
);
586 usb_stor_dbg(us
, "Manuf ID = %02X\n", manufacturerID
);
589 info
->smallpageshift
= 0;
590 info
->blocksize
= 16;
591 info
->blockshift
= 4;
592 info
->blockmask
= 15;
600 info
->smallpageshift
= 1;
606 info
->smallpageshift
= 1;
607 case 0x5d: // 5d is a ROM card with pagesize 512.
621 info
->blocksize
= 32;
622 info
->blockshift
= 5;
623 info
->blockmask
= 31;
627 info
->blocksize
= 32;
628 info
->blockshift
= 5;
629 info
->blockmask
= 31;
633 info
->blocksize
= 32;
634 info
->blockshift
= 5;
635 info
->blockmask
= 31;
639 info
->blocksize
= 32;
640 info
->blockshift
= 5;
641 info
->blockmask
= 31;
650 static int sddr55_read_map(struct us_data
*us
) {
652 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)(us
->extra
);
654 unsigned char *buffer
;
655 unsigned char *command
= us
->iobuf
;
658 unsigned short max_lba
;
664 numblocks
= info
->capacity
>> (info
->blockshift
+ info
->pageshift
);
666 buffer
= kmalloc( numblocks
* 2, GFP_NOIO
);
671 memset(command
, 0, 8);
673 command
[6] = numblocks
* 2 / 256;
676 result
= sddr55_bulk_transport(us
, DMA_TO_DEVICE
, command
, 8);
678 if ( result
!= USB_STOR_XFER_GOOD
) {
683 result
= sddr55_bulk_transport(us
, DMA_FROM_DEVICE
, buffer
, numblocks
* 2);
685 if ( result
!= USB_STOR_XFER_GOOD
) {
690 result
= sddr55_bulk_transport(us
, DMA_FROM_DEVICE
, command
, 2);
692 if ( result
!= USB_STOR_XFER_GOOD
) {
697 kfree(info
->lba_to_pba
);
698 kfree(info
->pba_to_lba
);
699 info
->lba_to_pba
= kmalloc(numblocks
*sizeof(int), GFP_NOIO
);
700 info
->pba_to_lba
= kmalloc(numblocks
*sizeof(int), GFP_NOIO
);
702 if (info
->lba_to_pba
== NULL
|| info
->pba_to_lba
== NULL
) {
703 kfree(info
->lba_to_pba
);
704 kfree(info
->pba_to_lba
);
705 info
->lba_to_pba
= NULL
;
706 info
->pba_to_lba
= NULL
;
711 memset(info
->lba_to_pba
, 0xff, numblocks
*sizeof(int));
712 memset(info
->pba_to_lba
, 0xff, numblocks
*sizeof(int));
714 /* set maximum lba */
715 max_lba
= info
->max_log_blks
;
720 * Each block is 64 bytes of control data, so block i is located in
721 * scatterlist block i*64/128k = i*(2^6)*(2^-17) = i*(2^-11)
724 for (i
=0; i
<numblocks
; i
++) {
727 lba
= short_pack(buffer
[i
* 2], buffer
[i
* 2 + 1]);
730 * Every 1024 physical blocks ("zone"), the LBA numbers
731 * go back to zero, but are within a higher
732 * block of LBA's. Also, there is a maximum of
733 * 1000 LBA's per zone. In other words, in PBA
734 * 1024-2047 you will find LBA 0-999 which are
735 * really LBA 1000-1999. Yes, this wastes 24
736 * physical blocks per zone. Go figure.
737 * These devices can have blocks go bad, so there
738 * are 24 spare blocks to use when blocks do go bad.
742 * SDDR55 returns 0xffff for a bad block, and 0x400 for the
743 * CIS block. (Is this true for cards 8MB or less??)
744 * Record these in the physical to logical map
747 info
->pba_to_lba
[i
] = lba
;
749 if (lba
>= max_lba
) {
753 if (info
->lba_to_pba
[lba
+ zone
* 1000] != NOT_ALLOCATED
&&
754 !info
->force_read_only
) {
756 "sddr55: map inconsistency at LBA %04X\n",
758 info
->force_read_only
= 1;
761 if (lba
<0x10 || (lba
>=0x3E0 && lba
<0x3EF))
762 usb_stor_dbg(us
, "LBA %04X <-> PBA %04X\n", lba
, i
);
764 info
->lba_to_pba
[lba
+ zone
* 1000] = i
;
772 static void sddr55_card_info_destructor(void *extra
) {
773 struct sddr55_card_info
*info
= (struct sddr55_card_info
*)extra
;
778 kfree(info
->lba_to_pba
);
779 kfree(info
->pba_to_lba
);
784 * Transport for the Sandisk SDDR-55
786 static int sddr55_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
789 static unsigned char inquiry_response
[8] = {
790 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
792 // write-protected for now, no block descriptor support
793 static unsigned char mode_page_01
[20] = {
794 0x0, 0x12, 0x00, 0x80, 0x0, 0x0, 0x0, 0x0,
796 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
798 unsigned char *ptr
= us
->iobuf
;
799 unsigned long capacity
;
803 unsigned short pages
;
804 struct sddr55_card_info
*info
;
808 sizeof(struct sddr55_card_info
), GFP_NOIO
);
810 return USB_STOR_TRANSPORT_ERROR
;
811 us
->extra_destructor
= sddr55_card_info_destructor
;
814 info
= (struct sddr55_card_info
*)(us
->extra
);
816 if (srb
->cmnd
[0] == REQUEST_SENSE
) {
817 usb_stor_dbg(us
, "request sense %02x/%02x/%02x\n",
819 info
->sense_data
[12],
820 info
->sense_data
[13]);
822 memcpy (ptr
, info
->sense_data
, sizeof info
->sense_data
);
825 usb_stor_set_xfer_buf (ptr
, sizeof info
->sense_data
, srb
);
826 memset (info
->sense_data
, 0, sizeof info
->sense_data
);
828 return USB_STOR_TRANSPORT_GOOD
;
831 memset (info
->sense_data
, 0, sizeof info
->sense_data
);
834 * Dummy up a response for INQUIRY since SDDR55 doesn't
835 * respond to INQUIRY commands
838 if (srb
->cmnd
[0] == INQUIRY
) {
839 memcpy(ptr
, inquiry_response
, 8);
840 fill_inquiry_response(us
, ptr
, 36);
841 return USB_STOR_TRANSPORT_GOOD
;
845 * only check card status if the map isn't allocated, ie no card seen yet
846 * or if it's been over half a second since we last accessed it
848 if (info
->lba_to_pba
== NULL
|| time_after(jiffies
, info
->last_access
+ HZ
/2)) {
850 /* check to see if a card is fitted */
851 result
= sddr55_status (us
);
853 result
= sddr55_status (us
);
855 set_sense_info (6, 0x28, 0); /* new media, set unit attention, not ready to ready */
857 return USB_STOR_TRANSPORT_FAILED
;
862 * if we detected a problem with the map when writing,
863 * don't allow any more access
865 if (info
->fatal_error
) {
867 set_sense_info (3, 0x31, 0);
868 return USB_STOR_TRANSPORT_FAILED
;
871 if (srb
->cmnd
[0] == READ_CAPACITY
) {
873 capacity
= sddr55_get_capacity(us
);
876 set_sense_info (3, 0x30, 0); /* incompatible medium */
877 return USB_STOR_TRANSPORT_FAILED
;
880 info
->capacity
= capacity
;
883 * figure out the maximum logical block number, allowing for
884 * the fact that only 250 out of every 256 are used
886 info
->max_log_blks
= ((info
->capacity
>> (info
->pageshift
+ info
->blockshift
)) / 256) * 250;
889 * Last page in the card, adjust as we only use 250 out of
892 capacity
= (capacity
/ 256) * 250;
894 capacity
/= PAGESIZE
;
897 ((__be32
*) ptr
)[0] = cpu_to_be32(capacity
);
898 ((__be32
*) ptr
)[1] = cpu_to_be32(PAGESIZE
);
899 usb_stor_set_xfer_buf(ptr
, 8, srb
);
903 return USB_STOR_TRANSPORT_GOOD
;
906 if (srb
->cmnd
[0] == MODE_SENSE_10
) {
908 memcpy(ptr
, mode_page_01
, sizeof mode_page_01
);
909 ptr
[3] = (info
->read_only
|| info
->force_read_only
) ? 0x80 : 0;
910 usb_stor_set_xfer_buf(ptr
, sizeof(mode_page_01
), srb
);
912 if ( (srb
->cmnd
[2] & 0x3F) == 0x01 ) {
913 usb_stor_dbg(us
, "Dummy up request for mode page 1\n");
914 return USB_STOR_TRANSPORT_GOOD
;
916 } else if ( (srb
->cmnd
[2] & 0x3F) == 0x3F ) {
917 usb_stor_dbg(us
, "Dummy up request for all mode pages\n");
918 return USB_STOR_TRANSPORT_GOOD
;
921 set_sense_info (5, 0x24, 0); /* invalid field in command */
922 return USB_STOR_TRANSPORT_FAILED
;
925 if (srb
->cmnd
[0] == ALLOW_MEDIUM_REMOVAL
) {
927 usb_stor_dbg(us
, "%s medium removal. Not that I can do anything about it...\n",
928 (srb
->cmnd
[4]&0x03) ? "Prevent" : "Allow");
930 return USB_STOR_TRANSPORT_GOOD
;
934 if (srb
->cmnd
[0] == READ_10
|| srb
->cmnd
[0] == WRITE_10
) {
936 page
= short_pack(srb
->cmnd
[3], srb
->cmnd
[2]);
938 page
|= short_pack(srb
->cmnd
[5], srb
->cmnd
[4]);
939 pages
= short_pack(srb
->cmnd
[8], srb
->cmnd
[7]);
941 page
<<= info
->smallpageshift
;
943 // convert page to block and page-within-block
945 lba
= page
>> info
->blockshift
;
946 page
= page
& info
->blockmask
;
948 // locate physical block corresponding to logical block
950 if (lba
>= info
->max_log_blks
) {
952 usb_stor_dbg(us
, "Error: Requested LBA %04X exceeds maximum block %04X\n",
953 lba
, info
->max_log_blks
- 1);
955 set_sense_info (5, 0x24, 0); /* invalid field in command */
957 return USB_STOR_TRANSPORT_FAILED
;
960 pba
= info
->lba_to_pba
[lba
];
962 if (srb
->cmnd
[0] == WRITE_10
) {
963 usb_stor_dbg(us
, "WRITE_10: write block %04X (LBA %04X) page %01X pages %d\n",
964 pba
, lba
, page
, pages
);
966 return sddr55_write_data(us
, lba
, page
, pages
);
968 usb_stor_dbg(us
, "READ_10: read block %04X (LBA %04X) page %01X pages %d\n",
969 pba
, lba
, page
, pages
);
971 return sddr55_read_data(us
, lba
, page
, pages
);
976 if (srb
->cmnd
[0] == TEST_UNIT_READY
) {
977 return USB_STOR_TRANSPORT_GOOD
;
980 if (srb
->cmnd
[0] == START_STOP
) {
981 return USB_STOR_TRANSPORT_GOOD
;
984 set_sense_info (5, 0x20, 0); /* illegal command */
986 return USB_STOR_TRANSPORT_FAILED
; // FIXME: sense buffer?
989 static struct scsi_host_template sddr55_host_template
;
991 static int sddr55_probe(struct usb_interface
*intf
,
992 const struct usb_device_id
*id
)
997 result
= usb_stor_probe1(&us
, intf
, id
,
998 (id
- sddr55_usb_ids
) + sddr55_unusual_dev_list
,
999 &sddr55_host_template
);
1003 us
->transport_name
= "SDDR55";
1004 us
->transport
= sddr55_transport
;
1005 us
->transport_reset
= sddr55_reset
;
1008 result
= usb_stor_probe2(us
);
1012 static struct usb_driver sddr55_driver
= {
1014 .probe
= sddr55_probe
,
1015 .disconnect
= usb_stor_disconnect
,
1016 .suspend
= usb_stor_suspend
,
1017 .resume
= usb_stor_resume
,
1018 .reset_resume
= usb_stor_reset_resume
,
1019 .pre_reset
= usb_stor_pre_reset
,
1020 .post_reset
= usb_stor_post_reset
,
1021 .id_table
= sddr55_usb_ids
,
1026 module_usb_stor_driver(sddr55_driver
, sddr55_host_template
, DRV_NAME
);