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