Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / usb / storage / datafab.c
blob579e9f52053adac18b5720271b4e443cc3e18821
1 /* Driver for Datafab USB Compact Flash reader
3 * $Id: datafab.c,v 1.7 2002/02/25 00:40:13 mdharm Exp $
5 * datafab driver v0.1:
7 * First release
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)
20 * Other contributors:
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
26 * later version.
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
50 * in that routine.
53 #include <linux/errno.h>
54 #include <linux/slab.h>
56 #include <scsi/scsi.h>
57 #include <scsi/scsi_cmnd.h>
59 #include "usb.h"
60 #include "transport.h"
61 #include "protocol.h"
62 #include "debug.h"
63 #include "datafab.h"
65 static int datafab_determine_lun(struct us_data *us,
66 struct datafab_info *info);
69 static inline int
70 datafab_bulk_read(struct us_data *us, unsigned char *data, unsigned int len) {
71 if (len == 0)
72 return USB_STOR_XFER_GOOD;
74 US_DEBUGP("datafab_bulk_read: len = %d\n", len);
75 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
76 data, len, NULL);
80 static inline int
81 datafab_bulk_write(struct us_data *us, unsigned char *data, unsigned int len) {
82 if (len == 0)
83 return USB_STOR_XFER_GOOD;
85 US_DEBUGP("datafab_bulk_write: len = %d\n", len);
86 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
87 data, len, NULL);
91 static int datafab_read_data(struct us_data *us,
92 struct datafab_info *info,
93 u32 sector,
94 u32 sectors)
96 unsigned char *command = us->iobuf;
97 unsigned char *buffer;
98 unsigned char thistime;
99 unsigned int totallen, alloclen;
100 int len, result;
101 unsigned int sg_offset = 0;
102 struct scatterlist *sg = NULL;
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)
115 return result;
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);
126 if (buffer == NULL)
127 return USB_STOR_TRANSPORT_ERROR;
129 do {
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;
136 command[0] = 0;
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;
144 command[6] = 0x20;
145 command[7] = 0x01;
147 // send the read command
148 result = datafab_bulk_write(us, command, 8);
149 if (result != USB_STOR_XFER_GOOD)
150 goto leave;
152 // read the result
153 result = datafab_bulk_read(us, buffer, len);
154 if (result != USB_STOR_XFER_GOOD)
155 goto leave;
157 // Store the data in the transfer buffer
158 usb_stor_access_xfer_buf(buffer, len, us->srb,
159 &sg, &sg_offset, TO_XFER_BUF);
161 sector += thistime;
162 totallen -= len;
163 } while (totallen > 0);
165 kfree(buffer);
166 return USB_STOR_TRANSPORT_GOOD;
168 leave:
169 kfree(buffer);
170 return USB_STOR_TRANSPORT_ERROR;
174 static int datafab_write_data(struct us_data *us,
175 struct datafab_info *info,
176 u32 sector,
177 u32 sectors)
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;
184 int len, result;
185 unsigned int sg_offset = 0;
186 struct scatterlist *sg = NULL;
188 // we're working in LBA mode. according to the ATA spec,
189 // we can support up to 28-bit addressing. I don't know if Datafab
190 // supports beyond 24-bit addressing. It's kind of hard to test
191 // since it requires > 8GB CF card.
193 if (sectors > 0x0FFFFFFF)
194 return USB_STOR_TRANSPORT_ERROR;
196 if (info->lun == -1) {
197 result = datafab_determine_lun(us, info);
198 if (result != USB_STOR_TRANSPORT_GOOD)
199 return result;
202 totallen = sectors * info->ssize;
204 // Since we don't write more than 64 KB at a time, we have to create
205 // a bounce buffer and move the data a piece at a time between the
206 // bounce buffer and the actual transfer buffer.
208 alloclen = min(totallen, 65536u);
209 buffer = kmalloc(alloclen, GFP_NOIO);
210 if (buffer == NULL)
211 return USB_STOR_TRANSPORT_ERROR;
213 do {
214 // loop, never allocate or transfer more than 64k at once
215 // (min(128k, 255*info->ssize) is the real limit)
217 len = min(totallen, alloclen);
218 thistime = (len / info->ssize) & 0xff;
220 // Get the data from the transfer buffer
221 usb_stor_access_xfer_buf(buffer, len, us->srb,
222 &sg, &sg_offset, FROM_XFER_BUF);
224 command[0] = 0;
225 command[1] = thistime;
226 command[2] = sector & 0xFF;
227 command[3] = (sector >> 8) & 0xFF;
228 command[4] = (sector >> 16) & 0xFF;
230 command[5] = 0xE0 + (info->lun << 4);
231 command[5] |= (sector >> 24) & 0x0F;
232 command[6] = 0x30;
233 command[7] = 0x02;
235 // send the command
236 result = datafab_bulk_write(us, command, 8);
237 if (result != USB_STOR_XFER_GOOD)
238 goto leave;
240 // send the data
241 result = datafab_bulk_write(us, buffer, len);
242 if (result != USB_STOR_XFER_GOOD)
243 goto leave;
245 // read the result
246 result = datafab_bulk_read(us, reply, 2);
247 if (result != USB_STOR_XFER_GOOD)
248 goto leave;
250 if (reply[0] != 0x50 && reply[1] != 0) {
251 US_DEBUGP("datafab_write_data: Gah! "
252 "write return code: %02x %02x\n",
253 reply[0], reply[1]);
254 result = USB_STOR_TRANSPORT_ERROR;
255 goto leave;
258 sector += thistime;
259 totallen -= len;
260 } while (totallen > 0);
262 kfree(buffer);
263 return USB_STOR_TRANSPORT_GOOD;
265 leave:
266 kfree(buffer);
267 return USB_STOR_TRANSPORT_ERROR;
271 static int datafab_determine_lun(struct us_data *us,
272 struct datafab_info *info)
274 // Dual-slot readers can be thought of as dual-LUN devices.
275 // We need to determine which card slot is being used.
276 // We'll send an IDENTIFY DEVICE command and see which LUN responds...
278 // There might be a better way of doing this?
280 static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
281 unsigned char *command = us->iobuf;
282 unsigned char *buf;
283 int count = 0, rc;
285 if (!us || !info)
286 return USB_STOR_TRANSPORT_ERROR;
288 memcpy(command, scommand, 8);
289 buf = kmalloc(512, GFP_NOIO);
290 if (!buf)
291 return USB_STOR_TRANSPORT_ERROR;
293 US_DEBUGP("datafab_determine_lun: locating...\n");
295 // we'll try 3 times before giving up...
297 while (count++ < 3) {
298 command[5] = 0xa0;
300 rc = datafab_bulk_write(us, command, 8);
301 if (rc != USB_STOR_XFER_GOOD) {
302 rc = USB_STOR_TRANSPORT_ERROR;
303 goto leave;
306 rc = datafab_bulk_read(us, buf, 512);
307 if (rc == USB_STOR_XFER_GOOD) {
308 info->lun = 0;
309 rc = USB_STOR_TRANSPORT_GOOD;
310 goto leave;
313 command[5] = 0xb0;
315 rc = datafab_bulk_write(us, command, 8);
316 if (rc != USB_STOR_XFER_GOOD) {
317 rc = USB_STOR_TRANSPORT_ERROR;
318 goto leave;
321 rc = datafab_bulk_read(us, buf, 512);
322 if (rc == USB_STOR_XFER_GOOD) {
323 info->lun = 1;
324 rc = USB_STOR_TRANSPORT_GOOD;
325 goto leave;
328 msleep(20);
331 rc = USB_STOR_TRANSPORT_ERROR;
333 leave:
334 kfree(buf);
335 return rc;
338 static int datafab_id_device(struct us_data *us,
339 struct datafab_info *info)
341 // this is a variation of the ATA "IDENTIFY DEVICE" command...according
342 // to the ATA spec, 'Sector Count' isn't used but the Windows driver
343 // sets this bit so we do too...
345 static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
346 unsigned char *command = us->iobuf;
347 unsigned char *reply;
348 int rc;
350 if (!us || !info)
351 return USB_STOR_TRANSPORT_ERROR;
353 if (info->lun == -1) {
354 rc = datafab_determine_lun(us, info);
355 if (rc != USB_STOR_TRANSPORT_GOOD)
356 return rc;
359 memcpy(command, scommand, 8);
360 reply = kmalloc(512, GFP_NOIO);
361 if (!reply)
362 return USB_STOR_TRANSPORT_ERROR;
364 command[5] += (info->lun << 4);
366 rc = datafab_bulk_write(us, command, 8);
367 if (rc != USB_STOR_XFER_GOOD) {
368 rc = USB_STOR_TRANSPORT_ERROR;
369 goto leave;
372 // we'll go ahead and extract the media capacity while we're here...
374 rc = datafab_bulk_read(us, reply, 512);
375 if (rc == USB_STOR_XFER_GOOD) {
376 // capacity is at word offset 57-58
378 info->sectors = ((u32)(reply[117]) << 24) |
379 ((u32)(reply[116]) << 16) |
380 ((u32)(reply[115]) << 8) |
381 ((u32)(reply[114]) );
382 rc = USB_STOR_TRANSPORT_GOOD;
383 goto leave;
386 rc = USB_STOR_TRANSPORT_ERROR;
388 leave:
389 kfree(reply);
390 return rc;
394 static int datafab_handle_mode_sense(struct us_data *us,
395 struct scsi_cmnd * srb,
396 int sense_6)
398 static unsigned char rw_err_page[12] = {
399 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
401 static unsigned char cache_page[12] = {
402 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
404 static unsigned char rbac_page[12] = {
405 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
407 static unsigned char timer_page[8] = {
408 0x1C, 0x6, 0, 0, 0, 0
410 unsigned char pc, page_code;
411 unsigned int i = 0;
412 struct datafab_info *info = (struct datafab_info *) (us->extra);
413 unsigned char *ptr = us->iobuf;
415 // most of this stuff is just a hack to get things working. the
416 // datafab reader doesn't present a SCSI interface so we
417 // fudge the SCSI commands...
420 pc = srb->cmnd[2] >> 6;
421 page_code = srb->cmnd[2] & 0x3F;
423 switch (pc) {
424 case 0x0:
425 US_DEBUGP("datafab_handle_mode_sense: Current values\n");
426 break;
427 case 0x1:
428 US_DEBUGP("datafab_handle_mode_sense: Changeable values\n");
429 break;
430 case 0x2:
431 US_DEBUGP("datafab_handle_mode_sense: Default values\n");
432 break;
433 case 0x3:
434 US_DEBUGP("datafab_handle_mode_sense: Saves values\n");
435 break;
438 memset(ptr, 0, 8);
439 if (sense_6) {
440 ptr[2] = 0x00; // WP enable: 0x80
441 i = 4;
442 } else {
443 ptr[3] = 0x00; // WP enable: 0x80
444 i = 8;
447 switch (page_code) {
448 default:
449 // vendor-specific mode
450 info->sense_key = 0x05;
451 info->sense_asc = 0x24;
452 info->sense_ascq = 0x00;
453 return USB_STOR_TRANSPORT_FAILED;
455 case 0x1:
456 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
457 i += sizeof(rw_err_page);
458 break;
460 case 0x8:
461 memcpy(ptr + i, cache_page, sizeof(cache_page));
462 i += sizeof(cache_page);
463 break;
465 case 0x1B:
466 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
467 i += sizeof(rbac_page);
468 break;
470 case 0x1C:
471 memcpy(ptr + i, timer_page, sizeof(timer_page));
472 i += sizeof(timer_page);
473 break;
475 case 0x3F: // retrieve all pages
476 memcpy(ptr + i, timer_page, sizeof(timer_page));
477 i += sizeof(timer_page);
478 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
479 i += sizeof(rbac_page);
480 memcpy(ptr + i, cache_page, sizeof(cache_page));
481 i += sizeof(cache_page);
482 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
483 i += sizeof(rw_err_page);
484 break;
487 if (sense_6)
488 ptr[0] = i - 1;
489 else
490 ((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
491 usb_stor_set_xfer_buf(ptr, i, srb);
493 return USB_STOR_TRANSPORT_GOOD;
496 static void datafab_info_destructor(void *extra)
498 // this routine is a placeholder...
499 // currently, we don't allocate any extra memory so we're okay
503 // Transport for the Datafab MDCFE-B
505 int datafab_transport(struct scsi_cmnd * srb, struct us_data *us)
507 struct datafab_info *info;
508 int rc;
509 unsigned long block, blocks;
510 unsigned char *ptr = us->iobuf;
511 static unsigned char inquiry_reply[8] = {
512 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
515 if (!us->extra) {
516 us->extra = kzalloc(sizeof(struct datafab_info), GFP_NOIO);
517 if (!us->extra) {
518 US_DEBUGP("datafab_transport: Gah! "
519 "Can't allocate storage for Datafab info struct!\n");
520 return USB_STOR_TRANSPORT_ERROR;
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)
539 return rc;
541 US_DEBUGP("datafab_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
542 info->sectors, info->ssize);
544 // build the reply
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
616 // necessary
618 memset(ptr, 0, 18);
619 ptr[0] = 0xF0;
620 ptr[2] = info->sense_key;
621 ptr[7] = 11;
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
648 media change */
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;
656 } else {
657 info->sense_key = UNIT_ATTENTION;
658 srb->result = SAM_STAT_CHECK_CONDITION;
660 return rc;
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;