1 /* $NetBSD: ofcons.c,v 1.40 2009/05/12 14:39:22 cegger Exp $ */
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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>
39 #include <sys/device.h>
41 #include <sys/systm.h>
42 #include <sys/callout.h>
44 #include <sys/kauth.h>
48 #include <dev/ofw/openfirm.h>
53 struct callout sc_poll_ch
;
59 #define OFBURSTLEN 128 /* max number of bytes to write in one chunk */
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);
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"))
97 return OF_instance_to_package(stdin
) == oba
->oba_phandle
98 || OF_instance_to_package(stdout
) == oba
->oba_phandle
;
102 ofcons_attach(device_t parent
, device_t self
, void *aux
)
104 struct ofcons_softc
*sc
= device_private(self
);
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
;
121 sc
= device_lookup_private(&ofcons_cd
, minor(dev
));
124 if (!(tp
= sc
->of_tty
))
125 sc
->of_tty
= tp
= ttymalloc();
126 tp
->t_oproc
= ofcons_start
;
127 tp
->t_param
= ofcons_param
;
129 if (kauth_authorize_device_tty(l
->l_cred
, KAUTH_DEVICE_TTY_OPEN
, tp
))
131 if (!(tp
->t_state
& TS_ISOPEN
)) {
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
);
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
);
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
;
197 if ((error
= (*tp
->t_linesw
->l_ioctl
)(tp
, cmd
, data
, flag
, l
)) != EPASSTHROUGH
)
199 return ttioctl(tp
, cmd
, data
, flag
, l
);
203 ofcons_tty(dev_t dev
)
205 struct ofcons_softc
*sc
= device_lookup_private(&ofcons_cd
, minor(dev
));
211 ofcons_start(struct tty
*tp
)
214 u_char buf
[OFBURSTLEN
];
217 if (tp
->t_state
& (TS_TIMEOUT
| TS_BUSY
| TS_TTSTOP
)) {
221 tp
->t_state
|= TS_BUSY
;
223 len
= q_to_b(&tp
->t_outq
, buf
, OFBURSTLEN
);
224 OF_write(stdout
, buf
, len
);
226 tp
->t_state
&= ~TS_BUSY
;
228 tp
->t_state
|= TS_TIMEOUT
;
229 callout_schedule(&tp
->t_rstrt_ch
, 1);
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
;
244 ofcons_pollin(void *aux
)
246 struct ofcons_softc
*sc
= aux
;
247 struct tty
*tp
= sc
->of_tty
;
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
);
261 char stdinbuf
[4], stdoutbuf
[4];
265 if ((chosen
= OF_finddevice("/chosen")) == -1)
267 if (OF_getprop(chosen
, "stdin", stdinbuf
, sizeof stdinbuf
) !=
269 OF_getprop(chosen
, "stdout", stdoutbuf
, sizeof stdoutbuf
) !=
273 /* Decode properties. */
274 stdin
= of_decode_int(stdinbuf
);
275 stdout
= of_decode_int(stdoutbuf
);
281 ofcons_cnprobe(struct consdev
*cd
)
288 maj
= cdevsw_lookup_major(&ofcons_cdevsw
);
289 cd
->cn_dev
= makedev(maj
, 0);
290 cd
->cn_pri
= CN_INTERNAL
;
294 ofcons_cninit(struct consdev
*cd
)
299 ofcons_cngetc(dev_t dev
)
301 unsigned char ch
= '\0';
304 while ((l
= OF_read(stdin
, &ch
, 1)) != 1)
305 if (l
!= -2 && l
!= 0)
311 ofcons_cnputc(dev_t dev
, int c
)
315 OF_write(stdout
, &ch
, 1);
319 ofcons_cnpollc(dev_t dev
, int on
)
321 struct ofcons_softc
*sc
= device_lookup_private(&ofcons_cd
, minor(dev
));
326 if (sc
->of_flags
& OFPOLL
)
327 callout_stop(&sc
->sc_poll_ch
);
328 sc
->of_flags
&= ~OFPOLL
;
330 if (!(sc
->of_flags
& OFPOLL
)) {
331 sc
->of_flags
|= OFPOLL
;
332 callout_reset(&sc
->sc_poll_ch
, 1, ofcons_pollin
, sc
);