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