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