- Linus: more PageDirty / swapcache handling
[davej-history.git] / drivers / usb / storage / shuttle_usbat.c
blobc0de9c14c8a73f9fd40768d42fb151e69fc62b15
1 /* Driver for SCM Microsystems USB-ATAPI cable
3 * $Id: shuttle_usbat.c,v 1.11 2000/11/13 22:29:36 mdharm Exp $
5 * Current development and maintenance by:
6 * (c) 2000 Robert Baruch (autophile@dol.net)
8 * Many originally ATAPI devices were slightly modified to meet the USB
9 * market by using some kind of translation from ATAPI to USB on the host,
10 * and the peripheral would translate from USB back to ATAPI.
12 * SCM Microsystems (www.scmmicro.com) makes a device, sold to OEM's only,
13 * which does the USB-to-ATAPI conversion. By obtaining the data sheet on
14 * their device under nondisclosure agreement, I have been able to write
15 * this driver for Linux.
17 * The chip used in the device can also be used for EPP and ISA translation
18 * as well. This driver is only guaranteed to work with the ATAPI
19 * translation.
21 * The only peripheral that I know of (as of 8 Sep 2000) that uses this
22 * device is the Hewlett-Packard 8200e/8210e CD-Writer Plus.
24 * This program is free software; you can redistribute it and/or modify it
25 * under the terms of the GNU General Public License as published by the
26 * Free Software Foundation; either version 2, or (at your option) any
27 * later version.
29 * This program is distributed in the hope that it will be useful, but
30 * WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * General Public License for more details.
34 * You should have received a copy of the GNU General Public License along
35 * with this program; if not, write to the Free Software Foundation, Inc.,
36 * 675 Mass Ave, Cambridge, MA 02139, USA.
39 #include "transport.h"
40 #include "protocol.h"
41 #include "usb.h"
42 #include "debug.h"
43 #include "shuttle_usbat.h"
45 #include <linux/sched.h>
46 #include <linux/errno.h>
47 #include <linux/malloc.h>
49 extern int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
50 u8 request, u8 requesttype, u16 value, u16 index,
51 void *data, u16 size);
52 extern int usb_stor_bulk_msg(struct us_data *us, void *data, int pipe,
53 unsigned int len, unsigned int *act_len);
55 #define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
56 #define LSB_of(s) ((s)&0xFF)
57 #define MSB_of(s) ((s)>>8)
59 int transferred = 0;
62 * Send a control message and wait for the response.
64 * us - the pointer to the us_data structure for the device to use
66 * request - the URB Setup Packet's first 6 bytes. The first byte always
67 * corresponds to the request type, and the second byte always corresponds
68 * to the request. The other 4 bytes do not correspond to value and index,
69 * since they are used in a custom way by the SCM protocol.
71 * xfer_data - a buffer from which to get, or to which to store, any data
72 * that gets send or received, respectively, with the URB. Even though
73 * it looks like we allocate a buffer in this code for the data, xfer_data
74 * must contain enough allocated space.
76 * xfer_len - the number of bytes to send or receive with the URB.
80 static int usbat_send_control(struct us_data *us,
81 int pipe,
82 unsigned char request,
83 unsigned char requesttype,
84 unsigned short value,
85 unsigned short index,
86 unsigned char *xfer_data,
87 unsigned int xfer_len) {
89 int result;
91 // Send the URB to the device and wait for a response.
93 /* Why are request and request type reversed in this call? */
95 result = usb_stor_control_msg(us, pipe,
96 request, requesttype, value, index,
97 xfer_data, xfer_len);
100 // Check the return code for the command.
102 if (result < 0) {
103 /* if the command was aborted, indicate that */
104 if (result == -ENOENT)
105 return USB_STOR_TRANSPORT_ABORTED;
107 /* a stall is a fatal condition from the device */
108 if (result == -EPIPE) {
109 US_DEBUGP("-- Stall on control pipe. Clearing\n");
110 result = usb_clear_halt(us->pusb_dev, pipe);
111 US_DEBUGP("-- usb_clear_halt() returns %d\n", result);
112 return USB_STOR_TRANSPORT_FAILED;
115 /* Uh oh... serious problem here */
116 return USB_STOR_TRANSPORT_ERROR;
119 return USB_STOR_TRANSPORT_GOOD;
122 static int usbat_raw_bulk(struct us_data *us,
123 int direction,
124 unsigned char *data,
125 unsigned short len) {
127 int result;
128 int act_len;
129 int pipe;
131 if (direction == SCSI_DATA_READ)
132 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
133 else
134 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
136 result = usb_stor_bulk_msg(us, data, pipe, len, &act_len);
138 /* if we stall, we need to clear it before we go on */
139 if (result == -EPIPE) {
140 US_DEBUGP("EPIPE: clearing endpoint halt for"
141 " pipe 0x%x, stalled at %d bytes\n",
142 pipe, act_len);
143 usb_clear_halt(us->pusb_dev, pipe);
146 if (result) {
148 /* NAK - that means we've retried a few times already */
149 if (result == -ETIMEDOUT) {
150 US_DEBUGP("usbat_raw_bulk():"
151 " device NAKed\n");
152 return US_BULK_TRANSFER_FAILED;
155 /* -ENOENT -- we canceled this transfer */
156 if (result == -ENOENT) {
157 US_DEBUGP("usbat_raw_bulk():"
158 " transfer aborted\n");
159 return US_BULK_TRANSFER_ABORTED;
162 if (result == -EPIPE) {
163 US_DEBUGP("usbat_raw_bulk():"
164 " output pipe stalled\n");
165 return US_BULK_TRANSFER_SHORT;
168 /* the catch-all case */
169 US_DEBUGP("us_transfer_partial(): unknown error\n");
170 return US_BULK_TRANSFER_FAILED;
173 if (act_len != len) {
174 US_DEBUGP("Warning: Transferred only %d bytes\n",
175 act_len);
176 return US_BULK_TRANSFER_SHORT;
179 US_DEBUGP("Transferred %s %d of %d bytes\n",
180 direction==SCSI_DATA_READ ? "in" : "out", act_len, len);
182 return US_BULK_TRANSFER_GOOD;
186 * Note: direction must be set if command_len == 0.
189 static int usbat_bulk_transport(struct us_data *us,
190 unsigned char *command,
191 unsigned short command_len,
192 int direction,
193 unsigned char *data,
194 unsigned short len,
195 int use_sg) {
197 int result = USB_STOR_TRANSPORT_GOOD;
198 int transferred = 0;
199 int i;
200 struct scatterlist *sg;
202 if (len==0)
203 return USB_STOR_TRANSPORT_GOOD;
205 /* transfer the data payload for the command, if there is any */
207 if (command_len != 0)
208 direction = (command[0]&0x80) ? SCSI_DATA_READ :
209 SCSI_DATA_WRITE;
211 if (!use_sg)
212 result = usbat_raw_bulk(us, direction, data, len);
213 else {
214 sg = (struct scatterlist *)data;
215 for (i=0; i<use_sg && transferred<len; i++) {
216 result = usbat_raw_bulk(us, direction,
217 sg[i].address,
218 len-transferred > sg[i].length ?
219 sg[i].length : len-transferred);
220 if (result!=US_BULK_TRANSFER_GOOD)
221 break;
222 transferred += sg[i].length;
226 return result;
229 int usbat_read(struct us_data *us,
230 unsigned char access,
231 unsigned char reg,
232 unsigned char *content) {
234 int result;
236 result = usbat_send_control(us,
237 usb_rcvctrlpipe(us->pusb_dev,0),
238 access,
239 0xC0,
240 (u16)reg,
242 content,
245 return result;
248 int usbat_write(struct us_data *us,
249 unsigned char access,
250 unsigned char reg,
251 unsigned char content) {
253 int result;
255 result = usbat_send_control(us,
256 usb_sndctrlpipe(us->pusb_dev,0),
257 access|0x01,
258 0x40,
259 short_pack(reg, content),
261 NULL,
264 return result;
267 int usbat_set_shuttle_features(struct us_data *us,
268 unsigned char external_trigger,
269 unsigned char epp_control,
270 unsigned char mask_byte,
271 unsigned char test_pattern,
272 unsigned char subcountH,
273 unsigned char subcountL) {
275 int result;
276 unsigned char command[8] = {
277 0x40, 0x81, epp_control, external_trigger,
278 test_pattern, mask_byte, subcountL, subcountH
281 result = usbat_send_control(us,
282 usb_sndctrlpipe(us->pusb_dev,0),
283 0x80,
284 0x40,
287 command,
290 return result;
293 int usbat_read_block(struct us_data *us,
294 unsigned char access,
295 unsigned char reg,
296 unsigned char *content,
297 unsigned short len,
298 int use_sg) {
300 int result;
301 unsigned char command[8] = {
302 0xC0, access|0x02, reg, 0x00, 0x00, 0x00,
303 LSB_of(len), MSB_of(len)
306 result = usbat_send_control(us,
307 usb_sndctrlpipe(us->pusb_dev,0),
308 0x80,
309 0x40,
312 command,
315 if (result != USB_STOR_TRANSPORT_GOOD)
316 return result;
318 result = usbat_bulk_transport(us,
319 NULL, 0, SCSI_DATA_READ, content, len, use_sg);
321 return result;
325 * Block, waiting for an ATA device to become not busy or to report
326 * an error condition.
329 int usbat_wait_not_busy(struct us_data *us, int minutes) {
331 int i;
332 int result;
333 unsigned char status;
335 /* Synchronizing cache on a CDR could take a heck of a long time,
336 * but probably not more than 10 minutes or so. On the other hand,
337 * doing a full blank on a CDRW at speed 1 will take about 75
338 * minutes!
341 for (i=0; i<1200+minutes*60; i++) {
343 result = usbat_read(us, USBAT_ATA, 0x17, &status);
345 if (result!=USB_STOR_TRANSPORT_GOOD)
346 return result;
347 if (status&0x01) // check condition
348 return USB_STOR_TRANSPORT_FAILED;
349 if (status&0x20) // device fault
350 return USB_STOR_TRANSPORT_FAILED;
352 if ((status&0x80)!=0x80) { // not busy
353 US_DEBUGP("Waited not busy for %d steps\n", i);
354 return USB_STOR_TRANSPORT_GOOD;
357 if (i<500)
358 wait_ms(10); // 5 seconds
359 else if (i<700)
360 wait_ms(50); // 10 seconds
361 else if (i<1200)
362 wait_ms(100); // 50 seconds
363 else
364 wait_ms(1000); // X minutes
367 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
368 minutes);
369 return USB_STOR_TRANSPORT_FAILED;
372 int usbat_write_block(struct us_data *us,
373 unsigned char access,
374 unsigned char reg,
375 unsigned char *content,
376 unsigned short len,
377 int use_sg,
378 int minutes) {
380 int result;
381 unsigned char command[8] = {
382 0x40, access|0x03, reg, 0x00, 0x00, 0x00,
383 LSB_of(len), MSB_of(len)
386 result = usbat_send_control(us,
387 usb_sndctrlpipe(us->pusb_dev,0),
388 0x80,
389 0x40,
392 command,
395 if (result != USB_STOR_TRANSPORT_GOOD)
396 return result;
398 result = usbat_bulk_transport(us,
399 NULL, 0, SCSI_DATA_WRITE, content, len, use_sg);
401 if (result != USB_STOR_TRANSPORT_GOOD)
402 return result;
404 return usbat_wait_not_busy(us, minutes);
407 int usbat_rw_block_test(struct us_data *us,
408 unsigned char access,
409 unsigned char *registers,
410 unsigned char *data_out,
411 unsigned short num_registers,
412 unsigned char data_reg,
413 unsigned char status_reg,
414 unsigned char timeout,
415 unsigned char qualifier,
416 int direction,
417 unsigned char *content,
418 unsigned short len,
419 int use_sg,
420 int minutes) {
422 int result;
424 // Not really sure the 0x07, 0x17, 0xfc, 0xe7 is necessary here,
425 // but that's what came out of the trace every single time.
427 unsigned char command[16] = {
428 0x40, access|0x07, 0x07, 0x17, 0xfc, 0xe7,
429 LSB_of(num_registers*2), MSB_of(num_registers*2),
430 (direction==SCSI_DATA_WRITE ? 0x40 : 0xC0),
431 access|(direction==SCSI_DATA_WRITE ? 0x05 : 0x04),
432 data_reg, status_reg,
433 timeout, qualifier, LSB_of(len), MSB_of(len)
436 int i;
437 unsigned char data[num_registers*2];
438 unsigned char status;
440 for (i=0; i<num_registers; i++) {
441 data[i<<1] = registers[i];
442 data[1+(i<<1)] = data_out[i];
445 for (i=0; i<20; i++) {
448 * The first time we send the full command, which consists
449 * of downloading the SCSI command followed by downloading
450 * the data via a write-and-test. Any other time we only
451 * send the command to download the data -- the SCSI command
452 * is still 'active' in some sense in the device.
454 * We're only going to try sending the data 10 times. After
455 * that, we just return a failure.
458 result = usbat_send_control(us,
459 usb_sndctrlpipe(us->pusb_dev,0),
460 0x80,
461 0x40,
464 (i==0 ? command : command+8),
465 (i==0 ? 16 : 8));
467 if (result != USB_STOR_TRANSPORT_GOOD)
468 return result;
470 if (i==0) {
472 result = usbat_bulk_transport(us,
473 NULL, 0, SCSI_DATA_WRITE,
474 data, num_registers*2, 0);
476 if (result!=USB_STOR_TRANSPORT_GOOD)
477 return result;
482 //US_DEBUGP("Transfer %s %d bytes, sg buffers %d\n",
483 // direction == SCSI_DATA_WRITE ? "out" : "in",
484 // len, use_sg);
486 result = usbat_bulk_transport(us,
487 NULL, 0, direction, content, len, use_sg);
490 * If we get a stall on the bulk download, we'll retry
491 * the bulk download -- but not the SCSI command because
492 * in some sense the SCSI command is still 'active' and
493 * waiting for the data. Don't ask me why this should be;
494 * I'm only following what the Windoze driver did.
496 * Note that a stall for the test-and-read/write command means
497 * that the test failed. In this case we're testing to make
498 * sure that the device is error-free
499 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
500 * hypothesis is that the USBAT chip somehow knows what
501 * the device will accept, but doesn't give the device any
502 * data until all data is received. Thus, the device would
503 * still be waiting for the first byte of data if a stall
504 * occurs, even if the stall implies that some data was
505 * transferred.
508 if (result == US_BULK_TRANSFER_SHORT) {
511 * If we're reading and we stalled, then clear
512 * the bulk output pipe only the first time.
515 if (direction==SCSI_DATA_READ && i==0)
516 usb_clear_halt(us->pusb_dev,
517 usb_sndbulkpipe(us->pusb_dev,
518 us->ep_out));
520 * Read status: is the device angry, or just busy?
523 result = usbat_read(us, USBAT_ATA,
524 direction==SCSI_DATA_WRITE ? 0x17 : 0x0E,
525 &status);
527 if (result!=USB_STOR_TRANSPORT_GOOD)
528 return result;
529 if (status&0x01) // check condition
530 return USB_STOR_TRANSPORT_FAILED;
531 if (status&0x20) // device fault
532 return USB_STOR_TRANSPORT_FAILED;
534 US_DEBUGP("Redoing %s\n",
535 direction==SCSI_DATA_WRITE ? "write" : "read");
537 } else if (result != US_BULK_TRANSFER_GOOD)
538 return result;
539 else
540 return usbat_wait_not_busy(us, minutes);
544 US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
545 direction==SCSI_DATA_WRITE ? "Writing" : "Reading");
547 return USB_STOR_TRANSPORT_FAILED;
551 * Write data to multiple registers at once. Not meant for large
552 * transfers of data!
555 int usbat_multiple_write(struct us_data *us,
556 unsigned char access,
557 unsigned char *registers,
558 unsigned char *data_out,
559 unsigned short num_registers) {
561 int result;
562 unsigned char data[num_registers*2];
563 int i;
564 unsigned char command[8] = {
565 0x40, access|0x07, 0x00, 0x00, 0x00, 0x00,
566 LSB_of(num_registers*2), MSB_of(num_registers*2)
569 for (i=0; i<num_registers; i++) {
570 data[i<<1] = registers[i];
571 data[1+(i<<1)] = data_out[i];
574 result = usbat_send_control(us,
575 usb_sndctrlpipe(us->pusb_dev,0),
576 0x80,
577 0x40,
580 command,
583 if (result != USB_STOR_TRANSPORT_GOOD)
584 return result;
586 result = usbat_bulk_transport(us,
587 NULL, 0, SCSI_DATA_WRITE, data, num_registers*2, 0);
589 if (result!=USB_STOR_TRANSPORT_GOOD)
590 return result;
592 return usbat_wait_not_busy(us, 0);
595 int usbat_read_user_io(struct us_data *us,
596 unsigned char *data_flags) {
598 int result;
600 result = usbat_send_control(us,
601 usb_rcvctrlpipe(us->pusb_dev,0),
602 0x82,
603 0xC0,
606 data_flags,
609 return result;
612 int usbat_write_user_io(struct us_data *us,
613 unsigned char enable_flags,
614 unsigned char data_flags) {
616 int result;
618 result = usbat_send_control(us,
619 usb_sndctrlpipe(us->pusb_dev,0),
620 0x82,
621 0x40,
622 short_pack(enable_flags, data_flags),
624 NULL,
627 return result;
631 * Squeeze a potentially huge (> 65535 byte) read10 command into
632 * a little ( <= 65535 byte) ATAPI pipe
635 int usbat_handle_read10(struct us_data *us,
636 unsigned char *registers,
637 unsigned char *data,
638 Scsi_Cmnd *srb) {
640 int result = USB_STOR_TRANSPORT_GOOD;
641 unsigned char *buffer;
642 unsigned int len;
643 unsigned int sector;
644 unsigned int amount;
645 struct scatterlist *sg = NULL;
646 int sg_segment = 0;
647 int sg_offset = 0;
649 US_DEBUGP("handle_read10: transfersize %d\n",
650 srb->transfersize);
652 if (srb->request_bufflen < 0x10000) {
654 result = usbat_rw_block_test(us, USBAT_ATA,
655 registers, data, 19,
656 0x10, 0x17, 0xFD, 0x30,
657 SCSI_DATA_READ,
658 srb->request_buffer,
659 srb->request_bufflen, srb->use_sg, 1);
661 return result;
665 * Since we're requesting more data than we can handle in
666 * a single read command (max is 64k-1), we will perform
667 * multiple reads, but each read must be in multiples of
668 * a sector. Luckily the sector size is in srb->transfersize
669 * (see linux/drivers/scsi/sr.c).
672 if (data[7+0] == GPCMD_READ_CD) {
673 len = short_pack(data[7+9], data[7+8]);
674 len <<= 16;
675 len |= data[7+7];
676 srb->transfersize = srb->request_bufflen/len;
680 len = (65535/srb->transfersize) * srb->transfersize;
681 US_DEBUGP("Max read is %d bytes\n", len);
682 buffer = kmalloc(len, GFP_KERNEL);
683 if (buffer == NULL) // bloody hell!
684 return USB_STOR_TRANSPORT_FAILED;
685 sector = short_pack(data[7+3], data[7+2]);
686 sector <<= 16;
687 sector |= short_pack(data[7+5], data[7+4]);
688 transferred = 0;
690 if (srb->use_sg) {
691 sg = (struct scatterlist *)srb->request_buffer;
692 sg_segment = 0; // for keeping track of where we are in
693 sg_offset = 0; // the scatter/gather list
696 while (transferred != srb->request_bufflen) {
698 if (len > srb->request_bufflen - transferred)
699 len = srb->request_bufflen - transferred;
701 data[3] = len&0xFF; // (cylL) = expected length (L)
702 data[4] = (len>>8)&0xFF; // (cylH) = expected length (H)
704 // Fix up the SCSI command sector and num sectors
706 data[7+2] = MSB_of(sector>>16); // SCSI command sector
707 data[7+3] = LSB_of(sector>>16);
708 data[7+4] = MSB_of(sector&0xFFFF);
709 data[7+5] = LSB_of(sector&0xFFFF);
710 if (data[7+0] == GPCMD_READ_CD)
711 data[7+6] = 0;
712 data[7+7] = MSB_of(len / srb->transfersize); // SCSI command
713 data[7+8] = LSB_of(len / srb->transfersize); // num sectors
715 result = usbat_rw_block_test(us, USBAT_ATA,
716 registers, data, 19,
717 0x10, 0x17, 0xFD, 0x30,
718 SCSI_DATA_READ,
719 buffer,
720 len, 0, 1);
722 if (result != USB_STOR_TRANSPORT_GOOD)
723 break;
725 // Transfer the received data into the srb buffer
727 if (!srb->use_sg) {
728 memcpy(srb->request_buffer+transferred, buffer, len);
729 } else {
730 amount = 0;
731 while (amount<len) {
732 if (len - amount >=
733 sg[sg_segment].length-sg_offset) {
734 memcpy(sg[sg_segment].address + sg_offset,
735 buffer + amount,
736 sg[sg_segment].length - sg_offset);
737 amount +=
738 sg[sg_segment].length-sg_offset;
739 sg_segment++;
740 sg_offset=0;
741 } else {
742 memcpy(sg[sg_segment].address + sg_offset,
743 buffer + amount,
744 len - amount);
745 sg_offset += (len - amount);
746 amount = len;
751 // Update the amount transferred and the sector number
753 transferred += len;
754 sector += len / srb->transfersize;
756 } // while transferred != srb->request_bufflen
758 kfree(buffer);
759 return result;
762 static int hp_8200e_select_and_test_registers(struct us_data *us) {
764 int result;
765 int selector;
766 unsigned char status;
768 // try device = master, then device = slave.
770 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
772 if ( (result = usbat_write(us, USBAT_ATA, 0x16, selector)) !=
773 USB_STOR_TRANSPORT_GOOD)
774 return result;
776 if ( (result = usbat_read(us, USBAT_ATA, 0x17, &status)) !=
777 USB_STOR_TRANSPORT_GOOD)
778 return result;
780 if ( (result = usbat_read(us, USBAT_ATA, 0x16, &status)) !=
781 USB_STOR_TRANSPORT_GOOD)
782 return result;
784 if ( (result = usbat_read(us, USBAT_ATA, 0x14, &status)) !=
785 USB_STOR_TRANSPORT_GOOD)
786 return result;
788 if ( (result = usbat_read(us, USBAT_ATA, 0x15, &status)) !=
789 USB_STOR_TRANSPORT_GOOD)
790 return result;
792 if ( (result = usbat_write(us, USBAT_ATA, 0x14, 0x55)) !=
793 USB_STOR_TRANSPORT_GOOD)
794 return result;
796 if ( (result = usbat_write(us, USBAT_ATA, 0x15, 0xAA)) !=
797 USB_STOR_TRANSPORT_GOOD)
798 return result;
800 if ( (result = usbat_read(us, USBAT_ATA, 0x14, &status)) !=
801 USB_STOR_TRANSPORT_GOOD)
802 return result;
804 if ( (result = usbat_read(us, USBAT_ATA, 0x15, &status)) !=
805 USB_STOR_TRANSPORT_GOOD)
806 return result;
809 return result;
812 int init_8200e(struct us_data *us) {
814 int result;
815 unsigned char status;
817 // Enable peripheral control signals
819 if ( (result = usbat_write_user_io(us,
820 USBAT_UIO_OE1 | USBAT_UIO_OE0,
821 USBAT_UIO_EPAD | USBAT_UIO_1)) != USB_STOR_TRANSPORT_GOOD)
822 return result;
824 US_DEBUGP("INIT 1\n");
826 wait_ms(2000);
828 if ( (result = usbat_read_user_io(us, &status)) !=
829 USB_STOR_TRANSPORT_GOOD)
830 return result;
832 US_DEBUGP("INIT 2\n");
834 if ( (result = usbat_read_user_io(us, &status)) !=
835 USB_STOR_TRANSPORT_GOOD)
836 return result;
838 US_DEBUGP("INIT 3\n");
840 // Reset peripheral, enable periph control signals
841 // (bring reset signal up)
843 if ( (result = usbat_write_user_io(us,
844 USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
845 USBAT_UIO_EPAD | USBAT_UIO_1)) != USB_STOR_TRANSPORT_GOOD)
846 return result;
848 US_DEBUGP("INIT 4\n");
850 // Enable periph control signals
851 // (bring reset signal down)
853 if ( (result = usbat_write_user_io(us,
854 USBAT_UIO_OE1 | USBAT_UIO_OE0,
855 USBAT_UIO_EPAD | USBAT_UIO_1)) != USB_STOR_TRANSPORT_GOOD)
856 return result;
858 US_DEBUGP("INIT 5\n");
860 wait_ms(250);
862 // Write 0x80 to ISA port 0x3F
864 if ( (result = usbat_write(us, USBAT_ISA, 0x3F, 0x80)) !=
865 USB_STOR_TRANSPORT_GOOD)
866 return result;
868 US_DEBUGP("INIT 6\n");
870 // Read ISA port 0x27
872 if ( (result = usbat_read(us, USBAT_ISA, 0x27, &status)) !=
873 USB_STOR_TRANSPORT_GOOD)
874 return result;
876 US_DEBUGP("INIT 7\n");
878 if ( (result = usbat_read_user_io(us, &status)) !=
879 USB_STOR_TRANSPORT_GOOD)
880 return result;
882 US_DEBUGP("INIT 8\n");
884 if ( (result = hp_8200e_select_and_test_registers(us)) !=
885 USB_STOR_TRANSPORT_GOOD)
886 return result;
888 US_DEBUGP("INIT 9\n");
890 if ( (result = usbat_read_user_io(us, &status)) !=
891 USB_STOR_TRANSPORT_GOOD)
892 return result;
894 US_DEBUGP("INIT 10\n");
896 // Enable periph control signals and card detect
898 if ( (result = usbat_write_user_io(us,
899 USBAT_UIO_ACKD |USBAT_UIO_OE1 | USBAT_UIO_OE0,
900 USBAT_UIO_EPAD | USBAT_UIO_1)) != USB_STOR_TRANSPORT_GOOD)
901 return result;
903 US_DEBUGP("INIT 11\n");
905 if ( (result = usbat_read_user_io(us, &status)) !=
906 USB_STOR_TRANSPORT_GOOD)
907 return result;
909 US_DEBUGP("INIT 12\n");
911 wait_ms(1400);
913 if ( (result = usbat_read_user_io(us, &status)) !=
914 USB_STOR_TRANSPORT_GOOD)
915 return result;
917 US_DEBUGP("INIT 13\n");
919 if ( (result = hp_8200e_select_and_test_registers(us)) !=
920 USB_STOR_TRANSPORT_GOOD)
921 return result;
923 US_DEBUGP("INIT 14\n");
925 if ( (result = usbat_set_shuttle_features(us,
926 0x83, 0x00, 0x88, 0x08, 0x15, 0x14)) !=
927 USB_STOR_TRANSPORT_GOOD)
928 return result;
930 US_DEBUGP("INIT 15\n");
932 return result;
936 * Transport for the HP 8200e
938 int hp8200e_transport(Scsi_Cmnd *srb, struct us_data *us)
940 int result;
941 unsigned char status;
942 unsigned char registers[32];
943 unsigned char data[32];
944 unsigned int len;
945 int i;
946 char string[64];
948 len = srb->request_bufflen;
950 /* Send A0 (ATA PACKET COMMAND).
951 Note: I guess we're never going to get any of the ATA
952 commands... just ATA Packet Commands.
955 registers[0] = 0x11;
956 registers[1] = 0x12;
957 registers[2] = 0x13;
958 registers[3] = 0x14;
959 registers[4] = 0x15;
960 registers[5] = 0x16;
961 registers[6] = 0x17;
962 data[0] = 0x00;
963 data[1] = 0x00;
964 data[2] = 0x00;
965 data[3] = len&0xFF; // (cylL) = expected length (L)
966 data[4] = (len>>8)&0xFF; // (cylH) = expected length (H)
967 data[5] = 0xB0; // (device sel) = slave
968 data[6] = 0xA0; // (command) = ATA PACKET COMMAND
970 for (i=7; i<19; i++) {
971 registers[i] = 0x10;
972 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
975 if (srb->cmnd[0] == TEST_UNIT_READY)
976 transferred = 0;
978 if (srb->sc_data_direction == SCSI_DATA_WRITE) {
980 result = usbat_rw_block_test(us, USBAT_ATA,
981 registers, data, 19,
982 0x10, 0x17, 0xFD, 0x30,
983 SCSI_DATA_WRITE,
984 srb->request_buffer,
985 len, srb->use_sg, 10);
987 if (result == USB_STOR_TRANSPORT_GOOD) {
988 transferred += len;
989 US_DEBUGP("Wrote %08X bytes\n", transferred);
992 return result;
994 } else if (srb->cmnd[0] == READ_10 ||
995 srb->cmnd[0] == GPCMD_READ_CD) {
997 return usbat_handle_read10(us, registers, data, srb);
1001 if (len > 0xFFFF) {
1002 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1003 len);
1004 return USB_STOR_TRANSPORT_ERROR;
1007 if ( (result = usbat_multiple_write(us,
1008 USBAT_ATA,
1009 registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
1010 return result;
1013 // Write the 12-byte command header.
1015 // If the command is BLANK then set the timer for 75 minutes.
1016 // Otherwise set it for 10 minutes.
1018 // NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1019 // AT SPEED 4 IS UNRELIABLE!!!
1021 if ( (result = usbat_write_block(us,
1022 USBAT_ATA, 0x10, srb->cmnd, 12, 0,
1023 srb->cmnd[0]==GPCMD_BLANK ? 75 : 10)) !=
1024 USB_STOR_TRANSPORT_GOOD) {
1025 return result;
1028 // If there is response data to be read in
1029 // then do it here.
1031 if (len != 0 && (srb->sc_data_direction == SCSI_DATA_READ)) {
1033 // How many bytes to read in? Check cylL register
1035 if ( (result = usbat_read(us, USBAT_ATA, 0x14, &status)) !=
1036 USB_STOR_TRANSPORT_GOOD) {
1037 return result;
1040 if (len>0xFF) { // need to read cylH also
1041 len = status;
1042 if ( (result = usbat_read(us, USBAT_ATA, 0x15,
1043 &status)) !=
1044 USB_STOR_TRANSPORT_GOOD) {
1045 return result;
1047 len += ((unsigned int)status)<<8;
1049 else
1050 len = status;
1053 result = usbat_read_block(us, USBAT_ATA, 0x10,
1054 srb->request_buffer, len, srb->use_sg);
1056 /* Debug-print the first 32 bytes of the transfer */
1058 if (!srb->use_sg) {
1059 string[0] = 0;
1060 for (i=0; i<len && i<32; i++) {
1061 sprintf(string+strlen(string), "%02X ",
1062 ((unsigned char *)srb->request_buffer)[i]);
1063 if ((i%16)==15) {
1064 US_DEBUGP("%s\n", string);
1065 string[0] = 0;
1068 if (string[0]!=0)
1069 US_DEBUGP("%s\n", string);
1073 return result;