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, const 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)
166 if (SLIST_NEXT(driver, link))
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);
177 kbd_delete_driver(keyboard_driver_t *driver)
179 SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link);
180 SLIST_NEXT(driver, link) = NULL;
184 /* register a keyboard and associate it with a function table */
186 kbd_register(keyboard_t *kbd)
188 const keyboard_driver_t **list;
189 const keyboard_driver_t *p;
194 mux = kbd_get_keyboard(kbd_find_keyboard("kbdmux", -1));
196 for (index = 0; index < keyboards; ++index) {
197 if (keyboard[index] == NULL)
200 if (index >= keyboards) {
201 if (kbd_realloc_array())
205 kbd->kb_index = index;
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;
213 SLIST_FOREACH(p, &keyboard_drivers, link) {
214 if (strcmp(p->name, kbd->kb_name) == 0) {
216 keyboard[index] = kbd;
219 bzero(&ki, sizeof(ki));
220 strcpy(ki.kb_name, kbd->kb_name);
221 ki.kb_unit = kbd->kb_unit;
223 (void)kbdd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
229 SET_FOREACH(list, kbddriver_set) {
231 if (strcmp(p->name, kbd->kb_name) == 0) {
233 keyboard[index] = kbd;
236 bzero(&ki, sizeof(ki));
237 strcpy(ki.kb_name, kbd->kb_name);
238 ki.kb_unit = kbd->kb_unit;
240 (void)kbdd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
251 kbd_unregister(keyboard_t *kbd)
256 if ((kbd->kb_index < 0) || (kbd->kb_index >= keyboards))
258 if (keyboard[kbd->kb_index] != kbd)
262 if (KBD_IS_BUSY(kbd)) {
263 error = (*kbd->kb_callback.kc_func)(kbd, KBDIO_UNLOADING,
264 kbd->kb_callback.kc_arg);
269 if (KBD_IS_BUSY(kbd)) {
275 keyboard[kbd->kb_index] = NULL;
281 /* find a function table by the driver name */
283 kbd_get_switch(char *driver)
285 const keyboard_driver_t **list;
286 const keyboard_driver_t *p;
288 SLIST_FOREACH(p, &keyboard_drivers, link) {
289 if (strcmp(p->name, driver) == 0)
292 SET_FOREACH(list, kbddriver_set) {
294 if (strcmp(p->name, driver) == 0)
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
309 * find the keyboard specified by a driver name and a unit number
310 * starting at given index
313 kbd_find_keyboard2(char *driver, int unit, int index)
317 if ((index < 0) || (index >= keyboards))
320 for (i = index; i < keyboards; ++i) {
321 if (keyboard[i] == NULL)
323 if (!KBD_IS_VALID(keyboard[i]))
325 if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver))
327 if ((unit != -1) && (keyboard[i]->kb_unit != unit))
335 /* find the keyboard specified by a driver name and a unit number */
337 kbd_find_keyboard(char *driver, int unit)
339 return (kbd_find_keyboard2(driver, unit, 0));
342 /* allocate a keyboard */
344 kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func,
354 index = kbd_find_keyboard(driver, unit);
356 if (KBD_IS_BUSY(keyboard[index])) {
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]);
371 kbd_release(keyboard_t *kbd, void *id)
377 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
379 } else if (kbd->kb_token != id) {
382 kbd->kb_token = NULL;
384 kbd->kb_callback.kc_func = NULL;
385 kbd->kb_callback.kc_arg = NULL;
386 kbdd_clear_state(kbd);
394 kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func,
401 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
403 } else if (kbd->kb_token != id) {
405 } else if (func == NULL) {
408 kbd->kb_callback.kc_func = func;
409 kbd->kb_callback.kc_arg = arg;
416 /* get a keyboard structure */
418 kbd_get_keyboard(int index)
420 if ((index < 0) || (index >= keyboards))
422 if (keyboard[index] == NULL)
424 if (!KBD_IS_VALID(keyboard[index]))
426 return (keyboard[index]);
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.
436 kbd_configure(int flags)
438 const keyboard_driver_t **list;
439 const keyboard_driver_t *p;
441 SLIST_FOREACH(p, &keyboard_drivers, link) {
442 if (p->configure != NULL)
443 (*p->configure)(flags);
445 SET_FOREACH(list, kbddriver_set) {
447 if (p->configure != NULL)
448 (*p->configure)(flags);
454 #ifdef KBD_INSTALL_CDEV
457 * Virtual keyboard cdev driver functions
458 * The virtual keyboard driver dispatches driver functions to
459 * appropriate subdrivers.
462 #define KBD_UNIT(dev) dev2unit(dev)
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;
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,
485 kbd_attach(keyboard_t *kbd)
488 if (kbd->kb_index >= keyboards)
490 if (keyboard[kbd->kb_index] != kbd)
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,
498 printf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
503 kbd_detach(keyboard_t *kbd)
506 if (kbd->kb_index >= keyboards)
508 if (keyboard[kbd->kb_index] != kbd)
511 free(kbd->kb_dev->si_drv1, M_DEVBUF);
512 destroy_dev(kbd->kb_dev);
518 * Generic keyboard cdev driver functions
519 * Keyboard subdrivers may call these functions to implement common
524 genkbd_putc(genkbd_softc_t *sc, char c)
528 if (sc->gkb_q_length == KB_QSIZE)
531 p = (sc->gkb_q_start + sc->gkb_q_length) % KB_QSIZE;
537 genkbd_getc(genkbd_softc_t *sc, char *buf, size_t len)
540 /* Determine copy size. */
541 if (sc->gkb_q_length == 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;
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;
556 static kbd_callback_func_t genkbd_event;
559 genkbdopen(struct cdev *dev, int mode, int flag, struct thread *td)
568 kbd = kbd_get_keyboard(KBD_INDEX(dev));
569 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
573 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
574 genkbd_event, (void *)sc);
579 /* assert(i == kbd->kb_index) */
580 /* assert(kbd == kbd_get_keyboard(i)) */
583 * NOTE: even when we have successfully claimed a keyboard,
584 * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
587 sc->gkb_q_length = 0;
594 genkbdclose(struct cdev *dev, int mode, int flag, struct thread *td)
601 * NOTE: the device may have already become invalid.
602 * kbd == NULL || !KBD_IS_VALID(kbd)
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... */
610 kbd_release(kbd, (void *)sc);
617 genkbdread(struct cdev *dev, struct uio *uio, int flag)
621 u_char buffer[KB_BUFSIZE];
629 kbd = kbd_get_keyboard(KBD_INDEX(dev));
630 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
634 while (sc->gkb_q_length == 0) {
635 if (flag & O_NONBLOCK) {
637 return (EWOULDBLOCK);
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)) {
644 return (ENXIO); /* our keyboard has gone... */
647 sc->gkb_flags &= ~KB_ASLEEP;
654 /* copy as much input as possible */
656 while (uio->uio_resid > 0) {
657 len = imin(uio->uio_resid, sizeof(buffer));
658 len = genkbd_getc(sc, buffer, len);
661 error = uiomove(buffer, len, uio);
670 genkbdwrite(struct cdev *dev, struct uio *uio, int flag)
674 kbd = kbd_get_keyboard(KBD_INDEX(dev));
675 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
681 genkbdioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
686 kbd = kbd_get_keyboard(KBD_INDEX(dev));
687 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
689 error = kbdd_ioctl(kbd, cmd, arg);
690 if (error == ENOIOCTL)
696 genkbdpoll(struct cdev *dev, int events, struct thread *td)
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);
713 selrecord(td, &sc->gkb_rsel);
720 genkbd_event(keyboard_t *kbd, int event, void *arg)
728 /* assert(KBD_IS_VALID(kbd)) */
729 sc = (genkbd_softc_t *)arg;
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;
741 selwakeuppri(&sc->gkb_rsel, PZERO);
747 /* obtain the current key input mode */
748 if (kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode))
751 /* read all pending input */
752 while (kbdd_check_char(kbd)) {
753 c = kbdd_read_char(kbd, FALSE);
756 if (c == ERRKEY) /* XXX: ring bell? */
758 if (!KBD_IS_BUSY(kbd))
759 /* the device is not open, discard the input */
762 /* store the byte as is for K_RAW and K_CODE modes */
763 if (mode != K_XLATE) {
764 genkbd_putc(sc, KEYCHAR(c));
769 if (c & RELKEY) /* key release is ignored */
772 /* process special keys; most of them are just ignored... */
774 switch (KEYCHAR(c)) {
778 case BTAB: /* a backtab: ESC [ Z */
779 genkbd_putc(sc, 0x1b);
780 genkbd_putc(sc, '[');
781 genkbd_putc(sc, 'Z');
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));
791 case MKEY: /* the META flag: prepend ESC */
792 genkbd_putc(sc, 0x1b);
793 genkbd_putc(sc, KEYCHAR(c));
795 case FKEY | SPCLKEY: /* a function key, return string */
796 cp = kbdd_get_fkeystr(kbd, KEYCHAR(c), &len);
799 genkbd_putc(sc, *cp++);
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;
811 selwakeuppri(&sc->gkb_rsel, PZERO);
817 #endif /* KBD_INSTALL_CDEV */
820 * Generic low-level keyboard functions
821 * The low-level functions in the keyboard subdriver may use these
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 *);
833 genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
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;
857 case KDGKBTYPE: /* get keyboard type */
858 *(int *)arg = kbd->kb_type;
861 case KDGETREPEAT: /* get keyboard repeat rate */
862 ((int *)arg)[0] = kbd->kb_delay1;
863 ((int *)arg)[1] = kbd->kb_delay2;
866 case GIO_KEYMAP: /* get keyboard translation table */
867 error = copyout(kbd->kb_keymap, *(void **)arg,
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] =
879 omapp->key[i].spcl = mapp->key[i].spcl;
880 omapp->key[i].flgs = mapp->key[i].flgs;
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;
898 error = copyin(*(void **)arg, mapp, sizeof *mapp);
906 error = keymap_change_ok(kbd->kb_keymap, mapp, curthread);
912 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
913 bcopy(mapp, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
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])) {
928 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
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])) {
939 error = key_change_ok(&kbd->kb_keymap->key[keyp->keynum],
940 &keyp->key, curthread);
945 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
953 case GIO_DEADKEYMAP: /* get accent key translation table */
954 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
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);
964 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
971 case GETFKEY: /* get functionkey string */
972 fkeyp = (fkeyarg_t *)arg;
973 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
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;
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) {
988 error = fkey_change_ok(&kbd->kb_fkeytab[fkeyp->keynum],
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);
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))
1021 key_change_ok(struct keyent_t *oldkey, struct keyent_t *newkey, struct thread *td)
1025 /* Low keymap_restrict_change means any changes are OK. */
1026 if (keymap_restrict_change <= 0)
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);
1041 /* Otherwise we have to see if any special keys are being changed. */
1042 for (i = 0; i < NUM_STATES; i++) {
1044 * If either the oldkey or the newkey action is restricted
1045 * then we must make sure that the action doesn't change.
1047 if (!RESTRICTED_KEY(oldkey, i) && !RESTRICTED_KEY(newkey, i))
1049 if ((oldkey->spcl & (0x80 >> i)) == (newkey->spcl & (0x80 >> i))
1050 && oldkey->map[i] == newkey->map[i])
1052 return priv_check(td, PRIV_KEYBOARD);
1059 keymap_change_ok(keymap_t *oldmap, keymap_t *newmap, struct thread *td)
1063 for (keycode = 0; keycode < NUM_KEYS; keycode++) {
1064 if ((error = key_change_ok(&oldmap->key[keycode],
1065 &newmap->key[keycode], td)) != 0)
1072 accent_change_ok(accentmap_t *oldmap, accentmap_t *newmap, struct thread *td)
1074 struct acc_t *oldacc, *newacc;
1077 if (keymap_restrict_change <= 2)
1080 if (oldmap->n_accs != newmap->n_accs)
1081 return priv_check(td, PRIV_KEYBOARD);
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 */
1093 if (oldacc->map[i][1] != newacc->map[i][1])
1094 return priv_check(td, PRIV_KEYBOARD);
1102 fkey_change_ok(fkeytab_t *oldkey, fkeyarg_t *newkey, struct thread *td)
1104 if (keymap_restrict_change <= 3)
1107 if (oldkey->len != newkey->flen ||
1108 bcmp(oldkey->str, newkey->keydef, oldkey->len) != 0)
1109 return priv_check(td, PRIV_KEYBOARD);
1115 /* get a pointer to the string associated with the given function key */
1117 genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
1122 if (fkey > kbd->kb_fkeytab_size)
1124 *len = kbd->kb_fkeytab[fkey].len;
1125 return (kbd->kb_fkeytab[fkey].str);
1128 /* diagnostic dump */
1130 get_kbd_type_name(int type)
1137 { KB_101, "AT 101/102" },
1138 { KB_OTHER, "generic" },
1142 for (i = 0; i < nitems(name_table); ++i) {
1143 if (type == name_table[i].type)
1144 return (name_table[i].name);
1150 genkbd_diag(keyboard_t *kbd, int level)
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);
1164 #define set_lockkey_state(k, s, l) \
1165 if (!((s) & l ## DOWN)) { \
1169 i = (s) & LOCK_MASK; \
1170 (void)kbdd_ioctl((k), KDSETLED, (caddr_t)&i); \
1174 save_accent_key(keyboard_t *kbd, u_int key, int *accents)
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 */
1188 * If the same accent key has been hit twice, produce the accent
1191 if (i == *accents) {
1192 key = kbd->kb_accentmap->acc[i - 1].accchar;
1197 /* remember the index and wait for the next key */
1203 make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
1208 acc = &kbd->kb_accentmap->acc[*accents - 1];
1212 * If the accent key is followed by the space key,
1213 * produce the accent char itself.
1216 return (acc->accchar);
1218 /* scan the accent map */
1219 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1220 if (acc->map[i][0] == 0) /* end of table */
1222 if (acc->map[i][0] == ch)
1223 return (acc->map[i][1]);
1225 /* this char cannot be accented... */
1230 genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1233 struct keyent_t *key;
1234 int state = *shiftstate;
1240 f = state & (AGRS | ALKED);
1241 if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
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)) )
1251 if (up) { /* break: key released */
1252 action = kbd->kb_lastact[keycode];
1253 kbd->kb_lastact[keycode] = NOP;
1256 if (state & SHIFTAON) {
1257 set_lockkey_state(kbd, state, ALK);
1266 if (state & SHIFTAON) {
1267 set_lockkey_state(kbd, state, ALK);
1276 if (state & SHIFTAON) {
1277 set_lockkey_state(kbd, state, ALK);
1286 if (state & SHIFTAON) {
1287 set_lockkey_state(kbd, state, ALK);
1296 if (state & SHIFTAON) {
1297 set_lockkey_state(kbd, state, ALK);
1306 if (state & SHIFTAON) {
1307 set_lockkey_state(kbd, state, ALK);
1334 /* release events of regular keys are not reported */
1335 *shiftstate &= ~SHIFTAON;
1338 *shiftstate = state & ~SHIFTAON;
1339 return (SPCLKEY | RELKEY | action);
1340 } else { /* make: key pressed */
1341 action = key->map[i];
1343 if (key->spcl & (0x80 >> i)) {
1345 if (kbd->kb_lastact[keycode] == NOP)
1346 kbd->kb_lastact[keycode] = action;
1347 if (kbd->kb_lastact[keycode] != action)
1352 set_lockkey_state(kbd, state, NLK);
1355 set_lockkey_state(kbd, state, CLK);
1358 set_lockkey_state(kbd, state, SLK);
1361 set_lockkey_state(kbd, state, ALK);
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:
1422 *shiftstate = state;
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,
1436 return (action | MKEY);
1442 /* other special keys */
1447 if (action >= F_FN && action <= L_FN)
1449 /* XXX: return fkey string for the FKEY? */
1450 return (SPCLKEY | action);
1452 *shiftstate = state;
1453 return (SPCLKEY | action);
1456 kbd->kb_lastact[keycode] = NOP;
1457 *shiftstate = state;
1459 /* make an accented char */
1460 action = make_accent_char(kbd, action, accents);
1461 if (action == ERRKEY)
1473 kbd_ev_event(keyboard_t *kbd, uint16_t type, uint16_t code, int32_t value)
1475 int delay[2], led = 0, leds, oleds;
1477 if (type == EV_LED) {
1478 leds = oleds = KBD_LED_VAL(kbd);
1497 kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
1499 } else if (type == EV_REP && code == REP_DELAY) {
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;
1506 kbdd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);