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