wskbd.c revision 1.69.2.6 1 /* $NetBSD: wskbd.c,v 1.69.2.6 2005/03/04 16:51:14 skrll Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
5 *
6 * Keysym translator:
7 * Contributed to The NetBSD Foundation by Juergen Hannken-Illjes.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Christopher G. Demetriou
20 * for the NetBSD Project.
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /*
37 * Copyright (c) 1992, 1993
38 * The Regents of the University of California. All rights reserved.
39 *
40 * This software was developed by the Computer Systems Engineering group
41 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
42 * contributed to Berkeley.
43 *
44 * All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Lawrence Berkeley Laboratory.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 * 3. Neither the name of the University nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * SUCH DAMAGE.
72 *
73 * @(#)kbd.c 8.2 (Berkeley) 10/30/93
74 */
75
76 /*
77 * Keyboard driver (/dev/wskbd*). Translates incoming bytes to ASCII or
78 * to `wscons_events' and passes them up to the appropriate reader.
79 */
80
81 #include <sys/cdefs.h>
82 __KERNEL_RCSID(0, "$NetBSD: wskbd.c,v 1.69.2.6 2005/03/04 16:51:14 skrll Exp $");
83
84 #include "opt_ddb.h"
85 #include "opt_kgdb.h"
86 #include "opt_wsdisplay_compat.h"
87
88 #include "wsdisplay.h"
89 #include "wskbd.h"
90 #include "wsmux.h"
91
92 #include <sys/param.h>
93 #include <sys/conf.h>
94 #include <sys/device.h>
95 #include <sys/ioctl.h>
96 #include <sys/kernel.h>
97 #include <sys/proc.h>
98 #include <sys/syslog.h>
99 #include <sys/systm.h>
100 #include <sys/callout.h>
101 #include <sys/malloc.h>
102 #include <sys/tty.h>
103 #include <sys/signalvar.h>
104 #include <sys/errno.h>
105 #include <sys/fcntl.h>
106 #include <sys/vnode.h>
107
108 #include <dev/wscons/wsconsio.h>
109 #include <dev/wscons/wskbdvar.h>
110 #include <dev/wscons/wsksymdef.h>
111 #include <dev/wscons/wsksymvar.h>
112 #include <dev/wscons/wsdisplayvar.h>
113 #include <dev/wscons/wseventvar.h>
114 #include <dev/wscons/wscons_callbacks.h>
115
116 #ifdef KGDB
117 #include <sys/kgdb.h>
118 #endif
119
120 #ifdef WSKBD_DEBUG
121 #define DPRINTF(x) if (wskbddebug) printf x
122 int wskbddebug = 0;
123 #else
124 #define DPRINTF(x)
125 #endif
126
127 #include <dev/wscons/wsmuxvar.h>
128
129 struct wskbd_internal {
130 const struct wskbd_mapdata *t_keymap;
131
132 const struct wskbd_consops *t_consops;
133 void *t_consaccesscookie;
134
135 int t_modifiers;
136 int t_composelen; /* remaining entries in t_composebuf */
137 keysym_t t_composebuf[2];
138
139 int t_flags;
140 #define WSKFL_METAESC 1
141
142 #define MAXKEYSYMSPERKEY 2 /* ESC <key> at max */
143 keysym_t t_symbols[MAXKEYSYMSPERKEY];
144
145 struct wskbd_softc *t_sc; /* back pointer */
146 };
147
148 struct wskbd_softc {
149 struct wsevsrc sc_base;
150
151 struct wskbd_internal *id;
152
153 const struct wskbd_accessops *sc_accessops;
154 void *sc_accesscookie;
155
156 int sc_ledstate;
157
158 int sc_isconsole;
159
160 struct wskbd_bell_data sc_bell_data;
161 struct wskbd_keyrepeat_data sc_keyrepeat_data;
162 #ifdef WSDISPLAY_SCROLLSUPPORT
163 struct wskbd_scroll_data sc_scroll_data;
164 #endif
165
166 int sc_repeating; /* we've called timeout() */
167 struct callout sc_repeat_ch;
168
169 int sc_translating; /* xlate to chars for emulation */
170
171 int sc_maplen; /* number of entries in sc_map */
172 struct wscons_keymap *sc_map; /* current translation map */
173 kbd_t sc_layout; /* current layout */
174
175 int sc_refcnt;
176 u_char sc_dying; /* device is being detached */
177 };
178
179 #define MOD_SHIFT_L (1 << 0)
180 #define MOD_SHIFT_R (1 << 1)
181 #define MOD_SHIFTLOCK (1 << 2)
182 #define MOD_CAPSLOCK (1 << 3)
183 #define MOD_CONTROL_L (1 << 4)
184 #define MOD_CONTROL_R (1 << 5)
185 #define MOD_META_L (1 << 6)
186 #define MOD_META_R (1 << 7)
187 #define MOD_MODESHIFT (1 << 8)
188 #define MOD_NUMLOCK (1 << 9)
189 #define MOD_COMPOSE (1 << 10)
190 #define MOD_HOLDSCREEN (1 << 11)
191 #define MOD_COMMAND (1 << 12)
192 #define MOD_COMMAND1 (1 << 13)
193 #define MOD_COMMAND2 (1 << 14)
194
195 #define MOD_ANYSHIFT (MOD_SHIFT_L | MOD_SHIFT_R | MOD_SHIFTLOCK)
196 #define MOD_ANYCONTROL (MOD_CONTROL_L | MOD_CONTROL_R)
197 #define MOD_ANYMETA (MOD_META_L | MOD_META_R)
198
199 #define MOD_ONESET(id, mask) (((id)->t_modifiers & (mask)) != 0)
200 #define MOD_ALLSET(id, mask) (((id)->t_modifiers & (mask)) == (mask))
201
202 #define GETMODSTATE(src, dst) \
203 do { \
204 dst |= (src & MOD_SHIFT_L) ? MOD_SHIFT_L : 0; \
205 dst |= (src & MOD_SHIFT_R) ? MOD_SHIFT_R : 0; \
206 dst |= (src & MOD_CONTROL_L) ? MOD_CONTROL_L : 0; \
207 dst |= (src & MOD_CONTROL_R) ? MOD_CONTROL_R : 0; \
208 dst |= (src & MOD_META_L) ? MOD_META_L : 0; \
209 dst |= (src & MOD_META_R) ? MOD_META_R : 0; \
210 } while (0)
211
212 static int wskbd_match(struct device *, struct cfdata *, void *);
213 static void wskbd_attach(struct device *, struct device *, void *);
214 static int wskbd_detach(struct device *, int);
215 static int wskbd_activate(struct device *, enum devact);
216
217 static int wskbd_displayioctl(struct device *, u_long, caddr_t, int,
218 struct lwp *);
219 #if NWSDISPLAY > 0
220 static int wskbd_set_display(struct device *, struct wsevsrc *);
221 #else
222 #define wskbd_set_display NULL
223 #endif
224
225 static inline void update_leds(struct wskbd_internal *);
226 static inline void update_modifier(struct wskbd_internal *, u_int, int, int);
227 static int internal_command(struct wskbd_softc *, u_int *, keysym_t, keysym_t);
228 static int wskbd_translate(struct wskbd_internal *, u_int, int);
229 static int wskbd_enable(struct wskbd_softc *, int);
230 #if NWSDISPLAY > 0
231 static void change_displayparam(struct wskbd_softc *, int, int, int);
232 static void wskbd_holdscreen(struct wskbd_softc *, int);
233 #endif
234
235 static int wskbd_do_ioctl_sc(struct wskbd_softc *, u_long, caddr_t, int,
236 struct lwp *);
237
238 #if NWSMUX > 0
239 static int wskbd_mux_open(struct wsevsrc *, struct wseventvar *);
240 static int wskbd_mux_close(struct wsevsrc *);
241 #else
242 #define wskbd_mux_open NULL
243 #define wskbd_mux_close NULL
244 #endif
245
246 static int wskbd_do_open(struct wskbd_softc *, struct wseventvar *);
247 static int wskbd_do_ioctl(struct device *, u_long, caddr_t, int, struct lwp *);
248
249 CFATTACH_DECL(wskbd, sizeof (struct wskbd_softc),
250 wskbd_match, wskbd_attach, wskbd_detach, wskbd_activate);
251
252 extern struct cfdriver wskbd_cd;
253
254 dev_type_open(wskbdopen);
255 dev_type_close(wskbdclose);
256 dev_type_read(wskbdread);
257 dev_type_ioctl(wskbdioctl);
258 dev_type_poll(wskbdpoll);
259 dev_type_kqfilter(wskbdkqfilter);
260
261 const struct cdevsw wskbd_cdevsw = {
262 wskbdopen, wskbdclose, wskbdread, nowrite, wskbdioctl,
263 nostop, notty, wskbdpoll, nommap, wskbdkqfilter,
264 };
265
266 #ifndef WSKBD_DEFAULT_BELL_PITCH
267 #define WSKBD_DEFAULT_BELL_PITCH 1500 /* 1500Hz */
268 #endif
269 #ifndef WSKBD_DEFAULT_BELL_PERIOD
270 #define WSKBD_DEFAULT_BELL_PERIOD 100 /* 100ms */
271 #endif
272 #ifndef WSKBD_DEFAULT_BELL_VOLUME
273 #define WSKBD_DEFAULT_BELL_VOLUME 50 /* 50% volume */
274 #endif
275
276 struct wskbd_bell_data wskbd_default_bell_data = {
277 WSKBD_BELL_DOALL,
278 WSKBD_DEFAULT_BELL_PITCH,
279 WSKBD_DEFAULT_BELL_PERIOD,
280 WSKBD_DEFAULT_BELL_VOLUME,
281 };
282
283 #ifdef WSDISPLAY_SCROLLSUPPORT
284 struct wskbd_scroll_data wskbd_default_scroll_data = {
285 WSKBD_SCROLL_DOALL,
286 WSKBD_SCROLL_MODE_NORMAL,
287 #ifdef WSDISPLAY_SCROLLCOMBO
288 WSDISPLAY_SCROLLCOMBO,
289 #else
290 MOD_SHIFT_L,
291 #endif
292 };
293 #endif
294
295 #ifndef WSKBD_DEFAULT_KEYREPEAT_DEL1
296 #define WSKBD_DEFAULT_KEYREPEAT_DEL1 400 /* 400ms to start repeating */
297 #endif
298 #ifndef WSKBD_DEFAULT_KEYREPEAT_DELN
299 #define WSKBD_DEFAULT_KEYREPEAT_DELN 100 /* 100ms to between repeats */
300 #endif
301
302 struct wskbd_keyrepeat_data wskbd_default_keyrepeat_data = {
303 WSKBD_KEYREPEAT_DOALL,
304 WSKBD_DEFAULT_KEYREPEAT_DEL1,
305 WSKBD_DEFAULT_KEYREPEAT_DELN,
306 };
307
308 #if NWSDISPLAY > 0 || NWSMUX > 0
309 struct wssrcops wskbd_srcops = {
310 WSMUX_KBD,
311 wskbd_mux_open, wskbd_mux_close, wskbd_do_ioctl,
312 wskbd_displayioctl, wskbd_set_display
313 };
314 #endif
315
316 #if NWSDISPLAY > 0
317 static void wskbd_repeat(void *v);
318 #endif
319
320 static int wskbd_console_initted;
321 static struct wskbd_softc *wskbd_console_device;
322 static struct wskbd_internal wskbd_console_data;
323
324 static void wskbd_update_layout(struct wskbd_internal *, kbd_t);
325
326 static void
327 wskbd_update_layout(struct wskbd_internal *id, kbd_t enc)
328 {
329
330 if (enc & KB_METAESC)
331 id->t_flags |= WSKFL_METAESC;
332 else
333 id->t_flags &= ~WSKFL_METAESC;
334 }
335
336 /*
337 * Print function (for parent devices).
338 */
339 int
340 wskbddevprint(void *aux, const char *pnp)
341 {
342 #if 0
343 struct wskbddev_attach_args *ap = aux;
344 #endif
345
346 if (pnp)
347 aprint_normal("wskbd at %s", pnp);
348 #if 0
349 aprint_normal(" console %d", ap->console);
350 #endif
351
352 return (UNCONF);
353 }
354
355 int
356 wskbd_match(struct device *parent, struct cfdata *match, void *aux)
357 {
358 struct wskbddev_attach_args *ap = aux;
359
360 if (match->wskbddevcf_console != WSKBDDEVCF_CONSOLE_UNK) {
361 /*
362 * If console-ness of device specified, either match
363 * exactly (at high priority), or fail.
364 */
365 if (match->wskbddevcf_console != 0 && ap->console != 0)
366 return (10);
367 else
368 return (0);
369 }
370
371 /* If console-ness unspecified, it wins. */
372 return (1);
373 }
374
375 void
376 wskbd_attach(struct device *parent, struct device *self, void *aux)
377 {
378 struct wskbd_softc *sc = (struct wskbd_softc *)self;
379 struct wskbddev_attach_args *ap = aux;
380 #if NWSMUX > 0
381 int mux, error;
382 #endif
383
384 sc->sc_isconsole = ap->console;
385
386 #if NWSMUX > 0 || NWSDISPLAY > 0
387 sc->sc_base.me_ops = &wskbd_srcops;
388 #endif
389 #if NWSMUX > 0
390 mux = sc->sc_base.me_dv.dv_cfdata->wskbddevcf_mux;
391 if (ap->console) {
392 /* Ignore mux for console; it always goes to the console mux. */
393 /* printf(" (mux %d ignored for console)", mux); */
394 mux = -1;
395 }
396 if (mux >= 0)
397 printf(" mux %d", mux);
398 #else
399 if (sc->sc_base.me_dv.dv_cfdata->wskbddevcf_mux >= 0)
400 printf(" (mux ignored)");
401 #endif
402
403 if (ap->console) {
404 sc->id = &wskbd_console_data;
405 } else {
406 sc->id = malloc(sizeof(struct wskbd_internal),
407 M_DEVBUF, M_WAITOK|M_ZERO);
408 sc->id->t_keymap = ap->keymap;
409 wskbd_update_layout(sc->id, ap->keymap->layout);
410 }
411
412 callout_init(&sc->sc_repeat_ch);
413
414 sc->id->t_sc = sc;
415
416 sc->sc_accessops = ap->accessops;
417 sc->sc_accesscookie = ap->accesscookie;
418 sc->sc_repeating = 0;
419 sc->sc_translating = 1;
420 sc->sc_ledstate = -1; /* force update */
421
422 if (wskbd_load_keymap(sc->id->t_keymap,
423 &sc->sc_map, &sc->sc_maplen) != 0)
424 panic("cannot load keymap");
425
426 sc->sc_layout = sc->id->t_keymap->layout;
427
428 /* set default bell and key repeat data */
429 sc->sc_bell_data = wskbd_default_bell_data;
430 sc->sc_keyrepeat_data = wskbd_default_keyrepeat_data;
431
432 #ifdef WSDISPLAY_SCROLLSUPPORT
433 sc->sc_scroll_data = wskbd_default_scroll_data;
434 #endif
435
436 if (ap->console) {
437 KASSERT(wskbd_console_initted);
438 KASSERT(wskbd_console_device == NULL);
439
440 wskbd_console_device = sc;
441
442 printf(": console keyboard");
443
444 #if NWSDISPLAY > 0
445 wsdisplay_set_console_kbd(&sc->sc_base); /* sets me_dispv */
446 if (sc->sc_base.me_dispdv != NULL)
447 printf(", using %s", sc->sc_base.me_dispdv->dv_xname);
448 #endif
449 }
450 printf("\n");
451
452 #if NWSMUX > 0
453 if (mux >= 0) {
454 error = wsmux_attach_sc(wsmux_getmux(mux), &sc->sc_base);
455 if (error)
456 printf("%s: attach error=%d\n",
457 sc->sc_base.me_dv.dv_xname, error);
458 }
459 #endif
460 }
461
462 void
463 wskbd_cnattach(const struct wskbd_consops *consops, void *conscookie,
464 const struct wskbd_mapdata *mapdata)
465 {
466 KASSERT(!wskbd_console_initted);
467
468 wskbd_console_data.t_keymap = mapdata;
469 wskbd_update_layout(&wskbd_console_data, mapdata->layout);
470
471 wskbd_console_data.t_consops = consops;
472 wskbd_console_data.t_consaccesscookie = conscookie;
473
474 #if NWSDISPLAY > 0
475 wsdisplay_set_cons_kbd(wskbd_cngetc, wskbd_cnpollc, wskbd_cnbell);
476 #endif
477
478 wskbd_console_initted = 1;
479 }
480
481 void
482 wskbd_cndetach(void)
483 {
484 KASSERT(wskbd_console_initted);
485
486 wskbd_console_data.t_keymap = 0;
487
488 wskbd_console_data.t_consops = 0;
489 wskbd_console_data.t_consaccesscookie = 0;
490
491 #if NWSDISPLAY > 0
492 wsdisplay_unset_cons_kbd();
493 #endif
494
495 wskbd_console_initted = 0;
496 }
497
498 #if NWSDISPLAY > 0
499 static void
500 wskbd_repeat(void *v)
501 {
502 struct wskbd_softc *sc = (struct wskbd_softc *)v;
503 int s = spltty();
504
505 if (!sc->sc_repeating) {
506 /*
507 * race condition: a "key up" event came in when wskbd_repeat()
508 * was already called but not yet spltty()'d
509 */
510 splx(s);
511 return;
512 }
513 if (sc->sc_base.me_dispdv != NULL) {
514 int i;
515 for (i = 0; i < sc->sc_repeating; i++)
516 wsdisplay_kbdinput(sc->sc_base.me_dispdv,
517 sc->id->t_symbols[i]);
518 }
519 callout_reset(&sc->sc_repeat_ch,
520 (hz * sc->sc_keyrepeat_data.delN) / 1000, wskbd_repeat, sc);
521 splx(s);
522 }
523 #endif
524
525 int
526 wskbd_activate(struct device *self, enum devact act)
527 {
528 struct wskbd_softc *sc = (struct wskbd_softc *)self;
529
530 if (act == DVACT_DEACTIVATE)
531 sc->sc_dying = 1;
532 return (0);
533 }
534
535 /*
536 * Detach a keyboard. To keep track of users of the softc we keep
537 * a reference count that's incremented while inside, e.g., read.
538 * If the keyboard is active and the reference count is > 0 (0 is the
539 * normal state) we post an event and then wait for the process
540 * that had the reference to wake us up again. Then we blow away the
541 * vnode and return (which will deallocate the softc).
542 */
543 int
544 wskbd_detach(struct device *self, int flags)
545 {
546 struct wskbd_softc *sc = (struct wskbd_softc *)self;
547 struct wseventvar *evar;
548 int maj, mn;
549 int s;
550
551 #if NWSMUX > 0
552 /* Tell parent mux we're leaving. */
553 if (sc->sc_base.me_parent != NULL)
554 wsmux_detach_sc(&sc->sc_base);
555 #endif
556
557 if (sc->sc_isconsole) {
558 KASSERT(wskbd_console_device == sc);
559 wskbd_console_device = NULL;
560 }
561
562 evar = sc->sc_base.me_evp;
563 if (evar != NULL && evar->io != NULL) {
564 s = spltty();
565 if (--sc->sc_refcnt >= 0) {
566 /* Wake everyone by generating a dummy event. */
567 if (++evar->put >= WSEVENT_QSIZE)
568 evar->put = 0;
569 WSEVENT_WAKEUP(evar);
570 /* Wait for processes to go away. */
571 if (tsleep(sc, PZERO, "wskdet", hz * 60))
572 printf("wskbd_detach: %s didn't detach\n",
573 sc->sc_base.me_dv.dv_xname);
574 }
575 splx(s);
576 }
577
578 /* locate the major number */
579 maj = cdevsw_lookup_major(&wskbd_cdevsw);
580
581 /* Nuke the vnodes for any open instances. */
582 mn = self->dv_unit;
583 vdevgone(maj, mn, mn, VCHR);
584
585 return (0);
586 }
587
588 void
589 wskbd_input(struct device *dev, u_int type, int value)
590 {
591 struct wskbd_softc *sc = (struct wskbd_softc *)dev;
592 struct wscons_event *ev;
593 struct wseventvar *evar;
594 struct timeval thistime;
595 #if NWSDISPLAY > 0
596 int num, i;
597 #endif
598 int put;
599
600 #if NWSDISPLAY > 0
601 if (sc->sc_repeating) {
602 sc->sc_repeating = 0;
603 callout_stop(&sc->sc_repeat_ch);
604 }
605
606 /*
607 * If /dev/wskbdN is not connected in event mode translate and
608 * send upstream.
609 */
610 if (sc->sc_translating) {
611 num = wskbd_translate(sc->id, type, value);
612 if (num > 0) {
613 if (sc->sc_base.me_dispdv != NULL) {
614 #ifdef WSDISPLAY_SCROLLSUPPORT
615 if (sc->id->t_symbols [0] != KS_Print_Screen) {
616 wsdisplay_scroll(sc->sc_base.
617 me_dispdv, WSDISPLAY_SCROLL_RESET);
618 }
619 #endif
620 for (i = 0; i < num; i++)
621 wsdisplay_kbdinput(
622 sc->sc_base.me_dispdv,
623 sc->id->t_symbols[i]);
624 }
625
626 sc->sc_repeating = num;
627 callout_reset(&sc->sc_repeat_ch,
628 (hz * sc->sc_keyrepeat_data.del1) / 1000,
629 wskbd_repeat, sc);
630 }
631 return;
632 }
633 #endif
634
635 /*
636 * Keyboard is generating events. Turn this keystroke into an
637 * event and put it in the queue. If the queue is full, the
638 * keystroke is lost (sorry!).
639 */
640
641 evar = sc->sc_base.me_evp;
642 if (evar == NULL) {
643 DPRINTF(("wskbd_input: not open\n"));
644 return;
645 }
646
647 #ifdef DIAGNOSTIC
648 if (evar->q == NULL) {
649 printf("wskbd_input: evar->q=NULL\n");
650 return;
651 }
652 #endif
653
654 put = evar->put;
655 ev = &evar->q[put];
656 put = (put + 1) % WSEVENT_QSIZE;
657 if (put == evar->get) {
658 log(LOG_WARNING, "%s: event queue overflow\n",
659 sc->sc_base.me_dv.dv_xname);
660 return;
661 }
662 ev->type = type;
663 ev->value = value;
664 microtime(&thistime);
665 TIMEVAL_TO_TIMESPEC(&thistime, &ev->time);
666 evar->put = put;
667 WSEVENT_WAKEUP(evar);
668 }
669
670 #ifdef WSDISPLAY_COMPAT_RAWKBD
671 void
672 wskbd_rawinput(struct device *dev, u_char *buf, int len)
673 {
674 #if NWSDISPLAY > 0
675 struct wskbd_softc *sc = (struct wskbd_softc *)dev;
676 int i;
677
678 if (sc->sc_base.me_dispdv != NULL)
679 for (i = 0; i < len; i++)
680 wsdisplay_kbdinput(sc->sc_base.me_dispdv, buf[i]);
681 /* this is KS_GROUP_Ascii */
682 #endif
683 }
684 #endif /* WSDISPLAY_COMPAT_RAWKBD */
685
686 #if NWSDISPLAY > 0
687 static void
688 wskbd_holdscreen(struct wskbd_softc *sc, int hold)
689 {
690 int new_state;
691
692 if (sc->sc_base.me_dispdv != NULL) {
693 wsdisplay_kbdholdscreen(sc->sc_base.me_dispdv, hold);
694 new_state = sc->sc_ledstate;
695 if (hold)
696 new_state |= WSKBD_LED_SCROLL;
697 else
698 new_state &= ~WSKBD_LED_SCROLL;
699 if (new_state != sc->sc_ledstate) {
700 (*sc->sc_accessops->set_leds)(sc->sc_accesscookie,
701 new_state);
702 sc->sc_ledstate = new_state;
703 }
704 }
705 }
706 #endif
707
708 static int
709 wskbd_enable(struct wskbd_softc *sc, int on)
710 {
711 int error;
712
713 #if 0
714 /* I don't understand the purpose of this code. And it seems to
715 * break things, so it's out. -- Lennart
716 */
717 if (!on && (!sc->sc_translating
718 #if NWSDISPLAY > 0
719 || sc->sc_base.me_dispdv
720 #endif
721 ))
722 return (EBUSY);
723 #endif
724 #if NWSDISPLAY > 0
725 if (sc->sc_base.me_dispdv != NULL)
726 return (0);
727 #endif
728
729 error = (*sc->sc_accessops->enable)(sc->sc_accesscookie, on);
730 DPRINTF(("wskbd_enable: sc=%p on=%d res=%d\n", sc, on, error));
731 return (error);
732 }
733
734 #if NWSMUX > 0
735 int
736 wskbd_mux_open(struct wsevsrc *me, struct wseventvar *evp)
737 {
738 struct wskbd_softc *sc = (struct wskbd_softc *)me;
739
740 if (sc->sc_dying)
741 return (EIO);
742
743 if (sc->sc_base.me_evp != NULL)
744 return (EBUSY);
745
746 return (wskbd_do_open(sc, evp));
747 }
748 #endif
749
750 int
751 wskbdopen(dev_t dev, int flags, int mode, struct lwp *l)
752 {
753 struct wskbd_softc *sc;
754 struct wseventvar *evar;
755 int unit, error;
756
757 unit = minor(dev);
758 if (unit >= wskbd_cd.cd_ndevs || /* make sure it was attached */
759 (sc = wskbd_cd.cd_devs[unit]) == NULL)
760 return (ENXIO);
761
762 #if NWSMUX > 0
763 DPRINTF(("wskbdopen: %s mux=%p p=%p\n", sc->sc_base.me_dv.dv_xname,
764 sc->sc_base.me_parent, p));
765 #endif
766
767 if (sc->sc_dying)
768 return (EIO);
769
770 if ((flags & (FREAD | FWRITE)) == FWRITE)
771 /* Not opening for read, only ioctl is available. */
772 return (0);
773
774 #if NWSMUX > 0
775 if (sc->sc_base.me_parent != NULL) {
776 /* Grab the keyboard out of the greedy hands of the mux. */
777 DPRINTF(("wskbdopen: detach\n"));
778 wsmux_detach_sc(&sc->sc_base);
779 }
780 #endif
781
782 if (sc->sc_base.me_evp != NULL)
783 return (EBUSY);
784
785 evar = &sc->sc_base.me_evar;
786 wsevent_init(evar);
787 evar->io = l->l_proc;
788
789 error = wskbd_do_open(sc, evar);
790 if (error) {
791 DPRINTF(("wskbdopen: %s open failed\n",
792 sc->sc_base.me_dv.dv_xname));
793 sc->sc_base.me_evp = NULL;
794 wsevent_fini(evar);
795 }
796 return (error);
797 }
798
799 int
800 wskbd_do_open(struct wskbd_softc *sc, struct wseventvar *evp)
801 {
802 sc->sc_base.me_evp = evp;
803 sc->sc_translating = 0;
804
805 return (wskbd_enable(sc, 1));
806 }
807
808 int
809 wskbdclose(dev_t dev, int flags, int mode, struct lwp *l)
810 {
811 struct wskbd_softc *sc =
812 (struct wskbd_softc *)wskbd_cd.cd_devs[minor(dev)];
813 struct wseventvar *evar = sc->sc_base.me_evp;
814
815 if (evar == NULL)
816 /* not open for read */
817 return (0);
818
819 sc->sc_base.me_evp = NULL;
820 sc->sc_translating = 1;
821 (void)wskbd_enable(sc, 0);
822 wsevent_fini(evar);
823
824 return (0);
825 }
826
827 #if NWSMUX > 0
828 int
829 wskbd_mux_close(struct wsevsrc *me)
830 {
831 struct wskbd_softc *sc = (struct wskbd_softc *)me;
832
833 sc->sc_base.me_evp = NULL;
834 sc->sc_translating = 1;
835 (void)wskbd_enable(sc, 0);
836
837 return (0);
838 }
839 #endif
840
841 int
842 wskbdread(dev_t dev, struct uio *uio, int flags)
843 {
844 struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
845 int error;
846
847 if (sc->sc_dying)
848 return (EIO);
849
850 #ifdef DIAGNOSTIC
851 if (sc->sc_base.me_evp == NULL) {
852 printf("wskbdread: evp == NULL\n");
853 return (EINVAL);
854 }
855 #endif
856
857 sc->sc_refcnt++;
858 error = wsevent_read(sc->sc_base.me_evp, uio, flags);
859 if (--sc->sc_refcnt < 0) {
860 wakeup(sc);
861 error = EIO;
862 }
863 return (error);
864 }
865
866 int
867 wskbdioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
868 {
869 return (wskbd_do_ioctl(wskbd_cd.cd_devs[minor(dev)], cmd, data, flag,l));
870 }
871
872 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
873 int
874 wskbd_do_ioctl(struct device *dv, u_long cmd, caddr_t data, int flag,
875 struct lwp *l)
876 {
877 struct wskbd_softc *sc = (struct wskbd_softc *)dv;
878 int error;
879
880 sc->sc_refcnt++;
881 error = wskbd_do_ioctl_sc(sc, cmd, data, flag, l);
882 if (--sc->sc_refcnt < 0)
883 wakeup(sc);
884 return (error);
885 }
886
887 int
888 wskbd_do_ioctl_sc(struct wskbd_softc *sc, u_long cmd, caddr_t data, int flag,
889 struct lwp *l)
890 {
891
892 /*
893 * Try the generic ioctls that the wskbd interface supports.
894 */
895 switch (cmd) {
896 case FIONBIO: /* we will remove this someday (soon???) */
897 return (0);
898
899 case FIOASYNC:
900 if (sc->sc_base.me_evp == NULL)
901 return (EINVAL);
902 sc->sc_base.me_evp->async = *(int *)data != 0;
903 return (0);
904
905 case FIOSETOWN:
906 if (sc->sc_base.me_evp == NULL)
907 return (EINVAL);
908 if (-*(int *)data != sc->sc_base.me_evp->io->p_pgid
909 && *(int *)data != sc->sc_base.me_evp->io->p_pid)
910 return (EPERM);
911 return (0);
912
913 case TIOCSPGRP:
914 if (sc->sc_base.me_evp == NULL)
915 return (EINVAL);
916 if (*(int *)data != sc->sc_base.me_evp->io->p_pgid)
917 return (EPERM);
918 return (0);
919 }
920
921 /*
922 * Try the keyboard driver for WSKBDIO ioctls. It returns EPASSTHROUGH
923 * if it didn't recognize the request.
924 */
925 return (wskbd_displayioctl(&sc->sc_base.me_dv, cmd, data, flag, l));
926 }
927
928 /*
929 * WSKBDIO ioctls, handled in both emulation mode and in ``raw'' mode.
930 * Some of these have no real effect in raw mode, however.
931 */
932 static int
933 wskbd_displayioctl(struct device *dev, u_long cmd, caddr_t data, int flag,
934 struct lwp *l)
935 {
936 #ifdef WSDISPLAY_SCROLLSUPPORT
937 struct wskbd_scroll_data *usdp, *ksdp;
938 #endif
939 struct wskbd_softc *sc = (struct wskbd_softc *)dev;
940 struct wskbd_bell_data *ubdp, *kbdp;
941 struct wskbd_keyrepeat_data *ukdp, *kkdp;
942 struct wskbd_map_data *umdp;
943 struct wskbd_mapdata md;
944 struct proc *p = l ? l->l_proc : NULL;
945 kbd_t enc;
946 void *buf;
947 int len, error;
948
949 switch (cmd) {
950 #define SETBELL(dstp, srcp, dfltp) \
951 do { \
952 (dstp)->pitch = ((srcp)->which & WSKBD_BELL_DOPITCH) ? \
953 (srcp)->pitch : (dfltp)->pitch; \
954 (dstp)->period = ((srcp)->which & WSKBD_BELL_DOPERIOD) ? \
955 (srcp)->period : (dfltp)->period; \
956 (dstp)->volume = ((srcp)->which & WSKBD_BELL_DOVOLUME) ? \
957 (srcp)->volume : (dfltp)->volume; \
958 (dstp)->which = WSKBD_BELL_DOALL; \
959 } while (0)
960
961 case WSKBDIO_BELL:
962 if ((flag & FWRITE) == 0)
963 return (EACCES);
964 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
965 WSKBDIO_COMPLEXBELL, (caddr_t)&sc->sc_bell_data, flag, l));
966
967 case WSKBDIO_COMPLEXBELL:
968 if ((flag & FWRITE) == 0)
969 return (EACCES);
970 ubdp = (struct wskbd_bell_data *)data;
971 SETBELL(ubdp, ubdp, &sc->sc_bell_data);
972 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
973 WSKBDIO_COMPLEXBELL, (caddr_t)ubdp, flag, l));
974
975 case WSKBDIO_SETBELL:
976 if ((flag & FWRITE) == 0)
977 return (EACCES);
978 kbdp = &sc->sc_bell_data;
979 setbell:
980 ubdp = (struct wskbd_bell_data *)data;
981 SETBELL(kbdp, ubdp, kbdp);
982 return (0);
983
984 case WSKBDIO_GETBELL:
985 kbdp = &sc->sc_bell_data;
986 getbell:
987 ubdp = (struct wskbd_bell_data *)data;
988 SETBELL(ubdp, kbdp, kbdp);
989 return (0);
990
991 case WSKBDIO_SETDEFAULTBELL:
992 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
993 return (error);
994 kbdp = &wskbd_default_bell_data;
995 goto setbell;
996
997
998 case WSKBDIO_GETDEFAULTBELL:
999 kbdp = &wskbd_default_bell_data;
1000 goto getbell;
1001
1002 #undef SETBELL
1003
1004 #define SETKEYREPEAT(dstp, srcp, dfltp) \
1005 do { \
1006 (dstp)->del1 = ((srcp)->which & WSKBD_KEYREPEAT_DODEL1) ? \
1007 (srcp)->del1 : (dfltp)->del1; \
1008 (dstp)->delN = ((srcp)->which & WSKBD_KEYREPEAT_DODELN) ? \
1009 (srcp)->delN : (dfltp)->delN; \
1010 (dstp)->which = WSKBD_KEYREPEAT_DOALL; \
1011 } while (0)
1012
1013 case WSKBDIO_SETKEYREPEAT:
1014 if ((flag & FWRITE) == 0)
1015 return (EACCES);
1016 kkdp = &sc->sc_keyrepeat_data;
1017 setkeyrepeat:
1018 ukdp = (struct wskbd_keyrepeat_data *)data;
1019 SETKEYREPEAT(kkdp, ukdp, kkdp);
1020 return (0);
1021
1022 case WSKBDIO_GETKEYREPEAT:
1023 kkdp = &sc->sc_keyrepeat_data;
1024 getkeyrepeat:
1025 ukdp = (struct wskbd_keyrepeat_data *)data;
1026 SETKEYREPEAT(ukdp, kkdp, kkdp);
1027 return (0);
1028
1029 case WSKBDIO_SETDEFAULTKEYREPEAT:
1030 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1031 return (error);
1032 kkdp = &wskbd_default_keyrepeat_data;
1033 goto setkeyrepeat;
1034
1035
1036 case WSKBDIO_GETDEFAULTKEYREPEAT:
1037 kkdp = &wskbd_default_keyrepeat_data;
1038 goto getkeyrepeat;
1039
1040 #ifdef WSDISPLAY_SCROLLSUPPORT
1041 #define SETSCROLLMOD(dstp, srcp, dfltp) \
1042 do { \
1043 (dstp)->mode = ((srcp)->which & WSKBD_SCROLL_DOMODE) ? \
1044 (srcp)->mode : (dfltp)->mode; \
1045 (dstp)->modifier = ((srcp)->which & WSKBD_SCROLL_DOMODIFIER) ? \
1046 (srcp)->modifier : (dfltp)->modifier; \
1047 (dstp)->which = WSKBD_SCROLL_DOALL; \
1048 } while (0)
1049
1050 case WSKBDIO_SETSCROLL:
1051 usdp = (struct wskbd_scroll_data *)data;
1052 ksdp = &sc->sc_scroll_data;
1053 SETSCROLLMOD(ksdp, usdp, ksdp);
1054 return (0);
1055
1056 case WSKBDIO_GETSCROLL:
1057 usdp = (struct wskbd_scroll_data *)data;
1058 ksdp = &sc->sc_scroll_data;
1059 SETSCROLLMOD(usdp, ksdp, ksdp);
1060 return (0);
1061 #else
1062 case WSKBDIO_GETSCROLL:
1063 case WSKBDIO_SETSCROLL:
1064 return ENODEV;
1065 #endif
1066
1067 #undef SETKEYREPEAT
1068
1069 case WSKBDIO_SETMAP:
1070 if ((flag & FWRITE) == 0)
1071 return (EACCES);
1072 umdp = (struct wskbd_map_data *)data;
1073 if (umdp->maplen > WSKBDIO_MAXMAPLEN)
1074 return (EINVAL);
1075
1076 len = umdp->maplen*sizeof(struct wscons_keymap);
1077 buf = malloc(len, M_TEMP, M_WAITOK);
1078 error = copyin(umdp->map, buf, len);
1079 if (error == 0) {
1080 wskbd_init_keymap(umdp->maplen,
1081 &sc->sc_map, &sc->sc_maplen);
1082 memcpy(sc->sc_map, buf, len);
1083 /* drop the variant bits handled by the map */
1084 sc->sc_layout = KB_USER |
1085 (KB_VARIANT(sc->sc_layout) & KB_HANDLEDBYWSKBD);
1086 wskbd_update_layout(sc->id, sc->sc_layout);
1087 }
1088 free(buf, M_TEMP);
1089 return(error);
1090
1091 case WSKBDIO_GETMAP:
1092 umdp = (struct wskbd_map_data *)data;
1093 if (umdp->maplen > sc->sc_maplen)
1094 umdp->maplen = sc->sc_maplen;
1095 error = copyout(sc->sc_map, umdp->map,
1096 umdp->maplen*sizeof(struct wscons_keymap));
1097 return(error);
1098
1099 case WSKBDIO_GETENCODING:
1100 *((kbd_t *) data) = sc->sc_layout;
1101 return(0);
1102
1103 case WSKBDIO_SETENCODING:
1104 if ((flag & FWRITE) == 0)
1105 return (EACCES);
1106 enc = *((kbd_t *)data);
1107 if (KB_ENCODING(enc) == KB_USER) {
1108 /* user map must already be loaded */
1109 if (KB_ENCODING(sc->sc_layout) != KB_USER)
1110 return (EINVAL);
1111 /* map variants make no sense */
1112 if (KB_VARIANT(enc) & ~KB_HANDLEDBYWSKBD)
1113 return (EINVAL);
1114 } else {
1115 md = *(sc->id->t_keymap); /* structure assignment */
1116 md.layout = enc;
1117 error = wskbd_load_keymap(&md, &sc->sc_map,
1118 &sc->sc_maplen);
1119 if (error)
1120 return (error);
1121 }
1122 sc->sc_layout = enc;
1123 wskbd_update_layout(sc->id, enc);
1124 return (0);
1125 }
1126
1127 /*
1128 * Try the keyboard driver for WSKBDIO ioctls. It returns -1
1129 * if it didn't recognize the request, and in turn we return
1130 * -1 if we didn't recognize the request.
1131 */
1132 /* printf("kbdaccess\n"); */
1133 error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
1134 flag, l);
1135 #ifdef WSDISPLAY_COMPAT_RAWKBD
1136 if (!error && cmd == WSKBDIO_SETMODE && *(int *)data == WSKBD_RAW) {
1137 int s = spltty();
1138 sc->id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
1139 | MOD_CONTROL_L | MOD_CONTROL_R
1140 | MOD_META_L | MOD_META_R
1141 | MOD_COMMAND
1142 | MOD_COMMAND1 | MOD_COMMAND2);
1143 #if NWSDISPLAY > 0
1144 if (sc->sc_repeating) {
1145 sc->sc_repeating = 0;
1146 callout_stop(&sc->sc_repeat_ch);
1147 }
1148 #endif
1149 splx(s);
1150 }
1151 #endif
1152 return (error);
1153 }
1154
1155 int
1156 wskbdpoll(dev_t dev, int events, struct lwp *l)
1157 {
1158 struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
1159
1160 if (sc->sc_base.me_evp == NULL)
1161 return (EINVAL);
1162 return (wsevent_poll(sc->sc_base.me_evp, events, l));
1163 }
1164
1165 int
1166 wskbdkqfilter(dev_t dev, struct knote *kn)
1167 {
1168 struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
1169
1170 if (sc->sc_base.me_evp == NULL)
1171 return (1);
1172 return (wsevent_kqfilter(sc->sc_base.me_evp, kn));
1173 }
1174
1175 #if NWSDISPLAY > 0
1176
1177 int
1178 wskbd_pickfree(void)
1179 {
1180 int i;
1181 struct wskbd_softc *sc;
1182
1183 for (i = 0; i < wskbd_cd.cd_ndevs; i++) {
1184 if ((sc = wskbd_cd.cd_devs[i]) == NULL)
1185 continue;
1186 if (sc->sc_base.me_dispdv == NULL)
1187 return (i);
1188 }
1189 return (-1);
1190 }
1191
1192 struct wsevsrc *
1193 wskbd_set_console_display(struct device *displaydv, struct wsevsrc *me)
1194 {
1195 struct wskbd_softc *sc = wskbd_console_device;
1196
1197 if (sc == NULL)
1198 return (NULL);
1199 sc->sc_base.me_dispdv = displaydv;
1200 #if NWSMUX > 0
1201 (void)wsmux_attach_sc((struct wsmux_softc *)me, &sc->sc_base);
1202 #endif
1203 return (&sc->sc_base);
1204 }
1205
1206 int
1207 wskbd_set_display(struct device *dv, struct wsevsrc *me)
1208 {
1209 struct wskbd_softc *sc = (struct wskbd_softc *)dv;
1210 struct device *displaydv = me != NULL ? me->me_dispdv : NULL;
1211 struct device *odisplaydv;
1212 int error;
1213
1214 DPRINTF(("wskbd_set_display: %s me=%p odisp=%p disp=%p cons=%d\n",
1215 dv->dv_xname, me, sc->sc_base.me_dispdv, displaydv,
1216 sc->sc_isconsole));
1217
1218 if (sc->sc_isconsole)
1219 return (EBUSY);
1220
1221 if (displaydv != NULL) {
1222 if (sc->sc_base.me_dispdv != NULL)
1223 return (EBUSY);
1224 } else {
1225 if (sc->sc_base.me_dispdv == NULL)
1226 return (ENXIO);
1227 }
1228
1229 odisplaydv = sc->sc_base.me_dispdv;
1230 sc->sc_base.me_dispdv = NULL;
1231 error = wskbd_enable(sc, displaydv != NULL);
1232 sc->sc_base.me_dispdv = displaydv;
1233 if (error) {
1234 sc->sc_base.me_dispdv = odisplaydv;
1235 return (error);
1236 }
1237
1238 if (displaydv)
1239 printf("%s: connecting to %s\n",
1240 sc->sc_base.me_dv.dv_xname, displaydv->dv_xname);
1241 else
1242 printf("%s: disconnecting from %s\n",
1243 sc->sc_base.me_dv.dv_xname, odisplaydv->dv_xname);
1244
1245 return (0);
1246 }
1247
1248 #endif /* NWSDISPLAY > 0 */
1249
1250 #if NWSMUX > 0
1251 int
1252 wskbd_add_mux(int unit, struct wsmux_softc *muxsc)
1253 {
1254 struct wskbd_softc *sc;
1255
1256 if (unit < 0 || unit >= wskbd_cd.cd_ndevs ||
1257 (sc = wskbd_cd.cd_devs[unit]) == NULL)
1258 return (ENXIO);
1259
1260 if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
1261 return (EBUSY);
1262
1263 return (wsmux_attach_sc(muxsc, &sc->sc_base));
1264 }
1265 #endif
1266
1267 /*
1268 * Console interface.
1269 */
1270 int
1271 wskbd_cngetc(dev_t dev)
1272 {
1273 static int num = 0;
1274 static int pos;
1275 u_int type;
1276 int data;
1277 keysym_t ks;
1278
1279 if (!wskbd_console_initted)
1280 return 0;
1281
1282 if (wskbd_console_device != NULL &&
1283 !wskbd_console_device->sc_translating)
1284 return 0;
1285
1286 for(;;) {
1287 if (num-- > 0) {
1288 ks = wskbd_console_data.t_symbols[pos++];
1289 if (KS_GROUP(ks) == KS_GROUP_Ascii)
1290 return (KS_VALUE(ks));
1291 } else {
1292 (*wskbd_console_data.t_consops->getc)
1293 (wskbd_console_data.t_consaccesscookie,
1294 &type, &data);
1295 num = wskbd_translate(&wskbd_console_data, type, data);
1296 pos = 0;
1297 }
1298 }
1299 }
1300
1301 void
1302 wskbd_cnpollc(dev_t dev, int poll)
1303 {
1304
1305 if (!wskbd_console_initted)
1306 return;
1307
1308 if (wskbd_console_device != NULL &&
1309 !wskbd_console_device->sc_translating)
1310 return;
1311
1312 (*wskbd_console_data.t_consops->pollc)
1313 (wskbd_console_data.t_consaccesscookie, poll);
1314 }
1315
1316 void
1317 wskbd_cnbell(dev_t dev, u_int pitch, u_int period, u_int volume)
1318 {
1319
1320 if (!wskbd_console_initted)
1321 return;
1322
1323 if (wskbd_console_data.t_consops->bell != NULL)
1324 (*wskbd_console_data.t_consops->bell)
1325 (wskbd_console_data.t_consaccesscookie, pitch, period,
1326 volume);
1327 }
1328
1329 static inline void
1330 update_leds(struct wskbd_internal *id)
1331 {
1332 int new_state;
1333
1334 new_state = 0;
1335 if (id->t_modifiers & (MOD_SHIFTLOCK | MOD_CAPSLOCK))
1336 new_state |= WSKBD_LED_CAPS;
1337 if (id->t_modifiers & MOD_NUMLOCK)
1338 new_state |= WSKBD_LED_NUM;
1339 if (id->t_modifiers & MOD_COMPOSE)
1340 new_state |= WSKBD_LED_COMPOSE;
1341 if (id->t_modifiers & MOD_HOLDSCREEN)
1342 new_state |= WSKBD_LED_SCROLL;
1343
1344 if (id->t_sc && new_state != id->t_sc->sc_ledstate) {
1345 (*id->t_sc->sc_accessops->set_leds)
1346 (id->t_sc->sc_accesscookie, new_state);
1347 id->t_sc->sc_ledstate = new_state;
1348 }
1349 }
1350
1351 static inline void
1352 update_modifier(struct wskbd_internal *id, u_int type, int toggle, int mask)
1353 {
1354 if (toggle) {
1355 if (type == WSCONS_EVENT_KEY_DOWN)
1356 id->t_modifiers ^= mask;
1357 } else {
1358 if (type == WSCONS_EVENT_KEY_DOWN)
1359 id->t_modifiers |= mask;
1360 else
1361 id->t_modifiers &= ~mask;
1362 }
1363 }
1364
1365 #if NWSDISPLAY > 0
1366 static void
1367 change_displayparam(struct wskbd_softc *sc, int param, int updown,
1368 int wraparound)
1369 {
1370 int res;
1371 struct wsdisplay_param dp;
1372
1373 dp.param = param;
1374 res = wsdisplay_param(sc->sc_base.me_dispdv, WSDISPLAYIO_GETPARAM, &dp);
1375
1376 if (res == EINVAL)
1377 return; /* no such parameter */
1378
1379 dp.curval += updown;
1380 if (dp.max < dp.curval)
1381 dp.curval = wraparound ? dp.min : dp.max;
1382 else
1383 if (dp.curval < dp.min)
1384 dp.curval = wraparound ? dp.max : dp.min;
1385 wsdisplay_param(sc->sc_base.me_dispdv, WSDISPLAYIO_SETPARAM, &dp);
1386 }
1387 #endif
1388
1389 static int
1390 internal_command(struct wskbd_softc *sc, u_int *type, keysym_t ksym,
1391 keysym_t ksym2)
1392 {
1393 #ifdef WSDISPLAY_SCROLLSUPPORT
1394 u_int state = 0;
1395 #endif
1396 switch (ksym) {
1397 #ifdef WSDISPLAY_SCROLLSUPPORT
1398 case KS_Cmd_ScrollFastUp:
1399 case KS_Cmd_ScrollFastDown:
1400 if (*type == WSCONS_EVENT_KEY_DOWN) {
1401 GETMODSTATE(sc->id->t_modifiers, state);
1402 if ((sc->sc_scroll_data.mode == WSKBD_SCROLL_MODE_HOLD
1403 && MOD_ONESET(sc->id, MOD_HOLDSCREEN))
1404 || (sc->sc_scroll_data.mode == WSKBD_SCROLL_MODE_NORMAL
1405 && sc->sc_scroll_data.modifier == state)) {
1406 update_modifier(sc->id, *type, 0, MOD_COMMAND);
1407 wsdisplay_scroll(sc->sc_base.me_dispdv,
1408 (ksym == KS_Cmd_ScrollFastUp) ?
1409 WSDISPLAY_SCROLL_BACKWARD :
1410 WSDISPLAY_SCROLL_FORWARD);
1411 return (1);
1412 } else {
1413 return (0);
1414 }
1415 }
1416
1417 case KS_Cmd_ScrollSlowUp:
1418 case KS_Cmd_ScrollSlowDown:
1419 if (*type == WSCONS_EVENT_KEY_DOWN) {
1420 GETMODSTATE(sc->id->t_modifiers, state);
1421 if ((sc->sc_scroll_data.mode == WSKBD_SCROLL_MODE_HOLD
1422 && MOD_ONESET(sc->id, MOD_HOLDSCREEN))
1423 || (sc->sc_scroll_data.mode == WSKBD_SCROLL_MODE_NORMAL
1424 && sc->sc_scroll_data.modifier == state)) {
1425 update_modifier(sc->id, *type, 0, MOD_COMMAND);
1426 wsdisplay_scroll(sc->sc_base.me_dispdv,
1427 (ksym == KS_Cmd_ScrollSlowUp) ?
1428 WSDISPLAY_SCROLL_BACKWARD | WSDISPLAY_SCROLL_LOW:
1429 WSDISPLAY_SCROLL_FORWARD | WSDISPLAY_SCROLL_LOW);
1430 return (1);
1431 } else {
1432 return (0);
1433 }
1434 }
1435 #endif
1436
1437 case KS_Cmd:
1438 update_modifier(sc->id, *type, 0, MOD_COMMAND);
1439 ksym = ksym2;
1440 break;
1441
1442 case KS_Cmd1:
1443 update_modifier(sc->id, *type, 0, MOD_COMMAND1);
1444 break;
1445
1446 case KS_Cmd2:
1447 update_modifier(sc->id, *type, 0, MOD_COMMAND2);
1448 break;
1449 }
1450
1451 if (*type != WSCONS_EVENT_KEY_DOWN ||
1452 (! MOD_ONESET(sc->id, MOD_COMMAND) &&
1453 ! MOD_ALLSET(sc->id, MOD_COMMAND1 | MOD_COMMAND2)))
1454 return (0);
1455
1456 #if defined(DDB) || defined(KGDB)
1457 if (ksym == KS_Cmd_Debugger) {
1458 if (sc->sc_isconsole) {
1459 #ifdef DDB
1460 console_debugger();
1461 #endif
1462 #ifdef KGDB
1463 kgdb_connect(1);
1464 #endif
1465 }
1466 /* discard this key (ddb discarded command modifiers) */
1467 *type = WSCONS_EVENT_KEY_UP;
1468 return (1);
1469 }
1470 #endif
1471
1472 #if NWSDISPLAY > 0
1473 if (sc->sc_base.me_dispdv == NULL)
1474 return (0);
1475
1476 switch (ksym) {
1477 case KS_Cmd_Screen0:
1478 case KS_Cmd_Screen1:
1479 case KS_Cmd_Screen2:
1480 case KS_Cmd_Screen3:
1481 case KS_Cmd_Screen4:
1482 case KS_Cmd_Screen5:
1483 case KS_Cmd_Screen6:
1484 case KS_Cmd_Screen7:
1485 case KS_Cmd_Screen8:
1486 case KS_Cmd_Screen9:
1487 wsdisplay_switch(sc->sc_base.me_dispdv, ksym - KS_Cmd_Screen0, 0);
1488 return (1);
1489 case KS_Cmd_ResetEmul:
1490 wsdisplay_reset(sc->sc_base.me_dispdv, WSDISPLAY_RESETEMUL);
1491 return (1);
1492 case KS_Cmd_ResetClose:
1493 wsdisplay_reset(sc->sc_base.me_dispdv, WSDISPLAY_RESETCLOSE);
1494 return (1);
1495 case KS_Cmd_BacklightOn:
1496 case KS_Cmd_BacklightOff:
1497 case KS_Cmd_BacklightToggle:
1498 change_displayparam(sc, WSDISPLAYIO_PARAM_BACKLIGHT,
1499 ksym == KS_Cmd_BacklightOff ? -1 : 1,
1500 ksym == KS_Cmd_BacklightToggle ? 1 : 0);
1501 return (1);
1502 case KS_Cmd_BrightnessUp:
1503 case KS_Cmd_BrightnessDown:
1504 case KS_Cmd_BrightnessRotate:
1505 change_displayparam(sc, WSDISPLAYIO_PARAM_BRIGHTNESS,
1506 ksym == KS_Cmd_BrightnessDown ? -1 : 1,
1507 ksym == KS_Cmd_BrightnessRotate ? 1 : 0);
1508 return (1);
1509 case KS_Cmd_ContrastUp:
1510 case KS_Cmd_ContrastDown:
1511 case KS_Cmd_ContrastRotate:
1512 change_displayparam(sc, WSDISPLAYIO_PARAM_CONTRAST,
1513 ksym == KS_Cmd_ContrastDown ? -1 : 1,
1514 ksym == KS_Cmd_ContrastRotate ? 1 : 0);
1515 return (1);
1516 }
1517 #endif
1518
1519 return (0);
1520 }
1521
1522 static int
1523 wskbd_translate(struct wskbd_internal *id, u_int type, int value)
1524 {
1525 struct wskbd_softc *sc = id->t_sc;
1526 keysym_t ksym, res, *group;
1527 struct wscons_keymap kpbuf, *kp;
1528 int iscommand = 0;
1529
1530 if (type == WSCONS_EVENT_ALL_KEYS_UP) {
1531 id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
1532 | MOD_CONTROL_L | MOD_CONTROL_R
1533 | MOD_META_L | MOD_META_R
1534 | MOD_MODESHIFT
1535 | MOD_COMMAND | MOD_COMMAND1 | MOD_COMMAND2);
1536 update_leds(id);
1537 return (0);
1538 }
1539
1540 if (sc != NULL) {
1541 if (value < 0 || value >= sc->sc_maplen) {
1542 #ifdef DEBUG
1543 printf("wskbd_translate: keycode %d out of range\n",
1544 value);
1545 #endif
1546 return (0);
1547 }
1548 kp = sc->sc_map + value;
1549 } else {
1550 kp = &kpbuf;
1551 wskbd_get_mapentry(id->t_keymap, value, kp);
1552 }
1553
1554 /* if this key has a command, process it first */
1555 if (sc != NULL && kp->command != KS_voidSymbol)
1556 iscommand = internal_command(sc, &type, kp->command,
1557 kp->group1[0]);
1558
1559 /* Now update modifiers */
1560 switch (kp->group1[0]) {
1561 case KS_Shift_L:
1562 update_modifier(id, type, 0, MOD_SHIFT_L);
1563 break;
1564
1565 case KS_Shift_R:
1566 update_modifier(id, type, 0, MOD_SHIFT_R);
1567 break;
1568
1569 case KS_Shift_Lock:
1570 update_modifier(id, type, 1, MOD_SHIFTLOCK);
1571 break;
1572
1573 case KS_Caps_Lock:
1574 update_modifier(id, type, 1, MOD_CAPSLOCK);
1575 break;
1576
1577 case KS_Control_L:
1578 update_modifier(id, type, 0, MOD_CONTROL_L);
1579 break;
1580
1581 case KS_Control_R:
1582 update_modifier(id, type, 0, MOD_CONTROL_R);
1583 break;
1584
1585 case KS_Alt_L:
1586 update_modifier(id, type, 0, MOD_META_L);
1587 break;
1588
1589 case KS_Alt_R:
1590 update_modifier(id, type, 0, MOD_META_R);
1591 break;
1592
1593 case KS_Mode_switch:
1594 update_modifier(id, type, 0, MOD_MODESHIFT);
1595 break;
1596
1597 case KS_Num_Lock:
1598 update_modifier(id, type, 1, MOD_NUMLOCK);
1599 break;
1600
1601 #if NWSDISPLAY > 0
1602 case KS_Hold_Screen:
1603 if (sc != NULL) {
1604 update_modifier(id, type, 1, MOD_HOLDSCREEN);
1605 wskbd_holdscreen(sc, id->t_modifiers & MOD_HOLDSCREEN);
1606 }
1607 break;
1608 #endif
1609 }
1610
1611 /* If this is a key release or we are in command mode, we are done */
1612 if (type != WSCONS_EVENT_KEY_DOWN || iscommand) {
1613 update_leds(id);
1614 return (0);
1615 }
1616
1617 /* Get the keysym */
1618 if (id->t_modifiers & MOD_MODESHIFT)
1619 group = & kp->group2[0];
1620 else
1621 group = & kp->group1[0];
1622
1623 if ((id->t_modifiers & MOD_NUMLOCK) != 0 &&
1624 KS_GROUP(group[1]) == KS_GROUP_Keypad) {
1625 if (MOD_ONESET(id, MOD_ANYSHIFT))
1626 ksym = group[0];
1627 else
1628 ksym = group[1];
1629 } else if (! MOD_ONESET(id, MOD_ANYSHIFT | MOD_CAPSLOCK)) {
1630 ksym = group[0];
1631 } else if (MOD_ONESET(id, MOD_CAPSLOCK)) {
1632 if (! MOD_ONESET(id, MOD_SHIFT_L | MOD_SHIFT_R))
1633 ksym = group[0];
1634 else
1635 ksym = group[1];
1636 if (ksym >= KS_a && ksym <= KS_z)
1637 ksym += KS_A - KS_a;
1638 else if (ksym >= KS_agrave && ksym <= KS_thorn &&
1639 ksym != KS_division)
1640 ksym += KS_Agrave - KS_agrave;
1641 } else if (MOD_ONESET(id, MOD_ANYSHIFT)) {
1642 ksym = group[1];
1643 } else {
1644 ksym = group[0];
1645 }
1646
1647 /* Process compose sequence and dead accents */
1648 res = KS_voidSymbol;
1649
1650 switch (KS_GROUP(ksym)) {
1651 case KS_GROUP_Ascii:
1652 case KS_GROUP_Keypad:
1653 case KS_GROUP_Function:
1654 res = ksym;
1655 break;
1656
1657 case KS_GROUP_Mod:
1658 if (ksym == KS_Multi_key) {
1659 update_modifier(id, 1, 0, MOD_COMPOSE);
1660 id->t_composelen = 2;
1661 }
1662 break;
1663
1664 case KS_GROUP_Dead:
1665 if (id->t_composelen == 0) {
1666 update_modifier(id, 1, 0, MOD_COMPOSE);
1667 id->t_composelen = 1;
1668 id->t_composebuf[0] = ksym;
1669 } else
1670 res = ksym;
1671 break;
1672 }
1673
1674 if (res == KS_voidSymbol) {
1675 update_leds(id);
1676 return (0);
1677 }
1678
1679 if (id->t_composelen > 0) {
1680 id->t_composebuf[2 - id->t_composelen] = res;
1681 if (--id->t_composelen == 0) {
1682 res = wskbd_compose_value(id->t_composebuf);
1683 update_modifier(id, 0, 0, MOD_COMPOSE);
1684 } else {
1685 return (0);
1686 }
1687 }
1688
1689 update_leds(id);
1690
1691 /* We are done, return the symbol */
1692 if (KS_GROUP(res) == KS_GROUP_Ascii) {
1693 if (MOD_ONESET(id, MOD_ANYCONTROL)) {
1694 if ((res >= KS_at && res <= KS_z) || res == KS_space)
1695 res = res & 0x1f;
1696 else if (res == KS_2)
1697 res = 0x00;
1698 else if (res >= KS_3 && res <= KS_7)
1699 res = KS_Escape + (res - KS_3);
1700 else if (res == KS_8)
1701 res = KS_Delete;
1702 }
1703 if (MOD_ONESET(id, MOD_ANYMETA)) {
1704 if (id->t_flags & WSKFL_METAESC) {
1705 id->t_symbols[0] = KS_Escape;
1706 id->t_symbols[1] = res;
1707 return (2);
1708 } else
1709 res |= 0x80;
1710 }
1711 }
1712
1713 id->t_symbols[0] = res;
1714 return (1);
1715 }
1716