wskbd.c revision 1.112.6.6 1 /* $NetBSD: wskbd.c,v 1.112.6.6 2009/01/17 13:29:10 mjf 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.112.6.6 2009/01/17 13:29:10 mjf 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 int major;
417
418 sc->sc_base.me_dv = self;
419 sc->sc_isconsole = ap->console;
420 sc->sc_hotkey = NULL;
421 sc->sc_hotkeycookie = NULL;
422
423 #if NWSMUX > 0 || NWSDISPLAY > 0
424 sc->sc_base.me_ops = &wskbd_srcops;
425 #endif
426 #if NWSMUX > 0
427 mux = device_cfdata(sc->sc_base.me_dv)->wskbddevcf_mux;
428 if (ap->console) {
429 /* Ignore mux for console; it always goes to the console mux. */
430 /* printf(" (mux %d ignored for console)", mux); */
431 mux = -1;
432 }
433 if (mux >= 0)
434 aprint_normal(" mux %d", mux);
435 #else
436 if (device_cfdata(sc->sc_base.me_dv)->wskbddevcf_mux >= 0)
437 aprint_normal(" (mux ignored)");
438 #endif
439
440 if (ap->console) {
441 sc->id = &wskbd_console_data;
442 } else {
443 sc->id = malloc(sizeof(struct wskbd_internal),
444 M_DEVBUF, M_WAITOK|M_ZERO);
445 sc->id->t_keymap = ap->keymap;
446 wskbd_update_layout(sc->id, ap->keymap->layout);
447 }
448
449 callout_init(&sc->sc_repeat_ch, 0);
450 callout_setfunc(&sc->sc_repeat_ch, wskbd_repeat, sc);
451
452 sc->id->t_sc = sc;
453
454 sc->sc_accessops = ap->accessops;
455 sc->sc_accesscookie = ap->accesscookie;
456 sc->sc_repeating = 0;
457 sc->sc_translating = 1;
458 sc->sc_ledstate = -1; /* force update */
459
460 if (wskbd_load_keymap(sc->id->t_keymap,
461 &sc->sc_map, &sc->sc_maplen) != 0)
462 panic("cannot load keymap");
463
464 sc->sc_layout = sc->id->t_keymap->layout;
465
466 /* set default bell and key repeat data */
467 sc->sc_bell_data = wskbd_default_bell_data;
468 sc->sc_keyrepeat_data = wskbd_default_keyrepeat_data;
469
470 #ifdef WSDISPLAY_SCROLLSUPPORT
471 sc->sc_scroll_data = wskbd_default_scroll_data;
472 #endif
473
474 if (ap->console) {
475 KASSERT(wskbd_console_initted);
476 KASSERT(wskbd_console_device == NULL);
477
478 wskbd_console_device = sc;
479
480 aprint_naive(": console keyboard");
481 aprint_normal(": console keyboard");
482
483 #if NWSDISPLAY > 0
484 wsdisplay_set_console_kbd(&sc->sc_base); /* sets me_dispv */
485 if (sc->sc_base.me_dispdv != NULL)
486 aprint_normal(", using %s",
487 device_xname(sc->sc_base.me_dispdv));
488 #endif
489 }
490 aprint_naive("\n");
491 aprint_normal("\n");
492
493 #if NWSMUX > 0
494 if (mux >= 0) {
495 error = wsmux_attach_sc(wsmux_getmux(mux), &sc->sc_base);
496 if (error)
497 aprint_error_dev(sc->sc_base.me_dv,
498 "attach error=%d\n", error);
499 }
500 #endif
501
502 if (!pmf_device_register(self, wskbd_suspend, NULL))
503 aprint_error_dev(self, "couldn't establish power handler\n");
504 else if (!pmf_class_input_register(self))
505 aprint_error_dev(self, "couldn't register as input device\n");
506
507 major = cdevsw_lookup_major(&wskbd_cdevsw);
508 device_register_name(makedev(major, device_unit(self)), self,
509 true, DEV_OTHER, "%s", device_xname(self));
510 }
511
512 static bool
513 wskbd_suspend(device_t dv PMF_FN_ARGS)
514 {
515 struct wskbd_softc *sc = device_private(dv);
516
517 sc->sc_repeating = 0;
518 callout_stop(&sc->sc_repeat_ch);
519
520 return true;
521 }
522
523 void
524 wskbd_cnattach(const struct wskbd_consops *consops, void *conscookie,
525 const struct wskbd_mapdata *mapdata)
526 {
527 KASSERT(!wskbd_console_initted);
528
529 wskbd_console_data.t_keymap = mapdata;
530 wskbd_update_layout(&wskbd_console_data, mapdata->layout);
531
532 wskbd_console_data.t_consops = consops;
533 wskbd_console_data.t_consaccesscookie = conscookie;
534
535 #if NWSDISPLAY > 0
536 wsdisplay_set_cons_kbd(wskbd_cngetc, wskbd_cnpollc, wskbd_cnbell);
537 #endif
538
539 wskbd_console_initted = 1;
540 }
541
542 void
543 wskbd_cndetach(void)
544 {
545 KASSERT(wskbd_console_initted);
546
547 wskbd_console_data.t_keymap = 0;
548
549 wskbd_console_data.t_consops = 0;
550 wskbd_console_data.t_consaccesscookie = 0;
551
552 #if NWSDISPLAY > 0
553 wsdisplay_unset_cons_kbd();
554 #endif
555
556 wskbd_console_initted = 0;
557 }
558
559 static void
560 wskbd_repeat(void *v)
561 {
562 struct wskbd_softc *sc = (struct wskbd_softc *)v;
563 int s = spltty();
564
565 if (!sc->sc_repeating) {
566 /*
567 * race condition: a "key up" event came in when wskbd_repeat()
568 * was already called but not yet spltty()'d
569 */
570 splx(s);
571 return;
572 }
573 if (sc->sc_translating) {
574 /* deliver keys */
575 #if NWSDISPLAY > 0
576 if (sc->sc_base.me_dispdv != NULL) {
577 int i;
578 for (i = 0; i < sc->sc_repeating; i++)
579 wsdisplay_kbdinput(sc->sc_base.me_dispdv,
580 sc->id->t_symbols[i]);
581 }
582 #endif
583 } else {
584 #if defined(WSKBD_EVENT_AUTOREPEAT)
585 /* queue event */
586 wskbd_deliver_event(sc, sc->sc_repeat_type,
587 sc->sc_repeat_value);
588 #endif /* defined(WSKBD_EVENT_AUTOREPEAT) */
589 }
590 callout_schedule(&sc->sc_repeat_ch, mstohz(sc->sc_keyrepeat_data.delN));
591 splx(s);
592 }
593
594 int
595 wskbd_activate(device_t self, enum devact act)
596 {
597 struct wskbd_softc *sc = device_private(self);
598
599 if (act == DVACT_DEACTIVATE)
600 sc->sc_dying = 1;
601 return (0);
602 }
603
604 /*
605 * Detach a keyboard. To keep track of users of the softc we keep
606 * a reference count that's incremented while inside, e.g., read.
607 * If the keyboard is active and the reference count is > 0 (0 is the
608 * normal state) we post an event and then wait for the process
609 * that had the reference to wake us up again. Then we blow away the
610 * vnode and return (which will deallocate the softc).
611 */
612 int
613 wskbd_detach(device_t self, int flags)
614 {
615 struct wskbd_softc *sc = device_private(self);
616 struct wseventvar *evar;
617 int maj, mn;
618 int s;
619
620 #if NWSMUX > 0
621 /* Tell parent mux we're leaving. */
622 if (sc->sc_base.me_parent != NULL)
623 wsmux_detach_sc(&sc->sc_base);
624 #endif
625
626 callout_stop(&sc->sc_repeat_ch);
627 callout_destroy(&sc->sc_repeat_ch);
628
629 if (sc->sc_isconsole) {
630 KASSERT(wskbd_console_device == sc);
631 wskbd_console_device = NULL;
632 }
633
634 pmf_device_deregister(self);
635
636 evar = sc->sc_base.me_evp;
637 if (evar != NULL && evar->io != NULL) {
638 s = spltty();
639 if (--sc->sc_refcnt >= 0) {
640 struct wscons_event event;
641
642 /* Wake everyone by generating a dummy event. */
643 event.type = 0;
644 event.value = 0;
645 if (wsevent_inject(evar, &event, 1) != 0)
646 wsevent_wakeup(evar);
647
648 /* Wait for processes to go away. */
649 if (tsleep(sc, PZERO, "wskdet", hz * 60))
650 aprint_error("wskbd_detach: %s didn't detach\n",
651 device_xname(self));
652 }
653 splx(s);
654 }
655
656 device_deregister_all(self);
657
658 /* locate the major number */
659 maj = cdevsw_lookup_major(&wskbd_cdevsw);
660
661 /* Nuke the vnodes for any open instances. */
662 mn = device_unit(self);
663 vdevgone(maj, mn, mn, VCHR);
664
665 return (0);
666 }
667
668 void
669 wskbd_input(device_t dev, u_int type, int value)
670 {
671 struct wskbd_softc *sc = device_private(dev);
672 #if NWSDISPLAY > 0
673 int num, i;
674 #endif
675
676 if (sc->sc_repeating) {
677 sc->sc_repeating = 0;
678 callout_stop(&sc->sc_repeat_ch);
679 }
680
681 device_active(dev, DVA_HARDWARE);
682
683 #if NWSDISPLAY > 0
684 /*
685 * If /dev/wskbdN is not connected in event mode translate and
686 * send upstream.
687 */
688 if (sc->sc_translating) {
689 num = wskbd_translate(sc->id, type, value);
690 if (num > 0) {
691 if (sc->sc_base.me_dispdv != NULL) {
692 #ifdef WSDISPLAY_SCROLLSUPPORT
693 if (sc->id->t_symbols [0] != KS_Print_Screen) {
694 wsdisplay_scroll(sc->sc_base.
695 me_dispdv, WSDISPLAY_SCROLL_RESET);
696 }
697 #endif
698 for (i = 0; i < num; i++)
699 wsdisplay_kbdinput(
700 sc->sc_base.me_dispdv,
701 sc->id->t_symbols[i]);
702 }
703
704 if (sc->sc_keyrepeat_data.del1 != 0) {
705 sc->sc_repeating = num;
706 callout_schedule(&sc->sc_repeat_ch,
707 mstohz(sc->sc_keyrepeat_data.del1));
708 }
709 }
710 return;
711 }
712 #endif
713
714 wskbd_deliver_event(sc, type, value);
715
716 #if defined(WSKBD_EVENT_AUTOREPEAT)
717 /* Repeat key presses if set. */
718 if (type == WSCONS_EVENT_KEY_DOWN && sc->sc_keyrepeat_data.del1 != 0) {
719 sc->sc_repeat_type = type;
720 sc->sc_repeat_value = value;
721 sc->sc_repeating = 1;
722 callout_schedule(&sc->sc_repeat_ch,
723 mstohz(sc->sc_keyrepeat_data.del1));
724 }
725 #endif /* defined(WSKBD_EVENT_AUTOREPEAT) */
726 }
727
728 /*
729 * Keyboard is generating events. Turn this keystroke into an
730 * event and put it in the queue. If the queue is full, the
731 * keystroke is lost (sorry!).
732 */
733 static void
734 wskbd_deliver_event(struct wskbd_softc *sc, u_int type, int value)
735 {
736 struct wseventvar *evar;
737 struct wscons_event event;
738
739 evar = sc->sc_base.me_evp;
740
741 if (evar == NULL) {
742 DPRINTF(("wskbd_input: not open\n"));
743 return;
744 }
745
746 #ifdef DIAGNOSTIC
747 if (evar->q == NULL) {
748 printf("wskbd_input: evar->q=NULL\n");
749 return;
750 }
751 #endif
752
753 event.type = type;
754 event.value = value;
755 if (wsevent_inject(evar, &event, 1) != 0)
756 log(LOG_WARNING, "%s: event queue overflow\n",
757 device_xname(sc->sc_base.me_dv));
758 }
759
760 #ifdef WSDISPLAY_COMPAT_RAWKBD
761 void
762 wskbd_rawinput(device_t dev, u_char *tbuf, int len)
763 {
764 #if NWSDISPLAY > 0
765 struct wskbd_softc *sc = device_private(dev);
766 int i;
767
768 if (sc->sc_base.me_dispdv != NULL)
769 for (i = 0; i < len; i++)
770 wsdisplay_kbdinput(sc->sc_base.me_dispdv, tbuf[i]);
771 /* this is KS_GROUP_Ascii */
772 #endif
773 }
774 #endif /* WSDISPLAY_COMPAT_RAWKBD */
775
776 #if NWSDISPLAY > 0
777 static void
778 wskbd_holdscreen(struct wskbd_softc *sc, int hold)
779 {
780 int new_state;
781
782 if (sc->sc_base.me_dispdv != NULL) {
783 wsdisplay_kbdholdscreen(sc->sc_base.me_dispdv, hold);
784 new_state = sc->sc_ledstate;
785 if (hold) {
786 #ifdef WSDISPLAY_SCROLLSUPPORT
787 sc->sc_scroll_data.mode = WSKBD_SCROLL_MODE_HOLD;
788 #endif
789 new_state |= WSKBD_LED_SCROLL;
790 } else {
791 #ifdef WSDISPLAY_SCROLLSUPPORT
792 sc->sc_scroll_data.mode = WSKBD_SCROLL_MODE_NORMAL;
793 #endif
794 new_state &= ~WSKBD_LED_SCROLL;
795 }
796 if (new_state != sc->sc_ledstate) {
797 (*sc->sc_accessops->set_leds)(sc->sc_accesscookie,
798 new_state);
799 sc->sc_ledstate = new_state;
800 #ifdef WSDISPLAY_SCROLLSUPPORT
801 if (!hold)
802 wsdisplay_scroll(sc->sc_base.me_dispdv,
803 WSDISPLAY_SCROLL_RESET);
804 #endif
805 }
806 }
807 }
808 #endif
809
810 static int
811 wskbd_enable(struct wskbd_softc *sc, int on)
812 {
813 int error;
814
815 #if 0
816 /* I don't understand the purpose of this code. And it seems to
817 * break things, so it's out. -- Lennart
818 */
819 if (!on && (!sc->sc_translating
820 #if NWSDISPLAY > 0
821 || sc->sc_base.me_dispdv
822 #endif
823 ))
824 return (EBUSY);
825 #endif
826 #if NWSDISPLAY > 0
827 if (sc->sc_base.me_dispdv != NULL)
828 return (0);
829 #endif
830
831 /* Always cancel auto repeat when fiddling with the kbd. */
832 if (sc->sc_repeating) {
833 sc->sc_repeating = 0;
834 callout_stop(&sc->sc_repeat_ch);
835 }
836
837 error = (*sc->sc_accessops->enable)(sc->sc_accesscookie, on);
838 DPRINTF(("wskbd_enable: sc=%p on=%d res=%d\n", sc, on, error));
839 return (error);
840 }
841
842 #if NWSMUX > 0
843 int
844 wskbd_mux_open(struct wsevsrc *me, struct wseventvar *evp)
845 {
846 struct wskbd_softc *sc = (struct wskbd_softc *)me;
847
848 if (sc->sc_dying)
849 return (EIO);
850
851 if (sc->sc_base.me_evp != NULL)
852 return (EBUSY);
853
854 return (wskbd_do_open(sc, evp));
855 }
856 #endif
857
858 int
859 wskbdopen(dev_t dev, int flags, int mode, struct lwp *l)
860 {
861 struct wskbd_softc *sc = device_lookup_private(&wskbd_cd, minor(dev));
862 struct wseventvar *evar;
863 int error;
864
865 if (sc == NULL)
866 return (ENXIO);
867
868 #if NWSMUX > 0
869 DPRINTF(("wskbdopen: %s mux=%p l=%p\n",
870 device_xname(sc->sc_base.me_dv), sc->sc_base.me_parent, l));
871 #endif
872
873 if (sc->sc_dying)
874 return (EIO);
875
876 if ((flags & (FREAD | FWRITE)) == FWRITE)
877 /* Not opening for read, only ioctl is available. */
878 return (0);
879
880 #if NWSMUX > 0
881 if (sc->sc_base.me_parent != NULL) {
882 /* Grab the keyboard out of the greedy hands of the mux. */
883 DPRINTF(("wskbdopen: detach\n"));
884 wsmux_detach_sc(&sc->sc_base);
885 }
886 #endif
887
888 if (sc->sc_base.me_evp != NULL)
889 return (EBUSY);
890
891 evar = &sc->sc_base.me_evar;
892 wsevent_init(evar, l->l_proc);
893
894 error = wskbd_do_open(sc, evar);
895 if (error) {
896 DPRINTF(("wskbdopen: %s open failed\n",
897 device_xname(sc->sc_base.me_dv)));
898 sc->sc_base.me_evp = NULL;
899 wsevent_fini(evar);
900 }
901 return (error);
902 }
903
904 int
905 wskbd_do_open(struct wskbd_softc *sc, struct wseventvar *evp)
906 {
907 sc->sc_base.me_evp = evp;
908 sc->sc_translating = 0;
909
910 return (wskbd_enable(sc, 1));
911 }
912
913 int
914 wskbdclose(dev_t dev, int flags, int mode,
915 struct lwp *l)
916 {
917 struct wskbd_softc *sc =
918 device_lookup_private(&wskbd_cd, minor(dev));
919 struct wseventvar *evar = sc->sc_base.me_evp;
920
921 if (evar == NULL)
922 /* not open for read */
923 return (0);
924
925 sc->sc_base.me_evp = NULL;
926 sc->sc_translating = 1;
927 (void)wskbd_enable(sc, 0);
928 wsevent_fini(evar);
929
930 return (0);
931 }
932
933 #if NWSMUX > 0
934 int
935 wskbd_mux_close(struct wsevsrc *me)
936 {
937 struct wskbd_softc *sc = (struct wskbd_softc *)me;
938
939 sc->sc_base.me_evp = NULL;
940 sc->sc_translating = 1;
941 (void)wskbd_enable(sc, 0);
942
943 return (0);
944 }
945 #endif
946
947 int
948 wskbdread(dev_t dev, struct uio *uio, int flags)
949 {
950 struct wskbd_softc *sc =
951 device_lookup_private(&wskbd_cd, minor(dev));
952 int error;
953
954 if (sc->sc_dying)
955 return (EIO);
956
957 #ifdef DIAGNOSTIC
958 if (sc->sc_base.me_evp == NULL) {
959 printf("wskbdread: evp == NULL\n");
960 return (EINVAL);
961 }
962 #endif
963
964 sc->sc_refcnt++;
965 error = wsevent_read(sc->sc_base.me_evp, uio, flags);
966 if (--sc->sc_refcnt < 0) {
967 wakeup(sc);
968 error = EIO;
969 }
970 return (error);
971 }
972
973 int
974 wskbdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
975 {
976 return (wskbd_do_ioctl(device_lookup(&wskbd_cd, minor(dev)),
977 cmd, data, flag,l));
978 }
979
980 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
981 int
982 wskbd_do_ioctl(device_t dv, u_long cmd, void *data, int flag,
983 struct lwp *l)
984 {
985 struct wskbd_softc *sc = device_private(dv);
986 int error;
987
988 sc->sc_refcnt++;
989 error = wskbd_do_ioctl_sc(sc, cmd, data, flag, l);
990 if (--sc->sc_refcnt < 0)
991 wakeup(sc);
992 return (error);
993 }
994
995 int
996 wskbd_do_ioctl_sc(struct wskbd_softc *sc, u_long cmd, void *data, int flag,
997 struct lwp *l)
998 {
999
1000 /*
1001 * Try the generic ioctls that the wskbd interface supports.
1002 */
1003 switch (cmd) {
1004 case FIONBIO: /* we will remove this someday (soon???) */
1005 return (0);
1006
1007 case FIOASYNC:
1008 if (sc->sc_base.me_evp == NULL)
1009 return (EINVAL);
1010 sc->sc_base.me_evp->async = *(int *)data != 0;
1011 return (0);
1012
1013 case FIOSETOWN:
1014 if (sc->sc_base.me_evp == NULL)
1015 return (EINVAL);
1016 if (-*(int *)data != sc->sc_base.me_evp->io->p_pgid
1017 && *(int *)data != sc->sc_base.me_evp->io->p_pid)
1018 return (EPERM);
1019 return (0);
1020
1021 case TIOCSPGRP:
1022 if (sc->sc_base.me_evp == NULL)
1023 return (EINVAL);
1024 if (*(int *)data != sc->sc_base.me_evp->io->p_pgid)
1025 return (EPERM);
1026 return (0);
1027 }
1028
1029 /*
1030 * Try the keyboard driver for WSKBDIO ioctls. It returns EPASSTHROUGH
1031 * if it didn't recognize the request.
1032 */
1033 return (wskbd_displayioctl(sc->sc_base.me_dv, cmd, data, flag, l));
1034 }
1035
1036 /*
1037 * WSKBDIO ioctls, handled in both emulation mode and in ``raw'' mode.
1038 * Some of these have no real effect in raw mode, however.
1039 */
1040 static int
1041 wskbd_displayioctl(device_t dev, u_long cmd, void *data, int flag,
1042 struct lwp *l)
1043 {
1044 #ifdef WSDISPLAY_SCROLLSUPPORT
1045 struct wskbd_scroll_data *usdp, *ksdp;
1046 #endif
1047 struct wskbd_softc *sc = device_private(dev);
1048 struct wskbd_bell_data *ubdp, *kbdp;
1049 struct wskbd_keyrepeat_data *ukdp, *kkdp;
1050 struct wskbd_map_data *umdp;
1051 struct wskbd_mapdata md;
1052 struct proc *p = l ? l->l_proc : NULL;
1053 kbd_t enc;
1054 void *tbuf;
1055 int len, error;
1056
1057 switch (cmd) {
1058 #define SETBELL(dstp, srcp, dfltp) \
1059 do { \
1060 (dstp)->pitch = ((srcp)->which & WSKBD_BELL_DOPITCH) ? \
1061 (srcp)->pitch : (dfltp)->pitch; \
1062 (dstp)->period = ((srcp)->which & WSKBD_BELL_DOPERIOD) ? \
1063 (srcp)->period : (dfltp)->period; \
1064 (dstp)->volume = ((srcp)->which & WSKBD_BELL_DOVOLUME) ? \
1065 (srcp)->volume : (dfltp)->volume; \
1066 (dstp)->which = WSKBD_BELL_DOALL; \
1067 } while (0)
1068
1069 case WSKBDIO_BELL:
1070 if ((flag & FWRITE) == 0)
1071 return (EACCES);
1072 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
1073 WSKBDIO_COMPLEXBELL, (void *)&sc->sc_bell_data, flag, l));
1074
1075 case WSKBDIO_COMPLEXBELL:
1076 if ((flag & FWRITE) == 0)
1077 return (EACCES);
1078 ubdp = (struct wskbd_bell_data *)data;
1079 SETBELL(ubdp, ubdp, &sc->sc_bell_data);
1080 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
1081 WSKBDIO_COMPLEXBELL, (void *)ubdp, flag, l));
1082
1083 case WSKBDIO_SETBELL:
1084 if ((flag & FWRITE) == 0)
1085 return (EACCES);
1086 kbdp = &sc->sc_bell_data;
1087 setbell:
1088 ubdp = (struct wskbd_bell_data *)data;
1089 SETBELL(kbdp, ubdp, kbdp);
1090 return (0);
1091
1092 case WSKBDIO_GETBELL:
1093 kbdp = &sc->sc_bell_data;
1094 getbell:
1095 ubdp = (struct wskbd_bell_data *)data;
1096 SETBELL(ubdp, kbdp, kbdp);
1097 return (0);
1098
1099 case WSKBDIO_SETDEFAULTBELL:
1100 if (p && (error = kauth_authorize_generic(l->l_cred,
1101 KAUTH_GENERIC_ISSUSER, NULL)) != 0)
1102 return (error);
1103 kbdp = &wskbd_default_bell_data;
1104 goto setbell;
1105
1106
1107 case WSKBDIO_GETDEFAULTBELL:
1108 kbdp = &wskbd_default_bell_data;
1109 goto getbell;
1110
1111 #undef SETBELL
1112
1113 #define SETKEYREPEAT(dstp, srcp, dfltp) \
1114 do { \
1115 (dstp)->del1 = ((srcp)->which & WSKBD_KEYREPEAT_DODEL1) ? \
1116 (srcp)->del1 : (dfltp)->del1; \
1117 (dstp)->delN = ((srcp)->which & WSKBD_KEYREPEAT_DODELN) ? \
1118 (srcp)->delN : (dfltp)->delN; \
1119 (dstp)->which = WSKBD_KEYREPEAT_DOALL; \
1120 } while (0)
1121
1122 case WSKBDIO_SETKEYREPEAT:
1123 if ((flag & FWRITE) == 0)
1124 return (EACCES);
1125 kkdp = &sc->sc_keyrepeat_data;
1126 setkeyrepeat:
1127 ukdp = (struct wskbd_keyrepeat_data *)data;
1128 SETKEYREPEAT(kkdp, ukdp, kkdp);
1129 return (0);
1130
1131 case WSKBDIO_GETKEYREPEAT:
1132 kkdp = &sc->sc_keyrepeat_data;
1133 getkeyrepeat:
1134 ukdp = (struct wskbd_keyrepeat_data *)data;
1135 SETKEYREPEAT(ukdp, kkdp, kkdp);
1136 return (0);
1137
1138 case WSKBDIO_SETDEFAULTKEYREPEAT:
1139 if ((error = kauth_authorize_generic(l->l_cred,
1140 KAUTH_GENERIC_ISSUSER, NULL)) != 0)
1141 return (error);
1142 kkdp = &wskbd_default_keyrepeat_data;
1143 goto setkeyrepeat;
1144
1145
1146 case WSKBDIO_GETDEFAULTKEYREPEAT:
1147 kkdp = &wskbd_default_keyrepeat_data;
1148 goto getkeyrepeat;
1149
1150 #ifdef WSDISPLAY_SCROLLSUPPORT
1151 #define SETSCROLLMOD(dstp, srcp, dfltp) \
1152 do { \
1153 (dstp)->mode = ((srcp)->which & WSKBD_SCROLL_DOMODE) ? \
1154 (srcp)->mode : (dfltp)->mode; \
1155 (dstp)->modifier = ((srcp)->which & WSKBD_SCROLL_DOMODIFIER) ? \
1156 (srcp)->modifier : (dfltp)->modifier; \
1157 (dstp)->which = WSKBD_SCROLL_DOALL; \
1158 } while (0)
1159
1160 case WSKBDIO_SETSCROLL:
1161 usdp = (struct wskbd_scroll_data *)data;
1162 ksdp = &sc->sc_scroll_data;
1163 SETSCROLLMOD(ksdp, usdp, ksdp);
1164 return (0);
1165
1166 case WSKBDIO_GETSCROLL:
1167 usdp = (struct wskbd_scroll_data *)data;
1168 ksdp = &sc->sc_scroll_data;
1169 SETSCROLLMOD(usdp, ksdp, ksdp);
1170 return (0);
1171 #else
1172 case WSKBDIO_GETSCROLL:
1173 case WSKBDIO_SETSCROLL:
1174 return ENODEV;
1175 #endif
1176
1177 #undef SETKEYREPEAT
1178
1179 case WSKBDIO_SETMAP:
1180 if ((flag & FWRITE) == 0)
1181 return (EACCES);
1182 umdp = (struct wskbd_map_data *)data;
1183 if (umdp->maplen > WSKBDIO_MAXMAPLEN)
1184 return (EINVAL);
1185
1186 len = umdp->maplen*sizeof(struct wscons_keymap);
1187 tbuf = malloc(len, M_TEMP, M_WAITOK);
1188 error = copyin(umdp->map, tbuf, len);
1189 if (error == 0) {
1190 wskbd_init_keymap(umdp->maplen,
1191 &sc->sc_map, &sc->sc_maplen);
1192 memcpy(sc->sc_map, tbuf, len);
1193 /* drop the variant bits handled by the map */
1194 sc->sc_layout = KB_USER |
1195 (KB_VARIANT(sc->sc_layout) & KB_HANDLEDBYWSKBD);
1196 wskbd_update_layout(sc->id, sc->sc_layout);
1197 }
1198 free(tbuf, M_TEMP);
1199 return(error);
1200
1201 case WSKBDIO_GETMAP:
1202 umdp = (struct wskbd_map_data *)data;
1203 if (umdp->maplen > sc->sc_maplen)
1204 umdp->maplen = sc->sc_maplen;
1205 error = copyout(sc->sc_map, umdp->map,
1206 umdp->maplen*sizeof(struct wscons_keymap));
1207 return(error);
1208
1209 case WSKBDIO_GETENCODING:
1210 *((kbd_t *) data) = sc->sc_layout;
1211 return(0);
1212
1213 case WSKBDIO_SETENCODING:
1214 if ((flag & FWRITE) == 0)
1215 return (EACCES);
1216 enc = *((kbd_t *)data);
1217 if (KB_ENCODING(enc) == KB_USER) {
1218 /* user map must already be loaded */
1219 if (KB_ENCODING(sc->sc_layout) != KB_USER)
1220 return (EINVAL);
1221 /* map variants make no sense */
1222 if (KB_VARIANT(enc) & ~KB_HANDLEDBYWSKBD)
1223 return (EINVAL);
1224 } else {
1225 md = *(sc->id->t_keymap); /* structure assignment */
1226 md.layout = enc;
1227 error = wskbd_load_keymap(&md, &sc->sc_map,
1228 &sc->sc_maplen);
1229 if (error)
1230 return (error);
1231 }
1232 sc->sc_layout = enc;
1233 wskbd_update_layout(sc->id, enc);
1234 return (0);
1235
1236 case WSKBDIO_SETVERSION:
1237 return wsevent_setversion(sc->sc_base.me_evp, *(int *)data);
1238 }
1239
1240 /*
1241 * Try the keyboard driver for WSKBDIO ioctls. It returns -1
1242 * if it didn't recognize the request, and in turn we return
1243 * -1 if we didn't recognize the request.
1244 */
1245 /* printf("kbdaccess\n"); */
1246 error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
1247 flag, l);
1248 #ifdef WSDISPLAY_COMPAT_RAWKBD
1249 if (!error && cmd == WSKBDIO_SETMODE && *(int *)data == WSKBD_RAW) {
1250 int s = spltty();
1251 sc->id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
1252 | MOD_CONTROL_L | MOD_CONTROL_R
1253 | MOD_META_L | MOD_META_R
1254 | MOD_COMMAND
1255 | MOD_COMMAND1 | MOD_COMMAND2);
1256 if (sc->sc_repeating) {
1257 sc->sc_repeating = 0;
1258 callout_stop(&sc->sc_repeat_ch);
1259 }
1260 splx(s);
1261 }
1262 #endif
1263 return (error);
1264 }
1265
1266 int
1267 wskbdpoll(dev_t dev, int events, struct lwp *l)
1268 {
1269 struct wskbd_softc *sc =
1270 device_lookup_private(&wskbd_cd, minor(dev));
1271
1272 if (sc->sc_base.me_evp == NULL)
1273 return (POLLERR);
1274 return (wsevent_poll(sc->sc_base.me_evp, events, l));
1275 }
1276
1277 int
1278 wskbdkqfilter(dev_t dev, struct knote *kn)
1279 {
1280 struct wskbd_softc *sc =
1281 device_lookup_private(&wskbd_cd, minor(dev));
1282
1283 if (sc->sc_base.me_evp == NULL)
1284 return (1);
1285 return (wsevent_kqfilter(sc->sc_base.me_evp, kn));
1286 }
1287
1288 #if NWSDISPLAY > 0
1289
1290 int
1291 wskbd_pickfree(void)
1292 {
1293 int i;
1294 struct wskbd_softc *sc;
1295
1296 for (i = 0; i < wskbd_cd.cd_ndevs; i++) {
1297 sc = device_lookup_private(&wskbd_cd, i);
1298 if (sc == NULL)
1299 continue;
1300 if (sc->sc_base.me_dispdv == NULL)
1301 return (i);
1302 }
1303 return (-1);
1304 }
1305
1306 struct wsevsrc *
1307 wskbd_set_console_display(device_t displaydv, struct wsevsrc *me)
1308 {
1309 struct wskbd_softc *sc = wskbd_console_device;
1310
1311 if (sc == NULL)
1312 return (NULL);
1313 sc->sc_base.me_dispdv = displaydv;
1314 #if NWSMUX > 0
1315 (void)wsmux_attach_sc((struct wsmux_softc *)me, &sc->sc_base);
1316 #endif
1317 return (&sc->sc_base);
1318 }
1319
1320 int
1321 wskbd_set_display(device_t dv, struct wsevsrc *me)
1322 {
1323 struct wskbd_softc *sc = device_private(dv);
1324 device_t displaydv = me != NULL ? me->me_dispdv : NULL;
1325 device_t odisplaydv;
1326 int error;
1327
1328 DPRINTF(("wskbd_set_display: %s me=%p odisp=%p disp=%p cons=%d\n",
1329 device_xname(dv), me, sc->sc_base.me_dispdv, displaydv,
1330 sc->sc_isconsole));
1331
1332 if (sc->sc_isconsole)
1333 return (EBUSY);
1334
1335 if (displaydv != NULL) {
1336 if (sc->sc_base.me_dispdv != NULL)
1337 return (EBUSY);
1338 } else {
1339 if (sc->sc_base.me_dispdv == NULL)
1340 return (ENXIO);
1341 }
1342
1343 odisplaydv = sc->sc_base.me_dispdv;
1344 sc->sc_base.me_dispdv = NULL;
1345 error = wskbd_enable(sc, displaydv != NULL);
1346 sc->sc_base.me_dispdv = displaydv;
1347 if (error) {
1348 sc->sc_base.me_dispdv = odisplaydv;
1349 return (error);
1350 }
1351
1352 if (displaydv)
1353 aprint_verbose_dev(sc->sc_base.me_dv, "connecting to %s\n",
1354 device_xname(displaydv));
1355 else
1356 aprint_verbose_dev(sc->sc_base.me_dv, "disconnecting from %s\n",
1357 device_xname(odisplaydv));
1358
1359 return (0);
1360 }
1361
1362 #endif /* NWSDISPLAY > 0 */
1363
1364 #if NWSMUX > 0
1365 int
1366 wskbd_add_mux(int unit, struct wsmux_softc *muxsc)
1367 {
1368 struct wskbd_softc *sc = device_lookup_private(&wskbd_cd, unit);
1369
1370 if (sc == NULL)
1371 return (ENXIO);
1372
1373 if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
1374 return (EBUSY);
1375
1376 return (wsmux_attach_sc(muxsc, &sc->sc_base));
1377 }
1378 #endif
1379
1380 /*
1381 * Console interface.
1382 */
1383 int
1384 wskbd_cngetc(dev_t dev)
1385 {
1386 static int num = 0;
1387 static int pos;
1388 u_int type;
1389 int data;
1390 keysym_t ks;
1391
1392 if (!wskbd_console_initted)
1393 return 0;
1394
1395 if (wskbd_console_device != NULL &&
1396 !wskbd_console_device->sc_translating)
1397 return 0;
1398
1399 for(;;) {
1400 if (num-- > 0) {
1401 ks = wskbd_console_data.t_symbols[pos++];
1402 if (KS_GROUP(ks) == KS_GROUP_Ascii)
1403 return (KS_VALUE(ks));
1404 } else {
1405 (*wskbd_console_data.t_consops->getc)
1406 (wskbd_console_data.t_consaccesscookie,
1407 &type, &data);
1408 if (type == WSCONS_EVENT_ASCII) {
1409 /*
1410 * We assume that when the driver falls back
1411 * to deliver pure ASCII it is in a state that
1412 * it can not track press/release events
1413 * reliable - so we clear all previously
1414 * accumulated modifier state.
1415 */
1416 wskbd_console_data.t_modifiers = 0;
1417 return(data);
1418 }
1419 num = wskbd_translate(&wskbd_console_data, type, data);
1420 pos = 0;
1421 }
1422 }
1423 }
1424
1425 void
1426 wskbd_cnpollc(dev_t dev, int poll)
1427 {
1428
1429 if (!wskbd_console_initted)
1430 return;
1431
1432 if (wskbd_console_device != NULL &&
1433 !wskbd_console_device->sc_translating)
1434 return;
1435
1436 (*wskbd_console_data.t_consops->pollc)
1437 (wskbd_console_data.t_consaccesscookie, poll);
1438 }
1439
1440 void
1441 wskbd_cnbell(dev_t dev, u_int pitch, u_int period, u_int volume)
1442 {
1443
1444 if (!wskbd_console_initted)
1445 return;
1446
1447 if (wskbd_console_data.t_consops->bell != NULL)
1448 (*wskbd_console_data.t_consops->bell)
1449 (wskbd_console_data.t_consaccesscookie, pitch, period,
1450 volume);
1451 }
1452
1453 static inline void
1454 update_leds(struct wskbd_internal *id)
1455 {
1456 int new_state;
1457
1458 new_state = 0;
1459 if (id->t_modifiers & (MOD_SHIFTLOCK | MOD_CAPSLOCK))
1460 new_state |= WSKBD_LED_CAPS;
1461 if (id->t_modifiers & MOD_NUMLOCK)
1462 new_state |= WSKBD_LED_NUM;
1463 if (id->t_modifiers & MOD_COMPOSE)
1464 new_state |= WSKBD_LED_COMPOSE;
1465 if (id->t_modifiers & MOD_HOLDSCREEN)
1466 new_state |= WSKBD_LED_SCROLL;
1467
1468 if (id->t_sc && new_state != id->t_sc->sc_ledstate) {
1469 (*id->t_sc->sc_accessops->set_leds)
1470 (id->t_sc->sc_accesscookie, new_state);
1471 id->t_sc->sc_ledstate = new_state;
1472 }
1473 }
1474
1475 static inline void
1476 update_modifier(struct wskbd_internal *id, u_int type, int toggle, int mask)
1477 {
1478 if (toggle) {
1479 if (type == WSCONS_EVENT_KEY_DOWN)
1480 id->t_modifiers ^= mask;
1481 } else {
1482 if (type == WSCONS_EVENT_KEY_DOWN)
1483 id->t_modifiers |= mask;
1484 else
1485 id->t_modifiers &= ~mask;
1486 }
1487 }
1488
1489 #if NWSDISPLAY > 0
1490 static void
1491 change_displayparam(struct wskbd_softc *sc, int param, int updown,
1492 int wraparound)
1493 {
1494 int res;
1495 struct wsdisplay_param dp;
1496
1497 dp.param = param;
1498 res = wsdisplay_param(sc->sc_base.me_dispdv, WSDISPLAYIO_GETPARAM, &dp);
1499
1500 if (res == EINVAL)
1501 return; /* no such parameter */
1502
1503 dp.curval += updown;
1504 if (dp.max < dp.curval)
1505 dp.curval = wraparound ? dp.min : dp.max;
1506 else
1507 if (dp.curval < dp.min)
1508 dp.curval = wraparound ? dp.max : dp.min;
1509 wsdisplay_param(sc->sc_base.me_dispdv, WSDISPLAYIO_SETPARAM, &dp);
1510 }
1511 #endif
1512
1513 static int
1514 internal_command(struct wskbd_softc *sc, u_int *type, keysym_t ksym,
1515 keysym_t ksym2)
1516 {
1517 #ifdef WSDISPLAY_SCROLLSUPPORT
1518 u_int state = 0;
1519 #endif
1520 switch (ksym) {
1521 case KS_Cmd_VolumeToggle:
1522 if (*type == WSCONS_EVENT_KEY_DOWN)
1523 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_TOGGLE);
1524 break;
1525 case KS_Cmd_VolumeUp:
1526 if (*type == WSCONS_EVENT_KEY_DOWN)
1527 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
1528 break;
1529 case KS_Cmd_VolumeDown:
1530 if (*type == WSCONS_EVENT_KEY_DOWN)
1531 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
1532 break;
1533 #ifdef WSDISPLAY_SCROLLSUPPORT
1534 case KS_Cmd_ScrollFastUp:
1535 case KS_Cmd_ScrollFastDown:
1536 if (*type == WSCONS_EVENT_KEY_DOWN) {
1537 GETMODSTATE(sc->id->t_modifiers, state);
1538 if ((sc->sc_scroll_data.mode == WSKBD_SCROLL_MODE_HOLD
1539 && MOD_ONESET(sc->id, MOD_HOLDSCREEN))
1540 || (sc->sc_scroll_data.mode == WSKBD_SCROLL_MODE_NORMAL
1541 && sc->sc_scroll_data.modifier == state)) {
1542 update_modifier(sc->id, *type, 0, MOD_COMMAND);
1543 wsdisplay_scroll(sc->sc_base.me_dispdv,
1544 (ksym == KS_Cmd_ScrollFastUp) ?
1545 WSDISPLAY_SCROLL_BACKWARD :
1546 WSDISPLAY_SCROLL_FORWARD);
1547 return (1);
1548 } else {
1549 return (0);
1550 }
1551 }
1552
1553 case KS_Cmd_ScrollSlowUp:
1554 case KS_Cmd_ScrollSlowDown:
1555 if (*type == WSCONS_EVENT_KEY_DOWN) {
1556 GETMODSTATE(sc->id->t_modifiers, state);
1557 if ((sc->sc_scroll_data.mode == WSKBD_SCROLL_MODE_HOLD
1558 && MOD_ONESET(sc->id, MOD_HOLDSCREEN))
1559 || (sc->sc_scroll_data.mode == WSKBD_SCROLL_MODE_NORMAL
1560 && sc->sc_scroll_data.modifier == state)) {
1561 update_modifier(sc->id, *type, 0, MOD_COMMAND);
1562 wsdisplay_scroll(sc->sc_base.me_dispdv,
1563 (ksym == KS_Cmd_ScrollSlowUp) ?
1564 WSDISPLAY_SCROLL_BACKWARD | WSDISPLAY_SCROLL_LOW:
1565 WSDISPLAY_SCROLL_FORWARD | WSDISPLAY_SCROLL_LOW);
1566 return (1);
1567 } else {
1568 return (0);
1569 }
1570 }
1571 #endif
1572
1573 case KS_Cmd:
1574 update_modifier(sc->id, *type, 0, MOD_COMMAND);
1575 ksym = ksym2;
1576 break;
1577
1578 case KS_Cmd1:
1579 update_modifier(sc->id, *type, 0, MOD_COMMAND1);
1580 break;
1581
1582 case KS_Cmd2:
1583 update_modifier(sc->id, *type, 0, MOD_COMMAND2);
1584 break;
1585 }
1586
1587 if (*type != WSCONS_EVENT_KEY_DOWN ||
1588 (! MOD_ONESET(sc->id, MOD_COMMAND) &&
1589 ! MOD_ALLSET(sc->id, MOD_COMMAND1 | MOD_COMMAND2)))
1590 return (0);
1591
1592 #if defined(DDB) || defined(KGDB)
1593 if (ksym == KS_Cmd_Debugger) {
1594 if (sc->sc_isconsole) {
1595 #ifdef DDB
1596 console_debugger();
1597 #endif
1598 #ifdef KGDB
1599 kgdb_connect(1);
1600 #endif
1601 }
1602 /* discard this key (ddb discarded command modifiers) */
1603 *type = WSCONS_EVENT_KEY_UP;
1604 return (1);
1605 }
1606 #endif
1607
1608 #if NWSDISPLAY > 0
1609 if (sc->sc_base.me_dispdv == NULL)
1610 return (0);
1611
1612 switch (ksym) {
1613 case KS_Cmd_Screen0:
1614 case KS_Cmd_Screen1:
1615 case KS_Cmd_Screen2:
1616 case KS_Cmd_Screen3:
1617 case KS_Cmd_Screen4:
1618 case KS_Cmd_Screen5:
1619 case KS_Cmd_Screen6:
1620 case KS_Cmd_Screen7:
1621 case KS_Cmd_Screen8:
1622 case KS_Cmd_Screen9:
1623 wsdisplay_switch(sc->sc_base.me_dispdv, ksym - KS_Cmd_Screen0, 0);
1624 return (1);
1625 case KS_Cmd_ResetEmul:
1626 wsdisplay_reset(sc->sc_base.me_dispdv, WSDISPLAY_RESETEMUL);
1627 return (1);
1628 case KS_Cmd_ResetClose:
1629 wsdisplay_reset(sc->sc_base.me_dispdv, WSDISPLAY_RESETCLOSE);
1630 return (1);
1631 case KS_Cmd_BacklightOn:
1632 case KS_Cmd_BacklightOff:
1633 case KS_Cmd_BacklightToggle:
1634 change_displayparam(sc, WSDISPLAYIO_PARAM_BACKLIGHT,
1635 ksym == KS_Cmd_BacklightOff ? -1 : 1,
1636 ksym == KS_Cmd_BacklightToggle ? 1 : 0);
1637 return (1);
1638 case KS_Cmd_BrightnessUp:
1639 case KS_Cmd_BrightnessDown:
1640 case KS_Cmd_BrightnessRotate:
1641 change_displayparam(sc, WSDISPLAYIO_PARAM_BRIGHTNESS,
1642 ksym == KS_Cmd_BrightnessDown ? -1 : 1,
1643 ksym == KS_Cmd_BrightnessRotate ? 1 : 0);
1644 return (1);
1645 case KS_Cmd_ContrastUp:
1646 case KS_Cmd_ContrastDown:
1647 case KS_Cmd_ContrastRotate:
1648 change_displayparam(sc, WSDISPLAYIO_PARAM_CONTRAST,
1649 ksym == KS_Cmd_ContrastDown ? -1 : 1,
1650 ksym == KS_Cmd_ContrastRotate ? 1 : 0);
1651 return (1);
1652 }
1653 #endif
1654
1655 return (0);
1656 }
1657
1658 device_t
1659 wskbd_hotkey_register(device_t self, void *cookie, wskbd_hotkey_plugin *hotkey)
1660 {
1661 struct wskbd_softc *sc = device_private(self);
1662
1663 sc->sc_hotkey = hotkey;
1664 sc->sc_hotkeycookie = cookie;
1665 return sc->sc_base.me_dv;
1666 }
1667
1668 static int
1669 wskbd_translate(struct wskbd_internal *id, u_int type, int value)
1670 {
1671 struct wskbd_softc *sc = id->t_sc;
1672 keysym_t ksym, res, *group;
1673 struct wscons_keymap kpbuf, *kp;
1674 int iscommand = 0;
1675 int ishotkey = 0;
1676
1677 if (type == WSCONS_EVENT_ALL_KEYS_UP) {
1678 id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
1679 | MOD_CONTROL_L | MOD_CONTROL_R
1680 | MOD_META_L | MOD_META_R
1681 | MOD_MODESHIFT
1682 | MOD_COMMAND | MOD_COMMAND1 | MOD_COMMAND2);
1683 update_leds(id);
1684 return (0);
1685 }
1686
1687 if (sc != NULL) {
1688 if (sc->sc_hotkey != NULL)
1689 ishotkey = sc->sc_hotkey(sc, sc->sc_hotkeycookie,
1690 type, value);
1691 if (ishotkey)
1692 return 0;
1693
1694 if (value < 0 || value >= sc->sc_maplen) {
1695 #ifdef DEBUG
1696 printf("%s: keycode %d out of range\n",
1697 __func__, value);
1698 #endif
1699 return (0);
1700 }
1701 kp = sc->sc_map + value;
1702 } else {
1703 kp = &kpbuf;
1704 wskbd_get_mapentry(id->t_keymap, value, kp);
1705 }
1706
1707 /* if this key has a command, process it first */
1708 if (sc != NULL && kp->command != KS_voidSymbol)
1709 iscommand = internal_command(sc, &type, kp->command,
1710 kp->group1[0]);
1711
1712 /* Now update modifiers */
1713 switch (kp->group1[0]) {
1714 case KS_Shift_L:
1715 update_modifier(id, type, 0, MOD_SHIFT_L);
1716 break;
1717
1718 case KS_Shift_R:
1719 update_modifier(id, type, 0, MOD_SHIFT_R);
1720 break;
1721
1722 case KS_Shift_Lock:
1723 update_modifier(id, type, 1, MOD_SHIFTLOCK);
1724 break;
1725
1726 case KS_Caps_Lock:
1727 update_modifier(id, type, 1, MOD_CAPSLOCK);
1728 break;
1729
1730 case KS_Control_L:
1731 update_modifier(id, type, 0, MOD_CONTROL_L);
1732 break;
1733
1734 case KS_Control_R:
1735 update_modifier(id, type, 0, MOD_CONTROL_R);
1736 break;
1737
1738 case KS_Alt_L:
1739 update_modifier(id, type, 0, MOD_META_L);
1740 break;
1741
1742 case KS_Alt_R:
1743 update_modifier(id, type, 0, MOD_META_R);
1744 break;
1745
1746 case KS_Mode_switch:
1747 update_modifier(id, type, 0, MOD_MODESHIFT);
1748 break;
1749
1750 case KS_Num_Lock:
1751 update_modifier(id, type, 1, MOD_NUMLOCK);
1752 break;
1753
1754 #if NWSDISPLAY > 0
1755 case KS_Hold_Screen:
1756 if (sc != NULL) {
1757 update_modifier(id, type, 1, MOD_HOLDSCREEN);
1758 wskbd_holdscreen(sc, id->t_modifiers & MOD_HOLDSCREEN);
1759 }
1760 break;
1761 #endif
1762 }
1763
1764 /* If this is a key release or we are in command mode, we are done */
1765 if (type != WSCONS_EVENT_KEY_DOWN || iscommand) {
1766 update_leds(id);
1767 return (0);
1768 }
1769
1770 /* Get the keysym */
1771 if (id->t_modifiers & MOD_MODESHIFT)
1772 group = & kp->group2[0];
1773 else
1774 group = & kp->group1[0];
1775
1776 if ((id->t_modifiers & MOD_NUMLOCK) != 0 &&
1777 KS_GROUP(group[1]) == KS_GROUP_Keypad) {
1778 if (MOD_ONESET(id, MOD_ANYSHIFT))
1779 ksym = group[0];
1780 else
1781 ksym = group[1];
1782 } else if (! MOD_ONESET(id, MOD_ANYSHIFT | MOD_CAPSLOCK)) {
1783 ksym = group[0];
1784 } else if (MOD_ONESET(id, MOD_CAPSLOCK)) {
1785 if (! MOD_ONESET(id, MOD_SHIFT_L | MOD_SHIFT_R))
1786 ksym = group[0];
1787 else
1788 ksym = group[1];
1789 if (ksym >= KS_a && ksym <= KS_z)
1790 ksym += KS_A - KS_a;
1791 else if (ksym >= KS_agrave && ksym <= KS_thorn &&
1792 ksym != KS_division)
1793 ksym += KS_Agrave - KS_agrave;
1794 } else if (MOD_ONESET(id, MOD_ANYSHIFT)) {
1795 ksym = group[1];
1796 } else {
1797 ksym = group[0];
1798 }
1799
1800 /* Process compose sequence and dead accents */
1801 res = KS_voidSymbol;
1802
1803 switch (KS_GROUP(ksym)) {
1804 case KS_GROUP_Ascii:
1805 case KS_GROUP_Keypad:
1806 case KS_GROUP_Function:
1807 res = ksym;
1808 break;
1809
1810 case KS_GROUP_Mod:
1811 if (ksym == KS_Multi_key) {
1812 update_modifier(id, 1, 0, MOD_COMPOSE);
1813 id->t_composelen = 2;
1814 }
1815 break;
1816
1817 case KS_GROUP_Dead:
1818 if (id->t_composelen == 0) {
1819 update_modifier(id, 1, 0, MOD_COMPOSE);
1820 id->t_composelen = 1;
1821 id->t_composebuf[0] = ksym;
1822 } else
1823 res = ksym;
1824 break;
1825 }
1826
1827 if (res == KS_voidSymbol) {
1828 update_leds(id);
1829 return (0);
1830 }
1831
1832 if (id->t_composelen > 0) {
1833 id->t_composebuf[2 - id->t_composelen] = res;
1834 if (--id->t_composelen == 0) {
1835 res = wskbd_compose_value(id->t_composebuf);
1836 update_modifier(id, 0, 0, MOD_COMPOSE);
1837 } else {
1838 return (0);
1839 }
1840 }
1841
1842 update_leds(id);
1843
1844 /* We are done, return the symbol */
1845 if (KS_GROUP(res) == KS_GROUP_Ascii) {
1846 if (MOD_ONESET(id, MOD_ANYCONTROL)) {
1847 if ((res >= KS_at && res <= KS_z) || res == KS_space)
1848 res = res & 0x1f;
1849 else if (res == KS_2)
1850 res = 0x00;
1851 else if (res >= KS_3 && res <= KS_7)
1852 res = KS_Escape + (res - KS_3);
1853 else if (res == KS_8)
1854 res = KS_Delete;
1855 }
1856 if (MOD_ONESET(id, MOD_ANYMETA)) {
1857 if (id->t_flags & WSKFL_METAESC) {
1858 id->t_symbols[0] = KS_Escape;
1859 id->t_symbols[1] = res;
1860 return (2);
1861 } else
1862 res |= 0x80;
1863 }
1864 }
1865
1866 id->t_symbols[0] = res;
1867 return (1);
1868 }
1869