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