[PATCH] slab: remove alloc_pages() calls
[linux-2.6/x86.git] / drivers / usb / storage / shuttle_usbat.c
blob33c55a6261bba4a9a5cc14656d265a078f9523a6
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/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 ATA read/write sectors command
71 * Use cmd=0x20 for read, cmd=0x30 for write
73 static void usbat_pack_ata_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 /* error/check condition (0x51 is ok) */
200 if (*reply & 0x01 && *reply != 0x51)
201 return USB_STOR_TRANSPORT_FAILED;
203 /* device fault */
204 if (*reply & 0x20)
205 return USB_STOR_TRANSPORT_FAILED;
207 return USB_STOR_TRANSPORT_GOOD;
211 * Stores critical information in internal registers in prepartion for the execution
212 * of a conditional usbat_read_blocks or usbat_write_blocks call.
214 static int usbat_set_shuttle_features(struct us_data *us,
215 unsigned char external_trigger,
216 unsigned char epp_control,
217 unsigned char mask_byte,
218 unsigned char test_pattern,
219 unsigned char subcountH,
220 unsigned char subcountL)
222 unsigned char *command = us->iobuf;
224 command[0] = 0x40;
225 command[1] = USBAT_CMD_SET_FEAT;
228 * The only bit relevant to ATA access is bit 6
229 * which defines 8 bit data access (set) or 16 bit (unset)
231 command[2] = epp_control;
234 * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
235 * ET1 and ET2 define an external event to be checked for on event of a
236 * _read_blocks or _write_blocks operation. The read/write will not take
237 * place unless the defined trigger signal is active.
239 command[3] = external_trigger;
242 * The resultant byte of the mask operation (see mask_byte) is compared for
243 * equivalence with this test pattern. If equal, the read/write will take
244 * place.
246 command[4] = test_pattern;
249 * This value is logically ANDed with the status register field specified
250 * in the read/write command.
252 command[5] = mask_byte;
255 * If ALQ is set in the qualifier, this field contains the address of the
256 * registers where the byte count should be read for transferring the data.
257 * If ALQ is not set, then this field contains the number of bytes to be
258 * transferred.
260 command[6] = subcountL;
261 command[7] = subcountH;
263 return usbat_execute_command(us, command, 8);
267 * Block, waiting for an ATA device to become not busy or to report
268 * an error condition.
270 static int usbat_wait_not_busy(struct us_data *us, int minutes)
272 int i;
273 int result;
274 unsigned char *status = us->iobuf;
276 /* Synchronizing cache on a CDR could take a heck of a long time,
277 * but probably not more than 10 minutes or so. On the other hand,
278 * doing a full blank on a CDRW at speed 1 will take about 75
279 * minutes!
282 for (i=0; i<1200+minutes*60; i++) {
284 result = usbat_get_status(us, status);
286 if (result!=USB_STOR_XFER_GOOD)
287 return USB_STOR_TRANSPORT_ERROR;
288 if (*status & 0x01) { /* check condition */
289 result = usbat_read(us, USBAT_ATA, 0x10, status);
290 return USB_STOR_TRANSPORT_FAILED;
292 if (*status & 0x20) /* device fault */
293 return USB_STOR_TRANSPORT_FAILED;
295 if ((*status & 0x80)==0x00) { /* not busy */
296 US_DEBUGP("Waited not busy for %d steps\n", i);
297 return USB_STOR_TRANSPORT_GOOD;
300 if (i<500)
301 msleep(10); /* 5 seconds */
302 else if (i<700)
303 msleep(50); /* 10 seconds */
304 else if (i<1200)
305 msleep(100); /* 50 seconds */
306 else
307 msleep(1000); /* X minutes */
310 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
311 minutes);
312 return USB_STOR_TRANSPORT_FAILED;
316 * Read block data from the data register
318 static int usbat_read_block(struct us_data *us,
319 unsigned char *content,
320 unsigned short len)
322 int result;
323 unsigned char *command = us->iobuf;
325 if (!len)
326 return USB_STOR_TRANSPORT_GOOD;
328 command[0] = 0xC0;
329 command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
330 command[2] = USBAT_ATA_DATA;
331 command[3] = 0;
332 command[4] = 0;
333 command[5] = 0;
334 command[6] = LSB_of(len);
335 command[7] = MSB_of(len);
337 result = usbat_execute_command(us, command, 8);
338 if (result != USB_STOR_XFER_GOOD)
339 return USB_STOR_TRANSPORT_ERROR;
341 result = usbat_bulk_read(us, content, len);
342 return (result == USB_STOR_XFER_GOOD ?
343 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
347 * Write block data via the data register
349 static int usbat_write_block(struct us_data *us,
350 unsigned char access,
351 unsigned char *content,
352 unsigned short len,
353 int minutes)
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);
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);
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);
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)
612 int result;
613 unsigned char *command = us->iobuf;
615 command[0] = 0xC0;
616 command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK;
617 command[2] = USBAT_ATA_DATA;
618 command[3] = USBAT_ATA_STATUS;
619 command[4] = 0xFD; /* Timeout (ms); */
620 command[5] = USBAT_QUAL_FCQ;
621 command[6] = LSB_of(len);
622 command[7] = MSB_of(len);
624 /* Multiple block read setup command */
625 result = usbat_execute_command(us, command, 8);
626 if (result != USB_STOR_XFER_GOOD)
627 return USB_STOR_TRANSPORT_FAILED;
629 /* Read the blocks we just asked for */
630 result = usbat_bulk_read(us, buffer, len);
631 if (result != USB_STOR_XFER_GOOD)
632 return USB_STOR_TRANSPORT_FAILED;
634 return USB_STOR_TRANSPORT_GOOD;
638 * Conditionally write blocks to device:
639 * Allows us to write blocks to a specific data register, based upon the
640 * condition that a status register can be successfully masked with a status
641 * qualifier. If this condition is not initially met, the write will wait
642 * up until a maximum amount of time has elapsed, as specified by timeout.
643 * The read will start when the condition is met, otherwise the command aborts.
645 * The qualifier defined here is not the value that is masked, it defines
646 * conditions for the write to take place. The actual masked qualifier (and
647 * other related details) are defined beforehand with _set_shuttle_features().
649 static int usbat_write_blocks(struct us_data *us,
650 unsigned char *buffer,
651 int len)
653 int result;
654 unsigned char *command = us->iobuf;
656 command[0] = 0x40;
657 command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK;
658 command[2] = USBAT_ATA_DATA;
659 command[3] = USBAT_ATA_STATUS;
660 command[4] = 0xFD; /* Timeout (ms) */
661 command[5] = USBAT_QUAL_FCQ;
662 command[6] = LSB_of(len);
663 command[7] = MSB_of(len);
665 /* Multiple block write setup command */
666 result = usbat_execute_command(us, command, 8);
667 if (result != USB_STOR_XFER_GOOD)
668 return USB_STOR_TRANSPORT_FAILED;
670 /* Write the data */
671 result = usbat_bulk_write(us, buffer, len);
672 if (result != USB_STOR_XFER_GOOD)
673 return USB_STOR_TRANSPORT_FAILED;
675 return USB_STOR_TRANSPORT_GOOD;
679 * Read the User IO register
681 static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
683 int result;
685 result = usb_stor_ctrl_transfer(us,
686 us->recv_ctrl_pipe,
687 USBAT_CMD_UIO,
688 0xC0,
691 data_flags,
692 USBAT_UIO_READ);
694 US_DEBUGP("usbat_read_user_io: UIO register reads %02X\n", (unsigned short) (*data_flags));
696 return result;
700 * Write to the User IO register
702 static int usbat_write_user_io(struct us_data *us,
703 unsigned char enable_flags,
704 unsigned char data_flags)
706 return usb_stor_ctrl_transfer(us,
707 us->send_ctrl_pipe,
708 USBAT_CMD_UIO,
709 0x40,
710 short_pack(enable_flags, data_flags),
712 NULL,
713 USBAT_UIO_WRITE);
717 * Reset the device
718 * Often needed on media change.
720 static int usbat_device_reset(struct us_data *us)
722 int rc;
725 * Reset peripheral, enable peripheral control signals
726 * (bring reset signal up)
728 rc = usbat_write_user_io(us,
729 USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
730 USBAT_UIO_EPAD | USBAT_UIO_1);
731 if (rc != USB_STOR_XFER_GOOD)
732 return USB_STOR_TRANSPORT_ERROR;
735 * Enable peripheral control signals
736 * (bring reset signal down)
738 rc = usbat_write_user_io(us,
739 USBAT_UIO_OE1 | USBAT_UIO_OE0,
740 USBAT_UIO_EPAD | USBAT_UIO_1);
741 if (rc != USB_STOR_XFER_GOOD)
742 return USB_STOR_TRANSPORT_ERROR;
744 return USB_STOR_TRANSPORT_GOOD;
748 * Enable card detect
750 static int usbat_device_enable_cdt(struct us_data *us)
752 int rc;
754 /* Enable peripheral control signals and card detect */
755 rc = usbat_write_user_io(us,
756 USBAT_UIO_ACKD | USBAT_UIO_OE1 | USBAT_UIO_OE0,
757 USBAT_UIO_EPAD | USBAT_UIO_1);
758 if (rc != USB_STOR_XFER_GOOD)
759 return USB_STOR_TRANSPORT_ERROR;
761 return USB_STOR_TRANSPORT_GOOD;
765 * Determine if media is present.
767 static int usbat_flash_check_media_present(unsigned char *uio)
769 if (*uio & USBAT_UIO_UI0) {
770 US_DEBUGP("usbat_flash_check_media_present: no media detected\n");
771 return USBAT_FLASH_MEDIA_NONE;
774 return USBAT_FLASH_MEDIA_CF;
778 * Determine if media has changed since last operation
780 static int usbat_flash_check_media_changed(unsigned char *uio)
782 if (*uio & USBAT_UIO_0) {
783 US_DEBUGP("usbat_flash_check_media_changed: media change detected\n");
784 return USBAT_FLASH_MEDIA_CHANGED;
787 return USBAT_FLASH_MEDIA_SAME;
791 * Check for media change / no media and handle the situation appropriately
793 static int usbat_flash_check_media(struct us_data *us,
794 struct usbat_info *info)
796 int rc;
797 unsigned char *uio = us->iobuf;
799 rc = usbat_read_user_io(us, uio);
800 if (rc != USB_STOR_XFER_GOOD)
801 return USB_STOR_TRANSPORT_ERROR;
803 /* Check for media existence */
804 rc = usbat_flash_check_media_present(uio);
805 if (rc == USBAT_FLASH_MEDIA_NONE) {
806 info->sense_key = 0x02;
807 info->sense_asc = 0x3A;
808 info->sense_ascq = 0x00;
809 return USB_STOR_TRANSPORT_FAILED;
812 /* Check for media change */
813 rc = usbat_flash_check_media_changed(uio);
814 if (rc == USBAT_FLASH_MEDIA_CHANGED) {
816 /* Reset and re-enable card detect */
817 rc = usbat_device_reset(us);
818 if (rc != USB_STOR_TRANSPORT_GOOD)
819 return rc;
820 rc = usbat_device_enable_cdt(us);
821 if (rc != USB_STOR_TRANSPORT_GOOD)
822 return rc;
824 msleep(50);
826 rc = usbat_read_user_io(us, uio);
827 if (rc != USB_STOR_XFER_GOOD)
828 return USB_STOR_TRANSPORT_ERROR;
830 info->sense_key = UNIT_ATTENTION;
831 info->sense_asc = 0x28;
832 info->sense_ascq = 0x00;
833 return USB_STOR_TRANSPORT_FAILED;
836 return USB_STOR_TRANSPORT_GOOD;
840 * Determine whether we are controlling a flash-based reader/writer,
841 * or a HP8200-based CD drive.
842 * Sets transport functions as appropriate.
844 static int usbat_identify_device(struct us_data *us,
845 struct usbat_info *info)
847 int rc;
848 unsigned char status;
850 if (!us || !info)
851 return USB_STOR_TRANSPORT_ERROR;
853 rc = usbat_device_reset(us);
854 if (rc != USB_STOR_TRANSPORT_GOOD)
855 return rc;
856 msleep(25);
859 * In attempt to distinguish between HP CDRW's and Flash readers, we now
860 * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
861 * readers), this command should fail with error. On ATAPI devices (i.e.
862 * CDROM drives), it should succeed.
864 rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1);
865 if (rc != USB_STOR_XFER_GOOD)
866 return USB_STOR_TRANSPORT_ERROR;
868 rc = usbat_get_status(us, &status);
869 if (rc != USB_STOR_XFER_GOOD)
870 return USB_STOR_TRANSPORT_ERROR;
872 /* Check for error bit, or if the command 'fell through' */
873 if (status == 0xA1 || !(status & 0x01)) {
874 /* Device is HP 8200 */
875 US_DEBUGP("usbat_identify_device: Detected HP8200 CDRW\n");
876 info->devicetype = USBAT_DEV_HP8200;
877 } else {
878 /* Device is a CompactFlash reader/writer */
879 US_DEBUGP("usbat_identify_device: Detected Flash reader/writer\n");
880 info->devicetype = USBAT_DEV_FLASH;
883 return USB_STOR_TRANSPORT_GOOD;
887 * Set the transport function based on the device type
889 static int usbat_set_transport(struct us_data *us,
890 struct usbat_info *info)
892 int rc;
894 if (!info->devicetype) {
895 rc = usbat_identify_device(us, info);
896 if (rc != USB_STOR_TRANSPORT_GOOD) {
897 US_DEBUGP("usbat_set_transport: Could not identify device\n");
898 return 1;
902 if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
903 us->transport = usbat_hp8200e_transport;
904 else if (usbat_get_device_type(us) == USBAT_DEV_FLASH)
905 us->transport = usbat_flash_transport;
907 return 0;
911 * Read the media capacity
913 static int usbat_flash_get_sector_count(struct us_data *us,
914 struct usbat_info *info)
916 unsigned char registers[3] = {
917 USBAT_ATA_SECCNT,
918 USBAT_ATA_DEVICE,
919 USBAT_ATA_CMD,
921 unsigned char command[3] = { 0x01, 0xA0, 0xEC };
922 unsigned char *reply;
923 unsigned char status;
924 int rc;
926 if (!us || !info)
927 return USB_STOR_TRANSPORT_ERROR;
929 reply = kmalloc(512, GFP_NOIO);
930 if (!reply)
931 return USB_STOR_TRANSPORT_ERROR;
933 /* ATA command : IDENTIFY DEVICE */
934 rc = usbat_multiple_write(us, registers, command, 3);
935 if (rc != USB_STOR_XFER_GOOD) {
936 US_DEBUGP("usbat_flash_get_sector_count: Gah! identify_device failed\n");
937 rc = USB_STOR_TRANSPORT_ERROR;
938 goto leave;
941 /* Read device status */
942 if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
943 rc = USB_STOR_TRANSPORT_ERROR;
944 goto leave;
947 msleep(100);
949 /* Read the device identification data */
950 rc = usbat_read_block(us, reply, 512);
951 if (rc != USB_STOR_TRANSPORT_GOOD)
952 goto leave;
954 info->sectors = ((u32)(reply[117]) << 24) |
955 ((u32)(reply[116]) << 16) |
956 ((u32)(reply[115]) << 8) |
957 ((u32)(reply[114]) );
959 rc = USB_STOR_TRANSPORT_GOOD;
961 leave:
962 kfree(reply);
963 return rc;
967 * Read data from device
969 static int usbat_flash_read_data(struct us_data *us,
970 struct usbat_info *info,
971 u32 sector,
972 u32 sectors)
974 unsigned char registers[7] = {
975 USBAT_ATA_FEATURES,
976 USBAT_ATA_SECCNT,
977 USBAT_ATA_SECNUM,
978 USBAT_ATA_LBA_ME,
979 USBAT_ATA_LBA_HI,
980 USBAT_ATA_DEVICE,
981 USBAT_ATA_STATUS,
983 unsigned char command[7];
984 unsigned char *buffer;
985 unsigned char thistime;
986 unsigned int totallen, alloclen;
987 int len, result;
988 unsigned int sg_idx = 0, sg_offset = 0;
990 result = usbat_flash_check_media(us, info);
991 if (result != USB_STOR_TRANSPORT_GOOD)
992 return result;
995 * we're working in LBA mode. according to the ATA spec,
996 * we can support up to 28-bit addressing. I don't know if Jumpshot
997 * supports beyond 24-bit addressing. It's kind of hard to test
998 * since it requires > 8GB CF card.
1001 if (sector > 0x0FFFFFFF)
1002 return USB_STOR_TRANSPORT_ERROR;
1004 totallen = sectors * info->ssize;
1007 * Since we don't read more than 64 KB at a time, we have to create
1008 * a bounce buffer and move the data a piece at a time between the
1009 * bounce buffer and the actual transfer buffer.
1012 alloclen = min(totallen, 65536u);
1013 buffer = kmalloc(alloclen, GFP_NOIO);
1014 if (buffer == NULL)
1015 return USB_STOR_TRANSPORT_ERROR;
1017 do {
1019 * loop, never allocate or transfer more than 64k at once
1020 * (min(128k, 255*info->ssize) is the real limit)
1022 len = min(totallen, alloclen);
1023 thistime = (len / info->ssize) & 0xff;
1025 /* ATA command 0x20 (READ SECTORS) */
1026 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
1028 /* Write/execute ATA read command */
1029 result = usbat_multiple_write(us, registers, command, 7);
1030 if (result != USB_STOR_TRANSPORT_GOOD)
1031 goto leave;
1033 /* Read the data we just requested */
1034 result = usbat_read_blocks(us, buffer, len);
1035 if (result != USB_STOR_TRANSPORT_GOOD)
1036 goto leave;
1038 US_DEBUGP("usbat_flash_read_data: %d bytes\n", len);
1040 /* Store the data in the transfer buffer */
1041 usb_stor_access_xfer_buf(buffer, len, us->srb,
1042 &sg_idx, &sg_offset, TO_XFER_BUF);
1044 sector += thistime;
1045 totallen -= len;
1046 } while (totallen > 0);
1048 kfree(buffer);
1049 return USB_STOR_TRANSPORT_GOOD;
1051 leave:
1052 kfree(buffer);
1053 return USB_STOR_TRANSPORT_ERROR;
1057 * Write data to device
1059 static int usbat_flash_write_data(struct us_data *us,
1060 struct usbat_info *info,
1061 u32 sector,
1062 u32 sectors)
1064 unsigned char registers[7] = {
1065 USBAT_ATA_FEATURES,
1066 USBAT_ATA_SECCNT,
1067 USBAT_ATA_SECNUM,
1068 USBAT_ATA_LBA_ME,
1069 USBAT_ATA_LBA_HI,
1070 USBAT_ATA_DEVICE,
1071 USBAT_ATA_STATUS,
1073 unsigned char command[7];
1074 unsigned char *buffer;
1075 unsigned char thistime;
1076 unsigned int totallen, alloclen;
1077 int len, result;
1078 unsigned int sg_idx = 0, sg_offset = 0;
1080 result = usbat_flash_check_media(us, info);
1081 if (result != USB_STOR_TRANSPORT_GOOD)
1082 return result;
1085 * we're working in LBA mode. according to the ATA spec,
1086 * we can support up to 28-bit addressing. I don't know if the device
1087 * supports beyond 24-bit addressing. It's kind of hard to test
1088 * since it requires > 8GB media.
1091 if (sector > 0x0FFFFFFF)
1092 return USB_STOR_TRANSPORT_ERROR;
1094 totallen = sectors * info->ssize;
1097 * Since we don't write more than 64 KB at a time, we have to create
1098 * a bounce buffer and move the data a piece at a time between the
1099 * bounce buffer and the actual transfer buffer.
1102 alloclen = min(totallen, 65536u);
1103 buffer = kmalloc(alloclen, GFP_NOIO);
1104 if (buffer == NULL)
1105 return USB_STOR_TRANSPORT_ERROR;
1107 do {
1109 * loop, never allocate or transfer more than 64k at once
1110 * (min(128k, 255*info->ssize) is the real limit)
1112 len = min(totallen, alloclen);
1113 thistime = (len / info->ssize) & 0xff;
1115 /* Get the data from the transfer buffer */
1116 usb_stor_access_xfer_buf(buffer, len, us->srb,
1117 &sg_idx, &sg_offset, FROM_XFER_BUF);
1119 /* ATA command 0x30 (WRITE SECTORS) */
1120 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
1122 /* Write/execute ATA write command */
1123 result = usbat_multiple_write(us, registers, command, 7);
1124 if (result != USB_STOR_TRANSPORT_GOOD)
1125 goto leave;
1127 /* Write the data */
1128 result = usbat_write_blocks(us, buffer, len);
1129 if (result != USB_STOR_TRANSPORT_GOOD)
1130 goto leave;
1132 sector += thistime;
1133 totallen -= len;
1134 } while (totallen > 0);
1136 kfree(buffer);
1137 return result;
1139 leave:
1140 kfree(buffer);
1141 return USB_STOR_TRANSPORT_ERROR;
1145 * Squeeze a potentially huge (> 65535 byte) read10 command into
1146 * a little ( <= 65535 byte) ATAPI pipe
1148 static int usbat_hp8200e_handle_read10(struct us_data *us,
1149 unsigned char *registers,
1150 unsigned char *data,
1151 struct scsi_cmnd *srb)
1153 int result = USB_STOR_TRANSPORT_GOOD;
1154 unsigned char *buffer;
1155 unsigned int len;
1156 unsigned int sector;
1157 unsigned int sg_segment = 0;
1158 unsigned int sg_offset = 0;
1160 US_DEBUGP("handle_read10: transfersize %d\n",
1161 srb->transfersize);
1163 if (srb->request_bufflen < 0x10000) {
1165 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1166 registers, data, 19,
1167 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1168 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1169 DMA_FROM_DEVICE,
1170 srb->request_buffer,
1171 srb->request_bufflen, srb->use_sg, 1);
1173 return result;
1177 * Since we're requesting more data than we can handle in
1178 * a single read command (max is 64k-1), we will perform
1179 * multiple reads, but each read must be in multiples of
1180 * a sector. Luckily the sector size is in srb->transfersize
1181 * (see linux/drivers/scsi/sr.c).
1184 if (data[7+0] == GPCMD_READ_CD) {
1185 len = short_pack(data[7+9], data[7+8]);
1186 len <<= 16;
1187 len |= data[7+7];
1188 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
1189 srb->transfersize = srb->request_bufflen/len;
1192 if (!srb->transfersize) {
1193 srb->transfersize = 2048; /* A guess */
1194 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
1195 srb->transfersize);
1199 * Since we only read in one block at a time, we have to create
1200 * a bounce buffer and move the data a piece at a time between the
1201 * bounce buffer and the actual transfer buffer.
1204 len = (65535/srb->transfersize) * srb->transfersize;
1205 US_DEBUGP("Max read is %d bytes\n", len);
1206 len = min(len, srb->request_bufflen);
1207 buffer = kmalloc(len, GFP_NOIO);
1208 if (buffer == NULL) /* bloody hell! */
1209 return USB_STOR_TRANSPORT_FAILED;
1210 sector = short_pack(data[7+3], data[7+2]);
1211 sector <<= 16;
1212 sector |= short_pack(data[7+5], data[7+4]);
1213 transferred = 0;
1215 sg_segment = 0; /* for keeping track of where we are in */
1216 sg_offset = 0; /* the scatter/gather list */
1218 while (transferred != srb->request_bufflen) {
1220 if (len > srb->request_bufflen - transferred)
1221 len = srb->request_bufflen - transferred;
1223 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1224 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
1226 /* Fix up the SCSI command sector and num sectors */
1228 data[7+2] = MSB_of(sector>>16); /* SCSI command sector */
1229 data[7+3] = LSB_of(sector>>16);
1230 data[7+4] = MSB_of(sector&0xFFFF);
1231 data[7+5] = LSB_of(sector&0xFFFF);
1232 if (data[7+0] == GPCMD_READ_CD)
1233 data[7+6] = 0;
1234 data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */
1235 data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */
1237 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1238 registers, data, 19,
1239 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1240 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1241 DMA_FROM_DEVICE,
1242 buffer,
1243 len, 0, 1);
1245 if (result != USB_STOR_TRANSPORT_GOOD)
1246 break;
1248 /* Store the data in the transfer buffer */
1249 usb_stor_access_xfer_buf(buffer, len, srb,
1250 &sg_segment, &sg_offset, TO_XFER_BUF);
1252 /* Update the amount transferred and the sector number */
1254 transferred += len;
1255 sector += len / srb->transfersize;
1257 } /* while transferred != srb->request_bufflen */
1259 kfree(buffer);
1260 return result;
1263 static int usbat_select_and_test_registers(struct us_data *us)
1265 int selector;
1266 unsigned char *status = us->iobuf;
1268 /* try device = master, then device = slave. */
1269 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
1270 if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
1271 USB_STOR_XFER_GOOD)
1272 return USB_STOR_TRANSPORT_ERROR;
1274 if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) !=
1275 USB_STOR_XFER_GOOD)
1276 return USB_STOR_TRANSPORT_ERROR;
1278 if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) !=
1279 USB_STOR_XFER_GOOD)
1280 return USB_STOR_TRANSPORT_ERROR;
1282 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1283 USB_STOR_XFER_GOOD)
1284 return USB_STOR_TRANSPORT_ERROR;
1286 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1287 USB_STOR_XFER_GOOD)
1288 return USB_STOR_TRANSPORT_ERROR;
1290 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) !=
1291 USB_STOR_XFER_GOOD)
1292 return USB_STOR_TRANSPORT_ERROR;
1294 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) !=
1295 USB_STOR_XFER_GOOD)
1296 return USB_STOR_TRANSPORT_ERROR;
1298 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1299 USB_STOR_XFER_GOOD)
1300 return USB_STOR_TRANSPORT_ERROR;
1302 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1303 USB_STOR_XFER_GOOD)
1304 return USB_STOR_TRANSPORT_ERROR;
1307 return USB_STOR_TRANSPORT_GOOD;
1311 * Initialize the USBAT processor and the storage device
1313 int init_usbat(struct us_data *us)
1315 int rc;
1316 struct usbat_info *info;
1317 unsigned char subcountH = USBAT_ATA_LBA_HI;
1318 unsigned char subcountL = USBAT_ATA_LBA_ME;
1319 unsigned char *status = us->iobuf;
1321 us->extra = kmalloc(sizeof(struct usbat_info), GFP_NOIO);
1322 if (!us->extra) {
1323 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1324 return 1;
1326 memset(us->extra, 0, sizeof(struct usbat_info));
1327 info = (struct usbat_info *) (us->extra);
1329 /* Enable peripheral control signals */
1330 rc = usbat_write_user_io(us,
1331 USBAT_UIO_OE1 | USBAT_UIO_OE0,
1332 USBAT_UIO_EPAD | USBAT_UIO_1);
1333 if (rc != USB_STOR_XFER_GOOD)
1334 return USB_STOR_TRANSPORT_ERROR;
1336 US_DEBUGP("INIT 1\n");
1338 msleep(2000);
1340 rc = usbat_read_user_io(us, status);
1341 if (rc != USB_STOR_TRANSPORT_GOOD)
1342 return rc;
1344 US_DEBUGP("INIT 2\n");
1346 rc = usbat_read_user_io(us, status);
1347 if (rc != USB_STOR_XFER_GOOD)
1348 return USB_STOR_TRANSPORT_ERROR;
1350 rc = usbat_read_user_io(us, status);
1351 if (rc != USB_STOR_XFER_GOOD)
1352 return USB_STOR_TRANSPORT_ERROR;
1354 US_DEBUGP("INIT 3\n");
1356 rc = usbat_select_and_test_registers(us);
1357 if (rc != USB_STOR_TRANSPORT_GOOD)
1358 return rc;
1360 US_DEBUGP("INIT 4\n");
1362 rc = usbat_read_user_io(us, status);
1363 if (rc != USB_STOR_XFER_GOOD)
1364 return USB_STOR_TRANSPORT_ERROR;
1366 US_DEBUGP("INIT 5\n");
1368 /* Enable peripheral control signals and card detect */
1369 rc = usbat_device_enable_cdt(us);
1370 if (rc != USB_STOR_TRANSPORT_GOOD)
1371 return rc;
1373 US_DEBUGP("INIT 6\n");
1375 rc = usbat_read_user_io(us, status);
1376 if (rc != USB_STOR_XFER_GOOD)
1377 return USB_STOR_TRANSPORT_ERROR;
1379 US_DEBUGP("INIT 7\n");
1381 msleep(1400);
1383 rc = usbat_read_user_io(us, status);
1384 if (rc != USB_STOR_XFER_GOOD)
1385 return USB_STOR_TRANSPORT_ERROR;
1387 US_DEBUGP("INIT 8\n");
1389 rc = usbat_select_and_test_registers(us);
1390 if (rc != USB_STOR_TRANSPORT_GOOD)
1391 return rc;
1393 US_DEBUGP("INIT 9\n");
1395 /* At this point, we need to detect which device we are using */
1396 if (usbat_set_transport(us, info))
1397 return USB_STOR_TRANSPORT_ERROR;
1399 US_DEBUGP("INIT 10\n");
1401 if (usbat_get_device_type(us) == USBAT_DEV_FLASH) {
1402 subcountH = 0x02;
1403 subcountL = 0x00;
1405 rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1406 0x00, 0x88, 0x08, subcountH, subcountL);
1407 if (rc != USB_STOR_XFER_GOOD)
1408 return USB_STOR_TRANSPORT_ERROR;
1410 US_DEBUGP("INIT 11\n");
1412 return USB_STOR_TRANSPORT_GOOD;
1416 * Transport for the HP 8200e
1418 static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1420 int result;
1421 unsigned char *status = us->iobuf;
1422 unsigned char registers[32];
1423 unsigned char data[32];
1424 unsigned int len;
1425 int i;
1426 char string[64];
1428 len = srb->request_bufflen;
1430 /* Send A0 (ATA PACKET COMMAND).
1431 Note: I guess we're never going to get any of the ATA
1432 commands... just ATA Packet Commands.
1435 registers[0] = USBAT_ATA_FEATURES;
1436 registers[1] = USBAT_ATA_SECCNT;
1437 registers[2] = USBAT_ATA_SECNUM;
1438 registers[3] = USBAT_ATA_LBA_ME;
1439 registers[4] = USBAT_ATA_LBA_HI;
1440 registers[5] = USBAT_ATA_DEVICE;
1441 registers[6] = USBAT_ATA_CMD;
1442 data[0] = 0x00;
1443 data[1] = 0x00;
1444 data[2] = 0x00;
1445 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1446 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
1447 data[5] = 0xB0; /* (device sel) = slave */
1448 data[6] = 0xA0; /* (command) = ATA PACKET COMMAND */
1450 for (i=7; i<19; i++) {
1451 registers[i] = 0x10;
1452 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1455 result = usbat_get_status(us, status);
1456 US_DEBUGP("Status = %02X\n", *status);
1457 if (result != USB_STOR_XFER_GOOD)
1458 return USB_STOR_TRANSPORT_ERROR;
1459 if (srb->cmnd[0] == TEST_UNIT_READY)
1460 transferred = 0;
1462 if (srb->sc_data_direction == DMA_TO_DEVICE) {
1464 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1465 registers, data, 19,
1466 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1467 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1468 DMA_TO_DEVICE,
1469 srb->request_buffer,
1470 len, srb->use_sg, 10);
1472 if (result == USB_STOR_TRANSPORT_GOOD) {
1473 transferred += len;
1474 US_DEBUGP("Wrote %08X bytes\n", transferred);
1477 return result;
1479 } else if (srb->cmnd[0] == READ_10 ||
1480 srb->cmnd[0] == GPCMD_READ_CD) {
1482 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1486 if (len > 0xFFFF) {
1487 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1488 len);
1489 return USB_STOR_TRANSPORT_ERROR;
1492 if ( (result = usbat_multiple_write(us,
1493 registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
1494 return result;
1498 * Write the 12-byte command header.
1500 * If the command is BLANK then set the timer for 75 minutes.
1501 * Otherwise set it for 10 minutes.
1503 * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1504 * AT SPEED 4 IS UNRELIABLE!!!
1507 if ( (result = usbat_write_block(us,
1508 USBAT_ATA, srb->cmnd, 12,
1509 srb->cmnd[0]==GPCMD_BLANK ? 75 : 10)) !=
1510 USB_STOR_TRANSPORT_GOOD) {
1511 return result;
1514 /* If there is response data to be read in then do it here. */
1516 if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1518 /* How many bytes to read in? Check cylL register */
1520 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1521 USB_STOR_XFER_GOOD) {
1522 return USB_STOR_TRANSPORT_ERROR;
1525 if (len > 0xFF) { /* need to read cylH also */
1526 len = *status;
1527 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1528 USB_STOR_XFER_GOOD) {
1529 return USB_STOR_TRANSPORT_ERROR;
1531 len += ((unsigned int) *status)<<8;
1533 else
1534 len = *status;
1537 result = usbat_read_block(us, srb->request_buffer, len);
1539 /* Debug-print the first 32 bytes of the transfer */
1541 if (!srb->use_sg) {
1542 string[0] = 0;
1543 for (i=0; i<len && i<32; i++) {
1544 sprintf(string+strlen(string), "%02X ",
1545 ((unsigned char *)srb->request_buffer)[i]);
1546 if ((i%16)==15) {
1547 US_DEBUGP("%s\n", string);
1548 string[0] = 0;
1551 if (string[0]!=0)
1552 US_DEBUGP("%s\n", string);
1556 return result;
1560 * Transport for USBAT02-based CompactFlash and similar storage devices
1562 static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1564 int rc;
1565 struct usbat_info *info = (struct usbat_info *) (us->extra);
1566 unsigned long block, blocks;
1567 unsigned char *ptr = us->iobuf;
1568 static unsigned char inquiry_response[36] = {
1569 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1572 if (srb->cmnd[0] == INQUIRY) {
1573 US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1574 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1575 fill_inquiry_response(us, ptr, 36);
1576 return USB_STOR_TRANSPORT_GOOD;
1579 if (srb->cmnd[0] == READ_CAPACITY) {
1580 rc = usbat_flash_check_media(us, info);
1581 if (rc != USB_STOR_TRANSPORT_GOOD)
1582 return rc;
1584 rc = usbat_flash_get_sector_count(us, info);
1585 if (rc != USB_STOR_TRANSPORT_GOOD)
1586 return rc;
1588 /* hard coded 512 byte sectors as per ATA spec */
1589 info->ssize = 0x200;
1590 US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1591 info->sectors, info->ssize);
1594 * build the reply
1595 * note: must return the sector number of the last sector,
1596 * *not* the total number of sectors
1598 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1599 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1600 usb_stor_set_xfer_buf(ptr, 8, srb);
1602 return USB_STOR_TRANSPORT_GOOD;
1605 if (srb->cmnd[0] == MODE_SELECT_10) {
1606 US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
1607 return USB_STOR_TRANSPORT_ERROR;
1610 if (srb->cmnd[0] == READ_10) {
1611 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1612 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1614 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1616 US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
1617 return usbat_flash_read_data(us, info, block, blocks);
1620 if (srb->cmnd[0] == READ_12) {
1622 * I don't think we'll ever see a READ_12 but support it anyway
1624 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1625 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1627 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1628 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1630 US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
1631 return usbat_flash_read_data(us, info, block, blocks);
1634 if (srb->cmnd[0] == WRITE_10) {
1635 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1636 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1638 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1640 US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
1641 return usbat_flash_write_data(us, info, block, blocks);
1644 if (srb->cmnd[0] == WRITE_12) {
1646 * I don't think we'll ever see a WRITE_12 but support it anyway
1648 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1649 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1651 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1652 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1654 US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
1655 return usbat_flash_write_data(us, info, block, blocks);
1659 if (srb->cmnd[0] == TEST_UNIT_READY) {
1660 US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
1662 rc = usbat_flash_check_media(us, info);
1663 if (rc != USB_STOR_TRANSPORT_GOOD)
1664 return rc;
1666 return usbat_check_status(us);
1669 if (srb->cmnd[0] == REQUEST_SENSE) {
1670 US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
1672 memset(ptr, 0, 18);
1673 ptr[0] = 0xF0;
1674 ptr[2] = info->sense_key;
1675 ptr[7] = 11;
1676 ptr[12] = info->sense_asc;
1677 ptr[13] = info->sense_ascq;
1678 usb_stor_set_xfer_buf(ptr, 18, srb);
1680 return USB_STOR_TRANSPORT_GOOD;
1683 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1685 * sure. whatever. not like we can stop the user from popping
1686 * the media out of the device (no locking doors, etc)
1688 return USB_STOR_TRANSPORT_GOOD;
1691 US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1692 srb->cmnd[0], srb->cmnd[0]);
1693 info->sense_key = 0x05;
1694 info->sense_asc = 0x20;
1695 info->sense_ascq = 0x00;
1696 return USB_STOR_TRANSPORT_FAILED;
1700 * Default transport function. Attempts to detect which transport function
1701 * should be called, makes it the new default, and calls it.
1703 * This function should never be called. Our usbat_init() function detects the
1704 * device type and changes the us->transport ptr to the transport function
1705 * relevant to the device.
1706 * However, we'll support this impossible(?) case anyway.
1708 int usbat_transport(struct scsi_cmnd *srb, struct us_data *us)
1710 struct usbat_info *info = (struct usbat_info*) (us->extra);
1712 if (usbat_set_transport(us, info))
1713 return USB_STOR_TRANSPORT_ERROR;
1715 return us->transport(srb, us);