ofcons.c revision 1.14 1 1.14 matt /* $NetBSD: ofcons.c,v 1.14 2001/08/25 19:05:04 matt Exp $ */
2 1.1 ws
3 1.1 ws /*
4 1.1 ws * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 1.1 ws * Copyright (C) 1995, 1996 TooLs GmbH.
6 1.1 ws * All rights reserved.
7 1.1 ws *
8 1.1 ws * Redistribution and use in source and binary forms, with or without
9 1.1 ws * modification, are permitted provided that the following conditions
10 1.1 ws * are met:
11 1.1 ws * 1. Redistributions of source code must retain the above copyright
12 1.1 ws * notice, this list of conditions and the following disclaimer.
13 1.1 ws * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 ws * notice, this list of conditions and the following disclaimer in the
15 1.1 ws * documentation and/or other materials provided with the distribution.
16 1.1 ws * 3. All advertising materials mentioning features or use of this software
17 1.1 ws * must display the following acknowledgement:
18 1.1 ws * This product includes software developed by TooLs GmbH.
19 1.1 ws * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 1.1 ws * derived from this software without specific prior written permission.
21 1.1 ws *
22 1.1 ws * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 1.1 ws * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 ws * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 ws * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 1.1 ws * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 1.1 ws * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 1.1 ws * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.1 ws * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 1.1 ws * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 1.1 ws * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 ws */
33 1.1 ws
34 1.1 ws #include <sys/param.h>
35 1.1 ws #include <sys/conf.h>
36 1.1 ws #include <sys/device.h>
37 1.1 ws #include <sys/proc.h>
38 1.1 ws #include <sys/systm.h>
39 1.10 thorpej #include <sys/callout.h>
40 1.1 ws #include <sys/tty.h>
41 1.1 ws
42 1.1 ws #include <dev/cons.h>
43 1.1 ws
44 1.1 ws #include <dev/ofw/openfirm.h>
45 1.1 ws
46 1.8 mycroft struct ofcons_softc {
47 1.1 ws struct device of_dev;
48 1.1 ws struct tty *of_tty;
49 1.10 thorpej struct callout sc_poll_ch;
50 1.1 ws int of_flags;
51 1.1 ws };
52 1.1 ws /* flags: */
53 1.1 ws #define OFPOLL 1
54 1.1 ws
55 1.1 ws #define OFBURSTLEN 128 /* max number of bytes to write in one chunk */
56 1.14 matt
57 1.14 matt cdev_decl(ofcons_);
58 1.14 matt cons_decl(ofcons_);
59 1.1 ws
60 1.1 ws static int stdin, stdout;
61 1.1 ws
62 1.8 mycroft static int ofcons_match __P((struct device *, struct cfdata *, void *));
63 1.8 mycroft static void ofcons_attach __P((struct device *, struct device *, void *));
64 1.1 ws
65 1.1 ws struct cfattach ofcons_ca = {
66 1.8 mycroft sizeof(struct ofcons_softc), ofcons_match, ofcons_attach
67 1.1 ws };
68 1.1 ws
69 1.6 thorpej extern struct cfdriver ofcons_cd;
70 1.1 ws
71 1.8 mycroft static int ofcons_probe __P((void));
72 1.1 ws
73 1.1 ws static int
74 1.8 mycroft ofcons_match(parent, match, aux)
75 1.1 ws struct device *parent;
76 1.4 thorpej struct cfdata *match;
77 1.4 thorpej void *aux;
78 1.1 ws {
79 1.8 mycroft struct ofbus_attach_args *oba = aux;
80 1.1 ws
81 1.8 mycroft if (strcmp(oba->oba_busname, "ofw"))
82 1.8 mycroft return (0);
83 1.8 mycroft if (!ofcons_probe())
84 1.1 ws return 0;
85 1.8 mycroft return OF_instance_to_package(stdin) == oba->oba_phandle
86 1.8 mycroft || OF_instance_to_package(stdout) == oba->oba_phandle;
87 1.1 ws }
88 1.1 ws
89 1.1 ws static void
90 1.8 mycroft ofcons_attach(parent, self, aux)
91 1.1 ws struct device *parent, *self;
92 1.1 ws void *aux;
93 1.1 ws {
94 1.11 scw struct ofcons_softc *sc = (struct ofcons_softc *) self;
95 1.11 scw
96 1.3 christos printf("\n");
97 1.10 thorpej
98 1.11 scw callout_init(&sc->sc_poll_ch);
99 1.1 ws }
100 1.1 ws
101 1.8 mycroft static void ofcons_start __P((struct tty *));
102 1.8 mycroft static int ofcons_param __P((struct tty *, struct termios *));
103 1.13 scw static void ofcons_pollin __P((void *));
104 1.1 ws
105 1.1 ws int
106 1.8 mycroft ofcons_open(dev, flag, mode, p)
107 1.1 ws dev_t dev;
108 1.1 ws int flag, mode;
109 1.1 ws struct proc *p;
110 1.1 ws {
111 1.8 mycroft struct ofcons_softc *sc;
112 1.1 ws int unit = minor(dev);
113 1.1 ws struct tty *tp;
114 1.1 ws
115 1.1 ws if (unit >= ofcons_cd.cd_ndevs)
116 1.1 ws return ENXIO;
117 1.1 ws sc = ofcons_cd.cd_devs[unit];
118 1.1 ws if (!sc)
119 1.1 ws return ENXIO;
120 1.1 ws if (!(tp = sc->of_tty))
121 1.1 ws sc->of_tty = tp = ttymalloc();
122 1.8 mycroft tp->t_oproc = ofcons_start;
123 1.8 mycroft tp->t_param = ofcons_param;
124 1.1 ws tp->t_dev = dev;
125 1.1 ws if (!(tp->t_state & TS_ISOPEN)) {
126 1.1 ws ttychars(tp);
127 1.1 ws tp->t_iflag = TTYDEF_IFLAG;
128 1.1 ws tp->t_oflag = TTYDEF_OFLAG;
129 1.1 ws tp->t_cflag = TTYDEF_CFLAG;
130 1.1 ws tp->t_lflag = TTYDEF_LFLAG;
131 1.1 ws tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
132 1.8 mycroft ofcons_param(tp, &tp->t_termios);
133 1.1 ws ttsetwater(tp);
134 1.1 ws } else if ((tp->t_state&TS_XCLUDE) && suser(p->p_ucred, &p->p_acflag))
135 1.1 ws return EBUSY;
136 1.1 ws tp->t_state |= TS_CARR_ON;
137 1.1 ws
138 1.1 ws if (!(sc->of_flags & OFPOLL)) {
139 1.1 ws sc->of_flags |= OFPOLL;
140 1.13 scw callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);
141 1.1 ws }
142 1.1 ws
143 1.12 eeh return (*tp->t_linesw->l_open)(dev, tp);
144 1.1 ws }
145 1.1 ws
146 1.1 ws int
147 1.8 mycroft ofcons_close(dev, flag, mode, p)
148 1.1 ws dev_t dev;
149 1.1 ws int flag, mode;
150 1.1 ws struct proc *p;
151 1.1 ws {
152 1.8 mycroft struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
153 1.1 ws struct tty *tp = sc->of_tty;
154 1.1 ws
155 1.10 thorpej callout_stop(&sc->sc_poll_ch);
156 1.1 ws sc->of_flags &= ~OFPOLL;
157 1.12 eeh (*tp->t_linesw->l_close)(tp, flag);
158 1.1 ws ttyclose(tp);
159 1.1 ws return 0;
160 1.1 ws }
161 1.1 ws
162 1.1 ws int
163 1.8 mycroft ofcons_read(dev, uio, flag)
164 1.1 ws dev_t dev;
165 1.1 ws struct uio *uio;
166 1.1 ws int flag;
167 1.1 ws {
168 1.8 mycroft struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
169 1.1 ws struct tty *tp = sc->of_tty;
170 1.1 ws
171 1.12 eeh return (*tp->t_linesw->l_read)(tp, uio, flag);
172 1.1 ws }
173 1.1 ws
174 1.1 ws int
175 1.8 mycroft ofcons_write(dev, uio, flag)
176 1.1 ws dev_t dev;
177 1.1 ws struct uio *uio;
178 1.1 ws int flag;
179 1.1 ws {
180 1.8 mycroft struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
181 1.1 ws struct tty *tp = sc->of_tty;
182 1.1 ws
183 1.12 eeh return (*tp->t_linesw->l_write)(tp, uio, flag);
184 1.1 ws }
185 1.1 ws
186 1.1 ws int
187 1.13 scw ofcons_poll(dev, events, p)
188 1.13 scw dev_t dev;
189 1.13 scw int events;
190 1.13 scw struct proc *p;
191 1.13 scw {
192 1.13 scw struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
193 1.13 scw struct tty *tp = sc->of_tty;
194 1.13 scw
195 1.13 scw return ((*tp->t_linesw->l_poll)(tp, events, p));
196 1.13 scw }
197 1.13 scw int
198 1.8 mycroft ofcons_ioctl(dev, cmd, data, flag, p)
199 1.1 ws dev_t dev;
200 1.1 ws u_long cmd;
201 1.1 ws caddr_t data;
202 1.1 ws int flag;
203 1.1 ws struct proc *p;
204 1.1 ws {
205 1.8 mycroft struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
206 1.1 ws struct tty *tp = sc->of_tty;
207 1.1 ws int error;
208 1.1 ws
209 1.12 eeh if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p)) >= 0)
210 1.1 ws return error;
211 1.1 ws if ((error = ttioctl(tp, cmd, data, flag, p)) >= 0)
212 1.1 ws return error;
213 1.1 ws return ENOTTY;
214 1.1 ws }
215 1.1 ws
216 1.1 ws struct tty *
217 1.8 mycroft ofcons_tty(dev)
218 1.1 ws dev_t dev;
219 1.1 ws {
220 1.8 mycroft struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
221 1.1 ws
222 1.1 ws return sc->of_tty;
223 1.1 ws }
224 1.1 ws
225 1.1 ws void
226 1.8 mycroft ofcons_stop(tp, flag)
227 1.1 ws struct tty *tp;
228 1.1 ws int flag;
229 1.1 ws {
230 1.1 ws }
231 1.1 ws
232 1.1 ws static void
233 1.8 mycroft ofcons_start(tp)
234 1.1 ws struct tty *tp;
235 1.1 ws {
236 1.1 ws struct clist *cl;
237 1.1 ws int s, len;
238 1.1 ws u_char buf[OFBURSTLEN];
239 1.1 ws
240 1.1 ws s = spltty();
241 1.1 ws if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
242 1.1 ws splx(s);
243 1.1 ws return;
244 1.1 ws }
245 1.1 ws tp->t_state |= TS_BUSY;
246 1.1 ws splx(s);
247 1.1 ws cl = &tp->t_outq;
248 1.1 ws len = q_to_b(cl, buf, OFBURSTLEN);
249 1.1 ws OF_write(stdout, buf, len);
250 1.1 ws s = spltty();
251 1.1 ws tp->t_state &= ~TS_BUSY;
252 1.1 ws if (cl->c_cc) {
253 1.1 ws tp->t_state |= TS_TIMEOUT;
254 1.10 thorpej callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp);
255 1.1 ws }
256 1.1 ws if (cl->c_cc <= tp->t_lowat) {
257 1.1 ws if (tp->t_state & TS_ASLEEP) {
258 1.1 ws tp->t_state &= ~TS_ASLEEP;
259 1.1 ws wakeup(cl);
260 1.1 ws }
261 1.1 ws selwakeup(&tp->t_wsel);
262 1.1 ws }
263 1.1 ws splx(s);
264 1.1 ws }
265 1.1 ws
266 1.1 ws static int
267 1.8 mycroft ofcons_param(tp, t)
268 1.1 ws struct tty *tp;
269 1.1 ws struct termios *t;
270 1.1 ws {
271 1.1 ws tp->t_ispeed = t->c_ispeed;
272 1.1 ws tp->t_ospeed = t->c_ospeed;
273 1.1 ws tp->t_cflag = t->c_cflag;
274 1.1 ws return 0;
275 1.1 ws }
276 1.1 ws
277 1.1 ws static void
278 1.13 scw ofcons_pollin(aux)
279 1.1 ws void *aux;
280 1.1 ws {
281 1.8 mycroft struct ofcons_softc *sc = aux;
282 1.1 ws struct tty *tp = sc->of_tty;
283 1.1 ws char ch;
284 1.1 ws
285 1.1 ws while (OF_read(stdin, &ch, 1) > 0) {
286 1.1 ws if (tp && (tp->t_state & TS_ISOPEN))
287 1.12 eeh (*tp->t_linesw->l_rint)(ch, tp);
288 1.1 ws }
289 1.13 scw callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);
290 1.1 ws }
291 1.1 ws
292 1.1 ws static int
293 1.8 mycroft ofcons_probe()
294 1.1 ws {
295 1.1 ws int chosen;
296 1.7 cgd char stdinbuf[4], stdoutbuf[4];
297 1.1 ws
298 1.1 ws if (stdin)
299 1.1 ws return 1;
300 1.1 ws if ((chosen = OF_finddevice("/chosen")) == -1)
301 1.1 ws return 0;
302 1.7 cgd if (OF_getprop(chosen, "stdin", stdinbuf, sizeof stdinbuf) !=
303 1.7 cgd sizeof stdinbuf ||
304 1.7 cgd OF_getprop(chosen, "stdout", stdoutbuf, sizeof stdoutbuf) !=
305 1.7 cgd sizeof stdoutbuf)
306 1.1 ws return 0;
307 1.7 cgd
308 1.7 cgd /* Decode properties. */
309 1.7 cgd stdin = of_decode_int(stdinbuf);
310 1.7 cgd stdout = of_decode_int(stdoutbuf);
311 1.7 cgd
312 1.1 ws return 1;
313 1.1 ws }
314 1.1 ws
315 1.1 ws void
316 1.8 mycroft ofcons_cnprobe(cd)
317 1.1 ws struct consdev *cd;
318 1.1 ws {
319 1.1 ws int maj;
320 1.1 ws
321 1.8 mycroft if (!ofcons_probe())
322 1.1 ws return;
323 1.1 ws
324 1.1 ws for (maj = 0; maj < nchrdev; maj++)
325 1.8 mycroft if (cdevsw[maj].d_open == ofcons_open)
326 1.1 ws break;
327 1.1 ws cd->cn_dev = makedev(maj, 0);
328 1.1 ws cd->cn_pri = CN_INTERNAL;
329 1.1 ws }
330 1.1 ws
331 1.1 ws void
332 1.8 mycroft ofcons_cninit(cd)
333 1.1 ws struct consdev *cd;
334 1.1 ws {
335 1.1 ws }
336 1.1 ws
337 1.1 ws int
338 1.8 mycroft ofcons_cngetc(dev)
339 1.1 ws dev_t dev;
340 1.1 ws {
341 1.5 mycroft unsigned char ch = '\0';
342 1.1 ws int l;
343 1.1 ws
344 1.1 ws while ((l = OF_read(stdin, &ch, 1)) != 1)
345 1.5 mycroft if (l != -2 && l != 0)
346 1.1 ws return -1;
347 1.1 ws return ch;
348 1.1 ws }
349 1.1 ws
350 1.1 ws void
351 1.8 mycroft ofcons_cnputc(dev, c)
352 1.1 ws dev_t dev;
353 1.1 ws int c;
354 1.1 ws {
355 1.1 ws char ch = c;
356 1.1 ws
357 1.1 ws OF_write(stdout, &ch, 1);
358 1.1 ws }
359 1.1 ws
360 1.1 ws void
361 1.8 mycroft ofcons_cnpollc(dev, on)
362 1.1 ws dev_t dev;
363 1.1 ws int on;
364 1.1 ws {
365 1.8 mycroft struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
366 1.1 ws
367 1.1 ws if (!sc)
368 1.1 ws return;
369 1.1 ws if (on) {
370 1.1 ws if (sc->of_flags & OFPOLL)
371 1.10 thorpej callout_stop(&sc->sc_poll_ch);
372 1.1 ws sc->of_flags &= ~OFPOLL;
373 1.1 ws } else {
374 1.1 ws if (!(sc->of_flags & OFPOLL)) {
375 1.1 ws sc->of_flags |= OFPOLL;
376 1.13 scw callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);
377 1.1 ws }
378 1.1 ws }
379 1.1 ws }
380