kd.c revision 1.11.4.1 1 /* $NetBSD: kd.c,v 1.11.4.1 2006/09/09 02:43:58 rpaulo 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.11.4.1 2006/09/09 02:43:58 rpaulo 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_cred, KAUTH_GENERIC_ISSUSER,
227 &l->l_acflag) != 0) )
228 {
229 return (EBUSY);
230 }
231
232 s = spltty();
233
234 if ((tp->t_state & TS_ISOPEN) == 0) {
235 /* First open. */
236
237 /* Notify the input device that serves us */
238 struct cons_channel *cc = kd->kd_in;
239 if (cc != NULL &&
240 (error = (*cc->cc_iopen)(cc)) != 0) {
241 splx(s);
242 return (error);
243 }
244
245 ttychars(tp);
246 tp->t_iflag = TTYDEF_IFLAG;
247 tp->t_oflag = TTYDEF_OFLAG;
248 tp->t_cflag = TTYDEF_CFLAG;
249 tp->t_lflag = TTYDEF_LFLAG;
250 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
251 (void) kdparam(tp, &tp->t_termios);
252 ttsetwater(tp);
253 tp->t_winsize.ws_row = kd->rows;
254 tp->t_winsize.ws_col = kd->cols;
255 /* Flush pending input? Clear translator? */
256 /* This (pseudo)device always has SOFTCAR */
257 tp->t_state |= TS_CARR_ON;
258 }
259
260 splx(s);
261
262 return ((*tp->t_linesw->l_open)(dev, tp));
263 }
264
265 int
266 kdclose(dev_t dev, int flag, int mode, struct lwp *l)
267 {
268 struct kd_softc *kd;
269 struct tty *tp;
270 struct cons_channel *cc;
271
272 kd = &kd_softc; /* XXX */
273 tp = kd->kd_tty;
274
275 /* XXX This is for cons.c. */
276 if ((tp->t_state & TS_ISOPEN) == 0)
277 return 0;
278
279 (*tp->t_linesw->l_close)(tp, flag);
280 ttyclose(tp);
281
282 if ((cc = kd->kd_in) != NULL)
283 (void)(*cc->cc_iclose)(cc);
284
285 return (0);
286 }
287
288 int
289 kdread(dev_t dev, struct uio *uio, 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_read)(tp, uio, flag));
298 }
299
300 int
301 kdwrite(dev_t dev, struct uio *uio, int flag)
302 {
303 struct kd_softc *kd;
304 struct tty *tp;
305
306 kd = &kd_softc; /* XXX */
307 tp = kd->kd_tty;
308
309 return ((*tp->t_linesw->l_write)(tp, uio, flag));
310 }
311
312 int
313 kdpoll(dev_t dev, int events, struct lwp *l)
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_poll)(tp, events, l));
322 }
323
324 int
325 kdioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
326 {
327 struct kd_softc *kd;
328 struct tty *tp;
329 int error;
330
331 kd = &kd_softc; /* XXX */
332 tp = kd->kd_tty;
333
334 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
335 if (error != EPASSTHROUGH)
336 return error;
337
338 error = ttioctl(tp, cmd, data, flag, l);
339 if (error != EPASSTHROUGH)
340 return error;
341
342 /* Handle any ioctl commands specific to kbd/display. */
343 /* XXX - Send KB* ioctls to kbd module? */
344 /* XXX - Send FB* ioctls to fb module? */
345
346 return EPASSTHROUGH;
347 }
348
349 static int
350 kdparam(struct tty *tp, struct termios *t)
351 {
352 /* XXX - These are ignored... */
353 tp->t_ispeed = t->c_ispeed;
354 tp->t_ospeed = t->c_ospeed;
355 tp->t_cflag = t->c_cflag;
356 return 0;
357 }
358
359
360 static void kd_later(void*);
361 static void kd_putfb(struct tty *);
362
363 static void
364 kdstart(struct tty *tp)
365 {
366 struct clist *cl;
367 int s;
368
369 s = spltty();
370 if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
371 goto out;
372
373 cl = &tp->t_outq;
374 if (cl->c_cc) {
375 if (kd_is_console) {
376 tp->t_state |= TS_BUSY;
377 if (is_spl0(s)) {
378 /* called at level zero - update screen now. */
379 (void) spllowersoftclock();
380 kd_putfb(tp);
381 (void) spltty();
382 tp->t_state &= ~TS_BUSY;
383 } else {
384 /* called at interrupt level - do it later */
385 callout_reset(&tp->t_rstrt_ch, 0,
386 kd_later, tp);
387 }
388 } else {
389 /*
390 * This driver uses the PROM for writing the screen,
391 * and that only works if this is the console device.
392 * If this is not the console, just flush the output.
393 * Sorry. (In that case, use xdm instead of getty.)
394 */
395 ndflush(cl, cl->c_cc);
396 }
397 }
398 if (cl->c_cc <= tp->t_lowat) {
399 if (tp->t_state & TS_ASLEEP) {
400 tp->t_state &= ~TS_ASLEEP;
401 wakeup((caddr_t)cl);
402 }
403 selwakeup(&tp->t_wsel);
404 }
405 out:
406 splx(s);
407 }
408
409 /*
410 * Timeout function to do delayed writes to the screen.
411 * Called at splsoftclock when requested by kdstart.
412 */
413 static void
414 kd_later(void *tpaddr)
415 {
416 struct tty *tp = tpaddr;
417 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(struct tty *tp)
434 {
435 char buf[PUT_WSIZE];
436 struct clist *cl = &tp->t_outq;
437 char *p, *end;
438 int len;
439
440 while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
441 /* PROM will barf if high bits are set. */
442 p = buf;
443 end = buf + len;
444 while (p < end)
445 *p++ &= 0x7f;
446 /* Now let the PROM print it. */
447 prom_putstr(buf, len);
448 }
449 }
450
451 /*
452 * Default PROM-based console input stream
453 */
454 static int kd_rom_iopen(struct cons_channel *);
455 static int kd_rom_iclose(struct cons_channel *);
456
457 static struct cons_channel prom_cons_channel;
458
459 int
460 kd_rom_iopen(struct cons_channel *cc)
461 {
462 return (0);
463 }
464
465 int
466 kd_rom_iclose(struct cons_channel *cc)
467 {
468 return (0);
469 }
470
471 /*
472 * Our "interrupt" routine for input. This is called by
473 * the keyboard driver (dev/sun/kbd.c) at spltty.
474 */
475 void
476 kd_cons_input(int c)
477 {
478 struct kd_softc *kd = &kd_softc;
479 struct tty *tp;
480
481 /* XXX: Make sure the device is open. */
482 tp = kd->kd_tty;
483 if (tp == NULL)
484 return;
485 if ((tp->t_state & TS_ISOPEN) == 0)
486 return;
487
488 (*tp->t_linesw->l_rint)(c, tp);
489 }
490
491
492 /****************************************************************
493 * kd console support
494 ****************************************************************/
495
496 /* The debugger gets its own key translation state. */
497 static struct kbd_state *kdcn_state;
498
499 static void kdcnprobe(struct consdev *);
500 static void kdcninit(struct consdev *);
501 static void kdcnputc(dev_t, int);
502 static void kdcnpollc(dev_t, int);
503
504 /* The keyboard driver uses cn_hw to access the real console driver */
505 extern struct consdev consdev_prom;
506 struct consdev consdev_kd = {
507 kdcnprobe,
508 kdcninit,
509 kdcngetc,
510 kdcnputc,
511 kdcnpollc,
512 NULL,
513 };
514 struct consdev *cn_hw = &consdev_kd;
515
516 void
517 cons_attach_input(struct cons_channel *cc, struct consdev *cn)
518 {
519 struct kd_softc *kd = &kd_softc;
520 struct kbd_softc *kds = cc->cc_dev;
521 struct kbd_state *ks;
522
523 /* Share the keyboard state */
524 kdcn_state = ks = &kds->k_state;
525
526 kd->kd_in = cc;
527 cc->cc_upstream = kd_cons_input;
528
529 /* Attach lower level. */
530 cn_hw->cn_dev = cn->cn_dev;
531 cn_hw->cn_pollc = cn->cn_pollc;
532 cn_hw->cn_getc = cn->cn_getc;
533
534 /* Attach us as console. */
535 cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
536 cn_tab->cn_probe = kdcnprobe;
537 cn_tab->cn_init = kdcninit;
538 cn_tab->cn_getc = kdcngetc;
539 cn_tab->cn_pollc = kdcnpollc;
540 cn_tab->cn_pri = CN_INTERNAL;
541
542 /* Set up initial PROM input channel for /dev/console */
543 prom_cons_channel.cc_dev = NULL;
544 prom_cons_channel.cc_iopen = kd_rom_iopen;
545 prom_cons_channel.cc_iclose = kd_rom_iclose;
546
547 /* Indicate that it is OK to use the PROM fbwrite */
548 kd_is_console = 1;
549 }
550
551
552 void kd_attach_input(struct cons_channel *);
553 void
554 kd_attach_input(struct cons_channel *cc)
555 {
556 struct kd_softc *kd = &kd_softc;
557
558 kd->kd_in = cc;
559 cc->cc_upstream = kd_cons_input;
560 }
561
562
563 /* We never call this. */
564 static void
565 kdcnprobe(struct consdev *cn)
566 {
567 }
568
569 static void
570 kdcninit(struct consdev *cn)
571 {
572 #if 0
573 struct kbd_state *ks = kdcn_state;
574
575 cn->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
576 cn->cn_pri = CN_INTERNAL;
577
578 /* This prepares kbd_translate() */
579 ks->kbd_id = KBD_MIN_TYPE;
580 kbd_xlate_init(ks);
581
582 /* Set up initial PROM input channel for /dev/console */
583 prom_cons_channel.cc_dev = NULL;
584 prom_cons_channel.cc_iopen = kd_rom_iopen;
585 prom_cons_channel.cc_iclose = kd_rom_iclose;
586 cons_attach_input(&prom_cons_channel);
587
588 /* Indicate that it is OK to use the PROM fbwrite */
589 kd_is_console = 1;
590 #endif
591 }
592
593 static int
594 kdcngetc(dev_t dev)
595 {
596 struct kbd_state *ks = kdcn_state;
597 int code, class, data, keysym;
598 extern int prom_cngetc(dev_t);
599
600
601 if (cn_hw->cn_getc == prom_cngetc) return (*cn_hw->cn_getc)(dev);
602 for (;;) {
603 code = (*cn_hw->cn_getc)(dev);
604 keysym = kbd_code_to_keysym(ks, code);
605 class = KEYSYM_CLASS(keysym);
606
607 switch (class) {
608 case KEYSYM_ASCII:
609 goto out;
610
611 case KEYSYM_CLRMOD:
612 case KEYSYM_SETMOD:
613 data = (keysym & 0x1F);
614 /* Only allow ctrl or shift. */
615 if (data > KBMOD_SHIFT_R)
616 break;
617 data = 1 << data;
618 if (class == KEYSYM_SETMOD)
619 ks->kbd_modbits |= data;
620 else
621 ks->kbd_modbits &= ~data;
622 break;
623
624 case KEYSYM_ALL_UP:
625 /* No toggle keys here. */
626 ks->kbd_modbits = 0;
627 break;
628
629 default: /* ignore all other keysyms */
630 break;
631 }
632 }
633 out:
634 return (keysym);
635 }
636
637 static void
638 kdcnputc(dev_t dev, int c)
639 {
640 int s;
641
642 s = splhigh();
643 prom_putchar(c);
644 splx(s);
645 }
646
647 static void
648 kdcnpollc(dev_t dev, int on)
649 {
650 struct kbd_state *ks = kdcn_state;
651
652 if (on) {
653 /* Entering debugger. */
654 #if NFB > 0
655 fb_unblank();
656 #endif
657 /* Clear shift keys too. */
658 ks->kbd_modbits = 0;
659 } else {
660 /* Resuming kernel. */
661 }
662 (*cn_hw->cn_pollc)(dev, on);
663 }
664