xref: /netbsd-src/sys/arch/sparc/dev/kd.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: kd.c,v 1.52 2013/10/19 19:40:23 mrg 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Console driver based on PROM primitives.
34  *
35  * This driver exists to provide a tty device that the indirect
36  * console driver can point to. It also provides a hook that
37  * allows another device to serve console input. This will normally
38  * be a keyboard driver (see sys/dev/sun/kbd.c)
39  */
40 
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: kd.c,v 1.52 2013/10/19 19:40:23 mrg Exp $");
43 
44 #include "opt_kgdb.h"
45 #include "fb.h"
46 
47 #include <sys/param.h>
48 #include <sys/proc.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.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 #include <sys/kauth.h>
57 
58 #include <machine/bsd_openprom.h>
59 #include <machine/promlib.h>
60 #include <machine/eeprom.h>
61 #include <machine/psl.h>
62 #include <machine/cpu.h>
63 #include <machine/kbd.h>
64 #include <machine/autoconf.h>
65 
66 #if defined(RASTERCONSOLE) && NFB > 0
67 #include <dev/sun/fbio.h>
68 #include <dev/sun/fbvar.h>
69 #endif
70 
71 #include <dev/cons.h>
72 #include <sparc/dev/cons.h>
73 
74 #include <dev/sun/event_var.h>
75 #include <dev/sun/kbd_xlate.h>
76 #include <dev/sun/kbdvar.h>
77 
78 #define PUT_WSIZE	64
79 
80 struct kd_softc {
81 	struct  tty *kd_tty;
82 	int rows, cols;
83 
84 	/* Console input hook */
85 	struct cons_channel *kd_in;
86 };
87 
88 /*
89  * There is no point in pretending there might be
90  * more than one keyboard/display device.
91  */
92 static struct kd_softc kd_softc;
93 
94 /* For keyboard driver to register itself as console input */
95 void kd_attach_input(struct cons_channel *);
96 
97 static void kd_init(struct kd_softc *);
98 static void kdstart(struct tty *);
99 static void kd_later(void *);
100 static void kd_putfb(struct tty *);
101 static int kdparam(struct tty *, struct termios *);
102 static void kd_cons_input(int);
103 
104 dev_type_open(kdopen);
105 dev_type_close(kdclose);
106 dev_type_read(kdread);
107 dev_type_write(kdwrite);
108 dev_type_ioctl(kdioctl);
109 dev_type_tty(kdtty);
110 dev_type_poll(kdpoll);
111 
112 const struct cdevsw kd_cdevsw = {
113 	kdopen, kdclose, kdread, kdwrite, kdioctl,
114 	nostop, kdtty, kdpoll, nommap, ttykqfilter, D_TTY
115 };
116 
117 /*
118  * Prepare the console tty; called on first open of /dev/console
119  */
120 static void
121 kd_init(struct kd_softc *kd)
122 {
123 	struct tty *tp;
124 
125 	tp = tty_alloc();
126 	callout_setfunc(&tp->t_rstrt_ch, kd_later, tp);
127 	tp->t_oproc = kdstart;
128 	tp->t_param = kdparam;
129 	tp->t_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
130 
131 	tty_attach(tp);
132 	kd->kd_tty = tp;
133 
134 	/*
135 	 * Get the console struct winsize.
136 	 */
137 #if defined(RASTERCONSOLE) && NFB > 0
138 	/* If the raster console driver is attached, copy its size */
139 	kd->rows = fbrcons_rows();
140 	kd->cols = fbrcons_cols();
141 	rcons_ttyinit(tp);
142 #endif
143 
144 	/* else, consult the PROM */
145 	switch (prom_version()) {
146 	char prop[6+1];		/* Enough for six digits */
147 	struct eeprom *ep;
148 	case PROM_OLDMON:
149 		if ((ep = (struct eeprom *)eeprom_va) == NULL)
150 			break;
151 		if (kd->rows == 0)
152 			kd->rows = (u_short)ep->eeTtyRows;
153 		if (kd->cols == 0)
154 			kd->cols = (u_short)ep->eeTtyCols;
155 		break;
156 
157 	case PROM_OBP_V0:
158 	case PROM_OBP_V2:
159 	case PROM_OBP_V3:
160 	case PROM_OPENFIRM:
161 		if (kd->rows == 0 &&
162 		    prom_getoption("screen-#rows", prop, sizeof prop) == 0)
163 			kd->rows = strtoul(prop, NULL, 10);
164 
165 		if (kd->cols == 0 &&
166 		    prom_getoption("screen-#columns", prop, sizeof prop) == 0)
167 			kd->cols = strtoul(prop, NULL, 10);
168 
169 		break;
170 	}
171 
172 	return;
173 }
174 
175 struct tty *
176 kdtty(dev_t dev)
177 {
178 	struct kd_softc *kd;
179 
180 	kd = &kd_softc; 	/* XXX */
181 	return (kd->kd_tty);
182 }
183 
184 int
185 kdopen(dev_t dev, int flag, int mode, struct lwp *l)
186 {
187 	struct kd_softc *kd;
188 	int error, s, unit;
189 	struct tty *tp;
190 static	int firstopen = 1;
191 
192 	unit = minor(dev);
193 	if (unit != 0)
194 		return ENXIO;
195 
196 	kd = &kd_softc; 	/* XXX */
197 	if (firstopen) {
198 		kd_init(kd);
199 		firstopen = 0;
200 	}
201 
202 	tp = kd->kd_tty;
203 
204 	/* It's simpler to do this up here. */
205 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
206 		return (EBUSY);
207 
208 	s = spltty();
209 	if ((tp->t_state & TS_ISOPEN) == 0) {
210 		/* First open. */
211 
212 		/* Notify the input device that serves us */
213 		struct cons_channel *cc = kd->kd_in;
214 		if (cc != NULL &&
215 		    (error = (*cc->cc_iopen)(cc)) != 0) {
216 			return (error);
217 		}
218 
219 		ttychars(tp);
220 		tp->t_iflag = TTYDEF_IFLAG;
221 		tp->t_oflag = TTYDEF_OFLAG;
222 		tp->t_cflag = TTYDEF_CFLAG;
223 		tp->t_lflag = TTYDEF_LFLAG;
224 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
225 		(void) kdparam(tp, &tp->t_termios);
226 		ttsetwater(tp);
227 		tp->t_winsize.ws_row = kd->rows;
228 		tp->t_winsize.ws_col = kd->cols;
229 		/* Flush pending input?  Clear translator? */
230 		/* This (pseudo)device always has SOFTCAR */
231 		tp->t_state |= TS_CARR_ON;
232 	}
233 
234 	splx(s);
235 
236 	return ((*tp->t_linesw->l_open)(dev, tp));
237 }
238 
239 int
240 kdclose(dev_t dev, int flag, int mode, struct lwp *l)
241 {
242 	struct kd_softc *kd;
243 	struct tty *tp;
244 	struct cons_channel *cc;
245 
246 	kd = &kd_softc; 	/* XXX */
247 	tp = kd->kd_tty;
248 
249 	/* XXX This is for cons.c. */
250 	if ((tp->t_state & TS_ISOPEN) == 0)
251 		return 0;
252 
253 	(*tp->t_linesw->l_close)(tp, flag);
254 	ttyclose(tp);
255 
256 	if ((cc = kd->kd_in) != NULL)
257 		(void)(*cc->cc_iclose)(cc);
258 
259 	return (0);
260 }
261 
262 int
263 kdread(dev_t dev, struct uio *uio, int flag)
264 {
265 	struct kd_softc *kd;
266 	struct tty *tp;
267 
268 	kd = &kd_softc; 	/* XXX */
269 	tp = kd->kd_tty;
270 
271 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
272 }
273 
274 int
275 kdwrite(dev_t dev, struct uio *uio, int flag)
276 {
277 	struct kd_softc *kd;
278 	struct tty *tp;
279 
280 	kd = &kd_softc; 	/* XXX */
281 	tp = kd->kd_tty;
282 
283 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
284 }
285 
286 int
287 kdpoll(dev_t dev, int events, struct lwp *l)
288 {
289 	struct kd_softc *kd;
290 	struct tty *tp;
291 
292 	kd = &kd_softc; 	/* XXX */
293 	tp = kd->kd_tty;
294 
295 	return ((*tp->t_linesw->l_poll)(tp, events, l));
296 }
297 
298 int
299 kdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
300 {
301 	struct kd_softc *kd;
302 	struct tty *tp;
303 	int error;
304 
305 	kd = &kd_softc; 	/* XXX */
306 	tp = kd->kd_tty;
307 
308 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
309 	if (error != EPASSTHROUGH)
310 		return error;
311 
312 	error = ttioctl(tp, cmd, data, flag, l);
313 	if (error != EPASSTHROUGH)
314 		return error;
315 
316 	/* Handle any ioctl commands specific to kbd/display. */
317 	/* XXX - Send KB* ioctls to kbd module? */
318 	/* XXX - Send FB* ioctls to fb module?  */
319 
320 	return EPASSTHROUGH;
321 }
322 
323 static int
324 kdparam(struct tty *tp, struct termios *t)
325 {
326 
327 	/* XXX - These are ignored... */
328 	tp->t_ispeed = t->c_ispeed;
329 	tp->t_ospeed = t->c_ospeed;
330 	tp->t_cflag = t->c_cflag;
331 	return 0;
332 }
333 
334 static void
335 kdstart(struct tty *tp)
336 {
337 	int s1, s2;
338 
339 	s1 = splsoftclock();
340 	s2 = spltty();
341 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
342 		goto out;
343 
344 	if (ttypull(tp)) {
345 		tp->t_state |= TS_BUSY;
346 		if ((s1 & PSR_PIL) == 0) {
347 			/* called at level zero - update screen now. */
348 			splx(s2);
349 			kd_putfb(tp);
350 			s2 = spltty();
351 			tp->t_state &= ~TS_BUSY;
352 		} else {
353 			/* called at interrupt level - do it later */
354 			callout_schedule(&tp->t_rstrt_ch, 0);
355 		}
356 	}
357 out:
358 	splx(s2);
359 	splx(s1);
360 }
361 
362 /*
363  * Timeout function to do delayed writes to the screen.
364  * Called at splsoftclock when requested by kdstart.
365  */
366 static void
367 kd_later(void *arg)
368 {
369 	struct tty *tp = arg;
370 	int s;
371 
372 	kd_putfb(tp);
373 
374 	s = spltty();
375 	tp->t_state &= ~TS_BUSY;
376 	(*tp->t_linesw->l_start)(tp);
377 	splx(s);
378 }
379 
380 /*
381  * Put text on the screen using the PROM monitor.
382  * This can take a while, so to avoid missing
383  * interrupts, this is called at splsoftclock.
384  */
385 static void
386 kd_putfb(struct tty *tp)
387 {
388 	char buf[PUT_WSIZE];
389 	struct clist *cl = &tp->t_outq;
390 	char *p, *end;
391 	int len;
392 
393 	while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
394 		/* PROM will barf if high bits are set. */
395 		p = buf;
396 		end = buf + len;
397 		while (p < end)
398 			*p++ &= 0x7f;
399 
400 		/* Now let the PROM print it. */
401 		prom_putstr(buf, len);
402 	}
403 }
404 
405 /*
406  * Our "interrupt" routine for input. This is called by
407  * the keyboard driver (dev/sun/kbd.c) at spltty.
408  */
409 static void
410 kd_cons_input(int c)
411 {
412 	struct kd_softc *kd = &kd_softc;
413 	struct tty *tp;
414 
415 	/* XXX: Make sure the device is open. */
416 	tp = kd->kd_tty;
417 	if (tp == NULL)
418 		return;
419 	if ((tp->t_state & TS_ISOPEN) == 0)
420 		return;
421 
422 	(*tp->t_linesw->l_rint)(c, tp);
423 }
424 
425 void
426 cons_attach_input(struct cons_channel *cc, struct consdev *cn)
427 {
428 	struct kd_softc *kd = &kd_softc;
429 
430 	kd->kd_in = cc;
431 	cc->cc_upstream = kd_cons_input;
432 }
433 
434 void
435 kd_attach_input(struct cons_channel *cc)
436 {
437 	struct kd_softc *kd = &kd_softc;
438 
439 	kd->kd_in = cc;
440 	cc->cc_upstream = kd_cons_input;
441 }
442 
443 /*
444  * Default PROM-based console input stream
445  * Since the PROM does not notify us when data is available on the
446  * input channel these functions periodically poll the PROM.
447  */
448 static int kd_rom_iopen(struct cons_channel *);
449 static int kd_rom_iclose(struct cons_channel *);
450 static void kd_rom_intr(void *);
451 
452 static struct cons_channel prom_cons_channel = {
453 	NULL,			/* no private data */
454 	kd_rom_iopen,
455 	kd_rom_iclose,
456 	NULL			/* will be set by kd driver */
457 };
458 
459 static struct callout prom_cons_callout;
460 
461 static int
462 kd_rom_iopen(struct cons_channel *cc)
463 {
464 	static bool callo;
465 
466 	if (!callo) {
467 		callout_init(&prom_cons_callout, 0);
468 		callo = true;
469 	}
470 
471 	/* Poll for ROM input 4 times per second */
472 	callout_reset(&prom_cons_callout, hz / 4, kd_rom_intr, cc);
473 	return (0);
474 }
475 
476 static int
477 kd_rom_iclose(struct cons_channel *cc)
478 {
479 
480 	callout_stop(&prom_cons_callout);
481 	return (0);
482 }
483 
484 /*
485  * "Interrupt" routine for input through ROM vectors
486  */
487 static void
488 kd_rom_intr(void *arg)
489 {
490 	struct cons_channel *cc = arg;
491 	int s, c;
492 
493 	callout_schedule(&prom_cons_callout, hz / 4);
494 
495 	s = spltty();
496 
497 	while ((c = prom_peekchar()) >= 0)
498 		(*cc->cc_upstream)(c);
499 
500 	splx(s);
501 }
502 
503 /*****************************************************************/
504 
505 int prom_stdin_node;
506 int prom_stdout_node;
507 char prom_stdin_args[16];
508 char prom_stdout_args[16];
509 
510 static void prom_cnprobe(struct consdev *);
511 static void prom_cninit(struct consdev *);
512 int  prom_cngetc(dev_t);	/* XXX: for sunkbd_wskbd_cngetc */
513 static void prom_cnputc(dev_t, int);
514 static void prom_cnpollc(dev_t, int);
515 
516 /*
517  * The console is set to this one initially,
518  * which lets us use the PROM until consinit()
519  * is called to select a real console.
520  */
521 struct consdev consdev_prom = {
522 	prom_cnprobe,
523 	prom_cninit,
524 	prom_cngetc,
525 	prom_cnputc,
526 	prom_cnpollc,
527 	NULL,
528 };
529 
530 /*
531  * The console table pointer is statically initialized
532  * to point to the PROM table, so that early calls to printf will work.
533  * this has been moved to cpu_startup()
534  */
535 #if 0
536 struct consdev *cn_tab = &consdev_prom;
537 #endif
538 
539 static void
540 prom_cnprobe(struct consdev *cn)
541 {
542 }
543 
544 static void
545 prom_cninit(struct consdev *cn)
546 {
547 }
548 
549 static void
550 prom_cnpollc(dev_t dev, int on)
551 {
552 
553 	if (on) {
554 		/* Entering debugger. */
555 #if NFB > 0
556 		fb_unblank();
557 #endif
558 	} else {
559 		/* Resuming kernel. */
560 	}
561 }
562 
563 
564 /*
565  * PROM console input putchar.
566  */
567 int
568 prom_cngetc(dev_t dev)
569 {
570 	int s, c;
571 
572 	s = splhigh();
573 	c = prom_getchar();
574 	splx(s);
575 	return (c);
576 }
577 
578 /*
579  * PROM console output putchar.
580  */
581 static void
582 prom_cnputc(dev_t dev, int c)
583 {
584 
585 	prom_putchar(c);
586 }
587 
588 
589 /*****************************************************************/
590 
591 static void prom_get_device_args(const char *, char *, unsigned int);
592 
593 static void
594 prom_get_device_args(const char *prop, char *args, unsigned int sz)
595 {
596 	const char *cp;
597 	char buffer[128];
598 
599 	cp = prom_getpropstringA(findroot(), prop, buffer, sizeof buffer);
600 
601 	/*
602 	 * Extract device-specific arguments from a PROM device path (if any)
603 	 */
604 	cp = buffer + strlen(buffer);
605 	while (cp >= buffer) {
606 		if (*cp == ':') {
607 			strncpy(args, cp+1, sz);
608 			break;
609 		}
610 		cp--;
611 	}
612 }
613 
614 /*
615  *
616  */
617 void
618 consinit(void)
619 {
620 #if 0
621 	int inSource, outSink;
622 #endif
623 
624 	switch (prom_version()) {
625 #if 0
626 	case PROM_OLDMON:
627 	case PROM_OBP_V0:
628 		/* The stdio handles identify the device type */
629 		inSource = prom_stdin();
630 		outSink  = prom_stdout();
631 		break;
632 	// XXXMRG  should these just set prom_stdin_node / prom_stdout_node?
633 #endif
634 
635 	case PROM_OBP_V2:
636 	case PROM_OBP_V3:
637 	case PROM_OPENFIRM:
638 
639 		/* Save PROM arguments for device matching */
640 		prom_get_device_args("stdin-path", prom_stdin_args,
641 				     sizeof(prom_stdin_args));
642 		prom_get_device_args("stdout-path", prom_stdout_args,
643 				    sizeof(prom_stdout_args));
644 
645 		/*
646 		 * Translate the STDIO package instance (`ihandle') -- that
647 		 * the PROM has already opened for us -- to a device tree
648 		 * node (i.e. a `phandle').
649 		 */
650 
651 		prom_stdin_node = prom_instance_to_package(prom_stdin());
652 		if (prom_stdin_node == 0)
653 			printf("consinit: cannot convert stdin ihandle\n");
654 
655 		prom_stdout_node = prom_instance_to_package(prom_stdout());
656 		if (prom_stdout_node == 0) {
657 			printf("consinit: cannot convert stdout ihandle\n");
658 			break;
659 		}
660 
661 		break;
662 
663 	default:
664 		break;
665 	}
666 
667 	/* Wire up /dev/console */
668 	cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
669 	cn_tab->cn_pri = CN_INTERNAL;
670 
671 	/* Set up initial PROM input channel for /dev/console */
672 	cons_attach_input(&prom_cons_channel, cn_tab);
673 
674 #ifdef	KGDB
675 	zs_kgdb_init();	/* XXX */
676 #endif
677 }
678