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