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_offset
= 0;
1000 struct scatterlist
*sg
= NULL
;
1002 result
= usbat_flash_check_media(us
, info
);
1003 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1007 * we're working in LBA mode. according to the ATA spec,
1008 * we can support up to 28-bit addressing. I don't know if Jumpshot
1009 * supports beyond 24-bit addressing. It's kind of hard to test
1010 * since it requires > 8GB CF card.
1013 if (sector
> 0x0FFFFFFF)
1014 return USB_STOR_TRANSPORT_ERROR
;
1016 totallen
= sectors
* info
->ssize
;
1019 * Since we don't read more than 64 KB at a time, we have to create
1020 * a bounce buffer and move the data a piece at a time between the
1021 * bounce buffer and the actual transfer buffer.
1024 alloclen
= min(totallen
, 65536u);
1025 buffer
= kmalloc(alloclen
, GFP_NOIO
);
1027 return USB_STOR_TRANSPORT_ERROR
;
1031 * loop, never allocate or transfer more than 64k at once
1032 * (min(128k, 255*info->ssize) is the real limit)
1034 len
= min(totallen
, alloclen
);
1035 thistime
= (len
/ info
->ssize
) & 0xff;
1037 /* ATA command 0x20 (READ SECTORS) */
1038 usbat_pack_ata_sector_cmd(command
, thistime
, sector
, 0x20);
1040 /* Write/execute ATA read command */
1041 result
= usbat_multiple_write(us
, registers
, command
, 7);
1042 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1045 /* Read the data we just requested */
1046 result
= usbat_read_blocks(us
, buffer
, len
, 0);
1047 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1050 US_DEBUGP("usbat_flash_read_data: %d bytes\n", len
);
1052 /* Store the data in the transfer buffer */
1053 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
1054 &sg
, &sg_offset
, TO_XFER_BUF
);
1058 } while (totallen
> 0);
1061 return USB_STOR_TRANSPORT_GOOD
;
1065 return USB_STOR_TRANSPORT_ERROR
;
1069 * Write data to device
1071 static int usbat_flash_write_data(struct us_data
*us
,
1072 struct usbat_info
*info
,
1076 unsigned char registers
[7] = {
1085 unsigned char command
[7];
1086 unsigned char *buffer
;
1087 unsigned char thistime
;
1088 unsigned int totallen
, alloclen
;
1090 unsigned int sg_offset
= 0;
1091 struct scatterlist
*sg
= NULL
;
1093 result
= usbat_flash_check_media(us
, info
);
1094 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1098 * we're working in LBA mode. according to the ATA spec,
1099 * we can support up to 28-bit addressing. I don't know if the device
1100 * supports beyond 24-bit addressing. It's kind of hard to test
1101 * since it requires > 8GB media.
1104 if (sector
> 0x0FFFFFFF)
1105 return USB_STOR_TRANSPORT_ERROR
;
1107 totallen
= sectors
* info
->ssize
;
1110 * Since we don't write more than 64 KB at a time, we have to create
1111 * a bounce buffer and move the data a piece at a time between the
1112 * bounce buffer and the actual transfer buffer.
1115 alloclen
= min(totallen
, 65536u);
1116 buffer
= kmalloc(alloclen
, GFP_NOIO
);
1118 return USB_STOR_TRANSPORT_ERROR
;
1122 * loop, never allocate or transfer more than 64k at once
1123 * (min(128k, 255*info->ssize) is the real limit)
1125 len
= min(totallen
, alloclen
);
1126 thistime
= (len
/ info
->ssize
) & 0xff;
1128 /* Get the data from the transfer buffer */
1129 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
1130 &sg
, &sg_offset
, FROM_XFER_BUF
);
1132 /* ATA command 0x30 (WRITE SECTORS) */
1133 usbat_pack_ata_sector_cmd(command
, thistime
, sector
, 0x30);
1135 /* Write/execute ATA write command */
1136 result
= usbat_multiple_write(us
, registers
, command
, 7);
1137 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1140 /* Write the data */
1141 result
= usbat_write_blocks(us
, buffer
, len
, 0);
1142 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1147 } while (totallen
> 0);
1154 return USB_STOR_TRANSPORT_ERROR
;
1158 * Squeeze a potentially huge (> 65535 byte) read10 command into
1159 * a little ( <= 65535 byte) ATAPI pipe
1161 static int usbat_hp8200e_handle_read10(struct us_data
*us
,
1162 unsigned char *registers
,
1163 unsigned char *data
,
1164 struct scsi_cmnd
*srb
)
1166 int result
= USB_STOR_TRANSPORT_GOOD
;
1167 unsigned char *buffer
;
1169 unsigned int sector
;
1170 unsigned int sg_offset
= 0;
1171 struct scatterlist
*sg
= NULL
;
1173 US_DEBUGP("handle_read10: transfersize %d\n",
1176 if (srb
->request_bufflen
< 0x10000) {
1178 result
= usbat_hp8200e_rw_block_test(us
, USBAT_ATA
,
1179 registers
, data
, 19,
1180 USBAT_ATA_DATA
, USBAT_ATA_STATUS
, 0xFD,
1181 (USBAT_QUAL_FCQ
| USBAT_QUAL_ALQ
),
1183 srb
->request_buffer
,
1184 srb
->request_bufflen
, srb
->use_sg
, 1);
1190 * Since we're requesting more data than we can handle in
1191 * a single read command (max is 64k-1), we will perform
1192 * multiple reads, but each read must be in multiples of
1193 * a sector. Luckily the sector size is in srb->transfersize
1194 * (see linux/drivers/scsi/sr.c).
1197 if (data
[7+0] == GPCMD_READ_CD
) {
1198 len
= short_pack(data
[7+9], data
[7+8]);
1201 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len
);
1202 srb
->transfersize
= srb
->request_bufflen
/len
;
1205 if (!srb
->transfersize
) {
1206 srb
->transfersize
= 2048; /* A guess */
1207 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
1212 * Since we only read in one block at a time, we have to create
1213 * a bounce buffer and move the data a piece at a time between the
1214 * bounce buffer and the actual transfer buffer.
1217 len
= (65535/srb
->transfersize
) * srb
->transfersize
;
1218 US_DEBUGP("Max read is %d bytes\n", len
);
1219 len
= min(len
, srb
->request_bufflen
);
1220 buffer
= kmalloc(len
, GFP_NOIO
);
1221 if (buffer
== NULL
) /* bloody hell! */
1222 return USB_STOR_TRANSPORT_FAILED
;
1223 sector
= short_pack(data
[7+3], data
[7+2]);
1225 sector
|= short_pack(data
[7+5], data
[7+4]);
1228 while (transferred
!= srb
->request_bufflen
) {
1230 if (len
> srb
->request_bufflen
- transferred
)
1231 len
= srb
->request_bufflen
- transferred
;
1233 data
[3] = len
&0xFF; /* (cylL) = expected length (L) */
1234 data
[4] = (len
>>8)&0xFF; /* (cylH) = expected length (H) */
1236 /* Fix up the SCSI command sector and num sectors */
1238 data
[7+2] = MSB_of(sector
>>16); /* SCSI command sector */
1239 data
[7+3] = LSB_of(sector
>>16);
1240 data
[7+4] = MSB_of(sector
&0xFFFF);
1241 data
[7+5] = LSB_of(sector
&0xFFFF);
1242 if (data
[7+0] == GPCMD_READ_CD
)
1244 data
[7+7] = MSB_of(len
/ srb
->transfersize
); /* SCSI command */
1245 data
[7+8] = LSB_of(len
/ srb
->transfersize
); /* num sectors */
1247 result
= usbat_hp8200e_rw_block_test(us
, USBAT_ATA
,
1248 registers
, data
, 19,
1249 USBAT_ATA_DATA
, USBAT_ATA_STATUS
, 0xFD,
1250 (USBAT_QUAL_FCQ
| USBAT_QUAL_ALQ
),
1255 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1258 /* Store the data in the transfer buffer */
1259 usb_stor_access_xfer_buf(buffer
, len
, srb
,
1260 &sg
, &sg_offset
, TO_XFER_BUF
);
1262 /* Update the amount transferred and the sector number */
1265 sector
+= len
/ srb
->transfersize
;
1267 } /* while transferred != srb->request_bufflen */
1273 static int usbat_select_and_test_registers(struct us_data
*us
)
1276 unsigned char *status
= us
->iobuf
;
1278 /* try device = master, then device = slave. */
1279 for (selector
= 0xA0; selector
<= 0xB0; selector
+= 0x10) {
1280 if (usbat_write(us
, USBAT_ATA
, USBAT_ATA_DEVICE
, selector
) !=
1282 return USB_STOR_TRANSPORT_ERROR
;
1284 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_STATUS
, status
) !=
1286 return USB_STOR_TRANSPORT_ERROR
;
1288 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_DEVICE
, status
) !=
1290 return USB_STOR_TRANSPORT_ERROR
;
1292 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, status
) !=
1294 return USB_STOR_TRANSPORT_ERROR
;
1296 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_HI
, status
) !=
1298 return USB_STOR_TRANSPORT_ERROR
;
1300 if (usbat_write(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, 0x55) !=
1302 return USB_STOR_TRANSPORT_ERROR
;
1304 if (usbat_write(us
, USBAT_ATA
, USBAT_ATA_LBA_HI
, 0xAA) !=
1306 return USB_STOR_TRANSPORT_ERROR
;
1308 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, status
) !=
1310 return USB_STOR_TRANSPORT_ERROR
;
1312 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, status
) !=
1314 return USB_STOR_TRANSPORT_ERROR
;
1317 return USB_STOR_TRANSPORT_GOOD
;
1321 * Initialize the USBAT processor and the storage device
1323 static int init_usbat(struct us_data
*us
, int devicetype
)
1326 struct usbat_info
*info
;
1327 unsigned char subcountH
= USBAT_ATA_LBA_HI
;
1328 unsigned char subcountL
= USBAT_ATA_LBA_ME
;
1329 unsigned char *status
= us
->iobuf
;
1331 us
->extra
= kzalloc(sizeof(struct usbat_info
), GFP_NOIO
);
1333 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1336 info
= (struct usbat_info
*) (us
->extra
);
1338 /* Enable peripheral control signals */
1339 rc
= usbat_write_user_io(us
,
1340 USBAT_UIO_OE1
| USBAT_UIO_OE0
,
1341 USBAT_UIO_EPAD
| USBAT_UIO_1
);
1342 if (rc
!= USB_STOR_XFER_GOOD
)
1343 return USB_STOR_TRANSPORT_ERROR
;
1345 US_DEBUGP("INIT 1\n");
1349 rc
= usbat_read_user_io(us
, status
);
1350 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1353 US_DEBUGP("INIT 2\n");
1355 rc
= usbat_read_user_io(us
, status
);
1356 if (rc
!= USB_STOR_XFER_GOOD
)
1357 return USB_STOR_TRANSPORT_ERROR
;
1359 rc
= usbat_read_user_io(us
, status
);
1360 if (rc
!= USB_STOR_XFER_GOOD
)
1361 return USB_STOR_TRANSPORT_ERROR
;
1363 US_DEBUGP("INIT 3\n");
1365 rc
= usbat_select_and_test_registers(us
);
1366 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1369 US_DEBUGP("INIT 4\n");
1371 rc
= usbat_read_user_io(us
, status
);
1372 if (rc
!= USB_STOR_XFER_GOOD
)
1373 return USB_STOR_TRANSPORT_ERROR
;
1375 US_DEBUGP("INIT 5\n");
1377 /* Enable peripheral control signals and card detect */
1378 rc
= usbat_device_enable_cdt(us
);
1379 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1382 US_DEBUGP("INIT 6\n");
1384 rc
= usbat_read_user_io(us
, status
);
1385 if (rc
!= USB_STOR_XFER_GOOD
)
1386 return USB_STOR_TRANSPORT_ERROR
;
1388 US_DEBUGP("INIT 7\n");
1392 rc
= usbat_read_user_io(us
, status
);
1393 if (rc
!= USB_STOR_XFER_GOOD
)
1394 return USB_STOR_TRANSPORT_ERROR
;
1396 US_DEBUGP("INIT 8\n");
1398 rc
= usbat_select_and_test_registers(us
);
1399 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1402 US_DEBUGP("INIT 9\n");
1404 /* At this point, we need to detect which device we are using */
1405 if (usbat_set_transport(us
, info
, devicetype
))
1406 return USB_STOR_TRANSPORT_ERROR
;
1408 US_DEBUGP("INIT 10\n");
1410 if (usbat_get_device_type(us
) == USBAT_DEV_FLASH
) {
1414 rc
= usbat_set_shuttle_features(us
, (USBAT_FEAT_ETEN
| USBAT_FEAT_ET2
| USBAT_FEAT_ET1
),
1415 0x00, 0x88, 0x08, subcountH
, subcountL
);
1416 if (rc
!= USB_STOR_XFER_GOOD
)
1417 return USB_STOR_TRANSPORT_ERROR
;
1419 US_DEBUGP("INIT 11\n");
1421 return USB_STOR_TRANSPORT_GOOD
;
1425 * Transport for the HP 8200e
1427 static int usbat_hp8200e_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
1430 unsigned char *status
= us
->iobuf
;
1431 unsigned char registers
[32];
1432 unsigned char data
[32];
1437 len
= srb
->request_bufflen
;
1439 /* Send A0 (ATA PACKET COMMAND).
1440 Note: I guess we're never going to get any of the ATA
1441 commands... just ATA Packet Commands.
1444 registers
[0] = USBAT_ATA_FEATURES
;
1445 registers
[1] = USBAT_ATA_SECCNT
;
1446 registers
[2] = USBAT_ATA_SECNUM
;
1447 registers
[3] = USBAT_ATA_LBA_ME
;
1448 registers
[4] = USBAT_ATA_LBA_HI
;
1449 registers
[5] = USBAT_ATA_DEVICE
;
1450 registers
[6] = USBAT_ATA_CMD
;
1454 data
[3] = len
&0xFF; /* (cylL) = expected length (L) */
1455 data
[4] = (len
>>8)&0xFF; /* (cylH) = expected length (H) */
1456 data
[5] = 0xB0; /* (device sel) = slave */
1457 data
[6] = 0xA0; /* (command) = ATA PACKET COMMAND */
1459 for (i
=7; i
<19; i
++) {
1460 registers
[i
] = 0x10;
1461 data
[i
] = (i
-7 >= srb
->cmd_len
) ? 0 : srb
->cmnd
[i
-7];
1464 result
= usbat_get_status(us
, status
);
1465 US_DEBUGP("Status = %02X\n", *status
);
1466 if (result
!= USB_STOR_XFER_GOOD
)
1467 return USB_STOR_TRANSPORT_ERROR
;
1468 if (srb
->cmnd
[0] == TEST_UNIT_READY
)
1471 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
1473 result
= usbat_hp8200e_rw_block_test(us
, USBAT_ATA
,
1474 registers
, data
, 19,
1475 USBAT_ATA_DATA
, USBAT_ATA_STATUS
, 0xFD,
1476 (USBAT_QUAL_FCQ
| USBAT_QUAL_ALQ
),
1478 srb
->request_buffer
,
1479 len
, srb
->use_sg
, 10);
1481 if (result
== USB_STOR_TRANSPORT_GOOD
) {
1483 US_DEBUGP("Wrote %08X bytes\n", transferred
);
1488 } else if (srb
->cmnd
[0] == READ_10
||
1489 srb
->cmnd
[0] == GPCMD_READ_CD
) {
1491 return usbat_hp8200e_handle_read10(us
, registers
, data
, srb
);
1496 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1498 return USB_STOR_TRANSPORT_ERROR
;
1501 if ( (result
= usbat_multiple_write(us
,
1502 registers
, data
, 7)) != USB_STOR_TRANSPORT_GOOD
) {
1507 * Write the 12-byte command header.
1509 * If the command is BLANK then set the timer for 75 minutes.
1510 * Otherwise set it for 10 minutes.
1512 * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1513 * AT SPEED 4 IS UNRELIABLE!!!
1516 if ((result
= usbat_write_block(us
,
1517 USBAT_ATA
, srb
->cmnd
, 12,
1518 (srb
->cmnd
[0]==GPCMD_BLANK
? 75 : 10), 0) !=
1519 USB_STOR_TRANSPORT_GOOD
)) {
1523 /* If there is response data to be read in then do it here. */
1525 if (len
!= 0 && (srb
->sc_data_direction
== DMA_FROM_DEVICE
)) {
1527 /* How many bytes to read in? Check cylL register */
1529 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, status
) !=
1530 USB_STOR_XFER_GOOD
) {
1531 return USB_STOR_TRANSPORT_ERROR
;
1534 if (len
> 0xFF) { /* need to read cylH also */
1536 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_HI
, status
) !=
1537 USB_STOR_XFER_GOOD
) {
1538 return USB_STOR_TRANSPORT_ERROR
;
1540 len
+= ((unsigned int) *status
)<<8;
1546 result
= usbat_read_block(us
, srb
->request_buffer
, len
, srb
->use_sg
);
1548 /* Debug-print the first 32 bytes of the transfer */
1552 for (i
=0; i
<len
&& i
<32; i
++) {
1553 sprintf(string
+strlen(string
), "%02X ",
1554 ((unsigned char *)srb
->request_buffer
)[i
]);
1556 US_DEBUGP("%s\n", string
);
1561 US_DEBUGP("%s\n", string
);
1569 * Transport for USBAT02-based CompactFlash and similar storage devices
1571 static int usbat_flash_transport(struct scsi_cmnd
* srb
, struct us_data
*us
)
1574 struct usbat_info
*info
= (struct usbat_info
*) (us
->extra
);
1575 unsigned long block
, blocks
;
1576 unsigned char *ptr
= us
->iobuf
;
1577 static unsigned char inquiry_response
[36] = {
1578 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1581 if (srb
->cmnd
[0] == INQUIRY
) {
1582 US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1583 memcpy(ptr
, inquiry_response
, sizeof(inquiry_response
));
1584 fill_inquiry_response(us
, ptr
, 36);
1585 return USB_STOR_TRANSPORT_GOOD
;
1588 if (srb
->cmnd
[0] == READ_CAPACITY
) {
1589 rc
= usbat_flash_check_media(us
, info
);
1590 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1593 rc
= usbat_flash_get_sector_count(us
, info
);
1594 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1597 /* hard coded 512 byte sectors as per ATA spec */
1598 info
->ssize
= 0x200;
1599 US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1600 info
->sectors
, info
->ssize
);
1604 * note: must return the sector number of the last sector,
1605 * *not* the total number of sectors
1607 ((__be32
*) ptr
)[0] = cpu_to_be32(info
->sectors
- 1);
1608 ((__be32
*) ptr
)[1] = cpu_to_be32(info
->ssize
);
1609 usb_stor_set_xfer_buf(ptr
, 8, srb
);
1611 return USB_STOR_TRANSPORT_GOOD
;
1614 if (srb
->cmnd
[0] == MODE_SELECT_10
) {
1615 US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
1616 return USB_STOR_TRANSPORT_ERROR
;
1619 if (srb
->cmnd
[0] == READ_10
) {
1620 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
1621 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
1623 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
1625 US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block
, blocks
);
1626 return usbat_flash_read_data(us
, info
, block
, blocks
);
1629 if (srb
->cmnd
[0] == READ_12
) {
1631 * I don't think we'll ever see a READ_12 but support it anyway
1633 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
1634 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
1636 blocks
= ((u32
)(srb
->cmnd
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
1637 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
1639 US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block
, blocks
);
1640 return usbat_flash_read_data(us
, info
, block
, blocks
);
1643 if (srb
->cmnd
[0] == WRITE_10
) {
1644 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
1645 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
1647 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
1649 US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block
, blocks
);
1650 return usbat_flash_write_data(us
, info
, block
, blocks
);
1653 if (srb
->cmnd
[0] == WRITE_12
) {
1655 * I don't think we'll ever see a WRITE_12 but support it anyway
1657 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
1658 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
1660 blocks
= ((u32
)(srb
->cmnd
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
1661 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
1663 US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block
, blocks
);
1664 return usbat_flash_write_data(us
, info
, block
, blocks
);
1668 if (srb
->cmnd
[0] == TEST_UNIT_READY
) {
1669 US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
1671 rc
= usbat_flash_check_media(us
, info
);
1672 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1675 return usbat_check_status(us
);
1678 if (srb
->cmnd
[0] == REQUEST_SENSE
) {
1679 US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
1683 ptr
[2] = info
->sense_key
;
1685 ptr
[12] = info
->sense_asc
;
1686 ptr
[13] = info
->sense_ascq
;
1687 usb_stor_set_xfer_buf(ptr
, 18, srb
);
1689 return USB_STOR_TRANSPORT_GOOD
;
1692 if (srb
->cmnd
[0] == ALLOW_MEDIUM_REMOVAL
) {
1694 * sure. whatever. not like we can stop the user from popping
1695 * the media out of the device (no locking doors, etc)
1697 return USB_STOR_TRANSPORT_GOOD
;
1700 US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1701 srb
->cmnd
[0], srb
->cmnd
[0]);
1702 info
->sense_key
= 0x05;
1703 info
->sense_asc
= 0x20;
1704 info
->sense_ascq
= 0x00;
1705 return USB_STOR_TRANSPORT_FAILED
;
1708 int init_usbat_cd(struct us_data
*us
)
1710 return init_usbat(us
, USBAT_DEV_HP8200
);
1714 int init_usbat_flash(struct us_data
*us
)
1716 return init_usbat(us
, USBAT_DEV_FLASH
);
1719 int init_usbat_probe(struct us_data
*us
)
1721 return init_usbat(us
, 0);
1725 * Default transport function. Attempts to detect which transport function
1726 * should be called, makes it the new default, and calls it.
1728 * This function should never be called. Our usbat_init() function detects the
1729 * device type and changes the us->transport ptr to the transport function
1730 * relevant to the device.
1731 * However, we'll support this impossible(?) case anyway.
1733 int usbat_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
1735 struct usbat_info
*info
= (struct usbat_info
*) (us
->extra
);
1737 if (usbat_set_transport(us
, info
, 0))
1738 return USB_STOR_TRANSPORT_ERROR
;
1740 return us
->transport(srb
, us
);