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