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