kd.c revision 1.16 1 1.16 gwr /* $NetBSD: kd.c,v 1.16 1996/04/26 18:36:54 gwr 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.1 gwr * 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.14 gwr tp = ttymalloc();
94 1.1 gwr
95 1.14 gwr kd->kd_tty = tp;
96 1.14 gwr tp->t_oproc = kdstart;
97 1.14 gwr tp->t_param = kdparam;
98 1.14 gwr tp->t_dev = makedev(KDMAJOR, unit);
99 1.14 gwr
100 1.14 gwr return;
101 1.13 gwr }
102 1.13 gwr
103 1.13 gwr struct tty *
104 1.13 gwr kdtty(dev)
105 1.13 gwr dev_t dev;
106 1.13 gwr {
107 1.14 gwr struct kd_softc *kd;
108 1.14 gwr
109 1.14 gwr kd = &kd_softc; /* XXX */
110 1.14 gwr return (kd->kd_tty);
111 1.1 gwr }
112 1.1 gwr
113 1.1 gwr int
114 1.1 gwr kdopen(dev, flag, mode, p)
115 1.1 gwr dev_t dev;
116 1.1 gwr int flag, mode;
117 1.1 gwr struct proc *p;
118 1.1 gwr {
119 1.14 gwr struct kd_softc *kd;
120 1.14 gwr int error, s, unit;
121 1.1 gwr struct tty *tp;
122 1.1 gwr
123 1.1 gwr unit = minor(dev);
124 1.14 gwr if (unit != 0)
125 1.1 gwr return ENXIO;
126 1.14 gwr kd = &kd_softc; /* XXX */
127 1.14 gwr tp = kd->kd_tty;
128 1.3 gwr
129 1.14 gwr if ((error = kbd_iopen(unit)) != 0) {
130 1.8 gwr #ifdef DIAGNOSTIC
131 1.8 gwr printf("kd: kbd_iopen, error=%d\n", error);
132 1.8 gwr #endif
133 1.3 gwr return (error);
134 1.8 gwr }
135 1.1 gwr
136 1.14 gwr /* It's simpler to do this up here. */
137 1.14 gwr if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE))
138 1.14 gwr == (TS_ISOPEN | TS_XCLUDE))
139 1.14 gwr && (p->p_ucred->cr_uid != 0) )
140 1.14 gwr {
141 1.14 gwr return (EBUSY);
142 1.14 gwr }
143 1.14 gwr
144 1.14 gwr s = spltty();
145 1.14 gwr
146 1.1 gwr if ((tp->t_state & TS_ISOPEN) == 0) {
147 1.14 gwr /* First open. */
148 1.1 gwr ttychars(tp);
149 1.1 gwr tp->t_iflag = TTYDEF_IFLAG;
150 1.1 gwr tp->t_oflag = TTYDEF_OFLAG;
151 1.1 gwr tp->t_cflag = TTYDEF_CFLAG;
152 1.1 gwr tp->t_lflag = TTYDEF_LFLAG;
153 1.1 gwr tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
154 1.14 gwr (void) kdparam(tp, &tp->t_termios);
155 1.1 gwr ttsetwater(tp);
156 1.14 gwr /* Flush pending input? Clear translator? */
157 1.14 gwr /* This (pseudo)device always has SOFTCAR */
158 1.14 gwr tp->t_state |= TS_CARR_ON;
159 1.14 gwr }
160 1.14 gwr
161 1.14 gwr splx(s);
162 1.1 gwr
163 1.1 gwr return ((*linesw[tp->t_line].l_open)(dev, tp));
164 1.1 gwr }
165 1.1 gwr
166 1.1 gwr int
167 1.1 gwr kdclose(dev, flag, mode, p)
168 1.1 gwr dev_t dev;
169 1.1 gwr int flag, mode;
170 1.1 gwr struct proc *p;
171 1.1 gwr {
172 1.14 gwr struct kd_softc *kd;
173 1.14 gwr struct tty *tp;
174 1.14 gwr
175 1.14 gwr kd = &kd_softc; /* XXX */
176 1.14 gwr tp = kd->kd_tty;
177 1.14 gwr
178 1.14 gwr /* XXX This is for cons.c. */
179 1.14 gwr if ((tp->t_state & TS_ISOPEN) == 0)
180 1.14 gwr return 0;
181 1.1 gwr
182 1.1 gwr (*linesw[tp->t_line].l_close)(tp, flag);
183 1.1 gwr ttyclose(tp);
184 1.1 gwr return (0);
185 1.1 gwr }
186 1.1 gwr
187 1.1 gwr int
188 1.1 gwr kdread(dev, uio, flag)
189 1.1 gwr dev_t dev;
190 1.1 gwr struct uio *uio;
191 1.1 gwr int flag;
192 1.1 gwr {
193 1.14 gwr struct kd_softc *kd;
194 1.14 gwr struct tty *tp;
195 1.14 gwr
196 1.14 gwr kd = &kd_softc; /* XXX */
197 1.14 gwr tp = kd->kd_tty;
198 1.1 gwr
199 1.1 gwr return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
200 1.1 gwr }
201 1.1 gwr
202 1.1 gwr int
203 1.1 gwr kdwrite(dev, uio, flag)
204 1.1 gwr dev_t dev;
205 1.1 gwr struct uio *uio;
206 1.1 gwr int flag;
207 1.1 gwr {
208 1.14 gwr struct kd_softc *kd;
209 1.14 gwr struct tty *tp;
210 1.14 gwr
211 1.14 gwr kd = &kd_softc; /* XXX */
212 1.14 gwr tp = kd->kd_tty;
213 1.1 gwr
214 1.1 gwr return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
215 1.12 mycroft }
216 1.12 mycroft
217 1.12 mycroft int
218 1.1 gwr kdioctl(dev, cmd, data, flag, p)
219 1.1 gwr dev_t dev;
220 1.14 gwr u_long cmd;
221 1.1 gwr caddr_t data;
222 1.1 gwr int flag;
223 1.1 gwr struct proc *p;
224 1.1 gwr {
225 1.14 gwr struct kd_softc *kd;
226 1.14 gwr struct tty *tp;
227 1.1 gwr int error;
228 1.14 gwr
229 1.14 gwr kd = &kd_softc; /* XXX */
230 1.14 gwr tp = kd->kd_tty;
231 1.1 gwr
232 1.1 gwr error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
233 1.1 gwr if (error >= 0)
234 1.1 gwr return error;
235 1.1 gwr error = ttioctl(tp, cmd, data, flag, p);
236 1.1 gwr if (error >= 0)
237 1.1 gwr return error;
238 1.1 gwr
239 1.1 gwr /* Handle any ioctl commands specific to kbd/display. */
240 1.1 gwr /* XXX - Send KB* ioctls to kbd module? */
241 1.1 gwr /* XXX - Send FB* ioctls to fb module? */
242 1.1 gwr
243 1.1 gwr return ENOTTY;
244 1.1 gwr }
245 1.1 gwr
246 1.14 gwr
247 1.14 gwr static int
248 1.14 gwr kdparam(tp, t)
249 1.14 gwr struct tty *tp;
250 1.14 gwr struct termios *t;
251 1.14 gwr {
252 1.14 gwr /* XXX - These are ignored... */
253 1.14 gwr tp->t_ispeed = t->c_ispeed;
254 1.14 gwr tp->t_ospeed = t->c_ospeed;
255 1.14 gwr tp->t_cflag = t->c_cflag;
256 1.14 gwr return 0;
257 1.14 gwr }
258 1.14 gwr
259 1.14 gwr
260 1.14 gwr int
261 1.14 gwr kdstop(tp, flag)
262 1.14 gwr struct tty *tp;
263 1.14 gwr int flag;
264 1.14 gwr {
265 1.14 gwr
266 1.14 gwr }
267 1.14 gwr
268 1.11 gwr static void kd_later(void*);
269 1.11 gwr static void kd_putfb(struct tty *);
270 1.11 gwr
271 1.1 gwr void
272 1.1 gwr kdstart(tp)
273 1.1 gwr struct tty *tp;
274 1.1 gwr {
275 1.1 gwr struct clist *cl;
276 1.11 gwr register int s;
277 1.1 gwr
278 1.1 gwr s = spltty();
279 1.1 gwr if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
280 1.1 gwr goto out;
281 1.11 gwr
282 1.1 gwr cl = &tp->t_outq;
283 1.11 gwr if (cl->c_cc) {
284 1.10 gwr if (kd_is_console) {
285 1.11 gwr tp->t_state |= TS_BUSY;
286 1.11 gwr if ((s & PSL_IPL) == 0) {
287 1.11 gwr /* called at level zero - update screen now. */
288 1.11 gwr (void) splsoftclock();
289 1.11 gwr kd_putfb(tp);
290 1.11 gwr (void) spltty();
291 1.11 gwr tp->t_state &= ~TS_BUSY;
292 1.11 gwr } else {
293 1.11 gwr /* called at interrupt level - do it later */
294 1.11 gwr timeout(kd_later, (void*)tp, 0);
295 1.11 gwr }
296 1.11 gwr } else {
297 1.11 gwr /*
298 1.11 gwr * This driver uses the PROM for writing the screen,
299 1.11 gwr * and that only works if this is the console device.
300 1.11 gwr * If this is not the console, just flush the output.
301 1.11 gwr * Sorry. (In that case, use xdm instead of getty.)
302 1.11 gwr */
303 1.11 gwr ndflush(cl, cl->c_cc);
304 1.11 gwr }
305 1.11 gwr }
306 1.11 gwr if (cl->c_cc <= tp->t_lowat) {
307 1.11 gwr if (tp->t_state & TS_ASLEEP) {
308 1.11 gwr tp->t_state &= ~TS_ASLEEP;
309 1.11 gwr wakeup((caddr_t)cl);
310 1.10 gwr }
311 1.11 gwr selwakeup(&tp->t_wsel);
312 1.1 gwr }
313 1.11 gwr out:
314 1.11 gwr splx(s);
315 1.11 gwr }
316 1.11 gwr
317 1.11 gwr /*
318 1.11 gwr * Timeout function to do delayed writes to the screen.
319 1.11 gwr * Called at splsoftclock when requested by kdstart.
320 1.11 gwr */
321 1.11 gwr static void
322 1.11 gwr kd_later(tpaddr)
323 1.11 gwr void *tpaddr;
324 1.11 gwr {
325 1.11 gwr struct tty *tp = tpaddr;
326 1.11 gwr register int s;
327 1.1 gwr
328 1.11 gwr kd_putfb(tp);
329 1.11 gwr
330 1.11 gwr s = spltty();
331 1.1 gwr tp->t_state &= ~TS_BUSY;
332 1.16 gwr (*linesw[tp->t_line].l_start)(tp);
333 1.11 gwr splx(s);
334 1.11 gwr }
335 1.11 gwr
336 1.11 gwr /*
337 1.11 gwr * Put text on the screen using the PROM monitor.
338 1.11 gwr * This can take a while, so to avoid missing
339 1.11 gwr * interrupts, this is called at splsoftclock.
340 1.11 gwr */
341 1.11 gwr static void kd_putfb(tp)
342 1.11 gwr struct tty *tp;
343 1.11 gwr {
344 1.14 gwr char buf[PUT_WSIZE];
345 1.11 gwr struct clist *cl = &tp->t_outq;
346 1.11 gwr char *p, *end;
347 1.11 gwr int len;
348 1.11 gwr
349 1.14 gwr while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
350 1.11 gwr /* PROM will barf if high bits are set. */
351 1.11 gwr p = buf;
352 1.11 gwr end = buf + len;
353 1.11 gwr while (p < end)
354 1.11 gwr *p++ &= 0x7f;
355 1.11 gwr (romVectorPtr->fbWriteStr)(buf, len);
356 1.1 gwr }
357 1.1 gwr }
358 1.1 gwr
359 1.14 gwr /*
360 1.15 gwr * Our "interrupt" routine for input. This is called by
361 1.15 gwr * the keyboard driver (dev/sun/kbd.c) at spltty.
362 1.14 gwr */
363 1.14 gwr void
364 1.14 gwr kd_input(c)
365 1.14 gwr int c;
366 1.14 gwr {
367 1.14 gwr struct kd_softc *kd = &kd_softc;
368 1.14 gwr struct tty *tp;
369 1.14 gwr
370 1.14 gwr /* XXX: Make sure the device is open. */
371 1.14 gwr tp = kd->kd_tty;
372 1.14 gwr if (tp == NULL)
373 1.14 gwr return;
374 1.14 gwr if ((tp->t_state & TS_ISOPEN) == 0)
375 1.14 gwr return;
376 1.11 gwr
377 1.16 gwr (*linesw[tp->t_line].l_rint)(c, tp);
378 1.1 gwr }
379 1.1 gwr
380 1.1 gwr
381 1.14 gwr /****************************************************************
382 1.1 gwr * kd console support
383 1.14 gwr ****************************************************************/
384 1.1 gwr
385 1.14 gwr extern void *zs_conschan;
386 1.14 gwr extern int zs_getc();
387 1.14 gwr extern void nullcnprobe();
388 1.14 gwr cons_decl(kd);
389 1.1 gwr
390 1.14 gwr /* The debugger gets its own key translation state. */
391 1.14 gwr static struct kbd_state kdcn_state;
392 1.1 gwr
393 1.14 gwr void
394 1.14 gwr kdcninit(cn)
395 1.14 gwr struct consdev *cn;
396 1.1 gwr {
397 1.14 gwr struct kbd_state *ks = &kdcn_state;
398 1.8 gwr
399 1.14 gwr mon_printf("console on kd0 (keyboard/display)\n");
400 1.8 gwr
401 1.8 gwr /* This prepares kbd_translate() */
402 1.14 gwr ks->kbd_id = KBD_MIN_TYPE;
403 1.14 gwr kbd_xlate_init(ks);
404 1.10 gwr
405 1.10 gwr /* Indicate that it is OK to use the PROM fbwrite */
406 1.10 gwr kd_is_console = 1;
407 1.1 gwr }
408 1.1 gwr
409 1.14 gwr int
410 1.1 gwr kdcngetc(dev)
411 1.1 gwr dev_t dev;
412 1.1 gwr {
413 1.14 gwr struct kbd_state *ks = &kdcn_state;
414 1.14 gwr int code, class, data, keysym;
415 1.14 gwr
416 1.14 gwr for (;;) {
417 1.14 gwr code = zs_getc(zs_conschan);
418 1.14 gwr keysym = kbd_code_to_keysym(ks, code);
419 1.14 gwr class = KEYSYM_CLASS(keysym);
420 1.14 gwr
421 1.14 gwr switch (class) {
422 1.14 gwr case KEYSYM_ASCII:
423 1.14 gwr goto out;
424 1.14 gwr
425 1.14 gwr case KEYSYM_CLRMOD:
426 1.14 gwr case KEYSYM_SETMOD:
427 1.14 gwr data = (keysym & 0x1F);
428 1.14 gwr /* Only allow ctrl or shift. */
429 1.14 gwr if (data > KBMOD_SHIFT_R)
430 1.14 gwr break;
431 1.14 gwr data = 1 << data;
432 1.14 gwr if (class == KEYSYM_SETMOD)
433 1.14 gwr ks->kbd_modbits |= data;
434 1.14 gwr else
435 1.14 gwr ks->kbd_modbits &= ~data;
436 1.14 gwr break;
437 1.1 gwr
438 1.14 gwr case KEYSYM_ALL_UP:
439 1.14 gwr /* No toggle keys here. */
440 1.14 gwr ks->kbd_modbits = 0;
441 1.14 gwr break;
442 1.1 gwr
443 1.14 gwr default: /* ignore all other keysyms */
444 1.14 gwr break;
445 1.14 gwr }
446 1.14 gwr }
447 1.14 gwr out:
448 1.14 gwr return (keysym);
449 1.1 gwr }
450 1.1 gwr
451 1.14 gwr void
452 1.1 gwr kdcnputc(dev, c)
453 1.1 gwr dev_t dev;
454 1.1 gwr int c;
455 1.1 gwr {
456 1.11 gwr (romVectorPtr->fbWriteChar)(c & 0x7f);
457 1.9 gwr }
458 1.9 gwr
459 1.9 gwr extern void fb_unblank();
460 1.9 gwr void kdcnpollc(dev, on)
461 1.9 gwr dev_t dev;
462 1.9 gwr int on;
463 1.9 gwr {
464 1.14 gwr struct kbd_state *ks = &kdcn_state;
465 1.14 gwr
466 1.14 gwr if (on) {
467 1.14 gwr /* Entering debugger. */
468 1.9 gwr fb_unblank();
469 1.14 gwr /* Clear shift keys too. */
470 1.14 gwr ks->kbd_modbits = 0;
471 1.14 gwr } else {
472 1.14 gwr /* Resuming kernel. */
473 1.14 gwr }
474 1.1 gwr }
475 1.14 gwr
476 1.14 gwr struct consdev consdev_kd = {
477 1.14 gwr nullcnprobe,
478 1.14 gwr kdcninit,
479 1.14 gwr kdcngetc,
480 1.14 gwr kdcnputc,
481 1.14 gwr kdcnpollc,
482 1.14 gwr makedev(KDMAJOR, 0),
483 1.14 gwr CN_INTERNAL
484 1.14 gwr };
485 1.14 gwr
486