promcons.c revision 1.31
1/* $NetBSD: promcons.c,v 1.31 2007/03/04 05:59:10 christos Exp $ */
2
3/*
4 * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
5 * All rights reserved.
6 *
7 * Author: Chris G. Demetriou
8 *
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
14 *
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 *
19 * Carnegie Mellon requests users of this software to return to
20 *
21 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
22 *  School of Computer Science
23 *  Carnegie Mellon University
24 *  Pittsburgh PA 15213-3890
25 *
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
28 */
29
30#include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
31
32__KERNEL_RCSID(0, "$NetBSD: promcons.c,v 1.31 2007/03/04 05:59:10 christos Exp $");
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/ioctl.h>
37#include <sys/select.h>
38#include <sys/tty.h>
39#include <sys/proc.h>
40#include <sys/user.h>
41#include <sys/file.h>
42#include <sys/uio.h>
43#include <sys/kernel.h>
44#include <sys/syslog.h>
45#include <sys/types.h>
46#include <sys/device.h>
47#include <sys/conf.h>
48#include <sys/kauth.h>
49
50#include <uvm/uvm_extern.h>
51
52#include <machine/cpuconf.h>
53#include <machine/prom.h>
54
55#ifdef _PMAP_MAY_USE_PROM_CONSOLE
56
57dev_type_open(promopen);
58dev_type_close(promclose);
59dev_type_read(promread);
60dev_type_write(promwrite);
61dev_type_ioctl(promioctl);
62dev_type_stop(promstop);
63dev_type_tty(promtty);
64dev_type_poll(prompoll);
65
66const struct cdevsw prom_cdevsw = {
67	promopen, promclose, promread, promwrite, promioctl,
68	promstop, promtty, prompoll, nommap, ttykqfilter, D_TTY
69};
70
71#define	PROM_POLL_HZ	50
72
73static struct  tty *prom_tty[1];
74static int polltime;
75
76void	promstart(struct tty *);
77void	promtimeout(void *);
78int	promparam(struct tty *, struct termios *);
79
80struct callout prom_ch = CALLOUT_INITIALIZER;
81
82int
83promopen(dev_t dev, int flag, int mode, struct lwp *l)
84{
85	int unit = minor(dev);
86	struct tty *tp;
87	int s;
88	int error = 0, setuptimeout = 0;
89
90	if (!pmap_uses_prom_console() || unit >= 1)
91		return ENXIO;
92
93	s = spltty();
94
95	if (!prom_tty[unit]) {
96		tp = prom_tty[unit] = ttymalloc();
97		tty_attach(tp);
98	} else
99		tp = prom_tty[unit];
100
101	tp->t_oproc = promstart;
102	tp->t_param = promparam;
103	tp->t_dev = dev;
104
105	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) {
106		splx(s);
107		return (EBUSY);
108	}
109
110	if ((tp->t_state & TS_ISOPEN) == 0) {
111		tp->t_state |= TS_CARR_ON;
112		ttychars(tp);
113		tp->t_iflag = TTYDEF_IFLAG;
114		tp->t_oflag = TTYDEF_OFLAG;
115		tp->t_cflag = TTYDEF_CFLAG|CLOCAL;
116		tp->t_lflag = TTYDEF_LFLAG;
117		tp->t_ispeed = tp->t_ospeed = 9600;
118		ttsetwater(tp);
119
120		setuptimeout = 1;
121	}
122
123	splx(s);
124
125	error = (*tp->t_linesw->l_open)(dev, tp);
126	if (error == 0 && setuptimeout) {
127		polltime = hz / PROM_POLL_HZ;
128		if (polltime < 1)
129			polltime = 1;
130		callout_reset(&prom_ch, polltime, promtimeout, tp);
131	}
132	return error;
133}
134
135int
136promclose(dev_t dev, int flag, int mode, struct lwp *l)
137{
138	int unit = minor(dev);
139	struct tty *tp = prom_tty[unit];
140
141	callout_stop(&prom_ch);
142	(*tp->t_linesw->l_close)(tp, flag);
143	ttyclose(tp);
144	return 0;
145}
146
147int
148promread(dev_t dev, struct uio *uio, int flag)
149{
150	struct tty *tp = prom_tty[minor(dev)];
151
152	return ((*tp->t_linesw->l_read)(tp, uio, flag));
153}
154
155int
156promwrite(dev_t dev, struct uio *uio, int flag)
157{
158	struct tty *tp = prom_tty[minor(dev)];
159
160	return ((*tp->t_linesw->l_write)(tp, uio, flag));
161}
162
163int
164prompoll(dev_t dev, int events, struct lwp *l)
165{
166	struct tty *tp = prom_tty[minor(dev)];
167
168	return ((*tp->t_linesw->l_poll)(tp, events, l));
169}
170
171int
172promioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
173{
174	int unit = minor(dev);
175	struct tty *tp = prom_tty[unit];
176	int error;
177
178	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
179	if (error != EPASSTHROUGH)
180		return error;
181	return ttioctl(tp, cmd, data, flag, l);
182}
183
184int
185promparam(struct tty *tp, struct termios *t)
186{
187
188	return 0;
189}
190
191void
192promstart(struct tty *tp)
193{
194	int s;
195
196	s = spltty();
197	if (tp->t_state & (TS_TTSTOP | TS_BUSY))
198		goto out;
199	if (tp->t_outq.c_cc <= tp->t_lowat) {
200		if (tp->t_state & TS_ASLEEP) {
201			tp->t_state &= ~TS_ASLEEP;
202			wakeup((void *)&tp->t_outq);
203		}
204		selwakeup(&tp->t_wsel);
205	}
206	tp->t_state |= TS_BUSY;
207	while (tp->t_outq.c_cc != 0)
208		promcnputc(tp->t_dev, getc(&tp->t_outq));
209	tp->t_state &= ~TS_BUSY;
210out:
211	splx(s);
212}
213
214/*
215 * Stop output on a line.
216 */
217void
218promstop(struct tty *tp, int flag)
219{
220	int s;
221
222	s = spltty();
223	if (tp->t_state & TS_BUSY)
224		if ((tp->t_state & TS_TTSTOP) == 0)
225			tp->t_state |= TS_FLUSH;
226	splx(s);
227}
228
229void
230promtimeout(void *v)
231{
232	struct tty *tp = v;
233	u_char c;
234
235	while (promcnlookc(tp->t_dev, &c)) {
236		if (tp->t_state & TS_ISOPEN)
237			(*tp->t_linesw->l_rint)(c, tp);
238	}
239	callout_reset(&prom_ch, polltime, promtimeout, tp);
240}
241
242struct tty *
243promtty(dev_t dev)
244{
245
246	if (minor(dev) != 0)
247		panic("promtty: bogus");
248
249	return prom_tty[0];
250}
251
252#else /* _PMAP_MAY_USE_PROM_CONSOLE */
253
254/*
255 * If not defined _PMAP_MAY_USE_PROM_CONSOLE,
256 * this fake prom_cdevsw is attached to the kernel.
257 * NEVER REMOVE!
258 */
259const struct cdevsw prom_cdevsw = {
260	noopen, noclose, noread, nowrite, noioctl,
261	nostop, notty, nopoll, nommap,
262};
263
264#endif /* _PMAP_MAY_USE_PROM_CONSOLE */
265