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"
61 MODULE_DESCRIPTION("Driver for Lexar \"Jumpshot\" Compact Flash reader");
62 MODULE_AUTHOR("Jimmie Mayfield <mayfield+usb@sackheads.org>");
63 MODULE_LICENSE("GPL");
66 * The table of devices
68 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
69 vendorName, productName, useProtocol, useTransport, \
70 initFunction, flags) \
71 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
72 .driver_info = (flags) }
74 static struct usb_device_id jumpshot_usb_ids
[] = {
75 # include "unusual_jumpshot.h"
76 { } /* Terminating entry */
78 MODULE_DEVICE_TABLE(usb
, jumpshot_usb_ids
);
85 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
86 vendor_name, product_name, use_protocol, use_transport, \
87 init_function, Flags) \
89 .vendorName = vendor_name, \
90 .productName = product_name, \
91 .useProtocol = use_protocol, \
92 .useTransport = use_transport, \
93 .initFunction = init_function, \
96 static struct us_unusual_dev jumpshot_unusual_dev_list
[] = {
97 # include "unusual_jumpshot.h"
98 { } /* Terminating entry */
104 struct jumpshot_info
{
105 unsigned long sectors
; /* total sector count */
106 unsigned long ssize
; /* sector size in bytes */
108 /* the following aren't used yet */
109 unsigned char sense_key
;
110 unsigned long sense_asc
; /* additional sense code */
111 unsigned long sense_ascq
; /* additional sense code qualifier */
114 static inline int jumpshot_bulk_read(struct us_data
*us
,
119 return USB_STOR_XFER_GOOD
;
121 usb_stor_dbg(us
, "len = %d\n", len
);
122 return usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
127 static inline int jumpshot_bulk_write(struct us_data
*us
,
132 return USB_STOR_XFER_GOOD
;
134 usb_stor_dbg(us
, "len = %d\n", len
);
135 return usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
140 static int jumpshot_get_status(struct us_data
*us
)
145 return USB_STOR_TRANSPORT_ERROR
;
148 rc
= usb_stor_ctrl_transfer(us
, us
->recv_ctrl_pipe
,
149 0, 0xA0, 0, 7, us
->iobuf
, 1);
151 if (rc
!= USB_STOR_XFER_GOOD
)
152 return USB_STOR_TRANSPORT_ERROR
;
154 if (us
->iobuf
[0] != 0x50) {
155 usb_stor_dbg(us
, "0x%2x\n", us
->iobuf
[0]);
156 return USB_STOR_TRANSPORT_ERROR
;
159 return USB_STOR_TRANSPORT_GOOD
;
162 static int jumpshot_read_data(struct us_data
*us
,
163 struct jumpshot_info
*info
,
167 unsigned char *command
= us
->iobuf
;
168 unsigned char *buffer
;
169 unsigned char thistime
;
170 unsigned int totallen
, alloclen
;
172 unsigned int sg_offset
= 0;
173 struct scatterlist
*sg
= NULL
;
175 // we're working in LBA mode. according to the ATA spec,
176 // we can support up to 28-bit addressing. I don't know if Jumpshot
177 // supports beyond 24-bit addressing. It's kind of hard to test
178 // since it requires > 8GB CF card.
180 if (sector
> 0x0FFFFFFF)
181 return USB_STOR_TRANSPORT_ERROR
;
183 totallen
= sectors
* info
->ssize
;
185 // Since we don't read more than 64 KB at a time, we have to create
186 // a bounce buffer and move the data a piece at a time between the
187 // bounce buffer and the actual transfer buffer.
189 alloclen
= min(totallen
, 65536u);
190 buffer
= kmalloc(alloclen
, GFP_NOIO
);
192 return USB_STOR_TRANSPORT_ERROR
;
195 // loop, never allocate or transfer more than 64k at once
196 // (min(128k, 255*info->ssize) is the real limit)
197 len
= min(totallen
, alloclen
);
198 thistime
= (len
/ info
->ssize
) & 0xff;
201 command
[1] = thistime
;
202 command
[2] = sector
& 0xFF;
203 command
[3] = (sector
>> 8) & 0xFF;
204 command
[4] = (sector
>> 16) & 0xFF;
206 command
[5] = 0xE0 | ((sector
>> 24) & 0x0F);
209 // send the setup + command
210 result
= usb_stor_ctrl_transfer(us
, us
->send_ctrl_pipe
,
211 0, 0x20, 0, 1, command
, 7);
212 if (result
!= USB_STOR_XFER_GOOD
)
216 result
= jumpshot_bulk_read(us
, buffer
, len
);
217 if (result
!= USB_STOR_XFER_GOOD
)
220 usb_stor_dbg(us
, "%d bytes\n", len
);
222 // Store the data in the transfer buffer
223 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
224 &sg
, &sg_offset
, TO_XFER_BUF
);
228 } while (totallen
> 0);
231 return USB_STOR_TRANSPORT_GOOD
;
235 return USB_STOR_TRANSPORT_ERROR
;
239 static int jumpshot_write_data(struct us_data
*us
,
240 struct jumpshot_info
*info
,
244 unsigned char *command
= us
->iobuf
;
245 unsigned char *buffer
;
246 unsigned char thistime
;
247 unsigned int totallen
, alloclen
;
248 int len
, result
, waitcount
;
249 unsigned int sg_offset
= 0;
250 struct scatterlist
*sg
= NULL
;
252 // we're working in LBA mode. according to the ATA spec,
253 // we can support up to 28-bit addressing. I don't know if Jumpshot
254 // supports beyond 24-bit addressing. It's kind of hard to test
255 // since it requires > 8GB CF card.
257 if (sector
> 0x0FFFFFFF)
258 return USB_STOR_TRANSPORT_ERROR
;
260 totallen
= sectors
* info
->ssize
;
262 // Since we don't write more than 64 KB at a time, we have to create
263 // a bounce buffer and move the data a piece at a time between the
264 // bounce buffer and the actual transfer buffer.
266 alloclen
= min(totallen
, 65536u);
267 buffer
= kmalloc(alloclen
, GFP_NOIO
);
269 return USB_STOR_TRANSPORT_ERROR
;
272 // loop, never allocate or transfer more than 64k at once
273 // (min(128k, 255*info->ssize) is the real limit)
275 len
= min(totallen
, alloclen
);
276 thistime
= (len
/ info
->ssize
) & 0xff;
278 // Get the data from the transfer buffer
279 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
280 &sg
, &sg_offset
, FROM_XFER_BUF
);
283 command
[1] = thistime
;
284 command
[2] = sector
& 0xFF;
285 command
[3] = (sector
>> 8) & 0xFF;
286 command
[4] = (sector
>> 16) & 0xFF;
288 command
[5] = 0xE0 | ((sector
>> 24) & 0x0F);
291 // send the setup + command
292 result
= usb_stor_ctrl_transfer(us
, us
->send_ctrl_pipe
,
293 0, 0x20, 0, 1, command
, 7);
294 if (result
!= USB_STOR_XFER_GOOD
)
298 result
= jumpshot_bulk_write(us
, buffer
, len
);
299 if (result
!= USB_STOR_XFER_GOOD
)
302 // read the result. apparently the bulk write can complete
303 // before the jumpshot drive is finished writing. so we loop
304 // here until we get a good return code
307 result
= jumpshot_get_status(us
);
308 if (result
!= USB_STOR_TRANSPORT_GOOD
) {
309 // I have not experimented to find the smallest value.
313 } while ((result
!= USB_STOR_TRANSPORT_GOOD
) && (waitcount
< 10));
315 if (result
!= USB_STOR_TRANSPORT_GOOD
)
316 usb_stor_dbg(us
, "Gah! Waitcount = 10. Bad write!?\n");
320 } while (totallen
> 0);
327 return USB_STOR_TRANSPORT_ERROR
;
330 static int jumpshot_id_device(struct us_data
*us
,
331 struct jumpshot_info
*info
)
333 unsigned char *command
= us
->iobuf
;
334 unsigned char *reply
;
338 return USB_STOR_TRANSPORT_ERROR
;
342 reply
= kmalloc(512, GFP_NOIO
);
344 return USB_STOR_TRANSPORT_ERROR
;
347 rc
= usb_stor_ctrl_transfer(us
, us
->send_ctrl_pipe
,
348 0, 0x20, 0, 6, command
, 2);
350 if (rc
!= USB_STOR_XFER_GOOD
) {
351 usb_stor_dbg(us
, "Gah! send_control for read_capacity failed\n");
352 rc
= USB_STOR_TRANSPORT_ERROR
;
357 rc
= jumpshot_bulk_read(us
, reply
, 512);
358 if (rc
!= USB_STOR_XFER_GOOD
) {
359 rc
= USB_STOR_TRANSPORT_ERROR
;
363 info
->sectors
= ((u32
)(reply
[117]) << 24) |
364 ((u32
)(reply
[116]) << 16) |
365 ((u32
)(reply
[115]) << 8) |
366 ((u32
)(reply
[114]) );
368 rc
= USB_STOR_TRANSPORT_GOOD
;
375 static int jumpshot_handle_mode_sense(struct us_data
*us
,
376 struct scsi_cmnd
* srb
,
379 static unsigned char rw_err_page
[12] = {
380 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
382 static unsigned char cache_page
[12] = {
383 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
385 static unsigned char rbac_page
[12] = {
386 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
388 static unsigned char timer_page
[8] = {
389 0x1C, 0x6, 0, 0, 0, 0
391 unsigned char pc
, page_code
;
393 struct jumpshot_info
*info
= (struct jumpshot_info
*) (us
->extra
);
394 unsigned char *ptr
= us
->iobuf
;
396 pc
= srb
->cmnd
[2] >> 6;
397 page_code
= srb
->cmnd
[2] & 0x3F;
401 usb_stor_dbg(us
, "Current values\n");
404 usb_stor_dbg(us
, "Changeable values\n");
407 usb_stor_dbg(us
, "Default values\n");
410 usb_stor_dbg(us
, "Saves values\n");
416 ptr
[2] = 0x00; // WP enable: 0x80
419 ptr
[3] = 0x00; // WP enable: 0x80
425 // vendor-specific mode
426 info
->sense_key
= 0x05;
427 info
->sense_asc
= 0x24;
428 info
->sense_ascq
= 0x00;
429 return USB_STOR_TRANSPORT_FAILED
;
432 memcpy(ptr
+ i
, rw_err_page
, sizeof(rw_err_page
));
433 i
+= sizeof(rw_err_page
);
437 memcpy(ptr
+ i
, cache_page
, sizeof(cache_page
));
438 i
+= sizeof(cache_page
);
442 memcpy(ptr
+ i
, rbac_page
, sizeof(rbac_page
));
443 i
+= sizeof(rbac_page
);
447 memcpy(ptr
+ i
, timer_page
, sizeof(timer_page
));
448 i
+= sizeof(timer_page
);
452 memcpy(ptr
+ i
, timer_page
, sizeof(timer_page
));
453 i
+= sizeof(timer_page
);
454 memcpy(ptr
+ i
, rbac_page
, sizeof(rbac_page
));
455 i
+= sizeof(rbac_page
);
456 memcpy(ptr
+ i
, cache_page
, sizeof(cache_page
));
457 i
+= sizeof(cache_page
);
458 memcpy(ptr
+ i
, rw_err_page
, sizeof(rw_err_page
));
459 i
+= sizeof(rw_err_page
);
466 ((__be16
*) ptr
)[0] = cpu_to_be16(i
- 2);
467 usb_stor_set_xfer_buf(ptr
, i
, srb
);
469 return USB_STOR_TRANSPORT_GOOD
;
473 static void jumpshot_info_destructor(void *extra
)
475 // this routine is a placeholder...
476 // currently, we don't allocate any extra blocks so we're okay
481 // Transport for the Lexar 'Jumpshot'
483 static int jumpshot_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
485 struct jumpshot_info
*info
;
487 unsigned long block
, blocks
;
488 unsigned char *ptr
= us
->iobuf
;
489 static unsigned char inquiry_response
[8] = {
490 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
494 us
->extra
= kzalloc(sizeof(struct jumpshot_info
), GFP_NOIO
);
496 return USB_STOR_TRANSPORT_ERROR
;
498 us
->extra_destructor
= jumpshot_info_destructor
;
501 info
= (struct jumpshot_info
*) (us
->extra
);
503 if (srb
->cmnd
[0] == INQUIRY
) {
504 usb_stor_dbg(us
, "INQUIRY - Returning bogus response\n");
505 memcpy(ptr
, inquiry_response
, sizeof(inquiry_response
));
506 fill_inquiry_response(us
, ptr
, 36);
507 return USB_STOR_TRANSPORT_GOOD
;
510 if (srb
->cmnd
[0] == READ_CAPACITY
) {
511 info
->ssize
= 0x200; // hard coded 512 byte sectors as per ATA spec
513 rc
= jumpshot_get_status(us
);
514 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
517 rc
= jumpshot_id_device(us
, info
);
518 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
521 usb_stor_dbg(us
, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
522 info
->sectors
, info
->ssize
);
526 ((__be32
*) ptr
)[0] = cpu_to_be32(info
->sectors
- 1);
527 ((__be32
*) ptr
)[1] = cpu_to_be32(info
->ssize
);
528 usb_stor_set_xfer_buf(ptr
, 8, srb
);
530 return USB_STOR_TRANSPORT_GOOD
;
533 if (srb
->cmnd
[0] == MODE_SELECT_10
) {
534 usb_stor_dbg(us
, "Gah! MODE_SELECT_10\n");
535 return USB_STOR_TRANSPORT_ERROR
;
538 if (srb
->cmnd
[0] == READ_10
) {
539 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
540 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
542 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
544 usb_stor_dbg(us
, "READ_10: read block 0x%04lx count %ld\n",
546 return jumpshot_read_data(us
, info
, block
, blocks
);
549 if (srb
->cmnd
[0] == READ_12
) {
550 // I don't think we'll ever see a READ_12 but support it anyway...
552 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
553 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
555 blocks
= ((u32
)(srb
->cmnd
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
556 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
558 usb_stor_dbg(us
, "READ_12: read block 0x%04lx count %ld\n",
560 return jumpshot_read_data(us
, info
, block
, blocks
);
563 if (srb
->cmnd
[0] == WRITE_10
) {
564 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
565 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
567 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
569 usb_stor_dbg(us
, "WRITE_10: write block 0x%04lx count %ld\n",
571 return jumpshot_write_data(us
, info
, block
, blocks
);
574 if (srb
->cmnd
[0] == WRITE_12
) {
575 // I don't think we'll ever see a WRITE_12 but support it anyway...
577 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
578 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
580 blocks
= ((u32
)(srb
->cmnd
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
581 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
583 usb_stor_dbg(us
, "WRITE_12: write block 0x%04lx count %ld\n",
585 return jumpshot_write_data(us
, info
, block
, blocks
);
589 if (srb
->cmnd
[0] == TEST_UNIT_READY
) {
590 usb_stor_dbg(us
, "TEST_UNIT_READY\n");
591 return jumpshot_get_status(us
);
594 if (srb
->cmnd
[0] == REQUEST_SENSE
) {
595 usb_stor_dbg(us
, "REQUEST_SENSE\n");
599 ptr
[2] = info
->sense_key
;
601 ptr
[12] = info
->sense_asc
;
602 ptr
[13] = info
->sense_ascq
;
603 usb_stor_set_xfer_buf(ptr
, 18, srb
);
605 return USB_STOR_TRANSPORT_GOOD
;
608 if (srb
->cmnd
[0] == MODE_SENSE
) {
609 usb_stor_dbg(us
, "MODE_SENSE_6 detected\n");
610 return jumpshot_handle_mode_sense(us
, srb
, 1);
613 if (srb
->cmnd
[0] == MODE_SENSE_10
) {
614 usb_stor_dbg(us
, "MODE_SENSE_10 detected\n");
615 return jumpshot_handle_mode_sense(us
, srb
, 0);
618 if (srb
->cmnd
[0] == ALLOW_MEDIUM_REMOVAL
) {
619 // sure. whatever. not like we can stop the user from popping
620 // the media out of the device (no locking doors, etc)
622 return USB_STOR_TRANSPORT_GOOD
;
625 if (srb
->cmnd
[0] == START_STOP
) {
626 /* this is used by sd.c'check_scsidisk_media_change to detect
628 usb_stor_dbg(us
, "START_STOP\n");
629 /* the first jumpshot_id_device after a media change returns
630 an error (determined experimentally) */
631 rc
= jumpshot_id_device(us
, info
);
632 if (rc
== USB_STOR_TRANSPORT_GOOD
) {
633 info
->sense_key
= NO_SENSE
;
634 srb
->result
= SUCCESS
;
636 info
->sense_key
= UNIT_ATTENTION
;
637 srb
->result
= SAM_STAT_CHECK_CONDITION
;
642 usb_stor_dbg(us
, "Gah! Unknown command: %d (0x%x)\n",
643 srb
->cmnd
[0], srb
->cmnd
[0]);
644 info
->sense_key
= 0x05;
645 info
->sense_asc
= 0x20;
646 info
->sense_ascq
= 0x00;
647 return USB_STOR_TRANSPORT_FAILED
;
650 static int jumpshot_probe(struct usb_interface
*intf
,
651 const struct usb_device_id
*id
)
656 result
= usb_stor_probe1(&us
, intf
, id
,
657 (id
- jumpshot_usb_ids
) + jumpshot_unusual_dev_list
);
661 us
->transport_name
= "Lexar Jumpshot Control/Bulk";
662 us
->transport
= jumpshot_transport
;
663 us
->transport_reset
= usb_stor_Bulk_reset
;
666 result
= usb_stor_probe2(us
);
670 static struct usb_driver jumpshot_driver
= {
671 .name
= "ums-jumpshot",
672 .probe
= jumpshot_probe
,
673 .disconnect
= usb_stor_disconnect
,
674 .suspend
= usb_stor_suspend
,
675 .resume
= usb_stor_resume
,
676 .reset_resume
= usb_stor_reset_resume
,
677 .pre_reset
= usb_stor_pre_reset
,
678 .post_reset
= usb_stor_post_reset
,
679 .id_table
= jumpshot_usb_ids
,
684 module_usb_driver(jumpshot_driver
);