1 /* Driver for Datafab USB Compact Flash reader
3 * $Id: datafab.c,v 1.7 2002/02/25 00:40:13 mdharm Exp $
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>
23 * This program is free software; you can redistribute it and/or modify it
24 * under the terms of the GNU General Public License as published by the
25 * Free Software Foundation; either version 2, or (at your option) any
28 * This program is distributed in the hope that it will be useful, but
29 * WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 * General Public License for more details.
33 * You should have received a copy of the GNU General Public License along
34 * with this program; if not, write to the Free Software Foundation, Inc.,
35 * 675 Mass Ave, Cambridge, MA 02139, USA.
39 * This driver attempts to support USB CompactFlash reader/writer devices
40 * based on Datafab USB-to-ATA chips. It was specifically developed for the
41 * Datafab MDCFE-B USB CompactFlash reader but has since been found to work
42 * with a variety of Datafab-based devices from a number of manufacturers.
43 * I've received a report of this driver working with a Datafab-based
44 * SmartMedia device though please be aware that I'm personally unable to
45 * test SmartMedia support.
47 * This driver supports reading and writing. If you're truly paranoid,
48 * however, you can force the driver into a write-protected state by setting
49 * the WP enable bits in datafab_handle_mode_sense(). See the comments
53 #include <linux/sched.h>
54 #include <linux/errno.h>
55 #include <linux/slab.h>
57 #include <scsi/scsi.h>
58 #include <scsi/scsi_cmnd.h>
61 #include "transport.h"
66 static int datafab_determine_lun(struct us_data *us,
67 struct datafab_info *info);
71 datafab_bulk_read(struct us_data *us, unsigned char *data, unsigned int len) {
73 return USB_STOR_XFER_GOOD;
75 US_DEBUGP("datafab_bulk_read: len = %d\n", len);
76 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
82 datafab_bulk_write(struct us_data *us, unsigned char *data, unsigned int len) {
84 return USB_STOR_XFER_GOOD;
86 US_DEBUGP("datafab_bulk_write: len = %d\n", len);
87 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
92 static int datafab_read_data(struct us_data *us,
93 struct datafab_info *info,
97 unsigned char *command = us->iobuf;
98 unsigned char *buffer;
99 unsigned char thistime;
100 unsigned int totallen, alloclen;
102 unsigned int sg_idx = 0, sg_offset = 0;
104 // we're working in LBA mode. according to the ATA spec,
105 // we can support up to 28-bit addressing. I don't know if Datafab
106 // supports beyond 24-bit addressing. It's kind of hard to test
107 // since it requires > 8GB CF card.
109 if (sectors > 0x0FFFFFFF)
110 return USB_STOR_TRANSPORT_ERROR;
112 if (info->lun == -1) {
113 result = datafab_determine_lun(us, info);
114 if (result != USB_STOR_TRANSPORT_GOOD)
118 totallen = sectors * info->ssize;
120 // Since we don't read more than 64 KB at a time, we have to create
121 // a bounce buffer and move the data a piece at a time between the
122 // bounce buffer and the actual transfer buffer.
124 alloclen = min(totallen, 65536u);
125 buffer = kmalloc(alloclen, GFP_NOIO);
127 return USB_STOR_TRANSPORT_ERROR;
130 // loop, never allocate or transfer more than 64k at once
131 // (min(128k, 255*info->ssize) is the real limit)
133 len = min(totallen, alloclen);
134 thistime = (len / info->ssize) & 0xff;
137 command[1] = thistime;
138 command[2] = sector & 0xFF;
139 command[3] = (sector >> 8) & 0xFF;
140 command[4] = (sector >> 16) & 0xFF;
142 command[5] = 0xE0 + (info->lun << 4);
143 command[5] |= (sector >> 24) & 0x0F;
147 // send the read command
148 result = datafab_bulk_write(us, command, 8);
149 if (result != USB_STOR_XFER_GOOD)
153 result = datafab_bulk_read(us, buffer, len);
154 if (result != USB_STOR_XFER_GOOD)
157 // Store the data in the transfer buffer
158 usb_stor_access_xfer_buf(buffer, len, us->srb,
159 &sg_idx, &sg_offset, TO_XFER_BUF);
163 } while (totallen > 0);
166 return USB_STOR_TRANSPORT_GOOD;
170 return USB_STOR_TRANSPORT_ERROR;
174 static int datafab_write_data(struct us_data *us,
175 struct datafab_info *info,
179 unsigned char *command = us->iobuf;
180 unsigned char *reply = us->iobuf;
181 unsigned char *buffer;
182 unsigned char thistime;
183 unsigned int totallen, alloclen;
185 unsigned int sg_idx = 0, sg_offset = 0;
187 // we're working in LBA mode. according to the ATA spec,
188 // we can support up to 28-bit addressing. I don't know if Datafab
189 // supports beyond 24-bit addressing. It's kind of hard to test
190 // since it requires > 8GB CF card.
192 if (sectors > 0x0FFFFFFF)
193 return USB_STOR_TRANSPORT_ERROR;
195 if (info->lun == -1) {
196 result = datafab_determine_lun(us, info);
197 if (result != USB_STOR_TRANSPORT_GOOD)
201 totallen = sectors * info->ssize;
203 // Since we don't write more than 64 KB at a time, we have to create
204 // a bounce buffer and move the data a piece at a time between the
205 // bounce buffer and the actual transfer buffer.
207 alloclen = min(totallen, 65536u);
208 buffer = kmalloc(alloclen, GFP_NOIO);
210 return USB_STOR_TRANSPORT_ERROR;
213 // loop, never allocate or transfer more than 64k at once
214 // (min(128k, 255*info->ssize) is the real limit)
216 len = min(totallen, alloclen);
217 thistime = (len / info->ssize) & 0xff;
219 // Get the data from the transfer buffer
220 usb_stor_access_xfer_buf(buffer, len, us->srb,
221 &sg_idx, &sg_offset, FROM_XFER_BUF);
224 command[1] = thistime;
225 command[2] = sector & 0xFF;
226 command[3] = (sector >> 8) & 0xFF;
227 command[4] = (sector >> 16) & 0xFF;
229 command[5] = 0xE0 + (info->lun << 4);
230 command[5] |= (sector >> 24) & 0x0F;
235 result = datafab_bulk_write(us, command, 8);
236 if (result != USB_STOR_XFER_GOOD)
240 result = datafab_bulk_write(us, buffer, len);
241 if (result != USB_STOR_XFER_GOOD)
245 result = datafab_bulk_read(us, reply, 2);
246 if (result != USB_STOR_XFER_GOOD)
249 if (reply[0] != 0x50 && reply[1] != 0) {
250 US_DEBUGP("datafab_write_data: Gah! "
251 "write return code: %02x %02x\n",
253 result = USB_STOR_TRANSPORT_ERROR;
259 } while (totallen > 0);
262 return USB_STOR_TRANSPORT_GOOD;
266 return USB_STOR_TRANSPORT_ERROR;
270 static int datafab_determine_lun(struct us_data *us,
271 struct datafab_info *info)
273 // Dual-slot readers can be thought of as dual-LUN devices.
274 // We need to determine which card slot is being used.
275 // We'll send an IDENTIFY DEVICE command and see which LUN responds...
277 // There might be a better way of doing this?
279 static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
280 unsigned char *command = us->iobuf;
285 return USB_STOR_TRANSPORT_ERROR;
287 memcpy(command, scommand, 8);
288 buf = kmalloc(512, GFP_NOIO);
290 return USB_STOR_TRANSPORT_ERROR;
292 US_DEBUGP("datafab_determine_lun: locating...\n");
294 // we'll try 3 times before giving up...
296 while (count++ < 3) {
299 rc = datafab_bulk_write(us, command, 8);
300 if (rc != USB_STOR_XFER_GOOD) {
301 rc = USB_STOR_TRANSPORT_ERROR;
305 rc = datafab_bulk_read(us, buf, 512);
306 if (rc == USB_STOR_XFER_GOOD) {
308 rc = USB_STOR_TRANSPORT_GOOD;
314 rc = datafab_bulk_write(us, command, 8);
315 if (rc != USB_STOR_XFER_GOOD) {
316 rc = USB_STOR_TRANSPORT_ERROR;
320 rc = datafab_bulk_read(us, buf, 512);
321 if (rc == USB_STOR_XFER_GOOD) {
323 rc = USB_STOR_TRANSPORT_GOOD;
330 rc = USB_STOR_TRANSPORT_ERROR;
337 static int datafab_id_device(struct us_data *us,
338 struct datafab_info *info)
340 // this is a variation of the ATA "IDENTIFY DEVICE" command...according
341 // to the ATA spec, 'Sector Count' isn't used but the Windows driver
342 // sets this bit so we do too...
344 static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
345 unsigned char *command = us->iobuf;
346 unsigned char *reply;
350 return USB_STOR_TRANSPORT_ERROR;
352 if (info->lun == -1) {
353 rc = datafab_determine_lun(us, info);
354 if (rc != USB_STOR_TRANSPORT_GOOD)
358 memcpy(command, scommand, 8);
359 reply = kmalloc(512, GFP_NOIO);
361 return USB_STOR_TRANSPORT_ERROR;
363 command[5] += (info->lun << 4);
365 rc = datafab_bulk_write(us, command, 8);
366 if (rc != USB_STOR_XFER_GOOD) {
367 rc = USB_STOR_TRANSPORT_ERROR;
371 // we'll go ahead and extract the media capacity while we're here...
373 rc = datafab_bulk_read(us, reply, 512);
374 if (rc == USB_STOR_XFER_GOOD) {
375 // capacity is at word offset 57-58
377 info->sectors = ((u32)(reply[117]) << 24) |
378 ((u32)(reply[116]) << 16) |
379 ((u32)(reply[115]) << 8) |
380 ((u32)(reply[114]) );
381 rc = USB_STOR_TRANSPORT_GOOD;
385 rc = USB_STOR_TRANSPORT_ERROR;
393 static int datafab_handle_mode_sense(struct us_data *us,
394 struct scsi_cmnd * srb,
397 static unsigned char rw_err_page[12] = {
398 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
400 static unsigned char cache_page[12] = {
401 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
403 static unsigned char rbac_page[12] = {
404 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
406 static unsigned char timer_page[8] = {
407 0x1C, 0x6, 0, 0, 0, 0
409 unsigned char pc, page_code;
411 struct datafab_info *info = (struct datafab_info *) (us->extra);
412 unsigned char *ptr = us->iobuf;
414 // most of this stuff is just a hack to get things working. the
415 // datafab reader doesn't present a SCSI interface so we
416 // fudge the SCSI commands...
419 pc = srb->cmnd[2] >> 6;
420 page_code = srb->cmnd[2] & 0x3F;
424 US_DEBUGP("datafab_handle_mode_sense: Current values\n");
427 US_DEBUGP("datafab_handle_mode_sense: Changeable values\n");
430 US_DEBUGP("datafab_handle_mode_sense: Default values\n");
433 US_DEBUGP("datafab_handle_mode_sense: Saves values\n");
439 ptr[2] = 0x00; // WP enable: 0x80
442 ptr[3] = 0x00; // WP enable: 0x80
448 // vendor-specific mode
449 info->sense_key = 0x05;
450 info->sense_asc = 0x24;
451 info->sense_ascq = 0x00;
452 return USB_STOR_TRANSPORT_FAILED;
455 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
456 i += sizeof(rw_err_page);
460 memcpy(ptr + i, cache_page, sizeof(cache_page));
461 i += sizeof(cache_page);
465 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
466 i += sizeof(rbac_page);
470 memcpy(ptr + i, timer_page, sizeof(timer_page));
471 i += sizeof(timer_page);
474 case 0x3F: // retrieve all pages
475 memcpy(ptr + i, timer_page, sizeof(timer_page));
476 i += sizeof(timer_page);
477 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
478 i += sizeof(rbac_page);
479 memcpy(ptr + i, cache_page, sizeof(cache_page));
480 i += sizeof(cache_page);
481 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
482 i += sizeof(rw_err_page);
489 ((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
490 usb_stor_set_xfer_buf(ptr, i, srb);
492 return USB_STOR_TRANSPORT_GOOD;
495 static void datafab_info_destructor(void *extra)
497 // this routine is a placeholder...
498 // currently, we don't allocate any extra memory so we're okay
502 // Transport for the Datafab MDCFE-B
504 int datafab_transport(struct scsi_cmnd * srb, struct us_data *us)
506 struct datafab_info *info;
508 unsigned long block, blocks;
509 unsigned char *ptr = us->iobuf;
510 static unsigned char inquiry_reply[8] = {
511 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
515 us->extra = kmalloc(sizeof(struct datafab_info), GFP_NOIO);
517 US_DEBUGP("datafab_transport: Gah! "
518 "Can't allocate storage for Datafab info struct!\n");
519 return USB_STOR_TRANSPORT_ERROR;
521 memset(us->extra, 0, sizeof(struct datafab_info));
522 us->extra_destructor = datafab_info_destructor;
523 ((struct datafab_info *)us->extra)->lun = -1;
526 info = (struct datafab_info *) (us->extra);
528 if (srb->cmnd[0] == INQUIRY) {
529 US_DEBUGP("datafab_transport: INQUIRY. Returning bogus response");
530 memcpy(ptr, inquiry_reply, sizeof(inquiry_reply));
531 fill_inquiry_response(us, ptr, 36);
532 return USB_STOR_TRANSPORT_GOOD;
535 if (srb->cmnd[0] == READ_CAPACITY) {
536 info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
537 rc = datafab_id_device(us, info);
538 if (rc != USB_STOR_TRANSPORT_GOOD)
541 US_DEBUGP("datafab_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
542 info->sectors, info->ssize);
545 // we need the last sector, not the number of sectors
546 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
547 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
548 usb_stor_set_xfer_buf(ptr, 8, srb);
550 return USB_STOR_TRANSPORT_GOOD;
553 if (srb->cmnd[0] == MODE_SELECT_10) {
554 US_DEBUGP("datafab_transport: Gah! MODE_SELECT_10.\n");
555 return USB_STOR_TRANSPORT_ERROR;
558 // don't bother implementing READ_6 or WRITE_6.
560 if (srb->cmnd[0] == READ_10) {
561 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
562 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
564 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
566 US_DEBUGP("datafab_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
567 return datafab_read_data(us, info, block, blocks);
570 if (srb->cmnd[0] == READ_12) {
571 // we'll probably never see a READ_12 but we'll do it anyway...
573 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
574 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
576 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
577 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
579 US_DEBUGP("datafab_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
580 return datafab_read_data(us, info, block, blocks);
583 if (srb->cmnd[0] == WRITE_10) {
584 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
585 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
587 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
589 US_DEBUGP("datafab_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
590 return datafab_write_data(us, info, block, blocks);
593 if (srb->cmnd[0] == WRITE_12) {
594 // we'll probably never see a WRITE_12 but we'll do it anyway...
596 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
597 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
599 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
600 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
602 US_DEBUGP("datafab_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
603 return datafab_write_data(us, info, block, blocks);
606 if (srb->cmnd[0] == TEST_UNIT_READY) {
607 US_DEBUGP("datafab_transport: TEST_UNIT_READY.\n");
608 return datafab_id_device(us, info);
611 if (srb->cmnd[0] == REQUEST_SENSE) {
612 US_DEBUGP("datafab_transport: REQUEST_SENSE. Returning faked response\n");
614 // this response is pretty bogus right now. eventually if necessary
615 // we can set the correct sense data. so far though it hasn't been
620 ptr[2] = info->sense_key;
622 ptr[12] = info->sense_asc;
623 ptr[13] = info->sense_ascq;
624 usb_stor_set_xfer_buf(ptr, 18, srb);
626 return USB_STOR_TRANSPORT_GOOD;
629 if (srb->cmnd[0] == MODE_SENSE) {
630 US_DEBUGP("datafab_transport: MODE_SENSE_6 detected\n");
631 return datafab_handle_mode_sense(us, srb, 1);
634 if (srb->cmnd[0] == MODE_SENSE_10) {
635 US_DEBUGP("datafab_transport: MODE_SENSE_10 detected\n");
636 return datafab_handle_mode_sense(us, srb, 0);
639 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
640 // sure. whatever. not like we can stop the user from
641 // popping the media out of the device (no locking doors, etc)
643 return USB_STOR_TRANSPORT_GOOD;
646 if (srb->cmnd[0] == START_STOP) {
647 /* this is used by sd.c'check_scsidisk_media_change to detect
649 US_DEBUGP("datafab_transport: START_STOP.\n");
650 /* the first datafab_id_device after a media change returns
651 an error (determined experimentally) */
652 rc = datafab_id_device(us, info);
653 if (rc == USB_STOR_TRANSPORT_GOOD) {
654 info->sense_key = NO_SENSE;
655 srb->result = SUCCESS;
657 info->sense_key = UNIT_ATTENTION;
658 srb->result = SAM_STAT_CHECK_CONDITION;
663 US_DEBUGP("datafab_transport: Gah! Unknown command: %d (0x%x)\n",
664 srb->cmnd[0], srb->cmnd[0]);
665 info->sense_key = 0x05;
666 info->sense_asc = 0x20;
667 info->sense_ascq = 0x00;
668 return USB_STOR_TRANSPORT_FAILED;