1 /* $OpenBSD: consinit.c,v 1.15 2016/03/07 13:21:51 naddy Exp $ */ 2 /* $NetBSD: consinit.c,v 1.9 2000/10/20 05:32:35 mrg Exp $ */ 3 4 /*- 5 * Copyright (c) 1999 Eduardo E. Horvath 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include "pcons.h" 33 #include "ukbd.h" 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/conf.h> 38 #include <sys/device.h> 39 #include <sys/file.h> 40 #include <sys/ioctl.h> 41 #include <sys/kernel.h> 42 #include <sys/proc.h> 43 #include <sys/tty.h> 44 #include <sys/time.h> 45 #include <sys/syslog.h> 46 47 #include <machine/autoconf.h> 48 #include <machine/openfirm.h> 49 #include <machine/conf.h> 50 #include <machine/cpu.h> 51 #include <machine/psl.h> 52 #include <machine/z8530var.h> 53 #include <machine/sparc64.h> 54 55 #include <dev/cons.h> 56 57 #include <sparc64/dev/cons.h> 58 59 #include <dev/usb/ukbdvar.h> 60 61 cons_decl(prom_); 62 63 int stdin = 0, stdout = 0; 64 65 /* 66 * The console is set to this one initially, 67 * which lets us use the PROM until consinit() 68 * is called to select a real console. 69 */ 70 struct consdev consdev_prom = { 71 prom_cnprobe, 72 prom_cninit, 73 prom_cngetc, 74 prom_cnputc, 75 prom_cnpollc, 76 NULL 77 }; 78 79 /* 80 * The console table pointer is statically initialized 81 * to point to the PROM (output only) table, so that 82 * early calls to printf will work. 83 */ 84 struct consdev *cn_tab = &consdev_prom; 85 86 void 87 prom_cnprobe(struct consdev *cd) 88 { 89 #if NPCONS > 0 90 int maj; 91 92 for (maj = 0; maj < nchrdev; maj++) 93 if (cdevsw[maj].d_open == pconsopen) 94 break; 95 cd->cn_dev = makedev(maj, 0); 96 cd->cn_pri = CN_MIDPRI; 97 #endif 98 } 99 100 int 101 prom_cngetc(dev_t dev) 102 { 103 unsigned char ch = '\0'; 104 int l; 105 #ifdef DDB 106 static int nplus = 0; 107 #endif 108 109 while ((l = OF_read(stdin, &ch, 1)) != 1) 110 /* void */; 111 #ifdef DDB 112 if (ch == '+') { 113 if (nplus++ > 3) 114 Debugger(); 115 } else 116 nplus = 0; 117 #endif 118 if (ch == '\r') 119 ch = '\n'; 120 if (ch == '\b') 121 ch = '\177'; 122 return ch; 123 } 124 125 void 126 prom_cninit(struct consdev *cn) 127 { 128 if (!stdin) stdin = OF_stdin(); 129 if (!stdout) stdout = OF_stdout(); 130 } 131 132 /* 133 * PROM console output putchar. 134 */ 135 void 136 prom_cnputc(dev_t dev, int c) 137 { 138 int s; 139 char c0 = (c & 0x7f); 140 141 #if 0 142 if (!stdout) stdout = OF_stdout(); 143 #endif 144 s = splhigh(); 145 OF_write(stdout, &c0, 1); 146 splx(s); 147 } 148 149 void 150 prom_cnpollc(dev_t dev, int on) 151 { 152 if (on) { 153 /* Entering debugger. */ 154 fb_unblank(); 155 } else { 156 /* Resuming kernel. */ 157 } 158 #if NPCONS > 0 159 pcons_cnpollc(dev, on); 160 #endif 161 } 162 163 /*****************************************************************/ 164 165 #ifdef DEBUG 166 #define DBPRINT(x) prom_printf x 167 #else 168 #define DBPRINT(x) 169 #endif 170 171 /* 172 * This function replaces sys/dev/cninit.c 173 * Determine which device is the console using 174 * the PROM "input source" and "output sink". 175 */ 176 void 177 consinit(void) 178 { 179 register int chosen; 180 char buffer[128]; 181 extern int stdinnode, fbnode; 182 char *consname = "unknown"; 183 184 DBPRINT(("consinit()\r\n")); 185 if (cn_tab != &consdev_prom) return; 186 187 DBPRINT(("setting up stdin\r\n")); 188 chosen = OF_finddevice("/chosen"); 189 OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)); 190 DBPRINT(("stdin instance = %x\r\n", stdin)); 191 192 if ((stdinnode = OF_instance_to_package(stdin)) == 0) { 193 printf("WARNING: no PROM stdin\n"); 194 } 195 #if NUKBD > 0 196 else { 197 if (OF_getprop(stdinnode, "compatible", buffer, 198 sizeof(buffer)) != -1 && strncmp("usb", buffer, 3) == 0) 199 ukbd_cnattach(); 200 } 201 #endif 202 203 DBPRINT(("setting up stdout\r\n")); 204 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)); 205 206 DBPRINT(("stdout instance = %x\r\n", stdout)); 207 208 if ((fbnode = OF_instance_to_package(stdout)) == 0) 209 printf("WARNING: no PROM stdout\n"); 210 211 DBPRINT(("stdout package = %x\r\n", fbnode)); 212 213 if (stdinnode && (OF_getproplen(stdinnode,"keyboard") >= 0)) { 214 consname = "keyboard/display"; 215 } else if (fbnode && 216 (OF_instance_to_path(stdin, buffer, sizeof(buffer)) >= 0)) { 217 consname = buffer; 218 } 219 printf("console is %s\n", consname); 220 221 /* Initialize PROM console */ 222 (*cn_tab->cn_probe)(cn_tab); 223 (*cn_tab->cn_init)(cn_tab); 224 } 225