Linux Audio

Check our new training course

Loading...
v3.1
   1/* Transport & Protocol Driver for In-System Design, Inc. ISD200 ASIC
 
 
   2 *
   3 * Current development and maintenance:
   4 *   (C) 2001-2002 Björn Stenberg (bjorn@haxx.se)
   5 *
   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>
  47#include <linux/module.h>
  48#include <linux/slab.h>
  49#include <linux/ata.h>
  50#include <linux/hdreg.h>
  51#include <linux/scatterlist.h>
  52
  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"
  62
 
 
  63MODULE_DESCRIPTION("Driver for In-System Design, Inc. ISD200 ASIC");
  64MODULE_AUTHOR("Björn Stenberg <bjorn@haxx.se>");
  65MODULE_LICENSE("GPL");
  66
  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), \
  77  .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
  78
  79struct usb_device_id isd200_usb_ids[] = {
  80#	include "unusual_isd200.h"
  81	{ }		/* Terminating entry */
  82};
  83MODULE_DEVICE_TABLE(usb, isd200_usb_ids);
  84
  85#undef UNUSUAL_DEV
  86#undef USUAL_DEV
  87
  88/*
  89 * The flags table
  90 */
  91#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
  92		    vendor_name, product_name, use_protocol, use_transport, \
  93		    init_function, Flags) \
  94{ \
  95	.vendorName = vendor_name,	\
  96	.productName = product_name,	\
  97	.useProtocol = use_protocol,	\
  98	.useTransport = use_transport,	\
  99	.initFunction = init_function,	\
 100}
 101
 102static struct us_unusual_dev isd200_unusual_dev_list[] = {
 103#	include "unusual_isd200.h"
 104	{ }		/* Terminating entry */
 105};
 106
 107#undef UNUSUAL_DEV
 108#undef USUAL_DEV
 109
 110
 111/* Timeout defines (in Seconds) */
 112
 113#define ISD200_ENUM_BSY_TIMEOUT		35
 114#define ISD200_ENUM_DETECT_TIMEOUT      30
 115#define ISD200_DEFAULT_TIMEOUT		30
 116
 117/* device flags */
 118#define DF_ATA_DEVICE		0x0001
 119#define DF_MEDIA_STATUS_ENABLED	0x0002
 120#define DF_REMOVABLE_MEDIA	0x0004
 121
 122/* capability bit definitions */
 123#define CAPABILITY_DMA		0x01
 124#define CAPABILITY_LBA		0x02
 125
 126/* command_setX bit definitions */
 127#define COMMANDSET_REMOVABLE	0x02
 128#define COMMANDSET_MEDIA_STATUS 0x10
 129
 130/* ATA Vendor Specific defines */
 131#define ATA_ADDRESS_DEVHEAD_STD      0xa0
 132#define ATA_ADDRESS_DEVHEAD_LBA_MODE 0x40    
 133#define ATA_ADDRESS_DEVHEAD_SLAVE    0x10
 134
 135/* Action Select bits */
 136#define ACTION_SELECT_0	     0x01
 137#define ACTION_SELECT_1	     0x02
 138#define ACTION_SELECT_2	     0x04
 139#define ACTION_SELECT_3	     0x08
 140#define ACTION_SELECT_4	     0x10
 141#define ACTION_SELECT_5	     0x20
 142#define ACTION_SELECT_6	     0x40
 143#define ACTION_SELECT_7	     0x80
 144
 145/* Register Select bits */
 146#define REG_ALTERNATE_STATUS	0x01
 147#define REG_DEVICE_CONTROL	0x01
 148#define REG_ERROR		0x02
 149#define REG_FEATURES		0x02
 150#define REG_SECTOR_COUNT	0x04
 151#define REG_SECTOR_NUMBER	0x08
 152#define REG_CYLINDER_LOW	0x10
 153#define REG_CYLINDER_HIGH	0x20
 154#define REG_DEVICE_HEAD		0x40
 155#define REG_STATUS		0x80
 156#define REG_COMMAND		0x80
 157
 158/* ATA registers offset definitions */
 159#define ATA_REG_ERROR_OFFSET		1
 160#define ATA_REG_LCYL_OFFSET		4
 161#define ATA_REG_HCYL_OFFSET		5
 162#define ATA_REG_STATUS_OFFSET		7
 163
 164/* ATA error definitions not in <linux/hdreg.h> */
 165#define ATA_ERROR_MEDIA_CHANGE		0x20
 166
 167/* ATA command definitions not in <linux/hdreg.h> */
 168#define ATA_COMMAND_GET_MEDIA_STATUS	0xDA
 169#define ATA_COMMAND_MEDIA_EJECT		0xED
 170
 171/* ATA drive control definitions */
 172#define ATA_DC_DISABLE_INTERRUPTS	0x02
 173#define ATA_DC_RESET_CONTROLLER		0x04
 174#define ATA_DC_REENABLE_CONTROLLER	0x00
 175
 176/*
 177 *  General purpose return codes
 178 */ 
 179
 180#define ISD200_ERROR		-1
 181#define ISD200_GOOD		 0
 182
 183/*
 184 * Transport return codes
 185 */
 186
 187#define ISD200_TRANSPORT_GOOD       0   /* Transport good, command good     */
 188#define ISD200_TRANSPORT_FAILED     1   /* Transport good, command failed   */
 189#define ISD200_TRANSPORT_ERROR      2   /* Transport bad (i.e. device dead) */
 190
 191/* driver action codes */
 192#define	ACTION_READ_STATUS	0
 193#define	ACTION_RESET		1
 194#define	ACTION_REENABLE		2
 195#define	ACTION_SOFT_RESET	3
 196#define	ACTION_ENUM		4
 197#define	ACTION_IDENTIFY		5
 198
 199
 200/*
 201 * ata_cdb struct
 202 */
 203
 204
 205union ata_cdb {
 206	struct {
 207		unsigned char SignatureByte0;
 208		unsigned char SignatureByte1;
 209		unsigned char ActionSelect;
 210		unsigned char RegisterSelect;
 211		unsigned char TransferBlockSize;
 212		unsigned char WriteData3F6;
 213		unsigned char WriteData1F1;
 214		unsigned char WriteData1F2;
 215		unsigned char WriteData1F3;
 216		unsigned char WriteData1F4;
 217		unsigned char WriteData1F5;
 218		unsigned char WriteData1F6;
 219		unsigned char WriteData1F7;
 220		unsigned char Reserved[3];
 221	} generic;
 222
 223	struct {
 224		unsigned char SignatureByte0;
 225		unsigned char SignatureByte1;
 226		unsigned char ActionSelect;
 227		unsigned char RegisterSelect;
 228		unsigned char TransferBlockSize;
 229		unsigned char AlternateStatusByte;
 230		unsigned char ErrorByte;
 231		unsigned char SectorCountByte;
 232		unsigned char SectorNumberByte;
 233		unsigned char CylinderLowByte;
 234		unsigned char CylinderHighByte;
 235		unsigned char DeviceHeadByte;
 236		unsigned char StatusByte;
 237		unsigned char Reserved[3];
 238	} read;
 239
 240	struct {
 241		unsigned char SignatureByte0;
 242		unsigned char SignatureByte1;
 243		unsigned char ActionSelect;
 244		unsigned char RegisterSelect;
 245		unsigned char TransferBlockSize;
 246		unsigned char DeviceControlByte;
 247		unsigned char FeaturesByte;
 248		unsigned char SectorCountByte;
 249		unsigned char SectorNumberByte;
 250		unsigned char CylinderLowByte;
 251		unsigned char CylinderHighByte;
 252		unsigned char DeviceHeadByte;
 253		unsigned char CommandByte;
 254		unsigned char Reserved[3];
 255	} write;
 256};
 257
 258
 259/*
 260 * Inquiry data structure. This is the data returned from the target
 261 * after it receives an inquiry.
 262 *
 263 * This structure may be extended by the number of bytes specified
 264 * in the field AdditionalLength. The defined size constant only
 265 * includes fields through ProductRevisionLevel.
 266 */
 267
 268/*
 269 * DeviceType field
 270 */
 271#define DIRECT_ACCESS_DEVICE	    0x00    /* disks */
 272#define DEVICE_REMOVABLE		0x80
 273
 274struct inquiry_data {
 275   	unsigned char DeviceType;
 276	unsigned char DeviceTypeModifier;
 277	unsigned char Versions;
 278	unsigned char Format; 
 279	unsigned char AdditionalLength;
 280	unsigned char Reserved[2];
 281	unsigned char Capability;
 282	unsigned char VendorId[8];
 283	unsigned char ProductId[16];
 284	unsigned char ProductRevisionLevel[4];
 285	unsigned char VendorSpecific[20];
 286	unsigned char Reserved3[40];
 287} __attribute__ ((packed));
 288
 289/*
 290 * INQUIRY data buffer size
 291 */
 292
 293#define INQUIRYDATABUFFERSIZE 36
 294
 295
 296/*
 297 * ISD200 CONFIG data struct
 298 */
 299
 300#define ATACFG_TIMING	  0x0f
 301#define ATACFG_ATAPI_RESET     0x10
 302#define ATACFG_MASTER	  0x20
 303#define ATACFG_BLOCKSIZE       0xa0
 304
 305#define ATACFGE_LAST_LUN       0x07
 306#define ATACFGE_DESC_OVERRIDE  0x08
 307#define ATACFGE_STATE_SUSPEND  0x10
 308#define ATACFGE_SKIP_BOOT      0x20
 309#define ATACFGE_CONF_DESC2     0x40
 310#define ATACFGE_INIT_STATUS    0x80
 311
 312#define CFG_CAPABILITY_SRST    0x01
 313
 314struct isd200_config {
 315	unsigned char EventNotification;
 316	unsigned char ExternalClock;
 317	unsigned char ATAInitTimeout;
 318	unsigned char ATAConfig;
 319	unsigned char ATAMajorCommand;
 320	unsigned char ATAMinorCommand;
 321	unsigned char ATAExtraConfig;
 322	unsigned char Capability;
 323}__attribute__ ((packed));
 324
 325
 326/*
 327 * ISD200 driver information struct
 328 */
 329
 330struct isd200_info {
 331	struct inquiry_data InquiryData;
 332	u16 *id;
 333	struct isd200_config ConfigData;
 334	unsigned char *RegsBuf;
 335	unsigned char ATARegs[8];
 336	unsigned char DeviceHead;
 337	unsigned char DeviceFlags;
 338
 339	/* maximum number of LUNs supported */
 340	unsigned char MaxLUNs;
 341	unsigned char cmnd[BLK_MAX_CDB];
 342	struct scsi_cmnd srb;
 343	struct scatterlist sg;
 344};
 345
 346
 347/*
 348 * Read Capacity Data - returned in Big Endian format
 349 */
 350
 351struct read_capacity_data {
 352	__be32 LogicalBlockAddress;
 353	__be32 BytesPerBlock;
 354};
 355
 356/*
 357 * Read Block Limits Data - returned in Big Endian format
 358 * This structure returns the maximum and minimum block
 359 * size for a TAPE device.
 360 */
 361
 362struct read_block_limits {
 363	unsigned char Reserved;
 364	unsigned char BlockMaximumSize[3];
 365	unsigned char BlockMinimumSize[2];
 366};
 367
 368
 369/*
 370 * Sense Data Format
 371 */
 372
 373#define SENSE_ERRCODE	   0x7f
 374#define SENSE_ERRCODE_VALID     0x80
 375#define SENSE_FLAG_SENSE_KEY    0x0f
 376#define SENSE_FLAG_BAD_LENGTH   0x20
 377#define SENSE_FLAG_END_OF_MEDIA 0x40
 378#define SENSE_FLAG_FILE_MARK    0x80
 379struct sense_data {
 380	unsigned char ErrorCode;
 381	unsigned char SegmentNumber;
 382	unsigned char Flags;
 383	unsigned char Information[4];
 384	unsigned char AdditionalSenseLength;
 385	unsigned char CommandSpecificInformation[4];
 386	unsigned char AdditionalSenseCode;
 387	unsigned char AdditionalSenseCodeQualifier;
 388	unsigned char FieldReplaceableUnitCode;
 389	unsigned char SenseKeySpecific[3];
 390} __attribute__ ((packed));
 391
 392/*
 393 * Default request sense buffer size
 394 */
 395
 396#define SENSE_BUFFER_SIZE 18
 397
 398/***********************************************************************
 399 * Helper routines
 400 ***********************************************************************/
 401
 402/**************************************************************************
 403 * isd200_build_sense
 404 *									 
 405 *  Builds an artificial sense buffer to report the results of a 
 406 *  failed command.
 407 *								       
 408 * RETURNS:
 409 *    void
 410 */
 411static void isd200_build_sense(struct us_data *us, struct scsi_cmnd *srb)
 412{
 413	struct isd200_info *info = (struct isd200_info *)us->extra;
 414	struct sense_data *buf = (struct sense_data *) &srb->sense_buffer[0];
 415	unsigned char error = info->ATARegs[ATA_REG_ERROR_OFFSET];
 416
 417	if(error & ATA_ERROR_MEDIA_CHANGE) {
 418		buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
 419		buf->AdditionalSenseLength = 0xb;
 420		buf->Flags = UNIT_ATTENTION;
 421		buf->AdditionalSenseCode = 0;
 422		buf->AdditionalSenseCodeQualifier = 0;
 423	} else if (error & ATA_MCR) {
 424		buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
 425		buf->AdditionalSenseLength = 0xb;
 426		buf->Flags =  UNIT_ATTENTION;
 427		buf->AdditionalSenseCode = 0;
 428		buf->AdditionalSenseCodeQualifier = 0;
 429	} else if (error & ATA_TRK0NF) {
 430		buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
 431		buf->AdditionalSenseLength = 0xb;
 432		buf->Flags =  NOT_READY;
 433		buf->AdditionalSenseCode = 0;
 434		buf->AdditionalSenseCodeQualifier = 0;
 435	} else if (error & ATA_UNC) {
 436		buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
 437		buf->AdditionalSenseLength = 0xb;
 438		buf->Flags =  DATA_PROTECT;
 439		buf->AdditionalSenseCode = 0;
 440		buf->AdditionalSenseCodeQualifier = 0;
 441	} else {
 442		buf->ErrorCode = 0;
 443		buf->AdditionalSenseLength = 0;
 444		buf->Flags =  0;
 445		buf->AdditionalSenseCode = 0;
 446		buf->AdditionalSenseCodeQualifier = 0;
 447	}
 448}
 449
 450
 451/***********************************************************************
 452 * Transport routines
 453 ***********************************************************************/
 454
 455/**************************************************************************
 456 *  isd200_set_srb(), isd200_srb_set_bufflen()
 457 *
 458 * Two helpers to facilitate in initialization of scsi_cmnd structure
 459 * Will need to change when struct scsi_cmnd changes
 460 */
 461static void isd200_set_srb(struct isd200_info *info,
 462	enum dma_data_direction dir, void* buff, unsigned bufflen)
 463{
 464	struct scsi_cmnd *srb = &info->srb;
 465
 466	if (buff)
 467		sg_init_one(&info->sg, buff, bufflen);
 468
 469	srb->sc_data_direction = dir;
 470	srb->sdb.table.sgl = buff ? &info->sg : NULL;
 471	srb->sdb.length = bufflen;
 472	srb->sdb.table.nents = buff ? 1 : 0;
 473}
 474
 475static void isd200_srb_set_bufflen(struct scsi_cmnd *srb, unsigned bufflen)
 476{
 477	srb->sdb.length = bufflen;
 478}
 479
 480
 481/**************************************************************************
 482 *  isd200_action
 483 *
 484 * Routine for sending commands to the isd200
 485 *
 486 * RETURNS:
 487 *    ISD status code
 488 */
 489static int isd200_action( struct us_data *us, int action, 
 490			  void* pointer, int value )
 491{
 492	union ata_cdb ata;
 493	/* static to prevent this large struct being placed on the valuable stack */
 494	static struct scsi_device srb_dev;
 495	struct isd200_info *info = (struct isd200_info *)us->extra;
 496	struct scsi_cmnd *srb = &info->srb;
 497	int status;
 498
 499	memset(&ata, 0, sizeof(ata));
 500	srb->cmnd = info->cmnd;
 501	srb->device = &srb_dev;
 502
 503	ata.generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
 504	ata.generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
 505	ata.generic.TransferBlockSize = 1;
 506
 507	switch ( action ) {
 508	case ACTION_READ_STATUS:
 509		US_DEBUGP("   isd200_action(READ_STATUS)\n");
 510		ata.generic.ActionSelect = ACTION_SELECT_0|ACTION_SELECT_2;
 511		ata.generic.RegisterSelect =
 512		  REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
 513		  REG_STATUS | REG_ERROR;
 514		isd200_set_srb(info, DMA_FROM_DEVICE, pointer, value);
 515		break;
 516
 517	case ACTION_ENUM:
 518		US_DEBUGP("   isd200_action(ENUM,0x%02x)\n",value);
 519		ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
 520					   ACTION_SELECT_3|ACTION_SELECT_4|
 521					   ACTION_SELECT_5;
 522		ata.generic.RegisterSelect = REG_DEVICE_HEAD;
 523		ata.write.DeviceHeadByte = value;
 524		isd200_set_srb(info, DMA_NONE, NULL, 0);
 525		break;
 526
 527	case ACTION_RESET:
 528		US_DEBUGP("   isd200_action(RESET)\n");
 529		ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
 530					   ACTION_SELECT_3|ACTION_SELECT_4;
 531		ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
 532		ata.write.DeviceControlByte = ATA_DC_RESET_CONTROLLER;
 533		isd200_set_srb(info, DMA_NONE, NULL, 0);
 534		break;
 535
 536	case ACTION_REENABLE:
 537		US_DEBUGP("   isd200_action(REENABLE)\n");
 538		ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
 539					   ACTION_SELECT_3|ACTION_SELECT_4;
 540		ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
 541		ata.write.DeviceControlByte = ATA_DC_REENABLE_CONTROLLER;
 542		isd200_set_srb(info, DMA_NONE, NULL, 0);
 543		break;
 544
 545	case ACTION_SOFT_RESET:
 546		US_DEBUGP("   isd200_action(SOFT_RESET)\n");
 547		ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_5;
 548		ata.generic.RegisterSelect = REG_DEVICE_HEAD | REG_COMMAND;
 549		ata.write.DeviceHeadByte = info->DeviceHead;
 550		ata.write.CommandByte = ATA_CMD_DEV_RESET;
 551		isd200_set_srb(info, DMA_NONE, NULL, 0);
 552		break;
 553
 554	case ACTION_IDENTIFY:
 555		US_DEBUGP("   isd200_action(IDENTIFY)\n");
 556		ata.generic.RegisterSelect = REG_COMMAND;
 557		ata.write.CommandByte = ATA_CMD_ID_ATA;
 558		isd200_set_srb(info, DMA_FROM_DEVICE, info->id,
 559				ATA_ID_WORDS * 2);
 560		break;
 561
 562	default:
 563		US_DEBUGP("Error: Undefined action %d\n",action);
 564		return ISD200_ERROR;
 565	}
 566
 567	memcpy(srb->cmnd, &ata, sizeof(ata.generic));
 568	srb->cmd_len = sizeof(ata.generic);
 569	status = usb_stor_Bulk_transport(srb, us);
 570	if (status == USB_STOR_TRANSPORT_GOOD)
 571		status = ISD200_GOOD;
 572	else {
 573		US_DEBUGP("   isd200_action(0x%02x) error: %d\n",action,status);
 
 574		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
 595	US_DEBUGP("Entering isd200_IssueATAReadRegs\n");
 596
 597	transferStatus = isd200_action( us, ACTION_READ_STATUS,
 598				    info->RegsBuf, sizeof(info->ATARegs) );
 599	if (transferStatus != ISD200_TRANSPORT_GOOD) {
 600		US_DEBUGP("   Error reading ATA registers\n");
 601		retStatus = ISD200_ERROR;
 602	} else {
 603		memcpy(info->ATARegs, info->RegsBuf, sizeof(info->ATARegs));
 604		US_DEBUGP("   Got ATA Register[ATA_REG_ERROR_OFFSET] = 0x%x\n",
 605			  info->ATARegs[ATA_REG_ERROR_OFFSET]);
 606	}
 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	 */
 634	if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
 635		US_DEBUGP("-- command was aborted\n");
 636		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:
 647		US_DEBUGP("-- transport indicates protocol failure\n");
 648		srb->result = SAM_STAT_CHECK_CONDITION;
 649		return;
 650
 651	case USB_STOR_TRANSPORT_FAILED:
 652		US_DEBUGP("-- transport indicates command failure\n");
 653		need_auto_sense = 1;
 654		break;
 655
 656	case USB_STOR_TRANSPORT_ERROR:
 657		US_DEBUGP("-- transport indicates transport error\n");
 658		srb->result = DID_ERROR << 16;
 659		/* Need reset here */
 660		return;
 661    
 662	default:
 663		US_DEBUGP("-- transport indicates unknown error\n");   
 664		srb->result = DID_ERROR << 16;
 665		/* Need reset here */
 666		return;
 667	}
 668
 669	if ((scsi_get_resid(srb) > 0) &&
 670	    !((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))) {
 675		US_DEBUGP("-- unexpectedly short transfer\n");
 676		need_auto_sense = 1;
 677	}
 678
 679	if (need_auto_sense) {
 680		result = isd200_read_regs(us);
 681		if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
 682			US_DEBUGP("-- auto-sense aborted\n");
 683			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 */
 711	clear_bit(US_FLIDX_ABORTING, &us->dflags);
 712	/* Need reset here */
 713}
 714
 715#ifdef CONFIG_USB_STORAGE_DEBUG
 716static void isd200_log_config( struct isd200_info* info )
 717{
 718	US_DEBUGP("      Event Notification: 0x%x\n", 
 719		  info->ConfigData.EventNotification);
 720	US_DEBUGP("      External Clock: 0x%x\n", 
 721		  info->ConfigData.ExternalClock);
 722	US_DEBUGP("      ATA Init Timeout: 0x%x\n", 
 723		  info->ConfigData.ATAInitTimeout);
 724	US_DEBUGP("      ATAPI Command Block Size: 0x%x\n", 
 725		  (info->ConfigData.ATAConfig & ATACFG_BLOCKSIZE) >> 6);
 726	US_DEBUGP("      Master/Slave Selection: 0x%x\n", 
 727		  info->ConfigData.ATAConfig & ATACFG_MASTER);
 728	US_DEBUGP("      ATAPI Reset: 0x%x\n",
 729		  info->ConfigData.ATAConfig & ATACFG_ATAPI_RESET);
 730	US_DEBUGP("      ATA Timing: 0x%x\n",
 731		  info->ConfigData.ATAConfig & ATACFG_TIMING);
 732	US_DEBUGP("      ATA Major Command: 0x%x\n", 
 733		  info->ConfigData.ATAMajorCommand);
 734	US_DEBUGP("      ATA Minor Command: 0x%x\n", 
 735		  info->ConfigData.ATAMinorCommand);
 736	US_DEBUGP("      Init Status: 0x%x\n", 
 737		  info->ConfigData.ATAExtraConfig & ATACFGE_INIT_STATUS);
 738	US_DEBUGP("      Config Descriptor 2: 0x%x\n", 
 739		  info->ConfigData.ATAExtraConfig & ATACFGE_CONF_DESC2);
 740	US_DEBUGP("      Skip Device Boot: 0x%x\n",
 741		  info->ConfigData.ATAExtraConfig & ATACFGE_SKIP_BOOT);
 742	US_DEBUGP("      ATA 3 State Supsend: 0x%x\n",
 743		  info->ConfigData.ATAExtraConfig & ATACFGE_STATE_SUSPEND);
 744	US_DEBUGP("      Descriptor Override: 0x%x\n", 
 745		  info->ConfigData.ATAExtraConfig & ATACFGE_DESC_OVERRIDE);
 746	US_DEBUGP("      Last LUN Identifier: 0x%x\n",
 747		  info->ConfigData.ATAExtraConfig & ATACFGE_LAST_LUN);
 748	US_DEBUGP("      SRST Enable: 0x%x\n", 
 749		  info->ConfigData.ATAExtraConfig & CFG_CAPABILITY_SRST);
 750}
 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
 768	US_DEBUGP("Entering isd200_write_config\n");
 769	US_DEBUGP("   Writing the following ISD200 Config Data:\n");
 770	isd200_log_config(info);
 771#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) {
 785		US_DEBUGP("   ISD200 Config Data was written successfully\n");
 786	} else {
 787		US_DEBUGP("   Request to write ISD200 Config Data failed!\n");
 788		retStatus = ISD200_ERROR;
 789	}
 790
 791	US_DEBUGP("Leaving isd200_write_config %08X\n", retStatus);
 792	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
 810	US_DEBUGP("Entering isd200_read_config\n");
 811
 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) {
 827		US_DEBUGP("   Retrieved the following ISD200 Config Data:\n");
 828#ifdef CONFIG_USB_STORAGE_DEBUG
 829		isd200_log_config(info);
 830#endif
 831	} else {
 832		US_DEBUGP("   Request to get ISD200 Config Data failed!\n");
 833		retStatus = ISD200_ERROR;
 834	}
 835
 836	US_DEBUGP("Leaving isd200_read_config %08X\n", retStatus);
 837	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
 854	US_DEBUGP("Entering isd200_atapi_soft_reset\n");
 855
 856	transferStatus = isd200_action( us, ACTION_SOFT_RESET, NULL, 0 );
 857	if (transferStatus != ISD200_TRANSPORT_GOOD) {
 858		US_DEBUGP("   Error issuing Atapi Soft Reset\n");
 859		retStatus = ISD200_ERROR;
 860	}
 861
 862	US_DEBUGP("Leaving isd200_atapi_soft_reset %08X\n", retStatus);
 863	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
 880	US_DEBUGP("Entering isd200_SRST\n");
 881
 882	transferStatus = isd200_action( us, ACTION_RESET, NULL, 0 );
 883
 884	/* check to see if this request failed */
 885	if (transferStatus != ISD200_TRANSPORT_GOOD) {
 886		US_DEBUGP("   Error issuing SRST\n");
 887		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) {
 894			US_DEBUGP("   Error taking drive out of reset\n");
 895			retStatus = ISD200_ERROR;
 896		} else {
 897			/* delay 50ms to give the drive a chance to recover after SRST */
 898			msleep(50);
 899		}
 900	}
 901
 902	US_DEBUGP("Leaving isd200_srst %08X\n", retStatus);
 903	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) {
 932#ifdef CONFIG_USB_STORAGE_DEBUG
 933		char* mstr = master_slave == ATA_ADDRESS_DEVHEAD_STD ?
 934			"Master" : "Slave";
 935#endif
 936
 937		status = isd200_action( us, ACTION_ENUM, NULL, master_slave );
 938		if ( status != ISD200_GOOD )
 939			break;
 940
 941		status = isd200_action( us, ACTION_READ_STATUS, 
 942					regs, 8 );
 943		if ( status != ISD200_GOOD )
 944			break;
 945
 946		if (!detect) {
 947			if (regs[ATA_REG_STATUS_OFFSET] & ATA_BUSY) {
 948				US_DEBUGP("   %s status is still BSY, try again...\n",mstr);
 
 
 949			} else {
 950				US_DEBUGP("   %s status !BSY, continue with next operation\n",mstr);
 
 
 951				break;
 952			}
 953		}
 954		/* check for ATA_BUSY and */
 955		/* ATA_DF (workaround ATA Zip drive) and */
 956		/* ATA_ERR (workaround for Archos CD-ROM) */
 957		else if (regs[ATA_REG_STATUS_OFFSET] &
 958			 (ATA_BUSY | ATA_DF | ATA_ERR)) {
 959			US_DEBUGP("   Status indicates it is not ready, try again...\n");
 960		}
 961		/* check for DRDY, ATA devices set DRDY after SRST */
 962		else if (regs[ATA_REG_STATUS_OFFSET] & ATA_DRDY) {
 963			US_DEBUGP("   Identified ATA device\n");
 964			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		*/
 
 971		else if (regs[ATA_REG_HCYL_OFFSET] == 0xEB &&
 972			 regs[ATA_REG_LCYL_OFFSET] == 0x14) {
 973			/* 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) {
 984				US_DEBUGP("   Identified ATAPI device as slave.  Rechecking again as master\n");
 985				recheckAsMaster = 1;
 986				master_slave = ATA_ADDRESS_DEVHEAD_STD;
 987			} else {
 988				US_DEBUGP("   Identified ATAPI device\n");
 989				info->DeviceHead = master_slave;
 990			      
 991				status = isd200_atapi_soft_reset(us);
 992				break;
 993			}
 994		} else {
 995 			US_DEBUGP("   Not ATA, not ATAPI. Weird.\n");
 996			break;
 997		}
 998
 999		/* check for timeout on this request */
1000		if (time_after_eq(jiffies, endTime)) {
1001			if (!detect)
1002				US_DEBUGP("   BSY check timeout, just continue with next operation...\n");
1003			else
1004				US_DEBUGP("   Device detect timeout!\n");
1005			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
1026	US_DEBUGP("Entering isd200_manual_enum\n");
1027
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)) {
1045			US_DEBUGP("   Setting Master/Slave selection to %d\n", isslave);
 
1046			info->ConfigData.ATAConfig &= 0x3f;
1047			info->ConfigData.ATAConfig |= (isslave<<6);
1048			retStatus = isd200_write_config(us);
1049		}
1050	}
1051
1052	US_DEBUGP("Leaving isd200_manual_enum %08X\n", retStatus);
1053	return(retStatus);
1054}
1055
1056static void isd200_fix_driveid(u16 *id)
1057{
1058#ifndef __LITTLE_ENDIAN
1059# ifdef __BIG_ENDIAN
1060	int i;
1061
1062	for (i = 0; i < ATA_ID_WORDS; i++)
1063		id[i] = __le16_to_cpu(id[i]);
1064# else
1065#  error "Please fix <asm/byteorder.h>"
1066# endif
1067#endif
1068}
1069
1070static void isd200_dump_driveid(u16 *id)
1071{
1072	US_DEBUGP("   Identify Data Structure:\n");
1073	US_DEBUGP("      config = 0x%x\n",	  id[ATA_ID_CONFIG]);
1074	US_DEBUGP("      cyls = 0x%x\n",	  id[ATA_ID_CYLS]);
1075	US_DEBUGP("      heads = 0x%x\n",	  id[ATA_ID_HEADS]);
1076	US_DEBUGP("      track_bytes = 0x%x\n",	  id[4]);
1077	US_DEBUGP("      sector_bytes = 0x%x\n",  id[5]);
1078	US_DEBUGP("      sectors = 0x%x\n",	  id[ATA_ID_SECTORS]);
1079	US_DEBUGP("      serial_no[0] = 0x%x\n",  *(char *)&id[ATA_ID_SERNO]);
1080	US_DEBUGP("      buf_type = 0x%x\n",	  id[20]);
1081	US_DEBUGP("      buf_size = 0x%x\n",	  id[ATA_ID_BUF_SIZE]);
1082	US_DEBUGP("      ecc_bytes = 0x%x\n",	  id[22]);
1083	US_DEBUGP("      fw_rev[0] = 0x%x\n",	  *(char *)&id[ATA_ID_FW_REV]);
1084	US_DEBUGP("      model[0] = 0x%x\n",	  *(char *)&id[ATA_ID_PROD]);
1085	US_DEBUGP("      max_multsect = 0x%x\n",  id[ATA_ID_MAX_MULTSECT] & 0xff);
1086	US_DEBUGP("      dword_io = 0x%x\n",	  id[ATA_ID_DWORD_IO]);
1087	US_DEBUGP("      capability = 0x%x\n",	  id[ATA_ID_CAPABILITY] >> 8);
1088	US_DEBUGP("      tPIO = 0x%x\n",	  id[ATA_ID_OLD_PIO_MODES] >> 8);
1089	US_DEBUGP("      tDMA = 0x%x\n",	  id[ATA_ID_OLD_DMA_MODES] >> 8);
1090	US_DEBUGP("      field_valid = 0x%x\n",	  id[ATA_ID_FIELD_VALID]);
1091	US_DEBUGP("      cur_cyls = 0x%x\n",	  id[ATA_ID_CUR_CYLS]);
1092	US_DEBUGP("      cur_heads = 0x%x\n",	  id[ATA_ID_CUR_HEADS]);
1093	US_DEBUGP("      cur_sectors = 0x%x\n",	  id[ATA_ID_CUR_SECTORS]);
1094	US_DEBUGP("      cur_capacity = 0x%x\n",  ata_id_u32(id, 57));
1095	US_DEBUGP("      multsect = 0x%x\n",	  id[ATA_ID_MULTSECT] & 0xff);
1096	US_DEBUGP("      lba_capacity = 0x%x\n",  ata_id_u32(id, ATA_ID_LBA_CAPACITY));
1097	US_DEBUGP("      command_set_1 = 0x%x\n", id[ATA_ID_COMMAND_SET_1]);
1098	US_DEBUGP("      command_set_2 = 0x%x\n", id[ATA_ID_COMMAND_SET_2]);
1099}
1100
1101/**************************************************************************
1102 * isd200_get_inquiry_data
1103 *
1104 * Get inquiry data
1105 *
1106 * RETURNS:
1107 *    ISD status code
1108 */
1109static int isd200_get_inquiry_data( struct us_data *us )
1110{
1111	struct isd200_info *info = (struct isd200_info *)us->extra;
1112	int retStatus = ISD200_GOOD;
1113	u16 *id = info->id;
1114
1115	US_DEBUGP("Entering isd200_get_inquiry_data\n");
1116
1117	/* set default to Master */
1118	info->DeviceHead = ATA_ADDRESS_DEVHEAD_STD;
1119
1120	/* attempt to manually enumerate this device */
1121	retStatus = isd200_manual_enum(us);
1122	if (retStatus == ISD200_GOOD) {
1123		int transferStatus;
1124
1125		/* check for an ATA device */
1126		if (info->DeviceFlags & DF_ATA_DEVICE) {
1127			/* this must be an ATA device */
1128			/* perform an ATA Command Identify */
1129			transferStatus = isd200_action( us, ACTION_IDENTIFY,
1130							id, ATA_ID_WORDS * 2);
1131			if (transferStatus != ISD200_TRANSPORT_GOOD) {
1132				/* Error issuing ATA Command Identify */
1133				US_DEBUGP("   Error issuing ATA Command Identify\n");
1134				retStatus = ISD200_ERROR;
1135			} else {
1136				/* ATA Command Identify successful */
1137				int i;
1138				__be16 *src;
1139				__u16 *dest;
1140
1141				isd200_fix_driveid(id);
1142				isd200_dump_driveid(id);
1143
1144				memset(&info->InquiryData, 0, sizeof(info->InquiryData));
1145
1146				/* Standard IDE interface only supports disks */
1147				info->InquiryData.DeviceType = DIRECT_ACCESS_DEVICE;
1148
1149				/* The length must be at least 36 (5 + 31) */
1150				info->InquiryData.AdditionalLength = 0x1F;
1151
1152				if (id[ATA_ID_COMMAND_SET_1] & COMMANDSET_MEDIA_STATUS) {
1153					/* set the removable bit */
1154					info->InquiryData.DeviceTypeModifier = DEVICE_REMOVABLE;
1155					info->DeviceFlags |= DF_REMOVABLE_MEDIA;
1156				}
1157
1158				/* Fill in vendor identification fields */
1159				src = (__be16 *)&id[ATA_ID_PROD];
1160				dest = (__u16*)info->InquiryData.VendorId;
1161				for (i=0;i<4;i++)
1162					dest[i] = be16_to_cpu(src[i]);
1163
1164				src = (__be16 *)&id[ATA_ID_PROD + 8/2];
1165				dest = (__u16*)info->InquiryData.ProductId;
1166				for (i=0;i<8;i++)
1167					dest[i] = be16_to_cpu(src[i]);
1168
1169				src = (__be16 *)&id[ATA_ID_FW_REV];
1170				dest = (__u16*)info->InquiryData.ProductRevisionLevel;
1171				for (i=0;i<2;i++)
1172					dest[i] = be16_to_cpu(src[i]);
1173
1174				/* determine if it supports Media Status Notification */
1175				if (id[ATA_ID_COMMAND_SET_2] & COMMANDSET_MEDIA_STATUS) {
1176					US_DEBUGP("   Device supports Media Status Notification\n");
1177
1178					/* Indicate that it is enabled, even though it is not
1179					 * This allows the lock/unlock of the media to work
1180					 * correctly.
 
 
1181					 */
1182					info->DeviceFlags |= DF_MEDIA_STATUS_ENABLED;
1183				}
1184				else
1185					info->DeviceFlags &= ~DF_MEDIA_STATUS_ENABLED;
1186
1187			}
1188		} else {
1189			/* 
1190			 * this must be an ATAPI device 
1191			 * use an ATAPI protocol (Transparent SCSI)
1192			 */
1193			us->protocol_name = "Transparent SCSI";
1194			us->proto_handler = usb_stor_transparent_scsi_command;
1195
1196			US_DEBUGP("Protocol changed to: %s\n", us->protocol_name);
 
1197	    
1198			/* Free driver structure */	    
1199			us->extra_destructor(info);
1200			kfree(info);
1201			us->extra = NULL;
1202			us->extra_destructor = NULL;
1203		}
1204	}
1205
1206	US_DEBUGP("Leaving isd200_get_inquiry_data %08X\n", retStatus);
1207
1208	return(retStatus);
1209}
1210
1211/**************************************************************************
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;
1224	u16 *id = info->id;
1225	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:
1237		US_DEBUGP("   ATA OUT - INQUIRY\n");
1238
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:
1247		US_DEBUGP("   ATA OUT - SCSIOP_MODE_SENSE\n");
1248
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;
1259			isd200_srb_set_bufflen(srb, 0);
1260		} else {
1261			US_DEBUGP("   Media Status not supported, just report okay\n");
1262			srb->result = SAM_STAT_GOOD;
1263			sendToTransport = 0;
1264		}
1265		break;
1266
1267	case TEST_UNIT_READY:
1268		US_DEBUGP("   ATA OUT - SCSIOP_TEST_UNIT_READY\n");
1269
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;
1277			isd200_srb_set_bufflen(srb, 0);
1278		} else {
1279			US_DEBUGP("   Media Status not supported, just report okay\n");
1280			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
1290		US_DEBUGP("   ATA OUT - SCSIOP_READ_CAPACITY\n");
1291
1292		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
1298		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:
1309		US_DEBUGP("   ATA OUT - SCSIOP_READ\n");
1310
1311		lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1312		blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1313
1314		if (ata_id_has_lba(id)) {
1315			sectnum = (unsigned char)(lba);
1316			cylinder = (unsigned short)(lba>>8);
1317			head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1318		} else {
1319			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]);
1324		}
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);
1337		ataCdb->write.CommandByte = ATA_CMD_PIO_READ;
1338		break;
1339
1340	case WRITE_10:
1341		US_DEBUGP("   ATA OUT - SCSIOP_WRITE\n");
1342
1343		lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1344		blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1345
1346		if (ata_id_has_lba(id)) {
1347			sectnum = (unsigned char)(lba);
1348			cylinder = (unsigned short)(lba>>8);
1349			head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1350		} else {
1351			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]);
1356		}
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);
1369		ataCdb->write.CommandByte = ATA_CMD_PIO_WRITE;
1370		break;
1371
1372	case ALLOW_MEDIUM_REMOVAL:
1373		US_DEBUGP("   ATA OUT - SCSIOP_MEDIUM_REMOVAL\n");
1374
1375		if (info->DeviceFlags & DF_REMOVABLE_MEDIA) {
1376			US_DEBUGP("   srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
 
1377	    
1378			ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1379			ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1380			ataCdb->generic.TransferBlockSize = 1;
1381			ataCdb->generic.RegisterSelect = REG_COMMAND;
1382			ataCdb->write.CommandByte = (srb->cmnd[4] & 0x1) ?
1383				ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK;
1384			isd200_srb_set_bufflen(srb, 0);
1385		} else {
1386			US_DEBUGP("   Not removeable media, just report okay\n");
1387			srb->result = SAM_STAT_GOOD;
1388			sendToTransport = 0;
1389		}
1390		break;
1391
1392	case START_STOP:    
1393		US_DEBUGP("   ATA OUT - SCSIOP_START_STOP_UNIT\n");
1394		US_DEBUGP("   srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
1395
1396		if ((srb->cmnd[4] & 0x3) == 0x2) {
1397			US_DEBUGP("   Media Eject\n");
1398			ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1399			ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1400			ataCdb->generic.TransferBlockSize = 0;
1401			ataCdb->generic.RegisterSelect = REG_COMMAND;
1402			ataCdb->write.CommandByte = ATA_COMMAND_MEDIA_EJECT;
1403		} else if ((srb->cmnd[4] & 0x3) == 0x1) {
1404			US_DEBUGP("   Get Media Status\n");
1405			ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1406			ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1407			ataCdb->generic.TransferBlockSize = 1;
1408			ataCdb->generic.RegisterSelect = REG_COMMAND;
1409			ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1410			isd200_srb_set_bufflen(srb, 0);
1411		} else {
1412			US_DEBUGP("   Nothing to do, just report okay\n");
1413			srb->result = SAM_STAT_GOOD;
1414			sendToTransport = 0;
1415		}
1416		break;
1417
1418	default:
1419		US_DEBUGP("Unsupported SCSI command - 0x%X\n", srb->cmnd[0]);
 
1420		srb->result = DID_ERROR << 16;
1421		sendToTransport = 0;
1422		break;
1423	}
1424
1425	return(sendToTransport);
1426}
1427
1428
1429/**************************************************************************
1430 * isd200_free_info
1431 *
1432 * Frees the driver structure.
1433 */
1434static void isd200_free_info_ptrs(void *info_)
1435{
1436	struct isd200_info *info = (struct isd200_info *) info_;
1437
1438	if (info) {
1439		kfree(info->id);
1440		kfree(info->RegsBuf);
1441		kfree(info->srb.sense_buffer);
1442	}
1443}
1444
1445/**************************************************************************
1446 * isd200_init_info
1447 *									 
1448 * Allocates (if necessary) and initializes the driver structure.
1449 *
1450 * RETURNS:
1451 *    ISD status code
1452 */
1453static int isd200_init_info(struct us_data *us)
1454{
1455	int retStatus = ISD200_GOOD;
1456	struct isd200_info *info;
1457
1458	info = kzalloc(sizeof(struct isd200_info), GFP_KERNEL);
1459	if (!info)
1460		retStatus = ISD200_ERROR;
1461	else {
1462		info->id = kzalloc(ATA_ID_WORDS * 2, GFP_KERNEL);
1463		info->RegsBuf = (unsigned char *)
1464				kmalloc(sizeof(info->ATARegs), GFP_KERNEL);
1465		info->srb.sense_buffer =
1466				kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
1467		if (!info->id || !info->RegsBuf || !info->srb.sense_buffer) {
1468			isd200_free_info_ptrs(info);
1469			kfree(info);
1470			retStatus = ISD200_ERROR;
1471		}
1472	}
1473
1474	if (retStatus == ISD200_GOOD) {
1475		us->extra = info;
1476		us->extra_destructor = isd200_free_info_ptrs;
1477	} else
1478		US_DEBUGP("ERROR - kmalloc failure\n");
1479
1480	return retStatus;
1481}
1482
1483/**************************************************************************
1484 * Initialization for the ISD200 
1485 */
1486
1487static int isd200_Initialization(struct us_data *us)
1488{
1489	US_DEBUGP("ISD200 Initialization...\n");
1490
1491	/* Initialize ISD200 info struct */
1492
1493	if (isd200_init_info(us) == ISD200_ERROR) {
1494		US_DEBUGP("ERROR Initializing ISD200 Info struct\n");
1495	} else {
1496		/* Get device specific data */
1497
1498		if (isd200_get_inquiry_data(us) != ISD200_GOOD)
1499			US_DEBUGP("ISD200 Initialization Failure\n");
1500		else
1501			US_DEBUGP("ISD200 Initialization complete\n");
1502	}
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
1512 * ASIC.  An ATAPI device that is connected as a slave device will be
1513 * detected in the driver initialization function and the protocol will
1514 * be changed to an ATAPI protocol (Transparent SCSI).
1515 *
1516 */
1517
1518static void isd200_ata_command(struct scsi_cmnd *srb, struct us_data *us)
1519{
1520	int sendToTransport = 1, orig_bufflen;
1521	union ata_cdb ataCdb;
1522
1523	/* Make sure driver was initialized */
1524
1525	if (us->extra == NULL)
1526		US_DEBUGP("ERROR Driver not initialized\n");
 
 
 
1527
1528	scsi_set_resid(srb, 0);
1529	/* scsi_bufflen might change in protocol translation to ata */
1530	orig_bufflen = scsi_bufflen(srb);
1531	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);
1536
1537	isd200_srb_set_bufflen(srb, orig_bufflen);
1538}
1539
 
 
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,
 
