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