Merge git://git.infradead.org/~dwmw2/rbtree-2.6
[linux-2.6] / drivers / usb / storage / shuttle_usbat.c
1 /* Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable
2  *
3  * $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 {
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_buf(us, us->recv_bulk_pipe, data, len, 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 {
151         if (len == 0)
152                 return USB_STOR_XFER_GOOD;
153
154         US_DEBUGP("usbat_bulk_write:  len = %d\n", len);
155         return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, data, len, NULL);
156 }
157
158 /*
159  * Some USBAT-specific commands can only be executed over a command transport
160  * This transport allows one (len=8) or two (len=16) vendor-specific commands
161  * to be executed.
162  */
163 static int usbat_execute_command(struct us_data *us,
164                                                                  unsigned char *commands,
165                                                                  unsigned int len)
166 {
167         return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
168                                                                   USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
169                                                                   commands, len);
170 }
171
172 /*
173  * Read the status register
174  */
175 static int usbat_get_status(struct us_data *us, unsigned char *status)
176 {
177         int rc;
178         rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
179
180         US_DEBUGP("usbat_get_status: 0x%02X\n", (unsigned short) (*status));
181         return rc;
182 }
183
184 /*
185  * Check the device status
186  */
187 static int usbat_check_status(struct us_data *us)
188 {
189         unsigned char *reply = us->iobuf;
190         int rc;
191
192         if (!us)
193                 return USB_STOR_TRANSPORT_ERROR;
194
195         rc = usbat_get_status(us, reply);
196         if (rc != USB_STOR_XFER_GOOD)
197                 return USB_STOR_TRANSPORT_FAILED;
198
199         /* error/check condition (0x51 is ok) */
200         if (*reply & 0x01 && *reply != 0x51)
201                 return USB_STOR_TRANSPORT_FAILED;
202
203         /* device fault */
204         if (*reply & 0x20)
205                 return USB_STOR_TRANSPORT_FAILED;
206
207         return USB_STOR_TRANSPORT_GOOD;
208 }
209
210 /*
211  * Stores critical information in internal registers in prepartion for the execution
212  * of a conditional usbat_read_blocks or usbat_write_blocks call.
213  */
214 static int usbat_set_shuttle_features(struct us_data *us,
215                                       unsigned char external_trigger,
216                                       unsigned char epp_control,
217                                       unsigned char mask_byte,
218                                       unsigned char test_pattern,
219                                       unsigned char subcountH,
220                                       unsigned char subcountL)
221 {
222         unsigned char *command = us->iobuf;
223
224         command[0] = 0x40;
225         command[1] = USBAT_CMD_SET_FEAT;
226
227         /*
228          * The only bit relevant to ATA access is bit 6
229          * which defines 8 bit data access (set) or 16 bit (unset)
230          */
231         command[2] = epp_control;
232
233         /*
234          * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
235          * ET1 and ET2 define an external event to be checked for on event of a
236          * _read_blocks or _write_blocks operation. The read/write will not take
237          * place unless the defined trigger signal is active.
238          */
239         command[3] = external_trigger;
240
241         /*
242          * The resultant byte of the mask operation (see mask_byte) is compared for
243          * equivalence with this test pattern. If equal, the read/write will take
244          * place.
245          */
246         command[4] = test_pattern;
247
248         /*
249          * This value is logically ANDed with the status register field specified
250          * in the read/write command.
251          */
252         command[5] = mask_byte;
253
254         /*
255          * If ALQ is set in the qualifier, this field contains the address of the
256          * registers where the byte count should be read for transferring the data.
257          * If ALQ is not set, then this field contains the number of bytes to be
258          * transferred.
259          */
260         command[6] = subcountL;
261         command[7] = subcountH;
262
263         return usbat_execute_command(us, command, 8);
264 }
265
266 /*
267  * Block, waiting for an ATA device to become not busy or to report
268  * an error condition.
269  */
270 static int usbat_wait_not_busy(struct us_data *us, int minutes)
271 {
272         int i;
273         int result;
274         unsigned char *status = us->iobuf;
275
276         /* Synchronizing cache on a CDR could take a heck of a long time,
277          * but probably not more than 10 minutes or so. On the other hand,
278          * doing a full blank on a CDRW at speed 1 will take about 75
279          * minutes!
280          */
281
282         for (i=0; i<1200+minutes*60; i++) {
283
284                 result = usbat_get_status(us, status);
285
286                 if (result!=USB_STOR_XFER_GOOD)
287                         return USB_STOR_TRANSPORT_ERROR;
288                 if (*status & 0x01) { /* check condition */
289                         result = usbat_read(us, USBAT_ATA, 0x10, status);
290                         return USB_STOR_TRANSPORT_FAILED;
291                 }
292                 if (*status & 0x20) /* device fault */
293                         return USB_STOR_TRANSPORT_FAILED;
294
295                 if ((*status & 0x80)==0x00) { /* not busy */
296                         US_DEBUGP("Waited not busy for %d steps\n", i);
297                         return USB_STOR_TRANSPORT_GOOD;
298                 }
299
300                 if (i<500)
301                         msleep(10); /* 5 seconds */
302                 else if (i<700)
303                         msleep(50); /* 10 seconds */
304                 else if (i<1200)
305                         msleep(100); /* 50 seconds */
306                 else
307                         msleep(1000); /* X minutes */
308         }
309
310         US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
311                 minutes);
312         return USB_STOR_TRANSPORT_FAILED;
313 }
314
315 /*
316  * Read block data from the data register
317  */
318 static int usbat_read_block(struct us_data *us,
319                             unsigned char *content,
320                             unsigned short len)
321 {
322         int result;
323         unsigned char *command = us->iobuf;
324
325         if (!len)
326                 return USB_STOR_TRANSPORT_GOOD;
327
328         command[0] = 0xC0;
329         command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
330         command[2] = USBAT_ATA_DATA;
331         command[3] = 0;
332         command[4] = 0;
333         command[5] = 0;
334         command[6] = LSB_of(len);
335         command[7] = MSB_of(len);
336
337         result = usbat_execute_command(us, command, 8);
338         if (result != USB_STOR_XFER_GOOD)
339                 return USB_STOR_TRANSPORT_ERROR;
340
341         result = usbat_bulk_read(us, content, len);
342         return (result == USB_STOR_XFER_GOOD ?
343                         USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
344 }
345
346 /*
347  * Write block data via the data register
348  */
349 static int usbat_write_block(struct us_data *us,
350                              unsigned char access,
351                              unsigned char *content,
352                              unsigned short len,
353                              int minutes)
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);
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);
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);
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 {
612         int result;
613         unsigned char *command = us->iobuf;
614
615         command[0] = 0xC0;
616         command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK;
617         command[2] = USBAT_ATA_DATA;
618         command[3] = USBAT_ATA_STATUS;
619         command[4] = 0xFD; /* Timeout (ms); */
620         command[5] = USBAT_QUAL_FCQ;
621         command[6] = LSB_of(len);
622         command[7] = MSB_of(len);
623
624         /* Multiple block read setup command */
625         result = usbat_execute_command(us, command, 8);
626         if (result != USB_STOR_XFER_GOOD)
627                 return USB_STOR_TRANSPORT_FAILED;
628         
629         /* Read the blocks we just asked for */
630         result = usbat_bulk_read(us, buffer, len);
631         if (result != USB_STOR_XFER_GOOD)
632                 return USB_STOR_TRANSPORT_FAILED;
633
634         return USB_STOR_TRANSPORT_GOOD;
635 }
636
637 /*
638  * Conditionally write blocks to device:
639  * Allows us to write blocks to a specific data register, based upon the
640  * condition that a status register can be successfully masked with a status
641  * qualifier. If this condition is not initially met, the write will wait
642  * up until a maximum amount of time has elapsed, as specified by timeout.
643  * The read will start when the condition is met, otherwise the command aborts.
644  *
645  * The qualifier defined here is not the value that is masked, it defines
646  * conditions for the write to take place. The actual masked qualifier (and
647  * other related details) are defined beforehand with _set_shuttle_features().
648  */
649 static int usbat_write_blocks(struct us_data *us,
650                                                           unsigned char *buffer,
651                                                           int len)
652 {
653         int result;
654         unsigned char *command = us->iobuf;
655
656         command[0] = 0x40;
657         command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK;
658         command[2] = USBAT_ATA_DATA;
659         command[3] = USBAT_ATA_STATUS;
660         command[4] = 0xFD; /* Timeout (ms) */
661         command[5] = USBAT_QUAL_FCQ;
662         command[6] = LSB_of(len);
663         command[7] = MSB_of(len);
664
665         /* Multiple block write setup command */
666         result = usbat_execute_command(us, command, 8);
667         if (result != USB_STOR_XFER_GOOD)
668                 return USB_STOR_TRANSPORT_FAILED;
669         
670         /* Write the data */
671         result = usbat_bulk_write(us, buffer, len);
672         if (result != USB_STOR_XFER_GOOD)
673                 return USB_STOR_TRANSPORT_FAILED;
674
675         return USB_STOR_TRANSPORT_GOOD;
676 }
677
678 /*
679  * Read the User IO register
680  */
681 static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
682 {
683         int result;
684
685         result = usb_stor_ctrl_transfer(us,
686                 us->recv_ctrl_pipe,
687                 USBAT_CMD_UIO,
688                 0xC0,
689                 0,
690                 0,
691                 data_flags,
692                 USBAT_UIO_READ);
693
694         US_DEBUGP("usbat_read_user_io: UIO register reads %02X\n", (unsigned short) (*data_flags));
695
696         return result;
697 }
698
699 /*
700  * Write to the User IO register
701  */
702 static int usbat_write_user_io(struct us_data *us,
703                                unsigned char enable_flags,
704                                unsigned char data_flags)
705 {
706         return usb_stor_ctrl_transfer(us,
707                 us->send_ctrl_pipe,
708                 USBAT_CMD_UIO,
709                 0x40,
710                 short_pack(enable_flags, data_flags),
711                 0,
712                 NULL,
713                 USBAT_UIO_WRITE);
714 }
715
716 /*
717  * Reset the device
718  * Often needed on media change.
719  */
720 static int usbat_device_reset(struct us_data *us)
721 {
722         int rc;
723
724         /*
725          * Reset peripheral, enable peripheral control signals
726          * (bring reset signal up)
727          */
728         rc = usbat_write_user_io(us,
729                                                          USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
730                                                          USBAT_UIO_EPAD | USBAT_UIO_1);
731         if (rc != USB_STOR_XFER_GOOD)
732                 return USB_STOR_TRANSPORT_ERROR;
733                         
734         /*
735          * Enable peripheral control signals
736          * (bring reset signal down)
737          */
738         rc = usbat_write_user_io(us,
739                                                          USBAT_UIO_OE1  | USBAT_UIO_OE0,
740                                                          USBAT_UIO_EPAD | USBAT_UIO_1);
741         if (rc != USB_STOR_XFER_GOOD)
742                 return USB_STOR_TRANSPORT_ERROR;
743
744         return USB_STOR_TRANSPORT_GOOD;
745 }
746
747 /*
748  * Enable card detect
749  */
750 static int usbat_device_enable_cdt(struct us_data *us)
751 {
752         int rc;
753
754         /* Enable peripheral control signals and card detect */
755         rc = usbat_write_user_io(us,
756                                                          USBAT_UIO_ACKD | USBAT_UIO_OE1  | USBAT_UIO_OE0,
757                                                          USBAT_UIO_EPAD | USBAT_UIO_1);
758         if (rc != USB_STOR_XFER_GOOD)
759                 return USB_STOR_TRANSPORT_ERROR;
760
761         return USB_STOR_TRANSPORT_GOOD;
762 }
763
764 /*
765  * Determine if media is present.
766  */
767 static int usbat_flash_check_media_present(unsigned char *uio)
768 {
769         if (*uio & USBAT_UIO_UI0) {
770                 US_DEBUGP("usbat_flash_check_media_present: no media detected\n");
771                 return USBAT_FLASH_MEDIA_NONE;
772         }
773
774         return USBAT_FLASH_MEDIA_CF;
775 }
776
777 /*
778  * Determine if media has changed since last operation
779  */
780 static int usbat_flash_check_media_changed(unsigned char *uio)
781 {
782         if (*uio & USBAT_UIO_0) {
783                 US_DEBUGP("usbat_flash_check_media_changed: media change detected\n");
784                 return USBAT_FLASH_MEDIA_CHANGED;
785         }
786
787         return USBAT_FLASH_MEDIA_SAME;
788 }
789
790 /*
791  * Check for media change / no media and handle the situation appropriately
792  */
793 static int usbat_flash_check_media(struct us_data *us,
794                                    struct usbat_info *info)
795 {
796         int rc;
797         unsigned char *uio = us->iobuf;
798
799         rc = usbat_read_user_io(us, uio);
800         if (rc != USB_STOR_XFER_GOOD)
801                 return USB_STOR_TRANSPORT_ERROR;
802
803         /* Check for media existence */
804         rc = usbat_flash_check_media_present(uio);
805         if (rc == USBAT_FLASH_MEDIA_NONE) {
806                 info->sense_key = 0x02;
807                 info->sense_asc = 0x3A;
808                 info->sense_ascq = 0x00;
809                 return USB_STOR_TRANSPORT_FAILED;
810         }
811
812         /* Check for media change */
813         rc = usbat_flash_check_media_changed(uio);
814         if (rc == USBAT_FLASH_MEDIA_CHANGED) {
815
816                 /* Reset and re-enable card detect */
817                 rc = usbat_device_reset(us);
818                 if (rc != USB_STOR_TRANSPORT_GOOD)
819                         return rc;
820                 rc = usbat_device_enable_cdt(us);
821                 if (rc != USB_STOR_TRANSPORT_GOOD)
822                         return rc;
823
824                 msleep(50);
825
826                 rc = usbat_read_user_io(us, uio);
827                 if (rc != USB_STOR_XFER_GOOD)
828                         return USB_STOR_TRANSPORT_ERROR;
829                 
830                 info->sense_key = UNIT_ATTENTION;
831                 info->sense_asc = 0x28;
832                 info->sense_ascq = 0x00;
833                 return USB_STOR_TRANSPORT_FAILED;
834         }
835
836         return USB_STOR_TRANSPORT_GOOD;
837 }
838
839 /*
840  * Determine whether we are controlling a flash-based reader/writer,
841  * or a HP8200-based CD drive.
842  * Sets transport functions as appropriate.
843  */
844 static int usbat_identify_device(struct us_data *us,
845                                  struct usbat_info *info)
846 {
847         int rc;
848         unsigned char status;
849
850         if (!us || !info)
851                 return USB_STOR_TRANSPORT_ERROR;
852
853         rc = usbat_device_reset(us);
854         if (rc != USB_STOR_TRANSPORT_GOOD)
855                 return rc;
856         msleep(500);
857
858         /*
859          * In attempt to distinguish between HP CDRW's and Flash readers, we now
860          * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
861          * readers), this command should fail with error. On ATAPI devices (i.e.
862          * CDROM drives), it should succeed.
863          */
864         rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1);
865         if (rc != USB_STOR_XFER_GOOD)
866                 return USB_STOR_TRANSPORT_ERROR;
867
868         rc = usbat_get_status(us, &status);
869         if (rc != USB_STOR_XFER_GOOD)
870                 return USB_STOR_TRANSPORT_ERROR;
871
872         /* Check for error bit, or if the command 'fell through' */
873         if (status == 0xA1 || !(status & 0x01)) {
874                 /* Device is HP 8200 */
875                 US_DEBUGP("usbat_identify_device: Detected HP8200 CDRW\n");
876                 info->devicetype = USBAT_DEV_HP8200;
877         } else {
878                 /* Device is a CompactFlash reader/writer */
879                 US_DEBUGP("usbat_identify_device: Detected Flash reader/writer\n");
880                 info->devicetype = USBAT_DEV_FLASH;
881         }
882
883         return USB_STOR_TRANSPORT_GOOD;
884 }
885
886 /*
887  * Set the transport function based on the device type
888  */
889 static int usbat_set_transport(struct us_data *us,
890                                struct usbat_info *info)
891 {
892         int rc;
893
894         if (!info->devicetype) {
895                 rc = usbat_identify_device(us, info);
896                 if (rc != USB_STOR_TRANSPORT_GOOD) {
897                         US_DEBUGP("usbat_set_transport: Could not identify device\n");
898                         return 1;
899                 }
900         }
901
902         if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
903                 us->transport = usbat_hp8200e_transport;
904         else if (usbat_get_device_type(us) == USBAT_DEV_FLASH)
905                 us->transport = usbat_flash_transport;
906
907         return 0;
908 }
909
910 /*
911  * Read the media capacity
912  */
913 static int usbat_flash_get_sector_count(struct us_data *us,
914                                         struct usbat_info *info)
915 {
916         unsigned char registers[3] = {
917                 USBAT_ATA_SECCNT,
918                 USBAT_ATA_DEVICE,
919                 USBAT_ATA_CMD,
920         };
921         unsigned char  command[3] = { 0x01, 0xA0, 0xEC };
922         unsigned char *reply;
923         unsigned char status;
924         int rc;
925
926         if (!us || !info)
927                 return USB_STOR_TRANSPORT_ERROR;
928
929         reply = kmalloc(512, GFP_NOIO);
930         if (!reply)
931                 return USB_STOR_TRANSPORT_ERROR;
932
933         /* ATA command : IDENTIFY DEVICE */
934         rc = usbat_multiple_write(us, registers, command, 3);
935         if (rc != USB_STOR_XFER_GOOD) {
936                 US_DEBUGP("usbat_flash_get_sector_count: Gah! identify_device failed\n");
937                 rc = USB_STOR_TRANSPORT_ERROR;
938                 goto leave;
939         }
940
941         /* Read device status */
942         if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
943                 rc = USB_STOR_TRANSPORT_ERROR;
944                 goto leave;
945         }
946
947         msleep(100);
948
949         /* Read the device identification data */
950         rc = usbat_read_block(us, reply, 512);
951         if (rc != USB_STOR_TRANSPORT_GOOD)
952                 goto leave;
953
954         info->sectors = ((u32)(reply[117]) << 24) |
955                 ((u32)(reply[116]) << 16) |
956                 ((u32)(reply[115]) <<  8) |
957                 ((u32)(reply[114])      );
958
959         rc = USB_STOR_TRANSPORT_GOOD;
960
961  leave:
962         kfree(reply);
963         return rc;
964 }
965
966 /*
967  * Read data from device
968  */
969 static int usbat_flash_read_data(struct us_data *us,
970                                                                  struct usbat_info *info,
971                                                                  u32 sector,
972                                                                  u32 sectors)
973 {
974         unsigned char registers[7] = {
975                 USBAT_ATA_FEATURES,
976                 USBAT_ATA_SECCNT,
977                 USBAT_ATA_SECNUM,
978                 USBAT_ATA_LBA_ME,
979                 USBAT_ATA_LBA_HI,
980                 USBAT_ATA_DEVICE,
981                 USBAT_ATA_STATUS,
982         };
983         unsigned char command[7];
984         unsigned char *buffer;
985         unsigned char  thistime;
986         unsigned int totallen, alloclen;
987         int len, result;
988         unsigned int sg_idx = 0, sg_offset = 0;
989
990         result = usbat_flash_check_media(us, info);
991         if (result != USB_STOR_TRANSPORT_GOOD)
992                 return result;
993
994         /*
995          * we're working in LBA mode.  according to the ATA spec,
996          * we can support up to 28-bit addressing.  I don't know if Jumpshot
997          * supports beyond 24-bit addressing.  It's kind of hard to test
998          * since it requires > 8GB CF card.
999          */
1000
1001         if (sector > 0x0FFFFFFF)
1002                 return USB_STOR_TRANSPORT_ERROR;
1003
1004         totallen = sectors * info->ssize;
1005
1006         /*
1007          * Since we don't read more than 64 KB at a time, we have to create
1008          * a bounce buffer and move the data a piece at a time between the
1009          * bounce buffer and the actual transfer buffer.
1010          */
1011
1012         alloclen = min(totallen, 65536u);
1013         buffer = kmalloc(alloclen, GFP_NOIO);
1014         if (buffer == NULL)
1015                 return USB_STOR_TRANSPORT_ERROR;
1016
1017         do {
1018                 /*
1019                  * loop, never allocate or transfer more than 64k at once
1020                  * (min(128k, 255*info->ssize) is the real limit)
1021                  */
1022                 len = min(totallen, alloclen);
1023                 thistime = (len / info->ssize) & 0xff;
1024  
1025                 /* ATA command 0x20 (READ SECTORS) */
1026                 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
1027
1028                 /* Write/execute ATA read command */
1029                 result = usbat_multiple_write(us, registers, command, 7);
1030                 if (result != USB_STOR_TRANSPORT_GOOD)
1031                         goto leave;
1032
1033                 /* Read the data we just requested */
1034                 result = usbat_read_blocks(us, buffer, len);
1035                 if (result != USB_STOR_TRANSPORT_GOOD)
1036                         goto leave;
1037          
1038                 US_DEBUGP("usbat_flash_read_data:  %d bytes\n", len);
1039         
1040                 /* Store the data in the transfer buffer */
1041                 usb_stor_access_xfer_buf(buffer, len, us->srb,
1042                                          &sg_idx, &sg_offset, TO_XFER_BUF);
1043
1044                 sector += thistime;
1045                 totallen -= len;
1046         } while (totallen > 0);
1047
1048         kfree(buffer);
1049         return USB_STOR_TRANSPORT_GOOD;
1050
1051 leave:
1052         kfree(buffer);
1053         return USB_STOR_TRANSPORT_ERROR;
1054 }
1055
1056 /*
1057  * Write data to device
1058  */
1059 static int usbat_flash_write_data(struct us_data *us,
1060                                                                   struct usbat_info *info,
1061                                                                   u32 sector,
1062                                                                   u32 sectors)
1063 {
1064         unsigned char registers[7] = {
1065                 USBAT_ATA_FEATURES,
1066                 USBAT_ATA_SECCNT,
1067                 USBAT_ATA_SECNUM,
1068                 USBAT_ATA_LBA_ME,
1069                 USBAT_ATA_LBA_HI,
1070                 USBAT_ATA_DEVICE,
1071                 USBAT_ATA_STATUS,
1072         };
1073         unsigned char command[7];
1074         unsigned char *buffer;
1075         unsigned char  thistime;
1076         unsigned int totallen, alloclen;
1077         int len, result;
1078         unsigned int sg_idx = 0, sg_offset = 0;
1079
1080         result = usbat_flash_check_media(us, info);
1081         if (result != USB_STOR_TRANSPORT_GOOD)
1082                 return result;
1083
1084         /*
1085          * we're working in LBA mode.  according to the ATA spec,
1086          * we can support up to 28-bit addressing.  I don't know if the device
1087          * supports beyond 24-bit addressing.  It's kind of hard to test
1088          * since it requires > 8GB media.
1089          */
1090
1091         if (sector > 0x0FFFFFFF)
1092                 return USB_STOR_TRANSPORT_ERROR;
1093
1094         totallen = sectors * info->ssize;
1095
1096         /*
1097          * Since we don't write more than 64 KB at a time, we have to create
1098          * a bounce buffer and move the data a piece at a time between the
1099          * bounce buffer and the actual transfer buffer.
1100          */
1101
1102         alloclen = min(totallen, 65536u);
1103         buffer = kmalloc(alloclen, GFP_NOIO);
1104         if (buffer == NULL)
1105                 return USB_STOR_TRANSPORT_ERROR;
1106
1107         do {
1108                 /*
1109                  * loop, never allocate or transfer more than 64k at once
1110                  * (min(128k, 255*info->ssize) is the real limit)
1111                  */
1112                 len = min(totallen, alloclen);
1113                 thistime = (len / info->ssize) & 0xff;
1114
1115                 /* Get the data from the transfer buffer */
1116                 usb_stor_access_xfer_buf(buffer, len, us->srb,
1117                                          &sg_idx, &sg_offset, FROM_XFER_BUF);
1118
1119                 /* ATA command 0x30 (WRITE SECTORS) */
1120                 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
1121
1122                 /* Write/execute ATA write command */
1123                 result = usbat_multiple_write(us, registers, command, 7);
1124                 if (result != USB_STOR_TRANSPORT_GOOD)
1125                         goto leave;
1126
1127                 /* Write the data */
1128                 result = usbat_write_blocks(us, buffer, len);
1129                 if (result != USB_STOR_TRANSPORT_GOOD)
1130                         goto leave;
1131
1132                 sector += thistime;
1133                 totallen -= len;
1134         } while (totallen > 0);
1135
1136         kfree(buffer);
1137         return result;
1138
1139 leave:
1140         kfree(buffer);
1141         return USB_STOR_TRANSPORT_ERROR;
1142 }
1143
1144 /*
1145  * Squeeze a potentially huge (> 65535 byte) read10 command into
1146  * a little ( <= 65535 byte) ATAPI pipe
1147  */
1148 static int usbat_hp8200e_handle_read10(struct us_data *us,
1149                                        unsigned char *registers,
1150                                        unsigned char *data,
1151                                        struct scsi_cmnd *srb)
1152 {
1153         int result = USB_STOR_TRANSPORT_GOOD;
1154         unsigned char *buffer;
1155         unsigned int len;
1156         unsigned int sector;
1157         unsigned int sg_segment = 0;
1158         unsigned int sg_offset = 0;
1159
1160         US_DEBUGP("handle_read10: transfersize %d\n",
1161                 srb->transfersize);
1162
1163         if (srb->request_bufflen < 0x10000) {
1164
1165                 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA, 
1166                         registers, data, 19,
1167                         USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1168                         (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1169                         DMA_FROM_DEVICE,
1170                         srb->request_buffer, 
1171                         srb->request_bufflen, srb->use_sg, 1);
1172
1173                 return result;
1174         }
1175
1176         /*
1177          * Since we're requesting more data than we can handle in
1178          * a single read command (max is 64k-1), we will perform
1179          * multiple reads, but each read must be in multiples of
1180          * a sector.  Luckily the sector size is in srb->transfersize
1181          * (see linux/drivers/scsi/sr.c).
1182          */
1183
1184         if (data[7+0] == GPCMD_READ_CD) {
1185                 len = short_pack(data[7+9], data[7+8]);
1186                 len <<= 16;
1187                 len |= data[7+7];
1188                 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
1189                 srb->transfersize = srb->request_bufflen/len;
1190         }
1191
1192         if (!srb->transfersize)  {
1193                 srb->transfersize = 2048; /* A guess */
1194                 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
1195                         srb->transfersize);
1196         }
1197
1198         /*
1199          * Since we only read in one block at a time, we have to create
1200          * a bounce buffer and move the data a piece at a time between the
1201          * bounce buffer and the actual transfer buffer.
1202          */
1203
1204         len = (65535/srb->transfersize) * srb->transfersize;
1205         US_DEBUGP("Max read is %d bytes\n", len);
1206         len = min(len, srb->request_bufflen);
1207         buffer = kmalloc(len, GFP_NOIO);
1208         if (buffer == NULL) /* bloody hell! */
1209                 return USB_STOR_TRANSPORT_FAILED;
1210         sector = short_pack(data[7+3], data[7+2]);
1211         sector <<= 16;
1212         sector |= short_pack(data[7+5], data[7+4]);
1213         transferred = 0;
1214
1215         sg_segment = 0; /* for keeping track of where we are in */
1216         sg_offset = 0;  /* the scatter/gather list */
1217
1218         while (transferred != srb->request_bufflen) {
1219
1220                 if (len > srb->request_bufflen - transferred)
1221                         len = srb->request_bufflen - transferred;
1222
1223                 data[3] = len&0xFF;       /* (cylL) = expected length (L) */
1224                 data[4] = (len>>8)&0xFF;  /* (cylH) = expected length (H) */
1225
1226                 /* Fix up the SCSI command sector and num sectors */
1227
1228                 data[7+2] = MSB_of(sector>>16); /* SCSI command sector */
1229                 data[7+3] = LSB_of(sector>>16);
1230                 data[7+4] = MSB_of(sector&0xFFFF);
1231                 data[7+5] = LSB_of(sector&0xFFFF);
1232                 if (data[7+0] == GPCMD_READ_CD)
1233                         data[7+6] = 0;
1234                 data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */
1235                 data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */
1236
1237                 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA, 
1238                         registers, data, 19,
1239                         USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD, 
1240                         (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1241                         DMA_FROM_DEVICE,
1242                         buffer,
1243                         len, 0, 1);
1244
1245                 if (result != USB_STOR_TRANSPORT_GOOD)
1246                         break;
1247
1248                 /* Store the data in the transfer buffer */
1249                 usb_stor_access_xfer_buf(buffer, len, srb,
1250                                  &sg_segment, &sg_offset, TO_XFER_BUF);
1251
1252                 /* Update the amount transferred and the sector number */
1253
1254                 transferred += len;
1255                 sector += len / srb->transfersize;
1256
1257         } /* while transferred != srb->request_bufflen */
1258
1259         kfree(buffer);
1260         return result;
1261 }
1262
1263 static int usbat_select_and_test_registers(struct us_data *us)
1264 {
1265         int selector;
1266         unsigned char *status = us->iobuf;
1267
1268         /* try device = master, then device = slave. */
1269         for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
1270                 if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
1271                                 USB_STOR_XFER_GOOD)
1272                         return USB_STOR_TRANSPORT_ERROR;
1273
1274                 if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) != 
1275                                 USB_STOR_XFER_GOOD)
1276                         return USB_STOR_TRANSPORT_ERROR;
1277
1278                 if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) != 
1279                                 USB_STOR_XFER_GOOD)
1280                         return USB_STOR_TRANSPORT_ERROR;
1281
1282                 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != 
1283                                 USB_STOR_XFER_GOOD)
1284                         return USB_STOR_TRANSPORT_ERROR;
1285
1286                 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) != 
1287                                 USB_STOR_XFER_GOOD)
1288                         return USB_STOR_TRANSPORT_ERROR;
1289
1290                 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) != 
1291                                 USB_STOR_XFER_GOOD)
1292                         return USB_STOR_TRANSPORT_ERROR;
1293
1294                 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) != 
1295                                 USB_STOR_XFER_GOOD)
1296                         return USB_STOR_TRANSPORT_ERROR;
1297
1298                 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != 
1299                                 USB_STOR_XFER_GOOD)
1300                         return USB_STOR_TRANSPORT_ERROR;
1301
1302                 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != 
1303                                 USB_STOR_XFER_GOOD)
1304                         return USB_STOR_TRANSPORT_ERROR;
1305         }
1306
1307         return USB_STOR_TRANSPORT_GOOD;
1308 }
1309
1310 /*
1311  * Initialize the USBAT processor and the storage device
1312  */
1313 int init_usbat(struct us_data *us)
1314 {
1315         int rc;
1316         struct usbat_info *info;
1317         unsigned char subcountH = USBAT_ATA_LBA_HI;
1318         unsigned char subcountL = USBAT_ATA_LBA_ME;
1319         unsigned char *status = us->iobuf;
1320
1321         us->extra = kzalloc(sizeof(struct usbat_info), GFP_NOIO);
1322         if (!us->extra) {
1323                 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1324                 return 1;
1325         }
1326         info = (struct usbat_info *) (us->extra);
1327
1328         /* Enable peripheral control signals */
1329         rc = usbat_write_user_io(us,
1330                                  USBAT_UIO_OE1 | USBAT_UIO_OE0,
1331                                  USBAT_UIO_EPAD | USBAT_UIO_1);
1332         if (rc != USB_STOR_XFER_GOOD)
1333                 return USB_STOR_TRANSPORT_ERROR;
1334
1335         US_DEBUGP("INIT 1\n");
1336
1337         msleep(2000);
1338
1339         rc = usbat_read_user_io(us, status);
1340         if (rc != USB_STOR_TRANSPORT_GOOD)
1341                 return rc;
1342
1343         US_DEBUGP("INIT 2\n");
1344
1345         rc = usbat_read_user_io(us, status);
1346         if (rc != USB_STOR_XFER_GOOD)
1347                 return USB_STOR_TRANSPORT_ERROR;
1348
1349         rc = usbat_read_user_io(us, status);
1350         if (rc != USB_STOR_XFER_GOOD)
1351                 return USB_STOR_TRANSPORT_ERROR;
1352
1353         US_DEBUGP("INIT 3\n");
1354
1355         rc = usbat_select_and_test_registers(us);
1356         if (rc != USB_STOR_TRANSPORT_GOOD)
1357                 return rc;
1358
1359         US_DEBUGP("INIT 4\n");
1360
1361         rc = usbat_read_user_io(us, status);
1362         if (rc != USB_STOR_XFER_GOOD)
1363                 return USB_STOR_TRANSPORT_ERROR;
1364
1365         US_DEBUGP("INIT 5\n");
1366
1367         /* Enable peripheral control signals and card detect */
1368         rc = usbat_device_enable_cdt(us);
1369         if (rc != USB_STOR_TRANSPORT_GOOD)
1370                 return rc;
1371
1372         US_DEBUGP("INIT 6\n");
1373
1374         rc = usbat_read_user_io(us, status);
1375         if (rc != USB_STOR_XFER_GOOD)
1376                 return USB_STOR_TRANSPORT_ERROR;
1377
1378         US_DEBUGP("INIT 7\n");
1379
1380         msleep(1400);
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 8\n");
1387
1388         rc = usbat_select_and_test_registers(us);
1389         if (rc != USB_STOR_TRANSPORT_GOOD)
1390                 return rc;
1391
1392         US_DEBUGP("INIT 9\n");
1393
1394         /* At this point, we need to detect which device we are using */
1395         if (usbat_set_transport(us, info))
1396                 return USB_STOR_TRANSPORT_ERROR;
1397
1398         US_DEBUGP("INIT 10\n");
1399
1400         if (usbat_get_device_type(us) == USBAT_DEV_FLASH) { 
1401                 subcountH = 0x02;
1402                 subcountL = 0x00;
1403         }
1404         rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1405                                                                         0x00, 0x88, 0x08, subcountH, subcountL);
1406         if (rc != USB_STOR_XFER_GOOD)
1407                 return USB_STOR_TRANSPORT_ERROR;
1408
1409         US_DEBUGP("INIT 11\n");
1410
1411         return USB_STOR_TRANSPORT_GOOD;
1412 }
1413
1414 /*
1415  * Transport for the HP 8200e
1416  */
1417 static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1418 {
1419         int result;
1420         unsigned char *status = us->iobuf;
1421         unsigned char registers[32];
1422         unsigned char data[32];
1423         unsigned int len;
1424         int i;
1425         char string[64];
1426
1427         len = srb->request_bufflen;
1428
1429         /* Send A0 (ATA PACKET COMMAND).
1430            Note: I guess we're never going to get any of the ATA
1431            commands... just ATA Packet Commands.
1432          */
1433
1434         registers[0] = USBAT_ATA_FEATURES;
1435         registers[1] = USBAT_ATA_SECCNT;
1436         registers[2] = USBAT_ATA_SECNUM;
1437         registers[3] = USBAT_ATA_LBA_ME;
1438         registers[4] = USBAT_ATA_LBA_HI;
1439         registers[5] = USBAT_ATA_DEVICE;
1440         registers[6] = USBAT_ATA_CMD;
1441         data[0] = 0x00;
1442         data[1] = 0x00;
1443         data[2] = 0x00;
1444         data[3] = len&0xFF;             /* (cylL) = expected length (L) */
1445         data[4] = (len>>8)&0xFF;        /* (cylH) = expected length (H) */
1446         data[5] = 0xB0;                 /* (device sel) = slave */
1447         data[6] = 0xA0;                 /* (command) = ATA PACKET COMMAND */
1448
1449         for (i=7; i<19; i++) {
1450                 registers[i] = 0x10;
1451                 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1452         }
1453
1454         result = usbat_get_status(us, status);
1455         US_DEBUGP("Status = %02X\n", *status);
1456         if (result != USB_STOR_XFER_GOOD)
1457                 return USB_STOR_TRANSPORT_ERROR;
1458         if (srb->cmnd[0] == TEST_UNIT_READY)
1459                 transferred = 0;
1460
1461         if (srb->sc_data_direction == DMA_TO_DEVICE) {
1462
1463                 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA, 
1464                         registers, data, 19,
1465                         USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1466                         (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1467                         DMA_TO_DEVICE,
1468                         srb->request_buffer, 
1469                         len, srb->use_sg, 10);
1470
1471                 if (result == USB_STOR_TRANSPORT_GOOD) {
1472                         transferred += len;
1473                         US_DEBUGP("Wrote %08X bytes\n", transferred);
1474                 }
1475
1476                 return result;
1477
1478         } else if (srb->cmnd[0] == READ_10 ||
1479                    srb->cmnd[0] == GPCMD_READ_CD) {
1480
1481                 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1482
1483         }
1484
1485         if (len > 0xFFFF) {
1486                 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1487                         len);
1488                 return USB_STOR_TRANSPORT_ERROR;
1489         }
1490
1491         if ( (result = usbat_multiple_write(us, 
1492                         registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
1493                 return result;
1494         }
1495
1496         /*
1497          * Write the 12-byte command header.
1498          *
1499          * If the command is BLANK then set the timer for 75 minutes.
1500          * Otherwise set it for 10 minutes.
1501          *
1502          * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1503          * AT SPEED 4 IS UNRELIABLE!!!
1504          */
1505
1506         if ( (result = usbat_write_block(us, 
1507                         USBAT_ATA, srb->cmnd, 12,
1508                         srb->cmnd[0]==GPCMD_BLANK ? 75 : 10)) !=
1509                                 USB_STOR_TRANSPORT_GOOD) {
1510                 return result;
1511         }
1512
1513         /* If there is response data to be read in then do it here. */
1514
1515         if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1516
1517                 /* How many bytes to read in? Check cylL register */
1518
1519                 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != 
1520                         USB_STOR_XFER_GOOD) {
1521                         return USB_STOR_TRANSPORT_ERROR;
1522                 }
1523
1524                 if (len > 0xFF) { /* need to read cylH also */
1525                         len = *status;
1526                         if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1527                                     USB_STOR_XFER_GOOD) {
1528                                 return USB_STOR_TRANSPORT_ERROR;
1529                         }
1530                         len += ((unsigned int) *status)<<8;
1531                 }
1532                 else
1533                         len = *status;
1534
1535
1536                 result = usbat_read_block(us, srb->request_buffer, len);
1537
1538                 /* Debug-print the first 32 bytes of the transfer */
1539
1540                 if (!srb->use_sg) {
1541                         string[0] = 0;
1542                         for (i=0; i<len && i<32; i++) {
1543                                 sprintf(string+strlen(string), "%02X ",
1544                                   ((unsigned char *)srb->request_buffer)[i]);
1545                                 if ((i%16)==15) {
1546                                         US_DEBUGP("%s\n", string);
1547                                         string[0] = 0;
1548                                 }
1549                         }
1550                         if (string[0]!=0)
1551                                 US_DEBUGP("%s\n", string);
1552                 }
1553         }
1554
1555         return result;
1556 }
1557
1558 /*
1559  * Transport for USBAT02-based CompactFlash and similar storage devices
1560  */
1561 static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1562 {
1563         int rc;
1564         struct usbat_info *info = (struct usbat_info *) (us->extra);
1565         unsigned long block, blocks;
1566         unsigned char *ptr = us->iobuf;
1567         static unsigned char inquiry_response[36] = {
1568                 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1569         };
1570
1571         if (srb->cmnd[0] == INQUIRY) {
1572                 US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1573                 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1574                 fill_inquiry_response(us, ptr, 36);
1575                 return USB_STOR_TRANSPORT_GOOD;
1576         }
1577
1578         if (srb->cmnd[0] == READ_CAPACITY) {
1579                 rc = usbat_flash_check_media(us, info);
1580                 if (rc != USB_STOR_TRANSPORT_GOOD)
1581                         return rc;
1582
1583                 rc = usbat_flash_get_sector_count(us, info);
1584                 if (rc != USB_STOR_TRANSPORT_GOOD)
1585                         return rc;
1586
1587                 /* hard coded 512 byte sectors as per ATA spec */
1588                 info->ssize = 0x200;
1589                 US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1590                           info->sectors, info->ssize);
1591
1592                 /*
1593                  * build the reply
1594                  * note: must return the sector number of the last sector,
1595                  * *not* the total number of sectors
1596                  */
1597                 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1598                 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1599                 usb_stor_set_xfer_buf(ptr, 8, srb);
1600
1601                 return USB_STOR_TRANSPORT_GOOD;
1602         }
1603
1604         if (srb->cmnd[0] == MODE_SELECT_10) {
1605                 US_DEBUGP("usbat_flash_transport:  Gah! MODE_SELECT_10.\n");
1606                 return USB_STOR_TRANSPORT_ERROR;
1607         }
1608
1609         if (srb->cmnd[0] == READ_10) {
1610                 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1611                                 ((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
1612
1613                 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1614
1615                 US_DEBUGP("usbat_flash_transport:  READ_10: read block 0x%04lx  count %ld\n", block, blocks);
1616                 return usbat_flash_read_data(us, info, block, blocks);
1617         }
1618
1619         if (srb->cmnd[0] == READ_12) {
1620                 /*
1621                  * I don't think we'll ever see a READ_12 but support it anyway
1622                  */
1623                 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1624                         ((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
1625
1626                 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1627                          ((u32)(srb->cmnd[8]) <<  8) | ((u32)(srb->cmnd[9]));
1628
1629                 US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx  count %ld\n", block, blocks);
1630                 return usbat_flash_read_data(us, info, block, blocks);
1631         }
1632
1633         if (srb->cmnd[0] == WRITE_10) {
1634                 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1635                         ((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
1636
1637                 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1638
1639                 US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx  count %ld\n", block, blocks);
1640                 return usbat_flash_write_data(us, info, block, blocks);
1641         }
1642
1643         if (srb->cmnd[0] == WRITE_12) {
1644                 /*
1645                  * I don't think we'll ever see a WRITE_12 but support it anyway
1646                  */
1647                 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1648                         ((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
1649
1650                 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1651                          ((u32)(srb->cmnd[8]) <<  8) | ((u32)(srb->cmnd[9]));
1652
1653                 US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx  count %ld\n", block, blocks);
1654                 return usbat_flash_write_data(us, info, block, blocks);
1655         }
1656
1657
1658         if (srb->cmnd[0] == TEST_UNIT_READY) {
1659                 US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
1660
1661                 rc = usbat_flash_check_media(us, info);
1662                 if (rc != USB_STOR_TRANSPORT_GOOD)
1663                         return rc;
1664
1665                 return usbat_check_status(us);
1666         }
1667
1668         if (srb->cmnd[0] == REQUEST_SENSE) {
1669                 US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
1670
1671                 memset(ptr, 0, 18);
1672                 ptr[0] = 0xF0;
1673                 ptr[2] = info->sense_key;
1674                 ptr[7] = 11;
1675                 ptr[12] = info->sense_asc;
1676                 ptr[13] = info->sense_ascq;
1677                 usb_stor_set_xfer_buf(ptr, 18, srb);
1678
1679                 return USB_STOR_TRANSPORT_GOOD;
1680         }
1681
1682         if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1683                 /*
1684                  * sure.  whatever.  not like we can stop the user from popping
1685                  * the media out of the device (no locking doors, etc)
1686                  */
1687                 return USB_STOR_TRANSPORT_GOOD;
1688         }
1689
1690         US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1691                           srb->cmnd[0], srb->cmnd[0]);
1692         info->sense_key = 0x05;
1693         info->sense_asc = 0x20;
1694         info->sense_ascq = 0x00;
1695         return USB_STOR_TRANSPORT_FAILED;
1696 }
1697
1698 /*
1699  * Default transport function. Attempts to detect which transport function
1700  * should be called, makes it the new default, and calls it.
1701  *
1702  * This function should never be called. Our usbat_init() function detects the
1703  * device type and changes the us->transport ptr to the transport function
1704  * relevant to the device.
1705  * However, we'll support this impossible(?) case anyway.
1706  */
1707 int usbat_transport(struct scsi_cmnd *srb, struct us_data *us)
1708 {
1709         struct usbat_info *info = (struct usbat_info*) (us->extra);
1710
1711         if (usbat_set_transport(us, info))
1712                 return USB_STOR_TRANSPORT_ERROR;
1713
1714         return us->transport(srb, us);  
1715 }
1716