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