1 /* Driver for SCM Microsystems USB-ATAPI cable
3 * $Id: shuttle_usbat.c,v 1.17 2002/04/22 03:39:43 mdharm Exp $
5 * Current development and maintenance by:
6 * (c) 2000, 2001 Robert Baruch (autophile@starband.net)
7 * (c) 2004, 2005 Daniel Drake <dsd@gentoo.org>
9 * Developed with the assistance of:
10 * (c) 2002 Alan Stern <stern@rowland.org>
12 * Flash support based on earlier work by:
13 * (c) 2002 Thomas Kreiling <usbdev@sm04.de>
15 * Many originally ATAPI devices were slightly modified to meet the USB
16 * market by using some kind of translation from ATAPI to USB on the host,
17 * and the peripheral would translate from USB back to ATAPI.
19 * SCM Microsystems (www.scmmicro.com) makes a device, sold to OEM's only,
20 * which does the USB-to-ATAPI conversion. By obtaining the data sheet on
21 * their device under nondisclosure agreement, I have been able to write
22 * this driver for Linux.
24 * The chip used in the device can also be used for EPP and ISA translation
25 * as well. This driver is only guaranteed to work with the ATAPI
28 * See the Kconfig help text for a list of devices known to be supported by
31 * This program is free software; you can redistribute it and/or modify it
32 * under the terms of the GNU General Public License as published by the
33 * Free Software Foundation; either version 2, or (at your option) any
36 * This program is distributed in the hope that it will be useful, but
37 * WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 * General Public License for more details.
41 * You should have received a copy of the GNU General Public License along
42 * with this program; if not, write to the Free Software Foundation, Inc.,
43 * 675 Mass Ave, Cambridge, MA 02139, USA.
46 #include <linux/sched.h>
47 #include <linux/errno.h>
48 #include <linux/slab.h>
49 #include <linux/cdrom.h>
51 #include <scsi/scsi.h>
52 #include <scsi/scsi_cmnd.h>
55 #include "transport.h"
58 #include "shuttle_usbat.h"
60 #define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
61 #define LSB_of(s) ((s)&0xFF)
62 #define MSB_of(s) ((s)>>8)
64 static int transferred
= 0;
66 static int usbat_flash_transport(struct scsi_cmnd
* srb
, struct us_data
*us
);
67 static int usbat_hp8200e_transport(struct scsi_cmnd
*srb
, struct us_data
*us
);
70 * Convenience function to produce an ATAPI read/write sectors command
71 * Use cmd=0x20 for read, cmd=0x30 for write
73 static void usbat_pack_atapi_sector_cmd(unsigned char *buf
,
74 unsigned char thistime
,
75 u32 sector
, unsigned char cmd
)
79 buf
[2] = sector
& 0xFF;
80 buf
[3] = (sector
>> 8) & 0xFF;
81 buf
[4] = (sector
>> 16) & 0xFF;
82 buf
[5] = 0xE0 | ((sector
>> 24) & 0x0F);
87 * Convenience function to get the device type (flash or hp8200)
89 static int usbat_get_device_type(struct us_data
*us
)
91 return ((struct usbat_info
*)us
->extra
)->devicetype
;
95 * Read a register from the device
97 static int usbat_read(struct us_data
*us
,
100 unsigned char *content
)
102 return usb_stor_ctrl_transfer(us
,
104 access
| USBAT_CMD_READ_REG
,
113 * Write to a register on the device
115 static int usbat_write(struct us_data
*us
,
116 unsigned char access
,
118 unsigned char content
)
120 return usb_stor_ctrl_transfer(us
,
122 access
| USBAT_CMD_WRITE_REG
,
124 short_pack(reg
, content
),
131 * Convenience function to perform a bulk read
133 static int usbat_bulk_read(struct us_data
*us
,
138 return USB_STOR_XFER_GOOD
;
140 US_DEBUGP("usbat_bulk_read: len = %d\n", len
);
141 return usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, data
, len
, NULL
);
145 * Convenience function to perform a bulk write
147 static int usbat_bulk_write(struct us_data
*us
,
152 return USB_STOR_XFER_GOOD
;
154 US_DEBUGP("usbat_bulk_write: len = %d\n", len
);
155 return usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
, data
, len
, NULL
);
159 * Some USBAT-specific commands can only be executed over a command transport
160 * This transport allows one (len=8) or two (len=16) vendor-specific commands
163 static int usbat_execute_command(struct us_data
*us
,
164 unsigned char *commands
,
167 return usb_stor_ctrl_transfer(us
, us
->send_ctrl_pipe
,
168 USBAT_CMD_EXEC_CMD
, 0x40, 0, 0,
173 * Read the status register
175 static int usbat_get_status(struct us_data
*us
, unsigned char *status
)
178 rc
= usbat_read(us
, USBAT_ATA
, USBAT_ATA_STATUS
, status
);
180 US_DEBUGP("usbat_get_status: 0x%02X\n", (unsigned short) (*status
));
185 * Check the device status
187 static int usbat_check_status(struct us_data
*us
)
189 unsigned char *reply
= us
->iobuf
;
193 return USB_STOR_TRANSPORT_ERROR
;
195 rc
= usbat_get_status(us
, reply
);
196 if (rc
!= USB_STOR_XFER_GOOD
)
197 return USB_STOR_TRANSPORT_FAILED
;
199 if (*reply
& 0x01 && *reply
!= 0x51) // error/check condition (0x51 is ok)
200 return USB_STOR_TRANSPORT_FAILED
;
202 if (*reply
& 0x20) // device fault
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
;
225 // The only bit relevant to ATA access is bit 6
226 // which defines 8 bit data access (set) or 16 bit (unset)
227 command
[2] = epp_control
;
229 // If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
230 // ET1 and ET2 define an external event to be checked for on event of a
231 // _read_blocks or _write_blocks operation. The read/write will not take
232 // place unless the defined trigger signal is active.
233 command
[3] = external_trigger
;
235 // The resultant byte of the mask operation (see mask_byte) is compared for
236 // equivalence with this test pattern. If equal, the read/write will take
238 command
[4] = test_pattern
;
240 // This value is logically ANDed with the status register field specified
241 // in the read/write command.
242 command
[5] = mask_byte
;
244 // If ALQ is set in the qualifier, this field contains the address of the
245 // registers where the byte count should be read for transferring the data.
246 // If ALQ is not set, then this field contains the number of bytes to be
248 command
[6] = subcountL
;
249 command
[7] = subcountH
;
251 return usbat_execute_command(us
, command
, 8);
255 * Block, waiting for an ATA device to become not busy or to report
256 * an error condition.
258 static int usbat_wait_not_busy(struct us_data
*us
, int minutes
)
262 unsigned char *status
= us
->iobuf
;
264 /* Synchronizing cache on a CDR could take a heck of a long time,
265 * but probably not more than 10 minutes or so. On the other hand,
266 * doing a full blank on a CDRW at speed 1 will take about 75
270 for (i
=0; i
<1200+minutes
*60; i
++) {
272 result
= usbat_get_status(us
, status
);
274 if (result
!=USB_STOR_XFER_GOOD
)
275 return USB_STOR_TRANSPORT_ERROR
;
276 if (*status
& 0x01) { // check condition
277 result
= usbat_read(us
, USBAT_ATA
, 0x10, status
);
278 return USB_STOR_TRANSPORT_FAILED
;
280 if (*status
& 0x20) // device fault
281 return USB_STOR_TRANSPORT_FAILED
;
283 if ((*status
& 0x80)==0x00) { // not busy
284 US_DEBUGP("Waited not busy for %d steps\n", i
);
285 return USB_STOR_TRANSPORT_GOOD
;
289 msleep(10); // 5 seconds
291 msleep(50); // 10 seconds
293 msleep(100); // 50 seconds
295 msleep(1000); // X minutes
298 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
300 return USB_STOR_TRANSPORT_FAILED
;
304 * Read block data from the data register
306 static int usbat_read_block(struct us_data
*us
,
307 unsigned char *content
,
311 unsigned char *command
= us
->iobuf
;
314 return USB_STOR_TRANSPORT_GOOD
;
317 command
[1] = USBAT_ATA
| USBAT_CMD_READ_BLOCK
;
318 command
[2] = USBAT_ATA_DATA
;
322 command
[6] = LSB_of(len
);
323 command
[7] = MSB_of(len
);
325 result
= usbat_execute_command(us
, command
, 8);
326 if (result
!= USB_STOR_XFER_GOOD
)
327 return USB_STOR_TRANSPORT_ERROR
;
329 result
= usbat_bulk_read(us
, content
, len
);
330 return (result
== USB_STOR_XFER_GOOD
?
331 USB_STOR_TRANSPORT_GOOD
: USB_STOR_TRANSPORT_ERROR
);
335 * Write block data via the data register
337 static int usbat_write_block(struct us_data
*us
,
338 unsigned char access
,
339 unsigned char *content
,
344 unsigned char *command
= us
->iobuf
;
347 return USB_STOR_TRANSPORT_GOOD
;
350 command
[1] = access
| USBAT_CMD_WRITE_BLOCK
;
351 command
[2] = USBAT_ATA_DATA
;
355 command
[6] = LSB_of(len
);
356 command
[7] = MSB_of(len
);
358 result
= usbat_execute_command(us
, command
, 8);
360 if (result
!= USB_STOR_XFER_GOOD
)
361 return USB_STOR_TRANSPORT_ERROR
;
363 result
= usbat_bulk_write(us
, content
, len
);
364 if (result
!= USB_STOR_XFER_GOOD
)
365 return USB_STOR_TRANSPORT_ERROR
;
367 return usbat_wait_not_busy(us
, minutes
);
371 * Process read and write requests
373 static int usbat_hp8200e_rw_block_test(struct us_data
*us
,
374 unsigned char access
,
375 unsigned char *registers
,
376 unsigned char *data_out
,
377 unsigned short num_registers
,
378 unsigned char data_reg
,
379 unsigned char status_reg
,
380 unsigned char timeout
,
381 unsigned char qualifier
,
383 unsigned char *content
,
389 unsigned int pipe
= (direction
== DMA_FROM_DEVICE
) ?
390 us
->recv_bulk_pipe
: us
->send_bulk_pipe
;
392 unsigned char *command
= us
->iobuf
;
395 unsigned char *data
= us
->iobuf
;
396 unsigned char *status
= us
->iobuf
;
398 BUG_ON(num_registers
> US_IOBUF_SIZE
/2);
400 for (i
=0; i
<20; i
++) {
403 * The first time we send the full command, which consists
404 * of downloading the SCSI command followed by downloading
405 * the data via a write-and-test. Any other time we only
406 * send the command to download the data -- the SCSI command
407 * is still 'active' in some sense in the device.
409 * We're only going to try sending the data 10 times. After
410 * that, we just return a failure.
415 // Write to multiple registers
416 // Not really sure the 0x07, 0x17, 0xfc, 0xe7 is necessary here,
417 // but that's what came out of the trace every single time.
419 command
[1] = access
| USBAT_CMD_WRITE_REGS
;
424 command
[6] = LSB_of(num_registers
*2);
425 command
[7] = MSB_of(num_registers
*2);
429 // Conditionally read or write blocks
430 command
[cmdlen
-8] = (direction
==DMA_TO_DEVICE
? 0x40 : 0xC0);
431 command
[cmdlen
-7] = access
|
432 (direction
==DMA_TO_DEVICE
?
433 USBAT_CMD_COND_WRITE_BLOCK
: USBAT_CMD_COND_READ_BLOCK
);
434 command
[cmdlen
-6] = data_reg
;
435 command
[cmdlen
-5] = status_reg
;
436 command
[cmdlen
-4] = timeout
;
437 command
[cmdlen
-3] = qualifier
;
438 command
[cmdlen
-2] = LSB_of(len
);
439 command
[cmdlen
-1] = MSB_of(len
);
441 result
= usbat_execute_command(us
, command
, cmdlen
);
443 if (result
!= USB_STOR_XFER_GOOD
)
444 return USB_STOR_TRANSPORT_ERROR
;
448 for (j
=0; j
<num_registers
; j
++) {
449 data
[j
<<1] = registers
[j
];
450 data
[1+(j
<<1)] = data_out
[j
];
453 result
= usbat_bulk_write(us
, data
, num_registers
*2);
454 if (result
!= USB_STOR_XFER_GOOD
)
455 return USB_STOR_TRANSPORT_ERROR
;
460 //US_DEBUGP("Transfer %s %d bytes, sg buffers %d\n",
461 // direction == DMA_TO_DEVICE ? "out" : "in",
464 result
= usb_stor_bulk_transfer_sg(us
,
465 pipe
, content
, len
, use_sg
, NULL
);
468 * If we get a stall on the bulk download, we'll retry
469 * the bulk download -- but not the SCSI command because
470 * in some sense the SCSI command is still 'active' and
471 * waiting for the data. Don't ask me why this should be;
472 * I'm only following what the Windoze driver did.
474 * Note that a stall for the test-and-read/write command means
475 * that the test failed. In this case we're testing to make
476 * sure that the device is error-free
477 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
478 * hypothesis is that the USBAT chip somehow knows what
479 * the device will accept, but doesn't give the device any
480 * data until all data is received. Thus, the device would
481 * still be waiting for the first byte of data if a stall
482 * occurs, even if the stall implies that some data was
486 if (result
== USB_STOR_XFER_SHORT
||
487 result
== USB_STOR_XFER_STALLED
) {
490 * If we're reading and we stalled, then clear
491 * the bulk output pipe only the first time.
494 if (direction
==DMA_FROM_DEVICE
&& i
==0) {
495 if (usb_stor_clear_halt(us
,
496 us
->send_bulk_pipe
) < 0)
497 return USB_STOR_TRANSPORT_ERROR
;
501 * Read status: is the device angry, or just busy?
504 result
= usbat_read(us
, USBAT_ATA
,
505 direction
==DMA_TO_DEVICE
?
506 USBAT_ATA_STATUS
: USBAT_ATA_ALTSTATUS
,
509 if (result
!=USB_STOR_XFER_GOOD
)
510 return USB_STOR_TRANSPORT_ERROR
;
511 if (*status
& 0x01) // check condition
512 return USB_STOR_TRANSPORT_FAILED
;
513 if (*status
& 0x20) // device fault
514 return USB_STOR_TRANSPORT_FAILED
;
516 US_DEBUGP("Redoing %s\n",
517 direction
==DMA_TO_DEVICE
? "write" : "read");
519 } else if (result
!= USB_STOR_XFER_GOOD
)
520 return USB_STOR_TRANSPORT_ERROR
;
522 return usbat_wait_not_busy(us
, minutes
);
526 US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
527 direction
==DMA_TO_DEVICE
? "Writing" : "Reading");
529 return USB_STOR_TRANSPORT_FAILED
;
533 * Write to multiple registers:
534 * Allows us to write specific data to any registers. The data to be written
535 * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN
536 * which gets sent through bulk out.
537 * Not designed for large transfers of data!
539 static int usbat_multiple_write(struct us_data
*us
,
540 unsigned char *registers
,
541 unsigned char *data_out
,
542 unsigned short num_registers
)
545 unsigned char *data
= us
->iobuf
;
546 unsigned char *command
= us
->iobuf
;
548 BUG_ON(num_registers
> US_IOBUF_SIZE
/2);
550 // Write to multiple registers, ATA access
552 command
[1] = USBAT_ATA
| USBAT_CMD_WRITE_REGS
;
560 // Number of bytes to be transferred (incl. addresses and data)
561 command
[6] = LSB_of(num_registers
*2);
562 command
[7] = MSB_of(num_registers
*2);
565 result
= usbat_execute_command(us
, command
, 8);
566 if (result
!= USB_STOR_XFER_GOOD
)
567 return USB_STOR_TRANSPORT_ERROR
;
569 // Create the reg/data, reg/data sequence
570 for (i
=0; i
<num_registers
; i
++) {
571 data
[i
<<1] = registers
[i
];
572 data
[1+(i
<<1)] = data_out
[i
];
576 result
= usbat_bulk_write(us
, data
, num_registers
*2);
577 if (result
!= USB_STOR_XFER_GOOD
)
578 return USB_STOR_TRANSPORT_ERROR
;
580 if (usbat_get_device_type(us
) == USBAT_DEV_HP8200
)
581 return usbat_wait_not_busy(us
, 0);
583 return USB_STOR_TRANSPORT_GOOD
;
587 * Conditionally read blocks from device:
588 * Allows us to read blocks from a specific data register, based upon the
589 * condition that a status register can be successfully masked with a status
590 * qualifier. If this condition is not initially met, the read will wait
591 * up until a maximum amount of time has elapsed, as specified by timeout.
592 * The read will start when the condition is met, otherwise the command aborts.
594 * The qualifier defined here is not the value that is masked, it defines
595 * conditions for the write to take place. The actual masked qualifier (and
596 * other related details) are defined beforehand with _set_shuttle_features().
598 static int usbat_read_blocks(struct us_data
*us
,
599 unsigned char *buffer
,
603 unsigned char *command
= us
->iobuf
;
606 command
[1] = USBAT_ATA
| USBAT_CMD_COND_READ_BLOCK
;
607 command
[2] = USBAT_ATA_DATA
;
608 command
[3] = USBAT_ATA_STATUS
;
609 command
[4] = 0xFD; // Timeout (ms);
610 command
[5] = USBAT_QUAL_FCQ
;
611 command
[6] = LSB_of(len
);
612 command
[7] = MSB_of(len
);
614 // Multiple block read setup command
615 result
= usbat_execute_command(us
, command
, 8);
616 if (result
!= USB_STOR_XFER_GOOD
)
617 return USB_STOR_TRANSPORT_FAILED
;
619 // Read the blocks we just asked for
620 result
= usbat_bulk_read(us
, buffer
, len
);
621 if (result
!= USB_STOR_XFER_GOOD
)
622 return USB_STOR_TRANSPORT_FAILED
;
624 return USB_STOR_TRANSPORT_GOOD
;
628 * Conditionally write blocks to device:
629 * Allows us to write blocks to a specific data register, based upon the
630 * condition that a status register can be successfully masked with a status
631 * qualifier. If this condition is not initially met, the write will wait
632 * up until a maximum amount of time has elapsed, as specified by timeout.
633 * The read will start when the condition is met, otherwise the command aborts.
635 * The qualifier defined here is not the value that is masked, it defines
636 * conditions for the write to take place. The actual masked qualifier (and
637 * other related details) are defined beforehand with _set_shuttle_features().
639 static int usbat_write_blocks(struct us_data
*us
,
640 unsigned char *buffer
,
644 unsigned char *command
= us
->iobuf
;
647 command
[1] = USBAT_ATA
| USBAT_CMD_COND_WRITE_BLOCK
;
648 command
[2] = USBAT_ATA_DATA
;
649 command
[3] = USBAT_ATA_STATUS
;
650 command
[4] = 0xFD; // Timeout (ms)
651 command
[5] = USBAT_QUAL_FCQ
;
652 command
[6] = LSB_of(len
);
653 command
[7] = MSB_of(len
);
655 // Multiple block write setup command
656 result
= usbat_execute_command(us
, command
, 8);
657 if (result
!= USB_STOR_XFER_GOOD
)
658 return USB_STOR_TRANSPORT_FAILED
;
661 result
= usbat_bulk_write(us
, buffer
, len
);
662 if (result
!= USB_STOR_XFER_GOOD
)
663 return USB_STOR_TRANSPORT_FAILED
;
665 return USB_STOR_TRANSPORT_GOOD
;
669 * Read the User IO register
671 static int usbat_read_user_io(struct us_data
*us
, unsigned char *data_flags
)
675 result
= usb_stor_ctrl_transfer(us
,
684 US_DEBUGP("usbat_read_user_io: UIO register reads %02X\n", (unsigned short) (*data_flags
));
690 * Write to the User IO register
692 static int usbat_write_user_io(struct us_data
*us
,
693 unsigned char enable_flags
,
694 unsigned char data_flags
)
696 return usb_stor_ctrl_transfer(us
,
700 short_pack(enable_flags
, data_flags
),
708 * Often needed on media change.
710 static int usbat_device_reset(struct us_data
*us
)
714 // Reset peripheral, enable peripheral control signals
715 // (bring reset signal up)
716 rc
= usbat_write_user_io(us
,
717 USBAT_UIO_DRVRST
| USBAT_UIO_OE1
| USBAT_UIO_OE0
,
718 USBAT_UIO_EPAD
| USBAT_UIO_1
);
719 if (rc
!= USB_STOR_XFER_GOOD
)
720 return USB_STOR_TRANSPORT_ERROR
;
722 // Enable peripheral control signals
723 // (bring reset signal down)
724 rc
= usbat_write_user_io(us
,
725 USBAT_UIO_OE1
| USBAT_UIO_OE0
,
726 USBAT_UIO_EPAD
| USBAT_UIO_1
);
727 if (rc
!= USB_STOR_XFER_GOOD
)
728 return USB_STOR_TRANSPORT_ERROR
;
730 return USB_STOR_TRANSPORT_GOOD
;
736 static int usbat_device_enable_cdt(struct us_data
*us
)
740 // Enable peripheral control signals and card detect
741 rc
= usbat_write_user_io(us
,
742 USBAT_UIO_ACKD
| USBAT_UIO_OE1
| USBAT_UIO_OE0
,
743 USBAT_UIO_EPAD
| USBAT_UIO_1
);
744 if (rc
!= USB_STOR_XFER_GOOD
)
745 return USB_STOR_TRANSPORT_ERROR
;
747 return USB_STOR_TRANSPORT_GOOD
;
751 * Determine if media is present.
753 static int usbat_flash_check_media_present(unsigned char *uio
)
755 if (*uio
& USBAT_UIO_UI0
) {
756 US_DEBUGP("usbat_flash_check_media_present: no media detected\n");
757 return USBAT_FLASH_MEDIA_NONE
;
760 return USBAT_FLASH_MEDIA_CF
;
764 * Determine if media has changed since last operation
766 static int usbat_flash_check_media_changed(unsigned char *uio
)
768 if (*uio
& USBAT_UIO_0
) {
769 US_DEBUGP("usbat_flash_check_media_changed: media change detected\n");
770 return USBAT_FLASH_MEDIA_CHANGED
;
773 return USBAT_FLASH_MEDIA_SAME
;
777 * Check for media change / no media and handle the situation appropriately
779 static int usbat_flash_check_media(struct us_data
*us
,
780 struct usbat_info
*info
)
783 unsigned char *uio
= us
->iobuf
;
785 rc
= usbat_read_user_io(us
, uio
);
786 if (rc
!= USB_STOR_XFER_GOOD
)
787 return USB_STOR_TRANSPORT_ERROR
;
789 // Check for media existence
790 rc
= usbat_flash_check_media_present(uio
);
791 if (rc
== USBAT_FLASH_MEDIA_NONE
) {
792 info
->sense_key
= 0x02;
793 info
->sense_asc
= 0x3A;
794 info
->sense_ascq
= 0x00;
795 return USB_STOR_TRANSPORT_FAILED
;
798 // Check for media change
799 rc
= usbat_flash_check_media_changed(uio
);
800 if (rc
== USBAT_FLASH_MEDIA_CHANGED
) {
802 // Reset and re-enable card detect
803 rc
= usbat_device_reset(us
);
804 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
806 rc
= usbat_device_enable_cdt(us
);
807 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
812 rc
= usbat_read_user_io(us
, uio
);
813 if (rc
!= USB_STOR_XFER_GOOD
)
814 return USB_STOR_TRANSPORT_ERROR
;
816 info
->sense_key
= UNIT_ATTENTION
;
817 info
->sense_asc
= 0x28;
818 info
->sense_ascq
= 0x00;
819 return USB_STOR_TRANSPORT_FAILED
;
822 return USB_STOR_TRANSPORT_GOOD
;
826 * Determine whether we are controlling a flash-based reader/writer,
827 * or a HP8200-based CD drive.
828 * Sets transport functions as appropriate.
830 static int usbat_identify_device(struct us_data
*us
,
831 struct usbat_info
*info
)
834 unsigned char status
;
837 return USB_STOR_TRANSPORT_ERROR
;
839 rc
= usbat_device_reset(us
);
840 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
845 * In attempt to distinguish between HP CDRW's and Flash readers, we now
846 * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
847 * readers), this command should fail with error. On ATAPI devices (i.e.
848 * CDROM drives), it should succeed.
850 rc
= usbat_write(us
, USBAT_ATA
, USBAT_ATA_CMD
, 0xA1);
851 if (rc
!= USB_STOR_XFER_GOOD
)
852 return USB_STOR_TRANSPORT_ERROR
;
854 rc
= usbat_get_status(us
, &status
);
855 if (rc
!= USB_STOR_XFER_GOOD
)
856 return USB_STOR_TRANSPORT_ERROR
;
858 // Check for error bit
860 // Device is a CompactFlash reader/writer
861 US_DEBUGP("usbat_identify_device: Detected Flash reader/writer\n");
862 info
->devicetype
= USBAT_DEV_FLASH
;
865 US_DEBUGP("usbat_identify_device: Detected HP8200 CDRW\n");
866 info
->devicetype
= USBAT_DEV_HP8200
;
869 return USB_STOR_TRANSPORT_GOOD
;
873 * Set the transport function based on the device type
875 static int usbat_set_transport(struct us_data
*us
,
876 struct usbat_info
*info
)
880 if (!info
->devicetype
) {
881 rc
= usbat_identify_device(us
, info
);
882 if (rc
!= USB_STOR_TRANSPORT_GOOD
) {
883 US_DEBUGP("usbat_set_transport: Could not identify device\n");
888 if (usbat_get_device_type(us
) == USBAT_DEV_HP8200
)
889 us
->transport
= usbat_hp8200e_transport
;
890 else if (usbat_get_device_type(us
) == USBAT_DEV_FLASH
)
891 us
->transport
= usbat_flash_transport
;
897 * Read the media capacity
899 static int usbat_flash_get_sector_count(struct us_data
*us
,
900 struct usbat_info
*info
)
902 unsigned char registers
[3] = {
907 unsigned char command
[3] = { 0x01, 0xA0, 0xEC };
908 unsigned char *reply
;
909 unsigned char status
;
913 return USB_STOR_TRANSPORT_ERROR
;
915 reply
= kmalloc(512, GFP_NOIO
);
917 return USB_STOR_TRANSPORT_ERROR
;
919 // ATAPI command : IDENTIFY DEVICE
920 rc
= usbat_multiple_write(us
, registers
, command
, 3);
921 if (rc
!= USB_STOR_XFER_GOOD
) {
922 US_DEBUGP("usbat_flash_get_sector_count: Gah! identify_device failed\n");
923 rc
= USB_STOR_TRANSPORT_ERROR
;
927 // Read device status
928 if (usbat_get_status(us
, &status
) != USB_STOR_XFER_GOOD
) {
929 rc
= USB_STOR_TRANSPORT_ERROR
;
935 // Read the device identification data
936 rc
= usbat_read_block(us
, reply
, 512);
937 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
940 info
->sectors
= ((u32
)(reply
[117]) << 24) |
941 ((u32
)(reply
[116]) << 16) |
942 ((u32
)(reply
[115]) << 8) |
943 ((u32
)(reply
[114]) );
945 rc
= USB_STOR_TRANSPORT_GOOD
;
953 * Read data from device
955 static int usbat_flash_read_data(struct us_data
*us
,
956 struct usbat_info
*info
,
960 unsigned char registers
[7] = {
969 unsigned char command
[7];
970 unsigned char *buffer
;
971 unsigned char thistime
;
972 unsigned int totallen
, alloclen
;
974 unsigned int sg_idx
= 0, sg_offset
= 0;
976 result
= usbat_flash_check_media(us
, info
);
977 if (result
!= USB_STOR_TRANSPORT_GOOD
)
980 // we're working in LBA mode. according to the ATA spec,
981 // we can support up to 28-bit addressing. I don't know if Jumpshot
982 // supports beyond 24-bit addressing. It's kind of hard to test
983 // since it requires > 8GB CF card.
985 if (sector
> 0x0FFFFFFF)
986 return USB_STOR_TRANSPORT_ERROR
;
988 totallen
= sectors
* info
->ssize
;
990 // Since we don't read more than 64 KB at a time, we have to create
991 // a bounce buffer and move the data a piece at a time between the
992 // bounce buffer and the actual transfer buffer.
994 alloclen
= min(totallen
, 65536u);
995 buffer
= kmalloc(alloclen
, GFP_NOIO
);
997 return USB_STOR_TRANSPORT_ERROR
;
1000 // loop, never allocate or transfer more than 64k at once
1001 // (min(128k, 255*info->ssize) is the real limit)
1002 len
= min(totallen
, alloclen
);
1003 thistime
= (len
/ info
->ssize
) & 0xff;
1005 // ATAPI command 0x20 (READ SECTORS)
1006 usbat_pack_atapi_sector_cmd(command
, thistime
, sector
, 0x20);
1008 // Write/execute ATAPI read command
1009 result
= usbat_multiple_write(us
, registers
, command
, 7);
1010 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1013 // Read the data we just requested
1014 result
= usbat_read_blocks(us
, buffer
, len
);
1015 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1018 US_DEBUGP("usbat_flash_read_data: %d bytes\n", len
);
1020 // Store the data in the transfer buffer
1021 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
1022 &sg_idx
, &sg_offset
, TO_XFER_BUF
);
1026 } while (totallen
> 0);
1029 return USB_STOR_TRANSPORT_GOOD
;
1033 return USB_STOR_TRANSPORT_ERROR
;
1037 * Write data to device
1039 static int usbat_flash_write_data(struct us_data
*us
,
1040 struct usbat_info
*info
,
1044 unsigned char registers
[7] = {
1053 unsigned char command
[7];
1054 unsigned char *buffer
;
1055 unsigned char thistime
;
1056 unsigned int totallen
, alloclen
;
1058 unsigned int sg_idx
= 0, sg_offset
= 0;
1060 result
= usbat_flash_check_media(us
, info
);
1061 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1064 // we're working in LBA mode. according to the ATA spec,
1065 // we can support up to 28-bit addressing. I don't know if Jumpshot
1066 // supports beyond 24-bit addressing. It's kind of hard to test
1067 // since it requires > 8GB CF card.
1069 if (sector
> 0x0FFFFFFF)
1070 return USB_STOR_TRANSPORT_ERROR
;
1072 totallen
= sectors
* info
->ssize
;
1074 // Since we don't write more than 64 KB at a time, we have to create
1075 // a bounce buffer and move the data a piece at a time between the
1076 // bounce buffer and the actual transfer buffer.
1078 alloclen
= min(totallen
, 65536u);
1079 buffer
= kmalloc(alloclen
, GFP_NOIO
);
1081 return USB_STOR_TRANSPORT_ERROR
;
1084 // loop, never allocate or transfer more than 64k at once
1085 // (min(128k, 255*info->ssize) is the real limit)
1086 len
= min(totallen
, alloclen
);
1087 thistime
= (len
/ info
->ssize
) & 0xff;
1089 // Get the data from the transfer buffer
1090 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
1091 &sg_idx
, &sg_offset
, FROM_XFER_BUF
);
1093 // ATAPI command 0x30 (WRITE SECTORS)
1094 usbat_pack_atapi_sector_cmd(command
, thistime
, sector
, 0x30);
1096 // Write/execute ATAPI write command
1097 result
= usbat_multiple_write(us
, registers
, command
, 7);
1098 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1102 result
= usbat_write_blocks(us
, buffer
, len
);
1103 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1108 } while (totallen
> 0);
1115 return USB_STOR_TRANSPORT_ERROR
;
1119 * Squeeze a potentially huge (> 65535 byte) read10 command into
1120 * a little ( <= 65535 byte) ATAPI pipe
1122 static int usbat_hp8200e_handle_read10(struct us_data
*us
,
1123 unsigned char *registers
,
1124 unsigned char *data
,
1125 struct scsi_cmnd
*srb
)
1127 int result
= USB_STOR_TRANSPORT_GOOD
;
1128 unsigned char *buffer
;
1130 unsigned int sector
;
1131 unsigned int sg_segment
= 0;
1132 unsigned int sg_offset
= 0;
1134 US_DEBUGP("handle_read10: transfersize %d\n",
1137 if (srb
->request_bufflen
< 0x10000) {
1139 result
= usbat_hp8200e_rw_block_test(us
, USBAT_ATA
,
1140 registers
, data
, 19,
1141 USBAT_ATA_DATA
, USBAT_ATA_STATUS
, 0xFD,
1142 (USBAT_QUAL_FCQ
| USBAT_QUAL_ALQ
),
1144 srb
->request_buffer
,
1145 srb
->request_bufflen
, srb
->use_sg
, 1);
1151 * Since we're requesting more data than we can handle in
1152 * a single read command (max is 64k-1), we will perform
1153 * multiple reads, but each read must be in multiples of
1154 * a sector. Luckily the sector size is in srb->transfersize
1155 * (see linux/drivers/scsi/sr.c).
1158 if (data
[7+0] == GPCMD_READ_CD
) {
1159 len
= short_pack(data
[7+9], data
[7+8]);
1162 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len
);
1163 srb
->transfersize
= srb
->request_bufflen
/len
;
1166 if (!srb
->transfersize
) {
1167 srb
->transfersize
= 2048; /* A guess */
1168 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
1172 // Since we only read in one block at a time, we have to create
1173 // a bounce buffer and move the data a piece at a time between the
1174 // bounce buffer and the actual transfer buffer.
1176 len
= (65535/srb
->transfersize
) * srb
->transfersize
;
1177 US_DEBUGP("Max read is %d bytes\n", len
);
1178 len
= min(len
, srb
->request_bufflen
);
1179 buffer
= kmalloc(len
, GFP_NOIO
);
1180 if (buffer
== NULL
) // bloody hell!
1181 return USB_STOR_TRANSPORT_FAILED
;
1182 sector
= short_pack(data
[7+3], data
[7+2]);
1184 sector
|= short_pack(data
[7+5], data
[7+4]);
1187 sg_segment
= 0; // for keeping track of where we are in
1188 sg_offset
= 0; // the scatter/gather list
1190 while (transferred
!= srb
->request_bufflen
) {
1192 if (len
> srb
->request_bufflen
- transferred
)
1193 len
= srb
->request_bufflen
- transferred
;
1195 data
[3] = len
&0xFF; // (cylL) = expected length (L)
1196 data
[4] = (len
>>8)&0xFF; // (cylH) = expected length (H)
1198 // Fix up the SCSI command sector and num sectors
1200 data
[7+2] = MSB_of(sector
>>16); // SCSI command sector
1201 data
[7+3] = LSB_of(sector
>>16);
1202 data
[7+4] = MSB_of(sector
&0xFFFF);
1203 data
[7+5] = LSB_of(sector
&0xFFFF);
1204 if (data
[7+0] == GPCMD_READ_CD
)
1206 data
[7+7] = MSB_of(len
/ srb
->transfersize
); // SCSI command
1207 data
[7+8] = LSB_of(len
/ srb
->transfersize
); // num sectors
1209 result
= usbat_hp8200e_rw_block_test(us
, USBAT_ATA
,
1210 registers
, data
, 19,
1211 USBAT_ATA_DATA
, USBAT_ATA_STATUS
, 0xFD,
1212 (USBAT_QUAL_FCQ
| USBAT_QUAL_ALQ
),
1217 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1220 // Store the data in the transfer buffer
1221 usb_stor_access_xfer_buf(buffer
, len
, srb
,
1222 &sg_segment
, &sg_offset
, TO_XFER_BUF
);
1224 // Update the amount transferred and the sector number
1227 sector
+= len
/ srb
->transfersize
;
1229 } // while transferred != srb->request_bufflen
1235 static int usbat_select_and_test_registers(struct us_data
*us
)
1238 unsigned char *status
= us
->iobuf
;
1240 // try device = master, then device = slave.
1241 for (selector
= 0xA0; selector
<= 0xB0; selector
+= 0x10) {
1242 if (usbat_write(us
, USBAT_ATA
, USBAT_ATA_DEVICE
, selector
) !=
1244 return USB_STOR_TRANSPORT_ERROR
;
1246 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_STATUS
, status
) !=
1248 return USB_STOR_TRANSPORT_ERROR
;
1250 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_DEVICE
, status
) !=
1252 return USB_STOR_TRANSPORT_ERROR
;
1254 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, status
) !=
1256 return USB_STOR_TRANSPORT_ERROR
;
1258 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_HI
, status
) !=
1260 return USB_STOR_TRANSPORT_ERROR
;
1262 if (usbat_write(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, 0x55) !=
1264 return USB_STOR_TRANSPORT_ERROR
;
1266 if (usbat_write(us
, USBAT_ATA
, USBAT_ATA_LBA_HI
, 0xAA) !=
1268 return USB_STOR_TRANSPORT_ERROR
;
1270 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, status
) !=
1272 return USB_STOR_TRANSPORT_ERROR
;
1274 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, status
) !=
1276 return USB_STOR_TRANSPORT_ERROR
;
1279 return USB_STOR_TRANSPORT_GOOD
;
1283 * Initialize the USBAT processor and the storage device
1285 int init_usbat(struct us_data
*us
)
1288 struct usbat_info
*info
;
1289 unsigned char subcountH
= USBAT_ATA_LBA_HI
;
1290 unsigned char subcountL
= USBAT_ATA_LBA_ME
;
1291 unsigned char *status
= us
->iobuf
;
1293 us
->extra
= kmalloc(sizeof(struct usbat_info
), GFP_NOIO
);
1295 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1298 memset(us
->extra
, 0, sizeof(struct usbat_info
));
1299 info
= (struct usbat_info
*) (us
->extra
);
1301 // Enable peripheral control signals
1302 rc
= usbat_write_user_io(us
,
1303 USBAT_UIO_OE1
| USBAT_UIO_OE0
,
1304 USBAT_UIO_EPAD
| USBAT_UIO_1
);
1305 if (rc
!= USB_STOR_XFER_GOOD
)
1306 return USB_STOR_TRANSPORT_ERROR
;
1308 US_DEBUGP("INIT 1\n");
1312 rc
= usbat_read_user_io(us
, status
);
1313 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1316 US_DEBUGP("INIT 2\n");
1318 rc
= usbat_read_user_io(us
, status
);
1319 if (rc
!= USB_STOR_XFER_GOOD
)
1320 return USB_STOR_TRANSPORT_ERROR
;
1322 rc
= usbat_read_user_io(us
, status
);
1323 if (rc
!= USB_STOR_XFER_GOOD
)
1324 return USB_STOR_TRANSPORT_ERROR
;
1326 US_DEBUGP("INIT 3\n");
1328 rc
= usbat_select_and_test_registers(us
);
1329 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1332 US_DEBUGP("INIT 4\n");
1334 rc
= usbat_read_user_io(us
, status
);
1335 if (rc
!= USB_STOR_XFER_GOOD
)
1336 return USB_STOR_TRANSPORT_ERROR
;
1338 US_DEBUGP("INIT 5\n");
1340 // Enable peripheral control signals and card detect
1341 rc
= usbat_device_enable_cdt(us
);
1342 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1345 US_DEBUGP("INIT 6\n");
1347 rc
= usbat_read_user_io(us
, status
);
1348 if (rc
!= USB_STOR_XFER_GOOD
)
1349 return USB_STOR_TRANSPORT_ERROR
;
1351 US_DEBUGP("INIT 7\n");
1355 rc
= usbat_read_user_io(us
, status
);
1356 if (rc
!= USB_STOR_XFER_GOOD
)
1357 return USB_STOR_TRANSPORT_ERROR
;
1359 US_DEBUGP("INIT 8\n");
1361 rc
= usbat_select_and_test_registers(us
);
1362 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1365 US_DEBUGP("INIT 9\n");
1367 // At this point, we need to detect which device we are using
1368 if (usbat_set_transport(us
, info
))
1369 return USB_STOR_TRANSPORT_ERROR
;
1371 US_DEBUGP("INIT 10\n");
1373 if (usbat_get_device_type(us
) == USBAT_DEV_FLASH
) {
1377 rc
= usbat_set_shuttle_features(us
, (USBAT_FEAT_ETEN
| USBAT_FEAT_ET2
| USBAT_FEAT_ET1
),
1378 0x00, 0x88, 0x08, subcountH
, subcountL
);
1379 if (rc
!= USB_STOR_XFER_GOOD
)
1380 return USB_STOR_TRANSPORT_ERROR
;
1382 US_DEBUGP("INIT 11\n");
1384 return USB_STOR_TRANSPORT_GOOD
;
1388 * Transport for the HP 8200e
1390 static int usbat_hp8200e_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
1393 unsigned char *status
= us
->iobuf
;
1394 unsigned char registers
[32];
1395 unsigned char data
[32];
1400 len
= srb
->request_bufflen
;
1402 /* Send A0 (ATA PACKET COMMAND).
1403 Note: I guess we're never going to get any of the ATA
1404 commands... just ATA Packet Commands.
1407 registers
[0] = USBAT_ATA_FEATURES
;
1408 registers
[1] = USBAT_ATA_SECCNT
;
1409 registers
[2] = USBAT_ATA_SECNUM
;
1410 registers
[3] = USBAT_ATA_LBA_ME
;
1411 registers
[4] = USBAT_ATA_LBA_HI
;
1412 registers
[5] = USBAT_ATA_DEVICE
;
1413 registers
[6] = USBAT_ATA_CMD
;
1417 data
[3] = len
&0xFF; // (cylL) = expected length (L)
1418 data
[4] = (len
>>8)&0xFF; // (cylH) = expected length (H)
1419 data
[5] = 0xB0; // (device sel) = slave
1420 data
[6] = 0xA0; // (command) = ATA PACKET COMMAND
1422 for (i
=7; i
<19; i
++) {
1423 registers
[i
] = 0x10;
1424 data
[i
] = (i
-7 >= srb
->cmd_len
) ? 0 : srb
->cmnd
[i
-7];
1427 result
= usbat_get_status(us
, status
);
1428 US_DEBUGP("Status = %02X\n", *status
);
1429 if (result
!= USB_STOR_XFER_GOOD
)
1430 return USB_STOR_TRANSPORT_ERROR
;
1431 if (srb
->cmnd
[0] == TEST_UNIT_READY
)
1434 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
1436 result
= usbat_hp8200e_rw_block_test(us
, USBAT_ATA
,
1437 registers
, data
, 19,
1438 USBAT_ATA_DATA
, USBAT_ATA_STATUS
, 0xFD,
1439 (USBAT_QUAL_FCQ
| USBAT_QUAL_ALQ
),
1441 srb
->request_buffer
,
1442 len
, srb
->use_sg
, 10);
1444 if (result
== USB_STOR_TRANSPORT_GOOD
) {
1446 US_DEBUGP("Wrote %08X bytes\n", transferred
);
1451 } else if (srb
->cmnd
[0] == READ_10
||
1452 srb
->cmnd
[0] == GPCMD_READ_CD
) {
1454 return usbat_hp8200e_handle_read10(us
, registers
, data
, srb
);
1459 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1461 return USB_STOR_TRANSPORT_ERROR
;
1464 if ( (result
= usbat_multiple_write(us
,
1465 registers
, data
, 7)) != USB_STOR_TRANSPORT_GOOD
) {
1469 // Write the 12-byte command header.
1471 // If the command is BLANK then set the timer for 75 minutes.
1472 // Otherwise set it for 10 minutes.
1474 // NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1475 // AT SPEED 4 IS UNRELIABLE!!!
1477 if ( (result
= usbat_write_block(us
,
1478 USBAT_ATA
, srb
->cmnd
, 12,
1479 srb
->cmnd
[0]==GPCMD_BLANK
? 75 : 10)) !=
1480 USB_STOR_TRANSPORT_GOOD
) {
1484 // If there is response data to be read in
1487 if (len
!= 0 && (srb
->sc_data_direction
== DMA_FROM_DEVICE
)) {
1489 // How many bytes to read in? Check cylL register
1491 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_ME
, status
) !=
1492 USB_STOR_XFER_GOOD
) {
1493 return USB_STOR_TRANSPORT_ERROR
;
1496 if (len
> 0xFF) { // need to read cylH also
1498 if (usbat_read(us
, USBAT_ATA
, USBAT_ATA_LBA_HI
, status
) !=
1499 USB_STOR_XFER_GOOD
) {
1500 return USB_STOR_TRANSPORT_ERROR
;
1502 len
+= ((unsigned int) *status
)<<8;
1508 result
= usbat_read_block(us
, srb
->request_buffer
, len
);
1510 /* Debug-print the first 32 bytes of the transfer */
1514 for (i
=0; i
<len
&& i
<32; i
++) {
1515 sprintf(string
+strlen(string
), "%02X ",
1516 ((unsigned char *)srb
->request_buffer
)[i
]);
1518 US_DEBUGP("%s\n", string
);
1523 US_DEBUGP("%s\n", string
);
1531 * Transport for USBAT02-based CompactFlash and similar storage devices
1533 static int usbat_flash_transport(struct scsi_cmnd
* srb
, struct us_data
*us
)
1536 struct usbat_info
*info
= (struct usbat_info
*) (us
->extra
);
1537 unsigned long block
, blocks
;
1538 unsigned char *ptr
= us
->iobuf
;
1539 static unsigned char inquiry_response
[36] = {
1540 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1543 if (srb
->cmnd
[0] == INQUIRY
) {
1544 US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1545 memcpy(ptr
, inquiry_response
, sizeof(inquiry_response
));
1546 fill_inquiry_response(us
, ptr
, 36);
1547 return USB_STOR_TRANSPORT_GOOD
;
1550 if (srb
->cmnd
[0] == READ_CAPACITY
) {
1551 rc
= usbat_flash_check_media(us
, info
);
1552 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1555 rc
= usbat_flash_get_sector_count(us
, info
);
1556 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1559 info
->ssize
= 0x200; // hard coded 512 byte sectors as per ATA spec
1560 US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1561 info
->sectors
, info
->ssize
);
1564 // note: must return the sector number of the last sector,
1565 // *not* the total number of sectors
1566 ((__be32
*) ptr
)[0] = cpu_to_be32(info
->sectors
- 1);
1567 ((__be32
*) ptr
)[1] = cpu_to_be32(info
->ssize
);
1568 usb_stor_set_xfer_buf(ptr
, 8, srb
);
1570 return USB_STOR_TRANSPORT_GOOD
;
1573 if (srb
->cmnd
[0] == MODE_SELECT_10
) {
1574 US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
1575 return USB_STOR_TRANSPORT_ERROR
;
1578 if (srb
->cmnd
[0] == READ_10
) {
1579 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
1580 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
1582 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
1584 US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block
, blocks
);
1585 return usbat_flash_read_data(us
, info
, block
, blocks
);
1588 if (srb
->cmnd
[0] == READ_12
) {
1589 // I don't think we'll ever see a READ_12 but support it anyway...
1590 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
1591 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
1593 blocks
= ((u32
)(srb
->cmnd
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
1594 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
1596 US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block
, blocks
);
1597 return usbat_flash_read_data(us
, info
, block
, blocks
);
1600 if (srb
->cmnd
[0] == WRITE_10
) {
1601 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
1602 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
1604 blocks
= ((u32
)(srb
->cmnd
[7]) << 8) | ((u32
)(srb
->cmnd
[8]));
1606 US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block
, blocks
);
1607 return usbat_flash_write_data(us
, info
, block
, blocks
);
1610 if (srb
->cmnd
[0] == WRITE_12
) {
1611 // I don't think we'll ever see a WRITE_12 but support it anyway...
1612 block
= ((u32
)(srb
->cmnd
[2]) << 24) | ((u32
)(srb
->cmnd
[3]) << 16) |
1613 ((u32
)(srb
->cmnd
[4]) << 8) | ((u32
)(srb
->cmnd
[5]));
1615 blocks
= ((u32
)(srb
->cmnd
[6]) << 24) | ((u32
)(srb
->cmnd
[7]) << 16) |
1616 ((u32
)(srb
->cmnd
[8]) << 8) | ((u32
)(srb
->cmnd
[9]));
1618 US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block
, blocks
);
1619 return usbat_flash_write_data(us
, info
, block
, blocks
);
1623 if (srb
->cmnd
[0] == TEST_UNIT_READY
) {
1624 US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
1626 rc
= usbat_flash_check_media(us
, info
);
1627 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1630 return usbat_check_status(us
);
1633 if (srb
->cmnd
[0] == REQUEST_SENSE
) {
1634 US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
1638 ptr
[2] = info
->sense_key
;
1640 ptr
[12] = info
->sense_asc
;
1641 ptr
[13] = info
->sense_ascq
;
1642 usb_stor_set_xfer_buf(ptr
, 18, srb
);
1644 return USB_STOR_TRANSPORT_GOOD
;
1647 if (srb
->cmnd
[0] == ALLOW_MEDIUM_REMOVAL
) {
1648 // sure. whatever. not like we can stop the user from popping
1649 // the media out of the device (no locking doors, etc)
1650 return USB_STOR_TRANSPORT_GOOD
;
1653 US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1654 srb
->cmnd
[0], srb
->cmnd
[0]);
1655 info
->sense_key
= 0x05;
1656 info
->sense_asc
= 0x20;
1657 info
->sense_ascq
= 0x00;
1658 return USB_STOR_TRANSPORT_FAILED
;
1662 * Default transport function. Attempts to detect which transport function
1663 * should be called, makes it the new default, and calls it.
1665 * This function should never be called. Our usbat_init() function detects the
1666 * device type and changes the us->transport ptr to the transport function
1667 * relevant to the device.
1668 * However, we'll support this impossible(?) case anyway.
1670 int usbat_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
1672 struct usbat_info
*info
= (struct usbat_info
*) (us
->extra
);
1674 if (usbat_set_transport(us
, info
))
1675 return USB_STOR_TRANSPORT_ERROR
;
1677 return us
->transport(srb
, us
);