1 /*- 2 * Copyright (c) 2007 Bruce M. Simpson. 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 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 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/kdb.h> 32 #include <sys/kernel.h> 33 #include <sys/priv.h> 34 #include <sys/systm.h> 35 #include <sys/types.h> 36 #include <sys/conf.h> 37 #include <sys/cons.h> 38 #include <sys/consio.h> 39 #include <sys/tty.h> 40 41 #include <dev/cfe/cfe_api.h> 42 #include <dev/cfe/cfe_error.h> 43 44 #include <ddb/ddb.h> 45 46 #ifndef CFECONS_POLL_HZ 47 #define CFECONS_POLL_HZ 4 48 #endif 49 #define CFEBURSTLEN 128 /* max number of bytes to write in one chunk */ 50 51 static tsw_open_t cfe_tty_open; 52 static tsw_close_t cfe_tty_close; 53 static tsw_outwakeup_t cfe_tty_outwakeup; 54 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, 60 }; 61 62 static int conhandle = -1; 63 /* XXX does cfe have to poll? */ 64 static int polltime; 65 static struct callout_handle cfe_timeouthandle 66 = CALLOUT_HANDLE_INITIALIZER(&cfe_timeouthandle); 67 68 #if defined(KDB) 69 static int alt_break_state; 70 #endif 71 72 static void cfe_timeout(void *); 73 74 static cn_probe_t cfe_cnprobe; 75 static cn_init_t cfe_cninit; 76 static cn_term_t cfe_cnterm; 77 static cn_getc_t cfe_cngetc; 78 static cn_putc_t cfe_cnputc; 79 80 CONSOLE_DRIVER(cfe); 81 82 static void 83 cn_drvinit(void *unused) 84 { 85 struct tty *tp; 86 87 if (cfe_consdev.cn_pri != CN_DEAD && 88 cfe_consdev.cn_name[0] != '\0') { 89 tp = tty_alloc(&cfe_ttydevsw, NULL); 90 tty_makedev(tp, NULL, "cfecons"); 91 } 92 } 93 94 static int 95 cfe_tty_open(struct tty *tp) 96 { 97 polltime = hz / CFECONS_POLL_HZ; 98 if (polltime < 1) 99 polltime = 1; 100 cfe_timeouthandle = timeout(cfe_timeout, tp, polltime); 101 102 return (0); 103 } 104 105 static void 106 cfe_tty_close(struct tty *tp) 107 { 108 109 /* XXX Should be replaced with callout_stop(9) */ 110 untimeout(cfe_timeout, tp, cfe_timeouthandle); 111 } 112 113 static void 114 cfe_tty_outwakeup(struct tty *tp) 115 { 116 int len, written, rc; 117 u_char buf[CFEBURSTLEN]; 118 119 for (;;) { 120 len = ttydisc_getc(tp, buf, sizeof buf); 121 if (len == 0) 122 break; 123 124 written = 0; 125 while (written < len) { 126 rc = cfe_write(conhandle, &buf[written], len - written); 127 if (rc < 0) 128 break; 129 written += rc; 130 } 131 } 132 } 133 134 static void 135 cfe_timeout(void *v) 136 { 137 struct tty *tp; 138 int c; 139 140 tp = (struct tty *)v; 141 142 tty_lock(tp); 143 while ((c = cfe_cngetc(NULL)) != -1) 144 ttydisc_rint(tp, c, 0); 145 ttydisc_rint_done(tp); 146 tty_unlock(tp); 147 148 cfe_timeouthandle = timeout(cfe_timeout, tp, polltime); 149 } 150 151 static void 152 cfe_cnprobe(struct consdev *cp) 153 { 154 155 conhandle = cfe_getstdhandle(CFE_STDHANDLE_CONSOLE); 156 if (conhandle < 0) { 157 cp->cn_pri = CN_DEAD; 158 return; 159 } 160 161 /* XXX */ 162 if (bootverbose) { 163 char *bootmsg = "Using CFE firmware console.\n"; 164 int i; 165 166 for (i = 0; i < strlen(bootmsg); i++) 167 cfe_cnputc(cp, bootmsg[i]); 168 } 169 170 cp->cn_pri = CN_LOW; 171 } 172 173 static void 174 cfe_cninit(struct consdev *cp) 175 { 176 177 strcpy(cp->cn_name, "cfecons"); 178 } 179 180 static void 181 cfe_cnterm(struct consdev *cp) 182 { 183 184 } 185 186 static int 187 cfe_cngetc(struct consdev *cp) 188 { 189 unsigned char ch; 190 191 if (cfe_read(conhandle, &ch, 1) == 1) { 192 #if defined(KDB) 193 kdb_alt_break(ch, &alt_break_state); 194 #endif 195 return (ch); 196 } 197 198 return (-1); 199 } 200 201 static void 202 cfe_cnputc(struct consdev *cp, int c) 203 { 204 char cbuf; 205 206 if (c == '\n') 207 cfe_cnputc(cp, '\r'); 208 209 cbuf = c; 210 while (cfe_write(conhandle, &cbuf, 1) == 0) 211 continue; 212 } 213 214 SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL); 215