1569};
1570
1571static int __init isd200_init(void)
1572{
1573	return usb_register(&isd200_driver);
1574}
1575
1576static void __exit isd200_exit(void)
1577{
1578	usb_deregister(&isd200_driver);
1579}
1580
1581module_init(isd200_init);
1582module_exit(isd200_exit);
v4.17
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * Transport & Protocol Driver for In-System Design, Inc. ISD200 ASIC
   4 *
   5 * Current development and maintenance:
   6 *   (C) 2001-2002 Björn Stenberg (bjorn@haxx.se)
   7 *
   8 * Developed with the assistance of:
   9 *   (C) 2002 Alan Stern <stern@rowland.org>
  10 *
  11 * Initial work:
  12 *   (C) 2000 In-System Design, Inc. (support@in-system.com)
  13 *
  14 * The ISD200 ASIC does not natively support ATA devices.  The chip
  15 * does implement an interface, the ATA Command Block (ATACB) which provides
  16 * a means of passing ATA commands and ATA register accesses to a device.
  17 *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  18 * History:
  19 *
  20 *  2002-10-19: Removed the specialized transfer routines.
  21 *		(Alan Stern <stern@rowland.harvard.edu>)
  22 *  2001-02-24: Removed lots of duplicate code and simplified the structure.
  23 *	      (bjorn@haxx.se)
  24 *  2002-01-16: Fixed endianness bug so it works on the ppc arch.
  25 *	      (Luc Saillard <luc@saillard.org>)
  26 *  2002-01-17: All bitfields removed.
  27 *	      (bjorn@haxx.se)
  28 */
  29
  30
  31/* Include files */
  32
  33#include <linux/jiffies.h>
  34#include <linux/errno.h>
  35#include <linux/module.h>
  36#include <linux/slab.h>
  37#include <linux/ata.h>
  38#include <linux/hdreg.h>
  39#include <linux/scatterlist.h>
  40
  41#include <scsi/scsi.h>
  42#include <scsi/scsi_cmnd.h>
  43#include <scsi/scsi_device.h>
  44
  45#include "usb.h"
  46#include "transport.h"
  47#include "protocol.h"
  48#include "debug.h"
  49#include "scsiglue.h"
  50
  51#define DRV_NAME "ums-isd200"
  52
  53MODULE_DESCRIPTION("Driver for In-System Design, Inc. ISD200 ASIC");
  54MODULE_AUTHOR("Björn Stenberg <bjorn@haxx.se>");
  55MODULE_LICENSE("GPL");
  56
  57static int isd200_Initialization(struct us_data *us);
  58
  59
  60/*
  61 * The table of devices
  62 */
  63#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
  64		    vendorName, productName, useProtocol, useTransport, \
  65		    initFunction, flags) \
  66{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
  67  .driver_info = (flags) }
  68
  69static struct usb_device_id isd200_usb_ids[] = {
  70#	include "unusual_isd200.h"
  71	{ }		/* Terminating entry */
  72};
  73MODULE_DEVICE_TABLE(usb, isd200_usb_ids);
  74
  75#undef UNUSUAL_DEV
 
  76
  77/*
  78 * The flags table
  79 */
  80#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
  81		    vendor_name, product_name, use_protocol, use_transport, \
  82		    init_function, Flags) \
  83{ \
  84	.vendorName = vendor_name,	\
  85	.productName = product_name,	\
  86	.useProtocol = use_protocol,	\
  87	.useTransport = use_transport,	\
  88	.initFunction = init_function,	\
  89}
  90
  91static struct us_unusual_dev isd200_unusual_dev_list[] = {
  92#	include "unusual_isd200.h"
  93	{ }		/* Terminating entry */
  94};
  95
  96#undef UNUSUAL_DEV
 
 
  97
  98/* Timeout defines (in Seconds) */
  99
 100#define ISD200_ENUM_BSY_TIMEOUT		35
 101#define ISD200_ENUM_DETECT_TIMEOUT      30
 102#define ISD200_DEFAULT_TIMEOUT		30
 103
 104/* device flags */
 105#define DF_ATA_DEVICE		0x0001
 106#define DF_MEDIA_STATUS_ENABLED	0x0002
 107#define DF_REMOVABLE_MEDIA	0x0004
 108
 109/* capability bit definitions */
 110#define CAPABILITY_DMA		0x01
 111#define CAPABILITY_LBA		0x02
 112
 113/* command_setX bit definitions */
 114#define COMMANDSET_REMOVABLE	0x02
 115#define COMMANDSET_MEDIA_STATUS 0x10
 116
 117/* ATA Vendor Specific defines */
 118#define ATA_ADDRESS_DEVHEAD_STD      0xa0
 119#define ATA_ADDRESS_DEVHEAD_LBA_MODE 0x40    
 120#define ATA_ADDRESS_DEVHEAD_SLAVE    0x10
 121
 122/* Action Select bits */
 123#define ACTION_SELECT_0	     0x01
 124#define ACTION_SELECT_1	     0x02
 125#define ACTION_SELECT_2	     0x04
 126#define ACTION_SELECT_3	     0x08
 127#define ACTION_SELECT_4	     0x10
 128#define ACTION_SELECT_5	     0x20
 129#define ACTION_SELECT_6	     0x40
 130#define ACTION_SELECT_7	     0x80
 131
 132/* Register Select bits */
 133#define REG_ALTERNATE_STATUS	0x01
 134#define REG_DEVICE_CONTROL	0x01
 135#define REG_ERROR		0x02
 136#define REG_FEATURES		0x02
 137#define REG_SECTOR_COUNT	0x04
 138#define REG_SECTOR_NUMBER	0x08
 139#define REG_CYLINDER_LOW	0x10
 140#define REG_CYLINDER_HIGH	0x20
 141#define REG_DEVICE_HEAD		0x40
 142#define REG_STATUS		0x80
 143#define REG_COMMAND		0x80
 144
 145/* ATA registers offset definitions */
 146#define ATA_REG_ERROR_OFFSET		1
 147#define ATA_REG_LCYL_OFFSET		4
 148#define ATA_REG_HCYL_OFFSET		5
 149#define ATA_REG_STATUS_OFFSET		7
 150
 151/* ATA error definitions not in <linux/hdreg.h> */
 152#define ATA_ERROR_MEDIA_CHANGE		0x20
 153
 154/* ATA command definitions not in <linux/hdreg.h> */
 155#define ATA_COMMAND_GET_MEDIA_STATUS	0xDA
 156#define ATA_COMMAND_MEDIA_EJECT		0xED
 157
 158/* ATA drive control definitions */
 159#define ATA_DC_DISABLE_INTERRUPTS	0x02
 160#define ATA_DC_RESET_CONTROLLER		0x04
 161#define ATA_DC_REENABLE_CONTROLLER	0x00
 162
 163/*
 164 *  General purpose return codes
 165 */ 
 166
 167#define ISD200_ERROR		-1
 168#define ISD200_GOOD		 0
 169
 170/*
 171 * Transport return codes
 172 */
 173
 174#define ISD200_TRANSPORT_GOOD       0   /* Transport good, command good     */
 175#define ISD200_TRANSPORT_FAILED     1   /* Transport good, command failed   */
 176#define ISD200_TRANSPORT_ERROR      2   /* Transport bad (i.e. device dead) */
 177
 178/* driver action codes */
 179#define	ACTION_READ_STATUS	0
 180#define	ACTION_RESET		1
 181#define	ACTION_REENABLE		2
 182#define	ACTION_SOFT_RESET	3
 183#define	ACTION_ENUM		4
 184#define	ACTION_IDENTIFY		5
 185
 186
 187/*
 188 * ata_cdb struct
 189 */
 190
 191
 192union ata_cdb {
 193	struct {
 194		unsigned char SignatureByte0;
 195		unsigned char SignatureByte1;
 196		unsigned char ActionSelect;
 197		unsigned char RegisterSelect;
 198		unsigned char TransferBlockSize;
 199		unsigned char WriteData3F6;
 200		unsigned char WriteData1F1;
 201		unsigned char WriteData1F2;
 202		unsigned char WriteData1F3;
 203		unsigned char WriteData1F4;
 204		unsigned char WriteData1F5;
 205		unsigned char WriteData1F6;
 206		unsigned char WriteData1F7;
 207		unsigned char Reserved[3];
 208	} generic;
 209
 210	struct {
 211		unsigned char SignatureByte0;
 212		unsigned char SignatureByte1;
 213		unsigned char ActionSelect;
 214		unsigned char RegisterSelect;
 215		unsigned char TransferBlockSize;
 216		unsigned char AlternateStatusByte;
 217		unsigned char ErrorByte;
 218		unsigned char SectorCountByte;
 219		unsigned char SectorNumberByte;
 220		unsigned char CylinderLowByte;
 221		unsigned char CylinderHighByte;
 222		unsigned char DeviceHeadByte;
 223		unsigned char StatusByte;
 224		unsigned char Reserved[3];
 225	} read;
 226
 227	struct {
 228		unsigned char SignatureByte0;
 229		unsigned char SignatureByte1;
 230		unsigned char ActionSelect;
 231		unsigned char RegisterSelect;
 232		unsigned char TransferBlockSize;
 233		unsigned char DeviceControlByte;
 234		unsigned char FeaturesByte;
 235		unsigned char SectorCountByte;
 236		unsigned char SectorNumberByte;
 237		unsigned char CylinderLowByte;
 238		unsigned char CylinderHighByte;
 239		unsigned char DeviceHeadByte;
 240		unsigned char CommandByte;
 241		unsigned char Reserved[3];
 242	} write;
 243};
 244
 245
 246/*
 247 * Inquiry data structure. This is the data returned from the target
 248 * after it receives an inquiry.
 249 *
 250 * This structure may be extended by the number of bytes specified
 251 * in the field AdditionalLength. The defined size constant only
 252 * includes fields through ProductRevisionLevel.
 253 */
 254
 255/*
 256 * DeviceType field
 257 */
 258#define DIRECT_ACCESS_DEVICE	    0x00    /* disks */
 259#define DEVICE_REMOVABLE		0x80
 260
 261struct inquiry_data {
 262   	unsigned char DeviceType;
 263	unsigned char DeviceTypeModifier;
 264	unsigned char Versions;
 265	unsigned char Format; 
 266	unsigned char AdditionalLength;
 267	unsigned char Reserved[2];
 268	unsigned char Capability;
 269	unsigned char VendorId[8];
 270	unsigned char ProductId[16];
 271	unsigned char ProductRevisionLevel[4];
 272	unsigned char VendorSpecific[20];
 273	unsigned char Reserved3[40];
 274} __attribute__ ((packed));
 275
 276/*
 277 * INQUIRY data buffer size
 278 */
 279
 280#define INQUIRYDATABUFFERSIZE 36
 281
 282
 283/*
 284 * ISD200 CONFIG data struct
 285 */
 286
 287#define ATACFG_TIMING	  0x0f
 288#define ATACFG_ATAPI_RESET     0x10
 289#define ATACFG_MASTER	  0x20
 290#define ATACFG_BLOCKSIZE       0xa0
 291
 292#define ATACFGE_LAST_LUN       0x07
 293#define ATACFGE_DESC_OVERRIDE  0x08
 294#define ATACFGE_STATE_SUSPEND  0x10
 295#define ATACFGE_SKIP_BOOT      0x20
 296#define ATACFGE_CONF_DESC2     0x40
 297#define ATACFGE_INIT_STATUS    0x80
 298
 299#define CFG_CAPABILITY_SRST    0x01
 300
 301struct isd200_config {
 302	unsigned char EventNotification;
 303	unsigned char ExternalClock;
 304	unsigned char ATAInitTimeout;
 305	unsigned char ATAConfig;
 306	unsigned char ATAMajorCommand;
 307	unsigned char ATAMinorCommand;
 308	unsigned char ATAExtraConfig;
 309	unsigned char Capability;
 310}__attribute__ ((packed));
 311
 312
 313/*
 314 * ISD200 driver information struct
 315 */
 316
 317struct isd200_info {
 318	struct inquiry_data InquiryData;
 319	u16 *id;
 320	struct isd200_config ConfigData;
 321	unsigned char *RegsBuf;
 322	unsigned char ATARegs[8];
 323	unsigned char DeviceHead;
 324	unsigned char DeviceFlags;
 325
 326	/* maximum number of LUNs supported */
 327	unsigned char MaxLUNs;
 328	unsigned char cmnd[BLK_MAX_CDB];
 329	struct scsi_cmnd srb;
 330	struct scatterlist sg;
 331};
 332
 333
 334/*
 335 * Read Capacity Data - returned in Big Endian format
 336 */
 337
 338struct read_capacity_data {
 339	__be32 LogicalBlockAddress;
 340	__be32 BytesPerBlock;
 341};
 342
 343/*
 344 * Read Block Limits Data - returned in Big Endian format
 345 * This structure returns the maximum and minimum block
 346 * size for a TAPE device.
 347 */
 348
 349struct read_block_limits {
 350	unsigned char Reserved;
 351	unsigned char BlockMaximumSize[3];
 352	unsigned char BlockMinimumSize[2];
 353};
 354
 355
 356/*
 357 * Sense Data Format
 358 */
 359
 360#define SENSE_ERRCODE	   0x7f
 361#define SENSE_ERRCODE_VALID     0x80
 362#define SENSE_FLAG_SENSE_KEY    0x0f
 363#define SENSE_FLAG_BAD_LENGTH   0x20
 364#define SENSE_FLAG_END_OF_MEDIA 0x40
 365#define SENSE_FLAG_FILE_MARK    0x80
 366struct sense_data {
 367	unsigned char ErrorCode;
 368	unsigned char SegmentNumber;
 369	unsigned char Flags;
 370	unsigned char Information[4];
 371	unsigned char AdditionalSenseLength;
 372	unsigned char CommandSpecificInformation[4];
 373	unsigned char AdditionalSenseCode;
 374	unsigned char AdditionalSenseCodeQualifier;
 375	unsigned char FieldReplaceableUnitCode;
 376	unsigned char SenseKeySpecific[3];
 377} __attribute__ ((packed));
 378
 379/*
 380 * Default request sense buffer size
 381 */
 382
 383#define SENSE_BUFFER_SIZE 18
 384
 385/***********************************************************************
 386 * Helper routines
 387 ***********************************************************************/
 388
 389/**************************************************************************
 390 * isd200_build_sense
 391 *									 
 392 *  Builds an artificial sense buffer to report the results of a 
 393 *  failed command.
 394 *								       
 395 * RETURNS:
 396 *    void
 397 */
 398static void isd200_build_sense(struct us_data *us, struct scsi_cmnd *srb)
 399{
 400	struct isd200_info *info = (struct isd200_info *)us->extra;
 401	struct sense_data *buf = (struct sense_data *) &srb->sense_buffer[0];
 402	unsigned char error = info->ATARegs[ATA_REG_ERROR_OFFSET];
 403
 404	if(error & ATA_ERROR_MEDIA_CHANGE) {
 405		buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
 406		buf->AdditionalSenseLength = 0xb;
 407		buf->Flags = UNIT_ATTENTION;
 408		buf->AdditionalSenseCode = 0;
 409		buf->AdditionalSenseCodeQualifier = 0;
 410	} else if (error & ATA_MCR) {
 411		buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
 412		buf->AdditionalSenseLength = 0xb;
 413		buf->Flags =  UNIT_ATTENTION;
 414		buf->AdditionalSenseCode = 0;
 415		buf->AdditionalSenseCodeQualifier = 0;
 416	} else if (error & ATA_TRK0NF) {
 417		buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
 418		buf->AdditionalSenseLength = 0xb;
 419		buf->Flags =  NOT_READY;
 420		buf->AdditionalSenseCode = 0;
 421		buf->AdditionalSenseCodeQualifier = 0;
 422	} else if (error & ATA_UNC) {
 423		buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
 424		buf->AdditionalSenseLength = 0xb;
 425		buf->Flags =  DATA_PROTECT;
 426		buf->AdditionalSenseCode = 0;
 427		buf->AdditionalSenseCodeQualifier = 0;
 428	} else {
 429		buf->ErrorCode = 0;
 430		buf->AdditionalSenseLength = 0;
 431		buf->Flags =  0;
 432		buf->AdditionalSenseCode = 0;
 433		buf->AdditionalSenseCodeQualifier = 0;
 434	}
 435}
 436
 437
 438/***********************************************************************
 439 * Transport routines
 440 ***********************************************************************/
 441
 442/**************************************************************************
 443 *  isd200_set_srb(), isd200_srb_set_bufflen()
 444 *
 445 * Two helpers to facilitate in initialization of scsi_cmnd structure
 446 * Will need to change when struct scsi_cmnd changes
 447 */
 448static void isd200_set_srb(struct isd200_info *info,
 449	enum dma_data_direction dir, void* buff, unsigned bufflen)
 450{
 451	struct scsi_cmnd *srb = &info->srb;
 452
 453	if (buff)
 454		sg_init_one(&info->sg, buff, bufflen);
 455
 456	srb->sc_data_direction = dir;
 457	srb->sdb.table.sgl = buff ? &info->sg : NULL;
 458	srb->sdb.length = bufflen;
 459	srb->sdb.table.nents = buff ? 1 : 0;
 460}
 461
 462static void isd200_srb_set_bufflen(struct scsi_cmnd *srb, unsigned bufflen)
 463{
 464	srb->sdb.length = bufflen;
 465}
 466
 467
 468/**************************************************************************
 469 *  isd200_action
 470 *
 471 * Routine for sending commands to the isd200
 472 *
 473 * RETURNS:
 474 *    ISD status code
 475 */
 476static int isd200_action( struct us_data *us, int action, 
 477			  void* pointer, int value )
 478{
 479	union ata_cdb ata;
 480	/* static to prevent this large struct being placed on the valuable stack */
 481	static struct scsi_device srb_dev;
 482	struct isd200_info *info = (struct isd200_info *)us->extra;
 483	struct scsi_cmnd *srb = &info->srb;
 484	int status;
 485
 486	memset(&ata, 0, sizeof(ata));
 487	srb->cmnd = info->cmnd;
 488	srb->device = &srb_dev;
 489
 490	ata.generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
 491	ata.generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
 492	ata.generic.TransferBlockSize = 1;
 493
 494	switch ( action ) {
 495	case ACTION_READ_STATUS:
 496		usb_stor_dbg(us, "   isd200_action(READ_STATUS)\n");
 497		ata.generic.ActionSelect = ACTION_SELECT_0|ACTION_SELECT_2;
 498		ata.generic.RegisterSelect =
 499		  REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
 500		  REG_STATUS | REG_ERROR;
 501		isd200_set_srb(info, DMA_FROM_DEVICE, pointer, value);
 502		break;
 503
 504	case ACTION_ENUM:
 505		usb_stor_dbg(us, "   isd200_action(ENUM,0x%02x)\n", value);
 506		ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
 507					   ACTION_SELECT_3|ACTION_SELECT_4|
 508					   ACTION_SELECT_5;
 509		ata.generic.RegisterSelect = REG_DEVICE_HEAD;
 510		ata.write.DeviceHeadByte = value;
 511		isd200_set_srb(info, DMA_NONE, NULL, 0);
 512		break;
 513
 514	case ACTION_RESET:
 515		usb_stor_dbg(us, "   isd200_action(RESET)\n");
 516		ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
 517					   ACTION_SELECT_3|ACTION_SELECT_4;
 518		ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
 519		ata.write.DeviceControlByte = ATA_DC_RESET_CONTROLLER;
 520		isd200_set_srb(info, DMA_NONE, NULL, 0);
 521		break;
 522
 523	case ACTION_REENABLE:
 524		usb_stor_dbg(us, "   isd200_action(REENABLE)\n");
 525		ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
 526					   ACTION_SELECT_3|ACTION_SELECT_4;
 527		ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
 528		ata.write.DeviceControlByte = ATA_DC_REENABLE_CONTROLLER;
 529		isd200_set_srb(info, DMA_NONE, NULL, 0);
 530		break;
 531
 532	case ACTION_SOFT_RESET:
 533		usb_stor_dbg(us, "   isd200_action(SOFT_RESET)\n");
 534		ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_5;
 535		ata.generic.RegisterSelect = REG_DEVICE_HEAD | REG_COMMAND;
 536		ata.write.DeviceHeadByte = info->DeviceHead;
 537		ata.write.CommandByte = ATA_CMD_DEV_RESET;
 538		isd200_set_srb(info, DMA_NONE, NULL, 0);
 539		break;
 540
 541	case ACTION_IDENTIFY:
 542		usb_stor_dbg(us, "   isd200_action(IDENTIFY)\n");
 543		ata.generic.RegisterSelect = REG_COMMAND;
 544		ata.write.CommandByte = ATA_CMD_ID_ATA;
 545		isd200_set_srb(info, DMA_FROM_DEVICE, info->id,
 546				ATA_ID_WORDS * 2);
 547		break;
 548
 549	default:
 550		usb_stor_dbg(us, "Error: Undefined action %d\n", action);
 551		return ISD200_ERROR;
 552	}
 553
 554	memcpy(srb->cmnd, &ata, sizeof(ata.generic));
 555	srb->cmd_len = sizeof(ata.generic);
 556	status = usb_stor_Bulk_transport(srb, us);
 557	if (status == USB_STOR_TRANSPORT_GOOD)
 558		status = ISD200_GOOD;
 559	else {
 560		usb_stor_dbg(us, "   isd200_action(0x%02x) error: %d\n",
 561			     action, status);
 562		status = ISD200_ERROR;
 563		/* need to reset device here */
 564	}
 565
 566	return status;
 567}
 568
 569/**************************************************************************
 570 * isd200_read_regs
 571 *									 
 572 * Read ATA Registers
 573 *
 574 * RETURNS:
 575 *    ISD status code
 576 */
 577static int isd200_read_regs( struct us_data *us )
 578{
 579	struct isd200_info *info = (struct isd200_info *)us->extra;
 580	int retStatus = ISD200_GOOD;
 581	int transferStatus;
 582
 583	usb_stor_dbg(us, "Entering isd200_IssueATAReadRegs\n");
 584
 585	transferStatus = isd200_action( us, ACTION_READ_STATUS,
 586				    info->RegsBuf, sizeof(info->ATARegs) );
 587	if (transferStatus != ISD200_TRANSPORT_GOOD) {
 588		usb_stor_dbg(us, "   Error reading ATA registers\n");
 589		retStatus = ISD200_ERROR;
 590	} else {
 591		memcpy(info->ATARegs, info->RegsBuf, sizeof(info->ATARegs));
 592		usb_stor_dbg(us, "   Got ATA Register[ATA_REG_ERROR_OFFSET] = 0x%x\n",
 593			     info->ATARegs[ATA_REG_ERROR_OFFSET]);
 594	}
 595
 596	return retStatus;
 597}
 598
 599
 600/**************************************************************************
 601 * Invoke the transport and basic error-handling/recovery methods
 602 *
 603 * This is used by the protocol layers to actually send the message to
 604 * the device and receive the response.
 605 */
 606static void isd200_invoke_transport( struct us_data *us, 
 607			      struct scsi_cmnd *srb, 
 608			      union ata_cdb *ataCdb )
 609{
 610	int need_auto_sense = 0;
 611	int transferStatus;
 612	int result;
 613
 614	/* send the command to the transport layer */
 615	memcpy(srb->cmnd, ataCdb, sizeof(ataCdb->generic));
 616	srb->cmd_len = sizeof(ataCdb->generic);
 617	transferStatus = usb_stor_Bulk_transport(srb, us);
 618
 619	/*
 620	 * if the command gets aborted by the higher layers, we need to
 621	 * short-circuit all other processing
 622	 */
 623	if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
 624		usb_stor_dbg(us, "-- command was aborted\n");
 625		goto Handle_Abort;
 626	}
 627
 628	switch (transferStatus) {
 629
 630	case USB_STOR_TRANSPORT_GOOD:
 631		/* Indicate a good result */
 632		srb->result = SAM_STAT_GOOD;
 633		break;
 634
 635	case USB_STOR_TRANSPORT_NO_SENSE:
 636		usb_stor_dbg(us, "-- transport indicates protocol failure\n");
 637		srb->result = SAM_STAT_CHECK_CONDITION;
 638		return;
 639
 640	case USB_STOR_TRANSPORT_FAILED:
 641		usb_stor_dbg(us, "-- transport indicates command failure\n");
 642		need_auto_sense = 1;
 643		break;
 644
 645	case USB_STOR_TRANSPORT_ERROR:
 646		usb_stor_dbg(us, "-- transport indicates transport error\n");
 647		srb->result = DID_ERROR << 16;
 648		/* Need reset here */
 649		return;
 650    
 651	default:
 652		usb_stor_dbg(us, "-- transport indicates unknown error\n");
 653		srb->result = DID_ERROR << 16;
 654		/* Need reset here */
 655		return;
 656	}
 657
 658	if ((scsi_get_resid(srb) > 0) &&
 659	    !((srb->cmnd[0] == REQUEST_SENSE) ||
 660	      (srb->cmnd[0] == INQUIRY) ||
 661	      (srb->cmnd[0] == MODE_SENSE) ||
 662	      (srb->cmnd[0] == LOG_SENSE) ||
 663	      (srb->cmnd[0] == MODE_SENSE_10))) {
 664		usb_stor_dbg(us, "-- unexpectedly short transfer\n");
 665		need_auto_sense = 1;
 666	}
 667
 668	if (need_auto_sense) {
 669		result = isd200_read_regs(us);
 670		if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
 671			usb_stor_dbg(us, "-- auto-sense aborted\n");
 672			goto Handle_Abort;
 673		}
 674		if (result == ISD200_GOOD) {
 675			isd200_build_sense(us, srb);
 676			srb->result = SAM_STAT_CHECK_CONDITION;
 677
 678			/* If things are really okay, then let's show that */
 679			if ((srb->sense_buffer[2] & 0xf) == 0x0)
 680				srb->result = SAM_STAT_GOOD;
 681		} else {
 682			srb->result = DID_ERROR << 16;
 683			/* Need reset here */
 684		}
 685	}
 686
 687	/*
 688	 * Regardless of auto-sense, if we _know_ we have an error
 689	 * condition, show that in the result code
 690	 */
 691	if (transferStatus == USB_STOR_TRANSPORT_FAILED)
 692		srb->result = SAM_STAT_CHECK_CONDITION;
 693	return;
 694
 695	/*
 696	 * abort processing: the bulk-only transport requires a reset
 697	 * following an abort
 698	 */
 699	Handle_Abort:
 700	srb->result = DID_ABORT << 16;
 701
 702	/* permit the reset transfer to take place */
 703	clear_bit(US_FLIDX_ABORTING, &us->dflags);
 704	/* Need reset here */
 705}
 706
 707#ifdef CONFIG_USB_STORAGE_DEBUG
 708static void isd200_log_config(struct us_data *us, struct isd200_info *info)
 709{
 710	usb_stor_dbg(us, "      Event Notification: 0x%x\n",
 711		     info->ConfigData.EventNotification);
 712	usb_stor_dbg(us, "      External Clock: 0x%x\n",
 713		     info->ConfigData.ExternalClock);
 714	usb_stor_dbg(us, "      ATA Init Timeout: 0x%x\n",
 715		     info->ConfigData.ATAInitTimeout);
 716	usb_stor_dbg(us, "      ATAPI Command Block Size: 0x%x\n",
 717		     (info->ConfigData.ATAConfig & ATACFG_BLOCKSIZE) >> 6);
 718	usb_stor_dbg(us, "      Master/Slave Selection: 0x%x\n",
 719		     info->ConfigData.ATAConfig & ATACFG_MASTER);
 720	usb_stor_dbg(us, "      ATAPI Reset: 0x%x\n",
 721		     info->ConfigData.ATAConfig & ATACFG_ATAPI_RESET);
 722	usb_stor_dbg(us, "      ATA Timing: 0x%x\n",
 723		     info->ConfigData.ATAConfig & ATACFG_TIMING);
 724	usb_stor_dbg(us, "      ATA Major Command: 0x%x\n",
 725		     info->ConfigData.ATAMajorCommand);
 726	usb_stor_dbg(us, "      ATA Minor Command: 0x%x\n",
 727		     info->ConfigData.ATAMinorCommand);
 728	usb_stor_dbg(us, "      Init Status: 0x%x\n",
 729		     info->ConfigData.ATAExtraConfig & ATACFGE_INIT_STATUS);
 730	usb_stor_dbg(us, "      Config Descriptor 2: 0x%x\n",
 731		     info->ConfigData.ATAExtraConfig & ATACFGE_CONF_DESC2);
 732	usb_stor_dbg(us, "      Skip Device Boot: 0x%x\n",
 733		     info->ConfigData.ATAExtraConfig & ATACFGE_SKIP_BOOT);
 734	usb_stor_dbg(us, "      ATA 3 State Suspend: 0x%x\n",
 735		     info->ConfigData.ATAExtraConfig & ATACFGE_STATE_SUSPEND);
 736	usb_stor_dbg(us, "      Descriptor Override: 0x%x\n",
 737		     info->ConfigData.ATAExtraConfig & ATACFGE_DESC_OVERRIDE);
 738	usb_stor_dbg(us, "      Last LUN Identifier: 0x%x\n",
 739		     info->ConfigData.ATAExtraConfig & ATACFGE_LAST_LUN);
 740	usb_stor_dbg(us, "      SRST Enable: 0x%x\n",
 741		     info->ConfigData.ATAExtraConfig & CFG_CAPABILITY_SRST);
 742}
 743#endif
 744
 745/**************************************************************************
 746 * isd200_write_config
 747 *									 
 748 * Write the ISD200 Configuration data
 749 *
 750 * RETURNS:
 751 *    ISD status code
 752 */
 753static int isd200_write_config( struct us_data *us ) 
 754{
 755	struct isd200_info *info = (struct isd200_info *)us->extra;
 756	int retStatus = ISD200_GOOD;
 757	int result;
 758
 759#ifdef CONFIG_USB_STORAGE_DEBUG
 760	usb_stor_dbg(us, "Entering isd200_write_config\n");
 761	usb_stor_dbg(us, "   Writing the following ISD200 Config Data:\n");
 762	isd200_log_config(us, info);
 763#endif
 764
 765	/* let's send the command via the control pipe */
 766	result = usb_stor_ctrl_transfer(
 767		us, 
 768		us->send_ctrl_pipe,
 769		0x01, 
 770		USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
 771		0x0000, 
 772		0x0002, 
 773		(void *) &info->ConfigData, 
 774		sizeof(info->ConfigData));
 775
 776	if (result >= 0) {
 777		usb_stor_dbg(us, "   ISD200 Config Data was written successfully\n");
 778	} else {
 779		usb_stor_dbg(us, "   Request to write ISD200 Config Data failed!\n");
 780		retStatus = ISD200_ERROR;
 781	}
 782
 783	usb_stor_dbg(us, "Leaving isd200_write_config %08X\n", retStatus);
 784	return retStatus;
 785}
 786
 787
 788/**************************************************************************
 789 * isd200_read_config
 790 *									 
 791 * Reads the ISD200 Configuration data
 792 *
 793 * RETURNS:
 794 *    ISD status code
 795 */
 796static int isd200_read_config( struct us_data *us ) 
 797{
 798	struct isd200_info *info = (struct isd200_info *)us->extra;
 799	int retStatus = ISD200_GOOD;
 800	int result;
 801
 802	usb_stor_dbg(us, "Entering isd200_read_config\n");
 803
 804	/* read the configuration information from ISD200.  Use this to */
 805	/* determine what the special ATA CDB bytes are.		*/
 806
 807	result = usb_stor_ctrl_transfer(
 808		us, 
 809		us->recv_ctrl_pipe,
 810		0x02, 
 811		USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
 812		0x0000, 
 813		0x0002, 
 814		(void *) &info->ConfigData, 
 815		sizeof(info->ConfigData));
 816
 817
 818	if (result >= 0) {
 819		usb_stor_dbg(us, "   Retrieved the following ISD200 Config Data:\n");
 820#ifdef CONFIG_USB_STORAGE_DEBUG
 821		isd200_log_config(us, info);
 822#endif
 823	} else {
 824		usb_stor_dbg(us, "   Request to get ISD200 Config Data failed!\n");
 825		retStatus = ISD200_ERROR;
 826	}
 827
 828	usb_stor_dbg(us, "Leaving isd200_read_config %08X\n", retStatus);
 829	return retStatus;
 830}
 831
 832
 833/**************************************************************************
 834 * isd200_atapi_soft_reset
 835 *									 
 836 * Perform an Atapi Soft Reset on the device
 837 *
 838 * RETURNS:
 839 *    NT status code
 840 */
 841static int isd200_atapi_soft_reset( struct us_data *us ) 
 842{
 843	int retStatus = ISD200_GOOD;
 844	int transferStatus;
 845
 846	usb_stor_dbg(us, "Entering isd200_atapi_soft_reset\n");
 847
 848	transferStatus = isd200_action( us, ACTION_SOFT_RESET, NULL, 0 );
 849	if (transferStatus != ISD200_TRANSPORT_GOOD) {
 850		usb_stor_dbg(us, "   Error issuing Atapi Soft Reset\n");
 851		retStatus = ISD200_ERROR;
 852	}
 853
 854	usb_stor_dbg(us, "Leaving isd200_atapi_soft_reset %08X\n", retStatus);
 855	return retStatus;
 856}
 857
 858
 859/**************************************************************************
 860 * isd200_srst
 861 *									 
 862 * Perform an SRST on the device
 863 *
 864 * RETURNS:
 865 *    ISD status code
 866 */
 867static int isd200_srst( struct us_data *us ) 
 868{
 869	int retStatus = ISD200_GOOD;
 870	int transferStatus;
 871
 872	usb_stor_dbg(us, "Entering isd200_SRST\n");
 873
 874	transferStatus = isd200_action( us, ACTION_RESET, NULL, 0 );
 875
 876	/* check to see if this request failed */
 877	if (transferStatus != ISD200_TRANSPORT_GOOD) {
 878		usb_stor_dbg(us, "   Error issuing SRST\n");
 879		retStatus = ISD200_ERROR;
 880	} else {
 881		/* delay 10ms to give the drive a chance to see it */
 882		msleep(10);
 883
 884		transferStatus = isd200_action( us, ACTION_REENABLE, NULL, 0 );
 885		if (transferStatus != ISD200_TRANSPORT_GOOD) {
 886			usb_stor_dbg(us, "   Error taking drive out of reset\n");
 887			retStatus = ISD200_ERROR;
 888		} else {
 889			/* delay 50ms to give the drive a chance to recover after SRST */
 890			msleep(50);
 891		}
 892	}
 893
 894	usb_stor_dbg(us, "Leaving isd200_srst %08X\n", retStatus);
 895	return retStatus;
 896}
 897
 898
 899/**************************************************************************
 900 * isd200_try_enum
 901 *									 
 902 * Helper function for isd200_manual_enum(). Does ENUM and READ_STATUS
 903 * and tries to analyze the status registers
 904 *
 905 * RETURNS:
 906 *    ISD status code
 907 */
 908static int isd200_try_enum(struct us_data *us, unsigned char master_slave,
 909			   int detect )
 910{
 911	int status = ISD200_GOOD;
 912	unsigned long endTime;
 913	struct isd200_info *info = (struct isd200_info *)us->extra;
 914	unsigned char *regs = info->RegsBuf;
 915	int recheckAsMaster = 0;
 916
 917	if ( detect )
 918		endTime = jiffies + ISD200_ENUM_DETECT_TIMEOUT * HZ;
 919	else
 920		endTime = jiffies + ISD200_ENUM_BSY_TIMEOUT * HZ;
 921
 922	/* loop until we detect !BSY or timeout */
 923	while(1) {
 
 
 
 
 924
 925		status = isd200_action( us, ACTION_ENUM, NULL, master_slave );
 926		if ( status != ISD200_GOOD )
 927			break;
 928
 929		status = isd200_action( us, ACTION_READ_STATUS, 
 930					regs, 8 );
 931		if ( status != ISD200_GOOD )
 932			break;
 933
 934		if (!detect) {
 935			if (regs[ATA_REG_STATUS_OFFSET] & ATA_BUSY) {
 936				usb_stor_dbg(us, "   %s status is still BSY, try again...\n",
 937					     master_slave == ATA_ADDRESS_DEVHEAD_STD ?
 938					     "Master" : "Slave");
 939			} else {
 940				usb_stor_dbg(us, "   %s status !BSY, continue with next operation\n",
 941					     master_slave == ATA_ADDRESS_DEVHEAD_STD ?
 942					     "Master" : "Slave");
 943				break;
 944			}
 945		}
 946		/* check for ATA_BUSY and */
 947		/* ATA_DF (workaround ATA Zip drive) and */
 948		/* ATA_ERR (workaround for Archos CD-ROM) */
 949		else if (regs[ATA_REG_STATUS_OFFSET] &
 950			 (ATA_BUSY | ATA_DF | ATA_ERR)) {
 951			usb_stor_dbg(us, "   Status indicates it is not ready, try again...\n");
 952		}
 953		/* check for DRDY, ATA devices set DRDY after SRST */
 954		else if (regs[ATA_REG_STATUS_OFFSET] & ATA_DRDY) {
 955			usb_stor_dbg(us, "   Identified ATA device\n");
 956			info->DeviceFlags |= DF_ATA_DEVICE;
 957			info->DeviceHead = master_slave;
 958			break;
 959		} 
 960		/*
 961		 * check Cylinder High/Low to
 962		 * determine if it is an ATAPI device
 963		 */
 964		else if (regs[ATA_REG_HCYL_OFFSET] == 0xEB &&
 965			 regs[ATA_REG_LCYL_OFFSET] == 0x14) {
 966			/*
 967			 * It seems that the RICOH
 968			 * MP6200A CD/RW drive will
 969			 * report itself okay as a
 970			 * slave when it is really a
 971			 * master. So this check again
 972			 * as a master device just to
 973			 * make sure it doesn't report
 974			 * itself okay as a master also
 975			 */
 976			if ((master_slave & ATA_ADDRESS_DEVHEAD_SLAVE) &&
 977			    !recheckAsMaster) {
 978				usb_stor_dbg(us, "   Identified ATAPI device as slave.  Rechecking again as master\n");
 979				recheckAsMaster = 1;
 980				master_slave = ATA_ADDRESS_DEVHEAD_STD;
 981			} else {
 982				usb_stor_dbg(us, "   Identified ATAPI device\n");
 983				info->DeviceHead = master_slave;
 984			      
 985				status = isd200_atapi_soft_reset(us);
 986				break;
 987			}
 988		} else {
 989			usb_stor_dbg(us, "   Not ATA, not ATAPI - Weird\n");
 990			break;
 991		}
 992
 993		/* check for timeout on this request */
 994		if (time_after_eq(jiffies, endTime)) {
 995			if (!detect)
 996				usb_stor_dbg(us, "   BSY check timeout, just continue with next operation...\n");
 997			else
 998				usb_stor_dbg(us, "   Device detect timeout!\n");
 999			break;
1000		}
1001	}
1002
1003	return status;
1004}
1005
1006/**************************************************************************
1007 * isd200_manual_enum
1008 *									 
1009 * Determines if the drive attached is an ATA or ATAPI and if it is a
1010 * master or slave.
1011 *
1012 * RETURNS:
1013 *    ISD status code
1014 */
1015static int isd200_manual_enum(struct us_data *us)
1016{
1017	struct isd200_info *info = (struct isd200_info *)us->extra;
1018	int retStatus = ISD200_GOOD;
1019
1020	usb_stor_dbg(us, "Entering isd200_manual_enum\n");
1021
1022	retStatus = isd200_read_config(us);
1023	if (retStatus == ISD200_GOOD) {
1024		int isslave;
1025		/* master or slave? */
1026		retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 0);
1027		if (retStatus == ISD200_GOOD)
1028			retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_SLAVE, 0);
1029
1030		if (retStatus == ISD200_GOOD) {
1031			retStatus = isd200_srst(us);
1032			if (retStatus == ISD200_GOOD)
1033				/* ata or atapi? */
1034				retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 1);
1035		}
1036
1037		isslave = (info->DeviceHead & ATA_ADDRESS_DEVHEAD_SLAVE) ? 1 : 0;
1038		if (!(info->ConfigData.ATAConfig & ATACFG_MASTER)) {
1039			usb_stor_dbg(us, "   Setting Master/Slave selection to %d\n",
1040				     isslave);
1041			info->ConfigData.ATAConfig &= 0x3f;
1042			info->ConfigData.ATAConfig |= (isslave<<6);
1043			retStatus = isd200_write_config(us);
1044		}
1045	}
1046
1047	usb_stor_dbg(us, "Leaving isd200_manual_enum %08X\n", retStatus);
1048	return(retStatus);
1049}
1050
1051static void isd200_fix_driveid(u16 *id)
1052{
1053#ifndef __LITTLE_ENDIAN
1054# ifdef __BIG_ENDIAN
1055	int i;
1056
1057	for (i = 0; i < ATA_ID_WORDS; i++)
1058		id[i] = __le16_to_cpu(id[i]);
1059# else
1060#  error "Please fix <asm/byteorder.h>"
1061# endif
1062#endif
1063}
1064
1065static void isd200_dump_driveid(struct us_data *us, u16 *id)
1066{
1067	usb_stor_dbg(us, "   Identify Data Structure:\n");
1068	usb_stor_dbg(us, "      config = 0x%x\n",	id[ATA_ID_CONFIG]);
1069	usb_stor_dbg(us, "      cyls = 0x%x\n",		id[ATA_ID_CYLS]);
1070	usb_stor_dbg(us, "      heads = 0x%x\n",	id[ATA_ID_HEADS]);
1071	usb_stor_dbg(us, "      track_bytes = 0x%x\n",	id[4]);
1072	usb_stor_dbg(us, "      sector_bytes = 0x%x\n", id[5]);
1073	usb_stor_dbg(us, "      sectors = 0x%x\n",	id[ATA_ID_SECTORS]);
1074	usb_stor_dbg(us, "      serial_no[0] = 0x%x\n", *(char *)&id[ATA_ID_SERNO]);
1075	usb_stor_dbg(us, "      buf_type = 0x%x\n",	id[20]);
1076	usb_stor_dbg(us, "      buf_size = 0x%x\n",	id[ATA_ID_BUF_SIZE]);
1077	usb_stor_dbg(us, "      ecc_bytes = 0x%x\n",	id[22]);
1078	usb_stor_dbg(us, "      fw_rev[0] = 0x%x\n",	*(char *)&id[ATA_ID_FW_REV]);
1079	usb_stor_dbg(us, "      model[0] = 0x%x\n",	*(char *)&id[ATA_ID_PROD]);
1080	usb_stor_dbg(us, "      max_multsect = 0x%x\n", id[ATA_ID_MAX_MULTSECT] & 0xff);
1081	usb_stor_dbg(us, "      dword_io = 0x%x\n",	id[ATA_ID_DWORD_IO]);
1082	usb_stor_dbg(us, "      capability = 0x%x\n",	id[ATA_ID_CAPABILITY] >> 8);
1083	usb_stor_dbg(us, "      tPIO = 0x%x\n",	  id[ATA_ID_OLD_PIO_MODES] >> 8);
1084	usb_stor_dbg(us, "      tDMA = 0x%x\n",	  id[ATA_ID_OLD_DMA_MODES] >> 8);
1085	usb_stor_dbg(us, "      field_valid = 0x%x\n",	id[ATA_ID_FIELD_VALID]);
1086	usb_stor_dbg(us, "      cur_cyls = 0x%x\n",	id[ATA_ID_CUR_CYLS]);
1087	usb_stor_dbg(us, "      cur_heads = 0x%x\n",	id[ATA_ID_CUR_HEADS]);
1088	usb_stor_dbg(us, "      cur_sectors = 0x%x\n",	id[ATA_ID_CUR_SECTORS]);
1089	usb_stor_dbg(us, "      cur_capacity = 0x%x\n", ata_id_u32(id, 57));
1090	usb_stor_dbg(us, "      multsect = 0x%x\n",	id[ATA_ID_MULTSECT] & 0xff);
1091	usb_stor_dbg(us, "      lba_capacity = 0x%x\n", ata_id_u32(id, ATA_ID_LBA_CAPACITY));
1092	usb_stor_dbg(us, "      command_set_1 = 0x%x\n", id[ATA_ID_COMMAND_SET_1]);
1093	usb_stor_dbg(us, "      command_set_2 = 0x%x\n", id[ATA_ID_COMMAND_SET_2]);
1094}
1095
1096/**************************************************************************
1097 * isd200_get_inquiry_data
1098 *
1099 * Get inquiry data
1100 *
1101 * RETURNS:
1102 *    ISD status code
1103 */
1104static int isd200_get_inquiry_data( struct us_data *us )
1105{
1106	struct isd200_info *info = (struct isd200_info *)us->extra;
1107	int retStatus = ISD200_GOOD;
1108	u16 *id = info->id;
1109
1110	usb_stor_dbg(us, "Entering isd200_get_inquiry_data\n");
1111
1112	/* set default to Master */
1113	info->DeviceHead = ATA_ADDRESS_DEVHEAD_STD;
1114
1115	/* attempt to manually enumerate this device */
1116	retStatus = isd200_manual_enum(us);
1117	if (retStatus == ISD200_GOOD) {
1118		int transferStatus;
1119
1120		/* check for an ATA device */
1121		if (info->DeviceFlags & DF_ATA_DEVICE) {
1122			/* this must be an ATA device */
1123			/* perform an ATA Command Identify */
1124			transferStatus = isd200_action( us, ACTION_IDENTIFY,
1125							id, ATA_ID_WORDS * 2);
1126			if (transferStatus != ISD200_TRANSPORT_GOOD) {
1127				/* Error issuing ATA Command Identify */
1128				usb_stor_dbg(us, "   Error issuing ATA Command Identify\n");
1129				retStatus = ISD200_ERROR;
1130			} else {
1131				/* ATA Command Identify successful */
1132				int i;
1133				__be16 *src;
1134				__u16 *dest;
1135
1136				isd200_fix_driveid(id);
1137				isd200_dump_driveid(us, id);
1138
1139				memset(&info->InquiryData, 0, sizeof(info->InquiryData));
1140
1141				/* Standard IDE interface only supports disks */
1142				info->InquiryData.DeviceType = DIRECT_ACCESS_DEVICE;
1143
1144				/* The length must be at least 36 (5 + 31) */
1145				info->InquiryData.AdditionalLength = 0x1F;
1146
1147				if (id[ATA_ID_COMMAND_SET_1] & COMMANDSET_MEDIA_STATUS) {
1148					/* set the removable bit */
1149					info->InquiryData.DeviceTypeModifier = DEVICE_REMOVABLE;
1150					info->DeviceFlags |= DF_REMOVABLE_MEDIA;
1151				}
1152
1153				/* Fill in vendor identification fields */
1154				src = (__be16 *)&id[ATA_ID_PROD];
1155				dest = (__u16*)info->InquiryData.VendorId;
1156				for (i=0;i<4;i++)
1157					dest[i] = be16_to_cpu(src[i]);
1158
1159				src = (__be16 *)&id[ATA_ID_PROD + 8/2];
1160				dest = (__u16*)info->InquiryData.ProductId;
1161				for (i=0;i<8;i++)
1162					dest[i] = be16_to_cpu(src[i]);
1163
1164				src = (__be16 *)&id[ATA_ID_FW_REV];
1165				dest = (__u16*)info->InquiryData.ProductRevisionLevel;
1166				for (i=0;i<2;i++)
1167					dest[i] = be16_to_cpu(src[i]);
1168
1169				/* determine if it supports Media Status Notification */
1170				if (id[ATA_ID_COMMAND_SET_2] & COMMANDSET_MEDIA_STATUS) {
1171					usb_stor_dbg(us, "   Device supports Media Status Notification\n");
1172
1173					/*
1174					 * Indicate that it is enabled, even
1175					 * though it is not.
1176					 * This allows the lock/unlock of the
1177					 * media to work correctly.
1178					 */
1179					info->DeviceFlags |= DF_MEDIA_STATUS_ENABLED;
1180				}
1181				else
1182					info->DeviceFlags &= ~DF_MEDIA_STATUS_ENABLED;
1183
1184			}
1185		} else {
1186			/* 
1187			 * this must be an ATAPI device 
1188			 * use an ATAPI protocol (Transparent SCSI)
1189			 */
1190			us->protocol_name = "Transparent SCSI";
1191			us->proto_handler = usb_stor_transparent_scsi_command;
1192
1193			usb_stor_dbg(us, "Protocol changed to: %s\n",
1194				     us->protocol_name);
1195	    
1196			/* Free driver structure */
1197			us->extra_destructor(info);
1198			kfree(info);
1199			us->extra = NULL;
1200			us->extra_destructor = NULL;
1201		}
1202	}
1203
1204	usb_stor_dbg(us, "Leaving isd200_get_inquiry_data %08X\n", retStatus);
1205
1206	return(retStatus);
1207}
1208
1209/**************************************************************************
1210 * isd200_scsi_to_ata
1211 *									 
1212 * Translate SCSI commands to ATA commands.
1213 *
1214 * RETURNS:
1215 *    1 if the command needs to be sent to the transport layer
1216 *    0 otherwise
1217 */
1218static int isd200_scsi_to_ata(struct scsi_cmnd *srb, struct us_data *us,
1219			      union ata_cdb * ataCdb)
1220{
1221	struct isd200_info *info = (struct isd200_info *)us->extra;
1222	u16 *id = info->id;
1223	int sendToTransport = 1;
1224	unsigned char sectnum, head;
1225	unsigned short cylinder;
1226	unsigned long lba;
1227	unsigned long blockCount;
1228	unsigned char senseData[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1229
1230	memset(ataCdb, 0, sizeof(union ata_cdb));
1231
1232	/* SCSI Command */
1233	switch (srb->cmnd[0]) {
1234	case INQUIRY:
1235		usb_stor_dbg(us, "   ATA OUT - INQUIRY\n");
1236
1237		/* copy InquiryData */
1238		usb_stor_set_xfer_buf((unsigned char *) &info->InquiryData,
1239				sizeof(info->InquiryData), srb);
1240		srb->result = SAM_STAT_GOOD;
1241		sendToTransport = 0;
1242		break;
1243
1244	case MODE_SENSE:
1245		usb_stor_dbg(us, "   ATA OUT - SCSIOP_MODE_SENSE\n");
1246
1247		/* Initialize the return buffer */
1248		usb_stor_set_xfer_buf(senseData, sizeof(senseData), srb);
1249
1250		if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
1251		{
1252			ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1253			ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1254			ataCdb->generic.TransferBlockSize = 1;
1255			ataCdb->generic.RegisterSelect = REG_COMMAND;
1256			ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1257			isd200_srb_set_bufflen(srb, 0);
1258		} else {
1259			usb_stor_dbg(us, "   Media Status not supported, just report okay\n");
1260			srb->result = SAM_STAT_GOOD;
1261			sendToTransport = 0;
1262		}
1263		break;
1264
1265	case TEST_UNIT_READY:
1266		usb_stor_dbg(us, "   ATA OUT - SCSIOP_TEST_UNIT_READY\n");
1267
1268		if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
1269		{
1270			ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1271			ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1272			ataCdb->generic.TransferBlockSize = 1;
1273			ataCdb->generic.RegisterSelect = REG_COMMAND;
1274			ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1275			isd200_srb_set_bufflen(srb, 0);
1276		} else {
1277			usb_stor_dbg(us, "   Media Status not supported, just report okay\n");
1278			srb->result = SAM_STAT_GOOD;
1279			sendToTransport = 0;
1280		}
1281		break;
1282
1283	case READ_CAPACITY:
1284	{
1285		unsigned long capacity;
1286		struct read_capacity_data readCapacityData;
1287
1288		usb_stor_dbg(us, "   ATA OUT - SCSIOP_READ_CAPACITY\n");
1289
1290		if (ata_id_has_lba(id))
1291			capacity = ata_id_u32(id, ATA_ID_LBA_CAPACITY) - 1;
1292		else
1293			capacity = (id[ATA_ID_HEADS] * id[ATA_ID_CYLS] *
1294				    id[ATA_ID_SECTORS]) - 1;
1295
1296		readCapacityData.LogicalBlockAddress = cpu_to_be32(capacity);
1297		readCapacityData.BytesPerBlock = cpu_to_be32(0x200);
1298
1299		usb_stor_set_xfer_buf((unsigned char *) &readCapacityData,
1300				sizeof(readCapacityData), srb);
1301		srb->result = SAM_STAT_GOOD;
1302		sendToTransport = 0;
1303	}
1304	break;
1305
1306	case READ_10:
1307		usb_stor_dbg(us, "   ATA OUT - SCSIOP_READ\n");
1308
1309		lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1310		blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1311
1312		if (ata_id_has_lba(id)) {
1313			sectnum = (unsigned char)(lba);
1314			cylinder = (unsigned short)(lba>>8);
1315			head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1316		} else {
1317			sectnum = (u8)((lba % id[ATA_ID_SECTORS]) + 1);
1318			cylinder = (u16)(lba / (id[ATA_ID_SECTORS] *
1319					id[ATA_ID_HEADS]));
1320			head = (u8)((lba / id[ATA_ID_SECTORS]) %
1321					id[ATA_ID_HEADS]);
1322		}
1323		ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1324		ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1325		ataCdb->generic.TransferBlockSize = 1;
1326		ataCdb->generic.RegisterSelect =
1327		  REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
1328		  REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
1329		  REG_DEVICE_HEAD  | REG_COMMAND;
1330		ataCdb->write.SectorCountByte = (unsigned char)blockCount;
1331		ataCdb->write.SectorNumberByte = sectnum;
1332		ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
1333		ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
1334		ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
1335		ataCdb->write.CommandByte = ATA_CMD_PIO_READ;
1336		break;
1337
1338	case WRITE_10:
1339		usb_stor_dbg(us, "   ATA OUT - SCSIOP_WRITE\n");
1340
1341		lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1342		blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1343
1344		if (ata_id_has_lba(id)) {
1345			sectnum = (unsigned char)(lba);
1346			cylinder = (unsigned short)(lba>>8);
1347			head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1348		} else {
1349			sectnum = (u8)((lba % id[ATA_ID_SECTORS]) + 1);
1350			cylinder = (u16)(lba / (id[ATA_ID_SECTORS] *
1351					id[ATA_ID_HEADS]));
1352			head = (u8)((lba / id[ATA_ID_SECTORS]) %
1353					id[ATA_ID_HEADS]);
1354		}
1355		ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1356		ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1357		ataCdb->generic.TransferBlockSize = 1;
1358		ataCdb->generic.RegisterSelect =
1359		  REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
1360		  REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
1361		  REG_DEVICE_HEAD  | REG_COMMAND;
1362		ataCdb->write.SectorCountByte = (unsigned char)blockCount;
1363		ataCdb->write.SectorNumberByte = sectnum;
1364		ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
1365		ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
1366		ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
1367		ataCdb->write.CommandByte = ATA_CMD_PIO_WRITE;
1368		break;
1369
1370	case ALLOW_MEDIUM_REMOVAL:
1371		usb_stor_dbg(us, "   ATA OUT - SCSIOP_MEDIUM_REMOVAL\n");
1372
1373		if (info->DeviceFlags & DF_REMOVABLE_MEDIA) {
1374			usb_stor_dbg(us, "   srb->cmnd[4] = 0x%X\n",
1375				     srb->cmnd[4]);
1376	    
1377			ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1378			ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1379			ataCdb->generic.TransferBlockSize = 1;
1380			ataCdb->generic.RegisterSelect = REG_COMMAND;
1381			ataCdb->write.CommandByte = (srb->cmnd[4] & 0x1) ?
1382				ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK;
1383			isd200_srb_set_bufflen(srb, 0);
1384		} else {
1385			usb_stor_dbg(us, "   Not removeable media, just report okay\n");
1386			srb->result = SAM_STAT_GOOD;
1387			sendToTransport = 0;
1388		}
1389		break;
1390
1391	case START_STOP:    
1392		usb_stor_dbg(us, "   ATA OUT - SCSIOP_START_STOP_UNIT\n");
1393		usb_stor_dbg(us, "   srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
1394
1395		if ((srb->cmnd[4] & 0x3) == 0x2) {
1396			usb_stor_dbg(us, "   Media Eject\n");
1397			ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1398			ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1399			ataCdb->generic.TransferBlockSize = 0;
1400			ataCdb->generic.RegisterSelect = REG_COMMAND;
1401			ataCdb->write.CommandByte = ATA_COMMAND_MEDIA_EJECT;
1402		} else if ((srb->cmnd[4] & 0x3) == 0x1) {
1403			usb_stor_dbg(us, "   Get Media Status\n");
1404			ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1405			ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1406			ataCdb->generic.TransferBlockSize = 1;
1407			ataCdb->generic.RegisterSelect = REG_COMMAND;
1408			ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1409			isd200_srb_set_bufflen(srb, 0);
1410		} else {
1411			usb_stor_dbg(us, "   Nothing to do, just report okay\n");
1412			srb->result = SAM_STAT_GOOD;
1413			sendToTransport = 0;
1414		}
1415		break;
1416
1417	default:
1418		usb_stor_dbg(us, "Unsupported SCSI command - 0x%X\n",
1419			     srb->cmnd[0]);
1420		srb->result = DID_ERROR << 16;
1421		sendToTransport = 0;
1422		break;
1423	}
1424
1425	return(sendToTransport);
1426}
1427
1428
1429/**************************************************************************
1430 * isd200_free_info
1431 *
1432 * Frees the driver structure.
1433 */
1434static void isd200_free_info_ptrs(void *info_)
1435{
1436	struct isd200_info *info = (struct isd200_info *) info_;
1437
1438	if (info) {
1439		kfree(info->id);
1440		kfree(info->RegsBuf);
1441		kfree(info->srb.sense_buffer);
1442	}
1443}
1444
1445/**************************************************************************
1446 * isd200_init_info
1447 *									 
1448 * Allocates (if necessary) and initializes the driver structure.
1449 *
1450 * RETURNS:
1451 *    ISD status code
1452 */
1453static int isd200_init_info(struct us_data *us)
1454{
 
1455	struct isd200_info *info;
1456
1457	info = kzalloc(sizeof(struct isd200_info), GFP_KERNEL);
1458	if (!info)
1459		return ISD200_ERROR;
1460
1461	info->id = kzalloc(ATA_ID_WORDS * 2, GFP_KERNEL);
1462	info->RegsBuf = kmalloc(sizeof(info->ATARegs), GFP_KERNEL);
1463	info->srb.sense_buffer = kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
1464
1465	if (!info->id || !info->RegsBuf || !info->srb.sense_buffer) {
1466		isd200_free_info_ptrs(info);
1467		kfree(info);
1468		return ISD200_ERROR;
 
 
1469	}
1470
1471	us->extra = info;
1472	us->extra_destructor = isd200_free_info_ptrs;
 
 
 
1473
1474	return ISD200_GOOD;
1475}
1476
1477/**************************************************************************
1478 * Initialization for the ISD200 
1479 */
1480
1481static int isd200_Initialization(struct us_data *us)
1482{
1483	usb_stor_dbg(us, "ISD200 Initialization...\n");
1484
1485	/* Initialize ISD200 info struct */
1486
1487	if (isd200_init_info(us) == ISD200_ERROR) {
1488		usb_stor_dbg(us, "ERROR Initializing ISD200 Info struct\n");
1489	} else {
1490		/* Get device specific data */
1491
1492		if (isd200_get_inquiry_data(us) != ISD200_GOOD)
1493			usb_stor_dbg(us, "ISD200 Initialization Failure\n");
1494		else
1495			usb_stor_dbg(us, "ISD200 Initialization complete\n");
1496	}
1497
1498	return 0;
1499}
1500
1501
1502/**************************************************************************
1503 * Protocol and Transport for the ISD200 ASIC
1504 *
1505 * This protocol and transport are for ATA devices connected to an ISD200
1506 * ASIC.  An ATAPI device that is connected as a slave device will be
1507 * detected in the driver initialization function and the protocol will
1508 * be changed to an ATAPI protocol (Transparent SCSI).
1509 *
1510 */
1511
1512static void isd200_ata_command(struct scsi_cmnd *srb, struct us_data *us)
1513{
1514	int sendToTransport = 1, orig_bufflen;
1515	union ata_cdb ataCdb;
1516
1517	/* Make sure driver was initialized */
1518
1519	if (us->extra == NULL) {
1520		usb_stor_dbg(us, "ERROR Driver not initialized\n");
1521		srb->result = DID_ERROR << 16;
1522		return;
1523	}
1524
1525	scsi_set_resid(srb, 0);
1526	/* scsi_bufflen might change in protocol translation to ata */
1527	orig_bufflen = scsi_bufflen(srb);
1528	sendToTransport = isd200_scsi_to_ata(srb, us, &ataCdb);
1529
1530	/* send the command to the transport layer */
1531	if (sendToTransport)
1532		isd200_invoke_transport(us, srb, &ataCdb);
1533
1534	isd200_srb_set_bufflen(srb, orig_bufflen);
1535}
1536
1537static struct scsi_host_template isd200_host_template;
1538
1539static int isd200_probe(struct usb_interface *intf,
1540			 const struct usb_device_id *id)
1541{
1542	struct us_data *us;
1543	int result;
1544
1545	result = usb_stor_probe1(&us, intf, id,
1546			(id - isd200_usb_ids) + isd200_unusual_dev_list,
1547			&isd200_host_template);
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 =		DRV_NAME,
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,
1569	.no_dynamic_id = 1,
1570};
1571
1572module_usb_stor_driver(isd200_driver, isd200_host_template, DRV_NAME);