xref: /netbsd/sys/dev/arcbios/arcbios_tty.c (revision c4a72b64)
1 /*	$NetBSD: arcbios_tty.c,v 1.6 2002/10/23 09:13:06 jdolecek 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@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.6 2002/10/23 09:13:06 jdolecek 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 proc *p)
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 && p->p_ucred->cr_uid != 0) {
105 		splx(s);
106 		return (EBUSY);
107 	}
108 
109 	splx(s);
110 
111 	error = (*tp->t_linesw->l_open)(dev, tp);
112 	if (error == 0 && setuptimeout)
113 		callout_reset(&arcbios_tty_ch, 1, arcbios_tty_poll, tp);
114 
115 	return (error);
116 }
117 
118 int
119 arcbios_ttyclose(dev_t dev, int flag, int mode, struct proc *p)
120 {
121 	int unit = minor(dev);
122 	struct tty *tp = arcbios_tty[unit];
123 
124 	callout_stop(&arcbios_tty_ch);
125 	(*tp->t_linesw->l_close)(tp, flag);
126 	ttyclose(tp);
127 	return (0);
128 }
129 
130 int
131 arcbios_ttyread(dev_t dev, struct uio *uio, int flag)
132 {
133 	struct tty *tp = arcbios_tty[minor(dev)];
134 
135 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
136 }
137 
138 int
139 arcbios_ttywrite(dev_t dev, struct uio *uio, int flag)
140 {
141 	struct tty *tp = arcbios_tty[minor(dev)];
142 
143 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
144 }
145 
146 int
147 arcbios_ttypoll(dev_t dev, int events, struct proc *p)
148 {
149 	struct tty *tp = arcbios_tty[minor(dev)];
150 
151 	return ((*tp->t_linesw->l_poll)(tp, events, p));
152 }
153 
154 int
155 arcbios_ttyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
156 {
157 	int unit = minor(dev);
158 	struct tty *tp = arcbios_tty[unit];
159 	int error;
160 
161 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
162 	if (error != EPASSTHROUGH)
163 		return (error);
164 	return (ttioctl(tp, cmd, data, flag, p));
165 }
166 
167 int
168 arcbios_tty_param(struct tty *tp, struct termios *t)
169 {
170 
171 	return (0);
172 }
173 
174 void
175 arcbios_tty_start(struct tty *tp)
176 {
177 	uint32_t count;
178 	int s;
179 
180 	s = spltty();
181 	if (tp->t_state & (TS_TTSTOP | TS_BUSY))
182 		goto out;
183 	if (tp->t_outq.c_cc <= tp->t_lowat) {
184 		if (tp->t_state & TS_ASLEEP) {
185 			tp->t_state &= ~TS_ASLEEP;
186 			wakeup((caddr_t)&tp->t_outq);
187 		}
188 		selwakeup(&tp->t_wsel);
189 	}
190 	tp->t_state |= TS_BUSY;
191 	while (tp->t_outq.c_cc != 0) {
192 		(*ARCBIOS->Write)(ARCBIOS_STDOUT, tp->t_outq.c_cf,
193 		    ndqb(&tp->t_outq, 0), &count);
194 		ndflush(&tp->t_outq, count);
195 	}
196 	tp->t_state &= ~TS_BUSY;
197  out:
198 	splx(s);
199 }
200 
201 void
202 arcbios_ttystop(struct tty *tp, int flag)
203 {
204 	int s;
205 
206 	s = spltty();
207 	if (tp->t_state & TS_BUSY)
208 		if ((tp->t_state & TS_TTSTOP) == 0)
209 			tp->t_state |= TS_FLUSH;
210 	splx(s);
211 }
212 
213 static int
214 arcbios_tty_getchar(int *cp)
215 {
216 	char c;
217 	int32_t q;
218 	u_int32_t count;
219 
220 	q = ARCBIOS->GetReadStatus(ARCBIOS_STDIN);
221 
222 	if (q == 0) {
223 		ARCBIOS->Read(ARCBIOS_STDIN, &c, 1, &count);
224 		*cp = c;
225 
226 		return 1;
227 	}
228 
229 	return 0;
230 }
231 
232 void
233 arcbios_tty_poll(void *v)
234 {
235 	struct tty *tp = v;
236 	int c, l_r;
237 
238 	while (arcbios_tty_getchar(&c)) {
239 		if (tp->t_state & TS_ISOPEN)
240 			l_r = (*tp->t_linesw->l_rint)(c, tp);
241 	}
242 	callout_reset(&arcbios_tty_ch, 1, arcbios_tty_poll, tp);
243 }
244 
245 struct tty *
246 arcbios_ttytty(dev_t dev)
247 {
248 
249 	if (minor(dev) != 0)
250 		panic("arcbios_ttytty: bogus");
251 
252 	return (arcbios_tty[0]);
253 }
254