1 /*- 2 * Copyright (C) 2008 by Nathan Whitehorn. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 17 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 18 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 19 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 20 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 21 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25 #include <sys/cdefs.h> 26 __FBSDID("$FreeBSD$"); 27 28 #include "opt_comconsole.h" 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/ofw/openfirm.h> 42 43 #include <ddb/ddb.h> 44 45 #include "mambocall.h" 46 47 #define MAMBOBURSTLEN 128 /* max number of bytes to write in one chunk */ 48 49 #define MAMBO_CONSOLE_WRITE 0 50 #define MAMBO_CONSOLE_READ 60 51 52 static tsw_outwakeup_t mambotty_outwakeup; 53 54 static struct ttydevsw mambo_ttydevsw = { 55 .tsw_flags = TF_NOPREFIX, 56 .tsw_outwakeup = mambotty_outwakeup, 57 }; 58 59 static int polltime; 60 static struct callout mambo_callout; 61 static struct tty *tp = NULL; 62 63 #if defined(KDB) && defined(ALT_BREAK_TO_DEBUGGER) 64 static int alt_break_state; 65 #endif 66 67 static void mambo_timeout(void *); 68 69 static cn_probe_t mambo_cnprobe; 70 static cn_init_t mambo_cninit; 71 static cn_term_t mambo_cnterm; 72 static cn_getc_t mambo_cngetc; 73 static cn_putc_t mambo_cnputc; 74 75 CONSOLE_DRIVER(mambo); 76 77 static void 78 cn_drvinit(void *unused) 79 { 80 81 if (mambo_consdev.cn_pri != CN_DEAD && 82 mambo_consdev.cn_name[0] != '\0') { 83 if (OF_finddevice("/mambo") == -1) 84 return; 85 86 tp = tty_alloc(&mambo_ttydevsw, NULL); 87 tty_init_console(tp, 0); 88 tty_makedev(tp, NULL, "%s", "mambocons"); 89 tty_makealias(tp, "mambocons"); 90 91 polltime = 1; 92 93 callout_init(&mambo_callout, CALLOUT_MPSAFE); 94 callout_reset(&mambo_callout, polltime, mambo_timeout, NULL); 95 } 96 } 97 98 SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL); 99 100 static void 101 mambotty_outwakeup(struct tty *tp) 102 { 103 int len; 104 u_char buf[MAMBOBURSTLEN]; 105 106 for (;;) { 107 len = ttydisc_getc(tp, buf, sizeof buf); 108 if (len == 0) 109 break; 110 mambocall(MAMBO_CONSOLE_WRITE, buf, (register_t)len, 1UL); 111 } 112 } 113 114 static void 115 mambo_timeout(void *v) 116 { 117 int c; 118 119 tty_lock(tp); 120 while ((c = mambo_cngetc(NULL)) != -1) 121 ttydisc_rint(tp, c, 0); 122 ttydisc_rint_done(tp); 123 tty_unlock(tp); 124 125 callout_reset(&mambo_callout, polltime, mambo_timeout, NULL); 126 } 127 128 static void 129 mambo_cnprobe(struct consdev *cp) 130 { 131 if (OF_finddevice("/mambo") == -1) { 132 cp->cn_pri = CN_DEAD; 133 return; 134 } 135 136 cp->cn_pri = CN_NORMAL; 137 } 138 139 static void 140 mambo_cninit(struct consdev *cp) 141 { 142 143 /* XXX: This is the alias, but that should be good enough */ 144 strcpy(cp->cn_name, "mambocons"); 145 } 146 147 static void 148 mambo_cnterm(struct consdev *cp) 149 { 150 } 151 152 static int 153 mambo_cngetc(struct consdev *cp) 154 { 155 int ch; 156 157 ch = mambocall(MAMBO_CONSOLE_READ); 158 159 if (ch > 0 && ch < 0xff) { 160 #if defined(KDB) && defined(ALT_BREAK_TO_DEBUGGER) 161 int kdb_brk; 162 163 if ((kdb_brk = kdb_alt_break(ch, &alt_break_state)) != 0) { 164 switch (kdb_brk) { 165 case KDB_REQ_DEBUGGER: 166 kdb_enter(KDB_WHY_BREAK, 167 "Break sequence on console"); 168 break; 169 case KDB_REQ_PANIC: 170 kdb_panic("Panic sequence on console"); 171 break; 172 case KDB_REQ_REBOOT: 173 kdb_reboot(); 174 break; 175 176 } 177 } 178 #endif 179 return (ch); 180 } 181 182 return (-1); 183 } 184 185 static void 186 mambo_cnputc(struct consdev *cp, int c) 187 { 188 char cbuf; 189 190 cbuf = c; 191 mambocall(MAMBO_CONSOLE_WRITE, &cbuf, 1UL, 1UL); 192 } 193