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 u_char *genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len);
68 static void genkbd_diag(keyboard_t *kbd, int level);
70 static SLIST_HEAD(, keyboard_driver) keyboard_drivers =
71 SLIST_HEAD_INITIALIZER(keyboard_drivers);
73 SET_DECLARE(kbddriver_set, keyboard_driver_t);
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
83 static int keyboards = 1;
84 static keyboard_t *kbd_ini;
85 static keyboard_t **keyboard = &kbd_ini;
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");
95 kbd_realloc_array(void)
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 bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
110 free(keyboard, M_DEVBUF);
116 printf("kbd: new array size %d\n", keyboards);
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'.
128 /* initialize the keyboard_t structure */
130 kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config,
131 int port, int port_size)
133 kbd->kb_flags = KB_NO_DEVICE; /* device has not been found */
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;
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;
149 bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact));
153 kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap,
154 fkeytab_t *fkeymap, int fkeymap_size)
156 kbd->kb_keymap = keymap;
157 kbd->kb_accentmap = accmap;
158 kbd->kb_fkeytab = fkeymap;
159 kbd->kb_fkeytab_size = fkeymap_size;
162 /* declare a new keyboard driver */
164 kbd_add_driver(keyboard_driver_t *driver)
167 if ((driver->flags & KBDF_REGISTERED) != 0)
170 KASSERT(SLIST_NEXT(driver, link) == NULL,
171 ("%s: keyboard driver list garbage detected", __func__));
172 if (driver->kbdsw->get_fkeystr == NULL)
173 driver->kbdsw->get_fkeystr = genkbd_get_fkeystr;
174 if (driver->kbdsw->diag == NULL)
175 driver->kbdsw->diag = genkbd_diag;
177 driver->flags |= KBDF_REGISTERED;
178 SLIST_INSERT_HEAD(&keyboard_drivers, driver, link);
183 kbd_delete_driver(keyboard_driver_t *driver)
186 if ((driver->flags & KBDF_REGISTERED) == 0)
189 driver->flags &= ~KBDF_REGISTERED;
190 SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link);
191 SLIST_NEXT(driver, link) = NULL;
195 /* register a keyboard and associate it with a function table */
197 kbd_register(keyboard_t *kbd)
199 const keyboard_driver_t *p;
204 mux = kbd_get_keyboard(kbd_find_keyboard("kbdmux", -1));
206 for (index = 0; index < keyboards; ++index) {
207 if (keyboard[index] == NULL)
210 if (index >= keyboards) {
211 if (kbd_realloc_array())
215 kbd->kb_index = index;
218 kbd->kb_active = 0; /* disabled until someone calls kbd_enable() */
219 kbd->kb_token = NULL;
220 kbd->kb_callback.kc_func = NULL;
221 kbd->kb_callback.kc_arg = NULL;
223 SLIST_FOREACH(p, &keyboard_drivers, link) {
224 if (strcmp(p->name, kbd->kb_name) == 0) {
226 keyboard[index] = kbd;
229 bzero(&ki, sizeof(ki));
230 strcpy(ki.kb_name, kbd->kb_name);
231 ki.kb_unit = kbd->kb_unit;
233 (void)kbdd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
244 kbd_unregister(keyboard_t *kbd)
249 if ((kbd->kb_index < 0) || (kbd->kb_index >= keyboards))
251 if (keyboard[kbd->kb_index] != kbd)
255 if (KBD_IS_BUSY(kbd)) {
256 error = (*kbd->kb_callback.kc_func)(kbd, KBDIO_UNLOADING,
257 kbd->kb_callback.kc_arg);
262 if (KBD_IS_BUSY(kbd)) {
268 keyboard[kbd->kb_index] = NULL;
274 /* find a function table by the driver name */
276 kbd_get_switch(char *driver)
278 const keyboard_driver_t *p;
280 SLIST_FOREACH(p, &keyboard_drivers, link) {
281 if (strcmp(p->name, driver) == 0)
289 * Keyboard client functions
290 * Keyboard clients, such as the console driver `syscons' and the keyboard
291 * cdev driver, use these functions to claim and release a keyboard for
296 * find the keyboard specified by a driver name and a unit number
297 * starting at given index
300 kbd_find_keyboard2(char *driver, int unit, int index)
304 if ((index < 0) || (index >= keyboards))
307 for (i = index; i < keyboards; ++i) {
308 if (keyboard[i] == NULL)
310 if (!KBD_IS_VALID(keyboard[i]))
312 if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver))
314 if ((unit != -1) && (keyboard[i]->kb_unit != unit))
322 /* find the keyboard specified by a driver name and a unit number */
324 kbd_find_keyboard(char *driver, int unit)
326 return (kbd_find_keyboard2(driver, unit, 0));
329 /* allocate a keyboard */
331 kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func,
341 index = kbd_find_keyboard(driver, unit);
343 if (KBD_IS_BUSY(keyboard[index])) {
347 keyboard[index]->kb_token = id;
348 KBD_BUSY(keyboard[index]);
349 keyboard[index]->kb_callback.kc_func = func;
350 keyboard[index]->kb_callback.kc_arg = arg;
351 kbdd_clear_state(keyboard[index]);
358 kbd_release(keyboard_t *kbd, void *id)
364 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
366 } else if (kbd->kb_token != id) {
369 kbd->kb_token = NULL;
371 kbd->kb_callback.kc_func = NULL;
372 kbd->kb_callback.kc_arg = NULL;
373 kbdd_clear_state(kbd);
381 kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func,
388 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
390 } else if (kbd->kb_token != id) {
392 } else if (func == NULL) {
395 kbd->kb_callback.kc_func = func;
396 kbd->kb_callback.kc_arg = arg;
403 /* get a keyboard structure */
405 kbd_get_keyboard(int index)
407 if ((index < 0) || (index >= keyboards))
409 if (keyboard[index] == NULL)
411 if (!KBD_IS_VALID(keyboard[index]))
413 return (keyboard[index]);
417 * The back door for the console driver; configure keyboards
418 * This function is for the kernel console to initialize keyboards
419 * at very early stage.
423 kbd_configure(int flags)
425 const keyboard_driver_t *p;
427 SLIST_FOREACH(p, &keyboard_drivers, link) {
428 if (p->configure != NULL)
429 (*p->configure)(flags);
435 #ifdef KBD_INSTALL_CDEV
438 * Virtual keyboard cdev driver functions
439 * The virtual keyboard driver dispatches driver functions to
440 * appropriate subdrivers.
443 #define KBD_UNIT(dev) dev2unit(dev)
445 static d_open_t genkbdopen;
446 static d_close_t genkbdclose;
447 static d_read_t genkbdread;
448 static d_write_t genkbdwrite;
449 static d_ioctl_t genkbdioctl;
450 static d_poll_t genkbdpoll;
453 static struct cdevsw kbd_cdevsw = {
454 .d_version = D_VERSION,
455 .d_flags = D_NEEDGIANT,
456 .d_open = genkbdopen,
457 .d_close = genkbdclose,
458 .d_read = genkbdread,
459 .d_write = genkbdwrite,
460 .d_ioctl = genkbdioctl,
461 .d_poll = genkbdpoll,
466 kbd_attach(keyboard_t *kbd)
469 if (kbd->kb_index >= keyboards)
471 if (keyboard[kbd->kb_index] != kbd)
474 kbd->kb_dev = make_dev(&kbd_cdevsw, kbd->kb_index, UID_ROOT, GID_WHEEL,
475 0600, "%s%r", kbd->kb_name, kbd->kb_unit);
476 make_dev_alias(kbd->kb_dev, "kbd%r", kbd->kb_index);
477 kbd->kb_dev->si_drv1 = malloc(sizeof(genkbd_softc_t), M_DEVBUF,
479 printf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
484 kbd_detach(keyboard_t *kbd)
487 if (kbd->kb_index >= keyboards)
489 if (keyboard[kbd->kb_index] != kbd)
492 free(kbd->kb_dev->si_drv1, M_DEVBUF);
493 destroy_dev(kbd->kb_dev);
499 * Generic keyboard cdev driver functions
500 * Keyboard subdrivers may call these functions to implement common
505 genkbd_putc(genkbd_softc_t *sc, char c)
509 if (sc->gkb_q_length == KB_QSIZE)
512 p = (sc->gkb_q_start + sc->gkb_q_length) % KB_QSIZE;
518 genkbd_getc(genkbd_softc_t *sc, char *buf, size_t len)
521 /* Determine copy size. */
522 if (sc->gkb_q_length == 0)
524 if (len >= sc->gkb_q_length)
525 len = sc->gkb_q_length;
526 if (len >= KB_QSIZE - sc->gkb_q_start)
527 len = KB_QSIZE - sc->gkb_q_start;
529 /* Copy out data and progress offset. */
530 memcpy(buf, sc->gkb_q + sc->gkb_q_start, len);
531 sc->gkb_q_start = (sc->gkb_q_start + len) % KB_QSIZE;
532 sc->gkb_q_length -= len;
537 static kbd_callback_func_t genkbd_event;
540 genkbdopen(struct cdev *dev, int mode, int flag, struct thread *td)
549 kbd = kbd_get_keyboard(KBD_INDEX(dev));
550 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
554 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
555 genkbd_event, (void *)sc);
560 /* assert(i == kbd->kb_index) */
561 /* assert(kbd == kbd_get_keyboard(i)) */
564 * NOTE: even when we have successfully claimed a keyboard,
565 * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
568 sc->gkb_q_length = 0;
575 genkbdclose(struct cdev *dev, int mode, int flag, struct thread *td)
582 * NOTE: the device may have already become invalid.
583 * kbd == NULL || !KBD_IS_VALID(kbd)
587 kbd = kbd_get_keyboard(KBD_INDEX(dev));
588 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
589 /* XXX: we shall be forgiving and don't report error... */
591 kbd_release(kbd, (void *)sc);
598 genkbdread(struct cdev *dev, struct uio *uio, int flag)
602 u_char buffer[KB_BUFSIZE];
610 kbd = kbd_get_keyboard(KBD_INDEX(dev));
611 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
615 while (sc->gkb_q_length == 0) {
616 if (flag & O_NONBLOCK) {
618 return (EWOULDBLOCK);
620 sc->gkb_flags |= KB_ASLEEP;
621 error = tsleep(sc, PZERO | PCATCH, "kbdrea", 0);
622 kbd = kbd_get_keyboard(KBD_INDEX(dev));
623 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
625 return (ENXIO); /* our keyboard has gone... */
628 sc->gkb_flags &= ~KB_ASLEEP;
635 /* copy as much input as possible */
637 while (uio->uio_resid > 0) {
638 len = imin(uio->uio_resid, sizeof(buffer));
639 len = genkbd_getc(sc, buffer, len);
642 error = uiomove(buffer, len, uio);
651 genkbdwrite(struct cdev *dev, struct uio *uio, int flag)
655 kbd = kbd_get_keyboard(KBD_INDEX(dev));
656 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
662 genkbdioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
667 kbd = kbd_get_keyboard(KBD_INDEX(dev));
668 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
670 error = kbdd_ioctl(kbd, cmd, arg);
671 if (error == ENOIOCTL)
677 genkbdpoll(struct cdev *dev, int events, struct thread *td)
687 kbd = kbd_get_keyboard(KBD_INDEX(dev));
688 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
689 revents = POLLHUP; /* the keyboard has gone */
690 } else if (events & (POLLIN | POLLRDNORM)) {
691 if (sc->gkb_q_length > 0)
692 revents = events & (POLLIN | POLLRDNORM);
694 selrecord(td, &sc->gkb_rsel);
701 genkbd_event(keyboard_t *kbd, int event, void *arg)
709 /* assert(KBD_IS_VALID(kbd)) */
710 sc = (genkbd_softc_t *)arg;
715 case KBDIO_UNLOADING:
716 /* the keyboard is going... */
717 kbd_release(kbd, (void *)sc);
718 if (sc->gkb_flags & KB_ASLEEP) {
719 sc->gkb_flags &= ~KB_ASLEEP;
722 selwakeuppri(&sc->gkb_rsel, PZERO);
728 /* obtain the current key input mode */
729 if (kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode))
732 /* read all pending input */
733 while (kbdd_check_char(kbd)) {
734 c = kbdd_read_char(kbd, FALSE);
737 if (c == ERRKEY) /* XXX: ring bell? */
739 if (!KBD_IS_BUSY(kbd))
740 /* the device is not open, discard the input */
743 /* store the byte as is for K_RAW and K_CODE modes */
744 if (mode != K_XLATE) {
745 genkbd_putc(sc, KEYCHAR(c));
750 if (c & RELKEY) /* key release is ignored */
753 /* process special keys; most of them are just ignored... */
755 switch (KEYCHAR(c)) {
759 case BTAB: /* a backtab: ESC [ Z */
760 genkbd_putc(sc, 0x1b);
761 genkbd_putc(sc, '[');
762 genkbd_putc(sc, 'Z');
767 /* normal chars, normal chars with the META, function keys */
768 switch (KEYFLAGS(c)) {
769 case 0: /* a normal char */
770 genkbd_putc(sc, KEYCHAR(c));
772 case MKEY: /* the META flag: prepend ESC */
773 genkbd_putc(sc, 0x1b);
774 genkbd_putc(sc, KEYCHAR(c));
776 case FKEY | SPCLKEY: /* a function key, return string */
777 cp = kbdd_get_fkeystr(kbd, KEYCHAR(c), &len);
780 genkbd_putc(sc, *cp++);
786 /* wake up sleeping/polling processes */
787 if (sc->gkb_q_length > 0) {
788 if (sc->gkb_flags & KB_ASLEEP) {
789 sc->gkb_flags &= ~KB_ASLEEP;
792 selwakeuppri(&sc->gkb_rsel, PZERO);
798 #endif /* KBD_INSTALL_CDEV */
801 * Generic low-level keyboard functions
802 * The low-level functions in the keyboard subdriver may use these
806 #ifndef KBD_DISABLE_KEYMAP_LOAD
807 static int key_change_ok(struct keyent_t *, struct keyent_t *, struct thread *);
808 static int keymap_change_ok(keymap_t *, keymap_t *, struct thread *);
809 static int accent_change_ok(accentmap_t *, accentmap_t *, struct thread *);
810 static int fkey_change_ok(fkeytab_t *, fkeyarg_t *, struct thread *);
814 genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
827 case KDGKBINFO: /* get keyboard information */
828 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
829 i = imin(strlen(kbd->kb_name) + 1,
830 sizeof(((keyboard_info_t *)arg)->kb_name));
831 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
832 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
833 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
834 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
835 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
838 case KDGKBTYPE: /* get keyboard type */
839 *(int *)arg = kbd->kb_type;
842 case KDGETREPEAT: /* get keyboard repeat rate */
843 ((int *)arg)[0] = kbd->kb_delay1;
844 ((int *)arg)[1] = kbd->kb_delay2;
847 case GIO_KEYMAP: /* get keyboard translation table */
848 error = copyout(kbd->kb_keymap, *(void **)arg,
852 case OGIO_KEYMAP: /* get keyboard translation table (compat) */
853 mapp = kbd->kb_keymap;
854 omapp = (okeymap_t *)arg;
855 omapp->n_keys = mapp->n_keys;
856 for (i = 0; i < NUM_KEYS; i++) {
857 for (j = 0; j < NUM_STATES; j++)
858 omapp->key[i].map[j] =
860 omapp->key[i].spcl = mapp->key[i].spcl;
861 omapp->key[i].flgs = mapp->key[i].flgs;
864 case PIO_KEYMAP: /* set keyboard translation table */
865 case OPIO_KEYMAP: /* set keyboard translation table (compat) */
866 #ifndef KBD_DISABLE_KEYMAP_LOAD
867 mapp = malloc(sizeof *mapp, M_TEMP, M_WAITOK);
868 if (cmd == OPIO_KEYMAP) {
869 omapp = (okeymap_t *)arg;
870 mapp->n_keys = omapp->n_keys;
871 for (i = 0; i < NUM_KEYS; i++) {
872 for (j = 0; j < NUM_STATES; j++)
873 mapp->key[i].map[j] =
874 omapp->key[i].map[j];
875 mapp->key[i].spcl = omapp->key[i].spcl;
876 mapp->key[i].flgs = omapp->key[i].flgs;
879 error = copyin(*(void **)arg, mapp, sizeof *mapp);
887 error = keymap_change_ok(kbd->kb_keymap, mapp, curthread);
893 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
894 bcopy(mapp, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
902 case GIO_KEYMAPENT: /* get keyboard translation table entry */
903 keyp = (keyarg_t *)arg;
904 if (keyp->keynum >= sizeof(kbd->kb_keymap->key) /
905 sizeof(kbd->kb_keymap->key[0])) {
909 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
912 case PIO_KEYMAPENT: /* set keyboard translation table entry */
913 #ifndef KBD_DISABLE_KEYMAP_LOAD
914 keyp = (keyarg_t *)arg;
915 if (keyp->keynum >= sizeof(kbd->kb_keymap->key) /
916 sizeof(kbd->kb_keymap->key[0])) {
920 error = key_change_ok(&kbd->kb_keymap->key[keyp->keynum],
921 &keyp->key, curthread);
926 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
934 case GIO_DEADKEYMAP: /* get accent key translation table */
935 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
937 case PIO_DEADKEYMAP: /* set accent key translation table */
938 #ifndef KBD_DISABLE_KEYMAP_LOAD
939 error = accent_change_ok(kbd->kb_accentmap,
940 (accentmap_t *)arg, curthread);
945 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
952 case GETFKEY: /* get functionkey string */
953 fkeyp = (fkeyarg_t *)arg;
954 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
958 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
959 kbd->kb_fkeytab[fkeyp->keynum].len);
960 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
962 case SETFKEY: /* set functionkey string */
963 #ifndef KBD_DISABLE_KEYMAP_LOAD
964 fkeyp = (fkeyarg_t *)arg;
965 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
969 error = fkey_change_ok(&kbd->kb_fkeytab[fkeyp->keynum],
975 kbd->kb_fkeytab[fkeyp->keynum].len = min(fkeyp->flen, MAXFK);
976 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
977 kbd->kb_fkeytab[fkeyp->keynum].len);
993 #ifndef KBD_DISABLE_KEYMAP_LOAD
994 #define RESTRICTED_KEY(key, i) \
995 ((key->spcl & (0x80 >> i)) && \
996 (key->map[i] == RBT || key->map[i] == SUSP || \
997 key->map[i] == STBY || key->map[i] == DBG || \
998 key->map[i] == PNC || key->map[i] == HALT || \
999 key->map[i] == PDWN))
1002 key_change_ok(struct keyent_t *oldkey, struct keyent_t *newkey, struct thread *td)
1006 /* Low keymap_restrict_change means any changes are OK. */
1007 if (keymap_restrict_change <= 0)
1010 /* High keymap_restrict_change means only root can change the keymap. */
1011 if (keymap_restrict_change >= 2) {
1012 for (i = 0; i < NUM_STATES; i++)
1013 if (oldkey->map[i] != newkey->map[i])
1014 return priv_check(td, PRIV_KEYBOARD);
1015 if (oldkey->spcl != newkey->spcl)
1016 return priv_check(td, PRIV_KEYBOARD);
1017 if (oldkey->flgs != newkey->flgs)
1018 return priv_check(td, PRIV_KEYBOARD);
1022 /* Otherwise we have to see if any special keys are being changed. */
1023 for (i = 0; i < NUM_STATES; i++) {
1025 * If either the oldkey or the newkey action is restricted
1026 * then we must make sure that the action doesn't change.
1028 if (!RESTRICTED_KEY(oldkey, i) && !RESTRICTED_KEY(newkey, i))
1030 if ((oldkey->spcl & (0x80 >> i)) == (newkey->spcl & (0x80 >> i))
1031 && oldkey->map[i] == newkey->map[i])
1033 return priv_check(td, PRIV_KEYBOARD);
1040 keymap_change_ok(keymap_t *oldmap, keymap_t *newmap, struct thread *td)
1044 for (keycode = 0; keycode < NUM_KEYS; keycode++) {
1045 if ((error = key_change_ok(&oldmap->key[keycode],
1046 &newmap->key[keycode], td)) != 0)
1053 accent_change_ok(accentmap_t *oldmap, accentmap_t *newmap, struct thread *td)
1055 struct acc_t *oldacc, *newacc;
1058 if (keymap_restrict_change <= 2)
1061 if (oldmap->n_accs != newmap->n_accs)
1062 return priv_check(td, PRIV_KEYBOARD);
1064 for (accent = 0; accent < oldmap->n_accs; accent++) {
1065 oldacc = &oldmap->acc[accent];
1066 newacc = &newmap->acc[accent];
1067 if (oldacc->accchar != newacc->accchar)
1068 return priv_check(td, PRIV_KEYBOARD);
1069 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1070 if (oldacc->map[i][0] != newacc->map[i][0])
1071 return priv_check(td, PRIV_KEYBOARD);
1072 if (oldacc->map[i][0] == 0) /* end of table */
1074 if (oldacc->map[i][1] != newacc->map[i][1])
1075 return priv_check(td, PRIV_KEYBOARD);
1083 fkey_change_ok(fkeytab_t *oldkey, fkeyarg_t *newkey, struct thread *td)
1085 if (keymap_restrict_change <= 3)
1088 if (oldkey->len != newkey->flen ||
1089 bcmp(oldkey->str, newkey->keydef, oldkey->len) != 0)
1090 return priv_check(td, PRIV_KEYBOARD);
1096 /* get a pointer to the string associated with the given function key */
1098 genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
1103 if (fkey > kbd->kb_fkeytab_size)
1105 *len = kbd->kb_fkeytab[fkey].len;
1106 return (kbd->kb_fkeytab[fkey].str);
1109 /* diagnostic dump */
1111 get_kbd_type_name(int type)
1118 { KB_101, "AT 101/102" },
1119 { KB_OTHER, "generic" },
1123 for (i = 0; i < nitems(name_table); ++i) {
1124 if (type == name_table[i].type)
1125 return (name_table[i].name);
1131 genkbd_diag(keyboard_t *kbd, int level)
1134 printf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
1135 kbd->kb_index, kbd->kb_name, kbd->kb_unit,
1136 get_kbd_type_name(kbd->kb_type), kbd->kb_type,
1137 kbd->kb_config, kbd->kb_flags);
1138 if (kbd->kb_io_base > 0)
1139 printf(", port:0x%x-0x%x", kbd->kb_io_base,
1140 kbd->kb_io_base + kbd->kb_io_size - 1);
1145 #define set_lockkey_state(k, s, l) \
1146 if (!((s) & l ## DOWN)) { \
1150 i = (s) & LOCK_MASK; \
1151 (void)kbdd_ioctl((k), KDSETLED, (caddr_t)&i); \
1155 save_accent_key(keyboard_t *kbd, u_int key, int *accents)
1159 /* make an index into the accent map */
1160 i = key - F_ACC + 1;
1161 if ((i > kbd->kb_accentmap->n_accs)
1162 || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
1163 /* the index is out of range or pointing to an empty entry */
1169 * If the same accent key has been hit twice, produce the accent
1172 if (i == *accents) {
1173 key = kbd->kb_accentmap->acc[i - 1].accchar;
1178 /* remember the index and wait for the next key */
1184 make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
1189 acc = &kbd->kb_accentmap->acc[*accents - 1];
1193 * If the accent key is followed by the space key,
1194 * produce the accent char itself.
1197 return (acc->accchar);
1199 /* scan the accent map */
1200 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1201 if (acc->map[i][0] == 0) /* end of table */
1203 if (acc->map[i][0] == ch)
1204 return (acc->map[i][1]);
1206 /* this char cannot be accented... */
1211 genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1214 struct keyent_t *key;
1215 int state = *shiftstate;
1221 f = state & (AGRS | ALKED);
1222 if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1224 key = &kbd->kb_keymap->key[i];
1225 i = ((state & SHIFTS) ? 1 : 0)
1226 | ((state & CTLS) ? 2 : 0)
1227 | ((state & ALTS) ? 4 : 0);
1228 if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1229 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1232 if (up) { /* break: key released */
1233 action = kbd->kb_lastact[keycode];
1234 kbd->kb_lastact[keycode] = NOP;
1237 if (state & SHIFTAON) {
1238 set_lockkey_state(kbd, state, ALK);
1247 if (state & SHIFTAON) {
1248 set_lockkey_state(kbd, state, ALK);
1257 if (state & SHIFTAON) {
1258 set_lockkey_state(kbd, state, ALK);
1267 if (state & SHIFTAON) {
1268 set_lockkey_state(kbd, state, ALK);
1277 if (state & SHIFTAON) {
1278 set_lockkey_state(kbd, state, ALK);
1287 if (state & SHIFTAON) {
1288 set_lockkey_state(kbd, state, ALK);
1315 /* release events of regular keys are not reported */
1316 *shiftstate &= ~SHIFTAON;
1319 *shiftstate = state & ~SHIFTAON;
1320 return (SPCLKEY | RELKEY | action);
1321 } else { /* make: key pressed */
1322 action = key->map[i];
1324 if (key->spcl & (0x80 >> i)) {
1326 if (kbd->kb_lastact[keycode] == NOP)
1327 kbd->kb_lastact[keycode] = action;
1328 if (kbd->kb_lastact[keycode] != action)
1333 set_lockkey_state(kbd, state, NLK);
1336 set_lockkey_state(kbd, state, CLK);
1339 set_lockkey_state(kbd, state, SLK);
1342 set_lockkey_state(kbd, state, ALK);
1344 /* NON-LOCKING KEYS */
1345 case SPSC: case RBT: case SUSP: case STBY:
1346 case DBG: case NEXT: case PREV: case PNC:
1347 case HALT: case PDWN:
1403 *shiftstate = state;
1406 /* is this an accent (dead) key? */
1407 *shiftstate = state;
1408 if (action >= F_ACC && action <= L_ACC) {
1409 action = save_accent_key(kbd, action,
1417 return (action | MKEY);
1423 /* other special keys */
1428 if (action >= F_FN && action <= L_FN)
1430 /* XXX: return fkey string for the FKEY? */
1431 return (SPCLKEY | action);
1433 *shiftstate = state;
1434 return (SPCLKEY | action);
1437 kbd->kb_lastact[keycode] = NOP;
1438 *shiftstate = state;
1440 /* make an accented char */
1441 action = make_accent_char(kbd, action, accents);
1442 if (action == ERRKEY)
1454 kbd_ev_event(keyboard_t *kbd, uint16_t type, uint16_t code, int32_t value)
1456 int delay[2], led = 0, leds, oleds;
1458 if (type == EV_LED) {
1459 leds = oleds = KBD_LED_VAL(kbd);
1478 kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
1480 } else if (type == EV_REP && code == REP_DELAY) {
1482 delay[1] = kbd->kb_delay2;
1483 kbdd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
1484 } else if (type == EV_REP && code == REP_PERIOD) {
1485 delay[0] = kbd->kb_delay1;
1487 kbdd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
1494 keyboard_driver_t *drv, **list;
1496 SET_FOREACH(list, kbddriver_set) {
1500 * The following printfs will almost universally get dropped,
1501 * with exception to kernel configs with EARLY_PRINTF and
1502 * special setups where msgbufinit() is called early with a
1503 * static buffer to capture output occurring before the dynamic
1504 * message buffer is mapped.
1506 if (kbd_add_driver(drv) != 0)
1507 printf("kbd: failed to register driver '%s'\n",
1509 else if (bootverbose)
1510 printf("kbd: registered driver '%s'\n",