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