Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Driver for SanDisk SDDR-55 SmartMedia reader
0004  *
0005  * SDDR55 driver v0.1:
0006  *
0007  * First release
0008  *
0009  * Current development and maintenance by:
0010  *   (c) 2002 Simon Munton
0011  */
0012 
0013 #include <linux/jiffies.h>
0014 #include <linux/errno.h>
0015 #include <linux/module.h>
0016 #include <linux/slab.h>
0017 
0018 #include <scsi/scsi.h>
0019 #include <scsi/scsi_cmnd.h>
0020 
0021 #include "usb.h"
0022 #include "transport.h"
0023 #include "protocol.h"
0024 #include "debug.h"
0025 #include "scsiglue.h"
0026 
0027 #define DRV_NAME "ums-sddr55"
0028 
0029 MODULE_DESCRIPTION("Driver for SanDisk SDDR-55 SmartMedia reader");
0030 MODULE_AUTHOR("Simon Munton");
0031 MODULE_LICENSE("GPL");
0032 MODULE_IMPORT_NS(USB_STORAGE);
0033 
0034 /*
0035  * The table of devices
0036  */
0037 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
0038             vendorName, productName, useProtocol, useTransport, \
0039             initFunction, flags) \
0040 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
0041   .driver_info = (flags) }
0042 
0043 static struct usb_device_id sddr55_usb_ids[] = {
0044 #   include "unusual_sddr55.h"
0045     { }     /* Terminating entry */
0046 };
0047 MODULE_DEVICE_TABLE(usb, sddr55_usb_ids);
0048 
0049 #undef UNUSUAL_DEV
0050 
0051 /*
0052  * The flags table
0053  */
0054 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
0055             vendor_name, product_name, use_protocol, use_transport, \
0056             init_function, Flags) \
0057 { \
0058     .vendorName = vendor_name,  \
0059     .productName = product_name,    \
0060     .useProtocol = use_protocol,    \
0061     .useTransport = use_transport,  \
0062     .initFunction = init_function,  \
0063 }
0064 
0065 static struct us_unusual_dev sddr55_unusual_dev_list[] = {
0066 #   include "unusual_sddr55.h"
0067     { }     /* Terminating entry */
0068 };
0069 
0070 #undef UNUSUAL_DEV
0071 
0072 
0073 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
0074 #define LSB_of(s) ((s)&0xFF)
0075 #define MSB_of(s) ((s)>>8)
0076 #define PAGESIZE  512
0077 
0078 #define set_sense_info(sk, asc, ascq)   \
0079     do {                \
0080     info->sense_data[2] = sk;   \
0081     info->sense_data[12] = asc; \
0082     info->sense_data[13] = ascq;    \
0083     } while (0)
0084 
0085 
0086 struct sddr55_card_info {
0087     unsigned long   capacity;   /* Size of card in bytes */
0088     int     max_log_blks;   /* maximum number of logical blocks */
0089     int     pageshift;  /* log2 of pagesize */
0090     int     smallpageshift; /* 1 if pagesize == 256 */
0091     int     blocksize;  /* Size of block in pages */
0092     int     blockshift; /* log2 of blocksize */
0093     int     blockmask;  /* 2^blockshift - 1 */
0094     int     read_only;  /* non zero if card is write protected */
0095     int     force_read_only;    /* non zero if we find a map error*/
0096     int     *lba_to_pba;    /* logical to physical map */
0097     int     *pba_to_lba;    /* physical to logical map */
0098     int     fatal_error;    /* set if we detect something nasty */
0099     unsigned long   last_access;    /* number of jiffies since we last talked to device */
0100     unsigned char   sense_data[18];
0101 };
0102 
0103 
0104 #define NOT_ALLOCATED       0xffffffff
0105 #define BAD_BLOCK       0xffff
0106 #define CIS_BLOCK       0x400
0107 #define UNUSED_BLOCK        0x3ff
0108 
0109 static int
0110 sddr55_bulk_transport(struct us_data *us, int direction,
0111               unsigned char *data, unsigned int len) {
0112     struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
0113     unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
0114             us->recv_bulk_pipe : us->send_bulk_pipe;
0115 
0116     if (!len)
0117         return USB_STOR_XFER_GOOD;
0118     info->last_access = jiffies;
0119     return usb_stor_bulk_transfer_buf(us, pipe, data, len, NULL);
0120 }
0121 
0122 /*
0123  * check if card inserted, if there is, update read_only status
0124  * return non zero if no card
0125  */
0126 
0127 static int sddr55_status(struct us_data *us)
0128 {
0129     int result;
0130     unsigned char *command = us->iobuf;
0131     unsigned char *status = us->iobuf;
0132     struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
0133 
0134     /* send command */
0135     memset(command, 0, 8);
0136     command[5] = 0xB0;
0137     command[7] = 0x80;
0138     result = sddr55_bulk_transport(us,
0139         DMA_TO_DEVICE, command, 8);
0140 
0141     usb_stor_dbg(us, "Result for send_command in status %d\n", result);
0142 
0143     if (result != USB_STOR_XFER_GOOD) {
0144         set_sense_info (4, 0, 0);   /* hardware error */
0145         return USB_STOR_TRANSPORT_ERROR;
0146     }
0147 
0148     result = sddr55_bulk_transport(us,
0149         DMA_FROM_DEVICE, status,    4);
0150 
0151     /* expect to get short transfer if no card fitted */
0152     if (result == USB_STOR_XFER_SHORT || result == USB_STOR_XFER_STALLED) {
0153         /* had a short transfer, no card inserted, free map memory */
0154         kfree(info->lba_to_pba);
0155         kfree(info->pba_to_lba);
0156         info->lba_to_pba = NULL;
0157         info->pba_to_lba = NULL;
0158 
0159         info->fatal_error = 0;
0160         info->force_read_only = 0;
0161 
0162         set_sense_info (2, 0x3a, 0);    /* not ready, medium not present */
0163         return USB_STOR_TRANSPORT_FAILED;
0164     }
0165 
0166     if (result != USB_STOR_XFER_GOOD) {
0167         set_sense_info (4, 0, 0);   /* hardware error */
0168         return USB_STOR_TRANSPORT_FAILED;
0169     }
0170     
0171     /* check write protect status */
0172     info->read_only = (status[0] & 0x20);
0173 
0174     /* now read status */
0175     result = sddr55_bulk_transport(us,
0176         DMA_FROM_DEVICE, status,    2);
0177 
0178     if (result != USB_STOR_XFER_GOOD) {
0179         set_sense_info (4, 0, 0);   /* hardware error */
0180     }
0181 
0182     return (result == USB_STOR_XFER_GOOD ?
0183             USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_FAILED);
0184 }
0185 
0186 
0187 static int sddr55_read_data(struct us_data *us,
0188         unsigned int lba,
0189         unsigned int page,
0190         unsigned short sectors) {
0191 
0192     int result = USB_STOR_TRANSPORT_GOOD;
0193     unsigned char *command = us->iobuf;
0194     unsigned char *status = us->iobuf;
0195     struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
0196     unsigned char *buffer;
0197 
0198     unsigned int pba;
0199     unsigned long address;
0200 
0201     unsigned short pages;
0202     unsigned int len, offset;
0203     struct scatterlist *sg;
0204 
0205     // Since we only read in one block at a time, we have to create
0206     // a bounce buffer and move the data a piece at a time between the
0207     // bounce buffer and the actual transfer buffer.
0208 
0209     len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
0210             info->smallpageshift) * PAGESIZE;
0211     buffer = kmalloc(len, GFP_NOIO);
0212     if (buffer == NULL)
0213         return USB_STOR_TRANSPORT_ERROR; /* out of memory */
0214     offset = 0;
0215     sg = NULL;
0216 
0217     while (sectors>0) {
0218 
0219         /* have we got to end? */
0220         if (lba >= info->max_log_blks)
0221             break;
0222 
0223         pba = info->lba_to_pba[lba];
0224 
0225         // Read as many sectors as possible in this block
0226 
0227         pages = min((unsigned int) sectors << info->smallpageshift,
0228                 info->blocksize - page);
0229         len = pages << info->pageshift;
0230 
0231         usb_stor_dbg(us, "Read %02X pages, from PBA %04X (LBA %04X) page %02X\n",
0232                  pages, pba, lba, page);
0233 
0234         if (pba == NOT_ALLOCATED) {
0235             /* no pba for this lba, fill with zeroes */
0236             memset (buffer, 0, len);
0237         } else {
0238 
0239             address = (pba << info->blockshift) + page;
0240 
0241             command[0] = 0;
0242             command[1] = LSB_of(address>>16);
0243             command[2] = LSB_of(address>>8);
0244             command[3] = LSB_of(address);
0245 
0246             command[4] = 0;
0247             command[5] = 0xB0;
0248             command[6] = LSB_of(pages << (1 - info->smallpageshift));
0249             command[7] = 0x85;
0250 
0251             /* send command */
0252             result = sddr55_bulk_transport(us,
0253                 DMA_TO_DEVICE, command, 8);
0254 
0255             usb_stor_dbg(us, "Result for send_command in read_data %d\n",
0256                      result);
0257 
0258             if (result != USB_STOR_XFER_GOOD) {
0259                 result = USB_STOR_TRANSPORT_ERROR;
0260                 goto leave;
0261             }
0262 
0263             /* read data */
0264             result = sddr55_bulk_transport(us,
0265                 DMA_FROM_DEVICE, buffer, len);
0266 
0267             if (result != USB_STOR_XFER_GOOD) {
0268                 result = USB_STOR_TRANSPORT_ERROR;
0269                 goto leave;
0270             }
0271 
0272             /* now read status */
0273             result = sddr55_bulk_transport(us,
0274                 DMA_FROM_DEVICE, status, 2);
0275 
0276             if (result != USB_STOR_XFER_GOOD) {
0277                 result = USB_STOR_TRANSPORT_ERROR;
0278                 goto leave;
0279             }
0280 
0281             /* check status for error */
0282             if (status[0] == 0xff && status[1] == 0x4) {
0283                 set_sense_info (3, 0x11, 0);
0284                 result = USB_STOR_TRANSPORT_FAILED;
0285                 goto leave;
0286             }
0287         }
0288 
0289         // Store the data in the transfer buffer
0290         usb_stor_access_xfer_buf(buffer, len, us->srb,
0291                 &sg, &offset, TO_XFER_BUF);
0292 
0293         page = 0;
0294         lba++;
0295         sectors -= pages >> info->smallpageshift;
0296     }
0297 
0298     result = USB_STOR_TRANSPORT_GOOD;
0299 
0300 leave:
0301     kfree(buffer);
0302 
0303     return result;
0304 }
0305 
0306 static int sddr55_write_data(struct us_data *us,
0307         unsigned int lba,
0308         unsigned int page,
0309         unsigned short sectors) {
0310 
0311     int result = USB_STOR_TRANSPORT_GOOD;
0312     unsigned char *command = us->iobuf;
0313     unsigned char *status = us->iobuf;
0314     struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
0315     unsigned char *buffer;
0316 
0317     unsigned int pba;
0318     unsigned int new_pba;
0319     unsigned long address;
0320 
0321     unsigned short pages;
0322     int i;
0323     unsigned int len, offset;
0324     struct scatterlist *sg;
0325 
0326     /* check if we are allowed to write */
0327     if (info->read_only || info->force_read_only) {
0328         set_sense_info (7, 0x27, 0);    /* read only */
0329         return USB_STOR_TRANSPORT_FAILED;
0330     }
0331 
0332     // Since we only write one block at a time, we have to create
0333     // a bounce buffer and move the data a piece at a time between the
0334     // bounce buffer and the actual transfer buffer.
0335 
0336     len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
0337             info->smallpageshift) * PAGESIZE;
0338     buffer = kmalloc(len, GFP_NOIO);
0339     if (buffer == NULL)
0340         return USB_STOR_TRANSPORT_ERROR;
0341     offset = 0;
0342     sg = NULL;
0343 
0344     while (sectors > 0) {
0345 
0346         /* have we got to end? */
0347         if (lba >= info->max_log_blks)
0348             break;
0349 
0350         pba = info->lba_to_pba[lba];
0351 
0352         // Write as many sectors as possible in this block
0353 
0354         pages = min((unsigned int) sectors << info->smallpageshift,
0355                 info->blocksize - page);
0356         len = pages << info->pageshift;
0357 
0358         // Get the data from the transfer buffer
0359         usb_stor_access_xfer_buf(buffer, len, us->srb,
0360                 &sg, &offset, FROM_XFER_BUF);
0361 
0362         usb_stor_dbg(us, "Write %02X pages, to PBA %04X (LBA %04X) page %02X\n",
0363                  pages, pba, lba, page);
0364             
0365         command[4] = 0;
0366 
0367         if (pba == NOT_ALLOCATED) {
0368             /* no pba allocated for this lba, find a free pba to use */
0369 
0370             int max_pba = (info->max_log_blks / 250 ) * 256;
0371             int found_count = 0;
0372             int found_pba = -1;
0373 
0374             /* set pba to first block in zone lba is in */
0375             pba = (lba / 1000) * 1024;
0376 
0377             usb_stor_dbg(us, "No PBA for LBA %04X\n", lba);
0378 
0379             if (max_pba > 1024)
0380                 max_pba = 1024;
0381 
0382             /*
0383              * Scan through the map looking for an unused block
0384              * leave 16 unused blocks at start (or as many as
0385              * possible) since the sddr55 seems to reuse a used
0386              * block when it shouldn't if we don't leave space.
0387              */
0388             for (i = 0; i < max_pba; i++, pba++) {
0389                 if (info->pba_to_lba[pba] == UNUSED_BLOCK) {
0390                     found_pba = pba;
0391                     if (found_count++ > 16)
0392                         break;
0393                 }
0394             }
0395 
0396             pba = found_pba;
0397 
0398             if (pba == -1) {
0399                 /* oh dear */
0400                 usb_stor_dbg(us, "Couldn't find unallocated block\n");
0401 
0402                 set_sense_info (3, 0x31, 0);    /* medium error */
0403                 result = USB_STOR_TRANSPORT_FAILED;
0404                 goto leave;
0405             }
0406 
0407             usb_stor_dbg(us, "Allocating PBA %04X for LBA %04X\n",
0408                      pba, lba);
0409 
0410             /* set writing to unallocated block flag */
0411             command[4] = 0x40;
0412         }
0413 
0414         address = (pba << info->blockshift) + page;
0415 
0416         command[1] = LSB_of(address>>16);
0417         command[2] = LSB_of(address>>8); 
0418         command[3] = LSB_of(address);
0419 
0420         /* set the lba into the command, modulo 1000 */
0421         command[0] = LSB_of(lba % 1000);
0422         command[6] = MSB_of(lba % 1000);
0423 
0424         command[4] |= LSB_of(pages >> info->smallpageshift);
0425         command[5] = 0xB0;
0426         command[7] = 0x86;
0427 
0428         /* send command */
0429         result = sddr55_bulk_transport(us,
0430             DMA_TO_DEVICE, command, 8);
0431 
0432         if (result != USB_STOR_XFER_GOOD) {
0433             usb_stor_dbg(us, "Result for send_command in write_data %d\n",
0434                      result);
0435 
0436             /* set_sense_info is superfluous here? */
0437             set_sense_info (3, 0x3, 0);/* peripheral write error */
0438             result = USB_STOR_TRANSPORT_FAILED;
0439             goto leave;
0440         }
0441 
0442         /* send the data */
0443         result = sddr55_bulk_transport(us,
0444             DMA_TO_DEVICE, buffer, len);
0445 
0446         if (result != USB_STOR_XFER_GOOD) {
0447             usb_stor_dbg(us, "Result for send_data in write_data %d\n",
0448                      result);
0449 
0450             /* set_sense_info is superfluous here? */
0451             set_sense_info (3, 0x3, 0);/* peripheral write error */
0452             result = USB_STOR_TRANSPORT_FAILED;
0453             goto leave;
0454         }
0455 
0456         /* now read status */
0457         result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, status, 6);
0458 
0459         if (result != USB_STOR_XFER_GOOD) {
0460             usb_stor_dbg(us, "Result for get_status in write_data %d\n",
0461                      result);
0462 
0463             /* set_sense_info is superfluous here? */
0464             set_sense_info (3, 0x3, 0);/* peripheral write error */
0465             result = USB_STOR_TRANSPORT_FAILED;
0466             goto leave;
0467         }
0468 
0469         new_pba = (status[3] + (status[4] << 8) + (status[5] << 16))
0470                           >> info->blockshift;
0471 
0472         /* check status for error */
0473         if (status[0] == 0xff && status[1] == 0x4) {
0474             info->pba_to_lba[new_pba] = BAD_BLOCK;
0475 
0476             set_sense_info (3, 0x0c, 0);
0477             result = USB_STOR_TRANSPORT_FAILED;
0478             goto leave;
0479         }
0480 
0481         usb_stor_dbg(us, "Updating maps for LBA %04X: old PBA %04X, new PBA %04X\n",
0482                  lba, pba, new_pba);
0483 
0484         /* update the lba<->pba maps, note new_pba might be the same as pba */
0485         info->lba_to_pba[lba] = new_pba;
0486         info->pba_to_lba[pba] = UNUSED_BLOCK;
0487 
0488         /* check that new_pba wasn't already being used */
0489         if (info->pba_to_lba[new_pba] != UNUSED_BLOCK) {
0490             printk(KERN_ERR "sddr55 error: new PBA %04X already in use for LBA %04X\n",
0491                 new_pba, info->pba_to_lba[new_pba]);
0492             info->fatal_error = 1;
0493             set_sense_info (3, 0x31, 0);
0494             result = USB_STOR_TRANSPORT_FAILED;
0495             goto leave;
0496         }
0497 
0498         /* update the pba<->lba maps for new_pba */
0499         info->pba_to_lba[new_pba] = lba % 1000;
0500 
0501         page = 0;
0502         lba++;
0503         sectors -= pages >> info->smallpageshift;
0504     }
0505     result = USB_STOR_TRANSPORT_GOOD;
0506 
0507  leave:
0508     kfree(buffer);
0509     return result;
0510 }
0511 
0512 static int sddr55_read_deviceID(struct us_data *us,
0513         unsigned char *manufacturerID,
0514         unsigned char *deviceID) {
0515 
0516     int result;
0517     unsigned char *command = us->iobuf;
0518     unsigned char *content = us->iobuf;
0519 
0520     memset(command, 0, 8);
0521     command[5] = 0xB0;
0522     command[7] = 0x84;
0523     result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
0524 
0525     usb_stor_dbg(us, "Result of send_control for device ID is %d\n",
0526              result);
0527 
0528     if (result != USB_STOR_XFER_GOOD)
0529         return USB_STOR_TRANSPORT_ERROR;
0530 
0531     result = sddr55_bulk_transport(us,
0532         DMA_FROM_DEVICE, content, 4);
0533 
0534     if (result != USB_STOR_XFER_GOOD)
0535         return USB_STOR_TRANSPORT_ERROR;
0536 
0537     *manufacturerID = content[0];
0538     *deviceID = content[1];
0539 
0540     if (content[0] != 0xff) {
0541             result = sddr55_bulk_transport(us,
0542             DMA_FROM_DEVICE, content, 2);
0543     }
0544 
0545     return USB_STOR_TRANSPORT_GOOD;
0546 }
0547 
0548 
0549 static int sddr55_reset(struct us_data *us)
0550 {
0551     return 0;
0552 }
0553 
0554 
0555 static unsigned long sddr55_get_capacity(struct us_data *us) {
0556 
0557     unsigned char manufacturerID;
0558     unsigned char deviceID;
0559     int result;
0560     struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
0561 
0562     usb_stor_dbg(us, "Reading capacity...\n");
0563 
0564     result = sddr55_read_deviceID(us,
0565         &manufacturerID,
0566         &deviceID);
0567 
0568     usb_stor_dbg(us, "Result of read_deviceID is %d\n", result);
0569 
0570     if (result != USB_STOR_XFER_GOOD)
0571         return 0;
0572 
0573     usb_stor_dbg(us, "Device ID = %02X\n", deviceID);
0574     usb_stor_dbg(us, "Manuf  ID = %02X\n", manufacturerID);
0575 
0576     info->pageshift = 9;
0577     info->smallpageshift = 0;
0578     info->blocksize = 16;
0579     info->blockshift = 4;
0580     info->blockmask = 15;
0581 
0582     switch (deviceID) {
0583 
0584     case 0x6e: // 1MB
0585     case 0xe8:
0586     case 0xec:
0587         info->pageshift = 8;
0588         info->smallpageshift = 1;
0589         return 0x00100000;
0590 
0591     case 0xea: // 2MB
0592     case 0x64:
0593         info->pageshift = 8;
0594         info->smallpageshift = 1;
0595         fallthrough;
0596     case 0x5d: // 5d is a ROM card with pagesize 512.
0597         return 0x00200000;
0598 
0599     case 0xe3: // 4MB
0600     case 0xe5:
0601     case 0x6b:
0602     case 0xd5:
0603         return 0x00400000;
0604 
0605     case 0xe6: // 8MB
0606     case 0xd6:
0607         return 0x00800000;
0608 
0609     case 0x73: // 16MB
0610         info->blocksize = 32;
0611         info->blockshift = 5;
0612         info->blockmask = 31;
0613         return 0x01000000;
0614 
0615     case 0x75: // 32MB
0616         info->blocksize = 32;
0617         info->blockshift = 5;
0618         info->blockmask = 31;
0619         return 0x02000000;
0620 
0621     case 0x76: // 64MB
0622         info->blocksize = 32;
0623         info->blockshift = 5;
0624         info->blockmask = 31;
0625         return 0x04000000;
0626 
0627     case 0x79: // 128MB
0628         info->blocksize = 32;
0629         info->blockshift = 5;
0630         info->blockmask = 31;
0631         return 0x08000000;
0632 
0633     default: // unknown
0634         return 0;
0635 
0636     }
0637 }
0638 
0639 static int sddr55_read_map(struct us_data *us) {
0640 
0641     struct sddr55_card_info *info = (struct sddr55_card_info *)(us->extra);
0642     int numblocks;
0643     unsigned char *buffer;
0644     unsigned char *command = us->iobuf;
0645     int i;
0646     unsigned short lba;
0647     unsigned short max_lba;
0648     int result;
0649 
0650     if (!info->capacity)
0651         return -1;
0652 
0653     numblocks = info->capacity >> (info->blockshift + info->pageshift);
0654     
0655     buffer = kmalloc_array(numblocks, 2, GFP_NOIO );
0656     
0657     if (!buffer)
0658         return -1;
0659 
0660     memset(command, 0, 8);
0661     command[5] = 0xB0;
0662     command[6] = numblocks * 2 / 256;
0663     command[7] = 0x8A;
0664 
0665     result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
0666 
0667     if ( result != USB_STOR_XFER_GOOD) {
0668         kfree (buffer);
0669         return -1;
0670     }
0671 
0672     result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, buffer, numblocks * 2);
0673 
0674     if ( result != USB_STOR_XFER_GOOD) {
0675         kfree (buffer);
0676         return -1;
0677     }
0678 
0679     result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, command, 2);
0680 
0681     if ( result != USB_STOR_XFER_GOOD) {
0682         kfree (buffer);
0683         return -1;
0684     }
0685 
0686     kfree(info->lba_to_pba);
0687     kfree(info->pba_to_lba);
0688     info->lba_to_pba = kmalloc_array(numblocks, sizeof(int), GFP_NOIO);
0689     info->pba_to_lba = kmalloc_array(numblocks, sizeof(int), GFP_NOIO);
0690 
0691     if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) {
0692         kfree(info->lba_to_pba);
0693         kfree(info->pba_to_lba);
0694         info->lba_to_pba = NULL;
0695         info->pba_to_lba = NULL;
0696         kfree(buffer);
0697         return -1;
0698     }
0699 
0700     memset(info->lba_to_pba, 0xff, numblocks*sizeof(int));
0701     memset(info->pba_to_lba, 0xff, numblocks*sizeof(int));
0702 
0703     /* set maximum lba */
0704     max_lba = info->max_log_blks;
0705     if (max_lba > 1000)
0706         max_lba = 1000;
0707 
0708     /*
0709      * Each block is 64 bytes of control data, so block i is located in
0710      * scatterlist block i*64/128k = i*(2^6)*(2^-17) = i*(2^-11)
0711      */
0712 
0713     for (i=0; i<numblocks; i++) {
0714         int zone = i / 1024;
0715 
0716         lba = short_pack(buffer[i * 2], buffer[i * 2 + 1]);
0717 
0718             /*
0719              * Every 1024 physical blocks ("zone"), the LBA numbers
0720              * go back to zero, but are within a higher
0721              * block of LBA's. Also, there is a maximum of
0722              * 1000 LBA's per zone. In other words, in PBA
0723              * 1024-2047 you will find LBA 0-999 which are
0724              * really LBA 1000-1999. Yes, this wastes 24
0725              * physical blocks per zone. Go figure. 
0726              * These devices can have blocks go bad, so there
0727              * are 24 spare blocks to use when blocks do go bad.
0728              */
0729 
0730             /*
0731              * SDDR55 returns 0xffff for a bad block, and 0x400 for the 
0732              * CIS block. (Is this true for cards 8MB or less??)
0733              * Record these in the physical to logical map
0734              */ 
0735 
0736         info->pba_to_lba[i] = lba;
0737 
0738         if (lba >= max_lba) {
0739             continue;
0740         }
0741         
0742         if (info->lba_to_pba[lba + zone * 1000] != NOT_ALLOCATED &&
0743             !info->force_read_only) {
0744             printk(KERN_WARNING
0745                    "sddr55: map inconsistency at LBA %04X\n",
0746                    lba + zone * 1000);
0747             info->force_read_only = 1;
0748         }
0749 
0750         if (lba<0x10 || (lba>=0x3E0 && lba<0x3EF))
0751             usb_stor_dbg(us, "LBA %04X <-> PBA %04X\n", lba, i);
0752 
0753         info->lba_to_pba[lba + zone * 1000] = i;
0754     }
0755 
0756     kfree(buffer);
0757     return 0;
0758 }
0759 
0760 
0761 static void sddr55_card_info_destructor(void *extra) {
0762     struct sddr55_card_info *info = (struct sddr55_card_info *)extra;
0763 
0764     if (!extra)
0765         return;
0766 
0767     kfree(info->lba_to_pba);
0768     kfree(info->pba_to_lba);
0769 }
0770 
0771 
0772 /*
0773  * Transport for the Sandisk SDDR-55
0774  */
0775 static int sddr55_transport(struct scsi_cmnd *srb, struct us_data *us)
0776 {
0777     int result;
0778     static unsigned char inquiry_response[8] = {
0779         0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
0780     };
0781     // write-protected for now, no block descriptor support
0782     static unsigned char mode_page_01[20] = {
0783         0x0, 0x12, 0x00, 0x80, 0x0, 0x0, 0x0, 0x0,
0784         0x01, 0x0A,
0785         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
0786     };
0787     unsigned char *ptr = us->iobuf;
0788     unsigned long capacity;
0789     unsigned int lba;
0790     unsigned int pba;
0791     unsigned int page;
0792     unsigned short pages;
0793     struct sddr55_card_info *info;
0794 
0795     if (!us->extra) {
0796         us->extra = kzalloc(
0797             sizeof(struct sddr55_card_info), GFP_NOIO);
0798         if (!us->extra)
0799             return USB_STOR_TRANSPORT_ERROR;
0800         us->extra_destructor = sddr55_card_info_destructor;
0801     }
0802 
0803     info = (struct sddr55_card_info *)(us->extra);
0804 
0805     if (srb->cmnd[0] == REQUEST_SENSE) {
0806         usb_stor_dbg(us, "request sense %02x/%02x/%02x\n",
0807                  info->sense_data[2],
0808                  info->sense_data[12],
0809                  info->sense_data[13]);
0810 
0811         memcpy (ptr, info->sense_data, sizeof info->sense_data);
0812         ptr[0] = 0x70;
0813         ptr[7] = 11;
0814         usb_stor_set_xfer_buf (ptr, sizeof info->sense_data, srb);
0815         memset (info->sense_data, 0, sizeof info->sense_data);
0816 
0817         return USB_STOR_TRANSPORT_GOOD;
0818     }
0819 
0820     memset (info->sense_data, 0, sizeof info->sense_data);
0821 
0822     /*
0823      * Dummy up a response for INQUIRY since SDDR55 doesn't
0824      * respond to INQUIRY commands
0825      */
0826 
0827     if (srb->cmnd[0] == INQUIRY) {
0828         memcpy(ptr, inquiry_response, 8);
0829         fill_inquiry_response(us, ptr, 36);
0830         return USB_STOR_TRANSPORT_GOOD;
0831     }
0832 
0833     /*
0834      * only check card status if the map isn't allocated, ie no card seen yet
0835      * or if it's been over half a second since we last accessed it
0836      */
0837     if (info->lba_to_pba == NULL || time_after(jiffies, info->last_access + HZ/2)) {
0838 
0839         /* check to see if a card is fitted */
0840         result = sddr55_status (us);
0841         if (result) {
0842             result = sddr55_status (us);
0843             if (!result) {
0844             set_sense_info (6, 0x28, 0);    /* new media, set unit attention, not ready to ready */
0845             }
0846             return USB_STOR_TRANSPORT_FAILED;
0847         }
0848     }
0849 
0850     /*
0851      * if we detected a problem with the map when writing,
0852      * don't allow any more access
0853      */
0854     if (info->fatal_error) {
0855 
0856         set_sense_info (3, 0x31, 0);
0857         return USB_STOR_TRANSPORT_FAILED;
0858     }
0859 
0860     if (srb->cmnd[0] == READ_CAPACITY) {
0861 
0862         capacity = sddr55_get_capacity(us);
0863 
0864         if (!capacity) {
0865             set_sense_info (3, 0x30, 0); /* incompatible medium */
0866             return USB_STOR_TRANSPORT_FAILED;
0867         }
0868 
0869         info->capacity = capacity;
0870 
0871         /*
0872          * figure out the maximum logical block number, allowing for
0873          * the fact that only 250 out of every 256 are used
0874          */
0875         info->max_log_blks = ((info->capacity >> (info->pageshift + info->blockshift)) / 256) * 250;
0876 
0877         /*
0878          * Last page in the card, adjust as we only use 250 out of
0879          * every 256 pages
0880          */
0881         capacity = (capacity / 256) * 250;
0882 
0883         capacity /= PAGESIZE;
0884         capacity--;
0885 
0886         ((__be32 *) ptr)[0] = cpu_to_be32(capacity);
0887         ((__be32 *) ptr)[1] = cpu_to_be32(PAGESIZE);
0888         usb_stor_set_xfer_buf(ptr, 8, srb);
0889 
0890         sddr55_read_map(us);
0891 
0892         return USB_STOR_TRANSPORT_GOOD;
0893     }
0894 
0895     if (srb->cmnd[0] == MODE_SENSE_10) {
0896 
0897         memcpy(ptr, mode_page_01, sizeof mode_page_01);
0898         ptr[3] = (info->read_only || info->force_read_only) ? 0x80 : 0;
0899         usb_stor_set_xfer_buf(ptr, sizeof(mode_page_01), srb);
0900 
0901         if ( (srb->cmnd[2] & 0x3F) == 0x01 ) {
0902             usb_stor_dbg(us, "Dummy up request for mode page 1\n");
0903             return USB_STOR_TRANSPORT_GOOD;
0904 
0905         } else if ( (srb->cmnd[2] & 0x3F) == 0x3F ) {
0906             usb_stor_dbg(us, "Dummy up request for all mode pages\n");
0907             return USB_STOR_TRANSPORT_GOOD;
0908         }
0909 
0910         set_sense_info (5, 0x24, 0);    /* invalid field in command */
0911         return USB_STOR_TRANSPORT_FAILED;
0912     }
0913 
0914     if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
0915 
0916         usb_stor_dbg(us, "%s medium removal. Not that I can do anything about it...\n",
0917                  (srb->cmnd[4]&0x03) ? "Prevent" : "Allow");
0918 
0919         return USB_STOR_TRANSPORT_GOOD;
0920 
0921     }
0922 
0923     if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10) {
0924 
0925         page = short_pack(srb->cmnd[3], srb->cmnd[2]);
0926         page <<= 16;
0927         page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
0928         pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
0929 
0930         page <<= info->smallpageshift;
0931 
0932         // convert page to block and page-within-block
0933 
0934         lba = page >> info->blockshift;
0935         page = page & info->blockmask;
0936 
0937         // locate physical block corresponding to logical block
0938 
0939         if (lba >= info->max_log_blks) {
0940 
0941             usb_stor_dbg(us, "Error: Requested LBA %04X exceeds maximum block %04X\n",
0942                      lba, info->max_log_blks - 1);
0943 
0944             set_sense_info (5, 0x24, 0);    /* invalid field in command */
0945 
0946             return USB_STOR_TRANSPORT_FAILED;
0947         }
0948 
0949         pba = info->lba_to_pba[lba];
0950 
0951         if (srb->cmnd[0] == WRITE_10) {
0952             usb_stor_dbg(us, "WRITE_10: write block %04X (LBA %04X) page %01X pages %d\n",
0953                      pba, lba, page, pages);
0954 
0955             return sddr55_write_data(us, lba, page, pages);
0956         } else {
0957             usb_stor_dbg(us, "READ_10: read block %04X (LBA %04X) page %01X pages %d\n",
0958                      pba, lba, page, pages);
0959 
0960             return sddr55_read_data(us, lba, page, pages);
0961         }
0962     }
0963 
0964 
0965     if (srb->cmnd[0] == TEST_UNIT_READY) {
0966         return USB_STOR_TRANSPORT_GOOD;
0967     }
0968 
0969     if (srb->cmnd[0] == START_STOP) {
0970         return USB_STOR_TRANSPORT_GOOD;
0971     }
0972 
0973     set_sense_info (5, 0x20, 0);    /* illegal command */
0974 
0975     return USB_STOR_TRANSPORT_FAILED; // FIXME: sense buffer?
0976 }
0977 
0978 static struct scsi_host_template sddr55_host_template;
0979 
0980 static int sddr55_probe(struct usb_interface *intf,
0981              const struct usb_device_id *id)
0982 {
0983     struct us_data *us;
0984     int result;
0985 
0986     result = usb_stor_probe1(&us, intf, id,
0987             (id - sddr55_usb_ids) + sddr55_unusual_dev_list,
0988             &sddr55_host_template);
0989     if (result)
0990         return result;
0991 
0992     us->transport_name = "SDDR55";
0993     us->transport = sddr55_transport;
0994     us->transport_reset = sddr55_reset;
0995     us->max_lun = 0;
0996 
0997     result = usb_stor_probe2(us);
0998     return result;
0999 }
1000 
1001 static struct usb_driver sddr55_driver = {
1002     .name =     DRV_NAME,
1003     .probe =    sddr55_probe,
1004     .disconnect =   usb_stor_disconnect,
1005     .suspend =  usb_stor_suspend,
1006     .resume =   usb_stor_resume,
1007     .reset_resume = usb_stor_reset_resume,
1008     .pre_reset =    usb_stor_pre_reset,
1009     .post_reset =   usb_stor_post_reset,
1010     .id_table = sddr55_usb_ids,
1011     .soft_unbind =  1,
1012     .no_dynamic_id = 1,
1013 };
1014 
1015 module_usb_stor_driver(sddr55_driver, sddr55_host_template, DRV_NAME);