1 /* Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable
3 * Current development and maintenance by:
4 * (c) 2000, 2001 Robert Baruch (autophile@starband.net)
5 * (c) 2004, 2005 Daniel Drake <dsd@gentoo.org>
7 * Developed with the assistance of:
8 * (c) 2002 Alan Stern <stern@rowland.org>
10 * Flash support based on earlier work by:
11 * (c) 2002 Thomas Kreiling <usbdev@sm04.de>
13 * Many originally ATAPI devices were slightly modified to meet the USB
14 * market by using some kind of translation from ATAPI to USB on the host,
15 * and the peripheral would translate from USB back to ATAPI.
17 * SCM Microsystems (www.scmmicro.com) makes a device, sold to OEM's only,
18 * which does the USB-to-ATAPI conversion. By obtaining the data sheet on
19 * their device under nondisclosure agreement, I have been able to write
20 * this driver for Linux.
22 * The chip used in the device can also be used for EPP and ISA translation
23 * as well. This driver is only guaranteed to work with the ATAPI
26 * See the Kconfig help text for a list of devices known to be supported by
29 * This program is free software; you can redistribute it and/or modify it
30 * under the terms of the GNU General Public License as published by the
31 * Free Software Foundation; either version 2, or (at your option) any
34 * This program is distributed in the hope that it will be useful, but
35 * WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37 * General Public License for more details.
39 * You should have received a copy of the GNU General Public License along
40 * with this program; if not, write to the Free Software Foundation, Inc.,
41 * 675 Mass Ave, Cambridge, MA 02139, USA.
44 #include <linux/errno.h>
45 #include <linux/module.h>
46 #include <linux/slab.h>
47 #include <linux/cdrom.h>
49 #include <scsi/scsi.h>
50 #include <scsi/scsi_cmnd.h>
53 #include "transport.h"
58 /* Supported device types */
59 #define USBAT_DEV_HP8200 0x01
60 #define USBAT_DEV_FLASH 0x02
62 #define USBAT_EPP_PORT 0x10
63 #define USBAT_EPP_REGISTER 0x30
64 #define USBAT_ATA 0x40
65 #define USBAT_ISA 0x50
67 /* Commands (need to be logically OR'd with an access type */
68 #define USBAT_CMD_READ_REG 0x00
69 #define USBAT_CMD_WRITE_REG 0x01
70 #define USBAT_CMD_READ_BLOCK 0x02
71 #define USBAT_CMD_WRITE_BLOCK 0x03
72 #define USBAT_CMD_COND_READ_BLOCK 0x04
73 #define USBAT_CMD_COND_WRITE_BLOCK 0x05
74 #define USBAT_CMD_WRITE_REGS 0x07
76 /* Commands (these don't need an access type) */
77 #define USBAT_CMD_EXEC_CMD 0x80
78 #define USBAT_CMD_SET_FEAT 0x81
79 #define USBAT_CMD_UIO 0x82
81 /* Methods of accessing UIO register */
82 #define USBAT_UIO_READ 1
83 #define USBAT_UIO_WRITE 0
86 #define USBAT_QUAL_FCQ 0x20 /* full compare */
87 #define USBAT_QUAL_ALQ 0x10 /* auto load subcount */
89 /* USBAT Flash Media status types */
90 #define USBAT_FLASH_MEDIA_NONE 0
91 #define USBAT_FLASH_MEDIA_CF 1
93 /* USBAT Flash Media change types */
94 #define USBAT_FLASH_MEDIA_SAME 0
95 #define USBAT_FLASH_MEDIA_CHANGED 1
97 /* USBAT ATA registers */
98 #define USBAT_ATA_DATA 0x10 /* read/write data (R/W) */
99 #define USBAT_ATA_FEATURES 0x11 /* set features (W) */
100 #define USBAT_ATA_ERROR 0x11 /* error (R) */
101 #define USBAT_ATA_SECCNT 0x12 /* sector count (R/W) */
102 #define USBAT_ATA_SECNUM 0x13 /* sector number (R/W) */
103 #define USBAT_ATA_LBA_ME 0x14 /* cylinder low (R/W) */
104 #define USBAT_ATA_LBA_HI 0x15 /* cylinder high (R/W) */
105 #define USBAT_ATA_DEVICE 0x16 /* head/device selection (R/W) */
106 #define USBAT_ATA_STATUS 0x17 /* device status (R) */
107 #define USBAT_ATA_CMD 0x17 /* device command (W) */
108 #define USBAT_ATA_ALTSTATUS 0x0E /* status (no clear IRQ) (R) */
110 /* USBAT User I/O Data registers */
111 #define USBAT_UIO_EPAD 0x80 /* Enable Peripheral Control Signals */
112 #define USBAT_UIO_CDT 0x40 /* Card Detect (Read Only) */
113 /* CDT = ACKD & !UI1 & !UI0 */
114 #define USBAT_UIO_1 0x20 /* I/O 1 */
115 #define USBAT_UIO_0 0x10 /* I/O 0 */
116 #define USBAT_UIO_EPP_ATA 0x08 /* 1=EPP mode, 0=ATA mode */
117 #define USBAT_UIO_UI1 0x04 /* Input 1 */
118 #define USBAT_UIO_UI0 0x02 /* Input 0 */
119 #define USBAT_UIO_INTR_ACK 0x01 /* Interrupt (ATA/ISA)/Acknowledge (EPP) */
121 /* USBAT User I/O Enable registers */
122 #define USBAT_UIO_DRVRST 0x80 /* Reset Peripheral */
123 #define USBAT_UIO_ACKD 0x40 /* Enable Card Detect */
124 #define USBAT_UIO_OE1 0x20 /* I/O 1 set=output/clr=input */
125 /* If ACKD=1, set OE1 to 1 also. */
126 #define USBAT_UIO_OE0 0x10 /* I/O 0 set=output/clr=input */
127 #define USBAT_UIO_ADPRST 0x01 /* Reset SCM chip */
130 #define USBAT_FEAT_ETEN 0x80 /* External trigger enable */
131 #define USBAT_FEAT_U1 0x08
132 #define USBAT_FEAT_U0 0x04
133 #define USBAT_FEAT_ET1 0x02
134 #define USBAT_FEAT_ET2 0x01
139 /* Used for Flash readers only */
140 unsigned long sectors; /* total sector count */
141 unsigned long ssize; /* sector size in bytes */
143 unsigned char sense_key;
144 unsigned long sense_asc; /* additional sense code */
145 unsigned long sense_ascq; /* additional sense code qualifier */
148 #define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
149 #define LSB_of(s) ((s)&0xFF)
150 #define MSB_of(s) ((s)>>8)
152 static int transferred = 0;
154 static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us);
155 static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us);
157 static int init_usbat_cd(struct us_data *us);
158 static int init_usbat_flash(struct us_data *us);
162 * The table of devices
164 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
165 vendorName, productName, useProtocol, useTransport, \
166 initFunction, flags) \
167 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
168 .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
170 struct usb_device_id usbat_usb_ids[] = {
171 # include "unusual_usbat.h"
172 { } /* Terminating entry */
174 MODULE_DEVICE_TABLE(usb, usbat_usb_ids);
181 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
182 vendor_name, product_name, use_protocol, use_transport, \
183 init_function, Flags) \
185 .vendorName = vendor_name, \
186 .productName = product_name, \
187 .useProtocol = use_protocol, \
188 .useTransport = use_transport, \
189 .initFunction = init_function, \
192 static struct us_unusual_dev usbat_unusual_dev_list[] = {
193 # include "unusual_usbat.h"
194 { } /* Terminating entry */
200 * Convenience function to produce an ATA read/write sectors command
201 * Use cmd=0x20 for read, cmd=0x30 for write
203 static void usbat_pack_ata_sector_cmd(unsigned char *buf,
204 unsigned char thistime,
205 u32 sector, unsigned char cmd)
209 buf[2] = sector & 0xFF;
210 buf[3] = (sector >> 8) & 0xFF;
211 buf[4] = (sector >> 16) & 0xFF;
212 buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
217 * Convenience function to get the device type (flash or hp8200)
219 static int usbat_get_device_type(struct us_data *us)
221 return ((struct usbat_info*)us->extra)->devicetype;
225 * Read a register from the device
227 static int usbat_read(struct us_data *us,
228 unsigned char access,
230 unsigned char *content)
232 return usb_stor_ctrl_transfer(us,
234 access | USBAT_CMD_READ_REG,
243 * Write to a register on the device
245 static int usbat_write(struct us_data *us,
246 unsigned char access,
248 unsigned char content)
250 return usb_stor_ctrl_transfer(us,
252 access | USBAT_CMD_WRITE_REG,
254 short_pack(reg, content),
261 * Convenience function to perform a bulk read
263 static int usbat_bulk_read(struct us_data *us,
269 return USB_STOR_XFER_GOOD;
271 US_DEBUGP("usbat_bulk_read: len = %d\n", len);
272 return usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe, buf, len, use_sg, NULL);
276 * Convenience function to perform a bulk write
278 static int usbat_bulk_write(struct us_data *us,
284 return USB_STOR_XFER_GOOD;
286 US_DEBUGP("usbat_bulk_write: len = %d\n", len);
287 return usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe, buf, len, use_sg, NULL);
291 * Some USBAT-specific commands can only be executed over a command transport
292 * This transport allows one (len=8) or two (len=16) vendor-specific commands
295 static int usbat_execute_command(struct us_data *us,
296 unsigned char *commands,
299 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
300 USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
305 * Read the status register
307 static int usbat_get_status(struct us_data *us, unsigned char *status)
310 rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
312 US_DEBUGP("usbat_get_status: 0x%02X\n", (unsigned short) (*status));
317 * Check the device status
319 static int usbat_check_status(struct us_data *us)
321 unsigned char *reply = us->iobuf;
324 rc = usbat_get_status(us, reply);
325 if (rc != USB_STOR_XFER_GOOD)
326 return USB_STOR_TRANSPORT_FAILED;
328 /* error/check condition (0x51 is ok) */
329 if (*reply & 0x01 && *reply != 0x51)
330 return USB_STOR_TRANSPORT_FAILED;
334 return USB_STOR_TRANSPORT_FAILED;
336 return USB_STOR_TRANSPORT_GOOD;
340 * Stores critical information in internal registers in prepartion for the execution
341 * of a conditional usbat_read_blocks or usbat_write_blocks call.
343 static int usbat_set_shuttle_features(struct us_data *us,
344 unsigned char external_trigger,
345 unsigned char epp_control,
346 unsigned char mask_byte,
347 unsigned char test_pattern,
348 unsigned char subcountH,
349 unsigned char subcountL)
351 unsigned char *command = us->iobuf;
354 command[1] = USBAT_CMD_SET_FEAT;
357 * The only bit relevant to ATA access is bit 6
358 * which defines 8 bit data access (set) or 16 bit (unset)
360 command[2] = epp_control;
363 * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
364 * ET1 and ET2 define an external event to be checked for on event of a
365 * _read_blocks or _write_blocks operation. The read/write will not take
366 * place unless the defined trigger signal is active.
368 command[3] = external_trigger;
371 * The resultant byte of the mask operation (see mask_byte) is compared for
372 * equivalence with this test pattern. If equal, the read/write will take
375 command[4] = test_pattern;
378 * This value is logically ANDed with the status register field specified
379 * in the read/write command.
381 command[5] = mask_byte;
384 * If ALQ is set in the qualifier, this field contains the address of the
385 * registers where the byte count should be read for transferring the data.
386 * If ALQ is not set, then this field contains the number of bytes to be
389 command[6] = subcountL;
390 command[7] = subcountH;
392 return usbat_execute_command(us, command, 8);
396 * Block, waiting for an ATA device to become not busy or to report
397 * an error condition.
399 static int usbat_wait_not_busy(struct us_data *us, int minutes)
403 unsigned char *status = us->iobuf;
405 /* Synchronizing cache on a CDR could take a heck of a long time,
406 * but probably not more than 10 minutes or so. On the other hand,
407 * doing a full blank on a CDRW at speed 1 will take about 75
411 for (i=0; i<1200+minutes*60; i++) {
413 result = usbat_get_status(us, status);
415 if (result!=USB_STOR_XFER_GOOD)
416 return USB_STOR_TRANSPORT_ERROR;
417 if (*status & 0x01) { /* check condition */
418 result = usbat_read(us, USBAT_ATA, 0x10, status);
419 return USB_STOR_TRANSPORT_FAILED;
421 if (*status & 0x20) /* device fault */
422 return USB_STOR_TRANSPORT_FAILED;
424 if ((*status & 0x80)==0x00) { /* not busy */
425 US_DEBUGP("Waited not busy for %d steps\n", i);
426 return USB_STOR_TRANSPORT_GOOD;
430 msleep(10); /* 5 seconds */
432 msleep(50); /* 10 seconds */
434 msleep(100); /* 50 seconds */
436 msleep(1000); /* X minutes */
439 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
441 return USB_STOR_TRANSPORT_FAILED;
445 * Read block data from the data register
447 static int usbat_read_block(struct us_data *us,
453 unsigned char *command = us->iobuf;
456 return USB_STOR_TRANSPORT_GOOD;
459 command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
460 command[2] = USBAT_ATA_DATA;
464 command[6] = LSB_of(len);
465 command[7] = MSB_of(len);
467 result = usbat_execute_command(us, command, 8);
468 if (result != USB_STOR_XFER_GOOD)
469 return USB_STOR_TRANSPORT_ERROR;
471 result = usbat_bulk_read(us, buf, len, use_sg);
472 return (result == USB_STOR_XFER_GOOD ?
473 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
477 * Write block data via the data register
479 static int usbat_write_block(struct us_data *us,
480 unsigned char access,
487 unsigned char *command = us->iobuf;
490 return USB_STOR_TRANSPORT_GOOD;
493 command[1] = access | USBAT_CMD_WRITE_BLOCK;
494 command[2] = USBAT_ATA_DATA;
498 command[6] = LSB_of(len);
499 command[7] = MSB_of(len);
501 result = usbat_execute_command(us, command, 8);
503 if (result != USB_STOR_XFER_GOOD)
504 return USB_STOR_TRANSPORT_ERROR;
506 result = usbat_bulk_write(us, buf, len, use_sg);
507 if (result != USB_STOR_XFER_GOOD)
508 return USB_STOR_TRANSPORT_ERROR;
510 return usbat_wait_not_busy(us, minutes);
514 * Process read and write requests
516 static int usbat_hp8200e_rw_block_test(struct us_data *us,
517 unsigned char access,
518 unsigned char *registers,
519 unsigned char *data_out,
520 unsigned short num_registers,
521 unsigned char data_reg,
522 unsigned char status_reg,
523 unsigned char timeout,
524 unsigned char qualifier,
532 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
533 us->recv_bulk_pipe : us->send_bulk_pipe;
535 unsigned char *command = us->iobuf;
538 unsigned char *data = us->iobuf;
539 unsigned char *status = us->iobuf;
541 BUG_ON(num_registers > US_IOBUF_SIZE/2);
543 for (i=0; i<20; i++) {
546 * The first time we send the full command, which consists
547 * of downloading the SCSI command followed by downloading
548 * the data via a write-and-test. Any other time we only
549 * send the command to download the data -- the SCSI command
550 * is still 'active' in some sense in the device.
552 * We're only going to try sending the data 10 times. After
553 * that, we just return a failure.
559 * Write to multiple registers
560 * Not really sure the 0x07, 0x17, 0xfc, 0xe7 is
561 * necessary here, but that's what came out of the
562 * trace every single time.
565 command[1] = access | USBAT_CMD_WRITE_REGS;
570 command[6] = LSB_of(num_registers*2);
571 command[7] = MSB_of(num_registers*2);
575 /* Conditionally read or write blocks */
576 command[cmdlen-8] = (direction==DMA_TO_DEVICE ? 0x40 : 0xC0);
577 command[cmdlen-7] = access |
578 (direction==DMA_TO_DEVICE ?
579 USBAT_CMD_COND_WRITE_BLOCK : USBAT_CMD_COND_READ_BLOCK);
580 command[cmdlen-6] = data_reg;
581 command[cmdlen-5] = status_reg;
582 command[cmdlen-4] = timeout;
583 command[cmdlen-3] = qualifier;
584 command[cmdlen-2] = LSB_of(len);
585 command[cmdlen-1] = MSB_of(len);
587 result = usbat_execute_command(us, command, cmdlen);
589 if (result != USB_STOR_XFER_GOOD)
590 return USB_STOR_TRANSPORT_ERROR;
594 for (j=0; j<num_registers; j++) {
595 data[j<<1] = registers[j];
596 data[1+(j<<1)] = data_out[j];
599 result = usbat_bulk_write(us, data, num_registers*2, 0);
600 if (result != USB_STOR_XFER_GOOD)
601 return USB_STOR_TRANSPORT_ERROR;
605 result = usb_stor_bulk_transfer_sg(us,
606 pipe, buf, len, use_sg, NULL);
609 * If we get a stall on the bulk download, we'll retry
610 * the bulk download -- but not the SCSI command because
611 * in some sense the SCSI command is still 'active' and
612 * waiting for the data. Don't ask me why this should be;
613 * I'm only following what the Windoze driver did.
615 * Note that a stall for the test-and-read/write command means
616 * that the test failed. In this case we're testing to make
617 * sure that the device is error-free
618 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
619 * hypothesis is that the USBAT chip somehow knows what
620 * the device will accept, but doesn't give the device any
621 * data until all data is received. Thus, the device would
622 * still be waiting for the first byte of data if a stall
623 * occurs, even if the stall implies that some data was
627 if (result == USB_STOR_XFER_SHORT ||
628 result == USB_STOR_XFER_STALLED) {
631 * If we're reading and we stalled, then clear
632 * the bulk output pipe only the first time.
635 if (direction==DMA_FROM_DEVICE && i==0) {
636 if (usb_stor_clear_halt(us,
637 us->send_bulk_pipe) < 0)
638 return USB_STOR_TRANSPORT_ERROR;
642 * Read status: is the device angry, or just busy?
645 result = usbat_read(us, USBAT_ATA,
646 direction==DMA_TO_DEVICE ?
647 USBAT_ATA_STATUS : USBAT_ATA_ALTSTATUS,
650 if (result!=USB_STOR_XFER_GOOD)
651 return USB_STOR_TRANSPORT_ERROR;
652 if (*status & 0x01) /* check condition */
653 return USB_STOR_TRANSPORT_FAILED;
654 if (*status & 0x20) /* device fault */
655 return USB_STOR_TRANSPORT_FAILED;
657 US_DEBUGP("Redoing %s\n",
658 direction==DMA_TO_DEVICE ? "write" : "read");
660 } else if (result != USB_STOR_XFER_GOOD)
661 return USB_STOR_TRANSPORT_ERROR;
663 return usbat_wait_not_busy(us, minutes);
667 US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
668 direction==DMA_TO_DEVICE ? "Writing" : "Reading");
670 return USB_STOR_TRANSPORT_FAILED;
674 * Write to multiple registers:
675 * Allows us to write specific data to any registers. The data to be written
676 * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN
677 * which gets sent through bulk out.
678 * Not designed for large transfers of data!
680 static int usbat_multiple_write(struct us_data *us,
681 unsigned char *registers,
682 unsigned char *data_out,
683 unsigned short num_registers)
686 unsigned char *data = us->iobuf;
687 unsigned char *command = us->iobuf;
689 BUG_ON(num_registers > US_IOBUF_SIZE/2);
691 /* Write to multiple registers, ATA access */
693 command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
701 /* Number of bytes to be transferred (incl. addresses and data) */
702 command[6] = LSB_of(num_registers*2);
703 command[7] = MSB_of(num_registers*2);
705 /* The setup command */
706 result = usbat_execute_command(us, command, 8);
707 if (result != USB_STOR_XFER_GOOD)
708 return USB_STOR_TRANSPORT_ERROR;
710 /* Create the reg/data, reg/data sequence */
711 for (i=0; i<num_registers; i++) {
712 data[i<<1] = registers[i];
713 data[1+(i<<1)] = data_out[i];
717 result = usbat_bulk_write(us, data, num_registers*2, 0);
718 if (result != USB_STOR_XFER_GOOD)
719 return USB_STOR_TRANSPORT_ERROR;
721 if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
722 return usbat_wait_not_busy(us, 0);
724 return USB_STOR_TRANSPORT_GOOD;
728 * Conditionally read blocks from device:
729 * Allows us to read blocks from a specific data register, based upon the
730 * condition that a status register can be successfully masked with a status
731 * qualifier. If this condition is not initially met, the read will wait
732 * up until a maximum amount of time has elapsed, as specified by timeout.
733 * The read will start when the condition is met, otherwise the command aborts.
735 * The qualifier defined here is not the value that is masked, it defines
736 * conditions for the write to take place. The actual masked qualifier (and
737 * other related details) are defined beforehand with _set_shuttle_features().
739 static int usbat_read_blocks(struct us_data *us,
745 unsigned char *command = us->iobuf;
748 command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK;
749 command[2] = USBAT_ATA_DATA;
750 command[3] = USBAT_ATA_STATUS;
751 command[4] = 0xFD; /* Timeout (ms); */
752 command[5] = USBAT_QUAL_FCQ;
753 command[6] = LSB_of(len);
754 command[7] = MSB_of(len);
756 /* Multiple block read setup command */
757 result = usbat_execute_command(us, command, 8);
758 if (result != USB_STOR_XFER_GOOD)
759 return USB_STOR_TRANSPORT_FAILED;
761 /* Read the blocks we just asked for */
762 result = usbat_bulk_read(us, buffer, len, use_sg);
763 if (result != USB_STOR_XFER_GOOD)
764 return USB_STOR_TRANSPORT_FAILED;
766 return USB_STOR_TRANSPORT_GOOD;
770 * Conditionally write blocks to device:
771 * Allows us to write blocks to a specific data register, based upon the
772 * condition that a status register can be successfully masked with a status
773 * qualifier. If this condition is not initially met, the write will wait
774 * up until a maximum amount of time has elapsed, as specified by timeout.
775 * The read will start when the condition is met, otherwise the command aborts.
777 * The qualifier defined here is not the value that is masked, it defines
778 * conditions for the write to take place. The actual masked qualifier (and
779 * other related details) are defined beforehand with _set_shuttle_features().
781 static int usbat_write_blocks(struct us_data *us,
787 unsigned char *command = us->iobuf;
790 command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK;
791 command[2] = USBAT_ATA_DATA;
792 command[3] = USBAT_ATA_STATUS;
793 command[4] = 0xFD; /* Timeout (ms) */
794 command[5] = USBAT_QUAL_FCQ;
795 command[6] = LSB_of(len);
796 command[7] = MSB_of(len);
798 /* Multiple block write setup command */
799 result = usbat_execute_command(us, command, 8);
800 if (result != USB_STOR_XFER_GOOD)
801 return USB_STOR_TRANSPORT_FAILED;
804 result = usbat_bulk_write(us, buffer, len, use_sg);
805 if (result != USB_STOR_XFER_GOOD)
806 return USB_STOR_TRANSPORT_FAILED;
808 return USB_STOR_TRANSPORT_GOOD;
812 * Read the User IO register
814 static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
818 result = usb_stor_ctrl_transfer(us,
827 US_DEBUGP("usbat_read_user_io: UIO register reads %02X\n", (unsigned short) (*data_flags));
833 * Write to the User IO register
835 static int usbat_write_user_io(struct us_data *us,
836 unsigned char enable_flags,
837 unsigned char data_flags)
839 return usb_stor_ctrl_transfer(us,
843 short_pack(enable_flags, data_flags),
851 * Often needed on media change.
853 static int usbat_device_reset(struct us_data *us)
858 * Reset peripheral, enable peripheral control signals
859 * (bring reset signal up)
861 rc = usbat_write_user_io(us,
862 USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
863 USBAT_UIO_EPAD | USBAT_UIO_1);
864 if (rc != USB_STOR_XFER_GOOD)
865 return USB_STOR_TRANSPORT_ERROR;
868 * Enable peripheral control signals
869 * (bring reset signal down)
871 rc = usbat_write_user_io(us,
872 USBAT_UIO_OE1 | USBAT_UIO_OE0,
873 USBAT_UIO_EPAD | USBAT_UIO_1);
874 if (rc != USB_STOR_XFER_GOOD)
875 return USB_STOR_TRANSPORT_ERROR;
877 return USB_STOR_TRANSPORT_GOOD;
883 static int usbat_device_enable_cdt(struct us_data *us)
887 /* Enable peripheral control signals and card detect */
888 rc = usbat_write_user_io(us,
889 USBAT_UIO_ACKD | USBAT_UIO_OE1 | USBAT_UIO_OE0,
890 USBAT_UIO_EPAD | USBAT_UIO_1);
891 if (rc != USB_STOR_XFER_GOOD)
892 return USB_STOR_TRANSPORT_ERROR;
894 return USB_STOR_TRANSPORT_GOOD;
898 * Determine if media is present.
900 static int usbat_flash_check_media_present(unsigned char *uio)
902 if (*uio & USBAT_UIO_UI0) {
903 US_DEBUGP("usbat_flash_check_media_present: no media detected\n");
904 return USBAT_FLASH_MEDIA_NONE;
907 return USBAT_FLASH_MEDIA_CF;
911 * Determine if media has changed since last operation
913 static int usbat_flash_check_media_changed(unsigned char *uio)
915 if (*uio & USBAT_UIO_0) {
916 US_DEBUGP("usbat_flash_check_media_changed: media change detected\n");
917 return USBAT_FLASH_MEDIA_CHANGED;
920 return USBAT_FLASH_MEDIA_SAME;
924 * Check for media change / no media and handle the situation appropriately
926 static int usbat_flash_check_media(struct us_data *us,
927 struct usbat_info *info)
930 unsigned char *uio = us->iobuf;
932 rc = usbat_read_user_io(us, uio);
933 if (rc != USB_STOR_XFER_GOOD)
934 return USB_STOR_TRANSPORT_ERROR;
936 /* Check for media existence */
937 rc = usbat_flash_check_media_present(uio);
938 if (rc == USBAT_FLASH_MEDIA_NONE) {
939 info->sense_key = 0x02;
940 info->sense_asc = 0x3A;
941 info->sense_ascq = 0x00;
942 return USB_STOR_TRANSPORT_FAILED;
945 /* Check for media change */
946 rc = usbat_flash_check_media_changed(uio);
947 if (rc == USBAT_FLASH_MEDIA_CHANGED) {
949 /* Reset and re-enable card detect */
950 rc = usbat_device_reset(us);
951 if (rc != USB_STOR_TRANSPORT_GOOD)
953 rc = usbat_device_enable_cdt(us);
954 if (rc != USB_STOR_TRANSPORT_GOOD)
959 rc = usbat_read_user_io(us, uio);
960 if (rc != USB_STOR_XFER_GOOD)
961 return USB_STOR_TRANSPORT_ERROR;
963 info->sense_key = UNIT_ATTENTION;
964 info->sense_asc = 0x28;
965 info->sense_ascq = 0x00;
966 return USB_STOR_TRANSPORT_FAILED;
969 return USB_STOR_TRANSPORT_GOOD;
973 * Determine whether we are controlling a flash-based reader/writer,
974 * or a HP8200-based CD drive.
975 * Sets transport functions as appropriate.
977 static int usbat_identify_device(struct us_data *us,
978 struct usbat_info *info)
981 unsigned char status;
984 return USB_STOR_TRANSPORT_ERROR;
986 rc = usbat_device_reset(us);
987 if (rc != USB_STOR_TRANSPORT_GOOD)
992 * In attempt to distinguish between HP CDRW's and Flash readers, we now
993 * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
994 * readers), this command should fail with error. On ATAPI devices (i.e.
995 * CDROM drives), it should succeed.
997 rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1);
998 if (rc != USB_STOR_XFER_GOOD)
999 return USB_STOR_TRANSPORT_ERROR;
1001 rc = usbat_get_status(us, &status);
1002 if (rc != USB_STOR_XFER_GOOD)
1003 return USB_STOR_TRANSPORT_ERROR;
1005 /* Check for error bit, or if the command 'fell through' */
1006 if (status == 0xA1 || !(status & 0x01)) {
1007 /* Device is HP 8200 */
1008 US_DEBUGP("usbat_identify_device: Detected HP8200 CDRW\n");
1009 info->devicetype = USBAT_DEV_HP8200;
1011 /* Device is a CompactFlash reader/writer */
1012 US_DEBUGP("usbat_identify_device: Detected Flash reader/writer\n");
1013 info->devicetype = USBAT_DEV_FLASH;
1016 return USB_STOR_TRANSPORT_GOOD;
1020 * Set the transport function based on the device type
1022 static int usbat_set_transport(struct us_data *us,
1023 struct usbat_info *info,
1027 if (!info->devicetype)
1028 info->devicetype = devicetype;
1030 if (!info->devicetype)
1031 usbat_identify_device(us, info);
1033 switch (info->devicetype) {
1035 return USB_STOR_TRANSPORT_ERROR;
1037 case USBAT_DEV_HP8200:
1038 us->transport = usbat_hp8200e_transport;
1041 case USBAT_DEV_FLASH:
1042 us->transport = usbat_flash_transport;
1050 * Read the media capacity
1052 static int usbat_flash_get_sector_count(struct us_data *us,
1053 struct usbat_info *info)
1055 unsigned char registers[3] = {
1060 unsigned char command[3] = { 0x01, 0xA0, 0xEC };
1061 unsigned char *reply;
1062 unsigned char status;
1066 return USB_STOR_TRANSPORT_ERROR;
1068 reply = kmalloc(512, GFP_NOIO);
1070 return USB_STOR_TRANSPORT_ERROR;
1072 /* ATA command : IDENTIFY DEVICE */
1073 rc = usbat_multiple_write(us, registers, command, 3);
1074 if (rc != USB_STOR_XFER_GOOD) {
1075 US_DEBUGP("usbat_flash_get_sector_count: Gah! identify_device failed\n");
1076 rc = USB_STOR_TRANSPORT_ERROR;
1080 /* Read device status */
1081 if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
1082 rc = USB_STOR_TRANSPORT_ERROR;
1088 /* Read the device identification data */
1089 rc = usbat_read_block(us, reply, 512, 0);
1090 if (rc != USB_STOR_TRANSPORT_GOOD)
1093 info->sectors = ((u32)(reply[117]) << 24) |
1094 ((u32)(reply[116]) << 16) |
1095 ((u32)(reply[115]) << 8) |
1096 ((u32)(reply[114]) );
1098 rc = USB_STOR_TRANSPORT_GOOD;
1106 * Read data from device
1108 static int usbat_flash_read_data(struct us_data *us,
1109 struct usbat_info *info,
1113 unsigned char registers[7] = {
1122 unsigned char command[7];
1123 unsigned char *buffer;
1124 unsigned char thistime;
1125 unsigned int totallen, alloclen;
1127 unsigned int sg_offset = 0;
1128 struct scatterlist *sg = NULL;
1130 result = usbat_flash_check_media(us, info);
1131 if (result != USB_STOR_TRANSPORT_GOOD)
1135 * we're working in LBA mode. according to the ATA spec,
1136 * we can support up to 28-bit addressing. I don't know if Jumpshot
1137 * supports beyond 24-bit addressing. It's kind of hard to test
1138 * since it requires > 8GB CF card.
1141 if (sector > 0x0FFFFFFF)
1142 return USB_STOR_TRANSPORT_ERROR;
1144 totallen = sectors * info->ssize;
1147 * Since we don't read more than 64 KB at a time, we have to create
1148 * a bounce buffer and move the data a piece at a time between the
1149 * bounce buffer and the actual transfer buffer.
1152 alloclen = min(totallen, 65536u);
1153 buffer = kmalloc(alloclen, GFP_NOIO);
1155 return USB_STOR_TRANSPORT_ERROR;
1159 * loop, never allocate or transfer more than 64k at once
1160 * (min(128k, 255*info->ssize) is the real limit)
1162 len = min(totallen, alloclen);
1163 thistime = (len / info->ssize) & 0xff;
1165 /* ATA command 0x20 (READ SECTORS) */
1166 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
1168 /* Write/execute ATA read command */
1169 result = usbat_multiple_write(us, registers, command, 7);
1170 if (result != USB_STOR_TRANSPORT_GOOD)
1173 /* Read the data we just requested */
1174 result = usbat_read_blocks(us, buffer, len, 0);
1175 if (result != USB_STOR_TRANSPORT_GOOD)
1178 US_DEBUGP("usbat_flash_read_data: %d bytes\n", len);
1180 /* Store the data in the transfer buffer */
1181 usb_stor_access_xfer_buf(buffer, len, us->srb,
1182 &sg, &sg_offset, TO_XFER_BUF);
1186 } while (totallen > 0);
1189 return USB_STOR_TRANSPORT_GOOD;
1193 return USB_STOR_TRANSPORT_ERROR;
1197 * Write data to device
1199 static int usbat_flash_write_data(struct us_data *us,
1200 struct usbat_info *info,
1204 unsigned char registers[7] = {
1213 unsigned char command[7];
1214 unsigned char *buffer;
1215 unsigned char thistime;
1216 unsigned int totallen, alloclen;
1218 unsigned int sg_offset = 0;
1219 struct scatterlist *sg = NULL;
1221 result = usbat_flash_check_media(us, info);
1222 if (result != USB_STOR_TRANSPORT_GOOD)
1226 * we're working in LBA mode. according to the ATA spec,
1227 * we can support up to 28-bit addressing. I don't know if the device
1228 * supports beyond 24-bit addressing. It's kind of hard to test
1229 * since it requires > 8GB media.
1232 if (sector > 0x0FFFFFFF)
1233 return USB_STOR_TRANSPORT_ERROR;
1235 totallen = sectors * info->ssize;
1238 * Since we don't write more than 64 KB at a time, we have to create
1239 * a bounce buffer and move the data a piece at a time between the
1240 * bounce buffer and the actual transfer buffer.
1243 alloclen = min(totallen, 65536u);
1244 buffer = kmalloc(alloclen, GFP_NOIO);
1246 return USB_STOR_TRANSPORT_ERROR;
1250 * loop, never allocate or transfer more than 64k at once
1251 * (min(128k, 255*info->ssize) is the real limit)
1253 len = min(totallen, alloclen);
1254 thistime = (len / info->ssize) & 0xff;
1256 /* Get the data from the transfer buffer */
1257 usb_stor_access_xfer_buf(buffer, len, us->srb,
1258 &sg, &sg_offset, FROM_XFER_BUF);
1260 /* ATA command 0x30 (WRITE SECTORS) */
1261 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
1263 /* Write/execute ATA write command */
1264 result = usbat_multiple_write(us, registers, command, 7);
1265 if (result != USB_STOR_TRANSPORT_GOOD)
1268 /* Write the data */
1269 result = usbat_write_blocks(us, buffer, len, 0);
1270 if (result != USB_STOR_TRANSPORT_GOOD)
1275 } while (totallen > 0);
1282 return USB_STOR_TRANSPORT_ERROR;
1286 * Squeeze a potentially huge (> 65535 byte) read10 command into
1287 * a little ( <= 65535 byte) ATAPI pipe
1289 static int usbat_hp8200e_handle_read10(struct us_data *us,
1290 unsigned char *registers,
1291 unsigned char *data,
1292 struct scsi_cmnd *srb)
1294 int result = USB_STOR_TRANSPORT_GOOD;
1295 unsigned char *buffer;
1297 unsigned int sector;
1298 unsigned int sg_offset = 0;
1299 struct scatterlist *sg = NULL;
1301 US_DEBUGP("handle_read10: transfersize %d\n",
1304 if (scsi_bufflen(srb) < 0x10000) {
1306 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1307 registers, data, 19,
1308 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1309 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1312 scsi_bufflen(srb), scsi_sg_count(srb), 1);
1318 * Since we're requesting more data than we can handle in
1319 * a single read command (max is 64k-1), we will perform
1320 * multiple reads, but each read must be in multiples of
1321 * a sector. Luckily the sector size is in srb->transfersize
1322 * (see linux/drivers/scsi/sr.c).
1325 if (data[7+0] == GPCMD_READ_CD) {
1326 len = short_pack(data[7+9], data[7+8]);
1329 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
1330 srb->transfersize = scsi_bufflen(srb)/len;
1333 if (!srb->transfersize) {
1334 srb->transfersize = 2048; /* A guess */
1335 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
1340 * Since we only read in one block at a time, we have to create
1341 * a bounce buffer and move the data a piece at a time between the
1342 * bounce buffer and the actual transfer buffer.
1345 len = (65535/srb->transfersize) * srb->transfersize;
1346 US_DEBUGP("Max read is %d bytes\n", len);
1347 len = min(len, scsi_bufflen(srb));
1348 buffer = kmalloc(len, GFP_NOIO);
1349 if (buffer == NULL) /* bloody hell! */
1350 return USB_STOR_TRANSPORT_FAILED;
1351 sector = short_pack(data[7+3], data[7+2]);
1353 sector |= short_pack(data[7+5], data[7+4]);
1356 while (transferred != scsi_bufflen(srb)) {
1358 if (len > scsi_bufflen(srb) - transferred)
1359 len = scsi_bufflen(srb) - transferred;
1361 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1362 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
1364 /* Fix up the SCSI command sector and num sectors */
1366 data[7+2] = MSB_of(sector>>16); /* SCSI command sector */
1367 data[7+3] = LSB_of(sector>>16);
1368 data[7+4] = MSB_of(sector&0xFFFF);
1369 data[7+5] = LSB_of(sector&0xFFFF);
1370 if (data[7+0] == GPCMD_READ_CD)
1372 data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */
1373 data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */
1375 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1376 registers, data, 19,
1377 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1378 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1383 if (result != USB_STOR_TRANSPORT_GOOD)
1386 /* Store the data in the transfer buffer */
1387 usb_stor_access_xfer_buf(buffer, len, srb,
1388 &sg, &sg_offset, TO_XFER_BUF);
1390 /* Update the amount transferred and the sector number */
1393 sector += len / srb->transfersize;
1395 } /* while transferred != scsi_bufflen(srb) */
1401 static int usbat_select_and_test_registers(struct us_data *us)
1404 unsigned char *status = us->iobuf;
1406 /* try device = master, then device = slave. */
1407 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
1408 if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
1410 return USB_STOR_TRANSPORT_ERROR;
1412 if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) !=
1414 return USB_STOR_TRANSPORT_ERROR;
1416 if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) !=
1418 return USB_STOR_TRANSPORT_ERROR;
1420 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1422 return USB_STOR_TRANSPORT_ERROR;
1424 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1426 return USB_STOR_TRANSPORT_ERROR;
1428 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) !=
1430 return USB_STOR_TRANSPORT_ERROR;
1432 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) !=
1434 return USB_STOR_TRANSPORT_ERROR;
1436 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1438 return USB_STOR_TRANSPORT_ERROR;
1440 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1442 return USB_STOR_TRANSPORT_ERROR;
1445 return USB_STOR_TRANSPORT_GOOD;
1449 * Initialize the USBAT processor and the storage device
1451 static int init_usbat(struct us_data *us, int devicetype)
1454 struct usbat_info *info;
1455 unsigned char subcountH = USBAT_ATA_LBA_HI;
1456 unsigned char subcountL = USBAT_ATA_LBA_ME;
1457 unsigned char *status = us->iobuf;
1459 us->extra = kzalloc(sizeof(struct usbat_info), GFP_NOIO);
1461 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1464 info = (struct usbat_info *) (us->extra);
1466 /* Enable peripheral control signals */
1467 rc = usbat_write_user_io(us,
1468 USBAT_UIO_OE1 | USBAT_UIO_OE0,
1469 USBAT_UIO_EPAD | USBAT_UIO_1);
1470 if (rc != USB_STOR_XFER_GOOD)
1471 return USB_STOR_TRANSPORT_ERROR;
1473 US_DEBUGP("INIT 1\n");
1477 rc = usbat_read_user_io(us, status);
1478 if (rc != USB_STOR_TRANSPORT_GOOD)
1481 US_DEBUGP("INIT 2\n");
1483 rc = usbat_read_user_io(us, status);
1484 if (rc != USB_STOR_XFER_GOOD)
1485 return USB_STOR_TRANSPORT_ERROR;
1487 rc = usbat_read_user_io(us, status);
1488 if (rc != USB_STOR_XFER_GOOD)
1489 return USB_STOR_TRANSPORT_ERROR;
1491 US_DEBUGP("INIT 3\n");
1493 rc = usbat_select_and_test_registers(us);
1494 if (rc != USB_STOR_TRANSPORT_GOOD)
1497 US_DEBUGP("INIT 4\n");
1499 rc = usbat_read_user_io(us, status);
1500 if (rc != USB_STOR_XFER_GOOD)
1501 return USB_STOR_TRANSPORT_ERROR;
1503 US_DEBUGP("INIT 5\n");
1505 /* Enable peripheral control signals and card detect */
1506 rc = usbat_device_enable_cdt(us);
1507 if (rc != USB_STOR_TRANSPORT_GOOD)
1510 US_DEBUGP("INIT 6\n");
1512 rc = usbat_read_user_io(us, status);
1513 if (rc != USB_STOR_XFER_GOOD)
1514 return USB_STOR_TRANSPORT_ERROR;
1516 US_DEBUGP("INIT 7\n");
1520 rc = usbat_read_user_io(us, status);
1521 if (rc != USB_STOR_XFER_GOOD)
1522 return USB_STOR_TRANSPORT_ERROR;
1524 US_DEBUGP("INIT 8\n");
1526 rc = usbat_select_and_test_registers(us);
1527 if (rc != USB_STOR_TRANSPORT_GOOD)
1530 US_DEBUGP("INIT 9\n");
1532 /* At this point, we need to detect which device we are using */
1533 if (usbat_set_transport(us, info, devicetype))
1534 return USB_STOR_TRANSPORT_ERROR;
1536 US_DEBUGP("INIT 10\n");
1538 if (usbat_get_device_type(us) == USBAT_DEV_FLASH) {
1542 rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1543 0x00, 0x88, 0x08, subcountH, subcountL);
1544 if (rc != USB_STOR_XFER_GOOD)
1545 return USB_STOR_TRANSPORT_ERROR;
1547 US_DEBUGP("INIT 11\n");
1549 return USB_STOR_TRANSPORT_GOOD;
1553 * Transport for the HP 8200e
1555 static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1558 unsigned char *status = us->iobuf;
1559 unsigned char registers[32];
1560 unsigned char data[32];
1564 len = scsi_bufflen(srb);
1566 /* Send A0 (ATA PACKET COMMAND).
1567 Note: I guess we're never going to get any of the ATA
1568 commands... just ATA Packet Commands.
1571 registers[0] = USBAT_ATA_FEATURES;
1572 registers[1] = USBAT_ATA_SECCNT;
1573 registers[2] = USBAT_ATA_SECNUM;
1574 registers[3] = USBAT_ATA_LBA_ME;
1575 registers[4] = USBAT_ATA_LBA_HI;
1576 registers[5] = USBAT_ATA_DEVICE;
1577 registers[6] = USBAT_ATA_CMD;
1581 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1582 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
1583 data[5] = 0xB0; /* (device sel) = slave */
1584 data[6] = 0xA0; /* (command) = ATA PACKET COMMAND */
1586 for (i=7; i<19; i++) {
1587 registers[i] = 0x10;
1588 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1591 result = usbat_get_status(us, status);
1592 US_DEBUGP("Status = %02X\n", *status);
1593 if (result != USB_STOR_XFER_GOOD)
1594 return USB_STOR_TRANSPORT_ERROR;
1595 if (srb->cmnd[0] == TEST_UNIT_READY)
1598 if (srb->sc_data_direction == DMA_TO_DEVICE) {
1600 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1601 registers, data, 19,
1602 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1603 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1606 len, scsi_sg_count(srb), 10);
1608 if (result == USB_STOR_TRANSPORT_GOOD) {
1610 US_DEBUGP("Wrote %08X bytes\n", transferred);
1615 } else if (srb->cmnd[0] == READ_10 ||
1616 srb->cmnd[0] == GPCMD_READ_CD) {
1618 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1623 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1625 return USB_STOR_TRANSPORT_ERROR;
1628 if ( (result = usbat_multiple_write(us,
1629 registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
1634 * Write the 12-byte command header.
1636 * If the command is BLANK then set the timer for 75 minutes.
1637 * Otherwise set it for 10 minutes.
1639 * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1640 * AT SPEED 4 IS UNRELIABLE!!!
1643 if ((result = usbat_write_block(us,
1644 USBAT_ATA, srb->cmnd, 12,
1645 (srb->cmnd[0]==GPCMD_BLANK ? 75 : 10), 0) !=
1646 USB_STOR_TRANSPORT_GOOD)) {
1650 /* If there is response data to be read in then do it here. */
1652 if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1654 /* How many bytes to read in? Check cylL register */
1656 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1657 USB_STOR_XFER_GOOD) {
1658 return USB_STOR_TRANSPORT_ERROR;
1661 if (len > 0xFF) { /* need to read cylH also */
1663 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1664 USB_STOR_XFER_GOOD) {
1665 return USB_STOR_TRANSPORT_ERROR;
1667 len += ((unsigned int) *status)<<8;
1673 result = usbat_read_block(us, scsi_sglist(srb), len,
1674 scsi_sg_count(srb));
1681 * Transport for USBAT02-based CompactFlash and similar storage devices
1683 static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1686 struct usbat_info *info = (struct usbat_info *) (us->extra);
1687 unsigned long block, blocks;
1688 unsigned char *ptr = us->iobuf;
1689 static unsigned char inquiry_response[36] = {
1690 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1693 if (srb->cmnd[0] == INQUIRY) {
1694 US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1695 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1696 fill_inquiry_response(us, ptr, 36);
1697 return USB_STOR_TRANSPORT_GOOD;
1700 if (srb->cmnd[0] == READ_CAPACITY) {
1701 rc = usbat_flash_check_media(us, info);
1702 if (rc != USB_STOR_TRANSPORT_GOOD)
1705 rc = usbat_flash_get_sector_count(us, info);
1706 if (rc != USB_STOR_TRANSPORT_GOOD)
1709 /* hard coded 512 byte sectors as per ATA spec */
1710 info->ssize = 0x200;
1711 US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1712 info->sectors, info->ssize);
1716 * note: must return the sector number of the last sector,
1717 * *not* the total number of sectors
1719 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1720 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1721 usb_stor_set_xfer_buf(ptr, 8, srb);
1723 return USB_STOR_TRANSPORT_GOOD;
1726 if (srb->cmnd[0] == MODE_SELECT_10) {
1727 US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
1728 return USB_STOR_TRANSPORT_ERROR;
1731 if (srb->cmnd[0] == READ_10) {
1732 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1733 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1735 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1737 US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
1738 return usbat_flash_read_data(us, info, block, blocks);
1741 if (srb->cmnd[0] == READ_12) {
1743 * I don't think we'll ever see a READ_12 but support it anyway
1745 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1746 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1748 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1749 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1751 US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
1752 return usbat_flash_read_data(us, info, block, blocks);
1755 if (srb->cmnd[0] == WRITE_10) {
1756 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1757 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1759 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1761 US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
1762 return usbat_flash_write_data(us, info, block, blocks);
1765 if (srb->cmnd[0] == WRITE_12) {
1767 * I don't think we'll ever see a WRITE_12 but support it anyway
1769 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1770 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1772 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1773 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1775 US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
1776 return usbat_flash_write_data(us, info, block, blocks);
1780 if (srb->cmnd[0] == TEST_UNIT_READY) {
1781 US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
1783 rc = usbat_flash_check_media(us, info);
1784 if (rc != USB_STOR_TRANSPORT_GOOD)
1787 return usbat_check_status(us);
1790 if (srb->cmnd[0] == REQUEST_SENSE) {
1791 US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
1795 ptr[2] = info->sense_key;
1797 ptr[12] = info->sense_asc;
1798 ptr[13] = info->sense_ascq;
1799 usb_stor_set_xfer_buf(ptr, 18, srb);
1801 return USB_STOR_TRANSPORT_GOOD;
1804 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1806 * sure. whatever. not like we can stop the user from popping
1807 * the media out of the device (no locking doors, etc)
1809 return USB_STOR_TRANSPORT_GOOD;
1812 US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1813 srb->cmnd[0], srb->cmnd[0]);
1814 info->sense_key = 0x05;
1815 info->sense_asc = 0x20;
1816 info->sense_ascq = 0x00;
1817 return USB_STOR_TRANSPORT_FAILED;
1820 static int init_usbat_cd(struct us_data *us)
1822 return init_usbat(us, USBAT_DEV_HP8200);
1825 static int init_usbat_flash(struct us_data *us)
1827 return init_usbat(us, USBAT_DEV_FLASH);
1830 static int usbat_probe(struct usb_interface *intf,
1831 const struct usb_device_id *id)
1836 result = usb_stor_probe1(&us, intf, id,
1837 (id - usbat_usb_ids) + usbat_unusual_dev_list);
1841 /* The actual transport will be determined later by the
1842 * initialization routine; this is just a placeholder.
1844 us->transport_name = "Shuttle USBAT";
1845 us->transport = usbat_flash_transport;
1846 us->transport_reset = usb_stor_CB_reset;
1849 result = usb_stor_probe2(us);
1853 static struct usb_driver usbat_driver = {
1854 .name = "ums-usbat",
1855 .probe = usbat_probe,
1856 .disconnect = usb_stor_disconnect,
1857 .suspend = usb_stor_suspend,
1858 .resume = usb_stor_resume,
1859 .reset_resume = usb_stor_reset_resume,
1860 .pre_reset = usb_stor_pre_reset,
1861 .post_reset = usb_stor_post_reset,
1862 .id_table = usbat_usb_ids,
1866 static int __init usbat_init(void)
1868 return usb_register(&usbat_driver);
1871 static void __exit usbat_exit(void)
1873 usb_deregister(&usbat_driver);
1876 module_init(usbat_init);
1877 module_exit(usbat_exit);