1/* $Id: console.c,v 1.1.1.1 2008/10/15 03:26:19 james26_jang Exp $
2 * console.c: Routines that deal with sending and receiving IO
3 *            to/from the current console device using the PROM.
4 *
5 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
6 * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7 */
8
9#include <linux/types.h>
10#include <linux/kernel.h>
11#include <linux/sched.h>
12#include <asm/openprom.h>
13#include <asm/oplib.h>
14#include <asm/system.h>
15#include <linux/string.h>
16
17extern int prom_stdin, prom_stdout;
18
19/* Non blocking get character from console input device, returns -1
20 * if no input was taken.  This can be used for polling.
21 */
22__inline__ int
23prom_nbgetchar(void)
24{
25	char inc;
26
27	if (p1275_cmd("read", P1275_ARG(1,P1275_ARG_OUT_BUF)|
28			      P1275_INOUT(3,1),
29			      prom_stdin, &inc, P1275_SIZE(1)) == 1)
30		return inc;
31	else
32		return -1;
33}
34
35/* Non blocking put character to console device, returns -1 if
36 * unsuccessful.
37 */
38__inline__ int
39prom_nbputchar(char c)
40{
41	char outc;
42
43	outc = c;
44	if (p1275_cmd("write", P1275_ARG(1,P1275_ARG_IN_BUF)|
45			       P1275_INOUT(3,1),
46			       prom_stdout, &outc, P1275_SIZE(1)) == 1)
47		return 0;
48	else
49		return -1;
50}
51
52/* Blocking version of get character routine above. */
53char
54prom_getchar(void)
55{
56	int character;
57	while((character = prom_nbgetchar()) == -1) ;
58	return (char) character;
59}
60
61/* Blocking version of put character routine above. */
62void
63prom_putchar(char c)
64{
65	prom_nbputchar(c);
66	return;
67}
68
69void
70prom_puts(char *s, int len)
71{
72	p1275_cmd("write", P1275_ARG(1,P1275_ARG_IN_BUF)|
73			   P1275_INOUT(3,1),
74			   prom_stdout, s, P1275_SIZE(len));
75}
76
77/* Query for input device type */
78enum prom_input_device
79prom_query_input_device()
80{
81	int st_p;
82	char propb[64];
83
84	st_p = prom_inst2pkg(prom_stdin);
85	if(prom_node_has_property(st_p, "keyboard"))
86		return PROMDEV_IKBD;
87	prom_getproperty(st_p, "device_type", propb, sizeof(propb));
88	if(strncmp(propb, "serial", 6))
89		return PROMDEV_I_UNK;
90	memset(propb, 0, sizeof(propb));
91	st_p = prom_finddevice ("/options");
92	prom_getproperty(st_p, "input-device", propb, sizeof(propb));
93
94	/*
95	 * If we get here with propb == 'keyboard', we are on ttya, as
96	 * the PROM defaulted to this due to 'no input device'.
97	 */
98	if (!strncmp(propb, "keyboard", 8))
99		return PROMDEV_ITTYA;
100
101	if (strncmp (propb, "tty", 3) || !propb[3])
102		return PROMDEV_I_UNK;
103	switch (propb[3]) {
104		case 'a': return PROMDEV_ITTYA;
105		case 'b': return PROMDEV_ITTYB;
106		default: return PROMDEV_I_UNK;
107	}
108}
109
110/* Query for output device type */
111
112enum prom_output_device
113prom_query_output_device()
114{
115	int st_p;
116	char propb[64];
117	int propl;
118
119	st_p = prom_inst2pkg(prom_stdout);
120	propl = prom_getproperty(st_p, "device_type", propb, sizeof(propb));
121	if (propl >= 0 && propl == sizeof("display") &&
122	    strncmp("display", propb, sizeof("display")) == 0)
123		return PROMDEV_OSCREEN;
124	if(strncmp("serial", propb, 6))
125		return PROMDEV_O_UNK;
126	memset(propb, 0, sizeof(propb));
127	st_p = prom_finddevice ("/options");
128	prom_getproperty(st_p, "output-device", propb, sizeof(propb));
129
130	/*
131	 * If we get here with propb == 'screen', we are on ttya, as
132	 * the PROM defaulted to this due to 'no input device'.
133	 */
134	if (!strncmp(propb, "screen", 6))
135		return PROMDEV_OTTYA;
136
137	if (strncmp (propb, "tty", 3) || !propb[3])
138		return PROMDEV_O_UNK;
139	switch (propb[3]) {
140		case 'a': return PROMDEV_OTTYA;
141		case 'b': return PROMDEV_OTTYB;
142		default: return PROMDEV_O_UNK;
143	}
144}
145