wskbd.c revision 1.119 1 /* $NetBSD: wskbd.c,v 1.119 2008/05/04 13:19:17 martin 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.119 2008/05/04 13:19:17 martin 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 if ((sc = device_private(wskbd_cd.cd_devs[i])) == NULL)
1288 continue;
1289 if (sc->sc_base.me_dispdv == NULL)
1290 return (i);
1291 }
1292 return (-1);
1293 }
1294
1295 struct wsevsrc *
1296 wskbd_set_console_display(device_t displaydv, struct wsevsrc *me)
1297 {
1298 struct wskbd_softc *sc = wskbd_console_device;
1299
1300 if (sc == NULL)
1301 return (NULL);
1302 sc->sc_base.me_dispdv = displaydv;
1303 #if NWSMUX > 0
1304 (void)wsmux_attach_sc((struct wsmux_softc *)me, &sc->sc_base);
1305 #endif
1306 return (&sc->sc_base);
1307 }
1308
1309 int
1310 wskbd_set_display(device_t dv, struct wsevsrc *me)
1311 {
1312 struct wskbd_softc *sc = device_private(dv);
1313 device_t displaydv = me != NULL ? me->me_dispdv : NULL;
1314 device_t odisplaydv;
1315 int error;
1316
1317 DPRINTF(("wskbd_set_display: %s me=%p odisp=%p disp=%p cons=%d\n",
1318 device_xname(dv), me, sc->sc_base.me_dispdv, displaydv,
1319 sc->sc_isconsole));
1320
1321 if (sc->sc_isconsole)
1322 return (EBUSY);
1323
1324 if (displaydv != NULL) {
1325 if (sc->sc_base.me_dispdv != NULL)
1326 return (EBUSY);
1327 } else {
1328 if (sc->sc_base.me_dispdv == NULL)
1329 return (ENXIO);
1330 }
1331
1332 odisplaydv = sc->sc_base.me_dispdv;
1333 sc->sc_base.me_dispdv = NULL;
1334 error = wskbd_enable(sc, displaydv != NULL);
1335 sc->sc_base.me_dispdv = displaydv;
1336 if (error) {
1337 sc->sc_base.me_dispdv = odisplaydv;
1338 return (error);
1339 }
1340
1341 if (displaydv)
1342 aprint_verbose_dev(sc->sc_base.me_dv, "connecting to %s\n",
1343 device_xname(displaydv));
1344 else
1345 aprint_verbose_dev(sc->sc_base.me_dv, "disconnecting from %s\n",
1346 device_xname(odisplaydv));
1347
1348 return (0);
1349 }
1350
1351 #endif /* NWSDISPLAY > 0 */
1352
1353 #if NWSMUX > 0
1354 int
1355 wskbd_add_mux(int unit, struct wsmux_softc *muxsc)
1356 {
1357 struct wskbd_softc *sc = device_lookup_private(&wskbd_cd, unit);
1358
1359 if (sc == NULL)
1360 return (ENXIO);
1361
1362 if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
1363 return (EBUSY);
1364
1365 return (wsmux_attach_sc(muxsc, &sc->sc_base));
1366 }
1367 #endif
1368
1369 /*
1370 * Console interface.
1371 */
1372 int
1373 wskbd_cngetc(dev_t dev)
1374 {
1375 static int num = 0;
1376 static int pos;
1377 u_int type;
1378 int data;
1379 keysym_t ks;
1380
1381 if (!wskbd_console_initted)
1382 return 0;
1383
1384 if (wskbd_console_device != NULL &&
1385 !wskbd_console_device->sc_translating)
1386 return 0;
1387
1388 for(;;) {
1389 if (num-- > 0) {
1390 ks = wskbd_console_data.t_symbols[pos++];
1391 if (KS_GROUP(ks) == KS_GROUP_Ascii)
1392 return (KS_VALUE(ks));
1393 } else {
1394 (*wskbd_console_data.t_consops->getc)
1395 (wskbd_console_data.t_consaccesscookie,
1396 &type, &data);
1397 if (type == WSCONS_EVENT_ASCII) {
1398 /*
1399 * We assume that when the driver falls back
1400 * to deliver pure ASCII it is in a state that
1401 * it can not track press/release events
1402 * reliable - so we clear all previously
1403 * accumulated modifier state.
1404 */
1405 wskbd_console_data.t_modifiers = 0;
1406 return(data);
1407 }
1408 num = wskbd_translate(&wskbd_console_data, type, data);
1409 pos = 0;
1410 }
1411 }
1412 }
1413
1414 void
1415 wskbd_cnpollc(dev_t dev, int poll)
1416 {
1417
1418 if (!wskbd_console_initted)
1419 return;
1420
1421 if (wskbd_console_device != NULL &&
1422 !wskbd_console_device->sc_translating)
1423 return;
1424
1425 (*wskbd_console_data.t_consops->pollc)
1426 (wskbd_console_data.t_consaccesscookie, poll);
1427 }
1428
1429 void
1430 wskbd_cnbell(dev_t dev, u_int pitch, u_int period, u_int volume)
1431 {
1432
1433 if (!wskbd_console_initted)
1434 return;
1435
1436 if (wskbd_console_data.t_consops->bell != NULL)
1437 (*wskbd_console_data.t_consops->bell)
1438 (wskbd_console_data.t_consaccesscookie, pitch, period,
1439 volume);
1440 }
1441
1442 static inline void
1443 update_leds(struct wskbd_internal *id)
1444 {
1445 int new_state;
1446
1447 new_state = 0;
1448 if (id->t_modifiers & (MOD_SHIFTLOCK | MOD_CAPSLOCK))
1449 new_state |= WSKBD_LED_CAPS;
1450 if (id->t_modifiers & MOD_NUMLOCK)
1451 new_state |= WSKBD_LED_NUM;
1452 if (id->t_modifiers & MOD_COMPOSE)
1453 new_state |= WSKBD_LED_COMPOSE;
1454 if (id->t_modifiers & MOD_HOLDSCREEN)
1455 new_state |= WSKBD_LED_SCROLL;
1456
1457 if (id->t_sc && new_state != id->t_sc->sc_ledstate) {
1458 (*id->t_sc->sc_accessops->set_leds)
1459 (id->t_sc->sc_accesscookie, new_state);
1460 id->t_sc->sc_ledstate = new_state;
1461 }
1462 }
1463
1464 static inline void
1465 update_modifier(struct wskbd_internal *id, u_int type, int toggle, int mask)
1466 {
1467 if (toggle) {
1468 if (type == WSCONS_EVENT_KEY_DOWN)
1469 id->t_modifiers ^= mask;
1470 } else {
1471 if (type == WSCONS_EVENT_KEY_DOWN)
1472 id->t_modifiers |= mask;
1473 else
1474 id->t_modifiers &= ~mask;
1475 }
1476 }
1477
1478 #if NWSDISPLAY > 0
1479 static void
1480 change_displayparam(struct wskbd_softc *sc, int param, int updown,
1481 int wraparound)
1482 {
1483 int res;
1484 struct wsdisplay_param dp;
1485
1486 dp.param = param;
1487 res = wsdisplay_param(sc->sc_base.me_dispdv, WSDISPLAYIO_GETPARAM, &dp);
1488
1489 if (res == EINVAL)
1490 return; /* no such parameter */
1491
1492 dp.curval += updown;
1493 if (dp.max < dp.curval)
1494 dp.curval = wraparound ? dp.min : dp.max;
1495 else
1496 if (dp.curval < dp.min)
1497 dp.curval = wraparound ? dp.max : dp.min;
1498 wsdisplay_param(sc->sc_base.me_dispdv, WSDISPLAYIO_SETPARAM, &dp);
1499 }
1500 #endif
1501
1502 static int
1503 internal_command(struct wskbd_softc *sc, u_int *type, keysym_t ksym,
1504 keysym_t ksym2)
1505 {
1506 #ifdef WSDISPLAY_SCROLLSUPPORT
1507 u_int state = 0;
1508 #endif
1509 switch (ksym) {
1510 case KS_Cmd_VolumeToggle:
1511 if (*type == WSCONS_EVENT_KEY_DOWN)
1512 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_TOGGLE);
1513 break;
1514 case KS_Cmd_VolumeUp:
1515 if (*type == WSCONS_EVENT_KEY_DOWN)
1516 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
1517 break;
1518 case KS_Cmd_VolumeDown:
1519 if (*type == WSCONS_EVENT_KEY_DOWN)
1520 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
1521 break;
1522 #ifdef WSDISPLAY_SCROLLSUPPORT
1523 case KS_Cmd_ScrollFastUp:
1524 case KS_Cmd_ScrollFastDown:
1525 if (*type == WSCONS_EVENT_KEY_DOWN) {
1526 GETMODSTATE(sc->id->t_modifiers, state);
1527 if ((sc->sc_scroll_data.mode == WSKBD_SCROLL_MODE_HOLD
1528 && MOD_ONESET(sc->id, MOD_HOLDSCREEN))
1529 || (sc->sc_scroll_data.mode == WSKBD_SCROLL_MODE_NORMAL
1530 && sc->sc_scroll_data.modifier == state)) {
1531 update_modifier(sc->id, *type, 0, MOD_COMMAND);
1532 wsdisplay_scroll(sc->sc_base.me_dispdv,
1533 (ksym == KS_Cmd_ScrollFastUp) ?
1534 WSDISPLAY_SCROLL_BACKWARD :
1535 WSDISPLAY_SCROLL_FORWARD);
1536 return (1);
1537 } else {
1538 return (0);
1539 }
1540 }
1541
1542 case KS_Cmd_ScrollSlowUp:
1543 case KS_Cmd_ScrollSlowDown:
1544 if (*type == WSCONS_EVENT_KEY_DOWN) {
1545 GETMODSTATE(sc->id->t_modifiers, state);
1546 if ((sc->sc_scroll_data.mode == WSKBD_SCROLL_MODE_HOLD
1547 && MOD_ONESET(sc->id, MOD_HOLDSCREEN))
1548 || (sc->sc_scroll_data.mode == WSKBD_SCROLL_MODE_NORMAL
1549 && sc->sc_scroll_data.modifier == state)) {
1550 update_modifier(sc->id, *type, 0, MOD_COMMAND);
1551 wsdisplay_scroll(sc->sc_base.me_dispdv,
1552 (ksym == KS_Cmd_ScrollSlowUp) ?
1553 WSDISPLAY_SCROLL_BACKWARD | WSDISPLAY_SCROLL_LOW:
1554 WSDISPLAY_SCROLL_FORWARD | WSDISPLAY_SCROLL_LOW);
1555 return (1);
1556 } else {
1557 return (0);
1558 }
1559 }
1560 #endif
1561
1562 case KS_Cmd:
1563 update_modifier(sc->id, *type, 0, MOD_COMMAND);
1564 ksym = ksym2;
1565 break;
1566
1567 case KS_Cmd1:
1568 update_modifier(sc->id, *type, 0, MOD_COMMAND1);
1569 break;
1570
1571 case KS_Cmd2:
1572 update_modifier(sc->id, *type, 0, MOD_COMMAND2);
1573 break;
1574 }
1575
1576 if (*type != WSCONS_EVENT_KEY_DOWN ||
1577 (! MOD_ONESET(sc->id, MOD_COMMAND) &&
1578 ! MOD_ALLSET(sc->id, MOD_COMMAND1 | MOD_COMMAND2)))
1579 return (0);
1580
1581 #if defined(DDB) || defined(KGDB)
1582 if (ksym == KS_Cmd_Debugger) {
1583 if (sc->sc_isconsole) {
1584 #ifdef DDB
1585 console_debugger();
1586 #endif
1587 #ifdef KGDB
1588 kgdb_connect(1);
1589 #endif
1590 }
1591 /* discard this key (ddb discarded command modifiers) */
1592 *type = WSCONS_EVENT_KEY_UP;
1593 return (1);
1594 }
1595 #endif
1596
1597 #if NWSDISPLAY > 0
1598 if (sc->sc_base.me_dispdv == NULL)
1599 return (0);
1600
1601 switch (ksym) {
1602 case KS_Cmd_Screen0:
1603 case KS_Cmd_Screen1:
1604 case KS_Cmd_Screen2:
1605 case KS_Cmd_Screen3:
1606 case KS_Cmd_Screen4:
1607 case KS_Cmd_Screen5:
1608 case KS_Cmd_Screen6:
1609 case KS_Cmd_Screen7:
1610 case KS_Cmd_Screen8:
1611 case KS_Cmd_Screen9:
1612 wsdisplay_switch(sc->sc_base.me_dispdv, ksym - KS_Cmd_Screen0, 0);
1613 return (1);
1614 case KS_Cmd_ResetEmul:
1615 wsdisplay_reset(sc->sc_base.me_dispdv, WSDISPLAY_RESETEMUL);
1616 return (1);
1617 case KS_Cmd_ResetClose:
1618 wsdisplay_reset(sc->sc_base.me_dispdv, WSDISPLAY_RESETCLOSE);
1619 return (1);
1620 case KS_Cmd_BacklightOn:
1621 case KS_Cmd_BacklightOff:
1622 case KS_Cmd_BacklightToggle:
1623 change_displayparam(sc, WSDISPLAYIO_PARAM_BACKLIGHT,
1624 ksym == KS_Cmd_BacklightOff ? -1 : 1,
1625 ksym == KS_Cmd_BacklightToggle ? 1 : 0);
1626 return (1);
1627 case KS_Cmd_BrightnessUp:
1628 case KS_Cmd_BrightnessDown:
1629 case KS_Cmd_BrightnessRotate:
1630 change_displayparam(sc, WSDISPLAYIO_PARAM_BRIGHTNESS,
1631 ksym == KS_Cmd_BrightnessDown ? -1 : 1,
1632 ksym == KS_Cmd_BrightnessRotate ? 1 : 0);
1633 return (1);
1634 case KS_Cmd_ContrastUp:
1635 case KS_Cmd_ContrastDown:
1636 case KS_Cmd_ContrastRotate:
1637 change_displayparam(sc, WSDISPLAYIO_PARAM_CONTRAST,
1638 ksym == KS_Cmd_ContrastDown ? -1 : 1,
1639 ksym == KS_Cmd_ContrastRotate ? 1 : 0);
1640 return (1);
1641 }
1642 #endif
1643
1644 return (0);
1645 }
1646
1647 device_t
1648 wskbd_hotkey_register(device_t self, void *cookie, wskbd_hotkey_plugin *hotkey)
1649 {
1650 struct wskbd_softc *sc = device_private(self);
1651
1652 sc->sc_hotkey = hotkey;
1653 sc->sc_hotkeycookie = cookie;
1654 return sc->sc_base.me_dv;
1655 }
1656
1657 static int
1658 wskbd_translate(struct wskbd_internal *id, u_int type, int value)
1659 {
1660 struct wskbd_softc *sc = id->t_sc;
1661 keysym_t ksym, res, *group;
1662 struct wscons_keymap kpbuf, *kp;
1663 int iscommand = 0;
1664 int ishotkey = 0;
1665
1666 if (type == WSCONS_EVENT_ALL_KEYS_UP) {
1667 id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
1668 | MOD_CONTROL_L | MOD_CONTROL_R
1669 | MOD_META_L | MOD_META_R
1670 | MOD_MODESHIFT
1671 | MOD_COMMAND | MOD_COMMAND1 | MOD_COMMAND2);
1672 update_leds(id);
1673 return (0);
1674 }
1675
1676 if (sc != NULL) {
1677 if (sc->sc_hotkey != NULL)
1678 ishotkey = sc->sc_hotkey(sc, sc->sc_hotkeycookie,
1679 type, value);
1680 if (ishotkey)
1681 return 0;
1682
1683 if (value < 0 || value >= sc->sc_maplen) {
1684 #ifdef DEBUG
1685 printf("%s: keycode %d out of range\n",
1686 __func__, value);
1687 #endif
1688 return (0);
1689 }
1690 kp = sc->sc_map + value;
1691 } else {
1692 kp = &kpbuf;
1693 wskbd_get_mapentry(id->t_keymap, value, kp);
1694 }
1695
1696 /* if this key has a command, process it first */
1697 if (sc != NULL && kp->command != KS_voidSymbol)
1698 iscommand = internal_command(sc, &type, kp->command,
1699 kp->group1[0]);
1700
1701 /* Now update modifiers */
1702 switch (kp->group1[0]) {
1703 case KS_Shift_L:
1704 update_modifier(id, type, 0, MOD_SHIFT_L);
1705 break;
1706
1707 case KS_Shift_R:
1708 update_modifier(id, type, 0, MOD_SHIFT_R);
1709 break;
1710
1711 case KS_Shift_Lock:
1712 update_modifier(id, type, 1, MOD_SHIFTLOCK);
1713 break;
1714
1715 case KS_Caps_Lock:
1716 update_modifier(id, type, 1, MOD_CAPSLOCK);
1717 break;
1718
1719 case KS_Control_L:
1720 update_modifier(id, type, 0, MOD_CONTROL_L);
1721 break;
1722
1723 case KS_Control_R:
1724 update_modifier(id, type, 0, MOD_CONTROL_R);
1725 break;
1726
1727 case KS_Alt_L:
1728 update_modifier(id, type, 0, MOD_META_L);
1729 break;
1730
1731 case KS_Alt_R:
1732 update_modifier(id, type, 0, MOD_META_R);
1733 break;
1734
1735 case KS_Mode_switch:
1736 update_modifier(id, type, 0, MOD_MODESHIFT);
1737 break;
1738
1739 case KS_Num_Lock:
1740 update_modifier(id, type, 1, MOD_NUMLOCK);
1741 break;
1742
1743 #if NWSDISPLAY > 0
1744 case KS_Hold_Screen:
1745 if (sc != NULL) {
1746 update_modifier(id, type, 1, MOD_HOLDSCREEN);
1747 wskbd_holdscreen(sc, id->t_modifiers & MOD_HOLDSCREEN);
1748 }
1749 break;
1750 #endif
1751 }
1752
1753 /* If this is a key release or we are in command mode, we are done */
1754 if (type != WSCONS_EVENT_KEY_DOWN || iscommand) {
1755 update_leds(id);
1756 return (0);
1757 }
1758
1759 /* Get the keysym */
1760 if (id->t_modifiers & MOD_MODESHIFT)
1761 group = & kp->group2[0];
1762 else
1763 group = & kp->group1[0];
1764
1765 if ((id->t_modifiers & MOD_NUMLOCK) != 0 &&
1766 KS_GROUP(group[1]) == KS_GROUP_Keypad) {
1767 if (MOD_ONESET(id, MOD_ANYSHIFT))
1768 ksym = group[0];
1769 else
1770 ksym = group[1];
1771 } else if (! MOD_ONESET(id, MOD_ANYSHIFT | MOD_CAPSLOCK)) {
1772 ksym = group[0];
1773 } else if (MOD_ONESET(id, MOD_CAPSLOCK)) {
1774 if (! MOD_ONESET(id, MOD_SHIFT_L | MOD_SHIFT_R))
1775 ksym = group[0];
1776 else
1777 ksym = group[1];
1778 if (ksym >= KS_a && ksym <= KS_z)
1779 ksym += KS_A - KS_a;
1780 else if (ksym >= KS_agrave && ksym <= KS_thorn &&
1781 ksym != KS_division)
1782 ksym += KS_Agrave - KS_agrave;
1783 } else if (MOD_ONESET(id, MOD_ANYSHIFT)) {
1784 ksym = group[1];
1785 } else {
1786 ksym = group[0];
1787 }
1788
1789 /* Process compose sequence and dead accents */
1790 res = KS_voidSymbol;
1791
1792 switch (KS_GROUP(ksym)) {
1793 case KS_GROUP_Ascii:
1794 case KS_GROUP_Keypad:
1795 case KS_GROUP_Function:
1796 res = ksym;
1797 break;
1798
1799 case KS_GROUP_Mod:
1800 if (ksym == KS_Multi_key) {
1801 update_modifier(id, 1, 0, MOD_COMPOSE);
1802 id->t_composelen = 2;
1803 }
1804 break;
1805
1806 case KS_GROUP_Dead:
1807 if (id->t_composelen == 0) {
1808 update_modifier(id, 1, 0, MOD_COMPOSE);
1809 id->t_composelen = 1;
1810 id->t_composebuf[0] = ksym;
1811 } else
1812 res = ksym;
1813 break;
1814 }
1815
1816 if (res == KS_voidSymbol) {
1817 update_leds(id);
1818 return (0);
1819 }
1820
1821 if (id->t_composelen > 0) {
1822 id->t_composebuf[2 - id->t_composelen] = res;
1823 if (--id->t_composelen == 0) {
1824 res = wskbd_compose_value(id->t_composebuf);
1825 update_modifier(id, 0, 0, MOD_COMPOSE);
1826 } else {
1827 return (0);
1828 }
1829 }
1830
1831 update_leds(id);
1832
1833 /* We are done, return the symbol */
1834 if (KS_GROUP(res) == KS_GROUP_Ascii) {
1835 if (MOD_ONESET(id, MOD_ANYCONTROL)) {
1836 if ((res >= KS_at && res <= KS_z) || res == KS_space)
1837 res = res & 0x1f;
1838 else if (res == KS_2)
1839 res = 0x00;
1840 else if (res >= KS_3 && res <= KS_7)
1841 res = KS_Escape + (res - KS_3);
1842 else if (res == KS_8)
1843 res = KS_Delete;
1844 }
1845 if (MOD_ONESET(id, MOD_ANYMETA)) {
1846 if (id->t_flags & WSKFL_METAESC) {
1847 id->t_symbols[0] = KS_Escape;
1848 id->t_symbols[1] = res;
1849 return (2);
1850 } else
1851 res |= 0x80;
1852 }
1853 }
1854
1855 id->t_symbols[0] = res;
1856 return (1);
1857 }
1858