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