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