xref: /netbsd/sys/arch/sun3/dev/kd.c (revision bf9ec67e)
1 /*	$NetBSD: kd.c,v 1.37 2002/03/17 19:40:51 atatat 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 ((*tp->t_linesw->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 	(*tp->t_linesw->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 ((*tp->t_linesw->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 ((*tp->t_linesw->l_write)(tp, uio, flag));
238 }
239 
240 int
241 kdpoll(dev, events, p)
242 	dev_t dev;
243 	int events;
244 	struct proc *p;
245 {
246 	struct kd_softc *kd;
247 	struct tty *tp;
248 
249 	kd = &kd_softc; 	/* XXX */
250 	tp = kd->kd_tty;
251 
252 	return ((*tp->t_linesw->l_poll)(tp, events, p));
253 }
254 
255 int
256 kdioctl(dev, cmd, data, flag, p)
257 	dev_t dev;
258 	u_long cmd;
259 	caddr_t data;
260 	int flag;
261 	struct proc *p;
262 {
263 	struct kd_softc *kd;
264 	struct tty *tp;
265 	int error;
266 
267 	kd = &kd_softc; 	/* XXX */
268 	tp = kd->kd_tty;
269 
270 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
271 	if (error != EPASSTHROUGH)
272 		return error;
273 
274 	error = ttioctl(tp, cmd, data, flag, p);
275 	if (error != EPASSTHROUGH)
276 		return error;
277 
278 	/* Handle any ioctl commands specific to kbd/display. */
279 	/* XXX - Send KB* ioctls to kbd module? */
280 	/* XXX - Send FB* ioctls to fb module?  */
281 
282 	return EPASSTHROUGH;
283 }
284 
285 void
286 kdstop(tp, flag)
287 	struct tty *tp;
288 	int flag;
289 {
290 
291 }
292 
293 
294 static int
295 kdparam(tp, t)
296 	struct tty *tp;
297 	struct termios *t;
298 {
299 	/* XXX - These are ignored... */
300 	tp->t_ispeed = t->c_ispeed;
301 	tp->t_ospeed = t->c_ospeed;
302 	tp->t_cflag = t->c_cflag;
303 	return 0;
304 }
305 
306 
307 static void kd_later(void*);
308 static void kd_putfb(struct tty *);
309 
310 static void
311 kdstart(tp)
312 	struct tty *tp;
313 {
314 	struct clist *cl;
315 	int s;
316 
317 	s = spltty();
318 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
319 		goto out;
320 
321 	cl = &tp->t_outq;
322 	if (cl->c_cc) {
323 		if (kd_is_console) {
324 			tp->t_state |= TS_BUSY;
325 			if ((s & PSL_IPL) == 0) {
326 				/* called at level zero - update screen now. */
327 				(void) spllowersoftclock();
328 				kd_putfb(tp);
329 				(void) spltty();
330 				tp->t_state &= ~TS_BUSY;
331 			} else {
332 				/* called at interrupt level - do it later */
333 				callout_reset(&tp->t_rstrt_ch, 0,
334 				    kd_later, tp);
335 			}
336 		} else {
337 			/*
338 			 * This driver uses the PROM for writing the screen,
339 			 * and that only works if this is the console device.
340 			 * If this is not the console, just flush the output.
341 			 * Sorry.  (In that case, use xdm instead of getty.)
342 			 */
343 			ndflush(cl, cl->c_cc);
344 		}
345 	}
346 	if (cl->c_cc <= tp->t_lowat) {
347 		if (tp->t_state & TS_ASLEEP) {
348 			tp->t_state &= ~TS_ASLEEP;
349 			wakeup((caddr_t)cl);
350 		}
351 		selwakeup(&tp->t_wsel);
352 	}
353 out:
354 	splx(s);
355 }
356 
357 /*
358  * Timeout function to do delayed writes to the screen.
359  * Called at splsoftclock when requested by kdstart.
360  */
361 static void
362 kd_later(tpaddr)
363 	void *tpaddr;
364 {
365 	struct tty *tp = tpaddr;
366 	int s;
367 
368 	kd_putfb(tp);
369 
370 	s = spltty();
371 	tp->t_state &= ~TS_BUSY;
372 	(*tp->t_linesw->l_start)(tp);
373 	splx(s);
374 }
375 
376 /*
377  * Put text on the screen using the PROM monitor.
378  * This can take a while, so to avoid missing
379  * interrupts, this is called at splsoftclock.
380  */
381 static void
382 kd_putfb(tp)
383 	struct tty *tp;
384 {
385 	char buf[PUT_WSIZE];
386 	struct clist *cl = &tp->t_outq;
387 	char *p, *end;
388 	int len;
389 
390 	while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
391 		/* PROM will barf if high bits are set. */
392 		p = buf;
393 		end = buf + len;
394 		while (p < end)
395 			*p++ &= 0x7f;
396 		(romVectorPtr->fbWriteStr)(buf, len);
397 	}
398 }
399 
400 void
401 cons_attach_input(cc, cn)
402 	struct cons_channel *cc;
403 	struct consdev *cn;
404 {
405 	struct kd_softc *kd = &kd_softc;
406 
407 	kd->kd_in = cc;
408 	cc->cc_upstream = kd_cons_input;
409 }
410 
411 /*
412  * Default PROM-based console input stream
413  */
414 static int kd_rom_iopen __P((struct cons_channel *));
415 static int kd_rom_iclose __P((struct cons_channel *));
416 
417 static struct cons_channel prom_cons_channel;
418 
419 int
420 kd_rom_iopen(cc)
421 	struct cons_channel *cc;
422 {
423 	/* No-op */
424 	return (0);
425 }
426 
427 int
428 kd_rom_iclose(cc)
429 	struct cons_channel *cc;
430 {
431 	/* No-op */
432 	return (0);
433 }
434 
435 /*
436  * Our "interrupt" routine for input. This is called by
437  * the keyboard driver (dev/sun/kbd.c) at spltty.
438  */
439 void
440 kd_cons_input(c)
441 	int c;
442 {
443 	struct kd_softc *kd = &kd_softc;
444 	struct tty *tp;
445 
446 	/* XXX: Make sure the device is open. */
447 	tp = kd->kd_tty;
448 	if (tp == NULL)
449 		return;
450 	if ((tp->t_state & TS_ISOPEN) == 0)
451 		return;
452 
453 	(*tp->t_linesw->l_rint)(c, tp);
454 }
455 
456 
457 /****************************************************************
458  * kd console support
459  ****************************************************************/
460 
461 /* The debugger gets its own key translation state. */
462 static struct kbd_state kdcn_state;
463 
464 static void kdcnprobe __P((struct consdev *));
465 static void kdcninit __P((struct consdev *));
466 static int  kdcngetc __P((dev_t));
467 static void kdcnputc __P((dev_t, int));
468 static void kdcnpollc __P((dev_t, int));
469 
470 struct consdev consdev_kd = {
471 	kdcnprobe,
472 	kdcninit,
473 	kdcngetc,
474 	kdcnputc,
475 	kdcnpollc,
476 	NULL,
477 };
478 
479 /* We never call this. */
480 static void
481 kdcnprobe(cn)
482 	struct consdev *cn;
483 {
484 }
485 
486 static void
487 kdcninit(cn)
488 	struct consdev *cn;
489 {
490 	struct kbd_state *ks = &kdcn_state;
491 
492 	cn->cn_dev = makedev(KDMAJOR, 0);
493 	cn->cn_pri = CN_INTERNAL;
494 
495 	/* This prepares kbd_translate() */
496 	ks->kbd_id = KBD_MIN_TYPE;
497 	kbd_xlate_init(ks);
498 
499 	/* Set up initial PROM input channel for /dev/console */
500 	prom_cons_channel.cc_dev = NULL;
501 	prom_cons_channel.cc_iopen = kd_rom_iopen;
502 	prom_cons_channel.cc_iclose = kd_rom_iclose;
503 	cons_attach_input(&prom_cons_channel, cn);
504 
505 	/* Indicate that it is OK to use the PROM fbwrite */
506 	kd_is_console = 1;
507 }
508 
509 static int
510 kdcngetc(dev)
511 	dev_t dev;
512 {
513 	struct kbd_state *ks = &kdcn_state;
514 	int code, class, data, keysym;
515 
516 	for (;;) {
517 		code = zs_getc(zs_conschan);
518 		keysym = kbd_code_to_keysym(ks, code);
519 		class = KEYSYM_CLASS(keysym);
520 
521 		switch (class) {
522 		case KEYSYM_ASCII:
523 			goto out;
524 
525 		case KEYSYM_CLRMOD:
526 		case KEYSYM_SETMOD:
527 			data = (keysym & 0x1F);
528 			/* Only allow ctrl or shift. */
529 			if (data > KBMOD_SHIFT_R)
530 				break;
531 			data = 1 << data;
532 			if (class == KEYSYM_SETMOD)
533 				ks->kbd_modbits |= data;
534 			else
535 				ks->kbd_modbits &= ~data;
536 			break;
537 
538 		case KEYSYM_ALL_UP:
539 			/* No toggle keys here. */
540 			ks->kbd_modbits = 0;
541 			break;
542 
543 		default:	/* ignore all other keysyms */
544 			break;
545 		}
546 	}
547 out:
548 	return (keysym);
549 }
550 
551 static void
552 kdcnputc(dev, c)
553 	dev_t dev;
554 	int c;
555 {
556 	(romVectorPtr->fbWriteChar)(c & 0x7f);
557 }
558 
559 static void
560 kdcnpollc(dev, on)
561 	dev_t dev;
562 	int on;
563 {
564 	struct kbd_state *ks = &kdcn_state;
565 
566 	if (on) {
567 		/* Entering debugger. */
568 #if NFB > 0
569 		fb_unblank();
570 #endif
571 		/* Clear shift keys too. */
572 		ks->kbd_modbits = 0;
573 	} else {
574 		/* Resuming kernel. */
575 	}
576 }
577 
578