ieee1284.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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 one immediately. If it times out, it returns
  58. * a value greater than zero. An error code less than zero
  59. * indicates an error (most likely a pending signal), and the
  60. * calling code should finish 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 zero 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 + (HZ + 24) / 25;
  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, (HZ + 99) / 100)) < 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. __set_current_state (TASK_INTERRUPTIBLE);
  195. schedule_timeout ((HZ+ 99) / 100);
  196. }
  197. }
  198. return 1;
  199. }
  200. #ifdef CONFIG_PARPORT_1284
  201. /* Terminate a negotiated mode. */
  202. static void parport_ieee1284_terminate (struct parport *port)
  203. {
  204. int r;
  205. port = port->physport;
  206. /* EPP terminates differently. */
  207. switch (port->ieee1284.mode) {
  208. case IEEE1284_MODE_EPP:
  209. case IEEE1284_MODE_EPPSL:
  210. case IEEE1284_MODE_EPPSWE:
  211. /* Terminate from EPP mode. */
  212. /* Event 68: Set nInit low */
  213. parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
  214. udelay (50);
  215. /* Event 69: Set nInit high, nSelectIn low */
  216. parport_frob_control (port,
  217. PARPORT_CONTROL_SELECT
  218. | PARPORT_CONTROL_INIT,
  219. PARPORT_CONTROL_SELECT
  220. | PARPORT_CONTROL_INIT);
  221. break;
  222. case IEEE1284_MODE_ECP:
  223. case IEEE1284_MODE_ECPRLE:
  224. case IEEE1284_MODE_ECPSWE:
  225. /* In ECP we can only terminate from fwd idle phase. */
  226. if (port->ieee1284.phase != IEEE1284_PH_FWD_IDLE) {
  227. /* Event 47: Set nInit high */
  228. parport_frob_control (port,
  229. PARPORT_CONTROL_INIT
  230. | PARPORT_CONTROL_AUTOFD,
  231. PARPORT_CONTROL_INIT
  232. | PARPORT_CONTROL_AUTOFD);
  233. /* Event 49: PError goes high */
  234. r = parport_wait_peripheral (port,
  235. PARPORT_STATUS_PAPEROUT,
  236. PARPORT_STATUS_PAPEROUT);
  237. if (r)
  238. DPRINTK (KERN_INFO "%s: Timeout at event 49\n",
  239. port->name);
  240. parport_data_forward (port);
  241. DPRINTK (KERN_DEBUG "%s: ECP direction: forward\n",
  242. port->name);
  243. port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
  244. }
  245. /* fall-though.. */
  246. default:
  247. /* Terminate from all other modes. */
  248. /* Event 22: Set nSelectIn low, nAutoFd high */
  249. parport_frob_control (port,
  250. PARPORT_CONTROL_SELECT
  251. | PARPORT_CONTROL_AUTOFD,
  252. PARPORT_CONTROL_SELECT);
  253. /* Event 24: nAck goes low */
  254. r = parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0);
  255. if (r)
  256. DPRINTK (KERN_INFO "%s: Timeout at event 24\n",
  257. port->name);
  258. /* Event 25: Set nAutoFd low */
  259. parport_frob_control (port,
  260. PARPORT_CONTROL_AUTOFD,
  261. PARPORT_CONTROL_AUTOFD);
  262. /* Event 27: nAck goes high */
  263. r = parport_wait_peripheral (port,
  264. PARPORT_STATUS_ACK,
  265. PARPORT_STATUS_ACK);
  266. if (r)
  267. DPRINTK (KERN_INFO "%s: Timeout at event 27\n",
  268. port->name);
  269. /* Event 29: Set nAutoFd high */
  270. parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
  271. }
  272. port->ieee1284.mode = IEEE1284_MODE_COMPAT;
  273. port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
  274. DPRINTK (KERN_DEBUG "%s: In compatibility (forward idle) mode\n",
  275. port->name);
  276. }
  277. #endif /* IEEE1284 support */
  278. /**
  279. * parport_negotiate - negotiate an IEEE 1284 mode
  280. * @port: port to use
  281. * @mode: mode to negotiate to
  282. *
  283. * Use this to negotiate to a particular IEEE 1284 transfer mode.
  284. * The @mode parameter should be one of the constants in
  285. * parport.h starting %IEEE1284_MODE_xxx.
  286. *
  287. * The return value is 0 if the peripheral has accepted the
  288. * negotiation to the mode specified, -1 if the peripheral is not
  289. * IEEE 1284 compliant (or not present), or 1 if the peripheral
  290. * has rejected the negotiation.
  291. */
  292. int parport_negotiate (struct parport *port, int mode)
  293. {
  294. #ifndef CONFIG_PARPORT_1284
  295. if (mode == IEEE1284_MODE_COMPAT)
  296. return 0;
  297. printk (KERN_ERR "parport: IEEE1284 not supported in this kernel\n");
  298. return -1;
  299. #else
  300. int m = mode & ~IEEE1284_ADDR;
  301. int r;
  302. unsigned char xflag;
  303. port = port->physport;
  304. /* Is there anything to do? */
  305. if (port->ieee1284.mode == mode)
  306. return 0;
  307. /* Is the difference just an address-or-not bit? */
  308. if ((port->ieee1284.mode & ~IEEE1284_ADDR) == (mode & ~IEEE1284_ADDR)){
  309. port->ieee1284.mode = mode;
  310. return 0;
  311. }
  312. /* Go to compability forward idle mode */
  313. if (port->ieee1284.mode != IEEE1284_MODE_COMPAT)
  314. parport_ieee1284_terminate (port);
  315. if (mode == IEEE1284_MODE_COMPAT)
  316. /* Compatibility mode: no negotiation. */
  317. return 0;
  318. switch (mode) {
  319. case IEEE1284_MODE_ECPSWE:
  320. m = IEEE1284_MODE_ECP;
  321. break;
  322. case IEEE1284_MODE_EPPSL:
  323. case IEEE1284_MODE_EPPSWE:
  324. m = IEEE1284_MODE_EPP;
  325. break;
  326. case IEEE1284_MODE_BECP:
  327. return -ENOSYS; /* FIXME (implement BECP) */
  328. }
  329. if (mode & IEEE1284_EXT_LINK)
  330. m = 1<<7; /* request extensibility link */
  331. port->ieee1284.phase = IEEE1284_PH_NEGOTIATION;
  332. /* Start off with nStrobe and nAutoFd high, and nSelectIn low */
  333. parport_frob_control (port,
  334. PARPORT_CONTROL_STROBE
  335. | PARPORT_CONTROL_AUTOFD
  336. | PARPORT_CONTROL_SELECT,
  337. PARPORT_CONTROL_SELECT);
  338. udelay(1);
  339. /* Event 0: Set data */
  340. parport_data_forward (port);
  341. parport_write_data (port, m);
  342. udelay (400); /* Shouldn't need to wait this long. */
  343. /* Event 1: Set nSelectIn high, nAutoFd low */
  344. parport_frob_control (port,
  345. PARPORT_CONTROL_SELECT
  346. | PARPORT_CONTROL_AUTOFD,
  347. PARPORT_CONTROL_AUTOFD);
  348. /* Event 2: PError, Select, nFault go high, nAck goes low */
  349. if (parport_wait_peripheral (port,
  350. PARPORT_STATUS_ERROR
  351. | PARPORT_STATUS_SELECT
  352. | PARPORT_STATUS_PAPEROUT
  353. | PARPORT_STATUS_ACK,
  354. PARPORT_STATUS_ERROR
  355. | PARPORT_STATUS_SELECT
  356. | PARPORT_STATUS_PAPEROUT)) {
  357. /* Timeout */
  358. parport_frob_control (port,
  359. PARPORT_CONTROL_SELECT
  360. | PARPORT_CONTROL_AUTOFD,
  361. PARPORT_CONTROL_SELECT);
  362. DPRINTK (KERN_DEBUG
  363. "%s: Peripheral not IEEE1284 compliant (0x%02X)\n",
  364. port->name, parport_read_status (port));
  365. port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
  366. return -1; /* Not IEEE1284 compliant */
  367. }
  368. /* Event 3: Set nStrobe low */
  369. parport_frob_control (port,
  370. PARPORT_CONTROL_STROBE,
  371. PARPORT_CONTROL_STROBE);
  372. /* Event 4: Set nStrobe and nAutoFd high */
  373. udelay (5);
  374. parport_frob_control (port,
  375. PARPORT_CONTROL_STROBE
  376. | PARPORT_CONTROL_AUTOFD,
  377. 0);
  378. /* Event 6: nAck goes high */
  379. if (parport_wait_peripheral (port,
  380. PARPORT_STATUS_ACK,
  381. PARPORT_STATUS_ACK)) {
  382. /* This shouldn't really happen with a compliant device. */
  383. DPRINTK (KERN_DEBUG
  384. "%s: Mode 0x%02x not supported? (0x%02x)\n",
  385. port->name, mode, port->ops->read_status (port));
  386. parport_ieee1284_terminate (port);
  387. return 1;
  388. }
  389. xflag = parport_read_status (port) & PARPORT_STATUS_SELECT;
  390. /* xflag should be high for all modes other than nibble (0). */
  391. if (mode && !xflag) {
  392. /* Mode not supported. */
  393. DPRINTK (KERN_DEBUG "%s: Mode 0x%02x rejected by peripheral\n",
  394. port->name, mode);
  395. parport_ieee1284_terminate (port);
  396. return 1;
  397. }
  398. /* More to do if we've requested extensibility link. */
  399. if (mode & IEEE1284_EXT_LINK) {
  400. m = mode & 0x7f;
  401. udelay (1);
  402. parport_write_data (port, m);
  403. udelay (1);
  404. /* Event 51: Set nStrobe low */
  405. parport_frob_control (port,
  406. PARPORT_CONTROL_STROBE,
  407. PARPORT_CONTROL_STROBE);
  408. /* Event 52: nAck goes low */
  409. if (parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0)) {
  410. /* This peripheral is _very_ slow. */
  411. DPRINTK (KERN_DEBUG
  412. "%s: Event 52 didn't happen\n",
  413. port->name);
  414. parport_ieee1284_terminate (port);
  415. return 1;
  416. }
  417. /* Event 53: Set nStrobe high */
  418. parport_frob_control (port,
  419. PARPORT_CONTROL_STROBE,
  420. 0);
  421. /* Event 55: nAck goes high */
  422. if (parport_wait_peripheral (port,
  423. PARPORT_STATUS_ACK,
  424. PARPORT_STATUS_ACK)) {
  425. /* This shouldn't really happen with a compliant
  426. * device. */
  427. DPRINTK (KERN_DEBUG
  428. "%s: Mode 0x%02x not supported? (0x%02x)\n",
  429. port->name, mode,
  430. port->ops->read_status (port));
  431. parport_ieee1284_terminate (port);
  432. return 1;
  433. }
  434. /* Event 54: Peripheral sets XFlag to reflect support */
  435. xflag = parport_read_status (port) & PARPORT_STATUS_SELECT;
  436. /* xflag should be high. */
  437. if (!xflag) {
  438. /* Extended mode not supported. */
  439. DPRINTK (KERN_DEBUG "%s: Extended mode 0x%02x not "
  440. "supported\n", port->name, mode);
  441. parport_ieee1284_terminate (port);
  442. return 1;
  443. }
  444. /* Any further setup is left to the caller. */
  445. }
  446. /* Mode is supported */
  447. DPRINTK (KERN_DEBUG "%s: In mode 0x%02x\n", port->name, mode);
  448. port->ieee1284.mode = mode;
  449. /* But ECP is special */
  450. if (!(mode & IEEE1284_EXT_LINK) && (m & IEEE1284_MODE_ECP)) {
  451. port->ieee1284.phase = IEEE1284_PH_ECP_SETUP;
  452. /* Event 30: Set nAutoFd low */
  453. parport_frob_control (port,
  454. PARPORT_CONTROL_AUTOFD,
  455. PARPORT_CONTROL_AUTOFD);
  456. /* Event 31: PError goes high. */
  457. r = parport_wait_peripheral (port,
  458. PARPORT_STATUS_PAPEROUT,
  459. PARPORT_STATUS_PAPEROUT);
  460. if (r) {
  461. DPRINTK (KERN_INFO "%s: Timeout at event 31\n",
  462. port->name);
  463. }
  464. port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
  465. DPRINTK (KERN_DEBUG "%s: ECP direction: forward\n",
  466. port->name);
  467. } else switch (mode) {
  468. case IEEE1284_MODE_NIBBLE:
  469. case IEEE1284_MODE_BYTE:
  470. port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
  471. break;
  472. default:
  473. port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
  474. }
  475. return 0;
  476. #endif /* IEEE1284 support */
  477. }
  478. /* Acknowledge that the peripheral has data available.
  479. * Events 18-20, in order to get from Reverse Idle phase
  480. * to Host Busy Data Available.
  481. * This will most likely be called from an interrupt.
  482. * Returns zero if data was available.
  483. */
  484. #ifdef CONFIG_PARPORT_1284
  485. static int parport_ieee1284_ack_data_avail (struct parport *port)
  486. {
  487. if (parport_read_status (port) & PARPORT_STATUS_ERROR)
  488. /* Event 18 didn't happen. */
  489. return -1;
  490. /* Event 20: nAutoFd goes high. */
  491. port->ops->frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
  492. port->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
  493. return 0;
  494. }
  495. #endif /* IEEE1284 support */
  496. /* Handle an interrupt. */
  497. void parport_ieee1284_interrupt (int which, void *handle, struct pt_regs *regs)
  498. {
  499. struct parport *port = handle;
  500. parport_ieee1284_wakeup (port);
  501. #ifdef CONFIG_PARPORT_1284
  502. if (port->ieee1284.phase == IEEE1284_PH_REV_IDLE) {
  503. /* An interrupt in this phase means that data
  504. * is now available. */
  505. DPRINTK (KERN_DEBUG "%s: Data available\n", port->name);
  506. parport_ieee1284_ack_data_avail (port);
  507. }
  508. #endif /* IEEE1284 support */
  509. }
  510. /**
  511. * parport_write - write a block of data to a parallel port
  512. * @port: port to write to
  513. * @buffer: data buffer (in kernel space)
  514. * @len: number of bytes of data to transfer
  515. *
  516. * This will write up to @len bytes of @buffer to the port
  517. * specified, using the IEEE 1284 transfer mode most recently
  518. * negotiated to (using parport_negotiate()), as long as that
  519. * mode supports forward transfers (host to peripheral).
  520. *
  521. * It is the caller's responsibility to ensure that the first
  522. * @len bytes of @buffer are valid.
  523. *
  524. * This function returns the number of bytes transferred (if zero
  525. * or positive), or else an error code.
  526. */
  527. ssize_t parport_write (struct parport *port, const void *buffer, size_t len)
  528. {
  529. #ifndef CONFIG_PARPORT_1284
  530. return port->ops->compat_write_data (port, buffer, len, 0);
  531. #else
  532. ssize_t retval;
  533. int mode = port->ieee1284.mode;
  534. int addr = mode & IEEE1284_ADDR;
  535. size_t (*fn) (struct parport *, const void *, size_t, int);
  536. /* Ignore the device-ID-request bit and the address bit. */
  537. mode &= ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
  538. /* Use the mode we're in. */
  539. switch (mode) {
  540. case IEEE1284_MODE_NIBBLE:
  541. case IEEE1284_MODE_BYTE:
  542. parport_negotiate (port, IEEE1284_MODE_COMPAT);
  543. case IEEE1284_MODE_COMPAT:
  544. DPRINTK (KERN_DEBUG "%s: Using compatibility mode\n",
  545. port->name);
  546. fn = port->ops->compat_write_data;
  547. break;
  548. case IEEE1284_MODE_EPP:
  549. DPRINTK (KERN_DEBUG "%s: Using EPP mode\n", port->name);
  550. if (addr) {
  551. fn = port->ops->epp_write_addr;
  552. } else {
  553. fn = port->ops->epp_write_data;
  554. }
  555. break;
  556. case IEEE1284_MODE_EPPSWE:
  557. DPRINTK (KERN_DEBUG "%s: Using software-emulated EPP mode\n",
  558. port->name);
  559. if (addr) {
  560. fn = parport_ieee1284_epp_write_addr;
  561. } else {
  562. fn = parport_ieee1284_epp_write_data;
  563. }
  564. break;
  565. case IEEE1284_MODE_ECP:
  566. case IEEE1284_MODE_ECPRLE:
  567. DPRINTK (KERN_DEBUG "%s: Using ECP mode\n", port->name);
  568. if (addr) {
  569. fn = port->ops->ecp_write_addr;
  570. } else {
  571. fn = port->ops->ecp_write_data;
  572. }
  573. break;
  574. case IEEE1284_MODE_ECPSWE:
  575. DPRINTK (KERN_DEBUG "%s: Using software-emulated ECP mode\n",
  576. port->name);
  577. /* The caller has specified that it must be emulated,
  578. * even if we have ECP hardware! */
  579. if (addr) {
  580. fn = parport_ieee1284_ecp_write_addr;
  581. } else {
  582. fn = parport_ieee1284_ecp_write_data;
  583. }
  584. break;
  585. default:
  586. DPRINTK (KERN_DEBUG "%s: Unknown mode 0x%02x\n", port->name,
  587. port->ieee1284.mode);
  588. return -ENOSYS;
  589. }
  590. retval = (*fn) (port, buffer, len, 0);
  591. DPRINTK (KERN_DEBUG "%s: wrote %d/%d bytes\n", port->name, retval, len);
  592. return retval;
  593. #endif /* IEEE1284 support */
  594. }
  595. /**
  596. * parport_read - read a block of data from a parallel port
  597. * @port: port to read from
  598. * @buffer: data buffer (in kernel space)
  599. * @len: number of bytes of data to transfer
  600. *
  601. * This will read up to @len bytes of @buffer to the port
  602. * specified, using the IEEE 1284 transfer mode most recently
  603. * negotiated to (using parport_negotiate()), as long as that
  604. * mode supports reverse transfers (peripheral to host).
  605. *
  606. * It is the caller's responsibility to ensure that the first
  607. * @len bytes of @buffer are available to write to.
  608. *
  609. * This function returns the number of bytes transferred (if zero
  610. * or positive), or else an error code.
  611. */
  612. ssize_t parport_read (struct parport *port, void *buffer, size_t len)
  613. {
  614. #ifndef CONFIG_PARPORT_1284
  615. printk (KERN_ERR "parport: IEEE1284 not supported in this kernel\n");
  616. return -ENODEV;
  617. #else
  618. int mode = port->physport->ieee1284.mode;
  619. int addr = mode & IEEE1284_ADDR;
  620. size_t (*fn) (struct parport *, void *, size_t, int);
  621. /* Ignore the device-ID-request bit and the address bit. */
  622. mode &= ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
  623. /* Use the mode we're in. */
  624. switch (mode) {
  625. case IEEE1284_MODE_COMPAT:
  626. /* if we can tri-state use BYTE mode instead of NIBBLE mode,
  627. * if that fails, revert to NIBBLE mode -- ought to store somewhere
  628. * the device's ability to do BYTE mode reverse transfers, so we don't
  629. * end up needlessly calling negotiate(BYTE) repeately.. (fb)
  630. */
  631. if ((port->physport->modes & PARPORT_MODE_TRISTATE) &&
  632. !parport_negotiate (port, IEEE1284_MODE_BYTE)) {
  633. /* got into BYTE mode OK */
  634. DPRINTK (KERN_DEBUG "%s: Using byte mode\n", port->name);
  635. fn = port->ops->byte_read_data;
  636. break;
  637. }
  638. if (parport_negotiate (port, IEEE1284_MODE_NIBBLE)) {
  639. return -EIO;
  640. }
  641. /* fall through to NIBBLE */
  642. case IEEE1284_MODE_NIBBLE:
  643. DPRINTK (KERN_DEBUG "%s: Using nibble mode\n", port->name);
  644. fn = port->ops->nibble_read_data;
  645. break;
  646. case IEEE1284_MODE_BYTE:
  647. DPRINTK (KERN_DEBUG "%s: Using byte mode\n", port->name);
  648. fn = port->ops->byte_read_data;
  649. break;
  650. case IEEE1284_MODE_EPP:
  651. DPRINTK (KERN_DEBUG "%s: Using EPP mode\n", port->name);
  652. if (addr) {
  653. fn = port->ops->epp_read_addr;
  654. } else {
  655. fn = port->ops->epp_read_data;
  656. }
  657. break;
  658. case IEEE1284_MODE_EPPSWE:
  659. DPRINTK (KERN_DEBUG "%s: Using software-emulated EPP mode\n",
  660. port->name);
  661. if (addr) {
  662. fn = parport_ieee1284_epp_read_addr;
  663. } else {
  664. fn = parport_ieee1284_epp_read_data;
  665. }
  666. break;
  667. case IEEE1284_MODE_ECP:
  668. case IEEE1284_MODE_ECPRLE:
  669. DPRINTK (KERN_DEBUG "%s: Using ECP mode\n", port->name);
  670. fn = port->ops->ecp_read_data;
  671. break;
  672. case IEEE1284_MODE_ECPSWE:
  673. DPRINTK (KERN_DEBUG "%s: Using software-emulated ECP mode\n",
  674. port->name);
  675. fn = parport_ieee1284_ecp_read_data;
  676. break;
  677. default:
  678. DPRINTK (KERN_DEBUG "%s: Unknown mode 0x%02x\n", port->name,
  679. port->physport->ieee1284.mode);
  680. return -ENOSYS;
  681. }
  682. return (*fn) (port, buffer, len, 0);
  683. #endif /* IEEE1284 support */
  684. }
  685. /**
  686. * parport_set_timeout - set the inactivity timeout for a device
  687. * @dev: device on a port
  688. * @inactivity: inactivity timeout (in jiffies)
  689. *
  690. * This sets the inactivity timeout for a particular device on a
  691. * port. This affects functions like parport_wait_peripheral().
  692. * The special value 0 means not to call schedule() while dealing
  693. * with this device.
  694. *
  695. * The return value is the previous inactivity timeout.
  696. *
  697. * Any callers of parport_wait_event() for this device are woken
  698. * up.
  699. */
  700. long parport_set_timeout (struct pardevice *dev, long inactivity)
  701. {
  702. long int old = dev->timeout;
  703. dev->timeout = inactivity;
  704. if (dev->port->physport->cad == dev)
  705. parport_ieee1284_wakeup (dev->port);
  706. return old;
  707. }
  708. /* Exported symbols for modules. */
  709. EXPORT_SYMBOL(parport_negotiate);
  710. EXPORT_SYMBOL(parport_write);
  711. EXPORT_SYMBOL(parport_read);
  712. EXPORT_SYMBOL(parport_wait_peripheral);
  713. EXPORT_SYMBOL(parport_wait_event);
  714. EXPORT_SYMBOL(parport_set_timeout);
  715. EXPORT_SYMBOL(parport_ieee1284_interrupt);