blob: d4fe0bb327a7c360c959a2028ec9bbbc9678d346 [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>
Alan Stern26d68182009-02-12 14:48:08 -050045#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/slab.h>
47#include <linux/cdrom.h>
48
49#include <scsi/scsi.h>
50#include <scsi/scsi_cmnd.h>
51
52#include "usb.h"
53#include "transport.h"
54#include "protocol.h"
55#include "debug.h"
Alan Stern26d68182009-02-12 14:48:08 -050056
57
58/* Supported device types */
59#define USBAT_DEV_HP8200 0x01
60#define USBAT_DEV_FLASH 0x02
61
62#define USBAT_EPP_PORT 0x10
63#define USBAT_EPP_REGISTER 0x30
64#define USBAT_ATA 0x40
65#define USBAT_ISA 0x50
66
67/* Commands (need to be logically OR'd with an access type */
68#define USBAT_CMD_READ_REG 0x00
69#define USBAT_CMD_WRITE_REG 0x01
70#define USBAT_CMD_READ_BLOCK 0x02
71#define USBAT_CMD_WRITE_BLOCK 0x03
72#define USBAT_CMD_COND_READ_BLOCK 0x04
73#define USBAT_CMD_COND_WRITE_BLOCK 0x05
74#define USBAT_CMD_WRITE_REGS 0x07
75
76/* Commands (these don't need an access type) */
77#define USBAT_CMD_EXEC_CMD 0x80
78#define USBAT_CMD_SET_FEAT 0x81
79#define USBAT_CMD_UIO 0x82
80
81/* Methods of accessing UIO register */
82#define USBAT_UIO_READ 1
83#define USBAT_UIO_WRITE 0
84
85/* Qualifier bits */
86#define USBAT_QUAL_FCQ 0x20 /* full compare */
87#define USBAT_QUAL_ALQ 0x10 /* auto load subcount */
88
89/* USBAT Flash Media status types */
90#define USBAT_FLASH_MEDIA_NONE 0
91#define USBAT_FLASH_MEDIA_CF 1
92
93/* USBAT Flash Media change types */
94#define USBAT_FLASH_MEDIA_SAME 0
95#define USBAT_FLASH_MEDIA_CHANGED 1
96
97/* USBAT ATA registers */
98#define USBAT_ATA_DATA 0x10 /* read/write data (R/W) */
99#define USBAT_ATA_FEATURES 0x11 /* set features (W) */
100#define USBAT_ATA_ERROR 0x11 /* error (R) */
101#define USBAT_ATA_SECCNT 0x12 /* sector count (R/W) */
102#define USBAT_ATA_SECNUM 0x13 /* sector number (R/W) */
103#define USBAT_ATA_LBA_ME 0x14 /* cylinder low (R/W) */
104#define USBAT_ATA_LBA_HI 0x15 /* cylinder high (R/W) */
105#define USBAT_ATA_DEVICE 0x16 /* head/device selection (R/W) */
106#define USBAT_ATA_STATUS 0x17 /* device status (R) */
107#define USBAT_ATA_CMD 0x17 /* device command (W) */
108#define USBAT_ATA_ALTSTATUS 0x0E /* status (no clear IRQ) (R) */
109
110/* USBAT User I/O Data registers */
111#define USBAT_UIO_EPAD 0x80 /* Enable Peripheral Control Signals */
112#define USBAT_UIO_CDT 0x40 /* Card Detect (Read Only) */
113 /* CDT = ACKD & !UI1 & !UI0 */
114#define USBAT_UIO_1 0x20 /* I/O 1 */
115#define USBAT_UIO_0 0x10 /* I/O 0 */
116#define USBAT_UIO_EPP_ATA 0x08 /* 1=EPP mode, 0=ATA mode */
117#define USBAT_UIO_UI1 0x04 /* Input 1 */
118#define USBAT_UIO_UI0 0x02 /* Input 0 */
119#define USBAT_UIO_INTR_ACK 0x01 /* Interrupt (ATA/ISA)/Acknowledge (EPP) */
120
121/* USBAT User I/O Enable registers */
122#define USBAT_UIO_DRVRST 0x80 /* Reset Peripheral */
123#define USBAT_UIO_ACKD 0x40 /* Enable Card Detect */
124#define USBAT_UIO_OE1 0x20 /* I/O 1 set=output/clr=input */
125 /* If ACKD=1, set OE1 to 1 also. */
126#define USBAT_UIO_OE0 0x10 /* I/O 0 set=output/clr=input */
127#define USBAT_UIO_ADPRST 0x01 /* Reset SCM chip */
128
129/* USBAT Features */
130#define USBAT_FEAT_ETEN 0x80 /* External trigger enable */
131#define USBAT_FEAT_U1 0x08
132#define USBAT_FEAT_U0 0x04
133#define USBAT_FEAT_ET1 0x02
134#define USBAT_FEAT_ET2 0x01
135
136struct usbat_info {
137 int devicetype;
138
139 /* Used for Flash readers only */
140 unsigned long sectors; /* total sector count */
141 unsigned long ssize; /* sector size in bytes */
142
143 unsigned char sense_key;
144 unsigned long sense_asc; /* additional sense code */
145 unsigned long sense_ascq; /* additional sense code qualifier */
146};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148#define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
149#define LSB_of(s) ((s)&0xFF)
150#define MSB_of(s) ((s)>>8)
151
152static int transferred = 0;
153
154static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us);
155static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us);
156
Alan Stern26d68182009-02-12 14:48:08 -0500157static int init_usbat_cd(struct us_data *us);
158static int init_usbat_flash(struct us_data *us);
159
160
161/*
162 * The table of devices
163 */
164#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
165 vendorName, productName, useProtocol, useTransport, \
166 initFunction, flags) \
167{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
168 .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
169
170struct usb_device_id usbat_usb_ids[] = {
171# include "unusual_usbat.h"
172 { } /* Terminating entry */
173};
174MODULE_DEVICE_TABLE(usb, usbat_usb_ids);
175
176#undef UNUSUAL_DEV
177
178/*
179 * The flags table
180 */
181#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
182 vendor_name, product_name, use_protocol, use_transport, \
183 init_function, Flags) \
184{ \
185 .vendorName = vendor_name, \
186 .productName = product_name, \
187 .useProtocol = use_protocol, \
188 .useTransport = use_transport, \
189 .initFunction = init_function, \
190}
191
192static struct us_unusual_dev usbat_unusual_dev_list[] = {
193# include "unusual_usbat.h"
194 { } /* Terminating entry */
195};
196
197#undef UNUSUAL_DEV
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/*
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100200 * Convenience function to produce an ATA read/write sectors command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * Use cmd=0x20 for read, cmd=0x30 for write
202 */
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100203static void usbat_pack_ata_sector_cmd(unsigned char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 unsigned char thistime,
205 u32 sector, unsigned char cmd)
206{
207 buf[0] = 0;
208 buf[1] = thistime;
209 buf[2] = sector & 0xFF;
210 buf[3] = (sector >> 8) & 0xFF;
211 buf[4] = (sector >> 16) & 0xFF;
212 buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
213 buf[6] = cmd;
214}
215
216/*
217 * Convenience function to get the device type (flash or hp8200)
218 */
219static int usbat_get_device_type(struct us_data *us)
220{
221 return ((struct usbat_info*)us->extra)->devicetype;
222}
223
224/*
225 * Read a register from the device
226 */
227static int usbat_read(struct us_data *us,
228 unsigned char access,
229 unsigned char reg,
230 unsigned char *content)
231{
232 return usb_stor_ctrl_transfer(us,
233 us->recv_ctrl_pipe,
234 access | USBAT_CMD_READ_REG,
235 0xC0,
236 (u16)reg,
237 0,
238 content,
239 1);
240}
241
242/*
243 * Write to a register on the device
244 */
245static int usbat_write(struct us_data *us,
246 unsigned char access,
247 unsigned char reg,
248 unsigned char content)
249{
250 return usb_stor_ctrl_transfer(us,
251 us->send_ctrl_pipe,
252 access | USBAT_CMD_WRITE_REG,
253 0x40,
254 short_pack(reg, content),
255 0,
256 NULL,
257 0);
258}
259
260/*
261 * Convenience function to perform a bulk read
262 */
263static int usbat_bulk_read(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300264 void* buf,
Peter Chubb141804d2006-05-02 18:30:12 +0100265 unsigned int len,
266 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 if (len == 0)
269 return USB_STOR_XFER_GOOD;
270
271 US_DEBUGP("usbat_bulk_read: len = %d\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +0300272 return usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe, buf, len, use_sg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275/*
276 * Convenience function to perform a bulk write
277 */
278static int usbat_bulk_write(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300279 void* buf,
Peter Chubb141804d2006-05-02 18:30:12 +0100280 unsigned int len,
281 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 if (len == 0)
284 return USB_STOR_XFER_GOOD;
285
286 US_DEBUGP("usbat_bulk_write: len = %d\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +0300287 return usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe, buf, len, use_sg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290/*
291 * Some USBAT-specific commands can only be executed over a command transport
292 * This transport allows one (len=8) or two (len=16) vendor-specific commands
293 * to be executed.
294 */
295static int usbat_execute_command(struct us_data *us,
296 unsigned char *commands,
297 unsigned int len)
298{
299 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
300 USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
301 commands, len);
302}
303
304/*
305 * Read the status register
306 */
307static int usbat_get_status(struct us_data *us, unsigned char *status)
308{
309 int rc;
310 rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
311
312 US_DEBUGP("usbat_get_status: 0x%02X\n", (unsigned short) (*status));
313 return rc;
314}
315
316/*
317 * Check the device status
318 */
319static int usbat_check_status(struct us_data *us)
320{
321 unsigned char *reply = us->iobuf;
322 int rc;
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 rc = usbat_get_status(us, reply);
325 if (rc != USB_STOR_XFER_GOOD)
326 return USB_STOR_TRANSPORT_FAILED;
327
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100328 /* error/check condition (0x51 is ok) */
329 if (*reply & 0x01 && *reply != 0x51)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return USB_STOR_TRANSPORT_FAILED;
331
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100332 /* device fault */
333 if (*reply & 0x20)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return USB_STOR_TRANSPORT_FAILED;
335
336 return USB_STOR_TRANSPORT_GOOD;
337}
338
339/*
340 * Stores critical information in internal registers in prepartion for the execution
341 * of a conditional usbat_read_blocks or usbat_write_blocks call.
342 */
343static int usbat_set_shuttle_features(struct us_data *us,
344 unsigned char external_trigger,
345 unsigned char epp_control,
346 unsigned char mask_byte,
347 unsigned char test_pattern,
348 unsigned char subcountH,
349 unsigned char subcountL)
350{
351 unsigned char *command = us->iobuf;
352
353 command[0] = 0x40;
354 command[1] = USBAT_CMD_SET_FEAT;
355
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100356 /*
357 * The only bit relevant to ATA access is bit 6
358 * which defines 8 bit data access (set) or 16 bit (unset)
359 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 command[2] = epp_control;
361
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100362 /*
363 * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
364 * ET1 and ET2 define an external event to be checked for on event of a
365 * _read_blocks or _write_blocks operation. The read/write will not take
366 * place unless the defined trigger signal is active.
367 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 command[3] = external_trigger;
369
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100370 /*
371 * The resultant byte of the mask operation (see mask_byte) is compared for
372 * equivalence with this test pattern. If equal, the read/write will take
373 * place.
374 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 command[4] = test_pattern;
376
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100377 /*
378 * This value is logically ANDed with the status register field specified
379 * in the read/write command.
380 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 command[5] = mask_byte;
382
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100383 /*
384 * If ALQ is set in the qualifier, this field contains the address of the
385 * registers where the byte count should be read for transferring the data.
386 * If ALQ is not set, then this field contains the number of bytes to be
387 * transferred.
388 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 command[6] = subcountL;
390 command[7] = subcountH;
391
392 return usbat_execute_command(us, command, 8);
393}
394
395/*
396 * Block, waiting for an ATA device to become not busy or to report
397 * an error condition.
398 */
399static int usbat_wait_not_busy(struct us_data *us, int minutes)
400{
401 int i;
402 int result;
403 unsigned char *status = us->iobuf;
404
405 /* Synchronizing cache on a CDR could take a heck of a long time,
406 * but probably not more than 10 minutes or so. On the other hand,
407 * doing a full blank on a CDRW at speed 1 will take about 75
408 * minutes!
409 */
410
411 for (i=0; i<1200+minutes*60; i++) {
412
413 result = usbat_get_status(us, status);
414
415 if (result!=USB_STOR_XFER_GOOD)
416 return USB_STOR_TRANSPORT_ERROR;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100417 if (*status & 0x01) { /* check condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 result = usbat_read(us, USBAT_ATA, 0x10, status);
419 return USB_STOR_TRANSPORT_FAILED;
420 }
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100421 if (*status & 0x20) /* device fault */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return USB_STOR_TRANSPORT_FAILED;
423
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100424 if ((*status & 0x80)==0x00) { /* not busy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 US_DEBUGP("Waited not busy for %d steps\n", i);
426 return USB_STOR_TRANSPORT_GOOD;
427 }
428
429 if (i<500)
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100430 msleep(10); /* 5 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 else if (i<700)
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100432 msleep(50); /* 10 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 else if (i<1200)
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100434 msleep(100); /* 50 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 else
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100436 msleep(1000); /* X minutes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438
439 US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
440 minutes);
441 return USB_STOR_TRANSPORT_FAILED;
442}
443
444/*
445 * Read block data from the data register
446 */
447static int usbat_read_block(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300448 void* buf,
Peter Chubb141804d2006-05-02 18:30:12 +0100449 unsigned short len,
450 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 int result;
453 unsigned char *command = us->iobuf;
454
455 if (!len)
456 return USB_STOR_TRANSPORT_GOOD;
457
458 command[0] = 0xC0;
459 command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
460 command[2] = USBAT_ATA_DATA;
461 command[3] = 0;
462 command[4] = 0;
463 command[5] = 0;
464 command[6] = LSB_of(len);
465 command[7] = MSB_of(len);
466
467 result = usbat_execute_command(us, command, 8);
468 if (result != USB_STOR_XFER_GOOD)
469 return USB_STOR_TRANSPORT_ERROR;
470
Boaz Harrosh4776e992007-09-09 20:40:56 +0300471 result = usbat_bulk_read(us, buf, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return (result == USB_STOR_XFER_GOOD ?
473 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
474}
475
476/*
477 * Write block data via the data register
478 */
479static int usbat_write_block(struct us_data *us,
480 unsigned char access,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300481 void* buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 unsigned short len,
Peter Chubb141804d2006-05-02 18:30:12 +0100483 int minutes,
484 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 int result;
487 unsigned char *command = us->iobuf;
488
489 if (!len)
490 return USB_STOR_TRANSPORT_GOOD;
491
492 command[0] = 0x40;
493 command[1] = access | USBAT_CMD_WRITE_BLOCK;
494 command[2] = USBAT_ATA_DATA;
495 command[3] = 0;
496 command[4] = 0;
497 command[5] = 0;
498 command[6] = LSB_of(len);
499 command[7] = MSB_of(len);
500
501 result = usbat_execute_command(us, command, 8);
502
503 if (result != USB_STOR_XFER_GOOD)
504 return USB_STOR_TRANSPORT_ERROR;
505
Boaz Harrosh4776e992007-09-09 20:40:56 +0300506 result = usbat_bulk_write(us, buf, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (result != USB_STOR_XFER_GOOD)
508 return USB_STOR_TRANSPORT_ERROR;
509
510 return usbat_wait_not_busy(us, minutes);
511}
512
513/*
514 * Process read and write requests
515 */
516static int usbat_hp8200e_rw_block_test(struct us_data *us,
517 unsigned char access,
518 unsigned char *registers,
519 unsigned char *data_out,
520 unsigned short num_registers,
521 unsigned char data_reg,
522 unsigned char status_reg,
523 unsigned char timeout,
524 unsigned char qualifier,
525 int direction,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300526 void *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 unsigned short len,
528 int use_sg,
529 int minutes)
530{
531 int result;
532 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
533 us->recv_bulk_pipe : us->send_bulk_pipe;
534
535 unsigned char *command = us->iobuf;
536 int i, j;
537 int cmdlen;
538 unsigned char *data = us->iobuf;
539 unsigned char *status = us->iobuf;
540
541 BUG_ON(num_registers > US_IOBUF_SIZE/2);
542
543 for (i=0; i<20; i++) {
544
545 /*
546 * The first time we send the full command, which consists
547 * of downloading the SCSI command followed by downloading
548 * the data via a write-and-test. Any other time we only
549 * send the command to download the data -- the SCSI command
550 * is still 'active' in some sense in the device.
551 *
552 * We're only going to try sending the data 10 times. After
553 * that, we just return a failure.
554 */
555
556 if (i==0) {
557 cmdlen = 16;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100558 /*
559 * Write to multiple registers
560 * Not really sure the 0x07, 0x17, 0xfc, 0xe7 is
561 * necessary here, but that's what came out of the
562 * trace every single time.
563 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 command[0] = 0x40;
565 command[1] = access | USBAT_CMD_WRITE_REGS;
566 command[2] = 0x07;
567 command[3] = 0x17;
568 command[4] = 0xFC;
569 command[5] = 0xE7;
570 command[6] = LSB_of(num_registers*2);
571 command[7] = MSB_of(num_registers*2);
572 } else
573 cmdlen = 8;
574
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100575 /* Conditionally read or write blocks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 command[cmdlen-8] = (direction==DMA_TO_DEVICE ? 0x40 : 0xC0);
577 command[cmdlen-7] = access |
578 (direction==DMA_TO_DEVICE ?
579 USBAT_CMD_COND_WRITE_BLOCK : USBAT_CMD_COND_READ_BLOCK);
580 command[cmdlen-6] = data_reg;
581 command[cmdlen-5] = status_reg;
582 command[cmdlen-4] = timeout;
583 command[cmdlen-3] = qualifier;
584 command[cmdlen-2] = LSB_of(len);
585 command[cmdlen-1] = MSB_of(len);
586
587 result = usbat_execute_command(us, command, cmdlen);
588
589 if (result != USB_STOR_XFER_GOOD)
590 return USB_STOR_TRANSPORT_ERROR;
591
592 if (i==0) {
593
594 for (j=0; j<num_registers; j++) {
595 data[j<<1] = registers[j];
596 data[1+(j<<1)] = data_out[j];
597 }
598
Peter Chubb141804d2006-05-02 18:30:12 +0100599 result = usbat_bulk_write(us, data, num_registers*2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (result != USB_STOR_XFER_GOOD)
601 return USB_STOR_TRANSPORT_ERROR;
602
603 }
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 result = usb_stor_bulk_transfer_sg(us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300606 pipe, buf, len, use_sg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /*
609 * If we get a stall on the bulk download, we'll retry
610 * the bulk download -- but not the SCSI command because
611 * in some sense the SCSI command is still 'active' and
612 * waiting for the data. Don't ask me why this should be;
613 * I'm only following what the Windoze driver did.
614 *
615 * Note that a stall for the test-and-read/write command means
616 * that the test failed. In this case we're testing to make
617 * sure that the device is error-free
618 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
619 * hypothesis is that the USBAT chip somehow knows what
620 * the device will accept, but doesn't give the device any
621 * data until all data is received. Thus, the device would
622 * still be waiting for the first byte of data if a stall
623 * occurs, even if the stall implies that some data was
624 * transferred.
625 */
626
627 if (result == USB_STOR_XFER_SHORT ||
628 result == USB_STOR_XFER_STALLED) {
629
630 /*
631 * If we're reading and we stalled, then clear
632 * the bulk output pipe only the first time.
633 */
634
635 if (direction==DMA_FROM_DEVICE && i==0) {
636 if (usb_stor_clear_halt(us,
637 us->send_bulk_pipe) < 0)
638 return USB_STOR_TRANSPORT_ERROR;
639 }
640
641 /*
642 * Read status: is the device angry, or just busy?
643 */
644
645 result = usbat_read(us, USBAT_ATA,
646 direction==DMA_TO_DEVICE ?
647 USBAT_ATA_STATUS : USBAT_ATA_ALTSTATUS,
648 status);
649
650 if (result!=USB_STOR_XFER_GOOD)
651 return USB_STOR_TRANSPORT_ERROR;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100652 if (*status & 0x01) /* check condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return USB_STOR_TRANSPORT_FAILED;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100654 if (*status & 0x20) /* device fault */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return USB_STOR_TRANSPORT_FAILED;
656
657 US_DEBUGP("Redoing %s\n",
658 direction==DMA_TO_DEVICE ? "write" : "read");
659
660 } else if (result != USB_STOR_XFER_GOOD)
661 return USB_STOR_TRANSPORT_ERROR;
662 else
663 return usbat_wait_not_busy(us, minutes);
664
665 }
666
667 US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
668 direction==DMA_TO_DEVICE ? "Writing" : "Reading");
669
670 return USB_STOR_TRANSPORT_FAILED;
671}
672
673/*
674 * Write to multiple registers:
675 * Allows us to write specific data to any registers. The data to be written
676 * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN
677 * which gets sent through bulk out.
678 * Not designed for large transfers of data!
679 */
680static int usbat_multiple_write(struct us_data *us,
681 unsigned char *registers,
682 unsigned char *data_out,
683 unsigned short num_registers)
684{
685 int i, result;
686 unsigned char *data = us->iobuf;
687 unsigned char *command = us->iobuf;
688
689 BUG_ON(num_registers > US_IOBUF_SIZE/2);
690
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100691 /* Write to multiple registers, ATA access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 command[0] = 0x40;
693 command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
694
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100695 /* No relevance */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 command[2] = 0;
697 command[3] = 0;
698 command[4] = 0;
699 command[5] = 0;
700
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100701 /* Number of bytes to be transferred (incl. addresses and data) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 command[6] = LSB_of(num_registers*2);
703 command[7] = MSB_of(num_registers*2);
704
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100705 /* The setup command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 result = usbat_execute_command(us, command, 8);
707 if (result != USB_STOR_XFER_GOOD)
708 return USB_STOR_TRANSPORT_ERROR;
709
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100710 /* Create the reg/data, reg/data sequence */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 for (i=0; i<num_registers; i++) {
712 data[i<<1] = registers[i];
713 data[1+(i<<1)] = data_out[i];
714 }
715
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100716 /* Send the data */
Peter Chubb141804d2006-05-02 18:30:12 +0100717 result = usbat_bulk_write(us, data, num_registers*2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (result != USB_STOR_XFER_GOOD)
719 return USB_STOR_TRANSPORT_ERROR;
720
721 if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
722 return usbat_wait_not_busy(us, 0);
723 else
724 return USB_STOR_TRANSPORT_GOOD;
725}
726
727/*
728 * Conditionally read blocks from device:
729 * Allows us to read blocks from a specific data register, based upon the
730 * condition that a status register can be successfully masked with a status
731 * qualifier. If this condition is not initially met, the read will wait
732 * up until a maximum amount of time has elapsed, as specified by timeout.
733 * The read will start when the condition is met, otherwise the command aborts.
734 *
735 * The qualifier defined here is not the value that is masked, it defines
736 * conditions for the write to take place. The actual masked qualifier (and
737 * other related details) are defined beforehand with _set_shuttle_features().
738 */
739static int usbat_read_blocks(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300740 void* buffer,
Peter Chubb141804d2006-05-02 18:30:12 +0100741 int len,
742 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 int result;
745 unsigned char *command = us->iobuf;
746
747 command[0] = 0xC0;
748 command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK;
749 command[2] = USBAT_ATA_DATA;
750 command[3] = USBAT_ATA_STATUS;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100751 command[4] = 0xFD; /* Timeout (ms); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 command[5] = USBAT_QUAL_FCQ;
753 command[6] = LSB_of(len);
754 command[7] = MSB_of(len);
755
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100756 /* Multiple block read setup command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 result = usbat_execute_command(us, command, 8);
758 if (result != USB_STOR_XFER_GOOD)
759 return USB_STOR_TRANSPORT_FAILED;
760
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100761 /* Read the blocks we just asked for */
Peter Chubb141804d2006-05-02 18:30:12 +0100762 result = usbat_bulk_read(us, buffer, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (result != USB_STOR_XFER_GOOD)
764 return USB_STOR_TRANSPORT_FAILED;
765
766 return USB_STOR_TRANSPORT_GOOD;
767}
768
769/*
770 * Conditionally write blocks to device:
771 * Allows us to write blocks to a specific data register, based upon the
772 * condition that a status register can be successfully masked with a status
773 * qualifier. If this condition is not initially met, the write will wait
774 * up until a maximum amount of time has elapsed, as specified by timeout.
775 * The read will start when the condition is met, otherwise the command aborts.
776 *
777 * The qualifier defined here is not the value that is masked, it defines
778 * conditions for the write to take place. The actual masked qualifier (and
779 * other related details) are defined beforehand with _set_shuttle_features().
780 */
781static int usbat_write_blocks(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300782 void* buffer,
Peter Chubb141804d2006-05-02 18:30:12 +0100783 int len,
784 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 int result;
787 unsigned char *command = us->iobuf;
788
789 command[0] = 0x40;
790 command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK;
791 command[2] = USBAT_ATA_DATA;
792 command[3] = USBAT_ATA_STATUS;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100793 command[4] = 0xFD; /* Timeout (ms) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 command[5] = USBAT_QUAL_FCQ;
795 command[6] = LSB_of(len);
796 command[7] = MSB_of(len);
797
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100798 /* Multiple block write setup command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 result = usbat_execute_command(us, command, 8);
800 if (result != USB_STOR_XFER_GOOD)
801 return USB_STOR_TRANSPORT_FAILED;
802
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100803 /* Write the data */
Peter Chubb141804d2006-05-02 18:30:12 +0100804 result = usbat_bulk_write(us, buffer, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (result != USB_STOR_XFER_GOOD)
806 return USB_STOR_TRANSPORT_FAILED;
807
808 return USB_STOR_TRANSPORT_GOOD;
809}
810
811/*
812 * Read the User IO register
813 */
814static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
815{
816 int result;
817
818 result = usb_stor_ctrl_transfer(us,
819 us->recv_ctrl_pipe,
820 USBAT_CMD_UIO,
821 0xC0,
822 0,
823 0,
824 data_flags,
825 USBAT_UIO_READ);
826
827 US_DEBUGP("usbat_read_user_io: UIO register reads %02X\n", (unsigned short) (*data_flags));
828
829 return result;
830}
831
832/*
833 * Write to the User IO register
834 */
835static int usbat_write_user_io(struct us_data *us,
836 unsigned char enable_flags,
837 unsigned char data_flags)
838{
839 return usb_stor_ctrl_transfer(us,
840 us->send_ctrl_pipe,
841 USBAT_CMD_UIO,
842 0x40,
843 short_pack(enable_flags, data_flags),
844 0,
845 NULL,
846 USBAT_UIO_WRITE);
847}
848
849/*
850 * Reset the device
851 * Often needed on media change.
852 */
853static int usbat_device_reset(struct us_data *us)
854{
855 int rc;
856
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100857 /*
858 * Reset peripheral, enable peripheral control signals
859 * (bring reset signal up)
860 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 rc = usbat_write_user_io(us,
862 USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
863 USBAT_UIO_EPAD | USBAT_UIO_1);
864 if (rc != USB_STOR_XFER_GOOD)
865 return USB_STOR_TRANSPORT_ERROR;
866
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100867 /*
868 * Enable peripheral control signals
869 * (bring reset signal down)
870 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 rc = usbat_write_user_io(us,
872 USBAT_UIO_OE1 | USBAT_UIO_OE0,
873 USBAT_UIO_EPAD | USBAT_UIO_1);
874 if (rc != USB_STOR_XFER_GOOD)
875 return USB_STOR_TRANSPORT_ERROR;
876
877 return USB_STOR_TRANSPORT_GOOD;
878}
879
880/*
881 * Enable card detect
882 */
883static int usbat_device_enable_cdt(struct us_data *us)
884{
885 int rc;
886
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100887 /* Enable peripheral control signals and card detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 rc = usbat_write_user_io(us,
889 USBAT_UIO_ACKD | USBAT_UIO_OE1 | USBAT_UIO_OE0,
890 USBAT_UIO_EPAD | USBAT_UIO_1);
891 if (rc != USB_STOR_XFER_GOOD)
892 return USB_STOR_TRANSPORT_ERROR;
893
894 return USB_STOR_TRANSPORT_GOOD;
895}
896
897/*
898 * Determine if media is present.
899 */
900static int usbat_flash_check_media_present(unsigned char *uio)
901{
902 if (*uio & USBAT_UIO_UI0) {
903 US_DEBUGP("usbat_flash_check_media_present: no media detected\n");
904 return USBAT_FLASH_MEDIA_NONE;
905 }
906
907 return USBAT_FLASH_MEDIA_CF;
908}
909
910/*
911 * Determine if media has changed since last operation
912 */
913static int usbat_flash_check_media_changed(unsigned char *uio)
914{
915 if (*uio & USBAT_UIO_0) {
916 US_DEBUGP("usbat_flash_check_media_changed: media change detected\n");
917 return USBAT_FLASH_MEDIA_CHANGED;
918 }
919
920 return USBAT_FLASH_MEDIA_SAME;
921}
922
923/*
924 * Check for media change / no media and handle the situation appropriately
925 */
926static int usbat_flash_check_media(struct us_data *us,
927 struct usbat_info *info)
928{
929 int rc;
930 unsigned char *uio = us->iobuf;
931
932 rc = usbat_read_user_io(us, uio);
933 if (rc != USB_STOR_XFER_GOOD)
934 return USB_STOR_TRANSPORT_ERROR;
935
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100936 /* Check for media existence */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 rc = usbat_flash_check_media_present(uio);
938 if (rc == USBAT_FLASH_MEDIA_NONE) {
939 info->sense_key = 0x02;
940 info->sense_asc = 0x3A;
941 info->sense_ascq = 0x00;
942 return USB_STOR_TRANSPORT_FAILED;
943 }
944
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100945 /* Check for media change */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 rc = usbat_flash_check_media_changed(uio);
947 if (rc == USBAT_FLASH_MEDIA_CHANGED) {
948
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100949 /* Reset and re-enable card detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 rc = usbat_device_reset(us);
951 if (rc != USB_STOR_TRANSPORT_GOOD)
952 return rc;
953 rc = usbat_device_enable_cdt(us);
954 if (rc != USB_STOR_TRANSPORT_GOOD)
955 return rc;
956
957 msleep(50);
958
959 rc = usbat_read_user_io(us, uio);
960 if (rc != USB_STOR_XFER_GOOD)
961 return USB_STOR_TRANSPORT_ERROR;
962
963 info->sense_key = UNIT_ATTENTION;
964 info->sense_asc = 0x28;
965 info->sense_ascq = 0x00;
966 return USB_STOR_TRANSPORT_FAILED;
967 }
968
969 return USB_STOR_TRANSPORT_GOOD;
970}
971
972/*
973 * Determine whether we are controlling a flash-based reader/writer,
974 * or a HP8200-based CD drive.
975 * Sets transport functions as appropriate.
976 */
977static int usbat_identify_device(struct us_data *us,
978 struct usbat_info *info)
979{
980 int rc;
981 unsigned char status;
982
983 if (!us || !info)
984 return USB_STOR_TRANSPORT_ERROR;
985
986 rc = usbat_device_reset(us);
987 if (rc != USB_STOR_TRANSPORT_GOOD)
988 return rc;
Daniel Drake8845add2005-11-17 09:48:01 -0800989 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 /*
Daniel Drake68a64572005-08-10 18:30:04 +0100992 * In attempt to distinguish between HP CDRW's and Flash readers, we now
993 * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
994 * readers), this command should fail with error. On ATAPI devices (i.e.
995 * CDROM drives), it should succeed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 */
Daniel Drake68a64572005-08-10 18:30:04 +0100997 rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1);
998 if (rc != USB_STOR_XFER_GOOD)
999 return USB_STOR_TRANSPORT_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Daniel Drake68a64572005-08-10 18:30:04 +01001001 rc = usbat_get_status(us, &status);
1002 if (rc != USB_STOR_XFER_GOOD)
1003 return USB_STOR_TRANSPORT_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001005 /* Check for error bit, or if the command 'fell through' */
Daniel Drakea8798532005-09-29 00:14:21 +01001006 if (status == 0xA1 || !(status & 0x01)) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001007 /* Device is HP 8200 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 US_DEBUGP("usbat_identify_device: Detected HP8200 CDRW\n");
1009 info->devicetype = USBAT_DEV_HP8200;
Daniel Drakea8798532005-09-29 00:14:21 +01001010 } else {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001011 /* Device is a CompactFlash reader/writer */
Daniel Drakea8798532005-09-29 00:14:21 +01001012 US_DEBUGP("usbat_identify_device: Detected Flash reader/writer\n");
1013 info->devicetype = USBAT_DEV_FLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015
1016 return USB_STOR_TRANSPORT_GOOD;
1017}
1018
1019/*
1020 * Set the transport function based on the device type
1021 */
1022static int usbat_set_transport(struct us_data *us,
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001023 struct usbat_info *info,
1024 int devicetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001027 if (!info->devicetype)
1028 info->devicetype = devicetype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001030 if (!info->devicetype)
1031 usbat_identify_device(us, info);
1032
1033 switch (info->devicetype) {
1034 default:
1035 return USB_STOR_TRANSPORT_ERROR;
1036
1037 case USBAT_DEV_HP8200:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 us->transport = usbat_hp8200e_transport;
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001039 break;
1040
1041 case USBAT_DEV_FLASH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 us->transport = usbat_flash_transport;
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001043 break;
1044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 return 0;
1047}
1048
1049/*
1050 * Read the media capacity
1051 */
1052static int usbat_flash_get_sector_count(struct us_data *us,
1053 struct usbat_info *info)
1054{
1055 unsigned char registers[3] = {
1056 USBAT_ATA_SECCNT,
1057 USBAT_ATA_DEVICE,
1058 USBAT_ATA_CMD,
1059 };
1060 unsigned char command[3] = { 0x01, 0xA0, 0xEC };
1061 unsigned char *reply;
1062 unsigned char status;
1063 int rc;
1064
1065 if (!us || !info)
1066 return USB_STOR_TRANSPORT_ERROR;
1067
1068 reply = kmalloc(512, GFP_NOIO);
1069 if (!reply)
1070 return USB_STOR_TRANSPORT_ERROR;
1071
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001072 /* ATA command : IDENTIFY DEVICE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 rc = usbat_multiple_write(us, registers, command, 3);
1074 if (rc != USB_STOR_XFER_GOOD) {
1075 US_DEBUGP("usbat_flash_get_sector_count: Gah! identify_device failed\n");
1076 rc = USB_STOR_TRANSPORT_ERROR;
1077 goto leave;
1078 }
1079
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001080 /* Read device status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
1082 rc = USB_STOR_TRANSPORT_ERROR;
1083 goto leave;
1084 }
1085
1086 msleep(100);
1087
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001088 /* Read the device identification data */
Peter Chubb141804d2006-05-02 18:30:12 +01001089 rc = usbat_read_block(us, reply, 512, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (rc != USB_STOR_TRANSPORT_GOOD)
1091 goto leave;
1092
1093 info->sectors = ((u32)(reply[117]) << 24) |
1094 ((u32)(reply[116]) << 16) |
1095 ((u32)(reply[115]) << 8) |
1096 ((u32)(reply[114]) );
1097
1098 rc = USB_STOR_TRANSPORT_GOOD;
1099
1100 leave:
1101 kfree(reply);
1102 return rc;
1103}
1104
1105/*
1106 * Read data from device
1107 */
1108static int usbat_flash_read_data(struct us_data *us,
1109 struct usbat_info *info,
1110 u32 sector,
1111 u32 sectors)
1112{
1113 unsigned char registers[7] = {
1114 USBAT_ATA_FEATURES,
1115 USBAT_ATA_SECCNT,
1116 USBAT_ATA_SECNUM,
1117 USBAT_ATA_LBA_ME,
1118 USBAT_ATA_LBA_HI,
1119 USBAT_ATA_DEVICE,
1120 USBAT_ATA_STATUS,
1121 };
1122 unsigned char command[7];
1123 unsigned char *buffer;
1124 unsigned char thistime;
1125 unsigned int totallen, alloclen;
1126 int len, result;
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001127 unsigned int sg_offset = 0;
1128 struct scatterlist *sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 result = usbat_flash_check_media(us, info);
1131 if (result != USB_STOR_TRANSPORT_GOOD)
1132 return result;
1133
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001134 /*
1135 * we're working in LBA mode. according to the ATA spec,
1136 * we can support up to 28-bit addressing. I don't know if Jumpshot
1137 * supports beyond 24-bit addressing. It's kind of hard to test
1138 * since it requires > 8GB CF card.
1139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 if (sector > 0x0FFFFFFF)
1142 return USB_STOR_TRANSPORT_ERROR;
1143
1144 totallen = sectors * info->ssize;
1145
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001146 /*
1147 * Since we don't read more than 64 KB at a time, we have to create
1148 * a bounce buffer and move the data a piece at a time between the
1149 * bounce buffer and the actual transfer buffer.
1150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 alloclen = min(totallen, 65536u);
1153 buffer = kmalloc(alloclen, GFP_NOIO);
1154 if (buffer == NULL)
1155 return USB_STOR_TRANSPORT_ERROR;
1156
1157 do {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001158 /*
1159 * loop, never allocate or transfer more than 64k at once
1160 * (min(128k, 255*info->ssize) is the real limit)
1161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 len = min(totallen, alloclen);
1163 thistime = (len / info->ssize) & 0xff;
1164
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001165 /* ATA command 0x20 (READ SECTORS) */
1166 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001168 /* Write/execute ATA read command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 result = usbat_multiple_write(us, registers, command, 7);
1170 if (result != USB_STOR_TRANSPORT_GOOD)
1171 goto leave;
1172
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001173 /* Read the data we just requested */
Peter Chubb141804d2006-05-02 18:30:12 +01001174 result = usbat_read_blocks(us, buffer, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (result != USB_STOR_TRANSPORT_GOOD)
1176 goto leave;
1177
1178 US_DEBUGP("usbat_flash_read_data: %d bytes\n", len);
1179
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001180 /* Store the data in the transfer buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 usb_stor_access_xfer_buf(buffer, len, us->srb,
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001182 &sg, &sg_offset, TO_XFER_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 sector += thistime;
1185 totallen -= len;
1186 } while (totallen > 0);
1187
1188 kfree(buffer);
1189 return USB_STOR_TRANSPORT_GOOD;
1190
1191leave:
1192 kfree(buffer);
1193 return USB_STOR_TRANSPORT_ERROR;
1194}
1195
1196/*
1197 * Write data to device
1198 */
1199static int usbat_flash_write_data(struct us_data *us,
1200 struct usbat_info *info,
1201 u32 sector,
1202 u32 sectors)
1203{
1204 unsigned char registers[7] = {
1205 USBAT_ATA_FEATURES,
1206 USBAT_ATA_SECCNT,
1207 USBAT_ATA_SECNUM,
1208 USBAT_ATA_LBA_ME,
1209 USBAT_ATA_LBA_HI,
1210 USBAT_ATA_DEVICE,
1211 USBAT_ATA_STATUS,
1212 };
1213 unsigned char command[7];
1214 unsigned char *buffer;
1215 unsigned char thistime;
1216 unsigned int totallen, alloclen;
1217 int len, result;
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001218 unsigned int sg_offset = 0;
1219 struct scatterlist *sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 result = usbat_flash_check_media(us, info);
1222 if (result != USB_STOR_TRANSPORT_GOOD)
1223 return result;
1224
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001225 /*
1226 * we're working in LBA mode. according to the ATA spec,
1227 * we can support up to 28-bit addressing. I don't know if the device
1228 * supports beyond 24-bit addressing. It's kind of hard to test
1229 * since it requires > 8GB media.
1230 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 if (sector > 0x0FFFFFFF)
1233 return USB_STOR_TRANSPORT_ERROR;
1234
1235 totallen = sectors * info->ssize;
1236
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001237 /*
1238 * Since we don't write more than 64 KB at a time, we have to create
1239 * a bounce buffer and move the data a piece at a time between the
1240 * bounce buffer and the actual transfer buffer.
1241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 alloclen = min(totallen, 65536u);
1244 buffer = kmalloc(alloclen, GFP_NOIO);
1245 if (buffer == NULL)
1246 return USB_STOR_TRANSPORT_ERROR;
1247
1248 do {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001249 /*
1250 * loop, never allocate or transfer more than 64k at once
1251 * (min(128k, 255*info->ssize) is the real limit)
1252 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 len = min(totallen, alloclen);
1254 thistime = (len / info->ssize) & 0xff;
1255
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001256 /* Get the data from the transfer buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 usb_stor_access_xfer_buf(buffer, len, us->srb,
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001258 &sg, &sg_offset, FROM_XFER_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001260 /* ATA command 0x30 (WRITE SECTORS) */
1261 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001263 /* Write/execute ATA write command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 result = usbat_multiple_write(us, registers, command, 7);
1265 if (result != USB_STOR_TRANSPORT_GOOD)
1266 goto leave;
1267
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001268 /* Write the data */
Peter Chubb141804d2006-05-02 18:30:12 +01001269 result = usbat_write_blocks(us, buffer, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (result != USB_STOR_TRANSPORT_GOOD)
1271 goto leave;
1272
1273 sector += thistime;
1274 totallen -= len;
1275 } while (totallen > 0);
1276
1277 kfree(buffer);
1278 return result;
1279
1280leave:
1281 kfree(buffer);
1282 return USB_STOR_TRANSPORT_ERROR;
1283}
1284
1285/*
1286 * Squeeze a potentially huge (> 65535 byte) read10 command into
1287 * a little ( <= 65535 byte) ATAPI pipe
1288 */
1289static int usbat_hp8200e_handle_read10(struct us_data *us,
1290 unsigned char *registers,
1291 unsigned char *data,
1292 struct scsi_cmnd *srb)
1293{
1294 int result = USB_STOR_TRANSPORT_GOOD;
1295 unsigned char *buffer;
1296 unsigned int len;
1297 unsigned int sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 unsigned int sg_offset = 0;
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001299 struct scatterlist *sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 US_DEBUGP("handle_read10: transfersize %d\n",
1302 srb->transfersize);
1303
Boaz Harrosh4776e992007-09-09 20:40:56 +03001304 if (scsi_bufflen(srb) < 0x10000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1307 registers, data, 19,
1308 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1309 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1310 DMA_FROM_DEVICE,
Boaz Harrosh4776e992007-09-09 20:40:56 +03001311 scsi_sglist(srb),
1312 scsi_bufflen(srb), scsi_sg_count(srb), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 return result;
1315 }
1316
1317 /*
1318 * Since we're requesting more data than we can handle in
1319 * a single read command (max is 64k-1), we will perform
1320 * multiple reads, but each read must be in multiples of
1321 * a sector. Luckily the sector size is in srb->transfersize
1322 * (see linux/drivers/scsi/sr.c).
1323 */
1324
1325 if (data[7+0] == GPCMD_READ_CD) {
1326 len = short_pack(data[7+9], data[7+8]);
1327 len <<= 16;
1328 len |= data[7+7];
1329 US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +03001330 srb->transfersize = scsi_bufflen(srb)/len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332
1333 if (!srb->transfersize) {
1334 srb->transfersize = 2048; /* A guess */
1335 US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
1336 srb->transfersize);
1337 }
1338
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001339 /*
1340 * Since we only read in one block at a time, we have to create
1341 * a bounce buffer and move the data a piece at a time between the
1342 * bounce buffer and the actual transfer buffer.
1343 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 len = (65535/srb->transfersize) * srb->transfersize;
1346 US_DEBUGP("Max read is %d bytes\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +03001347 len = min(len, scsi_bufflen(srb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 buffer = kmalloc(len, GFP_NOIO);
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001349 if (buffer == NULL) /* bloody hell! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return USB_STOR_TRANSPORT_FAILED;
1351 sector = short_pack(data[7+3], data[7+2]);
1352 sector <<= 16;
1353 sector |= short_pack(data[7+5], data[7+4]);
1354 transferred = 0;
1355
Boaz Harrosh4776e992007-09-09 20:40:56 +03001356 while (transferred != scsi_bufflen(srb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Boaz Harrosh4776e992007-09-09 20:40:56 +03001358 if (len > scsi_bufflen(srb) - transferred)
1359 len = scsi_bufflen(srb) - transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001361 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1362 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001364 /* Fix up the SCSI command sector and num sectors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001366 data[7+2] = MSB_of(sector>>16); /* SCSI command sector */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 data[7+3] = LSB_of(sector>>16);
1368 data[7+4] = MSB_of(sector&0xFFFF);
1369 data[7+5] = LSB_of(sector&0xFFFF);
1370 if (data[7+0] == GPCMD_READ_CD)
1371 data[7+6] = 0;
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001372 data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */
1373 data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1376 registers, data, 19,
1377 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1378 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1379 DMA_FROM_DEVICE,
1380 buffer,
1381 len, 0, 1);
1382
1383 if (result != USB_STOR_TRANSPORT_GOOD)
1384 break;
1385
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001386 /* Store the data in the transfer buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 usb_stor_access_xfer_buf(buffer, len, srb,
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001388 &sg, &sg_offset, TO_XFER_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001390 /* Update the amount transferred and the sector number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 transferred += len;
1393 sector += len / srb->transfersize;
1394
Boaz Harrosh4776e992007-09-09 20:40:56 +03001395 } /* while transferred != scsi_bufflen(srb) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 kfree(buffer);
1398 return result;
1399}
1400
1401static int usbat_select_and_test_registers(struct us_data *us)
1402{
1403 int selector;
1404 unsigned char *status = us->iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001406 /* try device = master, then device = slave. */
Daniel Drake68a64572005-08-10 18:30:04 +01001407 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
1408 if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 USB_STOR_XFER_GOOD)
1410 return USB_STOR_TRANSPORT_ERROR;
1411
1412 if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) !=
1413 USB_STOR_XFER_GOOD)
1414 return USB_STOR_TRANSPORT_ERROR;
1415
1416 if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) !=
1417 USB_STOR_XFER_GOOD)
1418 return USB_STOR_TRANSPORT_ERROR;
1419
1420 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1421 USB_STOR_XFER_GOOD)
1422 return USB_STOR_TRANSPORT_ERROR;
1423
1424 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1425 USB_STOR_XFER_GOOD)
1426 return USB_STOR_TRANSPORT_ERROR;
1427
1428 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) !=
1429 USB_STOR_XFER_GOOD)
1430 return USB_STOR_TRANSPORT_ERROR;
1431
1432 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) !=
1433 USB_STOR_XFER_GOOD)
1434 return USB_STOR_TRANSPORT_ERROR;
1435
1436 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1437 USB_STOR_XFER_GOOD)
1438 return USB_STOR_TRANSPORT_ERROR;
1439
1440 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1441 USB_STOR_XFER_GOOD)
1442 return USB_STOR_TRANSPORT_ERROR;
1443 }
1444
1445 return USB_STOR_TRANSPORT_GOOD;
1446}
1447
1448/*
1449 * Initialize the USBAT processor and the storage device
1450 */
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001451static int init_usbat(struct us_data *us, int devicetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
1453 int rc;
1454 struct usbat_info *info;
1455 unsigned char subcountH = USBAT_ATA_LBA_HI;
1456 unsigned char subcountL = USBAT_ATA_LBA_ME;
1457 unsigned char *status = us->iobuf;
1458
Oliver Neukum887c2562006-01-08 12:33:45 +01001459 us->extra = kzalloc(sizeof(struct usbat_info), GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (!us->extra) {
1461 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1462 return 1;
1463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 info = (struct usbat_info *) (us->extra);
1465
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001466 /* Enable peripheral control signals */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 rc = usbat_write_user_io(us,
1468 USBAT_UIO_OE1 | USBAT_UIO_OE0,
1469 USBAT_UIO_EPAD | USBAT_UIO_1);
1470 if (rc != USB_STOR_XFER_GOOD)
1471 return USB_STOR_TRANSPORT_ERROR;
1472
1473 US_DEBUGP("INIT 1\n");
1474
1475 msleep(2000);
1476
1477 rc = usbat_read_user_io(us, status);
1478 if (rc != USB_STOR_TRANSPORT_GOOD)
1479 return rc;
1480
1481 US_DEBUGP("INIT 2\n");
1482
1483 rc = usbat_read_user_io(us, status);
1484 if (rc != USB_STOR_XFER_GOOD)
1485 return USB_STOR_TRANSPORT_ERROR;
1486
1487 rc = usbat_read_user_io(us, status);
1488 if (rc != USB_STOR_XFER_GOOD)
1489 return USB_STOR_TRANSPORT_ERROR;
1490
1491 US_DEBUGP("INIT 3\n");
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 rc = usbat_select_and_test_registers(us);
1494 if (rc != USB_STOR_TRANSPORT_GOOD)
1495 return rc;
1496
Daniel Drake68a64572005-08-10 18:30:04 +01001497 US_DEBUGP("INIT 4\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 rc = usbat_read_user_io(us, status);
1500 if (rc != USB_STOR_XFER_GOOD)
1501 return USB_STOR_TRANSPORT_ERROR;
1502
Daniel Drake68a64572005-08-10 18:30:04 +01001503 US_DEBUGP("INIT 5\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001505 /* Enable peripheral control signals and card detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 rc = usbat_device_enable_cdt(us);
1507 if (rc != USB_STOR_TRANSPORT_GOOD)
1508 return rc;
1509
Daniel Drake68a64572005-08-10 18:30:04 +01001510 US_DEBUGP("INIT 6\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 rc = usbat_read_user_io(us, status);
1513 if (rc != USB_STOR_XFER_GOOD)
1514 return USB_STOR_TRANSPORT_ERROR;
1515
Daniel Drake68a64572005-08-10 18:30:04 +01001516 US_DEBUGP("INIT 7\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 msleep(1400);
1519
1520 rc = usbat_read_user_io(us, status);
1521 if (rc != USB_STOR_XFER_GOOD)
1522 return USB_STOR_TRANSPORT_ERROR;
1523
Daniel Drake68a64572005-08-10 18:30:04 +01001524 US_DEBUGP("INIT 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 rc = usbat_select_and_test_registers(us);
1527 if (rc != USB_STOR_TRANSPORT_GOOD)
1528 return rc;
1529
Daniel Drake68a64572005-08-10 18:30:04 +01001530 US_DEBUGP("INIT 9\n");
1531
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001532 /* At this point, we need to detect which device we are using */
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001533 if (usbat_set_transport(us, info, devicetype))
Daniel Drake68a64572005-08-10 18:30:04 +01001534 return USB_STOR_TRANSPORT_ERROR;
1535
1536 US_DEBUGP("INIT 10\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 if (usbat_get_device_type(us) == USBAT_DEV_FLASH) {
1539 subcountH = 0x02;
1540 subcountL = 0x00;
1541 }
1542 rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1543 0x00, 0x88, 0x08, subcountH, subcountL);
1544 if (rc != USB_STOR_XFER_GOOD)
1545 return USB_STOR_TRANSPORT_ERROR;
1546
Daniel Drake68a64572005-08-10 18:30:04 +01001547 US_DEBUGP("INIT 11\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 return USB_STOR_TRANSPORT_GOOD;
1550}
1551
1552/*
1553 * Transport for the HP 8200e
1554 */
1555static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1556{
1557 int result;
1558 unsigned char *status = us->iobuf;
1559 unsigned char registers[32];
1560 unsigned char data[32];
1561 unsigned int len;
1562 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Boaz Harrosh4776e992007-09-09 20:40:56 +03001564 len = scsi_bufflen(srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 /* Send A0 (ATA PACKET COMMAND).
1567 Note: I guess we're never going to get any of the ATA
1568 commands... just ATA Packet Commands.
1569 */
1570
1571 registers[0] = USBAT_ATA_FEATURES;
1572 registers[1] = USBAT_ATA_SECCNT;
1573 registers[2] = USBAT_ATA_SECNUM;
1574 registers[3] = USBAT_ATA_LBA_ME;
1575 registers[4] = USBAT_ATA_LBA_HI;
1576 registers[5] = USBAT_ATA_DEVICE;
1577 registers[6] = USBAT_ATA_CMD;
1578 data[0] = 0x00;
1579 data[1] = 0x00;
1580 data[2] = 0x00;
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001581 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1582 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
1583 data[5] = 0xB0; /* (device sel) = slave */
1584 data[6] = 0xA0; /* (command) = ATA PACKET COMMAND */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 for (i=7; i<19; i++) {
1587 registers[i] = 0x10;
1588 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1589 }
1590
1591 result = usbat_get_status(us, status);
1592 US_DEBUGP("Status = %02X\n", *status);
1593 if (result != USB_STOR_XFER_GOOD)
1594 return USB_STOR_TRANSPORT_ERROR;
1595 if (srb->cmnd[0] == TEST_UNIT_READY)
1596 transferred = 0;
1597
1598 if (srb->sc_data_direction == DMA_TO_DEVICE) {
1599
1600 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1601 registers, data, 19,
1602 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1603 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1604 DMA_TO_DEVICE,
Boaz Harrosh4776e992007-09-09 20:40:56 +03001605 scsi_sglist(srb),
1606 len, scsi_sg_count(srb), 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 if (result == USB_STOR_TRANSPORT_GOOD) {
1609 transferred += len;
1610 US_DEBUGP("Wrote %08X bytes\n", transferred);
1611 }
1612
1613 return result;
1614
1615 } else if (srb->cmnd[0] == READ_10 ||
1616 srb->cmnd[0] == GPCMD_READ_CD) {
1617
1618 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1619
1620 }
1621
1622 if (len > 0xFFFF) {
1623 US_DEBUGP("Error: len = %08X... what do I do now?\n",
1624 len);
1625 return USB_STOR_TRANSPORT_ERROR;
1626 }
1627
1628 if ( (result = usbat_multiple_write(us,
1629 registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
1630 return result;
1631 }
1632
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001633 /*
1634 * Write the 12-byte command header.
1635 *
1636 * If the command is BLANK then set the timer for 75 minutes.
1637 * Otherwise set it for 10 minutes.
1638 *
1639 * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1640 * AT SPEED 4 IS UNRELIABLE!!!
1641 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Peter Chubb141804d2006-05-02 18:30:12 +01001643 if ((result = usbat_write_block(us,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 USBAT_ATA, srb->cmnd, 12,
Peter Chubb141804d2006-05-02 18:30:12 +01001645 (srb->cmnd[0]==GPCMD_BLANK ? 75 : 10), 0) !=
1646 USB_STOR_TRANSPORT_GOOD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 return result;
1648 }
1649
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001650 /* If there is response data to be read in then do it here. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1653
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001654 /* How many bytes to read in? Check cylL register */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1657 USB_STOR_XFER_GOOD) {
1658 return USB_STOR_TRANSPORT_ERROR;
1659 }
1660
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001661 if (len > 0xFF) { /* need to read cylH also */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 len = *status;
1663 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1664 USB_STOR_XFER_GOOD) {
1665 return USB_STOR_TRANSPORT_ERROR;
1666 }
1667 len += ((unsigned int) *status)<<8;
1668 }
1669 else
1670 len = *status;
1671
1672
Boaz Harrosh4776e992007-09-09 20:40:56 +03001673 result = usbat_read_block(us, scsi_sglist(srb), len,
1674 scsi_sg_count(srb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676
1677 return result;
1678}
1679
1680/*
1681 * Transport for USBAT02-based CompactFlash and similar storage devices
1682 */
1683static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1684{
1685 int rc;
1686 struct usbat_info *info = (struct usbat_info *) (us->extra);
1687 unsigned long block, blocks;
1688 unsigned char *ptr = us->iobuf;
1689 static unsigned char inquiry_response[36] = {
1690 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1691 };
1692
1693 if (srb->cmnd[0] == INQUIRY) {
1694 US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1695 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1696 fill_inquiry_response(us, ptr, 36);
1697 return USB_STOR_TRANSPORT_GOOD;
1698 }
1699
1700 if (srb->cmnd[0] == READ_CAPACITY) {
1701 rc = usbat_flash_check_media(us, info);
1702 if (rc != USB_STOR_TRANSPORT_GOOD)
1703 return rc;
1704
1705 rc = usbat_flash_get_sector_count(us, info);
1706 if (rc != USB_STOR_TRANSPORT_GOOD)
1707 return rc;
1708
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001709 /* hard coded 512 byte sectors as per ATA spec */
1710 info->ssize = 0x200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1712 info->sectors, info->ssize);
1713
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001714 /*
1715 * build the reply
1716 * note: must return the sector number of the last sector,
1717 * *not* the total number of sectors
1718 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1720 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1721 usb_stor_set_xfer_buf(ptr, 8, srb);
1722
1723 return USB_STOR_TRANSPORT_GOOD;
1724 }
1725
1726 if (srb->cmnd[0] == MODE_SELECT_10) {
1727 US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
1728 return USB_STOR_TRANSPORT_ERROR;
1729 }
1730
1731 if (srb->cmnd[0] == READ_10) {
1732 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1733 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1734
1735 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1736
1737 US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
1738 return usbat_flash_read_data(us, info, block, blocks);
1739 }
1740
1741 if (srb->cmnd[0] == READ_12) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001742 /*
1743 * I don't think we'll ever see a READ_12 but support it anyway
1744 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1746 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1747
1748 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1749 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1750
1751 US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
1752 return usbat_flash_read_data(us, info, block, blocks);
1753 }
1754
1755 if (srb->cmnd[0] == WRITE_10) {
1756 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1757 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1758
1759 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1760
1761 US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
1762 return usbat_flash_write_data(us, info, block, blocks);
1763 }
1764
1765 if (srb->cmnd[0] == WRITE_12) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001766 /*
1767 * I don't think we'll ever see a WRITE_12 but support it anyway
1768 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1770 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1771
1772 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1773 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1774
1775 US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
1776 return usbat_flash_write_data(us, info, block, blocks);
1777 }
1778
1779
1780 if (srb->cmnd[0] == TEST_UNIT_READY) {
1781 US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
1782
1783 rc = usbat_flash_check_media(us, info);
1784 if (rc != USB_STOR_TRANSPORT_GOOD)
1785 return rc;
1786
1787 return usbat_check_status(us);
1788 }
1789
1790 if (srb->cmnd[0] == REQUEST_SENSE) {
1791 US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
1792
1793 memset(ptr, 0, 18);
1794 ptr[0] = 0xF0;
1795 ptr[2] = info->sense_key;
1796 ptr[7] = 11;
1797 ptr[12] = info->sense_asc;
1798 ptr[13] = info->sense_ascq;
1799 usb_stor_set_xfer_buf(ptr, 18, srb);
1800
1801 return USB_STOR_TRANSPORT_GOOD;
1802 }
1803
1804 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001805 /*
1806 * sure. whatever. not like we can stop the user from popping
1807 * the media out of the device (no locking doors, etc)
1808 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return USB_STOR_TRANSPORT_GOOD;
1810 }
1811
1812 US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1813 srb->cmnd[0], srb->cmnd[0]);
1814 info->sense_key = 0x05;
1815 info->sense_asc = 0x20;
1816 info->sense_ascq = 0x00;
1817 return USB_STOR_TRANSPORT_FAILED;
1818}
1819
Alan Stern26d68182009-02-12 14:48:08 -05001820static int init_usbat_cd(struct us_data *us)
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001821{
1822 return init_usbat(us, USBAT_DEV_HP8200);
1823}
1824
Alan Stern26d68182009-02-12 14:48:08 -05001825static int init_usbat_flash(struct us_data *us)
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001826{
1827 return init_usbat(us, USBAT_DEV_FLASH);
1828}
1829
Alan Stern26d68182009-02-12 14:48:08 -05001830static int usbat_probe(struct usb_interface *intf,
1831 const struct usb_device_id *id)
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001832{
Alan Stern26d68182009-02-12 14:48:08 -05001833 struct us_data *us;
1834 int result;
1835
1836 result = usb_stor_probe1(&us, intf, id,
1837 (id - usbat_usb_ids) + usbat_unusual_dev_list);
1838 if (result)
1839 return result;
1840
1841 /* The actual transport will be determined later by the
1842 * initialization routine; this is just a placeholder.
1843 */
1844 us->transport_name = "Shuttle USBAT";
1845 us->transport = usbat_flash_transport;
1846 us->transport_reset = usb_stor_CB_reset;
1847 us->max_lun = 1;
1848
1849 result = usb_stor_probe2(us);
1850 return result;
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001851}
1852
Alan Stern26d68182009-02-12 14:48:08 -05001853static struct usb_driver usbat_driver = {
1854 .name = "ums-usbat",
1855 .probe = usbat_probe,
1856 .disconnect = usb_stor_disconnect,
1857 .suspend = usb_stor_suspend,
1858 .resume = usb_stor_resume,
1859 .reset_resume = usb_stor_reset_resume,
1860 .pre_reset = usb_stor_pre_reset,
1861 .post_reset = usb_stor_post_reset,
1862 .id_table = usbat_usb_ids,
1863 .soft_unbind = 1,
1864};
1865
1866static int __init usbat_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Alan Stern26d68182009-02-12 14:48:08 -05001868 return usb_register(&usbat_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
Alan Stern26d68182009-02-12 14:48:08 -05001870
1871static void __exit usbat_exit(void)
1872{
1873 usb_deregister(&usbat_driver);
1874}
1875
1876module_init(usbat_init);
1877module_exit(usbat_exit);