1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Datafab USB Compact Flash reader
9 * Current development and maintenance by:
10 * (c) 2000 Jimmie Mayfield (mayfield+datafab@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)
21 * (c) 2002 Alan Stern <stern@rowland.org>
25 * This driver attempts to support USB CompactFlash reader/writer devices
26 * based on Datafab USB-to-ATA chips. It was specifically developed for the
27 * Datafab MDCFE-B USB CompactFlash reader but has since been found to work
28 * with a variety of Datafab-based devices from a number of manufacturers.
29 * I've received a report of this driver working with a Datafab-based
30 * SmartMedia device though please be aware that I'm personally unable to
31 * test SmartMedia support.
33 * This driver supports reading and writing. If you're truly paranoid,
34 * however, you can force the driver into a write-protected state by setting
35 * the WP enable bits in datafab_handle_mode_sense(). See the comments
39 #include <linux/errno.h>
40 #include <linux/module.h>
41 #include <linux/slab.h>
43 #include <scsi/scsi.h>
44 #include <scsi/scsi_cmnd.h>
47 #include "transport.h"
52 #define DRV_NAME "ums-datafab"
54 MODULE_DESCRIPTION("Driver for Datafab USB Compact Flash reader");
55 MODULE_AUTHOR("Jimmie Mayfield <mayfield+datafab@sackheads.org>");
56 MODULE_LICENSE("GPL");
59 unsigned long sectors
; /* total sector count */
60 unsigned long ssize
; /* sector size in bytes */
61 signed char lun
; /* used for dual-slot readers */
63 /* the following aren't used yet */
64 unsigned char sense_key
;
65 unsigned long sense_asc
; /* additional sense code */
66 unsigned long sense_ascq
; /* additional sense code qualifier */
69 static int datafab_determine_lun(struct us_data
*us
,
70 struct datafab_info
*info
);
74 * The table of devices
76 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
77 vendorName, productName, useProtocol, useTransport, \
78 initFunction, flags) \
79 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
80 .driver_info = (flags) }
82 static struct usb_device_id datafab_usb_ids
[] = {
83 # include "unusual_datafab.h"
84 { } /* Terminating entry */
86 MODULE_DEVICE_TABLE(usb
, datafab_usb_ids
);
93 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
94 vendor_name, product_name, use_protocol, use_transport, \
95 init_function, Flags) \
97 .vendorName = vendor_name, \
98 .productName = product_name, \
99 .useProtocol = use_protocol, \
100 .useTransport = use_transport, \
101 .initFunction = init_function, \
104 static struct us_unusual_dev datafab_unusual_dev_list
[] = {
105 # include "unusual_datafab.h"
106 { } /* Terminating entry */
113 datafab_bulk_read(struct us_data
*us
, unsigned char *data
, unsigned int len
) {
115 return USB_STOR_XFER_GOOD
;
117 usb_stor_dbg(us
, "len = %d\n", len
);
118 return usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
124 datafab_bulk_write(struct us_data
*us
, unsigned char *data
, unsigned int len
) {
126 return USB_STOR_XFER_GOOD
;
128 usb_stor_dbg(us
, "len = %d\n", len
);
129 return usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
134 static int datafab_read_data(struct us_data
*us
,
135 struct datafab_info
*info
,
139 unsigned char *command
= us
->iobuf
;
140 unsigned char *buffer
;
141 unsigned char thistime
;
142 unsigned int totallen
, alloclen
;
144 unsigned int sg_offset
= 0;
145 struct scatterlist
*sg
= NULL
;
147 // we're working in LBA mode. according to the ATA spec,
148 // we can support up to 28-bit addressing. I don't know if Datafab
149 // supports beyond 24-bit addressing. It's kind of hard to test
150 // since it requires > 8GB CF card.
152 if (sectors
> 0x0FFFFFFF)
153 return USB_STOR_TRANSPORT_ERROR
;
155 if (info
->lun
== -1) {
156 result
= datafab_determine_lun(us
, info
);
157 if (result
!= USB_STOR_TRANSPORT_GOOD
)
161 totallen
= sectors
* info
->ssize
;
163 // Since we don't read more than 64 KB at a time, we have to create
164 // a bounce buffer and move the data a piece at a time between the
165 // bounce buffer and the actual transfer buffer.
167 alloclen
= min(totallen
, 65536u);
168 buffer
= kmalloc(alloclen
, GFP_NOIO
);
170 return USB_STOR_TRANSPORT_ERROR
;
173 // loop, never allocate or transfer more than 64k at once
174 // (min(128k, 255*info->ssize) is the real limit)
176 len
= min(totallen
, alloclen
);
177 thistime
= (len
/ info
->ssize
) & 0xff;
180 command
[1] = thistime
;
181 command
[2] = sector
& 0xFF;
182 command
[3] = (sector
>> 8) & 0xFF;
183 command
[4] = (sector
>> 16) & 0xFF;
185 command
[5] = 0xE0 + (info
->lun
<< 4);
186 command
[5] |= (sector
>> 24) & 0x0F;
190 // send the read command
191 result
= datafab_bulk_write(us
, command
, 8);
192 if (result
!= USB_STOR_XFER_GOOD
)
196 result
= datafab_bulk_read(us
, buffer
, len
);
197 if (result
!= USB_STOR_XFER_GOOD
)
200 // Store the data in the transfer buffer
201 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
202 &sg
, &sg_offset
, TO_XFER_BUF
);
206 } while (totallen
> 0);
209 return USB_STOR_TRANSPORT_GOOD
;
213 return USB_STOR_TRANSPORT_ERROR
;
217 static int datafab_write_data(struct us_data
*us
,
218 struct datafab_info
*info
,
222 unsigned char *command
= us
->iobuf
;
223 unsigned char *reply
= us
->iobuf
;
224 unsigned char *buffer
;
225 unsigned char thistime
;
226 unsigned int totallen
, alloclen
;
228 unsigned int sg_offset
= 0;
229 struct scatterlist
*sg
= NULL
;
231 // we're working in LBA mode. according to the ATA spec,
232 // we can support up to 28-bit addressing. I don't know if Datafab
233 // supports beyond 24-bit addressing. It's kind of hard to test
234 // since it requires > 8GB CF card.
236 if (sectors
> 0x0FFFFFFF)
237 return USB_STOR_TRANSPORT_ERROR
;
239 if (info
->lun
== -1) {
240 result
= datafab_determine_lun(us
, info
);
241 if (result
!= USB_STOR_TRANSPORT_GOOD
)
245 totallen
= sectors
* info
->ssize
;
247 // Since we don't write more than 64 KB at a time, we have to create
248 // a bounce buffer and move the data a piece at a time between the
249 // bounce buffer and the actual transfer buffer.
251 alloclen
= min(totallen
, 65536u);
252 buffer
= kmalloc(alloclen
, GFP_NOIO
);
254 return USB_STOR_TRANSPORT_ERROR
;
257 // loop, never allocate or transfer more than 64k at once
258 // (min(128k, 255*info->ssize) is the real limit)
260 len
= min(totallen
, alloclen
);
261 thistime
= (len
/ info
->ssize
) & 0xff;
263 // Get the data from the transfer buffer
264 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
265 &sg
, &sg_offset
, FROM_XFER_BUF
);
268 command
[1] = thistime
;
269 command
[2] = sector
& 0xFF;
270 command
[3] = (sector
>> 8) & 0xFF;
271 command
[4] = (sector
>> 16) & 0xFF;
273 command
[5] = 0xE0 + (info
->lun
<< 4);
274 command
[5] |= (sector
>> 24) & 0x0F;
279 result
= datafab_bulk_write(us
, command
, 8);
280 if (result
!= USB_STOR_XFER_GOOD
)
284 result
= datafab_bulk_write(us
, buffer
, len
);
285 if (result
!= USB_STOR_XFER_GOOD
)
289 result
= datafab_bulk_read(us
, reply
, 2);
290 if (result
!= USB_STOR_XFER_GOOD
)
293 if (reply
[0] != 0x50 && reply
[1] != 0) {
294 usb_stor_dbg(us
, "Gah! write return code: %02x %02x\n",
296 result
= USB_STOR_TRANSPORT_ERROR
;
302 } while (totallen
> 0);
305 return USB_STOR_TRANSPORT_GOOD
;
309 return USB_STOR_TRANSPORT_ERROR
;
313 static int datafab_determine_lun(struct us_data
*us
,
314 struct datafab_info
*info
)
316 // Dual-slot readers can be thought of as dual-LUN devices.
317 // We need to determine which card slot is being used.
318 // We'll send an IDENTIFY DEVICE command and see which LUN responds...
320 // There might be a better way of doing this?
322 static unsigned char scommand
[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
323 unsigned char *command
= us
->iobuf
;
328 return USB_STOR_TRANSPORT_ERROR
;
330 memcpy(command
, scommand
, 8);
331 buf
= kmalloc(512, GFP_NOIO
);
333 return USB_STOR_TRANSPORT_ERROR
;
335 usb_stor_dbg(us
, "locating...\n");
337 // we'll try 3 times before giving up...
339 while (count
++ < 3) {
342 rc
= datafab_bulk_write(us
, command
, 8);
343 if (rc
!= USB_STOR_XFER_GOOD
) {
344 rc
= USB_STOR_TRANSPORT_ERROR
;
348 rc
= datafab_bulk_read(us
, buf
, 512);
349 if (rc
== USB_STOR_XFER_GOOD
) {
351 rc
= USB_STOR_TRANSPORT_GOOD
;
357 rc
= datafab_bulk_write(us
, command
, 8);
358 if (rc
!= USB_STOR_XFER_GOOD
) {
359 rc
= USB_STOR_TRANSPORT_ERROR
;
363 rc
= datafab_bulk_read(us
, buf
, 512);
364 if (rc
== USB_STOR_XFER_GOOD
) {
366 rc
= USB_STOR_TRANSPORT_GOOD
;
373 rc
= USB_STOR_TRANSPORT_ERROR
;
380 static int datafab_id_device(struct us_data
*us
,
381 struct datafab_info
*info
)
383 // this is a variation of the ATA "IDENTIFY DEVICE" command...according
384 // to the ATA spec, 'Sector Count' isn't used but the Windows driver
385 // sets this bit so we do too...
387 static unsigned char scommand
[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
388 unsigned char *command
= us
->iobuf
;
389 unsigned char *reply
;
393 return USB_STOR_TRANSPORT_ERROR
;
395 if (info
->lun
== -1) {
396 rc
= datafab_determine_lun(us
, info
);
397 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
401 memcpy(command
, scommand
, 8);
402 reply
= kmalloc(512, GFP_NOIO
);
404 return USB_STOR_TRANSPORT_ERROR
;
406 command
[5] += (info
->lun
<< 4);
408 rc
= datafab_bulk_write(us
, command
, 8);
409 if (rc
!= USB_STOR_XFER_GOOD
) {
410 rc
= USB_STOR_TRANSPORT_ERROR
;
414 // we'll go ahead and extract the media capacity while we're here...
416 rc
= datafab_bulk_read(us
, reply
, 512);
417 if (rc
== USB_STOR_XFER_GOOD
) {
418 // capacity is at word offset 57-58
420 info
->sectors
= ((u32
)(reply
[117]) << 24) |
421 ((u32
)(reply
[116]) << 16) |
422 ((u32
)(reply
[115]) << 8) |
423 ((u32
)(reply
[114]) );
424 rc
= USB_STOR_TRANSPORT_GOOD
;
428 rc
= USB_STOR_TRANSPORT_ERROR
;
436 static int datafab_handle_mode_sense(struct us_data
*us
,
437 struct scsi_cmnd
* srb
,
440 static unsigned char rw_err_page
[12] = {
441 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
443 static unsigned char cache_page
[12] = {
444 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
446 static unsigned char rbac_page
[12] = {
447 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
449 static unsigned char timer_page
[8] = {
450 0x1C, 0x6, 0, 0, 0, 0
452 unsigned char pc
, page_code
;
454 struct datafab_info
*info
= (struct datafab_info
*) (us
->extra
);
455 unsigned char *ptr
= us
->iobuf
;
457 // most of this stuff is just a hack to get things working. the
458 // datafab reader doesn't present a SCSI interface so we
459 // fudge the SCSI commands...
462 pc
= srb
->cmnd
[2] >> 6;
463 page_code
= srb
->cmnd
[2] & 0x3F;
467 usb_stor_dbg(us
, "Current values\n");
470 usb_stor_dbg(us
, "Changeable values\n");
473 usb_stor_dbg(us
, "Default values\n");
476 usb_stor_dbg(us
, "Saves values\n");
482 ptr
[2] = 0x00; // WP enable: 0x80
485 ptr
[3] = 0x00; // WP enable: 0x80
491 // vendor-specific mode
492 info
->sense_key
= 0x05;
493 info
->sense_asc
= 0x24;
494 info
->sense_ascq
= 0x00;
495 return USB_STOR_TRANSPORT_FAILED
;
498 memcpy(ptr
+ i
, rw_err_page
, sizeof(rw_err_page
));
499 i
+= sizeof(rw_err_page
);
503 memcpy(ptr
+ i
, cache_page
, sizeof(cache_page
));
504 i
+= sizeof(cache_page
);
508 memcpy(ptr
+ i
, rbac_page
, sizeof(rbac_page
));
509 i
+= sizeof(rbac_page
);
513 memcpy(ptr
+ i
, timer_page
, sizeof(timer_page
));
514 i
+= sizeof(timer_page
);
517 case 0x3F: // retrieve all pages
518 memcpy(ptr
+ i
, timer_page
, sizeof(timer_page
));
519 i
+= sizeof(timer_page
);
520 memcpy(ptr
+ i
, rbac_page
, sizeof(rbac_page
));
521 i
+= sizeof(rbac_page
);
522 memcpy(ptr
+ i
, cache_page
, sizeof(cache_page
));
523 i
+= sizeof(cache_page
);
524 memcpy(ptr
+ i
, rw_err_page
, sizeof(rw_err_page
));
525 i
+= sizeof(rw_err_page
);
532 ((__be16
*) ptr
)[0] = cpu_to_be16(i
- 2);
533 usb_stor_set_xfer_buf(ptr
, i
, srb
);
535 return USB_STOR_TRANSPORT_GOOD
;
538 static void datafab_info_destructor(void *extra
)
540 // this routine is a placeholder...
541 // currently, we don't allocate any extra memory so we're okay
545 // Transport for the Datafab MDCFE-B
547 static int datafab_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
549 struct datafab_info
*info
;
551 unsigned long block
, blocks
;
552 unsigned char *ptr
= us
->iobuf
;
553 static unsigned char inquiry_reply
[8] = {
554 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
558 us
->extra
= kzalloc(sizeof(struct datafab_info
), GFP_NOIO
);
560 return USB_STOR_TRANSPORT_ERROR
;
562 us
->extra_destructor
= datafab_info_destructor
;
563 ((struct datafab_info
*)us
->extra
)->lun
= -1;
566 info
= (struct datafab_info
*) (us
->extra
);
568 if (srb
->cmnd
[0] == INQUIRY
) {
569 usb_stor_dbg(us
, "INQUIRY - Returning bogus response\n");
570 memcpy(ptr
, inquiry_reply
, sizeof(inquiry_reply
));
571 fill_inquiry_response(us
, ptr
, 36);
572 return USB_STOR_TRANSPORT_GOOD
;
575 if (srb
->cmnd
[0] == READ_CAPACITY
) {
576 info
->ssize
= 0x200; // hard coded 512 byte sectors as per ATA spec
577 rc
= datafab_id_device(us
, info
);
578 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
581 usb_stor_dbg(us
, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
582 info
->sectors
, info
->ssize
);
585 // we need the last sector, not the number of sectors
586 ((__be32
*) ptr
)[0] = cpu_to_be32(info
->sectors
- 1);
587 ((__be32
*) ptr
)[1] = cpu_to_be32(info
->ssize
);
588 usb_stor_set_xfer_buf(ptr
, 8, srb
);
590 return USB_STOR_TRANSPORT_GOOD
;
593 if (srb
->cmnd
[0] == MODE_SELECT_10
) {
594 usb_stor_dbg(us
, "Gah! MODE_SELECT_10\n");
595 return USB_STOR_TRANSPORT_ERROR
;
598 // don't bother implementing READ_6 or WRITE_6.
600 if (srb
->cmnd
[0] == READ_10
) {
601 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
602 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
604 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
606 usb_stor_dbg(us
, "READ_10: read block 0x%04lx count %ld\n",
608 return datafab_read_data(us
, info
, block
, blocks
);
611 if (srb
->cmnd
[0] == READ_12
) {
612 // we'll probably never see a READ_12 but we'll do it anyway...
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
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
618 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
620 usb_stor_dbg(us
, "READ_12: read block 0x%04lx count %ld\n",
622 return datafab_read_data(us
, info
, block
, blocks
);
625 if (srb
->cmnd
[0] == WRITE_10
) {
626 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
627 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
629 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
631 usb_stor_dbg(us
, "WRITE_10: write block 0x%04lx count %ld\n",
633 return datafab_write_data(us
, info
, block
, blocks
);
636 if (srb
->cmnd
[0] == WRITE_12
) {
637 // we'll probably never see a WRITE_12 but we'll do it anyway...
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
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
643 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
645 usb_stor_dbg(us
, "WRITE_12: write block 0x%04lx count %ld\n",
647 return datafab_write_data(us
, info
, block
, blocks
);
650 if (srb
->cmnd
[0] == TEST_UNIT_READY
) {
651 usb_stor_dbg(us
, "TEST_UNIT_READY\n");
652 return datafab_id_device(us
, info
);
655 if (srb
->cmnd
[0] == REQUEST_SENSE
) {
656 usb_stor_dbg(us
, "REQUEST_SENSE - Returning faked response\n");
658 // this response is pretty bogus right now. eventually if necessary
659 // we can set the correct sense data. so far though it hasn't been
664 ptr
[2] = info
->sense_key
;
666 ptr
[12] = info
->sense_asc
;
667 ptr
[13] = info
->sense_ascq
;
668 usb_stor_set_xfer_buf(ptr
, 18, srb
);
670 return USB_STOR_TRANSPORT_GOOD
;
673 if (srb
->cmnd
[0] == MODE_SENSE
) {
674 usb_stor_dbg(us
, "MODE_SENSE_6 detected\n");
675 return datafab_handle_mode_sense(us
, srb
, 1);
678 if (srb
->cmnd
[0] == MODE_SENSE_10
) {
679 usb_stor_dbg(us
, "MODE_SENSE_10 detected\n");
680 return datafab_handle_mode_sense(us
, srb
, 0);
683 if (srb
->cmnd
[0] == ALLOW_MEDIUM_REMOVAL
) {
685 * sure. whatever. not like we can stop the user from
686 * popping the media out of the device (no locking doors, etc)
688 return USB_STOR_TRANSPORT_GOOD
;
691 if (srb
->cmnd
[0] == START_STOP
) {
693 * this is used by sd.c'check_scsidisk_media_change to detect
696 usb_stor_dbg(us
, "START_STOP\n");
698 * the first datafab_id_device after a media change returns
699 * an error (determined experimentally)
701 rc
= datafab_id_device(us
, info
);
702 if (rc
== USB_STOR_TRANSPORT_GOOD
) {
703 info
->sense_key
= NO_SENSE
;
704 srb
->result
= SUCCESS
;
706 info
->sense_key
= UNIT_ATTENTION
;
707 srb
->result
= SAM_STAT_CHECK_CONDITION
;
712 usb_stor_dbg(us
, "Gah! Unknown command: %d (0x%x)\n",
713 srb
->cmnd
[0], srb
->cmnd
[0]);
714 info
->sense_key
= 0x05;
715 info
->sense_asc
= 0x20;
716 info
->sense_ascq
= 0x00;
717 return USB_STOR_TRANSPORT_FAILED
;
720 static struct scsi_host_template datafab_host_template
;
722 static int datafab_probe(struct usb_interface
*intf
,
723 const struct usb_device_id
*id
)
728 result
= usb_stor_probe1(&us
, intf
, id
,
729 (id
- datafab_usb_ids
) + datafab_unusual_dev_list
,
730 &datafab_host_template
);
734 us
->transport_name
= "Datafab Bulk-Only";
735 us
->transport
= datafab_transport
;
736 us
->transport_reset
= usb_stor_Bulk_reset
;
739 result
= usb_stor_probe2(us
);
743 static struct usb_driver datafab_driver
= {
745 .probe
= datafab_probe
,
746 .disconnect
= usb_stor_disconnect
,
747 .suspend
= usb_stor_suspend
,
748 .resume
= usb_stor_resume
,
749 .reset_resume
= usb_stor_reset_resume
,
750 .pre_reset
= usb_stor_pre_reset
,
751 .post_reset
= usb_stor_post_reset
,
752 .id_table
= datafab_usb_ids
,
757 module_usb_stor_driver(datafab_driver
, datafab_host_template
, DRV_NAME
);