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
;
193 rc
= usbat_get_status(us
, reply
);
194 if (rc
!= USB_STOR_XFER_GOOD
)
195 return USB_STOR_TRANSPORT_FAILED
;
197 /* error/check condition (0x51 is ok) */
198 if (*reply
& 0x01 && *reply
!= 0x51)
199 return USB_STOR_TRANSPORT_FAILED
;
203 return USB_STOR_TRANSPORT_FAILED
;
205 return USB_STOR_TRANSPORT_GOOD
;
209 * Stores critical information in internal registers in prepartion for the execution
210 * of a conditional usbat_read_blocks or usbat_write_blocks call.
212 static int usbat_set_shuttle_features(struct us_data
*us
,
213 unsigned char external_trigger
,
214 unsigned char epp_control
,
215 unsigned char mask_byte
,
216 unsigned char test_pattern
,
217 unsigned char subcountH
,
218 unsigned char subcountL
)
220 unsigned char *command
= us
->iobuf
;
223 command
[1] = USBAT_CMD_SET_FEAT
;
226 * The only bit relevant to ATA access is bit 6
227 * which defines 8 bit data access (set) or 16 bit (unset)
229 command
[2] = epp_control
;
232 * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
233 * ET1 and ET2 define an external event to be checked for on event of a
234 * _read_blocks or _write_blocks operation. The read/write will not take
235 * place unless the defined trigger signal is active.
237 command
[3] = external_trigger
;
240 * The resultant byte of the mask operation (see mask_byte) is compared for
241 * equivalence with this test pattern. If equal, the read/write will take
244 command
[4] = test_pattern
;
247 * This value is logically ANDed with the status register field specified
248 * in the read/write command.
250 command
[5] = mask_byte
;
253 * If ALQ is set in the qualifier, this field contains the address of the
254 * registers where the byte count should be read for transferring the data.
255 * If ALQ is not set, then this field contains the number of bytes to be
258 command
[6] = subcountL
;
259 command
[7] = subcountH
;
261 return usbat_execute_command(us
, command
, 8);
265 * Block, waiting for an ATA device to become not busy or to report
266 * an error condition.
268 static int usbat_wait_not_busy(struct us_data
*us
, int minutes
)
272 unsigned char *status
= us
->iobuf
;
274 /* Synchronizing cache on a CDR could take a heck of a long time,
275 * but probably not more than 10 minutes or so. On the other hand,
276 * doing a full blank on a CDRW at speed 1 will take about 75
280 for (i
=0; i
<1200+minutes
*60; i
++) {
282 result
= usbat_get_status(us
, status
);
284 if (result
!=USB_STOR_XFER_GOOD
)
285 return USB_STOR_TRANSPORT_ERROR
;
286 if (*status
& 0x01) { /* check condition */
287 result
= usbat_read(us
, USBAT_ATA
, 0x10, status
);
288 return USB_STOR_TRANSPORT_FAILED
;
290 if (*status
& 0x20) /* device fault */
291 return USB_STOR_TRANSPORT_FAILED
;
293 if ((*status
& 0x80)==0x00) { /* not busy */
294 US_DEBUGP("Waited not busy for %d steps\n", i
);
295 return USB_STOR_TRANSPORT_GOOD
;
299 msleep(10); /* 5 seconds */
301 msleep(50); /* 10 seconds */
303 msleep(100); /* 50 seconds */
305 msleep(1000); /* X minutes */
308 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
310 return USB_STOR_TRANSPORT_FAILED
;
314 * Read block data from the data register
316 static int usbat_read_block(struct us_data
*us
,
317 unsigned char *content
,
322 unsigned char *command
= us
->iobuf
;
325 return USB_STOR_TRANSPORT_GOOD
;
328 command
[1] = USBAT_ATA
| USBAT_CMD_READ_BLOCK
;
329 command
[2] = USBAT_ATA_DATA
;
333 command
[6] = LSB_of(len
);
334 command
[7] = MSB_of(len
);
336 result
= usbat_execute_command(us
, command
, 8);
337 if (result
!= USB_STOR_XFER_GOOD
)
338 return USB_STOR_TRANSPORT_ERROR
;
340 result
= usbat_bulk_read(us
, content
, len
, use_sg
);
341 return (result
== USB_STOR_XFER_GOOD
?
342 USB_STOR_TRANSPORT_GOOD
: USB_STOR_TRANSPORT_ERROR
);
346 * Write block data via the data register
348 static int usbat_write_block(struct us_data
*us
,
349 unsigned char access
,
350 unsigned char *content
,
356 unsigned char *command
= us
->iobuf
;
359 return USB_STOR_TRANSPORT_GOOD
;
362 command
[1] = access
| USBAT_CMD_WRITE_BLOCK
;
363 command
[2] = USBAT_ATA_DATA
;
367 command
[6] = LSB_of(len
);
368 command
[7] = MSB_of(len
);
370 result
= usbat_execute_command(us
, command
, 8);
372 if (result
!= USB_STOR_XFER_GOOD
)
373 return USB_STOR_TRANSPORT_ERROR
;
375 result
= usbat_bulk_write(us
, content
, len
, use_sg
);
376 if (result
!= USB_STOR_XFER_GOOD
)
377 return USB_STOR_TRANSPORT_ERROR
;
379 return usbat_wait_not_busy(us
, minutes
);
383 * Process read and write requests
385 static int usbat_hp8200e_rw_block_test(struct us_data
*us
,
386 unsigned char access
,
387 unsigned char *registers
,
388 unsigned char *data_out
,
389 unsigned short num_registers
,
390 unsigned char data_reg
,
391 unsigned char status_reg
,
392 unsigned char timeout
,
393 unsigned char qualifier
,
395 unsigned char *content
,
401 unsigned int pipe
= (direction
== DMA_FROM_DEVICE
) ?
402 us
->recv_bulk_pipe
: us
->send_bulk_pipe
;
404 unsigned char *command
= us
->iobuf
;
407 unsigned char *data
= us
->iobuf
;
408 unsigned char *status
= us
->iobuf
;
410 BUG_ON(num_registers
> US_IOBUF_SIZE
/2);
412 for (i
=0; i
<20; i
++) {
415 * The first time we send the full command, which consists
416 * of downloading the SCSI command followed by downloading
417 * the data via a write-and-test. Any other time we only
418 * send the command to download the data -- the SCSI command
419 * is still 'active' in some sense in the device.
421 * We're only going to try sending the data 10 times. After
422 * that, we just return a failure.
428 * Write to multiple registers
429 * Not really sure the 0x07, 0x17, 0xfc, 0xe7 is
430 * necessary here, but that's what came out of the
431 * trace every single time.
434 command
[1] = access
| USBAT_CMD_WRITE_REGS
;
439 command
[6] = LSB_of(num_registers
*2);
440 command
[7] = MSB_of(num_registers
*2);
444 /* Conditionally read or write blocks */
445 command
[cmdlen
-8] = (direction
==DMA_TO_DEVICE
? 0x40 : 0xC0);
446 command
[cmdlen
-7] = access
|
447 (direction
==DMA_TO_DEVICE
?
448 USBAT_CMD_COND_WRITE_BLOCK
: USBAT_CMD_COND_READ_BLOCK
);
449 command
[cmdlen
-6] = data_reg
;
450 command
[cmdlen
-5] = status_reg
;
451 command
[cmdlen
-4] = timeout
;
452 command
[cmdlen
-3] = qualifier
;
453 command
[cmdlen
-2] = LSB_of(len
);
454 command
[cmdlen
-1] = MSB_of(len
);
456 result
= usbat_execute_command(us
, command
, cmdlen
);
458 if (result
!= USB_STOR_XFER_GOOD
)
459 return USB_STOR_TRANSPORT_ERROR
;
463 for (j
=0; j
<num_registers
; j
++) {
464 data
[j
<<1] = registers
[j
];
465 data
[1+(j
<<1)] = data_out
[j
];
468 result
= usbat_bulk_write(us
, data
, num_registers
*2, 0);
469 if (result
!= USB_STOR_XFER_GOOD
)
470 return USB_STOR_TRANSPORT_ERROR
;
474 result
= usb_stor_bulk_transfer_sg(us
,
475 pipe
, content
, len
, use_sg
, NULL
);
478 * If we get a stall on the bulk download, we'll retry
479 * the bulk download -- but not the SCSI command because
480 * in some sense the SCSI command is still 'active' and
481 * waiting for the data. Don't ask me why this should be;
482 * I'm only following what the Windoze driver did.
484 * Note that a stall for the test-and-read/write command means
485 * that the test failed. In this case we're testing to make
486 * sure that the device is error-free
487 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
488 * hypothesis is that the USBAT chip somehow knows what
489 * the device will accept, but doesn't give the device any
490 * data until all data is received. Thus, the device would
491 * still be waiting for the first byte of data if a stall
492 * occurs, even if the stall implies that some data was
496 if (result
== USB_STOR_XFER_SHORT
||
497 result
== USB_STOR_XFER_STALLED
) {
500 * If we're reading and we stalled, then clear
501 * the bulk output pipe only the first time.
504 if (direction
==DMA_FROM_DEVICE
&& i
==0) {
505 if (usb_stor_clear_halt(us
,
506 us
->send_bulk_pipe
) < 0)
507 return USB_STOR_TRANSPORT_ERROR
;
511 * Read status: is the device angry, or just busy?
514 result
= usbat_read(us
, USBAT_ATA
,
515 direction
==DMA_TO_DEVICE
?
516 USBAT_ATA_STATUS
: USBAT_ATA_ALTSTATUS
,
519 if (result
!=USB_STOR_XFER_GOOD
)
520 return USB_STOR_TRANSPORT_ERROR
;
521 if (*status
& 0x01) /* check condition */
522 return USB_STOR_TRANSPORT_FAILED
;
523 if (*status
& 0x20) /* device fault */
524 return USB_STOR_TRANSPORT_FAILED
;
526 US_DEBUGP("Redoing %s\n",
527 direction
==DMA_TO_DEVICE
? "write" : "read");
529 } else if (result
!= USB_STOR_XFER_GOOD
)
530 return USB_STOR_TRANSPORT_ERROR
;
532 return usbat_wait_not_busy(us
, minutes
);
536 US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
537 direction
==DMA_TO_DEVICE
? "Writing" : "Reading");
539 return USB_STOR_TRANSPORT_FAILED
;
543 * Write to multiple registers:
544 * Allows us to write specific data to any registers. The data to be written
545 * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN
546 * which gets sent through bulk out.
547 * Not designed for large transfers of data!
549 static int usbat_multiple_write(struct us_data
*us
,
550 unsigned char *registers
,
551 unsigned char *data_out
,
552 unsigned short num_registers
)
555 unsigned char *data
= us
->iobuf
;
556 unsigned char *command
= us
->iobuf
;
558 BUG_ON(num_registers
> US_IOBUF_SIZE
/2);
560 /* Write to multiple registers, ATA access */
562 command
[1] = USBAT_ATA
| USBAT_CMD_WRITE_REGS
;
570 /* Number of bytes to be transferred (incl. addresses and data) */
571 command
[6] = LSB_of(num_registers
*2);
572 command
[7] = MSB_of(num_registers
*2);
574 /* The setup command */
575 result
= usbat_execute_command(us
, command
, 8);
576 if (result
!= USB_STOR_XFER_GOOD
)
577 return USB_STOR_TRANSPORT_ERROR
;
579 /* Create the reg/data, reg/data sequence */
580 for (i
=0; i
<num_registers
; i
++) {
581 data
[i
<<1] = registers
[i
];
582 data
[1+(i
<<1)] = data_out
[i
];
586 result
= usbat_bulk_write(us
, data
, num_registers
*2, 0);
587 if (result
!= USB_STOR_XFER_GOOD
)
588 return USB_STOR_TRANSPORT_ERROR
;
590 if (usbat_get_device_type(us
) == USBAT_DEV_HP8200
)
591 return usbat_wait_not_busy(us
, 0);
593 return USB_STOR_TRANSPORT_GOOD
;
597 * Conditionally read blocks from device:
598 * Allows us to read blocks from a specific data register, based upon the
599 * condition that a status register can be successfully masked with a status
600 * qualifier. If this condition is not initially met, the read will wait
601 * up until a maximum amount of time has elapsed, as specified by timeout.
602 * The read will start when the condition is met, otherwise the command aborts.
604 * The qualifier defined here is not the value that is masked, it defines
605 * conditions for the write to take place. The actual masked qualifier (and
606 * other related details) are defined beforehand with _set_shuttle_features().
608 static int usbat_read_blocks(struct us_data
*us
,
609 unsigned char *buffer
,
614 unsigned char *command
= us
->iobuf
;
617 command
[1] = USBAT_ATA
| USBAT_CMD_COND_READ_BLOCK
;
618 command
[2] = USBAT_ATA_DATA
;
619 command
[3] = USBAT_ATA_STATUS
;
620 command
[4] = 0xFD; /* Timeout (ms); */
621 command
[5] = USBAT_QUAL_FCQ
;
622 command
[6] = LSB_of(len
);
623 command
[7] = MSB_of(len
);
625 /* Multiple block read setup command */
626 result
= usbat_execute_command(us
, command
, 8);
627 if (result
!= USB_STOR_XFER_GOOD
)
628 return USB_STOR_TRANSPORT_FAILED
;
630 /* Read the blocks we just asked for */
631 result
= usbat_bulk_read(us
, buffer
, len
, use_sg
);
632 if (result
!= USB_STOR_XFER_GOOD
)
633 return USB_STOR_TRANSPORT_FAILED
;
635 return USB_STOR_TRANSPORT_GOOD
;
639 * Conditionally write blocks to device:
640 * Allows us to write blocks to a specific data register, based upon the
641 * condition that a status register can be successfully masked with a status
642 * qualifier. If this condition is not initially met, the write will wait
643 * up until a maximum amount of time has elapsed, as specified by timeout.
644 * The read will start when the condition is met, otherwise the command aborts.
646 * The qualifier defined here is not the value that is masked, it defines
647 * conditions for the write to take place. The actual masked qualifier (and
648 * other related details) are defined beforehand with _set_shuttle_features().
650 static int usbat_write_blocks(struct us_data
*us
,
651 unsigned char *buffer
,
656 unsigned char *command
= us
->iobuf
;
659 command
[1] = USBAT_ATA
| USBAT_CMD_COND_WRITE_BLOCK
;
660 command
[2] = USBAT_ATA_DATA
;
661 command
[3] = USBAT_ATA_STATUS
;
662 command
[4] = 0xFD; /* Timeout (ms) */
663 command
[5] = USBAT_QUAL_FCQ
;
664 command
[6] = LSB_of(len
);
665 command
[7] = MSB_of(len
);
667 /* Multiple block write setup command */
668 result
= usbat_execute_command(us
, command
, 8);
669 if (result
!= USB_STOR_XFER_GOOD
)
670 return USB_STOR_TRANSPORT_FAILED
;
673 result
= usbat_bulk_write(us
, buffer
, len
, use_sg
);
674 if (result
!= USB_STOR_XFER_GOOD
)
675 return USB_STOR_TRANSPORT_FAILED
;
677 return USB_STOR_TRANSPORT_GOOD
;
681 * Read the User IO register
683 static int usbat_read_user_io(struct us_data
*us
, unsigned char *data_flags
)
687 result
= usb_stor_ctrl_transfer(us
,
696 US_DEBUGP("usbat_read_user_io: UIO register reads %02X\n", (unsigned short) (*data_flags
));
702 * Write to the User IO register
704 static int usbat_write_user_io(struct us_data
*us
,
705 unsigned char enable_flags
,
706 unsigned char data_flags
)
708 return usb_stor_ctrl_transfer(us
,
712 short_pack(enable_flags
, data_flags
),
720 * Often needed on media change.
722 static int usbat_device_reset(struct us_data
*us
)
727 * Reset peripheral, enable peripheral control signals
728 * (bring reset signal up)
730 rc
= usbat_write_user_io(us
,
731 USBAT_UIO_DRVRST
| USBAT_UIO_OE1
| USBAT_UIO_OE0
,
732 USBAT_UIO_EPAD
| USBAT_UIO_1
);
733 if (rc
!= USB_STOR_XFER_GOOD
)
734 return USB_STOR_TRANSPORT_ERROR
;
737 * Enable peripheral control signals
738 * (bring reset signal down)
740 rc
= usbat_write_user_io(us
,
741 USBAT_UIO_OE1
| USBAT_UIO_OE0
,
742 USBAT_UIO_EPAD
| USBAT_UIO_1
);
743 if (rc
!= USB_STOR_XFER_GOOD
)
744 return USB_STOR_TRANSPORT_ERROR
;
746 return USB_STOR_TRANSPORT_GOOD
;
752 static int usbat_device_enable_cdt(struct us_data
*us
)
756 /* Enable peripheral control signals and card detect */
757 rc
= usbat_write_user_io(us
,
758 USBAT_UIO_ACKD
| USBAT_UIO_OE1
| USBAT_UIO_OE0
,
759 USBAT_UIO_EPAD
| USBAT_UIO_1
);
760 if (rc
!= USB_STOR_XFER_GOOD
)
761 return USB_STOR_TRANSPORT_ERROR
;
763 return USB_STOR_TRANSPORT_GOOD
;
767 * Determine if media is present.
769 static int usbat_flash_check_media_present(unsigned char *uio
)
771 if (*uio
& USBAT_UIO_UI0
) {
772 US_DEBUGP("usbat_flash_check_media_present: no media detected\n");
773 return USBAT_FLASH_MEDIA_NONE
;
776 return USBAT_FLASH_MEDIA_CF
;
780 * Determine if media has changed since last operation
782 static int usbat_flash_check_media_changed(unsigned char *uio
)
784 if (*uio
& USBAT_UIO_0
) {
785 US_DEBUGP("usbat_flash_check_media_changed: media change detected\n");
786 return USBAT_FLASH_MEDIA_CHANGED
;
789 return USBAT_FLASH_MEDIA_SAME
;
793 * Check for media change / no media and handle the situation appropriately
795 static int usbat_flash_check_media(struct us_data
*us
,
796 struct usbat_info
*info
)
799 unsigned char *uio
= us
->iobuf
;
801 rc
= usbat_read_user_io(us
, uio
);
802 if (rc
!= USB_STOR_XFER_GOOD
)
803 return USB_STOR_TRANSPORT_ERROR
;
805 /* Check for media existence */
806 rc
= usbat_flash_check_media_present(uio
);
807 if (rc
== USBAT_FLASH_MEDIA_NONE
) {
808 info
->sense_key
= 0x02;
809 info
->sense_asc
= 0x3A;
810 info
->sense_ascq
= 0x00;
811 return USB_STOR_TRANSPORT_FAILED
;
814 /* Check for media change */
815 rc
= usbat_flash_check_media_changed(uio
);
816 if (rc
== USBAT_FLASH_MEDIA_CHANGED
) {
818 /* Reset and re-enable card detect */
819 rc
= usbat_device_reset(us
);
820 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
822 rc
= usbat_device_enable_cdt(us
);
823 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
828 rc
= usbat_read_user_io(us
, uio
);
829 if (rc
!= USB_STOR_XFER_GOOD
)
830 return USB_STOR_TRANSPORT_ERROR
;
832 info
->sense_key
= UNIT_ATTENTION
;
833 info
->sense_asc
= 0x28;
834 info
->sense_ascq
= 0x00;
835 return USB_STOR_TRANSPORT_FAILED
;
838 return USB_STOR_TRANSPORT_GOOD
;
842 * Determine whether we are controlling a flash-based reader/writer,
843 * or a HP8200-based CD drive.
844 * Sets transport functions as appropriate.
846 static int usbat_identify_device(struct us_data
*us
,
847 struct usbat_info
*info
)
850 unsigned char status
;
853 return USB_STOR_TRANSPORT_ERROR
;
855 rc
= usbat_device_reset(us
);
856 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
861 * In attempt to distinguish between HP CDRW's and Flash readers, we now
862 * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
863 * readers), this command should fail with error. On ATAPI devices (i.e.
864 * CDROM drives), it should succeed.
866 rc
= usbat_write(us
, USBAT_ATA
, USBAT_ATA_CMD
, 0xA1);
867 if (rc
!= USB_STOR_XFER_GOOD
)
868 return USB_STOR_TRANSPORT_ERROR
;
870 rc
= usbat_get_status(us
, &status
);
871 if (rc
!= USB_STOR_XFER_GOOD
)
872 return USB_STOR_TRANSPORT_ERROR
;
874 /* Check for error bit, or if the command 'fell through' */
875 if (status
== 0xA1 || !(status
& 0x01)) {
876 /* Device is HP 8200 */
877 US_DEBUGP("usbat_identify_device: Detected HP8200 CDRW\n");
878 info
->devicetype
= USBAT_DEV_HP8200
;
880 /* Device is a CompactFlash reader/writer */
881 US_DEBUGP("usbat_identify_device: Detected Flash reader/writer\n");
882 info
->devicetype
= USBAT_DEV_FLASH
;
885 return USB_STOR_TRANSPORT_GOOD
;
889 * Set the transport function based on the device type
891 static int usbat_set_transport(struct us_data
*us
,
892 struct usbat_info
*info
,
896 if (!info
->devicetype
)
897 info
->devicetype
= devicetype
;
899 if (!info
->devicetype
)
900 usbat_identify_device(us
, info
);
902 switch (info
->devicetype
) {
904 return USB_STOR_TRANSPORT_ERROR
;
906 case USBAT_DEV_HP8200
:
907 us
->transport
= usbat_hp8200e_transport
;
910 case USBAT_DEV_FLASH
:
911 us
->transport
= usbat_flash_transport
;
919 * Read the media capacity
921 static int usbat_flash_get_sector_count(struct us_data
*us
,
922 struct usbat_info
*info
)
924 unsigned char registers
[3] = {
929 unsigned char command
[3] = { 0x01, 0xA0, 0xEC };
930 unsigned char *reply
;
931 unsigned char status
;
935 return USB_STOR_TRANSPORT_ERROR
;
937 reply
= kmalloc(512, GFP_NOIO
);
939 return USB_STOR_TRANSPORT_ERROR
;
941 /* ATA command : IDENTIFY DEVICE */
942 rc
= usbat_multiple_write(us
, registers
, command
, 3);
943 if (rc
!= USB_STOR_XFER_GOOD
) {
944 US_DEBUGP("usbat_flash_get_sector_count: Gah! identify_device failed\n");
945 rc
= USB_STOR_TRANSPORT_ERROR
;
949 /* Read device status */
950 if (usbat_get_status(us
, &status
) != USB_STOR_XFER_GOOD
) {
951 rc
= USB_STOR_TRANSPORT_ERROR
;
957 /* Read the device identification data */
958 rc
= usbat_read_block(us
, reply
, 512, 0);
959 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
962 info
->sectors
= ((u32
)(reply
[117]) << 24) |
963 ((u32
)(reply
[116]) << 16) |
964 ((u32
)(reply
[115]) << 8) |
965 ((u32
)(reply
[114]) );
967 rc
= USB_STOR_TRANSPORT_GOOD
;
975 * Read data from device
977 static int usbat_flash_read_data(struct us_data
*us
,
978 struct usbat_info
*info
,
982 unsigned char registers
[7] = {
991 unsigned char command
[7];
992 unsigned char *buffer
;
993 unsigned char thistime
;
994 unsigned int totallen
, alloclen
;
996 unsigned int sg_idx
= 0, sg_offset
= 0;
998 result
= usbat_flash_check_media(us
, info
);
999 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1003 * we're working in LBA mode. according to the ATA spec,
1004 * we can support up to 28-bit addressing. I don't know if Jumpshot
1005 * supports beyond 24-bit addressing. It's kind of hard to test
1006 * since it requires > 8GB CF card.
1009 if (sector
> 0x0FFFFFFF)
1010 return USB_STOR_TRANSPORT_ERROR
;
1012 totallen
= sectors
* info
->ssize
;
1015 * Since we don't read more than 64 KB at a time, we have to create
1016 * a bounce buffer and move the data a piece at a time between the
1017 * bounce buffer and the actual transfer buffer.
1020 alloclen
= min(totallen
, 65536u);
1021 buffer
= kmalloc(alloclen
, GFP_NOIO
);
1023 return USB_STOR_TRANSPORT_ERROR
;
1027 * loop, never allocate or transfer more than 64k at once
1028 * (min(128k, 255*info->ssize) is the real limit)
1030 len
= min(totallen
, alloclen
);
1031 thistime
= (len
/ info
->ssize
) & 0xff;
1033 /* ATA command 0x20 (READ SECTORS) */
1034 usbat_pack_ata_sector_cmd(command
, thistime
, sector
, 0x20);
1036 /* Write/execute ATA read command */
1037 result
= usbat_multiple_write(us
, registers
, command
, 7);
1038 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1041 /* Read the data we just requested */
1042 result
= usbat_read_blocks(us
, buffer
, len
, 0);
1043 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1046 US_DEBUGP("usbat_flash_read_data: %d bytes\n", len
);
1048 /* Store the data in the transfer buffer */
1049 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
1050 &sg_idx
, &sg_offset
, TO_XFER_BUF
);
1054 } while (totallen
> 0);
1057 return USB_STOR_TRANSPORT_GOOD
;
1061 return USB_STOR_TRANSPORT_ERROR
;
1065 * Write data to device
1067 static int usbat_flash_write_data(struct us_data
*us
,
1068 struct usbat_info
*info
,
1072 unsigned char registers
[7] = {
1081 unsigned char command
[7];
1082 unsigned char *buffer
;
1083 unsigned char thistime
;
1084 unsigned int totallen
, alloclen
;
1086 unsigned int sg_idx
= 0, sg_offset
= 0;
1088 result
= usbat_flash_check_media(us
, info
);
1089 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1093 * we're working in LBA mode. according to the ATA spec,
1094 * we can support up to 28-bit addressing. I don't know if the device
1095 * supports beyond 24-bit addressing. It's kind of hard to test
1096 * since it requires > 8GB media.
1099 if (sector
> 0x0FFFFFFF)
1100 return USB_STOR_TRANSPORT_ERROR
;
1102 totallen
= sectors
* info
->ssize
;
1105 * Since we don't write more than 64 KB at a time, we have to create
1106 * a bounce buffer and move the data a piece at a time between the
1107 * bounce buffer and the actual transfer buffer.
1110 alloclen
= min(totallen
, 65536u);
1111 buffer
= kmalloc(alloclen
, GFP_NOIO
);
1113 return USB_STOR_TRANSPORT_ERROR
;
1117 * loop, never allocate or transfer more than 64k at once
1118 * (min(128k, 255*info->ssize) is the real limit)
1120 len
= min(totallen
, alloclen
);
1121 thistime
= (len
/ info
->ssize
) & 0xff;
1123 /* Get the data from the transfer buffer */
1124 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
1125 &sg_idx
, &sg_offset
, FROM_XFER_BUF
);
1127 /* ATA command 0x30 (WRITE SECTORS) */
1128 usbat_pack_ata_sector_cmd(command
, thistime
, sector
, 0x30);
1130 /* Write/execute ATA write command */
1131 result
= usbat_multiple_write(us
, registers
, command
, 7);
1132 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1135 /* Write the data */
1136 result
= usbat_write_blocks(us
, buffer
, len
, 0);
1137 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1142 } while (totallen
> 0);
1149 return USB_STOR_TRANSPORT_ERROR
;
1153 * Squeeze a potentially huge (> 65535 byte) read10 command into
1154 * a little ( <= 65535 byte) ATAPI pipe
1156 static int usbat_hp8200e_handle_read10(struct us_data
*us
,
1157 unsigned char *registers
,
1158 unsigned char *data
,
1159 struct scsi_cmnd
*srb
)
1161 int result
= USB_STOR_TRANSPORT_GOOD
;
1162 unsigned char *buffer
;
1164 unsigned int sector
;
1165 unsigned int sg_segment
= 0;
1166 unsigned int sg_offset
= 0;
1168 US_DEBUGP("handle_read10: transfersize %d\n",
1171 if (srb
->request_bufflen
< 0x10000) {
1173 result
= usbat_hp8200e_rw_block_test(us
, USBAT_ATA
,
1174 registers
, data
, 19,
1175 USBAT_ATA_DATA
, USBAT_ATA_STATUS
, 0xFD,
1176 (USBAT_QUAL_FCQ
| USBAT_QUAL_ALQ
),
1178 srb
->request_buffer
,
1179 srb
->request_bufflen
, srb
->use_sg
, 1);
1185 * Since we're requesting more data than we can handle in
1186 * a single read command (max is 64k-1), we will perform
1187 * multiple reads, but each read must be in multiples of
1188 * a sector. Luckily the sector size is in srb->transfersize
1189 * (see linux/drivers/scsi/sr.c).
1192 if (data
[7+0] == GPCMD_READ_CD
) {
1193 len
= short_pack(data
[7+9], data
[7+8]);
1196 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len
);
1197 srb
->transfersize
= srb
->request_bufflen
/len
;
1200 if (!srb
->transfersize
) {
1201 srb
->transfersize
= 2048; /* A guess */
1202 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
1207 * Since we only read in one block at a time, we have to create
1208 * a bounce buffer and move the data a piece at a time between the
1209 * bounce buffer and the actual transfer buffer.
1212 len
= (65535/srb
->transfersize
) * srb
->transfersize
;
1213 US_DEBUGP("Max read is %d bytes\n", len
);
1214 len
= min(len
, srb
->request_bufflen
);
1215 buffer
= kmalloc(len
, GFP_NOIO
);
1216 if (buffer
== NULL
) /* bloody hell! */
1217 return USB_STOR_TRANSPORT_FAILED
;
1218 sector
= short_pack(data
[7+3], data
[7+2]);
1220 sector
|= short_pack(data
[7+5], data
[7+4]);
1223 sg_segment
= 0; /* for keeping track of where we are in */
1224 sg_offset
= 0; /* the scatter/gather list */
1226 while (transferred
!= srb
->request_bufflen
) {
1228 if (len
> srb
->request_bufflen
- transferred
)
1229 len
= srb
->request_bufflen
- transferred
;
1231 data
[3] = len
&0xFF; /* (cylL) = expected length (L) */
1232 data
[4] = (len
>>8)&0xFF; /* (cylH) = expected length (H) */
1234 /* Fix up the SCSI command sector and num sectors */
1236 data
[7+2] = MSB_of(sector
>>16); /* SCSI command sector */
1237 data
[7+3] = LSB_of(sector
>>16);
1238 data
[7+4] = MSB_of(sector
&0xFFFF);
1239 data
[7+5] = LSB_of(sector
&0xFFFF);
1240 if (data
[7+0] == GPCMD_READ_CD
)
1242 data
[7+7] = MSB_of(len
/ srb
->transfersize
); /* SCSI command */
1243 data
[7+8] = LSB_of(len
/ srb
->transfersize
); /* num sectors */
1245 result
= usbat_hp8200e_rw_block_test(us
, USBAT_ATA
,
1246 registers
, data
, 19,
1247 USBAT_ATA_DATA
, USBAT_ATA_STATUS
, 0xFD,
1248 (USBAT_QUAL_FCQ
| USBAT_QUAL_ALQ
),
1253 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1256 /* Store the data in the transfer buffer */
1257 usb_stor_access_xfer_buf(buffer
, len
, srb
,
1258 &sg_segment
, &sg_offset
, TO_XFER_BUF
);
1260 /* Update the amount transferred and the sector number */
1263 sector
+= len
/ srb
->transfersize
;
1265 } /* while transferred != srb->request_bufflen */
1271 static int usbat_select_and_test_registers(struct us_data
*us
)
1274 unsigned char *status
= us
->iobuf
;
1276 /* try device = master, then device = slave. */
1277 for (selector
= 0xA0; selector
<= 0xB0; selector
+= 0x10) {
1278 if (usbat_write(us
, USBAT_ATA
, USBAT_ATA_DEVICE
, selector
) !=
1280 return USB_STOR_TRANSPORT_ERROR
;
1282 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_STATUS
, status
) !=
1284 return USB_STOR_TRANSPORT_ERROR
;
1286 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_DEVICE
, status
) !=
1288 return USB_STOR_TRANSPORT_ERROR
;
1290 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, status
) !=
1292 return USB_STOR_TRANSPORT_ERROR
;
1294 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_HI
, status
) !=
1296 return USB_STOR_TRANSPORT_ERROR
;
1298 if (usbat_write(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, 0x55) !=
1300 return USB_STOR_TRANSPORT_ERROR
;
1302 if (usbat_write(us
, USBAT_ATA
, USBAT_ATA_LBA_HI
, 0xAA) !=
1304 return USB_STOR_TRANSPORT_ERROR
;
1306 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, status
) !=
1308 return USB_STOR_TRANSPORT_ERROR
;
1310 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, status
) !=
1312 return USB_STOR_TRANSPORT_ERROR
;
1315 return USB_STOR_TRANSPORT_GOOD
;
1319 * Initialize the USBAT processor and the storage device
1321 static int init_usbat(struct us_data
*us
, int devicetype
)
1324 struct usbat_info
*info
;
1325 unsigned char subcountH
= USBAT_ATA_LBA_HI
;
1326 unsigned char subcountL
= USBAT_ATA_LBA_ME
;
1327 unsigned char *status
= us
->iobuf
;
1329 us
->extra
= kzalloc(sizeof(struct usbat_info
), GFP_NOIO
);
1331 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1334 info
= (struct usbat_info
*) (us
->extra
);
1336 /* Enable peripheral control signals */
1337 rc
= usbat_write_user_io(us
,
1338 USBAT_UIO_OE1
| USBAT_UIO_OE0
,
1339 USBAT_UIO_EPAD
| USBAT_UIO_1
);
1340 if (rc
!= USB_STOR_XFER_GOOD
)
1341 return USB_STOR_TRANSPORT_ERROR
;
1343 US_DEBUGP("INIT 1\n");
1347 rc
= usbat_read_user_io(us
, status
);
1348 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1351 US_DEBUGP("INIT 2\n");
1353 rc
= usbat_read_user_io(us
, status
);
1354 if (rc
!= USB_STOR_XFER_GOOD
)
1355 return USB_STOR_TRANSPORT_ERROR
;
1357 rc
= usbat_read_user_io(us
, status
);
1358 if (rc
!= USB_STOR_XFER_GOOD
)
1359 return USB_STOR_TRANSPORT_ERROR
;
1361 US_DEBUGP("INIT 3\n");
1363 rc
= usbat_select_and_test_registers(us
);
1364 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1367 US_DEBUGP("INIT 4\n");
1369 rc
= usbat_read_user_io(us
, status
);
1370 if (rc
!= USB_STOR_XFER_GOOD
)
1371 return USB_STOR_TRANSPORT_ERROR
;
1373 US_DEBUGP("INIT 5\n");
1375 /* Enable peripheral control signals and card detect */
1376 rc
= usbat_device_enable_cdt(us
);
1377 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1380 US_DEBUGP("INIT 6\n");
1382 rc
= usbat_read_user_io(us
, status
);
1383 if (rc
!= USB_STOR_XFER_GOOD
)
1384 return USB_STOR_TRANSPORT_ERROR
;
1386 US_DEBUGP("INIT 7\n");
1390 rc
= usbat_read_user_io(us
, status
);
1391 if (rc
!= USB_STOR_XFER_GOOD
)
1392 return USB_STOR_TRANSPORT_ERROR
;
1394 US_DEBUGP("INIT 8\n");
1396 rc
= usbat_select_and_test_registers(us
);
1397 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1400 US_DEBUGP("INIT 9\n");
1402 /* At this point, we need to detect which device we are using */
1403 if (usbat_set_transport(us
, info
, devicetype
))
1404 return USB_STOR_TRANSPORT_ERROR
;
1406 US_DEBUGP("INIT 10\n");
1408 if (usbat_get_device_type(us
) == USBAT_DEV_FLASH
) {
1412 rc
= usbat_set_shuttle_features(us
, (USBAT_FEAT_ETEN
| USBAT_FEAT_ET2
| USBAT_FEAT_ET1
),
1413 0x00, 0x88, 0x08, subcountH
, subcountL
);
1414 if (rc
!= USB_STOR_XFER_GOOD
)
1415 return USB_STOR_TRANSPORT_ERROR
;
1417 US_DEBUGP("INIT 11\n");
1419 return USB_STOR_TRANSPORT_GOOD
;
1423 * Transport for the HP 8200e
1425 static int usbat_hp8200e_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
1428 unsigned char *status
= us
->iobuf
;
1429 unsigned char registers
[32];
1430 unsigned char data
[32];
1435 len
= srb
->request_bufflen
;
1437 /* Send A0 (ATA PACKET COMMAND).
1438 Note: I guess we're never going to get any of the ATA
1439 commands... just ATA Packet Commands.
1442 registers
[0] = USBAT_ATA_FEATURES
;
1443 registers
[1] = USBAT_ATA_SECCNT
;
1444 registers
[2] = USBAT_ATA_SECNUM
;
1445 registers
[3] = USBAT_ATA_LBA_ME
;
1446 registers
[4] = USBAT_ATA_LBA_HI
;
1447 registers
[5] = USBAT_ATA_DEVICE
;
1448 registers
[6] = USBAT_ATA_CMD
;
1452 data
[3] = len
&0xFF; /* (cylL) = expected length (L) */
1453 data
[4] = (len
>>8)&0xFF; /* (cylH) = expected length (H) */
1454 data
[5] = 0xB0; /* (device sel) = slave */
1455 data
[6] = 0xA0; /* (command) = ATA PACKET COMMAND */
1457 for (i
=7; i
<19; i
++) {
1458 registers
[i
] = 0x10;
1459 data
[i
] = (i
-7 >= srb
->cmd_len
) ? 0 : srb
->cmnd
[i
-7];
1462 result
= usbat_get_status(us
, status
);
1463 US_DEBUGP("Status = %02X\n", *status
);
1464 if (result
!= USB_STOR_XFER_GOOD
)
1465 return USB_STOR_TRANSPORT_ERROR
;
1466 if (srb
->cmnd
[0] == TEST_UNIT_READY
)
1469 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
1471 result
= usbat_hp8200e_rw_block_test(us
, USBAT_ATA
,
1472 registers
, data
, 19,
1473 USBAT_ATA_DATA
, USBAT_ATA_STATUS
, 0xFD,
1474 (USBAT_QUAL_FCQ
| USBAT_QUAL_ALQ
),
1476 srb
->request_buffer
,
1477 len
, srb
->use_sg
, 10);
1479 if (result
== USB_STOR_TRANSPORT_GOOD
) {
1481 US_DEBUGP("Wrote %08X bytes\n", transferred
);
1486 } else if (srb
->cmnd
[0] == READ_10
||
1487 srb
->cmnd
[0] == GPCMD_READ_CD
) {
1489 return usbat_hp8200e_handle_read10(us
, registers
, data
, srb
);
1494 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1496 return USB_STOR_TRANSPORT_ERROR
;
1499 if ( (result
= usbat_multiple_write(us
,
1500 registers
, data
, 7)) != USB_STOR_TRANSPORT_GOOD
) {
1505 * Write the 12-byte command header.
1507 * If the command is BLANK then set the timer for 75 minutes.
1508 * Otherwise set it for 10 minutes.
1510 * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1511 * AT SPEED 4 IS UNRELIABLE!!!
1514 if ((result
= usbat_write_block(us
,
1515 USBAT_ATA
, srb
->cmnd
, 12,
1516 (srb
->cmnd
[0]==GPCMD_BLANK
? 75 : 10), 0) !=
1517 USB_STOR_TRANSPORT_GOOD
)) {
1521 /* If there is response data to be read in then do it here. */
1523 if (len
!= 0 && (srb
->sc_data_direction
== DMA_FROM_DEVICE
)) {
1525 /* How many bytes to read in? Check cylL register */
1527 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, status
) !=
1528 USB_STOR_XFER_GOOD
) {
1529 return USB_STOR_TRANSPORT_ERROR
;
1532 if (len
> 0xFF) { /* need to read cylH also */
1534 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_HI
, status
) !=
1535 USB_STOR_XFER_GOOD
) {
1536 return USB_STOR_TRANSPORT_ERROR
;
1538 len
+= ((unsigned int) *status
)<<8;
1544 result
= usbat_read_block(us
, srb
->request_buffer
, len
, srb
->use_sg
);
1546 /* Debug-print the first 32 bytes of the transfer */
1550 for (i
=0; i
<len
&& i
<32; i
++) {
1551 sprintf(string
+strlen(string
), "%02X ",
1552 ((unsigned char *)srb
->request_buffer
)[i
]);
1554 US_DEBUGP("%s\n", string
);
1559 US_DEBUGP("%s\n", string
);
1567 * Transport for USBAT02-based CompactFlash and similar storage devices
1569 static int usbat_flash_transport(struct scsi_cmnd
* srb
, struct us_data
*us
)
1572 struct usbat_info
*info
= (struct usbat_info
*) (us
->extra
);
1573 unsigned long block
, blocks
;
1574 unsigned char *ptr
= us
->iobuf
;
1575 static unsigned char inquiry_response
[36] = {
1576 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1579 if (srb
->cmnd
[0] == INQUIRY
) {
1580 US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1581 memcpy(ptr
, inquiry_response
, sizeof(inquiry_response
));
1582 fill_inquiry_response(us
, ptr
, 36);
1583 return USB_STOR_TRANSPORT_GOOD
;
1586 if (srb
->cmnd
[0] == READ_CAPACITY
) {
1587 rc
= usbat_flash_check_media(us
, info
);
1588 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1591 rc
= usbat_flash_get_sector_count(us
, info
);
1592 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1595 /* hard coded 512 byte sectors as per ATA spec */
1596 info
->ssize
= 0x200;
1597 US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1598 info
->sectors
, info
->ssize
);
1602 * note: must return the sector number of the last sector,
1603 * *not* the total number of sectors
1605 ((__be32
*) ptr
)[0] = cpu_to_be32(info
->sectors
- 1);
1606 ((__be32
*) ptr
)[1] = cpu_to_be32(info
->ssize
);
1607 usb_stor_set_xfer_buf(ptr
, 8, srb
);
1609 return USB_STOR_TRANSPORT_GOOD
;
1612 if (srb
->cmnd
[0] == MODE_SELECT_10
) {
1613 US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
1614 return USB_STOR_TRANSPORT_ERROR
;
1617 if (srb
->cmnd
[0] == READ_10
) {
1618 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
1619 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
1621 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
1623 US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block
, blocks
);
1624 return usbat_flash_read_data(us
, info
, block
, blocks
);
1627 if (srb
->cmnd
[0] == READ_12
) {
1629 * I don't think we'll ever see a READ_12 but support it anyway
1631 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
1632 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
1634 blocks
= ((u32
)(srb
->cmnd
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
1635 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
1637 US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block
, blocks
);
1638 return usbat_flash_read_data(us
, info
, block
, blocks
);
1641 if (srb
->cmnd
[0] == WRITE_10
) {
1642 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
1643 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
1645 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
1647 US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block
, blocks
);
1648 return usbat_flash_write_data(us
, info
, block
, blocks
);
1651 if (srb
->cmnd
[0] == WRITE_12
) {
1653 * I don't think we'll ever see a WRITE_12 but support it anyway
1655 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
1656 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
1658 blocks
= ((u32
)(srb
->cmnd
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
1659 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
1661 US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block
, blocks
);
1662 return usbat_flash_write_data(us
, info
, block
, blocks
);
1666 if (srb
->cmnd
[0] == TEST_UNIT_READY
) {
1667 US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
1669 rc
= usbat_flash_check_media(us
, info
);
1670 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1673 return usbat_check_status(us
);
1676 if (srb
->cmnd
[0] == REQUEST_SENSE
) {
1677 US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
1681 ptr
[2] = info
->sense_key
;
1683 ptr
[12] = info
->sense_asc
;
1684 ptr
[13] = info
->sense_ascq
;
1685 usb_stor_set_xfer_buf(ptr
, 18, srb
);
1687 return USB_STOR_TRANSPORT_GOOD
;
1690 if (srb
->cmnd
[0] == ALLOW_MEDIUM_REMOVAL
) {
1692 * sure. whatever. not like we can stop the user from popping
1693 * the media out of the device (no locking doors, etc)
1695 return USB_STOR_TRANSPORT_GOOD
;
1698 US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1699 srb
->cmnd
[0], srb
->cmnd
[0]);
1700 info
->sense_key
= 0x05;
1701 info
->sense_asc
= 0x20;
1702 info
->sense_ascq
= 0x00;
1703 return USB_STOR_TRANSPORT_FAILED
;
1706 int init_usbat_cd(struct us_data
*us
)
1708 return init_usbat(us
, USBAT_DEV_HP8200
);
1712 int init_usbat_flash(struct us_data
*us
)
1714 return init_usbat(us
, USBAT_DEV_FLASH
);
1717 int init_usbat_probe(struct us_data
*us
)
1719 return init_usbat(us
, 0);
1723 * Default transport function. Attempts to detect which transport function
1724 * should be called, makes it the new default, and calls it.
1726 * This function should never be called. Our usbat_init() function detects the
1727 * device type and changes the us->transport ptr to the transport function
1728 * relevant to the device.
1729 * However, we'll support this impossible(?) case anyway.
1731 int usbat_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
1733 struct usbat_info
*info
= (struct usbat_info
*) (us
->extra
);
1735 if (usbat_set_transport(us
, info
, 0))
1736 return USB_STOR_TRANSPORT_ERROR
;
1738 return us
->transport(srb
, us
);