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