No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / sparc64 / dev / kd.c
blobcc7a9778445f33753dc008a7aa66aa2857ad565b
1 /* $NetBSD: kd.c,v 1.48 2008/03/30 01:41:36 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.48 2008/03/30 01:41:36 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/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>
61 #include <dev/cons.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);
72 dev_type_tty(kdtty);
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 */
82 #define PUT_WSIZE 64
84 struct kd_softc {
85 struct device kd_dev; /* required first: base device */
86 struct tty *kd_tty;
87 int rows, cols;
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?
114 void
115 kd_init(struct kd_softc *kd)
117 struct tty *tp;
118 char prop[6+1];
120 kd = &kd_softc; /* XXX */
122 tp = ttymalloc();
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);
128 tty_attach(tp);
129 kd->kd_tty = tp;
132 * get the console struct winsize.
134 if (kd_is_console) {
135 fbconstty = tp;
138 if (kd->rows == 0 &&
139 prom_getoption("screen-#rows", prop, sizeof prop) == 0)
140 kd->rows = strtoul(prop, NULL, 10);
142 if (kd->cols == 0 &&
143 prom_getoption("screen-#columns", prop, sizeof prop) == 0)
144 kd->cols = strtoul(prop, NULL, 10);
147 struct tty *
148 kdtty(dev_t dev)
150 struct kd_softc *kd;
152 kd = &kd_softc; /* XXX */
153 return (kd->kd_tty);
157 kdopen(dev_t dev, int flag, int mode, struct lwp *l)
159 struct kd_softc *kd;
160 int error, s, unit;
161 struct tty *tp;
162 static int firstopen = 1;
164 unit = minor(dev);
165 if (unit != 0)
166 return ENXIO;
167 kd = &kd_softc; /* XXX */
169 if (firstopen) {
170 kd_init(kd);
171 firstopen = 0;
173 tp = kd->kd_tty;
175 /* It's simpler to do this up here. */
176 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
177 return (EBUSY);
179 s = spltty();
181 if ((tp->t_state & TS_ISOPEN) == 0) {
182 /* First open. */
184 /* Notify the input device that serves us */
185 struct cons_channel *cc = kd->kd_in;
186 if (cc != NULL &&
187 (error = (*cc->cc_iopen)(cc)) != 0) {
188 splx(s);
189 return (error);
192 ttychars(tp);
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);
199 ttsetwater(tp);
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;
207 splx(s);
209 return ((*tp->t_linesw->l_open)(dev, tp));
213 kdclose(dev_t dev, int flag, int mode, struct lwp *l)
215 struct kd_softc *kd;
216 struct tty *tp;
217 struct cons_channel *cc;
219 kd = &kd_softc; /* XXX */
220 tp = kd->kd_tty;
222 /* XXX This is for cons.c. */
223 if ((tp->t_state & TS_ISOPEN) == 0)
224 return 0;
226 (*tp->t_linesw->l_close)(tp, flag);
227 ttyclose(tp);
229 if ((cc = kd->kd_in) != NULL)
230 (void)(*cc->cc_iclose)(cc);
232 return (0);
236 kdread(dev_t dev, struct uio *uio, int flag)
238 struct kd_softc *kd;
239 struct tty *tp;
241 kd = &kd_softc; /* XXX */
242 tp = kd->kd_tty;
244 return ((*tp->t_linesw->l_read)(tp, uio, flag));
248 kdwrite(dev_t dev, struct uio *uio, int flag)
250 struct kd_softc *kd;
251 struct tty *tp;
253 kd = &kd_softc; /* XXX */
254 tp = kd->kd_tty;
256 return ((*tp->t_linesw->l_write)(tp, uio, flag));
260 kdpoll(dev_t dev, int events, struct lwp *l)
262 struct kd_softc *kd;
263 struct tty *tp;
265 kd = &kd_softc; /* XXX */
266 tp = kd->kd_tty;
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)
274 struct kd_softc *kd;
275 struct tty *tp;
276 int error;
278 kd = &kd_softc; /* XXX */
279 tp = kd->kd_tty;
281 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
282 if (error != EPASSTHROUGH)
283 return error;
285 error = ttioctl(tp, cmd, data, flag, l);
286 if (error != EPASSTHROUGH)
287 return error;
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? */
293 return EPASSTHROUGH;
296 static int
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;
303 return 0;
306 static void
307 kdstart(struct tty *tp)
309 struct clist *cl;
310 int s1, s2;
312 s1 = splsoftclock();
313 s2 = spltty();
314 if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
315 goto out;
317 cl = &tp->t_outq;
318 if (cl->c_cc) {
319 if (kd_is_console) {
320 tp->t_state |= TS_BUSY;
321 if (s1 == 0) {
322 /* called at level zero - update screen now. */
323 splx(s2);
324 kd_putfb(tp);
325 s2 = spltty();
326 tp->t_state &= ~TS_BUSY;
327 } else {
328 /* called at interrupt level - do it later */
329 callout_schedule(&tp->t_rstrt_ch, 0);
331 } else {
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);
341 ttypull(tp);
342 out:
343 splx(s2);
344 splx(s1);
348 * Timeout function to do delayed writes to the screen.
349 * Called at splsoftclock when requested by kdstart.
351 static void
352 kd_later(void *tpaddr)
354 struct tty *tp = tpaddr;
355 register int s;
357 kd_putfb(tp);
359 s = spltty();
360 tp->t_state &= ~TS_BUSY;
361 (*tp->t_linesw->l_start)(tp);
362 splx(s);
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.
370 static void
371 kd_putfb(struct tty *tp)
373 char buf[PUT_WSIZE];
374 struct clist *cl = &tp->t_outq;
375 char *p, *end;
376 int len;
378 while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
379 /* PROM will barf if high bits are set. */
380 p = buf;
381 end = buf + len;
382 while (p < end)
383 *p++ &= 0x7f;
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)
400 return (0);
404 kd_rom_iclose(struct cons_channel *cc)
406 return (0);
410 * Our "interrupt" routine for input. This is called by
411 * the keyboard driver (dev/sun/kbd.c) at spltty.
413 void
414 kd_cons_input(int c)
416 struct kd_softc *kd = &kd_softc;
417 struct tty *tp;
419 /* XXX: Make sure the device is open. */
420 tp = kd->kd_tty;
421 if (tp == NULL)
422 return;
423 if ((tp->t_state & TS_ISOPEN) == 0)
424 return;
426 (*tp->t_linesw->l_rint)(c, tp);
430 /****************************************************************
431 * kd console support
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,
446 .cn_init = kdcninit,
447 .cn_getc = kdcngetc,
448 .cn_putc = kdcnputc,
449 .cn_pollc = kdcnpollc,
451 struct consdev *cn_hw = &consdev_kd;
453 void
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;
463 kd->kd_in = cc;
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 */
485 kd_is_console = 1;
489 void kd_attach_input(struct cons_channel *);
490 void
491 kd_attach_input(struct cons_channel *cc)
493 struct kd_softc *kd = &kd_softc;
495 kd->kd_in = cc;
496 cc->cc_upstream = kd_cons_input;
500 /* We never call this. */
501 static void
502 kdcnprobe(struct consdev *cn)
506 static void
507 kdcninit(struct consdev *cn)
509 #if 0
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;
517 kbd_xlate_init(ks);
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 */
526 kd_is_console = 1;
527 #endif
530 static int
531 kdcngetc(dev_t dev)
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);
539 for (;;) {
540 code = (*cn_hw->cn_getc)(dev);
541 keysym = kbd_code_to_keysym(ks, code);
542 class = KEYSYM_CLASS(keysym);
544 switch (class) {
545 case KEYSYM_ASCII:
546 goto out;
548 case KEYSYM_CLRMOD:
549 case KEYSYM_SETMOD:
550 data = (keysym & 0x1F);
551 /* Only allow ctrl or shift. */
552 if (data > KBMOD_SHIFT_R)
553 break;
554 data = 1 << data;
555 if (class == KEYSYM_SETMOD)
556 ks->kbd_modbits |= data;
557 else
558 ks->kbd_modbits &= ~data;
559 break;
561 case KEYSYM_ALL_UP:
562 /* No toggle keys here. */
563 ks->kbd_modbits = 0;
564 break;
566 default: /* ignore all other keysyms */
567 break;
570 out:
571 return (keysym);
574 static void
575 kdcnputc(dev_t dev, int c)
577 int s;
578 char c0 = (c & 0x7f);
580 s = splhigh();
581 prom_write(prom_stdout(), &c0, 1);
582 splx(s);
585 static void
586 kdcnpollc(dev_t dev, int on)
588 struct kbd_state *ks = kdcn_state;
590 if (on) {
591 /* Entering debugger. */
592 #if NFB > 0
593 fb_unblank();
594 #endif
595 /* Clear shift keys too. */
596 ks->kbd_modbits = 0;
597 } else {
598 /* Resuming kernel. */
600 (*cn_hw->cn_pollc)(dev, on);