lpt.c revision 1.72.16.3 1 /* $NetBSD: lpt.c,v 1.72.16.3 2008/04/06 09:58:50 mjf 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/cdefs.h>
57 __KERNEL_RCSID(0, "$NetBSD: lpt.c,v 1.72.16.3 2008/04/06 09:58:50 mjf Exp $");
58
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/proc.h>
62 #include <sys/user.h>
63 #include <sys/malloc.h>
64 #include <sys/kernel.h>
65 #include <sys/ioctl.h>
66 #include <sys/uio.h>
67 #include <sys/device.h>
68 #include <sys/conf.h>
69 #include <sys/syslog.h>
70 #include <sys/intr.h>
71
72 #include <sys/bus.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 extern struct cfdriver lpt_cd;
93
94 dev_type_open(lptopen);
95 dev_type_close(lptclose);
96 dev_type_write(lptwrite);
97 dev_type_ioctl(lptioctl);
98
99 const struct cdevsw lpt_cdevsw = {
100 lptopen, lptclose, noread, lptwrite, lptioctl,
101 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
102 };
103
104 #define LPTUNIT(s) (minor(s) & 0x1f)
105 #define LPTFLAGS(s) (minor(s) & 0xe0)
106 #define LPA(unit) (unit + 128)
107 #define LPTCTL(unit) (unit + 256)
108
109 static void lptsoftintr(void *);
110
111 void
112 lpt_attach_subr(sc)
113 struct lpt_softc *sc;
114 {
115 int maj, unit;
116 bus_space_tag_t iot;
117 bus_space_handle_t ioh;
118
119 sc->sc_state = 0;
120
121 iot = sc->sc_iot;
122 ioh = sc->sc_ioh;
123
124 bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
125
126 callout_init(&sc->sc_wakeup_ch, 0);
127 sc->sc_sih = softint_establish(SOFTINT_SERIAL, lptsoftintr, sc);
128
129 sc->sc_dev_ok = 1;
130
131 maj = cdevsw_lookup_major(&lpt_cdevsw);
132 unit = device_unit(sc->sc_dev);
133
134 device_register_name(makedev(maj, unit), sc->sc_dev, true,
135 DEV_OTHER, "lpt%d", unit);
136 device_register_name(makedev(maj, LPA(unit)), sc->sc_dev, true,
137 DEV_OTHER, "lpa%d", unit);
138 device_register_name(makedev(maj, LPTCTL(unit)), sc->sc_dev, true,
139 DEV_OTHER, "lpt%dctl", unit);
140 }
141
142 int
143 lpt_detach_subr(device_t self, int flags)
144 {
145 struct lpt_softc *sc = device_private(self);
146
147 device_deregister_all(self);
148 sc->sc_dev_ok = 0;
149 softint_disestablish(sc->sc_sih);
150 callout_destroy(&sc->sc_wakeup_ch);
151 return 0;
152 }
153
154 /*
155 * Reset the printer, then wait until it's selected and not busy.
156 */
157 int
158 lptopen(dev_t dev, int flag, int mode, struct lwp *l)
159 {
160 u_char flags = LPTFLAGS(dev);
161 struct lpt_softc *sc;
162 bus_space_tag_t iot;
163 bus_space_handle_t ioh;
164 u_char control;
165 int error;
166 int spin;
167
168 sc = device_private(device_lookup(&lpt_cd, LPTUNIT(dev)));
169 if (!sc || !sc->sc_dev_ok)
170 return ENXIO;
171
172 #if 0 /* XXX what to do? */
173 if (sc->sc_irq == IRQUNK && (flags & LPT_NOINTR) == 0)
174 return ENXIO;
175 #endif
176
177 #ifdef DIAGNOSTIC
178 if (sc->sc_state)
179 aprint_verbose_dev(sc->sc_dev, "stat=0x%x not zero\n",
180 sc->sc_state);
181 #endif
182
183 if (sc->sc_state)
184 return EBUSY;
185
186 sc->sc_state = LPT_INIT;
187 sc->sc_flags = flags;
188 LPRINTF(("%s: open: flags=0x%x\n", device_xname(sc->sc_dev),
189 (unsigned)flags));
190 iot = sc->sc_iot;
191 ioh = sc->sc_ioh;
192
193 if ((flags & LPT_NOPRIME) == 0) {
194 /* assert INIT for 100 usec to start up printer */
195 bus_space_write_1(iot, ioh, lpt_control, LPC_SELECT);
196 delay(100);
197 }
198
199 control = LPC_SELECT | LPC_NINIT;
200 bus_space_write_1(iot, ioh, lpt_control, control);
201
202 /* wait till ready (printer running diagnostics) */
203 for (spin = 0; NOT_READY_ERR(); spin += STEP) {
204 if (spin >= TIMEOUT) {
205 sc->sc_state = 0;
206 return EBUSY;
207 }
208
209 /* wait 1/4 second, give up if we get a signal */
210 error = tsleep((void *)sc, LPTPRI | PCATCH, "lptopen", STEP);
211 if (error != EWOULDBLOCK) {
212 sc->sc_state = 0;
213 return error;
214 }
215 }
216
217 if ((flags & LPT_NOINTR) == 0)
218 control |= LPC_IENABLE;
219 if (flags & LPT_AUTOLF)
220 control |= LPC_AUTOLF;
221 sc->sc_control = control;
222 bus_space_write_1(iot, ioh, lpt_control, control);
223
224 sc->sc_inbuf = malloc(LPT_BSIZE, M_DEVBUF, M_WAITOK);
225 sc->sc_count = 0;
226 sc->sc_state = LPT_OPEN;
227
228 if ((sc->sc_flags & LPT_NOINTR) == 0)
229 lptwakeup(sc);
230
231 LPRINTF(("%s: opened\n", device_xname(sc->sc_dev)));
232 return 0;
233 }
234
235 int
236 lptnotready(status, sc)
237 u_char status;
238 struct lpt_softc *sc;
239 {
240 u_char new;
241
242 status = (status ^ LPS_INVERT) & LPS_MASK;
243 new = status & ~sc->sc_laststatus;
244 sc->sc_laststatus = status;
245
246 if (sc->sc_state & LPT_OPEN) {
247 if (new & LPS_SELECT)
248 log(LOG_NOTICE,
249 "%s: offline\n", device_xname(sc->sc_dev));
250 else if (new & LPS_NOPAPER)
251 log(LOG_NOTICE,
252 "%s: out of paper\n", device_xname(sc->sc_dev));
253 else if (new & LPS_NERR)
254 log(LOG_NOTICE,
255 "%s: output error\n", device_xname(sc->sc_dev));
256 }
257
258 return status;
259 }
260
261 void
262 lptwakeup(arg)
263 void *arg;
264 {
265 struct lpt_softc *sc = arg;
266 int s;
267
268 s = spllpt();
269 lptintr(sc);
270 splx(s);
271
272 callout_reset(&sc->sc_wakeup_ch, STEP, lptwakeup, sc);
273 }
274
275 /*
276 * Close the device, and free the local line buffer.
277 */
278 int
279 lptclose(dev_t dev, int flag, int mode,
280 struct lwp *l)
281 {
282 struct lpt_softc *sc =
283 device_private(device_lookup(&lpt_cd, LPTUNIT(dev)));
284 bus_space_tag_t iot = sc->sc_iot;
285 bus_space_handle_t ioh = sc->sc_ioh;
286
287 if (sc->sc_count)
288 (void) lptpushbytes(sc);
289
290 if ((sc->sc_flags & LPT_NOINTR) == 0)
291 callout_stop(&sc->sc_wakeup_ch);
292
293 bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
294 sc->sc_state = 0;
295 bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
296 free(sc->sc_inbuf, M_DEVBUF);
297
298 LPRINTF(("%s: closed\n", device_xname(sc->sc_dev)));
299 return 0;
300 }
301
302 int
303 lptpushbytes(sc)
304 struct lpt_softc *sc;
305 {
306 bus_space_tag_t iot = sc->sc_iot;
307 bus_space_handle_t ioh = sc->sc_ioh;
308 int error;
309
310 if (sc->sc_flags & LPT_NOINTR) {
311 int spin, tic;
312 u_char control = sc->sc_control;
313
314 while (sc->sc_count > 0) {
315 spin = 0;
316 while (NOT_READY()) {
317 if (++spin < sc->sc_spinmax)
318 continue;
319 tic = 0;
320 /* adapt busy-wait algorithm */
321 sc->sc_spinmax++;
322 while (NOT_READY_ERR()) {
323 /* exponential backoff */
324 tic = tic + tic + 1;
325 if (tic > TIMEOUT)
326 tic = TIMEOUT;
327 error = tsleep((void *)sc,
328 LPTPRI | PCATCH, "lptpsh", tic);
329 if (error != EWOULDBLOCK)
330 return error;
331 }
332 break;
333 }
334
335 bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
336 DELAY(1);
337 bus_space_write_1(iot, ioh, lpt_control,
338 control | LPC_STROBE);
339 DELAY(1);
340 sc->sc_count--;
341 bus_space_write_1(iot, ioh, lpt_control, control);
342 DELAY(1);
343
344 /* adapt busy-wait algorithm */
345 if (spin*2 + 16 < sc->sc_spinmax)
346 sc->sc_spinmax--;
347 }
348 } else {
349 int s;
350
351 while (sc->sc_count > 0) {
352 /* if the printer is ready for a char, give it one */
353 if ((sc->sc_state & LPT_OBUSY) == 0) {
354 LPRINTF(("%s: write %lu\n",
355 device_xname(sc->sc_dev),
356 (u_long)sc->sc_count));
357 s = spllpt();
358 (void) lptintr(sc);
359 splx(s);
360 }
361 error = tsleep((void *)sc, LPTPRI | PCATCH,
362 "lptwrite2", 0);
363 if (error)
364 return error;
365 }
366 }
367 return 0;
368 }
369
370 /*
371 * Copy a line from user space to a local buffer, then call putc to get the
372 * chars moved to the output queue.
373 */
374 int
375 lptwrite(dev_t dev, struct uio *uio, int flags)
376 {
377 struct lpt_softc *sc =
378 device_private(device_lookup(&lpt_cd, LPTUNIT(dev)));
379 size_t n;
380 int error = 0;
381
382 while ((n = min(LPT_BSIZE, uio->uio_resid)) != 0) {
383 uiomove(sc->sc_cp = sc->sc_inbuf, n, uio);
384 sc->sc_count = n;
385 error = lptpushbytes(sc);
386 if (error) {
387 /*
388 * Return accurate residual if interrupted or timed
389 * out.
390 */
391 uio->uio_resid += sc->sc_count;
392 sc->sc_count = 0;
393 return error;
394 }
395 }
396 return 0;
397 }
398
399 /*
400 * Handle printer interrupts which occur when the printer is ready to accept
401 * another char.
402 */
403 int
404 lptintr(arg)
405 void *arg;
406 {
407 struct lpt_softc *sc = arg;
408 bus_space_tag_t iot = sc->sc_iot;
409 bus_space_handle_t ioh = sc->sc_ioh;
410
411 #if 0
412 if ((sc->sc_state & LPT_OPEN) == 0)
413 return 0;
414 #endif
415
416 /* is printer online and ready for output */
417 if (NOT_READY() && NOT_READY_ERR())
418 return 0;
419
420 if (sc->sc_count) {
421 u_char control = sc->sc_control;
422 /* send char */
423 bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
424 DELAY(1);
425 bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE);
426 DELAY(1);
427 sc->sc_count--;
428 bus_space_write_1(iot, ioh, lpt_control, control);
429 DELAY(1);
430 sc->sc_state |= LPT_OBUSY;
431 } else
432 sc->sc_state &= ~LPT_OBUSY;
433
434 if (sc->sc_count == 0) {
435 /* none, wake up the top half to get more */
436 softint_schedule(sc->sc_sih);
437 }
438
439 return 1;
440 }
441
442 static void
443 lptsoftintr(void *cookie)
444 {
445
446 wakeup(cookie);
447 }
448
449 int
450 lptioctl(dev_t dev, u_long cmd, void *data,
451 int flag, struct lwp *l)
452 {
453 return ENODEV;
454 }
455