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