isd200.c 45 KB

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