Import 2.3.11
[davej-history.git] / drivers / misc / parport_ieee1284.c
blobde7a269c236a689d3fd464370aa5d17f6bf6d0f0
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/threads.h>
15 #include <linux/parport.h>
16 #include <linux/delay.h>
17 #include <linux/kernel.h>
18 #include <linux/interrupt.h>
20 #undef 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 & ~IEEE1284_ADDR;
224 unsigned char xflag;
226 port = port->physport;
228 /* Is there anything to do? */
229 if (port->ieee1284.mode == mode)
230 return 0;
232 /* Is the difference just an address-or-not bit? */
233 if ((port->ieee1284.mode & ~IEEE1284_ADDR) == (mode & ~IEEE1284_ADDR)){
234 port->ieee1284.mode = mode;
235 return 0;
238 /* Go to compability forward idle mode */
239 if (port->ieee1284.mode != IEEE1284_MODE_COMPAT)
240 parport_ieee1284_terminate (port);
242 if (mode == IEEE1284_MODE_COMPAT)
243 /* Compatibility mode: no negotiation. */
244 return 0;
246 switch (mode) {
247 case IEEE1284_MODE_ECPSWE:
248 m = IEEE1284_MODE_ECP;
249 break;
250 case IEEE1284_MODE_EPPSL:
251 case IEEE1284_MODE_EPPSWE:
252 m = IEEE1284_MODE_EPP;
253 break;
254 case IEEE1284_MODE_BECP:
255 return -ENOSYS; /* FIXME (implement BECP) */
258 port->ieee1284.phase = IEEE1284_PH_NEGOTIATION;
260 /* Start off with nStrobe and nAutoFd high, and nSelectIn low */
261 parport_frob_control (port,
262 PARPORT_CONTROL_STROBE
263 | PARPORT_CONTROL_AUTOFD
264 | PARPORT_CONTROL_SELECT,
265 PARPORT_CONTROL_SELECT);
266 udelay(1);
268 /* Event 0: Set data */
269 parport_write_data (port, m);
270 udelay (400); /* Shouldn't need to wait this long. */
272 /* Event 1: Set nSelectIn high, nAutoFd low */
273 parport_frob_control (port,
274 PARPORT_CONTROL_SELECT
275 | PARPORT_CONTROL_AUTOFD,
276 PARPORT_CONTROL_AUTOFD);
278 /* Event 2: PError, Select, nFault go high, nAck goes low */
279 if (parport_wait_peripheral (port,
280 PARPORT_STATUS_ERROR
281 | PARPORT_STATUS_SELECT
282 | PARPORT_STATUS_PAPEROUT
283 | PARPORT_STATUS_ACK,
284 PARPORT_STATUS_ERROR
285 | PARPORT_STATUS_SELECT
286 | PARPORT_STATUS_PAPEROUT)) {
287 /* Timeout */
288 parport_frob_control (port,
289 PARPORT_CONTROL_SELECT
290 | PARPORT_CONTROL_AUTOFD,
291 PARPORT_CONTROL_SELECT);
292 DPRINTK (KERN_DEBUG
293 "%s: Peripheral not IEEE1284 compliant (0x%02X)\n",
294 port->name, parport_read_status (port));
295 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
296 return -1; /* Not IEEE1284 compliant */
299 /* Event 3: Set nStrobe low */
300 parport_frob_control (port,
301 PARPORT_CONTROL_STROBE,
302 PARPORT_CONTROL_STROBE);
304 /* Event 4: Set nStrobe and nAutoFd high */
305 udelay (5);
306 parport_frob_control (port,
307 PARPORT_CONTROL_STROBE
308 | PARPORT_CONTROL_AUTOFD,
311 /* Event 6: nAck goes high */
312 if (parport_wait_peripheral (port,
313 PARPORT_STATUS_ACK
314 | PARPORT_STATUS_PAPEROUT,
315 PARPORT_STATUS_ACK)) {
316 if (parport_read_status (port) & PARPORT_STATUS_ACK)
317 printk (KERN_DEBUG
318 "%s: working around buggy peripheral: tell "
319 "Tim what make it is\n", port->name);
320 DPRINTK (KERN_DEBUG
321 "%s: Mode 0x%02x not supported? (0x%02x)\n",
322 port->name, mode, port->ops->read_status (port));
323 parport_ieee1284_terminate (port);
324 return 1;
327 xflag = parport_read_status (port) & PARPORT_STATUS_SELECT;
329 /* xflag should be high for all modes other than nibble (0). */
330 if (mode && !xflag) {
331 /* Mode not supported. */
332 DPRINTK (KERN_DEBUG "%s: Mode 0x%02x not supported\n",
333 port->name, mode);
334 parport_ieee1284_terminate (port);
335 return 1;
338 /* Mode is supported */
339 DPRINTK (KERN_DEBUG "%s: In mode 0x%02x\n", port->name, mode);
340 port->ieee1284.mode = mode;
342 /* But ECP is special */
343 if (mode & IEEE1284_MODE_ECP) {
344 port->ieee1284.phase = IEEE1284_PH_ECP_SETUP;
346 /* Event 30: Set nAutoFd low */
347 parport_frob_control (port,
348 PARPORT_CONTROL_AUTOFD,
349 PARPORT_CONTROL_AUTOFD);
351 /* Event 31: PError goes high. */
352 parport_wait_peripheral (port,
353 PARPORT_STATUS_PAPEROUT,
354 PARPORT_STATUS_PAPEROUT);
355 /* (Should check that this works..) */
357 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
358 DPRINTK (KERN_DEBUG "%s: ECP direction: forward\n",
359 port->name);
360 } else switch (mode) {
361 case IEEE1284_MODE_NIBBLE:
362 case IEEE1284_MODE_BYTE:
363 port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
364 break;
365 default:
366 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
370 return 0;
371 #endif /* IEEE1284 support */
374 /* Acknowledge that the peripheral has data available.
375 * Events 18-20, in order to get from Reverse Idle phase
376 * to Host Busy Data Available.
377 * This will most likely be called from an interrupt.
378 * Returns zero if data was available.
380 #ifdef CONFIG_PARPORT_1284
381 static int parport_ieee1284_ack_data_avail (struct parport *port)
383 if (parport_read_status (port) & PARPORT_STATUS_ERROR)
384 /* Event 18 didn't happen. */
385 return -1;
387 /* Event 20: nAutoFd goes high. */
388 port->ops->frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
389 port->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
390 return 0;
392 #endif /* IEEE1284 support */
394 /* Handle an interrupt. */
395 void parport_ieee1284_interrupt (int which, void *handle, struct pt_regs *regs)
397 struct parport *port = handle;
398 parport_ieee1284_wakeup (port);
400 #ifdef CONFIG_PARPORT_1284
401 if (port->ieee1284.phase == IEEE1284_PH_REV_IDLE) {
402 /* An interrupt in this phase means that data
403 * is now available. */
404 DPRINTK (KERN_DEBUG "%s: Data available\n", port->name);
405 parport_ieee1284_ack_data_avail (port);
407 #endif /* IEEE1284 support */
410 /* Write a block of data. */
411 ssize_t parport_write (struct parport *port, const void *buffer, size_t len)
413 #ifndef CONFIG_PARPORT_1284
414 return port->ops->compat_write_data (port, buffer, len, 0);
415 #else
416 ssize_t retval;
417 int mode = port->ieee1284.mode;
418 int addr = mode & IEEE1284_ADDR;
419 size_t (*fn) (struct parport *, const void *, size_t, int);
421 /* Ignore the device-ID-request bit and the address bit. */
422 mode &= ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
424 /* Use the mode we're in. */
425 switch (mode) {
426 case IEEE1284_MODE_NIBBLE:
427 parport_negotiate (port, IEEE1284_MODE_COMPAT);
428 case IEEE1284_MODE_COMPAT:
429 DPRINTK (KERN_DEBUG "%s: Using compatibility mode\n",
430 port->name);
431 fn = port->ops->compat_write_data;
432 break;
434 case IEEE1284_MODE_EPP:
435 DPRINTK (KERN_DEBUG "%s: Using EPP mode\n", port->name);
436 if (addr)
437 fn = port->ops->epp_write_addr;
438 else
439 fn = port->ops->epp_write_data;
440 break;
442 case IEEE1284_MODE_ECP:
443 case IEEE1284_MODE_ECPRLE:
444 DPRINTK (KERN_DEBUG "%s: Using ECP mode\n", port->name);
445 if (addr)
446 fn = port->ops->ecp_write_addr;
447 else
448 fn = port->ops->ecp_write_data;
449 break;
451 case IEEE1284_MODE_ECPSWE:
452 DPRINTK (KERN_DEBUG "%s: Using software-emulated ECP mode\n",
453 port->name);
454 /* The caller has specified that it must be emulated,
455 * even if we have ECP hardware! */
456 if (addr)
457 fn = parport_ieee1284_ecp_write_addr;
458 else
459 fn = parport_ieee1284_ecp_write_data;
460 break;
462 default:
463 DPRINTK (KERN_DEBUG "%s: Unknown mode 0x%02x\n", port->name,
464 port->ieee1284.mode);
465 return -ENOSYS;
468 retval = (*fn) (port, buffer, len, 0);
469 DPRINTK (KERN_DEBUG "%s: wrote %d/%d bytes\n", port->name, retval,
470 len);
471 return retval;
472 #endif /* IEEE1284 support */
475 /* Read a block of data. */
476 ssize_t parport_read (struct parport *port, void *buffer, size_t len)
478 #ifndef CONFIG_PARPORT_1284
479 printk (KERN_ERR "parport: IEEE1284 not supported in this kernel\n");
480 return -ENODEV;
481 #else
482 int mode = port->physport->ieee1284.mode;
483 int addr = mode & IEEE1284_ADDR;
484 size_t (*fn) (struct parport *, void *, size_t, int);
486 /* Ignore the device-ID-request bit and the address bit. */
487 mode &= ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
489 /* Use the mode we're in. */
490 switch (mode) {
491 case IEEE1284_MODE_COMPAT:
492 if (parport_negotiate (port, IEEE1284_MODE_NIBBLE))
493 return -EIO;
494 case IEEE1284_MODE_NIBBLE:
495 DPRINTK (KERN_DEBUG "%s: Using nibble mode\n", port->name);
496 fn = port->ops->nibble_read_data;
497 break;
499 case IEEE1284_MODE_BYTE:
500 DPRINTK (KERN_DEBUG "%s: Using byte mode\n", port->name);
501 fn = port->ops->byte_read_data;
502 break;
504 case IEEE1284_MODE_EPP:
505 DPRINTK (KERN_DEBUG "%s: Using EPP mode\n", port->name);
506 if (addr)
507 fn = port->ops->epp_read_addr;
508 else
509 fn = port->ops->epp_read_data;
510 break;
512 case IEEE1284_MODE_ECP:
513 case IEEE1284_MODE_ECPRLE:
514 DPRINTK (KERN_DEBUG "%s: Using ECP mode\n", port->name);
515 fn = port->ops->ecp_read_data;
516 break;
518 case IEEE1284_MODE_ECPSWE:
519 DPRINTK (KERN_DEBUG "%s: Using software-emulated ECP mode\n",
520 port->name);
521 fn = parport_ieee1284_ecp_read_data;
522 break;
524 default:
525 DPRINTK (KERN_DEBUG "%s: Unknown mode 0x%02x\n", port->name,
526 port->physport->ieee1284.mode);
527 return -ENOSYS;
530 return (*fn) (port, buffer, len, 0);
531 #endif /* IEEE1284 support */
534 /* Set the amount of time we wait while nothing's happening. */
535 long parport_set_timeout (struct pardevice *dev, long inactivity)
537 long int old = dev->timeout;
539 dev->timeout = inactivity;
541 if (dev->port->physport->cad == dev)
542 parport_ieee1284_wakeup (dev->port);
544 return old;