lpt.c revision 1.45 1 /* $NetBSD: lpt.c,v 1.45 1997/09/27 22:44:15 is Exp $ */
2
3 /*
4 * Copyright (c) 1993, 1994 Charles 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/buf.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
68 #include <machine/bus.h>
69 #include <machine/intr.h>
70
71 #include <dev/ic/lptreg.h>
72 #include <dev/ic/lptvar.h>
73
74 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */
75 #define STEP hz/4
76
77 #define LPTPRI (PZERO+8)
78 #define LPT_BSIZE 1024
79
80 #define LPTDEBUG
81
82 #ifndef LPTDEBUG
83 #define LPRINTF(a)
84 #else
85 #define LPRINTF(a) if (lptdebug) printf a
86 int lptdebug = 0;
87 #endif
88
89 /* XXX does not belong here */
90 cdev_decl(lpt);
91
92 struct cfdriver lpt_cd = {
93 NULL, "lpt", DV_TTY
94 };
95
96 #define LPTUNIT(s) (minor(s) & 0x1f)
97 #define LPTFLAGS(s) (minor(s) & 0xe0)
98
99 #define LPS_INVERT (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK)
100 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER)
101 #define NOT_READY() ((bus_space_read_1(iot, ioh, lpt_status) ^ LPS_INVERT) & LPS_MASK)
102 #define NOT_READY_ERR() not_ready(bus_space_read_1(iot, ioh, lpt_status), sc)
103 static int not_ready __P((u_char, struct lpt_softc *));
104
105 static void lptwakeup __P((void *arg));
106 static int pushbytes __P((struct lpt_softc *));
107
108 void
109 lpt_attach_subr(sc)
110 struct lpt_softc *sc;
111 {
112 bus_space_tag_t iot;
113 bus_space_handle_t ioh;
114
115 sc->sc_state = 0;
116
117 iot = sc->sc_iot;
118 ioh = sc->sc_ioh;
119
120 bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
121 }
122
123 /*
124 * Reset the printer, then wait until it's selected and not busy.
125 */
126 int
127 lptopen(dev, flag, mode, p)
128 dev_t dev;
129 int flag;
130 int mode;
131 struct proc *p;
132 {
133 int unit = LPTUNIT(dev);
134 u_char flags = LPTFLAGS(dev);
135 struct lpt_softc *sc;
136 bus_space_tag_t iot;
137 bus_space_handle_t ioh;
138 u_char control;
139 int error;
140 int spin;
141
142 if (unit >= lpt_cd.cd_ndevs)
143 return ENXIO;
144 sc = lpt_cd.cd_devs[unit];
145 if (!sc)
146 return ENXIO;
147
148 #if 0 /* XXX what to do? */
149 if (sc->sc_irq == IRQUNK && (flags & LPT_NOINTR) == 0)
150 return ENXIO;
151 #endif
152
153 #ifdef DIAGNOSTIC
154 if (sc->sc_state)
155 printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
156 sc->sc_state);
157 #endif
158
159 if (sc->sc_state)
160 return EBUSY;
161
162 sc->sc_state = LPT_INIT;
163 sc->sc_flags = flags;
164 LPRINTF(("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname,
165 (unsigned)flags));
166 iot = sc->sc_iot;
167 ioh = sc->sc_ioh;
168
169 if ((flags & LPT_NOPRIME) == 0) {
170 /* assert INIT for 100 usec to start up printer */
171 bus_space_write_1(iot, ioh, lpt_control, LPC_SELECT);
172 delay(100);
173 }
174
175 control = LPC_SELECT | LPC_NINIT;
176 bus_space_write_1(iot, ioh, lpt_control, control);
177
178 /* wait till ready (printer running diagnostics) */
179 for (spin = 0; NOT_READY_ERR(); spin += STEP) {
180 if (spin >= TIMEOUT) {
181 sc->sc_state = 0;
182 return EBUSY;
183 }
184
185 /* wait 1/4 second, give up if we get a signal */
186 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen", STEP);
187 if (error != EWOULDBLOCK) {
188 sc->sc_state = 0;
189 return error;
190 }
191 }
192
193 if ((flags & LPT_NOINTR) == 0)
194 control |= LPC_IENABLE;
195 if (flags & LPT_AUTOLF)
196 control |= LPC_AUTOLF;
197 sc->sc_control = control;
198 bus_space_write_1(iot, ioh, lpt_control, control);
199
200 sc->sc_inbuf = geteblk(LPT_BSIZE);
201 sc->sc_count = 0;
202 sc->sc_state = LPT_OPEN;
203
204 if ((sc->sc_flags & LPT_NOINTR) == 0)
205 lptwakeup(sc);
206
207 LPRINTF(("%s: opened\n", sc->sc_dev.dv_xname));
208 return 0;
209 }
210
211 int
212 not_ready(status, sc)
213 u_char status;
214 struct lpt_softc *sc;
215 {
216 u_char new;
217
218 status = (status ^ LPS_INVERT) & LPS_MASK;
219 new = status & ~sc->sc_laststatus;
220 sc->sc_laststatus = status;
221
222 if (new & LPS_SELECT)
223 log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
224 else if (new & LPS_NOPAPER)
225 log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
226 else if (new & LPS_NERR)
227 log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
228
229 return status;
230 }
231
232 void
233 lptwakeup(arg)
234 void *arg;
235 {
236 struct lpt_softc *sc = arg;
237 int s;
238
239 s = spltty();
240 lptintr(sc);
241 splx(s);
242
243 timeout(lptwakeup, sc, STEP);
244 }
245
246 /*
247 * Close the device, and free the local line buffer.
248 */
249 int
250 lptclose(dev, flag, mode, p)
251 dev_t dev;
252 int flag;
253 int mode;
254 struct proc *p;
255 {
256 int unit = LPTUNIT(dev);
257 struct lpt_softc *sc = lpt_cd.cd_devs[unit];
258 bus_space_tag_t iot = sc->sc_iot;
259 bus_space_handle_t ioh = sc->sc_ioh;
260
261 if (sc->sc_count)
262 (void) pushbytes(sc);
263
264 if ((sc->sc_flags & LPT_NOINTR) == 0)
265 untimeout(lptwakeup, sc);
266
267 bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
268 sc->sc_state = 0;
269 bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
270 brelse(sc->sc_inbuf);
271
272 LPRINTF(("%s: closed\n", sc->sc_dev.dv_xname));
273 return 0;
274 }
275
276 int
277 pushbytes(sc)
278 struct lpt_softc *sc;
279 {
280 bus_space_tag_t iot = sc->sc_iot;
281 bus_space_handle_t ioh = sc->sc_ioh;
282 int error;
283
284 if (sc->sc_flags & LPT_NOINTR) {
285 int spin, tic;
286 u_char control = sc->sc_control;
287
288 while (sc->sc_count > 0) {
289 spin = 0;
290 while (NOT_READY()) {
291 if (++spin < sc->sc_spinmax)
292 continue;
293 tic = 0;
294 /* adapt busy-wait algorithm */
295 sc->sc_spinmax++;
296 while (NOT_READY_ERR()) {
297 /* exponential backoff */
298 tic = tic + tic + 1;
299 if (tic > TIMEOUT)
300 tic = TIMEOUT;
301 error = tsleep((caddr_t)sc,
302 LPTPRI | PCATCH, "lptpsh", tic);
303 if (error != EWOULDBLOCK)
304 return error;
305 }
306 break;
307 }
308
309 bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
310 bus_space_write_1(iot, ioh, lpt_control,
311 control | LPC_STROBE);
312 sc->sc_count--;
313 bus_space_write_1(iot, ioh, lpt_control, control);
314
315 /* adapt busy-wait algorithm */
316 if (spin*2 + 16 < sc->sc_spinmax)
317 sc->sc_spinmax--;
318 }
319 } else {
320 int s;
321
322 while (sc->sc_count > 0) {
323 /* if the printer is ready for a char, give it one */
324 if ((sc->sc_state & LPT_OBUSY) == 0) {
325 LPRINTF(("%s: write %u\n", sc->sc_dev.dv_xname,
326 sc->sc_count));
327 s = spltty();
328 (void) lptintr(sc);
329 splx(s);
330 }
331 error = tsleep((caddr_t)sc, LPTPRI | PCATCH,
332 "lptwrite2", 0);
333 if (error)
334 return error;
335 }
336 }
337 return 0;
338 }
339
340 /*
341 * Copy a line from user space to a local buffer, then call putc to get the
342 * chars moved to the output queue.
343 */
344 int
345 lptwrite(dev, uio, flags)
346 dev_t dev;
347 struct uio *uio;
348 int flags;
349 {
350 struct lpt_softc *sc = lpt_cd.cd_devs[LPTUNIT(dev)];
351 size_t n;
352 int error = 0;
353
354 while ((n = min(LPT_BSIZE, uio->uio_resid)) != 0) {
355 uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio);
356 sc->sc_count = n;
357 error = pushbytes(sc);
358 if (error) {
359 /*
360 * Return accurate residual if interrupted or timed
361 * out.
362 */
363 uio->uio_resid += sc->sc_count;
364 sc->sc_count = 0;
365 return error;
366 }
367 }
368 return 0;
369 }
370
371 /*
372 * Handle printer interrupts which occur when the printer is ready to accept
373 * another char.
374 */
375 int
376 lptintr(arg)
377 void *arg;
378 {
379 struct lpt_softc *sc = arg;
380 bus_space_tag_t iot = sc->sc_iot;
381 bus_space_handle_t ioh = sc->sc_ioh;
382
383 #if 0
384 if ((sc->sc_state & LPT_OPEN) == 0)
385 return 0;
386 #endif
387
388 /* is printer online and ready for output */
389 if (NOT_READY() && NOT_READY_ERR())
390 return 0;
391
392 if (sc->sc_count) {
393 u_char control = sc->sc_control;
394 /* send char */
395 bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
396 bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE);
397 sc->sc_count--;
398 bus_space_write_1(iot, ioh, lpt_control, control);
399 sc->sc_state |= LPT_OBUSY;
400 } else
401 sc->sc_state &= ~LPT_OBUSY;
402
403 if (sc->sc_count == 0) {
404 /* none, wake up the top half to get more */
405 wakeup((caddr_t)sc);
406 }
407
408 return 1;
409 }
410
411 int
412 lptioctl(dev, cmd, data, flag, p)
413 dev_t dev;
414 u_long cmd;
415 caddr_t data;
416 int flag;
417 struct proc *p;
418 {
419 int error = 0;
420
421 switch (cmd) {
422 default:
423 error = ENODEV;
424 }
425
426 return error;
427 }
428