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