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 "transport.h"
59 #include <linux/sched.h>
60 #include <linux/errno.h>
61 #include <linux/slab.h>
63 static int datafab_determine_lun(struct us_data *us,
64 struct datafab_info *info);
68 datafab_bulk_read(struct us_data *us, unsigned char *data, unsigned int len) {
70 return USB_STOR_XFER_GOOD;
72 US_DEBUGP("datafab_bulk_read: len = %d\n", len);
73 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
79 datafab_bulk_write(struct us_data *us, unsigned char *data, unsigned int len) {
81 return USB_STOR_XFER_GOOD;
83 US_DEBUGP("datafab_bulk_write: len = %d\n", len);
84 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
89 static int datafab_read_data(struct us_data *us,
90 struct datafab_info *info,
94 unsigned char *command = us->iobuf;
95 unsigned char *buffer;
96 unsigned char thistime;
97 unsigned int totallen, alloclen;
99 unsigned int sg_idx = 0, sg_offset = 0;
101 // we're working in LBA mode. according to the ATA spec,
102 // we can support up to 28-bit addressing. I don't know if Datafab
103 // supports beyond 24-bit addressing. It's kind of hard to test
104 // since it requires > 8GB CF card.
106 if (sectors > 0x0FFFFFFF)
107 return USB_STOR_TRANSPORT_ERROR;
109 if (info->lun == -1) {
110 result = datafab_determine_lun(us, info);
111 if (result != USB_STOR_TRANSPORT_GOOD)
115 totallen = sectors * info->ssize;
117 // Since we don't read more than 64 KB at a time, we have to create
118 // a bounce buffer and move the data a piece at a time between the
119 // bounce buffer and the actual transfer buffer.
121 alloclen = min(totallen, 65536u);
122 buffer = kmalloc(alloclen, GFP_NOIO);
124 return USB_STOR_TRANSPORT_ERROR;
127 // loop, never allocate or transfer more than 64k at once
128 // (min(128k, 255*info->ssize) is the real limit)
130 len = min(totallen, alloclen);
131 thistime = (len / info->ssize) & 0xff;
134 command[1] = thistime;
135 command[2] = sector & 0xFF;
136 command[3] = (sector >> 8) & 0xFF;
137 command[4] = (sector >> 16) & 0xFF;
139 command[5] = 0xE0 + (info->lun << 4);
140 command[5] |= (sector >> 24) & 0x0F;
144 // send the read command
145 result = datafab_bulk_write(us, command, 8);
146 if (result != USB_STOR_XFER_GOOD)
150 result = datafab_bulk_read(us, buffer, len);
151 if (result != USB_STOR_XFER_GOOD)
154 // Store the data in the transfer buffer
155 usb_stor_access_xfer_buf(buffer, len, us->srb,
156 &sg_idx, &sg_offset, TO_XFER_BUF);
160 } while (totallen > 0);
163 return USB_STOR_TRANSPORT_GOOD;
167 return USB_STOR_TRANSPORT_ERROR;
171 static int datafab_write_data(struct us_data *us,
172 struct datafab_info *info,
176 unsigned char *command = us->iobuf;
177 unsigned char *reply = us->iobuf;
178 unsigned char *buffer;
179 unsigned char thistime;
180 unsigned int totallen, alloclen;
182 unsigned int sg_idx = 0, sg_offset = 0;
184 // we're working in LBA mode. according to the ATA spec,
185 // we can support up to 28-bit addressing. I don't know if Datafab
186 // supports beyond 24-bit addressing. It's kind of hard to test
187 // since it requires > 8GB CF card.
189 if (sectors > 0x0FFFFFFF)
190 return USB_STOR_TRANSPORT_ERROR;
192 if (info->lun == -1) {
193 result = datafab_determine_lun(us, info);
194 if (result != USB_STOR_TRANSPORT_GOOD)
198 totallen = sectors * info->ssize;
200 // Since we don't write more than 64 KB at a time, we have to create
201 // a bounce buffer and move the data a piece at a time between the
202 // bounce buffer and the actual transfer buffer.
204 alloclen = min(totallen, 65536u);
205 buffer = kmalloc(alloclen, GFP_NOIO);
207 return USB_STOR_TRANSPORT_ERROR;
210 // loop, never allocate or transfer more than 64k at once
211 // (min(128k, 255*info->ssize) is the real limit)
213 len = min(totallen, alloclen);
214 thistime = (len / info->ssize) & 0xff;
216 // Get the data from the transfer buffer
217 usb_stor_access_xfer_buf(buffer, len, us->srb,
218 &sg_idx, &sg_offset, FROM_XFER_BUF);
221 command[1] = thistime;
222 command[2] = sector & 0xFF;
223 command[3] = (sector >> 8) & 0xFF;
224 command[4] = (sector >> 16) & 0xFF;
226 command[5] = 0xE0 + (info->lun << 4);
227 command[5] |= (sector >> 24) & 0x0F;
232 result = datafab_bulk_write(us, command, 8);
233 if (result != USB_STOR_XFER_GOOD)
237 result = datafab_bulk_write(us, buffer, len);
238 if (result != USB_STOR_XFER_GOOD)
242 result = datafab_bulk_read(us, reply, 2);
243 if (result != USB_STOR_XFER_GOOD)
246 if (reply[0] != 0x50 && reply[1] != 0) {
247 US_DEBUGP("datafab_write_data: Gah! "
248 "write return code: %02x %02x\n",
250 result = USB_STOR_TRANSPORT_ERROR;
256 } while (totallen > 0);
259 return USB_STOR_TRANSPORT_GOOD;
263 return USB_STOR_TRANSPORT_ERROR;
267 static int datafab_determine_lun(struct us_data *us,
268 struct datafab_info *info)
270 // Dual-slot readers can be thought of as dual-LUN devices.
271 // We need to determine which card slot is being used.
272 // We'll send an IDENTIFY DEVICE command and see which LUN responds...
274 // There might be a better way of doing this?
276 static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
277 unsigned char *command = us->iobuf;
282 return USB_STOR_TRANSPORT_ERROR;
284 memcpy(command, scommand, 8);
285 buf = kmalloc(512, GFP_NOIO);
287 return USB_STOR_TRANSPORT_ERROR;
289 US_DEBUGP("datafab_determine_lun: locating...\n");
291 // we'll try 3 times before giving up...
293 while (count++ < 3) {
296 rc = datafab_bulk_write(us, command, 8);
297 if (rc != USB_STOR_XFER_GOOD) {
298 rc = USB_STOR_TRANSPORT_ERROR;
302 rc = datafab_bulk_read(us, buf, 512);
303 if (rc == USB_STOR_XFER_GOOD) {
305 rc = USB_STOR_TRANSPORT_GOOD;
311 rc = datafab_bulk_write(us, command, 8);
312 if (rc != USB_STOR_XFER_GOOD) {
313 rc = USB_STOR_TRANSPORT_ERROR;
317 rc = datafab_bulk_read(us, buf, 512);
318 if (rc == USB_STOR_XFER_GOOD) {
320 rc = USB_STOR_TRANSPORT_GOOD;
327 rc = USB_STOR_TRANSPORT_ERROR;
334 static int datafab_id_device(struct us_data *us,
335 struct datafab_info *info)
337 // this is a variation of the ATA "IDENTIFY DEVICE" command...according
338 // to the ATA spec, 'Sector Count' isn't used but the Windows driver
339 // sets this bit so we do too...
341 static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
342 unsigned char *command = us->iobuf;
343 unsigned char *reply;
347 return USB_STOR_TRANSPORT_ERROR;
349 if (info->lun == -1) {
350 rc = datafab_determine_lun(us, info);
351 if (rc != USB_STOR_TRANSPORT_GOOD)
355 memcpy(command, scommand, 8);
356 reply = kmalloc(512, GFP_NOIO);
358 return USB_STOR_TRANSPORT_ERROR;
360 command[5] += (info->lun << 4);
362 rc = datafab_bulk_write(us, command, 8);
363 if (rc != USB_STOR_XFER_GOOD) {
364 rc = USB_STOR_TRANSPORT_ERROR;
368 // we'll go ahead and extract the media capacity while we're here...
370 rc = datafab_bulk_read(us, reply, 512);
371 if (rc == USB_STOR_XFER_GOOD) {
372 // capacity is at word offset 57-58
374 info->sectors = ((u32)(reply[117]) << 24) |
375 ((u32)(reply[116]) << 16) |
376 ((u32)(reply[115]) << 8) |
377 ((u32)(reply[114]) );
378 rc = USB_STOR_TRANSPORT_GOOD;
382 rc = USB_STOR_TRANSPORT_ERROR;
390 static int datafab_handle_mode_sense(struct us_data *us,
394 static unsigned char rw_err_page[12] = {
395 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
397 static unsigned char cache_page[12] = {
398 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
400 static unsigned char rbac_page[12] = {
401 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
403 static unsigned char timer_page[8] = {
404 0x1C, 0x6, 0, 0, 0, 0
406 unsigned char pc, page_code;
408 struct datafab_info *info = (struct datafab_info *) (us->extra);
409 unsigned char *ptr = us->iobuf;
411 // most of this stuff is just a hack to get things working. the
412 // datafab reader doesn't present a SCSI interface so we
413 // fudge the SCSI commands...
416 pc = srb->cmnd[2] >> 6;
417 page_code = srb->cmnd[2] & 0x3F;
421 US_DEBUGP("datafab_handle_mode_sense: Current values\n");
424 US_DEBUGP("datafab_handle_mode_sense: Changeable values\n");
427 US_DEBUGP("datafab_handle_mode_sense: Default values\n");
430 US_DEBUGP("datafab_handle_mode_sense: Saves values\n");
436 ptr[2] = 0x00; // WP enable: 0x80
439 ptr[3] = 0x00; // WP enable: 0x80
445 // vendor-specific mode
446 info->sense_key = 0x05;
447 info->sense_asc = 0x24;
448 info->sense_ascq = 0x00;
449 return USB_STOR_TRANSPORT_FAILED;
452 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
453 i += sizeof(rw_err_page);
457 memcpy(ptr + i, cache_page, sizeof(cache_page));
458 i += sizeof(cache_page);
462 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
463 i += sizeof(rbac_page);
467 memcpy(ptr + i, timer_page, sizeof(timer_page));
468 i += sizeof(timer_page);
471 case 0x3F: // retrieve all pages
472 memcpy(ptr + i, timer_page, sizeof(timer_page));
473 i += sizeof(timer_page);
474 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
475 i += sizeof(rbac_page);
476 memcpy(ptr + i, cache_page, sizeof(cache_page));
477 i += sizeof(cache_page);
478 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
479 i += sizeof(rw_err_page);
486 ((u16 *) ptr)[0] = cpu_to_be16(i - 2);
487 usb_stor_set_xfer_buf(ptr, i, srb);
489 return USB_STOR_TRANSPORT_GOOD;
492 void datafab_info_destructor(void *extra)
494 // this routine is a placeholder...
495 // currently, we don't allocate any extra memory so we're okay
499 // Transport for the Datafab MDCFE-B
501 int datafab_transport(Scsi_Cmnd * srb, struct us_data *us)
503 struct datafab_info *info;
505 unsigned long block, blocks;
506 unsigned char *ptr = us->iobuf;
507 static unsigned char inquiry_reply[8] = {
508 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
512 us->extra = kmalloc(sizeof(struct datafab_info), GFP_NOIO);
514 US_DEBUGP("datafab_transport: Gah! "
515 "Can't allocate storage for Datafab info struct!\n");
516 return USB_STOR_TRANSPORT_ERROR;
518 memset(us->extra, 0, sizeof(struct datafab_info));
519 us->extra_destructor = datafab_info_destructor;
520 ((struct datafab_info *)us->extra)->lun = -1;
523 info = (struct datafab_info *) (us->extra);
525 if (srb->cmnd[0] == INQUIRY) {
526 US_DEBUGP("datafab_transport: INQUIRY. Returning bogus response");
527 memcpy(ptr, inquiry_reply, sizeof(inquiry_reply));
528 fill_inquiry_response(us, ptr, 36);
529 return USB_STOR_TRANSPORT_GOOD;
532 if (srb->cmnd[0] == READ_CAPACITY) {
533 info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
534 rc = datafab_id_device(us, info);
535 if (rc != USB_STOR_TRANSPORT_GOOD)
538 US_DEBUGP("datafab_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
539 info->sectors, info->ssize);
542 // we need the last sector, not the number of sectors
543 ((u32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
544 ((u32 *) ptr)[1] = cpu_to_be32(info->ssize);
545 usb_stor_set_xfer_buf(ptr, 8, srb);
547 return USB_STOR_TRANSPORT_GOOD;
550 if (srb->cmnd[0] == MODE_SELECT_10) {
551 US_DEBUGP("datafab_transport: Gah! MODE_SELECT_10.\n");
552 return USB_STOR_TRANSPORT_ERROR;
555 // don't bother implementing READ_6 or WRITE_6.
557 if (srb->cmnd[0] == READ_10) {
558 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
559 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
561 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
563 US_DEBUGP("datafab_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
564 return datafab_read_data(us, info, block, blocks);
567 if (srb->cmnd[0] == READ_12) {
568 // we'll probably never see a READ_12 but we'll do it anyway...
570 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
571 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
573 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
574 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
576 US_DEBUGP("datafab_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
577 return datafab_read_data(us, info, block, blocks);
580 if (srb->cmnd[0] == WRITE_10) {
581 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
582 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
584 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
586 US_DEBUGP("datafab_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
587 return datafab_write_data(us, info, block, blocks);
590 if (srb->cmnd[0] == WRITE_12) {
591 // we'll probably never see a WRITE_12 but we'll do it anyway...
593 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
594 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
596 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
597 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
599 US_DEBUGP("datafab_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
600 return datafab_write_data(us, info, block, blocks);
603 if (srb->cmnd[0] == TEST_UNIT_READY) {
604 US_DEBUGP("datafab_transport: TEST_UNIT_READY.\n");
605 return datafab_id_device(us, info);
608 if (srb->cmnd[0] == REQUEST_SENSE) {
609 US_DEBUGP("datafab_transport: REQUEST_SENSE. Returning faked response\n");
611 // this response is pretty bogus right now. eventually if necessary
612 // we can set the correct sense data. so far though it hasn't been
617 ptr[2] = info->sense_key;
619 ptr[12] = info->sense_asc;
620 ptr[13] = info->sense_ascq;
621 usb_stor_set_xfer_buf(ptr, 18, srb);
623 return USB_STOR_TRANSPORT_GOOD;
626 if (srb->cmnd[0] == MODE_SENSE) {
627 US_DEBUGP("datafab_transport: MODE_SENSE_6 detected\n");
628 return datafab_handle_mode_sense(us, srb, TRUE);
631 if (srb->cmnd[0] == MODE_SENSE_10) {
632 US_DEBUGP("datafab_transport: MODE_SENSE_10 detected\n");
633 return datafab_handle_mode_sense(us, srb, FALSE);
636 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
637 // sure. whatever. not like we can stop the user from
638 // popping the media out of the device (no locking doors, etc)
640 return USB_STOR_TRANSPORT_GOOD;
643 if (srb->cmnd[0] == START_STOP) {
644 /* this is used by sd.c'check_scsidisk_media_change to detect
646 US_DEBUGP("datafab_transport: START_STOP.\n");
647 /* the first datafab_id_device after a media change returns
648 an error (determined experimentally) */
649 rc = datafab_id_device(us, info);
650 if (rc == USB_STOR_TRANSPORT_GOOD) {
651 info->sense_key = NO_SENSE;
652 srb->result = SUCCESS;
654 info->sense_key = UNIT_ATTENTION;
655 srb->result = SAM_STAT_CHECK_CONDITION;
660 US_DEBUGP("datafab_transport: Gah! Unknown command: %d (0x%x)\n",
661 srb->cmnd[0], srb->cmnd[0]);
662 info->sense_key = 0x05;
663 info->sense_asc = 0x20;
664 info->sense_ascq = 0x00;
665 return USB_STOR_TRANSPORT_FAILED;