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/kbd/kbdreg.h>
52 #define KBD_INDEX(dev) dev2unit(dev)
57 typedef struct genkbd_softc {
58 int gkb_flags; /* flag/status bits */
59 #define KB_ASLEEP (1 << 0)
60 struct selinfo gkb_rsel;
61 char gkb_q[KB_QSIZE]; /* input queue */
62 unsigned int gkb_q_start;
63 unsigned int gkb_q_length;
66 static SLIST_HEAD(, keyboard_driver) keyboard_drivers =
67 SLIST_HEAD_INITIALIZER(keyboard_drivers);
69 SET_DECLARE(kbddriver_set, const keyboard_driver_t);
74 * We need at least one entry each in order to initialize a keyboard
75 * for the kernel console. The arrays will be increased dynamically
79 static int keyboards = 1;
80 static keyboard_t *kbd_ini;
81 static keyboard_t **keyboard = &kbd_ini;
82 static keyboard_switch_t *kbdsw_ini;
83 keyboard_switch_t **kbdsw = &kbdsw_ini;
85 static int keymap_restrict_change;
86 static SYSCTL_NODE(_hw, OID_AUTO, kbd, CTLFLAG_RD, 0, "kbd");
87 SYSCTL_INT(_hw_kbd, OID_AUTO, keymap_restrict_change, CTLFLAG_RW,
88 &keymap_restrict_change, 0, "restrict ability to change keymap");
93 kbd_realloc_array(void)
96 keyboard_switch_t **new_kbdsw;
101 newsize = rounddown(keyboards + ARRAY_DELTA, ARRAY_DELTA);
102 new_kbd = malloc(sizeof(*new_kbd)*newsize, M_DEVBUF, M_NOWAIT|M_ZERO);
103 if (new_kbd == NULL) {
107 new_kbdsw = malloc(sizeof(*new_kbdsw)*newsize, M_DEVBUF,
109 if (new_kbdsw == NULL) {
110 free(new_kbd, M_DEVBUF);
114 bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
115 bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards);
117 free(keyboard, M_DEVBUF);
118 free(kbdsw, M_DEVBUF);
126 printf("kbd: new array size %d\n", keyboards);
132 * Low-level keyboard driver functions
133 * Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard
134 * driver, call these functions to initialize the keyboard_t structure
135 * and register it to the virtual keyboard driver `kbd'.
138 /* initialize the keyboard_t structure */
140 kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config,
141 int port, int port_size)
143 kbd->kb_flags = KB_NO_DEVICE; /* device has not been found */
147 kbd->kb_config = config & ~KB_CONF_PROBE_ONLY;
148 kbd->kb_led = 0; /* unknown */
149 kbd->kb_io_base = port;
150 kbd->kb_io_size = port_size;
152 kbd->kb_keymap = NULL;
153 kbd->kb_accentmap = NULL;
154 kbd->kb_fkeytab = NULL;
155 kbd->kb_fkeytab_size = 0;
156 kbd->kb_delay1 = KB_DELAY1; /* these values are advisory only */
157 kbd->kb_delay2 = KB_DELAY2;
159 bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact));
163 kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap,
164 fkeytab_t *fkeymap, int fkeymap_size)
166 kbd->kb_keymap = keymap;
167 kbd->kb_accentmap = accmap;
168 kbd->kb_fkeytab = fkeymap;
169 kbd->kb_fkeytab_size = fkeymap_size;
172 /* declare a new keyboard driver */
174 kbd_add_driver(keyboard_driver_t *driver)
176 if (SLIST_NEXT(driver, link))
178 SLIST_INSERT_HEAD(&keyboard_drivers, driver, link);
183 kbd_delete_driver(keyboard_driver_t *driver)
185 SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link);
186 SLIST_NEXT(driver, link) = NULL;
190 /* register a keyboard and associate it with a function table */
192 kbd_register(keyboard_t *kbd)
194 const keyboard_driver_t **list;
195 const keyboard_driver_t *p;
200 mux = kbd_get_keyboard(kbd_find_keyboard("kbdmux", -1));
202 for (index = 0; index < keyboards; ++index) {
203 if (keyboard[index] == NULL)
206 if (index >= keyboards) {
207 if (kbd_realloc_array())
211 kbd->kb_index = index;
214 kbd->kb_active = 0; /* disabled until someone calls kbd_enable() */
215 kbd->kb_token = NULL;
216 kbd->kb_callback.kc_func = NULL;
217 kbd->kb_callback.kc_arg = NULL;
219 SLIST_FOREACH(p, &keyboard_drivers, link) {
220 if (strcmp(p->name, kbd->kb_name) == 0) {
221 keyboard[index] = kbd;
222 kbdsw[index] = p->kbdsw;
225 bzero(&ki, sizeof(ki));
226 strcpy(ki.kb_name, kbd->kb_name);
227 ki.kb_unit = kbd->kb_unit;
229 (void)kbdd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
235 SET_FOREACH(list, kbddriver_set) {
237 if (strcmp(p->name, kbd->kb_name) == 0) {
238 keyboard[index] = kbd;
239 kbdsw[index] = p->kbdsw;
242 bzero(&ki, sizeof(ki));
243 strcpy(ki.kb_name, kbd->kb_name);
244 ki.kb_unit = kbd->kb_unit;
246 (void)kbdd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
257 kbd_unregister(keyboard_t *kbd)
262 if ((kbd->kb_index < 0) || (kbd->kb_index >= keyboards))
264 if (keyboard[kbd->kb_index] != kbd)
268 if (KBD_IS_BUSY(kbd)) {
269 error = (*kbd->kb_callback.kc_func)(kbd, KBDIO_UNLOADING,
270 kbd->kb_callback.kc_arg);
275 if (KBD_IS_BUSY(kbd)) {
281 keyboard[kbd->kb_index] = NULL;
282 kbdsw[kbd->kb_index] = NULL;
288 /* find a function table by the driver name */
290 kbd_get_switch(char *driver)
292 const keyboard_driver_t **list;
293 const keyboard_driver_t *p;
295 SLIST_FOREACH(p, &keyboard_drivers, link) {
296 if (strcmp(p->name, driver) == 0)
299 SET_FOREACH(list, kbddriver_set) {
301 if (strcmp(p->name, driver) == 0)
309 * Keyboard client functions
310 * Keyboard clients, such as the console driver `syscons' and the keyboard
311 * cdev driver, use these functions to claim and release a keyboard for
316 * find the keyboard specified by a driver name and a unit number
317 * starting at given index
320 kbd_find_keyboard2(char *driver, int unit, int index)
324 if ((index < 0) || (index >= keyboards))
327 for (i = index; i < keyboards; ++i) {
328 if (keyboard[i] == NULL)
330 if (!KBD_IS_VALID(keyboard[i]))
332 if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver))
334 if ((unit != -1) && (keyboard[i]->kb_unit != unit))
342 /* find the keyboard specified by a driver name and a unit number */
344 kbd_find_keyboard(char *driver, int unit)
346 return (kbd_find_keyboard2(driver, unit, 0));
349 /* allocate a keyboard */
351 kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func,
361 index = kbd_find_keyboard(driver, unit);
363 if (KBD_IS_BUSY(keyboard[index])) {
367 keyboard[index]->kb_token = id;
368 KBD_BUSY(keyboard[index]);
369 keyboard[index]->kb_callback.kc_func = func;
370 keyboard[index]->kb_callback.kc_arg = arg;
371 kbdd_clear_state(keyboard[index]);
378 kbd_release(keyboard_t *kbd, void *id)
384 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
386 } else if (kbd->kb_token != id) {
389 kbd->kb_token = NULL;
391 kbd->kb_callback.kc_func = NULL;
392 kbd->kb_callback.kc_arg = NULL;
393 kbdd_clear_state(kbd);
401 kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func,
408 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
410 } else if (kbd->kb_token != id) {
412 } else if (func == NULL) {
415 kbd->kb_callback.kc_func = func;
416 kbd->kb_callback.kc_arg = arg;
423 /* get a keyboard structure */
425 kbd_get_keyboard(int index)
427 if ((index < 0) || (index >= keyboards))
429 if (keyboard[index] == NULL)
431 if (!KBD_IS_VALID(keyboard[index]))
433 return (keyboard[index]);
437 * The back door for the console driver; configure keyboards
438 * This function is for the kernel console to initialize keyboards
439 * at very early stage.
443 kbd_configure(int flags)
445 const keyboard_driver_t **list;
446 const keyboard_driver_t *p;
448 SLIST_FOREACH(p, &keyboard_drivers, link) {
449 if (p->configure != NULL)
450 (*p->configure)(flags);
452 SET_FOREACH(list, kbddriver_set) {
454 if (p->configure != NULL)
455 (*p->configure)(flags);
461 #ifdef KBD_INSTALL_CDEV
464 * Virtual keyboard cdev driver functions
465 * The virtual keyboard driver dispatches driver functions to
466 * appropriate subdrivers.
469 #define KBD_UNIT(dev) dev2unit(dev)
471 static d_open_t genkbdopen;
472 static d_close_t genkbdclose;
473 static d_read_t genkbdread;
474 static d_write_t genkbdwrite;
475 static d_ioctl_t genkbdioctl;
476 static d_poll_t genkbdpoll;
479 static struct cdevsw kbd_cdevsw = {
480 .d_version = D_VERSION,
481 .d_flags = D_NEEDGIANT,
482 .d_open = genkbdopen,
483 .d_close = genkbdclose,
484 .d_read = genkbdread,
485 .d_write = genkbdwrite,
486 .d_ioctl = genkbdioctl,
487 .d_poll = genkbdpoll,
492 kbd_attach(keyboard_t *kbd)
495 if (kbd->kb_index >= keyboards)
497 if (keyboard[kbd->kb_index] != kbd)
500 kbd->kb_dev = make_dev(&kbd_cdevsw, kbd->kb_index, UID_ROOT, GID_WHEEL,
501 0600, "%s%r", kbd->kb_name, kbd->kb_unit);
502 make_dev_alias(kbd->kb_dev, "kbd%r", kbd->kb_index);
503 kbd->kb_dev->si_drv1 = malloc(sizeof(genkbd_softc_t), M_DEVBUF,
505 printf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
510 kbd_detach(keyboard_t *kbd)
513 if (kbd->kb_index >= keyboards)
515 if (keyboard[kbd->kb_index] != kbd)
518 free(kbd->kb_dev->si_drv1, M_DEVBUF);
519 destroy_dev(kbd->kb_dev);
525 * Generic keyboard cdev driver functions
526 * Keyboard subdrivers may call these functions to implement common
531 genkbd_putc(genkbd_softc_t *sc, char c)
535 if (sc->gkb_q_length == KB_QSIZE)
538 p = (sc->gkb_q_start + sc->gkb_q_length) % KB_QSIZE;
544 genkbd_getc(genkbd_softc_t *sc, char *buf, size_t len)
547 /* Determine copy size. */
548 if (sc->gkb_q_length == 0)
550 if (len >= sc->gkb_q_length)
551 len = sc->gkb_q_length;
552 if (len >= KB_QSIZE - sc->gkb_q_start)
553 len = KB_QSIZE - sc->gkb_q_start;
555 /* Copy out data and progress offset. */
556 memcpy(buf, sc->gkb_q + sc->gkb_q_start, len);
557 sc->gkb_q_start = (sc->gkb_q_start + len) % KB_QSIZE;
558 sc->gkb_q_length -= len;
563 static kbd_callback_func_t genkbd_event;
566 genkbdopen(struct cdev *dev, int mode, int flag, struct thread *td)
575 kbd = kbd_get_keyboard(KBD_INDEX(dev));
576 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
580 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
581 genkbd_event, (void *)sc);
586 /* assert(i == kbd->kb_index) */
587 /* assert(kbd == kbd_get_keyboard(i)) */
590 * NOTE: even when we have successfully claimed a keyboard,
591 * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
594 sc->gkb_q_length = 0;
601 genkbdclose(struct cdev *dev, int mode, int flag, struct thread *td)
608 * NOTE: the device may have already become invalid.
609 * kbd == NULL || !KBD_IS_VALID(kbd)
613 kbd = kbd_get_keyboard(KBD_INDEX(dev));
614 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
615 /* XXX: we shall be forgiving and don't report error... */
617 kbd_release(kbd, (void *)sc);
624 genkbdread(struct cdev *dev, struct uio *uio, int flag)
628 u_char buffer[KB_BUFSIZE];
636 kbd = kbd_get_keyboard(KBD_INDEX(dev));
637 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
641 while (sc->gkb_q_length == 0) {
642 if (flag & O_NONBLOCK) {
644 return (EWOULDBLOCK);
646 sc->gkb_flags |= KB_ASLEEP;
647 error = tsleep(sc, PZERO | PCATCH, "kbdrea", 0);
648 kbd = kbd_get_keyboard(KBD_INDEX(dev));
649 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
651 return (ENXIO); /* our keyboard has gone... */
654 sc->gkb_flags &= ~KB_ASLEEP;
661 /* copy as much input as possible */
663 while (uio->uio_resid > 0) {
664 len = imin(uio->uio_resid, sizeof(buffer));
665 len = genkbd_getc(sc, buffer, len);
668 error = uiomove(buffer, len, uio);
677 genkbdwrite(struct cdev *dev, struct uio *uio, int flag)
681 kbd = kbd_get_keyboard(KBD_INDEX(dev));
682 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
688 genkbdioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
693 kbd = kbd_get_keyboard(KBD_INDEX(dev));
694 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
696 error = kbdd_ioctl(kbd, cmd, arg);
697 if (error == ENOIOCTL)
703 genkbdpoll(struct cdev *dev, int events, struct thread *td)
713 kbd = kbd_get_keyboard(KBD_INDEX(dev));
714 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
715 revents = POLLHUP; /* the keyboard has gone */
716 } else if (events & (POLLIN | POLLRDNORM)) {
717 if (sc->gkb_q_length > 0)
718 revents = events & (POLLIN | POLLRDNORM);
720 selrecord(td, &sc->gkb_rsel);
727 genkbd_event(keyboard_t *kbd, int event, void *arg)
735 /* assert(KBD_IS_VALID(kbd)) */
736 sc = (genkbd_softc_t *)arg;
741 case KBDIO_UNLOADING:
742 /* the keyboard is going... */
743 kbd_release(kbd, (void *)sc);
744 if (sc->gkb_flags & KB_ASLEEP) {
745 sc->gkb_flags &= ~KB_ASLEEP;
748 selwakeuppri(&sc->gkb_rsel, PZERO);
754 /* obtain the current key input mode */
755 if (kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode))
758 /* read all pending input */
759 while (kbdd_check_char(kbd)) {
760 c = kbdd_read_char(kbd, FALSE);
763 if (c == ERRKEY) /* XXX: ring bell? */
765 if (!KBD_IS_BUSY(kbd))
766 /* the device is not open, discard the input */
769 /* store the byte as is for K_RAW and K_CODE modes */
770 if (mode != K_XLATE) {
771 genkbd_putc(sc, KEYCHAR(c));
776 if (c & RELKEY) /* key release is ignored */
779 /* process special keys; most of them are just ignored... */
781 switch (KEYCHAR(c)) {
785 case BTAB: /* a backtab: ESC [ Z */
786 genkbd_putc(sc, 0x1b);
787 genkbd_putc(sc, '[');
788 genkbd_putc(sc, 'Z');
793 /* normal chars, normal chars with the META, function keys */
794 switch (KEYFLAGS(c)) {
795 case 0: /* a normal char */
796 genkbd_putc(sc, KEYCHAR(c));
798 case MKEY: /* the META flag: prepend ESC */
799 genkbd_putc(sc, 0x1b);
800 genkbd_putc(sc, KEYCHAR(c));
802 case FKEY | SPCLKEY: /* a function key, return string */
803 cp = kbdd_get_fkeystr(kbd, KEYCHAR(c), &len);
806 genkbd_putc(sc, *cp++);
812 /* wake up sleeping/polling processes */
813 if (sc->gkb_q_length > 0) {
814 if (sc->gkb_flags & KB_ASLEEP) {
815 sc->gkb_flags &= ~KB_ASLEEP;
818 selwakeuppri(&sc->gkb_rsel, PZERO);
824 #endif /* KBD_INSTALL_CDEV */
827 * Generic low-level keyboard functions
828 * The low-level functions in the keyboard subdriver may use these
832 #ifndef KBD_DISABLE_KEYMAP_LOAD
833 static int key_change_ok(struct keyent_t *, struct keyent_t *, struct thread *);
834 static int keymap_change_ok(keymap_t *, keymap_t *, struct thread *);
835 static int accent_change_ok(accentmap_t *, accentmap_t *, struct thread *);
836 static int fkey_change_ok(fkeytab_t *, fkeyarg_t *, struct thread *);
840 genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
853 case KDGKBINFO: /* get keyboard information */
854 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
855 i = imin(strlen(kbd->kb_name) + 1,
856 sizeof(((keyboard_info_t *)arg)->kb_name));
857 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
858 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
859 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
860 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
861 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
864 case KDGKBTYPE: /* get keyboard type */
865 *(int *)arg = kbd->kb_type;
868 case KDGETREPEAT: /* get keyboard repeat rate */
869 ((int *)arg)[0] = kbd->kb_delay1;
870 ((int *)arg)[1] = kbd->kb_delay2;
873 case GIO_KEYMAP: /* get keyboard translation table */
874 error = copyout(kbd->kb_keymap, *(void **)arg,
878 case OGIO_KEYMAP: /* get keyboard translation table (compat) */
879 mapp = kbd->kb_keymap;
880 omapp = (okeymap_t *)arg;
881 omapp->n_keys = mapp->n_keys;
882 for (i = 0; i < NUM_KEYS; i++) {
883 for (j = 0; j < NUM_STATES; j++)
884 omapp->key[i].map[j] =
886 omapp->key[i].spcl = mapp->key[i].spcl;
887 omapp->key[i].flgs = mapp->key[i].flgs;
890 case PIO_KEYMAP: /* set keyboard translation table */
891 case OPIO_KEYMAP: /* set keyboard translation table (compat) */
892 #ifndef KBD_DISABLE_KEYMAP_LOAD
893 mapp = malloc(sizeof *mapp, M_TEMP, M_WAITOK);
894 if (cmd == OPIO_KEYMAP) {
895 omapp = (okeymap_t *)arg;
896 mapp->n_keys = omapp->n_keys;
897 for (i = 0; i < NUM_KEYS; i++) {
898 for (j = 0; j < NUM_STATES; j++)
899 mapp->key[i].map[j] =
900 omapp->key[i].map[j];
901 mapp->key[i].spcl = omapp->key[i].spcl;
902 mapp->key[i].flgs = omapp->key[i].flgs;
905 error = copyin(*(void **)arg, mapp, sizeof *mapp);
913 error = keymap_change_ok(kbd->kb_keymap, mapp, curthread);
919 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
920 bcopy(mapp, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
928 case GIO_KEYMAPENT: /* get keyboard translation table entry */
929 keyp = (keyarg_t *)arg;
930 if (keyp->keynum >= sizeof(kbd->kb_keymap->key) /
931 sizeof(kbd->kb_keymap->key[0])) {
935 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
938 case PIO_KEYMAPENT: /* set keyboard translation table entry */
939 #ifndef KBD_DISABLE_KEYMAP_LOAD
940 keyp = (keyarg_t *)arg;
941 if (keyp->keynum >= sizeof(kbd->kb_keymap->key) /
942 sizeof(kbd->kb_keymap->key[0])) {
946 error = key_change_ok(&kbd->kb_keymap->key[keyp->keynum],
947 &keyp->key, curthread);
952 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
960 case GIO_DEADKEYMAP: /* get accent key translation table */
961 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
963 case PIO_DEADKEYMAP: /* set accent key translation table */
964 #ifndef KBD_DISABLE_KEYMAP_LOAD
965 error = accent_change_ok(kbd->kb_accentmap,
966 (accentmap_t *)arg, curthread);
971 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
978 case GETFKEY: /* get functionkey string */
979 fkeyp = (fkeyarg_t *)arg;
980 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
984 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
985 kbd->kb_fkeytab[fkeyp->keynum].len);
986 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
988 case SETFKEY: /* set functionkey string */
989 #ifndef KBD_DISABLE_KEYMAP_LOAD
990 fkeyp = (fkeyarg_t *)arg;
991 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
995 error = fkey_change_ok(&kbd->kb_fkeytab[fkeyp->keynum],
1001 kbd->kb_fkeytab[fkeyp->keynum].len = min(fkeyp->flen, MAXFK);
1002 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
1003 kbd->kb_fkeytab[fkeyp->keynum].len);
1019 #ifndef KBD_DISABLE_KEYMAP_LOAD
1020 #define RESTRICTED_KEY(key, i) \
1021 ((key->spcl & (0x80 >> i)) && \
1022 (key->map[i] == RBT || key->map[i] == SUSP || \
1023 key->map[i] == STBY || key->map[i] == DBG || \
1024 key->map[i] == PNC || key->map[i] == HALT || \
1025 key->map[i] == PDWN))
1028 key_change_ok(struct keyent_t *oldkey, struct keyent_t *newkey, struct thread *td)
1032 /* Low keymap_restrict_change means any changes are OK. */
1033 if (keymap_restrict_change <= 0)
1036 /* High keymap_restrict_change means only root can change the keymap. */
1037 if (keymap_restrict_change >= 2) {
1038 for (i = 0; i < NUM_STATES; i++)
1039 if (oldkey->map[i] != newkey->map[i])
1040 return priv_check(td, PRIV_KEYBOARD);
1041 if (oldkey->spcl != newkey->spcl)
1042 return priv_check(td, PRIV_KEYBOARD);
1043 if (oldkey->flgs != newkey->flgs)
1044 return priv_check(td, PRIV_KEYBOARD);
1048 /* Otherwise we have to see if any special keys are being changed. */
1049 for (i = 0; i < NUM_STATES; i++) {
1051 * If either the oldkey or the newkey action is restricted
1052 * then we must make sure that the action doesn't change.
1054 if (!RESTRICTED_KEY(oldkey, i) && !RESTRICTED_KEY(newkey, i))
1056 if ((oldkey->spcl & (0x80 >> i)) == (newkey->spcl & (0x80 >> i))
1057 && oldkey->map[i] == newkey->map[i])
1059 return priv_check(td, PRIV_KEYBOARD);
1066 keymap_change_ok(keymap_t *oldmap, keymap_t *newmap, struct thread *td)
1070 for (keycode = 0; keycode < NUM_KEYS; keycode++) {
1071 if ((error = key_change_ok(&oldmap->key[keycode],
1072 &newmap->key[keycode], td)) != 0)
1079 accent_change_ok(accentmap_t *oldmap, accentmap_t *newmap, struct thread *td)
1081 struct acc_t *oldacc, *newacc;
1084 if (keymap_restrict_change <= 2)
1087 if (oldmap->n_accs != newmap->n_accs)
1088 return priv_check(td, PRIV_KEYBOARD);
1090 for (accent = 0; accent < oldmap->n_accs; accent++) {
1091 oldacc = &oldmap->acc[accent];
1092 newacc = &newmap->acc[accent];
1093 if (oldacc->accchar != newacc->accchar)
1094 return priv_check(td, PRIV_KEYBOARD);
1095 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1096 if (oldacc->map[i][0] != newacc->map[i][0])
1097 return priv_check(td, PRIV_KEYBOARD);
1098 if (oldacc->map[i][0] == 0) /* end of table */
1100 if (oldacc->map[i][1] != newacc->map[i][1])
1101 return priv_check(td, PRIV_KEYBOARD);
1109 fkey_change_ok(fkeytab_t *oldkey, fkeyarg_t *newkey, struct thread *td)
1111 if (keymap_restrict_change <= 3)
1114 if (oldkey->len != newkey->flen ||
1115 bcmp(oldkey->str, newkey->keydef, oldkey->len) != 0)
1116 return priv_check(td, PRIV_KEYBOARD);
1122 /* get a pointer to the string associated with the given function key */
1124 genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
1129 if (fkey > kbd->kb_fkeytab_size)
1131 *len = kbd->kb_fkeytab[fkey].len;
1132 return (kbd->kb_fkeytab[fkey].str);
1135 /* diagnostic dump */
1137 get_kbd_type_name(int type)
1144 { KB_101, "AT 101/102" },
1145 { KB_OTHER, "generic" },
1149 for (i = 0; i < nitems(name_table); ++i) {
1150 if (type == name_table[i].type)
1151 return (name_table[i].name);
1157 genkbd_diag(keyboard_t *kbd, int level)
1160 printf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
1161 kbd->kb_index, kbd->kb_name, kbd->kb_unit,
1162 get_kbd_type_name(kbd->kb_type), kbd->kb_type,
1163 kbd->kb_config, kbd->kb_flags);
1164 if (kbd->kb_io_base > 0)
1165 printf(", port:0x%x-0x%x", kbd->kb_io_base,
1166 kbd->kb_io_base + kbd->kb_io_size - 1);
1171 #define set_lockkey_state(k, s, l) \
1172 if (!((s) & l ## DOWN)) { \
1176 i = (s) & LOCK_MASK; \
1177 (void)kbdd_ioctl((k), KDSETLED, (caddr_t)&i); \
1181 save_accent_key(keyboard_t *kbd, u_int key, int *accents)
1185 /* make an index into the accent map */
1186 i = key - F_ACC + 1;
1187 if ((i > kbd->kb_accentmap->n_accs)
1188 || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
1189 /* the index is out of range or pointing to an empty entry */
1195 * If the same accent key has been hit twice, produce the accent
1198 if (i == *accents) {
1199 key = kbd->kb_accentmap->acc[i - 1].accchar;
1204 /* remember the index and wait for the next key */
1210 make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
1215 acc = &kbd->kb_accentmap->acc[*accents - 1];
1219 * If the accent key is followed by the space key,
1220 * produce the accent char itself.
1223 return (acc->accchar);
1225 /* scan the accent map */
1226 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1227 if (acc->map[i][0] == 0) /* end of table */
1229 if (acc->map[i][0] == ch)
1230 return (acc->map[i][1]);
1232 /* this char cannot be accented... */
1237 genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1240 struct keyent_t *key;
1241 int state = *shiftstate;
1247 f = state & (AGRS | ALKED);
1248 if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1250 key = &kbd->kb_keymap->key[i];
1251 i = ((state & SHIFTS) ? 1 : 0)
1252 | ((state & CTLS) ? 2 : 0)
1253 | ((state & ALTS) ? 4 : 0);
1254 if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1255 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1258 if (up) { /* break: key released */
1259 action = kbd->kb_lastact[keycode];
1260 kbd->kb_lastact[keycode] = NOP;
1263 if (state & SHIFTAON) {
1264 set_lockkey_state(kbd, state, ALK);
1273 if (state & SHIFTAON) {
1274 set_lockkey_state(kbd, state, ALK);
1283 if (state & SHIFTAON) {
1284 set_lockkey_state(kbd, state, ALK);
1293 if (state & SHIFTAON) {
1294 set_lockkey_state(kbd, state, ALK);
1303 if (state & SHIFTAON) {
1304 set_lockkey_state(kbd, state, ALK);
1313 if (state & SHIFTAON) {
1314 set_lockkey_state(kbd, state, ALK);
1341 /* release events of regular keys are not reported */
1342 *shiftstate &= ~SHIFTAON;
1345 *shiftstate = state & ~SHIFTAON;
1346 return (SPCLKEY | RELKEY | action);
1347 } else { /* make: key pressed */
1348 action = key->map[i];
1350 if (key->spcl & (0x80 >> i)) {
1352 if (kbd->kb_lastact[keycode] == NOP)
1353 kbd->kb_lastact[keycode] = action;
1354 if (kbd->kb_lastact[keycode] != action)
1359 set_lockkey_state(kbd, state, NLK);
1362 set_lockkey_state(kbd, state, CLK);
1365 set_lockkey_state(kbd, state, SLK);
1368 set_lockkey_state(kbd, state, ALK);
1370 /* NON-LOCKING KEYS */
1371 case SPSC: case RBT: case SUSP: case STBY:
1372 case DBG: case NEXT: case PREV: case PNC:
1373 case HALT: case PDWN:
1429 *shiftstate = state;
1432 /* is this an accent (dead) key? */
1433 *shiftstate = state;
1434 if (action >= F_ACC && action <= L_ACC) {
1435 action = save_accent_key(kbd, action,
1443 return (action | MKEY);
1449 /* other special keys */
1454 if (action >= F_FN && action <= L_FN)
1456 /* XXX: return fkey string for the FKEY? */
1457 return (SPCLKEY | action);
1459 *shiftstate = state;
1460 return (SPCLKEY | action);
1463 kbd->kb_lastact[keycode] = NOP;
1464 *shiftstate = state;
1466 /* make an accented char */
1467 action = make_accent_char(kbd, action, accents);
1468 if (action == ERRKEY)