xref: /netbsd-src/sys/arch/sparc64/dev/kd.c (revision ce2c90c7c172d95d2402a5b3d96d8f8e6d138a21)
1 /*	$NetBSD: kd.c,v 1.41 2006/10/01 18:56:22 elad 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.41 2006/10/01 18:56:22 elad 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/openfirm.h>
62 #include <machine/eeprom.h>
63 #include <machine/psl.h>
64 #include <machine/cpu.h>
65 #include <machine/kbd.h>
66 #include <machine/autoconf.h>
67 
68 #include <dev/cons.h>
69 #include <dev/sun/event_var.h>
70 #include <dev/sun/kbd_xlate.h>
71 #include <dev/sun/kbdvar.h>
72 #include <sparc64/dev/cons.h>
73 
74 dev_type_open(kdopen);
75 dev_type_close(kdclose);
76 dev_type_read(kdread);
77 dev_type_write(kdwrite);
78 dev_type_ioctl(kdioctl);
79 dev_type_tty(kdtty);
80 dev_type_poll(kdpoll);
81 
82 const struct cdevsw kd_cdevsw = {
83 	kdopen, kdclose, kdread, kdwrite, kdioctl,
84 	nostop, kdtty, kdpoll, nommap, ttykqfilter, D_TTY
85 };
86 
87 struct	tty *fbconstty = 0;	/* tty structure for frame buffer console */
88 
89 #define PUT_WSIZE	64
90 
91 struct kd_softc {
92 	struct	device kd_dev;		/* required first: base device */
93 	struct  tty *kd_tty;
94 	int rows, cols;
95 
96 	/* Console input hook */
97 	struct cons_channel *kd_in;
98 };
99 
100 /*
101  * There is no point in pretending there might be
102  * more than one keyboard/display device.
103  */
104 static struct kd_softc kd_softc;
105 static int kd_is_console;
106 
107 static int kdparam(struct tty *, struct termios *);
108 static void kdstart(struct tty *);
109 static void kd_init(struct kd_softc *);
110 static void kd_cons_input(int);
111 static int  kdcngetc(dev_t);
112 
113 int	cons_ocount;		/* output byte count */
114 
115 /*
116  * This is called by kbd_attach()
117  * XXX - Make this a proper child of kbd?
118  */
119 void
120 kd_init(struct kd_softc *kd)
121 {
122 	struct tty *tp;
123 	char prop[6+1];
124 
125 	kd = &kd_softc; 	/* XXX */
126 
127 	tp = ttymalloc();
128 	tp->t_oproc = kdstart;
129 	tp->t_param = kdparam;
130 	tp->t_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
131 
132 	tty_attach(tp);
133 	kd->kd_tty = tp;
134 
135 	/*
136 	 * get the console struct winsize.
137 	 */
138 	if (kd_is_console) {
139 		fbconstty = tp;
140 	}
141 
142 	if (kd->rows == 0 &&
143 	    prom_getoption("screen-#rows", prop, sizeof prop) == 0)
144 		kd->rows = strtoul(prop, NULL, 10);
145 
146 	if (kd->cols == 0 &&
147 	    prom_getoption("screen-#columns", prop, sizeof prop) == 0)
148 		kd->cols = strtoul(prop, NULL, 10);
149 }
150 
151 struct tty *
152 kdtty(dev_t dev)
153 {
154 	struct kd_softc *kd;
155 
156 	kd = &kd_softc; 	/* XXX */
157 	return (kd->kd_tty);
158 }
159 
160 int
161 kdopen(dev_t dev, int flag, int mode, struct lwp *l)
162 {
163 	struct kd_softc *kd;
164 	int error, s, unit;
165 	struct tty *tp;
166 static	int firstopen = 1;
167 
168 	unit = minor(dev);
169 	if (unit != 0)
170 		return ENXIO;
171 	kd = &kd_softc; 	/* XXX */
172 
173 	if (firstopen) {
174 		kd_init(kd);
175 		firstopen = 0;
176 	}
177 	tp = kd->kd_tty;
178 
179 	/* It's simpler to do this up here. */
180 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
181 		return (EBUSY);
182 
183 	s = spltty();
184 
185 	if ((tp->t_state & TS_ISOPEN) == 0) {
186 		/* First open. */
187 
188 		/* Notify the input device that serves us */
189 		struct cons_channel *cc = kd->kd_in;
190 		if (cc != NULL &&
191 		    (error = (*cc->cc_iopen)(cc)) != 0) {
192 			splx(s);
193 			return (error);
194 		}
195 
196 		ttychars(tp);
197 		tp->t_iflag = TTYDEF_IFLAG;
198 		tp->t_oflag = TTYDEF_OFLAG;
199 		tp->t_cflag = TTYDEF_CFLAG;
200 		tp->t_lflag = TTYDEF_LFLAG;
201 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
202 		(void) kdparam(tp, &tp->t_termios);
203 		ttsetwater(tp);
204 		tp->t_winsize.ws_row = kd->rows;
205 		tp->t_winsize.ws_col = kd->cols;
206 		/* Flush pending input?  Clear translator? */
207 		/* This (pseudo)device always has SOFTCAR */
208 		tp->t_state |= TS_CARR_ON;
209 	}
210 
211 	splx(s);
212 
213 	return ((*tp->t_linesw->l_open)(dev, tp));
214 }
215 
216 int
217 kdclose(dev_t dev, int flag, int mode, struct lwp *l)
218 {
219 	struct kd_softc *kd;
220 	struct tty *tp;
221 	struct cons_channel *cc;
222 
223 	kd = &kd_softc; 	/* XXX */
224 	tp = kd->kd_tty;
225 
226 	/* XXX This is for cons.c. */
227 	if ((tp->t_state & TS_ISOPEN) == 0)
228 		return 0;
229 
230 	(*tp->t_linesw->l_close)(tp, flag);
231 	ttyclose(tp);
232 
233 	if ((cc = kd->kd_in) != NULL)
234 		(void)(*cc->cc_iclose)(cc);
235 
236 	return (0);
237 }
238 
239 int
240 kdread(dev_t dev, struct uio *uio, int flag)
241 {
242 	struct kd_softc *kd;
243 	struct tty *tp;
244 
245 	kd = &kd_softc; 	/* XXX */
246 	tp = kd->kd_tty;
247 
248 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
249 }
250 
251 int
252 kdwrite(dev_t dev, struct uio *uio, int flag)
253 {
254 	struct kd_softc *kd;
255 	struct tty *tp;
256 
257 	kd = &kd_softc; 	/* XXX */
258 	tp = kd->kd_tty;
259 
260 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
261 }
262 
263 int
264 kdpoll(dev_t dev, int events, struct lwp *l)
265 {
266 	struct kd_softc *kd;
267 	struct tty *tp;
268 
269 	kd = &kd_softc; 	/* XXX */
270 	tp = kd->kd_tty;
271 
272 	return ((*tp->t_linesw->l_poll)(tp, events, l));
273 }
274 
275 int
276 kdioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
277 {
278 	struct kd_softc *kd;
279 	struct tty *tp;
280 	int error;
281 
282 	kd = &kd_softc; 	/* XXX */
283 	tp = kd->kd_tty;
284 
285 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
286 	if (error != EPASSTHROUGH)
287 		return error;
288 
289 	error = ttioctl(tp, cmd, data, flag, l);
290 	if (error != EPASSTHROUGH)
291 		return error;
292 
293 	/* Handle any ioctl commands specific to kbd/display. */
294 	/* XXX - Send KB* ioctls to kbd module? */
295 	/* XXX - Send FB* ioctls to fb module?  */
296 
297 	return EPASSTHROUGH;
298 }
299 
300 static int
301 kdparam(struct tty *tp, struct termios *t)
302 {
303 	/* XXX - These are ignored... */
304 	tp->t_ispeed = t->c_ispeed;
305 	tp->t_ospeed = t->c_ospeed;
306 	tp->t_cflag = t->c_cflag;
307 	return 0;
308 }
309 
310 
311 static void kd_later(void*);
312 static void kd_putfb(struct tty *);
313 
314 static void
315 kdstart(struct tty *tp)
316 {
317 	struct clist *cl;
318 	register int s;
319 
320 	s = spltty();
321 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
322 		goto out;
323 
324 	cl = &tp->t_outq;
325 	if (cl->c_cc) {
326 		if (kd_is_console) {
327 			tp->t_state |= TS_BUSY;
328 			if (s == 0) {
329 				/* called at level zero - update screen now. */
330 				(void) spllowersoftclock();
331 				kd_putfb(tp);
332 				(void) spltty();
333 				tp->t_state &= ~TS_BUSY;
334 			} else {
335 				/* called at interrupt level - do it later */
336 				callout_reset(&tp->t_rstrt_ch, 0,
337 				    kd_later, tp);
338 			}
339 		} else {
340 			/*
341 			 * This driver uses the PROM for writing the screen,
342 			 * and that only works if this is the console device.
343 			 * If this is not the console, just flush the output.
344 			 * Sorry.  (In that case, use xdm instead of getty.)
345 			 */
346 			ndflush(cl, cl->c_cc);
347 		}
348 	}
349 	if (cl->c_cc <= tp->t_lowat) {
350 		if (tp->t_state & TS_ASLEEP) {
351 			tp->t_state &= ~TS_ASLEEP;
352 			wakeup((caddr_t)cl);
353 		}
354 		selwakeup(&tp->t_wsel);
355 	}
356 out:
357 	splx(s);
358 }
359 
360 /*
361  * Timeout function to do delayed writes to the screen.
362  * Called at splsoftclock when requested by kdstart.
363  */
364 static void
365 kd_later(void *tpaddr)
366 {
367 	struct tty *tp = tpaddr;
368 	register int s;
369 
370 	kd_putfb(tp);
371 
372 	s = spltty();
373 	tp->t_state &= ~TS_BUSY;
374 	(*tp->t_linesw->l_start)(tp);
375 	splx(s);
376 }
377 
378 /*
379  * Put text on the screen using the PROM monitor.
380  * This can take a while, so to avoid missing
381  * interrupts, this is called at splsoftclock.
382  */
383 static void
384 kd_putfb(struct tty *tp)
385 {
386 	char buf[PUT_WSIZE];
387 	struct clist *cl = &tp->t_outq;
388 	char *p, *end;
389 	int len;
390 
391 	while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
392 		/* PROM will barf if high bits are set. */
393 		p = buf;
394 		end = buf + len;
395 		while (p < end)
396 			*p++ &= 0x7f;
397 		/* Now let the PROM print it. */
398 		prom_write(prom_stdout(), buf, len);
399 	}
400 }
401 
402 /*
403  * Default PROM-based console input stream
404  */
405 static int kd_rom_iopen(struct cons_channel *);
406 static int kd_rom_iclose(struct cons_channel *);
407 
408 static struct cons_channel prom_cons_channel;
409 
410 int
411 kd_rom_iopen(struct cons_channel *cc)
412 {
413 	return (0);
414 }
415 
416 int
417 kd_rom_iclose(struct cons_channel *cc)
418 {
419 	return (0);
420 }
421 
422 /*
423  * Our "interrupt" routine for input. This is called by
424  * the keyboard driver (dev/sun/kbd.c) at spltty.
425  */
426 void
427 kd_cons_input(int c)
428 {
429 	struct kd_softc *kd = &kd_softc;
430 	struct tty *tp;
431 
432 	/* XXX: Make sure the device is open. */
433 	tp = kd->kd_tty;
434 	if (tp == NULL)
435 		return;
436 	if ((tp->t_state & TS_ISOPEN) == 0)
437 		return;
438 
439 	(*tp->t_linesw->l_rint)(c, tp);
440 }
441 
442 
443 /****************************************************************
444  * kd console support
445  ****************************************************************/
446 
447 /* The debugger gets its own key translation state. */
448 static struct kbd_state *kdcn_state;
449 
450 static void kdcnprobe(struct consdev *);
451 static void kdcninit(struct consdev *);
452 static void kdcnputc(dev_t, int);
453 static void kdcnpollc(dev_t, int);
454 
455 /* The keyboard driver uses cn_hw to access the real console driver */
456 extern struct consdev consdev_prom;
457 struct consdev consdev_kd = {
458 	kdcnprobe,
459 	kdcninit,
460 	kdcngetc,
461 	kdcnputc,
462 	kdcnpollc,
463 	NULL,
464 };
465 struct consdev *cn_hw = &consdev_kd;
466 
467 void
468 cons_attach_input(struct cons_channel *cc, struct consdev *cn)
469 {
470 	struct kd_softc *kd = &kd_softc;
471 	struct kbd_softc *kds = cc->cc_dev;
472 	struct kbd_state *ks;
473 
474 	/* Share the keyboard state */
475 	kdcn_state = ks = &kds->k_state;
476 
477 	kd->kd_in = cc;
478 	cc->cc_upstream = kd_cons_input;
479 
480 	/* Attach lower level. */
481 	cn_hw->cn_dev = cn->cn_dev;
482 	cn_hw->cn_pollc = cn->cn_pollc;
483 	cn_hw->cn_getc = cn->cn_getc;
484 
485 	/* Attach us as console. */
486 	cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
487 	cn_tab->cn_probe = kdcnprobe;
488 	cn_tab->cn_init = kdcninit;
489 	cn_tab->cn_getc = kdcngetc;
490 	cn_tab->cn_pollc = kdcnpollc;
491 	cn_tab->cn_pri = CN_INTERNAL;
492 
493 	/* Set up initial PROM input channel for /dev/console */
494 	prom_cons_channel.cc_dev = NULL;
495 	prom_cons_channel.cc_iopen = kd_rom_iopen;
496 	prom_cons_channel.cc_iclose = kd_rom_iclose;
497 
498 	/* Indicate that it is OK to use the PROM fbwrite */
499 	kd_is_console = 1;
500 }
501 
502 
503 void kd_attach_input(struct cons_channel *);
504 void
505 kd_attach_input(struct cons_channel *cc)
506 {
507 	struct kd_softc *kd = &kd_softc;
508 
509 	kd->kd_in = cc;
510 	cc->cc_upstream = kd_cons_input;
511 }
512 
513 
514 /* We never call this. */
515 static void
516 kdcnprobe(struct consdev *cn)
517 {
518 }
519 
520 static void
521 kdcninit(struct consdev *cn)
522 {
523 #if 0
524 	struct kbd_state *ks = kdcn_state;
525 
526 	cn->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
527 	cn->cn_pri = CN_INTERNAL;
528 
529 	/* This prepares kbd_translate() */
530 	ks->kbd_id = KBD_MIN_TYPE;
531 	kbd_xlate_init(ks);
532 
533 	/* Set up initial PROM input channel for /dev/console */
534 	prom_cons_channel.cc_dev = NULL;
535 	prom_cons_channel.cc_iopen = kd_rom_iopen;
536 	prom_cons_channel.cc_iclose = kd_rom_iclose;
537 	cons_attach_input(&prom_cons_channel);
538 
539 	/* Indicate that it is OK to use the PROM fbwrite */
540 	kd_is_console = 1;
541 #endif
542 }
543 
544 static int
545 kdcngetc(dev_t dev)
546 {
547 	struct kbd_state *ks = kdcn_state;
548 	int code, class, data, keysym;
549 	extern int prom_cngetc(dev_t);
550 
551 
552 	if (cn_hw->cn_getc == prom_cngetc) return (*cn_hw->cn_getc)(dev);
553 	for (;;) {
554 		code = (*cn_hw->cn_getc)(dev);
555 		keysym = kbd_code_to_keysym(ks, code);
556 		class = KEYSYM_CLASS(keysym);
557 
558 		switch (class) {
559 		case KEYSYM_ASCII:
560 			goto out;
561 
562 		case KEYSYM_CLRMOD:
563 		case KEYSYM_SETMOD:
564 			data = (keysym & 0x1F);
565 			/* Only allow ctrl or shift. */
566 			if (data > KBMOD_SHIFT_R)
567 				break;
568 			data = 1 << data;
569 			if (class == KEYSYM_SETMOD)
570 				ks->kbd_modbits |= data;
571 			else
572 				ks->kbd_modbits &= ~data;
573 			break;
574 
575 		case KEYSYM_ALL_UP:
576 			/* No toggle keys here. */
577 			ks->kbd_modbits = 0;
578 			break;
579 
580 		default:	/* ignore all other keysyms */
581 			break;
582 		}
583 	}
584 out:
585 	return (keysym);
586 }
587 
588 static void
589 kdcnputc(dev_t dev, int c)
590 {
591 	int s;
592 	char c0 = (c & 0x7f);
593 
594 	s = splhigh();
595 	prom_write(prom_stdout(), &c0, 1);
596 	splx(s);
597 }
598 
599 static void
600 kdcnpollc(dev_t dev, int on)
601 {
602 	struct kbd_state *ks = kdcn_state;
603 
604 	if (on) {
605 		/* Entering debugger. */
606 #if NFB > 0
607 		fb_unblank();
608 #endif
609 		/* Clear shift keys too. */
610 		ks->kbd_modbits = 0;
611 	} else {
612 		/* Resuming kernel. */
613 	}
614 	(*cn_hw->cn_pollc)(dev, on);
615 }
616