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,
139 return USB_STOR_XFER_GOOD;
141 US_DEBUGP("usbat_bulk_read: len = %d\n", len);
142 return usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe, data, len, use_sg, NULL);
146 * Convenience function to perform a bulk write
148 static int usbat_bulk_write(struct us_data *us,
154 return USB_STOR_XFER_GOOD;
156 US_DEBUGP("usbat_bulk_write: len = %d\n", len);
157 return usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe, data, len, use_sg, NULL);
161 * Some USBAT-specific commands can only be executed over a command transport
162 * This transport allows one (len=8) or two (len=16) vendor-specific commands
165 static int usbat_execute_command(struct us_data *us,
166 unsigned char *commands,
169 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
170 USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
175 * Read the status register
177 static int usbat_get_status(struct us_data *us, unsigned char *status)
180 rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
182 US_DEBUGP("usbat_get_status: 0x%02X\n", (unsigned short) (*status));
187 * Check the device status
189 static int usbat_check_status(struct us_data *us)
191 unsigned char *reply = us->iobuf;
195 return USB_STOR_TRANSPORT_ERROR;
197 rc = usbat_get_status(us, reply);
198 if (rc != USB_STOR_XFER_GOOD)
199 return USB_STOR_TRANSPORT_FAILED;
201 /* error/check condition (0x51 is ok) */
202 if (*reply & 0x01 && *reply != 0x51)
203 return USB_STOR_TRANSPORT_FAILED;
207 return USB_STOR_TRANSPORT_FAILED;
209 return USB_STOR_TRANSPORT_GOOD;
213 * Stores critical information in internal registers in prepartion for the execution
214 * of a conditional usbat_read_blocks or usbat_write_blocks call.
216 static int usbat_set_shuttle_features(struct us_data *us,
217 unsigned char external_trigger,
218 unsigned char epp_control,
219 unsigned char mask_byte,
220 unsigned char test_pattern,
221 unsigned char subcountH,
222 unsigned char subcountL)
224 unsigned char *command = us->iobuf;
227 command[1] = USBAT_CMD_SET_FEAT;
230 * The only bit relevant to ATA access is bit 6
231 * which defines 8 bit data access (set) or 16 bit (unset)
233 command[2] = epp_control;
236 * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
237 * ET1 and ET2 define an external event to be checked for on event of a
238 * _read_blocks or _write_blocks operation. The read/write will not take
239 * place unless the defined trigger signal is active.
241 command[3] = external_trigger;
244 * The resultant byte of the mask operation (see mask_byte) is compared for
245 * equivalence with this test pattern. If equal, the read/write will take
248 command[4] = test_pattern;
251 * This value is logically ANDed with the status register field specified
252 * in the read/write command.
254 command[5] = mask_byte;
257 * If ALQ is set in the qualifier, this field contains the address of the
258 * registers where the byte count should be read for transferring the data.
259 * If ALQ is not set, then this field contains the number of bytes to be
262 command[6] = subcountL;
263 command[7] = subcountH;
265 return usbat_execute_command(us, command, 8);
269 * Block, waiting for an ATA device to become not busy or to report
270 * an error condition.
272 static int usbat_wait_not_busy(struct us_data *us, int minutes)
276 unsigned char *status = us->iobuf;
278 /* Synchronizing cache on a CDR could take a heck of a long time,
279 * but probably not more than 10 minutes or so. On the other hand,
280 * doing a full blank on a CDRW at speed 1 will take about 75
284 for (i=0; i<1200+minutes*60; i++) {
286 result = usbat_get_status(us, status);
288 if (result!=USB_STOR_XFER_GOOD)
289 return USB_STOR_TRANSPORT_ERROR;
290 if (*status & 0x01) { /* check condition */
291 result = usbat_read(us, USBAT_ATA, 0x10, status);
292 return USB_STOR_TRANSPORT_FAILED;
294 if (*status & 0x20) /* device fault */
295 return USB_STOR_TRANSPORT_FAILED;
297 if ((*status & 0x80)==0x00) { /* not busy */
298 US_DEBUGP("Waited not busy for %d steps\n", i);
299 return USB_STOR_TRANSPORT_GOOD;
303 msleep(10); /* 5 seconds */
305 msleep(50); /* 10 seconds */
307 msleep(100); /* 50 seconds */
309 msleep(1000); /* X minutes */
312 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
314 return USB_STOR_TRANSPORT_FAILED;
318 * Read block data from the data register
320 static int usbat_read_block(struct us_data *us,
321 unsigned char *content,
326 unsigned char *command = us->iobuf;
329 return USB_STOR_TRANSPORT_GOOD;
332 command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
333 command[2] = USBAT_ATA_DATA;
337 command[6] = LSB_of(len);
338 command[7] = MSB_of(len);
340 result = usbat_execute_command(us, command, 8);
341 if (result != USB_STOR_XFER_GOOD)
342 return USB_STOR_TRANSPORT_ERROR;
344 result = usbat_bulk_read(us, content, len, use_sg);
345 return (result == USB_STOR_XFER_GOOD ?
346 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
350 * Write block data via the data register
352 static int usbat_write_block(struct us_data *us,
353 unsigned char access,
354 unsigned char *content,
360 unsigned char *command = us->iobuf;
363 return USB_STOR_TRANSPORT_GOOD;
366 command[1] = access | USBAT_CMD_WRITE_BLOCK;
367 command[2] = USBAT_ATA_DATA;
371 command[6] = LSB_of(len);
372 command[7] = MSB_of(len);
374 result = usbat_execute_command(us, command, 8);
376 if (result != USB_STOR_XFER_GOOD)
377 return USB_STOR_TRANSPORT_ERROR;
379 result = usbat_bulk_write(us, content, len, use_sg);
380 if (result != USB_STOR_XFER_GOOD)
381 return USB_STOR_TRANSPORT_ERROR;
383 return usbat_wait_not_busy(us, minutes);
387 * Process read and write requests
389 static int usbat_hp8200e_rw_block_test(struct us_data *us,
390 unsigned char access,
391 unsigned char *registers,
392 unsigned char *data_out,
393 unsigned short num_registers,
394 unsigned char data_reg,
395 unsigned char status_reg,
396 unsigned char timeout,
397 unsigned char qualifier,
399 unsigned char *content,
405 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
406 us->recv_bulk_pipe : us->send_bulk_pipe;
408 unsigned char *command = us->iobuf;
411 unsigned char *data = us->iobuf;
412 unsigned char *status = us->iobuf;
414 BUG_ON(num_registers > US_IOBUF_SIZE/2);
416 for (i=0; i<20; i++) {
419 * The first time we send the full command, which consists
420 * of downloading the SCSI command followed by downloading
421 * the data via a write-and-test. Any other time we only
422 * send the command to download the data -- the SCSI command
423 * is still 'active' in some sense in the device.
425 * We're only going to try sending the data 10 times. After
426 * that, we just return a failure.
432 * Write to multiple registers
433 * Not really sure the 0x07, 0x17, 0xfc, 0xe7 is
434 * necessary here, but that's what came out of the
435 * trace every single time.
438 command[1] = access | USBAT_CMD_WRITE_REGS;
443 command[6] = LSB_of(num_registers*2);
444 command[7] = MSB_of(num_registers*2);
448 /* Conditionally read or write blocks */
449 command[cmdlen-8] = (direction==DMA_TO_DEVICE ? 0x40 : 0xC0);
450 command[cmdlen-7] = access |
451 (direction==DMA_TO_DEVICE ?
452 USBAT_CMD_COND_WRITE_BLOCK : USBAT_CMD_COND_READ_BLOCK);
453 command[cmdlen-6] = data_reg;
454 command[cmdlen-5] = status_reg;
455 command[cmdlen-4] = timeout;
456 command[cmdlen-3] = qualifier;
457 command[cmdlen-2] = LSB_of(len);
458 command[cmdlen-1] = MSB_of(len);
460 result = usbat_execute_command(us, command, cmdlen);
462 if (result != USB_STOR_XFER_GOOD)
463 return USB_STOR_TRANSPORT_ERROR;
467 for (j=0; j<num_registers; j++) {
468 data[j<<1] = registers[j];
469 data[1+(j<<1)] = data_out[j];
472 result = usbat_bulk_write(us, data, num_registers*2, 0);
473 if (result != USB_STOR_XFER_GOOD)
474 return USB_STOR_TRANSPORT_ERROR;
478 result = usb_stor_bulk_transfer_sg(us,
479 pipe, content, len, use_sg, NULL);
482 * If we get a stall on the bulk download, we'll retry
483 * the bulk download -- but not the SCSI command because
484 * in some sense the SCSI command is still 'active' and
485 * waiting for the data. Don't ask me why this should be;
486 * I'm only following what the Windoze driver did.
488 * Note that a stall for the test-and-read/write command means
489 * that the test failed. In this case we're testing to make
490 * sure that the device is error-free
491 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
492 * hypothesis is that the USBAT chip somehow knows what
493 * the device will accept, but doesn't give the device any
494 * data until all data is received. Thus, the device would
495 * still be waiting for the first byte of data if a stall
496 * occurs, even if the stall implies that some data was
500 if (result == USB_STOR_XFER_SHORT ||
501 result == USB_STOR_XFER_STALLED) {
504 * If we're reading and we stalled, then clear
505 * the bulk output pipe only the first time.
508 if (direction==DMA_FROM_DEVICE && i==0) {
509 if (usb_stor_clear_halt(us,
510 us->send_bulk_pipe) < 0)
511 return USB_STOR_TRANSPORT_ERROR;
515 * Read status: is the device angry, or just busy?
518 result = usbat_read(us, USBAT_ATA,
519 direction==DMA_TO_DEVICE ?
520 USBAT_ATA_STATUS : USBAT_ATA_ALTSTATUS,
523 if (result!=USB_STOR_XFER_GOOD)
524 return USB_STOR_TRANSPORT_ERROR;
525 if (*status & 0x01) /* check condition */
526 return USB_STOR_TRANSPORT_FAILED;
527 if (*status & 0x20) /* device fault */
528 return USB_STOR_TRANSPORT_FAILED;
530 US_DEBUGP("Redoing %s\n",
531 direction==DMA_TO_DEVICE ? "write" : "read");
533 } else if (result != USB_STOR_XFER_GOOD)
534 return USB_STOR_TRANSPORT_ERROR;
536 return usbat_wait_not_busy(us, minutes);
540 US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
541 direction==DMA_TO_DEVICE ? "Writing" : "Reading");
543 return USB_STOR_TRANSPORT_FAILED;
547 * Write to multiple registers:
548 * Allows us to write specific data to any registers. The data to be written
549 * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN
550 * which gets sent through bulk out.
551 * Not designed for large transfers of data!
553 static int usbat_multiple_write(struct us_data *us,
554 unsigned char *registers,
555 unsigned char *data_out,
556 unsigned short num_registers)
559 unsigned char *data = us->iobuf;
560 unsigned char *command = us->iobuf;
562 BUG_ON(num_registers > US_IOBUF_SIZE/2);
564 /* Write to multiple registers, ATA access */
566 command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
574 /* Number of bytes to be transferred (incl. addresses and data) */
575 command[6] = LSB_of(num_registers*2);
576 command[7] = MSB_of(num_registers*2);
578 /* The setup command */
579 result = usbat_execute_command(us, command, 8);
580 if (result != USB_STOR_XFER_GOOD)
581 return USB_STOR_TRANSPORT_ERROR;
583 /* Create the reg/data, reg/data sequence */
584 for (i=0; i<num_registers; i++) {
585 data[i<<1] = registers[i];
586 data[1+(i<<1)] = data_out[i];
590 result = usbat_bulk_write(us, data, num_registers*2, 0);
591 if (result != USB_STOR_XFER_GOOD)
592 return USB_STOR_TRANSPORT_ERROR;
594 if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
595 return usbat_wait_not_busy(us, 0);
597 return USB_STOR_TRANSPORT_GOOD;
601 * Conditionally read blocks from device:
602 * Allows us to read blocks from a specific data register, based upon the
603 * condition that a status register can be successfully masked with a status
604 * qualifier. If this condition is not initially met, the read will wait
605 * up until a maximum amount of time has elapsed, as specified by timeout.
606 * The read will start when the condition is met, otherwise the command aborts.
608 * The qualifier defined here is not the value that is masked, it defines
609 * conditions for the write to take place. The actual masked qualifier (and
610 * other related details) are defined beforehand with _set_shuttle_features().
612 static int usbat_read_blocks(struct us_data *us,
613 unsigned char *buffer,
618 unsigned char *command = us->iobuf;
621 command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK;
622 command[2] = USBAT_ATA_DATA;
623 command[3] = USBAT_ATA_STATUS;
624 command[4] = 0xFD; /* Timeout (ms); */
625 command[5] = USBAT_QUAL_FCQ;
626 command[6] = LSB_of(len);
627 command[7] = MSB_of(len);
629 /* Multiple block read setup command */
630 result = usbat_execute_command(us, command, 8);
631 if (result != USB_STOR_XFER_GOOD)
632 return USB_STOR_TRANSPORT_FAILED;
634 /* Read the blocks we just asked for */
635 result = usbat_bulk_read(us, buffer, len, use_sg);
636 if (result != USB_STOR_XFER_GOOD)
637 return USB_STOR_TRANSPORT_FAILED;
639 return USB_STOR_TRANSPORT_GOOD;
643 * Conditionally write blocks to device:
644 * Allows us to write blocks to a specific data register, based upon the
645 * condition that a status register can be successfully masked with a status
646 * qualifier. If this condition is not initially met, the write will wait
647 * up until a maximum amount of time has elapsed, as specified by timeout.
648 * The read will start when the condition is met, otherwise the command aborts.
650 * The qualifier defined here is not the value that is masked, it defines
651 * conditions for the write to take place. The actual masked qualifier (and
652 * other related details) are defined beforehand with _set_shuttle_features().
654 static int usbat_write_blocks(struct us_data *us,
655 unsigned char *buffer,
660 unsigned char *command = us->iobuf;
663 command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK;
664 command[2] = USBAT_ATA_DATA;
665 command[3] = USBAT_ATA_STATUS;
666 command[4] = 0xFD; /* Timeout (ms) */
667 command[5] = USBAT_QUAL_FCQ;
668 command[6] = LSB_of(len);
669 command[7] = MSB_of(len);
671 /* Multiple block write setup command */
672 result = usbat_execute_command(us, command, 8);
673 if (result != USB_STOR_XFER_GOOD)
674 return USB_STOR_TRANSPORT_FAILED;
677 result = usbat_bulk_write(us, buffer, len, use_sg);
678 if (result != USB_STOR_XFER_GOOD)
679 return USB_STOR_TRANSPORT_FAILED;
681 return USB_STOR_TRANSPORT_GOOD;
685 * Read the User IO register
687 static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
691 result = usb_stor_ctrl_transfer(us,
700 US_DEBUGP("usbat_read_user_io: UIO register reads %02X\n", (unsigned short) (*data_flags));
706 * Write to the User IO register
708 static int usbat_write_user_io(struct us_data *us,
709 unsigned char enable_flags,
710 unsigned char data_flags)
712 return usb_stor_ctrl_transfer(us,
716 short_pack(enable_flags, data_flags),
724 * Often needed on media change.
726 static int usbat_device_reset(struct us_data *us)
731 * Reset peripheral, enable peripheral control signals
732 * (bring reset signal up)
734 rc = usbat_write_user_io(us,
735 USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
736 USBAT_UIO_EPAD | USBAT_UIO_1);
737 if (rc != USB_STOR_XFER_GOOD)
738 return USB_STOR_TRANSPORT_ERROR;
741 * Enable peripheral control signals
742 * (bring reset signal down)
744 rc = usbat_write_user_io(us,
745 USBAT_UIO_OE1 | USBAT_UIO_OE0,
746 USBAT_UIO_EPAD | USBAT_UIO_1);
747 if (rc != USB_STOR_XFER_GOOD)
748 return USB_STOR_TRANSPORT_ERROR;
750 return USB_STOR_TRANSPORT_GOOD;
756 static int usbat_device_enable_cdt(struct us_data *us)
760 /* Enable peripheral control signals and card detect */
761 rc = usbat_write_user_io(us,
762 USBAT_UIO_ACKD | USBAT_UIO_OE1 | USBAT_UIO_OE0,
763 USBAT_UIO_EPAD | USBAT_UIO_1);
764 if (rc != USB_STOR_XFER_GOOD)
765 return USB_STOR_TRANSPORT_ERROR;
767 return USB_STOR_TRANSPORT_GOOD;
771 * Determine if media is present.
773 static int usbat_flash_check_media_present(unsigned char *uio)
775 if (*uio & USBAT_UIO_UI0) {
776 US_DEBUGP("usbat_flash_check_media_present: no media detected\n");
777 return USBAT_FLASH_MEDIA_NONE;
780 return USBAT_FLASH_MEDIA_CF;
784 * Determine if media has changed since last operation
786 static int usbat_flash_check_media_changed(unsigned char *uio)
788 if (*uio & USBAT_UIO_0) {
789 US_DEBUGP("usbat_flash_check_media_changed: media change detected\n");
790 return USBAT_FLASH_MEDIA_CHANGED;
793 return USBAT_FLASH_MEDIA_SAME;
797 * Check for media change / no media and handle the situation appropriately
799 static int usbat_flash_check_media(struct us_data *us,
800 struct usbat_info *info)
803 unsigned char *uio = us->iobuf;
805 rc = usbat_read_user_io(us, uio);
806 if (rc != USB_STOR_XFER_GOOD)
807 return USB_STOR_TRANSPORT_ERROR;
809 /* Check for media existence */
810 rc = usbat_flash_check_media_present(uio);
811 if (rc == USBAT_FLASH_MEDIA_NONE) {
812 info->sense_key = 0x02;
813 info->sense_asc = 0x3A;
814 info->sense_ascq = 0x00;
815 return USB_STOR_TRANSPORT_FAILED;
818 /* Check for media change */
819 rc = usbat_flash_check_media_changed(uio);
820 if (rc == USBAT_FLASH_MEDIA_CHANGED) {
822 /* Reset and re-enable card detect */
823 rc = usbat_device_reset(us);
824 if (rc != USB_STOR_TRANSPORT_GOOD)
826 rc = usbat_device_enable_cdt(us);
827 if (rc != USB_STOR_TRANSPORT_GOOD)
832 rc = usbat_read_user_io(us, uio);
833 if (rc != USB_STOR_XFER_GOOD)
834 return USB_STOR_TRANSPORT_ERROR;
836 info->sense_key = UNIT_ATTENTION;
837 info->sense_asc = 0x28;
838 info->sense_ascq = 0x00;
839 return USB_STOR_TRANSPORT_FAILED;
842 return USB_STOR_TRANSPORT_GOOD;
846 * Determine whether we are controlling a flash-based reader/writer,
847 * or a HP8200-based CD drive.
848 * Sets transport functions as appropriate.
850 static int usbat_identify_device(struct us_data *us,
851 struct usbat_info *info)
854 unsigned char status;
857 return USB_STOR_TRANSPORT_ERROR;
859 rc = usbat_device_reset(us);
860 if (rc != USB_STOR_TRANSPORT_GOOD)
865 * In attempt to distinguish between HP CDRW's and Flash readers, we now
866 * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
867 * readers), this command should fail with error. On ATAPI devices (i.e.
868 * CDROM drives), it should succeed.
870 rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1);
871 if (rc != USB_STOR_XFER_GOOD)
872 return USB_STOR_TRANSPORT_ERROR;
874 rc = usbat_get_status(us, &status);
875 if (rc != USB_STOR_XFER_GOOD)
876 return USB_STOR_TRANSPORT_ERROR;
878 /* Check for error bit, or if the command 'fell through' */
879 if (status == 0xA1 || !(status & 0x01)) {
880 /* Device is HP 8200 */
881 US_DEBUGP("usbat_identify_device: Detected HP8200 CDRW\n");
882 info->devicetype = USBAT_DEV_HP8200;
884 /* Device is a CompactFlash reader/writer */
885 US_DEBUGP("usbat_identify_device: Detected Flash reader/writer\n");
886 info->devicetype = USBAT_DEV_FLASH;
889 return USB_STOR_TRANSPORT_GOOD;
893 * Set the transport function based on the device type
895 static int usbat_set_transport(struct us_data *us,
896 struct usbat_info *info)
900 if (!info->devicetype) {
901 rc = usbat_identify_device(us, info);
902 if (rc != USB_STOR_TRANSPORT_GOOD) {
903 US_DEBUGP("usbat_set_transport: Could not identify device\n");
908 if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
909 us->transport = usbat_hp8200e_transport;
910 else if (usbat_get_device_type(us) == USBAT_DEV_FLASH)
911 us->transport = usbat_flash_transport;
917 * Read the media capacity
919 static int usbat_flash_get_sector_count(struct us_data *us,
920 struct usbat_info *info)
922 unsigned char registers[3] = {
927 unsigned char command[3] = { 0x01, 0xA0, 0xEC };
928 unsigned char *reply;
929 unsigned char status;
933 return USB_STOR_TRANSPORT_ERROR;
935 reply = kmalloc(512, GFP_NOIO);
937 return USB_STOR_TRANSPORT_ERROR;
939 /* ATA command : IDENTIFY DEVICE */
940 rc = usbat_multiple_write(us, registers, command, 3);
941 if (rc != USB_STOR_XFER_GOOD) {
942 US_DEBUGP("usbat_flash_get_sector_count: Gah! identify_device failed\n");
943 rc = USB_STOR_TRANSPORT_ERROR;
947 /* Read device status */
948 if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
949 rc = USB_STOR_TRANSPORT_ERROR;
955 /* Read the device identification data */
956 rc = usbat_read_block(us, reply, 512, 0);
957 if (rc != USB_STOR_TRANSPORT_GOOD)
960 info->sectors = ((u32)(reply[117]) << 24) |
961 ((u32)(reply[116]) << 16) |
962 ((u32)(reply[115]) << 8) |
963 ((u32)(reply[114]) );
965 rc = USB_STOR_TRANSPORT_GOOD;
973 * Read data from device
975 static int usbat_flash_read_data(struct us_data *us,
976 struct usbat_info *info,
980 unsigned char registers[7] = {
989 unsigned char command[7];
990 unsigned char *buffer;
991 unsigned char thistime;
992 unsigned int totallen, alloclen;
994 unsigned int sg_idx = 0, sg_offset = 0;
996 result = usbat_flash_check_media(us, info);
997 if (result != USB_STOR_TRANSPORT_GOOD)
1001 * we're working in LBA mode. according to the ATA spec,
1002 * we can support up to 28-bit addressing. I don't know if Jumpshot
1003 * supports beyond 24-bit addressing. It's kind of hard to test
1004 * since it requires > 8GB CF card.
1007 if (sector > 0x0FFFFFFF)
1008 return USB_STOR_TRANSPORT_ERROR;
1010 totallen = sectors * info->ssize;
1013 * Since we don't read more than 64 KB at a time, we have to create
1014 * a bounce buffer and move the data a piece at a time between the
1015 * bounce buffer and the actual transfer buffer.
1018 alloclen = min(totallen, 65536u);
1019 buffer = kmalloc(alloclen, GFP_NOIO);
1021 return USB_STOR_TRANSPORT_ERROR;
1025 * loop, never allocate or transfer more than 64k at once
1026 * (min(128k, 255*info->ssize) is the real limit)
1028 len = min(totallen, alloclen);
1029 thistime = (len / info->ssize) & 0xff;
1031 /* ATA command 0x20 (READ SECTORS) */
1032 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
1034 /* Write/execute ATA read command */
1035 result = usbat_multiple_write(us, registers, command, 7);
1036 if (result != USB_STOR_TRANSPORT_GOOD)
1039 /* Read the data we just requested */
1040 result = usbat_read_blocks(us, buffer, len, 0);
1041 if (result != USB_STOR_TRANSPORT_GOOD)
1044 US_DEBUGP("usbat_flash_read_data: %d bytes\n", len);
1046 /* Store the data in the transfer buffer */
1047 usb_stor_access_xfer_buf(buffer, len, us->srb,
1048 &sg_idx, &sg_offset, TO_XFER_BUF);
1052 } while (totallen > 0);
1055 return USB_STOR_TRANSPORT_GOOD;
1059 return USB_STOR_TRANSPORT_ERROR;
1063 * Write data to device
1065 static int usbat_flash_write_data(struct us_data *us,
1066 struct usbat_info *info,
1070 unsigned char registers[7] = {
1079 unsigned char command[7];
1080 unsigned char *buffer;
1081 unsigned char thistime;
1082 unsigned int totallen, alloclen;
1084 unsigned int sg_idx = 0, sg_offset = 0;
1086 result = usbat_flash_check_media(us, info);
1087 if (result != USB_STOR_TRANSPORT_GOOD)
1091 * we're working in LBA mode. according to the ATA spec,
1092 * we can support up to 28-bit addressing. I don't know if the device
1093 * supports beyond 24-bit addressing. It's kind of hard to test
1094 * since it requires > 8GB media.
1097 if (sector > 0x0FFFFFFF)
1098 return USB_STOR_TRANSPORT_ERROR;
1100 totallen = sectors * info->ssize;
1103 * Since we don't write more than 64 KB at a time, we have to create
1104 * a bounce buffer and move the data a piece at a time between the
1105 * bounce buffer and the actual transfer buffer.
1108 alloclen = min(totallen, 65536u);
1109 buffer = kmalloc(alloclen, GFP_NOIO);
1111 return USB_STOR_TRANSPORT_ERROR;
1115 * loop, never allocate or transfer more than 64k at once
1116 * (min(128k, 255*info->ssize) is the real limit)
1118 len = min(totallen, alloclen);
1119 thistime = (len / info->ssize) & 0xff;
1121 /* Get the data from the transfer buffer */
1122 usb_stor_access_xfer_buf(buffer, len, us->srb,
1123 &sg_idx, &sg_offset, FROM_XFER_BUF);
1125 /* ATA command 0x30 (WRITE SECTORS) */
1126 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
1128 /* Write/execute ATA write command */
1129 result = usbat_multiple_write(us, registers, command, 7);
1130 if (result != USB_STOR_TRANSPORT_GOOD)
1133 /* Write the data */
1134 result = usbat_write_blocks(us, buffer, len, 0);
1135 if (result != USB_STOR_TRANSPORT_GOOD)
1140 } while (totallen > 0);
1147 return USB_STOR_TRANSPORT_ERROR;
1151 * Squeeze a potentially huge (> 65535 byte) read10 command into
1152 * a little ( <= 65535 byte) ATAPI pipe
1154 static int usbat_hp8200e_handle_read10(struct us_data *us,
1155 unsigned char *registers,
1156 unsigned char *data,
1157 struct scsi_cmnd *srb)
1159 int result = USB_STOR_TRANSPORT_GOOD;
1160 unsigned char *buffer;
1162 unsigned int sector;
1163 unsigned int sg_segment = 0;
1164 unsigned int sg_offset = 0;
1166 US_DEBUGP("handle_read10: transfersize %d\n",
1169 if (srb->request_bufflen < 0x10000) {
1171 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1172 registers, data, 19,
1173 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1174 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1176 srb->request_buffer,
1177 srb->request_bufflen, srb->use_sg, 1);
1183 * Since we're requesting more data than we can handle in
1184 * a single read command (max is 64k-1), we will perform
1185 * multiple reads, but each read must be in multiples of
1186 * a sector. Luckily the sector size is in srb->transfersize
1187 * (see linux/drivers/scsi/sr.c).
1190 if (data[7+0] == GPCMD_READ_CD) {
1191 len = short_pack(data[7+9], data[7+8]);
1194 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
1195 srb->transfersize = srb->request_bufflen/len;
1198 if (!srb->transfersize) {
1199 srb->transfersize = 2048; /* A guess */
1200 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
1205 * Since we only read in one block at a time, we have to create
1206 * a bounce buffer and move the data a piece at a time between the
1207 * bounce buffer and the actual transfer buffer.
1210 len = (65535/srb->transfersize) * srb->transfersize;
1211 US_DEBUGP("Max read is %d bytes\n", len);
1212 len = min(len, srb->request_bufflen);
1213 buffer = kmalloc(len, GFP_NOIO);
1214 if (buffer == NULL) /* bloody hell! */
1215 return USB_STOR_TRANSPORT_FAILED;
1216 sector = short_pack(data[7+3], data[7+2]);
1218 sector |= short_pack(data[7+5], data[7+4]);
1221 sg_segment = 0; /* for keeping track of where we are in */
1222 sg_offset = 0; /* the scatter/gather list */
1224 while (transferred != srb->request_bufflen) {
1226 if (len > srb->request_bufflen - transferred)
1227 len = srb->request_bufflen - transferred;
1229 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1230 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
1232 /* Fix up the SCSI command sector and num sectors */
1234 data[7+2] = MSB_of(sector>>16); /* SCSI command sector */
1235 data[7+3] = LSB_of(sector>>16);
1236 data[7+4] = MSB_of(sector&0xFFFF);
1237 data[7+5] = LSB_of(sector&0xFFFF);
1238 if (data[7+0] == GPCMD_READ_CD)
1240 data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */
1241 data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */
1243 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1244 registers, data, 19,
1245 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1246 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1251 if (result != USB_STOR_TRANSPORT_GOOD)
1254 /* Store the data in the transfer buffer */
1255 usb_stor_access_xfer_buf(buffer, len, srb,
1256 &sg_segment, &sg_offset, TO_XFER_BUF);
1258 /* Update the amount transferred and the sector number */
1261 sector += len / srb->transfersize;
1263 } /* while transferred != srb->request_bufflen */
1269 static int usbat_select_and_test_registers(struct us_data *us)
1272 unsigned char *status = us->iobuf;
1274 /* try device = master, then device = slave. */
1275 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
1276 if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
1278 return USB_STOR_TRANSPORT_ERROR;
1280 if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) !=
1282 return USB_STOR_TRANSPORT_ERROR;
1284 if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) !=
1286 return USB_STOR_TRANSPORT_ERROR;
1288 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1290 return USB_STOR_TRANSPORT_ERROR;
1292 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1294 return USB_STOR_TRANSPORT_ERROR;
1296 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) !=
1298 return USB_STOR_TRANSPORT_ERROR;
1300 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) !=
1302 return USB_STOR_TRANSPORT_ERROR;
1304 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1306 return USB_STOR_TRANSPORT_ERROR;
1308 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1310 return USB_STOR_TRANSPORT_ERROR;
1313 return USB_STOR_TRANSPORT_GOOD;
1317 * Initialize the USBAT processor and the storage device
1319 int init_usbat(struct us_data *us)
1322 struct usbat_info *info;
1323 unsigned char subcountH = USBAT_ATA_LBA_HI;
1324 unsigned char subcountL = USBAT_ATA_LBA_ME;
1325 unsigned char *status = us->iobuf;
1327 us->extra = kzalloc(sizeof(struct usbat_info), GFP_NOIO);
1329 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1332 info = (struct usbat_info *) (us->extra);
1334 /* Enable peripheral control signals */
1335 rc = usbat_write_user_io(us,
1336 USBAT_UIO_OE1 | USBAT_UIO_OE0,
1337 USBAT_UIO_EPAD | USBAT_UIO_1);
1338 if (rc != USB_STOR_XFER_GOOD)
1339 return USB_STOR_TRANSPORT_ERROR;
1341 US_DEBUGP("INIT 1\n");
1345 rc = usbat_read_user_io(us, status);
1346 if (rc != USB_STOR_TRANSPORT_GOOD)
1349 US_DEBUGP("INIT 2\n");
1351 rc = usbat_read_user_io(us, status);
1352 if (rc != USB_STOR_XFER_GOOD)
1353 return USB_STOR_TRANSPORT_ERROR;
1355 rc = usbat_read_user_io(us, status);
1356 if (rc != USB_STOR_XFER_GOOD)
1357 return USB_STOR_TRANSPORT_ERROR;
1359 US_DEBUGP("INIT 3\n");
1361 rc = usbat_select_and_test_registers(us);
1362 if (rc != USB_STOR_TRANSPORT_GOOD)
1365 US_DEBUGP("INIT 4\n");
1367 rc = usbat_read_user_io(us, status);
1368 if (rc != USB_STOR_XFER_GOOD)
1369 return USB_STOR_TRANSPORT_ERROR;
1371 US_DEBUGP("INIT 5\n");
1373 /* Enable peripheral control signals and card detect */
1374 rc = usbat_device_enable_cdt(us);
1375 if (rc != USB_STOR_TRANSPORT_GOOD)
1378 US_DEBUGP("INIT 6\n");
1380 rc = usbat_read_user_io(us, status);
1381 if (rc != USB_STOR_XFER_GOOD)
1382 return USB_STOR_TRANSPORT_ERROR;
1384 US_DEBUGP("INIT 7\n");
1388 rc = usbat_read_user_io(us, status);
1389 if (rc != USB_STOR_XFER_GOOD)
1390 return USB_STOR_TRANSPORT_ERROR;
1392 US_DEBUGP("INIT 8\n");
1394 rc = usbat_select_and_test_registers(us);
1395 if (rc != USB_STOR_TRANSPORT_GOOD)
1398 US_DEBUGP("INIT 9\n");
1400 /* At this point, we need to detect which device we are using */
1401 if (usbat_set_transport(us, info))
1402 return USB_STOR_TRANSPORT_ERROR;
1404 US_DEBUGP("INIT 10\n");
1406 if (usbat_get_device_type(us) == USBAT_DEV_FLASH) {
1410 rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1411 0x00, 0x88, 0x08, subcountH, subcountL);
1412 if (rc != USB_STOR_XFER_GOOD)
1413 return USB_STOR_TRANSPORT_ERROR;
1415 US_DEBUGP("INIT 11\n");
1417 return USB_STOR_TRANSPORT_GOOD;
1421 * Transport for the HP 8200e
1423 static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1426 unsigned char *status = us->iobuf;
1427 unsigned char registers[32];
1428 unsigned char data[32];
1433 len = srb->request_bufflen;
1435 /* Send A0 (ATA PACKET COMMAND).
1436 Note: I guess we're never going to get any of the ATA
1437 commands... just ATA Packet Commands.
1440 registers[0] = USBAT_ATA_FEATURES;
1441 registers[1] = USBAT_ATA_SECCNT;
1442 registers[2] = USBAT_ATA_SECNUM;
1443 registers[3] = USBAT_ATA_LBA_ME;
1444 registers[4] = USBAT_ATA_LBA_HI;
1445 registers[5] = USBAT_ATA_DEVICE;
1446 registers[6] = USBAT_ATA_CMD;
1450 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1451 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
1452 data[5] = 0xB0; /* (device sel) = slave */
1453 data[6] = 0xA0; /* (command) = ATA PACKET COMMAND */
1455 for (i=7; i<19; i++) {
1456 registers[i] = 0x10;
1457 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1460 result = usbat_get_status(us, status);
1461 US_DEBUGP("Status = %02X\n", *status);
1462 if (result != USB_STOR_XFER_GOOD)
1463 return USB_STOR_TRANSPORT_ERROR;
1464 if (srb->cmnd[0] == TEST_UNIT_READY)
1467 if (srb->sc_data_direction == DMA_TO_DEVICE) {
1469 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1470 registers, data, 19,
1471 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1472 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1474 srb->request_buffer,
1475 len, srb->use_sg, 10);
1477 if (result == USB_STOR_TRANSPORT_GOOD) {
1479 US_DEBUGP("Wrote %08X bytes\n", transferred);
1484 } else if (srb->cmnd[0] == READ_10 ||
1485 srb->cmnd[0] == GPCMD_READ_CD) {
1487 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1492 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1494 return USB_STOR_TRANSPORT_ERROR;
1497 if ( (result = usbat_multiple_write(us,
1498 registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
1503 * Write the 12-byte command header.
1505 * If the command is BLANK then set the timer for 75 minutes.
1506 * Otherwise set it for 10 minutes.
1508 * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1509 * AT SPEED 4 IS UNRELIABLE!!!
1512 if ((result = usbat_write_block(us,
1513 USBAT_ATA, srb->cmnd, 12,
1514 (srb->cmnd[0]==GPCMD_BLANK ? 75 : 10), 0) !=
1515 USB_STOR_TRANSPORT_GOOD)) {
1519 /* If there is response data to be read in then do it here. */
1521 if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1523 /* How many bytes to read in? Check cylL register */
1525 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1526 USB_STOR_XFER_GOOD) {
1527 return USB_STOR_TRANSPORT_ERROR;
1530 if (len > 0xFF) { /* need to read cylH also */
1532 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1533 USB_STOR_XFER_GOOD) {
1534 return USB_STOR_TRANSPORT_ERROR;
1536 len += ((unsigned int) *status)<<8;
1542 result = usbat_read_block(us, srb->request_buffer, len, srb->use_sg);
1544 /* Debug-print the first 32 bytes of the transfer */
1548 for (i=0; i<len && i<32; i++) {
1549 sprintf(string+strlen(string), "%02X ",
1550 ((unsigned char *)srb->request_buffer)[i]);
1552 US_DEBUGP("%s\n", string);
1557 US_DEBUGP("%s\n", string);
1565 * Transport for USBAT02-based CompactFlash and similar storage devices
1567 static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1570 struct usbat_info *info = (struct usbat_info *) (us->extra);
1571 unsigned long block, blocks;
1572 unsigned char *ptr = us->iobuf;
1573 static unsigned char inquiry_response[36] = {
1574 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1577 if (srb->cmnd[0] == INQUIRY) {
1578 US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1579 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1580 fill_inquiry_response(us, ptr, 36);
1581 return USB_STOR_TRANSPORT_GOOD;
1584 if (srb->cmnd[0] == READ_CAPACITY) {
1585 rc = usbat_flash_check_media(us, info);
1586 if (rc != USB_STOR_TRANSPORT_GOOD)
1589 rc = usbat_flash_get_sector_count(us, info);
1590 if (rc != USB_STOR_TRANSPORT_GOOD)
1593 /* hard coded 512 byte sectors as per ATA spec */
1594 info->ssize = 0x200;
1595 US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1596 info->sectors, info->ssize);
1600 * note: must return the sector number of the last sector,
1601 * *not* the total number of sectors
1603 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1604 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1605 usb_stor_set_xfer_buf(ptr, 8, srb);
1607 return USB_STOR_TRANSPORT_GOOD;
1610 if (srb->cmnd[0] == MODE_SELECT_10) {
1611 US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
1612 return USB_STOR_TRANSPORT_ERROR;
1615 if (srb->cmnd[0] == READ_10) {
1616 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1617 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1619 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1621 US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
1622 return usbat_flash_read_data(us, info, block, blocks);
1625 if (srb->cmnd[0] == READ_12) {
1627 * I don't think we'll ever see a READ_12 but support it anyway
1629 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1630 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1632 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1633 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1635 US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
1636 return usbat_flash_read_data(us, info, block, blocks);
1639 if (srb->cmnd[0] == WRITE_10) {
1640 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1641 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1643 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1645 US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
1646 return usbat_flash_write_data(us, info, block, blocks);
1649 if (srb->cmnd[0] == WRITE_12) {
1651 * I don't think we'll ever see a WRITE_12 but support it anyway
1653 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1654 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1656 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1657 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1659 US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
1660 return usbat_flash_write_data(us, info, block, blocks);
1664 if (srb->cmnd[0] == TEST_UNIT_READY) {
1665 US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
1667 rc = usbat_flash_check_media(us, info);
1668 if (rc != USB_STOR_TRANSPORT_GOOD)
1671 return usbat_check_status(us);
1674 if (srb->cmnd[0] == REQUEST_SENSE) {
1675 US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
1679 ptr[2] = info->sense_key;
1681 ptr[12] = info->sense_asc;
1682 ptr[13] = info->sense_ascq;
1683 usb_stor_set_xfer_buf(ptr, 18, srb);
1685 return USB_STOR_TRANSPORT_GOOD;
1688 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1690 * sure. whatever. not like we can stop the user from popping
1691 * the media out of the device (no locking doors, etc)
1693 return USB_STOR_TRANSPORT_GOOD;
1696 US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1697 srb->cmnd[0], srb->cmnd[0]);
1698 info->sense_key = 0x05;
1699 info->sense_asc = 0x20;
1700 info->sense_ascq = 0x00;
1701 return USB_STOR_TRANSPORT_FAILED;
1705 * Default transport function. Attempts to detect which transport function
1706 * should be called, makes it the new default, and calls it.
1708 * This function should never be called. Our usbat_init() function detects the
1709 * device type and changes the us->transport ptr to the transport function
1710 * relevant to the device.
1711 * However, we'll support this impossible(?) case anyway.
1713 int usbat_transport(struct scsi_cmnd *srb, struct us_data *us)
1715 struct usbat_info *info = (struct usbat_info*) (us->extra);
1717 if (usbat_set_transport(us, info))
1718 return USB_STOR_TRANSPORT_ERROR;
1720 return us->transport(srb, us);