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/errno.h>
47 #include <linux/slab.h>
48 #include <linux/cdrom.h>
50 #include <scsi/scsi.h>
51 #include <scsi/scsi_cmnd.h>
54 #include "transport.h"
57 #include "shuttle_usbat.h"
59 #define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
60 #define LSB_of(s) ((s)&0xFF)
61 #define MSB_of(s) ((s)>>8)
63 static int transferred
= 0;
65 static int usbat_flash_transport(struct scsi_cmnd
* srb
, struct us_data
*us
);
66 static int usbat_hp8200e_transport(struct scsi_cmnd
*srb
, struct us_data
*us
);
69 * Convenience function to produce an ATA read/write sectors command
70 * Use cmd=0x20 for read, cmd=0x30 for write
72 static void usbat_pack_ata_sector_cmd(unsigned char *buf
,
73 unsigned char thistime
,
74 u32 sector
, unsigned char cmd
)
78 buf
[2] = sector
& 0xFF;
79 buf
[3] = (sector
>> 8) & 0xFF;
80 buf
[4] = (sector
>> 16) & 0xFF;
81 buf
[5] = 0xE0 | ((sector
>> 24) & 0x0F);
86 * Convenience function to get the device type (flash or hp8200)
88 static int usbat_get_device_type(struct us_data
*us
)
90 return ((struct usbat_info
*)us
->extra
)->devicetype
;
94 * Read a register from the device
96 static int usbat_read(struct us_data
*us
,
99 unsigned char *content
)
101 return usb_stor_ctrl_transfer(us
,
103 access
| USBAT_CMD_READ_REG
,
112 * Write to a register on the device
114 static int usbat_write(struct us_data
*us
,
115 unsigned char access
,
117 unsigned char content
)
119 return usb_stor_ctrl_transfer(us
,
121 access
| USBAT_CMD_WRITE_REG
,
123 short_pack(reg
, content
),
130 * Convenience function to perform a bulk read
132 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_sg(us
, us
->recv_bulk_pipe
, data
, len
, use_sg
, NULL
);
145 * Convenience function to perform a bulk write
147 static int usbat_bulk_write(struct us_data
*us
,
153 return USB_STOR_XFER_GOOD
;
155 US_DEBUGP("usbat_bulk_write: len = %d\n", len
);
156 return usb_stor_bulk_transfer_sg(us
, us
->send_bulk_pipe
, data
, len
, use_sg
, NULL
);
160 * Some USBAT-specific commands can only be executed over a command transport
161 * This transport allows one (len=8) or two (len=16) vendor-specific commands
164 static int usbat_execute_command(struct us_data
*us
,
165 unsigned char *commands
,
168 return usb_stor_ctrl_transfer(us
, us
->send_ctrl_pipe
,
169 USBAT_CMD_EXEC_CMD
, 0x40, 0, 0,
174 * Read the status register
176 static int usbat_get_status(struct us_data
*us
, unsigned char *status
)
179 rc
= usbat_read(us
, USBAT_ATA
, USBAT_ATA_STATUS
, status
);
181 US_DEBUGP("usbat_get_status: 0x%02X\n", (unsigned short) (*status
));
186 * Check the device status
188 static int usbat_check_status(struct us_data
*us
)
190 unsigned char *reply
= us
->iobuf
;
194 return USB_STOR_TRANSPORT_ERROR
;
196 rc
= usbat_get_status(us
, reply
);
197 if (rc
!= USB_STOR_XFER_GOOD
)
198 return USB_STOR_TRANSPORT_FAILED
;
200 /* error/check condition (0x51 is ok) */
201 if (*reply
& 0x01 && *reply
!= 0x51)
202 return USB_STOR_TRANSPORT_FAILED
;
206 return USB_STOR_TRANSPORT_FAILED
;
208 return USB_STOR_TRANSPORT_GOOD
;
212 * Stores critical information in internal registers in prepartion for the execution
213 * of a conditional usbat_read_blocks or usbat_write_blocks call.
215 static int usbat_set_shuttle_features(struct us_data
*us
,
216 unsigned char external_trigger
,
217 unsigned char epp_control
,
218 unsigned char mask_byte
,
219 unsigned char test_pattern
,
220 unsigned char subcountH
,
221 unsigned char subcountL
)
223 unsigned char *command
= us
->iobuf
;
226 command
[1] = USBAT_CMD_SET_FEAT
;
229 * The only bit relevant to ATA access is bit 6
230 * which defines 8 bit data access (set) or 16 bit (unset)
232 command
[2] = epp_control
;
235 * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
236 * ET1 and ET2 define an external event to be checked for on event of a
237 * _read_blocks or _write_blocks operation. The read/write will not take
238 * place unless the defined trigger signal is active.
240 command
[3] = external_trigger
;
243 * The resultant byte of the mask operation (see mask_byte) is compared for
244 * equivalence with this test pattern. If equal, the read/write will take
247 command
[4] = test_pattern
;
250 * This value is logically ANDed with the status register field specified
251 * in the read/write command.
253 command
[5] = mask_byte
;
256 * If ALQ is set in the qualifier, this field contains the address of the
257 * registers where the byte count should be read for transferring the data.
258 * If ALQ is not set, then this field contains the number of bytes to be
261 command
[6] = subcountL
;
262 command
[7] = subcountH
;
264 return usbat_execute_command(us
, command
, 8);
268 * Block, waiting for an ATA device to become not busy or to report
269 * an error condition.
271 static int usbat_wait_not_busy(struct us_data
*us
, int minutes
)
275 unsigned char *status
= us
->iobuf
;
277 /* Synchronizing cache on a CDR could take a heck of a long time,
278 * but probably not more than 10 minutes or so. On the other hand,
279 * doing a full blank on a CDRW at speed 1 will take about 75
283 for (i
=0; i
<1200+minutes
*60; i
++) {
285 result
= usbat_get_status(us
, status
);
287 if (result
!=USB_STOR_XFER_GOOD
)
288 return USB_STOR_TRANSPORT_ERROR
;
289 if (*status
& 0x01) { /* check condition */
290 result
= usbat_read(us
, USBAT_ATA
, 0x10, status
);
291 return USB_STOR_TRANSPORT_FAILED
;
293 if (*status
& 0x20) /* device fault */
294 return USB_STOR_TRANSPORT_FAILED
;
296 if ((*status
& 0x80)==0x00) { /* not busy */
297 US_DEBUGP("Waited not busy for %d steps\n", i
);
298 return USB_STOR_TRANSPORT_GOOD
;
302 msleep(10); /* 5 seconds */
304 msleep(50); /* 10 seconds */
306 msleep(100); /* 50 seconds */
308 msleep(1000); /* X minutes */
311 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
313 return USB_STOR_TRANSPORT_FAILED
;
317 * Read block data from the data register
319 static int usbat_read_block(struct us_data
*us
,
320 unsigned char *content
,
325 unsigned char *command
= us
->iobuf
;
328 return USB_STOR_TRANSPORT_GOOD
;
331 command
[1] = USBAT_ATA
| USBAT_CMD_READ_BLOCK
;
332 command
[2] = USBAT_ATA_DATA
;
336 command
[6] = LSB_of(len
);
337 command
[7] = MSB_of(len
);
339 result
= usbat_execute_command(us
, command
, 8);
340 if (result
!= USB_STOR_XFER_GOOD
)
341 return USB_STOR_TRANSPORT_ERROR
;
343 result
= usbat_bulk_read(us
, content
, len
, use_sg
);
344 return (result
== USB_STOR_XFER_GOOD
?
345 USB_STOR_TRANSPORT_GOOD
: USB_STOR_TRANSPORT_ERROR
);
349 * Write block data via the data register
351 static int usbat_write_block(struct us_data
*us
,
352 unsigned char access
,
353 unsigned char *content
,
359 unsigned char *command
= us
->iobuf
;
362 return USB_STOR_TRANSPORT_GOOD
;
365 command
[1] = access
| USBAT_CMD_WRITE_BLOCK
;
366 command
[2] = USBAT_ATA_DATA
;
370 command
[6] = LSB_of(len
);
371 command
[7] = MSB_of(len
);
373 result
= usbat_execute_command(us
, command
, 8);
375 if (result
!= USB_STOR_XFER_GOOD
)
376 return USB_STOR_TRANSPORT_ERROR
;
378 result
= usbat_bulk_write(us
, content
, len
, use_sg
);
379 if (result
!= USB_STOR_XFER_GOOD
)
380 return USB_STOR_TRANSPORT_ERROR
;
382 return usbat_wait_not_busy(us
, minutes
);
386 * Process read and write requests
388 static int usbat_hp8200e_rw_block_test(struct us_data
*us
,
389 unsigned char access
,
390 unsigned char *registers
,
391 unsigned char *data_out
,
392 unsigned short num_registers
,
393 unsigned char data_reg
,
394 unsigned char status_reg
,
395 unsigned char timeout
,
396 unsigned char qualifier
,
398 unsigned char *content
,
404 unsigned int pipe
= (direction
== DMA_FROM_DEVICE
) ?
405 us
->recv_bulk_pipe
: us
->send_bulk_pipe
;
407 unsigned char *command
= us
->iobuf
;
410 unsigned char *data
= us
->iobuf
;
411 unsigned char *status
= us
->iobuf
;
413 BUG_ON(num_registers
> US_IOBUF_SIZE
/2);
415 for (i
=0; i
<20; i
++) {
418 * The first time we send the full command, which consists
419 * of downloading the SCSI command followed by downloading
420 * the data via a write-and-test. Any other time we only
421 * send the command to download the data -- the SCSI command
422 * is still 'active' in some sense in the device.
424 * We're only going to try sending the data 10 times. After
425 * that, we just return a failure.
431 * Write to multiple registers
432 * Not really sure the 0x07, 0x17, 0xfc, 0xe7 is
433 * necessary here, but that's what came out of the
434 * trace every single time.
437 command
[1] = access
| USBAT_CMD_WRITE_REGS
;
442 command
[6] = LSB_of(num_registers
*2);
443 command
[7] = MSB_of(num_registers
*2);
447 /* Conditionally read or write blocks */
448 command
[cmdlen
-8] = (direction
==DMA_TO_DEVICE
? 0x40 : 0xC0);
449 command
[cmdlen
-7] = access
|
450 (direction
==DMA_TO_DEVICE
?
451 USBAT_CMD_COND_WRITE_BLOCK
: USBAT_CMD_COND_READ_BLOCK
);
452 command
[cmdlen
-6] = data_reg
;
453 command
[cmdlen
-5] = status_reg
;
454 command
[cmdlen
-4] = timeout
;
455 command
[cmdlen
-3] = qualifier
;
456 command
[cmdlen
-2] = LSB_of(len
);
457 command
[cmdlen
-1] = MSB_of(len
);
459 result
= usbat_execute_command(us
, command
, cmdlen
);
461 if (result
!= USB_STOR_XFER_GOOD
)
462 return USB_STOR_TRANSPORT_ERROR
;
466 for (j
=0; j
<num_registers
; j
++) {
467 data
[j
<<1] = registers
[j
];
468 data
[1+(j
<<1)] = data_out
[j
];
471 result
= usbat_bulk_write(us
, data
, num_registers
*2, 0);
472 if (result
!= USB_STOR_XFER_GOOD
)
473 return USB_STOR_TRANSPORT_ERROR
;
477 result
= usb_stor_bulk_transfer_sg(us
,
478 pipe
, content
, len
, use_sg
, NULL
);
481 * If we get a stall on the bulk download, we'll retry
482 * the bulk download -- but not the SCSI command because
483 * in some sense the SCSI command is still 'active' and
484 * waiting for the data. Don't ask me why this should be;
485 * I'm only following what the Windoze driver did.
487 * Note that a stall for the test-and-read/write command means
488 * that the test failed. In this case we're testing to make
489 * sure that the device is error-free
490 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
491 * hypothesis is that the USBAT chip somehow knows what
492 * the device will accept, but doesn't give the device any
493 * data until all data is received. Thus, the device would
494 * still be waiting for the first byte of data if a stall
495 * occurs, even if the stall implies that some data was
499 if (result
== USB_STOR_XFER_SHORT
||
500 result
== USB_STOR_XFER_STALLED
) {
503 * If we're reading and we stalled, then clear
504 * the bulk output pipe only the first time.
507 if (direction
==DMA_FROM_DEVICE
&& i
==0) {
508 if (usb_stor_clear_halt(us
,
509 us
->send_bulk_pipe
) < 0)
510 return USB_STOR_TRANSPORT_ERROR
;
514 * Read status: is the device angry, or just busy?
517 result
= usbat_read(us
, USBAT_ATA
,
518 direction
==DMA_TO_DEVICE
?
519 USBAT_ATA_STATUS
: USBAT_ATA_ALTSTATUS
,
522 if (result
!=USB_STOR_XFER_GOOD
)
523 return USB_STOR_TRANSPORT_ERROR
;
524 if (*status
& 0x01) /* check condition */
525 return USB_STOR_TRANSPORT_FAILED
;
526 if (*status
& 0x20) /* device fault */
527 return USB_STOR_TRANSPORT_FAILED
;
529 US_DEBUGP("Redoing %s\n",
530 direction
==DMA_TO_DEVICE
? "write" : "read");
532 } else if (result
!= USB_STOR_XFER_GOOD
)
533 return USB_STOR_TRANSPORT_ERROR
;
535 return usbat_wait_not_busy(us
, minutes
);
539 US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
540 direction
==DMA_TO_DEVICE
? "Writing" : "Reading");
542 return USB_STOR_TRANSPORT_FAILED
;
546 * Write to multiple registers:
547 * Allows us to write specific data to any registers. The data to be written
548 * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN
549 * which gets sent through bulk out.
550 * Not designed for large transfers of data!
552 static int usbat_multiple_write(struct us_data
*us
,
553 unsigned char *registers
,
554 unsigned char *data_out
,
555 unsigned short num_registers
)
558 unsigned char *data
= us
->iobuf
;
559 unsigned char *command
= us
->iobuf
;
561 BUG_ON(num_registers
> US_IOBUF_SIZE
/2);
563 /* Write to multiple registers, ATA access */
565 command
[1] = USBAT_ATA
| USBAT_CMD_WRITE_REGS
;
573 /* Number of bytes to be transferred (incl. addresses and data) */
574 command
[6] = LSB_of(num_registers
*2);
575 command
[7] = MSB_of(num_registers
*2);
577 /* The setup command */
578 result
= usbat_execute_command(us
, command
, 8);
579 if (result
!= USB_STOR_XFER_GOOD
)
580 return USB_STOR_TRANSPORT_ERROR
;
582 /* Create the reg/data, reg/data sequence */
583 for (i
=0; i
<num_registers
; i
++) {
584 data
[i
<<1] = registers
[i
];
585 data
[1+(i
<<1)] = data_out
[i
];
589 result
= usbat_bulk_write(us
, data
, num_registers
*2, 0);
590 if (result
!= USB_STOR_XFER_GOOD
)
591 return USB_STOR_TRANSPORT_ERROR
;
593 if (usbat_get_device_type(us
) == USBAT_DEV_HP8200
)
594 return usbat_wait_not_busy(us
, 0);
596 return USB_STOR_TRANSPORT_GOOD
;
600 * Conditionally read blocks from device:
601 * Allows us to read blocks from a specific data register, based upon the
602 * condition that a status register can be successfully masked with a status
603 * qualifier. If this condition is not initially met, the read will wait
604 * up until a maximum amount of time has elapsed, as specified by timeout.
605 * The read will start when the condition is met, otherwise the command aborts.
607 * The qualifier defined here is not the value that is masked, it defines
608 * conditions for the write to take place. The actual masked qualifier (and
609 * other related details) are defined beforehand with _set_shuttle_features().
611 static int usbat_read_blocks(struct us_data
*us
,
612 unsigned char *buffer
,
617 unsigned char *command
= us
->iobuf
;
620 command
[1] = USBAT_ATA
| USBAT_CMD_COND_READ_BLOCK
;
621 command
[2] = USBAT_ATA_DATA
;
622 command
[3] = USBAT_ATA_STATUS
;
623 command
[4] = 0xFD; /* Timeout (ms); */
624 command
[5] = USBAT_QUAL_FCQ
;
625 command
[6] = LSB_of(len
);
626 command
[7] = MSB_of(len
);
628 /* Multiple block read setup command */
629 result
= usbat_execute_command(us
, command
, 8);
630 if (result
!= USB_STOR_XFER_GOOD
)
631 return USB_STOR_TRANSPORT_FAILED
;
633 /* Read the blocks we just asked for */
634 result
= usbat_bulk_read(us
, buffer
, len
, use_sg
);
635 if (result
!= USB_STOR_XFER_GOOD
)
636 return USB_STOR_TRANSPORT_FAILED
;
638 return USB_STOR_TRANSPORT_GOOD
;
642 * Conditionally write blocks to device:
643 * Allows us to write blocks to a specific data register, based upon the
644 * condition that a status register can be successfully masked with a status
645 * qualifier. If this condition is not initially met, the write will wait
646 * up until a maximum amount of time has elapsed, as specified by timeout.
647 * The read will start when the condition is met, otherwise the command aborts.
649 * The qualifier defined here is not the value that is masked, it defines
650 * conditions for the write to take place. The actual masked qualifier (and
651 * other related details) are defined beforehand with _set_shuttle_features().
653 static int usbat_write_blocks(struct us_data
*us
,
654 unsigned char *buffer
,
659 unsigned char *command
= us
->iobuf
;
662 command
[1] = USBAT_ATA
| USBAT_CMD_COND_WRITE_BLOCK
;
663 command
[2] = USBAT_ATA_DATA
;
664 command
[3] = USBAT_ATA_STATUS
;
665 command
[4] = 0xFD; /* Timeout (ms) */
666 command
[5] = USBAT_QUAL_FCQ
;
667 command
[6] = LSB_of(len
);
668 command
[7] = MSB_of(len
);
670 /* Multiple block write setup command */
671 result
= usbat_execute_command(us
, command
, 8);
672 if (result
!= USB_STOR_XFER_GOOD
)
673 return USB_STOR_TRANSPORT_FAILED
;
676 result
= usbat_bulk_write(us
, buffer
, len
, use_sg
);
677 if (result
!= USB_STOR_XFER_GOOD
)
678 return USB_STOR_TRANSPORT_FAILED
;
680 return USB_STOR_TRANSPORT_GOOD
;
684 * Read the User IO register
686 static int usbat_read_user_io(struct us_data
*us
, unsigned char *data_flags
)
690 result
= usb_stor_ctrl_transfer(us
,
699 US_DEBUGP("usbat_read_user_io: UIO register reads %02X\n", (unsigned short) (*data_flags
));
705 * Write to the User IO register
707 static int usbat_write_user_io(struct us_data
*us
,
708 unsigned char enable_flags
,
709 unsigned char data_flags
)
711 return usb_stor_ctrl_transfer(us
,
715 short_pack(enable_flags
, data_flags
),
723 * Often needed on media change.
725 static int usbat_device_reset(struct us_data
*us
)
730 * Reset peripheral, enable peripheral control signals
731 * (bring reset signal up)
733 rc
= usbat_write_user_io(us
,
734 USBAT_UIO_DRVRST
| USBAT_UIO_OE1
| USBAT_UIO_OE0
,
735 USBAT_UIO_EPAD
| USBAT_UIO_1
);
736 if (rc
!= USB_STOR_XFER_GOOD
)
737 return USB_STOR_TRANSPORT_ERROR
;
740 * Enable peripheral control signals
741 * (bring reset signal down)
743 rc
= usbat_write_user_io(us
,
744 USBAT_UIO_OE1
| USBAT_UIO_OE0
,
745 USBAT_UIO_EPAD
| USBAT_UIO_1
);
746 if (rc
!= USB_STOR_XFER_GOOD
)
747 return USB_STOR_TRANSPORT_ERROR
;
749 return USB_STOR_TRANSPORT_GOOD
;
755 static int usbat_device_enable_cdt(struct us_data
*us
)
759 /* Enable peripheral control signals and card detect */
760 rc
= usbat_write_user_io(us
,
761 USBAT_UIO_ACKD
| USBAT_UIO_OE1
| USBAT_UIO_OE0
,
762 USBAT_UIO_EPAD
| USBAT_UIO_1
);
763 if (rc
!= USB_STOR_XFER_GOOD
)
764 return USB_STOR_TRANSPORT_ERROR
;
766 return USB_STOR_TRANSPORT_GOOD
;
770 * Determine if media is present.
772 static int usbat_flash_check_media_present(unsigned char *uio
)
774 if (*uio
& USBAT_UIO_UI0
) {
775 US_DEBUGP("usbat_flash_check_media_present: no media detected\n");
776 return USBAT_FLASH_MEDIA_NONE
;
779 return USBAT_FLASH_MEDIA_CF
;
783 * Determine if media has changed since last operation
785 static int usbat_flash_check_media_changed(unsigned char *uio
)
787 if (*uio
& USBAT_UIO_0
) {
788 US_DEBUGP("usbat_flash_check_media_changed: media change detected\n");
789 return USBAT_FLASH_MEDIA_CHANGED
;
792 return USBAT_FLASH_MEDIA_SAME
;
796 * Check for media change / no media and handle the situation appropriately
798 static int usbat_flash_check_media(struct us_data
*us
,
799 struct usbat_info
*info
)
802 unsigned char *uio
= us
->iobuf
;
804 rc
= usbat_read_user_io(us
, uio
);
805 if (rc
!= USB_STOR_XFER_GOOD
)
806 return USB_STOR_TRANSPORT_ERROR
;
808 /* Check for media existence */
809 rc
= usbat_flash_check_media_present(uio
);
810 if (rc
== USBAT_FLASH_MEDIA_NONE
) {
811 info
->sense_key
= 0x02;
812 info
->sense_asc
= 0x3A;
813 info
->sense_ascq
= 0x00;
814 return USB_STOR_TRANSPORT_FAILED
;
817 /* Check for media change */
818 rc
= usbat_flash_check_media_changed(uio
);
819 if (rc
== USBAT_FLASH_MEDIA_CHANGED
) {
821 /* Reset and re-enable card detect */
822 rc
= usbat_device_reset(us
);
823 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
825 rc
= usbat_device_enable_cdt(us
);
826 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
831 rc
= usbat_read_user_io(us
, uio
);
832 if (rc
!= USB_STOR_XFER_GOOD
)
833 return USB_STOR_TRANSPORT_ERROR
;
835 info
->sense_key
= UNIT_ATTENTION
;
836 info
->sense_asc
= 0x28;
837 info
->sense_ascq
= 0x00;
838 return USB_STOR_TRANSPORT_FAILED
;
841 return USB_STOR_TRANSPORT_GOOD
;
845 * Determine whether we are controlling a flash-based reader/writer,
846 * or a HP8200-based CD drive.
847 * Sets transport functions as appropriate.
849 static int usbat_identify_device(struct us_data
*us
,
850 struct usbat_info
*info
)
853 unsigned char status
;
856 return USB_STOR_TRANSPORT_ERROR
;
858 rc
= usbat_device_reset(us
);
859 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
864 * In attempt to distinguish between HP CDRW's and Flash readers, we now
865 * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
866 * readers), this command should fail with error. On ATAPI devices (i.e.
867 * CDROM drives), it should succeed.
869 rc
= usbat_write(us
, USBAT_ATA
, USBAT_ATA_CMD
, 0xA1);
870 if (rc
!= USB_STOR_XFER_GOOD
)
871 return USB_STOR_TRANSPORT_ERROR
;
873 rc
= usbat_get_status(us
, &status
);
874 if (rc
!= USB_STOR_XFER_GOOD
)
875 return USB_STOR_TRANSPORT_ERROR
;
877 /* Check for error bit, or if the command 'fell through' */
878 if (status
== 0xA1 || !(status
& 0x01)) {
879 /* Device is HP 8200 */
880 US_DEBUGP("usbat_identify_device: Detected HP8200 CDRW\n");
881 info
->devicetype
= USBAT_DEV_HP8200
;
883 /* Device is a CompactFlash reader/writer */
884 US_DEBUGP("usbat_identify_device: Detected Flash reader/writer\n");
885 info
->devicetype
= USBAT_DEV_FLASH
;
888 return USB_STOR_TRANSPORT_GOOD
;
892 * Set the transport function based on the device type
894 static int usbat_set_transport(struct us_data
*us
,
895 struct usbat_info
*info
,
899 if (!info
->devicetype
)
900 info
->devicetype
= devicetype
;
902 if (!info
->devicetype
)
903 usbat_identify_device(us
, info
);
905 switch (info
->devicetype
) {
907 return USB_STOR_TRANSPORT_ERROR
;
909 case USBAT_DEV_HP8200
:
910 us
->transport
= usbat_hp8200e_transport
;
913 case USBAT_DEV_FLASH
:
914 us
->transport
= usbat_flash_transport
;
922 * Read the media capacity
924 static int usbat_flash_get_sector_count(struct us_data
*us
,
925 struct usbat_info
*info
)
927 unsigned char registers
[3] = {
932 unsigned char command
[3] = { 0x01, 0xA0, 0xEC };
933 unsigned char *reply
;
934 unsigned char status
;
938 return USB_STOR_TRANSPORT_ERROR
;
940 reply
= kmalloc(512, GFP_NOIO
);
942 return USB_STOR_TRANSPORT_ERROR
;
944 /* ATA command : IDENTIFY DEVICE */
945 rc
= usbat_multiple_write(us
, registers
, command
, 3);
946 if (rc
!= USB_STOR_XFER_GOOD
) {
947 US_DEBUGP("usbat_flash_get_sector_count: Gah! identify_device failed\n");
948 rc
= USB_STOR_TRANSPORT_ERROR
;
952 /* Read device status */
953 if (usbat_get_status(us
, &status
) != USB_STOR_XFER_GOOD
) {
954 rc
= USB_STOR_TRANSPORT_ERROR
;
960 /* Read the device identification data */
961 rc
= usbat_read_block(us
, reply
, 512, 0);
962 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
965 info
->sectors
= ((u32
)(reply
[117]) << 24) |
966 ((u32
)(reply
[116]) << 16) |
967 ((u32
)(reply
[115]) << 8) |
968 ((u32
)(reply
[114]) );
970 rc
= USB_STOR_TRANSPORT_GOOD
;
978 * Read data from device
980 static int usbat_flash_read_data(struct us_data
*us
,
981 struct usbat_info
*info
,
985 unsigned char registers
[7] = {
994 unsigned char command
[7];
995 unsigned char *buffer
;
996 unsigned char thistime
;
997 unsigned int totallen
, alloclen
;
999 unsigned int sg_idx
= 0, sg_offset
= 0;
1001 result
= usbat_flash_check_media(us
, info
);
1002 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1006 * we're working in LBA mode. according to the ATA spec,
1007 * we can support up to 28-bit addressing. I don't know if Jumpshot
1008 * supports beyond 24-bit addressing. It's kind of hard to test
1009 * since it requires > 8GB CF card.
1012 if (sector
> 0x0FFFFFFF)
1013 return USB_STOR_TRANSPORT_ERROR
;
1015 totallen
= sectors
* info
->ssize
;
1018 * Since we don't read more than 64 KB at a time, we have to create
1019 * a bounce buffer and move the data a piece at a time between the
1020 * bounce buffer and the actual transfer buffer.
1023 alloclen
= min(totallen
, 65536u);
1024 buffer
= kmalloc(alloclen
, GFP_NOIO
);
1026 return USB_STOR_TRANSPORT_ERROR
;
1030 * loop, never allocate or transfer more than 64k at once
1031 * (min(128k, 255*info->ssize) is the real limit)
1033 len
= min(totallen
, alloclen
);
1034 thistime
= (len
/ info
->ssize
) & 0xff;
1036 /* ATA command 0x20 (READ SECTORS) */
1037 usbat_pack_ata_sector_cmd(command
, thistime
, sector
, 0x20);
1039 /* Write/execute ATA read command */
1040 result
= usbat_multiple_write(us
, registers
, command
, 7);
1041 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1044 /* Read the data we just requested */
1045 result
= usbat_read_blocks(us
, buffer
, len
, 0);
1046 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1049 US_DEBUGP("usbat_flash_read_data: %d bytes\n", len
);
1051 /* Store the data in the transfer buffer */
1052 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
1053 &sg_idx
, &sg_offset
, TO_XFER_BUF
);
1057 } while (totallen
> 0);
1060 return USB_STOR_TRANSPORT_GOOD
;
1064 return USB_STOR_TRANSPORT_ERROR
;
1068 * Write data to device
1070 static int usbat_flash_write_data(struct us_data
*us
,
1071 struct usbat_info
*info
,
1075 unsigned char registers
[7] = {
1084 unsigned char command
[7];
1085 unsigned char *buffer
;
1086 unsigned char thistime
;
1087 unsigned int totallen
, alloclen
;
1089 unsigned int sg_idx
= 0, sg_offset
= 0;
1091 result
= usbat_flash_check_media(us
, info
);
1092 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1096 * we're working in LBA mode. according to the ATA spec,
1097 * we can support up to 28-bit addressing. I don't know if the device
1098 * supports beyond 24-bit addressing. It's kind of hard to test
1099 * since it requires > 8GB media.
1102 if (sector
> 0x0FFFFFFF)
1103 return USB_STOR_TRANSPORT_ERROR
;
1105 totallen
= sectors
* info
->ssize
;
1108 * Since we don't write more than 64 KB at a time, we have to create
1109 * a bounce buffer and move the data a piece at a time between the
1110 * bounce buffer and the actual transfer buffer.
1113 alloclen
= min(totallen
, 65536u);
1114 buffer
= kmalloc(alloclen
, GFP_NOIO
);
1116 return USB_STOR_TRANSPORT_ERROR
;
1120 * loop, never allocate or transfer more than 64k at once
1121 * (min(128k, 255*info->ssize) is the real limit)
1123 len
= min(totallen
, alloclen
);
1124 thistime
= (len
/ info
->ssize
) & 0xff;
1126 /* Get the data from the transfer buffer */
1127 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
1128 &sg_idx
, &sg_offset
, FROM_XFER_BUF
);
1130 /* ATA command 0x30 (WRITE SECTORS) */
1131 usbat_pack_ata_sector_cmd(command
, thistime
, sector
, 0x30);
1133 /* Write/execute ATA write command */
1134 result
= usbat_multiple_write(us
, registers
, command
, 7);
1135 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1138 /* Write the data */
1139 result
= usbat_write_blocks(us
, buffer
, len
, 0);
1140 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1145 } while (totallen
> 0);
1152 return USB_STOR_TRANSPORT_ERROR
;
1156 * Squeeze a potentially huge (> 65535 byte) read10 command into
1157 * a little ( <= 65535 byte) ATAPI pipe
1159 static int usbat_hp8200e_handle_read10(struct us_data
*us
,
1160 unsigned char *registers
,
1161 unsigned char *data
,
1162 struct scsi_cmnd
*srb
)
1164 int result
= USB_STOR_TRANSPORT_GOOD
;
1165 unsigned char *buffer
;
1167 unsigned int sector
;
1168 unsigned int sg_segment
= 0;
1169 unsigned int sg_offset
= 0;
1171 US_DEBUGP("handle_read10: transfersize %d\n",
1174 if (srb
->request_bufflen
< 0x10000) {
1176 result
= usbat_hp8200e_rw_block_test(us
, USBAT_ATA
,
1177 registers
, data
, 19,
1178 USBAT_ATA_DATA
, USBAT_ATA_STATUS
, 0xFD,
1179 (USBAT_QUAL_FCQ
| USBAT_QUAL_ALQ
),
1181 srb
->request_buffer
,
1182 srb
->request_bufflen
, srb
->use_sg
, 1);
1188 * Since we're requesting more data than we can handle in
1189 * a single read command (max is 64k-1), we will perform
1190 * multiple reads, but each read must be in multiples of
1191 * a sector. Luckily the sector size is in srb->transfersize
1192 * (see linux/drivers/scsi/sr.c).
1195 if (data
[7+0] == GPCMD_READ_CD
) {
1196 len
= short_pack(data
[7+9], data
[7+8]);
1199 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len
);
1200 srb
->transfersize
= srb
->request_bufflen
/len
;
1203 if (!srb
->transfersize
) {
1204 srb
->transfersize
= 2048; /* A guess */
1205 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
1210 * Since we only read in one block at a time, we have to create
1211 * a bounce buffer and move the data a piece at a time between the
1212 * bounce buffer and the actual transfer buffer.
1215 len
= (65535/srb
->transfersize
) * srb
->transfersize
;
1216 US_DEBUGP("Max read is %d bytes\n", len
);
1217 len
= min(len
, srb
->request_bufflen
);
1218 buffer
= kmalloc(len
, GFP_NOIO
);
1219 if (buffer
== NULL
) /* bloody hell! */
1220 return USB_STOR_TRANSPORT_FAILED
;
1221 sector
= short_pack(data
[7+3], data
[7+2]);
1223 sector
|= short_pack(data
[7+5], data
[7+4]);
1226 sg_segment
= 0; /* for keeping track of where we are in */
1227 sg_offset
= 0; /* the scatter/gather list */
1229 while (transferred
!= srb
->request_bufflen
) {
1231 if (len
> srb
->request_bufflen
- transferred
)
1232 len
= srb
->request_bufflen
- transferred
;
1234 data
[3] = len
&0xFF; /* (cylL) = expected length (L) */
1235 data
[4] = (len
>>8)&0xFF; /* (cylH) = expected length (H) */
1237 /* Fix up the SCSI command sector and num sectors */
1239 data
[7+2] = MSB_of(sector
>>16); /* SCSI command sector */
1240 data
[7+3] = LSB_of(sector
>>16);
1241 data
[7+4] = MSB_of(sector
&0xFFFF);
1242 data
[7+5] = LSB_of(sector
&0xFFFF);
1243 if (data
[7+0] == GPCMD_READ_CD
)
1245 data
[7+7] = MSB_of(len
/ srb
->transfersize
); /* SCSI command */
1246 data
[7+8] = LSB_of(len
/ srb
->transfersize
); /* num sectors */
1248 result
= usbat_hp8200e_rw_block_test(us
, USBAT_ATA
,
1249 registers
, data
, 19,
1250 USBAT_ATA_DATA
, USBAT_ATA_STATUS
, 0xFD,
1251 (USBAT_QUAL_FCQ
| USBAT_QUAL_ALQ
),
1256 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1259 /* Store the data in the transfer buffer */
1260 usb_stor_access_xfer_buf(buffer
, len
, srb
,
1261 &sg_segment
, &sg_offset
, TO_XFER_BUF
);
1263 /* Update the amount transferred and the sector number */
1266 sector
+= len
/ srb
->transfersize
;
1268 } /* while transferred != srb->request_bufflen */
1274 static int usbat_select_and_test_registers(struct us_data
*us
)
1277 unsigned char *status
= us
->iobuf
;
1279 /* try device = master, then device = slave. */
1280 for (selector
= 0xA0; selector
<= 0xB0; selector
+= 0x10) {
1281 if (usbat_write(us
, USBAT_ATA
, USBAT_ATA_DEVICE
, selector
) !=
1283 return USB_STOR_TRANSPORT_ERROR
;
1285 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_STATUS
, status
) !=
1287 return USB_STOR_TRANSPORT_ERROR
;
1289 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_DEVICE
, status
) !=
1291 return USB_STOR_TRANSPORT_ERROR
;
1293 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, status
) !=
1295 return USB_STOR_TRANSPORT_ERROR
;
1297 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_HI
, status
) !=
1299 return USB_STOR_TRANSPORT_ERROR
;
1301 if (usbat_write(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, 0x55) !=
1303 return USB_STOR_TRANSPORT_ERROR
;
1305 if (usbat_write(us
, USBAT_ATA
, USBAT_ATA_LBA_HI
, 0xAA) !=
1307 return USB_STOR_TRANSPORT_ERROR
;
1309 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, status
) !=
1311 return USB_STOR_TRANSPORT_ERROR
;
1313 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, status
) !=
1315 return USB_STOR_TRANSPORT_ERROR
;
1318 return USB_STOR_TRANSPORT_GOOD
;
1322 * Initialize the USBAT processor and the storage device
1324 static int init_usbat(struct us_data
*us
, int devicetype
)
1327 struct usbat_info
*info
;
1328 unsigned char subcountH
= USBAT_ATA_LBA_HI
;
1329 unsigned char subcountL
= USBAT_ATA_LBA_ME
;
1330 unsigned char *status
= us
->iobuf
;
1332 us
->extra
= kzalloc(sizeof(struct usbat_info
), GFP_NOIO
);
1334 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1337 info
= (struct usbat_info
*) (us
->extra
);
1339 /* Enable peripheral control signals */
1340 rc
= usbat_write_user_io(us
,
1341 USBAT_UIO_OE1
| USBAT_UIO_OE0
,
1342 USBAT_UIO_EPAD
| USBAT_UIO_1
);
1343 if (rc
!= USB_STOR_XFER_GOOD
)
1344 return USB_STOR_TRANSPORT_ERROR
;
1346 US_DEBUGP("INIT 1\n");
1350 rc
= usbat_read_user_io(us
, status
);
1351 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1354 US_DEBUGP("INIT 2\n");
1356 rc
= usbat_read_user_io(us
, status
);
1357 if (rc
!= USB_STOR_XFER_GOOD
)
1358 return USB_STOR_TRANSPORT_ERROR
;
1360 rc
= usbat_read_user_io(us
, status
);
1361 if (rc
!= USB_STOR_XFER_GOOD
)
1362 return USB_STOR_TRANSPORT_ERROR
;
1364 US_DEBUGP("INIT 3\n");
1366 rc
= usbat_select_and_test_registers(us
);
1367 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1370 US_DEBUGP("INIT 4\n");
1372 rc
= usbat_read_user_io(us
, status
);
1373 if (rc
!= USB_STOR_XFER_GOOD
)
1374 return USB_STOR_TRANSPORT_ERROR
;
1376 US_DEBUGP("INIT 5\n");
1378 /* Enable peripheral control signals and card detect */
1379 rc
= usbat_device_enable_cdt(us
);
1380 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1383 US_DEBUGP("INIT 6\n");
1385 rc
= usbat_read_user_io(us
, status
);
1386 if (rc
!= USB_STOR_XFER_GOOD
)
1387 return USB_STOR_TRANSPORT_ERROR
;
1389 US_DEBUGP("INIT 7\n");
1393 rc
= usbat_read_user_io(us
, status
);
1394 if (rc
!= USB_STOR_XFER_GOOD
)
1395 return USB_STOR_TRANSPORT_ERROR
;
1397 US_DEBUGP("INIT 8\n");
1399 rc
= usbat_select_and_test_registers(us
);
1400 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1403 US_DEBUGP("INIT 9\n");
1405 /* At this point, we need to detect which device we are using */
1406 if (usbat_set_transport(us
, info
, devicetype
))
1407 return USB_STOR_TRANSPORT_ERROR
;
1409 US_DEBUGP("INIT 10\n");
1411 if (usbat_get_device_type(us
) == USBAT_DEV_FLASH
) {
1415 rc
= usbat_set_shuttle_features(us
, (USBAT_FEAT_ETEN
| USBAT_FEAT_ET2
| USBAT_FEAT_ET1
),
1416 0x00, 0x88, 0x08, subcountH
, subcountL
);
1417 if (rc
!= USB_STOR_XFER_GOOD
)
1418 return USB_STOR_TRANSPORT_ERROR
;
1420 US_DEBUGP("INIT 11\n");
1422 return USB_STOR_TRANSPORT_GOOD
;
1426 * Transport for the HP 8200e
1428 static int usbat_hp8200e_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
1431 unsigned char *status
= us
->iobuf
;
1432 unsigned char registers
[32];
1433 unsigned char data
[32];
1438 len
= srb
->request_bufflen
;
1440 /* Send A0 (ATA PACKET COMMAND).
1441 Note: I guess we're never going to get any of the ATA
1442 commands... just ATA Packet Commands.
1445 registers
[0] = USBAT_ATA_FEATURES
;
1446 registers
[1] = USBAT_ATA_SECCNT
;
1447 registers
[2] = USBAT_ATA_SECNUM
;
1448 registers
[3] = USBAT_ATA_LBA_ME
;
1449 registers
[4] = USBAT_ATA_LBA_HI
;
1450 registers
[5] = USBAT_ATA_DEVICE
;
1451 registers
[6] = USBAT_ATA_CMD
;
1455 data
[3] = len
&0xFF; /* (cylL) = expected length (L) */
1456 data
[4] = (len
>>8)&0xFF; /* (cylH) = expected length (H) */
1457 data
[5] = 0xB0; /* (device sel) = slave */
1458 data
[6] = 0xA0; /* (command) = ATA PACKET COMMAND */
1460 for (i
=7; i
<19; i
++) {
1461 registers
[i
] = 0x10;
1462 data
[i
] = (i
-7 >= srb
->cmd_len
) ? 0 : srb
->cmnd
[i
-7];
1465 result
= usbat_get_status(us
, status
);
1466 US_DEBUGP("Status = %02X\n", *status
);
1467 if (result
!= USB_STOR_XFER_GOOD
)
1468 return USB_STOR_TRANSPORT_ERROR
;
1469 if (srb
->cmnd
[0] == TEST_UNIT_READY
)
1472 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
1474 result
= usbat_hp8200e_rw_block_test(us
, USBAT_ATA
,
1475 registers
, data
, 19,
1476 USBAT_ATA_DATA
, USBAT_ATA_STATUS
, 0xFD,
1477 (USBAT_QUAL_FCQ
| USBAT_QUAL_ALQ
),
1479 srb
->request_buffer
,
1480 len
, srb
->use_sg
, 10);
1482 if (result
== USB_STOR_TRANSPORT_GOOD
) {
1484 US_DEBUGP("Wrote %08X bytes\n", transferred
);
1489 } else if (srb
->cmnd
[0] == READ_10
||
1490 srb
->cmnd
[0] == GPCMD_READ_CD
) {
1492 return usbat_hp8200e_handle_read10(us
, registers
, data
, srb
);
1497 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1499 return USB_STOR_TRANSPORT_ERROR
;
1502 if ( (result
= usbat_multiple_write(us
,
1503 registers
, data
, 7)) != USB_STOR_TRANSPORT_GOOD
) {
1508 * Write the 12-byte command header.
1510 * If the command is BLANK then set the timer for 75 minutes.
1511 * Otherwise set it for 10 minutes.
1513 * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1514 * AT SPEED 4 IS UNRELIABLE!!!
1517 if ((result
= usbat_write_block(us
,
1518 USBAT_ATA
, srb
->cmnd
, 12,
1519 (srb
->cmnd
[0]==GPCMD_BLANK
? 75 : 10), 0) !=
1520 USB_STOR_TRANSPORT_GOOD
)) {
1524 /* If there is response data to be read in then do it here. */
1526 if (len
!= 0 && (srb
->sc_data_direction
== DMA_FROM_DEVICE
)) {
1528 /* How many bytes to read in? Check cylL register */
1530 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, status
) !=
1531 USB_STOR_XFER_GOOD
) {
1532 return USB_STOR_TRANSPORT_ERROR
;
1535 if (len
> 0xFF) { /* need to read cylH also */
1537 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_HI
, status
) !=
1538 USB_STOR_XFER_GOOD
) {
1539 return USB_STOR_TRANSPORT_ERROR
;
1541 len
+= ((unsigned int) *status
)<<8;
1547 result
= usbat_read_block(us
, srb
->request_buffer
, len
, srb
->use_sg
);
1549 /* Debug-print the first 32 bytes of the transfer */
1553 for (i
=0; i
<len
&& i
<32; i
++) {
1554 sprintf(string
+strlen(string
), "%02X ",
1555 ((unsigned char *)srb
->request_buffer
)[i
]);
1557 US_DEBUGP("%s\n", string
);
1562 US_DEBUGP("%s\n", string
);
1570 * Transport for USBAT02-based CompactFlash and similar storage devices
1572 static int usbat_flash_transport(struct scsi_cmnd
* srb
, struct us_data
*us
)
1575 struct usbat_info
*info
= (struct usbat_info
*) (us
->extra
);
1576 unsigned long block
, blocks
;
1577 unsigned char *ptr
= us
->iobuf
;
1578 static unsigned char inquiry_response
[36] = {
1579 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1582 if (srb
->cmnd
[0] == INQUIRY
) {
1583 US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1584 memcpy(ptr
, inquiry_response
, sizeof(inquiry_response
));
1585 fill_inquiry_response(us
, ptr
, 36);
1586 return USB_STOR_TRANSPORT_GOOD
;
1589 if (srb
->cmnd
[0] == READ_CAPACITY
) {
1590 rc
= usbat_flash_check_media(us
, info
);
1591 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1594 rc
= usbat_flash_get_sector_count(us
, info
);
1595 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1598 /* hard coded 512 byte sectors as per ATA spec */
1599 info
->ssize
= 0x200;
1600 US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1601 info
->sectors
, info
->ssize
);
1605 * note: must return the sector number of the last sector,
1606 * *not* the total number of sectors
1608 ((__be32
*) ptr
)[0] = cpu_to_be32(info
->sectors
- 1);
1609 ((__be32
*) ptr
)[1] = cpu_to_be32(info
->ssize
);
1610 usb_stor_set_xfer_buf(ptr
, 8, srb
);
1612 return USB_STOR_TRANSPORT_GOOD
;
1615 if (srb
->cmnd
[0] == MODE_SELECT_10
) {
1616 US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
1617 return USB_STOR_TRANSPORT_ERROR
;
1620 if (srb
->cmnd
[0] == READ_10
) {
1621 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
1622 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
1624 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
1626 US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block
, blocks
);
1627 return usbat_flash_read_data(us
, info
, block
, blocks
);
1630 if (srb
->cmnd
[0] == READ_12
) {
1632 * I don't think we'll ever see a READ_12 but support it anyway
1634 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
1635 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
1637 blocks
= ((u32
)(srb
->cmnd
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
1638 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
1640 US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block
, blocks
);
1641 return usbat_flash_read_data(us
, info
, block
, blocks
);
1644 if (srb
->cmnd
[0] == WRITE_10
) {
1645 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
1646 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
1648 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
1650 US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block
, blocks
);
1651 return usbat_flash_write_data(us
, info
, block
, blocks
);
1654 if (srb
->cmnd
[0] == WRITE_12
) {
1656 * I don't think we'll ever see a WRITE_12 but support it anyway
1658 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
1659 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
1661 blocks
= ((u32
)(srb
->cmnd
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
1662 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
1664 US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block
, blocks
);
1665 return usbat_flash_write_data(us
, info
, block
, blocks
);
1669 if (srb
->cmnd
[0] == TEST_UNIT_READY
) {
1670 US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
1672 rc
= usbat_flash_check_media(us
, info
);
1673 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1676 return usbat_check_status(us
);
1679 if (srb
->cmnd
[0] == REQUEST_SENSE
) {
1680 US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
1684 ptr
[2] = info
->sense_key
;
1686 ptr
[12] = info
->sense_asc
;
1687 ptr
[13] = info
->sense_ascq
;
1688 usb_stor_set_xfer_buf(ptr
, 18, srb
);
1690 return USB_STOR_TRANSPORT_GOOD
;
1693 if (srb
->cmnd
[0] == ALLOW_MEDIUM_REMOVAL
) {
1695 * sure. whatever. not like we can stop the user from popping
1696 * the media out of the device (no locking doors, etc)
1698 return USB_STOR_TRANSPORT_GOOD
;
1701 US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1702 srb
->cmnd
[0], srb
->cmnd
[0]);
1703 info
->sense_key
= 0x05;
1704 info
->sense_asc
= 0x20;
1705 info
->sense_ascq
= 0x00;
1706 return USB_STOR_TRANSPORT_FAILED
;
1709 int init_usbat_cd(struct us_data
*us
)
1711 return init_usbat(us
, USBAT_DEV_HP8200
);
1715 int init_usbat_flash(struct us_data
*us
)
1717 return init_usbat(us
, USBAT_DEV_FLASH
);
1720 int init_usbat_probe(struct us_data
*us
)
1722 return init_usbat(us
, 0);
1726 * Default transport function. Attempts to detect which transport function
1727 * should be called, makes it the new default, and calls it.
1729 * This function should never be called. Our usbat_init() function detects the
1730 * device type and changes the us->transport ptr to the transport function
1731 * relevant to the device.
1732 * However, we'll support this impossible(?) case anyway.
1734 int usbat_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
1736 struct usbat_info
*info
= (struct usbat_info
*) (us
->extra
);
1738 if (usbat_set_transport(us
, info
, 0))
1739 return USB_STOR_TRANSPORT_ERROR
;
1741 return us
->transport(srb
, us
);