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