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