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