xref: /netbsd-src/sys/arch/sun3/dev/kd.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: kd.c,v 1.33 2000/05/21 14:36:16 eeh Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Gordon W. Ross.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Keyboard/Display device.
41  *
42  * This driver exists simply to provide a tty device that
43  * the indirect console driver can point to.
44  * The kbd driver sends its input here.
45  * Output goes to the screen via PROM printf.
46  */
47 
48 #include <sys/param.h>
49 #include <sys/proc.h>
50 #include <sys/systm.h>
51 #include <sys/ioctl.h>
52 #include <sys/tty.h>
53 #include <sys/file.h>
54 #include <sys/conf.h>
55 #include <sys/device.h>
56 
57 #include <machine/autoconf.h>
58 #include <machine/mon.h>
59 #include <machine/psl.h>
60 
61 #include <dev/cons.h>
62 #include <dev/sun/event_var.h>
63 #include <dev/sun/kbd_xlate.h>
64 #include <dev/sun/kbdvar.h>
65 #include <sun3/dev/zs_cons.h>
66 
67 #include "fb.h"
68 
69 extern void fb_unblank __P((void)); /* XXX */
70 
71 #define	KDMAJOR 1
72 #define PUT_WSIZE	64
73 
74 cdev_decl(kd);	/* open, close, read, write, ioctl, stop, ... */
75 
76 struct kd_softc {
77 	struct	device kd_dev;		/* required first: base device */
78 	struct  tty *kd_tty;
79 
80 	/* Console input hook */
81 	struct cons_channel *kd_in;
82 };
83 
84 /*
85  * There is no point in pretending there might be
86  * more than one keyboard/display device.
87  */
88 static struct kd_softc kd_softc;
89 static int kd_is_console;
90 
91 static int kdparam(struct tty *, struct termios *);
92 static void kdstart(struct tty *);
93 static void kd_init __P((struct kd_softc *));
94 static void kd_cons_input __P((int));
95 
96 
97 /*
98  * Prepare the console tty; called on first open of /dev/console
99  */
100 void
101 kd_init(kd)
102 	struct kd_softc *kd;
103 {
104 	struct tty *tp;
105 
106 	tp = ttymalloc();
107 	tp->t_oproc = kdstart;
108 	tp->t_param = kdparam;
109 	tp->t_dev = makedev(KDMAJOR, 0);
110 
111 	tty_attach(tp);
112 	kd->kd_tty = tp;
113 
114 	return;
115 }
116 
117 struct tty *
118 kdtty(dev)
119 	dev_t dev;
120 {
121 	struct kd_softc *kd;
122 
123 	kd = &kd_softc; 	/* XXX */
124 	return (kd->kd_tty);
125 }
126 
127 int
128 kdopen(dev, flag, mode, p)
129 	dev_t dev;
130 	int flag, mode;
131 	struct proc *p;
132 {
133 	struct kd_softc *kd;
134 	int error, s, unit;
135 	struct tty *tp;
136 static	int firstopen = 1;
137 
138 	unit = minor(dev);
139 	if (unit != 0)
140 		return ENXIO;
141 	kd = &kd_softc; 	/* XXX */
142 	if (firstopen) {
143 		kd_init(kd);
144 		firstopen = 0;
145 	}
146 	tp = kd->kd_tty;
147 
148 	/* It's simpler to do this up here. */
149 	if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE))
150 	     ==             (TS_ISOPEN | TS_XCLUDE))
151 	    && (p->p_ucred->cr_uid != 0) )
152 	{
153 		return (EBUSY);
154 	}
155 
156 	s = spltty();
157 
158 	if ((tp->t_state & TS_ISOPEN) == 0) {
159 		/* First open. */
160 
161 		/* Notify the input device that serves us */
162 		struct cons_channel *cc = kd->kd_in;
163 		if (cc != NULL &&
164 		    (error = (*cc->cc_iopen)(cc)) != 0) {
165 			return (error);
166 		}
167 
168 		ttychars(tp);
169 		tp->t_iflag = TTYDEF_IFLAG;
170 		tp->t_oflag = TTYDEF_OFLAG;
171 		tp->t_cflag = TTYDEF_CFLAG;
172 		tp->t_lflag = TTYDEF_LFLAG;
173 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
174 		(void) kdparam(tp, &tp->t_termios);
175 		ttsetwater(tp);
176 		/* Flush pending input?  Clear translator? */
177 		/* This (pseudo)device always has SOFTCAR */
178 		tp->t_state |= TS_CARR_ON;
179 	}
180 
181 	splx(s);
182 
183 	return ((*linesw[tp->t_line].l_open)(dev, tp));
184 }
185 
186 int
187 kdclose(dev, flag, mode, p)
188 	dev_t dev;
189 	int flag, mode;
190 	struct proc *p;
191 {
192 	struct kd_softc *kd;
193 	struct tty *tp;
194 	struct cons_channel *cc;
195 
196 	kd = &kd_softc; 	/* XXX */
197 	tp = kd->kd_tty;
198 
199 	/* XXX This is for cons.c. */
200 	if ((tp->t_state & TS_ISOPEN) == 0)
201 		return 0;
202 
203 	(*linesw[tp->t_line].l_close)(tp, flag);
204 	ttyclose(tp);
205 	if ((cc = kd->kd_in) != NULL)
206 		(void)(*cc->cc_iclose)(cc->cc_dev);
207 	return (0);
208 }
209 
210 int
211 kdread(dev, uio, flag)
212 	dev_t dev;
213 	struct uio *uio;
214 	int flag;
215 {
216 	struct kd_softc *kd;
217 	struct tty *tp;
218 
219 	kd = &kd_softc; 	/* XXX */
220 	tp = kd->kd_tty;
221 
222 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
223 }
224 
225 int
226 kdwrite(dev, uio, flag)
227 	dev_t dev;
228 	struct uio *uio;
229 	int flag;
230 {
231 	struct kd_softc *kd;
232 	struct tty *tp;
233 
234 	kd = &kd_softc; 	/* XXX */
235 	tp = kd->kd_tty;
236 
237 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
238 }
239 
240 int
241 kdioctl(dev, cmd, data, flag, p)
242 	dev_t dev;
243 	u_long cmd;
244 	caddr_t data;
245 	int flag;
246 	struct proc *p;
247 {
248 	struct kd_softc *kd;
249 	struct tty *tp;
250 	int error;
251 
252 	kd = &kd_softc; 	/* XXX */
253 	tp = kd->kd_tty;
254 
255 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
256 	if (error >= 0)
257 		return error;
258 	error = ttioctl(tp, cmd, data, flag, p);
259 	if (error >= 0)
260 		return error;
261 
262 	/* Handle any ioctl commands specific to kbd/display. */
263 	/* XXX - Send KB* ioctls to kbd module? */
264 	/* XXX - Send FB* ioctls to fb module?  */
265 
266 	return ENOTTY;
267 }
268 
269 void
270 kdstop(tp, flag)
271 	struct tty *tp;
272 	int flag;
273 {
274 
275 }
276 
277 
278 static int
279 kdparam(tp, t)
280 	struct tty *tp;
281 	struct termios *t;
282 {
283 	/* XXX - These are ignored... */
284 	tp->t_ispeed = t->c_ispeed;
285 	tp->t_ospeed = t->c_ospeed;
286 	tp->t_cflag = t->c_cflag;
287 	return 0;
288 }
289 
290 
291 static void kd_later(void*);
292 static void kd_putfb(struct tty *);
293 
294 static void
295 kdstart(tp)
296 	struct tty *tp;
297 {
298 	struct clist *cl;
299 	register int s;
300 
301 	s = spltty();
302 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
303 		goto out;
304 
305 	cl = &tp->t_outq;
306 	if (cl->c_cc) {
307 		if (kd_is_console) {
308 			tp->t_state |= TS_BUSY;
309 			if ((s & PSL_IPL) == 0) {
310 				/* called at level zero - update screen now. */
311 				(void) spllowersoftclock();
312 				kd_putfb(tp);
313 				(void) spltty();
314 				tp->t_state &= ~TS_BUSY;
315 			} else {
316 				/* called at interrupt level - do it later */
317 				callout_reset(&tp->t_rstrt_ch, 0,
318 				    kd_later, tp);
319 			}
320 		} else {
321 			/*
322 			 * This driver uses the PROM for writing the screen,
323 			 * and that only works if this is the console device.
324 			 * If this is not the console, just flush the output.
325 			 * Sorry.  (In that case, use xdm instead of getty.)
326 			 */
327 			ndflush(cl, cl->c_cc);
328 		}
329 	}
330 	if (cl->c_cc <= tp->t_lowat) {
331 		if (tp->t_state & TS_ASLEEP) {
332 			tp->t_state &= ~TS_ASLEEP;
333 			wakeup((caddr_t)cl);
334 		}
335 		selwakeup(&tp->t_wsel);
336 	}
337 out:
338 	splx(s);
339 }
340 
341 /*
342  * Timeout function to do delayed writes to the screen.
343  * Called at splsoftclock when requested by kdstart.
344  */
345 static void
346 kd_later(tpaddr)
347 	void *tpaddr;
348 {
349 	struct tty *tp = tpaddr;
350 	register int s;
351 
352 	kd_putfb(tp);
353 
354 	s = spltty();
355 	tp->t_state &= ~TS_BUSY;
356 	(*linesw[tp->t_line].l_start)(tp);
357 	splx(s);
358 }
359 
360 /*
361  * Put text on the screen using the PROM monitor.
362  * This can take a while, so to avoid missing
363  * interrupts, this is called at splsoftclock.
364  */
365 static void
366 kd_putfb(tp)
367 	struct tty *tp;
368 {
369 	char buf[PUT_WSIZE];
370 	struct clist *cl = &tp->t_outq;
371 	char *p, *end;
372 	int len;
373 
374 	while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
375 		/* PROM will barf if high bits are set. */
376 		p = buf;
377 		end = buf + len;
378 		while (p < end)
379 			*p++ &= 0x7f;
380 		(romVectorPtr->fbWriteStr)(buf, len);
381 	}
382 }
383 
384 void
385 cons_attach_input(cc, cn)
386 	struct cons_channel *cc;
387 	struct consdev *cn;
388 {
389 	struct kd_softc *kd = &kd_softc;
390 
391 	kd->kd_in = cc;
392 	cc->cc_upstream = kd_cons_input;
393 }
394 
395 /*
396  * Default PROM-based console input stream
397  */
398 static int kd_rom_iopen __P((struct cons_channel *));
399 static int kd_rom_iclose __P((struct cons_channel *));
400 
401 static struct cons_channel prom_cons_channel;
402 
403 int
404 kd_rom_iopen(cc)
405 	struct cons_channel *cc;
406 {
407 	/* No-op */
408 	return (0);
409 }
410 
411 int
412 kd_rom_iclose(cc)
413 	struct cons_channel *cc;
414 {
415 	/* No-op */
416 	return (0);
417 }
418 
419 /*
420  * Our "interrupt" routine for input. This is called by
421  * the keyboard driver (dev/sun/kbd.c) at spltty.
422  */
423 void
424 kd_cons_input(c)
425 	int c;
426 {
427 	struct kd_softc *kd = &kd_softc;
428 	struct tty *tp;
429 
430 	/* XXX: Make sure the device is open. */
431 	tp = kd->kd_tty;
432 	if (tp == NULL)
433 		return;
434 	if ((tp->t_state & TS_ISOPEN) == 0)
435 		return;
436 
437 	(*linesw[tp->t_line].l_rint)(c, tp);
438 }
439 
440 
441 /****************************************************************
442  * kd console support
443  ****************************************************************/
444 
445 /* The debugger gets its own key translation state. */
446 static struct kbd_state kdcn_state;
447 
448 static void kdcnprobe __P((struct consdev *));
449 static void kdcninit __P((struct consdev *));
450 static int  kdcngetc __P((dev_t));
451 static void kdcnputc __P((dev_t, int));
452 static void kdcnpollc __P((dev_t, int));
453 
454 struct consdev consdev_kd = {
455 	kdcnprobe,
456 	kdcninit,
457 	kdcngetc,
458 	kdcnputc,
459 	kdcnpollc,
460 	NULL,
461 };
462 
463 /* We never call this. */
464 static void
465 kdcnprobe(cn)
466 	struct consdev *cn;
467 {
468 }
469 
470 static void
471 kdcninit(cn)
472 	struct consdev *cn;
473 {
474 	struct kbd_state *ks = &kdcn_state;
475 
476 	cn->cn_dev = makedev(KDMAJOR, 0);
477 	cn->cn_pri = CN_INTERNAL;
478 
479 	/* This prepares kbd_translate() */
480 	ks->kbd_id = KBD_MIN_TYPE;
481 	kbd_xlate_init(ks);
482 
483 	/* Set up initial PROM input channel for /dev/console */
484 	prom_cons_channel.cc_dev = NULL;
485 	prom_cons_channel.cc_iopen = kd_rom_iopen;
486 	prom_cons_channel.cc_iclose = kd_rom_iclose;
487 	cons_attach_input(&prom_cons_channel, cn);
488 
489 	/* Indicate that it is OK to use the PROM fbwrite */
490 	kd_is_console = 1;
491 }
492 
493 static int
494 kdcngetc(dev)
495 	dev_t dev;
496 {
497 	struct kbd_state *ks = &kdcn_state;
498 	int code, class, data, keysym;
499 
500 	for (;;) {
501 		code = zs_getc(zs_conschan);
502 		keysym = kbd_code_to_keysym(ks, code);
503 		class = KEYSYM_CLASS(keysym);
504 
505 		switch (class) {
506 		case KEYSYM_ASCII:
507 			goto out;
508 
509 		case KEYSYM_CLRMOD:
510 		case KEYSYM_SETMOD:
511 			data = (keysym & 0x1F);
512 			/* Only allow ctrl or shift. */
513 			if (data > KBMOD_SHIFT_R)
514 				break;
515 			data = 1 << data;
516 			if (class == KEYSYM_SETMOD)
517 				ks->kbd_modbits |= data;
518 			else
519 				ks->kbd_modbits &= ~data;
520 			break;
521 
522 		case KEYSYM_ALL_UP:
523 			/* No toggle keys here. */
524 			ks->kbd_modbits = 0;
525 			break;
526 
527 		default:	/* ignore all other keysyms */
528 			break;
529 		}
530 	}
531 out:
532 	return (keysym);
533 }
534 
535 static void
536 kdcnputc(dev, c)
537 	dev_t dev;
538 	int c;
539 {
540 	(romVectorPtr->fbWriteChar)(c & 0x7f);
541 }
542 
543 static void
544 kdcnpollc(dev, on)
545 	dev_t dev;
546 	int on;
547 {
548 	struct kbd_state *ks = &kdcn_state;
549 
550 	if (on) {
551 		/* Entering debugger. */
552 #if NFB > 0
553 		fb_unblank();
554 #endif
555 		/* Clear shift keys too. */
556 		ks->kbd_modbits = 0;
557 	} else {
558 		/* Resuming kernel. */
559 	}
560 }
561 
562