alpha: fix trivial section mismatch warnings
[pv_ops_mirror.git] / drivers / usb / storage / datafab.c
blobc87ad1bae1d6327b3f131af80cf04033ebbf78cf
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_idx = 0, sg_offset = 0;
103 // we're working in LBA mode. according to the ATA spec,
104 // we can support up to 28-bit addressing. I don't know if Datafab
105 // supports beyond 24-bit addressing. It's kind of hard to test
106 // since it requires > 8GB CF card.
108 if (sectors > 0x0FFFFFFF)
109 return USB_STOR_TRANSPORT_ERROR;
111 if (info->lun == -1) {
112 result = datafab_determine_lun(us, info);
113 if (result != USB_STOR_TRANSPORT_GOOD)
114 return result;
117 totallen = sectors * info->ssize;
119 // Since we don't read more than 64 KB at a time, we have to create
120 // a bounce buffer and move the data a piece at a time between the
121 // bounce buffer and the actual transfer buffer.
123 alloclen = min(totallen, 65536u);
124 buffer = kmalloc(alloclen, GFP_NOIO);
125 if (buffer == NULL)
126 return USB_STOR_TRANSPORT_ERROR;
128 do {
129 // loop, never allocate or transfer more than 64k at once
130 // (min(128k, 255*info->ssize) is the real limit)
132 len = min(totallen, alloclen);
133 thistime = (len / info->ssize) & 0xff;
135 command[0] = 0;
136 command[1] = thistime;
137 command[2] = sector & 0xFF;
138 command[3] = (sector >> 8) & 0xFF;
139 command[4] = (sector >> 16) & 0xFF;
141 command[5] = 0xE0 + (info->lun << 4);
142 command[5] |= (sector >> 24) & 0x0F;
143 command[6] = 0x20;
144 command[7] = 0x01;
146 // send the read command
147 result = datafab_bulk_write(us, command, 8);
148 if (result != USB_STOR_XFER_GOOD)
149 goto leave;
151 // read the result
152 result = datafab_bulk_read(us, buffer, len);
153 if (result != USB_STOR_XFER_GOOD)
154 goto leave;
156 // Store the data in the transfer buffer
157 usb_stor_access_xfer_buf(buffer, len, us->srb,
158 &sg_idx, &sg_offset, TO_XFER_BUF);
160 sector += thistime;
161 totallen -= len;
162 } while (totallen > 0);
164 kfree(buffer);
165 return USB_STOR_TRANSPORT_GOOD;
167 leave:
168 kfree(buffer);
169 return USB_STOR_TRANSPORT_ERROR;
173 static int datafab_write_data(struct us_data *us,
174 struct datafab_info *info,
175 u32 sector,
176 u32 sectors)
178 unsigned char *command = us->iobuf;
179 unsigned char *reply = us->iobuf;
180 unsigned char *buffer;
181 unsigned char thistime;
182 unsigned int totallen, alloclen;
183 int len, result;
184 unsigned int sg_idx = 0, sg_offset = 0;
186 // we're working in LBA mode. according to the ATA spec,
187 // we can support up to 28-bit addressing. I don't know if Datafab
188 // supports beyond 24-bit addressing. It's kind of hard to test
189 // since it requires > 8GB CF card.
191 if (sectors > 0x0FFFFFFF)
192 return USB_STOR_TRANSPORT_ERROR;
194 if (info->lun == -1) {
195 result = datafab_determine_lun(us, info);
196 if (result != USB_STOR_TRANSPORT_GOOD)
197 return result;
200 totallen = sectors * info->ssize;
202 // Since we don't write more than 64 KB at a time, we have to create
203 // a bounce buffer and move the data a piece at a time between the
204 // bounce buffer and the actual transfer buffer.
206 alloclen = min(totallen, 65536u);
207 buffer = kmalloc(alloclen, GFP_NOIO);
208 if (buffer == NULL)
209 return USB_STOR_TRANSPORT_ERROR;
211 do {
212 // loop, never allocate or transfer more than 64k at once
213 // (min(128k, 255*info->ssize) is the real limit)
215 len = min(totallen, alloclen);
216 thistime = (len / info->ssize) & 0xff;
218 // Get the data from the transfer buffer
219 usb_stor_access_xfer_buf(buffer, len, us->srb,
220 &sg_idx, &sg_offset, FROM_XFER_BUF);
222 command[0] = 0;
223 command[1] = thistime;
224 command[2] = sector & 0xFF;
225 command[3] = (sector >> 8) & 0xFF;
226 command[4] = (sector >> 16) & 0xFF;
228 command[5] = 0xE0 + (info->lun << 4);
229 command[5] |= (sector >> 24) & 0x0F;
230 command[6] = 0x30;
231 command[7] = 0x02;
233 // send the command
234 result = datafab_bulk_write(us, command, 8);
235 if (result != USB_STOR_XFER_GOOD)
236 goto leave;
238 // send the data
239 result = datafab_bulk_write(us, buffer, len);
240 if (result != USB_STOR_XFER_GOOD)
241 goto leave;
243 // read the result
244 result = datafab_bulk_read(us, reply, 2);
245 if (result != USB_STOR_XFER_GOOD)
246 goto leave;
248 if (reply[0] != 0x50 && reply[1] != 0) {
249 US_DEBUGP("datafab_write_data: Gah! "
250 "write return code: %02x %02x\n",
251 reply[0], reply[1]);
252 result = USB_STOR_TRANSPORT_ERROR;
253 goto leave;
256 sector += thistime;
257 totallen -= len;
258 } while (totallen > 0);
260 kfree(buffer);
261 return USB_STOR_TRANSPORT_GOOD;
263 leave:
264 kfree(buffer);
265 return USB_STOR_TRANSPORT_ERROR;
269 static int datafab_determine_lun(struct us_data *us,
270 struct datafab_info *info)
272 // Dual-slot readers can be thought of as dual-LUN devices.
273 // We need to determine which card slot is being used.
274 // We'll send an IDENTIFY DEVICE command and see which LUN responds...
276 // There might be a better way of doing this?
278 static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
279 unsigned char *command = us->iobuf;
280 unsigned char *buf;
281 int count = 0, rc;
283 if (!us || !info)
284 return USB_STOR_TRANSPORT_ERROR;
286 memcpy(command, scommand, 8);
287 buf = kmalloc(512, GFP_NOIO);
288 if (!buf)
289 return USB_STOR_TRANSPORT_ERROR;
291 US_DEBUGP("datafab_determine_lun: locating...\n");
293 // we'll try 3 times before giving up...
295 while (count++ < 3) {
296 command[5] = 0xa0;
298 rc = datafab_bulk_write(us, command, 8);
299 if (rc != USB_STOR_XFER_GOOD) {
300 rc = USB_STOR_TRANSPORT_ERROR;
301 goto leave;
304 rc = datafab_bulk_read(us, buf, 512);
305 if (rc == USB_STOR_XFER_GOOD) {
306 info->lun = 0;
307 rc = USB_STOR_TRANSPORT_GOOD;
308 goto leave;
311 command[5] = 0xb0;
313 rc = datafab_bulk_write(us, command, 8);
314 if (rc != USB_STOR_XFER_GOOD) {
315 rc = USB_STOR_TRANSPORT_ERROR;
316 goto leave;
319 rc = datafab_bulk_read(us, buf, 512);
320 if (rc == USB_STOR_XFER_GOOD) {
321 info->lun = 1;
322 rc = USB_STOR_TRANSPORT_GOOD;
323 goto leave;
326 msleep(20);
329 rc = USB_STOR_TRANSPORT_ERROR;
331 leave:
332 kfree(buf);
333 return rc;
336 static int datafab_id_device(struct us_data *us,
337 struct datafab_info *info)
339 // this is a variation of the ATA "IDENTIFY DEVICE" command...according
340 // to the ATA spec, 'Sector Count' isn't used but the Windows driver
341 // sets this bit so we do too...
343 static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
344 unsigned char *command = us->iobuf;
345 unsigned char *reply;
346 int rc;
348 if (!us || !info)
349 return USB_STOR_TRANSPORT_ERROR;
351 if (info->lun == -1) {
352 rc = datafab_determine_lun(us, info);
353 if (rc != USB_STOR_TRANSPORT_GOOD)
354 return rc;
357 memcpy(command, scommand, 8);
358 reply = kmalloc(512, GFP_NOIO);
359 if (!reply)
360 return USB_STOR_TRANSPORT_ERROR;
362 command[5] += (info->lun << 4);
364 rc = datafab_bulk_write(us, command, 8);
365 if (rc != USB_STOR_XFER_GOOD) {
366 rc = USB_STOR_TRANSPORT_ERROR;
367 goto leave;
370 // we'll go ahead and extract the media capacity while we're here...
372 rc = datafab_bulk_read(us, reply, 512);
373 if (rc == USB_STOR_XFER_GOOD) {
374 // capacity is at word offset 57-58
376 info->sectors = ((u32)(reply[117]) << 24) |
377 ((u32)(reply[116]) << 16) |
378 ((u32)(reply[115]) << 8) |
379 ((u32)(reply[114]) );
380 rc = USB_STOR_TRANSPORT_GOOD;
381 goto leave;
384 rc = USB_STOR_TRANSPORT_ERROR;
386 leave:
387 kfree(reply);
388 return rc;
392 static int datafab_handle_mode_sense(struct us_data *us,
393 struct scsi_cmnd * srb,
394 int sense_6)
396 static unsigned char rw_err_page[12] = {
397 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
399 static unsigned char cache_page[12] = {
400 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
402 static unsigned char rbac_page[12] = {
403 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
405 static unsigned char timer_page[8] = {
406 0x1C, 0x6, 0, 0, 0, 0
408 unsigned char pc, page_code;
409 unsigned int i = 0;
410 struct datafab_info *info = (struct datafab_info *) (us->extra);
411 unsigned char *ptr = us->iobuf;
413 // most of this stuff is just a hack to get things working. the
414 // datafab reader doesn't present a SCSI interface so we
415 // fudge the SCSI commands...
418 pc = srb->cmnd[2] >> 6;
419 page_code = srb->cmnd[2] & 0x3F;
421 switch (pc) {
422 case 0x0:
423 US_DEBUGP("datafab_handle_mode_sense: Current values\n");
424 break;
425 case 0x1:
426 US_DEBUGP("datafab_handle_mode_sense: Changeable values\n");
427 break;
428 case 0x2:
429 US_DEBUGP("datafab_handle_mode_sense: Default values\n");
430 break;
431 case 0x3:
432 US_DEBUGP("datafab_handle_mode_sense: Saves values\n");
433 break;
436 memset(ptr, 0, 8);
437 if (sense_6) {
438 ptr[2] = 0x00; // WP enable: 0x80
439 i = 4;
440 } else {
441 ptr[3] = 0x00; // WP enable: 0x80
442 i = 8;
445 switch (page_code) {
446 default:
447 // vendor-specific mode
448 info->sense_key = 0x05;
449 info->sense_asc = 0x24;
450 info->sense_ascq = 0x00;
451 return USB_STOR_TRANSPORT_FAILED;
453 case 0x1:
454 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
455 i += sizeof(rw_err_page);
456 break;
458 case 0x8:
459 memcpy(ptr + i, cache_page, sizeof(cache_page));
460 i += sizeof(cache_page);
461 break;
463 case 0x1B:
464 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
465 i += sizeof(rbac_page);
466 break;
468 case 0x1C:
469 memcpy(ptr + i, timer_page, sizeof(timer_page));
470 i += sizeof(timer_page);
471 break;
473 case 0x3F: // retrieve all pages
474 memcpy(ptr + i, timer_page, sizeof(timer_page));
475 i += sizeof(timer_page);
476 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
477 i += sizeof(rbac_page);
478 memcpy(ptr + i, cache_page, sizeof(cache_page));
479 i += sizeof(cache_page);
480 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
481 i += sizeof(rw_err_page);
482 break;
485 if (sense_6)
486 ptr[0] = i - 1;
487 else
488 ((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
489 usb_stor_set_xfer_buf(ptr, i, srb);
491 return USB_STOR_TRANSPORT_GOOD;
494 static void datafab_info_destructor(void *extra)
496 // this routine is a placeholder...
497 // currently, we don't allocate any extra memory so we're okay
501 // Transport for the Datafab MDCFE-B
503 int datafab_transport(struct scsi_cmnd * srb, struct us_data *us)
505 struct datafab_info *info;
506 int rc;
507 unsigned long block, blocks;
508 unsigned char *ptr = us->iobuf;
509 static unsigned char inquiry_reply[8] = {
510 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
513 if (!us->extra) {
514 us->extra = kzalloc(sizeof(struct datafab_info), GFP_NOIO);
515 if (!us->extra) {
516 US_DEBUGP("datafab_transport: Gah! "
517 "Can't allocate storage for Datafab info struct!\n");
518 return USB_STOR_TRANSPORT_ERROR;
520 us->extra_destructor = datafab_info_destructor;
521 ((struct datafab_info *)us->extra)->lun = -1;
524 info = (struct datafab_info *) (us->extra);
526 if (srb->cmnd[0] == INQUIRY) {
527 US_DEBUGP("datafab_transport: INQUIRY. Returning bogus response");
528 memcpy(ptr, inquiry_reply, sizeof(inquiry_reply));
529 fill_inquiry_response(us, ptr, 36);
530 return USB_STOR_TRANSPORT_GOOD;
533 if (srb->cmnd[0] == READ_CAPACITY) {
534 info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
535 rc = datafab_id_device(us, info);
536 if (rc != USB_STOR_TRANSPORT_GOOD)
537 return rc;
539 US_DEBUGP("datafab_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
540 info->sectors, info->ssize);
542 // build the reply
543 // we need the last sector, not the number of sectors
544 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
545 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
546 usb_stor_set_xfer_buf(ptr, 8, srb);
548 return USB_STOR_TRANSPORT_GOOD;
551 if (srb->cmnd[0] == MODE_SELECT_10) {
552 US_DEBUGP("datafab_transport: Gah! MODE_SELECT_10.\n");
553 return USB_STOR_TRANSPORT_ERROR;
556 // don't bother implementing READ_6 or WRITE_6.
558 if (srb->cmnd[0] == READ_10) {
559 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
560 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
562 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
564 US_DEBUGP("datafab_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
565 return datafab_read_data(us, info, block, blocks);
568 if (srb->cmnd[0] == READ_12) {
569 // we'll probably never see a READ_12 but we'll do it anyway...
571 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
572 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
574 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
575 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
577 US_DEBUGP("datafab_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
578 return datafab_read_data(us, info, block, blocks);
581 if (srb->cmnd[0] == WRITE_10) {
582 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
583 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
585 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
587 US_DEBUGP("datafab_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
588 return datafab_write_data(us, info, block, blocks);
591 if (srb->cmnd[0] == WRITE_12) {
592 // we'll probably never see a WRITE_12 but we'll do it anyway...
594 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
595 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
597 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
598 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
600 US_DEBUGP("datafab_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
601 return datafab_write_data(us, info, block, blocks);
604 if (srb->cmnd[0] == TEST_UNIT_READY) {
605 US_DEBUGP("datafab_transport: TEST_UNIT_READY.\n");
606 return datafab_id_device(us, info);
609 if (srb->cmnd[0] == REQUEST_SENSE) {
610 US_DEBUGP("datafab_transport: REQUEST_SENSE. Returning faked response\n");
612 // this response is pretty bogus right now. eventually if necessary
613 // we can set the correct sense data. so far though it hasn't been
614 // necessary
616 memset(ptr, 0, 18);
617 ptr[0] = 0xF0;
618 ptr[2] = info->sense_key;
619 ptr[7] = 11;
620 ptr[12] = info->sense_asc;
621 ptr[13] = info->sense_ascq;
622 usb_stor_set_xfer_buf(ptr, 18, srb);
624 return USB_STOR_TRANSPORT_GOOD;
627 if (srb->cmnd[0] == MODE_SENSE) {
628 US_DEBUGP("datafab_transport: MODE_SENSE_6 detected\n");
629 return datafab_handle_mode_sense(us, srb, 1);
632 if (srb->cmnd[0] == MODE_SENSE_10) {
633 US_DEBUGP("datafab_transport: MODE_SENSE_10 detected\n");
634 return datafab_handle_mode_sense(us, srb, 0);
637 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
638 // sure. whatever. not like we can stop the user from
639 // popping the media out of the device (no locking doors, etc)
641 return USB_STOR_TRANSPORT_GOOD;
644 if (srb->cmnd[0] == START_STOP) {
645 /* this is used by sd.c'check_scsidisk_media_change to detect
646 media change */
647 US_DEBUGP("datafab_transport: START_STOP.\n");
648 /* the first datafab_id_device after a media change returns
649 an error (determined experimentally) */
650 rc = datafab_id_device(us, info);
651 if (rc == USB_STOR_TRANSPORT_GOOD) {
652 info->sense_key = NO_SENSE;
653 srb->result = SUCCESS;
654 } else {
655 info->sense_key = UNIT_ATTENTION;
656 srb->result = SAM_STAT_CHECK_CONDITION;
658 return rc;
661 US_DEBUGP("datafab_transport: Gah! Unknown command: %d (0x%x)\n",
662 srb->cmnd[0], srb->cmnd[0]);
663 info->sense_key = 0x05;
664 info->sense_asc = 0x20;
665 info->sense_ascq = 0x00;
666 return USB_STOR_TRANSPORT_FAILED;