kd.c revision 1.7 1 /* $NetBSD: kd.c,v 1.7 2003/08/24 17:32:04 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 * 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.7 2003/08/24 17:32:04 uwe Exp $");
50
51 #include "opt_rcons.h"
52
53 #include <sys/param.h>
54 #include <sys/proc.h>
55 #include <sys/systm.h>
56 #include <sys/ioctl.h>
57 #include <sys/tty.h>
58 #include <sys/file.h>
59 #include <sys/conf.h>
60 #include <sys/device.h>
61
62 #include <machine/promlib.h>
63 #include <machine/eeprom.h>
64 #include <machine/psl.h>
65 #include <machine/cpu.h>
66 #include <machine/kbd.h>
67 #include <machine/autoconf.h>
68
69 #ifdef RASTERCONSOLE
70 #include <dev/sun/fbio.h>
71 #include <machine/fbvar.h>
72 #endif
73
74
75 #include <dev/cons.h>
76 #include <dev/sun/event_var.h>
77 #include <dev/sun/kbd_xlate.h>
78 #include <dev/sun/kbdvar.h>
79 #include <sun2/dev/cons.h>
80
81 struct tty *fbconstty = 0; /* tty structure for frame buffer console */
82
83 #define PUT_WSIZE 64
84
85 struct kd_softc {
86 struct device kd_dev; /* required first: base device */
87 struct tty *kd_tty;
88 int rows, cols;
89
90 /* Console input hook */
91 struct cons_channel *kd_in;
92 };
93
94 /*
95 * There is no point in pretending there might be
96 * more than one keyboard/display device.
97 */
98 static struct kd_softc kd_softc;
99 static int kd_is_console;
100
101 static int kdparam(struct tty *, struct termios *);
102 static void kdstart(struct tty *);
103 static void kd_init __P((struct kd_softc *));
104 static void kd_cons_input __P((int));
105 static int kdcngetc __P((dev_t));
106
107 dev_type_open(kdopen);
108 dev_type_close(kdclose);
109 dev_type_read(kdread);
110 dev_type_write(kdwrite);
111 dev_type_ioctl(kdioctl);
112 dev_type_tty(kdtty);
113 dev_type_poll(kdpoll);
114
115 const struct cdevsw kd_cdevsw = {
116 kdopen, kdclose, kdread, kdwrite, kdioctl,
117 nostop, kdtty, kdpoll, nommap, ttykqfilter, D_TTY
118 };
119
120 /*
121 * This is called by kbd_attach()
122 * XXX - Make this a proper child of kbd?
123 */
124 void
125 kd_init(kd)
126 struct kd_softc *kd;
127 {
128 struct tty *tp;
129 #ifdef PROM_OLDMON
130 struct eeprom *ep;
131 #endif
132 #ifdef notyet /* PROM_OBP_V2 */
133 int i;
134 char *prop;
135 #endif
136
137 kd = &kd_softc; /* XXX */
138
139 tp = ttymalloc();
140 tp->t_oproc = kdstart;
141 tp->t_param = kdparam;
142 tp->t_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
143
144 tty_attach(tp);
145 kd->kd_tty = tp;
146
147 /*
148 * get the console struct winsize.
149 */
150 if (kd_is_console) {
151 fbconstty = tp;
152 #ifdef RASTERCONSOLE
153 kd->rows = fbrcons_rows();
154 kd->cols = fbrcons_cols();
155 rcons_ttyinit(tp);
156 #endif
157 }
158
159 /* else, consult the PROM */
160 switch (prom_version()) {
161 #ifdef PROM_OLDMON
162 case PROM_OLDMON:
163 if ((ep = (struct eeprom *)eeprom_va) == NULL)
164 break;
165 if (kd->rows == 0)
166 kd->rows = (u_short)ep->eeTtyRows;
167 if (kd->cols == 0)
168 kd->cols = (u_short)ep->eeTtyCols;
169 break;
170 #endif /* PROM_OLDMON */
171 #ifdef notyet /* PROM_OBP_V2 */
172 case PROM_OBP_V0:
173 case PROM_OBP_V2:
174 case PROM_OBP_V3:
175 case PROM_OPENFIRM:
176
177 if (kd->rows == 0 &&
178 (prop = PROM_getpropstring(optionsnode, "screen-#rows"))) {
179 i = 0;
180 while (*prop != '\0')
181 i = i * 10 + *prop++ - '0';
182 kd->rows = (unsigned short)i;
183 }
184 if (kd->cols == 0 &&
185 (prop = PROM_getpropstring(optionsnode, "screen-#columns"))) {
186 i = 0;
187 while (*prop != '\0')
188 i = i * 10 + *prop++ - '0';
189 kd->cols = (unsigned short)i;
190 }
191 break;
192 #endif /* PROM_OBP_V2 */
193 }
194 return;
195 }
196
197 struct tty *
198 kdtty(dev)
199 dev_t dev;
200 {
201 struct kd_softc *kd;
202
203 kd = &kd_softc; /* XXX */
204 return (kd->kd_tty);
205 }
206
207 int
208 kdopen(dev, flag, mode, p)
209 dev_t dev;
210 int flag, mode;
211 struct proc *p;
212 {
213 struct kd_softc *kd;
214 int error, s, unit;
215 struct tty *tp;
216 static int firstopen = 1;
217
218 unit = minor(dev);
219 if (unit != 0)
220 return ENXIO;
221 kd = &kd_softc; /* XXX */
222
223 if (firstopen) {
224 kd_init(kd);
225 firstopen = 0;
226 }
227 tp = kd->kd_tty;
228
229 /* It's simpler to do this up here. */
230 if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE))
231 == (TS_ISOPEN | TS_XCLUDE))
232 && (p->p_ucred->cr_uid != 0) )
233 {
234 return (EBUSY);
235 }
236
237 s = spltty();
238
239 if ((tp->t_state & TS_ISOPEN) == 0) {
240 /* First open. */
241
242 /* Notify the input device that serves us */
243 struct cons_channel *cc = kd->kd_in;
244 if (cc != NULL &&
245 (error = (*cc->cc_iopen)(cc)) != 0) {
246 splx(s);
247 return (error);
248 }
249
250 ttychars(tp);
251 tp->t_iflag = TTYDEF_IFLAG;
252 tp->t_oflag = TTYDEF_OFLAG;
253 tp->t_cflag = TTYDEF_CFLAG;
254 tp->t_lflag = TTYDEF_LFLAG;
255 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
256 (void) kdparam(tp, &tp->t_termios);
257 ttsetwater(tp);
258 tp->t_winsize.ws_row = kd->rows;
259 tp->t_winsize.ws_col = kd->cols;
260 /* Flush pending input? Clear translator? */
261 /* This (pseudo)device always has SOFTCAR */
262 tp->t_state |= TS_CARR_ON;
263 }
264
265 splx(s);
266
267 return ((*tp->t_linesw->l_open)(dev, tp));
268 }
269
270 int
271 kdclose(dev, flag, mode, p)
272 dev_t dev;
273 int flag, mode;
274 struct proc *p;
275 {
276 struct kd_softc *kd;
277 struct tty *tp;
278 struct cons_channel *cc;
279
280 kd = &kd_softc; /* XXX */
281 tp = kd->kd_tty;
282
283 /* XXX This is for cons.c. */
284 if ((tp->t_state & TS_ISOPEN) == 0)
285 return 0;
286
287 (*tp->t_linesw->l_close)(tp, flag);
288 ttyclose(tp);
289
290 if ((cc = kd->kd_in) != NULL)
291 (void)(*cc->cc_iclose)(cc);
292
293 return (0);
294 }
295
296 int
297 kdread(dev, uio, flag)
298 dev_t dev;
299 struct uio *uio;
300 int flag;
301 {
302 struct kd_softc *kd;
303 struct tty *tp;
304
305 kd = &kd_softc; /* XXX */
306 tp = kd->kd_tty;
307
308 return ((*tp->t_linesw->l_read)(tp, uio, flag));
309 }
310
311 int
312 kdwrite(dev, uio, flag)
313 dev_t dev;
314 struct uio *uio;
315 int flag;
316 {
317 struct kd_softc *kd;
318 struct tty *tp;
319
320 kd = &kd_softc; /* XXX */
321 tp = kd->kd_tty;
322
323 return ((*tp->t_linesw->l_write)(tp, uio, flag));
324 }
325
326 int
327 kdpoll(dev, events, p)
328 dev_t dev;
329 int events;
330 struct proc *p;
331 {
332 struct kd_softc *kd;
333 struct tty *tp;
334
335 kd = &kd_softc; /* XXX */
336 tp = kd->kd_tty;
337
338 return ((*tp->t_linesw->l_poll)(tp, events, p));
339 }
340
341 int
342 kdioctl(dev, cmd, data, flag, p)
343 dev_t dev;
344 u_long cmd;
345 caddr_t data;
346 int flag;
347 struct proc *p;
348 {
349 struct kd_softc *kd;
350 struct tty *tp;
351 int error;
352
353 kd = &kd_softc; /* XXX */
354 tp = kd->kd_tty;
355
356 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
357 if (error != EPASSTHROUGH)
358 return error;
359
360 error = ttioctl(tp, cmd, data, flag, p);
361 if (error != EPASSTHROUGH)
362 return error;
363
364 /* Handle any ioctl commands specific to kbd/display. */
365 /* XXX - Send KB* ioctls to kbd module? */
366 /* XXX - Send FB* ioctls to fb module? */
367
368 return EPASSTHROUGH;
369 }
370
371 static int
372 kdparam(tp, t)
373 struct tty *tp;
374 struct termios *t;
375 {
376 /* XXX - These are ignored... */
377 tp->t_ispeed = t->c_ispeed;
378 tp->t_ospeed = t->c_ospeed;
379 tp->t_cflag = t->c_cflag;
380 return 0;
381 }
382
383
384 static void kd_later(void*);
385 static void kd_putfb(struct tty *);
386
387 static void
388 kdstart(tp)
389 struct tty *tp;
390 {
391 struct clist *cl;
392 register int s;
393
394 s = spltty();
395 if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
396 goto out;
397
398 cl = &tp->t_outq;
399 if (cl->c_cc) {
400 if (kd_is_console) {
401 tp->t_state |= TS_BUSY;
402 if (is_spl0(s)) {
403 /* called at level zero - update screen now. */
404 (void) spllowersoftclock();
405 kd_putfb(tp);
406 (void) spltty();
407 tp->t_state &= ~TS_BUSY;
408 } else {
409 /* called at interrupt level - do it later */
410 callout_reset(&tp->t_rstrt_ch, 0,
411 kd_later, tp);
412 }
413 } else {
414 /*
415 * This driver uses the PROM for writing the screen,
416 * and that only works if this is the console device.
417 * If this is not the console, just flush the output.
418 * Sorry. (In that case, use xdm instead of getty.)
419 */
420 ndflush(cl, cl->c_cc);
421 }
422 }
423 if (cl->c_cc <= tp->t_lowat) {
424 if (tp->t_state & TS_ASLEEP) {
425 tp->t_state &= ~TS_ASLEEP;
426 wakeup((caddr_t)cl);
427 }
428 selwakeup(&tp->t_wsel);
429 }
430 out:
431 splx(s);
432 }
433
434 /*
435 * Timeout function to do delayed writes to the screen.
436 * Called at splsoftclock when requested by kdstart.
437 */
438 static void
439 kd_later(tpaddr)
440 void *tpaddr;
441 {
442 struct tty *tp = tpaddr;
443 register int s;
444
445 kd_putfb(tp);
446
447 s = spltty();
448 tp->t_state &= ~TS_BUSY;
449 (*tp->t_linesw->l_start)(tp);
450 splx(s);
451 }
452
453 /*
454 * Put text on the screen using the PROM monitor.
455 * This can take a while, so to avoid missing
456 * interrupts, this is called at splsoftclock.
457 */
458 static void
459 kd_putfb(tp)
460 struct tty *tp;
461 {
462 char buf[PUT_WSIZE];
463 struct clist *cl = &tp->t_outq;
464 char *p, *end;
465 int len;
466
467 while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
468 /* PROM will barf if high bits are set. */
469 p = buf;
470 end = buf + len;
471 while (p < end)
472 *p++ &= 0x7f;
473 /* Now let the PROM print it. */
474 prom_putstr(buf, len);
475 }
476 }
477
478 /*
479 * Default PROM-based console input stream
480 */
481 static int kd_rom_iopen __P((struct cons_channel *));
482 static int kd_rom_iclose __P((struct cons_channel *));
483
484 static struct cons_channel prom_cons_channel;
485
486 int
487 kd_rom_iopen(cc)
488 struct cons_channel *cc;
489 {
490 return (0);
491 }
492
493 int
494 kd_rom_iclose(cc)
495 struct cons_channel *cc;
496 {
497 return (0);
498 }
499
500 /*
501 * Our "interrupt" routine for input. This is called by
502 * the keyboard driver (dev/sun/kbd.c) at spltty.
503 */
504 void
505 kd_cons_input(c)
506 int c;
507 {
508 struct kd_softc *kd = &kd_softc;
509 struct tty *tp;
510
511 /* XXX: Make sure the device is open. */
512 tp = kd->kd_tty;
513 if (tp == NULL)
514 return;
515 if ((tp->t_state & TS_ISOPEN) == 0)
516 return;
517
518 (*tp->t_linesw->l_rint)(c, tp);
519 }
520
521
522 /****************************************************************
523 * kd console support
524 ****************************************************************/
525
526 /* The debugger gets its own key translation state. */
527 static struct kbd_state *kdcn_state;
528
529 static void kdcnprobe __P((struct consdev *));
530 static void kdcninit __P((struct consdev *));
531 static void kdcnputc __P((dev_t, int));
532 static void kdcnpollc __P((dev_t, int));
533
534 /* The keyboard driver uses cn_hw to access the real console driver */
535 extern struct consdev consdev_prom;
536 struct consdev consdev_kd = {
537 kdcnprobe,
538 kdcninit,
539 kdcngetc,
540 kdcnputc,
541 kdcnpollc,
542 NULL,
543 };
544 struct consdev *cn_hw = &consdev_kd;
545
546 void
547 cons_attach_input(cc, cn)
548 struct cons_channel *cc;
549 struct consdev *cn;
550 {
551 struct kd_softc *kd = &kd_softc;
552 struct kbd_softc *kds = cc->cc_dev;
553 struct kbd_state *ks;
554
555 /* Share the keyboard state */
556 kdcn_state = ks = &kds->k_state;
557
558 kd->kd_in = cc;
559 cc->cc_upstream = kd_cons_input;
560
561 /* Attach lower level. */
562 cn_hw->cn_dev = cn->cn_dev;
563 cn_hw->cn_pollc = cn->cn_pollc;
564 cn_hw->cn_getc = cn->cn_getc;
565
566 /* Attach us as console. */
567 cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
568 cn_tab->cn_probe = kdcnprobe;
569 cn_tab->cn_init = kdcninit;
570 cn_tab->cn_getc = kdcngetc;
571 cn_tab->cn_pollc = kdcnpollc;
572 cn_tab->cn_pri = CN_INTERNAL;
573
574 /* Set up initial PROM input channel for /dev/console */
575 prom_cons_channel.cc_dev = NULL;
576 prom_cons_channel.cc_iopen = kd_rom_iopen;
577 prom_cons_channel.cc_iclose = kd_rom_iclose;
578
579 /* Indicate that it is OK to use the PROM fbwrite */
580 kd_is_console = 1;
581 }
582
583
584 void kd_attach_input(struct cons_channel *);
585 void
586 kd_attach_input(cc)
587 struct cons_channel *cc;
588 {
589 struct kd_softc *kd = &kd_softc;
590
591 kd->kd_in = cc;
592 cc->cc_upstream = kd_cons_input;
593 }
594
595
596 /* We never call this. */
597 static void
598 kdcnprobe(cn)
599 struct consdev *cn;
600 {
601 }
602
603 static void
604 kdcninit(cn)
605 struct consdev *cn;
606 {
607 #if 0
608 struct kbd_state *ks = kdcn_state;
609
610 cn->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
611 cn->cn_pri = CN_INTERNAL;
612
613 /* This prepares kbd_translate() */
614 ks->kbd_id = KBD_MIN_TYPE;
615 kbd_xlate_init(ks);
616
617 /* Set up initial PROM input channel for /dev/console */
618 prom_cons_channel.cc_dev = NULL;
619 prom_cons_channel.cc_iopen = kd_rom_iopen;
620 prom_cons_channel.cc_iclose = kd_rom_iclose;
621 cons_attach_input(&prom_cons_channel);
622
623 /* Indicate that it is OK to use the PROM fbwrite */
624 kd_is_console = 1;
625 #endif
626 }
627
628 static int
629 kdcngetc(dev)
630 dev_t dev;
631 {
632 struct kbd_state *ks = kdcn_state;
633 int code, class, data, keysym;
634 extern int prom_cngetc __P((dev_t));
635
636
637 if (cn_hw->cn_getc == prom_cngetc) return (*cn_hw->cn_getc)(dev);
638 for (;;) {
639 code = (*cn_hw->cn_getc)(dev);
640 keysym = kbd_code_to_keysym(ks, code);
641 class = KEYSYM_CLASS(keysym);
642
643 switch (class) {
644 case KEYSYM_ASCII:
645 goto out;
646
647 case KEYSYM_CLRMOD:
648 case KEYSYM_SETMOD:
649 data = (keysym & 0x1F);
650 /* Only allow ctrl or shift. */
651 if (data > KBMOD_SHIFT_R)
652 break;
653 data = 1 << data;
654 if (class == KEYSYM_SETMOD)
655 ks->kbd_modbits |= data;
656 else
657 ks->kbd_modbits &= ~data;
658 break;
659
660 case KEYSYM_ALL_UP:
661 /* No toggle keys here. */
662 ks->kbd_modbits = 0;
663 break;
664
665 default: /* ignore all other keysyms */
666 break;
667 }
668 }
669 out:
670 return (keysym);
671 }
672
673 static void
674 kdcnputc(dev, c)
675 dev_t dev;
676 int c;
677 {
678 int s;
679
680 s = splhigh();
681 prom_putchar(c);
682 splx(s);
683 }
684
685 static void
686 kdcnpollc(dev, on)
687 dev_t dev;
688 int on;
689 {
690 struct kbd_state *ks = kdcn_state;
691
692 if (on) {
693 /* Entering debugger. */
694 #if NFB > 0
695 fb_unblank();
696 #endif
697 /* Clear shift keys too. */
698 ks->kbd_modbits = 0;
699 } else {
700 /* Resuming kernel. */
701 }
702 (*cn_hw->cn_pollc)(dev, on);
703 }
704