No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / ofw / ofcons.c
blobd0e07490c6e78827622c409b75bea06127ea395c
1 /* $NetBSD: ofcons.c,v 1.40 2009/05/12 14:39:22 cegger Exp $ */
3 /*
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH.
6 * All rights reserved.
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.
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.
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ofcons.c,v 1.40 2009/05/12 14:39:22 cegger Exp $");
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 #include <sys/kauth.h>
46 #include <dev/cons.h>
48 #include <dev/ofw/openfirm.h>
50 struct ofcons_softc {
51 struct device of_dev;
52 struct tty *of_tty;
53 struct callout sc_poll_ch;
54 int of_flags;
56 /* flags: */
57 #define OFPOLL 1
59 #define OFBURSTLEN 128 /* max number of bytes to write in one chunk */
61 cons_decl(ofcons_);
63 static int stdin, stdout;
65 static int ofcons_match(device_t, cfdata_t, void *);
66 static void ofcons_attach(device_t, device_t, void *);
68 CFATTACH_DECL(ofcons, sizeof(struct ofcons_softc),
69 ofcons_match, ofcons_attach, NULL, NULL);
71 extern struct cfdriver ofcons_cd;
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);
81 const struct cdevsw ofcons_cdevsw = {
82 ofcons_open, ofcons_close, ofcons_read, ofcons_write, ofcons_ioctl,
83 nostop, ofcons_tty, ofcons_poll, nommap, ttykqfilter, D_TTY
86 static int ofcons_probe(void);
88 static int
89 ofcons_match(device_t parent, cfdata_t match, void *aux)
91 struct ofbus_attach_args *oba = aux;
93 if (strcmp(oba->oba_busname, "ofw"))
94 return (0);
95 if (!ofcons_probe())
96 return 0;
97 return OF_instance_to_package(stdin) == oba->oba_phandle
98 || OF_instance_to_package(stdout) == oba->oba_phandle;
101 static void
102 ofcons_attach(device_t parent, device_t self, void *aux)
104 struct ofcons_softc *sc = device_private(self);
106 printf("\n");
108 callout_init(&sc->sc_poll_ch, 0);
111 static void ofcons_start(struct tty *);
112 static int ofcons_param(struct tty *, struct termios *);
113 static void ofcons_pollin(void *);
116 ofcons_open(dev_t dev, int flag, int mode, struct lwp *l)
118 struct ofcons_softc *sc;
119 struct tty *tp;
121 sc = device_lookup_private(&ofcons_cd, minor(dev));
122 if (!sc)
123 return ENXIO;
124 if (!(tp = sc->of_tty))
125 sc->of_tty = tp = ttymalloc();
126 tp->t_oproc = ofcons_start;
127 tp->t_param = ofcons_param;
128 tp->t_dev = dev;
129 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
130 return (EBUSY);
131 if (!(tp->t_state & TS_ISOPEN)) {
132 ttychars(tp);
133 tp->t_iflag = TTYDEF_IFLAG;
134 tp->t_oflag = TTYDEF_OFLAG;
135 tp->t_cflag = TTYDEF_CFLAG;
136 tp->t_lflag = TTYDEF_LFLAG;
137 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
138 ofcons_param(tp, &tp->t_termios);
139 ttsetwater(tp);
141 tp->t_state |= TS_CARR_ON;
143 if (!(sc->of_flags & OFPOLL)) {
144 sc->of_flags |= OFPOLL;
145 callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);
148 return (*tp->t_linesw->l_open)(dev, tp);
152 ofcons_close(dev_t dev, int flag, int mode, struct lwp *l)
154 struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, minor(dev));
155 struct tty *tp = sc->of_tty;
157 callout_stop(&sc->sc_poll_ch);
158 sc->of_flags &= ~OFPOLL;
159 (*tp->t_linesw->l_close)(tp, flag);
160 ttyclose(tp);
161 return 0;
165 ofcons_read(dev_t dev, struct uio *uio, int flag)
167 struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, minor(dev));
168 struct tty *tp = sc->of_tty;
170 return (*tp->t_linesw->l_read)(tp, uio, flag);
174 ofcons_write(dev_t dev, struct uio *uio, int flag)
176 struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, minor(dev));
177 struct tty *tp = sc->of_tty;
179 return (*tp->t_linesw->l_write)(tp, uio, flag);
183 ofcons_poll(dev_t dev, int events, struct lwp *l)
185 struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, minor(dev));
186 struct tty *tp = sc->of_tty;
188 return ((*tp->t_linesw->l_poll)(tp, events, l));
191 ofcons_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
193 struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, minor(dev));
194 struct tty *tp = sc->of_tty;
195 int error;
197 if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) != EPASSTHROUGH)
198 return error;
199 return ttioctl(tp, cmd, data, flag, l);
202 struct tty *
203 ofcons_tty(dev_t dev)
205 struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, minor(dev));
207 return sc->of_tty;
210 static void
211 ofcons_start(struct tty *tp)
213 int s, len;
214 u_char buf[OFBURSTLEN];
216 s = spltty();
217 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
218 splx(s);
219 return;
221 tp->t_state |= TS_BUSY;
222 splx(s);
223 len = q_to_b(&tp->t_outq, buf, OFBURSTLEN);
224 OF_write(stdout, buf, len);
225 s = spltty();
226 tp->t_state &= ~TS_BUSY;
227 if (ttypull(tp)) {
228 tp->t_state |= TS_TIMEOUT;
229 callout_schedule(&tp->t_rstrt_ch, 1);
231 splx(s);
234 static int
235 ofcons_param(struct tty *tp, struct termios *t)
237 tp->t_ispeed = t->c_ispeed;
238 tp->t_ospeed = t->c_ospeed;
239 tp->t_cflag = t->c_cflag;
240 return 0;
243 static void
244 ofcons_pollin(void *aux)
246 struct ofcons_softc *sc = aux;
247 struct tty *tp = sc->of_tty;
248 char ch;
250 while (OF_read(stdin, &ch, 1) > 0) {
251 if (tp && (tp->t_state & TS_ISOPEN))
252 (*tp->t_linesw->l_rint)(ch, tp);
254 callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);
257 static int
258 ofcons_probe(void)
260 int chosen;
261 char stdinbuf[4], stdoutbuf[4];
263 if (stdin)
264 return 1;
265 if ((chosen = OF_finddevice("/chosen")) == -1)
266 return 0;
267 if (OF_getprop(chosen, "stdin", stdinbuf, sizeof stdinbuf) !=
268 sizeof stdinbuf ||
269 OF_getprop(chosen, "stdout", stdoutbuf, sizeof stdoutbuf) !=
270 sizeof stdoutbuf)
271 return 0;
273 /* Decode properties. */
274 stdin = of_decode_int(stdinbuf);
275 stdout = of_decode_int(stdoutbuf);
277 return 1;
280 void
281 ofcons_cnprobe(struct consdev *cd)
283 int maj;
285 if (!ofcons_probe())
286 return;
288 maj = cdevsw_lookup_major(&ofcons_cdevsw);
289 cd->cn_dev = makedev(maj, 0);
290 cd->cn_pri = CN_INTERNAL;
293 void
294 ofcons_cninit(struct consdev *cd)
299 ofcons_cngetc(dev_t dev)
301 unsigned char ch = '\0';
302 int l;
304 while ((l = OF_read(stdin, &ch, 1)) != 1)
305 if (l != -2 && l != 0)
306 return -1;
307 return ch;
310 void
311 ofcons_cnputc(dev_t dev, int c)
313 char ch = c;
315 OF_write(stdout, &ch, 1);
318 void
319 ofcons_cnpollc(dev_t dev, int on)
321 struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, minor(dev));
323 if (!sc)
324 return;
325 if (on) {
326 if (sc->of_flags & OFPOLL)
327 callout_stop(&sc->sc_poll_ch);
328 sc->of_flags &= ~OFPOLL;
329 } else {
330 if (!(sc->of_flags & OFPOLL)) {
331 sc->of_flags |= OFPOLL;
332 callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);