No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / sun2 / dev / kd.c
blobc344a5d9699d40df2c8654b5a195c0663d0eded1
1 /* $NetBSD: kd.c,v 1.20 2008/03/29 19:15:35 tsutsui Exp $ */
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gordon W. Ross.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Keyboard/Display device.
35 * This driver exists simply to provide a tty device that
36 * the indirect console driver can point to.
37 * The kbd driver sends its input here.
38 * Output goes to the screen via PROM printf.
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: kd.c,v 1.20 2008/03/29 19:15:35 tsutsui Exp $");
44 #include <sys/param.h>
45 #include <sys/proc.h>
46 #include <sys/systm.h>
47 #include <sys/ioctl.h>
48 #include <sys/tty.h>
49 #include <sys/file.h>
50 #include <sys/conf.h>
51 #include <sys/device.h>
52 #include <sys/kauth.h>
54 #include <machine/promlib.h>
55 #include <machine/eeprom.h>
56 #include <machine/psl.h>
57 #include <machine/cpu.h>
58 #include <machine/kbd.h>
59 #include <machine/autoconf.h>
61 #ifdef RASTERCONSOLE
62 #include <dev/sun/fbio.h>
63 #include <machine/fbvar.h>
64 #endif
67 #include <dev/cons.h>
68 #include <dev/sun/event_var.h>
69 #include <dev/sun/kbd_xlate.h>
70 #include <dev/sun/kbdvar.h>
71 #include <sun2/dev/cons.h>
73 struct tty *fbconstty = 0; /* tty structure for frame buffer console */
75 #define PUT_WSIZE 64
77 struct kd_softc {
78 struct device kd_dev; /* required first: base device */
79 struct tty *kd_tty;
80 int rows, cols;
82 /* Console input hook */
83 struct cons_channel *kd_in;
87 * There is no point in pretending there might be
88 * more than one keyboard/display device.
90 static struct kd_softc kd_softc;
91 static int kd_is_console;
93 static int kdparam(struct tty *, struct termios *);
94 static void kdstart(struct tty *);
95 static void kd_init(struct kd_softc *);
96 static void kd_cons_input(int);
97 static int kdcngetc(dev_t);
98 static void kd_later(void*);
100 dev_type_open(kdopen);
101 dev_type_close(kdclose);
102 dev_type_read(kdread);
103 dev_type_write(kdwrite);
104 dev_type_ioctl(kdioctl);
105 dev_type_tty(kdtty);
106 dev_type_poll(kdpoll);
108 const struct cdevsw kd_cdevsw = {
109 kdopen, kdclose, kdread, kdwrite, kdioctl,
110 nostop, kdtty, kdpoll, nommap, ttykqfilter, D_TTY
114 * This is called by kbd_attach()
115 * XXX - Make this a proper child of kbd?
117 void
118 kd_init(struct kd_softc *kd)
120 struct tty *tp;
121 #ifdef PROM_OLDMON
122 struct eeprom *ep;
123 #endif
124 #ifdef notyet /* PROM_OBP_V2 */
125 int i;
126 char *prop;
127 #endif
129 kd = &kd_softc; /* XXX */
131 tp = ttymalloc();
132 callout_setfunc(&tp->t_rstrt_ch, kd_later, tp);
133 tp->t_oproc = kdstart;
134 tp->t_param = kdparam;
135 tp->t_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
137 tty_attach(tp);
138 kd->kd_tty = tp;
141 * get the console struct winsize.
143 if (kd_is_console) {
144 fbconstty = tp;
145 #ifdef RASTERCONSOLE
146 kd->rows = fbrcons_rows();
147 kd->cols = fbrcons_cols();
148 rcons_ttyinit(tp);
149 #endif
152 /* else, consult the PROM */
153 switch (prom_version()) {
154 #ifdef PROM_OLDMON
155 case PROM_OLDMON:
156 if ((ep = (struct eeprom *)eeprom_va) == NULL)
157 break;
158 if (kd->rows == 0)
159 kd->rows = (u_short)ep->eeTtyRows;
160 if (kd->cols == 0)
161 kd->cols = (u_short)ep->eeTtyCols;
162 break;
163 #endif /* PROM_OLDMON */
164 #ifdef notyet /* PROM_OBP_V2 */
165 case PROM_OBP_V0:
166 case PROM_OBP_V2:
167 case PROM_OBP_V3:
168 case PROM_OPENFIRM:
170 if (kd->rows == 0 &&
171 (prop = PROM_getpropstring(optionsnode, "screen-#rows"))) {
172 i = 0;
173 while (*prop != '\0')
174 i = i * 10 + *prop++ - '0';
175 kd->rows = (unsigned short)i;
177 if (kd->cols == 0 &&
178 (prop = PROM_getpropstring(optionsnode, "screen-#columns"))) {
179 i = 0;
180 while (*prop != '\0')
181 i = i * 10 + *prop++ - '0';
182 kd->cols = (unsigned short)i;
184 break;
185 #endif /* PROM_OBP_V2 */
187 return;
190 struct tty *
191 kdtty(dev_t dev)
193 struct kd_softc *kd;
195 kd = &kd_softc; /* XXX */
196 return (kd->kd_tty);
199 int
200 kdopen(dev_t dev, int flag, int mode, struct lwp *l)
202 struct kd_softc *kd;
203 int error, s, unit;
204 struct tty *tp;
205 static int firstopen = 1;
207 unit = minor(dev);
208 if (unit != 0)
209 return ENXIO;
210 kd = &kd_softc; /* XXX */
212 if (firstopen) {
213 kd_init(kd);
214 firstopen = 0;
216 tp = kd->kd_tty;
218 /* It's simpler to do this up here. */
219 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
220 return (EBUSY);
222 s = spltty();
224 if ((tp->t_state & TS_ISOPEN) == 0) {
225 /* First open. */
227 /* Notify the input device that serves us */
228 struct cons_channel *cc = kd->kd_in;
229 if (cc != NULL &&
230 (error = (*cc->cc_iopen)(cc)) != 0) {
231 splx(s);
232 return (error);
235 ttychars(tp);
236 tp->t_iflag = TTYDEF_IFLAG;
237 tp->t_oflag = TTYDEF_OFLAG;
238 tp->t_cflag = TTYDEF_CFLAG;
239 tp->t_lflag = TTYDEF_LFLAG;
240 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
241 (void) kdparam(tp, &tp->t_termios);
242 ttsetwater(tp);
243 tp->t_winsize.ws_row = kd->rows;
244 tp->t_winsize.ws_col = kd->cols;
245 /* Flush pending input? Clear translator? */
246 /* This (pseudo)device always has SOFTCAR */
247 tp->t_state |= TS_CARR_ON;
250 splx(s);
252 return ((*tp->t_linesw->l_open)(dev, tp));
255 int
256 kdclose(dev_t dev, int flag, int mode, struct lwp *l)
258 struct kd_softc *kd;
259 struct tty *tp;
260 struct cons_channel *cc;
262 kd = &kd_softc; /* XXX */
263 tp = kd->kd_tty;
265 /* XXX This is for cons.c. */
266 if ((tp->t_state & TS_ISOPEN) == 0)
267 return 0;
269 (*tp->t_linesw->l_close)(tp, flag);
270 ttyclose(tp);
272 if ((cc = kd->kd_in) != NULL)
273 (void)(*cc->cc_iclose)(cc);
275 return (0);
278 int
279 kdread(dev_t dev, struct uio *uio, int flag)
281 struct kd_softc *kd;
282 struct tty *tp;
284 kd = &kd_softc; /* XXX */
285 tp = kd->kd_tty;
287 return ((*tp->t_linesw->l_read)(tp, uio, flag));
290 int
291 kdwrite(dev_t dev, struct uio *uio, int flag)
293 struct kd_softc *kd;
294 struct tty *tp;
296 kd = &kd_softc; /* XXX */
297 tp = kd->kd_tty;
299 return ((*tp->t_linesw->l_write)(tp, uio, flag));
302 int
303 kdpoll(dev_t dev, int events, struct lwp *l)
305 struct kd_softc *kd;
306 struct tty *tp;
308 kd = &kd_softc; /* XXX */
309 tp = kd->kd_tty;
311 return ((*tp->t_linesw->l_poll)(tp, events, l));
314 int
315 kdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
317 struct kd_softc *kd;
318 struct tty *tp;
319 int error;
321 kd = &kd_softc; /* XXX */
322 tp = kd->kd_tty;
324 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
325 if (error != EPASSTHROUGH)
326 return error;
328 error = ttioctl(tp, cmd, data, flag, l);
329 if (error != EPASSTHROUGH)
330 return error;
332 /* Handle any ioctl commands specific to kbd/display. */
333 /* XXX - Send KB* ioctls to kbd module? */
334 /* XXX - Send FB* ioctls to fb module? */
336 return EPASSTHROUGH;
339 static int
340 kdparam(struct tty *tp, struct termios *t)
342 /* XXX - These are ignored... */
343 tp->t_ispeed = t->c_ispeed;
344 tp->t_ospeed = t->c_ospeed;
345 tp->t_cflag = t->c_cflag;
346 return 0;
350 static void kd_putfb(struct tty *);
352 static void
353 kdstart(struct tty *tp)
355 struct clist *cl;
356 int s1, s2;
358 s1 = splsoftclock();
359 s2 = spltty();
360 if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
361 goto out;
363 cl = &tp->t_outq;
364 if (ttypull(tp)) {
365 if (kd_is_console) {
366 tp->t_state |= TS_BUSY;
367 if (is_spl0(s1)) {
368 /* called at level zero - update screen now. */
369 splx(s2);
370 kd_putfb(tp);
371 s2 = spltty();
372 tp->t_state &= ~TS_BUSY;
373 } else {
374 /* called at interrupt level - do it later */
375 callout_schedule(&tp->t_rstrt_ch, 0);
377 } else {
379 * This driver uses the PROM for writing the screen,
380 * and that only works if this is the console device.
381 * If this is not the console, just flush the output.
382 * Sorry. (In that case, use xdm instead of getty.)
384 ndflush(cl, cl->c_cc);
387 out:
388 splx(s2);
389 splx(s1);
393 * Timeout function to do delayed writes to the screen.
394 * Called at splsoftclock when requested by kdstart.
396 static void
397 kd_later(void *tpaddr)
399 struct tty *tp = tpaddr;
400 int s;
402 kd_putfb(tp);
404 s = spltty();
405 tp->t_state &= ~TS_BUSY;
406 (*tp->t_linesw->l_start)(tp);
407 splx(s);
411 * Put text on the screen using the PROM monitor.
412 * This can take a while, so to avoid missing
413 * interrupts, this is called at splsoftclock.
415 static void
416 kd_putfb(struct tty *tp)
418 char buf[PUT_WSIZE];
419 struct clist *cl = &tp->t_outq;
420 char *p, *end;
421 int len;
423 while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
424 /* PROM will barf if high bits are set. */
425 p = buf;
426 end = buf + len;
427 while (p < end)
428 *p++ &= 0x7f;
429 /* Now let the PROM print it. */
430 prom_putstr(buf, len);
435 * Default PROM-based console input stream
437 static int kd_rom_iopen(struct cons_channel *);
438 static int kd_rom_iclose(struct cons_channel *);
440 static struct cons_channel prom_cons_channel;
442 int
443 kd_rom_iopen(struct cons_channel *cc)
445 return (0);
448 int
449 kd_rom_iclose(struct cons_channel *cc)
451 return (0);
455 * Our "interrupt" routine for input. This is called by
456 * the keyboard driver (dev/sun/kbd.c) at spltty.
458 void
459 kd_cons_input(int c)
461 struct kd_softc *kd = &kd_softc;
462 struct tty *tp;
464 /* XXX: Make sure the device is open. */
465 tp = kd->kd_tty;
466 if (tp == NULL)
467 return;
468 if ((tp->t_state & TS_ISOPEN) == 0)
469 return;
471 (*tp->t_linesw->l_rint)(c, tp);
475 /****************************************************************
476 * kd console support
477 ****************************************************************/
479 /* The debugger gets its own key translation state. */
480 static struct kbd_state *kdcn_state;
482 static void kdcnprobe(struct consdev *);
483 static void kdcninit(struct consdev *);
484 static void kdcnputc(dev_t, int);
485 static void kdcnpollc(dev_t, int);
487 /* The keyboard driver uses cn_hw to access the real console driver */
488 extern struct consdev consdev_prom;
489 struct consdev consdev_kd = {
490 kdcnprobe,
491 kdcninit,
492 kdcngetc,
493 kdcnputc,
494 kdcnpollc,
495 NULL,
497 struct consdev *cn_hw = &consdev_kd;
499 void
500 cons_attach_input(struct cons_channel *cc, struct consdev *cn)
502 struct kd_softc *kd = &kd_softc;
503 struct kbd_softc *kds = cc->cc_private;
504 struct kbd_state *ks;
506 /* Share the keyboard state */
507 kdcn_state = ks = &kds->k_state;
509 kd->kd_in = cc;
510 cc->cc_upstream = kd_cons_input;
512 /* Attach lower level. */
513 cn_hw->cn_dev = cn->cn_dev;
514 cn_hw->cn_pollc = cn->cn_pollc;
515 cn_hw->cn_getc = cn->cn_getc;
517 /* Attach us as console. */
518 cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
519 cn_tab->cn_probe = kdcnprobe;
520 cn_tab->cn_init = kdcninit;
521 cn_tab->cn_getc = kdcngetc;
522 cn_tab->cn_pollc = kdcnpollc;
523 cn_tab->cn_pri = CN_INTERNAL;
525 /* Set up initial PROM input channel for /dev/console */
526 prom_cons_channel.cc_private = NULL;
527 prom_cons_channel.cc_iopen = kd_rom_iopen;
528 prom_cons_channel.cc_iclose = kd_rom_iclose;
530 /* Indicate that it is OK to use the PROM fbwrite */
531 kd_is_console = 1;
535 void kd_attach_input(struct cons_channel *);
536 void
537 kd_attach_input(struct cons_channel *cc)
539 struct kd_softc *kd = &kd_softc;
541 kd->kd_in = cc;
542 cc->cc_upstream = kd_cons_input;
546 /* We never call this. */
547 static void
548 kdcnprobe(struct consdev *cn)
552 static void
553 kdcninit(struct consdev *cn)
555 #if 0
556 struct kbd_state *ks = kdcn_state;
558 cn->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
559 cn->cn_pri = CN_INTERNAL;
561 /* This prepares kbd_translate() */
562 ks->kbd_id = KBD_MIN_TYPE;
563 kbd_xlate_init(ks);
565 /* Set up initial PROM input channel for /dev/console */
566 prom_cons_channel.cc_private = NULL;
567 prom_cons_channel.cc_iopen = kd_rom_iopen;
568 prom_cons_channel.cc_iclose = kd_rom_iclose;
569 cons_attach_input(&prom_cons_channel);
571 /* Indicate that it is OK to use the PROM fbwrite */
572 kd_is_console = 1;
573 #endif
576 static int
577 kdcngetc(dev_t dev)
579 struct kbd_state *ks = kdcn_state;
580 int code, class, data, keysym;
581 extern int prom_cngetc(dev_t);
584 if (cn_hw->cn_getc == prom_cngetc) return (*cn_hw->cn_getc)(dev);
585 for (;;) {
586 code = (*cn_hw->cn_getc)(dev);
587 keysym = kbd_code_to_keysym(ks, code);
588 class = KEYSYM_CLASS(keysym);
590 switch (class) {
591 case KEYSYM_ASCII:
592 goto out;
594 case KEYSYM_CLRMOD:
595 case KEYSYM_SETMOD:
596 data = (keysym & 0x1F);
597 /* Only allow ctrl or shift. */
598 if (data > KBMOD_SHIFT_R)
599 break;
600 data = 1 << data;
601 if (class == KEYSYM_SETMOD)
602 ks->kbd_modbits |= data;
603 else
604 ks->kbd_modbits &= ~data;
605 break;
607 case KEYSYM_ALL_UP:
608 /* No toggle keys here. */
609 ks->kbd_modbits = 0;
610 break;
612 default: /* ignore all other keysyms */
613 break;
616 out:
617 return (keysym);
620 static void
621 kdcnputc(dev_t dev, int c)
623 int s;
625 s = splhigh();
626 prom_putchar(c);
627 splx(s);
630 static void
631 kdcnpollc(dev_t dev, int on)
633 struct kbd_state *ks = kdcn_state;
635 if (on) {
636 /* Entering debugger. */
637 #if NFB > 0
638 fb_unblank();
639 #endif
640 /* Clear shift keys too. */
641 ks->kbd_modbits = 0;
642 } else {
643 /* Resuming kernel. */
645 (*cn_hw->cn_pollc)(dev, on);