blob: a3ec86b913a10422587f8a9ba7011c97bd65bbd5 [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"
Akinobu Mitaaa519be2015-05-06 18:24:21 +090056#include "scsiglue.h"
57
58#define DRV_NAME "ums-usbat"
Alan Stern26d68182009-02-12 14:48:08 -050059
Maciej Grela4246b062009-02-28 12:39:20 -080060MODULE_DESCRIPTION("Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable");
61MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>, Robert Baruch <autophile@starband.net>");
62MODULE_LICENSE("GPL");
Alan Stern26d68182009-02-12 14:48:08 -050063
64/* Supported device types */
65#define USBAT_DEV_HP8200 0x01
66#define USBAT_DEV_FLASH 0x02
67
68#define USBAT_EPP_PORT 0x10
69#define USBAT_EPP_REGISTER 0x30
70#define USBAT_ATA 0x40
71#define USBAT_ISA 0x50
72
73/* Commands (need to be logically OR'd with an access type */
74#define USBAT_CMD_READ_REG 0x00
75#define USBAT_CMD_WRITE_REG 0x01
76#define USBAT_CMD_READ_BLOCK 0x02
77#define USBAT_CMD_WRITE_BLOCK 0x03
78#define USBAT_CMD_COND_READ_BLOCK 0x04
79#define USBAT_CMD_COND_WRITE_BLOCK 0x05
80#define USBAT_CMD_WRITE_REGS 0x07
81
82/* Commands (these don't need an access type) */
83#define USBAT_CMD_EXEC_CMD 0x80
84#define USBAT_CMD_SET_FEAT 0x81
85#define USBAT_CMD_UIO 0x82
86
87/* Methods of accessing UIO register */
88#define USBAT_UIO_READ 1
89#define USBAT_UIO_WRITE 0
90
91/* Qualifier bits */
92#define USBAT_QUAL_FCQ 0x20 /* full compare */
93#define USBAT_QUAL_ALQ 0x10 /* auto load subcount */
94
95/* USBAT Flash Media status types */
96#define USBAT_FLASH_MEDIA_NONE 0
97#define USBAT_FLASH_MEDIA_CF 1
98
99/* USBAT Flash Media change types */
100#define USBAT_FLASH_MEDIA_SAME 0
101#define USBAT_FLASH_MEDIA_CHANGED 1
102
103/* USBAT ATA registers */
104#define USBAT_ATA_DATA 0x10 /* read/write data (R/W) */
105#define USBAT_ATA_FEATURES 0x11 /* set features (W) */
106#define USBAT_ATA_ERROR 0x11 /* error (R) */
107#define USBAT_ATA_SECCNT 0x12 /* sector count (R/W) */
108#define USBAT_ATA_SECNUM 0x13 /* sector number (R/W) */
109#define USBAT_ATA_LBA_ME 0x14 /* cylinder low (R/W) */
110#define USBAT_ATA_LBA_HI 0x15 /* cylinder high (R/W) */
111#define USBAT_ATA_DEVICE 0x16 /* head/device selection (R/W) */
112#define USBAT_ATA_STATUS 0x17 /* device status (R) */
113#define USBAT_ATA_CMD 0x17 /* device command (W) */
114#define USBAT_ATA_ALTSTATUS 0x0E /* status (no clear IRQ) (R) */
115
116/* USBAT User I/O Data registers */
117#define USBAT_UIO_EPAD 0x80 /* Enable Peripheral Control Signals */
118#define USBAT_UIO_CDT 0x40 /* Card Detect (Read Only) */
119 /* CDT = ACKD & !UI1 & !UI0 */
120#define USBAT_UIO_1 0x20 /* I/O 1 */
121#define USBAT_UIO_0 0x10 /* I/O 0 */
122#define USBAT_UIO_EPP_ATA 0x08 /* 1=EPP mode, 0=ATA mode */
123#define USBAT_UIO_UI1 0x04 /* Input 1 */
124#define USBAT_UIO_UI0 0x02 /* Input 0 */
125#define USBAT_UIO_INTR_ACK 0x01 /* Interrupt (ATA/ISA)/Acknowledge (EPP) */
126
127/* USBAT User I/O Enable registers */
128#define USBAT_UIO_DRVRST 0x80 /* Reset Peripheral */
129#define USBAT_UIO_ACKD 0x40 /* Enable Card Detect */
130#define USBAT_UIO_OE1 0x20 /* I/O 1 set=output/clr=input */
131 /* If ACKD=1, set OE1 to 1 also. */
132#define USBAT_UIO_OE0 0x10 /* I/O 0 set=output/clr=input */
133#define USBAT_UIO_ADPRST 0x01 /* Reset SCM chip */
134
135/* USBAT Features */
136#define USBAT_FEAT_ETEN 0x80 /* External trigger enable */
137#define USBAT_FEAT_U1 0x08
138#define USBAT_FEAT_U0 0x04
139#define USBAT_FEAT_ET1 0x02
140#define USBAT_FEAT_ET2 0x01
141
142struct usbat_info {
143 int devicetype;
144
145 /* Used for Flash readers only */
146 unsigned long sectors; /* total sector count */
147 unsigned long ssize; /* sector size in bytes */
148
149 unsigned char sense_key;
150 unsigned long sense_asc; /* additional sense code */
151 unsigned long sense_ascq; /* additional sense code qualifier */
152};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154#define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
155#define LSB_of(s) ((s)&0xFF)
156#define MSB_of(s) ((s)>>8)
157
158static int transferred = 0;
159
160static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us);
161static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us);
162
Alan Stern26d68182009-02-12 14:48:08 -0500163static int init_usbat_cd(struct us_data *us);
164static int init_usbat_flash(struct us_data *us);
165
166
167/*
168 * The table of devices
169 */
170#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
171 vendorName, productName, useProtocol, useTransport, \
172 initFunction, flags) \
173{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
Sebastian Andrzej Siewiorf61870e2012-08-28 22:37:13 +0200174 .driver_info = (flags) }
Alan Stern26d68182009-02-12 14:48:08 -0500175
Felipe Balbice3af892011-11-15 09:53:40 +0200176static struct usb_device_id usbat_usb_ids[] = {
Alan Stern26d68182009-02-12 14:48:08 -0500177# include "unusual_usbat.h"
178 { } /* Terminating entry */
179};
180MODULE_DEVICE_TABLE(usb, usbat_usb_ids);
181
182#undef UNUSUAL_DEV
183
184/*
185 * The flags table
186 */
187#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
188 vendor_name, product_name, use_protocol, use_transport, \
189 init_function, Flags) \
190{ \
191 .vendorName = vendor_name, \
192 .productName = product_name, \
193 .useProtocol = use_protocol, \
194 .useTransport = use_transport, \
195 .initFunction = init_function, \
196}
197
198static struct us_unusual_dev usbat_unusual_dev_list[] = {
199# include "unusual_usbat.h"
200 { } /* Terminating entry */
201};
202
203#undef UNUSUAL_DEV
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/*
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100206 * Convenience function to produce an ATA read/write sectors command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * Use cmd=0x20 for read, cmd=0x30 for write
208 */
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100209static void usbat_pack_ata_sector_cmd(unsigned char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 unsigned char thistime,
211 u32 sector, unsigned char cmd)
212{
213 buf[0] = 0;
214 buf[1] = thistime;
215 buf[2] = sector & 0xFF;
216 buf[3] = (sector >> 8) & 0xFF;
217 buf[4] = (sector >> 16) & 0xFF;
218 buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
219 buf[6] = cmd;
220}
221
222/*
223 * Convenience function to get the device type (flash or hp8200)
224 */
225static int usbat_get_device_type(struct us_data *us)
226{
227 return ((struct usbat_info*)us->extra)->devicetype;
228}
229
230/*
231 * Read a register from the device
232 */
233static int usbat_read(struct us_data *us,
234 unsigned char access,
235 unsigned char reg,
236 unsigned char *content)
237{
238 return usb_stor_ctrl_transfer(us,
239 us->recv_ctrl_pipe,
240 access | USBAT_CMD_READ_REG,
241 0xC0,
242 (u16)reg,
243 0,
244 content,
245 1);
246}
247
248/*
249 * Write to a register on the device
250 */
251static int usbat_write(struct us_data *us,
252 unsigned char access,
253 unsigned char reg,
254 unsigned char content)
255{
256 return usb_stor_ctrl_transfer(us,
257 us->send_ctrl_pipe,
258 access | USBAT_CMD_WRITE_REG,
259 0x40,
260 short_pack(reg, content),
261 0,
262 NULL,
263 0);
264}
265
266/*
267 * Convenience function to perform a bulk read
268 */
269static int usbat_bulk_read(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300270 void* buf,
Peter Chubb141804d2006-05-02 18:30:12 +0100271 unsigned int len,
272 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 if (len == 0)
275 return USB_STOR_XFER_GOOD;
276
Joe Perches191648d2013-04-19 11:44:00 -0700277 usb_stor_dbg(us, "len = %d\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +0300278 return usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe, buf, len, use_sg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281/*
282 * Convenience function to perform a bulk write
283 */
284static int usbat_bulk_write(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300285 void* buf,
Peter Chubb141804d2006-05-02 18:30:12 +0100286 unsigned int len,
287 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 if (len == 0)
290 return USB_STOR_XFER_GOOD;
291
Joe Perches191648d2013-04-19 11:44:00 -0700292 usb_stor_dbg(us, "len = %d\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +0300293 return usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe, buf, len, use_sg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296/*
297 * Some USBAT-specific commands can only be executed over a command transport
298 * This transport allows one (len=8) or two (len=16) vendor-specific commands
299 * to be executed.
300 */
301static int usbat_execute_command(struct us_data *us,
302 unsigned char *commands,
303 unsigned int len)
304{
305 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
306 USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
307 commands, len);
308}
309
310/*
311 * Read the status register
312 */
313static int usbat_get_status(struct us_data *us, unsigned char *status)
314{
315 int rc;
316 rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
317
Joe Perches191648d2013-04-19 11:44:00 -0700318 usb_stor_dbg(us, "0x%02X\n", *status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return rc;
320}
321
322/*
323 * Check the device status
324 */
325static int usbat_check_status(struct us_data *us)
326{
327 unsigned char *reply = us->iobuf;
328 int rc;
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 rc = usbat_get_status(us, reply);
331 if (rc != USB_STOR_XFER_GOOD)
332 return USB_STOR_TRANSPORT_FAILED;
333
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100334 /* error/check condition (0x51 is ok) */
335 if (*reply & 0x01 && *reply != 0x51)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return USB_STOR_TRANSPORT_FAILED;
337
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100338 /* device fault */
339 if (*reply & 0x20)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return USB_STOR_TRANSPORT_FAILED;
341
342 return USB_STOR_TRANSPORT_GOOD;
343}
344
345/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300346 * Stores critical information in internal registers in preparation for the execution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 * of a conditional usbat_read_blocks or usbat_write_blocks call.
348 */
349static int usbat_set_shuttle_features(struct us_data *us,
350 unsigned char external_trigger,
351 unsigned char epp_control,
352 unsigned char mask_byte,
353 unsigned char test_pattern,
354 unsigned char subcountH,
355 unsigned char subcountL)
356{
357 unsigned char *command = us->iobuf;
358
359 command[0] = 0x40;
360 command[1] = USBAT_CMD_SET_FEAT;
361
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100362 /*
363 * The only bit relevant to ATA access is bit 6
364 * which defines 8 bit data access (set) or 16 bit (unset)
365 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 command[2] = epp_control;
367
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100368 /*
369 * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
370 * ET1 and ET2 define an external event to be checked for on event of a
371 * _read_blocks or _write_blocks operation. The read/write will not take
372 * place unless the defined trigger signal is active.
373 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 command[3] = external_trigger;
375
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100376 /*
377 * The resultant byte of the mask operation (see mask_byte) is compared for
378 * equivalence with this test pattern. If equal, the read/write will take
379 * place.
380 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 command[4] = test_pattern;
382
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100383 /*
384 * This value is logically ANDed with the status register field specified
385 * in the read/write command.
386 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 command[5] = mask_byte;
388
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100389 /*
390 * If ALQ is set in the qualifier, this field contains the address of the
391 * registers where the byte count should be read for transferring the data.
392 * If ALQ is not set, then this field contains the number of bytes to be
393 * transferred.
394 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 command[6] = subcountL;
396 command[7] = subcountH;
397
398 return usbat_execute_command(us, command, 8);
399}
400
401/*
402 * Block, waiting for an ATA device to become not busy or to report
403 * an error condition.
404 */
405static int usbat_wait_not_busy(struct us_data *us, int minutes)
406{
407 int i;
408 int result;
409 unsigned char *status = us->iobuf;
410
411 /* Synchronizing cache on a CDR could take a heck of a long time,
412 * but probably not more than 10 minutes or so. On the other hand,
413 * doing a full blank on a CDRW at speed 1 will take about 75
414 * minutes!
415 */
416
417 for (i=0; i<1200+minutes*60; i++) {
418
419 result = usbat_get_status(us, status);
420
421 if (result!=USB_STOR_XFER_GOOD)
422 return USB_STOR_TRANSPORT_ERROR;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100423 if (*status & 0x01) { /* check condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 result = usbat_read(us, USBAT_ATA, 0x10, status);
425 return USB_STOR_TRANSPORT_FAILED;
426 }
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100427 if (*status & 0x20) /* device fault */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return USB_STOR_TRANSPORT_FAILED;
429
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100430 if ((*status & 0x80)==0x00) { /* not busy */
Joe Perches191648d2013-04-19 11:44:00 -0700431 usb_stor_dbg(us, "Waited not busy for %d steps\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return USB_STOR_TRANSPORT_GOOD;
433 }
434
435 if (i<500)
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100436 msleep(10); /* 5 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 else if (i<700)
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100438 msleep(50); /* 10 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 else if (i<1200)
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100440 msleep(100); /* 50 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 else
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100442 msleep(1000); /* X minutes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
Joe Perches191648d2013-04-19 11:44:00 -0700445 usb_stor_dbg(us, "Waited not busy for %d minutes, timing out\n",
446 minutes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return USB_STOR_TRANSPORT_FAILED;
448}
449
450/*
451 * Read block data from the data register
452 */
453static int usbat_read_block(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300454 void* buf,
Peter Chubb141804d2006-05-02 18:30:12 +0100455 unsigned short len,
456 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 int result;
459 unsigned char *command = us->iobuf;
460
461 if (!len)
462 return USB_STOR_TRANSPORT_GOOD;
463
464 command[0] = 0xC0;
465 command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
466 command[2] = USBAT_ATA_DATA;
467 command[3] = 0;
468 command[4] = 0;
469 command[5] = 0;
470 command[6] = LSB_of(len);
471 command[7] = MSB_of(len);
472
473 result = usbat_execute_command(us, command, 8);
474 if (result != USB_STOR_XFER_GOOD)
475 return USB_STOR_TRANSPORT_ERROR;
476
Boaz Harrosh4776e992007-09-09 20:40:56 +0300477 result = usbat_bulk_read(us, buf, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return (result == USB_STOR_XFER_GOOD ?
479 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
480}
481
482/*
483 * Write block data via the data register
484 */
485static int usbat_write_block(struct us_data *us,
486 unsigned char access,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300487 void* buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 unsigned short len,
Peter Chubb141804d2006-05-02 18:30:12 +0100489 int minutes,
490 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 int result;
493 unsigned char *command = us->iobuf;
494
495 if (!len)
496 return USB_STOR_TRANSPORT_GOOD;
497
498 command[0] = 0x40;
499 command[1] = access | USBAT_CMD_WRITE_BLOCK;
500 command[2] = USBAT_ATA_DATA;
501 command[3] = 0;
502 command[4] = 0;
503 command[5] = 0;
504 command[6] = LSB_of(len);
505 command[7] = MSB_of(len);
506
507 result = usbat_execute_command(us, command, 8);
508
509 if (result != USB_STOR_XFER_GOOD)
510 return USB_STOR_TRANSPORT_ERROR;
511
Boaz Harrosh4776e992007-09-09 20:40:56 +0300512 result = usbat_bulk_write(us, buf, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (result != USB_STOR_XFER_GOOD)
514 return USB_STOR_TRANSPORT_ERROR;
515
516 return usbat_wait_not_busy(us, minutes);
517}
518
519/*
520 * Process read and write requests
521 */
522static int usbat_hp8200e_rw_block_test(struct us_data *us,
523 unsigned char access,
524 unsigned char *registers,
525 unsigned char *data_out,
526 unsigned short num_registers,
527 unsigned char data_reg,
528 unsigned char status_reg,
529 unsigned char timeout,
530 unsigned char qualifier,
531 int direction,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300532 void *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 unsigned short len,
534 int use_sg,
535 int minutes)
536{
537 int result;
538 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
539 us->recv_bulk_pipe : us->send_bulk_pipe;
540
541 unsigned char *command = us->iobuf;
542 int i, j;
543 int cmdlen;
544 unsigned char *data = us->iobuf;
545 unsigned char *status = us->iobuf;
546
547 BUG_ON(num_registers > US_IOBUF_SIZE/2);
548
549 for (i=0; i<20; i++) {
550
551 /*
552 * The first time we send the full command, which consists
553 * of downloading the SCSI command followed by downloading
554 * the data via a write-and-test. Any other time we only
555 * send the command to download the data -- the SCSI command
556 * is still 'active' in some sense in the device.
557 *
558 * We're only going to try sending the data 10 times. After
559 * that, we just return a failure.
560 */
561
562 if (i==0) {
563 cmdlen = 16;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100564 /*
565 * Write to multiple registers
566 * Not really sure the 0x07, 0x17, 0xfc, 0xe7 is
567 * necessary here, but that's what came out of the
568 * trace every single time.
569 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 command[0] = 0x40;
571 command[1] = access | USBAT_CMD_WRITE_REGS;
572 command[2] = 0x07;
573 command[3] = 0x17;
574 command[4] = 0xFC;
575 command[5] = 0xE7;
576 command[6] = LSB_of(num_registers*2);
577 command[7] = MSB_of(num_registers*2);
578 } else
579 cmdlen = 8;
580
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100581 /* Conditionally read or write blocks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 command[cmdlen-8] = (direction==DMA_TO_DEVICE ? 0x40 : 0xC0);
583 command[cmdlen-7] = access |
584 (direction==DMA_TO_DEVICE ?
585 USBAT_CMD_COND_WRITE_BLOCK : USBAT_CMD_COND_READ_BLOCK);
586 command[cmdlen-6] = data_reg;
587 command[cmdlen-5] = status_reg;
588 command[cmdlen-4] = timeout;
589 command[cmdlen-3] = qualifier;
590 command[cmdlen-2] = LSB_of(len);
591 command[cmdlen-1] = MSB_of(len);
592
593 result = usbat_execute_command(us, command, cmdlen);
594
595 if (result != USB_STOR_XFER_GOOD)
596 return USB_STOR_TRANSPORT_ERROR;
597
598 if (i==0) {
599
600 for (j=0; j<num_registers; j++) {
601 data[j<<1] = registers[j];
602 data[1+(j<<1)] = data_out[j];
603 }
604
Peter Chubb141804d2006-05-02 18:30:12 +0100605 result = usbat_bulk_write(us, data, num_registers*2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (result != USB_STOR_XFER_GOOD)
607 return USB_STOR_TRANSPORT_ERROR;
608
609 }
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 result = usb_stor_bulk_transfer_sg(us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300612 pipe, buf, len, use_sg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 /*
615 * If we get a stall on the bulk download, we'll retry
616 * the bulk download -- but not the SCSI command because
617 * in some sense the SCSI command is still 'active' and
618 * waiting for the data. Don't ask me why this should be;
619 * I'm only following what the Windoze driver did.
620 *
621 * Note that a stall for the test-and-read/write command means
622 * that the test failed. In this case we're testing to make
623 * sure that the device is error-free
624 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
625 * hypothesis is that the USBAT chip somehow knows what
626 * the device will accept, but doesn't give the device any
627 * data until all data is received. Thus, the device would
628 * still be waiting for the first byte of data if a stall
629 * occurs, even if the stall implies that some data was
630 * transferred.
631 */
632
633 if (result == USB_STOR_XFER_SHORT ||
634 result == USB_STOR_XFER_STALLED) {
635
636 /*
637 * If we're reading and we stalled, then clear
638 * the bulk output pipe only the first time.
639 */
640
641 if (direction==DMA_FROM_DEVICE && i==0) {
642 if (usb_stor_clear_halt(us,
643 us->send_bulk_pipe) < 0)
644 return USB_STOR_TRANSPORT_ERROR;
645 }
646
647 /*
648 * Read status: is the device angry, or just busy?
649 */
650
651 result = usbat_read(us, USBAT_ATA,
652 direction==DMA_TO_DEVICE ?
653 USBAT_ATA_STATUS : USBAT_ATA_ALTSTATUS,
654 status);
655
656 if (result!=USB_STOR_XFER_GOOD)
657 return USB_STOR_TRANSPORT_ERROR;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100658 if (*status & 0x01) /* check condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return USB_STOR_TRANSPORT_FAILED;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100660 if (*status & 0x20) /* device fault */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return USB_STOR_TRANSPORT_FAILED;
662
Joe Perches191648d2013-04-19 11:44:00 -0700663 usb_stor_dbg(us, "Redoing %s\n",
664 direction == DMA_TO_DEVICE
665 ? "write" : "read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 } else if (result != USB_STOR_XFER_GOOD)
668 return USB_STOR_TRANSPORT_ERROR;
669 else
670 return usbat_wait_not_busy(us, minutes);
671
672 }
673
Joe Perches191648d2013-04-19 11:44:00 -0700674 usb_stor_dbg(us, "Bummer! %s bulk data 20 times failed\n",
675 direction == DMA_TO_DEVICE ? "Writing" : "Reading");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 return USB_STOR_TRANSPORT_FAILED;
678}
679
680/*
681 * Write to multiple registers:
682 * Allows us to write specific data to any registers. The data to be written
683 * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN
684 * which gets sent through bulk out.
685 * Not designed for large transfers of data!
686 */
687static int usbat_multiple_write(struct us_data *us,
688 unsigned char *registers,
689 unsigned char *data_out,
690 unsigned short num_registers)
691{
692 int i, result;
693 unsigned char *data = us->iobuf;
694 unsigned char *command = us->iobuf;
695
696 BUG_ON(num_registers > US_IOBUF_SIZE/2);
697
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100698 /* Write to multiple registers, ATA access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 command[0] = 0x40;
700 command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
701
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100702 /* No relevance */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 command[2] = 0;
704 command[3] = 0;
705 command[4] = 0;
706 command[5] = 0;
707
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100708 /* Number of bytes to be transferred (incl. addresses and data) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 command[6] = LSB_of(num_registers*2);
710 command[7] = MSB_of(num_registers*2);
711
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100712 /* The setup command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 result = usbat_execute_command(us, command, 8);
714 if (result != USB_STOR_XFER_GOOD)
715 return USB_STOR_TRANSPORT_ERROR;
716
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100717 /* Create the reg/data, reg/data sequence */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 for (i=0; i<num_registers; i++) {
719 data[i<<1] = registers[i];
720 data[1+(i<<1)] = data_out[i];
721 }
722
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100723 /* Send the data */
Peter Chubb141804d2006-05-02 18:30:12 +0100724 result = usbat_bulk_write(us, data, num_registers*2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (result != USB_STOR_XFER_GOOD)
726 return USB_STOR_TRANSPORT_ERROR;
727
728 if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
729 return usbat_wait_not_busy(us, 0);
730 else
731 return USB_STOR_TRANSPORT_GOOD;
732}
733
734/*
735 * Conditionally read blocks from device:
736 * Allows us to read blocks from a specific data register, based upon the
737 * condition that a status register can be successfully masked with a status
738 * qualifier. If this condition is not initially met, the read will wait
739 * up until a maximum amount of time has elapsed, as specified by timeout.
740 * The read will start when the condition is met, otherwise the command aborts.
741 *
742 * The qualifier defined here is not the value that is masked, it defines
743 * conditions for the write to take place. The actual masked qualifier (and
744 * other related details) are defined beforehand with _set_shuttle_features().
745 */
746static int usbat_read_blocks(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300747 void* buffer,
Peter Chubb141804d2006-05-02 18:30:12 +0100748 int len,
749 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 int result;
752 unsigned char *command = us->iobuf;
753
754 command[0] = 0xC0;
755 command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK;
756 command[2] = USBAT_ATA_DATA;
757 command[3] = USBAT_ATA_STATUS;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100758 command[4] = 0xFD; /* Timeout (ms); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 command[5] = USBAT_QUAL_FCQ;
760 command[6] = LSB_of(len);
761 command[7] = MSB_of(len);
762
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100763 /* Multiple block read setup command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 result = usbat_execute_command(us, command, 8);
765 if (result != USB_STOR_XFER_GOOD)
766 return USB_STOR_TRANSPORT_FAILED;
767
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100768 /* Read the blocks we just asked for */
Peter Chubb141804d2006-05-02 18:30:12 +0100769 result = usbat_bulk_read(us, buffer, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (result != USB_STOR_XFER_GOOD)
771 return USB_STOR_TRANSPORT_FAILED;
772
773 return USB_STOR_TRANSPORT_GOOD;
774}
775
776/*
777 * Conditionally write blocks to device:
778 * Allows us to write blocks to a specific data register, based upon the
779 * condition that a status register can be successfully masked with a status
780 * qualifier. If this condition is not initially met, the write will wait
781 * up until a maximum amount of time has elapsed, as specified by timeout.
782 * The read will start when the condition is met, otherwise the command aborts.
783 *
784 * The qualifier defined here is not the value that is masked, it defines
785 * conditions for the write to take place. The actual masked qualifier (and
786 * other related details) are defined beforehand with _set_shuttle_features().
787 */
788static int usbat_write_blocks(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300789 void* buffer,
Peter Chubb141804d2006-05-02 18:30:12 +0100790 int len,
791 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 int result;
794 unsigned char *command = us->iobuf;
795
796 command[0] = 0x40;
797 command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK;
798 command[2] = USBAT_ATA_DATA;
799 command[3] = USBAT_ATA_STATUS;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100800 command[4] = 0xFD; /* Timeout (ms) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 command[5] = USBAT_QUAL_FCQ;
802 command[6] = LSB_of(len);
803 command[7] = MSB_of(len);
804
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100805 /* Multiple block write setup command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 result = usbat_execute_command(us, command, 8);
807 if (result != USB_STOR_XFER_GOOD)
808 return USB_STOR_TRANSPORT_FAILED;
809
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100810 /* Write the data */
Peter Chubb141804d2006-05-02 18:30:12 +0100811 result = usbat_bulk_write(us, buffer, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (result != USB_STOR_XFER_GOOD)
813 return USB_STOR_TRANSPORT_FAILED;
814
815 return USB_STOR_TRANSPORT_GOOD;
816}
817
818/*
819 * Read the User IO register
820 */
821static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
822{
823 int result;
824
825 result = usb_stor_ctrl_transfer(us,
826 us->recv_ctrl_pipe,
827 USBAT_CMD_UIO,
828 0xC0,
829 0,
830 0,
831 data_flags,
832 USBAT_UIO_READ);
833
Joe Perches191648d2013-04-19 11:44:00 -0700834 usb_stor_dbg(us, "UIO register reads %02X\n", *data_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 return result;
837}
838
839/*
840 * Write to the User IO register
841 */
842static int usbat_write_user_io(struct us_data *us,
843 unsigned char enable_flags,
844 unsigned char data_flags)
845{
846 return usb_stor_ctrl_transfer(us,
847 us->send_ctrl_pipe,
848 USBAT_CMD_UIO,
849 0x40,
850 short_pack(enable_flags, data_flags),
851 0,
852 NULL,
853 USBAT_UIO_WRITE);
854}
855
856/*
857 * Reset the device
858 * Often needed on media change.
859 */
860static int usbat_device_reset(struct us_data *us)
861{
862 int rc;
863
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100864 /*
865 * Reset peripheral, enable peripheral control signals
866 * (bring reset signal up)
867 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 rc = usbat_write_user_io(us,
869 USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
870 USBAT_UIO_EPAD | USBAT_UIO_1);
871 if (rc != USB_STOR_XFER_GOOD)
872 return USB_STOR_TRANSPORT_ERROR;
873
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100874 /*
875 * Enable peripheral control signals
876 * (bring reset signal down)
877 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 rc = usbat_write_user_io(us,
879 USBAT_UIO_OE1 | USBAT_UIO_OE0,
880 USBAT_UIO_EPAD | USBAT_UIO_1);
881 if (rc != USB_STOR_XFER_GOOD)
882 return USB_STOR_TRANSPORT_ERROR;
883
884 return USB_STOR_TRANSPORT_GOOD;
885}
886
887/*
888 * Enable card detect
889 */
890static int usbat_device_enable_cdt(struct us_data *us)
891{
892 int rc;
893
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100894 /* Enable peripheral control signals and card detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 rc = usbat_write_user_io(us,
896 USBAT_UIO_ACKD | USBAT_UIO_OE1 | USBAT_UIO_OE0,
897 USBAT_UIO_EPAD | USBAT_UIO_1);
898 if (rc != USB_STOR_XFER_GOOD)
899 return USB_STOR_TRANSPORT_ERROR;
900
901 return USB_STOR_TRANSPORT_GOOD;
902}
903
904/*
905 * Determine if media is present.
906 */
Joe Perches191648d2013-04-19 11:44:00 -0700907static int usbat_flash_check_media_present(struct us_data *us,
908 unsigned char *uio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 if (*uio & USBAT_UIO_UI0) {
Joe Perches191648d2013-04-19 11:44:00 -0700911 usb_stor_dbg(us, "no media detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return USBAT_FLASH_MEDIA_NONE;
913 }
914
915 return USBAT_FLASH_MEDIA_CF;
916}
917
918/*
919 * Determine if media has changed since last operation
920 */
Joe Perches191648d2013-04-19 11:44:00 -0700921static int usbat_flash_check_media_changed(struct us_data *us,
922 unsigned char *uio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 if (*uio & USBAT_UIO_0) {
Joe Perches191648d2013-04-19 11:44:00 -0700925 usb_stor_dbg(us, "media change detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return USBAT_FLASH_MEDIA_CHANGED;
927 }
928
929 return USBAT_FLASH_MEDIA_SAME;
930}
931
932/*
933 * Check for media change / no media and handle the situation appropriately
934 */
935static int usbat_flash_check_media(struct us_data *us,
936 struct usbat_info *info)
937{
938 int rc;
939 unsigned char *uio = us->iobuf;
940
941 rc = usbat_read_user_io(us, uio);
942 if (rc != USB_STOR_XFER_GOOD)
943 return USB_STOR_TRANSPORT_ERROR;
944
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100945 /* Check for media existence */
Joe Perches191648d2013-04-19 11:44:00 -0700946 rc = usbat_flash_check_media_present(us, uio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (rc == USBAT_FLASH_MEDIA_NONE) {
948 info->sense_key = 0x02;
949 info->sense_asc = 0x3A;
950 info->sense_ascq = 0x00;
951 return USB_STOR_TRANSPORT_FAILED;
952 }
953
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100954 /* Check for media change */
Joe Perches191648d2013-04-19 11:44:00 -0700955 rc = usbat_flash_check_media_changed(us, uio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (rc == USBAT_FLASH_MEDIA_CHANGED) {
957
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100958 /* Reset and re-enable card detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 rc = usbat_device_reset(us);
960 if (rc != USB_STOR_TRANSPORT_GOOD)
961 return rc;
962 rc = usbat_device_enable_cdt(us);
963 if (rc != USB_STOR_TRANSPORT_GOOD)
964 return rc;
965
966 msleep(50);
967
968 rc = usbat_read_user_io(us, uio);
969 if (rc != USB_STOR_XFER_GOOD)
970 return USB_STOR_TRANSPORT_ERROR;
971
972 info->sense_key = UNIT_ATTENTION;
973 info->sense_asc = 0x28;
974 info->sense_ascq = 0x00;
975 return USB_STOR_TRANSPORT_FAILED;
976 }
977
978 return USB_STOR_TRANSPORT_GOOD;
979}
980
981/*
982 * Determine whether we are controlling a flash-based reader/writer,
983 * or a HP8200-based CD drive.
984 * Sets transport functions as appropriate.
985 */
986static int usbat_identify_device(struct us_data *us,
987 struct usbat_info *info)
988{
989 int rc;
990 unsigned char status;
991
992 if (!us || !info)
993 return USB_STOR_TRANSPORT_ERROR;
994
995 rc = usbat_device_reset(us);
996 if (rc != USB_STOR_TRANSPORT_GOOD)
997 return rc;
Daniel Drake8845add2005-11-17 09:48:01 -0800998 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 /*
Daniel Drake68a64572005-08-10 18:30:04 +01001001 * In attempt to distinguish between HP CDRW's and Flash readers, we now
1002 * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
1003 * readers), this command should fail with error. On ATAPI devices (i.e.
1004 * CDROM drives), it should succeed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 */
Daniel Drake68a64572005-08-10 18:30:04 +01001006 rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1);
1007 if (rc != USB_STOR_XFER_GOOD)
1008 return USB_STOR_TRANSPORT_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Daniel Drake68a64572005-08-10 18:30:04 +01001010 rc = usbat_get_status(us, &status);
1011 if (rc != USB_STOR_XFER_GOOD)
1012 return USB_STOR_TRANSPORT_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001014 /* Check for error bit, or if the command 'fell through' */
Daniel Drakea8798532005-09-29 00:14:21 +01001015 if (status == 0xA1 || !(status & 0x01)) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001016 /* Device is HP 8200 */
Joe Perches191648d2013-04-19 11:44:00 -07001017 usb_stor_dbg(us, "Detected HP8200 CDRW\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 info->devicetype = USBAT_DEV_HP8200;
Daniel Drakea8798532005-09-29 00:14:21 +01001019 } else {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001020 /* Device is a CompactFlash reader/writer */
Joe Perches191648d2013-04-19 11:44:00 -07001021 usb_stor_dbg(us, "Detected Flash reader/writer\n");
Daniel Drakea8798532005-09-29 00:14:21 +01001022 info->devicetype = USBAT_DEV_FLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024
1025 return USB_STOR_TRANSPORT_GOOD;
1026}
1027
1028/*
1029 * Set the transport function based on the device type
1030 */
1031static int usbat_set_transport(struct us_data *us,
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001032 struct usbat_info *info,
1033 int devicetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001036 if (!info->devicetype)
1037 info->devicetype = devicetype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001039 if (!info->devicetype)
1040 usbat_identify_device(us, info);
1041
1042 switch (info->devicetype) {
1043 default:
1044 return USB_STOR_TRANSPORT_ERROR;
1045
1046 case USBAT_DEV_HP8200:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 us->transport = usbat_hp8200e_transport;
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001048 break;
1049
1050 case USBAT_DEV_FLASH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 us->transport = usbat_flash_transport;
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001052 break;
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 return 0;
1056}
1057
1058/*
1059 * Read the media capacity
1060 */
1061static int usbat_flash_get_sector_count(struct us_data *us,
1062 struct usbat_info *info)
1063{
1064 unsigned char registers[3] = {
1065 USBAT_ATA_SECCNT,
1066 USBAT_ATA_DEVICE,
1067 USBAT_ATA_CMD,
1068 };
1069 unsigned char command[3] = { 0x01, 0xA0, 0xEC };
1070 unsigned char *reply;
1071 unsigned char status;
1072 int rc;
1073
1074 if (!us || !info)
1075 return USB_STOR_TRANSPORT_ERROR;
1076
1077 reply = kmalloc(512, GFP_NOIO);
1078 if (!reply)
1079 return USB_STOR_TRANSPORT_ERROR;
1080
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001081 /* ATA command : IDENTIFY DEVICE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 rc = usbat_multiple_write(us, registers, command, 3);
1083 if (rc != USB_STOR_XFER_GOOD) {
Joe Perches191648d2013-04-19 11:44:00 -07001084 usb_stor_dbg(us, "Gah! identify_device failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 rc = USB_STOR_TRANSPORT_ERROR;
1086 goto leave;
1087 }
1088
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001089 /* Read device status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
1091 rc = USB_STOR_TRANSPORT_ERROR;
1092 goto leave;
1093 }
1094
1095 msleep(100);
1096
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001097 /* Read the device identification data */
Peter Chubb141804d2006-05-02 18:30:12 +01001098 rc = usbat_read_block(us, reply, 512, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (rc != USB_STOR_TRANSPORT_GOOD)
1100 goto leave;
1101
1102 info->sectors = ((u32)(reply[117]) << 24) |
1103 ((u32)(reply[116]) << 16) |
1104 ((u32)(reply[115]) << 8) |
1105 ((u32)(reply[114]) );
1106
1107 rc = USB_STOR_TRANSPORT_GOOD;
1108
1109 leave:
1110 kfree(reply);
1111 return rc;
1112}
1113
1114/*
1115 * Read data from device
1116 */
1117static int usbat_flash_read_data(struct us_data *us,
1118 struct usbat_info *info,
1119 u32 sector,
1120 u32 sectors)
1121{
1122 unsigned char registers[7] = {
1123 USBAT_ATA_FEATURES,
1124 USBAT_ATA_SECCNT,
1125 USBAT_ATA_SECNUM,
1126 USBAT_ATA_LBA_ME,
1127 USBAT_ATA_LBA_HI,
1128 USBAT_ATA_DEVICE,
1129 USBAT_ATA_STATUS,
1130 };
1131 unsigned char command[7];
1132 unsigned char *buffer;
1133 unsigned char thistime;
1134 unsigned int totallen, alloclen;
1135 int len, result;
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001136 unsigned int sg_offset = 0;
1137 struct scatterlist *sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 result = usbat_flash_check_media(us, info);
1140 if (result != USB_STOR_TRANSPORT_GOOD)
1141 return result;
1142
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001143 /*
1144 * we're working in LBA mode. according to the ATA spec,
1145 * we can support up to 28-bit addressing. I don't know if Jumpshot
1146 * supports beyond 24-bit addressing. It's kind of hard to test
1147 * since it requires > 8GB CF card.
1148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 if (sector > 0x0FFFFFFF)
1151 return USB_STOR_TRANSPORT_ERROR;
1152
1153 totallen = sectors * info->ssize;
1154
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001155 /*
1156 * Since we don't read more than 64 KB at a time, we have to create
1157 * a bounce buffer and move the data a piece at a time between the
1158 * bounce buffer and the actual transfer buffer.
1159 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 alloclen = min(totallen, 65536u);
1162 buffer = kmalloc(alloclen, GFP_NOIO);
1163 if (buffer == NULL)
1164 return USB_STOR_TRANSPORT_ERROR;
1165
1166 do {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001167 /*
1168 * loop, never allocate or transfer more than 64k at once
1169 * (min(128k, 255*info->ssize) is the real limit)
1170 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 len = min(totallen, alloclen);
1172 thistime = (len / info->ssize) & 0xff;
1173
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001174 /* ATA command 0x20 (READ SECTORS) */
1175 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001177 /* Write/execute ATA read command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 result = usbat_multiple_write(us, registers, command, 7);
1179 if (result != USB_STOR_TRANSPORT_GOOD)
1180 goto leave;
1181
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001182 /* Read the data we just requested */
Peter Chubb141804d2006-05-02 18:30:12 +01001183 result = usbat_read_blocks(us, buffer, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (result != USB_STOR_TRANSPORT_GOOD)
1185 goto leave;
1186
Joe Perches191648d2013-04-19 11:44:00 -07001187 usb_stor_dbg(us, "%d bytes\n", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001189 /* Store the data in the transfer buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 usb_stor_access_xfer_buf(buffer, len, us->srb,
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001191 &sg, &sg_offset, TO_XFER_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 sector += thistime;
1194 totallen -= len;
1195 } while (totallen > 0);
1196
1197 kfree(buffer);
1198 return USB_STOR_TRANSPORT_GOOD;
1199
1200leave:
1201 kfree(buffer);
1202 return USB_STOR_TRANSPORT_ERROR;
1203}
1204
1205/*
1206 * Write data to device
1207 */
1208static int usbat_flash_write_data(struct us_data *us,
1209 struct usbat_info *info,
1210 u32 sector,
1211 u32 sectors)
1212{
1213 unsigned char registers[7] = {
1214 USBAT_ATA_FEATURES,
1215 USBAT_ATA_SECCNT,
1216 USBAT_ATA_SECNUM,
1217 USBAT_ATA_LBA_ME,
1218 USBAT_ATA_LBA_HI,
1219 USBAT_ATA_DEVICE,
1220 USBAT_ATA_STATUS,
1221 };
1222 unsigned char command[7];
1223 unsigned char *buffer;
1224 unsigned char thistime;
1225 unsigned int totallen, alloclen;
1226 int len, result;
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001227 unsigned int sg_offset = 0;
1228 struct scatterlist *sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 result = usbat_flash_check_media(us, info);
1231 if (result != USB_STOR_TRANSPORT_GOOD)
1232 return result;
1233
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001234 /*
1235 * we're working in LBA mode. according to the ATA spec,
1236 * we can support up to 28-bit addressing. I don't know if the device
1237 * supports beyond 24-bit addressing. It's kind of hard to test
1238 * since it requires > 8GB media.
1239 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 if (sector > 0x0FFFFFFF)
1242 return USB_STOR_TRANSPORT_ERROR;
1243
1244 totallen = sectors * info->ssize;
1245
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001246 /*
1247 * Since we don't write more than 64 KB at a time, we have to create
1248 * a bounce buffer and move the data a piece at a time between the
1249 * bounce buffer and the actual transfer buffer.
1250 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 alloclen = min(totallen, 65536u);
1253 buffer = kmalloc(alloclen, GFP_NOIO);
1254 if (buffer == NULL)
1255 return USB_STOR_TRANSPORT_ERROR;
1256
1257 do {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001258 /*
1259 * loop, never allocate or transfer more than 64k at once
1260 * (min(128k, 255*info->ssize) is the real limit)
1261 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 len = min(totallen, alloclen);
1263 thistime = (len / info->ssize) & 0xff;
1264
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001265 /* Get the data from the transfer buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 usb_stor_access_xfer_buf(buffer, len, us->srb,
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001267 &sg, &sg_offset, FROM_XFER_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001269 /* ATA command 0x30 (WRITE SECTORS) */
1270 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001272 /* Write/execute ATA write command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 result = usbat_multiple_write(us, registers, command, 7);
1274 if (result != USB_STOR_TRANSPORT_GOOD)
1275 goto leave;
1276
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001277 /* Write the data */
Peter Chubb141804d2006-05-02 18:30:12 +01001278 result = usbat_write_blocks(us, buffer, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (result != USB_STOR_TRANSPORT_GOOD)
1280 goto leave;
1281
1282 sector += thistime;
1283 totallen -= len;
1284 } while (totallen > 0);
1285
1286 kfree(buffer);
1287 return result;
1288
1289leave:
1290 kfree(buffer);
1291 return USB_STOR_TRANSPORT_ERROR;
1292}
1293
1294/*
1295 * Squeeze a potentially huge (> 65535 byte) read10 command into
1296 * a little ( <= 65535 byte) ATAPI pipe
1297 */
1298static int usbat_hp8200e_handle_read10(struct us_data *us,
1299 unsigned char *registers,
1300 unsigned char *data,
1301 struct scsi_cmnd *srb)
1302{
1303 int result = USB_STOR_TRANSPORT_GOOD;
1304 unsigned char *buffer;
1305 unsigned int len;
1306 unsigned int sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 unsigned int sg_offset = 0;
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001308 struct scatterlist *sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Joe Perches191648d2013-04-19 11:44:00 -07001310 usb_stor_dbg(us, "transfersize %d\n", srb->transfersize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Boaz Harrosh4776e992007-09-09 20:40:56 +03001312 if (scsi_bufflen(srb) < 0x10000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1315 registers, data, 19,
1316 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1317 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1318 DMA_FROM_DEVICE,
Boaz Harrosh4776e992007-09-09 20:40:56 +03001319 scsi_sglist(srb),
1320 scsi_bufflen(srb), scsi_sg_count(srb), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 return result;
1323 }
1324
1325 /*
1326 * Since we're requesting more data than we can handle in
1327 * a single read command (max is 64k-1), we will perform
1328 * multiple reads, but each read must be in multiples of
1329 * a sector. Luckily the sector size is in srb->transfersize
1330 * (see linux/drivers/scsi/sr.c).
1331 */
1332
1333 if (data[7+0] == GPCMD_READ_CD) {
1334 len = short_pack(data[7+9], data[7+8]);
1335 len <<= 16;
1336 len |= data[7+7];
Joe Perches191648d2013-04-19 11:44:00 -07001337 usb_stor_dbg(us, "GPCMD_READ_CD: len %d\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +03001338 srb->transfersize = scsi_bufflen(srb)/len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340
1341 if (!srb->transfersize) {
1342 srb->transfersize = 2048; /* A guess */
Joe Perches191648d2013-04-19 11:44:00 -07001343 usb_stor_dbg(us, "transfersize 0, forcing %d\n",
1344 srb->transfersize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001347 /*
1348 * Since we only read in one block at a time, we have to create
1349 * a bounce buffer and move the data a piece at a time between the
1350 * bounce buffer and the actual transfer buffer.
1351 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 len = (65535/srb->transfersize) * srb->transfersize;
Joe Perches191648d2013-04-19 11:44:00 -07001354 usb_stor_dbg(us, "Max read is %d bytes\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +03001355 len = min(len, scsi_bufflen(srb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 buffer = kmalloc(len, GFP_NOIO);
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001357 if (buffer == NULL) /* bloody hell! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return USB_STOR_TRANSPORT_FAILED;
1359 sector = short_pack(data[7+3], data[7+2]);
1360 sector <<= 16;
1361 sector |= short_pack(data[7+5], data[7+4]);
1362 transferred = 0;
1363
Boaz Harrosh4776e992007-09-09 20:40:56 +03001364 while (transferred != scsi_bufflen(srb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Boaz Harrosh4776e992007-09-09 20:40:56 +03001366 if (len > scsi_bufflen(srb) - transferred)
1367 len = scsi_bufflen(srb) - transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001369 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1370 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001372 /* Fix up the SCSI command sector and num sectors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001374 data[7+2] = MSB_of(sector>>16); /* SCSI command sector */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 data[7+3] = LSB_of(sector>>16);
1376 data[7+4] = MSB_of(sector&0xFFFF);
1377 data[7+5] = LSB_of(sector&0xFFFF);
1378 if (data[7+0] == GPCMD_READ_CD)
1379 data[7+6] = 0;
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001380 data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */
1381 data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1384 registers, data, 19,
1385 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1386 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1387 DMA_FROM_DEVICE,
1388 buffer,
1389 len, 0, 1);
1390
1391 if (result != USB_STOR_TRANSPORT_GOOD)
1392 break;
1393
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001394 /* Store the data in the transfer buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 usb_stor_access_xfer_buf(buffer, len, srb,
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001396 &sg, &sg_offset, TO_XFER_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001398 /* Update the amount transferred and the sector number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 transferred += len;
1401 sector += len / srb->transfersize;
1402
Boaz Harrosh4776e992007-09-09 20:40:56 +03001403 } /* while transferred != scsi_bufflen(srb) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 kfree(buffer);
1406 return result;
1407}
1408
1409static int usbat_select_and_test_registers(struct us_data *us)
1410{
1411 int selector;
1412 unsigned char *status = us->iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001414 /* try device = master, then device = slave. */
Daniel Drake68a64572005-08-10 18:30:04 +01001415 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
1416 if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 USB_STOR_XFER_GOOD)
1418 return USB_STOR_TRANSPORT_ERROR;
1419
1420 if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) !=
1421 USB_STOR_XFER_GOOD)
1422 return USB_STOR_TRANSPORT_ERROR;
1423
1424 if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) !=
1425 USB_STOR_XFER_GOOD)
1426 return USB_STOR_TRANSPORT_ERROR;
1427
1428 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1429 USB_STOR_XFER_GOOD)
1430 return USB_STOR_TRANSPORT_ERROR;
1431
1432 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1433 USB_STOR_XFER_GOOD)
1434 return USB_STOR_TRANSPORT_ERROR;
1435
1436 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) !=
1437 USB_STOR_XFER_GOOD)
1438 return USB_STOR_TRANSPORT_ERROR;
1439
1440 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) !=
1441 USB_STOR_XFER_GOOD)
1442 return USB_STOR_TRANSPORT_ERROR;
1443
1444 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1445 USB_STOR_XFER_GOOD)
1446 return USB_STOR_TRANSPORT_ERROR;
1447
1448 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1449 USB_STOR_XFER_GOOD)
1450 return USB_STOR_TRANSPORT_ERROR;
1451 }
1452
1453 return USB_STOR_TRANSPORT_GOOD;
1454}
1455
1456/*
1457 * Initialize the USBAT processor and the storage device
1458 */
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001459static int init_usbat(struct us_data *us, int devicetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 int rc;
1462 struct usbat_info *info;
1463 unsigned char subcountH = USBAT_ATA_LBA_HI;
1464 unsigned char subcountL = USBAT_ATA_LBA_ME;
1465 unsigned char *status = us->iobuf;
1466
Oliver Neukum887c2562006-01-08 12:33:45 +01001467 us->extra = kzalloc(sizeof(struct usbat_info), GFP_NOIO);
Joe Perches191648d2013-04-19 11:44:00 -07001468 if (!us->extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return 1;
Joe Perches191648d2013-04-19 11:44:00 -07001470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 info = (struct usbat_info *) (us->extra);
1472
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001473 /* Enable peripheral control signals */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 rc = usbat_write_user_io(us,
1475 USBAT_UIO_OE1 | USBAT_UIO_OE0,
1476 USBAT_UIO_EPAD | USBAT_UIO_1);
1477 if (rc != USB_STOR_XFER_GOOD)
1478 return USB_STOR_TRANSPORT_ERROR;
1479
Joe Perches191648d2013-04-19 11:44:00 -07001480 usb_stor_dbg(us, "INIT 1\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 msleep(2000);
1483
1484 rc = usbat_read_user_io(us, status);
1485 if (rc != USB_STOR_TRANSPORT_GOOD)
1486 return rc;
1487
Joe Perches191648d2013-04-19 11:44:00 -07001488 usb_stor_dbg(us, "INIT 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 rc = usbat_read_user_io(us, status);
1491 if (rc != USB_STOR_XFER_GOOD)
1492 return USB_STOR_TRANSPORT_ERROR;
1493
1494 rc = usbat_read_user_io(us, status);
1495 if (rc != USB_STOR_XFER_GOOD)
1496 return USB_STOR_TRANSPORT_ERROR;
1497
Joe Perches191648d2013-04-19 11:44:00 -07001498 usb_stor_dbg(us, "INIT 3\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 rc = usbat_select_and_test_registers(us);
1501 if (rc != USB_STOR_TRANSPORT_GOOD)
1502 return rc;
1503
Joe Perches191648d2013-04-19 11:44:00 -07001504 usb_stor_dbg(us, "INIT 4\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 rc = usbat_read_user_io(us, status);
1507 if (rc != USB_STOR_XFER_GOOD)
1508 return USB_STOR_TRANSPORT_ERROR;
1509
Joe Perches191648d2013-04-19 11:44:00 -07001510 usb_stor_dbg(us, "INIT 5\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001512 /* Enable peripheral control signals and card detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 rc = usbat_device_enable_cdt(us);
1514 if (rc != USB_STOR_TRANSPORT_GOOD)
1515 return rc;
1516
Joe Perches191648d2013-04-19 11:44:00 -07001517 usb_stor_dbg(us, "INIT 6\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 rc = usbat_read_user_io(us, status);
1520 if (rc != USB_STOR_XFER_GOOD)
1521 return USB_STOR_TRANSPORT_ERROR;
1522
Joe Perches191648d2013-04-19 11:44:00 -07001523 usb_stor_dbg(us, "INIT 7\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 msleep(1400);
1526
1527 rc = usbat_read_user_io(us, status);
1528 if (rc != USB_STOR_XFER_GOOD)
1529 return USB_STOR_TRANSPORT_ERROR;
1530
Joe Perches191648d2013-04-19 11:44:00 -07001531 usb_stor_dbg(us, "INIT 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 rc = usbat_select_and_test_registers(us);
1534 if (rc != USB_STOR_TRANSPORT_GOOD)
1535 return rc;
1536
Joe Perches191648d2013-04-19 11:44:00 -07001537 usb_stor_dbg(us, "INIT 9\n");
Daniel Drake68a64572005-08-10 18:30:04 +01001538
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001539 /* At this point, we need to detect which device we are using */
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001540 if (usbat_set_transport(us, info, devicetype))
Daniel Drake68a64572005-08-10 18:30:04 +01001541 return USB_STOR_TRANSPORT_ERROR;
1542
Joe Perches191648d2013-04-19 11:44:00 -07001543 usb_stor_dbg(us, "INIT 10\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 if (usbat_get_device_type(us) == USBAT_DEV_FLASH) {
1546 subcountH = 0x02;
1547 subcountL = 0x00;
1548 }
1549 rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1550 0x00, 0x88, 0x08, subcountH, subcountL);
1551 if (rc != USB_STOR_XFER_GOOD)
1552 return USB_STOR_TRANSPORT_ERROR;
1553
Joe Perches191648d2013-04-19 11:44:00 -07001554 usb_stor_dbg(us, "INIT 11\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 return USB_STOR_TRANSPORT_GOOD;
1557}
1558
1559/*
1560 * Transport for the HP 8200e
1561 */
1562static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1563{
1564 int result;
1565 unsigned char *status = us->iobuf;
1566 unsigned char registers[32];
1567 unsigned char data[32];
1568 unsigned int len;
1569 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Boaz Harrosh4776e992007-09-09 20:40:56 +03001571 len = scsi_bufflen(srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 /* Send A0 (ATA PACKET COMMAND).
1574 Note: I guess we're never going to get any of the ATA
1575 commands... just ATA Packet Commands.
1576 */
1577
1578 registers[0] = USBAT_ATA_FEATURES;
1579 registers[1] = USBAT_ATA_SECCNT;
1580 registers[2] = USBAT_ATA_SECNUM;
1581 registers[3] = USBAT_ATA_LBA_ME;
1582 registers[4] = USBAT_ATA_LBA_HI;
1583 registers[5] = USBAT_ATA_DEVICE;
1584 registers[6] = USBAT_ATA_CMD;
1585 data[0] = 0x00;
1586 data[1] = 0x00;
1587 data[2] = 0x00;
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001588 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1589 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
1590 data[5] = 0xB0; /* (device sel) = slave */
1591 data[6] = 0xA0; /* (command) = ATA PACKET COMMAND */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 for (i=7; i<19; i++) {
1594 registers[i] = 0x10;
1595 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1596 }
1597
1598 result = usbat_get_status(us, status);
Joe Perches191648d2013-04-19 11:44:00 -07001599 usb_stor_dbg(us, "Status = %02X\n", *status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (result != USB_STOR_XFER_GOOD)
1601 return USB_STOR_TRANSPORT_ERROR;
1602 if (srb->cmnd[0] == TEST_UNIT_READY)
1603 transferred = 0;
1604
1605 if (srb->sc_data_direction == DMA_TO_DEVICE) {
1606
1607 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1608 registers, data, 19,
1609 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1610 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1611 DMA_TO_DEVICE,
Boaz Harrosh4776e992007-09-09 20:40:56 +03001612 scsi_sglist(srb),
1613 len, scsi_sg_count(srb), 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 if (result == USB_STOR_TRANSPORT_GOOD) {
1616 transferred += len;
Joe Perches191648d2013-04-19 11:44:00 -07001617 usb_stor_dbg(us, "Wrote %08X bytes\n", transferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
1619
1620 return result;
1621
1622 } else if (srb->cmnd[0] == READ_10 ||
1623 srb->cmnd[0] == GPCMD_READ_CD) {
1624
1625 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1626
1627 }
1628
1629 if (len > 0xFFFF) {
Joe Perches191648d2013-04-19 11:44:00 -07001630 usb_stor_dbg(us, "Error: len = %08X... what do I do now?\n",
1631 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return USB_STOR_TRANSPORT_ERROR;
1633 }
1634
Roel Kluin5d3034a2010-02-17 11:50:26 +01001635 result = usbat_multiple_write(us, registers, data, 7);
1636
1637 if (result != USB_STOR_TRANSPORT_GOOD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001640 /*
1641 * Write the 12-byte command header.
1642 *
1643 * If the command is BLANK then set the timer for 75 minutes.
1644 * Otherwise set it for 10 minutes.
1645 *
1646 * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1647 * AT SPEED 4 IS UNRELIABLE!!!
1648 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Roel Kluin5d3034a2010-02-17 11:50:26 +01001650 result = usbat_write_block(us, USBAT_ATA, srb->cmnd, 12,
1651 srb->cmnd[0] == GPCMD_BLANK ? 75 : 10, 0);
1652
1653 if (result != USB_STOR_TRANSPORT_GOOD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001656 /* If there is response data to be read in then do it here. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1659
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001660 /* How many bytes to read in? Check cylL register */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1663 USB_STOR_XFER_GOOD) {
1664 return USB_STOR_TRANSPORT_ERROR;
1665 }
1666
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001667 if (len > 0xFF) { /* need to read cylH also */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 len = *status;
1669 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1670 USB_STOR_XFER_GOOD) {
1671 return USB_STOR_TRANSPORT_ERROR;
1672 }
1673 len += ((unsigned int) *status)<<8;
1674 }
1675 else
1676 len = *status;
1677
1678
Boaz Harrosh4776e992007-09-09 20:40:56 +03001679 result = usbat_read_block(us, scsi_sglist(srb), len,
1680 scsi_sg_count(srb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682
1683 return result;
1684}
1685
1686/*
1687 * Transport for USBAT02-based CompactFlash and similar storage devices
1688 */
1689static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1690{
1691 int rc;
1692 struct usbat_info *info = (struct usbat_info *) (us->extra);
1693 unsigned long block, blocks;
1694 unsigned char *ptr = us->iobuf;
1695 static unsigned char inquiry_response[36] = {
1696 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1697 };
1698
1699 if (srb->cmnd[0] == INQUIRY) {
Joe Perches191648d2013-04-19 11:44:00 -07001700 usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1702 fill_inquiry_response(us, ptr, 36);
1703 return USB_STOR_TRANSPORT_GOOD;
1704 }
1705
1706 if (srb->cmnd[0] == READ_CAPACITY) {
1707 rc = usbat_flash_check_media(us, info);
1708 if (rc != USB_STOR_TRANSPORT_GOOD)
1709 return rc;
1710
1711 rc = usbat_flash_get_sector_count(us, info);
1712 if (rc != USB_STOR_TRANSPORT_GOOD)
1713 return rc;
1714
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001715 /* hard coded 512 byte sectors as per ATA spec */
1716 info->ssize = 0x200;
Joe Perches191648d2013-04-19 11:44:00 -07001717 usb_stor_dbg(us, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1718 info->sectors, info->ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001720 /*
1721 * build the reply
1722 * note: must return the sector number of the last sector,
1723 * *not* the total number of sectors
1724 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1726 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1727 usb_stor_set_xfer_buf(ptr, 8, srb);
1728
1729 return USB_STOR_TRANSPORT_GOOD;
1730 }
1731
1732 if (srb->cmnd[0] == MODE_SELECT_10) {
Joe Perches191648d2013-04-19 11:44:00 -07001733 usb_stor_dbg(us, "Gah! MODE_SELECT_10\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return USB_STOR_TRANSPORT_ERROR;
1735 }
1736
1737 if (srb->cmnd[0] == READ_10) {
1738 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1739 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1740
1741 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1742
Joe Perches191648d2013-04-19 11:44:00 -07001743 usb_stor_dbg(us, "READ_10: read block 0x%04lx count %ld\n",
1744 block, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return usbat_flash_read_data(us, info, block, blocks);
1746 }
1747
1748 if (srb->cmnd[0] == READ_12) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001749 /*
1750 * I don't think we'll ever see a READ_12 but support it anyway
1751 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1753 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1754
1755 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1756 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1757
Joe Perches191648d2013-04-19 11:44:00 -07001758 usb_stor_dbg(us, "READ_12: read block 0x%04lx count %ld\n",
1759 block, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return usbat_flash_read_data(us, info, block, blocks);
1761 }
1762
1763 if (srb->cmnd[0] == WRITE_10) {
1764 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1765 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1766
1767 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1768
Joe Perches191648d2013-04-19 11:44:00 -07001769 usb_stor_dbg(us, "WRITE_10: write block 0x%04lx count %ld\n",
1770 block, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 return usbat_flash_write_data(us, info, block, blocks);
1772 }
1773
1774 if (srb->cmnd[0] == WRITE_12) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001775 /*
1776 * I don't think we'll ever see a WRITE_12 but support it anyway
1777 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1779 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1780
1781 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1782 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1783
Joe Perches191648d2013-04-19 11:44:00 -07001784 usb_stor_dbg(us, "WRITE_12: write block 0x%04lx count %ld\n",
1785 block, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return usbat_flash_write_data(us, info, block, blocks);
1787 }
1788
1789
1790 if (srb->cmnd[0] == TEST_UNIT_READY) {
Joe Perches191648d2013-04-19 11:44:00 -07001791 usb_stor_dbg(us, "TEST_UNIT_READY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 rc = usbat_flash_check_media(us, info);
1794 if (rc != USB_STOR_TRANSPORT_GOOD)
1795 return rc;
1796
1797 return usbat_check_status(us);
1798 }
1799
1800 if (srb->cmnd[0] == REQUEST_SENSE) {
Joe Perches191648d2013-04-19 11:44:00 -07001801 usb_stor_dbg(us, "REQUEST_SENSE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 memset(ptr, 0, 18);
1804 ptr[0] = 0xF0;
1805 ptr[2] = info->sense_key;
1806 ptr[7] = 11;
1807 ptr[12] = info->sense_asc;
1808 ptr[13] = info->sense_ascq;
1809 usb_stor_set_xfer_buf(ptr, 18, srb);
1810
1811 return USB_STOR_TRANSPORT_GOOD;
1812 }
1813
1814 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001815 /*
1816 * sure. whatever. not like we can stop the user from popping
1817 * the media out of the device (no locking doors, etc)
1818 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return USB_STOR_TRANSPORT_GOOD;
1820 }
1821
Joe Perches191648d2013-04-19 11:44:00 -07001822 usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
1823 srb->cmnd[0], srb->cmnd[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 info->sense_key = 0x05;
1825 info->sense_asc = 0x20;
1826 info->sense_ascq = 0x00;
1827 return USB_STOR_TRANSPORT_FAILED;
1828}
1829
Alan Stern26d68182009-02-12 14:48:08 -05001830static int init_usbat_cd(struct us_data *us)
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001831{
1832 return init_usbat(us, USBAT_DEV_HP8200);
1833}
1834
Alan Stern26d68182009-02-12 14:48:08 -05001835static int init_usbat_flash(struct us_data *us)
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001836{
1837 return init_usbat(us, USBAT_DEV_FLASH);
1838}
1839
Akinobu Mitaaa519be2015-05-06 18:24:21 +09001840static struct scsi_host_template usbat_host_template;
1841
Alan Stern26d68182009-02-12 14:48:08 -05001842static int usbat_probe(struct usb_interface *intf,
1843 const struct usb_device_id *id)
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001844{
Alan Stern26d68182009-02-12 14:48:08 -05001845 struct us_data *us;
1846 int result;
1847
1848 result = usb_stor_probe1(&us, intf, id,
Akinobu Mitaaa519be2015-05-06 18:24:21 +09001849 (id - usbat_usb_ids) + usbat_unusual_dev_list,
1850 &usbat_host_template);
Alan Stern26d68182009-02-12 14:48:08 -05001851 if (result)
1852 return result;
1853
1854 /* The actual transport will be determined later by the
1855 * initialization routine; this is just a placeholder.
1856 */
1857 us->transport_name = "Shuttle USBAT";
1858 us->transport = usbat_flash_transport;
1859 us->transport_reset = usb_stor_CB_reset;
Daniele Forsidf602c22014-04-29 11:44:03 +02001860 us->max_lun = 0;
Alan Stern26d68182009-02-12 14:48:08 -05001861
1862 result = usb_stor_probe2(us);
1863 return result;
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001864}
1865
Alan Stern26d68182009-02-12 14:48:08 -05001866static struct usb_driver usbat_driver = {
Akinobu Mitaaa519be2015-05-06 18:24:21 +09001867 .name = DRV_NAME,
Alan Stern26d68182009-02-12 14:48:08 -05001868 .probe = usbat_probe,
1869 .disconnect = usb_stor_disconnect,
1870 .suspend = usb_stor_suspend,
1871 .resume = usb_stor_resume,
1872 .reset_resume = usb_stor_reset_resume,
1873 .pre_reset = usb_stor_pre_reset,
1874 .post_reset = usb_stor_post_reset,
1875 .id_table = usbat_usb_ids,
1876 .soft_unbind = 1,
Huajun Lie73b2db2012-01-14 10:15:21 +08001877 .no_dynamic_id = 1,
Alan Stern26d68182009-02-12 14:48:08 -05001878};
1879
Akinobu Mitaaa519be2015-05-06 18:24:21 +09001880module_usb_stor_driver(usbat_driver, usbat_host_template, DRV_NAME);