ieee1284_ops.c 23 KB

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