ieee1284_ops.c 23 KB

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