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