ieee1284.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*
  2. * IEEE-1284 implementation for parport.
  3. *
  4. * Authors: Phil Blundell <philb@gnu.org>
  5. * Carsten Gross <carsten@sol.wohnheim.uni-ulm.de>
  6. * Jose Renau <renau@acm.org>
  7. * Tim Waugh <tim@cyberelk.demon.co.uk> (largely rewritten)
  8. *
  9. * This file is responsible for IEEE 1284 negotiation, and for handing
  10. * read/write requests to low-level drivers.
  11. *
  12. * Any part of this program may be used in documents licensed under
  13. * the GNU Free Documentation License, Version 1.1 or any later version
  14. * published by the Free Software Foundation.
  15. *
  16. * Various hacks, Fred Barnes <frmb2@ukc.ac.uk>, 04/2000
  17. */
  18. #include <linux/module.h>
  19. #include <linux/threads.h>
  20. #include <linux/parport.h>
  21. #include <linux/delay.h>
  22. #include <linux/kernel.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/timer.h>
  25. #include <linux/sched/signal.h>
  26. #undef DEBUG /* undef me for production */
  27. #ifdef CONFIG_LP_CONSOLE
  28. #undef DEBUG /* Don't want a garbled console */
  29. #endif
  30. #ifdef DEBUG
  31. #define DPRINTK(stuff...) printk (stuff)
  32. #else
  33. #define DPRINTK(stuff...)
  34. #endif
  35. /* Make parport_wait_peripheral wake up.
  36. * It will be useful to call this from an interrupt handler. */
  37. static void parport_ieee1284_wakeup (struct parport *port)
  38. {
  39. up (&port->physport->ieee1284.irq);
  40. }
  41. static void timeout_waiting_on_port (struct timer_list *t)
  42. {
  43. struct parport *port = from_timer(port, t, timer);
  44. parport_ieee1284_wakeup (port);
  45. }
  46. /**
  47. * parport_wait_event - wait for an event on a parallel port
  48. * @port: port to wait on
  49. * @timeout: time to wait (in jiffies)
  50. *
  51. * This function waits for up to @timeout jiffies for an
  52. * interrupt to occur on a parallel port. If the port timeout is
  53. * set to zero, it returns immediately.
  54. *
  55. * If an interrupt occurs before the timeout period elapses, this
  56. * function returns zero immediately. If it times out, it returns
  57. * one. An error code less than zero indicates an error (most
  58. * likely a pending signal), and the calling code should finish
  59. * what it's doing as soon as it can.
  60. */
  61. int parport_wait_event (struct parport *port, signed long timeout)
  62. {
  63. int ret;
  64. if (!port->physport->cad->timeout)
  65. /* Zero timeout is special, and we can't down() the
  66. semaphore. */
  67. return 1;
  68. timer_setup(&port->timer, timeout_waiting_on_port, 0);
  69. mod_timer(&port->timer, jiffies + timeout);
  70. ret = down_interruptible (&port->physport->ieee1284.irq);
  71. if (!del_timer_sync(&port->timer) && !ret)
  72. /* Timed out. */
  73. ret = 1;
  74. return ret;
  75. }
  76. /**
  77. * parport_poll_peripheral - poll status lines
  78. * @port: port to watch
  79. * @mask: status lines to watch
  80. * @result: desired values of chosen status lines
  81. * @usec: timeout
  82. *
  83. * This function busy-waits until the masked status lines have
  84. * the desired values, or until the timeout period elapses. The
  85. * @mask and @result parameters are bitmasks, with the bits
  86. * defined by the constants in parport.h: %PARPORT_STATUS_BUSY,
  87. * and so on.
  88. *
  89. * This function does not call schedule(); instead it busy-waits
  90. * using udelay(). It currently has a resolution of 5usec.
  91. *
  92. * If the status lines take on the desired values before the
  93. * timeout period elapses, parport_poll_peripheral() returns zero
  94. * immediately. A return value greater than zero indicates
  95. * a timeout. An error code (less than zero) indicates an error,
  96. * most likely a signal that arrived, and the caller should
  97. * finish what it is doing as soon as possible.
  98. */
  99. int parport_poll_peripheral(struct parport *port,
  100. unsigned char mask,
  101. unsigned char result,
  102. int usec)
  103. {
  104. /* Zero return code is success, >0 is timeout. */
  105. int count = usec / 5 + 2;
  106. int i;
  107. unsigned char status;
  108. for (i = 0; i < count; i++) {
  109. status = parport_read_status (port);
  110. if ((status & mask) == result)
  111. return 0;
  112. if (signal_pending (current))
  113. return -EINTR;
  114. if (need_resched())
  115. break;
  116. if (i >= 2)
  117. udelay (5);
  118. }
  119. return 1;
  120. }
  121. /**
  122. * parport_wait_peripheral - wait for status lines to change in 35ms
  123. * @port: port to watch
  124. * @mask: status lines to watch
  125. * @result: desired values of chosen status lines
  126. *
  127. * This function waits until the masked status lines have the
  128. * desired values, or until 35ms have elapsed (see IEEE 1284-1994
  129. * page 24 to 25 for why this value in particular is hardcoded).
  130. * The @mask and @result parameters are bitmasks, with the bits
  131. * defined by the constants in parport.h: %PARPORT_STATUS_BUSY,
  132. * and so on.
  133. *
  134. * The port is polled quickly to start off with, in anticipation
  135. * of a fast response from the peripheral. This fast polling
  136. * time is configurable (using /proc), and defaults to 500usec.
  137. * If the timeout for this port (see parport_set_timeout()) is
  138. * zero, the fast polling time is 35ms, and this function does
  139. * not call schedule().
  140. *
  141. * If the timeout for this port is non-zero, after the fast
  142. * polling fails it uses parport_wait_event() to wait for up to
  143. * 10ms, waking up if an interrupt occurs.
  144. */
  145. int parport_wait_peripheral(struct parport *port,
  146. unsigned char mask,
  147. unsigned char result)
  148. {
  149. int ret;
  150. int usec;
  151. unsigned long deadline;
  152. unsigned char status;
  153. usec = port->physport->spintime; /* usecs of fast polling */
  154. if (!port->physport->cad->timeout)
  155. /* A zero timeout is "special": busy wait for the
  156. entire 35ms. */
  157. usec = 35000;
  158. /* Fast polling.
  159. *
  160. * This should be adjustable.
  161. * How about making a note (in the device structure) of how long
  162. * it takes, so we know for next time?
  163. */
  164. ret = parport_poll_peripheral (port, mask, result, usec);
  165. if (ret != 1)
  166. return ret;
  167. if (!port->physport->cad->timeout)
  168. /* We may be in an interrupt handler, so we can't poll
  169. * slowly anyway. */
  170. return 1;
  171. /* 40ms of slow polling. */
  172. deadline = jiffies + msecs_to_jiffies(40);
  173. while (time_before (jiffies, deadline)) {
  174. if (signal_pending (current))
  175. return -EINTR;
  176. /* Wait for 10ms (or until an interrupt occurs if
  177. * the handler is set) */
  178. if ((ret = parport_wait_event (port, msecs_to_jiffies(10))) < 0)
  179. return ret;
  180. status = parport_read_status (port);
  181. if ((status & mask) == result)
  182. return 0;
  183. if (!ret) {
  184. /* parport_wait_event didn't time out, but the
  185. * peripheral wasn't actually ready either.
  186. * Wait for another 10ms. */
  187. schedule_timeout_interruptible(msecs_to_jiffies(10));
  188. }
  189. }
  190. return 1;
  191. }
  192. #ifdef CONFIG_PARPORT_1284
  193. /* Terminate a negotiated mode. */
  194. static void parport_ieee1284_terminate (struct parport *port)
  195. {
  196. int r;
  197. port = port->physport;
  198. /* EPP terminates differently. */
  199. switch (port->ieee1284.mode) {
  200. case IEEE1284_MODE_EPP:
  201. case IEEE1284_MODE_EPPSL:
  202. case IEEE1284_MODE_EPPSWE:
  203. /* Terminate from EPP mode. */
  204. /* Event 68: Set nInit low */
  205. parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
  206. udelay (50);
  207. /* Event 69: Set nInit high, nSelectIn low */
  208. parport_frob_control (port,
  209. PARPORT_CONTROL_SELECT
  210. | PARPORT_CONTROL_INIT,
  211. PARPORT_CONTROL_SELECT
  212. | PARPORT_CONTROL_INIT);
  213. break;
  214. case IEEE1284_MODE_ECP:
  215. case IEEE1284_MODE_ECPRLE:
  216. case IEEE1284_MODE_ECPSWE:
  217. /* In ECP we can only terminate from fwd idle phase. */
  218. if (port->ieee1284.phase != IEEE1284_PH_FWD_IDLE) {
  219. /* Event 47: Set nInit high */
  220. parport_frob_control (port,
  221. PARPORT_CONTROL_INIT
  222. | PARPORT_CONTROL_AUTOFD,
  223. PARPORT_CONTROL_INIT
  224. | PARPORT_CONTROL_AUTOFD);
  225. /* Event 49: PError goes high */
  226. r = parport_wait_peripheral (port,
  227. PARPORT_STATUS_PAPEROUT,
  228. PARPORT_STATUS_PAPEROUT);
  229. if (r)
  230. DPRINTK (KERN_INFO "%s: Timeout at event 49\n",
  231. port->name);
  232. parport_data_forward (port);
  233. DPRINTK (KERN_DEBUG "%s: ECP direction: forward\n",
  234. port->name);
  235. port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
  236. }
  237. /* fall through */
  238. default:
  239. /* Terminate from all other modes. */
  240. /* Event 22: Set nSelectIn low, nAutoFd high */
  241. parport_frob_control (port,
  242. PARPORT_CONTROL_SELECT
  243. | PARPORT_CONTROL_AUTOFD,
  244. PARPORT_CONTROL_SELECT);
  245. /* Event 24: nAck goes low */
  246. r = parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0);
  247. if (r)
  248. DPRINTK (KERN_INFO "%s: Timeout at event 24\n",
  249. port->name);
  250. /* Event 25: Set nAutoFd low */
  251. parport_frob_control (port,
  252. PARPORT_CONTROL_AUTOFD,
  253. PARPORT_CONTROL_AUTOFD);
  254. /* Event 27: nAck goes high */
  255. r = parport_wait_peripheral (port,
  256. PARPORT_STATUS_ACK,
  257. PARPORT_STATUS_ACK);
  258. if (r)
  259. DPRINTK (KERN_INFO "%s: Timeout at event 27\n",
  260. port->name);
  261. /* Event 29: Set nAutoFd high */
  262. parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
  263. }
  264. port->ieee1284.mode = IEEE1284_MODE_COMPAT;
  265. port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
  266. DPRINTK (KERN_DEBUG "%s: In compatibility (forward idle) mode\n",
  267. port->name);
  268. }
  269. #endif /* IEEE1284 support */
  270. /**
  271. * parport_negotiate - negotiate an IEEE 1284 mode
  272. * @port: port to use
  273. * @mode: mode to negotiate to
  274. *
  275. * Use this to negotiate to a particular IEEE 1284 transfer mode.
  276. * The @mode parameter should be one of the constants in
  277. * parport.h starting %IEEE1284_MODE_xxx.
  278. *
  279. * The return value is 0 if the peripheral has accepted the
  280. * negotiation to the mode specified, -1 if the peripheral is not
  281. * IEEE 1284 compliant (or not present), or 1 if the peripheral
  282. * has rejected the negotiation.
  283. */
  284. int parport_negotiate (struct parport *port, int mode)
  285. {
  286. #ifndef CONFIG_PARPORT_1284
  287. if (mode == IEEE1284_MODE_COMPAT)
  288. return 0;
  289. printk (KERN_ERR "parport: IEEE1284 not supported in this kernel\n");
  290. return -1;
  291. #else
  292. int m = mode & ~IEEE1284_ADDR;
  293. int r;
  294. unsigned char xflag;
  295. port = port->physport;
  296. /* Is there anything to do? */
  297. if (port->ieee1284.mode == mode)
  298. return 0;
  299. /* Is the difference just an address-or-not bit? */
  300. if ((port->ieee1284.mode & ~IEEE1284_ADDR) == (mode & ~IEEE1284_ADDR)){
  301. port->ieee1284.mode = mode;
  302. return 0;
  303. }
  304. /* Go to compatibility forward idle mode */
  305. if (port->ieee1284.mode != IEEE1284_MODE_COMPAT)
  306. parport_ieee1284_terminate (port);
  307. if (mode == IEEE1284_MODE_COMPAT)
  308. /* Compatibility mode: no negotiation. */
  309. return 0;
  310. switch (mode) {
  311. case IEEE1284_MODE_ECPSWE:
  312. m = IEEE1284_MODE_ECP;
  313. break;
  314. case IEEE1284_MODE_EPPSL:
  315. case IEEE1284_MODE_EPPSWE:
  316. m = IEEE1284_MODE_EPP;
  317. break;
  318. case IEEE1284_MODE_BECP:
  319. return -ENOSYS; /* FIXME (implement BECP) */
  320. }
  321. if (mode & IEEE1284_EXT_LINK)
  322. m = 1<<7; /* request extensibility link */
  323. port->ieee1284.phase = IEEE1284_PH_NEGOTIATION;
  324. /* Start off with nStrobe and nAutoFd high, and nSelectIn low */
  325. parport_frob_control (port,
  326. PARPORT_CONTROL_STROBE
  327. | PARPORT_CONTROL_AUTOFD
  328. | PARPORT_CONTROL_SELECT,
  329. PARPORT_CONTROL_SELECT);
  330. udelay(1);
  331. /* Event 0: Set data */
  332. parport_data_forward (port);
  333. parport_write_data (port, m);
  334. udelay (400); /* Shouldn't need to wait this long. */
  335. /* Event 1: Set nSelectIn high, nAutoFd low */
  336. parport_frob_control (port,
  337. PARPORT_CONTROL_SELECT
  338. | PARPORT_CONTROL_AUTOFD,
  339. PARPORT_CONTROL_AUTOFD);
  340. /* Event 2: PError, Select, nFault go high, nAck goes low */
  341. if (parport_wait_peripheral (port,
  342. PARPORT_STATUS_ERROR
  343. | PARPORT_STATUS_SELECT
  344. | PARPORT_STATUS_PAPEROUT
  345. | PARPORT_STATUS_ACK,
  346. PARPORT_STATUS_ERROR
  347. | PARPORT_STATUS_SELECT
  348. | PARPORT_STATUS_PAPEROUT)) {
  349. /* Timeout */
  350. parport_frob_control (port,
  351. PARPORT_CONTROL_SELECT
  352. | PARPORT_CONTROL_AUTOFD,
  353. PARPORT_CONTROL_SELECT);
  354. DPRINTK (KERN_DEBUG
  355. "%s: Peripheral not IEEE1284 compliant (0x%02X)\n",
  356. port->name, parport_read_status (port));
  357. port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
  358. return -1; /* Not IEEE1284 compliant */
  359. }
  360. /* Event 3: Set nStrobe low */
  361. parport_frob_control (port,
  362. PARPORT_CONTROL_STROBE,
  363. PARPORT_CONTROL_STROBE);
  364. /* Event 4: Set nStrobe and nAutoFd high */
  365. udelay (5);
  366. parport_frob_control (port,
  367. PARPORT_CONTROL_STROBE
  368. | PARPORT_CONTROL_AUTOFD,
  369. 0);
  370. /* Event 6: nAck goes high */
  371. if (parport_wait_peripheral (port,
  372. PARPORT_STATUS_ACK,
  373. PARPORT_STATUS_ACK)) {
  374. /* This shouldn't really happen with a compliant device. */
  375. DPRINTK (KERN_DEBUG
  376. "%s: Mode 0x%02x not supported? (0x%02x)\n",
  377. port->name, mode, port->ops->read_status (port));
  378. parport_ieee1284_terminate (port);
  379. return 1;
  380. }
  381. xflag = parport_read_status (port) & PARPORT_STATUS_SELECT;
  382. /* xflag should be high for all modes other than nibble (0). */
  383. if (mode && !xflag) {
  384. /* Mode not supported. */
  385. DPRINTK (KERN_DEBUG "%s: Mode 0x%02x rejected by peripheral\n",
  386. port->name, mode);
  387. parport_ieee1284_terminate (port);
  388. return 1;
  389. }
  390. /* More to do if we've requested extensibility link. */
  391. if (mode & IEEE1284_EXT_LINK) {
  392. m = mode & 0x7f;
  393. udelay (1);
  394. parport_write_data (port, m);
  395. udelay (1);
  396. /* Event 51: Set nStrobe low */
  397. parport_frob_control (port,
  398. PARPORT_CONTROL_STROBE,
  399. PARPORT_CONTROL_STROBE);
  400. /* Event 52: nAck goes low */
  401. if (parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0)) {
  402. /* This peripheral is _very_ slow. */
  403. DPRINTK (KERN_DEBUG
  404. "%s: Event 52 didn't happen\n",
  405. port->name);
  406. parport_ieee1284_terminate (port);
  407. return 1;
  408. }
  409. /* Event 53: Set nStrobe high */
  410. parport_frob_control (port,
  411. PARPORT_CONTROL_STROBE,
  412. 0);
  413. /* Event 55: nAck goes high */
  414. if (parport_wait_peripheral (port,
  415. PARPORT_STATUS_ACK,
  416. PARPORT_STATUS_ACK)) {
  417. /* This shouldn't really happen with a compliant
  418. * device. */
  419. DPRINTK (KERN_DEBUG
  420. "%s: Mode 0x%02x not supported? (0x%02x)\n",
  421. port->name, mode,
  422. port->ops->read_status (port));
  423. parport_ieee1284_terminate (port);
  424. return 1;
  425. }
  426. /* Event 54: Peripheral sets XFlag to reflect support */
  427. xflag = parport_read_status (port) & PARPORT_STATUS_SELECT;
  428. /* xflag should be high. */
  429. if (!xflag) {
  430. /* Extended mode not supported. */
  431. DPRINTK (KERN_DEBUG "%s: Extended mode 0x%02x not "
  432. "supported\n", port->name, mode);
  433. parport_ieee1284_terminate (port);
  434. return 1;
  435. }
  436. /* Any further setup is left to the caller. */
  437. }
  438. /* Mode is supported */
  439. DPRINTK (KERN_DEBUG "%s: In mode 0x%02x\n", port->name, mode);
  440. port->ieee1284.mode = mode;
  441. /* But ECP is special */
  442. if (!(mode & IEEE1284_EXT_LINK) && (m & IEEE1284_MODE_ECP)) {
  443. port->ieee1284.phase = IEEE1284_PH_ECP_SETUP;
  444. /* Event 30: Set nAutoFd low */
  445. parport_frob_control (port,
  446. PARPORT_CONTROL_AUTOFD,
  447. PARPORT_CONTROL_AUTOFD);
  448. /* Event 31: PError goes high. */
  449. r = parport_wait_peripheral (port,
  450. PARPORT_STATUS_PAPEROUT,
  451. PARPORT_STATUS_PAPEROUT);
  452. if (r) {
  453. DPRINTK (KERN_INFO "%s: Timeout at event 31\n",
  454. port->name);
  455. }
  456. port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
  457. DPRINTK (KERN_DEBUG "%s: ECP direction: forward\n",
  458. port->name);
  459. } else switch (mode) {
  460. case IEEE1284_MODE_NIBBLE:
  461. case IEEE1284_MODE_BYTE:
  462. port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
  463. break;
  464. default:
  465. port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
  466. }
  467. return 0;
  468. #endif /* IEEE1284 support */
  469. }
  470. /* Acknowledge that the peripheral has data available.
  471. * Events 18-20, in order to get from Reverse Idle phase
  472. * to Host Busy Data Available.
  473. * This will most likely be called from an interrupt.
  474. * Returns zero if data was available.
  475. */
  476. #ifdef CONFIG_PARPORT_1284
  477. static int parport_ieee1284_ack_data_avail (struct parport *port)
  478. {
  479. if (parport_read_status (port) & PARPORT_STATUS_ERROR)
  480. /* Event 18 didn't happen. */
  481. return -1;
  482. /* Event 20: nAutoFd goes high. */
  483. port->ops->frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
  484. port->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
  485. return 0;
  486. }
  487. #endif /* IEEE1284 support */
  488. /* Handle an interrupt. */
  489. void parport_ieee1284_interrupt (void *handle)
  490. {
  491. struct parport *port = handle;
  492. parport_ieee1284_wakeup (port);
  493. #ifdef CONFIG_PARPORT_1284
  494. if (port->ieee1284.phase == IEEE1284_PH_REV_IDLE) {
  495. /* An interrupt in this phase means that data
  496. * is now available. */
  497. DPRINTK (KERN_DEBUG "%s: Data available\n", port->name);
  498. parport_ieee1284_ack_data_avail (port);
  499. }
  500. #endif /* IEEE1284 support */
  501. }
  502. /**
  503. * parport_write - write a block of data to a parallel port
  504. * @port: port to write to
  505. * @buffer: data buffer (in kernel space)
  506. * @len: number of bytes of data to transfer
  507. *
  508. * This will write up to @len bytes of @buffer to the port
  509. * specified, using the IEEE 1284 transfer mode most recently
  510. * negotiated to (using parport_negotiate()), as long as that
  511. * mode supports forward transfers (host to peripheral).
  512. *
  513. * It is the caller's responsibility to ensure that the first
  514. * @len bytes of @buffer are valid.
  515. *
  516. * This function returns the number of bytes transferred (if zero
  517. * or positive), or else an error code.
  518. */
  519. ssize_t parport_write (struct parport *port, const void *buffer, size_t len)
  520. {
  521. #ifndef CONFIG_PARPORT_1284
  522. return port->ops->compat_write_data (port, buffer, len, 0);
  523. #else
  524. ssize_t retval;
  525. int mode = port->ieee1284.mode;
  526. int addr = mode & IEEE1284_ADDR;
  527. size_t (*fn) (struct parport *, const void *, size_t, int);
  528. /* Ignore the device-ID-request bit and the address bit. */
  529. mode &= ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
  530. /* Use the mode we're in. */
  531. switch (mode) {
  532. case IEEE1284_MODE_NIBBLE:
  533. case IEEE1284_MODE_BYTE:
  534. parport_negotiate (port, IEEE1284_MODE_COMPAT);
  535. /* fall through */
  536. case IEEE1284_MODE_COMPAT:
  537. DPRINTK (KERN_DEBUG "%s: Using compatibility mode\n",
  538. port->name);
  539. fn = port->ops->compat_write_data;
  540. break;
  541. case IEEE1284_MODE_EPP:
  542. DPRINTK (KERN_DEBUG "%s: Using EPP mode\n", port->name);
  543. if (addr) {
  544. fn = port->ops->epp_write_addr;
  545. } else {
  546. fn = port->ops->epp_write_data;
  547. }
  548. break;
  549. case IEEE1284_MODE_EPPSWE:
  550. DPRINTK (KERN_DEBUG "%s: Using software-emulated EPP mode\n",
  551. port->name);
  552. if (addr) {
  553. fn = parport_ieee1284_epp_write_addr;
  554. } else {
  555. fn = parport_ieee1284_epp_write_data;
  556. }
  557. break;
  558. case IEEE1284_MODE_ECP:
  559. case IEEE1284_MODE_ECPRLE:
  560. DPRINTK (KERN_DEBUG "%s: Using ECP mode\n", port->name);
  561. if (addr) {
  562. fn = port->ops->ecp_write_addr;
  563. } else {
  564. fn = port->ops->ecp_write_data;
  565. }
  566. break;
  567. case IEEE1284_MODE_ECPSWE:
  568. DPRINTK (KERN_DEBUG "%s: Using software-emulated ECP mode\n",
  569. port->name);
  570. /* The caller has specified that it must be emulated,
  571. * even if we have ECP hardware! */
  572. if (addr) {
  573. fn = parport_ieee1284_ecp_write_addr;
  574. } else {
  575. fn = parport_ieee1284_ecp_write_data;
  576. }
  577. break;
  578. default:
  579. DPRINTK (KERN_DEBUG "%s: Unknown mode 0x%02x\n", port->name,
  580. port->ieee1284.mode);
  581. return -ENOSYS;
  582. }
  583. retval = (*fn) (port, buffer, len, 0);
  584. DPRINTK (KERN_DEBUG "%s: wrote %d/%d bytes\n", port->name, retval, len);
  585. return retval;
  586. #endif /* IEEE1284 support */
  587. }
  588. /**
  589. * parport_read - read a block of data from a parallel port
  590. * @port: port to read from
  591. * @buffer: data buffer (in kernel space)
  592. * @len: number of bytes of data to transfer
  593. *
  594. * This will read up to @len bytes of @buffer to the port
  595. * specified, using the IEEE 1284 transfer mode most recently
  596. * negotiated to (using parport_negotiate()), as long as that
  597. * mode supports reverse transfers (peripheral to host).
  598. *
  599. * It is the caller's responsibility to ensure that the first
  600. * @len bytes of @buffer are available to write to.
  601. *
  602. * This function returns the number of bytes transferred (if zero
  603. * or positive), or else an error code.
  604. */
  605. ssize_t parport_read (struct parport *port, void *buffer, size_t len)
  606. {
  607. #ifndef CONFIG_PARPORT_1284
  608. printk (KERN_ERR "parport: IEEE1284 not supported in this kernel\n");
  609. return -ENODEV;
  610. #else
  611. int mode = port->physport->ieee1284.mode;
  612. int addr = mode & IEEE1284_ADDR;
  613. size_t (*fn) (struct parport *, void *, size_t, int);
  614. /* Ignore the device-ID-request bit and the address bit. */
  615. mode &= ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
  616. /* Use the mode we're in. */
  617. switch (mode) {
  618. case IEEE1284_MODE_COMPAT:
  619. /* if we can tri-state use BYTE mode instead of NIBBLE mode,
  620. * if that fails, revert to NIBBLE mode -- ought to store somewhere
  621. * the device's ability to do BYTE mode reverse transfers, so we don't
  622. * end up needlessly calling negotiate(BYTE) repeately.. (fb)
  623. */
  624. if ((port->physport->modes & PARPORT_MODE_TRISTATE) &&
  625. !parport_negotiate (port, IEEE1284_MODE_BYTE)) {
  626. /* got into BYTE mode OK */
  627. DPRINTK (KERN_DEBUG "%s: Using byte mode\n", port->name);
  628. fn = port->ops->byte_read_data;
  629. break;
  630. }
  631. if (parport_negotiate (port, IEEE1284_MODE_NIBBLE)) {
  632. return -EIO;
  633. }
  634. /* fall through to NIBBLE */
  635. case IEEE1284_MODE_NIBBLE:
  636. DPRINTK (KERN_DEBUG "%s: Using nibble mode\n", port->name);
  637. fn = port->ops->nibble_read_data;
  638. break;
  639. case IEEE1284_MODE_BYTE:
  640. DPRINTK (KERN_DEBUG "%s: Using byte mode\n", port->name);
  641. fn = port->ops->byte_read_data;
  642. break;
  643. case IEEE1284_MODE_EPP:
  644. DPRINTK (KERN_DEBUG "%s: Using EPP mode\n", port->name);
  645. if (addr) {
  646. fn = port->ops->epp_read_addr;
  647. } else {
  648. fn = port->ops->epp_read_data;
  649. }
  650. break;
  651. case IEEE1284_MODE_EPPSWE:
  652. DPRINTK (KERN_DEBUG "%s: Using software-emulated EPP mode\n",
  653. port->name);
  654. if (addr) {
  655. fn = parport_ieee1284_epp_read_addr;
  656. } else {
  657. fn = parport_ieee1284_epp_read_data;
  658. }
  659. break;
  660. case IEEE1284_MODE_ECP:
  661. case IEEE1284_MODE_ECPRLE:
  662. DPRINTK (KERN_DEBUG "%s: Using ECP mode\n", port->name);
  663. fn = port->ops->ecp_read_data;
  664. break;
  665. case IEEE1284_MODE_ECPSWE:
  666. DPRINTK (KERN_DEBUG "%s: Using software-emulated ECP mode\n",
  667. port->name);
  668. fn = parport_ieee1284_ecp_read_data;
  669. break;
  670. default:
  671. DPRINTK (KERN_DEBUG "%s: Unknown mode 0x%02x\n", port->name,
  672. port->physport->ieee1284.mode);
  673. return -ENOSYS;
  674. }
  675. return (*fn) (port, buffer, len, 0);
  676. #endif /* IEEE1284 support */
  677. }
  678. /**
  679. * parport_set_timeout - set the inactivity timeout for a device
  680. * @dev: device on a port
  681. * @inactivity: inactivity timeout (in jiffies)
  682. *
  683. * This sets the inactivity timeout for a particular device on a
  684. * port. This affects functions like parport_wait_peripheral().
  685. * The special value 0 means not to call schedule() while dealing
  686. * with this device.
  687. *
  688. * The return value is the previous inactivity timeout.
  689. *
  690. * Any callers of parport_wait_event() for this device are woken
  691. * up.
  692. */
  693. long parport_set_timeout (struct pardevice *dev, long inactivity)
  694. {
  695. long int old = dev->timeout;
  696. dev->timeout = inactivity;
  697. if (dev->port->physport->cad == dev)
  698. parport_ieee1284_wakeup (dev->port);
  699. return old;
  700. }
  701. /* Exported symbols for modules. */
  702. EXPORT_SYMBOL(parport_negotiate);
  703. EXPORT_SYMBOL(parport_write);
  704. EXPORT_SYMBOL(parport_read);
  705. EXPORT_SYMBOL(parport_wait_peripheral);
  706. EXPORT_SYMBOL(parport_wait_event);
  707. EXPORT_SYMBOL(parport_set_timeout);
  708. EXPORT_SYMBOL(parport_ieee1284_interrupt);