isd200.c 45 KB

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