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