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