blob: 008d805c3d21cde7458058a6a2a5b0803da92bcf [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
Maciej Grela4246b062009-02-28 12:39:20 -080057MODULE_DESCRIPTION("Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable");
58MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>, Robert Baruch <autophile@starband.net>");
59MODULE_LICENSE("GPL");
Alan Stern26d68182009-02-12 14:48:08 -050060
61/* Supported device types */
62#define USBAT_DEV_HP8200 0x01
63#define USBAT_DEV_FLASH 0x02
64
65#define USBAT_EPP_PORT 0x10
66#define USBAT_EPP_REGISTER 0x30
67#define USBAT_ATA 0x40
68#define USBAT_ISA 0x50
69
70/* Commands (need to be logically OR'd with an access type */
71#define USBAT_CMD_READ_REG 0x00
72#define USBAT_CMD_WRITE_REG 0x01
73#define USBAT_CMD_READ_BLOCK 0x02
74#define USBAT_CMD_WRITE_BLOCK 0x03
75#define USBAT_CMD_COND_READ_BLOCK 0x04
76#define USBAT_CMD_COND_WRITE_BLOCK 0x05
77#define USBAT_CMD_WRITE_REGS 0x07
78
79/* Commands (these don't need an access type) */
80#define USBAT_CMD_EXEC_CMD 0x80
81#define USBAT_CMD_SET_FEAT 0x81
82#define USBAT_CMD_UIO 0x82
83
84/* Methods of accessing UIO register */
85#define USBAT_UIO_READ 1
86#define USBAT_UIO_WRITE 0
87
88/* Qualifier bits */
89#define USBAT_QUAL_FCQ 0x20 /* full compare */
90#define USBAT_QUAL_ALQ 0x10 /* auto load subcount */
91
92/* USBAT Flash Media status types */
93#define USBAT_FLASH_MEDIA_NONE 0
94#define USBAT_FLASH_MEDIA_CF 1
95
96/* USBAT Flash Media change types */
97#define USBAT_FLASH_MEDIA_SAME 0
98#define USBAT_FLASH_MEDIA_CHANGED 1
99
100/* USBAT ATA registers */
101#define USBAT_ATA_DATA 0x10 /* read/write data (R/W) */
102#define USBAT_ATA_FEATURES 0x11 /* set features (W) */
103#define USBAT_ATA_ERROR 0x11 /* error (R) */
104#define USBAT_ATA_SECCNT 0x12 /* sector count (R/W) */
105#define USBAT_ATA_SECNUM 0x13 /* sector number (R/W) */
106#define USBAT_ATA_LBA_ME 0x14 /* cylinder low (R/W) */
107#define USBAT_ATA_LBA_HI 0x15 /* cylinder high (R/W) */
108#define USBAT_ATA_DEVICE 0x16 /* head/device selection (R/W) */
109#define USBAT_ATA_STATUS 0x17 /* device status (R) */
110#define USBAT_ATA_CMD 0x17 /* device command (W) */
111#define USBAT_ATA_ALTSTATUS 0x0E /* status (no clear IRQ) (R) */
112
113/* USBAT User I/O Data registers */
114#define USBAT_UIO_EPAD 0x80 /* Enable Peripheral Control Signals */
115#define USBAT_UIO_CDT 0x40 /* Card Detect (Read Only) */
116 /* CDT = ACKD & !UI1 & !UI0 */
117#define USBAT_UIO_1 0x20 /* I/O 1 */
118#define USBAT_UIO_0 0x10 /* I/O 0 */
119#define USBAT_UIO_EPP_ATA 0x08 /* 1=EPP mode, 0=ATA mode */
120#define USBAT_UIO_UI1 0x04 /* Input 1 */
121#define USBAT_UIO_UI0 0x02 /* Input 0 */
122#define USBAT_UIO_INTR_ACK 0x01 /* Interrupt (ATA/ISA)/Acknowledge (EPP) */
123
124/* USBAT User I/O Enable registers */
125#define USBAT_UIO_DRVRST 0x80 /* Reset Peripheral */
126#define USBAT_UIO_ACKD 0x40 /* Enable Card Detect */
127#define USBAT_UIO_OE1 0x20 /* I/O 1 set=output/clr=input */
128 /* If ACKD=1, set OE1 to 1 also. */
129#define USBAT_UIO_OE0 0x10 /* I/O 0 set=output/clr=input */
130#define USBAT_UIO_ADPRST 0x01 /* Reset SCM chip */
131
132/* USBAT Features */
133#define USBAT_FEAT_ETEN 0x80 /* External trigger enable */
134#define USBAT_FEAT_U1 0x08
135#define USBAT_FEAT_U0 0x04
136#define USBAT_FEAT_ET1 0x02
137#define USBAT_FEAT_ET2 0x01
138
139struct usbat_info {
140 int devicetype;
141
142 /* Used for Flash readers only */
143 unsigned long sectors; /* total sector count */
144 unsigned long ssize; /* sector size in bytes */
145
146 unsigned char sense_key;
147 unsigned long sense_asc; /* additional sense code */
148 unsigned long sense_ascq; /* additional sense code qualifier */
149};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151#define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
152#define LSB_of(s) ((s)&0xFF)
153#define MSB_of(s) ((s)>>8)
154
155static int transferred = 0;
156
157static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us);
158static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us);
159
Alan Stern26d68182009-02-12 14:48:08 -0500160static int init_usbat_cd(struct us_data *us);
161static int init_usbat_flash(struct us_data *us);
162
163
164/*
165 * The table of devices
166 */
167#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
168 vendorName, productName, useProtocol, useTransport, \
169 initFunction, flags) \
170{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
Sebastian Andrzej Siewiorf61870e2012-08-28 22:37:13 +0200171 .driver_info = (flags) }
Alan Stern26d68182009-02-12 14:48:08 -0500172
Felipe Balbice3af892011-11-15 09:53:40 +0200173static struct usb_device_id usbat_usb_ids[] = {
Alan Stern26d68182009-02-12 14:48:08 -0500174# include "unusual_usbat.h"
175 { } /* Terminating entry */
176};
177MODULE_DEVICE_TABLE(usb, usbat_usb_ids);
178
179#undef UNUSUAL_DEV
180
181/*
182 * The flags table
183 */
184#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
185 vendor_name, product_name, use_protocol, use_transport, \
186 init_function, Flags) \
187{ \
188 .vendorName = vendor_name, \
189 .productName = product_name, \
190 .useProtocol = use_protocol, \
191 .useTransport = use_transport, \
192 .initFunction = init_function, \
193}
194
195static struct us_unusual_dev usbat_unusual_dev_list[] = {
196# include "unusual_usbat.h"
197 { } /* Terminating entry */
198};
199
200#undef UNUSUAL_DEV
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/*
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100203 * Convenience function to produce an ATA read/write sectors command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * Use cmd=0x20 for read, cmd=0x30 for write
205 */
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100206static void usbat_pack_ata_sector_cmd(unsigned char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 unsigned char thistime,
208 u32 sector, unsigned char cmd)
209{
210 buf[0] = 0;
211 buf[1] = thistime;
212 buf[2] = sector & 0xFF;
213 buf[3] = (sector >> 8) & 0xFF;
214 buf[4] = (sector >> 16) & 0xFF;
215 buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
216 buf[6] = cmd;
217}
218
219/*
220 * Convenience function to get the device type (flash or hp8200)
221 */
222static int usbat_get_device_type(struct us_data *us)
223{
224 return ((struct usbat_info*)us->extra)->devicetype;
225}
226
227/*
228 * Read a register from the device
229 */
230static int usbat_read(struct us_data *us,
231 unsigned char access,
232 unsigned char reg,
233 unsigned char *content)
234{
235 return usb_stor_ctrl_transfer(us,
236 us->recv_ctrl_pipe,
237 access | USBAT_CMD_READ_REG,
238 0xC0,
239 (u16)reg,
240 0,
241 content,
242 1);
243}
244
245/*
246 * Write to a register on the device
247 */
248static int usbat_write(struct us_data *us,
249 unsigned char access,
250 unsigned char reg,
251 unsigned char content)
252{
253 return usb_stor_ctrl_transfer(us,
254 us->send_ctrl_pipe,
255 access | USBAT_CMD_WRITE_REG,
256 0x40,
257 short_pack(reg, content),
258 0,
259 NULL,
260 0);
261}
262
263/*
264 * Convenience function to perform a bulk read
265 */
266static int usbat_bulk_read(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300267 void* buf,
Peter Chubb141804d2006-05-02 18:30:12 +0100268 unsigned int len,
269 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 if (len == 0)
272 return USB_STOR_XFER_GOOD;
273
Joe Perches191648d2013-04-19 11:44:00 -0700274 usb_stor_dbg(us, "len = %d\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +0300275 return usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe, buf, len, use_sg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/*
279 * Convenience function to perform a bulk write
280 */
281static int usbat_bulk_write(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300282 void* buf,
Peter Chubb141804d2006-05-02 18:30:12 +0100283 unsigned int len,
284 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 if (len == 0)
287 return USB_STOR_XFER_GOOD;
288
Joe Perches191648d2013-04-19 11:44:00 -0700289 usb_stor_dbg(us, "len = %d\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +0300290 return usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe, buf, len, use_sg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293/*
294 * Some USBAT-specific commands can only be executed over a command transport
295 * This transport allows one (len=8) or two (len=16) vendor-specific commands
296 * to be executed.
297 */
298static int usbat_execute_command(struct us_data *us,
299 unsigned char *commands,
300 unsigned int len)
301{
302 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
303 USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
304 commands, len);
305}
306
307/*
308 * Read the status register
309 */
310static int usbat_get_status(struct us_data *us, unsigned char *status)
311{
312 int rc;
313 rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
314
Joe Perches191648d2013-04-19 11:44:00 -0700315 usb_stor_dbg(us, "0x%02X\n", *status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return rc;
317}
318
319/*
320 * Check the device status
321 */
322static int usbat_check_status(struct us_data *us)
323{
324 unsigned char *reply = us->iobuf;
325 int rc;
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 rc = usbat_get_status(us, reply);
328 if (rc != USB_STOR_XFER_GOOD)
329 return USB_STOR_TRANSPORT_FAILED;
330
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100331 /* error/check condition (0x51 is ok) */
332 if (*reply & 0x01 && *reply != 0x51)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return USB_STOR_TRANSPORT_FAILED;
334
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100335 /* device fault */
336 if (*reply & 0x20)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return USB_STOR_TRANSPORT_FAILED;
338
339 return USB_STOR_TRANSPORT_GOOD;
340}
341
342/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300343 * Stores critical information in internal registers in preparation for the execution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 * of a conditional usbat_read_blocks or usbat_write_blocks call.
345 */
346static int usbat_set_shuttle_features(struct us_data *us,
347 unsigned char external_trigger,
348 unsigned char epp_control,
349 unsigned char mask_byte,
350 unsigned char test_pattern,
351 unsigned char subcountH,
352 unsigned char subcountL)
353{
354 unsigned char *command = us->iobuf;
355
356 command[0] = 0x40;
357 command[1] = USBAT_CMD_SET_FEAT;
358
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100359 /*
360 * The only bit relevant to ATA access is bit 6
361 * which defines 8 bit data access (set) or 16 bit (unset)
362 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 command[2] = epp_control;
364
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100365 /*
366 * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
367 * ET1 and ET2 define an external event to be checked for on event of a
368 * _read_blocks or _write_blocks operation. The read/write will not take
369 * place unless the defined trigger signal is active.
370 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 command[3] = external_trigger;
372
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100373 /*
374 * The resultant byte of the mask operation (see mask_byte) is compared for
375 * equivalence with this test pattern. If equal, the read/write will take
376 * place.
377 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 command[4] = test_pattern;
379
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100380 /*
381 * This value is logically ANDed with the status register field specified
382 * in the read/write command.
383 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 command[5] = mask_byte;
385
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100386 /*
387 * If ALQ is set in the qualifier, this field contains the address of the
388 * registers where the byte count should be read for transferring the data.
389 * If ALQ is not set, then this field contains the number of bytes to be
390 * transferred.
391 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 command[6] = subcountL;
393 command[7] = subcountH;
394
395 return usbat_execute_command(us, command, 8);
396}
397
398/*
399 * Block, waiting for an ATA device to become not busy or to report
400 * an error condition.
401 */
402static int usbat_wait_not_busy(struct us_data *us, int minutes)
403{
404 int i;
405 int result;
406 unsigned char *status = us->iobuf;
407
408 /* Synchronizing cache on a CDR could take a heck of a long time,
409 * but probably not more than 10 minutes or so. On the other hand,
410 * doing a full blank on a CDRW at speed 1 will take about 75
411 * minutes!
412 */
413
414 for (i=0; i<1200+minutes*60; i++) {
415
416 result = usbat_get_status(us, status);
417
418 if (result!=USB_STOR_XFER_GOOD)
419 return USB_STOR_TRANSPORT_ERROR;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100420 if (*status & 0x01) { /* check condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 result = usbat_read(us, USBAT_ATA, 0x10, status);
422 return USB_STOR_TRANSPORT_FAILED;
423 }
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100424 if (*status & 0x20) /* device fault */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return USB_STOR_TRANSPORT_FAILED;
426
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100427 if ((*status & 0x80)==0x00) { /* not busy */
Joe Perches191648d2013-04-19 11:44:00 -0700428 usb_stor_dbg(us, "Waited not busy for %d steps\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return USB_STOR_TRANSPORT_GOOD;
430 }
431
432 if (i<500)
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100433 msleep(10); /* 5 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 else if (i<700)
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100435 msleep(50); /* 10 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 else if (i<1200)
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100437 msleep(100); /* 50 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 else
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100439 msleep(1000); /* X minutes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
Joe Perches191648d2013-04-19 11:44:00 -0700442 usb_stor_dbg(us, "Waited not busy for %d minutes, timing out\n",
443 minutes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return USB_STOR_TRANSPORT_FAILED;
445}
446
447/*
448 * Read block data from the data register
449 */
450static int usbat_read_block(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300451 void* buf,
Peter Chubb141804d2006-05-02 18:30:12 +0100452 unsigned short len,
453 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 int result;
456 unsigned char *command = us->iobuf;
457
458 if (!len)
459 return USB_STOR_TRANSPORT_GOOD;
460
461 command[0] = 0xC0;
462 command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
463 command[2] = USBAT_ATA_DATA;
464 command[3] = 0;
465 command[4] = 0;
466 command[5] = 0;
467 command[6] = LSB_of(len);
468 command[7] = MSB_of(len);
469
470 result = usbat_execute_command(us, command, 8);
471 if (result != USB_STOR_XFER_GOOD)
472 return USB_STOR_TRANSPORT_ERROR;
473
Boaz Harrosh4776e992007-09-09 20:40:56 +0300474 result = usbat_bulk_read(us, buf, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return (result == USB_STOR_XFER_GOOD ?
476 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
477}
478
479/*
480 * Write block data via the data register
481 */
482static int usbat_write_block(struct us_data *us,
483 unsigned char access,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300484 void* buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 unsigned short len,
Peter Chubb141804d2006-05-02 18:30:12 +0100486 int minutes,
487 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 int result;
490 unsigned char *command = us->iobuf;
491
492 if (!len)
493 return USB_STOR_TRANSPORT_GOOD;
494
495 command[0] = 0x40;
496 command[1] = access | USBAT_CMD_WRITE_BLOCK;
497 command[2] = USBAT_ATA_DATA;
498 command[3] = 0;
499 command[4] = 0;
500 command[5] = 0;
501 command[6] = LSB_of(len);
502 command[7] = MSB_of(len);
503
504 result = usbat_execute_command(us, command, 8);
505
506 if (result != USB_STOR_XFER_GOOD)
507 return USB_STOR_TRANSPORT_ERROR;
508
Boaz Harrosh4776e992007-09-09 20:40:56 +0300509 result = usbat_bulk_write(us, buf, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (result != USB_STOR_XFER_GOOD)
511 return USB_STOR_TRANSPORT_ERROR;
512
513 return usbat_wait_not_busy(us, minutes);
514}
515
516/*
517 * Process read and write requests
518 */
519static int usbat_hp8200e_rw_block_test(struct us_data *us,
520 unsigned char access,
521 unsigned char *registers,
522 unsigned char *data_out,
523 unsigned short num_registers,
524 unsigned char data_reg,
525 unsigned char status_reg,
526 unsigned char timeout,
527 unsigned char qualifier,
528 int direction,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300529 void *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 unsigned short len,
531 int use_sg,
532 int minutes)
533{
534 int result;
535 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
536 us->recv_bulk_pipe : us->send_bulk_pipe;
537
538 unsigned char *command = us->iobuf;
539 int i, j;
540 int cmdlen;
541 unsigned char *data = us->iobuf;
542 unsigned char *status = us->iobuf;
543
544 BUG_ON(num_registers > US_IOBUF_SIZE/2);
545
546 for (i=0; i<20; i++) {
547
548 /*
549 * The first time we send the full command, which consists
550 * of downloading the SCSI command followed by downloading
551 * the data via a write-and-test. Any other time we only
552 * send the command to download the data -- the SCSI command
553 * is still 'active' in some sense in the device.
554 *
555 * We're only going to try sending the data 10 times. After
556 * that, we just return a failure.
557 */
558
559 if (i==0) {
560 cmdlen = 16;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100561 /*
562 * Write to multiple registers
563 * Not really sure the 0x07, 0x17, 0xfc, 0xe7 is
564 * necessary here, but that's what came out of the
565 * trace every single time.
566 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 command[0] = 0x40;
568 command[1] = access | USBAT_CMD_WRITE_REGS;
569 command[2] = 0x07;
570 command[3] = 0x17;
571 command[4] = 0xFC;
572 command[5] = 0xE7;
573 command[6] = LSB_of(num_registers*2);
574 command[7] = MSB_of(num_registers*2);
575 } else
576 cmdlen = 8;
577
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100578 /* Conditionally read or write blocks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 command[cmdlen-8] = (direction==DMA_TO_DEVICE ? 0x40 : 0xC0);
580 command[cmdlen-7] = access |
581 (direction==DMA_TO_DEVICE ?
582 USBAT_CMD_COND_WRITE_BLOCK : USBAT_CMD_COND_READ_BLOCK);
583 command[cmdlen-6] = data_reg;
584 command[cmdlen-5] = status_reg;
585 command[cmdlen-4] = timeout;
586 command[cmdlen-3] = qualifier;
587 command[cmdlen-2] = LSB_of(len);
588 command[cmdlen-1] = MSB_of(len);
589
590 result = usbat_execute_command(us, command, cmdlen);
591
592 if (result != USB_STOR_XFER_GOOD)
593 return USB_STOR_TRANSPORT_ERROR;
594
595 if (i==0) {
596
597 for (j=0; j<num_registers; j++) {
598 data[j<<1] = registers[j];
599 data[1+(j<<1)] = data_out[j];
600 }
601
Peter Chubb141804d2006-05-02 18:30:12 +0100602 result = usbat_bulk_write(us, data, num_registers*2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (result != USB_STOR_XFER_GOOD)
604 return USB_STOR_TRANSPORT_ERROR;
605
606 }
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 result = usb_stor_bulk_transfer_sg(us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300609 pipe, buf, len, use_sg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 /*
612 * If we get a stall on the bulk download, we'll retry
613 * the bulk download -- but not the SCSI command because
614 * in some sense the SCSI command is still 'active' and
615 * waiting for the data. Don't ask me why this should be;
616 * I'm only following what the Windoze driver did.
617 *
618 * Note that a stall for the test-and-read/write command means
619 * that the test failed. In this case we're testing to make
620 * sure that the device is error-free
621 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
622 * hypothesis is that the USBAT chip somehow knows what
623 * the device will accept, but doesn't give the device any
624 * data until all data is received. Thus, the device would
625 * still be waiting for the first byte of data if a stall
626 * occurs, even if the stall implies that some data was
627 * transferred.
628 */
629
630 if (result == USB_STOR_XFER_SHORT ||
631 result == USB_STOR_XFER_STALLED) {
632
633 /*
634 * If we're reading and we stalled, then clear
635 * the bulk output pipe only the first time.
636 */
637
638 if (direction==DMA_FROM_DEVICE && i==0) {
639 if (usb_stor_clear_halt(us,
640 us->send_bulk_pipe) < 0)
641 return USB_STOR_TRANSPORT_ERROR;
642 }
643
644 /*
645 * Read status: is the device angry, or just busy?
646 */
647
648 result = usbat_read(us, USBAT_ATA,
649 direction==DMA_TO_DEVICE ?
650 USBAT_ATA_STATUS : USBAT_ATA_ALTSTATUS,
651 status);
652
653 if (result!=USB_STOR_XFER_GOOD)
654 return USB_STOR_TRANSPORT_ERROR;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100655 if (*status & 0x01) /* check condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return USB_STOR_TRANSPORT_FAILED;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100657 if (*status & 0x20) /* device fault */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return USB_STOR_TRANSPORT_FAILED;
659
Joe Perches191648d2013-04-19 11:44:00 -0700660 usb_stor_dbg(us, "Redoing %s\n",
661 direction == DMA_TO_DEVICE
662 ? "write" : "read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 } else if (result != USB_STOR_XFER_GOOD)
665 return USB_STOR_TRANSPORT_ERROR;
666 else
667 return usbat_wait_not_busy(us, minutes);
668
669 }
670
Joe Perches191648d2013-04-19 11:44:00 -0700671 usb_stor_dbg(us, "Bummer! %s bulk data 20 times failed\n",
672 direction == DMA_TO_DEVICE ? "Writing" : "Reading");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 return USB_STOR_TRANSPORT_FAILED;
675}
676
677/*
678 * Write to multiple registers:
679 * Allows us to write specific data to any registers. The data to be written
680 * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN
681 * which gets sent through bulk out.
682 * Not designed for large transfers of data!
683 */
684static int usbat_multiple_write(struct us_data *us,
685 unsigned char *registers,
686 unsigned char *data_out,
687 unsigned short num_registers)
688{
689 int i, result;
690 unsigned char *data = us->iobuf;
691 unsigned char *command = us->iobuf;
692
693 BUG_ON(num_registers > US_IOBUF_SIZE/2);
694
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100695 /* Write to multiple registers, ATA access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 command[0] = 0x40;
697 command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
698
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100699 /* No relevance */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 command[2] = 0;
701 command[3] = 0;
702 command[4] = 0;
703 command[5] = 0;
704
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100705 /* Number of bytes to be transferred (incl. addresses and data) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 command[6] = LSB_of(num_registers*2);
707 command[7] = MSB_of(num_registers*2);
708
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100709 /* The setup command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 result = usbat_execute_command(us, command, 8);
711 if (result != USB_STOR_XFER_GOOD)
712 return USB_STOR_TRANSPORT_ERROR;
713
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100714 /* Create the reg/data, reg/data sequence */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 for (i=0; i<num_registers; i++) {
716 data[i<<1] = registers[i];
717 data[1+(i<<1)] = data_out[i];
718 }
719
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100720 /* Send the data */
Peter Chubb141804d2006-05-02 18:30:12 +0100721 result = usbat_bulk_write(us, data, num_registers*2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (result != USB_STOR_XFER_GOOD)
723 return USB_STOR_TRANSPORT_ERROR;
724
725 if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
726 return usbat_wait_not_busy(us, 0);
727 else
728 return USB_STOR_TRANSPORT_GOOD;
729}
730
731/*
732 * Conditionally read blocks from device:
733 * Allows us to read blocks from a specific data register, based upon the
734 * condition that a status register can be successfully masked with a status
735 * qualifier. If this condition is not initially met, the read will wait
736 * up until a maximum amount of time has elapsed, as specified by timeout.
737 * The read will start when the condition is met, otherwise the command aborts.
738 *
739 * The qualifier defined here is not the value that is masked, it defines
740 * conditions for the write to take place. The actual masked qualifier (and
741 * other related details) are defined beforehand with _set_shuttle_features().
742 */
743static int usbat_read_blocks(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300744 void* buffer,
Peter Chubb141804d2006-05-02 18:30:12 +0100745 int len,
746 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 int result;
749 unsigned char *command = us->iobuf;
750
751 command[0] = 0xC0;
752 command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK;
753 command[2] = USBAT_ATA_DATA;
754 command[3] = USBAT_ATA_STATUS;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100755 command[4] = 0xFD; /* Timeout (ms); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 command[5] = USBAT_QUAL_FCQ;
757 command[6] = LSB_of(len);
758 command[7] = MSB_of(len);
759
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100760 /* Multiple block read setup command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 result = usbat_execute_command(us, command, 8);
762 if (result != USB_STOR_XFER_GOOD)
763 return USB_STOR_TRANSPORT_FAILED;
764
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100765 /* Read the blocks we just asked for */
Peter Chubb141804d2006-05-02 18:30:12 +0100766 result = usbat_bulk_read(us, buffer, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (result != USB_STOR_XFER_GOOD)
768 return USB_STOR_TRANSPORT_FAILED;
769
770 return USB_STOR_TRANSPORT_GOOD;
771}
772
773/*
774 * Conditionally write blocks to device:
775 * Allows us to write blocks to a specific data register, based upon the
776 * condition that a status register can be successfully masked with a status
777 * qualifier. If this condition is not initially met, the write will wait
778 * up until a maximum amount of time has elapsed, as specified by timeout.
779 * The read will start when the condition is met, otherwise the command aborts.
780 *
781 * The qualifier defined here is not the value that is masked, it defines
782 * conditions for the write to take place. The actual masked qualifier (and
783 * other related details) are defined beforehand with _set_shuttle_features().
784 */
785static int usbat_write_blocks(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300786 void* buffer,
Peter Chubb141804d2006-05-02 18:30:12 +0100787 int len,
788 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 int result;
791 unsigned char *command = us->iobuf;
792
793 command[0] = 0x40;
794 command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK;
795 command[2] = USBAT_ATA_DATA;
796 command[3] = USBAT_ATA_STATUS;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100797 command[4] = 0xFD; /* Timeout (ms) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 command[5] = USBAT_QUAL_FCQ;
799 command[6] = LSB_of(len);
800 command[7] = MSB_of(len);
801
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100802 /* Multiple block write setup command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 result = usbat_execute_command(us, command, 8);
804 if (result != USB_STOR_XFER_GOOD)
805 return USB_STOR_TRANSPORT_FAILED;
806
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100807 /* Write the data */
Peter Chubb141804d2006-05-02 18:30:12 +0100808 result = usbat_bulk_write(us, buffer, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (result != USB_STOR_XFER_GOOD)
810 return USB_STOR_TRANSPORT_FAILED;
811
812 return USB_STOR_TRANSPORT_GOOD;
813}
814
815/*
816 * Read the User IO register
817 */
818static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
819{
820 int result;
821
822 result = usb_stor_ctrl_transfer(us,
823 us->recv_ctrl_pipe,
824 USBAT_CMD_UIO,
825 0xC0,
826 0,
827 0,
828 data_flags,
829 USBAT_UIO_READ);
830
Joe Perches191648d2013-04-19 11:44:00 -0700831 usb_stor_dbg(us, "UIO register reads %02X\n", *data_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 return result;
834}
835
836/*
837 * Write to the User IO register
838 */
839static int usbat_write_user_io(struct us_data *us,
840 unsigned char enable_flags,
841 unsigned char data_flags)
842{
843 return usb_stor_ctrl_transfer(us,
844 us->send_ctrl_pipe,
845 USBAT_CMD_UIO,
846 0x40,
847 short_pack(enable_flags, data_flags),
848 0,
849 NULL,
850 USBAT_UIO_WRITE);
851}
852
853/*
854 * Reset the device
855 * Often needed on media change.
856 */
857static int usbat_device_reset(struct us_data *us)
858{
859 int rc;
860
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100861 /*
862 * Reset peripheral, enable peripheral control signals
863 * (bring reset signal up)
864 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 rc = usbat_write_user_io(us,
866 USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
867 USBAT_UIO_EPAD | USBAT_UIO_1);
868 if (rc != USB_STOR_XFER_GOOD)
869 return USB_STOR_TRANSPORT_ERROR;
870
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100871 /*
872 * Enable peripheral control signals
873 * (bring reset signal down)
874 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 rc = usbat_write_user_io(us,
876 USBAT_UIO_OE1 | USBAT_UIO_OE0,
877 USBAT_UIO_EPAD | USBAT_UIO_1);
878 if (rc != USB_STOR_XFER_GOOD)
879 return USB_STOR_TRANSPORT_ERROR;
880
881 return USB_STOR_TRANSPORT_GOOD;
882}
883
884/*
885 * Enable card detect
886 */
887static int usbat_device_enable_cdt(struct us_data *us)
888{
889 int rc;
890
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100891 /* Enable peripheral control signals and card detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 rc = usbat_write_user_io(us,
893 USBAT_UIO_ACKD | USBAT_UIO_OE1 | USBAT_UIO_OE0,
894 USBAT_UIO_EPAD | USBAT_UIO_1);
895 if (rc != USB_STOR_XFER_GOOD)
896 return USB_STOR_TRANSPORT_ERROR;
897
898 return USB_STOR_TRANSPORT_GOOD;
899}
900
901/*
902 * Determine if media is present.
903 */
Joe Perches191648d2013-04-19 11:44:00 -0700904static int usbat_flash_check_media_present(struct us_data *us,
905 unsigned char *uio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 if (*uio & USBAT_UIO_UI0) {
Joe Perches191648d2013-04-19 11:44:00 -0700908 usb_stor_dbg(us, "no media detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return USBAT_FLASH_MEDIA_NONE;
910 }
911
912 return USBAT_FLASH_MEDIA_CF;
913}
914
915/*
916 * Determine if media has changed since last operation
917 */
Joe Perches191648d2013-04-19 11:44:00 -0700918static int usbat_flash_check_media_changed(struct us_data *us,
919 unsigned char *uio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 if (*uio & USBAT_UIO_0) {
Joe Perches191648d2013-04-19 11:44:00 -0700922 usb_stor_dbg(us, "media change detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return USBAT_FLASH_MEDIA_CHANGED;
924 }
925
926 return USBAT_FLASH_MEDIA_SAME;
927}
928
929/*
930 * Check for media change / no media and handle the situation appropriately
931 */
932static int usbat_flash_check_media(struct us_data *us,
933 struct usbat_info *info)
934{
935 int rc;
936 unsigned char *uio = us->iobuf;
937
938 rc = usbat_read_user_io(us, uio);
939 if (rc != USB_STOR_XFER_GOOD)
940 return USB_STOR_TRANSPORT_ERROR;
941
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100942 /* Check for media existence */
Joe Perches191648d2013-04-19 11:44:00 -0700943 rc = usbat_flash_check_media_present(us, uio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 if (rc == USBAT_FLASH_MEDIA_NONE) {
945 info->sense_key = 0x02;
946 info->sense_asc = 0x3A;
947 info->sense_ascq = 0x00;
948 return USB_STOR_TRANSPORT_FAILED;
949 }
950
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100951 /* Check for media change */
Joe Perches191648d2013-04-19 11:44:00 -0700952 rc = usbat_flash_check_media_changed(us, uio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (rc == USBAT_FLASH_MEDIA_CHANGED) {
954
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100955 /* Reset and re-enable card detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 rc = usbat_device_reset(us);
957 if (rc != USB_STOR_TRANSPORT_GOOD)
958 return rc;
959 rc = usbat_device_enable_cdt(us);
960 if (rc != USB_STOR_TRANSPORT_GOOD)
961 return rc;
962
963 msleep(50);
964
965 rc = usbat_read_user_io(us, uio);
966 if (rc != USB_STOR_XFER_GOOD)
967 return USB_STOR_TRANSPORT_ERROR;
968
969 info->sense_key = UNIT_ATTENTION;
970 info->sense_asc = 0x28;
971 info->sense_ascq = 0x00;
972 return USB_STOR_TRANSPORT_FAILED;
973 }
974
975 return USB_STOR_TRANSPORT_GOOD;
976}
977
978/*
979 * Determine whether we are controlling a flash-based reader/writer,
980 * or a HP8200-based CD drive.
981 * Sets transport functions as appropriate.
982 */
983static int usbat_identify_device(struct us_data *us,
984 struct usbat_info *info)
985{
986 int rc;
987 unsigned char status;
988
989 if (!us || !info)
990 return USB_STOR_TRANSPORT_ERROR;
991
992 rc = usbat_device_reset(us);
993 if (rc != USB_STOR_TRANSPORT_GOOD)
994 return rc;
Daniel Drake8845add2005-11-17 09:48:01 -0800995 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 /*
Daniel Drake68a64572005-08-10 18:30:04 +0100998 * In attempt to distinguish between HP CDRW's and Flash readers, we now
999 * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
1000 * readers), this command should fail with error. On ATAPI devices (i.e.
1001 * CDROM drives), it should succeed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 */
Daniel Drake68a64572005-08-10 18:30:04 +01001003 rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1);
1004 if (rc != USB_STOR_XFER_GOOD)
1005 return USB_STOR_TRANSPORT_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Daniel Drake68a64572005-08-10 18:30:04 +01001007 rc = usbat_get_status(us, &status);
1008 if (rc != USB_STOR_XFER_GOOD)
1009 return USB_STOR_TRANSPORT_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001011 /* Check for error bit, or if the command 'fell through' */
Daniel Drakea8798532005-09-29 00:14:21 +01001012 if (status == 0xA1 || !(status & 0x01)) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001013 /* Device is HP 8200 */
Joe Perches191648d2013-04-19 11:44:00 -07001014 usb_stor_dbg(us, "Detected HP8200 CDRW\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 info->devicetype = USBAT_DEV_HP8200;
Daniel Drakea8798532005-09-29 00:14:21 +01001016 } else {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001017 /* Device is a CompactFlash reader/writer */
Joe Perches191648d2013-04-19 11:44:00 -07001018 usb_stor_dbg(us, "Detected Flash reader/writer\n");
Daniel Drakea8798532005-09-29 00:14:21 +01001019 info->devicetype = USBAT_DEV_FLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021
1022 return USB_STOR_TRANSPORT_GOOD;
1023}
1024
1025/*
1026 * Set the transport function based on the device type
1027 */
1028static int usbat_set_transport(struct us_data *us,
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001029 struct usbat_info *info,
1030 int devicetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001033 if (!info->devicetype)
1034 info->devicetype = devicetype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001036 if (!info->devicetype)
1037 usbat_identify_device(us, info);
1038
1039 switch (info->devicetype) {
1040 default:
1041 return USB_STOR_TRANSPORT_ERROR;
1042
1043 case USBAT_DEV_HP8200:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 us->transport = usbat_hp8200e_transport;
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001045 break;
1046
1047 case USBAT_DEV_FLASH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 us->transport = usbat_flash_transport;
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001049 break;
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 return 0;
1053}
1054
1055/*
1056 * Read the media capacity
1057 */
1058static int usbat_flash_get_sector_count(struct us_data *us,
1059 struct usbat_info *info)
1060{
1061 unsigned char registers[3] = {
1062 USBAT_ATA_SECCNT,
1063 USBAT_ATA_DEVICE,
1064 USBAT_ATA_CMD,
1065 };
1066 unsigned char command[3] = { 0x01, 0xA0, 0xEC };
1067 unsigned char *reply;
1068 unsigned char status;
1069 int rc;
1070
1071 if (!us || !info)
1072 return USB_STOR_TRANSPORT_ERROR;
1073
1074 reply = kmalloc(512, GFP_NOIO);
1075 if (!reply)
1076 return USB_STOR_TRANSPORT_ERROR;
1077
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001078 /* ATA command : IDENTIFY DEVICE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 rc = usbat_multiple_write(us, registers, command, 3);
1080 if (rc != USB_STOR_XFER_GOOD) {
Joe Perches191648d2013-04-19 11:44:00 -07001081 usb_stor_dbg(us, "Gah! identify_device failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 rc = USB_STOR_TRANSPORT_ERROR;
1083 goto leave;
1084 }
1085
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001086 /* Read device status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
1088 rc = USB_STOR_TRANSPORT_ERROR;
1089 goto leave;
1090 }
1091
1092 msleep(100);
1093
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001094 /* Read the device identification data */
Peter Chubb141804d2006-05-02 18:30:12 +01001095 rc = usbat_read_block(us, reply, 512, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (rc != USB_STOR_TRANSPORT_GOOD)
1097 goto leave;
1098
1099 info->sectors = ((u32)(reply[117]) << 24) |
1100 ((u32)(reply[116]) << 16) |
1101 ((u32)(reply[115]) << 8) |
1102 ((u32)(reply[114]) );
1103
1104 rc = USB_STOR_TRANSPORT_GOOD;
1105
1106 leave:
1107 kfree(reply);
1108 return rc;
1109}
1110
1111/*
1112 * Read data from device
1113 */
1114static int usbat_flash_read_data(struct us_data *us,
1115 struct usbat_info *info,
1116 u32 sector,
1117 u32 sectors)
1118{
1119 unsigned char registers[7] = {
1120 USBAT_ATA_FEATURES,
1121 USBAT_ATA_SECCNT,
1122 USBAT_ATA_SECNUM,
1123 USBAT_ATA_LBA_ME,
1124 USBAT_ATA_LBA_HI,
1125 USBAT_ATA_DEVICE,
1126 USBAT_ATA_STATUS,
1127 };
1128 unsigned char command[7];
1129 unsigned char *buffer;
1130 unsigned char thistime;
1131 unsigned int totallen, alloclen;
1132 int len, result;
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001133 unsigned int sg_offset = 0;
1134 struct scatterlist *sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 result = usbat_flash_check_media(us, info);
1137 if (result != USB_STOR_TRANSPORT_GOOD)
1138 return result;
1139
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001140 /*
1141 * we're working in LBA mode. according to the ATA spec,
1142 * we can support up to 28-bit addressing. I don't know if Jumpshot
1143 * supports beyond 24-bit addressing. It's kind of hard to test
1144 * since it requires > 8GB CF card.
1145 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 if (sector > 0x0FFFFFFF)
1148 return USB_STOR_TRANSPORT_ERROR;
1149
1150 totallen = sectors * info->ssize;
1151
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001152 /*
1153 * Since we don't read more than 64 KB at a time, we have to create
1154 * a bounce buffer and move the data a piece at a time between the
1155 * bounce buffer and the actual transfer buffer.
1156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 alloclen = min(totallen, 65536u);
1159 buffer = kmalloc(alloclen, GFP_NOIO);
1160 if (buffer == NULL)
1161 return USB_STOR_TRANSPORT_ERROR;
1162
1163 do {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001164 /*
1165 * loop, never allocate or transfer more than 64k at once
1166 * (min(128k, 255*info->ssize) is the real limit)
1167 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 len = min(totallen, alloclen);
1169 thistime = (len / info->ssize) & 0xff;
1170
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001171 /* ATA command 0x20 (READ SECTORS) */
1172 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001174 /* Write/execute ATA read command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 result = usbat_multiple_write(us, registers, command, 7);
1176 if (result != USB_STOR_TRANSPORT_GOOD)
1177 goto leave;
1178
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001179 /* Read the data we just requested */
Peter Chubb141804d2006-05-02 18:30:12 +01001180 result = usbat_read_blocks(us, buffer, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (result != USB_STOR_TRANSPORT_GOOD)
1182 goto leave;
1183
Joe Perches191648d2013-04-19 11:44:00 -07001184 usb_stor_dbg(us, "%d bytes\n", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001186 /* Store the data in the transfer buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 usb_stor_access_xfer_buf(buffer, len, us->srb,
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001188 &sg, &sg_offset, TO_XFER_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 sector += thistime;
1191 totallen -= len;
1192 } while (totallen > 0);
1193
1194 kfree(buffer);
1195 return USB_STOR_TRANSPORT_GOOD;
1196
1197leave:
1198 kfree(buffer);
1199 return USB_STOR_TRANSPORT_ERROR;
1200}
1201
1202/*
1203 * Write data to device
1204 */
1205static int usbat_flash_write_data(struct us_data *us,
1206 struct usbat_info *info,
1207 u32 sector,
1208 u32 sectors)
1209{
1210 unsigned char registers[7] = {
1211 USBAT_ATA_FEATURES,
1212 USBAT_ATA_SECCNT,
1213 USBAT_ATA_SECNUM,
1214 USBAT_ATA_LBA_ME,
1215 USBAT_ATA_LBA_HI,
1216 USBAT_ATA_DEVICE,
1217 USBAT_ATA_STATUS,
1218 };
1219 unsigned char command[7];
1220 unsigned char *buffer;
1221 unsigned char thistime;
1222 unsigned int totallen, alloclen;
1223 int len, result;
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001224 unsigned int sg_offset = 0;
1225 struct scatterlist *sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 result = usbat_flash_check_media(us, info);
1228 if (result != USB_STOR_TRANSPORT_GOOD)
1229 return result;
1230
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001231 /*
1232 * we're working in LBA mode. according to the ATA spec,
1233 * we can support up to 28-bit addressing. I don't know if the device
1234 * supports beyond 24-bit addressing. It's kind of hard to test
1235 * since it requires > 8GB media.
1236 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 if (sector > 0x0FFFFFFF)
1239 return USB_STOR_TRANSPORT_ERROR;
1240
1241 totallen = sectors * info->ssize;
1242
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001243 /*
1244 * Since we don't write more than 64 KB at a time, we have to create
1245 * a bounce buffer and move the data a piece at a time between the
1246 * bounce buffer and the actual transfer buffer.
1247 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 alloclen = min(totallen, 65536u);
1250 buffer = kmalloc(alloclen, GFP_NOIO);
1251 if (buffer == NULL)
1252 return USB_STOR_TRANSPORT_ERROR;
1253
1254 do {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001255 /*
1256 * loop, never allocate or transfer more than 64k at once
1257 * (min(128k, 255*info->ssize) is the real limit)
1258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 len = min(totallen, alloclen);
1260 thistime = (len / info->ssize) & 0xff;
1261
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001262 /* Get the data from the transfer buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 usb_stor_access_xfer_buf(buffer, len, us->srb,
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001264 &sg, &sg_offset, FROM_XFER_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001266 /* ATA command 0x30 (WRITE SECTORS) */
1267 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001269 /* Write/execute ATA write command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 result = usbat_multiple_write(us, registers, command, 7);
1271 if (result != USB_STOR_TRANSPORT_GOOD)
1272 goto leave;
1273
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001274 /* Write the data */
Peter Chubb141804d2006-05-02 18:30:12 +01001275 result = usbat_write_blocks(us, buffer, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (result != USB_STOR_TRANSPORT_GOOD)
1277 goto leave;
1278
1279 sector += thistime;
1280 totallen -= len;
1281 } while (totallen > 0);
1282
1283 kfree(buffer);
1284 return result;
1285
1286leave:
1287 kfree(buffer);
1288 return USB_STOR_TRANSPORT_ERROR;
1289}
1290
1291/*
1292 * Squeeze a potentially huge (> 65535 byte) read10 command into
1293 * a little ( <= 65535 byte) ATAPI pipe
1294 */
1295static int usbat_hp8200e_handle_read10(struct us_data *us,
1296 unsigned char *registers,
1297 unsigned char *data,
1298 struct scsi_cmnd *srb)
1299{
1300 int result = USB_STOR_TRANSPORT_GOOD;
1301 unsigned char *buffer;
1302 unsigned int len;
1303 unsigned int sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 unsigned int sg_offset = 0;
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001305 struct scatterlist *sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Joe Perches191648d2013-04-19 11:44:00 -07001307 usb_stor_dbg(us, "transfersize %d\n", srb->transfersize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Boaz Harrosh4776e992007-09-09 20:40:56 +03001309 if (scsi_bufflen(srb) < 0x10000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1312 registers, data, 19,
1313 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1314 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1315 DMA_FROM_DEVICE,
Boaz Harrosh4776e992007-09-09 20:40:56 +03001316 scsi_sglist(srb),
1317 scsi_bufflen(srb), scsi_sg_count(srb), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 return result;
1320 }
1321
1322 /*
1323 * Since we're requesting more data than we can handle in
1324 * a single read command (max is 64k-1), we will perform
1325 * multiple reads, but each read must be in multiples of
1326 * a sector. Luckily the sector size is in srb->transfersize
1327 * (see linux/drivers/scsi/sr.c).
1328 */
1329
1330 if (data[7+0] == GPCMD_READ_CD) {
1331 len = short_pack(data[7+9], data[7+8]);
1332 len <<= 16;
1333 len |= data[7+7];
Joe Perches191648d2013-04-19 11:44:00 -07001334 usb_stor_dbg(us, "GPCMD_READ_CD: len %d\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +03001335 srb->transfersize = scsi_bufflen(srb)/len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337
1338 if (!srb->transfersize) {
1339 srb->transfersize = 2048; /* A guess */
Joe Perches191648d2013-04-19 11:44:00 -07001340 usb_stor_dbg(us, "transfersize 0, forcing %d\n",
1341 srb->transfersize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001344 /*
1345 * Since we only read in one block at a time, we have to create
1346 * a bounce buffer and move the data a piece at a time between the
1347 * bounce buffer and the actual transfer buffer.
1348 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 len = (65535/srb->transfersize) * srb->transfersize;
Joe Perches191648d2013-04-19 11:44:00 -07001351 usb_stor_dbg(us, "Max read is %d bytes\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +03001352 len = min(len, scsi_bufflen(srb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 buffer = kmalloc(len, GFP_NOIO);
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001354 if (buffer == NULL) /* bloody hell! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return USB_STOR_TRANSPORT_FAILED;
1356 sector = short_pack(data[7+3], data[7+2]);
1357 sector <<= 16;
1358 sector |= short_pack(data[7+5], data[7+4]);
1359 transferred = 0;
1360
Boaz Harrosh4776e992007-09-09 20:40:56 +03001361 while (transferred != scsi_bufflen(srb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Boaz Harrosh4776e992007-09-09 20:40:56 +03001363 if (len > scsi_bufflen(srb) - transferred)
1364 len = scsi_bufflen(srb) - transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001366 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1367 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001369 /* Fix up the SCSI command sector and num sectors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001371 data[7+2] = MSB_of(sector>>16); /* SCSI command sector */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 data[7+3] = LSB_of(sector>>16);
1373 data[7+4] = MSB_of(sector&0xFFFF);
1374 data[7+5] = LSB_of(sector&0xFFFF);
1375 if (data[7+0] == GPCMD_READ_CD)
1376 data[7+6] = 0;
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001377 data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */
1378 data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1381 registers, data, 19,
1382 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1383 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1384 DMA_FROM_DEVICE,
1385 buffer,
1386 len, 0, 1);
1387
1388 if (result != USB_STOR_TRANSPORT_GOOD)
1389 break;
1390
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001391 /* Store the data in the transfer buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 usb_stor_access_xfer_buf(buffer, len, srb,
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001393 &sg, &sg_offset, TO_XFER_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001395 /* Update the amount transferred and the sector number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 transferred += len;
1398 sector += len / srb->transfersize;
1399
Boaz Harrosh4776e992007-09-09 20:40:56 +03001400 } /* while transferred != scsi_bufflen(srb) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 kfree(buffer);
1403 return result;
1404}
1405
1406static int usbat_select_and_test_registers(struct us_data *us)
1407{
1408 int selector;
1409 unsigned char *status = us->iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001411 /* try device = master, then device = slave. */
Daniel Drake68a64572005-08-10 18:30:04 +01001412 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
1413 if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 USB_STOR_XFER_GOOD)
1415 return USB_STOR_TRANSPORT_ERROR;
1416
1417 if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) !=
1418 USB_STOR_XFER_GOOD)
1419 return USB_STOR_TRANSPORT_ERROR;
1420
1421 if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) !=
1422 USB_STOR_XFER_GOOD)
1423 return USB_STOR_TRANSPORT_ERROR;
1424
1425 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1426 USB_STOR_XFER_GOOD)
1427 return USB_STOR_TRANSPORT_ERROR;
1428
1429 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1430 USB_STOR_XFER_GOOD)
1431 return USB_STOR_TRANSPORT_ERROR;
1432
1433 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) !=
1434 USB_STOR_XFER_GOOD)
1435 return USB_STOR_TRANSPORT_ERROR;
1436
1437 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) !=
1438 USB_STOR_XFER_GOOD)
1439 return USB_STOR_TRANSPORT_ERROR;
1440
1441 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1442 USB_STOR_XFER_GOOD)
1443 return USB_STOR_TRANSPORT_ERROR;
1444
1445 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1446 USB_STOR_XFER_GOOD)
1447 return USB_STOR_TRANSPORT_ERROR;
1448 }
1449
1450 return USB_STOR_TRANSPORT_GOOD;
1451}
1452
1453/*
1454 * Initialize the USBAT processor and the storage device
1455 */
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001456static int init_usbat(struct us_data *us, int devicetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 int rc;
1459 struct usbat_info *info;
1460 unsigned char subcountH = USBAT_ATA_LBA_HI;
1461 unsigned char subcountL = USBAT_ATA_LBA_ME;
1462 unsigned char *status = us->iobuf;
1463
Oliver Neukum887c2562006-01-08 12:33:45 +01001464 us->extra = kzalloc(sizeof(struct usbat_info), GFP_NOIO);
Joe Perches191648d2013-04-19 11:44:00 -07001465 if (!us->extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return 1;
Joe Perches191648d2013-04-19 11:44:00 -07001467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 info = (struct usbat_info *) (us->extra);
1469
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001470 /* Enable peripheral control signals */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 rc = usbat_write_user_io(us,
1472 USBAT_UIO_OE1 | USBAT_UIO_OE0,
1473 USBAT_UIO_EPAD | USBAT_UIO_1);
1474 if (rc != USB_STOR_XFER_GOOD)
1475 return USB_STOR_TRANSPORT_ERROR;
1476
Joe Perches191648d2013-04-19 11:44:00 -07001477 usb_stor_dbg(us, "INIT 1\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 msleep(2000);
1480
1481 rc = usbat_read_user_io(us, status);
1482 if (rc != USB_STOR_TRANSPORT_GOOD)
1483 return rc;
1484
Joe Perches191648d2013-04-19 11:44:00 -07001485 usb_stor_dbg(us, "INIT 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 rc = usbat_read_user_io(us, status);
1488 if (rc != USB_STOR_XFER_GOOD)
1489 return USB_STOR_TRANSPORT_ERROR;
1490
1491 rc = usbat_read_user_io(us, status);
1492 if (rc != USB_STOR_XFER_GOOD)
1493 return USB_STOR_TRANSPORT_ERROR;
1494
Joe Perches191648d2013-04-19 11:44:00 -07001495 usb_stor_dbg(us, "INIT 3\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 rc = usbat_select_and_test_registers(us);
1498 if (rc != USB_STOR_TRANSPORT_GOOD)
1499 return rc;
1500
Joe Perches191648d2013-04-19 11:44:00 -07001501 usb_stor_dbg(us, "INIT 4\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 rc = usbat_read_user_io(us, status);
1504 if (rc != USB_STOR_XFER_GOOD)
1505 return USB_STOR_TRANSPORT_ERROR;
1506
Joe Perches191648d2013-04-19 11:44:00 -07001507 usb_stor_dbg(us, "INIT 5\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001509 /* Enable peripheral control signals and card detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 rc = usbat_device_enable_cdt(us);
1511 if (rc != USB_STOR_TRANSPORT_GOOD)
1512 return rc;
1513
Joe Perches191648d2013-04-19 11:44:00 -07001514 usb_stor_dbg(us, "INIT 6\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 rc = usbat_read_user_io(us, status);
1517 if (rc != USB_STOR_XFER_GOOD)
1518 return USB_STOR_TRANSPORT_ERROR;
1519
Joe Perches191648d2013-04-19 11:44:00 -07001520 usb_stor_dbg(us, "INIT 7\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 msleep(1400);
1523
1524 rc = usbat_read_user_io(us, status);
1525 if (rc != USB_STOR_XFER_GOOD)
1526 return USB_STOR_TRANSPORT_ERROR;
1527
Joe Perches191648d2013-04-19 11:44:00 -07001528 usb_stor_dbg(us, "INIT 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 rc = usbat_select_and_test_registers(us);
1531 if (rc != USB_STOR_TRANSPORT_GOOD)
1532 return rc;
1533
Joe Perches191648d2013-04-19 11:44:00 -07001534 usb_stor_dbg(us, "INIT 9\n");
Daniel Drake68a64572005-08-10 18:30:04 +01001535
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001536 /* At this point, we need to detect which device we are using */
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001537 if (usbat_set_transport(us, info, devicetype))
Daniel Drake68a64572005-08-10 18:30:04 +01001538 return USB_STOR_TRANSPORT_ERROR;
1539
Joe Perches191648d2013-04-19 11:44:00 -07001540 usb_stor_dbg(us, "INIT 10\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 if (usbat_get_device_type(us) == USBAT_DEV_FLASH) {
1543 subcountH = 0x02;
1544 subcountL = 0x00;
1545 }
1546 rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1547 0x00, 0x88, 0x08, subcountH, subcountL);
1548 if (rc != USB_STOR_XFER_GOOD)
1549 return USB_STOR_TRANSPORT_ERROR;
1550
Joe Perches191648d2013-04-19 11:44:00 -07001551 usb_stor_dbg(us, "INIT 11\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 return USB_STOR_TRANSPORT_GOOD;
1554}
1555
1556/*
1557 * Transport for the HP 8200e
1558 */
1559static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1560{
1561 int result;
1562 unsigned char *status = us->iobuf;
1563 unsigned char registers[32];
1564 unsigned char data[32];
1565 unsigned int len;
1566 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Boaz Harrosh4776e992007-09-09 20:40:56 +03001568 len = scsi_bufflen(srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 /* Send A0 (ATA PACKET COMMAND).
1571 Note: I guess we're never going to get any of the ATA
1572 commands... just ATA Packet Commands.
1573 */
1574
1575 registers[0] = USBAT_ATA_FEATURES;
1576 registers[1] = USBAT_ATA_SECCNT;
1577 registers[2] = USBAT_ATA_SECNUM;
1578 registers[3] = USBAT_ATA_LBA_ME;
1579 registers[4] = USBAT_ATA_LBA_HI;
1580 registers[5] = USBAT_ATA_DEVICE;
1581 registers[6] = USBAT_ATA_CMD;
1582 data[0] = 0x00;
1583 data[1] = 0x00;
1584 data[2] = 0x00;
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001585 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1586 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
1587 data[5] = 0xB0; /* (device sel) = slave */
1588 data[6] = 0xA0; /* (command) = ATA PACKET COMMAND */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 for (i=7; i<19; i++) {
1591 registers[i] = 0x10;
1592 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1593 }
1594
1595 result = usbat_get_status(us, status);
Joe Perches191648d2013-04-19 11:44:00 -07001596 usb_stor_dbg(us, "Status = %02X\n", *status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (result != USB_STOR_XFER_GOOD)
1598 return USB_STOR_TRANSPORT_ERROR;
1599 if (srb->cmnd[0] == TEST_UNIT_READY)
1600 transferred = 0;
1601
1602 if (srb->sc_data_direction == DMA_TO_DEVICE) {
1603
1604 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1605 registers, data, 19,
1606 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1607 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1608 DMA_TO_DEVICE,
Boaz Harrosh4776e992007-09-09 20:40:56 +03001609 scsi_sglist(srb),
1610 len, scsi_sg_count(srb), 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 if (result == USB_STOR_TRANSPORT_GOOD) {
1613 transferred += len;
Joe Perches191648d2013-04-19 11:44:00 -07001614 usb_stor_dbg(us, "Wrote %08X bytes\n", transferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
1616
1617 return result;
1618
1619 } else if (srb->cmnd[0] == READ_10 ||
1620 srb->cmnd[0] == GPCMD_READ_CD) {
1621
1622 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1623
1624 }
1625
1626 if (len > 0xFFFF) {
Joe Perches191648d2013-04-19 11:44:00 -07001627 usb_stor_dbg(us, "Error: len = %08X... what do I do now?\n",
1628 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return USB_STOR_TRANSPORT_ERROR;
1630 }
1631
Roel Kluin5d3034a2010-02-17 11:50:26 +01001632 result = usbat_multiple_write(us, registers, data, 7);
1633
1634 if (result != USB_STOR_TRANSPORT_GOOD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001637 /*
1638 * Write the 12-byte command header.
1639 *
1640 * If the command is BLANK then set the timer for 75 minutes.
1641 * Otherwise set it for 10 minutes.
1642 *
1643 * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1644 * AT SPEED 4 IS UNRELIABLE!!!
1645 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Roel Kluin5d3034a2010-02-17 11:50:26 +01001647 result = usbat_write_block(us, USBAT_ATA, srb->cmnd, 12,
1648 srb->cmnd[0] == GPCMD_BLANK ? 75 : 10, 0);
1649
1650 if (result != USB_STOR_TRANSPORT_GOOD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001653 /* If there is response data to be read in then do it here. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1656
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001657 /* How many bytes to read in? Check cylL register */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1660 USB_STOR_XFER_GOOD) {
1661 return USB_STOR_TRANSPORT_ERROR;
1662 }
1663
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001664 if (len > 0xFF) { /* need to read cylH also */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 len = *status;
1666 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1667 USB_STOR_XFER_GOOD) {
1668 return USB_STOR_TRANSPORT_ERROR;
1669 }
1670 len += ((unsigned int) *status)<<8;
1671 }
1672 else
1673 len = *status;
1674
1675
Boaz Harrosh4776e992007-09-09 20:40:56 +03001676 result = usbat_read_block(us, scsi_sglist(srb), len,
1677 scsi_sg_count(srb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
1679
1680 return result;
1681}
1682
1683/*
1684 * Transport for USBAT02-based CompactFlash and similar storage devices
1685 */
1686static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1687{
1688 int rc;
1689 struct usbat_info *info = (struct usbat_info *) (us->extra);
1690 unsigned long block, blocks;
1691 unsigned char *ptr = us->iobuf;
1692 static unsigned char inquiry_response[36] = {
1693 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1694 };
1695
1696 if (srb->cmnd[0] == INQUIRY) {
Joe Perches191648d2013-04-19 11:44:00 -07001697 usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1699 fill_inquiry_response(us, ptr, 36);
1700 return USB_STOR_TRANSPORT_GOOD;
1701 }
1702
1703 if (srb->cmnd[0] == READ_CAPACITY) {
1704 rc = usbat_flash_check_media(us, info);
1705 if (rc != USB_STOR_TRANSPORT_GOOD)
1706 return rc;
1707
1708 rc = usbat_flash_get_sector_count(us, info);
1709 if (rc != USB_STOR_TRANSPORT_GOOD)
1710 return rc;
1711
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001712 /* hard coded 512 byte sectors as per ATA spec */
1713 info->ssize = 0x200;
Joe Perches191648d2013-04-19 11:44:00 -07001714 usb_stor_dbg(us, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1715 info->sectors, info->ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001717 /*
1718 * build the reply
1719 * note: must return the sector number of the last sector,
1720 * *not* the total number of sectors
1721 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1723 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1724 usb_stor_set_xfer_buf(ptr, 8, srb);
1725
1726 return USB_STOR_TRANSPORT_GOOD;
1727 }
1728
1729 if (srb->cmnd[0] == MODE_SELECT_10) {
Joe Perches191648d2013-04-19 11:44:00 -07001730 usb_stor_dbg(us, "Gah! MODE_SELECT_10\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return USB_STOR_TRANSPORT_ERROR;
1732 }
1733
1734 if (srb->cmnd[0] == READ_10) {
1735 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1736 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1737
1738 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1739
Joe Perches191648d2013-04-19 11:44:00 -07001740 usb_stor_dbg(us, "READ_10: read block 0x%04lx count %ld\n",
1741 block, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return usbat_flash_read_data(us, info, block, blocks);
1743 }
1744
1745 if (srb->cmnd[0] == READ_12) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001746 /*
1747 * I don't think we'll ever see a READ_12 but support it anyway
1748 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1750 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1751
1752 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1753 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1754
Joe Perches191648d2013-04-19 11:44:00 -07001755 usb_stor_dbg(us, "READ_12: read block 0x%04lx count %ld\n",
1756 block, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return usbat_flash_read_data(us, info, block, blocks);
1758 }
1759
1760 if (srb->cmnd[0] == WRITE_10) {
1761 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1762 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1763
1764 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1765
Joe Perches191648d2013-04-19 11:44:00 -07001766 usb_stor_dbg(us, "WRITE_10: write block 0x%04lx count %ld\n",
1767 block, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return usbat_flash_write_data(us, info, block, blocks);
1769 }
1770
1771 if (srb->cmnd[0] == WRITE_12) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001772 /*
1773 * I don't think we'll ever see a WRITE_12 but support it anyway
1774 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1776 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1777
1778 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1779 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1780
Joe Perches191648d2013-04-19 11:44:00 -07001781 usb_stor_dbg(us, "WRITE_12: write block 0x%04lx count %ld\n",
1782 block, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return usbat_flash_write_data(us, info, block, blocks);
1784 }
1785
1786
1787 if (srb->cmnd[0] == TEST_UNIT_READY) {
Joe Perches191648d2013-04-19 11:44:00 -07001788 usb_stor_dbg(us, "TEST_UNIT_READY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 rc = usbat_flash_check_media(us, info);
1791 if (rc != USB_STOR_TRANSPORT_GOOD)
1792 return rc;
1793
1794 return usbat_check_status(us);
1795 }
1796
1797 if (srb->cmnd[0] == REQUEST_SENSE) {
Joe Perches191648d2013-04-19 11:44:00 -07001798 usb_stor_dbg(us, "REQUEST_SENSE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 memset(ptr, 0, 18);
1801 ptr[0] = 0xF0;
1802 ptr[2] = info->sense_key;
1803 ptr[7] = 11;
1804 ptr[12] = info->sense_asc;
1805 ptr[13] = info->sense_ascq;
1806 usb_stor_set_xfer_buf(ptr, 18, srb);
1807
1808 return USB_STOR_TRANSPORT_GOOD;
1809 }
1810
1811 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001812 /*
1813 * sure. whatever. not like we can stop the user from popping
1814 * the media out of the device (no locking doors, etc)
1815 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return USB_STOR_TRANSPORT_GOOD;
1817 }
1818
Joe Perches191648d2013-04-19 11:44:00 -07001819 usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
1820 srb->cmnd[0], srb->cmnd[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 info->sense_key = 0x05;
1822 info->sense_asc = 0x20;
1823 info->sense_ascq = 0x00;
1824 return USB_STOR_TRANSPORT_FAILED;
1825}
1826
Alan Stern26d68182009-02-12 14:48:08 -05001827static int init_usbat_cd(struct us_data *us)
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001828{
1829 return init_usbat(us, USBAT_DEV_HP8200);
1830}
1831
Alan Stern26d68182009-02-12 14:48:08 -05001832static int init_usbat_flash(struct us_data *us)
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001833{
1834 return init_usbat(us, USBAT_DEV_FLASH);
1835}
1836
Alan Stern26d68182009-02-12 14:48:08 -05001837static int usbat_probe(struct usb_interface *intf,
1838 const struct usb_device_id *id)
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001839{
Alan Stern26d68182009-02-12 14:48:08 -05001840 struct us_data *us;
1841 int result;
1842
1843 result = usb_stor_probe1(&us, intf, id,
1844 (id - usbat_usb_ids) + usbat_unusual_dev_list);
1845 if (result)
1846 return result;
1847
1848 /* The actual transport will be determined later by the
1849 * initialization routine; this is just a placeholder.
1850 */
1851 us->transport_name = "Shuttle USBAT";
1852 us->transport = usbat_flash_transport;
1853 us->transport_reset = usb_stor_CB_reset;
Daniele Forsidf602c22014-04-29 11:44:03 +02001854 us->max_lun = 0;
Alan Stern26d68182009-02-12 14:48:08 -05001855
1856 result = usb_stor_probe2(us);
1857 return result;
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001858}
1859
Alan Stern26d68182009-02-12 14:48:08 -05001860static struct usb_driver usbat_driver = {
1861 .name = "ums-usbat",
1862 .probe = usbat_probe,
1863 .disconnect = usb_stor_disconnect,
1864 .suspend = usb_stor_suspend,
1865 .resume = usb_stor_resume,
1866 .reset_resume = usb_stor_reset_resume,
1867 .pre_reset = usb_stor_pre_reset,
1868 .post_reset = usb_stor_post_reset,
1869 .id_table = usbat_usb_ids,
1870 .soft_unbind = 1,
Huajun Lie73b2db2012-01-14 10:15:21 +08001871 .no_dynamic_id = 1,
Alan Stern26d68182009-02-12 14:48:08 -05001872};
1873
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001874module_usb_driver(usbat_driver);