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