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