/* $NetBSD: prom.c,v 1.3 1997/09/06 14:03:58 drochner Exp $ */12/*-3* Mach Operating System4* Copyright (c) 1992 Carnegie Mellon University5* All Rights Reserved.6*7* Permission to use, copy, modify and distribute this software and its8* documentation is hereby granted, provided that both the copyright9* notice and this permission notice appear in all copies of the10* software, derivative works or modified versions, and any portions11* thereof, and that both notices appear in supporting documentation.12*13* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"14* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR15* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.16*17* Carnegie Mellon requests users of this software to return to18*19* Software Distribution Coordinator or [email protected]20* School of Computer Science21* Carnegie Mellon University22* Pittsburgh PA 15213-389023*24* any improvements or extensions that they make and grant Carnegie Mellon25* the rights to redistribute these changes.26*/2728#include <sys/types.h>2930#include "bootstrap.h"31#include "openfirm.h"3233static void ofw_cons_probe(struct console *cp);34static int ofw_cons_init(int);35void ofw_cons_putchar(int);36int ofw_cons_getchar(void);37int ofw_cons_poll(void);3839static ihandle_t stdin;40static ihandle_t stdout;4142struct console ofwconsole = {43.c_name = "ofw",44.c_desc = "Open Firmware console",45.c_probe = ofw_cons_probe,46.c_init = ofw_cons_init,47.c_out = ofw_cons_putchar,48.c_in = ofw_cons_getchar,49.c_ready = ofw_cons_poll,50};5152static void53ofw_cons_probe(struct console *cp)54{5556OF_getprop(chosen, "stdin", &stdin, sizeof(stdin));57OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));58cp->c_flags |= C_PRESENTIN|C_PRESENTOUT;59}6061static int62ofw_cons_init(int arg)63{64return (0);65}6667void68ofw_cons_putchar(int c)69{70char cbuf;7172if (c == '\n') {73cbuf = '\r';74OF_write(stdout, &cbuf, 1);75}7677cbuf = c;78OF_write(stdout, &cbuf, 1);79}8081static int saved_char = -1;8283int84ofw_cons_getchar(void)85{86unsigned char ch = '\0';87int l;8889if (saved_char != -1) {90l = saved_char;91saved_char = -1;92return (l);93}9495/* At least since version 4.0.0, QEMU became bug-compatible96* with PowerVM's vty, by inserting a \0 after every \r.97* As this confuses loader's interpreter and as a \0 coming98* from the console doesn't seem reasonable, it's filtered here. */99if (OF_read(stdin, &ch, 1) > 0 && ch != '\0')100return (ch);101102return (-1);103}104105int106ofw_cons_poll(void)107{108unsigned char ch;109110if (saved_char != -1)111return (1);112113if (OF_read(stdin, &ch, 1) > 0) {114saved_char = ch;115return (1);116}117118return (0);119}120121122