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