timeout/untimeout ==> callout_*
[dragonfly.git] / sys / dev / misc / lpt / lpt.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1990 William F. Jolitz, TeleMuse
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This software is a component of "386BSD" developed by
16 * William F. Jolitz, TeleMuse.
17 * 4. Neither the name of the developer nor the name "386BSD"
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
22 * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
23 * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
24 * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
25 * NOT MAKE USE OF THIS WORK.
26 *
27 * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
28 * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
29 * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES
30 * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
31 * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
32 * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
33 * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
34 * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 *
48 * from: unknown origin, 386BSD 0.1
49 * From Id: lpt.c,v 1.55.2.1 1996/11/12 09:08:38 phk Exp
50 * From Id: nlpt.c,v 1.14 1999/02/08 13:55:43 des Exp
51 * $FreeBSD: src/sys/dev/ppbus/lpt.c,v 1.15.2.3 2000/07/07 00:30:40 obrien Exp $
6aed8a5b 52 * $DragonFly: src/sys/dev/misc/lpt/lpt.c,v 1.10 2004/09/19 00:23:33 dillon Exp $
984263bc
MD
53 */
54
55/*
56 * Device Driver for AT parallel printer port
57 * Written by William Jolitz 12/18/90
58 */
59
60/*
61 * Updated for ppbus by Nicolas Souchu
62 * [Mon Jul 28 1997]
63 */
64
65#include "opt_lpt.h"
66
67#include <sys/param.h>
68#include <sys/systm.h>
69#include <sys/module.h>
70#include <sys/bus.h>
71#include <sys/conf.h>
72#include <sys/kernel.h>
73#include <sys/uio.h>
74#include <sys/syslog.h>
75#include <sys/malloc.h>
76
77#include <machine/clock.h>
78#include <machine/bus.h>
79#include <machine/resource.h>
80#include <sys/rman.h>
81
1f2de5d4
MD
82#include "lptio.h"
83#include <bus/ppbus/ppbconf.h>
84#include <bus/ppbus/ppb_1284.h>
85#include "lpt.h"
984263bc 86#include "ppbus_if.h"
1f2de5d4 87#include <bus/ppbus/ppbio.h>
984263bc
MD
88
89MALLOC_DEFINE(M_LPT, "lpt", "LPT buffers");
90
91#ifndef LPT_DEBUG
92#define lprintf(args)
93#else
94#define lprintf(args) \
95 do { \
96 if (lptflag) \
97 printf args; \
98 } while (0)
99static int volatile lptflag = 1;
100#endif
101
102#define LPINITRDY 4 /* wait up to 4 seconds for a ready */
103#define LPTOUTINITIAL 10 /* initial timeout to wait for ready 1/10 s */
104#define LPTOUTMAX 1 /* maximal timeout 1 s */
984263bc
MD
105#define BUFSIZE 1024
106#define BUFSTATSIZE 32
107
108#define LPTUNIT(s) ((s)&0x03)
109#define LPTFLAGS(s) ((s)&0xfc)
110
111struct lpt_data {
984263bc
MD
112 short sc_state;
113 /* default case: negative prime, negative ack, handshake strobe,
114 prime once */
115 u_char sc_control;
116 char sc_flags;
e4c9c0c8 117#define LP_UNITMASK 0x03 /* up to 4 units */
984263bc
MD
118#define LP_POS_INIT 0x04 /* if we are a postive init signal */
119#define LP_POS_ACK 0x08 /* if we are a positive going ack */
120#define LP_NO_PRIME 0x10 /* don't prime the printer at all */
121#define LP_PRIMEOPEN 0x20 /* prime on every open */
122#define LP_AUTOLF 0x40 /* tell printer to do an automatic lf */
123#define LP_BYPASS 0x80 /* bypass printer ready checks */
124 void *sc_inbuf;
125 void *sc_statbuf;
126 short sc_xfercnt ;
127 char sc_primed;
128 char *sc_cp ;
129 u_short sc_irq ; /* IRQ status of port */
130#define LP_HAS_IRQ 0x01 /* we have an irq available */
131#define LP_USE_IRQ 0x02 /* we are using our irq */
132#define LP_ENABLE_IRQ 0x04 /* enable IRQ on open */
133#define LP_ENABLE_EXT 0x10 /* we shall use advanced mode when possible */
134 u_char sc_backoff ; /* time to call lptout() again */
135
136 struct resource *intr_resource; /* interrupt resource */
137 void *intr_cookie; /* interrupt registration cookie */
6aed8a5b 138 struct callout sc_callout;
984263bc
MD
139};
140
141#define LPT_NAME "lpt" /* our official name */
142
143static timeout_t lptout;
144static int lpt_port_test(device_t dev, u_char data, u_char mask);
145static int lpt_detect(device_t dev);
146
147#define DEVTOSOFTC(dev) \
148 ((struct lpt_data *)device_get_softc(dev))
149#define UNITOSOFTC(unit) \
150 ((struct lpt_data *)devclass_get_softc(lpt_devclass, (unit)))
151#define UNITODEVICE(unit) \
152 (devclass_get_device(lpt_devclass, (unit)))
153
154static void lptintr(device_t dev);
155static void lpt_intr(void *arg); /* without spls */
156
157static devclass_t lpt_devclass;
158
159
160/* bits for state */
161#define OPEN (1<<0) /* device is open */
162#define ASLP (1<<1) /* awaiting draining of printer */
163#define EERROR (1<<2) /* error was received from printer */
164#define OBUSY (1<<3) /* printer is busy doing output */
165#define LPTOUT (1<<4) /* timeout while not selected */
166#define TOUT (1<<5) /* timeout while not selected */
167#define LPTINIT (1<<6) /* waiting to initialize for open */
168#define INTERRUPTED (1<<7) /* write call was interrupted */
169
170#define HAVEBUS (1<<8) /* the driver owns the bus */
171
172
173/* status masks to interrogate printer status */
174#define RDY_MASK (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR) /* ready ? */
175#define LP_READY (LPS_SEL|LPS_NBSY|LPS_NERR)
176
177/* Printer Ready condition - from lpa.c */
178/* Only used in polling code */
179#define LPS_INVERT (LPS_NBSY | LPS_NACK | LPS_SEL | LPS_NERR)
180#define LPS_MASK (LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR)
181#define NOT_READY(ppbus) ((ppb_rstr(ppbus)^LPS_INVERT)&LPS_MASK)
182
183#define MAX_SLEEP (hz*5) /* Timeout while waiting for device ready */
184#define MAX_SPIN 20 /* Max delay for device ready in usecs */
185
186
187static d_open_t lptopen;
188static d_close_t lptclose;
189static d_write_t lptwrite;
190static d_read_t lptread;
191static d_ioctl_t lptioctl;
192
193#define CDEV_MAJOR 16
194static struct cdevsw lpt_cdevsw = {
fabb8ceb
MD
195 /* name */ LPT_NAME,
196 /* maj */ CDEV_MAJOR,
197 /* flags */ 0,
198 /* port */ NULL,
455fcd7e 199 /* clone */ NULL,
fabb8ceb 200
984263bc
MD
201 /* open */ lptopen,
202 /* close */ lptclose,
203 /* read */ lptread,
204 /* write */ lptwrite,
205 /* ioctl */ lptioctl,
206 /* poll */ nopoll,
207 /* mmap */ nommap,
208 /* strategy */ nostrategy,
984263bc 209 /* dump */ nodump,
fabb8ceb 210 /* psize */ nopsize
984263bc
MD
211};
212
213static int
214lpt_request_ppbus(device_t dev, int how)
215{
216 device_t ppbus = device_get_parent(dev);
217 struct lpt_data *sc = DEVTOSOFTC(dev);
218 int error;
219
220 if (sc->sc_state & HAVEBUS)
221 return (0);
222
223 /* we have the bus only if the request succeded */
224 if ((error = ppb_request_bus(ppbus, dev, how)) == 0)
225 sc->sc_state |= HAVEBUS;
226
227 return (error);
228}
229
230static int
231lpt_release_ppbus(device_t dev)
232{
233 device_t ppbus = device_get_parent(dev);
234 struct lpt_data *sc = DEVTOSOFTC(dev);
235 int error = 0;
236
237 if ((error = ppb_release_bus(ppbus, dev)) == 0)
238 sc->sc_state &= ~HAVEBUS;
239
240 return (error);
241}
242
243/*
244 * Internal routine to lptprobe to do port tests of one byte value
245 */
246static int
247lpt_port_test(device_t ppbus, u_char data, u_char mask)
248{
249 int temp, timeout;
250
251 data = data & mask;
252 ppb_wdtr(ppbus, data);
253 timeout = 10000;
254 do {
255 DELAY(10);
256 temp = ppb_rdtr(ppbus) & mask;
257 }
258 while (temp != data && --timeout);
259 lprintf(("out=%x\tin=%x\ttout=%d\n", data, temp, timeout));
260 return (temp == data);
261}
262
263/*
264 * Probe simplified by replacing multiple loops with a hardcoded
265 * test pattern - 1999/02/08 des@freebsd.org
266 *
267 * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94
268 * Based partially on Rod Grimes' printer probe
269 *
270 * Logic:
271 * 1) If no port address was given, use the bios detected ports
272 * and autodetect what ports the printers are on.
273 * 2) Otherwise, probe the data port at the address given,
274 * using the method in Rod Grimes' port probe.
275 * (Much code ripped off directly from Rod's probe.)
276 *
277 * Comments from Rod's probe:
278 * Logic:
279 * 1) You should be able to write to and read back the same value
280 * to the data port. Do an alternating zeros, alternating ones,
281 * walking zero, and walking one test to check for stuck bits.
282 *
283 * 2) You should be able to write to and read back the same value
284 * to the control port lower 5 bits, the upper 3 bits are reserved
285 * per the IBM PC technical reference manauls and different boards
286 * do different things with them. Do an alternating zeros, alternating
287 * ones, walking zero, and walking one test to check for stuck bits.
288 *
289 * Some printers drag the strobe line down when the are powered off
290 * so this bit has been masked out of the control port test.
291 *
292 * XXX Some printers may not like a fast pulse on init or strobe, I
293 * don't know at this point, if that becomes a problem these bits
294 * should be turned off in the mask byte for the control port test.
295 *
296 * We are finally left with a mask of 0x14, due to some printers
297 * being adamant about holding other bits high ........
298 *
299 * Before probing the control port, we write a 0 to the data port -
300 * If not, some printers chuck out garbage when the strobe line
301 * gets toggled.
302 *
303 * 3) Set the data and control ports to a value of 0
304 *
305 * This probe routine has been tested on Epson Lx-800, HP LJ3P,
306 * Epson FX-1170 and C.Itoh 8510RM
307 * printers.
308 * Quick exit on fail added.
309 */
310static int
311lpt_detect(device_t dev)
312{
313 device_t ppbus = device_get_parent(dev);
314
315 static u_char testbyte[18] = {
316 0x55, /* alternating zeros */
317 0xaa, /* alternating ones */
318 0xfe, 0xfd, 0xfb, 0xf7,
319 0xef, 0xdf, 0xbf, 0x7f, /* walking zero */
320 0x01, 0x02, 0x04, 0x08,
321 0x10, 0x20, 0x40, 0x80 /* walking one */
322 };
323 int i, error, status;
324
325 status = 1; /* assume success */
326
327 if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
328 printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
329 status = 0;
330 goto end_probe;
331 }
332
333 for (i = 0; i < 18 && status; i++)
334 if (!lpt_port_test(ppbus, testbyte[i], 0xff)) {
335 status = 0;
336 goto end_probe;
337 }
338
339end_probe:
340 /* write 0's to control and data ports */
341 ppb_wdtr(ppbus, 0);
342 ppb_wctr(ppbus, 0);
343
344 lpt_release_ppbus(dev);
345
346 return (status);
347}
348
349static void
350lpt_identify(driver_t *driver, device_t parent)
351{
352
353 BUS_ADD_CHILD(parent, 0, LPT_NAME, 0);
354}
355
356/*
357 * lpt_probe()
358 */
359static int
360lpt_probe(device_t dev)
361{
362 struct lpt_data *sc;
363
364 sc = DEVTOSOFTC(dev);
365 bzero(sc, sizeof(struct lpt_data));
366
367 /*
368 * Now, try to detect the printer.
369 */
370 if (!lpt_detect(dev))
371 return (ENXIO);
372
373 device_set_desc(dev, "Printer");
374
375 return (0);
376}
377
378static int
379lpt_attach(device_t dev)
380{
381 device_t ppbus = device_get_parent(dev);
382 struct lpt_data *sc = DEVTOSOFTC(dev);
383 int zero = 0, unit = device_get_unit(dev);
384 int error;
385 uintptr_t irq;
386
387 sc->sc_primed = 0; /* not primed yet */
6aed8a5b 388 callout_init(&sc->sc_callout);
984263bc
MD
389
390 if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
391 printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
392 return (0);
393 }
394
395 ppb_wctr(ppbus, LPC_NINIT);
396
397 /* check if we can use interrupt, should be done by ppc stuff */
398 lprintf(("oldirq %x\n", sc->sc_irq));
399
400 /* retrieve the ppbus irq */
401 BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq);
402
403 if (irq > 0) {
404 /* declare our interrupt handler */
405 sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
406 &zero, irq, irq, 1, RF_SHAREABLE);
407 }
408 if (sc->intr_resource) {
409 sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ;
410 device_printf(dev, "Interrupt-driven port\n");
411 } else {
412 sc->sc_irq = 0;
413 device_printf(dev, "Polled port\n");
414 }
415 lprintf(("irq %x %x\n", irq, sc->sc_irq));
416
417 lpt_release_ppbus(dev);
418
e4c9c0c8 419 cdevsw_add(&lpt_cdevsw, LP_UNITMASK, unit);
984263bc
MD
420 make_dev(&lpt_cdevsw, unit,
421 UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d", unit);
422 make_dev(&lpt_cdevsw, unit | LP_BYPASS,
423 UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d.ctl", unit);
424 return (0);
425}
426
427static void
428lptout(void *arg)
429{
430 device_t dev = (device_t)arg;
431 struct lpt_data *sc = DEVTOSOFTC(dev);
432#ifdef LPT_DEBUG
433 device_t ppbus = device_get_parent(dev);
434#endif
435
436 lprintf(("T %x ", ppb_rstr(ppbus)));
437 if (sc->sc_state & OPEN) {
438 sc->sc_backoff++;
439 if (sc->sc_backoff > hz/LPTOUTMAX)
440 sc->sc_backoff = sc->sc_backoff > hz/LPTOUTMAX;
6aed8a5b
MD
441 callout_reset(&sc->sc_callout, sc->sc_backoff, lptout, dev);
442 } else {
984263bc 443 sc->sc_state &= ~TOUT;
6aed8a5b 444 }
984263bc
MD
445
446 if (sc->sc_state & EERROR)
447 sc->sc_state &= ~EERROR;
448
449 /*
450 * Avoid possible hangs due to missed interrupts
451 */
452 if (sc->sc_xfercnt) {
453 lptintr(dev);
454 } else {
455 sc->sc_state &= ~OBUSY;
456 wakeup((caddr_t)dev);
457 }
458}
459
460/*
461 * lptopen -- reset the printer, then wait until it's selected and not busy.
462 * If LP_BYPASS flag is selected, then we do not try to select the
463 * printer -- this is just used for passing ioctls.
464 */
465
466static int
41c20dac 467lptopen(dev_t dev, int flags, int fmt, struct thread *p)
984263bc
MD
468{
469 int s;
470 int trys, err;
471 u_int unit = LPTUNIT(minor(dev));
472 struct lpt_data *sc = UNITOSOFTC(unit);
473 device_t lptdev = UNITODEVICE(unit);
474 device_t ppbus = device_get_parent(lptdev);
475
476 if (!sc)
477 return (ENXIO);
478
479 if (sc->sc_state) {
480 lprintf((LPT_NAME ": still open %x\n", sc->sc_state));
481 return(EBUSY);
482 } else
483 sc->sc_state |= LPTINIT;
484
485 sc->sc_flags = LPTFLAGS(minor(dev));
486
487 /* Check for open with BYPASS flag set. */
488 if (sc->sc_flags & LP_BYPASS) {
489 sc->sc_state = OPEN;
490 return(0);
491 }
492
493 /* request the ppbus only if we don't have it already */
494 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
495 /* give it a chance to try later */
496 sc->sc_state = 0;
497 return (err);
498 }
499
500 s = spltty();
501 lprintf((LPT_NAME " flags 0x%x\n", sc->sc_flags));
502
503 /* set IRQ status according to ENABLE_IRQ flag
504 */
505 if (sc->sc_irq & LP_ENABLE_IRQ)
506 sc->sc_irq |= LP_USE_IRQ;
507 else
508 sc->sc_irq &= ~LP_USE_IRQ;
509
510 /* init printer */
511 if ((sc->sc_flags & LP_NO_PRIME) == 0) {
512 if((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) {
513 ppb_wctr(ppbus, 0);
514 sc->sc_primed++;
515 DELAY(500);
516 }
517 }
518
519 ppb_wctr(ppbus, LPC_SEL|LPC_NINIT);
520
521 /* wait till ready (printer running diagnostics) */
522 trys = 0;
523 do {
524 /* ran out of waiting for the printer */
525 if (trys++ >= LPINITRDY*4) {
526 splx(s);
527 sc->sc_state = 0;
528 lprintf(("status %x\n", ppb_rstr(ppbus)));
529
530 lpt_release_ppbus(lptdev);
531 return (EBUSY);
532 }
533
534 /* wait 1/4 second, give up if we get a signal */
377d4740 535 if (tsleep((caddr_t)lptdev, PCATCH, "lptinit", hz/4) !=
984263bc
MD
536 EWOULDBLOCK) {
537 sc->sc_state = 0;
538 splx(s);
539
540 lpt_release_ppbus(lptdev);
541 return (EBUSY);
542 }
543
544 /* is printer online and ready for output */
545 } while ((ppb_rstr(ppbus) &
546 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
547 (LPS_SEL|LPS_NBSY|LPS_NERR));
548
549 sc->sc_control = LPC_SEL|LPC_NINIT;
550 if (sc->sc_flags & LP_AUTOLF)
551 sc->sc_control |= LPC_AUTOL;
552
553 /* enable interrupt if interrupt-driven */
554 if (sc->sc_irq & LP_USE_IRQ)
555 sc->sc_control |= LPC_ENA;
556
557 ppb_wctr(ppbus, sc->sc_control);
558
559 sc->sc_state = OPEN;
560 sc->sc_inbuf = malloc(BUFSIZE, M_LPT, M_WAITOK);
561 sc->sc_statbuf = malloc(BUFSTATSIZE, M_LPT, M_WAITOK);
562 sc->sc_xfercnt = 0;
563 splx(s);
564
565 /* release the ppbus */
566 lpt_release_ppbus(lptdev);
567
568 /* only use timeout if using interrupt */
569 lprintf(("irq %x\n", sc->sc_irq));
570 if (sc->sc_irq & LP_USE_IRQ) {
571 sc->sc_state |= TOUT;
6aed8a5b
MD
572 sc->sc_backoff = hz / LPTOUTINITIAL;
573 callout_reset(&sc->sc_callout, sc->sc_backoff, lptout, lptdev);
984263bc
MD
574 }
575
576 lprintf(("opened.\n"));
577 return(0);
578}
579
580/*
581 * lptclose -- close the device, free the local line buffer.
582 *
583 * Check for interrupted write call added.
584 */
585
586static int
41c20dac 587lptclose(dev_t dev, int flags, int fmt, struct thread *p)
984263bc
MD
588{
589 u_int unit = LPTUNIT(minor(dev));
590 struct lpt_data *sc = UNITOSOFTC(unit);
591 device_t lptdev = UNITODEVICE(unit);
592 device_t ppbus = device_get_parent(lptdev);
593 int err;
594
595 if(sc->sc_flags & LP_BYPASS)
596 goto end_close;
597
598 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0)
599 return (err);
600
601 sc->sc_state &= ~OPEN;
602
603 /* if the last write was interrupted, don't complete it */
6aed8a5b 604 if((!(sc->sc_state & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ)) {
984263bc 605 while ((ppb_rstr(ppbus) &
6aed8a5b
MD
606 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
607 (LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt) {
984263bc 608 /* wait 1/4 second, give up if we get a signal */
377d4740 609 if (tsleep((caddr_t)lptdev, PCATCH,
6aed8a5b 610 "lpclose", hz) != EWOULDBLOCK) {
984263bc 611 break;
6aed8a5b
MD
612 }
613 }
614 }
615 callout_stop(&sc->sc_callout);
984263bc
MD
616 ppb_wctr(ppbus, LPC_NINIT);
617 free(sc->sc_inbuf, M_LPT);
618 free(sc->sc_statbuf, M_LPT);
619
620end_close:
621 /* release the bus anyway
622 * unregistration of interrupt forced by release
623 */
624 lpt_release_ppbus(lptdev);
625
626 sc->sc_state = 0;
627 sc->sc_xfercnt = 0;
628 lprintf(("closed.\n"));
629 return(0);
630}
631
632/*
633 * lpt_pushbytes()
634 * Workhorse for actually spinning and writing bytes to printer
635 * Derived from lpa.c
636 * Originally by ?
637 *
638 * This code is only used when we are polling the port
639 */
640static int
641lpt_pushbytes(device_t dev)
642{
643 struct lpt_data *sc = DEVTOSOFTC(dev);
644 device_t ppbus = device_get_parent(dev);
645 int spin, err, tic;
646 char ch;
647
648 lprintf(("p"));
649 /* loop for every character .. */
650 while (sc->sc_xfercnt > 0) {
651 /* printer data */
652 ch = *(sc->sc_cp);
653 sc->sc_cp++;
654 sc->sc_xfercnt--;
655
656 /*
657 * Wait for printer ready.
658 * Loop 20 usecs testing BUSY bit, then sleep
659 * for exponentially increasing timeout. (vak)
660 */
661 for (spin = 0; NOT_READY(ppbus) && spin < MAX_SPIN; ++spin)
662 DELAY(1); /* XXX delay is NOT this accurate! */
663 if (spin >= MAX_SPIN) {
664 tic = 0;
665 while (NOT_READY(ppbus)) {
666 /*
667 * Now sleep, every cycle a
668 * little longer ..
669 */
670 tic = tic + tic + 1;
671 /*
672 * But no more than 10 seconds. (vak)
673 */
674 if (tic > MAX_SLEEP)
675 tic = MAX_SLEEP;
377d4740 676 err = tsleep((caddr_t)dev, 0,
984263bc
MD
677 LPT_NAME "poll", tic);
678 if (err != EWOULDBLOCK) {
679 return (err);
680 }
681 }
682 }
683
684 /* output data */
685 ppb_wdtr(ppbus, ch);
686 /* strobe */
687 ppb_wctr(ppbus, sc->sc_control|LPC_STB);
688 ppb_wctr(ppbus, sc->sc_control);
689
690 }
691 return(0);
692}
693
694/*
695 * lptread --retrieve printer status in IEEE1284 NIBBLE mode
696 */
697
698static int
699lptread(dev_t dev, struct uio *uio, int ioflag)
700{
701 u_int unit = LPTUNIT(minor(dev));
702 struct lpt_data *sc = UNITOSOFTC(unit);
703 device_t lptdev = UNITODEVICE(unit);
704 device_t ppbus = device_get_parent(lptdev);
705 int error = 0, len;
706
707 if (sc->sc_flags & LP_BYPASS) {
708 /* we can't do reads in bypass mode */
709 return (EPERM);
710 }
711
712 if ((error = ppb_1284_negociate(ppbus, PPB_NIBBLE, 0)))
713 return (error);
714
715 /* read data in an other buffer, read/write may be simultaneous */
716 len = 0;
717 while (uio->uio_resid) {
718 if ((error = ppb_1284_read(ppbus, PPB_NIBBLE,
719 sc->sc_statbuf, min(BUFSTATSIZE,
720 uio->uio_resid), &len))) {
721 goto error;
722 }
723
724 if (!len)
725 goto error; /* no more data */
726
727 if ((error = uiomove(sc->sc_statbuf, len, uio)))
728 goto error;
729 }
730
731error:
732 ppb_1284_terminate(ppbus);
733 return (error);
734}
735
736/*
737 * lptwrite --copy a line from user space to a local buffer, then call
738 * putc to get the chars moved to the output queue.
739 *
740 * Flagging of interrupted write added.
741 */
742
743static int
744lptwrite(dev_t dev, struct uio *uio, int ioflag)
745{
f96d6c88 746 unsigned n;
984263bc
MD
747 int err;
748 u_int unit = LPTUNIT(minor(dev));
749 struct lpt_data *sc = UNITOSOFTC(unit);
750 device_t lptdev = UNITODEVICE(unit);
751 device_t ppbus = device_get_parent(lptdev);
752
753 if(sc->sc_flags & LP_BYPASS) {
754 /* we can't do writes in bypass mode */
755 return(EPERM);
756 }
757
758 /* request the ppbus only if we don't have it already */
759 /* XXX interrupt registration?! */
760 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0)
761 return (err);
762
763 /* if interrupts are working, register the handler */
764 if (sc->sc_irq & LP_USE_IRQ) {
765 /* register our interrupt handler */
766 err = BUS_SETUP_INTR(ppbus, lptdev, sc->intr_resource,
767 INTR_TYPE_TTY, lpt_intr, lptdev,
768 &sc->intr_cookie);
769 if (err) {
770 device_printf(lptdev, "handler registration failed, polled mode.\n");
771 sc->sc_irq &= ~LP_USE_IRQ;
772 }
773 }
774
775 sc->sc_state &= ~INTERRUPTED;
776 while ((n = min(BUFSIZE, uio->uio_resid)) != 0) {
777 sc->sc_cp = sc->sc_inbuf;
778 uiomove(sc->sc_cp, n, uio);
779 sc->sc_xfercnt = n ;
780
781 if (sc->sc_irq & LP_ENABLE_EXT) {
782 /* try any extended mode */
783 err = ppb_write(ppbus, sc->sc_cp,
784 sc->sc_xfercnt, 0);
785 switch (err) {
786 case 0:
787 /* if not all data was sent, we could rely
788 * on polling for the last bytes */
789 sc->sc_xfercnt = 0;
790 break;
791 case EINTR:
792 sc->sc_state |= INTERRUPTED;
793 return(err);
794 case EINVAL:
795 /* advanced mode not avail */
796 log(LOG_NOTICE, LPT_NAME "%d: advanced mode not avail, polling\n", unit);
797 break;
798 default:
799 return(err);
800 }
801 } else while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) {
802 lprintf(("i"));
803 /* if the printer is ready for a char, */
804 /* give it one */
805 if ((sc->sc_state & OBUSY) == 0){
806 lprintf(("\nC %d. ", sc->sc_xfercnt));
807 lptintr(lptdev);
808 }
809 lprintf(("W "));
810 if (sc->sc_state & OBUSY)
811 if ((err = tsleep((caddr_t)lptdev,
377d4740 812 PCATCH, LPT_NAME "write", 0))) {
984263bc
MD
813 sc->sc_state |= INTERRUPTED;
814 return(err);
815 }
816 }
817
818 /* check to see if we must do a polled write */
819 if(!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) {
820 lprintf(("p"));
821
822 err = lpt_pushbytes(lptdev);
823
824 if (err)
825 return(err);
826 }
827 }
828
829 /* we have not been interrupted, release the ppbus */
830 lpt_release_ppbus(lptdev);
831
832 return(0);
833}
834
835/*
836 * lpt_intr -- handle printer interrupts which occur when the printer is
837 * ready to accept another char.
838 *
839 * do checking for interrupted write call.
840 */
841
842static void
843lpt_intr(void *arg)
844{
845 device_t lptdev = (device_t)arg;
846 device_t ppbus = device_get_parent(lptdev);
847 struct lpt_data *sc = DEVTOSOFTC(lptdev);
848 int sts = 0;
849 int i;
850
851 /* we must own the bus to use it */
852 if ((sc->sc_state & HAVEBUS) == 0)
853 return;
854
855 /*
856 * Is printer online and ready for output?
857 *
858 * Avoid falling back to lptout() too quickly. First spin-loop
859 * to see if the printer will become ready ``really soon now''.
860 */
861 for (i = 0; i < 100 &&
862 ((sts=ppb_rstr(ppbus)) & RDY_MASK) != LP_READY; i++) ;
863
864 if ((sts & RDY_MASK) == LP_READY) {
865 sc->sc_state = (sc->sc_state | OBUSY) & ~EERROR;
866 sc->sc_backoff = hz/LPTOUTINITIAL;
867
868 if (sc->sc_xfercnt) {
869 /* send char */
870 /*lprintf(("%x ", *sc->sc_cp)); */
871 ppb_wdtr(ppbus, *sc->sc_cp++) ;
872 ppb_wctr(ppbus, sc->sc_control|LPC_STB);
873 /* DELAY(X) */
874 ppb_wctr(ppbus, sc->sc_control);
875
876 /* any more data for printer */
877 if(--(sc->sc_xfercnt) > 0) return;
878 }
879
880 /*
881 * No more data waiting for printer.
882 * Wakeup is not done if write call was not interrupted.
883 */
884 sc->sc_state &= ~OBUSY;
885
886 if(!(sc->sc_state & INTERRUPTED))
887 wakeup((caddr_t)lptdev);
888 lprintf(("w "));
889 return;
890 } else { /* check for error */
891 if(((sts & (LPS_NERR | LPS_OUT) ) != LPS_NERR) &&
892 (sc->sc_state & OPEN))
893 sc->sc_state |= EERROR;
894 /* lptout() will jump in and try to restart. */
895 }
896 lprintf(("sts %x ", sts));
897}
898
899static void
900lptintr(device_t dev)
901{
902 /* call the interrupt at required spl level */
903 int s = spltty();
904
905 lpt_intr(dev);
906
907 splx(s);
908 return;
909}
910
911static int
41c20dac 912lptioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *p)
984263bc
MD
913{
914 int error = 0;
915 u_int unit = LPTUNIT(minor(dev));
916 struct lpt_data *sc = UNITOSOFTC(unit);
917 u_char old_sc_irq; /* old printer IRQ status */
918
919 switch (cmd) {
920 case LPT_IRQ :
921 if(sc->sc_irq & LP_HAS_IRQ) {
922 /*
923 * NOTE:
924 * If the IRQ status is changed,
925 * this will only be visible on the
926 * next open.
927 *
928 * If interrupt status changes,
929 * this gets syslog'd.
930 */
931 old_sc_irq = sc->sc_irq;
932 switch(*(int*)data) {
933 case 0:
934 sc->sc_irq &= (~LP_ENABLE_IRQ);
935 break;
936 case 1:
937 sc->sc_irq &= (~LP_ENABLE_EXT);
938 sc->sc_irq |= LP_ENABLE_IRQ;
939 break;
940 case 2:
941 /* classic irq based transfer and advanced
942 * modes are in conflict
943 */
944 sc->sc_irq &= (~LP_ENABLE_IRQ);
945 sc->sc_irq |= LP_ENABLE_EXT;
946 break;
947 case 3:
948 sc->sc_irq &= (~LP_ENABLE_EXT);
949 break;
950 default:
951 break;
952 }
953
954 if (old_sc_irq != sc->sc_irq )
955 log(LOG_NOTICE, LPT_NAME "%d: switched to %s %s mode\n",
956 unit,
957 (sc->sc_irq & LP_ENABLE_IRQ)?
958 "interrupt-driven":"polled",
959 (sc->sc_irq & LP_ENABLE_EXT)?
960 "extended":"standard");
961 } else /* polled port */
962 error = EOPNOTSUPP;
963 break;
964 default:
965 error = ENODEV;
966 }
967
968 return(error);
969}
970
971static device_method_t lpt_methods[] = {
972 /* device interface */
973 DEVMETHOD(device_identify, lpt_identify),
974 DEVMETHOD(device_probe, lpt_probe),
975 DEVMETHOD(device_attach, lpt_attach),
976
977 { 0, 0 }
978};
979
980static driver_t lpt_driver = {
981 LPT_NAME,
982 lpt_methods,
983 sizeof(struct lpt_data),
984};
985
986DRIVER_MODULE(lpt, ppbus, lpt_driver, lpt_devclass, 0, 0);