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