Import 2.3.10pre1
[davej-history.git] / drivers / misc / parport_ieee1284.c
blob6cac030a195b520b422336d9479648c665acd721
1 /* $Id: parport_ieee1284.c,v 1.4 1997/10/19 21:37:21 philip Exp $
2 * IEEE-1284 implementation for parport.
4 * Authors: Phil Blundell <Philip.Blundell@pobox.com>
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)
9 * This file is responsible for IEEE 1284 negotiation, and for handing
10 * read/write requests to low-level drivers.
13 #include <linux/config.h>
14 #include <linux/tasks.h>
15 #include <linux/parport.h>
16 #include <linux/delay.h>
17 #include <linux/kernel.h>
18 #include <linux/interrupt.h>
20 #define DEBUG /* undef me for production */
22 #ifdef CONFIG_LP_CONSOLE
23 #undef DEBUG /* Don't want a garbled console */
24 #endif
26 #ifdef DEBUG
27 #define DPRINTK(stuff...) printk (stuff)
28 #else
29 #define DPRINTK(stuff...)
30 #endif
32 /* Make parport_wait_peripheral wake up.
33 * It will be useful to call this from an interrupt handler. */
34 void parport_ieee1284_wakeup (struct parport *port)
36 up (&port->physport->ieee1284.irq);
39 static struct parport *port_from_cookie[PARPORT_MAX];
40 static void timeout_waiting_on_port (unsigned long cookie)
42 parport_ieee1284_wakeup (port_from_cookie[cookie % PARPORT_MAX]);
45 /* Wait for a parport_ieee1284_wakeup.
46 * 0: success
47 * <0: error (exit as soon as possible)
48 * >0: timed out
50 int parport_wait_event (struct parport *port, signed long timeout)
52 int ret;
53 struct timer_list timer;
55 if (!port->physport->cad->timeout)
56 /* Zero timeout is special, and we can't down() the
57 semaphore. */
58 return 1;
60 init_timer (&timer);
61 timer.expires = jiffies + timeout;
62 timer.function = timeout_waiting_on_port;
63 port_from_cookie[port->number % PARPORT_MAX] = port;
64 timer.data = port->number;
66 add_timer (&timer);
67 ret = down_interruptible (&port->physport->ieee1284.irq);
68 if (!del_timer (&timer) && !ret)
69 /* Timed out. */
70 ret = 1;
72 return ret;
75 /* Wait for Status line(s) to change in 35 ms - see IEEE1284-1994 page 24 to
76 * 25 for this. After this time we can create a timeout because the
77 * peripheral doesn't conform to IEEE1284. We want to save CPU time: we are
78 * waiting a maximum time of 500 us busy (this is for speed). If there is
79 * not the right answer in this time, we call schedule and other processes
80 * are able to eat the time up to 40ms.
81 */
83 int parport_wait_peripheral(struct parport *port,
84 unsigned char mask,
85 unsigned char result)
87 int counter;
88 long deadline;
89 unsigned char status;
91 counter = port->physport->spintime; /* usecs of fast polling */
92 if (!port->physport->cad->timeout)
93 /* A zero timeout is "special": busy wait for the
94 entire 35ms. */
95 counter = 35000;
97 /* Fast polling.
99 * This should be adjustable.
100 * How about making a note (in the device structure) of how long
101 * it takes, so we know for next time?
103 for (counter /= 5; counter > 0; counter--) {
104 status = parport_read_status (port);
105 if ((status & mask) == result)
106 return 0;
107 if (signal_pending (current))
108 return -EINTR;
109 if (current->need_resched)
110 break;
111 udelay(5);
114 if (!port->physport->cad->timeout)
115 /* We may be in an interrupt handler, so we can't poll
116 * slowly anyway. */
117 return 1;
119 /* 40ms of slow polling. */
120 deadline = jiffies + (HZ + 24) / 25;
121 while (time_before (jiffies, deadline)) {
122 int ret;
124 if (signal_pending (current))
125 return -EINTR;
127 /* Wait for 10ms (or until an interrupt occurs if
128 * the handler is set) */
129 if ((ret = parport_wait_event (port, (HZ + 99) / 100)) < 0)
130 return ret;
132 status = parport_read_status (port);
133 if ((status & mask) == result)
134 return 0;
136 if (!ret) {
137 /* parport_wait_event didn't time out, but the
138 * peripheral wasn't actually ready either.
139 * Wait for another 10ms. */
140 current->state = TASK_INTERRUPTIBLE;
141 schedule_timeout ((HZ+ 99) / 100);
145 return 1;
148 #ifdef CONFIG_PARPORT_1284
149 /* Terminate a negotiated mode. */
150 static void parport_ieee1284_terminate (struct parport *port)
152 port = port->physport;
154 port->ieee1284.phase = IEEE1284_PH_TERMINATE;
156 /* EPP terminates differently. */
157 switch (port->ieee1284.mode) {
158 case IEEE1284_MODE_EPP:
159 case IEEE1284_MODE_EPPSL:
160 case IEEE1284_MODE_EPPSWE:
161 /* Terminate from EPP mode. */
163 /* Event 68: Set nInit low */
164 parport_frob_control (port,
165 PARPORT_CONTROL_INIT,
166 PARPORT_CONTROL_INIT);
167 udelay (50);
169 /* Event 69: Set nInit high, nSelectIn low */
170 parport_frob_control (port,
171 PARPORT_CONTROL_SELECT,
172 PARPORT_CONTROL_SELECT);
173 break;
175 default:
176 /* Terminate from all other modes. */
178 /* Event 22: Set nSelectIn low, nAutoFd high */
179 parport_frob_control (port,
180 PARPORT_CONTROL_SELECT
181 | PARPORT_CONTROL_AUTOFD,
182 PARPORT_CONTROL_SELECT);
184 /* Event 24: nAck goes low */
185 parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0);
187 /* Event 25: Set nAutoFd low */
188 parport_frob_control (port,
189 PARPORT_CONTROL_AUTOFD,
190 PARPORT_CONTROL_AUTOFD);
192 /* Event 27: nAck goes high */
193 parport_wait_peripheral (port,
194 PARPORT_STATUS_ACK,
195 PARPORT_STATUS_ACK);
197 /* Event 29: Set nAutoFd high */
198 parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
201 port->ieee1284.mode = IEEE1284_MODE_COMPAT;
202 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
204 DPRINTK (KERN_DEBUG "%s: In compatibility (forward idle) mode\n",
205 port->name);
207 #endif /* IEEE1284 support */
209 /* Negotiate an IEEE 1284 mode.
210 * return values are:
211 * 0 - handshake OK; IEEE1284 peripheral and mode available
212 * -1 - handshake failed; peripheral is not compliant (or none present)
213 * 1 - handshake OK; IEEE1284 peripheral present but mode not available
215 int parport_negotiate (struct parport *port, int mode)
217 #ifndef CONFIG_PARPORT_1284
218 if (mode == IEEE1284_MODE_COMPAT)
219 return 0;
220 printk (KERN_ERR "parport: IEEE1284 not supported in this kernel\n");
221 return -1;
222 #else
223 int m = mode;
224 unsigned char xflag;
226 port = port->physport;
228 /* Is there anything to do? */
229 if (port->ieee1284.mode == mode)
230 return 0;
232 /* Go to compability forward idle mode */
233 if (port->ieee1284.mode != IEEE1284_MODE_COMPAT)
234 parport_ieee1284_terminate (port);
236 if (mode == IEEE1284_MODE_COMPAT)
237 /* Compatibility mode: no negotiation. */
238 return 0;
240 switch (mode) {
241 case IEEE1284_MODE_ECPSWE:
242 m = IEEE1284_MODE_ECP;
243 break;
244 case IEEE1284_MODE_EPPSL:
245 case IEEE1284_MODE_EPPSWE:
246 m = IEEE1284_MODE_EPP;
247 break;
248 case IEEE1284_MODE_BECP:
249 return -ENOSYS; /* FIXME (implement BECP) */
252 port->ieee1284.phase = IEEE1284_PH_NEGOTIATION;
254 /* Start off with nStrobe and nAutoFd high, and nSelectIn low */
255 parport_frob_control (port,
256 PARPORT_CONTROL_STROBE
257 | PARPORT_CONTROL_AUTOFD
258 | PARPORT_CONTROL_SELECT,
259 PARPORT_CONTROL_SELECT);
260 udelay(1);
262 /* Event 0: Set data */
263 parport_write_data (port, m);
264 udelay (400); /* Shouldn't need to wait this long. */
266 /* Event 1: Set nSelectIn high, nAutoFd low */
267 parport_frob_control (port,
268 PARPORT_CONTROL_SELECT
269 | PARPORT_CONTROL_AUTOFD,
270 PARPORT_CONTROL_AUTOFD);
272 /* Event 2: PError, Select, nFault go high, nAck goes low */
273 if (parport_wait_peripheral (port,
274 PARPORT_STATUS_ERROR
275 | PARPORT_STATUS_SELECT
276 | PARPORT_STATUS_PAPEROUT
277 | PARPORT_STATUS_ACK,
278 PARPORT_STATUS_ERROR
279 | PARPORT_STATUS_SELECT
280 | PARPORT_STATUS_PAPEROUT)) {
281 /* Timeout */
282 parport_frob_control (port,
283 PARPORT_CONTROL_SELECT
284 | PARPORT_CONTROL_AUTOFD,
285 PARPORT_CONTROL_SELECT);
286 DPRINTK (KERN_DEBUG
287 "%s: Peripheral not IEEE1284 compliant (0x%02X)\n",
288 port->name, parport_read_status (port));
289 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
290 return -1; /* Not IEEE1284 compliant */
293 /* Event 3: Set nStrobe low */
294 parport_frob_control (port,
295 PARPORT_CONTROL_STROBE,
296 PARPORT_CONTROL_STROBE);
298 /* Event 4: Set nStrobe and nAutoFd high */
299 udelay (5);
300 parport_frob_control (port,
301 PARPORT_CONTROL_STROBE
302 | PARPORT_CONTROL_AUTOFD,
305 /* Event 6: nAck goes high */
306 if (parport_wait_peripheral (port,
307 PARPORT_STATUS_ACK
308 | PARPORT_STATUS_PAPEROUT,
309 PARPORT_STATUS_ACK)) {
310 if (parport_read_status (port) & PARPORT_STATUS_ACK)
311 printk (KERN_DEBUG
312 "%s: working around buggy peripheral: tell "
313 "Tim what make it is\n", port->name);
314 DPRINTK (KERN_DEBUG
315 "%s: Mode 0x%02x not supported? (0x%02x)\n",
316 port->name, mode, port->ops->read_status (port));
317 parport_ieee1284_terminate (port);
318 return 1;
321 xflag = parport_read_status (port) & PARPORT_STATUS_SELECT;
323 /* xflag should be high for all modes other than nibble (0). */
324 if (mode && !xflag) {
325 /* Mode not supported. */
326 DPRINTK (KERN_DEBUG "%s: Mode 0x%02x not supported\n",
327 port->name, mode);
328 parport_ieee1284_terminate (port);
329 return 1;
332 /* Mode is supported */
333 DPRINTK (KERN_DEBUG "%s: In mode 0x%02x\n", port->name, mode);
334 port->ieee1284.mode = mode;
336 /* But ECP is special */
337 if (mode & IEEE1284_MODE_ECP) {
338 port->ieee1284.phase = IEEE1284_PH_ECP_SETUP;
340 /* Event 30: Set nAutoFd low */
341 parport_frob_control (port,
342 PARPORT_CONTROL_AUTOFD,
343 PARPORT_CONTROL_AUTOFD);
345 /* Event 31: PError goes high. */
346 parport_wait_peripheral (port,
347 PARPORT_STATUS_PAPEROUT,
348 PARPORT_STATUS_PAPEROUT);
349 /* (Should check that this works..) */
351 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
352 DPRINTK (KERN_DEBUG "%s: ECP direction: forward\n",
353 port->name);
354 } else switch (mode) {
355 case IEEE1284_MODE_NIBBLE:
356 case IEEE1284_MODE_BYTE:
357 port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
358 break;
359 default:
360 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
364 return 0;
365 #endif /* IEEE1284 support */
368 /* Acknowledge that the peripheral has data available.
369 * Events 18-20, in order to get from Reverse Idle phase
370 * to Host Busy Data Available.
371 * This will most likely be called from an interrupt.
372 * Returns zero if data was available.
374 #ifdef CONFIG_PARPORT_1284
375 static int parport_ieee1284_ack_data_avail (struct parport *port)
377 if (parport_read_status (port) & PARPORT_STATUS_ERROR)
378 /* Event 18 didn't happen. */
379 return -1;
381 /* Event 20: nAutoFd goes high. */
382 port->ops->frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
383 port->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
384 return 0;
386 #endif /* IEEE1284 support */
388 /* Handle an interrupt. */
389 void parport_ieee1284_interrupt (int which, void *handle, struct pt_regs *regs)
391 struct parport *port = handle;
392 parport_ieee1284_wakeup (port);
394 #ifdef CONFIG_PARPORT_1284
395 if (port->ieee1284.phase == IEEE1284_PH_REV_IDLE) {
396 /* An interrupt in this phase means that data
397 * is now available. */
398 DPRINTK (KERN_DEBUG "%s: Data available\n", port->name);
399 parport_ieee1284_ack_data_avail (port);
401 #endif /* IEEE1284 support */
404 /* Write a block of data. */
405 ssize_t parport_write (struct parport *port, const void *buffer, size_t len)
407 #ifndef CONFIG_PARPORT_1284
408 return port->ops->compat_write_data (port, buffer, len, 0);
409 #else
410 ssize_t retval;
411 int mode = port->ieee1284.mode;
412 size_t (*fn) (struct parport *, const void *, size_t, int);
414 /* Ignore the device-ID-request bit. */
415 mode &= ~IEEE1284_DEVICEID;
417 /* Use the mode we're in. */
418 switch (mode) {
419 case IEEE1284_MODE_NIBBLE:
420 parport_negotiate (port, IEEE1284_MODE_COMPAT);
421 case IEEE1284_MODE_COMPAT:
422 DPRINTK (KERN_DEBUG "%s: Using compatibility mode\n",
423 port->name);
424 fn = port->ops->compat_write_data;
425 break;
427 case IEEE1284_MODE_EPP:
428 DPRINTK (KERN_DEBUG "%s: Using EPP mode\n", port->name);
429 fn = port->ops->epp_write_data;
430 break;
432 case IEEE1284_MODE_ECP:
433 case IEEE1284_MODE_ECPRLE:
434 DPRINTK (KERN_DEBUG "%s: Using ECP mode\n", port->name);
435 fn = port->ops->ecp_write_data;
436 break;
438 case IEEE1284_MODE_ECPSWE:
439 DPRINTK (KERN_DEBUG "%s: Using software-emulated ECP mode\n",
440 port->name);
441 /* The caller has specified that it must be emulated,
442 * even if we have ECP hardware! */
443 fn = parport_ieee1284_ecp_write_data;
444 break;
446 default:
447 DPRINTK (KERN_DEBUG "%s: Unknown mode 0x%02x\n", port->name,
448 port->ieee1284.mode);
449 return -ENOSYS;
452 retval = (*fn) (port, buffer, len, 0);
453 DPRINTK (KERN_DEBUG "%s: wrote %d/%d bytes\n", port->name, retval,
454 len);
455 return retval;
456 #endif /* IEEE1284 support */
459 /* Read a block of data. */
460 ssize_t parport_read (struct parport *port, void *buffer, size_t len)
462 #ifndef CONFIG_PARPORT_1284
463 printk (KERN_ERR "parport: IEEE1284 not supported in this kernel\n");
464 return -ENODEV;
465 #else
466 int mode = port->physport->ieee1284.mode;
467 size_t (*fn) (struct parport *, void *, size_t, int);
469 /* Ignore the device-ID-request bit. */
470 mode &= ~IEEE1284_DEVICEID;
472 /* Use the mode we're in. */
473 switch (mode) {
474 case IEEE1284_MODE_COMPAT:
475 if (parport_negotiate (port, IEEE1284_MODE_NIBBLE))
476 return -EIO;
477 case IEEE1284_MODE_NIBBLE:
478 DPRINTK (KERN_DEBUG "%s: Using nibble mode\n", port->name);
479 fn = port->ops->nibble_read_data;
480 break;
482 case IEEE1284_MODE_BYTE:
483 DPRINTK (KERN_DEBUG "%s: Using byte mode\n", port->name);
484 fn = port->ops->byte_read_data;
485 break;
487 case IEEE1284_MODE_EPP:
488 DPRINTK (KERN_DEBUG "%s: Using EPP mode\n", port->name);
489 fn = port->ops->epp_read_data;
490 break;
492 case IEEE1284_MODE_ECP:
493 case IEEE1284_MODE_ECPRLE:
494 DPRINTK (KERN_DEBUG "%s: Using ECP mode\n", port->name);
495 fn = port->ops->ecp_read_data;
496 break;
498 case IEEE1284_MODE_ECPSWE:
499 DPRINTK (KERN_DEBUG "%s: Using software-emulated ECP mode\n",
500 port->name);
501 fn = parport_ieee1284_ecp_read_data;
502 break;
504 default:
505 DPRINTK (KERN_DEBUG "%s: Unknown mode 0x%02x\n", port->name,
506 port->physport->ieee1284.mode);
507 return -ENOSYS;
510 return (*fn) (port, buffer, len, 0);
511 #endif /* IEEE1284 support */
514 /* Set the amount of time we wait while nothing's happening. */
515 long parport_set_timeout (struct pardevice *dev, long inactivity)
517 long int old = dev->timeout;
519 dev->timeout = inactivity;
521 if (dev->port->physport->cad == dev)
522 parport_ieee1284_wakeup (dev->port);
524 return old;