Merge branch 'linux-next' of git://git.infradead.org/~dedekind/ubifs-2.6
[linux-2.6] / drivers / usb / storage / shuttle_usbat.c
1 /* Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable
2  *
3  * Current development and maintenance by:
4  *   (c) 2000, 2001 Robert Baruch (autophile@starband.net)
5  *   (c) 2004, 2005 Daniel Drake <dsd@gentoo.org>
6  *
7  * Developed with the assistance of:
8  *   (c) 2002 Alan Stern <stern@rowland.org>
9  *
10  * Flash support based on earlier work by:
11  *   (c) 2002 Thomas Kreiling <usbdev@sm04.de>
12  *
13  * Many originally ATAPI devices were slightly modified to meet the USB
14  * market by using some kind of translation from ATAPI to USB on the host,
15  * and the peripheral would translate from USB back to ATAPI.
16  *
17  * SCM Microsystems (www.scmmicro.com) makes a device, sold to OEM's only, 
18  * which does the USB-to-ATAPI conversion.  By obtaining the data sheet on
19  * their device under nondisclosure agreement, I have been able to write
20  * this driver for Linux.
21  *
22  * The chip used in the device can also be used for EPP and ISA translation
23  * as well. This driver is only guaranteed to work with the ATAPI
24  * translation.
25  *
26  * See the Kconfig help text for a list of devices known to be supported by
27  * this driver.
28  *
29  * This program is free software; you can redistribute it and/or modify it
30  * under the terms of the GNU General Public License as published by the
31  * Free Software Foundation; either version 2, or (at your option) any
32  * later version.
33  *
34  * This program is distributed in the hope that it will be useful, but
35  * WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
37  * General Public License for more details.
38  *
39  * You should have received a copy of the GNU General Public License along
40  * with this program; if not, write to the Free Software Foundation, Inc.,
41  * 675 Mass Ave, Cambridge, MA 02139, USA.
42  */
43
44 #include <linux/errno.h>
45 #include <linux/slab.h>
46 #include <linux/cdrom.h>
47
48 #include <scsi/scsi.h>
49 #include <scsi/scsi_cmnd.h>
50
51 #include "usb.h"
52 #include "transport.h"
53 #include "protocol.h"
54 #include "debug.h"
55 #include "shuttle_usbat.h"
56
57 #define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
58 #define LSB_of(s) ((s)&0xFF)
59 #define MSB_of(s) ((s)>>8)
60
61 static int transferred = 0;
62
63 static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us);
64 static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us);
65
66 /*
67  * Convenience function to produce an ATA read/write sectors command
68  * Use cmd=0x20 for read, cmd=0x30 for write
69  */
70 static void usbat_pack_ata_sector_cmd(unsigned char *buf,
71                                         unsigned char thistime,
72                                         u32 sector, unsigned char cmd)
73 {
74         buf[0] = 0;
75         buf[1] = thistime;
76         buf[2] = sector & 0xFF;
77         buf[3] = (sector >>  8) & 0xFF;
78         buf[4] = (sector >> 16) & 0xFF;
79         buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
80         buf[6] = cmd;
81 }
82
83 /*
84  * Convenience function to get the device type (flash or hp8200)
85  */
86 static int usbat_get_device_type(struct us_data *us)
87 {
88         return ((struct usbat_info*)us->extra)->devicetype;
89 }
90
91 /*
92  * Read a register from the device
93  */
94 static int usbat_read(struct us_data *us,
95                       unsigned char access,
96                       unsigned char reg,
97                       unsigned char *content)
98 {
99         return usb_stor_ctrl_transfer(us,
100                 us->recv_ctrl_pipe,
101                 access | USBAT_CMD_READ_REG,
102                 0xC0,
103                 (u16)reg,
104                 0,
105                 content,
106                 1);
107 }
108
109 /*
110  * Write to a register on the device
111  */
112 static int usbat_write(struct us_data *us,
113                        unsigned char access,
114                        unsigned char reg,
115                        unsigned char content)
116 {
117         return usb_stor_ctrl_transfer(us,
118                 us->send_ctrl_pipe,
119                 access | USBAT_CMD_WRITE_REG,
120                 0x40,
121                 short_pack(reg, content),
122                 0,
123                 NULL,
124                 0);
125 }
126
127 /*
128  * Convenience function to perform a bulk read
129  */
130 static int usbat_bulk_read(struct us_data *us,
131                            void* buf,
132                            unsigned int len,
133                            int use_sg)
134 {
135         if (len == 0)
136                 return USB_STOR_XFER_GOOD;
137
138         US_DEBUGP("usbat_bulk_read: len = %d\n", len);
139         return usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe, buf, len, use_sg, NULL);
140 }
141
142 /*
143  * Convenience function to perform a bulk write
144  */
145 static int usbat_bulk_write(struct us_data *us,
146                             void* buf,
147                             unsigned int len,
148                             int use_sg)
149 {
150         if (len == 0)
151                 return USB_STOR_XFER_GOOD;
152
153         US_DEBUGP("usbat_bulk_write:  len = %d\n", len);
154         return usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe, buf, len, use_sg, NULL);
155 }
156
157 /*
158  * Some USBAT-specific commands can only be executed over a command transport
159  * This transport allows one (len=8) or two (len=16) vendor-specific commands
160  * to be executed.
161  */
162 static int usbat_execute_command(struct us_data *us,
163                                                                  unsigned char *commands,
164                                                                  unsigned int len)
165 {
166         return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
167                                                                   USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
168                                                                   commands, len);
169 }
170
171 /*
172  * Read the status register
173  */
174 static int usbat_get_status(struct us_data *us, unsigned char *status)
175 {
176         int rc;
177         rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
178
179         US_DEBUGP("usbat_get_status: 0x%02X\n", (unsigned short) (*status));
180         return rc;
181 }
182
183 /*
184  * Check the device status
185  */
186 static int usbat_check_status(struct us_data *us)
187 {
188         unsigned char *reply = us->iobuf;
189         int rc;
190
191         rc = usbat_get_status(us, reply);
192         if (rc != USB_STOR_XFER_GOOD)
193                 return USB_STOR_TRANSPORT_FAILED;
194
195         /* error/check condition (0x51 is ok) */
196         if (*reply & 0x01 && *reply != 0x51)
197                 return USB_STOR_TRANSPORT_FAILED;
198
199         /* device fault */
200         if (*reply & 0x20)
201                 return USB_STOR_TRANSPORT_FAILED;
202
203         return USB_STOR_TRANSPORT_GOOD;
204 }
205
206 /*
207  * Stores critical information in internal registers in prepartion for the execution
208  * of a conditional usbat_read_blocks or usbat_write_blocks call.
209  */
210 static int usbat_set_shuttle_features(struct us_data *us,
211                                       unsigned char external_trigger,
212                                       unsigned char epp_control,
213                                       unsigned char mask_byte,
214                                       unsigned char test_pattern,
215                                       unsigned char subcountH,
216                                       unsigned char subcountL)
217 {
218         unsigned char *command = us->iobuf;
219
220         command[0] = 0x40;
221         command[1] = USBAT_CMD_SET_FEAT;
222
223         /*
224          * The only bit relevant to ATA access is bit 6
225          * which defines 8 bit data access (set) or 16 bit (unset)
226          */
227         command[2] = epp_control;
228
229         /*
230          * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
231          * ET1 and ET2 define an external event to be checked for on event of a
232          * _read_blocks or _write_blocks operation. The read/write will not take
233          * place unless the defined trigger signal is active.
234          */
235         command[3] = external_trigger;
236
237         /*
238          * The resultant byte of the mask operation (see mask_byte) is compared for
239          * equivalence with this test pattern. If equal, the read/write will take
240          * place.
241          */
242         command[4] = test_pattern;
243
244         /*
245          * This value is logically ANDed with the status register field specified
246          * in the read/write command.
247          */
248         command[5] = mask_byte;
249
250         /*
251          * If ALQ is set in the qualifier, this field contains the address of the
252          * registers where the byte count should be read for transferring the data.
253          * If ALQ is not set, then this field contains the number of bytes to be
254          * transferred.
255          */
256         command[6] = subcountL;
257         command[7] = subcountH;
258
259         return usbat_execute_command(us, command, 8);
260 }
261
262 /*
263  * Block, waiting for an ATA device to become not busy or to report
264  * an error condition.
265  */
266 static int usbat_wait_not_busy(struct us_data *us, int minutes)
267 {
268         int i;
269         int result;
270         unsigned char *status = us->iobuf;
271
272         /* Synchronizing cache on a CDR could take a heck of a long time,
273          * but probably not more than 10 minutes or so. On the other hand,
274          * doing a full blank on a CDRW at speed 1 will take about 75
275          * minutes!
276          */
277
278         for (i=0; i<1200+minutes*60; i++) {
279
280                 result = usbat_get_status(us, status);
281
282                 if (result!=USB_STOR_XFER_GOOD)
283                         return USB_STOR_TRANSPORT_ERROR;
284                 if (*status & 0x01) { /* check condition */
285                         result = usbat_read(us, USBAT_ATA, 0x10, status);
286                         return USB_STOR_TRANSPORT_FAILED;
287                 }
288                 if (*status & 0x20) /* device fault */
289                         return USB_STOR_TRANSPORT_FAILED;
290
291                 if ((*status & 0x80)==0x00) { /* not busy */
292                         US_DEBUGP("Waited not busy for %d steps\n", i);
293                         return USB_STOR_TRANSPORT_GOOD;
294                 }
295
296                 if (i<500)
297                         msleep(10); /* 5 seconds */
298                 else if (i<700)
299                         msleep(50); /* 10 seconds */
300                 else if (i<1200)
301                         msleep(100); /* 50 seconds */
302                 else
303                         msleep(1000); /* X minutes */
304         }
305
306         US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
307                 minutes);
308         return USB_STOR_TRANSPORT_FAILED;
309 }
310
311 /*
312  * Read block data from the data register
313  */
314 static int usbat_read_block(struct us_data *us,
315                             void* buf,
316                             unsigned short len,
317                             int use_sg)
318 {
319         int result;
320         unsigned char *command = us->iobuf;
321
322         if (!len)
323                 return USB_STOR_TRANSPORT_GOOD;
324
325         command[0] = 0xC0;
326         command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
327         command[2] = USBAT_ATA_DATA;
328         command[3] = 0;
329         command[4] = 0;
330         command[5] = 0;
331         command[6] = LSB_of(len);
332         command[7] = MSB_of(len);
333
334         result = usbat_execute_command(us, command, 8);
335         if (result != USB_STOR_XFER_GOOD)
336                 return USB_STOR_TRANSPORT_ERROR;
337
338         result = usbat_bulk_read(us, buf, len, use_sg);
339         return (result == USB_STOR_XFER_GOOD ?
340                         USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
341 }
342
343 /*
344  * Write block data via the data register
345  */
346 static int usbat_write_block(struct us_data *us,
347                              unsigned char access,
348                              void* buf,
349                              unsigned short len,
350                              int minutes,
351                              int use_sg)
352 {
353         int result;
354         unsigned char *command = us->iobuf;
355
356         if (!len)
357                 return USB_STOR_TRANSPORT_GOOD;
358
359         command[0] = 0x40;
360         command[1] = access | USBAT_CMD_WRITE_BLOCK;
361         command[2] = USBAT_ATA_DATA;
362         command[3] = 0;
363         command[4] = 0;
364         command[5] = 0;
365         command[6] = LSB_of(len);
366         command[7] = MSB_of(len);
367
368         result = usbat_execute_command(us, command, 8);
369
370         if (result != USB_STOR_XFER_GOOD)
371                 return USB_STOR_TRANSPORT_ERROR;
372
373         result = usbat_bulk_write(us, buf, len, use_sg);
374         if (result != USB_STOR_XFER_GOOD)
375                 return USB_STOR_TRANSPORT_ERROR;
376
377         return usbat_wait_not_busy(us, minutes);
378 }
379
380 /*
381  * Process read and write requests
382  */
383 static int usbat_hp8200e_rw_block_test(struct us_data *us,
384                                        unsigned char access,
385                                        unsigned char *registers,
386                                        unsigned char *data_out,
387                                        unsigned short num_registers,
388                                        unsigned char data_reg,
389                                        unsigned char status_reg,
390                                        unsigned char timeout,
391                                        unsigned char qualifier,
392                                        int direction,
393                                        void *buf,
394                                        unsigned short len,
395                                        int use_sg,
396                                        int minutes)
397 {
398         int result;
399         unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
400                         us->recv_bulk_pipe : us->send_bulk_pipe;
401
402         unsigned char *command = us->iobuf;
403         int i, j;
404         int cmdlen;
405         unsigned char *data = us->iobuf;
406         unsigned char *status = us->iobuf;
407
408         BUG_ON(num_registers > US_IOBUF_SIZE/2);
409
410         for (i=0; i<20; i++) {
411
412                 /*
413                  * The first time we send the full command, which consists
414                  * of downloading the SCSI command followed by downloading
415                  * the data via a write-and-test.  Any other time we only
416                  * send the command to download the data -- the SCSI command
417                  * is still 'active' in some sense in the device.
418                  * 
419                  * We're only going to try sending the data 10 times. After
420                  * that, we just return a failure.
421                  */
422
423                 if (i==0) {
424                         cmdlen = 16;
425                         /*
426                          * Write to multiple registers
427                          * Not really sure the 0x07, 0x17, 0xfc, 0xe7 is
428                          * necessary here, but that's what came out of the
429                          * trace every single time.
430                          */
431                         command[0] = 0x40;
432                         command[1] = access | USBAT_CMD_WRITE_REGS;
433                         command[2] = 0x07;
434                         command[3] = 0x17;
435                         command[4] = 0xFC;
436                         command[5] = 0xE7;
437                         command[6] = LSB_of(num_registers*2);
438                         command[7] = MSB_of(num_registers*2);
439                 } else
440                         cmdlen = 8;
441
442                 /* Conditionally read or write blocks */
443                 command[cmdlen-8] = (direction==DMA_TO_DEVICE ? 0x40 : 0xC0);
444                 command[cmdlen-7] = access |
445                                 (direction==DMA_TO_DEVICE ?
446                                  USBAT_CMD_COND_WRITE_BLOCK : USBAT_CMD_COND_READ_BLOCK);
447                 command[cmdlen-6] = data_reg;
448                 command[cmdlen-5] = status_reg;
449                 command[cmdlen-4] = timeout;
450                 command[cmdlen-3] = qualifier;
451                 command[cmdlen-2] = LSB_of(len);
452                 command[cmdlen-1] = MSB_of(len);
453
454                 result = usbat_execute_command(us, command, cmdlen);
455
456                 if (result != USB_STOR_XFER_GOOD)
457                         return USB_STOR_TRANSPORT_ERROR;
458
459                 if (i==0) {
460
461                         for (j=0; j<num_registers; j++) {
462                                 data[j<<1] = registers[j];
463                                 data[1+(j<<1)] = data_out[j];
464                         }
465
466                         result = usbat_bulk_write(us, data, num_registers*2, 0);
467                         if (result != USB_STOR_XFER_GOOD)
468                                 return USB_STOR_TRANSPORT_ERROR;
469
470                 }
471
472                 result = usb_stor_bulk_transfer_sg(us,
473                         pipe, buf, len, use_sg, NULL);
474
475                 /*
476                  * If we get a stall on the bulk download, we'll retry
477                  * the bulk download -- but not the SCSI command because
478                  * in some sense the SCSI command is still 'active' and
479                  * waiting for the data. Don't ask me why this should be;
480                  * I'm only following what the Windoze driver did.
481                  *
482                  * Note that a stall for the test-and-read/write command means
483                  * that the test failed. In this case we're testing to make
484                  * sure that the device is error-free
485                  * (i.e. bit 0 -- CHK -- of status is 0). The most likely
486                  * hypothesis is that the USBAT chip somehow knows what
487                  * the device will accept, but doesn't give the device any
488                  * data until all data is received. Thus, the device would
489                  * still be waiting for the first byte of data if a stall
490                  * occurs, even if the stall implies that some data was
491                  * transferred.
492                  */
493
494                 if (result == USB_STOR_XFER_SHORT ||
495                                 result == USB_STOR_XFER_STALLED) {
496
497                         /*
498                          * If we're reading and we stalled, then clear
499                          * the bulk output pipe only the first time.
500                          */
501
502                         if (direction==DMA_FROM_DEVICE && i==0) {
503                                 if (usb_stor_clear_halt(us,
504                                                 us->send_bulk_pipe) < 0)
505                                         return USB_STOR_TRANSPORT_ERROR;
506                         }
507
508                         /*
509                          * Read status: is the device angry, or just busy?
510                          */
511
512                         result = usbat_read(us, USBAT_ATA, 
513                                 direction==DMA_TO_DEVICE ?
514                                         USBAT_ATA_STATUS : USBAT_ATA_ALTSTATUS,
515                                 status);
516
517                         if (result!=USB_STOR_XFER_GOOD)
518                                 return USB_STOR_TRANSPORT_ERROR;
519                         if (*status & 0x01) /* check condition */
520                                 return USB_STOR_TRANSPORT_FAILED;
521                         if (*status & 0x20) /* device fault */
522                                 return USB_STOR_TRANSPORT_FAILED;
523
524                         US_DEBUGP("Redoing %s\n",
525                           direction==DMA_TO_DEVICE ? "write" : "read");
526
527                 } else if (result != USB_STOR_XFER_GOOD)
528                         return USB_STOR_TRANSPORT_ERROR;
529                 else
530                         return usbat_wait_not_busy(us, minutes);
531
532         }
533
534         US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
535                 direction==DMA_TO_DEVICE ? "Writing" : "Reading");
536
537         return USB_STOR_TRANSPORT_FAILED;
538 }
539
540 /*
541  * Write to multiple registers:
542  * Allows us to write specific data to any registers. The data to be written
543  * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN
544  * which gets sent through bulk out.
545  * Not designed for large transfers of data!
546  */
547 static int usbat_multiple_write(struct us_data *us,
548                                 unsigned char *registers,
549                                 unsigned char *data_out,
550                                 unsigned short num_registers)
551 {
552         int i, result;
553         unsigned char *data = us->iobuf;
554         unsigned char *command = us->iobuf;
555
556         BUG_ON(num_registers > US_IOBUF_SIZE/2);
557
558         /* Write to multiple registers, ATA access */
559         command[0] = 0x40;
560         command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
561
562         /* No relevance */
563         command[2] = 0;
564         command[3] = 0;
565         command[4] = 0;
566         command[5] = 0;
567
568         /* Number of bytes to be transferred (incl. addresses and data) */
569         command[6] = LSB_of(num_registers*2);
570         command[7] = MSB_of(num_registers*2);
571
572         /* The setup command */
573         result = usbat_execute_command(us, command, 8);
574         if (result != USB_STOR_XFER_GOOD)
575                 return USB_STOR_TRANSPORT_ERROR;
576
577         /* Create the reg/data, reg/data sequence */
578         for (i=0; i<num_registers; i++) {
579                 data[i<<1] = registers[i];
580                 data[1+(i<<1)] = data_out[i];
581         }
582
583         /* Send the data */
584         result = usbat_bulk_write(us, data, num_registers*2, 0);
585         if (result != USB_STOR_XFER_GOOD)
586                 return USB_STOR_TRANSPORT_ERROR;
587
588         if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
589                 return usbat_wait_not_busy(us, 0);
590         else
591                 return USB_STOR_TRANSPORT_GOOD;
592 }
593
594 /*
595  * Conditionally read blocks from device:
596  * Allows us to read blocks from a specific data register, based upon the
597  * condition that a status register can be successfully masked with a status
598  * qualifier. If this condition is not initially met, the read will wait
599  * up until a maximum amount of time has elapsed, as specified by timeout.
600  * The read will start when the condition is met, otherwise the command aborts.
601  *
602  * The qualifier defined here is not the value that is masked, it defines
603  * conditions for the write to take place. The actual masked qualifier (and
604  * other related details) are defined beforehand with _set_shuttle_features().
605  */
606 static int usbat_read_blocks(struct us_data *us,
607                              void* buffer,
608                              int len,
609                              int use_sg)
610 {
611         int result;
612         unsigned char *command = us->iobuf;
613
614         command[0] = 0xC0;
615         command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK;
616         command[2] = USBAT_ATA_DATA;
617         command[3] = USBAT_ATA_STATUS;
618         command[4] = 0xFD; /* Timeout (ms); */
619         command[5] = USBAT_QUAL_FCQ;
620         command[6] = LSB_of(len);
621         command[7] = MSB_of(len);
622
623         /* Multiple block read setup command */
624         result = usbat_execute_command(us, command, 8);
625         if (result != USB_STOR_XFER_GOOD)
626                 return USB_STOR_TRANSPORT_FAILED;
627         
628         /* Read the blocks we just asked for */
629         result = usbat_bulk_read(us, buffer, len, use_sg);
630         if (result != USB_STOR_XFER_GOOD)
631                 return USB_STOR_TRANSPORT_FAILED;
632
633         return USB_STOR_TRANSPORT_GOOD;
634 }
635
636 /*
637  * Conditionally write blocks to device:
638  * Allows us to write blocks to a specific data register, based upon the
639  * condition that a status register can be successfully masked with a status
640  * qualifier. If this condition is not initially met, the write will wait
641  * up until a maximum amount of time has elapsed, as specified by timeout.
642  * The read will start when the condition is met, otherwise the command aborts.
643  *
644  * The qualifier defined here is not the value that is masked, it defines
645  * conditions for the write to take place. The actual masked qualifier (and
646  * other related details) are defined beforehand with _set_shuttle_features().
647  */
648 static int usbat_write_blocks(struct us_data *us,
649                               void* buffer,
650                               int len,
651                               int use_sg)
652 {
653         int result;
654         unsigned char *command = us->iobuf;
655
656         command[0] = 0x40;
657         command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK;
658         command[2] = USBAT_ATA_DATA;
659         command[3] = USBAT_ATA_STATUS;
660         command[4] = 0xFD; /* Timeout (ms) */
661         command[5] = USBAT_QUAL_FCQ;
662         command[6] = LSB_of(len);
663         command[7] = MSB_of(len);
664
665         /* Multiple block write setup command */
666         result = usbat_execute_command(us, command, 8);
667         if (result != USB_STOR_XFER_GOOD)
668                 return USB_STOR_TRANSPORT_FAILED;
669         
670         /* Write the data */
671         result = usbat_bulk_write(us, buffer, len, use_sg);
672         if (result != USB_STOR_XFER_GOOD)
673                 return USB_STOR_TRANSPORT_FAILED;
674
675         return USB_STOR_TRANSPORT_GOOD;
676 }
677
678 /*
679  * Read the User IO register
680  */
681 static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
682 {
683         int result;
684
685         result = usb_stor_ctrl_transfer(us,
686                 us->recv_ctrl_pipe,
687                 USBAT_CMD_UIO,
688                 0xC0,
689                 0,
690                 0,
691                 data_flags,
692                 USBAT_UIO_READ);
693
694         US_DEBUGP("usbat_read_user_io: UIO register reads %02X\n", (unsigned short) (*data_flags));
695
696         return result;
697 }
698
699 /*
700  * Write to the User IO register
701  */
702 static int usbat_write_user_io(struct us_data *us,
703                                unsigned char enable_flags,
704                                unsigned char data_flags)
705 {
706         return usb_stor_ctrl_transfer(us,
707                 us->send_ctrl_pipe,
708                 USBAT_CMD_UIO,
709                 0x40,
710                 short_pack(enable_flags, data_flags),
711                 0,
712                 NULL,
713                 USBAT_UIO_WRITE);
714 }
715
716 /*
717  * Reset the device
718  * Often needed on media change.
719  */
720 static int usbat_device_reset(struct us_data *us)
721 {
722         int rc;
723
724         /*
725          * Reset peripheral, enable peripheral control signals
726          * (bring reset signal up)
727          */
728         rc = usbat_write_user_io(us,
729                                                          USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
730                                                          USBAT_UIO_EPAD | USBAT_UIO_1);
731         if (rc != USB_STOR_XFER_GOOD)
732                 return USB_STOR_TRANSPORT_ERROR;
733                         
734         /*
735          * Enable peripheral control signals
736          * (bring reset signal down)
737          */
738         rc = usbat_write_user_io(us,
739                                                          USBAT_UIO_OE1  | USBAT_UIO_OE0,
740                                                          USBAT_UIO_EPAD | USBAT_UIO_1);
741         if (rc != USB_STOR_XFER_GOOD)
742                 return USB_STOR_TRANSPORT_ERROR;
743
744         return USB_STOR_TRANSPORT_GOOD;
745 }
746
747 /*
748  * Enable card detect
749  */
750 static int usbat_device_enable_cdt(struct us_data *us)
751 {
752         int rc;
753
754         /* Enable peripheral control signals and card detect */
755         rc = usbat_write_user_io(us,
756                                                          USBAT_UIO_ACKD | USBAT_UIO_OE1  | USBAT_UIO_OE0,
757                                                          USBAT_UIO_EPAD | USBAT_UIO_1);
758         if (rc != USB_STOR_XFER_GOOD)
759                 return USB_STOR_TRANSPORT_ERROR;
760
761         return USB_STOR_TRANSPORT_GOOD;
762 }
763
764 /*
765  * Determine if media is present.
766  */
767 static int usbat_flash_check_media_present(unsigned char *uio)
768 {
769         if (*uio & USBAT_UIO_UI0) {
770                 US_DEBUGP("usbat_flash_check_media_present: no media detected\n");
771                 return USBAT_FLASH_MEDIA_NONE;
772         }
773
774         return USBAT_FLASH_MEDIA_CF;
775 }
776
777 /*
778  * Determine if media has changed since last operation
779  */
780 static int usbat_flash_check_media_changed(unsigned char *uio)
781 {
782         if (*uio & USBAT_UIO_0) {
783                 US_DEBUGP("usbat_flash_check_media_changed: media change detected\n");
784                 return USBAT_FLASH_MEDIA_CHANGED;
785         }
786
787         return USBAT_FLASH_MEDIA_SAME;
788 }
789
790 /*
791  * Check for media change / no media and handle the situation appropriately
792  */
793 static int usbat_flash_check_media(struct us_data *us,
794                                    struct usbat_info *info)
795 {
796         int rc;
797         unsigned char *uio = us->iobuf;
798
799         rc = usbat_read_user_io(us, uio);
800         if (rc != USB_STOR_XFER_GOOD)
801                 return USB_STOR_TRANSPORT_ERROR;
802
803         /* Check for media existence */
804         rc = usbat_flash_check_media_present(uio);
805         if (rc == USBAT_FLASH_MEDIA_NONE) {
806                 info->sense_key = 0x02;
807                 info->sense_asc = 0x3A;
808                 info->sense_ascq = 0x00;
809                 return USB_STOR_TRANSPORT_FAILED;
810         }
811
812         /* Check for media change */
813         rc = usbat_flash_check_media_changed(uio);
814         if (rc == USBAT_FLASH_MEDIA_CHANGED) {
815
816                 /* Reset and re-enable card detect */
817                 rc = usbat_device_reset(us);
818                 if (rc != USB_STOR_TRANSPORT_GOOD)
819                         return rc;
820                 rc = usbat_device_enable_cdt(us);
821                 if (rc != USB_STOR_TRANSPORT_GOOD)
822                         return rc;
823
824                 msleep(50);
825
826                 rc = usbat_read_user_io(us, uio);
827                 if (rc != USB_STOR_XFER_GOOD)
828                         return USB_STOR_TRANSPORT_ERROR;
829                 
830                 info->sense_key = UNIT_ATTENTION;
831                 info->sense_asc = 0x28;
832                 info->sense_ascq = 0x00;
833                 return USB_STOR_TRANSPORT_FAILED;
834         }
835
836         return USB_STOR_TRANSPORT_GOOD;
837 }
838
839 /*
840  * Determine whether we are controlling a flash-based reader/writer,
841  * or a HP8200-based CD drive.
842  * Sets transport functions as appropriate.
843  */
844 static int usbat_identify_device(struct us_data *us,
845                                  struct usbat_info *info)
846 {
847         int rc;
848         unsigned char status;
849
850         if (!us || !info)
851                 return USB_STOR_TRANSPORT_ERROR;
852
853         rc = usbat_device_reset(us);
854         if (rc != USB_STOR_TRANSPORT_GOOD)
855                 return rc;
856         msleep(500);
857
858         /*
859          * In attempt to distinguish between HP CDRW's and Flash readers, we now
860          * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
861          * readers), this command should fail with error. On ATAPI devices (i.e.
862          * CDROM drives), it should succeed.
863          */
864         rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1);
865         if (rc != USB_STOR_XFER_GOOD)
866                 return USB_STOR_TRANSPORT_ERROR;
867
868         rc = usbat_get_status(us, &status);
869         if (rc != USB_STOR_XFER_GOOD)
870                 return USB_STOR_TRANSPORT_ERROR;
871
872         /* Check for error bit, or if the command 'fell through' */
873         if (status == 0xA1 || !(status & 0x01)) {
874                 /* Device is HP 8200 */
875                 US_DEBUGP("usbat_identify_device: Detected HP8200 CDRW\n");
876                 info->devicetype = USBAT_DEV_HP8200;
877         } else {
878                 /* Device is a CompactFlash reader/writer */
879                 US_DEBUGP("usbat_identify_device: Detected Flash reader/writer\n");
880                 info->devicetype = USBAT_DEV_FLASH;
881         }
882
883         return USB_STOR_TRANSPORT_GOOD;
884 }
885
886 /*
887  * Set the transport function based on the device type
888  */
889 static int usbat_set_transport(struct us_data *us,
890                                struct usbat_info *info,
891                                int devicetype)
892 {
893
894         if (!info->devicetype)
895                 info->devicetype = devicetype;
896
897         if (!info->devicetype)
898                 usbat_identify_device(us, info);
899
900         switch (info->devicetype) {
901         default:
902                 return USB_STOR_TRANSPORT_ERROR;
903
904         case  USBAT_DEV_HP8200:
905                 us->transport = usbat_hp8200e_transport;
906                 break;
907
908         case USBAT_DEV_FLASH:
909                 us->transport = usbat_flash_transport;
910                 break;
911         }
912
913         return 0;
914 }
915
916 /*
917  * Read the media capacity
918  */
919 static int usbat_flash_get_sector_count(struct us_data *us,
920                                         struct usbat_info *info)
921 {
922         unsigned char registers[3] = {
923                 USBAT_ATA_SECCNT,
924                 USBAT_ATA_DEVICE,
925                 USBAT_ATA_CMD,
926         };
927         unsigned char  command[3] = { 0x01, 0xA0, 0xEC };
928         unsigned char *reply;
929         unsigned char status;
930         int rc;
931
932         if (!us || !info)
933                 return USB_STOR_TRANSPORT_ERROR;
934
935         reply = kmalloc(512, GFP_NOIO);
936         if (!reply)
937                 return USB_STOR_TRANSPORT_ERROR;
938
939         /* ATA command : IDENTIFY DEVICE */
940         rc = usbat_multiple_write(us, registers, command, 3);
941         if (rc != USB_STOR_XFER_GOOD) {
942                 US_DEBUGP("usbat_flash_get_sector_count: Gah! identify_device failed\n");
943                 rc = USB_STOR_TRANSPORT_ERROR;
944                 goto leave;
945         }
946
947         /* Read device status */
948         if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
949                 rc = USB_STOR_TRANSPORT_ERROR;
950                 goto leave;
951         }
952
953         msleep(100);
954
955         /* Read the device identification data */
956         rc = usbat_read_block(us, reply, 512, 0);
957         if (rc != USB_STOR_TRANSPORT_GOOD)
958                 goto leave;
959
960         info->sectors = ((u32)(reply[117]) << 24) |
961                 ((u32)(reply[116]) << 16) |
962                 ((u32)(reply[115]) <<  8) |
963                 ((u32)(reply[114])      );
964
965         rc = USB_STOR_TRANSPORT_GOOD;
966
967  leave:
968         kfree(reply);
969         return rc;
970 }
971
972 /*
973  * Read data from device
974  */
975 static int usbat_flash_read_data(struct us_data *us,
976                                                                  struct usbat_info *info,
977                                                                  u32 sector,
978                                                                  u32 sectors)
979 {
980         unsigned char registers[7] = {
981                 USBAT_ATA_FEATURES,
982                 USBAT_ATA_SECCNT,
983                 USBAT_ATA_SECNUM,
984                 USBAT_ATA_LBA_ME,
985                 USBAT_ATA_LBA_HI,
986                 USBAT_ATA_DEVICE,
987                 USBAT_ATA_STATUS,
988         };
989         unsigned char command[7];
990         unsigned char *buffer;
991         unsigned char  thistime;
992         unsigned int totallen, alloclen;
993         int len, result;
994         unsigned int sg_offset = 0;
995         struct scatterlist *sg = NULL;
996
997         result = usbat_flash_check_media(us, info);
998         if (result != USB_STOR_TRANSPORT_GOOD)
999                 return result;
1000
1001         /*
1002          * we're working in LBA mode.  according to the ATA spec,
1003          * we can support up to 28-bit addressing.  I don't know if Jumpshot
1004          * supports beyond 24-bit addressing.  It's kind of hard to test
1005          * since it requires > 8GB CF card.
1006          */
1007
1008         if (sector > 0x0FFFFFFF)
1009                 return USB_STOR_TRANSPORT_ERROR;
1010
1011         totallen = sectors * info->ssize;
1012
1013         /*
1014          * Since we don't read more than 64 KB at a time, we have to create
1015          * a bounce buffer and move the data a piece at a time between the
1016          * bounce buffer and the actual transfer buffer.
1017          */
1018
1019         alloclen = min(totallen, 65536u);
1020         buffer = kmalloc(alloclen, GFP_NOIO);
1021         if (buffer == NULL)
1022                 return USB_STOR_TRANSPORT_ERROR;
1023
1024         do {
1025                 /*
1026                  * loop, never allocate or transfer more than 64k at once
1027                  * (min(128k, 255*info->ssize) is the real limit)
1028                  */
1029                 len = min(totallen, alloclen);
1030                 thistime = (len / info->ssize) & 0xff;
1031  
1032                 /* ATA command 0x20 (READ SECTORS) */
1033                 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
1034
1035                 /* Write/execute ATA read command */
1036                 result = usbat_multiple_write(us, registers, command, 7);
1037                 if (result != USB_STOR_TRANSPORT_GOOD)
1038                         goto leave;
1039
1040                 /* Read the data we just requested */
1041                 result = usbat_read_blocks(us, buffer, len, 0);
1042                 if (result != USB_STOR_TRANSPORT_GOOD)
1043                         goto leave;
1044          
1045                 US_DEBUGP("usbat_flash_read_data:  %d bytes\n", len);
1046         
1047                 /* Store the data in the transfer buffer */
1048                 usb_stor_access_xfer_buf(buffer, len, us->srb,
1049                                          &sg, &sg_offset, TO_XFER_BUF);
1050
1051                 sector += thistime;
1052                 totallen -= len;
1053         } while (totallen > 0);
1054
1055         kfree(buffer);
1056         return USB_STOR_TRANSPORT_GOOD;
1057
1058 leave:
1059         kfree(buffer);
1060         return USB_STOR_TRANSPORT_ERROR;
1061 }
1062
1063 /*
1064  * Write data to device
1065  */
1066 static int usbat_flash_write_data(struct us_data *us,
1067                                                                   struct usbat_info *info,
1068                                                                   u32 sector,
1069                                                                   u32 sectors)
1070 {
1071         unsigned char registers[7] = {
1072                 USBAT_ATA_FEATURES,
1073                 USBAT_ATA_SECCNT,
1074                 USBAT_ATA_SECNUM,
1075                 USBAT_ATA_LBA_ME,
1076                 USBAT_ATA_LBA_HI,
1077                 USBAT_ATA_DEVICE,
1078                 USBAT_ATA_STATUS,
1079         };
1080         unsigned char command[7];
1081         unsigned char *buffer;
1082         unsigned char  thistime;
1083         unsigned int totallen, alloclen;
1084         int len, result;
1085         unsigned int sg_offset = 0;
1086         struct scatterlist *sg = NULL;
1087
1088         result = usbat_flash_check_media(us, info);
1089         if (result != USB_STOR_TRANSPORT_GOOD)
1090                 return result;
1091
1092         /*
1093          * we're working in LBA mode.  according to the ATA spec,
1094          * we can support up to 28-bit addressing.  I don't know if the device
1095          * supports beyond 24-bit addressing.  It's kind of hard to test
1096          * since it requires > 8GB media.
1097          */
1098
1099         if (sector > 0x0FFFFFFF)
1100                 return USB_STOR_TRANSPORT_ERROR;
1101
1102         totallen = sectors * info->ssize;
1103
1104         /*
1105          * Since we don't write more than 64 KB at a time, we have to create
1106          * a bounce buffer and move the data a piece at a time between the
1107          * bounce buffer and the actual transfer buffer.
1108          */
1109
1110         alloclen = min(totallen, 65536u);
1111         buffer = kmalloc(alloclen, GFP_NOIO);
1112         if (buffer == NULL)
1113                 return USB_STOR_TRANSPORT_ERROR;
1114
1115         do {
1116                 /*
1117                  * loop, never allocate or transfer more than 64k at once
1118                  * (min(128k, 255*info->ssize) is the real limit)
1119                  */
1120                 len = min(totallen, alloclen);
1121                 thistime = (len / info->ssize) & 0xff;
1122
1123                 /* Get the data from the transfer buffer */
1124                 usb_stor_access_xfer_buf(buffer, len, us->srb,
1125                                          &sg, &sg_offset, FROM_XFER_BUF);
1126
1127                 /* ATA command 0x30 (WRITE SECTORS) */
1128                 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
1129
1130                 /* Write/execute ATA write command */
1131                 result = usbat_multiple_write(us, registers, command, 7);
1132                 if (result != USB_STOR_TRANSPORT_GOOD)
1133                         goto leave;
1134
1135                 /* Write the data */
1136                 result = usbat_write_blocks(us, buffer, len, 0);
1137                 if (result != USB_STOR_TRANSPORT_GOOD)
1138                         goto leave;
1139
1140                 sector += thistime;
1141                 totallen -= len;
1142         } while (totallen > 0);
1143
1144         kfree(buffer);
1145         return result;
1146
1147 leave:
1148         kfree(buffer);
1149         return USB_STOR_TRANSPORT_ERROR;
1150 }
1151
1152 /*
1153  * Squeeze a potentially huge (> 65535 byte) read10 command into
1154  * a little ( <= 65535 byte) ATAPI pipe
1155  */
1156 static int usbat_hp8200e_handle_read10(struct us_data *us,
1157                                        unsigned char *registers,
1158                                        unsigned char *data,
1159                                        struct scsi_cmnd *srb)
1160 {
1161         int result = USB_STOR_TRANSPORT_GOOD;
1162         unsigned char *buffer;
1163         unsigned int len;
1164         unsigned int sector;
1165         unsigned int sg_offset = 0;
1166         struct scatterlist *sg = NULL;
1167
1168         US_DEBUGP("handle_read10: transfersize %d\n",
1169                 srb->transfersize);
1170
1171         if (scsi_bufflen(srb) < 0x10000) {
1172
1173                 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA, 
1174                         registers, data, 19,
1175                         USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1176                         (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1177                         DMA_FROM_DEVICE,
1178                         scsi_sglist(srb),
1179                         scsi_bufflen(srb), scsi_sg_count(srb), 1);
1180
1181                 return result;
1182         }
1183
1184         /*
1185          * Since we're requesting more data than we can handle in
1186          * a single read command (max is 64k-1), we will perform
1187          * multiple reads, but each read must be in multiples of
1188          * a sector.  Luckily the sector size is in srb->transfersize
1189          * (see linux/drivers/scsi/sr.c).
1190          */
1191
1192         if (data[7+0] == GPCMD_READ_CD) {
1193                 len = short_pack(data[7+9], data[7+8]);
1194                 len <<= 16;
1195                 len |= data[7+7];
1196                 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
1197                 srb->transfersize = scsi_bufflen(srb)/len;
1198         }
1199
1200         if (!srb->transfersize)  {
1201                 srb->transfersize = 2048; /* A guess */
1202                 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
1203                         srb->transfersize);
1204         }
1205
1206         /*
1207          * Since we only read in one block at a time, we have to create
1208          * a bounce buffer and move the data a piece at a time between the
1209          * bounce buffer and the actual transfer buffer.
1210          */
1211
1212         len = (65535/srb->transfersize) * srb->transfersize;
1213         US_DEBUGP("Max read is %d bytes\n", len);
1214         len = min(len, scsi_bufflen(srb));
1215         buffer = kmalloc(len, GFP_NOIO);
1216         if (buffer == NULL) /* bloody hell! */
1217                 return USB_STOR_TRANSPORT_FAILED;
1218         sector = short_pack(data[7+3], data[7+2]);
1219         sector <<= 16;
1220         sector |= short_pack(data[7+5], data[7+4]);
1221         transferred = 0;
1222
1223         while (transferred != scsi_bufflen(srb)) {
1224
1225                 if (len > scsi_bufflen(srb) - transferred)
1226                         len = scsi_bufflen(srb) - transferred;
1227
1228                 data[3] = len&0xFF;       /* (cylL) = expected length (L) */
1229                 data[4] = (len>>8)&0xFF;  /* (cylH) = expected length (H) */
1230
1231                 /* Fix up the SCSI command sector and num sectors */
1232
1233                 data[7+2] = MSB_of(sector>>16); /* SCSI command sector */
1234                 data[7+3] = LSB_of(sector>>16);
1235                 data[7+4] = MSB_of(sector&0xFFFF);
1236                 data[7+5] = LSB_of(sector&0xFFFF);
1237                 if (data[7+0] == GPCMD_READ_CD)
1238                         data[7+6] = 0;
1239                 data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */
1240                 data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */
1241
1242                 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA, 
1243                         registers, data, 19,
1244                         USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD, 
1245                         (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1246                         DMA_FROM_DEVICE,
1247                         buffer,
1248                         len, 0, 1);
1249
1250                 if (result != USB_STOR_TRANSPORT_GOOD)
1251                         break;
1252
1253                 /* Store the data in the transfer buffer */
1254                 usb_stor_access_xfer_buf(buffer, len, srb,
1255                                  &sg, &sg_offset, TO_XFER_BUF);
1256
1257                 /* Update the amount transferred and the sector number */
1258
1259                 transferred += len;
1260                 sector += len / srb->transfersize;
1261
1262         } /* while transferred != scsi_bufflen(srb) */
1263
1264         kfree(buffer);
1265         return result;
1266 }
1267
1268 static int usbat_select_and_test_registers(struct us_data *us)
1269 {
1270         int selector;
1271         unsigned char *status = us->iobuf;
1272
1273         /* try device = master, then device = slave. */
1274         for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
1275                 if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
1276                                 USB_STOR_XFER_GOOD)
1277                         return USB_STOR_TRANSPORT_ERROR;
1278
1279                 if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) != 
1280                                 USB_STOR_XFER_GOOD)
1281                         return USB_STOR_TRANSPORT_ERROR;
1282
1283                 if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) != 
1284                                 USB_STOR_XFER_GOOD)
1285                         return USB_STOR_TRANSPORT_ERROR;
1286
1287                 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != 
1288                                 USB_STOR_XFER_GOOD)
1289                         return USB_STOR_TRANSPORT_ERROR;
1290
1291                 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) != 
1292                                 USB_STOR_XFER_GOOD)
1293                         return USB_STOR_TRANSPORT_ERROR;
1294
1295                 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) != 
1296                                 USB_STOR_XFER_GOOD)
1297                         return USB_STOR_TRANSPORT_ERROR;
1298
1299                 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) != 
1300                                 USB_STOR_XFER_GOOD)
1301                         return USB_STOR_TRANSPORT_ERROR;
1302
1303                 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != 
1304                                 USB_STOR_XFER_GOOD)
1305                         return USB_STOR_TRANSPORT_ERROR;
1306
1307                 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != 
1308                                 USB_STOR_XFER_GOOD)
1309                         return USB_STOR_TRANSPORT_ERROR;
1310         }
1311
1312         return USB_STOR_TRANSPORT_GOOD;
1313 }
1314
1315 /*
1316  * Initialize the USBAT processor and the storage device
1317  */
1318 static int init_usbat(struct us_data *us, int devicetype)
1319 {
1320         int rc;
1321         struct usbat_info *info;
1322         unsigned char subcountH = USBAT_ATA_LBA_HI;
1323         unsigned char subcountL = USBAT_ATA_LBA_ME;
1324         unsigned char *status = us->iobuf;
1325
1326         us->extra = kzalloc(sizeof(struct usbat_info), GFP_NOIO);
1327         if (!us->extra) {
1328                 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1329                 return 1;
1330         }
1331         info = (struct usbat_info *) (us->extra);
1332
1333         /* Enable peripheral control signals */
1334         rc = usbat_write_user_io(us,
1335                                  USBAT_UIO_OE1 | USBAT_UIO_OE0,
1336                                  USBAT_UIO_EPAD | USBAT_UIO_1);
1337         if (rc != USB_STOR_XFER_GOOD)
1338                 return USB_STOR_TRANSPORT_ERROR;
1339
1340         US_DEBUGP("INIT 1\n");
1341
1342         msleep(2000);
1343
1344         rc = usbat_read_user_io(us, status);
1345         if (rc != USB_STOR_TRANSPORT_GOOD)
1346                 return rc;
1347
1348         US_DEBUGP("INIT 2\n");
1349
1350         rc = usbat_read_user_io(us, status);
1351         if (rc != USB_STOR_XFER_GOOD)
1352                 return USB_STOR_TRANSPORT_ERROR;
1353
1354         rc = usbat_read_user_io(us, status);
1355         if (rc != USB_STOR_XFER_GOOD)
1356                 return USB_STOR_TRANSPORT_ERROR;
1357
1358         US_DEBUGP("INIT 3\n");
1359
1360         rc = usbat_select_and_test_registers(us);
1361         if (rc != USB_STOR_TRANSPORT_GOOD)
1362                 return rc;
1363
1364         US_DEBUGP("INIT 4\n");
1365
1366         rc = usbat_read_user_io(us, status);
1367         if (rc != USB_STOR_XFER_GOOD)
1368                 return USB_STOR_TRANSPORT_ERROR;
1369
1370         US_DEBUGP("INIT 5\n");
1371
1372         /* Enable peripheral control signals and card detect */
1373         rc = usbat_device_enable_cdt(us);
1374         if (rc != USB_STOR_TRANSPORT_GOOD)
1375                 return rc;
1376
1377         US_DEBUGP("INIT 6\n");
1378
1379         rc = usbat_read_user_io(us, status);
1380         if (rc != USB_STOR_XFER_GOOD)
1381                 return USB_STOR_TRANSPORT_ERROR;
1382
1383         US_DEBUGP("INIT 7\n");
1384
1385         msleep(1400);
1386
1387         rc = usbat_read_user_io(us, status);
1388         if (rc != USB_STOR_XFER_GOOD)
1389                 return USB_STOR_TRANSPORT_ERROR;
1390
1391         US_DEBUGP("INIT 8\n");
1392
1393         rc = usbat_select_and_test_registers(us);
1394         if (rc != USB_STOR_TRANSPORT_GOOD)
1395                 return rc;
1396
1397         US_DEBUGP("INIT 9\n");
1398
1399         /* At this point, we need to detect which device we are using */
1400         if (usbat_set_transport(us, info, devicetype))
1401                 return USB_STOR_TRANSPORT_ERROR;
1402
1403         US_DEBUGP("INIT 10\n");
1404
1405         if (usbat_get_device_type(us) == USBAT_DEV_FLASH) { 
1406                 subcountH = 0x02;
1407                 subcountL = 0x00;
1408         }
1409         rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1410                                                                         0x00, 0x88, 0x08, subcountH, subcountL);
1411         if (rc != USB_STOR_XFER_GOOD)
1412                 return USB_STOR_TRANSPORT_ERROR;
1413
1414         US_DEBUGP("INIT 11\n");
1415
1416         return USB_STOR_TRANSPORT_GOOD;
1417 }
1418
1419 /*
1420  * Transport for the HP 8200e
1421  */
1422 static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1423 {
1424         int result;
1425         unsigned char *status = us->iobuf;
1426         unsigned char registers[32];
1427         unsigned char data[32];
1428         unsigned int len;
1429         int i;
1430
1431         len = scsi_bufflen(srb);
1432
1433         /* Send A0 (ATA PACKET COMMAND).
1434            Note: I guess we're never going to get any of the ATA
1435            commands... just ATA Packet Commands.
1436          */
1437
1438         registers[0] = USBAT_ATA_FEATURES;
1439         registers[1] = USBAT_ATA_SECCNT;
1440         registers[2] = USBAT_ATA_SECNUM;
1441         registers[3] = USBAT_ATA_LBA_ME;
1442         registers[4] = USBAT_ATA_LBA_HI;
1443         registers[5] = USBAT_ATA_DEVICE;
1444         registers[6] = USBAT_ATA_CMD;
1445         data[0] = 0x00;
1446         data[1] = 0x00;
1447         data[2] = 0x00;
1448         data[3] = len&0xFF;             /* (cylL) = expected length (L) */
1449         data[4] = (len>>8)&0xFF;        /* (cylH) = expected length (H) */
1450         data[5] = 0xB0;                 /* (device sel) = slave */
1451         data[6] = 0xA0;                 /* (command) = ATA PACKET COMMAND */
1452
1453         for (i=7; i<19; i++) {
1454                 registers[i] = 0x10;
1455                 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1456         }
1457
1458         result = usbat_get_status(us, status);
1459         US_DEBUGP("Status = %02X\n", *status);
1460         if (result != USB_STOR_XFER_GOOD)
1461                 return USB_STOR_TRANSPORT_ERROR;
1462         if (srb->cmnd[0] == TEST_UNIT_READY)
1463                 transferred = 0;
1464
1465         if (srb->sc_data_direction == DMA_TO_DEVICE) {
1466
1467                 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA, 
1468                         registers, data, 19,
1469                         USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1470                         (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1471                         DMA_TO_DEVICE,
1472                         scsi_sglist(srb),
1473                         len, scsi_sg_count(srb), 10);
1474
1475                 if (result == USB_STOR_TRANSPORT_GOOD) {
1476                         transferred += len;
1477                         US_DEBUGP("Wrote %08X bytes\n", transferred);
1478                 }
1479
1480                 return result;
1481
1482         } else if (srb->cmnd[0] == READ_10 ||
1483                    srb->cmnd[0] == GPCMD_READ_CD) {
1484
1485                 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1486
1487         }
1488
1489         if (len > 0xFFFF) {
1490                 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1491                         len);
1492                 return USB_STOR_TRANSPORT_ERROR;
1493         }
1494
1495         if ( (result = usbat_multiple_write(us, 
1496                         registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
1497                 return result;
1498         }
1499
1500         /*
1501          * Write the 12-byte command header.
1502          *
1503          * If the command is BLANK then set the timer for 75 minutes.
1504          * Otherwise set it for 10 minutes.
1505          *
1506          * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1507          * AT SPEED 4 IS UNRELIABLE!!!
1508          */
1509
1510         if ((result = usbat_write_block(us,
1511                         USBAT_ATA, srb->cmnd, 12,
1512                                 (srb->cmnd[0]==GPCMD_BLANK ? 75 : 10), 0) !=
1513                              USB_STOR_TRANSPORT_GOOD)) {
1514                 return result;
1515         }
1516
1517         /* If there is response data to be read in then do it here. */
1518
1519         if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1520
1521                 /* How many bytes to read in? Check cylL register */
1522
1523                 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != 
1524                         USB_STOR_XFER_GOOD) {
1525                         return USB_STOR_TRANSPORT_ERROR;
1526                 }
1527
1528                 if (len > 0xFF) { /* need to read cylH also */
1529                         len = *status;
1530                         if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1531                                     USB_STOR_XFER_GOOD) {
1532                                 return USB_STOR_TRANSPORT_ERROR;
1533                         }
1534                         len += ((unsigned int) *status)<<8;
1535                 }
1536                 else
1537                         len = *status;
1538
1539
1540                 result = usbat_read_block(us, scsi_sglist(srb), len,
1541                                                            scsi_sg_count(srb));
1542         }
1543
1544         return result;
1545 }
1546
1547 /*
1548  * Transport for USBAT02-based CompactFlash and similar storage devices
1549  */
1550 static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1551 {
1552         int rc;
1553         struct usbat_info *info = (struct usbat_info *) (us->extra);
1554         unsigned long block, blocks;
1555         unsigned char *ptr = us->iobuf;
1556         static unsigned char inquiry_response[36] = {
1557                 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1558         };
1559
1560         if (srb->cmnd[0] == INQUIRY) {
1561                 US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1562                 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1563                 fill_inquiry_response(us, ptr, 36);
1564                 return USB_STOR_TRANSPORT_GOOD;
1565         }
1566
1567         if (srb->cmnd[0] == READ_CAPACITY) {
1568                 rc = usbat_flash_check_media(us, info);
1569                 if (rc != USB_STOR_TRANSPORT_GOOD)
1570                         return rc;
1571
1572                 rc = usbat_flash_get_sector_count(us, info);
1573                 if (rc != USB_STOR_TRANSPORT_GOOD)
1574                         return rc;
1575
1576                 /* hard coded 512 byte sectors as per ATA spec */
1577                 info->ssize = 0x200;
1578                 US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1579                           info->sectors, info->ssize);
1580
1581                 /*
1582                  * build the reply
1583                  * note: must return the sector number of the last sector,
1584                  * *not* the total number of sectors
1585                  */
1586                 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1587                 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1588                 usb_stor_set_xfer_buf(ptr, 8, srb);
1589
1590                 return USB_STOR_TRANSPORT_GOOD;
1591         }
1592
1593         if (srb->cmnd[0] == MODE_SELECT_10) {
1594                 US_DEBUGP("usbat_flash_transport:  Gah! MODE_SELECT_10.\n");
1595                 return USB_STOR_TRANSPORT_ERROR;
1596         }
1597
1598         if (srb->cmnd[0] == READ_10) {
1599                 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1600                                 ((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
1601
1602                 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1603
1604                 US_DEBUGP("usbat_flash_transport:  READ_10: read block 0x%04lx  count %ld\n", block, blocks);
1605                 return usbat_flash_read_data(us, info, block, blocks);
1606         }
1607
1608         if (srb->cmnd[0] == READ_12) {
1609                 /*
1610                  * I don't think we'll ever see a READ_12 but support it anyway
1611                  */
1612                 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1613                         ((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
1614
1615                 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1616                          ((u32)(srb->cmnd[8]) <<  8) | ((u32)(srb->cmnd[9]));
1617
1618                 US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx  count %ld\n", block, blocks);
1619                 return usbat_flash_read_data(us, info, block, blocks);
1620         }
1621
1622         if (srb->cmnd[0] == WRITE_10) {
1623                 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1624                         ((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
1625
1626                 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1627
1628                 US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx  count %ld\n", block, blocks);
1629                 return usbat_flash_write_data(us, info, block, blocks);
1630         }
1631
1632         if (srb->cmnd[0] == WRITE_12) {
1633                 /*
1634                  * I don't think we'll ever see a WRITE_12 but support it anyway
1635                  */
1636                 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1637                         ((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
1638
1639                 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1640                          ((u32)(srb->cmnd[8]) <<  8) | ((u32)(srb->cmnd[9]));
1641
1642                 US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx  count %ld\n", block, blocks);
1643                 return usbat_flash_write_data(us, info, block, blocks);
1644         }
1645
1646
1647         if (srb->cmnd[0] == TEST_UNIT_READY) {
1648                 US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
1649
1650                 rc = usbat_flash_check_media(us, info);
1651                 if (rc != USB_STOR_TRANSPORT_GOOD)
1652                         return rc;
1653
1654                 return usbat_check_status(us);
1655         }
1656
1657         if (srb->cmnd[0] == REQUEST_SENSE) {
1658                 US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
1659
1660                 memset(ptr, 0, 18);
1661                 ptr[0] = 0xF0;
1662                 ptr[2] = info->sense_key;
1663                 ptr[7] = 11;
1664                 ptr[12] = info->sense_asc;
1665                 ptr[13] = info->sense_ascq;
1666                 usb_stor_set_xfer_buf(ptr, 18, srb);
1667
1668                 return USB_STOR_TRANSPORT_GOOD;
1669         }
1670
1671         if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1672                 /*
1673                  * sure.  whatever.  not like we can stop the user from popping
1674                  * the media out of the device (no locking doors, etc)
1675                  */
1676                 return USB_STOR_TRANSPORT_GOOD;
1677         }
1678
1679         US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1680                           srb->cmnd[0], srb->cmnd[0]);
1681         info->sense_key = 0x05;
1682         info->sense_asc = 0x20;
1683         info->sense_ascq = 0x00;
1684         return USB_STOR_TRANSPORT_FAILED;
1685 }
1686
1687 int init_usbat_cd(struct us_data *us)
1688 {
1689         return init_usbat(us, USBAT_DEV_HP8200);
1690 }
1691
1692
1693 int init_usbat_flash(struct us_data *us)
1694 {
1695         return init_usbat(us, USBAT_DEV_FLASH);
1696 }
1697
1698 int init_usbat_probe(struct us_data *us)
1699 {
1700         return init_usbat(us, 0);
1701 }
1702
1703 /*
1704  * Default transport function. Attempts to detect which transport function
1705  * should be called, makes it the new default, and calls it.
1706  *
1707  * This function should never be called. Our usbat_init() function detects the
1708  * device type and changes the us->transport ptr to the transport function
1709  * relevant to the device.
1710  * However, we'll support this impossible(?) case anyway.
1711  */
1712 int usbat_transport(struct scsi_cmnd *srb, struct us_data *us)
1713 {
1714         struct usbat_info *info = (struct usbat_info*) (us->extra);
1715
1716         if (usbat_set_transport(us, info, 0))
1717                 return USB_STOR_TRANSPORT_ERROR;
1718
1719         return us->transport(srb, us);  
1720 }