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
28 * See the Kconfig help text for a list of devices known to be supported by
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
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/sched.h>
47 #include <linux/errno.h>
48 #include <linux/slab.h>
49 #include <linux/cdrom.h>
51 #include <scsi/scsi.h>
52 #include <scsi/scsi_cmnd.h>
55 #include "transport.h"
58 #include "shuttle_usbat.h"
60 #define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
61 #define LSB_of(s) ((s)&0xFF)
62 #define MSB_of(s) ((s)>>8)
64 static int transferred = 0;
66 static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us);
67 static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us);
70 * Convenience function to produce an ATA read/write sectors command
71 * Use cmd=0x20 for read, cmd=0x30 for write
73 static void usbat_pack_ata_sector_cmd(unsigned char *buf,
74 unsigned char thistime,
75 u32 sector, unsigned char cmd)
79 buf[2] = sector & 0xFF;
80 buf[3] = (sector >> 8) & 0xFF;
81 buf[4] = (sector >> 16) & 0xFF;
82 buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
87 * Convenience function to get the device type (flash or hp8200)
89 static int usbat_get_device_type(struct us_data *us)
91 return ((struct usbat_info*)us->extra)->devicetype;
95 * Read a register from the device
97 static int usbat_read(struct us_data *us,
100 unsigned char *content)
102 return usb_stor_ctrl_transfer(us,
104 access | USBAT_CMD_READ_REG,
113 * Write to a register on the device
115 static int usbat_write(struct us_data *us,
116 unsigned char access,
118 unsigned char content)
120 return usb_stor_ctrl_transfer(us,
122 access | USBAT_CMD_WRITE_REG,
124 short_pack(reg, content),
131 * Convenience function to perform a bulk read
133 static int usbat_bulk_read(struct us_data *us,
138 return USB_STOR_XFER_GOOD;
140 US_DEBUGP("usbat_bulk_read: len = %d\n", len);
141 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, data, len, NULL);
145 * Convenience function to perform a bulk write
147 static int usbat_bulk_write(struct us_data *us,
152 return USB_STOR_XFER_GOOD;
154 US_DEBUGP("usbat_bulk_write: len = %d\n", len);
155 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, data, len, NULL);
159 * Some USBAT-specific commands can only be executed over a command transport
160 * This transport allows one (len=8) or two (len=16) vendor-specific commands
163 static int usbat_execute_command(struct us_data *us,
164 unsigned char *commands,
167 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
168 USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
173 * Read the status register
175 static int usbat_get_status(struct us_data *us, unsigned char *status)
178 rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
180 US_DEBUGP("usbat_get_status: 0x%02X\n", (unsigned short) (*status));
185 * Check the device status
187 static int usbat_check_status(struct us_data *us)
189 unsigned char *reply = us->iobuf;
193 return USB_STOR_TRANSPORT_ERROR;
195 rc = usbat_get_status(us, reply);
196 if (rc != USB_STOR_XFER_GOOD)
197 return USB_STOR_TRANSPORT_FAILED;
199 /* error/check condition (0x51 is ok) */
200 if (*reply & 0x01 && *reply != 0x51)
201 return USB_STOR_TRANSPORT_FAILED;
205 return USB_STOR_TRANSPORT_FAILED;
207 return USB_STOR_TRANSPORT_GOOD;
211 * Stores critical information in internal registers in prepartion for the execution
212 * of a conditional usbat_read_blocks or usbat_write_blocks call.
214 static int usbat_set_shuttle_features(struct us_data *us,
215 unsigned char external_trigger,
216 unsigned char epp_control,
217 unsigned char mask_byte,
218 unsigned char test_pattern,
219 unsigned char subcountH,
220 unsigned char subcountL)
222 unsigned char *command = us->iobuf;
225 command[1] = USBAT_CMD_SET_FEAT;
228 * The only bit relevant to ATA access is bit 6
229 * which defines 8 bit data access (set) or 16 bit (unset)
231 command[2] = epp_control;
234 * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
235 * ET1 and ET2 define an external event to be checked for on event of a
236 * _read_blocks or _write_blocks operation. The read/write will not take
237 * place unless the defined trigger signal is active.
239 command[3] = external_trigger;
242 * The resultant byte of the mask operation (see mask_byte) is compared for
243 * equivalence with this test pattern. If equal, the read/write will take
246 command[4] = test_pattern;
249 * This value is logically ANDed with the status register field specified
250 * in the read/write command.
252 command[5] = mask_byte;
255 * If ALQ is set in the qualifier, this field contains the address of the
256 * registers where the byte count should be read for transferring the data.
257 * If ALQ is not set, then this field contains the number of bytes to be
260 command[6] = subcountL;
261 command[7] = subcountH;
263 return usbat_execute_command(us, command, 8);
267 * Block, waiting for an ATA device to become not busy or to report
268 * an error condition.
270 static int usbat_wait_not_busy(struct us_data *us, int minutes)
274 unsigned char *status = us->iobuf;
276 /* Synchronizing cache on a CDR could take a heck of a long time,
277 * but probably not more than 10 minutes or so. On the other hand,
278 * doing a full blank on a CDRW at speed 1 will take about 75
282 for (i=0; i<1200+minutes*60; i++) {
284 result = usbat_get_status(us, status);
286 if (result!=USB_STOR_XFER_GOOD)
287 return USB_STOR_TRANSPORT_ERROR;
288 if (*status & 0x01) { /* check condition */
289 result = usbat_read(us, USBAT_ATA, 0x10, status);
290 return USB_STOR_TRANSPORT_FAILED;
292 if (*status & 0x20) /* device fault */
293 return USB_STOR_TRANSPORT_FAILED;
295 if ((*status & 0x80)==0x00) { /* not busy */
296 US_DEBUGP("Waited not busy for %d steps\n", i);
297 return USB_STOR_TRANSPORT_GOOD;
301 msleep(10); /* 5 seconds */
303 msleep(50); /* 10 seconds */
305 msleep(100); /* 50 seconds */
307 msleep(1000); /* X minutes */
310 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
312 return USB_STOR_TRANSPORT_FAILED;
316 * Read block data from the data register
318 static int usbat_read_block(struct us_data *us,
319 unsigned char *content,
323 unsigned char *command = us->iobuf;
326 return USB_STOR_TRANSPORT_GOOD;
329 command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
330 command[2] = USBAT_ATA_DATA;
334 command[6] = LSB_of(len);
335 command[7] = MSB_of(len);
337 result = usbat_execute_command(us, command, 8);
338 if (result != USB_STOR_XFER_GOOD)
339 return USB_STOR_TRANSPORT_ERROR;
341 result = usbat_bulk_read(us, content, len);
342 return (result == USB_STOR_XFER_GOOD ?
343 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
347 * Write block data via the data register
349 static int usbat_write_block(struct us_data *us,
350 unsigned char access,
351 unsigned char *content,
356 unsigned char *command = us->iobuf;
359 return USB_STOR_TRANSPORT_GOOD;
362 command[1] = access | USBAT_CMD_WRITE_BLOCK;
363 command[2] = USBAT_ATA_DATA;
367 command[6] = LSB_of(len);
368 command[7] = MSB_of(len);
370 result = usbat_execute_command(us, command, 8);
372 if (result != USB_STOR_XFER_GOOD)
373 return USB_STOR_TRANSPORT_ERROR;
375 result = usbat_bulk_write(us, content, len);
376 if (result != USB_STOR_XFER_GOOD)
377 return USB_STOR_TRANSPORT_ERROR;
379 return usbat_wait_not_busy(us, minutes);
383 * Process read and write requests
385 static int usbat_hp8200e_rw_block_test(struct us_data *us,
386 unsigned char access,
387 unsigned char *registers,
388 unsigned char *data_out,
389 unsigned short num_registers,
390 unsigned char data_reg,
391 unsigned char status_reg,
392 unsigned char timeout,
393 unsigned char qualifier,
395 unsigned char *content,
401 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
402 us->recv_bulk_pipe : us->send_bulk_pipe;
404 unsigned char *command = us->iobuf;
407 unsigned char *data = us->iobuf;
408 unsigned char *status = us->iobuf;
410 BUG_ON(num_registers > US_IOBUF_SIZE/2);
412 for (i=0; i<20; i++) {
415 * The first time we send the full command, which consists
416 * of downloading the SCSI command followed by downloading
417 * the data via a write-and-test. Any other time we only
418 * send the command to download the data -- the SCSI command
419 * is still 'active' in some sense in the device.
421 * We're only going to try sending the data 10 times. After
422 * that, we just return a failure.
428 * Write to multiple registers
429 * Not really sure the 0x07, 0x17, 0xfc, 0xe7 is
430 * necessary here, but that's what came out of the
431 * trace every single time.
434 command[1] = access | USBAT_CMD_WRITE_REGS;
439 command[6] = LSB_of(num_registers*2);
440 command[7] = MSB_of(num_registers*2);
444 /* Conditionally read or write blocks */
445 command[cmdlen-8] = (direction==DMA_TO_DEVICE ? 0x40 : 0xC0);
446 command[cmdlen-7] = access |
447 (direction==DMA_TO_DEVICE ?
448 USBAT_CMD_COND_WRITE_BLOCK : USBAT_CMD_COND_READ_BLOCK);
449 command[cmdlen-6] = data_reg;
450 command[cmdlen-5] = status_reg;
451 command[cmdlen-4] = timeout;
452 command[cmdlen-3] = qualifier;
453 command[cmdlen-2] = LSB_of(len);
454 command[cmdlen-1] = MSB_of(len);
456 result = usbat_execute_command(us, command, cmdlen);
458 if (result != USB_STOR_XFER_GOOD)
459 return USB_STOR_TRANSPORT_ERROR;
463 for (j=0; j<num_registers; j++) {
464 data[j<<1] = registers[j];
465 data[1+(j<<1)] = data_out[j];
468 result = usbat_bulk_write(us, data, num_registers*2);
469 if (result != USB_STOR_XFER_GOOD)
470 return USB_STOR_TRANSPORT_ERROR;
474 result = usb_stor_bulk_transfer_sg(us,
475 pipe, content, len, use_sg, NULL);
478 * If we get a stall on the bulk download, we'll retry
479 * the bulk download -- but not the SCSI command because
480 * in some sense the SCSI command is still 'active' and
481 * waiting for the data. Don't ask me why this should be;
482 * I'm only following what the Windoze driver did.
484 * Note that a stall for the test-and-read/write command means
485 * that the test failed. In this case we're testing to make
486 * sure that the device is error-free
487 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
488 * hypothesis is that the USBAT chip somehow knows what
489 * the device will accept, but doesn't give the device any
490 * data until all data is received. Thus, the device would
491 * still be waiting for the first byte of data if a stall
492 * occurs, even if the stall implies that some data was
496 if (result == USB_STOR_XFER_SHORT ||
497 result == USB_STOR_XFER_STALLED) {
500 * If we're reading and we stalled, then clear
501 * the bulk output pipe only the first time.
504 if (direction==DMA_FROM_DEVICE && i==0) {
505 if (usb_stor_clear_halt(us,
506 us->send_bulk_pipe) < 0)
507 return USB_STOR_TRANSPORT_ERROR;
511 * Read status: is the device angry, or just busy?
514 result = usbat_read(us, USBAT_ATA,
515 direction==DMA_TO_DEVICE ?
516 USBAT_ATA_STATUS : USBAT_ATA_ALTSTATUS,
519 if (result!=USB_STOR_XFER_GOOD)
520 return USB_STOR_TRANSPORT_ERROR;
521 if (*status & 0x01) /* check condition */
522 return USB_STOR_TRANSPORT_FAILED;
523 if (*status & 0x20) /* device fault */
524 return USB_STOR_TRANSPORT_FAILED;
526 US_DEBUGP("Redoing %s\n",
527 direction==DMA_TO_DEVICE ? "write" : "read");
529 } else if (result != USB_STOR_XFER_GOOD)
530 return USB_STOR_TRANSPORT_ERROR;
532 return usbat_wait_not_busy(us, minutes);
536 US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
537 direction==DMA_TO_DEVICE ? "Writing" : "Reading");
539 return USB_STOR_TRANSPORT_FAILED;
543 * Write to multiple registers:
544 * Allows us to write specific data to any registers. The data to be written
545 * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN
546 * which gets sent through bulk out.
547 * Not designed for large transfers of data!
549 static int usbat_multiple_write(struct us_data *us,
550 unsigned char *registers,
551 unsigned char *data_out,
552 unsigned short num_registers)
555 unsigned char *data = us->iobuf;
556 unsigned char *command = us->iobuf;
558 BUG_ON(num_registers > US_IOBUF_SIZE/2);
560 /* Write to multiple registers, ATA access */
562 command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
570 /* Number of bytes to be transferred (incl. addresses and data) */
571 command[6] = LSB_of(num_registers*2);
572 command[7] = MSB_of(num_registers*2);
574 /* The setup command */
575 result = usbat_execute_command(us, command, 8);
576 if (result != USB_STOR_XFER_GOOD)
577 return USB_STOR_TRANSPORT_ERROR;
579 /* Create the reg/data, reg/data sequence */
580 for (i=0; i<num_registers; i++) {
581 data[i<<1] = registers[i];
582 data[1+(i<<1)] = data_out[i];
586 result = usbat_bulk_write(us, data, num_registers*2);
587 if (result != USB_STOR_XFER_GOOD)
588 return USB_STOR_TRANSPORT_ERROR;
590 if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
591 return usbat_wait_not_busy(us, 0);
593 return USB_STOR_TRANSPORT_GOOD;
597 * Conditionally read blocks from device:
598 * Allows us to read blocks from a specific data register, based upon the
599 * condition that a status register can be successfully masked with a status
600 * qualifier. If this condition is not initially met, the read will wait
601 * up until a maximum amount of time has elapsed, as specified by timeout.
602 * The read will start when the condition is met, otherwise the command aborts.
604 * The qualifier defined here is not the value that is masked, it defines
605 * conditions for the write to take place. The actual masked qualifier (and
606 * other related details) are defined beforehand with _set_shuttle_features().
608 static int usbat_read_blocks(struct us_data *us,
609 unsigned char *buffer,
613 unsigned char *command = us->iobuf;
616 command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK;
617 command[2] = USBAT_ATA_DATA;
618 command[3] = USBAT_ATA_STATUS;
619 command[4] = 0xFD; /* Timeout (ms); */
620 command[5] = USBAT_QUAL_FCQ;
621 command[6] = LSB_of(len);
622 command[7] = MSB_of(len);
624 /* Multiple block read setup command */
625 result = usbat_execute_command(us, command, 8);
626 if (result != USB_STOR_XFER_GOOD)
627 return USB_STOR_TRANSPORT_FAILED;
629 /* Read the blocks we just asked for */
630 result = usbat_bulk_read(us, buffer, len);
631 if (result != USB_STOR_XFER_GOOD)
632 return USB_STOR_TRANSPORT_FAILED;
634 return USB_STOR_TRANSPORT_GOOD;
638 * Conditionally write blocks to device:
639 * Allows us to write blocks to a specific data register, based upon the
640 * condition that a status register can be successfully masked with a status
641 * qualifier. If this condition is not initially met, the write will wait
642 * up until a maximum amount of time has elapsed, as specified by timeout.
643 * The read will start when the condition is met, otherwise the command aborts.
645 * The qualifier defined here is not the value that is masked, it defines
646 * conditions for the write to take place. The actual masked qualifier (and
647 * other related details) are defined beforehand with _set_shuttle_features().
649 static int usbat_write_blocks(struct us_data *us,
650 unsigned char *buffer,
654 unsigned char *command = us->iobuf;
657 command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK;
658 command[2] = USBAT_ATA_DATA;
659 command[3] = USBAT_ATA_STATUS;
660 command[4] = 0xFD; /* Timeout (ms) */
661 command[5] = USBAT_QUAL_FCQ;
662 command[6] = LSB_of(len);
663 command[7] = MSB_of(len);
665 /* Multiple block write setup command */
666 result = usbat_execute_command(us, command, 8);
667 if (result != USB_STOR_XFER_GOOD)
668 return USB_STOR_TRANSPORT_FAILED;
671 result = usbat_bulk_write(us, buffer, len);
672 if (result != USB_STOR_XFER_GOOD)
673 return USB_STOR_TRANSPORT_FAILED;
675 return USB_STOR_TRANSPORT_GOOD;
679 * Read the User IO register
681 static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
685 result = usb_stor_ctrl_transfer(us,
694 US_DEBUGP("usbat_read_user_io: UIO register reads %02X\n", (unsigned short) (*data_flags));
700 * Write to the User IO register
702 static int usbat_write_user_io(struct us_data *us,
703 unsigned char enable_flags,
704 unsigned char data_flags)
706 return usb_stor_ctrl_transfer(us,
710 short_pack(enable_flags, data_flags),
718 * Often needed on media change.
720 static int usbat_device_reset(struct us_data *us)
725 * Reset peripheral, enable peripheral control signals
726 * (bring reset signal up)
728 rc = usbat_write_user_io(us,
729 USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
730 USBAT_UIO_EPAD | USBAT_UIO_1);
731 if (rc != USB_STOR_XFER_GOOD)
732 return USB_STOR_TRANSPORT_ERROR;
735 * Enable peripheral control signals
736 * (bring reset signal down)
738 rc = usbat_write_user_io(us,
739 USBAT_UIO_OE1 | USBAT_UIO_OE0,
740 USBAT_UIO_EPAD | USBAT_UIO_1);
741 if (rc != USB_STOR_XFER_GOOD)
742 return USB_STOR_TRANSPORT_ERROR;
744 return USB_STOR_TRANSPORT_GOOD;
750 static int usbat_device_enable_cdt(struct us_data *us)
754 /* Enable peripheral control signals and card detect */
755 rc = usbat_write_user_io(us,
756 USBAT_UIO_ACKD | USBAT_UIO_OE1 | USBAT_UIO_OE0,
757 USBAT_UIO_EPAD | USBAT_UIO_1);
758 if (rc != USB_STOR_XFER_GOOD)
759 return USB_STOR_TRANSPORT_ERROR;
761 return USB_STOR_TRANSPORT_GOOD;
765 * Determine if media is present.
767 static int usbat_flash_check_media_present(unsigned char *uio)
769 if (*uio & USBAT_UIO_UI0) {
770 US_DEBUGP("usbat_flash_check_media_present: no media detected\n");
771 return USBAT_FLASH_MEDIA_NONE;
774 return USBAT_FLASH_MEDIA_CF;
778 * Determine if media has changed since last operation
780 static int usbat_flash_check_media_changed(unsigned char *uio)
782 if (*uio & USBAT_UIO_0) {
783 US_DEBUGP("usbat_flash_check_media_changed: media change detected\n");
784 return USBAT_FLASH_MEDIA_CHANGED;
787 return USBAT_FLASH_MEDIA_SAME;
791 * Check for media change / no media and handle the situation appropriately
793 static int usbat_flash_check_media(struct us_data *us,
794 struct usbat_info *info)
797 unsigned char *uio = us->iobuf;
799 rc = usbat_read_user_io(us, uio);
800 if (rc != USB_STOR_XFER_GOOD)
801 return USB_STOR_TRANSPORT_ERROR;
803 /* Check for media existence */
804 rc = usbat_flash_check_media_present(uio);
805 if (rc == USBAT_FLASH_MEDIA_NONE) {
806 info->sense_key = 0x02;
807 info->sense_asc = 0x3A;
808 info->sense_ascq = 0x00;
809 return USB_STOR_TRANSPORT_FAILED;
812 /* Check for media change */
813 rc = usbat_flash_check_media_changed(uio);
814 if (rc == USBAT_FLASH_MEDIA_CHANGED) {
816 /* Reset and re-enable card detect */
817 rc = usbat_device_reset(us);
818 if (rc != USB_STOR_TRANSPORT_GOOD)
820 rc = usbat_device_enable_cdt(us);
821 if (rc != USB_STOR_TRANSPORT_GOOD)
826 rc = usbat_read_user_io(us, uio);
827 if (rc != USB_STOR_XFER_GOOD)
828 return USB_STOR_TRANSPORT_ERROR;
830 info->sense_key = UNIT_ATTENTION;
831 info->sense_asc = 0x28;
832 info->sense_ascq = 0x00;
833 return USB_STOR_TRANSPORT_FAILED;
836 return USB_STOR_TRANSPORT_GOOD;
840 * Determine whether we are controlling a flash-based reader/writer,
841 * or a HP8200-based CD drive.
842 * Sets transport functions as appropriate.
844 static int usbat_identify_device(struct us_data *us,
845 struct usbat_info *info)
848 unsigned char status;
851 return USB_STOR_TRANSPORT_ERROR;
853 rc = usbat_device_reset(us);
854 if (rc != USB_STOR_TRANSPORT_GOOD)
859 * In attempt to distinguish between HP CDRW's and Flash readers, we now
860 * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
861 * readers), this command should fail with error. On ATAPI devices (i.e.
862 * CDROM drives), it should succeed.
864 rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1);
865 if (rc != USB_STOR_XFER_GOOD)
866 return USB_STOR_TRANSPORT_ERROR;
868 rc = usbat_get_status(us, &status);
869 if (rc != USB_STOR_XFER_GOOD)
870 return USB_STOR_TRANSPORT_ERROR;
872 /* Check for error bit, or if the command 'fell through' */
873 if (status == 0xA1 || !(status & 0x01)) {
874 /* Device is HP 8200 */
875 US_DEBUGP("usbat_identify_device: Detected HP8200 CDRW\n");
876 info->devicetype = USBAT_DEV_HP8200;
878 /* Device is a CompactFlash reader/writer */
879 US_DEBUGP("usbat_identify_device: Detected Flash reader/writer\n");
880 info->devicetype = USBAT_DEV_FLASH;
883 return USB_STOR_TRANSPORT_GOOD;
887 * Set the transport function based on the device type
889 static int usbat_set_transport(struct us_data *us,
890 struct usbat_info *info)
894 if (!info->devicetype) {
895 rc = usbat_identify_device(us, info);
896 if (rc != USB_STOR_TRANSPORT_GOOD) {
897 US_DEBUGP("usbat_set_transport: Could not identify device\n");
902 if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
903 us->transport = usbat_hp8200e_transport;
904 else if (usbat_get_device_type(us) == USBAT_DEV_FLASH)
905 us->transport = usbat_flash_transport;
911 * Read the media capacity
913 static int usbat_flash_get_sector_count(struct us_data *us,
914 struct usbat_info *info)
916 unsigned char registers[3] = {
921 unsigned char command[3] = { 0x01, 0xA0, 0xEC };
922 unsigned char *reply;
923 unsigned char status;
927 return USB_STOR_TRANSPORT_ERROR;
929 reply = kmalloc(512, GFP_NOIO);
931 return USB_STOR_TRANSPORT_ERROR;
933 /* ATA command : IDENTIFY DEVICE */
934 rc = usbat_multiple_write(us, registers, command, 3);
935 if (rc != USB_STOR_XFER_GOOD) {
936 US_DEBUGP("usbat_flash_get_sector_count: Gah! identify_device failed\n");
937 rc = USB_STOR_TRANSPORT_ERROR;
941 /* Read device status */
942 if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
943 rc = USB_STOR_TRANSPORT_ERROR;
949 /* Read the device identification data */
950 rc = usbat_read_block(us, reply, 512);
951 if (rc != USB_STOR_TRANSPORT_GOOD)
954 info->sectors = ((u32)(reply[117]) << 24) |
955 ((u32)(reply[116]) << 16) |
956 ((u32)(reply[115]) << 8) |
957 ((u32)(reply[114]) );
959 rc = USB_STOR_TRANSPORT_GOOD;
967 * Read data from device
969 static int usbat_flash_read_data(struct us_data *us,
970 struct usbat_info *info,
974 unsigned char registers[7] = {
983 unsigned char command[7];
984 unsigned char *buffer;
985 unsigned char thistime;
986 unsigned int totallen, alloclen;
988 unsigned int sg_idx = 0, sg_offset = 0;
990 result = usbat_flash_check_media(us, info);
991 if (result != USB_STOR_TRANSPORT_GOOD)
995 * we're working in LBA mode. according to the ATA spec,
996 * we can support up to 28-bit addressing. I don't know if Jumpshot
997 * supports beyond 24-bit addressing. It's kind of hard to test
998 * since it requires > 8GB CF card.
1001 if (sector > 0x0FFFFFFF)
1002 return USB_STOR_TRANSPORT_ERROR;
1004 totallen = sectors * info->ssize;
1007 * Since we don't read more than 64 KB at a time, we have to create
1008 * a bounce buffer and move the data a piece at a time between the
1009 * bounce buffer and the actual transfer buffer.
1012 alloclen = min(totallen, 65536u);
1013 buffer = kmalloc(alloclen, GFP_NOIO);
1015 return USB_STOR_TRANSPORT_ERROR;
1019 * loop, never allocate or transfer more than 64k at once
1020 * (min(128k, 255*info->ssize) is the real limit)
1022 len = min(totallen, alloclen);
1023 thistime = (len / info->ssize) & 0xff;
1025 /* ATA command 0x20 (READ SECTORS) */
1026 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
1028 /* Write/execute ATA read command */
1029 result = usbat_multiple_write(us, registers, command, 7);
1030 if (result != USB_STOR_TRANSPORT_GOOD)
1033 /* Read the data we just requested */
1034 result = usbat_read_blocks(us, buffer, len);
1035 if (result != USB_STOR_TRANSPORT_GOOD)
1038 US_DEBUGP("usbat_flash_read_data: %d bytes\n", len);
1040 /* Store the data in the transfer buffer */
1041 usb_stor_access_xfer_buf(buffer, len, us->srb,
1042 &sg_idx, &sg_offset, TO_XFER_BUF);
1046 } while (totallen > 0);
1049 return USB_STOR_TRANSPORT_GOOD;
1053 return USB_STOR_TRANSPORT_ERROR;
1057 * Write data to device
1059 static int usbat_flash_write_data(struct us_data *us,
1060 struct usbat_info *info,
1064 unsigned char registers[7] = {
1073 unsigned char command[7];
1074 unsigned char *buffer;
1075 unsigned char thistime;
1076 unsigned int totallen, alloclen;
1078 unsigned int sg_idx = 0, sg_offset = 0;
1080 result = usbat_flash_check_media(us, info);
1081 if (result != USB_STOR_TRANSPORT_GOOD)
1085 * we're working in LBA mode. according to the ATA spec,
1086 * we can support up to 28-bit addressing. I don't know if the device
1087 * supports beyond 24-bit addressing. It's kind of hard to test
1088 * since it requires > 8GB media.
1091 if (sector > 0x0FFFFFFF)
1092 return USB_STOR_TRANSPORT_ERROR;
1094 totallen = sectors * info->ssize;
1097 * Since we don't write more than 64 KB at a time, we have to create
1098 * a bounce buffer and move the data a piece at a time between the
1099 * bounce buffer and the actual transfer buffer.
1102 alloclen = min(totallen, 65536u);
1103 buffer = kmalloc(alloclen, GFP_NOIO);
1105 return USB_STOR_TRANSPORT_ERROR;
1109 * loop, never allocate or transfer more than 64k at once
1110 * (min(128k, 255*info->ssize) is the real limit)
1112 len = min(totallen, alloclen);
1113 thistime = (len / info->ssize) & 0xff;
1115 /* Get the data from the transfer buffer */
1116 usb_stor_access_xfer_buf(buffer, len, us->srb,
1117 &sg_idx, &sg_offset, FROM_XFER_BUF);
1119 /* ATA command 0x30 (WRITE SECTORS) */
1120 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
1122 /* Write/execute ATA write command */
1123 result = usbat_multiple_write(us, registers, command, 7);
1124 if (result != USB_STOR_TRANSPORT_GOOD)
1127 /* Write the data */
1128 result = usbat_write_blocks(us, buffer, len);
1129 if (result != USB_STOR_TRANSPORT_GOOD)
1134 } while (totallen > 0);
1141 return USB_STOR_TRANSPORT_ERROR;
1145 * Squeeze a potentially huge (> 65535 byte) read10 command into
1146 * a little ( <= 65535 byte) ATAPI pipe
1148 static int usbat_hp8200e_handle_read10(struct us_data *us,
1149 unsigned char *registers,
1150 unsigned char *data,
1151 struct scsi_cmnd *srb)
1153 int result = USB_STOR_TRANSPORT_GOOD;
1154 unsigned char *buffer;
1156 unsigned int sector;
1157 unsigned int sg_segment = 0;
1158 unsigned int sg_offset = 0;
1160 US_DEBUGP("handle_read10: transfersize %d\n",
1163 if (srb->request_bufflen < 0x10000) {
1165 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1166 registers, data, 19,
1167 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1168 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1170 srb->request_buffer,
1171 srb->request_bufflen, srb->use_sg, 1);
1177 * Since we're requesting more data than we can handle in
1178 * a single read command (max is 64k-1), we will perform
1179 * multiple reads, but each read must be in multiples of
1180 * a sector. Luckily the sector size is in srb->transfersize
1181 * (see linux/drivers/scsi/sr.c).
1184 if (data[7+0] == GPCMD_READ_CD) {
1185 len = short_pack(data[7+9], data[7+8]);
1188 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
1189 srb->transfersize = srb->request_bufflen/len;
1192 if (!srb->transfersize) {
1193 srb->transfersize = 2048; /* A guess */
1194 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
1199 * Since we only read in one block at a time, we have to create
1200 * a bounce buffer and move the data a piece at a time between the
1201 * bounce buffer and the actual transfer buffer.
1204 len = (65535/srb->transfersize) * srb->transfersize;
1205 US_DEBUGP("Max read is %d bytes\n", len);
1206 len = min(len, srb->request_bufflen);
1207 buffer = kmalloc(len, GFP_NOIO);
1208 if (buffer == NULL) /* bloody hell! */
1209 return USB_STOR_TRANSPORT_FAILED;
1210 sector = short_pack(data[7+3], data[7+2]);
1212 sector |= short_pack(data[7+5], data[7+4]);
1215 sg_segment = 0; /* for keeping track of where we are in */
1216 sg_offset = 0; /* the scatter/gather list */
1218 while (transferred != srb->request_bufflen) {
1220 if (len > srb->request_bufflen - transferred)
1221 len = srb->request_bufflen - transferred;
1223 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1224 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
1226 /* Fix up the SCSI command sector and num sectors */
1228 data[7+2] = MSB_of(sector>>16); /* SCSI command sector */
1229 data[7+3] = LSB_of(sector>>16);
1230 data[7+4] = MSB_of(sector&0xFFFF);
1231 data[7+5] = LSB_of(sector&0xFFFF);
1232 if (data[7+0] == GPCMD_READ_CD)
1234 data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */
1235 data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */
1237 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1238 registers, data, 19,
1239 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1240 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1245 if (result != USB_STOR_TRANSPORT_GOOD)
1248 /* Store the data in the transfer buffer */
1249 usb_stor_access_xfer_buf(buffer, len, srb,
1250 &sg_segment, &sg_offset, TO_XFER_BUF);
1252 /* Update the amount transferred and the sector number */
1255 sector += len / srb->transfersize;
1257 } /* while transferred != srb->request_bufflen */
1263 static int usbat_select_and_test_registers(struct us_data *us)
1266 unsigned char *status = us->iobuf;
1268 /* try device = master, then device = slave. */
1269 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
1270 if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
1272 return USB_STOR_TRANSPORT_ERROR;
1274 if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) !=
1276 return USB_STOR_TRANSPORT_ERROR;
1278 if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) !=
1280 return USB_STOR_TRANSPORT_ERROR;
1282 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1284 return USB_STOR_TRANSPORT_ERROR;
1286 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1288 return USB_STOR_TRANSPORT_ERROR;
1290 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) !=
1292 return USB_STOR_TRANSPORT_ERROR;
1294 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) !=
1296 return USB_STOR_TRANSPORT_ERROR;
1298 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1300 return USB_STOR_TRANSPORT_ERROR;
1302 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1304 return USB_STOR_TRANSPORT_ERROR;
1307 return USB_STOR_TRANSPORT_GOOD;
1311 * Initialize the USBAT processor and the storage device
1313 int init_usbat(struct us_data *us)
1316 struct usbat_info *info;
1317 unsigned char subcountH = USBAT_ATA_LBA_HI;
1318 unsigned char subcountL = USBAT_ATA_LBA_ME;
1319 unsigned char *status = us->iobuf;
1321 us->extra = kmalloc(sizeof(struct usbat_info), GFP_NOIO);
1323 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1326 memset(us->extra, 0, sizeof(struct usbat_info));
1327 info = (struct usbat_info *) (us->extra);
1329 /* Enable peripheral control signals */
1330 rc = usbat_write_user_io(us,
1331 USBAT_UIO_OE1 | USBAT_UIO_OE0,
1332 USBAT_UIO_EPAD | USBAT_UIO_1);
1333 if (rc != USB_STOR_XFER_GOOD)
1334 return USB_STOR_TRANSPORT_ERROR;
1336 US_DEBUGP("INIT 1\n");
1340 rc = usbat_read_user_io(us, status);
1341 if (rc != USB_STOR_TRANSPORT_GOOD)
1344 US_DEBUGP("INIT 2\n");
1346 rc = usbat_read_user_io(us, status);
1347 if (rc != USB_STOR_XFER_GOOD)
1348 return USB_STOR_TRANSPORT_ERROR;
1350 rc = usbat_read_user_io(us, status);
1351 if (rc != USB_STOR_XFER_GOOD)
1352 return USB_STOR_TRANSPORT_ERROR;
1354 US_DEBUGP("INIT 3\n");
1356 rc = usbat_select_and_test_registers(us);
1357 if (rc != USB_STOR_TRANSPORT_GOOD)
1360 US_DEBUGP("INIT 4\n");
1362 rc = usbat_read_user_io(us, status);
1363 if (rc != USB_STOR_XFER_GOOD)
1364 return USB_STOR_TRANSPORT_ERROR;
1366 US_DEBUGP("INIT 5\n");
1368 /* Enable peripheral control signals and card detect */
1369 rc = usbat_device_enable_cdt(us);
1370 if (rc != USB_STOR_TRANSPORT_GOOD)
1373 US_DEBUGP("INIT 6\n");
1375 rc = usbat_read_user_io(us, status);
1376 if (rc != USB_STOR_XFER_GOOD)
1377 return USB_STOR_TRANSPORT_ERROR;
1379 US_DEBUGP("INIT 7\n");
1383 rc = usbat_read_user_io(us, status);
1384 if (rc != USB_STOR_XFER_GOOD)
1385 return USB_STOR_TRANSPORT_ERROR;
1387 US_DEBUGP("INIT 8\n");
1389 rc = usbat_select_and_test_registers(us);
1390 if (rc != USB_STOR_TRANSPORT_GOOD)
1393 US_DEBUGP("INIT 9\n");
1395 /* At this point, we need to detect which device we are using */
1396 if (usbat_set_transport(us, info))
1397 return USB_STOR_TRANSPORT_ERROR;
1399 US_DEBUGP("INIT 10\n");
1401 if (usbat_get_device_type(us) == USBAT_DEV_FLASH) {
1405 rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1406 0x00, 0x88, 0x08, subcountH, subcountL);
1407 if (rc != USB_STOR_XFER_GOOD)
1408 return USB_STOR_TRANSPORT_ERROR;
1410 US_DEBUGP("INIT 11\n");
1412 return USB_STOR_TRANSPORT_GOOD;
1416 * Transport for the HP 8200e
1418 static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1421 unsigned char *status = us->iobuf;
1422 unsigned char registers[32];
1423 unsigned char data[32];
1428 len = srb->request_bufflen;
1430 /* Send A0 (ATA PACKET COMMAND).
1431 Note: I guess we're never going to get any of the ATA
1432 commands... just ATA Packet Commands.
1435 registers[0] = USBAT_ATA_FEATURES;
1436 registers[1] = USBAT_ATA_SECCNT;
1437 registers[2] = USBAT_ATA_SECNUM;
1438 registers[3] = USBAT_ATA_LBA_ME;
1439 registers[4] = USBAT_ATA_LBA_HI;
1440 registers[5] = USBAT_ATA_DEVICE;
1441 registers[6] = USBAT_ATA_CMD;
1445 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1446 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
1447 data[5] = 0xB0; /* (device sel) = slave */
1448 data[6] = 0xA0; /* (command) = ATA PACKET COMMAND */
1450 for (i=7; i<19; i++) {
1451 registers[i] = 0x10;
1452 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1455 result = usbat_get_status(us, status);
1456 US_DEBUGP("Status = %02X\n", *status);
1457 if (result != USB_STOR_XFER_GOOD)
1458 return USB_STOR_TRANSPORT_ERROR;
1459 if (srb->cmnd[0] == TEST_UNIT_READY)
1462 if (srb->sc_data_direction == DMA_TO_DEVICE) {
1464 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1465 registers, data, 19,
1466 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1467 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1469 srb->request_buffer,
1470 len, srb->use_sg, 10);
1472 if (result == USB_STOR_TRANSPORT_GOOD) {
1474 US_DEBUGP("Wrote %08X bytes\n", transferred);
1479 } else if (srb->cmnd[0] == READ_10 ||
1480 srb->cmnd[0] == GPCMD_READ_CD) {
1482 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1487 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1489 return USB_STOR_TRANSPORT_ERROR;
1492 if ( (result = usbat_multiple_write(us,
1493 registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
1498 * Write the 12-byte command header.
1500 * If the command is BLANK then set the timer for 75 minutes.
1501 * Otherwise set it for 10 minutes.
1503 * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1504 * AT SPEED 4 IS UNRELIABLE!!!
1507 if ( (result = usbat_write_block(us,
1508 USBAT_ATA, srb->cmnd, 12,
1509 srb->cmnd[0]==GPCMD_BLANK ? 75 : 10)) !=
1510 USB_STOR_TRANSPORT_GOOD) {
1514 /* If there is response data to be read in then do it here. */
1516 if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1518 /* How many bytes to read in? Check cylL register */
1520 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1521 USB_STOR_XFER_GOOD) {
1522 return USB_STOR_TRANSPORT_ERROR;
1525 if (len > 0xFF) { /* need to read cylH also */
1527 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1528 USB_STOR_XFER_GOOD) {
1529 return USB_STOR_TRANSPORT_ERROR;
1531 len += ((unsigned int) *status)<<8;
1537 result = usbat_read_block(us, srb->request_buffer, len);
1539 /* Debug-print the first 32 bytes of the transfer */
1543 for (i=0; i<len && i<32; i++) {
1544 sprintf(string+strlen(string), "%02X ",
1545 ((unsigned char *)srb->request_buffer)[i]);
1547 US_DEBUGP("%s\n", string);
1552 US_DEBUGP("%s\n", string);
1560 * Transport for USBAT02-based CompactFlash and similar storage devices
1562 static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1565 struct usbat_info *info = (struct usbat_info *) (us->extra);
1566 unsigned long block, blocks;
1567 unsigned char *ptr = us->iobuf;
1568 static unsigned char inquiry_response[36] = {
1569 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1572 if (srb->cmnd[0] == INQUIRY) {
1573 US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1574 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1575 fill_inquiry_response(us, ptr, 36);
1576 return USB_STOR_TRANSPORT_GOOD;
1579 if (srb->cmnd[0] == READ_CAPACITY) {
1580 rc = usbat_flash_check_media(us, info);
1581 if (rc != USB_STOR_TRANSPORT_GOOD)
1584 rc = usbat_flash_get_sector_count(us, info);
1585 if (rc != USB_STOR_TRANSPORT_GOOD)
1588 /* hard coded 512 byte sectors as per ATA spec */
1589 info->ssize = 0x200;
1590 US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1591 info->sectors, info->ssize);
1595 * note: must return the sector number of the last sector,
1596 * *not* the total number of sectors
1598 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1599 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1600 usb_stor_set_xfer_buf(ptr, 8, srb);
1602 return USB_STOR_TRANSPORT_GOOD;
1605 if (srb->cmnd[0] == MODE_SELECT_10) {
1606 US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
1607 return USB_STOR_TRANSPORT_ERROR;
1610 if (srb->cmnd[0] == READ_10) {
1611 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1612 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1614 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1616 US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
1617 return usbat_flash_read_data(us, info, block, blocks);
1620 if (srb->cmnd[0] == READ_12) {
1622 * I don't think we'll ever see a READ_12 but support it anyway
1624 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1625 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1627 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1628 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1630 US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
1631 return usbat_flash_read_data(us, info, block, blocks);
1634 if (srb->cmnd[0] == WRITE_10) {
1635 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1636 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1638 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1640 US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
1641 return usbat_flash_write_data(us, info, block, blocks);
1644 if (srb->cmnd[0] == WRITE_12) {
1646 * I don't think we'll ever see a WRITE_12 but support it anyway
1648 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1649 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1651 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1652 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1654 US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
1655 return usbat_flash_write_data(us, info, block, blocks);
1659 if (srb->cmnd[0] == TEST_UNIT_READY) {
1660 US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
1662 rc = usbat_flash_check_media(us, info);
1663 if (rc != USB_STOR_TRANSPORT_GOOD)
1666 return usbat_check_status(us);
1669 if (srb->cmnd[0] == REQUEST_SENSE) {
1670 US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
1674 ptr[2] = info->sense_key;
1676 ptr[12] = info->sense_asc;
1677 ptr[13] = info->sense_ascq;
1678 usb_stor_set_xfer_buf(ptr, 18, srb);
1680 return USB_STOR_TRANSPORT_GOOD;
1683 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1685 * sure. whatever. not like we can stop the user from popping
1686 * the media out of the device (no locking doors, etc)
1688 return USB_STOR_TRANSPORT_GOOD;
1691 US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1692 srb->cmnd[0], srb->cmnd[0]);
1693 info->sense_key = 0x05;
1694 info->sense_asc = 0x20;
1695 info->sense_ascq = 0x00;
1696 return USB_STOR_TRANSPORT_FAILED;
1700 * Default transport function. Attempts to detect which transport function
1701 * should be called, makes it the new default, and calls it.
1703 * This function should never be called. Our usbat_init() function detects the
1704 * device type and changes the us->transport ptr to the transport function
1705 * relevant to the device.
1706 * However, we'll support this impossible(?) case anyway.
1708 int usbat_transport(struct scsi_cmnd *srb, struct us_data *us)
1710 struct usbat_info *info = (struct usbat_info*) (us->extra);
1712 if (usbat_set_transport(us, info))
1713 return USB_STOR_TRANSPORT_ERROR;
1715 return us->transport(srb, us);