ieee1284.c 23 KB

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