ieee1284.c 23 KB

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