2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
40 #include <sys/fcntl.h>
44 #include <sys/selinfo.h>
45 #include <sys/sysctl.h>
50 #include <dev/evdev/input-event-codes.h>
51 #include <dev/kbd/kbdreg.h>
53 #define KBD_INDEX(dev) dev2unit(dev)
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;
67 static SLIST_HEAD(, keyboard_driver) keyboard_drivers =
68 SLIST_HEAD_INITIALIZER(keyboard_drivers);
70 SET_DECLARE(kbddriver_set, const keyboard_driver_t);
75 * We need at least one entry each in order to initialize a keyboard
76 * for the kernel console. The arrays will be increased dynamically
80 static int keyboards = 1;
81 static keyboard_t *kbd_ini;
82 static keyboard_t **keyboard = &kbd_ini;
83 static keyboard_switch_t *kbdsw_ini;
84 keyboard_switch_t **kbdsw = &kbdsw_ini;
86 static int keymap_restrict_change;
87 static SYSCTL_NODE(_hw, OID_AUTO, kbd, CTLFLAG_RD, 0, "kbd");
88 SYSCTL_INT(_hw_kbd, OID_AUTO, keymap_restrict_change, CTLFLAG_RW,
89 &keymap_restrict_change, 0, "restrict ability to change keymap");
94 kbd_realloc_array(void)
97 keyboard_switch_t **new_kbdsw;
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) {
108 new_kbdsw = malloc(sizeof(*new_kbdsw)*newsize, M_DEVBUF,
110 if (new_kbdsw == NULL) {
111 free(new_kbd, M_DEVBUF);
115 bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
116 bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards);
118 free(keyboard, M_DEVBUF);
119 free(kbdsw, M_DEVBUF);
127 printf("kbd: new array size %d\n", keyboards);
133 * Low-level keyboard driver functions
134 * Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard
135 * driver, call these functions to initialize the keyboard_t structure
136 * and register it to the virtual keyboard driver `kbd'.
139 /* initialize the keyboard_t structure */
141 kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config,
142 int port, int port_size)
144 kbd->kb_flags = KB_NO_DEVICE; /* device has not been found */
148 kbd->kb_config = config & ~KB_CONF_PROBE_ONLY;
149 kbd->kb_led = 0; /* unknown */
150 kbd->kb_io_base = port;
151 kbd->kb_io_size = port_size;
153 kbd->kb_keymap = NULL;
154 kbd->kb_accentmap = NULL;
155 kbd->kb_fkeytab = NULL;
156 kbd->kb_fkeytab_size = 0;
157 kbd->kb_delay1 = KB_DELAY1; /* these values are advisory only */
158 kbd->kb_delay2 = KB_DELAY2;
160 bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact));
164 kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap,
165 fkeytab_t *fkeymap, int fkeymap_size)
167 kbd->kb_keymap = keymap;
168 kbd->kb_accentmap = accmap;
169 kbd->kb_fkeytab = fkeymap;
170 kbd->kb_fkeytab_size = fkeymap_size;
173 /* declare a new keyboard driver */
175 kbd_add_driver(keyboard_driver_t *driver)
177 if (SLIST_NEXT(driver, link))
179 SLIST_INSERT_HEAD(&keyboard_drivers, driver, link);
184 kbd_delete_driver(keyboard_driver_t *driver)
186 SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link);
187 SLIST_NEXT(driver, link) = NULL;
191 /* register a keyboard and associate it with a function table */
193 kbd_register(keyboard_t *kbd)
195 const keyboard_driver_t **list;
196 const keyboard_driver_t *p;
201 mux = kbd_get_keyboard(kbd_find_keyboard("kbdmux", -1));
203 for (index = 0; index < keyboards; ++index) {
204 if (keyboard[index] == NULL)
207 if (index >= keyboards) {
208 if (kbd_realloc_array())
212 kbd->kb_index = index;
215 kbd->kb_active = 0; /* disabled until someone calls kbd_enable() */
216 kbd->kb_token = NULL;
217 kbd->kb_callback.kc_func = NULL;
218 kbd->kb_callback.kc_arg = NULL;
220 SLIST_FOREACH(p, &keyboard_drivers, link) {
221 if (strcmp(p->name, kbd->kb_name) == 0) {
222 keyboard[index] = kbd;
223 kbdsw[index] = p->kbdsw;
226 bzero(&ki, sizeof(ki));
227 strcpy(ki.kb_name, kbd->kb_name);
228 ki.kb_unit = kbd->kb_unit;
230 (void)kbdd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
236 SET_FOREACH(list, kbddriver_set) {
238 if (strcmp(p->name, kbd->kb_name) == 0) {
239 keyboard[index] = kbd;
240 kbdsw[index] = p->kbdsw;
243 bzero(&ki, sizeof(ki));
244 strcpy(ki.kb_name, kbd->kb_name);
245 ki.kb_unit = kbd->kb_unit;
247 (void)kbdd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
258 kbd_unregister(keyboard_t *kbd)
263 if ((kbd->kb_index < 0) || (kbd->kb_index >= keyboards))
265 if (keyboard[kbd->kb_index] != kbd)
269 if (KBD_IS_BUSY(kbd)) {
270 error = (*kbd->kb_callback.kc_func)(kbd, KBDIO_UNLOADING,
271 kbd->kb_callback.kc_arg);
276 if (KBD_IS_BUSY(kbd)) {
282 keyboard[kbd->kb_index] = NULL;
283 kbdsw[kbd->kb_index] = NULL;
289 /* find a function table by the driver name */
291 kbd_get_switch(char *driver)
293 const keyboard_driver_t **list;
294 const keyboard_driver_t *p;
296 SLIST_FOREACH(p, &keyboard_drivers, link) {
297 if (strcmp(p->name, driver) == 0)
300 SET_FOREACH(list, kbddriver_set) {
302 if (strcmp(p->name, driver) == 0)
310 * Keyboard client functions
311 * Keyboard clients, such as the console driver `syscons' and the keyboard
312 * cdev driver, use these functions to claim and release a keyboard for
317 * find the keyboard specified by a driver name and a unit number
318 * starting at given index
321 kbd_find_keyboard2(char *driver, int unit, int index)
325 if ((index < 0) || (index >= keyboards))
328 for (i = index; i < keyboards; ++i) {
329 if (keyboard[i] == NULL)
331 if (!KBD_IS_VALID(keyboard[i]))
333 if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver))
335 if ((unit != -1) && (keyboard[i]->kb_unit != unit))
343 /* find the keyboard specified by a driver name and a unit number */
345 kbd_find_keyboard(char *driver, int unit)
347 return (kbd_find_keyboard2(driver, unit, 0));
350 /* allocate a keyboard */
352 kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func,
362 index = kbd_find_keyboard(driver, unit);
364 if (KBD_IS_BUSY(keyboard[index])) {
368 keyboard[index]->kb_token = id;
369 KBD_BUSY(keyboard[index]);
370 keyboard[index]->kb_callback.kc_func = func;
371 keyboard[index]->kb_callback.kc_arg = arg;
372 kbdd_clear_state(keyboard[index]);
379 kbd_release(keyboard_t *kbd, void *id)
385 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
387 } else if (kbd->kb_token != id) {
390 kbd->kb_token = NULL;
392 kbd->kb_callback.kc_func = NULL;
393 kbd->kb_callback.kc_arg = NULL;
394 kbdd_clear_state(kbd);
402 kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func,
409 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
411 } else if (kbd->kb_token != id) {
413 } else if (func == NULL) {
416 kbd->kb_callback.kc_func = func;
417 kbd->kb_callback.kc_arg = arg;
424 /* get a keyboard structure */
426 kbd_get_keyboard(int index)
428 if ((index < 0) || (index >= keyboards))
430 if (keyboard[index] == NULL)
432 if (!KBD_IS_VALID(keyboard[index]))
434 return (keyboard[index]);
438 * The back door for the console driver; configure keyboards
439 * This function is for the kernel console to initialize keyboards
440 * at very early stage.
444 kbd_configure(int flags)
446 const keyboard_driver_t **list;
447 const keyboard_driver_t *p;
449 SLIST_FOREACH(p, &keyboard_drivers, link) {
450 if (p->configure != NULL)
451 (*p->configure)(flags);
453 SET_FOREACH(list, kbddriver_set) {
455 if (p->configure != NULL)
456 (*p->configure)(flags);
462 #ifdef KBD_INSTALL_CDEV
465 * Virtual keyboard cdev driver functions
466 * The virtual keyboard driver dispatches driver functions to
467 * appropriate subdrivers.
470 #define KBD_UNIT(dev) dev2unit(dev)
472 static d_open_t genkbdopen;
473 static d_close_t genkbdclose;
474 static d_read_t genkbdread;
475 static d_write_t genkbdwrite;
476 static d_ioctl_t genkbdioctl;
477 static d_poll_t genkbdpoll;
480 static struct cdevsw kbd_cdevsw = {
481 .d_version = D_VERSION,
482 .d_flags = D_NEEDGIANT,
483 .d_open = genkbdopen,
484 .d_close = genkbdclose,
485 .d_read = genkbdread,
486 .d_write = genkbdwrite,
487 .d_ioctl = genkbdioctl,
488 .d_poll = genkbdpoll,
493 kbd_attach(keyboard_t *kbd)
496 if (kbd->kb_index >= keyboards)
498 if (keyboard[kbd->kb_index] != kbd)
501 kbd->kb_dev = make_dev(&kbd_cdevsw, kbd->kb_index, UID_ROOT, GID_WHEEL,
502 0600, "%s%r", kbd->kb_name, kbd->kb_unit);
503 make_dev_alias(kbd->kb_dev, "kbd%r", kbd->kb_index);
504 kbd->kb_dev->si_drv1 = malloc(sizeof(genkbd_softc_t), M_DEVBUF,
506 printf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
511 kbd_detach(keyboard_t *kbd)
514 if (kbd->kb_index >= keyboards)
516 if (keyboard[kbd->kb_index] != kbd)
519 free(kbd->kb_dev->si_drv1, M_DEVBUF);
520 destroy_dev(kbd->kb_dev);
526 * Generic keyboard cdev driver functions
527 * Keyboard subdrivers may call these functions to implement common
532 genkbd_putc(genkbd_softc_t *sc, char c)
536 if (sc->gkb_q_length == KB_QSIZE)
539 p = (sc->gkb_q_start + sc->gkb_q_length) % KB_QSIZE;
545 genkbd_getc(genkbd_softc_t *sc, char *buf, size_t len)
548 /* Determine copy size. */
549 if (sc->gkb_q_length == 0)
551 if (len >= sc->gkb_q_length)
552 len = sc->gkb_q_length;
553 if (len >= KB_QSIZE - sc->gkb_q_start)
554 len = KB_QSIZE - sc->gkb_q_start;
556 /* Copy out data and progress offset. */
557 memcpy(buf, sc->gkb_q + sc->gkb_q_start, len);
558 sc->gkb_q_start = (sc->gkb_q_start + len) % KB_QSIZE;
559 sc->gkb_q_length -= len;
564 static kbd_callback_func_t genkbd_event;
567 genkbdopen(struct cdev *dev, int mode, int flag, struct thread *td)
576 kbd = kbd_get_keyboard(KBD_INDEX(dev));
577 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
581 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
582 genkbd_event, (void *)sc);
587 /* assert(i == kbd->kb_index) */
588 /* assert(kbd == kbd_get_keyboard(i)) */
591 * NOTE: even when we have successfully claimed a keyboard,
592 * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
595 sc->gkb_q_length = 0;
602 genkbdclose(struct cdev *dev, int mode, int flag, struct thread *td)
609 * NOTE: the device may have already become invalid.
610 * kbd == NULL || !KBD_IS_VALID(kbd)
614 kbd = kbd_get_keyboard(KBD_INDEX(dev));
615 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
616 /* XXX: we shall be forgiving and don't report error... */
618 kbd_release(kbd, (void *)sc);
625 genkbdread(struct cdev *dev, struct uio *uio, int flag)
629 u_char buffer[KB_BUFSIZE];
637 kbd = kbd_get_keyboard(KBD_INDEX(dev));
638 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
642 while (sc->gkb_q_length == 0) {
643 if (flag & O_NONBLOCK) {
645 return (EWOULDBLOCK);
647 sc->gkb_flags |= KB_ASLEEP;
648 error = tsleep(sc, PZERO | PCATCH, "kbdrea", 0);
649 kbd = kbd_get_keyboard(KBD_INDEX(dev));
650 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
652 return (ENXIO); /* our keyboard has gone... */
655 sc->gkb_flags &= ~KB_ASLEEP;
662 /* copy as much input as possible */
664 while (uio->uio_resid > 0) {
665 len = imin(uio->uio_resid, sizeof(buffer));
666 len = genkbd_getc(sc, buffer, len);
669 error = uiomove(buffer, len, uio);
678 genkbdwrite(struct cdev *dev, struct uio *uio, int flag)
682 kbd = kbd_get_keyboard(KBD_INDEX(dev));
683 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
689 genkbdioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
694 kbd = kbd_get_keyboard(KBD_INDEX(dev));
695 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
697 error = kbdd_ioctl(kbd, cmd, arg);
698 if (error == ENOIOCTL)
704 genkbdpoll(struct cdev *dev, int events, struct thread *td)
714 kbd = kbd_get_keyboard(KBD_INDEX(dev));
715 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
716 revents = POLLHUP; /* the keyboard has gone */
717 } else if (events & (POLLIN | POLLRDNORM)) {
718 if (sc->gkb_q_length > 0)
719 revents = events & (POLLIN | POLLRDNORM);
721 selrecord(td, &sc->gkb_rsel);
728 genkbd_event(keyboard_t *kbd, int event, void *arg)
736 /* assert(KBD_IS_VALID(kbd)) */
737 sc = (genkbd_softc_t *)arg;
742 case KBDIO_UNLOADING:
743 /* the keyboard is going... */
744 kbd_release(kbd, (void *)sc);
745 if (sc->gkb_flags & KB_ASLEEP) {
746 sc->gkb_flags &= ~KB_ASLEEP;
749 selwakeuppri(&sc->gkb_rsel, PZERO);
755 /* obtain the current key input mode */
756 if (kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode))
759 /* read all pending input */
760 while (kbdd_check_char(kbd)) {
761 c = kbdd_read_char(kbd, FALSE);
764 if (c == ERRKEY) /* XXX: ring bell? */
766 if (!KBD_IS_BUSY(kbd))
767 /* the device is not open, discard the input */
770 /* store the byte as is for K_RAW and K_CODE modes */
771 if (mode != K_XLATE) {
772 genkbd_putc(sc, KEYCHAR(c));
777 if (c & RELKEY) /* key release is ignored */
780 /* process special keys; most of them are just ignored... */
782 switch (KEYCHAR(c)) {
786 case BTAB: /* a backtab: ESC [ Z */
787 genkbd_putc(sc, 0x1b);
788 genkbd_putc(sc, '[');
789 genkbd_putc(sc, 'Z');
794 /* normal chars, normal chars with the META, function keys */
795 switch (KEYFLAGS(c)) {
796 case 0: /* a normal char */
797 genkbd_putc(sc, KEYCHAR(c));
799 case MKEY: /* the META flag: prepend ESC */
800 genkbd_putc(sc, 0x1b);
801 genkbd_putc(sc, KEYCHAR(c));
803 case FKEY | SPCLKEY: /* a function key, return string */
804 cp = kbdd_get_fkeystr(kbd, KEYCHAR(c), &len);
807 genkbd_putc(sc, *cp++);
813 /* wake up sleeping/polling processes */
814 if (sc->gkb_q_length > 0) {
815 if (sc->gkb_flags & KB_ASLEEP) {
816 sc->gkb_flags &= ~KB_ASLEEP;
819 selwakeuppri(&sc->gkb_rsel, PZERO);
825 #endif /* KBD_INSTALL_CDEV */
828 * Generic low-level keyboard functions
829 * The low-level functions in the keyboard subdriver may use these
833 #ifndef KBD_DISABLE_KEYMAP_LOAD
834 static int key_change_ok(struct keyent_t *, struct keyent_t *, struct thread *);
835 static int keymap_change_ok(keymap_t *, keymap_t *, struct thread *);
836 static int accent_change_ok(accentmap_t *, accentmap_t *, struct thread *);
837 static int fkey_change_ok(fkeytab_t *, fkeyarg_t *, struct thread *);
841 genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
854 case KDGKBINFO: /* get keyboard information */
855 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
856 i = imin(strlen(kbd->kb_name) + 1,
857 sizeof(((keyboard_info_t *)arg)->kb_name));
858 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
859 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
860 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
861 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
862 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
865 case KDGKBTYPE: /* get keyboard type */
866 *(int *)arg = kbd->kb_type;
869 case KDGETREPEAT: /* get keyboard repeat rate */
870 ((int *)arg)[0] = kbd->kb_delay1;
871 ((int *)arg)[1] = kbd->kb_delay2;
874 case GIO_KEYMAP: /* get keyboard translation table */
875 error = copyout(kbd->kb_keymap, *(void **)arg,
879 case OGIO_KEYMAP: /* get keyboard translation table (compat) */
880 mapp = kbd->kb_keymap;
881 omapp = (okeymap_t *)arg;
882 omapp->n_keys = mapp->n_keys;
883 for (i = 0; i < NUM_KEYS; i++) {
884 for (j = 0; j < NUM_STATES; j++)
885 omapp->key[i].map[j] =
887 omapp->key[i].spcl = mapp->key[i].spcl;
888 omapp->key[i].flgs = mapp->key[i].flgs;
891 case PIO_KEYMAP: /* set keyboard translation table */
892 case OPIO_KEYMAP: /* set keyboard translation table (compat) */
893 #ifndef KBD_DISABLE_KEYMAP_LOAD
894 mapp = malloc(sizeof *mapp, M_TEMP, M_WAITOK);
895 if (cmd == OPIO_KEYMAP) {
896 omapp = (okeymap_t *)arg;
897 mapp->n_keys = omapp->n_keys;
898 for (i = 0; i < NUM_KEYS; i++) {
899 for (j = 0; j < NUM_STATES; j++)
900 mapp->key[i].map[j] =
901 omapp->key[i].map[j];
902 mapp->key[i].spcl = omapp->key[i].spcl;
903 mapp->key[i].flgs = omapp->key[i].flgs;
906 error = copyin(*(void **)arg, mapp, sizeof *mapp);
914 error = keymap_change_ok(kbd->kb_keymap, mapp, curthread);
920 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
921 bcopy(mapp, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
929 case GIO_KEYMAPENT: /* get keyboard translation table entry */
930 keyp = (keyarg_t *)arg;
931 if (keyp->keynum >= sizeof(kbd->kb_keymap->key) /
932 sizeof(kbd->kb_keymap->key[0])) {
936 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
939 case PIO_KEYMAPENT: /* set keyboard translation table entry */
940 #ifndef KBD_DISABLE_KEYMAP_LOAD
941 keyp = (keyarg_t *)arg;
942 if (keyp->keynum >= sizeof(kbd->kb_keymap->key) /
943 sizeof(kbd->kb_keymap->key[0])) {
947 error = key_change_ok(&kbd->kb_keymap->key[keyp->keynum],
948 &keyp->key, curthread);
953 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
961 case GIO_DEADKEYMAP: /* get accent key translation table */
962 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
964 case PIO_DEADKEYMAP: /* set accent key translation table */
965 #ifndef KBD_DISABLE_KEYMAP_LOAD
966 error = accent_change_ok(kbd->kb_accentmap,
967 (accentmap_t *)arg, curthread);
972 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
979 case GETFKEY: /* get functionkey string */
980 fkeyp = (fkeyarg_t *)arg;
981 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
985 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
986 kbd->kb_fkeytab[fkeyp->keynum].len);
987 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
989 case SETFKEY: /* set functionkey string */
990 #ifndef KBD_DISABLE_KEYMAP_LOAD
991 fkeyp = (fkeyarg_t *)arg;
992 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
996 error = fkey_change_ok(&kbd->kb_fkeytab[fkeyp->keynum],
1002 kbd->kb_fkeytab[fkeyp->keynum].len = min(fkeyp->flen, MAXFK);
1003 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
1004 kbd->kb_fkeytab[fkeyp->keynum].len);
1020 #ifndef KBD_DISABLE_KEYMAP_LOAD
1021 #define RESTRICTED_KEY(key, i) \
1022 ((key->spcl & (0x80 >> i)) && \
1023 (key->map[i] == RBT || key->map[i] == SUSP || \
1024 key->map[i] == STBY || key->map[i] == DBG || \
1025 key->map[i] == PNC || key->map[i] == HALT || \
1026 key->map[i] == PDWN))
1029 key_change_ok(struct keyent_t *oldkey, struct keyent_t *newkey, struct thread *td)
1033 /* Low keymap_restrict_change means any changes are OK. */
1034 if (keymap_restrict_change <= 0)
1037 /* High keymap_restrict_change means only root can change the keymap. */
1038 if (keymap_restrict_change >= 2) {
1039 for (i = 0; i < NUM_STATES; i++)
1040 if (oldkey->map[i] != newkey->map[i])
1041 return priv_check(td, PRIV_KEYBOARD);
1042 if (oldkey->spcl != newkey->spcl)
1043 return priv_check(td, PRIV_KEYBOARD);
1044 if (oldkey->flgs != newkey->flgs)
1045 return priv_check(td, PRIV_KEYBOARD);
1049 /* Otherwise we have to see if any special keys are being changed. */
1050 for (i = 0; i < NUM_STATES; i++) {
1052 * If either the oldkey or the newkey action is restricted
1053 * then we must make sure that the action doesn't change.
1055 if (!RESTRICTED_KEY(oldkey, i) && !RESTRICTED_KEY(newkey, i))
1057 if ((oldkey->spcl & (0x80 >> i)) == (newkey->spcl & (0x80 >> i))
1058 && oldkey->map[i] == newkey->map[i])
1060 return priv_check(td, PRIV_KEYBOARD);
1067 keymap_change_ok(keymap_t *oldmap, keymap_t *newmap, struct thread *td)
1071 for (keycode = 0; keycode < NUM_KEYS; keycode++) {
1072 if ((error = key_change_ok(&oldmap->key[keycode],
1073 &newmap->key[keycode], td)) != 0)
1080 accent_change_ok(accentmap_t *oldmap, accentmap_t *newmap, struct thread *td)
1082 struct acc_t *oldacc, *newacc;
1085 if (keymap_restrict_change <= 2)
1088 if (oldmap->n_accs != newmap->n_accs)
1089 return priv_check(td, PRIV_KEYBOARD);
1091 for (accent = 0; accent < oldmap->n_accs; accent++) {
1092 oldacc = &oldmap->acc[accent];
1093 newacc = &newmap->acc[accent];
1094 if (oldacc->accchar != newacc->accchar)
1095 return priv_check(td, PRIV_KEYBOARD);
1096 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1097 if (oldacc->map[i][0] != newacc->map[i][0])
1098 return priv_check(td, PRIV_KEYBOARD);
1099 if (oldacc->map[i][0] == 0) /* end of table */
1101 if (oldacc->map[i][1] != newacc->map[i][1])
1102 return priv_check(td, PRIV_KEYBOARD);
1110 fkey_change_ok(fkeytab_t *oldkey, fkeyarg_t *newkey, struct thread *td)
1112 if (keymap_restrict_change <= 3)
1115 if (oldkey->len != newkey->flen ||
1116 bcmp(oldkey->str, newkey->keydef, oldkey->len) != 0)
1117 return priv_check(td, PRIV_KEYBOARD);
1123 /* get a pointer to the string associated with the given function key */
1125 genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
1130 if (fkey > kbd->kb_fkeytab_size)
1132 *len = kbd->kb_fkeytab[fkey].len;
1133 return (kbd->kb_fkeytab[fkey].str);
1136 /* diagnostic dump */
1138 get_kbd_type_name(int type)
1145 { KB_101, "AT 101/102" },
1146 { KB_OTHER, "generic" },
1150 for (i = 0; i < nitems(name_table); ++i) {
1151 if (type == name_table[i].type)
1152 return (name_table[i].name);
1158 genkbd_diag(keyboard_t *kbd, int level)
1161 printf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
1162 kbd->kb_index, kbd->kb_name, kbd->kb_unit,
1163 get_kbd_type_name(kbd->kb_type), kbd->kb_type,
1164 kbd->kb_config, kbd->kb_flags);
1165 if (kbd->kb_io_base > 0)
1166 printf(", port:0x%x-0x%x", kbd->kb_io_base,
1167 kbd->kb_io_base + kbd->kb_io_size - 1);
1172 #define set_lockkey_state(k, s, l) \
1173 if (!((s) & l ## DOWN)) { \
1177 i = (s) & LOCK_MASK; \
1178 (void)kbdd_ioctl((k), KDSETLED, (caddr_t)&i); \
1182 save_accent_key(keyboard_t *kbd, u_int key, int *accents)
1186 /* make an index into the accent map */
1187 i = key - F_ACC + 1;
1188 if ((i > kbd->kb_accentmap->n_accs)
1189 || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
1190 /* the index is out of range or pointing to an empty entry */
1196 * If the same accent key has been hit twice, produce the accent
1199 if (i == *accents) {
1200 key = kbd->kb_accentmap->acc[i - 1].accchar;
1205 /* remember the index and wait for the next key */
1211 make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
1216 acc = &kbd->kb_accentmap->acc[*accents - 1];
1220 * If the accent key is followed by the space key,
1221 * produce the accent char itself.
1224 return (acc->accchar);
1226 /* scan the accent map */
1227 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1228 if (acc->map[i][0] == 0) /* end of table */
1230 if (acc->map[i][0] == ch)
1231 return (acc->map[i][1]);
1233 /* this char cannot be accented... */
1238 genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1241 struct keyent_t *key;
1242 int state = *shiftstate;
1248 f = state & (AGRS | ALKED);
1249 if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1251 key = &kbd->kb_keymap->key[i];
1252 i = ((state & SHIFTS) ? 1 : 0)
1253 | ((state & CTLS) ? 2 : 0)
1254 | ((state & ALTS) ? 4 : 0);
1255 if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1256 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1259 if (up) { /* break: key released */
1260 action = kbd->kb_lastact[keycode];
1261 kbd->kb_lastact[keycode] = NOP;
1264 if (state & SHIFTAON) {
1265 set_lockkey_state(kbd, state, ALK);
1274 if (state & SHIFTAON) {
1275 set_lockkey_state(kbd, state, ALK);
1284 if (state & SHIFTAON) {
1285 set_lockkey_state(kbd, state, ALK);
1294 if (state & SHIFTAON) {
1295 set_lockkey_state(kbd, state, ALK);
1304 if (state & SHIFTAON) {
1305 set_lockkey_state(kbd, state, ALK);
1314 if (state & SHIFTAON) {
1315 set_lockkey_state(kbd, state, ALK);
1342 /* release events of regular keys are not reported */
1343 *shiftstate &= ~SHIFTAON;
1346 *shiftstate = state & ~SHIFTAON;
1347 return (SPCLKEY | RELKEY | action);
1348 } else { /* make: key pressed */
1349 action = key->map[i];
1351 if (key->spcl & (0x80 >> i)) {
1353 if (kbd->kb_lastact[keycode] == NOP)
1354 kbd->kb_lastact[keycode] = action;
1355 if (kbd->kb_lastact[keycode] != action)
1360 set_lockkey_state(kbd, state, NLK);
1363 set_lockkey_state(kbd, state, CLK);
1366 set_lockkey_state(kbd, state, SLK);
1369 set_lockkey_state(kbd, state, ALK);
1371 /* NON-LOCKING KEYS */
1372 case SPSC: case RBT: case SUSP: case STBY:
1373 case DBG: case NEXT: case PREV: case PNC:
1374 case HALT: case PDWN:
1430 *shiftstate = state;
1433 /* is this an accent (dead) key? */
1434 *shiftstate = state;
1435 if (action >= F_ACC && action <= L_ACC) {
1436 action = save_accent_key(kbd, action,
1444 return (action | MKEY);
1450 /* other special keys */
1455 if (action >= F_FN && action <= L_FN)
1457 /* XXX: return fkey string for the FKEY? */
1458 return (SPCLKEY | action);
1460 *shiftstate = state;
1461 return (SPCLKEY | action);
1464 kbd->kb_lastact[keycode] = NOP;
1465 *shiftstate = state;
1467 /* make an accented char */
1468 action = make_accent_char(kbd, action, accents);
1469 if (action == ERRKEY)
1481 kbd_ev_event(keyboard_t *kbd, uint16_t type, uint16_t code, int32_t value)
1483 int delay[2], led = 0, leds, oleds;
1485 if (type == EV_LED) {
1486 leds = oleds = KBD_LED_VAL(kbd);
1505 kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
1507 } else if (type == EV_REP && code == REP_DELAY) {
1509 delay[1] = kbd->kb_delay2;
1510 kbdd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
1511 } else if (type == EV_REP && code == REP_PERIOD) {
1512 delay[0] = kbd->kb_delay1;
1514 kbdd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);