olpt.c revision 126080
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 * $FreeBSD: head/sys/pc98/cbus/olpt.c 126080 2004-02-21 21:10:55Z phk $
50 */
51
52/*
53 * Device Driver for AT parallel printer port
54 * Written by William Jolitz 12/18/90
55 */
56
57/*
58 * Parallel port TCP/IP interfaces added.  I looked at the driver from
59 * MACH but this is a complete rewrite, and btw. incompatible, and it
60 * should perform better too.  I have never run the MACH driver though.
61 *
62 * This driver sends two bytes (0x08, 0x00) in front of each packet,
63 * to allow us to distinguish another format later.
64 *
65 * Now added a Linux/Crynwr compatibility mode which is enabled using
66 * IF_LINK0 - Tim Wilkinson.
67 *
68 * TODO:
69 *    Make HDLC/PPP mode, use IF_LLC1 to enable.
70 *
71 * Connect the two computers using a Laplink parallel cable to use this
72 * feature:
73 *
74 *      +----------------------------------------+
75 * 	|A-name	A-End	B-End	Descr.	Port/Bit |
76 *      +----------------------------------------+
77 *	|DATA0	2	15	Data	0/0x01   |
78 *	|-ERROR	15	2	   	1/0x08   |
79 *      +----------------------------------------+
80 *	|DATA1	3	13	Data	0/0x02	 |
81 *	|+SLCT	13	3	   	1/0x10   |
82 *      +----------------------------------------+
83 *	|DATA2	4	12	Data	0/0x04   |
84 *	|+PE	12	4	   	1/0x20   |
85 *      +----------------------------------------+
86 *	|DATA3	5	10	Strobe	0/0x08   |
87 *	|-ACK	10	5	   	1/0x40   |
88 *      +----------------------------------------+
89 *	|DATA4	6	11	Data	0/0x10   |
90 *	|BUSY	11	6	   	1/~0x80  |
91 *      +----------------------------------------+
92 *	|GND	18-25	18-25	GND	-        |
93 *      +----------------------------------------+
94 *
95 * Expect transfer-rates up to 75 kbyte/sec.
96 *
97 * If GCC could correctly grok
98 *	register int port asm("edx")
99 * the code would be cleaner
100 *
101 * Poul-Henning Kamp <phk@freebsd.org>
102 */
103
104#include <sys/param.h>
105#include <sys/systm.h>
106#include <sys/conf.h>
107#include <sys/bus.h>
108#include <sys/kernel.h>
109#include <sys/uio.h>
110#include <sys/syslog.h>
111#include <sys/malloc.h>
112
113#include <machine/clock.h>
114#include <machine/bus.h>
115#include <machine/resource.h>
116#include <sys/rman.h>
117
118#include <isa/isavar.h>
119
120#include <i386/isa/lptreg.h>
121#include <dev/ppbus/lptio.h>
122
123#define	LPINITRDY	4	/* wait up to 4 seconds for a ready */
124#define	LPTOUTINITIAL	10	/* initial timeout to wait for ready 1/10 s */
125#define	LPTOUTMAX	1	/* maximal timeout 1 s */
126#define	LPPRI		(PZERO+8)
127#define	BUFSIZE		1024
128
129#ifndef PC98
130/* BIOS printer list - used by BIOS probe*/
131#define	BIOS_LPT_PORTS	0x408
132#define	BIOS_PORTS	(short *)(KERNBASE+BIOS_LPT_PORTS)
133#define	BIOS_MAX_LPT	4
134#endif
135
136
137#ifndef DEBUG
138#define lprintf(args)
139#else
140#define lprintf(args)	do {				\
141				if (lptflag)		\
142					printf args;	\
143			} while (0)
144static int volatile lptflag = 1;
145#endif
146
147#define	LPTUNIT(s)	((s)&0x03)
148#define	LPTFLAGS(s)	((s)&0xfc)
149
150struct lpt_softc {
151	struct resource *res_port;
152	struct resource *res_irq;
153	void *sc_ih;
154
155	int	sc_port;
156	short	sc_state;
157	/* default case: negative prime, negative ack, handshake strobe,
158	   prime once */
159	u_char	sc_control;
160	char	sc_flags;
161#define LP_POS_INIT	0x04	/* if we are a postive init signal */
162#define LP_POS_ACK	0x08	/* if we are a positive going ack */
163#define LP_NO_PRIME	0x10	/* don't prime the printer at all */
164#define LP_PRIMEOPEN	0x20	/* prime on every open */
165#define LP_AUTOLF	0x40	/* tell printer to do an automatic lf */
166#define LP_BYPASS	0x80	/* bypass  printer ready checks */
167	void	*sc_inbuf;
168	short	sc_xfercnt ;
169	char	sc_primed;
170	char	*sc_cp ;
171	u_char	sc_irq ;	/* IRQ status of port */
172#define LP_HAS_IRQ	0x01	/* we have an irq available */
173#define LP_USE_IRQ	0x02	/* we are using our irq */
174#define LP_ENABLE_IRQ	0x04	/* enable IRQ on open */
175	u_char	sc_backoff ;	/* time to call lptout() again */
176};
177
178/* bits for state */
179#define	OPEN		(1<<0)	/* device is open */
180#define	ASLP		(1<<1)	/* awaiting draining of printer */
181#define	ERROR		(1<<2)	/* error was received from printer */
182#define	OBUSY		(1<<3)	/* printer is busy doing output */
183#define LPTOUT		(1<<4)	/* timeout while not selected */
184#define TOUT		(1<<5)	/* timeout while not selected */
185#define INIT		(1<<6)	/* waiting to initialize for open */
186#define INTERRUPTED	(1<<7)	/* write call was interrupted */
187
188
189/* status masks to interrogate printer status */
190#define RDY_MASK	(LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)	/* ready ? */
191#define LP_READY	(LPS_SEL|LPS_NBSY|LPS_NERR)
192
193/* Printer Ready condition  - from lpa.c */
194/* Only used in polling code */
195#ifdef PC98
196#define	NOT_READY(x)	((inb(x) & LPS_NBSY) != LPS_NBSY)
197#else	/* IBM-PC */
198#define	LPS_INVERT	(LPS_NBSY | LPS_NACK |           LPS_SEL | LPS_NERR)
199#define	LPS_MASK	(LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR)
200#define	NOT_READY(x)	((inb(x)^LPS_INVERT)&LPS_MASK)
201#endif
202
203#define	MAX_SLEEP	(hz*5)	/* Timeout while waiting for device ready */
204#define	MAX_SPIN	20	/* Max delay for device ready in usecs */
205
206static timeout_t lptout;
207static int lpt_probe(device_t);
208static int lpt_attach(device_t);
209static void lpt_intr(void *);
210
211static devclass_t olpt_devclass;
212
213static device_method_t olpt_methods[] = {
214	DEVMETHOD(device_probe,		lpt_probe),
215	DEVMETHOD(device_attach,	lpt_attach),
216	{ 0, 0 }
217};
218
219static driver_t olpt_driver = {
220	"olpt",
221	olpt_methods,
222	sizeof (struct lpt_softc),
223};
224
225DRIVER_MODULE(olpt, isa, olpt_driver, olpt_devclass, 0, 0);
226
227static	d_open_t	lptopen;
228static	d_close_t	lptclose;
229static	d_write_t	lptwrite;
230static	d_ioctl_t	lptioctl;
231
232static struct cdevsw lpt_cdevsw = {
233	.d_version =	D_VERSION,
234	.d_flags =	D_NEEDGIANT,
235	.d_open =	lptopen,
236	.d_close =	lptclose,
237	.d_write =	lptwrite,
238	.d_ioctl =	lptioctl,
239	.d_name =	"lpt",
240};
241
242static bus_addr_t lpt_iat[] = {0, 2, 4, 6};
243
244#ifndef PC98
245/*
246 * Internal routine to lptprobe to do port tests of one byte value
247 */
248static int
249lpt_port_test (int port, u_char data, u_char mask)
250{
251	int	temp, timeout;
252
253	data = data & mask;
254	outb(port, data);
255	timeout = 10000;
256	do {
257		DELAY(10);
258		temp = inb(port) & mask;
259	}
260	while (temp != data && --timeout);
261	lprintf(("Port 0x%x\tout=%x\tin=%x\ttout=%d\n",
262		port, data, temp, timeout));
263	return (temp == data);
264}
265#endif /* PC98 */
266
267/*
268 * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94
269 * Based partially on Rod Grimes' printer probe
270 *
271 * Logic:
272 *	1) If no port address was given, use the bios detected ports
273 *	   and autodetect what ports the printers are on.
274 *	2) Otherwise, probe the data port at the address given,
275 *	   using the method in Rod Grimes' port probe.
276 *	   (Much code ripped off directly from Rod's probe.)
277 *
278 * Comments from Rod's probe:
279 * Logic:
280 *	1) You should be able to write to and read back the same value
281 *	   to the data port.  Do an alternating zeros, alternating ones,
282 *	   walking zero, and walking one test to check for stuck bits.
283 *
284 *	2) You should be able to write to and read back the same value
285 *	   to the control port lower 5 bits, the upper 3 bits are reserved
286 *	   per the IBM PC technical reference manauls and different boards
287 *	   do different things with them.  Do an alternating zeros, alternating
288 *	   ones, walking zero, and walking one test to check for stuck bits.
289 *
290 *	   Some printers drag the strobe line down when the are powered off
291 * 	   so this bit has been masked out of the control port test.
292 *
293 *	   XXX Some printers may not like a fast pulse on init or strobe, I
294 *	   don't know at this point, if that becomes a problem these bits
295 *	   should be turned off in the mask byte for the control port test.
296 *
297 *	   We are finally left with a mask of 0x14, due to some printers
298 *	   being adamant about holding other bits high ........
299 *
300 *	   Before probing the control port, we write a 0 to the data port -
301 *	   If not, some printers chuck out garbage when the strobe line
302 *	   gets toggled.
303 *
304 *	3) Set the data and control ports to a value of 0
305 *
306 *	This probe routine has been tested on Epson Lx-800, HP LJ3P,
307 *	Epson FX-1170 and C.Itoh 8510RM
308 *	printers.
309 *	Quick exit on fail added.
310 */
311
312int
313lpt_probe(device_t dev)
314{
315#ifdef PC98
316#define PC98_OLD_LPT 0x40
317#define PC98_IEEE_1284_FUNCTION 0x149
318	int rid;
319	struct resource *res;
320
321	/* Check isapnp ids */
322	if (isa_get_vendorid(dev))
323		return ENXIO;
324
325	rid = 0;
326	res = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid, lpt_iat, 4,
327				  RF_ACTIVE);
328	if (res == NULL)
329		return ENXIO;
330	isa_load_resourcev(res, lpt_iat, 4);
331
332	if (isa_get_port(dev) == PC98_OLD_LPT) {
333		unsigned int pc98_ieee_mode, tmp;
334
335		tmp = inb(PC98_IEEE_1284_FUNCTION);
336		pc98_ieee_mode = tmp;
337		if ((tmp & 0x10) == 0x10) {
338			outb(PC98_IEEE_1284_FUNCTION, tmp & ~0x10);
339			tmp = inb(PC98_IEEE_1284_FUNCTION);
340			if ((tmp & 0x10) != 0x10) {
341				outb(PC98_IEEE_1284_FUNCTION, pc98_ieee_mode);
342				bus_release_resource(dev, SYS_RES_IOPORT, rid,
343						     res);
344				return ENXIO;
345			}
346		}
347	}
348
349	bus_release_resource(dev, SYS_RES_IOPORT, rid, res);
350	return 0;
351#else
352	int		port;
353	static short	next_bios_lpt = 0;
354	int		status;
355	static u_char	testbyte[18] = {
356		0x55,			/* alternating zeros */
357		0xaa,			/* alternating ones */
358		0xfe, 0xfd, 0xfb, 0xf7,
359		0xef, 0xdf, 0xbf, 0x7f,	/* walking zero */
360		0x01, 0x02, 0x04, 0x08,
361		0x10, 0x20, 0x40, 0x80	/* walking one */
362	};
363	int		i;
364
365	/*
366	 * Make sure there is some way for lptopen to see that
367	 * the port is not configured
368	 * This 0 will remain if the port isn't attached
369	 */
370	(lpt_sc + dvp->id_unit)->sc_port = 0;
371
372	status = IO_LPTSIZE;
373	/* If port not specified, use bios list */
374	if(dvp->id_iobase < 0) {	/* port? */
375		if((next_bios_lpt < BIOS_MAX_LPT) &&
376				(*(BIOS_PORTS+next_bios_lpt) != 0) ) {
377			dvp->id_iobase = *(BIOS_PORTS+next_bios_lpt++);
378			goto end_probe;
379		} else
380			return (0);
381	}
382
383	/* Port was explicitly specified */
384	/* This allows probing of ports unknown to the BIOS */
385	port = dvp->id_iobase + lpt_data;
386	for (i = 0; i < 18; i++) {
387		if (!lpt_port_test(port, testbyte[i], 0xff)) {
388			status = 0;
389			goto end_probe;
390		}
391	}
392
393end_probe:
394	/* write 0's to control and data ports */
395	outb(dvp->id_iobase+lpt_data, 0);
396	outb(dvp->id_iobase+lpt_control, 0);
397
398	return (status);
399#endif
400}
401
402/* XXX Todo - try and detect if interrupt is working */
403int
404lpt_attach(device_t dev)
405{
406	int	rid, unit;
407	struct	lpt_softc	*sc;
408
409	unit = device_get_unit(dev);
410	sc = device_get_softc(dev);
411
412	rid = 0;
413	sc->res_port = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid,
414					   lpt_iat, 4, RF_ACTIVE);
415	if (sc->res_port == NULL)
416		return ENXIO;
417	isa_load_resourcev(sc->res_port, lpt_iat, 4);
418
419	sc->sc_port = rman_get_start(sc->res_port);
420	sc->sc_primed = 0;	/* not primed yet */
421#ifdef PC98
422	outb(sc->sc_port+lpt_pstb_ctrl,	LPC_DIS_PSTB);	/* PSTB disable */
423	outb(sc->sc_port+lpt_control,	LPC_MODE8255);	/* 8255 mode set */
424	outb(sc->sc_port+lpt_control,	LPC_NIRQ8);	/* IRQ8 inactive */
425	outb(sc->sc_port+lpt_control,	LPC_NPSTB);	/* PSTB inactive */
426	outb(sc->sc_port+lpt_pstb_ctrl,	LPC_EN_PSTB);	/* PSTB enable */
427#else
428	outb(sc->sc_port+lpt_control, LPC_NINIT);
429#endif
430
431	sc->sc_irq = 0;
432	if (isa_get_irq(dev) != -1) {
433		rid = 0;
434		sc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
435						 0, ~0, 1, RF_ACTIVE);
436		if (sc->res_irq == NULL) {
437			bus_release_resource(dev, SYS_RES_IOPORT, 0,
438					     sc->res_port);
439			return ENXIO;
440		}
441		if (bus_setup_intr(dev, sc->res_irq, INTR_TYPE_TTY, lpt_intr,
442				   sc, &sc->sc_ih)) {
443			bus_release_resource(dev, SYS_RES_IOPORT, 0,
444					     sc->res_port);
445			bus_release_resource(dev, SYS_RES_IRQ, 0,
446					     sc->res_irq);
447			return ENXIO;
448		}
449		sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ;
450		device_printf(dev, "Interrupt-driven port");
451	}
452
453	/* XXX what to do about the flags in the minor number? */
454	make_dev(&lpt_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600, "lpt%d", unit);
455	make_dev(&lpt_cdevsw, unit | LP_BYPASS,
456			UID_ROOT, GID_WHEEL, 0600, "lpctl%d", unit);
457
458	return 0;
459}
460
461/*
462 * lptopen -- reset the printer, then wait until it's selected and not busy.
463 *	If LP_BYPASS flag is selected, then we do not try to select the
464 *	printer -- this is just used for passing ioctls.
465 */
466
467static	int
468lptopen (dev_t dev, int flags, int fmt, struct thread *td)
469{
470	struct lpt_softc *sc;
471	int s;
472#ifdef PC98
473	int port;
474#else
475	int trys, port;
476#endif
477
478	sc = devclass_get_softc(olpt_devclass, LPTUNIT(minor(dev)));
479	if (sc->sc_port == 0)
480		return (ENXIO);
481
482	if (sc->sc_state) {
483		lprintf(("lp: still open %x\n", sc->sc_state));
484		return(EBUSY);
485	} else
486		sc->sc_state |= INIT;
487
488	sc->sc_flags = LPTFLAGS(minor(dev));
489
490	/* Check for open with BYPASS flag set. */
491	if (sc->sc_flags & LP_BYPASS) {
492		sc->sc_state = OPEN;
493		return(0);
494	}
495
496	s = spltty();
497	lprintf(("lp flags 0x%x\n", sc->sc_flags));
498	port = sc->sc_port;
499
500	/* set IRQ status according to ENABLE_IRQ flag */
501	if (sc->sc_irq & LP_ENABLE_IRQ)
502		sc->sc_irq |= LP_USE_IRQ;
503	else
504		sc->sc_irq &= ~LP_USE_IRQ;
505
506	/* init printer */
507#ifndef PC98
508	if ((sc->sc_flags & LP_NO_PRIME) == 0) {
509		if((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) {
510			outb(port+lpt_control, 0);
511			sc->sc_primed++;
512			DELAY(500);
513		}
514	}
515
516	outb (port+lpt_control, LPC_SEL|LPC_NINIT);
517
518	/* wait till ready (printer running diagnostics) */
519	trys = 0;
520	do {
521		/* ran out of waiting for the printer */
522		if (trys++ >= LPINITRDY*4) {
523			splx(s);
524			sc->sc_state = 0;
525			lprintf(("status %x\n", inb(port+lpt_status)));
526			return (EBUSY);
527		}
528
529		/* wait 1/4 second, give up if we get a signal */
530		if (tsleep (sc, LPPRI|PCATCH, "lptinit", hz/4) !=
531		    EWOULDBLOCK) {
532			sc->sc_state = 0;
533			splx(s);
534			return (EBUSY);
535		}
536
537		/* is printer online and ready for output */
538	} while ((inb(port+lpt_status) & (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
539		 (LPS_SEL|LPS_NBSY|LPS_NERR));
540
541	sc->sc_control = LPC_SEL|LPC_NINIT;
542	if (sc->sc_flags & LP_AUTOLF)
543		sc->sc_control |= LPC_AUTOL;
544
545	/* enable interrupt if interrupt-driven */
546	if (sc->sc_irq & LP_USE_IRQ)
547		sc->sc_control |= LPC_ENA;
548
549	outb(port+lpt_control, sc->sc_control);
550#endif
551
552	sc->sc_state = OPEN;
553	sc->sc_inbuf = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
554	sc->sc_xfercnt = 0;
555	splx(s);
556
557	/* only use timeout if using interrupt */
558	lprintf(("irq %x\n", sc->sc_irq));
559	if (sc->sc_irq & LP_USE_IRQ) {
560		sc->sc_state |= TOUT;
561		timeout (lptout, (caddr_t)sc,
562			 (sc->sc_backoff = hz/LPTOUTINITIAL));
563	}
564
565	lprintf(("opened.\n"));
566	return(0);
567}
568
569static void
570lptout (void *arg)
571{
572	struct lpt_softc *sc = arg;
573	int pl;
574
575	lprintf(("T %x ", inb(sc->sc_port+lpt_status)));
576	if (sc->sc_state & OPEN) {
577		sc->sc_backoff++;
578		if (sc->sc_backoff > hz/LPTOUTMAX)
579			sc->sc_backoff = sc->sc_backoff > hz/LPTOUTMAX;
580		timeout (lptout, (caddr_t)sc, sc->sc_backoff);
581	} else
582		sc->sc_state &= ~TOUT;
583
584	if (sc->sc_state & ERROR)
585		sc->sc_state &= ~ERROR;
586
587	/*
588	 * Avoid possible hangs do to missed interrupts
589	 */
590	if (sc->sc_xfercnt) {
591		pl = spltty();
592		lpt_intr(sc);
593		splx(pl);
594	} else {
595		sc->sc_state &= ~OBUSY;
596		wakeup(sc);
597	}
598}
599
600/*
601 * lptclose -- close the device, free the local line buffer.
602 *
603 * Check for interrupted write call added.
604 */
605
606static	int
607lptclose(dev_t dev, int flags, int fmt, struct thread *td)
608{
609	struct lpt_softc *sc;
610#ifndef PC98
611	int port;
612#endif
613
614	sc = devclass_get_softc(olpt_devclass, LPTUNIT(minor(dev)));
615	if(sc->sc_flags & LP_BYPASS)
616		goto end_close;
617
618#ifndef PC98
619	port = sc->sc_port;
620#endif
621	sc->sc_state &= ~OPEN;
622
623#ifndef PC98
624	/* if the last write was interrupted, don't complete it */
625	if((!(sc->sc_state  & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ))
626		while ((inb(port+lpt_status) & (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
627			(LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt)
628			/* wait 1/4 second, give up if we get a signal */
629			if (tsleep (sc, LPPRI|PCATCH,
630				"lpclose", hz) != EWOULDBLOCK)
631				break;
632
633	outb(sc->sc_port+lpt_control, LPC_NINIT);
634#endif
635	free(sc->sc_inbuf, M_DEVBUF);
636
637end_close:
638	sc->sc_state = 0;
639	sc->sc_xfercnt = 0;
640	lprintf(("closed.\n"));
641	return(0);
642}
643
644/*
645 * pushbytes()
646 *	Workhorse for actually spinning and writing bytes to printer
647 *	Derived from lpa.c
648 *	Originally by ?
649 *
650 *	This code is only used when we are polling the port
651 */
652static int
653pushbytes(struct lpt_softc * sc)
654{
655	int spin, err, tic;
656	char ch;
657	int port = sc->sc_port;
658
659	lprintf(("p"));
660	/* loop for every character .. */
661	while (sc->sc_xfercnt > 0) {
662		/* printer data */
663		ch = *(sc->sc_cp);
664		sc->sc_cp++;
665		sc->sc_xfercnt--;
666
667		/*
668		 * Wait for printer ready.
669		 * Loop 20 usecs testing BUSY bit, then sleep
670		 * for exponentially increasing timeout. (vak)
671		 */
672		for (spin=0; NOT_READY(port+lpt_status) && spin<MAX_SPIN; ++spin)
673			DELAY(1);	/* XXX delay is NOT this accurate! */
674		if (spin >= MAX_SPIN) {
675			tic = 0;
676			while (NOT_READY(port+lpt_status)) {
677				/*
678				 * Now sleep, every cycle a
679				 * little longer ..
680				 */
681				tic = tic + tic + 1;
682				/*
683				 * But no more than 10 seconds. (vak)
684				 */
685				if (tic > MAX_SLEEP)
686					tic = MAX_SLEEP;
687				err = tsleep(sc, LPPRI,
688					"lptpoll", tic);
689				if (err != EWOULDBLOCK) {
690					return (err);
691				}
692			}
693		}
694
695		/* output data */
696		outb(port+lpt_data, ch);
697#ifdef PC98
698		DELAY(1);
699		outb(port+lpt_control, LPC_PSTB);
700		DELAY(1);
701		outb(port+lpt_control, LPC_NPSTB);
702#else
703		/* strobe */
704		outb(port+lpt_control, sc->sc_control|LPC_STB);
705		outb(port+lpt_control, sc->sc_control);
706#endif
707
708	}
709	return(0);
710}
711
712/*
713 * lptwrite --copy a line from user space to a local buffer, then call
714 * putc to get the chars moved to the output queue.
715 *
716 * Flagging of interrupted write added.
717 */
718
719static	int
720lptwrite(dev_t dev, struct uio * uio, int ioflag)
721{
722	register unsigned n;
723	int pl, err;
724	struct lpt_softc *sc;
725
726	sc = devclass_get_softc(olpt_devclass, LPTUNIT(minor(dev)));
727	if(sc->sc_flags & LP_BYPASS) {
728		/* we can't do writes in bypass mode */
729		return(EPERM);
730	}
731
732	sc->sc_state &= ~INTERRUPTED;
733	while ((n = min(BUFSIZE, uio->uio_resid)) != 0) {
734		sc->sc_cp = sc->sc_inbuf;
735		uiomove(sc->sc_cp, n, uio);
736		sc->sc_xfercnt = n ;
737		while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) {
738			lprintf(("i"));
739			/* if the printer is ready for a char, */
740			/* give it one */
741			if ((sc->sc_state & OBUSY) == 0){
742				lprintf(("\nC %d. ", sc->sc_xfercnt));
743				pl = spltty();
744				lpt_intr(sc);
745				(void) splx(pl);
746			}
747			lprintf(("W "));
748			if (sc->sc_state & OBUSY)
749				if ((err = tsleep (sc,
750					 LPPRI|PCATCH, "lpwrite", 0))) {
751					sc->sc_state |= INTERRUPTED;
752					return(err);
753				}
754		}
755		/* check to see if we must do a polled write */
756		if(!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) {
757			lprintf(("p"));
758			if((err = pushbytes(sc)))
759				return(err);
760		}
761	}
762	return(0);
763}
764
765/*
766 * lptintr -- handle printer interrupts which occur when the printer is
767 * ready to accept another char.
768 *
769 * do checking for interrupted write call.
770 */
771
772static void
773lpt_intr(void *arg)
774{
775}
776
777static	int
778lptioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
779{
780	int	error = 0;
781        struct	lpt_softc *sc;
782        u_int	unit = LPTUNIT(minor(dev));
783	u_char	old_sc_irq;	/* old printer IRQ status */
784
785        sc = devclass_get_softc(olpt_devclass, unit);
786
787	switch (cmd) {
788	case LPT_IRQ :
789		if(sc->sc_irq & LP_HAS_IRQ) {
790			/*
791			 * NOTE:
792			 * If the IRQ status is changed,
793			 * this will only be visible on the
794			 * next open.
795			 *
796			 * If interrupt status changes,
797			 * this gets syslog'd.
798			 */
799			old_sc_irq = sc->sc_irq;
800			if(*(int*)data == 0)
801				sc->sc_irq &= (~LP_ENABLE_IRQ);
802			else
803				sc->sc_irq |= LP_ENABLE_IRQ;
804			if (old_sc_irq != sc->sc_irq )
805				log(LOG_NOTICE, "lpt%c switched to %s mode\n",
806					(char)unit+'0',
807					(sc->sc_irq & LP_ENABLE_IRQ)?
808					"interrupt-driven":"polled");
809		} else /* polled port */
810			error = EOPNOTSUPP;
811		break;
812	default:
813		error = ENODEV;
814	}
815
816	return(error);
817}
818