ofw_console.c revision 183181
1/* $NetBSD: prom.c,v 1.3 1997/09/06 14:03:58 drochner Exp $ */ 2 3/*- 4 * Mach Operating System 5 * Copyright (c) 1992 Carnegie Mellon University 6 * All Rights Reserved. 7 * 8 * Permission to use, copy, modify and distribute this software and its 9 * documentation is hereby granted, provided that both the copyright 10 * notice and this permission notice appear in all copies of the 11 * software, derivative works or modified versions, and any portions 12 * thereof, and that both notices appear in supporting documentation. 13 * 14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 17 * 18 * Carnegie Mellon requests users of this software to return to 19 * 20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 21 * School of Computer Science 22 * Carnegie Mellon University 23 * Pittsburgh PA 15213-3890 24 * 25 * any improvements or extensions that they make and grant Carnegie Mellon 26 * the rights to redistribute these changes. 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: head/sys/boot/ofw/libofw/ofw_console.c 183181 2008-09-19 11:00:14Z sobomax $"); 31 32#include <sys/types.h> 33 34#include "bootstrap.h" 35#include "openfirm.h" 36 37static void ofw_cons_probe(struct console *cp); 38static int ofw_cons_init(int); 39void ofw_cons_putchar(int); 40int ofw_cons_getchar(void); 41int ofw_cons_poll(void); 42 43static ihandle_t stdin; 44static ihandle_t stdout; 45#ifdef POWERMAC_SCREEN_HACK 46static ihandle_t stdout1; 47static int do_stdout1 = 0; 48#endif 49 50struct console ofwconsole = { 51 "ofw", 52 "Open Firmware console", 53 0, 54 ofw_cons_probe, 55 ofw_cons_init, 56 ofw_cons_putchar, 57 ofw_cons_getchar, 58 ofw_cons_poll, 59}; 60 61static void 62ofw_cons_probe(struct console *cp) 63{ 64#ifdef POWERMAC_SCREEN_HACK 65 char path1[128], path2[128]; 66#endif 67 68 OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)); 69 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)); 70#ifdef POWERMAC_SCREEN_HACK 71 stdout1 = OF_open("screen"); 72 if (stdout1 != -1) { 73 if (OF_instance_to_path(stdout, path1, sizeof(path1)) == -1) 74 path1[0] = '\0'; 75 if (OF_instance_to_path(stdout1, path2, sizeof(path2)) == -1) 76 path2[0] = '\0'; 77 if (strcmp(path1, path2) == 0) { 78 OF_close(stdout1); 79 } else { 80 do_stdout1 = 1; 81 } 82 } 83#endif 84 cp->c_flags |= C_PRESENTIN|C_PRESENTOUT; 85} 86 87static int 88ofw_cons_init(int arg) 89{ 90 return 0; 91} 92 93void 94ofw_cons_putchar(int c) 95{ 96 char cbuf; 97 98 if (c == '\n') { 99 cbuf = '\r'; 100 OF_write(stdout, &cbuf, 1); 101#ifdef POWERMAC_SCREEN_HACK 102 if (do_stdout1 != 0) 103 OF_write(stdout1, &cbuf, 1); 104#endif 105 } 106 107 cbuf = c; 108 OF_write(stdout, &cbuf, 1); 109#ifdef POWERMAC_SCREEN_HACK 110 if (do_stdout1 != 0) 111 OF_write(stdout1, &cbuf, 1); 112#endif 113} 114 115static int saved_char = -1; 116 117int 118ofw_cons_getchar() 119{ 120 unsigned char ch = '\0'; 121 int l; 122 123 if (saved_char != -1) { 124 l = saved_char; 125 saved_char = -1; 126 return l; 127 } 128 129 if (OF_read(stdin, &ch, 1) > 0) 130 return (ch); 131 132 return (-1); 133} 134 135int 136ofw_cons_poll() 137{ 138 unsigned char ch; 139 140 if (saved_char != -1) 141 return 1; 142 143 if (OF_read(stdin, &ch, 1) > 0) { 144 saved_char = ch; 145 return 1; 146 } 147 148 return 0; 149} 150