kd.c revision 1.20 1 1.20 christos /* $NetBSD: kd.c,v 1.20 1996/10/13 03:47:32 christos Exp $ */
2 1.4 cgd
3 1.1 gwr /*
4 1.11 gwr * Copyright (c) 1994, 1995 Gordon W. Ross
5 1.1 gwr * All rights reserved.
6 1.1 gwr *
7 1.1 gwr * Redistribution and use in source and binary forms, with or without
8 1.1 gwr * modification, are permitted provided that the following conditions
9 1.1 gwr * are met:
10 1.1 gwr * 1. Redistributions of source code must retain the above copyright
11 1.1 gwr * notice, this list of conditions and the following disclaimer.
12 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 gwr * notice, this list of conditions and the following disclaimer in the
14 1.1 gwr * documentation and/or other materials provided with the distribution.
15 1.2 gwr * 3. The name of the author may not be used to endorse or promote products
16 1.1 gwr * derived from this software without specific prior written permission.
17 1.11 gwr * 4. All advertising materials mentioning features or use of this software
18 1.11 gwr * must display the following acknowledgement:
19 1.11 gwr * This product includes software developed by Gordon Ross
20 1.1 gwr *
21 1.2 gwr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.2 gwr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.2 gwr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.2 gwr * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.2 gwr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.2 gwr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.2 gwr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.2 gwr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.2 gwr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.2 gwr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 gwr */
32 1.1 gwr
33 1.1 gwr /*
34 1.1 gwr * Keyboard/Display device.
35 1.1 gwr *
36 1.1 gwr * This driver exists simply to provide a tty device that
37 1.1 gwr * the indirect console driver can point to.
38 1.1 gwr * The kbd driver sends its input here.
39 1.20 christos * Output goes to the screen via PROM printf.
40 1.1 gwr */
41 1.1 gwr
42 1.1 gwr #include <sys/param.h>
43 1.1 gwr #include <sys/proc.h>
44 1.1 gwr #include <sys/systm.h>
45 1.1 gwr #include <sys/ioctl.h>
46 1.1 gwr #include <sys/tty.h>
47 1.1 gwr #include <sys/file.h>
48 1.1 gwr #include <sys/conf.h>
49 1.1 gwr #include <sys/device.h>
50 1.1 gwr
51 1.1 gwr #include <machine/autoconf.h>
52 1.1 gwr #include <machine/mon.h>
53 1.11 gwr #include <machine/psl.h>
54 1.1 gwr
55 1.1 gwr #include <dev/cons.h>
56 1.14 gwr #include <dev/sun/kbd_xlate.h>
57 1.1 gwr
58 1.14 gwr #define KDMAJOR 1
59 1.14 gwr #define PUT_WSIZE 64
60 1.14 gwr
61 1.14 gwr cdev_decl(kd); /* open, close, read, write, ioctl, stop, ... */
62 1.14 gwr
63 1.14 gwr struct kd_softc {
64 1.14 gwr struct device kd_dev; /* required first: base device */
65 1.14 gwr struct tty *kd_tty;
66 1.14 gwr };
67 1.1 gwr
68 1.13 gwr /*
69 1.13 gwr * There is no point in pretending there might be
70 1.13 gwr * more than one keyboard/display device.
71 1.13 gwr */
72 1.14 gwr struct kd_softc kd_softc;
73 1.1 gwr
74 1.1 gwr static int kdparam(struct tty *, struct termios *);
75 1.1 gwr static void kdstart(struct tty *);
76 1.1 gwr
77 1.10 gwr int kd_is_console;
78 1.10 gwr
79 1.14 gwr /*
80 1.14 gwr * This is called by kbd_attach()
81 1.14 gwr * XXX - Make this a proper child of kbd?
82 1.14 gwr */
83 1.7 gwr void
84 1.14 gwr kd_init(unit)
85 1.14 gwr int unit;
86 1.1 gwr {
87 1.14 gwr struct kd_softc *kd;
88 1.14 gwr struct tty *tp;
89 1.14 gwr
90 1.14 gwr if (unit != 0)
91 1.14 gwr return;
92 1.14 gwr kd = &kd_softc; /* XXX */
93 1.17 gwr
94 1.14 gwr tp = ttymalloc();
95 1.14 gwr tp->t_oproc = kdstart;
96 1.14 gwr tp->t_param = kdparam;
97 1.14 gwr tp->t_dev = makedev(KDMAJOR, unit);
98 1.17 gwr tty_attach(tp);
99 1.17 gwr
100 1.17 gwr kd->kd_tty = tp;
101 1.14 gwr
102 1.14 gwr return;
103 1.13 gwr }
104 1.13 gwr
105 1.13 gwr struct tty *
106 1.13 gwr kdtty(dev)
107 1.13 gwr dev_t dev;
108 1.13 gwr {
109 1.14 gwr struct kd_softc *kd;
110 1.14 gwr
111 1.14 gwr kd = &kd_softc; /* XXX */
112 1.14 gwr return (kd->kd_tty);
113 1.1 gwr }
114 1.1 gwr
115 1.1 gwr int
116 1.1 gwr kdopen(dev, flag, mode, p)
117 1.1 gwr dev_t dev;
118 1.1 gwr int flag, mode;
119 1.1 gwr struct proc *p;
120 1.1 gwr {
121 1.14 gwr struct kd_softc *kd;
122 1.14 gwr int error, s, unit;
123 1.1 gwr struct tty *tp;
124 1.1 gwr
125 1.1 gwr unit = minor(dev);
126 1.14 gwr if (unit != 0)
127 1.1 gwr return ENXIO;
128 1.14 gwr kd = &kd_softc; /* XXX */
129 1.14 gwr tp = kd->kd_tty;
130 1.3 gwr
131 1.14 gwr if ((error = kbd_iopen(unit)) != 0) {
132 1.8 gwr #ifdef DIAGNOSTIC
133 1.20 christos printf("kd: kbd_iopen, error=%d\n", error);
134 1.8 gwr #endif
135 1.3 gwr return (error);
136 1.8 gwr }
137 1.1 gwr
138 1.14 gwr /* It's simpler to do this up here. */
139 1.14 gwr if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE))
140 1.14 gwr == (TS_ISOPEN | TS_XCLUDE))
141 1.14 gwr && (p->p_ucred->cr_uid != 0) )
142 1.14 gwr {
143 1.14 gwr return (EBUSY);
144 1.14 gwr }
145 1.14 gwr
146 1.14 gwr s = spltty();
147 1.14 gwr
148 1.1 gwr if ((tp->t_state & TS_ISOPEN) == 0) {
149 1.14 gwr /* First open. */
150 1.1 gwr ttychars(tp);
151 1.1 gwr tp->t_iflag = TTYDEF_IFLAG;
152 1.1 gwr tp->t_oflag = TTYDEF_OFLAG;
153 1.1 gwr tp->t_cflag = TTYDEF_CFLAG;
154 1.1 gwr tp->t_lflag = TTYDEF_LFLAG;
155 1.1 gwr tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
156 1.14 gwr (void) kdparam(tp, &tp->t_termios);
157 1.1 gwr ttsetwater(tp);
158 1.14 gwr /* Flush pending input? Clear translator? */
159 1.14 gwr /* This (pseudo)device always has SOFTCAR */
160 1.14 gwr tp->t_state |= TS_CARR_ON;
161 1.14 gwr }
162 1.14 gwr
163 1.14 gwr splx(s);
164 1.1 gwr
165 1.1 gwr return ((*linesw[tp->t_line].l_open)(dev, tp));
166 1.1 gwr }
167 1.1 gwr
168 1.1 gwr int
169 1.1 gwr kdclose(dev, flag, mode, p)
170 1.1 gwr dev_t dev;
171 1.1 gwr int flag, mode;
172 1.1 gwr struct proc *p;
173 1.1 gwr {
174 1.14 gwr struct kd_softc *kd;
175 1.14 gwr struct tty *tp;
176 1.14 gwr
177 1.14 gwr kd = &kd_softc; /* XXX */
178 1.14 gwr tp = kd->kd_tty;
179 1.14 gwr
180 1.14 gwr /* XXX This is for cons.c. */
181 1.14 gwr if ((tp->t_state & TS_ISOPEN) == 0)
182 1.14 gwr return 0;
183 1.1 gwr
184 1.1 gwr (*linesw[tp->t_line].l_close)(tp, flag);
185 1.1 gwr ttyclose(tp);
186 1.1 gwr return (0);
187 1.1 gwr }
188 1.1 gwr
189 1.1 gwr int
190 1.1 gwr kdread(dev, uio, flag)
191 1.1 gwr dev_t dev;
192 1.1 gwr struct uio *uio;
193 1.1 gwr int flag;
194 1.1 gwr {
195 1.14 gwr struct kd_softc *kd;
196 1.14 gwr struct tty *tp;
197 1.14 gwr
198 1.14 gwr kd = &kd_softc; /* XXX */
199 1.14 gwr tp = kd->kd_tty;
200 1.1 gwr
201 1.1 gwr return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
202 1.1 gwr }
203 1.1 gwr
204 1.1 gwr int
205 1.1 gwr kdwrite(dev, uio, flag)
206 1.1 gwr dev_t dev;
207 1.1 gwr struct uio *uio;
208 1.1 gwr int flag;
209 1.1 gwr {
210 1.14 gwr struct kd_softc *kd;
211 1.14 gwr struct tty *tp;
212 1.14 gwr
213 1.14 gwr kd = &kd_softc; /* XXX */
214 1.14 gwr tp = kd->kd_tty;
215 1.1 gwr
216 1.1 gwr return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
217 1.12 mycroft }
218 1.12 mycroft
219 1.12 mycroft int
220 1.1 gwr kdioctl(dev, cmd, data, flag, p)
221 1.1 gwr dev_t dev;
222 1.14 gwr u_long cmd;
223 1.1 gwr caddr_t data;
224 1.1 gwr int flag;
225 1.1 gwr struct proc *p;
226 1.1 gwr {
227 1.14 gwr struct kd_softc *kd;
228 1.14 gwr struct tty *tp;
229 1.1 gwr int error;
230 1.14 gwr
231 1.14 gwr kd = &kd_softc; /* XXX */
232 1.14 gwr tp = kd->kd_tty;
233 1.1 gwr
234 1.1 gwr error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
235 1.1 gwr if (error >= 0)
236 1.1 gwr return error;
237 1.1 gwr error = ttioctl(tp, cmd, data, flag, p);
238 1.1 gwr if (error >= 0)
239 1.1 gwr return error;
240 1.1 gwr
241 1.1 gwr /* Handle any ioctl commands specific to kbd/display. */
242 1.1 gwr /* XXX - Send KB* ioctls to kbd module? */
243 1.1 gwr /* XXX - Send FB* ioctls to fb module? */
244 1.1 gwr
245 1.1 gwr return ENOTTY;
246 1.1 gwr }
247 1.1 gwr
248 1.14 gwr
249 1.14 gwr static int
250 1.14 gwr kdparam(tp, t)
251 1.14 gwr struct tty *tp;
252 1.14 gwr struct termios *t;
253 1.14 gwr {
254 1.14 gwr /* XXX - These are ignored... */
255 1.14 gwr tp->t_ispeed = t->c_ispeed;
256 1.14 gwr tp->t_ospeed = t->c_ospeed;
257 1.14 gwr tp->t_cflag = t->c_cflag;
258 1.14 gwr return 0;
259 1.14 gwr }
260 1.14 gwr
261 1.14 gwr
262 1.18 mycroft void
263 1.14 gwr kdstop(tp, flag)
264 1.14 gwr struct tty *tp;
265 1.14 gwr int flag;
266 1.14 gwr {
267 1.14 gwr
268 1.14 gwr }
269 1.14 gwr
270 1.11 gwr static void kd_later(void*);
271 1.11 gwr static void kd_putfb(struct tty *);
272 1.11 gwr
273 1.1 gwr void
274 1.1 gwr kdstart(tp)
275 1.1 gwr struct tty *tp;
276 1.1 gwr {
277 1.1 gwr struct clist *cl;
278 1.11 gwr register int s;
279 1.1 gwr
280 1.1 gwr s = spltty();
281 1.1 gwr if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
282 1.1 gwr goto out;
283 1.11 gwr
284 1.1 gwr cl = &tp->t_outq;
285 1.11 gwr if (cl->c_cc) {
286 1.10 gwr if (kd_is_console) {
287 1.11 gwr tp->t_state |= TS_BUSY;
288 1.11 gwr if ((s & PSL_IPL) == 0) {
289 1.11 gwr /* called at level zero - update screen now. */
290 1.11 gwr (void) splsoftclock();
291 1.11 gwr kd_putfb(tp);
292 1.11 gwr (void) spltty();
293 1.11 gwr tp->t_state &= ~TS_BUSY;
294 1.11 gwr } else {
295 1.11 gwr /* called at interrupt level - do it later */
296 1.11 gwr timeout(kd_later, (void*)tp, 0);
297 1.11 gwr }
298 1.11 gwr } else {
299 1.11 gwr /*
300 1.11 gwr * This driver uses the PROM for writing the screen,
301 1.11 gwr * and that only works if this is the console device.
302 1.11 gwr * If this is not the console, just flush the output.
303 1.11 gwr * Sorry. (In that case, use xdm instead of getty.)
304 1.11 gwr */
305 1.11 gwr ndflush(cl, cl->c_cc);
306 1.11 gwr }
307 1.11 gwr }
308 1.11 gwr if (cl->c_cc <= tp->t_lowat) {
309 1.11 gwr if (tp->t_state & TS_ASLEEP) {
310 1.11 gwr tp->t_state &= ~TS_ASLEEP;
311 1.11 gwr wakeup((caddr_t)cl);
312 1.10 gwr }
313 1.11 gwr selwakeup(&tp->t_wsel);
314 1.1 gwr }
315 1.11 gwr out:
316 1.11 gwr splx(s);
317 1.11 gwr }
318 1.11 gwr
319 1.11 gwr /*
320 1.11 gwr * Timeout function to do delayed writes to the screen.
321 1.11 gwr * Called at splsoftclock when requested by kdstart.
322 1.11 gwr */
323 1.11 gwr static void
324 1.11 gwr kd_later(tpaddr)
325 1.11 gwr void *tpaddr;
326 1.11 gwr {
327 1.11 gwr struct tty *tp = tpaddr;
328 1.11 gwr register int s;
329 1.1 gwr
330 1.11 gwr kd_putfb(tp);
331 1.11 gwr
332 1.11 gwr s = spltty();
333 1.1 gwr tp->t_state &= ~TS_BUSY;
334 1.16 gwr (*linesw[tp->t_line].l_start)(tp);
335 1.11 gwr splx(s);
336 1.11 gwr }
337 1.11 gwr
338 1.11 gwr /*
339 1.11 gwr * Put text on the screen using the PROM monitor.
340 1.11 gwr * This can take a while, so to avoid missing
341 1.11 gwr * interrupts, this is called at splsoftclock.
342 1.11 gwr */
343 1.11 gwr static void kd_putfb(tp)
344 1.11 gwr struct tty *tp;
345 1.11 gwr {
346 1.14 gwr char buf[PUT_WSIZE];
347 1.11 gwr struct clist *cl = &tp->t_outq;
348 1.11 gwr char *p, *end;
349 1.11 gwr int len;
350 1.11 gwr
351 1.14 gwr while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
352 1.11 gwr /* PROM will barf if high bits are set. */
353 1.11 gwr p = buf;
354 1.11 gwr end = buf + len;
355 1.11 gwr while (p < end)
356 1.11 gwr *p++ &= 0x7f;
357 1.11 gwr (romVectorPtr->fbWriteStr)(buf, len);
358 1.1 gwr }
359 1.1 gwr }
360 1.1 gwr
361 1.14 gwr /*
362 1.15 gwr * Our "interrupt" routine for input. This is called by
363 1.15 gwr * the keyboard driver (dev/sun/kbd.c) at spltty.
364 1.14 gwr */
365 1.14 gwr void
366 1.14 gwr kd_input(c)
367 1.14 gwr int c;
368 1.14 gwr {
369 1.14 gwr struct kd_softc *kd = &kd_softc;
370 1.14 gwr struct tty *tp;
371 1.14 gwr
372 1.14 gwr /* XXX: Make sure the device is open. */
373 1.14 gwr tp = kd->kd_tty;
374 1.14 gwr if (tp == NULL)
375 1.14 gwr return;
376 1.14 gwr if ((tp->t_state & TS_ISOPEN) == 0)
377 1.14 gwr return;
378 1.11 gwr
379 1.16 gwr (*linesw[tp->t_line].l_rint)(c, tp);
380 1.1 gwr }
381 1.1 gwr
382 1.1 gwr
383 1.14 gwr /****************************************************************
384 1.1 gwr * kd console support
385 1.14 gwr ****************************************************************/
386 1.1 gwr
387 1.14 gwr extern void *zs_conschan;
388 1.14 gwr extern int zs_getc();
389 1.14 gwr extern void nullcnprobe();
390 1.14 gwr cons_decl(kd);
391 1.1 gwr
392 1.14 gwr /* The debugger gets its own key translation state. */
393 1.14 gwr static struct kbd_state kdcn_state;
394 1.1 gwr
395 1.14 gwr void
396 1.14 gwr kdcninit(cn)
397 1.14 gwr struct consdev *cn;
398 1.1 gwr {
399 1.14 gwr struct kbd_state *ks = &kdcn_state;
400 1.8 gwr
401 1.14 gwr mon_printf("console on kd0 (keyboard/display)\n");
402 1.8 gwr
403 1.8 gwr /* This prepares kbd_translate() */
404 1.14 gwr ks->kbd_id = KBD_MIN_TYPE;
405 1.14 gwr kbd_xlate_init(ks);
406 1.10 gwr
407 1.10 gwr /* Indicate that it is OK to use the PROM fbwrite */
408 1.10 gwr kd_is_console = 1;
409 1.1 gwr }
410 1.1 gwr
411 1.14 gwr int
412 1.1 gwr kdcngetc(dev)
413 1.1 gwr dev_t dev;
414 1.1 gwr {
415 1.14 gwr struct kbd_state *ks = &kdcn_state;
416 1.14 gwr int code, class, data, keysym;
417 1.14 gwr
418 1.14 gwr for (;;) {
419 1.14 gwr code = zs_getc(zs_conschan);
420 1.14 gwr keysym = kbd_code_to_keysym(ks, code);
421 1.14 gwr class = KEYSYM_CLASS(keysym);
422 1.14 gwr
423 1.14 gwr switch (class) {
424 1.14 gwr case KEYSYM_ASCII:
425 1.14 gwr goto out;
426 1.14 gwr
427 1.14 gwr case KEYSYM_CLRMOD:
428 1.14 gwr case KEYSYM_SETMOD:
429 1.14 gwr data = (keysym & 0x1F);
430 1.14 gwr /* Only allow ctrl or shift. */
431 1.14 gwr if (data > KBMOD_SHIFT_R)
432 1.14 gwr break;
433 1.14 gwr data = 1 << data;
434 1.14 gwr if (class == KEYSYM_SETMOD)
435 1.14 gwr ks->kbd_modbits |= data;
436 1.14 gwr else
437 1.14 gwr ks->kbd_modbits &= ~data;
438 1.14 gwr break;
439 1.1 gwr
440 1.14 gwr case KEYSYM_ALL_UP:
441 1.14 gwr /* No toggle keys here. */
442 1.14 gwr ks->kbd_modbits = 0;
443 1.14 gwr break;
444 1.1 gwr
445 1.14 gwr default: /* ignore all other keysyms */
446 1.14 gwr break;
447 1.14 gwr }
448 1.14 gwr }
449 1.14 gwr out:
450 1.14 gwr return (keysym);
451 1.1 gwr }
452 1.1 gwr
453 1.14 gwr void
454 1.1 gwr kdcnputc(dev, c)
455 1.1 gwr dev_t dev;
456 1.1 gwr int c;
457 1.1 gwr {
458 1.11 gwr (romVectorPtr->fbWriteChar)(c & 0x7f);
459 1.9 gwr }
460 1.9 gwr
461 1.9 gwr extern void fb_unblank();
462 1.9 gwr void kdcnpollc(dev, on)
463 1.9 gwr dev_t dev;
464 1.9 gwr int on;
465 1.9 gwr {
466 1.14 gwr struct kbd_state *ks = &kdcn_state;
467 1.14 gwr
468 1.14 gwr if (on) {
469 1.14 gwr /* Entering debugger. */
470 1.9 gwr fb_unblank();
471 1.14 gwr /* Clear shift keys too. */
472 1.14 gwr ks->kbd_modbits = 0;
473 1.14 gwr } else {
474 1.14 gwr /* Resuming kernel. */
475 1.14 gwr }
476 1.1 gwr }
477 1.14 gwr
478 1.14 gwr struct consdev consdev_kd = {
479 1.14 gwr nullcnprobe,
480 1.14 gwr kdcninit,
481 1.14 gwr kdcngetc,
482 1.14 gwr kdcnputc,
483 1.14 gwr kdcnpollc,
484 1.14 gwr makedev(KDMAJOR, 0),
485 1.14 gwr CN_INTERNAL
486 1.14 gwr };
487 1.14 gwr
488