lpt.c revision 1.36 1 /* $NetBSD: lpt.c,v 1.36 2014/03/16 05:20:23 dholland Exp $ */
2
3 /*
4 * Copyright (c) 1996 Leo Weppelman
5 * Copyright (c) 1993, 1994 Charles M. Hannum.
6 * Copyright (c) 1990 William F. Jolitz, TeleMuse
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This software is a component of "386BSD" developed by
20 * William F. Jolitz, TeleMuse.
21 * 4. Neither the name of the developer nor the name "386BSD"
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 /*
39 * Device Driver originally written for AT parallel printer port. Now
40 * drives the printer port on the YM2149.
41 *
42 * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
43 * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
44 * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
45 * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
46 * NOT MAKE USE OF THIS WORK.
47 *
48 * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
49 * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
50 * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES
51 * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
52 * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
53 * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
54 * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
55 * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
56 */
57
58 #include <sys/cdefs.h>
59 __KERNEL_RCSID(0, "$NetBSD: lpt.c,v 1.36 2014/03/16 05:20:23 dholland Exp $");
60
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/callout.h>
64 #include <sys/proc.h>
65 #include <sys/buf.h>
66 #include <sys/kernel.h>
67 #include <sys/ioctl.h>
68 #include <sys/uio.h>
69 #include <sys/device.h>
70 #include <sys/conf.h>
71 #include <sys/syslog.h>
72
73 #include <machine/cpu.h>
74 #include <machine/iomap.h>
75 #include <machine/mfp.h>
76 #include <machine/intr.h>
77
78 #include <atari/dev/ym2149reg.h>
79
80 #include "ioconf.h"
81
82 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */
83 #define STEP hz/4
84
85 #define LPTPRI (PZERO+8)
86 #define LPT_BSIZE 1024
87
88 #if !defined(DEBUG) || !defined(notdef)
89 #define lprintf if (0) aprint_error_dev
90 #else
91 #define lprintf if (lptdebug) aprint_error_dev
92 int lptdebug = 1;
93 #endif
94
95 struct lpt_softc {
96 device_t sc_dev;
97 struct callout sc_wakeup_ch;
98 size_t sc_count;
99 struct buf *sc_inbuf;
100 u_char *sc_cp;
101 int sc_spinmax;
102 u_char sc_state;
103 #define LPT_OPEN 0x01 /* device is open */
104 #define LPT_OBUSY 0x02 /* printer is busy doing output */
105 #define LPT_INIT 0x04 /* waiting to initialize for open */
106 u_char sc_flags;
107 #define LPT_AUTOLF 0x20 /* automatic LF on CR XXX: LWP - not yet... */
108 #define LPT_NOINTR 0x40 /* do not use interrupt */
109 void *sc_sicookie;
110 };
111
112 #define LPTUNIT(s) (minor(s) & 0x1f)
113 #define LPTFLAGS(s) (minor(s) & 0xe0)
114 #define NOT_READY() (MFP->mf_gpip & IO_PBSY)
115
116 /* {b,c}devsw[] function prototypes */
117 dev_type_open(lpopen);
118 dev_type_close(lpclose);
119 dev_type_write(lpwrite);
120 dev_type_ioctl(lpioctl);
121
122 static void lptwakeup (void *arg);
123 static int pushbytes (struct lpt_softc *);
124 static void lptpseudointr (void *);
125 int lptintr (struct lpt_softc *);
126 int lpthwintr (void *);
127
128
129 /*
130 * Autoconfig stuff
131 */
132 static void lpattach (device_t, device_t, void *);
133 static int lpmatch (device_t, cfdata_t , void *);
134
135 CFATTACH_DECL_NEW(lp, sizeof(struct lpt_softc),
136 lpmatch, lpattach, NULL, NULL);
137
138 const struct cdevsw lp_cdevsw = {
139 .d_open = lpopen,
140 .d_close = lpclose,
141 .d_read = noread,
142 .d_write = lpwrite,
143 .d_ioctl = lpioctl,
144 .d_stop = nostop,
145 .d_tty = notty,
146 .d_poll = nopoll,
147 .d_mmap = nommap,
148 .d_kqfilter = nokqfilter,
149 .d_flag = 0
150 };
151
152 /*ARGSUSED*/
153 static int
154 lpmatch(device_t parent, cfdata_t cf, void *aux)
155 {
156 static int lpt_matched = 0;
157
158 /* Match at most 1 lpt unit */
159 if (strcmp((char *)aux, "lpt") || lpt_matched)
160 return 0;
161 lpt_matched = 1;
162 return (1);
163 }
164
165 /*ARGSUSED*/
166 static void
167 lpattach(device_t parent, device_t self, void *aux)
168 {
169 struct lpt_softc *sc = device_private(self);
170
171 sc->sc_dev = self;
172 sc->sc_state = 0;
173
174 aprint_normal("\n");
175
176 if (intr_establish(0, USER_VEC, 0, (hw_ifun_t)lpthwintr, sc) == NULL)
177 aprint_error_dev(self, "Can't establish interrupt\n");
178 ym2149_strobe(1);
179 sc->sc_sicookie = softint_establish(SOFTINT_SERIAL, lptpseudointr, sc);
180
181 callout_init(&sc->sc_wakeup_ch, 0);
182 }
183
184 /*
185 * Reset the printer, then wait until it's selected and not busy.
186 */
187 int
188 lpopen(dev_t dev, int flag, int mode, struct lwp *l)
189 {
190 u_char flags = LPTFLAGS(dev);
191 struct lpt_softc *sc;
192 int error;
193 int spin;
194 int sps;
195
196 sc = device_lookup_private(&lp_cd, LPTUNIT(dev));
197 if (!sc)
198 return ENXIO;
199
200 #ifdef DIAGNOSTIC
201 if (sc->sc_state)
202 aprint_verbose_dev(sc->sc_dev, "stat=0x%x not zero\n",
203 sc->sc_state);
204 #endif
205
206 if (sc->sc_state)
207 return EBUSY;
208
209 sc->sc_state = LPT_INIT;
210 sc->sc_flags = flags;
211 lprintf(sc->sc_dev, "open: flags=0x%x\n", flags);
212
213 /* wait till ready (printer running diagnostics) */
214 for (spin = 0; NOT_READY(); spin += STEP) {
215 if (spin >= TIMEOUT) {
216 sc->sc_state = 0;
217 return EBUSY;
218 }
219
220 /* wait 1/4 second, give up if we get a signal */
221 if ((error = tsleep((void *)sc, LPTPRI | PCATCH, "lptopen",
222 STEP)) != EWOULDBLOCK) {
223 sc->sc_state = 0;
224 return error;
225 }
226 }
227
228 sc->sc_inbuf = geteblk(LPT_BSIZE);
229 sc->sc_count = 0;
230 sc->sc_state = LPT_OPEN;
231
232 if ((sc->sc_flags & LPT_NOINTR) == 0) {
233 lptwakeup(sc);
234
235 sps = splhigh();
236 MFP->mf_imrb |= IB_PBSY;
237 MFP->mf_ierb |= IB_PBSY;
238 splx(sps);
239 }
240
241 lprintf(sc->sc_dev, "opened\n");
242 return 0;
243 }
244
245 void
246 lptwakeup(void *arg)
247 {
248 struct lpt_softc *sc = arg;
249
250 lptpseudointr(sc);
251
252 callout_reset(&sc->sc_wakeup_ch, STEP, lptwakeup, sc);
253 }
254
255 /*
256 * Close the device, and free the local line buffer.
257 */
258 int
259 lpclose(dev_t dev, int flag, int mode, struct lwp *l)
260 {
261 struct lpt_softc *sc = device_lookup_private(&lp_cd, LPTUNIT(dev));
262 int sps;
263
264 if (sc->sc_count)
265 (void) pushbytes(sc);
266
267 if ((sc->sc_flags & LPT_NOINTR) == 0) {
268 callout_stop(&sc->sc_wakeup_ch);
269
270 sps = splhigh();
271 MFP->mf_ierb &= ~IB_PBSY;
272 MFP->mf_imrb &= ~IB_PBSY;
273 splx(sps);
274 }
275
276 sc->sc_state = 0;
277 brelse(sc->sc_inbuf, 0);
278
279 lprintf(sc->sc_dev, "closed\n");
280 return 0;
281 }
282
283 int
284 pushbytes(struct lpt_softc *sc)
285 {
286 int error;
287
288 if (sc->sc_flags & LPT_NOINTR) {
289 int spin, tic;
290
291 while (sc->sc_count > 0) {
292 spin = 0;
293 while (NOT_READY()) {
294 if (++spin < sc->sc_spinmax)
295 continue;
296 tic = 0;
297 /* adapt busy-wait algorithm */
298 sc->sc_spinmax++;
299 while (NOT_READY()) {
300 /* exponential backoff */
301 tic = tic + tic + 1;
302 if (tic > TIMEOUT)
303 tic = TIMEOUT;
304 error = tsleep((void *)sc,
305 LPTPRI | PCATCH, "lptpsh", tic);
306 if (error != EWOULDBLOCK)
307 return error;
308 }
309 break;
310 }
311
312 ym2149_write_ioport(YM_IOB, *sc->sc_cp++);
313 ym2149_strobe(0);
314 sc->sc_count--;
315 ym2149_strobe(1);
316
317 /* adapt busy-wait algorithm */
318 if (spin*2 + 16 < sc->sc_spinmax)
319 sc->sc_spinmax--;
320 }
321 } else {
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(sc->sc_dev, "write %d\n",
326 sc->sc_count);
327 (void) lptpseudointr(sc);
328 }
329 if ((error = tsleep((void *)sc, LPTPRI | PCATCH,
330 "lptwrite2", 0)) != 0)
331 return error;
332 }
333 }
334 return 0;
335 }
336
337 /*
338 * Copy a line from user space to a local buffer, then call putc to get the
339 * chars moved to the output queue.
340 */
341 int
342 lpwrite(dev_t dev, struct uio *uio, int flags)
343 {
344 struct lpt_softc *sc = device_lookup_private(&lp_cd,LPTUNIT(dev));
345 size_t n;
346 int error = 0;
347
348 while ((n = min(LPT_BSIZE, uio->uio_resid)) > 0) {
349 uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio);
350 sc->sc_count = n;
351 error = pushbytes(sc);
352 if (error) {
353 /*
354 * Return accurate residual if interrupted or timed
355 * out.
356 */
357 uio->uio_resid += sc->sc_count;
358 sc->sc_count = 0;
359 return error;
360 }
361 }
362 return 0;
363 }
364
365 /*
366 * Handle printer interrupts which occur when the printer is ready to accept
367 * another char.
368 */
369 int
370 lptintr(struct lpt_softc *sc)
371 {
372 /* is printer online and ready for output */
373 if (NOT_READY())
374 return 0;
375
376 if (sc->sc_count) {
377
378 /* send char */
379 ym2149_write_ioport(YM_IOB, *sc->sc_cp++);
380 ym2149_strobe(0);
381 sc->sc_count--;
382 ym2149_strobe(1);
383 sc->sc_state |= LPT_OBUSY;
384 } else
385 sc->sc_state &= ~LPT_OBUSY;
386
387 if (sc->sc_count == 0) {
388 /* none, wake up the top half to get more */
389 wakeup((void *)sc);
390 }
391
392 return 1;
393 }
394
395 static void
396 lptpseudointr(void *arg)
397 {
398 struct lpt_softc *sc;
399 int s;
400
401 sc = arg;
402 s = spltty();
403 lptintr(sc);
404 splx(s);
405 }
406
407 int
408 lpthwintr(void *arg)
409 {
410 struct lpt_softc *sc;
411
412 sc = arg;
413 softint_schedule(sc->sc_sicookie);
414 return 1;
415 }
416
417 int
418 lpioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
419 {
420 int error = 0;
421
422 switch (cmd) {
423 default:
424 error = ENODEV;
425 }
426
427 return error;
428 }
429