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