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