1 /* $NetBSD: pcons.c,v 1.28 2007/11/19 18:51:43 ad 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.28 2007/11/19 18:51:43 ad 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/openfirm.h>
56 #include <machine/cpu.h>
57 #include <machine/eeprom.h>
58 #include <machine/psl.h>
62 #include <sparc64/dev/cons.h>
64 static int pconsmatch(struct device
*, struct cfdata
*, void *);
65 static void pconsattach(struct device
*, struct device
*, void *);
67 CFATTACH_DECL(pcons
, sizeof(struct pconssoftc
),
68 pconsmatch
, pconsattach
, NULL
, NULL
);
70 extern struct cfdriver pcons_cd
;
72 dev_type_open(pconsopen
);
73 dev_type_close(pconsclose
);
74 dev_type_read(pconsread
);
75 dev_type_write(pconswrite
);
76 dev_type_ioctl(pconsioctl
);
77 dev_type_tty(pconstty
);
78 dev_type_poll(pconspoll
);
80 const struct cdevsw pcons_cdevsw
= {
81 pconsopen
, pconsclose
, pconsread
, pconswrite
, pconsioctl
,
82 nostop
, pconstty
, pconspoll
, nommap
, ttykqfilter
, D_TTY
85 static struct cnm_state pcons_cnm_state
;
87 static int pconsprobe(void);
88 extern struct consdev
*cn_tab
;
91 pconsmatch(struct device
*parent
, struct cfdata
*match
, void *aux
)
93 struct mainbus_attach_args
*ma
= aux
;
94 extern int prom_cngetc(dev_t
);
96 /* Only attach if no other console has attached. */
97 return ((strcmp("pcons", ma
->ma_name
) == 0) &&
98 (cn_tab
->cn_getc
== prom_cngetc
));
103 pconsattach(struct device
*parent
, struct device
*self
, void *aux
)
105 struct pconssoftc
*sc
= (struct pconssoftc
*) self
;
111 cn_init_magic(&pcons_cnm_state
);
112 cn_set_magic("+++++");
113 callout_init(&sc
->sc_poll_ch
, 0);
116 static void pconsstart(struct tty
*);
117 static int pconsparam(struct tty
*, struct termios
*);
118 static void pcons_poll(void *);
121 pconsopen(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
123 struct pconssoftc
*sc
;
129 sc
= device_lookup_private(&pcons_cd
, minor(dev
));
132 if (!(tp
= sc
->of_tty
))
133 sc
->of_tty
= tp
= ttymalloc();
134 tp
->t_oproc
= pconsstart
;
135 tp
->t_param
= pconsparam
;
137 cn_tab
->cn_dev
= dev
;
138 if (kauth_authorize_device_tty(l
->l_cred
, KAUTH_DEVICE_TTY_OPEN
, tp
))
140 if (!(tp
->t_state
& TS_ISOPEN
)) {
142 tp
->t_iflag
= TTYDEF_IFLAG
;
143 tp
->t_oflag
= TTYDEF_OFLAG
;
144 tp
->t_cflag
= TTYDEF_CFLAG
;
145 tp
->t_lflag
= TTYDEF_LFLAG
;
146 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
147 pconsparam(tp
, &tp
->t_termios
);
150 tp
->t_state
|= TS_CARR_ON
;
152 if (!(sc
->of_flags
& OFPOLL
)) {
153 sc
->of_flags
|= OFPOLL
;
154 callout_reset(&sc
->sc_poll_ch
, 1, pcons_poll
, sc
);
157 return (*tp
->t_linesw
->l_open
)(dev
, tp
);
161 pconsclose(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
163 struct pconssoftc
*sc
= device_lookup_private(&pcons_cd
,minor(dev
));
164 struct tty
*tp
= sc
->of_tty
;
166 callout_stop(&sc
->sc_poll_ch
);
167 sc
->of_flags
&= ~OFPOLL
;
168 (*tp
->t_linesw
->l_close
)(tp
, flag
);
174 pconsread(dev_t dev
, struct uio
*uio
, int flag
)
176 struct pconssoftc
*sc
= device_lookup_private(&pcons_cd
, minor(dev
));
177 struct tty
*tp
= sc
->of_tty
;
179 return (*tp
->t_linesw
->l_read
)(tp
, uio
, flag
);
183 pconswrite(dev_t dev
, struct uio
*uio
, int flag
)
185 struct pconssoftc
*sc
= device_lookup_private(&pcons_cd
, minor(dev
));
186 struct tty
*tp
= sc
->of_tty
;
188 return (*tp
->t_linesw
->l_write
)(tp
, uio
, flag
);
192 pconspoll(dev_t dev
, int events
, struct lwp
*l
)
194 struct pconssoftc
*sc
= device_lookup_private(&pcons_cd
, minor(dev
));
195 struct tty
*tp
= sc
->of_tty
;
197 return ((*tp
->t_linesw
->l_poll
)(tp
, events
, l
));
201 pconsioctl(dev_t dev
, u_long cmd
, void *data
, int flag
, struct lwp
*l
)
203 struct pconssoftc
*sc
= device_lookup_private(&pcons_cd
, minor(dev
));
204 struct tty
*tp
= sc
->of_tty
;
207 if ((error
= (*tp
->t_linesw
->l_ioctl
)(tp
, cmd
, data
, flag
, l
)) != EPASSTHROUGH
)
209 return ttioctl(tp
, cmd
, data
, flag
, l
);
215 struct pconssoftc
*sc
= device_lookup_private(&pcons_cd
, minor(dev
));
221 pconsstart(struct tty
*tp
)
225 u_char buf
[OFBURSTLEN
];
228 if (tp
->t_state
& (TS_TIMEOUT
| TS_BUSY
| TS_TTSTOP
)) {
232 tp
->t_state
|= TS_BUSY
;
235 len
= q_to_b(cl
, buf
, OFBURSTLEN
);
236 prom_write(prom_stdout(), buf
, len
);
238 tp
->t_state
&= ~TS_BUSY
;
240 tp
->t_state
|= TS_TIMEOUT
;
241 callout_schedule(&tp
->t_rstrt_ch
, 1);
247 pconsparam(struct tty
*tp
, struct termios
*t
)
249 tp
->t_ispeed
= t
->c_ispeed
;
250 tp
->t_ospeed
= t
->c_ospeed
;
251 tp
->t_cflag
= t
->c_cflag
;
256 pcons_poll(void *aux
)
258 struct pconssoftc
*sc
= aux
;
259 struct tty
*tp
= sc
->of_tty
;
262 while (prom_read(prom_stdin(), &ch
, 1) > 0) {
263 cn_check_magic(tp
->t_dev
, ch
, pcons_cnm_state
);
264 if (tp
&& (tp
->t_state
& TS_ISOPEN
))
265 (*tp
->t_linesw
->l_rint
)(ch
, tp
);
267 callout_reset(&sc
->sc_poll_ch
, 1, pcons_poll
, sc
);
274 return (prom_stdin() && prom_stdout());
278 pcons_cnpollc(dev_t dev
, int on
)
280 struct pconssoftc
*sc
;
282 sc
= device_lookup_private(&pcons_cd
, minor(dev
));
287 if (sc
->of_flags
& OFPOLL
)
288 callout_stop(&sc
->sc_poll_ch
);
289 sc
->of_flags
&= ~OFPOLL
;
291 /* Resuming kernel. */
292 if (!(sc
->of_flags
& OFPOLL
)) {
293 sc
->of_flags
|= OFPOLL
;
294 callout_reset(&sc
->sc_poll_ch
, 1, pcons_poll
, sc
);
299 void pcons_dopoll(void);
303 pcons_poll(device_lookup_private(&pcons_cd
, 0));