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