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 <linux/sched.h>
43 #include <linux/errno.h>
44 #include <linux/slab.h>
45 #include <linux/cdrom.h>
47 #include <scsi/scsi.h>
48 #include <scsi/scsi_cmnd.h>
50 #include "transport.h"
54 #include "shuttle_usbat.h"
56 #define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
57 #define LSB_of(s) ((s)&0xFF)
58 #define MSB_of(s) ((s)>>8)
62 static int usbat_read(struct us_data *us,
65 unsigned char *content)
69 result = usb_stor_ctrl_transfer(us,
81 static int usbat_write(struct us_data *us,
84 unsigned char content)
88 result = usb_stor_ctrl_transfer(us,
92 short_pack(reg, content),
100 static int usbat_set_shuttle_features(struct us_data *us,
101 unsigned char external_trigger,
102 unsigned char epp_control,
103 unsigned char mask_byte,
104 unsigned char test_pattern,
105 unsigned char subcountH,
106 unsigned char subcountL)
109 unsigned char *command = us->iobuf;
113 command[2] = epp_control;
114 command[3] = external_trigger;
115 command[4] = test_pattern;
116 command[5] = mask_byte;
117 command[6] = subcountL;
118 command[7] = subcountH;
120 result = usb_stor_ctrl_transfer(us,
132 static int usbat_read_block(struct us_data *us,
133 unsigned char access,
135 unsigned char *content,
140 unsigned char *command = us->iobuf;
143 return USB_STOR_TRANSPORT_GOOD;
146 command[1] = access | 0x02;
151 command[6] = LSB_of(len);
152 command[7] = MSB_of(len);
154 result = usb_stor_ctrl_transfer(us,
163 if (result != USB_STOR_XFER_GOOD)
164 return USB_STOR_TRANSPORT_ERROR;
166 result = usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe,
167 content, len, use_sg, NULL);
169 return (result == USB_STOR_XFER_GOOD ?
170 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
174 * Block, waiting for an ATA device to become not busy or to report
175 * an error condition.
178 static int usbat_wait_not_busy(struct us_data *us, int minutes)
182 unsigned char *status = us->iobuf;
184 /* Synchronizing cache on a CDR could take a heck of a long time,
185 * but probably not more than 10 minutes or so. On the other hand,
186 * doing a full blank on a CDRW at speed 1 will take about 75
190 for (i=0; i<1200+minutes*60; i++) {
192 result = usbat_read(us, USBAT_ATA, 0x17, status);
194 if (result!=USB_STOR_XFER_GOOD)
195 return USB_STOR_TRANSPORT_ERROR;
196 if (*status & 0x01) { // check condition
197 result = usbat_read(us, USBAT_ATA, 0x10, status);
198 return USB_STOR_TRANSPORT_FAILED;
200 if (*status & 0x20) // device fault
201 return USB_STOR_TRANSPORT_FAILED;
203 if ((*status & 0x80)==0x00) { // not busy
204 US_DEBUGP("Waited not busy for %d steps\n", i);
205 return USB_STOR_TRANSPORT_GOOD;
209 msleep(10); // 5 seconds
211 msleep(50); // 10 seconds
213 msleep(100); // 50 seconds
215 msleep(1000); // X minutes
218 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
220 return USB_STOR_TRANSPORT_FAILED;
223 static int usbat_write_block(struct us_data *us,
224 unsigned char access,
226 unsigned char *content,
228 int use_sg, int minutes)
231 unsigned char *command = us->iobuf;
234 return USB_STOR_TRANSPORT_GOOD;
237 command[1] = access | 0x03;
242 command[6] = LSB_of(len);
243 command[7] = MSB_of(len);
245 result = usb_stor_ctrl_transfer(us,
254 if (result != USB_STOR_XFER_GOOD)
255 return USB_STOR_TRANSPORT_ERROR;
257 result = usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe,
258 content, len, use_sg, NULL);
260 if (result != USB_STOR_XFER_GOOD)
261 return USB_STOR_TRANSPORT_ERROR;
263 return usbat_wait_not_busy(us, minutes);
266 static int usbat_rw_block_test(struct us_data *us,
267 unsigned char access,
268 unsigned char *registers,
269 unsigned char *data_out,
270 unsigned short num_registers,
271 unsigned char data_reg,
272 unsigned char status_reg,
273 unsigned char timeout,
274 unsigned char qualifier,
276 unsigned char *content,
282 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
283 us->recv_bulk_pipe : us->send_bulk_pipe;
285 // Not really sure the 0x07, 0x17, 0xfc, 0xe7 is necessary here,
286 // but that's what came out of the trace every single time.
288 unsigned char *command = us->iobuf;
291 unsigned char *data = us->iobuf;
292 unsigned char *status = us->iobuf;
294 BUG_ON(num_registers > US_IOBUF_SIZE/2);
296 for (i=0; i<20; i++) {
299 * The first time we send the full command, which consists
300 * of downloading the SCSI command followed by downloading
301 * the data via a write-and-test. Any other time we only
302 * send the command to download the data -- the SCSI command
303 * is still 'active' in some sense in the device.
305 * We're only going to try sending the data 10 times. After
306 * that, we just return a failure.
312 command[1] = access | 0x07;
317 command[6] = LSB_of(num_registers*2);
318 command[7] = MSB_of(num_registers*2);
322 command[cmdlen-8] = (direction==DMA_TO_DEVICE ? 0x40 : 0xC0);
323 command[cmdlen-7] = access |
324 (direction==DMA_TO_DEVICE ? 0x05 : 0x04);
325 command[cmdlen-6] = data_reg;
326 command[cmdlen-5] = status_reg;
327 command[cmdlen-4] = timeout;
328 command[cmdlen-3] = qualifier;
329 command[cmdlen-2] = LSB_of(len);
330 command[cmdlen-1] = MSB_of(len);
332 result = usb_stor_ctrl_transfer(us,
341 if (result != USB_STOR_XFER_GOOD)
342 return USB_STOR_TRANSPORT_ERROR;
346 for (j=0; j<num_registers; j++) {
347 data[j<<1] = registers[j];
348 data[1+(j<<1)] = data_out[j];
351 result = usb_stor_bulk_transfer_buf(us,
353 data, num_registers*2, NULL);
355 if (result != USB_STOR_XFER_GOOD)
356 return USB_STOR_TRANSPORT_ERROR;
361 //US_DEBUGP("Transfer %s %d bytes, sg buffers %d\n",
362 // direction == DMA_TO_DEVICE ? "out" : "in",
365 result = usb_stor_bulk_transfer_sg(us,
366 pipe, content, len, use_sg, NULL);
369 * If we get a stall on the bulk download, we'll retry
370 * the bulk download -- but not the SCSI command because
371 * in some sense the SCSI command is still 'active' and
372 * waiting for the data. Don't ask me why this should be;
373 * I'm only following what the Windoze driver did.
375 * Note that a stall for the test-and-read/write command means
376 * that the test failed. In this case we're testing to make
377 * sure that the device is error-free
378 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
379 * hypothesis is that the USBAT chip somehow knows what
380 * the device will accept, but doesn't give the device any
381 * data until all data is received. Thus, the device would
382 * still be waiting for the first byte of data if a stall
383 * occurs, even if the stall implies that some data was
387 if (result == USB_STOR_XFER_SHORT ||
388 result == USB_STOR_XFER_STALLED) {
391 * If we're reading and we stalled, then clear
392 * the bulk output pipe only the first time.
395 if (direction==DMA_FROM_DEVICE && i==0) {
396 if (usb_stor_clear_halt(us,
397 us->send_bulk_pipe) < 0)
398 return USB_STOR_TRANSPORT_ERROR;
402 * Read status: is the device angry, or just busy?
405 result = usbat_read(us, USBAT_ATA,
406 direction==DMA_TO_DEVICE ? 0x17 : 0x0E,
409 if (result!=USB_STOR_XFER_GOOD)
410 return USB_STOR_TRANSPORT_ERROR;
411 if (*status & 0x01) // check condition
412 return USB_STOR_TRANSPORT_FAILED;
413 if (*status & 0x20) // device fault
414 return USB_STOR_TRANSPORT_FAILED;
416 US_DEBUGP("Redoing %s\n",
417 direction==DMA_TO_DEVICE ? "write" : "read");
419 } else if (result != USB_STOR_XFER_GOOD)
420 return USB_STOR_TRANSPORT_ERROR;
422 return usbat_wait_not_busy(us, minutes);
426 US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
427 direction==DMA_TO_DEVICE ? "Writing" : "Reading");
429 return USB_STOR_TRANSPORT_FAILED;
433 * Write data to multiple registers at once. Not meant for large
437 static int usbat_multiple_write(struct us_data *us,
438 unsigned char access,
439 unsigned char *registers,
440 unsigned char *data_out,
441 unsigned short num_registers)
444 unsigned char *data = us->iobuf;
446 unsigned char *command = us->iobuf;
448 BUG_ON(num_registers > US_IOBUF_SIZE/2);
451 command[1] = access | 0x07;
456 command[6] = LSB_of(num_registers*2);
457 command[7] = MSB_of(num_registers*2);
459 result = usb_stor_ctrl_transfer(us,
468 if (result != USB_STOR_XFER_GOOD)
469 return USB_STOR_TRANSPORT_ERROR;
471 for (i=0; i<num_registers; i++) {
472 data[i<<1] = registers[i];
473 data[1+(i<<1)] = data_out[i];
476 result = usb_stor_bulk_transfer_buf(us,
477 us->send_bulk_pipe, data, num_registers*2, NULL);
479 if (result != USB_STOR_XFER_GOOD)
480 return USB_STOR_TRANSPORT_ERROR;
482 return usbat_wait_not_busy(us, 0);
485 static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
489 result = usb_stor_ctrl_transfer(us,
501 static int usbat_write_user_io(struct us_data *us,
502 unsigned char enable_flags,
503 unsigned char data_flags)
507 result = usb_stor_ctrl_transfer(us,
511 short_pack(enable_flags, data_flags),
520 * Squeeze a potentially huge (> 65535 byte) read10 command into
521 * a little ( <= 65535 byte) ATAPI pipe
524 static int usbat_handle_read10(struct us_data *us,
525 unsigned char *registers,
527 struct scsi_cmnd *srb)
529 int result = USB_STOR_TRANSPORT_GOOD;
530 unsigned char *buffer;
533 unsigned int sg_segment = 0;
534 unsigned int sg_offset = 0;
536 US_DEBUGP("handle_read10: transfersize %d\n",
539 if (srb->request_bufflen < 0x10000) {
541 result = usbat_rw_block_test(us, USBAT_ATA,
543 0x10, 0x17, 0xFD, 0x30,
546 srb->request_bufflen, srb->use_sg, 1);
552 * Since we're requesting more data than we can handle in
553 * a single read command (max is 64k-1), we will perform
554 * multiple reads, but each read must be in multiples of
555 * a sector. Luckily the sector size is in srb->transfersize
556 * (see linux/drivers/scsi/sr.c).
559 if (data[7+0] == GPCMD_READ_CD) {
560 len = short_pack(data[7+9], data[7+8]);
563 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
564 srb->transfersize = srb->request_bufflen/len;
567 if (!srb->transfersize) {
568 srb->transfersize = 2048; /* A guess */
569 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
573 // Since we only read in one block at a time, we have to create
574 // a bounce buffer and move the data a piece at a time between the
575 // bounce buffer and the actual transfer buffer.
577 len = (65535/srb->transfersize) * srb->transfersize;
578 US_DEBUGP("Max read is %d bytes\n", len);
579 len = min(len, srb->request_bufflen);
580 buffer = kmalloc(len, GFP_NOIO);
581 if (buffer == NULL) // bloody hell!
582 return USB_STOR_TRANSPORT_FAILED;
583 sector = short_pack(data[7+3], data[7+2]);
585 sector |= short_pack(data[7+5], data[7+4]);
588 sg_segment = 0; // for keeping track of where we are in
589 sg_offset = 0; // the scatter/gather list
591 while (transferred != srb->request_bufflen) {
593 if (len > srb->request_bufflen - transferred)
594 len = srb->request_bufflen - transferred;
596 data[3] = len&0xFF; // (cylL) = expected length (L)
597 data[4] = (len>>8)&0xFF; // (cylH) = expected length (H)
599 // Fix up the SCSI command sector and num sectors
601 data[7+2] = MSB_of(sector>>16); // SCSI command sector
602 data[7+3] = LSB_of(sector>>16);
603 data[7+4] = MSB_of(sector&0xFFFF);
604 data[7+5] = LSB_of(sector&0xFFFF);
605 if (data[7+0] == GPCMD_READ_CD)
607 data[7+7] = MSB_of(len / srb->transfersize); // SCSI command
608 data[7+8] = LSB_of(len / srb->transfersize); // num sectors
610 result = usbat_rw_block_test(us, USBAT_ATA,
612 0x10, 0x17, 0xFD, 0x30,
617 if (result != USB_STOR_TRANSPORT_GOOD)
620 // Store the data in the transfer buffer
621 usb_stor_access_xfer_buf(buffer, len, srb,
622 &sg_segment, &sg_offset, TO_XFER_BUF);
624 // Update the amount transferred and the sector number
627 sector += len / srb->transfersize;
629 } // while transferred != srb->request_bufflen
635 static int hp_8200e_select_and_test_registers(struct us_data *us)
638 unsigned char *status = us->iobuf;
640 // try device = master, then device = slave.
642 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
644 if (usbat_write(us, USBAT_ATA, 0x16, selector) !=
646 return USB_STOR_TRANSPORT_ERROR;
648 if (usbat_read(us, USBAT_ATA, 0x17, status) !=
650 return USB_STOR_TRANSPORT_ERROR;
652 if (usbat_read(us, USBAT_ATA, 0x16, status) !=
654 return USB_STOR_TRANSPORT_ERROR;
656 if (usbat_read(us, USBAT_ATA, 0x14, status) !=
658 return USB_STOR_TRANSPORT_ERROR;
660 if (usbat_read(us, USBAT_ATA, 0x15, status) !=
662 return USB_STOR_TRANSPORT_ERROR;
664 if (usbat_write(us, USBAT_ATA, 0x14, 0x55) !=
666 return USB_STOR_TRANSPORT_ERROR;
668 if (usbat_write(us, USBAT_ATA, 0x15, 0xAA) !=
670 return USB_STOR_TRANSPORT_ERROR;
672 if (usbat_read(us, USBAT_ATA, 0x14, status) !=
674 return USB_STOR_TRANSPORT_ERROR;
676 if (usbat_read(us, USBAT_ATA, 0x15, status) !=
678 return USB_STOR_TRANSPORT_ERROR;
681 return USB_STOR_TRANSPORT_GOOD;
684 int init_8200e(struct us_data *us)
687 unsigned char *status = us->iobuf;
689 // Enable peripheral control signals
691 if (usbat_write_user_io(us,
692 USBAT_UIO_OE1 | USBAT_UIO_OE0,
693 USBAT_UIO_EPAD | USBAT_UIO_1) != USB_STOR_XFER_GOOD)
694 return USB_STOR_TRANSPORT_ERROR;
696 US_DEBUGP("INIT 1\n");
700 if (usbat_read_user_io(us, status) !=
702 return USB_STOR_TRANSPORT_ERROR;
704 US_DEBUGP("INIT 2\n");
706 if (usbat_read_user_io(us, status) !=
708 return USB_STOR_TRANSPORT_ERROR;
710 US_DEBUGP("INIT 3\n");
712 // Reset peripheral, enable periph control signals
713 // (bring reset signal up)
715 if (usbat_write_user_io(us,
716 USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
717 USBAT_UIO_EPAD | USBAT_UIO_1) != USB_STOR_XFER_GOOD)
718 return USB_STOR_TRANSPORT_ERROR;
720 US_DEBUGP("INIT 4\n");
722 // Enable periph control signals
723 // (bring reset signal down)
725 if (usbat_write_user_io(us,
726 USBAT_UIO_OE1 | USBAT_UIO_OE0,
727 USBAT_UIO_EPAD | USBAT_UIO_1) != USB_STOR_XFER_GOOD)
728 return USB_STOR_TRANSPORT_ERROR;
730 US_DEBUGP("INIT 5\n");
734 // Write 0x80 to ISA port 0x3F
736 if (usbat_write(us, USBAT_ISA, 0x3F, 0x80) !=
738 return USB_STOR_TRANSPORT_ERROR;
740 US_DEBUGP("INIT 6\n");
742 // Read ISA port 0x27
744 if (usbat_read(us, USBAT_ISA, 0x27, status) !=
746 return USB_STOR_TRANSPORT_ERROR;
748 US_DEBUGP("INIT 7\n");
750 if (usbat_read_user_io(us, status) !=
752 return USB_STOR_TRANSPORT_ERROR;
754 US_DEBUGP("INIT 8\n");
756 if ( (result = hp_8200e_select_and_test_registers(us)) !=
757 USB_STOR_TRANSPORT_GOOD)
760 US_DEBUGP("INIT 9\n");
762 if (usbat_read_user_io(us, status) !=
764 return USB_STOR_TRANSPORT_ERROR;
766 US_DEBUGP("INIT 10\n");
768 // Enable periph control signals and card detect
770 if (usbat_write_user_io(us,
771 USBAT_UIO_ACKD |USBAT_UIO_OE1 | USBAT_UIO_OE0,
772 USBAT_UIO_EPAD | USBAT_UIO_1) != USB_STOR_XFER_GOOD)
773 return USB_STOR_TRANSPORT_ERROR;
775 US_DEBUGP("INIT 11\n");
777 if (usbat_read_user_io(us, status) !=
779 return USB_STOR_TRANSPORT_ERROR;
781 US_DEBUGP("INIT 12\n");
785 if (usbat_read_user_io(us, status) !=
787 return USB_STOR_TRANSPORT_ERROR;
789 US_DEBUGP("INIT 13\n");
791 if ( (result = hp_8200e_select_and_test_registers(us)) !=
792 USB_STOR_TRANSPORT_GOOD)
795 US_DEBUGP("INIT 14\n");
797 if (usbat_set_shuttle_features(us,
798 0x83, 0x00, 0x88, 0x08, 0x15, 0x14) !=
800 return USB_STOR_TRANSPORT_ERROR;
802 US_DEBUGP("INIT 15\n");
804 return USB_STOR_TRANSPORT_GOOD;
808 * Transport for the HP 8200e
810 int hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
813 unsigned char *status = us->iobuf;
814 unsigned char registers[32];
815 unsigned char data[32];
820 len = srb->request_bufflen;
822 /* Send A0 (ATA PACKET COMMAND).
823 Note: I guess we're never going to get any of the ATA
824 commands... just ATA Packet Commands.
837 data[3] = len&0xFF; // (cylL) = expected length (L)
838 data[4] = (len>>8)&0xFF; // (cylH) = expected length (H)
839 data[5] = 0xB0; // (device sel) = slave
840 data[6] = 0xA0; // (command) = ATA PACKET COMMAND
842 for (i=7; i<19; i++) {
844 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
847 result = usbat_read(us, USBAT_ATA, 0x17, status);
848 US_DEBUGP("Status = %02X\n", *status);
849 if (result != USB_STOR_XFER_GOOD)
850 return USB_STOR_TRANSPORT_ERROR;
851 if (srb->cmnd[0] == TEST_UNIT_READY)
854 if (srb->sc_data_direction == DMA_TO_DEVICE) {
856 result = usbat_rw_block_test(us, USBAT_ATA,
858 0x10, 0x17, 0xFD, 0x30,
861 len, srb->use_sg, 10);
863 if (result == USB_STOR_TRANSPORT_GOOD) {
865 US_DEBUGP("Wrote %08X bytes\n", transferred);
870 } else if (srb->cmnd[0] == READ_10 ||
871 srb->cmnd[0] == GPCMD_READ_CD) {
873 return usbat_handle_read10(us, registers, data, srb);
878 US_DEBUGP("Error: len = %08X... what do I do now?\n",
880 return USB_STOR_TRANSPORT_ERROR;
883 if ( (result = usbat_multiple_write(us,
885 registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
889 // Write the 12-byte command header.
891 // If the command is BLANK then set the timer for 75 minutes.
892 // Otherwise set it for 10 minutes.
894 // NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
895 // AT SPEED 4 IS UNRELIABLE!!!
897 if ( (result = usbat_write_block(us,
898 USBAT_ATA, 0x10, srb->cmnd, 12, 0,
899 srb->cmnd[0]==GPCMD_BLANK ? 75 : 10)) !=
900 USB_STOR_TRANSPORT_GOOD) {
904 // If there is response data to be read in
907 if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
909 // How many bytes to read in? Check cylL register
911 if (usbat_read(us, USBAT_ATA, 0x14, status) !=
912 USB_STOR_XFER_GOOD) {
913 return USB_STOR_TRANSPORT_ERROR;
916 if (len > 0xFF) { // need to read cylH also
918 if (usbat_read(us, USBAT_ATA, 0x15, status) !=
919 USB_STOR_XFER_GOOD) {
920 return USB_STOR_TRANSPORT_ERROR;
922 len += ((unsigned int) *status)<<8;
928 result = usbat_read_block(us, USBAT_ATA, 0x10,
929 srb->request_buffer, len, srb->use_sg);
931 /* Debug-print the first 32 bytes of the transfer */
935 for (i=0; i<len && i<32; i++) {
936 sprintf(string+strlen(string), "%02X ",
937 ((unsigned char *)srb->request_buffer)[i]);
939 US_DEBUGP("%s\n", string);
944 US_DEBUGP("%s\n", string);