isd200.c 45 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) }
  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. usb_stor_dbg(us, " 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. usb_stor_dbg(us, " 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. usb_stor_dbg(us, " 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. usb_stor_dbg(us, " 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. usb_stor_dbg(us, " 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. usb_stor_dbg(us, " 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. usb_stor_dbg(us, "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. usb_stor_dbg(us, " isd200_action(0x%02x) error: %d\n",
  487. action, status);
  488. status = ISD200_ERROR;
  489. /* need to reset device here */
  490. }
  491. return status;
  492. }
  493. /**************************************************************************
  494. * isd200_read_regs
  495. *
  496. * Read ATA Registers
  497. *
  498. * RETURNS:
  499. * ISD status code
  500. */
  501. static int isd200_read_regs( struct us_data *us )
  502. {
  503. struct isd200_info *info = (struct isd200_info *)us->extra;
  504. int retStatus = ISD200_GOOD;
  505. int transferStatus;
  506. usb_stor_dbg(us, "Entering isd200_IssueATAReadRegs\n");
  507. transferStatus = isd200_action( us, ACTION_READ_STATUS,
  508. info->RegsBuf, sizeof(info->ATARegs) );
  509. if (transferStatus != ISD200_TRANSPORT_GOOD) {
  510. usb_stor_dbg(us, " Error reading ATA registers\n");
  511. retStatus = ISD200_ERROR;
  512. } else {
  513. memcpy(info->ATARegs, info->RegsBuf, sizeof(info->ATARegs));
  514. usb_stor_dbg(us, " Got ATA Register[ATA_REG_ERROR_OFFSET] = 0x%x\n",
  515. info->ATARegs[ATA_REG_ERROR_OFFSET]);
  516. }
  517. return retStatus;
  518. }
  519. /**************************************************************************
  520. * Invoke the transport and basic error-handling/recovery methods
  521. *
  522. * This is used by the protocol layers to actually send the message to
  523. * the device and receive the response.
  524. */
  525. static void isd200_invoke_transport( struct us_data *us,
  526. struct scsi_cmnd *srb,
  527. union ata_cdb *ataCdb )
  528. {
  529. int need_auto_sense = 0;
  530. int transferStatus;
  531. int result;
  532. /* send the command to the transport layer */
  533. memcpy(srb->cmnd, ataCdb, sizeof(ataCdb->generic));
  534. srb->cmd_len = sizeof(ataCdb->generic);
  535. transferStatus = usb_stor_Bulk_transport(srb, us);
  536. /* if the command gets aborted by the higher layers, we need to
  537. * short-circuit all other processing
  538. */
  539. if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
  540. usb_stor_dbg(us, "-- command was aborted\n");
  541. goto Handle_Abort;
  542. }
  543. switch (transferStatus) {
  544. case USB_STOR_TRANSPORT_GOOD:
  545. /* Indicate a good result */
  546. srb->result = SAM_STAT_GOOD;
  547. break;
  548. case USB_STOR_TRANSPORT_NO_SENSE:
  549. usb_stor_dbg(us, "-- transport indicates protocol failure\n");
  550. srb->result = SAM_STAT_CHECK_CONDITION;
  551. return;
  552. case USB_STOR_TRANSPORT_FAILED:
  553. usb_stor_dbg(us, "-- transport indicates command failure\n");
  554. need_auto_sense = 1;
  555. break;
  556. case USB_STOR_TRANSPORT_ERROR:
  557. usb_stor_dbg(us, "-- transport indicates transport error\n");
  558. srb->result = DID_ERROR << 16;
  559. /* Need reset here */
  560. return;
  561. default:
  562. usb_stor_dbg(us, "-- transport indicates unknown error\n");
  563. srb->result = DID_ERROR << 16;
  564. /* Need reset here */
  565. return;
  566. }
  567. if ((scsi_get_resid(srb) > 0) &&
  568. !((srb->cmnd[0] == REQUEST_SENSE) ||
  569. (srb->cmnd[0] == INQUIRY) ||
  570. (srb->cmnd[0] == MODE_SENSE) ||
  571. (srb->cmnd[0] == LOG_SENSE) ||
  572. (srb->cmnd[0] == MODE_SENSE_10))) {
  573. usb_stor_dbg(us, "-- unexpectedly short transfer\n");
  574. need_auto_sense = 1;
  575. }
  576. if (need_auto_sense) {
  577. result = isd200_read_regs(us);
  578. if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
  579. usb_stor_dbg(us, "-- auto-sense aborted\n");
  580. goto Handle_Abort;
  581. }
  582. if (result == ISD200_GOOD) {
  583. isd200_build_sense(us, srb);
  584. srb->result = SAM_STAT_CHECK_CONDITION;
  585. /* If things are really okay, then let's show that */
  586. if ((srb->sense_buffer[2] & 0xf) == 0x0)
  587. srb->result = SAM_STAT_GOOD;
  588. } else {
  589. srb->result = DID_ERROR << 16;
  590. /* Need reset here */
  591. }
  592. }
  593. /* Regardless of auto-sense, if we _know_ we have an error
  594. * condition, show that in the result code
  595. */
  596. if (transferStatus == USB_STOR_TRANSPORT_FAILED)
  597. srb->result = SAM_STAT_CHECK_CONDITION;
  598. return;
  599. /* abort processing: the bulk-only transport requires a reset
  600. * following an abort */
  601. Handle_Abort:
  602. srb->result = DID_ABORT << 16;
  603. /* permit the reset transfer to take place */
  604. clear_bit(US_FLIDX_ABORTING, &us->dflags);
  605. /* Need reset here */
  606. }
  607. #ifdef CONFIG_USB_STORAGE_DEBUG
  608. static void isd200_log_config(struct us_data *us, struct isd200_info *info)
  609. {
  610. usb_stor_dbg(us, " Event Notification: 0x%x\n",
  611. info->ConfigData.EventNotification);
  612. usb_stor_dbg(us, " External Clock: 0x%x\n",
  613. info->ConfigData.ExternalClock);
  614. usb_stor_dbg(us, " ATA Init Timeout: 0x%x\n",
  615. info->ConfigData.ATAInitTimeout);
  616. usb_stor_dbg(us, " ATAPI Command Block Size: 0x%x\n",
  617. (info->ConfigData.ATAConfig & ATACFG_BLOCKSIZE) >> 6);
  618. usb_stor_dbg(us, " Master/Slave Selection: 0x%x\n",
  619. info->ConfigData.ATAConfig & ATACFG_MASTER);
  620. usb_stor_dbg(us, " ATAPI Reset: 0x%x\n",
  621. info->ConfigData.ATAConfig & ATACFG_ATAPI_RESET);
  622. usb_stor_dbg(us, " ATA Timing: 0x%x\n",
  623. info->ConfigData.ATAConfig & ATACFG_TIMING);
  624. usb_stor_dbg(us, " ATA Major Command: 0x%x\n",
  625. info->ConfigData.ATAMajorCommand);
  626. usb_stor_dbg(us, " ATA Minor Command: 0x%x\n",
  627. info->ConfigData.ATAMinorCommand);
  628. usb_stor_dbg(us, " Init Status: 0x%x\n",
  629. info->ConfigData.ATAExtraConfig & ATACFGE_INIT_STATUS);
  630. usb_stor_dbg(us, " Config Descriptor 2: 0x%x\n",
  631. info->ConfigData.ATAExtraConfig & ATACFGE_CONF_DESC2);
  632. usb_stor_dbg(us, " Skip Device Boot: 0x%x\n",
  633. info->ConfigData.ATAExtraConfig & ATACFGE_SKIP_BOOT);
  634. usb_stor_dbg(us, " ATA 3 State Supsend: 0x%x\n",
  635. info->ConfigData.ATAExtraConfig & ATACFGE_STATE_SUSPEND);
  636. usb_stor_dbg(us, " Descriptor Override: 0x%x\n",
  637. info->ConfigData.ATAExtraConfig & ATACFGE_DESC_OVERRIDE);
  638. usb_stor_dbg(us, " Last LUN Identifier: 0x%x\n",
  639. info->ConfigData.ATAExtraConfig & ATACFGE_LAST_LUN);
  640. usb_stor_dbg(us, " SRST Enable: 0x%x\n",
  641. info->ConfigData.ATAExtraConfig & CFG_CAPABILITY_SRST);
  642. }
  643. #endif
  644. /**************************************************************************
  645. * isd200_write_config
  646. *
  647. * Write the ISD200 Configuration data
  648. *
  649. * RETURNS:
  650. * ISD status code
  651. */
  652. static int isd200_write_config( struct us_data *us )
  653. {
  654. struct isd200_info *info = (struct isd200_info *)us->extra;
  655. int retStatus = ISD200_GOOD;
  656. int result;
  657. #ifdef CONFIG_USB_STORAGE_DEBUG
  658. usb_stor_dbg(us, "Entering isd200_write_config\n");
  659. usb_stor_dbg(us, " Writing the following ISD200 Config Data:\n");
  660. isd200_log_config(us, info);
  661. #endif
  662. /* let's send the command via the control pipe */
  663. result = usb_stor_ctrl_transfer(
  664. us,
  665. us->send_ctrl_pipe,
  666. 0x01,
  667. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  668. 0x0000,
  669. 0x0002,
  670. (void *) &info->ConfigData,
  671. sizeof(info->ConfigData));
  672. if (result >= 0) {
  673. usb_stor_dbg(us, " ISD200 Config Data was written successfully\n");
  674. } else {
  675. usb_stor_dbg(us, " Request to write ISD200 Config Data failed!\n");
  676. retStatus = ISD200_ERROR;
  677. }
  678. usb_stor_dbg(us, "Leaving isd200_write_config %08X\n", retStatus);
  679. return retStatus;
  680. }
  681. /**************************************************************************
  682. * isd200_read_config
  683. *
  684. * Reads the ISD200 Configuration data
  685. *
  686. * RETURNS:
  687. * ISD status code
  688. */
  689. static int isd200_read_config( struct us_data *us )
  690. {
  691. struct isd200_info *info = (struct isd200_info *)us->extra;
  692. int retStatus = ISD200_GOOD;
  693. int result;
  694. usb_stor_dbg(us, "Entering isd200_read_config\n");
  695. /* read the configuration information from ISD200. Use this to */
  696. /* determine what the special ATA CDB bytes are. */
  697. result = usb_stor_ctrl_transfer(
  698. us,
  699. us->recv_ctrl_pipe,
  700. 0x02,
  701. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  702. 0x0000,
  703. 0x0002,
  704. (void *) &info->ConfigData,
  705. sizeof(info->ConfigData));
  706. if (result >= 0) {
  707. usb_stor_dbg(us, " Retrieved the following ISD200 Config Data:\n");
  708. #ifdef CONFIG_USB_STORAGE_DEBUG
  709. isd200_log_config(us, info);
  710. #endif
  711. } else {
  712. usb_stor_dbg(us, " Request to get ISD200 Config Data failed!\n");
  713. retStatus = ISD200_ERROR;
  714. }
  715. usb_stor_dbg(us, "Leaving isd200_read_config %08X\n", retStatus);
  716. return retStatus;
  717. }
  718. /**************************************************************************
  719. * isd200_atapi_soft_reset
  720. *
  721. * Perform an Atapi Soft Reset on the device
  722. *
  723. * RETURNS:
  724. * NT status code
  725. */
  726. static int isd200_atapi_soft_reset( struct us_data *us )
  727. {
  728. int retStatus = ISD200_GOOD;
  729. int transferStatus;
  730. usb_stor_dbg(us, "Entering isd200_atapi_soft_reset\n");
  731. transferStatus = isd200_action( us, ACTION_SOFT_RESET, NULL, 0 );
  732. if (transferStatus != ISD200_TRANSPORT_GOOD) {
  733. usb_stor_dbg(us, " Error issuing Atapi Soft Reset\n");
  734. retStatus = ISD200_ERROR;
  735. }
  736. usb_stor_dbg(us, "Leaving isd200_atapi_soft_reset %08X\n", retStatus);
  737. return retStatus;
  738. }
  739. /**************************************************************************
  740. * isd200_srst
  741. *
  742. * Perform an SRST on the device
  743. *
  744. * RETURNS:
  745. * ISD status code
  746. */
  747. static int isd200_srst( struct us_data *us )
  748. {
  749. int retStatus = ISD200_GOOD;
  750. int transferStatus;
  751. usb_stor_dbg(us, "Entering isd200_SRST\n");
  752. transferStatus = isd200_action( us, ACTION_RESET, NULL, 0 );
  753. /* check to see if this request failed */
  754. if (transferStatus != ISD200_TRANSPORT_GOOD) {
  755. usb_stor_dbg(us, " Error issuing SRST\n");
  756. retStatus = ISD200_ERROR;
  757. } else {
  758. /* delay 10ms to give the drive a chance to see it */
  759. msleep(10);
  760. transferStatus = isd200_action( us, ACTION_REENABLE, NULL, 0 );
  761. if (transferStatus != ISD200_TRANSPORT_GOOD) {
  762. usb_stor_dbg(us, " Error taking drive out of reset\n");
  763. retStatus = ISD200_ERROR;
  764. } else {
  765. /* delay 50ms to give the drive a chance to recover after SRST */
  766. msleep(50);
  767. }
  768. }
  769. usb_stor_dbg(us, "Leaving isd200_srst %08X\n", retStatus);
  770. return retStatus;
  771. }
  772. /**************************************************************************
  773. * isd200_try_enum
  774. *
  775. * Helper function for isd200_manual_enum(). Does ENUM and READ_STATUS
  776. * and tries to analyze the status registers
  777. *
  778. * RETURNS:
  779. * ISD status code
  780. */
  781. static int isd200_try_enum(struct us_data *us, unsigned char master_slave,
  782. int detect )
  783. {
  784. int status = ISD200_GOOD;
  785. unsigned long endTime;
  786. struct isd200_info *info = (struct isd200_info *)us->extra;
  787. unsigned char *regs = info->RegsBuf;
  788. int recheckAsMaster = 0;
  789. if ( detect )
  790. endTime = jiffies + ISD200_ENUM_DETECT_TIMEOUT * HZ;
  791. else
  792. endTime = jiffies + ISD200_ENUM_BSY_TIMEOUT * HZ;
  793. /* loop until we detect !BSY or timeout */
  794. while(1) {
  795. status = isd200_action( us, ACTION_ENUM, NULL, master_slave );
  796. if ( status != ISD200_GOOD )
  797. break;
  798. status = isd200_action( us, ACTION_READ_STATUS,
  799. regs, 8 );
  800. if ( status != ISD200_GOOD )
  801. break;
  802. if (!detect) {
  803. if (regs[ATA_REG_STATUS_OFFSET] & ATA_BUSY) {
  804. usb_stor_dbg(us, " %s status is still BSY, try again...\n",
  805. master_slave == ATA_ADDRESS_DEVHEAD_STD ?
  806. "Master" : "Slave");
  807. } else {
  808. usb_stor_dbg(us, " %s status !BSY, continue with next operation\n",
  809. master_slave == ATA_ADDRESS_DEVHEAD_STD ?
  810. "Master" : "Slave");
  811. break;
  812. }
  813. }
  814. /* check for ATA_BUSY and */
  815. /* ATA_DF (workaround ATA Zip drive) and */
  816. /* ATA_ERR (workaround for Archos CD-ROM) */
  817. else if (regs[ATA_REG_STATUS_OFFSET] &
  818. (ATA_BUSY | ATA_DF | ATA_ERR)) {
  819. usb_stor_dbg(us, " Status indicates it is not ready, try again...\n");
  820. }
  821. /* check for DRDY, ATA devices set DRDY after SRST */
  822. else if (regs[ATA_REG_STATUS_OFFSET] & ATA_DRDY) {
  823. usb_stor_dbg(us, " Identified ATA device\n");
  824. info->DeviceFlags |= DF_ATA_DEVICE;
  825. info->DeviceHead = master_slave;
  826. break;
  827. }
  828. /* check Cylinder High/Low to
  829. determine if it is an ATAPI device
  830. */
  831. else if (regs[ATA_REG_HCYL_OFFSET] == 0xEB &&
  832. regs[ATA_REG_LCYL_OFFSET] == 0x14) {
  833. /* It seems that the RICOH
  834. MP6200A CD/RW drive will
  835. report itself okay as a
  836. slave when it is really a
  837. master. So this check again
  838. as a master device just to
  839. make sure it doesn't report
  840. itself okay as a master also
  841. */
  842. if ((master_slave & ATA_ADDRESS_DEVHEAD_SLAVE) &&
  843. !recheckAsMaster) {
  844. usb_stor_dbg(us, " Identified ATAPI device as slave. Rechecking again as master\n");
  845. recheckAsMaster = 1;
  846. master_slave = ATA_ADDRESS_DEVHEAD_STD;
  847. } else {
  848. usb_stor_dbg(us, " Identified ATAPI device\n");
  849. info->DeviceHead = master_slave;
  850. status = isd200_atapi_soft_reset(us);
  851. break;
  852. }
  853. } else {
  854. usb_stor_dbg(us, " Not ATA, not ATAPI - Weird\n");
  855. break;
  856. }
  857. /* check for timeout on this request */
  858. if (time_after_eq(jiffies, endTime)) {
  859. if (!detect)
  860. usb_stor_dbg(us, " BSY check timeout, just continue with next operation...\n");
  861. else
  862. usb_stor_dbg(us, " Device detect timeout!\n");
  863. break;
  864. }
  865. }
  866. return status;
  867. }
  868. /**************************************************************************
  869. * isd200_manual_enum
  870. *
  871. * Determines if the drive attached is an ATA or ATAPI and if it is a
  872. * master or slave.
  873. *
  874. * RETURNS:
  875. * ISD status code
  876. */
  877. static int isd200_manual_enum(struct us_data *us)
  878. {
  879. struct isd200_info *info = (struct isd200_info *)us->extra;
  880. int retStatus = ISD200_GOOD;
  881. usb_stor_dbg(us, "Entering isd200_manual_enum\n");
  882. retStatus = isd200_read_config(us);
  883. if (retStatus == ISD200_GOOD) {
  884. int isslave;
  885. /* master or slave? */
  886. retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 0);
  887. if (retStatus == ISD200_GOOD)
  888. retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_SLAVE, 0);
  889. if (retStatus == ISD200_GOOD) {
  890. retStatus = isd200_srst(us);
  891. if (retStatus == ISD200_GOOD)
  892. /* ata or atapi? */
  893. retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 1);
  894. }
  895. isslave = (info->DeviceHead & ATA_ADDRESS_DEVHEAD_SLAVE) ? 1 : 0;
  896. if (!(info->ConfigData.ATAConfig & ATACFG_MASTER)) {
  897. usb_stor_dbg(us, " Setting Master/Slave selection to %d\n",
  898. isslave);
  899. info->ConfigData.ATAConfig &= 0x3f;
  900. info->ConfigData.ATAConfig |= (isslave<<6);
  901. retStatus = isd200_write_config(us);
  902. }
  903. }
  904. usb_stor_dbg(us, "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(struct us_data *us, u16 *id)
  920. {
  921. usb_stor_dbg(us, " Identify Data Structure:\n");
  922. usb_stor_dbg(us, " config = 0x%x\n", id[ATA_ID_CONFIG]);
  923. usb_stor_dbg(us, " cyls = 0x%x\n", id[ATA_ID_CYLS]);
  924. usb_stor_dbg(us, " heads = 0x%x\n", id[ATA_ID_HEADS]);
  925. usb_stor_dbg(us, " track_bytes = 0x%x\n", id[4]);
  926. usb_stor_dbg(us, " sector_bytes = 0x%x\n", id[5]);
  927. usb_stor_dbg(us, " sectors = 0x%x\n", id[ATA_ID_SECTORS]);
  928. usb_stor_dbg(us, " serial_no[0] = 0x%x\n", *(char *)&id[ATA_ID_SERNO]);
  929. usb_stor_dbg(us, " buf_type = 0x%x\n", id[20]);
  930. usb_stor_dbg(us, " buf_size = 0x%x\n", id[ATA_ID_BUF_SIZE]);
  931. usb_stor_dbg(us, " ecc_bytes = 0x%x\n", id[22]);
  932. usb_stor_dbg(us, " fw_rev[0] = 0x%x\n", *(char *)&id[ATA_ID_FW_REV]);
  933. usb_stor_dbg(us, " model[0] = 0x%x\n", *(char *)&id[ATA_ID_PROD]);
  934. usb_stor_dbg(us, " max_multsect = 0x%x\n", id[ATA_ID_MAX_MULTSECT] & 0xff);
  935. usb_stor_dbg(us, " dword_io = 0x%x\n", id[ATA_ID_DWORD_IO]);
  936. usb_stor_dbg(us, " capability = 0x%x\n", id[ATA_ID_CAPABILITY] >> 8);
  937. usb_stor_dbg(us, " tPIO = 0x%x\n", id[ATA_ID_OLD_PIO_MODES] >> 8);
  938. usb_stor_dbg(us, " tDMA = 0x%x\n", id[ATA_ID_OLD_DMA_MODES] >> 8);
  939. usb_stor_dbg(us, " field_valid = 0x%x\n", id[ATA_ID_FIELD_VALID]);
  940. usb_stor_dbg(us, " cur_cyls = 0x%x\n", id[ATA_ID_CUR_CYLS]);
  941. usb_stor_dbg(us, " cur_heads = 0x%x\n", id[ATA_ID_CUR_HEADS]);
  942. usb_stor_dbg(us, " cur_sectors = 0x%x\n", id[ATA_ID_CUR_SECTORS]);
  943. usb_stor_dbg(us, " cur_capacity = 0x%x\n", ata_id_u32(id, 57));
  944. usb_stor_dbg(us, " multsect = 0x%x\n", id[ATA_ID_MULTSECT] & 0xff);
  945. usb_stor_dbg(us, " lba_capacity = 0x%x\n", ata_id_u32(id, ATA_ID_LBA_CAPACITY));
  946. usb_stor_dbg(us, " command_set_1 = 0x%x\n", id[ATA_ID_COMMAND_SET_1]);
  947. usb_stor_dbg(us, " 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. usb_stor_dbg(us, "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. usb_stor_dbg(us, " 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(us, 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. usb_stor_dbg(us, " 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. usb_stor_dbg(us, "Protocol changed to: %s\n",
  1029. us->protocol_name);
  1030. /* Free driver structure */
  1031. us->extra_destructor(info);
  1032. kfree(info);
  1033. us->extra = NULL;
  1034. us->extra_destructor = NULL;
  1035. }
  1036. }
  1037. usb_stor_dbg(us, "Leaving isd200_get_inquiry_data %08X\n", retStatus);
  1038. return(retStatus);
  1039. }
  1040. /**************************************************************************
  1041. * isd200_scsi_to_ata
  1042. *
  1043. * Translate SCSI commands to ATA commands.
  1044. *
  1045. * RETURNS:
  1046. * 1 if the command needs to be sent to the transport layer
  1047. * 0 otherwise
  1048. */
  1049. static int isd200_scsi_to_ata(struct scsi_cmnd *srb, struct us_data *us,
  1050. union ata_cdb * ataCdb)
  1051. {
  1052. struct isd200_info *info = (struct isd200_info *)us->extra;
  1053. u16 *id = info->id;
  1054. int sendToTransport = 1;
  1055. unsigned char sectnum, head;
  1056. unsigned short cylinder;
  1057. unsigned long lba;
  1058. unsigned long blockCount;
  1059. unsigned char senseData[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  1060. memset(ataCdb, 0, sizeof(union ata_cdb));
  1061. /* SCSI Command */
  1062. switch (srb->cmnd[0]) {
  1063. case INQUIRY:
  1064. usb_stor_dbg(us, " ATA OUT - INQUIRY\n");
  1065. /* copy InquiryData */
  1066. usb_stor_set_xfer_buf((unsigned char *) &info->InquiryData,
  1067. sizeof(info->InquiryData), srb);
  1068. srb->result = SAM_STAT_GOOD;
  1069. sendToTransport = 0;
  1070. break;
  1071. case MODE_SENSE:
  1072. usb_stor_dbg(us, " ATA OUT - SCSIOP_MODE_SENSE\n");
  1073. /* Initialize the return buffer */
  1074. usb_stor_set_xfer_buf(senseData, sizeof(senseData), srb);
  1075. if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
  1076. {
  1077. ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  1078. ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  1079. ataCdb->generic.TransferBlockSize = 1;
  1080. ataCdb->generic.RegisterSelect = REG_COMMAND;
  1081. ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
  1082. isd200_srb_set_bufflen(srb, 0);
  1083. } else {
  1084. usb_stor_dbg(us, " Media Status not supported, just report okay\n");
  1085. srb->result = SAM_STAT_GOOD;
  1086. sendToTransport = 0;
  1087. }
  1088. break;
  1089. case TEST_UNIT_READY:
  1090. usb_stor_dbg(us, " ATA OUT - SCSIOP_TEST_UNIT_READY\n");
  1091. if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
  1092. {
  1093. ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  1094. ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  1095. ataCdb->generic.TransferBlockSize = 1;
  1096. ataCdb->generic.RegisterSelect = REG_COMMAND;
  1097. ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
  1098. isd200_srb_set_bufflen(srb, 0);
  1099. } else {
  1100. usb_stor_dbg(us, " Media Status not supported, just report okay\n");
  1101. srb->result = SAM_STAT_GOOD;
  1102. sendToTransport = 0;
  1103. }
  1104. break;
  1105. case READ_CAPACITY:
  1106. {
  1107. unsigned long capacity;
  1108. struct read_capacity_data readCapacityData;
  1109. usb_stor_dbg(us, " ATA OUT - SCSIOP_READ_CAPACITY\n");
  1110. if (ata_id_has_lba(id))
  1111. capacity = ata_id_u32(id, ATA_ID_LBA_CAPACITY) - 1;
  1112. else
  1113. capacity = (id[ATA_ID_HEADS] * id[ATA_ID_CYLS] *
  1114. id[ATA_ID_SECTORS]) - 1;
  1115. readCapacityData.LogicalBlockAddress = cpu_to_be32(capacity);
  1116. readCapacityData.BytesPerBlock = cpu_to_be32(0x200);
  1117. usb_stor_set_xfer_buf((unsigned char *) &readCapacityData,
  1118. sizeof(readCapacityData), srb);
  1119. srb->result = SAM_STAT_GOOD;
  1120. sendToTransport = 0;
  1121. }
  1122. break;
  1123. case READ_10:
  1124. usb_stor_dbg(us, " ATA OUT - SCSIOP_READ\n");
  1125. lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
  1126. blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
  1127. if (ata_id_has_lba(id)) {
  1128. sectnum = (unsigned char)(lba);
  1129. cylinder = (unsigned short)(lba>>8);
  1130. head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
  1131. } else {
  1132. sectnum = (u8)((lba % id[ATA_ID_SECTORS]) + 1);
  1133. cylinder = (u16)(lba / (id[ATA_ID_SECTORS] *
  1134. id[ATA_ID_HEADS]));
  1135. head = (u8)((lba / id[ATA_ID_SECTORS]) %
  1136. id[ATA_ID_HEADS]);
  1137. }
  1138. ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  1139. ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  1140. ataCdb->generic.TransferBlockSize = 1;
  1141. ataCdb->generic.RegisterSelect =
  1142. REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
  1143. REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
  1144. REG_DEVICE_HEAD | REG_COMMAND;
  1145. ataCdb->write.SectorCountByte = (unsigned char)blockCount;
  1146. ataCdb->write.SectorNumberByte = sectnum;
  1147. ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
  1148. ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
  1149. ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
  1150. ataCdb->write.CommandByte = ATA_CMD_PIO_READ;
  1151. break;
  1152. case WRITE_10:
  1153. usb_stor_dbg(us, " ATA OUT - SCSIOP_WRITE\n");
  1154. lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
  1155. blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
  1156. if (ata_id_has_lba(id)) {
  1157. sectnum = (unsigned char)(lba);
  1158. cylinder = (unsigned short)(lba>>8);
  1159. head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
  1160. } else {
  1161. sectnum = (u8)((lba % id[ATA_ID_SECTORS]) + 1);
  1162. cylinder = (u16)(lba / (id[ATA_ID_SECTORS] *
  1163. id[ATA_ID_HEADS]));
  1164. head = (u8)((lba / id[ATA_ID_SECTORS]) %
  1165. id[ATA_ID_HEADS]);
  1166. }
  1167. ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  1168. ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  1169. ataCdb->generic.TransferBlockSize = 1;
  1170. ataCdb->generic.RegisterSelect =
  1171. REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
  1172. REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
  1173. REG_DEVICE_HEAD | REG_COMMAND;
  1174. ataCdb->write.SectorCountByte = (unsigned char)blockCount;
  1175. ataCdb->write.SectorNumberByte = sectnum;
  1176. ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
  1177. ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
  1178. ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
  1179. ataCdb->write.CommandByte = ATA_CMD_PIO_WRITE;
  1180. break;
  1181. case ALLOW_MEDIUM_REMOVAL:
  1182. usb_stor_dbg(us, " ATA OUT - SCSIOP_MEDIUM_REMOVAL\n");
  1183. if (info->DeviceFlags & DF_REMOVABLE_MEDIA) {
  1184. usb_stor_dbg(us, " srb->cmnd[4] = 0x%X\n",
  1185. srb->cmnd[4]);
  1186. ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  1187. ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  1188. ataCdb->generic.TransferBlockSize = 1;
  1189. ataCdb->generic.RegisterSelect = REG_COMMAND;
  1190. ataCdb->write.CommandByte = (srb->cmnd[4] & 0x1) ?
  1191. ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK;
  1192. isd200_srb_set_bufflen(srb, 0);
  1193. } else {
  1194. usb_stor_dbg(us, " Not removeable media, just report okay\n");
  1195. srb->result = SAM_STAT_GOOD;
  1196. sendToTransport = 0;
  1197. }
  1198. break;
  1199. case START_STOP:
  1200. usb_stor_dbg(us, " ATA OUT - SCSIOP_START_STOP_UNIT\n");
  1201. usb_stor_dbg(us, " srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
  1202. if ((srb->cmnd[4] & 0x3) == 0x2) {
  1203. usb_stor_dbg(us, " Media Eject\n");
  1204. ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  1205. ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  1206. ataCdb->generic.TransferBlockSize = 0;
  1207. ataCdb->generic.RegisterSelect = REG_COMMAND;
  1208. ataCdb->write.CommandByte = ATA_COMMAND_MEDIA_EJECT;
  1209. } else if ((srb->cmnd[4] & 0x3) == 0x1) {
  1210. usb_stor_dbg(us, " Get Media Status\n");
  1211. ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  1212. ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  1213. ataCdb->generic.TransferBlockSize = 1;
  1214. ataCdb->generic.RegisterSelect = REG_COMMAND;
  1215. ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
  1216. isd200_srb_set_bufflen(srb, 0);
  1217. } else {
  1218. usb_stor_dbg(us, " Nothing to do, just report okay\n");
  1219. srb->result = SAM_STAT_GOOD;
  1220. sendToTransport = 0;
  1221. }
  1222. break;
  1223. default:
  1224. usb_stor_dbg(us, "Unsupported SCSI command - 0x%X\n",
  1225. srb->cmnd[0]);
  1226. srb->result = DID_ERROR << 16;
  1227. sendToTransport = 0;
  1228. break;
  1229. }
  1230. return(sendToTransport);
  1231. }
  1232. /**************************************************************************
  1233. * isd200_free_info
  1234. *
  1235. * Frees the driver structure.
  1236. */
  1237. static void isd200_free_info_ptrs(void *info_)
  1238. {
  1239. struct isd200_info *info = (struct isd200_info *) info_;
  1240. if (info) {
  1241. kfree(info->id);
  1242. kfree(info->RegsBuf);
  1243. kfree(info->srb.sense_buffer);
  1244. }
  1245. }
  1246. /**************************************************************************
  1247. * isd200_init_info
  1248. *
  1249. * Allocates (if necessary) and initializes the driver structure.
  1250. *
  1251. * RETURNS:
  1252. * ISD status code
  1253. */
  1254. static int isd200_init_info(struct us_data *us)
  1255. {
  1256. int retStatus = ISD200_GOOD;
  1257. struct isd200_info *info;
  1258. info = kzalloc(sizeof(struct isd200_info), GFP_KERNEL);
  1259. if (!info)
  1260. retStatus = ISD200_ERROR;
  1261. else {
  1262. info->id = kzalloc(ATA_ID_WORDS * 2, GFP_KERNEL);
  1263. info->RegsBuf = kmalloc(sizeof(info->ATARegs), GFP_KERNEL);
  1264. info->srb.sense_buffer =
  1265. kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
  1266. if (!info->id || !info->RegsBuf || !info->srb.sense_buffer) {
  1267. isd200_free_info_ptrs(info);
  1268. kfree(info);
  1269. retStatus = ISD200_ERROR;
  1270. }
  1271. }
  1272. if (retStatus == ISD200_GOOD) {
  1273. us->extra = info;
  1274. us->extra_destructor = isd200_free_info_ptrs;
  1275. }
  1276. return retStatus;
  1277. }
  1278. /**************************************************************************
  1279. * Initialization for the ISD200
  1280. */
  1281. static int isd200_Initialization(struct us_data *us)
  1282. {
  1283. usb_stor_dbg(us, "ISD200 Initialization...\n");
  1284. /* Initialize ISD200 info struct */
  1285. if (isd200_init_info(us) == ISD200_ERROR) {
  1286. usb_stor_dbg(us, "ERROR Initializing ISD200 Info struct\n");
  1287. } else {
  1288. /* Get device specific data */
  1289. if (isd200_get_inquiry_data(us) != ISD200_GOOD)
  1290. usb_stor_dbg(us, "ISD200 Initialization Failure\n");
  1291. else
  1292. usb_stor_dbg(us, "ISD200 Initialization complete\n");
  1293. }
  1294. return 0;
  1295. }
  1296. /**************************************************************************
  1297. * Protocol and Transport for the ISD200 ASIC
  1298. *
  1299. * This protocol and transport are for ATA devices connected to an ISD200
  1300. * ASIC. An ATAPI device that is connected as a slave device will be
  1301. * detected in the driver initialization function and the protocol will
  1302. * be changed to an ATAPI protocol (Transparent SCSI).
  1303. *
  1304. */
  1305. static void isd200_ata_command(struct scsi_cmnd *srb, struct us_data *us)
  1306. {
  1307. int sendToTransport = 1, orig_bufflen;
  1308. union ata_cdb ataCdb;
  1309. /* Make sure driver was initialized */
  1310. if (us->extra == NULL)
  1311. usb_stor_dbg(us, "ERROR Driver not initialized\n");
  1312. scsi_set_resid(srb, 0);
  1313. /* scsi_bufflen might change in protocol translation to ata */
  1314. orig_bufflen = scsi_bufflen(srb);
  1315. sendToTransport = isd200_scsi_to_ata(srb, us, &ataCdb);
  1316. /* send the command to the transport layer */
  1317. if (sendToTransport)
  1318. isd200_invoke_transport(us, srb, &ataCdb);
  1319. isd200_srb_set_bufflen(srb, orig_bufflen);
  1320. }
  1321. static int isd200_probe(struct usb_interface *intf,
  1322. const struct usb_device_id *id)
  1323. {
  1324. struct us_data *us;
  1325. int result;
  1326. result = usb_stor_probe1(&us, intf, id,
  1327. (id - isd200_usb_ids) + isd200_unusual_dev_list);
  1328. if (result)
  1329. return result;
  1330. us->protocol_name = "ISD200 ATA/ATAPI";
  1331. us->proto_handler = isd200_ata_command;
  1332. result = usb_stor_probe2(us);
  1333. return result;
  1334. }
  1335. static struct usb_driver isd200_driver = {
  1336. .name = "ums-isd200",
  1337. .probe = isd200_probe,
  1338. .disconnect = usb_stor_disconnect,
  1339. .suspend = usb_stor_suspend,
  1340. .resume = usb_stor_resume,
  1341. .reset_resume = usb_stor_reset_resume,
  1342. .pre_reset = usb_stor_pre_reset,
  1343. .post_reset = usb_stor_post_reset,
  1344. .id_table = isd200_usb_ids,
  1345. .soft_unbind = 1,
  1346. .no_dynamic_id = 1,
  1347. };
  1348. module_usb_driver(isd200_driver);