isd200.c 44 KB

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