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