ofcons.c revision 1.7 1 1.7 cgd /* $NetBSD: ofcons.c,v 1.7 1998/01/26 21:49:00 cgd Exp $ */
2 1.1 ws
3 1.1 ws /*
4 1.1 ws * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 1.1 ws * Copyright (C) 1995, 1996 TooLs GmbH.
6 1.1 ws * All rights reserved.
7 1.1 ws *
8 1.1 ws * Redistribution and use in source and binary forms, with or without
9 1.1 ws * modification, are permitted provided that the following conditions
10 1.1 ws * are met:
11 1.1 ws * 1. Redistributions of source code must retain the above copyright
12 1.1 ws * notice, this list of conditions and the following disclaimer.
13 1.1 ws * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 ws * notice, this list of conditions and the following disclaimer in the
15 1.1 ws * documentation and/or other materials provided with the distribution.
16 1.1 ws * 3. All advertising materials mentioning features or use of this software
17 1.1 ws * must display the following acknowledgement:
18 1.1 ws * This product includes software developed by TooLs GmbH.
19 1.1 ws * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 1.1 ws * derived from this software without specific prior written permission.
21 1.1 ws *
22 1.1 ws * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 1.1 ws * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 ws * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 ws * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 1.1 ws * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 1.1 ws * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 1.1 ws * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.1 ws * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 1.1 ws * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 1.1 ws * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 ws */
33 1.1 ws
34 1.1 ws #include <sys/param.h>
35 1.1 ws #include <sys/conf.h>
36 1.1 ws #include <sys/device.h>
37 1.1 ws #include <sys/proc.h>
38 1.1 ws #include <sys/systm.h>
39 1.1 ws #include <sys/tty.h>
40 1.1 ws
41 1.1 ws #include <dev/cons.h>
42 1.1 ws
43 1.1 ws #include <dev/ofw/openfirm.h>
44 1.1 ws
45 1.1 ws struct ofc_softc {
46 1.1 ws struct device of_dev;
47 1.1 ws struct tty *of_tty;
48 1.1 ws int of_flags;
49 1.1 ws };
50 1.1 ws /* flags: */
51 1.1 ws #define OFPOLL 1
52 1.1 ws
53 1.1 ws #define OFBURSTLEN 128 /* max number of bytes to write in one chunk */
54 1.1 ws
55 1.1 ws static int stdin, stdout;
56 1.1 ws
57 1.4 thorpej static int ofcmatch __P((struct device *, struct cfdata *, void *));
58 1.1 ws static void ofcattach __P((struct device *, struct device *, void *));
59 1.1 ws
60 1.1 ws struct cfattach ofcons_ca = {
61 1.1 ws sizeof(struct ofc_softc), ofcmatch, ofcattach
62 1.1 ws };
63 1.1 ws
64 1.6 thorpej extern struct cfdriver ofcons_cd;
65 1.1 ws
66 1.1 ws static int ofcprobe __P((void));
67 1.1 ws
68 1.1 ws static int
69 1.1 ws ofcmatch(parent, match, aux)
70 1.1 ws struct device *parent;
71 1.4 thorpej struct cfdata *match;
72 1.4 thorpej void *aux;
73 1.1 ws {
74 1.1 ws struct ofprobe *ofp = aux;
75 1.1 ws
76 1.1 ws if (!ofcprobe())
77 1.1 ws return 0;
78 1.1 ws return OF_instance_to_package(stdin) == ofp->phandle
79 1.1 ws || OF_instance_to_package(stdout) == ofp->phandle;
80 1.1 ws }
81 1.1 ws
82 1.1 ws static void
83 1.1 ws ofcattach(parent, self, aux)
84 1.1 ws struct device *parent, *self;
85 1.1 ws void *aux;
86 1.1 ws {
87 1.3 christos printf("\n");
88 1.1 ws }
89 1.1 ws
90 1.1 ws static void ofcstart __P((struct tty *));
91 1.1 ws static int ofcparam __P((struct tty *, struct termios *));
92 1.1 ws static void ofcpoll __P((void *));
93 1.1 ws
94 1.1 ws int
95 1.1 ws ofcopen(dev, flag, mode, p)
96 1.1 ws dev_t dev;
97 1.1 ws int flag, mode;
98 1.1 ws struct proc *p;
99 1.1 ws {
100 1.1 ws struct ofc_softc *sc;
101 1.1 ws int unit = minor(dev);
102 1.1 ws struct tty *tp;
103 1.1 ws
104 1.1 ws if (unit >= ofcons_cd.cd_ndevs)
105 1.1 ws return ENXIO;
106 1.1 ws sc = ofcons_cd.cd_devs[unit];
107 1.1 ws if (!sc)
108 1.1 ws return ENXIO;
109 1.1 ws if (!(tp = sc->of_tty))
110 1.1 ws sc->of_tty = tp = ttymalloc();
111 1.1 ws tp->t_oproc = ofcstart;
112 1.1 ws tp->t_param = ofcparam;
113 1.1 ws tp->t_dev = dev;
114 1.1 ws if (!(tp->t_state & TS_ISOPEN)) {
115 1.1 ws tp->t_state |= TS_WOPEN;
116 1.1 ws ttychars(tp);
117 1.1 ws tp->t_iflag = TTYDEF_IFLAG;
118 1.1 ws tp->t_oflag = TTYDEF_OFLAG;
119 1.1 ws tp->t_cflag = TTYDEF_CFLAG;
120 1.1 ws tp->t_lflag = TTYDEF_LFLAG;
121 1.1 ws tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
122 1.1 ws ofcparam(tp, &tp->t_termios);
123 1.1 ws ttsetwater(tp);
124 1.1 ws } else if ((tp->t_state&TS_XCLUDE) && suser(p->p_ucred, &p->p_acflag))
125 1.1 ws return EBUSY;
126 1.1 ws tp->t_state |= TS_CARR_ON;
127 1.1 ws
128 1.1 ws if (!(sc->of_flags & OFPOLL)) {
129 1.1 ws sc->of_flags |= OFPOLL;
130 1.1 ws timeout(ofcpoll, sc, 1);
131 1.1 ws }
132 1.1 ws
133 1.1 ws return (*linesw[tp->t_line].l_open)(dev, tp);
134 1.1 ws }
135 1.1 ws
136 1.1 ws int
137 1.1 ws ofcclose(dev, flag, mode, p)
138 1.1 ws dev_t dev;
139 1.1 ws int flag, mode;
140 1.1 ws struct proc *p;
141 1.1 ws {
142 1.1 ws struct ofc_softc *sc = ofcons_cd.cd_devs[minor(dev)];
143 1.1 ws struct tty *tp = sc->of_tty;
144 1.1 ws
145 1.1 ws untimeout(ofcpoll, sc);
146 1.1 ws sc->of_flags &= ~OFPOLL;
147 1.1 ws (*linesw[tp->t_line].l_close)(tp, flag);
148 1.1 ws ttyclose(tp);
149 1.1 ws return 0;
150 1.1 ws }
151 1.1 ws
152 1.1 ws int
153 1.1 ws ofcread(dev, uio, flag)
154 1.1 ws dev_t dev;
155 1.1 ws struct uio *uio;
156 1.1 ws int flag;
157 1.1 ws {
158 1.1 ws struct ofc_softc *sc = ofcons_cd.cd_devs[minor(dev)];
159 1.1 ws struct tty *tp = sc->of_tty;
160 1.1 ws
161 1.1 ws return (*linesw[tp->t_line].l_read)(tp, uio, flag);
162 1.1 ws }
163 1.1 ws
164 1.1 ws int
165 1.1 ws ofcwrite(dev, uio, flag)
166 1.1 ws dev_t dev;
167 1.1 ws struct uio *uio;
168 1.1 ws int flag;
169 1.1 ws {
170 1.1 ws struct ofc_softc *sc = ofcons_cd.cd_devs[minor(dev)];
171 1.1 ws struct tty *tp = sc->of_tty;
172 1.1 ws
173 1.1 ws return (*linesw[tp->t_line].l_write)(tp, uio, flag);
174 1.1 ws }
175 1.1 ws
176 1.1 ws int
177 1.1 ws ofcioctl(dev, cmd, data, flag, p)
178 1.1 ws dev_t dev;
179 1.1 ws u_long cmd;
180 1.1 ws caddr_t data;
181 1.1 ws int flag;
182 1.1 ws struct proc *p;
183 1.1 ws {
184 1.1 ws struct ofc_softc *sc = ofcons_cd.cd_devs[minor(dev)];
185 1.1 ws struct tty *tp = sc->of_tty;
186 1.1 ws int error;
187 1.1 ws
188 1.1 ws if ((error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p)) >= 0)
189 1.1 ws return error;
190 1.1 ws if ((error = ttioctl(tp, cmd, data, flag, p)) >= 0)
191 1.1 ws return error;
192 1.1 ws return ENOTTY;
193 1.1 ws }
194 1.1 ws
195 1.1 ws struct tty *
196 1.1 ws ofctty(dev)
197 1.1 ws dev_t dev;
198 1.1 ws {
199 1.1 ws struct ofc_softc *sc = ofcons_cd.cd_devs[minor(dev)];
200 1.1 ws
201 1.1 ws return sc->of_tty;
202 1.1 ws }
203 1.1 ws
204 1.1 ws void
205 1.1 ws ofcstop(tp, flag)
206 1.1 ws struct tty *tp;
207 1.1 ws int flag;
208 1.1 ws {
209 1.1 ws }
210 1.1 ws
211 1.1 ws static void
212 1.1 ws ofcstart(tp)
213 1.1 ws struct tty *tp;
214 1.1 ws {
215 1.1 ws struct clist *cl;
216 1.1 ws int s, len;
217 1.1 ws u_char buf[OFBURSTLEN];
218 1.1 ws
219 1.1 ws s = spltty();
220 1.1 ws if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
221 1.1 ws splx(s);
222 1.1 ws return;
223 1.1 ws }
224 1.1 ws tp->t_state |= TS_BUSY;
225 1.1 ws splx(s);
226 1.1 ws cl = &tp->t_outq;
227 1.1 ws len = q_to_b(cl, buf, OFBURSTLEN);
228 1.1 ws OF_write(stdout, buf, len);
229 1.1 ws s = spltty();
230 1.1 ws tp->t_state &= ~TS_BUSY;
231 1.1 ws if (cl->c_cc) {
232 1.1 ws tp->t_state |= TS_TIMEOUT;
233 1.1 ws timeout(ttrstrt, (void *)tp, 1);
234 1.1 ws }
235 1.1 ws if (cl->c_cc <= tp->t_lowat) {
236 1.1 ws if (tp->t_state & TS_ASLEEP) {
237 1.1 ws tp->t_state &= ~TS_ASLEEP;
238 1.1 ws wakeup(cl);
239 1.1 ws }
240 1.1 ws selwakeup(&tp->t_wsel);
241 1.1 ws }
242 1.1 ws splx(s);
243 1.1 ws }
244 1.1 ws
245 1.1 ws static int
246 1.1 ws ofcparam(tp, t)
247 1.1 ws struct tty *tp;
248 1.1 ws struct termios *t;
249 1.1 ws {
250 1.1 ws tp->t_ispeed = t->c_ispeed;
251 1.1 ws tp->t_ospeed = t->c_ospeed;
252 1.1 ws tp->t_cflag = t->c_cflag;
253 1.1 ws return 0;
254 1.1 ws }
255 1.1 ws
256 1.1 ws static void
257 1.1 ws ofcpoll(aux)
258 1.1 ws void *aux;
259 1.1 ws {
260 1.1 ws struct ofc_softc *sc = aux;
261 1.1 ws struct tty *tp = sc->of_tty;
262 1.1 ws char ch;
263 1.1 ws
264 1.1 ws while (OF_read(stdin, &ch, 1) > 0) {
265 1.1 ws if (tp && (tp->t_state & TS_ISOPEN))
266 1.1 ws (*linesw[tp->t_line].l_rint)(ch, tp);
267 1.1 ws }
268 1.1 ws timeout(ofcpoll, sc, 1);
269 1.1 ws }
270 1.1 ws
271 1.1 ws static int
272 1.1 ws ofcprobe()
273 1.1 ws {
274 1.1 ws int chosen;
275 1.7 cgd char stdinbuf[4], stdoutbuf[4];
276 1.1 ws
277 1.1 ws if (stdin)
278 1.1 ws return 1;
279 1.1 ws if ((chosen = OF_finddevice("/chosen")) == -1)
280 1.1 ws return 0;
281 1.7 cgd if (OF_getprop(chosen, "stdin", stdinbuf, sizeof stdinbuf) !=
282 1.7 cgd sizeof stdinbuf ||
283 1.7 cgd OF_getprop(chosen, "stdout", stdoutbuf, sizeof stdoutbuf) !=
284 1.7 cgd sizeof stdoutbuf)
285 1.1 ws return 0;
286 1.7 cgd
287 1.7 cgd /* Decode properties. */
288 1.7 cgd stdin = of_decode_int(stdinbuf);
289 1.7 cgd stdout = of_decode_int(stdoutbuf);
290 1.7 cgd
291 1.1 ws return 1;
292 1.1 ws }
293 1.1 ws
294 1.1 ws void
295 1.1 ws ofccnprobe(cd)
296 1.1 ws struct consdev *cd;
297 1.1 ws {
298 1.1 ws int maj;
299 1.1 ws
300 1.1 ws if (!ofcprobe())
301 1.1 ws return;
302 1.1 ws
303 1.1 ws for (maj = 0; maj < nchrdev; maj++)
304 1.1 ws if (cdevsw[maj].d_open == ofcopen)
305 1.1 ws break;
306 1.1 ws cd->cn_dev = makedev(maj, 0);
307 1.1 ws cd->cn_pri = CN_INTERNAL;
308 1.1 ws }
309 1.1 ws
310 1.1 ws void
311 1.1 ws ofccninit(cd)
312 1.1 ws struct consdev *cd;
313 1.1 ws {
314 1.1 ws }
315 1.1 ws
316 1.1 ws int
317 1.1 ws ofccngetc(dev)
318 1.1 ws dev_t dev;
319 1.1 ws {
320 1.5 mycroft unsigned char ch = '\0';
321 1.1 ws int l;
322 1.1 ws
323 1.1 ws while ((l = OF_read(stdin, &ch, 1)) != 1)
324 1.5 mycroft if (l != -2 && l != 0)
325 1.1 ws return -1;
326 1.1 ws return ch;
327 1.1 ws }
328 1.1 ws
329 1.1 ws void
330 1.1 ws ofccnputc(dev, c)
331 1.1 ws dev_t dev;
332 1.1 ws int c;
333 1.1 ws {
334 1.1 ws char ch = c;
335 1.1 ws
336 1.1 ws OF_write(stdout, &ch, 1);
337 1.1 ws }
338 1.1 ws
339 1.1 ws void
340 1.1 ws ofccnpollc(dev, on)
341 1.1 ws dev_t dev;
342 1.1 ws int on;
343 1.1 ws {
344 1.1 ws struct ofc_softc *sc = ofcons_cd.cd_devs[minor(dev)];
345 1.1 ws
346 1.1 ws if (!sc)
347 1.1 ws return;
348 1.1 ws if (on) {
349 1.1 ws if (sc->of_flags & OFPOLL)
350 1.1 ws untimeout(ofcpoll, sc);
351 1.1 ws sc->of_flags &= ~OFPOLL;
352 1.1 ws } else {
353 1.1 ws if (!(sc->of_flags & OFPOLL)) {
354 1.1 ws sc->of_flags |= OFPOLL;
355 1.1 ws timeout(ofcpoll, sc, 1);
356 1.1 ws }
357 1.1 ws }
358 1.1 ws }
359