blob: 1bac215202d20b0783890b28d9dbef886a5af35b [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
Akinobu Mitaaa519be2015-05-06 18:24:21 +090063#define DRV_NAME "ums-isd200"
64
Maciej Grela4246b062009-02-28 12:39:20 -080065MODULE_DESCRIPTION("Driver for In-System Design, Inc. ISD200 ASIC");
Al Virod36b6912011-12-29 17:09:01 -050066MODULE_AUTHOR("Björn Stenberg <bjorn@haxx.se>");
Maciej Grela4246b062009-02-28 12:39:20 -080067MODULE_LICENSE("GPL");
Alan Stern32d54932009-02-12 14:47:54 -050068
69static int isd200_Initialization(struct us_data *us);
70
71
72/*
73 * The table of devices
74 */
75#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
76 vendorName, productName, useProtocol, useTransport, \
77 initFunction, flags) \
78{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
Sebastian Andrzej Siewiorf61870e2012-08-28 22:37:13 +020079 .driver_info = (flags) }
Alan Stern32d54932009-02-12 14:47:54 -050080
Felipe Balbie7724ba2011-11-15 09:53:33 +020081static struct usb_device_id isd200_usb_ids[] = {
Alan Stern32d54932009-02-12 14:47:54 -050082# include "unusual_isd200.h"
83 { } /* Terminating entry */
84};
85MODULE_DEVICE_TABLE(usb, isd200_usb_ids);
86
87#undef UNUSUAL_DEV
Alan Stern32d54932009-02-12 14:47:54 -050088
89/*
90 * The flags table
91 */
92#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
93 vendor_name, product_name, use_protocol, use_transport, \
94 init_function, Flags) \
95{ \
96 .vendorName = vendor_name, \
97 .productName = product_name, \
98 .useProtocol = use_protocol, \
99 .useTransport = use_transport, \
100 .initFunction = init_function, \
101}
102
103static struct us_unusual_dev isd200_unusual_dev_list[] = {
104# include "unusual_isd200.h"
105 { } /* Terminating entry */
106};
107
108#undef UNUSUAL_DEV
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110/* Timeout defines (in Seconds) */
111
112#define ISD200_ENUM_BSY_TIMEOUT 35
113#define ISD200_ENUM_DETECT_TIMEOUT 30
114#define ISD200_DEFAULT_TIMEOUT 30
115
116/* device flags */
117#define DF_ATA_DEVICE 0x0001
118#define DF_MEDIA_STATUS_ENABLED 0x0002
119#define DF_REMOVABLE_MEDIA 0x0004
120
121/* capability bit definitions */
122#define CAPABILITY_DMA 0x01
123#define CAPABILITY_LBA 0x02
124
125/* command_setX bit definitions */
126#define COMMANDSET_REMOVABLE 0x02
127#define COMMANDSET_MEDIA_STATUS 0x10
128
129/* ATA Vendor Specific defines */
130#define ATA_ADDRESS_DEVHEAD_STD 0xa0
131#define ATA_ADDRESS_DEVHEAD_LBA_MODE 0x40
132#define ATA_ADDRESS_DEVHEAD_SLAVE 0x10
133
134/* Action Select bits */
135#define ACTION_SELECT_0 0x01
136#define ACTION_SELECT_1 0x02
137#define ACTION_SELECT_2 0x04
138#define ACTION_SELECT_3 0x08
139#define ACTION_SELECT_4 0x10
140#define ACTION_SELECT_5 0x20
141#define ACTION_SELECT_6 0x40
142#define ACTION_SELECT_7 0x80
143
144/* Register Select bits */
145#define REG_ALTERNATE_STATUS 0x01
146#define REG_DEVICE_CONTROL 0x01
147#define REG_ERROR 0x02
148#define REG_FEATURES 0x02
149#define REG_SECTOR_COUNT 0x04
150#define REG_SECTOR_NUMBER 0x08
151#define REG_CYLINDER_LOW 0x10
152#define REG_CYLINDER_HIGH 0x20
153#define REG_DEVICE_HEAD 0x40
154#define REG_STATUS 0x80
155#define REG_COMMAND 0x80
156
Bartlomiej Zolnierkiewiczc74e8092007-10-27 18:01:04 +0200157/* ATA registers offset definitions */
158#define ATA_REG_ERROR_OFFSET 1
159#define ATA_REG_LCYL_OFFSET 4
160#define ATA_REG_HCYL_OFFSET 5
161#define ATA_REG_STATUS_OFFSET 7
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* ATA error definitions not in <linux/hdreg.h> */
164#define ATA_ERROR_MEDIA_CHANGE 0x20
165
166/* ATA command definitions not in <linux/hdreg.h> */
167#define ATA_COMMAND_GET_MEDIA_STATUS 0xDA
168#define ATA_COMMAND_MEDIA_EJECT 0xED
169
170/* ATA drive control definitions */
171#define ATA_DC_DISABLE_INTERRUPTS 0x02
172#define ATA_DC_RESET_CONTROLLER 0x04
173#define ATA_DC_REENABLE_CONTROLLER 0x00
174
175/*
176 * General purpose return codes
177 */
178
179#define ISD200_ERROR -1
180#define ISD200_GOOD 0
181
182/*
183 * Transport return codes
184 */
185
186#define ISD200_TRANSPORT_GOOD 0 /* Transport good, command good */
187#define ISD200_TRANSPORT_FAILED 1 /* Transport good, command failed */
188#define ISD200_TRANSPORT_ERROR 2 /* Transport bad (i.e. device dead) */
189
190/* driver action codes */
191#define ACTION_READ_STATUS 0
192#define ACTION_RESET 1
193#define ACTION_REENABLE 2
194#define ACTION_SOFT_RESET 3
195#define ACTION_ENUM 4
196#define ACTION_IDENTIFY 5
197
198
199/*
200 * ata_cdb struct
201 */
202
203
204union ata_cdb {
205 struct {
206 unsigned char SignatureByte0;
207 unsigned char SignatureByte1;
208 unsigned char ActionSelect;
209 unsigned char RegisterSelect;
210 unsigned char TransferBlockSize;
211 unsigned char WriteData3F6;
212 unsigned char WriteData1F1;
213 unsigned char WriteData1F2;
214 unsigned char WriteData1F3;
215 unsigned char WriteData1F4;
216 unsigned char WriteData1F5;
217 unsigned char WriteData1F6;
218 unsigned char WriteData1F7;
219 unsigned char Reserved[3];
220 } generic;
221
222 struct {
223 unsigned char SignatureByte0;
224 unsigned char SignatureByte1;
225 unsigned char ActionSelect;
226 unsigned char RegisterSelect;
227 unsigned char TransferBlockSize;
228 unsigned char AlternateStatusByte;
229 unsigned char ErrorByte;
230 unsigned char SectorCountByte;
231 unsigned char SectorNumberByte;
232 unsigned char CylinderLowByte;
233 unsigned char CylinderHighByte;
234 unsigned char DeviceHeadByte;
235 unsigned char StatusByte;
236 unsigned char Reserved[3];
237 } read;
238
239 struct {
240 unsigned char SignatureByte0;
241 unsigned char SignatureByte1;
242 unsigned char ActionSelect;
243 unsigned char RegisterSelect;
244 unsigned char TransferBlockSize;
245 unsigned char DeviceControlByte;
246 unsigned char FeaturesByte;
247 unsigned char SectorCountByte;
248 unsigned char SectorNumberByte;
249 unsigned char CylinderLowByte;
250 unsigned char CylinderHighByte;
251 unsigned char DeviceHeadByte;
252 unsigned char CommandByte;
253 unsigned char Reserved[3];
254 } write;
255};
256
257
258/*
259 * Inquiry data structure. This is the data returned from the target
260 * after it receives an inquiry.
261 *
262 * This structure may be extended by the number of bytes specified
263 * in the field AdditionalLength. The defined size constant only
264 * includes fields through ProductRevisionLevel.
265 */
266
267/*
268 * DeviceType field
269 */
270#define DIRECT_ACCESS_DEVICE 0x00 /* disks */
271#define DEVICE_REMOVABLE 0x80
272
273struct inquiry_data {
274 unsigned char DeviceType;
275 unsigned char DeviceTypeModifier;
276 unsigned char Versions;
277 unsigned char Format;
278 unsigned char AdditionalLength;
279 unsigned char Reserved[2];
280 unsigned char Capability;
281 unsigned char VendorId[8];
282 unsigned char ProductId[16];
283 unsigned char ProductRevisionLevel[4];
284 unsigned char VendorSpecific[20];
285 unsigned char Reserved3[40];
286} __attribute__ ((packed));
287
288/*
289 * INQUIRY data buffer size
290 */
291
292#define INQUIRYDATABUFFERSIZE 36
293
294
295/*
296 * ISD200 CONFIG data struct
297 */
298
299#define ATACFG_TIMING 0x0f
300#define ATACFG_ATAPI_RESET 0x10
301#define ATACFG_MASTER 0x20
302#define ATACFG_BLOCKSIZE 0xa0
303
304#define ATACFGE_LAST_LUN 0x07
305#define ATACFGE_DESC_OVERRIDE 0x08
306#define ATACFGE_STATE_SUSPEND 0x10
307#define ATACFGE_SKIP_BOOT 0x20
308#define ATACFGE_CONF_DESC2 0x40
309#define ATACFGE_INIT_STATUS 0x80
310
311#define CFG_CAPABILITY_SRST 0x01
312
313struct isd200_config {
314 unsigned char EventNotification;
315 unsigned char ExternalClock;
316 unsigned char ATAInitTimeout;
317 unsigned char ATAConfig;
318 unsigned char ATAMajorCommand;
319 unsigned char ATAMinorCommand;
320 unsigned char ATAExtraConfig;
321 unsigned char Capability;
322}__attribute__ ((packed));
323
324
325/*
326 * ISD200 driver information struct
327 */
328
329struct isd200_info {
330 struct inquiry_data InquiryData;
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +0200331 u16 *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct isd200_config ConfigData;
333 unsigned char *RegsBuf;
334 unsigned char ATARegs[8];
335 unsigned char DeviceHead;
336 unsigned char DeviceFlags;
337
338 /* maximum number of LUNs supported */
339 unsigned char MaxLUNs;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300340 unsigned char cmnd[BLK_MAX_CDB];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 struct scsi_cmnd srb;
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300342 struct scatterlist sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343};
344
345
346/*
347 * Read Capacity Data - returned in Big Endian format
348 */
349
350struct read_capacity_data {
351 __be32 LogicalBlockAddress;
352 __be32 BytesPerBlock;
353};
354
355/*
356 * Read Block Limits Data - returned in Big Endian format
357 * This structure returns the maximum and minimum block
358 * size for a TAPE device.
359 */
360
361struct read_block_limits {
362 unsigned char Reserved;
363 unsigned char BlockMaximumSize[3];
364 unsigned char BlockMinimumSize[2];
365};
366
367
368/*
369 * Sense Data Format
370 */
371
372#define SENSE_ERRCODE 0x7f
373#define SENSE_ERRCODE_VALID 0x80
374#define SENSE_FLAG_SENSE_KEY 0x0f
375#define SENSE_FLAG_BAD_LENGTH 0x20
376#define SENSE_FLAG_END_OF_MEDIA 0x40
377#define SENSE_FLAG_FILE_MARK 0x80
378struct sense_data {
379 unsigned char ErrorCode;
380 unsigned char SegmentNumber;
381 unsigned char Flags;
382 unsigned char Information[4];
383 unsigned char AdditionalSenseLength;
384 unsigned char CommandSpecificInformation[4];
385 unsigned char AdditionalSenseCode;
386 unsigned char AdditionalSenseCodeQualifier;
387 unsigned char FieldReplaceableUnitCode;
388 unsigned char SenseKeySpecific[3];
389} __attribute__ ((packed));
390
391/*
392 * Default request sense buffer size
393 */
394
395#define SENSE_BUFFER_SIZE 18
396
397/***********************************************************************
398 * Helper routines
399 ***********************************************************************/
400
401/**************************************************************************
402 * isd200_build_sense
403 *
404 * Builds an artificial sense buffer to report the results of a
405 * failed command.
406 *
407 * RETURNS:
408 * void
409 */
410static void isd200_build_sense(struct us_data *us, struct scsi_cmnd *srb)
411{
412 struct isd200_info *info = (struct isd200_info *)us->extra;
413 struct sense_data *buf = (struct sense_data *) &srb->sense_buffer[0];
Bartlomiej Zolnierkiewiczc74e8092007-10-27 18:01:04 +0200414 unsigned char error = info->ATARegs[ATA_REG_ERROR_OFFSET];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 if(error & ATA_ERROR_MEDIA_CHANGE) {
417 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
418 buf->AdditionalSenseLength = 0xb;
419 buf->Flags = UNIT_ATTENTION;
420 buf->AdditionalSenseCode = 0;
421 buf->AdditionalSenseCodeQualifier = 0;
Bartlomiej Zolnierkiewiczfcd3ec62009-04-01 21:42:25 +0200422 } else if (error & ATA_MCR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
424 buf->AdditionalSenseLength = 0xb;
425 buf->Flags = UNIT_ATTENTION;
426 buf->AdditionalSenseCode = 0;
427 buf->AdditionalSenseCodeQualifier = 0;
Bartlomiej Zolnierkiewiczfcd3ec62009-04-01 21:42:25 +0200428 } else if (error & ATA_TRK0NF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
430 buf->AdditionalSenseLength = 0xb;
431 buf->Flags = NOT_READY;
432 buf->AdditionalSenseCode = 0;
433 buf->AdditionalSenseCodeQualifier = 0;
Bartlomiej Zolnierkiewiczfcd3ec62009-04-01 21:42:25 +0200434 } else if (error & ATA_UNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
436 buf->AdditionalSenseLength = 0xb;
437 buf->Flags = DATA_PROTECT;
438 buf->AdditionalSenseCode = 0;
439 buf->AdditionalSenseCodeQualifier = 0;
440 } else {
441 buf->ErrorCode = 0;
442 buf->AdditionalSenseLength = 0;
443 buf->Flags = 0;
444 buf->AdditionalSenseCode = 0;
445 buf->AdditionalSenseCodeQualifier = 0;
446 }
447}
448
449
450/***********************************************************************
451 * Transport routines
452 ***********************************************************************/
453
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300454/**************************************************************************
455 * isd200_set_srb(), isd200_srb_set_bufflen()
456 *
457 * Two helpers to facilitate in initialization of scsi_cmnd structure
458 * Will need to change when struct scsi_cmnd changes
459 */
460static void isd200_set_srb(struct isd200_info *info,
461 enum dma_data_direction dir, void* buff, unsigned bufflen)
462{
463 struct scsi_cmnd *srb = &info->srb;
464
465 if (buff)
466 sg_init_one(&info->sg, buff, bufflen);
467
468 srb->sc_data_direction = dir;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200469 srb->sdb.table.sgl = buff ? &info->sg : NULL;
470 srb->sdb.length = bufflen;
471 srb->sdb.table.nents = buff ? 1 : 0;
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300472}
473
474static void isd200_srb_set_bufflen(struct scsi_cmnd *srb, unsigned bufflen)
475{
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200476 srb->sdb.length = bufflen;
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480/**************************************************************************
481 * isd200_action
482 *
483 * Routine for sending commands to the isd200
484 *
485 * RETURNS:
486 * ISD status code
487 */
488static int isd200_action( struct us_data *us, int action,
489 void* pointer, int value )
490{
491 union ata_cdb ata;
Ian Munsie0df4d8e2010-03-03 11:00:37 +1100492 /* static to prevent this large struct being placed on the valuable stack */
493 static struct scsi_device srb_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 struct isd200_info *info = (struct isd200_info *)us->extra;
495 struct scsi_cmnd *srb = &info->srb;
496 int status;
497
498 memset(&ata, 0, sizeof(ata));
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300499 srb->cmnd = info->cmnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 srb->device = &srb_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 ata.generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
503 ata.generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
504 ata.generic.TransferBlockSize = 1;
505
506 switch ( action ) {
507 case ACTION_READ_STATUS:
Joe Perches191648d2013-04-19 11:44:00 -0700508 usb_stor_dbg(us, " isd200_action(READ_STATUS)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 ata.generic.ActionSelect = ACTION_SELECT_0|ACTION_SELECT_2;
510 ata.generic.RegisterSelect =
511 REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
512 REG_STATUS | REG_ERROR;
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300513 isd200_set_srb(info, DMA_FROM_DEVICE, pointer, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 break;
515
516 case ACTION_ENUM:
Joe Perches191648d2013-04-19 11:44:00 -0700517 usb_stor_dbg(us, " isd200_action(ENUM,0x%02x)\n", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
519 ACTION_SELECT_3|ACTION_SELECT_4|
520 ACTION_SELECT_5;
521 ata.generic.RegisterSelect = REG_DEVICE_HEAD;
522 ata.write.DeviceHeadByte = value;
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300523 isd200_set_srb(info, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 break;
525
526 case ACTION_RESET:
Joe Perches191648d2013-04-19 11:44:00 -0700527 usb_stor_dbg(us, " isd200_action(RESET)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
529 ACTION_SELECT_3|ACTION_SELECT_4;
530 ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
531 ata.write.DeviceControlByte = ATA_DC_RESET_CONTROLLER;
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300532 isd200_set_srb(info, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 break;
534
535 case ACTION_REENABLE:
Joe Perches191648d2013-04-19 11:44:00 -0700536 usb_stor_dbg(us, " isd200_action(REENABLE)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
538 ACTION_SELECT_3|ACTION_SELECT_4;
539 ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
540 ata.write.DeviceControlByte = ATA_DC_REENABLE_CONTROLLER;
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300541 isd200_set_srb(info, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 break;
543
544 case ACTION_SOFT_RESET:
Joe Perches191648d2013-04-19 11:44:00 -0700545 usb_stor_dbg(us, " isd200_action(SOFT_RESET)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_5;
547 ata.generic.RegisterSelect = REG_DEVICE_HEAD | REG_COMMAND;
548 ata.write.DeviceHeadByte = info->DeviceHead;
Bartlomiej Zolnierkiewiczf08f31f2009-04-01 21:42:24 +0200549 ata.write.CommandByte = ATA_CMD_DEV_RESET;
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300550 isd200_set_srb(info, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 break;
552
553 case ACTION_IDENTIFY:
Joe Perches191648d2013-04-19 11:44:00 -0700554 usb_stor_dbg(us, " isd200_action(IDENTIFY)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 ata.generic.RegisterSelect = REG_COMMAND;
Bartlomiej Zolnierkiewiczf08f31f2009-04-01 21:42:24 +0200556 ata.write.CommandByte = ATA_CMD_ID_ATA;
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300557 isd200_set_srb(info, DMA_FROM_DEVICE, info->id,
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +0200558 ATA_ID_WORDS * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 break;
560
561 default:
Joe Perches191648d2013-04-19 11:44:00 -0700562 usb_stor_dbg(us, "Error: Undefined action %d\n", action);
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300563 return ISD200_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565
566 memcpy(srb->cmnd, &ata, sizeof(ata.generic));
567 srb->cmd_len = sizeof(ata.generic);
568 status = usb_stor_Bulk_transport(srb, us);
569 if (status == USB_STOR_TRANSPORT_GOOD)
570 status = ISD200_GOOD;
571 else {
Joe Perches191648d2013-04-19 11:44:00 -0700572 usb_stor_dbg(us, " isd200_action(0x%02x) error: %d\n",
573 action, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 status = ISD200_ERROR;
575 /* need to reset device here */
576 }
577
578 return status;
579}
580
581/**************************************************************************
582 * isd200_read_regs
583 *
584 * Read ATA Registers
585 *
586 * RETURNS:
587 * ISD status code
588 */
589static int isd200_read_regs( struct us_data *us )
590{
591 struct isd200_info *info = (struct isd200_info *)us->extra;
592 int retStatus = ISD200_GOOD;
593 int transferStatus;
594
Joe Perches191648d2013-04-19 11:44:00 -0700595 usb_stor_dbg(us, "Entering isd200_IssueATAReadRegs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 transferStatus = isd200_action( us, ACTION_READ_STATUS,
598 info->RegsBuf, sizeof(info->ATARegs) );
599 if (transferStatus != ISD200_TRANSPORT_GOOD) {
Joe Perches191648d2013-04-19 11:44:00 -0700600 usb_stor_dbg(us, " Error reading ATA registers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 retStatus = ISD200_ERROR;
602 } else {
603 memcpy(info->ATARegs, info->RegsBuf, sizeof(info->ATARegs));
Joe Perches191648d2013-04-19 11:44:00 -0700604 usb_stor_dbg(us, " Got ATA Register[ATA_REG_ERROR_OFFSET] = 0x%x\n",
605 info->ATARegs[ATA_REG_ERROR_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
608 return retStatus;
609}
610
611
612/**************************************************************************
613 * Invoke the transport and basic error-handling/recovery methods
614 *
615 * This is used by the protocol layers to actually send the message to
616 * the device and receive the response.
617 */
618static void isd200_invoke_transport( struct us_data *us,
619 struct scsi_cmnd *srb,
620 union ata_cdb *ataCdb )
621{
622 int need_auto_sense = 0;
623 int transferStatus;
624 int result;
625
626 /* send the command to the transport layer */
627 memcpy(srb->cmnd, ataCdb, sizeof(ataCdb->generic));
628 srb->cmd_len = sizeof(ataCdb->generic);
629 transferStatus = usb_stor_Bulk_transport(srb, us);
630
631 /* if the command gets aborted by the higher layers, we need to
632 * short-circuit all other processing
633 */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400634 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
Joe Perches191648d2013-04-19 11:44:00 -0700635 usb_stor_dbg(us, "-- command was aborted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 goto Handle_Abort;
637 }
638
639 switch (transferStatus) {
640
641 case USB_STOR_TRANSPORT_GOOD:
642 /* Indicate a good result */
643 srb->result = SAM_STAT_GOOD;
644 break;
645
646 case USB_STOR_TRANSPORT_NO_SENSE:
Joe Perches191648d2013-04-19 11:44:00 -0700647 usb_stor_dbg(us, "-- transport indicates protocol failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 srb->result = SAM_STAT_CHECK_CONDITION;
649 return;
650
651 case USB_STOR_TRANSPORT_FAILED:
Joe Perches191648d2013-04-19 11:44:00 -0700652 usb_stor_dbg(us, "-- transport indicates command failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 need_auto_sense = 1;
654 break;
655
656 case USB_STOR_TRANSPORT_ERROR:
Joe Perches191648d2013-04-19 11:44:00 -0700657 usb_stor_dbg(us, "-- transport indicates transport error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 srb->result = DID_ERROR << 16;
659 /* Need reset here */
660 return;
661
662 default:
Joe Perches191648d2013-04-19 11:44:00 -0700663 usb_stor_dbg(us, "-- transport indicates unknown error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 srb->result = DID_ERROR << 16;
665 /* Need reset here */
666 return;
667 }
668
Boaz Harrosh48c23d32007-08-22 19:10:45 +0300669 if ((scsi_get_resid(srb) > 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 !((srb->cmnd[0] == REQUEST_SENSE) ||
671 (srb->cmnd[0] == INQUIRY) ||
672 (srb->cmnd[0] == MODE_SENSE) ||
673 (srb->cmnd[0] == LOG_SENSE) ||
674 (srb->cmnd[0] == MODE_SENSE_10))) {
Joe Perches191648d2013-04-19 11:44:00 -0700675 usb_stor_dbg(us, "-- unexpectedly short transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 need_auto_sense = 1;
677 }
678
679 if (need_auto_sense) {
680 result = isd200_read_regs(us);
Alan Stern7e4d6c32008-05-01 15:35:18 -0400681 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
Joe Perches191648d2013-04-19 11:44:00 -0700682 usb_stor_dbg(us, "-- auto-sense aborted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 goto Handle_Abort;
684 }
685 if (result == ISD200_GOOD) {
686 isd200_build_sense(us, srb);
687 srb->result = SAM_STAT_CHECK_CONDITION;
688
689 /* If things are really okay, then let's show that */
690 if ((srb->sense_buffer[2] & 0xf) == 0x0)
691 srb->result = SAM_STAT_GOOD;
692 } else {
693 srb->result = DID_ERROR << 16;
694 /* Need reset here */
695 }
696 }
697
698 /* Regardless of auto-sense, if we _know_ we have an error
699 * condition, show that in the result code
700 */
701 if (transferStatus == USB_STOR_TRANSPORT_FAILED)
702 srb->result = SAM_STAT_CHECK_CONDITION;
703 return;
704
705 /* abort processing: the bulk-only transport requires a reset
706 * following an abort */
707 Handle_Abort:
708 srb->result = DID_ABORT << 16;
709
710 /* permit the reset transfer to take place */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400711 clear_bit(US_FLIDX_ABORTING, &us->dflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* Need reset here */
713}
714
715#ifdef CONFIG_USB_STORAGE_DEBUG
Joe Perches191648d2013-04-19 11:44:00 -0700716static void isd200_log_config(struct us_data *us, struct isd200_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Joe Perches191648d2013-04-19 11:44:00 -0700718 usb_stor_dbg(us, " Event Notification: 0x%x\n",
719 info->ConfigData.EventNotification);
720 usb_stor_dbg(us, " External Clock: 0x%x\n",
721 info->ConfigData.ExternalClock);
722 usb_stor_dbg(us, " ATA Init Timeout: 0x%x\n",
723 info->ConfigData.ATAInitTimeout);
724 usb_stor_dbg(us, " ATAPI Command Block Size: 0x%x\n",
725 (info->ConfigData.ATAConfig & ATACFG_BLOCKSIZE) >> 6);
726 usb_stor_dbg(us, " Master/Slave Selection: 0x%x\n",
727 info->ConfigData.ATAConfig & ATACFG_MASTER);
728 usb_stor_dbg(us, " ATAPI Reset: 0x%x\n",
729 info->ConfigData.ATAConfig & ATACFG_ATAPI_RESET);
730 usb_stor_dbg(us, " ATA Timing: 0x%x\n",
731 info->ConfigData.ATAConfig & ATACFG_TIMING);
732 usb_stor_dbg(us, " ATA Major Command: 0x%x\n",
733 info->ConfigData.ATAMajorCommand);
734 usb_stor_dbg(us, " ATA Minor Command: 0x%x\n",
735 info->ConfigData.ATAMinorCommand);
736 usb_stor_dbg(us, " Init Status: 0x%x\n",
737 info->ConfigData.ATAExtraConfig & ATACFGE_INIT_STATUS);
738 usb_stor_dbg(us, " Config Descriptor 2: 0x%x\n",
739 info->ConfigData.ATAExtraConfig & ATACFGE_CONF_DESC2);
740 usb_stor_dbg(us, " Skip Device Boot: 0x%x\n",
741 info->ConfigData.ATAExtraConfig & ATACFGE_SKIP_BOOT);
Yannick Guerrini333c65b2015-02-24 16:42:45 +0100742 usb_stor_dbg(us, " ATA 3 State Suspend: 0x%x\n",
Joe Perches191648d2013-04-19 11:44:00 -0700743 info->ConfigData.ATAExtraConfig & ATACFGE_STATE_SUSPEND);
744 usb_stor_dbg(us, " Descriptor Override: 0x%x\n",
745 info->ConfigData.ATAExtraConfig & ATACFGE_DESC_OVERRIDE);
746 usb_stor_dbg(us, " Last LUN Identifier: 0x%x\n",
747 info->ConfigData.ATAExtraConfig & ATACFGE_LAST_LUN);
748 usb_stor_dbg(us, " SRST Enable: 0x%x\n",
749 info->ConfigData.ATAExtraConfig & CFG_CAPABILITY_SRST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751#endif
752
753/**************************************************************************
754 * isd200_write_config
755 *
756 * Write the ISD200 Configuration data
757 *
758 * RETURNS:
759 * ISD status code
760 */
761static int isd200_write_config( struct us_data *us )
762{
763 struct isd200_info *info = (struct isd200_info *)us->extra;
764 int retStatus = ISD200_GOOD;
765 int result;
766
767#ifdef CONFIG_USB_STORAGE_DEBUG
Joe Perches191648d2013-04-19 11:44:00 -0700768 usb_stor_dbg(us, "Entering isd200_write_config\n");
769 usb_stor_dbg(us, " Writing the following ISD200 Config Data:\n");
770 isd200_log_config(us, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771#endif
772
773 /* let's send the command via the control pipe */
774 result = usb_stor_ctrl_transfer(
775 us,
776 us->send_ctrl_pipe,
777 0x01,
778 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
779 0x0000,
780 0x0002,
781 (void *) &info->ConfigData,
782 sizeof(info->ConfigData));
783
784 if (result >= 0) {
Joe Perches191648d2013-04-19 11:44:00 -0700785 usb_stor_dbg(us, " ISD200 Config Data was written successfully\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 } else {
Joe Perches191648d2013-04-19 11:44:00 -0700787 usb_stor_dbg(us, " Request to write ISD200 Config Data failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 retStatus = ISD200_ERROR;
789 }
790
Joe Perches191648d2013-04-19 11:44:00 -0700791 usb_stor_dbg(us, "Leaving isd200_write_config %08X\n", retStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return retStatus;
793}
794
795
796/**************************************************************************
797 * isd200_read_config
798 *
799 * Reads the ISD200 Configuration data
800 *
801 * RETURNS:
802 * ISD status code
803 */
804static int isd200_read_config( struct us_data *us )
805{
806 struct isd200_info *info = (struct isd200_info *)us->extra;
807 int retStatus = ISD200_GOOD;
808 int result;
809
Joe Perches191648d2013-04-19 11:44:00 -0700810 usb_stor_dbg(us, "Entering isd200_read_config\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 /* read the configuration information from ISD200. Use this to */
813 /* determine what the special ATA CDB bytes are. */
814
815 result = usb_stor_ctrl_transfer(
816 us,
817 us->recv_ctrl_pipe,
818 0x02,
819 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
820 0x0000,
821 0x0002,
822 (void *) &info->ConfigData,
823 sizeof(info->ConfigData));
824
825
826 if (result >= 0) {
Joe Perches191648d2013-04-19 11:44:00 -0700827 usb_stor_dbg(us, " Retrieved the following ISD200 Config Data:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828#ifdef CONFIG_USB_STORAGE_DEBUG
Joe Perches191648d2013-04-19 11:44:00 -0700829 isd200_log_config(us, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830#endif
831 } else {
Joe Perches191648d2013-04-19 11:44:00 -0700832 usb_stor_dbg(us, " Request to get ISD200 Config Data failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 retStatus = ISD200_ERROR;
834 }
835
Joe Perches191648d2013-04-19 11:44:00 -0700836 usb_stor_dbg(us, "Leaving isd200_read_config %08X\n", retStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return retStatus;
838}
839
840
841/**************************************************************************
842 * isd200_atapi_soft_reset
843 *
844 * Perform an Atapi Soft Reset on the device
845 *
846 * RETURNS:
847 * NT status code
848 */
849static int isd200_atapi_soft_reset( struct us_data *us )
850{
851 int retStatus = ISD200_GOOD;
852 int transferStatus;
853
Joe Perches191648d2013-04-19 11:44:00 -0700854 usb_stor_dbg(us, "Entering isd200_atapi_soft_reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 transferStatus = isd200_action( us, ACTION_SOFT_RESET, NULL, 0 );
857 if (transferStatus != ISD200_TRANSPORT_GOOD) {
Joe Perches191648d2013-04-19 11:44:00 -0700858 usb_stor_dbg(us, " Error issuing Atapi Soft Reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 retStatus = ISD200_ERROR;
860 }
861
Joe Perches191648d2013-04-19 11:44:00 -0700862 usb_stor_dbg(us, "Leaving isd200_atapi_soft_reset %08X\n", retStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return retStatus;
864}
865
866
867/**************************************************************************
868 * isd200_srst
869 *
870 * Perform an SRST on the device
871 *
872 * RETURNS:
873 * ISD status code
874 */
875static int isd200_srst( struct us_data *us )
876{
877 int retStatus = ISD200_GOOD;
878 int transferStatus;
879
Joe Perches191648d2013-04-19 11:44:00 -0700880 usb_stor_dbg(us, "Entering isd200_SRST\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 transferStatus = isd200_action( us, ACTION_RESET, NULL, 0 );
883
884 /* check to see if this request failed */
885 if (transferStatus != ISD200_TRANSPORT_GOOD) {
Joe Perches191648d2013-04-19 11:44:00 -0700886 usb_stor_dbg(us, " Error issuing SRST\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 retStatus = ISD200_ERROR;
888 } else {
889 /* delay 10ms to give the drive a chance to see it */
890 msleep(10);
891
892 transferStatus = isd200_action( us, ACTION_REENABLE, NULL, 0 );
893 if (transferStatus != ISD200_TRANSPORT_GOOD) {
Joe Perches191648d2013-04-19 11:44:00 -0700894 usb_stor_dbg(us, " Error taking drive out of reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 retStatus = ISD200_ERROR;
896 } else {
897 /* delay 50ms to give the drive a chance to recover after SRST */
898 msleep(50);
899 }
900 }
901
Joe Perches191648d2013-04-19 11:44:00 -0700902 usb_stor_dbg(us, "Leaving isd200_srst %08X\n", retStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return retStatus;
904}
905
906
907/**************************************************************************
908 * isd200_try_enum
909 *
910 * Helper function for isd200_manual_enum(). Does ENUM and READ_STATUS
911 * and tries to analyze the status registers
912 *
913 * RETURNS:
914 * ISD status code
915 */
916static int isd200_try_enum(struct us_data *us, unsigned char master_slave,
917 int detect )
918{
919 int status = ISD200_GOOD;
920 unsigned long endTime;
921 struct isd200_info *info = (struct isd200_info *)us->extra;
922 unsigned char *regs = info->RegsBuf;
923 int recheckAsMaster = 0;
924
925 if ( detect )
926 endTime = jiffies + ISD200_ENUM_DETECT_TIMEOUT * HZ;
927 else
928 endTime = jiffies + ISD200_ENUM_BSY_TIMEOUT * HZ;
929
930 /* loop until we detect !BSY or timeout */
931 while(1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 status = isd200_action( us, ACTION_ENUM, NULL, master_slave );
934 if ( status != ISD200_GOOD )
935 break;
936
937 status = isd200_action( us, ACTION_READ_STATUS,
938 regs, 8 );
939 if ( status != ISD200_GOOD )
940 break;
941
942 if (!detect) {
Bartlomiej Zolnierkiewiczfcd3ec62009-04-01 21:42:25 +0200943 if (regs[ATA_REG_STATUS_OFFSET] & ATA_BUSY) {
Joe Perches191648d2013-04-19 11:44:00 -0700944 usb_stor_dbg(us, " %s status is still BSY, try again...\n",
945 master_slave == ATA_ADDRESS_DEVHEAD_STD ?
946 "Master" : "Slave");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 } else {
Joe Perches191648d2013-04-19 11:44:00 -0700948 usb_stor_dbg(us, " %s status !BSY, continue with next operation\n",
949 master_slave == ATA_ADDRESS_DEVHEAD_STD ?
950 "Master" : "Slave");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 break;
952 }
953 }
Bartlomiej Zolnierkiewiczfcd3ec62009-04-01 21:42:25 +0200954 /* check for ATA_BUSY and */
955 /* ATA_DF (workaround ATA Zip drive) and */
956 /* ATA_ERR (workaround for Archos CD-ROM) */
Bartlomiej Zolnierkiewiczc74e8092007-10-27 18:01:04 +0200957 else if (regs[ATA_REG_STATUS_OFFSET] &
Bartlomiej Zolnierkiewiczfcd3ec62009-04-01 21:42:25 +0200958 (ATA_BUSY | ATA_DF | ATA_ERR)) {
Joe Perches191648d2013-04-19 11:44:00 -0700959 usb_stor_dbg(us, " Status indicates it is not ready, try again...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961 /* check for DRDY, ATA devices set DRDY after SRST */
Bartlomiej Zolnierkiewiczfcd3ec62009-04-01 21:42:25 +0200962 else if (regs[ATA_REG_STATUS_OFFSET] & ATA_DRDY) {
Joe Perches191648d2013-04-19 11:44:00 -0700963 usb_stor_dbg(us, " Identified ATA device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 info->DeviceFlags |= DF_ATA_DEVICE;
965 info->DeviceHead = master_slave;
966 break;
967 }
968 /* check Cylinder High/Low to
969 determine if it is an ATAPI device
970 */
Bartlomiej Zolnierkiewiczc74e8092007-10-27 18:01:04 +0200971 else if (regs[ATA_REG_HCYL_OFFSET] == 0xEB &&
972 regs[ATA_REG_LCYL_OFFSET] == 0x14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /* It seems that the RICOH
974 MP6200A CD/RW drive will
975 report itself okay as a
976 slave when it is really a
977 master. So this check again
978 as a master device just to
979 make sure it doesn't report
980 itself okay as a master also
981 */
982 if ((master_slave & ATA_ADDRESS_DEVHEAD_SLAVE) &&
983 !recheckAsMaster) {
Joe Perches191648d2013-04-19 11:44:00 -0700984 usb_stor_dbg(us, " Identified ATAPI device as slave. Rechecking again as master\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 recheckAsMaster = 1;
986 master_slave = ATA_ADDRESS_DEVHEAD_STD;
987 } else {
Joe Perches191648d2013-04-19 11:44:00 -0700988 usb_stor_dbg(us, " Identified ATAPI device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 info->DeviceHead = master_slave;
990
991 status = isd200_atapi_soft_reset(us);
992 break;
993 }
994 } else {
Joe Perches191648d2013-04-19 11:44:00 -0700995 usb_stor_dbg(us, " Not ATA, not ATAPI - Weird\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 break;
997 }
998
999 /* check for timeout on this request */
1000 if (time_after_eq(jiffies, endTime)) {
1001 if (!detect)
Joe Perches191648d2013-04-19 11:44:00 -07001002 usb_stor_dbg(us, " BSY check timeout, just continue with next operation...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 else
Joe Perches191648d2013-04-19 11:44:00 -07001004 usb_stor_dbg(us, " Device detect timeout!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 break;
1006 }
1007 }
1008
1009 return status;
1010}
1011
1012/**************************************************************************
1013 * isd200_manual_enum
1014 *
1015 * Determines if the drive attached is an ATA or ATAPI and if it is a
1016 * master or slave.
1017 *
1018 * RETURNS:
1019 * ISD status code
1020 */
1021static int isd200_manual_enum(struct us_data *us)
1022{
1023 struct isd200_info *info = (struct isd200_info *)us->extra;
1024 int retStatus = ISD200_GOOD;
1025
Joe Perches191648d2013-04-19 11:44:00 -07001026 usb_stor_dbg(us, "Entering isd200_manual_enum\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 retStatus = isd200_read_config(us);
1029 if (retStatus == ISD200_GOOD) {
1030 int isslave;
1031 /* master or slave? */
1032 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 0);
1033 if (retStatus == ISD200_GOOD)
1034 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_SLAVE, 0);
1035
1036 if (retStatus == ISD200_GOOD) {
1037 retStatus = isd200_srst(us);
1038 if (retStatus == ISD200_GOOD)
1039 /* ata or atapi? */
1040 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 1);
1041 }
1042
1043 isslave = (info->DeviceHead & ATA_ADDRESS_DEVHEAD_SLAVE) ? 1 : 0;
1044 if (!(info->ConfigData.ATAConfig & ATACFG_MASTER)) {
Joe Perches191648d2013-04-19 11:44:00 -07001045 usb_stor_dbg(us, " Setting Master/Slave selection to %d\n",
1046 isslave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 info->ConfigData.ATAConfig &= 0x3f;
1048 info->ConfigData.ATAConfig |= (isslave<<6);
1049 retStatus = isd200_write_config(us);
1050 }
1051 }
1052
Joe Perches191648d2013-04-19 11:44:00 -07001053 usb_stor_dbg(us, "Leaving isd200_manual_enum %08X\n", retStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return(retStatus);
1055}
1056
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001057static void isd200_fix_driveid(u16 *id)
Alan Cox3794ade2007-10-15 15:08:11 +01001058{
1059#ifndef __LITTLE_ENDIAN
1060# ifdef __BIG_ENDIAN
1061 int i;
Alan Cox3794ade2007-10-15 15:08:11 +01001062
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001063 for (i = 0; i < ATA_ID_WORDS; i++)
1064 id[i] = __le16_to_cpu(id[i]);
Alan Cox3794ade2007-10-15 15:08:11 +01001065# else
1066# error "Please fix <asm/byteorder.h>"
1067# endif
1068#endif
1069}
1070
Joe Perches191648d2013-04-19 11:44:00 -07001071static void isd200_dump_driveid(struct us_data *us, u16 *id)
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001072{
Joe Perches191648d2013-04-19 11:44:00 -07001073 usb_stor_dbg(us, " Identify Data Structure:\n");
1074 usb_stor_dbg(us, " config = 0x%x\n", id[ATA_ID_CONFIG]);
1075 usb_stor_dbg(us, " cyls = 0x%x\n", id[ATA_ID_CYLS]);
1076 usb_stor_dbg(us, " heads = 0x%x\n", id[ATA_ID_HEADS]);
1077 usb_stor_dbg(us, " track_bytes = 0x%x\n", id[4]);
1078 usb_stor_dbg(us, " sector_bytes = 0x%x\n", id[5]);
1079 usb_stor_dbg(us, " sectors = 0x%x\n", id[ATA_ID_SECTORS]);
1080 usb_stor_dbg(us, " serial_no[0] = 0x%x\n", *(char *)&id[ATA_ID_SERNO]);
1081 usb_stor_dbg(us, " buf_type = 0x%x\n", id[20]);
1082 usb_stor_dbg(us, " buf_size = 0x%x\n", id[ATA_ID_BUF_SIZE]);
1083 usb_stor_dbg(us, " ecc_bytes = 0x%x\n", id[22]);
1084 usb_stor_dbg(us, " fw_rev[0] = 0x%x\n", *(char *)&id[ATA_ID_FW_REV]);
1085 usb_stor_dbg(us, " model[0] = 0x%x\n", *(char *)&id[ATA_ID_PROD]);
1086 usb_stor_dbg(us, " max_multsect = 0x%x\n", id[ATA_ID_MAX_MULTSECT] & 0xff);
1087 usb_stor_dbg(us, " dword_io = 0x%x\n", id[ATA_ID_DWORD_IO]);
1088 usb_stor_dbg(us, " capability = 0x%x\n", id[ATA_ID_CAPABILITY] >> 8);
1089 usb_stor_dbg(us, " tPIO = 0x%x\n", id[ATA_ID_OLD_PIO_MODES] >> 8);
1090 usb_stor_dbg(us, " tDMA = 0x%x\n", id[ATA_ID_OLD_DMA_MODES] >> 8);
1091 usb_stor_dbg(us, " field_valid = 0x%x\n", id[ATA_ID_FIELD_VALID]);
1092 usb_stor_dbg(us, " cur_cyls = 0x%x\n", id[ATA_ID_CUR_CYLS]);
1093 usb_stor_dbg(us, " cur_heads = 0x%x\n", id[ATA_ID_CUR_HEADS]);
1094 usb_stor_dbg(us, " cur_sectors = 0x%x\n", id[ATA_ID_CUR_SECTORS]);
1095 usb_stor_dbg(us, " cur_capacity = 0x%x\n", ata_id_u32(id, 57));
1096 usb_stor_dbg(us, " multsect = 0x%x\n", id[ATA_ID_MULTSECT] & 0xff);
1097 usb_stor_dbg(us, " lba_capacity = 0x%x\n", ata_id_u32(id, ATA_ID_LBA_CAPACITY));
1098 usb_stor_dbg(us, " command_set_1 = 0x%x\n", id[ATA_ID_COMMAND_SET_1]);
1099 usb_stor_dbg(us, " command_set_2 = 0x%x\n", id[ATA_ID_COMMAND_SET_2]);
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102/**************************************************************************
1103 * isd200_get_inquiry_data
1104 *
1105 * Get inquiry data
1106 *
1107 * RETURNS:
1108 * ISD status code
1109 */
1110static int isd200_get_inquiry_data( struct us_data *us )
1111{
1112 struct isd200_info *info = (struct isd200_info *)us->extra;
1113 int retStatus = ISD200_GOOD;
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001114 u16 *id = info->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Joe Perches191648d2013-04-19 11:44:00 -07001116 usb_stor_dbg(us, "Entering isd200_get_inquiry_data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 /* set default to Master */
1119 info->DeviceHead = ATA_ADDRESS_DEVHEAD_STD;
1120
1121 /* attempt to manually enumerate this device */
1122 retStatus = isd200_manual_enum(us);
1123 if (retStatus == ISD200_GOOD) {
1124 int transferStatus;
1125
1126 /* check for an ATA device */
1127 if (info->DeviceFlags & DF_ATA_DEVICE) {
1128 /* this must be an ATA device */
1129 /* perform an ATA Command Identify */
1130 transferStatus = isd200_action( us, ACTION_IDENTIFY,
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001131 id, ATA_ID_WORDS * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (transferStatus != ISD200_TRANSPORT_GOOD) {
1133 /* Error issuing ATA Command Identify */
Joe Perches191648d2013-04-19 11:44:00 -07001134 usb_stor_dbg(us, " Error issuing ATA Command Identify\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 retStatus = ISD200_ERROR;
1136 } else {
1137 /* ATA Command Identify successful */
1138 int i;
1139 __be16 *src;
1140 __u16 *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001142 isd200_fix_driveid(id);
Joe Perches191648d2013-04-19 11:44:00 -07001143 isd200_dump_driveid(us, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 memset(&info->InquiryData, 0, sizeof(info->InquiryData));
1146
1147 /* Standard IDE interface only supports disks */
1148 info->InquiryData.DeviceType = DIRECT_ACCESS_DEVICE;
1149
1150 /* The length must be at least 36 (5 + 31) */
1151 info->InquiryData.AdditionalLength = 0x1F;
1152
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001153 if (id[ATA_ID_COMMAND_SET_1] & COMMANDSET_MEDIA_STATUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 /* set the removable bit */
1155 info->InquiryData.DeviceTypeModifier = DEVICE_REMOVABLE;
1156 info->DeviceFlags |= DF_REMOVABLE_MEDIA;
1157 }
1158
1159 /* Fill in vendor identification fields */
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001160 src = (__be16 *)&id[ATA_ID_PROD];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 dest = (__u16*)info->InquiryData.VendorId;
1162 for (i=0;i<4;i++)
1163 dest[i] = be16_to_cpu(src[i]);
1164
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001165 src = (__be16 *)&id[ATA_ID_PROD + 8/2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 dest = (__u16*)info->InquiryData.ProductId;
1167 for (i=0;i<8;i++)
1168 dest[i] = be16_to_cpu(src[i]);
1169
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001170 src = (__be16 *)&id[ATA_ID_FW_REV];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 dest = (__u16*)info->InquiryData.ProductRevisionLevel;
1172 for (i=0;i<2;i++)
1173 dest[i] = be16_to_cpu(src[i]);
1174
1175 /* determine if it supports Media Status Notification */
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001176 if (id[ATA_ID_COMMAND_SET_2] & COMMANDSET_MEDIA_STATUS) {
Joe Perches191648d2013-04-19 11:44:00 -07001177 usb_stor_dbg(us, " Device supports Media Status Notification\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 /* Indicate that it is enabled, even though it is not
1180 * This allows the lock/unlock of the media to work
1181 * correctly.
1182 */
1183 info->DeviceFlags |= DF_MEDIA_STATUS_ENABLED;
1184 }
1185 else
1186 info->DeviceFlags &= ~DF_MEDIA_STATUS_ENABLED;
1187
1188 }
1189 } else {
1190 /*
1191 * this must be an ATAPI device
1192 * use an ATAPI protocol (Transparent SCSI)
1193 */
1194 us->protocol_name = "Transparent SCSI";
1195 us->proto_handler = usb_stor_transparent_scsi_command;
1196
Joe Perches191648d2013-04-19 11:44:00 -07001197 usb_stor_dbg(us, "Protocol changed to: %s\n",
1198 us->protocol_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 /* Free driver structure */
1201 us->extra_destructor(info);
Boaz Harroshba2ef3b2008-03-17 14:21:01 -07001202 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 us->extra = NULL;
1204 us->extra_destructor = NULL;
1205 }
1206 }
1207
Joe Perches191648d2013-04-19 11:44:00 -07001208 usb_stor_dbg(us, "Leaving isd200_get_inquiry_data %08X\n", retStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 return(retStatus);
1211}
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213/**************************************************************************
1214 * isd200_scsi_to_ata
1215 *
1216 * Translate SCSI commands to ATA commands.
1217 *
1218 * RETURNS:
1219 * 1 if the command needs to be sent to the transport layer
1220 * 0 otherwise
1221 */
1222static int isd200_scsi_to_ata(struct scsi_cmnd *srb, struct us_data *us,
1223 union ata_cdb * ataCdb)
1224{
1225 struct isd200_info *info = (struct isd200_info *)us->extra;
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001226 u16 *id = info->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 int sendToTransport = 1;
1228 unsigned char sectnum, head;
1229 unsigned short cylinder;
1230 unsigned long lba;
1231 unsigned long blockCount;
1232 unsigned char senseData[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1233
1234 memset(ataCdb, 0, sizeof(union ata_cdb));
1235
1236 /* SCSI Command */
1237 switch (srb->cmnd[0]) {
1238 case INQUIRY:
Joe Perches191648d2013-04-19 11:44:00 -07001239 usb_stor_dbg(us, " ATA OUT - INQUIRY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 /* copy InquiryData */
1242 usb_stor_set_xfer_buf((unsigned char *) &info->InquiryData,
1243 sizeof(info->InquiryData), srb);
1244 srb->result = SAM_STAT_GOOD;
1245 sendToTransport = 0;
1246 break;
1247
1248 case MODE_SENSE:
Joe Perches191648d2013-04-19 11:44:00 -07001249 usb_stor_dbg(us, " ATA OUT - SCSIOP_MODE_SENSE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 /* Initialize the return buffer */
1252 usb_stor_set_xfer_buf(senseData, sizeof(senseData), srb);
1253
1254 if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
1255 {
1256 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1257 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1258 ataCdb->generic.TransferBlockSize = 1;
1259 ataCdb->generic.RegisterSelect = REG_COMMAND;
1260 ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
Boaz Harrosh48c23d32007-08-22 19:10:45 +03001261 isd200_srb_set_bufflen(srb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 } else {
Joe Perches191648d2013-04-19 11:44:00 -07001263 usb_stor_dbg(us, " Media Status not supported, just report okay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 srb->result = SAM_STAT_GOOD;
1265 sendToTransport = 0;
1266 }
1267 break;
1268
1269 case TEST_UNIT_READY:
Joe Perches191648d2013-04-19 11:44:00 -07001270 usb_stor_dbg(us, " ATA OUT - SCSIOP_TEST_UNIT_READY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
1273 {
1274 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1275 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1276 ataCdb->generic.TransferBlockSize = 1;
1277 ataCdb->generic.RegisterSelect = REG_COMMAND;
1278 ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
Boaz Harrosh48c23d32007-08-22 19:10:45 +03001279 isd200_srb_set_bufflen(srb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 } else {
Joe Perches191648d2013-04-19 11:44:00 -07001281 usb_stor_dbg(us, " Media Status not supported, just report okay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 srb->result = SAM_STAT_GOOD;
1283 sendToTransport = 0;
1284 }
1285 break;
1286
1287 case READ_CAPACITY:
1288 {
1289 unsigned long capacity;
1290 struct read_capacity_data readCapacityData;
1291
Joe Perches191648d2013-04-19 11:44:00 -07001292 usb_stor_dbg(us, " ATA OUT - SCSIOP_READ_CAPACITY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001294 if (ata_id_has_lba(id))
1295 capacity = ata_id_u32(id, ATA_ID_LBA_CAPACITY) - 1;
1296 else
1297 capacity = (id[ATA_ID_HEADS] * id[ATA_ID_CYLS] *
1298 id[ATA_ID_SECTORS]) - 1;
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 readCapacityData.LogicalBlockAddress = cpu_to_be32(capacity);
1301 readCapacityData.BytesPerBlock = cpu_to_be32(0x200);
1302
1303 usb_stor_set_xfer_buf((unsigned char *) &readCapacityData,
1304 sizeof(readCapacityData), srb);
1305 srb->result = SAM_STAT_GOOD;
1306 sendToTransport = 0;
1307 }
1308 break;
1309
1310 case READ_10:
Joe Perches191648d2013-04-19 11:44:00 -07001311 usb_stor_dbg(us, " ATA OUT - SCSIOP_READ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1314 blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1315
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001316 if (ata_id_has_lba(id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 sectnum = (unsigned char)(lba);
1318 cylinder = (unsigned short)(lba>>8);
1319 head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1320 } else {
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001321 sectnum = (u8)((lba % id[ATA_ID_SECTORS]) + 1);
1322 cylinder = (u16)(lba / (id[ATA_ID_SECTORS] *
1323 id[ATA_ID_HEADS]));
1324 head = (u8)((lba / id[ATA_ID_SECTORS]) %
1325 id[ATA_ID_HEADS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1328 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1329 ataCdb->generic.TransferBlockSize = 1;
1330 ataCdb->generic.RegisterSelect =
1331 REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
1332 REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
1333 REG_DEVICE_HEAD | REG_COMMAND;
1334 ataCdb->write.SectorCountByte = (unsigned char)blockCount;
1335 ataCdb->write.SectorNumberByte = sectnum;
1336 ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
1337 ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
1338 ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
Bartlomiej Zolnierkiewiczf08f31f2009-04-01 21:42:24 +02001339 ataCdb->write.CommandByte = ATA_CMD_PIO_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 break;
1341
1342 case WRITE_10:
Joe Perches191648d2013-04-19 11:44:00 -07001343 usb_stor_dbg(us, " ATA OUT - SCSIOP_WRITE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1346 blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1347
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001348 if (ata_id_has_lba(id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 sectnum = (unsigned char)(lba);
1350 cylinder = (unsigned short)(lba>>8);
1351 head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1352 } else {
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001353 sectnum = (u8)((lba % id[ATA_ID_SECTORS]) + 1);
1354 cylinder = (u16)(lba / (id[ATA_ID_SECTORS] *
1355 id[ATA_ID_HEADS]));
1356 head = (u8)((lba / id[ATA_ID_SECTORS]) %
1357 id[ATA_ID_HEADS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1360 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1361 ataCdb->generic.TransferBlockSize = 1;
1362 ataCdb->generic.RegisterSelect =
1363 REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
1364 REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
1365 REG_DEVICE_HEAD | REG_COMMAND;
1366 ataCdb->write.SectorCountByte = (unsigned char)blockCount;
1367 ataCdb->write.SectorNumberByte = sectnum;
1368 ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
1369 ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
1370 ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
Bartlomiej Zolnierkiewiczf08f31f2009-04-01 21:42:24 +02001371 ataCdb->write.CommandByte = ATA_CMD_PIO_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 break;
1373
1374 case ALLOW_MEDIUM_REMOVAL:
Joe Perches191648d2013-04-19 11:44:00 -07001375 usb_stor_dbg(us, " ATA OUT - SCSIOP_MEDIUM_REMOVAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 if (info->DeviceFlags & DF_REMOVABLE_MEDIA) {
Joe Perches191648d2013-04-19 11:44:00 -07001378 usb_stor_dbg(us, " srb->cmnd[4] = 0x%X\n",
1379 srb->cmnd[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1382 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1383 ataCdb->generic.TransferBlockSize = 1;
1384 ataCdb->generic.RegisterSelect = REG_COMMAND;
1385 ataCdb->write.CommandByte = (srb->cmnd[4] & 0x1) ?
Bartlomiej Zolnierkiewiczf08f31f2009-04-01 21:42:24 +02001386 ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK;
Boaz Harrosh48c23d32007-08-22 19:10:45 +03001387 isd200_srb_set_bufflen(srb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 } else {
Joe Perches191648d2013-04-19 11:44:00 -07001389 usb_stor_dbg(us, " Not removeable media, just report okay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 srb->result = SAM_STAT_GOOD;
1391 sendToTransport = 0;
1392 }
1393 break;
1394
1395 case START_STOP:
Joe Perches191648d2013-04-19 11:44:00 -07001396 usb_stor_dbg(us, " ATA OUT - SCSIOP_START_STOP_UNIT\n");
1397 usb_stor_dbg(us, " srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 if ((srb->cmnd[4] & 0x3) == 0x2) {
Joe Perches191648d2013-04-19 11:44:00 -07001400 usb_stor_dbg(us, " Media Eject\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1402 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1403 ataCdb->generic.TransferBlockSize = 0;
1404 ataCdb->generic.RegisterSelect = REG_COMMAND;
1405 ataCdb->write.CommandByte = ATA_COMMAND_MEDIA_EJECT;
1406 } else if ((srb->cmnd[4] & 0x3) == 0x1) {
Joe Perches191648d2013-04-19 11:44:00 -07001407 usb_stor_dbg(us, " Get Media Status\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1409 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1410 ataCdb->generic.TransferBlockSize = 1;
1411 ataCdb->generic.RegisterSelect = REG_COMMAND;
1412 ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
Boaz Harrosh48c23d32007-08-22 19:10:45 +03001413 isd200_srb_set_bufflen(srb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 } else {
Joe Perches191648d2013-04-19 11:44:00 -07001415 usb_stor_dbg(us, " Nothing to do, just report okay\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 srb->result = SAM_STAT_GOOD;
1417 sendToTransport = 0;
1418 }
1419 break;
1420
1421 default:
Joe Perches191648d2013-04-19 11:44:00 -07001422 usb_stor_dbg(us, "Unsupported SCSI command - 0x%X\n",
1423 srb->cmnd[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 srb->result = DID_ERROR << 16;
1425 sendToTransport = 0;
1426 break;
1427 }
1428
1429 return(sendToTransport);
1430}
1431
1432
1433/**************************************************************************
1434 * isd200_free_info
1435 *
1436 * Frees the driver structure.
1437 */
1438static void isd200_free_info_ptrs(void *info_)
1439{
1440 struct isd200_info *info = (struct isd200_info *) info_;
1441
1442 if (info) {
1443 kfree(info->id);
1444 kfree(info->RegsBuf);
Boaz Harroshe92e63a2008-03-12 19:20:09 +02001445 kfree(info->srb.sense_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447}
1448
1449/**************************************************************************
1450 * isd200_init_info
1451 *
1452 * Allocates (if necessary) and initializes the driver structure.
1453 *
1454 * RETURNS:
1455 * ISD status code
1456 */
1457static int isd200_init_info(struct us_data *us)
1458{
1459 int retStatus = ISD200_GOOD;
1460 struct isd200_info *info;
1461
Joe Perchesa5cc8042010-05-31 20:23:19 -07001462 info = kzalloc(sizeof(struct isd200_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (!info)
1464 retStatus = ISD200_ERROR;
1465 else {
Bartlomiej Zolnierkiewiczd8881cd2009-04-01 21:42:19 +02001466 info->id = kzalloc(ATA_ID_WORDS * 2, GFP_KERNEL);
Zhang Yanfei5b2750d2013-03-12 13:33:27 +08001467 info->RegsBuf = kmalloc(sizeof(info->ATARegs), GFP_KERNEL);
Boaz Harroshe92e63a2008-03-12 19:20:09 +02001468 info->srb.sense_buffer =
1469 kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
1470 if (!info->id || !info->RegsBuf || !info->srb.sense_buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 isd200_free_info_ptrs(info);
1472 kfree(info);
1473 retStatus = ISD200_ERROR;
Oliver Neukum887c2562006-01-08 12:33:45 +01001474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
1476
1477 if (retStatus == ISD200_GOOD) {
1478 us->extra = info;
1479 us->extra_destructor = isd200_free_info_ptrs;
Joe Perches191648d2013-04-19 11:44:00 -07001480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01001482 return retStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
1485/**************************************************************************
1486 * Initialization for the ISD200
1487 */
1488
Alan Stern32d54932009-02-12 14:47:54 -05001489static int isd200_Initialization(struct us_data *us)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Joe Perches191648d2013-04-19 11:44:00 -07001491 usb_stor_dbg(us, "ISD200 Initialization...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 /* Initialize ISD200 info struct */
1494
1495 if (isd200_init_info(us) == ISD200_ERROR) {
Joe Perches191648d2013-04-19 11:44:00 -07001496 usb_stor_dbg(us, "ERROR Initializing ISD200 Info struct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 } else {
1498 /* Get device specific data */
1499
1500 if (isd200_get_inquiry_data(us) != ISD200_GOOD)
Joe Perches191648d2013-04-19 11:44:00 -07001501 usb_stor_dbg(us, "ISD200 Initialization Failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 else
Joe Perches191648d2013-04-19 11:44:00 -07001503 usb_stor_dbg(us, "ISD200 Initialization complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
1505
1506 return 0;
1507}
1508
1509
1510/**************************************************************************
1511 * Protocol and Transport for the ISD200 ASIC
1512 *
1513 * This protocol and transport are for ATA devices connected to an ISD200
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001514 * ASIC. An ATAPI device that is connected as a slave device will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 * detected in the driver initialization function and the protocol will
1516 * be changed to an ATAPI protocol (Transparent SCSI).
1517 *
1518 */
1519
Alan Stern32d54932009-02-12 14:47:54 -05001520static void isd200_ata_command(struct scsi_cmnd *srb, struct us_data *us)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Boaz Harrosh48c23d32007-08-22 19:10:45 +03001522 int sendToTransport = 1, orig_bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 union ata_cdb ataCdb;
1524
1525 /* Make sure driver was initialized */
1526
1527 if (us->extra == NULL)
Joe Perches191648d2013-04-19 11:44:00 -07001528 usb_stor_dbg(us, "ERROR Driver not initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Boaz Harrosh48c23d32007-08-22 19:10:45 +03001530 scsi_set_resid(srb, 0);
1531 /* scsi_bufflen might change in protocol translation to ata */
1532 orig_bufflen = scsi_bufflen(srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 sendToTransport = isd200_scsi_to_ata(srb, us, &ataCdb);
1534
1535 /* send the command to the transport layer */
1536 if (sendToTransport)
1537 isd200_invoke_transport(us, srb, &ataCdb);
Boaz Harrosh48c23d32007-08-22 19:10:45 +03001538
1539 isd200_srb_set_bufflen(srb, orig_bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
Alan Stern32d54932009-02-12 14:47:54 -05001541
Akinobu Mitaaa519be2015-05-06 18:24:21 +09001542static struct scsi_host_template isd200_host_template;
1543
Alan Stern32d54932009-02-12 14:47:54 -05001544static int isd200_probe(struct usb_interface *intf,
1545 const struct usb_device_id *id)
1546{
1547 struct us_data *us;
1548 int result;
1549
1550 result = usb_stor_probe1(&us, intf, id,
Akinobu Mitaaa519be2015-05-06 18:24:21 +09001551 (id - isd200_usb_ids) + isd200_unusual_dev_list,
1552 &isd200_host_template);
Alan Stern32d54932009-02-12 14:47:54 -05001553 if (result)
1554 return result;
1555
1556 us->protocol_name = "ISD200 ATA/ATAPI";
1557 us->proto_handler = isd200_ata_command;
1558
1559 result = usb_stor_probe2(us);
1560 return result;
1561}
1562
1563static struct usb_driver isd200_driver = {
Akinobu Mitaaa519be2015-05-06 18:24:21 +09001564 .name = DRV_NAME,
Alan Stern32d54932009-02-12 14:47:54 -05001565 .probe = isd200_probe,
1566 .disconnect = usb_stor_disconnect,
1567 .suspend = usb_stor_suspend,
1568 .resume = usb_stor_resume,
1569 .reset_resume = usb_stor_reset_resume,
1570 .pre_reset = usb_stor_pre_reset,
1571 .post_reset = usb_stor_post_reset,
1572 .id_table = isd200_usb_ids,
1573 .soft_unbind = 1,
Huajun Lie73b2db2012-01-14 10:15:21 +08001574 .no_dynamic_id = 1,
Alan Stern32d54932009-02-12 14:47:54 -05001575};
1576
Akinobu Mitaaa519be2015-05-06 18:24:21 +09001577module_usb_stor_driver(isd200_driver, isd200_host_template, DRV_NAME);