isd200.c 46 KB

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