arcbios_tty.c revision 1.9.2.3 1 /* $NetBSD: arcbios_tty.c,v 1.9.2.3 2007/09/03 14:33:25 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.3 2007/09/03 14:33:25 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 callout_t arcbios_tty_ch;
51 bool arcbios_ch_init;
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 (!arcbios_ch_init) {
81 arcbios_ch_init = true;
82 callout_init(&arcbios_tty_ch, 0);
83 }
84
85 if (unit != 0)
86 return (ENODEV);
87
88 s = spltty();
89
90 if (arcbios_tty[unit] == NULL) {
91 tp = arcbios_tty[unit] = ttymalloc();
92 tty_attach(tp);
93 } else
94 tp = arcbios_tty[unit];
95
96 tp->t_oproc = arcbios_tty_start;
97 tp->t_param = arcbios_tty_param;
98 tp->t_dev = dev;
99
100 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) {
101 splx(s);
102 return (EBUSY);
103 }
104
105 if ((tp->t_state & TS_ISOPEN) == 0) {
106 tp->t_state |= TS_CARR_ON;
107 ttychars(tp);
108 tp->t_iflag = TTYDEF_IFLAG;
109 tp->t_oflag = TTYDEF_OFLAG;
110 tp->t_cflag = TTYDEF_CFLAG | CLOCAL;
111 tp->t_lflag = TTYDEF_LFLAG;
112 tp->t_ispeed = tp->t_ospeed = 9600;
113 ttsetwater(tp);
114
115 setuptimeout = 1;
116 }
117
118 splx(s);
119
120 error = (*tp->t_linesw->l_open)(dev, tp);
121 if (error == 0 && setuptimeout)
122 callout_reset(&arcbios_tty_ch, 1, arcbios_tty_poll, tp);
123
124 return (error);
125 }
126
127 int
128 arcbios_ttyclose(dev_t dev, int flag, int mode, struct lwp *l)
129 {
130 int unit = minor(dev);
131 struct tty *tp = arcbios_tty[unit];
132
133 callout_stop(&arcbios_tty_ch);
134 (*tp->t_linesw->l_close)(tp, flag);
135 ttyclose(tp);
136 return (0);
137 }
138
139 int
140 arcbios_ttyread(dev_t dev, struct uio *uio, int flag)
141 {
142 struct tty *tp = arcbios_tty[minor(dev)];
143
144 return ((*tp->t_linesw->l_read)(tp, uio, flag));
145 }
146
147 int
148 arcbios_ttywrite(dev_t dev, struct uio *uio, int flag)
149 {
150 struct tty *tp = arcbios_tty[minor(dev)];
151
152 return ((*tp->t_linesw->l_write)(tp, uio, flag));
153 }
154
155 int
156 arcbios_ttypoll(dev_t dev, int events, struct lwp *l)
157 {
158 struct tty *tp = arcbios_tty[minor(dev)];
159
160 return ((*tp->t_linesw->l_poll)(tp, events, l));
161 }
162
163 int
164 arcbios_ttyioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
165 {
166 int unit = minor(dev);
167 struct tty *tp = arcbios_tty[unit];
168 int error;
169
170 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
171 if (error != EPASSTHROUGH)
172 return (error);
173 return (ttioctl(tp, cmd, data, flag, l));
174 }
175
176 int
177 arcbios_tty_param(struct tty *tp, struct termios *t)
178 {
179
180 return (0);
181 }
182
183 void
184 arcbios_tty_start(struct tty *tp)
185 {
186 u_long count;
187 int s;
188
189 s = spltty();
190 if (tp->t_state & (TS_TTSTOP | TS_BUSY))
191 goto out;
192 if (tp->t_outq.c_cc <= tp->t_lowat) {
193 if (tp->t_state & TS_ASLEEP) {
194 tp->t_state &= ~TS_ASLEEP;
195 wakeup((void *)&tp->t_outq);
196 }
197 selwakeup(&tp->t_wsel);
198 }
199 tp->t_state |= TS_BUSY;
200 while (tp->t_outq.c_cc != 0) {
201 (*ARCBIOS->Write)(ARCBIOS_STDOUT, tp->t_outq.c_cf,
202 ndqb(&tp->t_outq, 0), &count);
203 ndflush(&tp->t_outq, count);
204 }
205 tp->t_state &= ~TS_BUSY;
206 out:
207 splx(s);
208 }
209
210 void
211 arcbios_ttystop(struct tty *tp, int flag)
212 {
213 int s;
214
215 s = spltty();
216 if (tp->t_state & TS_BUSY)
217 if ((tp->t_state & TS_TTSTOP) == 0)
218 tp->t_state |= TS_FLUSH;
219 splx(s);
220 }
221
222 static int
223 arcbios_tty_getchar(int *cp)
224 {
225 char c;
226 int32_t q;
227 u_long count;
228
229 q = ARCBIOS->GetReadStatus(ARCBIOS_STDIN);
230
231 if (q == 0) {
232 ARCBIOS->Read(ARCBIOS_STDIN, &c, 1, &count);
233 *cp = c;
234
235 return 1;
236 }
237
238 return 0;
239 }
240
241 void
242 arcbios_tty_poll(void *v)
243 {
244 struct tty *tp = v;
245 int c, l_r;
246
247 while (arcbios_tty_getchar(&c)) {
248 if (tp->t_state & TS_ISOPEN)
249 l_r = (*tp->t_linesw->l_rint)(c, tp);
250 }
251 callout_reset(&arcbios_tty_ch, 1, arcbios_tty_poll, tp);
252 }
253
254 struct tty *
255 arcbios_ttytty(dev_t dev)
256 {
257
258 if (minor(dev) != 0)
259 panic("arcbios_ttytty: bogus");
260
261 return (arcbios_tty[0]);
262 }
263