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