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