/
promcons.c
263 lines (218 loc) · 5.58 KB
/
promcons.c
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* $NetBSD: promcons.c,v 1.26.10.3 2006/05/13 10:26:26 elad Exp $ */
/*
* Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
* All rights reserved.
*
* Author: Chris G. Demetriou
*
* Permission to use, copy, modify and distribute this software and
* its documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
* FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie the
* rights to redistribute these changes.
*/
#include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
__KERNEL_RCSID(0, "$NetBSD: promcons.c,v 1.26.10.3 2006/05/13 10:26:26 elad Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/tty.h>
#include <sys/proc.h>
#include <sys/user.h>
#include <sys/file.h>
#include <sys/uio.h>
#include <sys/kernel.h>
#include <sys/syslog.h>
#include <sys/types.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/kauth.h>
#include <uvm/uvm_extern.h>
#include <machine/cpuconf.h>
#include <machine/prom.h>
#ifdef _PMAP_MAY_USE_PROM_CONSOLE
dev_type_open(promopen);
dev_type_close(promclose);
dev_type_read(promread);
dev_type_write(promwrite);
dev_type_ioctl(promioctl);
dev_type_stop(promstop);
dev_type_tty(promtty);
dev_type_poll(prompoll);
const struct cdevsw prom_cdevsw = {
promopen, promclose, promread, promwrite, promioctl,
promstop, promtty, prompoll, nommap, ttykqfilter, D_TTY
};
#define PROM_POLL_HZ 50
static struct tty *prom_tty[1];
static int polltime;
void promstart(struct tty *);
void promtimeout(void *);
int promparam(struct tty *, struct termios *);
struct callout prom_ch = CALLOUT_INITIALIZER;
int
promopen(dev_t dev, int flag, int mode, struct lwp *l)
{
int unit = minor(dev);
struct tty *tp;
int s;
int error = 0, setuptimeout = 0;
if (!pmap_uses_prom_console() || unit >= 1)
return ENXIO;
s = spltty();
if (!prom_tty[unit]) {
tp = prom_tty[unit] = ttymalloc();
tty_attach(tp);
} else
tp = prom_tty[unit];
tp->t_oproc = promstart;
tp->t_param = promparam;
tp->t_dev = dev;
if ((tp->t_state & TS_ISOPEN) == 0) {
tp->t_state |= TS_CARR_ON;
ttychars(tp);
tp->t_iflag = TTYDEF_IFLAG;
tp->t_oflag = TTYDEF_OFLAG;
tp->t_cflag = TTYDEF_CFLAG|CLOCAL;
tp->t_lflag = TTYDEF_LFLAG;
tp->t_ispeed = tp->t_ospeed = 9600;
ttsetwater(tp);
setuptimeout = 1;
} else if (tp->t_state&TS_XCLUDE &&
kauth_authorize_generic(l->l_proc->p_cred, KAUTH_GENERIC_ISSUSER,
&l->l_proc->p_acflag) != 0) {
splx(s);
return EBUSY;
}
splx(s);
error = (*tp->t_linesw->l_open)(dev, tp);
if (error == 0 && setuptimeout) {
polltime = hz / PROM_POLL_HZ;
if (polltime < 1)
polltime = 1;
callout_reset(&prom_ch, polltime, promtimeout, tp);
}
return error;
}
int
promclose(dev_t dev, int flag, int mode, struct lwp *l)
{
int unit = minor(dev);
struct tty *tp = prom_tty[unit];
callout_stop(&prom_ch);
(*tp->t_linesw->l_close)(tp, flag);
ttyclose(tp);
return 0;
}
int
promread(dev_t dev, struct uio *uio, int flag)
{
struct tty *tp = prom_tty[minor(dev)];
return ((*tp->t_linesw->l_read)(tp, uio, flag));
}
int
promwrite(dev_t dev, struct uio *uio, int flag)
{
struct tty *tp = prom_tty[minor(dev)];
return ((*tp->t_linesw->l_write)(tp, uio, flag));
}
int
prompoll(dev_t dev, int events, struct lwp *l)
{
struct tty *tp = prom_tty[minor(dev)];
return ((*tp->t_linesw->l_poll)(tp, events, l));
}
int
promioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
{
int unit = minor(dev);
struct tty *tp = prom_tty[unit];
int error;
error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
if (error != EPASSTHROUGH)
return error;
return ttioctl(tp, cmd, data, flag, l);
}
int
promparam(struct tty *tp, struct termios *t)
{
return 0;
}
void
promstart(struct tty *tp)
{
int s;
s = spltty();
if (tp->t_state & (TS_TTSTOP | TS_BUSY))
goto out;
if (tp->t_outq.c_cc <= tp->t_lowat) {
if (tp->t_state & TS_ASLEEP) {
tp->t_state &= ~TS_ASLEEP;
wakeup((caddr_t)&tp->t_outq);
}
selwakeup(&tp->t_wsel);
}
tp->t_state |= TS_BUSY;
while (tp->t_outq.c_cc != 0)
promcnputc(tp->t_dev, getc(&tp->t_outq));
tp->t_state &= ~TS_BUSY;
out:
splx(s);
}
/*
* Stop output on a line.
*/
void
promstop(struct tty *tp, int flag)
{
int s;
s = spltty();
if (tp->t_state & TS_BUSY)
if ((tp->t_state & TS_TTSTOP) == 0)
tp->t_state |= TS_FLUSH;
splx(s);
}
void
promtimeout(void *v)
{
struct tty *tp = v;
u_char c;
while (promcnlookc(tp->t_dev, &c)) {
if (tp->t_state & TS_ISOPEN)
(*tp->t_linesw->l_rint)(c, tp);
}
callout_reset(&prom_ch, polltime, promtimeout, tp);
}
struct tty *
promtty(dev_t dev)
{
if (minor(dev) != 0)
panic("promtty: bogus");
return prom_tty[0];
}
#else /* _PMAP_MAY_USE_PROM_CONSOLE */
/*
* If not defined _PMAP_MAY_USE_PROM_CONSOLE,
* this fake prom_cdevsw is attached to the kernel.
* NEVER REMOVE!
*/
const struct cdevsw prom_cdevsw = {
noopen, noclose, noread, nowrite, noioctl,
nostop, notty, nopoll, nommap,
};
#endif /* _PMAP_MAY_USE_PROM_CONSOLE */