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