arcbios_tty.c revision 1.11.10.2 1 /* $NetBSD: arcbios_tty.c,v 1.11.10.2 2006/03/10 14:39:01 elad 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.11.10.2 2006/03/10 14:39:01 elad 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
44 #include <dev/cons.h>
45
46 #include <dev/arcbios/arcbios.h>
47 #include <dev/arcbios/arcbiosvar.h>
48
49 struct callout arcbios_tty_ch = CALLOUT_INITIALIZER;
50
51 static struct tty *arcbios_tty[1];
52
53 void arcbios_tty_start(struct tty *);
54 void arcbios_tty_poll(void *);
55 int arcbios_tty_param(struct tty *, struct termios *);
56
57 dev_type_open(arcbios_ttyopen);
58 dev_type_close(arcbios_ttyclose);
59 dev_type_read(arcbios_ttyread);
60 dev_type_write(arcbios_ttywrite);
61 dev_type_ioctl(arcbios_ttyioctl);
62 dev_type_stop(arcbios_ttystop);
63 dev_type_tty(arcbios_ttytty);
64 dev_type_poll(arcbios_ttypoll);
65
66 const struct cdevsw arcbios_cdevsw = {
67 arcbios_ttyopen, arcbios_ttyclose, arcbios_ttyread, arcbios_ttywrite,
68 arcbios_ttyioctl, arcbios_ttystop, arcbios_ttytty, arcbios_ttypoll,
69 nommap, ttykqfilter, D_TTY,
70 };
71
72 int
73 arcbios_ttyopen(dev_t dev, int flag, int mode, struct lwp *l)
74 {
75 int unit = minor(dev);
76 struct tty *tp;
77 int s, error = 0, setuptimeout = 0;
78
79 if (unit != 0)
80 return (ENODEV);
81
82 s = spltty();
83
84 if (arcbios_tty[unit] == NULL) {
85 tp = arcbios_tty[unit] = ttymalloc();
86 tty_attach(tp);
87 } else
88 tp = arcbios_tty[unit];
89
90 tp->t_oproc = arcbios_tty_start;
91 tp->t_param = arcbios_tty_param;
92 tp->t_dev = dev;
93 if ((tp->t_state & TS_ISOPEN) == 0) {
94 tp->t_state |= TS_CARR_ON;
95 ttychars(tp);
96 tp->t_iflag = TTYDEF_IFLAG;
97 tp->t_oflag = TTYDEF_OFLAG;
98 tp->t_cflag = TTYDEF_CFLAG | CLOCAL;
99 tp->t_lflag = TTYDEF_LFLAG;
100 tp->t_ispeed = tp->t_ospeed = 9600;
101 ttsetwater(tp);
102
103 setuptimeout = 1;
104 } else if (tp->t_state & TS_XCLUDE &&
105 kauth_authorize_generic(l->l_proc->p_cred, KAUTH_GENERIC_ISSUSER,
106 &l->l_proc->p_acflag) != 0) {
107 splx(s);
108 return (EBUSY);
109 }
110
111 splx(s);
112
113 error = (*tp->t_linesw->l_open)(dev, tp);
114 if (error == 0 && setuptimeout)
115 callout_reset(&arcbios_tty_ch, 1, arcbios_tty_poll, tp);
116
117 return (error);
118 }
119
120 int
121 arcbios_ttyclose(dev_t dev, int flag, int mode, struct lwp *l)
122 {
123 int unit = minor(dev);
124 struct tty *tp = arcbios_tty[unit];
125
126 callout_stop(&arcbios_tty_ch);
127 (*tp->t_linesw->l_close)(tp, flag);
128 ttyclose(tp);
129 return (0);
130 }
131
132 int
133 arcbios_ttyread(dev_t dev, struct uio *uio, int flag)
134 {
135 struct tty *tp = arcbios_tty[minor(dev)];
136
137 return ((*tp->t_linesw->l_read)(tp, uio, flag));
138 }
139
140 int
141 arcbios_ttywrite(dev_t dev, struct uio *uio, int flag)
142 {
143 struct tty *tp = arcbios_tty[minor(dev)];
144
145 return ((*tp->t_linesw->l_write)(tp, uio, flag));
146 }
147
148 int
149 arcbios_ttypoll(dev_t dev, int events, struct lwp *l)
150 {
151 struct tty *tp = arcbios_tty[minor(dev)];
152
153 return ((*tp->t_linesw->l_poll)(tp, events, l));
154 }
155
156 int
157 arcbios_ttyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
158 {
159 int unit = minor(dev);
160 struct tty *tp = arcbios_tty[unit];
161 int error;
162
163 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
164 if (error != EPASSTHROUGH)
165 return (error);
166 return (ttioctl(tp, cmd, data, flag, l));
167 }
168
169 int
170 arcbios_tty_param(struct tty *tp, struct termios *t)
171 {
172
173 return (0);
174 }
175
176 void
177 arcbios_tty_start(struct tty *tp)
178 {
179 u_long count;
180 int s;
181
182 s = spltty();
183 if (tp->t_state & (TS_TTSTOP | TS_BUSY))
184 goto out;
185 if (tp->t_outq.c_cc <= tp->t_lowat) {
186 if (tp->t_state & TS_ASLEEP) {
187 tp->t_state &= ~TS_ASLEEP;
188 wakeup((caddr_t)&tp->t_outq);
189 }
190 selwakeup(&tp->t_wsel);
191 }
192 tp->t_state |= TS_BUSY;
193 while (tp->t_outq.c_cc != 0) {
194 (*ARCBIOS->Write)(ARCBIOS_STDOUT, tp->t_outq.c_cf,
195 ndqb(&tp->t_outq, 0), &count);
196 ndflush(&tp->t_outq, count);
197 }
198 tp->t_state &= ~TS_BUSY;
199 out:
200 splx(s);
201 }
202
203 void
204 arcbios_ttystop(struct tty *tp, int flag)
205 {
206 int s;
207
208 s = spltty();
209 if (tp->t_state & TS_BUSY)
210 if ((tp->t_state & TS_TTSTOP) == 0)
211 tp->t_state |= TS_FLUSH;
212 splx(s);
213 }
214
215 static int
216 arcbios_tty_getchar(int *cp)
217 {
218 char c;
219 int32_t q;
220 u_long count;
221
222 q = ARCBIOS->GetReadStatus(ARCBIOS_STDIN);
223
224 if (q == 0) {
225 ARCBIOS->Read(ARCBIOS_STDIN, &c, 1, &count);
226 *cp = c;
227
228 return 1;
229 }
230
231 return 0;
232 }
233
234 void
235 arcbios_tty_poll(void *v)
236 {
237 struct tty *tp = v;
238 int c, l_r;
239
240 while (arcbios_tty_getchar(&c)) {
241 if (tp->t_state & TS_ISOPEN)
242 l_r = (*tp->t_linesw->l_rint)(c, tp);
243 }
244 callout_reset(&arcbios_tty_ch, 1, arcbios_tty_poll, tp);
245 }
246
247 struct tty *
248 arcbios_ttytty(dev_t dev)
249 {
250
251 if (minor(dev) != 0)
252 panic("arcbios_ttytty: bogus");
253
254 return (arcbios_tty[0]);
255 }
256