unixcons.c revision 1.1.4.2 1 1.1.4.2 yamt /* $NetBSD: unixcons.c,v 1.1.4.2 2009/05/04 08:12:16 yamt Exp $ */
2 1.1.4.2 yamt
3 1.1.4.2 yamt /*
4 1.1.4.2 yamt * Copyright (c) 2009 NONAKA Kimihiro <nonaka (at) netbsd.org>
5 1.1.4.2 yamt * All rights reserved.
6 1.1.4.2 yamt *
7 1.1.4.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.1.4.2 yamt * modification, are permitted provided that the following conditions
9 1.1.4.2 yamt * are met:
10 1.1.4.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.1.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer in the
14 1.1.4.2 yamt * documentation and/or other materials provided with the distribution.
15 1.1.4.2 yamt *
16 1.1.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1.4.2 yamt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1.4.2 yamt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1.4.2 yamt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1.4.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1.4.2 yamt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1.4.2 yamt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1.4.2 yamt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1.4.2 yamt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1.4.2 yamt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1.4.2 yamt */
27 1.1.4.2 yamt
28 1.1.4.2 yamt #include "boot.h"
29 1.1.4.2 yamt #include "bootinfo.h"
30 1.1.4.2 yamt #include "unixdev.h"
31 1.1.4.2 yamt
32 1.1.4.2 yamt #include "compat_linux.h"
33 1.1.4.2 yamt #include "termios.h"
34 1.1.4.2 yamt
35 1.1.4.2 yamt struct btinfo_console bi_cons;
36 1.1.4.2 yamt
37 1.1.4.2 yamt static int iodev = CONSDEV_GLASS;
38 1.1.4.2 yamt static int infd = 0;
39 1.1.4.2 yamt static int outfd = 1;
40 1.1.4.2 yamt
41 1.1.4.2 yamt static const char *comdevname[] = {
42 1.1.4.2 yamt "/dev/ttyS0",
43 1.1.4.2 yamt };
44 1.1.4.2 yamt
45 1.1.4.2 yamt static void common_putc(int fd, int c);
46 1.1.4.2 yamt static int common_getc(int fd, int timo);
47 1.1.4.2 yamt
48 1.1.4.2 yamt void
49 1.1.4.2 yamt consinit(int dev, int speed)
50 1.1.4.2 yamt {
51 1.1.4.2 yamt struct linux_termios termios;
52 1.1.4.2 yamt int fd;
53 1.1.4.2 yamt
54 1.1.4.2 yamt switch (dev) {
55 1.1.4.2 yamt case CONSDEV_COM0:
56 1.1.4.2 yamt iodev = dev;
57 1.1.4.2 yamt break;
58 1.1.4.2 yamt
59 1.1.4.2 yamt case CONSDEV_GLASS:
60 1.1.4.2 yamt default:
61 1.1.4.2 yamt glass_console:
62 1.1.4.2 yamt iodev = CONSDEV_GLASS;
63 1.1.4.2 yamt break;
64 1.1.4.2 yamt }
65 1.1.4.2 yamt
66 1.1.4.2 yamt if (infd >= 0 && infd == outfd) {
67 1.1.4.2 yamt uclose(infd);
68 1.1.4.2 yamt infd = 0;
69 1.1.4.2 yamt outfd = 1;
70 1.1.4.2 yamt }
71 1.1.4.2 yamt
72 1.1.4.2 yamt if (iodev == CONSDEV_GLASS) {
73 1.1.4.2 yamt infd = 0;
74 1.1.4.2 yamt outfd = 1;
75 1.1.4.2 yamt
76 1.1.4.2 yamt strlcpy(bi_cons.devname, "glass", sizeof(bi_cons.devname));
77 1.1.4.2 yamt bi_cons.addr = -1;
78 1.1.4.2 yamt bi_cons.speed = -1;
79 1.1.4.2 yamt } else {
80 1.1.4.2 yamt fd = uopen(comdevname[iodev - CONSDEV_COM0], LINUX_O_RDWR);
81 1.1.4.2 yamt if (fd < 0)
82 1.1.4.2 yamt goto glass_console;
83 1.1.4.2 yamt infd = outfd = fd;
84 1.1.4.2 yamt
85 1.1.4.2 yamt /* set speed */
86 1.1.4.2 yamt linux_tcgetattr(fd, &termios);
87 1.1.4.2 yamt if (linux_cfsetspeed(&termios, speed) < 0) {
88 1.1.4.2 yamt speed = 9600;
89 1.1.4.2 yamt if (linux_cfsetspeed(&termios, speed) < 0)
90 1.1.4.2 yamt goto glass_console;
91 1.1.4.2 yamt }
92 1.1.4.2 yamt if (linux_tcsetattr(fd, LINUX_TCSETS, &termios) < 0)
93 1.1.4.2 yamt goto glass_console;
94 1.1.4.2 yamt
95 1.1.4.2 yamt snprintf(bi_cons.devname, sizeof(bi_cons.devname), "com%d",
96 1.1.4.2 yamt iodev - CONSDEV_COM0);
97 1.1.4.2 yamt bi_cons.addr = -1;
98 1.1.4.2 yamt bi_cons.speed = speed;
99 1.1.4.2 yamt }
100 1.1.4.2 yamt BI_ADD(&bi_cons, BTINFO_CONSDEV, sizeof(bi_cons));
101 1.1.4.2 yamt }
102 1.1.4.2 yamt
103 1.1.4.2 yamt void
104 1.1.4.2 yamt putchar(int c)
105 1.1.4.2 yamt {
106 1.1.4.2 yamt
107 1.1.4.2 yamt common_putc(outfd, c);
108 1.1.4.2 yamt }
109 1.1.4.2 yamt
110 1.1.4.2 yamt int
111 1.1.4.2 yamt getchar(void)
112 1.1.4.2 yamt {
113 1.1.4.2 yamt
114 1.1.4.2 yamt return common_getc(infd, 1);
115 1.1.4.2 yamt }
116 1.1.4.2 yamt
117 1.1.4.2 yamt static void
118 1.1.4.2 yamt common_putc(int fd, int c)
119 1.1.4.2 yamt {
120 1.1.4.2 yamt
121 1.1.4.2 yamt (void)uwrite(fd, &c, 1);
122 1.1.4.2 yamt }
123 1.1.4.2 yamt
124 1.1.4.2 yamt static int
125 1.1.4.2 yamt common_getc(int fd, int timo)
126 1.1.4.2 yamt {
127 1.1.4.2 yamt struct linux_timeval tv;
128 1.1.4.2 yamt fd_set fdset;
129 1.1.4.2 yamt int nfds, n;
130 1.1.4.2 yamt char c;
131 1.1.4.2 yamt
132 1.1.4.2 yamt for (; timo < 0 || timo > 0; --timo) {
133 1.1.4.2 yamt tv.tv_sec = 1;
134 1.1.4.2 yamt tv.tv_usec = 0;
135 1.1.4.2 yamt FD_ZERO(&fdset);
136 1.1.4.2 yamt
137 1.1.4.2 yamt nfds = 1;
138 1.1.4.2 yamt FD_SET(fd, &fdset);
139 1.1.4.2 yamt
140 1.1.4.2 yamt n = uselect(nfds, &fdset, NULL, NULL, &tv);
141 1.1.4.2 yamt if (n > 0)
142 1.1.4.2 yamt break;
143 1.1.4.2 yamt }
144 1.1.4.2 yamt
145 1.1.4.2 yamt if (timo > 0) {
146 1.1.4.2 yamt for (fd = 0; fd < nfds; fd++) {
147 1.1.4.2 yamt if (FD_ISSET(fd, &fdset)) {
148 1.1.4.2 yamt return (uread(fd, &c, 1) < 1 ? -1 : c);
149 1.1.4.2 yamt }
150 1.1.4.2 yamt }
151 1.1.4.2 yamt }
152 1.1.4.2 yamt return -1;
153 1.1.4.2 yamt }
154 1.1.4.2 yamt
155 1.1.4.2 yamt int
156 1.1.4.2 yamt awaitkey(int timeout, int tell)
157 1.1.4.2 yamt {
158 1.1.4.2 yamt struct linux_termios orig_termios, raw_termios;
159 1.1.4.2 yamt int c = 0;
160 1.1.4.2 yamt int i;
161 1.1.4.2 yamt
162 1.1.4.2 yamt /* set raw mode */
163 1.1.4.2 yamt linux_tcgetattr(infd, &orig_termios);
164 1.1.4.2 yamt raw_termios = orig_termios;
165 1.1.4.2 yamt linux_cfmakeraw(&raw_termios);
166 1.1.4.2 yamt linux_tcsetattr(infd, LINUX_TCSETS, &raw_termios);
167 1.1.4.2 yamt
168 1.1.4.2 yamt for (i = timeout; i > 0; i--) {
169 1.1.4.2 yamt if (tell) {
170 1.1.4.2 yamt char numbuf[20];
171 1.1.4.2 yamt int len, j;
172 1.1.4.2 yamt
173 1.1.4.2 yamt sprintf(numbuf, "%d ", i);
174 1.1.4.2 yamt len = strlen(numbuf);
175 1.1.4.2 yamt for (j = 0; j < len; j++)
176 1.1.4.2 yamt numbuf[len + j] = '\b';
177 1.1.4.2 yamt numbuf[len + j] = '\0';
178 1.1.4.2 yamt printf(numbuf);
179 1.1.4.2 yamt }
180 1.1.4.2 yamt c = common_getc(infd, 1);
181 1.1.4.2 yamt if (c == 0)
182 1.1.4.2 yamt c = -1;
183 1.1.4.2 yamt if (c >= 0)
184 1.1.4.2 yamt break;
185 1.1.4.2 yamt }
186 1.1.4.2 yamt if (i == 0)
187 1.1.4.2 yamt c = '\0';
188 1.1.4.2 yamt
189 1.1.4.2 yamt /* set original mode */
190 1.1.4.2 yamt linux_tcsetattr(infd, LINUX_TCSETS, &orig_termios);
191 1.1.4.2 yamt
192 1.1.4.2 yamt if (tell)
193 1.1.4.2 yamt printf("0 \n");
194 1.1.4.2 yamt
195 1.1.4.2 yamt return c;
196 1.1.4.2 yamt }
197 1.1.4.2 yamt
198 1.1.4.2 yamt void dummycall2(void);
199 1.1.4.2 yamt void
200 1.1.4.2 yamt dummycall2(void)
201 1.1.4.2 yamt {
202 1.1.4.2 yamt
203 1.1.4.2 yamt (void)linux_termio_to_bsd_termios;
204 1.1.4.2 yamt (void)bsd_termios_to_linux_termio;
205 1.1.4.2 yamt (void)linux_termios_to_bsd_termios;
206 1.1.4.2 yamt (void)bsd_termios_to_linux_termios;
207 1.1.4.2 yamt }
208