blob: 3b0294e4df93d935de3b9e82e6697aa64bf2d8a2 [file] [log] [blame]
Felipe Balbif0183a32016-04-18 13:09:11 +03001/*
2 * Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Current development and maintenance by:
5 * (c) 2000, 2001 Robert Baruch (autophile@starband.net)
6 * (c) 2004, 2005 Daniel Drake <dsd@gentoo.org>
7 *
8 * Developed with the assistance of:
9 * (c) 2002 Alan Stern <stern@rowland.org>
10 *
11 * Flash support based on earlier work by:
12 * (c) 2002 Thomas Kreiling <usbdev@sm04.de>
13 *
14 * Many originally ATAPI devices were slightly modified to meet the USB
15 * market by using some kind of translation from ATAPI to USB on the host,
16 * and the peripheral would translate from USB back to ATAPI.
17 *
18 * SCM Microsystems (www.scmmicro.com) makes a device, sold to OEM's only,
19 * which does the USB-to-ATAPI conversion. By obtaining the data sheet on
20 * their device under nondisclosure agreement, I have been able to write
21 * this driver for Linux.
22 *
23 * The chip used in the device can also be used for EPP and ISA translation
24 * as well. This driver is only guaranteed to work with the ATAPI
25 * translation.
26 *
27 * See the Kconfig help text for a list of devices known to be supported by
28 * this driver.
29 *
30 * This program is free software; you can redistribute it and/or modify it
31 * under the terms of the GNU General Public License as published by the
32 * Free Software Foundation; either version 2, or (at your option) any
33 * later version.
34 *
35 * This program is distributed in the hope that it will be useful, but
36 * WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
38 * General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 675 Mass Ave, Cambridge, MA 02139, USA.
43 */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/errno.h>
Alan Stern26d68182009-02-12 14:48:08 -050046#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/slab.h>
48#include <linux/cdrom.h>
49
50#include <scsi/scsi.h>
51#include <scsi/scsi_cmnd.h>
52
53#include "usb.h"
54#include "transport.h"
55#include "protocol.h"
56#include "debug.h"
Akinobu Mitaaa519be2015-05-06 18:24:21 +090057#include "scsiglue.h"
58
59#define DRV_NAME "ums-usbat"
Alan Stern26d68182009-02-12 14:48:08 -050060
Maciej Grela4246b062009-02-28 12:39:20 -080061MODULE_DESCRIPTION("Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable");
62MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>, Robert Baruch <autophile@starband.net>");
63MODULE_LICENSE("GPL");
Alan Stern26d68182009-02-12 14:48:08 -050064
65/* Supported device types */
66#define USBAT_DEV_HP8200 0x01
67#define USBAT_DEV_FLASH 0x02
68
69#define USBAT_EPP_PORT 0x10
70#define USBAT_EPP_REGISTER 0x30
71#define USBAT_ATA 0x40
72#define USBAT_ISA 0x50
73
74/* Commands (need to be logically OR'd with an access type */
75#define USBAT_CMD_READ_REG 0x00
76#define USBAT_CMD_WRITE_REG 0x01
77#define USBAT_CMD_READ_BLOCK 0x02
78#define USBAT_CMD_WRITE_BLOCK 0x03
79#define USBAT_CMD_COND_READ_BLOCK 0x04
80#define USBAT_CMD_COND_WRITE_BLOCK 0x05
81#define USBAT_CMD_WRITE_REGS 0x07
82
83/* Commands (these don't need an access type) */
84#define USBAT_CMD_EXEC_CMD 0x80
85#define USBAT_CMD_SET_FEAT 0x81
86#define USBAT_CMD_UIO 0x82
87
88/* Methods of accessing UIO register */
89#define USBAT_UIO_READ 1
90#define USBAT_UIO_WRITE 0
91
92/* Qualifier bits */
93#define USBAT_QUAL_FCQ 0x20 /* full compare */
94#define USBAT_QUAL_ALQ 0x10 /* auto load subcount */
95
96/* USBAT Flash Media status types */
97#define USBAT_FLASH_MEDIA_NONE 0
98#define USBAT_FLASH_MEDIA_CF 1
99
100/* USBAT Flash Media change types */
101#define USBAT_FLASH_MEDIA_SAME 0
102#define USBAT_FLASH_MEDIA_CHANGED 1
103
104/* USBAT ATA registers */
105#define USBAT_ATA_DATA 0x10 /* read/write data (R/W) */
106#define USBAT_ATA_FEATURES 0x11 /* set features (W) */
107#define USBAT_ATA_ERROR 0x11 /* error (R) */
108#define USBAT_ATA_SECCNT 0x12 /* sector count (R/W) */
109#define USBAT_ATA_SECNUM 0x13 /* sector number (R/W) */
110#define USBAT_ATA_LBA_ME 0x14 /* cylinder low (R/W) */
111#define USBAT_ATA_LBA_HI 0x15 /* cylinder high (R/W) */
112#define USBAT_ATA_DEVICE 0x16 /* head/device selection (R/W) */
113#define USBAT_ATA_STATUS 0x17 /* device status (R) */
114#define USBAT_ATA_CMD 0x17 /* device command (W) */
115#define USBAT_ATA_ALTSTATUS 0x0E /* status (no clear IRQ) (R) */
116
117/* USBAT User I/O Data registers */
118#define USBAT_UIO_EPAD 0x80 /* Enable Peripheral Control Signals */
119#define USBAT_UIO_CDT 0x40 /* Card Detect (Read Only) */
120 /* CDT = ACKD & !UI1 & !UI0 */
121#define USBAT_UIO_1 0x20 /* I/O 1 */
122#define USBAT_UIO_0 0x10 /* I/O 0 */
123#define USBAT_UIO_EPP_ATA 0x08 /* 1=EPP mode, 0=ATA mode */
124#define USBAT_UIO_UI1 0x04 /* Input 1 */
125#define USBAT_UIO_UI0 0x02 /* Input 0 */
126#define USBAT_UIO_INTR_ACK 0x01 /* Interrupt (ATA/ISA)/Acknowledge (EPP) */
127
128/* USBAT User I/O Enable registers */
129#define USBAT_UIO_DRVRST 0x80 /* Reset Peripheral */
130#define USBAT_UIO_ACKD 0x40 /* Enable Card Detect */
131#define USBAT_UIO_OE1 0x20 /* I/O 1 set=output/clr=input */
132 /* If ACKD=1, set OE1 to 1 also. */
133#define USBAT_UIO_OE0 0x10 /* I/O 0 set=output/clr=input */
134#define USBAT_UIO_ADPRST 0x01 /* Reset SCM chip */
135
136/* USBAT Features */
137#define USBAT_FEAT_ETEN 0x80 /* External trigger enable */
138#define USBAT_FEAT_U1 0x08
139#define USBAT_FEAT_U0 0x04
140#define USBAT_FEAT_ET1 0x02
141#define USBAT_FEAT_ET2 0x01
142
143struct usbat_info {
144 int devicetype;
145
146 /* Used for Flash readers only */
147 unsigned long sectors; /* total sector count */
148 unsigned long ssize; /* sector size in bytes */
149
150 unsigned char sense_key;
151 unsigned long sense_asc; /* additional sense code */
152 unsigned long sense_ascq; /* additional sense code qualifier */
153};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155#define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
156#define LSB_of(s) ((s)&0xFF)
157#define MSB_of(s) ((s)>>8)
158
159static int transferred = 0;
160
161static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us);
162static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us);
163
Alan Stern26d68182009-02-12 14:48:08 -0500164static int init_usbat_cd(struct us_data *us);
165static int init_usbat_flash(struct us_data *us);
166
167
168/*
169 * The table of devices
170 */
171#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
172 vendorName, productName, useProtocol, useTransport, \
173 initFunction, flags) \
174{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
Sebastian Andrzej Siewiorf61870e2012-08-28 22:37:13 +0200175 .driver_info = (flags) }
Alan Stern26d68182009-02-12 14:48:08 -0500176
Felipe Balbice3af892011-11-15 09:53:40 +0200177static struct usb_device_id usbat_usb_ids[] = {
Alan Stern26d68182009-02-12 14:48:08 -0500178# include "unusual_usbat.h"
179 { } /* Terminating entry */
180};
181MODULE_DEVICE_TABLE(usb, usbat_usb_ids);
182
183#undef UNUSUAL_DEV
184
185/*
186 * The flags table
187 */
188#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
189 vendor_name, product_name, use_protocol, use_transport, \
190 init_function, Flags) \
191{ \
192 .vendorName = vendor_name, \
193 .productName = product_name, \
194 .useProtocol = use_protocol, \
195 .useTransport = use_transport, \
196 .initFunction = init_function, \
197}
198
199static struct us_unusual_dev usbat_unusual_dev_list[] = {
200# include "unusual_usbat.h"
201 { } /* Terminating entry */
202};
203
204#undef UNUSUAL_DEV
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/*
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100207 * Convenience function to produce an ATA read/write sectors command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 * Use cmd=0x20 for read, cmd=0x30 for write
209 */
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100210static void usbat_pack_ata_sector_cmd(unsigned char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 unsigned char thistime,
212 u32 sector, unsigned char cmd)
213{
214 buf[0] = 0;
215 buf[1] = thistime;
216 buf[2] = sector & 0xFF;
217 buf[3] = (sector >> 8) & 0xFF;
218 buf[4] = (sector >> 16) & 0xFF;
219 buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
220 buf[6] = cmd;
221}
222
223/*
224 * Convenience function to get the device type (flash or hp8200)
225 */
226static int usbat_get_device_type(struct us_data *us)
227{
228 return ((struct usbat_info*)us->extra)->devicetype;
229}
230
231/*
232 * Read a register from the device
233 */
234static int usbat_read(struct us_data *us,
235 unsigned char access,
236 unsigned char reg,
237 unsigned char *content)
238{
239 return usb_stor_ctrl_transfer(us,
240 us->recv_ctrl_pipe,
241 access | USBAT_CMD_READ_REG,
242 0xC0,
243 (u16)reg,
244 0,
245 content,
246 1);
247}
248
249/*
250 * Write to a register on the device
251 */
252static int usbat_write(struct us_data *us,
253 unsigned char access,
254 unsigned char reg,
255 unsigned char content)
256{
257 return usb_stor_ctrl_transfer(us,
258 us->send_ctrl_pipe,
259 access | USBAT_CMD_WRITE_REG,
260 0x40,
261 short_pack(reg, content),
262 0,
263 NULL,
264 0);
265}
266
267/*
268 * Convenience function to perform a bulk read
269 */
270static int usbat_bulk_read(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300271 void* buf,
Peter Chubb141804d2006-05-02 18:30:12 +0100272 unsigned int len,
273 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 if (len == 0)
276 return USB_STOR_XFER_GOOD;
277
Joe Perches191648d2013-04-19 11:44:00 -0700278 usb_stor_dbg(us, "len = %d\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +0300279 return usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe, buf, len, use_sg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/*
283 * Convenience function to perform a bulk write
284 */
285static int usbat_bulk_write(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300286 void* buf,
Peter Chubb141804d2006-05-02 18:30:12 +0100287 unsigned int len,
288 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 if (len == 0)
291 return USB_STOR_XFER_GOOD;
292
Joe Perches191648d2013-04-19 11:44:00 -0700293 usb_stor_dbg(us, "len = %d\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +0300294 return usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe, buf, len, use_sg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297/*
298 * Some USBAT-specific commands can only be executed over a command transport
299 * This transport allows one (len=8) or two (len=16) vendor-specific commands
300 * to be executed.
301 */
302static int usbat_execute_command(struct us_data *us,
303 unsigned char *commands,
304 unsigned int len)
305{
306 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
307 USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
308 commands, len);
309}
310
311/*
312 * Read the status register
313 */
314static int usbat_get_status(struct us_data *us, unsigned char *status)
315{
316 int rc;
317 rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
318
Joe Perches191648d2013-04-19 11:44:00 -0700319 usb_stor_dbg(us, "0x%02X\n", *status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return rc;
321}
322
323/*
324 * Check the device status
325 */
326static int usbat_check_status(struct us_data *us)
327{
328 unsigned char *reply = us->iobuf;
329 int rc;
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 rc = usbat_get_status(us, reply);
332 if (rc != USB_STOR_XFER_GOOD)
333 return USB_STOR_TRANSPORT_FAILED;
334
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100335 /* error/check condition (0x51 is ok) */
336 if (*reply & 0x01 && *reply != 0x51)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return USB_STOR_TRANSPORT_FAILED;
338
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100339 /* device fault */
340 if (*reply & 0x20)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return USB_STOR_TRANSPORT_FAILED;
342
343 return USB_STOR_TRANSPORT_GOOD;
344}
345
346/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300347 * Stores critical information in internal registers in preparation for the execution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 * of a conditional usbat_read_blocks or usbat_write_blocks call.
349 */
350static int usbat_set_shuttle_features(struct us_data *us,
351 unsigned char external_trigger,
352 unsigned char epp_control,
353 unsigned char mask_byte,
354 unsigned char test_pattern,
355 unsigned char subcountH,
356 unsigned char subcountL)
357{
358 unsigned char *command = us->iobuf;
359
360 command[0] = 0x40;
361 command[1] = USBAT_CMD_SET_FEAT;
362
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100363 /*
364 * The only bit relevant to ATA access is bit 6
365 * which defines 8 bit data access (set) or 16 bit (unset)
366 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 command[2] = epp_control;
368
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100369 /*
370 * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
371 * ET1 and ET2 define an external event to be checked for on event of a
372 * _read_blocks or _write_blocks operation. The read/write will not take
373 * place unless the defined trigger signal is active.
374 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 command[3] = external_trigger;
376
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100377 /*
378 * The resultant byte of the mask operation (see mask_byte) is compared for
379 * equivalence with this test pattern. If equal, the read/write will take
380 * place.
381 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 command[4] = test_pattern;
383
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100384 /*
385 * This value is logically ANDed with the status register field specified
386 * in the read/write command.
387 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 command[5] = mask_byte;
389
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100390 /*
391 * If ALQ is set in the qualifier, this field contains the address of the
392 * registers where the byte count should be read for transferring the data.
393 * If ALQ is not set, then this field contains the number of bytes to be
394 * transferred.
395 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 command[6] = subcountL;
397 command[7] = subcountH;
398
399 return usbat_execute_command(us, command, 8);
400}
401
402/*
403 * Block, waiting for an ATA device to become not busy or to report
404 * an error condition.
405 */
406static int usbat_wait_not_busy(struct us_data *us, int minutes)
407{
408 int i;
409 int result;
410 unsigned char *status = us->iobuf;
411
Felipe Balbif0183a32016-04-18 13:09:11 +0300412 /*
413 * Synchronizing cache on a CDR could take a heck of a long time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 * but probably not more than 10 minutes or so. On the other hand,
415 * doing a full blank on a CDRW at speed 1 will take about 75
416 * minutes!
417 */
418
419 for (i=0; i<1200+minutes*60; i++) {
420
421 result = usbat_get_status(us, status);
422
423 if (result!=USB_STOR_XFER_GOOD)
424 return USB_STOR_TRANSPORT_ERROR;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100425 if (*status & 0x01) { /* check condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 result = usbat_read(us, USBAT_ATA, 0x10, status);
427 return USB_STOR_TRANSPORT_FAILED;
428 }
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100429 if (*status & 0x20) /* device fault */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return USB_STOR_TRANSPORT_FAILED;
431
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100432 if ((*status & 0x80)==0x00) { /* not busy */
Joe Perches191648d2013-04-19 11:44:00 -0700433 usb_stor_dbg(us, "Waited not busy for %d steps\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return USB_STOR_TRANSPORT_GOOD;
435 }
436
437 if (i<500)
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100438 msleep(10); /* 5 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 else if (i<700)
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100440 msleep(50); /* 10 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 else if (i<1200)
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100442 msleep(100); /* 50 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 else
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100444 msleep(1000); /* X minutes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
Joe Perches191648d2013-04-19 11:44:00 -0700447 usb_stor_dbg(us, "Waited not busy for %d minutes, timing out\n",
448 minutes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return USB_STOR_TRANSPORT_FAILED;
450}
451
452/*
453 * Read block data from the data register
454 */
455static int usbat_read_block(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300456 void* buf,
Peter Chubb141804d2006-05-02 18:30:12 +0100457 unsigned short len,
458 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 int result;
461 unsigned char *command = us->iobuf;
462
463 if (!len)
464 return USB_STOR_TRANSPORT_GOOD;
465
466 command[0] = 0xC0;
467 command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
468 command[2] = USBAT_ATA_DATA;
469 command[3] = 0;
470 command[4] = 0;
471 command[5] = 0;
472 command[6] = LSB_of(len);
473 command[7] = MSB_of(len);
474
475 result = usbat_execute_command(us, command, 8);
476 if (result != USB_STOR_XFER_GOOD)
477 return USB_STOR_TRANSPORT_ERROR;
478
Boaz Harrosh4776e992007-09-09 20:40:56 +0300479 result = usbat_bulk_read(us, buf, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return (result == USB_STOR_XFER_GOOD ?
481 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
482}
483
484/*
485 * Write block data via the data register
486 */
487static int usbat_write_block(struct us_data *us,
488 unsigned char access,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300489 void* buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 unsigned short len,
Peter Chubb141804d2006-05-02 18:30:12 +0100491 int minutes,
492 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 int result;
495 unsigned char *command = us->iobuf;
496
497 if (!len)
498 return USB_STOR_TRANSPORT_GOOD;
499
500 command[0] = 0x40;
501 command[1] = access | USBAT_CMD_WRITE_BLOCK;
502 command[2] = USBAT_ATA_DATA;
503 command[3] = 0;
504 command[4] = 0;
505 command[5] = 0;
506 command[6] = LSB_of(len);
507 command[7] = MSB_of(len);
508
509 result = usbat_execute_command(us, command, 8);
510
511 if (result != USB_STOR_XFER_GOOD)
512 return USB_STOR_TRANSPORT_ERROR;
513
Boaz Harrosh4776e992007-09-09 20:40:56 +0300514 result = usbat_bulk_write(us, buf, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (result != USB_STOR_XFER_GOOD)
516 return USB_STOR_TRANSPORT_ERROR;
517
518 return usbat_wait_not_busy(us, minutes);
519}
520
521/*
522 * Process read and write requests
523 */
524static int usbat_hp8200e_rw_block_test(struct us_data *us,
525 unsigned char access,
526 unsigned char *registers,
527 unsigned char *data_out,
528 unsigned short num_registers,
529 unsigned char data_reg,
530 unsigned char status_reg,
531 unsigned char timeout,
532 unsigned char qualifier,
533 int direction,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300534 void *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 unsigned short len,
536 int use_sg,
537 int minutes)
538{
539 int result;
540 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
541 us->recv_bulk_pipe : us->send_bulk_pipe;
542
543 unsigned char *command = us->iobuf;
544 int i, j;
545 int cmdlen;
546 unsigned char *data = us->iobuf;
547 unsigned char *status = us->iobuf;
548
549 BUG_ON(num_registers > US_IOBUF_SIZE/2);
550
551 for (i=0; i<20; i++) {
552
553 /*
554 * The first time we send the full command, which consists
555 * of downloading the SCSI command followed by downloading
556 * the data via a write-and-test. Any other time we only
557 * send the command to download the data -- the SCSI command
558 * is still 'active' in some sense in the device.
559 *
560 * We're only going to try sending the data 10 times. After
561 * that, we just return a failure.
562 */
563
564 if (i==0) {
565 cmdlen = 16;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100566 /*
567 * Write to multiple registers
568 * Not really sure the 0x07, 0x17, 0xfc, 0xe7 is
569 * necessary here, but that's what came out of the
570 * trace every single time.
571 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 command[0] = 0x40;
573 command[1] = access | USBAT_CMD_WRITE_REGS;
574 command[2] = 0x07;
575 command[3] = 0x17;
576 command[4] = 0xFC;
577 command[5] = 0xE7;
578 command[6] = LSB_of(num_registers*2);
579 command[7] = MSB_of(num_registers*2);
580 } else
581 cmdlen = 8;
582
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100583 /* Conditionally read or write blocks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 command[cmdlen-8] = (direction==DMA_TO_DEVICE ? 0x40 : 0xC0);
585 command[cmdlen-7] = access |
586 (direction==DMA_TO_DEVICE ?
587 USBAT_CMD_COND_WRITE_BLOCK : USBAT_CMD_COND_READ_BLOCK);
588 command[cmdlen-6] = data_reg;
589 command[cmdlen-5] = status_reg;
590 command[cmdlen-4] = timeout;
591 command[cmdlen-3] = qualifier;
592 command[cmdlen-2] = LSB_of(len);
593 command[cmdlen-1] = MSB_of(len);
594
595 result = usbat_execute_command(us, command, cmdlen);
596
597 if (result != USB_STOR_XFER_GOOD)
598 return USB_STOR_TRANSPORT_ERROR;
599
600 if (i==0) {
601
602 for (j=0; j<num_registers; j++) {
603 data[j<<1] = registers[j];
604 data[1+(j<<1)] = data_out[j];
605 }
606
Peter Chubb141804d2006-05-02 18:30:12 +0100607 result = usbat_bulk_write(us, data, num_registers*2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (result != USB_STOR_XFER_GOOD)
609 return USB_STOR_TRANSPORT_ERROR;
610
611 }
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 result = usb_stor_bulk_transfer_sg(us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300614 pipe, buf, len, use_sg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 /*
617 * If we get a stall on the bulk download, we'll retry
618 * the bulk download -- but not the SCSI command because
619 * in some sense the SCSI command is still 'active' and
620 * waiting for the data. Don't ask me why this should be;
621 * I'm only following what the Windoze driver did.
622 *
623 * Note that a stall for the test-and-read/write command means
624 * that the test failed. In this case we're testing to make
625 * sure that the device is error-free
626 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
627 * hypothesis is that the USBAT chip somehow knows what
628 * the device will accept, but doesn't give the device any
629 * data until all data is received. Thus, the device would
630 * still be waiting for the first byte of data if a stall
631 * occurs, even if the stall implies that some data was
632 * transferred.
633 */
634
635 if (result == USB_STOR_XFER_SHORT ||
636 result == USB_STOR_XFER_STALLED) {
637
638 /*
639 * If we're reading and we stalled, then clear
640 * the bulk output pipe only the first time.
641 */
642
643 if (direction==DMA_FROM_DEVICE && i==0) {
644 if (usb_stor_clear_halt(us,
645 us->send_bulk_pipe) < 0)
646 return USB_STOR_TRANSPORT_ERROR;
647 }
648
649 /*
650 * Read status: is the device angry, or just busy?
651 */
652
653 result = usbat_read(us, USBAT_ATA,
654 direction==DMA_TO_DEVICE ?
655 USBAT_ATA_STATUS : USBAT_ATA_ALTSTATUS,
656 status);
657
658 if (result!=USB_STOR_XFER_GOOD)
659 return USB_STOR_TRANSPORT_ERROR;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100660 if (*status & 0x01) /* check condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return USB_STOR_TRANSPORT_FAILED;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100662 if (*status & 0x20) /* device fault */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return USB_STOR_TRANSPORT_FAILED;
664
Joe Perches191648d2013-04-19 11:44:00 -0700665 usb_stor_dbg(us, "Redoing %s\n",
666 direction == DMA_TO_DEVICE
667 ? "write" : "read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 } else if (result != USB_STOR_XFER_GOOD)
670 return USB_STOR_TRANSPORT_ERROR;
671 else
672 return usbat_wait_not_busy(us, minutes);
673
674 }
675
Joe Perches191648d2013-04-19 11:44:00 -0700676 usb_stor_dbg(us, "Bummer! %s bulk data 20 times failed\n",
677 direction == DMA_TO_DEVICE ? "Writing" : "Reading");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 return USB_STOR_TRANSPORT_FAILED;
680}
681
682/*
683 * Write to multiple registers:
684 * Allows us to write specific data to any registers. The data to be written
685 * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN
686 * which gets sent through bulk out.
687 * Not designed for large transfers of data!
688 */
689static int usbat_multiple_write(struct us_data *us,
690 unsigned char *registers,
691 unsigned char *data_out,
692 unsigned short num_registers)
693{
694 int i, result;
695 unsigned char *data = us->iobuf;
696 unsigned char *command = us->iobuf;
697
698 BUG_ON(num_registers > US_IOBUF_SIZE/2);
699
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100700 /* Write to multiple registers, ATA access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 command[0] = 0x40;
702 command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
703
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100704 /* No relevance */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 command[2] = 0;
706 command[3] = 0;
707 command[4] = 0;
708 command[5] = 0;
709
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100710 /* Number of bytes to be transferred (incl. addresses and data) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 command[6] = LSB_of(num_registers*2);
712 command[7] = MSB_of(num_registers*2);
713
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100714 /* The setup command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 result = usbat_execute_command(us, command, 8);
716 if (result != USB_STOR_XFER_GOOD)
717 return USB_STOR_TRANSPORT_ERROR;
718
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100719 /* Create the reg/data, reg/data sequence */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 for (i=0; i<num_registers; i++) {
721 data[i<<1] = registers[i];
722 data[1+(i<<1)] = data_out[i];
723 }
724
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100725 /* Send the data */
Peter Chubb141804d2006-05-02 18:30:12 +0100726 result = usbat_bulk_write(us, data, num_registers*2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (result != USB_STOR_XFER_GOOD)
728 return USB_STOR_TRANSPORT_ERROR;
729
730 if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
731 return usbat_wait_not_busy(us, 0);
732 else
733 return USB_STOR_TRANSPORT_GOOD;
734}
735
736/*
737 * Conditionally read blocks from device:
738 * Allows us to read blocks from a specific data register, based upon the
739 * condition that a status register can be successfully masked with a status
740 * qualifier. If this condition is not initially met, the read will wait
741 * up until a maximum amount of time has elapsed, as specified by timeout.
742 * The read will start when the condition is met, otherwise the command aborts.
743 *
744 * The qualifier defined here is not the value that is masked, it defines
745 * conditions for the write to take place. The actual masked qualifier (and
746 * other related details) are defined beforehand with _set_shuttle_features().
747 */
748static int usbat_read_blocks(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300749 void* buffer,
Peter Chubb141804d2006-05-02 18:30:12 +0100750 int len,
751 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 int result;
754 unsigned char *command = us->iobuf;
755
756 command[0] = 0xC0;
757 command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK;
758 command[2] = USBAT_ATA_DATA;
759 command[3] = USBAT_ATA_STATUS;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100760 command[4] = 0xFD; /* Timeout (ms); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 command[5] = USBAT_QUAL_FCQ;
762 command[6] = LSB_of(len);
763 command[7] = MSB_of(len);
764
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100765 /* Multiple block read setup command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 result = usbat_execute_command(us, command, 8);
767 if (result != USB_STOR_XFER_GOOD)
768 return USB_STOR_TRANSPORT_FAILED;
769
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100770 /* Read the blocks we just asked for */
Peter Chubb141804d2006-05-02 18:30:12 +0100771 result = usbat_bulk_read(us, buffer, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (result != USB_STOR_XFER_GOOD)
773 return USB_STOR_TRANSPORT_FAILED;
774
775 return USB_STOR_TRANSPORT_GOOD;
776}
777
778/*
779 * Conditionally write blocks to device:
780 * Allows us to write blocks to a specific data register, based upon the
781 * condition that a status register can be successfully masked with a status
782 * qualifier. If this condition is not initially met, the write will wait
783 * up until a maximum amount of time has elapsed, as specified by timeout.
784 * The read will start when the condition is met, otherwise the command aborts.
785 *
786 * The qualifier defined here is not the value that is masked, it defines
787 * conditions for the write to take place. The actual masked qualifier (and
788 * other related details) are defined beforehand with _set_shuttle_features().
789 */
790static int usbat_write_blocks(struct us_data *us,
Boaz Harrosh4776e992007-09-09 20:40:56 +0300791 void* buffer,
Peter Chubb141804d2006-05-02 18:30:12 +0100792 int len,
793 int use_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 int result;
796 unsigned char *command = us->iobuf;
797
798 command[0] = 0x40;
799 command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK;
800 command[2] = USBAT_ATA_DATA;
801 command[3] = USBAT_ATA_STATUS;
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100802 command[4] = 0xFD; /* Timeout (ms) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 command[5] = USBAT_QUAL_FCQ;
804 command[6] = LSB_of(len);
805 command[7] = MSB_of(len);
806
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100807 /* Multiple block write setup command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 result = usbat_execute_command(us, command, 8);
809 if (result != USB_STOR_XFER_GOOD)
810 return USB_STOR_TRANSPORT_FAILED;
811
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100812 /* Write the data */
Peter Chubb141804d2006-05-02 18:30:12 +0100813 result = usbat_bulk_write(us, buffer, len, use_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (result != USB_STOR_XFER_GOOD)
815 return USB_STOR_TRANSPORT_FAILED;
816
817 return USB_STOR_TRANSPORT_GOOD;
818}
819
820/*
821 * Read the User IO register
822 */
823static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
824{
825 int result;
826
827 result = usb_stor_ctrl_transfer(us,
828 us->recv_ctrl_pipe,
829 USBAT_CMD_UIO,
830 0xC0,
831 0,
832 0,
833 data_flags,
834 USBAT_UIO_READ);
835
Joe Perches191648d2013-04-19 11:44:00 -0700836 usb_stor_dbg(us, "UIO register reads %02X\n", *data_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 return result;
839}
840
841/*
842 * Write to the User IO register
843 */
844static int usbat_write_user_io(struct us_data *us,
845 unsigned char enable_flags,
846 unsigned char data_flags)
847{
848 return usb_stor_ctrl_transfer(us,
849 us->send_ctrl_pipe,
850 USBAT_CMD_UIO,
851 0x40,
852 short_pack(enable_flags, data_flags),
853 0,
854 NULL,
855 USBAT_UIO_WRITE);
856}
857
858/*
859 * Reset the device
860 * Often needed on media change.
861 */
862static int usbat_device_reset(struct us_data *us)
863{
864 int rc;
865
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100866 /*
867 * Reset peripheral, enable peripheral control signals
868 * (bring reset signal up)
869 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 rc = usbat_write_user_io(us,
871 USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
872 USBAT_UIO_EPAD | USBAT_UIO_1);
873 if (rc != USB_STOR_XFER_GOOD)
874 return USB_STOR_TRANSPORT_ERROR;
875
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100876 /*
877 * Enable peripheral control signals
878 * (bring reset signal down)
879 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 rc = usbat_write_user_io(us,
881 USBAT_UIO_OE1 | USBAT_UIO_OE0,
882 USBAT_UIO_EPAD | USBAT_UIO_1);
883 if (rc != USB_STOR_XFER_GOOD)
884 return USB_STOR_TRANSPORT_ERROR;
885
886 return USB_STOR_TRANSPORT_GOOD;
887}
888
889/*
890 * Enable card detect
891 */
892static int usbat_device_enable_cdt(struct us_data *us)
893{
894 int rc;
895
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100896 /* Enable peripheral control signals and card detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 rc = usbat_write_user_io(us,
898 USBAT_UIO_ACKD | USBAT_UIO_OE1 | USBAT_UIO_OE0,
899 USBAT_UIO_EPAD | USBAT_UIO_1);
900 if (rc != USB_STOR_XFER_GOOD)
901 return USB_STOR_TRANSPORT_ERROR;
902
903 return USB_STOR_TRANSPORT_GOOD;
904}
905
906/*
907 * Determine if media is present.
908 */
Joe Perches191648d2013-04-19 11:44:00 -0700909static int usbat_flash_check_media_present(struct us_data *us,
910 unsigned char *uio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 if (*uio & USBAT_UIO_UI0) {
Joe Perches191648d2013-04-19 11:44:00 -0700913 usb_stor_dbg(us, "no media detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return USBAT_FLASH_MEDIA_NONE;
915 }
916
917 return USBAT_FLASH_MEDIA_CF;
918}
919
920/*
921 * Determine if media has changed since last operation
922 */
Joe Perches191648d2013-04-19 11:44:00 -0700923static int usbat_flash_check_media_changed(struct us_data *us,
924 unsigned char *uio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 if (*uio & USBAT_UIO_0) {
Joe Perches191648d2013-04-19 11:44:00 -0700927 usb_stor_dbg(us, "media change detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return USBAT_FLASH_MEDIA_CHANGED;
929 }
930
931 return USBAT_FLASH_MEDIA_SAME;
932}
933
934/*
935 * Check for media change / no media and handle the situation appropriately
936 */
937static int usbat_flash_check_media(struct us_data *us,
938 struct usbat_info *info)
939{
940 int rc;
941 unsigned char *uio = us->iobuf;
942
943 rc = usbat_read_user_io(us, uio);
944 if (rc != USB_STOR_XFER_GOOD)
945 return USB_STOR_TRANSPORT_ERROR;
946
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100947 /* Check for media existence */
Joe Perches191648d2013-04-19 11:44:00 -0700948 rc = usbat_flash_check_media_present(us, uio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (rc == USBAT_FLASH_MEDIA_NONE) {
950 info->sense_key = 0x02;
951 info->sense_asc = 0x3A;
952 info->sense_ascq = 0x00;
953 return USB_STOR_TRANSPORT_FAILED;
954 }
955
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100956 /* Check for media change */
Joe Perches191648d2013-04-19 11:44:00 -0700957 rc = usbat_flash_check_media_changed(us, uio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (rc == USBAT_FLASH_MEDIA_CHANGED) {
959
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100960 /* Reset and re-enable card detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 rc = usbat_device_reset(us);
962 if (rc != USB_STOR_TRANSPORT_GOOD)
963 return rc;
964 rc = usbat_device_enable_cdt(us);
965 if (rc != USB_STOR_TRANSPORT_GOOD)
966 return rc;
967
968 msleep(50);
969
970 rc = usbat_read_user_io(us, uio);
971 if (rc != USB_STOR_XFER_GOOD)
972 return USB_STOR_TRANSPORT_ERROR;
973
974 info->sense_key = UNIT_ATTENTION;
975 info->sense_asc = 0x28;
976 info->sense_ascq = 0x00;
977 return USB_STOR_TRANSPORT_FAILED;
978 }
979
980 return USB_STOR_TRANSPORT_GOOD;
981}
982
983/*
984 * Determine whether we are controlling a flash-based reader/writer,
985 * or a HP8200-based CD drive.
986 * Sets transport functions as appropriate.
987 */
988static int usbat_identify_device(struct us_data *us,
989 struct usbat_info *info)
990{
991 int rc;
992 unsigned char status;
993
994 if (!us || !info)
995 return USB_STOR_TRANSPORT_ERROR;
996
997 rc = usbat_device_reset(us);
998 if (rc != USB_STOR_TRANSPORT_GOOD)
999 return rc;
Daniel Drake8845add2005-11-17 09:48:01 -08001000 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /*
Daniel Drake68a64572005-08-10 18:30:04 +01001003 * In attempt to distinguish between HP CDRW's and Flash readers, we now
1004 * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
1005 * readers), this command should fail with error. On ATAPI devices (i.e.
1006 * CDROM drives), it should succeed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 */
Daniel Drake68a64572005-08-10 18:30:04 +01001008 rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1);
1009 if (rc != USB_STOR_XFER_GOOD)
1010 return USB_STOR_TRANSPORT_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Daniel Drake68a64572005-08-10 18:30:04 +01001012 rc = usbat_get_status(us, &status);
1013 if (rc != USB_STOR_XFER_GOOD)
1014 return USB_STOR_TRANSPORT_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001016 /* Check for error bit, or if the command 'fell through' */
Daniel Drakea8798532005-09-29 00:14:21 +01001017 if (status == 0xA1 || !(status & 0x01)) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001018 /* Device is HP 8200 */
Joe Perches191648d2013-04-19 11:44:00 -07001019 usb_stor_dbg(us, "Detected HP8200 CDRW\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 info->devicetype = USBAT_DEV_HP8200;
Daniel Drakea8798532005-09-29 00:14:21 +01001021 } else {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001022 /* Device is a CompactFlash reader/writer */
Joe Perches191648d2013-04-19 11:44:00 -07001023 usb_stor_dbg(us, "Detected Flash reader/writer\n");
Daniel Drakea8798532005-09-29 00:14:21 +01001024 info->devicetype = USBAT_DEV_FLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026
1027 return USB_STOR_TRANSPORT_GOOD;
1028}
1029
1030/*
1031 * Set the transport function based on the device type
1032 */
1033static int usbat_set_transport(struct us_data *us,
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001034 struct usbat_info *info,
1035 int devicetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001038 if (!info->devicetype)
1039 info->devicetype = devicetype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001041 if (!info->devicetype)
1042 usbat_identify_device(us, info);
1043
1044 switch (info->devicetype) {
1045 default:
1046 return USB_STOR_TRANSPORT_ERROR;
1047
1048 case USBAT_DEV_HP8200:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 us->transport = usbat_hp8200e_transport;
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001050 break;
1051
1052 case USBAT_DEV_FLASH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 us->transport = usbat_flash_transport;
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001054 break;
1055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 return 0;
1058}
1059
1060/*
1061 * Read the media capacity
1062 */
1063static int usbat_flash_get_sector_count(struct us_data *us,
1064 struct usbat_info *info)
1065{
1066 unsigned char registers[3] = {
1067 USBAT_ATA_SECCNT,
1068 USBAT_ATA_DEVICE,
1069 USBAT_ATA_CMD,
1070 };
1071 unsigned char command[3] = { 0x01, 0xA0, 0xEC };
1072 unsigned char *reply;
1073 unsigned char status;
1074 int rc;
1075
1076 if (!us || !info)
1077 return USB_STOR_TRANSPORT_ERROR;
1078
1079 reply = kmalloc(512, GFP_NOIO);
1080 if (!reply)
1081 return USB_STOR_TRANSPORT_ERROR;
1082
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001083 /* ATA command : IDENTIFY DEVICE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 rc = usbat_multiple_write(us, registers, command, 3);
1085 if (rc != USB_STOR_XFER_GOOD) {
Joe Perches191648d2013-04-19 11:44:00 -07001086 usb_stor_dbg(us, "Gah! identify_device failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 rc = USB_STOR_TRANSPORT_ERROR;
1088 goto leave;
1089 }
1090
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001091 /* Read device status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
1093 rc = USB_STOR_TRANSPORT_ERROR;
1094 goto leave;
1095 }
1096
1097 msleep(100);
1098
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001099 /* Read the device identification data */
Peter Chubb141804d2006-05-02 18:30:12 +01001100 rc = usbat_read_block(us, reply, 512, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (rc != USB_STOR_TRANSPORT_GOOD)
1102 goto leave;
1103
1104 info->sectors = ((u32)(reply[117]) << 24) |
1105 ((u32)(reply[116]) << 16) |
1106 ((u32)(reply[115]) << 8) |
1107 ((u32)(reply[114]) );
1108
1109 rc = USB_STOR_TRANSPORT_GOOD;
1110
1111 leave:
1112 kfree(reply);
1113 return rc;
1114}
1115
1116/*
1117 * Read data from device
1118 */
1119static int usbat_flash_read_data(struct us_data *us,
1120 struct usbat_info *info,
1121 u32 sector,
1122 u32 sectors)
1123{
1124 unsigned char registers[7] = {
1125 USBAT_ATA_FEATURES,
1126 USBAT_ATA_SECCNT,
1127 USBAT_ATA_SECNUM,
1128 USBAT_ATA_LBA_ME,
1129 USBAT_ATA_LBA_HI,
1130 USBAT_ATA_DEVICE,
1131 USBAT_ATA_STATUS,
1132 };
1133 unsigned char command[7];
1134 unsigned char *buffer;
1135 unsigned char thistime;
1136 unsigned int totallen, alloclen;
1137 int len, result;
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001138 unsigned int sg_offset = 0;
1139 struct scatterlist *sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 result = usbat_flash_check_media(us, info);
1142 if (result != USB_STOR_TRANSPORT_GOOD)
1143 return result;
1144
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001145 /*
1146 * we're working in LBA mode. according to the ATA spec,
1147 * we can support up to 28-bit addressing. I don't know if Jumpshot
1148 * supports beyond 24-bit addressing. It's kind of hard to test
1149 * since it requires > 8GB CF card.
1150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 if (sector > 0x0FFFFFFF)
1153 return USB_STOR_TRANSPORT_ERROR;
1154
1155 totallen = sectors * info->ssize;
1156
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001157 /*
1158 * Since we don't read more than 64 KB at a time, we have to create
1159 * a bounce buffer and move the data a piece at a time between the
1160 * bounce buffer and the actual transfer buffer.
1161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 alloclen = min(totallen, 65536u);
1164 buffer = kmalloc(alloclen, GFP_NOIO);
1165 if (buffer == NULL)
1166 return USB_STOR_TRANSPORT_ERROR;
1167
1168 do {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001169 /*
1170 * loop, never allocate or transfer more than 64k at once
1171 * (min(128k, 255*info->ssize) is the real limit)
1172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 len = min(totallen, alloclen);
1174 thistime = (len / info->ssize) & 0xff;
1175
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001176 /* ATA command 0x20 (READ SECTORS) */
1177 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001179 /* Write/execute ATA read command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 result = usbat_multiple_write(us, registers, command, 7);
1181 if (result != USB_STOR_TRANSPORT_GOOD)
1182 goto leave;
1183
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001184 /* Read the data we just requested */
Peter Chubb141804d2006-05-02 18:30:12 +01001185 result = usbat_read_blocks(us, buffer, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (result != USB_STOR_TRANSPORT_GOOD)
1187 goto leave;
1188
Joe Perches191648d2013-04-19 11:44:00 -07001189 usb_stor_dbg(us, "%d bytes\n", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001191 /* Store the data in the transfer buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 usb_stor_access_xfer_buf(buffer, len, us->srb,
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001193 &sg, &sg_offset, TO_XFER_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 sector += thistime;
1196 totallen -= len;
1197 } while (totallen > 0);
1198
1199 kfree(buffer);
1200 return USB_STOR_TRANSPORT_GOOD;
1201
1202leave:
1203 kfree(buffer);
1204 return USB_STOR_TRANSPORT_ERROR;
1205}
1206
1207/*
1208 * Write data to device
1209 */
1210static int usbat_flash_write_data(struct us_data *us,
1211 struct usbat_info *info,
1212 u32 sector,
1213 u32 sectors)
1214{
1215 unsigned char registers[7] = {
1216 USBAT_ATA_FEATURES,
1217 USBAT_ATA_SECCNT,
1218 USBAT_ATA_SECNUM,
1219 USBAT_ATA_LBA_ME,
1220 USBAT_ATA_LBA_HI,
1221 USBAT_ATA_DEVICE,
1222 USBAT_ATA_STATUS,
1223 };
1224 unsigned char command[7];
1225 unsigned char *buffer;
1226 unsigned char thistime;
1227 unsigned int totallen, alloclen;
1228 int len, result;
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001229 unsigned int sg_offset = 0;
1230 struct scatterlist *sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 result = usbat_flash_check_media(us, info);
1233 if (result != USB_STOR_TRANSPORT_GOOD)
1234 return result;
1235
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001236 /*
1237 * we're working in LBA mode. according to the ATA spec,
1238 * we can support up to 28-bit addressing. I don't know if the device
1239 * supports beyond 24-bit addressing. It's kind of hard to test
1240 * since it requires > 8GB media.
1241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 if (sector > 0x0FFFFFFF)
1244 return USB_STOR_TRANSPORT_ERROR;
1245
1246 totallen = sectors * info->ssize;
1247
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001248 /*
1249 * Since we don't write more than 64 KB at a time, we have to create
1250 * a bounce buffer and move the data a piece at a time between the
1251 * bounce buffer and the actual transfer buffer.
1252 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 alloclen = min(totallen, 65536u);
1255 buffer = kmalloc(alloclen, GFP_NOIO);
1256 if (buffer == NULL)
1257 return USB_STOR_TRANSPORT_ERROR;
1258
1259 do {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001260 /*
1261 * loop, never allocate or transfer more than 64k at once
1262 * (min(128k, 255*info->ssize) is the real limit)
1263 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 len = min(totallen, alloclen);
1265 thistime = (len / info->ssize) & 0xff;
1266
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001267 /* Get the data from the transfer buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 usb_stor_access_xfer_buf(buffer, len, us->srb,
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001269 &sg, &sg_offset, FROM_XFER_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001271 /* ATA command 0x30 (WRITE SECTORS) */
1272 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001274 /* Write/execute ATA write command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 result = usbat_multiple_write(us, registers, command, 7);
1276 if (result != USB_STOR_TRANSPORT_GOOD)
1277 goto leave;
1278
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001279 /* Write the data */
Peter Chubb141804d2006-05-02 18:30:12 +01001280 result = usbat_write_blocks(us, buffer, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (result != USB_STOR_TRANSPORT_GOOD)
1282 goto leave;
1283
1284 sector += thistime;
1285 totallen -= len;
1286 } while (totallen > 0);
1287
1288 kfree(buffer);
1289 return result;
1290
1291leave:
1292 kfree(buffer);
1293 return USB_STOR_TRANSPORT_ERROR;
1294}
1295
1296/*
1297 * Squeeze a potentially huge (> 65535 byte) read10 command into
1298 * a little ( <= 65535 byte) ATAPI pipe
1299 */
1300static int usbat_hp8200e_handle_read10(struct us_data *us,
1301 unsigned char *registers,
1302 unsigned char *data,
1303 struct scsi_cmnd *srb)
1304{
1305 int result = USB_STOR_TRANSPORT_GOOD;
1306 unsigned char *buffer;
1307 unsigned int len;
1308 unsigned int sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 unsigned int sg_offset = 0;
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001310 struct scatterlist *sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Joe Perches191648d2013-04-19 11:44:00 -07001312 usb_stor_dbg(us, "transfersize %d\n", srb->transfersize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Boaz Harrosh4776e992007-09-09 20:40:56 +03001314 if (scsi_bufflen(srb) < 0x10000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1317 registers, data, 19,
1318 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1319 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1320 DMA_FROM_DEVICE,
Boaz Harrosh4776e992007-09-09 20:40:56 +03001321 scsi_sglist(srb),
1322 scsi_bufflen(srb), scsi_sg_count(srb), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 return result;
1325 }
1326
1327 /*
1328 * Since we're requesting more data than we can handle in
1329 * a single read command (max is 64k-1), we will perform
1330 * multiple reads, but each read must be in multiples of
1331 * a sector. Luckily the sector size is in srb->transfersize
1332 * (see linux/drivers/scsi/sr.c).
1333 */
1334
1335 if (data[7+0] == GPCMD_READ_CD) {
1336 len = short_pack(data[7+9], data[7+8]);
1337 len <<= 16;
1338 len |= data[7+7];
Joe Perches191648d2013-04-19 11:44:00 -07001339 usb_stor_dbg(us, "GPCMD_READ_CD: len %d\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +03001340 srb->transfersize = scsi_bufflen(srb)/len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342
1343 if (!srb->transfersize) {
1344 srb->transfersize = 2048; /* A guess */
Joe Perches191648d2013-04-19 11:44:00 -07001345 usb_stor_dbg(us, "transfersize 0, forcing %d\n",
1346 srb->transfersize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001349 /*
1350 * Since we only read in one block at a time, we have to create
1351 * a bounce buffer and move the data a piece at a time between the
1352 * bounce buffer and the actual transfer buffer.
1353 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 len = (65535/srb->transfersize) * srb->transfersize;
Joe Perches191648d2013-04-19 11:44:00 -07001356 usb_stor_dbg(us, "Max read is %d bytes\n", len);
Boaz Harrosh4776e992007-09-09 20:40:56 +03001357 len = min(len, scsi_bufflen(srb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 buffer = kmalloc(len, GFP_NOIO);
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001359 if (buffer == NULL) /* bloody hell! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return USB_STOR_TRANSPORT_FAILED;
1361 sector = short_pack(data[7+3], data[7+2]);
1362 sector <<= 16;
1363 sector |= short_pack(data[7+5], data[7+4]);
1364 transferred = 0;
1365
Boaz Harrosh4776e992007-09-09 20:40:56 +03001366 while (transferred != scsi_bufflen(srb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Boaz Harrosh4776e992007-09-09 20:40:56 +03001368 if (len > scsi_bufflen(srb) - transferred)
1369 len = scsi_bufflen(srb) - transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001371 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1372 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001374 /* Fix up the SCSI command sector and num sectors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001376 data[7+2] = MSB_of(sector>>16); /* SCSI command sector */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 data[7+3] = LSB_of(sector>>16);
1378 data[7+4] = MSB_of(sector&0xFFFF);
1379 data[7+5] = LSB_of(sector&0xFFFF);
1380 if (data[7+0] == GPCMD_READ_CD)
1381 data[7+6] = 0;
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001382 data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */
1383 data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1386 registers, data, 19,
1387 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1388 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1389 DMA_FROM_DEVICE,
1390 buffer,
1391 len, 0, 1);
1392
1393 if (result != USB_STOR_TRANSPORT_GOOD)
1394 break;
1395
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001396 /* Store the data in the transfer buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 usb_stor_access_xfer_buf(buffer, len, srb,
Jens Axboe1f6f31a2007-05-11 12:33:09 +02001398 &sg, &sg_offset, TO_XFER_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001400 /* Update the amount transferred and the sector number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 transferred += len;
1403 sector += len / srb->transfersize;
1404
Boaz Harrosh4776e992007-09-09 20:40:56 +03001405 } /* while transferred != scsi_bufflen(srb) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 kfree(buffer);
1408 return result;
1409}
1410
1411static int usbat_select_and_test_registers(struct us_data *us)
1412{
1413 int selector;
1414 unsigned char *status = us->iobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001416 /* try device = master, then device = slave. */
Daniel Drake68a64572005-08-10 18:30:04 +01001417 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
1418 if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 USB_STOR_XFER_GOOD)
1420 return USB_STOR_TRANSPORT_ERROR;
1421
1422 if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) !=
1423 USB_STOR_XFER_GOOD)
1424 return USB_STOR_TRANSPORT_ERROR;
1425
1426 if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) !=
1427 USB_STOR_XFER_GOOD)
1428 return USB_STOR_TRANSPORT_ERROR;
1429
1430 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1431 USB_STOR_XFER_GOOD)
1432 return USB_STOR_TRANSPORT_ERROR;
1433
1434 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1435 USB_STOR_XFER_GOOD)
1436 return USB_STOR_TRANSPORT_ERROR;
1437
1438 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) !=
1439 USB_STOR_XFER_GOOD)
1440 return USB_STOR_TRANSPORT_ERROR;
1441
1442 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) !=
1443 USB_STOR_XFER_GOOD)
1444 return USB_STOR_TRANSPORT_ERROR;
1445
1446 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1447 USB_STOR_XFER_GOOD)
1448 return USB_STOR_TRANSPORT_ERROR;
1449
1450 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1451 USB_STOR_XFER_GOOD)
1452 return USB_STOR_TRANSPORT_ERROR;
1453 }
1454
1455 return USB_STOR_TRANSPORT_GOOD;
1456}
1457
1458/*
1459 * Initialize the USBAT processor and the storage device
1460 */
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001461static int init_usbat(struct us_data *us, int devicetype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
1463 int rc;
1464 struct usbat_info *info;
1465 unsigned char subcountH = USBAT_ATA_LBA_HI;
1466 unsigned char subcountL = USBAT_ATA_LBA_ME;
1467 unsigned char *status = us->iobuf;
1468
Oliver Neukum887c2562006-01-08 12:33:45 +01001469 us->extra = kzalloc(sizeof(struct usbat_info), GFP_NOIO);
Joe Perches191648d2013-04-19 11:44:00 -07001470 if (!us->extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return 1;
Joe Perches191648d2013-04-19 11:44:00 -07001472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 info = (struct usbat_info *) (us->extra);
1474
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001475 /* Enable peripheral control signals */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 rc = usbat_write_user_io(us,
1477 USBAT_UIO_OE1 | USBAT_UIO_OE0,
1478 USBAT_UIO_EPAD | USBAT_UIO_1);
1479 if (rc != USB_STOR_XFER_GOOD)
1480 return USB_STOR_TRANSPORT_ERROR;
1481
Joe Perches191648d2013-04-19 11:44:00 -07001482 usb_stor_dbg(us, "INIT 1\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 msleep(2000);
1485
1486 rc = usbat_read_user_io(us, status);
1487 if (rc != USB_STOR_TRANSPORT_GOOD)
1488 return rc;
1489
Joe Perches191648d2013-04-19 11:44:00 -07001490 usb_stor_dbg(us, "INIT 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 rc = usbat_read_user_io(us, status);
1493 if (rc != USB_STOR_XFER_GOOD)
1494 return USB_STOR_TRANSPORT_ERROR;
1495
1496 rc = usbat_read_user_io(us, status);
1497 if (rc != USB_STOR_XFER_GOOD)
1498 return USB_STOR_TRANSPORT_ERROR;
1499
Joe Perches191648d2013-04-19 11:44:00 -07001500 usb_stor_dbg(us, "INIT 3\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 rc = usbat_select_and_test_registers(us);
1503 if (rc != USB_STOR_TRANSPORT_GOOD)
1504 return rc;
1505
Joe Perches191648d2013-04-19 11:44:00 -07001506 usb_stor_dbg(us, "INIT 4\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 rc = usbat_read_user_io(us, status);
1509 if (rc != USB_STOR_XFER_GOOD)
1510 return USB_STOR_TRANSPORT_ERROR;
1511
Joe Perches191648d2013-04-19 11:44:00 -07001512 usb_stor_dbg(us, "INIT 5\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001514 /* Enable peripheral control signals and card detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 rc = usbat_device_enable_cdt(us);
1516 if (rc != USB_STOR_TRANSPORT_GOOD)
1517 return rc;
1518
Joe Perches191648d2013-04-19 11:44:00 -07001519 usb_stor_dbg(us, "INIT 6\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 rc = usbat_read_user_io(us, status);
1522 if (rc != USB_STOR_XFER_GOOD)
1523 return USB_STOR_TRANSPORT_ERROR;
1524
Joe Perches191648d2013-04-19 11:44:00 -07001525 usb_stor_dbg(us, "INIT 7\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 msleep(1400);
1528
1529 rc = usbat_read_user_io(us, status);
1530 if (rc != USB_STOR_XFER_GOOD)
1531 return USB_STOR_TRANSPORT_ERROR;
1532
Joe Perches191648d2013-04-19 11:44:00 -07001533 usb_stor_dbg(us, "INIT 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 rc = usbat_select_and_test_registers(us);
1536 if (rc != USB_STOR_TRANSPORT_GOOD)
1537 return rc;
1538
Joe Perches191648d2013-04-19 11:44:00 -07001539 usb_stor_dbg(us, "INIT 9\n");
Daniel Drake68a64572005-08-10 18:30:04 +01001540
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001541 /* At this point, we need to detect which device we are using */
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001542 if (usbat_set_transport(us, info, devicetype))
Daniel Drake68a64572005-08-10 18:30:04 +01001543 return USB_STOR_TRANSPORT_ERROR;
1544
Joe Perches191648d2013-04-19 11:44:00 -07001545 usb_stor_dbg(us, "INIT 10\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 if (usbat_get_device_type(us) == USBAT_DEV_FLASH) {
1548 subcountH = 0x02;
1549 subcountL = 0x00;
1550 }
1551 rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1552 0x00, 0x88, 0x08, subcountH, subcountL);
1553 if (rc != USB_STOR_XFER_GOOD)
1554 return USB_STOR_TRANSPORT_ERROR;
1555
Joe Perches191648d2013-04-19 11:44:00 -07001556 usb_stor_dbg(us, "INIT 11\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 return USB_STOR_TRANSPORT_GOOD;
1559}
1560
1561/*
1562 * Transport for the HP 8200e
1563 */
1564static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1565{
1566 int result;
1567 unsigned char *status = us->iobuf;
1568 unsigned char registers[32];
1569 unsigned char data[32];
1570 unsigned int len;
1571 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Boaz Harrosh4776e992007-09-09 20:40:56 +03001573 len = scsi_bufflen(srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Felipe Balbif0183a32016-04-18 13:09:11 +03001575 /*
1576 * Send A0 (ATA PACKET COMMAND).
1577 * Note: I guess we're never going to get any of the ATA
1578 * commands... just ATA Packet Commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 */
1580
1581 registers[0] = USBAT_ATA_FEATURES;
1582 registers[1] = USBAT_ATA_SECCNT;
1583 registers[2] = USBAT_ATA_SECNUM;
1584 registers[3] = USBAT_ATA_LBA_ME;
1585 registers[4] = USBAT_ATA_LBA_HI;
1586 registers[5] = USBAT_ATA_DEVICE;
1587 registers[6] = USBAT_ATA_CMD;
1588 data[0] = 0x00;
1589 data[1] = 0x00;
1590 data[2] = 0x00;
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001591 data[3] = len&0xFF; /* (cylL) = expected length (L) */
1592 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
1593 data[5] = 0xB0; /* (device sel) = slave */
1594 data[6] = 0xA0; /* (command) = ATA PACKET COMMAND */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 for (i=7; i<19; i++) {
1597 registers[i] = 0x10;
1598 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1599 }
1600
1601 result = usbat_get_status(us, status);
Joe Perches191648d2013-04-19 11:44:00 -07001602 usb_stor_dbg(us, "Status = %02X\n", *status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (result != USB_STOR_XFER_GOOD)
1604 return USB_STOR_TRANSPORT_ERROR;
1605 if (srb->cmnd[0] == TEST_UNIT_READY)
1606 transferred = 0;
1607
1608 if (srb->sc_data_direction == DMA_TO_DEVICE) {
1609
1610 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1611 registers, data, 19,
1612 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1613 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1614 DMA_TO_DEVICE,
Boaz Harrosh4776e992007-09-09 20:40:56 +03001615 scsi_sglist(srb),
1616 len, scsi_sg_count(srb), 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 if (result == USB_STOR_TRANSPORT_GOOD) {
1619 transferred += len;
Joe Perches191648d2013-04-19 11:44:00 -07001620 usb_stor_dbg(us, "Wrote %08X bytes\n", transferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
1622
1623 return result;
1624
1625 } else if (srb->cmnd[0] == READ_10 ||
1626 srb->cmnd[0] == GPCMD_READ_CD) {
1627
1628 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1629
1630 }
1631
1632 if (len > 0xFFFF) {
Joe Perches191648d2013-04-19 11:44:00 -07001633 usb_stor_dbg(us, "Error: len = %08X... what do I do now?\n",
1634 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return USB_STOR_TRANSPORT_ERROR;
1636 }
1637
Roel Kluin5d3034a2010-02-17 11:50:26 +01001638 result = usbat_multiple_write(us, registers, data, 7);
1639
1640 if (result != USB_STOR_TRANSPORT_GOOD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001643 /*
1644 * Write the 12-byte command header.
1645 *
1646 * If the command is BLANK then set the timer for 75 minutes.
1647 * Otherwise set it for 10 minutes.
1648 *
1649 * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1650 * AT SPEED 4 IS UNRELIABLE!!!
1651 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Roel Kluin5d3034a2010-02-17 11:50:26 +01001653 result = usbat_write_block(us, USBAT_ATA, srb->cmnd, 12,
1654 srb->cmnd[0] == GPCMD_BLANK ? 75 : 10, 0);
1655
1656 if (result != USB_STOR_TRANSPORT_GOOD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001659 /* If there is response data to be read in then do it here. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1662
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001663 /* How many bytes to read in? Check cylL register */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1666 USB_STOR_XFER_GOOD) {
1667 return USB_STOR_TRANSPORT_ERROR;
1668 }
1669
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001670 if (len > 0xFF) { /* need to read cylH also */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 len = *status;
1672 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1673 USB_STOR_XFER_GOOD) {
1674 return USB_STOR_TRANSPORT_ERROR;
1675 }
1676 len += ((unsigned int) *status)<<8;
1677 }
1678 else
1679 len = *status;
1680
1681
Boaz Harrosh4776e992007-09-09 20:40:56 +03001682 result = usbat_read_block(us, scsi_sglist(srb), len,
1683 scsi_sg_count(srb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 }
1685
1686 return result;
1687}
1688
1689/*
1690 * Transport for USBAT02-based CompactFlash and similar storage devices
1691 */
1692static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1693{
1694 int rc;
1695 struct usbat_info *info = (struct usbat_info *) (us->extra);
1696 unsigned long block, blocks;
1697 unsigned char *ptr = us->iobuf;
1698 static unsigned char inquiry_response[36] = {
1699 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1700 };
1701
1702 if (srb->cmnd[0] == INQUIRY) {
Joe Perches191648d2013-04-19 11:44:00 -07001703 usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1705 fill_inquiry_response(us, ptr, 36);
1706 return USB_STOR_TRANSPORT_GOOD;
1707 }
1708
1709 if (srb->cmnd[0] == READ_CAPACITY) {
1710 rc = usbat_flash_check_media(us, info);
1711 if (rc != USB_STOR_TRANSPORT_GOOD)
1712 return rc;
1713
1714 rc = usbat_flash_get_sector_count(us, info);
1715 if (rc != USB_STOR_TRANSPORT_GOOD)
1716 return rc;
1717
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001718 /* hard coded 512 byte sectors as per ATA spec */
1719 info->ssize = 0x200;
Joe Perches191648d2013-04-19 11:44:00 -07001720 usb_stor_dbg(us, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1721 info->sectors, info->ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001723 /*
1724 * build the reply
1725 * note: must return the sector number of the last sector,
1726 * *not* the total number of sectors
1727 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1729 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1730 usb_stor_set_xfer_buf(ptr, 8, srb);
1731
1732 return USB_STOR_TRANSPORT_GOOD;
1733 }
1734
1735 if (srb->cmnd[0] == MODE_SELECT_10) {
Joe Perches191648d2013-04-19 11:44:00 -07001736 usb_stor_dbg(us, "Gah! MODE_SELECT_10\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return USB_STOR_TRANSPORT_ERROR;
1738 }
1739
1740 if (srb->cmnd[0] == READ_10) {
1741 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1742 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1743
1744 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1745
Joe Perches191648d2013-04-19 11:44:00 -07001746 usb_stor_dbg(us, "READ_10: read block 0x%04lx count %ld\n",
1747 block, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 return usbat_flash_read_data(us, info, block, blocks);
1749 }
1750
1751 if (srb->cmnd[0] == READ_12) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001752 /*
1753 * I don't think we'll ever see a READ_12 but support it anyway
1754 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1756 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1757
1758 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1759 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1760
Joe Perches191648d2013-04-19 11:44:00 -07001761 usb_stor_dbg(us, "READ_12: read block 0x%04lx count %ld\n",
1762 block, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return usbat_flash_read_data(us, info, block, blocks);
1764 }
1765
1766 if (srb->cmnd[0] == WRITE_10) {
1767 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1768 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1769
1770 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1771
Joe Perches191648d2013-04-19 11:44:00 -07001772 usb_stor_dbg(us, "WRITE_10: write block 0x%04lx count %ld\n",
1773 block, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return usbat_flash_write_data(us, info, block, blocks);
1775 }
1776
1777 if (srb->cmnd[0] == WRITE_12) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001778 /*
1779 * I don't think we'll ever see a WRITE_12 but support it anyway
1780 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1782 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1783
1784 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1785 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1786
Joe Perches191648d2013-04-19 11:44:00 -07001787 usb_stor_dbg(us, "WRITE_12: write block 0x%04lx count %ld\n",
1788 block, blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return usbat_flash_write_data(us, info, block, blocks);
1790 }
1791
1792
1793 if (srb->cmnd[0] == TEST_UNIT_READY) {
Joe Perches191648d2013-04-19 11:44:00 -07001794 usb_stor_dbg(us, "TEST_UNIT_READY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 rc = usbat_flash_check_media(us, info);
1797 if (rc != USB_STOR_TRANSPORT_GOOD)
1798 return rc;
1799
1800 return usbat_check_status(us);
1801 }
1802
1803 if (srb->cmnd[0] == REQUEST_SENSE) {
Joe Perches191648d2013-04-19 11:44:00 -07001804 usb_stor_dbg(us, "REQUEST_SENSE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 memset(ptr, 0, 18);
1807 ptr[0] = 0xF0;
1808 ptr[2] = info->sense_key;
1809 ptr[7] = 11;
1810 ptr[12] = info->sense_asc;
1811 ptr[13] = info->sense_ascq;
1812 usb_stor_set_xfer_buf(ptr, 18, srb);
1813
1814 return USB_STOR_TRANSPORT_GOOD;
1815 }
1816
1817 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
Daniel Drakeb7b1e652005-09-30 12:49:36 +01001818 /*
1819 * sure. whatever. not like we can stop the user from popping
1820 * the media out of the device (no locking doors, etc)
1821 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return USB_STOR_TRANSPORT_GOOD;
1823 }
1824
Joe Perches191648d2013-04-19 11:44:00 -07001825 usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
1826 srb->cmnd[0], srb->cmnd[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 info->sense_key = 0x05;
1828 info->sense_asc = 0x20;
1829 info->sense_ascq = 0x00;
1830 return USB_STOR_TRANSPORT_FAILED;
1831}
1832
Alan Stern26d68182009-02-12 14:48:08 -05001833static int init_usbat_cd(struct us_data *us)
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001834{
1835 return init_usbat(us, USBAT_DEV_HP8200);
1836}
1837
Alan Stern26d68182009-02-12 14:48:08 -05001838static int init_usbat_flash(struct us_data *us)
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001839{
1840 return init_usbat(us, USBAT_DEV_FLASH);
1841}
1842
Akinobu Mitaaa519be2015-05-06 18:24:21 +09001843static struct scsi_host_template usbat_host_template;
1844
Alan Stern26d68182009-02-12 14:48:08 -05001845static int usbat_probe(struct usb_interface *intf,
1846 const struct usb_device_id *id)
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001847{
Alan Stern26d68182009-02-12 14:48:08 -05001848 struct us_data *us;
1849 int result;
1850
1851 result = usb_stor_probe1(&us, intf, id,
Akinobu Mitaaa519be2015-05-06 18:24:21 +09001852 (id - usbat_usb_ids) + usbat_unusual_dev_list,
1853 &usbat_host_template);
Alan Stern26d68182009-02-12 14:48:08 -05001854 if (result)
1855 return result;
1856
Felipe Balbif0183a32016-04-18 13:09:11 +03001857 /*
1858 * The actual transport will be determined later by the
Alan Stern26d68182009-02-12 14:48:08 -05001859 * initialization routine; this is just a placeholder.
1860 */
1861 us->transport_name = "Shuttle USBAT";
1862 us->transport = usbat_flash_transport;
1863 us->transport_reset = usb_stor_CB_reset;
Daniele Forsidf602c22014-04-29 11:44:03 +02001864 us->max_lun = 0;
Alan Stern26d68182009-02-12 14:48:08 -05001865
1866 result = usb_stor_probe2(us);
1867 return result;
Peter Chubbbdcfd9e2006-05-02 18:29:34 +01001868}
1869
Alan Stern26d68182009-02-12 14:48:08 -05001870static struct usb_driver usbat_driver = {
Akinobu Mitaaa519be2015-05-06 18:24:21 +09001871 .name = DRV_NAME,
Alan Stern26d68182009-02-12 14:48:08 -05001872 .probe = usbat_probe,
1873 .disconnect = usb_stor_disconnect,
1874 .suspend = usb_stor_suspend,
1875 .resume = usb_stor_resume,
1876 .reset_resume = usb_stor_reset_resume,
1877 .pre_reset = usb_stor_pre_reset,
1878 .post_reset = usb_stor_post_reset,
1879 .id_table = usbat_usb_ids,
1880 .soft_unbind = 1,
Huajun Lie73b2db2012-01-14 10:15:21 +08001881 .no_dynamic_id = 1,
Alan Stern26d68182009-02-12 14:48:08 -05001882};
1883
Akinobu Mitaaa519be2015-05-06 18:24:21 +09001884module_usb_stor_driver(usbat_driver, usbat_host_template, DRV_NAME);