ppa.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. /* ppa.c -- low level driver for the IOMEGA PPA3
  2. * parallel port SCSI host adapter.
  3. *
  4. * (The PPA3 is the embedded controller in the ZIP drive.)
  5. *
  6. * (c) 1995,1996 Grant R. Guenther, grant@torque.net,
  7. * under the terms of the GNU General Public License.
  8. *
  9. * Current Maintainer: David Campbell (Perth, Western Australia, GMT+0800)
  10. * campbell@torque.net
  11. */
  12. #include <linux/config.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/blkdev.h>
  17. #include <linux/parport.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/delay.h>
  20. #include <asm/io.h>
  21. #include <scsi/scsi.h>
  22. #include <scsi/scsi_cmnd.h>
  23. #include <scsi/scsi_device.h>
  24. #include <scsi/scsi_host.h>
  25. static void ppa_reset_pulse(unsigned int base);
  26. typedef struct {
  27. struct pardevice *dev; /* Parport device entry */
  28. int base; /* Actual port address */
  29. int mode; /* Transfer mode */
  30. struct scsi_cmnd *cur_cmd; /* Current queued command */
  31. struct work_struct ppa_tq; /* Polling interrupt stuff */
  32. unsigned long jstart; /* Jiffies at start */
  33. unsigned long recon_tmo; /* How many usecs to wait for reconnection (6th bit) */
  34. unsigned int failed:1; /* Failure flag */
  35. unsigned wanted:1; /* Parport sharing busy flag */
  36. wait_queue_head_t *waiting;
  37. struct Scsi_Host *host;
  38. struct list_head list;
  39. } ppa_struct;
  40. #include "ppa.h"
  41. static inline ppa_struct *ppa_dev(struct Scsi_Host *host)
  42. {
  43. return *(ppa_struct **)&host->hostdata;
  44. }
  45. static DEFINE_SPINLOCK(arbitration_lock);
  46. static void got_it(ppa_struct *dev)
  47. {
  48. dev->base = dev->dev->port->base;
  49. if (dev->cur_cmd)
  50. dev->cur_cmd->SCp.phase = 1;
  51. else
  52. wake_up(dev->waiting);
  53. }
  54. static void ppa_wakeup(void *ref)
  55. {
  56. ppa_struct *dev = (ppa_struct *) ref;
  57. unsigned long flags;
  58. spin_lock_irqsave(&arbitration_lock, flags);
  59. if (dev->wanted) {
  60. parport_claim(dev->dev);
  61. got_it(dev);
  62. dev->wanted = 0;
  63. }
  64. spin_unlock_irqrestore(&arbitration_lock, flags);
  65. return;
  66. }
  67. static int ppa_pb_claim(ppa_struct *dev)
  68. {
  69. unsigned long flags;
  70. int res = 1;
  71. spin_lock_irqsave(&arbitration_lock, flags);
  72. if (parport_claim(dev->dev) == 0) {
  73. got_it(dev);
  74. res = 0;
  75. }
  76. dev->wanted = res;
  77. spin_unlock_irqrestore(&arbitration_lock, flags);
  78. return res;
  79. }
  80. static void ppa_pb_dismiss(ppa_struct *dev)
  81. {
  82. unsigned long flags;
  83. int wanted;
  84. spin_lock_irqsave(&arbitration_lock, flags);
  85. wanted = dev->wanted;
  86. dev->wanted = 0;
  87. spin_unlock_irqrestore(&arbitration_lock, flags);
  88. if (!wanted)
  89. parport_release(dev->dev);
  90. }
  91. static inline void ppa_pb_release(ppa_struct *dev)
  92. {
  93. parport_release(dev->dev);
  94. }
  95. /*
  96. * Start of Chipset kludges
  97. */
  98. /* This is to give the ppa driver a way to modify the timings (and other
  99. * parameters) by writing to the /proc/scsi/ppa/0 file.
  100. * Very simple method really... (To simple, no error checking :( )
  101. * Reason: Kernel hackers HATE having to unload and reload modules for
  102. * testing...
  103. * Also gives a method to use a script to obtain optimum timings (TODO)
  104. */
  105. static inline int ppa_proc_write(ppa_struct *dev, char *buffer, int length)
  106. {
  107. unsigned long x;
  108. if ((length > 5) && (strncmp(buffer, "mode=", 5) == 0)) {
  109. x = simple_strtoul(buffer + 5, NULL, 0);
  110. dev->mode = x;
  111. return length;
  112. }
  113. if ((length > 10) && (strncmp(buffer, "recon_tmo=", 10) == 0)) {
  114. x = simple_strtoul(buffer + 10, NULL, 0);
  115. dev->recon_tmo = x;
  116. printk("ppa: recon_tmo set to %ld\n", x);
  117. return length;
  118. }
  119. printk("ppa /proc: invalid variable\n");
  120. return (-EINVAL);
  121. }
  122. static int ppa_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset, int length, int inout)
  123. {
  124. int len = 0;
  125. ppa_struct *dev = ppa_dev(host);
  126. if (inout)
  127. return ppa_proc_write(dev, buffer, length);
  128. len += sprintf(buffer + len, "Version : %s\n", PPA_VERSION);
  129. len +=
  130. sprintf(buffer + len, "Parport : %s\n",
  131. dev->dev->port->name);
  132. len +=
  133. sprintf(buffer + len, "Mode : %s\n",
  134. PPA_MODE_STRING[dev->mode]);
  135. #if PPA_DEBUG > 0
  136. len +=
  137. sprintf(buffer + len, "recon_tmo : %lu\n", dev->recon_tmo);
  138. #endif
  139. /* Request for beyond end of buffer */
  140. if (offset > length)
  141. return 0;
  142. *start = buffer + offset;
  143. len -= offset;
  144. if (len > length)
  145. len = length;
  146. return len;
  147. }
  148. static int device_check(ppa_struct *dev);
  149. #if PPA_DEBUG > 0
  150. #define ppa_fail(x,y) printk("ppa: ppa_fail(%i) from %s at line %d\n",\
  151. y, __FUNCTION__, __LINE__); ppa_fail_func(x,y);
  152. static inline void ppa_fail_func(ppa_struct *dev, int error_code)
  153. #else
  154. static inline void ppa_fail(ppa_struct *dev, int error_code)
  155. #endif
  156. {
  157. /* If we fail a device then we trash status / message bytes */
  158. if (dev->cur_cmd) {
  159. dev->cur_cmd->result = error_code << 16;
  160. dev->failed = 1;
  161. }
  162. }
  163. /*
  164. * Wait for the high bit to be set.
  165. *
  166. * In principle, this could be tied to an interrupt, but the adapter
  167. * doesn't appear to be designed to support interrupts. We spin on
  168. * the 0x80 ready bit.
  169. */
  170. static unsigned char ppa_wait(ppa_struct *dev)
  171. {
  172. int k;
  173. unsigned short ppb = dev->base;
  174. unsigned char r;
  175. k = PPA_SPIN_TMO;
  176. /* Wait for bit 6 and 7 - PJC */
  177. for (r = r_str(ppb); ((r & 0xc0) != 0xc0) && (k); k--) {
  178. udelay(1);
  179. r = r_str(ppb);
  180. }
  181. /*
  182. * return some status information.
  183. * Semantics: 0xc0 = ZIP wants more data
  184. * 0xd0 = ZIP wants to send more data
  185. * 0xe0 = ZIP is expecting SCSI command data
  186. * 0xf0 = end of transfer, ZIP is sending status
  187. */
  188. if (k)
  189. return (r & 0xf0);
  190. /* Counter expired - Time out occurred */
  191. ppa_fail(dev, DID_TIME_OUT);
  192. printk("ppa timeout in ppa_wait\n");
  193. return 0; /* command timed out */
  194. }
  195. /*
  196. * Clear EPP Timeout Bit
  197. */
  198. static inline void epp_reset(unsigned short ppb)
  199. {
  200. int i;
  201. i = r_str(ppb);
  202. w_str(ppb, i);
  203. w_str(ppb, i & 0xfe);
  204. }
  205. /*
  206. * Wait for empty ECP fifo (if we are in ECP fifo mode only)
  207. */
  208. static inline void ecp_sync(ppa_struct *dev)
  209. {
  210. int i, ppb_hi = dev->dev->port->base_hi;
  211. if (ppb_hi == 0)
  212. return;
  213. if ((r_ecr(ppb_hi) & 0xe0) == 0x60) { /* mode 011 == ECP fifo mode */
  214. for (i = 0; i < 100; i++) {
  215. if (r_ecr(ppb_hi) & 0x01)
  216. return;
  217. udelay(5);
  218. }
  219. printk("ppa: ECP sync failed as data still present in FIFO.\n");
  220. }
  221. }
  222. static int ppa_byte_out(unsigned short base, const char *buffer, int len)
  223. {
  224. int i;
  225. for (i = len; i; i--) {
  226. w_dtr(base, *buffer++);
  227. w_ctr(base, 0xe);
  228. w_ctr(base, 0xc);
  229. }
  230. return 1; /* All went well - we hope! */
  231. }
  232. static int ppa_byte_in(unsigned short base, char *buffer, int len)
  233. {
  234. int i;
  235. for (i = len; i; i--) {
  236. *buffer++ = r_dtr(base);
  237. w_ctr(base, 0x27);
  238. w_ctr(base, 0x25);
  239. }
  240. return 1; /* All went well - we hope! */
  241. }
  242. static int ppa_nibble_in(unsigned short base, char *buffer, int len)
  243. {
  244. for (; len; len--) {
  245. unsigned char h;
  246. w_ctr(base, 0x4);
  247. h = r_str(base) & 0xf0;
  248. w_ctr(base, 0x6);
  249. *buffer++ = h | ((r_str(base) & 0xf0) >> 4);
  250. }
  251. return 1; /* All went well - we hope! */
  252. }
  253. static int ppa_out(ppa_struct *dev, char *buffer, int len)
  254. {
  255. int r;
  256. unsigned short ppb = dev->base;
  257. r = ppa_wait(dev);
  258. if ((r & 0x50) != 0x40) {
  259. ppa_fail(dev, DID_ERROR);
  260. return 0;
  261. }
  262. switch (dev->mode) {
  263. case PPA_NIBBLE:
  264. case PPA_PS2:
  265. /* 8 bit output, with a loop */
  266. r = ppa_byte_out(ppb, buffer, len);
  267. break;
  268. case PPA_EPP_32:
  269. case PPA_EPP_16:
  270. case PPA_EPP_8:
  271. epp_reset(ppb);
  272. w_ctr(ppb, 0x4);
  273. #ifdef CONFIG_SCSI_IZIP_EPP16
  274. if (!(((long) buffer | len) & 0x01))
  275. outsw(ppb + 4, buffer, len >> 1);
  276. #else
  277. if (!(((long) buffer | len) & 0x03))
  278. outsl(ppb + 4, buffer, len >> 2);
  279. #endif
  280. else
  281. outsb(ppb + 4, buffer, len);
  282. w_ctr(ppb, 0xc);
  283. r = !(r_str(ppb) & 0x01);
  284. w_ctr(ppb, 0xc);
  285. ecp_sync(dev);
  286. break;
  287. default:
  288. printk("PPA: bug in ppa_out()\n");
  289. r = 0;
  290. }
  291. return r;
  292. }
  293. static int ppa_in(ppa_struct *dev, char *buffer, int len)
  294. {
  295. int r;
  296. unsigned short ppb = dev->base;
  297. r = ppa_wait(dev);
  298. if ((r & 0x50) != 0x50) {
  299. ppa_fail(dev, DID_ERROR);
  300. return 0;
  301. }
  302. switch (dev->mode) {
  303. case PPA_NIBBLE:
  304. /* 4 bit input, with a loop */
  305. r = ppa_nibble_in(ppb, buffer, len);
  306. w_ctr(ppb, 0xc);
  307. break;
  308. case PPA_PS2:
  309. /* 8 bit input, with a loop */
  310. w_ctr(ppb, 0x25);
  311. r = ppa_byte_in(ppb, buffer, len);
  312. w_ctr(ppb, 0x4);
  313. w_ctr(ppb, 0xc);
  314. break;
  315. case PPA_EPP_32:
  316. case PPA_EPP_16:
  317. case PPA_EPP_8:
  318. epp_reset(ppb);
  319. w_ctr(ppb, 0x24);
  320. #ifdef CONFIG_SCSI_IZIP_EPP16
  321. if (!(((long) buffer | len) & 0x01))
  322. insw(ppb + 4, buffer, len >> 1);
  323. #else
  324. if (!(((long) buffer | len) & 0x03))
  325. insl(ppb + 4, buffer, len >> 2);
  326. #endif
  327. else
  328. insb(ppb + 4, buffer, len);
  329. w_ctr(ppb, 0x2c);
  330. r = !(r_str(ppb) & 0x01);
  331. w_ctr(ppb, 0x2c);
  332. ecp_sync(dev);
  333. break;
  334. default:
  335. printk("PPA: bug in ppa_ins()\n");
  336. r = 0;
  337. break;
  338. }
  339. return r;
  340. }
  341. /* end of ppa_io.h */
  342. static inline void ppa_d_pulse(unsigned short ppb, unsigned char b)
  343. {
  344. w_dtr(ppb, b);
  345. w_ctr(ppb, 0xc);
  346. w_ctr(ppb, 0xe);
  347. w_ctr(ppb, 0xc);
  348. w_ctr(ppb, 0x4);
  349. w_ctr(ppb, 0xc);
  350. }
  351. static void ppa_disconnect(ppa_struct *dev)
  352. {
  353. unsigned short ppb = dev->base;
  354. ppa_d_pulse(ppb, 0);
  355. ppa_d_pulse(ppb, 0x3c);
  356. ppa_d_pulse(ppb, 0x20);
  357. ppa_d_pulse(ppb, 0xf);
  358. }
  359. static inline void ppa_c_pulse(unsigned short ppb, unsigned char b)
  360. {
  361. w_dtr(ppb, b);
  362. w_ctr(ppb, 0x4);
  363. w_ctr(ppb, 0x6);
  364. w_ctr(ppb, 0x4);
  365. w_ctr(ppb, 0xc);
  366. }
  367. static inline void ppa_connect(ppa_struct *dev, int flag)
  368. {
  369. unsigned short ppb = dev->base;
  370. ppa_c_pulse(ppb, 0);
  371. ppa_c_pulse(ppb, 0x3c);
  372. ppa_c_pulse(ppb, 0x20);
  373. if ((flag == CONNECT_EPP_MAYBE) && IN_EPP_MODE(dev->mode))
  374. ppa_c_pulse(ppb, 0xcf);
  375. else
  376. ppa_c_pulse(ppb, 0x8f);
  377. }
  378. static int ppa_select(ppa_struct *dev, int target)
  379. {
  380. int k;
  381. unsigned short ppb = dev->base;
  382. /*
  383. * Bit 6 (0x40) is the device selected bit.
  384. * First we must wait till the current device goes off line...
  385. */
  386. k = PPA_SELECT_TMO;
  387. do {
  388. k--;
  389. udelay(1);
  390. } while ((r_str(ppb) & 0x40) && (k));
  391. if (!k)
  392. return 0;
  393. w_dtr(ppb, (1 << target));
  394. w_ctr(ppb, 0xe);
  395. w_ctr(ppb, 0xc);
  396. w_dtr(ppb, 0x80); /* This is NOT the initator */
  397. w_ctr(ppb, 0x8);
  398. k = PPA_SELECT_TMO;
  399. do {
  400. k--;
  401. udelay(1);
  402. }
  403. while (!(r_str(ppb) & 0x40) && (k));
  404. if (!k)
  405. return 0;
  406. return 1;
  407. }
  408. /*
  409. * This is based on a trace of what the Iomega DOS 'guest' driver does.
  410. * I've tried several different kinds of parallel ports with guest and
  411. * coded this to react in the same ways that it does.
  412. *
  413. * The return value from this function is just a hint about where the
  414. * handshaking failed.
  415. *
  416. */
  417. static int ppa_init(ppa_struct *dev)
  418. {
  419. int retv;
  420. unsigned short ppb = dev->base;
  421. ppa_disconnect(dev);
  422. ppa_connect(dev, CONNECT_NORMAL);
  423. retv = 2; /* Failed */
  424. w_ctr(ppb, 0xe);
  425. if ((r_str(ppb) & 0x08) == 0x08)
  426. retv--;
  427. w_ctr(ppb, 0xc);
  428. if ((r_str(ppb) & 0x08) == 0x00)
  429. retv--;
  430. if (!retv)
  431. ppa_reset_pulse(ppb);
  432. udelay(1000); /* Allow devices to settle down */
  433. ppa_disconnect(dev);
  434. udelay(1000); /* Another delay to allow devices to settle */
  435. if (retv)
  436. return -EIO;
  437. return device_check(dev);
  438. }
  439. static inline int ppa_send_command(struct scsi_cmnd *cmd)
  440. {
  441. ppa_struct *dev = ppa_dev(cmd->device->host);
  442. int k;
  443. w_ctr(dev->base, 0x0c);
  444. for (k = 0; k < cmd->cmd_len; k++)
  445. if (!ppa_out(dev, &cmd->cmnd[k], 1))
  446. return 0;
  447. return 1;
  448. }
  449. /*
  450. * The bulk flag enables some optimisations in the data transfer loops,
  451. * it should be true for any command that transfers data in integral
  452. * numbers of sectors.
  453. *
  454. * The driver appears to remain stable if we speed up the parallel port
  455. * i/o in this function, but not elsewhere.
  456. */
  457. static int ppa_completion(struct scsi_cmnd *cmd)
  458. {
  459. /* Return codes:
  460. * -1 Error
  461. * 0 Told to schedule
  462. * 1 Finished data transfer
  463. */
  464. ppa_struct *dev = ppa_dev(cmd->device->host);
  465. unsigned short ppb = dev->base;
  466. unsigned long start_jiffies = jiffies;
  467. unsigned char r, v;
  468. int fast, bulk, status;
  469. v = cmd->cmnd[0];
  470. bulk = ((v == READ_6) ||
  471. (v == READ_10) || (v == WRITE_6) || (v == WRITE_10));
  472. /*
  473. * We only get here if the drive is ready to comunicate,
  474. * hence no need for a full ppa_wait.
  475. */
  476. r = (r_str(ppb) & 0xf0);
  477. while (r != (unsigned char) 0xf0) {
  478. /*
  479. * If we have been running for more than a full timer tick
  480. * then take a rest.
  481. */
  482. if (time_after(jiffies, start_jiffies + 1))
  483. return 0;
  484. if ((cmd->SCp.this_residual <= 0)) {
  485. ppa_fail(dev, DID_ERROR);
  486. return -1; /* ERROR_RETURN */
  487. }
  488. /* On some hardware we have SCSI disconnected (6th bit low)
  489. * for about 100usecs. It is too expensive to wait a
  490. * tick on every loop so we busy wait for no more than
  491. * 500usecs to give the drive a chance first. We do not
  492. * change things for "normal" hardware since generally
  493. * the 6th bit is always high.
  494. * This makes the CPU load higher on some hardware
  495. * but otherwise we can not get more than 50K/secs
  496. * on this problem hardware.
  497. */
  498. if ((r & 0xc0) != 0xc0) {
  499. /* Wait for reconnection should be no more than
  500. * jiffy/2 = 5ms = 5000 loops
  501. */
  502. unsigned long k = dev->recon_tmo;
  503. for (; k && ((r = (r_str(ppb) & 0xf0)) & 0xc0) != 0xc0;
  504. k--)
  505. udelay(1);
  506. if (!k)
  507. return 0;
  508. }
  509. /* determine if we should use burst I/O */
  510. fast = (bulk && (cmd->SCp.this_residual >= PPA_BURST_SIZE))
  511. ? PPA_BURST_SIZE : 1;
  512. if (r == (unsigned char) 0xc0)
  513. status = ppa_out(dev, cmd->SCp.ptr, fast);
  514. else
  515. status = ppa_in(dev, cmd->SCp.ptr, fast);
  516. cmd->SCp.ptr += fast;
  517. cmd->SCp.this_residual -= fast;
  518. if (!status) {
  519. ppa_fail(dev, DID_BUS_BUSY);
  520. return -1; /* ERROR_RETURN */
  521. }
  522. if (cmd->SCp.buffer && !cmd->SCp.this_residual) {
  523. /* if scatter/gather, advance to the next segment */
  524. if (cmd->SCp.buffers_residual--) {
  525. cmd->SCp.buffer++;
  526. cmd->SCp.this_residual =
  527. cmd->SCp.buffer->length;
  528. cmd->SCp.ptr =
  529. page_address(cmd->SCp.buffer->page) +
  530. cmd->SCp.buffer->offset;
  531. }
  532. }
  533. /* Now check to see if the drive is ready to comunicate */
  534. r = (r_str(ppb) & 0xf0);
  535. /* If not, drop back down to the scheduler and wait a timer tick */
  536. if (!(r & 0x80))
  537. return 0;
  538. }
  539. return 1; /* FINISH_RETURN */
  540. }
  541. /*
  542. * Since the PPA itself doesn't generate interrupts, we use
  543. * the scheduler's task queue to generate a stream of call-backs and
  544. * complete the request when the drive is ready.
  545. */
  546. static void ppa_interrupt(void *data)
  547. {
  548. ppa_struct *dev = (ppa_struct *) data;
  549. struct scsi_cmnd *cmd = dev->cur_cmd;
  550. if (!cmd) {
  551. printk("PPA: bug in ppa_interrupt\n");
  552. return;
  553. }
  554. if (ppa_engine(dev, cmd)) {
  555. dev->ppa_tq.data = (void *) dev;
  556. schedule_delayed_work(&dev->ppa_tq, 1);
  557. return;
  558. }
  559. /* Command must of completed hence it is safe to let go... */
  560. #if PPA_DEBUG > 0
  561. switch ((cmd->result >> 16) & 0xff) {
  562. case DID_OK:
  563. break;
  564. case DID_NO_CONNECT:
  565. printk("ppa: no device at SCSI ID %i\n", cmd->device->target);
  566. break;
  567. case DID_BUS_BUSY:
  568. printk("ppa: BUS BUSY - EPP timeout detected\n");
  569. break;
  570. case DID_TIME_OUT:
  571. printk("ppa: unknown timeout\n");
  572. break;
  573. case DID_ABORT:
  574. printk("ppa: told to abort\n");
  575. break;
  576. case DID_PARITY:
  577. printk("ppa: parity error (???)\n");
  578. break;
  579. case DID_ERROR:
  580. printk("ppa: internal driver error\n");
  581. break;
  582. case DID_RESET:
  583. printk("ppa: told to reset device\n");
  584. break;
  585. case DID_BAD_INTR:
  586. printk("ppa: bad interrupt (???)\n");
  587. break;
  588. default:
  589. printk("ppa: bad return code (%02x)\n",
  590. (cmd->result >> 16) & 0xff);
  591. }
  592. #endif
  593. if (cmd->SCp.phase > 1)
  594. ppa_disconnect(dev);
  595. ppa_pb_dismiss(dev);
  596. dev->cur_cmd = NULL;
  597. cmd->scsi_done(cmd);
  598. }
  599. static int ppa_engine(ppa_struct *dev, struct scsi_cmnd *cmd)
  600. {
  601. unsigned short ppb = dev->base;
  602. unsigned char l = 0, h = 0;
  603. int retv;
  604. /* First check for any errors that may of occurred
  605. * Here we check for internal errors
  606. */
  607. if (dev->failed)
  608. return 0;
  609. switch (cmd->SCp.phase) {
  610. case 0: /* Phase 0 - Waiting for parport */
  611. if (time_after(jiffies, dev->jstart + HZ)) {
  612. /*
  613. * We waited more than a second
  614. * for parport to call us
  615. */
  616. ppa_fail(dev, DID_BUS_BUSY);
  617. return 0;
  618. }
  619. return 1; /* wait until ppa_wakeup claims parport */
  620. case 1: /* Phase 1 - Connected */
  621. { /* Perform a sanity check for cable unplugged */
  622. int retv = 2; /* Failed */
  623. ppa_connect(dev, CONNECT_EPP_MAYBE);
  624. w_ctr(ppb, 0xe);
  625. if ((r_str(ppb) & 0x08) == 0x08)
  626. retv--;
  627. w_ctr(ppb, 0xc);
  628. if ((r_str(ppb) & 0x08) == 0x00)
  629. retv--;
  630. if (retv) {
  631. if ((jiffies - dev->jstart) > (1 * HZ)) {
  632. printk
  633. ("ppa: Parallel port cable is unplugged!!\n");
  634. ppa_fail(dev, DID_BUS_BUSY);
  635. return 0;
  636. } else {
  637. ppa_disconnect(dev);
  638. return 1; /* Try again in a jiffy */
  639. }
  640. }
  641. cmd->SCp.phase++;
  642. }
  643. case 2: /* Phase 2 - We are now talking to the scsi bus */
  644. if (!ppa_select(dev, cmd->device->id)) {
  645. ppa_fail(dev, DID_NO_CONNECT);
  646. return 0;
  647. }
  648. cmd->SCp.phase++;
  649. case 3: /* Phase 3 - Ready to accept a command */
  650. w_ctr(ppb, 0x0c);
  651. if (!(r_str(ppb) & 0x80))
  652. return 1;
  653. if (!ppa_send_command(cmd))
  654. return 0;
  655. cmd->SCp.phase++;
  656. case 4: /* Phase 4 - Setup scatter/gather buffers */
  657. if (cmd->use_sg) {
  658. /* if many buffers are available, start filling the first */
  659. cmd->SCp.buffer =
  660. (struct scatterlist *) cmd->request_buffer;
  661. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  662. cmd->SCp.ptr =
  663. page_address(cmd->SCp.buffer->page) +
  664. cmd->SCp.buffer->offset;
  665. } else {
  666. /* else fill the only available buffer */
  667. cmd->SCp.buffer = NULL;
  668. cmd->SCp.this_residual = cmd->request_bufflen;
  669. cmd->SCp.ptr = cmd->request_buffer;
  670. }
  671. cmd->SCp.buffers_residual = cmd->use_sg - 1;
  672. cmd->SCp.phase++;
  673. case 5: /* Phase 5 - Data transfer stage */
  674. w_ctr(ppb, 0x0c);
  675. if (!(r_str(ppb) & 0x80))
  676. return 1;
  677. retv = ppa_completion(cmd);
  678. if (retv == -1)
  679. return 0;
  680. if (retv == 0)
  681. return 1;
  682. cmd->SCp.phase++;
  683. case 6: /* Phase 6 - Read status/message */
  684. cmd->result = DID_OK << 16;
  685. /* Check for data overrun */
  686. if (ppa_wait(dev) != (unsigned char) 0xf0) {
  687. ppa_fail(dev, DID_ERROR);
  688. return 0;
  689. }
  690. if (ppa_in(dev, &l, 1)) { /* read status byte */
  691. /* Check for optional message byte */
  692. if (ppa_wait(dev) == (unsigned char) 0xf0)
  693. ppa_in(dev, &h, 1);
  694. cmd->result =
  695. (DID_OK << 16) + (h << 8) + (l & STATUS_MASK);
  696. }
  697. return 0; /* Finished */
  698. break;
  699. default:
  700. printk("ppa: Invalid scsi phase\n");
  701. }
  702. return 0;
  703. }
  704. static int ppa_queuecommand(struct scsi_cmnd *cmd,
  705. void (*done) (struct scsi_cmnd *))
  706. {
  707. ppa_struct *dev = ppa_dev(cmd->device->host);
  708. if (dev->cur_cmd) {
  709. printk("PPA: bug in ppa_queuecommand\n");
  710. return 0;
  711. }
  712. dev->failed = 0;
  713. dev->jstart = jiffies;
  714. dev->cur_cmd = cmd;
  715. cmd->scsi_done = done;
  716. cmd->result = DID_ERROR << 16; /* default return code */
  717. cmd->SCp.phase = 0; /* bus free */
  718. dev->ppa_tq.data = dev;
  719. schedule_work(&dev->ppa_tq);
  720. ppa_pb_claim(dev);
  721. return 0;
  722. }
  723. /*
  724. * Apparently the disk->capacity attribute is off by 1 sector
  725. * for all disk drives. We add the one here, but it should really
  726. * be done in sd.c. Even if it gets fixed there, this will still
  727. * work.
  728. */
  729. static int ppa_biosparam(struct scsi_device *sdev, struct block_device *dev,
  730. sector_t capacity, int ip[])
  731. {
  732. ip[0] = 0x40;
  733. ip[1] = 0x20;
  734. ip[2] = ((unsigned long) capacity + 1) / (ip[0] * ip[1]);
  735. if (ip[2] > 1024) {
  736. ip[0] = 0xff;
  737. ip[1] = 0x3f;
  738. ip[2] = ((unsigned long) capacity + 1) / (ip[0] * ip[1]);
  739. if (ip[2] > 1023)
  740. ip[2] = 1023;
  741. }
  742. return 0;
  743. }
  744. static int ppa_abort(struct scsi_cmnd *cmd)
  745. {
  746. ppa_struct *dev = ppa_dev(cmd->device->host);
  747. /*
  748. * There is no method for aborting commands since Iomega
  749. * have tied the SCSI_MESSAGE line high in the interface
  750. */
  751. switch (cmd->SCp.phase) {
  752. case 0: /* Do not have access to parport */
  753. case 1: /* Have not connected to interface */
  754. dev->cur_cmd = NULL; /* Forget the problem */
  755. return SUCCESS;
  756. break;
  757. default: /* SCSI command sent, can not abort */
  758. return FAILED;
  759. break;
  760. }
  761. }
  762. static void ppa_reset_pulse(unsigned int base)
  763. {
  764. w_dtr(base, 0x40);
  765. w_ctr(base, 0x8);
  766. udelay(30);
  767. w_ctr(base, 0xc);
  768. }
  769. static int ppa_reset(struct scsi_cmnd *cmd)
  770. {
  771. ppa_struct *dev = ppa_dev(cmd->device->host);
  772. if (cmd->SCp.phase)
  773. ppa_disconnect(dev);
  774. dev->cur_cmd = NULL; /* Forget the problem */
  775. ppa_connect(dev, CONNECT_NORMAL);
  776. ppa_reset_pulse(dev->base);
  777. mdelay(1); /* device settle delay */
  778. ppa_disconnect(dev);
  779. mdelay(1); /* device settle delay */
  780. return SUCCESS;
  781. }
  782. static int device_check(ppa_struct *dev)
  783. {
  784. /* This routine looks for a device and then attempts to use EPP
  785. to send a command. If all goes as planned then EPP is available. */
  786. static char cmd[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  787. int loop, old_mode, status, k, ppb = dev->base;
  788. unsigned char l;
  789. old_mode = dev->mode;
  790. for (loop = 0; loop < 8; loop++) {
  791. /* Attempt to use EPP for Test Unit Ready */
  792. if ((ppb & 0x0007) == 0x0000)
  793. dev->mode = PPA_EPP_32;
  794. second_pass:
  795. ppa_connect(dev, CONNECT_EPP_MAYBE);
  796. /* Select SCSI device */
  797. if (!ppa_select(dev, loop)) {
  798. ppa_disconnect(dev);
  799. continue;
  800. }
  801. printk("ppa: Found device at ID %i, Attempting to use %s\n",
  802. loop, PPA_MODE_STRING[dev->mode]);
  803. /* Send SCSI command */
  804. status = 1;
  805. w_ctr(ppb, 0x0c);
  806. for (l = 0; (l < 6) && (status); l++)
  807. status = ppa_out(dev, cmd, 1);
  808. if (!status) {
  809. ppa_disconnect(dev);
  810. ppa_connect(dev, CONNECT_EPP_MAYBE);
  811. w_dtr(ppb, 0x40);
  812. w_ctr(ppb, 0x08);
  813. udelay(30);
  814. w_ctr(ppb, 0x0c);
  815. udelay(1000);
  816. ppa_disconnect(dev);
  817. udelay(1000);
  818. if (dev->mode == PPA_EPP_32) {
  819. dev->mode = old_mode;
  820. goto second_pass;
  821. }
  822. return -EIO;
  823. }
  824. w_ctr(ppb, 0x0c);
  825. k = 1000000; /* 1 Second */
  826. do {
  827. l = r_str(ppb);
  828. k--;
  829. udelay(1);
  830. } while (!(l & 0x80) && (k));
  831. l &= 0xf0;
  832. if (l != 0xf0) {
  833. ppa_disconnect(dev);
  834. ppa_connect(dev, CONNECT_EPP_MAYBE);
  835. ppa_reset_pulse(ppb);
  836. udelay(1000);
  837. ppa_disconnect(dev);
  838. udelay(1000);
  839. if (dev->mode == PPA_EPP_32) {
  840. dev->mode = old_mode;
  841. goto second_pass;
  842. }
  843. return -EIO;
  844. }
  845. ppa_disconnect(dev);
  846. printk("ppa: Communication established with ID %i using %s\n",
  847. loop, PPA_MODE_STRING[dev->mode]);
  848. ppa_connect(dev, CONNECT_EPP_MAYBE);
  849. ppa_reset_pulse(ppb);
  850. udelay(1000);
  851. ppa_disconnect(dev);
  852. udelay(1000);
  853. return 0;
  854. }
  855. return -ENODEV;
  856. }
  857. static struct scsi_host_template ppa_template = {
  858. .module = THIS_MODULE,
  859. .proc_name = "ppa",
  860. .proc_info = ppa_proc_info,
  861. .name = "Iomega VPI0 (ppa) interface",
  862. .queuecommand = ppa_queuecommand,
  863. .eh_abort_handler = ppa_abort,
  864. .eh_bus_reset_handler = ppa_reset,
  865. .eh_host_reset_handler = ppa_reset,
  866. .bios_param = ppa_biosparam,
  867. .this_id = -1,
  868. .sg_tablesize = SG_ALL,
  869. .cmd_per_lun = 1,
  870. .use_clustering = ENABLE_CLUSTERING,
  871. .can_queue = 1,
  872. };
  873. /***************************************************************************
  874. * Parallel port probing routines *
  875. ***************************************************************************/
  876. static LIST_HEAD(ppa_hosts);
  877. static int __ppa_attach(struct parport *pb)
  878. {
  879. struct Scsi_Host *host;
  880. DECLARE_WAIT_QUEUE_HEAD(waiting);
  881. DEFINE_WAIT(wait);
  882. ppa_struct *dev;
  883. int ports;
  884. int modes, ppb, ppb_hi;
  885. int err = -ENOMEM;
  886. dev = kmalloc(sizeof(ppa_struct), GFP_KERNEL);
  887. if (!dev)
  888. return -ENOMEM;
  889. memset(dev, 0, sizeof(ppa_struct));
  890. dev->base = -1;
  891. dev->mode = PPA_AUTODETECT;
  892. dev->recon_tmo = PPA_RECON_TMO;
  893. init_waitqueue_head(&waiting);
  894. dev->dev = parport_register_device(pb, "ppa", NULL, ppa_wakeup,
  895. NULL, 0, dev);
  896. if (!dev->dev)
  897. goto out;
  898. /* Claim the bus so it remembers what we do to the control
  899. * registers. [ CTR and ECP ]
  900. */
  901. err = -EBUSY;
  902. dev->waiting = &waiting;
  903. prepare_to_wait(&waiting, &wait, TASK_UNINTERRUPTIBLE);
  904. if (ppa_pb_claim(dev))
  905. schedule_timeout(3 * HZ);
  906. if (dev->wanted) {
  907. printk(KERN_ERR "ppa%d: failed to claim parport because "
  908. "a pardevice is owning the port for too long "
  909. "time!\n", pb->number);
  910. ppa_pb_dismiss(dev);
  911. dev->waiting = NULL;
  912. finish_wait(&waiting, &wait);
  913. goto out1;
  914. }
  915. dev->waiting = NULL;
  916. finish_wait(&waiting, &wait);
  917. ppb = dev->base = dev->dev->port->base;
  918. ppb_hi = dev->dev->port->base_hi;
  919. w_ctr(ppb, 0x0c);
  920. modes = dev->dev->port->modes;
  921. /* Mode detection works up the chain of speed
  922. * This avoids a nasty if-then-else-if-... tree
  923. */
  924. dev->mode = PPA_NIBBLE;
  925. if (modes & PARPORT_MODE_TRISTATE)
  926. dev->mode = PPA_PS2;
  927. if (modes & PARPORT_MODE_ECP) {
  928. w_ecr(ppb_hi, 0x20);
  929. dev->mode = PPA_PS2;
  930. }
  931. if ((modes & PARPORT_MODE_EPP) && (modes & PARPORT_MODE_ECP))
  932. w_ecr(ppb_hi, 0x80);
  933. /* Done configuration */
  934. err = ppa_init(dev);
  935. ppa_pb_release(dev);
  936. if (err)
  937. goto out1;
  938. /* now the glue ... */
  939. if (dev->mode == PPA_NIBBLE || dev->mode == PPA_PS2)
  940. ports = 3;
  941. else
  942. ports = 8;
  943. INIT_WORK(&dev->ppa_tq, ppa_interrupt, dev);
  944. err = -ENOMEM;
  945. host = scsi_host_alloc(&ppa_template, sizeof(ppa_struct *));
  946. if (!host)
  947. goto out1;
  948. host->io_port = pb->base;
  949. host->n_io_port = ports;
  950. host->dma_channel = -1;
  951. host->unique_id = pb->number;
  952. *(ppa_struct **)&host->hostdata = dev;
  953. dev->host = host;
  954. list_add_tail(&dev->list, &ppa_hosts);
  955. err = scsi_add_host(host, NULL);
  956. if (err)
  957. goto out2;
  958. scsi_scan_host(host);
  959. return 0;
  960. out2:
  961. list_del_init(&dev->list);
  962. scsi_host_put(host);
  963. out1:
  964. parport_unregister_device(dev->dev);
  965. out:
  966. kfree(dev);
  967. return err;
  968. }
  969. static void ppa_attach(struct parport *pb)
  970. {
  971. __ppa_attach(pb);
  972. }
  973. static void ppa_detach(struct parport *pb)
  974. {
  975. ppa_struct *dev;
  976. list_for_each_entry(dev, &ppa_hosts, list) {
  977. if (dev->dev->port == pb) {
  978. list_del_init(&dev->list);
  979. scsi_remove_host(dev->host);
  980. scsi_host_put(dev->host);
  981. parport_unregister_device(dev->dev);
  982. kfree(dev);
  983. break;
  984. }
  985. }
  986. }
  987. static struct parport_driver ppa_driver = {
  988. .name = "ppa",
  989. .attach = ppa_attach,
  990. .detach = ppa_detach,
  991. };
  992. static int __init ppa_driver_init(void)
  993. {
  994. printk("ppa: Version %s\n", PPA_VERSION);
  995. return parport_register_driver(&ppa_driver);
  996. }
  997. static void __exit ppa_driver_exit(void)
  998. {
  999. parport_unregister_driver(&ppa_driver);
  1000. }
  1001. module_init(ppa_driver_init);
  1002. module_exit(ppa_driver_exit);
  1003. MODULE_LICENSE("GPL");