RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / usb / storage / shuttle_usbat.c
bloba0ff394bf57af952866db8aa06cc44c01fe221f9
1 /* Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable
3 * $Id: shuttle_usbat.c,v 1.17 2002/04/22 03:39:43 mdharm Exp $
5 * Current development and maintenance by:
6 * (c) 2000, 2001 Robert Baruch (autophile@starband.net)
7 * (c) 2004, 2005 Daniel Drake <dsd@gentoo.org>
9 * Developed with the assistance of:
10 * (c) 2002 Alan Stern <stern@rowland.org>
12 * Flash support based on earlier work by:
13 * (c) 2002 Thomas Kreiling <usbdev@sm04.de>
15 * Many originally ATAPI devices were slightly modified to meet the USB
16 * market by using some kind of translation from ATAPI to USB on the host,
17 * and the peripheral would translate from USB back to ATAPI.
19 * SCM Microsystems (www.scmmicro.com) makes a device, sold to OEM's only,
20 * which does the USB-to-ATAPI conversion. By obtaining the data sheet on
21 * their device under nondisclosure agreement, I have been able to write
22 * this driver for Linux.
24 * The chip used in the device can also be used for EPP and ISA translation
25 * as well. This driver is only guaranteed to work with the ATAPI
26 * translation.
28 * See the Kconfig help text for a list of devices known to be supported by
29 * this driver.
31 * This program is free software; you can redistribute it and/or modify it
32 * under the terms of the GNU General Public License as published by the
33 * Free Software Foundation; either version 2, or (at your option) any
34 * later version.
36 * This program is distributed in the hope that it will be useful, but
37 * WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 * General Public License for more details.
41 * You should have received a copy of the GNU General Public License along
42 * with this program; if not, write to the Free Software Foundation, Inc.,
43 * 675 Mass Ave, Cambridge, MA 02139, USA.
46 #include <linux/errno.h>
47 #include <linux/slab.h>
48 #include <linux/cdrom.h>
50 #include <scsi/scsi.h>
51 #include <scsi/scsi_cmnd.h>
53 #include "usb.h"
54 #include "transport.h"
55 #include "protocol.h"
56 #include "debug.h"
57 #include "shuttle_usbat.h"
59 #define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
60 #define LSB_of(s) ((s)&0xFF)
61 #define MSB_of(s) ((s)>>8)
63 static int transferred = 0;
65 static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us);
66 static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us);
69 * Convenience function to produce an ATA read/write sectors command
70 * Use cmd=0x20 for read, cmd=0x30 for write
72 static void usbat_pack_ata_sector_cmd(unsigned char *buf,
73 unsigned char thistime,
74 u32 sector, unsigned char cmd)
76 buf[0] = 0;
77 buf[1] = thistime;
78 buf[2] = sector & 0xFF;
79 buf[3] = (sector >> 8) & 0xFF;
80 buf[4] = (sector >> 16) & 0xFF;
81 buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
82 buf[6] = cmd;
86 * Convenience function to get the device type (flash or hp8200)
88 static int usbat_get_device_type(struct us_data *us)
90 return ((struct usbat_info*)us->extra)->devicetype;
94 * Read a register from the device
96 static int usbat_read(struct us_data *us,
97 unsigned char access,
98 unsigned char reg,
99 unsigned char *content)
101 return usb_stor_ctrl_transfer(us,
102 us->recv_ctrl_pipe,
103 access | USBAT_CMD_READ_REG,
104 0xC0,
105 (u16)reg,
107 content,
112 * Write to a register on the device
114 static int usbat_write(struct us_data *us,
115 unsigned char access,
116 unsigned char reg,
117 unsigned char content)
119 return usb_stor_ctrl_transfer(us,
120 us->send_ctrl_pipe,
121 access | USBAT_CMD_WRITE_REG,
122 0x40,
123 short_pack(reg, content),
125 NULL,
130 * Convenience function to perform a bulk read
132 static int usbat_bulk_read(struct us_data *us,
133 unsigned char *data,
134 unsigned int len,
135 int use_sg)
137 if (len == 0)
138 return USB_STOR_XFER_GOOD;
140 US_DEBUGP("usbat_bulk_read: len = %d\n", len);
141 return usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe, data, len, use_sg, NULL);
145 * Convenience function to perform a bulk write
147 static int usbat_bulk_write(struct us_data *us,
148 unsigned char *data,
149 unsigned int len,
150 int use_sg)
152 if (len == 0)
153 return USB_STOR_XFER_GOOD;
155 US_DEBUGP("usbat_bulk_write: len = %d\n", len);
156 return usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe, data, len, use_sg, NULL);
160 * Some USBAT-specific commands can only be executed over a command transport
161 * This transport allows one (len=8) or two (len=16) vendor-specific commands
162 * to be executed.
164 static int usbat_execute_command(struct us_data *us,
165 unsigned char *commands,
166 unsigned int len)
168 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
169 USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
170 commands, len);
174 * Read the status register
176 static int usbat_get_status(struct us_data *us, unsigned char *status)
178 int rc;
179 rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
181 US_DEBUGP("usbat_get_status: 0x%02X\n", (unsigned short) (*status));
182 return rc;
186 * Check the device status
188 static int usbat_check_status(struct us_data *us)
190 unsigned char *reply = us->iobuf;
191 int rc;
193 if (!us)
194 return USB_STOR_TRANSPORT_ERROR;
196 rc = usbat_get_status(us, reply);
197 if (rc != USB_STOR_XFER_GOOD)
198 return USB_STOR_TRANSPORT_FAILED;
200 /* error/check condition (0x51 is ok) */
201 if (*reply & 0x01 && *reply != 0x51)
202 return USB_STOR_TRANSPORT_FAILED;
204 /* device fault */
205 if (*reply & 0x20)
206 return USB_STOR_TRANSPORT_FAILED;
208 return USB_STOR_TRANSPORT_GOOD;
212 * Stores critical information in internal registers in prepartion for the execution
213 * of a conditional usbat_read_blocks or usbat_write_blocks call.
215 static int usbat_set_shuttle_features(struct us_data *us,
216 unsigned char external_trigger,
217 unsigned char epp_control,
218 unsigned char mask_byte,
219 unsigned char test_pattern,
220 unsigned char subcountH,
221 unsigned char subcountL)
223 unsigned char *command = us->iobuf;
225 command[0] = 0x40;
226 command[1] = USBAT_CMD_SET_FEAT;
229 * The only bit relevant to ATA access is bit 6
230 * which defines 8 bit data access (set) or 16 bit (unset)
232 command[2] = epp_control;
235 * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
236 * ET1 and ET2 define an external event to be checked for on event of a
237 * _read_blocks or _write_blocks operation. The read/write will not take
238 * place unless the defined trigger signal is active.
240 command[3] = external_trigger;
243 * The resultant byte of the mask operation (see mask_byte) is compared for
244 * equivalence with this test pattern. If equal, the read/write will take
245 * place.
247 command[4] = test_pattern;
250 * This value is logically ANDed with the status register field specified
251 * in the read/write command.
253 command[5] = mask_byte;
256 * If ALQ is set in the qualifier, this field contains the address of the
257 * registers where the byte count should be read for transferring the data.
258 * If ALQ is not set, then this field contains the number of bytes to be
259 * transferred.
261 command[6] = subcountL;
262 command[7] = subcountH;
264 return usbat_execute_command(us, command, 8);
268 * Block, waiting for an ATA device to become not busy or to report
269 * an error condition.
271 static int usbat_wait_not_busy(struct us_data *us, int minutes)
273 int i;
274 int result;
275 unsigned char *status = us->iobuf;
277 /* Synchronizing cache on a CDR could take a heck of a long time,
278 * but probably not more than 10 minutes or so. On the other hand,
279 * doing a full blank on a CDRW at speed 1 will take about 75
280 * minutes!
283 for (i=0; i<1200+minutes*60; i++) {
285 result = usbat_get_status(us, status);
287 if (result!=USB_STOR_XFER_GOOD)
288 return USB_STOR_TRANSPORT_ERROR;
289 if (*status & 0x01) { /* check condition */
290 result = usbat_read(us, USBAT_ATA, 0x10, status);
291 return USB_STOR_TRANSPORT_FAILED;
293 if (*status & 0x20) /* device fault */
294 return USB_STOR_TRANSPORT_FAILED;
296 if ((*status & 0x80)==0x00) { /* not busy */
297 US_DEBUGP("Waited not busy for %d steps\n", i);
298 return USB_STOR_TRANSPORT_GOOD;
301 if (i<500)
302 msleep(10); /* 5 seconds */
303 else if (i<700)
304 msleep(50); /* 10 seconds */
305 else if (i<1200)
306 msleep(100); /* 50 seconds */
307 else
308 msleep(1000); /* X minutes */
311 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
312 minutes);
313 return USB_STOR_TRANSPORT_FAILED;
317 * Read block data from the data register
319 static int usbat_read_block(struct us_data *us,
320 unsigned char *content,
321 unsigned short len,
322 int use_sg)
324 int result;
325 unsigned char *command = us->iobuf;
327 if (!len)
328 return USB_STOR_TRANSPORT_GOOD;
330 command[0] = 0xC0;
331 command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
332 command[2] = USBAT_ATA_DATA;
333 command[3] = 0;
334 command[4] = 0;
335 command[5] = 0;
336 command[6] = LSB_of(len);
337 command[7] = MSB_of(len);
339 result = usbat_execute_command(us, command, 8);
340 if (result != USB_STOR_XFER_GOOD)
341 return USB_STOR_TRANSPORT_ERROR;
343 result = usbat_bulk_read(us, content, len, use_sg);
344 return (result == USB_STOR_XFER_GOOD ?
345 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
349 * Write block data via the data register
351 static int usbat_write_block(struct us_data *us,
352 unsigned char access,
353 unsigned char *content,
354 unsigned short len,
355 int minutes,
356 int use_sg)
358 int result;
359 unsigned char *command = us->iobuf;
361 if (!len)
362 return USB_STOR_TRANSPORT_GOOD;
364 command[0] = 0x40;
365 command[1] = access | USBAT_CMD_WRITE_BLOCK;
366 command[2] = USBAT_ATA_DATA;
367 command[3] = 0;
368 command[4] = 0;
369 command[5] = 0;
370 command[6] = LSB_of(len);
371 command[7] = MSB_of(len);
373 result = usbat_execute_command(us, command, 8);
375 if (result != USB_STOR_XFER_GOOD)
376 return USB_STOR_TRANSPORT_ERROR;
378 result = usbat_bulk_write(us, content, len, use_sg);
379 if (result != USB_STOR_XFER_GOOD)
380 return USB_STOR_TRANSPORT_ERROR;
382 return usbat_wait_not_busy(us, minutes);
386 * Process read and write requests
388 static int usbat_hp8200e_rw_block_test(struct us_data *us,
389 unsigned char access,
390 unsigned char *registers,
391 unsigned char *data_out,
392 unsigned short num_registers,
393 unsigned char data_reg,
394 unsigned char status_reg,
395 unsigned char timeout,
396 unsigned char qualifier,
397 int direction,
398 unsigned char *content,
399 unsigned short len,
400 int use_sg,
401 int minutes)
403 int result;
404 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
405 us->recv_bulk_pipe : us->send_bulk_pipe;
407 unsigned char *command = us->iobuf;
408 int i, j;
409 int cmdlen;
410 unsigned char *data = us->iobuf;
411 unsigned char *status = us->iobuf;
413 BUG_ON(num_registers > US_IOBUF_SIZE/2);
415 for (i=0; i<20; i++) {
418 * The first time we send the full command, which consists
419 * of downloading the SCSI command followed by downloading
420 * the data via a write-and-test. Any other time we only
421 * send the command to download the data -- the SCSI command
422 * is still 'active' in some sense in the device.
424 * We're only going to try sending the data 10 times. After
425 * that, we just return a failure.
428 if (i==0) {
429 cmdlen = 16;
431 * Write to multiple registers
432 * Not really sure the 0x07, 0x17, 0xfc, 0xe7 is
433 * necessary here, but that's what came out of the
434 * trace every single time.
436 command[0] = 0x40;
437 command[1] = access | USBAT_CMD_WRITE_REGS;
438 command[2] = 0x07;
439 command[3] = 0x17;
440 command[4] = 0xFC;
441 command[5] = 0xE7;
442 command[6] = LSB_of(num_registers*2);
443 command[7] = MSB_of(num_registers*2);
444 } else
445 cmdlen = 8;
447 /* Conditionally read or write blocks */
448 command[cmdlen-8] = (direction==DMA_TO_DEVICE ? 0x40 : 0xC0);
449 command[cmdlen-7] = access |
450 (direction==DMA_TO_DEVICE ?
451 USBAT_CMD_COND_WRITE_BLOCK : USBAT_CMD_COND_READ_BLOCK);
452 command[cmdlen-6] = data_reg;
453 command[cmdlen-5] = status_reg;
454 command[cmdlen-4] = timeout;
455 command[cmdlen-3] = qualifier;
456 command[cmdlen-2] = LSB_of(len);
457 command[cmdlen-1] = MSB_of(len);
459 result = usbat_execute_command(us, command, cmdlen);
461 if (result != USB_STOR_XFER_GOOD)
462 return USB_STOR_TRANSPORT_ERROR;
464 if (i==0) {
466 for (j=0; j<num_registers; j++) {
467 data[j<<1] = registers[j];
468 data[1+(j<<1)] = data_out[j];
471 result = usbat_bulk_write(us, data, num_registers*2, 0);
472 if (result != USB_STOR_XFER_GOOD)
473 return USB_STOR_TRANSPORT_ERROR;
477 result = usb_stor_bulk_transfer_sg(us,
478 pipe, content, len, use_sg, NULL);
481 * If we get a stall on the bulk download, we'll retry
482 * the bulk download -- but not the SCSI command because
483 * in some sense the SCSI command is still 'active' and
484 * waiting for the data. Don't ask me why this should be;
485 * I'm only following what the Windoze driver did.
487 * Note that a stall for the test-and-read/write command means
488 * that the test failed. In this case we're testing to make
489 * sure that the device is error-free
490 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
491 * hypothesis is that the USBAT chip somehow knows what
492 * the device will accept, but doesn't give the device any
493 * data until all data is received. Thus, the device would
494 * still be waiting for the first byte of data if a stall
495 * occurs, even if the stall implies that some data was
496 * transferred.
499 if (result == USB_STOR_XFER_SHORT ||
500 result == USB_STOR_XFER_STALLED) {
503 * If we're reading and we stalled, then clear
504 * the bulk output pipe only the first time.
507 if (direction==DMA_FROM_DEVICE && i==0) {
508 if (usb_stor_clear_halt(us,
509 us->send_bulk_pipe) < 0)
510 return USB_STOR_TRANSPORT_ERROR;
514 * Read status: is the device angry, or just busy?
517 result = usbat_read(us, USBAT_ATA,
518 direction==DMA_TO_DEVICE ?
519 USBAT_ATA_STATUS : USBAT_ATA_ALTSTATUS,
520 status);
522 if (result!=USB_STOR_XFER_GOOD)
523 return USB_STOR_TRANSPORT_ERROR;
524 if (*status & 0x01) /* check condition */
525 return USB_STOR_TRANSPORT_FAILED;
526 if (*status & 0x20) /* device fault */
527 return USB_STOR_TRANSPORT_FAILED;
529 US_DEBUGP("Redoing %s\n",
530 direction==DMA_TO_DEVICE ? "write" : "read");
532 } else if (result != USB_STOR_XFER_GOOD)
533 return USB_STOR_TRANSPORT_ERROR;
534 else
535 return usbat_wait_not_busy(us, minutes);
539 US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
540 direction==DMA_TO_DEVICE ? "Writing" : "Reading");
542 return USB_STOR_TRANSPORT_FAILED;
546 * Write to multiple registers:
547 * Allows us to write specific data to any registers. The data to be written
548 * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN
549 * which gets sent through bulk out.
550 * Not designed for large transfers of data!
552 static int usbat_multiple_write(struct us_data *us,
553 unsigned char *registers,
554 unsigned char *data_out,
555 unsigned short num_registers)
557 int i, result;
558 unsigned char *data = us->iobuf;
559 unsigned char *command = us->iobuf;
561 BUG_ON(num_registers > US_IOBUF_SIZE/2);
563 /* Write to multiple registers, ATA access */
564 command[0] = 0x40;
565 command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
567 /* No relevance */
568 command[2] = 0;
569 command[3] = 0;
570 command[4] = 0;
571 command[5] = 0;
573 /* Number of bytes to be transferred (incl. addresses and data) */
574 command[6] = LSB_of(num_registers*2);
575 command[7] = MSB_of(num_registers*2);
577 /* The setup command */
578 result = usbat_execute_command(us, command, 8);
579 if (result != USB_STOR_XFER_GOOD)
580 return USB_STOR_TRANSPORT_ERROR;
582 /* Create the reg/data, reg/data sequence */
583 for (i=0; i<num_registers; i++) {
584 data[i<<1] = registers[i];
585 data[1+(i<<1)] = data_out[i];
588 /* Send the data */
589 result = usbat_bulk_write(us, data, num_registers*2, 0);
590 if (result != USB_STOR_XFER_GOOD)
591 return USB_STOR_TRANSPORT_ERROR;
593 if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
594 return usbat_wait_not_busy(us, 0);
595 else
596 return USB_STOR_TRANSPORT_GOOD;
600 * Conditionally read blocks from device:
601 * Allows us to read blocks from a specific data register, based upon the
602 * condition that a status register can be successfully masked with a status
603 * qualifier. If this condition is not initially met, the read will wait
604 * up until a maximum amount of time has elapsed, as specified by timeout.
605 * The read will start when the condition is met, otherwise the command aborts.
607 * The qualifier defined here is not the value that is masked, it defines
608 * conditions for the write to take place. The actual masked qualifier (and
609 * other related details) are defined beforehand with _set_shuttle_features().
611 static int usbat_read_blocks(struct us_data *us,
612 unsigned char *buffer,
613 int len,
614 int use_sg)
616 int result;
617 unsigned char *command = us->iobuf;
619 command[0] = 0xC0;
620 command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK;
621 command[2] = USBAT_ATA_DATA;
622 command[3] = USBAT_ATA_STATUS;
623 command[4] = 0xFD; /* Timeout (ms); */
624 command[5] = USBAT_QUAL_FCQ;
625 command[6] = LSB_of(len);
626 command[7] = MSB_of(len);
628 /* Multiple block read setup command */
629 result = usbat_execute_command(us, command, 8);
630 if (result != USB_STOR_XFER_GOOD)
631 return USB_STOR_TRANSPORT_FAILED;
633 /* Read the blocks we just asked for */
634 result = usbat_bulk_read(us, buffer, len, use_sg);
635 if (result != USB_STOR_XFER_GOOD)
636 return USB_STOR_TRANSPORT_FAILED;
638 return USB_STOR_TRANSPORT_GOOD;
642 * Conditionally write blocks to device:
643 * Allows us to write blocks to a specific data register, based upon the
644 * condition that a status register can be successfully masked with a status
645 * qualifier. If this condition is not initially met, the write will wait
646 * up until a maximum amount of time has elapsed, as specified by timeout.
647 * The read will start when the condition is met, otherwise the command aborts.
649 * The qualifier defined here is not the value that is masked, it defines
650 * conditions for the write to take place. The actual masked qualifier (and
651 * other related details) are defined beforehand with _set_shuttle_features().
653 static int usbat_write_blocks(struct us_data *us,
654 unsigned char *buffer,
655 int len,
656 int use_sg)
658 int result;
659 unsigned char *command = us->iobuf;
661 command[0] = 0x40;
662 command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK;
663 command[2] = USBAT_ATA_DATA;
664 command[3] = USBAT_ATA_STATUS;
665 command[4] = 0xFD; /* Timeout (ms) */
666 command[5] = USBAT_QUAL_FCQ;
667 command[6] = LSB_of(len);
668 command[7] = MSB_of(len);
670 /* Multiple block write setup command */
671 result = usbat_execute_command(us, command, 8);
672 if (result != USB_STOR_XFER_GOOD)
673 return USB_STOR_TRANSPORT_FAILED;
675 /* Write the data */
676 result = usbat_bulk_write(us, buffer, len, use_sg);
677 if (result != USB_STOR_XFER_GOOD)
678 return USB_STOR_TRANSPORT_FAILED;
680 return USB_STOR_TRANSPORT_GOOD;
684 * Read the User IO register
686 static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
688 int result;
690 result = usb_stor_ctrl_transfer(us,
691 us->recv_ctrl_pipe,
692 USBAT_CMD_UIO,
693 0xC0,
696 data_flags,
697 USBAT_UIO_READ);
699 US_DEBUGP("usbat_read_user_io: UIO register reads %02X\n", (unsigned short) (*data_flags));
701 return result;
705 * Write to the User IO register
707 static int usbat_write_user_io(struct us_data *us,
708 unsigned char enable_flags,
709 unsigned char data_flags)
711 return usb_stor_ctrl_transfer(us,
712 us->send_ctrl_pipe,
713 USBAT_CMD_UIO,
714 0x40,
715 short_pack(enable_flags, data_flags),
717 NULL,
718 USBAT_UIO_WRITE);
722 * Reset the device
723 * Often needed on media change.
725 static int usbat_device_reset(struct us_data *us)
727 int rc;
730 * Reset peripheral, enable peripheral control signals
731 * (bring reset signal up)
733 rc = usbat_write_user_io(us,
734 USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
735 USBAT_UIO_EPAD | USBAT_UIO_1);
736 if (rc != USB_STOR_XFER_GOOD)
737 return USB_STOR_TRANSPORT_ERROR;
740 * Enable peripheral control signals
741 * (bring reset signal down)
743 rc = usbat_write_user_io(us,
744 USBAT_UIO_OE1 | USBAT_UIO_OE0,
745 USBAT_UIO_EPAD | USBAT_UIO_1);
746 if (rc != USB_STOR_XFER_GOOD)
747 return USB_STOR_TRANSPORT_ERROR;
749 return USB_STOR_TRANSPORT_GOOD;
753 * Enable card detect
755 static int usbat_device_enable_cdt(struct us_data *us)
757 int rc;
759 /* Enable peripheral control signals and card detect */
760 rc = usbat_write_user_io(us,
761 USBAT_UIO_ACKD | USBAT_UIO_OE1 | USBAT_UIO_OE0,
762 USBAT_UIO_EPAD | USBAT_UIO_1);
763 if (rc != USB_STOR_XFER_GOOD)
764 return USB_STOR_TRANSPORT_ERROR;
766 return USB_STOR_TRANSPORT_GOOD;
770 * Determine if media is present.
772 static int usbat_flash_check_media_present(unsigned char *uio)
774 if (*uio & USBAT_UIO_UI0) {
775 US_DEBUGP("usbat_flash_check_media_present: no media detected\n");
776 return USBAT_FLASH_MEDIA_NONE;
779 return USBAT_FLASH_MEDIA_CF;
783 * Determine if media has changed since last operation
785 static int usbat_flash_check_media_changed(unsigned char *uio)
787 if (*uio & USBAT_UIO_0) {
788 US_DEBUGP("usbat_flash_check_media_changed: media change detected\n");
789 return USBAT_FLASH_MEDIA_CHANGED;
792 return USBAT_FLASH_MEDIA_SAME;
796 * Check for media change / no media and handle the situation appropriately
798 static int usbat_flash_check_media(struct us_data *us,
799 struct usbat_info *info)
801 int rc;
802 unsigned char *uio = us->iobuf;
804 rc = usbat_read_user_io(us, uio);
805 if (rc != USB_STOR_XFER_GOOD)
806 return USB_STOR_TRANSPORT_ERROR;
808 /* Check for media existence */
809 rc = usbat_flash_check_media_present(uio);
810 if (rc == USBAT_FLASH_MEDIA_NONE) {
811 info->sense_key = 0x02;
812 info->sense_asc = 0x3A;
813 info->sense_ascq = 0x00;
814 return USB_STOR_TRANSPORT_FAILED;
817 /* Check for media change */
818 rc = usbat_flash_check_media_changed(uio);
819 if (rc == USBAT_FLASH_MEDIA_CHANGED) {
821 /* Reset and re-enable card detect */
822 rc = usbat_device_reset(us);
823 if (rc != USB_STOR_TRANSPORT_GOOD)
824 return rc;
825 rc = usbat_device_enable_cdt(us);
826 if (rc != USB_STOR_TRANSPORT_GOOD)
827 return rc;
829 msleep(50);
831 rc = usbat_read_user_io(us, uio);
832 if (rc != USB_STOR_XFER_GOOD)
833 return USB_STOR_TRANSPORT_ERROR;
835 info->sense_key = UNIT_ATTENTION;
836 info->sense_asc = 0x28;
837 info->sense_ascq = 0x00;
838 return USB_STOR_TRANSPORT_FAILED;
841 return USB_STOR_TRANSPORT_GOOD;
845 * Determine whether we are controlling a flash-based reader/writer,
846 * or a HP8200-based CD drive.
847 * Sets transport functions as appropriate.
849 static int usbat_identify_device(struct us_data *us,
850 struct usbat_info *info)
852 int rc;
853 unsigned char status;
855 if (!us || !info)
856 return USB_STOR_TRANSPORT_ERROR;
858 rc = usbat_device_reset(us);
859 if (rc != USB_STOR_TRANSPORT_GOOD)
860 return rc;
861 msleep(500);
864 * In attempt to distinguish between HP CDRW's and Flash readers, we now
865 * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
866 * readers), this command should fail with error. On ATAPI devices (i.e.
867 * CDROM drives), it should succeed.
869 rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1);
870 if (rc != USB_STOR_XFER_GOOD)
871 return USB_STOR_TRANSPORT_ERROR;
873 rc = usbat_get_status(us, &status);
874 if (rc != USB_STOR_XFER_GOOD)
875 return USB_STOR_TRANSPORT_ERROR;
877 /* Check for error bit, or if the command 'fell through' */
878 if (status == 0xA1 || !(status & 0x01)) {
879 /* Device is HP 8200 */
880 US_DEBUGP("usbat_identify_device: Detected HP8200 CDRW\n");
881 info->devicetype = USBAT_DEV_HP8200;
882 } else {
883 /* Device is a CompactFlash reader/writer */
884 US_DEBUGP("usbat_identify_device: Detected Flash reader/writer\n");
885 info->devicetype = USBAT_DEV_FLASH;
888 return USB_STOR_TRANSPORT_GOOD;
892 * Set the transport function based on the device type
894 static int usbat_set_transport(struct us_data *us,
895 struct usbat_info *info,
896 int devicetype)
899 if (!info->devicetype)
900 info->devicetype = devicetype;
902 if (!info->devicetype)
903 usbat_identify_device(us, info);
905 switch (info->devicetype) {
906 default:
907 return USB_STOR_TRANSPORT_ERROR;
909 case USBAT_DEV_HP8200:
910 us->transport = usbat_hp8200e_transport;
911 break;
913 case USBAT_DEV_FLASH:
914 us->transport = usbat_flash_transport;
915 break;
918 return 0;
922 * Read the media capacity
924 static int usbat_flash_get_sector_count(struct us_data *us,
925 struct usbat_info *info)
927 unsigned char registers[3] = {
928 USBAT_ATA_SECCNT,
929 USBAT_ATA_DEVICE,
930 USBAT_ATA_CMD,
932 unsigned char command[3] = { 0x01, 0xA0, 0xEC };
933 unsigned char *reply;
934 unsigned char status;
935 int rc;
937 if (!us || !info)
938 return USB_STOR_TRANSPORT_ERROR;
940 reply = kmalloc(512, GFP_NOIO);
941 if (!reply)
942 return USB_STOR_TRANSPORT_ERROR;
944 /* ATA command : IDENTIFY DEVICE */
945 rc = usbat_multiple_write(us, registers, command, 3);
946 if (rc != USB_STOR_XFER_GOOD) {
947 US_DEBUGP("usbat_flash_get_sector_count: Gah! identify_device failed\n");
948 rc = USB_STOR_TRANSPORT_ERROR;
949 goto leave;
952 /* Read device status */
953 if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
954 rc = USB_STOR_TRANSPORT_ERROR;
955 goto leave;
958 msleep(100);
960 /* Read the device identification data */
961 rc = usbat_read_block(us, reply, 512, 0);
962 if (rc != USB_STOR_TRANSPORT_GOOD)
963 goto leave;
965 info->sectors = ((u32)(reply[117]) << 24) |
966 ((u32)(reply[116]) << 16) |
967 ((u32)(reply[115]) << 8) |
968 ((u32)(reply[114]) );
970 rc = USB_STOR_TRANSPORT_GOOD;
972 leave:
973 kfree(reply);
974 return rc;
978 * Read data from device
980 static int usbat_flash_read_data(struct us_data *us,
981 struct usbat_info *info,
982 u32 sector,
983 u32 sectors)
985 unsigned char registers[7] = {
986 USBAT_ATA_FEATURES,
987 USBAT_ATA_SECCNT,
988 USBAT_ATA_SECNUM,
989 USBAT_ATA_LBA_ME,
990 USBAT_ATA_LBA_HI,
991 USBAT_ATA_DEVICE,
992 USBAT_ATA_STATUS,
994 unsigned char command[7];
995 unsigned char *buffer;
996 unsigned char thistime;
997 unsigned int totallen, alloclen;
998 int len, result;
999 unsigned int sg_offset = 0;
1000 struct scatterlist *sg = NULL;
1002 result = usbat_flash_check_media(us, info);
1003 if (result != USB_STOR_TRANSPORT_GOOD)
1004 return result;
1007 * we're working in LBA mode. according to the ATA spec,
1008 * we can support up to 28-bit addressing. I don't know if Jumpshot
1009 * supports beyond 24-bit addressing. It's kind of hard to test
1010 * since it requires > 8GB CF card.
1013 if (sector > 0x0FFFFFFF)
1014 return USB_STOR_TRANSPORT_ERROR;
1016 totallen = sectors * info->ssize;
1019 * Since we don't read more than 64 KB at a time, we have to create
1020 * a bounce buffer and move the data a piece at a time between the
1021 * bounce buffer and the actual transfer buffer.
1024 alloclen = min(totallen, 65536u);
1025 buffer = kmalloc(alloclen, GFP_NOIO);
1026 if (buffer == NULL)
1027 return USB_STOR_TRANSPORT_ERROR;
1029 do {
1031 * loop, never allocate or transfer more than 64k at once
1032 * (min(128k, 255*info->ssize) is the real limit)
1034 len = min(totallen, alloclen);
1035 thistime = (len / info->ssize) & 0xff;
1037 /* ATA command 0x20 (READ SECTORS) */
1038 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
1040 /* Write/execute ATA read command */
1041 result = usbat_multiple_write(us, registers, command, 7);
1042 if (result != USB_STOR_TRANSPORT_GOOD)
1043 goto leave;
1045 /* Read the data we just requested */
1046 result = usbat_read_blocks(us, buffer, len, 0);
1047 if (result != USB_STOR_TRANSPORT_GOOD)
1048 goto leave;
1050 US_DEBUGP("usbat_flash_read_data: %d bytes\n", len);
1052 /* Store the data in the transfer buffer */
1053 usb_stor_access_xfer_buf(buffer, len, us->srb,
1054 &sg, &sg_offset, TO_XFER_BUF);
1056 sector += thistime;
1057 totallen -= len;
1058 } while (totallen > 0);
1060 kfree(buffer);
1061 return USB_STOR_TRANSPORT_GOOD;
1063 leave:
1064 kfree(buffer);
1065 return USB_STOR_TRANSPORT_ERROR;
1069 * Write data to device
1071 static int usbat_flash_write_data(struct us_data *us,
1072 struct usbat_info *info,
1073 u32 sector,
1074 u32 sectors)
1076 unsigned char registers[7] = {
1077 USBAT_ATA_FEATURES,
1078 USBAT_ATA_SECCNT,
1079 USBAT_ATA_SECNUM,
1080 USBAT_ATA_LBA_ME,
1081 USBAT_ATA_LBA_HI,
1082 USBAT_ATA_DEVICE,
1083 USBAT_ATA_STATUS,
1085 unsigned char command[7];
1086 unsigned char *buffer;
1087 unsigned char thistime;
1088 unsigned int totallen, alloclen;
1089 int len, result;
1090 unsigned int sg_offset = 0;
1091 struct scatterlist *sg = NULL;
1093 result = usbat_flash_check_media(us, info);
1094 if (result != USB_STOR_TRANSPORT_GOOD)
1095 return result;
1098 * we're working in LBA mode. according to the ATA spec,
1099 * we can support up to 28-bit addressing. I don't know if the device
1100 * supports beyond 24-bit addressing. It's kind of hard to test
1101 * since it requires > 8GB media.
1104 if (sector > 0x0FFFFFFF)
1105 return USB_STOR_TRANSPORT_ERROR;
1107 totallen = sectors * info->ssize;
1110 * Since we don't write more than 64 KB at a time, we have to create
1111 * a bounce buffer and move the data a piece at a time between the
1112 * bounce buffer and the actual transfer buffer.
1115 alloclen = min(totallen, 65536u);
1116 buffer = kmalloc(alloclen, GFP_NOIO);
1117 if (buffer == NULL)
1118 return USB_STOR_TRANSPORT_ERROR;
1120 do {
1122 * loop, never allocate or transfer more than 64k at once
1123 * (min(128k, 255*info->ssize) is the real limit)
1125 len = min(totallen, alloclen);
1126 thistime = (len / info->ssize) & 0xff;
1128 /* Get the data from the transfer buffer */
1129 usb_stor_access_xfer_buf(buffer, len, us->srb,
1130 &sg, &sg_offset, FROM_XFER_BUF);
1132 /* ATA command 0x30 (WRITE SECTORS) */
1133 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
1135 /* Write/execute ATA write command */
1136 result = usbat_multiple_write(us, registers, command, 7);
1137 if (result != USB_STOR_TRANSPORT_GOOD)
1138 goto leave;
1140 /* Write the data */
1141 result = usbat_write_blocks(us, buffer, len, 0);
1142 if (result != USB_STOR_TRANSPORT_GOOD)
1143 goto leave;
1145 sector += thistime;
1146 totallen -= len;
1147 } while (totallen > 0);
1149 kfree(buffer);
1150 return result;
1152 leave:
1153 kfree(buffer);
1154 return USB_STOR_TRANSPORT_ERROR;
1158 * Squeeze a potentially huge (> 65535 byte) read10 command into
1159 * a little ( <= 65535 byte) ATAPI pipe
1161 static int usbat_hp8200e_handle_read10(struct us_data *us,
1162 unsigned char *registers,
1163 unsigned char *data,
1164 struct scsi_cmnd *srb)
1166 int result = USB_STOR_TRANSPORT_GOOD;
1167 unsigned char *buffer;
1168 unsigned int len;
1169 unsigned int sector;
1170 unsigned int sg_offset = 0;
1171 struct scatterlist *sg = NULL;
1173 US_DEBUGP("handle_read10: transfersize %d\n",
1174 srb->transfersize);
1176 if (srb->request_bufflen < 0x10000) {
1178 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1179 registers, data, 19,
1180 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1181 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1182 DMA_FROM_DEVICE,
1183 srb->request_buffer,
1184 srb->request_bufflen, srb->use_sg, 1);
1186 return result;
1190 * Since we're requesting more data than we can handle in
1191 * a single read command (max is 64k-1), we will perform
1192 * multiple reads, but each read must be in multiples of
1193 * a sector. Luckily the sector size is in srb->transfersize
1194 * (see linux/drivers/scsi/sr.c).
1197 if (data[7+0] == GPCMD_READ_CD) {
1198 len = short_pack(data[7+9], data[7+8]);
1199 len <<= 16;
1200 len |= data[7+7];
1201 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
1202 srb->transfersize = srb->request_bufflen/len;
1205 if (!srb->transfersize) {
1206 srb->transfersize = 2048; /* A guess */
1207 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
1208 srb->transfersize);
1212 * Since we only read in one block at a time, we have to create
1213 * a bounce buffer and move the data a piece at a time between the
1214 * bounce buffer and the actual transfer buffer.
1217 len = (65535/srb->transfersize) * srb->transfersize;
1218 US_DEBUGP("Max read is %d bytes\n", len);
1219 len = min(len, srb->request_bufflen);
1220 buffer = kmalloc(len, GFP_NOIO);
1221 if (buffer == NULL) /* bloody hell! */
1222 return USB_STOR_TRANSPORT_FAILED;
1223 sector = short_pack(data[7+3], data[7+2]);
1224 sector <<= 16;
1225 sector |= short_pack(data[7+5], data[7+4]);
1226 transferred = 0;
1228 while (transferred != srb->request_bufflen) {
1230 if (len > srb->request_bufflen - transferred)
1231 len = srb->request_bufflen - transferred;
1233 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1234 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
1236 /* Fix up the SCSI command sector and num sectors */
1238 data[7+2] = MSB_of(sector>>16); /* SCSI command sector */
1239 data[7+3] = LSB_of(sector>>16);
1240 data[7+4] = MSB_of(sector&0xFFFF);
1241 data[7+5] = LSB_of(sector&0xFFFF);
1242 if (data[7+0] == GPCMD_READ_CD)
1243 data[7+6] = 0;
1244 data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */
1245 data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */
1247 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1248 registers, data, 19,
1249 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1250 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1251 DMA_FROM_DEVICE,
1252 buffer,
1253 len, 0, 1);
1255 if (result != USB_STOR_TRANSPORT_GOOD)
1256 break;
1258 /* Store the data in the transfer buffer */
1259 usb_stor_access_xfer_buf(buffer, len, srb,
1260 &sg, &sg_offset, TO_XFER_BUF);
1262 /* Update the amount transferred and the sector number */
1264 transferred += len;
1265 sector += len / srb->transfersize;
1267 } /* while transferred != srb->request_bufflen */
1269 kfree(buffer);
1270 return result;
1273 static int usbat_select_and_test_registers(struct us_data *us)
1275 int selector;
1276 unsigned char *status = us->iobuf;
1278 /* try device = master, then device = slave. */
1279 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
1280 if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
1281 USB_STOR_XFER_GOOD)
1282 return USB_STOR_TRANSPORT_ERROR;
1284 if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) !=
1285 USB_STOR_XFER_GOOD)
1286 return USB_STOR_TRANSPORT_ERROR;
1288 if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) !=
1289 USB_STOR_XFER_GOOD)
1290 return USB_STOR_TRANSPORT_ERROR;
1292 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1293 USB_STOR_XFER_GOOD)
1294 return USB_STOR_TRANSPORT_ERROR;
1296 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1297 USB_STOR_XFER_GOOD)
1298 return USB_STOR_TRANSPORT_ERROR;
1300 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) !=
1301 USB_STOR_XFER_GOOD)
1302 return USB_STOR_TRANSPORT_ERROR;
1304 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) !=
1305 USB_STOR_XFER_GOOD)
1306 return USB_STOR_TRANSPORT_ERROR;
1308 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1309 USB_STOR_XFER_GOOD)
1310 return USB_STOR_TRANSPORT_ERROR;
1312 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1313 USB_STOR_XFER_GOOD)
1314 return USB_STOR_TRANSPORT_ERROR;
1317 return USB_STOR_TRANSPORT_GOOD;
1321 * Initialize the USBAT processor and the storage device
1323 static int init_usbat(struct us_data *us, int devicetype)
1325 int rc;
1326 struct usbat_info *info;
1327 unsigned char subcountH = USBAT_ATA_LBA_HI;
1328 unsigned char subcountL = USBAT_ATA_LBA_ME;
1329 unsigned char *status = us->iobuf;
1331 us->extra = kzalloc(sizeof(struct usbat_info), GFP_NOIO);
1332 if (!us->extra) {
1333 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1334 return 1;
1336 info = (struct usbat_info *) (us->extra);
1338 /* Enable peripheral control signals */
1339 rc = usbat_write_user_io(us,
1340 USBAT_UIO_OE1 | USBAT_UIO_OE0,
1341 USBAT_UIO_EPAD | USBAT_UIO_1);
1342 if (rc != USB_STOR_XFER_GOOD)
1343 return USB_STOR_TRANSPORT_ERROR;
1345 US_DEBUGP("INIT 1\n");
1347 msleep(2000);
1349 rc = usbat_read_user_io(us, status);
1350 if (rc != USB_STOR_TRANSPORT_GOOD)
1351 return rc;
1353 US_DEBUGP("INIT 2\n");
1355 rc = usbat_read_user_io(us, status);
1356 if (rc != USB_STOR_XFER_GOOD)
1357 return USB_STOR_TRANSPORT_ERROR;
1359 rc = usbat_read_user_io(us, status);
1360 if (rc != USB_STOR_XFER_GOOD)
1361 return USB_STOR_TRANSPORT_ERROR;
1363 US_DEBUGP("INIT 3\n");
1365 rc = usbat_select_and_test_registers(us);
1366 if (rc != USB_STOR_TRANSPORT_GOOD)
1367 return rc;
1369 US_DEBUGP("INIT 4\n");
1371 rc = usbat_read_user_io(us, status);
1372 if (rc != USB_STOR_XFER_GOOD)
1373 return USB_STOR_TRANSPORT_ERROR;
1375 US_DEBUGP("INIT 5\n");
1377 /* Enable peripheral control signals and card detect */
1378 rc = usbat_device_enable_cdt(us);
1379 if (rc != USB_STOR_TRANSPORT_GOOD)
1380 return rc;
1382 US_DEBUGP("INIT 6\n");
1384 rc = usbat_read_user_io(us, status);
1385 if (rc != USB_STOR_XFER_GOOD)
1386 return USB_STOR_TRANSPORT_ERROR;
1388 US_DEBUGP("INIT 7\n");
1390 msleep(1400);
1392 rc = usbat_read_user_io(us, status);
1393 if (rc != USB_STOR_XFER_GOOD)
1394 return USB_STOR_TRANSPORT_ERROR;
1396 US_DEBUGP("INIT 8\n");
1398 rc = usbat_select_and_test_registers(us);
1399 if (rc != USB_STOR_TRANSPORT_GOOD)
1400 return rc;
1402 US_DEBUGP("INIT 9\n");
1404 /* At this point, we need to detect which device we are using */
1405 if (usbat_set_transport(us, info, devicetype))
1406 return USB_STOR_TRANSPORT_ERROR;
1408 US_DEBUGP("INIT 10\n");
1410 if (usbat_get_device_type(us) == USBAT_DEV_FLASH) {
1411 subcountH = 0x02;
1412 subcountL = 0x00;
1414 rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1415 0x00, 0x88, 0x08, subcountH, subcountL);
1416 if (rc != USB_STOR_XFER_GOOD)
1417 return USB_STOR_TRANSPORT_ERROR;
1419 US_DEBUGP("INIT 11\n");
1421 return USB_STOR_TRANSPORT_GOOD;
1425 * Transport for the HP 8200e
1427 static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1429 int result;
1430 unsigned char *status = us->iobuf;
1431 unsigned char registers[32];
1432 unsigned char data[32];
1433 unsigned int len;
1434 int i;
1435 char string[64];
1437 len = srb->request_bufflen;
1439 /* Send A0 (ATA PACKET COMMAND).
1440 Note: I guess we're never going to get any of the ATA
1441 commands... just ATA Packet Commands.
1444 registers[0] = USBAT_ATA_FEATURES;
1445 registers[1] = USBAT_ATA_SECCNT;
1446 registers[2] = USBAT_ATA_SECNUM;
1447 registers[3] = USBAT_ATA_LBA_ME;
1448 registers[4] = USBAT_ATA_LBA_HI;
1449 registers[5] = USBAT_ATA_DEVICE;
1450 registers[6] = USBAT_ATA_CMD;
1451 data[0] = 0x00;
1452 data[1] = 0x00;
1453 data[2] = 0x00;
1454 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1455 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
1456 data[5] = 0xB0; /* (device sel) = slave */
1457 data[6] = 0xA0; /* (command) = ATA PACKET COMMAND */
1459 for (i=7; i<19; i++) {
1460 registers[i] = 0x10;
1461 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1464 result = usbat_get_status(us, status);
1465 US_DEBUGP("Status = %02X\n", *status);
1466 if (result != USB_STOR_XFER_GOOD)
1467 return USB_STOR_TRANSPORT_ERROR;
1468 if (srb->cmnd[0] == TEST_UNIT_READY)
1469 transferred = 0;
1471 if (srb->sc_data_direction == DMA_TO_DEVICE) {
1473 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1474 registers, data, 19,
1475 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1476 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1477 DMA_TO_DEVICE,
1478 srb->request_buffer,
1479 len, srb->use_sg, 10);
1481 if (result == USB_STOR_TRANSPORT_GOOD) {
1482 transferred += len;
1483 US_DEBUGP("Wrote %08X bytes\n", transferred);
1486 return result;
1488 } else if (srb->cmnd[0] == READ_10 ||
1489 srb->cmnd[0] == GPCMD_READ_CD) {
1491 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1495 if (len > 0xFFFF) {
1496 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1497 len);
1498 return USB_STOR_TRANSPORT_ERROR;
1501 if ( (result = usbat_multiple_write(us,
1502 registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
1503 return result;
1507 * Write the 12-byte command header.
1509 * If the command is BLANK then set the timer for 75 minutes.
1510 * Otherwise set it for 10 minutes.
1512 * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1513 * AT SPEED 4 IS UNRELIABLE!!!
1516 if ((result = usbat_write_block(us,
1517 USBAT_ATA, srb->cmnd, 12,
1518 (srb->cmnd[0]==GPCMD_BLANK ? 75 : 10), 0) !=
1519 USB_STOR_TRANSPORT_GOOD)) {
1520 return result;
1523 /* If there is response data to be read in then do it here. */
1525 if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1527 /* How many bytes to read in? Check cylL register */
1529 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1530 USB_STOR_XFER_GOOD) {
1531 return USB_STOR_TRANSPORT_ERROR;
1534 if (len > 0xFF) { /* need to read cylH also */
1535 len = *status;
1536 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1537 USB_STOR_XFER_GOOD) {
1538 return USB_STOR_TRANSPORT_ERROR;
1540 len += ((unsigned int) *status)<<8;
1542 else
1543 len = *status;
1546 result = usbat_read_block(us, srb->request_buffer, len, srb->use_sg);
1548 /* Debug-print the first 32 bytes of the transfer */
1550 if (!srb->use_sg) {
1551 string[0] = 0;
1552 for (i=0; i<len && i<32; i++) {
1553 sprintf(string+strlen(string), "%02X ",
1554 ((unsigned char *)srb->request_buffer)[i]);
1555 if ((i%16)==15) {
1556 US_DEBUGP("%s\n", string);
1557 string[0] = 0;
1560 if (string[0]!=0)
1561 US_DEBUGP("%s\n", string);
1565 return result;
1569 * Transport for USBAT02-based CompactFlash and similar storage devices
1571 static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1573 int rc;
1574 struct usbat_info *info = (struct usbat_info *) (us->extra);
1575 unsigned long block, blocks;
1576 unsigned char *ptr = us->iobuf;
1577 static unsigned char inquiry_response[36] = {
1578 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1581 if (srb->cmnd[0] == INQUIRY) {
1582 US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1583 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1584 fill_inquiry_response(us, ptr, 36);
1585 return USB_STOR_TRANSPORT_GOOD;
1588 if (srb->cmnd[0] == READ_CAPACITY) {
1589 rc = usbat_flash_check_media(us, info);
1590 if (rc != USB_STOR_TRANSPORT_GOOD)
1591 return rc;
1593 rc = usbat_flash_get_sector_count(us, info);
1594 if (rc != USB_STOR_TRANSPORT_GOOD)
1595 return rc;
1597 /* hard coded 512 byte sectors as per ATA spec */
1598 info->ssize = 0x200;
1599 US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1600 info->sectors, info->ssize);
1603 * build the reply
1604 * note: must return the sector number of the last sector,
1605 * *not* the total number of sectors
1607 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1608 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1609 usb_stor_set_xfer_buf(ptr, 8, srb);
1611 return USB_STOR_TRANSPORT_GOOD;
1614 if (srb->cmnd[0] == MODE_SELECT_10) {
1615 US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
1616 return USB_STOR_TRANSPORT_ERROR;
1619 if (srb->cmnd[0] == READ_10) {
1620 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1621 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1623 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1625 US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
1626 return usbat_flash_read_data(us, info, block, blocks);
1629 if (srb->cmnd[0] == READ_12) {
1631 * I don't think we'll ever see a READ_12 but support it anyway
1633 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1634 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1636 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1637 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1639 US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
1640 return usbat_flash_read_data(us, info, block, blocks);
1643 if (srb->cmnd[0] == WRITE_10) {
1644 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1645 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1647 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1649 US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
1650 return usbat_flash_write_data(us, info, block, blocks);
1653 if (srb->cmnd[0] == WRITE_12) {
1655 * I don't think we'll ever see a WRITE_12 but support it anyway
1657 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1658 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1660 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1661 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1663 US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
1664 return usbat_flash_write_data(us, info, block, blocks);
1668 if (srb->cmnd[0] == TEST_UNIT_READY) {
1669 US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
1671 rc = usbat_flash_check_media(us, info);
1672 if (rc != USB_STOR_TRANSPORT_GOOD)
1673 return rc;
1675 return usbat_check_status(us);
1678 if (srb->cmnd[0] == REQUEST_SENSE) {
1679 US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
1681 memset(ptr, 0, 18);
1682 ptr[0] = 0xF0;
1683 ptr[2] = info->sense_key;
1684 ptr[7] = 11;
1685 ptr[12] = info->sense_asc;
1686 ptr[13] = info->sense_ascq;
1687 usb_stor_set_xfer_buf(ptr, 18, srb);
1689 return USB_STOR_TRANSPORT_GOOD;
1692 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1694 * sure. whatever. not like we can stop the user from popping
1695 * the media out of the device (no locking doors, etc)
1697 return USB_STOR_TRANSPORT_GOOD;
1700 US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1701 srb->cmnd[0], srb->cmnd[0]);
1702 info->sense_key = 0x05;
1703 info->sense_asc = 0x20;
1704 info->sense_ascq = 0x00;
1705 return USB_STOR_TRANSPORT_FAILED;
1708 int init_usbat_cd(struct us_data *us)
1710 return init_usbat(us, USBAT_DEV_HP8200);
1714 int init_usbat_flash(struct us_data *us)
1716 return init_usbat(us, USBAT_DEV_FLASH);
1719 int init_usbat_probe(struct us_data *us)
1721 return init_usbat(us, 0);
1725 * Default transport function. Attempts to detect which transport function
1726 * should be called, makes it the new default, and calls it.
1728 * This function should never be called. Our usbat_init() function detects the
1729 * device type and changes the us->transport ptr to the transport function
1730 * relevant to the device.
1731 * However, we'll support this impossible(?) case anyway.
1733 int usbat_transport(struct scsi_cmnd *srb, struct us_data *us)
1735 struct usbat_info *info = (struct usbat_info*) (us->extra);
1737 if (usbat_set_transport(us, info, 0))
1738 return USB_STOR_TRANSPORT_ERROR;
1740 return us->transport(srb, us);