V4L/DVB (6715): ivtv: Remove unnecessary register update
[linux-2.6/verdex.git] / drivers / usb / storage / shuttle_usbat.c
blobcb22a9ad16943cfbccdef9356651c1fa4f499342
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
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/errno.h>
47 #include <linux/slab.h>
48 #include <linux/cdrom.h>
50 #include <scsi/scsi.h>
51 #include <scsi/scsi_cmnd.h>
53 #include "usb.h"
54 #include "transport.h"
55 #include "protocol.h"
56 #include "debug.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)
76 buf[0] = 0;
77 buf[1] = thistime;
78 buf[2] = sector & 0xFF;
79 buf[3] = (sector >> 8) & 0xFF;
80 buf[4] = (sector >> 16) & 0xFF;
81 buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
82 buf[6] = cmd;
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,
97 unsigned char access,
98 unsigned char reg,
99 unsigned char *content)
101 return usb_stor_ctrl_transfer(us,
102 us->recv_ctrl_pipe,
103 access | USBAT_CMD_READ_REG,
104 0xC0,
105 (u16)reg,
107 content,
112 * Write to a register on the device
114 static int usbat_write(struct us_data *us,
115 unsigned char access,
116 unsigned char reg,
117 unsigned char content)
119 return usb_stor_ctrl_transfer(us,
120 us->send_ctrl_pipe,
121 access | USBAT_CMD_WRITE_REG,
122 0x40,
123 short_pack(reg, content),
125 NULL,
130 * Convenience function to perform a bulk read
132 static int usbat_bulk_read(struct us_data *us,
133 unsigned char *data,
134 unsigned int len,
135 int use_sg)
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_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,
148 unsigned char *data,
149 unsigned int len,
150 int use_sg)
152 if (len == 0)
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
162 * to be executed.
164 static int usbat_execute_command(struct us_data *us,
165 unsigned char *commands,
166 unsigned int len)
168 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
169 USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
170 commands, len);
174 * Read the status register
176 static int usbat_get_status(struct us_data *us, unsigned char *status)
178 int rc;
179 rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
181 US_DEBUGP("usbat_get_status: 0x%02X\n", (unsigned short) (*status));
182 return rc;
186 * Check the device status
188 static int usbat_check_status(struct us_data *us)
190 unsigned char *reply = us->iobuf;
191 int rc;
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;
201 /* device fault */
202 if (*reply & 0x20)
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;
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
242 * place.
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
256 * transferred.
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)
270 int i;
271 int result;
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
277 * minutes!
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;
298 if (i<500)
299 msleep(10); /* 5 seconds */
300 else if (i<700)
301 msleep(50); /* 10 seconds */
302 else if (i<1200)
303 msleep(100); /* 50 seconds */
304 else
305 msleep(1000); /* X minutes */
308 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
309 minutes);
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,
318 unsigned short len,
319 int use_sg)
321 int result;
322 unsigned char *command = us->iobuf;
324 if (!len)
325 return USB_STOR_TRANSPORT_GOOD;
327 command[0] = 0xC0;
328 command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
329 command[2] = USBAT_ATA_DATA;
330 command[3] = 0;
331 command[4] = 0;
332 command[5] = 0;
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,
351 unsigned short len,
352 int minutes,
353 int use_sg)
355 int result;
356 unsigned char *command = us->iobuf;
358 if (!len)
359 return USB_STOR_TRANSPORT_GOOD;
361 command[0] = 0x40;
362 command[1] = access | USBAT_CMD_WRITE_BLOCK;
363 command[2] = USBAT_ATA_DATA;
364 command[3] = 0;
365 command[4] = 0;
366 command[5] = 0;
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,
394 int direction,
395 unsigned char *content,
396 unsigned short len,
397 int use_sg,
398 int minutes)
400 int result;
401 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
402 us->recv_bulk_pipe : us->send_bulk_pipe;
404 unsigned char *command = us->iobuf;
405 int i, j;
406 int cmdlen;
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.
425 if (i==0) {
426 cmdlen = 16;
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.
433 command[0] = 0x40;
434 command[1] = access | USBAT_CMD_WRITE_REGS;
435 command[2] = 0x07;
436 command[3] = 0x17;
437 command[4] = 0xFC;
438 command[5] = 0xE7;
439 command[6] = LSB_of(num_registers*2);
440 command[7] = MSB_of(num_registers*2);
441 } else
442 cmdlen = 8;
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;
461 if (i==0) {
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
493 * transferred.
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,
517 status);
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;
531 else
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)
554 int i, result;
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 */
561 command[0] = 0x40;
562 command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
564 /* No relevance */
565 command[2] = 0;
566 command[3] = 0;
567 command[4] = 0;
568 command[5] = 0;
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];
585 /* Send the data */
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);
592 else
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,
610 int len,
611 int use_sg)
613 int result;
614 unsigned char *command = us->iobuf;
616 command[0] = 0xC0;
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,
652 int len,
653 int use_sg)
655 int result;
656 unsigned char *command = us->iobuf;
658 command[0] = 0x40;
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;
672 /* Write the data */
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)
685 int result;
687 result = usb_stor_ctrl_transfer(us,
688 us->recv_ctrl_pipe,
689 USBAT_CMD_UIO,
690 0xC0,
693 data_flags,
694 USBAT_UIO_READ);
696 US_DEBUGP("usbat_read_user_io: UIO register reads %02X\n", (unsigned short) (*data_flags));
698 return result;
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,
709 us->send_ctrl_pipe,
710 USBAT_CMD_UIO,
711 0x40,
712 short_pack(enable_flags, data_flags),
714 NULL,
715 USBAT_UIO_WRITE);
719 * Reset the device
720 * Often needed on media change.
722 static int usbat_device_reset(struct us_data *us)
724 int rc;
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;
750 * Enable card detect
752 static int usbat_device_enable_cdt(struct us_data *us)
754 int rc;
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)
798 int rc;
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)
821 return rc;
822 rc = usbat_device_enable_cdt(us);
823 if (rc != USB_STOR_TRANSPORT_GOOD)
824 return rc;
826 msleep(50);
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)
849 int rc;
850 unsigned char status;
852 if (!us || !info)
853 return USB_STOR_TRANSPORT_ERROR;
855 rc = usbat_device_reset(us);
856 if (rc != USB_STOR_TRANSPORT_GOOD)
857 return rc;
858 msleep(500);
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;
879 } else {
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,
893 int devicetype)
896 if (!info->devicetype)
897 info->devicetype = devicetype;
899 if (!info->devicetype)
900 usbat_identify_device(us, info);
902 switch (info->devicetype) {
903 default:
904 return USB_STOR_TRANSPORT_ERROR;
906 case USBAT_DEV_HP8200:
907 us->transport = usbat_hp8200e_transport;
908 break;
910 case USBAT_DEV_FLASH:
911 us->transport = usbat_flash_transport;
912 break;
915 return 0;
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] = {
925 USBAT_ATA_SECCNT,
926 USBAT_ATA_DEVICE,
927 USBAT_ATA_CMD,
929 unsigned char command[3] = { 0x01, 0xA0, 0xEC };
930 unsigned char *reply;
931 unsigned char status;
932 int rc;
934 if (!us || !info)
935 return USB_STOR_TRANSPORT_ERROR;
937 reply = kmalloc(512, GFP_NOIO);
938 if (!reply)
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;
946 goto leave;
949 /* Read device status */
950 if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
951 rc = USB_STOR_TRANSPORT_ERROR;
952 goto leave;
955 msleep(100);
957 /* Read the device identification data */
958 rc = usbat_read_block(us, reply, 512, 0);
959 if (rc != USB_STOR_TRANSPORT_GOOD)
960 goto leave;
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;
969 leave:
970 kfree(reply);
971 return rc;
975 * Read data from device
977 static int usbat_flash_read_data(struct us_data *us,
978 struct usbat_info *info,
979 u32 sector,
980 u32 sectors)
982 unsigned char registers[7] = {
983 USBAT_ATA_FEATURES,
984 USBAT_ATA_SECCNT,
985 USBAT_ATA_SECNUM,
986 USBAT_ATA_LBA_ME,
987 USBAT_ATA_LBA_HI,
988 USBAT_ATA_DEVICE,
989 USBAT_ATA_STATUS,
991 unsigned char command[7];
992 unsigned char *buffer;
993 unsigned char thistime;
994 unsigned int totallen, alloclen;
995 int len, result;
996 unsigned int sg_offset = 0;
997 struct scatterlist *sg = NULL;
999 result = usbat_flash_check_media(us, info);
1000 if (result != USB_STOR_TRANSPORT_GOOD)
1001 return result;
1004 * we're working in LBA mode. according to the ATA spec,
1005 * we can support up to 28-bit addressing. I don't know if Jumpshot
1006 * supports beyond 24-bit addressing. It's kind of hard to test
1007 * since it requires > 8GB CF card.
1010 if (sector > 0x0FFFFFFF)
1011 return USB_STOR_TRANSPORT_ERROR;
1013 totallen = sectors * info->ssize;
1016 * Since we don't read more than 64 KB at a time, we have to create
1017 * a bounce buffer and move the data a piece at a time between the
1018 * bounce buffer and the actual transfer buffer.
1021 alloclen = min(totallen, 65536u);
1022 buffer = kmalloc(alloclen, GFP_NOIO);
1023 if (buffer == NULL)
1024 return USB_STOR_TRANSPORT_ERROR;
1026 do {
1028 * loop, never allocate or transfer more than 64k at once
1029 * (min(128k, 255*info->ssize) is the real limit)
1031 len = min(totallen, alloclen);
1032 thistime = (len / info->ssize) & 0xff;
1034 /* ATA command 0x20 (READ SECTORS) */
1035 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
1037 /* Write/execute ATA read command */
1038 result = usbat_multiple_write(us, registers, command, 7);
1039 if (result != USB_STOR_TRANSPORT_GOOD)
1040 goto leave;
1042 /* Read the data we just requested */
1043 result = usbat_read_blocks(us, buffer, len, 0);
1044 if (result != USB_STOR_TRANSPORT_GOOD)
1045 goto leave;
1047 US_DEBUGP("usbat_flash_read_data: %d bytes\n", len);
1049 /* Store the data in the transfer buffer */
1050 usb_stor_access_xfer_buf(buffer, len, us->srb,
1051 &sg, &sg_offset, TO_XFER_BUF);
1053 sector += thistime;
1054 totallen -= len;
1055 } while (totallen > 0);
1057 kfree(buffer);
1058 return USB_STOR_TRANSPORT_GOOD;
1060 leave:
1061 kfree(buffer);
1062 return USB_STOR_TRANSPORT_ERROR;
1066 * Write data to device
1068 static int usbat_flash_write_data(struct us_data *us,
1069 struct usbat_info *info,
1070 u32 sector,
1071 u32 sectors)
1073 unsigned char registers[7] = {
1074 USBAT_ATA_FEATURES,
1075 USBAT_ATA_SECCNT,
1076 USBAT_ATA_SECNUM,
1077 USBAT_ATA_LBA_ME,
1078 USBAT_ATA_LBA_HI,
1079 USBAT_ATA_DEVICE,
1080 USBAT_ATA_STATUS,
1082 unsigned char command[7];
1083 unsigned char *buffer;
1084 unsigned char thistime;
1085 unsigned int totallen, alloclen;
1086 int len, result;
1087 unsigned int sg_offset = 0;
1088 struct scatterlist *sg = NULL;
1090 result = usbat_flash_check_media(us, info);
1091 if (result != USB_STOR_TRANSPORT_GOOD)
1092 return result;
1095 * we're working in LBA mode. according to the ATA spec,
1096 * we can support up to 28-bit addressing. I don't know if the device
1097 * supports beyond 24-bit addressing. It's kind of hard to test
1098 * since it requires > 8GB media.
1101 if (sector > 0x0FFFFFFF)
1102 return USB_STOR_TRANSPORT_ERROR;
1104 totallen = sectors * info->ssize;
1107 * Since we don't write more than 64 KB at a time, we have to create
1108 * a bounce buffer and move the data a piece at a time between the
1109 * bounce buffer and the actual transfer buffer.
1112 alloclen = min(totallen, 65536u);
1113 buffer = kmalloc(alloclen, GFP_NOIO);
1114 if (buffer == NULL)
1115 return USB_STOR_TRANSPORT_ERROR;
1117 do {
1119 * loop, never allocate or transfer more than 64k at once
1120 * (min(128k, 255*info->ssize) is the real limit)
1122 len = min(totallen, alloclen);
1123 thistime = (len / info->ssize) & 0xff;
1125 /* Get the data from the transfer buffer */
1126 usb_stor_access_xfer_buf(buffer, len, us->srb,
1127 &sg, &sg_offset, FROM_XFER_BUF);
1129 /* ATA command 0x30 (WRITE SECTORS) */
1130 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
1132 /* Write/execute ATA write command */
1133 result = usbat_multiple_write(us, registers, command, 7);
1134 if (result != USB_STOR_TRANSPORT_GOOD)
1135 goto leave;
1137 /* Write the data */
1138 result = usbat_write_blocks(us, buffer, len, 0);
1139 if (result != USB_STOR_TRANSPORT_GOOD)
1140 goto leave;
1142 sector += thistime;
1143 totallen -= len;
1144 } while (totallen > 0);
1146 kfree(buffer);
1147 return result;
1149 leave:
1150 kfree(buffer);
1151 return USB_STOR_TRANSPORT_ERROR;
1155 * Squeeze a potentially huge (> 65535 byte) read10 command into
1156 * a little ( <= 65535 byte) ATAPI pipe
1158 static int usbat_hp8200e_handle_read10(struct us_data *us,
1159 unsigned char *registers,
1160 unsigned char *data,
1161 struct scsi_cmnd *srb)
1163 int result = USB_STOR_TRANSPORT_GOOD;
1164 unsigned char *buffer;
1165 unsigned int len;
1166 unsigned int sector;
1167 unsigned int sg_offset = 0;
1168 struct scatterlist *sg = NULL;
1170 US_DEBUGP("handle_read10: transfersize %d\n",
1171 srb->transfersize);
1173 if (srb->request_bufflen < 0x10000) {
1175 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1176 registers, data, 19,
1177 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1178 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1179 DMA_FROM_DEVICE,
1180 srb->request_buffer,
1181 srb->request_bufflen, srb->use_sg, 1);
1183 return result;
1187 * Since we're requesting more data than we can handle in
1188 * a single read command (max is 64k-1), we will perform
1189 * multiple reads, but each read must be in multiples of
1190 * a sector. Luckily the sector size is in srb->transfersize
1191 * (see linux/drivers/scsi/sr.c).
1194 if (data[7+0] == GPCMD_READ_CD) {
1195 len = short_pack(data[7+9], data[7+8]);
1196 len <<= 16;
1197 len |= data[7+7];
1198 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
1199 srb->transfersize = srb->request_bufflen/len;
1202 if (!srb->transfersize) {
1203 srb->transfersize = 2048; /* A guess */
1204 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
1205 srb->transfersize);
1209 * Since we only read in one block at a time, we have to create
1210 * a bounce buffer and move the data a piece at a time between the
1211 * bounce buffer and the actual transfer buffer.
1214 len = (65535/srb->transfersize) * srb->transfersize;
1215 US_DEBUGP("Max read is %d bytes\n", len);
1216 len = min(len, srb->request_bufflen);
1217 buffer = kmalloc(len, GFP_NOIO);
1218 if (buffer == NULL) /* bloody hell! */
1219 return USB_STOR_TRANSPORT_FAILED;
1220 sector = short_pack(data[7+3], data[7+2]);
1221 sector <<= 16;
1222 sector |= short_pack(data[7+5], data[7+4]);
1223 transferred = 0;
1225 while (transferred != srb->request_bufflen) {
1227 if (len > srb->request_bufflen - transferred)
1228 len = srb->request_bufflen - transferred;
1230 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1231 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
1233 /* Fix up the SCSI command sector and num sectors */
1235 data[7+2] = MSB_of(sector>>16); /* SCSI command sector */
1236 data[7+3] = LSB_of(sector>>16);
1237 data[7+4] = MSB_of(sector&0xFFFF);
1238 data[7+5] = LSB_of(sector&0xFFFF);
1239 if (data[7+0] == GPCMD_READ_CD)
1240 data[7+6] = 0;
1241 data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */
1242 data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */
1244 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1245 registers, data, 19,
1246 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1247 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1248 DMA_FROM_DEVICE,
1249 buffer,
1250 len, 0, 1);
1252 if (result != USB_STOR_TRANSPORT_GOOD)
1253 break;
1255 /* Store the data in the transfer buffer */
1256 usb_stor_access_xfer_buf(buffer, len, srb,
1257 &sg, &sg_offset, TO_XFER_BUF);
1259 /* Update the amount transferred and the sector number */
1261 transferred += len;
1262 sector += len / srb->transfersize;
1264 } /* while transferred != srb->request_bufflen */
1266 kfree(buffer);
1267 return result;
1270 static int usbat_select_and_test_registers(struct us_data *us)
1272 int selector;
1273 unsigned char *status = us->iobuf;
1275 /* try device = master, then device = slave. */
1276 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
1277 if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
1278 USB_STOR_XFER_GOOD)
1279 return USB_STOR_TRANSPORT_ERROR;
1281 if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) !=
1282 USB_STOR_XFER_GOOD)
1283 return USB_STOR_TRANSPORT_ERROR;
1285 if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) !=
1286 USB_STOR_XFER_GOOD)
1287 return USB_STOR_TRANSPORT_ERROR;
1289 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1290 USB_STOR_XFER_GOOD)
1291 return USB_STOR_TRANSPORT_ERROR;
1293 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1294 USB_STOR_XFER_GOOD)
1295 return USB_STOR_TRANSPORT_ERROR;
1297 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) !=
1298 USB_STOR_XFER_GOOD)
1299 return USB_STOR_TRANSPORT_ERROR;
1301 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) !=
1302 USB_STOR_XFER_GOOD)
1303 return USB_STOR_TRANSPORT_ERROR;
1305 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1306 USB_STOR_XFER_GOOD)
1307 return USB_STOR_TRANSPORT_ERROR;
1309 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1310 USB_STOR_XFER_GOOD)
1311 return USB_STOR_TRANSPORT_ERROR;
1314 return USB_STOR_TRANSPORT_GOOD;
1318 * Initialize the USBAT processor and the storage device
1320 static int init_usbat(struct us_data *us, int devicetype)
1322 int rc;
1323 struct usbat_info *info;
1324 unsigned char subcountH = USBAT_ATA_LBA_HI;
1325 unsigned char subcountL = USBAT_ATA_LBA_ME;
1326 unsigned char *status = us->iobuf;
1328 us->extra = kzalloc(sizeof(struct usbat_info), GFP_NOIO);
1329 if (!us->extra) {
1330 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1331 return 1;
1333 info = (struct usbat_info *) (us->extra);
1335 /* Enable peripheral control signals */
1336 rc = usbat_write_user_io(us,
1337 USBAT_UIO_OE1 | USBAT_UIO_OE0,
1338 USBAT_UIO_EPAD | USBAT_UIO_1);
1339 if (rc != USB_STOR_XFER_GOOD)
1340 return USB_STOR_TRANSPORT_ERROR;
1342 US_DEBUGP("INIT 1\n");
1344 msleep(2000);
1346 rc = usbat_read_user_io(us, status);
1347 if (rc != USB_STOR_TRANSPORT_GOOD)
1348 return rc;
1350 US_DEBUGP("INIT 2\n");
1352 rc = usbat_read_user_io(us, status);
1353 if (rc != USB_STOR_XFER_GOOD)
1354 return USB_STOR_TRANSPORT_ERROR;
1356 rc = usbat_read_user_io(us, status);
1357 if (rc != USB_STOR_XFER_GOOD)
1358 return USB_STOR_TRANSPORT_ERROR;
1360 US_DEBUGP("INIT 3\n");
1362 rc = usbat_select_and_test_registers(us);
1363 if (rc != USB_STOR_TRANSPORT_GOOD)
1364 return rc;
1366 US_DEBUGP("INIT 4\n");
1368 rc = usbat_read_user_io(us, status);
1369 if (rc != USB_STOR_XFER_GOOD)
1370 return USB_STOR_TRANSPORT_ERROR;
1372 US_DEBUGP("INIT 5\n");
1374 /* Enable peripheral control signals and card detect */
1375 rc = usbat_device_enable_cdt(us);
1376 if (rc != USB_STOR_TRANSPORT_GOOD)
1377 return rc;
1379 US_DEBUGP("INIT 6\n");
1381 rc = usbat_read_user_io(us, status);
1382 if (rc != USB_STOR_XFER_GOOD)
1383 return USB_STOR_TRANSPORT_ERROR;
1385 US_DEBUGP("INIT 7\n");
1387 msleep(1400);
1389 rc = usbat_read_user_io(us, status);
1390 if (rc != USB_STOR_XFER_GOOD)
1391 return USB_STOR_TRANSPORT_ERROR;
1393 US_DEBUGP("INIT 8\n");
1395 rc = usbat_select_and_test_registers(us);
1396 if (rc != USB_STOR_TRANSPORT_GOOD)
1397 return rc;
1399 US_DEBUGP("INIT 9\n");
1401 /* At this point, we need to detect which device we are using */
1402 if (usbat_set_transport(us, info, devicetype))
1403 return USB_STOR_TRANSPORT_ERROR;
1405 US_DEBUGP("INIT 10\n");
1407 if (usbat_get_device_type(us) == USBAT_DEV_FLASH) {
1408 subcountH = 0x02;
1409 subcountL = 0x00;
1411 rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1412 0x00, 0x88, 0x08, subcountH, subcountL);
1413 if (rc != USB_STOR_XFER_GOOD)
1414 return USB_STOR_TRANSPORT_ERROR;
1416 US_DEBUGP("INIT 11\n");
1418 return USB_STOR_TRANSPORT_GOOD;
1422 * Transport for the HP 8200e
1424 static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1426 int result;
1427 unsigned char *status = us->iobuf;
1428 unsigned char registers[32];
1429 unsigned char data[32];
1430 unsigned int len;
1431 int i;
1432 char string[64];
1434 len = srb->request_bufflen;
1436 /* Send A0 (ATA PACKET COMMAND).
1437 Note: I guess we're never going to get any of the ATA
1438 commands... just ATA Packet Commands.
1441 registers[0] = USBAT_ATA_FEATURES;
1442 registers[1] = USBAT_ATA_SECCNT;
1443 registers[2] = USBAT_ATA_SECNUM;
1444 registers[3] = USBAT_ATA_LBA_ME;
1445 registers[4] = USBAT_ATA_LBA_HI;
1446 registers[5] = USBAT_ATA_DEVICE;
1447 registers[6] = USBAT_ATA_CMD;
1448 data[0] = 0x00;
1449 data[1] = 0x00;
1450 data[2] = 0x00;
1451 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1452 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
1453 data[5] = 0xB0; /* (device sel) = slave */
1454 data[6] = 0xA0; /* (command) = ATA PACKET COMMAND */
1456 for (i=7; i<19; i++) {
1457 registers[i] = 0x10;
1458 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1461 result = usbat_get_status(us, status);
1462 US_DEBUGP("Status = %02X\n", *status);
1463 if (result != USB_STOR_XFER_GOOD)
1464 return USB_STOR_TRANSPORT_ERROR;
1465 if (srb->cmnd[0] == TEST_UNIT_READY)
1466 transferred = 0;
1468 if (srb->sc_data_direction == DMA_TO_DEVICE) {
1470 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1471 registers, data, 19,
1472 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1473 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1474 DMA_TO_DEVICE,
1475 srb->request_buffer,
1476 len, srb->use_sg, 10);
1478 if (result == USB_STOR_TRANSPORT_GOOD) {
1479 transferred += len;
1480 US_DEBUGP("Wrote %08X bytes\n", transferred);
1483 return result;
1485 } else if (srb->cmnd[0] == READ_10 ||
1486 srb->cmnd[0] == GPCMD_READ_CD) {
1488 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1492 if (len > 0xFFFF) {
1493 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1494 len);
1495 return USB_STOR_TRANSPORT_ERROR;
1498 if ( (result = usbat_multiple_write(us,
1499 registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
1500 return result;
1504 * Write the 12-byte command header.
1506 * If the command is BLANK then set the timer for 75 minutes.
1507 * Otherwise set it for 10 minutes.
1509 * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1510 * AT SPEED 4 IS UNRELIABLE!!!
1513 if ((result = usbat_write_block(us,
1514 USBAT_ATA, srb->cmnd, 12,
1515 (srb->cmnd[0]==GPCMD_BLANK ? 75 : 10), 0) !=
1516 USB_STOR_TRANSPORT_GOOD)) {
1517 return result;
1520 /* If there is response data to be read in then do it here. */
1522 if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1524 /* How many bytes to read in? Check cylL register */
1526 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1527 USB_STOR_XFER_GOOD) {
1528 return USB_STOR_TRANSPORT_ERROR;
1531 if (len > 0xFF) { /* need to read cylH also */
1532 len = *status;
1533 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1534 USB_STOR_XFER_GOOD) {
1535 return USB_STOR_TRANSPORT_ERROR;
1537 len += ((unsigned int) *status)<<8;
1539 else
1540 len = *status;
1543 result = usbat_read_block(us, srb->request_buffer, len, srb->use_sg);
1545 /* Debug-print the first 32 bytes of the transfer */
1547 if (!srb->use_sg) {
1548 string[0] = 0;
1549 for (i=0; i<len && i<32; i++) {
1550 sprintf(string+strlen(string), "%02X ",
1551 ((unsigned char *)srb->request_buffer)[i]);
1552 if ((i%16)==15) {
1553 US_DEBUGP("%s\n", string);
1554 string[0] = 0;
1557 if (string[0]!=0)
1558 US_DEBUGP("%s\n", string);
1562 return result;
1566 * Transport for USBAT02-based CompactFlash and similar storage devices
1568 static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1570 int rc;
1571 struct usbat_info *info = (struct usbat_info *) (us->extra);
1572 unsigned long block, blocks;
1573 unsigned char *ptr = us->iobuf;
1574 static unsigned char inquiry_response[36] = {
1575 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1578 if (srb->cmnd[0] == INQUIRY) {
1579 US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1580 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1581 fill_inquiry_response(us, ptr, 36);
1582 return USB_STOR_TRANSPORT_GOOD;
1585 if (srb->cmnd[0] == READ_CAPACITY) {
1586 rc = usbat_flash_check_media(us, info);
1587 if (rc != USB_STOR_TRANSPORT_GOOD)
1588 return rc;
1590 rc = usbat_flash_get_sector_count(us, info);
1591 if (rc != USB_STOR_TRANSPORT_GOOD)
1592 return rc;
1594 /* hard coded 512 byte sectors as per ATA spec */
1595 info->ssize = 0x200;
1596 US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1597 info->sectors, info->ssize);
1600 * build the reply
1601 * note: must return the sector number of the last sector,
1602 * *not* the total number of sectors
1604 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1605 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1606 usb_stor_set_xfer_buf(ptr, 8, srb);
1608 return USB_STOR_TRANSPORT_GOOD;
1611 if (srb->cmnd[0] == MODE_SELECT_10) {
1612 US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
1613 return USB_STOR_TRANSPORT_ERROR;
1616 if (srb->cmnd[0] == READ_10) {
1617 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1618 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1620 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1622 US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
1623 return usbat_flash_read_data(us, info, block, blocks);
1626 if (srb->cmnd[0] == READ_12) {
1628 * I don't think we'll ever see a READ_12 but support it anyway
1630 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1631 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1633 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1634 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1636 US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
1637 return usbat_flash_read_data(us, info, block, blocks);
1640 if (srb->cmnd[0] == WRITE_10) {
1641 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1642 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1644 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1646 US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
1647 return usbat_flash_write_data(us, info, block, blocks);
1650 if (srb->cmnd[0] == WRITE_12) {
1652 * I don't think we'll ever see a WRITE_12 but support it anyway
1654 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1655 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1657 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1658 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1660 US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
1661 return usbat_flash_write_data(us, info, block, blocks);
1665 if (srb->cmnd[0] == TEST_UNIT_READY) {
1666 US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
1668 rc = usbat_flash_check_media(us, info);
1669 if (rc != USB_STOR_TRANSPORT_GOOD)
1670 return rc;
1672 return usbat_check_status(us);
1675 if (srb->cmnd[0] == REQUEST_SENSE) {
1676 US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
1678 memset(ptr, 0, 18);
1679 ptr[0] = 0xF0;
1680 ptr[2] = info->sense_key;
1681 ptr[7] = 11;
1682 ptr[12] = info->sense_asc;
1683 ptr[13] = info->sense_ascq;
1684 usb_stor_set_xfer_buf(ptr, 18, srb);
1686 return USB_STOR_TRANSPORT_GOOD;
1689 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1691 * sure. whatever. not like we can stop the user from popping
1692 * the media out of the device (no locking doors, etc)
1694 return USB_STOR_TRANSPORT_GOOD;
1697 US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1698 srb->cmnd[0], srb->cmnd[0]);
1699 info->sense_key = 0x05;
1700 info->sense_asc = 0x20;
1701 info->sense_ascq = 0x00;
1702 return USB_STOR_TRANSPORT_FAILED;
1705 int init_usbat_cd(struct us_data *us)
1707 return init_usbat(us, USBAT_DEV_HP8200);
1711 int init_usbat_flash(struct us_data *us)
1713 return init_usbat(us, USBAT_DEV_FLASH);
1716 int init_usbat_probe(struct us_data *us)
1718 return init_usbat(us, 0);
1722 * Default transport function. Attempts to detect which transport function
1723 * should be called, makes it the new default, and calls it.
1725 * This function should never be called. Our usbat_init() function detects the
1726 * device type and changes the us->transport ptr to the transport function
1727 * relevant to the device.
1728 * However, we'll support this impossible(?) case anyway.
1730 int usbat_transport(struct scsi_cmnd *srb, struct us_data *us)
1732 struct usbat_info *info = (struct usbat_info*) (us->extra);
1734 if (usbat_set_transport(us, info, 0))
1735 return USB_STOR_TRANSPORT_ERROR;
1737 return us->transport(srb, us);