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