pcio.c revision 1.3 1 /* $NetBSD: pcio.c,v 1.3 1997/03/22 01:48:37 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1996
5 * Matthias Drochner. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project
18 * by Matthias Drochner.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 /*
36 * console I/O needs lowlevel routines from conio.S and comio.S
37 */
38
39 #include <lib/libsa/stand.h>
40
41 #include "libi386.h"
42
43 extern void conputc __P((int));
44 extern int congetc __P((void));
45 extern int coniskey __P((void));
46
47 #ifdef SUPPORT_SERIAL
48 extern int computc __P((int, int));
49 extern int comgetc __P((int));
50 extern int comstatus __P((int));
51
52 static int iodev;
53 #endif
54
55 static char *iodevname[] = {
56 "pc",
57 #ifdef SUPPORT_SERIAL
58 "com0",
59 "com1"
60 #endif
61 };
62
63 char *
64 initio(dev)
65 int dev;
66 {
67 #ifdef SUPPORT_SERIAL
68 switch (dev) {
69 case CONSDEV_AUTO:
70 /*
71 * serial console must have hardware handshake! check: 1.
72 * character output without error 2. status bits for modem
73 * ready set (status seems only useful after character
74 * output)
75 */
76 cominit(0);
77 if (!(computc(' ', 0) & 0x80) && (comstatus(0) & 0x00b0))
78 dev = CONSDEV_COM0;
79 else {
80 cominit(1);
81 if (!(computc(' ', 1) & 0x80) && (comstatus(1) & 0x00b0))
82 dev = CONSDEV_COM1;
83 else
84 dev = CONSDEV_PC;
85 }
86 break;
87 case CONSDEV_COM0:
88 cominit(0);
89 break;
90 case CONSDEV_COM1:
91 cominit(1);
92 break;
93 default:
94 dev = CONSDEV_PC;
95 break;
96 }
97 return (iodevname[iodev = dev]);
98 #else
99 return (iodevname[0]);
100 #endif
101 }
102
103 static inline void
104 internal_putchar(c)
105 int c;
106 {
107 #ifdef SUPPORT_SERIAL
108 switch (iodev) {
109 case CONSDEV_PC:
110 #endif
111 conputc(c);
112 #ifdef SUPPORT_SERIAL
113 break;
114 case CONSDEV_COM0:
115 computc(c, 0);
116 break;
117 case CONSDEV_COM1:
118 computc(c, 1);
119 break;
120 }
121 #endif
122 }
123
124 void
125 putchar(c)
126 int c;
127 {
128 if (c == '\n')
129 internal_putchar('\r');
130 internal_putchar(c);
131 }
132
133 int
134 getchar()
135 {
136 #ifdef SUPPORT_SERIAL
137 switch (iodev) {
138 case CONSDEV_PC:
139 #endif
140 return (congetc());
141 #ifdef SUPPORT_SERIAL
142 case CONSDEV_COM0:
143 return (comgetc(0));
144 case CONSDEV_COM1:
145 return (comgetc(1));
146 }
147 #endif
148 }
149
150 int
151 iskey()
152 {
153 #ifdef SUPPORT_SERIAL
154 switch (iodev) {
155 case CONSDEV_PC:
156 #endif
157 return (coniskey());
158 #ifdef SUPPORT_SERIAL
159 case CONSDEV_COM0:
160 return (!!(comstatus(0) & 0x0100));
161 case CONSDEV_COM1:
162 return (!!(comstatus(1) & 0x0100));
163 }
164 #endif
165 }
166