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