2 * Copyright (c) 2007 Bruce M. Simpson.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
38 #include <sys/consio.h>
41 #include <dev/cfe/cfe_api.h>
42 #include <dev/cfe/cfe_error.h>
46 #ifndef CFECONS_POLL_HZ
47 #define CFECONS_POLL_HZ 4
49 #define CFEBURSTLEN 128 /* max number of bytes to write in one chunk */
51 static tsw_open_t cfe_tty_open;
52 static tsw_close_t cfe_tty_close;
53 static tsw_outwakeup_t cfe_tty_outwakeup;
55 static struct ttydevsw cfe_ttydevsw = {
56 .tsw_flags = TF_NOPREFIX,
57 .tsw_open = cfe_tty_open,
58 .tsw_close = cfe_tty_close,
59 .tsw_outwakeup = cfe_tty_outwakeup,
62 static int conhandle = -1;
63 /* XXX does cfe have to poll? */
65 static struct callout cfe_timer;
68 static int alt_break_state;
71 static void cfe_timeout(void *);
73 static cn_probe_t cfe_cnprobe;
74 static cn_init_t cfe_cninit;
75 static cn_term_t cfe_cnterm;
76 static cn_getc_t cfe_cngetc;
77 static cn_putc_t cfe_cnputc;
78 static cn_grab_t cfe_cngrab;
79 static cn_ungrab_t cfe_cnungrab;
84 cn_drvinit(void *unused)
88 if (cfe_consdev.cn_pri != CN_DEAD &&
89 cfe_consdev.cn_name[0] != '\0') {
90 tp = tty_alloc(&cfe_ttydevsw, NULL);
91 callout_init_mtx(&cfe_timer, tty_getlock(tp), 0);
92 tty_makedev(tp, NULL, "cfecons");
97 cfe_tty_open(struct tty *tp)
99 polltime = hz / CFECONS_POLL_HZ;
102 callout_reset(&cfe_timer, polltime, cfe_timeout, tp);
108 cfe_tty_close(struct tty *tp)
111 callout_stop(&cfe_timer);
115 cfe_tty_outwakeup(struct tty *tp)
117 int len, written, rc;
118 u_char buf[CFEBURSTLEN];
121 len = ttydisc_getc(tp, buf, sizeof buf);
126 while (written < len) {
127 rc = cfe_write(conhandle, &buf[written], len - written);
141 tp = (struct tty *)v;
143 tty_lock_assert(tp, MA_OWNED);
144 while ((c = cfe_cngetc(NULL)) != -1)
145 ttydisc_rint(tp, c, 0);
146 ttydisc_rint_done(tp);
148 callout_reset(&cfe_timer, polltime, cfe_timeout, tp);
152 cfe_cnprobe(struct consdev *cp)
155 conhandle = cfe_getstdhandle(CFE_STDHANDLE_CONSOLE);
157 cp->cn_pri = CN_DEAD;
163 char *bootmsg = "Using CFE firmware console.\n";
166 for (i = 0; i < strlen(bootmsg); i++)
167 cfe_cnputc(cp, bootmsg[i]);
174 cfe_cninit(struct consdev *cp)
177 strcpy(cp->cn_name, "cfecons");
181 cfe_cnterm(struct consdev *cp)
187 cfe_cngrab(struct consdev *cp)
193 cfe_cnungrab(struct consdev *cp)
199 cfe_cngetc(struct consdev *cp)
203 if (cfe_read(conhandle, &ch, 1) == 1) {
205 kdb_alt_break(ch, &alt_break_state);
214 cfe_cnputc(struct consdev *cp, int c)
219 cfe_cnputc(cp, '\r');
222 while (cfe_write(conhandle, &cbuf, 1) == 0)
226 SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL);