xref: /netbsd-src/sys/arch/sun3/dev/kd.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: kd.c,v 1.52 2007/10/28 13:29:21 rjs 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/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: kd.c,v 1.52 2007/10/28 13:29:21 rjs Exp $");
50 
51 #include <sys/param.h>
52 #include <sys/proc.h>
53 #include <sys/systm.h>
54 #include <sys/ioctl.h>
55 #include <sys/tty.h>
56 #include <sys/file.h>
57 #include <sys/conf.h>
58 #include <sys/device.h>
59 #include <sys/kauth.h>
60 
61 #include <machine/autoconf.h>
62 #include <machine/cpu.h>
63 #include <machine/mon.h>
64 #include <machine/psl.h>
65 
66 #include <dev/cons.h>
67 #include <dev/sun/event_var.h>
68 #include <dev/sun/kbd_xlate.h>
69 #include <dev/sun/kbdvar.h>
70 #include <sun3/dev/zs_cons.h>
71 
72 #include "fb.h"
73 
74 #define PUT_WSIZE	64
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(struct kd_softc *);
94 static void kd_cons_input(int);
95 static void kd_later(void *);
96 
97 dev_type_open(kdopen);
98 dev_type_close(kdclose);
99 dev_type_read(kdread);
100 dev_type_write(kdwrite);
101 dev_type_ioctl(kdioctl);
102 dev_type_tty(kdtty);
103 dev_type_poll(kdpoll);
104 
105 const struct cdevsw kd_cdevsw = {
106 	kdopen, kdclose, kdread, kdwrite, kdioctl,
107 	nostop, kdtty, kdpoll, nommap, ttykqfilter, D_TTY
108 };
109 
110 /*
111  * Prepare the console tty; called on first open of /dev/console
112  */
113 void
114 kd_init(struct kd_softc *kd)
115 {
116 	struct tty *tp;
117 
118 	tp = ttymalloc();
119 	callout_setfunc(&tp->t_rstrt_ch, kd_later, tp);
120 
121 	tp->t_oproc = kdstart;
122 	tp->t_param = kdparam;
123 	tp->t_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
124 
125 	tty_attach(tp);
126 	kd->kd_tty = tp;
127 
128 	return;
129 }
130 
131 struct tty *
132 kdtty(dev_t dev)
133 {
134 	struct kd_softc *kd;
135 
136 	kd = &kd_softc; 	/* XXX */
137 	return (kd->kd_tty);
138 }
139 
140 int
141 kdopen(dev_t dev, int flag, int mode, struct lwp *l)
142 {
143 	struct kd_softc *kd;
144 	int error, s, unit;
145 	struct tty *tp;
146 static	int firstopen = 1;
147 
148 	unit = minor(dev);
149 	if (unit != 0)
150 		return ENXIO;
151 	kd = &kd_softc; 	/* XXX */
152 	if (firstopen) {
153 		kd_init(kd);
154 		firstopen = 0;
155 	}
156 	tp = kd->kd_tty;
157 
158 	/* It's simpler to do this up here. */
159 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
160 		return (EBUSY);
161 
162 	s = spltty();
163 
164 	if ((tp->t_state & TS_ISOPEN) == 0) {
165 		/* First open. */
166 
167 		/* Notify the input device that serves us */
168 		struct cons_channel *cc = kd->kd_in;
169 		if (cc != NULL &&
170 		    (error = (*cc->cc_iopen)(cc)) != 0) {
171 			return (error);
172 		}
173 
174 		ttychars(tp);
175 		tp->t_iflag = TTYDEF_IFLAG;
176 		tp->t_oflag = TTYDEF_OFLAG;
177 		tp->t_cflag = TTYDEF_CFLAG;
178 		tp->t_lflag = TTYDEF_LFLAG;
179 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
180 		(void) kdparam(tp, &tp->t_termios);
181 		ttsetwater(tp);
182 		/* Flush pending input?  Clear translator? */
183 		/* This (pseudo)device always has SOFTCAR */
184 		tp->t_state |= TS_CARR_ON;
185 	}
186 
187 	splx(s);
188 
189 	return ((*tp->t_linesw->l_open)(dev, tp));
190 }
191 
192 int
193 kdclose(dev_t dev, int flag, int mode, struct lwp *l)
194 {
195 	struct kd_softc *kd;
196 	struct tty *tp;
197 	struct cons_channel *cc;
198 
199 	kd = &kd_softc; 	/* XXX */
200 	tp = kd->kd_tty;
201 
202 	/* XXX This is for cons.c. */
203 	if ((tp->t_state & TS_ISOPEN) == 0)
204 		return 0;
205 
206 	(*tp->t_linesw->l_close)(tp, flag);
207 	ttyclose(tp);
208 	if ((cc = kd->kd_in) != NULL)
209 		(void)(*cc->cc_iclose)(cc);
210 	return (0);
211 }
212 
213 int
214 kdread(dev_t dev, struct uio *uio, 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_t dev, struct uio *uio, int flag)
227 {
228 	struct kd_softc *kd;
229 	struct tty *tp;
230 
231 	kd = &kd_softc; 	/* XXX */
232 	tp = kd->kd_tty;
233 
234 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
235 }
236 
237 int
238 kdpoll(dev_t dev, int events, struct lwp *l)
239 {
240 	struct kd_softc *kd;
241 	struct tty *tp;
242 
243 	kd = &kd_softc; 	/* XXX */
244 	tp = kd->kd_tty;
245 
246 	return ((*tp->t_linesw->l_poll)(tp, events, l));
247 }
248 
249 int
250 kdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
251 {
252 	struct kd_softc *kd;
253 	struct tty *tp;
254 	int error;
255 
256 	kd = &kd_softc; 	/* XXX */
257 	tp = kd->kd_tty;
258 
259 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
260 	if (error != EPASSTHROUGH)
261 		return error;
262 
263 	error = ttioctl(tp, cmd, data, flag, l);
264 	if (error != EPASSTHROUGH)
265 		return error;
266 
267 	/* Handle any ioctl commands specific to kbd/display. */
268 	/* XXX - Send KB* ioctls to kbd module? */
269 	/* XXX - Send FB* ioctls to fb module?  */
270 
271 	return EPASSTHROUGH;
272 }
273 
274 static int
275 kdparam(struct tty *tp, struct termios *t)
276 {
277 	/* XXX - These are ignored... */
278 	tp->t_ispeed = t->c_ispeed;
279 	tp->t_ospeed = t->c_ospeed;
280 	tp->t_cflag = t->c_cflag;
281 	return 0;
282 }
283 
284 
285 static void kd_putfb(struct tty *);
286 
287 static void
288 kdstart(struct tty *tp)
289 {
290 	struct clist *cl;
291 	int s1, s2;
292 
293 	s1 = splsoftclock();
294 	s2 = spltty();
295 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
296 		goto out;
297 
298 	cl = &tp->t_outq;
299 	if (cl->c_cc) {
300 		if (kd_is_console) {
301 			tp->t_state |= TS_BUSY;
302 			if ((s1 & PSL_IPL) == 0) {
303 				/* called at level zero - update screen now. */
304 				splx(s2);
305 				kd_putfb(tp);
306 				s2 = spltty();
307 				tp->t_state &= ~TS_BUSY;
308 			} else {
309 				/* called at interrupt level - do it later */
310 				callout_schedule(&tp->t_rstrt_ch, 0);
311 			}
312 		} else {
313 			/*
314 			 * This driver uses the PROM for writing the screen,
315 			 * and that only works if this is the console device.
316 			 * If this is not the console, just flush the output.
317 			 * Sorry.  (In that case, use xdm instead of getty.)
318 			 */
319 			ndflush(cl, cl->c_cc);
320 		}
321 	}
322 	if (cl->c_cc <= tp->t_lowat) {
323 		if (tp->t_state & TS_ASLEEP) {
324 			tp->t_state &= ~TS_ASLEEP;
325 			wakeup((void *)cl);
326 		}
327 		selwakeup(&tp->t_wsel);
328 	}
329 out:
330 	splx(s2);
331 	splx(s1);
332 }
333 
334 /*
335  * Timeout function to do delayed writes to the screen.
336  * Called at splsoftclock when requested by kdstart.
337  */
338 static void
339 kd_later(void *tpaddr)
340 {
341 	struct tty *tp = tpaddr;
342 	int s;
343 
344 	kd_putfb(tp);
345 
346 	s = spltty();
347 	tp->t_state &= ~TS_BUSY;
348 	(*tp->t_linesw->l_start)(tp);
349 	splx(s);
350 }
351 
352 /*
353  * Put text on the screen using the PROM monitor.
354  * This can take a while, so to avoid missing
355  * interrupts, this is called at splsoftclock.
356  */
357 static void
358 kd_putfb(struct tty *tp)
359 {
360 	char buf[PUT_WSIZE];
361 	struct clist *cl = &tp->t_outq;
362 	char *p, *end;
363 	int len;
364 
365 	while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
366 		/* PROM will barf if high bits are set. */
367 		p = buf;
368 		end = buf + len;
369 		while (p < end)
370 			*p++ &= 0x7f;
371 		(romVectorPtr->fbWriteStr)(buf, len);
372 	}
373 }
374 
375 void
376 cons_attach_input(struct cons_channel *cc, struct consdev *cn)
377 {
378 	struct kd_softc *kd = &kd_softc;
379 
380 	kd->kd_in = cc;
381 	cc->cc_upstream = kd_cons_input;
382 }
383 
384 /*
385  * Default PROM-based console input stream
386  */
387 static int kd_rom_iopen(struct cons_channel *);
388 static int kd_rom_iclose(struct cons_channel *);
389 
390 static struct cons_channel prom_cons_channel;
391 
392 int
393 kd_rom_iopen(struct cons_channel *cc)
394 {
395 	/* No-op */
396 	return (0);
397 }
398 
399 int
400 kd_rom_iclose(struct cons_channel *cc)
401 {
402 	/* No-op */
403 	return (0);
404 }
405 
406 /*
407  * Our "interrupt" routine for input. This is called by
408  * the keyboard driver (dev/sun/kbd.c) at spltty.
409  */
410 void
411 kd_cons_input(int c)
412 {
413 	struct kd_softc *kd = &kd_softc;
414 	struct tty *tp;
415 
416 	/* XXX: Make sure the device is open. */
417 	tp = kd->kd_tty;
418 	if (tp == NULL)
419 		return;
420 	if ((tp->t_state & TS_ISOPEN) == 0)
421 		return;
422 
423 	(*tp->t_linesw->l_rint)(c, tp);
424 }
425 
426 
427 /****************************************************************
428  * kd console support
429  ****************************************************************/
430 
431 /* The debugger gets its own key translation state. */
432 static struct kbd_state kdcn_state;
433 
434 static void kdcnprobe(struct consdev *);
435 static void kdcninit(struct consdev *);
436 static int  kdcngetc(dev_t);
437 static void kdcnputc(dev_t, int);
438 static void kdcnpollc(dev_t, int);
439 
440 struct consdev consdev_kd = {
441 	kdcnprobe,
442 	kdcninit,
443 	kdcngetc,
444 	kdcnputc,
445 	kdcnpollc,
446 	NULL,
447 };
448 
449 /* We never call this. */
450 static void
451 kdcnprobe(struct consdev *cn)
452 {
453 }
454 
455 static void
456 kdcninit(struct consdev *cn)
457 {
458 	struct kbd_state *ks = &kdcn_state;
459 
460 	cn->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
461 	cn->cn_pri = CN_INTERNAL;
462 
463 	/* This prepares kbd_translate() */
464 	ks->kbd_id = KBD_MIN_TYPE;
465 	kbd_xlate_init(ks);
466 
467 	/* Set up initial PROM input channel for /dev/console */
468 	prom_cons_channel.cc_dev = NULL;
469 	prom_cons_channel.cc_iopen = kd_rom_iopen;
470 	prom_cons_channel.cc_iclose = kd_rom_iclose;
471 	cons_attach_input(&prom_cons_channel, cn);
472 
473 	/* Indicate that it is OK to use the PROM fbwrite */
474 	kd_is_console = 1;
475 }
476 
477 static int
478 kdcngetc(dev_t dev)
479 {
480 	struct kbd_state *ks = &kdcn_state;
481 	int code, class, data, keysym;
482 
483 	for (;;) {
484 		code = zs_getc(zs_conschan);
485 		keysym = kbd_code_to_keysym(ks, code);
486 		class = KEYSYM_CLASS(keysym);
487 
488 		switch (class) {
489 		case KEYSYM_ASCII:
490 			goto out;
491 
492 		case KEYSYM_CLRMOD:
493 		case KEYSYM_SETMOD:
494 			data = (keysym & 0x1F);
495 			/* Only allow ctrl or shift. */
496 			if (data > KBMOD_SHIFT_R)
497 				break;
498 			data = 1 << data;
499 			if (class == KEYSYM_SETMOD)
500 				ks->kbd_modbits |= data;
501 			else
502 				ks->kbd_modbits &= ~data;
503 			break;
504 
505 		case KEYSYM_ALL_UP:
506 			/* No toggle keys here. */
507 			ks->kbd_modbits = 0;
508 			break;
509 
510 		default:	/* ignore all other keysyms */
511 			break;
512 		}
513 	}
514 out:
515 	return (keysym);
516 }
517 
518 static void
519 kdcnputc(dev_t dev, int c)
520 {
521 	(romVectorPtr->fbWriteChar)(c & 0x7f);
522 }
523 
524 static void
525 kdcnpollc(dev_t dev, int on)
526 {
527 	struct kbd_state *ks = &kdcn_state;
528 
529 	if (on) {
530 		/* Entering debugger. */
531 #if NFB > 0
532 		fb_unblank();
533 #endif
534 		/* Clear shift keys too. */
535 		ks->kbd_modbits = 0;
536 	} else {
537 		/* Resuming kernel. */
538 	}
539 }
540 
541