ieee1284.c 22 KB

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