ofcons.c revision 1.14 1 /* $NetBSD: ofcons.c,v 1.14 2001/08/25 19:05:04 matt 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 #include <sys/param.h>
35 #include <sys/conf.h>
36 #include <sys/device.h>
37 #include <sys/proc.h>
38 #include <sys/systm.h>
39 #include <sys/callout.h>
40 #include <sys/tty.h>
41
42 #include <dev/cons.h>
43
44 #include <dev/ofw/openfirm.h>
45
46 struct ofcons_softc {
47 struct device of_dev;
48 struct tty *of_tty;
49 struct callout sc_poll_ch;
50 int of_flags;
51 };
52 /* flags: */
53 #define OFPOLL 1
54
55 #define OFBURSTLEN 128 /* max number of bytes to write in one chunk */
56
57 cdev_decl(ofcons_);
58 cons_decl(ofcons_);
59
60 static int stdin, stdout;
61
62 static int ofcons_match __P((struct device *, struct cfdata *, void *));
63 static void ofcons_attach __P((struct device *, struct device *, void *));
64
65 struct cfattach ofcons_ca = {
66 sizeof(struct ofcons_softc), ofcons_match, ofcons_attach
67 };
68
69 extern struct cfdriver ofcons_cd;
70
71 static int ofcons_probe __P((void));
72
73 static int
74 ofcons_match(parent, match, aux)
75 struct device *parent;
76 struct cfdata *match;
77 void *aux;
78 {
79 struct ofbus_attach_args *oba = aux;
80
81 if (strcmp(oba->oba_busname, "ofw"))
82 return (0);
83 if (!ofcons_probe())
84 return 0;
85 return OF_instance_to_package(stdin) == oba->oba_phandle
86 || OF_instance_to_package(stdout) == oba->oba_phandle;
87 }
88
89 static void
90 ofcons_attach(parent, self, aux)
91 struct device *parent, *self;
92 void *aux;
93 {
94 struct ofcons_softc *sc = (struct ofcons_softc *) self;
95
96 printf("\n");
97
98 callout_init(&sc->sc_poll_ch);
99 }
100
101 static void ofcons_start __P((struct tty *));
102 static int ofcons_param __P((struct tty *, struct termios *));
103 static void ofcons_pollin __P((void *));
104
105 int
106 ofcons_open(dev, flag, mode, p)
107 dev_t dev;
108 int flag, mode;
109 struct proc *p;
110 {
111 struct ofcons_softc *sc;
112 int unit = minor(dev);
113 struct tty *tp;
114
115 if (unit >= ofcons_cd.cd_ndevs)
116 return ENXIO;
117 sc = ofcons_cd.cd_devs[unit];
118 if (!sc)
119 return ENXIO;
120 if (!(tp = sc->of_tty))
121 sc->of_tty = tp = ttymalloc();
122 tp->t_oproc = ofcons_start;
123 tp->t_param = ofcons_param;
124 tp->t_dev = dev;
125 if (!(tp->t_state & TS_ISOPEN)) {
126 ttychars(tp);
127 tp->t_iflag = TTYDEF_IFLAG;
128 tp->t_oflag = TTYDEF_OFLAG;
129 tp->t_cflag = TTYDEF_CFLAG;
130 tp->t_lflag = TTYDEF_LFLAG;
131 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
132 ofcons_param(tp, &tp->t_termios);
133 ttsetwater(tp);
134 } else if ((tp->t_state&TS_XCLUDE) && suser(p->p_ucred, &p->p_acflag))
135 return EBUSY;
136 tp->t_state |= TS_CARR_ON;
137
138 if (!(sc->of_flags & OFPOLL)) {
139 sc->of_flags |= OFPOLL;
140 callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);
141 }
142
143 return (*tp->t_linesw->l_open)(dev, tp);
144 }
145
146 int
147 ofcons_close(dev, flag, mode, p)
148 dev_t dev;
149 int flag, mode;
150 struct proc *p;
151 {
152 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
153 struct tty *tp = sc->of_tty;
154
155 callout_stop(&sc->sc_poll_ch);
156 sc->of_flags &= ~OFPOLL;
157 (*tp->t_linesw->l_close)(tp, flag);
158 ttyclose(tp);
159 return 0;
160 }
161
162 int
163 ofcons_read(dev, uio, flag)
164 dev_t dev;
165 struct uio *uio;
166 int flag;
167 {
168 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
169 struct tty *tp = sc->of_tty;
170
171 return (*tp->t_linesw->l_read)(tp, uio, flag);
172 }
173
174 int
175 ofcons_write(dev, uio, flag)
176 dev_t dev;
177 struct uio *uio;
178 int flag;
179 {
180 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
181 struct tty *tp = sc->of_tty;
182
183 return (*tp->t_linesw->l_write)(tp, uio, flag);
184 }
185
186 int
187 ofcons_poll(dev, events, p)
188 dev_t dev;
189 int events;
190 struct proc *p;
191 {
192 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
193 struct tty *tp = sc->of_tty;
194
195 return ((*tp->t_linesw->l_poll)(tp, events, p));
196 }
197 int
198 ofcons_ioctl(dev, cmd, data, flag, p)
199 dev_t dev;
200 u_long cmd;
201 caddr_t data;
202 int flag;
203 struct proc *p;
204 {
205 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
206 struct tty *tp = sc->of_tty;
207 int error;
208
209 if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p)) >= 0)
210 return error;
211 if ((error = ttioctl(tp, cmd, data, flag, p)) >= 0)
212 return error;
213 return ENOTTY;
214 }
215
216 struct tty *
217 ofcons_tty(dev)
218 dev_t dev;
219 {
220 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
221
222 return sc->of_tty;
223 }
224
225 void
226 ofcons_stop(tp, flag)
227 struct tty *tp;
228 int flag;
229 {
230 }
231
232 static void
233 ofcons_start(tp)
234 struct tty *tp;
235 {
236 struct clist *cl;
237 int s, len;
238 u_char buf[OFBURSTLEN];
239
240 s = spltty();
241 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
242 splx(s);
243 return;
244 }
245 tp->t_state |= TS_BUSY;
246 splx(s);
247 cl = &tp->t_outq;
248 len = q_to_b(cl, buf, OFBURSTLEN);
249 OF_write(stdout, buf, len);
250 s = spltty();
251 tp->t_state &= ~TS_BUSY;
252 if (cl->c_cc) {
253 tp->t_state |= TS_TIMEOUT;
254 callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp);
255 }
256 if (cl->c_cc <= tp->t_lowat) {
257 if (tp->t_state & TS_ASLEEP) {
258 tp->t_state &= ~TS_ASLEEP;
259 wakeup(cl);
260 }
261 selwakeup(&tp->t_wsel);
262 }
263 splx(s);
264 }
265
266 static int
267 ofcons_param(tp, t)
268 struct tty *tp;
269 struct termios *t;
270 {
271 tp->t_ispeed = t->c_ispeed;
272 tp->t_ospeed = t->c_ospeed;
273 tp->t_cflag = t->c_cflag;
274 return 0;
275 }
276
277 static void
278 ofcons_pollin(aux)
279 void *aux;
280 {
281 struct ofcons_softc *sc = aux;
282 struct tty *tp = sc->of_tty;
283 char ch;
284
285 while (OF_read(stdin, &ch, 1) > 0) {
286 if (tp && (tp->t_state & TS_ISOPEN))
287 (*tp->t_linesw->l_rint)(ch, tp);
288 }
289 callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);
290 }
291
292 static int
293 ofcons_probe()
294 {
295 int chosen;
296 char stdinbuf[4], stdoutbuf[4];
297
298 if (stdin)
299 return 1;
300 if ((chosen = OF_finddevice("/chosen")) == -1)
301 return 0;
302 if (OF_getprop(chosen, "stdin", stdinbuf, sizeof stdinbuf) !=
303 sizeof stdinbuf ||
304 OF_getprop(chosen, "stdout", stdoutbuf, sizeof stdoutbuf) !=
305 sizeof stdoutbuf)
306 return 0;
307
308 /* Decode properties. */
309 stdin = of_decode_int(stdinbuf);
310 stdout = of_decode_int(stdoutbuf);
311
312 return 1;
313 }
314
315 void
316 ofcons_cnprobe(cd)
317 struct consdev *cd;
318 {
319 int maj;
320
321 if (!ofcons_probe())
322 return;
323
324 for (maj = 0; maj < nchrdev; maj++)
325 if (cdevsw[maj].d_open == ofcons_open)
326 break;
327 cd->cn_dev = makedev(maj, 0);
328 cd->cn_pri = CN_INTERNAL;
329 }
330
331 void
332 ofcons_cninit(cd)
333 struct consdev *cd;
334 {
335 }
336
337 int
338 ofcons_cngetc(dev)
339 dev_t dev;
340 {
341 unsigned char ch = '\0';
342 int l;
343
344 while ((l = OF_read(stdin, &ch, 1)) != 1)
345 if (l != -2 && l != 0)
346 return -1;
347 return ch;
348 }
349
350 void
351 ofcons_cnputc(dev, c)
352 dev_t dev;
353 int c;
354 {
355 char ch = c;
356
357 OF_write(stdout, &ch, 1);
358 }
359
360 void
361 ofcons_cnpollc(dev, on)
362 dev_t dev;
363 int on;
364 {
365 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
366
367 if (!sc)
368 return;
369 if (on) {
370 if (sc->of_flags & OFPOLL)
371 callout_stop(&sc->sc_poll_ch);
372 sc->of_flags &= ~OFPOLL;
373 } else {
374 if (!(sc->of_flags & OFPOLL)) {
375 sc->of_flags |= OFPOLL;
376 callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);
377 }
378 }
379 }
380