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