kd.c revision 1.13 1 1.13 gwr /* $NetBSD: kd.c,v 1.13 1995/04/26 23:20:15 gwr Exp $ */
2 1.4 cgd
3 1.1 gwr /*
4 1.11 gwr * Copyright (c) 1994, 1995 Gordon W. Ross
5 1.1 gwr * All rights reserved.
6 1.1 gwr *
7 1.1 gwr * Redistribution and use in source and binary forms, with or without
8 1.1 gwr * modification, are permitted provided that the following conditions
9 1.1 gwr * are met:
10 1.1 gwr * 1. Redistributions of source code must retain the above copyright
11 1.1 gwr * notice, this list of conditions and the following disclaimer.
12 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 gwr * notice, this list of conditions and the following disclaimer in the
14 1.1 gwr * documentation and/or other materials provided with the distribution.
15 1.2 gwr * 3. The name of the author may not be used to endorse or promote products
16 1.1 gwr * derived from this software without specific prior written permission.
17 1.11 gwr * 4. All advertising materials mentioning features or use of this software
18 1.11 gwr * must display the following acknowledgement:
19 1.11 gwr * This product includes software developed by Gordon Ross
20 1.1 gwr *
21 1.2 gwr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.2 gwr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.2 gwr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.2 gwr * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.2 gwr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.2 gwr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.2 gwr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.2 gwr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.2 gwr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.2 gwr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 gwr */
32 1.1 gwr
33 1.1 gwr /*
34 1.1 gwr * Keyboard/Display device.
35 1.1 gwr *
36 1.1 gwr * This driver exists simply to provide a tty device that
37 1.1 gwr * the indirect console driver can point to.
38 1.1 gwr * The kbd driver sends its input here.
39 1.1 gwr * Output goes to the screen via PROM printf.
40 1.1 gwr */
41 1.1 gwr
42 1.1 gwr #include <sys/param.h>
43 1.1 gwr #include <sys/proc.h>
44 1.1 gwr #include <sys/systm.h>
45 1.1 gwr #include <sys/ioctl.h>
46 1.1 gwr #include <sys/tty.h>
47 1.1 gwr #include <sys/file.h>
48 1.1 gwr #include <sys/conf.h>
49 1.1 gwr #include <sys/device.h>
50 1.1 gwr
51 1.1 gwr #include <machine/autoconf.h>
52 1.1 gwr #include <machine/mon.h>
53 1.11 gwr #include <machine/psl.h>
54 1.1 gwr
55 1.1 gwr #include <dev/cons.h>
56 1.1 gwr
57 1.1 gwr #define BURST 64
58 1.1 gwr
59 1.13 gwr /*
60 1.13 gwr * There is no point in pretending there might be
61 1.13 gwr * more than one keyboard/display device.
62 1.13 gwr */
63 1.1 gwr struct tty *kd_tty[1];
64 1.1 gwr
65 1.1 gwr int kdopen(dev_t, int, int, struct proc *);
66 1.1 gwr int kdclose(dev_t, int, int, struct proc *);
67 1.1 gwr int kdread(dev_t, struct uio *, int);
68 1.1 gwr int kdwrite(dev_t, struct uio *, int);
69 1.1 gwr int kdioctl(dev_t, int, caddr_t, int, struct proc *);
70 1.1 gwr
71 1.1 gwr static int kdparam(struct tty *, struct termios *);
72 1.1 gwr static void kdstart(struct tty *);
73 1.1 gwr
74 1.10 gwr int kd_is_console;
75 1.10 gwr
76 1.7 gwr /* This is called by kbd_serial() like a pseudo-device. */
77 1.7 gwr void
78 1.7 gwr kd_attach(n)
79 1.7 gwr int n;
80 1.1 gwr {
81 1.1 gwr kd_tty[0] = ttymalloc();
82 1.1 gwr
83 1.1 gwr /* Tell keyboard module where to send read data. */
84 1.1 gwr kbd_ascii(kd_tty[0]);
85 1.13 gwr }
86 1.13 gwr
87 1.13 gwr struct tty *
88 1.13 gwr kdtty(dev)
89 1.13 gwr dev_t dev;
90 1.13 gwr {
91 1.13 gwr return kd_tty[0];
92 1.1 gwr }
93 1.1 gwr
94 1.1 gwr int
95 1.1 gwr kdopen(dev, flag, mode, p)
96 1.1 gwr dev_t dev;
97 1.1 gwr int flag, mode;
98 1.1 gwr struct proc *p;
99 1.1 gwr {
100 1.3 gwr int error, unit;
101 1.1 gwr struct tty *tp;
102 1.1 gwr
103 1.1 gwr unit = minor(dev);
104 1.1 gwr if (unit) return ENXIO;
105 1.1 gwr
106 1.1 gwr tp = kd_tty[unit];
107 1.1 gwr if (tp == NULL)
108 1.1 gwr return ENXIO;
109 1.3 gwr
110 1.8 gwr if ((error = kbd_iopen()) != 0) {
111 1.8 gwr #ifdef DIAGNOSTIC
112 1.8 gwr printf("kd: kbd_iopen, error=%d\n", error);
113 1.8 gwr #endif
114 1.3 gwr return (error);
115 1.8 gwr }
116 1.1 gwr
117 1.1 gwr tp->t_oproc = kdstart;
118 1.1 gwr tp->t_param = kdparam;
119 1.1 gwr tp->t_dev = dev;
120 1.1 gwr if ((tp->t_state & TS_ISOPEN) == 0) {
121 1.1 gwr tp->t_state |= TS_WOPEN;
122 1.1 gwr ttychars(tp);
123 1.1 gwr tp->t_iflag = TTYDEF_IFLAG;
124 1.1 gwr tp->t_oflag = TTYDEF_OFLAG;
125 1.1 gwr tp->t_cflag = TTYDEF_CFLAG;
126 1.1 gwr tp->t_lflag = TTYDEF_LFLAG;
127 1.1 gwr tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
128 1.1 gwr kdparam(tp, &tp->t_termios);
129 1.1 gwr ttsetwater(tp);
130 1.1 gwr } else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
131 1.1 gwr return EBUSY;
132 1.1 gwr tp->t_state |= TS_CARR_ON;
133 1.1 gwr
134 1.1 gwr return ((*linesw[tp->t_line].l_open)(dev, tp));
135 1.1 gwr }
136 1.1 gwr
137 1.1 gwr int
138 1.1 gwr kdclose(dev, flag, mode, p)
139 1.1 gwr dev_t dev;
140 1.1 gwr int flag, mode;
141 1.1 gwr struct proc *p;
142 1.1 gwr {
143 1.1 gwr int unit = minor(dev);
144 1.1 gwr struct tty *tp = kd_tty[unit];
145 1.1 gwr
146 1.1 gwr (*linesw[tp->t_line].l_close)(tp, flag);
147 1.1 gwr ttyclose(tp);
148 1.1 gwr return (0);
149 1.1 gwr }
150 1.1 gwr
151 1.1 gwr int
152 1.1 gwr kdread(dev, uio, flag)
153 1.1 gwr dev_t dev;
154 1.1 gwr struct uio *uio;
155 1.1 gwr int flag;
156 1.1 gwr {
157 1.1 gwr int unit = minor(dev);
158 1.1 gwr struct tty *tp = kd_tty[unit];
159 1.1 gwr
160 1.1 gwr return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
161 1.1 gwr }
162 1.1 gwr
163 1.1 gwr int
164 1.1 gwr kdwrite(dev, uio, flag)
165 1.1 gwr dev_t dev;
166 1.1 gwr struct uio *uio;
167 1.1 gwr int flag;
168 1.1 gwr {
169 1.1 gwr int unit = minor(dev);
170 1.1 gwr struct tty *tp = kd_tty[unit];
171 1.1 gwr
172 1.1 gwr return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
173 1.12 mycroft }
174 1.12 mycroft
175 1.12 mycroft int
176 1.12 mycroft kdstop(tp, flag)
177 1.12 mycroft struct tty *tp;
178 1.12 mycroft int flag;
179 1.12 mycroft {
180 1.12 mycroft
181 1.1 gwr }
182 1.1 gwr
183 1.1 gwr int
184 1.1 gwr kdioctl(dev, cmd, data, flag, p)
185 1.1 gwr dev_t dev;
186 1.1 gwr int cmd;
187 1.1 gwr caddr_t data;
188 1.1 gwr int flag;
189 1.1 gwr struct proc *p;
190 1.1 gwr {
191 1.1 gwr int error;
192 1.1 gwr int unit = minor(dev);
193 1.1 gwr struct tty *tp = kd_tty[unit];
194 1.1 gwr
195 1.1 gwr error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
196 1.1 gwr if (error >= 0)
197 1.1 gwr return error;
198 1.1 gwr error = ttioctl(tp, cmd, data, flag, p);
199 1.1 gwr if (error >= 0)
200 1.1 gwr return error;
201 1.1 gwr
202 1.1 gwr /* Handle any ioctl commands specific to kbd/display. */
203 1.1 gwr /* XXX - Send KB* ioctls to kbd module? */
204 1.1 gwr /* XXX - Send FB* ioctls to fb module? */
205 1.1 gwr
206 1.1 gwr return ENOTTY;
207 1.1 gwr }
208 1.1 gwr
209 1.11 gwr static void kd_later(void*);
210 1.11 gwr static void kd_putfb(struct tty *);
211 1.11 gwr
212 1.1 gwr void
213 1.1 gwr kdstart(tp)
214 1.1 gwr struct tty *tp;
215 1.1 gwr {
216 1.1 gwr struct clist *cl;
217 1.11 gwr register int s;
218 1.1 gwr
219 1.1 gwr s = spltty();
220 1.1 gwr if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
221 1.1 gwr goto out;
222 1.11 gwr
223 1.1 gwr cl = &tp->t_outq;
224 1.11 gwr if (cl->c_cc) {
225 1.10 gwr if (kd_is_console) {
226 1.11 gwr tp->t_state |= TS_BUSY;
227 1.11 gwr if ((s & PSL_IPL) == 0) {
228 1.11 gwr /* called at level zero - update screen now. */
229 1.11 gwr (void) splsoftclock();
230 1.11 gwr kd_putfb(tp);
231 1.11 gwr (void) spltty();
232 1.11 gwr tp->t_state &= ~TS_BUSY;
233 1.11 gwr } else {
234 1.11 gwr /* called at interrupt level - do it later */
235 1.11 gwr timeout(kd_later, (void*)tp, 0);
236 1.11 gwr }
237 1.11 gwr } else {
238 1.11 gwr /*
239 1.11 gwr * This driver uses the PROM for writing the screen,
240 1.11 gwr * and that only works if this is the console device.
241 1.11 gwr * If this is not the console, just flush the output.
242 1.11 gwr * Sorry. (In that case, use xdm instead of getty.)
243 1.11 gwr */
244 1.11 gwr ndflush(cl, cl->c_cc);
245 1.11 gwr }
246 1.11 gwr }
247 1.11 gwr if (cl->c_cc <= tp->t_lowat) {
248 1.11 gwr if (tp->t_state & TS_ASLEEP) {
249 1.11 gwr tp->t_state &= ~TS_ASLEEP;
250 1.11 gwr wakeup((caddr_t)cl);
251 1.10 gwr }
252 1.11 gwr selwakeup(&tp->t_wsel);
253 1.1 gwr }
254 1.11 gwr out:
255 1.11 gwr splx(s);
256 1.11 gwr }
257 1.11 gwr
258 1.11 gwr /*
259 1.11 gwr * Timeout function to do delayed writes to the screen.
260 1.11 gwr * Called at splsoftclock when requested by kdstart.
261 1.11 gwr */
262 1.11 gwr static void
263 1.11 gwr kd_later(tpaddr)
264 1.11 gwr void *tpaddr;
265 1.11 gwr {
266 1.11 gwr struct tty *tp = tpaddr;
267 1.11 gwr register int s;
268 1.1 gwr
269 1.11 gwr kd_putfb(tp);
270 1.11 gwr
271 1.11 gwr s = spltty();
272 1.1 gwr tp->t_state &= ~TS_BUSY;
273 1.11 gwr if (tp->t_line)
274 1.11 gwr (*linesw[tp->t_line].l_start)(tp);
275 1.11 gwr else
276 1.11 gwr kdstart(tp);
277 1.11 gwr splx(s);
278 1.11 gwr }
279 1.11 gwr
280 1.11 gwr /*
281 1.11 gwr * Put text on the screen using the PROM monitor.
282 1.11 gwr * This can take a while, so to avoid missing
283 1.11 gwr * interrupts, this is called at splsoftclock.
284 1.11 gwr */
285 1.11 gwr static void kd_putfb(tp)
286 1.11 gwr struct tty *tp;
287 1.11 gwr {
288 1.11 gwr char buf[BURST];
289 1.11 gwr struct clist *cl = &tp->t_outq;
290 1.11 gwr char *p, *end;
291 1.11 gwr int len;
292 1.11 gwr
293 1.11 gwr while ((len = q_to_b(cl, buf, BURST-1)) > 0) {
294 1.11 gwr /* PROM will barf if high bits are set. */
295 1.11 gwr p = buf;
296 1.11 gwr end = buf + len;
297 1.11 gwr while (p < end)
298 1.11 gwr *p++ &= 0x7f;
299 1.11 gwr (romVectorPtr->fbWriteStr)(buf, len);
300 1.1 gwr }
301 1.1 gwr }
302 1.1 gwr
303 1.11 gwr
304 1.1 gwr static int
305 1.1 gwr kdparam(tp, t)
306 1.1 gwr struct tty *tp;
307 1.1 gwr struct termios *t;
308 1.1 gwr {
309 1.1 gwr /* XXX - These are ignored... */
310 1.1 gwr tp->t_ispeed = t->c_ispeed;
311 1.1 gwr tp->t_ospeed = t->c_ospeed;
312 1.1 gwr tp->t_cflag = t->c_cflag;
313 1.1 gwr return 0;
314 1.1 gwr }
315 1.1 gwr
316 1.1 gwr
317 1.1 gwr /*
318 1.1 gwr * kd console support
319 1.1 gwr */
320 1.1 gwr
321 1.8 gwr extern int zscnprobe_kbd(), zscngetc(), kbd_translate();
322 1.1 gwr
323 1.1 gwr kdcnprobe(cp)
324 1.1 gwr struct consdev *cp;
325 1.1 gwr {
326 1.1 gwr int maj;
327 1.1 gwr
328 1.1 gwr /* locate the major number */
329 1.1 gwr for (maj = 0; maj < nchrdev; maj++)
330 1.6 gwr if (cdevsw[maj].d_open == (void*)kdopen)
331 1.1 gwr break;
332 1.1 gwr
333 1.1 gwr /* initialize required fields */
334 1.1 gwr cp->cn_dev = makedev(maj, 0);
335 1.1 gwr cp->cn_pri = zscnprobe_kbd();
336 1.1 gwr }
337 1.1 gwr
338 1.1 gwr kdcninit(cp)
339 1.1 gwr struct consdev *cp;
340 1.1 gwr {
341 1.8 gwr
342 1.8 gwr /* This prepares zscngetc() */
343 1.8 gwr zs_set_conschan(1, 0);
344 1.8 gwr
345 1.8 gwr /* This prepares kbd_translate() */
346 1.8 gwr kbd_init_tables();
347 1.10 gwr
348 1.10 gwr /* Indicate that it is OK to use the PROM fbwrite */
349 1.10 gwr kd_is_console = 1;
350 1.8 gwr
351 1.1 gwr mon_printf("console on kd0 (keyboard/display)\n");
352 1.1 gwr }
353 1.1 gwr
354 1.1 gwr kdcngetc(dev)
355 1.1 gwr dev_t dev;
356 1.1 gwr {
357 1.8 gwr int c;
358 1.1 gwr
359 1.8 gwr do {
360 1.8 gwr c = zscngetc(0);
361 1.8 gwr c = kbd_translate(c);
362 1.8 gwr } while (c == -1);
363 1.1 gwr
364 1.1 gwr return (c);
365 1.1 gwr }
366 1.1 gwr
367 1.1 gwr kdcnputc(dev, c)
368 1.1 gwr dev_t dev;
369 1.1 gwr int c;
370 1.1 gwr {
371 1.11 gwr (romVectorPtr->fbWriteChar)(c & 0x7f);
372 1.9 gwr }
373 1.9 gwr
374 1.9 gwr extern void fb_unblank();
375 1.9 gwr void kdcnpollc(dev, on)
376 1.9 gwr dev_t dev;
377 1.9 gwr int on;
378 1.9 gwr {
379 1.9 gwr if (on)
380 1.9 gwr fb_unblank();
381 1.1 gwr }
382