Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1 | /* Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/errno.h> |
Alan Stern | 26d6818 | 2009-02-12 14:48:08 -0500 | [diff] [blame] | 45 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #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 Stern | 26d6818 | 2009-02-12 14:48:08 -0500 | [diff] [blame] | 56 | |
Maciej Grela | 4246b06 | 2009-02-28 12:39:20 -0800 | [diff] [blame] | 57 | MODULE_DESCRIPTION("Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable"); |
| 58 | MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>, Robert Baruch <autophile@starband.net>"); |
| 59 | MODULE_LICENSE("GPL"); |
Alan Stern | 26d6818 | 2009-02-12 14:48:08 -0500 | [diff] [blame] | 60 | |
| 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 | |
| 139 | struct 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
| 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 | |
| 155 | static int transferred = 0; |
| 156 | |
| 157 | static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us); |
| 158 | static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us); |
| 159 | |
Alan Stern | 26d6818 | 2009-02-12 14:48:08 -0500 | [diff] [blame] | 160 | static int init_usbat_cd(struct us_data *us); |
| 161 | static 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), \ |
| 171 | .driver_info = (flags)|(USB_US_TYPE_STOR<<24) } |
| 172 | |
| 173 | struct usb_device_id usbat_usb_ids[] = { |
| 174 | # include "unusual_usbat.h" |
| 175 | { } /* Terminating entry */ |
| 176 | }; |
| 177 | MODULE_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 | |
| 195 | static struct us_unusual_dev usbat_unusual_dev_list[] = { |
| 196 | # include "unusual_usbat.h" |
| 197 | { } /* Terminating entry */ |
| 198 | }; |
| 199 | |
| 200 | #undef UNUSUAL_DEV |
| 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | /* |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 203 | * Convenience function to produce an ATA read/write sectors command |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | * Use cmd=0x20 for read, cmd=0x30 for write |
| 205 | */ |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 206 | static void usbat_pack_ata_sector_cmd(unsigned char *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | 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 | */ |
| 222 | static 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 | */ |
| 230 | static 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 | */ |
| 248 | static 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 | */ |
| 266 | static int usbat_bulk_read(struct us_data *us, |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 267 | void* buf, |
Peter Chubb | 141804d | 2006-05-02 18:30:12 +0100 | [diff] [blame] | 268 | unsigned int len, |
| 269 | int use_sg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
| 271 | if (len == 0) |
| 272 | return USB_STOR_XFER_GOOD; |
| 273 | |
| 274 | US_DEBUGP("usbat_bulk_read: len = %d\n", len); |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 275 | return usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe, buf, len, use_sg, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Convenience function to perform a bulk write |
| 280 | */ |
| 281 | static int usbat_bulk_write(struct us_data *us, |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 282 | void* buf, |
Peter Chubb | 141804d | 2006-05-02 18:30:12 +0100 | [diff] [blame] | 283 | unsigned int len, |
| 284 | int use_sg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
| 286 | if (len == 0) |
| 287 | return USB_STOR_XFER_GOOD; |
| 288 | |
| 289 | US_DEBUGP("usbat_bulk_write: len = %d\n", len); |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 290 | return usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe, buf, len, use_sg, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 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 | */ |
| 298 | static 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 | */ |
| 310 | static 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 | |
| 315 | US_DEBUGP("usbat_get_status: 0x%02X\n", (unsigned short) (*status)); |
| 316 | return rc; |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Check the device status |
| 321 | */ |
| 322 | static int usbat_check_status(struct us_data *us) |
| 323 | { |
| 324 | unsigned char *reply = us->iobuf; |
| 325 | int rc; |
| 326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | rc = usbat_get_status(us, reply); |
| 328 | if (rc != USB_STOR_XFER_GOOD) |
| 329 | return USB_STOR_TRANSPORT_FAILED; |
| 330 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 331 | /* error/check condition (0x51 is ok) */ |
| 332 | if (*reply & 0x01 && *reply != 0x51) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | return USB_STOR_TRANSPORT_FAILED; |
| 334 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 335 | /* device fault */ |
| 336 | if (*reply & 0x20) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | return USB_STOR_TRANSPORT_FAILED; |
| 338 | |
| 339 | return USB_STOR_TRANSPORT_GOOD; |
| 340 | } |
| 341 | |
| 342 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 343 | * Stores critical information in internal registers in preparation for the execution |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | * of a conditional usbat_read_blocks or usbat_write_blocks call. |
| 345 | */ |
| 346 | static 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 Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 359 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | command[2] = epp_control; |
| 364 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 365 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | command[3] = external_trigger; |
| 372 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 373 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | command[4] = test_pattern; |
| 379 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 380 | /* |
| 381 | * This value is logically ANDed with the status register field specified |
| 382 | * in the read/write command. |
| 383 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | command[5] = mask_byte; |
| 385 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 386 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | 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 | */ |
| 402 | static 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 Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 420 | if (*status & 0x01) { /* check condition */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | result = usbat_read(us, USBAT_ATA, 0x10, status); |
| 422 | return USB_STOR_TRANSPORT_FAILED; |
| 423 | } |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 424 | if (*status & 0x20) /* device fault */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | return USB_STOR_TRANSPORT_FAILED; |
| 426 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 427 | if ((*status & 0x80)==0x00) { /* not busy */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | US_DEBUGP("Waited not busy for %d steps\n", i); |
| 429 | return USB_STOR_TRANSPORT_GOOD; |
| 430 | } |
| 431 | |
| 432 | if (i<500) |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 433 | msleep(10); /* 5 seconds */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | else if (i<700) |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 435 | msleep(50); /* 10 seconds */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | else if (i<1200) |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 437 | msleep(100); /* 50 seconds */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | else |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 439 | msleep(1000); /* X minutes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | US_DEBUGP("Waited not busy for %d minutes, timing out.\n", |
| 443 | minutes); |
| 444 | return USB_STOR_TRANSPORT_FAILED; |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * Read block data from the data register |
| 449 | */ |
| 450 | static int usbat_read_block(struct us_data *us, |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 451 | void* buf, |
Peter Chubb | 141804d | 2006-05-02 18:30:12 +0100 | [diff] [blame] | 452 | unsigned short len, |
| 453 | int use_sg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | { |
| 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 Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 474 | result = usbat_bulk_read(us, buf, len, use_sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | 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 | */ |
| 482 | static int usbat_write_block(struct us_data *us, |
| 483 | unsigned char access, |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 484 | void* buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | unsigned short len, |
Peter Chubb | 141804d | 2006-05-02 18:30:12 +0100 | [diff] [blame] | 486 | int minutes, |
| 487 | int use_sg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { |
| 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 Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 509 | result = usbat_bulk_write(us, buf, len, use_sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | 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 | */ |
| 519 | static 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 Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 529 | void *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | 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 Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 561 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | 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 Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 578 | /* Conditionally read or write blocks */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | 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 Chubb | 141804d | 2006-05-02 18:30:12 +0100 | [diff] [blame] | 602 | result = usbat_bulk_write(us, data, num_registers*2, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | if (result != USB_STOR_XFER_GOOD) |
| 604 | return USB_STOR_TRANSPORT_ERROR; |
| 605 | |
| 606 | } |
| 607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | result = usb_stor_bulk_transfer_sg(us, |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 609 | pipe, buf, len, use_sg, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
| 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 Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 655 | if (*status & 0x01) /* check condition */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | return USB_STOR_TRANSPORT_FAILED; |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 657 | if (*status & 0x20) /* device fault */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | return USB_STOR_TRANSPORT_FAILED; |
| 659 | |
| 660 | US_DEBUGP("Redoing %s\n", |
| 661 | direction==DMA_TO_DEVICE ? "write" : "read"); |
| 662 | |
| 663 | } else if (result != USB_STOR_XFER_GOOD) |
| 664 | return USB_STOR_TRANSPORT_ERROR; |
| 665 | else |
| 666 | return usbat_wait_not_busy(us, minutes); |
| 667 | |
| 668 | } |
| 669 | |
| 670 | US_DEBUGP("Bummer! %s bulk data 20 times failed.\n", |
| 671 | direction==DMA_TO_DEVICE ? "Writing" : "Reading"); |
| 672 | |
| 673 | return USB_STOR_TRANSPORT_FAILED; |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | * Write to multiple registers: |
| 678 | * Allows us to write specific data to any registers. The data to be written |
| 679 | * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN |
| 680 | * which gets sent through bulk out. |
| 681 | * Not designed for large transfers of data! |
| 682 | */ |
| 683 | static int usbat_multiple_write(struct us_data *us, |
| 684 | unsigned char *registers, |
| 685 | unsigned char *data_out, |
| 686 | unsigned short num_registers) |
| 687 | { |
| 688 | int i, result; |
| 689 | unsigned char *data = us->iobuf; |
| 690 | unsigned char *command = us->iobuf; |
| 691 | |
| 692 | BUG_ON(num_registers > US_IOBUF_SIZE/2); |
| 693 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 694 | /* Write to multiple registers, ATA access */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | command[0] = 0x40; |
| 696 | command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS; |
| 697 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 698 | /* No relevance */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | command[2] = 0; |
| 700 | command[3] = 0; |
| 701 | command[4] = 0; |
| 702 | command[5] = 0; |
| 703 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 704 | /* Number of bytes to be transferred (incl. addresses and data) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | command[6] = LSB_of(num_registers*2); |
| 706 | command[7] = MSB_of(num_registers*2); |
| 707 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 708 | /* The setup command */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | result = usbat_execute_command(us, command, 8); |
| 710 | if (result != USB_STOR_XFER_GOOD) |
| 711 | return USB_STOR_TRANSPORT_ERROR; |
| 712 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 713 | /* Create the reg/data, reg/data sequence */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | for (i=0; i<num_registers; i++) { |
| 715 | data[i<<1] = registers[i]; |
| 716 | data[1+(i<<1)] = data_out[i]; |
| 717 | } |
| 718 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 719 | /* Send the data */ |
Peter Chubb | 141804d | 2006-05-02 18:30:12 +0100 | [diff] [blame] | 720 | result = usbat_bulk_write(us, data, num_registers*2, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | if (result != USB_STOR_XFER_GOOD) |
| 722 | return USB_STOR_TRANSPORT_ERROR; |
| 723 | |
| 724 | if (usbat_get_device_type(us) == USBAT_DEV_HP8200) |
| 725 | return usbat_wait_not_busy(us, 0); |
| 726 | else |
| 727 | return USB_STOR_TRANSPORT_GOOD; |
| 728 | } |
| 729 | |
| 730 | /* |
| 731 | * Conditionally read blocks from device: |
| 732 | * Allows us to read blocks from a specific data register, based upon the |
| 733 | * condition that a status register can be successfully masked with a status |
| 734 | * qualifier. If this condition is not initially met, the read will wait |
| 735 | * up until a maximum amount of time has elapsed, as specified by timeout. |
| 736 | * The read will start when the condition is met, otherwise the command aborts. |
| 737 | * |
| 738 | * The qualifier defined here is not the value that is masked, it defines |
| 739 | * conditions for the write to take place. The actual masked qualifier (and |
| 740 | * other related details) are defined beforehand with _set_shuttle_features(). |
| 741 | */ |
| 742 | static int usbat_read_blocks(struct us_data *us, |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 743 | void* buffer, |
Peter Chubb | 141804d | 2006-05-02 18:30:12 +0100 | [diff] [blame] | 744 | int len, |
| 745 | int use_sg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | { |
| 747 | int result; |
| 748 | unsigned char *command = us->iobuf; |
| 749 | |
| 750 | command[0] = 0xC0; |
| 751 | command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK; |
| 752 | command[2] = USBAT_ATA_DATA; |
| 753 | command[3] = USBAT_ATA_STATUS; |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 754 | command[4] = 0xFD; /* Timeout (ms); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | command[5] = USBAT_QUAL_FCQ; |
| 756 | command[6] = LSB_of(len); |
| 757 | command[7] = MSB_of(len); |
| 758 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 759 | /* Multiple block read setup command */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | result = usbat_execute_command(us, command, 8); |
| 761 | if (result != USB_STOR_XFER_GOOD) |
| 762 | return USB_STOR_TRANSPORT_FAILED; |
| 763 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 764 | /* Read the blocks we just asked for */ |
Peter Chubb | 141804d | 2006-05-02 18:30:12 +0100 | [diff] [blame] | 765 | result = usbat_bulk_read(us, buffer, len, use_sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | if (result != USB_STOR_XFER_GOOD) |
| 767 | return USB_STOR_TRANSPORT_FAILED; |
| 768 | |
| 769 | return USB_STOR_TRANSPORT_GOOD; |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | * Conditionally write blocks to device: |
| 774 | * Allows us to write blocks to a specific data register, based upon the |
| 775 | * condition that a status register can be successfully masked with a status |
| 776 | * qualifier. If this condition is not initially met, the write will wait |
| 777 | * up until a maximum amount of time has elapsed, as specified by timeout. |
| 778 | * The read will start when the condition is met, otherwise the command aborts. |
| 779 | * |
| 780 | * The qualifier defined here is not the value that is masked, it defines |
| 781 | * conditions for the write to take place. The actual masked qualifier (and |
| 782 | * other related details) are defined beforehand with _set_shuttle_features(). |
| 783 | */ |
| 784 | static int usbat_write_blocks(struct us_data *us, |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 785 | void* buffer, |
Peter Chubb | 141804d | 2006-05-02 18:30:12 +0100 | [diff] [blame] | 786 | int len, |
| 787 | int use_sg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | { |
| 789 | int result; |
| 790 | unsigned char *command = us->iobuf; |
| 791 | |
| 792 | command[0] = 0x40; |
| 793 | command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK; |
| 794 | command[2] = USBAT_ATA_DATA; |
| 795 | command[3] = USBAT_ATA_STATUS; |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 796 | command[4] = 0xFD; /* Timeout (ms) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | command[5] = USBAT_QUAL_FCQ; |
| 798 | command[6] = LSB_of(len); |
| 799 | command[7] = MSB_of(len); |
| 800 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 801 | /* Multiple block write setup command */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | result = usbat_execute_command(us, command, 8); |
| 803 | if (result != USB_STOR_XFER_GOOD) |
| 804 | return USB_STOR_TRANSPORT_FAILED; |
| 805 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 806 | /* Write the data */ |
Peter Chubb | 141804d | 2006-05-02 18:30:12 +0100 | [diff] [blame] | 807 | result = usbat_bulk_write(us, buffer, len, use_sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | if (result != USB_STOR_XFER_GOOD) |
| 809 | return USB_STOR_TRANSPORT_FAILED; |
| 810 | |
| 811 | return USB_STOR_TRANSPORT_GOOD; |
| 812 | } |
| 813 | |
| 814 | /* |
| 815 | * Read the User IO register |
| 816 | */ |
| 817 | static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags) |
| 818 | { |
| 819 | int result; |
| 820 | |
| 821 | result = usb_stor_ctrl_transfer(us, |
| 822 | us->recv_ctrl_pipe, |
| 823 | USBAT_CMD_UIO, |
| 824 | 0xC0, |
| 825 | 0, |
| 826 | 0, |
| 827 | data_flags, |
| 828 | USBAT_UIO_READ); |
| 829 | |
| 830 | US_DEBUGP("usbat_read_user_io: UIO register reads %02X\n", (unsigned short) (*data_flags)); |
| 831 | |
| 832 | return result; |
| 833 | } |
| 834 | |
| 835 | /* |
| 836 | * Write to the User IO register |
| 837 | */ |
| 838 | static int usbat_write_user_io(struct us_data *us, |
| 839 | unsigned char enable_flags, |
| 840 | unsigned char data_flags) |
| 841 | { |
| 842 | return usb_stor_ctrl_transfer(us, |
| 843 | us->send_ctrl_pipe, |
| 844 | USBAT_CMD_UIO, |
| 845 | 0x40, |
| 846 | short_pack(enable_flags, data_flags), |
| 847 | 0, |
| 848 | NULL, |
| 849 | USBAT_UIO_WRITE); |
| 850 | } |
| 851 | |
| 852 | /* |
| 853 | * Reset the device |
| 854 | * Often needed on media change. |
| 855 | */ |
| 856 | static int usbat_device_reset(struct us_data *us) |
| 857 | { |
| 858 | int rc; |
| 859 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 860 | /* |
| 861 | * Reset peripheral, enable peripheral control signals |
| 862 | * (bring reset signal up) |
| 863 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | rc = usbat_write_user_io(us, |
| 865 | USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0, |
| 866 | USBAT_UIO_EPAD | USBAT_UIO_1); |
| 867 | if (rc != USB_STOR_XFER_GOOD) |
| 868 | return USB_STOR_TRANSPORT_ERROR; |
| 869 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 870 | /* |
| 871 | * Enable peripheral control signals |
| 872 | * (bring reset signal down) |
| 873 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | rc = usbat_write_user_io(us, |
| 875 | USBAT_UIO_OE1 | USBAT_UIO_OE0, |
| 876 | USBAT_UIO_EPAD | USBAT_UIO_1); |
| 877 | if (rc != USB_STOR_XFER_GOOD) |
| 878 | return USB_STOR_TRANSPORT_ERROR; |
| 879 | |
| 880 | return USB_STOR_TRANSPORT_GOOD; |
| 881 | } |
| 882 | |
| 883 | /* |
| 884 | * Enable card detect |
| 885 | */ |
| 886 | static int usbat_device_enable_cdt(struct us_data *us) |
| 887 | { |
| 888 | int rc; |
| 889 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 890 | /* Enable peripheral control signals and card detect */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | rc = usbat_write_user_io(us, |
| 892 | USBAT_UIO_ACKD | USBAT_UIO_OE1 | USBAT_UIO_OE0, |
| 893 | USBAT_UIO_EPAD | USBAT_UIO_1); |
| 894 | if (rc != USB_STOR_XFER_GOOD) |
| 895 | return USB_STOR_TRANSPORT_ERROR; |
| 896 | |
| 897 | return USB_STOR_TRANSPORT_GOOD; |
| 898 | } |
| 899 | |
| 900 | /* |
| 901 | * Determine if media is present. |
| 902 | */ |
| 903 | static int usbat_flash_check_media_present(unsigned char *uio) |
| 904 | { |
| 905 | if (*uio & USBAT_UIO_UI0) { |
| 906 | US_DEBUGP("usbat_flash_check_media_present: no media detected\n"); |
| 907 | return USBAT_FLASH_MEDIA_NONE; |
| 908 | } |
| 909 | |
| 910 | return USBAT_FLASH_MEDIA_CF; |
| 911 | } |
| 912 | |
| 913 | /* |
| 914 | * Determine if media has changed since last operation |
| 915 | */ |
| 916 | static int usbat_flash_check_media_changed(unsigned char *uio) |
| 917 | { |
| 918 | if (*uio & USBAT_UIO_0) { |
| 919 | US_DEBUGP("usbat_flash_check_media_changed: media change detected\n"); |
| 920 | return USBAT_FLASH_MEDIA_CHANGED; |
| 921 | } |
| 922 | |
| 923 | return USBAT_FLASH_MEDIA_SAME; |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * Check for media change / no media and handle the situation appropriately |
| 928 | */ |
| 929 | static int usbat_flash_check_media(struct us_data *us, |
| 930 | struct usbat_info *info) |
| 931 | { |
| 932 | int rc; |
| 933 | unsigned char *uio = us->iobuf; |
| 934 | |
| 935 | rc = usbat_read_user_io(us, uio); |
| 936 | if (rc != USB_STOR_XFER_GOOD) |
| 937 | return USB_STOR_TRANSPORT_ERROR; |
| 938 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 939 | /* Check for media existence */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | rc = usbat_flash_check_media_present(uio); |
| 941 | if (rc == USBAT_FLASH_MEDIA_NONE) { |
| 942 | info->sense_key = 0x02; |
| 943 | info->sense_asc = 0x3A; |
| 944 | info->sense_ascq = 0x00; |
| 945 | return USB_STOR_TRANSPORT_FAILED; |
| 946 | } |
| 947 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 948 | /* Check for media change */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | rc = usbat_flash_check_media_changed(uio); |
| 950 | if (rc == USBAT_FLASH_MEDIA_CHANGED) { |
| 951 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 952 | /* Reset and re-enable card detect */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | rc = usbat_device_reset(us); |
| 954 | if (rc != USB_STOR_TRANSPORT_GOOD) |
| 955 | return rc; |
| 956 | rc = usbat_device_enable_cdt(us); |
| 957 | if (rc != USB_STOR_TRANSPORT_GOOD) |
| 958 | return rc; |
| 959 | |
| 960 | msleep(50); |
| 961 | |
| 962 | rc = usbat_read_user_io(us, uio); |
| 963 | if (rc != USB_STOR_XFER_GOOD) |
| 964 | return USB_STOR_TRANSPORT_ERROR; |
| 965 | |
| 966 | info->sense_key = UNIT_ATTENTION; |
| 967 | info->sense_asc = 0x28; |
| 968 | info->sense_ascq = 0x00; |
| 969 | return USB_STOR_TRANSPORT_FAILED; |
| 970 | } |
| 971 | |
| 972 | return USB_STOR_TRANSPORT_GOOD; |
| 973 | } |
| 974 | |
| 975 | /* |
| 976 | * Determine whether we are controlling a flash-based reader/writer, |
| 977 | * or a HP8200-based CD drive. |
| 978 | * Sets transport functions as appropriate. |
| 979 | */ |
| 980 | static int usbat_identify_device(struct us_data *us, |
| 981 | struct usbat_info *info) |
| 982 | { |
| 983 | int rc; |
| 984 | unsigned char status; |
| 985 | |
| 986 | if (!us || !info) |
| 987 | return USB_STOR_TRANSPORT_ERROR; |
| 988 | |
| 989 | rc = usbat_device_reset(us); |
| 990 | if (rc != USB_STOR_TRANSPORT_GOOD) |
| 991 | return rc; |
Daniel Drake | 8845add | 2005-11-17 09:48:01 -0800 | [diff] [blame] | 992 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | |
| 994 | /* |
Daniel Drake | 68a6457 | 2005-08-10 18:30:04 +0100 | [diff] [blame] | 995 | * In attempt to distinguish between HP CDRW's and Flash readers, we now |
| 996 | * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash |
| 997 | * readers), this command should fail with error. On ATAPI devices (i.e. |
| 998 | * CDROM drives), it should succeed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | */ |
Daniel Drake | 68a6457 | 2005-08-10 18:30:04 +0100 | [diff] [blame] | 1000 | rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1); |
| 1001 | if (rc != USB_STOR_XFER_GOOD) |
| 1002 | return USB_STOR_TRANSPORT_ERROR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | |
Daniel Drake | 68a6457 | 2005-08-10 18:30:04 +0100 | [diff] [blame] | 1004 | rc = usbat_get_status(us, &status); |
| 1005 | if (rc != USB_STOR_XFER_GOOD) |
| 1006 | return USB_STOR_TRANSPORT_ERROR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1008 | /* Check for error bit, or if the command 'fell through' */ |
Daniel Drake | a879853 | 2005-09-29 00:14:21 +0100 | [diff] [blame] | 1009 | if (status == 0xA1 || !(status & 0x01)) { |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1010 | /* Device is HP 8200 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | US_DEBUGP("usbat_identify_device: Detected HP8200 CDRW\n"); |
| 1012 | info->devicetype = USBAT_DEV_HP8200; |
Daniel Drake | a879853 | 2005-09-29 00:14:21 +0100 | [diff] [blame] | 1013 | } else { |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1014 | /* Device is a CompactFlash reader/writer */ |
Daniel Drake | a879853 | 2005-09-29 00:14:21 +0100 | [diff] [blame] | 1015 | US_DEBUGP("usbat_identify_device: Detected Flash reader/writer\n"); |
| 1016 | info->devicetype = USBAT_DEV_FLASH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | return USB_STOR_TRANSPORT_GOOD; |
| 1020 | } |
| 1021 | |
| 1022 | /* |
| 1023 | * Set the transport function based on the device type |
| 1024 | */ |
| 1025 | static int usbat_set_transport(struct us_data *us, |
Peter Chubb | bdcfd9e | 2006-05-02 18:29:34 +0100 | [diff] [blame] | 1026 | struct usbat_info *info, |
| 1027 | int devicetype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | |
Peter Chubb | bdcfd9e | 2006-05-02 18:29:34 +0100 | [diff] [blame] | 1030 | if (!info->devicetype) |
| 1031 | info->devicetype = devicetype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | |
Peter Chubb | bdcfd9e | 2006-05-02 18:29:34 +0100 | [diff] [blame] | 1033 | if (!info->devicetype) |
| 1034 | usbat_identify_device(us, info); |
| 1035 | |
| 1036 | switch (info->devicetype) { |
| 1037 | default: |
| 1038 | return USB_STOR_TRANSPORT_ERROR; |
| 1039 | |
| 1040 | case USBAT_DEV_HP8200: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | us->transport = usbat_hp8200e_transport; |
Peter Chubb | bdcfd9e | 2006-05-02 18:29:34 +0100 | [diff] [blame] | 1042 | break; |
| 1043 | |
| 1044 | case USBAT_DEV_FLASH: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | us->transport = usbat_flash_transport; |
Peter Chubb | bdcfd9e | 2006-05-02 18:29:34 +0100 | [diff] [blame] | 1046 | break; |
| 1047 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | /* |
| 1053 | * Read the media capacity |
| 1054 | */ |
| 1055 | static int usbat_flash_get_sector_count(struct us_data *us, |
| 1056 | struct usbat_info *info) |
| 1057 | { |
| 1058 | unsigned char registers[3] = { |
| 1059 | USBAT_ATA_SECCNT, |
| 1060 | USBAT_ATA_DEVICE, |
| 1061 | USBAT_ATA_CMD, |
| 1062 | }; |
| 1063 | unsigned char command[3] = { 0x01, 0xA0, 0xEC }; |
| 1064 | unsigned char *reply; |
| 1065 | unsigned char status; |
| 1066 | int rc; |
| 1067 | |
| 1068 | if (!us || !info) |
| 1069 | return USB_STOR_TRANSPORT_ERROR; |
| 1070 | |
| 1071 | reply = kmalloc(512, GFP_NOIO); |
| 1072 | if (!reply) |
| 1073 | return USB_STOR_TRANSPORT_ERROR; |
| 1074 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1075 | /* ATA command : IDENTIFY DEVICE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | rc = usbat_multiple_write(us, registers, command, 3); |
| 1077 | if (rc != USB_STOR_XFER_GOOD) { |
| 1078 | US_DEBUGP("usbat_flash_get_sector_count: Gah! identify_device failed\n"); |
| 1079 | rc = USB_STOR_TRANSPORT_ERROR; |
| 1080 | goto leave; |
| 1081 | } |
| 1082 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1083 | /* Read device status */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) { |
| 1085 | rc = USB_STOR_TRANSPORT_ERROR; |
| 1086 | goto leave; |
| 1087 | } |
| 1088 | |
| 1089 | msleep(100); |
| 1090 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1091 | /* Read the device identification data */ |
Peter Chubb | 141804d | 2006-05-02 18:30:12 +0100 | [diff] [blame] | 1092 | rc = usbat_read_block(us, reply, 512, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | if (rc != USB_STOR_TRANSPORT_GOOD) |
| 1094 | goto leave; |
| 1095 | |
| 1096 | info->sectors = ((u32)(reply[117]) << 24) | |
| 1097 | ((u32)(reply[116]) << 16) | |
| 1098 | ((u32)(reply[115]) << 8) | |
| 1099 | ((u32)(reply[114]) ); |
| 1100 | |
| 1101 | rc = USB_STOR_TRANSPORT_GOOD; |
| 1102 | |
| 1103 | leave: |
| 1104 | kfree(reply); |
| 1105 | return rc; |
| 1106 | } |
| 1107 | |
| 1108 | /* |
| 1109 | * Read data from device |
| 1110 | */ |
| 1111 | static int usbat_flash_read_data(struct us_data *us, |
| 1112 | struct usbat_info *info, |
| 1113 | u32 sector, |
| 1114 | u32 sectors) |
| 1115 | { |
| 1116 | unsigned char registers[7] = { |
| 1117 | USBAT_ATA_FEATURES, |
| 1118 | USBAT_ATA_SECCNT, |
| 1119 | USBAT_ATA_SECNUM, |
| 1120 | USBAT_ATA_LBA_ME, |
| 1121 | USBAT_ATA_LBA_HI, |
| 1122 | USBAT_ATA_DEVICE, |
| 1123 | USBAT_ATA_STATUS, |
| 1124 | }; |
| 1125 | unsigned char command[7]; |
| 1126 | unsigned char *buffer; |
| 1127 | unsigned char thistime; |
| 1128 | unsigned int totallen, alloclen; |
| 1129 | int len, result; |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 1130 | unsigned int sg_offset = 0; |
| 1131 | struct scatterlist *sg = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | |
| 1133 | result = usbat_flash_check_media(us, info); |
| 1134 | if (result != USB_STOR_TRANSPORT_GOOD) |
| 1135 | return result; |
| 1136 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1137 | /* |
| 1138 | * we're working in LBA mode. according to the ATA spec, |
| 1139 | * we can support up to 28-bit addressing. I don't know if Jumpshot |
| 1140 | * supports beyond 24-bit addressing. It's kind of hard to test |
| 1141 | * since it requires > 8GB CF card. |
| 1142 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | |
| 1144 | if (sector > 0x0FFFFFFF) |
| 1145 | return USB_STOR_TRANSPORT_ERROR; |
| 1146 | |
| 1147 | totallen = sectors * info->ssize; |
| 1148 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1149 | /* |
| 1150 | * Since we don't read more than 64 KB at a time, we have to create |
| 1151 | * a bounce buffer and move the data a piece at a time between the |
| 1152 | * bounce buffer and the actual transfer buffer. |
| 1153 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | |
| 1155 | alloclen = min(totallen, 65536u); |
| 1156 | buffer = kmalloc(alloclen, GFP_NOIO); |
| 1157 | if (buffer == NULL) |
| 1158 | return USB_STOR_TRANSPORT_ERROR; |
| 1159 | |
| 1160 | do { |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1161 | /* |
| 1162 | * loop, never allocate or transfer more than 64k at once |
| 1163 | * (min(128k, 255*info->ssize) is the real limit) |
| 1164 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | len = min(totallen, alloclen); |
| 1166 | thistime = (len / info->ssize) & 0xff; |
| 1167 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1168 | /* ATA command 0x20 (READ SECTORS) */ |
| 1169 | usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1171 | /* Write/execute ATA read command */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | result = usbat_multiple_write(us, registers, command, 7); |
| 1173 | if (result != USB_STOR_TRANSPORT_GOOD) |
| 1174 | goto leave; |
| 1175 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1176 | /* Read the data we just requested */ |
Peter Chubb | 141804d | 2006-05-02 18:30:12 +0100 | [diff] [blame] | 1177 | result = usbat_read_blocks(us, buffer, len, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | if (result != USB_STOR_TRANSPORT_GOOD) |
| 1179 | goto leave; |
| 1180 | |
| 1181 | US_DEBUGP("usbat_flash_read_data: %d bytes\n", len); |
| 1182 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1183 | /* Store the data in the transfer buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | usb_stor_access_xfer_buf(buffer, len, us->srb, |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 1185 | &sg, &sg_offset, TO_XFER_BUF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
| 1187 | sector += thistime; |
| 1188 | totallen -= len; |
| 1189 | } while (totallen > 0); |
| 1190 | |
| 1191 | kfree(buffer); |
| 1192 | return USB_STOR_TRANSPORT_GOOD; |
| 1193 | |
| 1194 | leave: |
| 1195 | kfree(buffer); |
| 1196 | return USB_STOR_TRANSPORT_ERROR; |
| 1197 | } |
| 1198 | |
| 1199 | /* |
| 1200 | * Write data to device |
| 1201 | */ |
| 1202 | static int usbat_flash_write_data(struct us_data *us, |
| 1203 | struct usbat_info *info, |
| 1204 | u32 sector, |
| 1205 | u32 sectors) |
| 1206 | { |
| 1207 | unsigned char registers[7] = { |
| 1208 | USBAT_ATA_FEATURES, |
| 1209 | USBAT_ATA_SECCNT, |
| 1210 | USBAT_ATA_SECNUM, |
| 1211 | USBAT_ATA_LBA_ME, |
| 1212 | USBAT_ATA_LBA_HI, |
| 1213 | USBAT_ATA_DEVICE, |
| 1214 | USBAT_ATA_STATUS, |
| 1215 | }; |
| 1216 | unsigned char command[7]; |
| 1217 | unsigned char *buffer; |
| 1218 | unsigned char thistime; |
| 1219 | unsigned int totallen, alloclen; |
| 1220 | int len, result; |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 1221 | unsigned int sg_offset = 0; |
| 1222 | struct scatterlist *sg = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | |
| 1224 | result = usbat_flash_check_media(us, info); |
| 1225 | if (result != USB_STOR_TRANSPORT_GOOD) |
| 1226 | return result; |
| 1227 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1228 | /* |
| 1229 | * we're working in LBA mode. according to the ATA spec, |
| 1230 | * we can support up to 28-bit addressing. I don't know if the device |
| 1231 | * supports beyond 24-bit addressing. It's kind of hard to test |
| 1232 | * since it requires > 8GB media. |
| 1233 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | |
| 1235 | if (sector > 0x0FFFFFFF) |
| 1236 | return USB_STOR_TRANSPORT_ERROR; |
| 1237 | |
| 1238 | totallen = sectors * info->ssize; |
| 1239 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1240 | /* |
| 1241 | * Since we don't write more than 64 KB at a time, we have to create |
| 1242 | * a bounce buffer and move the data a piece at a time between the |
| 1243 | * bounce buffer and the actual transfer buffer. |
| 1244 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | |
| 1246 | alloclen = min(totallen, 65536u); |
| 1247 | buffer = kmalloc(alloclen, GFP_NOIO); |
| 1248 | if (buffer == NULL) |
| 1249 | return USB_STOR_TRANSPORT_ERROR; |
| 1250 | |
| 1251 | do { |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1252 | /* |
| 1253 | * loop, never allocate or transfer more than 64k at once |
| 1254 | * (min(128k, 255*info->ssize) is the real limit) |
| 1255 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | len = min(totallen, alloclen); |
| 1257 | thistime = (len / info->ssize) & 0xff; |
| 1258 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1259 | /* Get the data from the transfer buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | usb_stor_access_xfer_buf(buffer, len, us->srb, |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 1261 | &sg, &sg_offset, FROM_XFER_BUF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1263 | /* ATA command 0x30 (WRITE SECTORS) */ |
| 1264 | usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1266 | /* Write/execute ATA write command */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | result = usbat_multiple_write(us, registers, command, 7); |
| 1268 | if (result != USB_STOR_TRANSPORT_GOOD) |
| 1269 | goto leave; |
| 1270 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1271 | /* Write the data */ |
Peter Chubb | 141804d | 2006-05-02 18:30:12 +0100 | [diff] [blame] | 1272 | result = usbat_write_blocks(us, buffer, len, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | if (result != USB_STOR_TRANSPORT_GOOD) |
| 1274 | goto leave; |
| 1275 | |
| 1276 | sector += thistime; |
| 1277 | totallen -= len; |
| 1278 | } while (totallen > 0); |
| 1279 | |
| 1280 | kfree(buffer); |
| 1281 | return result; |
| 1282 | |
| 1283 | leave: |
| 1284 | kfree(buffer); |
| 1285 | return USB_STOR_TRANSPORT_ERROR; |
| 1286 | } |
| 1287 | |
| 1288 | /* |
| 1289 | * Squeeze a potentially huge (> 65535 byte) read10 command into |
| 1290 | * a little ( <= 65535 byte) ATAPI pipe |
| 1291 | */ |
| 1292 | static int usbat_hp8200e_handle_read10(struct us_data *us, |
| 1293 | unsigned char *registers, |
| 1294 | unsigned char *data, |
| 1295 | struct scsi_cmnd *srb) |
| 1296 | { |
| 1297 | int result = USB_STOR_TRANSPORT_GOOD; |
| 1298 | unsigned char *buffer; |
| 1299 | unsigned int len; |
| 1300 | unsigned int sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | unsigned int sg_offset = 0; |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 1302 | struct scatterlist *sg = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | |
| 1304 | US_DEBUGP("handle_read10: transfersize %d\n", |
| 1305 | srb->transfersize); |
| 1306 | |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 1307 | if (scsi_bufflen(srb) < 0x10000) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | |
| 1309 | result = usbat_hp8200e_rw_block_test(us, USBAT_ATA, |
| 1310 | registers, data, 19, |
| 1311 | USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD, |
| 1312 | (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ), |
| 1313 | DMA_FROM_DEVICE, |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 1314 | scsi_sglist(srb), |
| 1315 | scsi_bufflen(srb), scsi_sg_count(srb), 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | |
| 1317 | return result; |
| 1318 | } |
| 1319 | |
| 1320 | /* |
| 1321 | * Since we're requesting more data than we can handle in |
| 1322 | * a single read command (max is 64k-1), we will perform |
| 1323 | * multiple reads, but each read must be in multiples of |
| 1324 | * a sector. Luckily the sector size is in srb->transfersize |
| 1325 | * (see linux/drivers/scsi/sr.c). |
| 1326 | */ |
| 1327 | |
| 1328 | if (data[7+0] == GPCMD_READ_CD) { |
| 1329 | len = short_pack(data[7+9], data[7+8]); |
| 1330 | len <<= 16; |
| 1331 | len |= data[7+7]; |
| 1332 | US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len); |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 1333 | srb->transfersize = scsi_bufflen(srb)/len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | if (!srb->transfersize) { |
| 1337 | srb->transfersize = 2048; /* A guess */ |
| 1338 | US_DEBUGP("handle_read10: transfersize 0, forcing %d\n", |
| 1339 | srb->transfersize); |
| 1340 | } |
| 1341 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1342 | /* |
| 1343 | * Since we only read in one block at a time, we have to create |
| 1344 | * a bounce buffer and move the data a piece at a time between the |
| 1345 | * bounce buffer and the actual transfer buffer. |
| 1346 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | |
| 1348 | len = (65535/srb->transfersize) * srb->transfersize; |
| 1349 | US_DEBUGP("Max read is %d bytes\n", len); |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 1350 | len = min(len, scsi_bufflen(srb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | buffer = kmalloc(len, GFP_NOIO); |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1352 | if (buffer == NULL) /* bloody hell! */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | return USB_STOR_TRANSPORT_FAILED; |
| 1354 | sector = short_pack(data[7+3], data[7+2]); |
| 1355 | sector <<= 16; |
| 1356 | sector |= short_pack(data[7+5], data[7+4]); |
| 1357 | transferred = 0; |
| 1358 | |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 1359 | while (transferred != scsi_bufflen(srb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 1361 | if (len > scsi_bufflen(srb) - transferred) |
| 1362 | len = scsi_bufflen(srb) - transferred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1364 | data[3] = len&0xFF; /* (cylL) = expected length (L) */ |
| 1365 | data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1367 | /* Fix up the SCSI command sector and num sectors */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1369 | data[7+2] = MSB_of(sector>>16); /* SCSI command sector */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | data[7+3] = LSB_of(sector>>16); |
| 1371 | data[7+4] = MSB_of(sector&0xFFFF); |
| 1372 | data[7+5] = LSB_of(sector&0xFFFF); |
| 1373 | if (data[7+0] == GPCMD_READ_CD) |
| 1374 | data[7+6] = 0; |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1375 | data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */ |
| 1376 | data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | |
| 1378 | result = usbat_hp8200e_rw_block_test(us, USBAT_ATA, |
| 1379 | registers, data, 19, |
| 1380 | USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD, |
| 1381 | (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ), |
| 1382 | DMA_FROM_DEVICE, |
| 1383 | buffer, |
| 1384 | len, 0, 1); |
| 1385 | |
| 1386 | if (result != USB_STOR_TRANSPORT_GOOD) |
| 1387 | break; |
| 1388 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1389 | /* Store the data in the transfer buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | usb_stor_access_xfer_buf(buffer, len, srb, |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 1391 | &sg, &sg_offset, TO_XFER_BUF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1393 | /* Update the amount transferred and the sector number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | |
| 1395 | transferred += len; |
| 1396 | sector += len / srb->transfersize; |
| 1397 | |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 1398 | } /* while transferred != scsi_bufflen(srb) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | |
| 1400 | kfree(buffer); |
| 1401 | return result; |
| 1402 | } |
| 1403 | |
| 1404 | static int usbat_select_and_test_registers(struct us_data *us) |
| 1405 | { |
| 1406 | int selector; |
| 1407 | unsigned char *status = us->iobuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1409 | /* try device = master, then device = slave. */ |
Daniel Drake | 68a6457 | 2005-08-10 18:30:04 +0100 | [diff] [blame] | 1410 | for (selector = 0xA0; selector <= 0xB0; selector += 0x10) { |
| 1411 | if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) != |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | USB_STOR_XFER_GOOD) |
| 1413 | return USB_STOR_TRANSPORT_ERROR; |
| 1414 | |
| 1415 | if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) != |
| 1416 | USB_STOR_XFER_GOOD) |
| 1417 | return USB_STOR_TRANSPORT_ERROR; |
| 1418 | |
| 1419 | if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) != |
| 1420 | USB_STOR_XFER_GOOD) |
| 1421 | return USB_STOR_TRANSPORT_ERROR; |
| 1422 | |
| 1423 | if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != |
| 1424 | USB_STOR_XFER_GOOD) |
| 1425 | return USB_STOR_TRANSPORT_ERROR; |
| 1426 | |
| 1427 | if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) != |
| 1428 | USB_STOR_XFER_GOOD) |
| 1429 | return USB_STOR_TRANSPORT_ERROR; |
| 1430 | |
| 1431 | if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) != |
| 1432 | USB_STOR_XFER_GOOD) |
| 1433 | return USB_STOR_TRANSPORT_ERROR; |
| 1434 | |
| 1435 | if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) != |
| 1436 | USB_STOR_XFER_GOOD) |
| 1437 | return USB_STOR_TRANSPORT_ERROR; |
| 1438 | |
| 1439 | if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != |
| 1440 | USB_STOR_XFER_GOOD) |
| 1441 | return USB_STOR_TRANSPORT_ERROR; |
| 1442 | |
| 1443 | if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != |
| 1444 | USB_STOR_XFER_GOOD) |
| 1445 | return USB_STOR_TRANSPORT_ERROR; |
| 1446 | } |
| 1447 | |
| 1448 | return USB_STOR_TRANSPORT_GOOD; |
| 1449 | } |
| 1450 | |
| 1451 | /* |
| 1452 | * Initialize the USBAT processor and the storage device |
| 1453 | */ |
Peter Chubb | bdcfd9e | 2006-05-02 18:29:34 +0100 | [diff] [blame] | 1454 | static int init_usbat(struct us_data *us, int devicetype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | { |
| 1456 | int rc; |
| 1457 | struct usbat_info *info; |
| 1458 | unsigned char subcountH = USBAT_ATA_LBA_HI; |
| 1459 | unsigned char subcountL = USBAT_ATA_LBA_ME; |
| 1460 | unsigned char *status = us->iobuf; |
| 1461 | |
Oliver Neukum | 887c256 | 2006-01-08 12:33:45 +0100 | [diff] [blame] | 1462 | us->extra = kzalloc(sizeof(struct usbat_info), GFP_NOIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | if (!us->extra) { |
| 1464 | US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n"); |
| 1465 | return 1; |
| 1466 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | info = (struct usbat_info *) (us->extra); |
| 1468 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1469 | /* Enable peripheral control signals */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | rc = usbat_write_user_io(us, |
| 1471 | USBAT_UIO_OE1 | USBAT_UIO_OE0, |
| 1472 | USBAT_UIO_EPAD | USBAT_UIO_1); |
| 1473 | if (rc != USB_STOR_XFER_GOOD) |
| 1474 | return USB_STOR_TRANSPORT_ERROR; |
| 1475 | |
| 1476 | US_DEBUGP("INIT 1\n"); |
| 1477 | |
| 1478 | msleep(2000); |
| 1479 | |
| 1480 | rc = usbat_read_user_io(us, status); |
| 1481 | if (rc != USB_STOR_TRANSPORT_GOOD) |
| 1482 | return rc; |
| 1483 | |
| 1484 | US_DEBUGP("INIT 2\n"); |
| 1485 | |
| 1486 | rc = usbat_read_user_io(us, status); |
| 1487 | if (rc != USB_STOR_XFER_GOOD) |
| 1488 | return USB_STOR_TRANSPORT_ERROR; |
| 1489 | |
| 1490 | rc = usbat_read_user_io(us, status); |
| 1491 | if (rc != USB_STOR_XFER_GOOD) |
| 1492 | return USB_STOR_TRANSPORT_ERROR; |
| 1493 | |
| 1494 | US_DEBUGP("INIT 3\n"); |
| 1495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | rc = usbat_select_and_test_registers(us); |
| 1497 | if (rc != USB_STOR_TRANSPORT_GOOD) |
| 1498 | return rc; |
| 1499 | |
Daniel Drake | 68a6457 | 2005-08-10 18:30:04 +0100 | [diff] [blame] | 1500 | US_DEBUGP("INIT 4\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | |
| 1502 | rc = usbat_read_user_io(us, status); |
| 1503 | if (rc != USB_STOR_XFER_GOOD) |
| 1504 | return USB_STOR_TRANSPORT_ERROR; |
| 1505 | |
Daniel Drake | 68a6457 | 2005-08-10 18:30:04 +0100 | [diff] [blame] | 1506 | US_DEBUGP("INIT 5\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1508 | /* Enable peripheral control signals and card detect */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | rc = usbat_device_enable_cdt(us); |
| 1510 | if (rc != USB_STOR_TRANSPORT_GOOD) |
| 1511 | return rc; |
| 1512 | |
Daniel Drake | 68a6457 | 2005-08-10 18:30:04 +0100 | [diff] [blame] | 1513 | US_DEBUGP("INIT 6\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | |
| 1515 | rc = usbat_read_user_io(us, status); |
| 1516 | if (rc != USB_STOR_XFER_GOOD) |
| 1517 | return USB_STOR_TRANSPORT_ERROR; |
| 1518 | |
Daniel Drake | 68a6457 | 2005-08-10 18:30:04 +0100 | [diff] [blame] | 1519 | US_DEBUGP("INIT 7\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | |
| 1521 | msleep(1400); |
| 1522 | |
| 1523 | rc = usbat_read_user_io(us, status); |
| 1524 | if (rc != USB_STOR_XFER_GOOD) |
| 1525 | return USB_STOR_TRANSPORT_ERROR; |
| 1526 | |
Daniel Drake | 68a6457 | 2005-08-10 18:30:04 +0100 | [diff] [blame] | 1527 | US_DEBUGP("INIT 8\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | |
| 1529 | rc = usbat_select_and_test_registers(us); |
| 1530 | if (rc != USB_STOR_TRANSPORT_GOOD) |
| 1531 | return rc; |
| 1532 | |
Daniel Drake | 68a6457 | 2005-08-10 18:30:04 +0100 | [diff] [blame] | 1533 | US_DEBUGP("INIT 9\n"); |
| 1534 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1535 | /* At this point, we need to detect which device we are using */ |
Peter Chubb | bdcfd9e | 2006-05-02 18:29:34 +0100 | [diff] [blame] | 1536 | if (usbat_set_transport(us, info, devicetype)) |
Daniel Drake | 68a6457 | 2005-08-10 18:30:04 +0100 | [diff] [blame] | 1537 | return USB_STOR_TRANSPORT_ERROR; |
| 1538 | |
| 1539 | US_DEBUGP("INIT 10\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | |
| 1541 | if (usbat_get_device_type(us) == USBAT_DEV_FLASH) { |
| 1542 | subcountH = 0x02; |
| 1543 | subcountL = 0x00; |
| 1544 | } |
| 1545 | rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1), |
| 1546 | 0x00, 0x88, 0x08, subcountH, subcountL); |
| 1547 | if (rc != USB_STOR_XFER_GOOD) |
| 1548 | return USB_STOR_TRANSPORT_ERROR; |
| 1549 | |
Daniel Drake | 68a6457 | 2005-08-10 18:30:04 +0100 | [diff] [blame] | 1550 | US_DEBUGP("INIT 11\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | |
| 1552 | return USB_STOR_TRANSPORT_GOOD; |
| 1553 | } |
| 1554 | |
| 1555 | /* |
| 1556 | * Transport for the HP 8200e |
| 1557 | */ |
| 1558 | static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us) |
| 1559 | { |
| 1560 | int result; |
| 1561 | unsigned char *status = us->iobuf; |
| 1562 | unsigned char registers[32]; |
| 1563 | unsigned char data[32]; |
| 1564 | unsigned int len; |
| 1565 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 1567 | len = scsi_bufflen(srb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | |
| 1569 | /* Send A0 (ATA PACKET COMMAND). |
| 1570 | Note: I guess we're never going to get any of the ATA |
| 1571 | commands... just ATA Packet Commands. |
| 1572 | */ |
| 1573 | |
| 1574 | registers[0] = USBAT_ATA_FEATURES; |
| 1575 | registers[1] = USBAT_ATA_SECCNT; |
| 1576 | registers[2] = USBAT_ATA_SECNUM; |
| 1577 | registers[3] = USBAT_ATA_LBA_ME; |
| 1578 | registers[4] = USBAT_ATA_LBA_HI; |
| 1579 | registers[5] = USBAT_ATA_DEVICE; |
| 1580 | registers[6] = USBAT_ATA_CMD; |
| 1581 | data[0] = 0x00; |
| 1582 | data[1] = 0x00; |
| 1583 | data[2] = 0x00; |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1584 | data[3] = len&0xFF; /* (cylL) = expected length (L) */ |
| 1585 | data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */ |
| 1586 | data[5] = 0xB0; /* (device sel) = slave */ |
| 1587 | data[6] = 0xA0; /* (command) = ATA PACKET COMMAND */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | |
| 1589 | for (i=7; i<19; i++) { |
| 1590 | registers[i] = 0x10; |
| 1591 | data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7]; |
| 1592 | } |
| 1593 | |
| 1594 | result = usbat_get_status(us, status); |
| 1595 | US_DEBUGP("Status = %02X\n", *status); |
| 1596 | if (result != USB_STOR_XFER_GOOD) |
| 1597 | return USB_STOR_TRANSPORT_ERROR; |
| 1598 | if (srb->cmnd[0] == TEST_UNIT_READY) |
| 1599 | transferred = 0; |
| 1600 | |
| 1601 | if (srb->sc_data_direction == DMA_TO_DEVICE) { |
| 1602 | |
| 1603 | result = usbat_hp8200e_rw_block_test(us, USBAT_ATA, |
| 1604 | registers, data, 19, |
| 1605 | USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD, |
| 1606 | (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ), |
| 1607 | DMA_TO_DEVICE, |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 1608 | scsi_sglist(srb), |
| 1609 | len, scsi_sg_count(srb), 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | |
| 1611 | if (result == USB_STOR_TRANSPORT_GOOD) { |
| 1612 | transferred += len; |
| 1613 | US_DEBUGP("Wrote %08X bytes\n", transferred); |
| 1614 | } |
| 1615 | |
| 1616 | return result; |
| 1617 | |
| 1618 | } else if (srb->cmnd[0] == READ_10 || |
| 1619 | srb->cmnd[0] == GPCMD_READ_CD) { |
| 1620 | |
| 1621 | return usbat_hp8200e_handle_read10(us, registers, data, srb); |
| 1622 | |
| 1623 | } |
| 1624 | |
| 1625 | if (len > 0xFFFF) { |
| 1626 | US_DEBUGP("Error: len = %08X... what do I do now?\n", |
| 1627 | len); |
| 1628 | return USB_STOR_TRANSPORT_ERROR; |
| 1629 | } |
| 1630 | |
Roel Kluin | 5d3034a | 2010-02-17 11:50:26 +0100 | [diff] [blame] | 1631 | result = usbat_multiple_write(us, registers, data, 7); |
| 1632 | |
| 1633 | if (result != USB_STOR_TRANSPORT_GOOD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1636 | /* |
| 1637 | * Write the 12-byte command header. |
| 1638 | * |
| 1639 | * If the command is BLANK then set the timer for 75 minutes. |
| 1640 | * Otherwise set it for 10 minutes. |
| 1641 | * |
| 1642 | * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW |
| 1643 | * AT SPEED 4 IS UNRELIABLE!!! |
| 1644 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
Roel Kluin | 5d3034a | 2010-02-17 11:50:26 +0100 | [diff] [blame] | 1646 | result = usbat_write_block(us, USBAT_ATA, srb->cmnd, 12, |
| 1647 | srb->cmnd[0] == GPCMD_BLANK ? 75 : 10, 0); |
| 1648 | |
| 1649 | if (result != USB_STOR_TRANSPORT_GOOD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1652 | /* If there is response data to be read in then do it here. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | |
| 1654 | if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) { |
| 1655 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1656 | /* How many bytes to read in? Check cylL register */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | |
| 1658 | if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != |
| 1659 | USB_STOR_XFER_GOOD) { |
| 1660 | return USB_STOR_TRANSPORT_ERROR; |
| 1661 | } |
| 1662 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1663 | if (len > 0xFF) { /* need to read cylH also */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | len = *status; |
| 1665 | if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) != |
| 1666 | USB_STOR_XFER_GOOD) { |
| 1667 | return USB_STOR_TRANSPORT_ERROR; |
| 1668 | } |
| 1669 | len += ((unsigned int) *status)<<8; |
| 1670 | } |
| 1671 | else |
| 1672 | len = *status; |
| 1673 | |
| 1674 | |
Boaz Harrosh | 4776e99 | 2007-09-09 20:40:56 +0300 | [diff] [blame] | 1675 | result = usbat_read_block(us, scsi_sglist(srb), len, |
| 1676 | scsi_sg_count(srb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | return result; |
| 1680 | } |
| 1681 | |
| 1682 | /* |
| 1683 | * Transport for USBAT02-based CompactFlash and similar storage devices |
| 1684 | */ |
| 1685 | static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us) |
| 1686 | { |
| 1687 | int rc; |
| 1688 | struct usbat_info *info = (struct usbat_info *) (us->extra); |
| 1689 | unsigned long block, blocks; |
| 1690 | unsigned char *ptr = us->iobuf; |
| 1691 | static unsigned char inquiry_response[36] = { |
| 1692 | 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00 |
| 1693 | }; |
| 1694 | |
| 1695 | if (srb->cmnd[0] == INQUIRY) { |
| 1696 | US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n"); |
| 1697 | memcpy(ptr, inquiry_response, sizeof(inquiry_response)); |
| 1698 | fill_inquiry_response(us, ptr, 36); |
| 1699 | return USB_STOR_TRANSPORT_GOOD; |
| 1700 | } |
| 1701 | |
| 1702 | if (srb->cmnd[0] == READ_CAPACITY) { |
| 1703 | rc = usbat_flash_check_media(us, info); |
| 1704 | if (rc != USB_STOR_TRANSPORT_GOOD) |
| 1705 | return rc; |
| 1706 | |
| 1707 | rc = usbat_flash_get_sector_count(us, info); |
| 1708 | if (rc != USB_STOR_TRANSPORT_GOOD) |
| 1709 | return rc; |
| 1710 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1711 | /* hard coded 512 byte sectors as per ATA spec */ |
| 1712 | info->ssize = 0x200; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n", |
| 1714 | info->sectors, info->ssize); |
| 1715 | |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1716 | /* |
| 1717 | * build the reply |
| 1718 | * note: must return the sector number of the last sector, |
| 1719 | * *not* the total number of sectors |
| 1720 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1); |
| 1722 | ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize); |
| 1723 | usb_stor_set_xfer_buf(ptr, 8, srb); |
| 1724 | |
| 1725 | return USB_STOR_TRANSPORT_GOOD; |
| 1726 | } |
| 1727 | |
| 1728 | if (srb->cmnd[0] == MODE_SELECT_10) { |
| 1729 | US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n"); |
| 1730 | return USB_STOR_TRANSPORT_ERROR; |
| 1731 | } |
| 1732 | |
| 1733 | if (srb->cmnd[0] == READ_10) { |
| 1734 | block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | |
| 1735 | ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); |
| 1736 | |
| 1737 | blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8])); |
| 1738 | |
| 1739 | US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks); |
| 1740 | return usbat_flash_read_data(us, info, block, blocks); |
| 1741 | } |
| 1742 | |
| 1743 | if (srb->cmnd[0] == READ_12) { |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1744 | /* |
| 1745 | * I don't think we'll ever see a READ_12 but support it anyway |
| 1746 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | |
| 1748 | ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); |
| 1749 | |
| 1750 | blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) | |
| 1751 | ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9])); |
| 1752 | |
| 1753 | US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks); |
| 1754 | return usbat_flash_read_data(us, info, block, blocks); |
| 1755 | } |
| 1756 | |
| 1757 | if (srb->cmnd[0] == WRITE_10) { |
| 1758 | block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | |
| 1759 | ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); |
| 1760 | |
| 1761 | blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8])); |
| 1762 | |
| 1763 | US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks); |
| 1764 | return usbat_flash_write_data(us, info, block, blocks); |
| 1765 | } |
| 1766 | |
| 1767 | if (srb->cmnd[0] == WRITE_12) { |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1768 | /* |
| 1769 | * I don't think we'll ever see a WRITE_12 but support it anyway |
| 1770 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | |
| 1772 | ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); |
| 1773 | |
| 1774 | blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) | |
| 1775 | ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9])); |
| 1776 | |
| 1777 | US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks); |
| 1778 | return usbat_flash_write_data(us, info, block, blocks); |
| 1779 | } |
| 1780 | |
| 1781 | |
| 1782 | if (srb->cmnd[0] == TEST_UNIT_READY) { |
| 1783 | US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n"); |
| 1784 | |
| 1785 | rc = usbat_flash_check_media(us, info); |
| 1786 | if (rc != USB_STOR_TRANSPORT_GOOD) |
| 1787 | return rc; |
| 1788 | |
| 1789 | return usbat_check_status(us); |
| 1790 | } |
| 1791 | |
| 1792 | if (srb->cmnd[0] == REQUEST_SENSE) { |
| 1793 | US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n"); |
| 1794 | |
| 1795 | memset(ptr, 0, 18); |
| 1796 | ptr[0] = 0xF0; |
| 1797 | ptr[2] = info->sense_key; |
| 1798 | ptr[7] = 11; |
| 1799 | ptr[12] = info->sense_asc; |
| 1800 | ptr[13] = info->sense_ascq; |
| 1801 | usb_stor_set_xfer_buf(ptr, 18, srb); |
| 1802 | |
| 1803 | return USB_STOR_TRANSPORT_GOOD; |
| 1804 | } |
| 1805 | |
| 1806 | if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) { |
Daniel Drake | b7b1e65 | 2005-09-30 12:49:36 +0100 | [diff] [blame] | 1807 | /* |
| 1808 | * sure. whatever. not like we can stop the user from popping |
| 1809 | * the media out of the device (no locking doors, etc) |
| 1810 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | return USB_STOR_TRANSPORT_GOOD; |
| 1812 | } |
| 1813 | |
| 1814 | US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n", |
| 1815 | srb->cmnd[0], srb->cmnd[0]); |
| 1816 | info->sense_key = 0x05; |
| 1817 | info->sense_asc = 0x20; |
| 1818 | info->sense_ascq = 0x00; |
| 1819 | return USB_STOR_TRANSPORT_FAILED; |
| 1820 | } |
| 1821 | |
Alan Stern | 26d6818 | 2009-02-12 14:48:08 -0500 | [diff] [blame] | 1822 | static int init_usbat_cd(struct us_data *us) |
Peter Chubb | bdcfd9e | 2006-05-02 18:29:34 +0100 | [diff] [blame] | 1823 | { |
| 1824 | return init_usbat(us, USBAT_DEV_HP8200); |
| 1825 | } |
| 1826 | |
Alan Stern | 26d6818 | 2009-02-12 14:48:08 -0500 | [diff] [blame] | 1827 | static int init_usbat_flash(struct us_data *us) |
Peter Chubb | bdcfd9e | 2006-05-02 18:29:34 +0100 | [diff] [blame] | 1828 | { |
| 1829 | return init_usbat(us, USBAT_DEV_FLASH); |
| 1830 | } |
| 1831 | |
Alan Stern | 26d6818 | 2009-02-12 14:48:08 -0500 | [diff] [blame] | 1832 | static int usbat_probe(struct usb_interface *intf, |
| 1833 | const struct usb_device_id *id) |
Peter Chubb | bdcfd9e | 2006-05-02 18:29:34 +0100 | [diff] [blame] | 1834 | { |
Alan Stern | 26d6818 | 2009-02-12 14:48:08 -0500 | [diff] [blame] | 1835 | struct us_data *us; |
| 1836 | int result; |
| 1837 | |
| 1838 | result = usb_stor_probe1(&us, intf, id, |
| 1839 | (id - usbat_usb_ids) + usbat_unusual_dev_list); |
| 1840 | if (result) |
| 1841 | return result; |
| 1842 | |
| 1843 | /* The actual transport will be determined later by the |
| 1844 | * initialization routine; this is just a placeholder. |
| 1845 | */ |
| 1846 | us->transport_name = "Shuttle USBAT"; |
| 1847 | us->transport = usbat_flash_transport; |
| 1848 | us->transport_reset = usb_stor_CB_reset; |
| 1849 | us->max_lun = 1; |
| 1850 | |
| 1851 | result = usb_stor_probe2(us); |
| 1852 | return result; |
Peter Chubb | bdcfd9e | 2006-05-02 18:29:34 +0100 | [diff] [blame] | 1853 | } |
| 1854 | |
Alan Stern | 26d6818 | 2009-02-12 14:48:08 -0500 | [diff] [blame] | 1855 | static struct usb_driver usbat_driver = { |
| 1856 | .name = "ums-usbat", |
| 1857 | .probe = usbat_probe, |
| 1858 | .disconnect = usb_stor_disconnect, |
| 1859 | .suspend = usb_stor_suspend, |
| 1860 | .resume = usb_stor_resume, |
| 1861 | .reset_resume = usb_stor_reset_resume, |
| 1862 | .pre_reset = usb_stor_pre_reset, |
| 1863 | .post_reset = usb_stor_post_reset, |
| 1864 | .id_table = usbat_usb_ids, |
| 1865 | .soft_unbind = 1, |
| 1866 | }; |
| 1867 | |
| 1868 | static int __init usbat_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | { |
Alan Stern | 26d6818 | 2009-02-12 14:48:08 -0500 | [diff] [blame] | 1870 | return usb_register(&usbat_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | } |
Alan Stern | 26d6818 | 2009-02-12 14:48:08 -0500 | [diff] [blame] | 1872 | |
| 1873 | static void __exit usbat_exit(void) |
| 1874 | { |
| 1875 | usb_deregister(&usbat_driver); |
| 1876 | } |
| 1877 | |
| 1878 | module_init(usbat_init); |
| 1879 | module_exit(usbat_exit); |