1 /* Driver for SCM Microsystems USB-ATAPI cable
3 * $Id: shuttle_usbat.c,v 1.17 2002/04/22 03:39:43 mdharm Exp $
5 * Current development and maintenance by:
6 * (c) 2000, 2001 Robert Baruch (autophile@starband.net)
8 * Developed with the assistance of:
9 * (c) 2002 Alan Stern <stern@rowland.org>
11 * Many originally ATAPI devices were slightly modified to meet the USB
12 * market by using some kind of translation from ATAPI to USB on the host,
13 * and the peripheral would translate from USB back to ATAPI.
15 * SCM Microsystems (www.scmmicro.com) makes a device, sold to OEM's only,
16 * which does the USB-to-ATAPI conversion. By obtaining the data sheet on
17 * their device under nondisclosure agreement, I have been able to write
18 * this driver for Linux.
20 * The chip used in the device can also be used for EPP and ISA translation
21 * as well. This driver is only guaranteed to work with the ATAPI
24 * The only peripheral that I know of (as of 27 Mar 2001) that uses this
25 * device is the Hewlett-Packard 8200e/8210e/8230e CD-Writer Plus.
27 * This program is free software; you can redistribute it and/or modify it
28 * under the terms of the GNU General Public License as published by the
29 * Free Software Foundation; either version 2, or (at your option) any
32 * This program is distributed in the hope that it will be useful, but
33 * WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35 * General Public License for more details.
37 * You should have received a copy of the GNU General Public License along
38 * with this program; if not, write to the Free Software Foundation, Inc.,
39 * 675 Mass Ave, Cambridge, MA 02139, USA.
42 #include "transport.h"
46 #include "shuttle_usbat.h"
48 #include <linux/sched.h>
49 #include <linux/errno.h>
50 #include <linux/slab.h>
52 #define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
53 #define LSB_of(s) ((s)&0xFF)
54 #define MSB_of(s) ((s)>>8)
58 static int usbat_read(struct us_data *us,
61 unsigned char *content)
65 result = usb_stor_ctrl_transfer(us,
77 static int usbat_write(struct us_data *us,
80 unsigned char content)
84 result = usb_stor_ctrl_transfer(us,
88 short_pack(reg, content),
96 static int usbat_set_shuttle_features(struct us_data *us,
97 unsigned char external_trigger,
98 unsigned char epp_control,
99 unsigned char mask_byte,
100 unsigned char test_pattern,
101 unsigned char subcountH,
102 unsigned char subcountL)
105 unsigned char *command = us->iobuf;
109 command[2] = epp_control;
110 command[3] = external_trigger;
111 command[4] = test_pattern;
112 command[5] = mask_byte;
113 command[6] = subcountL;
114 command[7] = subcountH;
116 result = usb_stor_ctrl_transfer(us,
128 static int usbat_read_block(struct us_data *us,
129 unsigned char access,
131 unsigned char *content,
136 unsigned char *command = us->iobuf;
139 return USB_STOR_TRANSPORT_GOOD;
142 command[1] = access | 0x02;
147 command[6] = LSB_of(len);
148 command[7] = MSB_of(len);
150 result = usb_stor_ctrl_transfer(us,
159 if (result != USB_STOR_XFER_GOOD)
160 return USB_STOR_TRANSPORT_ERROR;
162 result = usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe,
163 content, len, use_sg, NULL);
165 return (result == USB_STOR_XFER_GOOD ?
166 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
170 * Block, waiting for an ATA device to become not busy or to report
171 * an error condition.
174 static int usbat_wait_not_busy(struct us_data *us, int minutes)
178 unsigned char *status = us->iobuf;
180 /* Synchronizing cache on a CDR could take a heck of a long time,
181 * but probably not more than 10 minutes or so. On the other hand,
182 * doing a full blank on a CDRW at speed 1 will take about 75
186 for (i=0; i<1200+minutes*60; i++) {
188 result = usbat_read(us, USBAT_ATA, 0x17, status);
190 if (result!=USB_STOR_XFER_GOOD)
191 return USB_STOR_TRANSPORT_ERROR;
192 if (*status & 0x01) { // check condition
193 result = usbat_read(us, USBAT_ATA, 0x10, status);
194 return USB_STOR_TRANSPORT_FAILED;
196 if (*status & 0x20) // device fault
197 return USB_STOR_TRANSPORT_FAILED;
199 if ((*status & 0x80)==0x00) { // not busy
200 US_DEBUGP("Waited not busy for %d steps\n", i);
201 return USB_STOR_TRANSPORT_GOOD;
205 msleep(10); // 5 seconds
207 msleep(50); // 10 seconds
209 msleep(100); // 50 seconds
211 msleep(1000); // X minutes
214 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
216 return USB_STOR_TRANSPORT_FAILED;
219 static int usbat_write_block(struct us_data *us,
220 unsigned char access,
222 unsigned char *content,
224 int use_sg, int minutes)
227 unsigned char *command = us->iobuf;
230 return USB_STOR_TRANSPORT_GOOD;
233 command[1] = access | 0x03;
238 command[6] = LSB_of(len);
239 command[7] = MSB_of(len);
241 result = usb_stor_ctrl_transfer(us,
250 if (result != USB_STOR_XFER_GOOD)
251 return USB_STOR_TRANSPORT_ERROR;
253 result = usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe,
254 content, len, use_sg, NULL);
256 if (result != USB_STOR_XFER_GOOD)
257 return USB_STOR_TRANSPORT_ERROR;
259 return usbat_wait_not_busy(us, minutes);
262 static int usbat_rw_block_test(struct us_data *us,
263 unsigned char access,
264 unsigned char *registers,
265 unsigned char *data_out,
266 unsigned short num_registers,
267 unsigned char data_reg,
268 unsigned char status_reg,
269 unsigned char timeout,
270 unsigned char qualifier,
272 unsigned char *content,
278 unsigned int pipe = (direction == SCSI_DATA_READ) ?
279 us->recv_bulk_pipe : us->send_bulk_pipe;
281 // Not really sure the 0x07, 0x17, 0xfc, 0xe7 is necessary here,
282 // but that's what came out of the trace every single time.
284 unsigned char *command = us->iobuf;
287 unsigned char *data = us->iobuf;
288 unsigned char *status = us->iobuf;
290 BUG_ON(num_registers > US_IOBUF_SIZE/2);
292 for (i=0; i<20; i++) {
295 * The first time we send the full command, which consists
296 * of downloading the SCSI command followed by downloading
297 * the data via a write-and-test. Any other time we only
298 * send the command to download the data -- the SCSI command
299 * is still 'active' in some sense in the device.
301 * We're only going to try sending the data 10 times. After
302 * that, we just return a failure.
308 command[1] = access | 0x07;
313 command[6] = LSB_of(num_registers*2);
314 command[7] = MSB_of(num_registers*2);
318 command[cmdlen-8] = (direction==SCSI_DATA_WRITE ? 0x40 : 0xC0);
319 command[cmdlen-7] = access |
320 (direction==SCSI_DATA_WRITE ? 0x05 : 0x04);
321 command[cmdlen-6] = data_reg;
322 command[cmdlen-5] = status_reg;
323 command[cmdlen-4] = timeout;
324 command[cmdlen-3] = qualifier;
325 command[cmdlen-2] = LSB_of(len);
326 command[cmdlen-1] = MSB_of(len);
328 result = usb_stor_ctrl_transfer(us,
337 if (result != USB_STOR_XFER_GOOD)
338 return USB_STOR_TRANSPORT_ERROR;
342 for (j=0; j<num_registers; j++) {
343 data[j<<1] = registers[j];
344 data[1+(j<<1)] = data_out[j];
347 result = usb_stor_bulk_transfer_buf(us,
349 data, num_registers*2, NULL);
351 if (result != USB_STOR_XFER_GOOD)
352 return USB_STOR_TRANSPORT_ERROR;
357 //US_DEBUGP("Transfer %s %d bytes, sg buffers %d\n",
358 // direction == SCSI_DATA_WRITE ? "out" : "in",
361 result = usb_stor_bulk_transfer_sg(us,
362 pipe, content, len, use_sg, NULL);
365 * If we get a stall on the bulk download, we'll retry
366 * the bulk download -- but not the SCSI command because
367 * in some sense the SCSI command is still 'active' and
368 * waiting for the data. Don't ask me why this should be;
369 * I'm only following what the Windoze driver did.
371 * Note that a stall for the test-and-read/write command means
372 * that the test failed. In this case we're testing to make
373 * sure that the device is error-free
374 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
375 * hypothesis is that the USBAT chip somehow knows what
376 * the device will accept, but doesn't give the device any
377 * data until all data is received. Thus, the device would
378 * still be waiting for the first byte of data if a stall
379 * occurs, even if the stall implies that some data was
383 if (result == USB_STOR_XFER_SHORT ||
384 result == USB_STOR_XFER_STALLED) {
387 * If we're reading and we stalled, then clear
388 * the bulk output pipe only the first time.
391 if (direction==SCSI_DATA_READ && i==0) {
392 if (usb_stor_clear_halt(us,
393 us->send_bulk_pipe) < 0)
394 return USB_STOR_TRANSPORT_ERROR;
398 * Read status: is the device angry, or just busy?
401 result = usbat_read(us, USBAT_ATA,
402 direction==SCSI_DATA_WRITE ? 0x17 : 0x0E,
405 if (result!=USB_STOR_XFER_GOOD)
406 return USB_STOR_TRANSPORT_ERROR;
407 if (*status & 0x01) // check condition
408 return USB_STOR_TRANSPORT_FAILED;
409 if (*status & 0x20) // device fault
410 return USB_STOR_TRANSPORT_FAILED;
412 US_DEBUGP("Redoing %s\n",
413 direction==SCSI_DATA_WRITE ? "write" : "read");
415 } else if (result != USB_STOR_XFER_GOOD)
416 return USB_STOR_TRANSPORT_ERROR;
418 return usbat_wait_not_busy(us, minutes);
422 US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
423 direction==SCSI_DATA_WRITE ? "Writing" : "Reading");
425 return USB_STOR_TRANSPORT_FAILED;
429 * Write data to multiple registers at once. Not meant for large
433 static int usbat_multiple_write(struct us_data *us,
434 unsigned char access,
435 unsigned char *registers,
436 unsigned char *data_out,
437 unsigned short num_registers)
440 unsigned char *data = us->iobuf;
442 unsigned char *command = us->iobuf;
444 BUG_ON(num_registers > US_IOBUF_SIZE/2);
447 command[1] = access | 0x07;
452 command[6] = LSB_of(num_registers*2);
453 command[7] = MSB_of(num_registers*2);
455 result = usb_stor_ctrl_transfer(us,
464 if (result != USB_STOR_XFER_GOOD)
465 return USB_STOR_TRANSPORT_ERROR;
467 for (i=0; i<num_registers; i++) {
468 data[i<<1] = registers[i];
469 data[1+(i<<1)] = data_out[i];
472 result = usb_stor_bulk_transfer_buf(us,
473 us->send_bulk_pipe, data, num_registers*2, NULL);
475 if (result != USB_STOR_XFER_GOOD)
476 return USB_STOR_TRANSPORT_ERROR;
478 return usbat_wait_not_busy(us, 0);
481 static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
485 result = usb_stor_ctrl_transfer(us,
497 static int usbat_write_user_io(struct us_data *us,
498 unsigned char enable_flags,
499 unsigned char data_flags)
503 result = usb_stor_ctrl_transfer(us,
507 short_pack(enable_flags, data_flags),
516 * Squeeze a potentially huge (> 65535 byte) read10 command into
517 * a little ( <= 65535 byte) ATAPI pipe
520 static int usbat_handle_read10(struct us_data *us,
521 unsigned char *registers,
525 int result = USB_STOR_TRANSPORT_GOOD;
526 unsigned char *buffer;
529 unsigned int sg_segment = 0;
530 unsigned int sg_offset = 0;
532 US_DEBUGP("handle_read10: transfersize %d\n",
535 if (srb->request_bufflen < 0x10000) {
537 result = usbat_rw_block_test(us, USBAT_ATA,
539 0x10, 0x17, 0xFD, 0x30,
542 srb->request_bufflen, srb->use_sg, 1);
548 * Since we're requesting more data than we can handle in
549 * a single read command (max is 64k-1), we will perform
550 * multiple reads, but each read must be in multiples of
551 * a sector. Luckily the sector size is in srb->transfersize
552 * (see linux/drivers/scsi/sr.c).
555 if (data[7+0] == GPCMD_READ_CD) {
556 len = short_pack(data[7+9], data[7+8]);
559 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
560 srb->transfersize = srb->request_bufflen/len;
563 if (!srb->transfersize) {
564 srb->transfersize = 2048; /* A guess */
565 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
569 // Since we only read in one block at a time, we have to create
570 // a bounce buffer and move the data a piece at a time between the
571 // bounce buffer and the actual transfer buffer.
573 len = (65535/srb->transfersize) * srb->transfersize;
574 US_DEBUGP("Max read is %d bytes\n", len);
575 len = min(len, srb->request_bufflen);
576 buffer = kmalloc(len, GFP_NOIO);
577 if (buffer == NULL) // bloody hell!
578 return USB_STOR_TRANSPORT_FAILED;
579 sector = short_pack(data[7+3], data[7+2]);
581 sector |= short_pack(data[7+5], data[7+4]);
584 sg_segment = 0; // for keeping track of where we are in
585 sg_offset = 0; // the scatter/gather list
587 while (transferred != srb->request_bufflen) {
589 if (len > srb->request_bufflen - transferred)
590 len = srb->request_bufflen - transferred;
592 data[3] = len&0xFF; // (cylL) = expected length (L)
593 data[4] = (len>>8)&0xFF; // (cylH) = expected length (H)
595 // Fix up the SCSI command sector and num sectors
597 data[7+2] = MSB_of(sector>>16); // SCSI command sector
598 data[7+3] = LSB_of(sector>>16);
599 data[7+4] = MSB_of(sector&0xFFFF);
600 data[7+5] = LSB_of(sector&0xFFFF);
601 if (data[7+0] == GPCMD_READ_CD)
603 data[7+7] = MSB_of(len / srb->transfersize); // SCSI command
604 data[7+8] = LSB_of(len / srb->transfersize); // num sectors
606 result = usbat_rw_block_test(us, USBAT_ATA,
608 0x10, 0x17, 0xFD, 0x30,
613 if (result != USB_STOR_TRANSPORT_GOOD)
616 // Store the data in the transfer buffer
617 usb_stor_access_xfer_buf(buffer, len, srb,
618 &sg_segment, &sg_offset, TO_XFER_BUF);
620 // Update the amount transferred and the sector number
623 sector += len / srb->transfersize;
625 } // while transferred != srb->request_bufflen
631 static int hp_8200e_select_and_test_registers(struct us_data *us)
634 unsigned char *status = us->iobuf;
636 // try device = master, then device = slave.
638 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
640 if (usbat_write(us, USBAT_ATA, 0x16, selector) !=
642 return USB_STOR_TRANSPORT_ERROR;
644 if (usbat_read(us, USBAT_ATA, 0x17, status) !=
646 return USB_STOR_TRANSPORT_ERROR;
648 if (usbat_read(us, USBAT_ATA, 0x16, status) !=
650 return USB_STOR_TRANSPORT_ERROR;
652 if (usbat_read(us, USBAT_ATA, 0x14, status) !=
654 return USB_STOR_TRANSPORT_ERROR;
656 if (usbat_read(us, USBAT_ATA, 0x15, status) !=
658 return USB_STOR_TRANSPORT_ERROR;
660 if (usbat_write(us, USBAT_ATA, 0x14, 0x55) !=
662 return USB_STOR_TRANSPORT_ERROR;
664 if (usbat_write(us, USBAT_ATA, 0x15, 0xAA) !=
666 return USB_STOR_TRANSPORT_ERROR;
668 if (usbat_read(us, USBAT_ATA, 0x14, status) !=
670 return USB_STOR_TRANSPORT_ERROR;
672 if (usbat_read(us, USBAT_ATA, 0x15, status) !=
674 return USB_STOR_TRANSPORT_ERROR;
677 return USB_STOR_TRANSPORT_GOOD;
680 int init_8200e(struct us_data *us)
683 unsigned char *status = us->iobuf;
685 // Enable peripheral control signals
687 if (usbat_write_user_io(us,
688 USBAT_UIO_OE1 | USBAT_UIO_OE0,
689 USBAT_UIO_EPAD | USBAT_UIO_1) != USB_STOR_XFER_GOOD)
690 return USB_STOR_TRANSPORT_ERROR;
692 US_DEBUGP("INIT 1\n");
696 if (usbat_read_user_io(us, status) !=
698 return USB_STOR_TRANSPORT_ERROR;
700 US_DEBUGP("INIT 2\n");
702 if (usbat_read_user_io(us, status) !=
704 return USB_STOR_TRANSPORT_ERROR;
706 US_DEBUGP("INIT 3\n");
708 // Reset peripheral, enable periph control signals
709 // (bring reset signal up)
711 if (usbat_write_user_io(us,
712 USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
713 USBAT_UIO_EPAD | USBAT_UIO_1) != USB_STOR_XFER_GOOD)
714 return USB_STOR_TRANSPORT_ERROR;
716 US_DEBUGP("INIT 4\n");
718 // Enable periph control signals
719 // (bring reset signal down)
721 if (usbat_write_user_io(us,
722 USBAT_UIO_OE1 | USBAT_UIO_OE0,
723 USBAT_UIO_EPAD | USBAT_UIO_1) != USB_STOR_XFER_GOOD)
724 return USB_STOR_TRANSPORT_ERROR;
726 US_DEBUGP("INIT 5\n");
730 // Write 0x80 to ISA port 0x3F
732 if (usbat_write(us, USBAT_ISA, 0x3F, 0x80) !=
734 return USB_STOR_TRANSPORT_ERROR;
736 US_DEBUGP("INIT 6\n");
738 // Read ISA port 0x27
740 if (usbat_read(us, USBAT_ISA, 0x27, status) !=
742 return USB_STOR_TRANSPORT_ERROR;
744 US_DEBUGP("INIT 7\n");
746 if (usbat_read_user_io(us, status) !=
748 return USB_STOR_TRANSPORT_ERROR;
750 US_DEBUGP("INIT 8\n");
752 if ( (result = hp_8200e_select_and_test_registers(us)) !=
753 USB_STOR_TRANSPORT_GOOD)
756 US_DEBUGP("INIT 9\n");
758 if (usbat_read_user_io(us, status) !=
760 return USB_STOR_TRANSPORT_ERROR;
762 US_DEBUGP("INIT 10\n");
764 // Enable periph control signals and card detect
766 if (usbat_write_user_io(us,
767 USBAT_UIO_ACKD |USBAT_UIO_OE1 | USBAT_UIO_OE0,
768 USBAT_UIO_EPAD | USBAT_UIO_1) != USB_STOR_XFER_GOOD)
769 return USB_STOR_TRANSPORT_ERROR;
771 US_DEBUGP("INIT 11\n");
773 if (usbat_read_user_io(us, status) !=
775 return USB_STOR_TRANSPORT_ERROR;
777 US_DEBUGP("INIT 12\n");
781 if (usbat_read_user_io(us, status) !=
783 return USB_STOR_TRANSPORT_ERROR;
785 US_DEBUGP("INIT 13\n");
787 if ( (result = hp_8200e_select_and_test_registers(us)) !=
788 USB_STOR_TRANSPORT_GOOD)
791 US_DEBUGP("INIT 14\n");
793 if (usbat_set_shuttle_features(us,
794 0x83, 0x00, 0x88, 0x08, 0x15, 0x14) !=
796 return USB_STOR_TRANSPORT_ERROR;
798 US_DEBUGP("INIT 15\n");
800 return USB_STOR_TRANSPORT_GOOD;
804 * Transport for the HP 8200e
806 int hp8200e_transport(Scsi_Cmnd *srb, struct us_data *us)
809 unsigned char *status = us->iobuf;
810 unsigned char registers[32];
811 unsigned char data[32];
816 len = srb->request_bufflen;
818 /* Send A0 (ATA PACKET COMMAND).
819 Note: I guess we're never going to get any of the ATA
820 commands... just ATA Packet Commands.
833 data[3] = len&0xFF; // (cylL) = expected length (L)
834 data[4] = (len>>8)&0xFF; // (cylH) = expected length (H)
835 data[5] = 0xB0; // (device sel) = slave
836 data[6] = 0xA0; // (command) = ATA PACKET COMMAND
838 for (i=7; i<19; i++) {
840 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
843 result = usbat_read(us, USBAT_ATA, 0x17, status);
844 US_DEBUGP("Status = %02X\n", *status);
845 if (result != USB_STOR_XFER_GOOD)
846 return USB_STOR_TRANSPORT_ERROR;
847 if (srb->cmnd[0] == TEST_UNIT_READY)
850 if (srb->sc_data_direction == SCSI_DATA_WRITE) {
852 result = usbat_rw_block_test(us, USBAT_ATA,
854 0x10, 0x17, 0xFD, 0x30,
857 len, srb->use_sg, 10);
859 if (result == USB_STOR_TRANSPORT_GOOD) {
861 US_DEBUGP("Wrote %08X bytes\n", transferred);
866 } else if (srb->cmnd[0] == READ_10 ||
867 srb->cmnd[0] == GPCMD_READ_CD) {
869 return usbat_handle_read10(us, registers, data, srb);
874 US_DEBUGP("Error: len = %08X... what do I do now?\n",
876 return USB_STOR_TRANSPORT_ERROR;
879 if ( (result = usbat_multiple_write(us,
881 registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
885 // Write the 12-byte command header.
887 // If the command is BLANK then set the timer for 75 minutes.
888 // Otherwise set it for 10 minutes.
890 // NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
891 // AT SPEED 4 IS UNRELIABLE!!!
893 if ( (result = usbat_write_block(us,
894 USBAT_ATA, 0x10, srb->cmnd, 12, 0,
895 srb->cmnd[0]==GPCMD_BLANK ? 75 : 10)) !=
896 USB_STOR_TRANSPORT_GOOD) {
900 // If there is response data to be read in
903 if (len != 0 && (srb->sc_data_direction == SCSI_DATA_READ)) {
905 // How many bytes to read in? Check cylL register
907 if (usbat_read(us, USBAT_ATA, 0x14, status) !=
908 USB_STOR_XFER_GOOD) {
909 return USB_STOR_TRANSPORT_ERROR;
912 if (len > 0xFF) { // need to read cylH also
914 if (usbat_read(us, USBAT_ATA, 0x15, status) !=
915 USB_STOR_XFER_GOOD) {
916 return USB_STOR_TRANSPORT_ERROR;
918 len += ((unsigned int) *status)<<8;
924 result = usbat_read_block(us, USBAT_ATA, 0x10,
925 srb->request_buffer, len, srb->use_sg);
927 /* Debug-print the first 32 bytes of the transfer */
931 for (i=0; i<len && i<32; i++) {
932 sprintf(string+strlen(string), "%02X ",
933 ((unsigned char *)srb->request_buffer)[i]);
935 US_DEBUGP("%s\n", string);
940 US_DEBUGP("%s\n", string);