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