Linux 4.19-rc7
[linux-2.6/btrfs-unstable.git] / drivers / usb / storage / sddr55.c
blobb8527c55335b6e3a977995fa41cc8d7a3337b219
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Driver for SanDisk SDDR-55 SmartMedia reader
5 * SDDR55 driver v0.1:
7 * First release
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>
21 #include "usb.h"
22 #include "transport.h"
23 #include "protocol.h"
24 #include "debug.h"
25 #include "scsiglue.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");
34 * The table of devices
36 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
37 vendorName, productName, useProtocol, useTransport, \
38 initFunction, flags) \
39 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
40 .driver_info = (flags) }
42 static struct usb_device_id sddr55_usb_ids[] = {
43 # include "unusual_sddr55.h"
44 { } /* Terminating entry */
46 MODULE_DEVICE_TABLE(usb, sddr55_usb_ids);
48 #undef UNUSUAL_DEV
51 * The flags table
53 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
54 vendor_name, product_name, use_protocol, use_transport, \
55 init_function, Flags) \
56 { \
57 .vendorName = vendor_name, \
58 .productName = product_name, \
59 .useProtocol = use_protocol, \
60 .useTransport = use_transport, \
61 .initFunction = init_function, \
64 static struct us_unusual_dev sddr55_unusual_dev_list[] = {
65 # include "unusual_sddr55.h"
66 { } /* Terminating entry */
69 #undef UNUSUAL_DEV
72 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
73 #define LSB_of(s) ((s)&0xFF)
74 #define MSB_of(s) ((s)>>8)
75 #define PAGESIZE 512
77 #define set_sense_info(sk, asc, ascq) \
78 do { \
79 info->sense_data[2] = sk; \
80 info->sense_data[12] = asc; \
81 info->sense_data[13] = ascq; \
82 } while (0)
85 struct sddr55_card_info {
86 unsigned long capacity; /* Size of card in bytes */
87 int max_log_blks; /* maximum number of logical blocks */
88 int pageshift; /* log2 of pagesize */
89 int smallpageshift; /* 1 if pagesize == 256 */
90 int blocksize; /* Size of block in pages */
91 int blockshift; /* log2 of blocksize */
92 int blockmask; /* 2^blockshift - 1 */
93 int read_only; /* non zero if card is write protected */
94 int force_read_only; /* non zero if we find a map error*/
95 int *lba_to_pba; /* logical to physical map */
96 int *pba_to_lba; /* physical to logical map */
97 int fatal_error; /* set if we detect something nasty */
98 unsigned long last_access; /* number of jiffies since we last talked to device */
99 unsigned char sense_data[18];
103 #define NOT_ALLOCATED 0xffffffff
104 #define BAD_BLOCK 0xffff
105 #define CIS_BLOCK 0x400
106 #define UNUSED_BLOCK 0x3ff
108 static int
109 sddr55_bulk_transport(struct us_data *us, int direction,
110 unsigned char *data, unsigned int len) {
111 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
112 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
113 us->recv_bulk_pipe : us->send_bulk_pipe;
115 if (!len)
116 return USB_STOR_XFER_GOOD;
117 info->last_access = jiffies;
118 return usb_stor_bulk_transfer_buf(us, pipe, data, len, NULL);
122 * check if card inserted, if there is, update read_only status
123 * return non zero if no card
126 static int sddr55_status(struct us_data *us)
128 int result;
129 unsigned char *command = us->iobuf;
130 unsigned char *status = us->iobuf;
131 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
133 /* send command */
134 memset(command, 0, 8);
135 command[5] = 0xB0;
136 command[7] = 0x80;
137 result = sddr55_bulk_transport(us,
138 DMA_TO_DEVICE, command, 8);
140 usb_stor_dbg(us, "Result for send_command in status %d\n", result);
142 if (result != USB_STOR_XFER_GOOD) {
143 set_sense_info (4, 0, 0); /* hardware error */
144 return USB_STOR_TRANSPORT_ERROR;
147 result = sddr55_bulk_transport(us,
148 DMA_FROM_DEVICE, status, 4);
150 /* expect to get short transfer if no card fitted */
151 if (result == USB_STOR_XFER_SHORT || result == USB_STOR_XFER_STALLED) {
152 /* had a short transfer, no card inserted, free map memory */
153 kfree(info->lba_to_pba);
154 kfree(info->pba_to_lba);
155 info->lba_to_pba = NULL;
156 info->pba_to_lba = NULL;
158 info->fatal_error = 0;
159 info->force_read_only = 0;
161 set_sense_info (2, 0x3a, 0); /* not ready, medium not present */
162 return USB_STOR_TRANSPORT_FAILED;
165 if (result != USB_STOR_XFER_GOOD) {
166 set_sense_info (4, 0, 0); /* hardware error */
167 return USB_STOR_TRANSPORT_FAILED;
170 /* check write protect status */
171 info->read_only = (status[0] & 0x20);
173 /* now read status */
174 result = sddr55_bulk_transport(us,
175 DMA_FROM_DEVICE, status, 2);
177 if (result != USB_STOR_XFER_GOOD) {
178 set_sense_info (4, 0, 0); /* hardware error */
181 return (result == USB_STOR_XFER_GOOD ?
182 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_FAILED);
186 static int sddr55_read_data(struct us_data *us,
187 unsigned int lba,
188 unsigned int page,
189 unsigned short sectors) {
191 int result = USB_STOR_TRANSPORT_GOOD;
192 unsigned char *command = us->iobuf;
193 unsigned char *status = us->iobuf;
194 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
195 unsigned char *buffer;
197 unsigned int pba;
198 unsigned long address;
200 unsigned short pages;
201 unsigned int len, offset;
202 struct scatterlist *sg;
204 // Since we only read in one block at a time, we have to create
205 // a bounce buffer and move the data a piece at a time between the
206 // bounce buffer and the actual transfer buffer.
208 len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
209 info->smallpageshift) * PAGESIZE;
210 buffer = kmalloc(len, GFP_NOIO);
211 if (buffer == NULL)
212 return USB_STOR_TRANSPORT_ERROR; /* out of memory */
213 offset = 0;
214 sg = NULL;
216 while (sectors>0) {
218 /* have we got to end? */
219 if (lba >= info->max_log_blks)
220 break;
222 pba = info->lba_to_pba[lba];
224 // Read as many sectors as possible in this block
226 pages = min((unsigned int) sectors << info->smallpageshift,
227 info->blocksize - page);
228 len = pages << info->pageshift;
230 usb_stor_dbg(us, "Read %02X pages, from PBA %04X (LBA %04X) page %02X\n",
231 pages, pba, lba, page);
233 if (pba == NOT_ALLOCATED) {
234 /* no pba for this lba, fill with zeroes */
235 memset (buffer, 0, len);
236 } else {
238 address = (pba << info->blockshift) + page;
240 command[0] = 0;
241 command[1] = LSB_of(address>>16);
242 command[2] = LSB_of(address>>8);
243 command[3] = LSB_of(address);
245 command[4] = 0;
246 command[5] = 0xB0;
247 command[6] = LSB_of(pages << (1 - info->smallpageshift));
248 command[7] = 0x85;
250 /* send command */
251 result = sddr55_bulk_transport(us,
252 DMA_TO_DEVICE, command, 8);
254 usb_stor_dbg(us, "Result for send_command in read_data %d\n",
255 result);
257 if (result != USB_STOR_XFER_GOOD) {
258 result = USB_STOR_TRANSPORT_ERROR;
259 goto leave;
262 /* read data */
263 result = sddr55_bulk_transport(us,
264 DMA_FROM_DEVICE, buffer, len);
266 if (result != USB_STOR_XFER_GOOD) {
267 result = USB_STOR_TRANSPORT_ERROR;
268 goto leave;
271 /* now read status */
272 result = sddr55_bulk_transport(us,
273 DMA_FROM_DEVICE, status, 2);
275 if (result != USB_STOR_XFER_GOOD) {
276 result = USB_STOR_TRANSPORT_ERROR;
277 goto leave;
280 /* check status for error */
281 if (status[0] == 0xff && status[1] == 0x4) {
282 set_sense_info (3, 0x11, 0);
283 result = USB_STOR_TRANSPORT_FAILED;
284 goto leave;
288 // Store the data in the transfer buffer
289 usb_stor_access_xfer_buf(buffer, len, us->srb,
290 &sg, &offset, TO_XFER_BUF);
292 page = 0;
293 lba++;
294 sectors -= pages >> info->smallpageshift;
297 result = USB_STOR_TRANSPORT_GOOD;
299 leave:
300 kfree(buffer);
302 return result;
305 static int sddr55_write_data(struct us_data *us,
306 unsigned int lba,
307 unsigned int page,
308 unsigned short sectors) {
310 int result = USB_STOR_TRANSPORT_GOOD;
311 unsigned char *command = us->iobuf;
312 unsigned char *status = us->iobuf;
313 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
314 unsigned char *buffer;
316 unsigned int pba;
317 unsigned int new_pba;
318 unsigned long address;
320 unsigned short pages;
321 int i;
322 unsigned int len, offset;
323 struct scatterlist *sg;
325 /* check if we are allowed to write */
326 if (info->read_only || info->force_read_only) {
327 set_sense_info (7, 0x27, 0); /* read only */
328 return USB_STOR_TRANSPORT_FAILED;
331 // Since we only write one block at a time, we have to create
332 // a bounce buffer and move the data a piece at a time between the
333 // bounce buffer and the actual transfer buffer.
335 len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
336 info->smallpageshift) * PAGESIZE;
337 buffer = kmalloc(len, GFP_NOIO);
338 if (buffer == NULL)
339 return USB_STOR_TRANSPORT_ERROR;
340 offset = 0;
341 sg = NULL;
343 while (sectors > 0) {
345 /* have we got to end? */
346 if (lba >= info->max_log_blks)
347 break;
349 pba = info->lba_to_pba[lba];
351 // Write as many sectors as possible in this block
353 pages = min((unsigned int) sectors << info->smallpageshift,
354 info->blocksize - page);
355 len = pages << info->pageshift;
357 // Get the data from the transfer buffer
358 usb_stor_access_xfer_buf(buffer, len, us->srb,
359 &sg, &offset, FROM_XFER_BUF);
361 usb_stor_dbg(us, "Write %02X pages, to PBA %04X (LBA %04X) page %02X\n",
362 pages, pba, lba, page);
364 command[4] = 0;
366 if (pba == NOT_ALLOCATED) {
367 /* no pba allocated for this lba, find a free pba to use */
369 int max_pba = (info->max_log_blks / 250 ) * 256;
370 int found_count = 0;
371 int found_pba = -1;
373 /* set pba to first block in zone lba is in */
374 pba = (lba / 1000) * 1024;
376 usb_stor_dbg(us, "No PBA for LBA %04X\n", lba);
378 if (max_pba > 1024)
379 max_pba = 1024;
382 * Scan through the map looking for an unused block
383 * leave 16 unused blocks at start (or as many as
384 * possible) since the sddr55 seems to reuse a used
385 * block when it shouldn't if we don't leave space.
387 for (i = 0; i < max_pba; i++, pba++) {
388 if (info->pba_to_lba[pba] == UNUSED_BLOCK) {
389 found_pba = pba;
390 if (found_count++ > 16)
391 break;
395 pba = found_pba;
397 if (pba == -1) {
398 /* oh dear */
399 usb_stor_dbg(us, "Couldn't find unallocated block\n");
401 set_sense_info (3, 0x31, 0); /* medium error */
402 result = USB_STOR_TRANSPORT_FAILED;
403 goto leave;
406 usb_stor_dbg(us, "Allocating PBA %04X for LBA %04X\n",
407 pba, lba);
409 /* set writing to unallocated block flag */
410 command[4] = 0x40;
413 address = (pba << info->blockshift) + page;
415 command[1] = LSB_of(address>>16);
416 command[2] = LSB_of(address>>8);
417 command[3] = LSB_of(address);
419 /* set the lba into the command, modulo 1000 */
420 command[0] = LSB_of(lba % 1000);
421 command[6] = MSB_of(lba % 1000);
423 command[4] |= LSB_of(pages >> info->smallpageshift);
424 command[5] = 0xB0;
425 command[7] = 0x86;
427 /* send command */
428 result = sddr55_bulk_transport(us,
429 DMA_TO_DEVICE, command, 8);
431 if (result != USB_STOR_XFER_GOOD) {
432 usb_stor_dbg(us, "Result for send_command in write_data %d\n",
433 result);
435 /* set_sense_info is superfluous here? */
436 set_sense_info (3, 0x3, 0);/* peripheral write error */
437 result = USB_STOR_TRANSPORT_FAILED;
438 goto leave;
441 /* send the data */
442 result = sddr55_bulk_transport(us,
443 DMA_TO_DEVICE, buffer, len);
445 if (result != USB_STOR_XFER_GOOD) {
446 usb_stor_dbg(us, "Result for send_data in write_data %d\n",
447 result);
449 /* set_sense_info is superfluous here? */
450 set_sense_info (3, 0x3, 0);/* peripheral write error */
451 result = USB_STOR_TRANSPORT_FAILED;
452 goto leave;
455 /* now read status */
456 result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, status, 6);
458 if (result != USB_STOR_XFER_GOOD) {
459 usb_stor_dbg(us, "Result for get_status in write_data %d\n",
460 result);
462 /* set_sense_info is superfluous here? */
463 set_sense_info (3, 0x3, 0);/* peripheral write error */
464 result = USB_STOR_TRANSPORT_FAILED;
465 goto leave;
468 new_pba = (status[3] + (status[4] << 8) + (status[5] << 16))
469 >> info->blockshift;
471 /* check status for error */
472 if (status[0] == 0xff && status[1] == 0x4) {
473 info->pba_to_lba[new_pba] = BAD_BLOCK;
475 set_sense_info (3, 0x0c, 0);
476 result = USB_STOR_TRANSPORT_FAILED;
477 goto leave;
480 usb_stor_dbg(us, "Updating maps for LBA %04X: old PBA %04X, new PBA %04X\n",
481 lba, pba, new_pba);
483 /* update the lba<->pba maps, note new_pba might be the same as pba */
484 info->lba_to_pba[lba] = new_pba;
485 info->pba_to_lba[pba] = UNUSED_BLOCK;
487 /* check that new_pba wasn't already being used */
488 if (info->pba_to_lba[new_pba] != UNUSED_BLOCK) {
489 printk(KERN_ERR "sddr55 error: new PBA %04X already in use for LBA %04X\n",
490 new_pba, info->pba_to_lba[new_pba]);
491 info->fatal_error = 1;
492 set_sense_info (3, 0x31, 0);
493 result = USB_STOR_TRANSPORT_FAILED;
494 goto leave;
497 /* update the pba<->lba maps for new_pba */
498 info->pba_to_lba[new_pba] = lba % 1000;
500 page = 0;
501 lba++;
502 sectors -= pages >> info->smallpageshift;
504 result = USB_STOR_TRANSPORT_GOOD;
506 leave:
507 kfree(buffer);
508 return result;
511 static int sddr55_read_deviceID(struct us_data *us,
512 unsigned char *manufacturerID,
513 unsigned char *deviceID) {
515 int result;
516 unsigned char *command = us->iobuf;
517 unsigned char *content = us->iobuf;
519 memset(command, 0, 8);
520 command[5] = 0xB0;
521 command[7] = 0x84;
522 result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
524 usb_stor_dbg(us, "Result of send_control for device ID is %d\n",
525 result);
527 if (result != USB_STOR_XFER_GOOD)
528 return USB_STOR_TRANSPORT_ERROR;
530 result = sddr55_bulk_transport(us,
531 DMA_FROM_DEVICE, content, 4);
533 if (result != USB_STOR_XFER_GOOD)
534 return USB_STOR_TRANSPORT_ERROR;
536 *manufacturerID = content[0];
537 *deviceID = content[1];
539 if (content[0] != 0xff) {
540 result = sddr55_bulk_transport(us,
541 DMA_FROM_DEVICE, content, 2);
544 return USB_STOR_TRANSPORT_GOOD;
548 static int sddr55_reset(struct us_data *us)
550 return 0;
554 static unsigned long sddr55_get_capacity(struct us_data *us) {
556 unsigned char uninitialized_var(manufacturerID);
557 unsigned char uninitialized_var(deviceID);
558 int result;
559 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
561 usb_stor_dbg(us, "Reading capacity...\n");
563 result = sddr55_read_deviceID(us,
564 &manufacturerID,
565 &deviceID);
567 usb_stor_dbg(us, "Result of read_deviceID is %d\n", result);
569 if (result != USB_STOR_XFER_GOOD)
570 return 0;
572 usb_stor_dbg(us, "Device ID = %02X\n", deviceID);
573 usb_stor_dbg(us, "Manuf ID = %02X\n", manufacturerID);
575 info->pageshift = 9;
576 info->smallpageshift = 0;
577 info->blocksize = 16;
578 info->blockshift = 4;
579 info->blockmask = 15;
581 switch (deviceID) {
583 case 0x6e: // 1MB
584 case 0xe8:
585 case 0xec:
586 info->pageshift = 8;
587 info->smallpageshift = 1;
588 return 0x00100000;
590 case 0xea: // 2MB
591 case 0x64:
592 info->pageshift = 8;
593 info->smallpageshift = 1;
594 /* fall through */
595 case 0x5d: // 5d is a ROM card with pagesize 512.
596 return 0x00200000;
598 case 0xe3: // 4MB
599 case 0xe5:
600 case 0x6b:
601 case 0xd5:
602 return 0x00400000;
604 case 0xe6: // 8MB
605 case 0xd6:
606 return 0x00800000;
608 case 0x73: // 16MB
609 info->blocksize = 32;
610 info->blockshift = 5;
611 info->blockmask = 31;
612 return 0x01000000;
614 case 0x75: // 32MB
615 info->blocksize = 32;
616 info->blockshift = 5;
617 info->blockmask = 31;
618 return 0x02000000;
620 case 0x76: // 64MB
621 info->blocksize = 32;
622 info->blockshift = 5;
623 info->blockmask = 31;
624 return 0x04000000;
626 case 0x79: // 128MB
627 info->blocksize = 32;
628 info->blockshift = 5;
629 info->blockmask = 31;
630 return 0x08000000;
632 default: // unknown
633 return 0;
638 static int sddr55_read_map(struct us_data *us) {
640 struct sddr55_card_info *info = (struct sddr55_card_info *)(us->extra);
641 int numblocks;
642 unsigned char *buffer;
643 unsigned char *command = us->iobuf;
644 int i;
645 unsigned short lba;
646 unsigned short max_lba;
647 int result;
649 if (!info->capacity)
650 return -1;
652 numblocks = info->capacity >> (info->blockshift + info->pageshift);
654 buffer = kmalloc_array(numblocks, 2, GFP_NOIO );
656 if (!buffer)
657 return -1;
659 memset(command, 0, 8);
660 command[5] = 0xB0;
661 command[6] = numblocks * 2 / 256;
662 command[7] = 0x8A;
664 result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
666 if ( result != USB_STOR_XFER_GOOD) {
667 kfree (buffer);
668 return -1;
671 result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, buffer, numblocks * 2);
673 if ( result != USB_STOR_XFER_GOOD) {
674 kfree (buffer);
675 return -1;
678 result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, command, 2);
680 if ( result != USB_STOR_XFER_GOOD) {
681 kfree (buffer);
682 return -1;
685 kfree(info->lba_to_pba);
686 kfree(info->pba_to_lba);
687 info->lba_to_pba = kmalloc_array(numblocks, sizeof(int), GFP_NOIO);
688 info->pba_to_lba = kmalloc_array(numblocks, sizeof(int), GFP_NOIO);
690 if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) {
691 kfree(info->lba_to_pba);
692 kfree(info->pba_to_lba);
693 info->lba_to_pba = NULL;
694 info->pba_to_lba = NULL;
695 kfree(buffer);
696 return -1;
699 memset(info->lba_to_pba, 0xff, numblocks*sizeof(int));
700 memset(info->pba_to_lba, 0xff, numblocks*sizeof(int));
702 /* set maximum lba */
703 max_lba = info->max_log_blks;
704 if (max_lba > 1000)
705 max_lba = 1000;
708 * Each block is 64 bytes of control data, so block i is located in
709 * scatterlist block i*64/128k = i*(2^6)*(2^-17) = i*(2^-11)
712 for (i=0; i<numblocks; i++) {
713 int zone = i / 1024;
715 lba = short_pack(buffer[i * 2], buffer[i * 2 + 1]);
718 * Every 1024 physical blocks ("zone"), the LBA numbers
719 * go back to zero, but are within a higher
720 * block of LBA's. Also, there is a maximum of
721 * 1000 LBA's per zone. In other words, in PBA
722 * 1024-2047 you will find LBA 0-999 which are
723 * really LBA 1000-1999. Yes, this wastes 24
724 * physical blocks per zone. Go figure.
725 * These devices can have blocks go bad, so there
726 * are 24 spare blocks to use when blocks do go bad.
730 * SDDR55 returns 0xffff for a bad block, and 0x400 for the
731 * CIS block. (Is this true for cards 8MB or less??)
732 * Record these in the physical to logical map
735 info->pba_to_lba[i] = lba;
737 if (lba >= max_lba) {
738 continue;
741 if (info->lba_to_pba[lba + zone * 1000] != NOT_ALLOCATED &&
742 !info->force_read_only) {
743 printk(KERN_WARNING
744 "sddr55: map inconsistency at LBA %04X\n",
745 lba + zone * 1000);
746 info->force_read_only = 1;
749 if (lba<0x10 || (lba>=0x3E0 && lba<0x3EF))
750 usb_stor_dbg(us, "LBA %04X <-> PBA %04X\n", lba, i);
752 info->lba_to_pba[lba + zone * 1000] = i;
755 kfree(buffer);
756 return 0;
760 static void sddr55_card_info_destructor(void *extra) {
761 struct sddr55_card_info *info = (struct sddr55_card_info *)extra;
763 if (!extra)
764 return;
766 kfree(info->lba_to_pba);
767 kfree(info->pba_to_lba);
772 * Transport for the Sandisk SDDR-55
774 static int sddr55_transport(struct scsi_cmnd *srb, struct us_data *us)
776 int result;
777 static unsigned char inquiry_response[8] = {
778 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
780 // write-protected for now, no block descriptor support
781 static unsigned char mode_page_01[20] = {
782 0x0, 0x12, 0x00, 0x80, 0x0, 0x0, 0x0, 0x0,
783 0x01, 0x0A,
784 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
786 unsigned char *ptr = us->iobuf;
787 unsigned long capacity;
788 unsigned int lba;
789 unsigned int pba;
790 unsigned int page;
791 unsigned short pages;
792 struct sddr55_card_info *info;
794 if (!us->extra) {
795 us->extra = kzalloc(
796 sizeof(struct sddr55_card_info), GFP_NOIO);
797 if (!us->extra)
798 return USB_STOR_TRANSPORT_ERROR;
799 us->extra_destructor = sddr55_card_info_destructor;
802 info = (struct sddr55_card_info *)(us->extra);
804 if (srb->cmnd[0] == REQUEST_SENSE) {
805 usb_stor_dbg(us, "request sense %02x/%02x/%02x\n",
806 info->sense_data[2],
807 info->sense_data[12],
808 info->sense_data[13]);
810 memcpy (ptr, info->sense_data, sizeof info->sense_data);
811 ptr[0] = 0x70;
812 ptr[7] = 11;
813 usb_stor_set_xfer_buf (ptr, sizeof info->sense_data, srb);
814 memset (info->sense_data, 0, sizeof info->sense_data);
816 return USB_STOR_TRANSPORT_GOOD;
819 memset (info->sense_data, 0, sizeof info->sense_data);
822 * Dummy up a response for INQUIRY since SDDR55 doesn't
823 * respond to INQUIRY commands
826 if (srb->cmnd[0] == INQUIRY) {
827 memcpy(ptr, inquiry_response, 8);
828 fill_inquiry_response(us, ptr, 36);
829 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);
840 if (result) {
841 result = sddr55_status (us);
842 if (!result) {
843 set_sense_info (6, 0x28, 0); /* new media, set unit attention, not ready to ready */
845 return USB_STOR_TRANSPORT_FAILED;
850 * if we detected a problem with the map when writing,
851 * don't allow any more access
853 if (info->fatal_error) {
855 set_sense_info (3, 0x31, 0);
856 return USB_STOR_TRANSPORT_FAILED;
859 if (srb->cmnd[0] == READ_CAPACITY) {
861 capacity = sddr55_get_capacity(us);
863 if (!capacity) {
864 set_sense_info (3, 0x30, 0); /* incompatible medium */
865 return USB_STOR_TRANSPORT_FAILED;
868 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
874 info->max_log_blks = ((info->capacity >> (info->pageshift + info->blockshift)) / 256) * 250;
877 * Last page in the card, adjust as we only use 250 out of
878 * every 256 pages
880 capacity = (capacity / 256) * 250;
882 capacity /= PAGESIZE;
883 capacity--;
885 ((__be32 *) ptr)[0] = cpu_to_be32(capacity);
886 ((__be32 *) ptr)[1] = cpu_to_be32(PAGESIZE);
887 usb_stor_set_xfer_buf(ptr, 8, srb);
889 sddr55_read_map(us);
891 return USB_STOR_TRANSPORT_GOOD;
894 if (srb->cmnd[0] == MODE_SENSE_10) {
896 memcpy(ptr, mode_page_01, sizeof mode_page_01);
897 ptr[3] = (info->read_only || info->force_read_only) ? 0x80 : 0;
898 usb_stor_set_xfer_buf(ptr, sizeof(mode_page_01), srb);
900 if ( (srb->cmnd[2] & 0x3F) == 0x01 ) {
901 usb_stor_dbg(us, "Dummy up request for mode page 1\n");
902 return USB_STOR_TRANSPORT_GOOD;
904 } else if ( (srb->cmnd[2] & 0x3F) == 0x3F ) {
905 usb_stor_dbg(us, "Dummy up request for all mode pages\n");
906 return USB_STOR_TRANSPORT_GOOD;
909 set_sense_info (5, 0x24, 0); /* invalid field in command */
910 return USB_STOR_TRANSPORT_FAILED;
913 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
915 usb_stor_dbg(us, "%s medium removal. Not that I can do anything about it...\n",
916 (srb->cmnd[4]&0x03) ? "Prevent" : "Allow");
918 return USB_STOR_TRANSPORT_GOOD;
922 if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10) {
924 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
925 page <<= 16;
926 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
927 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
929 page <<= info->smallpageshift;
931 // convert page to block and page-within-block
933 lba = page >> info->blockshift;
934 page = page & info->blockmask;
936 // locate physical block corresponding to logical block
938 if (lba >= info->max_log_blks) {
940 usb_stor_dbg(us, "Error: Requested LBA %04X exceeds maximum block %04X\n",
941 lba, info->max_log_blks - 1);
943 set_sense_info (5, 0x24, 0); /* invalid field in command */
945 return USB_STOR_TRANSPORT_FAILED;
948 pba = info->lba_to_pba[lba];
950 if (srb->cmnd[0] == WRITE_10) {
951 usb_stor_dbg(us, "WRITE_10: write block %04X (LBA %04X) page %01X pages %d\n",
952 pba, lba, page, pages);
954 return sddr55_write_data(us, lba, page, pages);
955 } else {
956 usb_stor_dbg(us, "READ_10: read block %04X (LBA %04X) page %01X pages %d\n",
957 pba, lba, page, pages);
959 return sddr55_read_data(us, lba, page, pages);
964 if (srb->cmnd[0] == TEST_UNIT_READY) {
965 return USB_STOR_TRANSPORT_GOOD;
968 if (srb->cmnd[0] == START_STOP) {
969 return USB_STOR_TRANSPORT_GOOD;
972 set_sense_info (5, 0x20, 0); /* illegal command */
974 return USB_STOR_TRANSPORT_FAILED; // FIXME: sense buffer?
977 static struct scsi_host_template sddr55_host_template;
979 static int sddr55_probe(struct usb_interface *intf,
980 const struct usb_device_id *id)
982 struct us_data *us;
983 int result;
985 result = usb_stor_probe1(&us, intf, id,
986 (id - sddr55_usb_ids) + sddr55_unusual_dev_list,
987 &sddr55_host_template);
988 if (result)
989 return result;
991 us->transport_name = "SDDR55";
992 us->transport = sddr55_transport;
993 us->transport_reset = sddr55_reset;
994 us->max_lun = 0;
996 result = usb_stor_probe2(us);
997 return result;
1000 static struct usb_driver sddr55_driver = {
1001 .name = DRV_NAME,
1002 .probe = sddr55_probe,
1003 .disconnect = usb_stor_disconnect,
1004 .suspend = usb_stor_suspend,
1005 .resume = usb_stor_resume,
1006 .reset_resume = usb_stor_reset_resume,
1007 .pre_reset = usb_stor_pre_reset,
1008 .post_reset = usb_stor_post_reset,
1009 .id_table = sddr55_usb_ids,
1010 .soft_unbind = 1,
1011 .no_dynamic_id = 1,
1014 module_usb_stor_driver(sddr55_driver, sddr55_host_template, DRV_NAME);