lptvar.h revision 1.9 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 * $Id: lptvar.h,v 1.9 1993/12/20 09:06:23 mycroft Exp $
49 */
50
51 /*
52 * Device Driver for AT parallel printer port
53 * Written by William Jolitz 12/18/90
54 *
55 * Hacked heavily by Rod Grimes and Eric Haug...
56 */
57
58 #include "lpt.h"
59 #if NLPT > 0
60
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/proc.h>
64 #include <sys/user.h>
65 #include <sys/buf.h>
66 #include <sys/kernel.h>
67 #include <sys/ioctl.h>
68 #include <sys/tty.h>
69 #include <sys/uio.h>
70
71 #include <machine/pio.h>
72
73 #include <i386/isa/isa.h>
74 #include <i386/isa/isa_device.h>
75 #include <i386/isa/lptreg.h>
76
77 #define LPINITRDY 4 /* wait up to 4 seconds for a ready */
78 #define LPTOUTTIME 4 /* wait up to 4 seconds for a ready */
79 #define LPPRI (PZERO+8)
80 #define BUFSIZE 1024
81
82 #ifndef DEBUG
83 #define lprintf
84 #else
85 #define lprintf if (lptflag) printf
86 int lptflag = 1;
87 #endif
88
89 int lptout();
90 #ifdef DEBUG
91 int lptflag = 1;
92 #endif
93
94 int lptprobe(), lptattach(), lptintr();
95
96 struct isa_driver lptdriver = {
97 lptprobe, lptattach, "lpt"
98 };
99
100 #define LPTUNIT(s) (((s)>>6)&0x3)
101 #define LPTFLAGS(s) ((s)&0x3f)
102
103 struct lpt_softc {
104 short sc_port;
105 short sc_state;
106 /* default case: negative prime, negative ack, handshake strobe,
107 prime once */
108 u_char sc_control;
109 char sc_flags;
110 #define LP_POS_INIT 0x01 /* if we are a postive init signal */
111 #define LP_POS_ACK 0x02 /* if we are a positive going ack */
112 #define LP_NO_PRIME 0x04 /* don't prime the printer at all */
113 #define LP_PRIMEOPEN 0x08 /* prime on every open */
114 #define LP_AUTOLF 0x10 /* tell printer to do an automatic lf */
115 #define LP_BYPASS 0x20 /* bypass printer ready checks */
116 struct buf *sc_inbuf;
117 short sc_xfercnt ;
118 char sc_primed;
119 char *sc_cp ;
120 } lpt_sc[NLPT] ;
121
122 /* bits for state */
123 #define OPEN (1<<0) /* device is open */
124 #define ASLP (1<<1) /* awaiting draining of printer */
125 #define ERROR (1<<2) /* error was received from printer */
126 #define OBUSY (1<<3) /* printer is busy doing output */
127 #define LPTOUT (1<<4) /* timeout while not selected */
128 #define TOUT (1<<5) /* timeout while not selected */
129 #define INIT (1<<6) /* waiting to initialize for open */
130
131 /*
132 * Internal routine to lptprobe to do port tests of one byte value
133 */
134 int
135 lpt_port_test(short port, u_char data, u_char mask)
136 {
137 int temp, timeout;
138
139 data = data & mask;
140 outb(port, data);
141 timeout = 100;
142 do
143 temp = inb(port) & mask;
144 while (temp != data && --timeout);
145 lprintf("Port 0x%x\tout=%x\tin=%x\n", port, data, temp);
146 return (temp == data);
147 }
148
149 /*
150 * New lptprobe routine written by Rodney W. Grimes, 3/25/1993
151 *
152 * Logic:
153 * 1) You should be able to write to and read back the same value
154 * to the data port. Do an alternating zeros, alternating ones,
155 * walking zero, and walking one test to check for stuck bits.
156 *
157 * 2) You should be able to write to and read back the same value
158 * to the control port lower 5 bits, the upper 3 bits are reserved
159 * per the IBM PC technical reference manauls and different boards
160 * do different things with them. Do an alternating zeros, alternating
161 * ones, walking zero, and walking one test to check for stuck bits.
162 *
163 * Some printers drag the strobe line down when the are powered off
164 * so this bit has been masked out of the control port test.
165 *
166 * XXX Some printers may not like a fast pulse on init or strobe, I
167 * don't know at this point, if that becomes a problem these bits
168 * should be turned off in the mask byte for the control port test.
169 *
170 * 3) Set the data and control ports to a value of 0
171 */
172
173 int
174 lptprobe(struct isa_device *dvp)
175 {
176 int status;
177 short port;
178 u_char data;
179 u_char mask;
180 int i;
181
182 status = IO_LPTSIZE;
183
184 port = dvp->id_iobase + lpt_data;
185 mask = 0xff;
186 while (mask != 0)
187 {
188 data = 0x55; /* Alternating zeros */
189 if (!lpt_port_test(port, data, mask)) status = 0;
190
191 data = 0xaa; /* Alternating ones */
192 if (!lpt_port_test(port, data, mask)) status = 0;
193
194 for (i = 0; i < 8; i++) /* Walking zero */
195 {
196 data = ~(1 << i);
197 if (!lpt_port_test(port, data, mask)) status = 0;
198 }
199
200 for (i = 0; i < 8; i++) /* Walking one */
201 {
202 data = (1 << i);
203 if (!lpt_port_test(port, data, mask)) status = 0;
204 }
205
206 if (port == dvp->id_iobase + lpt_data)
207 {
208 port = dvp->id_iobase + lpt_control;
209 mask = 0x1e;
210 }
211 else
212 mask = 0;
213 }
214 outb(dvp->id_iobase+lpt_data, 0);
215 outb(dvp->id_iobase+lpt_control, 0);
216 return (status);
217 }
218
219 lptattach(isdp)
220 struct isa_device *isdp;
221 {
222 struct lpt_softc *sc;
223
224 sc = lpt_sc + isdp->id_unit;
225 sc->sc_port = isdp->id_iobase;
226 sc->sc_state = 0;
227 outb(sc->sc_port+lpt_control, LPC_NINIT);
228 return (1);
229 }
230
231 /*
232 * lptopen -- reset the printer, then wait until it's selected and not busy.
233 */
234
235 lptopen(dev, flag)
236 dev_t dev;
237 int flag;
238 {
239 struct lpt_softc *sc = lpt_sc + LPTUNIT(minor(dev));
240 int s;
241 int trys, port;
242
243 if (sc->sc_state) {
244 lprintf("lp: still open %x\n", sc->sc_state);
245 return(EBUSY);
246 } else sc->sc_state |= INIT;
247
248 s = spltty();
249 sc->sc_flags = LPTFLAGS(minor(dev));
250 lprintf("lp flags 0x%x\n", sc->sc_flags);
251 port = sc->sc_port;
252
253 /* init printer */
254 if((sc->sc_flags & LP_NO_PRIME) == 0) {
255 if((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) {
256 outb(port+lpt_control, 0);
257 sc->sc_primed++;
258 DELAY(500);
259 }
260 }
261 outb(port+lpt_control, LPC_SEL|LPC_NINIT);
262
263 /* wait till ready (printer running diagnostics) */
264 trys = 0;
265 do {
266 /* ran out of waiting for the printer */
267 if (trys++ >= LPINITRDY*4) {
268 splx(s);
269 sc->sc_state = 0;
270 lprintf ("status %x\n", inb(port+lpt_status) );
271 return (EBUSY);
272 }
273
274 /* wait 1/4 second, give up if we get a signal */
275 if (tsleep((caddr_t)sc,
276 LPPRI|PCATCH, "lptinit", hz/4) != EWOULDBLOCK) {
277 sc->sc_state = 0;
278 splx(s);
279 return (EBUSY);
280 }
281
282 /* is printer online and ready for output */
283 } while ((inb(port+lpt_status) & (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
284 (LPS_SEL|LPS_NBSY|LPS_NERR));
285
286 if(sc->sc_flags&LP_AUTOLF) {
287 outb(port+lpt_control, LPC_SEL|LPC_NINIT|LPC_ENA|LPC_AUTOL);
288 sc->sc_control = LPC_SEL|LPC_NINIT|LPC_ENA|LPC_AUTOL;
289 } else {
290 outb(port+lpt_control, LPC_SEL|LPC_NINIT|LPC_ENA);
291 sc->sc_control = LPC_SEL|LPC_NINIT|LPC_ENA;
292 }
293
294 sc->sc_state = OPEN | TOUT;
295 sc->sc_inbuf = geteblk(BUFSIZE);
296 sc->sc_xfercnt = 0;
297 splx(s);
298 timeout((timeout_t)lptout, (caddr_t)sc, hz/2);
299 lprintf("opened.\n");
300 return(0);
301 }
302
303 lptout (sc)
304 struct lpt_softc *sc;
305 { int pl;
306
307 lprintf ("T %x ", inb(sc->sc_port+lpt_status));
308 if (sc->sc_state&OPEN)
309 timeout((timeout_t)lptout, (caddr_t)sc, hz/2);
310 else sc->sc_state &= ~TOUT;
311
312 if (sc->sc_state & ERROR)
313 sc->sc_state &= ~ERROR;
314
315 /*
316 * Avoid possible hangs do to missed interrupts
317 */
318 if (sc->sc_xfercnt) {
319 pl = spltty();
320 lptintr(sc - lpt_sc);
321 splx(pl);
322 } else {
323 sc->sc_state &= ~OBUSY;
324 wakeup((caddr_t)sc);
325 }
326 }
327
328 /*
329 * lptclose -- close the device, free the local line buffer.
330 */
331
332 lptclose(dev, flag)
333 int flag;
334 {
335 struct lpt_softc *sc = lpt_sc + LPTUNIT(minor(dev));
336 int port = sc->sc_port;
337
338 sc->sc_state &= ~OPEN;
339 while ((inb(port+lpt_status) & (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
340 (LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt)
341 /* wait 1/4 second, give up if we get a signal */
342 if (tsleep((caddr_t)sc,
343 LPPRI|PCATCH, "lpclose", hz) != EWOULDBLOCK)
344 break;
345
346 sc->sc_state = 0;
347 sc->sc_xfercnt = 0;
348 outb(sc->sc_port+lpt_control, LPC_NINIT);
349 brelse(sc->sc_inbuf);
350 lprintf("closed.\n");
351 return(0);
352 }
353
354 /*
355 * lptwrite --copy a line from user space to a local buffer, then call
356 * putc to get the chars moved to the output queue.
357 */
358
359 lptwrite(dev, uio)
360 dev_t dev;
361 struct uio *uio;
362 {
363 register unsigned n;
364 int pl, err;
365 struct lpt_softc *sc = lpt_sc + LPTUNIT(minor(dev));
366
367 while (n = MIN(BUFSIZE, uio->uio_resid)) {
368 sc->sc_cp = sc->sc_inbuf->b_un.b_addr ;
369 uiomove(sc->sc_cp, n, uio);
370 sc->sc_xfercnt = n ;
371 while (sc->sc_xfercnt > 0) {
372 /* if the printer is ready for a char, give it one */
373 if ((sc->sc_state & OBUSY) == 0){
374 lprintf("\nC %d. ", sc->sc_xfercnt);
375 pl = spltty();
376 lptintr(sc - lpt_sc);
377 (void) splx(pl);
378 }
379 lprintf("W ");
380 if (err = tsleep((caddr_t)sc,
381 LPPRI|PCATCH, "lpwrite", 0))
382 return(err);
383 }
384 }
385 return(0);
386 }
387
388 /*
389 * lptintr -- handle printer interrupts which occur when the printer is
390 * ready to accept another char.
391 */
392
393 lptintr(unit)
394 {
395 struct lpt_softc *sc = lpt_sc + unit;
396 int port = sc->sc_port,sts;
397
398 sts = inb(port+lpt_status);
399
400 if (!sc->sc_state) {
401 printf ("lpt%d: stray interrupt sts=0x%02x\n", unit, sts);
402 return;
403 }
404
405 /* is printer online and ready for output */
406 if ((sts & (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR/*|LPS_NACK*/)) ==
407 (LPS_SEL|LPS_NBSY|LPS_NERR)) {
408 /* is this a false interrupt ? */
409 if ((sc->sc_state & OBUSY)
410 && (sts & LPS_NACK) == 0) return;
411 sc->sc_state |= OBUSY; sc->sc_state &= ~ERROR;
412
413 if (sc->sc_xfercnt) {
414 /* send char */
415 /*lprintf("%x ", *sc->sc_cp); */
416 outb(port+lpt_data, *sc->sc_cp++) ; sc->sc_xfercnt-- ;
417 outb(port+lpt_control, sc->sc_control|LPC_STB);
418 /* DELAY(X) */
419 outb(port+lpt_control, sc->sc_control);
420 }
421
422 /* any more bytes for the printer? */
423 if (sc->sc_xfercnt > 0) return;
424
425 /* none, wake up the top half to get more */
426 sc->sc_state &= ~OBUSY;
427 wakeup((caddr_t)sc);
428 lprintf("w ");
429 return;
430 } else sc->sc_state |= ERROR;
431 lprintf("sts %x ", sts);
432 }
433
434 int
435 lptioctl(dev_t dev, int cmd, caddr_t data, int flag)
436 {
437 int error;
438
439 error = 0;
440 switch (cmd) {
441 #ifdef THISISASAMPLE
442 case XXX:
443 dothis; andthis; andthat;
444 error=x;
445 break;
446 #endif /* THISISASAMPLE */
447 default:
448 error = ENODEV;
449 }
450
451 return(error);
452 }
453
454 #endif /* NLPT */
455