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