patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / usb / storage / shuttle_usbat.c
1 /* Driver for SCM Microsystems USB-ATAPI cable
2  *
3  * $Id: shuttle_usbat.c,v 1.17 2002/04/22 03:39:43 mdharm Exp $
4  *
5  * Current development and maintenance by:
6  *   (c) 2000, 2001 Robert Baruch (autophile@starband.net)
7  *
8  * Developed with the assistance of:
9  *   (c) 2002 Alan Stern <stern@rowland.org>
10  *
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.
14  *
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.
19  *
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
22  * translation.
23  *
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.
26  *
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
30  * later version.
31  *
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.
36  *
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.
40  */
41
42 #include "transport.h"
43 #include "protocol.h"
44 #include "usb.h"
45 #include "debug.h"
46 #include "shuttle_usbat.h"
47
48 #include <linux/sched.h>
49 #include <linux/errno.h>
50 #include <linux/slab.h>
51
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)
55
56 int transferred = 0;
57
58 static int usbat_read(struct us_data *us,
59                       unsigned char access,
60                       unsigned char reg,
61                       unsigned char *content)
62 {
63         int result;
64
65         result = usb_stor_ctrl_transfer(us,
66                 us->recv_ctrl_pipe,
67                 access,
68                 0xC0,
69                 (u16)reg,
70                 0,
71                 content,
72                 1);
73
74         return result;
75 }
76
77 static int usbat_write(struct us_data *us,
78                        unsigned char access,
79                        unsigned char reg,
80                        unsigned char content)
81 {
82         int result;
83
84         result = usb_stor_ctrl_transfer(us,
85                 us->send_ctrl_pipe,
86                 access|0x01,
87                 0x40,
88                 short_pack(reg, content),
89                 0,
90                 NULL,
91                 0);
92
93         return result;
94 }
95
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)
103 {
104         int result;
105         unsigned char *command = us->iobuf;
106
107         command[0] = 0x40;
108         command[1] = 0x81;
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;
115
116         result = usb_stor_ctrl_transfer(us,
117                 us->send_ctrl_pipe,
118                 0x80,
119                 0x40,
120                 0,
121                 0,
122                 command,
123                 8);
124
125         return result;
126 }
127
128 static int usbat_read_block(struct us_data *us,
129                             unsigned char access,
130                             unsigned char reg,
131                             unsigned char *content,
132                             unsigned short len,
133                             int use_sg)
134 {
135         int result;
136         unsigned char *command = us->iobuf;
137
138         if (!len)
139                 return USB_STOR_TRANSPORT_GOOD;
140
141         command[0] = 0xC0;
142         command[1] = access | 0x02;
143         command[2] = reg;
144         command[3] = 0;
145         command[4] = 0;
146         command[5] = 0;
147         command[6] = LSB_of(len);
148         command[7] = MSB_of(len);
149
150         result = usb_stor_ctrl_transfer(us,
151                 us->send_ctrl_pipe,
152                 0x80,
153                 0x40,
154                 0,
155                 0,
156                 command,
157                 8);
158
159         if (result != USB_STOR_XFER_GOOD)
160                 return USB_STOR_TRANSPORT_ERROR;
161
162         result = usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe,
163                         content, len, use_sg, NULL);
164
165         return (result == USB_STOR_XFER_GOOD ?
166                         USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
167 }
168
169 /*
170  * Block, waiting for an ATA device to become not busy or to report
171  * an error condition.
172  */
173
174 static int usbat_wait_not_busy(struct us_data *us, int minutes)
175 {
176         int i;
177         int result;
178         unsigned char *status = us->iobuf;
179
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
183          * minutes!
184          */
185
186         for (i=0; i<1200+minutes*60; i++) {
187
188                 result = usbat_read(us, USBAT_ATA, 0x17, status);
189
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;
195                 }
196                 if (*status & 0x20) // device fault
197                         return USB_STOR_TRANSPORT_FAILED;
198
199                 if ((*status & 0x80)==0x00) { // not busy
200                         US_DEBUGP("Waited not busy for %d steps\n", i);
201                         return USB_STOR_TRANSPORT_GOOD;
202                 }
203
204                 if (i<500)
205                         msleep(10); // 5 seconds
206                 else if (i<700)
207                         msleep(50); // 10 seconds
208                 else if (i<1200)
209                         msleep(100); // 50 seconds
210                 else
211                         msleep(1000); // X minutes
212         }
213
214         US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
215                 minutes);
216         return USB_STOR_TRANSPORT_FAILED;
217 }
218
219 static int usbat_write_block(struct us_data *us,
220                              unsigned char access, 
221                              unsigned char reg,
222                              unsigned char *content,
223                              unsigned short len,
224                              int use_sg, int minutes)
225 {
226         int result;
227         unsigned char *command = us->iobuf;
228
229         if (!len)
230                 return USB_STOR_TRANSPORT_GOOD;
231
232         command[0] = 0x40;
233         command[1] = access | 0x03;
234         command[2] = reg;
235         command[3] = 0;
236         command[4] = 0;
237         command[5] = 0;
238         command[6] = LSB_of(len);
239         command[7] = MSB_of(len);
240
241         result = usb_stor_ctrl_transfer(us,
242                 us->send_ctrl_pipe,
243                 0x80,
244                 0x40,
245                 0,
246                 0,
247                 command,
248                 8);
249
250         if (result != USB_STOR_XFER_GOOD)
251                 return USB_STOR_TRANSPORT_ERROR;
252
253         result = usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe,
254                         content, len, use_sg, NULL);
255
256         if (result != USB_STOR_XFER_GOOD)
257                 return USB_STOR_TRANSPORT_ERROR;
258
259         return usbat_wait_not_busy(us, minutes);
260 }
261
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,
271                                int direction,
272                                unsigned char *content,
273                                unsigned short len,
274                                int use_sg,
275                                int minutes)
276 {
277         int result;
278         unsigned int pipe = (direction == SCSI_DATA_READ) ?
279                         us->recv_bulk_pipe : us->send_bulk_pipe;
280
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.
283
284         unsigned char *command = us->iobuf;
285         int i, j;
286         int cmdlen;
287         unsigned char *data = us->iobuf;
288         unsigned char *status = us->iobuf;
289
290         BUG_ON(num_registers > US_IOBUF_SIZE/2);
291
292         for (i=0; i<20; i++) {
293
294                 /*
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.
300                  * 
301                  * We're only going to try sending the data 10 times. After
302                  * that, we just return a failure.
303                  */
304
305                 if (i==0) {
306                         cmdlen = 16;
307                         command[0] = 0x40;
308                         command[1] = access | 0x07;
309                         command[2] = 0x07;
310                         command[3] = 0x17;
311                         command[4] = 0xFC;
312                         command[5] = 0xE7;
313                         command[6] = LSB_of(num_registers*2);
314                         command[7] = MSB_of(num_registers*2);
315                 } else
316                         cmdlen = 8;
317
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);
327
328                 result = usb_stor_ctrl_transfer(us,
329                         us->send_ctrl_pipe,
330                         0x80,
331                         0x40,
332                         0,
333                         0,
334                         command,
335                         cmdlen);
336
337                 if (result != USB_STOR_XFER_GOOD)
338                         return USB_STOR_TRANSPORT_ERROR;
339
340                 if (i==0) {
341
342                         for (j=0; j<num_registers; j++) {
343                                 data[j<<1] = registers[j];
344                                 data[1+(j<<1)] = data_out[j];
345                         }
346
347                         result = usb_stor_bulk_transfer_buf(us,
348                                         us->send_bulk_pipe,
349                                         data, num_registers*2, NULL);
350
351                         if (result != USB_STOR_XFER_GOOD)
352                                 return USB_STOR_TRANSPORT_ERROR;
353
354                 }
355
356
357                 //US_DEBUGP("Transfer %s %d bytes, sg buffers %d\n",
358                 //      direction == SCSI_DATA_WRITE ? "out" : "in",
359                 //      len, use_sg);
360
361                 result = usb_stor_bulk_transfer_sg(us,
362                         pipe, content, len, use_sg, NULL);
363
364                 /*
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.
370                  *
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
380                  * transferred.
381                  */
382
383                 if (result == USB_STOR_XFER_SHORT ||
384                                 result == USB_STOR_XFER_STALLED) {
385
386                         /*
387                          * If we're reading and we stalled, then clear
388                          * the bulk output pipe only the first time.
389                          */
390
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;
395                         }
396
397                         /*
398                          * Read status: is the device angry, or just busy?
399                          */
400
401                         result = usbat_read(us, USBAT_ATA, 
402                                 direction==SCSI_DATA_WRITE ? 0x17 : 0x0E, 
403                                 status);
404
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;
411
412                         US_DEBUGP("Redoing %s\n",
413                           direction==SCSI_DATA_WRITE ? "write" : "read");
414
415                 } else if (result != USB_STOR_XFER_GOOD)
416                         return USB_STOR_TRANSPORT_ERROR;
417                 else
418                         return usbat_wait_not_busy(us, minutes);
419
420         }
421
422         US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
423                 direction==SCSI_DATA_WRITE ? "Writing" : "Reading");
424
425         return USB_STOR_TRANSPORT_FAILED;
426 }
427
428 /*
429  * Write data to multiple registers at once. Not meant for large
430  * transfers of data!
431  */
432
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)
438 {
439         int result;
440         unsigned char *data = us->iobuf;
441         int i;
442         unsigned char *command = us->iobuf;
443
444         BUG_ON(num_registers > US_IOBUF_SIZE/2);
445
446         command[0] = 0x40;
447         command[1] = access | 0x07;
448         command[2] = 0;
449         command[3] = 0;
450         command[4] = 0;
451         command[5] = 0;
452         command[6] = LSB_of(num_registers*2);
453         command[7] = MSB_of(num_registers*2);
454
455         result = usb_stor_ctrl_transfer(us,
456                 us->send_ctrl_pipe,
457                 0x80,
458                 0x40,
459                 0,
460                 0,
461                 command,
462                 8);
463
464         if (result != USB_STOR_XFER_GOOD)
465                 return USB_STOR_TRANSPORT_ERROR;
466
467         for (i=0; i<num_registers; i++) {
468                 data[i<<1] = registers[i];
469                 data[1+(i<<1)] = data_out[i];
470         }
471
472         result = usb_stor_bulk_transfer_buf(us,
473                 us->send_bulk_pipe, data, num_registers*2, NULL);
474
475         if (result != USB_STOR_XFER_GOOD)
476                 return USB_STOR_TRANSPORT_ERROR;
477
478         return usbat_wait_not_busy(us, 0);
479 }
480
481 static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
482 {
483         int result;
484
485         result = usb_stor_ctrl_transfer(us,
486                 us->recv_ctrl_pipe,
487                 0x82,
488                 0xC0,
489                 0,
490                 0,
491                 data_flags,
492                 1);
493
494         return result;
495 }
496
497 static int usbat_write_user_io(struct us_data *us,
498                                unsigned char enable_flags,
499                                unsigned char data_flags)
500 {
501         int result;
502
503         result = usb_stor_ctrl_transfer(us,
504                 us->send_ctrl_pipe,
505                 0x82,
506                 0x40,
507                 short_pack(enable_flags, data_flags),
508                 0,
509                 NULL,
510                 0);
511
512         return result;
513 }
514
515 /*
516  * Squeeze a potentially huge (> 65535 byte) read10 command into
517  * a little ( <= 65535 byte) ATAPI pipe
518  */
519
520 static int usbat_handle_read10(struct us_data *us,
521                                unsigned char *registers,
522                                unsigned char *data,
523                                Scsi_Cmnd *srb)
524 {
525         int result = USB_STOR_TRANSPORT_GOOD;
526         unsigned char *buffer;
527         unsigned int len;
528         unsigned int sector;
529         unsigned int sg_segment = 0;
530         unsigned int sg_offset = 0;
531
532         US_DEBUGP("handle_read10: transfersize %d\n",
533                 srb->transfersize);
534
535         if (srb->request_bufflen < 0x10000) {
536
537                 result = usbat_rw_block_test(us, USBAT_ATA, 
538                         registers, data, 19,
539                         0x10, 0x17, 0xFD, 0x30,
540                         SCSI_DATA_READ,
541                         srb->request_buffer, 
542                         srb->request_bufflen, srb->use_sg, 1);
543
544                 return result;
545         }
546
547         /*
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).
553          */
554
555         if (data[7+0] == GPCMD_READ_CD) {
556                 len = short_pack(data[7+9], data[7+8]);
557                 len <<= 16;
558                 len |= data[7+7];
559                 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
560                 srb->transfersize = srb->request_bufflen/len;
561         }
562
563         if (!srb->transfersize)  {
564                 srb->transfersize = 2048; /* A guess */
565                 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
566                         srb->transfersize);
567         }
568
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.
572
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]);
580         sector <<= 16;
581         sector |= short_pack(data[7+5], data[7+4]);
582         transferred = 0;
583
584         sg_segment = 0; // for keeping track of where we are in
585         sg_offset = 0;  // the scatter/gather list
586
587         while (transferred != srb->request_bufflen) {
588
589                 if (len > srb->request_bufflen - transferred)
590                         len = srb->request_bufflen - transferred;
591
592                 data[3] = len&0xFF;       // (cylL) = expected length (L)
593                 data[4] = (len>>8)&0xFF;  // (cylH) = expected length (H)
594
595                 // Fix up the SCSI command sector and num sectors
596
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)
602                         data[7+6] = 0;
603                 data[7+7] = MSB_of(len / srb->transfersize); // SCSI command
604                 data[7+8] = LSB_of(len / srb->transfersize); // num sectors
605
606                 result = usbat_rw_block_test(us, USBAT_ATA, 
607                         registers, data, 19,
608                         0x10, 0x17, 0xFD, 0x30,
609                         SCSI_DATA_READ,
610                         buffer,
611                         len, 0, 1);
612
613                 if (result != USB_STOR_TRANSPORT_GOOD)
614                         break;
615
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);
619
620                 // Update the amount transferred and the sector number
621
622                 transferred += len;
623                 sector += len / srb->transfersize;
624
625         } // while transferred != srb->request_bufflen
626
627         kfree(buffer);
628         return result;
629 }
630
631 static int hp_8200e_select_and_test_registers(struct us_data *us)
632 {
633         int selector;
634         unsigned char *status = us->iobuf;
635
636         // try device = master, then device = slave.
637
638         for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
639
640                 if (usbat_write(us, USBAT_ATA, 0x16, selector) != 
641                                 USB_STOR_XFER_GOOD)
642                         return USB_STOR_TRANSPORT_ERROR;
643
644                 if (usbat_read(us, USBAT_ATA, 0x17, status) != 
645                                 USB_STOR_XFER_GOOD)
646                         return USB_STOR_TRANSPORT_ERROR;
647
648                 if (usbat_read(us, USBAT_ATA, 0x16, status) != 
649                                 USB_STOR_XFER_GOOD)
650                         return USB_STOR_TRANSPORT_ERROR;
651
652                 if (usbat_read(us, USBAT_ATA, 0x14, status) != 
653                                 USB_STOR_XFER_GOOD)
654                         return USB_STOR_TRANSPORT_ERROR;
655
656                 if (usbat_read(us, USBAT_ATA, 0x15, status) != 
657                                 USB_STOR_XFER_GOOD)
658                         return USB_STOR_TRANSPORT_ERROR;
659
660                 if (usbat_write(us, USBAT_ATA, 0x14, 0x55) != 
661                                 USB_STOR_XFER_GOOD)
662                         return USB_STOR_TRANSPORT_ERROR;
663
664                 if (usbat_write(us, USBAT_ATA, 0x15, 0xAA) != 
665                                 USB_STOR_XFER_GOOD)
666                         return USB_STOR_TRANSPORT_ERROR;
667
668                 if (usbat_read(us, USBAT_ATA, 0x14, status) != 
669                                 USB_STOR_XFER_GOOD)
670                         return USB_STOR_TRANSPORT_ERROR;
671
672                 if (usbat_read(us, USBAT_ATA, 0x15, status) != 
673                                 USB_STOR_XFER_GOOD)
674                         return USB_STOR_TRANSPORT_ERROR;
675         }
676
677         return USB_STOR_TRANSPORT_GOOD;
678 }
679
680 int init_8200e(struct us_data *us)
681 {
682         int result;
683         unsigned char *status = us->iobuf;
684
685         // Enable peripheral control signals
686
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;
691
692         US_DEBUGP("INIT 1\n");
693
694         msleep(2000);
695
696         if (usbat_read_user_io(us, status) !=
697                         USB_STOR_XFER_GOOD)
698                 return USB_STOR_TRANSPORT_ERROR;
699
700         US_DEBUGP("INIT 2\n");
701
702         if (usbat_read_user_io(us, status) !=
703                         USB_STOR_XFER_GOOD)
704                 return USB_STOR_TRANSPORT_ERROR;
705
706         US_DEBUGP("INIT 3\n");
707
708         // Reset peripheral, enable periph control signals
709         // (bring reset signal up)
710
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;
715
716         US_DEBUGP("INIT 4\n");
717
718         // Enable periph control signals
719         // (bring reset signal down)
720
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;
725
726         US_DEBUGP("INIT 5\n");
727
728         msleep(250);
729
730         // Write 0x80 to ISA port 0x3F
731
732         if (usbat_write(us, USBAT_ISA, 0x3F, 0x80) !=
733                         USB_STOR_XFER_GOOD)
734                 return USB_STOR_TRANSPORT_ERROR;
735
736         US_DEBUGP("INIT 6\n");
737
738         // Read ISA port 0x27
739
740         if (usbat_read(us, USBAT_ISA, 0x27, status) !=
741                         USB_STOR_XFER_GOOD)
742                 return USB_STOR_TRANSPORT_ERROR;
743
744         US_DEBUGP("INIT 7\n");
745
746         if (usbat_read_user_io(us, status) !=
747                         USB_STOR_XFER_GOOD)
748                 return USB_STOR_TRANSPORT_ERROR;
749
750         US_DEBUGP("INIT 8\n");
751
752         if ( (result = hp_8200e_select_and_test_registers(us)) !=
753                          USB_STOR_TRANSPORT_GOOD)
754                 return result;
755
756         US_DEBUGP("INIT 9\n");
757
758         if (usbat_read_user_io(us, status) !=
759                         USB_STOR_XFER_GOOD)
760                 return USB_STOR_TRANSPORT_ERROR;
761
762         US_DEBUGP("INIT 10\n");
763
764         // Enable periph control signals and card detect
765
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;
770
771         US_DEBUGP("INIT 11\n");
772
773         if (usbat_read_user_io(us, status) !=
774                         USB_STOR_XFER_GOOD)
775                 return USB_STOR_TRANSPORT_ERROR;
776
777         US_DEBUGP("INIT 12\n");
778
779         msleep(1400);
780
781         if (usbat_read_user_io(us, status) !=
782                         USB_STOR_XFER_GOOD)
783                 return USB_STOR_TRANSPORT_ERROR;
784
785         US_DEBUGP("INIT 13\n");
786
787         if ( (result = hp_8200e_select_and_test_registers(us)) !=
788                          USB_STOR_TRANSPORT_GOOD)
789                 return result;
790
791         US_DEBUGP("INIT 14\n");
792
793         if (usbat_set_shuttle_features(us, 
794                         0x83, 0x00, 0x88, 0x08, 0x15, 0x14) !=
795                          USB_STOR_XFER_GOOD)
796                 return USB_STOR_TRANSPORT_ERROR;
797
798         US_DEBUGP("INIT 15\n");
799
800         return USB_STOR_TRANSPORT_GOOD;
801 }
802
803 /*
804  * Transport for the HP 8200e
805  */
806 int hp8200e_transport(Scsi_Cmnd *srb, struct us_data *us)
807 {
808         int result;
809         unsigned char *status = us->iobuf;
810         unsigned char registers[32];
811         unsigned char data[32];
812         unsigned int len;
813         int i;
814         char string[64];
815
816         len = srb->request_bufflen;
817
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.
821          */
822
823         registers[0] = 0x11;
824         registers[1] = 0x12;
825         registers[2] = 0x13;
826         registers[3] = 0x14;
827         registers[4] = 0x15;
828         registers[5] = 0x16;
829         registers[6] = 0x17;
830         data[0] = 0x00;
831         data[1] = 0x00;
832         data[2] = 0x00;
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
837
838         for (i=7; i<19; i++) {
839                 registers[i] = 0x10;
840                 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
841         }
842
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)
848                 transferred = 0;
849
850         if (srb->sc_data_direction == SCSI_DATA_WRITE) {
851
852                 result = usbat_rw_block_test(us, USBAT_ATA, 
853                         registers, data, 19,
854                         0x10, 0x17, 0xFD, 0x30,
855                         SCSI_DATA_WRITE,
856                         srb->request_buffer, 
857                         len, srb->use_sg, 10);
858
859                 if (result == USB_STOR_TRANSPORT_GOOD) {
860                         transferred += len;
861                         US_DEBUGP("Wrote %08X bytes\n", transferred);
862                 }
863
864                 return result;
865
866         } else if (srb->cmnd[0] == READ_10 ||
867                    srb->cmnd[0] == GPCMD_READ_CD) {
868
869                 return usbat_handle_read10(us, registers, data, srb);
870
871         }
872
873         if (len > 0xFFFF) {
874                 US_DEBUGP("Error: len = %08X... what do I do now?\n",
875                         len);
876                 return USB_STOR_TRANSPORT_ERROR;
877         }
878
879         if ( (result = usbat_multiple_write(us, 
880                         USBAT_ATA,
881                         registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
882                 return result;
883         }
884
885         // Write the 12-byte command header.
886
887         // If the command is BLANK then set the timer for 75 minutes.
888         // Otherwise set it for 10 minutes.
889
890         // NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
891         // AT SPEED 4 IS UNRELIABLE!!!
892
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) {
897                 return result;
898         }
899
900         // If there is response data to be read in 
901         // then do it here.
902
903         if (len != 0 && (srb->sc_data_direction == SCSI_DATA_READ)) {
904
905                 // How many bytes to read in? Check cylL register
906
907                 if (usbat_read(us, USBAT_ATA, 0x14, status) != 
908                         USB_STOR_XFER_GOOD) {
909                         return USB_STOR_TRANSPORT_ERROR;
910                 }
911
912                 if (len > 0xFF) { // need to read cylH also
913                         len = *status;
914                         if (usbat_read(us, USBAT_ATA, 0x15, status) !=
915                                     USB_STOR_XFER_GOOD) {
916                                 return USB_STOR_TRANSPORT_ERROR;
917                         }
918                         len += ((unsigned int) *status)<<8;
919                 }
920                 else
921                         len = *status;
922
923
924                 result = usbat_read_block(us, USBAT_ATA, 0x10, 
925                         srb->request_buffer, len, srb->use_sg);
926
927                 /* Debug-print the first 32 bytes of the transfer */
928
929                 if (!srb->use_sg) {
930                         string[0] = 0;
931                         for (i=0; i<len && i<32; i++) {
932                                 sprintf(string+strlen(string), "%02X ",
933                                   ((unsigned char *)srb->request_buffer)[i]);
934                                 if ((i%16)==15) {
935                                         US_DEBUGP("%s\n", string);
936                                         string[0] = 0;
937                                 }
938                         }
939                         if (string[0]!=0)
940                                 US_DEBUGP("%s\n", string);
941                 }
942         }
943
944         return result;
945 }
946
947