1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
/* $OpenBSD: unixcons.c,v 1.2 2008/01/23 16:37:57 jsing Exp $ */
/*
* Copyright (c) 1997-1999 Michael Shalayeff
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <dev/cons.h>
#include "libsa.h"
#include "unixdev.h"
struct termios tioc;
int tioc_valid = 0;
#define NCOM 3
const char *compath[NCOM] = {
"/dev/ttyS0", /* com0 */
"/dev/ttyS2", /* com1 */
"/dev/ttyS1" /* com2 */
};
int com_fd = -1; /* open serial port */
int com_speed = 9600; /* default speed is 9600 baud */
/* Local prototypes */
void common_putc(dev_t, int);
int common_getc(dev_t);
void
common_putc(dev_t dev, int c)
{
/* Always send to stdout. */
(void)uwrite(1, &c, 1);
/* Copy to serial if open. */
if (com_fd != -1)
(void)uwrite(com_fd, &c, 1);
}
int
common_getc(dev_t dev)
{
struct timeval tv;
fd_set fdset;
int fd, nfds, n;
char c;
while (1) {
tv.tv_sec = 0;
tv.tv_usec = 10000;
FD_ZERO(&fdset);
/* Always read from stdin. */
fd = 0;
nfds = 1;
FD_SET(fd, &fdset);
/* Read from serial if open. */
if (com_fd != -1) {
nfds = com_fd+1;
FD_SET(com_fd, &fdset);
}
n = uselect(nfds, &fdset, NULL, NULL, &tv);
if ((dev & 0x80) != 0)
return (n > 0);
if (n > 0)
break;
}
for (fd = 0; fd < nfds; fd++)
if (FD_ISSET(fd, &fdset))
break;
return (uread(fd, &c, 1) < 1 ? -1 : c);
}
void
cn_probe(struct consdev *cn)
{
cn->cn_pri = CN_MIDPRI;
cn->cn_dev = makedev(0,0);
printf("cn%d ", minor(cn->cn_dev));
}
void
cn_init(struct consdev *cn)
{
struct termios t;
if (!tioc_valid && tcgetattr(0, &t) == 0) {
tioc = t;
tioc_valid = 1;
cfmakeraw(&t);
(void)tcsetattr(0, TCSAFLUSH, &t);
}
}
void
cn_putc(dev_t dev, int c)
{
common_putc(dev, c);
}
int
cn_getc(dev_t dev)
{
return (common_getc(dev));
}
void
com_probe(struct consdev *cn)
{
int i;
struct linux_stat sb;
for (i = 0; i < NCOM; i++) {
if (ustat(compath[i], &sb) != 0)
continue;
printf("com%d ", i);
}
cn->cn_pri = CN_LOWPRI;
/* XXX from arm/conf.c */
cn->cn_dev = makedev(12, 0);
}
void
com_init(struct consdev *cn)
{
struct termios t;
int unit = minor(cn->cn_dev);
if (unit >= NCOM)
return;
if (com_fd != -1)
uclose(com_fd);
com_fd = uopen(compath[unit], O_RDWR);
if (com_fd == -1)
return;
if (tcgetattr(com_fd, &t) == 0) {
cfmakeraw(&t);
cfsetspeed(&t, (speed_t)com_speed);
(void)tcsetattr(com_fd, TCSAFLUSH, &t);
}
}
void
com_putc(dev_t dev, int c)
{
common_putc(dev, c);
}
int
com_getc(dev_t dev)
{
return (common_getc(dev));
}
int
cnspeed(dev_t dev, int sp)
{
if (major(dev) == 12) /* comN */
return (comspeed(dev, sp));
/* cn0 or anything else */
return (9600);
}
/* call with sp == 0 to query the current speed */
int
comspeed(dev_t dev, int sp)
{
struct termios t;
if (sp <= 0)
return (com_speed);
/* check if the new speed is a valid baud rate */
if (cfsetspeed(&t, (speed_t)sp) != 0)
sp = com_speed;
if (cn_tab && cn_tab->cn_dev == dev && com_speed != sp) {
printf("com%d: changing speed to %d baud in 5 seconds, "
"change your terminal to match!\n\a",
minor(dev), sp);
sleep(5);
if (com_fd != -1 && tcgetattr(com_fd, &t) == 0) {
(void)cfsetspeed(&t, (speed_t)sp);
(void)tcsetattr(com_fd, TCSAFLUSH, &t);
}
printf("\n");
}
if (com_speed != sp) {
printf("com%d: %d baud\n", minor(dev), sp);
com_speed = sp;
}
return (com_speed);
}
|