lpt.c revision 1.53 1 1.53 thorpej /* $NetBSD: lpt.c,v 1.53 1999/02/12 01:51:37 thorpej Exp $ */
2 1.23 cgd
3 1.1 cgd /*
4 1.51 mycroft * Copyright (c) 1993, 1994 Charles M. Hannum.
5 1.1 cgd * Copyright (c) 1990 William F. Jolitz, TeleMuse
6 1.1 cgd * All rights reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This software is a component of "386BSD" developed by
19 1.1 cgd * William F. Jolitz, TeleMuse.
20 1.1 cgd * 4. Neither the name of the developer nor the name "386BSD"
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
25 1.1 cgd * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
26 1.1 cgd * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
27 1.1 cgd * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
28 1.1 cgd * NOT MAKE USE OF THIS WORK.
29 1.1 cgd *
30 1.1 cgd * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
31 1.1 cgd * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
32 1.1 cgd * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES
33 1.1 cgd * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
34 1.1 cgd * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
35 1.1 cgd * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
36 1.1 cgd * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
37 1.1 cgd * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
38 1.1 cgd *
39 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
40 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE
43 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 1.1 cgd * SUCH DAMAGE.
50 1.1 cgd */
51 1.1 cgd
52 1.1 cgd /*
53 1.45 is * Device Driver for AT style parallel printer port
54 1.1 cgd */
55 1.1 cgd
56 1.9 mycroft #include <sys/param.h>
57 1.9 mycroft #include <sys/systm.h>
58 1.9 mycroft #include <sys/proc.h>
59 1.9 mycroft #include <sys/user.h>
60 1.47 thorpej #include <sys/malloc.h>
61 1.9 mycroft #include <sys/kernel.h>
62 1.9 mycroft #include <sys/ioctl.h>
63 1.9 mycroft #include <sys/uio.h>
64 1.11 mycroft #include <sys/device.h>
65 1.38 christos #include <sys/conf.h>
66 1.11 mycroft #include <sys/syslog.h>
67 1.1 cgd
68 1.39 mycroft #include <machine/bus.h>
69 1.37 cgd #include <machine/intr.h>
70 1.9 mycroft
71 1.45 is #include <dev/ic/lptreg.h>
72 1.45 is #include <dev/ic/lptvar.h>
73 1.1 cgd
74 1.11 mycroft #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */
75 1.11 mycroft #define STEP hz/4
76 1.11 mycroft
77 1.11 mycroft #define LPTPRI (PZERO+8)
78 1.11 mycroft #define LPT_BSIZE 1024
79 1.1 cgd
80 1.45 is #define LPTDEBUG
81 1.45 is
82 1.44 mikel #ifndef LPTDEBUG
83 1.38 christos #define LPRINTF(a)
84 1.1 cgd #else
85 1.44 mikel #define LPRINTF(a) if (lptdebug) printf a
86 1.44 mikel int lptdebug = 0;
87 1.1 cgd #endif
88 1.1 cgd
89 1.38 christos /* XXX does not belong here */
90 1.38 christos cdev_decl(lpt);
91 1.38 christos
92 1.48 thorpej extern struct cfdriver lpt_cd;
93 1.1 cgd
94 1.11 mycroft #define LPTUNIT(s) (minor(s) & 0x1f)
95 1.11 mycroft #define LPTFLAGS(s) (minor(s) & 0xe0)
96 1.1 cgd
97 1.15 mycroft void
98 1.45 is lpt_attach_subr(sc)
99 1.45 is struct lpt_softc *sc;
100 1.1 cgd {
101 1.42 thorpej bus_space_tag_t iot;
102 1.42 thorpej bus_space_handle_t ioh;
103 1.15 mycroft
104 1.6 mycroft sc->sc_state = 0;
105 1.32 cgd
106 1.45 is iot = sc->sc_iot;
107 1.45 is ioh = sc->sc_ioh;
108 1.32 cgd
109 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
110 1.49 cgd
111 1.49 cgd sc->sc_dev_ok = 1;
112 1.1 cgd }
113 1.1 cgd
114 1.1 cgd /*
115 1.11 mycroft * Reset the printer, then wait until it's selected and not busy.
116 1.1 cgd */
117 1.11 mycroft int
118 1.38 christos lptopen(dev, flag, mode, p)
119 1.1 cgd dev_t dev;
120 1.1 cgd int flag;
121 1.38 christos int mode;
122 1.38 christos struct proc *p;
123 1.1 cgd {
124 1.11 mycroft int unit = LPTUNIT(dev);
125 1.11 mycroft u_char flags = LPTFLAGS(dev);
126 1.11 mycroft struct lpt_softc *sc;
127 1.42 thorpej bus_space_tag_t iot;
128 1.42 thorpej bus_space_handle_t ioh;
129 1.11 mycroft u_char control;
130 1.11 mycroft int error;
131 1.11 mycroft int spin;
132 1.11 mycroft
133 1.33 thorpej if (unit >= lpt_cd.cd_ndevs)
134 1.11 mycroft return ENXIO;
135 1.33 thorpej sc = lpt_cd.cd_devs[unit];
136 1.49 cgd if (!sc || !sc->sc_dev_ok)
137 1.11 mycroft return ENXIO;
138 1.11 mycroft
139 1.45 is #if 0 /* XXX what to do? */
140 1.25 mycroft if (sc->sc_irq == IRQUNK && (flags & LPT_NOINTR) == 0)
141 1.11 mycroft return ENXIO;
142 1.45 is #endif
143 1.11 mycroft
144 1.11 mycroft #ifdef DIAGNOSTIC
145 1.11 mycroft if (sc->sc_state)
146 1.41 christos printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
147 1.11 mycroft sc->sc_state);
148 1.11 mycroft #endif
149 1.11 mycroft
150 1.11 mycroft if (sc->sc_state)
151 1.11 mycroft return EBUSY;
152 1.1 cgd
153 1.11 mycroft sc->sc_state = LPT_INIT;
154 1.11 mycroft sc->sc_flags = flags;
155 1.44 mikel LPRINTF(("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname,
156 1.44 mikel (unsigned)flags));
157 1.42 thorpej iot = sc->sc_iot;
158 1.32 cgd ioh = sc->sc_ioh;
159 1.11 mycroft
160 1.11 mycroft if ((flags & LPT_NOPRIME) == 0) {
161 1.11 mycroft /* assert INIT for 100 usec to start up printer */
162 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, LPC_SELECT);
163 1.14 mycroft delay(100);
164 1.1 cgd }
165 1.11 mycroft
166 1.11 mycroft control = LPC_SELECT | LPC_NINIT;
167 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, control);
168 1.1 cgd
169 1.1 cgd /* wait till ready (printer running diagnostics) */
170 1.25 mycroft for (spin = 0; NOT_READY_ERR(); spin += STEP) {
171 1.11 mycroft if (spin >= TIMEOUT) {
172 1.1 cgd sc->sc_state = 0;
173 1.11 mycroft return EBUSY;
174 1.1 cgd }
175 1.1 cgd
176 1.1 cgd /* wait 1/4 second, give up if we get a signal */
177 1.38 christos error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen", STEP);
178 1.38 christos if (error != EWOULDBLOCK) {
179 1.1 cgd sc->sc_state = 0;
180 1.11 mycroft return error;
181 1.1 cgd }
182 1.1 cgd }
183 1.1 cgd
184 1.11 mycroft if ((flags & LPT_NOINTR) == 0)
185 1.11 mycroft control |= LPC_IENABLE;
186 1.11 mycroft if (flags & LPT_AUTOLF)
187 1.11 mycroft control |= LPC_AUTOLF;
188 1.11 mycroft sc->sc_control = control;
189 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, control);
190 1.11 mycroft
191 1.47 thorpej sc->sc_inbuf = malloc(LPT_BSIZE, M_DEVBUF, M_WAITOK);
192 1.11 mycroft sc->sc_count = 0;
193 1.11 mycroft sc->sc_state = LPT_OPEN;
194 1.25 mycroft
195 1.25 mycroft if ((sc->sc_flags & LPT_NOINTR) == 0)
196 1.25 mycroft lptwakeup(sc);
197 1.11 mycroft
198 1.38 christos LPRINTF(("%s: opened\n", sc->sc_dev.dv_xname));
199 1.11 mycroft return 0;
200 1.1 cgd }
201 1.1 cgd
202 1.11 mycroft int
203 1.46 is lptnotready(status, sc)
204 1.11 mycroft u_char status;
205 1.1 cgd struct lpt_softc *sc;
206 1.11 mycroft {
207 1.21 mycroft u_char new;
208 1.21 mycroft
209 1.25 mycroft status = (status ^ LPS_INVERT) & LPS_MASK;
210 1.21 mycroft new = status & ~sc->sc_laststatus;
211 1.21 mycroft sc->sc_laststatus = status;
212 1.1 cgd
213 1.52 perry if (sc->sc_state & LPT_OPEN) {
214 1.52 perry if (new & LPS_SELECT)
215 1.52 perry log(LOG_NOTICE,
216 1.52 perry "%s: offline\n", sc->sc_dev.dv_xname);
217 1.52 perry else if (new & LPS_NOPAPER)
218 1.52 perry log(LOG_NOTICE,
219 1.52 perry "%s: out of paper\n", sc->sc_dev.dv_xname);
220 1.52 perry else if (new & LPS_NERR)
221 1.52 perry log(LOG_NOTICE,
222 1.52 perry "%s: output error\n", sc->sc_dev.dv_xname);
223 1.52 perry }
224 1.52 perry
225 1.25 mycroft return status;
226 1.11 mycroft }
227 1.11 mycroft
228 1.11 mycroft void
229 1.25 mycroft lptwakeup(arg)
230 1.18 cgd void *arg;
231 1.18 cgd {
232 1.25 mycroft struct lpt_softc *sc = arg;
233 1.11 mycroft int s;
234 1.11 mycroft
235 1.50 is s = spllpt();
236 1.16 mycroft lptintr(sc);
237 1.13 mycroft splx(s);
238 1.11 mycroft
239 1.25 mycroft timeout(lptwakeup, sc, STEP);
240 1.1 cgd }
241 1.1 cgd
242 1.1 cgd /*
243 1.11 mycroft * Close the device, and free the local line buffer.
244 1.1 cgd */
245 1.36 mycroft int
246 1.38 christos lptclose(dev, flag, mode, p)
247 1.11 mycroft dev_t dev;
248 1.1 cgd int flag;
249 1.38 christos int mode;
250 1.38 christos struct proc *p;
251 1.1 cgd {
252 1.11 mycroft int unit = LPTUNIT(dev);
253 1.33 thorpej struct lpt_softc *sc = lpt_cd.cd_devs[unit];
254 1.42 thorpej bus_space_tag_t iot = sc->sc_iot;
255 1.42 thorpej bus_space_handle_t ioh = sc->sc_ioh;
256 1.1 cgd
257 1.11 mycroft if (sc->sc_count)
258 1.46 is (void) lptpushbytes(sc);
259 1.1 cgd
260 1.25 mycroft if ((sc->sc_flags & LPT_NOINTR) == 0)
261 1.25 mycroft untimeout(lptwakeup, sc);
262 1.25 mycroft
263 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
264 1.1 cgd sc->sc_state = 0;
265 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
266 1.47 thorpej free(sc->sc_inbuf, M_DEVBUF);
267 1.11 mycroft
268 1.38 christos LPRINTF(("%s: closed\n", sc->sc_dev.dv_xname));
269 1.11 mycroft return 0;
270 1.11 mycroft }
271 1.11 mycroft
272 1.11 mycroft int
273 1.46 is lptpushbytes(sc)
274 1.11 mycroft struct lpt_softc *sc;
275 1.11 mycroft {
276 1.42 thorpej bus_space_tag_t iot = sc->sc_iot;
277 1.42 thorpej bus_space_handle_t ioh = sc->sc_ioh;
278 1.11 mycroft int error;
279 1.11 mycroft
280 1.11 mycroft if (sc->sc_flags & LPT_NOINTR) {
281 1.11 mycroft int spin, tic;
282 1.11 mycroft u_char control = sc->sc_control;
283 1.11 mycroft
284 1.11 mycroft while (sc->sc_count > 0) {
285 1.11 mycroft spin = 0;
286 1.25 mycroft while (NOT_READY()) {
287 1.25 mycroft if (++spin < sc->sc_spinmax)
288 1.25 mycroft continue;
289 1.11 mycroft tic = 0;
290 1.12 mycroft /* adapt busy-wait algorithm */
291 1.12 mycroft sc->sc_spinmax++;
292 1.25 mycroft while (NOT_READY_ERR()) {
293 1.11 mycroft /* exponential backoff */
294 1.11 mycroft tic = tic + tic + 1;
295 1.11 mycroft if (tic > TIMEOUT)
296 1.11 mycroft tic = TIMEOUT;
297 1.11 mycroft error = tsleep((caddr_t)sc,
298 1.11 mycroft LPTPRI | PCATCH, "lptpsh", tic);
299 1.11 mycroft if (error != EWOULDBLOCK)
300 1.11 mycroft return error;
301 1.11 mycroft }
302 1.25 mycroft break;
303 1.11 mycroft }
304 1.11 mycroft
305 1.42 thorpej bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
306 1.44 mikel bus_space_write_1(iot, ioh, lpt_control,
307 1.44 mikel control | LPC_STROBE);
308 1.11 mycroft sc->sc_count--;
309 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, control);
310 1.11 mycroft
311 1.11 mycroft /* adapt busy-wait algorithm */
312 1.25 mycroft if (spin*2 + 16 < sc->sc_spinmax)
313 1.12 mycroft sc->sc_spinmax--;
314 1.11 mycroft }
315 1.11 mycroft } else {
316 1.11 mycroft int s;
317 1.11 mycroft
318 1.11 mycroft while (sc->sc_count > 0) {
319 1.11 mycroft /* if the printer is ready for a char, give it one */
320 1.11 mycroft if ((sc->sc_state & LPT_OBUSY) == 0) {
321 1.53 thorpej LPRINTF(("%s: write %lu\n", sc->sc_dev.dv_xname,
322 1.53 thorpej (u_long)sc->sc_count));
323 1.50 is s = spllpt();
324 1.16 mycroft (void) lptintr(sc);
325 1.11 mycroft splx(s);
326 1.11 mycroft }
327 1.38 christos error = tsleep((caddr_t)sc, LPTPRI | PCATCH,
328 1.38 christos "lptwrite2", 0);
329 1.38 christos if (error)
330 1.11 mycroft return error;
331 1.11 mycroft }
332 1.11 mycroft }
333 1.11 mycroft return 0;
334 1.1 cgd }
335 1.1 cgd
336 1.1 cgd /*
337 1.11 mycroft * Copy a line from user space to a local buffer, then call putc to get the
338 1.11 mycroft * chars moved to the output queue.
339 1.1 cgd */
340 1.36 mycroft int
341 1.38 christos lptwrite(dev, uio, flags)
342 1.1 cgd dev_t dev;
343 1.1 cgd struct uio *uio;
344 1.38 christos int flags;
345 1.1 cgd {
346 1.33 thorpej struct lpt_softc *sc = lpt_cd.cd_devs[LPTUNIT(dev)];
347 1.11 mycroft size_t n;
348 1.11 mycroft int error = 0;
349 1.11 mycroft
350 1.38 christos while ((n = min(LPT_BSIZE, uio->uio_resid)) != 0) {
351 1.47 thorpej uiomove(sc->sc_cp = sc->sc_inbuf, n, uio);
352 1.11 mycroft sc->sc_count = n;
353 1.46 is error = lptpushbytes(sc);
354 1.11 mycroft if (error) {
355 1.11 mycroft /*
356 1.11 mycroft * Return accurate residual if interrupted or timed
357 1.11 mycroft * out.
358 1.11 mycroft */
359 1.11 mycroft uio->uio_resid += sc->sc_count;
360 1.11 mycroft sc->sc_count = 0;
361 1.11 mycroft return error;
362 1.1 cgd }
363 1.1 cgd }
364 1.11 mycroft return 0;
365 1.1 cgd }
366 1.1 cgd
367 1.1 cgd /*
368 1.11 mycroft * Handle printer interrupts which occur when the printer is ready to accept
369 1.11 mycroft * another char.
370 1.1 cgd */
371 1.11 mycroft int
372 1.30 cgd lptintr(arg)
373 1.30 cgd void *arg;
374 1.1 cgd {
375 1.30 cgd struct lpt_softc *sc = arg;
376 1.42 thorpej bus_space_tag_t iot = sc->sc_iot;
377 1.42 thorpej bus_space_handle_t ioh = sc->sc_ioh;
378 1.1 cgd
379 1.13 mycroft #if 0
380 1.11 mycroft if ((sc->sc_state & LPT_OPEN) == 0)
381 1.11 mycroft return 0;
382 1.13 mycroft #endif
383 1.6 mycroft
384 1.11 mycroft /* is printer online and ready for output */
385 1.25 mycroft if (NOT_READY() && NOT_READY_ERR())
386 1.11 mycroft return 0;
387 1.6 mycroft
388 1.11 mycroft if (sc->sc_count) {
389 1.13 mycroft u_char control = sc->sc_control;
390 1.11 mycroft /* send char */
391 1.42 thorpej bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
392 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE);
393 1.13 mycroft sc->sc_count--;
394 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, control);
395 1.11 mycroft sc->sc_state |= LPT_OBUSY;
396 1.11 mycroft } else
397 1.11 mycroft sc->sc_state &= ~LPT_OBUSY;
398 1.1 cgd
399 1.11 mycroft if (sc->sc_count == 0) {
400 1.1 cgd /* none, wake up the top half to get more */
401 1.1 cgd wakeup((caddr_t)sc);
402 1.11 mycroft }
403 1.11 mycroft
404 1.11 mycroft return 1;
405 1.1 cgd }
406 1.1 cgd
407 1.2 cgd int
408 1.38 christos lptioctl(dev, cmd, data, flag, p)
409 1.11 mycroft dev_t dev;
410 1.24 cgd u_long cmd;
411 1.11 mycroft caddr_t data;
412 1.11 mycroft int flag;
413 1.38 christos struct proc *p;
414 1.2 cgd {
415 1.11 mycroft int error = 0;
416 1.2 cgd
417 1.2 cgd switch (cmd) {
418 1.2 cgd default:
419 1.2 cgd error = ENODEV;
420 1.2 cgd }
421 1.2 cgd
422 1.11 mycroft return error;
423 1.2 cgd }
424