isd200.c 45 KB

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