kd.c revision 1.9.8.1 1 /* $NetBSD: kd.c,v 1.9.8.1 2006/06/21 14:57:05 yamt 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.9.8.1 2006/06/21 14:57:05 yamt 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 #include <sys/kauth.h>
60
61 #include <machine/promlib.h>
62 #include <machine/eeprom.h>
63 #include <machine/psl.h>
64 #include <machine/cpu.h>
65 #include <machine/kbd.h>
66 #include <machine/autoconf.h>
67
68 #ifdef RASTERCONSOLE
69 #include <dev/sun/fbio.h>
70 #include <machine/fbvar.h>
71 #endif
72
73
74 #include <dev/cons.h>
75 #include <dev/sun/event_var.h>
76 #include <dev/sun/kbd_xlate.h>
77 #include <dev/sun/kbdvar.h>
78 #include <sun2/dev/cons.h>
79
80 struct tty *fbconstty = 0; /* tty structure for frame buffer console */
81
82 #define PUT_WSIZE 64
83
84 struct kd_softc {
85 struct device kd_dev; /* required first: base device */
86 struct tty *kd_tty;
87 int rows, cols;
88
89 /* Console input hook */
90 struct cons_channel *kd_in;
91 };
92
93 /*
94 * There is no point in pretending there might be
95 * more than one keyboard/display device.
96 */
97 static struct kd_softc kd_softc;
98 static int kd_is_console;
99
100 static int kdparam(struct tty *, struct termios *);
101 static void kdstart(struct tty *);
102 static void kd_init(struct kd_softc *);
103 static void kd_cons_input(int);
104 static int kdcngetc(dev_t);
105
106 dev_type_open(kdopen);
107 dev_type_close(kdclose);
108 dev_type_read(kdread);
109 dev_type_write(kdwrite);
110 dev_type_ioctl(kdioctl);
111 dev_type_tty(kdtty);
112 dev_type_poll(kdpoll);
113
114 const struct cdevsw kd_cdevsw = {
115 kdopen, kdclose, kdread, kdwrite, kdioctl,
116 nostop, kdtty, kdpoll, nommap, ttykqfilter, D_TTY
117 };
118
119 /*
120 * This is called by kbd_attach()
121 * XXX - Make this a proper child of kbd?
122 */
123 void
124 kd_init(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_t dev)
197 {
198 struct kd_softc *kd;
199
200 kd = &kd_softc; /* XXX */
201 return (kd->kd_tty);
202 }
203
204 int
205 kdopen(dev_t dev, int flag, int mode, struct lwp *l)
206 {
207 struct kd_softc *kd;
208 int error, s, unit;
209 struct tty *tp;
210 static int firstopen = 1;
211
212 unit = minor(dev);
213 if (unit != 0)
214 return ENXIO;
215 kd = &kd_softc; /* XXX */
216
217 if (firstopen) {
218 kd_init(kd);
219 firstopen = 0;
220 }
221 tp = kd->kd_tty;
222
223 /* It's simpler to do this up here. */
224 if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE))
225 == (TS_ISOPEN | TS_XCLUDE))
226 && (kauth_authorize_generic(l->l_proc->p_cred, KAUTH_GENERIC_ISSUSER, &l->l_proc->p_acflag) != 0) )
227 {
228 return (EBUSY);
229 }
230
231 s = spltty();
232
233 if ((tp->t_state & TS_ISOPEN) == 0) {
234 /* First open. */
235
236 /* Notify the input device that serves us */
237 struct cons_channel *cc = kd->kd_in;
238 if (cc != NULL &&
239 (error = (*cc->cc_iopen)(cc)) != 0) {
240 splx(s);
241 return (error);
242 }
243
244 ttychars(tp);
245 tp->t_iflag = TTYDEF_IFLAG;
246 tp->t_oflag = TTYDEF_OFLAG;
247 tp->t_cflag = TTYDEF_CFLAG;
248 tp->t_lflag = TTYDEF_LFLAG;
249 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
250 (void) kdparam(tp, &tp->t_termios);
251 ttsetwater(tp);
252 tp->t_winsize.ws_row = kd->rows;
253 tp->t_winsize.ws_col = kd->cols;
254 /* Flush pending input? Clear translator? */
255 /* This (pseudo)device always has SOFTCAR */
256 tp->t_state |= TS_CARR_ON;
257 }
258
259 splx(s);
260
261 return ((*tp->t_linesw->l_open)(dev, tp));
262 }
263
264 int
265 kdclose(dev_t dev, int flag, int mode, struct lwp *l)
266 {
267 struct kd_softc *kd;
268 struct tty *tp;
269 struct cons_channel *cc;
270
271 kd = &kd_softc; /* XXX */
272 tp = kd->kd_tty;
273
274 /* XXX This is for cons.c. */
275 if ((tp->t_state & TS_ISOPEN) == 0)
276 return 0;
277
278 (*tp->t_linesw->l_close)(tp, flag);
279 ttyclose(tp);
280
281 if ((cc = kd->kd_in) != NULL)
282 (void)(*cc->cc_iclose)(cc);
283
284 return (0);
285 }
286
287 int
288 kdread(dev_t dev, struct uio *uio, int flag)
289 {
290 struct kd_softc *kd;
291 struct tty *tp;
292
293 kd = &kd_softc; /* XXX */
294 tp = kd->kd_tty;
295
296 return ((*tp->t_linesw->l_read)(tp, uio, flag));
297 }
298
299 int
300 kdwrite(dev_t dev, struct uio *uio, 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_write)(tp, uio, flag));
309 }
310
311 int
312 kdpoll(dev_t dev, int events, struct lwp *l)
313 {
314 struct kd_softc *kd;
315 struct tty *tp;
316
317 kd = &kd_softc; /* XXX */
318 tp = kd->kd_tty;
319
320 return ((*tp->t_linesw->l_poll)(tp, events, l));
321 }
322
323 int
324 kdioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
325 {
326 struct kd_softc *kd;
327 struct tty *tp;
328 int error;
329
330 kd = &kd_softc; /* XXX */
331 tp = kd->kd_tty;
332
333 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
334 if (error != EPASSTHROUGH)
335 return error;
336
337 error = ttioctl(tp, cmd, data, flag, l);
338 if (error != EPASSTHROUGH)
339 return error;
340
341 /* Handle any ioctl commands specific to kbd/display. */
342 /* XXX - Send KB* ioctls to kbd module? */
343 /* XXX - Send FB* ioctls to fb module? */
344
345 return EPASSTHROUGH;
346 }
347
348 static int
349 kdparam(struct tty *tp, struct termios *t)
350 {
351 /* XXX - These are ignored... */
352 tp->t_ispeed = t->c_ispeed;
353 tp->t_ospeed = t->c_ospeed;
354 tp->t_cflag = t->c_cflag;
355 return 0;
356 }
357
358
359 static void kd_later(void*);
360 static void kd_putfb(struct tty *);
361
362 static void
363 kdstart(struct tty *tp)
364 {
365 struct clist *cl;
366 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 (is_spl0(s)) {
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(void *tpaddr)
414 {
415 struct tty *tp = tpaddr;
416 int s;
417
418 kd_putfb(tp);
419
420 s = spltty();
421 tp->t_state &= ~TS_BUSY;
422 (*tp->t_linesw->l_start)(tp);
423 splx(s);
424 }
425
426 /*
427 * Put text on the screen using the PROM monitor.
428 * This can take a while, so to avoid missing
429 * interrupts, this is called at splsoftclock.
430 */
431 static void
432 kd_putfb(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 prom_putstr(buf, len);
447 }
448 }
449
450 /*
451 * Default PROM-based console input stream
452 */
453 static int kd_rom_iopen(struct cons_channel *);
454 static int kd_rom_iclose(struct cons_channel *);
455
456 static struct cons_channel prom_cons_channel;
457
458 int
459 kd_rom_iopen(struct cons_channel *cc)
460 {
461 return (0);
462 }
463
464 int
465 kd_rom_iclose(struct cons_channel *cc)
466 {
467 return (0);
468 }
469
470 /*
471 * Our "interrupt" routine for input. This is called by
472 * the keyboard driver (dev/sun/kbd.c) at spltty.
473 */
474 void
475 kd_cons_input(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(struct consdev *);
499 static void kdcninit(struct consdev *);
500 static void kdcnputc(dev_t, int);
501 static void kdcnpollc(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(struct cons_channel *cc, struct consdev *cn)
517 {
518 struct kd_softc *kd = &kd_softc;
519 struct kbd_softc *kds = cc->cc_dev;
520 struct kbd_state *ks;
521
522 /* Share the keyboard state */
523 kdcn_state = ks = &kds->k_state;
524
525 kd->kd_in = cc;
526 cc->cc_upstream = kd_cons_input;
527
528 /* Attach lower level. */
529 cn_hw->cn_dev = cn->cn_dev;
530 cn_hw->cn_pollc = cn->cn_pollc;
531 cn_hw->cn_getc = cn->cn_getc;
532
533 /* Attach us as console. */
534 cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
535 cn_tab->cn_probe = kdcnprobe;
536 cn_tab->cn_init = kdcninit;
537 cn_tab->cn_getc = kdcngetc;
538 cn_tab->cn_pollc = kdcnpollc;
539 cn_tab->cn_pri = CN_INTERNAL;
540
541 /* Set up initial PROM input channel for /dev/console */
542 prom_cons_channel.cc_dev = NULL;
543 prom_cons_channel.cc_iopen = kd_rom_iopen;
544 prom_cons_channel.cc_iclose = kd_rom_iclose;
545
546 /* Indicate that it is OK to use the PROM fbwrite */
547 kd_is_console = 1;
548 }
549
550
551 void kd_attach_input(struct cons_channel *);
552 void
553 kd_attach_input(struct cons_channel *cc)
554 {
555 struct kd_softc *kd = &kd_softc;
556
557 kd->kd_in = cc;
558 cc->cc_upstream = kd_cons_input;
559 }
560
561
562 /* We never call this. */
563 static void
564 kdcnprobe(struct consdev *cn)
565 {
566 }
567
568 static void
569 kdcninit(struct consdev *cn)
570 {
571 #if 0
572 struct kbd_state *ks = kdcn_state;
573
574 cn->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
575 cn->cn_pri = CN_INTERNAL;
576
577 /* This prepares kbd_translate() */
578 ks->kbd_id = KBD_MIN_TYPE;
579 kbd_xlate_init(ks);
580
581 /* Set up initial PROM input channel for /dev/console */
582 prom_cons_channel.cc_dev = NULL;
583 prom_cons_channel.cc_iopen = kd_rom_iopen;
584 prom_cons_channel.cc_iclose = kd_rom_iclose;
585 cons_attach_input(&prom_cons_channel);
586
587 /* Indicate that it is OK to use the PROM fbwrite */
588 kd_is_console = 1;
589 #endif
590 }
591
592 static int
593 kdcngetc(dev_t dev)
594 {
595 struct kbd_state *ks = kdcn_state;
596 int code, class, data, keysym;
597 extern int prom_cngetc(dev_t);
598
599
600 if (cn_hw->cn_getc == prom_cngetc) return (*cn_hw->cn_getc)(dev);
601 for (;;) {
602 code = (*cn_hw->cn_getc)(dev);
603 keysym = kbd_code_to_keysym(ks, code);
604 class = KEYSYM_CLASS(keysym);
605
606 switch (class) {
607 case KEYSYM_ASCII:
608 goto out;
609
610 case KEYSYM_CLRMOD:
611 case KEYSYM_SETMOD:
612 data = (keysym & 0x1F);
613 /* Only allow ctrl or shift. */
614 if (data > KBMOD_SHIFT_R)
615 break;
616 data = 1 << data;
617 if (class == KEYSYM_SETMOD)
618 ks->kbd_modbits |= data;
619 else
620 ks->kbd_modbits &= ~data;
621 break;
622
623 case KEYSYM_ALL_UP:
624 /* No toggle keys here. */
625 ks->kbd_modbits = 0;
626 break;
627
628 default: /* ignore all other keysyms */
629 break;
630 }
631 }
632 out:
633 return (keysym);
634 }
635
636 static void
637 kdcnputc(dev_t dev, int c)
638 {
639 int s;
640
641 s = splhigh();
642 prom_putchar(c);
643 splx(s);
644 }
645
646 static void
647 kdcnpollc(dev_t dev, int on)
648 {
649 struct kbd_state *ks = kdcn_state;
650
651 if (on) {
652 /* Entering debugger. */
653 #if NFB > 0
654 fb_unblank();
655 #endif
656 /* Clear shift keys too. */
657 ks->kbd_modbits = 0;
658 } else {
659 /* Resuming kernel. */
660 }
661 (*cn_hw->cn_pollc)(dev, on);
662 }
663