ofw_console.c revision 225203
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 225203 2011-08-26 21:46:36Z rwatson $"); 28 29#include "opt_comconsole.h" 30#include "opt_ofw.h" 31 32#include <sys/param.h> 33#include <sys/kdb.h> 34#include <sys/kernel.h> 35#include <sys/priv.h> 36#include <sys/systm.h> 37#include <sys/types.h> 38#include <sys/conf.h> 39#include <sys/cons.h> 40#include <sys/consio.h> 41#include <sys/tty.h> 42 43#include <dev/ofw/openfirm.h> 44 45#include <ddb/ddb.h> 46 47#ifndef OFWCONS_POLL_HZ 48#define OFWCONS_POLL_HZ 4 /* 50-100 works best on Ultra2 */ 49#endif 50#define OFBURSTLEN 128 /* max number of bytes to write in one chunk */ 51 52static tsw_open_t ofwtty_open; 53static tsw_close_t ofwtty_close; 54static tsw_outwakeup_t ofwtty_outwakeup; 55 56static struct ttydevsw ofw_ttydevsw = { 57 .tsw_flags = TF_NOPREFIX, 58 .tsw_open = ofwtty_open, 59 .tsw_close = ofwtty_close, 60 .tsw_outwakeup = ofwtty_outwakeup, 61}; 62 63static int polltime; 64static struct callout_handle ofw_timeouthandle 65 = CALLOUT_HANDLE_INITIALIZER(&ofw_timeouthandle); 66 67#if defined(KDB) 68static int alt_break_state; 69#endif 70 71static void ofw_timeout(void *); 72 73static cn_probe_t ofw_cnprobe; 74static cn_init_t ofw_cninit; 75static cn_term_t ofw_cnterm; 76static cn_getc_t ofw_cngetc; 77static cn_putc_t ofw_cnputc; 78 79CONSOLE_DRIVER(ofw); 80 81static void 82cn_drvinit(void *unused) 83{ 84 phandle_t options; 85 char output[32]; 86 struct tty *tp; 87 88 if (ofw_consdev.cn_pri != CN_DEAD && 89 ofw_consdev.cn_name[0] != '\0') { 90 if ((options = OF_finddevice("/options")) == -1 || 91 OF_getprop(options, "output-device", output, 92 sizeof(output)) == -1) 93 return; 94 /* 95 * XXX: This is a hack and it may result in two /dev/ttya 96 * XXX: devices on platforms where the sab driver works. 97 */ 98 tp = tty_alloc(&ofw_ttydevsw, NULL); 99 tty_makedev(tp, NULL, "%s", output); 100 tty_makealias(tp, "ofwcons"); 101 } 102} 103 104SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL); 105 106static int stdin; 107static int stdout; 108 109static int 110ofwtty_open(struct tty *tp) 111{ 112 polltime = hz / OFWCONS_POLL_HZ; 113 if (polltime < 1) 114 polltime = 1; 115 116 ofw_timeouthandle = timeout(ofw_timeout, tp, polltime); 117 118 return (0); 119} 120 121static void 122ofwtty_close(struct tty *tp) 123{ 124 125 /* XXX Should be replaced with callout_stop(9) */ 126 untimeout(ofw_timeout, tp, ofw_timeouthandle); 127} 128 129static void 130ofwtty_outwakeup(struct tty *tp) 131{ 132 int len; 133 u_char buf[OFBURSTLEN]; 134 135 for (;;) { 136 len = ttydisc_getc(tp, buf, sizeof buf); 137 if (len == 0) 138 break; 139 OF_write(stdout, buf, len); 140 } 141} 142 143static void 144ofw_timeout(void *v) 145{ 146 struct tty *tp; 147 int c; 148 149 tp = (struct tty *)v; 150 151 tty_lock(tp); 152 while ((c = ofw_cngetc(NULL)) != -1) 153 ttydisc_rint(tp, c, 0); 154 ttydisc_rint_done(tp); 155 tty_unlock(tp); 156 157 ofw_timeouthandle = timeout(ofw_timeout, tp, polltime); 158} 159 160static void 161ofw_cnprobe(struct consdev *cp) 162{ 163 int chosen; 164 165 if ((chosen = OF_finddevice("/chosen")) == -1) { 166 cp->cn_pri = CN_DEAD; 167 return; 168 } 169 170 if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1) { 171 cp->cn_pri = CN_DEAD; 172 return; 173 } 174 175 if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1) { 176 cp->cn_pri = CN_DEAD; 177 return; 178 } 179 180 cp->cn_pri = CN_LOW; 181} 182 183static void 184ofw_cninit(struct consdev *cp) 185{ 186 187 /* XXX: This is the alias, but that should be good enough */ 188 strcpy(cp->cn_name, "ofwcons"); 189} 190 191static void 192ofw_cnterm(struct consdev *cp) 193{ 194} 195 196static int 197ofw_cngetc(struct consdev *cp) 198{ 199 unsigned char ch; 200 201 if (OF_read(stdin, &ch, 1) > 0) { 202#if defined(KDB) 203 kdb_alt_break(ch, &alt_break_state); 204#endif 205 return (ch); 206 } 207 208 return (-1); 209} 210 211static void 212ofw_cnputc(struct consdev *cp, int c) 213{ 214 char cbuf; 215 216 if (c == '\n') { 217 cbuf = '\r'; 218 OF_write(stdout, &cbuf, 1); 219 } 220 221 cbuf = c; 222 OF_write(stdout, &cbuf, 1); 223} 224