kd.c revision 1.17 1 /* $NetBSD: kd.c,v 1.17 2001/05/02 10:32:19 scw 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 ((*tp->t_linesw->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 (*tp->t_linesw->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 ((*tp->t_linesw->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 ((*tp->t_linesw->l_write)(tp, uio, flag));
294 }
295
296 int
297 kdpoll(dev, events, p)
298 dev_t dev;
299 int events;
300 struct proc *p;
301 {
302 struct kd_softc *kd;
303 struct tty *tp;
304
305 kd = &kd_softc; /* XXX */
306 tp = kd->kd_tty;
307
308 return ((*tp->t_linesw->l_poll)(tp, events, p));
309 }
310
311 int
312 kdioctl(dev, cmd, data, flag, p)
313 dev_t dev;
314 u_long cmd;
315 caddr_t data;
316 int flag;
317 struct proc *p;
318 {
319 struct kd_softc *kd;
320 struct tty *tp;
321 int error;
322
323 kd = &kd_softc; /* XXX */
324 tp = kd->kd_tty;
325
326 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
327 if (error >= 0)
328 return error;
329 error = ttioctl(tp, cmd, data, flag, p);
330 if (error >= 0)
331 return error;
332
333 /* Handle any ioctl commands specific to kbd/display. */
334 /* XXX - Send KB* ioctls to kbd module? */
335 /* XXX - Send FB* ioctls to fb module? */
336
337 return ENOTTY;
338 }
339
340 void
341 kdstop(tp, flag)
342 struct tty *tp;
343 int flag;
344 {
345
346 }
347
348
349 static int
350 kdparam(tp, t)
351 struct tty *tp;
352 struct termios *t;
353 {
354 /* XXX - These are ignored... */
355 tp->t_ispeed = t->c_ispeed;
356 tp->t_ospeed = t->c_ospeed;
357 tp->t_cflag = t->c_cflag;
358 return 0;
359 }
360
361
362 static void kd_later(void*);
363 static void kd_putfb(struct tty *);
364
365 static void
366 kdstart(tp)
367 struct tty *tp;
368 {
369 struct clist *cl;
370 int s;
371
372 s = spltty();
373 if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
374 goto out;
375
376 cl = &tp->t_outq;
377 if (cl->c_cc) {
378 tp->t_state |= TS_BUSY;
379 if ((s & PSR_PIL) == 0) {
380 /* called at level zero - update screen now. */
381 (void) spllowersoftclock();
382 kd_putfb(tp);
383 (void) spltty();
384 tp->t_state &= ~TS_BUSY;
385 } else {
386 /* called at interrupt level - do it later */
387 callout_reset(&tp->t_rstrt_ch, 0, kd_later, tp);
388 }
389 }
390 if (cl->c_cc <= tp->t_lowat) {
391 if (tp->t_state & TS_ASLEEP) {
392 tp->t_state &= ~TS_ASLEEP;
393 wakeup((caddr_t)cl);
394 }
395 selwakeup(&tp->t_wsel);
396 }
397 out:
398 splx(s);
399 }
400
401 /*
402 * Timeout function to do delayed writes to the screen.
403 * Called at splsoftclock when requested by kdstart.
404 */
405 static void
406 kd_later(arg)
407 void *arg;
408 {
409 struct tty *tp = arg;
410 int s;
411
412 kd_putfb(tp);
413
414 s = spltty();
415 tp->t_state &= ~TS_BUSY;
416 (*tp->t_linesw->l_start)(tp);
417 splx(s);
418 }
419
420 /*
421 * Put text on the screen using the PROM monitor.
422 * This can take a while, so to avoid missing
423 * interrupts, this is called at splsoftclock.
424 */
425 static void
426 kd_putfb(tp)
427 struct tty *tp;
428 {
429 char buf[PUT_WSIZE];
430 struct clist *cl = &tp->t_outq;
431 char *p, *end;
432 int len;
433
434 while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
435 /* PROM will barf if high bits are set. */
436 p = buf;
437 end = buf + len;
438 while (p < end)
439 *p++ &= 0x7f;
440
441 /* Now let the PROM print it. */
442 prom_putstr(buf, len);
443 }
444 }
445
446 /*
447 * Our "interrupt" routine for input. This is called by
448 * the keyboard driver (dev/sun/kbd.c) at spltty.
449 */
450 void
451 kd_cons_input(c)
452 int c;
453 {
454 struct kd_softc *kd = &kd_softc;
455 struct tty *tp;
456
457 /* XXX: Make sure the device is open. */
458 tp = kd->kd_tty;
459 if (tp == NULL)
460 return;
461 if ((tp->t_state & TS_ISOPEN) == 0)
462 return;
463
464 (*tp->t_linesw->l_rint)(c, tp);
465 }
466
467 void
468 cons_attach_input(cc, cn)
469 struct cons_channel *cc;
470 struct consdev *cn;
471 {
472 struct kd_softc *kd = &kd_softc;
473
474 kd->kd_in = cc;
475 cc->cc_upstream = kd_cons_input;
476 }
477
478 void kd_attach_input(struct cons_channel *);
479 void
480 kd_attach_input(cc)
481 struct cons_channel *cc;
482 {
483 struct kd_softc *kd = &kd_softc;
484
485 kd->kd_in = cc;
486 cc->cc_upstream = kd_cons_input;
487 }
488
489 /*
490 * Default PROM-based console input stream
491 * Since the PROM does not notify us when data is available on the
492 * input channel these functions periodically poll the PROM.
493 */
494 static int kd_rom_iopen __P((struct cons_channel *));
495 static int kd_rom_iclose __P((struct cons_channel *));
496 static void kd_rom_intr __P((void *));
497
498 static struct cons_channel prom_cons_channel;
499
500 int
501 kd_rom_iopen(cc)
502 struct cons_channel *cc;
503 {
504 /* Poll for ROM input 4 times per second */
505 callout_reset(&cc->cc_callout, hz / 4, kd_rom_intr, cc);
506 return (0);
507 }
508
509 int
510 kd_rom_iclose(cc)
511 struct cons_channel *cc;
512 {
513
514 callout_stop(&cc->cc_callout);
515 return (0);
516 }
517
518 /*
519 * "Interrupt" routine for input through ROM vectors
520 */
521 void
522 kd_rom_intr(arg)
523 void *arg;
524 {
525 struct cons_channel *cc = arg;
526 int s, c;
527
528 /* Re-schedule */
529 callout_reset(&cc->cc_callout, hz / 4, kd_rom_intr, cc);
530
531 s = spltty();
532
533 while ((c = prom_peekchar()) >= 0)
534 (*cc->cc_upstream)(c);
535
536 splx(s);
537 }
538
539 /*****************************************************************/
540
541 int prom_stdin_node;
542 int prom_stdout_node;
543 char prom_stdin_args[16];
544 char prom_stdout_args[16];
545
546 extern void prom_cnprobe __P((struct consdev *));
547 static void prom_cninit __P((struct consdev *));
548 static int prom_cngetc __P((dev_t));
549 static void prom_cnputc __P((dev_t, int));
550 extern void prom_cnpollc __P((dev_t, int));
551
552 /*
553 * The console is set to this one initially,
554 * which lets us use the PROM until consinit()
555 * is called to select a real console.
556 */
557 struct consdev consdev_prom = {
558 prom_cnprobe,
559 prom_cninit,
560 prom_cngetc,
561 prom_cnputc,
562 prom_cnpollc,
563 NULL,
564 };
565
566 /*
567 * The console table pointer is statically initialized
568 * to point to the PROM table, so that early calls to printf will work.
569 */
570 struct consdev *cn_tab = &consdev_prom;
571
572 void
573 prom_cnprobe(cn)
574 struct consdev *cn;
575 {
576 }
577
578 static void
579 prom_cninit(cn)
580 struct consdev *cn;
581 {
582 }
583
584 void
585 prom_cnpollc(dev, on)
586 dev_t dev;
587 int on;
588 {
589
590 if (on) {
591 /* Entering debugger. */
592 #if NFB > 0
593 fb_unblank();
594 #endif
595 } else {
596 /* Resuming kernel. */
597 }
598 }
599
600
601 /*
602 * PROM console input putchar.
603 */
604 static int
605 prom_cngetc(dev)
606 dev_t dev;
607 {
608 int s, c;
609
610 s = splhigh();
611 c = prom_getchar();
612 splx(s);
613 return (c);
614 }
615
616 /*
617 * PROM console output putchar.
618 */
619 static void
620 prom_cnputc(dev, c)
621 dev_t dev;
622 int c;
623 {
624
625 prom_putchar(c);
626 }
627
628
629 /*****************************************************************/
630
631 static void prom_get_device_args __P((const char *, char *, unsigned int));
632
633 void
634 prom_get_device_args(prop, args, sz)
635 const char *prop;
636 char *args;
637 unsigned int sz;
638 {
639 char *cp, buffer[128];
640
641 cp = getpropstringA(findroot(), (char *)prop, buffer, sizeof buffer);
642
643 /*
644 * Extract device-specific arguments from a PROM device path (if any)
645 */
646 cp = buffer + strlen(buffer);
647 while (cp >= buffer) {
648 if (*cp == ':') {
649 strncpy(args, cp+1, sz);
650 break;
651 }
652 cp--;
653 }
654 }
655
656 /*
657 *
658 */
659 void
660 consinit()
661 {
662 int inSource, outSink;
663
664 switch (prom_version()) {
665 case PROM_OLDMON:
666 case PROM_OBP_V0:
667 /* The stdio handles identify the device type */
668 inSource = prom_stdin();
669 outSink = prom_stdout();
670 break;
671
672 case PROM_OBP_V2:
673 case PROM_OBP_V3:
674 case PROM_OPENFIRM:
675
676 /* Save PROM arguments for device matching */
677 prom_get_device_args("stdin-path", prom_stdin_args,
678 sizeof(prom_stdin_args));
679 prom_get_device_args("stdout-path", prom_stdout_args,
680 sizeof(prom_stdout_args));
681
682 /*
683 * Translate the STDIO package instance (`ihandle') -- that
684 * the PROM has already opened for us -- to a device tree
685 * node (i.e. a `phandle').
686 */
687
688 prom_stdin_node = prom_instance_to_package(prom_stdin());
689 if (prom_stdin_node == 0)
690 printf("consinit: cannot convert stdin ihandle\n");
691
692 prom_stdout_node = prom_instance_to_package(prom_stdout());
693 if (prom_stdout_node == 0) {
694 printf("consinit: cannot convert stdout ihandle\n");
695 break;
696 }
697
698 break;
699
700 default:
701 break;
702 }
703
704 /* Wire up /dev/console */
705 cn_tab->cn_dev = makedev(KDMAJOR, 0);
706 cn_tab->cn_pri = CN_INTERNAL;
707
708 /* Set up initial PROM input channel for /dev/console */
709 prom_cons_channel.cc_dev = NULL;
710 prom_cons_channel.cc_iopen = kd_rom_iopen;
711 prom_cons_channel.cc_iclose = kd_rom_iclose;
712 cons_attach_input(&prom_cons_channel, cn_tab);
713
714 #ifdef KGDB
715 zs_kgdb_init(); /* XXX */
716 #endif
717 }
718