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 #define DRV_NAME "ums-jumpshot"
63 MODULE_DESCRIPTION("Driver for Lexar \"Jumpshot\" Compact Flash reader");
64 MODULE_AUTHOR("Jimmie Mayfield <mayfield+usb@sackheads.org>");
65 MODULE_LICENSE("GPL");
68 * The table of devices
70 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
71 vendorName, productName, useProtocol, useTransport, \
72 initFunction, flags) \
73 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
74 .driver_info = (flags) }
76 static struct usb_device_id jumpshot_usb_ids
[] = {
77 # include "unusual_jumpshot.h"
78 { } /* Terminating entry */
80 MODULE_DEVICE_TABLE(usb
, jumpshot_usb_ids
);
87 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
88 vendor_name, product_name, use_protocol, use_transport, \
89 init_function, Flags) \
91 .vendorName = vendor_name, \
92 .productName = product_name, \
93 .useProtocol = use_protocol, \
94 .useTransport = use_transport, \
95 .initFunction = init_function, \
98 static struct us_unusual_dev jumpshot_unusual_dev_list
[] = {
99 # include "unusual_jumpshot.h"
100 { } /* Terminating entry */
106 struct jumpshot_info
{
107 unsigned long sectors
; /* total sector count */
108 unsigned long ssize
; /* sector size in bytes */
110 /* the following aren't used yet */
111 unsigned char sense_key
;
112 unsigned long sense_asc
; /* additional sense code */
113 unsigned long sense_ascq
; /* additional sense code qualifier */
116 static inline int jumpshot_bulk_read(struct us_data
*us
,
121 return USB_STOR_XFER_GOOD
;
123 usb_stor_dbg(us
, "len = %d\n", len
);
124 return usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
129 static inline int jumpshot_bulk_write(struct us_data
*us
,
134 return USB_STOR_XFER_GOOD
;
136 usb_stor_dbg(us
, "len = %d\n", len
);
137 return usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
142 static int jumpshot_get_status(struct us_data
*us
)
147 return USB_STOR_TRANSPORT_ERROR
;
150 rc
= usb_stor_ctrl_transfer(us
, us
->recv_ctrl_pipe
,
151 0, 0xA0, 0, 7, us
->iobuf
, 1);
153 if (rc
!= USB_STOR_XFER_GOOD
)
154 return USB_STOR_TRANSPORT_ERROR
;
156 if (us
->iobuf
[0] != 0x50) {
157 usb_stor_dbg(us
, "0x%2x\n", us
->iobuf
[0]);
158 return USB_STOR_TRANSPORT_ERROR
;
161 return USB_STOR_TRANSPORT_GOOD
;
164 static int jumpshot_read_data(struct us_data
*us
,
165 struct jumpshot_info
*info
,
169 unsigned char *command
= us
->iobuf
;
170 unsigned char *buffer
;
171 unsigned char thistime
;
172 unsigned int totallen
, alloclen
;
174 unsigned int sg_offset
= 0;
175 struct scatterlist
*sg
= NULL
;
177 // we're working in LBA mode. according to the ATA spec,
178 // we can support up to 28-bit addressing. I don't know if Jumpshot
179 // supports beyond 24-bit addressing. It's kind of hard to test
180 // since it requires > 8GB CF card.
182 if (sector
> 0x0FFFFFFF)
183 return USB_STOR_TRANSPORT_ERROR
;
185 totallen
= sectors
* info
->ssize
;
187 // Since we don't read more than 64 KB at a time, we have to create
188 // a bounce buffer and move the data a piece at a time between the
189 // bounce buffer and the actual transfer buffer.
191 alloclen
= min(totallen
, 65536u);
192 buffer
= kmalloc(alloclen
, GFP_NOIO
);
194 return USB_STOR_TRANSPORT_ERROR
;
197 // loop, never allocate or transfer more than 64k at once
198 // (min(128k, 255*info->ssize) is the real limit)
199 len
= min(totallen
, alloclen
);
200 thistime
= (len
/ info
->ssize
) & 0xff;
203 command
[1] = thistime
;
204 command
[2] = sector
& 0xFF;
205 command
[3] = (sector
>> 8) & 0xFF;
206 command
[4] = (sector
>> 16) & 0xFF;
208 command
[5] = 0xE0 | ((sector
>> 24) & 0x0F);
211 // send the setup + command
212 result
= usb_stor_ctrl_transfer(us
, us
->send_ctrl_pipe
,
213 0, 0x20, 0, 1, command
, 7);
214 if (result
!= USB_STOR_XFER_GOOD
)
218 result
= jumpshot_bulk_read(us
, buffer
, len
);
219 if (result
!= USB_STOR_XFER_GOOD
)
222 usb_stor_dbg(us
, "%d bytes\n", len
);
224 // Store the data in the transfer buffer
225 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
226 &sg
, &sg_offset
, TO_XFER_BUF
);
230 } while (totallen
> 0);
233 return USB_STOR_TRANSPORT_GOOD
;
237 return USB_STOR_TRANSPORT_ERROR
;
241 static int jumpshot_write_data(struct us_data
*us
,
242 struct jumpshot_info
*info
,
246 unsigned char *command
= us
->iobuf
;
247 unsigned char *buffer
;
248 unsigned char thistime
;
249 unsigned int totallen
, alloclen
;
250 int len
, result
, waitcount
;
251 unsigned int sg_offset
= 0;
252 struct scatterlist
*sg
= NULL
;
254 // we're working in LBA mode. according to the ATA spec,
255 // we can support up to 28-bit addressing. I don't know if Jumpshot
256 // supports beyond 24-bit addressing. It's kind of hard to test
257 // since it requires > 8GB CF card.
259 if (sector
> 0x0FFFFFFF)
260 return USB_STOR_TRANSPORT_ERROR
;
262 totallen
= sectors
* info
->ssize
;
264 // Since we don't write more than 64 KB at a time, we have to create
265 // a bounce buffer and move the data a piece at a time between the
266 // bounce buffer and the actual transfer buffer.
268 alloclen
= min(totallen
, 65536u);
269 buffer
= kmalloc(alloclen
, GFP_NOIO
);
271 return USB_STOR_TRANSPORT_ERROR
;
274 // loop, never allocate or transfer more than 64k at once
275 // (min(128k, 255*info->ssize) is the real limit)
277 len
= min(totallen
, alloclen
);
278 thistime
= (len
/ info
->ssize
) & 0xff;
280 // Get the data from the transfer buffer
281 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
282 &sg
, &sg_offset
, FROM_XFER_BUF
);
285 command
[1] = thistime
;
286 command
[2] = sector
& 0xFF;
287 command
[3] = (sector
>> 8) & 0xFF;
288 command
[4] = (sector
>> 16) & 0xFF;
290 command
[5] = 0xE0 | ((sector
>> 24) & 0x0F);
293 // send the setup + command
294 result
= usb_stor_ctrl_transfer(us
, us
->send_ctrl_pipe
,
295 0, 0x20, 0, 1, command
, 7);
296 if (result
!= USB_STOR_XFER_GOOD
)
300 result
= jumpshot_bulk_write(us
, buffer
, len
);
301 if (result
!= USB_STOR_XFER_GOOD
)
304 // read the result. apparently the bulk write can complete
305 // before the jumpshot drive is finished writing. so we loop
306 // here until we get a good return code
309 result
= jumpshot_get_status(us
);
310 if (result
!= USB_STOR_TRANSPORT_GOOD
) {
311 // I have not experimented to find the smallest value.
315 } while ((result
!= USB_STOR_TRANSPORT_GOOD
) && (waitcount
< 10));
317 if (result
!= USB_STOR_TRANSPORT_GOOD
)
318 usb_stor_dbg(us
, "Gah! Waitcount = 10. Bad write!?\n");
322 } while (totallen
> 0);
329 return USB_STOR_TRANSPORT_ERROR
;
332 static int jumpshot_id_device(struct us_data
*us
,
333 struct jumpshot_info
*info
)
335 unsigned char *command
= us
->iobuf
;
336 unsigned char *reply
;
340 return USB_STOR_TRANSPORT_ERROR
;
344 reply
= kmalloc(512, GFP_NOIO
);
346 return USB_STOR_TRANSPORT_ERROR
;
349 rc
= usb_stor_ctrl_transfer(us
, us
->send_ctrl_pipe
,
350 0, 0x20, 0, 6, command
, 2);
352 if (rc
!= USB_STOR_XFER_GOOD
) {
353 usb_stor_dbg(us
, "Gah! send_control for read_capacity failed\n");
354 rc
= USB_STOR_TRANSPORT_ERROR
;
359 rc
= jumpshot_bulk_read(us
, reply
, 512);
360 if (rc
!= USB_STOR_XFER_GOOD
) {
361 rc
= USB_STOR_TRANSPORT_ERROR
;
365 info
->sectors
= ((u32
)(reply
[117]) << 24) |
366 ((u32
)(reply
[116]) << 16) |
367 ((u32
)(reply
[115]) << 8) |
368 ((u32
)(reply
[114]) );
370 rc
= USB_STOR_TRANSPORT_GOOD
;
377 static int jumpshot_handle_mode_sense(struct us_data
*us
,
378 struct scsi_cmnd
* srb
,
381 static unsigned char rw_err_page
[12] = {
382 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
384 static unsigned char cache_page
[12] = {
385 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
387 static unsigned char rbac_page
[12] = {
388 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
390 static unsigned char timer_page
[8] = {
391 0x1C, 0x6, 0, 0, 0, 0
393 unsigned char pc
, page_code
;
395 struct jumpshot_info
*info
= (struct jumpshot_info
*) (us
->extra
);
396 unsigned char *ptr
= us
->iobuf
;
398 pc
= srb
->cmnd
[2] >> 6;
399 page_code
= srb
->cmnd
[2] & 0x3F;
403 usb_stor_dbg(us
, "Current values\n");
406 usb_stor_dbg(us
, "Changeable values\n");
409 usb_stor_dbg(us
, "Default values\n");
412 usb_stor_dbg(us
, "Saves values\n");
418 ptr
[2] = 0x00; // WP enable: 0x80
421 ptr
[3] = 0x00; // WP enable: 0x80
427 // vendor-specific mode
428 info
->sense_key
= 0x05;
429 info
->sense_asc
= 0x24;
430 info
->sense_ascq
= 0x00;
431 return USB_STOR_TRANSPORT_FAILED
;
434 memcpy(ptr
+ i
, rw_err_page
, sizeof(rw_err_page
));
435 i
+= sizeof(rw_err_page
);
439 memcpy(ptr
+ i
, cache_page
, sizeof(cache_page
));
440 i
+= sizeof(cache_page
);
444 memcpy(ptr
+ i
, rbac_page
, sizeof(rbac_page
));
445 i
+= sizeof(rbac_page
);
449 memcpy(ptr
+ i
, timer_page
, sizeof(timer_page
));
450 i
+= sizeof(timer_page
);
454 memcpy(ptr
+ i
, timer_page
, sizeof(timer_page
));
455 i
+= sizeof(timer_page
);
456 memcpy(ptr
+ i
, rbac_page
, sizeof(rbac_page
));
457 i
+= sizeof(rbac_page
);
458 memcpy(ptr
+ i
, cache_page
, sizeof(cache_page
));
459 i
+= sizeof(cache_page
);
460 memcpy(ptr
+ i
, rw_err_page
, sizeof(rw_err_page
));
461 i
+= sizeof(rw_err_page
);
468 ((__be16
*) ptr
)[0] = cpu_to_be16(i
- 2);
469 usb_stor_set_xfer_buf(ptr
, i
, srb
);
471 return USB_STOR_TRANSPORT_GOOD
;
475 static void jumpshot_info_destructor(void *extra
)
477 // this routine is a placeholder...
478 // currently, we don't allocate any extra blocks so we're okay
483 // Transport for the Lexar 'Jumpshot'
485 static int jumpshot_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
487 struct jumpshot_info
*info
;
489 unsigned long block
, blocks
;
490 unsigned char *ptr
= us
->iobuf
;
491 static unsigned char inquiry_response
[8] = {
492 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
496 us
->extra
= kzalloc(sizeof(struct jumpshot_info
), GFP_NOIO
);
498 return USB_STOR_TRANSPORT_ERROR
;
500 us
->extra_destructor
= jumpshot_info_destructor
;
503 info
= (struct jumpshot_info
*) (us
->extra
);
505 if (srb
->cmnd
[0] == INQUIRY
) {
506 usb_stor_dbg(us
, "INQUIRY - Returning bogus response\n");
507 memcpy(ptr
, inquiry_response
, sizeof(inquiry_response
));
508 fill_inquiry_response(us
, ptr
, 36);
509 return USB_STOR_TRANSPORT_GOOD
;
512 if (srb
->cmnd
[0] == READ_CAPACITY
) {
513 info
->ssize
= 0x200; // hard coded 512 byte sectors as per ATA spec
515 rc
= jumpshot_get_status(us
);
516 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
519 rc
= jumpshot_id_device(us
, info
);
520 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
523 usb_stor_dbg(us
, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
524 info
->sectors
, info
->ssize
);
528 ((__be32
*) ptr
)[0] = cpu_to_be32(info
->sectors
- 1);
529 ((__be32
*) ptr
)[1] = cpu_to_be32(info
->ssize
);
530 usb_stor_set_xfer_buf(ptr
, 8, srb
);
532 return USB_STOR_TRANSPORT_GOOD
;
535 if (srb
->cmnd
[0] == MODE_SELECT_10
) {
536 usb_stor_dbg(us
, "Gah! MODE_SELECT_10\n");
537 return USB_STOR_TRANSPORT_ERROR
;
540 if (srb
->cmnd
[0] == READ_10
) {
541 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
542 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
544 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
546 usb_stor_dbg(us
, "READ_10: read block 0x%04lx count %ld\n",
548 return jumpshot_read_data(us
, info
, block
, blocks
);
551 if (srb
->cmnd
[0] == READ_12
) {
552 // I don't think we'll ever see a READ_12 but support it anyway...
554 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
555 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
557 blocks
= ((u32
)(srb
->cmnd
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
558 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
560 usb_stor_dbg(us
, "READ_12: read block 0x%04lx count %ld\n",
562 return jumpshot_read_data(us
, info
, block
, blocks
);
565 if (srb
->cmnd
[0] == WRITE_10
) {
566 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
567 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
569 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
571 usb_stor_dbg(us
, "WRITE_10: write block 0x%04lx count %ld\n",
573 return jumpshot_write_data(us
, info
, block
, blocks
);
576 if (srb
->cmnd
[0] == WRITE_12
) {
577 // I don't think we'll ever see a WRITE_12 but support it anyway...
579 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
580 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
582 blocks
= ((u32
)(srb
->cmnd
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
583 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
585 usb_stor_dbg(us
, "WRITE_12: write block 0x%04lx count %ld\n",
587 return jumpshot_write_data(us
, info
, block
, blocks
);
591 if (srb
->cmnd
[0] == TEST_UNIT_READY
) {
592 usb_stor_dbg(us
, "TEST_UNIT_READY\n");
593 return jumpshot_get_status(us
);
596 if (srb
->cmnd
[0] == REQUEST_SENSE
) {
597 usb_stor_dbg(us
, "REQUEST_SENSE\n");
601 ptr
[2] = info
->sense_key
;
603 ptr
[12] = info
->sense_asc
;
604 ptr
[13] = info
->sense_ascq
;
605 usb_stor_set_xfer_buf(ptr
, 18, srb
);
607 return USB_STOR_TRANSPORT_GOOD
;
610 if (srb
->cmnd
[0] == MODE_SENSE
) {
611 usb_stor_dbg(us
, "MODE_SENSE_6 detected\n");
612 return jumpshot_handle_mode_sense(us
, srb
, 1);
615 if (srb
->cmnd
[0] == MODE_SENSE_10
) {
616 usb_stor_dbg(us
, "MODE_SENSE_10 detected\n");
617 return jumpshot_handle_mode_sense(us
, srb
, 0);
620 if (srb
->cmnd
[0] == ALLOW_MEDIUM_REMOVAL
) {
621 // sure. whatever. not like we can stop the user from popping
622 // the media out of the device (no locking doors, etc)
624 return USB_STOR_TRANSPORT_GOOD
;
627 if (srb
->cmnd
[0] == START_STOP
) {
628 /* this is used by sd.c'check_scsidisk_media_change to detect
630 usb_stor_dbg(us
, "START_STOP\n");
631 /* the first jumpshot_id_device after a media change returns
632 an error (determined experimentally) */
633 rc
= jumpshot_id_device(us
, info
);
634 if (rc
== USB_STOR_TRANSPORT_GOOD
) {
635 info
->sense_key
= NO_SENSE
;
636 srb
->result
= SUCCESS
;
638 info
->sense_key
= UNIT_ATTENTION
;
639 srb
->result
= SAM_STAT_CHECK_CONDITION
;
644 usb_stor_dbg(us
, "Gah! Unknown command: %d (0x%x)\n",
645 srb
->cmnd
[0], srb
->cmnd
[0]);
646 info
->sense_key
= 0x05;
647 info
->sense_asc
= 0x20;
648 info
->sense_ascq
= 0x00;
649 return USB_STOR_TRANSPORT_FAILED
;
652 static struct scsi_host_template jumpshot_host_template
;
654 static int jumpshot_probe(struct usb_interface
*intf
,
655 const struct usb_device_id
*id
)
660 result
= usb_stor_probe1(&us
, intf
, id
,
661 (id
- jumpshot_usb_ids
) + jumpshot_unusual_dev_list
,
662 &jumpshot_host_template
);
666 us
->transport_name
= "Lexar Jumpshot Control/Bulk";
667 us
->transport
= jumpshot_transport
;
668 us
->transport_reset
= usb_stor_Bulk_reset
;
671 result
= usb_stor_probe2(us
);
675 static struct usb_driver jumpshot_driver
= {
677 .probe
= jumpshot_probe
,
678 .disconnect
= usb_stor_disconnect
,
679 .suspend
= usb_stor_suspend
,
680 .resume
= usb_stor_resume
,
681 .reset_resume
= usb_stor_reset_resume
,
682 .pre_reset
= usb_stor_pre_reset
,
683 .post_reset
= usb_stor_post_reset
,
684 .id_table
= jumpshot_usb_ids
,
689 module_usb_stor_driver(jumpshot_driver
, jumpshot_host_template
, DRV_NAME
);