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