kd.c revision 1.19.4.3 1 /* $NetBSD: kd.c,v 1.19.4.3 2001/10/11 00:01:51 fvdl 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 "opt_kgdb.h"
49 #include "fb.h"
50
51 #include <sys/param.h>
52 #include <sys/proc.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/ioctl.h>
56 #include <sys/tty.h>
57 #include <sys/file.h>
58 #include <sys/conf.h>
59 #include <sys/device.h>
60 #include <sys/vnode.h>
61
62 #include <machine/bsd_openprom.h>
63 #include <machine/promlib.h>
64 #include <machine/eeprom.h>
65 #include <machine/psl.h>
66 #include <machine/cpu.h>
67 #include <machine/kbd.h>
68 #include <machine/autoconf.h>
69 #include <machine/conf.h>
70
71 #if defined(RASTERCONSOLE) && NFB > 0
72 #include <dev/sun/fbio.h>
73 #include <dev/sun/fbvar.h>
74 #endif
75
76 #include <dev/cons.h>
77 #include <sparc/dev/cons.h>
78
79 #include <dev/sun/event_var.h>
80 #include <dev/sun/kbd_xlate.h>
81 #include <dev/sun/kbdvar.h>
82
83 #define KDMAJOR 1
84 #define PUT_WSIZE 64
85
86 struct kd_softc {
87 struct device kd_dev; /* required first: base device */
88 struct tty *kd_tty;
89 int rows, cols;
90
91 /* Console input hook */
92 struct cons_channel *kd_in;
93 };
94
95 /*
96 * There is no point in pretending there might be
97 * more than one keyboard/display device.
98 */
99 static struct kd_softc kd_softc;
100
101 static int kdparam(struct tty *, struct termios *);
102 static void kdstart(struct tty *);
103 static void kd_init __P((struct kd_softc *, struct vnode *devvp));
104 static void kd_cons_input __P((int));
105
106 /*
107 * Prepare the console tty; called on first open of /dev/console
108 */
109 void
110 kd_init(kd, devvp)
111 struct kd_softc *kd;
112 struct vnode *devvp;
113 {
114 struct tty *tp;
115
116 tp = ttymalloc();
117 tp->t_oproc = kdstart;
118 tp->t_param = kdparam;
119 tp->t_devvp = devvp;
120
121 tty_attach(tp);
122 kd->kd_tty = tp;
123
124 /*
125 * Get the console struct winsize.
126 */
127 #if defined(RASTERCONSOLE) && NFB > 0
128 /* If the raster console driver is attached, copy its size */
129 kd->rows = fbrcons_rows();
130 kd->cols = fbrcons_cols();
131 rcons_ttyinit(tp);
132 #endif
133
134 /* else, consult the PROM */
135 switch (prom_version()) {
136 char *prop;
137 struct eeprom *ep;
138 case PROM_OLDMON:
139 if ((ep = (struct eeprom *)eeprom_va) == NULL)
140 break;
141 if (kd->rows == 0)
142 kd->rows = (u_short)ep->eeTtyRows;
143 if (kd->cols == 0)
144 kd->cols = (u_short)ep->eeTtyCols;
145 break;
146 case PROM_OBP_V0:
147 case PROM_OBP_V2:
148 case PROM_OBP_V3:
149 case PROM_OPENFIRM:
150
151 if (kd->rows == 0 &&
152 (prop = PROM_getpropstring(optionsnode, "screen-#rows"))) {
153 int i = 0;
154
155 while (*prop != '\0')
156 i = i * 10 + *prop++ - '0';
157 kd->rows = (unsigned short)i;
158 }
159 if (kd->cols == 0 &&
160 (prop = PROM_getpropstring(optionsnode, "screen-#columns"))) {
161 int i = 0;
162
163 while (*prop != '\0')
164 i = i * 10 + *prop++ - '0';
165 kd->cols = (unsigned short)i;
166 }
167 break;
168 }
169
170 return;
171 }
172
173 struct tty *
174 kdtty(devvp)
175 struct vnode *devvp;
176 {
177 struct kd_softc *kd;
178
179 kd = &kd_softc; /* XXX */
180 return (kd->kd_tty);
181 }
182
183 int
184 kdopen(devvp, flag, mode, p)
185 struct vnode *devvp;
186 int flag, mode;
187 struct proc *p;
188 {
189 dev_t dev;
190 struct kd_softc *kd;
191 int error, s, unit;
192 struct tty *tp;
193 static int firstopen = 1;
194
195 dev = vdev_rdev(devvp);
196
197 unit = minor(dev);
198 if (unit != 0)
199 return ENXIO;
200
201 kd = &kd_softc; /* XXX */
202 if (firstopen) {
203 kd_init(kd, devvp);
204 firstopen = 0;
205 }
206
207 tp = kd->kd_tty;
208
209 /* It's simpler to do this up here. */
210 if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE))
211 == (TS_ISOPEN | TS_XCLUDE))
212 && (p->p_ucred->cr_uid != 0) )
213 {
214 return (EBUSY);
215 }
216
217 s = spltty();
218 if ((tp->t_state & TS_ISOPEN) == 0) {
219 /* First open. */
220
221 /* Notify the input device that serves us */
222 struct cons_channel *cc = kd->kd_in;
223 if (cc != NULL &&
224 (error = (*cc->cc_iopen)(cc, devvp)) != 0) {
225 return (error);
226 }
227
228 ttychars(tp);
229 tp->t_iflag = TTYDEF_IFLAG;
230 tp->t_oflag = TTYDEF_OFLAG;
231 tp->t_cflag = TTYDEF_CFLAG;
232 tp->t_lflag = TTYDEF_LFLAG;
233 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
234 (void) kdparam(tp, &tp->t_termios);
235 ttsetwater(tp);
236 tp->t_winsize.ws_row = kd->rows;
237 tp->t_winsize.ws_col = kd->cols;
238 /* Flush pending input? Clear translator? */
239 /* This (pseudo)device always has SOFTCAR */
240 tp->t_state |= TS_CARR_ON;
241 }
242
243 splx(s);
244
245 return ((*tp->t_linesw->l_open)(devvp, tp));
246 }
247
248 int
249 kdclose(devvp, flag, mode, p)
250 struct vnode *devvp;
251 int flag, mode;
252 struct proc *p;
253 {
254 struct kd_softc *kd;
255 struct tty *tp;
256 struct cons_channel *cc;
257
258 kd = &kd_softc; /* XXX */
259 tp = kd->kd_tty;
260
261 /* XXX This is for cons.c. */
262 if ((tp->t_state & TS_ISOPEN) == 0)
263 return 0;
264
265 (*tp->t_linesw->l_close)(tp, flag);
266 ttyclose(tp);
267
268 if ((cc = kd->kd_in) != NULL)
269 (void)(*cc->cc_iclose)(cc, devvp);
270
271 return (0);
272 }
273
274 int
275 kdread(devvp, uio, flag)
276 struct vnode *devvp;
277 struct uio *uio;
278 int flag;
279 {
280 struct kd_softc *kd;
281 struct tty *tp;
282
283 kd = &kd_softc; /* XXX */
284 tp = kd->kd_tty;
285
286 return ((*tp->t_linesw->l_read)(tp, uio, flag));
287 }
288
289 int
290 kdwrite(devvp, uio, flag)
291 struct vnode *devvp;
292 struct uio *uio;
293 int flag;
294 {
295 struct kd_softc *kd;
296 struct tty *tp;
297
298 kd = &kd_softc; /* XXX */
299 tp = kd->kd_tty;
300
301 return ((*tp->t_linesw->l_write)(tp, uio, flag));
302 }
303
304 int
305 kdpoll(devvp, events, p)
306 struct vnode *devvp;
307 int events;
308 struct proc *p;
309 {
310 struct kd_softc *kd;
311 struct tty *tp;
312
313 kd = &kd_softc; /* XXX */
314 tp = kd->kd_tty;
315
316 return ((*tp->t_linesw->l_poll)(tp, events, p));
317 }
318
319 int
320 kdioctl(devvp, cmd, data, flag, p)
321 struct vnode *devvp;
322 u_long cmd;
323 caddr_t data;
324 int flag;
325 struct proc *p;
326 {
327 struct kd_softc *kd;
328 struct tty *tp;
329 int error;
330
331 kd = &kd_softc; /* XXX */
332 tp = kd->kd_tty;
333
334 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
335 if (error >= 0)
336 return error;
337 error = ttioctl(tp, cmd, data, flag, p);
338 if (error >= 0)
339 return error;
340
341 /* Handle any ioctl commands specific to kbd/display. */
342 /* XXX - Send KB* ioctls to kbd module? */
343 /* XXX - Send FB* ioctls to fb module? */
344
345 return ENOTTY;
346 }
347
348 void
349 kdstop(tp, flag)
350 struct tty *tp;
351 int flag;
352 {
353
354 }
355
356
357 static int
358 kdparam(tp, t)
359 struct tty *tp;
360 struct termios *t;
361 {
362 /* XXX - These are ignored... */
363 tp->t_ispeed = t->c_ispeed;
364 tp->t_ospeed = t->c_ospeed;
365 tp->t_cflag = t->c_cflag;
366 return 0;
367 }
368
369
370 static void kd_later(void*);
371 static void kd_putfb(struct tty *);
372
373 static void
374 kdstart(tp)
375 struct tty *tp;
376 {
377 struct clist *cl;
378 int s;
379
380 s = spltty();
381 if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
382 goto out;
383
384 cl = &tp->t_outq;
385 if (cl->c_cc) {
386 tp->t_state |= TS_BUSY;
387 if ((s & PSR_PIL) == 0) {
388 /* called at level zero - update screen now. */
389 (void) spllowersoftclock();
390 kd_putfb(tp);
391 (void) spltty();
392 tp->t_state &= ~TS_BUSY;
393 } else {
394 /* called at interrupt level - do it later */
395 callout_reset(&tp->t_rstrt_ch, 0, kd_later, tp);
396 }
397 }
398 if (cl->c_cc <= tp->t_lowat) {
399 if (tp->t_state & TS_ASLEEP) {
400 tp->t_state &= ~TS_ASLEEP;
401 wakeup((caddr_t)cl);
402 }
403 selwakeup(&tp->t_wsel);
404 }
405 out:
406 splx(s);
407 }
408
409 /*
410 * Timeout function to do delayed writes to the screen.
411 * Called at splsoftclock when requested by kdstart.
412 */
413 static void
414 kd_later(arg)
415 void *arg;
416 {
417 struct tty *tp = arg;
418 int s;
419
420 kd_putfb(tp);
421
422 s = spltty();
423 tp->t_state &= ~TS_BUSY;
424 (*tp->t_linesw->l_start)(tp);
425 splx(s);
426 }
427
428 /*
429 * Put text on the screen using the PROM monitor.
430 * This can take a while, so to avoid missing
431 * interrupts, this is called at splsoftclock.
432 */
433 static void
434 kd_putfb(tp)
435 struct tty *tp;
436 {
437 char buf[PUT_WSIZE];
438 struct clist *cl = &tp->t_outq;
439 char *p, *end;
440 int len;
441
442 while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
443 /* PROM will barf if high bits are set. */
444 p = buf;
445 end = buf + len;
446 while (p < end)
447 *p++ &= 0x7f;
448
449 /* Now let the PROM print it. */
450 prom_putstr(buf, len);
451 }
452 }
453
454 /*
455 * Our "interrupt" routine for input. This is called by
456 * the keyboard driver (dev/sun/kbd.c) at spltty.
457 */
458 void
459 kd_cons_input(c)
460 int c;
461 {
462 struct kd_softc *kd = &kd_softc;
463 struct tty *tp;
464
465 /* XXX: Make sure the device is open. */
466 tp = kd->kd_tty;
467 if (tp == NULL)
468 return;
469 if ((tp->t_state & TS_ISOPEN) == 0)
470 return;
471
472 (*tp->t_linesw->l_rint)(c, tp);
473 }
474
475 void
476 cons_attach_input(cc, cn)
477 struct cons_channel *cc;
478 struct consdev *cn;
479 {
480 struct kd_softc *kd = &kd_softc;
481
482 kd->kd_in = cc;
483 cc->cc_upstream = kd_cons_input;
484 }
485
486 void kd_attach_input(struct cons_channel *);
487 void
488 kd_attach_input(cc)
489 struct cons_channel *cc;
490 {
491 struct kd_softc *kd = &kd_softc;
492
493 kd->kd_in = cc;
494 cc->cc_upstream = kd_cons_input;
495 }
496
497 /*
498 * Default PROM-based console input stream
499 * Since the PROM does not notify us when data is available on the
500 * input channel these functions periodically poll the PROM.
501 */
502 static int kd_rom_iopen __P((struct cons_channel *, struct vnode *));
503 static int kd_rom_iclose __P((struct cons_channel *, struct vnode *));
504 static void kd_rom_intr __P((void *));
505
506 static struct cons_channel prom_cons_channel;
507
508 int
509 kd_rom_iopen(cc, devvp)
510 struct cons_channel *cc;
511 struct vnode *devvp;
512 {
513 /* Poll for ROM input 4 times per second */
514 callout_reset(&cc->cc_callout, hz / 4, kd_rom_intr, cc);
515 return (0);
516 }
517
518 int
519 kd_rom_iclose(cc, devvp)
520 struct cons_channel *cc;
521 struct vnode *devvp;
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(KDMAJOR, 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