ieee1284_ops.c 23 KB

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