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