blob: ae6d64810d2a9d4cc8fc9db09fd68047136df801 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Current development and maintenance by:
4 * (c) 2000, 2001 Robert Baruch (autophile@starband.net)
5 * (c) 2004, 2005 Daniel Drake <dsd@gentoo.org>
6 *
7 * Developed with the assistance of:
8 * (c) 2002 Alan Stern <stern@rowland.org>
9 *
10 * Flash support based on earlier work by:
11 * (c) 2002 Thomas Kreiling <usbdev@sm04.de>
12 *
13 * Many originally ATAPI devices were slightly modified to meet the USB
14 * market by using some kind of translation from ATAPI to USB on the host,
15 * and the peripheral would translate from USB back to ATAPI.
16 *
17 * SCM Microsystems (www.scmmicro.com) makes a device, sold to OEM's only,
18 * which does the USB-to-ATAPI conversion. By obtaining the data sheet on
19 * their device under nondisclosure agreement, I have been able to write
20 * this driver for Linux.
21 *
22 * The chip used in the device can also be used for EPP and ISA translation
23 * as well. This driver is only guaranteed to work with the ATAPI
24 * translation.
25 *
26 * See the Kconfig help text for a list of devices known to be supported by
27 * this driver.
28 *
29 * This program is free software; you can redistribute it and/or modify it
30 * under the terms of the GNU General Public License as published by the
31 * Free Software Foundation; either version 2, or (at your option) any
32 * later version.
33 *
34 * This program is distributed in the hope that it will be useful, but
35 * WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37 * General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License along
40 * with this program; if not, write to the Free Software Foundation, Inc.,
41 * 675 Mass Ave, Cambridge, MA 02139, USA.
42 */
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/errno.h>
45#include <linux/slab.h>
46#include <linux/cdrom.h>
47
48#include <scsi/scsi.h>
49#include <scsi/scsi_cmnd.h>
50
51#include "usb.h"
52#include "transport.h"
53#include "protocol.h"
54#include "debug.h"
55#include "shuttle_usbat.h"
56
57#define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
58#define LSB_of(s) ((s)&0xFF)
59#define MSB_of(s) ((s)>>8)
60
61static int transferred = 0;
62
63static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us);
64static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us);
65
66/*
Daniel Drakeb7b1e652005-09-30 12:49:36 +010067 * Convenience function to produce an ATA read/write sectors command
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * Use cmd=0x20 for read, cmd=0x30 for write
69 */
Daniel Drakeb7b1e652005-09-30 12:49:36 +010070static void usbat_pack_ata_sector_cmd(unsigned char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 unsigned char thistime,
72 u32 sector, unsigned char cmd)
73{
74 buf[0] = 0;
75 buf[1] = thistime;
76 buf[2] = sector & 0xFF;
77 buf[3] = (sector >> 8) & 0xFF;
78 buf[4] = (sector >> 16) & 0xFF;
79 buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
80 buf[6] = cmd;
81}
82
83/*
84 * Convenience function to get the device type (flash or hp8200)
85 */
86static int usbat_get_device_type(struct us_data *us)
87{
88 return ((struct usbat_info*)us->extra)->devicetype;
89}
90
91/*
92 * Read a register from the device
93 */
94static int usbat_read(struct us_data *us,
95 unsigned char access,
96 unsigned char reg,
97 unsigned char *content)
98{
99 return usb_stor_ctrl_transfer(us,
100 us->recv_ctrl_pipe,
101 access | USBAT_CMD_READ_REG,
102 0xC0,
103 (u16)reg,
104 0,
105 content,
106 1);
107}
108
109/*
110 * Write to a register on the device
111 */
112static int usbat_write(struct us_data *us,
113 unsigned char access,
114 unsigned char reg,
115 unsigned char content)
116{
117 return usb_stor_ctrl_transfer(us,
118 us->send_ctrl_pipe,
119 access | USBAT_CMD_WRITE_REG,
120 0x40,
121 short_pack(reg, content),
122 0,
123 NULL,
124 0);
125}
126
127/*
128 * Convenience function to perform a bulk read
129 */
130static int usbat_bulk_read(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300131 void* buf,
Peter Chubb141804d2006-05-02 18:30:12 +0100132 unsigned int len,
133 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 if (len == 0)
136 return USB_STOR_XFER_GOOD;
137
138 US_DEBUGP("usbat_bulk_read: len = %d\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +0300139 return usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe, buf, len, use_sg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142/*
143 * Convenience function to perform a bulk write
144 */
145static int usbat_bulk_write(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300146 void* buf,
Peter Chubb141804d2006-05-02 18:30:12 +0100147 unsigned int len,
148 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 if (len == 0)
151 return USB_STOR_XFER_GOOD;
152
153 US_DEBUGP("usbat_bulk_write: len = %d\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +0300154 return usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe, buf, len, use_sg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157/*
158 * Some USBAT-specific commands can only be executed over a command transport
159 * This transport allows one (len=8) or two (len=16) vendor-specific commands
160 * to be executed.
161 */
162static int usbat_execute_command(struct us_data *us,
163 unsigned char *commands,
164 unsigned int len)
165{
166 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
167 USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
168 commands, len);
169}
170
171/*
172 * Read the status register
173 */
174static int usbat_get_status(struct us_data *us, unsigned char *status)
175{
176 int rc;
177 rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
178
179 US_DEBUGP("usbat_get_status: 0x%02X\n", (unsigned short) (*status));
180 return rc;
181}
182
183/*
184 * Check the device status
185 */
186static int usbat_check_status(struct us_data *us)
187{
188 unsigned char *reply = us->iobuf;
189 int rc;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 rc = usbat_get_status(us, reply);
192 if (rc != USB_STOR_XFER_GOOD)
193 return USB_STOR_TRANSPORT_FAILED;
194
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100195 /* error/check condition (0x51 is ok) */
196 if (*reply & 0x01 && *reply != 0x51)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return USB_STOR_TRANSPORT_FAILED;
198
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100199 /* device fault */
200 if (*reply & 0x20)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return USB_STOR_TRANSPORT_FAILED;
202
203 return USB_STOR_TRANSPORT_GOOD;
204}
205
206/*
207 * Stores critical information in internal registers in prepartion for the execution
208 * of a conditional usbat_read_blocks or usbat_write_blocks call.
209 */
210static int usbat_set_shuttle_features(struct us_data *us,
211 unsigned char external_trigger,
212 unsigned char epp_control,
213 unsigned char mask_byte,
214 unsigned char test_pattern,
215 unsigned char subcountH,
216 unsigned char subcountL)
217{
218 unsigned char *command = us->iobuf;
219
220 command[0] = 0x40;
221 command[1] = USBAT_CMD_SET_FEAT;
222
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100223 /*
224 * The only bit relevant to ATA access is bit 6
225 * which defines 8 bit data access (set) or 16 bit (unset)
226 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 command[2] = epp_control;
228
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100229 /*
230 * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
231 * ET1 and ET2 define an external event to be checked for on event of a
232 * _read_blocks or _write_blocks operation. The read/write will not take
233 * place unless the defined trigger signal is active.
234 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 command[3] = external_trigger;
236
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100237 /*
238 * The resultant byte of the mask operation (see mask_byte) is compared for
239 * equivalence with this test pattern. If equal, the read/write will take
240 * place.
241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 command[4] = test_pattern;
243
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100244 /*
245 * This value is logically ANDed with the status register field specified
246 * in the read/write command.
247 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 command[5] = mask_byte;
249
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100250 /*
251 * If ALQ is set in the qualifier, this field contains the address of the
252 * registers where the byte count should be read for transferring the data.
253 * If ALQ is not set, then this field contains the number of bytes to be
254 * transferred.
255 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 command[6] = subcountL;
257 command[7] = subcountH;
258
259 return usbat_execute_command(us, command, 8);
260}
261
262/*
263 * Block, waiting for an ATA device to become not busy or to report
264 * an error condition.
265 */
266static int usbat_wait_not_busy(struct us_data *us, int minutes)
267{
268 int i;
269 int result;
270 unsigned char *status = us->iobuf;
271
272 /* Synchronizing cache on a CDR could take a heck of a long time,
273 * but probably not more than 10 minutes or so. On the other hand,
274 * doing a full blank on a CDRW at speed 1 will take about 75
275 * minutes!
276 */
277
278 for (i=0; i<1200+minutes*60; i++) {
279
280 result = usbat_get_status(us, status);
281
282 if (result!=USB_STOR_XFER_GOOD)
283 return USB_STOR_TRANSPORT_ERROR;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100284 if (*status & 0x01) { /* check condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 result = usbat_read(us, USBAT_ATA, 0x10, status);
286 return USB_STOR_TRANSPORT_FAILED;
287 }
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100288 if (*status & 0x20) /* device fault */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return USB_STOR_TRANSPORT_FAILED;
290
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100291 if ((*status & 0x80)==0x00) { /* not busy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 US_DEBUGP("Waited not busy for %d steps\n", i);
293 return USB_STOR_TRANSPORT_GOOD;
294 }
295
296 if (i<500)
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100297 msleep(10); /* 5 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 else if (i<700)
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100299 msleep(50); /* 10 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 else if (i<1200)
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100301 msleep(100); /* 50 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 else
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100303 msleep(1000); /* X minutes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
306 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
307 minutes);
308 return USB_STOR_TRANSPORT_FAILED;
309}
310
311/*
312 * Read block data from the data register
313 */
314static int usbat_read_block(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300315 void* buf,
Peter Chubb141804d2006-05-02 18:30:12 +0100316 unsigned short len,
317 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 int result;
320 unsigned char *command = us->iobuf;
321
322 if (!len)
323 return USB_STOR_TRANSPORT_GOOD;
324
325 command[0] = 0xC0;
326 command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
327 command[2] = USBAT_ATA_DATA;
328 command[3] = 0;
329 command[4] = 0;
330 command[5] = 0;
331 command[6] = LSB_of(len);
332 command[7] = MSB_of(len);
333
334 result = usbat_execute_command(us, command, 8);
335 if (result != USB_STOR_XFER_GOOD)
336 return USB_STOR_TRANSPORT_ERROR;
337
Boaz Harrosh4776e992007-09-09 20:40:56 +0300338 result = usbat_bulk_read(us, buf, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return (result == USB_STOR_XFER_GOOD ?
340 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
341}
342
343/*
344 * Write block data via the data register
345 */
346static int usbat_write_block(struct us_data *us,
347 unsigned char access,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300348 void* buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 unsigned short len,
Peter Chubb141804d2006-05-02 18:30:12 +0100350 int minutes,
351 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 int result;
354 unsigned char *command = us->iobuf;
355
356 if (!len)
357 return USB_STOR_TRANSPORT_GOOD;
358
359 command[0] = 0x40;
360 command[1] = access | USBAT_CMD_WRITE_BLOCK;
361 command[2] = USBAT_ATA_DATA;
362 command[3] = 0;
363 command[4] = 0;
364 command[5] = 0;
365 command[6] = LSB_of(len);
366 command[7] = MSB_of(len);
367
368 result = usbat_execute_command(us, command, 8);
369
370 if (result != USB_STOR_XFER_GOOD)
371 return USB_STOR_TRANSPORT_ERROR;
372
Boaz Harrosh4776e992007-09-09 20:40:56 +0300373 result = usbat_bulk_write(us, buf, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (result != USB_STOR_XFER_GOOD)
375 return USB_STOR_TRANSPORT_ERROR;
376
377 return usbat_wait_not_busy(us, minutes);
378}
379
380/*
381 * Process read and write requests
382 */
383static int usbat_hp8200e_rw_block_test(struct us_data *us,
384 unsigned char access,
385 unsigned char *registers,
386 unsigned char *data_out,
387 unsigned short num_registers,
388 unsigned char data_reg,
389 unsigned char status_reg,
390 unsigned char timeout,
391 unsigned char qualifier,
392 int direction,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300393 void *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 unsigned short len,
395 int use_sg,
396 int minutes)
397{
398 int result;
399 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
400 us->recv_bulk_pipe : us->send_bulk_pipe;
401
402 unsigned char *command = us->iobuf;
403 int i, j;
404 int cmdlen;
405 unsigned char *data = us->iobuf;
406 unsigned char *status = us->iobuf;
407
408 BUG_ON(num_registers > US_IOBUF_SIZE/2);
409
410 for (i=0; i<20; i++) {
411
412 /*
413 * The first time we send the full command, which consists
414 * of downloading the SCSI command followed by downloading
415 * the data via a write-and-test. Any other time we only
416 * send the command to download the data -- the SCSI command
417 * is still 'active' in some sense in the device.
418 *
419 * We're only going to try sending the data 10 times. After
420 * that, we just return a failure.
421 */
422
423 if (i==0) {
424 cmdlen = 16;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100425 /*
426 * Write to multiple registers
427 * Not really sure the 0x07, 0x17, 0xfc, 0xe7 is
428 * necessary here, but that's what came out of the
429 * trace every single time.
430 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 command[0] = 0x40;
432 command[1] = access | USBAT_CMD_WRITE_REGS;
433 command[2] = 0x07;
434 command[3] = 0x17;
435 command[4] = 0xFC;
436 command[5] = 0xE7;
437 command[6] = LSB_of(num_registers*2);
438 command[7] = MSB_of(num_registers*2);
439 } else
440 cmdlen = 8;
441
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100442 /* Conditionally read or write blocks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 command[cmdlen-8] = (direction==DMA_TO_DEVICE ? 0x40 : 0xC0);
444 command[cmdlen-7] = access |
445 (direction==DMA_TO_DEVICE ?
446 USBAT_CMD_COND_WRITE_BLOCK : USBAT_CMD_COND_READ_BLOCK);
447 command[cmdlen-6] = data_reg;
448 command[cmdlen-5] = status_reg;
449 command[cmdlen-4] = timeout;
450 command[cmdlen-3] = qualifier;
451 command[cmdlen-2] = LSB_of(len);
452 command[cmdlen-1] = MSB_of(len);
453
454 result = usbat_execute_command(us, command, cmdlen);
455
456 if (result != USB_STOR_XFER_GOOD)
457 return USB_STOR_TRANSPORT_ERROR;
458
459 if (i==0) {
460
461 for (j=0; j<num_registers; j++) {
462 data[j<<1] = registers[j];
463 data[1+(j<<1)] = data_out[j];
464 }
465
Peter Chubb141804d2006-05-02 18:30:12 +0100466 result = usbat_bulk_write(us, data, num_registers*2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (result != USB_STOR_XFER_GOOD)
468 return USB_STOR_TRANSPORT_ERROR;
469
470 }
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 result = usb_stor_bulk_transfer_sg(us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300473 pipe, buf, len, use_sg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /*
476 * If we get a stall on the bulk download, we'll retry
477 * the bulk download -- but not the SCSI command because
478 * in some sense the SCSI command is still 'active' and
479 * waiting for the data. Don't ask me why this should be;
480 * I'm only following what the Windoze driver did.
481 *
482 * Note that a stall for the test-and-read/write command means
483 * that the test failed. In this case we're testing to make
484 * sure that the device is error-free
485 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
486 * hypothesis is that the USBAT chip somehow knows what
487 * the device will accept, but doesn't give the device any
488 * data until all data is received. Thus, the device would
489 * still be waiting for the first byte of data if a stall
490 * occurs, even if the stall implies that some data was
491 * transferred.
492 */
493
494 if (result == USB_STOR_XFER_SHORT ||
495 result == USB_STOR_XFER_STALLED) {
496
497 /*
498 * If we're reading and we stalled, then clear
499 * the bulk output pipe only the first time.
500 */
501
502 if (direction==DMA_FROM_DEVICE && i==0) {
503 if (usb_stor_clear_halt(us,
504 us->send_bulk_pipe) < 0)
505 return USB_STOR_TRANSPORT_ERROR;
506 }
507
508 /*
509 * Read status: is the device angry, or just busy?
510 */
511
512 result = usbat_read(us, USBAT_ATA,
513 direction==DMA_TO_DEVICE ?
514 USBAT_ATA_STATUS : USBAT_ATA_ALTSTATUS,
515 status);
516
517 if (result!=USB_STOR_XFER_GOOD)
518 return USB_STOR_TRANSPORT_ERROR;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100519 if (*status & 0x01) /* check condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return USB_STOR_TRANSPORT_FAILED;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100521 if (*status & 0x20) /* device fault */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return USB_STOR_TRANSPORT_FAILED;
523
524 US_DEBUGP("Redoing %s\n",
525 direction==DMA_TO_DEVICE ? "write" : "read");
526
527 } else if (result != USB_STOR_XFER_GOOD)
528 return USB_STOR_TRANSPORT_ERROR;
529 else
530 return usbat_wait_not_busy(us, minutes);
531
532 }
533
534 US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
535 direction==DMA_TO_DEVICE ? "Writing" : "Reading");
536
537 return USB_STOR_TRANSPORT_FAILED;
538}
539
540/*
541 * Write to multiple registers:
542 * Allows us to write specific data to any registers. The data to be written
543 * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN
544 * which gets sent through bulk out.
545 * Not designed for large transfers of data!
546 */
547static int usbat_multiple_write(struct us_data *us,
548 unsigned char *registers,
549 unsigned char *data_out,
550 unsigned short num_registers)
551{
552 int i, result;
553 unsigned char *data = us->iobuf;
554 unsigned char *command = us->iobuf;
555
556 BUG_ON(num_registers > US_IOBUF_SIZE/2);
557
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100558 /* Write to multiple registers, ATA access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 command[0] = 0x40;
560 command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
561
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100562 /* No relevance */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 command[2] = 0;
564 command[3] = 0;
565 command[4] = 0;
566 command[5] = 0;
567
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100568 /* Number of bytes to be transferred (incl. addresses and data) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 command[6] = LSB_of(num_registers*2);
570 command[7] = MSB_of(num_registers*2);
571
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100572 /* The setup command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 result = usbat_execute_command(us, command, 8);
574 if (result != USB_STOR_XFER_GOOD)
575 return USB_STOR_TRANSPORT_ERROR;
576
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100577 /* Create the reg/data, reg/data sequence */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 for (i=0; i<num_registers; i++) {
579 data[i<<1] = registers[i];
580 data[1+(i<<1)] = data_out[i];
581 }
582
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100583 /* Send the data */
Peter Chubb141804d2006-05-02 18:30:12 +0100584 result = usbat_bulk_write(us, data, num_registers*2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (result != USB_STOR_XFER_GOOD)
586 return USB_STOR_TRANSPORT_ERROR;
587
588 if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
589 return usbat_wait_not_busy(us, 0);
590 else
591 return USB_STOR_TRANSPORT_GOOD;
592}
593
594/*
595 * Conditionally read blocks from device:
596 * Allows us to read blocks from a specific data register, based upon the
597 * condition that a status register can be successfully masked with a status
598 * qualifier. If this condition is not initially met, the read will wait
599 * up until a maximum amount of time has elapsed, as specified by timeout.
600 * The read will start when the condition is met, otherwise the command aborts.
601 *
602 * The qualifier defined here is not the value that is masked, it defines
603 * conditions for the write to take place. The actual masked qualifier (and
604 * other related details) are defined beforehand with _set_shuttle_features().
605 */
606static int usbat_read_blocks(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300607 void* buffer,
Peter Chubb141804d2006-05-02 18:30:12 +0100608 int len,
609 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 int result;
612 unsigned char *command = us->iobuf;
613
614 command[0] = 0xC0;
615 command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK;
616 command[2] = USBAT_ATA_DATA;
617 command[3] = USBAT_ATA_STATUS;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100618 command[4] = 0xFD; /* Timeout (ms); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 command[5] = USBAT_QUAL_FCQ;
620 command[6] = LSB_of(len);
621 command[7] = MSB_of(len);
622
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100623 /* Multiple block read setup command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 result = usbat_execute_command(us, command, 8);
625 if (result != USB_STOR_XFER_GOOD)
626 return USB_STOR_TRANSPORT_FAILED;
627
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100628 /* Read the blocks we just asked for */
Peter Chubb141804d2006-05-02 18:30:12 +0100629 result = usbat_bulk_read(us, buffer, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (result != USB_STOR_XFER_GOOD)
631 return USB_STOR_TRANSPORT_FAILED;
632
633 return USB_STOR_TRANSPORT_GOOD;
634}
635
636/*
637 * Conditionally write blocks to device:
638 * Allows us to write blocks to a specific data register, based upon the
639 * condition that a status register can be successfully masked with a status
640 * qualifier. If this condition is not initially met, the write will wait
641 * up until a maximum amount of time has elapsed, as specified by timeout.
642 * The read will start when the condition is met, otherwise the command aborts.
643 *
644 * The qualifier defined here is not the value that is masked, it defines
645 * conditions for the write to take place. The actual masked qualifier (and
646 * other related details) are defined beforehand with _set_shuttle_features().
647 */
648static int usbat_write_blocks(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300649 void* buffer,
Peter Chubb141804d2006-05-02 18:30:12 +0100650 int len,
651 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
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 */
Peter Chubb141804d2006-05-02 18:30:12 +0100671 result = usbat_bulk_write(us, buffer, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 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,
Peter Chubbbdcfd9e2006-05-02 18:29:34 +0100890 struct usbat_info *info,
891 int devicetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Peter Chubbbdcfd9e2006-05-02 18:29:34 +0100894 if (!info->devicetype)
895 info->devicetype = devicetype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Peter Chubbbdcfd9e2006-05-02 18:29:34 +0100897 if (!info->devicetype)
898 usbat_identify_device(us, info);
899
900 switch (info->devicetype) {
901 default:
902 return USB_STOR_TRANSPORT_ERROR;
903
904 case USBAT_DEV_HP8200:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 us->transport = usbat_hp8200e_transport;
Peter Chubbbdcfd9e2006-05-02 18:29:34 +0100906 break;
907
908 case USBAT_DEV_FLASH:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 us->transport = usbat_flash_transport;
Peter Chubbbdcfd9e2006-05-02 18:29:34 +0100910 break;
911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 return 0;
914}
915
916/*
917 * Read the media capacity
918 */
919static int usbat_flash_get_sector_count(struct us_data *us,
920 struct usbat_info *info)
921{
922 unsigned char registers[3] = {
923 USBAT_ATA_SECCNT,
924 USBAT_ATA_DEVICE,
925 USBAT_ATA_CMD,
926 };
927 unsigned char command[3] = { 0x01, 0xA0, 0xEC };
928 unsigned char *reply;
929 unsigned char status;
930 int rc;
931
932 if (!us || !info)
933 return USB_STOR_TRANSPORT_ERROR;
934
935 reply = kmalloc(512, GFP_NOIO);
936 if (!reply)
937 return USB_STOR_TRANSPORT_ERROR;
938
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100939 /* ATA command : IDENTIFY DEVICE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 rc = usbat_multiple_write(us, registers, command, 3);
941 if (rc != USB_STOR_XFER_GOOD) {
942 US_DEBUGP("usbat_flash_get_sector_count: Gah! identify_device failed\n");
943 rc = USB_STOR_TRANSPORT_ERROR;
944 goto leave;
945 }
946
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100947 /* Read device status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
949 rc = USB_STOR_TRANSPORT_ERROR;
950 goto leave;
951 }
952
953 msleep(100);
954
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100955 /* Read the device identification data */
Peter Chubb141804d2006-05-02 18:30:12 +0100956 rc = usbat_read_block(us, reply, 512, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (rc != USB_STOR_TRANSPORT_GOOD)
958 goto leave;
959
960 info->sectors = ((u32)(reply[117]) << 24) |
961 ((u32)(reply[116]) << 16) |
962 ((u32)(reply[115]) << 8) |
963 ((u32)(reply[114]) );
964
965 rc = USB_STOR_TRANSPORT_GOOD;
966
967 leave:
968 kfree(reply);
969 return rc;
970}
971
972/*
973 * Read data from device
974 */
975static int usbat_flash_read_data(struct us_data *us,
976 struct usbat_info *info,
977 u32 sector,
978 u32 sectors)
979{
980 unsigned char registers[7] = {
981 USBAT_ATA_FEATURES,
982 USBAT_ATA_SECCNT,
983 USBAT_ATA_SECNUM,
984 USBAT_ATA_LBA_ME,
985 USBAT_ATA_LBA_HI,
986 USBAT_ATA_DEVICE,
987 USBAT_ATA_STATUS,
988 };
989 unsigned char command[7];
990 unsigned char *buffer;
991 unsigned char thistime;
992 unsigned int totallen, alloclen;
993 int len, result;
Jens Axboe1f6f31a2007-05-11 12:33:09 +0200994 unsigned int sg_offset = 0;
995 struct scatterlist *sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 result = usbat_flash_check_media(us, info);
998 if (result != USB_STOR_TRANSPORT_GOOD)
999 return result;
1000
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001001 /*
1002 * we're working in LBA mode. according to the ATA spec,
1003 * we can support up to 28-bit addressing. I don't know if Jumpshot
1004 * supports beyond 24-bit addressing. It's kind of hard to test
1005 * since it requires > 8GB CF card.
1006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 if (sector > 0x0FFFFFFF)
1009 return USB_STOR_TRANSPORT_ERROR;
1010
1011 totallen = sectors * info->ssize;
1012
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001013 /*
1014 * Since we don't read more than 64 KB at a time, we have to create
1015 * a bounce buffer and move the data a piece at a time between the
1016 * bounce buffer and the actual transfer buffer.
1017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 alloclen = min(totallen, 65536u);
1020 buffer = kmalloc(alloclen, GFP_NOIO);
1021 if (buffer == NULL)
1022 return USB_STOR_TRANSPORT_ERROR;
1023
1024 do {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001025 /*
1026 * loop, never allocate or transfer more than 64k at once
1027 * (min(128k, 255*info->ssize) is the real limit)
1028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 len = min(totallen, alloclen);
1030 thistime = (len / info->ssize) & 0xff;
1031
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001032 /* ATA command 0x20 (READ SECTORS) */
1033 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001035 /* Write/execute ATA read command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 result = usbat_multiple_write(us, registers, command, 7);
1037 if (result != USB_STOR_TRANSPORT_GOOD)
1038 goto leave;
1039
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001040 /* Read the data we just requested */
Peter Chubb141804d2006-05-02 18:30:12 +01001041 result = usbat_read_blocks(us, buffer, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (result != USB_STOR_TRANSPORT_GOOD)
1043 goto leave;
1044
1045 US_DEBUGP("usbat_flash_read_data: %d bytes\n", len);
1046
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001047 /* Store the data in the transfer buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 usb_stor_access_xfer_buf(buffer, len, us->srb,
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001049 &sg, &sg_offset, TO_XFER_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 sector += thistime;
1052 totallen -= len;
1053 } while (totallen > 0);
1054
1055 kfree(buffer);
1056 return USB_STOR_TRANSPORT_GOOD;
1057
1058leave:
1059 kfree(buffer);
1060 return USB_STOR_TRANSPORT_ERROR;
1061}
1062
1063/*
1064 * Write data to device
1065 */
1066static int usbat_flash_write_data(struct us_data *us,
1067 struct usbat_info *info,
1068 u32 sector,
1069 u32 sectors)
1070{
1071 unsigned char registers[7] = {
1072 USBAT_ATA_FEATURES,
1073 USBAT_ATA_SECCNT,
1074 USBAT_ATA_SECNUM,
1075 USBAT_ATA_LBA_ME,
1076 USBAT_ATA_LBA_HI,
1077 USBAT_ATA_DEVICE,
1078 USBAT_ATA_STATUS,
1079 };
1080 unsigned char command[7];
1081 unsigned char *buffer;
1082 unsigned char thistime;
1083 unsigned int totallen, alloclen;
1084 int len, result;
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001085 unsigned int sg_offset = 0;
1086 struct scatterlist *sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 result = usbat_flash_check_media(us, info);
1089 if (result != USB_STOR_TRANSPORT_GOOD)
1090 return result;
1091
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001092 /*
1093 * we're working in LBA mode. according to the ATA spec,
1094 * we can support up to 28-bit addressing. I don't know if the device
1095 * supports beyond 24-bit addressing. It's kind of hard to test
1096 * since it requires > 8GB media.
1097 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 if (sector > 0x0FFFFFFF)
1100 return USB_STOR_TRANSPORT_ERROR;
1101
1102 totallen = sectors * info->ssize;
1103
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001104 /*
1105 * Since we don't write more than 64 KB at a time, we have to create
1106 * a bounce buffer and move the data a piece at a time between the
1107 * bounce buffer and the actual transfer buffer.
1108 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 alloclen = min(totallen, 65536u);
1111 buffer = kmalloc(alloclen, GFP_NOIO);
1112 if (buffer == NULL)
1113 return USB_STOR_TRANSPORT_ERROR;
1114
1115 do {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001116 /*
1117 * loop, never allocate or transfer more than 64k at once
1118 * (min(128k, 255*info->ssize) is the real limit)
1119 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 len = min(totallen, alloclen);
1121 thistime = (len / info->ssize) & 0xff;
1122
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001123 /* Get the data from the transfer buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 usb_stor_access_xfer_buf(buffer, len, us->srb,
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001125 &sg, &sg_offset, FROM_XFER_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001127 /* ATA command 0x30 (WRITE SECTORS) */
1128 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001130 /* Write/execute ATA write command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 result = usbat_multiple_write(us, registers, command, 7);
1132 if (result != USB_STOR_TRANSPORT_GOOD)
1133 goto leave;
1134
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001135 /* Write the data */
Peter Chubb141804d2006-05-02 18:30:12 +01001136 result = usbat_write_blocks(us, buffer, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (result != USB_STOR_TRANSPORT_GOOD)
1138 goto leave;
1139
1140 sector += thistime;
1141 totallen -= len;
1142 } while (totallen > 0);
1143
1144 kfree(buffer);
1145 return result;
1146
1147leave:
1148 kfree(buffer);
1149 return USB_STOR_TRANSPORT_ERROR;
1150}
1151
1152/*
1153 * Squeeze a potentially huge (> 65535 byte) read10 command into
1154 * a little ( <= 65535 byte) ATAPI pipe
1155 */
1156static int usbat_hp8200e_handle_read10(struct us_data *us,
1157 unsigned char *registers,
1158 unsigned char *data,
1159 struct scsi_cmnd *srb)
1160{
1161 int result = USB_STOR_TRANSPORT_GOOD;
1162 unsigned char *buffer;
1163 unsigned int len;
1164 unsigned int sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 unsigned int sg_offset = 0;
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001166 struct scatterlist *sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 US_DEBUGP("handle_read10: transfersize %d\n",
1169 srb->transfersize);
1170
Boaz Harrosh4776e992007-09-09 20:40:56 +03001171 if (scsi_bufflen(srb) < 0x10000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1174 registers, data, 19,
1175 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1176 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1177 DMA_FROM_DEVICE,
Boaz Harrosh4776e992007-09-09 20:40:56 +03001178 scsi_sglist(srb),
1179 scsi_bufflen(srb), scsi_sg_count(srb), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 return result;
1182 }
1183
1184 /*
1185 * Since we're requesting more data than we can handle in
1186 * a single read command (max is 64k-1), we will perform
1187 * multiple reads, but each read must be in multiples of
1188 * a sector. Luckily the sector size is in srb->transfersize
1189 * (see linux/drivers/scsi/sr.c).
1190 */
1191
1192 if (data[7+0] == GPCMD_READ_CD) {
1193 len = short_pack(data[7+9], data[7+8]);
1194 len <<= 16;
1195 len |= data[7+7];
1196 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +03001197 srb->transfersize = scsi_bufflen(srb)/len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
1199
1200 if (!srb->transfersize) {
1201 srb->transfersize = 2048; /* A guess */
1202 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
1203 srb->transfersize);
1204 }
1205
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001206 /*
1207 * Since we only read in one block at a time, we have to create
1208 * a bounce buffer and move the data a piece at a time between the
1209 * bounce buffer and the actual transfer buffer.
1210 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 len = (65535/srb->transfersize) * srb->transfersize;
1213 US_DEBUGP("Max read is %d bytes\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +03001214 len = min(len, scsi_bufflen(srb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 buffer = kmalloc(len, GFP_NOIO);
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001216 if (buffer == NULL) /* bloody hell! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return USB_STOR_TRANSPORT_FAILED;
1218 sector = short_pack(data[7+3], data[7+2]);
1219 sector <<= 16;
1220 sector |= short_pack(data[7+5], data[7+4]);
1221 transferred = 0;
1222
Boaz Harrosh4776e992007-09-09 20:40:56 +03001223 while (transferred != scsi_bufflen(srb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Boaz Harrosh4776e992007-09-09 20:40:56 +03001225 if (len > scsi_bufflen(srb) - transferred)
1226 len = scsi_bufflen(srb) - transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001228 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1229 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001231 /* Fix up the SCSI command sector and num sectors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001233 data[7+2] = MSB_of(sector>>16); /* SCSI command sector */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 data[7+3] = LSB_of(sector>>16);
1235 data[7+4] = MSB_of(sector&0xFFFF);
1236 data[7+5] = LSB_of(sector&0xFFFF);
1237 if (data[7+0] == GPCMD_READ_CD)
1238 data[7+6] = 0;
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001239 data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */
1240 data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1243 registers, data, 19,
1244 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1245 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1246 DMA_FROM_DEVICE,
1247 buffer,
1248 len, 0, 1);
1249
1250 if (result != USB_STOR_TRANSPORT_GOOD)
1251 break;
1252
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001253 /* Store the data in the transfer buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 usb_stor_access_xfer_buf(buffer, len, srb,
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001255 &sg, &sg_offset, TO_XFER_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001257 /* Update the amount transferred and the sector number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 transferred += len;
1260 sector += len / srb->transfersize;
1261
Boaz Harrosh4776e992007-09-09 20:40:56 +03001262 } /* while transferred != scsi_bufflen(srb) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 kfree(buffer);
1265 return result;
1266}
1267
1268static int usbat_select_and_test_registers(struct us_data *us)
1269{
1270 int selector;
1271 unsigned char *status = us->iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001273 /* try device = master, then device = slave. */
Daniel Drake68a64572005-08-10 18:30:04 +01001274 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
1275 if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 USB_STOR_XFER_GOOD)
1277 return USB_STOR_TRANSPORT_ERROR;
1278
1279 if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) !=
1280 USB_STOR_XFER_GOOD)
1281 return USB_STOR_TRANSPORT_ERROR;
1282
1283 if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) !=
1284 USB_STOR_XFER_GOOD)
1285 return USB_STOR_TRANSPORT_ERROR;
1286
1287 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1288 USB_STOR_XFER_GOOD)
1289 return USB_STOR_TRANSPORT_ERROR;
1290
1291 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1292 USB_STOR_XFER_GOOD)
1293 return USB_STOR_TRANSPORT_ERROR;
1294
1295 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) !=
1296 USB_STOR_XFER_GOOD)
1297 return USB_STOR_TRANSPORT_ERROR;
1298
1299 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) !=
1300 USB_STOR_XFER_GOOD)
1301 return USB_STOR_TRANSPORT_ERROR;
1302
1303 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1304 USB_STOR_XFER_GOOD)
1305 return USB_STOR_TRANSPORT_ERROR;
1306
1307 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1308 USB_STOR_XFER_GOOD)
1309 return USB_STOR_TRANSPORT_ERROR;
1310 }
1311
1312 return USB_STOR_TRANSPORT_GOOD;
1313}
1314
1315/*
1316 * Initialize the USBAT processor and the storage device
1317 */
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001318static int init_usbat(struct us_data *us, int devicetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 int rc;
1321 struct usbat_info *info;
1322 unsigned char subcountH = USBAT_ATA_LBA_HI;
1323 unsigned char subcountL = USBAT_ATA_LBA_ME;
1324 unsigned char *status = us->iobuf;
1325
Oliver Neukum887c2562006-01-08 12:33:45 +01001326 us->extra = kzalloc(sizeof(struct usbat_info), GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (!us->extra) {
1328 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1329 return 1;
1330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 info = (struct usbat_info *) (us->extra);
1332
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001333 /* Enable peripheral control signals */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 rc = usbat_write_user_io(us,
1335 USBAT_UIO_OE1 | USBAT_UIO_OE0,
1336 USBAT_UIO_EPAD | USBAT_UIO_1);
1337 if (rc != USB_STOR_XFER_GOOD)
1338 return USB_STOR_TRANSPORT_ERROR;
1339
1340 US_DEBUGP("INIT 1\n");
1341
1342 msleep(2000);
1343
1344 rc = usbat_read_user_io(us, status);
1345 if (rc != USB_STOR_TRANSPORT_GOOD)
1346 return rc;
1347
1348 US_DEBUGP("INIT 2\n");
1349
1350 rc = usbat_read_user_io(us, status);
1351 if (rc != USB_STOR_XFER_GOOD)
1352 return USB_STOR_TRANSPORT_ERROR;
1353
1354 rc = usbat_read_user_io(us, status);
1355 if (rc != USB_STOR_XFER_GOOD)
1356 return USB_STOR_TRANSPORT_ERROR;
1357
1358 US_DEBUGP("INIT 3\n");
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 rc = usbat_select_and_test_registers(us);
1361 if (rc != USB_STOR_TRANSPORT_GOOD)
1362 return rc;
1363
Daniel Drake68a64572005-08-10 18:30:04 +01001364 US_DEBUGP("INIT 4\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 rc = usbat_read_user_io(us, status);
1367 if (rc != USB_STOR_XFER_GOOD)
1368 return USB_STOR_TRANSPORT_ERROR;
1369
Daniel Drake68a64572005-08-10 18:30:04 +01001370 US_DEBUGP("INIT 5\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001372 /* Enable peripheral control signals and card detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 rc = usbat_device_enable_cdt(us);
1374 if (rc != USB_STOR_TRANSPORT_GOOD)
1375 return rc;
1376
Daniel Drake68a64572005-08-10 18:30:04 +01001377 US_DEBUGP("INIT 6\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 rc = usbat_read_user_io(us, status);
1380 if (rc != USB_STOR_XFER_GOOD)
1381 return USB_STOR_TRANSPORT_ERROR;
1382
Daniel Drake68a64572005-08-10 18:30:04 +01001383 US_DEBUGP("INIT 7\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 msleep(1400);
1386
1387 rc = usbat_read_user_io(us, status);
1388 if (rc != USB_STOR_XFER_GOOD)
1389 return USB_STOR_TRANSPORT_ERROR;
1390
Daniel Drake68a64572005-08-10 18:30:04 +01001391 US_DEBUGP("INIT 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 rc = usbat_select_and_test_registers(us);
1394 if (rc != USB_STOR_TRANSPORT_GOOD)
1395 return rc;
1396
Daniel Drake68a64572005-08-10 18:30:04 +01001397 US_DEBUGP("INIT 9\n");
1398
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001399 /* At this point, we need to detect which device we are using */
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001400 if (usbat_set_transport(us, info, devicetype))
Daniel Drake68a64572005-08-10 18:30:04 +01001401 return USB_STOR_TRANSPORT_ERROR;
1402
1403 US_DEBUGP("INIT 10\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 if (usbat_get_device_type(us) == USBAT_DEV_FLASH) {
1406 subcountH = 0x02;
1407 subcountL = 0x00;
1408 }
1409 rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1410 0x00, 0x88, 0x08, subcountH, subcountL);
1411 if (rc != USB_STOR_XFER_GOOD)
1412 return USB_STOR_TRANSPORT_ERROR;
1413
Daniel Drake68a64572005-08-10 18:30:04 +01001414 US_DEBUGP("INIT 11\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 return USB_STOR_TRANSPORT_GOOD;
1417}
1418
1419/*
1420 * Transport for the HP 8200e
1421 */
1422static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1423{
1424 int result;
1425 unsigned char *status = us->iobuf;
1426 unsigned char registers[32];
1427 unsigned char data[32];
1428 unsigned int len;
1429 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Boaz Harrosh4776e992007-09-09 20:40:56 +03001431 len = scsi_bufflen(srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 /* Send A0 (ATA PACKET COMMAND).
1434 Note: I guess we're never going to get any of the ATA
1435 commands... just ATA Packet Commands.
1436 */
1437
1438 registers[0] = USBAT_ATA_FEATURES;
1439 registers[1] = USBAT_ATA_SECCNT;
1440 registers[2] = USBAT_ATA_SECNUM;
1441 registers[3] = USBAT_ATA_LBA_ME;
1442 registers[4] = USBAT_ATA_LBA_HI;
1443 registers[5] = USBAT_ATA_DEVICE;
1444 registers[6] = USBAT_ATA_CMD;
1445 data[0] = 0x00;
1446 data[1] = 0x00;
1447 data[2] = 0x00;
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001448 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1449 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
1450 data[5] = 0xB0; /* (device sel) = slave */
1451 data[6] = 0xA0; /* (command) = ATA PACKET COMMAND */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 for (i=7; i<19; i++) {
1454 registers[i] = 0x10;
1455 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1456 }
1457
1458 result = usbat_get_status(us, status);
1459 US_DEBUGP("Status = %02X\n", *status);
1460 if (result != USB_STOR_XFER_GOOD)
1461 return USB_STOR_TRANSPORT_ERROR;
1462 if (srb->cmnd[0] == TEST_UNIT_READY)
1463 transferred = 0;
1464
1465 if (srb->sc_data_direction == DMA_TO_DEVICE) {
1466
1467 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1468 registers, data, 19,
1469 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1470 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1471 DMA_TO_DEVICE,
Boaz Harrosh4776e992007-09-09 20:40:56 +03001472 scsi_sglist(srb),
1473 len, scsi_sg_count(srb), 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 if (result == USB_STOR_TRANSPORT_GOOD) {
1476 transferred += len;
1477 US_DEBUGP("Wrote %08X bytes\n", transferred);
1478 }
1479
1480 return result;
1481
1482 } else if (srb->cmnd[0] == READ_10 ||
1483 srb->cmnd[0] == GPCMD_READ_CD) {
1484
1485 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1486
1487 }
1488
1489 if (len > 0xFFFF) {
1490 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1491 len);
1492 return USB_STOR_TRANSPORT_ERROR;
1493 }
1494
1495 if ( (result = usbat_multiple_write(us,
1496 registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
1497 return result;
1498 }
1499
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001500 /*
1501 * Write the 12-byte command header.
1502 *
1503 * If the command is BLANK then set the timer for 75 minutes.
1504 * Otherwise set it for 10 minutes.
1505 *
1506 * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1507 * AT SPEED 4 IS UNRELIABLE!!!
1508 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Peter Chubb141804d2006-05-02 18:30:12 +01001510 if ((result = usbat_write_block(us,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 USBAT_ATA, srb->cmnd, 12,
Peter Chubb141804d2006-05-02 18:30:12 +01001512 (srb->cmnd[0]==GPCMD_BLANK ? 75 : 10), 0) !=
1513 USB_STOR_TRANSPORT_GOOD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return result;
1515 }
1516
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001517 /* If there is response data to be read in then do it here. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1520
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001521 /* How many bytes to read in? Check cylL register */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1524 USB_STOR_XFER_GOOD) {
1525 return USB_STOR_TRANSPORT_ERROR;
1526 }
1527
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001528 if (len > 0xFF) { /* need to read cylH also */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 len = *status;
1530 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1531 USB_STOR_XFER_GOOD) {
1532 return USB_STOR_TRANSPORT_ERROR;
1533 }
1534 len += ((unsigned int) *status)<<8;
1535 }
1536 else
1537 len = *status;
1538
1539
Boaz Harrosh4776e992007-09-09 20:40:56 +03001540 result = usbat_read_block(us, scsi_sglist(srb), len,
1541 scsi_sg_count(srb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543
1544 return result;
1545}
1546
1547/*
1548 * Transport for USBAT02-based CompactFlash and similar storage devices
1549 */
1550static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1551{
1552 int rc;
1553 struct usbat_info *info = (struct usbat_info *) (us->extra);
1554 unsigned long block, blocks;
1555 unsigned char *ptr = us->iobuf;
1556 static unsigned char inquiry_response[36] = {
1557 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1558 };
1559
1560 if (srb->cmnd[0] == INQUIRY) {
1561 US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1562 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1563 fill_inquiry_response(us, ptr, 36);
1564 return USB_STOR_TRANSPORT_GOOD;
1565 }
1566
1567 if (srb->cmnd[0] == READ_CAPACITY) {
1568 rc = usbat_flash_check_media(us, info);
1569 if (rc != USB_STOR_TRANSPORT_GOOD)
1570 return rc;
1571
1572 rc = usbat_flash_get_sector_count(us, info);
1573 if (rc != USB_STOR_TRANSPORT_GOOD)
1574 return rc;
1575
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001576 /* hard coded 512 byte sectors as per ATA spec */
1577 info->ssize = 0x200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1579 info->sectors, info->ssize);
1580
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001581 /*
1582 * build the reply
1583 * note: must return the sector number of the last sector,
1584 * *not* the total number of sectors
1585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1587 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1588 usb_stor_set_xfer_buf(ptr, 8, srb);
1589
1590 return USB_STOR_TRANSPORT_GOOD;
1591 }
1592
1593 if (srb->cmnd[0] == MODE_SELECT_10) {
1594 US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
1595 return USB_STOR_TRANSPORT_ERROR;
1596 }
1597
1598 if (srb->cmnd[0] == READ_10) {
1599 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1600 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1601
1602 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1603
1604 US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
1605 return usbat_flash_read_data(us, info, block, blocks);
1606 }
1607
1608 if (srb->cmnd[0] == READ_12) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001609 /*
1610 * I don't think we'll ever see a READ_12 but support it anyway
1611 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1613 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1614
1615 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1616 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1617
1618 US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
1619 return usbat_flash_read_data(us, info, block, blocks);
1620 }
1621
1622 if (srb->cmnd[0] == WRITE_10) {
1623 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1624 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1625
1626 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1627
1628 US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
1629 return usbat_flash_write_data(us, info, block, blocks);
1630 }
1631
1632 if (srb->cmnd[0] == WRITE_12) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001633 /*
1634 * I don't think we'll ever see a WRITE_12 but support it anyway
1635 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1637 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1638
1639 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1640 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1641
1642 US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
1643 return usbat_flash_write_data(us, info, block, blocks);
1644 }
1645
1646
1647 if (srb->cmnd[0] == TEST_UNIT_READY) {
1648 US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
1649
1650 rc = usbat_flash_check_media(us, info);
1651 if (rc != USB_STOR_TRANSPORT_GOOD)
1652 return rc;
1653
1654 return usbat_check_status(us);
1655 }
1656
1657 if (srb->cmnd[0] == REQUEST_SENSE) {
1658 US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
1659
1660 memset(ptr, 0, 18);
1661 ptr[0] = 0xF0;
1662 ptr[2] = info->sense_key;
1663 ptr[7] = 11;
1664 ptr[12] = info->sense_asc;
1665 ptr[13] = info->sense_ascq;
1666 usb_stor_set_xfer_buf(ptr, 18, srb);
1667
1668 return USB_STOR_TRANSPORT_GOOD;
1669 }
1670
1671 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001672 /*
1673 * sure. whatever. not like we can stop the user from popping
1674 * the media out of the device (no locking doors, etc)
1675 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return USB_STOR_TRANSPORT_GOOD;
1677 }
1678
1679 US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1680 srb->cmnd[0], srb->cmnd[0]);
1681 info->sense_key = 0x05;
1682 info->sense_asc = 0x20;
1683 info->sense_ascq = 0x00;
1684 return USB_STOR_TRANSPORT_FAILED;
1685}
1686
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001687int init_usbat_cd(struct us_data *us)
1688{
1689 return init_usbat(us, USBAT_DEV_HP8200);
1690}
1691
1692
1693int init_usbat_flash(struct us_data *us)
1694{
1695 return init_usbat(us, USBAT_DEV_FLASH);
1696}
1697
1698int init_usbat_probe(struct us_data *us)
1699{
1700 return init_usbat(us, 0);
1701}
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703/*
1704 * Default transport function. Attempts to detect which transport function
1705 * should be called, makes it the new default, and calls it.
1706 *
1707 * This function should never be called. Our usbat_init() function detects the
1708 * device type and changes the us->transport ptr to the transport function
1709 * relevant to the device.
1710 * However, we'll support this impossible(?) case anyway.
1711 */
1712int usbat_transport(struct scsi_cmnd *srb, struct us_data *us)
1713{
1714 struct usbat_info *info = (struct usbat_info*) (us->extra);
1715
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001716 if (usbat_set_transport(us, info, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return USB_STOR_TRANSPORT_ERROR;
1718
1719 return us->transport(srb, us);
1720}