arcbios_tty.c revision 1.9.2.2 1 /* $NetBSD: arcbios_tty.c,v 1.9.2.2 2006/12/30 20:47:54 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
5 * All rights reserved.
6 *
7 * Author: Chris G. Demetriou
8 *
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
14 *
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 *
19 * Carnegie Mellon requests users of this software to return to
20 *
21 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
25 *
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: arcbios_tty.c,v 1.9.2.2 2006/12/30 20:47:54 yamt Exp $");
32
33 #include <sys/param.h>
34 #include <sys/user.h>
35 #include <sys/uio.h>
36 #include <sys/systm.h>
37 #include <sys/callout.h>
38 #include <sys/kernel.h>
39 #include <sys/conf.h>
40 #include <sys/proc.h>
41 #include <sys/tty.h>
42 #include <sys/termios.h>
43 #include <sys/kauth.h>
44
45 #include <dev/cons.h>
46
47 #include <dev/arcbios/arcbios.h>
48 #include <dev/arcbios/arcbiosvar.h>
49
50 struct callout arcbios_tty_ch = CALLOUT_INITIALIZER;
51
52 static struct tty *arcbios_tty[1];
53
54 void arcbios_tty_start(struct tty *);
55 void arcbios_tty_poll(void *);
56 int arcbios_tty_param(struct tty *, struct termios *);
57
58 dev_type_open(arcbios_ttyopen);
59 dev_type_close(arcbios_ttyclose);
60 dev_type_read(arcbios_ttyread);
61 dev_type_write(arcbios_ttywrite);
62 dev_type_ioctl(arcbios_ttyioctl);
63 dev_type_stop(arcbios_ttystop);
64 dev_type_tty(arcbios_ttytty);
65 dev_type_poll(arcbios_ttypoll);
66
67 const struct cdevsw arcbios_cdevsw = {
68 arcbios_ttyopen, arcbios_ttyclose, arcbios_ttyread, arcbios_ttywrite,
69 arcbios_ttyioctl, arcbios_ttystop, arcbios_ttytty, arcbios_ttypoll,
70 nommap, ttykqfilter, D_TTY,
71 };
72
73 int
74 arcbios_ttyopen(dev_t dev, int flag, int mode, struct lwp *l)
75 {
76 int unit = minor(dev);
77 struct tty *tp;
78 int s, error = 0, setuptimeout = 0;
79
80 if (unit != 0)
81 return (ENODEV);
82
83 s = spltty();
84
85 if (arcbios_tty[unit] == NULL) {
86 tp = arcbios_tty[unit] = ttymalloc();
87 tty_attach(tp);
88 } else
89 tp = arcbios_tty[unit];
90
91 tp->t_oproc = arcbios_tty_start;
92 tp->t_param = arcbios_tty_param;
93 tp->t_dev = dev;
94
95 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) {
96 splx(s);
97 return (EBUSY);
98 }
99
100 if ((tp->t_state & TS_ISOPEN) == 0) {
101 tp->t_state |= TS_CARR_ON;
102 ttychars(tp);
103 tp->t_iflag = TTYDEF_IFLAG;
104 tp->t_oflag = TTYDEF_OFLAG;
105 tp->t_cflag = TTYDEF_CFLAG | CLOCAL;
106 tp->t_lflag = TTYDEF_LFLAG;
107 tp->t_ispeed = tp->t_ospeed = 9600;
108 ttsetwater(tp);
109
110 setuptimeout = 1;
111 }
112
113 splx(s);
114
115 error = (*tp->t_linesw->l_open)(dev, tp);
116 if (error == 0 && setuptimeout)
117 callout_reset(&arcbios_tty_ch, 1, arcbios_tty_poll, tp);
118
119 return (error);
120 }
121
122 int
123 arcbios_ttyclose(dev_t dev, int flag, int mode, struct lwp *l)
124 {
125 int unit = minor(dev);
126 struct tty *tp = arcbios_tty[unit];
127
128 callout_stop(&arcbios_tty_ch);
129 (*tp->t_linesw->l_close)(tp, flag);
130 ttyclose(tp);
131 return (0);
132 }
133
134 int
135 arcbios_ttyread(dev_t dev, struct uio *uio, int flag)
136 {
137 struct tty *tp = arcbios_tty[minor(dev)];
138
139 return ((*tp->t_linesw->l_read)(tp, uio, flag));
140 }
141
142 int
143 arcbios_ttywrite(dev_t dev, struct uio *uio, int flag)
144 {
145 struct tty *tp = arcbios_tty[minor(dev)];
146
147 return ((*tp->t_linesw->l_write)(tp, uio, flag));
148 }
149
150 int
151 arcbios_ttypoll(dev_t dev, int events, struct lwp *l)
152 {
153 struct tty *tp = arcbios_tty[minor(dev)];
154
155 return ((*tp->t_linesw->l_poll)(tp, events, l));
156 }
157
158 int
159 arcbios_ttyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
160 {
161 int unit = minor(dev);
162 struct tty *tp = arcbios_tty[unit];
163 int error;
164
165 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
166 if (error != EPASSTHROUGH)
167 return (error);
168 return (ttioctl(tp, cmd, data, flag, l));
169 }
170
171 int
172 arcbios_tty_param(struct tty *tp, struct termios *t)
173 {
174
175 return (0);
176 }
177
178 void
179 arcbios_tty_start(struct tty *tp)
180 {
181 u_long count;
182 int s;
183
184 s = spltty();
185 if (tp->t_state & (TS_TTSTOP | TS_BUSY))
186 goto out;
187 if (tp->t_outq.c_cc <= tp->t_lowat) {
188 if (tp->t_state & TS_ASLEEP) {
189 tp->t_state &= ~TS_ASLEEP;
190 wakeup((caddr_t)&tp->t_outq);
191 }
192 selwakeup(&tp->t_wsel);
193 }
194 tp->t_state |= TS_BUSY;
195 while (tp->t_outq.c_cc != 0) {
196 (*ARCBIOS->Write)(ARCBIOS_STDOUT, tp->t_outq.c_cf,
197 ndqb(&tp->t_outq, 0), &count);
198 ndflush(&tp->t_outq, count);
199 }
200 tp->t_state &= ~TS_BUSY;
201 out:
202 splx(s);
203 }
204
205 void
206 arcbios_ttystop(struct tty *tp, int flag)
207 {
208 int s;
209
210 s = spltty();
211 if (tp->t_state & TS_BUSY)
212 if ((tp->t_state & TS_TTSTOP) == 0)
213 tp->t_state |= TS_FLUSH;
214 splx(s);
215 }
216
217 static int
218 arcbios_tty_getchar(int *cp)
219 {
220 char c;
221 int32_t q;
222 u_long count;
223
224 q = ARCBIOS->GetReadStatus(ARCBIOS_STDIN);
225
226 if (q == 0) {
227 ARCBIOS->Read(ARCBIOS_STDIN, &c, 1, &count);
228 *cp = c;
229
230 return 1;
231 }
232
233 return 0;
234 }
235
236 void
237 arcbios_tty_poll(void *v)
238 {
239 struct tty *tp = v;
240 int c, l_r;
241
242 while (arcbios_tty_getchar(&c)) {
243 if (tp->t_state & TS_ISOPEN)
244 l_r = (*tp->t_linesw->l_rint)(c, tp);
245 }
246 callout_reset(&arcbios_tty_ch, 1, arcbios_tty_poll, tp);
247 }
248
249 struct tty *
250 arcbios_ttytty(dev_t dev)
251 {
252
253 if (minor(dev) != 0)
254 panic("arcbios_ttytty: bogus");
255
256 return (arcbios_tty[0]);
257 }
258