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