ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 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 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 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 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 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                         wait_ms(10); // 5 seconds
206                 else if (i<700)
207                         wait_ms(50); // 10 seconds
208                 else if (i<1200)
209                         wait_ms(100); // 50 seconds
210                 else
211                         wait_ms(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 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,
225              int minutes) {
226
227         int result;
228         unsigned char *command = us->iobuf;
229
230         if (!len)
231                 return USB_STOR_TRANSPORT_GOOD;
232
233         command[0] = 0x40;
234         command[1] = access | 0x03;
235         command[2] = reg;
236         command[3] = 0;
237         command[4] = 0;
238         command[5] = 0;
239         command[6] = LSB_of(len);
240         command[7] = MSB_of(len);
241
242         result = usb_stor_ctrl_transfer(us,
243                 us->send_ctrl_pipe,
244                 0x80,
245                 0x40,
246                 0,
247                 0,
248                 command,
249                 8);
250
251         if (result != USB_STOR_XFER_GOOD)
252                 return USB_STOR_TRANSPORT_ERROR;
253
254         result = usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe,
255                         content, len, use_sg, NULL);
256
257         if (result != USB_STOR_XFER_GOOD)
258                 return USB_STOR_TRANSPORT_ERROR;
259
260         return usbat_wait_not_busy(us, minutes);
261 }
262
263 int usbat_rw_block_test(struct us_data *us,
264              unsigned char access,
265              unsigned char *registers,
266              unsigned char *data_out,
267              unsigned short num_registers,
268              unsigned char data_reg, 
269              unsigned char status_reg, 
270              unsigned char timeout, 
271              unsigned char qualifier, 
272              int direction,
273              unsigned char *content,
274              unsigned short len,
275              int use_sg,
276              int minutes) {
277
278         int result;
279         unsigned int pipe = (direction == SCSI_DATA_READ) ?
280                         us->recv_bulk_pipe : us->send_bulk_pipe;
281
282         // Not really sure the 0x07, 0x17, 0xfc, 0xe7 is necessary here,
283         // but that's what came out of the trace every single time.
284
285         unsigned char *command = us->iobuf;
286         int i, j;
287         int cmdlen;
288         unsigned char *data = us->iobuf;
289         unsigned char *status = us->iobuf;
290
291         BUG_ON(num_registers > US_IOBUF_SIZE/2);
292
293         for (i=0; i<20; i++) {
294
295                 /*
296                  * The first time we send the full command, which consists
297                  * of downloading the SCSI command followed by downloading
298                  * the data via a write-and-test.  Any other time we only
299                  * send the command to download the data -- the SCSI command
300                  * is still 'active' in some sense in the device.
301                  * 
302                  * We're only going to try sending the data 10 times. After
303                  * that, we just return a failure.
304                  */
305
306                 if (i==0) {
307                         cmdlen = 16;
308                         command[0] = 0x40;
309                         command[1] = access | 0x07;
310                         command[2] = 0x07;
311                         command[3] = 0x17;
312                         command[4] = 0xFC;
313                         command[5] = 0xE7;
314                         command[6] = LSB_of(num_registers*2);
315                         command[7] = MSB_of(num_registers*2);
316                 } else
317                         cmdlen = 8;
318
319                 command[cmdlen-8] = (direction==SCSI_DATA_WRITE ? 0x40 : 0xC0);
320                 command[cmdlen-7] = access |
321                                 (direction==SCSI_DATA_WRITE ? 0x05 : 0x04);
322                 command[cmdlen-6] = data_reg;
323                 command[cmdlen-5] = status_reg;
324                 command[cmdlen-4] = timeout;
325                 command[cmdlen-3] = qualifier;
326                 command[cmdlen-2] = LSB_of(len);
327                 command[cmdlen-1] = MSB_of(len);
328
329                 result = usb_stor_ctrl_transfer(us,
330                         us->send_ctrl_pipe,
331                         0x80,
332                         0x40,
333                         0,
334                         0,
335                         command,
336                         cmdlen);
337
338                 if (result != USB_STOR_XFER_GOOD)
339                         return USB_STOR_TRANSPORT_ERROR;
340
341                 if (i==0) {
342
343                         for (j=0; j<num_registers; j++) {
344                                 data[j<<1] = registers[j];
345                                 data[1+(j<<1)] = data_out[j];
346                         }
347
348                         result = usb_stor_bulk_transfer_buf(us,
349                                         us->send_bulk_pipe,
350                                         data, num_registers*2, NULL);
351
352                         if (result != USB_STOR_XFER_GOOD)
353                                 return USB_STOR_TRANSPORT_ERROR;
354
355                 }
356
357
358                 //US_DEBUGP("Transfer %s %d bytes, sg buffers %d\n",
359                 //      direction == SCSI_DATA_WRITE ? "out" : "in",
360                 //      len, use_sg);
361
362                 result = usb_stor_bulk_transfer_sg(us,
363                         pipe, content, len, use_sg, NULL);
364
365                 /*
366                  * If we get a stall on the bulk download, we'll retry
367                  * the bulk download -- but not the SCSI command because
368                  * in some sense the SCSI command is still 'active' and
369                  * waiting for the data. Don't ask me why this should be;
370                  * I'm only following what the Windoze driver did.
371                  *
372                  * Note that a stall for the test-and-read/write command means
373                  * that the test failed. In this case we're testing to make
374                  * sure that the device is error-free
375                  * (i.e. bit 0 -- CHK -- of status is 0). The most likely
376                  * hypothesis is that the USBAT chip somehow knows what
377                  * the device will accept, but doesn't give the device any
378                  * data until all data is received. Thus, the device would
379                  * still be waiting for the first byte of data if a stall
380                  * occurs, even if the stall implies that some data was
381                  * transferred.
382                  */
383
384                 if (result == USB_STOR_XFER_SHORT ||
385                                 result == USB_STOR_XFER_STALLED) {
386
387                         /*
388                          * If we're reading and we stalled, then clear
389                          * the bulk output pipe only the first time.
390                          */
391
392                         if (direction==SCSI_DATA_READ && i==0) {
393                                 if (usb_stor_clear_halt(us,
394                                                 us->send_bulk_pipe) < 0)
395                                         return USB_STOR_TRANSPORT_ERROR;
396                         }
397
398                         /*
399                          * Read status: is the device angry, or just busy?
400                          */
401
402                         result = usbat_read(us, USBAT_ATA, 
403                                 direction==SCSI_DATA_WRITE ? 0x17 : 0x0E, 
404                                 status);
405
406                         if (result!=USB_STOR_XFER_GOOD)
407                                 return USB_STOR_TRANSPORT_ERROR;
408                         if (*status & 0x01) // check condition
409                                 return USB_STOR_TRANSPORT_FAILED;
410                         if (*status & 0x20) // device fault
411                                 return USB_STOR_TRANSPORT_FAILED;
412
413                         US_DEBUGP("Redoing %s\n",
414                           direction==SCSI_DATA_WRITE ? "write" : "read");
415
416                 } else if (result != USB_STOR_XFER_GOOD)
417                         return USB_STOR_TRANSPORT_ERROR;
418                 else
419                         return usbat_wait_not_busy(us, minutes);
420
421         }
422
423         US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
424                 direction==SCSI_DATA_WRITE ? "Writing" : "Reading");
425
426         return USB_STOR_TRANSPORT_FAILED;
427 }
428
429 /*
430  * Write data to multiple registers at once. Not meant for large
431  * transfers of data!
432  */
433
434 int usbat_multiple_write(struct us_data *us, 
435                         unsigned char access,
436                         unsigned char *registers,
437                         unsigned char *data_out,
438                         unsigned short num_registers) {
439
440         int result;
441         unsigned char *data = us->iobuf;
442         int i;
443         unsigned char *command = us->iobuf;
444
445         BUG_ON(num_registers > US_IOBUF_SIZE/2);
446
447         command[0] = 0x40;
448         command[1] = access | 0x07;
449         command[2] = 0;
450         command[3] = 0;
451         command[4] = 0;
452         command[5] = 0;
453         command[6] = LSB_of(num_registers*2);
454         command[7] = MSB_of(num_registers*2);
455
456         result = usb_stor_ctrl_transfer(us,
457                 us->send_ctrl_pipe,
458                 0x80,
459                 0x40,
460                 0,
461                 0,
462                 command,
463                 8);
464
465         if (result != USB_STOR_XFER_GOOD)
466                 return USB_STOR_TRANSPORT_ERROR;
467
468         for (i=0; i<num_registers; i++) {
469                 data[i<<1] = registers[i];
470                 data[1+(i<<1)] = data_out[i];
471         }
472
473         result = usb_stor_bulk_transfer_buf(us,
474                 us->send_bulk_pipe, data, num_registers*2, NULL);
475
476         if (result != USB_STOR_XFER_GOOD)
477                 return USB_STOR_TRANSPORT_ERROR;
478
479         return usbat_wait_not_busy(us, 0);
480 }
481
482 int usbat_read_user_io(struct us_data *us,
483                 unsigned char *data_flags) {
484
485         int result;
486
487         result = usb_stor_ctrl_transfer(us,
488                 us->recv_ctrl_pipe,
489                 0x82,
490                 0xC0,
491                 0,
492                 0,
493                 data_flags,
494                 1);
495
496         return result;
497 }
498
499 int usbat_write_user_io(struct us_data *us,
500                 unsigned char enable_flags,
501                 unsigned char data_flags) {
502
503         int result;
504
505         result = usb_stor_ctrl_transfer(us,
506                 us->send_ctrl_pipe,
507                 0x82,
508                 0x40,
509                 short_pack(enable_flags, data_flags),
510                 0,
511                 NULL,
512                 0);
513
514         return result;
515 }
516
517 /*
518  * Squeeze a potentially huge (> 65535 byte) read10 command into
519  * a little ( <= 65535 byte) ATAPI pipe
520  */
521
522 int usbat_handle_read10(struct us_data *us,
523                 unsigned char *registers,
524                 unsigned char *data,
525                 Scsi_Cmnd *srb) {
526
527         int result = USB_STOR_TRANSPORT_GOOD;
528         unsigned char *buffer;
529         unsigned int len;
530         unsigned int sector;
531         unsigned int sg_segment = 0;
532         unsigned int sg_offset = 0;
533
534         US_DEBUGP("handle_read10: transfersize %d\n",
535                 srb->transfersize);
536
537         if (srb->request_bufflen < 0x10000) {
538
539                 result = usbat_rw_block_test(us, USBAT_ATA, 
540                         registers, data, 19,
541                         0x10, 0x17, 0xFD, 0x30,
542                         SCSI_DATA_READ,
543                         srb->request_buffer, 
544                         srb->request_bufflen, srb->use_sg, 1);
545
546                 return result;
547         }
548
549         /*
550          * Since we're requesting more data than we can handle in
551          * a single read command (max is 64k-1), we will perform
552          * multiple reads, but each read must be in multiples of
553          * a sector.  Luckily the sector size is in srb->transfersize
554          * (see linux/drivers/scsi/sr.c).
555          */
556
557         if (data[7+0] == GPCMD_READ_CD) {
558                 len = short_pack(data[7+9], data[7+8]);
559                 len <<= 16;
560                 len |= data[7+7];
561                 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
562                 srb->transfersize = srb->request_bufflen/len;
563         }
564
565         if (!srb->transfersize)  {
566                 srb->transfersize = 2048; /* A guess */
567                 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
568                         srb->transfersize);
569         }
570
571         // Since we only read in one block at a time, we have to create
572         // a bounce buffer and move the data a piece at a time between the
573         // bounce buffer and the actual transfer buffer.
574
575         len = (65535/srb->transfersize) * srb->transfersize;
576         US_DEBUGP("Max read is %d bytes\n", len);
577         len = min(len, srb->request_bufflen);
578         buffer = kmalloc(len, GFP_NOIO);
579         if (buffer == NULL) // bloody hell!
580                 return USB_STOR_TRANSPORT_FAILED;
581         sector = short_pack(data[7+3], data[7+2]);
582         sector <<= 16;
583         sector |= short_pack(data[7+5], data[7+4]);
584         transferred = 0;
585
586         sg_segment = 0; // for keeping track of where we are in
587         sg_offset = 0;  // the scatter/gather list
588
589         while (transferred != srb->request_bufflen) {
590
591                 if (len > srb->request_bufflen - transferred)
592                         len = srb->request_bufflen - transferred;
593
594                 data[3] = len&0xFF;       // (cylL) = expected length (L)
595                 data[4] = (len>>8)&0xFF;  // (cylH) = expected length (H)
596
597                 // Fix up the SCSI command sector and num sectors
598
599                 data[7+2] = MSB_of(sector>>16); // SCSI command sector
600                 data[7+3] = LSB_of(sector>>16);
601                 data[7+4] = MSB_of(sector&0xFFFF);
602                 data[7+5] = LSB_of(sector&0xFFFF);
603                 if (data[7+0] == GPCMD_READ_CD)
604                         data[7+6] = 0;
605                 data[7+7] = MSB_of(len / srb->transfersize); // SCSI command
606                 data[7+8] = LSB_of(len / srb->transfersize); // num sectors
607
608                 result = usbat_rw_block_test(us, USBAT_ATA, 
609                         registers, data, 19,
610                         0x10, 0x17, 0xFD, 0x30,
611                         SCSI_DATA_READ,
612                         buffer,
613                         len, 0, 1);
614
615                 if (result != USB_STOR_TRANSPORT_GOOD)
616                         break;
617
618                 // Store the data in the transfer buffer
619                 usb_stor_access_xfer_buf(buffer, len, srb,
620                                  &sg_segment, &sg_offset, TO_XFER_BUF);
621
622                 // Update the amount transferred and the sector number
623
624                 transferred += len;
625                 sector += len / srb->transfersize;
626
627         } // while transferred != srb->request_bufflen
628
629         kfree(buffer);
630         return result;
631 }
632
633 static int hp_8200e_select_and_test_registers(struct us_data *us) {
634
635         int selector;
636         unsigned char *status = us->iobuf;
637
638         // try device = master, then device = slave.
639
640         for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
641
642                 if (usbat_write(us, USBAT_ATA, 0x16, selector) != 
643                                 USB_STOR_XFER_GOOD)
644                         return USB_STOR_TRANSPORT_ERROR;
645
646                 if (usbat_read(us, USBAT_ATA, 0x17, status) != 
647                                 USB_STOR_XFER_GOOD)
648                         return USB_STOR_TRANSPORT_ERROR;
649
650                 if (usbat_read(us, USBAT_ATA, 0x16, status) != 
651                                 USB_STOR_XFER_GOOD)
652                         return USB_STOR_TRANSPORT_ERROR;
653
654                 if (usbat_read(us, USBAT_ATA, 0x14, status) != 
655                                 USB_STOR_XFER_GOOD)
656                         return USB_STOR_TRANSPORT_ERROR;
657
658                 if (usbat_read(us, USBAT_ATA, 0x15, status) != 
659                                 USB_STOR_XFER_GOOD)
660                         return USB_STOR_TRANSPORT_ERROR;
661
662                 if (usbat_write(us, USBAT_ATA, 0x14, 0x55) != 
663                                 USB_STOR_XFER_GOOD)
664                         return USB_STOR_TRANSPORT_ERROR;
665
666                 if (usbat_write(us, USBAT_ATA, 0x15, 0xAA) != 
667                                 USB_STOR_XFER_GOOD)
668                         return USB_STOR_TRANSPORT_ERROR;
669
670                 if (usbat_read(us, USBAT_ATA, 0x14, status) != 
671                                 USB_STOR_XFER_GOOD)
672                         return USB_STOR_TRANSPORT_ERROR;
673
674                 if (usbat_read(us, USBAT_ATA, 0x15, status) != 
675                                 USB_STOR_XFER_GOOD)
676                         return USB_STOR_TRANSPORT_ERROR;
677         }
678
679         return USB_STOR_TRANSPORT_GOOD;
680 }
681
682 int init_8200e(struct us_data *us) {
683
684         int result;
685         unsigned char *status = us->iobuf;
686
687         // Enable peripheral control signals
688
689         if (usbat_write_user_io(us,
690           USBAT_UIO_OE1 | USBAT_UIO_OE0,
691           USBAT_UIO_EPAD | USBAT_UIO_1) != USB_STOR_XFER_GOOD)
692                 return USB_STOR_TRANSPORT_ERROR;
693
694         US_DEBUGP("INIT 1\n");
695
696         wait_ms(2000);
697
698         if (usbat_read_user_io(us, status) !=
699                         USB_STOR_XFER_GOOD)
700                 return USB_STOR_TRANSPORT_ERROR;
701
702         US_DEBUGP("INIT 2\n");
703
704         if (usbat_read_user_io(us, status) !=
705                         USB_STOR_XFER_GOOD)
706                 return USB_STOR_TRANSPORT_ERROR;
707
708         US_DEBUGP("INIT 3\n");
709
710         // Reset peripheral, enable periph control signals
711         // (bring reset signal up)
712
713         if (usbat_write_user_io(us,
714           USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
715           USBAT_UIO_EPAD | USBAT_UIO_1) != USB_STOR_XFER_GOOD)
716                 return USB_STOR_TRANSPORT_ERROR;
717
718         US_DEBUGP("INIT 4\n");
719
720         // Enable periph control signals
721         // (bring reset signal down)
722
723         if (usbat_write_user_io(us,
724           USBAT_UIO_OE1 | USBAT_UIO_OE0,
725           USBAT_UIO_EPAD | USBAT_UIO_1) != USB_STOR_XFER_GOOD)
726                 return USB_STOR_TRANSPORT_ERROR;
727
728         US_DEBUGP("INIT 5\n");
729
730         wait_ms(250);
731
732         // Write 0x80 to ISA port 0x3F
733
734         if (usbat_write(us, USBAT_ISA, 0x3F, 0x80) !=
735                         USB_STOR_XFER_GOOD)
736                 return USB_STOR_TRANSPORT_ERROR;
737
738         US_DEBUGP("INIT 6\n");
739
740         // Read ISA port 0x27
741
742         if (usbat_read(us, USBAT_ISA, 0x27, status) !=
743                         USB_STOR_XFER_GOOD)
744                 return USB_STOR_TRANSPORT_ERROR;
745
746         US_DEBUGP("INIT 7\n");
747
748         if (usbat_read_user_io(us, status) !=
749                         USB_STOR_XFER_GOOD)
750                 return USB_STOR_TRANSPORT_ERROR;
751
752         US_DEBUGP("INIT 8\n");
753
754         if ( (result = hp_8200e_select_and_test_registers(us)) !=
755                          USB_STOR_TRANSPORT_GOOD)
756                 return result;
757
758         US_DEBUGP("INIT 9\n");
759
760         if (usbat_read_user_io(us, status) !=
761                         USB_STOR_XFER_GOOD)
762                 return USB_STOR_TRANSPORT_ERROR;
763
764         US_DEBUGP("INIT 10\n");
765
766         // Enable periph control signals and card detect
767
768         if (usbat_write_user_io(us,
769           USBAT_UIO_ACKD |USBAT_UIO_OE1 | USBAT_UIO_OE0,
770           USBAT_UIO_EPAD | USBAT_UIO_1) != USB_STOR_XFER_GOOD)
771                 return USB_STOR_TRANSPORT_ERROR;
772
773         US_DEBUGP("INIT 11\n");
774
775         if (usbat_read_user_io(us, status) !=
776                         USB_STOR_XFER_GOOD)
777                 return USB_STOR_TRANSPORT_ERROR;
778
779         US_DEBUGP("INIT 12\n");
780
781         wait_ms(1400);
782
783         if (usbat_read_user_io(us, status) !=
784                         USB_STOR_XFER_GOOD)
785                 return USB_STOR_TRANSPORT_ERROR;
786
787         US_DEBUGP("INIT 13\n");
788
789         if ( (result = hp_8200e_select_and_test_registers(us)) !=
790                          USB_STOR_TRANSPORT_GOOD)
791                 return result;
792
793         US_DEBUGP("INIT 14\n");
794
795         if (usbat_set_shuttle_features(us, 
796                         0x83, 0x00, 0x88, 0x08, 0x15, 0x14) !=
797                          USB_STOR_XFER_GOOD)
798                 return USB_STOR_TRANSPORT_ERROR;
799
800         US_DEBUGP("INIT 15\n");
801
802         return USB_STOR_TRANSPORT_GOOD;
803 }
804
805 /*
806  * Transport for the HP 8200e
807  */
808 int hp8200e_transport(Scsi_Cmnd *srb, struct us_data *us)
809 {
810         int result;
811         unsigned char *status = us->iobuf;
812         unsigned char registers[32];
813         unsigned char data[32];
814         unsigned int len;
815         int i;
816         char string[64];
817
818         len = srb->request_bufflen;
819
820         /* Send A0 (ATA PACKET COMMAND).
821            Note: I guess we're never going to get any of the ATA
822            commands... just ATA Packet Commands.
823          */
824
825         registers[0] = 0x11;
826         registers[1] = 0x12;
827         registers[2] = 0x13;
828         registers[3] = 0x14;
829         registers[4] = 0x15;
830         registers[5] = 0x16;
831         registers[6] = 0x17;
832         data[0] = 0x00;
833         data[1] = 0x00;
834         data[2] = 0x00;
835         data[3] = len&0xFF;             // (cylL) = expected length (L)
836         data[4] = (len>>8)&0xFF;        // (cylH) = expected length (H)
837         data[5] = 0xB0;                 // (device sel) = slave
838         data[6] = 0xA0;                 // (command) = ATA PACKET COMMAND
839
840         for (i=7; i<19; i++) {
841                 registers[i] = 0x10;
842                 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
843         }
844
845         result = usbat_read(us, USBAT_ATA, 0x17, status);
846         US_DEBUGP("Status = %02X\n", *status);
847         if (result != USB_STOR_XFER_GOOD)
848                 return USB_STOR_TRANSPORT_ERROR;
849         if (srb->cmnd[0] == TEST_UNIT_READY)
850                 transferred = 0;
851
852         if (srb->sc_data_direction == SCSI_DATA_WRITE) {
853
854                 result = usbat_rw_block_test(us, USBAT_ATA, 
855                         registers, data, 19,
856                         0x10, 0x17, 0xFD, 0x30,
857                         SCSI_DATA_WRITE,
858                         srb->request_buffer, 
859                         len, srb->use_sg, 10);
860
861                 if (result == USB_STOR_TRANSPORT_GOOD) {
862                         transferred += len;
863                         US_DEBUGP("Wrote %08X bytes\n", transferred);
864                 }
865
866                 return result;
867
868         } else if (srb->cmnd[0] == READ_10 ||
869                    srb->cmnd[0] == GPCMD_READ_CD) {
870
871                 return usbat_handle_read10(us, registers, data, srb);
872
873         }
874
875         if (len > 0xFFFF) {
876                 US_DEBUGP("Error: len = %08X... what do I do now?\n",
877                         len);
878                 return USB_STOR_TRANSPORT_ERROR;
879         }
880
881         if ( (result = usbat_multiple_write(us, 
882                         USBAT_ATA,
883                         registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
884                 return result;
885         }
886
887         // Write the 12-byte command header.
888
889         // If the command is BLANK then set the timer for 75 minutes.
890         // Otherwise set it for 10 minutes.
891
892         // NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
893         // AT SPEED 4 IS UNRELIABLE!!!
894
895         if ( (result = usbat_write_block(us, 
896                         USBAT_ATA, 0x10, srb->cmnd, 12, 0,
897                         srb->cmnd[0]==GPCMD_BLANK ? 75 : 10)) !=
898                                 USB_STOR_TRANSPORT_GOOD) {
899                 return result;
900         }
901
902         // If there is response data to be read in 
903         // then do it here.
904
905         if (len != 0 && (srb->sc_data_direction == SCSI_DATA_READ)) {
906
907                 // How many bytes to read in? Check cylL register
908
909                 if (usbat_read(us, USBAT_ATA, 0x14, status) != 
910                         USB_STOR_XFER_GOOD) {
911                         return USB_STOR_TRANSPORT_ERROR;
912                 }
913
914                 if (len > 0xFF) { // need to read cylH also
915                         len = *status;
916                         if (usbat_read(us, USBAT_ATA, 0x15, status) !=
917                                     USB_STOR_XFER_GOOD) {
918                                 return USB_STOR_TRANSPORT_ERROR;
919                         }
920                         len += ((unsigned int) *status)<<8;
921                 }
922                 else
923                         len = *status;
924
925
926                 result = usbat_read_block(us, USBAT_ATA, 0x10, 
927                         srb->request_buffer, len, srb->use_sg);
928
929                 /* Debug-print the first 32 bytes of the transfer */
930
931                 if (!srb->use_sg) {
932                         string[0] = 0;
933                         for (i=0; i<len && i<32; i++) {
934                                 sprintf(string+strlen(string), "%02X ",
935                                   ((unsigned char *)srb->request_buffer)[i]);
936                                 if ((i%16)==15) {
937                                         US_DEBUGP("%s\n", string);
938                                         string[0] = 0;
939                                 }
940                         }
941                         if (string[0]!=0)
942                                 US_DEBUGP("%s\n", string);
943                 }
944         }
945
946         return result;
947 }
948
949