isd200.c 45 KB

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