1 /* Driver for Lexar "Jumpshot" Compact Flash reader
3 * $Id: jumpshot.c,v 1.7 2002/02/25 00:40:13 mdharm Exp $
5 * jumpshot driver v0.1:
9 * Current development and maintenance by:
10 * (c) 2000 Jimmie Mayfield (mayfield+usb@sackheads.org)
12 * Many thanks to Robert Baruch for the SanDisk SmartMedia reader driver
13 * which I used as a template for this driver.
15 * Some bugfixes and scatter-gather code by Gregory P. Smith
16 * (greg-usb@electricrain.com)
18 * Fix for media change by Joerg Schneider (js@joergschneider.com)
20 * Developed with the assistance of:
22 * (C) 2002 Alan Stern <stern@rowland.org>
24 * This program is free software; you can redistribute it and/or modify it
25 * under the terms of the GNU General Public License as published by the
26 * Free Software Foundation; either version 2, or (at your option) any
29 * This program is distributed in the hope that it will be useful, but
30 * WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * General Public License for more details.
34 * You should have received a copy of the GNU General Public License along
35 * with this program; if not, write to the Free Software Foundation, Inc.,
36 * 675 Mass Ave, Cambridge, MA 02139, USA.
40 * This driver attempts to support the Lexar Jumpshot USB CompactFlash
41 * reader. Like many other USB CompactFlash readers, the Jumpshot contains
44 * This driver supports reading and writing. If you're truly paranoid,
45 * however, you can force the driver into a write-protected state by setting
46 * the WP enable bits in jumpshot_handle_mode_sense. See the comments
50 #include "transport.h"
56 #include <linux/sched.h>
57 #include <linux/errno.h>
58 #include <linux/slab.h>
60 static inline int jumpshot_bulk_read(struct us_data *us,
65 return USB_STOR_XFER_GOOD;
67 US_DEBUGP("jumpshot_bulk_read: len = %d\n", len);
68 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
73 static inline int jumpshot_bulk_write(struct us_data *us,
78 return USB_STOR_XFER_GOOD;
80 US_DEBUGP("jumpshot_bulk_write: len = %d\n", len);
81 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
86 static int jumpshot_get_status(struct us_data *us)
91 return USB_STOR_TRANSPORT_ERROR;
94 rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
95 0, 0xA0, 0, 7, us->iobuf, 1);
97 if (rc != USB_STOR_XFER_GOOD)
98 return USB_STOR_TRANSPORT_ERROR;
100 if (us->iobuf[0] != 0x50) {
101 US_DEBUGP("jumpshot_get_status: 0x%2x\n",
103 return USB_STOR_TRANSPORT_ERROR;
106 return USB_STOR_TRANSPORT_GOOD;
109 static int jumpshot_read_data(struct us_data *us,
110 struct jumpshot_info *info,
114 unsigned char *command = us->iobuf;
115 unsigned char *buffer;
116 unsigned char thistime;
117 unsigned int totallen, alloclen;
119 unsigned int sg_idx = 0, sg_offset = 0;
121 // we're working in LBA mode. according to the ATA spec,
122 // we can support up to 28-bit addressing. I don't know if Jumpshot
123 // supports beyond 24-bit addressing. It's kind of hard to test
124 // since it requires > 8GB CF card.
126 if (sector > 0x0FFFFFFF)
127 return USB_STOR_TRANSPORT_ERROR;
129 totallen = sectors * info->ssize;
131 // Since we don't read more than 64 KB at a time, we have to create
132 // a bounce buffer and move the data a piece at a time between the
133 // bounce buffer and the actual transfer buffer.
135 alloclen = min(totallen, 65536u);
136 buffer = kmalloc(alloclen, GFP_NOIO);
138 return USB_STOR_TRANSPORT_ERROR;
141 // loop, never allocate or transfer more than 64k at once
142 // (min(128k, 255*info->ssize) is the real limit)
143 len = min(totallen, alloclen);
144 thistime = (len / info->ssize) & 0xff;
147 command[1] = thistime;
148 command[2] = sector & 0xFF;
149 command[3] = (sector >> 8) & 0xFF;
150 command[4] = (sector >> 16) & 0xFF;
152 command[5] = 0xE0 | ((sector >> 24) & 0x0F);
155 // send the setup + command
156 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
157 0, 0x20, 0, 1, command, 7);
158 if (result != USB_STOR_XFER_GOOD)
162 result = jumpshot_bulk_read(us, buffer, len);
163 if (result != USB_STOR_XFER_GOOD)
166 US_DEBUGP("jumpshot_read_data: %d bytes\n", len);
168 // Store the data in the transfer buffer
169 usb_stor_access_xfer_buf(buffer, len, us->srb,
170 &sg_idx, &sg_offset, TO_XFER_BUF);
174 } while (totallen > 0);
177 return USB_STOR_TRANSPORT_GOOD;
181 return USB_STOR_TRANSPORT_ERROR;
185 static int jumpshot_write_data(struct us_data *us,
186 struct jumpshot_info *info,
190 unsigned char *command = us->iobuf;
191 unsigned char *buffer;
192 unsigned char thistime;
193 unsigned int totallen, alloclen;
194 int len, result, waitcount;
195 unsigned int sg_idx = 0, sg_offset = 0;
197 // we're working in LBA mode. according to the ATA spec,
198 // we can support up to 28-bit addressing. I don't know if Jumpshot
199 // supports beyond 24-bit addressing. It's kind of hard to test
200 // since it requires > 8GB CF card.
202 if (sector > 0x0FFFFFFF)
203 return USB_STOR_TRANSPORT_ERROR;
205 totallen = sectors * info->ssize;
207 // Since we don't write more than 64 KB at a time, we have to create
208 // a bounce buffer and move the data a piece at a time between the
209 // bounce buffer and the actual transfer buffer.
211 alloclen = min(totallen, 65536u);
212 buffer = kmalloc(alloclen, GFP_NOIO);
214 return USB_STOR_TRANSPORT_ERROR;
217 // loop, never allocate or transfer more than 64k at once
218 // (min(128k, 255*info->ssize) is the real limit)
220 len = min(totallen, alloclen);
221 thistime = (len / info->ssize) & 0xff;
223 // Get the data from the transfer buffer
224 usb_stor_access_xfer_buf(buffer, len, us->srb,
225 &sg_idx, &sg_offset, FROM_XFER_BUF);
228 command[1] = thistime;
229 command[2] = sector & 0xFF;
230 command[3] = (sector >> 8) & 0xFF;
231 command[4] = (sector >> 16) & 0xFF;
233 command[5] = 0xE0 | ((sector >> 24) & 0x0F);
236 // send the setup + command
237 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
238 0, 0x20, 0, 1, command, 7);
239 if (result != USB_STOR_XFER_GOOD)
243 result = jumpshot_bulk_write(us, buffer, len);
244 if (result != USB_STOR_XFER_GOOD)
247 // read the result. apparently the bulk write can complete
248 // before the jumpshot drive is finished writing. so we loop
249 // here until we get a good return code
252 result = jumpshot_get_status(us);
253 if (result != USB_STOR_TRANSPORT_GOOD) {
254 // I have not experimented to find the smallest value.
258 } while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));
260 if (result != USB_STOR_TRANSPORT_GOOD)
261 US_DEBUGP("jumpshot_write_data: Gah! Waitcount = 10. Bad write!?\n");
265 } while (totallen > 0);
272 return USB_STOR_TRANSPORT_ERROR;
275 static int jumpshot_id_device(struct us_data *us,
276 struct jumpshot_info *info)
278 unsigned char *command = us->iobuf;
279 unsigned char *reply;
283 return USB_STOR_TRANSPORT_ERROR;
287 reply = kmalloc(512, GFP_NOIO);
289 return USB_STOR_TRANSPORT_ERROR;
292 rc = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
293 0, 0x20, 0, 6, command, 2);
295 if (rc != USB_STOR_XFER_GOOD) {
296 US_DEBUGP("jumpshot_id_device: Gah! "
297 "send_control for read_capacity failed\n");
298 rc = USB_STOR_TRANSPORT_ERROR;
303 rc = jumpshot_bulk_read(us, reply, 512);
304 if (rc != USB_STOR_XFER_GOOD) {
305 rc = USB_STOR_TRANSPORT_ERROR;
309 info->sectors = ((u32)(reply[117]) << 24) |
310 ((u32)(reply[116]) << 16) |
311 ((u32)(reply[115]) << 8) |
312 ((u32)(reply[114]) );
314 rc = USB_STOR_TRANSPORT_GOOD;
321 static int jumpshot_handle_mode_sense(struct us_data *us,
325 static unsigned char rw_err_page[12] = {
326 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
328 static unsigned char cache_page[12] = {
329 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
331 static unsigned char rbac_page[12] = {
332 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
334 static unsigned char timer_page[8] = {
335 0x1C, 0x6, 0, 0, 0, 0
337 unsigned char pc, page_code;
339 struct jumpshot_info *info = (struct jumpshot_info *) (us->extra);
340 unsigned char *ptr = us->iobuf;
342 pc = srb->cmnd[2] >> 6;
343 page_code = srb->cmnd[2] & 0x3F;
347 US_DEBUGP("jumpshot_handle_mode_sense: Current values\n");
350 US_DEBUGP("jumpshot_handle_mode_sense: Changeable values\n");
353 US_DEBUGP("jumpshot_handle_mode_sense: Default values\n");
356 US_DEBUGP("jumpshot_handle_mode_sense: Saves values\n");
362 ptr[2] = 0x00; // WP enable: 0x80
365 ptr[3] = 0x00; // WP enable: 0x80
371 // vendor-specific mode
372 info->sense_key = 0x05;
373 info->sense_asc = 0x24;
374 info->sense_ascq = 0x00;
375 return USB_STOR_TRANSPORT_FAILED;
378 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
379 i += sizeof(rw_err_page);
383 memcpy(ptr + i, cache_page, sizeof(cache_page));
384 i += sizeof(cache_page);
388 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
389 i += sizeof(rbac_page);
393 memcpy(ptr + i, timer_page, sizeof(timer_page));
394 i += sizeof(timer_page);
398 memcpy(ptr + i, timer_page, sizeof(timer_page));
399 i += sizeof(timer_page);
400 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
401 i += sizeof(rbac_page);
402 memcpy(ptr + i, cache_page, sizeof(cache_page));
403 i += sizeof(cache_page);
404 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
405 i += sizeof(rw_err_page);
412 ((u16 *) ptr)[0] = cpu_to_be16(i - 2);
413 usb_stor_set_xfer_buf(ptr, i, srb);
415 return USB_STOR_TRANSPORT_GOOD;
419 static void jumpshot_info_destructor(void *extra)
421 // this routine is a placeholder...
422 // currently, we don't allocate any extra blocks so we're okay
427 // Transport for the Lexar 'Jumpshot'
429 int jumpshot_transport(Scsi_Cmnd * srb, struct us_data *us)
431 struct jumpshot_info *info;
433 unsigned long block, blocks;
434 unsigned char *ptr = us->iobuf;
435 static unsigned char inquiry_response[8] = {
436 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
440 us->extra = kmalloc(sizeof(struct jumpshot_info), GFP_NOIO);
442 US_DEBUGP("jumpshot_transport: Gah! Can't allocate storage for jumpshot info struct!\n");
443 return USB_STOR_TRANSPORT_ERROR;
445 memset(us->extra, 0, sizeof(struct jumpshot_info));
446 us->extra_destructor = jumpshot_info_destructor;
449 info = (struct jumpshot_info *) (us->extra);
451 if (srb->cmnd[0] == INQUIRY) {
452 US_DEBUGP("jumpshot_transport: INQUIRY. Returning bogus response.\n");
453 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
454 fill_inquiry_response(us, ptr, 36);
455 return USB_STOR_TRANSPORT_GOOD;
458 if (srb->cmnd[0] == READ_CAPACITY) {
459 info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
461 rc = jumpshot_get_status(us);
462 if (rc != USB_STOR_TRANSPORT_GOOD)
465 rc = jumpshot_id_device(us, info);
466 if (rc != USB_STOR_TRANSPORT_GOOD)
469 US_DEBUGP("jumpshot_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
470 info->sectors, info->ssize);
474 ((u32 *) ptr)[0] = cpu_to_be32(info->sectors);
475 ((u32 *) ptr)[1] = cpu_to_be32(info->ssize);
476 usb_stor_set_xfer_buf(ptr, 8, srb);
478 return USB_STOR_TRANSPORT_GOOD;
481 if (srb->cmnd[0] == MODE_SELECT_10) {
482 US_DEBUGP("jumpshot_transport: Gah! MODE_SELECT_10.\n");
483 return USB_STOR_TRANSPORT_ERROR;
486 if (srb->cmnd[0] == READ_10) {
487 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
488 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
490 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
492 US_DEBUGP("jumpshot_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
493 return jumpshot_read_data(us, info, block, blocks);
496 if (srb->cmnd[0] == READ_12) {
497 // I don't think we'll ever see a READ_12 but support it anyway...
499 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
500 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
502 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
503 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
505 US_DEBUGP("jumpshot_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
506 return jumpshot_read_data(us, info, block, blocks);
509 if (srb->cmnd[0] == WRITE_10) {
510 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
511 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
513 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
515 US_DEBUGP("jumpshot_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
516 return jumpshot_write_data(us, info, block, blocks);
519 if (srb->cmnd[0] == WRITE_12) {
520 // I don't think we'll ever see a WRITE_12 but support it anyway...
522 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
523 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
525 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
526 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
528 US_DEBUGP("jumpshot_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
529 return jumpshot_write_data(us, info, block, blocks);
533 if (srb->cmnd[0] == TEST_UNIT_READY) {
534 US_DEBUGP("jumpshot_transport: TEST_UNIT_READY.\n");
535 return jumpshot_get_status(us);
538 if (srb->cmnd[0] == REQUEST_SENSE) {
539 US_DEBUGP("jumpshot_transport: REQUEST_SENSE.\n");
543 ptr[2] = info->sense_key;
545 ptr[12] = info->sense_asc;
546 ptr[13] = info->sense_ascq;
547 usb_stor_set_xfer_buf(ptr, 18, srb);
549 return USB_STOR_TRANSPORT_GOOD;
552 if (srb->cmnd[0] == MODE_SENSE) {
553 US_DEBUGP("jumpshot_transport: MODE_SENSE_6 detected\n");
554 return jumpshot_handle_mode_sense(us, srb, TRUE);
557 if (srb->cmnd[0] == MODE_SENSE_10) {
558 US_DEBUGP("jumpshot_transport: MODE_SENSE_10 detected\n");
559 return jumpshot_handle_mode_sense(us, srb, FALSE);
562 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
563 // sure. whatever. not like we can stop the user from popping
564 // the media out of the device (no locking doors, etc)
566 return USB_STOR_TRANSPORT_GOOD;
569 if (srb->cmnd[0] == START_STOP) {
570 /* this is used by sd.c'check_scsidisk_media_change to detect
572 US_DEBUGP("jumpshot_transport: START_STOP.\n");
573 /* the first jumpshot_id_device after a media change returns
574 an error (determined experimentally) */
575 rc = jumpshot_id_device(us, info);
576 if (rc == USB_STOR_TRANSPORT_GOOD) {
577 info->sense_key = NO_SENSE;
578 srb->result = SUCCESS;
580 info->sense_key = UNIT_ATTENTION;
581 srb->result = SAM_STAT_CHECK_CONDITION;
586 US_DEBUGP("jumpshot_transport: Gah! Unknown command: %d (0x%x)\n",
587 srb->cmnd[0], srb->cmnd[0]);
588 info->sense_key = 0x05;
589 info->sense_asc = 0x20;
590 info->sense_ascq = 0x00;
591 return USB_STOR_TRANSPORT_FAILED;