2 * Driver for Datafab USB Compact Flash reader
8 * Current development and maintenance by:
9 * (c) 2000 Jimmie Mayfield (mayfield+datafab@sackheads.org)
11 * Many thanks to Robert Baruch for the SanDisk SmartMedia reader driver
12 * which I used as a template for this driver.
14 * Some bugfixes and scatter-gather code by Gregory P. Smith
15 * (greg-usb@electricrain.com)
17 * Fix for media change by Joerg Schneider (js@joergschneider.com)
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 USB CompactFlash reader/writer devices
39 * based on Datafab USB-to-ATA chips. It was specifically developed for the
40 * Datafab MDCFE-B USB CompactFlash reader but has since been found to work
41 * with a variety of Datafab-based devices from a number of manufacturers.
42 * I've received a report of this driver working with a Datafab-based
43 * SmartMedia device though please be aware that I'm personally unable to
44 * test SmartMedia support.
46 * This driver supports reading and writing. If you're truly paranoid,
47 * however, you can force the driver into a write-protected state by setting
48 * the WP enable bits in datafab_handle_mode_sense(). See the comments
52 #include <linux/errno.h>
53 #include <linux/module.h>
54 #include <linux/slab.h>
56 #include <scsi/scsi.h>
57 #include <scsi/scsi_cmnd.h>
60 #include "transport.h"
65 #define DRV_NAME "ums-datafab"
67 MODULE_DESCRIPTION("Driver for Datafab USB Compact Flash reader");
68 MODULE_AUTHOR("Jimmie Mayfield <mayfield+datafab@sackheads.org>");
69 MODULE_LICENSE("GPL");
72 unsigned long sectors
; /* total sector count */
73 unsigned long ssize
; /* sector size in bytes */
74 signed char lun
; /* used for dual-slot readers */
76 /* the following aren't used yet */
77 unsigned char sense_key
;
78 unsigned long sense_asc
; /* additional sense code */
79 unsigned long sense_ascq
; /* additional sense code qualifier */
82 static int datafab_determine_lun(struct us_data
*us
,
83 struct datafab_info
*info
);
87 * The table of devices
89 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
90 vendorName, productName, useProtocol, useTransport, \
91 initFunction, flags) \
92 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
93 .driver_info = (flags) }
95 static struct usb_device_id datafab_usb_ids
[] = {
96 # include "unusual_datafab.h"
97 { } /* Terminating entry */
99 MODULE_DEVICE_TABLE(usb
, datafab_usb_ids
);
106 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
107 vendor_name, product_name, use_protocol, use_transport, \
108 init_function, Flags) \
110 .vendorName = vendor_name, \
111 .productName = product_name, \
112 .useProtocol = use_protocol, \
113 .useTransport = use_transport, \
114 .initFunction = init_function, \
117 static struct us_unusual_dev datafab_unusual_dev_list
[] = {
118 # include "unusual_datafab.h"
119 { } /* Terminating entry */
126 datafab_bulk_read(struct us_data
*us
, unsigned char *data
, unsigned int len
) {
128 return USB_STOR_XFER_GOOD
;
130 usb_stor_dbg(us
, "len = %d\n", len
);
131 return usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
137 datafab_bulk_write(struct us_data
*us
, unsigned char *data
, unsigned int len
) {
139 return USB_STOR_XFER_GOOD
;
141 usb_stor_dbg(us
, "len = %d\n", len
);
142 return usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
147 static int datafab_read_data(struct us_data
*us
,
148 struct datafab_info
*info
,
152 unsigned char *command
= us
->iobuf
;
153 unsigned char *buffer
;
154 unsigned char thistime
;
155 unsigned int totallen
, alloclen
;
157 unsigned int sg_offset
= 0;
158 struct scatterlist
*sg
= NULL
;
160 // we're working in LBA mode. according to the ATA spec,
161 // we can support up to 28-bit addressing. I don't know if Datafab
162 // supports beyond 24-bit addressing. It's kind of hard to test
163 // since it requires > 8GB CF card.
165 if (sectors
> 0x0FFFFFFF)
166 return USB_STOR_TRANSPORT_ERROR
;
168 if (info
->lun
== -1) {
169 result
= datafab_determine_lun(us
, info
);
170 if (result
!= USB_STOR_TRANSPORT_GOOD
)
174 totallen
= sectors
* info
->ssize
;
176 // Since we don't read more than 64 KB at a time, we have to create
177 // a bounce buffer and move the data a piece at a time between the
178 // bounce buffer and the actual transfer buffer.
180 alloclen
= min(totallen
, 65536u);
181 buffer
= kmalloc(alloclen
, GFP_NOIO
);
183 return USB_STOR_TRANSPORT_ERROR
;
186 // loop, never allocate or transfer more than 64k at once
187 // (min(128k, 255*info->ssize) is the real limit)
189 len
= min(totallen
, alloclen
);
190 thistime
= (len
/ info
->ssize
) & 0xff;
193 command
[1] = thistime
;
194 command
[2] = sector
& 0xFF;
195 command
[3] = (sector
>> 8) & 0xFF;
196 command
[4] = (sector
>> 16) & 0xFF;
198 command
[5] = 0xE0 + (info
->lun
<< 4);
199 command
[5] |= (sector
>> 24) & 0x0F;
203 // send the read command
204 result
= datafab_bulk_write(us
, command
, 8);
205 if (result
!= USB_STOR_XFER_GOOD
)
209 result
= datafab_bulk_read(us
, buffer
, len
);
210 if (result
!= USB_STOR_XFER_GOOD
)
213 // Store the data in the transfer buffer
214 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
215 &sg
, &sg_offset
, TO_XFER_BUF
);
219 } while (totallen
> 0);
222 return USB_STOR_TRANSPORT_GOOD
;
226 return USB_STOR_TRANSPORT_ERROR
;
230 static int datafab_write_data(struct us_data
*us
,
231 struct datafab_info
*info
,
235 unsigned char *command
= us
->iobuf
;
236 unsigned char *reply
= us
->iobuf
;
237 unsigned char *buffer
;
238 unsigned char thistime
;
239 unsigned int totallen
, alloclen
;
241 unsigned int sg_offset
= 0;
242 struct scatterlist
*sg
= NULL
;
244 // we're working in LBA mode. according to the ATA spec,
245 // we can support up to 28-bit addressing. I don't know if Datafab
246 // supports beyond 24-bit addressing. It's kind of hard to test
247 // since it requires > 8GB CF card.
249 if (sectors
> 0x0FFFFFFF)
250 return USB_STOR_TRANSPORT_ERROR
;
252 if (info
->lun
== -1) {
253 result
= datafab_determine_lun(us
, info
);
254 if (result
!= USB_STOR_TRANSPORT_GOOD
)
258 totallen
= sectors
* info
->ssize
;
260 // Since we don't write more than 64 KB at a time, we have to create
261 // a bounce buffer and move the data a piece at a time between the
262 // bounce buffer and the actual transfer buffer.
264 alloclen
= min(totallen
, 65536u);
265 buffer
= kmalloc(alloclen
, GFP_NOIO
);
267 return USB_STOR_TRANSPORT_ERROR
;
270 // loop, never allocate or transfer more than 64k at once
271 // (min(128k, 255*info->ssize) is the real limit)
273 len
= min(totallen
, alloclen
);
274 thistime
= (len
/ info
->ssize
) & 0xff;
276 // Get the data from the transfer buffer
277 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
278 &sg
, &sg_offset
, FROM_XFER_BUF
);
281 command
[1] = thistime
;
282 command
[2] = sector
& 0xFF;
283 command
[3] = (sector
>> 8) & 0xFF;
284 command
[4] = (sector
>> 16) & 0xFF;
286 command
[5] = 0xE0 + (info
->lun
<< 4);
287 command
[5] |= (sector
>> 24) & 0x0F;
292 result
= datafab_bulk_write(us
, command
, 8);
293 if (result
!= USB_STOR_XFER_GOOD
)
297 result
= datafab_bulk_write(us
, buffer
, len
);
298 if (result
!= USB_STOR_XFER_GOOD
)
302 result
= datafab_bulk_read(us
, reply
, 2);
303 if (result
!= USB_STOR_XFER_GOOD
)
306 if (reply
[0] != 0x50 && reply
[1] != 0) {
307 usb_stor_dbg(us
, "Gah! write return code: %02x %02x\n",
309 result
= USB_STOR_TRANSPORT_ERROR
;
315 } while (totallen
> 0);
318 return USB_STOR_TRANSPORT_GOOD
;
322 return USB_STOR_TRANSPORT_ERROR
;
326 static int datafab_determine_lun(struct us_data
*us
,
327 struct datafab_info
*info
)
329 // Dual-slot readers can be thought of as dual-LUN devices.
330 // We need to determine which card slot is being used.
331 // We'll send an IDENTIFY DEVICE command and see which LUN responds...
333 // There might be a better way of doing this?
335 static unsigned char scommand
[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
336 unsigned char *command
= us
->iobuf
;
341 return USB_STOR_TRANSPORT_ERROR
;
343 memcpy(command
, scommand
, 8);
344 buf
= kmalloc(512, GFP_NOIO
);
346 return USB_STOR_TRANSPORT_ERROR
;
348 usb_stor_dbg(us
, "locating...\n");
350 // we'll try 3 times before giving up...
352 while (count
++ < 3) {
355 rc
= datafab_bulk_write(us
, command
, 8);
356 if (rc
!= USB_STOR_XFER_GOOD
) {
357 rc
= USB_STOR_TRANSPORT_ERROR
;
361 rc
= datafab_bulk_read(us
, buf
, 512);
362 if (rc
== USB_STOR_XFER_GOOD
) {
364 rc
= USB_STOR_TRANSPORT_GOOD
;
370 rc
= datafab_bulk_write(us
, command
, 8);
371 if (rc
!= USB_STOR_XFER_GOOD
) {
372 rc
= USB_STOR_TRANSPORT_ERROR
;
376 rc
= datafab_bulk_read(us
, buf
, 512);
377 if (rc
== USB_STOR_XFER_GOOD
) {
379 rc
= USB_STOR_TRANSPORT_GOOD
;
386 rc
= USB_STOR_TRANSPORT_ERROR
;
393 static int datafab_id_device(struct us_data
*us
,
394 struct datafab_info
*info
)
396 // this is a variation of the ATA "IDENTIFY DEVICE" command...according
397 // to the ATA spec, 'Sector Count' isn't used but the Windows driver
398 // sets this bit so we do too...
400 static unsigned char scommand
[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
401 unsigned char *command
= us
->iobuf
;
402 unsigned char *reply
;
406 return USB_STOR_TRANSPORT_ERROR
;
408 if (info
->lun
== -1) {
409 rc
= datafab_determine_lun(us
, info
);
410 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
414 memcpy(command
, scommand
, 8);
415 reply
= kmalloc(512, GFP_NOIO
);
417 return USB_STOR_TRANSPORT_ERROR
;
419 command
[5] += (info
->lun
<< 4);
421 rc
= datafab_bulk_write(us
, command
, 8);
422 if (rc
!= USB_STOR_XFER_GOOD
) {
423 rc
= USB_STOR_TRANSPORT_ERROR
;
427 // we'll go ahead and extract the media capacity while we're here...
429 rc
= datafab_bulk_read(us
, reply
, 512);
430 if (rc
== USB_STOR_XFER_GOOD
) {
431 // capacity is at word offset 57-58
433 info
->sectors
= ((u32
)(reply
[117]) << 24) |
434 ((u32
)(reply
[116]) << 16) |
435 ((u32
)(reply
[115]) << 8) |
436 ((u32
)(reply
[114]) );
437 rc
= USB_STOR_TRANSPORT_GOOD
;
441 rc
= USB_STOR_TRANSPORT_ERROR
;
449 static int datafab_handle_mode_sense(struct us_data
*us
,
450 struct scsi_cmnd
* srb
,
453 static unsigned char rw_err_page
[12] = {
454 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
456 static unsigned char cache_page
[12] = {
457 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
459 static unsigned char rbac_page
[12] = {
460 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
462 static unsigned char timer_page
[8] = {
463 0x1C, 0x6, 0, 0, 0, 0
465 unsigned char pc
, page_code
;
467 struct datafab_info
*info
= (struct datafab_info
*) (us
->extra
);
468 unsigned char *ptr
= us
->iobuf
;
470 // most of this stuff is just a hack to get things working. the
471 // datafab reader doesn't present a SCSI interface so we
472 // fudge the SCSI commands...
475 pc
= srb
->cmnd
[2] >> 6;
476 page_code
= srb
->cmnd
[2] & 0x3F;
480 usb_stor_dbg(us
, "Current values\n");
483 usb_stor_dbg(us
, "Changeable values\n");
486 usb_stor_dbg(us
, "Default values\n");
489 usb_stor_dbg(us
, "Saves values\n");
495 ptr
[2] = 0x00; // WP enable: 0x80
498 ptr
[3] = 0x00; // WP enable: 0x80
504 // vendor-specific mode
505 info
->sense_key
= 0x05;
506 info
->sense_asc
= 0x24;
507 info
->sense_ascq
= 0x00;
508 return USB_STOR_TRANSPORT_FAILED
;
511 memcpy(ptr
+ i
, rw_err_page
, sizeof(rw_err_page
));
512 i
+= sizeof(rw_err_page
);
516 memcpy(ptr
+ i
, cache_page
, sizeof(cache_page
));
517 i
+= sizeof(cache_page
);
521 memcpy(ptr
+ i
, rbac_page
, sizeof(rbac_page
));
522 i
+= sizeof(rbac_page
);
526 memcpy(ptr
+ i
, timer_page
, sizeof(timer_page
));
527 i
+= sizeof(timer_page
);
530 case 0x3F: // retrieve all pages
531 memcpy(ptr
+ i
, timer_page
, sizeof(timer_page
));
532 i
+= sizeof(timer_page
);
533 memcpy(ptr
+ i
, rbac_page
, sizeof(rbac_page
));
534 i
+= sizeof(rbac_page
);
535 memcpy(ptr
+ i
, cache_page
, sizeof(cache_page
));
536 i
+= sizeof(cache_page
);
537 memcpy(ptr
+ i
, rw_err_page
, sizeof(rw_err_page
));
538 i
+= sizeof(rw_err_page
);
545 ((__be16
*) ptr
)[0] = cpu_to_be16(i
- 2);
546 usb_stor_set_xfer_buf(ptr
, i
, srb
);
548 return USB_STOR_TRANSPORT_GOOD
;
551 static void datafab_info_destructor(void *extra
)
553 // this routine is a placeholder...
554 // currently, we don't allocate any extra memory so we're okay
558 // Transport for the Datafab MDCFE-B
560 static int datafab_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
562 struct datafab_info
*info
;
564 unsigned long block
, blocks
;
565 unsigned char *ptr
= us
->iobuf
;
566 static unsigned char inquiry_reply
[8] = {
567 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
571 us
->extra
= kzalloc(sizeof(struct datafab_info
), GFP_NOIO
);
573 return USB_STOR_TRANSPORT_ERROR
;
575 us
->extra_destructor
= datafab_info_destructor
;
576 ((struct datafab_info
*)us
->extra
)->lun
= -1;
579 info
= (struct datafab_info
*) (us
->extra
);
581 if (srb
->cmnd
[0] == INQUIRY
) {
582 usb_stor_dbg(us
, "INQUIRY - Returning bogus response\n");
583 memcpy(ptr
, inquiry_reply
, sizeof(inquiry_reply
));
584 fill_inquiry_response(us
, ptr
, 36);
585 return USB_STOR_TRANSPORT_GOOD
;
588 if (srb
->cmnd
[0] == READ_CAPACITY
) {
589 info
->ssize
= 0x200; // hard coded 512 byte sectors as per ATA spec
590 rc
= datafab_id_device(us
, info
);
591 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
594 usb_stor_dbg(us
, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
595 info
->sectors
, info
->ssize
);
598 // we need the last sector, not the number of sectors
599 ((__be32
*) ptr
)[0] = cpu_to_be32(info
->sectors
- 1);
600 ((__be32
*) ptr
)[1] = cpu_to_be32(info
->ssize
);
601 usb_stor_set_xfer_buf(ptr
, 8, srb
);
603 return USB_STOR_TRANSPORT_GOOD
;
606 if (srb
->cmnd
[0] == MODE_SELECT_10
) {
607 usb_stor_dbg(us
, "Gah! MODE_SELECT_10\n");
608 return USB_STOR_TRANSPORT_ERROR
;
611 // don't bother implementing READ_6 or WRITE_6.
613 if (srb
->cmnd
[0] == READ_10
) {
614 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
615 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
617 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
619 usb_stor_dbg(us
, "READ_10: read block 0x%04lx count %ld\n",
621 return datafab_read_data(us
, info
, block
, blocks
);
624 if (srb
->cmnd
[0] == READ_12
) {
625 // we'll probably never see a READ_12 but we'll do it anyway...
627 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
628 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
630 blocks
= ((u32
)(srb
->cmnd
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
631 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
633 usb_stor_dbg(us
, "READ_12: read block 0x%04lx count %ld\n",
635 return datafab_read_data(us
, info
, block
, blocks
);
638 if (srb
->cmnd
[0] == WRITE_10
) {
639 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
640 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
642 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
644 usb_stor_dbg(us
, "WRITE_10: write block 0x%04lx count %ld\n",
646 return datafab_write_data(us
, info
, block
, blocks
);
649 if (srb
->cmnd
[0] == WRITE_12
) {
650 // we'll probably never see a WRITE_12 but we'll do it anyway...
652 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
653 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
655 blocks
= ((u32
)(srb
->cmnd
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
656 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
658 usb_stor_dbg(us
, "WRITE_12: write block 0x%04lx count %ld\n",
660 return datafab_write_data(us
, info
, block
, blocks
);
663 if (srb
->cmnd
[0] == TEST_UNIT_READY
) {
664 usb_stor_dbg(us
, "TEST_UNIT_READY\n");
665 return datafab_id_device(us
, info
);
668 if (srb
->cmnd
[0] == REQUEST_SENSE
) {
669 usb_stor_dbg(us
, "REQUEST_SENSE - Returning faked response\n");
671 // this response is pretty bogus right now. eventually if necessary
672 // we can set the correct sense data. so far though it hasn't been
677 ptr
[2] = info
->sense_key
;
679 ptr
[12] = info
->sense_asc
;
680 ptr
[13] = info
->sense_ascq
;
681 usb_stor_set_xfer_buf(ptr
, 18, srb
);
683 return USB_STOR_TRANSPORT_GOOD
;
686 if (srb
->cmnd
[0] == MODE_SENSE
) {
687 usb_stor_dbg(us
, "MODE_SENSE_6 detected\n");
688 return datafab_handle_mode_sense(us
, srb
, 1);
691 if (srb
->cmnd
[0] == MODE_SENSE_10
) {
692 usb_stor_dbg(us
, "MODE_SENSE_10 detected\n");
693 return datafab_handle_mode_sense(us
, srb
, 0);
696 if (srb
->cmnd
[0] == ALLOW_MEDIUM_REMOVAL
) {
698 * sure. whatever. not like we can stop the user from
699 * popping the media out of the device (no locking doors, etc)
701 return USB_STOR_TRANSPORT_GOOD
;
704 if (srb
->cmnd
[0] == START_STOP
) {
706 * this is used by sd.c'check_scsidisk_media_change to detect
709 usb_stor_dbg(us
, "START_STOP\n");
711 * the first datafab_id_device after a media change returns
712 * an error (determined experimentally)
714 rc
= datafab_id_device(us
, info
);
715 if (rc
== USB_STOR_TRANSPORT_GOOD
) {
716 info
->sense_key
= NO_SENSE
;
717 srb
->result
= SUCCESS
;
719 info
->sense_key
= UNIT_ATTENTION
;
720 srb
->result
= SAM_STAT_CHECK_CONDITION
;
725 usb_stor_dbg(us
, "Gah! Unknown command: %d (0x%x)\n",
726 srb
->cmnd
[0], srb
->cmnd
[0]);
727 info
->sense_key
= 0x05;
728 info
->sense_asc
= 0x20;
729 info
->sense_ascq
= 0x00;
730 return USB_STOR_TRANSPORT_FAILED
;
733 static struct scsi_host_template datafab_host_template
;
735 static int datafab_probe(struct usb_interface
*intf
,
736 const struct usb_device_id
*id
)
741 result
= usb_stor_probe1(&us
, intf
, id
,
742 (id
- datafab_usb_ids
) + datafab_unusual_dev_list
,
743 &datafab_host_template
);
747 us
->transport_name
= "Datafab Bulk-Only";
748 us
->transport
= datafab_transport
;
749 us
->transport_reset
= usb_stor_Bulk_reset
;
752 result
= usb_stor_probe2(us
);
756 static struct usb_driver datafab_driver
= {
758 .probe
= datafab_probe
,
759 .disconnect
= usb_stor_disconnect
,
760 .suspend
= usb_stor_suspend
,
761 .resume
= usb_stor_resume
,
762 .reset_resume
= usb_stor_reset_resume
,
763 .pre_reset
= usb_stor_pre_reset
,
764 .post_reset
= usb_stor_post_reset
,
765 .id_table
= datafab_usb_ids
,
770 module_usb_stor_driver(datafab_driver
, datafab_host_template
, DRV_NAME
);