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