kd.c revision 1.10 1 /* $NetBSD: kd.c,v 1.10 2000/03/21 10:56:23 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 * Keyboard/Display device.
41 *
42 * This driver exists simply to provide a tty device that
43 * the indirect console driver can point to.
44 * The kbd driver sends its input here.
45 * Output goes to the screen via PROM printf.
46 */
47
48 #include <sys/param.h>
49 #include <sys/proc.h>
50 #include <sys/systm.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
57 #include <machine/openfirm.h>
58 #include <machine/eeprom.h>
59 #include <machine/psl.h>
60 #include <machine/cpu.h>
61 #include <machine/kbd.h>
62 #include <machine/autoconf.h>
63 #include <machine/conf.h>
64
65 #ifdef RASTERCONSOLE
66 #include <machine/fbio.h>
67 #include <machine/fbvar.h>
68 #endif
69
70
71 #include <dev/cons.h>
72 #include <dev/sun/event_var.h>
73 #include <dev/sun/kbd_xlate.h>
74 #include <dev/sun/kbdvar.h>
75 #include <sparc64/dev/cons.h>
76
77 struct tty *fbconstty = 0; /* tty structure for frame buffer console */
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 static int kd_is_console;
97
98 static int kdparam(struct tty *, struct termios *);
99 static void kdstart(struct tty *);
100 static void kd_init __P((struct kd_softc *));
101 static void kd_cons_input __P((int));
102
103 int cons_ocount; /* output byte count */
104
105 /*
106 * This is called by kbd_attach()
107 * XXX - Make this a proper child of kbd?
108 */
109 void
110 kd_init(kd)
111 struct kd_softc *kd;
112 {
113 struct tty *tp;
114 int i;
115 char *prop;
116
117 kd = &kd_softc; /* XXX */
118
119 tp = ttymalloc();
120 tp->t_oproc = kdstart;
121 tp->t_param = kdparam;
122 tp->t_dev = makedev(KDMAJOR, 0);
123
124 tty_attach(tp);
125 kd->kd_tty = tp;
126
127 /*
128 * get the console struct winsize.
129 */
130 if (kd_is_console) {
131 fbconstty = tp;
132 #ifdef RASTERCONSOLE
133 kd->rows = fbrcons_rows();
134 kd->cols = fbrcons_cols();
135 rcons_ttyinit(tp);
136 #endif
137 }
138
139 if (kd->rows == 0 &&
140 (prop = getpropstring(optionsnode, "screen-#rows"))) {
141 i = 0;
142 while (*prop != '\0')
143 i = i * 10 + *prop++ - '0';
144 kd->rows = (unsigned short)i;
145 }
146 if (kd->cols == 0 &&
147 (prop = getpropstring(optionsnode, "screen-#columns"))) {
148 i = 0;
149 while (*prop != '\0')
150 i = i * 10 + *prop++ - '0';
151 kd->cols = (unsigned short)i;
152 }
153 return;
154 }
155
156 struct tty *
157 kdtty(dev)
158 dev_t dev;
159 {
160 struct kd_softc *kd;
161
162 kd = &kd_softc; /* XXX */
163 return (kd->kd_tty);
164 }
165
166 int
167 kdopen(dev, flag, mode, p)
168 dev_t dev;
169 int flag, mode;
170 struct proc *p;
171 {
172 struct kd_softc *kd;
173 int error, s, unit;
174 struct tty *tp;
175 static int firstopen = 1;
176
177 unit = minor(dev);
178 if (unit != 0)
179 return ENXIO;
180 kd = &kd_softc; /* XXX */
181
182 if (firstopen) {
183 kd_init(kd);
184 firstopen = 0;
185 }
186 tp = kd->kd_tty;
187
188 /* It's simpler to do this up here. */
189 if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE))
190 == (TS_ISOPEN | TS_XCLUDE))
191 && (p->p_ucred->cr_uid != 0) )
192 {
193 return (EBUSY);
194 }
195
196 s = spltty();
197
198 if ((tp->t_state & TS_ISOPEN) == 0) {
199 /* First open. */
200
201 /* Notify the input device that serves us */
202 struct cons_channel *cc = kd->kd_in;
203 if (cc != NULL &&
204 (error = (*cc->cc_iopen)(cc)) != 0) {
205 return (error);
206 }
207
208 ttychars(tp);
209 tp->t_iflag = TTYDEF_IFLAG;
210 tp->t_oflag = TTYDEF_OFLAG;
211 tp->t_cflag = TTYDEF_CFLAG;
212 tp->t_lflag = TTYDEF_LFLAG;
213 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
214 (void) kdparam(tp, &tp->t_termios);
215 ttsetwater(tp);
216 tp->t_winsize.ws_row = kd->rows;
217 tp->t_winsize.ws_col = kd->cols;
218 /* Flush pending input? Clear translator? */
219 /* This (pseudo)device always has SOFTCAR */
220 tp->t_state |= TS_CARR_ON;
221 }
222
223 splx(s);
224
225 return ((*linesw[tp->t_line].l_open)(dev, tp));
226 }
227
228 int
229 kdclose(dev, flag, mode, p)
230 dev_t dev;
231 int flag, mode;
232 struct proc *p;
233 {
234 struct kd_softc *kd;
235 struct tty *tp;
236 struct cons_channel *cc;
237
238 kd = &kd_softc; /* XXX */
239 tp = kd->kd_tty;
240
241 /* XXX This is for cons.c. */
242 if ((tp->t_state & TS_ISOPEN) == 0)
243 return 0;
244
245 (*linesw[tp->t_line].l_close)(tp, flag);
246 ttyclose(tp);
247
248 if ((cc = kd->kd_in) != NULL)
249 (void)(*cc->cc_iclose)(cc->cc_dev);
250
251 return (0);
252 }
253
254 int
255 kdread(dev, uio, flag)
256 dev_t dev;
257 struct uio *uio;
258 int flag;
259 {
260 struct kd_softc *kd;
261 struct tty *tp;
262
263 kd = &kd_softc; /* XXX */
264 tp = kd->kd_tty;
265
266 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
267 }
268
269 int
270 kdwrite(dev, uio, flag)
271 dev_t dev;
272 struct uio *uio;
273 int flag;
274 {
275 struct kd_softc *kd;
276 struct tty *tp;
277
278 kd = &kd_softc; /* XXX */
279 tp = kd->kd_tty;
280
281 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
282 }
283
284 int
285 kdioctl(dev, cmd, data, flag, p)
286 dev_t dev;
287 u_long cmd;
288 caddr_t data;
289 int flag;
290 struct proc *p;
291 {
292 struct kd_softc *kd;
293 struct tty *tp;
294 int error;
295
296 kd = &kd_softc; /* XXX */
297 tp = kd->kd_tty;
298
299 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
300 if (error >= 0)
301 return error;
302 error = ttioctl(tp, cmd, data, flag, p);
303 if (error >= 0)
304 return error;
305
306 /* Handle any ioctl commands specific to kbd/display. */
307 /* XXX - Send KB* ioctls to kbd module? */
308 /* XXX - Send FB* ioctls to fb module? */
309
310 return ENOTTY;
311 }
312
313 void
314 kdstop(tp, flag)
315 struct tty *tp;
316 int flag;
317 {
318
319 }
320
321
322 static int
323 kdparam(tp, t)
324 struct tty *tp;
325 struct termios *t;
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
335 static void kd_later(void*);
336 static void kd_putfb(struct tty *);
337
338 static void
339 kdstart(tp)
340 struct tty *tp;
341 {
342 struct clist *cl;
343 register int s;
344
345 s = spltty();
346 if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
347 goto out;
348
349 cl = &tp->t_outq;
350 if (cl->c_cc) {
351 if (kd_is_console) {
352 tp->t_state |= TS_BUSY;
353 if ((s & PSR_PIL) == 0) {
354 /* called at level zero - update screen now. */
355 (void) spllowersoftclock();
356 kd_putfb(tp);
357 (void) spltty();
358 tp->t_state &= ~TS_BUSY;
359 } else {
360 /* called at interrupt level - do it later */
361 timeout(kd_later, (void*)tp, 0);
362 }
363 } else {
364 /*
365 * This driver uses the PROM for writing the screen,
366 * and that only works if this is the console device.
367 * If this is not the console, just flush the output.
368 * Sorry. (In that case, use xdm instead of getty.)
369 */
370 ndflush(cl, cl->c_cc);
371 }
372 }
373 if (cl->c_cc <= tp->t_lowat) {
374 if (tp->t_state & TS_ASLEEP) {
375 tp->t_state &= ~TS_ASLEEP;
376 wakeup((caddr_t)cl);
377 }
378 selwakeup(&tp->t_wsel);
379 }
380 out:
381 splx(s);
382 }
383
384 /*
385 * Timeout function to do delayed writes to the screen.
386 * Called at splsoftclock when requested by kdstart.
387 */
388 static void
389 kd_later(tpaddr)
390 void *tpaddr;
391 {
392 struct tty *tp = tpaddr;
393 register int s;
394
395 kd_putfb(tp);
396
397 s = spltty();
398 tp->t_state &= ~TS_BUSY;
399 (*linesw[tp->t_line].l_start)(tp);
400 splx(s);
401 }
402
403 /*
404 * Put text on the screen using the PROM monitor.
405 * This can take a while, so to avoid missing
406 * interrupts, this is called at splsoftclock.
407 */
408 static void
409 kd_putfb(tp)
410 struct tty *tp;
411 {
412 char buf[PUT_WSIZE];
413 struct clist *cl = &tp->t_outq;
414 char *p, *end;
415 int len;
416
417 while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
418 /* PROM will barf if high bits are set. */
419 p = buf;
420 end = buf + len;
421 while (p < end)
422 *p++ &= 0x7f;
423 /* Now let the PROM print it. */
424 OF_write(OF_stdout(), buf, len);
425 }
426 }
427
428 void
429 cons_attach_input(cc)
430 struct cons_channel *cc;
431 {
432 struct kd_softc *kd = &kd_softc;
433
434 kd->kd_in = cc;
435 cc->cc_upstream = kd_cons_input;
436 }
437
438 /*
439 * Default PROM-based console input stream
440 */
441 static int kd_rom_iopen __P((struct cons_channel *));
442 static int kd_rom_iclose __P((struct cons_channel *));
443
444 static struct cons_channel prom_cons_channel;
445
446 int
447 kd_rom_iopen(cc)
448 struct cons_channel *cc;
449 {
450 return (0);
451 }
452
453 int
454 kd_rom_iclose(cc)
455 struct cons_channel *cc;
456 {
457 return (0);
458 }
459
460 /*
461 * Our "interrupt" routine for input. This is called by
462 * the keyboard driver (dev/sun/kbd.c) at spltty.
463 */
464 void
465 kd_cons_input(c)
466 int c;
467 {
468 struct kd_softc *kd = &kd_softc;
469 struct tty *tp;
470
471 /* XXX: Make sure the device is open. */
472 tp = kd->kd_tty;
473 if (tp == NULL)
474 return;
475 if ((tp->t_state & TS_ISOPEN) == 0)
476 return;
477
478 (*linesw[tp->t_line].l_rint)(c, tp);
479 }
480
481
482 /****************************************************************
483 * kd console support
484 ****************************************************************/
485
486 /* The debugger gets its own key translation state. */
487 static struct kbd_state kdcn_state;
488
489 static void kdcnprobe __P((struct consdev *));
490 static void kdcninit __P((struct consdev *));
491 static int kdcngetc __P((dev_t));
492 static void kdcnputc __P((dev_t, int));
493 static void kdcnpollc __P((dev_t, int));
494
495 /* The keyboard driver uses cn_hw to access the real console driver */
496 extern struct consdev consdev_prom;
497 struct consdev *cn_hw = &consdev_prom;
498 struct consdev consdev_kd = {
499 kdcnprobe,
500 kdcninit,
501 kdcngetc,
502 kdcnputc,
503 kdcnpollc,
504 NULL,
505 };
506
507 /* We never call this. */
508 static void
509 kdcnprobe(cn)
510 struct consdev *cn;
511 {
512 }
513
514 static void
515 kdcninit(cn)
516 struct consdev *cn;
517 {
518 struct kbd_state *ks = &kdcn_state;
519
520 cn->cn_dev = makedev(KDMAJOR, 0);
521 cn->cn_pri = CN_INTERNAL;
522
523 /* This prepares kbd_translate() */
524 ks->kbd_id = KBD_MIN_TYPE;
525 kbd_xlate_init(ks);
526
527 /* Set up initial PROM input channel for /dev/console */
528 prom_cons_channel.cc_dev = NULL;
529 prom_cons_channel.cc_iopen = kd_rom_iopen;
530 prom_cons_channel.cc_iclose = kd_rom_iclose;
531 cons_attach_input(&prom_cons_channel);
532
533 /* Indicate that it is OK to use the PROM fbwrite */
534 kd_is_console = 1;
535 }
536
537 static int
538 kdcngetc(dev)
539 dev_t dev;
540 {
541 struct kbd_state *ks = &kdcn_state;
542 int code, class, data, keysym;
543
544 for (;;) {
545 code = (*cn_hw->cn_getc)(dev);
546 keysym = kbd_code_to_keysym(ks, code);
547 class = KEYSYM_CLASS(keysym);
548
549 switch (class) {
550 case KEYSYM_ASCII:
551 goto out;
552
553 case KEYSYM_CLRMOD:
554 case KEYSYM_SETMOD:
555 data = (keysym & 0x1F);
556 /* Only allow ctrl or shift. */
557 if (data > KBMOD_SHIFT_R)
558 break;
559 data = 1 << data;
560 if (class == KEYSYM_SETMOD)
561 ks->kbd_modbits |= data;
562 else
563 ks->kbd_modbits &= ~data;
564 break;
565
566 case KEYSYM_ALL_UP:
567 /* No toggle keys here. */
568 ks->kbd_modbits = 0;
569 break;
570
571 default: /* ignore all other keysyms */
572 break;
573 }
574 }
575 out:
576 return (keysym);
577 }
578
579 static void
580 kdcnputc(dev, c)
581 dev_t dev;
582 int c;
583 {
584 int s;
585 char c0 = (c & 0x7f);
586
587 s = splhigh();
588 OF_write(OF_stdout(), &c0, 1);
589 splx(s);
590 }
591
592 static void
593 kdcnpollc(dev, on)
594 dev_t dev;
595 int on;
596 {
597 struct kbd_state *ks = &kdcn_state;
598
599 if (on) {
600 /* Entering debugger. */
601 #if NFB > 0
602 fb_unblank();
603 #endif
604 /* Clear shift keys too. */
605 ks->kbd_modbits = 0;
606 } else {
607 /* Resuming kernel. */
608 }
609 (*cn_hw->cn_pollc)(dev, on);
610 }
611