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