1 /* $NetBSD: kd.c,v 1.20 2008/03/29 19:15:35 tsutsui Exp $ */
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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>
46 #include <sys/systm.h>
47 #include <sys/ioctl.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>
62 #include <dev/sun/fbio.h>
63 #include <machine/fbvar.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 */
78 struct device kd_dev
; /* required first: base device */
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
);
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?
118 kd_init(struct kd_softc
*kd
)
124 #ifdef notyet /* PROM_OBP_V2 */
129 kd
= &kd_softc
; /* XXX */
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);
141 * get the console struct winsize.
146 kd
->rows
= fbrcons_rows();
147 kd
->cols
= fbrcons_cols();
152 /* else, consult the PROM */
153 switch (prom_version()) {
156 if ((ep
= (struct eeprom
*)eeprom_va
) == NULL
)
159 kd
->rows
= (u_short
)ep
->eeTtyRows
;
161 kd
->cols
= (u_short
)ep
->eeTtyCols
;
163 #endif /* PROM_OLDMON */
164 #ifdef notyet /* PROM_OBP_V2 */
171 (prop
= PROM_getpropstring(optionsnode
, "screen-#rows"))) {
173 while (*prop
!= '\0')
174 i
= i
* 10 + *prop
++ - '0';
175 kd
->rows
= (unsigned short)i
;
178 (prop
= PROM_getpropstring(optionsnode
, "screen-#columns"))) {
180 while (*prop
!= '\0')
181 i
= i
* 10 + *prop
++ - '0';
182 kd
->cols
= (unsigned short)i
;
185 #endif /* PROM_OBP_V2 */
195 kd
= &kd_softc
; /* XXX */
200 kdopen(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
205 static int firstopen
= 1;
210 kd
= &kd_softc
; /* XXX */
218 /* It's simpler to do this up here. */
219 if (kauth_authorize_device_tty(l
->l_cred
, KAUTH_DEVICE_TTY_OPEN
, tp
))
224 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
227 /* Notify the input device that serves us */
228 struct cons_channel
*cc
= kd
->kd_in
;
230 (error
= (*cc
->cc_iopen
)(cc
)) != 0) {
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
);
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
;
252 return ((*tp
->t_linesw
->l_open
)(dev
, tp
));
256 kdclose(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
260 struct cons_channel
*cc
;
262 kd
= &kd_softc
; /* XXX */
265 /* XXX This is for cons.c. */
266 if ((tp
->t_state
& TS_ISOPEN
) == 0)
269 (*tp
->t_linesw
->l_close
)(tp
, flag
);
272 if ((cc
= kd
->kd_in
) != NULL
)
273 (void)(*cc
->cc_iclose
)(cc
);
279 kdread(dev_t dev
, struct uio
*uio
, int flag
)
284 kd
= &kd_softc
; /* XXX */
287 return ((*tp
->t_linesw
->l_read
)(tp
, uio
, flag
));
291 kdwrite(dev_t dev
, struct uio
*uio
, int flag
)
296 kd
= &kd_softc
; /* XXX */
299 return ((*tp
->t_linesw
->l_write
)(tp
, uio
, flag
));
303 kdpoll(dev_t dev
, int events
, struct lwp
*l
)
308 kd
= &kd_softc
; /* XXX */
311 return ((*tp
->t_linesw
->l_poll
)(tp
, events
, l
));
315 kdioctl(dev_t dev
, u_long cmd
, void *data
, int flag
, struct lwp
*l
)
321 kd
= &kd_softc
; /* XXX */
324 error
= (*tp
->t_linesw
->l_ioctl
)(tp
, cmd
, data
, flag
, l
);
325 if (error
!= EPASSTHROUGH
)
328 error
= ttioctl(tp
, cmd
, data
, flag
, l
);
329 if (error
!= EPASSTHROUGH
)
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? */
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
;
350 static void kd_putfb(struct tty
*);
353 kdstart(struct tty
*tp
)
360 if (tp
->t_state
& (TS_BUSY
|TS_TTSTOP
|TS_TIMEOUT
))
366 tp
->t_state
|= TS_BUSY
;
368 /* called at level zero - update screen now. */
372 tp
->t_state
&= ~TS_BUSY
;
374 /* called at interrupt level - do it later */
375 callout_schedule(&tp
->t_rstrt_ch
, 0);
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
);
393 * Timeout function to do delayed writes to the screen.
394 * Called at splsoftclock when requested by kdstart.
397 kd_later(void *tpaddr
)
399 struct tty
*tp
= tpaddr
;
405 tp
->t_state
&= ~TS_BUSY
;
406 (*tp
->t_linesw
->l_start
)(tp
);
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.
416 kd_putfb(struct tty
*tp
)
419 struct clist
*cl
= &tp
->t_outq
;
423 while ((len
= q_to_b(cl
, buf
, PUT_WSIZE
-1)) > 0) {
424 /* PROM will barf if high bits are set. */
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
;
443 kd_rom_iopen(struct cons_channel
*cc
)
449 kd_rom_iclose(struct cons_channel
*cc
)
455 * Our "interrupt" routine for input. This is called by
456 * the keyboard driver (dev/sun/kbd.c) at spltty.
461 struct kd_softc
*kd
= &kd_softc
;
464 /* XXX: Make sure the device is open. */
468 if ((tp
->t_state
& TS_ISOPEN
) == 0)
471 (*tp
->t_linesw
->l_rint
)(c
, tp
);
475 /****************************************************************
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
= {
497 struct consdev
*cn_hw
= &consdev_kd
;
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
;
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 */
535 void kd_attach_input(struct cons_channel
*);
537 kd_attach_input(struct cons_channel
*cc
)
539 struct kd_softc
*kd
= &kd_softc
;
542 cc
->cc_upstream
= kd_cons_input
;
546 /* We never call this. */
548 kdcnprobe(struct consdev
*cn
)
553 kdcninit(struct consdev
*cn
)
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
;
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 */
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
);
586 code
= (*cn_hw
->cn_getc
)(dev
);
587 keysym
= kbd_code_to_keysym(ks
, code
);
588 class = KEYSYM_CLASS(keysym
);
596 data
= (keysym
& 0x1F);
597 /* Only allow ctrl or shift. */
598 if (data
> KBMOD_SHIFT_R
)
601 if (class == KEYSYM_SETMOD
)
602 ks
->kbd_modbits
|= data
;
604 ks
->kbd_modbits
&= ~data
;
608 /* No toggle keys here. */
612 default: /* ignore all other keysyms */
621 kdcnputc(dev_t dev
, int c
)
631 kdcnpollc(dev_t dev
, int on
)
633 struct kbd_state
*ks
= kdcn_state
;
636 /* Entering debugger. */
640 /* Clear shift keys too. */
643 /* Resuming kernel. */
645 (*cn_hw
->cn_pollc
)(dev
, on
);