blob: 599d8bff26c3f19164f5a79c428a11f3da4e3322 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Transport & Protocol Driver for In-System Design, Inc. ISD200 ASIC
2 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Current development and maintenance:
Jan Engelhardt96de0e22007-10-19 23:21:04 +02004 * (C) 2001-2002 Björn Stenberg (bjorn@haxx.se)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Developed with the assistance of:
7 * (C) 2002 Alan Stern <stern@rowland.org>
8 *
9 * Initial work:
10 * (C) 2000 In-System Design, Inc. (support@in-system.com)
11 *
12 * The ISD200 ASIC does not natively support ATA devices. The chip
13 * does implement an interface, the ATA Command Block (ATACB) which provides
14 * a means of passing ATA commands and ATA register accesses to a device.
15 *
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 2, or (at your option) any
19 * later version.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 * History:
31 *
32 * 2002-10-19: Removed the specialized transfer routines.
33 * (Alan Stern <stern@rowland.harvard.edu>)
34 * 2001-02-24: Removed lots of duplicate code and simplified the structure.
35 * (bjorn@haxx.se)
36 * 2002-01-16: Fixed endianness bug so it works on the ppc arch.
37 * (Luc Saillard <luc@saillard.org>)
38 * 2002-01-17: All bitfields removed.
39 * (bjorn@haxx.se)
40 */
41
42
43/* Include files */
44
45#include <linux/jiffies.h>
46#include <linux/errno.h>
Alan Stern32d54932009-02-12 14:47:54 -050047#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/slab.h>
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +020049#include <linux/ata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/hdreg.h>
Boaz Harrosh48c23d32007-08-22 19:10:45 +030051#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#include <scsi/scsi.h>
54#include <scsi/scsi_cmnd.h>
55#include <scsi/scsi_device.h>
56
57#include "usb.h"
58#include "transport.h"
59#include "protocol.h"
60#include "debug.h"
61#include "scsiglue.h"
Alan Stern32d54932009-02-12 14:47:54 -050062
Maciej Grela4246b062009-02-28 12:39:20 -080063MODULE_DESCRIPTION("Driver for In-System Design, Inc. ISD200 ASIC");
Al Virod36b6912011-12-29 17:09:01 -050064MODULE_AUTHOR("Björn Stenberg <bjorn@haxx.se>");
Maciej Grela4246b062009-02-28 12:39:20 -080065MODULE_LICENSE("GPL");
Alan Stern32d54932009-02-12 14:47:54 -050066
67static int isd200_Initialization(struct us_data *us);
68
69
70/*
71 * The table of devices
72 */
73#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
74 vendorName, productName, useProtocol, useTransport, \
75 initFunction, flags) \
76{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
Sebastian Andrzej Siewiorf61870e2012-08-28 22:37:13 +020077 .driver_info = (flags) }
Alan Stern32d54932009-02-12 14:47:54 -050078
Felipe Balbie7724ba2011-11-15 09:53:33 +020079static struct usb_device_id isd200_usb_ids[] = {
Alan Stern32d54932009-02-12 14:47:54 -050080# include "unusual_isd200.h"
81 { } /* Terminating entry */
82};
83MODULE_DEVICE_TABLE(usb, isd200_usb_ids);
84
85#undef UNUSUAL_DEV
Alan Stern32d54932009-02-12 14:47:54 -050086
87/*
88 * The flags table
89 */
90#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
91 vendor_name, product_name, use_protocol, use_transport, \
92 init_function, Flags) \
93{ \
94 .vendorName = vendor_name, \
95 .productName = product_name, \
96 .useProtocol = use_protocol, \
97 .useTransport = use_transport, \
98 .initFunction = init_function, \
99}
100
101static struct us_unusual_dev isd200_unusual_dev_list[] = {
102# include "unusual_isd200.h"
103 { } /* Terminating entry */
104};
105
106#undef UNUSUAL_DEV
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/* Timeout defines (in Seconds) */
109
110#define ISD200_ENUM_BSY_TIMEOUT 35
111#define ISD200_ENUM_DETECT_TIMEOUT 30
112#define ISD200_DEFAULT_TIMEOUT 30
113
114/* device flags */
115#define DF_ATA_DEVICE 0x0001
116#define DF_MEDIA_STATUS_ENABLED 0x0002
117#define DF_REMOVABLE_MEDIA 0x0004
118
119/* capability bit definitions */
120#define CAPABILITY_DMA 0x01
121#define CAPABILITY_LBA 0x02
122
123/* command_setX bit definitions */
124#define COMMANDSET_REMOVABLE 0x02
125#define COMMANDSET_MEDIA_STATUS 0x10
126
127/* ATA Vendor Specific defines */
128#define ATA_ADDRESS_DEVHEAD_STD 0xa0
129#define ATA_ADDRESS_DEVHEAD_LBA_MODE 0x40
130#define ATA_ADDRESS_DEVHEAD_SLAVE 0x10
131
132/* Action Select bits */
133#define ACTION_SELECT_0 0x01
134#define ACTION_SELECT_1 0x02
135#define ACTION_SELECT_2 0x04
136#define ACTION_SELECT_3 0x08
137#define ACTION_SELECT_4 0x10
138#define ACTION_SELECT_5 0x20
139#define ACTION_SELECT_6 0x40
140#define ACTION_SELECT_7 0x80
141
142/* Register Select bits */
143#define REG_ALTERNATE_STATUS 0x01
144#define REG_DEVICE_CONTROL 0x01
145#define REG_ERROR 0x02
146#define REG_FEATURES 0x02
147#define REG_SECTOR_COUNT 0x04
148#define REG_SECTOR_NUMBER 0x08
149#define REG_CYLINDER_LOW 0x10
150#define REG_CYLINDER_HIGH 0x20
151#define REG_DEVICE_HEAD 0x40
152#define REG_STATUS 0x80
153#define REG_COMMAND 0x80
154
Bartlomiej Zolnierkiewiczc74e8092007-10-27 18:01:04 +0200155/* ATA registers offset definitions */
156#define ATA_REG_ERROR_OFFSET 1
157#define ATA_REG_LCYL_OFFSET 4
158#define ATA_REG_HCYL_OFFSET 5
159#define ATA_REG_STATUS_OFFSET 7
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/* ATA error definitions not in <linux/hdreg.h> */
162#define ATA_ERROR_MEDIA_CHANGE 0x20
163
164/* ATA command definitions not in <linux/hdreg.h> */
165#define ATA_COMMAND_GET_MEDIA_STATUS 0xDA
166#define ATA_COMMAND_MEDIA_EJECT 0xED
167
168/* ATA drive control definitions */
169#define ATA_DC_DISABLE_INTERRUPTS 0x02
170#define ATA_DC_RESET_CONTROLLER 0x04
171#define ATA_DC_REENABLE_CONTROLLER 0x00
172
173/*
174 * General purpose return codes
175 */
176
177#define ISD200_ERROR -1
178#define ISD200_GOOD 0
179
180/*
181 * Transport return codes
182 */
183
184#define ISD200_TRANSPORT_GOOD 0 /* Transport good, command good */
185#define ISD200_TRANSPORT_FAILED 1 /* Transport good, command failed */
186#define ISD200_TRANSPORT_ERROR 2 /* Transport bad (i.e. device dead) */
187
188/* driver action codes */
189#define ACTION_READ_STATUS 0
190#define ACTION_RESET 1
191#define ACTION_REENABLE 2
192#define ACTION_SOFT_RESET 3
193#define ACTION_ENUM 4
194#define ACTION_IDENTIFY 5
195
196
197/*
198 * ata_cdb struct
199 */
200
201
202union ata_cdb {
203 struct {
204 unsigned char SignatureByte0;
205 unsigned char SignatureByte1;
206 unsigned char ActionSelect;
207 unsigned char RegisterSelect;
208 unsigned char TransferBlockSize;
209 unsigned char WriteData3F6;
210 unsigned char WriteData1F1;
211 unsigned char WriteData1F2;
212 unsigned char WriteData1F3;
213 unsigned char WriteData1F4;
214 unsigned char WriteData1F5;
215 unsigned char WriteData1F6;
216 unsigned char WriteData1F7;
217 unsigned char Reserved[3];
218 } generic;
219
220 struct {
221 unsigned char SignatureByte0;
222 unsigned char SignatureByte1;
223 unsigned char ActionSelect;
224 unsigned char RegisterSelect;
225 unsigned char TransferBlockSize;
226 unsigned char AlternateStatusByte;
227 unsigned char ErrorByte;
228 unsigned char SectorCountByte;
229 unsigned char SectorNumberByte;
230 unsigned char CylinderLowByte;
231 unsigned char CylinderHighByte;
232 unsigned char DeviceHeadByte;
233 unsigned char StatusByte;
234 unsigned char Reserved[3];
235 } read;
236
237 struct {
238 unsigned char SignatureByte0;
239 unsigned char SignatureByte1;
240 unsigned char ActionSelect;
241 unsigned char RegisterSelect;
242 unsigned char TransferBlockSize;
243 unsigned char DeviceControlByte;
244 unsigned char FeaturesByte;
245 unsigned char SectorCountByte;
246 unsigned char SectorNumberByte;
247 unsigned char CylinderLowByte;
248 unsigned char CylinderHighByte;
249 unsigned char DeviceHeadByte;
250 unsigned char CommandByte;
251 unsigned char Reserved[3];
252 } write;
253};
254
255
256/*
257 * Inquiry data structure. This is the data returned from the target
258 * after it receives an inquiry.
259 *
260 * This structure may be extended by the number of bytes specified
261 * in the field AdditionalLength. The defined size constant only
262 * includes fields through ProductRevisionLevel.
263 */
264
265/*
266 * DeviceType field
267 */
268#define DIRECT_ACCESS_DEVICE 0x00 /* disks */
269#define DEVICE_REMOVABLE 0x80
270
271struct inquiry_data {
272 unsigned char DeviceType;
273 unsigned char DeviceTypeModifier;
274 unsigned char Versions;
275 unsigned char Format;
276 unsigned char AdditionalLength;
277 unsigned char Reserved[2];
278 unsigned char Capability;
279 unsigned char VendorId[8];
280 unsigned char ProductId[16];
281 unsigned char ProductRevisionLevel[4];
282 unsigned char VendorSpecific[20];
283 unsigned char Reserved3[40];
284} __attribute__ ((packed));
285
286/*
287 * INQUIRY data buffer size
288 */
289
290#define INQUIRYDATABUFFERSIZE 36
291
292
293/*
294 * ISD200 CONFIG data struct
295 */
296
297#define ATACFG_TIMING 0x0f
298#define ATACFG_ATAPI_RESET 0x10
299#define ATACFG_MASTER 0x20
300#define ATACFG_BLOCKSIZE 0xa0
301
302#define ATACFGE_LAST_LUN 0x07
303#define ATACFGE_DESC_OVERRIDE 0x08
304#define ATACFGE_STATE_SUSPEND 0x10
305#define ATACFGE_SKIP_BOOT 0x20
306#define ATACFGE_CONF_DESC2 0x40
307#define ATACFGE_INIT_STATUS 0x80
308
309#define CFG_CAPABILITY_SRST 0x01
310
311struct isd200_config {
312 unsigned char EventNotification;
313 unsigned char ExternalClock;
314 unsigned char ATAInitTimeout;
315 unsigned char ATAConfig;
316 unsigned char ATAMajorCommand;
317 unsigned char ATAMinorCommand;
318 unsigned char ATAExtraConfig;
319 unsigned char Capability;
320}__attribute__ ((packed));
321
322
323/*
324 * ISD200 driver information struct
325 */
326
327struct isd200_info {
328 struct inquiry_data InquiryData;
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +0200329 u16 *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct isd200_config ConfigData;
331 unsigned char *RegsBuf;
332 unsigned char ATARegs[8];
333 unsigned char DeviceHead;
334 unsigned char DeviceFlags;
335
336 /* maximum number of LUNs supported */
337 unsigned char MaxLUNs;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300338 unsigned char cmnd[BLK_MAX_CDB];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct scsi_cmnd srb;
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300340 struct scatterlist sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341};
342
343
344/*
345 * Read Capacity Data - returned in Big Endian format
346 */
347
348struct read_capacity_data {
349 __be32 LogicalBlockAddress;
350 __be32 BytesPerBlock;
351};
352
353/*
354 * Read Block Limits Data - returned in Big Endian format
355 * This structure returns the maximum and minimum block
356 * size for a TAPE device.
357 */
358
359struct read_block_limits {
360 unsigned char Reserved;
361 unsigned char BlockMaximumSize[3];
362 unsigned char BlockMinimumSize[2];
363};
364
365
366/*
367 * Sense Data Format
368 */
369
370#define SENSE_ERRCODE 0x7f
371#define SENSE_ERRCODE_VALID 0x80
372#define SENSE_FLAG_SENSE_KEY 0x0f
373#define SENSE_FLAG_BAD_LENGTH 0x20
374#define SENSE_FLAG_END_OF_MEDIA 0x40
375#define SENSE_FLAG_FILE_MARK 0x80
376struct sense_data {
377 unsigned char ErrorCode;
378 unsigned char SegmentNumber;
379 unsigned char Flags;
380 unsigned char Information[4];
381 unsigned char AdditionalSenseLength;
382 unsigned char CommandSpecificInformation[4];
383 unsigned char AdditionalSenseCode;
384 unsigned char AdditionalSenseCodeQualifier;
385 unsigned char FieldReplaceableUnitCode;
386 unsigned char SenseKeySpecific[3];
387} __attribute__ ((packed));
388
389/*
390 * Default request sense buffer size
391 */
392
393#define SENSE_BUFFER_SIZE 18
394
395/***********************************************************************
396 * Helper routines
397 ***********************************************************************/
398
399/**************************************************************************
400 * isd200_build_sense
401 *
402 * Builds an artificial sense buffer to report the results of a
403 * failed command.
404 *
405 * RETURNS:
406 * void
407 */
408static void isd200_build_sense(struct us_data *us, struct scsi_cmnd *srb)
409{
410 struct isd200_info *info = (struct isd200_info *)us->extra;
411 struct sense_data *buf = (struct sense_data *) &srb->sense_buffer[0];
Bartlomiej Zolnierkiewiczc74e8092007-10-27 18:01:04 +0200412 unsigned char error = info->ATARegs[ATA_REG_ERROR_OFFSET];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 if(error & ATA_ERROR_MEDIA_CHANGE) {
415 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
416 buf->AdditionalSenseLength = 0xb;
417 buf->Flags = UNIT_ATTENTION;
418 buf->AdditionalSenseCode = 0;
419 buf->AdditionalSenseCodeQualifier = 0;
Bartlomiej Zolnierkiewiczfcd3ec62009-04-01 21:42:25 +0200420 } else if (error & ATA_MCR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
422 buf->AdditionalSenseLength = 0xb;
423 buf->Flags = UNIT_ATTENTION;
424 buf->AdditionalSenseCode = 0;
425 buf->AdditionalSenseCodeQualifier = 0;
Bartlomiej Zolnierkiewiczfcd3ec62009-04-01 21:42:25 +0200426 } else if (error & ATA_TRK0NF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
428 buf->AdditionalSenseLength = 0xb;
429 buf->Flags = NOT_READY;
430 buf->AdditionalSenseCode = 0;
431 buf->AdditionalSenseCodeQualifier = 0;
Bartlomiej Zolnierkiewiczfcd3ec62009-04-01 21:42:25 +0200432 } else if (error & ATA_UNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
434 buf->AdditionalSenseLength = 0xb;
435 buf->Flags = DATA_PROTECT;
436 buf->AdditionalSenseCode = 0;
437 buf->AdditionalSenseCodeQualifier = 0;
438 } else {
439 buf->ErrorCode = 0;
440 buf->AdditionalSenseLength = 0;
441 buf->Flags = 0;
442 buf->AdditionalSenseCode = 0;
443 buf->AdditionalSenseCodeQualifier = 0;
444 }
445}
446
447
448/***********************************************************************
449 * Transport routines
450 ***********************************************************************/
451
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300452/**************************************************************************
453 * isd200_set_srb(), isd200_srb_set_bufflen()
454 *
455 * Two helpers to facilitate in initialization of scsi_cmnd structure
456 * Will need to change when struct scsi_cmnd changes
457 */
458static void isd200_set_srb(struct isd200_info *info,
459 enum dma_data_direction dir, void* buff, unsigned bufflen)
460{
461 struct scsi_cmnd *srb = &info->srb;
462
463 if (buff)
464 sg_init_one(&info->sg, buff, bufflen);
465
466 srb->sc_data_direction = dir;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200467 srb->sdb.table.sgl = buff ? &info->sg : NULL;
468 srb->sdb.length = bufflen;
469 srb->sdb.table.nents = buff ? 1 : 0;
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300470}
471
472static void isd200_srb_set_bufflen(struct scsi_cmnd *srb, unsigned bufflen)
473{
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200474 srb->sdb.length = bufflen;
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300475}
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478/**************************************************************************
479 * isd200_action
480 *
481 * Routine for sending commands to the isd200
482 *
483 * RETURNS:
484 * ISD status code
485 */
486static int isd200_action( struct us_data *us, int action,
487 void* pointer, int value )
488{
489 union ata_cdb ata;
Ian Munsie0df4d8e2010-03-03 11:00:37 +1100490 /* static to prevent this large struct being placed on the valuable stack */
491 static struct scsi_device srb_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 struct isd200_info *info = (struct isd200_info *)us->extra;
493 struct scsi_cmnd *srb = &info->srb;
494 int status;
495
496 memset(&ata, 0, sizeof(ata));
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300497 srb->cmnd = info->cmnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 srb->device = &srb_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 ata.generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
501 ata.generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
502 ata.generic.TransferBlockSize = 1;
503
504 switch ( action ) {
505 case ACTION_READ_STATUS:
Joe Perches191648d2013-04-19 11:44:00 -0700506 usb_stor_dbg(us, " isd200_action(READ_STATUS)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 ata.generic.ActionSelect = ACTION_SELECT_0|ACTION_SELECT_2;
508 ata.generic.RegisterSelect =
509 REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
510 REG_STATUS | REG_ERROR;
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300511 isd200_set_srb(info, DMA_FROM_DEVICE, pointer, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 break;
513
514 case ACTION_ENUM:
Joe Perches191648d2013-04-19 11:44:00 -0700515 usb_stor_dbg(us, " isd200_action(ENUM,0x%02x)\n", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
517 ACTION_SELECT_3|ACTION_SELECT_4|
518 ACTION_SELECT_5;
519 ata.generic.RegisterSelect = REG_DEVICE_HEAD;
520 ata.write.DeviceHeadByte = value;
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300521 isd200_set_srb(info, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 break;
523
524 case ACTION_RESET:
Joe Perches191648d2013-04-19 11:44:00 -0700525 usb_stor_dbg(us, " isd200_action(RESET)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
527 ACTION_SELECT_3|ACTION_SELECT_4;
528 ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
529 ata.write.DeviceControlByte = ATA_DC_RESET_CONTROLLER;
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300530 isd200_set_srb(info, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 break;
532
533 case ACTION_REENABLE:
Joe Perches191648d2013-04-19 11:44:00 -0700534 usb_stor_dbg(us, " isd200_action(REENABLE)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
536 ACTION_SELECT_3|ACTION_SELECT_4;
537 ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
538 ata.write.DeviceControlByte = ATA_DC_REENABLE_CONTROLLER;
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300539 isd200_set_srb(info, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 break;
541
542 case ACTION_SOFT_RESET:
Joe Perches191648d2013-04-19 11:44:00 -0700543 usb_stor_dbg(us, " isd200_action(SOFT_RESET)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_5;
545 ata.generic.RegisterSelect = REG_DEVICE_HEAD | REG_COMMAND;
546 ata.write.DeviceHeadByte = info->DeviceHead;
Bartlomiej Zolnierkiewiczf08f31f2009-04-01 21:42:24 +0200547 ata.write.CommandByte = ATA_CMD_DEV_RESET;
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300548 isd200_set_srb(info, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 break;
550
551 case ACTION_IDENTIFY:
Joe Perches191648d2013-04-19 11:44:00 -0700552 usb_stor_dbg(us, " isd200_action(IDENTIFY)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 ata.generic.RegisterSelect = REG_COMMAND;
Bartlomiej Zolnierkiewiczf08f31f2009-04-01 21:42:24 +0200554 ata.write.CommandByte = ATA_CMD_ID_ATA;
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300555 isd200_set_srb(info, DMA_FROM_DEVICE, info->id,
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +0200556 ATA_ID_WORDS * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 break;
558
559 default:
Joe Perches191648d2013-04-19 11:44:00 -0700560 usb_stor_dbg(us, "Error: Undefined action %d\n", action);
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300561 return ISD200_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563
564 memcpy(srb->cmnd, &ata, sizeof(ata.generic));
565 srb->cmd_len = sizeof(ata.generic);
566 status = usb_stor_Bulk_transport(srb, us);
567 if (status == USB_STOR_TRANSPORT_GOOD)
568 status = ISD200_GOOD;
569 else {
Joe Perches191648d2013-04-19 11:44:00 -0700570 usb_stor_dbg(us, " isd200_action(0x%02x) error: %d\n",
571 action, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 status = ISD200_ERROR;
573 /* need to reset device here */
574 }
575
576 return status;
577}
578
579/**************************************************************************
580 * isd200_read_regs
581 *
582 * Read ATA Registers
583 *
584 * RETURNS:
585 * ISD status code
586 */
587static int isd200_read_regs( struct us_data *us )
588{
589 struct isd200_info *info = (struct isd200_info *)us->extra;
590 int retStatus = ISD200_GOOD;
591 int transferStatus;
592
Joe Perches191648d2013-04-19 11:44:00 -0700593 usb_stor_dbg(us, "Entering isd200_IssueATAReadRegs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 transferStatus = isd200_action( us, ACTION_READ_STATUS,
596 info->RegsBuf, sizeof(info->ATARegs) );
597 if (transferStatus != ISD200_TRANSPORT_GOOD) {
Joe Perches191648d2013-04-19 11:44:00 -0700598 usb_stor_dbg(us, " Error reading ATA registers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 retStatus = ISD200_ERROR;
600 } else {
601 memcpy(info->ATARegs, info->RegsBuf, sizeof(info->ATARegs));
Joe Perches191648d2013-04-19 11:44:00 -0700602 usb_stor_dbg(us, " Got ATA Register[ATA_REG_ERROR_OFFSET] = 0x%x\n",
603 info->ATARegs[ATA_REG_ERROR_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
606 return retStatus;
607}
608
609
610/**************************************************************************
611 * Invoke the transport and basic error-handling/recovery methods
612 *
613 * This is used by the protocol layers to actually send the message to
614 * the device and receive the response.
615 */
616static void isd200_invoke_transport( struct us_data *us,
617 struct scsi_cmnd *srb,
618 union ata_cdb *ataCdb )
619{
620 int need_auto_sense = 0;
621 int transferStatus;
622 int result;
623
624 /* send the command to the transport layer */
625 memcpy(srb->cmnd, ataCdb, sizeof(ataCdb->generic));
626 srb->cmd_len = sizeof(ataCdb->generic);
627 transferStatus = usb_stor_Bulk_transport(srb, us);
628
629 /* if the command gets aborted by the higher layers, we need to
630 * short-circuit all other processing
631 */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400632 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
Joe Perches191648d2013-04-19 11:44:00 -0700633 usb_stor_dbg(us, "-- command was aborted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 goto Handle_Abort;
635 }
636
637 switch (transferStatus) {
638
639 case USB_STOR_TRANSPORT_GOOD:
640 /* Indicate a good result */
641 srb->result = SAM_STAT_GOOD;
642 break;
643
644 case USB_STOR_TRANSPORT_NO_SENSE:
Joe Perches191648d2013-04-19 11:44:00 -0700645 usb_stor_dbg(us, "-- transport indicates protocol failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 srb->result = SAM_STAT_CHECK_CONDITION;
647 return;
648
649 case USB_STOR_TRANSPORT_FAILED:
Joe Perches191648d2013-04-19 11:44:00 -0700650 usb_stor_dbg(us, "-- transport indicates command failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 need_auto_sense = 1;
652 break;
653
654 case USB_STOR_TRANSPORT_ERROR:
Joe Perches191648d2013-04-19 11:44:00 -0700655 usb_stor_dbg(us, "-- transport indicates transport error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 srb->result = DID_ERROR << 16;
657 /* Need reset here */
658 return;
659
660 default:
Joe Perches191648d2013-04-19 11:44:00 -0700661 usb_stor_dbg(us, "-- transport indicates unknown error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 srb->result = DID_ERROR << 16;
663 /* Need reset here */
664 return;
665 }
666
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300667 if ((scsi_get_resid(srb) > 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 !((srb->cmnd[0] == REQUEST_SENSE) ||
669 (srb->cmnd[0] == INQUIRY) ||
670 (srb->cmnd[0] == MODE_SENSE) ||
671 (srb->cmnd[0] == LOG_SENSE) ||
672 (srb->cmnd[0] == MODE_SENSE_10))) {
Joe Perches191648d2013-04-19 11:44:00 -0700673 usb_stor_dbg(us, "-- unexpectedly short transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 need_auto_sense = 1;
675 }
676
677 if (need_auto_sense) {
678 result = isd200_read_regs(us);
Alan Stern7e4d6c32008-05-01 15:35:18 -0400679 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
Joe Perches191648d2013-04-19 11:44:00 -0700680 usb_stor_dbg(us, "-- auto-sense aborted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 goto Handle_Abort;
682 }
683 if (result == ISD200_GOOD) {
684 isd200_build_sense(us, srb);
685 srb->result = SAM_STAT_CHECK_CONDITION;
686
687 /* If things are really okay, then let's show that */
688 if ((srb->sense_buffer[2] & 0xf) == 0x0)
689 srb->result = SAM_STAT_GOOD;
690 } else {
691 srb->result = DID_ERROR << 16;
692 /* Need reset here */
693 }
694 }
695
696 /* Regardless of auto-sense, if we _know_ we have an error
697 * condition, show that in the result code
698 */
699 if (transferStatus == USB_STOR_TRANSPORT_FAILED)
700 srb->result = SAM_STAT_CHECK_CONDITION;
701 return;
702
703 /* abort processing: the bulk-only transport requires a reset
704 * following an abort */
705 Handle_Abort:
706 srb->result = DID_ABORT << 16;
707
708 /* permit the reset transfer to take place */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400709 clear_bit(US_FLIDX_ABORTING, &us->dflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* Need reset here */
711}
712
713#ifdef CONFIG_USB_STORAGE_DEBUG
Joe Perches191648d2013-04-19 11:44:00 -0700714static void isd200_log_config(struct us_data *us, struct isd200_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Joe Perches191648d2013-04-19 11:44:00 -0700716 usb_stor_dbg(us, " Event Notification: 0x%x\n",
717 info->ConfigData.EventNotification);
718 usb_stor_dbg(us, " External Clock: 0x%x\n",
719 info->ConfigData.ExternalClock);
720 usb_stor_dbg(us, " ATA Init Timeout: 0x%x\n",
721 info->ConfigData.ATAInitTimeout);
722 usb_stor_dbg(us, " ATAPI Command Block Size: 0x%x\n",
723 (info->ConfigData.ATAConfig & ATACFG_BLOCKSIZE) >> 6);
724 usb_stor_dbg(us, " Master/Slave Selection: 0x%x\n",
725 info->ConfigData.ATAConfig & ATACFG_MASTER);
726 usb_stor_dbg(us, " ATAPI Reset: 0x%x\n",
727 info->ConfigData.ATAConfig & ATACFG_ATAPI_RESET);
728 usb_stor_dbg(us, " ATA Timing: 0x%x\n",
729 info->ConfigData.ATAConfig & ATACFG_TIMING);
730 usb_stor_dbg(us, " ATA Major Command: 0x%x\n",
731 info->ConfigData.ATAMajorCommand);
732 usb_stor_dbg(us, " ATA Minor Command: 0x%x\n",
733 info->ConfigData.ATAMinorCommand);
734 usb_stor_dbg(us, " Init Status: 0x%x\n",
735 info->ConfigData.ATAExtraConfig & ATACFGE_INIT_STATUS);
736 usb_stor_dbg(us, " Config Descriptor 2: 0x%x\n",
737 info->ConfigData.ATAExtraConfig & ATACFGE_CONF_DESC2);
738 usb_stor_dbg(us, " Skip Device Boot: 0x%x\n",
739 info->ConfigData.ATAExtraConfig & ATACFGE_SKIP_BOOT);
740 usb_stor_dbg(us, " ATA 3 State Supsend: 0x%x\n",
741 info->ConfigData.ATAExtraConfig & ATACFGE_STATE_SUSPEND);
742 usb_stor_dbg(us, " Descriptor Override: 0x%x\n",
743 info->ConfigData.ATAExtraConfig & ATACFGE_DESC_OVERRIDE);
744 usb_stor_dbg(us, " Last LUN Identifier: 0x%x\n",
745 info->ConfigData.ATAExtraConfig & ATACFGE_LAST_LUN);
746 usb_stor_dbg(us, " SRST Enable: 0x%x\n",
747 info->ConfigData.ATAExtraConfig & CFG_CAPABILITY_SRST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749#endif
750
751/**************************************************************************
752 * isd200_write_config
753 *
754 * Write the ISD200 Configuration data
755 *
756 * RETURNS:
757 * ISD status code
758 */
759static int isd200_write_config( struct us_data *us )
760{
761 struct isd200_info *info = (struct isd200_info *)us->extra;
762 int retStatus = ISD200_GOOD;
763 int result;
764
765#ifdef CONFIG_USB_STORAGE_DEBUG
Joe Perches191648d2013-04-19 11:44:00 -0700766 usb_stor_dbg(us, "Entering isd200_write_config\n");
767 usb_stor_dbg(us, " Writing the following ISD200 Config Data:\n");
768 isd200_log_config(us, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769#endif
770
771 /* let's send the command via the control pipe */
772 result = usb_stor_ctrl_transfer(
773 us,
774 us->send_ctrl_pipe,
775 0x01,
776 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
777 0x0000,
778 0x0002,
779 (void *) &info->ConfigData,
780 sizeof(info->ConfigData));
781
782 if (result >= 0) {
Joe Perches191648d2013-04-19 11:44:00 -0700783 usb_stor_dbg(us, " ISD200 Config Data was written successfully\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 } else {
Joe Perches191648d2013-04-19 11:44:00 -0700785 usb_stor_dbg(us, " Request to write ISD200 Config Data failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 retStatus = ISD200_ERROR;
787 }
788
Joe Perches191648d2013-04-19 11:44:00 -0700789 usb_stor_dbg(us, "Leaving isd200_write_config %08X\n", retStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return retStatus;
791}
792
793
794/**************************************************************************
795 * isd200_read_config
796 *
797 * Reads the ISD200 Configuration data
798 *
799 * RETURNS:
800 * ISD status code
801 */
802static int isd200_read_config( struct us_data *us )
803{
804 struct isd200_info *info = (struct isd200_info *)us->extra;
805 int retStatus = ISD200_GOOD;
806 int result;
807
Joe Perches191648d2013-04-19 11:44:00 -0700808 usb_stor_dbg(us, "Entering isd200_read_config\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 /* read the configuration information from ISD200. Use this to */
811 /* determine what the special ATA CDB bytes are. */
812
813 result = usb_stor_ctrl_transfer(
814 us,
815 us->recv_ctrl_pipe,
816 0x02,
817 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
818 0x0000,
819 0x0002,
820 (void *) &info->ConfigData,
821 sizeof(info->ConfigData));
822
823
824 if (result >= 0) {
Joe Perches191648d2013-04-19 11:44:00 -0700825 usb_stor_dbg(us, " Retrieved the following ISD200 Config Data:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826#ifdef CONFIG_USB_STORAGE_DEBUG
Joe Perches191648d2013-04-19 11:44:00 -0700827 isd200_log_config(us, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828#endif
829 } else {
Joe Perches191648d2013-04-19 11:44:00 -0700830 usb_stor_dbg(us, " Request to get ISD200 Config Data failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 retStatus = ISD200_ERROR;
832 }
833
Joe Perches191648d2013-04-19 11:44:00 -0700834 usb_stor_dbg(us, "Leaving isd200_read_config %08X\n", retStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return retStatus;
836}
837
838
839/**************************************************************************
840 * isd200_atapi_soft_reset
841 *
842 * Perform an Atapi Soft Reset on the device
843 *
844 * RETURNS:
845 * NT status code
846 */
847static int isd200_atapi_soft_reset( struct us_data *us )
848{
849 int retStatus = ISD200_GOOD;
850 int transferStatus;
851
Joe Perches191648d2013-04-19 11:44:00 -0700852 usb_stor_dbg(us, "Entering isd200_atapi_soft_reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 transferStatus = isd200_action( us, ACTION_SOFT_RESET, NULL, 0 );
855 if (transferStatus != ISD200_TRANSPORT_GOOD) {
Joe Perches191648d2013-04-19 11:44:00 -0700856 usb_stor_dbg(us, " Error issuing Atapi Soft Reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 retStatus = ISD200_ERROR;
858 }
859
Joe Perches191648d2013-04-19 11:44:00 -0700860 usb_stor_dbg(us, "Leaving isd200_atapi_soft_reset %08X\n", retStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return retStatus;
862}
863
864
865/**************************************************************************
866 * isd200_srst
867 *
868 * Perform an SRST on the device
869 *
870 * RETURNS:
871 * ISD status code
872 */
873static int isd200_srst( struct us_data *us )
874{
875 int retStatus = ISD200_GOOD;
876 int transferStatus;
877
Joe Perches191648d2013-04-19 11:44:00 -0700878 usb_stor_dbg(us, "Entering isd200_SRST\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 transferStatus = isd200_action( us, ACTION_RESET, NULL, 0 );
881
882 /* check to see if this request failed */
883 if (transferStatus != ISD200_TRANSPORT_GOOD) {
Joe Perches191648d2013-04-19 11:44:00 -0700884 usb_stor_dbg(us, " Error issuing SRST\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 retStatus = ISD200_ERROR;
886 } else {
887 /* delay 10ms to give the drive a chance to see it */
888 msleep(10);
889
890 transferStatus = isd200_action( us, ACTION_REENABLE, NULL, 0 );
891 if (transferStatus != ISD200_TRANSPORT_GOOD) {
Joe Perches191648d2013-04-19 11:44:00 -0700892 usb_stor_dbg(us, " Error taking drive out of reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 retStatus = ISD200_ERROR;
894 } else {
895 /* delay 50ms to give the drive a chance to recover after SRST */
896 msleep(50);
897 }
898 }
899
Joe Perches191648d2013-04-19 11:44:00 -0700900 usb_stor_dbg(us, "Leaving isd200_srst %08X\n", retStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return retStatus;
902}
903
904
905/**************************************************************************
906 * isd200_try_enum
907 *
908 * Helper function for isd200_manual_enum(). Does ENUM and READ_STATUS
909 * and tries to analyze the status registers
910 *
911 * RETURNS:
912 * ISD status code
913 */
914static int isd200_try_enum(struct us_data *us, unsigned char master_slave,
915 int detect )
916{
917 int status = ISD200_GOOD;
918 unsigned long endTime;
919 struct isd200_info *info = (struct isd200_info *)us->extra;
920 unsigned char *regs = info->RegsBuf;
921 int recheckAsMaster = 0;
922
923 if ( detect )
924 endTime = jiffies + ISD200_ENUM_DETECT_TIMEOUT * HZ;
925 else
926 endTime = jiffies + ISD200_ENUM_BSY_TIMEOUT * HZ;
927
928 /* loop until we detect !BSY or timeout */
929 while(1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 status = isd200_action( us, ACTION_ENUM, NULL, master_slave );
932 if ( status != ISD200_GOOD )
933 break;
934
935 status = isd200_action( us, ACTION_READ_STATUS,
936 regs, 8 );
937 if ( status != ISD200_GOOD )
938 break;
939
940 if (!detect) {
Bartlomiej Zolnierkiewiczfcd3ec62009-04-01 21:42:25 +0200941 if (regs[ATA_REG_STATUS_OFFSET] & ATA_BUSY) {
Joe Perches191648d2013-04-19 11:44:00 -0700942 usb_stor_dbg(us, " %s status is still BSY, try again...\n",
943 master_slave == ATA_ADDRESS_DEVHEAD_STD ?
944 "Master" : "Slave");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 } else {
Joe Perches191648d2013-04-19 11:44:00 -0700946 usb_stor_dbg(us, " %s status !BSY, continue with next operation\n",
947 master_slave == ATA_ADDRESS_DEVHEAD_STD ?
948 "Master" : "Slave");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 break;
950 }
951 }
Bartlomiej Zolnierkiewiczfcd3ec62009-04-01 21:42:25 +0200952 /* check for ATA_BUSY and */
953 /* ATA_DF (workaround ATA Zip drive) and */
954 /* ATA_ERR (workaround for Archos CD-ROM) */
Bartlomiej Zolnierkiewiczc74e8092007-10-27 18:01:04 +0200955 else if (regs[ATA_REG_STATUS_OFFSET] &
Bartlomiej Zolnierkiewiczfcd3ec62009-04-01 21:42:25 +0200956 (ATA_BUSY | ATA_DF | ATA_ERR)) {
Joe Perches191648d2013-04-19 11:44:00 -0700957 usb_stor_dbg(us, " Status indicates it is not ready, try again...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959 /* check for DRDY, ATA devices set DRDY after SRST */
Bartlomiej Zolnierkiewiczfcd3ec62009-04-01 21:42:25 +0200960 else if (regs[ATA_REG_STATUS_OFFSET] & ATA_DRDY) {
Joe Perches191648d2013-04-19 11:44:00 -0700961 usb_stor_dbg(us, " Identified ATA device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 info->DeviceFlags |= DF_ATA_DEVICE;
963 info->DeviceHead = master_slave;
964 break;
965 }
966 /* check Cylinder High/Low to
967 determine if it is an ATAPI device
968 */
Bartlomiej Zolnierkiewiczc74e8092007-10-27 18:01:04 +0200969 else if (regs[ATA_REG_HCYL_OFFSET] == 0xEB &&
970 regs[ATA_REG_LCYL_OFFSET] == 0x14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /* It seems that the RICOH
972 MP6200A CD/RW drive will
973 report itself okay as a
974 slave when it is really a
975 master. So this check again
976 as a master device just to
977 make sure it doesn't report
978 itself okay as a master also
979 */
980 if ((master_slave & ATA_ADDRESS_DEVHEAD_SLAVE) &&
981 !recheckAsMaster) {
Joe Perches191648d2013-04-19 11:44:00 -0700982 usb_stor_dbg(us, " Identified ATAPI device as slave. Rechecking again as master\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 recheckAsMaster = 1;
984 master_slave = ATA_ADDRESS_DEVHEAD_STD;
985 } else {
Joe Perches191648d2013-04-19 11:44:00 -0700986 usb_stor_dbg(us, " Identified ATAPI device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 info->DeviceHead = master_slave;
988
989 status = isd200_atapi_soft_reset(us);
990 break;
991 }
992 } else {
Joe Perches191648d2013-04-19 11:44:00 -0700993 usb_stor_dbg(us, " Not ATA, not ATAPI - Weird\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 break;
995 }
996
997 /* check for timeout on this request */
998 if (time_after_eq(jiffies, endTime)) {
999 if (!detect)
Joe Perches191648d2013-04-19 11:44:00 -07001000 usb_stor_dbg(us, " BSY check timeout, just continue with next operation...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 else
Joe Perches191648d2013-04-19 11:44:00 -07001002 usb_stor_dbg(us, " Device detect timeout!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 break;
1004 }
1005 }
1006
1007 return status;
1008}
1009
1010/**************************************************************************
1011 * isd200_manual_enum
1012 *
1013 * Determines if the drive attached is an ATA or ATAPI and if it is a
1014 * master or slave.
1015 *
1016 * RETURNS:
1017 * ISD status code
1018 */
1019static int isd200_manual_enum(struct us_data *us)
1020{
1021 struct isd200_info *info = (struct isd200_info *)us->extra;
1022 int retStatus = ISD200_GOOD;
1023
Joe Perches191648d2013-04-19 11:44:00 -07001024 usb_stor_dbg(us, "Entering isd200_manual_enum\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 retStatus = isd200_read_config(us);
1027 if (retStatus == ISD200_GOOD) {
1028 int isslave;
1029 /* master or slave? */
1030 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 0);
1031 if (retStatus == ISD200_GOOD)
1032 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_SLAVE, 0);
1033
1034 if (retStatus == ISD200_GOOD) {
1035 retStatus = isd200_srst(us);
1036 if (retStatus == ISD200_GOOD)
1037 /* ata or atapi? */
1038 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 1);
1039 }
1040
1041 isslave = (info->DeviceHead & ATA_ADDRESS_DEVHEAD_SLAVE) ? 1 : 0;
1042 if (!(info->ConfigData.ATAConfig & ATACFG_MASTER)) {
Joe Perches191648d2013-04-19 11:44:00 -07001043 usb_stor_dbg(us, " Setting Master/Slave selection to %d\n",
1044 isslave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 info->ConfigData.ATAConfig &= 0x3f;
1046 info->ConfigData.ATAConfig |= (isslave<<6);
1047 retStatus = isd200_write_config(us);
1048 }
1049 }
1050
Joe Perches191648d2013-04-19 11:44:00 -07001051 usb_stor_dbg(us, "Leaving isd200_manual_enum %08X\n", retStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return(retStatus);
1053}
1054
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001055static void isd200_fix_driveid(u16 *id)
Alan Cox3794ade2007-10-15 15:08:11 +01001056{
1057#ifndef __LITTLE_ENDIAN
1058# ifdef __BIG_ENDIAN
1059 int i;
Alan Cox3794ade2007-10-15 15:08:11 +01001060
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001061 for (i = 0; i < ATA_ID_WORDS; i++)
1062 id[i] = __le16_to_cpu(id[i]);
Alan Cox3794ade2007-10-15 15:08:11 +01001063# else
1064# error "Please fix <asm/byteorder.h>"
1065# endif
1066#endif
1067}
1068
Joe Perches191648d2013-04-19 11:44:00 -07001069static void isd200_dump_driveid(struct us_data *us, u16 *id)
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001070{
Joe Perches191648d2013-04-19 11:44:00 -07001071 usb_stor_dbg(us, " Identify Data Structure:\n");
1072 usb_stor_dbg(us, " config = 0x%x\n", id[ATA_ID_CONFIG]);
1073 usb_stor_dbg(us, " cyls = 0x%x\n", id[ATA_ID_CYLS]);
1074 usb_stor_dbg(us, " heads = 0x%x\n", id[ATA_ID_HEADS]);
1075 usb_stor_dbg(us, " track_bytes = 0x%x\n", id[4]);
1076 usb_stor_dbg(us, " sector_bytes = 0x%x\n", id[5]);
1077 usb_stor_dbg(us, " sectors = 0x%x\n", id[ATA_ID_SECTORS]);
1078 usb_stor_dbg(us, " serial_no[0] = 0x%x\n", *(char *)&id[ATA_ID_SERNO]);
1079 usb_stor_dbg(us, " buf_type = 0x%x\n", id[20]);
1080 usb_stor_dbg(us, " buf_size = 0x%x\n", id[ATA_ID_BUF_SIZE]);
1081 usb_stor_dbg(us, " ecc_bytes = 0x%x\n", id[22]);
1082 usb_stor_dbg(us, " fw_rev[0] = 0x%x\n", *(char *)&id[ATA_ID_FW_REV]);
1083 usb_stor_dbg(us, " model[0] = 0x%x\n", *(char *)&id[ATA_ID_PROD]);
1084 usb_stor_dbg(us, " max_multsect = 0x%x\n", id[ATA_ID_MAX_MULTSECT] & 0xff);
1085 usb_stor_dbg(us, " dword_io = 0x%x\n", id[ATA_ID_DWORD_IO]);
1086 usb_stor_dbg(us, " capability = 0x%x\n", id[ATA_ID_CAPABILITY] >> 8);
1087 usb_stor_dbg(us, " tPIO = 0x%x\n", id[ATA_ID_OLD_PIO_MODES] >> 8);
1088 usb_stor_dbg(us, " tDMA = 0x%x\n", id[ATA_ID_OLD_DMA_MODES] >> 8);
1089 usb_stor_dbg(us, " field_valid = 0x%x\n", id[ATA_ID_FIELD_VALID]);
1090 usb_stor_dbg(us, " cur_cyls = 0x%x\n", id[ATA_ID_CUR_CYLS]);
1091 usb_stor_dbg(us, " cur_heads = 0x%x\n", id[ATA_ID_CUR_HEADS]);
1092 usb_stor_dbg(us, " cur_sectors = 0x%x\n", id[ATA_ID_CUR_SECTORS]);
1093 usb_stor_dbg(us, " cur_capacity = 0x%x\n", ata_id_u32(id, 57));
1094 usb_stor_dbg(us, " multsect = 0x%x\n", id[ATA_ID_MULTSECT] & 0xff);
1095 usb_stor_dbg(us, " lba_capacity = 0x%x\n", ata_id_u32(id, ATA_ID_LBA_CAPACITY));
1096 usb_stor_dbg(us, " command_set_1 = 0x%x\n", id[ATA_ID_COMMAND_SET_1]);
1097 usb_stor_dbg(us, " command_set_2 = 0x%x\n", id[ATA_ID_COMMAND_SET_2]);
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001098}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100/**************************************************************************
1101 * isd200_get_inquiry_data
1102 *
1103 * Get inquiry data
1104 *
1105 * RETURNS:
1106 * ISD status code
1107 */
1108static int isd200_get_inquiry_data( struct us_data *us )
1109{
1110 struct isd200_info *info = (struct isd200_info *)us->extra;
1111 int retStatus = ISD200_GOOD;
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001112 u16 *id = info->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Joe Perches191648d2013-04-19 11:44:00 -07001114 usb_stor_dbg(us, "Entering isd200_get_inquiry_data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 /* set default to Master */
1117 info->DeviceHead = ATA_ADDRESS_DEVHEAD_STD;
1118
1119 /* attempt to manually enumerate this device */
1120 retStatus = isd200_manual_enum(us);
1121 if (retStatus == ISD200_GOOD) {
1122 int transferStatus;
1123
1124 /* check for an ATA device */
1125 if (info->DeviceFlags & DF_ATA_DEVICE) {
1126 /* this must be an ATA device */
1127 /* perform an ATA Command Identify */
1128 transferStatus = isd200_action( us, ACTION_IDENTIFY,
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001129 id, ATA_ID_WORDS * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (transferStatus != ISD200_TRANSPORT_GOOD) {
1131 /* Error issuing ATA Command Identify */
Joe Perches191648d2013-04-19 11:44:00 -07001132 usb_stor_dbg(us, " Error issuing ATA Command Identify\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 retStatus = ISD200_ERROR;
1134 } else {
1135 /* ATA Command Identify successful */
1136 int i;
1137 __be16 *src;
1138 __u16 *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001140 isd200_fix_driveid(id);
Joe Perches191648d2013-04-19 11:44:00 -07001141 isd200_dump_driveid(us, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 memset(&info->InquiryData, 0, sizeof(info->InquiryData));
1144
1145 /* Standard IDE interface only supports disks */
1146 info->InquiryData.DeviceType = DIRECT_ACCESS_DEVICE;
1147
1148 /* The length must be at least 36 (5 + 31) */
1149 info->InquiryData.AdditionalLength = 0x1F;
1150
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001151 if (id[ATA_ID_COMMAND_SET_1] & COMMANDSET_MEDIA_STATUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 /* set the removable bit */
1153 info->InquiryData.DeviceTypeModifier = DEVICE_REMOVABLE;
1154 info->DeviceFlags |= DF_REMOVABLE_MEDIA;
1155 }
1156
1157 /* Fill in vendor identification fields */
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001158 src = (__be16 *)&id[ATA_ID_PROD];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 dest = (__u16*)info->InquiryData.VendorId;
1160 for (i=0;i<4;i++)
1161 dest[i] = be16_to_cpu(src[i]);
1162
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001163 src = (__be16 *)&id[ATA_ID_PROD + 8/2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 dest = (__u16*)info->InquiryData.ProductId;
1165 for (i=0;i<8;i++)
1166 dest[i] = be16_to_cpu(src[i]);
1167
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001168 src = (__be16 *)&id[ATA_ID_FW_REV];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 dest = (__u16*)info->InquiryData.ProductRevisionLevel;
1170 for (i=0;i<2;i++)
1171 dest[i] = be16_to_cpu(src[i]);
1172
1173 /* determine if it supports Media Status Notification */
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001174 if (id[ATA_ID_COMMAND_SET_2] & COMMANDSET_MEDIA_STATUS) {
Joe Perches191648d2013-04-19 11:44:00 -07001175 usb_stor_dbg(us, " Device supports Media Status Notification\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 /* Indicate that it is enabled, even though it is not
1178 * This allows the lock/unlock of the media to work
1179 * correctly.
1180 */
1181 info->DeviceFlags |= DF_MEDIA_STATUS_ENABLED;
1182 }
1183 else
1184 info->DeviceFlags &= ~DF_MEDIA_STATUS_ENABLED;
1185
1186 }
1187 } else {
1188 /*
1189 * this must be an ATAPI device
1190 * use an ATAPI protocol (Transparent SCSI)
1191 */
1192 us->protocol_name = "Transparent SCSI";
1193 us->proto_handler = usb_stor_transparent_scsi_command;
1194
Joe Perches191648d2013-04-19 11:44:00 -07001195 usb_stor_dbg(us, "Protocol changed to: %s\n",
1196 us->protocol_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 /* Free driver structure */
1199 us->extra_destructor(info);
Boaz Harroshba2ef3b2008-03-17 14:21:01 -07001200 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 us->extra = NULL;
1202 us->extra_destructor = NULL;
1203 }
1204 }
1205
Joe Perches191648d2013-04-19 11:44:00 -07001206 usb_stor_dbg(us, "Leaving isd200_get_inquiry_data %08X\n", retStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 return(retStatus);
1209}
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211/**************************************************************************
1212 * isd200_scsi_to_ata
1213 *
1214 * Translate SCSI commands to ATA commands.
1215 *
1216 * RETURNS:
1217 * 1 if the command needs to be sent to the transport layer
1218 * 0 otherwise
1219 */
1220static int isd200_scsi_to_ata(struct scsi_cmnd *srb, struct us_data *us,
1221 union ata_cdb * ataCdb)
1222{
1223 struct isd200_info *info = (struct isd200_info *)us->extra;
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001224 u16 *id = info->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 int sendToTransport = 1;
1226 unsigned char sectnum, head;
1227 unsigned short cylinder;
1228 unsigned long lba;
1229 unsigned long blockCount;
1230 unsigned char senseData[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1231
1232 memset(ataCdb, 0, sizeof(union ata_cdb));
1233
1234 /* SCSI Command */
1235 switch (srb->cmnd[0]) {
1236 case INQUIRY:
Joe Perches191648d2013-04-19 11:44:00 -07001237 usb_stor_dbg(us, " ATA OUT - INQUIRY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 /* copy InquiryData */
1240 usb_stor_set_xfer_buf((unsigned char *) &info->InquiryData,
1241 sizeof(info->InquiryData), srb);
1242 srb->result = SAM_STAT_GOOD;
1243 sendToTransport = 0;
1244 break;
1245
1246 case MODE_SENSE:
Joe Perches191648d2013-04-19 11:44:00 -07001247 usb_stor_dbg(us, " ATA OUT - SCSIOP_MODE_SENSE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 /* Initialize the return buffer */
1250 usb_stor_set_xfer_buf(senseData, sizeof(senseData), srb);
1251
1252 if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
1253 {
1254 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1255 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1256 ataCdb->generic.TransferBlockSize = 1;
1257 ataCdb->generic.RegisterSelect = REG_COMMAND;
1258 ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
Boaz Harrosh48c23d32007-08-22 19:10:45 +03001259 isd200_srb_set_bufflen(srb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 } else {
Joe Perches191648d2013-04-19 11:44:00 -07001261 usb_stor_dbg(us, " Media Status not supported, just report okay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 srb->result = SAM_STAT_GOOD;
1263 sendToTransport = 0;
1264 }
1265 break;
1266
1267 case TEST_UNIT_READY:
Joe Perches191648d2013-04-19 11:44:00 -07001268 usb_stor_dbg(us, " ATA OUT - SCSIOP_TEST_UNIT_READY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
1271 {
1272 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1273 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1274 ataCdb->generic.TransferBlockSize = 1;
1275 ataCdb->generic.RegisterSelect = REG_COMMAND;
1276 ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
Boaz Harrosh48c23d32007-08-22 19:10:45 +03001277 isd200_srb_set_bufflen(srb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 } else {
Joe Perches191648d2013-04-19 11:44:00 -07001279 usb_stor_dbg(us, " Media Status not supported, just report okay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 srb->result = SAM_STAT_GOOD;
1281 sendToTransport = 0;
1282 }
1283 break;
1284
1285 case READ_CAPACITY:
1286 {
1287 unsigned long capacity;
1288 struct read_capacity_data readCapacityData;
1289
Joe Perches191648d2013-04-19 11:44:00 -07001290 usb_stor_dbg(us, " ATA OUT - SCSIOP_READ_CAPACITY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001292 if (ata_id_has_lba(id))
1293 capacity = ata_id_u32(id, ATA_ID_LBA_CAPACITY) - 1;
1294 else
1295 capacity = (id[ATA_ID_HEADS] * id[ATA_ID_CYLS] *
1296 id[ATA_ID_SECTORS]) - 1;
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 readCapacityData.LogicalBlockAddress = cpu_to_be32(capacity);
1299 readCapacityData.BytesPerBlock = cpu_to_be32(0x200);
1300
1301 usb_stor_set_xfer_buf((unsigned char *) &readCapacityData,
1302 sizeof(readCapacityData), srb);
1303 srb->result = SAM_STAT_GOOD;
1304 sendToTransport = 0;
1305 }
1306 break;
1307
1308 case READ_10:
Joe Perches191648d2013-04-19 11:44:00 -07001309 usb_stor_dbg(us, " ATA OUT - SCSIOP_READ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1312 blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1313
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001314 if (ata_id_has_lba(id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 sectnum = (unsigned char)(lba);
1316 cylinder = (unsigned short)(lba>>8);
1317 head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1318 } else {
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001319 sectnum = (u8)((lba % id[ATA_ID_SECTORS]) + 1);
1320 cylinder = (u16)(lba / (id[ATA_ID_SECTORS] *
1321 id[ATA_ID_HEADS]));
1322 head = (u8)((lba / id[ATA_ID_SECTORS]) %
1323 id[ATA_ID_HEADS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1326 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1327 ataCdb->generic.TransferBlockSize = 1;
1328 ataCdb->generic.RegisterSelect =
1329 REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
1330 REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
1331 REG_DEVICE_HEAD | REG_COMMAND;
1332 ataCdb->write.SectorCountByte = (unsigned char)blockCount;
1333 ataCdb->write.SectorNumberByte = sectnum;
1334 ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
1335 ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
1336 ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
Bartlomiej Zolnierkiewiczf08f31f2009-04-01 21:42:24 +02001337 ataCdb->write.CommandByte = ATA_CMD_PIO_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 break;
1339
1340 case WRITE_10:
Joe Perches191648d2013-04-19 11:44:00 -07001341 usb_stor_dbg(us, " ATA OUT - SCSIOP_WRITE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1344 blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1345
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001346 if (ata_id_has_lba(id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 sectnum = (unsigned char)(lba);
1348 cylinder = (unsigned short)(lba>>8);
1349 head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1350 } else {
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001351 sectnum = (u8)((lba % id[ATA_ID_SECTORS]) + 1);
1352 cylinder = (u16)(lba / (id[ATA_ID_SECTORS] *
1353 id[ATA_ID_HEADS]));
1354 head = (u8)((lba / id[ATA_ID_SECTORS]) %
1355 id[ATA_ID_HEADS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1358 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1359 ataCdb->generic.TransferBlockSize = 1;
1360 ataCdb->generic.RegisterSelect =
1361 REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
1362 REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
1363 REG_DEVICE_HEAD | REG_COMMAND;
1364 ataCdb->write.SectorCountByte = (unsigned char)blockCount;
1365 ataCdb->write.SectorNumberByte = sectnum;
1366 ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
1367 ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
1368 ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
Bartlomiej Zolnierkiewiczf08f31f2009-04-01 21:42:24 +02001369 ataCdb->write.CommandByte = ATA_CMD_PIO_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 break;
1371
1372 case ALLOW_MEDIUM_REMOVAL:
Joe Perches191648d2013-04-19 11:44:00 -07001373 usb_stor_dbg(us, " ATA OUT - SCSIOP_MEDIUM_REMOVAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 if (info->DeviceFlags & DF_REMOVABLE_MEDIA) {
Joe Perches191648d2013-04-19 11:44:00 -07001376 usb_stor_dbg(us, " srb->cmnd[4] = 0x%X\n",
1377 srb->cmnd[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1380 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1381 ataCdb->generic.TransferBlockSize = 1;
1382 ataCdb->generic.RegisterSelect = REG_COMMAND;
1383 ataCdb->write.CommandByte = (srb->cmnd[4] & 0x1) ?
Bartlomiej Zolnierkiewiczf08f31f2009-04-01 21:42:24 +02001384 ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK;
Boaz Harrosh48c23d32007-08-22 19:10:45 +03001385 isd200_srb_set_bufflen(srb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 } else {
Joe Perches191648d2013-04-19 11:44:00 -07001387 usb_stor_dbg(us, " Not removeable media, just report okay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 srb->result = SAM_STAT_GOOD;
1389 sendToTransport = 0;
1390 }
1391 break;
1392
1393 case START_STOP:
Joe Perches191648d2013-04-19 11:44:00 -07001394 usb_stor_dbg(us, " ATA OUT - SCSIOP_START_STOP_UNIT\n");
1395 usb_stor_dbg(us, " srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 if ((srb->cmnd[4] & 0x3) == 0x2) {
Joe Perches191648d2013-04-19 11:44:00 -07001398 usb_stor_dbg(us, " Media Eject\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1400 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1401 ataCdb->generic.TransferBlockSize = 0;
1402 ataCdb->generic.RegisterSelect = REG_COMMAND;
1403 ataCdb->write.CommandByte = ATA_COMMAND_MEDIA_EJECT;
1404 } else if ((srb->cmnd[4] & 0x3) == 0x1) {
Joe Perches191648d2013-04-19 11:44:00 -07001405 usb_stor_dbg(us, " Get Media Status\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1407 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1408 ataCdb->generic.TransferBlockSize = 1;
1409 ataCdb->generic.RegisterSelect = REG_COMMAND;
1410 ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
Boaz Harrosh48c23d32007-08-22 19:10:45 +03001411 isd200_srb_set_bufflen(srb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 } else {
Joe Perches191648d2013-04-19 11:44:00 -07001413 usb_stor_dbg(us, " Nothing to do, just report okay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 srb->result = SAM_STAT_GOOD;
1415 sendToTransport = 0;
1416 }
1417 break;
1418
1419 default:
Joe Perches191648d2013-04-19 11:44:00 -07001420 usb_stor_dbg(us, "Unsupported SCSI command - 0x%X\n",
1421 srb->cmnd[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 srb->result = DID_ERROR << 16;
1423 sendToTransport = 0;
1424 break;
1425 }
1426
1427 return(sendToTransport);
1428}
1429
1430
1431/**************************************************************************
1432 * isd200_free_info
1433 *
1434 * Frees the driver structure.
1435 */
1436static void isd200_free_info_ptrs(void *info_)
1437{
1438 struct isd200_info *info = (struct isd200_info *) info_;
1439
1440 if (info) {
1441 kfree(info->id);
1442 kfree(info->RegsBuf);
Boaz Harroshe92e63a2008-03-12 19:20:09 +02001443 kfree(info->srb.sense_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445}
1446
1447/**************************************************************************
1448 * isd200_init_info
1449 *
1450 * Allocates (if necessary) and initializes the driver structure.
1451 *
1452 * RETURNS:
1453 * ISD status code
1454 */
1455static int isd200_init_info(struct us_data *us)
1456{
1457 int retStatus = ISD200_GOOD;
1458 struct isd200_info *info;
1459
Joe Perchesa5cc8042010-05-31 20:23:19 -07001460 info = kzalloc(sizeof(struct isd200_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (!info)
1462 retStatus = ISD200_ERROR;
1463 else {
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001464 info->id = kzalloc(ATA_ID_WORDS * 2, GFP_KERNEL);
Zhang Yanfei5b2750d2013-03-12 13:33:27 +08001465 info->RegsBuf = kmalloc(sizeof(info->ATARegs), GFP_KERNEL);
Boaz Harroshe92e63a2008-03-12 19:20:09 +02001466 info->srb.sense_buffer =
1467 kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
1468 if (!info->id || !info->RegsBuf || !info->srb.sense_buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 isd200_free_info_ptrs(info);
1470 kfree(info);
1471 retStatus = ISD200_ERROR;
Oliver Neukum887c2562006-01-08 12:33:45 +01001472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
1474
1475 if (retStatus == ISD200_GOOD) {
1476 us->extra = info;
1477 us->extra_destructor = isd200_free_info_ptrs;
Joe Perches191648d2013-04-19 11:44:00 -07001478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01001480 return retStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
1483/**************************************************************************
1484 * Initialization for the ISD200
1485 */
1486
Alan Stern32d54932009-02-12 14:47:54 -05001487static int isd200_Initialization(struct us_data *us)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Joe Perches191648d2013-04-19 11:44:00 -07001489 usb_stor_dbg(us, "ISD200 Initialization...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 /* Initialize ISD200 info struct */
1492
1493 if (isd200_init_info(us) == ISD200_ERROR) {
Joe Perches191648d2013-04-19 11:44:00 -07001494 usb_stor_dbg(us, "ERROR Initializing ISD200 Info struct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 } else {
1496 /* Get device specific data */
1497
1498 if (isd200_get_inquiry_data(us) != ISD200_GOOD)
Joe Perches191648d2013-04-19 11:44:00 -07001499 usb_stor_dbg(us, "ISD200 Initialization Failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 else
Joe Perches191648d2013-04-19 11:44:00 -07001501 usb_stor_dbg(us, "ISD200 Initialization complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503
1504 return 0;
1505}
1506
1507
1508/**************************************************************************
1509 * Protocol and Transport for the ISD200 ASIC
1510 *
1511 * This protocol and transport are for ATA devices connected to an ISD200
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001512 * ASIC. An ATAPI device that is connected as a slave device will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 * detected in the driver initialization function and the protocol will
1514 * be changed to an ATAPI protocol (Transparent SCSI).
1515 *
1516 */
1517
Alan Stern32d54932009-02-12 14:47:54 -05001518static void isd200_ata_command(struct scsi_cmnd *srb, struct us_data *us)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Boaz Harrosh48c23d32007-08-22 19:10:45 +03001520 int sendToTransport = 1, orig_bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 union ata_cdb ataCdb;
1522
1523 /* Make sure driver was initialized */
1524
1525 if (us->extra == NULL)
Joe Perches191648d2013-04-19 11:44:00 -07001526 usb_stor_dbg(us, "ERROR Driver not initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Boaz Harrosh48c23d32007-08-22 19:10:45 +03001528 scsi_set_resid(srb, 0);
1529 /* scsi_bufflen might change in protocol translation to ata */
1530 orig_bufflen = scsi_bufflen(srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 sendToTransport = isd200_scsi_to_ata(srb, us, &ataCdb);
1532
1533 /* send the command to the transport layer */
1534 if (sendToTransport)
1535 isd200_invoke_transport(us, srb, &ataCdb);
Boaz Harrosh48c23d32007-08-22 19:10:45 +03001536
1537 isd200_srb_set_bufflen(srb, orig_bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
Alan Stern32d54932009-02-12 14:47:54 -05001539
1540static int isd200_probe(struct usb_interface *intf,
1541 const struct usb_device_id *id)
1542{
1543 struct us_data *us;
1544 int result;
1545
1546 result = usb_stor_probe1(&us, intf, id,
1547 (id - isd200_usb_ids) + isd200_unusual_dev_list);
1548 if (result)
1549 return result;
1550
1551 us->protocol_name = "ISD200 ATA/ATAPI";
1552 us->proto_handler = isd200_ata_command;
1553
1554 result = usb_stor_probe2(us);
1555 return result;
1556}
1557
1558static struct usb_driver isd200_driver = {
1559 .name = "ums-isd200",
1560 .probe = isd200_probe,
1561 .disconnect = usb_stor_disconnect,
1562 .suspend = usb_stor_suspend,
1563 .resume = usb_stor_resume,
1564 .reset_resume = usb_stor_reset_resume,
1565 .pre_reset = usb_stor_pre_reset,
1566 .post_reset = usb_stor_post_reset,
1567 .id_table = isd200_usb_ids,
1568 .soft_unbind = 1,
Huajun Lie73b2db2012-01-14 10:15:21 +08001569 .no_dynamic_id = 1,
Alan Stern32d54932009-02-12 14:47:54 -05001570};
1571
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001572module_usb_driver(isd200_driver);