0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
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
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 { }
0046 };
0047 MODULE_DEVICE_TABLE(usb, sddr55_usb_ids);
0048
0049 #undef UNUSUAL_DEV
0050
0051
0052
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 { }
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;
0088 int max_log_blks;
0089 int pageshift;
0090 int smallpageshift;
0091 int blocksize;
0092 int blockshift;
0093 int blockmask;
0094 int read_only;
0095 int force_read_only;
0096 int *lba_to_pba;
0097 int *pba_to_lba;
0098 int fatal_error;
0099 unsigned long last_access;
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
0124
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
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);
0145 return USB_STOR_TRANSPORT_ERROR;
0146 }
0147
0148 result = sddr55_bulk_transport(us,
0149 DMA_FROM_DEVICE, status, 4);
0150
0151
0152 if (result == USB_STOR_XFER_SHORT || result == USB_STOR_XFER_STALLED) {
0153
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);
0163 return USB_STOR_TRANSPORT_FAILED;
0164 }
0165
0166 if (result != USB_STOR_XFER_GOOD) {
0167 set_sense_info (4, 0, 0);
0168 return USB_STOR_TRANSPORT_FAILED;
0169 }
0170
0171
0172 info->read_only = (status[0] & 0x20);
0173
0174
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);
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
0206
0207
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;
0214 offset = 0;
0215 sg = NULL;
0216
0217 while (sectors>0) {
0218
0219
0220 if (lba >= info->max_log_blks)
0221 break;
0222
0223 pba = info->lba_to_pba[lba];
0224
0225
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
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
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
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
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
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
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
0327 if (info->read_only || info->force_read_only) {
0328 set_sense_info (7, 0x27, 0);
0329 return USB_STOR_TRANSPORT_FAILED;
0330 }
0331
0332
0333
0334
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
0347 if (lba >= info->max_log_blks)
0348 break;
0349
0350 pba = info->lba_to_pba[lba];
0351
0352
0353
0354 pages = min((unsigned int) sectors << info->smallpageshift,
0355 info->blocksize - page);
0356 len = pages << info->pageshift;
0357
0358
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
0369
0370 int max_pba = (info->max_log_blks / 250 ) * 256;
0371 int found_count = 0;
0372 int found_pba = -1;
0373
0374
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
0384
0385
0386
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
0400 usb_stor_dbg(us, "Couldn't find unallocated block\n");
0401
0402 set_sense_info (3, 0x31, 0);
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
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
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
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
0437 set_sense_info (3, 0x3, 0);
0438 result = USB_STOR_TRANSPORT_FAILED;
0439 goto leave;
0440 }
0441
0442
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
0451 set_sense_info (3, 0x3, 0);
0452 result = USB_STOR_TRANSPORT_FAILED;
0453 goto leave;
0454 }
0455
0456
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
0464 set_sense_info (3, 0x3, 0);
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
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
0485 info->lba_to_pba[lba] = new_pba;
0486 info->pba_to_lba[pba] = UNUSED_BLOCK;
0487
0488
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
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:
0585 case 0xe8:
0586 case 0xec:
0587 info->pageshift = 8;
0588 info->smallpageshift = 1;
0589 return 0x00100000;
0590
0591 case 0xea:
0592 case 0x64:
0593 info->pageshift = 8;
0594 info->smallpageshift = 1;
0595 fallthrough;
0596 case 0x5d:
0597 return 0x00200000;
0598
0599 case 0xe3:
0600 case 0xe5:
0601 case 0x6b:
0602 case 0xd5:
0603 return 0x00400000;
0604
0605 case 0xe6:
0606 case 0xd6:
0607 return 0x00800000;
0608
0609 case 0x73:
0610 info->blocksize = 32;
0611 info->blockshift = 5;
0612 info->blockmask = 31;
0613 return 0x01000000;
0614
0615 case 0x75:
0616 info->blocksize = 32;
0617 info->blockshift = 5;
0618 info->blockmask = 31;
0619 return 0x02000000;
0620
0621 case 0x76:
0622 info->blocksize = 32;
0623 info->blockshift = 5;
0624 info->blockmask = 31;
0625 return 0x04000000;
0626
0627 case 0x79:
0628 info->blocksize = 32;
0629 info->blockshift = 5;
0630 info->blockmask = 31;
0631 return 0x08000000;
0632
0633 default:
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
0704 max_lba = info->max_log_blks;
0705 if (max_lba > 1000)
0706 max_lba = 1000;
0707
0708
0709
0710
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
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731
0732
0733
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
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
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
0824
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
0835
0836
0837 if (info->lba_to_pba == NULL || time_after(jiffies, info->last_access + HZ/2)) {
0838
0839
0840 result = sddr55_status (us);
0841 if (result) {
0842 result = sddr55_status (us);
0843 if (!result) {
0844 set_sense_info (6, 0x28, 0);
0845 }
0846 return USB_STOR_TRANSPORT_FAILED;
0847 }
0848 }
0849
0850
0851
0852
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);
0866 return USB_STOR_TRANSPORT_FAILED;
0867 }
0868
0869 info->capacity = capacity;
0870
0871
0872
0873
0874
0875 info->max_log_blks = ((info->capacity >> (info->pageshift + info->blockshift)) / 256) * 250;
0876
0877
0878
0879
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);
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
0933
0934 lba = page >> info->blockshift;
0935 page = page & info->blockmask;
0936
0937
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);
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);
0974
0975 return USB_STOR_TRANSPORT_FAILED;
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);