blob: fea176d7e79a3bcb5540df2cc7012944c3f191cc [file] [log] [blame]
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001/* Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
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
64static int transferred = 0;
65
66static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us);
67static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us);
68
69/*
Daniel Drakeb7b1e652005-09-30 12:49:36 +010070 * Convenience function to produce an ATA read/write sectors command
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * Use cmd=0x20 for read, cmd=0x30 for write
72 */
Daniel Drakeb7b1e652005-09-30 12:49:36 +010073static void usbat_pack_ata_sector_cmd(unsigned char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 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 */
89static 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 */
97static 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 */
115static 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 */
133static 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 */
147static 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 */
163static 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 */
175static 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 */
187static 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
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100199 /* error/check condition (0x51 is ok) */
200 if (*reply & 0x01 && *reply != 0x51)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return USB_STOR_TRANSPORT_FAILED;
202
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100203 /* device fault */
204 if (*reply & 0x20)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 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 */
214static 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
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100227 /*
228 * The only bit relevant to ATA access is bit 6
229 * which defines 8 bit data access (set) or 16 bit (unset)
230 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 command[2] = epp_control;
232
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100233 /*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 command[3] = external_trigger;
240
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100241 /*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 command[4] = test_pattern;
247
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100248 /*
249 * This value is logically ANDed with the status register field specified
250 * in the read/write command.
251 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 command[5] = mask_byte;
253
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100254 /*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 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 */
270static 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;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100288 if (*status & 0x01) { /* check condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 result = usbat_read(us, USBAT_ATA, 0x10, status);
290 return USB_STOR_TRANSPORT_FAILED;
291 }
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100292 if (*status & 0x20) /* device fault */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return USB_STOR_TRANSPORT_FAILED;
294
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100295 if ((*status & 0x80)==0x00) { /* not busy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 US_DEBUGP("Waited not busy for %d steps\n", i);
297 return USB_STOR_TRANSPORT_GOOD;
298 }
299
300 if (i<500)
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100301 msleep(10); /* 5 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 else if (i<700)
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100303 msleep(50); /* 10 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 else if (i<1200)
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100305 msleep(100); /* 50 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 else
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100307 msleep(1000); /* X minutes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
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 */
318static 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 */
349static 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 */
385static 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;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100427 /*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 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
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100444 /* Conditionally read or write blocks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 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;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100521 if (*status & 0x01) /* check condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return USB_STOR_TRANSPORT_FAILED;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100523 if (*status & 0x20) /* device fault */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 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 */
549static 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
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100560 /* Write to multiple registers, ATA access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 command[0] = 0x40;
562 command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
563
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100564 /* No relevance */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 command[2] = 0;
566 command[3] = 0;
567 command[4] = 0;
568 command[5] = 0;
569
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100570 /* Number of bytes to be transferred (incl. addresses and data) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 command[6] = LSB_of(num_registers*2);
572 command[7] = MSB_of(num_registers*2);
573
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100574 /* The setup command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 result = usbat_execute_command(us, command, 8);
576 if (result != USB_STOR_XFER_GOOD)
577 return USB_STOR_TRANSPORT_ERROR;
578
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100579 /* Create the reg/data, reg/data sequence */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 for (i=0; i<num_registers; i++) {
581 data[i<<1] = registers[i];
582 data[1+(i<<1)] = data_out[i];
583 }
584
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100585 /* Send the data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 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 */
608static 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;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100619 command[4] = 0xFD; /* Timeout (ms); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 command[5] = USBAT_QUAL_FCQ;
621 command[6] = LSB_of(len);
622 command[7] = MSB_of(len);
623
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100624 /* Multiple block read setup command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 result = usbat_execute_command(us, command, 8);
626 if (result != USB_STOR_XFER_GOOD)
627 return USB_STOR_TRANSPORT_FAILED;
628
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100629 /* Read the blocks we just asked for */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 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 */
649static 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;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100660 command[4] = 0xFD; /* Timeout (ms) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 command[5] = USBAT_QUAL_FCQ;
662 command[6] = LSB_of(len);
663 command[7] = MSB_of(len);
664
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100665 /* Multiple block write setup command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 result = usbat_execute_command(us, command, 8);
667 if (result != USB_STOR_XFER_GOOD)
668 return USB_STOR_TRANSPORT_FAILED;
669
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100670 /* Write the data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 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 */
681static 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 */
702static 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 */
720static int usbat_device_reset(struct us_data *us)
721{
722 int rc;
723
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100724 /*
725 * Reset peripheral, enable peripheral control signals
726 * (bring reset signal up)
727 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 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
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100734 /*
735 * Enable peripheral control signals
736 * (bring reset signal down)
737 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 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 */
750static int usbat_device_enable_cdt(struct us_data *us)
751{
752 int rc;
753
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100754 /* Enable peripheral control signals and card detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 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 */
767static 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 */
780static 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 */
793static 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
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100803 /* Check for media existence */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 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
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100812 /* Check for media change */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 rc = usbat_flash_check_media_changed(uio);
814 if (rc == USBAT_FLASH_MEDIA_CHANGED) {
815
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100816 /* Reset and re-enable card detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 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 */
844static 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;
Daniel Drake8845add2005-11-17 09:48:01 -0800856 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 /*
Daniel Drake68a64572005-08-10 18:30:04 +0100859 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 */
Daniel Drake68a64572005-08-10 18:30:04 +0100864 rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1);
865 if (rc != USB_STOR_XFER_GOOD)
866 return USB_STOR_TRANSPORT_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Daniel Drake68a64572005-08-10 18:30:04 +0100868 rc = usbat_get_status(us, &status);
869 if (rc != USB_STOR_XFER_GOOD)
870 return USB_STOR_TRANSPORT_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100872 /* Check for error bit, or if the command 'fell through' */
Daniel Drakea8798532005-09-29 00:14:21 +0100873 if (status == 0xA1 || !(status & 0x01)) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100874 /* Device is HP 8200 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 US_DEBUGP("usbat_identify_device: Detected HP8200 CDRW\n");
876 info->devicetype = USBAT_DEV_HP8200;
Daniel Drakea8798532005-09-29 00:14:21 +0100877 } else {
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100878 /* Device is a CompactFlash reader/writer */
Daniel Drakea8798532005-09-29 00:14:21 +0100879 US_DEBUGP("usbat_identify_device: Detected Flash reader/writer\n");
880 info->devicetype = USBAT_DEV_FLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882
883 return USB_STOR_TRANSPORT_GOOD;
884}
885
886/*
887 * Set the transport function based on the device type
888 */
889static 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 */
913static 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
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100933 /* ATA command : IDENTIFY DEVICE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 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
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100941 /* Read device status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 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
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100949 /* Read the device identification data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 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 */
969static 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
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100994 /*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 if (sector > 0x0FFFFFFF)
1002 return USB_STOR_TRANSPORT_ERROR;
1003
1004 totallen = sectors * info->ssize;
1005
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001006 /*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 alloclen = min(totallen, 65536u);
1013 buffer = kmalloc(alloclen, GFP_NOIO);
1014 if (buffer == NULL)
1015 return USB_STOR_TRANSPORT_ERROR;
1016
1017 do {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001018 /*
1019 * loop, never allocate or transfer more than 64k at once
1020 * (min(128k, 255*info->ssize) is the real limit)
1021 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 len = min(totallen, alloclen);
1023 thistime = (len / info->ssize) & 0xff;
1024
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001025 /* ATA command 0x20 (READ SECTORS) */
1026 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001028 /* Write/execute ATA read command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 result = usbat_multiple_write(us, registers, command, 7);
1030 if (result != USB_STOR_TRANSPORT_GOOD)
1031 goto leave;
1032
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001033 /* Read the data we just requested */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 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
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001040 /* Store the data in the transfer buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 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
1051leave:
1052 kfree(buffer);
1053 return USB_STOR_TRANSPORT_ERROR;
1054}
1055
1056/*
1057 * Write data to device
1058 */
1059static 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
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001084 /*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 if (sector > 0x0FFFFFFF)
1092 return USB_STOR_TRANSPORT_ERROR;
1093
1094 totallen = sectors * info->ssize;
1095
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001096 /*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 alloclen = min(totallen, 65536u);
1103 buffer = kmalloc(alloclen, GFP_NOIO);
1104 if (buffer == NULL)
1105 return USB_STOR_TRANSPORT_ERROR;
1106
1107 do {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001108 /*
1109 * loop, never allocate or transfer more than 64k at once
1110 * (min(128k, 255*info->ssize) is the real limit)
1111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 len = min(totallen, alloclen);
1113 thistime = (len / info->ssize) & 0xff;
1114
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001115 /* Get the data from the transfer buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 usb_stor_access_xfer_buf(buffer, len, us->srb,
1117 &sg_idx, &sg_offset, FROM_XFER_BUF);
1118
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001119 /* ATA command 0x30 (WRITE SECTORS) */
1120 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001122 /* Write/execute ATA write command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 result = usbat_multiple_write(us, registers, command, 7);
1124 if (result != USB_STOR_TRANSPORT_GOOD)
1125 goto leave;
1126
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001127 /* Write the data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 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
1139leave:
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 */
1148static 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
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001198 /*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
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);
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001208 if (buffer == NULL) /* bloody hell! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 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
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001215 sg_segment = 0; /* for keeping track of where we are in */
1216 sg_offset = 0; /* the scatter/gather list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 while (transferred != srb->request_bufflen) {
1219
1220 if (len > srb->request_bufflen - transferred)
1221 len = srb->request_bufflen - transferred;
1222
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001223 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1224 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001226 /* Fix up the SCSI command sector and num sectors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001228 data[7+2] = MSB_of(sector>>16); /* SCSI command sector */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 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;
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001234 data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */
1235 data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
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
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001248 /* Store the data in the transfer buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 usb_stor_access_xfer_buf(buffer, len, srb,
1250 &sg_segment, &sg_offset, TO_XFER_BUF);
1251
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001252 /* Update the amount transferred and the sector number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 transferred += len;
1255 sector += len / srb->transfersize;
1256
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001257 } /* while transferred != srb->request_bufflen */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 kfree(buffer);
1260 return result;
1261}
1262
1263static int usbat_select_and_test_registers(struct us_data *us)
1264{
1265 int selector;
1266 unsigned char *status = us->iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001268 /* try device = master, then device = slave. */
Daniel Drake68a64572005-08-10 18:30:04 +01001269 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
1270 if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 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 */
1313int 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 = kmalloc(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 memset(us->extra, 0, sizeof(struct usbat_info));
1327 info = (struct usbat_info *) (us->extra);
1328
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001329 /* Enable peripheral control signals */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 rc = usbat_write_user_io(us,
1331 USBAT_UIO_OE1 | USBAT_UIO_OE0,
1332 USBAT_UIO_EPAD | USBAT_UIO_1);
1333 if (rc != USB_STOR_XFER_GOOD)
1334 return USB_STOR_TRANSPORT_ERROR;
1335
1336 US_DEBUGP("INIT 1\n");
1337
1338 msleep(2000);
1339
1340 rc = usbat_read_user_io(us, status);
1341 if (rc != USB_STOR_TRANSPORT_GOOD)
1342 return rc;
1343
1344 US_DEBUGP("INIT 2\n");
1345
1346 rc = usbat_read_user_io(us, status);
1347 if (rc != USB_STOR_XFER_GOOD)
1348 return USB_STOR_TRANSPORT_ERROR;
1349
1350 rc = usbat_read_user_io(us, status);
1351 if (rc != USB_STOR_XFER_GOOD)
1352 return USB_STOR_TRANSPORT_ERROR;
1353
1354 US_DEBUGP("INIT 3\n");
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 rc = usbat_select_and_test_registers(us);
1357 if (rc != USB_STOR_TRANSPORT_GOOD)
1358 return rc;
1359
Daniel Drake68a64572005-08-10 18:30:04 +01001360 US_DEBUGP("INIT 4\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 rc = usbat_read_user_io(us, status);
1363 if (rc != USB_STOR_XFER_GOOD)
1364 return USB_STOR_TRANSPORT_ERROR;
1365
Daniel Drake68a64572005-08-10 18:30:04 +01001366 US_DEBUGP("INIT 5\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001368 /* Enable peripheral control signals and card detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 rc = usbat_device_enable_cdt(us);
1370 if (rc != USB_STOR_TRANSPORT_GOOD)
1371 return rc;
1372
Daniel Drake68a64572005-08-10 18:30:04 +01001373 US_DEBUGP("INIT 6\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 rc = usbat_read_user_io(us, status);
1376 if (rc != USB_STOR_XFER_GOOD)
1377 return USB_STOR_TRANSPORT_ERROR;
1378
Daniel Drake68a64572005-08-10 18:30:04 +01001379 US_DEBUGP("INIT 7\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 msleep(1400);
1382
1383 rc = usbat_read_user_io(us, status);
1384 if (rc != USB_STOR_XFER_GOOD)
1385 return USB_STOR_TRANSPORT_ERROR;
1386
Daniel Drake68a64572005-08-10 18:30:04 +01001387 US_DEBUGP("INIT 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 rc = usbat_select_and_test_registers(us);
1390 if (rc != USB_STOR_TRANSPORT_GOOD)
1391 return rc;
1392
Daniel Drake68a64572005-08-10 18:30:04 +01001393 US_DEBUGP("INIT 9\n");
1394
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001395 /* At this point, we need to detect which device we are using */
Daniel Drake68a64572005-08-10 18:30:04 +01001396 if (usbat_set_transport(us, info))
1397 return USB_STOR_TRANSPORT_ERROR;
1398
1399 US_DEBUGP("INIT 10\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 if (usbat_get_device_type(us) == USBAT_DEV_FLASH) {
1402 subcountH = 0x02;
1403 subcountL = 0x00;
1404 }
1405 rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1406 0x00, 0x88, 0x08, subcountH, subcountL);
1407 if (rc != USB_STOR_XFER_GOOD)
1408 return USB_STOR_TRANSPORT_ERROR;
1409
Daniel Drake68a64572005-08-10 18:30:04 +01001410 US_DEBUGP("INIT 11\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 return USB_STOR_TRANSPORT_GOOD;
1413}
1414
1415/*
1416 * Transport for the HP 8200e
1417 */
1418static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1419{
1420 int result;
1421 unsigned char *status = us->iobuf;
1422 unsigned char registers[32];
1423 unsigned char data[32];
1424 unsigned int len;
1425 int i;
1426 char string[64];
1427
1428 len = srb->request_bufflen;
1429
1430 /* Send A0 (ATA PACKET COMMAND).
1431 Note: I guess we're never going to get any of the ATA
1432 commands... just ATA Packet Commands.
1433 */
1434
1435 registers[0] = USBAT_ATA_FEATURES;
1436 registers[1] = USBAT_ATA_SECCNT;
1437 registers[2] = USBAT_ATA_SECNUM;
1438 registers[3] = USBAT_ATA_LBA_ME;
1439 registers[4] = USBAT_ATA_LBA_HI;
1440 registers[5] = USBAT_ATA_DEVICE;
1441 registers[6] = USBAT_ATA_CMD;
1442 data[0] = 0x00;
1443 data[1] = 0x00;
1444 data[2] = 0x00;
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001445 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1446 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
1447 data[5] = 0xB0; /* (device sel) = slave */
1448 data[6] = 0xA0; /* (command) = ATA PACKET COMMAND */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 for (i=7; i<19; i++) {
1451 registers[i] = 0x10;
1452 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1453 }
1454
1455 result = usbat_get_status(us, status);
1456 US_DEBUGP("Status = %02X\n", *status);
1457 if (result != USB_STOR_XFER_GOOD)
1458 return USB_STOR_TRANSPORT_ERROR;
1459 if (srb->cmnd[0] == TEST_UNIT_READY)
1460 transferred = 0;
1461
1462 if (srb->sc_data_direction == DMA_TO_DEVICE) {
1463
1464 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1465 registers, data, 19,
1466 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1467 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1468 DMA_TO_DEVICE,
1469 srb->request_buffer,
1470 len, srb->use_sg, 10);
1471
1472 if (result == USB_STOR_TRANSPORT_GOOD) {
1473 transferred += len;
1474 US_DEBUGP("Wrote %08X bytes\n", transferred);
1475 }
1476
1477 return result;
1478
1479 } else if (srb->cmnd[0] == READ_10 ||
1480 srb->cmnd[0] == GPCMD_READ_CD) {
1481
1482 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1483
1484 }
1485
1486 if (len > 0xFFFF) {
1487 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1488 len);
1489 return USB_STOR_TRANSPORT_ERROR;
1490 }
1491
1492 if ( (result = usbat_multiple_write(us,
1493 registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
1494 return result;
1495 }
1496
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001497 /*
1498 * Write the 12-byte command header.
1499 *
1500 * If the command is BLANK then set the timer for 75 minutes.
1501 * Otherwise set it for 10 minutes.
1502 *
1503 * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1504 * AT SPEED 4 IS UNRELIABLE!!!
1505 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 if ( (result = usbat_write_block(us,
1508 USBAT_ATA, srb->cmnd, 12,
1509 srb->cmnd[0]==GPCMD_BLANK ? 75 : 10)) !=
1510 USB_STOR_TRANSPORT_GOOD) {
1511 return result;
1512 }
1513
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001514 /* If there is response data to be read in then do it here. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1517
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001518 /* How many bytes to read in? Check cylL register */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1521 USB_STOR_XFER_GOOD) {
1522 return USB_STOR_TRANSPORT_ERROR;
1523 }
1524
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001525 if (len > 0xFF) { /* need to read cylH also */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 len = *status;
1527 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1528 USB_STOR_XFER_GOOD) {
1529 return USB_STOR_TRANSPORT_ERROR;
1530 }
1531 len += ((unsigned int) *status)<<8;
1532 }
1533 else
1534 len = *status;
1535
1536
1537 result = usbat_read_block(us, srb->request_buffer, len);
1538
1539 /* Debug-print the first 32 bytes of the transfer */
1540
1541 if (!srb->use_sg) {
1542 string[0] = 0;
1543 for (i=0; i<len && i<32; i++) {
1544 sprintf(string+strlen(string), "%02X ",
1545 ((unsigned char *)srb->request_buffer)[i]);
1546 if ((i%16)==15) {
1547 US_DEBUGP("%s\n", string);
1548 string[0] = 0;
1549 }
1550 }
1551 if (string[0]!=0)
1552 US_DEBUGP("%s\n", string);
1553 }
1554 }
1555
1556 return result;
1557}
1558
1559/*
1560 * Transport for USBAT02-based CompactFlash and similar storage devices
1561 */
1562static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1563{
1564 int rc;
1565 struct usbat_info *info = (struct usbat_info *) (us->extra);
1566 unsigned long block, blocks;
1567 unsigned char *ptr = us->iobuf;
1568 static unsigned char inquiry_response[36] = {
1569 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1570 };
1571
1572 if (srb->cmnd[0] == INQUIRY) {
1573 US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1574 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1575 fill_inquiry_response(us, ptr, 36);
1576 return USB_STOR_TRANSPORT_GOOD;
1577 }
1578
1579 if (srb->cmnd[0] == READ_CAPACITY) {
1580 rc = usbat_flash_check_media(us, info);
1581 if (rc != USB_STOR_TRANSPORT_GOOD)
1582 return rc;
1583
1584 rc = usbat_flash_get_sector_count(us, info);
1585 if (rc != USB_STOR_TRANSPORT_GOOD)
1586 return rc;
1587
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001588 /* hard coded 512 byte sectors as per ATA spec */
1589 info->ssize = 0x200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1591 info->sectors, info->ssize);
1592
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001593 /*
1594 * build the reply
1595 * note: must return the sector number of the last sector,
1596 * *not* the total number of sectors
1597 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1599 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1600 usb_stor_set_xfer_buf(ptr, 8, srb);
1601
1602 return USB_STOR_TRANSPORT_GOOD;
1603 }
1604
1605 if (srb->cmnd[0] == MODE_SELECT_10) {
1606 US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
1607 return USB_STOR_TRANSPORT_ERROR;
1608 }
1609
1610 if (srb->cmnd[0] == READ_10) {
1611 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1612 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1613
1614 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1615
1616 US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
1617 return usbat_flash_read_data(us, info, block, blocks);
1618 }
1619
1620 if (srb->cmnd[0] == READ_12) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001621 /*
1622 * I don't think we'll ever see a READ_12 but support it anyway
1623 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1625 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1626
1627 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1628 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1629
1630 US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
1631 return usbat_flash_read_data(us, info, block, blocks);
1632 }
1633
1634 if (srb->cmnd[0] == WRITE_10) {
1635 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1636 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1637
1638 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1639
1640 US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
1641 return usbat_flash_write_data(us, info, block, blocks);
1642 }
1643
1644 if (srb->cmnd[0] == WRITE_12) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001645 /*
1646 * I don't think we'll ever see a WRITE_12 but support it anyway
1647 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1649 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1650
1651 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1652 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1653
1654 US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
1655 return usbat_flash_write_data(us, info, block, blocks);
1656 }
1657
1658
1659 if (srb->cmnd[0] == TEST_UNIT_READY) {
1660 US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
1661
1662 rc = usbat_flash_check_media(us, info);
1663 if (rc != USB_STOR_TRANSPORT_GOOD)
1664 return rc;
1665
1666 return usbat_check_status(us);
1667 }
1668
1669 if (srb->cmnd[0] == REQUEST_SENSE) {
1670 US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
1671
1672 memset(ptr, 0, 18);
1673 ptr[0] = 0xF0;
1674 ptr[2] = info->sense_key;
1675 ptr[7] = 11;
1676 ptr[12] = info->sense_asc;
1677 ptr[13] = info->sense_ascq;
1678 usb_stor_set_xfer_buf(ptr, 18, srb);
1679
1680 return USB_STOR_TRANSPORT_GOOD;
1681 }
1682
1683 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001684 /*
1685 * sure. whatever. not like we can stop the user from popping
1686 * the media out of the device (no locking doors, etc)
1687 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return USB_STOR_TRANSPORT_GOOD;
1689 }
1690
1691 US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1692 srb->cmnd[0], srb->cmnd[0]);
1693 info->sense_key = 0x05;
1694 info->sense_asc = 0x20;
1695 info->sense_ascq = 0x00;
1696 return USB_STOR_TRANSPORT_FAILED;
1697}
1698
1699/*
1700 * Default transport function. Attempts to detect which transport function
1701 * should be called, makes it the new default, and calls it.
1702 *
1703 * This function should never be called. Our usbat_init() function detects the
1704 * device type and changes the us->transport ptr to the transport function
1705 * relevant to the device.
1706 * However, we'll support this impossible(?) case anyway.
1707 */
1708int usbat_transport(struct scsi_cmnd *srb, struct us_data *us)
1709{
1710 struct usbat_info *info = (struct usbat_info*) (us->extra);
1711
1712 if (usbat_set_transport(us, info))
1713 return USB_STOR_TRANSPORT_ERROR;
1714
1715 return us->transport(srb, us);
1716}
1717