[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / usb / storage / shuttle_usbat.c
blobf3b60288696cfbc1162418368892773959562dbe
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
26 * translation.
28 * See the Kconfig help text for a list of devices known to be supported by
29 * this driver.
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
34 * later version.
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>
54 #include "usb.h"
55 #include "transport.h"
56 #include "protocol.h"
57 #include "debug.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)
77 buf[0] = 0;
78 buf[1] = thistime;
79 buf[2] = sector & 0xFF;
80 buf[3] = (sector >> 8) & 0xFF;
81 buf[4] = (sector >> 16) & 0xFF;
82 buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
83 buf[6] = cmd;
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,
98 unsigned char access,
99 unsigned char reg,
100 unsigned char *content)
102 return usb_stor_ctrl_transfer(us,
103 us->recv_ctrl_pipe,
104 access | USBAT_CMD_READ_REG,
105 0xC0,
106 (u16)reg,
108 content,
113 * Write to a register on the device
115 static int usbat_write(struct us_data *us,
116 unsigned char access,
117 unsigned char reg,
118 unsigned char content)
120 return usb_stor_ctrl_transfer(us,
121 us->send_ctrl_pipe,
122 access | USBAT_CMD_WRITE_REG,
123 0x40,
124 short_pack(reg, content),
126 NULL,
131 * Convenience function to perform a bulk read
133 static int usbat_bulk_read(struct us_data *us,
134 unsigned char *data,
135 unsigned int len)
137 if (len == 0)
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,
148 unsigned char *data,
149 unsigned int len)
151 if (len == 0)
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
161 * to be executed.
163 static int usbat_execute_command(struct us_data *us,
164 unsigned char *commands,
165 unsigned int len)
167 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
168 USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
169 commands, len);
173 * Read the status register
175 static int usbat_get_status(struct us_data *us, unsigned char *status)
177 int rc;
178 rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
180 US_DEBUGP("usbat_get_status: 0x%02X\n", (unsigned short) (*status));
181 return rc;
185 * Check the device status
187 static int usbat_check_status(struct us_data *us)
189 unsigned char *reply = us->iobuf;
190 int rc;
192 if (!us)
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;
222 command[0] = 0x40;
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
237 // place.
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
247 // transferred.
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)
260 int i;
261 int result;
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
267 * minutes!
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;
288 if (i<500)
289 msleep(10); // 5 seconds
290 else if (i<700)
291 msleep(50); // 10 seconds
292 else if (i<1200)
293 msleep(100); // 50 seconds
294 else
295 msleep(1000); // X minutes
298 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
299 minutes);
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,
308 unsigned short len)
310 int result;
311 unsigned char *command = us->iobuf;
313 if (!len)
314 return USB_STOR_TRANSPORT_GOOD;
316 command[0] = 0xC0;
317 command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
318 command[2] = USBAT_ATA_DATA;
319 command[3] = 0;
320 command[4] = 0;
321 command[5] = 0;
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,
340 unsigned short len,
341 int minutes)
343 int result;
344 unsigned char *command = us->iobuf;
346 if (!len)
347 return USB_STOR_TRANSPORT_GOOD;
349 command[0] = 0x40;
350 command[1] = access | USBAT_CMD_WRITE_BLOCK;
351 command[2] = USBAT_ATA_DATA;
352 command[3] = 0;
353 command[4] = 0;
354 command[5] = 0;
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,
382 int direction,
383 unsigned char *content,
384 unsigned short len,
385 int use_sg,
386 int minutes)
388 int result;
389 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
390 us->recv_bulk_pipe : us->send_bulk_pipe;
392 unsigned char *command = us->iobuf;
393 int i, j;
394 int cmdlen;
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.
413 if (i==0) {
414 cmdlen = 16;
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.
418 command[0] = 0x40;
419 command[1] = access | USBAT_CMD_WRITE_REGS;
420 command[2] = 0x07;
421 command[3] = 0x17;
422 command[4] = 0xFC;
423 command[5] = 0xE7;
424 command[6] = LSB_of(num_registers*2);
425 command[7] = MSB_of(num_registers*2);
426 } else
427 cmdlen = 8;
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;
446 if (i==0) {
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",
462 // len, use_sg);
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
483 * transferred.
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,
507 status);
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;
521 else
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)
544 int i, result;
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
551 command[0] = 0x40;
552 command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
554 // No relevance
555 command[2] = 0;
556 command[3] = 0;
557 command[4] = 0;
558 command[5] = 0;
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);
564 // The setup command
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];
575 // Send the data
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);
582 else
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,
600 int len)
602 int result;
603 unsigned char *command = us->iobuf;
605 command[0] = 0xC0;
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,
641 int len)
643 int result;
644 unsigned char *command = us->iobuf;
646 command[0] = 0x40;
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;
660 // Write the data
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)
673 int result;
675 result = usb_stor_ctrl_transfer(us,
676 us->recv_ctrl_pipe,
677 USBAT_CMD_UIO,
678 0xC0,
681 data_flags,
682 USBAT_UIO_READ);
684 US_DEBUGP("usbat_read_user_io: UIO register reads %02X\n", (unsigned short) (*data_flags));
686 return result;
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,
697 us->send_ctrl_pipe,
698 USBAT_CMD_UIO,
699 0x40,
700 short_pack(enable_flags, data_flags),
702 NULL,
703 USBAT_UIO_WRITE);
707 * Reset the device
708 * Often needed on media change.
710 static int usbat_device_reset(struct us_data *us)
712 int rc;
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;
734 * Enable card detect
736 static int usbat_device_enable_cdt(struct us_data *us)
738 int rc;
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)
782 int rc;
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)
805 return rc;
806 rc = usbat_device_enable_cdt(us);
807 if (rc != USB_STOR_TRANSPORT_GOOD)
808 return rc;
810 msleep(50);
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)
833 int rc;
834 unsigned char status;
836 if (!us || !info)
837 return USB_STOR_TRANSPORT_ERROR;
839 rc = usbat_device_reset(us);
840 if (rc != USB_STOR_TRANSPORT_GOOD)
841 return rc;
844 * By examining the device signature after a reset, we can identify
845 * whether the device supports the ATAPI packet interface.
846 * The flash-devices do not support this, whereas the HP CDRW's obviously
847 * do.
849 * This method is not ideal, but works because no other devices have been
850 * produced based on the USBAT/USBAT02.
852 * Section 9.1 of the ATAPI-4 spec states (amongst other things) that
853 * after a device reset, a Cylinder low of 0x14 indicates that the device
854 * does support packet commands.
856 rc = usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, &status);
857 if (rc != USB_STOR_XFER_GOOD)
858 return USB_STOR_TRANSPORT_ERROR;
860 US_DEBUGP("usbat_identify_device: Cylinder low is %02X\n", status);
862 if (status == 0x14) {
863 // Device is HP 8200
864 US_DEBUGP("usbat_identify_device: Detected HP8200 CDRW\n");
865 info->devicetype = USBAT_DEV_HP8200;
866 } else {
867 // Device is a CompactFlash reader/writer
868 US_DEBUGP("usbat_identify_device: Detected Flash reader/writer\n");
869 info->devicetype = USBAT_DEV_FLASH;
872 return USB_STOR_TRANSPORT_GOOD;
876 * Set the transport function based on the device type
878 static int usbat_set_transport(struct us_data *us,
879 struct usbat_info *info)
881 int rc;
883 if (!info->devicetype) {
884 rc = usbat_identify_device(us, info);
885 if (rc != USB_STOR_TRANSPORT_GOOD) {
886 US_DEBUGP("usbat_set_transport: Could not identify device\n");
887 return 1;
891 if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
892 us->transport = usbat_hp8200e_transport;
893 else if (usbat_get_device_type(us) == USBAT_DEV_FLASH)
894 us->transport = usbat_flash_transport;
896 return 0;
900 * Read the media capacity
902 static int usbat_flash_get_sector_count(struct us_data *us,
903 struct usbat_info *info)
905 unsigned char registers[3] = {
906 USBAT_ATA_SECCNT,
907 USBAT_ATA_DEVICE,
908 USBAT_ATA_CMD,
910 unsigned char command[3] = { 0x01, 0xA0, 0xEC };
911 unsigned char *reply;
912 unsigned char status;
913 int rc;
915 if (!us || !info)
916 return USB_STOR_TRANSPORT_ERROR;
918 reply = kmalloc(512, GFP_NOIO);
919 if (!reply)
920 return USB_STOR_TRANSPORT_ERROR;
922 // ATAPI command : IDENTIFY DEVICE
923 rc = usbat_multiple_write(us, registers, command, 3);
924 if (rc != USB_STOR_XFER_GOOD) {
925 US_DEBUGP("usbat_flash_get_sector_count: Gah! identify_device failed\n");
926 rc = USB_STOR_TRANSPORT_ERROR;
927 goto leave;
930 // Read device status
931 if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
932 rc = USB_STOR_TRANSPORT_ERROR;
933 goto leave;
936 msleep(100);
938 // Read the device identification data
939 rc = usbat_read_block(us, reply, 512);
940 if (rc != USB_STOR_TRANSPORT_GOOD)
941 goto leave;
943 info->sectors = ((u32)(reply[117]) << 24) |
944 ((u32)(reply[116]) << 16) |
945 ((u32)(reply[115]) << 8) |
946 ((u32)(reply[114]) );
948 rc = USB_STOR_TRANSPORT_GOOD;
950 leave:
951 kfree(reply);
952 return rc;
956 * Read data from device
958 static int usbat_flash_read_data(struct us_data *us,
959 struct usbat_info *info,
960 u32 sector,
961 u32 sectors)
963 unsigned char registers[7] = {
964 USBAT_ATA_FEATURES,
965 USBAT_ATA_SECCNT,
966 USBAT_ATA_SECNUM,
967 USBAT_ATA_LBA_ME,
968 USBAT_ATA_LBA_HI,
969 USBAT_ATA_DEVICE,
970 USBAT_ATA_STATUS,
972 unsigned char command[7];
973 unsigned char *buffer;
974 unsigned char thistime;
975 unsigned int totallen, alloclen;
976 int len, result;
977 unsigned int sg_idx = 0, sg_offset = 0;
979 result = usbat_flash_check_media(us, info);
980 if (result != USB_STOR_TRANSPORT_GOOD)
981 return result;
983 // we're working in LBA mode. according to the ATA spec,
984 // we can support up to 28-bit addressing. I don't know if Jumpshot
985 // supports beyond 24-bit addressing. It's kind of hard to test
986 // since it requires > 8GB CF card.
988 if (sector > 0x0FFFFFFF)
989 return USB_STOR_TRANSPORT_ERROR;
991 totallen = sectors * info->ssize;
993 // Since we don't read more than 64 KB at a time, we have to create
994 // a bounce buffer and move the data a piece at a time between the
995 // bounce buffer and the actual transfer buffer.
997 alloclen = min(totallen, 65536u);
998 buffer = kmalloc(alloclen, GFP_NOIO);
999 if (buffer == NULL)
1000 return USB_STOR_TRANSPORT_ERROR;
1002 do {
1003 // loop, never allocate or transfer more than 64k at once
1004 // (min(128k, 255*info->ssize) is the real limit)
1005 len = min(totallen, alloclen);
1006 thistime = (len / info->ssize) & 0xff;
1008 // ATAPI command 0x20 (READ SECTORS)
1009 usbat_pack_atapi_sector_cmd(command, thistime, sector, 0x20);
1011 // Write/execute ATAPI read command
1012 result = usbat_multiple_write(us, registers, command, 7);
1013 if (result != USB_STOR_TRANSPORT_GOOD)
1014 goto leave;
1016 // Read the data we just requested
1017 result = usbat_read_blocks(us, buffer, len);
1018 if (result != USB_STOR_TRANSPORT_GOOD)
1019 goto leave;
1021 US_DEBUGP("usbat_flash_read_data: %d bytes\n", len);
1023 // Store the data in the transfer buffer
1024 usb_stor_access_xfer_buf(buffer, len, us->srb,
1025 &sg_idx, &sg_offset, TO_XFER_BUF);
1027 sector += thistime;
1028 totallen -= len;
1029 } while (totallen > 0);
1031 kfree(buffer);
1032 return USB_STOR_TRANSPORT_GOOD;
1034 leave:
1035 kfree(buffer);
1036 return USB_STOR_TRANSPORT_ERROR;
1040 * Write data to device
1042 static int usbat_flash_write_data(struct us_data *us,
1043 struct usbat_info *info,
1044 u32 sector,
1045 u32 sectors)
1047 unsigned char registers[7] = {
1048 USBAT_ATA_FEATURES,
1049 USBAT_ATA_SECCNT,
1050 USBAT_ATA_SECNUM,
1051 USBAT_ATA_LBA_ME,
1052 USBAT_ATA_LBA_HI,
1053 USBAT_ATA_DEVICE,
1054 USBAT_ATA_STATUS,
1056 unsigned char command[7];
1057 unsigned char *buffer;
1058 unsigned char thistime;
1059 unsigned int totallen, alloclen;
1060 int len, result;
1061 unsigned int sg_idx = 0, sg_offset = 0;
1063 result = usbat_flash_check_media(us, info);
1064 if (result != USB_STOR_TRANSPORT_GOOD)
1065 return result;
1067 // we're working in LBA mode. according to the ATA spec,
1068 // we can support up to 28-bit addressing. I don't know if Jumpshot
1069 // supports beyond 24-bit addressing. It's kind of hard to test
1070 // since it requires > 8GB CF card.
1072 if (sector > 0x0FFFFFFF)
1073 return USB_STOR_TRANSPORT_ERROR;
1075 totallen = sectors * info->ssize;
1077 // Since we don't write more than 64 KB at a time, we have to create
1078 // a bounce buffer and move the data a piece at a time between the
1079 // bounce buffer and the actual transfer buffer.
1081 alloclen = min(totallen, 65536u);
1082 buffer = kmalloc(alloclen, GFP_NOIO);
1083 if (buffer == NULL)
1084 return USB_STOR_TRANSPORT_ERROR;
1086 do {
1087 // loop, never allocate or transfer more than 64k at once
1088 // (min(128k, 255*info->ssize) is the real limit)
1089 len = min(totallen, alloclen);
1090 thistime = (len / info->ssize) & 0xff;
1092 // Get the data from the transfer buffer
1093 usb_stor_access_xfer_buf(buffer, len, us->srb,
1094 &sg_idx, &sg_offset, FROM_XFER_BUF);
1096 // ATAPI command 0x30 (WRITE SECTORS)
1097 usbat_pack_atapi_sector_cmd(command, thistime, sector, 0x30);
1099 // Write/execute ATAPI write command
1100 result = usbat_multiple_write(us, registers, command, 7);
1101 if (result != USB_STOR_TRANSPORT_GOOD)
1102 goto leave;
1104 // Write the data
1105 result = usbat_write_blocks(us, buffer, len);
1106 if (result != USB_STOR_TRANSPORT_GOOD)
1107 goto leave;
1109 sector += thistime;
1110 totallen -= len;
1111 } while (totallen > 0);
1113 kfree(buffer);
1114 return result;
1116 leave:
1117 kfree(buffer);
1118 return USB_STOR_TRANSPORT_ERROR;
1122 * Squeeze a potentially huge (> 65535 byte) read10 command into
1123 * a little ( <= 65535 byte) ATAPI pipe
1125 static int usbat_hp8200e_handle_read10(struct us_data *us,
1126 unsigned char *registers,
1127 unsigned char *data,
1128 struct scsi_cmnd *srb)
1130 int result = USB_STOR_TRANSPORT_GOOD;
1131 unsigned char *buffer;
1132 unsigned int len;
1133 unsigned int sector;
1134 unsigned int sg_segment = 0;
1135 unsigned int sg_offset = 0;
1137 US_DEBUGP("handle_read10: transfersize %d\n",
1138 srb->transfersize);
1140 if (srb->request_bufflen < 0x10000) {
1142 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1143 registers, data, 19,
1144 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1145 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1146 DMA_FROM_DEVICE,
1147 srb->request_buffer,
1148 srb->request_bufflen, srb->use_sg, 1);
1150 return result;
1154 * Since we're requesting more data than we can handle in
1155 * a single read command (max is 64k-1), we will perform
1156 * multiple reads, but each read must be in multiples of
1157 * a sector. Luckily the sector size is in srb->transfersize
1158 * (see linux/drivers/scsi/sr.c).
1161 if (data[7+0] == GPCMD_READ_CD) {
1162 len = short_pack(data[7+9], data[7+8]);
1163 len <<= 16;
1164 len |= data[7+7];
1165 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
1166 srb->transfersize = srb->request_bufflen/len;
1169 if (!srb->transfersize) {
1170 srb->transfersize = 2048; /* A guess */
1171 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
1172 srb->transfersize);
1175 // Since we only read in one block at a time, we have to create
1176 // a bounce buffer and move the data a piece at a time between the
1177 // bounce buffer and the actual transfer buffer.
1179 len = (65535/srb->transfersize) * srb->transfersize;
1180 US_DEBUGP("Max read is %d bytes\n", len);
1181 len = min(len, srb->request_bufflen);
1182 buffer = kmalloc(len, GFP_NOIO);
1183 if (buffer == NULL) // bloody hell!
1184 return USB_STOR_TRANSPORT_FAILED;
1185 sector = short_pack(data[7+3], data[7+2]);
1186 sector <<= 16;
1187 sector |= short_pack(data[7+5], data[7+4]);
1188 transferred = 0;
1190 sg_segment = 0; // for keeping track of where we are in
1191 sg_offset = 0; // the scatter/gather list
1193 while (transferred != srb->request_bufflen) {
1195 if (len > srb->request_bufflen - transferred)
1196 len = srb->request_bufflen - transferred;
1198 data[3] = len&0xFF; // (cylL) = expected length (L)
1199 data[4] = (len>>8)&0xFF; // (cylH) = expected length (H)
1201 // Fix up the SCSI command sector and num sectors
1203 data[7+2] = MSB_of(sector>>16); // SCSI command sector
1204 data[7+3] = LSB_of(sector>>16);
1205 data[7+4] = MSB_of(sector&0xFFFF);
1206 data[7+5] = LSB_of(sector&0xFFFF);
1207 if (data[7+0] == GPCMD_READ_CD)
1208 data[7+6] = 0;
1209 data[7+7] = MSB_of(len / srb->transfersize); // SCSI command
1210 data[7+8] = LSB_of(len / srb->transfersize); // num sectors
1212 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1213 registers, data, 19,
1214 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1215 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1216 DMA_FROM_DEVICE,
1217 buffer,
1218 len, 0, 1);
1220 if (result != USB_STOR_TRANSPORT_GOOD)
1221 break;
1223 // Store the data in the transfer buffer
1224 usb_stor_access_xfer_buf(buffer, len, srb,
1225 &sg_segment, &sg_offset, TO_XFER_BUF);
1227 // Update the amount transferred and the sector number
1229 transferred += len;
1230 sector += len / srb->transfersize;
1232 } // while transferred != srb->request_bufflen
1234 kfree(buffer);
1235 return result;
1238 static int usbat_select_and_test_registers(struct us_data *us)
1240 int selector;
1241 unsigned char *status = us->iobuf;
1242 unsigned char max_selector = 0xB0;
1243 if (usbat_get_device_type(us) == USBAT_DEV_FLASH)
1244 max_selector = 0xA0;
1246 // try device = master, then device = slave.
1248 for (selector = 0xA0; selector <= max_selector; selector += 0x10) {
1250 if (usbat_get_device_type(us) == USBAT_DEV_HP8200 &&
1251 usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
1252 USB_STOR_XFER_GOOD)
1253 return USB_STOR_TRANSPORT_ERROR;
1255 if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) !=
1256 USB_STOR_XFER_GOOD)
1257 return USB_STOR_TRANSPORT_ERROR;
1259 if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) !=
1260 USB_STOR_XFER_GOOD)
1261 return USB_STOR_TRANSPORT_ERROR;
1263 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1264 USB_STOR_XFER_GOOD)
1265 return USB_STOR_TRANSPORT_ERROR;
1267 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1268 USB_STOR_XFER_GOOD)
1269 return USB_STOR_TRANSPORT_ERROR;
1271 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) !=
1272 USB_STOR_XFER_GOOD)
1273 return USB_STOR_TRANSPORT_ERROR;
1275 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) !=
1276 USB_STOR_XFER_GOOD)
1277 return USB_STOR_TRANSPORT_ERROR;
1279 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1280 USB_STOR_XFER_GOOD)
1281 return USB_STOR_TRANSPORT_ERROR;
1283 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1284 USB_STOR_XFER_GOOD)
1285 return USB_STOR_TRANSPORT_ERROR;
1288 return USB_STOR_TRANSPORT_GOOD;
1292 * Initialize the USBAT processor and the storage device
1294 int init_usbat(struct us_data *us)
1296 int rc;
1297 struct usbat_info *info;
1298 unsigned char subcountH = USBAT_ATA_LBA_HI;
1299 unsigned char subcountL = USBAT_ATA_LBA_ME;
1300 unsigned char *status = us->iobuf;
1302 us->extra = kmalloc(sizeof(struct usbat_info), GFP_NOIO);
1303 if (!us->extra) {
1304 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1305 return 1;
1307 memset(us->extra, 0, sizeof(struct usbat_info));
1308 info = (struct usbat_info *) (us->extra);
1310 // Enable peripheral control signals
1311 rc = usbat_write_user_io(us,
1312 USBAT_UIO_OE1 | USBAT_UIO_OE0,
1313 USBAT_UIO_EPAD | USBAT_UIO_1);
1314 if (rc != USB_STOR_XFER_GOOD)
1315 return USB_STOR_TRANSPORT_ERROR;
1317 US_DEBUGP("INIT 1\n");
1319 msleep(2000);
1321 rc = usbat_read_user_io(us, status);
1322 if (rc != USB_STOR_TRANSPORT_GOOD)
1323 return rc;
1325 US_DEBUGP("INIT 2\n");
1327 rc = usbat_read_user_io(us, status);
1328 if (rc != USB_STOR_XFER_GOOD)
1329 return USB_STOR_TRANSPORT_ERROR;
1331 rc = usbat_read_user_io(us, status);
1332 if (rc != USB_STOR_XFER_GOOD)
1333 return USB_STOR_TRANSPORT_ERROR;
1335 US_DEBUGP("INIT 3\n");
1337 // At this point, we need to detect which device we are using
1338 if (usbat_set_transport(us, info))
1339 return USB_STOR_TRANSPORT_ERROR;
1341 US_DEBUGP("INIT 4\n");
1343 if (usbat_get_device_type(us) == USBAT_DEV_HP8200) {
1344 msleep(250);
1346 // Write 0x80 to ISA port 0x3F
1347 rc = usbat_write(us, USBAT_ISA, 0x3F, 0x80);
1348 if (rc != USB_STOR_XFER_GOOD)
1349 return USB_STOR_TRANSPORT_ERROR;
1351 US_DEBUGP("INIT 5\n");
1353 // Read ISA port 0x27
1354 rc = usbat_read(us, USBAT_ISA, 0x27, status);
1355 if (rc != USB_STOR_XFER_GOOD)
1356 return USB_STOR_TRANSPORT_ERROR;
1358 US_DEBUGP("INIT 6\n");
1360 rc = usbat_read_user_io(us, status);
1361 if (rc != USB_STOR_XFER_GOOD)
1362 return USB_STOR_TRANSPORT_ERROR;
1364 US_DEBUGP("INIT 7\n");
1367 rc = usbat_select_and_test_registers(us);
1368 if (rc != USB_STOR_TRANSPORT_GOOD)
1369 return rc;
1371 US_DEBUGP("INIT 8\n");
1373 rc = usbat_read_user_io(us, status);
1374 if (rc != USB_STOR_XFER_GOOD)
1375 return USB_STOR_TRANSPORT_ERROR;
1377 US_DEBUGP("INIT 9\n");
1379 // Enable peripheral control signals and card detect
1380 rc = usbat_device_enable_cdt(us);
1381 if (rc != USB_STOR_TRANSPORT_GOOD)
1382 return rc;
1384 US_DEBUGP("INIT 10\n");
1386 rc = usbat_read_user_io(us, status);
1387 if (rc != USB_STOR_XFER_GOOD)
1388 return USB_STOR_TRANSPORT_ERROR;
1390 US_DEBUGP("INIT 11\n");
1392 msleep(1400);
1394 rc = usbat_read_user_io(us, status);
1395 if (rc != USB_STOR_XFER_GOOD)
1396 return USB_STOR_TRANSPORT_ERROR;
1398 US_DEBUGP("INIT 12\n");
1400 rc = usbat_select_and_test_registers(us);
1401 if (rc != USB_STOR_TRANSPORT_GOOD)
1402 return rc;
1404 US_DEBUGP("INIT 13\n");
1406 if (usbat_get_device_type(us) == USBAT_DEV_FLASH) {
1407 subcountH = 0x02;
1408 subcountL = 0x00;
1410 rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1411 0x00, 0x88, 0x08, subcountH, subcountL);
1412 if (rc != USB_STOR_XFER_GOOD)
1413 return USB_STOR_TRANSPORT_ERROR;
1415 US_DEBUGP("INIT 14\n");
1417 return USB_STOR_TRANSPORT_GOOD;
1421 * Transport for the HP 8200e
1423 static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1425 int result;
1426 unsigned char *status = us->iobuf;
1427 unsigned char registers[32];
1428 unsigned char data[32];
1429 unsigned int len;
1430 int i;
1431 char string[64];
1433 len = srb->request_bufflen;
1435 /* Send A0 (ATA PACKET COMMAND).
1436 Note: I guess we're never going to get any of the ATA
1437 commands... just ATA Packet Commands.
1440 registers[0] = USBAT_ATA_FEATURES;
1441 registers[1] = USBAT_ATA_SECCNT;
1442 registers[2] = USBAT_ATA_SECNUM;
1443 registers[3] = USBAT_ATA_LBA_ME;
1444 registers[4] = USBAT_ATA_LBA_HI;
1445 registers[5] = USBAT_ATA_DEVICE;
1446 registers[6] = USBAT_ATA_CMD;
1447 data[0] = 0x00;
1448 data[1] = 0x00;
1449 data[2] = 0x00;
1450 data[3] = len&0xFF; // (cylL) = expected length (L)
1451 data[4] = (len>>8)&0xFF; // (cylH) = expected length (H)
1452 data[5] = 0xB0; // (device sel) = slave
1453 data[6] = 0xA0; // (command) = ATA PACKET COMMAND
1455 for (i=7; i<19; i++) {
1456 registers[i] = 0x10;
1457 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1460 result = usbat_get_status(us, status);
1461 US_DEBUGP("Status = %02X\n", *status);
1462 if (result != USB_STOR_XFER_GOOD)
1463 return USB_STOR_TRANSPORT_ERROR;
1464 if (srb->cmnd[0] == TEST_UNIT_READY)
1465 transferred = 0;
1467 if (srb->sc_data_direction == DMA_TO_DEVICE) {
1469 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1470 registers, data, 19,
1471 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1472 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1473 DMA_TO_DEVICE,
1474 srb->request_buffer,
1475 len, srb->use_sg, 10);
1477 if (result == USB_STOR_TRANSPORT_GOOD) {
1478 transferred += len;
1479 US_DEBUGP("Wrote %08X bytes\n", transferred);
1482 return result;
1484 } else if (srb->cmnd[0] == READ_10 ||
1485 srb->cmnd[0] == GPCMD_READ_CD) {
1487 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1491 if (len > 0xFFFF) {
1492 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1493 len);
1494 return USB_STOR_TRANSPORT_ERROR;
1497 if ( (result = usbat_multiple_write(us,
1498 registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
1499 return result;
1502 // Write the 12-byte command header.
1504 // If the command is BLANK then set the timer for 75 minutes.
1505 // Otherwise set it for 10 minutes.
1507 // NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1508 // AT SPEED 4 IS UNRELIABLE!!!
1510 if ( (result = usbat_write_block(us,
1511 USBAT_ATA, srb->cmnd, 12,
1512 srb->cmnd[0]==GPCMD_BLANK ? 75 : 10)) !=
1513 USB_STOR_TRANSPORT_GOOD) {
1514 return result;
1517 // If there is response data to be read in
1518 // then do it here.
1520 if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1522 // How many bytes to read in? Check cylL register
1524 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1525 USB_STOR_XFER_GOOD) {
1526 return USB_STOR_TRANSPORT_ERROR;
1529 if (len > 0xFF) { // need to read cylH also
1530 len = *status;
1531 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1532 USB_STOR_XFER_GOOD) {
1533 return USB_STOR_TRANSPORT_ERROR;
1535 len += ((unsigned int) *status)<<8;
1537 else
1538 len = *status;
1541 result = usbat_read_block(us, srb->request_buffer, len);
1543 /* Debug-print the first 32 bytes of the transfer */
1545 if (!srb->use_sg) {
1546 string[0] = 0;
1547 for (i=0; i<len && i<32; i++) {
1548 sprintf(string+strlen(string), "%02X ",
1549 ((unsigned char *)srb->request_buffer)[i]);
1550 if ((i%16)==15) {
1551 US_DEBUGP("%s\n", string);
1552 string[0] = 0;
1555 if (string[0]!=0)
1556 US_DEBUGP("%s\n", string);
1560 return result;
1564 * Transport for USBAT02-based CompactFlash and similar storage devices
1566 static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1568 int rc;
1569 struct usbat_info *info = (struct usbat_info *) (us->extra);
1570 unsigned long block, blocks;
1571 unsigned char *ptr = us->iobuf;
1572 static unsigned char inquiry_response[36] = {
1573 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1576 if (srb->cmnd[0] == INQUIRY) {
1577 US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1578 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1579 fill_inquiry_response(us, ptr, 36);
1580 return USB_STOR_TRANSPORT_GOOD;
1583 if (srb->cmnd[0] == READ_CAPACITY) {
1584 rc = usbat_flash_check_media(us, info);
1585 if (rc != USB_STOR_TRANSPORT_GOOD)
1586 return rc;
1588 rc = usbat_flash_get_sector_count(us, info);
1589 if (rc != USB_STOR_TRANSPORT_GOOD)
1590 return rc;
1592 info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
1593 US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1594 info->sectors, info->ssize);
1596 // build the reply
1597 // note: must return the sector number of the last sector,
1598 // *not* the total number of sectors
1599 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1600 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1601 usb_stor_set_xfer_buf(ptr, 8, srb);
1603 return USB_STOR_TRANSPORT_GOOD;
1606 if (srb->cmnd[0] == MODE_SELECT_10) {
1607 US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
1608 return USB_STOR_TRANSPORT_ERROR;
1611 if (srb->cmnd[0] == READ_10) {
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[7]) << 8) | ((u32)(srb->cmnd[8]));
1617 US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
1618 return usbat_flash_read_data(us, info, block, blocks);
1621 if (srb->cmnd[0] == READ_12) {
1622 // I don't think we'll ever see a READ_12 but support it anyway...
1623 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1624 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1626 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1627 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1629 US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
1630 return usbat_flash_read_data(us, info, block, blocks);
1633 if (srb->cmnd[0] == WRITE_10) {
1634 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1635 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1637 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1639 US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
1640 return usbat_flash_write_data(us, info, block, blocks);
1643 if (srb->cmnd[0] == WRITE_12) {
1644 // I don't think we'll ever see a WRITE_12 but support it anyway...
1645 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1646 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1648 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1649 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1651 US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
1652 return usbat_flash_write_data(us, info, block, blocks);
1656 if (srb->cmnd[0] == TEST_UNIT_READY) {
1657 US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
1659 rc = usbat_flash_check_media(us, info);
1660 if (rc != USB_STOR_TRANSPORT_GOOD)
1661 return rc;
1663 return usbat_check_status(us);
1666 if (srb->cmnd[0] == REQUEST_SENSE) {
1667 US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
1669 memset(ptr, 0, 18);
1670 ptr[0] = 0xF0;
1671 ptr[2] = info->sense_key;
1672 ptr[7] = 11;
1673 ptr[12] = info->sense_asc;
1674 ptr[13] = info->sense_ascq;
1675 usb_stor_set_xfer_buf(ptr, 18, srb);
1677 return USB_STOR_TRANSPORT_GOOD;
1680 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1681 // sure. whatever. not like we can stop the user from popping
1682 // the media out of the device (no locking doors, etc)
1683 return USB_STOR_TRANSPORT_GOOD;
1686 US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1687 srb->cmnd[0], srb->cmnd[0]);
1688 info->sense_key = 0x05;
1689 info->sense_asc = 0x20;
1690 info->sense_ascq = 0x00;
1691 return USB_STOR_TRANSPORT_FAILED;
1695 * Default transport function. Attempts to detect which transport function
1696 * should be called, makes it the new default, and calls it.
1698 * This function should never be called. Our usbat_init() function detects the
1699 * device type and changes the us->transport ptr to the transport function
1700 * relevant to the device.
1701 * However, we'll support this impossible(?) case anyway.
1703 int usbat_transport(struct scsi_cmnd *srb, struct us_data *us)
1705 struct usbat_info *info = (struct usbat_info*) (us->extra);
1707 if (usbat_set_transport(us, info))
1708 return USB_STOR_TRANSPORT_ERROR;
1710 return us->transport(srb, us);