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