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