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