Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / sparc64 / dev / pcons.c
blob01b8d3c50a58a51ca1f35101a1c3de43fba6b1fa
1 /* $NetBSD: pcons.c,v 1.28 2007/11/19 18:51:43 ad Exp $ */
3 /*-
4 * Copyright (c) 2000 Eduardo E. Horvath
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
28 * SUCH DAMAGE.
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 $");
39 #include "opt_ddb.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/conf.h>
44 #include <sys/device.h>
45 #include <sys/file.h>
46 #include <sys/ioctl.h>
47 #include <sys/kernel.h>
48 #include <sys/proc.h>
49 #include <sys/tty.h>
50 #include <sys/time.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>
60 #include <dev/cons.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;
90 static int
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));
102 static void
103 pconsattach(struct device *parent, struct device *self, void *aux)
105 struct pconssoftc *sc = (struct pconssoftc *) self;
107 printf("\n");
108 if (!pconsprobe())
109 return;
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;
124 struct tty *tp;
125 struct proc *p;
127 p = l->l_proc;
129 sc = device_lookup_private(&pcons_cd, minor(dev));
130 if (!sc)
131 return ENXIO;
132 if (!(tp = sc->of_tty))
133 sc->of_tty = tp = ttymalloc();
134 tp->t_oproc = pconsstart;
135 tp->t_param = pconsparam;
136 tp->t_dev = dev;
137 cn_tab->cn_dev = dev;
138 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
139 return (EBUSY);
140 if (!(tp->t_state & TS_ISOPEN)) {
141 ttychars(tp);
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);
148 ttsetwater(tp);
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);
169 ttyclose(tp);
170 return 0;
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;
205 int error;
207 if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) != EPASSTHROUGH)
208 return error;
209 return ttioctl(tp, cmd, data, flag, l);
212 struct tty *
213 pconstty(dev_t dev)
215 struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
217 return sc->of_tty;
220 static void
221 pconsstart(struct tty *tp)
223 struct clist *cl;
224 int s, len;
225 u_char buf[OFBURSTLEN];
227 s = spltty();
228 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
229 splx(s);
230 return;
232 tp->t_state |= TS_BUSY;
233 splx(s);
234 cl = &tp->t_outq;
235 len = q_to_b(cl, buf, OFBURSTLEN);
236 prom_write(prom_stdout(), buf, len);
237 s = spltty();
238 tp->t_state &= ~TS_BUSY;
239 if (ttypull(tp)) {
240 tp->t_state |= TS_TIMEOUT;
241 callout_schedule(&tp->t_rstrt_ch, 1);
243 splx(s);
246 static int
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;
252 return 0;
255 static void
256 pcons_poll(void *aux)
258 struct pconssoftc *sc = aux;
259 struct tty *tp = sc->of_tty;
260 char ch;
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);
271 pconsprobe(void)
274 return (prom_stdin() && prom_stdout());
277 void
278 pcons_cnpollc(dev_t dev, int on)
280 struct pconssoftc *sc;
282 sc = device_lookup_private(&pcons_cd, minor(dev));
283 if (sc == NULL)
284 return;
286 if (on) {
287 if (sc->of_flags & OFPOLL)
288 callout_stop(&sc->sc_poll_ch);
289 sc->of_flags &= ~OFPOLL;
290 } else {
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);
300 void
301 pcons_dopoll(void)
303 pcons_poll(device_lookup_private(&pcons_cd, 0));