1 /* $NetBSD: lpt.c,v 1.40 2023/01/06 10:28:28 tsutsui 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.40 2023/01/06 10:28:28 tsutsui 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 static dev_type_open(lpopen);
118 static dev_type_close(lpclose);
119 static dev_type_write(lpwrite);
120 static 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_discard = nodiscard,
150 .d_flag = 0
151 };
152
153 /*ARGSUSED*/
154 static int
lpmatch(device_t parent,cfdata_t cf,void * aux)155 lpmatch(device_t parent, cfdata_t cf, void *aux)
156 {
157 static int lpt_matched = 0;
158
159 /* Match at most 1 lpt unit */
160 if (strcmp((char *)aux, "lpt") || lpt_matched)
161 return 0;
162 lpt_matched = 1;
163 return (1);
164 }
165
166 /*ARGSUSED*/
167 static void
lpattach(device_t parent,device_t self,void * aux)168 lpattach(device_t parent, device_t self, void *aux)
169 {
170 struct lpt_softc *sc = device_private(self);
171
172 sc->sc_dev = self;
173 sc->sc_state = 0;
174
175 aprint_normal("\n");
176
177 if (intr_establish(0, USER_VEC, 0, (hw_ifun_t)lpthwintr, sc) == NULL)
178 aprint_error_dev(self, "Can't establish interrupt\n");
179 ym2149_strobe(1);
180 sc->sc_sicookie = softint_establish(SOFTINT_SERIAL, lptpseudointr, sc);
181
182 callout_init(&sc->sc_wakeup_ch, 0);
183 }
184
185 /*
186 * Reset the printer, then wait until it's selected and not busy.
187 */
188 static int
lpopen(dev_t dev,int flag,int mode,struct lwp * l)189 lpopen(dev_t dev, int flag, int mode, struct lwp *l)
190 {
191 u_char flags = LPTFLAGS(dev);
192 struct lpt_softc *sc;
193 int error;
194 int spin;
195 int sps;
196
197 sc = device_lookup_private(&lp_cd, LPTUNIT(dev));
198 if (!sc)
199 return ENXIO;
200
201 #ifdef DIAGNOSTIC
202 if (sc->sc_state)
203 aprint_verbose_dev(sc->sc_dev, "stat=0x%x not zero\n",
204 sc->sc_state);
205 #endif
206
207 if (sc->sc_state)
208 return EBUSY;
209
210 sc->sc_state = LPT_INIT;
211 sc->sc_flags = flags;
212 lprintf(sc->sc_dev, "open: flags=0x%x\n", flags);
213
214 /* wait till ready (printer running diagnostics) */
215 for (spin = 0; NOT_READY(); spin += STEP) {
216 if (spin >= TIMEOUT) {
217 sc->sc_state = 0;
218 return EBUSY;
219 }
220
221 /* wait 1/4 second, give up if we get a signal */
222 if ((error = tsleep((void *)sc, LPTPRI | PCATCH, "lptopen",
223 STEP)) != EWOULDBLOCK) {
224 sc->sc_state = 0;
225 return error;
226 }
227 }
228
229 sc->sc_inbuf = geteblk(LPT_BSIZE);
230 sc->sc_count = 0;
231 sc->sc_state = LPT_OPEN;
232
233 if ((sc->sc_flags & LPT_NOINTR) == 0) {
234 lptwakeup(sc);
235
236 sps = splhigh();
237 MFP->mf_imrb |= IB_PBSY;
238 MFP->mf_ierb |= IB_PBSY;
239 splx(sps);
240 }
241
242 lprintf(sc->sc_dev, "opened\n");
243 return 0;
244 }
245
246 void
lptwakeup(void * arg)247 lptwakeup(void *arg)
248 {
249 struct lpt_softc *sc = arg;
250
251 lptpseudointr(sc);
252
253 callout_reset(&sc->sc_wakeup_ch, STEP, lptwakeup, sc);
254 }
255
256 /*
257 * Close the device, and free the local line buffer.
258 */
259 static int
lpclose(dev_t dev,int flag,int mode,struct lwp * l)260 lpclose(dev_t dev, int flag, int mode, struct lwp *l)
261 {
262 struct lpt_softc *sc = device_lookup_private(&lp_cd, LPTUNIT(dev));
263 int sps;
264
265 if (sc->sc_count)
266 (void) pushbytes(sc);
267
268 if ((sc->sc_flags & LPT_NOINTR) == 0) {
269 callout_stop(&sc->sc_wakeup_ch);
270
271 sps = splhigh();
272 MFP->mf_ierb &= ~IB_PBSY;
273 MFP->mf_imrb &= ~IB_PBSY;
274 splx(sps);
275 }
276
277 sc->sc_state = 0;
278 brelse(sc->sc_inbuf, 0);
279
280 lprintf(sc->sc_dev, "closed\n");
281 return 0;
282 }
283
284 int
pushbytes(struct lpt_softc * sc)285 pushbytes(struct lpt_softc *sc)
286 {
287 int error;
288
289 if (sc->sc_flags & LPT_NOINTR) {
290 int spin, tic;
291
292 while (sc->sc_count > 0) {
293 spin = 0;
294 while (NOT_READY()) {
295 if (++spin < sc->sc_spinmax)
296 continue;
297 tic = 0;
298 /* adapt busy-wait algorithm */
299 sc->sc_spinmax++;
300 while (NOT_READY()) {
301 /* exponential backoff */
302 tic = tic + tic + 1;
303 if (tic > TIMEOUT)
304 tic = TIMEOUT;
305 error = tsleep((void *)sc,
306 LPTPRI | PCATCH, "lptpsh", tic);
307 if (error != EWOULDBLOCK)
308 return error;
309 }
310 break;
311 }
312
313 ym2149_write_ioport(YM_IOB, *sc->sc_cp++);
314 ym2149_strobe(0);
315 sc->sc_count--;
316 ym2149_strobe(1);
317
318 /* adapt busy-wait algorithm */
319 if (spin*2 + 16 < sc->sc_spinmax)
320 sc->sc_spinmax--;
321 }
322 } else {
323 while (sc->sc_count > 0) {
324 /* if the printer is ready for a char, give it one */
325 if ((sc->sc_state & LPT_OBUSY) == 0) {
326 lprintf(sc->sc_dev, "write %d\n",
327 sc->sc_count);
328 (void) lptpseudointr(sc);
329 }
330 if ((error = tsleep((void *)sc, LPTPRI | PCATCH,
331 "lptwrite2", 0)) != 0)
332 return error;
333 }
334 }
335 return 0;
336 }
337
338 /*
339 * Copy a line from user space to a local buffer, then call putc to get the
340 * chars moved to the output queue.
341 */
342 static int
lpwrite(dev_t dev,struct uio * uio,int flags)343 lpwrite(dev_t dev, struct uio *uio, int flags)
344 {
345 struct lpt_softc *sc = device_lookup_private(&lp_cd,LPTUNIT(dev));
346 size_t n;
347 int error = 0;
348
349 while ((n = uimin(LPT_BSIZE, uio->uio_resid)) > 0) {
350 uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio);
351 sc->sc_count = n;
352 error = pushbytes(sc);
353 if (error) {
354 /*
355 * Return accurate residual if interrupted or timed
356 * out.
357 */
358 uio->uio_resid += sc->sc_count;
359 sc->sc_count = 0;
360 return error;
361 }
362 }
363 return 0;
364 }
365
366 /*
367 * Handle printer interrupts which occur when the printer is ready to accept
368 * another char.
369 */
370 int
lptintr(struct lpt_softc * sc)371 lptintr(struct lpt_softc *sc)
372 {
373 /* is printer online and ready for output */
374 if (NOT_READY())
375 return 0;
376
377 if (sc->sc_count) {
378
379 /* send char */
380 ym2149_write_ioport(YM_IOB, *sc->sc_cp++);
381 ym2149_strobe(0);
382 sc->sc_count--;
383 ym2149_strobe(1);
384 sc->sc_state |= LPT_OBUSY;
385 } else
386 sc->sc_state &= ~LPT_OBUSY;
387
388 if (sc->sc_count == 0) {
389 /* none, wake up the top half to get more */
390 wakeup((void *)sc);
391 }
392
393 return 1;
394 }
395
396 static void
lptpseudointr(void * arg)397 lptpseudointr(void *arg)
398 {
399 struct lpt_softc *sc;
400 int s;
401
402 sc = arg;
403 s = spltty();
404 lptintr(sc);
405 splx(s);
406 }
407
408 int
lpthwintr(void * arg)409 lpthwintr(void *arg)
410 {
411 struct lpt_softc *sc;
412
413 sc = arg;
414 softint_schedule(sc->sc_sicookie);
415 return 1;
416 }
417
418 static int
lpioctl(dev_t dev,u_long cmd,void * data,int flag,struct lwp * l)419 lpioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
420 {
421 int error = 0;
422
423 switch (cmd) {
424 default:
425 error = ENODEV;
426 }
427
428 return error;
429 }
430