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