wskbd.c revision 1.25 1 /* $NetBSD: wskbd.c,v 1.25 1999/07/01 11:18:20 drochner 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.25 1999/07/01 11:18:20 drochner 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 NWSDISPLAY > 0
875 if (sc->sc_repeating) {
876 sc->sc_repeating = 0;
877 untimeout(wskbd_repeat, sc);
878 }
879 #endif
880 splx(s);
881 }
882 #endif
883 return (error);
884 }
885
886 int
887 wskbdpoll(dev, events, p)
888 dev_t dev;
889 int events;
890 struct proc *p;
891 {
892 struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
893
894 return (wsevent_poll(&sc->sc_events, events, p));
895 }
896
897 #if NWSDISPLAY > 0
898
899 int
900 wskbd_pickfree()
901 {
902 int i;
903 struct wskbd_softc *sc;
904
905 for (i = 0; i < wskbd_cd.cd_ndevs; i++) {
906 if ((sc = wskbd_cd.cd_devs[i]) == NULL)
907 continue;
908 if (sc->sc_displaydv == NULL)
909 return (i);
910 }
911 return (-1);
912 }
913
914 int
915 wskbd_set_display(idx, displaydv, kbddv)
916 int idx;
917 struct device *displaydv, **kbddv;
918 {
919 struct wskbd_softc *sc;
920
921 if (idx >= wskbd_cd.cd_ndevs || /* make sure it was attached */
922 (sc = wskbd_cd.cd_devs[idx]) == NULL)
923 return (ENXIO);
924
925 if (sc->sc_isconsole)
926 return (EBUSY);
927 if (displaydv) {
928 if (sc->sc_displaydv)
929 return (EBUSY);
930 printf("%s: connecting to %s\n",
931 sc->sc_dv.dv_xname, displaydv->dv_xname);
932 } else {
933 if (sc->sc_displaydv == NULL)
934 return (ENXIO);
935 printf("%s: disconnecting\n", sc->sc_dv.dv_xname);
936 }
937
938 sc->sc_displaydv = displaydv;
939 wskbd_enable(sc, displaydv != NULL);
940 if (kbddv)
941 *kbddv = &sc->sc_dv;
942 return (0);
943 }
944
945 struct device *
946 wskbd_set_console_display(displaydv)
947 struct device *displaydv;
948 {
949 if (!wskbd_console_device)
950 return (0);
951 wskbd_console_device->sc_displaydv = displaydv;
952 return (&wskbd_console_device->sc_dv);
953 }
954
955 #endif /* NWSDISPLAY > 0 */
956
957 /*
958 * Console interface.
959 */
960 int
961 wskbd_cngetc(dev)
962 dev_t dev;
963 {
964 u_int type;
965 int data;
966 keysym_t ks;
967
968 if (!wskbd_console_initted)
969 return 0;
970
971 if (wskbd_console_device != NULL &&
972 !wskbd_console_device->sc_translating)
973 return 0;
974
975 for(;;) {
976 (*wskbd_console_data.t_consops->getc)
977 (wskbd_console_data.t_consaccesscookie, &type, &data);
978 ks = wskbd_translate(&wskbd_console_data, type, data);
979
980 if (KS_GROUP(ks) == KS_GROUP_Ascii)
981 return (KS_VALUE(ks));
982 }
983 }
984
985 void
986 wskbd_cnpollc(dev, poll)
987 dev_t dev;
988 int poll;
989 {
990
991 if (!wskbd_console_initted)
992 return;
993
994 if (wskbd_console_device != NULL &&
995 !wskbd_console_device->sc_translating)
996 return;
997
998 (*wskbd_console_data.t_consops->pollc)
999 (wskbd_console_data.t_consaccesscookie, poll);
1000 }
1001
1002 static inline void
1003 update_leds(id)
1004 struct wskbd_internal *id;
1005 {
1006 int new_state;
1007
1008 new_state = 0;
1009 if (id->t_modifiers & (MOD_SHIFTLOCK | MOD_CAPSLOCK))
1010 new_state |= WSKBD_LED_CAPS;
1011 if (id->t_modifiers & MOD_NUMLOCK)
1012 new_state |= WSKBD_LED_NUM;
1013 if (id->t_modifiers & MOD_COMPOSE)
1014 new_state |= WSKBD_LED_COMPOSE;
1015 if (id->t_modifiers & MOD_HOLDSCREEN)
1016 new_state |= WSKBD_LED_SCROLL;
1017
1018 if (id->t_sc && new_state != id->t_sc->sc_ledstate) {
1019 (*id->t_sc->sc_accessops->set_leds)
1020 (id->t_sc->sc_accesscookie, new_state);
1021 id->t_sc->sc_ledstate = new_state;
1022 }
1023 }
1024
1025 static inline void
1026 update_modifier(id, type, toggle, mask)
1027 struct wskbd_internal *id;
1028 u_int type;
1029 int toggle;
1030 int mask;
1031 {
1032 if (toggle) {
1033 if (type == WSCONS_EVENT_KEY_DOWN)
1034 id->t_modifiers ^= mask;
1035 } else {
1036 if (type == WSCONS_EVENT_KEY_DOWN)
1037 id->t_modifiers |= mask;
1038 else
1039 id->t_modifiers &= ~mask;
1040 }
1041 }
1042
1043 static int
1044 internal_command(sc, type, ksym)
1045 struct wskbd_softc *sc;
1046 u_int *type;
1047 keysym_t ksym;
1048 {
1049 switch (ksym) {
1050 case KS_Cmd:
1051 update_modifier(sc->id, *type, 0, MOD_COMMAND);
1052 break;
1053
1054 case KS_Cmd1:
1055 update_modifier(sc->id, *type, 0, MOD_COMMAND1);
1056 break;
1057
1058 case KS_Cmd2:
1059 update_modifier(sc->id, *type, 0, MOD_COMMAND2);
1060 break;
1061 }
1062
1063 if (*type != WSCONS_EVENT_KEY_DOWN ||
1064 (! MOD_ONESET(sc->id, MOD_COMMAND) &&
1065 ! MOD_ALLSET(sc->id, MOD_COMMAND1 | MOD_COMMAND2)))
1066 return (0);
1067
1068 switch (ksym) {
1069 #ifdef DDB
1070 case KS_Cmd_Debugger:
1071 if (sc->sc_isconsole)
1072 console_debugger();
1073 /* discard this key (ddb discarded command modifiers) */
1074 *type = WSCONS_EVENT_KEY_UP;
1075 return (1);
1076 #endif
1077
1078 #if NWSDISPLAY > 0
1079 case KS_Cmd_Screen0:
1080 case KS_Cmd_Screen1:
1081 case KS_Cmd_Screen2:
1082 case KS_Cmd_Screen3:
1083 case KS_Cmd_Screen4:
1084 case KS_Cmd_Screen5:
1085 case KS_Cmd_Screen6:
1086 case KS_Cmd_Screen7:
1087 case KS_Cmd_Screen8:
1088 case KS_Cmd_Screen9:
1089 wsdisplay_switch(sc->sc_displaydv, ksym - KS_Cmd_Screen0, 0);
1090 return (1);
1091 case KS_Cmd_ResetEmul:
1092 wsdisplay_reset(sc->sc_displaydv, WSDISPLAY_RESETEMUL);
1093 return (1);
1094 case KS_Cmd_ResetClose:
1095 wsdisplay_reset(sc->sc_displaydv, WSDISPLAY_RESETCLOSE);
1096 return (1);
1097 #endif
1098 }
1099 return (0);
1100 }
1101
1102 static keysym_t
1103 wskbd_translate(id, type, value)
1104 struct wskbd_internal *id;
1105 u_int type;
1106 int value;
1107 {
1108 struct wskbd_softc *sc = id->t_sc;
1109 keysym_t ksym, res, *group;
1110 struct wscons_keymap kpbuf, *kp;
1111 int iscommand = 0;
1112
1113 if (type == WSCONS_EVENT_ALL_KEYS_UP) {
1114 id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
1115 | MOD_CONTROL_L | MOD_CONTROL_R
1116 | MOD_META_L | MOD_META_R
1117 | MOD_MODESHIFT
1118 | MOD_COMMAND | MOD_COMMAND1 | MOD_COMMAND2);
1119 update_leds(id);
1120 return (KS_voidSymbol);
1121 }
1122
1123 if (sc != NULL) {
1124 if (value < 0 || value >= sc->sc_maplen) {
1125 #ifdef DEBUG
1126 printf("wskbd_translate: keycode %d out of range\n",
1127 value);
1128 #endif
1129 return (KS_voidSymbol);
1130 }
1131 kp = sc->sc_map + value;
1132 } else {
1133 kp = &kpbuf;
1134 wskbd_get_mapentry(id->t_keymap, value, kp);
1135 }
1136
1137 /* if this key has a command, process it first */
1138 if (sc != NULL && kp->command != KS_voidSymbol)
1139 iscommand = internal_command(sc, &type, kp->command);
1140
1141 /* Now update modifiers */
1142 switch (kp->group1[0]) {
1143 case KS_Shift_L:
1144 update_modifier(id, type, 0, MOD_SHIFT_L);
1145 break;
1146
1147 case KS_Shift_R:
1148 update_modifier(id, type, 0, MOD_SHIFT_R);
1149 break;
1150
1151 case KS_Shift_Lock:
1152 update_modifier(id, type, 1, MOD_SHIFTLOCK);
1153 break;
1154
1155 case KS_Caps_Lock:
1156 update_modifier(id, type, 1, MOD_CAPSLOCK);
1157 break;
1158
1159 case KS_Control_L:
1160 update_modifier(id, type, 0, MOD_CONTROL_L);
1161 break;
1162
1163 case KS_Control_R:
1164 update_modifier(id, type, 0, MOD_CONTROL_R);
1165 break;
1166
1167 case KS_Alt_L:
1168 update_modifier(id, type, 0, MOD_META_L);
1169 break;
1170
1171 case KS_Alt_R:
1172 update_modifier(id, type, 0, MOD_META_R);
1173 break;
1174
1175 case KS_Mode_switch:
1176 update_modifier(id, type, 0, MOD_MODESHIFT);
1177 break;
1178
1179 case KS_Num_Lock:
1180 update_modifier(id, type, 1, MOD_NUMLOCK);
1181 break;
1182
1183 #if NWSDISPLAY > 0
1184 case KS_Hold_Screen:
1185 if (sc != NULL) {
1186 update_modifier(id, type, 1, MOD_HOLDSCREEN);
1187 wskbd_holdscreen(sc, id->t_modifiers & MOD_HOLDSCREEN);
1188 }
1189 break;
1190 #endif
1191 }
1192
1193 /* If this is a key release or we are in command mode, we are done */
1194 if (type != WSCONS_EVENT_KEY_DOWN || iscommand) {
1195 update_leds(id);
1196 return (KS_voidSymbol);
1197 }
1198
1199 /* Get the keysym */
1200 if (id->t_modifiers & MOD_MODESHIFT)
1201 group = & kp->group2[0];
1202 else
1203 group = & kp->group1[0];
1204
1205 if ((id->t_modifiers & MOD_NUMLOCK) != 0 &&
1206 KS_GROUP(group[1]) == KS_GROUP_Keypad) {
1207 if (MOD_ONESET(id, MOD_ANYSHIFT))
1208 ksym = group[0];
1209 else
1210 ksym = group[1];
1211 } else if (! MOD_ONESET(id, MOD_ANYSHIFT | MOD_CAPSLOCK)) {
1212 ksym = group[0];
1213 } else if (MOD_ONESET(id, MOD_CAPSLOCK)) {
1214 if (! MOD_ONESET(id, MOD_SHIFT_L | MOD_SHIFT_R))
1215 ksym = group[0];
1216 else
1217 ksym = group[1];
1218 if (ksym >= KS_a && ksym <= KS_z)
1219 ksym += KS_A - KS_a;
1220 else if (ksym >= KS_agrave && ksym <= KS_thorn &&
1221 ksym != KS_division)
1222 ksym += KS_Agrave - KS_agrave;
1223 } else if (MOD_ONESET(id, MOD_ANYSHIFT)) {
1224 ksym = group[1];
1225 } else {
1226 ksym = group[0];
1227 }
1228
1229 /* Process compose sequence and dead accents */
1230 res = KS_voidSymbol;
1231
1232 switch (KS_GROUP(ksym)) {
1233 case KS_GROUP_Ascii:
1234 case KS_GROUP_Keypad:
1235 case KS_GROUP_Function:
1236 res = ksym;
1237 break;
1238
1239 case KS_GROUP_Mod:
1240 if (ksym == KS_Multi_key) {
1241 update_modifier(id, 1, 0, MOD_COMPOSE);
1242 id->t_composelen = 2;
1243 }
1244 break;
1245
1246 case KS_GROUP_Dead:
1247 if (id->t_composelen == 0) {
1248 update_modifier(id, 1, 0, MOD_COMPOSE);
1249 id->t_composelen = 1;
1250 id->t_composebuf[0] = ksym;
1251 } else
1252 res = ksym;
1253 break;
1254 }
1255
1256 if (res == KS_voidSymbol) {
1257 update_leds(id);
1258 return (res);
1259 }
1260
1261 if (id->t_composelen > 0) {
1262 id->t_composebuf[2 - id->t_composelen] = res;
1263 if (--id->t_composelen == 0) {
1264 res = wskbd_compose_value(id->t_composebuf);
1265 update_modifier(id, 0, 0, MOD_COMPOSE);
1266 } else {
1267 return (KS_voidSymbol);
1268 }
1269 }
1270
1271 update_leds(id);
1272
1273 /* We are done, return the symbol */
1274 if (KS_GROUP(res) == KS_GROUP_Ascii) {
1275 if (MOD_ONESET(id, MOD_ANYCONTROL)) {
1276 if ((res >= KS_at && res <= KS_z) || res == KS_space)
1277 res = res & 0x1f;
1278 else if (res == KS_2)
1279 res = 0x00;
1280 else if (res >= KS_3 && res <= KS_7)
1281 res = KS_Escape + (res - KS_3);
1282 else if (res == KS_8)
1283 res = KS_Delete;
1284 }
1285 if (MOD_ONESET(id, MOD_ANYMETA))
1286 res |= 0x80;
1287 }
1288
1289 return (res);
1290 }
1291