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