1 /* $NetBSD: pcons.c,v 1.18 2008/06/13 13:11:26 cegger Exp $ */
4 * Copyright (c) 2000 Eduardo E. Horvath
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Default console driver. Uses the PROM or whatever
33 * driver(s) are appropriate.
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: pcons.c,v 1.18 2008/06/13 13:11:26 cegger Exp $");
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/device.h>
46 #include <sys/ioctl.h>
47 #include <sys/kernel.h>
51 #include <sys/syslog.h>
52 #include <sys/kauth.h>
54 #include <machine/autoconf.h>
55 #include <machine/promlib.h>
56 #include <machine/cpu.h>
57 #include <machine/psl.h>
61 #include <sun2/dev/cons.h>
65 static int pconsmatch(device_t
, cfdata_t
, void *);
66 static void pconsattach(device_t
, device_t
, void *);
68 CFATTACH_DECL_NEW(pcons
, sizeof(struct pconssoftc
),
69 pconsmatch
, pconsattach
, NULL
, NULL
);
71 static struct cnm_state pcons_cnm_state
;
73 static int pconsprobe(void);
75 dev_type_open(pconsopen
);
76 dev_type_close(pconsclose
);
77 dev_type_read(pconsread
);
78 dev_type_write(pconswrite
);
79 dev_type_ioctl(pconsioctl
);
80 dev_type_tty(pconstty
);
81 dev_type_poll(pconspoll
);
83 const struct cdevsw pcons_cdevsw
= {
84 pconsopen
, pconsclose
, pconsread
, pconswrite
, pconsioctl
,
85 nostop
, pconstty
, pconspoll
, nommap
, ttykqfilter
, D_TTY
89 pconsmatch(device_t parent
, cfdata_t cf
, void *aux
)
91 struct mainbus_attach_args
*ma
= aux
;
92 extern int prom_cngetc(dev_t
);
94 /* Only attach if no other console has attached. */
95 return (ma
->ma_name
!= NULL
) &&
96 (strcmp("pcons", ma
->ma_name
) == 0) &&
97 (cn_tab
->cn_getc
== prom_cngetc
);
102 pconsattach(device_t parent
, device_t self
, void *aux
)
104 struct pconssoftc
*sc
= device_private(self
);
110 cn_init_magic(&pcons_cnm_state
);
111 cn_set_magic("+++++");
112 callout_init(&sc
->sc_poll_ch
, 0);
115 static void pconsstart(struct tty
*);
116 static int pconsparam(struct tty
*, struct termios
*);
117 static void pcons_poll(void *);
120 pconsopen(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
122 struct pconssoftc
*sc
;
125 sc
= device_lookup_private(&pcons_cd
, minor(dev
));
128 if ((tp
= sc
->of_tty
) == NULL
)
129 sc
->of_tty
= tp
= ttymalloc();
130 tp
->t_oproc
= pconsstart
;
131 tp
->t_param
= pconsparam
;
133 cn_tab
->cn_dev
= dev
;
134 if (kauth_authorize_device_tty(l
->l_cred
, KAUTH_DEVICE_TTY_OPEN
, tp
))
136 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
138 tp
->t_iflag
= TTYDEF_IFLAG
;
139 tp
->t_oflag
= TTYDEF_OFLAG
;
140 tp
->t_cflag
= TTYDEF_CFLAG
;
141 tp
->t_lflag
= TTYDEF_LFLAG
;
142 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
143 pconsparam(tp
, &tp
->t_termios
);
146 tp
->t_state
|= TS_CARR_ON
;
148 if ((sc
->of_flags
& OFPOLL
) == 0) {
149 sc
->of_flags
|= OFPOLL
;
150 callout_reset(&sc
->sc_poll_ch
, 1, pcons_poll
, sc
);
153 return (*tp
->t_linesw
->l_open
)(dev
, tp
);
157 pconsclose(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
159 struct pconssoftc
*sc
= device_lookup_private(&pcons_cd
, minor(dev
));
160 struct tty
*tp
= sc
->of_tty
;
162 callout_stop(&sc
->sc_poll_ch
);
163 sc
->of_flags
&= ~OFPOLL
;
164 (*tp
->t_linesw
->l_close
)(tp
, flag
);
170 pconsread(dev_t dev
, struct uio
*uio
, int flag
)
172 struct pconssoftc
*sc
= device_lookup_private(&pcons_cd
, minor(dev
));
173 struct tty
*tp
= sc
->of_tty
;
175 return (*tp
->t_linesw
->l_read
)(tp
, uio
, flag
);
179 pconswrite(dev_t dev
, struct uio
*uio
, int flag
)
181 struct pconssoftc
*sc
= device_lookup_private(&pcons_cd
, minor(dev
));
182 struct tty
*tp
= sc
->of_tty
;
184 return (*tp
->t_linesw
->l_write
)(tp
, uio
, flag
);
188 pconspoll(dev_t dev
, int events
, struct lwp
*l
)
190 struct pconssoftc
*sc
= device_lookup_private(&pcons_cd
, minor(dev
));
191 struct tty
*tp
= sc
->of_tty
;
193 return (*tp
->t_linesw
->l_poll
)(tp
, events
, l
);
197 pconsioctl(dev_t dev
, u_long cmd
, void *data
, int flag
, struct lwp
*l
)
199 struct pconssoftc
*sc
= device_lookup_private(&pcons_cd
, minor(dev
));
200 struct tty
*tp
= sc
->of_tty
;
203 if ((error
= (*tp
->t_linesw
->l_ioctl
)(tp
, cmd
, data
, flag
, l
)) >= 0)
205 if ((error
= ttioctl(tp
, cmd
, data
, flag
, l
)) >= 0)
213 struct pconssoftc
*sc
= device_lookup_private(&pcons_cd
, minor(dev
));
219 pconsstart(struct tty
*tp
)
223 uint8_t buf
[OFBURSTLEN
];
226 if (tp
->t_state
& (TS_TIMEOUT
| TS_BUSY
| TS_TTSTOP
)) {
230 tp
->t_state
|= TS_BUSY
;
233 len
= q_to_b(cl
, buf
, OFBURSTLEN
);
234 prom_putstr(buf
, len
);
236 tp
->t_state
&= ~TS_BUSY
;
238 tp
->t_state
|= TS_TIMEOUT
;
239 callout_schedule(&tp
->t_rstrt_ch
, 1);
245 pconsparam(struct tty
*tp
, struct termios
*t
)
248 tp
->t_ispeed
= t
->c_ispeed
;
249 tp
->t_ospeed
= t
->c_ospeed
;
250 tp
->t_cflag
= t
->c_cflag
;
255 pcons_poll(void *aux
)
257 struct pconssoftc
*sc
= aux
;
258 struct tty
*tp
= sc
->of_tty
;
262 while ((c
= prom_peekchar()) >= 0) {
264 cn_check_magic(tp
->t_dev
, ch
, pcons_cnm_state
);
265 if (tp
&& (tp
->t_state
& TS_ISOPEN
))
266 (*tp
->t_linesw
->l_rint
)(ch
, tp
);
268 callout_reset(&sc
->sc_poll_ch
, 1, pcons_poll
, sc
);
275 switch (prom_version()) {
280 #endif /* PROM_OLDMON */
285 return prom_stdin() && prom_stdout();
286 #endif /* PROM_OBP_V2 */
294 pcons_cnpollc(dev_t dev
, int on
)
296 struct pconssoftc
*sc
;
298 sc
= device_lookup_private(&pcons_cd
, minor(dev
));
303 if (sc
->of_flags
& OFPOLL
)
304 callout_stop(&sc
->sc_poll_ch
);
305 sc
->of_flags
&= ~OFPOLL
;
307 /* Resuming kernel. */
308 if ((sc
->of_flags
& OFPOLL
) == 0) {
309 sc
->of_flags
|= OFPOLL
;
310 callout_reset(&sc
->sc_poll_ch
, 1, pcons_poll
, sc
);
315 void pcons_dopoll(void);
321 pcons_poll(device_lookup_private(&pcons_cd
, 0));