xref: /netbsd-src/sys/arch/news68k/news68k/romcons.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: romcons.c,v 1.2 2014/03/16 05:20:25 dholland Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5  * Copyright (C) 1995, 1996 TooLs GmbH.
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 product includes software developed by TooLs GmbH.
19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 /*
34  * romcons.c - from sys/dev/ofw/ofcons.c
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: romcons.c,v 1.2 2014/03/16 05:20:25 dholland Exp $");
39 
40 #include <sys/param.h>
41 #include <sys/conf.h>
42 #include <sys/device.h>
43 #include <sys/proc.h>
44 #include <sys/systm.h>
45 #include <sys/callout.h>
46 #include <sys/tty.h>
47 #include <sys/kauth.h>
48 
49 #include <dev/cons.h>
50 
51 #include <machine/autoconf.h>
52 #include <machine/romcall.h>
53 
54 #include "ioconf.h"
55 
56 struct romcons_softc {
57 	device_t sc_dev;
58 	struct tty *sc_tty;
59 	struct callout sc_poll_ch;
60 	int sc_flags;
61 #define	CONS_POLL	1
62 };
63 
64 #define	BURSTLEN	128	/* max number of bytes to write in one chunk */
65 
66 cons_decl(romcons_);
67 
68 static int romcons_match(device_t, cfdata_t, void *);
69 static void romcons_attach(device_t, device_t, void *);
70 
71 CFATTACH_DECL_NEW(romcons, sizeof(struct romcons_softc),
72     romcons_match, romcons_attach, NULL, NULL);
73 
74 dev_type_open(romcons_open);
75 dev_type_close(romcons_close);
76 dev_type_read(romcons_read);
77 dev_type_write(romcons_write);
78 dev_type_ioctl(romcons_ioctl);
79 dev_type_tty(romcons_tty);
80 dev_type_poll(romcons_poll);
81 
82 void romcons_kbdinput(int);
83 
84 const struct cdevsw romcons_cdevsw = {
85 	.d_open = romcons_open,
86 	.d_close = romcons_close,
87 	.d_read = romcons_read,
88 	.d_write = romcons_write,
89 	.d_ioctl = romcons_ioctl,
90 	.d_stop = nostop,
91 	.d_tty = romcons_tty,
92 	.d_poll = romcons_poll,
93 	.d_mmap = nommap,
94 	.d_kqfilter = ttykqfilter,
95 	.d_flag = D_TTY
96 };
97 
98 struct consdev consdev_rom = cons_init(romcons_);
99 
100 bool romcons_is_console;
101 
102 static int
103 romcons_match(device_t parent, cfdata_t match, void *aux)
104 {
105 	struct mainbus_attach_args *ma = aux;
106 	static bool romcons_matched;
107 
108 	if (strcmp(ma->ma_name, "romcons"))
109 		return 0;
110 
111 	if (!romcons_is_console)
112 		return 0;
113 
114 	if (romcons_matched)
115 		return 0;
116 
117 	romcons_matched = true;
118 	return 1;
119 }
120 
121 static void
122 romcons_attach(device_t parent, device_t self, void *aux)
123 {
124 	struct romcons_softc *sc = device_private(self);
125 
126 	sc->sc_dev = self;
127 	vectab[46] = romcallvec;	/* XXX */
128 	aprint_normal("\n");
129 
130 	callout_init(&sc->sc_poll_ch, 0);
131 }
132 
133 static void romcons_start(struct tty *);
134 static int romcons_param(struct tty *, struct termios *);
135 static void romcons_pollin(void *);
136 
137 int
138 romcons_open(dev_t dev, int flag, int mode, struct lwp *l)
139 {
140 	struct romcons_softc *sc;
141 	struct tty *tp;
142 
143 	sc = device_lookup_private(&romcons_cd, minor(dev));
144 	if (sc == NULL)
145 		return ENXIO;
146 	if ((tp = sc->sc_tty) == 0)
147 		sc->sc_tty = tp = tty_alloc();
148 	tp->t_oproc = romcons_start;
149 	tp->t_param = romcons_param;
150 	tp->t_dev = dev;
151 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
152 		return EBUSY;
153 	if ((tp->t_state & TS_ISOPEN) == 0) {
154 		ttychars(tp);
155 		tp->t_iflag = TTYDEF_IFLAG;
156 		tp->t_oflag = TTYDEF_OFLAG;
157 		tp->t_cflag = TTYDEF_CFLAG;
158 		tp->t_lflag = TTYDEF_LFLAG;
159 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
160 		romcons_param(tp, &tp->t_termios);
161 		ttsetwater(tp);
162 	}
163 	tp->t_state |= TS_CARR_ON;
164 
165 	if ((sc->sc_flags & CONS_POLL) == 0) {
166 		sc->sc_flags |= CONS_POLL;
167 		callout_reset(&sc->sc_poll_ch, 1, romcons_pollin, sc);
168 	}
169 
170 	return (*tp->t_linesw->l_open)(dev, tp);
171 }
172 
173 int
174 romcons_close(dev_t dev, int flag, int mode, struct lwp *l)
175 {
176 	struct romcons_softc *sc;
177 	struct tty *tp;
178 
179 	sc = device_lookup_private(&romcons_cd, minor(dev));
180 	tp = sc->sc_tty;
181 	callout_stop(&sc->sc_poll_ch);
182 	sc->sc_flags &= ~CONS_POLL;
183 	(*tp->t_linesw->l_close)(tp, flag);
184 	ttyclose(tp);
185 	return 0;
186 }
187 
188 int
189 romcons_read(dev_t dev, struct uio *uio, int flag)
190 {
191 	struct romcons_softc *sc;
192 	struct tty *tp;
193 
194 	sc = device_lookup_private(&romcons_cd, minor(dev));
195 	tp = sc->sc_tty;
196 
197 	return (*tp->t_linesw->l_read)(tp, uio, flag);
198 }
199 
200 int
201 romcons_write(dev_t dev, struct uio *uio, int flag)
202 {
203 	struct romcons_softc *sc;
204 	struct tty *tp;
205 
206 	sc = device_lookup_private(&romcons_cd, minor(dev));
207 	tp = sc->sc_tty;
208 	return (*tp->t_linesw->l_write)(tp, uio, flag);
209 }
210 
211 int
212 romcons_poll(dev_t dev, int events, struct lwp *l)
213 {
214 	struct romcons_softc *sc;
215 	struct tty *tp;
216 
217 	sc = device_lookup_private(&romcons_cd, minor(dev));
218 	tp = sc->sc_tty;
219 	return (*tp->t_linesw->l_poll)(tp, events, l);
220 }
221 int
222 romcons_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
223 {
224 	struct romcons_softc *sc;
225 	struct tty *tp;
226 	int error;
227 
228 	sc = device_lookup_private(&romcons_cd, minor(dev));
229 	tp = sc->sc_tty;
230 	if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) !=
231 	    EPASSTHROUGH)
232 		return error;
233 	return ttioctl(tp, cmd, data, flag, l);
234 }
235 
236 struct tty *
237 romcons_tty(dev_t dev)
238 {
239 	struct romcons_softc *sc;
240 
241 	sc = device_lookup_private(&romcons_cd, minor(dev));
242 	return sc->sc_tty;
243 }
244 
245 static void
246 romcons_start(struct tty *tp)
247 {
248 	int s, len;
249 	uint8_t buf[BURSTLEN];
250 
251 	s = spltty();
252 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
253 		splx(s);
254 		return;
255 	}
256 	tp->t_state |= TS_BUSY;
257 	splx(s);
258 	len = q_to_b(&tp->t_outq, buf, BURSTLEN);
259 	s = splhigh();
260 	rom_write(1, buf, len);
261 	splx(s);
262 	s = spltty();
263 	tp->t_state &= ~TS_BUSY;
264 	if (ttypull(tp)) {
265 		tp->t_state |= TS_TIMEOUT;
266 		callout_schedule(&tp->t_rstrt_ch, 1);
267 	}
268 	splx(s);
269 }
270 
271 static int
272 romcons_param(struct tty *tp, struct termios *t)
273 {
274 
275 	tp->t_ispeed = t->c_ispeed;
276 	tp->t_ospeed = t->c_ospeed;
277 	tp->t_cflag = t->c_cflag;
278 	return 0;
279 }
280 
281 static void
282 romcons_pollin(void *aux)
283 {
284 	struct romcons_softc *sc = aux;
285 	struct tty *tp = sc->sc_tty;
286 	char ch;
287 	int rv;
288 
289 	while (0 && (rv = rom_read(1, &ch, 1)) > 0) {
290 		if (tp && (tp->t_state & TS_ISOPEN))
291 			(*tp->t_linesw->l_rint)(ch, tp);
292 	}
293 	callout_reset(&sc->sc_poll_ch, 1, romcons_pollin, sc);
294 }
295 
296 void
297 romcons_kbdinput(int ks)
298 {
299 	struct romcons_softc *sc;
300 	struct tty *tp;
301 
302 	sc = device_lookup_private(&romcons_cd, 0);
303 	tp = sc->sc_tty;
304 	if (tp && (tp->t_state & TS_ISOPEN))
305 		(*tp->t_linesw->l_rint)(ks, tp);
306 }
307 
308 void
309 romcons_cnprobe(struct consdev *cd)
310 {
311 }
312 
313 void
314 romcons_cninit(struct consdev *cd)
315 {
316 	int maj;
317 
318 	maj = cdevsw_lookup_major(&romcons_cdevsw);
319 	cd->cn_dev = makedev(maj, 0);
320 	romcons_is_console = true;
321 	vectab[46] = romcallvec;	/* XXX */
322 }
323 
324 int
325 romcons_cngetc(dev_t dev)
326 {
327 	unsigned char ch = '\0';
328 	int l;
329 
330 	while ((l = rom_read(1, &ch, 1)) != 1)
331 		if (l != -2 && l != 0)
332 			return -1;
333 	return ch;
334 }
335 
336 void
337 romcons_cnputc(dev_t dev, int c)
338 {
339 	char ch = c;
340 
341 	rom_write(1, &ch, 1);
342 }
343 
344 void
345 romcons_cnpollc(dev_t dev, int on)
346 {
347 	struct romcons_softc *sc;
348 
349 	sc = device_lookup_private(&romcons_cd, minor(dev));
350 
351 	if (sc == NULL)
352 		return;
353 	if (on) {
354 		if (sc->sc_flags & CONS_POLL)
355 			callout_stop(&sc->sc_poll_ch);
356 		sc->sc_flags &= ~CONS_POLL;
357 	} else {
358 		if ((sc->sc_flags & CONS_POLL) == 0) {
359 			sc->sc_flags |= CONS_POLL;
360 			callout_reset(&sc->sc_poll_ch, 1, romcons_pollin, sc);
361 		}
362 	}
363 }
364