ofw_console.c revision 120544
1/*
2 * Copyright (C) 2001 Benno Rice.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD: head/sys/dev/ofw/ofw_console.c 120544 2003-09-28 06:51:47Z jake $");
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/dev/ofw/ofw_console.c 120544 2003-09-28 06:51:47Z jake $");
31
32#include "opt_ddb.h"
33#include "opt_comconsole.h"
34
35#include <sys/param.h>
36#include <sys/kernel.h>
37#include <sys/systm.h>
38#include <sys/types.h>
39#include <sys/conf.h>
40#include <sys/cons.h>
41#include <sys/consio.h>
42#include <sys/tty.h>
43
44#include <dev/ofw/openfirm.h>
45
46#include <ddb/ddb.h>
47
48#define	OFW_POLL_HZ	4
49
50static d_open_t		ofw_dev_open;
51static d_close_t	ofw_dev_close;
52static d_ioctl_t	ofw_dev_ioctl;
53
54#define	CDEV_MAJOR	97
55
56static struct cdevsw ofw_cdevsw = {
57	.d_open =	ofw_dev_open,
58	.d_close =	ofw_dev_close,
59	.d_read =	ttyread,
60	.d_write =	ttywrite,
61	.d_ioctl =	ofw_dev_ioctl,
62	.d_poll =	ttypoll,
63	.d_name =	"ofw",
64	.d_maj =	CDEV_MAJOR,
65};
66
67static struct tty		*ofw_tp = NULL;
68static int			polltime;
69static struct callout_handle	ofw_timeouthandle
70    = CALLOUT_HANDLE_INITIALIZER(&ofw_timeouthandle);
71
72#if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER)
73static int			alt_break_state;
74#endif
75
76static void	ofw_tty_start(struct tty *);
77static int	ofw_tty_param(struct tty *, struct termios *);
78static void	ofw_tty_stop(struct tty *, int);
79static void	ofw_timeout(void *);
80
81static cn_probe_t	ofw_cons_probe;
82static cn_init_t	ofw_cons_init;
83static cn_getc_t	ofw_cons_getc;
84static cn_checkc_t 	ofw_cons_checkc;
85static cn_putc_t	ofw_cons_putc;
86
87CONS_DRIVER(ofw, ofw_cons_probe, ofw_cons_init, NULL, ofw_cons_getc,
88    ofw_cons_checkc, ofw_cons_putc, NULL);
89
90static void
91cn_drvinit(void *unused)
92{
93	phandle_t options;
94	char output[32];
95	dev_t dev;
96
97	if (ofw_consdev.cn_pri != CN_DEAD &&
98	    ofw_consdev.cn_name[0] != '\0') {
99		if ((options = OF_finddevice("/options")) == -1 ||
100		    OF_getprop(options, "output-device", output,
101		    sizeof(output)) == -1)
102			return;
103		dev = make_dev(&ofw_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "%s",
104		    output);
105		make_dev_alias(dev, "ofwcons");
106	}
107}
108
109SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE + CDEV_MAJOR, cn_drvinit, NULL)
110
111static int	stdin;
112static int	stdout;
113
114static int
115ofw_dev_open(dev_t dev, int flag, int mode, struct thread *td)
116{
117	struct	tty *tp;
118	int	unit;
119	int	error, setuptimeout;
120
121	error = 0;
122	setuptimeout = 0;
123	unit = minor(dev);
124
125	tp = ofw_tp = dev->si_tty = ttymalloc(ofw_tp);
126
127	tp->t_oproc = ofw_tty_start;
128	tp->t_param = ofw_tty_param;
129	tp->t_stop = ofw_tty_stop;
130	tp->t_dev = dev;
131
132	if ((tp->t_state & TS_ISOPEN) == 0) {
133		tp->t_state |= TS_CARR_ON;
134		ttychars(tp);
135		tp->t_iflag = TTYDEF_IFLAG;
136		tp->t_oflag = TTYDEF_OFLAG;
137		tp->t_cflag = TTYDEF_CFLAG|CLOCAL;
138		tp->t_lflag = TTYDEF_LFLAG;
139		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
140		ttsetwater(tp);
141
142		setuptimeout = 1;
143	} else if ((tp->t_state & TS_XCLUDE) && suser(td)) {
144		return (EBUSY);
145	}
146
147	error = (*linesw[tp->t_line].l_open)(dev, tp);
148
149	if (error == 0 && setuptimeout) {
150		polltime = hz / OFW_POLL_HZ;
151		if (polltime < 1) {
152			polltime = 1;
153		}
154
155		ofw_timeouthandle = timeout(ofw_timeout, tp, polltime);
156	}
157
158	return (error);
159}
160
161static int
162ofw_dev_close(dev_t dev, int flag, int mode, struct thread *td)
163{
164	int	unit;
165	struct	tty *tp;
166
167	unit = minor(dev);
168	tp = ofw_tp;
169
170	if (unit != 0) {
171		return (ENXIO);
172	}
173
174	(*linesw[tp->t_line].l_close)(tp, flag);
175	ttyclose(tp);
176
177	return (0);
178}
179
180static int
181ofw_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
182{
183	int	unit;
184	struct	tty *tp;
185	int	error;
186
187	unit = minor(dev);
188	tp = ofw_tp;
189
190	if (unit != 0) {
191		return (ENXIO);
192	}
193
194	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
195	if (error != ENOIOCTL) {
196		return (error);
197	}
198
199	error = ttioctl(tp, cmd, data, flag);
200	if (error != ENOIOCTL) {
201		return (error);
202	}
203
204	return (ENOTTY);
205}
206
207static int
208ofw_tty_param(struct tty *tp, struct termios *t)
209{
210
211	return (0);
212}
213
214static void
215ofw_tty_start(struct tty *tp)
216{
217
218	if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
219		ttwwakeup(tp);
220		return;
221	}
222
223	tp->t_state |= TS_BUSY;
224	while (tp->t_outq.c_cc != 0) {
225		ofw_cons_putc(NULL, getc(&tp->t_outq));
226	}
227	tp->t_state &= ~TS_BUSY;
228
229	ttwwakeup(tp);
230}
231
232static void
233ofw_tty_stop(struct tty *tp, int flag)
234{
235
236	if (tp->t_state & TS_BUSY) {
237		if ((tp->t_state & TS_TTSTOP) == 0) {
238			tp->t_state |= TS_FLUSH;
239		}
240	}
241}
242
243static void
244ofw_timeout(void *v)
245{
246	struct	tty *tp;
247	int 	c;
248
249	tp = (struct tty *)v;
250
251	while ((c = ofw_cons_checkc(NULL)) != -1) {
252		if (tp->t_state & TS_ISOPEN) {
253			(*linesw[tp->t_line].l_rint)(c, tp);
254		}
255	}
256
257	ofw_timeouthandle = timeout(ofw_timeout, tp, polltime);
258}
259
260static void
261ofw_cons_probe(struct consdev *cp)
262{
263	int chosen;
264
265	if ((chosen = OF_finddevice("/chosen")) == -1) {
266		cp->cn_pri = CN_DEAD;
267		return;
268	}
269
270	if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1) {
271		cp->cn_pri = CN_DEAD;
272		return;
273	}
274
275	if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1) {
276		cp->cn_pri = CN_DEAD;
277		return;
278	}
279
280	cp->cn_pri = CN_LOW;
281}
282
283static void
284ofw_cons_init(struct consdev *cp)
285{
286
287	/* XXX: This is the alias, but that should be good enough */
288	sprintf(cp->cn_name, "ofwcons");
289	cp->cn_tp = ofw_tp;
290}
291
292static int
293ofw_cons_getc(struct consdev *cp)
294{
295	unsigned char ch;
296	int l;
297
298	ch = '\0';
299
300	while ((l = OF_read(stdin, &ch, 1)) != 1) {
301		if (l != -2 && l != 0) {
302			return (-1);
303		}
304	}
305
306#if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER)
307	if (db_alt_break(ch, &alt_break_state))
308		breakpoint();
309#endif
310
311	return (ch);
312}
313
314static int
315ofw_cons_checkc(struct consdev *cp)
316{
317	unsigned char ch;
318
319	if (OF_read(stdin, &ch, 1) > 0) {
320#if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER)
321		if (db_alt_break(ch, &alt_break_state))
322			breakpoint();
323#endif
324		return (ch);
325	}
326
327	return (-1);
328}
329
330static void
331ofw_cons_putc(struct consdev *cp, int c)
332{
333	char cbuf;
334
335	if (c == '\n') {
336		cbuf = '\r';
337		OF_write(stdout, &cbuf, 1);
338	}
339
340	cbuf = c;
341	OF_write(stdout, &cbuf, 1);
342}
343