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