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 #define DRV_NAME "ums-datafab"
66 MODULE_DESCRIPTION("Driver for Datafab USB Compact Flash reader");
67 MODULE_AUTHOR("Jimmie Mayfield <mayfield+datafab@sackheads.org>");
68 MODULE_LICENSE("GPL");
71 unsigned long sectors
; /* total sector count */
72 unsigned long ssize
; /* sector size in bytes */
73 signed char lun
; /* used for dual-slot readers */
75 /* the following aren't used yet */
76 unsigned char sense_key
;
77 unsigned long sense_asc
; /* additional sense code */
78 unsigned long sense_ascq
; /* additional sense code qualifier */
81 static int datafab_determine_lun(struct us_data
*us
,
82 struct datafab_info
*info
);
86 * The table of devices
88 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
89 vendorName, productName, useProtocol, useTransport, \
90 initFunction, flags) \
91 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
92 .driver_info = (flags) }
94 static struct usb_device_id datafab_usb_ids
[] = {
95 # include "unusual_datafab.h"
96 { } /* Terminating entry */
98 MODULE_DEVICE_TABLE(usb
, datafab_usb_ids
);
105 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
106 vendor_name, product_name, use_protocol, use_transport, \
107 init_function, Flags) \
109 .vendorName = vendor_name, \
110 .productName = product_name, \
111 .useProtocol = use_protocol, \
112 .useTransport = use_transport, \
113 .initFunction = init_function, \
116 static struct us_unusual_dev datafab_unusual_dev_list
[] = {
117 # include "unusual_datafab.h"
118 { } /* Terminating entry */
125 datafab_bulk_read(struct us_data
*us
, unsigned char *data
, unsigned int len
) {
127 return USB_STOR_XFER_GOOD
;
129 usb_stor_dbg(us
, "len = %d\n", len
);
130 return usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
136 datafab_bulk_write(struct us_data
*us
, unsigned char *data
, unsigned int len
) {
138 return USB_STOR_XFER_GOOD
;
140 usb_stor_dbg(us
, "len = %d\n", len
);
141 return usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
146 static int datafab_read_data(struct us_data
*us
,
147 struct datafab_info
*info
,
151 unsigned char *command
= us
->iobuf
;
152 unsigned char *buffer
;
153 unsigned char thistime
;
154 unsigned int totallen
, alloclen
;
156 unsigned int sg_offset
= 0;
157 struct scatterlist
*sg
= NULL
;
159 // we're working in LBA mode. according to the ATA spec,
160 // we can support up to 28-bit addressing. I don't know if Datafab
161 // supports beyond 24-bit addressing. It's kind of hard to test
162 // since it requires > 8GB CF card.
164 if (sectors
> 0x0FFFFFFF)
165 return USB_STOR_TRANSPORT_ERROR
;
167 if (info
->lun
== -1) {
168 result
= datafab_determine_lun(us
, info
);
169 if (result
!= USB_STOR_TRANSPORT_GOOD
)
173 totallen
= sectors
* info
->ssize
;
175 // Since we don't read more than 64 KB at a time, we have to create
176 // a bounce buffer and move the data a piece at a time between the
177 // bounce buffer and the actual transfer buffer.
179 alloclen
= min(totallen
, 65536u);
180 buffer
= kmalloc(alloclen
, GFP_NOIO
);
182 return USB_STOR_TRANSPORT_ERROR
;
185 // loop, never allocate or transfer more than 64k at once
186 // (min(128k, 255*info->ssize) is the real limit)
188 len
= min(totallen
, alloclen
);
189 thistime
= (len
/ info
->ssize
) & 0xff;
192 command
[1] = thistime
;
193 command
[2] = sector
& 0xFF;
194 command
[3] = (sector
>> 8) & 0xFF;
195 command
[4] = (sector
>> 16) & 0xFF;
197 command
[5] = 0xE0 + (info
->lun
<< 4);
198 command
[5] |= (sector
>> 24) & 0x0F;
202 // send the read command
203 result
= datafab_bulk_write(us
, command
, 8);
204 if (result
!= USB_STOR_XFER_GOOD
)
208 result
= datafab_bulk_read(us
, buffer
, len
);
209 if (result
!= USB_STOR_XFER_GOOD
)
212 // Store the data in the transfer buffer
213 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
214 &sg
, &sg_offset
, TO_XFER_BUF
);
218 } while (totallen
> 0);
221 return USB_STOR_TRANSPORT_GOOD
;
225 return USB_STOR_TRANSPORT_ERROR
;
229 static int datafab_write_data(struct us_data
*us
,
230 struct datafab_info
*info
,
234 unsigned char *command
= us
->iobuf
;
235 unsigned char *reply
= us
->iobuf
;
236 unsigned char *buffer
;
237 unsigned char thistime
;
238 unsigned int totallen
, alloclen
;
240 unsigned int sg_offset
= 0;
241 struct scatterlist
*sg
= NULL
;
243 // we're working in LBA mode. according to the ATA spec,
244 // we can support up to 28-bit addressing. I don't know if Datafab
245 // supports beyond 24-bit addressing. It's kind of hard to test
246 // since it requires > 8GB CF card.
248 if (sectors
> 0x0FFFFFFF)
249 return USB_STOR_TRANSPORT_ERROR
;
251 if (info
->lun
== -1) {
252 result
= datafab_determine_lun(us
, info
);
253 if (result
!= USB_STOR_TRANSPORT_GOOD
)
257 totallen
= sectors
* info
->ssize
;
259 // Since we don't write more than 64 KB at a time, we have to create
260 // a bounce buffer and move the data a piece at a time between the
261 // bounce buffer and the actual transfer buffer.
263 alloclen
= min(totallen
, 65536u);
264 buffer
= kmalloc(alloclen
, GFP_NOIO
);
266 return USB_STOR_TRANSPORT_ERROR
;
269 // loop, never allocate or transfer more than 64k at once
270 // (min(128k, 255*info->ssize) is the real limit)
272 len
= min(totallen
, alloclen
);
273 thistime
= (len
/ info
->ssize
) & 0xff;
275 // Get the data from the transfer buffer
276 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
277 &sg
, &sg_offset
, FROM_XFER_BUF
);
280 command
[1] = thistime
;
281 command
[2] = sector
& 0xFF;
282 command
[3] = (sector
>> 8) & 0xFF;
283 command
[4] = (sector
>> 16) & 0xFF;
285 command
[5] = 0xE0 + (info
->lun
<< 4);
286 command
[5] |= (sector
>> 24) & 0x0F;
291 result
= datafab_bulk_write(us
, command
, 8);
292 if (result
!= USB_STOR_XFER_GOOD
)
296 result
= datafab_bulk_write(us
, buffer
, len
);
297 if (result
!= USB_STOR_XFER_GOOD
)
301 result
= datafab_bulk_read(us
, reply
, 2);
302 if (result
!= USB_STOR_XFER_GOOD
)
305 if (reply
[0] != 0x50 && reply
[1] != 0) {
306 usb_stor_dbg(us
, "Gah! write return code: %02x %02x\n",
308 result
= USB_STOR_TRANSPORT_ERROR
;
314 } while (totallen
> 0);
317 return USB_STOR_TRANSPORT_GOOD
;
321 return USB_STOR_TRANSPORT_ERROR
;
325 static int datafab_determine_lun(struct us_data
*us
,
326 struct datafab_info
*info
)
328 // Dual-slot readers can be thought of as dual-LUN devices.
329 // We need to determine which card slot is being used.
330 // We'll send an IDENTIFY DEVICE command and see which LUN responds...
332 // There might be a better way of doing this?
334 static unsigned char scommand
[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
335 unsigned char *command
= us
->iobuf
;
340 return USB_STOR_TRANSPORT_ERROR
;
342 memcpy(command
, scommand
, 8);
343 buf
= kmalloc(512, GFP_NOIO
);
345 return USB_STOR_TRANSPORT_ERROR
;
347 usb_stor_dbg(us
, "locating...\n");
349 // we'll try 3 times before giving up...
351 while (count
++ < 3) {
354 rc
= datafab_bulk_write(us
, command
, 8);
355 if (rc
!= USB_STOR_XFER_GOOD
) {
356 rc
= USB_STOR_TRANSPORT_ERROR
;
360 rc
= datafab_bulk_read(us
, buf
, 512);
361 if (rc
== USB_STOR_XFER_GOOD
) {
363 rc
= USB_STOR_TRANSPORT_GOOD
;
369 rc
= datafab_bulk_write(us
, command
, 8);
370 if (rc
!= USB_STOR_XFER_GOOD
) {
371 rc
= USB_STOR_TRANSPORT_ERROR
;
375 rc
= datafab_bulk_read(us
, buf
, 512);
376 if (rc
== USB_STOR_XFER_GOOD
) {
378 rc
= USB_STOR_TRANSPORT_GOOD
;
385 rc
= USB_STOR_TRANSPORT_ERROR
;
392 static int datafab_id_device(struct us_data
*us
,
393 struct datafab_info
*info
)
395 // this is a variation of the ATA "IDENTIFY DEVICE" command...according
396 // to the ATA spec, 'Sector Count' isn't used but the Windows driver
397 // sets this bit so we do too...
399 static unsigned char scommand
[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
400 unsigned char *command
= us
->iobuf
;
401 unsigned char *reply
;
405 return USB_STOR_TRANSPORT_ERROR
;
407 if (info
->lun
== -1) {
408 rc
= datafab_determine_lun(us
, info
);
409 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
413 memcpy(command
, scommand
, 8);
414 reply
= kmalloc(512, GFP_NOIO
);
416 return USB_STOR_TRANSPORT_ERROR
;
418 command
[5] += (info
->lun
<< 4);
420 rc
= datafab_bulk_write(us
, command
, 8);
421 if (rc
!= USB_STOR_XFER_GOOD
) {
422 rc
= USB_STOR_TRANSPORT_ERROR
;
426 // we'll go ahead and extract the media capacity while we're here...
428 rc
= datafab_bulk_read(us
, reply
, 512);
429 if (rc
== USB_STOR_XFER_GOOD
) {
430 // capacity is at word offset 57-58
432 info
->sectors
= ((u32
)(reply
[117]) << 24) |
433 ((u32
)(reply
[116]) << 16) |
434 ((u32
)(reply
[115]) << 8) |
435 ((u32
)(reply
[114]) );
436 rc
= USB_STOR_TRANSPORT_GOOD
;
440 rc
= USB_STOR_TRANSPORT_ERROR
;
448 static int datafab_handle_mode_sense(struct us_data
*us
,
449 struct scsi_cmnd
* srb
,
452 static unsigned char rw_err_page
[12] = {
453 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
455 static unsigned char cache_page
[12] = {
456 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
458 static unsigned char rbac_page
[12] = {
459 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
461 static unsigned char timer_page
[8] = {
462 0x1C, 0x6, 0, 0, 0, 0
464 unsigned char pc
, page_code
;
466 struct datafab_info
*info
= (struct datafab_info
*) (us
->extra
);
467 unsigned char *ptr
= us
->iobuf
;
469 // most of this stuff is just a hack to get things working. the
470 // datafab reader doesn't present a SCSI interface so we
471 // fudge the SCSI commands...
474 pc
= srb
->cmnd
[2] >> 6;
475 page_code
= srb
->cmnd
[2] & 0x3F;
479 usb_stor_dbg(us
, "Current values\n");
482 usb_stor_dbg(us
, "Changeable values\n");
485 usb_stor_dbg(us
, "Default values\n");
488 usb_stor_dbg(us
, "Saves values\n");
494 ptr
[2] = 0x00; // WP enable: 0x80
497 ptr
[3] = 0x00; // WP enable: 0x80
503 // vendor-specific mode
504 info
->sense_key
= 0x05;
505 info
->sense_asc
= 0x24;
506 info
->sense_ascq
= 0x00;
507 return USB_STOR_TRANSPORT_FAILED
;
510 memcpy(ptr
+ i
, rw_err_page
, sizeof(rw_err_page
));
511 i
+= sizeof(rw_err_page
);
515 memcpy(ptr
+ i
, cache_page
, sizeof(cache_page
));
516 i
+= sizeof(cache_page
);
520 memcpy(ptr
+ i
, rbac_page
, sizeof(rbac_page
));
521 i
+= sizeof(rbac_page
);
525 memcpy(ptr
+ i
, timer_page
, sizeof(timer_page
));
526 i
+= sizeof(timer_page
);
529 case 0x3F: // retrieve all pages
530 memcpy(ptr
+ i
, timer_page
, sizeof(timer_page
));
531 i
+= sizeof(timer_page
);
532 memcpy(ptr
+ i
, rbac_page
, sizeof(rbac_page
));
533 i
+= sizeof(rbac_page
);
534 memcpy(ptr
+ i
, cache_page
, sizeof(cache_page
));
535 i
+= sizeof(cache_page
);
536 memcpy(ptr
+ i
, rw_err_page
, sizeof(rw_err_page
));
537 i
+= sizeof(rw_err_page
);
544 ((__be16
*) ptr
)[0] = cpu_to_be16(i
- 2);
545 usb_stor_set_xfer_buf(ptr
, i
, srb
);
547 return USB_STOR_TRANSPORT_GOOD
;
550 static void datafab_info_destructor(void *extra
)
552 // this routine is a placeholder...
553 // currently, we don't allocate any extra memory so we're okay
557 // Transport for the Datafab MDCFE-B
559 static int datafab_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
561 struct datafab_info
*info
;
563 unsigned long block
, blocks
;
564 unsigned char *ptr
= us
->iobuf
;
565 static unsigned char inquiry_reply
[8] = {
566 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
570 us
->extra
= kzalloc(sizeof(struct datafab_info
), GFP_NOIO
);
572 return USB_STOR_TRANSPORT_ERROR
;
574 us
->extra_destructor
= datafab_info_destructor
;
575 ((struct datafab_info
*)us
->extra
)->lun
= -1;
578 info
= (struct datafab_info
*) (us
->extra
);
580 if (srb
->cmnd
[0] == INQUIRY
) {
581 usb_stor_dbg(us
, "INQUIRY - Returning bogus response\n");
582 memcpy(ptr
, inquiry_reply
, sizeof(inquiry_reply
));
583 fill_inquiry_response(us
, ptr
, 36);
584 return USB_STOR_TRANSPORT_GOOD
;
587 if (srb
->cmnd
[0] == READ_CAPACITY
) {
588 info
->ssize
= 0x200; // hard coded 512 byte sectors as per ATA spec
589 rc
= datafab_id_device(us
, info
);
590 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
593 usb_stor_dbg(us
, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
594 info
->sectors
, info
->ssize
);
597 // we need the last sector, not the number of sectors
598 ((__be32
*) ptr
)[0] = cpu_to_be32(info
->sectors
- 1);
599 ((__be32
*) ptr
)[1] = cpu_to_be32(info
->ssize
);
600 usb_stor_set_xfer_buf(ptr
, 8, srb
);
602 return USB_STOR_TRANSPORT_GOOD
;
605 if (srb
->cmnd
[0] == MODE_SELECT_10
) {
606 usb_stor_dbg(us
, "Gah! MODE_SELECT_10\n");
607 return USB_STOR_TRANSPORT_ERROR
;
610 // don't bother implementing READ_6 or WRITE_6.
612 if (srb
->cmnd
[0] == READ_10
) {
613 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
614 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
616 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
618 usb_stor_dbg(us
, "READ_10: read block 0x%04lx count %ld\n",
620 return datafab_read_data(us
, info
, block
, blocks
);
623 if (srb
->cmnd
[0] == READ_12
) {
624 // we'll probably never see a READ_12 but we'll do it anyway...
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
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
630 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
632 usb_stor_dbg(us
, "READ_12: read block 0x%04lx count %ld\n",
634 return datafab_read_data(us
, info
, block
, blocks
);
637 if (srb
->cmnd
[0] == WRITE_10
) {
638 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
639 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
641 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
643 usb_stor_dbg(us
, "WRITE_10: write block 0x%04lx count %ld\n",
645 return datafab_write_data(us
, info
, block
, blocks
);
648 if (srb
->cmnd
[0] == WRITE_12
) {
649 // we'll probably never see a WRITE_12 but we'll do it anyway...
651 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
652 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
654 blocks
= ((u32
)(srb
->cmnd
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
655 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
657 usb_stor_dbg(us
, "WRITE_12: write block 0x%04lx count %ld\n",
659 return datafab_write_data(us
, info
, block
, blocks
);
662 if (srb
->cmnd
[0] == TEST_UNIT_READY
) {
663 usb_stor_dbg(us
, "TEST_UNIT_READY\n");
664 return datafab_id_device(us
, info
);
667 if (srb
->cmnd
[0] == REQUEST_SENSE
) {
668 usb_stor_dbg(us
, "REQUEST_SENSE - Returning faked response\n");
670 // this response is pretty bogus right now. eventually if necessary
671 // we can set the correct sense data. so far though it hasn't been
676 ptr
[2] = info
->sense_key
;
678 ptr
[12] = info
->sense_asc
;
679 ptr
[13] = info
->sense_ascq
;
680 usb_stor_set_xfer_buf(ptr
, 18, srb
);
682 return USB_STOR_TRANSPORT_GOOD
;
685 if (srb
->cmnd
[0] == MODE_SENSE
) {
686 usb_stor_dbg(us
, "MODE_SENSE_6 detected\n");
687 return datafab_handle_mode_sense(us
, srb
, 1);
690 if (srb
->cmnd
[0] == MODE_SENSE_10
) {
691 usb_stor_dbg(us
, "MODE_SENSE_10 detected\n");
692 return datafab_handle_mode_sense(us
, srb
, 0);
695 if (srb
->cmnd
[0] == ALLOW_MEDIUM_REMOVAL
) {
696 // sure. whatever. not like we can stop the user from
697 // popping the media out of the device (no locking doors, etc)
699 return USB_STOR_TRANSPORT_GOOD
;
702 if (srb
->cmnd
[0] == START_STOP
) {
703 /* this is used by sd.c'check_scsidisk_media_change to detect
705 usb_stor_dbg(us
, "START_STOP\n");
706 /* the first datafab_id_device after a media change returns
707 an error (determined experimentally) */
708 rc
= datafab_id_device(us
, info
);
709 if (rc
== USB_STOR_TRANSPORT_GOOD
) {
710 info
->sense_key
= NO_SENSE
;
711 srb
->result
= SUCCESS
;
713 info
->sense_key
= UNIT_ATTENTION
;
714 srb
->result
= SAM_STAT_CHECK_CONDITION
;
719 usb_stor_dbg(us
, "Gah! Unknown command: %d (0x%x)\n",
720 srb
->cmnd
[0], srb
->cmnd
[0]);
721 info
->sense_key
= 0x05;
722 info
->sense_asc
= 0x20;
723 info
->sense_ascq
= 0x00;
724 return USB_STOR_TRANSPORT_FAILED
;
727 static struct scsi_host_template datafab_host_template
;
729 static int datafab_probe(struct usb_interface
*intf
,
730 const struct usb_device_id
*id
)
735 result
= usb_stor_probe1(&us
, intf
, id
,
736 (id
- datafab_usb_ids
) + datafab_unusual_dev_list
,
737 &datafab_host_template
);
741 us
->transport_name
= "Datafab Bulk-Only";
742 us
->transport
= datafab_transport
;
743 us
->transport_reset
= usb_stor_Bulk_reset
;
746 result
= usb_stor_probe2(us
);
750 static struct usb_driver datafab_driver
= {
752 .probe
= datafab_probe
,
753 .disconnect
= usb_stor_disconnect
,
754 .suspend
= usb_stor_suspend
,
755 .resume
= usb_stor_resume
,
756 .reset_resume
= usb_stor_reset_resume
,
757 .pre_reset
= usb_stor_pre_reset
,
758 .post_reset
= usb_stor_post_reset
,
759 .id_table
= datafab_usb_ids
,
764 module_usb_stor_driver(datafab_driver
, datafab_host_template
, DRV_NAME
);