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