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/slab.h>
51 #include <scsi/scsi.h>
52 #include <scsi/scsi_cmnd.h>
55 #include "transport.h"
61 static inline int jumpshot_bulk_read(struct us_data
*us
,
66 return USB_STOR_XFER_GOOD
;
68 US_DEBUGP("jumpshot_bulk_read: len = %d\n", len
);
69 return usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
74 static inline int jumpshot_bulk_write(struct us_data
*us
,
79 return USB_STOR_XFER_GOOD
;
81 US_DEBUGP("jumpshot_bulk_write: len = %d\n", len
);
82 return usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
87 static int jumpshot_get_status(struct us_data
*us
)
92 return USB_STOR_TRANSPORT_ERROR
;
95 rc
= usb_stor_ctrl_transfer(us
, us
->recv_ctrl_pipe
,
96 0, 0xA0, 0, 7, us
->iobuf
, 1);
98 if (rc
!= USB_STOR_XFER_GOOD
)
99 return USB_STOR_TRANSPORT_ERROR
;
101 if (us
->iobuf
[0] != 0x50) {
102 US_DEBUGP("jumpshot_get_status: 0x%2x\n",
104 return USB_STOR_TRANSPORT_ERROR
;
107 return USB_STOR_TRANSPORT_GOOD
;
110 static int jumpshot_read_data(struct us_data
*us
,
111 struct jumpshot_info
*info
,
115 unsigned char *command
= us
->iobuf
;
116 unsigned char *buffer
;
117 unsigned char thistime
;
118 unsigned int totallen
, alloclen
;
120 unsigned int sg_offset
= 0;
121 struct scatterlist
*sg
= NULL
;
123 // we're working in LBA mode. according to the ATA spec,
124 // we can support up to 28-bit addressing. I don't know if Jumpshot
125 // supports beyond 24-bit addressing. It's kind of hard to test
126 // since it requires > 8GB CF card.
128 if (sector
> 0x0FFFFFFF)
129 return USB_STOR_TRANSPORT_ERROR
;
131 totallen
= sectors
* info
->ssize
;
133 // Since we don't read more than 64 KB at a time, we have to create
134 // a bounce buffer and move the data a piece at a time between the
135 // bounce buffer and the actual transfer buffer.
137 alloclen
= min(totallen
, 65536u);
138 buffer
= kmalloc(alloclen
, GFP_NOIO
);
140 return USB_STOR_TRANSPORT_ERROR
;
143 // loop, never allocate or transfer more than 64k at once
144 // (min(128k, 255*info->ssize) is the real limit)
145 len
= min(totallen
, alloclen
);
146 thistime
= (len
/ info
->ssize
) & 0xff;
149 command
[1] = thistime
;
150 command
[2] = sector
& 0xFF;
151 command
[3] = (sector
>> 8) & 0xFF;
152 command
[4] = (sector
>> 16) & 0xFF;
154 command
[5] = 0xE0 | ((sector
>> 24) & 0x0F);
157 // send the setup + command
158 result
= usb_stor_ctrl_transfer(us
, us
->send_ctrl_pipe
,
159 0, 0x20, 0, 1, command
, 7);
160 if (result
!= USB_STOR_XFER_GOOD
)
164 result
= jumpshot_bulk_read(us
, buffer
, len
);
165 if (result
!= USB_STOR_XFER_GOOD
)
168 US_DEBUGP("jumpshot_read_data: %d bytes\n", len
);
170 // Store the data in the transfer buffer
171 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
172 &sg
, &sg_offset
, TO_XFER_BUF
);
176 } while (totallen
> 0);
179 return USB_STOR_TRANSPORT_GOOD
;
183 return USB_STOR_TRANSPORT_ERROR
;
187 static int jumpshot_write_data(struct us_data
*us
,
188 struct jumpshot_info
*info
,
192 unsigned char *command
= us
->iobuf
;
193 unsigned char *buffer
;
194 unsigned char thistime
;
195 unsigned int totallen
, alloclen
;
196 int len
, result
, waitcount
;
197 unsigned int sg_offset
= 0;
198 struct scatterlist
*sg
= NULL
;
200 // we're working in LBA mode. according to the ATA spec,
201 // we can support up to 28-bit addressing. I don't know if Jumpshot
202 // supports beyond 24-bit addressing. It's kind of hard to test
203 // since it requires > 8GB CF card.
205 if (sector
> 0x0FFFFFFF)
206 return USB_STOR_TRANSPORT_ERROR
;
208 totallen
= sectors
* info
->ssize
;
210 // Since we don't write more than 64 KB at a time, we have to create
211 // a bounce buffer and move the data a piece at a time between the
212 // bounce buffer and the actual transfer buffer.
214 alloclen
= min(totallen
, 65536u);
215 buffer
= kmalloc(alloclen
, GFP_NOIO
);
217 return USB_STOR_TRANSPORT_ERROR
;
220 // loop, never allocate or transfer more than 64k at once
221 // (min(128k, 255*info->ssize) is the real limit)
223 len
= min(totallen
, alloclen
);
224 thistime
= (len
/ info
->ssize
) & 0xff;
226 // Get the data from the transfer buffer
227 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
228 &sg
, &sg_offset
, FROM_XFER_BUF
);
231 command
[1] = thistime
;
232 command
[2] = sector
& 0xFF;
233 command
[3] = (sector
>> 8) & 0xFF;
234 command
[4] = (sector
>> 16) & 0xFF;
236 command
[5] = 0xE0 | ((sector
>> 24) & 0x0F);
239 // send the setup + command
240 result
= usb_stor_ctrl_transfer(us
, us
->send_ctrl_pipe
,
241 0, 0x20, 0, 1, command
, 7);
242 if (result
!= USB_STOR_XFER_GOOD
)
246 result
= jumpshot_bulk_write(us
, buffer
, len
);
247 if (result
!= USB_STOR_XFER_GOOD
)
250 // read the result. apparently the bulk write can complete
251 // before the jumpshot drive is finished writing. so we loop
252 // here until we get a good return code
255 result
= jumpshot_get_status(us
);
256 if (result
!= USB_STOR_TRANSPORT_GOOD
) {
257 // I have not experimented to find the smallest value.
261 } while ((result
!= USB_STOR_TRANSPORT_GOOD
) && (waitcount
< 10));
263 if (result
!= USB_STOR_TRANSPORT_GOOD
)
264 US_DEBUGP("jumpshot_write_data: Gah! Waitcount = 10. Bad write!?\n");
268 } while (totallen
> 0);
275 return USB_STOR_TRANSPORT_ERROR
;
278 static int jumpshot_id_device(struct us_data
*us
,
279 struct jumpshot_info
*info
)
281 unsigned char *command
= us
->iobuf
;
282 unsigned char *reply
;
286 return USB_STOR_TRANSPORT_ERROR
;
290 reply
= kmalloc(512, GFP_NOIO
);
292 return USB_STOR_TRANSPORT_ERROR
;
295 rc
= usb_stor_ctrl_transfer(us
, us
->send_ctrl_pipe
,
296 0, 0x20, 0, 6, command
, 2);
298 if (rc
!= USB_STOR_XFER_GOOD
) {
299 US_DEBUGP("jumpshot_id_device: Gah! "
300 "send_control for read_capacity failed\n");
301 rc
= USB_STOR_TRANSPORT_ERROR
;
306 rc
= jumpshot_bulk_read(us
, reply
, 512);
307 if (rc
!= USB_STOR_XFER_GOOD
) {
308 rc
= USB_STOR_TRANSPORT_ERROR
;
312 info
->sectors
= ((u32
)(reply
[117]) << 24) |
313 ((u32
)(reply
[116]) << 16) |
314 ((u32
)(reply
[115]) << 8) |
315 ((u32
)(reply
[114]) );
317 rc
= USB_STOR_TRANSPORT_GOOD
;
324 static int jumpshot_handle_mode_sense(struct us_data
*us
,
325 struct scsi_cmnd
* srb
,
328 static unsigned char rw_err_page
[12] = {
329 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
331 static unsigned char cache_page
[12] = {
332 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
334 static unsigned char rbac_page
[12] = {
335 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
337 static unsigned char timer_page
[8] = {
338 0x1C, 0x6, 0, 0, 0, 0
340 unsigned char pc
, page_code
;
342 struct jumpshot_info
*info
= (struct jumpshot_info
*) (us
->extra
);
343 unsigned char *ptr
= us
->iobuf
;
345 pc
= srb
->cmnd
[2] >> 6;
346 page_code
= srb
->cmnd
[2] & 0x3F;
350 US_DEBUGP("jumpshot_handle_mode_sense: Current values\n");
353 US_DEBUGP("jumpshot_handle_mode_sense: Changeable values\n");
356 US_DEBUGP("jumpshot_handle_mode_sense: Default values\n");
359 US_DEBUGP("jumpshot_handle_mode_sense: Saves values\n");
365 ptr
[2] = 0x00; // WP enable: 0x80
368 ptr
[3] = 0x00; // WP enable: 0x80
374 // vendor-specific mode
375 info
->sense_key
= 0x05;
376 info
->sense_asc
= 0x24;
377 info
->sense_ascq
= 0x00;
378 return USB_STOR_TRANSPORT_FAILED
;
381 memcpy(ptr
+ i
, rw_err_page
, sizeof(rw_err_page
));
382 i
+= sizeof(rw_err_page
);
386 memcpy(ptr
+ i
, cache_page
, sizeof(cache_page
));
387 i
+= sizeof(cache_page
);
391 memcpy(ptr
+ i
, rbac_page
, sizeof(rbac_page
));
392 i
+= sizeof(rbac_page
);
396 memcpy(ptr
+ i
, timer_page
, sizeof(timer_page
));
397 i
+= sizeof(timer_page
);
401 memcpy(ptr
+ i
, timer_page
, sizeof(timer_page
));
402 i
+= sizeof(timer_page
);
403 memcpy(ptr
+ i
, rbac_page
, sizeof(rbac_page
));
404 i
+= sizeof(rbac_page
);
405 memcpy(ptr
+ i
, cache_page
, sizeof(cache_page
));
406 i
+= sizeof(cache_page
);
407 memcpy(ptr
+ i
, rw_err_page
, sizeof(rw_err_page
));
408 i
+= sizeof(rw_err_page
);
415 ((__be16
*) ptr
)[0] = cpu_to_be16(i
- 2);
416 usb_stor_set_xfer_buf(ptr
, i
, srb
);
418 return USB_STOR_TRANSPORT_GOOD
;
422 static void jumpshot_info_destructor(void *extra
)
424 // this routine is a placeholder...
425 // currently, we don't allocate any extra blocks so we're okay
430 // Transport for the Lexar 'Jumpshot'
432 int jumpshot_transport(struct scsi_cmnd
* srb
, struct us_data
*us
)
434 struct jumpshot_info
*info
;
436 unsigned long block
, blocks
;
437 unsigned char *ptr
= us
->iobuf
;
438 static unsigned char inquiry_response
[8] = {
439 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
443 us
->extra
= kzalloc(sizeof(struct jumpshot_info
), GFP_NOIO
);
445 US_DEBUGP("jumpshot_transport: Gah! Can't allocate storage for jumpshot info struct!\n");
446 return USB_STOR_TRANSPORT_ERROR
;
448 us
->extra_destructor
= jumpshot_info_destructor
;
451 info
= (struct jumpshot_info
*) (us
->extra
);
453 if (srb
->cmnd
[0] == INQUIRY
) {
454 US_DEBUGP("jumpshot_transport: INQUIRY. Returning bogus response.\n");
455 memcpy(ptr
, inquiry_response
, sizeof(inquiry_response
));
456 fill_inquiry_response(us
, ptr
, 36);
457 return USB_STOR_TRANSPORT_GOOD
;
460 if (srb
->cmnd
[0] == READ_CAPACITY
) {
461 info
->ssize
= 0x200; // hard coded 512 byte sectors as per ATA spec
463 rc
= jumpshot_get_status(us
);
464 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
467 rc
= jumpshot_id_device(us
, info
);
468 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
471 US_DEBUGP("jumpshot_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
472 info
->sectors
, info
->ssize
);
476 ((__be32
*) ptr
)[0] = cpu_to_be32(info
->sectors
- 1);
477 ((__be32
*) ptr
)[1] = cpu_to_be32(info
->ssize
);
478 usb_stor_set_xfer_buf(ptr
, 8, srb
);
480 return USB_STOR_TRANSPORT_GOOD
;
483 if (srb
->cmnd
[0] == MODE_SELECT_10
) {
484 US_DEBUGP("jumpshot_transport: Gah! MODE_SELECT_10.\n");
485 return USB_STOR_TRANSPORT_ERROR
;
488 if (srb
->cmnd
[0] == READ_10
) {
489 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
490 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
492 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
494 US_DEBUGP("jumpshot_transport: READ_10: read block 0x%04lx count %ld\n", block
, blocks
);
495 return jumpshot_read_data(us
, info
, block
, blocks
);
498 if (srb
->cmnd
[0] == READ_12
) {
499 // I don't think we'll ever see a READ_12 but support it anyway...
501 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
502 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
504 blocks
= ((u32
)(srb
->cmnd
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
505 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
507 US_DEBUGP("jumpshot_transport: READ_12: read block 0x%04lx count %ld\n", block
, blocks
);
508 return jumpshot_read_data(us
, info
, block
, blocks
);
511 if (srb
->cmnd
[0] == WRITE_10
) {
512 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
513 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
515 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
517 US_DEBUGP("jumpshot_transport: WRITE_10: write block 0x%04lx count %ld\n", block
, blocks
);
518 return jumpshot_write_data(us
, info
, block
, blocks
);
521 if (srb
->cmnd
[0] == WRITE_12
) {
522 // I don't think we'll ever see a WRITE_12 but support it anyway...
524 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
525 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
527 blocks
= ((u32
)(srb
->cmnd
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
528 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
530 US_DEBUGP("jumpshot_transport: WRITE_12: write block 0x%04lx count %ld\n", block
, blocks
);
531 return jumpshot_write_data(us
, info
, block
, blocks
);
535 if (srb
->cmnd
[0] == TEST_UNIT_READY
) {
536 US_DEBUGP("jumpshot_transport: TEST_UNIT_READY.\n");
537 return jumpshot_get_status(us
);
540 if (srb
->cmnd
[0] == REQUEST_SENSE
) {
541 US_DEBUGP("jumpshot_transport: REQUEST_SENSE.\n");
545 ptr
[2] = info
->sense_key
;
547 ptr
[12] = info
->sense_asc
;
548 ptr
[13] = info
->sense_ascq
;
549 usb_stor_set_xfer_buf(ptr
, 18, srb
);
551 return USB_STOR_TRANSPORT_GOOD
;
554 if (srb
->cmnd
[0] == MODE_SENSE
) {
555 US_DEBUGP("jumpshot_transport: MODE_SENSE_6 detected\n");
556 return jumpshot_handle_mode_sense(us
, srb
, 1);
559 if (srb
->cmnd
[0] == MODE_SENSE_10
) {
560 US_DEBUGP("jumpshot_transport: MODE_SENSE_10 detected\n");
561 return jumpshot_handle_mode_sense(us
, srb
, 0);
564 if (srb
->cmnd
[0] == ALLOW_MEDIUM_REMOVAL
) {
565 // sure. whatever. not like we can stop the user from popping
566 // the media out of the device (no locking doors, etc)
568 return USB_STOR_TRANSPORT_GOOD
;
571 if (srb
->cmnd
[0] == START_STOP
) {
572 /* this is used by sd.c'check_scsidisk_media_change to detect
574 US_DEBUGP("jumpshot_transport: START_STOP.\n");
575 /* the first jumpshot_id_device after a media change returns
576 an error (determined experimentally) */
577 rc
= jumpshot_id_device(us
, info
);
578 if (rc
== USB_STOR_TRANSPORT_GOOD
) {
579 info
->sense_key
= NO_SENSE
;
580 srb
->result
= SUCCESS
;
582 info
->sense_key
= UNIT_ATTENTION
;
583 srb
->result
= SAM_STAT_CHECK_CONDITION
;
588 US_DEBUGP("jumpshot_transport: Gah! Unknown command: %d (0x%x)\n",
589 srb
->cmnd
[0], srb
->cmnd
[0]);
590 info
->sense_key
= 0x05;
591 info
->sense_asc
= 0x20;
592 info
->sense_ascq
= 0x00;
593 return USB_STOR_TRANSPORT_FAILED
;