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