isd200.c 46 KB

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