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 int usbat_read(struct us_data *us,
61 unsigned char *content) {
65 result = usb_stor_ctrl_transfer(us,
77 int usbat_write(struct us_data *us,
80 unsigned char content) {
84 result = usb_stor_ctrl_transfer(us,
88 short_pack(reg, content),
96 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 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 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 wait_ms(10); // 5 seconds
207 wait_ms(50); // 10 seconds
209 wait_ms(100); // 50 seconds
211 wait_ms(1000); // X minutes
214 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
216 return USB_STOR_TRANSPORT_FAILED;
219 int usbat_write_block(struct us_data *us,
220 unsigned char access,
222 unsigned char *content,
228 unsigned char *command = us->iobuf;
231 return USB_STOR_TRANSPORT_GOOD;
234 command[1] = access | 0x03;
239 command[6] = LSB_of(len);
240 command[7] = MSB_of(len);
242 result = usb_stor_ctrl_transfer(us,
251 if (result != USB_STOR_XFER_GOOD)
252 return USB_STOR_TRANSPORT_ERROR;
254 result = usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe,
255 content, len, use_sg, NULL);
257 if (result != USB_STOR_XFER_GOOD)
258 return USB_STOR_TRANSPORT_ERROR;
260 return usbat_wait_not_busy(us, minutes);
263 int usbat_rw_block_test(struct us_data *us,
264 unsigned char access,
265 unsigned char *registers,
266 unsigned char *data_out,
267 unsigned short num_registers,
268 unsigned char data_reg,
269 unsigned char status_reg,
270 unsigned char timeout,
271 unsigned char qualifier,
273 unsigned char *content,
279 unsigned int pipe = (direction == SCSI_DATA_READ) ?
280 us->recv_bulk_pipe : us->send_bulk_pipe;
282 // Not really sure the 0x07, 0x17, 0xfc, 0xe7 is necessary here,
283 // but that's what came out of the trace every single time.
285 unsigned char *command = us->iobuf;
288 unsigned char *data = us->iobuf;
289 unsigned char *status = us->iobuf;
291 BUG_ON(num_registers > US_IOBUF_SIZE/2);
293 for (i=0; i<20; i++) {
296 * The first time we send the full command, which consists
297 * of downloading the SCSI command followed by downloading
298 * the data via a write-and-test. Any other time we only
299 * send the command to download the data -- the SCSI command
300 * is still 'active' in some sense in the device.
302 * We're only going to try sending the data 10 times. After
303 * that, we just return a failure.
309 command[1] = access | 0x07;
314 command[6] = LSB_of(num_registers*2);
315 command[7] = MSB_of(num_registers*2);
319 command[cmdlen-8] = (direction==SCSI_DATA_WRITE ? 0x40 : 0xC0);
320 command[cmdlen-7] = access |
321 (direction==SCSI_DATA_WRITE ? 0x05 : 0x04);
322 command[cmdlen-6] = data_reg;
323 command[cmdlen-5] = status_reg;
324 command[cmdlen-4] = timeout;
325 command[cmdlen-3] = qualifier;
326 command[cmdlen-2] = LSB_of(len);
327 command[cmdlen-1] = MSB_of(len);
329 result = usb_stor_ctrl_transfer(us,
338 if (result != USB_STOR_XFER_GOOD)
339 return USB_STOR_TRANSPORT_ERROR;
343 for (j=0; j<num_registers; j++) {
344 data[j<<1] = registers[j];
345 data[1+(j<<1)] = data_out[j];
348 result = usb_stor_bulk_transfer_buf(us,
350 data, num_registers*2, NULL);
352 if (result != USB_STOR_XFER_GOOD)
353 return USB_STOR_TRANSPORT_ERROR;
358 //US_DEBUGP("Transfer %s %d bytes, sg buffers %d\n",
359 // direction == SCSI_DATA_WRITE ? "out" : "in",
362 result = usb_stor_bulk_transfer_sg(us,
363 pipe, content, len, use_sg, NULL);
366 * If we get a stall on the bulk download, we'll retry
367 * the bulk download -- but not the SCSI command because
368 * in some sense the SCSI command is still 'active' and
369 * waiting for the data. Don't ask me why this should be;
370 * I'm only following what the Windoze driver did.
372 * Note that a stall for the test-and-read/write command means
373 * that the test failed. In this case we're testing to make
374 * sure that the device is error-free
375 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
376 * hypothesis is that the USBAT chip somehow knows what
377 * the device will accept, but doesn't give the device any
378 * data until all data is received. Thus, the device would
379 * still be waiting for the first byte of data if a stall
380 * occurs, even if the stall implies that some data was
384 if (result == USB_STOR_XFER_SHORT ||
385 result == USB_STOR_XFER_STALLED) {
388 * If we're reading and we stalled, then clear
389 * the bulk output pipe only the first time.
392 if (direction==SCSI_DATA_READ && i==0) {
393 if (usb_stor_clear_halt(us,
394 us->send_bulk_pipe) < 0)
395 return USB_STOR_TRANSPORT_ERROR;
399 * Read status: is the device angry, or just busy?
402 result = usbat_read(us, USBAT_ATA,
403 direction==SCSI_DATA_WRITE ? 0x17 : 0x0E,
406 if (result!=USB_STOR_XFER_GOOD)
407 return USB_STOR_TRANSPORT_ERROR;
408 if (*status & 0x01) // check condition
409 return USB_STOR_TRANSPORT_FAILED;
410 if (*status & 0x20) // device fault
411 return USB_STOR_TRANSPORT_FAILED;
413 US_DEBUGP("Redoing %s\n",
414 direction==SCSI_DATA_WRITE ? "write" : "read");
416 } else if (result != USB_STOR_XFER_GOOD)
417 return USB_STOR_TRANSPORT_ERROR;
419 return usbat_wait_not_busy(us, minutes);
423 US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
424 direction==SCSI_DATA_WRITE ? "Writing" : "Reading");
426 return USB_STOR_TRANSPORT_FAILED;
430 * Write data to multiple registers at once. Not meant for large
434 int usbat_multiple_write(struct us_data *us,
435 unsigned char access,
436 unsigned char *registers,
437 unsigned char *data_out,
438 unsigned short num_registers) {
441 unsigned char *data = us->iobuf;
443 unsigned char *command = us->iobuf;
445 BUG_ON(num_registers > US_IOBUF_SIZE/2);
448 command[1] = access | 0x07;
453 command[6] = LSB_of(num_registers*2);
454 command[7] = MSB_of(num_registers*2);
456 result = usb_stor_ctrl_transfer(us,
465 if (result != USB_STOR_XFER_GOOD)
466 return USB_STOR_TRANSPORT_ERROR;
468 for (i=0; i<num_registers; i++) {
469 data[i<<1] = registers[i];
470 data[1+(i<<1)] = data_out[i];
473 result = usb_stor_bulk_transfer_buf(us,
474 us->send_bulk_pipe, data, num_registers*2, NULL);
476 if (result != USB_STOR_XFER_GOOD)
477 return USB_STOR_TRANSPORT_ERROR;
479 return usbat_wait_not_busy(us, 0);
482 int usbat_read_user_io(struct us_data *us,
483 unsigned char *data_flags) {
487 result = usb_stor_ctrl_transfer(us,
499 int usbat_write_user_io(struct us_data *us,
500 unsigned char enable_flags,
501 unsigned char data_flags) {
505 result = usb_stor_ctrl_transfer(us,
509 short_pack(enable_flags, data_flags),
518 * Squeeze a potentially huge (> 65535 byte) read10 command into
519 * a little ( <= 65535 byte) ATAPI pipe
522 int usbat_handle_read10(struct us_data *us,
523 unsigned char *registers,
527 int result = USB_STOR_TRANSPORT_GOOD;
528 unsigned char *buffer;
531 unsigned int sg_segment = 0;
532 unsigned int sg_offset = 0;
534 US_DEBUGP("handle_read10: transfersize %d\n",
537 if (srb->request_bufflen < 0x10000) {
539 result = usbat_rw_block_test(us, USBAT_ATA,
541 0x10, 0x17, 0xFD, 0x30,
544 srb->request_bufflen, srb->use_sg, 1);
550 * Since we're requesting more data than we can handle in
551 * a single read command (max is 64k-1), we will perform
552 * multiple reads, but each read must be in multiples of
553 * a sector. Luckily the sector size is in srb->transfersize
554 * (see linux/drivers/scsi/sr.c).
557 if (data[7+0] == GPCMD_READ_CD) {
558 len = short_pack(data[7+9], data[7+8]);
561 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
562 srb->transfersize = srb->request_bufflen/len;
565 if (!srb->transfersize) {
566 srb->transfersize = 2048; /* A guess */
567 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
571 // Since we only read in one block at a time, we have to create
572 // a bounce buffer and move the data a piece at a time between the
573 // bounce buffer and the actual transfer buffer.
575 len = (65535/srb->transfersize) * srb->transfersize;
576 US_DEBUGP("Max read is %d bytes\n", len);
577 len = min(len, srb->request_bufflen);
578 buffer = kmalloc(len, GFP_NOIO);
579 if (buffer == NULL) // bloody hell!
580 return USB_STOR_TRANSPORT_FAILED;
581 sector = short_pack(data[7+3], data[7+2]);
583 sector |= short_pack(data[7+5], data[7+4]);
586 sg_segment = 0; // for keeping track of where we are in
587 sg_offset = 0; // the scatter/gather list
589 while (transferred != srb->request_bufflen) {
591 if (len > srb->request_bufflen - transferred)
592 len = srb->request_bufflen - transferred;
594 data[3] = len&0xFF; // (cylL) = expected length (L)
595 data[4] = (len>>8)&0xFF; // (cylH) = expected length (H)
597 // Fix up the SCSI command sector and num sectors
599 data[7+2] = MSB_of(sector>>16); // SCSI command sector
600 data[7+3] = LSB_of(sector>>16);
601 data[7+4] = MSB_of(sector&0xFFFF);
602 data[7+5] = LSB_of(sector&0xFFFF);
603 if (data[7+0] == GPCMD_READ_CD)
605 data[7+7] = MSB_of(len / srb->transfersize); // SCSI command
606 data[7+8] = LSB_of(len / srb->transfersize); // num sectors
608 result = usbat_rw_block_test(us, USBAT_ATA,
610 0x10, 0x17, 0xFD, 0x30,
615 if (result != USB_STOR_TRANSPORT_GOOD)
618 // Store the data in the transfer buffer
619 usb_stor_access_xfer_buf(buffer, len, srb,
620 &sg_segment, &sg_offset, TO_XFER_BUF);
622 // Update the amount transferred and the sector number
625 sector += len / srb->transfersize;
627 } // while transferred != srb->request_bufflen
633 static int hp_8200e_select_and_test_registers(struct us_data *us) {
636 unsigned char *status = us->iobuf;
638 // try device = master, then device = slave.
640 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
642 if (usbat_write(us, USBAT_ATA, 0x16, selector) !=
644 return USB_STOR_TRANSPORT_ERROR;
646 if (usbat_read(us, USBAT_ATA, 0x17, status) !=
648 return USB_STOR_TRANSPORT_ERROR;
650 if (usbat_read(us, USBAT_ATA, 0x16, status) !=
652 return USB_STOR_TRANSPORT_ERROR;
654 if (usbat_read(us, USBAT_ATA, 0x14, status) !=
656 return USB_STOR_TRANSPORT_ERROR;
658 if (usbat_read(us, USBAT_ATA, 0x15, status) !=
660 return USB_STOR_TRANSPORT_ERROR;
662 if (usbat_write(us, USBAT_ATA, 0x14, 0x55) !=
664 return USB_STOR_TRANSPORT_ERROR;
666 if (usbat_write(us, USBAT_ATA, 0x15, 0xAA) !=
668 return USB_STOR_TRANSPORT_ERROR;
670 if (usbat_read(us, USBAT_ATA, 0x14, status) !=
672 return USB_STOR_TRANSPORT_ERROR;
674 if (usbat_read(us, USBAT_ATA, 0x15, status) !=
676 return USB_STOR_TRANSPORT_ERROR;
679 return USB_STOR_TRANSPORT_GOOD;
682 int init_8200e(struct us_data *us) {
685 unsigned char *status = us->iobuf;
687 // Enable peripheral control signals
689 if (usbat_write_user_io(us,
690 USBAT_UIO_OE1 | USBAT_UIO_OE0,
691 USBAT_UIO_EPAD | USBAT_UIO_1) != USB_STOR_XFER_GOOD)
692 return USB_STOR_TRANSPORT_ERROR;
694 US_DEBUGP("INIT 1\n");
698 if (usbat_read_user_io(us, status) !=
700 return USB_STOR_TRANSPORT_ERROR;
702 US_DEBUGP("INIT 2\n");
704 if (usbat_read_user_io(us, status) !=
706 return USB_STOR_TRANSPORT_ERROR;
708 US_DEBUGP("INIT 3\n");
710 // Reset peripheral, enable periph control signals
711 // (bring reset signal up)
713 if (usbat_write_user_io(us,
714 USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
715 USBAT_UIO_EPAD | USBAT_UIO_1) != USB_STOR_XFER_GOOD)
716 return USB_STOR_TRANSPORT_ERROR;
718 US_DEBUGP("INIT 4\n");
720 // Enable periph control signals
721 // (bring reset signal down)
723 if (usbat_write_user_io(us,
724 USBAT_UIO_OE1 | USBAT_UIO_OE0,
725 USBAT_UIO_EPAD | USBAT_UIO_1) != USB_STOR_XFER_GOOD)
726 return USB_STOR_TRANSPORT_ERROR;
728 US_DEBUGP("INIT 5\n");
732 // Write 0x80 to ISA port 0x3F
734 if (usbat_write(us, USBAT_ISA, 0x3F, 0x80) !=
736 return USB_STOR_TRANSPORT_ERROR;
738 US_DEBUGP("INIT 6\n");
740 // Read ISA port 0x27
742 if (usbat_read(us, USBAT_ISA, 0x27, status) !=
744 return USB_STOR_TRANSPORT_ERROR;
746 US_DEBUGP("INIT 7\n");
748 if (usbat_read_user_io(us, status) !=
750 return USB_STOR_TRANSPORT_ERROR;
752 US_DEBUGP("INIT 8\n");
754 if ( (result = hp_8200e_select_and_test_registers(us)) !=
755 USB_STOR_TRANSPORT_GOOD)
758 US_DEBUGP("INIT 9\n");
760 if (usbat_read_user_io(us, status) !=
762 return USB_STOR_TRANSPORT_ERROR;
764 US_DEBUGP("INIT 10\n");
766 // Enable periph control signals and card detect
768 if (usbat_write_user_io(us,
769 USBAT_UIO_ACKD |USBAT_UIO_OE1 | USBAT_UIO_OE0,
770 USBAT_UIO_EPAD | USBAT_UIO_1) != USB_STOR_XFER_GOOD)
771 return USB_STOR_TRANSPORT_ERROR;
773 US_DEBUGP("INIT 11\n");
775 if (usbat_read_user_io(us, status) !=
777 return USB_STOR_TRANSPORT_ERROR;
779 US_DEBUGP("INIT 12\n");
783 if (usbat_read_user_io(us, status) !=
785 return USB_STOR_TRANSPORT_ERROR;
787 US_DEBUGP("INIT 13\n");
789 if ( (result = hp_8200e_select_and_test_registers(us)) !=
790 USB_STOR_TRANSPORT_GOOD)
793 US_DEBUGP("INIT 14\n");
795 if (usbat_set_shuttle_features(us,
796 0x83, 0x00, 0x88, 0x08, 0x15, 0x14) !=
798 return USB_STOR_TRANSPORT_ERROR;
800 US_DEBUGP("INIT 15\n");
802 return USB_STOR_TRANSPORT_GOOD;
806 * Transport for the HP 8200e
808 int hp8200e_transport(Scsi_Cmnd *srb, struct us_data *us)
811 unsigned char *status = us->iobuf;
812 unsigned char registers[32];
813 unsigned char data[32];
818 len = srb->request_bufflen;
820 /* Send A0 (ATA PACKET COMMAND).
821 Note: I guess we're never going to get any of the ATA
822 commands... just ATA Packet Commands.
835 data[3] = len&0xFF; // (cylL) = expected length (L)
836 data[4] = (len>>8)&0xFF; // (cylH) = expected length (H)
837 data[5] = 0xB0; // (device sel) = slave
838 data[6] = 0xA0; // (command) = ATA PACKET COMMAND
840 for (i=7; i<19; i++) {
842 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
845 result = usbat_read(us, USBAT_ATA, 0x17, status);
846 US_DEBUGP("Status = %02X\n", *status);
847 if (result != USB_STOR_XFER_GOOD)
848 return USB_STOR_TRANSPORT_ERROR;
849 if (srb->cmnd[0] == TEST_UNIT_READY)
852 if (srb->sc_data_direction == SCSI_DATA_WRITE) {
854 result = usbat_rw_block_test(us, USBAT_ATA,
856 0x10, 0x17, 0xFD, 0x30,
859 len, srb->use_sg, 10);
861 if (result == USB_STOR_TRANSPORT_GOOD) {
863 US_DEBUGP("Wrote %08X bytes\n", transferred);
868 } else if (srb->cmnd[0] == READ_10 ||
869 srb->cmnd[0] == GPCMD_READ_CD) {
871 return usbat_handle_read10(us, registers, data, srb);
876 US_DEBUGP("Error: len = %08X... what do I do now?\n",
878 return USB_STOR_TRANSPORT_ERROR;
881 if ( (result = usbat_multiple_write(us,
883 registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
887 // Write the 12-byte command header.
889 // If the command is BLANK then set the timer for 75 minutes.
890 // Otherwise set it for 10 minutes.
892 // NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
893 // AT SPEED 4 IS UNRELIABLE!!!
895 if ( (result = usbat_write_block(us,
896 USBAT_ATA, 0x10, srb->cmnd, 12, 0,
897 srb->cmnd[0]==GPCMD_BLANK ? 75 : 10)) !=
898 USB_STOR_TRANSPORT_GOOD) {
902 // If there is response data to be read in
905 if (len != 0 && (srb->sc_data_direction == SCSI_DATA_READ)) {
907 // How many bytes to read in? Check cylL register
909 if (usbat_read(us, USBAT_ATA, 0x14, status) !=
910 USB_STOR_XFER_GOOD) {
911 return USB_STOR_TRANSPORT_ERROR;
914 if (len > 0xFF) { // need to read cylH also
916 if (usbat_read(us, USBAT_ATA, 0x15, status) !=
917 USB_STOR_XFER_GOOD) {
918 return USB_STOR_TRANSPORT_ERROR;
920 len += ((unsigned int) *status)<<8;
926 result = usbat_read_block(us, USBAT_ATA, 0x10,
927 srb->request_buffer, len, srb->use_sg);
929 /* Debug-print the first 32 bytes of the transfer */
933 for (i=0; i<len && i<32; i++) {
934 sprintf(string+strlen(string), "%02X ",
935 ((unsigned char *)srb->request_buffer)[i]);
937 US_DEBUGP("%s\n", string);
942 US_DEBUGP("%s\n", string);