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