wskbd.c revision 1.24 1 /* $NetBSD: wskbd.c,v 1.24 1999/06/30 06:21:21 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 static const char _copyright[] __attribute__ ((unused)) =
37 "Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.";
38 static const char _rcsid[] __attribute__ ((unused)) =
39 "$NetBSD: wskbd.c,v 1.24 1999/06/30 06:21:21 augustss Exp $";
40
41 /*
42 * Copyright (c) 1992, 1993
43 * The Regents of the University of California. All rights reserved.
44 *
45 * This software was developed by the Computer Systems Engineering group
46 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
47 * contributed to Berkeley.
48 *
49 * All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the University of
52 * California, Lawrence Berkeley Laboratory.
53 *
54 * Redistribution and use in source and binary forms, with or without
55 * modification, are permitted provided that the following conditions
56 * are met:
57 * 1. Redistributions of source code must retain the above copyright
58 * notice, this list of conditions and the following disclaimer.
59 * 2. Redistributions in binary form must reproduce the above copyright
60 * notice, this list of conditions and the following disclaimer in the
61 * documentation and/or other materials provided with the distribution.
62 * 3. All advertising materials mentioning features or use of this software
63 * must display the following acknowledgement:
64 * This product includes software developed by the University of
65 * California, Berkeley and its contributors.
66 * 4. Neither the name of the University nor the names of its contributors
67 * may be used to endorse or promote products derived from this software
68 * without specific prior written permission.
69 *
70 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
71 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
72 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
73 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
74 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
75 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
76 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
77 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
78 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
79 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
80 * SUCH DAMAGE.
81 *
82 * @(#)kbd.c 8.2 (Berkeley) 10/30/93
83 */
84
85 /*
86 * Keyboard driver (/dev/wskbd*). Translates incoming bytes to ASCII or
87 * to `wscons_events' and passes them up to the appropriate reader.
88 */
89
90 #include "opt_ddb.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/malloc.h>
101 #include <sys/tty.h>
102 #include <sys/signalvar.h>
103 #include <sys/errno.h>
104 #include <sys/fcntl.h>
105 #include <sys/vnode.h>
106
107 #include <dev/wscons/wsconsio.h>
108 #include <dev/wscons/wskbdvar.h>
109 #include <dev/wscons/wsksymdef.h>
110 #include <dev/wscons/wsksymvar.h>
111 #include <dev/wscons/wseventvar.h>
112 #include <dev/wscons/wscons_callbacks.h>
113
114 #include "opt_wsdisplay_compat.h"
115
116 #include "wsdisplay.h"
117
118 struct wskbd_internal {
119 const struct wskbd_mapdata *t_keymap;
120
121 const struct wskbd_consops *t_consops;
122 void *t_consaccesscookie;
123
124 int t_modifiers;
125 int t_composelen; /* remaining entries in t_composebuf */
126 keysym_t t_composebuf[2];
127
128 struct wskbd_softc *t_sc; /* back pointer */
129 };
130
131 struct wskbd_softc {
132 struct device sc_dv;
133
134 struct wskbd_internal *id;
135
136 const struct wskbd_accessops *sc_accessops;
137 void *sc_accesscookie;
138
139 int sc_ledstate;
140
141 int sc_ready; /* accepting events */
142 struct wseventvar sc_events; /* event queue state */
143
144 int sc_isconsole;
145 #if NWSDISPLAY > 0
146 struct device *sc_displaydv;
147 #endif
148
149 struct wskbd_bell_data sc_bell_data;
150 struct wskbd_keyrepeat_data sc_keyrepeat_data;
151
152 int sc_repeating; /* we've called timeout() */
153 keysym_t sc_repeatsym; /* repeated symbol */
154
155 int sc_translating; /* xlate to chars for emulation */
156
157 int sc_maplen; /* number of entries in sc_map */
158 struct wscons_keymap *sc_map; /* current translation map */
159 kbd_t sc_layout; /* current layout */
160
161 int sc_refcnt;
162 u_char sc_dying; /* device is being detached */
163
164 };
165
166 #define MOD_SHIFT_L (1 << 0)
167 #define MOD_SHIFT_R (1 << 1)
168 #define MOD_SHIFTLOCK (1 << 2)
169 #define MOD_CAPSLOCK (1 << 3)
170 #define MOD_CONTROL_L (1 << 4)
171 #define MOD_CONTROL_R (1 << 5)
172 #define MOD_META_L (1 << 6)
173 #define MOD_META_R (1 << 7)
174 #define MOD_MODESHIFT (1 << 8)
175 #define MOD_NUMLOCK (1 << 9)
176 #define MOD_COMPOSE (1 << 10)
177 #define MOD_HOLDSCREEN (1 << 11)
178 #define MOD_COMMAND (1 << 12)
179 #define MOD_COMMAND1 (1 << 13)
180 #define MOD_COMMAND2 (1 << 14)
181
182 #define MOD_ANYSHIFT (MOD_SHIFT_L | MOD_SHIFT_R | MOD_SHIFTLOCK)
183 #define MOD_ANYCONTROL (MOD_CONTROL_L | MOD_CONTROL_R)
184 #define MOD_ANYMETA (MOD_META_L | MOD_META_R)
185
186 #define MOD_ONESET(id, mask) (((id)->t_modifiers & (mask)) != 0)
187 #define MOD_ALLSET(id, mask) (((id)->t_modifiers & (mask)) == (mask))
188
189 int wskbd_match __P((struct device *, struct cfdata *, void *));
190 void wskbd_attach __P((struct device *, struct device *, void *));
191 int wskbd_detach __P((struct device *, int));
192 int wskbd_activate __P((struct device *, enum devact));
193
194 static inline void update_leds __P((struct wskbd_internal *));
195 static inline void update_modifier __P((struct wskbd_internal *, u_int, int, int));
196 static int internal_command __P((struct wskbd_softc *, u_int *, keysym_t));
197 static keysym_t wskbd_translate __P((struct wskbd_internal *, u_int, int));
198 static int wskbd_enable __P((struct wskbd_softc *, int));
199 #if NWSDISPLAY > 0
200 static void wskbd_holdscreen __P((struct wskbd_softc *, int));
201 #endif
202
203 int wskbd_do_ioctl __P((struct wskbd_softc *, u_long, caddr_t,
204 int, struct proc *));
205
206 struct cfattach wskbd_ca = {
207 sizeof (struct wskbd_softc), wskbd_match, wskbd_attach,
208 };
209
210 extern struct cfdriver wskbd_cd;
211
212 #ifndef WSKBD_DEFAULT_BELL_PITCH
213 #define WSKBD_DEFAULT_BELL_PITCH 1500 /* 1500Hz */
214 #endif
215 #ifndef WSKBD_DEFAULT_BELL_PERIOD
216 #define WSKBD_DEFAULT_BELL_PERIOD 100 /* 100ms */
217 #endif
218 #ifndef WSKBD_DEFAULT_BELL_VOLUME
219 #define WSKBD_DEFAULT_BELL_VOLUME 50 /* 50% volume */
220 #endif
221
222 struct wskbd_bell_data wskbd_default_bell_data = {
223 WSKBD_BELL_DOALL,
224 WSKBD_DEFAULT_BELL_PITCH,
225 WSKBD_DEFAULT_BELL_PERIOD,
226 WSKBD_DEFAULT_BELL_VOLUME,
227 };
228
229 #ifndef WSKBD_DEFAULT_KEYREPEAT_DEL1
230 #define WSKBD_DEFAULT_KEYREPEAT_DEL1 400 /* 400ms to start repeating */
231 #endif
232 #ifndef WSKBD_DEFAULT_KEYREPEAT_DELN
233 #define WSKBD_DEFAULT_KEYREPEAT_DELN 100 /* 100ms to between repeats */
234 #endif
235
236 struct wskbd_keyrepeat_data wskbd_default_keyrepeat_data = {
237 WSKBD_KEYREPEAT_DOALL,
238 WSKBD_DEFAULT_KEYREPEAT_DEL1,
239 WSKBD_DEFAULT_KEYREPEAT_DELN,
240 };
241
242 cdev_decl(wskbd);
243
244 #if NWSDISPLAY > 0
245 static void wskbd_repeat __P((void *v));
246 #endif
247
248 static int wskbd_console_initted;
249 static struct wskbd_softc *wskbd_console_device;
250 static struct wskbd_internal wskbd_console_data;
251
252 /*
253 * Print function (for parent devices).
254 */
255 int
256 wskbddevprint(aux, pnp)
257 void *aux;
258 const char *pnp;
259 {
260 #if 0
261 struct wskbddev_attach_args *ap = aux;
262 #endif
263
264 if (pnp)
265 printf("wskbd at %s", pnp);
266 #if 0
267 printf(" console %d", ap->console);
268 #endif
269
270 return (UNCONF);
271 }
272
273 int
274 wskbd_match(parent, match, aux)
275 struct device *parent;
276 struct cfdata *match;
277 void *aux;
278 {
279 struct wskbddev_attach_args *ap = aux;
280
281 if (match->wskbddevcf_console != WSKBDDEVCF_CONSOLE_UNK) {
282 /*
283 * If console-ness of device specified, either match
284 * exactly (at high priority), or fail.
285 */
286 if (match->wskbddevcf_console != 0 && ap->console != 0)
287 return (10);
288 else
289 return (0);
290 }
291
292 /* If console-ness unspecified, it wins. */
293 return (1);
294 }
295
296 void
297 wskbd_attach(parent, self, aux)
298 struct device *parent, *self;
299 void *aux;
300 {
301 struct wskbd_softc *sc = (struct wskbd_softc *)self;
302 struct wskbddev_attach_args *ap = aux;
303
304 #if NWSDISPLAY > 0
305 sc->sc_displaydv = NULL;
306 #endif
307 sc->sc_isconsole = ap->console;
308
309 if (ap->console) {
310 KASSERT(wskbd_console_initted);
311 KASSERT(wskbd_console_device == NULL);
312
313 wskbd_console_device = sc;
314
315 printf(": console keyboard");
316
317 #if NWSDISPLAY > 0
318 if ((sc->sc_displaydv = wsdisplay_set_console_kbd(self)))
319 printf(", using %s", sc->sc_displaydv->dv_xname);
320 #endif
321 }
322 printf("\n");
323
324 if (ap->console) {
325 sc->id = &wskbd_console_data;
326 } else {
327 sc->id = malloc(sizeof(struct wskbd_internal),
328 M_DEVBUF, M_WAITOK);
329 sc->id->t_keymap = ap->keymap;
330 sc->id->t_modifiers = 0;
331 }
332
333 sc->id->t_sc = sc;
334
335 sc->sc_accessops = ap->accessops;
336 sc->sc_accesscookie = ap->accesscookie;
337 sc->sc_ready = 0; /* sanity */
338 sc->sc_repeating = 0;
339 sc->sc_translating = 1;
340 sc->sc_ledstate = -1; /* force update */
341
342 if (wskbd_load_keymap(sc->id->t_keymap,
343 &sc->sc_map, &sc->sc_maplen) != 0)
344 panic("cannot load keymap");
345
346 sc->sc_layout = sc->id->t_keymap->layout;
347
348 /* set default bell and key repeat data */
349 sc->sc_bell_data = wskbd_default_bell_data;
350 sc->sc_keyrepeat_data = wskbd_default_keyrepeat_data;
351 }
352
353 void
354 wskbd_cnattach(consops, conscookie, mapdata)
355 const struct wskbd_consops *consops;
356 void *conscookie;
357 const struct wskbd_mapdata *mapdata;
358 {
359
360 KASSERT(!wskbd_console_initted);
361
362 wskbd_console_data.t_keymap = mapdata;
363
364 wskbd_console_data.t_consops = consops;
365 wskbd_console_data.t_consaccesscookie = conscookie;
366
367 #if NWSDISPLAY > 0
368 wsdisplay_set_cons_kbd(wskbd_cngetc, wskbd_cnpollc);
369 #endif
370
371 wskbd_console_initted = 1;
372 }
373
374 #if NWSDISPLAY > 0
375 static void
376 wskbd_repeat(v)
377 void *v;
378 {
379 struct wskbd_softc *sc = (struct wskbd_softc *)v;
380 int s = spltty();
381
382 if (!sc->sc_repeating) {
383 /*
384 * race condition: a "key up" event came in when wskbd_repeat()
385 * was already called but not yet spltty()'d
386 */
387 splx(s);
388 return;
389 }
390 if (sc->sc_displaydv != NULL)
391 wsdisplay_kbdinput(sc->sc_displaydv, sc->sc_repeatsym);
392 timeout(wskbd_repeat, sc,
393 (hz * sc->sc_keyrepeat_data.delN) / 1000);
394 splx(s);
395 }
396 #endif
397
398 int
399 wskbd_activate(self, act)
400 struct device *self;
401 enum devact act;
402 {
403 /* XXX should we do something more? */
404 return (0);
405 }
406
407 /*
408 * Detach a keyboard. To keep track of users of the softc we keep
409 * a reference count that's incremented while inside, e.g., read.
410 * If the mouse is active and the reference count is > 0 (0 is the
411 * normal state) we post an event and then wait for the process
412 * that had the reference to wake us up again. Then we blow away the
413 * vnode and return (which will deallocate the softc).
414 * XXX what should we do if we are connected to a display?
415 */
416 int
417 wskbd_detach(self, flags)
418 struct device *self;
419 int flags;
420 {
421 struct wskbd_softc *sc = (struct wskbd_softc *)self;
422 struct wseventvar *evar;
423 int maj, mn;
424 int s;
425
426 evar = &sc->sc_events;
427 if (evar->io) {
428 s = spltty();
429 if (--sc->sc_refcnt >= 0) {
430 /* Wake everyone by generating a dummy event. */
431 if (++evar->put >= WSEVENT_QSIZE)
432 evar->put = 0;
433 WSEVENT_WAKEUP(evar);
434 /* Wait for processes to go away. */
435 if (tsleep(sc, PZERO, "wskdet", hz * 60))
436 printf("wskbd_detach: %s didn't detach\n",
437 sc->sc_dv.dv_xname);
438 }
439 splx(s);
440 }
441
442 /* locate the major number */
443 for (maj = 0; maj < nchrdev; maj++)
444 if (cdevsw[maj].d_open == wskbdopen)
445 break;
446
447 /* Nuke the vnodes for any open instances. */
448 mn = self->dv_unit;
449 vdevgone(maj, mn, mn, VCHR);
450
451 return (0);
452 }
453
454 void
455 wskbd_input(dev, type, value)
456 struct device *dev;
457 u_int type;
458 int value;
459 {
460 struct wskbd_softc *sc = (struct wskbd_softc *)dev;
461 struct wscons_event *ev;
462 struct wseventvar *evar;
463 struct timeval xxxtime;
464 #if NWSDISPLAY > 0
465 keysym_t ks;
466 #endif
467 int put;
468
469 #if NWSDISPLAY > 0
470 if (sc->sc_repeating) {
471 sc->sc_repeating = 0;
472 untimeout(wskbd_repeat, sc);
473 }
474
475 /*
476 * If /dev/wskbd is not connected in event mode translate and
477 * send upstream.
478 */
479 if (sc->sc_translating) {
480 ks = wskbd_translate(sc->id, type, value);
481 if (ks != KS_voidSymbol) {
482 sc->sc_repeatsym = ks;
483 if (sc->sc_displaydv != NULL)
484 wsdisplay_kbdinput(sc->sc_displaydv,
485 sc->sc_repeatsym);
486
487 sc->sc_repeating = 1;
488 timeout(wskbd_repeat, sc,
489 (hz * sc->sc_keyrepeat_data.del1) / 1000);
490 }
491 return;
492 }
493 #endif
494
495 /*
496 * Keyboard is generating events. Turn this keystroke into an
497 * event and put it in the queue. If the queue is full, the
498 * keystroke is lost (sorry!).
499 */
500
501 /* no one to receive; punt!*/
502 if (!sc->sc_ready)
503 return;
504
505 evar = &sc->sc_events;
506
507 put = evar->put;
508 ev = &evar->q[put];
509 put = (put + 1) % WSEVENT_QSIZE;
510 if (put == evar->get) {
511 log(LOG_WARNING, "%s: event queue overflow\n",
512 sc->sc_dv.dv_xname);
513 return;
514 }
515 ev->type = type;
516 ev->value = value;
517 microtime(&xxxtime);
518 TIMEVAL_TO_TIMESPEC(&xxxtime, &ev->time);
519 evar->put = put;
520 WSEVENT_WAKEUP(evar);
521 }
522
523 #ifdef WSDISPLAY_COMPAT_RAWKBD
524 void
525 wskbd_rawinput(dev, buf, len)
526 struct device *dev;
527 char *buf;
528 int len;
529 {
530 #if NWSDISPLAY > 0
531 struct wskbd_softc *sc = (struct wskbd_softc *)dev;
532 int i;
533
534 for (i = 0; i < len; i++)
535 wsdisplay_kbdinput(sc->sc_displaydv, buf[i]);
536 /* this is KS_GROUP_Ascii */
537 #endif
538 }
539 #endif /* WSDISPLAY_COMPAT_RAWKBD */
540
541 #if NWSDISPLAY > 0
542 static void
543 wskbd_holdscreen(sc, hold)
544 struct wskbd_softc *sc;
545 int hold;
546 {
547 int new_state;
548
549 if (sc->sc_displaydv != NULL) {
550 wsdisplay_kbdholdscreen(sc->sc_displaydv, hold);
551 new_state = sc->sc_ledstate;
552 if (hold)
553 new_state |= WSKBD_LED_SCROLL;
554 else
555 new_state &= ~WSKBD_LED_SCROLL;
556 if (new_state != sc->sc_ledstate) {
557 (*sc->sc_accessops->set_leds)(sc->sc_accesscookie,
558 new_state);
559 sc->sc_ledstate = new_state;
560 }
561 }
562 }
563 #endif
564
565 static int
566 wskbd_enable(sc, on)
567 struct wskbd_softc *sc;
568 int on;
569 {
570 int res;
571
572 /* XXX reference count? */
573 if (!on && (!sc->sc_translating
574 #if NWSDISPLAY > 0
575 || sc->sc_displaydv
576 #endif
577 ))
578 return (EBUSY);
579
580 res = (*sc->sc_accessops->enable)(sc->sc_accesscookie, on);
581 return (res);
582 }
583
584 int
585 wskbdopen(dev, flags, mode, p)
586 dev_t dev;
587 int flags, mode;
588 struct proc *p;
589 {
590 struct wskbd_softc *sc;
591 int unit;
592
593 unit = minor(dev);
594 if (unit >= wskbd_cd.cd_ndevs || /* make sure it was attached */
595 (sc = wskbd_cd.cd_devs[unit]) == NULL)
596 return (ENXIO);
597
598 if (sc->sc_dying)
599 return (EIO);
600
601 if (sc->sc_events.io) /* and that it's not in use */
602 return (EBUSY);
603
604 sc->sc_events.io = p;
605 wsevent_init(&sc->sc_events); /* may cause sleep */
606
607 sc->sc_translating = 0;
608 sc->sc_ready = 1; /* start accepting events */
609
610 wskbd_enable(sc, 1);
611 return (0);
612 }
613
614 int
615 wskbdclose(dev, flags, mode, p)
616 dev_t dev;
617 int flags, mode;
618 struct proc *p;
619 {
620 struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
621
622 sc->sc_ready = 0; /* stop accepting events */
623 sc->sc_translating = 1;
624
625 wsevent_fini(&sc->sc_events);
626 sc->sc_events.io = NULL;
627
628 wskbd_enable(sc, 0);
629 return (0);
630 }
631
632 int
633 wskbdread(dev, uio, flags)
634 dev_t dev;
635 struct uio *uio;
636 int flags;
637 {
638 struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
639 int error;
640
641 if (sc->sc_dying)
642 return (EIO);
643
644 sc->sc_refcnt++;
645 error = wsevent_read(&sc->sc_events, uio, flags);
646 if (--sc->sc_refcnt < 0) {
647 wakeup(sc);
648 error = EIO;
649 }
650 return (error);
651 }
652
653 int
654 wskbdioctl(dev, cmd, data, flag, p)
655 dev_t dev;
656 u_long cmd;
657 caddr_t data;
658 int flag;
659 struct proc *p;
660 {
661 struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
662 int error;
663
664 sc->sc_refcnt++;
665 error = wskbd_do_ioctl(sc, cmd, data, flag, p);
666 if (--sc->sc_refcnt < 0)
667 wakeup(sc);
668 return (error);
669 }
670
671 int
672 wskbd_do_ioctl(sc, cmd, data, flag, p)
673 struct wskbd_softc *sc;
674 u_long cmd;
675 caddr_t data;
676 int flag;
677 struct proc *p;
678 {
679 int error;
680
681 /*
682 * Try the generic ioctls that the wskbd interface supports.
683 */
684 switch (cmd) {
685 case FIONBIO: /* we will remove this someday (soon???) */
686 return (0);
687
688 case FIOASYNC:
689 sc->sc_events.async = *(int *)data != 0;
690 return (0);
691
692 case TIOCSPGRP:
693 if (*(int *)data != sc->sc_events.io->p_pgid)
694 return (EPERM);
695 return (0);
696 }
697
698 /*
699 * Try the keyboard driver for WSKBDIO ioctls. It returns -1
700 * if it didn't recognize the request.
701 */
702 error = wskbd_displayioctl((struct device *)sc, cmd, data, flag, p);
703 return (error != -1 ? error : ENOTTY);
704 }
705
706 /*
707 * WSKBDIO ioctls, handled in both emulation mode and in ``raw'' mode.
708 * Some of these have no real effect in raw mode, however.
709 */
710 int
711 wskbd_displayioctl(dev, cmd, data, flag, p)
712 struct device *dev;
713 u_long cmd;
714 caddr_t data;
715 int flag;
716 struct proc *p;
717 {
718 struct wskbd_softc *sc = (struct wskbd_softc *)dev;
719 struct wskbd_bell_data *ubdp, *kbdp;
720 struct wskbd_keyrepeat_data *ukdp, *kkdp;
721 struct wskbd_map_data *umdp;
722 struct wskbd_mapdata md;
723 void *buf;
724 int len, error;
725
726 switch (cmd) {
727 #define SETBELL(dstp, srcp, dfltp) \
728 do { \
729 (dstp)->pitch = ((srcp)->which & WSKBD_BELL_DOPITCH) ? \
730 (srcp)->pitch : (dfltp)->pitch; \
731 (dstp)->period = ((srcp)->which & WSKBD_BELL_DOPERIOD) ? \
732 (srcp)->period : (dfltp)->period; \
733 (dstp)->volume = ((srcp)->which & WSKBD_BELL_DOVOLUME) ? \
734 (srcp)->volume : (dfltp)->volume; \
735 (dstp)->which = WSKBD_BELL_DOALL; \
736 } while (0)
737
738 case WSKBDIO_BELL:
739 if ((flag & FWRITE) == 0)
740 return (EACCES);
741 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
742 WSKBDIO_COMPLEXBELL, (caddr_t)&sc->sc_bell_data, flag, p));
743
744 case WSKBDIO_COMPLEXBELL:
745 if ((flag & FWRITE) == 0)
746 return (EACCES);
747 ubdp = (struct wskbd_bell_data *)data;
748 SETBELL(ubdp, ubdp, &sc->sc_bell_data);
749 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
750 WSKBDIO_COMPLEXBELL, (caddr_t)ubdp, flag, p));
751
752 case WSKBDIO_SETBELL:
753 if ((flag & FWRITE) == 0)
754 return (EACCES);
755 kbdp = &sc->sc_bell_data;
756 setbell:
757 ubdp = (struct wskbd_bell_data *)data;
758 SETBELL(kbdp, ubdp, kbdp);
759 return (0);
760
761 case WSKBDIO_GETBELL:
762 kbdp = &sc->sc_bell_data;
763 getbell:
764 ubdp = (struct wskbd_bell_data *)data;
765 SETBELL(ubdp, kbdp, kbdp);
766 return (0);
767
768 case WSKBDIO_SETDEFAULTBELL:
769 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
770 return (error);
771 kbdp = &wskbd_default_bell_data;
772 goto setbell;
773
774
775 case WSKBDIO_GETDEFAULTBELL:
776 kbdp = &wskbd_default_bell_data;
777 goto getbell;
778
779 #undef SETBELL
780
781 #define SETKEYREPEAT(dstp, srcp, dfltp) \
782 do { \
783 (dstp)->del1 = ((srcp)->which & WSKBD_KEYREPEAT_DODEL1) ? \
784 (srcp)->del1 : (dfltp)->del1; \
785 (dstp)->delN = ((srcp)->which & WSKBD_KEYREPEAT_DODELN) ? \
786 (srcp)->delN : (dfltp)->delN; \
787 (dstp)->which = WSKBD_KEYREPEAT_DOALL; \
788 } while (0)
789
790 case WSKBDIO_SETKEYREPEAT:
791 if ((flag & FWRITE) == 0)
792 return (EACCES);
793 kkdp = &sc->sc_keyrepeat_data;
794 setkeyrepeat:
795 ukdp = (struct wskbd_keyrepeat_data *)data;
796 SETKEYREPEAT(kkdp, ukdp, kkdp);
797 return (0);
798
799 case WSKBDIO_GETKEYREPEAT:
800 kkdp = &sc->sc_keyrepeat_data;
801 getkeyrepeat:
802 ukdp = (struct wskbd_keyrepeat_data *)data;
803 SETKEYREPEAT(ukdp, kkdp, kkdp);
804 return (0);
805
806 case WSKBDIO_SETDEFAULTKEYREPEAT:
807 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
808 return (error);
809 kkdp = &wskbd_default_keyrepeat_data;
810 goto setkeyrepeat;
811
812
813 case WSKBDIO_GETDEFAULTKEYREPEAT:
814 kkdp = &wskbd_default_keyrepeat_data;
815 goto getkeyrepeat;
816
817 #undef SETKEYREPEAT
818
819 case WSKBDIO_SETMAP:
820 if ((flag & FWRITE) == 0)
821 return (EACCES);
822 umdp = (struct wskbd_map_data *)data;
823 len = umdp->maplen*sizeof(struct wscons_keymap);
824 buf = malloc(len, M_TEMP, M_WAITOK);
825 error = copyin(umdp->map, buf, len);
826 if (error == 0) {
827 wskbd_init_keymap(umdp->maplen,
828 &sc->sc_map, &sc->sc_maplen);
829 memcpy(sc->sc_map, buf, len);
830 sc->sc_layout = KB_USER;
831 }
832 free(buf, M_TEMP);
833 return(error);
834
835 case WSKBDIO_GETMAP:
836 umdp = (struct wskbd_map_data *)data;
837 if (umdp->maplen > sc->sc_maplen)
838 umdp->maplen = sc->sc_maplen;
839 error = copyout(sc->sc_map, umdp->map,
840 umdp->maplen*sizeof(struct wscons_keymap));
841 return(error);
842
843 case WSKBDIO_GETENCODING:
844 *((kbd_t *) data) = sc->sc_layout;
845 return(0);
846
847 case WSKBDIO_SETENCODING:
848 if ((flag & FWRITE) == 0)
849 return (EACCES);
850 md = *(sc->id->t_keymap); /* structure assignment */
851 md.layout = *((kbd_t *)data);
852 error = wskbd_load_keymap(&md, &sc->sc_map, &sc->sc_maplen);
853 if (error == 0)
854 sc->sc_layout = *((kbd_t *)data);
855 return(error);
856 }
857
858 /*
859 * Try the keyboard driver for WSKBDIO ioctls. It returns -1
860 * if it didn't recognize the request, and in turn we return
861 * -1 if we didn't recognize the request.
862 */
863 /* printf("kbdaccess\n"); */
864 error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
865 flag, p);
866 #ifdef WSDISPLAY_COMPAT_RAWKBD
867 if (!error && cmd == WSKBDIO_SETMODE && *(int *)data == WSKBD_RAW) {
868 int s = spltty();
869 sc->id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
870 | MOD_CONTROL_L | MOD_CONTROL_R
871 | MOD_META_L | MOD_META_R
872 | MOD_COMMAND
873 | MOD_COMMAND1 | MOD_COMMAND2);
874 if (sc->sc_repeating) {
875 sc->sc_repeating = 0;
876 untimeout(wskbd_repeat, sc);
877 }
878 splx(s);
879 }
880 #endif
881 return (error);
882 }
883
884 int
885 wskbdpoll(dev, events, p)
886 dev_t dev;
887 int events;
888 struct proc *p;
889 {
890 struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
891
892 return (wsevent_poll(&sc->sc_events, events, p));
893 }
894
895 #if NWSDISPLAY > 0
896
897 int
898 wskbd_pickfree()
899 {
900 int i;
901 struct wskbd_softc *sc;
902
903 for (i = 0; i < wskbd_cd.cd_ndevs; i++) {
904 if ((sc = wskbd_cd.cd_devs[i]) == NULL)
905 continue;
906 if (sc->sc_displaydv == NULL)
907 return (i);
908 }
909 return (-1);
910 }
911
912 int
913 wskbd_set_display(idx, displaydv, kbddv)
914 int idx;
915 struct device *displaydv, **kbddv;
916 {
917 struct wskbd_softc *sc;
918
919 if (idx >= wskbd_cd.cd_ndevs || /* make sure it was attached */
920 (sc = wskbd_cd.cd_devs[idx]) == NULL)
921 return (ENXIO);
922
923 if (sc->sc_isconsole)
924 return (EBUSY);
925 if (displaydv) {
926 if (sc->sc_displaydv)
927 return (EBUSY);
928 printf("%s: connecting to %s\n",
929 sc->sc_dv.dv_xname, displaydv->dv_xname);
930 } else {
931 if (sc->sc_displaydv == NULL)
932 return (ENXIO);
933 printf("%s: disconnecting\n", sc->sc_dv.dv_xname);
934 }
935
936 sc->sc_displaydv = displaydv;
937 wskbd_enable(sc, displaydv != NULL);
938 if (kbddv)
939 *kbddv = &sc->sc_dv;
940 return (0);
941 }
942
943 struct device *
944 wskbd_set_console_display(displaydv)
945 struct device *displaydv;
946 {
947 if (!wskbd_console_device)
948 return (0);
949 wskbd_console_device->sc_displaydv = displaydv;
950 return (&wskbd_console_device->sc_dv);
951 }
952
953 #endif /* NWSDISPLAY > 0 */
954
955 /*
956 * Console interface.
957 */
958 int
959 wskbd_cngetc(dev)
960 dev_t dev;
961 {
962 u_int type;
963 int data;
964 keysym_t ks;
965
966 if (!wskbd_console_initted)
967 return 0;
968
969 if (wskbd_console_device != NULL &&
970 !wskbd_console_device->sc_translating)
971 return 0;
972
973 for(;;) {
974 (*wskbd_console_data.t_consops->getc)
975 (wskbd_console_data.t_consaccesscookie, &type, &data);
976 ks = wskbd_translate(&wskbd_console_data, type, data);
977
978 if (KS_GROUP(ks) == KS_GROUP_Ascii)
979 return (KS_VALUE(ks));
980 }
981 }
982
983 void
984 wskbd_cnpollc(dev, poll)
985 dev_t dev;
986 int poll;
987 {
988
989 if (!wskbd_console_initted)
990 return;
991
992 if (wskbd_console_device != NULL &&
993 !wskbd_console_device->sc_translating)
994 return;
995
996 (*wskbd_console_data.t_consops->pollc)
997 (wskbd_console_data.t_consaccesscookie, poll);
998 }
999
1000 static inline void
1001 update_leds(id)
1002 struct wskbd_internal *id;
1003 {
1004 int new_state;
1005
1006 new_state = 0;
1007 if (id->t_modifiers & (MOD_SHIFTLOCK | MOD_CAPSLOCK))
1008 new_state |= WSKBD_LED_CAPS;
1009 if (id->t_modifiers & MOD_NUMLOCK)
1010 new_state |= WSKBD_LED_NUM;
1011 if (id->t_modifiers & MOD_COMPOSE)
1012 new_state |= WSKBD_LED_COMPOSE;
1013 if (id->t_modifiers & MOD_HOLDSCREEN)
1014 new_state |= WSKBD_LED_SCROLL;
1015
1016 if (id->t_sc && new_state != id->t_sc->sc_ledstate) {
1017 (*id->t_sc->sc_accessops->set_leds)
1018 (id->t_sc->sc_accesscookie, new_state);
1019 id->t_sc->sc_ledstate = new_state;
1020 }
1021 }
1022
1023 static inline void
1024 update_modifier(id, type, toggle, mask)
1025 struct wskbd_internal *id;
1026 u_int type;
1027 int toggle;
1028 int mask;
1029 {
1030 if (toggle) {
1031 if (type == WSCONS_EVENT_KEY_DOWN)
1032 id->t_modifiers ^= mask;
1033 } else {
1034 if (type == WSCONS_EVENT_KEY_DOWN)
1035 id->t_modifiers |= mask;
1036 else
1037 id->t_modifiers &= ~mask;
1038 }
1039 }
1040
1041 static int
1042 internal_command(sc, type, ksym)
1043 struct wskbd_softc *sc;
1044 u_int *type;
1045 keysym_t ksym;
1046 {
1047 switch (ksym) {
1048 case KS_Cmd:
1049 update_modifier(sc->id, *type, 0, MOD_COMMAND);
1050 break;
1051
1052 case KS_Cmd1:
1053 update_modifier(sc->id, *type, 0, MOD_COMMAND1);
1054 break;
1055
1056 case KS_Cmd2:
1057 update_modifier(sc->id, *type, 0, MOD_COMMAND2);
1058 break;
1059 }
1060
1061 if (*type != WSCONS_EVENT_KEY_DOWN ||
1062 (! MOD_ONESET(sc->id, MOD_COMMAND) &&
1063 ! MOD_ALLSET(sc->id, MOD_COMMAND1 | MOD_COMMAND2)))
1064 return (0);
1065
1066 switch (ksym) {
1067 #ifdef DDB
1068 case KS_Cmd_Debugger:
1069 if (sc->sc_isconsole)
1070 console_debugger();
1071 /* discard this key (ddb discarded command modifiers) */
1072 *type = WSCONS_EVENT_KEY_UP;
1073 return (1);
1074 #endif
1075
1076 #if NWSDISPLAY > 0
1077 case KS_Cmd_Screen0:
1078 case KS_Cmd_Screen1:
1079 case KS_Cmd_Screen2:
1080 case KS_Cmd_Screen3:
1081 case KS_Cmd_Screen4:
1082 case KS_Cmd_Screen5:
1083 case KS_Cmd_Screen6:
1084 case KS_Cmd_Screen7:
1085 case KS_Cmd_Screen8:
1086 case KS_Cmd_Screen9:
1087 wsdisplay_switch(sc->sc_displaydv, ksym - KS_Cmd_Screen0, 0);
1088 return (1);
1089 case KS_Cmd_ResetEmul:
1090 wsdisplay_reset(sc->sc_displaydv, WSDISPLAY_RESETEMUL);
1091 return (1);
1092 case KS_Cmd_ResetClose:
1093 wsdisplay_reset(sc->sc_displaydv, WSDISPLAY_RESETCLOSE);
1094 return (1);
1095 #endif
1096 }
1097 return (0);
1098 }
1099
1100 static keysym_t
1101 wskbd_translate(id, type, value)
1102 struct wskbd_internal *id;
1103 u_int type;
1104 int value;
1105 {
1106 struct wskbd_softc *sc = id->t_sc;
1107 keysym_t ksym, res, *group;
1108 struct wscons_keymap kpbuf, *kp;
1109 int iscommand = 0;
1110
1111 if (type == WSCONS_EVENT_ALL_KEYS_UP) {
1112 id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
1113 | MOD_CONTROL_L | MOD_CONTROL_R
1114 | MOD_META_L | MOD_META_R
1115 | MOD_MODESHIFT
1116 | MOD_COMMAND | MOD_COMMAND1 | MOD_COMMAND2);
1117 update_leds(id);
1118 return (KS_voidSymbol);
1119 }
1120
1121 if (sc != NULL) {
1122 if (value < 0 || value >= sc->sc_maplen) {
1123 #ifdef DEBUG
1124 printf("wskbd_translate: keycode %d out of range\n",
1125 value);
1126 #endif
1127 return (KS_voidSymbol);
1128 }
1129 kp = sc->sc_map + value;
1130 } else {
1131 kp = &kpbuf;
1132 wskbd_get_mapentry(id->t_keymap, value, kp);
1133 }
1134
1135 /* if this key has a command, process it first */
1136 if (sc != NULL && kp->command != KS_voidSymbol)
1137 iscommand = internal_command(sc, &type, kp->command);
1138
1139 /* Now update modifiers */
1140 switch (kp->group1[0]) {
1141 case KS_Shift_L:
1142 update_modifier(id, type, 0, MOD_SHIFT_L);
1143 break;
1144
1145 case KS_Shift_R:
1146 update_modifier(id, type, 0, MOD_SHIFT_R);
1147 break;
1148
1149 case KS_Shift_Lock:
1150 update_modifier(id, type, 1, MOD_SHIFTLOCK);
1151 break;
1152
1153 case KS_Caps_Lock:
1154 update_modifier(id, type, 1, MOD_CAPSLOCK);
1155 break;
1156
1157 case KS_Control_L:
1158 update_modifier(id, type, 0, MOD_CONTROL_L);
1159 break;
1160
1161 case KS_Control_R:
1162 update_modifier(id, type, 0, MOD_CONTROL_R);
1163 break;
1164
1165 case KS_Alt_L:
1166 update_modifier(id, type, 0, MOD_META_L);
1167 break;
1168
1169 case KS_Alt_R:
1170 update_modifier(id, type, 0, MOD_META_R);
1171 break;
1172
1173 case KS_Mode_switch:
1174 update_modifier(id, type, 0, MOD_MODESHIFT);
1175 break;
1176
1177 case KS_Num_Lock:
1178 update_modifier(id, type, 1, MOD_NUMLOCK);
1179 break;
1180
1181 #if NWSDISPLAY > 0
1182 case KS_Hold_Screen:
1183 if (sc != NULL) {
1184 update_modifier(id, type, 1, MOD_HOLDSCREEN);
1185 wskbd_holdscreen(sc, id->t_modifiers & MOD_HOLDSCREEN);
1186 }
1187 break;
1188 #endif
1189 }
1190
1191 /* If this is a key release or we are in command mode, we are done */
1192 if (type != WSCONS_EVENT_KEY_DOWN || iscommand) {
1193 update_leds(id);
1194 return (KS_voidSymbol);
1195 }
1196
1197 /* Get the keysym */
1198 if (id->t_modifiers & MOD_MODESHIFT)
1199 group = & kp->group2[0];
1200 else
1201 group = & kp->group1[0];
1202
1203 if ((id->t_modifiers & MOD_NUMLOCK) != 0 &&
1204 KS_GROUP(group[1]) == KS_GROUP_Keypad) {
1205 if (MOD_ONESET(id, MOD_ANYSHIFT))
1206 ksym = group[0];
1207 else
1208 ksym = group[1];
1209 } else if (! MOD_ONESET(id, MOD_ANYSHIFT | MOD_CAPSLOCK)) {
1210 ksym = group[0];
1211 } else if (MOD_ONESET(id, MOD_CAPSLOCK)) {
1212 if (! MOD_ONESET(id, MOD_SHIFT_L | MOD_SHIFT_R))
1213 ksym = group[0];
1214 else
1215 ksym = group[1];
1216 if (ksym >= KS_a && ksym <= KS_z)
1217 ksym += KS_A - KS_a;
1218 else if (ksym >= KS_agrave && ksym <= KS_thorn &&
1219 ksym != KS_division)
1220 ksym += KS_Agrave - KS_agrave;
1221 } else if (MOD_ONESET(id, MOD_ANYSHIFT)) {
1222 ksym = group[1];
1223 } else {
1224 ksym = group[0];
1225 }
1226
1227 /* Process compose sequence and dead accents */
1228 res = KS_voidSymbol;
1229
1230 switch (KS_GROUP(ksym)) {
1231 case KS_GROUP_Ascii:
1232 case KS_GROUP_Keypad:
1233 case KS_GROUP_Function:
1234 res = ksym;
1235 break;
1236
1237 case KS_GROUP_Mod:
1238 if (ksym == KS_Multi_key) {
1239 update_modifier(id, 1, 0, MOD_COMPOSE);
1240 id->t_composelen = 2;
1241 }
1242 break;
1243
1244 case KS_GROUP_Dead:
1245 if (id->t_composelen == 0) {
1246 update_modifier(id, 1, 0, MOD_COMPOSE);
1247 id->t_composelen = 1;
1248 id->t_composebuf[0] = ksym;
1249 } else
1250 res = ksym;
1251 break;
1252 }
1253
1254 if (res == KS_voidSymbol) {
1255 update_leds(id);
1256 return (res);
1257 }
1258
1259 if (id->t_composelen > 0) {
1260 id->t_composebuf[2 - id->t_composelen] = res;
1261 if (--id->t_composelen == 0) {
1262 res = wskbd_compose_value(id->t_composebuf);
1263 update_modifier(id, 0, 0, MOD_COMPOSE);
1264 } else {
1265 return (KS_voidSymbol);
1266 }
1267 }
1268
1269 update_leds(id);
1270
1271 /* We are done, return the symbol */
1272 if (KS_GROUP(res) == KS_GROUP_Ascii) {
1273 if (MOD_ONESET(id, MOD_ANYCONTROL)) {
1274 if ((res >= KS_at && res <= KS_z) || res == KS_space)
1275 res = res & 0x1f;
1276 else if (res == KS_2)
1277 res = 0x00;
1278 else if (res >= KS_3 && res <= KS_7)
1279 res = KS_Escape + (res - KS_3);
1280 else if (res == KS_8)
1281 res = KS_Delete;
1282 }
1283 if (MOD_ONESET(id, MOD_ANYMETA))
1284 res |= 0x80;
1285 }
1286
1287 return (res);
1288 }
1289