0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 #include <linux/errno.h>
0037 #include <linux/module.h>
0038 #include <linux/slab.h>
0039
0040 #include <scsi/scsi.h>
0041 #include <scsi/scsi_cmnd.h>
0042
0043 #include "usb.h"
0044 #include "transport.h"
0045 #include "protocol.h"
0046 #include "debug.h"
0047 #include "scsiglue.h"
0048
0049 #define DRV_NAME "ums-jumpshot"
0050
0051 MODULE_DESCRIPTION("Driver for Lexar \"Jumpshot\" Compact Flash reader");
0052 MODULE_AUTHOR("Jimmie Mayfield <mayfield+usb@sackheads.org>");
0053 MODULE_LICENSE("GPL");
0054 MODULE_IMPORT_NS(USB_STORAGE);
0055
0056
0057
0058
0059 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
0060 vendorName, productName, useProtocol, useTransport, \
0061 initFunction, flags) \
0062 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
0063 .driver_info = (flags) }
0064
0065 static struct usb_device_id jumpshot_usb_ids[] = {
0066 # include "unusual_jumpshot.h"
0067 { }
0068 };
0069 MODULE_DEVICE_TABLE(usb, jumpshot_usb_ids);
0070
0071 #undef UNUSUAL_DEV
0072
0073
0074
0075
0076 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
0077 vendor_name, product_name, use_protocol, use_transport, \
0078 init_function, Flags) \
0079 { \
0080 .vendorName = vendor_name, \
0081 .productName = product_name, \
0082 .useProtocol = use_protocol, \
0083 .useTransport = use_transport, \
0084 .initFunction = init_function, \
0085 }
0086
0087 static struct us_unusual_dev jumpshot_unusual_dev_list[] = {
0088 # include "unusual_jumpshot.h"
0089 { }
0090 };
0091
0092 #undef UNUSUAL_DEV
0093
0094
0095 struct jumpshot_info {
0096 unsigned long sectors;
0097 unsigned long ssize;
0098
0099
0100 unsigned char sense_key;
0101 unsigned long sense_asc;
0102 unsigned long sense_ascq;
0103 };
0104
0105 static inline int jumpshot_bulk_read(struct us_data *us,
0106 unsigned char *data,
0107 unsigned int len)
0108 {
0109 if (len == 0)
0110 return USB_STOR_XFER_GOOD;
0111
0112 usb_stor_dbg(us, "len = %d\n", len);
0113 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
0114 data, len, NULL);
0115 }
0116
0117
0118 static inline int jumpshot_bulk_write(struct us_data *us,
0119 unsigned char *data,
0120 unsigned int len)
0121 {
0122 if (len == 0)
0123 return USB_STOR_XFER_GOOD;
0124
0125 usb_stor_dbg(us, "len = %d\n", len);
0126 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
0127 data, len, NULL);
0128 }
0129
0130
0131 static int jumpshot_get_status(struct us_data *us)
0132 {
0133 int rc;
0134
0135 if (!us)
0136 return USB_STOR_TRANSPORT_ERROR;
0137
0138
0139 rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
0140 0, 0xA0, 0, 7, us->iobuf, 1);
0141
0142 if (rc != USB_STOR_XFER_GOOD)
0143 return USB_STOR_TRANSPORT_ERROR;
0144
0145 if (us->iobuf[0] != 0x50) {
0146 usb_stor_dbg(us, "0x%2x\n", us->iobuf[0]);
0147 return USB_STOR_TRANSPORT_ERROR;
0148 }
0149
0150 return USB_STOR_TRANSPORT_GOOD;
0151 }
0152
0153 static int jumpshot_read_data(struct us_data *us,
0154 struct jumpshot_info *info,
0155 u32 sector,
0156 u32 sectors)
0157 {
0158 unsigned char *command = us->iobuf;
0159 unsigned char *buffer;
0160 unsigned char thistime;
0161 unsigned int totallen, alloclen;
0162 int len, result;
0163 unsigned int sg_offset = 0;
0164 struct scatterlist *sg = NULL;
0165
0166
0167
0168
0169
0170
0171 if (sector > 0x0FFFFFFF)
0172 return USB_STOR_TRANSPORT_ERROR;
0173
0174 totallen = sectors * info->ssize;
0175
0176
0177
0178
0179
0180 alloclen = min(totallen, 65536u);
0181 buffer = kmalloc(alloclen, GFP_NOIO);
0182 if (buffer == NULL)
0183 return USB_STOR_TRANSPORT_ERROR;
0184
0185 do {
0186
0187
0188 len = min(totallen, alloclen);
0189 thistime = (len / info->ssize) & 0xff;
0190
0191 command[0] = 0;
0192 command[1] = thistime;
0193 command[2] = sector & 0xFF;
0194 command[3] = (sector >> 8) & 0xFF;
0195 command[4] = (sector >> 16) & 0xFF;
0196
0197 command[5] = 0xE0 | ((sector >> 24) & 0x0F);
0198 command[6] = 0x20;
0199
0200
0201 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
0202 0, 0x20, 0, 1, command, 7);
0203 if (result != USB_STOR_XFER_GOOD)
0204 goto leave;
0205
0206
0207 result = jumpshot_bulk_read(us, buffer, len);
0208 if (result != USB_STOR_XFER_GOOD)
0209 goto leave;
0210
0211 usb_stor_dbg(us, "%d bytes\n", len);
0212
0213
0214 usb_stor_access_xfer_buf(buffer, len, us->srb,
0215 &sg, &sg_offset, TO_XFER_BUF);
0216
0217 sector += thistime;
0218 totallen -= len;
0219 } while (totallen > 0);
0220
0221 kfree(buffer);
0222 return USB_STOR_TRANSPORT_GOOD;
0223
0224 leave:
0225 kfree(buffer);
0226 return USB_STOR_TRANSPORT_ERROR;
0227 }
0228
0229
0230 static int jumpshot_write_data(struct us_data *us,
0231 struct jumpshot_info *info,
0232 u32 sector,
0233 u32 sectors)
0234 {
0235 unsigned char *command = us->iobuf;
0236 unsigned char *buffer;
0237 unsigned char thistime;
0238 unsigned int totallen, alloclen;
0239 int len, result, waitcount;
0240 unsigned int sg_offset = 0;
0241 struct scatterlist *sg = NULL;
0242
0243
0244
0245
0246
0247
0248 if (sector > 0x0FFFFFFF)
0249 return USB_STOR_TRANSPORT_ERROR;
0250
0251 totallen = sectors * info->ssize;
0252
0253
0254
0255
0256
0257 alloclen = min(totallen, 65536u);
0258 buffer = kmalloc(alloclen, GFP_NOIO);
0259 if (buffer == NULL)
0260 return USB_STOR_TRANSPORT_ERROR;
0261
0262 do {
0263
0264
0265
0266 len = min(totallen, alloclen);
0267 thistime = (len / info->ssize) & 0xff;
0268
0269
0270 usb_stor_access_xfer_buf(buffer, len, us->srb,
0271 &sg, &sg_offset, FROM_XFER_BUF);
0272
0273 command[0] = 0;
0274 command[1] = thistime;
0275 command[2] = sector & 0xFF;
0276 command[3] = (sector >> 8) & 0xFF;
0277 command[4] = (sector >> 16) & 0xFF;
0278
0279 command[5] = 0xE0 | ((sector >> 24) & 0x0F);
0280 command[6] = 0x30;
0281
0282
0283 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
0284 0, 0x20, 0, 1, command, 7);
0285 if (result != USB_STOR_XFER_GOOD)
0286 goto leave;
0287
0288
0289 result = jumpshot_bulk_write(us, buffer, len);
0290 if (result != USB_STOR_XFER_GOOD)
0291 goto leave;
0292
0293
0294
0295
0296 waitcount = 0;
0297 do {
0298 result = jumpshot_get_status(us);
0299 if (result != USB_STOR_TRANSPORT_GOOD) {
0300
0301
0302 msleep(50);
0303 }
0304 } while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));
0305
0306 if (result != USB_STOR_TRANSPORT_GOOD)
0307 usb_stor_dbg(us, "Gah! Waitcount = 10. Bad write!?\n");
0308
0309 sector += thistime;
0310 totallen -= len;
0311 } while (totallen > 0);
0312
0313 kfree(buffer);
0314 return result;
0315
0316 leave:
0317 kfree(buffer);
0318 return USB_STOR_TRANSPORT_ERROR;
0319 }
0320
0321 static int jumpshot_id_device(struct us_data *us,
0322 struct jumpshot_info *info)
0323 {
0324 unsigned char *command = us->iobuf;
0325 unsigned char *reply;
0326 int rc;
0327
0328 if (!info)
0329 return USB_STOR_TRANSPORT_ERROR;
0330
0331 command[0] = 0xE0;
0332 command[1] = 0xEC;
0333 reply = kmalloc(512, GFP_NOIO);
0334 if (!reply)
0335 return USB_STOR_TRANSPORT_ERROR;
0336
0337
0338 rc = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
0339 0, 0x20, 0, 6, command, 2);
0340
0341 if (rc != USB_STOR_XFER_GOOD) {
0342 usb_stor_dbg(us, "Gah! send_control for read_capacity failed\n");
0343 rc = USB_STOR_TRANSPORT_ERROR;
0344 goto leave;
0345 }
0346
0347
0348 rc = jumpshot_bulk_read(us, reply, 512);
0349 if (rc != USB_STOR_XFER_GOOD) {
0350 rc = USB_STOR_TRANSPORT_ERROR;
0351 goto leave;
0352 }
0353
0354 info->sectors = ((u32)(reply[117]) << 24) |
0355 ((u32)(reply[116]) << 16) |
0356 ((u32)(reply[115]) << 8) |
0357 ((u32)(reply[114]) );
0358
0359 rc = USB_STOR_TRANSPORT_GOOD;
0360
0361 leave:
0362 kfree(reply);
0363 return rc;
0364 }
0365
0366 static int jumpshot_handle_mode_sense(struct us_data *us,
0367 struct scsi_cmnd * srb,
0368 int sense_6)
0369 {
0370 static unsigned char rw_err_page[12] = {
0371 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
0372 };
0373 static unsigned char cache_page[12] = {
0374 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
0375 };
0376 static unsigned char rbac_page[12] = {
0377 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
0378 };
0379 static unsigned char timer_page[8] = {
0380 0x1C, 0x6, 0, 0, 0, 0
0381 };
0382 unsigned char pc, page_code;
0383 unsigned int i = 0;
0384 struct jumpshot_info *info = (struct jumpshot_info *) (us->extra);
0385 unsigned char *ptr = us->iobuf;
0386
0387 pc = srb->cmnd[2] >> 6;
0388 page_code = srb->cmnd[2] & 0x3F;
0389
0390 switch (pc) {
0391 case 0x0:
0392 usb_stor_dbg(us, "Current values\n");
0393 break;
0394 case 0x1:
0395 usb_stor_dbg(us, "Changeable values\n");
0396 break;
0397 case 0x2:
0398 usb_stor_dbg(us, "Default values\n");
0399 break;
0400 case 0x3:
0401 usb_stor_dbg(us, "Saves values\n");
0402 break;
0403 }
0404
0405 memset(ptr, 0, 8);
0406 if (sense_6) {
0407 ptr[2] = 0x00;
0408 i = 4;
0409 } else {
0410 ptr[3] = 0x00;
0411 i = 8;
0412 }
0413
0414 switch (page_code) {
0415 case 0x0:
0416
0417 info->sense_key = 0x05;
0418 info->sense_asc = 0x24;
0419 info->sense_ascq = 0x00;
0420 return USB_STOR_TRANSPORT_FAILED;
0421
0422 case 0x1:
0423 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
0424 i += sizeof(rw_err_page);
0425 break;
0426
0427 case 0x8:
0428 memcpy(ptr + i, cache_page, sizeof(cache_page));
0429 i += sizeof(cache_page);
0430 break;
0431
0432 case 0x1B:
0433 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
0434 i += sizeof(rbac_page);
0435 break;
0436
0437 case 0x1C:
0438 memcpy(ptr + i, timer_page, sizeof(timer_page));
0439 i += sizeof(timer_page);
0440 break;
0441
0442 case 0x3F:
0443 memcpy(ptr + i, timer_page, sizeof(timer_page));
0444 i += sizeof(timer_page);
0445 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
0446 i += sizeof(rbac_page);
0447 memcpy(ptr + i, cache_page, sizeof(cache_page));
0448 i += sizeof(cache_page);
0449 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
0450 i += sizeof(rw_err_page);
0451 break;
0452 }
0453
0454 if (sense_6)
0455 ptr[0] = i - 1;
0456 else
0457 ((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
0458 usb_stor_set_xfer_buf(ptr, i, srb);
0459
0460 return USB_STOR_TRANSPORT_GOOD;
0461 }
0462
0463
0464 static void jumpshot_info_destructor(void *extra)
0465 {
0466
0467
0468 }
0469
0470
0471
0472
0473
0474 static int jumpshot_transport(struct scsi_cmnd *srb, struct us_data *us)
0475 {
0476 struct jumpshot_info *info;
0477 int rc;
0478 unsigned long block, blocks;
0479 unsigned char *ptr = us->iobuf;
0480 static unsigned char inquiry_response[8] = {
0481 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
0482 };
0483
0484 if (!us->extra) {
0485 us->extra = kzalloc(sizeof(struct jumpshot_info), GFP_NOIO);
0486 if (!us->extra)
0487 return USB_STOR_TRANSPORT_ERROR;
0488
0489 us->extra_destructor = jumpshot_info_destructor;
0490 }
0491
0492 info = (struct jumpshot_info *) (us->extra);
0493
0494 if (srb->cmnd[0] == INQUIRY) {
0495 usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
0496 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
0497 fill_inquiry_response(us, ptr, 36);
0498 return USB_STOR_TRANSPORT_GOOD;
0499 }
0500
0501 if (srb->cmnd[0] == READ_CAPACITY) {
0502 info->ssize = 0x200;
0503
0504 rc = jumpshot_get_status(us);
0505 if (rc != USB_STOR_TRANSPORT_GOOD)
0506 return rc;
0507
0508 rc = jumpshot_id_device(us, info);
0509 if (rc != USB_STOR_TRANSPORT_GOOD)
0510 return rc;
0511
0512 usb_stor_dbg(us, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
0513 info->sectors, info->ssize);
0514
0515
0516
0517 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
0518 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
0519 usb_stor_set_xfer_buf(ptr, 8, srb);
0520
0521 return USB_STOR_TRANSPORT_GOOD;
0522 }
0523
0524 if (srb->cmnd[0] == MODE_SELECT_10) {
0525 usb_stor_dbg(us, "Gah! MODE_SELECT_10\n");
0526 return USB_STOR_TRANSPORT_ERROR;
0527 }
0528
0529 if (srb->cmnd[0] == READ_10) {
0530 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
0531 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
0532
0533 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
0534
0535 usb_stor_dbg(us, "READ_10: read block 0x%04lx count %ld\n",
0536 block, blocks);
0537 return jumpshot_read_data(us, info, block, blocks);
0538 }
0539
0540 if (srb->cmnd[0] == READ_12) {
0541
0542
0543 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
0544 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
0545
0546 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
0547 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
0548
0549 usb_stor_dbg(us, "READ_12: read block 0x%04lx count %ld\n",
0550 block, blocks);
0551 return jumpshot_read_data(us, info, block, blocks);
0552 }
0553
0554 if (srb->cmnd[0] == WRITE_10) {
0555 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
0556 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
0557
0558 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
0559
0560 usb_stor_dbg(us, "WRITE_10: write block 0x%04lx count %ld\n",
0561 block, blocks);
0562 return jumpshot_write_data(us, info, block, blocks);
0563 }
0564
0565 if (srb->cmnd[0] == WRITE_12) {
0566
0567
0568 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
0569 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
0570
0571 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
0572 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
0573
0574 usb_stor_dbg(us, "WRITE_12: write block 0x%04lx count %ld\n",
0575 block, blocks);
0576 return jumpshot_write_data(us, info, block, blocks);
0577 }
0578
0579
0580 if (srb->cmnd[0] == TEST_UNIT_READY) {
0581 usb_stor_dbg(us, "TEST_UNIT_READY\n");
0582 return jumpshot_get_status(us);
0583 }
0584
0585 if (srb->cmnd[0] == REQUEST_SENSE) {
0586 usb_stor_dbg(us, "REQUEST_SENSE\n");
0587
0588 memset(ptr, 0, 18);
0589 ptr[0] = 0xF0;
0590 ptr[2] = info->sense_key;
0591 ptr[7] = 11;
0592 ptr[12] = info->sense_asc;
0593 ptr[13] = info->sense_ascq;
0594 usb_stor_set_xfer_buf(ptr, 18, srb);
0595
0596 return USB_STOR_TRANSPORT_GOOD;
0597 }
0598
0599 if (srb->cmnd[0] == MODE_SENSE) {
0600 usb_stor_dbg(us, "MODE_SENSE_6 detected\n");
0601 return jumpshot_handle_mode_sense(us, srb, 1);
0602 }
0603
0604 if (srb->cmnd[0] == MODE_SENSE_10) {
0605 usb_stor_dbg(us, "MODE_SENSE_10 detected\n");
0606 return jumpshot_handle_mode_sense(us, srb, 0);
0607 }
0608
0609 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
0610
0611
0612
0613
0614 return USB_STOR_TRANSPORT_GOOD;
0615 }
0616
0617 if (srb->cmnd[0] == START_STOP) {
0618
0619
0620
0621
0622 usb_stor_dbg(us, "START_STOP\n");
0623
0624
0625
0626
0627 rc = jumpshot_id_device(us, info);
0628 if (rc == USB_STOR_TRANSPORT_GOOD) {
0629 info->sense_key = NO_SENSE;
0630 srb->result = SUCCESS;
0631 } else {
0632 info->sense_key = UNIT_ATTENTION;
0633 srb->result = SAM_STAT_CHECK_CONDITION;
0634 }
0635 return rc;
0636 }
0637
0638 usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
0639 srb->cmnd[0], srb->cmnd[0]);
0640 info->sense_key = 0x05;
0641 info->sense_asc = 0x20;
0642 info->sense_ascq = 0x00;
0643 return USB_STOR_TRANSPORT_FAILED;
0644 }
0645
0646 static struct scsi_host_template jumpshot_host_template;
0647
0648 static int jumpshot_probe(struct usb_interface *intf,
0649 const struct usb_device_id *id)
0650 {
0651 struct us_data *us;
0652 int result;
0653
0654 result = usb_stor_probe1(&us, intf, id,
0655 (id - jumpshot_usb_ids) + jumpshot_unusual_dev_list,
0656 &jumpshot_host_template);
0657 if (result)
0658 return result;
0659
0660 us->transport_name = "Lexar Jumpshot Control/Bulk";
0661 us->transport = jumpshot_transport;
0662 us->transport_reset = usb_stor_Bulk_reset;
0663 us->max_lun = 1;
0664
0665 result = usb_stor_probe2(us);
0666 return result;
0667 }
0668
0669 static struct usb_driver jumpshot_driver = {
0670 .name = DRV_NAME,
0671 .probe = jumpshot_probe,
0672 .disconnect = usb_stor_disconnect,
0673 .suspend = usb_stor_suspend,
0674 .resume = usb_stor_resume,
0675 .reset_resume = usb_stor_reset_resume,
0676 .pre_reset = usb_stor_pre_reset,
0677 .post_reset = usb_stor_post_reset,
0678 .id_table = jumpshot_usb_ids,
0679 .soft_unbind = 1,
0680 .no_dynamic_id = 1,
0681 };
0682
0683 module_usb_stor_driver(jumpshot_driver, jumpshot_host_template, DRV_NAME);