]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/kbd/kbd.c
kbd: remove kbdsw, store pointer to driver in each keyboard_t
[FreeBSD/FreeBSD.git] / sys / dev / kbd / kbd.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer as
12  *    the first lines of this file unmodified.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_kbd.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/conf.h>
40 #include <sys/fcntl.h>
41 #include <sys/poll.h>
42 #include <sys/priv.h>
43 #include <sys/proc.h>
44 #include <sys/selinfo.h>
45 #include <sys/sysctl.h>
46 #include <sys/uio.h>
47
48 #include <sys/kbio.h>
49
50 #include <dev/evdev/input-event-codes.h>
51 #include <dev/kbd/kbdreg.h>
52
53 #define KBD_INDEX(dev)  dev2unit(dev)
54
55 #define KB_QSIZE        512
56 #define KB_BUFSIZE      64
57
58 typedef struct genkbd_softc {
59         int             gkb_flags;      /* flag/status bits */
60 #define KB_ASLEEP       (1 << 0)
61         struct selinfo  gkb_rsel;
62         char            gkb_q[KB_QSIZE];                /* input queue */
63         unsigned int    gkb_q_start;
64         unsigned int    gkb_q_length;
65 } genkbd_softc_t;
66
67 static u_char   *genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len);
68 static void     genkbd_diag(keyboard_t *kbd, int level);
69
70 static  SLIST_HEAD(, keyboard_driver) keyboard_drivers =
71         SLIST_HEAD_INITIALIZER(keyboard_drivers);
72
73 SET_DECLARE(kbddriver_set, const keyboard_driver_t);
74
75 /* local arrays */
76
77 /*
78  * We need at least one entry each in order to initialize a keyboard
79  * for the kernel console.  The arrays will be increased dynamically
80  * when necessary.
81  */
82
83 static int              keyboards = 1;
84 static keyboard_t       *kbd_ini;
85 static keyboard_t       **keyboard = &kbd_ini;
86
87 static int keymap_restrict_change;
88 static SYSCTL_NODE(_hw, OID_AUTO, kbd, CTLFLAG_RD, 0, "kbd");
89 SYSCTL_INT(_hw_kbd, OID_AUTO, keymap_restrict_change, CTLFLAG_RW,
90     &keymap_restrict_change, 0, "restrict ability to change keymap");
91
92 #define ARRAY_DELTA     4
93
94 static int
95 kbd_realloc_array(void)
96 {
97         keyboard_t **new_kbd;
98         int newsize;
99         int s;
100
101         s = spltty();
102         newsize = rounddown(keyboards + ARRAY_DELTA, ARRAY_DELTA);
103         new_kbd = malloc(sizeof(*new_kbd)*newsize, M_DEVBUF, M_NOWAIT|M_ZERO);
104         if (new_kbd == NULL) {
105                 splx(s);
106                 return (ENOMEM);
107         }
108         bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
109         if (keyboards > 1)
110                 free(keyboard, M_DEVBUF);
111         keyboard = new_kbd;
112         keyboards = newsize;
113         splx(s);
114
115         if (bootverbose)
116                 printf("kbd: new array size %d\n", keyboards);
117
118         return (0);
119 }
120
121 /*
122  * Low-level keyboard driver functions
123  * Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard
124  * driver, call these functions to initialize the keyboard_t structure
125  * and register it to the virtual keyboard driver `kbd'.
126  */
127
128 /* initialize the keyboard_t structure */
129 void
130 kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config,
131                 int port, int port_size)
132 {
133         kbd->kb_flags = KB_NO_DEVICE;   /* device has not been found */
134         kbd->kb_name = name;
135         kbd->kb_type = type;
136         kbd->kb_unit = unit;
137         kbd->kb_config = config & ~KB_CONF_PROBE_ONLY;
138         kbd->kb_led = 0;                /* unknown */
139         kbd->kb_io_base = port;
140         kbd->kb_io_size = port_size;
141         kbd->kb_data = NULL;
142         kbd->kb_keymap = NULL;
143         kbd->kb_accentmap = NULL;
144         kbd->kb_fkeytab = NULL;
145         kbd->kb_fkeytab_size = 0;
146         kbd->kb_delay1 = KB_DELAY1;     /* these values are advisory only */
147         kbd->kb_delay2 = KB_DELAY2;
148         kbd->kb_count = 0L;
149         bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact));
150 }
151
152 void
153 kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap,
154              fkeytab_t *fkeymap, int fkeymap_size)
155 {
156         kbd->kb_keymap = keymap;
157         kbd->kb_accentmap = accmap;
158         kbd->kb_fkeytab = fkeymap;
159         kbd->kb_fkeytab_size = fkeymap_size;
160 }
161
162 /* declare a new keyboard driver */
163 int
164 kbd_add_driver(keyboard_driver_t *driver)
165 {
166         if (SLIST_NEXT(driver, link))
167                 return (EINVAL);
168         if (driver->kbdsw->get_fkeystr == NULL)
169                 driver->kbdsw->get_fkeystr = genkbd_get_fkeystr;
170         if (driver->kbdsw->diag == NULL)
171                 driver->kbdsw->diag = genkbd_diag;
172         SLIST_INSERT_HEAD(&keyboard_drivers, driver, link);
173         return (0);
174 }
175
176 int
177 kbd_delete_driver(keyboard_driver_t *driver)
178 {
179         SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link);
180         SLIST_NEXT(driver, link) = NULL;
181         return (0);
182 }
183
184 /* register a keyboard and associate it with a function table */
185 int
186 kbd_register(keyboard_t *kbd)
187 {
188         const keyboard_driver_t **list;
189         const keyboard_driver_t *p;
190         keyboard_t *mux;
191         keyboard_info_t ki;
192         int index;
193
194         mux = kbd_get_keyboard(kbd_find_keyboard("kbdmux", -1));
195
196         for (index = 0; index < keyboards; ++index) {
197                 if (keyboard[index] == NULL)
198                         break;
199         }
200         if (index >= keyboards) {
201                 if (kbd_realloc_array())
202                         return (-1);
203         }
204
205         kbd->kb_index = index;
206         KBD_UNBUSY(kbd);
207         KBD_VALID(kbd);
208         kbd->kb_active = 0;     /* disabled until someone calls kbd_enable() */
209         kbd->kb_token = NULL;
210         kbd->kb_callback.kc_func = NULL;
211         kbd->kb_callback.kc_arg = NULL;
212
213         SLIST_FOREACH(p, &keyboard_drivers, link) {
214                 if (strcmp(p->name, kbd->kb_name) == 0) {
215                         kbd->kb_drv = p;
216                         keyboard[index] = kbd;
217
218                         if (mux != NULL) {
219                                 bzero(&ki, sizeof(ki));
220                                 strcpy(ki.kb_name, kbd->kb_name);
221                                 ki.kb_unit = kbd->kb_unit;
222
223                                 (void)kbdd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
224                         }
225
226                         return (index);
227                 }
228         }
229         SET_FOREACH(list, kbddriver_set) {
230                 p = *list;
231                 if (strcmp(p->name, kbd->kb_name) == 0) {
232                         kbd->kb_drv = p;
233                         keyboard[index] = kbd;
234
235                         if (mux != NULL) {
236                                 bzero(&ki, sizeof(ki));
237                                 strcpy(ki.kb_name, kbd->kb_name);
238                                 ki.kb_unit = kbd->kb_unit;
239
240                                 (void)kbdd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
241                         }
242
243                         return (index);
244                 }
245         }
246
247         return (-1);
248 }
249
250 int
251 kbd_unregister(keyboard_t *kbd)
252 {
253         int error;
254         int s;
255
256         if ((kbd->kb_index < 0) || (kbd->kb_index >= keyboards))
257                 return (ENOENT);
258         if (keyboard[kbd->kb_index] != kbd)
259                 return (ENOENT);
260
261         s = spltty();
262         if (KBD_IS_BUSY(kbd)) {
263                 error = (*kbd->kb_callback.kc_func)(kbd, KBDIO_UNLOADING,
264                     kbd->kb_callback.kc_arg);
265                 if (error) {
266                         splx(s);
267                         return (error);
268                 }
269                 if (KBD_IS_BUSY(kbd)) {
270                         splx(s);
271                         return (EBUSY);
272                 }
273         }
274         KBD_INVALID(kbd);
275         keyboard[kbd->kb_index] = NULL;
276
277         splx(s);
278         return (0);
279 }
280
281 /* find a function table by the driver name */
282 keyboard_switch_t *
283 kbd_get_switch(char *driver)
284 {
285         const keyboard_driver_t **list;
286         const keyboard_driver_t *p;
287
288         SLIST_FOREACH(p, &keyboard_drivers, link) {
289                 if (strcmp(p->name, driver) == 0)
290                         return (p->kbdsw);
291         }
292         SET_FOREACH(list, kbddriver_set) {
293                 p = *list;
294                 if (strcmp(p->name, driver) == 0)
295                         return (p->kbdsw);
296         }
297
298         return (NULL);
299 }
300
301 /*
302  * Keyboard client functions
303  * Keyboard clients, such as the console driver `syscons' and the keyboard
304  * cdev driver, use these functions to claim and release a keyboard for
305  * exclusive use.
306  */
307
308 /*
309  * find the keyboard specified by a driver name and a unit number
310  * starting at given index
311  */
312 int
313 kbd_find_keyboard2(char *driver, int unit, int index)
314 {
315         int i;
316
317         if ((index < 0) || (index >= keyboards))
318                 return (-1);
319
320         for (i = index; i < keyboards; ++i) {
321                 if (keyboard[i] == NULL)
322                         continue;
323                 if (!KBD_IS_VALID(keyboard[i]))
324                         continue;
325                 if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver))
326                         continue;
327                 if ((unit != -1) && (keyboard[i]->kb_unit != unit))
328                         continue;
329                 return (i);
330         }
331
332         return (-1);
333 }
334
335 /* find the keyboard specified by a driver name and a unit number */
336 int
337 kbd_find_keyboard(char *driver, int unit)
338 {
339         return (kbd_find_keyboard2(driver, unit, 0));
340 }
341
342 /* allocate a keyboard */
343 int
344 kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func,
345              void *arg)
346 {
347         int index;
348         int s;
349
350         if (func == NULL)
351                 return (-1);
352
353         s = spltty();
354         index = kbd_find_keyboard(driver, unit);
355         if (index >= 0) {
356                 if (KBD_IS_BUSY(keyboard[index])) {
357                         splx(s);
358                         return (-1);
359                 }
360                 keyboard[index]->kb_token = id;
361                 KBD_BUSY(keyboard[index]);
362                 keyboard[index]->kb_callback.kc_func = func;
363                 keyboard[index]->kb_callback.kc_arg = arg;
364                 kbdd_clear_state(keyboard[index]);
365         }
366         splx(s);
367         return (index);
368 }
369
370 int
371 kbd_release(keyboard_t *kbd, void *id)
372 {
373         int error;
374         int s;
375
376         s = spltty();
377         if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
378                 error = EINVAL;
379         } else if (kbd->kb_token != id) {
380                 error = EPERM;
381         } else {
382                 kbd->kb_token = NULL;
383                 KBD_UNBUSY(kbd);
384                 kbd->kb_callback.kc_func = NULL;
385                 kbd->kb_callback.kc_arg = NULL;
386                 kbdd_clear_state(kbd);
387                 error = 0;
388         }
389         splx(s);
390         return (error);
391 }
392
393 int
394 kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func,
395                     void *arg)
396 {
397         int error;
398         int s;
399
400         s = spltty();
401         if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
402                 error = EINVAL;
403         } else if (kbd->kb_token != id) {
404                 error = EPERM;
405         } else if (func == NULL) {
406                 error = EINVAL;
407         } else {
408                 kbd->kb_callback.kc_func = func;
409                 kbd->kb_callback.kc_arg = arg;
410                 error = 0;
411         }
412         splx(s);
413         return (error);
414 }
415
416 /* get a keyboard structure */
417 keyboard_t *
418 kbd_get_keyboard(int index)
419 {
420         if ((index < 0) || (index >= keyboards))
421                 return (NULL);
422         if (keyboard[index] == NULL)
423                 return (NULL);
424         if (!KBD_IS_VALID(keyboard[index]))
425                 return (NULL);
426         return (keyboard[index]);
427 }
428
429 /*
430  * The back door for the console driver; configure keyboards
431  * This function is for the kernel console to initialize keyboards
432  * at very early stage.
433  */
434
435 int
436 kbd_configure(int flags)
437 {
438         const keyboard_driver_t **list;
439         const keyboard_driver_t *p;
440
441         SLIST_FOREACH(p, &keyboard_drivers, link) {
442                 if (p->configure != NULL)
443                         (*p->configure)(flags);
444         }
445         SET_FOREACH(list, kbddriver_set) {
446                 p = *list;
447                 if (p->configure != NULL)
448                         (*p->configure)(flags);
449         }
450
451         return (0);
452 }
453
454 #ifdef KBD_INSTALL_CDEV
455
456 /*
457  * Virtual keyboard cdev driver functions
458  * The virtual keyboard driver dispatches driver functions to
459  * appropriate subdrivers.
460  */
461
462 #define KBD_UNIT(dev)   dev2unit(dev)
463
464 static d_open_t         genkbdopen;
465 static d_close_t        genkbdclose;
466 static d_read_t         genkbdread;
467 static d_write_t        genkbdwrite;
468 static d_ioctl_t        genkbdioctl;
469 static d_poll_t         genkbdpoll;
470
471
472 static struct cdevsw kbd_cdevsw = {
473         .d_version =    D_VERSION,
474         .d_flags =      D_NEEDGIANT,
475         .d_open =       genkbdopen,
476         .d_close =      genkbdclose,
477         .d_read =       genkbdread,
478         .d_write =      genkbdwrite,
479         .d_ioctl =      genkbdioctl,
480         .d_poll =       genkbdpoll,
481         .d_name =       "kbd",
482 };
483
484 int
485 kbd_attach(keyboard_t *kbd)
486 {
487
488         if (kbd->kb_index >= keyboards)
489                 return (EINVAL);
490         if (keyboard[kbd->kb_index] != kbd)
491                 return (EINVAL);
492
493         kbd->kb_dev = make_dev(&kbd_cdevsw, kbd->kb_index, UID_ROOT, GID_WHEEL,
494             0600, "%s%r", kbd->kb_name, kbd->kb_unit);
495         make_dev_alias(kbd->kb_dev, "kbd%r", kbd->kb_index);
496         kbd->kb_dev->si_drv1 = malloc(sizeof(genkbd_softc_t), M_DEVBUF,
497             M_WAITOK | M_ZERO);
498         printf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
499         return (0);
500 }
501
502 int
503 kbd_detach(keyboard_t *kbd)
504 {
505
506         if (kbd->kb_index >= keyboards)
507                 return (EINVAL);
508         if (keyboard[kbd->kb_index] != kbd)
509                 return (EINVAL);
510
511         free(kbd->kb_dev->si_drv1, M_DEVBUF);
512         destroy_dev(kbd->kb_dev);
513
514         return (0);
515 }
516
517 /*
518  * Generic keyboard cdev driver functions
519  * Keyboard subdrivers may call these functions to implement common
520  * driver functions.
521  */
522
523 static void
524 genkbd_putc(genkbd_softc_t *sc, char c)
525 {
526         unsigned int p;
527
528         if (sc->gkb_q_length == KB_QSIZE)
529                 return;
530
531         p = (sc->gkb_q_start + sc->gkb_q_length) % KB_QSIZE;
532         sc->gkb_q[p] = c;
533         sc->gkb_q_length++;
534 }
535
536 static size_t
537 genkbd_getc(genkbd_softc_t *sc, char *buf, size_t len)
538 {
539
540         /* Determine copy size. */
541         if (sc->gkb_q_length == 0)
542                 return (0);
543         if (len >= sc->gkb_q_length)
544                 len = sc->gkb_q_length;
545         if (len >= KB_QSIZE - sc->gkb_q_start)
546                 len = KB_QSIZE - sc->gkb_q_start;
547
548         /* Copy out data and progress offset. */
549         memcpy(buf, sc->gkb_q + sc->gkb_q_start, len);
550         sc->gkb_q_start = (sc->gkb_q_start + len) % KB_QSIZE;
551         sc->gkb_q_length -= len;
552
553         return (len);
554 }
555
556 static kbd_callback_func_t genkbd_event;
557
558 static int
559 genkbdopen(struct cdev *dev, int mode, int flag, struct thread *td)
560 {
561         keyboard_t *kbd;
562         genkbd_softc_t *sc;
563         int s;
564         int i;
565
566         s = spltty();
567         sc = dev->si_drv1;
568         kbd = kbd_get_keyboard(KBD_INDEX(dev));
569         if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
570                 splx(s);
571                 return (ENXIO);
572         }
573         i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
574             genkbd_event, (void *)sc);
575         if (i < 0) {
576                 splx(s);
577                 return (EBUSY);
578         }
579         /* assert(i == kbd->kb_index) */
580         /* assert(kbd == kbd_get_keyboard(i)) */
581
582         /*
583          * NOTE: even when we have successfully claimed a keyboard,
584          * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
585          */
586
587         sc->gkb_q_length = 0;
588         splx(s);
589
590         return (0);
591 }
592
593 static int
594 genkbdclose(struct cdev *dev, int mode, int flag, struct thread *td)
595 {
596         keyboard_t *kbd;
597         genkbd_softc_t *sc;
598         int s;
599
600         /*
601          * NOTE: the device may have already become invalid.
602          * kbd == NULL || !KBD_IS_VALID(kbd)
603          */
604         s = spltty();
605         sc = dev->si_drv1;
606         kbd = kbd_get_keyboard(KBD_INDEX(dev));
607         if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
608                 /* XXX: we shall be forgiving and don't report error... */
609         } else {
610                 kbd_release(kbd, (void *)sc);
611         }
612         splx(s);
613         return (0);
614 }
615
616 static int
617 genkbdread(struct cdev *dev, struct uio *uio, int flag)
618 {
619         keyboard_t *kbd;
620         genkbd_softc_t *sc;
621         u_char buffer[KB_BUFSIZE];
622         int len;
623         int error;
624         int s;
625
626         /* wait for input */
627         s = spltty();
628         sc = dev->si_drv1;
629         kbd = kbd_get_keyboard(KBD_INDEX(dev));
630         if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
631                 splx(s);
632                 return (ENXIO);
633         }
634         while (sc->gkb_q_length == 0) {
635                 if (flag & O_NONBLOCK) {
636                         splx(s);
637                         return (EWOULDBLOCK);
638                 }
639                 sc->gkb_flags |= KB_ASLEEP;
640                 error = tsleep(sc, PZERO | PCATCH, "kbdrea", 0);
641                 kbd = kbd_get_keyboard(KBD_INDEX(dev));
642                 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
643                         splx(s);
644                         return (ENXIO); /* our keyboard has gone... */
645                 }
646                 if (error) {
647                         sc->gkb_flags &= ~KB_ASLEEP;
648                         splx(s);
649                         return (error);
650                 }
651         }
652         splx(s);
653
654         /* copy as much input as possible */
655         error = 0;
656         while (uio->uio_resid > 0) {
657                 len = imin(uio->uio_resid, sizeof(buffer));
658                 len = genkbd_getc(sc, buffer, len);
659                 if (len <= 0)
660                         break;
661                 error = uiomove(buffer, len, uio);
662                 if (error)
663                         break;
664         }
665
666         return (error);
667 }
668
669 static int
670 genkbdwrite(struct cdev *dev, struct uio *uio, int flag)
671 {
672         keyboard_t *kbd;
673
674         kbd = kbd_get_keyboard(KBD_INDEX(dev));
675         if ((kbd == NULL) || !KBD_IS_VALID(kbd))
676                 return (ENXIO);
677         return (ENODEV);
678 }
679
680 static int
681 genkbdioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
682 {
683         keyboard_t *kbd;
684         int error;
685
686         kbd = kbd_get_keyboard(KBD_INDEX(dev));
687         if ((kbd == NULL) || !KBD_IS_VALID(kbd))
688                 return (ENXIO);
689         error = kbdd_ioctl(kbd, cmd, arg);
690         if (error == ENOIOCTL)
691                 error = ENODEV;
692         return (error);
693 }
694
695 static int
696 genkbdpoll(struct cdev *dev, int events, struct thread *td)
697 {
698         keyboard_t *kbd;
699         genkbd_softc_t *sc;
700         int revents;
701         int s;
702
703         revents = 0;
704         s = spltty();
705         sc = dev->si_drv1;
706         kbd = kbd_get_keyboard(KBD_INDEX(dev));
707         if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
708                 revents =  POLLHUP;     /* the keyboard has gone */
709         } else if (events & (POLLIN | POLLRDNORM)) {
710                 if (sc->gkb_q_length > 0)
711                         revents = events & (POLLIN | POLLRDNORM);
712                 else
713                         selrecord(td, &sc->gkb_rsel);
714         }
715         splx(s);
716         return (revents);
717 }
718
719 static int
720 genkbd_event(keyboard_t *kbd, int event, void *arg)
721 {
722         genkbd_softc_t *sc;
723         size_t len;
724         u_char *cp;
725         int mode;
726         u_int c;
727
728         /* assert(KBD_IS_VALID(kbd)) */
729         sc = (genkbd_softc_t *)arg;
730
731         switch (event) {
732         case KBDIO_KEYINPUT:
733                 break;
734         case KBDIO_UNLOADING:
735                 /* the keyboard is going... */
736                 kbd_release(kbd, (void *)sc);
737                 if (sc->gkb_flags & KB_ASLEEP) {
738                         sc->gkb_flags &= ~KB_ASLEEP;
739                         wakeup(sc);
740                 }
741                 selwakeuppri(&sc->gkb_rsel, PZERO);
742                 return (0);
743         default:
744                 return (EINVAL);
745         }
746
747         /* obtain the current key input mode */
748         if (kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode))
749                 mode = K_XLATE;
750
751         /* read all pending input */
752         while (kbdd_check_char(kbd)) {
753                 c = kbdd_read_char(kbd, FALSE);
754                 if (c == NOKEY)
755                         continue;
756                 if (c == ERRKEY)        /* XXX: ring bell? */
757                         continue;
758                 if (!KBD_IS_BUSY(kbd))
759                         /* the device is not open, discard the input */
760                         continue;
761
762                 /* store the byte as is for K_RAW and K_CODE modes */
763                 if (mode != K_XLATE) {
764                         genkbd_putc(sc, KEYCHAR(c));
765                         continue;
766                 }
767
768                 /* K_XLATE */
769                 if (c & RELKEY) /* key release is ignored */
770                         continue;
771
772                 /* process special keys; most of them are just ignored... */
773                 if (c & SPCLKEY) {
774                         switch (KEYCHAR(c)) {
775                         default:
776                                 /* ignore them... */
777                                 continue;
778                         case BTAB:      /* a backtab: ESC [ Z */
779                                 genkbd_putc(sc, 0x1b);
780                                 genkbd_putc(sc, '[');
781                                 genkbd_putc(sc, 'Z');
782                                 continue;
783                         }
784                 }
785
786                 /* normal chars, normal chars with the META, function keys */
787                 switch (KEYFLAGS(c)) {
788                 case 0:                 /* a normal char */
789                         genkbd_putc(sc, KEYCHAR(c));
790                         break;
791                 case MKEY:              /* the META flag: prepend ESC */
792                         genkbd_putc(sc, 0x1b);
793                         genkbd_putc(sc, KEYCHAR(c));
794                         break;
795                 case FKEY | SPCLKEY:    /* a function key, return string */
796                         cp = kbdd_get_fkeystr(kbd, KEYCHAR(c), &len);
797                         if (cp != NULL) {
798                                 while (len-- >  0)
799                                         genkbd_putc(sc, *cp++);
800                         }
801                         break;
802                 }
803         }
804
805         /* wake up sleeping/polling processes */
806         if (sc->gkb_q_length > 0) {
807                 if (sc->gkb_flags & KB_ASLEEP) {
808                         sc->gkb_flags &= ~KB_ASLEEP;
809                         wakeup(sc);
810                 }
811                 selwakeuppri(&sc->gkb_rsel, PZERO);
812         }
813
814         return (0);
815 }
816
817 #endif /* KBD_INSTALL_CDEV */
818
819 /*
820  * Generic low-level keyboard functions
821  * The low-level functions in the keyboard subdriver may use these
822  * functions.
823  */
824
825 #ifndef KBD_DISABLE_KEYMAP_LOAD
826 static int key_change_ok(struct keyent_t *, struct keyent_t *, struct thread *);
827 static int keymap_change_ok(keymap_t *, keymap_t *, struct thread *);
828 static int accent_change_ok(accentmap_t *, accentmap_t *, struct thread *);
829 static int fkey_change_ok(fkeytab_t *, fkeyarg_t *, struct thread *);
830 #endif
831
832 int
833 genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
834 {
835         keymap_t *mapp;
836         okeymap_t *omapp;
837         keyarg_t *keyp;
838         fkeyarg_t *fkeyp;
839         int s;
840         int i, j;
841         int error;
842
843         s = spltty();
844         switch (cmd) {
845
846         case KDGKBINFO:         /* get keyboard information */
847                 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
848                 i = imin(strlen(kbd->kb_name) + 1,
849                     sizeof(((keyboard_info_t *)arg)->kb_name));
850                 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
851                 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
852                 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
853                 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
854                 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
855                 break;
856
857         case KDGKBTYPE:         /* get keyboard type */
858                 *(int *)arg = kbd->kb_type;
859                 break;
860
861         case KDGETREPEAT:       /* get keyboard repeat rate */
862                 ((int *)arg)[0] = kbd->kb_delay1;
863                 ((int *)arg)[1] = kbd->kb_delay2;
864                 break;
865
866         case GIO_KEYMAP:        /* get keyboard translation table */
867                 error = copyout(kbd->kb_keymap, *(void **)arg,
868                     sizeof(keymap_t));
869                 splx(s);
870                 return (error);
871         case OGIO_KEYMAP:       /* get keyboard translation table (compat) */
872                 mapp = kbd->kb_keymap;
873                 omapp = (okeymap_t *)arg;
874                 omapp->n_keys = mapp->n_keys;
875                 for (i = 0; i < NUM_KEYS; i++) {
876                         for (j = 0; j < NUM_STATES; j++)
877                                 omapp->key[i].map[j] =
878                                     mapp->key[i].map[j];
879                         omapp->key[i].spcl = mapp->key[i].spcl;
880                         omapp->key[i].flgs = mapp->key[i].flgs;
881                 }
882                 break;
883         case PIO_KEYMAP:        /* set keyboard translation table */
884         case OPIO_KEYMAP:       /* set keyboard translation table (compat) */
885 #ifndef KBD_DISABLE_KEYMAP_LOAD
886                 mapp = malloc(sizeof *mapp, M_TEMP, M_WAITOK);
887                 if (cmd == OPIO_KEYMAP) {
888                         omapp = (okeymap_t *)arg;
889                         mapp->n_keys = omapp->n_keys;
890                         for (i = 0; i < NUM_KEYS; i++) {
891                                 for (j = 0; j < NUM_STATES; j++)
892                                         mapp->key[i].map[j] =
893                                             omapp->key[i].map[j];
894                                 mapp->key[i].spcl = omapp->key[i].spcl;
895                                 mapp->key[i].flgs = omapp->key[i].flgs;
896                         }
897                 } else {
898                         error = copyin(*(void **)arg, mapp, sizeof *mapp);
899                         if (error != 0) {
900                                 splx(s);
901                                 free(mapp, M_TEMP);
902                                 return (error);
903                         }
904                 }
905
906                 error = keymap_change_ok(kbd->kb_keymap, mapp, curthread);
907                 if (error != 0) {
908                         splx(s);
909                         free(mapp, M_TEMP);
910                         return (error);
911                 }
912                 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
913                 bcopy(mapp, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
914                 free(mapp, M_TEMP);
915                 break;
916 #else
917                 splx(s);
918                 return (ENODEV);
919 #endif
920
921         case GIO_KEYMAPENT:     /* get keyboard translation table entry */
922                 keyp = (keyarg_t *)arg;
923                 if (keyp->keynum >= sizeof(kbd->kb_keymap->key) /
924                     sizeof(kbd->kb_keymap->key[0])) {
925                         splx(s);
926                         return (EINVAL);
927                 }
928                 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
929                     sizeof(keyp->key));
930                 break;
931         case PIO_KEYMAPENT:     /* set keyboard translation table entry */
932 #ifndef KBD_DISABLE_KEYMAP_LOAD
933                 keyp = (keyarg_t *)arg;
934                 if (keyp->keynum >= sizeof(kbd->kb_keymap->key) /
935                     sizeof(kbd->kb_keymap->key[0])) {
936                         splx(s);
937                         return (EINVAL);
938                 }
939                 error = key_change_ok(&kbd->kb_keymap->key[keyp->keynum],
940                     &keyp->key, curthread);
941                 if (error != 0) {
942                         splx(s);
943                         return (error);
944                 }
945                 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
946                     sizeof(keyp->key));
947                 break;
948 #else
949                 splx(s);
950                 return (ENODEV);
951 #endif
952
953         case GIO_DEADKEYMAP:    /* get accent key translation table */
954                 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
955                 break;
956         case PIO_DEADKEYMAP:    /* set accent key translation table */
957 #ifndef KBD_DISABLE_KEYMAP_LOAD
958                 error = accent_change_ok(kbd->kb_accentmap,
959                     (accentmap_t *)arg, curthread);
960                 if (error != 0) {
961                         splx(s);
962                         return (error);
963                 }
964                 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
965                 break;
966 #else
967                 splx(s);
968                 return (ENODEV);
969 #endif
970
971         case GETFKEY:           /* get functionkey string */
972                 fkeyp = (fkeyarg_t *)arg;
973                 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
974                         splx(s);
975                         return (EINVAL);
976                 }
977                 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
978                     kbd->kb_fkeytab[fkeyp->keynum].len);
979                 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
980                 break;
981         case SETFKEY:           /* set functionkey string */
982 #ifndef KBD_DISABLE_KEYMAP_LOAD
983                 fkeyp = (fkeyarg_t *)arg;
984                 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
985                         splx(s);
986                         return (EINVAL);
987                 }
988                 error = fkey_change_ok(&kbd->kb_fkeytab[fkeyp->keynum],
989                     fkeyp, curthread);
990                 if (error != 0) {
991                         splx(s);
992                         return (error);
993                 }
994                 kbd->kb_fkeytab[fkeyp->keynum].len = min(fkeyp->flen, MAXFK);
995                 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
996                     kbd->kb_fkeytab[fkeyp->keynum].len);
997                 break;
998 #else
999                 splx(s);
1000                 return (ENODEV);
1001 #endif
1002
1003         default:
1004                 splx(s);
1005                 return (ENOIOCTL);
1006         }
1007
1008         splx(s);
1009         return (0);
1010 }
1011
1012 #ifndef KBD_DISABLE_KEYMAP_LOAD
1013 #define RESTRICTED_KEY(key, i) \
1014         ((key->spcl & (0x80 >> i)) && \
1015                 (key->map[i] == RBT || key->map[i] == SUSP || \
1016                  key->map[i] == STBY || key->map[i] == DBG || \
1017                  key->map[i] == PNC || key->map[i] == HALT || \
1018                  key->map[i] == PDWN))
1019
1020 static int
1021 key_change_ok(struct keyent_t *oldkey, struct keyent_t *newkey, struct thread *td)
1022 {
1023         int i;
1024
1025         /* Low keymap_restrict_change means any changes are OK. */
1026         if (keymap_restrict_change <= 0)
1027                 return (0);
1028
1029         /* High keymap_restrict_change means only root can change the keymap. */
1030         if (keymap_restrict_change >= 2) {
1031                 for (i = 0; i < NUM_STATES; i++)
1032                         if (oldkey->map[i] != newkey->map[i])
1033                                 return priv_check(td, PRIV_KEYBOARD);
1034                 if (oldkey->spcl != newkey->spcl)
1035                         return priv_check(td, PRIV_KEYBOARD);
1036                 if (oldkey->flgs != newkey->flgs)
1037                         return priv_check(td, PRIV_KEYBOARD);
1038                 return (0);
1039         }
1040
1041         /* Otherwise we have to see if any special keys are being changed. */
1042         for (i = 0; i < NUM_STATES; i++) {
1043                 /*
1044                  * If either the oldkey or the newkey action is restricted
1045                  * then we must make sure that the action doesn't change.
1046                  */
1047                 if (!RESTRICTED_KEY(oldkey, i) && !RESTRICTED_KEY(newkey, i))
1048                         continue;
1049                 if ((oldkey->spcl & (0x80 >> i)) == (newkey->spcl & (0x80 >> i))
1050                     && oldkey->map[i] == newkey->map[i])
1051                         continue;
1052                 return priv_check(td, PRIV_KEYBOARD);
1053         }
1054
1055         return (0);
1056 }
1057
1058 static int
1059 keymap_change_ok(keymap_t *oldmap, keymap_t *newmap, struct thread *td)
1060 {
1061         int keycode, error;
1062
1063         for (keycode = 0; keycode < NUM_KEYS; keycode++) {
1064                 if ((error = key_change_ok(&oldmap->key[keycode],
1065                     &newmap->key[keycode], td)) != 0)
1066                         return (error);
1067         }
1068         return (0);
1069 }
1070
1071 static int
1072 accent_change_ok(accentmap_t *oldmap, accentmap_t *newmap, struct thread *td)
1073 {
1074         struct acc_t *oldacc, *newacc;
1075         int accent, i;
1076
1077         if (keymap_restrict_change <= 2)
1078                 return (0);
1079
1080         if (oldmap->n_accs != newmap->n_accs)
1081                 return priv_check(td, PRIV_KEYBOARD);
1082
1083         for (accent = 0; accent < oldmap->n_accs; accent++) {
1084                 oldacc = &oldmap->acc[accent];
1085                 newacc = &newmap->acc[accent];
1086                 if (oldacc->accchar != newacc->accchar)
1087                         return priv_check(td, PRIV_KEYBOARD);
1088                 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1089                         if (oldacc->map[i][0] != newacc->map[i][0])
1090                                 return priv_check(td, PRIV_KEYBOARD);
1091                         if (oldacc->map[i][0] == 0)     /* end of table */
1092                                 break;
1093                         if (oldacc->map[i][1] != newacc->map[i][1])
1094                                 return priv_check(td, PRIV_KEYBOARD);
1095                 }
1096         }
1097
1098         return (0);
1099 }
1100
1101 static int
1102 fkey_change_ok(fkeytab_t *oldkey, fkeyarg_t *newkey, struct thread *td)
1103 {
1104         if (keymap_restrict_change <= 3)
1105                 return (0);
1106
1107         if (oldkey->len != newkey->flen ||
1108             bcmp(oldkey->str, newkey->keydef, oldkey->len) != 0)
1109                 return priv_check(td, PRIV_KEYBOARD);
1110
1111         return (0);
1112 }
1113 #endif
1114
1115 /* get a pointer to the string associated with the given function key */
1116 static u_char *
1117 genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
1118 {
1119         if (kbd == NULL)
1120                 return (NULL);
1121         fkey -= F_FN;
1122         if (fkey > kbd->kb_fkeytab_size)
1123                 return (NULL);
1124         *len = kbd->kb_fkeytab[fkey].len;
1125         return (kbd->kb_fkeytab[fkey].str);
1126 }
1127
1128 /* diagnostic dump */
1129 static char *
1130 get_kbd_type_name(int type)
1131 {
1132         static struct {
1133                 int type;
1134                 char *name;
1135         } name_table[] = {
1136                 { KB_84,        "AT 84" },
1137                 { KB_101,       "AT 101/102" },
1138                 { KB_OTHER,     "generic" },
1139         };
1140         int i;
1141
1142         for (i = 0; i < nitems(name_table); ++i) {
1143                 if (type == name_table[i].type)
1144                         return (name_table[i].name);
1145         }
1146         return ("unknown");
1147 }
1148
1149 static void
1150 genkbd_diag(keyboard_t *kbd, int level)
1151 {
1152         if (level > 0) {
1153                 printf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
1154                     kbd->kb_index, kbd->kb_name, kbd->kb_unit,
1155                     get_kbd_type_name(kbd->kb_type), kbd->kb_type,
1156                     kbd->kb_config, kbd->kb_flags);
1157                 if (kbd->kb_io_base > 0)
1158                         printf(", port:0x%x-0x%x", kbd->kb_io_base,
1159                             kbd->kb_io_base + kbd->kb_io_size - 1);
1160                 printf("\n");
1161         }
1162 }
1163
1164 #define set_lockkey_state(k, s, l)                              \
1165         if (!((s) & l ## DOWN)) {                               \
1166                 int i;                                          \
1167                 (s) |= l ## DOWN;                               \
1168                 (s) ^= l ## ED;                                 \
1169                 i = (s) & LOCK_MASK;                            \
1170                 (void)kbdd_ioctl((k), KDSETLED, (caddr_t)&i);   \
1171         }
1172
1173 static u_int
1174 save_accent_key(keyboard_t *kbd, u_int key, int *accents)
1175 {
1176         int i;
1177
1178         /* make an index into the accent map */
1179         i = key - F_ACC + 1;
1180         if ((i > kbd->kb_accentmap->n_accs)
1181             || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
1182                 /* the index is out of range or pointing to an empty entry */
1183                 *accents = 0;
1184                 return (ERRKEY);
1185         }
1186
1187         /*
1188          * If the same accent key has been hit twice, produce the accent
1189          * char itself.
1190          */
1191         if (i == *accents) {
1192                 key = kbd->kb_accentmap->acc[i - 1].accchar;
1193                 *accents = 0;
1194                 return (key);
1195         }
1196
1197         /* remember the index and wait for the next key  */
1198         *accents = i;
1199         return (NOKEY);
1200 }
1201
1202 static u_int
1203 make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
1204 {
1205         struct acc_t *acc;
1206         int i;
1207
1208         acc = &kbd->kb_accentmap->acc[*accents - 1];
1209         *accents = 0;
1210
1211         /*
1212          * If the accent key is followed by the space key,
1213          * produce the accent char itself.
1214          */
1215         if (ch == ' ')
1216                 return (acc->accchar);
1217
1218         /* scan the accent map */
1219         for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1220                 if (acc->map[i][0] == 0)        /* end of table */
1221                         break;
1222                 if (acc->map[i][0] == ch)
1223                         return (acc->map[i][1]);
1224         }
1225         /* this char cannot be accented... */
1226         return (ERRKEY);
1227 }
1228
1229 int
1230 genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1231                  int *accents)
1232 {
1233         struct keyent_t *key;
1234         int state = *shiftstate;
1235         int action;
1236         int f;
1237         int i;
1238
1239         i = keycode;
1240         f = state & (AGRS | ALKED);
1241         if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1242                 i += ALTGR_OFFSET;
1243         key = &kbd->kb_keymap->key[i];
1244         i = ((state & SHIFTS) ? 1 : 0)
1245             | ((state & CTLS) ? 2 : 0)
1246             | ((state & ALTS) ? 4 : 0);
1247         if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1248                 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1249                 i ^= 1;
1250
1251         if (up) {       /* break: key released */
1252                 action = kbd->kb_lastact[keycode];
1253                 kbd->kb_lastact[keycode] = NOP;
1254                 switch (action) {
1255                 case LSHA:
1256                         if (state & SHIFTAON) {
1257                                 set_lockkey_state(kbd, state, ALK);
1258                                 state &= ~ALKDOWN;
1259                         }
1260                         action = LSH;
1261                         /* FALL THROUGH */
1262                 case LSH:
1263                         state &= ~SHIFTS1;
1264                         break;
1265                 case RSHA:
1266                         if (state & SHIFTAON) {
1267                                 set_lockkey_state(kbd, state, ALK);
1268                                 state &= ~ALKDOWN;
1269                         }
1270                         action = RSH;
1271                         /* FALL THROUGH */
1272                 case RSH:
1273                         state &= ~SHIFTS2;
1274                         break;
1275                 case LCTRA:
1276                         if (state & SHIFTAON) {
1277                                 set_lockkey_state(kbd, state, ALK);
1278                                 state &= ~ALKDOWN;
1279                         }
1280                         action = LCTR;
1281                         /* FALL THROUGH */
1282                 case LCTR:
1283                         state &= ~CTLS1;
1284                         break;
1285                 case RCTRA:
1286                         if (state & SHIFTAON) {
1287                                 set_lockkey_state(kbd, state, ALK);
1288                                 state &= ~ALKDOWN;
1289                         }
1290                         action = RCTR;
1291                         /* FALL THROUGH */
1292                 case RCTR:
1293                         state &= ~CTLS2;
1294                         break;
1295                 case LALTA:
1296                         if (state & SHIFTAON) {
1297                                 set_lockkey_state(kbd, state, ALK);
1298                                 state &= ~ALKDOWN;
1299                         }
1300                         action = LALT;
1301                         /* FALL THROUGH */
1302                 case LALT:
1303                         state &= ~ALTS1;
1304                         break;
1305                 case RALTA:
1306                         if (state & SHIFTAON) {
1307                                 set_lockkey_state(kbd, state, ALK);
1308                                 state &= ~ALKDOWN;
1309                         }
1310                         action = RALT;
1311                         /* FALL THROUGH */
1312                 case RALT:
1313                         state &= ~ALTS2;
1314                         break;
1315                 case ASH:
1316                         state &= ~AGRS1;
1317                         break;
1318                 case META:
1319                         state &= ~METAS1;
1320                         break;
1321                 case NLK:
1322                         state &= ~NLKDOWN;
1323                         break;
1324                 case CLK:
1325                         state &= ~CLKDOWN;
1326                         break;
1327                 case SLK:
1328                         state &= ~SLKDOWN;
1329                         break;
1330                 case ALK:
1331                         state &= ~ALKDOWN;
1332                         break;
1333                 case NOP:
1334                         /* release events of regular keys are not reported */
1335                         *shiftstate &= ~SHIFTAON;
1336                         return (NOKEY);
1337                 }
1338                 *shiftstate = state & ~SHIFTAON;
1339                 return (SPCLKEY | RELKEY | action);
1340         } else {        /* make: key pressed */
1341                 action = key->map[i];
1342                 state &= ~SHIFTAON;
1343                 if (key->spcl & (0x80 >> i)) {
1344                         /* special keys */
1345                         if (kbd->kb_lastact[keycode] == NOP)
1346                                 kbd->kb_lastact[keycode] = action;
1347                         if (kbd->kb_lastact[keycode] != action)
1348                                 action = NOP;
1349                         switch (action) {
1350                         /* LOCKING KEYS */
1351                         case NLK:
1352                                 set_lockkey_state(kbd, state, NLK);
1353                                 break;
1354                         case CLK:
1355                                 set_lockkey_state(kbd, state, CLK);
1356                                 break;
1357                         case SLK:
1358                                 set_lockkey_state(kbd, state, SLK);
1359                                 break;
1360                         case ALK:
1361                                 set_lockkey_state(kbd, state, ALK);
1362                                 break;
1363                         /* NON-LOCKING KEYS */
1364                         case SPSC: case RBT:  case SUSP: case STBY:
1365                         case DBG:  case NEXT: case PREV: case PNC:
1366                         case HALT: case PDWN:
1367                                 *accents = 0;
1368                                 break;
1369                         case BTAB:
1370                                 *accents = 0;
1371                                 action |= BKEY;
1372                                 break;
1373                         case LSHA:
1374                                 state |= SHIFTAON;
1375                                 action = LSH;
1376                                 /* FALL THROUGH */
1377                         case LSH:
1378                                 state |= SHIFTS1;
1379                                 break;
1380                         case RSHA:
1381                                 state |= SHIFTAON;
1382                                 action = RSH;
1383                                 /* FALL THROUGH */
1384                         case RSH:
1385                                 state |= SHIFTS2;
1386                                 break;
1387                         case LCTRA:
1388                                 state |= SHIFTAON;
1389                                 action = LCTR;
1390                                 /* FALL THROUGH */
1391                         case LCTR:
1392                                 state |= CTLS1;
1393                                 break;
1394                         case RCTRA:
1395                                 state |= SHIFTAON;
1396                                 action = RCTR;
1397                                 /* FALL THROUGH */
1398                         case RCTR:
1399                                 state |= CTLS2;
1400                                 break;
1401                         case LALTA:
1402                                 state |= SHIFTAON;
1403                                 action = LALT;
1404                                 /* FALL THROUGH */
1405                         case LALT:
1406                                 state |= ALTS1;
1407                                 break;
1408                         case RALTA:
1409                                 state |= SHIFTAON;
1410                                 action = RALT;
1411                                 /* FALL THROUGH */
1412                         case RALT:
1413                                 state |= ALTS2;
1414                                 break;
1415                         case ASH:
1416                                 state |= AGRS1;
1417                                 break;
1418                         case META:
1419                                 state |= METAS1;
1420                                 break;
1421                         case NOP:
1422                                 *shiftstate = state;
1423                                 return (NOKEY);
1424                         default:
1425                                 /* is this an accent (dead) key? */
1426                                 *shiftstate = state;
1427                                 if (action >= F_ACC && action <= L_ACC) {
1428                                         action = save_accent_key(kbd, action,
1429                                                                  accents);
1430                                         switch (action) {
1431                                         case NOKEY:
1432                                         case ERRKEY:
1433                                                 return (action);
1434                                         default:
1435                                                 if (state & METAS)
1436                                                         return (action | MKEY);
1437                                                 else
1438                                                         return (action);
1439                                         }
1440                                         /* NOT REACHED */
1441                                 }
1442                                 /* other special keys */
1443                                 if (*accents > 0) {
1444                                         *accents = 0;
1445                                         return (ERRKEY);
1446                                 }
1447                                 if (action >= F_FN && action <= L_FN)
1448                                         action |= FKEY;
1449                                 /* XXX: return fkey string for the FKEY? */
1450                                 return (SPCLKEY | action);
1451                         }
1452                         *shiftstate = state;
1453                         return (SPCLKEY | action);
1454                 } else {
1455                         /* regular keys */
1456                         kbd->kb_lastact[keycode] = NOP;
1457                         *shiftstate = state;
1458                         if (*accents > 0) {
1459                                 /* make an accented char */
1460                                 action = make_accent_char(kbd, action, accents);
1461                                 if (action == ERRKEY)
1462                                         return (action);
1463                         }
1464                         if (state & METAS)
1465                                 action |= MKEY;
1466                         return (action);
1467                 }
1468         }
1469         /* NOT REACHED */
1470 }
1471
1472 void
1473 kbd_ev_event(keyboard_t *kbd, uint16_t type, uint16_t code, int32_t value)
1474 {
1475         int delay[2], led = 0, leds, oleds;
1476
1477         if (type == EV_LED) {
1478                 leds = oleds = KBD_LED_VAL(kbd);
1479                 switch (code) {
1480                 case LED_CAPSL:
1481                         led = CLKED;
1482                         break;
1483                 case LED_NUML:
1484                         led = NLKED;
1485                         break;
1486                 case LED_SCROLLL:
1487                         led = SLKED;
1488                         break;
1489                 }
1490
1491                 if (value)
1492                         leds |= led;
1493                 else
1494                         leds &= ~led;
1495
1496                 if (leds != oleds)
1497                         kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
1498
1499         } else if (type == EV_REP && code == REP_DELAY) {
1500                 delay[0] = value;
1501                 delay[1] = kbd->kb_delay2;
1502                 kbdd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
1503         } else if (type == EV_REP && code == REP_PERIOD) {
1504                 delay[0] = kbd->kb_delay1;
1505                 delay[1] = value;
1506                 kbdd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
1507         }
1508 }