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