wskbd.c revision 1.74 1 /* $NetBSD: wskbd.c,v 1.74 2004/05/28 21:42:29 christos 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.74 2004/05/28 21:42:29 christos 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 proc *);
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 proc *);
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 proc *);
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 if (sc->id->t_symbols [0] != KS_Print_Screen) {
615 wsdisplay_scroll (sc->sc_base.
616 me_dispdv, WSDISPLAY_SCROLL_RESET);
617 }
618 for (i = 0; i < num; i++)
619 wsdisplay_kbdinput(
620 sc->sc_base.me_dispdv,
621 sc->id->t_symbols[i]);
622 }
623
624 sc->sc_repeating = num;
625 callout_reset(&sc->sc_repeat_ch,
626 (hz * sc->sc_keyrepeat_data.del1) / 1000,
627 wskbd_repeat, sc);
628 }
629 return;
630 }
631 #endif
632
633 /*
634 * Keyboard is generating events. Turn this keystroke into an
635 * event and put it in the queue. If the queue is full, the
636 * keystroke is lost (sorry!).
637 */
638
639 evar = sc->sc_base.me_evp;
640 if (evar == NULL) {
641 DPRINTF(("wskbd_input: not open\n"));
642 return;
643 }
644
645 #ifdef DIAGNOSTIC
646 if (evar->q == NULL) {
647 printf("wskbd_input: evar->q=NULL\n");
648 return;
649 }
650 #endif
651
652 put = evar->put;
653 ev = &evar->q[put];
654 put = (put + 1) % WSEVENT_QSIZE;
655 if (put == evar->get) {
656 log(LOG_WARNING, "%s: event queue overflow\n",
657 sc->sc_base.me_dv.dv_xname);
658 return;
659 }
660 ev->type = type;
661 ev->value = value;
662 microtime(&thistime);
663 TIMEVAL_TO_TIMESPEC(&thistime, &ev->time);
664 evar->put = put;
665 WSEVENT_WAKEUP(evar);
666 }
667
668 #ifdef WSDISPLAY_COMPAT_RAWKBD
669 void
670 wskbd_rawinput(struct device *dev, u_char *buf, int len)
671 {
672 #if NWSDISPLAY > 0
673 struct wskbd_softc *sc = (struct wskbd_softc *)dev;
674 int i;
675
676 if (sc->sc_base.me_dispdv != NULL)
677 for (i = 0; i < len; i++)
678 wsdisplay_kbdinput(sc->sc_base.me_dispdv, buf[i]);
679 /* this is KS_GROUP_Ascii */
680 #endif
681 }
682 #endif /* WSDISPLAY_COMPAT_RAWKBD */
683
684 #if NWSDISPLAY > 0
685 static void
686 wskbd_holdscreen(struct wskbd_softc *sc, int hold)
687 {
688 int new_state;
689
690 if (sc->sc_base.me_dispdv != NULL) {
691 wsdisplay_kbdholdscreen(sc->sc_base.me_dispdv, hold);
692 new_state = sc->sc_ledstate;
693 if (hold)
694 new_state |= WSKBD_LED_SCROLL;
695 else
696 new_state &= ~WSKBD_LED_SCROLL;
697 if (new_state != sc->sc_ledstate) {
698 (*sc->sc_accessops->set_leds)(sc->sc_accesscookie,
699 new_state);
700 sc->sc_ledstate = new_state;
701 }
702 }
703 }
704 #endif
705
706 static int
707 wskbd_enable(struct wskbd_softc *sc, int on)
708 {
709 int error;
710
711 #if 0
712 /* I don't understand the purpose of this code. And it seems to
713 * break things, so it's out. -- Lennart
714 */
715 if (!on && (!sc->sc_translating
716 #if NWSDISPLAY > 0
717 || sc->sc_base.me_dispdv
718 #endif
719 ))
720 return (EBUSY);
721 #endif
722 #if NWSDISPLAY > 0
723 if (sc->sc_base.me_dispdv != NULL)
724 return (0);
725 #endif
726
727 error = (*sc->sc_accessops->enable)(sc->sc_accesscookie, on);
728 DPRINTF(("wskbd_enable: sc=%p on=%d res=%d\n", sc, on, error));
729 return (error);
730 }
731
732 #if NWSMUX > 0
733 int
734 wskbd_mux_open(struct wsevsrc *me, struct wseventvar *evp)
735 {
736 struct wskbd_softc *sc = (struct wskbd_softc *)me;
737
738 if (sc->sc_dying)
739 return (EIO);
740
741 if (sc->sc_base.me_evp != NULL)
742 return (EBUSY);
743
744 return (wskbd_do_open(sc, evp));
745 }
746 #endif
747
748 int
749 wskbdopen(dev_t dev, int flags, int mode, struct proc *p)
750 {
751 struct wskbd_softc *sc;
752 struct wseventvar *evar;
753 int unit, error;
754
755 unit = minor(dev);
756 if (unit >= wskbd_cd.cd_ndevs || /* make sure it was attached */
757 (sc = wskbd_cd.cd_devs[unit]) == NULL)
758 return (ENXIO);
759
760 #if NWSMUX > 0
761 DPRINTF(("wskbdopen: %s mux=%p p=%p\n", sc->sc_base.me_dv.dv_xname,
762 sc->sc_base.me_parent, p));
763 #endif
764
765 if (sc->sc_dying)
766 return (EIO);
767
768 if ((flags & (FREAD | FWRITE)) == FWRITE)
769 /* Not opening for read, only ioctl is available. */
770 return (0);
771
772 #if NWSMUX > 0
773 if (sc->sc_base.me_parent != NULL) {
774 /* Grab the keyboard out of the greedy hands of the mux. */
775 DPRINTF(("wskbdopen: detach\n"));
776 wsmux_detach_sc(&sc->sc_base);
777 }
778 #endif
779
780 if (sc->sc_base.me_evp != NULL)
781 return (EBUSY);
782
783 evar = &sc->sc_base.me_evar;
784 wsevent_init(evar);
785 evar->io = p;
786
787 error = wskbd_do_open(sc, evar);
788 if (error) {
789 DPRINTF(("wskbdopen: %s open failed\n",
790 sc->sc_base.me_dv.dv_xname));
791 sc->sc_base.me_evp = NULL;
792 wsevent_fini(evar);
793 }
794 return (error);
795 }
796
797 int
798 wskbd_do_open(struct wskbd_softc *sc, struct wseventvar *evp)
799 {
800 sc->sc_base.me_evp = evp;
801 sc->sc_translating = 0;
802
803 return (wskbd_enable(sc, 1));
804 }
805
806 int
807 wskbdclose(dev_t dev, int flags, int mode, struct proc *p)
808 {
809 struct wskbd_softc *sc =
810 (struct wskbd_softc *)wskbd_cd.cd_devs[minor(dev)];
811 struct wseventvar *evar = sc->sc_base.me_evp;
812
813 if (evar == NULL)
814 /* not open for read */
815 return (0);
816
817 sc->sc_base.me_evp = NULL;
818 sc->sc_translating = 1;
819 (void)wskbd_enable(sc, 0);
820 wsevent_fini(evar);
821
822 return (0);
823 }
824
825 #if NWSMUX > 0
826 int
827 wskbd_mux_close(struct wsevsrc *me)
828 {
829 struct wskbd_softc *sc = (struct wskbd_softc *)me;
830
831 sc->sc_base.me_evp = NULL;
832 sc->sc_translating = 1;
833 (void)wskbd_enable(sc, 0);
834
835 return (0);
836 }
837 #endif
838
839 int
840 wskbdread(dev_t dev, struct uio *uio, int flags)
841 {
842 struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
843 int error;
844
845 if (sc->sc_dying)
846 return (EIO);
847
848 #ifdef DIAGNOSTIC
849 if (sc->sc_base.me_evp == NULL) {
850 printf("wskbdread: evp == NULL\n");
851 return (EINVAL);
852 }
853 #endif
854
855 sc->sc_refcnt++;
856 error = wsevent_read(sc->sc_base.me_evp, uio, flags);
857 if (--sc->sc_refcnt < 0) {
858 wakeup(sc);
859 error = EIO;
860 }
861 return (error);
862 }
863
864 int
865 wskbdioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
866 {
867 return (wskbd_do_ioctl(wskbd_cd.cd_devs[minor(dev)], cmd, data, flag,p));
868 }
869
870 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
871 int
872 wskbd_do_ioctl(struct device *dv, u_long cmd, caddr_t data, int flag,
873 struct proc *p)
874 {
875 struct wskbd_softc *sc = (struct wskbd_softc *)dv;
876 int error;
877
878 sc->sc_refcnt++;
879 error = wskbd_do_ioctl_sc(sc, cmd, data, flag, p);
880 if (--sc->sc_refcnt < 0)
881 wakeup(sc);
882 return (error);
883 }
884
885 int
886 wskbd_do_ioctl_sc(struct wskbd_softc *sc, u_long cmd, caddr_t data, int flag,
887 struct proc *p)
888 {
889
890 /*
891 * Try the generic ioctls that the wskbd interface supports.
892 */
893 switch (cmd) {
894 case FIONBIO: /* we will remove this someday (soon???) */
895 return (0);
896
897 case FIOASYNC:
898 if (sc->sc_base.me_evp == NULL)
899 return (EINVAL);
900 sc->sc_base.me_evp->async = *(int *)data != 0;
901 return (0);
902
903 case FIOSETOWN:
904 if (sc->sc_base.me_evp == NULL)
905 return (EINVAL);
906 if (-*(int *)data != sc->sc_base.me_evp->io->p_pgid
907 && *(int *)data != sc->sc_base.me_evp->io->p_pid)
908 return (EPERM);
909 return (0);
910
911 case TIOCSPGRP:
912 if (sc->sc_base.me_evp == NULL)
913 return (EINVAL);
914 if (*(int *)data != sc->sc_base.me_evp->io->p_pgid)
915 return (EPERM);
916 return (0);
917 }
918
919 /*
920 * Try the keyboard driver for WSKBDIO ioctls. It returns EPASSTHROUGH
921 * if it didn't recognize the request.
922 */
923 return (wskbd_displayioctl(&sc->sc_base.me_dv, cmd, data, flag, p));
924 }
925
926 /*
927 * WSKBDIO ioctls, handled in both emulation mode and in ``raw'' mode.
928 * Some of these have no real effect in raw mode, however.
929 */
930 static int
931 wskbd_displayioctl(struct device *dev, u_long cmd, caddr_t data, int flag,
932 struct proc *p)
933 {
934 #ifdef WSDISPLAY_SCROLLSUPPORT
935 struct wskbd_scroll_data *usdp, *ksdp;
936 #endif
937 struct wskbd_softc *sc = (struct wskbd_softc *)dev;
938 struct wskbd_bell_data *ubdp, *kbdp;
939 struct wskbd_keyrepeat_data *ukdp, *kkdp;
940 struct wskbd_map_data *umdp;
941 struct wskbd_mapdata md;
942 kbd_t enc;
943 void *buf;
944 int len, error;
945
946 switch (cmd) {
947 #define SETBELL(dstp, srcp, dfltp) \
948 do { \
949 (dstp)->pitch = ((srcp)->which & WSKBD_BELL_DOPITCH) ? \
950 (srcp)->pitch : (dfltp)->pitch; \
951 (dstp)->period = ((srcp)->which & WSKBD_BELL_DOPERIOD) ? \
952 (srcp)->period : (dfltp)->period; \
953 (dstp)->volume = ((srcp)->which & WSKBD_BELL_DOVOLUME) ? \
954 (srcp)->volume : (dfltp)->volume; \
955 (dstp)->which = WSKBD_BELL_DOALL; \
956 } while (0)
957
958 case WSKBDIO_BELL:
959 if ((flag & FWRITE) == 0)
960 return (EACCES);
961 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
962 WSKBDIO_COMPLEXBELL, (caddr_t)&sc->sc_bell_data, flag, p));
963
964 case WSKBDIO_COMPLEXBELL:
965 if ((flag & FWRITE) == 0)
966 return (EACCES);
967 ubdp = (struct wskbd_bell_data *)data;
968 SETBELL(ubdp, ubdp, &sc->sc_bell_data);
969 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
970 WSKBDIO_COMPLEXBELL, (caddr_t)ubdp, flag, p));
971
972 case WSKBDIO_SETBELL:
973 if ((flag & FWRITE) == 0)
974 return (EACCES);
975 kbdp = &sc->sc_bell_data;
976 setbell:
977 ubdp = (struct wskbd_bell_data *)data;
978 SETBELL(kbdp, ubdp, kbdp);
979 return (0);
980
981 case WSKBDIO_GETBELL:
982 kbdp = &sc->sc_bell_data;
983 getbell:
984 ubdp = (struct wskbd_bell_data *)data;
985 SETBELL(ubdp, kbdp, kbdp);
986 return (0);
987
988 case WSKBDIO_SETDEFAULTBELL:
989 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
990 return (error);
991 kbdp = &wskbd_default_bell_data;
992 goto setbell;
993
994
995 case WSKBDIO_GETDEFAULTBELL:
996 kbdp = &wskbd_default_bell_data;
997 goto getbell;
998
999 #undef SETBELL
1000
1001 #define SETKEYREPEAT(dstp, srcp, dfltp) \
1002 do { \
1003 (dstp)->del1 = ((srcp)->which & WSKBD_KEYREPEAT_DODEL1) ? \
1004 (srcp)->del1 : (dfltp)->del1; \
1005 (dstp)->delN = ((srcp)->which & WSKBD_KEYREPEAT_DODELN) ? \
1006 (srcp)->delN : (dfltp)->delN; \
1007 (dstp)->which = WSKBD_KEYREPEAT_DOALL; \
1008 } while (0)
1009
1010 case WSKBDIO_SETKEYREPEAT:
1011 if ((flag & FWRITE) == 0)
1012 return (EACCES);
1013 kkdp = &sc->sc_keyrepeat_data;
1014 setkeyrepeat:
1015 ukdp = (struct wskbd_keyrepeat_data *)data;
1016 SETKEYREPEAT(kkdp, ukdp, kkdp);
1017 return (0);
1018
1019 case WSKBDIO_GETKEYREPEAT:
1020 kkdp = &sc->sc_keyrepeat_data;
1021 getkeyrepeat:
1022 ukdp = (struct wskbd_keyrepeat_data *)data;
1023 SETKEYREPEAT(ukdp, kkdp, kkdp);
1024 return (0);
1025
1026 case WSKBDIO_SETDEFAULTKEYREPEAT:
1027 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1028 return (error);
1029 kkdp = &wskbd_default_keyrepeat_data;
1030 goto setkeyrepeat;
1031
1032
1033 case WSKBDIO_GETDEFAULTKEYREPEAT:
1034 kkdp = &wskbd_default_keyrepeat_data;
1035 goto getkeyrepeat;
1036
1037 #ifdef WSDISPLAY_SCROLLSUPPORT
1038 #define SETSCROLLMOD(dstp, srcp, dfltp) \
1039 do { \
1040 (dstp)->mode = ((srcp)->which & WSKBD_SCROLL_DOMODE) ? \
1041 (srcp)->mode : (dfltp)->mode; \
1042 (dstp)->modifier = ((srcp)->which & WSKBD_SCROLL_DOMODIFIER) ? \
1043 (srcp)->modifier : (dfltp)->modifier; \
1044 (dstp)->which = WSKBD_SCROLL_DOALL; \
1045 } while (0)
1046
1047 case WSKBDIO_SETSCROLL:
1048 usdp = (struct wskbd_scroll_data *)data;
1049 ksdp = &sc->sc_scroll_data;
1050 SETSCROLLMOD(ksdp, usdp, ksdp);
1051 return (0);
1052
1053 case WSKBDIO_GETSCROLL:
1054 usdp = (struct wskbd_scroll_data *)data;
1055 ksdp = &sc->sc_scroll_data;
1056 SETSCROLLMOD(usdp, ksdp, ksdp);
1057 return (0);
1058 #endif
1059
1060 #undef SETKEYREPEAT
1061
1062 case WSKBDIO_SETMAP:
1063 if ((flag & FWRITE) == 0)
1064 return (EACCES);
1065 umdp = (struct wskbd_map_data *)data;
1066 if (umdp->maplen > WSKBDIO_MAXMAPLEN)
1067 return (EINVAL);
1068
1069 len = umdp->maplen*sizeof(struct wscons_keymap);
1070 buf = malloc(len, M_TEMP, M_WAITOK);
1071 error = copyin(umdp->map, buf, len);
1072 if (error == 0) {
1073 wskbd_init_keymap(umdp->maplen,
1074 &sc->sc_map, &sc->sc_maplen);
1075 memcpy(sc->sc_map, buf, len);
1076 /* drop the variant bits handled by the map */
1077 sc->sc_layout = KB_USER |
1078 (KB_VARIANT(sc->sc_layout) & KB_HANDLEDBYWSKBD);
1079 wskbd_update_layout(sc->id, sc->sc_layout);
1080 }
1081 free(buf, M_TEMP);
1082 return(error);
1083
1084 case WSKBDIO_GETMAP:
1085 umdp = (struct wskbd_map_data *)data;
1086 if (umdp->maplen > sc->sc_maplen)
1087 umdp->maplen = sc->sc_maplen;
1088 error = copyout(sc->sc_map, umdp->map,
1089 umdp->maplen*sizeof(struct wscons_keymap));
1090 return(error);
1091
1092 case WSKBDIO_GETENCODING:
1093 *((kbd_t *) data) = sc->sc_layout;
1094 return(0);
1095
1096 case WSKBDIO_SETENCODING:
1097 if ((flag & FWRITE) == 0)
1098 return (EACCES);
1099 enc = *((kbd_t *)data);
1100 if (KB_ENCODING(enc) == KB_USER) {
1101 /* user map must already be loaded */
1102 if (KB_ENCODING(sc->sc_layout) != KB_USER)
1103 return (EINVAL);
1104 /* map variants make no sense */
1105 if (KB_VARIANT(enc) & ~KB_HANDLEDBYWSKBD)
1106 return (EINVAL);
1107 } else {
1108 md = *(sc->id->t_keymap); /* structure assignment */
1109 md.layout = enc;
1110 error = wskbd_load_keymap(&md, &sc->sc_map,
1111 &sc->sc_maplen);
1112 if (error)
1113 return (error);
1114 }
1115 sc->sc_layout = enc;
1116 wskbd_update_layout(sc->id, enc);
1117 return (0);
1118 }
1119
1120 /*
1121 * Try the keyboard driver for WSKBDIO ioctls. It returns -1
1122 * if it didn't recognize the request, and in turn we return
1123 * -1 if we didn't recognize the request.
1124 */
1125 /* printf("kbdaccess\n"); */
1126 error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
1127 flag, p);
1128 #ifdef WSDISPLAY_COMPAT_RAWKBD
1129 if (!error && cmd == WSKBDIO_SETMODE && *(int *)data == WSKBD_RAW) {
1130 int s = spltty();
1131 sc->id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
1132 | MOD_CONTROL_L | MOD_CONTROL_R
1133 | MOD_META_L | MOD_META_R
1134 | MOD_COMMAND
1135 | MOD_COMMAND1 | MOD_COMMAND2);
1136 #if NWSDISPLAY > 0
1137 if (sc->sc_repeating) {
1138 sc->sc_repeating = 0;
1139 callout_stop(&sc->sc_repeat_ch);
1140 }
1141 #endif
1142 splx(s);
1143 }
1144 #endif
1145 return (error);
1146 }
1147
1148 int
1149 wskbdpoll(dev_t dev, int events, struct proc *p)
1150 {
1151 struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
1152
1153 if (sc->sc_base.me_evp == NULL)
1154 return (EINVAL);
1155 return (wsevent_poll(sc->sc_base.me_evp, events, p));
1156 }
1157
1158 int
1159 wskbdkqfilter(dev_t dev, struct knote *kn)
1160 {
1161 struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
1162
1163 if (sc->sc_base.me_evp == NULL)
1164 return (1);
1165 return (wsevent_kqfilter(sc->sc_base.me_evp, kn));
1166 }
1167
1168 #if NWSDISPLAY > 0
1169
1170 int
1171 wskbd_pickfree(void)
1172 {
1173 int i;
1174 struct wskbd_softc *sc;
1175
1176 for (i = 0; i < wskbd_cd.cd_ndevs; i++) {
1177 if ((sc = wskbd_cd.cd_devs[i]) == NULL)
1178 continue;
1179 if (sc->sc_base.me_dispdv == NULL)
1180 return (i);
1181 }
1182 return (-1);
1183 }
1184
1185 struct wsevsrc *
1186 wskbd_set_console_display(struct device *displaydv, struct wsevsrc *me)
1187 {
1188 struct wskbd_softc *sc = wskbd_console_device;
1189
1190 if (sc == NULL)
1191 return (NULL);
1192 sc->sc_base.me_dispdv = displaydv;
1193 #if NWSMUX > 0
1194 (void)wsmux_attach_sc((struct wsmux_softc *)me, &sc->sc_base);
1195 #endif
1196 return (&sc->sc_base);
1197 }
1198
1199 int
1200 wskbd_set_display(struct device *dv, struct wsevsrc *me)
1201 {
1202 struct wskbd_softc *sc = (struct wskbd_softc *)dv;
1203 struct device *displaydv = me != NULL ? me->me_dispdv : NULL;
1204 struct device *odisplaydv;
1205 int error;
1206
1207 DPRINTF(("wskbd_set_display: %s me=%p odisp=%p disp=%p cons=%d\n",
1208 dv->dv_xname, me, sc->sc_base.me_dispdv, displaydv,
1209 sc->sc_isconsole));
1210
1211 if (sc->sc_isconsole)
1212 return (EBUSY);
1213
1214 if (displaydv != NULL) {
1215 if (sc->sc_base.me_dispdv != NULL)
1216 return (EBUSY);
1217 } else {
1218 if (sc->sc_base.me_dispdv == NULL)
1219 return (ENXIO);
1220 }
1221
1222 odisplaydv = sc->sc_base.me_dispdv;
1223 sc->sc_base.me_dispdv = NULL;
1224 error = wskbd_enable(sc, displaydv != NULL);
1225 sc->sc_base.me_dispdv = displaydv;
1226 if (error) {
1227 sc->sc_base.me_dispdv = odisplaydv;
1228 return (error);
1229 }
1230
1231 if (displaydv)
1232 printf("%s: connecting to %s\n",
1233 sc->sc_base.me_dv.dv_xname, displaydv->dv_xname);
1234 else
1235 printf("%s: disconnecting from %s\n",
1236 sc->sc_base.me_dv.dv_xname, odisplaydv->dv_xname);
1237
1238 return (0);
1239 }
1240
1241 #endif /* NWSDISPLAY > 0 */
1242
1243 #if NWSMUX > 0
1244 int
1245 wskbd_add_mux(int unit, struct wsmux_softc *muxsc)
1246 {
1247 struct wskbd_softc *sc;
1248
1249 if (unit < 0 || unit >= wskbd_cd.cd_ndevs ||
1250 (sc = wskbd_cd.cd_devs[unit]) == NULL)
1251 return (ENXIO);
1252
1253 if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
1254 return (EBUSY);
1255
1256 return (wsmux_attach_sc(muxsc, &sc->sc_base));
1257 }
1258 #endif
1259
1260 /*
1261 * Console interface.
1262 */
1263 int
1264 wskbd_cngetc(dev_t dev)
1265 {
1266 static int num = 0;
1267 static int pos;
1268 u_int type;
1269 int data;
1270 keysym_t ks;
1271
1272 if (!wskbd_console_initted)
1273 return 0;
1274
1275 if (wskbd_console_device != NULL &&
1276 !wskbd_console_device->sc_translating)
1277 return 0;
1278
1279 for(;;) {
1280 if (num-- > 0) {
1281 ks = wskbd_console_data.t_symbols[pos++];
1282 if (KS_GROUP(ks) == KS_GROUP_Ascii)
1283 return (KS_VALUE(ks));
1284 } else {
1285 (*wskbd_console_data.t_consops->getc)
1286 (wskbd_console_data.t_consaccesscookie,
1287 &type, &data);
1288 num = wskbd_translate(&wskbd_console_data, type, data);
1289 pos = 0;
1290 }
1291 }
1292 }
1293
1294 void
1295 wskbd_cnpollc(dev_t dev, int poll)
1296 {
1297
1298 if (!wskbd_console_initted)
1299 return;
1300
1301 if (wskbd_console_device != NULL &&
1302 !wskbd_console_device->sc_translating)
1303 return;
1304
1305 (*wskbd_console_data.t_consops->pollc)
1306 (wskbd_console_data.t_consaccesscookie, poll);
1307 }
1308
1309 void
1310 wskbd_cnbell(dev_t dev, u_int pitch, u_int period, u_int volume)
1311 {
1312
1313 if (!wskbd_console_initted)
1314 return;
1315
1316 if (wskbd_console_data.t_consops->bell != NULL)
1317 (*wskbd_console_data.t_consops->bell)
1318 (wskbd_console_data.t_consaccesscookie, pitch, period,
1319 volume);
1320 }
1321
1322 static inline void
1323 update_leds(struct wskbd_internal *id)
1324 {
1325 int new_state;
1326
1327 new_state = 0;
1328 if (id->t_modifiers & (MOD_SHIFTLOCK | MOD_CAPSLOCK))
1329 new_state |= WSKBD_LED_CAPS;
1330 if (id->t_modifiers & MOD_NUMLOCK)
1331 new_state |= WSKBD_LED_NUM;
1332 if (id->t_modifiers & MOD_COMPOSE)
1333 new_state |= WSKBD_LED_COMPOSE;
1334 if (id->t_modifiers & MOD_HOLDSCREEN)
1335 new_state |= WSKBD_LED_SCROLL;
1336
1337 if (id->t_sc && new_state != id->t_sc->sc_ledstate) {
1338 (*id->t_sc->sc_accessops->set_leds)
1339 (id->t_sc->sc_accesscookie, new_state);
1340 id->t_sc->sc_ledstate = new_state;
1341 }
1342 }
1343
1344 static inline void
1345 update_modifier(struct wskbd_internal *id, u_int type, int toggle, int mask)
1346 {
1347 if (toggle) {
1348 if (type == WSCONS_EVENT_KEY_DOWN)
1349 id->t_modifiers ^= mask;
1350 } else {
1351 if (type == WSCONS_EVENT_KEY_DOWN)
1352 id->t_modifiers |= mask;
1353 else
1354 id->t_modifiers &= ~mask;
1355 }
1356 }
1357
1358 #if NWSDISPLAY > 0
1359 static void
1360 change_displayparam(struct wskbd_softc *sc, int param, int updown,
1361 int wraparound)
1362 {
1363 int res;
1364 struct wsdisplay_param dp;
1365
1366 dp.param = param;
1367 res = wsdisplay_param(sc->sc_base.me_dispdv, WSDISPLAYIO_GETPARAM, &dp);
1368
1369 if (res == EINVAL)
1370 return; /* no such parameter */
1371
1372 dp.curval += updown;
1373 if (dp.max < dp.curval)
1374 dp.curval = wraparound ? dp.min : dp.max;
1375 else
1376 if (dp.curval < dp.min)
1377 dp.curval = wraparound ? dp.max : dp.min;
1378 wsdisplay_param(sc->sc_base.me_dispdv, WSDISPLAYIO_SETPARAM, &dp);
1379 }
1380 #endif
1381
1382 static int
1383 internal_command(struct wskbd_softc *sc, u_int *type, keysym_t ksym,
1384 keysym_t ksym2)
1385 {
1386 u_int state=0;
1387 switch (ksym) {
1388 #ifdef WSDISPLAY_SCROLLSUPPORT
1389 case KS_Cmd_ScrollFastUp:
1390 case KS_Cmd_ScrollFastDown:
1391 if (*type == WSCONS_EVENT_KEY_DOWN) {
1392 GETMODSTATE(sc->id->t_modifiers, state);
1393 if ((sc->sc_scroll_data.mode == WSKBD_SCROLL_MODE_HOLD
1394 && MOD_ONESET(sc->id, MOD_HOLDSCREEN))
1395 || (sc->sc_scroll_data.mode == WSKBD_SCROLL_MODE_NORMAL
1396 && sc->sc_scroll_data.modifier == state)) {
1397 update_modifier(sc->id, *type, 0, MOD_COMMAND);
1398 wsdisplay_scroll(sc->sc_base.me_dispdv,
1399 (ksym == KS_Cmd_ScrollFastUp) ?
1400 WSDISPLAY_SCROLL_BACKWARD :
1401 WSDISPLAY_SCROLL_FORWARD);
1402 return (1);
1403 } else {
1404 return (0);
1405 }
1406 }
1407
1408 case KS_Cmd_ScrollSlowUp:
1409 case KS_Cmd_ScrollSlowDown:
1410 if (*type == WSCONS_EVENT_KEY_DOWN) {
1411 GETMODSTATE(sc->id->t_modifiers, state);
1412 if ((sc->sc_scroll_data.mode == WSKBD_SCROLL_MODE_HOLD
1413 && MOD_ONESET(sc->id, MOD_HOLDSCREEN))
1414 || (sc->sc_scroll_data.mode == WSKBD_SCROLL_MODE_NORMAL
1415 && sc->sc_scroll_data.modifier == state)) {
1416 update_modifier(sc->id, *type, 0, MOD_COMMAND);
1417 wsdisplay_scroll(sc->sc_base.me_dispdv,
1418 (ksym == KS_Cmd_ScrollSlowUp) ?
1419 WSDISPLAY_SCROLL_BACKWARD | WSDISPLAY_SCROLL_LOW:
1420 WSDISPLAY_SCROLL_FORWARD | WSDISPLAY_SCROLL_LOW);
1421 return (1);
1422 } else {
1423 return (0);
1424 }
1425 }
1426 #endif
1427
1428 case KS_Cmd:
1429 update_modifier(sc->id, *type, 0, MOD_COMMAND);
1430 ksym = ksym2;
1431 break;
1432
1433 case KS_Cmd1:
1434 update_modifier(sc->id, *type, 0, MOD_COMMAND1);
1435 break;
1436
1437 case KS_Cmd2:
1438 update_modifier(sc->id, *type, 0, MOD_COMMAND2);
1439 break;
1440 }
1441
1442 if (*type != WSCONS_EVENT_KEY_DOWN ||
1443 (! MOD_ONESET(sc->id, MOD_COMMAND) &&
1444 ! MOD_ALLSET(sc->id, MOD_COMMAND1 | MOD_COMMAND2)))
1445 return (0);
1446
1447 #if defined(DDB) || defined(KGDB)
1448 if (ksym == KS_Cmd_Debugger) {
1449 if (sc->sc_isconsole) {
1450 #ifdef DDB
1451 console_debugger();
1452 #endif
1453 #ifdef KGDB
1454 kgdb_connect(1);
1455 #endif
1456 }
1457 /* discard this key (ddb discarded command modifiers) */
1458 *type = WSCONS_EVENT_KEY_UP;
1459 return (1);
1460 }
1461 #endif
1462
1463 #if NWSDISPLAY > 0
1464 if (sc->sc_base.me_dispdv == NULL)
1465 return (0);
1466
1467 switch (ksym) {
1468 case KS_Cmd_Screen0:
1469 case KS_Cmd_Screen1:
1470 case KS_Cmd_Screen2:
1471 case KS_Cmd_Screen3:
1472 case KS_Cmd_Screen4:
1473 case KS_Cmd_Screen5:
1474 case KS_Cmd_Screen6:
1475 case KS_Cmd_Screen7:
1476 case KS_Cmd_Screen8:
1477 case KS_Cmd_Screen9:
1478 wsdisplay_switch(sc->sc_base.me_dispdv, ksym - KS_Cmd_Screen0, 0);
1479 return (1);
1480 case KS_Cmd_ResetEmul:
1481 wsdisplay_reset(sc->sc_base.me_dispdv, WSDISPLAY_RESETEMUL);
1482 return (1);
1483 case KS_Cmd_ResetClose:
1484 wsdisplay_reset(sc->sc_base.me_dispdv, WSDISPLAY_RESETCLOSE);
1485 return (1);
1486 case KS_Cmd_BacklightOn:
1487 case KS_Cmd_BacklightOff:
1488 case KS_Cmd_BacklightToggle:
1489 change_displayparam(sc, WSDISPLAYIO_PARAM_BACKLIGHT,
1490 ksym == KS_Cmd_BacklightOff ? -1 : 1,
1491 ksym == KS_Cmd_BacklightToggle ? 1 : 0);
1492 return (1);
1493 case KS_Cmd_BrightnessUp:
1494 case KS_Cmd_BrightnessDown:
1495 case KS_Cmd_BrightnessRotate:
1496 change_displayparam(sc, WSDISPLAYIO_PARAM_BRIGHTNESS,
1497 ksym == KS_Cmd_BrightnessDown ? -1 : 1,
1498 ksym == KS_Cmd_BrightnessRotate ? 1 : 0);
1499 return (1);
1500 case KS_Cmd_ContrastUp:
1501 case KS_Cmd_ContrastDown:
1502 case KS_Cmd_ContrastRotate:
1503 change_displayparam(sc, WSDISPLAYIO_PARAM_CONTRAST,
1504 ksym == KS_Cmd_ContrastDown ? -1 : 1,
1505 ksym == KS_Cmd_ContrastRotate ? 1 : 0);
1506 return (1);
1507 }
1508 #endif
1509
1510 return (0);
1511 }
1512
1513 static int
1514 wskbd_translate(struct wskbd_internal *id, u_int type, int value)
1515 {
1516 struct wskbd_softc *sc = id->t_sc;
1517 keysym_t ksym, res, *group;
1518 struct wscons_keymap kpbuf, *kp;
1519 int iscommand = 0;
1520
1521 if (type == WSCONS_EVENT_ALL_KEYS_UP) {
1522 id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
1523 | MOD_CONTROL_L | MOD_CONTROL_R
1524 | MOD_META_L | MOD_META_R
1525 | MOD_MODESHIFT
1526 | MOD_COMMAND | MOD_COMMAND1 | MOD_COMMAND2);
1527 update_leds(id);
1528 return (0);
1529 }
1530
1531 if (sc != NULL) {
1532 if (value < 0 || value >= sc->sc_maplen) {
1533 #ifdef DEBUG
1534 printf("wskbd_translate: keycode %d out of range\n",
1535 value);
1536 #endif
1537 return (0);
1538 }
1539 kp = sc->sc_map + value;
1540 } else {
1541 kp = &kpbuf;
1542 wskbd_get_mapentry(id->t_keymap, value, kp);
1543 }
1544
1545 /* if this key has a command, process it first */
1546 if (sc != NULL && kp->command != KS_voidSymbol)
1547 iscommand = internal_command(sc, &type, kp->command,
1548 kp->group1[0]);
1549
1550 /* Now update modifiers */
1551 switch (kp->group1[0]) {
1552 case KS_Shift_L:
1553 update_modifier(id, type, 0, MOD_SHIFT_L);
1554 break;
1555
1556 case KS_Shift_R:
1557 update_modifier(id, type, 0, MOD_SHIFT_R);
1558 break;
1559
1560 case KS_Shift_Lock:
1561 update_modifier(id, type, 1, MOD_SHIFTLOCK);
1562 break;
1563
1564 case KS_Caps_Lock:
1565 update_modifier(id, type, 1, MOD_CAPSLOCK);
1566 break;
1567
1568 case KS_Control_L:
1569 update_modifier(id, type, 0, MOD_CONTROL_L);
1570 break;
1571
1572 case KS_Control_R:
1573 update_modifier(id, type, 0, MOD_CONTROL_R);
1574 break;
1575
1576 case KS_Alt_L:
1577 update_modifier(id, type, 0, MOD_META_L);
1578 break;
1579
1580 case KS_Alt_R:
1581 update_modifier(id, type, 0, MOD_META_R);
1582 break;
1583
1584 case KS_Mode_switch:
1585 update_modifier(id, type, 0, MOD_MODESHIFT);
1586 break;
1587
1588 case KS_Num_Lock:
1589 update_modifier(id, type, 1, MOD_NUMLOCK);
1590 break;
1591
1592 #if NWSDISPLAY > 0
1593 case KS_Hold_Screen:
1594 if (sc != NULL) {
1595 update_modifier(id, type, 1, MOD_HOLDSCREEN);
1596 wskbd_holdscreen(sc, id->t_modifiers & MOD_HOLDSCREEN);
1597 }
1598 break;
1599 #endif
1600 }
1601
1602 /* If this is a key release or we are in command mode, we are done */
1603 if (type != WSCONS_EVENT_KEY_DOWN || iscommand) {
1604 update_leds(id);
1605 return (0);
1606 }
1607
1608 /* Get the keysym */
1609 if (id->t_modifiers & MOD_MODESHIFT)
1610 group = & kp->group2[0];
1611 else
1612 group = & kp->group1[0];
1613
1614 if ((id->t_modifiers & MOD_NUMLOCK) != 0 &&
1615 KS_GROUP(group[1]) == KS_GROUP_Keypad) {
1616 if (MOD_ONESET(id, MOD_ANYSHIFT))
1617 ksym = group[0];
1618 else
1619 ksym = group[1];
1620 } else if (! MOD_ONESET(id, MOD_ANYSHIFT | MOD_CAPSLOCK)) {
1621 ksym = group[0];
1622 } else if (MOD_ONESET(id, MOD_CAPSLOCK)) {
1623 if (! MOD_ONESET(id, MOD_SHIFT_L | MOD_SHIFT_R))
1624 ksym = group[0];
1625 else
1626 ksym = group[1];
1627 if (ksym >= KS_a && ksym <= KS_z)
1628 ksym += KS_A - KS_a;
1629 else if (ksym >= KS_agrave && ksym <= KS_thorn &&
1630 ksym != KS_division)
1631 ksym += KS_Agrave - KS_agrave;
1632 } else if (MOD_ONESET(id, MOD_ANYSHIFT)) {
1633 ksym = group[1];
1634 } else {
1635 ksym = group[0];
1636 }
1637
1638 /* Process compose sequence and dead accents */
1639 res = KS_voidSymbol;
1640
1641 switch (KS_GROUP(ksym)) {
1642 case KS_GROUP_Ascii:
1643 case KS_GROUP_Keypad:
1644 case KS_GROUP_Function:
1645 res = ksym;
1646 break;
1647
1648 case KS_GROUP_Mod:
1649 if (ksym == KS_Multi_key) {
1650 update_modifier(id, 1, 0, MOD_COMPOSE);
1651 id->t_composelen = 2;
1652 }
1653 break;
1654
1655 case KS_GROUP_Dead:
1656 if (id->t_composelen == 0) {
1657 update_modifier(id, 1, 0, MOD_COMPOSE);
1658 id->t_composelen = 1;
1659 id->t_composebuf[0] = ksym;
1660 } else
1661 res = ksym;
1662 break;
1663 }
1664
1665 if (res == KS_voidSymbol) {
1666 update_leds(id);
1667 return (0);
1668 }
1669
1670 if (id->t_composelen > 0) {
1671 id->t_composebuf[2 - id->t_composelen] = res;
1672 if (--id->t_composelen == 0) {
1673 res = wskbd_compose_value(id->t_composebuf);
1674 update_modifier(id, 0, 0, MOD_COMPOSE);
1675 } else {
1676 return (0);
1677 }
1678 }
1679
1680 update_leds(id);
1681
1682 /* We are done, return the symbol */
1683 if (KS_GROUP(res) == KS_GROUP_Ascii) {
1684 if (MOD_ONESET(id, MOD_ANYCONTROL)) {
1685 if ((res >= KS_at && res <= KS_z) || res == KS_space)
1686 res = res & 0x1f;
1687 else if (res == KS_2)
1688 res = 0x00;
1689 else if (res >= KS_3 && res <= KS_7)
1690 res = KS_Escape + (res - KS_3);
1691 else if (res == KS_8)
1692 res = KS_Delete;
1693 }
1694 if (MOD_ONESET(id, MOD_ANYMETA)) {
1695 if (id->t_flags & WSKFL_METAESC) {
1696 id->t_symbols[0] = KS_Escape;
1697 id->t_symbols[1] = res;
1698 return (2);
1699 } else
1700 res |= 0x80;
1701 }
1702 }
1703
1704 id->t_symbols[0] = res;
1705 return (1);
1706 }
1707