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