kd.c revision 1.45 1 /* $NetBSD: kd.c,v 1.45 2007/10/18 18:54:58 joerg Exp $ */
2
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gordon W. Ross.
9 *
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.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Keyboard/Display device.
41 *
42 * This driver exists simply to provide a tty device that
43 * the indirect console driver can point to.
44 * The kbd driver sends its input here.
45 * Output goes to the screen via PROM printf.
46 */
47
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: kd.c,v 1.45 2007/10/18 18:54:58 joerg Exp $");
50
51 #include <sys/param.h>
52 #include <sys/proc.h>
53 #include <sys/systm.h>
54 #include <sys/ioctl.h>
55 #include <sys/tty.h>
56 #include <sys/file.h>
57 #include <sys/conf.h>
58 #include <sys/device.h>
59 #include <sys/kauth.h>
60
61 #include <machine/openfirm.h>
62 #include <machine/eeprom.h>
63 #include <machine/psl.h>
64 #include <machine/cpu.h>
65 #include <machine/kbd.h>
66 #include <machine/autoconf.h>
67
68 #include <dev/cons.h>
69 #include <dev/sun/event_var.h>
70 #include <dev/sun/kbd_xlate.h>
71 #include <dev/sun/kbdvar.h>
72 #include <sparc64/dev/cons.h>
73
74 dev_type_open(kdopen);
75 dev_type_close(kdclose);
76 dev_type_read(kdread);
77 dev_type_write(kdwrite);
78 dev_type_ioctl(kdioctl);
79 dev_type_tty(kdtty);
80 dev_type_poll(kdpoll);
81
82 const struct cdevsw kd_cdevsw = {
83 kdopen, kdclose, kdread, kdwrite, kdioctl,
84 nostop, kdtty, kdpoll, nommap, ttykqfilter, D_TTY
85 };
86
87 struct tty *fbconstty = 0; /* tty structure for frame buffer console */
88
89 #define PUT_WSIZE 64
90
91 struct kd_softc {
92 struct device kd_dev; /* required first: base device */
93 struct tty *kd_tty;
94 int rows, cols;
95
96 /* Console input hook */
97 struct cons_channel *kd_in;
98 };
99
100 /*
101 * There is no point in pretending there might be
102 * more than one keyboard/display device.
103 */
104 static struct kd_softc kd_softc;
105 static int kd_is_console;
106
107 static int kdparam(struct tty *, struct termios *);
108 static void kdstart(struct tty *);
109 static void kd_init(struct kd_softc *);
110 static void kd_cons_input(int);
111 static int kdcngetc(dev_t);
112
113 int cons_ocount; /* output byte count */
114
115 /*
116 * This is called by kbd_attach()
117 * XXX - Make this a proper child of kbd?
118 */
119 void
120 kd_init(struct kd_softc *kd)
121 {
122 struct tty *tp;
123 char prop[6+1];
124
125 kd = &kd_softc; /* XXX */
126
127 tp = ttymalloc();
128 callout_setfunc(&tp->t_rstrt_ch, kd_later, tp);
129 tp->t_oproc = kdstart;
130 tp->t_param = kdparam;
131 tp->t_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
132
133 tty_attach(tp);
134 kd->kd_tty = tp;
135
136 /*
137 * get the console struct winsize.
138 */
139 if (kd_is_console) {
140 fbconstty = tp;
141 }
142
143 if (kd->rows == 0 &&
144 prom_getoption("screen-#rows", prop, sizeof prop) == 0)
145 kd->rows = strtoul(prop, NULL, 10);
146
147 if (kd->cols == 0 &&
148 prom_getoption("screen-#columns", prop, sizeof prop) == 0)
149 kd->cols = strtoul(prop, NULL, 10);
150 }
151
152 struct tty *
153 kdtty(dev_t dev)
154 {
155 struct kd_softc *kd;
156
157 kd = &kd_softc; /* XXX */
158 return (kd->kd_tty);
159 }
160
161 int
162 kdopen(dev_t dev, int flag, int mode, struct lwp *l)
163 {
164 struct kd_softc *kd;
165 int error, s, unit;
166 struct tty *tp;
167 static int firstopen = 1;
168
169 unit = minor(dev);
170 if (unit != 0)
171 return ENXIO;
172 kd = &kd_softc; /* XXX */
173
174 if (firstopen) {
175 kd_init(kd);
176 firstopen = 0;
177 }
178 tp = kd->kd_tty;
179
180 /* It's simpler to do this up here. */
181 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
182 return (EBUSY);
183
184 s = spltty();
185
186 if ((tp->t_state & TS_ISOPEN) == 0) {
187 /* First open. */
188
189 /* Notify the input device that serves us */
190 struct cons_channel *cc = kd->kd_in;
191 if (cc != NULL &&
192 (error = (*cc->cc_iopen)(cc)) != 0) {
193 splx(s);
194 return (error);
195 }
196
197 ttychars(tp);
198 tp->t_iflag = TTYDEF_IFLAG;
199 tp->t_oflag = TTYDEF_OFLAG;
200 tp->t_cflag = TTYDEF_CFLAG;
201 tp->t_lflag = TTYDEF_LFLAG;
202 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
203 (void) kdparam(tp, &tp->t_termios);
204 ttsetwater(tp);
205 tp->t_winsize.ws_row = kd->rows;
206 tp->t_winsize.ws_col = kd->cols;
207 /* Flush pending input? Clear translator? */
208 /* This (pseudo)device always has SOFTCAR */
209 tp->t_state |= TS_CARR_ON;
210 }
211
212 splx(s);
213
214 return ((*tp->t_linesw->l_open)(dev, tp));
215 }
216
217 int
218 kdclose(dev_t dev, int flag, int mode, struct lwp *l)
219 {
220 struct kd_softc *kd;
221 struct tty *tp;
222 struct cons_channel *cc;
223
224 kd = &kd_softc; /* XXX */
225 tp = kd->kd_tty;
226
227 /* XXX This is for cons.c. */
228 if ((tp->t_state & TS_ISOPEN) == 0)
229 return 0;
230
231 (*tp->t_linesw->l_close)(tp, flag);
232 ttyclose(tp);
233
234 if ((cc = kd->kd_in) != NULL)
235 (void)(*cc->cc_iclose)(cc);
236
237 return (0);
238 }
239
240 int
241 kdread(dev_t dev, struct uio *uio, int flag)
242 {
243 struct kd_softc *kd;
244 struct tty *tp;
245
246 kd = &kd_softc; /* XXX */
247 tp = kd->kd_tty;
248
249 return ((*tp->t_linesw->l_read)(tp, uio, flag));
250 }
251
252 int
253 kdwrite(dev_t dev, struct uio *uio, int flag)
254 {
255 struct kd_softc *kd;
256 struct tty *tp;
257
258 kd = &kd_softc; /* XXX */
259 tp = kd->kd_tty;
260
261 return ((*tp->t_linesw->l_write)(tp, uio, flag));
262 }
263
264 int
265 kdpoll(dev_t dev, int events, struct lwp *l)
266 {
267 struct kd_softc *kd;
268 struct tty *tp;
269
270 kd = &kd_softc; /* XXX */
271 tp = kd->kd_tty;
272
273 return ((*tp->t_linesw->l_poll)(tp, events, l));
274 }
275
276 int
277 kdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
278 {
279 struct kd_softc *kd;
280 struct tty *tp;
281 int error;
282
283 kd = &kd_softc; /* XXX */
284 tp = kd->kd_tty;
285
286 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
287 if (error != EPASSTHROUGH)
288 return error;
289
290 error = ttioctl(tp, cmd, data, flag, l);
291 if (error != EPASSTHROUGH)
292 return error;
293
294 /* Handle any ioctl commands specific to kbd/display. */
295 /* XXX - Send KB* ioctls to kbd module? */
296 /* XXX - Send FB* ioctls to fb module? */
297
298 return EPASSTHROUGH;
299 }
300
301 static int
302 kdparam(struct tty *tp, struct termios *t)
303 {
304 /* XXX - These are ignored... */
305 tp->t_ispeed = t->c_ispeed;
306 tp->t_ospeed = t->c_ospeed;
307 tp->t_cflag = t->c_cflag;
308 return 0;
309 }
310
311
312 static void kd_later(void*);
313 static void kd_putfb(struct tty *);
314
315 static void
316 kdstart(struct tty *tp)
317 {
318 struct clist *cl;
319 int s1, s2;
320
321 s1 = splsoftclock();
322 s2 = spltty();
323 if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
324 goto out;
325
326 cl = &tp->t_outq;
327 if (cl->c_cc) {
328 if (kd_is_console) {
329 tp->t_state |= TS_BUSY;
330 if (s1 == 0) {
331 /* called at level zero - update screen now. */
332 splx(s2);
333 kd_putfb(tp);
334 s2 = spltty();
335 tp->t_state &= ~TS_BUSY;
336 } else {
337 /* called at interrupt level - do it later */
338 callout_schedule(&tp->t_rstrt_ch, 0);
339 }
340 } else {
341 /*
342 * This driver uses the PROM for writing the screen,
343 * and that only works if this is the console device.
344 * If this is not the console, just flush the output.
345 * Sorry. (In that case, use xdm instead of getty.)
346 */
347 ndflush(cl, cl->c_cc);
348 }
349 }
350 if (cl->c_cc <= tp->t_lowat) {
351 if (tp->t_state & TS_ASLEEP) {
352 tp->t_state &= ~TS_ASLEEP;
353 wakeup((void *)cl);
354 }
355 selwakeup(&tp->t_wsel);
356 }
357 out:
358 splx(s2);
359 splx(s1);
360 }
361
362 /*
363 * Timeout function to do delayed writes to the screen.
364 * Called at splsoftclock when requested by kdstart.
365 */
366 static void
367 kd_later(void *tpaddr)
368 {
369 struct tty *tp = tpaddr;
370 register int s;
371
372 kd_putfb(tp);
373
374 s = spltty();
375 tp->t_state &= ~TS_BUSY;
376 (*tp->t_linesw->l_start)(tp);
377 splx(s);
378 }
379
380 /*
381 * Put text on the screen using the PROM monitor.
382 * This can take a while, so to avoid missing
383 * interrupts, this is called at splsoftclock.
384 */
385 static void
386 kd_putfb(struct tty *tp)
387 {
388 char buf[PUT_WSIZE];
389 struct clist *cl = &tp->t_outq;
390 char *p, *end;
391 int len;
392
393 while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
394 /* PROM will barf if high bits are set. */
395 p = buf;
396 end = buf + len;
397 while (p < end)
398 *p++ &= 0x7f;
399 /* Now let the PROM print it. */
400 prom_write(prom_stdout(), buf, len);
401 }
402 }
403
404 /*
405 * Default PROM-based console input stream
406 */
407 static int kd_rom_iopen(struct cons_channel *);
408 static int kd_rom_iclose(struct cons_channel *);
409
410 static struct cons_channel prom_cons_channel;
411
412 int
413 kd_rom_iopen(struct cons_channel *cc)
414 {
415 return (0);
416 }
417
418 int
419 kd_rom_iclose(struct cons_channel *cc)
420 {
421 return (0);
422 }
423
424 /*
425 * Our "interrupt" routine for input. This is called by
426 * the keyboard driver (dev/sun/kbd.c) at spltty.
427 */
428 void
429 kd_cons_input(int c)
430 {
431 struct kd_softc *kd = &kd_softc;
432 struct tty *tp;
433
434 /* XXX: Make sure the device is open. */
435 tp = kd->kd_tty;
436 if (tp == NULL)
437 return;
438 if ((tp->t_state & TS_ISOPEN) == 0)
439 return;
440
441 (*tp->t_linesw->l_rint)(c, tp);
442 }
443
444
445 /****************************************************************
446 * kd console support
447 ****************************************************************/
448
449 /* The debugger gets its own key translation state. */
450 static struct kbd_state *kdcn_state;
451
452 static void kdcnprobe(struct consdev *);
453 static void kdcninit(struct consdev *);
454 static void kdcnputc(dev_t, int);
455 static void kdcnpollc(dev_t, int);
456
457 /* The keyboard driver uses cn_hw to access the real console driver */
458 extern struct consdev consdev_prom;
459 struct consdev consdev_kd = {
460 .cn_probe = kdcnprobe,
461 .cn_init = kdcninit,
462 .cn_getc = kdcngetc,
463 .cn_putc = kdcnputc,
464 .cn_pollc = kdcnpollc,
465 };
466 struct consdev *cn_hw = &consdev_kd;
467
468 void
469 cons_attach_input(struct cons_channel *cc, struct consdev *cn)
470 {
471 struct kd_softc *kd = &kd_softc;
472 struct kbd_softc *kds = cc->cc_dev;
473 struct kbd_state *ks;
474
475 /* Share the keyboard state */
476 kdcn_state = ks = &kds->k_state;
477
478 kd->kd_in = cc;
479 cc->cc_upstream = kd_cons_input;
480
481 /* Attach lower level. */
482 cn_hw->cn_dev = cn->cn_dev;
483 cn_hw->cn_pollc = cn->cn_pollc;
484 cn_hw->cn_getc = cn->cn_getc;
485
486 /* Attach us as console. */
487 cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
488 cn_tab->cn_probe = kdcnprobe;
489 cn_tab->cn_init = kdcninit;
490 cn_tab->cn_getc = kdcngetc;
491 cn_tab->cn_pollc = kdcnpollc;
492 cn_tab->cn_pri = CN_INTERNAL;
493
494 /* Set up initial PROM input channel for /dev/console */
495 prom_cons_channel.cc_dev = NULL;
496 prom_cons_channel.cc_iopen = kd_rom_iopen;
497 prom_cons_channel.cc_iclose = kd_rom_iclose;
498
499 /* Indicate that it is OK to use the PROM fbwrite */
500 kd_is_console = 1;
501 }
502
503
504 void kd_attach_input(struct cons_channel *);
505 void
506 kd_attach_input(struct cons_channel *cc)
507 {
508 struct kd_softc *kd = &kd_softc;
509
510 kd->kd_in = cc;
511 cc->cc_upstream = kd_cons_input;
512 }
513
514
515 /* We never call this. */
516 static void
517 kdcnprobe(struct consdev *cn)
518 {
519 }
520
521 static void
522 kdcninit(struct consdev *cn)
523 {
524 #if 0
525 struct kbd_state *ks = kdcn_state;
526
527 cn->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
528 cn->cn_pri = CN_INTERNAL;
529
530 /* This prepares kbd_translate() */
531 ks->kbd_id = KBD_MIN_TYPE;
532 kbd_xlate_init(ks);
533
534 /* Set up initial PROM input channel for /dev/console */
535 prom_cons_channel.cc_dev = NULL;
536 prom_cons_channel.cc_iopen = kd_rom_iopen;
537 prom_cons_channel.cc_iclose = kd_rom_iclose;
538 cons_attach_input(&prom_cons_channel);
539
540 /* Indicate that it is OK to use the PROM fbwrite */
541 kd_is_console = 1;
542 #endif
543 }
544
545 static int
546 kdcngetc(dev_t dev)
547 {
548 struct kbd_state *ks = kdcn_state;
549 int code, class, data, keysym;
550 extern int prom_cngetc(dev_t);
551
552
553 if (cn_hw->cn_getc == prom_cngetc) return (*cn_hw->cn_getc)(dev);
554 for (;;) {
555 code = (*cn_hw->cn_getc)(dev);
556 keysym = kbd_code_to_keysym(ks, code);
557 class = KEYSYM_CLASS(keysym);
558
559 switch (class) {
560 case KEYSYM_ASCII:
561 goto out;
562
563 case KEYSYM_CLRMOD:
564 case KEYSYM_SETMOD:
565 data = (keysym & 0x1F);
566 /* Only allow ctrl or shift. */
567 if (data > KBMOD_SHIFT_R)
568 break;
569 data = 1 << data;
570 if (class == KEYSYM_SETMOD)
571 ks->kbd_modbits |= data;
572 else
573 ks->kbd_modbits &= ~data;
574 break;
575
576 case KEYSYM_ALL_UP:
577 /* No toggle keys here. */
578 ks->kbd_modbits = 0;
579 break;
580
581 default: /* ignore all other keysyms */
582 break;
583 }
584 }
585 out:
586 return (keysym);
587 }
588
589 static void
590 kdcnputc(dev_t dev, int c)
591 {
592 int s;
593 char c0 = (c & 0x7f);
594
595 s = splhigh();
596 prom_write(prom_stdout(), &c0, 1);
597 splx(s);
598 }
599
600 static void
601 kdcnpollc(dev_t dev, int on)
602 {
603 struct kbd_state *ks = kdcn_state;
604
605 if (on) {
606 /* Entering debugger. */
607 #if NFB > 0
608 fb_unblank();
609 #endif
610 /* Clear shift keys too. */
611 ks->kbd_modbits = 0;
612 } else {
613 /* Resuming kernel. */
614 }
615 (*cn_hw->cn_pollc)(dev, on);
616 }
617