1 /* Driver for SCM Microsystems USB-ATAPI cable
3 * $Id: shuttle_usbat.c,v 1.16 2002/02/25 00:40:13 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 extern int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
53 u8 request, u8 requesttype, u16 value, u16 index,
54 void *data, u16 size);
55 extern int usb_stor_bulk_msg(struct us_data *us, void *data, int pipe,
56 unsigned int len, unsigned int *act_len);
58 #define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
59 #define LSB_of(s) ((s)&0xFF)
60 #define MSB_of(s) ((s)>>8)
65 * Send a control message and wait for the response.
67 * us - the pointer to the us_data structure for the device to use
69 * request - the URB Setup Packet's first 6 bytes. The first byte always
70 * corresponds to the request type, and the second byte always corresponds
71 * to the request. The other 4 bytes do not correspond to value and index,
72 * since they are used in a custom way by the SCM protocol.
74 * xfer_data - a buffer from which to get, or to which to store, any data
75 * that gets send or received, respectively, with the URB. Even though
76 * it looks like we allocate a buffer in this code for the data, xfer_data
77 * must contain enough allocated space.
79 * xfer_len - the number of bytes to send or receive with the URB.
83 static int usbat_send_control(struct us_data *us,
85 unsigned char request,
86 unsigned char requesttype,
89 unsigned char *xfer_data,
90 unsigned int xfer_len) {
94 // Send the URB to the device and wait for a response.
96 /* Why are request and request type reversed in this call? */
98 result = usb_stor_control_msg(us, pipe,
99 request, requesttype, value, index,
100 xfer_data, xfer_len);
103 // Check the return code for the command.
106 /* if the command was aborted, indicate that */
107 if (result == -ECONNRESET)
108 return USB_STOR_TRANSPORT_ABORTED;
110 /* a stall is a fatal condition from the device */
111 if (result == -EPIPE) {
112 US_DEBUGP("-- Stall on control pipe. Clearing\n");
113 result = usb_stor_clear_halt(us, pipe);
114 US_DEBUGP("-- usb_stor_clear_halt() returns %d\n", result);
115 return USB_STOR_TRANSPORT_FAILED;
118 /* Uh oh... serious problem here */
119 return USB_STOR_TRANSPORT_ERROR;
122 return USB_STOR_TRANSPORT_GOOD;
125 static int usbat_raw_bulk(struct us_data *us,
128 unsigned short len) {
134 if (direction == SCSI_DATA_READ)
135 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
137 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
139 result = usb_stor_bulk_msg(us, data, pipe, len, &act_len);
141 /* if we stall, we need to clear it before we go on */
142 if (result == -EPIPE) {
143 US_DEBUGP("EPIPE: clearing endpoint halt for"
144 " pipe 0x%x, stalled at %d bytes\n",
146 usb_stor_clear_halt(us, pipe);
151 /* NAK - that means we've retried a few times already */
152 if (result == -ETIMEDOUT) {
153 US_DEBUGP("usbat_raw_bulk():"
155 return US_BULK_TRANSFER_FAILED;
158 /* -ECONNRESET -- we canceled this transfer */
159 if (result == -ECONNRESET) {
160 US_DEBUGP("usbat_raw_bulk():"
161 " transfer aborted\n");
162 return US_BULK_TRANSFER_ABORTED;
165 if (result == -EPIPE) {
166 US_DEBUGP("usbat_raw_bulk():"
167 " output pipe stalled\n");
168 return US_BULK_TRANSFER_SHORT;
171 /* the catch-all case */
172 US_DEBUGP("us_transfer_partial(): unknown error\n");
173 return US_BULK_TRANSFER_FAILED;
176 if (act_len != len) {
177 US_DEBUGP("Warning: Transferred only %d bytes\n",
179 return US_BULK_TRANSFER_SHORT;
182 US_DEBUGP("Transferred %s %d of %d bytes\n",
183 direction==SCSI_DATA_READ ? "in" : "out", act_len, len);
185 return US_BULK_TRANSFER_GOOD;
189 * Note: direction must be set if command_len == 0.
192 static int usbat_bulk_transport(struct us_data *us,
193 unsigned char *command,
194 unsigned short command_len,
200 int result = USB_STOR_TRANSPORT_GOOD;
203 struct scatterlist *sg;
206 return USB_STOR_TRANSPORT_GOOD;
208 /* transfer the data payload for the command, if there is any */
210 if (command_len != 0)
211 direction = (command[0]&0x80) ? SCSI_DATA_READ :
215 result = usbat_raw_bulk(us, direction, data, len);
217 sg = (struct scatterlist *)data;
218 for (i=0; i<use_sg && transferred<len; i++) {
219 result = usbat_raw_bulk(us, direction,
221 len-transferred > sg[i].length ?
222 sg[i].length : len-transferred);
223 if (result!=US_BULK_TRANSFER_GOOD)
225 transferred += sg[i].length;
232 int usbat_read(struct us_data *us,
233 unsigned char access,
235 unsigned char *content) {
239 result = usbat_send_control(us,
240 usb_rcvctrlpipe(us->pusb_dev,0),
251 int usbat_write(struct us_data *us,
252 unsigned char access,
254 unsigned char content) {
258 result = usbat_send_control(us,
259 usb_sndctrlpipe(us->pusb_dev,0),
262 short_pack(reg, content),
270 int usbat_set_shuttle_features(struct us_data *us,
271 unsigned char external_trigger,
272 unsigned char epp_control,
273 unsigned char mask_byte,
274 unsigned char test_pattern,
275 unsigned char subcountH,
276 unsigned char subcountL) {
279 unsigned char command[8] = {
280 0x40, 0x81, epp_control, external_trigger,
281 test_pattern, mask_byte, subcountL, subcountH
284 result = usbat_send_control(us,
285 usb_sndctrlpipe(us->pusb_dev,0),
296 int usbat_read_block(struct us_data *us,
297 unsigned char access,
299 unsigned char *content,
304 unsigned char command[8] = {
305 0xC0, access|0x02, reg, 0x00, 0x00, 0x00,
306 LSB_of(len), MSB_of(len)
309 result = usbat_send_control(us,
310 usb_sndctrlpipe(us->pusb_dev,0),
318 if (result != USB_STOR_TRANSPORT_GOOD)
321 result = usbat_bulk_transport(us,
322 NULL, 0, SCSI_DATA_READ, content, len, use_sg);
328 * Block, waiting for an ATA device to become not busy or to report
329 * an error condition.
332 int usbat_wait_not_busy(struct us_data *us, int minutes) {
336 unsigned char status;
338 /* Synchronizing cache on a CDR could take a heck of a long time,
339 * but probably not more than 10 minutes or so. On the other hand,
340 * doing a full blank on a CDRW at speed 1 will take about 75
344 for (i=0; i<1200+minutes*60; i++) {
346 result = usbat_read(us, USBAT_ATA, 0x17, &status);
348 if (result!=USB_STOR_TRANSPORT_GOOD)
350 if (status&0x01) { // check condition
351 result = usbat_read(us, USBAT_ATA, 0x10, &status);
352 return USB_STOR_TRANSPORT_FAILED;
354 if (status&0x20) // device fault
355 return USB_STOR_TRANSPORT_FAILED;
357 if ((status&0x80)==0x00) { // not busy
358 US_DEBUGP("Waited not busy for %d steps\n", i);
359 return USB_STOR_TRANSPORT_GOOD;
363 wait_ms(10); // 5 seconds
365 wait_ms(50); // 10 seconds
367 wait_ms(100); // 50 seconds
369 wait_ms(1000); // X minutes
372 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
374 return USB_STOR_TRANSPORT_FAILED;
377 int usbat_write_block(struct us_data *us,
378 unsigned char access,
380 unsigned char *content,
386 unsigned char command[8] = {
387 0x40, access|0x03, reg, 0x00, 0x00, 0x00,
388 LSB_of(len), MSB_of(len)
391 result = usbat_send_control(us,
392 usb_sndctrlpipe(us->pusb_dev,0),
400 if (result != USB_STOR_TRANSPORT_GOOD)
403 result = usbat_bulk_transport(us,
404 NULL, 0, SCSI_DATA_WRITE, content, len, use_sg);
406 if (result != USB_STOR_TRANSPORT_GOOD)
409 return usbat_wait_not_busy(us, minutes);
412 int usbat_rw_block_test(struct us_data *us,
413 unsigned char access,
414 unsigned char *registers,
415 unsigned char *data_out,
416 unsigned short num_registers,
417 unsigned char data_reg,
418 unsigned char status_reg,
419 unsigned char timeout,
420 unsigned char qualifier,
422 unsigned char *content,
429 // Not really sure the 0x07, 0x17, 0xfc, 0xe7 is necessary here,
430 // but that's what came out of the trace every single time.
432 unsigned char command[16] = {
433 0x40, access|0x07, 0x07, 0x17, 0xfc, 0xe7,
434 LSB_of(num_registers*2), MSB_of(num_registers*2),
435 (direction==SCSI_DATA_WRITE ? 0x40 : 0xC0),
436 access|(direction==SCSI_DATA_WRITE ? 0x05 : 0x04),
437 data_reg, status_reg,
438 timeout, qualifier, LSB_of(len), MSB_of(len)
442 unsigned char data[num_registers*2];
443 unsigned char status;
445 for (i=0; i<num_registers; i++) {
446 data[i<<1] = registers[i];
447 data[1+(i<<1)] = data_out[i];
450 for (i=0; i<20; i++) {
453 * The first time we send the full command, which consists
454 * of downloading the SCSI command followed by downloading
455 * the data via a write-and-test. Any other time we only
456 * send the command to download the data -- the SCSI command
457 * is still 'active' in some sense in the device.
459 * We're only going to try sending the data 10 times. After
460 * that, we just return a failure.
463 result = usbat_send_control(us,
464 usb_sndctrlpipe(us->pusb_dev,0),
469 (i==0 ? command : command+8),
472 if (result != USB_STOR_TRANSPORT_GOOD)
477 result = usbat_bulk_transport(us,
478 NULL, 0, SCSI_DATA_WRITE,
479 data, num_registers*2, 0);
481 if (result!=USB_STOR_TRANSPORT_GOOD)
487 //US_DEBUGP("Transfer %s %d bytes, sg buffers %d\n",
488 // direction == SCSI_DATA_WRITE ? "out" : "in",
491 result = usbat_bulk_transport(us,
492 NULL, 0, direction, content, len, use_sg);
495 * If we get a stall on the bulk download, we'll retry
496 * the bulk download -- but not the SCSI command because
497 * in some sense the SCSI command is still 'active' and
498 * waiting for the data. Don't ask me why this should be;
499 * I'm only following what the Windoze driver did.
501 * Note that a stall for the test-and-read/write command means
502 * that the test failed. In this case we're testing to make
503 * sure that the device is error-free
504 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
505 * hypothesis is that the USBAT chip somehow knows what
506 * the device will accept, but doesn't give the device any
507 * data until all data is received. Thus, the device would
508 * still be waiting for the first byte of data if a stall
509 * occurs, even if the stall implies that some data was
513 if (result == US_BULK_TRANSFER_SHORT) {
516 * If we're reading and we stalled, then clear
517 * the bulk output pipe only the first time.
520 if (direction==SCSI_DATA_READ && i==0)
521 usb_stor_clear_halt(us,
522 usb_sndbulkpipe(us->pusb_dev,
525 * Read status: is the device angry, or just busy?
528 result = usbat_read(us, USBAT_ATA,
529 direction==SCSI_DATA_WRITE ? 0x17 : 0x0E,
532 if (result!=USB_STOR_TRANSPORT_GOOD)
534 if (status&0x01) // check condition
535 return USB_STOR_TRANSPORT_FAILED;
536 if (status&0x20) // device fault
537 return USB_STOR_TRANSPORT_FAILED;
539 US_DEBUGP("Redoing %s\n",
540 direction==SCSI_DATA_WRITE ? "write" : "read");
542 } else if (result != US_BULK_TRANSFER_GOOD)
545 return usbat_wait_not_busy(us, minutes);
549 US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
550 direction==SCSI_DATA_WRITE ? "Writing" : "Reading");
552 return USB_STOR_TRANSPORT_FAILED;
556 * Write data to multiple registers at once. Not meant for large
560 int usbat_multiple_write(struct us_data *us,
561 unsigned char access,
562 unsigned char *registers,
563 unsigned char *data_out,
564 unsigned short num_registers) {
567 unsigned char data[num_registers*2];
569 unsigned char command[8] = {
570 0x40, access|0x07, 0x00, 0x00, 0x00, 0x00,
571 LSB_of(num_registers*2), MSB_of(num_registers*2)
574 for (i=0; i<num_registers; i++) {
575 data[i<<1] = registers[i];
576 data[1+(i<<1)] = data_out[i];
579 result = usbat_send_control(us,
580 usb_sndctrlpipe(us->pusb_dev,0),
588 if (result != USB_STOR_TRANSPORT_GOOD)
591 result = usbat_bulk_transport(us,
592 NULL, 0, SCSI_DATA_WRITE, data, num_registers*2, 0);
594 if (result!=USB_STOR_TRANSPORT_GOOD)
597 return usbat_wait_not_busy(us, 0);
600 int usbat_read_user_io(struct us_data *us,
601 unsigned char *data_flags) {
605 result = usbat_send_control(us,
606 usb_rcvctrlpipe(us->pusb_dev,0),
617 int usbat_write_user_io(struct us_data *us,
618 unsigned char enable_flags,
619 unsigned char data_flags) {
623 result = usbat_send_control(us,
624 usb_sndctrlpipe(us->pusb_dev,0),
627 short_pack(enable_flags, data_flags),
636 * Squeeze a potentially huge (> 65535 byte) read10 command into
637 * a little ( <= 65535 byte) ATAPI pipe
640 int usbat_handle_read10(struct us_data *us,
641 unsigned char *registers,
645 int result = USB_STOR_TRANSPORT_GOOD;
646 unsigned char *buffer;
650 struct scatterlist *sg = NULL;
654 US_DEBUGP("handle_read10: transfersize %d\n",
657 if (srb->request_bufflen < 0x10000) {
659 result = usbat_rw_block_test(us, USBAT_ATA,
661 0x10, 0x17, 0xFD, 0x30,
664 srb->request_bufflen, srb->use_sg, 1);
670 * Since we're requesting more data than we can handle in
671 * a single read command (max is 64k-1), we will perform
672 * multiple reads, but each read must be in multiples of
673 * a sector. Luckily the sector size is in srb->transfersize
674 * (see linux/drivers/scsi/sr.c).
677 if (data[7+0] == GPCMD_READ_CD) {
678 len = short_pack(data[7+9], data[7+8]);
681 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
682 srb->transfersize = srb->request_bufflen/len;
685 if (!srb->transfersize) {
686 srb->transfersize = 2048; /* A guess */
687 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
691 len = (65535/srb->transfersize) * srb->transfersize;
692 US_DEBUGP("Max read is %d bytes\n", len);
693 buffer = kmalloc(len, GFP_NOIO);
694 if (buffer == NULL) // bloody hell!
695 return USB_STOR_TRANSPORT_FAILED;
696 sector = short_pack(data[7+3], data[7+2]);
698 sector |= short_pack(data[7+5], data[7+4]);
702 sg = (struct scatterlist *)srb->request_buffer;
703 sg_segment = 0; // for keeping track of where we are in
704 sg_offset = 0; // the scatter/gather list
707 while (transferred != srb->request_bufflen) {
709 if (len > srb->request_bufflen - transferred)
710 len = srb->request_bufflen - transferred;
712 data[3] = len&0xFF; // (cylL) = expected length (L)
713 data[4] = (len>>8)&0xFF; // (cylH) = expected length (H)
715 // Fix up the SCSI command sector and num sectors
717 data[7+2] = MSB_of(sector>>16); // SCSI command sector
718 data[7+3] = LSB_of(sector>>16);
719 data[7+4] = MSB_of(sector&0xFFFF);
720 data[7+5] = LSB_of(sector&0xFFFF);
721 if (data[7+0] == GPCMD_READ_CD)
723 data[7+7] = MSB_of(len / srb->transfersize); // SCSI command
724 data[7+8] = LSB_of(len / srb->transfersize); // num sectors
726 result = usbat_rw_block_test(us, USBAT_ATA,
728 0x10, 0x17, 0xFD, 0x30,
733 if (result != USB_STOR_TRANSPORT_GOOD)
736 // Transfer the received data into the srb buffer
739 memcpy(srb->request_buffer+transferred, buffer, len);
744 sg[sg_segment].length-sg_offset) {
745 memcpy(sg[sg_segment].address + sg_offset,
747 sg[sg_segment].length - sg_offset);
749 sg[sg_segment].length-sg_offset;
753 memcpy(sg[sg_segment].address + sg_offset,
756 sg_offset += (len - amount);
762 // Update the amount transferred and the sector number
765 sector += len / srb->transfersize;
767 } // while transferred != srb->request_bufflen
773 static int hp_8200e_select_and_test_registers(struct us_data *us) {
777 unsigned char status;
779 // try device = master, then device = slave.
781 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
783 if ( (result = usbat_write(us, USBAT_ATA, 0x16, selector)) !=
784 USB_STOR_TRANSPORT_GOOD)
787 if ( (result = usbat_read(us, USBAT_ATA, 0x17, &status)) !=
788 USB_STOR_TRANSPORT_GOOD)
791 if ( (result = usbat_read(us, USBAT_ATA, 0x16, &status)) !=
792 USB_STOR_TRANSPORT_GOOD)
795 if ( (result = usbat_read(us, USBAT_ATA, 0x14, &status)) !=
796 USB_STOR_TRANSPORT_GOOD)
799 if ( (result = usbat_read(us, USBAT_ATA, 0x15, &status)) !=
800 USB_STOR_TRANSPORT_GOOD)
803 if ( (result = usbat_write(us, USBAT_ATA, 0x14, 0x55)) !=
804 USB_STOR_TRANSPORT_GOOD)
807 if ( (result = usbat_write(us, USBAT_ATA, 0x15, 0xAA)) !=
808 USB_STOR_TRANSPORT_GOOD)
811 if ( (result = usbat_read(us, USBAT_ATA, 0x14, &status)) !=
812 USB_STOR_TRANSPORT_GOOD)
815 if ( (result = usbat_read(us, USBAT_ATA, 0x15, &status)) !=
816 USB_STOR_TRANSPORT_GOOD)
823 int init_8200e(struct us_data *us) {
826 unsigned char status;
828 // Enable peripheral control signals
830 if ( (result = usbat_write_user_io(us,
831 USBAT_UIO_OE1 | USBAT_UIO_OE0,
832 USBAT_UIO_EPAD | USBAT_UIO_1)) != USB_STOR_TRANSPORT_GOOD)
835 US_DEBUGP("INIT 1\n");
839 if ( (result = usbat_read_user_io(us, &status)) !=
840 USB_STOR_TRANSPORT_GOOD)
843 US_DEBUGP("INIT 2\n");
845 if ( (result = usbat_read_user_io(us, &status)) !=
846 USB_STOR_TRANSPORT_GOOD)
849 US_DEBUGP("INIT 3\n");
851 // Reset peripheral, enable periph control signals
852 // (bring reset signal up)
854 if ( (result = usbat_write_user_io(us,
855 USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
856 USBAT_UIO_EPAD | USBAT_UIO_1)) != USB_STOR_TRANSPORT_GOOD)
859 US_DEBUGP("INIT 4\n");
861 // Enable periph control signals
862 // (bring reset signal down)
864 if ( (result = usbat_write_user_io(us,
865 USBAT_UIO_OE1 | USBAT_UIO_OE0,
866 USBAT_UIO_EPAD | USBAT_UIO_1)) != USB_STOR_TRANSPORT_GOOD)
869 US_DEBUGP("INIT 5\n");
873 // Write 0x80 to ISA port 0x3F
875 if ( (result = usbat_write(us, USBAT_ISA, 0x3F, 0x80)) !=
876 USB_STOR_TRANSPORT_GOOD)
879 US_DEBUGP("INIT 6\n");
881 // Read ISA port 0x27
883 if ( (result = usbat_read(us, USBAT_ISA, 0x27, &status)) !=
884 USB_STOR_TRANSPORT_GOOD)
887 US_DEBUGP("INIT 7\n");
889 if ( (result = usbat_read_user_io(us, &status)) !=
890 USB_STOR_TRANSPORT_GOOD)
893 US_DEBUGP("INIT 8\n");
895 if ( (result = hp_8200e_select_and_test_registers(us)) !=
896 USB_STOR_TRANSPORT_GOOD)
899 US_DEBUGP("INIT 9\n");
901 if ( (result = usbat_read_user_io(us, &status)) !=
902 USB_STOR_TRANSPORT_GOOD)
905 US_DEBUGP("INIT 10\n");
907 // Enable periph control signals and card detect
909 if ( (result = usbat_write_user_io(us,
910 USBAT_UIO_ACKD |USBAT_UIO_OE1 | USBAT_UIO_OE0,
911 USBAT_UIO_EPAD | USBAT_UIO_1)) != USB_STOR_TRANSPORT_GOOD)
914 US_DEBUGP("INIT 11\n");
916 if ( (result = usbat_read_user_io(us, &status)) !=
917 USB_STOR_TRANSPORT_GOOD)
920 US_DEBUGP("INIT 12\n");
924 if ( (result = usbat_read_user_io(us, &status)) !=
925 USB_STOR_TRANSPORT_GOOD)
928 US_DEBUGP("INIT 13\n");
930 if ( (result = hp_8200e_select_and_test_registers(us)) !=
931 USB_STOR_TRANSPORT_GOOD)
934 US_DEBUGP("INIT 14\n");
936 if ( (result = usbat_set_shuttle_features(us,
937 0x83, 0x00, 0x88, 0x08, 0x15, 0x14)) !=
938 USB_STOR_TRANSPORT_GOOD)
941 US_DEBUGP("INIT 15\n");
947 * Transport for the HP 8200e
949 int hp8200e_transport(Scsi_Cmnd *srb, struct us_data *us)
952 unsigned char status;
953 unsigned char registers[32];
954 unsigned char data[32];
959 len = srb->request_bufflen;
961 /* Send A0 (ATA PACKET COMMAND).
962 Note: I guess we're never going to get any of the ATA
963 commands... just ATA Packet Commands.
976 data[3] = len&0xFF; // (cylL) = expected length (L)
977 data[4] = (len>>8)&0xFF; // (cylH) = expected length (H)
978 data[5] = 0xB0; // (device sel) = slave
979 data[6] = 0xA0; // (command) = ATA PACKET COMMAND
981 for (i=7; i<19; i++) {
983 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
986 result = usbat_read(us, USBAT_ATA, 0x17, &status);
987 US_DEBUGP("Status = %02X\n", status);
989 if (srb->cmnd[0] == TEST_UNIT_READY)
992 if (srb->sc_data_direction == SCSI_DATA_WRITE) {
994 result = usbat_rw_block_test(us, USBAT_ATA,
996 0x10, 0x17, 0xFD, 0x30,
999 len, srb->use_sg, 10);
1001 if (result == USB_STOR_TRANSPORT_GOOD) {
1003 US_DEBUGP("Wrote %08X bytes\n", transferred);
1008 } else if (srb->cmnd[0] == READ_10 ||
1009 srb->cmnd[0] == GPCMD_READ_CD) {
1011 return usbat_handle_read10(us, registers, data, srb);
1016 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1018 return USB_STOR_TRANSPORT_ERROR;
1021 if ( (result = usbat_multiple_write(us,
1023 registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
1027 // Write the 12-byte command header.
1029 // If the command is BLANK then set the timer for 75 minutes.
1030 // Otherwise set it for 10 minutes.
1032 // NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1033 // AT SPEED 4 IS UNRELIABLE!!!
1035 if ( (result = usbat_write_block(us,
1036 USBAT_ATA, 0x10, srb->cmnd, 12, 0,
1037 srb->cmnd[0]==GPCMD_BLANK ? 75 : 10)) !=
1038 USB_STOR_TRANSPORT_GOOD) {
1042 // If there is response data to be read in
1045 if (len != 0 && (srb->sc_data_direction == SCSI_DATA_READ)) {
1047 // How many bytes to read in? Check cylL register
1049 if ( (result = usbat_read(us, USBAT_ATA, 0x14, &status)) !=
1050 USB_STOR_TRANSPORT_GOOD) {
1054 if (len>0xFF) { // need to read cylH also
1056 if ( (result = usbat_read(us, USBAT_ATA, 0x15,
1058 USB_STOR_TRANSPORT_GOOD) {
1061 len += ((unsigned int)status)<<8;
1067 result = usbat_read_block(us, USBAT_ATA, 0x10,
1068 srb->request_buffer, len, srb->use_sg);
1070 /* Debug-print the first 32 bytes of the transfer */
1074 for (i=0; i<len && i<32; i++) {
1075 sprintf(string+strlen(string), "%02X ",
1076 ((unsigned char *)srb->request_buffer)[i]);
1078 US_DEBUGP("%s\n", string);
1083 US_DEBUGP("%s\n", string);