Loading...
1/* Driver for SanDisk SDDR-55 SmartMedia reader
2 *
3 * SDDR55 driver v0.1:
4 *
5 * First release
6 *
7 * Current development and maintenance by:
8 * (c) 2002 Simon Munton
9 *
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
13 * later version.
14 *
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.
19 *
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.
23 */
24
25#include <linux/jiffies.h>
26#include <linux/errno.h>
27#include <linux/module.h>
28#include <linux/slab.h>
29
30#include <scsi/scsi.h>
31#include <scsi/scsi_cmnd.h>
32
33#include "usb.h"
34#include "transport.h"
35#include "protocol.h"
36#include "debug.h"
37#include "scsiglue.h"
38
39#define DRV_NAME "ums-sddr55"
40
41MODULE_DESCRIPTION("Driver for SanDisk SDDR-55 SmartMedia reader");
42MODULE_AUTHOR("Simon Munton");
43MODULE_LICENSE("GPL");
44
45/*
46 * The table of devices
47 */
48#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
49 vendorName, productName, useProtocol, useTransport, \
50 initFunction, flags) \
51{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
52 .driver_info = (flags) }
53
54static struct usb_device_id sddr55_usb_ids[] = {
55# include "unusual_sddr55.h"
56 { } /* Terminating entry */
57};
58MODULE_DEVICE_TABLE(usb, sddr55_usb_ids);
59
60#undef UNUSUAL_DEV
61
62/*
63 * The flags table
64 */
65#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
66 vendor_name, product_name, use_protocol, use_transport, \
67 init_function, Flags) \
68{ \
69 .vendorName = vendor_name, \
70 .productName = product_name, \
71 .useProtocol = use_protocol, \
72 .useTransport = use_transport, \
73 .initFunction = init_function, \
74}
75
76static struct us_unusual_dev sddr55_unusual_dev_list[] = {
77# include "unusual_sddr55.h"
78 { } /* Terminating entry */
79};
80
81#undef UNUSUAL_DEV
82
83
84#define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
85#define LSB_of(s) ((s)&0xFF)
86#define MSB_of(s) ((s)>>8)
87#define PAGESIZE 512
88
89#define set_sense_info(sk, asc, ascq) \
90 do { \
91 info->sense_data[2] = sk; \
92 info->sense_data[12] = asc; \
93 info->sense_data[13] = ascq; \
94 } while (0)
95
96
97struct sddr55_card_info {
98 unsigned long capacity; /* Size of card in bytes */
99 int max_log_blks; /* maximum number of logical blocks */
100 int pageshift; /* log2 of pagesize */
101 int smallpageshift; /* 1 if pagesize == 256 */
102 int blocksize; /* Size of block in pages */
103 int blockshift; /* log2 of blocksize */
104 int blockmask; /* 2^blockshift - 1 */
105 int read_only; /* non zero if card is write protected */
106 int force_read_only; /* non zero if we find a map error*/
107 int *lba_to_pba; /* logical to physical map */
108 int *pba_to_lba; /* physical to logical map */
109 int fatal_error; /* set if we detect something nasty */
110 unsigned long last_access; /* number of jiffies since we last talked to device */
111 unsigned char sense_data[18];
112};
113
114
115#define NOT_ALLOCATED 0xffffffff
116#define BAD_BLOCK 0xffff
117#define CIS_BLOCK 0x400
118#define UNUSED_BLOCK 0x3ff
119
120static int
121sddr55_bulk_transport(struct us_data *us, int direction,
122 unsigned char *data, unsigned int len) {
123 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
124 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
125 us->recv_bulk_pipe : us->send_bulk_pipe;
126
127 if (!len)
128 return USB_STOR_XFER_GOOD;
129 info->last_access = jiffies;
130 return usb_stor_bulk_transfer_buf(us, pipe, data, len, NULL);
131}
132
133/* check if card inserted, if there is, update read_only status
134 * return non zero if no card
135 */
136
137static int sddr55_status(struct us_data *us)
138{
139 int result;
140 unsigned char *command = us->iobuf;
141 unsigned char *status = us->iobuf;
142 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
143
144 /* send command */
145 memset(command, 0, 8);
146 command[5] = 0xB0;
147 command[7] = 0x80;
148 result = sddr55_bulk_transport(us,
149 DMA_TO_DEVICE, command, 8);
150
151 usb_stor_dbg(us, "Result for send_command in status %d\n", result);
152
153 if (result != USB_STOR_XFER_GOOD) {
154 set_sense_info (4, 0, 0); /* hardware error */
155 return USB_STOR_TRANSPORT_ERROR;
156 }
157
158 result = sddr55_bulk_transport(us,
159 DMA_FROM_DEVICE, status, 4);
160
161 /* expect to get short transfer if no card fitted */
162 if (result == USB_STOR_XFER_SHORT || result == USB_STOR_XFER_STALLED) {
163 /* had a short transfer, no card inserted, free map memory */
164 kfree(info->lba_to_pba);
165 kfree(info->pba_to_lba);
166 info->lba_to_pba = NULL;
167 info->pba_to_lba = NULL;
168
169 info->fatal_error = 0;
170 info->force_read_only = 0;
171
172 set_sense_info (2, 0x3a, 0); /* not ready, medium not present */
173 return USB_STOR_TRANSPORT_FAILED;
174 }
175
176 if (result != USB_STOR_XFER_GOOD) {
177 set_sense_info (4, 0, 0); /* hardware error */
178 return USB_STOR_TRANSPORT_FAILED;
179 }
180
181 /* check write protect status */
182 info->read_only = (status[0] & 0x20);
183
184 /* now read status */
185 result = sddr55_bulk_transport(us,
186 DMA_FROM_DEVICE, status, 2);
187
188 if (result != USB_STOR_XFER_GOOD) {
189 set_sense_info (4, 0, 0); /* hardware error */
190 }
191
192 return (result == USB_STOR_XFER_GOOD ?
193 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_FAILED);
194}
195
196
197static int sddr55_read_data(struct us_data *us,
198 unsigned int lba,
199 unsigned int page,
200 unsigned short sectors) {
201
202 int result = USB_STOR_TRANSPORT_GOOD;
203 unsigned char *command = us->iobuf;
204 unsigned char *status = us->iobuf;
205 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
206 unsigned char *buffer;
207
208 unsigned int pba;
209 unsigned long address;
210
211 unsigned short pages;
212 unsigned int len, offset;
213 struct scatterlist *sg;
214
215 // Since we only read in one block at a time, we have to create
216 // a bounce buffer and move the data a piece at a time between the
217 // bounce buffer and the actual transfer buffer.
218
219 len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
220 info->smallpageshift) * PAGESIZE;
221 buffer = kmalloc(len, GFP_NOIO);
222 if (buffer == NULL)
223 return USB_STOR_TRANSPORT_ERROR; /* out of memory */
224 offset = 0;
225 sg = NULL;
226
227 while (sectors>0) {
228
229 /* have we got to end? */
230 if (lba >= info->max_log_blks)
231 break;
232
233 pba = info->lba_to_pba[lba];
234
235 // Read as many sectors as possible in this block
236
237 pages = min((unsigned int) sectors << info->smallpageshift,
238 info->blocksize - page);
239 len = pages << info->pageshift;
240
241 usb_stor_dbg(us, "Read %02X pages, from PBA %04X (LBA %04X) page %02X\n",
242 pages, pba, lba, page);
243
244 if (pba == NOT_ALLOCATED) {
245 /* no pba for this lba, fill with zeroes */
246 memset (buffer, 0, len);
247 } else {
248
249 address = (pba << info->blockshift) + page;
250
251 command[0] = 0;
252 command[1] = LSB_of(address>>16);
253 command[2] = LSB_of(address>>8);
254 command[3] = LSB_of(address);
255
256 command[4] = 0;
257 command[5] = 0xB0;
258 command[6] = LSB_of(pages << (1 - info->smallpageshift));
259 command[7] = 0x85;
260
261 /* send command */
262 result = sddr55_bulk_transport(us,
263 DMA_TO_DEVICE, command, 8);
264
265 usb_stor_dbg(us, "Result for send_command in read_data %d\n",
266 result);
267
268 if (result != USB_STOR_XFER_GOOD) {
269 result = USB_STOR_TRANSPORT_ERROR;
270 goto leave;
271 }
272
273 /* read data */
274 result = sddr55_bulk_transport(us,
275 DMA_FROM_DEVICE, buffer, len);
276
277 if (result != USB_STOR_XFER_GOOD) {
278 result = USB_STOR_TRANSPORT_ERROR;
279 goto leave;
280 }
281
282 /* now read status */
283 result = sddr55_bulk_transport(us,
284 DMA_FROM_DEVICE, status, 2);
285
286 if (result != USB_STOR_XFER_GOOD) {
287 result = USB_STOR_TRANSPORT_ERROR;
288 goto leave;
289 }
290
291 /* check status for error */
292 if (status[0] == 0xff && status[1] == 0x4) {
293 set_sense_info (3, 0x11, 0);
294 result = USB_STOR_TRANSPORT_FAILED;
295 goto leave;
296 }
297 }
298
299 // Store the data in the transfer buffer
300 usb_stor_access_xfer_buf(buffer, len, us->srb,
301 &sg, &offset, TO_XFER_BUF);
302
303 page = 0;
304 lba++;
305 sectors -= pages >> info->smallpageshift;
306 }
307
308 result = USB_STOR_TRANSPORT_GOOD;
309
310leave:
311 kfree(buffer);
312
313 return result;
314}
315
316static int sddr55_write_data(struct us_data *us,
317 unsigned int lba,
318 unsigned int page,
319 unsigned short sectors) {
320
321 int result = USB_STOR_TRANSPORT_GOOD;
322 unsigned char *command = us->iobuf;
323 unsigned char *status = us->iobuf;
324 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
325 unsigned char *buffer;
326
327 unsigned int pba;
328 unsigned int new_pba;
329 unsigned long address;
330
331 unsigned short pages;
332 int i;
333 unsigned int len, offset;
334 struct scatterlist *sg;
335
336 /* check if we are allowed to write */
337 if (info->read_only || info->force_read_only) {
338 set_sense_info (7, 0x27, 0); /* read only */
339 return USB_STOR_TRANSPORT_FAILED;
340 }
341
342 // Since we only write one block at a time, we have to create
343 // a bounce buffer and move the data a piece at a time between the
344 // bounce buffer and the actual transfer buffer.
345
346 len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
347 info->smallpageshift) * PAGESIZE;
348 buffer = kmalloc(len, GFP_NOIO);
349 if (buffer == NULL)
350 return USB_STOR_TRANSPORT_ERROR;
351 offset = 0;
352 sg = NULL;
353
354 while (sectors > 0) {
355
356 /* have we got to end? */
357 if (lba >= info->max_log_blks)
358 break;
359
360 pba = info->lba_to_pba[lba];
361
362 // Write as many sectors as possible in this block
363
364 pages = min((unsigned int) sectors << info->smallpageshift,
365 info->blocksize - page);
366 len = pages << info->pageshift;
367
368 // Get the data from the transfer buffer
369 usb_stor_access_xfer_buf(buffer, len, us->srb,
370 &sg, &offset, FROM_XFER_BUF);
371
372 usb_stor_dbg(us, "Write %02X pages, to PBA %04X (LBA %04X) page %02X\n",
373 pages, pba, lba, page);
374
375 command[4] = 0;
376
377 if (pba == NOT_ALLOCATED) {
378 /* no pba allocated for this lba, find a free pba to use */
379
380 int max_pba = (info->max_log_blks / 250 ) * 256;
381 int found_count = 0;
382 int found_pba = -1;
383
384 /* set pba to first block in zone lba is in */
385 pba = (lba / 1000) * 1024;
386
387 usb_stor_dbg(us, "No PBA for LBA %04X\n", lba);
388
389 if (max_pba > 1024)
390 max_pba = 1024;
391
392 /*
393 * Scan through the map looking for an unused block
394 * leave 16 unused blocks at start (or as many as
395 * possible) since the sddr55 seems to reuse a used
396 * block when it shouldn't if we don't leave space.
397 */
398 for (i = 0; i < max_pba; i++, pba++) {
399 if (info->pba_to_lba[pba] == UNUSED_BLOCK) {
400 found_pba = pba;
401 if (found_count++ > 16)
402 break;
403 }
404 }
405
406 pba = found_pba;
407
408 if (pba == -1) {
409 /* oh dear */
410 usb_stor_dbg(us, "Couldn't find unallocated block\n");
411
412 set_sense_info (3, 0x31, 0); /* medium error */
413 result = USB_STOR_TRANSPORT_FAILED;
414 goto leave;
415 }
416
417 usb_stor_dbg(us, "Allocating PBA %04X for LBA %04X\n",
418 pba, lba);
419
420 /* set writing to unallocated block flag */
421 command[4] = 0x40;
422 }
423
424 address = (pba << info->blockshift) + page;
425
426 command[1] = LSB_of(address>>16);
427 command[2] = LSB_of(address>>8);
428 command[3] = LSB_of(address);
429
430 /* set the lba into the command, modulo 1000 */
431 command[0] = LSB_of(lba % 1000);
432 command[6] = MSB_of(lba % 1000);
433
434 command[4] |= LSB_of(pages >> info->smallpageshift);
435 command[5] = 0xB0;
436 command[7] = 0x86;
437
438 /* send command */
439 result = sddr55_bulk_transport(us,
440 DMA_TO_DEVICE, command, 8);
441
442 if (result != USB_STOR_XFER_GOOD) {
443 usb_stor_dbg(us, "Result for send_command in write_data %d\n",
444 result);
445
446 /* set_sense_info is superfluous here? */
447 set_sense_info (3, 0x3, 0);/* peripheral write error */
448 result = USB_STOR_TRANSPORT_FAILED;
449 goto leave;
450 }
451
452 /* send the data */
453 result = sddr55_bulk_transport(us,
454 DMA_TO_DEVICE, buffer, len);
455
456 if (result != USB_STOR_XFER_GOOD) {
457 usb_stor_dbg(us, "Result for send_data in write_data %d\n",
458 result);
459
460 /* set_sense_info is superfluous here? */
461 set_sense_info (3, 0x3, 0);/* peripheral write error */
462 result = USB_STOR_TRANSPORT_FAILED;
463 goto leave;
464 }
465
466 /* now read status */
467 result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, status, 6);
468
469 if (result != USB_STOR_XFER_GOOD) {
470 usb_stor_dbg(us, "Result for get_status in write_data %d\n",
471 result);
472
473 /* set_sense_info is superfluous here? */
474 set_sense_info (3, 0x3, 0);/* peripheral write error */
475 result = USB_STOR_TRANSPORT_FAILED;
476 goto leave;
477 }
478
479 new_pba = (status[3] + (status[4] << 8) + (status[5] << 16))
480 >> info->blockshift;
481
482 /* check status for error */
483 if (status[0] == 0xff && status[1] == 0x4) {
484 info->pba_to_lba[new_pba] = BAD_BLOCK;
485
486 set_sense_info (3, 0x0c, 0);
487 result = USB_STOR_TRANSPORT_FAILED;
488 goto leave;
489 }
490
491 usb_stor_dbg(us, "Updating maps for LBA %04X: old PBA %04X, new PBA %04X\n",
492 lba, pba, new_pba);
493
494 /* update the lba<->pba maps, note new_pba might be the same as pba */
495 info->lba_to_pba[lba] = new_pba;
496 info->pba_to_lba[pba] = UNUSED_BLOCK;
497
498 /* check that new_pba wasn't already being used */
499 if (info->pba_to_lba[new_pba] != UNUSED_BLOCK) {
500 printk(KERN_ERR "sddr55 error: new PBA %04X already in use for LBA %04X\n",
501 new_pba, info->pba_to_lba[new_pba]);
502 info->fatal_error = 1;
503 set_sense_info (3, 0x31, 0);
504 result = USB_STOR_TRANSPORT_FAILED;
505 goto leave;
506 }
507
508 /* update the pba<->lba maps for new_pba */
509 info->pba_to_lba[new_pba] = lba % 1000;
510
511 page = 0;
512 lba++;
513 sectors -= pages >> info->smallpageshift;
514 }
515 result = USB_STOR_TRANSPORT_GOOD;
516
517 leave:
518 kfree(buffer);
519 return result;
520}
521
522static int sddr55_read_deviceID(struct us_data *us,
523 unsigned char *manufacturerID,
524 unsigned char *deviceID) {
525
526 int result;
527 unsigned char *command = us->iobuf;
528 unsigned char *content = us->iobuf;
529
530 memset(command, 0, 8);
531 command[5] = 0xB0;
532 command[7] = 0x84;
533 result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
534
535 usb_stor_dbg(us, "Result of send_control for device ID is %d\n",
536 result);
537
538 if (result != USB_STOR_XFER_GOOD)
539 return USB_STOR_TRANSPORT_ERROR;
540
541 result = sddr55_bulk_transport(us,
542 DMA_FROM_DEVICE, content, 4);
543
544 if (result != USB_STOR_XFER_GOOD)
545 return USB_STOR_TRANSPORT_ERROR;
546
547 *manufacturerID = content[0];
548 *deviceID = content[1];
549
550 if (content[0] != 0xff) {
551 result = sddr55_bulk_transport(us,
552 DMA_FROM_DEVICE, content, 2);
553 }
554
555 return USB_STOR_TRANSPORT_GOOD;
556}
557
558
559static int sddr55_reset(struct us_data *us)
560{
561 return 0;
562}
563
564
565static unsigned long sddr55_get_capacity(struct us_data *us) {
566
567 unsigned char uninitialized_var(manufacturerID);
568 unsigned char uninitialized_var(deviceID);
569 int result;
570 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
571
572 usb_stor_dbg(us, "Reading capacity...\n");
573
574 result = sddr55_read_deviceID(us,
575 &manufacturerID,
576 &deviceID);
577
578 usb_stor_dbg(us, "Result of read_deviceID is %d\n", result);
579
580 if (result != USB_STOR_XFER_GOOD)
581 return 0;
582
583 usb_stor_dbg(us, "Device ID = %02X\n", deviceID);
584 usb_stor_dbg(us, "Manuf ID = %02X\n", manufacturerID);
585
586 info->pageshift = 9;
587 info->smallpageshift = 0;
588 info->blocksize = 16;
589 info->blockshift = 4;
590 info->blockmask = 15;
591
592 switch (deviceID) {
593
594 case 0x6e: // 1MB
595 case 0xe8:
596 case 0xec:
597 info->pageshift = 8;
598 info->smallpageshift = 1;
599 return 0x00100000;
600
601 case 0xea: // 2MB
602 case 0x64:
603 info->pageshift = 8;
604 info->smallpageshift = 1;
605 case 0x5d: // 5d is a ROM card with pagesize 512.
606 return 0x00200000;
607
608 case 0xe3: // 4MB
609 case 0xe5:
610 case 0x6b:
611 case 0xd5:
612 return 0x00400000;
613
614 case 0xe6: // 8MB
615 case 0xd6:
616 return 0x00800000;
617
618 case 0x73: // 16MB
619 info->blocksize = 32;
620 info->blockshift = 5;
621 info->blockmask = 31;
622 return 0x01000000;
623
624 case 0x75: // 32MB
625 info->blocksize = 32;
626 info->blockshift = 5;
627 info->blockmask = 31;
628 return 0x02000000;
629
630 case 0x76: // 64MB
631 info->blocksize = 32;
632 info->blockshift = 5;
633 info->blockmask = 31;
634 return 0x04000000;
635
636 case 0x79: // 128MB
637 info->blocksize = 32;
638 info->blockshift = 5;
639 info->blockmask = 31;
640 return 0x08000000;
641
642 default: // unknown
643 return 0;
644
645 }
646}
647
648static int sddr55_read_map(struct us_data *us) {
649
650 struct sddr55_card_info *info = (struct sddr55_card_info *)(us->extra);
651 int numblocks;
652 unsigned char *buffer;
653 unsigned char *command = us->iobuf;
654 int i;
655 unsigned short lba;
656 unsigned short max_lba;
657 int result;
658
659 if (!info->capacity)
660 return -1;
661
662 numblocks = info->capacity >> (info->blockshift + info->pageshift);
663
664 buffer = kmalloc( numblocks * 2, GFP_NOIO );
665
666 if (!buffer)
667 return -1;
668
669 memset(command, 0, 8);
670 command[5] = 0xB0;
671 command[6] = numblocks * 2 / 256;
672 command[7] = 0x8A;
673
674 result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
675
676 if ( result != USB_STOR_XFER_GOOD) {
677 kfree (buffer);
678 return -1;
679 }
680
681 result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, buffer, numblocks * 2);
682
683 if ( result != USB_STOR_XFER_GOOD) {
684 kfree (buffer);
685 return -1;
686 }
687
688 result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, command, 2);
689
690 if ( result != USB_STOR_XFER_GOOD) {
691 kfree (buffer);
692 return -1;
693 }
694
695 kfree(info->lba_to_pba);
696 kfree(info->pba_to_lba);
697 info->lba_to_pba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
698 info->pba_to_lba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
699
700 if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) {
701 kfree(info->lba_to_pba);
702 kfree(info->pba_to_lba);
703 info->lba_to_pba = NULL;
704 info->pba_to_lba = NULL;
705 kfree(buffer);
706 return -1;
707 }
708
709 memset(info->lba_to_pba, 0xff, numblocks*sizeof(int));
710 memset(info->pba_to_lba, 0xff, numblocks*sizeof(int));
711
712 /* set maximum lba */
713 max_lba = info->max_log_blks;
714 if (max_lba > 1000)
715 max_lba = 1000;
716
717 // Each block is 64 bytes of control data, so block i is located in
718 // scatterlist block i*64/128k = i*(2^6)*(2^-17) = i*(2^-11)
719
720 for (i=0; i<numblocks; i++) {
721 int zone = i / 1024;
722
723 lba = short_pack(buffer[i * 2], buffer[i * 2 + 1]);
724
725 /* Every 1024 physical blocks ("zone"), the LBA numbers
726 * go back to zero, but are within a higher
727 * block of LBA's. Also, there is a maximum of
728 * 1000 LBA's per zone. In other words, in PBA
729 * 1024-2047 you will find LBA 0-999 which are
730 * really LBA 1000-1999. Yes, this wastes 24
731 * physical blocks per zone. Go figure.
732 * These devices can have blocks go bad, so there
733 * are 24 spare blocks to use when blocks do go bad.
734 */
735
736 /* SDDR55 returns 0xffff for a bad block, and 0x400 for the
737 * CIS block. (Is this true for cards 8MB or less??)
738 * Record these in the physical to logical map
739 */
740
741 info->pba_to_lba[i] = lba;
742
743 if (lba >= max_lba) {
744 continue;
745 }
746
747 if (info->lba_to_pba[lba + zone * 1000] != NOT_ALLOCATED &&
748 !info->force_read_only) {
749 printk(KERN_WARNING
750 "sddr55: map inconsistency at LBA %04X\n",
751 lba + zone * 1000);
752 info->force_read_only = 1;
753 }
754
755 if (lba<0x10 || (lba>=0x3E0 && lba<0x3EF))
756 usb_stor_dbg(us, "LBA %04X <-> PBA %04X\n", lba, i);
757
758 info->lba_to_pba[lba + zone * 1000] = i;
759 }
760
761 kfree(buffer);
762 return 0;
763}
764
765
766static void sddr55_card_info_destructor(void *extra) {
767 struct sddr55_card_info *info = (struct sddr55_card_info *)extra;
768
769 if (!extra)
770 return;
771
772 kfree(info->lba_to_pba);
773 kfree(info->pba_to_lba);
774}
775
776
777/*
778 * Transport for the Sandisk SDDR-55
779 */
780static int sddr55_transport(struct scsi_cmnd *srb, struct us_data *us)
781{
782 int result;
783 static unsigned char inquiry_response[8] = {
784 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
785 };
786 // write-protected for now, no block descriptor support
787 static unsigned char mode_page_01[20] = {
788 0x0, 0x12, 0x00, 0x80, 0x0, 0x0, 0x0, 0x0,
789 0x01, 0x0A,
790 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
791 };
792 unsigned char *ptr = us->iobuf;
793 unsigned long capacity;
794 unsigned int lba;
795 unsigned int pba;
796 unsigned int page;
797 unsigned short pages;
798 struct sddr55_card_info *info;
799
800 if (!us->extra) {
801 us->extra = kzalloc(
802 sizeof(struct sddr55_card_info), GFP_NOIO);
803 if (!us->extra)
804 return USB_STOR_TRANSPORT_ERROR;
805 us->extra_destructor = sddr55_card_info_destructor;
806 }
807
808 info = (struct sddr55_card_info *)(us->extra);
809
810 if (srb->cmnd[0] == REQUEST_SENSE) {
811 usb_stor_dbg(us, "request sense %02x/%02x/%02x\n",
812 info->sense_data[2],
813 info->sense_data[12],
814 info->sense_data[13]);
815
816 memcpy (ptr, info->sense_data, sizeof info->sense_data);
817 ptr[0] = 0x70;
818 ptr[7] = 11;
819 usb_stor_set_xfer_buf (ptr, sizeof info->sense_data, srb);
820 memset (info->sense_data, 0, sizeof info->sense_data);
821
822 return USB_STOR_TRANSPORT_GOOD;
823 }
824
825 memset (info->sense_data, 0, sizeof info->sense_data);
826
827 /* Dummy up a response for INQUIRY since SDDR55 doesn't
828 respond to INQUIRY commands */
829
830 if (srb->cmnd[0] == INQUIRY) {
831 memcpy(ptr, inquiry_response, 8);
832 fill_inquiry_response(us, ptr, 36);
833 return USB_STOR_TRANSPORT_GOOD;
834 }
835
836 /* only check card status if the map isn't allocated, ie no card seen yet
837 * or if it's been over half a second since we last accessed it
838 */
839 if (info->lba_to_pba == NULL || time_after(jiffies, info->last_access + HZ/2)) {
840
841 /* check to see if a card is fitted */
842 result = sddr55_status (us);
843 if (result) {
844 result = sddr55_status (us);
845 if (!result) {
846 set_sense_info (6, 0x28, 0); /* new media, set unit attention, not ready to ready */
847 }
848 return USB_STOR_TRANSPORT_FAILED;
849 }
850 }
851
852 /* if we detected a problem with the map when writing,
853 don't allow any more access */
854 if (info->fatal_error) {
855
856 set_sense_info (3, 0x31, 0);
857 return USB_STOR_TRANSPORT_FAILED;
858 }
859
860 if (srb->cmnd[0] == READ_CAPACITY) {
861
862 capacity = sddr55_get_capacity(us);
863
864 if (!capacity) {
865 set_sense_info (3, 0x30, 0); /* incompatible medium */
866 return USB_STOR_TRANSPORT_FAILED;
867 }
868
869 info->capacity = capacity;
870
871 /* figure out the maximum logical block number, allowing for
872 * the fact that only 250 out of every 256 are used */
873 info->max_log_blks = ((info->capacity >> (info->pageshift + info->blockshift)) / 256) * 250;
874
875 /* Last page in the card, adjust as we only use 250 out of
876 * every 256 pages */
877 capacity = (capacity / 256) * 250;
878
879 capacity /= PAGESIZE;
880 capacity--;
881
882 ((__be32 *) ptr)[0] = cpu_to_be32(capacity);
883 ((__be32 *) ptr)[1] = cpu_to_be32(PAGESIZE);
884 usb_stor_set_xfer_buf(ptr, 8, srb);
885
886 sddr55_read_map(us);
887
888 return USB_STOR_TRANSPORT_GOOD;
889 }
890
891 if (srb->cmnd[0] == MODE_SENSE_10) {
892
893 memcpy(ptr, mode_page_01, sizeof mode_page_01);
894 ptr[3] = (info->read_only || info->force_read_only) ? 0x80 : 0;
895 usb_stor_set_xfer_buf(ptr, sizeof(mode_page_01), srb);
896
897 if ( (srb->cmnd[2] & 0x3F) == 0x01 ) {
898 usb_stor_dbg(us, "Dummy up request for mode page 1\n");
899 return USB_STOR_TRANSPORT_GOOD;
900
901 } else if ( (srb->cmnd[2] & 0x3F) == 0x3F ) {
902 usb_stor_dbg(us, "Dummy up request for all mode pages\n");
903 return USB_STOR_TRANSPORT_GOOD;
904 }
905
906 set_sense_info (5, 0x24, 0); /* invalid field in command */
907 return USB_STOR_TRANSPORT_FAILED;
908 }
909
910 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
911
912 usb_stor_dbg(us, "%s medium removal. Not that I can do anything about it...\n",
913 (srb->cmnd[4]&0x03) ? "Prevent" : "Allow");
914
915 return USB_STOR_TRANSPORT_GOOD;
916
917 }
918
919 if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10) {
920
921 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
922 page <<= 16;
923 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
924 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
925
926 page <<= info->smallpageshift;
927
928 // convert page to block and page-within-block
929
930 lba = page >> info->blockshift;
931 page = page & info->blockmask;
932
933 // locate physical block corresponding to logical block
934
935 if (lba >= info->max_log_blks) {
936
937 usb_stor_dbg(us, "Error: Requested LBA %04X exceeds maximum block %04X\n",
938 lba, info->max_log_blks - 1);
939
940 set_sense_info (5, 0x24, 0); /* invalid field in command */
941
942 return USB_STOR_TRANSPORT_FAILED;
943 }
944
945 pba = info->lba_to_pba[lba];
946
947 if (srb->cmnd[0] == WRITE_10) {
948 usb_stor_dbg(us, "WRITE_10: write block %04X (LBA %04X) page %01X pages %d\n",
949 pba, lba, page, pages);
950
951 return sddr55_write_data(us, lba, page, pages);
952 } else {
953 usb_stor_dbg(us, "READ_10: read block %04X (LBA %04X) page %01X pages %d\n",
954 pba, lba, page, pages);
955
956 return sddr55_read_data(us, lba, page, pages);
957 }
958 }
959
960
961 if (srb->cmnd[0] == TEST_UNIT_READY) {
962 return USB_STOR_TRANSPORT_GOOD;
963 }
964
965 if (srb->cmnd[0] == START_STOP) {
966 return USB_STOR_TRANSPORT_GOOD;
967 }
968
969 set_sense_info (5, 0x20, 0); /* illegal command */
970
971 return USB_STOR_TRANSPORT_FAILED; // FIXME: sense buffer?
972}
973
974static struct scsi_host_template sddr55_host_template;
975
976static int sddr55_probe(struct usb_interface *intf,
977 const struct usb_device_id *id)
978{
979 struct us_data *us;
980 int result;
981
982 result = usb_stor_probe1(&us, intf, id,
983 (id - sddr55_usb_ids) + sddr55_unusual_dev_list,
984 &sddr55_host_template);
985 if (result)
986 return result;
987
988 us->transport_name = "SDDR55";
989 us->transport = sddr55_transport;
990 us->transport_reset = sddr55_reset;
991 us->max_lun = 0;
992
993 result = usb_stor_probe2(us);
994 return result;
995}
996
997static struct usb_driver sddr55_driver = {
998 .name = DRV_NAME,
999 .probe = sddr55_probe,
1000 .disconnect = usb_stor_disconnect,
1001 .suspend = usb_stor_suspend,
1002 .resume = usb_stor_resume,
1003 .reset_resume = usb_stor_reset_resume,
1004 .pre_reset = usb_stor_pre_reset,
1005 .post_reset = usb_stor_post_reset,
1006 .id_table = sddr55_usb_ids,
1007 .soft_unbind = 1,
1008 .no_dynamic_id = 1,
1009};
1010
1011module_usb_stor_driver(sddr55_driver, sddr55_host_template, DRV_NAME);
1/*
2 * Driver for SanDisk SDDR-55 SmartMedia reader
3 *
4 * SDDR55 driver v0.1:
5 *
6 * First release
7 *
8 * Current development and maintenance by:
9 * (c) 2002 Simon Munton
10 *
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
14 * later version.
15 *
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.
20 *
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.
24 */
25
26#include <linux/jiffies.h>
27#include <linux/errno.h>
28#include <linux/module.h>
29#include <linux/slab.h>
30
31#include <scsi/scsi.h>
32#include <scsi/scsi_cmnd.h>
33
34#include "usb.h"
35#include "transport.h"
36#include "protocol.h"
37#include "debug.h"
38#include "scsiglue.h"
39
40#define DRV_NAME "ums-sddr55"
41
42MODULE_DESCRIPTION("Driver for SanDisk SDDR-55 SmartMedia reader");
43MODULE_AUTHOR("Simon Munton");
44MODULE_LICENSE("GPL");
45
46/*
47 * The table of devices
48 */
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) }
54
55static struct usb_device_id sddr55_usb_ids[] = {
56# include "unusual_sddr55.h"
57 { } /* Terminating entry */
58};
59MODULE_DEVICE_TABLE(usb, sddr55_usb_ids);
60
61#undef UNUSUAL_DEV
62
63/*
64 * The flags table
65 */
66#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
67 vendor_name, product_name, use_protocol, use_transport, \
68 init_function, Flags) \
69{ \
70 .vendorName = vendor_name, \
71 .productName = product_name, \
72 .useProtocol = use_protocol, \
73 .useTransport = use_transport, \
74 .initFunction = init_function, \
75}
76
77static struct us_unusual_dev sddr55_unusual_dev_list[] = {
78# include "unusual_sddr55.h"
79 { } /* Terminating entry */
80};
81
82#undef UNUSUAL_DEV
83
84
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)
88#define PAGESIZE 512
89
90#define set_sense_info(sk, asc, ascq) \
91 do { \
92 info->sense_data[2] = sk; \
93 info->sense_data[12] = asc; \
94 info->sense_data[13] = ascq; \
95 } while (0)
96
97
98struct 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];
113};
114
115
116#define NOT_ALLOCATED 0xffffffff
117#define BAD_BLOCK 0xffff
118#define CIS_BLOCK 0x400
119#define UNUSED_BLOCK 0x3ff
120
121static int
122sddr55_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;
127
128 if (!len)
129 return USB_STOR_XFER_GOOD;
130 info->last_access = jiffies;
131 return usb_stor_bulk_transfer_buf(us, pipe, data, len, NULL);
132}
133
134/*
135 * check if card inserted, if there is, update read_only status
136 * return non zero if no card
137 */
138
139static int sddr55_status(struct us_data *us)
140{
141 int result;
142 unsigned char *command = us->iobuf;
143 unsigned char *status = us->iobuf;
144 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
145
146 /* send command */
147 memset(command, 0, 8);
148 command[5] = 0xB0;
149 command[7] = 0x80;
150 result = sddr55_bulk_transport(us,
151 DMA_TO_DEVICE, command, 8);
152
153 usb_stor_dbg(us, "Result for send_command in status %d\n", result);
154
155 if (result != USB_STOR_XFER_GOOD) {
156 set_sense_info (4, 0, 0); /* hardware error */
157 return USB_STOR_TRANSPORT_ERROR;
158 }
159
160 result = sddr55_bulk_transport(us,
161 DMA_FROM_DEVICE, status, 4);
162
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;
170
171 info->fatal_error = 0;
172 info->force_read_only = 0;
173
174 set_sense_info (2, 0x3a, 0); /* not ready, medium not present */
175 return USB_STOR_TRANSPORT_FAILED;
176 }
177
178 if (result != USB_STOR_XFER_GOOD) {
179 set_sense_info (4, 0, 0); /* hardware error */
180 return USB_STOR_TRANSPORT_FAILED;
181 }
182
183 /* check write protect status */
184 info->read_only = (status[0] & 0x20);
185
186 /* now read status */
187 result = sddr55_bulk_transport(us,
188 DMA_FROM_DEVICE, status, 2);
189
190 if (result != USB_STOR_XFER_GOOD) {
191 set_sense_info (4, 0, 0); /* hardware error */
192 }
193
194 return (result == USB_STOR_XFER_GOOD ?
195 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_FAILED);
196}
197
198
199static int sddr55_read_data(struct us_data *us,
200 unsigned int lba,
201 unsigned int page,
202 unsigned short sectors) {
203
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;
209
210 unsigned int pba;
211 unsigned long address;
212
213 unsigned short pages;
214 unsigned int len, offset;
215 struct scatterlist *sg;
216
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.
220
221 len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
222 info->smallpageshift) * PAGESIZE;
223 buffer = kmalloc(len, GFP_NOIO);
224 if (buffer == NULL)
225 return USB_STOR_TRANSPORT_ERROR; /* out of memory */
226 offset = 0;
227 sg = NULL;
228
229 while (sectors>0) {
230
231 /* have we got to end? */
232 if (lba >= info->max_log_blks)
233 break;
234
235 pba = info->lba_to_pba[lba];
236
237 // Read as many sectors as possible in this block
238
239 pages = min((unsigned int) sectors << info->smallpageshift,
240 info->blocksize - page);
241 len = pages << info->pageshift;
242
243 usb_stor_dbg(us, "Read %02X pages, from PBA %04X (LBA %04X) page %02X\n",
244 pages, pba, lba, page);
245
246 if (pba == NOT_ALLOCATED) {
247 /* no pba for this lba, fill with zeroes */
248 memset (buffer, 0, len);
249 } else {
250
251 address = (pba << info->blockshift) + page;
252
253 command[0] = 0;
254 command[1] = LSB_of(address>>16);
255 command[2] = LSB_of(address>>8);
256 command[3] = LSB_of(address);
257
258 command[4] = 0;
259 command[5] = 0xB0;
260 command[6] = LSB_of(pages << (1 - info->smallpageshift));
261 command[7] = 0x85;
262
263 /* send command */
264 result = sddr55_bulk_transport(us,
265 DMA_TO_DEVICE, command, 8);
266
267 usb_stor_dbg(us, "Result for send_command in read_data %d\n",
268 result);
269
270 if (result != USB_STOR_XFER_GOOD) {
271 result = USB_STOR_TRANSPORT_ERROR;
272 goto leave;
273 }
274
275 /* read data */
276 result = sddr55_bulk_transport(us,
277 DMA_FROM_DEVICE, buffer, len);
278
279 if (result != USB_STOR_XFER_GOOD) {
280 result = USB_STOR_TRANSPORT_ERROR;
281 goto leave;
282 }
283
284 /* now read status */
285 result = sddr55_bulk_transport(us,
286 DMA_FROM_DEVICE, status, 2);
287
288 if (result != USB_STOR_XFER_GOOD) {
289 result = USB_STOR_TRANSPORT_ERROR;
290 goto leave;
291 }
292
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;
297 goto leave;
298 }
299 }
300
301 // Store the data in the transfer buffer
302 usb_stor_access_xfer_buf(buffer, len, us->srb,
303 &sg, &offset, TO_XFER_BUF);
304
305 page = 0;
306 lba++;
307 sectors -= pages >> info->smallpageshift;
308 }
309
310 result = USB_STOR_TRANSPORT_GOOD;
311
312leave:
313 kfree(buffer);
314
315 return result;
316}
317
318static int sddr55_write_data(struct us_data *us,
319 unsigned int lba,
320 unsigned int page,
321 unsigned short sectors) {
322
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;
328
329 unsigned int pba;
330 unsigned int new_pba;
331 unsigned long address;
332
333 unsigned short pages;
334 int i;
335 unsigned int len, offset;
336 struct scatterlist *sg;
337
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;
342 }
343
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.
347
348 len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
349 info->smallpageshift) * PAGESIZE;
350 buffer = kmalloc(len, GFP_NOIO);
351 if (buffer == NULL)
352 return USB_STOR_TRANSPORT_ERROR;
353 offset = 0;
354 sg = NULL;
355
356 while (sectors > 0) {
357
358 /* have we got to end? */
359 if (lba >= info->max_log_blks)
360 break;
361
362 pba = info->lba_to_pba[lba];
363
364 // Write as many sectors as possible in this block
365
366 pages = min((unsigned int) sectors << info->smallpageshift,
367 info->blocksize - page);
368 len = pages << info->pageshift;
369
370 // Get the data from the transfer buffer
371 usb_stor_access_xfer_buf(buffer, len, us->srb,
372 &sg, &offset, FROM_XFER_BUF);
373
374 usb_stor_dbg(us, "Write %02X pages, to PBA %04X (LBA %04X) page %02X\n",
375 pages, pba, lba, page);
376
377 command[4] = 0;
378
379 if (pba == NOT_ALLOCATED) {
380 /* no pba allocated for this lba, find a free pba to use */
381
382 int max_pba = (info->max_log_blks / 250 ) * 256;
383 int found_count = 0;
384 int found_pba = -1;
385
386 /* set pba to first block in zone lba is in */
387 pba = (lba / 1000) * 1024;
388
389 usb_stor_dbg(us, "No PBA for LBA %04X\n", lba);
390
391 if (max_pba > 1024)
392 max_pba = 1024;
393
394 /*
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.
399 */
400 for (i = 0; i < max_pba; i++, pba++) {
401 if (info->pba_to_lba[pba] == UNUSED_BLOCK) {
402 found_pba = pba;
403 if (found_count++ > 16)
404 break;
405 }
406 }
407
408 pba = found_pba;
409
410 if (pba == -1) {
411 /* oh dear */
412 usb_stor_dbg(us, "Couldn't find unallocated block\n");
413
414 set_sense_info (3, 0x31, 0); /* medium error */
415 result = USB_STOR_TRANSPORT_FAILED;
416 goto leave;
417 }
418
419 usb_stor_dbg(us, "Allocating PBA %04X for LBA %04X\n",
420 pba, lba);
421
422 /* set writing to unallocated block flag */
423 command[4] = 0x40;
424 }
425
426 address = (pba << info->blockshift) + page;
427
428 command[1] = LSB_of(address>>16);
429 command[2] = LSB_of(address>>8);
430 command[3] = LSB_of(address);
431
432 /* set the lba into the command, modulo 1000 */
433 command[0] = LSB_of(lba % 1000);
434 command[6] = MSB_of(lba % 1000);
435
436 command[4] |= LSB_of(pages >> info->smallpageshift);
437 command[5] = 0xB0;
438 command[7] = 0x86;
439
440 /* send command */
441 result = sddr55_bulk_transport(us,
442 DMA_TO_DEVICE, command, 8);
443
444 if (result != USB_STOR_XFER_GOOD) {
445 usb_stor_dbg(us, "Result for send_command in write_data %d\n",
446 result);
447
448 /* set_sense_info is superfluous here? */
449 set_sense_info (3, 0x3, 0);/* peripheral write error */
450 result = USB_STOR_TRANSPORT_FAILED;
451 goto leave;
452 }
453
454 /* send the data */
455 result = sddr55_bulk_transport(us,
456 DMA_TO_DEVICE, buffer, len);
457
458 if (result != USB_STOR_XFER_GOOD) {
459 usb_stor_dbg(us, "Result for send_data in write_data %d\n",
460 result);
461
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;
466 }
467
468 /* now read status */
469 result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, status, 6);
470
471 if (result != USB_STOR_XFER_GOOD) {
472 usb_stor_dbg(us, "Result for get_status in write_data %d\n",
473 result);
474
475 /* set_sense_info is superfluous here? */
476 set_sense_info (3, 0x3, 0);/* peripheral write error */
477 result = USB_STOR_TRANSPORT_FAILED;
478 goto leave;
479 }
480
481 new_pba = (status[3] + (status[4] << 8) + (status[5] << 16))
482 >> info->blockshift;
483
484 /* check status for error */
485 if (status[0] == 0xff && status[1] == 0x4) {
486 info->pba_to_lba[new_pba] = BAD_BLOCK;
487
488 set_sense_info (3, 0x0c, 0);
489 result = USB_STOR_TRANSPORT_FAILED;
490 goto leave;
491 }
492
493 usb_stor_dbg(us, "Updating maps for LBA %04X: old PBA %04X, new PBA %04X\n",
494 lba, pba, new_pba);
495
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;
499
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;
507 goto leave;
508 }
509
510 /* update the pba<->lba maps for new_pba */
511 info->pba_to_lba[new_pba] = lba % 1000;
512
513 page = 0;
514 lba++;
515 sectors -= pages >> info->smallpageshift;
516 }
517 result = USB_STOR_TRANSPORT_GOOD;
518
519 leave:
520 kfree(buffer);
521 return result;
522}
523
524static int sddr55_read_deviceID(struct us_data *us,
525 unsigned char *manufacturerID,
526 unsigned char *deviceID) {
527
528 int result;
529 unsigned char *command = us->iobuf;
530 unsigned char *content = us->iobuf;
531
532 memset(command, 0, 8);
533 command[5] = 0xB0;
534 command[7] = 0x84;
535 result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
536
537 usb_stor_dbg(us, "Result of send_control for device ID is %d\n",
538 result);
539
540 if (result != USB_STOR_XFER_GOOD)
541 return USB_STOR_TRANSPORT_ERROR;
542
543 result = sddr55_bulk_transport(us,
544 DMA_FROM_DEVICE, content, 4);
545
546 if (result != USB_STOR_XFER_GOOD)
547 return USB_STOR_TRANSPORT_ERROR;
548
549 *manufacturerID = content[0];
550 *deviceID = content[1];
551
552 if (content[0] != 0xff) {
553 result = sddr55_bulk_transport(us,
554 DMA_FROM_DEVICE, content, 2);
555 }
556
557 return USB_STOR_TRANSPORT_GOOD;
558}
559
560
561static int sddr55_reset(struct us_data *us)
562{
563 return 0;
564}
565
566
567static unsigned long sddr55_get_capacity(struct us_data *us) {
568
569 unsigned char uninitialized_var(manufacturerID);
570 unsigned char uninitialized_var(deviceID);
571 int result;
572 struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
573
574 usb_stor_dbg(us, "Reading capacity...\n");
575
576 result = sddr55_read_deviceID(us,
577 &manufacturerID,
578 &deviceID);
579
580 usb_stor_dbg(us, "Result of read_deviceID is %d\n", result);
581
582 if (result != USB_STOR_XFER_GOOD)
583 return 0;
584
585 usb_stor_dbg(us, "Device ID = %02X\n", deviceID);
586 usb_stor_dbg(us, "Manuf ID = %02X\n", manufacturerID);
587
588 info->pageshift = 9;
589 info->smallpageshift = 0;
590 info->blocksize = 16;
591 info->blockshift = 4;
592 info->blockmask = 15;
593
594 switch (deviceID) {
595
596 case 0x6e: // 1MB
597 case 0xe8:
598 case 0xec:
599 info->pageshift = 8;
600 info->smallpageshift = 1;
601 return 0x00100000;
602
603 case 0xea: // 2MB
604 case 0x64:
605 info->pageshift = 8;
606 info->smallpageshift = 1;
607 case 0x5d: // 5d is a ROM card with pagesize 512.
608 return 0x00200000;
609
610 case 0xe3: // 4MB
611 case 0xe5:
612 case 0x6b:
613 case 0xd5:
614 return 0x00400000;
615
616 case 0xe6: // 8MB
617 case 0xd6:
618 return 0x00800000;
619
620 case 0x73: // 16MB
621 info->blocksize = 32;
622 info->blockshift = 5;
623 info->blockmask = 31;
624 return 0x01000000;
625
626 case 0x75: // 32MB
627 info->blocksize = 32;
628 info->blockshift = 5;
629 info->blockmask = 31;
630 return 0x02000000;
631
632 case 0x76: // 64MB
633 info->blocksize = 32;
634 info->blockshift = 5;
635 info->blockmask = 31;
636 return 0x04000000;
637
638 case 0x79: // 128MB
639 info->blocksize = 32;
640 info->blockshift = 5;
641 info->blockmask = 31;
642 return 0x08000000;
643
644 default: // unknown
645 return 0;
646
647 }
648}
649
650static int sddr55_read_map(struct us_data *us) {
651
652 struct sddr55_card_info *info = (struct sddr55_card_info *)(us->extra);
653 int numblocks;
654 unsigned char *buffer;
655 unsigned char *command = us->iobuf;
656 int i;
657 unsigned short lba;
658 unsigned short max_lba;
659 int result;
660
661 if (!info->capacity)
662 return -1;
663
664 numblocks = info->capacity >> (info->blockshift + info->pageshift);
665
666 buffer = kmalloc( numblocks * 2, GFP_NOIO );
667
668 if (!buffer)
669 return -1;
670
671 memset(command, 0, 8);
672 command[5] = 0xB0;
673 command[6] = numblocks * 2 / 256;
674 command[7] = 0x8A;
675
676 result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
677
678 if ( result != USB_STOR_XFER_GOOD) {
679 kfree (buffer);
680 return -1;
681 }
682
683 result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, buffer, numblocks * 2);
684
685 if ( result != USB_STOR_XFER_GOOD) {
686 kfree (buffer);
687 return -1;
688 }
689
690 result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, command, 2);
691
692 if ( result != USB_STOR_XFER_GOOD) {
693 kfree (buffer);
694 return -1;
695 }
696
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);
701
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;
707 kfree(buffer);
708 return -1;
709 }
710
711 memset(info->lba_to_pba, 0xff, numblocks*sizeof(int));
712 memset(info->pba_to_lba, 0xff, numblocks*sizeof(int));
713
714 /* set maximum lba */
715 max_lba = info->max_log_blks;
716 if (max_lba > 1000)
717 max_lba = 1000;
718
719 /*
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)
722 */
723
724 for (i=0; i<numblocks; i++) {
725 int zone = i / 1024;
726
727 lba = short_pack(buffer[i * 2], buffer[i * 2 + 1]);
728
729 /*
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.
739 */
740
741 /*
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
745 */
746
747 info->pba_to_lba[i] = lba;
748
749 if (lba >= max_lba) {
750 continue;
751 }
752
753 if (info->lba_to_pba[lba + zone * 1000] != NOT_ALLOCATED &&
754 !info->force_read_only) {
755 printk(KERN_WARNING
756 "sddr55: map inconsistency at LBA %04X\n",
757 lba + zone * 1000);
758 info->force_read_only = 1;
759 }
760
761 if (lba<0x10 || (lba>=0x3E0 && lba<0x3EF))
762 usb_stor_dbg(us, "LBA %04X <-> PBA %04X\n", lba, i);
763
764 info->lba_to_pba[lba + zone * 1000] = i;
765 }
766
767 kfree(buffer);
768 return 0;
769}
770
771
772static void sddr55_card_info_destructor(void *extra) {
773 struct sddr55_card_info *info = (struct sddr55_card_info *)extra;
774
775 if (!extra)
776 return;
777
778 kfree(info->lba_to_pba);
779 kfree(info->pba_to_lba);
780}
781
782
783/*
784 * Transport for the Sandisk SDDR-55
785 */
786static int sddr55_transport(struct scsi_cmnd *srb, struct us_data *us)
787{
788 int result;
789 static unsigned char inquiry_response[8] = {
790 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
791 };
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,
795 0x01, 0x0A,
796 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
797 };
798 unsigned char *ptr = us->iobuf;
799 unsigned long capacity;
800 unsigned int lba;
801 unsigned int pba;
802 unsigned int page;
803 unsigned short pages;
804 struct sddr55_card_info *info;
805
806 if (!us->extra) {
807 us->extra = kzalloc(
808 sizeof(struct sddr55_card_info), GFP_NOIO);
809 if (!us->extra)
810 return USB_STOR_TRANSPORT_ERROR;
811 us->extra_destructor = sddr55_card_info_destructor;
812 }
813
814 info = (struct sddr55_card_info *)(us->extra);
815
816 if (srb->cmnd[0] == REQUEST_SENSE) {
817 usb_stor_dbg(us, "request sense %02x/%02x/%02x\n",
818 info->sense_data[2],
819 info->sense_data[12],
820 info->sense_data[13]);
821
822 memcpy (ptr, info->sense_data, sizeof info->sense_data);
823 ptr[0] = 0x70;
824 ptr[7] = 11;
825 usb_stor_set_xfer_buf (ptr, sizeof info->sense_data, srb);
826 memset (info->sense_data, 0, sizeof info->sense_data);
827
828 return USB_STOR_TRANSPORT_GOOD;
829 }
830
831 memset (info->sense_data, 0, sizeof info->sense_data);
832
833 /*
834 * Dummy up a response for INQUIRY since SDDR55 doesn't
835 * respond to INQUIRY commands
836 */
837
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;
842 }
843
844 /*
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
847 */
848 if (info->lba_to_pba == NULL || time_after(jiffies, info->last_access + HZ/2)) {
849
850 /* check to see if a card is fitted */
851 result = sddr55_status (us);
852 if (result) {
853 result = sddr55_status (us);
854 if (!result) {
855 set_sense_info (6, 0x28, 0); /* new media, set unit attention, not ready to ready */
856 }
857 return USB_STOR_TRANSPORT_FAILED;
858 }
859 }
860
861 /*
862 * if we detected a problem with the map when writing,
863 * don't allow any more access
864 */
865 if (info->fatal_error) {
866
867 set_sense_info (3, 0x31, 0);
868 return USB_STOR_TRANSPORT_FAILED;
869 }
870
871 if (srb->cmnd[0] == READ_CAPACITY) {
872
873 capacity = sddr55_get_capacity(us);
874
875 if (!capacity) {
876 set_sense_info (3, 0x30, 0); /* incompatible medium */
877 return USB_STOR_TRANSPORT_FAILED;
878 }
879
880 info->capacity = capacity;
881
882 /*
883 * figure out the maximum logical block number, allowing for
884 * the fact that only 250 out of every 256 are used
885 */
886 info->max_log_blks = ((info->capacity >> (info->pageshift + info->blockshift)) / 256) * 250;
887
888 /*
889 * Last page in the card, adjust as we only use 250 out of
890 * every 256 pages
891 */
892 capacity = (capacity / 256) * 250;
893
894 capacity /= PAGESIZE;
895 capacity--;
896
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);
900
901 sddr55_read_map(us);
902
903 return USB_STOR_TRANSPORT_GOOD;
904 }
905
906 if (srb->cmnd[0] == MODE_SENSE_10) {
907
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);
911
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;
915
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;
919 }
920
921 set_sense_info (5, 0x24, 0); /* invalid field in command */
922 return USB_STOR_TRANSPORT_FAILED;
923 }
924
925 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
926
927 usb_stor_dbg(us, "%s medium removal. Not that I can do anything about it...\n",
928 (srb->cmnd[4]&0x03) ? "Prevent" : "Allow");
929
930 return USB_STOR_TRANSPORT_GOOD;
931
932 }
933
934 if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10) {
935
936 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
937 page <<= 16;
938 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
939 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
940
941 page <<= info->smallpageshift;
942
943 // convert page to block and page-within-block
944
945 lba = page >> info->blockshift;
946 page = page & info->blockmask;
947
948 // locate physical block corresponding to logical block
949
950 if (lba >= info->max_log_blks) {
951
952 usb_stor_dbg(us, "Error: Requested LBA %04X exceeds maximum block %04X\n",
953 lba, info->max_log_blks - 1);
954
955 set_sense_info (5, 0x24, 0); /* invalid field in command */
956
957 return USB_STOR_TRANSPORT_FAILED;
958 }
959
960 pba = info->lba_to_pba[lba];
961
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);
965
966 return sddr55_write_data(us, lba, page, pages);
967 } else {
968 usb_stor_dbg(us, "READ_10: read block %04X (LBA %04X) page %01X pages %d\n",
969 pba, lba, page, pages);
970
971 return sddr55_read_data(us, lba, page, pages);
972 }
973 }
974
975
976 if (srb->cmnd[0] == TEST_UNIT_READY) {
977 return USB_STOR_TRANSPORT_GOOD;
978 }
979
980 if (srb->cmnd[0] == START_STOP) {
981 return USB_STOR_TRANSPORT_GOOD;
982 }
983
984 set_sense_info (5, 0x20, 0); /* illegal command */
985
986 return USB_STOR_TRANSPORT_FAILED; // FIXME: sense buffer?
987}
988
989static struct scsi_host_template sddr55_host_template;
990
991static int sddr55_probe(struct usb_interface *intf,
992 const struct usb_device_id *id)
993{
994 struct us_data *us;
995 int result;
996
997 result = usb_stor_probe1(&us, intf, id,
998 (id - sddr55_usb_ids) + sddr55_unusual_dev_list,
999 &sddr55_host_template);
1000 if (result)
1001 return result;
1002
1003 us->transport_name = "SDDR55";
1004 us->transport = sddr55_transport;
1005 us->transport_reset = sddr55_reset;
1006 us->max_lun = 0;
1007
1008 result = usb_stor_probe2(us);
1009 return result;
1010}
1011
1012static struct usb_driver sddr55_driver = {
1013 .name = DRV_NAME,
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,
1022 .soft_unbind = 1,
1023 .no_dynamic_id = 1,
1024};
1025
1026module_usb_stor_driver(sddr55_driver, sddr55_host_template, DRV_NAME);