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