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