1 /* $NetBSD: kd.c,v 1.54 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.54 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/autoconf.h>
55 #include <machine/cpu.h>
56 #include <machine/mon.h>
57 #include <machine/psl.h>
60 #include <dev/sun/event_var.h>
61 #include <dev/sun/kbd_xlate.h>
62 #include <dev/sun/kbdvar.h>
63 #include <sun3/dev/zs_cons.h>
70 struct device kd_dev
; /* required first: base device */
73 /* Console input hook */
74 struct cons_channel
*kd_in
;
78 * There is no point in pretending there might be
79 * more than one keyboard/display device.
81 static struct kd_softc kd_softc
;
82 static int kd_is_console
;
84 static int kdparam(struct tty
*, struct termios
*);
85 static void kdstart(struct tty
*);
86 static void kd_init(struct kd_softc
*);
87 static void kd_cons_input(int);
88 static void kd_later(void *);
90 dev_type_open(kdopen
);
91 dev_type_close(kdclose
);
92 dev_type_read(kdread
);
93 dev_type_write(kdwrite
);
94 dev_type_ioctl(kdioctl
);
96 dev_type_poll(kdpoll
);
98 const struct cdevsw kd_cdevsw
= {
99 kdopen
, kdclose
, kdread
, kdwrite
, kdioctl
,
100 nostop
, kdtty
, kdpoll
, nommap
, ttykqfilter
, D_TTY
104 * Prepare the console tty; called on first open of /dev/console
107 kd_init(struct kd_softc
*kd
)
112 callout_setfunc(&tp
->t_rstrt_ch
, kd_later
, tp
);
114 tp
->t_oproc
= kdstart
;
115 tp
->t_param
= kdparam
;
116 tp
->t_dev
= makedev(cdevsw_lookup_major(&kd_cdevsw
), 0);
129 kd
= &kd_softc
; /* XXX */
134 kdopen(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
139 static int firstopen
= 1;
144 kd
= &kd_softc
; /* XXX */
151 /* It's simpler to do this up here. */
152 if (kauth_authorize_device_tty(l
->l_cred
, KAUTH_DEVICE_TTY_OPEN
, tp
))
157 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
160 /* Notify the input device that serves us */
161 struct cons_channel
*cc
= kd
->kd_in
;
163 (error
= (*cc
->cc_iopen
)(cc
)) != 0) {
168 tp
->t_iflag
= TTYDEF_IFLAG
;
169 tp
->t_oflag
= TTYDEF_OFLAG
;
170 tp
->t_cflag
= TTYDEF_CFLAG
;
171 tp
->t_lflag
= TTYDEF_LFLAG
;
172 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
173 (void) kdparam(tp
, &tp
->t_termios
);
175 /* Flush pending input? Clear translator? */
176 /* This (pseudo)device always has SOFTCAR */
177 tp
->t_state
|= TS_CARR_ON
;
182 return ((*tp
->t_linesw
->l_open
)(dev
, tp
));
186 kdclose(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
190 struct cons_channel
*cc
;
192 kd
= &kd_softc
; /* XXX */
195 /* XXX This is for cons.c. */
196 if ((tp
->t_state
& TS_ISOPEN
) == 0)
199 (*tp
->t_linesw
->l_close
)(tp
, flag
);
201 if ((cc
= kd
->kd_in
) != NULL
)
202 (void)(*cc
->cc_iclose
)(cc
);
207 kdread(dev_t dev
, struct uio
*uio
, int flag
)
212 kd
= &kd_softc
; /* XXX */
215 return ((*tp
->t_linesw
->l_read
)(tp
, uio
, flag
));
219 kdwrite(dev_t dev
, struct uio
*uio
, int flag
)
224 kd
= &kd_softc
; /* XXX */
227 return ((*tp
->t_linesw
->l_write
)(tp
, uio
, flag
));
231 kdpoll(dev_t dev
, int events
, struct lwp
*l
)
236 kd
= &kd_softc
; /* XXX */
239 return ((*tp
->t_linesw
->l_poll
)(tp
, events
, l
));
243 kdioctl(dev_t dev
, u_long cmd
, void *data
, int flag
, struct lwp
*l
)
249 kd
= &kd_softc
; /* XXX */
252 error
= (*tp
->t_linesw
->l_ioctl
)(tp
, cmd
, data
, flag
, l
);
253 if (error
!= EPASSTHROUGH
)
256 error
= ttioctl(tp
, cmd
, data
, flag
, l
);
257 if (error
!= EPASSTHROUGH
)
260 /* Handle any ioctl commands specific to kbd/display. */
261 /* XXX - Send KB* ioctls to kbd module? */
262 /* XXX - Send FB* ioctls to fb module? */
268 kdparam(struct tty
*tp
, struct termios
*t
)
270 /* XXX - These are ignored... */
271 tp
->t_ispeed
= t
->c_ispeed
;
272 tp
->t_ospeed
= t
->c_ospeed
;
273 tp
->t_cflag
= t
->c_cflag
;
278 static void kd_putfb(struct tty
*);
281 kdstart(struct tty
*tp
)
288 if (tp
->t_state
& (TS_BUSY
|TS_TTSTOP
|TS_TIMEOUT
))
294 tp
->t_state
|= TS_BUSY
;
295 if ((s1
& PSL_IPL
) == 0) {
296 /* called at level zero - update screen now. */
300 tp
->t_state
&= ~TS_BUSY
;
302 /* called at interrupt level - do it later */
303 callout_schedule(&tp
->t_rstrt_ch
, 0);
307 * This driver uses the PROM for writing the screen,
308 * and that only works if this is the console device.
309 * If this is not the console, just flush the output.
310 * Sorry. (In that case, use xdm instead of getty.)
312 ndflush(cl
, cl
->c_cc
);
321 * Timeout function to do delayed writes to the screen.
322 * Called at splsoftclock when requested by kdstart.
325 kd_later(void *tpaddr
)
327 struct tty
*tp
= tpaddr
;
333 tp
->t_state
&= ~TS_BUSY
;
334 (*tp
->t_linesw
->l_start
)(tp
);
339 * Put text on the screen using the PROM monitor.
340 * This can take a while, so to avoid missing
341 * interrupts, this is called at splsoftclock.
344 kd_putfb(struct tty
*tp
)
347 struct clist
*cl
= &tp
->t_outq
;
351 while ((len
= q_to_b(cl
, buf
, PUT_WSIZE
-1)) > 0) {
352 /* PROM will barf if high bits are set. */
357 (romVectorPtr
->fbWriteStr
)(buf
, len
);
362 cons_attach_input(struct cons_channel
*cc
, struct consdev
*cn
)
364 struct kd_softc
*kd
= &kd_softc
;
367 cc
->cc_upstream
= kd_cons_input
;
371 * Default PROM-based console input stream
373 static int kd_rom_iopen(struct cons_channel
*);
374 static int kd_rom_iclose(struct cons_channel
*);
376 static struct cons_channel prom_cons_channel
;
379 kd_rom_iopen(struct cons_channel
*cc
)
386 kd_rom_iclose(struct cons_channel
*cc
)
393 * Our "interrupt" routine for input. This is called by
394 * the keyboard driver (dev/sun/kbd.c) at spltty.
399 struct kd_softc
*kd
= &kd_softc
;
402 /* XXX: Make sure the device is open. */
406 if ((tp
->t_state
& TS_ISOPEN
) == 0)
409 (*tp
->t_linesw
->l_rint
)(c
, tp
);
413 /****************************************************************
415 ****************************************************************/
417 /* The debugger gets its own key translation state. */
418 static struct kbd_state kdcn_state
;
420 static void kdcnprobe(struct consdev
*);
421 static void kdcninit(struct consdev
*);
422 static int kdcngetc(dev_t
);
423 static void kdcnputc(dev_t
, int);
424 static void kdcnpollc(dev_t
, int);
426 struct consdev consdev_kd
= {
435 /* We never call this. */
437 kdcnprobe(struct consdev
*cn
)
442 kdcninit(struct consdev
*cn
)
444 struct kbd_state
*ks
= &kdcn_state
;
446 cn
->cn_dev
= makedev(cdevsw_lookup_major(&kd_cdevsw
), 0);
447 cn
->cn_pri
= CN_INTERNAL
;
449 /* This prepares kbd_translate() */
450 ks
->kbd_id
= KBD_MIN_TYPE
;
453 /* Set up initial PROM input channel for /dev/console */
454 prom_cons_channel
.cc_private
= NULL
;
455 prom_cons_channel
.cc_iopen
= kd_rom_iopen
;
456 prom_cons_channel
.cc_iclose
= kd_rom_iclose
;
457 cons_attach_input(&prom_cons_channel
, cn
);
459 /* Indicate that it is OK to use the PROM fbwrite */
466 struct kbd_state
*ks
= &kdcn_state
;
467 int code
, class, data
, keysym
;
470 code
= zs_getc(zs_conschan
);
471 keysym
= kbd_code_to_keysym(ks
, code
);
472 class = KEYSYM_CLASS(keysym
);
480 data
= (keysym
& 0x1F);
481 /* Only allow ctrl or shift. */
482 if (data
> KBMOD_SHIFT_R
)
485 if (class == KEYSYM_SETMOD
)
486 ks
->kbd_modbits
|= data
;
488 ks
->kbd_modbits
&= ~data
;
492 /* No toggle keys here. */
496 default: /* ignore all other keysyms */
505 kdcnputc(dev_t dev
, int c
)
507 (romVectorPtr
->fbWriteChar
)(c
& 0x7f);
511 kdcnpollc(dev_t dev
, int on
)
513 struct kbd_state
*ks
= &kdcn_state
;
516 /* Entering debugger. */
520 /* Clear shift keys too. */
523 /* Resuming kernel. */