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