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