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