kd.c revision 1.46 1 /* $NetBSD: kd.c,v 1.46 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 * Console driver based on PROM primitives.
41 *
42 * This driver exists to provide a tty device that the indirect
43 * console driver can point to. It also provides a hook that
44 * allows another device to serve console input. This will normally
45 * be a keyboard driver (see sys/dev/sun/kbd.c)
46 */
47
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: kd.c,v 1.46 2007/10/18 18:54:58 joerg Exp $");
50
51 #include "opt_kgdb.h"
52 #include "fb.h"
53
54 #include <sys/param.h>
55 #include <sys/proc.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/ioctl.h>
59 #include <sys/tty.h>
60 #include <sys/file.h>
61 #include <sys/conf.h>
62 #include <sys/device.h>
63 #include <sys/kauth.h>
64
65 #include <machine/bsd_openprom.h>
66 #include <machine/promlib.h>
67 #include <machine/eeprom.h>
68 #include <machine/psl.h>
69 #include <machine/cpu.h>
70 #include <machine/kbd.h>
71 #include <machine/autoconf.h>
72
73 #if defined(RASTERCONSOLE) && NFB > 0
74 #include <dev/sun/fbio.h>
75 #include <dev/sun/fbvar.h>
76 #endif
77
78 #include <dev/cons.h>
79 #include <sparc/dev/cons.h>
80
81 #include <dev/sun/event_var.h>
82 #include <dev/sun/kbd_xlate.h>
83 #include <dev/sun/kbdvar.h>
84
85 #define PUT_WSIZE 64
86
87 struct kd_softc {
88 struct device kd_dev; /* required first: base device */
89 struct tty *kd_tty;
90 int rows, cols;
91
92 /* Console input hook */
93 struct cons_channel *kd_in;
94 };
95
96 /*
97 * There is no point in pretending there might be
98 * more than one keyboard/display device.
99 */
100 static struct kd_softc kd_softc;
101
102 /* For keyboard driver to register itself as console input */
103 void kd_attach_input(struct cons_channel *);
104
105 static void kd_init(struct kd_softc *);
106 static void kdstart(struct tty *);
107 static void kd_later(void *);
108 static void kd_putfb(struct tty *);
109 static int kdparam(struct tty *, struct termios *);
110 static void kd_cons_input(int);
111
112 dev_type_open(kdopen);
113 dev_type_close(kdclose);
114 dev_type_read(kdread);
115 dev_type_write(kdwrite);
116 dev_type_ioctl(kdioctl);
117 dev_type_tty(kdtty);
118 dev_type_poll(kdpoll);
119
120 const struct cdevsw kd_cdevsw = {
121 kdopen, kdclose, kdread, kdwrite, kdioctl,
122 nostop, kdtty, kdpoll, nommap, ttykqfilter, D_TTY
123 };
124
125 /*
126 * Prepare the console tty; called on first open of /dev/console
127 */
128 static void
129 kd_init(struct kd_softc *kd)
130 {
131 struct tty *tp;
132
133 tp = ttymalloc();
134 callout_setfunc(&tp->t_rstrt_ch, kd_later, tp);
135 tp->t_oproc = kdstart;
136 tp->t_param = kdparam;
137 tp->t_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
138
139 tty_attach(tp);
140 kd->kd_tty = tp;
141
142 /*
143 * Get the console struct winsize.
144 */
145 #if defined(RASTERCONSOLE) && NFB > 0
146 /* If the raster console driver is attached, copy its size */
147 kd->rows = fbrcons_rows();
148 kd->cols = fbrcons_cols();
149 rcons_ttyinit(tp);
150 #endif
151
152 /* else, consult the PROM */
153 switch (prom_version()) {
154 char prop[6+1]; /* Enough for six digits */
155 struct eeprom *ep;
156 case PROM_OLDMON:
157 if ((ep = (struct eeprom *)eeprom_va) == NULL)
158 break;
159 if (kd->rows == 0)
160 kd->rows = (u_short)ep->eeTtyRows;
161 if (kd->cols == 0)
162 kd->cols = (u_short)ep->eeTtyCols;
163 break;
164
165 case PROM_OBP_V0:
166 case PROM_OBP_V2:
167 case PROM_OBP_V3:
168 case PROM_OPENFIRM:
169 if (kd->rows == 0 &&
170 prom_getoption("screen-#rows", prop, sizeof prop) == 0)
171 kd->rows = strtoul(prop, NULL, 10);
172
173 if (kd->cols == 0 &&
174 prom_getoption("screen-#columns", prop, sizeof prop) == 0)
175 kd->cols = strtoul(prop, NULL, 10);
176
177 break;
178 }
179
180 return;
181 }
182
183 struct tty *
184 kdtty(dev_t dev)
185 {
186 struct kd_softc *kd;
187
188 kd = &kd_softc; /* XXX */
189 return (kd->kd_tty);
190 }
191
192 int
193 kdopen(dev_t dev, int flag, int mode, struct lwp *l)
194 {
195 struct kd_softc *kd;
196 int error, s, unit;
197 struct tty *tp;
198 static int firstopen = 1;
199
200 unit = minor(dev);
201 if (unit != 0)
202 return ENXIO;
203
204 kd = &kd_softc; /* XXX */
205 if (firstopen) {
206 kd_init(kd);
207 firstopen = 0;
208 }
209
210 tp = kd->kd_tty;
211
212 /* It's simpler to do this up here. */
213 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
214 return (EBUSY);
215
216 s = spltty();
217 if ((tp->t_state & TS_ISOPEN) == 0) {
218 /* First open. */
219
220 /* Notify the input device that serves us */
221 struct cons_channel *cc = kd->kd_in;
222 if (cc != NULL &&
223 (error = (*cc->cc_iopen)(cc)) != 0) {
224 return (error);
225 }
226
227 ttychars(tp);
228 tp->t_iflag = TTYDEF_IFLAG;
229 tp->t_oflag = TTYDEF_OFLAG;
230 tp->t_cflag = TTYDEF_CFLAG;
231 tp->t_lflag = TTYDEF_LFLAG;
232 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
233 (void) kdparam(tp, &tp->t_termios);
234 ttsetwater(tp);
235 tp->t_winsize.ws_row = kd->rows;
236 tp->t_winsize.ws_col = kd->cols;
237 /* Flush pending input? Clear translator? */
238 /* This (pseudo)device always has SOFTCAR */
239 tp->t_state |= TS_CARR_ON;
240 }
241
242 splx(s);
243
244 return ((*tp->t_linesw->l_open)(dev, tp));
245 }
246
247 int
248 kdclose(dev_t dev, int flag, int mode, struct lwp *l)
249 {
250 struct kd_softc *kd;
251 struct tty *tp;
252 struct cons_channel *cc;
253
254 kd = &kd_softc; /* XXX */
255 tp = kd->kd_tty;
256
257 /* XXX This is for cons.c. */
258 if ((tp->t_state & TS_ISOPEN) == 0)
259 return 0;
260
261 (*tp->t_linesw->l_close)(tp, flag);
262 ttyclose(tp);
263
264 if ((cc = kd->kd_in) != NULL)
265 (void)(*cc->cc_iclose)(cc);
266
267 return (0);
268 }
269
270 int
271 kdread(dev_t dev, struct uio *uio, int flag)
272 {
273 struct kd_softc *kd;
274 struct tty *tp;
275
276 kd = &kd_softc; /* XXX */
277 tp = kd->kd_tty;
278
279 return ((*tp->t_linesw->l_read)(tp, uio, flag));
280 }
281
282 int
283 kdwrite(dev_t dev, struct uio *uio, int flag)
284 {
285 struct kd_softc *kd;
286 struct tty *tp;
287
288 kd = &kd_softc; /* XXX */
289 tp = kd->kd_tty;
290
291 return ((*tp->t_linesw->l_write)(tp, uio, flag));
292 }
293
294 int
295 kdpoll(dev_t dev, int events, struct lwp *l)
296 {
297 struct kd_softc *kd;
298 struct tty *tp;
299
300 kd = &kd_softc; /* XXX */
301 tp = kd->kd_tty;
302
303 return ((*tp->t_linesw->l_poll)(tp, events, l));
304 }
305
306 int
307 kdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
308 {
309 struct kd_softc *kd;
310 struct tty *tp;
311 int error;
312
313 kd = &kd_softc; /* XXX */
314 tp = kd->kd_tty;
315
316 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
317 if (error != EPASSTHROUGH)
318 return error;
319
320 error = ttioctl(tp, cmd, data, flag, l);
321 if (error != EPASSTHROUGH)
322 return error;
323
324 /* Handle any ioctl commands specific to kbd/display. */
325 /* XXX - Send KB* ioctls to kbd module? */
326 /* XXX - Send FB* ioctls to fb module? */
327
328 return EPASSTHROUGH;
329 }
330
331 static int
332 kdparam(struct tty *tp, struct termios *t)
333 {
334
335 /* XXX - These are ignored... */
336 tp->t_ispeed = t->c_ispeed;
337 tp->t_ospeed = t->c_ospeed;
338 tp->t_cflag = t->c_cflag;
339 return 0;
340 }
341
342 static void
343 kdstart(struct tty *tp)
344 {
345 struct clist *cl;
346 int s1, s2;
347
348 s1 = splsoftclock();
349 s2 = spltty();
350 if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
351 goto out;
352
353 cl = &tp->t_outq;
354 if (cl->c_cc) {
355 tp->t_state |= TS_BUSY;
356 if ((s1 & PSR_PIL) == 0) {
357 /* called at level zero - update screen now. */
358 splx(s2);
359 kd_putfb(tp);
360 s2 = spltty();
361 tp->t_state &= ~TS_BUSY;
362 } else {
363 /* called at interrupt level - do it later */
364 callout_schedule(&tp->t_rstrt_ch, 0);
365 }
366 }
367 if (cl->c_cc <= tp->t_lowat) {
368 if (tp->t_state & TS_ASLEEP) {
369 tp->t_state &= ~TS_ASLEEP;
370 wakeup((void *)cl);
371 }
372 selwakeup(&tp->t_wsel);
373 }
374 out:
375 splx(s2);
376 splx(s1);
377 }
378
379 /*
380 * Timeout function to do delayed writes to the screen.
381 * Called at splsoftclock when requested by kdstart.
382 */
383 static void
384 kd_later(void *arg)
385 {
386 struct tty *tp = arg;
387 int s;
388
389 kd_putfb(tp);
390
391 s = spltty();
392 tp->t_state &= ~TS_BUSY;
393 (*tp->t_linesw->l_start)(tp);
394 splx(s);
395 }
396
397 /*
398 * Put text on the screen using the PROM monitor.
399 * This can take a while, so to avoid missing
400 * interrupts, this is called at splsoftclock.
401 */
402 static void
403 kd_putfb(struct tty *tp)
404 {
405 char buf[PUT_WSIZE];
406 struct clist *cl = &tp->t_outq;
407 char *p, *end;
408 int len;
409
410 while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
411 /* PROM will barf if high bits are set. */
412 p = buf;
413 end = buf + len;
414 while (p < end)
415 *p++ &= 0x7f;
416
417 /* Now let the PROM print it. */
418 prom_putstr(buf, len);
419 }
420 }
421
422 /*
423 * Our "interrupt" routine for input. This is called by
424 * the keyboard driver (dev/sun/kbd.c) at spltty.
425 */
426 static void
427 kd_cons_input(int c)
428 {
429 struct kd_softc *kd = &kd_softc;
430 struct tty *tp;
431
432 /* XXX: Make sure the device is open. */
433 tp = kd->kd_tty;
434 if (tp == NULL)
435 return;
436 if ((tp->t_state & TS_ISOPEN) == 0)
437 return;
438
439 (*tp->t_linesw->l_rint)(c, tp);
440 }
441
442 void
443 cons_attach_input(struct cons_channel *cc, struct consdev *cn)
444 {
445 struct kd_softc *kd = &kd_softc;
446
447 kd->kd_in = cc;
448 cc->cc_upstream = kd_cons_input;
449 }
450
451 void
452 kd_attach_input(struct cons_channel *cc)
453 {
454 struct kd_softc *kd = &kd_softc;
455
456 kd->kd_in = cc;
457 cc->cc_upstream = kd_cons_input;
458 }
459
460 /*
461 * Default PROM-based console input stream
462 * Since the PROM does not notify us when data is available on the
463 * input channel these functions periodically poll the PROM.
464 */
465 static int kd_rom_iopen(struct cons_channel *);
466 static int kd_rom_iclose(struct cons_channel *);
467 static void kd_rom_intr(void *);
468
469 static struct cons_channel prom_cons_channel = {
470 NULL, /* no private data */
471 kd_rom_iopen,
472 kd_rom_iclose,
473 NULL /* will be set by kd driver */
474 };
475
476 static struct callout prom_cons_callout;
477
478 static int
479 kd_rom_iopen(struct cons_channel *cc)
480 {
481 static bool callo;
482
483 if (!callo) {
484 callout_init(&prom_cons_callout, 0);
485 callo = true;
486 }
487
488 /* Poll for ROM input 4 times per second */
489 callout_reset(&prom_cons_callout, hz / 4, kd_rom_intr, cc);
490 return (0);
491 }
492
493 static int
494 kd_rom_iclose(struct cons_channel *cc)
495 {
496
497 callout_stop(&prom_cons_callout);
498 return (0);
499 }
500
501 /*
502 * "Interrupt" routine for input through ROM vectors
503 */
504 static void
505 kd_rom_intr(void *arg)
506 {
507 struct cons_channel *cc = arg;
508 int s, c;
509
510 callout_schedule(&prom_cons_callout, hz / 4);
511
512 s = spltty();
513
514 while ((c = prom_peekchar()) >= 0)
515 (*cc->cc_upstream)(c);
516
517 splx(s);
518 }
519
520 /*****************************************************************/
521
522 int prom_stdin_node;
523 int prom_stdout_node;
524 char prom_stdin_args[16];
525 char prom_stdout_args[16];
526
527 static void prom_cnprobe(struct consdev *);
528 static void prom_cninit(struct consdev *);
529 int prom_cngetc(dev_t); /* XXX: for sunkbd_wskbd_cngetc */
530 static void prom_cnputc(dev_t, int);
531 static void prom_cnpollc(dev_t, int);
532
533 /*
534 * The console is set to this one initially,
535 * which lets us use the PROM until consinit()
536 * is called to select a real console.
537 */
538 struct consdev consdev_prom = {
539 prom_cnprobe,
540 prom_cninit,
541 prom_cngetc,
542 prom_cnputc,
543 prom_cnpollc,
544 NULL,
545 };
546
547 /*
548 * The console table pointer is statically initialized
549 * to point to the PROM table, so that early calls to printf will work.
550 */
551 struct consdev *cn_tab = &consdev_prom;
552
553 static void
554 prom_cnprobe(struct consdev *cn)
555 {
556 }
557
558 static void
559 prom_cninit(struct consdev *cn)
560 {
561 }
562
563 static void
564 prom_cnpollc(dev_t dev, int on)
565 {
566
567 if (on) {
568 /* Entering debugger. */
569 #if NFB > 0
570 fb_unblank();
571 #endif
572 } else {
573 /* Resuming kernel. */
574 }
575 }
576
577
578 /*
579 * PROM console input putchar.
580 */
581 int
582 prom_cngetc(dev_t dev)
583 {
584 int s, c;
585
586 s = splhigh();
587 c = prom_getchar();
588 splx(s);
589 return (c);
590 }
591
592 /*
593 * PROM console output putchar.
594 */
595 static void
596 prom_cnputc(dev_t dev, int c)
597 {
598
599 prom_putchar(c);
600 }
601
602
603 /*****************************************************************/
604
605 static void prom_get_device_args(const char *, char *, unsigned int);
606
607 static void
608 prom_get_device_args(const char *prop, char *args, unsigned int sz)
609 {
610 const char *cp;
611 char buffer[128];
612
613 cp = prom_getpropstringA(findroot(), prop, buffer, sizeof buffer);
614
615 /*
616 * Extract device-specific arguments from a PROM device path (if any)
617 */
618 cp = buffer + strlen(buffer);
619 while (cp >= buffer) {
620 if (*cp == ':') {
621 strncpy(args, cp+1, sz);
622 break;
623 }
624 cp--;
625 }
626 }
627
628 /*
629 *
630 */
631 void
632 consinit(void)
633 {
634 int inSource, outSink;
635
636 switch (prom_version()) {
637 case PROM_OLDMON:
638 case PROM_OBP_V0:
639 /* The stdio handles identify the device type */
640 inSource = prom_stdin();
641 outSink = prom_stdout();
642 break;
643
644 case PROM_OBP_V2:
645 case PROM_OBP_V3:
646 case PROM_OPENFIRM:
647
648 /* Save PROM arguments for device matching */
649 prom_get_device_args("stdin-path", prom_stdin_args,
650 sizeof(prom_stdin_args));
651 prom_get_device_args("stdout-path", prom_stdout_args,
652 sizeof(prom_stdout_args));
653
654 /*
655 * Translate the STDIO package instance (`ihandle') -- that
656 * the PROM has already opened for us -- to a device tree
657 * node (i.e. a `phandle').
658 */
659
660 prom_stdin_node = prom_instance_to_package(prom_stdin());
661 if (prom_stdin_node == 0)
662 printf("consinit: cannot convert stdin ihandle\n");
663
664 prom_stdout_node = prom_instance_to_package(prom_stdout());
665 if (prom_stdout_node == 0) {
666 printf("consinit: cannot convert stdout ihandle\n");
667 break;
668 }
669
670 break;
671
672 default:
673 break;
674 }
675
676 /* Wire up /dev/console */
677 cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
678 cn_tab->cn_pri = CN_INTERNAL;
679
680 /* Set up initial PROM input channel for /dev/console */
681 cons_attach_input(&prom_cons_channel, cn_tab);
682
683 #ifdef KGDB
684 zs_kgdb_init(); /* XXX */
685 #endif
686 }
687