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 */
27 #define DPRINTK(stuff...) printk (stuff)
29 #define DPRINTK(stuff...)
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.
47 * <0: error (exit as soon as possible)
50 int parport_wait_event (struct parport
*port
, signed long timeout
)
53 struct timer_list timer
;
55 if (!port
->physport
->cad
->timeout
)
56 /* Zero timeout is special, and we can't down() the
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
;
67 ret
= down_interruptible (&port
->physport
->ieee1284
.irq
);
68 if (!del_timer (&timer
) && !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.
83 int parport_wait_peripheral(struct parport
*port
,
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
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
)
107 if (signal_pending (current
))
109 if (current
->need_resched
)
114 if (!port
->physport
->cad
->timeout
)
115 /* We may be in an interrupt handler, so we can't poll
119 /* 40ms of slow polling. */
120 deadline
= jiffies
+ (HZ
+ 24) / 25;
121 while (time_before (jiffies
, deadline
)) {
124 if (signal_pending (current
))
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)
132 status
= parport_read_status (port
);
133 if ((status
& mask
) == result
)
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);
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
);
169 /* Event 69: Set nInit high, nSelectIn low */
170 parport_frob_control (port
,
171 PARPORT_CONTROL_SELECT
,
172 PARPORT_CONTROL_SELECT
);
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
,
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",
207 #endif /* IEEE1284 support */
209 /* Negotiate an IEEE 1284 mode.
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
)
220 printk (KERN_ERR
"parport: IEEE1284 not supported in this kernel\n");
226 port
= port
->physport
;
228 /* Is there anything to do? */
229 if (port
->ieee1284
.mode
== mode
)
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. */
241 case IEEE1284_MODE_ECPSWE
:
242 m
= IEEE1284_MODE_ECP
;
244 case IEEE1284_MODE_EPPSL
:
245 case IEEE1284_MODE_EPPSWE
:
246 m
= IEEE1284_MODE_EPP
;
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
);
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
,
275 | PARPORT_STATUS_SELECT
276 | PARPORT_STATUS_PAPEROUT
277 | PARPORT_STATUS_ACK
,
279 | PARPORT_STATUS_SELECT
280 | PARPORT_STATUS_PAPEROUT
)) {
282 parport_frob_control (port
,
283 PARPORT_CONTROL_SELECT
284 | PARPORT_CONTROL_AUTOFD
,
285 PARPORT_CONTROL_SELECT
);
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 */
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
,
308 | PARPORT_STATUS_PAPEROUT
,
309 PARPORT_STATUS_ACK
)) {
310 if (parport_read_status (port
) & PARPORT_STATUS_ACK
)
312 "%s: working around buggy peripheral: tell "
313 "Tim what make it is\n", port
->name
);
315 "%s: Mode 0x%02x not supported? (0x%02x)\n",
316 port
->name
, mode
, port
->ops
->read_status (port
));
317 parport_ieee1284_terminate (port
);
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",
328 parport_ieee1284_terminate (port
);
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",
354 } else switch (mode
) {
355 case IEEE1284_MODE_NIBBLE
:
356 case IEEE1284_MODE_BYTE
:
357 port
->ieee1284
.phase
= IEEE1284_PH_REV_IDLE
;
360 port
->ieee1284
.phase
= IEEE1284_PH_FWD_IDLE
;
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. */
381 /* Event 20: nAutoFd goes high. */
382 port
->ops
->frob_control (port
, PARPORT_CONTROL_AUTOFD
, 0);
383 port
->ieee1284
.phase
= IEEE1284_PH_HBUSY_DAVAIL
;
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);
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. */
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",
424 fn
= port
->ops
->compat_write_data
;
427 case IEEE1284_MODE_EPP
:
428 DPRINTK (KERN_DEBUG
"%s: Using EPP mode\n", port
->name
);
429 fn
= port
->ops
->epp_write_data
;
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
;
438 case IEEE1284_MODE_ECPSWE
:
439 DPRINTK (KERN_DEBUG
"%s: Using software-emulated ECP mode\n",
441 /* The caller has specified that it must be emulated,
442 * even if we have ECP hardware! */
443 fn
= parport_ieee1284_ecp_write_data
;
447 DPRINTK (KERN_DEBUG
"%s: Unknown mode 0x%02x\n", port
->name
,
448 port
->ieee1284
.mode
);
452 retval
= (*fn
) (port
, buffer
, len
, 0);
453 DPRINTK (KERN_DEBUG
"%s: wrote %d/%d bytes\n", port
->name
, 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");
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. */
474 case IEEE1284_MODE_COMPAT
:
475 if (parport_negotiate (port
, IEEE1284_MODE_NIBBLE
))
477 case IEEE1284_MODE_NIBBLE
:
478 DPRINTK (KERN_DEBUG
"%s: Using nibble mode\n", port
->name
);
479 fn
= port
->ops
->nibble_read_data
;
482 case IEEE1284_MODE_BYTE
:
483 DPRINTK (KERN_DEBUG
"%s: Using byte mode\n", port
->name
);
484 fn
= port
->ops
->byte_read_data
;
487 case IEEE1284_MODE_EPP
:
488 DPRINTK (KERN_DEBUG
"%s: Using EPP mode\n", port
->name
);
489 fn
= port
->ops
->epp_read_data
;
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
;
498 case IEEE1284_MODE_ECPSWE
:
499 DPRINTK (KERN_DEBUG
"%s: Using software-emulated ECP mode\n",
501 fn
= parport_ieee1284_ecp_read_data
;
505 DPRINTK (KERN_DEBUG
"%s: Unknown mode 0x%02x\n", port
->name
,
506 port
->physport
->ieee1284
.mode
);
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
);