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