isd200.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  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. 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. struct scsi_device srb_dev;
  419. struct isd200_info *info = (struct isd200_info *)us->extra;
  420. struct scsi_cmnd *srb = &info->srb;
  421. int status;
  422. memset(&ata, 0, sizeof(ata));
  423. memset(&srb_dev, 0, sizeof(srb_dev));
  424. srb->cmnd = info->cmnd;
  425. srb->device = &srb_dev;
  426. ++srb->serial_number;
  427. ata.generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  428. ata.generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  429. ata.generic.TransferBlockSize = 1;
  430. switch ( action ) {
  431. case ACTION_READ_STATUS:
  432. US_DEBUGP(" isd200_action(READ_STATUS)\n");
  433. ata.generic.ActionSelect = ACTION_SELECT_0|ACTION_SELECT_2;
  434. ata.generic.RegisterSelect =
  435. REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
  436. REG_STATUS | REG_ERROR;
  437. isd200_set_srb(info, DMA_FROM_DEVICE, pointer, value);
  438. break;
  439. case ACTION_ENUM:
  440. US_DEBUGP(" isd200_action(ENUM,0x%02x)\n",value);
  441. ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
  442. ACTION_SELECT_3|ACTION_SELECT_4|
  443. ACTION_SELECT_5;
  444. ata.generic.RegisterSelect = REG_DEVICE_HEAD;
  445. ata.write.DeviceHeadByte = value;
  446. isd200_set_srb(info, DMA_NONE, NULL, 0);
  447. break;
  448. case ACTION_RESET:
  449. US_DEBUGP(" isd200_action(RESET)\n");
  450. ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
  451. ACTION_SELECT_3|ACTION_SELECT_4;
  452. ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
  453. ata.write.DeviceControlByte = ATA_DC_RESET_CONTROLLER;
  454. isd200_set_srb(info, DMA_NONE, NULL, 0);
  455. break;
  456. case ACTION_REENABLE:
  457. US_DEBUGP(" isd200_action(REENABLE)\n");
  458. ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
  459. ACTION_SELECT_3|ACTION_SELECT_4;
  460. ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
  461. ata.write.DeviceControlByte = ATA_DC_REENABLE_CONTROLLER;
  462. isd200_set_srb(info, DMA_NONE, NULL, 0);
  463. break;
  464. case ACTION_SOFT_RESET:
  465. US_DEBUGP(" isd200_action(SOFT_RESET)\n");
  466. ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_5;
  467. ata.generic.RegisterSelect = REG_DEVICE_HEAD | REG_COMMAND;
  468. ata.write.DeviceHeadByte = info->DeviceHead;
  469. ata.write.CommandByte = ATA_CMD_DEV_RESET;
  470. isd200_set_srb(info, DMA_NONE, NULL, 0);
  471. break;
  472. case ACTION_IDENTIFY:
  473. US_DEBUGP(" isd200_action(IDENTIFY)\n");
  474. ata.generic.RegisterSelect = REG_COMMAND;
  475. ata.write.CommandByte = ATA_CMD_ID_ATA;
  476. isd200_set_srb(info, DMA_FROM_DEVICE, info->id,
  477. ATA_ID_WORDS * 2);
  478. break;
  479. default:
  480. US_DEBUGP("Error: Undefined action %d\n",action);
  481. return ISD200_ERROR;
  482. }
  483. memcpy(srb->cmnd, &ata, sizeof(ata.generic));
  484. srb->cmd_len = sizeof(ata.generic);
  485. status = usb_stor_Bulk_transport(srb, us);
  486. if (status == USB_STOR_TRANSPORT_GOOD)
  487. status = ISD200_GOOD;
  488. else {
  489. US_DEBUGP(" isd200_action(0x%02x) error: %d\n",action,status);
  490. status = ISD200_ERROR;
  491. /* need to reset device here */
  492. }
  493. return status;
  494. }
  495. /**************************************************************************
  496. * isd200_read_regs
  497. *
  498. * Read ATA Registers
  499. *
  500. * RETURNS:
  501. * ISD status code
  502. */
  503. static int isd200_read_regs( struct us_data *us )
  504. {
  505. struct isd200_info *info = (struct isd200_info *)us->extra;
  506. int retStatus = ISD200_GOOD;
  507. int transferStatus;
  508. US_DEBUGP("Entering isd200_IssueATAReadRegs\n");
  509. transferStatus = isd200_action( us, ACTION_READ_STATUS,
  510. info->RegsBuf, sizeof(info->ATARegs) );
  511. if (transferStatus != ISD200_TRANSPORT_GOOD) {
  512. US_DEBUGP(" Error reading ATA registers\n");
  513. retStatus = ISD200_ERROR;
  514. } else {
  515. memcpy(info->ATARegs, info->RegsBuf, sizeof(info->ATARegs));
  516. US_DEBUGP(" Got ATA Register[ATA_REG_ERROR_OFFSET] = 0x%x\n",
  517. info->ATARegs[ATA_REG_ERROR_OFFSET]);
  518. }
  519. return retStatus;
  520. }
  521. /**************************************************************************
  522. * Invoke the transport and basic error-handling/recovery methods
  523. *
  524. * This is used by the protocol layers to actually send the message to
  525. * the device and receive the response.
  526. */
  527. static void isd200_invoke_transport( struct us_data *us,
  528. struct scsi_cmnd *srb,
  529. union ata_cdb *ataCdb )
  530. {
  531. int need_auto_sense = 0;
  532. int transferStatus;
  533. int result;
  534. /* send the command to the transport layer */
  535. memcpy(srb->cmnd, ataCdb, sizeof(ataCdb->generic));
  536. srb->cmd_len = sizeof(ataCdb->generic);
  537. transferStatus = usb_stor_Bulk_transport(srb, us);
  538. /* if the command gets aborted by the higher layers, we need to
  539. * short-circuit all other processing
  540. */
  541. if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
  542. US_DEBUGP("-- command was aborted\n");
  543. goto Handle_Abort;
  544. }
  545. switch (transferStatus) {
  546. case USB_STOR_TRANSPORT_GOOD:
  547. /* Indicate a good result */
  548. srb->result = SAM_STAT_GOOD;
  549. break;
  550. case USB_STOR_TRANSPORT_NO_SENSE:
  551. US_DEBUGP("-- transport indicates protocol failure\n");
  552. srb->result = SAM_STAT_CHECK_CONDITION;
  553. return;
  554. case USB_STOR_TRANSPORT_FAILED:
  555. US_DEBUGP("-- transport indicates command failure\n");
  556. need_auto_sense = 1;
  557. break;
  558. case USB_STOR_TRANSPORT_ERROR:
  559. US_DEBUGP("-- transport indicates transport error\n");
  560. srb->result = DID_ERROR << 16;
  561. /* Need reset here */
  562. return;
  563. default:
  564. US_DEBUGP("-- transport indicates unknown error\n");
  565. srb->result = DID_ERROR << 16;
  566. /* Need reset here */
  567. return;
  568. }
  569. if ((scsi_get_resid(srb) > 0) &&
  570. !((srb->cmnd[0] == REQUEST_SENSE) ||
  571. (srb->cmnd[0] == INQUIRY) ||
  572. (srb->cmnd[0] == MODE_SENSE) ||
  573. (srb->cmnd[0] == LOG_SENSE) ||
  574. (srb->cmnd[0] == MODE_SENSE_10))) {
  575. US_DEBUGP("-- unexpectedly short transfer\n");
  576. need_auto_sense = 1;
  577. }
  578. if (need_auto_sense) {
  579. result = isd200_read_regs(us);
  580. if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
  581. US_DEBUGP("-- auto-sense aborted\n");
  582. goto Handle_Abort;
  583. }
  584. if (result == ISD200_GOOD) {
  585. isd200_build_sense(us, srb);
  586. srb->result = SAM_STAT_CHECK_CONDITION;
  587. /* If things are really okay, then let's show that */
  588. if ((srb->sense_buffer[2] & 0xf) == 0x0)
  589. srb->result = SAM_STAT_GOOD;
  590. } else {
  591. srb->result = DID_ERROR << 16;
  592. /* Need reset here */
  593. }
  594. }
  595. /* Regardless of auto-sense, if we _know_ we have an error
  596. * condition, show that in the result code
  597. */
  598. if (transferStatus == USB_STOR_TRANSPORT_FAILED)
  599. srb->result = SAM_STAT_CHECK_CONDITION;
  600. return;
  601. /* abort processing: the bulk-only transport requires a reset
  602. * following an abort */
  603. Handle_Abort:
  604. srb->result = DID_ABORT << 16;
  605. /* permit the reset transfer to take place */
  606. clear_bit(US_FLIDX_ABORTING, &us->dflags);
  607. /* Need reset here */
  608. }
  609. #ifdef CONFIG_USB_STORAGE_DEBUG
  610. static void isd200_log_config( struct isd200_info* info )
  611. {
  612. US_DEBUGP(" Event Notification: 0x%x\n",
  613. info->ConfigData.EventNotification);
  614. US_DEBUGP(" External Clock: 0x%x\n",
  615. info->ConfigData.ExternalClock);
  616. US_DEBUGP(" ATA Init Timeout: 0x%x\n",
  617. info->ConfigData.ATAInitTimeout);
  618. US_DEBUGP(" ATAPI Command Block Size: 0x%x\n",
  619. (info->ConfigData.ATAConfig & ATACFG_BLOCKSIZE) >> 6);
  620. US_DEBUGP(" Master/Slave Selection: 0x%x\n",
  621. info->ConfigData.ATAConfig & ATACFG_MASTER);
  622. US_DEBUGP(" ATAPI Reset: 0x%x\n",
  623. info->ConfigData.ATAConfig & ATACFG_ATAPI_RESET);
  624. US_DEBUGP(" ATA Timing: 0x%x\n",
  625. info->ConfigData.ATAConfig & ATACFG_TIMING);
  626. US_DEBUGP(" ATA Major Command: 0x%x\n",
  627. info->ConfigData.ATAMajorCommand);
  628. US_DEBUGP(" ATA Minor Command: 0x%x\n",
  629. info->ConfigData.ATAMinorCommand);
  630. US_DEBUGP(" Init Status: 0x%x\n",
  631. info->ConfigData.ATAExtraConfig & ATACFGE_INIT_STATUS);
  632. US_DEBUGP(" Config Descriptor 2: 0x%x\n",
  633. info->ConfigData.ATAExtraConfig & ATACFGE_CONF_DESC2);
  634. US_DEBUGP(" Skip Device Boot: 0x%x\n",
  635. info->ConfigData.ATAExtraConfig & ATACFGE_SKIP_BOOT);
  636. US_DEBUGP(" ATA 3 State Supsend: 0x%x\n",
  637. info->ConfigData.ATAExtraConfig & ATACFGE_STATE_SUSPEND);
  638. US_DEBUGP(" Descriptor Override: 0x%x\n",
  639. info->ConfigData.ATAExtraConfig & ATACFGE_DESC_OVERRIDE);
  640. US_DEBUGP(" Last LUN Identifier: 0x%x\n",
  641. info->ConfigData.ATAExtraConfig & ATACFGE_LAST_LUN);
  642. US_DEBUGP(" SRST Enable: 0x%x\n",
  643. info->ConfigData.ATAExtraConfig & CFG_CAPABILITY_SRST);
  644. }
  645. #endif
  646. /**************************************************************************
  647. * isd200_write_config
  648. *
  649. * Write the ISD200 Configuration data
  650. *
  651. * RETURNS:
  652. * ISD status code
  653. */
  654. static int isd200_write_config( struct us_data *us )
  655. {
  656. struct isd200_info *info = (struct isd200_info *)us->extra;
  657. int retStatus = ISD200_GOOD;
  658. int result;
  659. #ifdef CONFIG_USB_STORAGE_DEBUG
  660. US_DEBUGP("Entering isd200_write_config\n");
  661. US_DEBUGP(" Writing the following ISD200 Config Data:\n");
  662. isd200_log_config(info);
  663. #endif
  664. /* let's send the command via the control pipe */
  665. result = usb_stor_ctrl_transfer(
  666. us,
  667. us->send_ctrl_pipe,
  668. 0x01,
  669. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  670. 0x0000,
  671. 0x0002,
  672. (void *) &info->ConfigData,
  673. sizeof(info->ConfigData));
  674. if (result >= 0) {
  675. US_DEBUGP(" ISD200 Config Data was written successfully\n");
  676. } else {
  677. US_DEBUGP(" Request to write ISD200 Config Data failed!\n");
  678. retStatus = ISD200_ERROR;
  679. }
  680. US_DEBUGP("Leaving isd200_write_config %08X\n", retStatus);
  681. return retStatus;
  682. }
  683. /**************************************************************************
  684. * isd200_read_config
  685. *
  686. * Reads the ISD200 Configuration data
  687. *
  688. * RETURNS:
  689. * ISD status code
  690. */
  691. static int isd200_read_config( struct us_data *us )
  692. {
  693. struct isd200_info *info = (struct isd200_info *)us->extra;
  694. int retStatus = ISD200_GOOD;
  695. int result;
  696. US_DEBUGP("Entering isd200_read_config\n");
  697. /* read the configuration information from ISD200. Use this to */
  698. /* determine what the special ATA CDB bytes are. */
  699. result = usb_stor_ctrl_transfer(
  700. us,
  701. us->recv_ctrl_pipe,
  702. 0x02,
  703. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  704. 0x0000,
  705. 0x0002,
  706. (void *) &info->ConfigData,
  707. sizeof(info->ConfigData));
  708. if (result >= 0) {
  709. US_DEBUGP(" Retrieved the following ISD200 Config Data:\n");
  710. #ifdef CONFIG_USB_STORAGE_DEBUG
  711. isd200_log_config(info);
  712. #endif
  713. } else {
  714. US_DEBUGP(" Request to get ISD200 Config Data failed!\n");
  715. retStatus = ISD200_ERROR;
  716. }
  717. US_DEBUGP("Leaving isd200_read_config %08X\n", retStatus);
  718. return retStatus;
  719. }
  720. /**************************************************************************
  721. * isd200_atapi_soft_reset
  722. *
  723. * Perform an Atapi Soft Reset on the device
  724. *
  725. * RETURNS:
  726. * NT status code
  727. */
  728. static int isd200_atapi_soft_reset( struct us_data *us )
  729. {
  730. int retStatus = ISD200_GOOD;
  731. int transferStatus;
  732. US_DEBUGP("Entering isd200_atapi_soft_reset\n");
  733. transferStatus = isd200_action( us, ACTION_SOFT_RESET, NULL, 0 );
  734. if (transferStatus != ISD200_TRANSPORT_GOOD) {
  735. US_DEBUGP(" Error issuing Atapi Soft Reset\n");
  736. retStatus = ISD200_ERROR;
  737. }
  738. US_DEBUGP("Leaving isd200_atapi_soft_reset %08X\n", retStatus);
  739. return retStatus;
  740. }
  741. /**************************************************************************
  742. * isd200_srst
  743. *
  744. * Perform an SRST on the device
  745. *
  746. * RETURNS:
  747. * ISD status code
  748. */
  749. static int isd200_srst( struct us_data *us )
  750. {
  751. int retStatus = ISD200_GOOD;
  752. int transferStatus;
  753. US_DEBUGP("Entering isd200_SRST\n");
  754. transferStatus = isd200_action( us, ACTION_RESET, NULL, 0 );
  755. /* check to see if this request failed */
  756. if (transferStatus != ISD200_TRANSPORT_GOOD) {
  757. US_DEBUGP(" Error issuing SRST\n");
  758. retStatus = ISD200_ERROR;
  759. } else {
  760. /* delay 10ms to give the drive a chance to see it */
  761. msleep(10);
  762. transferStatus = isd200_action( us, ACTION_REENABLE, NULL, 0 );
  763. if (transferStatus != ISD200_TRANSPORT_GOOD) {
  764. US_DEBUGP(" Error taking drive out of reset\n");
  765. retStatus = ISD200_ERROR;
  766. } else {
  767. /* delay 50ms to give the drive a chance to recover after SRST */
  768. msleep(50);
  769. }
  770. }
  771. US_DEBUGP("Leaving isd200_srst %08X\n", retStatus);
  772. return retStatus;
  773. }
  774. /**************************************************************************
  775. * isd200_try_enum
  776. *
  777. * Helper function for isd200_manual_enum(). Does ENUM and READ_STATUS
  778. * and tries to analyze the status registers
  779. *
  780. * RETURNS:
  781. * ISD status code
  782. */
  783. static int isd200_try_enum(struct us_data *us, unsigned char master_slave,
  784. int detect )
  785. {
  786. int status = ISD200_GOOD;
  787. unsigned long endTime;
  788. struct isd200_info *info = (struct isd200_info *)us->extra;
  789. unsigned char *regs = info->RegsBuf;
  790. int recheckAsMaster = 0;
  791. if ( detect )
  792. endTime = jiffies + ISD200_ENUM_DETECT_TIMEOUT * HZ;
  793. else
  794. endTime = jiffies + ISD200_ENUM_BSY_TIMEOUT * HZ;
  795. /* loop until we detect !BSY or timeout */
  796. while(1) {
  797. #ifdef CONFIG_USB_STORAGE_DEBUG
  798. char* mstr = master_slave == ATA_ADDRESS_DEVHEAD_STD ?
  799. "Master" : "Slave";
  800. #endif
  801. status = isd200_action( us, ACTION_ENUM, NULL, master_slave );
  802. if ( status != ISD200_GOOD )
  803. break;
  804. status = isd200_action( us, ACTION_READ_STATUS,
  805. regs, 8 );
  806. if ( status != ISD200_GOOD )
  807. break;
  808. if (!detect) {
  809. if (regs[ATA_REG_STATUS_OFFSET] & ATA_BUSY) {
  810. US_DEBUGP(" %s status is still BSY, try again...\n",mstr);
  811. } else {
  812. US_DEBUGP(" %s status !BSY, continue with next operation\n",mstr);
  813. break;
  814. }
  815. }
  816. /* check for ATA_BUSY and */
  817. /* ATA_DF (workaround ATA Zip drive) and */
  818. /* ATA_ERR (workaround for Archos CD-ROM) */
  819. else if (regs[ATA_REG_STATUS_OFFSET] &
  820. (ATA_BUSY | ATA_DF | ATA_ERR)) {
  821. US_DEBUGP(" Status indicates it is not ready, try again...\n");
  822. }
  823. /* check for DRDY, ATA devices set DRDY after SRST */
  824. else if (regs[ATA_REG_STATUS_OFFSET] & ATA_DRDY) {
  825. US_DEBUGP(" Identified ATA device\n");
  826. info->DeviceFlags |= DF_ATA_DEVICE;
  827. info->DeviceHead = master_slave;
  828. break;
  829. }
  830. /* check Cylinder High/Low to
  831. determine if it is an ATAPI device
  832. */
  833. else if (regs[ATA_REG_HCYL_OFFSET] == 0xEB &&
  834. regs[ATA_REG_LCYL_OFFSET] == 0x14) {
  835. /* It seems that the RICOH
  836. MP6200A CD/RW drive will
  837. report itself okay as a
  838. slave when it is really a
  839. master. So this check again
  840. as a master device just to
  841. make sure it doesn't report
  842. itself okay as a master also
  843. */
  844. if ((master_slave & ATA_ADDRESS_DEVHEAD_SLAVE) &&
  845. !recheckAsMaster) {
  846. US_DEBUGP(" Identified ATAPI device as slave. Rechecking again as master\n");
  847. recheckAsMaster = 1;
  848. master_slave = ATA_ADDRESS_DEVHEAD_STD;
  849. } else {
  850. US_DEBUGP(" Identified ATAPI device\n");
  851. info->DeviceHead = master_slave;
  852. status = isd200_atapi_soft_reset(us);
  853. break;
  854. }
  855. } else {
  856. US_DEBUGP(" Not ATA, not ATAPI. Weird.\n");
  857. break;
  858. }
  859. /* check for timeout on this request */
  860. if (time_after_eq(jiffies, endTime)) {
  861. if (!detect)
  862. US_DEBUGP(" BSY check timeout, just continue with next operation...\n");
  863. else
  864. US_DEBUGP(" Device detect timeout!\n");
  865. break;
  866. }
  867. }
  868. return status;
  869. }
  870. /**************************************************************************
  871. * isd200_manual_enum
  872. *
  873. * Determines if the drive attached is an ATA or ATAPI and if it is a
  874. * master or slave.
  875. *
  876. * RETURNS:
  877. * ISD status code
  878. */
  879. static int isd200_manual_enum(struct us_data *us)
  880. {
  881. struct isd200_info *info = (struct isd200_info *)us->extra;
  882. int retStatus = ISD200_GOOD;
  883. US_DEBUGP("Entering isd200_manual_enum\n");
  884. retStatus = isd200_read_config(us);
  885. if (retStatus == ISD200_GOOD) {
  886. int isslave;
  887. /* master or slave? */
  888. retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 0);
  889. if (retStatus == ISD200_GOOD)
  890. retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_SLAVE, 0);
  891. if (retStatus == ISD200_GOOD) {
  892. retStatus = isd200_srst(us);
  893. if (retStatus == ISD200_GOOD)
  894. /* ata or atapi? */
  895. retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 1);
  896. }
  897. isslave = (info->DeviceHead & ATA_ADDRESS_DEVHEAD_SLAVE) ? 1 : 0;
  898. if (!(info->ConfigData.ATAConfig & ATACFG_MASTER)) {
  899. US_DEBUGP(" Setting Master/Slave selection to %d\n", isslave);
  900. info->ConfigData.ATAConfig &= 0x3f;
  901. info->ConfigData.ATAConfig |= (isslave<<6);
  902. retStatus = isd200_write_config(us);
  903. }
  904. }
  905. US_DEBUGP("Leaving isd200_manual_enum %08X\n", retStatus);
  906. return(retStatus);
  907. }
  908. static void isd200_fix_driveid(u16 *id)
  909. {
  910. #ifndef __LITTLE_ENDIAN
  911. # ifdef __BIG_ENDIAN
  912. int i;
  913. for (i = 0; i < ATA_ID_WORDS; i++)
  914. id[i] = __le16_to_cpu(id[i]);
  915. # else
  916. # error "Please fix <asm/byteorder.h>"
  917. # endif
  918. #endif
  919. }
  920. static void isd200_dump_driveid(u16 *id)
  921. {
  922. US_DEBUGP(" Identify Data Structure:\n");
  923. US_DEBUGP(" config = 0x%x\n", id[ATA_ID_CONFIG]);
  924. US_DEBUGP(" cyls = 0x%x\n", id[ATA_ID_CYLS]);
  925. US_DEBUGP(" heads = 0x%x\n", id[ATA_ID_HEADS]);
  926. US_DEBUGP(" track_bytes = 0x%x\n", id[4]);
  927. US_DEBUGP(" sector_bytes = 0x%x\n", id[5]);
  928. US_DEBUGP(" sectors = 0x%x\n", id[ATA_ID_SECTORS]);
  929. US_DEBUGP(" serial_no[0] = 0x%x\n", *(char *)&id[ATA_ID_SERNO]);
  930. US_DEBUGP(" buf_type = 0x%x\n", id[20]);
  931. US_DEBUGP(" buf_size = 0x%x\n", id[ATA_ID_BUF_SIZE]);
  932. US_DEBUGP(" ecc_bytes = 0x%x\n", id[22]);
  933. US_DEBUGP(" fw_rev[0] = 0x%x\n", *(char *)&id[ATA_ID_FW_REV]);
  934. US_DEBUGP(" model[0] = 0x%x\n", *(char *)&id[ATA_ID_PROD]);
  935. US_DEBUGP(" max_multsect = 0x%x\n", id[ATA_ID_MAX_MULTSECT] & 0xff);
  936. US_DEBUGP(" dword_io = 0x%x\n", id[ATA_ID_DWORD_IO]);
  937. US_DEBUGP(" capability = 0x%x\n", id[ATA_ID_CAPABILITY] >> 8);
  938. US_DEBUGP(" tPIO = 0x%x\n", id[ATA_ID_OLD_PIO_MODES] >> 8);
  939. US_DEBUGP(" tDMA = 0x%x\n", id[ATA_ID_OLD_DMA_MODES] >> 8);
  940. US_DEBUGP(" field_valid = 0x%x\n", id[ATA_ID_FIELD_VALID]);
  941. US_DEBUGP(" cur_cyls = 0x%x\n", id[ATA_ID_CUR_CYLS]);
  942. US_DEBUGP(" cur_heads = 0x%x\n", id[ATA_ID_CUR_HEADS]);
  943. US_DEBUGP(" cur_sectors = 0x%x\n", id[ATA_ID_CUR_SECTORS]);
  944. US_DEBUGP(" cur_capacity = 0x%x\n", ata_id_u32(id, 57));
  945. US_DEBUGP(" multsect = 0x%x\n", id[ATA_ID_MULTSECT] & 0xff);
  946. US_DEBUGP(" lba_capacity = 0x%x\n", ata_id_u32(id, ATA_ID_LBA_CAPACITY));
  947. US_DEBUGP(" command_set_1 = 0x%x\n", id[ATA_ID_COMMAND_SET_1]);
  948. US_DEBUGP(" command_set_2 = 0x%x\n", id[ATA_ID_COMMAND_SET_2]);
  949. }
  950. /**************************************************************************
  951. * isd200_get_inquiry_data
  952. *
  953. * Get inquiry data
  954. *
  955. * RETURNS:
  956. * ISD status code
  957. */
  958. static int isd200_get_inquiry_data( struct us_data *us )
  959. {
  960. struct isd200_info *info = (struct isd200_info *)us->extra;
  961. int retStatus = ISD200_GOOD;
  962. u16 *id = info->id;
  963. US_DEBUGP("Entering isd200_get_inquiry_data\n");
  964. /* set default to Master */
  965. info->DeviceHead = ATA_ADDRESS_DEVHEAD_STD;
  966. /* attempt to manually enumerate this device */
  967. retStatus = isd200_manual_enum(us);
  968. if (retStatus == ISD200_GOOD) {
  969. int transferStatus;
  970. /* check for an ATA device */
  971. if (info->DeviceFlags & DF_ATA_DEVICE) {
  972. /* this must be an ATA device */
  973. /* perform an ATA Command Identify */
  974. transferStatus = isd200_action( us, ACTION_IDENTIFY,
  975. id, ATA_ID_WORDS * 2);
  976. if (transferStatus != ISD200_TRANSPORT_GOOD) {
  977. /* Error issuing ATA Command Identify */
  978. US_DEBUGP(" Error issuing ATA Command Identify\n");
  979. retStatus = ISD200_ERROR;
  980. } else {
  981. /* ATA Command Identify successful */
  982. int i;
  983. __be16 *src;
  984. __u16 *dest;
  985. isd200_fix_driveid(id);
  986. isd200_dump_driveid(id);
  987. memset(&info->InquiryData, 0, sizeof(info->InquiryData));
  988. /* Standard IDE interface only supports disks */
  989. info->InquiryData.DeviceType = DIRECT_ACCESS_DEVICE;
  990. /* The length must be at least 36 (5 + 31) */
  991. info->InquiryData.AdditionalLength = 0x1F;
  992. if (id[ATA_ID_COMMAND_SET_1] & COMMANDSET_MEDIA_STATUS) {
  993. /* set the removable bit */
  994. info->InquiryData.DeviceTypeModifier = DEVICE_REMOVABLE;
  995. info->DeviceFlags |= DF_REMOVABLE_MEDIA;
  996. }
  997. /* Fill in vendor identification fields */
  998. src = (__be16 *)&id[ATA_ID_PROD];
  999. dest = (__u16*)info->InquiryData.VendorId;
  1000. for (i=0;i<4;i++)
  1001. dest[i] = be16_to_cpu(src[i]);
  1002. src = (__be16 *)&id[ATA_ID_PROD + 8/2];
  1003. dest = (__u16*)info->InquiryData.ProductId;
  1004. for (i=0;i<8;i++)
  1005. dest[i] = be16_to_cpu(src[i]);
  1006. src = (__be16 *)&id[ATA_ID_FW_REV];
  1007. dest = (__u16*)info->InquiryData.ProductRevisionLevel;
  1008. for (i=0;i<2;i++)
  1009. dest[i] = be16_to_cpu(src[i]);
  1010. /* determine if it supports Media Status Notification */
  1011. if (id[ATA_ID_COMMAND_SET_2] & COMMANDSET_MEDIA_STATUS) {
  1012. US_DEBUGP(" Device supports Media Status Notification\n");
  1013. /* Indicate that it is enabled, even though it is not
  1014. * This allows the lock/unlock of the media to work
  1015. * correctly.
  1016. */
  1017. info->DeviceFlags |= DF_MEDIA_STATUS_ENABLED;
  1018. }
  1019. else
  1020. info->DeviceFlags &= ~DF_MEDIA_STATUS_ENABLED;
  1021. }
  1022. } else {
  1023. /*
  1024. * this must be an ATAPI device
  1025. * use an ATAPI protocol (Transparent SCSI)
  1026. */
  1027. us->protocol_name = "Transparent SCSI";
  1028. us->proto_handler = usb_stor_transparent_scsi_command;
  1029. US_DEBUGP("Protocol changed to: %s\n", 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. US_DEBUGP("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. US_DEBUGP(" 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. US_DEBUGP(" 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. US_DEBUGP(" 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. US_DEBUGP(" 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. US_DEBUGP(" 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. US_DEBUGP(" 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. US_DEBUGP(" 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. US_DEBUGP(" 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. US_DEBUGP(" ATA OUT - SCSIOP_MEDIUM_REMOVAL\n");
  1183. if (info->DeviceFlags & DF_REMOVABLE_MEDIA) {
  1184. US_DEBUGP(" srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
  1185. ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  1186. ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  1187. ataCdb->generic.TransferBlockSize = 1;
  1188. ataCdb->generic.RegisterSelect = REG_COMMAND;
  1189. ataCdb->write.CommandByte = (srb->cmnd[4] & 0x1) ?
  1190. ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK;
  1191. isd200_srb_set_bufflen(srb, 0);
  1192. } else {
  1193. US_DEBUGP(" Not removeable media, just report okay\n");
  1194. srb->result = SAM_STAT_GOOD;
  1195. sendToTransport = 0;
  1196. }
  1197. break;
  1198. case START_STOP:
  1199. US_DEBUGP(" ATA OUT - SCSIOP_START_STOP_UNIT\n");
  1200. US_DEBUGP(" srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
  1201. if ((srb->cmnd[4] & 0x3) == 0x2) {
  1202. US_DEBUGP(" Media Eject\n");
  1203. ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  1204. ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  1205. ataCdb->generic.TransferBlockSize = 0;
  1206. ataCdb->generic.RegisterSelect = REG_COMMAND;
  1207. ataCdb->write.CommandByte = ATA_COMMAND_MEDIA_EJECT;
  1208. } else if ((srb->cmnd[4] & 0x3) == 0x1) {
  1209. US_DEBUGP(" Get Media Status\n");
  1210. ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  1211. ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  1212. ataCdb->generic.TransferBlockSize = 1;
  1213. ataCdb->generic.RegisterSelect = REG_COMMAND;
  1214. ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
  1215. isd200_srb_set_bufflen(srb, 0);
  1216. } else {
  1217. US_DEBUGP(" Nothing to do, just report okay\n");
  1218. srb->result = SAM_STAT_GOOD;
  1219. sendToTransport = 0;
  1220. }
  1221. break;
  1222. default:
  1223. US_DEBUGP("Unsupported SCSI command - 0x%X\n", srb->cmnd[0]);
  1224. srb->result = DID_ERROR << 16;
  1225. sendToTransport = 0;
  1226. break;
  1227. }
  1228. return(sendToTransport);
  1229. }
  1230. /**************************************************************************
  1231. * isd200_free_info
  1232. *
  1233. * Frees the driver structure.
  1234. */
  1235. static void isd200_free_info_ptrs(void *info_)
  1236. {
  1237. struct isd200_info *info = (struct isd200_info *) info_;
  1238. if (info) {
  1239. kfree(info->id);
  1240. kfree(info->RegsBuf);
  1241. kfree(info->srb.sense_buffer);
  1242. }
  1243. }
  1244. /**************************************************************************
  1245. * isd200_init_info
  1246. *
  1247. * Allocates (if necessary) and initializes the driver structure.
  1248. *
  1249. * RETURNS:
  1250. * ISD status code
  1251. */
  1252. static int isd200_init_info(struct us_data *us)
  1253. {
  1254. int retStatus = ISD200_GOOD;
  1255. struct isd200_info *info;
  1256. info = (struct isd200_info *)
  1257. kzalloc(sizeof(struct isd200_info), GFP_KERNEL);
  1258. if (!info)
  1259. retStatus = ISD200_ERROR;
  1260. else {
  1261. info->id = kzalloc(ATA_ID_WORDS * 2, GFP_KERNEL);
  1262. info->RegsBuf = (unsigned char *)
  1263. 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. } else
  1276. US_DEBUGP("ERROR - kmalloc failure\n");
  1277. return retStatus;
  1278. }
  1279. /**************************************************************************
  1280. * Initialization for the ISD200
  1281. */
  1282. static int isd200_Initialization(struct us_data *us)
  1283. {
  1284. US_DEBUGP("ISD200 Initialization...\n");
  1285. /* Initialize ISD200 info struct */
  1286. if (isd200_init_info(us) == ISD200_ERROR) {
  1287. US_DEBUGP("ERROR Initializing ISD200 Info struct\n");
  1288. } else {
  1289. /* Get device specific data */
  1290. if (isd200_get_inquiry_data(us) != ISD200_GOOD)
  1291. US_DEBUGP("ISD200 Initialization Failure\n");
  1292. else
  1293. US_DEBUGP("ISD200 Initialization complete\n");
  1294. }
  1295. return 0;
  1296. }
  1297. /**************************************************************************
  1298. * Protocol and Transport for the ISD200 ASIC
  1299. *
  1300. * This protocol and transport are for ATA devices connected to an ISD200
  1301. * ASIC. An ATAPI device that is conected as a slave device will be
  1302. * detected in the driver initialization function and the protocol will
  1303. * be changed to an ATAPI protocol (Transparent SCSI).
  1304. *
  1305. */
  1306. static void isd200_ata_command(struct scsi_cmnd *srb, struct us_data *us)
  1307. {
  1308. int sendToTransport = 1, orig_bufflen;
  1309. union ata_cdb ataCdb;
  1310. /* Make sure driver was initialized */
  1311. if (us->extra == NULL)
  1312. US_DEBUGP("ERROR Driver not initialized\n");
  1313. scsi_set_resid(srb, 0);
  1314. /* scsi_bufflen might change in protocol translation to ata */
  1315. orig_bufflen = scsi_bufflen(srb);
  1316. sendToTransport = isd200_scsi_to_ata(srb, us, &ataCdb);
  1317. /* send the command to the transport layer */
  1318. if (sendToTransport)
  1319. isd200_invoke_transport(us, srb, &ataCdb);
  1320. isd200_srb_set_bufflen(srb, orig_bufflen);
  1321. }
  1322. static int isd200_probe(struct usb_interface *intf,
  1323. const struct usb_device_id *id)
  1324. {
  1325. struct us_data *us;
  1326. int result;
  1327. result = usb_stor_probe1(&us, intf, id,
  1328. (id - isd200_usb_ids) + isd200_unusual_dev_list);
  1329. if (result)
  1330. return result;
  1331. us->protocol_name = "ISD200 ATA/ATAPI";
  1332. us->proto_handler = isd200_ata_command;
  1333. result = usb_stor_probe2(us);
  1334. return result;
  1335. }
  1336. static struct usb_driver isd200_driver = {
  1337. .name = "ums-isd200",
  1338. .probe = isd200_probe,
  1339. .disconnect = usb_stor_disconnect,
  1340. .suspend = usb_stor_suspend,
  1341. .resume = usb_stor_resume,
  1342. .reset_resume = usb_stor_reset_resume,
  1343. .pre_reset = usb_stor_pre_reset,
  1344. .post_reset = usb_stor_post_reset,
  1345. .id_table = isd200_usb_ids,
  1346. .soft_unbind = 1,
  1347. };
  1348. static int __init isd200_init(void)
  1349. {
  1350. return usb_register(&isd200_driver);
  1351. }
  1352. static void __exit isd200_exit(void)
  1353. {
  1354. usb_deregister(&isd200_driver);
  1355. }
  1356. module_init(isd200_init);
  1357. module_exit(isd200_exit);