1 /* $NetBSD: kd.c,v 1.48 2008/03/30 01:41:36 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.48 2008/03/30 01:41:36 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/openfirm.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/event_var.h>
63 #include <dev/sun/kbd_xlate.h>
64 #include <dev/sun/kbdvar.h>
65 #include <sparc64/dev/cons.h>
67 dev_type_open(kdopen
);
68 dev_type_close(kdclose
);
69 dev_type_read(kdread
);
70 dev_type_write(kdwrite
);
71 dev_type_ioctl(kdioctl
);
73 dev_type_poll(kdpoll
);
75 const struct cdevsw kd_cdevsw
= {
76 kdopen
, kdclose
, kdread
, kdwrite
, kdioctl
,
77 nostop
, kdtty
, kdpoll
, nommap
, ttykqfilter
, D_TTY
80 struct tty
*fbconstty
= 0; /* tty structure for frame buffer console */
85 struct device kd_dev
; /* required first: base device */
89 /* Console input hook */
90 struct cons_channel
*kd_in
;
94 * There is no point in pretending there might be
95 * more than one keyboard/display device.
97 static struct kd_softc kd_softc
;
98 static int kd_is_console
;
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 static void kd_putfb(struct tty
*);
108 int cons_ocount
; /* output byte count */
111 * This is called by kbd_attach()
112 * XXX - Make this a proper child of kbd?
115 kd_init(struct kd_softc
*kd
)
120 kd
= &kd_softc
; /* XXX */
123 callout_setfunc(&tp
->t_rstrt_ch
, kd_later
, tp
);
124 tp
->t_oproc
= kdstart
;
125 tp
->t_param
= kdparam
;
126 tp
->t_dev
= makedev(cdevsw_lookup_major(&kd_cdevsw
), 0);
132 * get the console struct winsize.
139 prom_getoption("screen-#rows", prop
, sizeof prop
) == 0)
140 kd
->rows
= strtoul(prop
, NULL
, 10);
143 prom_getoption("screen-#columns", prop
, sizeof prop
) == 0)
144 kd
->cols
= strtoul(prop
, NULL
, 10);
152 kd
= &kd_softc
; /* XXX */
157 kdopen(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
162 static int firstopen
= 1;
167 kd
= &kd_softc
; /* XXX */
175 /* It's simpler to do this up here. */
176 if (kauth_authorize_device_tty(l
->l_cred
, KAUTH_DEVICE_TTY_OPEN
, tp
))
181 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
184 /* Notify the input device that serves us */
185 struct cons_channel
*cc
= kd
->kd_in
;
187 (error
= (*cc
->cc_iopen
)(cc
)) != 0) {
193 tp
->t_iflag
= TTYDEF_IFLAG
;
194 tp
->t_oflag
= TTYDEF_OFLAG
;
195 tp
->t_cflag
= TTYDEF_CFLAG
;
196 tp
->t_lflag
= TTYDEF_LFLAG
;
197 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
198 (void) kdparam(tp
, &tp
->t_termios
);
200 tp
->t_winsize
.ws_row
= kd
->rows
;
201 tp
->t_winsize
.ws_col
= kd
->cols
;
202 /* Flush pending input? Clear translator? */
203 /* This (pseudo)device always has SOFTCAR */
204 tp
->t_state
|= TS_CARR_ON
;
209 return ((*tp
->t_linesw
->l_open
)(dev
, tp
));
213 kdclose(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
217 struct cons_channel
*cc
;
219 kd
= &kd_softc
; /* XXX */
222 /* XXX This is for cons.c. */
223 if ((tp
->t_state
& TS_ISOPEN
) == 0)
226 (*tp
->t_linesw
->l_close
)(tp
, flag
);
229 if ((cc
= kd
->kd_in
) != NULL
)
230 (void)(*cc
->cc_iclose
)(cc
);
236 kdread(dev_t dev
, struct uio
*uio
, int flag
)
241 kd
= &kd_softc
; /* XXX */
244 return ((*tp
->t_linesw
->l_read
)(tp
, uio
, flag
));
248 kdwrite(dev_t dev
, struct uio
*uio
, int flag
)
253 kd
= &kd_softc
; /* XXX */
256 return ((*tp
->t_linesw
->l_write
)(tp
, uio
, flag
));
260 kdpoll(dev_t dev
, int events
, struct lwp
*l
)
265 kd
= &kd_softc
; /* XXX */
268 return ((*tp
->t_linesw
->l_poll
)(tp
, events
, l
));
272 kdioctl(dev_t dev
, u_long cmd
, void *data
, int flag
, struct lwp
*l
)
278 kd
= &kd_softc
; /* XXX */
281 error
= (*tp
->t_linesw
->l_ioctl
)(tp
, cmd
, data
, flag
, l
);
282 if (error
!= EPASSTHROUGH
)
285 error
= ttioctl(tp
, cmd
, data
, flag
, l
);
286 if (error
!= EPASSTHROUGH
)
289 /* Handle any ioctl commands specific to kbd/display. */
290 /* XXX - Send KB* ioctls to kbd module? */
291 /* XXX - Send FB* ioctls to fb module? */
297 kdparam(struct tty
*tp
, struct termios
*t
)
299 /* XXX - These are ignored... */
300 tp
->t_ispeed
= t
->c_ispeed
;
301 tp
->t_ospeed
= t
->c_ospeed
;
302 tp
->t_cflag
= t
->c_cflag
;
307 kdstart(struct tty
*tp
)
314 if (tp
->t_state
& (TS_BUSY
|TS_TTSTOP
|TS_TIMEOUT
))
320 tp
->t_state
|= TS_BUSY
;
322 /* called at level zero - update screen now. */
326 tp
->t_state
&= ~TS_BUSY
;
328 /* called at interrupt level - do it later */
329 callout_schedule(&tp
->t_rstrt_ch
, 0);
333 * This driver uses the PROM for writing the screen,
334 * and that only works if this is the console device.
335 * If this is not the console, just flush the output.
336 * Sorry. (In that case, use xdm instead of getty.)
338 ndflush(cl
, cl
->c_cc
);
348 * Timeout function to do delayed writes to the screen.
349 * Called at splsoftclock when requested by kdstart.
352 kd_later(void *tpaddr
)
354 struct tty
*tp
= tpaddr
;
360 tp
->t_state
&= ~TS_BUSY
;
361 (*tp
->t_linesw
->l_start
)(tp
);
366 * Put text on the screen using the PROM monitor.
367 * This can take a while, so to avoid missing
368 * interrupts, this is called at splsoftclock.
371 kd_putfb(struct tty
*tp
)
374 struct clist
*cl
= &tp
->t_outq
;
378 while ((len
= q_to_b(cl
, buf
, PUT_WSIZE
-1)) > 0) {
379 /* PROM will barf if high bits are set. */
384 /* Now let the PROM print it. */
385 prom_write(prom_stdout(), buf
, len
);
390 * Default PROM-based console input stream
392 static int kd_rom_iopen(struct cons_channel
*);
393 static int kd_rom_iclose(struct cons_channel
*);
395 static struct cons_channel prom_cons_channel
;
398 kd_rom_iopen(struct cons_channel
*cc
)
404 kd_rom_iclose(struct cons_channel
*cc
)
410 * Our "interrupt" routine for input. This is called by
411 * the keyboard driver (dev/sun/kbd.c) at spltty.
416 struct kd_softc
*kd
= &kd_softc
;
419 /* XXX: Make sure the device is open. */
423 if ((tp
->t_state
& TS_ISOPEN
) == 0)
426 (*tp
->t_linesw
->l_rint
)(c
, tp
);
430 /****************************************************************
432 ****************************************************************/
434 /* The debugger gets its own key translation state. */
435 static struct kbd_state
*kdcn_state
;
437 static void kdcnprobe(struct consdev
*);
438 static void kdcninit(struct consdev
*);
439 static void kdcnputc(dev_t
, int);
440 static void kdcnpollc(dev_t
, int);
442 /* The keyboard driver uses cn_hw to access the real console driver */
443 extern struct consdev consdev_prom
;
444 struct consdev consdev_kd
= {
445 .cn_probe
= kdcnprobe
,
449 .cn_pollc
= kdcnpollc
,
451 struct consdev
*cn_hw
= &consdev_kd
;
454 cons_attach_input(struct cons_channel
*cc
, struct consdev
*cn
)
456 struct kd_softc
*kd
= &kd_softc
;
457 struct kbd_softc
*kds
= cc
->cc_private
;
458 struct kbd_state
*ks
;
460 /* Share the keyboard state */
461 kdcn_state
= ks
= &kds
->k_state
;
464 cc
->cc_upstream
= kd_cons_input
;
466 /* Attach lower level. */
467 cn_hw
->cn_dev
= cn
->cn_dev
;
468 cn_hw
->cn_pollc
= cn
->cn_pollc
;
469 cn_hw
->cn_getc
= cn
->cn_getc
;
471 /* Attach us as console. */
472 cn_tab
->cn_dev
= makedev(cdevsw_lookup_major(&kd_cdevsw
), 0);
473 cn_tab
->cn_probe
= kdcnprobe
;
474 cn_tab
->cn_init
= kdcninit
;
475 cn_tab
->cn_getc
= kdcngetc
;
476 cn_tab
->cn_pollc
= kdcnpollc
;
477 cn_tab
->cn_pri
= CN_INTERNAL
;
479 /* Set up initial PROM input channel for /dev/console */
480 prom_cons_channel
.cc_private
= NULL
;
481 prom_cons_channel
.cc_iopen
= kd_rom_iopen
;
482 prom_cons_channel
.cc_iclose
= kd_rom_iclose
;
484 /* Indicate that it is OK to use the PROM fbwrite */
489 void kd_attach_input(struct cons_channel
*);
491 kd_attach_input(struct cons_channel
*cc
)
493 struct kd_softc
*kd
= &kd_softc
;
496 cc
->cc_upstream
= kd_cons_input
;
500 /* We never call this. */
502 kdcnprobe(struct consdev
*cn
)
507 kdcninit(struct consdev
*cn
)
510 struct kbd_state
*ks
= kdcn_state
;
512 cn
->cn_dev
= makedev(cdevsw_lookup_major(&kd_cdevsw
), 0);
513 cn
->cn_pri
= CN_INTERNAL
;
515 /* This prepares kbd_translate() */
516 ks
->kbd_id
= KBD_MIN_TYPE
;
519 /* Set up initial PROM input channel for /dev/console */
520 prom_cons_channel
.cc_private
= NULL
;
521 prom_cons_channel
.cc_iopen
= kd_rom_iopen
;
522 prom_cons_channel
.cc_iclose
= kd_rom_iclose
;
523 cons_attach_input(&prom_cons_channel
);
525 /* Indicate that it is OK to use the PROM fbwrite */
533 struct kbd_state
*ks
= kdcn_state
;
534 int code
, class, data
, keysym
;
535 extern int prom_cngetc(dev_t
);
538 if (cn_hw
->cn_getc
== prom_cngetc
) return (*cn_hw
->cn_getc
)(dev
);
540 code
= (*cn_hw
->cn_getc
)(dev
);
541 keysym
= kbd_code_to_keysym(ks
, code
);
542 class = KEYSYM_CLASS(keysym
);
550 data
= (keysym
& 0x1F);
551 /* Only allow ctrl or shift. */
552 if (data
> KBMOD_SHIFT_R
)
555 if (class == KEYSYM_SETMOD
)
556 ks
->kbd_modbits
|= data
;
558 ks
->kbd_modbits
&= ~data
;
562 /* No toggle keys here. */
566 default: /* ignore all other keysyms */
575 kdcnputc(dev_t dev
, int c
)
578 char c0
= (c
& 0x7f);
581 prom_write(prom_stdout(), &c0
, 1);
586 kdcnpollc(dev_t dev
, int on
)
588 struct kbd_state
*ks
= kdcn_state
;
591 /* Entering debugger. */
595 /* Clear shift keys too. */
598 /* Resuming kernel. */
600 (*cn_hw
->cn_pollc
)(dev
, on
);