ieee1284_ops.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. /* IEEE-1284 operations for parport.
  2. *
  3. * This file is for generic IEEE 1284 operations. The idea is that
  4. * they are used by the low-level drivers. If they have a special way
  5. * of doing something, they can provide their own routines (and put
  6. * the function pointers in port->ops); if not, they can just use these
  7. * as a fallback.
  8. *
  9. * Note: Make no assumptions about hardware or architecture in this file!
  10. *
  11. * Author: Tim Waugh <tim@cyberelk.demon.co.uk>
  12. * Fixed AUTOFD polarity in ecp_forward_to_reverse(). Fred Barnes, 1999
  13. * Software emulated EPP fixes, Fred Barnes, 04/2001.
  14. */
  15. #include <linux/config.h>
  16. #include <linux/module.h>
  17. #include <linux/parport.h>
  18. #include <linux/delay.h>
  19. #include <linux/sched.h>
  20. #include <asm/uaccess.h>
  21. #undef DEBUG /* undef me for production */
  22. #ifdef CONFIG_LP_CONSOLE
  23. #undef DEBUG /* Don't want a garbled console */
  24. #endif
  25. #ifdef DEBUG
  26. #define DPRINTK(stuff...) printk (stuff)
  27. #else
  28. #define DPRINTK(stuff...)
  29. #endif
  30. /*** *
  31. * One-way data transfer functions. *
  32. * ***/
  33. /* Compatibility mode. */
  34. size_t parport_ieee1284_write_compat (struct parport *port,
  35. const void *buffer, size_t len,
  36. int flags)
  37. {
  38. int no_irq = 1;
  39. ssize_t count = 0;
  40. const unsigned char *addr = buffer;
  41. unsigned char byte;
  42. struct pardevice *dev = port->physport->cad;
  43. unsigned char ctl = (PARPORT_CONTROL_SELECT
  44. | PARPORT_CONTROL_INIT);
  45. if (port->irq != PARPORT_IRQ_NONE) {
  46. parport_enable_irq (port);
  47. no_irq = 0;
  48. }
  49. port->physport->ieee1284.phase = IEEE1284_PH_FWD_DATA;
  50. parport_write_control (port, ctl);
  51. parport_data_forward (port);
  52. while (count < len) {
  53. unsigned long expire = jiffies + dev->timeout;
  54. long wait = (HZ + 99) / 100;
  55. unsigned char mask = (PARPORT_STATUS_ERROR
  56. | PARPORT_STATUS_BUSY);
  57. unsigned char val = (PARPORT_STATUS_ERROR
  58. | PARPORT_STATUS_BUSY);
  59. /* Wait until the peripheral's ready */
  60. do {
  61. /* Is the peripheral ready yet? */
  62. if (!parport_wait_peripheral (port, mask, val))
  63. /* Skip the loop */
  64. goto ready;
  65. /* Is the peripheral upset? */
  66. if ((parport_read_status (port) &
  67. (PARPORT_STATUS_PAPEROUT |
  68. PARPORT_STATUS_SELECT |
  69. PARPORT_STATUS_ERROR))
  70. != (PARPORT_STATUS_SELECT |
  71. PARPORT_STATUS_ERROR))
  72. /* If nFault is asserted (i.e. no
  73. * error) and PAPEROUT and SELECT are
  74. * just red herrings, give the driver
  75. * a chance to check it's happy with
  76. * that before continuing. */
  77. goto stop;
  78. /* Have we run out of time? */
  79. if (!time_before (jiffies, expire))
  80. break;
  81. /* Yield the port for a while. If this is the
  82. first time around the loop, don't let go of
  83. the port. This way, we find out if we have
  84. our interrupt handler called. */
  85. if (count && no_irq) {
  86. parport_release (dev);
  87. __set_current_state (TASK_INTERRUPTIBLE);
  88. schedule_timeout (wait);
  89. parport_claim_or_block (dev);
  90. }
  91. else
  92. /* We must have the device claimed here */
  93. parport_wait_event (port, wait);
  94. /* Is there a signal pending? */
  95. if (signal_pending (current))
  96. break;
  97. /* Wait longer next time. */
  98. wait *= 2;
  99. } while (time_before (jiffies, expire));
  100. if (signal_pending (current))
  101. break;
  102. DPRINTK (KERN_DEBUG "%s: Timed out\n", port->name);
  103. break;
  104. ready:
  105. /* Write the character to the data lines. */
  106. byte = *addr++;
  107. parport_write_data (port, byte);
  108. udelay (1);
  109. /* Pulse strobe. */
  110. parport_write_control (port, ctl | PARPORT_CONTROL_STROBE);
  111. udelay (1); /* strobe */
  112. parport_write_control (port, ctl);
  113. udelay (1); /* hold */
  114. /* Assume the peripheral received it. */
  115. count++;
  116. /* Let another process run if it needs to. */
  117. if (time_before (jiffies, expire))
  118. if (!parport_yield_blocking (dev)
  119. && need_resched())
  120. schedule ();
  121. }
  122. stop:
  123. port->physport->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
  124. return count;
  125. }
  126. /* Nibble mode. */
  127. size_t parport_ieee1284_read_nibble (struct parport *port,
  128. void *buffer, size_t len,
  129. int flags)
  130. {
  131. #ifndef CONFIG_PARPORT_1284
  132. return 0;
  133. #else
  134. unsigned char *buf = buffer;
  135. int i;
  136. unsigned char byte = 0;
  137. len *= 2; /* in nibbles */
  138. for (i=0; i < len; i++) {
  139. unsigned char nibble;
  140. /* Does the error line indicate end of data? */
  141. if (((i & 1) == 0) &&
  142. (parport_read_status(port) & PARPORT_STATUS_ERROR)) {
  143. port->physport->ieee1284.phase = IEEE1284_PH_HBUSY_DNA;
  144. DPRINTK (KERN_DEBUG
  145. "%s: No more nibble data (%d bytes)\n",
  146. port->name, i/2);
  147. /* Go to reverse idle phase. */
  148. parport_frob_control (port,
  149. PARPORT_CONTROL_AUTOFD,
  150. PARPORT_CONTROL_AUTOFD);
  151. port->physport->ieee1284.phase = IEEE1284_PH_REV_IDLE;
  152. break;
  153. }
  154. /* Event 7: Set nAutoFd low. */
  155. parport_frob_control (port,
  156. PARPORT_CONTROL_AUTOFD,
  157. PARPORT_CONTROL_AUTOFD);
  158. /* Event 9: nAck goes low. */
  159. port->ieee1284.phase = IEEE1284_PH_REV_DATA;
  160. if (parport_wait_peripheral (port,
  161. PARPORT_STATUS_ACK, 0)) {
  162. /* Timeout -- no more data? */
  163. DPRINTK (KERN_DEBUG
  164. "%s: Nibble timeout at event 9 (%d bytes)\n",
  165. port->name, i/2);
  166. parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
  167. break;
  168. }
  169. /* Read a nibble. */
  170. nibble = parport_read_status (port) >> 3;
  171. nibble &= ~8;
  172. if ((nibble & 0x10) == 0)
  173. nibble |= 8;
  174. nibble &= 0xf;
  175. /* Event 10: Set nAutoFd high. */
  176. parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
  177. /* Event 11: nAck goes high. */
  178. if (parport_wait_peripheral (port,
  179. PARPORT_STATUS_ACK,
  180. PARPORT_STATUS_ACK)) {
  181. /* Timeout -- no more data? */
  182. DPRINTK (KERN_DEBUG
  183. "%s: Nibble timeout at event 11\n",
  184. port->name);
  185. break;
  186. }
  187. if (i & 1) {
  188. /* Second nibble */
  189. byte |= nibble << 4;
  190. *buf++ = byte;
  191. } else
  192. byte = nibble;
  193. }
  194. i /= 2; /* i is now in bytes */
  195. if (i == len) {
  196. /* Read the last nibble without checking data avail. */
  197. port = port->physport;
  198. if (parport_read_status (port) & PARPORT_STATUS_ERROR)
  199. port->ieee1284.phase = IEEE1284_PH_HBUSY_DNA;
  200. else
  201. port->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
  202. }
  203. return i;
  204. #endif /* IEEE1284 support */
  205. }
  206. /* Byte mode. */
  207. size_t parport_ieee1284_read_byte (struct parport *port,
  208. void *buffer, size_t len,
  209. int flags)
  210. {
  211. #ifndef CONFIG_PARPORT_1284
  212. return 0;
  213. #else
  214. unsigned char *buf = buffer;
  215. ssize_t count = 0;
  216. for (count = 0; count < len; count++) {
  217. unsigned char byte;
  218. /* Data available? */
  219. if (parport_read_status (port) & PARPORT_STATUS_ERROR) {
  220. port->physport->ieee1284.phase = IEEE1284_PH_HBUSY_DNA;
  221. DPRINTK (KERN_DEBUG
  222. "%s: No more byte data (%Zd bytes)\n",
  223. port->name, count);
  224. /* Go to reverse idle phase. */
  225. parport_frob_control (port,
  226. PARPORT_CONTROL_AUTOFD,
  227. PARPORT_CONTROL_AUTOFD);
  228. port->physport->ieee1284.phase = IEEE1284_PH_REV_IDLE;
  229. break;
  230. }
  231. /* Event 14: Place data bus in high impedance state. */
  232. parport_data_reverse (port);
  233. /* Event 7: Set nAutoFd low. */
  234. parport_frob_control (port,
  235. PARPORT_CONTROL_AUTOFD,
  236. PARPORT_CONTROL_AUTOFD);
  237. /* Event 9: nAck goes low. */
  238. port->physport->ieee1284.phase = IEEE1284_PH_REV_DATA;
  239. if (parport_wait_peripheral (port,
  240. PARPORT_STATUS_ACK,
  241. 0)) {
  242. /* Timeout -- no more data? */
  243. parport_frob_control (port, PARPORT_CONTROL_AUTOFD,
  244. 0);
  245. DPRINTK (KERN_DEBUG "%s: Byte timeout at event 9\n",
  246. port->name);
  247. break;
  248. }
  249. byte = parport_read_data (port);
  250. *buf++ = byte;
  251. /* Event 10: Set nAutoFd high */
  252. parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
  253. /* Event 11: nAck goes high. */
  254. if (parport_wait_peripheral (port,
  255. PARPORT_STATUS_ACK,
  256. PARPORT_STATUS_ACK)) {
  257. /* Timeout -- no more data? */
  258. DPRINTK (KERN_DEBUG "%s: Byte timeout at event 11\n",
  259. port->name);
  260. break;
  261. }
  262. /* Event 16: Set nStrobe low. */
  263. parport_frob_control (port,
  264. PARPORT_CONTROL_STROBE,
  265. PARPORT_CONTROL_STROBE);
  266. udelay (5);
  267. /* Event 17: Set nStrobe high. */
  268. parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
  269. }
  270. if (count == len) {
  271. /* Read the last byte without checking data avail. */
  272. port = port->physport;
  273. if (parport_read_status (port) & PARPORT_STATUS_ERROR)
  274. port->ieee1284.phase = IEEE1284_PH_HBUSY_DNA;
  275. else
  276. port->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
  277. }
  278. return count;
  279. #endif /* IEEE1284 support */
  280. }
  281. /*** *
  282. * ECP Functions. *
  283. * ***/
  284. #ifdef CONFIG_PARPORT_1284
  285. static inline
  286. int ecp_forward_to_reverse (struct parport *port)
  287. {
  288. int retval;
  289. /* Event 38: Set nAutoFd low */
  290. parport_frob_control (port,
  291. PARPORT_CONTROL_AUTOFD,
  292. PARPORT_CONTROL_AUTOFD);
  293. parport_data_reverse (port);
  294. udelay (5);
  295. /* Event 39: Set nInit low to initiate bus reversal */
  296. parport_frob_control (port,
  297. PARPORT_CONTROL_INIT,
  298. 0);
  299. /* Event 40: PError goes low */
  300. retval = parport_wait_peripheral (port,
  301. PARPORT_STATUS_PAPEROUT, 0);
  302. if (!retval) {
  303. DPRINTK (KERN_DEBUG "%s: ECP direction: reverse\n",
  304. port->name);
  305. port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
  306. } else {
  307. DPRINTK (KERN_DEBUG "%s: ECP direction: failed to reverse\n",
  308. port->name);
  309. port->ieee1284.phase = IEEE1284_PH_ECP_DIR_UNKNOWN;
  310. }
  311. return retval;
  312. }
  313. static inline
  314. int ecp_reverse_to_forward (struct parport *port)
  315. {
  316. int retval;
  317. /* Event 47: Set nInit high */
  318. parport_frob_control (port,
  319. PARPORT_CONTROL_INIT
  320. | PARPORT_CONTROL_AUTOFD,
  321. PARPORT_CONTROL_INIT
  322. | PARPORT_CONTROL_AUTOFD);
  323. /* Event 49: PError goes high */
  324. retval = parport_wait_peripheral (port,
  325. PARPORT_STATUS_PAPEROUT,
  326. PARPORT_STATUS_PAPEROUT);
  327. if (!retval) {
  328. parport_data_forward (port);
  329. DPRINTK (KERN_DEBUG "%s: ECP direction: forward\n",
  330. port->name);
  331. port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
  332. } else {
  333. DPRINTK (KERN_DEBUG
  334. "%s: ECP direction: failed to switch forward\n",
  335. port->name);
  336. port->ieee1284.phase = IEEE1284_PH_ECP_DIR_UNKNOWN;
  337. }
  338. return retval;
  339. }
  340. #endif /* IEEE1284 support */
  341. /* ECP mode, forward channel, data. */
  342. size_t parport_ieee1284_ecp_write_data (struct parport *port,
  343. const void *buffer, size_t len,
  344. int flags)
  345. {
  346. #ifndef CONFIG_PARPORT_1284
  347. return 0;
  348. #else
  349. const unsigned char *buf = buffer;
  350. size_t written;
  351. int retry;
  352. port = port->physport;
  353. if (port->ieee1284.phase != IEEE1284_PH_FWD_IDLE)
  354. if (ecp_reverse_to_forward (port))
  355. return 0;
  356. port->ieee1284.phase = IEEE1284_PH_FWD_DATA;
  357. /* HostAck high (data, not command) */
  358. parport_frob_control (port,
  359. PARPORT_CONTROL_AUTOFD
  360. | PARPORT_CONTROL_STROBE
  361. | PARPORT_CONTROL_INIT,
  362. PARPORT_CONTROL_INIT);
  363. for (written = 0; written < len; written++, buf++) {
  364. unsigned long expire = jiffies + port->cad->timeout;
  365. unsigned char byte;
  366. byte = *buf;
  367. try_again:
  368. parport_write_data (port, byte);
  369. parport_frob_control (port, PARPORT_CONTROL_STROBE,
  370. PARPORT_CONTROL_STROBE);
  371. udelay (5);
  372. for (retry = 0; retry < 100; retry++) {
  373. if (!parport_wait_peripheral (port,
  374. PARPORT_STATUS_BUSY, 0))
  375. goto success;
  376. if (signal_pending (current)) {
  377. parport_frob_control (port,
  378. PARPORT_CONTROL_STROBE,
  379. 0);
  380. break;
  381. }
  382. }
  383. /* Time for Host Transfer Recovery (page 41 of IEEE1284) */
  384. DPRINTK (KERN_DEBUG "%s: ECP transfer stalled!\n", port->name);
  385. parport_frob_control (port, PARPORT_CONTROL_INIT,
  386. PARPORT_CONTROL_INIT);
  387. udelay (50);
  388. if (parport_read_status (port) & PARPORT_STATUS_PAPEROUT) {
  389. /* It's buggered. */
  390. parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
  391. break;
  392. }
  393. parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
  394. udelay (50);
  395. if (!(parport_read_status (port) & PARPORT_STATUS_PAPEROUT))
  396. break;
  397. DPRINTK (KERN_DEBUG "%s: Host transfer recovered\n",
  398. port->name);
  399. if (time_after_eq (jiffies, expire)) break;
  400. goto try_again;
  401. success:
  402. parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
  403. udelay (5);
  404. if (parport_wait_peripheral (port,
  405. PARPORT_STATUS_BUSY,
  406. PARPORT_STATUS_BUSY))
  407. /* Peripheral hasn't accepted the data. */
  408. break;
  409. }
  410. port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
  411. return written;
  412. #endif /* IEEE1284 support */
  413. }
  414. /* ECP mode, reverse channel, data. */
  415. size_t parport_ieee1284_ecp_read_data (struct parport *port,
  416. void *buffer, size_t len, int flags)
  417. {
  418. #ifndef CONFIG_PARPORT_1284
  419. return 0;
  420. #else
  421. struct pardevice *dev = port->cad;
  422. unsigned char *buf = buffer;
  423. int rle_count = 0; /* shut gcc up */
  424. unsigned char ctl;
  425. int rle = 0;
  426. ssize_t count = 0;
  427. port = port->physport;
  428. if (port->ieee1284.phase != IEEE1284_PH_REV_IDLE)
  429. if (ecp_forward_to_reverse (port))
  430. return 0;
  431. port->ieee1284.phase = IEEE1284_PH_REV_DATA;
  432. /* Set HostAck low to start accepting data. */
  433. ctl = parport_read_control (port);
  434. ctl &= ~(PARPORT_CONTROL_STROBE | PARPORT_CONTROL_INIT |
  435. PARPORT_CONTROL_AUTOFD);
  436. parport_write_control (port,
  437. ctl | PARPORT_CONTROL_AUTOFD);
  438. while (count < len) {
  439. unsigned long expire = jiffies + dev->timeout;
  440. unsigned char byte;
  441. int command;
  442. /* Event 43: Peripheral sets nAck low. It can take as
  443. long as it wants. */
  444. while (parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0)) {
  445. /* The peripheral hasn't given us data in
  446. 35ms. If we have data to give back to the
  447. caller, do it now. */
  448. if (count)
  449. goto out;
  450. /* If we've used up all the time we were allowed,
  451. give up altogether. */
  452. if (!time_before (jiffies, expire))
  453. goto out;
  454. /* Yield the port for a while. */
  455. if (count && dev->port->irq != PARPORT_IRQ_NONE) {
  456. parport_release (dev);
  457. __set_current_state (TASK_INTERRUPTIBLE);
  458. schedule_timeout ((HZ + 24) / 25);
  459. parport_claim_or_block (dev);
  460. }
  461. else
  462. /* We must have the device claimed here. */
  463. parport_wait_event (port, (HZ + 24) / 25);
  464. /* Is there a signal pending? */
  465. if (signal_pending (current))
  466. goto out;
  467. }
  468. /* Is this a command? */
  469. if (rle)
  470. /* The last byte was a run-length count, so
  471. this can't be as well. */
  472. command = 0;
  473. else
  474. command = (parport_read_status (port) &
  475. PARPORT_STATUS_BUSY) ? 1 : 0;
  476. /* Read the data. */
  477. byte = parport_read_data (port);
  478. /* If this is a channel command, rather than an RLE
  479. command or a normal data byte, don't accept it. */
  480. if (command) {
  481. if (byte & 0x80) {
  482. DPRINTK (KERN_DEBUG "%s: stopping short at "
  483. "channel command (%02x)\n",
  484. port->name, byte);
  485. goto out;
  486. }
  487. else if (port->ieee1284.mode != IEEE1284_MODE_ECPRLE)
  488. DPRINTK (KERN_DEBUG "%s: device illegally "
  489. "using RLE; accepting anyway\n",
  490. port->name);
  491. rle_count = byte + 1;
  492. /* Are we allowed to read that many bytes? */
  493. if (rle_count > (len - count)) {
  494. DPRINTK (KERN_DEBUG "%s: leaving %d RLE bytes "
  495. "for next time\n", port->name,
  496. rle_count);
  497. break;
  498. }
  499. rle = 1;
  500. }
  501. /* Event 44: Set HostAck high, acknowledging handshake. */
  502. parport_write_control (port, ctl);
  503. /* Event 45: The peripheral has 35ms to set nAck high. */
  504. if (parport_wait_peripheral (port, PARPORT_STATUS_ACK,
  505. PARPORT_STATUS_ACK)) {
  506. /* It's gone wrong. Return what data we have
  507. to the caller. */
  508. DPRINTK (KERN_DEBUG "ECP read timed out at 45\n");
  509. if (command)
  510. printk (KERN_WARNING
  511. "%s: command ignored (%02x)\n",
  512. port->name, byte);
  513. break;
  514. }
  515. /* Event 46: Set HostAck low and accept the data. */
  516. parport_write_control (port,
  517. ctl | PARPORT_CONTROL_AUTOFD);
  518. /* If we just read a run-length count, fetch the data. */
  519. if (command)
  520. continue;
  521. /* If this is the byte after a run-length count, decompress. */
  522. if (rle) {
  523. rle = 0;
  524. memset (buf, byte, rle_count);
  525. buf += rle_count;
  526. count += rle_count;
  527. DPRINTK (KERN_DEBUG "%s: decompressed to %d bytes\n",
  528. port->name, rle_count);
  529. } else {
  530. /* Normal data byte. */
  531. *buf = byte;
  532. buf++, count++;
  533. }
  534. }
  535. out:
  536. port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
  537. return count;
  538. #endif /* IEEE1284 support */
  539. }
  540. /* ECP mode, forward channel, commands. */
  541. size_t parport_ieee1284_ecp_write_addr (struct parport *port,
  542. const void *buffer, size_t len,
  543. int flags)
  544. {
  545. #ifndef CONFIG_PARPORT_1284
  546. return 0;
  547. #else
  548. const unsigned char *buf = buffer;
  549. size_t written;
  550. int retry;
  551. port = port->physport;
  552. if (port->ieee1284.phase != IEEE1284_PH_FWD_IDLE)
  553. if (ecp_reverse_to_forward (port))
  554. return 0;
  555. port->ieee1284.phase = IEEE1284_PH_FWD_DATA;
  556. /* HostAck low (command, not data) */
  557. parport_frob_control (port,
  558. PARPORT_CONTROL_AUTOFD
  559. | PARPORT_CONTROL_STROBE
  560. | PARPORT_CONTROL_INIT,
  561. PARPORT_CONTROL_AUTOFD
  562. | PARPORT_CONTROL_INIT);
  563. for (written = 0; written < len; written++, buf++) {
  564. unsigned long expire = jiffies + port->cad->timeout;
  565. unsigned char byte;
  566. byte = *buf;
  567. try_again:
  568. parport_write_data (port, byte);
  569. parport_frob_control (port, PARPORT_CONTROL_STROBE,
  570. PARPORT_CONTROL_STROBE);
  571. udelay (5);
  572. for (retry = 0; retry < 100; retry++) {
  573. if (!parport_wait_peripheral (port,
  574. PARPORT_STATUS_BUSY, 0))
  575. goto success;
  576. if (signal_pending (current)) {
  577. parport_frob_control (port,
  578. PARPORT_CONTROL_STROBE,
  579. 0);
  580. break;
  581. }
  582. }
  583. /* Time for Host Transfer Recovery (page 41 of IEEE1284) */
  584. DPRINTK (KERN_DEBUG "%s: ECP transfer stalled!\n", port->name);
  585. parport_frob_control (port, PARPORT_CONTROL_INIT,
  586. PARPORT_CONTROL_INIT);
  587. udelay (50);
  588. if (parport_read_status (port) & PARPORT_STATUS_PAPEROUT) {
  589. /* It's buggered. */
  590. parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
  591. break;
  592. }
  593. parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
  594. udelay (50);
  595. if (!(parport_read_status (port) & PARPORT_STATUS_PAPEROUT))
  596. break;
  597. DPRINTK (KERN_DEBUG "%s: Host transfer recovered\n",
  598. port->name);
  599. if (time_after_eq (jiffies, expire)) break;
  600. goto try_again;
  601. success:
  602. parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
  603. udelay (5);
  604. if (parport_wait_peripheral (port,
  605. PARPORT_STATUS_BUSY,
  606. PARPORT_STATUS_BUSY))
  607. /* Peripheral hasn't accepted the data. */
  608. break;
  609. }
  610. port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
  611. return written;
  612. #endif /* IEEE1284 support */
  613. }
  614. /*** *
  615. * EPP functions. *
  616. * ***/
  617. /* EPP mode, forward channel, data. */
  618. size_t parport_ieee1284_epp_write_data (struct parport *port,
  619. const void *buffer, size_t len,
  620. int flags)
  621. {
  622. unsigned char *bp = (unsigned char *) buffer;
  623. size_t ret = 0;
  624. /* set EPP idle state (just to make sure) with strobe low */
  625. parport_frob_control (port,
  626. PARPORT_CONTROL_STROBE |
  627. PARPORT_CONTROL_AUTOFD |
  628. PARPORT_CONTROL_SELECT |
  629. PARPORT_CONTROL_INIT,
  630. PARPORT_CONTROL_STROBE |
  631. PARPORT_CONTROL_INIT);
  632. port->ops->data_forward (port);
  633. for (; len > 0; len--, bp++) {
  634. /* Event 62: Write data and set autofd low */
  635. parport_write_data (port, *bp);
  636. parport_frob_control (port, PARPORT_CONTROL_AUTOFD,
  637. PARPORT_CONTROL_AUTOFD);
  638. /* Event 58: wait for busy (nWait) to go high */
  639. if (parport_poll_peripheral (port, PARPORT_STATUS_BUSY, 0, 10))
  640. break;
  641. /* Event 63: set nAutoFd (nDStrb) high */
  642. parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
  643. /* Event 60: wait for busy (nWait) to go low */
  644. if (parport_poll_peripheral (port, PARPORT_STATUS_BUSY,
  645. PARPORT_STATUS_BUSY, 5))
  646. break;
  647. ret++;
  648. }
  649. /* Event 61: set strobe (nWrite) high */
  650. parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
  651. return ret;
  652. }
  653. /* EPP mode, reverse channel, data. */
  654. size_t parport_ieee1284_epp_read_data (struct parport *port,
  655. void *buffer, size_t len,
  656. int flags)
  657. {
  658. unsigned char *bp = (unsigned char *) buffer;
  659. unsigned ret = 0;
  660. /* set EPP idle state (just to make sure) with strobe high */
  661. parport_frob_control (port,
  662. PARPORT_CONTROL_STROBE |
  663. PARPORT_CONTROL_AUTOFD |
  664. PARPORT_CONTROL_SELECT |
  665. PARPORT_CONTROL_INIT,
  666. PARPORT_CONTROL_INIT);
  667. port->ops->data_reverse (port);
  668. for (; len > 0; len--, bp++) {
  669. /* Event 67: set nAutoFd (nDStrb) low */
  670. parport_frob_control (port,
  671. PARPORT_CONTROL_AUTOFD,
  672. PARPORT_CONTROL_AUTOFD);
  673. /* Event 58: wait for Busy to go high */
  674. if (parport_wait_peripheral (port, PARPORT_STATUS_BUSY, 0)) {
  675. break;
  676. }
  677. *bp = parport_read_data (port);
  678. /* Event 63: set nAutoFd (nDStrb) high */
  679. parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
  680. /* Event 60: wait for Busy to go low */
  681. if (parport_poll_peripheral (port, PARPORT_STATUS_BUSY,
  682. PARPORT_STATUS_BUSY, 5)) {
  683. break;
  684. }
  685. ret++;
  686. }
  687. port->ops->data_forward (port);
  688. return ret;
  689. }
  690. /* EPP mode, forward channel, addresses. */
  691. size_t parport_ieee1284_epp_write_addr (struct parport *port,
  692. const void *buffer, size_t len,
  693. int flags)
  694. {
  695. unsigned char *bp = (unsigned char *) buffer;
  696. size_t ret = 0;
  697. /* set EPP idle state (just to make sure) with strobe low */
  698. parport_frob_control (port,
  699. PARPORT_CONTROL_STROBE |
  700. PARPORT_CONTROL_AUTOFD |
  701. PARPORT_CONTROL_SELECT |
  702. PARPORT_CONTROL_INIT,
  703. PARPORT_CONTROL_STROBE |
  704. PARPORT_CONTROL_INIT);
  705. port->ops->data_forward (port);
  706. for (; len > 0; len--, bp++) {
  707. /* Event 56: Write data and set nAStrb low. */
  708. parport_write_data (port, *bp);
  709. parport_frob_control (port, PARPORT_CONTROL_SELECT,
  710. PARPORT_CONTROL_SELECT);
  711. /* Event 58: wait for busy (nWait) to go high */
  712. if (parport_poll_peripheral (port, PARPORT_STATUS_BUSY, 0, 10))
  713. break;
  714. /* Event 59: set nAStrb high */
  715. parport_frob_control (port, PARPORT_CONTROL_SELECT, 0);
  716. /* Event 60: wait for busy (nWait) to go low */
  717. if (parport_poll_peripheral (port, PARPORT_STATUS_BUSY,
  718. PARPORT_STATUS_BUSY, 5))
  719. break;
  720. ret++;
  721. }
  722. /* Event 61: set strobe (nWrite) high */
  723. parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
  724. return ret;
  725. }
  726. /* EPP mode, reverse channel, addresses. */
  727. size_t parport_ieee1284_epp_read_addr (struct parport *port,
  728. void *buffer, size_t len,
  729. int flags)
  730. {
  731. unsigned char *bp = (unsigned char *) buffer;
  732. unsigned ret = 0;
  733. /* Set EPP idle state (just to make sure) with strobe high */
  734. parport_frob_control (port,
  735. PARPORT_CONTROL_STROBE |
  736. PARPORT_CONTROL_AUTOFD |
  737. PARPORT_CONTROL_SELECT |
  738. PARPORT_CONTROL_INIT,
  739. PARPORT_CONTROL_INIT);
  740. port->ops->data_reverse (port);
  741. for (; len > 0; len--, bp++) {
  742. /* Event 64: set nSelectIn (nAStrb) low */
  743. parport_frob_control (port, PARPORT_CONTROL_SELECT,
  744. PARPORT_CONTROL_SELECT);
  745. /* Event 58: wait for Busy to go high */
  746. if (parport_wait_peripheral (port, PARPORT_STATUS_BUSY, 0)) {
  747. break;
  748. }
  749. *bp = parport_read_data (port);
  750. /* Event 59: set nSelectIn (nAStrb) high */
  751. parport_frob_control (port, PARPORT_CONTROL_SELECT,
  752. PARPORT_CONTROL_SELECT);
  753. /* Event 60: wait for Busy to go low */
  754. if (parport_poll_peripheral (port, PARPORT_STATUS_BUSY,
  755. PARPORT_STATUS_BUSY, 5))
  756. break;
  757. ret++;
  758. }
  759. port->ops->data_forward (port);
  760. return ret;
  761. }
  762. EXPORT_SYMBOL(parport_ieee1284_ecp_write_data);
  763. EXPORT_SYMBOL(parport_ieee1284_ecp_read_data);
  764. EXPORT_SYMBOL(parport_ieee1284_ecp_write_addr);
  765. EXPORT_SYMBOL(parport_ieee1284_write_compat);
  766. EXPORT_SYMBOL(parport_ieee1284_read_nibble);
  767. EXPORT_SYMBOL(parport_ieee1284_read_byte);
  768. EXPORT_SYMBOL(parport_ieee1284_epp_write_data);
  769. EXPORT_SYMBOL(parport_ieee1284_epp_read_data);
  770. EXPORT_SYMBOL(parport_ieee1284_epp_write_addr);
  771. EXPORT_SYMBOL(parport_ieee1284_epp_read_addr);