isd200.c 41 KB

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