ieee1284_ops.c 23 KB

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