kd.c revision 1.11 1 /* $NetBSD: kd.c,v 1.11 2000/03/23 06:45:37 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/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 callout_reset(&tp->t_rstrt_ch, 0,
362 kd_later, tp);
363 }
364 } else {
365 /*
366 * This driver uses the PROM for writing the screen,
367 * and that only works if this is the console device.
368 * If this is not the console, just flush the output.
369 * Sorry. (In that case, use xdm instead of getty.)
370 */
371 ndflush(cl, cl->c_cc);
372 }
373 }
374 if (cl->c_cc <= tp->t_lowat) {
375 if (tp->t_state & TS_ASLEEP) {
376 tp->t_state &= ~TS_ASLEEP;
377 wakeup((caddr_t)cl);
378 }
379 selwakeup(&tp->t_wsel);
380 }
381 out:
382 splx(s);
383 }
384
385 /*
386 * Timeout function to do delayed writes to the screen.
387 * Called at splsoftclock when requested by kdstart.
388 */
389 static void
390 kd_later(tpaddr)
391 void *tpaddr;
392 {
393 struct tty *tp = tpaddr;
394 register int s;
395
396 kd_putfb(tp);
397
398 s = spltty();
399 tp->t_state &= ~TS_BUSY;
400 (*linesw[tp->t_line].l_start)(tp);
401 splx(s);
402 }
403
404 /*
405 * Put text on the screen using the PROM monitor.
406 * This can take a while, so to avoid missing
407 * interrupts, this is called at splsoftclock.
408 */
409 static void
410 kd_putfb(tp)
411 struct tty *tp;
412 {
413 char buf[PUT_WSIZE];
414 struct clist *cl = &tp->t_outq;
415 char *p, *end;
416 int len;
417
418 while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
419 /* PROM will barf if high bits are set. */
420 p = buf;
421 end = buf + len;
422 while (p < end)
423 *p++ &= 0x7f;
424 /* Now let the PROM print it. */
425 OF_write(OF_stdout(), buf, len);
426 }
427 }
428
429 void
430 cons_attach_input(cc)
431 struct cons_channel *cc;
432 {
433 struct kd_softc *kd = &kd_softc;
434
435 kd->kd_in = cc;
436 cc->cc_upstream = kd_cons_input;
437 }
438
439 /*
440 * Default PROM-based console input stream
441 */
442 static int kd_rom_iopen __P((struct cons_channel *));
443 static int kd_rom_iclose __P((struct cons_channel *));
444
445 static struct cons_channel prom_cons_channel;
446
447 int
448 kd_rom_iopen(cc)
449 struct cons_channel *cc;
450 {
451 return (0);
452 }
453
454 int
455 kd_rom_iclose(cc)
456 struct cons_channel *cc;
457 {
458 return (0);
459 }
460
461 /*
462 * Our "interrupt" routine for input. This is called by
463 * the keyboard driver (dev/sun/kbd.c) at spltty.
464 */
465 void
466 kd_cons_input(c)
467 int c;
468 {
469 struct kd_softc *kd = &kd_softc;
470 struct tty *tp;
471
472 /* XXX: Make sure the device is open. */
473 tp = kd->kd_tty;
474 if (tp == NULL)
475 return;
476 if ((tp->t_state & TS_ISOPEN) == 0)
477 return;
478
479 (*linesw[tp->t_line].l_rint)(c, tp);
480 }
481
482
483 /****************************************************************
484 * kd console support
485 ****************************************************************/
486
487 /* The debugger gets its own key translation state. */
488 static struct kbd_state kdcn_state;
489
490 static void kdcnprobe __P((struct consdev *));
491 static void kdcninit __P((struct consdev *));
492 static int kdcngetc __P((dev_t));
493 static void kdcnputc __P((dev_t, int));
494 static void kdcnpollc __P((dev_t, int));
495
496 /* The keyboard driver uses cn_hw to access the real console driver */
497 extern struct consdev consdev_prom;
498 struct consdev *cn_hw = &consdev_prom;
499 struct consdev consdev_kd = {
500 kdcnprobe,
501 kdcninit,
502 kdcngetc,
503 kdcnputc,
504 kdcnpollc,
505 NULL,
506 };
507
508 /* We never call this. */
509 static void
510 kdcnprobe(cn)
511 struct consdev *cn;
512 {
513 }
514
515 static void
516 kdcninit(cn)
517 struct consdev *cn;
518 {
519 struct kbd_state *ks = &kdcn_state;
520
521 cn->cn_dev = makedev(KDMAJOR, 0);
522 cn->cn_pri = CN_INTERNAL;
523
524 /* This prepares kbd_translate() */
525 ks->kbd_id = KBD_MIN_TYPE;
526 kbd_xlate_init(ks);
527
528 /* Set up initial PROM input channel for /dev/console */
529 prom_cons_channel.cc_dev = NULL;
530 prom_cons_channel.cc_iopen = kd_rom_iopen;
531 prom_cons_channel.cc_iclose = kd_rom_iclose;
532 cons_attach_input(&prom_cons_channel);
533
534 /* Indicate that it is OK to use the PROM fbwrite */
535 kd_is_console = 1;
536 }
537
538 static int
539 kdcngetc(dev)
540 dev_t dev;
541 {
542 struct kbd_state *ks = &kdcn_state;
543 int code, class, data, keysym;
544
545 for (;;) {
546 code = (*cn_hw->cn_getc)(dev);
547 keysym = kbd_code_to_keysym(ks, code);
548 class = KEYSYM_CLASS(keysym);
549
550 switch (class) {
551 case KEYSYM_ASCII:
552 goto out;
553
554 case KEYSYM_CLRMOD:
555 case KEYSYM_SETMOD:
556 data = (keysym & 0x1F);
557 /* Only allow ctrl or shift. */
558 if (data > KBMOD_SHIFT_R)
559 break;
560 data = 1 << data;
561 if (class == KEYSYM_SETMOD)
562 ks->kbd_modbits |= data;
563 else
564 ks->kbd_modbits &= ~data;
565 break;
566
567 case KEYSYM_ALL_UP:
568 /* No toggle keys here. */
569 ks->kbd_modbits = 0;
570 break;
571
572 default: /* ignore all other keysyms */
573 break;
574 }
575 }
576 out:
577 return (keysym);
578 }
579
580 static void
581 kdcnputc(dev, c)
582 dev_t dev;
583 int c;
584 {
585 int s;
586 char c0 = (c & 0x7f);
587
588 s = splhigh();
589 OF_write(OF_stdout(), &c0, 1);
590 splx(s);
591 }
592
593 static void
594 kdcnpollc(dev, on)
595 dev_t dev;
596 int on;
597 {
598 struct kbd_state *ks = &kdcn_state;
599
600 if (on) {
601 /* Entering debugger. */
602 #if NFB > 0
603 fb_unblank();
604 #endif
605 /* Clear shift keys too. */
606 ks->kbd_modbits = 0;
607 } else {
608 /* Resuming kernel. */
609 }
610 (*cn_hw->cn_pollc)(dev, on);
611 }
612