1 /* Driver for Lexar "Jumpshot" Compact Flash reader
3 * jumpshot driver v0.1:
7 * Current development and maintenance by:
8 * (c) 2000 Jimmie Mayfield (mayfield+usb@sackheads.org)
10 * Many thanks to Robert Baruch for the SanDisk SmartMedia reader driver
11 * which I used as a template for this driver.
13 * Some bugfixes and scatter-gather code by Gregory P. Smith
14 * (greg-usb@electricrain.com)
16 * Fix for media change by Joerg Schneider (js@joergschneider.com)
18 * Developed with the assistance of:
20 * (C) 2002 Alan Stern <stern@rowland.org>
22 * This program is free software; you can redistribute it and/or modify it
23 * under the terms of the GNU General Public License as published by the
24 * Free Software Foundation; either version 2, or (at your option) any
27 * This program is distributed in the hope that it will be useful, but
28 * WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 * General Public License for more details.
32 * You should have received a copy of the GNU General Public License along
33 * with this program; if not, write to the Free Software Foundation, Inc.,
34 * 675 Mass Ave, Cambridge, MA 02139, USA.
38 * This driver attempts to support the Lexar Jumpshot USB CompactFlash
39 * reader. Like many other USB CompactFlash readers, the Jumpshot contains
42 * This driver supports reading and writing. If you're truly paranoid,
43 * however, you can force the driver into a write-protected state by setting
44 * the WP enable bits in jumpshot_handle_mode_sense. See the comments
48 #include <linux/errno.h>
49 #include <linux/module.h>
50 #include <linux/slab.h>
52 #include <scsi/scsi.h>
53 #include <scsi/scsi_cmnd.h>
56 #include "transport.h"
62 * The table of devices
64 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
65 vendorName, productName, useProtocol, useTransport, \
66 initFunction, flags) \
67 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
68 .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
70 struct usb_device_id jumpshot_usb_ids[] = {
71 # include "unusual_jumpshot.h"
72 { } /* Terminating entry */
74 MODULE_DEVICE_TABLE(usb, jumpshot_usb_ids);
81 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
82 vendor_name, product_name, use_protocol, use_transport, \
83 init_function, Flags) \
85 .vendorName = vendor_name, \
86 .productName = product_name, \
87 .useProtocol = use_protocol, \
88 .useTransport = use_transport, \
89 .initFunction = init_function, \
92 static struct us_unusual_dev jumpshot_unusual_dev_list[] = {
93 # include "unusual_jumpshot.h"
94 { } /* Terminating entry */
100 struct jumpshot_info {
101 unsigned long sectors; /* total sector count */
102 unsigned long ssize; /* sector size in bytes */
104 /* the following aren't used yet */
105 unsigned char sense_key;
106 unsigned long sense_asc; /* additional sense code */
107 unsigned long sense_ascq; /* additional sense code qualifier */
110 static inline int jumpshot_bulk_read(struct us_data *us,
115 return USB_STOR_XFER_GOOD;
117 US_DEBUGP("jumpshot_bulk_read: len = %d\n", len);
118 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
123 static inline int jumpshot_bulk_write(struct us_data *us,
128 return USB_STOR_XFER_GOOD;
130 US_DEBUGP("jumpshot_bulk_write: len = %d\n", len);
131 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
136 static int jumpshot_get_status(struct us_data *us)
141 return USB_STOR_TRANSPORT_ERROR;
144 rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
145 0, 0xA0, 0, 7, us->iobuf, 1);
147 if (rc != USB_STOR_XFER_GOOD)
148 return USB_STOR_TRANSPORT_ERROR;
150 if (us->iobuf[0] != 0x50) {
151 US_DEBUGP("jumpshot_get_status: 0x%2x\n",
153 return USB_STOR_TRANSPORT_ERROR;
156 return USB_STOR_TRANSPORT_GOOD;
159 static int jumpshot_read_data(struct us_data *us,
160 struct jumpshot_info *info,
164 unsigned char *command = us->iobuf;
165 unsigned char *buffer;
166 unsigned char thistime;
167 unsigned int totallen, alloclen;
169 unsigned int sg_offset = 0;
170 struct scatterlist *sg = NULL;
172 // we're working in LBA mode. according to the ATA spec,
173 // we can support up to 28-bit addressing. I don't know if Jumpshot
174 // supports beyond 24-bit addressing. It's kind of hard to test
175 // since it requires > 8GB CF card.
177 if (sector > 0x0FFFFFFF)
178 return USB_STOR_TRANSPORT_ERROR;
180 totallen = sectors * info->ssize;
182 // Since we don't read more than 64 KB at a time, we have to create
183 // a bounce buffer and move the data a piece at a time between the
184 // bounce buffer and the actual transfer buffer.
186 alloclen = min(totallen, 65536u);
187 buffer = kmalloc(alloclen, GFP_NOIO);
189 return USB_STOR_TRANSPORT_ERROR;
192 // loop, never allocate or transfer more than 64k at once
193 // (min(128k, 255*info->ssize) is the real limit)
194 len = min(totallen, alloclen);
195 thistime = (len / info->ssize) & 0xff;
198 command[1] = thistime;
199 command[2] = sector & 0xFF;
200 command[3] = (sector >> 8) & 0xFF;
201 command[4] = (sector >> 16) & 0xFF;
203 command[5] = 0xE0 | ((sector >> 24) & 0x0F);
206 // send the setup + command
207 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
208 0, 0x20, 0, 1, command, 7);
209 if (result != USB_STOR_XFER_GOOD)
213 result = jumpshot_bulk_read(us, buffer, len);
214 if (result != USB_STOR_XFER_GOOD)
217 US_DEBUGP("jumpshot_read_data: %d bytes\n", len);
219 // Store the data in the transfer buffer
220 usb_stor_access_xfer_buf(buffer, len, us->srb,
221 &sg, &sg_offset, TO_XFER_BUF);
225 } while (totallen > 0);
228 return USB_STOR_TRANSPORT_GOOD;
232 return USB_STOR_TRANSPORT_ERROR;
236 static int jumpshot_write_data(struct us_data *us,
237 struct jumpshot_info *info,
241 unsigned char *command = us->iobuf;
242 unsigned char *buffer;
243 unsigned char thistime;
244 unsigned int totallen, alloclen;
245 int len, result, waitcount;
246 unsigned int sg_offset = 0;
247 struct scatterlist *sg = NULL;
249 // we're working in LBA mode. according to the ATA spec,
250 // we can support up to 28-bit addressing. I don't know if Jumpshot
251 // supports beyond 24-bit addressing. It's kind of hard to test
252 // since it requires > 8GB CF card.
254 if (sector > 0x0FFFFFFF)
255 return USB_STOR_TRANSPORT_ERROR;
257 totallen = sectors * info->ssize;
259 // Since we don't write more than 64 KB at a time, we have to create
260 // a bounce buffer and move the data a piece at a time between the
261 // bounce buffer and the actual transfer buffer.
263 alloclen = min(totallen, 65536u);
264 buffer = kmalloc(alloclen, GFP_NOIO);
266 return USB_STOR_TRANSPORT_ERROR;
269 // loop, never allocate or transfer more than 64k at once
270 // (min(128k, 255*info->ssize) is the real limit)
272 len = min(totallen, alloclen);
273 thistime = (len / info->ssize) & 0xff;
275 // Get the data from the transfer buffer
276 usb_stor_access_xfer_buf(buffer, len, us->srb,
277 &sg, &sg_offset, FROM_XFER_BUF);
280 command[1] = thistime;
281 command[2] = sector & 0xFF;
282 command[3] = (sector >> 8) & 0xFF;
283 command[4] = (sector >> 16) & 0xFF;
285 command[5] = 0xE0 | ((sector >> 24) & 0x0F);
288 // send the setup + command
289 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
290 0, 0x20, 0, 1, command, 7);
291 if (result != USB_STOR_XFER_GOOD)
295 result = jumpshot_bulk_write(us, buffer, len);
296 if (result != USB_STOR_XFER_GOOD)
299 // read the result. apparently the bulk write can complete
300 // before the jumpshot drive is finished writing. so we loop
301 // here until we get a good return code
304 result = jumpshot_get_status(us);
305 if (result != USB_STOR_TRANSPORT_GOOD) {
306 // I have not experimented to find the smallest value.
310 } while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));
312 if (result != USB_STOR_TRANSPORT_GOOD)
313 US_DEBUGP("jumpshot_write_data: Gah! Waitcount = 10. Bad write!?\n");
317 } while (totallen > 0);
324 return USB_STOR_TRANSPORT_ERROR;
327 static int jumpshot_id_device(struct us_data *us,
328 struct jumpshot_info *info)
330 unsigned char *command = us->iobuf;
331 unsigned char *reply;
335 return USB_STOR_TRANSPORT_ERROR;
339 reply = kmalloc(512, GFP_NOIO);
341 return USB_STOR_TRANSPORT_ERROR;
344 rc = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
345 0, 0x20, 0, 6, command, 2);
347 if (rc != USB_STOR_XFER_GOOD) {
348 US_DEBUGP("jumpshot_id_device: Gah! "
349 "send_control for read_capacity failed\n");
350 rc = USB_STOR_TRANSPORT_ERROR;
355 rc = jumpshot_bulk_read(us, reply, 512);
356 if (rc != USB_STOR_XFER_GOOD) {
357 rc = USB_STOR_TRANSPORT_ERROR;
361 info->sectors = ((u32)(reply[117]) << 24) |
362 ((u32)(reply[116]) << 16) |
363 ((u32)(reply[115]) << 8) |
364 ((u32)(reply[114]) );
366 rc = USB_STOR_TRANSPORT_GOOD;
373 static int jumpshot_handle_mode_sense(struct us_data *us,
374 struct scsi_cmnd * srb,
377 static unsigned char rw_err_page[12] = {
378 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
380 static unsigned char cache_page[12] = {
381 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
383 static unsigned char rbac_page[12] = {
384 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
386 static unsigned char timer_page[8] = {
387 0x1C, 0x6, 0, 0, 0, 0
389 unsigned char pc, page_code;
391 struct jumpshot_info *info = (struct jumpshot_info *) (us->extra);
392 unsigned char *ptr = us->iobuf;
394 pc = srb->cmnd[2] >> 6;
395 page_code = srb->cmnd[2] & 0x3F;
399 US_DEBUGP("jumpshot_handle_mode_sense: Current values\n");
402 US_DEBUGP("jumpshot_handle_mode_sense: Changeable values\n");
405 US_DEBUGP("jumpshot_handle_mode_sense: Default values\n");
408 US_DEBUGP("jumpshot_handle_mode_sense: Saves values\n");
414 ptr[2] = 0x00; // WP enable: 0x80
417 ptr[3] = 0x00; // WP enable: 0x80
423 // vendor-specific mode
424 info->sense_key = 0x05;
425 info->sense_asc = 0x24;
426 info->sense_ascq = 0x00;
427 return USB_STOR_TRANSPORT_FAILED;
430 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
431 i += sizeof(rw_err_page);
435 memcpy(ptr + i, cache_page, sizeof(cache_page));
436 i += sizeof(cache_page);
440 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
441 i += sizeof(rbac_page);
445 memcpy(ptr + i, timer_page, sizeof(timer_page));
446 i += sizeof(timer_page);
450 memcpy(ptr + i, timer_page, sizeof(timer_page));
451 i += sizeof(timer_page);
452 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
453 i += sizeof(rbac_page);
454 memcpy(ptr + i, cache_page, sizeof(cache_page));
455 i += sizeof(cache_page);
456 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
457 i += sizeof(rw_err_page);
464 ((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
465 usb_stor_set_xfer_buf(ptr, i, srb);
467 return USB_STOR_TRANSPORT_GOOD;
471 static void jumpshot_info_destructor(void *extra)
473 // this routine is a placeholder...
474 // currently, we don't allocate any extra blocks so we're okay
479 // Transport for the Lexar 'Jumpshot'
481 static int jumpshot_transport(struct scsi_cmnd *srb, struct us_data *us)
483 struct jumpshot_info *info;
485 unsigned long block, blocks;
486 unsigned char *ptr = us->iobuf;
487 static unsigned char inquiry_response[8] = {
488 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
492 us->extra = kzalloc(sizeof(struct jumpshot_info), GFP_NOIO);
494 US_DEBUGP("jumpshot_transport: Gah! Can't allocate storage for jumpshot info struct!\n");
495 return USB_STOR_TRANSPORT_ERROR;
497 us->extra_destructor = jumpshot_info_destructor;
500 info = (struct jumpshot_info *) (us->extra);
502 if (srb->cmnd[0] == INQUIRY) {
503 US_DEBUGP("jumpshot_transport: INQUIRY. Returning bogus response.\n");
504 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
505 fill_inquiry_response(us, ptr, 36);
506 return USB_STOR_TRANSPORT_GOOD;
509 if (srb->cmnd[0] == READ_CAPACITY) {
510 info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
512 rc = jumpshot_get_status(us);
513 if (rc != USB_STOR_TRANSPORT_GOOD)
516 rc = jumpshot_id_device(us, info);
517 if (rc != USB_STOR_TRANSPORT_GOOD)
520 US_DEBUGP("jumpshot_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
521 info->sectors, info->ssize);
525 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
526 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
527 usb_stor_set_xfer_buf(ptr, 8, srb);
529 return USB_STOR_TRANSPORT_GOOD;
532 if (srb->cmnd[0] == MODE_SELECT_10) {
533 US_DEBUGP("jumpshot_transport: Gah! MODE_SELECT_10.\n");
534 return USB_STOR_TRANSPORT_ERROR;
537 if (srb->cmnd[0] == READ_10) {
538 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
539 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
541 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
543 US_DEBUGP("jumpshot_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
544 return jumpshot_read_data(us, info, block, blocks);
547 if (srb->cmnd[0] == READ_12) {
548 // I don't think we'll ever see a READ_12 but support it anyway...
550 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
551 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
553 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
554 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
556 US_DEBUGP("jumpshot_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
557 return jumpshot_read_data(us, info, block, blocks);
560 if (srb->cmnd[0] == WRITE_10) {
561 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
562 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
564 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
566 US_DEBUGP("jumpshot_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
567 return jumpshot_write_data(us, info, block, blocks);
570 if (srb->cmnd[0] == WRITE_12) {
571 // I don't think we'll ever see a WRITE_12 but support it anyway...
573 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
574 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
576 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
577 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
579 US_DEBUGP("jumpshot_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
580 return jumpshot_write_data(us, info, block, blocks);
584 if (srb->cmnd[0] == TEST_UNIT_READY) {
585 US_DEBUGP("jumpshot_transport: TEST_UNIT_READY.\n");
586 return jumpshot_get_status(us);
589 if (srb->cmnd[0] == REQUEST_SENSE) {
590 US_DEBUGP("jumpshot_transport: REQUEST_SENSE.\n");
594 ptr[2] = info->sense_key;
596 ptr[12] = info->sense_asc;
597 ptr[13] = info->sense_ascq;
598 usb_stor_set_xfer_buf(ptr, 18, srb);
600 return USB_STOR_TRANSPORT_GOOD;
603 if (srb->cmnd[0] == MODE_SENSE) {
604 US_DEBUGP("jumpshot_transport: MODE_SENSE_6 detected\n");
605 return jumpshot_handle_mode_sense(us, srb, 1);
608 if (srb->cmnd[0] == MODE_SENSE_10) {
609 US_DEBUGP("jumpshot_transport: MODE_SENSE_10 detected\n");
610 return jumpshot_handle_mode_sense(us, srb, 0);
613 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
614 // sure. whatever. not like we can stop the user from popping
615 // the media out of the device (no locking doors, etc)
617 return USB_STOR_TRANSPORT_GOOD;
620 if (srb->cmnd[0] == START_STOP) {
621 /* this is used by sd.c'check_scsidisk_media_change to detect
623 US_DEBUGP("jumpshot_transport: START_STOP.\n");
624 /* the first jumpshot_id_device after a media change returns
625 an error (determined experimentally) */
626 rc = jumpshot_id_device(us, info);
627 if (rc == USB_STOR_TRANSPORT_GOOD) {
628 info->sense_key = NO_SENSE;
629 srb->result = SUCCESS;
631 info->sense_key = UNIT_ATTENTION;
632 srb->result = SAM_STAT_CHECK_CONDITION;
637 US_DEBUGP("jumpshot_transport: Gah! Unknown command: %d (0x%x)\n",
638 srb->cmnd[0], srb->cmnd[0]);
639 info->sense_key = 0x05;
640 info->sense_asc = 0x20;
641 info->sense_ascq = 0x00;
642 return USB_STOR_TRANSPORT_FAILED;
645 static int jumpshot_probe(struct usb_interface *intf,
646 const struct usb_device_id *id)
651 result = usb_stor_probe1(&us, intf, id,
652 (id - jumpshot_usb_ids) + jumpshot_unusual_dev_list);
656 us->transport_name = "Lexar Jumpshot Control/Bulk";
657 us->transport = jumpshot_transport;
658 us->transport_reset = usb_stor_Bulk_reset;
661 result = usb_stor_probe2(us);
665 static struct usb_driver jumpshot_driver = {
666 .name = "ums-jumpshot",
667 .probe = jumpshot_probe,
668 .disconnect = usb_stor_disconnect,
669 .suspend = usb_stor_suspend,
670 .resume = usb_stor_resume,
671 .reset_resume = usb_stor_reset_resume,
672 .pre_reset = usb_stor_pre_reset,
673 .post_reset = usb_stor_post_reset,
674 .id_table = jumpshot_usb_ids,
678 static int __init jumpshot_init(void)
680 return usb_register(&jumpshot_driver);
683 static void __exit jumpshot_exit(void)
685 usb_deregister(&jumpshot_driver);
688 module_init(jumpshot_init);
689 module_exit(jumpshot_exit);