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