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/module.h>
53 #include <linux/slab.h>
55 #include <scsi/scsi.h>
56 #include <scsi/scsi_cmnd.h>
59 #include "transport.h"
64 unsigned long sectors; /* total sector count */
65 unsigned long ssize; /* sector size in bytes */
66 signed char lun; /* used for dual-slot readers */
68 /* the following aren't used yet */
69 unsigned char sense_key;
70 unsigned long sense_asc; /* additional sense code */
71 unsigned long sense_ascq; /* additional sense code qualifier */
74 static int datafab_determine_lun(struct us_data *us,
75 struct datafab_info *info);
79 * The table of devices
81 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
82 vendorName, productName, useProtocol, useTransport, \
83 initFunction, flags) \
84 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
85 .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
87 struct usb_device_id datafab_usb_ids[] = {
88 # include "unusual_datafab.h"
89 { } /* Terminating entry */
91 MODULE_DEVICE_TABLE(usb, datafab_usb_ids);
98 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
99 vendor_name, product_name, use_protocol, use_transport, \
100 init_function, Flags) \
102 .vendorName = vendor_name, \
103 .productName = product_name, \
104 .useProtocol = use_protocol, \
105 .useTransport = use_transport, \
106 .initFunction = init_function, \
109 static struct us_unusual_dev datafab_unusual_dev_list[] = {
110 # include "unusual_datafab.h"
111 { } /* Terminating entry */
118 datafab_bulk_read(struct us_data *us, unsigned char *data, unsigned int len) {
120 return USB_STOR_XFER_GOOD;
122 US_DEBUGP("datafab_bulk_read: len = %d\n", len);
123 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
129 datafab_bulk_write(struct us_data *us, unsigned char *data, unsigned int len) {
131 return USB_STOR_XFER_GOOD;
133 US_DEBUGP("datafab_bulk_write: len = %d\n", len);
134 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
139 static int datafab_read_data(struct us_data *us,
140 struct datafab_info *info,
144 unsigned char *command = us->iobuf;
145 unsigned char *buffer;
146 unsigned char thistime;
147 unsigned int totallen, alloclen;
149 unsigned int sg_offset = 0;
150 struct scatterlist *sg = NULL;
152 // we're working in LBA mode. according to the ATA spec,
153 // we can support up to 28-bit addressing. I don't know if Datafab
154 // supports beyond 24-bit addressing. It's kind of hard to test
155 // since it requires > 8GB CF card.
157 if (sectors > 0x0FFFFFFF)
158 return USB_STOR_TRANSPORT_ERROR;
160 if (info->lun == -1) {
161 result = datafab_determine_lun(us, info);
162 if (result != USB_STOR_TRANSPORT_GOOD)
166 totallen = sectors * info->ssize;
168 // Since we don't read more than 64 KB at a time, we have to create
169 // a bounce buffer and move the data a piece at a time between the
170 // bounce buffer and the actual transfer buffer.
172 alloclen = min(totallen, 65536u);
173 buffer = kmalloc(alloclen, GFP_NOIO);
175 return USB_STOR_TRANSPORT_ERROR;
178 // loop, never allocate or transfer more than 64k at once
179 // (min(128k, 255*info->ssize) is the real limit)
181 len = min(totallen, alloclen);
182 thistime = (len / info->ssize) & 0xff;
185 command[1] = thistime;
186 command[2] = sector & 0xFF;
187 command[3] = (sector >> 8) & 0xFF;
188 command[4] = (sector >> 16) & 0xFF;
190 command[5] = 0xE0 + (info->lun << 4);
191 command[5] |= (sector >> 24) & 0x0F;
195 // send the read command
196 result = datafab_bulk_write(us, command, 8);
197 if (result != USB_STOR_XFER_GOOD)
201 result = datafab_bulk_read(us, buffer, len);
202 if (result != USB_STOR_XFER_GOOD)
205 // Store the data in the transfer buffer
206 usb_stor_access_xfer_buf(buffer, len, us->srb,
207 &sg, &sg_offset, TO_XFER_BUF);
211 } while (totallen > 0);
214 return USB_STOR_TRANSPORT_GOOD;
218 return USB_STOR_TRANSPORT_ERROR;
222 static int datafab_write_data(struct us_data *us,
223 struct datafab_info *info,
227 unsigned char *command = us->iobuf;
228 unsigned char *reply = us->iobuf;
229 unsigned char *buffer;
230 unsigned char thistime;
231 unsigned int totallen, alloclen;
233 unsigned int sg_offset = 0;
234 struct scatterlist *sg = NULL;
236 // we're working in LBA mode. according to the ATA spec,
237 // we can support up to 28-bit addressing. I don't know if Datafab
238 // supports beyond 24-bit addressing. It's kind of hard to test
239 // since it requires > 8GB CF card.
241 if (sectors > 0x0FFFFFFF)
242 return USB_STOR_TRANSPORT_ERROR;
244 if (info->lun == -1) {
245 result = datafab_determine_lun(us, info);
246 if (result != USB_STOR_TRANSPORT_GOOD)
250 totallen = sectors * info->ssize;
252 // Since we don't write more than 64 KB at a time, we have to create
253 // a bounce buffer and move the data a piece at a time between the
254 // bounce buffer and the actual transfer buffer.
256 alloclen = min(totallen, 65536u);
257 buffer = kmalloc(alloclen, GFP_NOIO);
259 return USB_STOR_TRANSPORT_ERROR;
262 // loop, never allocate or transfer more than 64k at once
263 // (min(128k, 255*info->ssize) is the real limit)
265 len = min(totallen, alloclen);
266 thistime = (len / info->ssize) & 0xff;
268 // Get the data from the transfer buffer
269 usb_stor_access_xfer_buf(buffer, len, us->srb,
270 &sg, &sg_offset, FROM_XFER_BUF);
273 command[1] = thistime;
274 command[2] = sector & 0xFF;
275 command[3] = (sector >> 8) & 0xFF;
276 command[4] = (sector >> 16) & 0xFF;
278 command[5] = 0xE0 + (info->lun << 4);
279 command[5] |= (sector >> 24) & 0x0F;
284 result = datafab_bulk_write(us, command, 8);
285 if (result != USB_STOR_XFER_GOOD)
289 result = datafab_bulk_write(us, buffer, len);
290 if (result != USB_STOR_XFER_GOOD)
294 result = datafab_bulk_read(us, reply, 2);
295 if (result != USB_STOR_XFER_GOOD)
298 if (reply[0] != 0x50 && reply[1] != 0) {
299 US_DEBUGP("datafab_write_data: Gah! "
300 "write return code: %02x %02x\n",
302 result = USB_STOR_TRANSPORT_ERROR;
308 } while (totallen > 0);
311 return USB_STOR_TRANSPORT_GOOD;
315 return USB_STOR_TRANSPORT_ERROR;
319 static int datafab_determine_lun(struct us_data *us,
320 struct datafab_info *info)
322 // Dual-slot readers can be thought of as dual-LUN devices.
323 // We need to determine which card slot is being used.
324 // We'll send an IDENTIFY DEVICE command and see which LUN responds...
326 // There might be a better way of doing this?
328 static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
329 unsigned char *command = us->iobuf;
334 return USB_STOR_TRANSPORT_ERROR;
336 memcpy(command, scommand, 8);
337 buf = kmalloc(512, GFP_NOIO);
339 return USB_STOR_TRANSPORT_ERROR;
341 US_DEBUGP("datafab_determine_lun: locating...\n");
343 // we'll try 3 times before giving up...
345 while (count++ < 3) {
348 rc = datafab_bulk_write(us, command, 8);
349 if (rc != USB_STOR_XFER_GOOD) {
350 rc = USB_STOR_TRANSPORT_ERROR;
354 rc = datafab_bulk_read(us, buf, 512);
355 if (rc == USB_STOR_XFER_GOOD) {
357 rc = USB_STOR_TRANSPORT_GOOD;
363 rc = datafab_bulk_write(us, command, 8);
364 if (rc != USB_STOR_XFER_GOOD) {
365 rc = USB_STOR_TRANSPORT_ERROR;
369 rc = datafab_bulk_read(us, buf, 512);
370 if (rc == USB_STOR_XFER_GOOD) {
372 rc = USB_STOR_TRANSPORT_GOOD;
379 rc = USB_STOR_TRANSPORT_ERROR;
386 static int datafab_id_device(struct us_data *us,
387 struct datafab_info *info)
389 // this is a variation of the ATA "IDENTIFY DEVICE" command...according
390 // to the ATA spec, 'Sector Count' isn't used but the Windows driver
391 // sets this bit so we do too...
393 static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
394 unsigned char *command = us->iobuf;
395 unsigned char *reply;
399 return USB_STOR_TRANSPORT_ERROR;
401 if (info->lun == -1) {
402 rc = datafab_determine_lun(us, info);
403 if (rc != USB_STOR_TRANSPORT_GOOD)
407 memcpy(command, scommand, 8);
408 reply = kmalloc(512, GFP_NOIO);
410 return USB_STOR_TRANSPORT_ERROR;
412 command[5] += (info->lun << 4);
414 rc = datafab_bulk_write(us, command, 8);
415 if (rc != USB_STOR_XFER_GOOD) {
416 rc = USB_STOR_TRANSPORT_ERROR;
420 // we'll go ahead and extract the media capacity while we're here...
422 rc = datafab_bulk_read(us, reply, 512);
423 if (rc == USB_STOR_XFER_GOOD) {
424 // capacity is at word offset 57-58
426 info->sectors = ((u32)(reply[117]) << 24) |
427 ((u32)(reply[116]) << 16) |
428 ((u32)(reply[115]) << 8) |
429 ((u32)(reply[114]) );
430 rc = USB_STOR_TRANSPORT_GOOD;
434 rc = USB_STOR_TRANSPORT_ERROR;
442 static int datafab_handle_mode_sense(struct us_data *us,
443 struct scsi_cmnd * srb,
446 static unsigned char rw_err_page[12] = {
447 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
449 static unsigned char cache_page[12] = {
450 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
452 static unsigned char rbac_page[12] = {
453 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
455 static unsigned char timer_page[8] = {
456 0x1C, 0x6, 0, 0, 0, 0
458 unsigned char pc, page_code;
460 struct datafab_info *info = (struct datafab_info *) (us->extra);
461 unsigned char *ptr = us->iobuf;
463 // most of this stuff is just a hack to get things working. the
464 // datafab reader doesn't present a SCSI interface so we
465 // fudge the SCSI commands...
468 pc = srb->cmnd[2] >> 6;
469 page_code = srb->cmnd[2] & 0x3F;
473 US_DEBUGP("datafab_handle_mode_sense: Current values\n");
476 US_DEBUGP("datafab_handle_mode_sense: Changeable values\n");
479 US_DEBUGP("datafab_handle_mode_sense: Default values\n");
482 US_DEBUGP("datafab_handle_mode_sense: Saves values\n");
488 ptr[2] = 0x00; // WP enable: 0x80
491 ptr[3] = 0x00; // WP enable: 0x80
497 // vendor-specific mode
498 info->sense_key = 0x05;
499 info->sense_asc = 0x24;
500 info->sense_ascq = 0x00;
501 return USB_STOR_TRANSPORT_FAILED;
504 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
505 i += sizeof(rw_err_page);
509 memcpy(ptr + i, cache_page, sizeof(cache_page));
510 i += sizeof(cache_page);
514 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
515 i += sizeof(rbac_page);
519 memcpy(ptr + i, timer_page, sizeof(timer_page));
520 i += sizeof(timer_page);
523 case 0x3F: // retrieve all pages
524 memcpy(ptr + i, timer_page, sizeof(timer_page));
525 i += sizeof(timer_page);
526 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
527 i += sizeof(rbac_page);
528 memcpy(ptr + i, cache_page, sizeof(cache_page));
529 i += sizeof(cache_page);
530 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
531 i += sizeof(rw_err_page);
538 ((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
539 usb_stor_set_xfer_buf(ptr, i, srb);
541 return USB_STOR_TRANSPORT_GOOD;
544 static void datafab_info_destructor(void *extra)
546 // this routine is a placeholder...
547 // currently, we don't allocate any extra memory so we're okay
551 // Transport for the Datafab MDCFE-B
553 static int datafab_transport(struct scsi_cmnd *srb, struct us_data *us)
555 struct datafab_info *info;
557 unsigned long block, blocks;
558 unsigned char *ptr = us->iobuf;
559 static unsigned char inquiry_reply[8] = {
560 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
564 us->extra = kzalloc(sizeof(struct datafab_info), GFP_NOIO);
566 US_DEBUGP("datafab_transport: Gah! "
567 "Can't allocate storage for Datafab info struct!\n");
568 return USB_STOR_TRANSPORT_ERROR;
570 us->extra_destructor = datafab_info_destructor;
571 ((struct datafab_info *)us->extra)->lun = -1;
574 info = (struct datafab_info *) (us->extra);
576 if (srb->cmnd[0] == INQUIRY) {
577 US_DEBUGP("datafab_transport: INQUIRY. Returning bogus response");
578 memcpy(ptr, inquiry_reply, sizeof(inquiry_reply));
579 fill_inquiry_response(us, ptr, 36);
580 return USB_STOR_TRANSPORT_GOOD;
583 if (srb->cmnd[0] == READ_CAPACITY) {
584 info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
585 rc = datafab_id_device(us, info);
586 if (rc != USB_STOR_TRANSPORT_GOOD)
589 US_DEBUGP("datafab_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
590 info->sectors, info->ssize);
593 // we need the last sector, not the number of sectors
594 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
595 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
596 usb_stor_set_xfer_buf(ptr, 8, srb);
598 return USB_STOR_TRANSPORT_GOOD;
601 if (srb->cmnd[0] == MODE_SELECT_10) {
602 US_DEBUGP("datafab_transport: Gah! MODE_SELECT_10.\n");
603 return USB_STOR_TRANSPORT_ERROR;
606 // don't bother implementing READ_6 or WRITE_6.
608 if (srb->cmnd[0] == READ_10) {
609 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
610 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
612 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
614 US_DEBUGP("datafab_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
615 return datafab_read_data(us, info, block, blocks);
618 if (srb->cmnd[0] == READ_12) {
619 // we'll probably never see a READ_12 but we'll do it anyway...
621 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
622 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
624 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
625 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
627 US_DEBUGP("datafab_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
628 return datafab_read_data(us, info, block, blocks);
631 if (srb->cmnd[0] == WRITE_10) {
632 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
633 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
635 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
637 US_DEBUGP("datafab_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
638 return datafab_write_data(us, info, block, blocks);
641 if (srb->cmnd[0] == WRITE_12) {
642 // we'll probably never see a WRITE_12 but we'll do it anyway...
644 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
645 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
647 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
648 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
650 US_DEBUGP("datafab_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
651 return datafab_write_data(us, info, block, blocks);
654 if (srb->cmnd[0] == TEST_UNIT_READY) {
655 US_DEBUGP("datafab_transport: TEST_UNIT_READY.\n");
656 return datafab_id_device(us, info);
659 if (srb->cmnd[0] == REQUEST_SENSE) {
660 US_DEBUGP("datafab_transport: REQUEST_SENSE. Returning faked response\n");
662 // this response is pretty bogus right now. eventually if necessary
663 // we can set the correct sense data. so far though it hasn't been
668 ptr[2] = info->sense_key;
670 ptr[12] = info->sense_asc;
671 ptr[13] = info->sense_ascq;
672 usb_stor_set_xfer_buf(ptr, 18, srb);
674 return USB_STOR_TRANSPORT_GOOD;
677 if (srb->cmnd[0] == MODE_SENSE) {
678 US_DEBUGP("datafab_transport: MODE_SENSE_6 detected\n");
679 return datafab_handle_mode_sense(us, srb, 1);
682 if (srb->cmnd[0] == MODE_SENSE_10) {
683 US_DEBUGP("datafab_transport: MODE_SENSE_10 detected\n");
684 return datafab_handle_mode_sense(us, srb, 0);
687 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
688 // sure. whatever. not like we can stop the user from
689 // popping the media out of the device (no locking doors, etc)
691 return USB_STOR_TRANSPORT_GOOD;
694 if (srb->cmnd[0] == START_STOP) {
695 /* this is used by sd.c'check_scsidisk_media_change to detect
697 US_DEBUGP("datafab_transport: START_STOP.\n");
698 /* the first datafab_id_device after a media change returns
699 an error (determined experimentally) */
700 rc = datafab_id_device(us, info);
701 if (rc == USB_STOR_TRANSPORT_GOOD) {
702 info->sense_key = NO_SENSE;
703 srb->result = SUCCESS;
705 info->sense_key = UNIT_ATTENTION;
706 srb->result = SAM_STAT_CHECK_CONDITION;
711 US_DEBUGP("datafab_transport: Gah! Unknown command: %d (0x%x)\n",
712 srb->cmnd[0], srb->cmnd[0]);
713 info->sense_key = 0x05;
714 info->sense_asc = 0x20;
715 info->sense_ascq = 0x00;
716 return USB_STOR_TRANSPORT_FAILED;
719 static int datafab_probe(struct usb_interface *intf,
720 const struct usb_device_id *id)
725 result = usb_stor_probe1(&us, intf, id,
726 (id - datafab_usb_ids) + datafab_unusual_dev_list);
730 us->transport_name = "Datafab Bulk-Only";
731 us->transport = datafab_transport;
732 us->transport_reset = usb_stor_Bulk_reset;
735 result = usb_stor_probe2(us);
739 static struct usb_driver datafab_driver = {
740 .name = "ums-datafab",
741 .probe = datafab_probe,
742 .disconnect = usb_stor_disconnect,
743 .suspend = usb_stor_suspend,
744 .resume = usb_stor_resume,
745 .reset_resume = usb_stor_reset_resume,
746 .pre_reset = usb_stor_pre_reset,
747 .post_reset = usb_stor_post_reset,
748 .id_table = datafab_usb_ids,
752 static int __init datafab_init(void)
754 return usb_register(&datafab_driver);
757 static void __exit datafab_exit(void)
759 usb_deregister(&datafab_driver);
762 module_init(datafab_init);
763 module_exit(datafab_exit);