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");
223 int m
= mode
& ~IEEE1284_ADDR
;
226 port
= port
->physport
;
228 /* Is there anything to do? */
229 if (port
->ieee1284
.mode
== mode
)
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
;
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. */
247 case IEEE1284_MODE_ECPSWE
:
248 m
= IEEE1284_MODE_ECP
;
250 case IEEE1284_MODE_EPPSL
:
251 case IEEE1284_MODE_EPPSWE
:
252 m
= IEEE1284_MODE_EPP
;
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
);
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
,
281 | PARPORT_STATUS_SELECT
282 | PARPORT_STATUS_PAPEROUT
283 | PARPORT_STATUS_ACK
,
285 | PARPORT_STATUS_SELECT
286 | PARPORT_STATUS_PAPEROUT
)) {
288 parport_frob_control (port
,
289 PARPORT_CONTROL_SELECT
290 | PARPORT_CONTROL_AUTOFD
,
291 PARPORT_CONTROL_SELECT
);
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 */
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
,
314 | PARPORT_STATUS_PAPEROUT
,
315 PARPORT_STATUS_ACK
)) {
316 if (parport_read_status (port
) & PARPORT_STATUS_ACK
)
318 "%s: working around buggy peripheral: tell "
319 "Tim what make it is\n", port
->name
);
321 "%s: Mode 0x%02x not supported? (0x%02x)\n",
322 port
->name
, mode
, port
->ops
->read_status (port
));
323 parport_ieee1284_terminate (port
);
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",
334 parport_ieee1284_terminate (port
);
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",
360 } else switch (mode
) {
361 case IEEE1284_MODE_NIBBLE
:
362 case IEEE1284_MODE_BYTE
:
363 port
->ieee1284
.phase
= IEEE1284_PH_REV_IDLE
;
366 port
->ieee1284
.phase
= IEEE1284_PH_FWD_IDLE
;
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. */
387 /* Event 20: nAutoFd goes high. */
388 port
->ops
->frob_control (port
, PARPORT_CONTROL_AUTOFD
, 0);
389 port
->ieee1284
.phase
= IEEE1284_PH_HBUSY_DAVAIL
;
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);
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. */
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",
431 fn
= port
->ops
->compat_write_data
;
434 case IEEE1284_MODE_EPP
:
435 DPRINTK (KERN_DEBUG
"%s: Using EPP mode\n", port
->name
);
437 fn
= port
->ops
->epp_write_addr
;
439 fn
= port
->ops
->epp_write_data
;
442 case IEEE1284_MODE_ECP
:
443 case IEEE1284_MODE_ECPRLE
:
444 DPRINTK (KERN_DEBUG
"%s: Using ECP mode\n", port
->name
);
446 fn
= port
->ops
->ecp_write_addr
;
448 fn
= port
->ops
->ecp_write_data
;
451 case IEEE1284_MODE_ECPSWE
:
452 DPRINTK (KERN_DEBUG
"%s: Using software-emulated ECP mode\n",
454 /* The caller has specified that it must be emulated,
455 * even if we have ECP hardware! */
457 fn
= parport_ieee1284_ecp_write_addr
;
459 fn
= parport_ieee1284_ecp_write_data
;
463 DPRINTK (KERN_DEBUG
"%s: Unknown mode 0x%02x\n", port
->name
,
464 port
->ieee1284
.mode
);
468 retval
= (*fn
) (port
, buffer
, len
, 0);
469 DPRINTK (KERN_DEBUG
"%s: wrote %d/%d bytes\n", port
->name
, 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");
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. */
491 case IEEE1284_MODE_COMPAT
:
492 if (parport_negotiate (port
, IEEE1284_MODE_NIBBLE
))
494 case IEEE1284_MODE_NIBBLE
:
495 DPRINTK (KERN_DEBUG
"%s: Using nibble mode\n", port
->name
);
496 fn
= port
->ops
->nibble_read_data
;
499 case IEEE1284_MODE_BYTE
:
500 DPRINTK (KERN_DEBUG
"%s: Using byte mode\n", port
->name
);
501 fn
= port
->ops
->byte_read_data
;
504 case IEEE1284_MODE_EPP
:
505 DPRINTK (KERN_DEBUG
"%s: Using EPP mode\n", port
->name
);
507 fn
= port
->ops
->epp_read_addr
;
509 fn
= port
->ops
->epp_read_data
;
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
;
518 case IEEE1284_MODE_ECPSWE
:
519 DPRINTK (KERN_DEBUG
"%s: Using software-emulated ECP mode\n",
521 fn
= parport_ieee1284_ecp_read_data
;
525 DPRINTK (KERN_DEBUG
"%s: Unknown mode 0x%02x\n", port
->name
,
526 port
->physport
->ieee1284
.mode
);
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
);