1 /* Driver for Datafab USB Compact Flash reader
7 * Current development and maintenance by:
8 * (c) 2000 Jimmie Mayfield (mayfield+datafab@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)
19 * (c) 2002 Alan Stern <stern@rowland.org>
21 * This program is free software; you can redistribute it and/or modify it
22 * under the terms of the GNU General Public License as published by the
23 * Free Software Foundation; either version 2, or (at your option) any
26 * This program is distributed in the hope that it will be useful, but
27 * WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * General Public License for more details.
31 * You should have received a copy of the GNU General Public License along
32 * with this program; if not, write to the Free Software Foundation, Inc.,
33 * 675 Mass Ave, Cambridge, MA 02139, USA.
37 * This driver attempts to support USB CompactFlash reader/writer devices
38 * based on Datafab USB-to-ATA chips. It was specifically developed for the
39 * Datafab MDCFE-B USB CompactFlash reader but has since been found to work
40 * with a variety of Datafab-based devices from a number of manufacturers.
41 * I've received a report of this driver working with a Datafab-based
42 * SmartMedia device though please be aware that I'm personally unable to
43 * test SmartMedia support.
45 * This driver supports reading and writing. If you're truly paranoid,
46 * however, you can force the driver into a write-protected state by setting
47 * the WP enable bits in datafab_handle_mode_sense(). See the comments
51 #include <linux/errno.h>
52 #include <linux/slab.h>
54 #include <scsi/scsi.h>
55 #include <scsi/scsi_cmnd.h>
58 #include "transport.h"
63 static int datafab_determine_lun(struct us_data *us,
64 struct datafab_info *info);
68 datafab_bulk_read(struct us_data *us, unsigned char *data, unsigned int len) {
70 return USB_STOR_XFER_GOOD;
72 US_DEBUGP("datafab_bulk_read: len = %d\n", len);
73 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
79 datafab_bulk_write(struct us_data *us, unsigned char *data, unsigned int len) {
81 return USB_STOR_XFER_GOOD;
83 US_DEBUGP("datafab_bulk_write: len = %d\n", len);
84 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
89 static int datafab_read_data(struct us_data *us,
90 struct datafab_info *info,
94 unsigned char *command = us->iobuf;
95 unsigned char *buffer;
96 unsigned char thistime;
97 unsigned int totallen, alloclen;
99 unsigned int sg_offset = 0;
100 struct scatterlist *sg = NULL;
102 // we're working in LBA mode. according to the ATA spec,
103 // we can support up to 28-bit addressing. I don't know if Datafab
104 // supports beyond 24-bit addressing. It's kind of hard to test
105 // since it requires > 8GB CF card.
107 if (sectors > 0x0FFFFFFF)
108 return USB_STOR_TRANSPORT_ERROR;
110 if (info->lun == -1) {
111 result = datafab_determine_lun(us, info);
112 if (result != USB_STOR_TRANSPORT_GOOD)
116 totallen = sectors * info->ssize;
118 // Since we don't read more than 64 KB at a time, we have to create
119 // a bounce buffer and move the data a piece at a time between the
120 // bounce buffer and the actual transfer buffer.
122 alloclen = min(totallen, 65536u);
123 buffer = kmalloc(alloclen, GFP_NOIO);
125 return USB_STOR_TRANSPORT_ERROR;
128 // loop, never allocate or transfer more than 64k at once
129 // (min(128k, 255*info->ssize) is the real limit)
131 len = min(totallen, alloclen);
132 thistime = (len / info->ssize) & 0xff;
135 command[1] = thistime;
136 command[2] = sector & 0xFF;
137 command[3] = (sector >> 8) & 0xFF;
138 command[4] = (sector >> 16) & 0xFF;
140 command[5] = 0xE0 + (info->lun << 4);
141 command[5] |= (sector >> 24) & 0x0F;
145 // send the read command
146 result = datafab_bulk_write(us, command, 8);
147 if (result != USB_STOR_XFER_GOOD)
151 result = datafab_bulk_read(us, buffer, len);
152 if (result != USB_STOR_XFER_GOOD)
155 // Store the data in the transfer buffer
156 usb_stor_access_xfer_buf(buffer, len, us->srb,
157 &sg, &sg_offset, TO_XFER_BUF);
161 } while (totallen > 0);
164 return USB_STOR_TRANSPORT_GOOD;
168 return USB_STOR_TRANSPORT_ERROR;
172 static int datafab_write_data(struct us_data *us,
173 struct datafab_info *info,
177 unsigned char *command = us->iobuf;
178 unsigned char *reply = us->iobuf;
179 unsigned char *buffer;
180 unsigned char thistime;
181 unsigned int totallen, alloclen;
183 unsigned int sg_offset = 0;
184 struct scatterlist *sg = NULL;
186 // we're working in LBA mode. according to the ATA spec,
187 // we can support up to 28-bit addressing. I don't know if Datafab
188 // supports beyond 24-bit addressing. It's kind of hard to test
189 // since it requires > 8GB CF card.
191 if (sectors > 0x0FFFFFFF)
192 return USB_STOR_TRANSPORT_ERROR;
194 if (info->lun == -1) {
195 result = datafab_determine_lun(us, info);
196 if (result != USB_STOR_TRANSPORT_GOOD)
200 totallen = sectors * info->ssize;
202 // Since we don't write more than 64 KB at a time, we have to create
203 // a bounce buffer and move the data a piece at a time between the
204 // bounce buffer and the actual transfer buffer.
206 alloclen = min(totallen, 65536u);
207 buffer = kmalloc(alloclen, GFP_NOIO);
209 return USB_STOR_TRANSPORT_ERROR;
212 // loop, never allocate or transfer more than 64k at once
213 // (min(128k, 255*info->ssize) is the real limit)
215 len = min(totallen, alloclen);
216 thistime = (len / info->ssize) & 0xff;
218 // Get the data from the transfer buffer
219 usb_stor_access_xfer_buf(buffer, len, us->srb,
220 &sg, &sg_offset, FROM_XFER_BUF);
223 command[1] = thistime;
224 command[2] = sector & 0xFF;
225 command[3] = (sector >> 8) & 0xFF;
226 command[4] = (sector >> 16) & 0xFF;
228 command[5] = 0xE0 + (info->lun << 4);
229 command[5] |= (sector >> 24) & 0x0F;
234 result = datafab_bulk_write(us, command, 8);
235 if (result != USB_STOR_XFER_GOOD)
239 result = datafab_bulk_write(us, buffer, len);
240 if (result != USB_STOR_XFER_GOOD)
244 result = datafab_bulk_read(us, reply, 2);
245 if (result != USB_STOR_XFER_GOOD)
248 if (reply[0] != 0x50 && reply[1] != 0) {
249 US_DEBUGP("datafab_write_data: Gah! "
250 "write return code: %02x %02x\n",
252 result = USB_STOR_TRANSPORT_ERROR;
258 } while (totallen > 0);
261 return USB_STOR_TRANSPORT_GOOD;
265 return USB_STOR_TRANSPORT_ERROR;
269 static int datafab_determine_lun(struct us_data *us,
270 struct datafab_info *info)
272 // Dual-slot readers can be thought of as dual-LUN devices.
273 // We need to determine which card slot is being used.
274 // We'll send an IDENTIFY DEVICE command and see which LUN responds...
276 // There might be a better way of doing this?
278 static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
279 unsigned char *command = us->iobuf;
284 return USB_STOR_TRANSPORT_ERROR;
286 memcpy(command, scommand, 8);
287 buf = kmalloc(512, GFP_NOIO);
289 return USB_STOR_TRANSPORT_ERROR;
291 US_DEBUGP("datafab_determine_lun: locating...\n");
293 // we'll try 3 times before giving up...
295 while (count++ < 3) {
298 rc = datafab_bulk_write(us, command, 8);
299 if (rc != USB_STOR_XFER_GOOD) {
300 rc = USB_STOR_TRANSPORT_ERROR;
304 rc = datafab_bulk_read(us, buf, 512);
305 if (rc == USB_STOR_XFER_GOOD) {
307 rc = USB_STOR_TRANSPORT_GOOD;
313 rc = datafab_bulk_write(us, command, 8);
314 if (rc != USB_STOR_XFER_GOOD) {
315 rc = USB_STOR_TRANSPORT_ERROR;
319 rc = datafab_bulk_read(us, buf, 512);
320 if (rc == USB_STOR_XFER_GOOD) {
322 rc = USB_STOR_TRANSPORT_GOOD;
329 rc = USB_STOR_TRANSPORT_ERROR;
336 static int datafab_id_device(struct us_data *us,
337 struct datafab_info *info)
339 // this is a variation of the ATA "IDENTIFY DEVICE" command...according
340 // to the ATA spec, 'Sector Count' isn't used but the Windows driver
341 // sets this bit so we do too...
343 static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
344 unsigned char *command = us->iobuf;
345 unsigned char *reply;
349 return USB_STOR_TRANSPORT_ERROR;
351 if (info->lun == -1) {
352 rc = datafab_determine_lun(us, info);
353 if (rc != USB_STOR_TRANSPORT_GOOD)
357 memcpy(command, scommand, 8);
358 reply = kmalloc(512, GFP_NOIO);
360 return USB_STOR_TRANSPORT_ERROR;
362 command[5] += (info->lun << 4);
364 rc = datafab_bulk_write(us, command, 8);
365 if (rc != USB_STOR_XFER_GOOD) {
366 rc = USB_STOR_TRANSPORT_ERROR;
370 // we'll go ahead and extract the media capacity while we're here...
372 rc = datafab_bulk_read(us, reply, 512);
373 if (rc == USB_STOR_XFER_GOOD) {
374 // capacity is at word offset 57-58
376 info->sectors = ((u32)(reply[117]) << 24) |
377 ((u32)(reply[116]) << 16) |
378 ((u32)(reply[115]) << 8) |
379 ((u32)(reply[114]) );
380 rc = USB_STOR_TRANSPORT_GOOD;
384 rc = USB_STOR_TRANSPORT_ERROR;
392 static int datafab_handle_mode_sense(struct us_data *us,
393 struct scsi_cmnd * srb,
396 static unsigned char rw_err_page[12] = {
397 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
399 static unsigned char cache_page[12] = {
400 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
402 static unsigned char rbac_page[12] = {
403 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
405 static unsigned char timer_page[8] = {
406 0x1C, 0x6, 0, 0, 0, 0
408 unsigned char pc, page_code;
410 struct datafab_info *info = (struct datafab_info *) (us->extra);
411 unsigned char *ptr = us->iobuf;
413 // most of this stuff is just a hack to get things working. the
414 // datafab reader doesn't present a SCSI interface so we
415 // fudge the SCSI commands...
418 pc = srb->cmnd[2] >> 6;
419 page_code = srb->cmnd[2] & 0x3F;
423 US_DEBUGP("datafab_handle_mode_sense: Current values\n");
426 US_DEBUGP("datafab_handle_mode_sense: Changeable values\n");
429 US_DEBUGP("datafab_handle_mode_sense: Default values\n");
432 US_DEBUGP("datafab_handle_mode_sense: Saves values\n");
438 ptr[2] = 0x00; // WP enable: 0x80
441 ptr[3] = 0x00; // WP enable: 0x80
447 // vendor-specific mode
448 info->sense_key = 0x05;
449 info->sense_asc = 0x24;
450 info->sense_ascq = 0x00;
451 return USB_STOR_TRANSPORT_FAILED;
454 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
455 i += sizeof(rw_err_page);
459 memcpy(ptr + i, cache_page, sizeof(cache_page));
460 i += sizeof(cache_page);
464 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
465 i += sizeof(rbac_page);
469 memcpy(ptr + i, timer_page, sizeof(timer_page));
470 i += sizeof(timer_page);
473 case 0x3F: // retrieve all pages
474 memcpy(ptr + i, timer_page, sizeof(timer_page));
475 i += sizeof(timer_page);
476 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
477 i += sizeof(rbac_page);
478 memcpy(ptr + i, cache_page, sizeof(cache_page));
479 i += sizeof(cache_page);
480 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
481 i += sizeof(rw_err_page);
488 ((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
489 usb_stor_set_xfer_buf(ptr, i, srb);
491 return USB_STOR_TRANSPORT_GOOD;
494 static void datafab_info_destructor(void *extra)
496 // this routine is a placeholder...
497 // currently, we don't allocate any extra memory so we're okay
501 // Transport for the Datafab MDCFE-B
503 int datafab_transport(struct scsi_cmnd * srb, struct us_data *us)
505 struct datafab_info *info;
507 unsigned long block, blocks;
508 unsigned char *ptr = us->iobuf;
509 static unsigned char inquiry_reply[8] = {
510 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
514 us->extra = kzalloc(sizeof(struct datafab_info), GFP_NOIO);
516 US_DEBUGP("datafab_transport: Gah! "
517 "Can't allocate storage for Datafab info struct!\n");
518 return USB_STOR_TRANSPORT_ERROR;
520 us->extra_destructor = datafab_info_destructor;
521 ((struct datafab_info *)us->extra)->lun = -1;
524 info = (struct datafab_info *) (us->extra);
526 if (srb->cmnd[0] == INQUIRY) {
527 US_DEBUGP("datafab_transport: INQUIRY. Returning bogus response");
528 memcpy(ptr, inquiry_reply, sizeof(inquiry_reply));
529 fill_inquiry_response(us, ptr, 36);
530 return USB_STOR_TRANSPORT_GOOD;
533 if (srb->cmnd[0] == READ_CAPACITY) {
534 info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
535 rc = datafab_id_device(us, info);
536 if (rc != USB_STOR_TRANSPORT_GOOD)
539 US_DEBUGP("datafab_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
540 info->sectors, info->ssize);
543 // we need the last sector, not the number of sectors
544 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
545 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
546 usb_stor_set_xfer_buf(ptr, 8, srb);
548 return USB_STOR_TRANSPORT_GOOD;
551 if (srb->cmnd[0] == MODE_SELECT_10) {
552 US_DEBUGP("datafab_transport: Gah! MODE_SELECT_10.\n");
553 return USB_STOR_TRANSPORT_ERROR;
556 // don't bother implementing READ_6 or WRITE_6.
558 if (srb->cmnd[0] == READ_10) {
559 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
560 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
562 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
564 US_DEBUGP("datafab_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
565 return datafab_read_data(us, info, block, blocks);
568 if (srb->cmnd[0] == READ_12) {
569 // we'll probably never see a READ_12 but we'll do it anyway...
571 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
572 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
574 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
575 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
577 US_DEBUGP("datafab_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
578 return datafab_read_data(us, info, block, blocks);
581 if (srb->cmnd[0] == WRITE_10) {
582 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
583 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
585 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
587 US_DEBUGP("datafab_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
588 return datafab_write_data(us, info, block, blocks);
591 if (srb->cmnd[0] == WRITE_12) {
592 // we'll probably never see a WRITE_12 but we'll do it anyway...
594 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
595 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
597 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
598 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
600 US_DEBUGP("datafab_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
601 return datafab_write_data(us, info, block, blocks);
604 if (srb->cmnd[0] == TEST_UNIT_READY) {
605 US_DEBUGP("datafab_transport: TEST_UNIT_READY.\n");
606 return datafab_id_device(us, info);
609 if (srb->cmnd[0] == REQUEST_SENSE) {
610 US_DEBUGP("datafab_transport: REQUEST_SENSE. Returning faked response\n");
612 // this response is pretty bogus right now. eventually if necessary
613 // we can set the correct sense data. so far though it hasn't been
618 ptr[2] = info->sense_key;
620 ptr[12] = info->sense_asc;
621 ptr[13] = info->sense_ascq;
622 usb_stor_set_xfer_buf(ptr, 18, srb);
624 return USB_STOR_TRANSPORT_GOOD;
627 if (srb->cmnd[0] == MODE_SENSE) {
628 US_DEBUGP("datafab_transport: MODE_SENSE_6 detected\n");
629 return datafab_handle_mode_sense(us, srb, 1);
632 if (srb->cmnd[0] == MODE_SENSE_10) {
633 US_DEBUGP("datafab_transport: MODE_SENSE_10 detected\n");
634 return datafab_handle_mode_sense(us, srb, 0);
637 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
638 // sure. whatever. not like we can stop the user from
639 // popping the media out of the device (no locking doors, etc)
641 return USB_STOR_TRANSPORT_GOOD;
644 if (srb->cmnd[0] == START_STOP) {
645 /* this is used by sd.c'check_scsidisk_media_change to detect
647 US_DEBUGP("datafab_transport: START_STOP.\n");
648 /* the first datafab_id_device after a media change returns
649 an error (determined experimentally) */
650 rc = datafab_id_device(us, info);
651 if (rc == USB_STOR_TRANSPORT_GOOD) {
652 info->sense_key = NO_SENSE;
653 srb->result = SUCCESS;
655 info->sense_key = UNIT_ATTENTION;
656 srb->result = SAM_STAT_CHECK_CONDITION;
661 US_DEBUGP("datafab_transport: Gah! Unknown command: %d (0x%x)\n",
662 srb->cmnd[0], srb->cmnd[0]);
663 info->sense_key = 0x05;
664 info->sense_asc = 0x20;
665 info->sense_ascq = 0x00;
666 return USB_STOR_TRANSPORT_FAILED;