2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer as
10 * the first lines of this file unmodified.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
38 #include <sys/fcntl.h>
42 #include <sys/selinfo.h>
43 #include <sys/sysctl.h>
48 #include <dev/kbd/kbdreg.h>
50 #define KBD_INDEX(dev) dev2unit(dev)
55 typedef struct genkbd_softc {
56 int gkb_flags; /* flag/status bits */
57 #define KB_ASLEEP (1 << 0)
58 struct selinfo gkb_rsel;
59 char gkb_q[KB_QSIZE]; /* input queue */
60 unsigned int gkb_q_start;
61 unsigned int gkb_q_length;
64 static SLIST_HEAD(, keyboard_driver) keyboard_drivers =
65 SLIST_HEAD_INITIALIZER(keyboard_drivers);
67 SET_DECLARE(kbddriver_set, const keyboard_driver_t);
72 * We need at least one entry each in order to initialize a keyboard
73 * for the kernel console. The arrays will be increased dynamically
77 static int keyboards = 1;
78 static keyboard_t *kbd_ini;
79 static keyboard_t **keyboard = &kbd_ini;
80 static keyboard_switch_t *kbdsw_ini;
81 keyboard_switch_t **kbdsw = &kbdsw_ini;
83 static int keymap_restrict_change;
84 SYSCTL_NODE(_hw, OID_AUTO, kbd, CTLFLAG_RD, 0, "kbd");
85 SYSCTL_INT(_hw_kbd, OID_AUTO, keymap_restrict_change, CTLFLAG_RW,
86 &keymap_restrict_change, 0, "restrict ability to change keymap");
91 kbd_realloc_array(void)
94 keyboard_switch_t **new_kbdsw;
99 newsize = ((keyboards + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
100 new_kbd = malloc(sizeof(*new_kbd)*newsize, M_DEVBUF, M_NOWAIT|M_ZERO);
101 if (new_kbd == NULL) {
105 new_kbdsw = malloc(sizeof(*new_kbdsw)*newsize, M_DEVBUF,
107 if (new_kbdsw == NULL) {
108 free(new_kbd, M_DEVBUF);
112 bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
113 bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards);
115 free(keyboard, M_DEVBUF);
116 free(kbdsw, M_DEVBUF);
124 printf("kbd: new array size %d\n", keyboards);
130 * Low-level keyboard driver functions
131 * Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard
132 * driver, call these functions to initialize the keyboard_t structure
133 * and register it to the virtual keyboard driver `kbd'.
136 /* initialize the keyboard_t structure */
138 kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config,
139 int port, int port_size)
141 kbd->kb_flags = KB_NO_DEVICE; /* device has not been found */
145 kbd->kb_config = config & ~KB_CONF_PROBE_ONLY;
146 kbd->kb_led = 0; /* unknown */
147 kbd->kb_io_base = port;
148 kbd->kb_io_size = port_size;
150 kbd->kb_keymap = NULL;
151 kbd->kb_accentmap = NULL;
152 kbd->kb_fkeytab = NULL;
153 kbd->kb_fkeytab_size = 0;
154 kbd->kb_delay1 = KB_DELAY1; /* these values are advisory only */
155 kbd->kb_delay2 = KB_DELAY2;
157 bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact));
161 kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap,
162 fkeytab_t *fkeymap, int fkeymap_size)
164 kbd->kb_keymap = keymap;
165 kbd->kb_accentmap = accmap;
166 kbd->kb_fkeytab = fkeymap;
167 kbd->kb_fkeytab_size = fkeymap_size;
170 /* declare a new keyboard driver */
172 kbd_add_driver(keyboard_driver_t *driver)
174 if (SLIST_NEXT(driver, link))
176 SLIST_INSERT_HEAD(&keyboard_drivers, driver, link);
181 kbd_delete_driver(keyboard_driver_t *driver)
183 SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link);
184 SLIST_NEXT(driver, link) = NULL;
188 /* register a keyboard and associate it with a function table */
190 kbd_register(keyboard_t *kbd)
192 const keyboard_driver_t **list;
193 const keyboard_driver_t *p;
198 mux = kbd_get_keyboard(kbd_find_keyboard("kbdmux", -1));
200 for (index = 0; index < keyboards; ++index) {
201 if (keyboard[index] == NULL)
204 if (index >= keyboards) {
205 if (kbd_realloc_array())
209 kbd->kb_index = index;
212 kbd->kb_active = 0; /* disabled until someone calls kbd_enable() */
213 kbd->kb_token = NULL;
214 kbd->kb_callback.kc_func = NULL;
215 kbd->kb_callback.kc_arg = NULL;
217 SLIST_FOREACH(p, &keyboard_drivers, link) {
218 if (strcmp(p->name, kbd->kb_name) == 0) {
219 keyboard[index] = kbd;
220 kbdsw[index] = p->kbdsw;
223 bzero(&ki, sizeof(ki));
224 strcpy(ki.kb_name, kbd->kb_name);
225 ki.kb_unit = kbd->kb_unit;
227 kbdd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
233 SET_FOREACH(list, kbddriver_set) {
235 if (strcmp(p->name, kbd->kb_name) == 0) {
236 keyboard[index] = kbd;
237 kbdsw[index] = p->kbdsw;
240 bzero(&ki, sizeof(ki));
241 strcpy(ki.kb_name, kbd->kb_name);
242 ki.kb_unit = kbd->kb_unit;
244 kbdd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
255 kbd_unregister(keyboard_t *kbd)
260 if ((kbd->kb_index < 0) || (kbd->kb_index >= keyboards))
262 if (keyboard[kbd->kb_index] != kbd)
266 if (KBD_IS_BUSY(kbd)) {
267 error = (*kbd->kb_callback.kc_func)(kbd, KBDIO_UNLOADING,
268 kbd->kb_callback.kc_arg);
273 if (KBD_IS_BUSY(kbd)) {
279 keyboard[kbd->kb_index] = NULL;
280 kbdsw[kbd->kb_index] = NULL;
286 /* find a funciton table by the driver name */
288 *kbd_get_switch(char *driver)
290 const keyboard_driver_t **list;
291 const keyboard_driver_t *p;
293 SLIST_FOREACH(p, &keyboard_drivers, link) {
294 if (strcmp(p->name, driver) == 0)
297 SET_FOREACH(list, kbddriver_set) {
299 if (strcmp(p->name, driver) == 0)
307 * Keyboard client functions
308 * Keyboard clients, such as the console driver `syscons' and the keyboard
309 * cdev driver, use these functions to claim and release a keyboard for
314 * find the keyboard specified by a driver name and a unit number
315 * starting at given index
318 kbd_find_keyboard2(char *driver, int unit, int index)
322 if ((index < 0) || (index >= keyboards))
325 for (i = index; i < keyboards; ++i) {
326 if (keyboard[i] == NULL)
328 if (!KBD_IS_VALID(keyboard[i]))
330 if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver))
332 if ((unit != -1) && (keyboard[i]->kb_unit != unit))
340 /* find the keyboard specified by a driver name and a unit number */
342 kbd_find_keyboard(char *driver, int unit)
344 return (kbd_find_keyboard2(driver, unit, 0));
347 /* allocate a keyboard */
349 kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func,
359 index = kbd_find_keyboard(driver, unit);
361 if (KBD_IS_BUSY(keyboard[index])) {
365 keyboard[index]->kb_token = id;
366 KBD_BUSY(keyboard[index]);
367 keyboard[index]->kb_callback.kc_func = func;
368 keyboard[index]->kb_callback.kc_arg = arg;
369 kbdd_clear_state(keyboard[index]);
376 kbd_release(keyboard_t *kbd, void *id)
382 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
384 } else if (kbd->kb_token != id) {
387 kbd->kb_token = NULL;
389 kbd->kb_callback.kc_func = NULL;
390 kbd->kb_callback.kc_arg = NULL;
391 kbdd_clear_state(kbd);
399 kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func,
406 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
408 } else if (kbd->kb_token != id) {
410 } else if (func == NULL) {
413 kbd->kb_callback.kc_func = func;
414 kbd->kb_callback.kc_arg = arg;
421 /* get a keyboard structure */
423 *kbd_get_keyboard(int index)
425 if ((index < 0) || (index >= keyboards))
427 if (keyboard[index] == NULL)
429 if (!KBD_IS_VALID(keyboard[index]))
431 return (keyboard[index]);
435 * The back door for the console driver; configure keyboards
436 * This function is for the kernel console to initialize keyboards
437 * at very early stage.
441 kbd_configure(int flags)
443 const keyboard_driver_t **list;
444 const keyboard_driver_t *p;
446 SLIST_FOREACH(p, &keyboard_drivers, link) {
447 if (p->configure != NULL)
448 (*p->configure)(flags);
450 SET_FOREACH(list, kbddriver_set) {
452 if (p->configure != NULL)
453 (*p->configure)(flags);
459 #ifdef KBD_INSTALL_CDEV
462 * Virtual keyboard cdev driver functions
463 * The virtual keyboard driver dispatches driver functions to
464 * appropriate subdrivers.
467 #define KBD_UNIT(dev) dev2unit(dev)
469 static d_open_t genkbdopen;
470 static d_close_t genkbdclose;
471 static d_read_t genkbdread;
472 static d_write_t genkbdwrite;
473 static d_ioctl_t genkbdioctl;
474 static d_poll_t genkbdpoll;
477 static struct cdevsw kbd_cdevsw = {
478 .d_version = D_VERSION,
479 .d_flags = D_NEEDGIANT,
480 .d_open = genkbdopen,
481 .d_close = genkbdclose,
482 .d_read = genkbdread,
483 .d_write = genkbdwrite,
484 .d_ioctl = genkbdioctl,
485 .d_poll = genkbdpoll,
490 kbd_attach(keyboard_t *kbd)
493 if (kbd->kb_index >= keyboards)
495 if (keyboard[kbd->kb_index] != kbd)
498 kbd->kb_dev = make_dev(&kbd_cdevsw, kbd->kb_index, UID_ROOT, GID_WHEEL,
499 0600, "%s%r", kbd->kb_name, kbd->kb_unit);
500 make_dev_alias(kbd->kb_dev, "kbd%r", kbd->kb_index);
501 kbd->kb_dev->si_drv1 = malloc(sizeof(genkbd_softc_t), M_DEVBUF,
503 printf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
508 kbd_detach(keyboard_t *kbd)
511 if (kbd->kb_index >= keyboards)
513 if (keyboard[kbd->kb_index] != kbd)
516 free(kbd->kb_dev->si_drv1, M_DEVBUF);
517 destroy_dev(kbd->kb_dev);
523 * Generic keyboard cdev driver functions
524 * Keyboard subdrivers may call these functions to implement common
529 genkbd_putc(genkbd_softc_t *sc, char c)
533 if (sc->gkb_q_length == KB_QSIZE)
536 p = (sc->gkb_q_start + sc->gkb_q_length) % KB_QSIZE;
542 genkbd_getc(genkbd_softc_t *sc, char *buf, size_t len)
545 /* Determine copy size. */
546 if (sc->gkb_q_length == 0)
548 if (len >= sc->gkb_q_length)
549 len = sc->gkb_q_length;
550 if (len >= KB_QSIZE - sc->gkb_q_start)
551 len = KB_QSIZE - sc->gkb_q_start;
553 /* Copy out data and progress offset. */
554 memcpy(buf, sc->gkb_q + sc->gkb_q_start, len);
555 sc->gkb_q_start = (sc->gkb_q_start + len) % KB_QSIZE;
556 sc->gkb_q_length -= len;
561 static kbd_callback_func_t genkbd_event;
564 genkbdopen(struct cdev *dev, int mode, int flag, struct thread *td)
573 kbd = kbd_get_keyboard(KBD_INDEX(dev));
574 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
578 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
579 genkbd_event, (void *)sc);
584 /* assert(i == kbd->kb_index) */
585 /* assert(kbd == kbd_get_keyboard(i)) */
588 * NOTE: even when we have successfully claimed a keyboard,
589 * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
592 sc->gkb_q_length = 0;
599 genkbdclose(struct cdev *dev, int mode, int flag, struct thread *td)
606 * NOTE: the device may have already become invalid.
607 * kbd == NULL || !KBD_IS_VALID(kbd)
611 kbd = kbd_get_keyboard(KBD_INDEX(dev));
612 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
613 /* XXX: we shall be forgiving and don't report error... */
615 kbd_release(kbd, (void *)sc);
622 genkbdread(struct cdev *dev, struct uio *uio, int flag)
626 u_char buffer[KB_BUFSIZE];
634 kbd = kbd_get_keyboard(KBD_INDEX(dev));
635 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
639 while (sc->gkb_q_length == 0) {
640 if (flag & O_NONBLOCK) {
642 return (EWOULDBLOCK);
644 sc->gkb_flags |= KB_ASLEEP;
645 error = tsleep(sc, PZERO | PCATCH, "kbdrea", 0);
646 kbd = kbd_get_keyboard(KBD_INDEX(dev));
647 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
649 return (ENXIO); /* our keyboard has gone... */
652 sc->gkb_flags &= ~KB_ASLEEP;
659 /* copy as much input as possible */
661 while (uio->uio_resid > 0) {
662 len = imin(uio->uio_resid, sizeof(buffer));
663 len = genkbd_getc(sc, buffer, len);
666 error = uiomove(buffer, len, uio);
675 genkbdwrite(struct cdev *dev, struct uio *uio, int flag)
679 kbd = kbd_get_keyboard(KBD_INDEX(dev));
680 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
686 genkbdioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
691 kbd = kbd_get_keyboard(KBD_INDEX(dev));
692 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
694 error = kbdd_ioctl(kbd, cmd, arg);
695 if (error == ENOIOCTL)
701 genkbdpoll(struct cdev *dev, int events, struct thread *td)
711 kbd = kbd_get_keyboard(KBD_INDEX(dev));
712 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
713 revents = POLLHUP; /* the keyboard has gone */
714 } else if (events & (POLLIN | POLLRDNORM)) {
715 if (sc->gkb_q_length > 0)
716 revents = events & (POLLIN | POLLRDNORM);
718 selrecord(td, &sc->gkb_rsel);
725 genkbd_event(keyboard_t *kbd, int event, void *arg)
733 /* assert(KBD_IS_VALID(kbd)) */
734 sc = (genkbd_softc_t *)arg;
739 case KBDIO_UNLOADING:
740 /* the keyboard is going... */
741 kbd_release(kbd, (void *)sc);
742 if (sc->gkb_flags & KB_ASLEEP) {
743 sc->gkb_flags &= ~KB_ASLEEP;
746 selwakeuppri(&sc->gkb_rsel, PZERO);
752 /* obtain the current key input mode */
753 if (kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode))
756 /* read all pending input */
757 while (kbdd_check_char(kbd)) {
758 c = kbdd_read_char(kbd, FALSE);
761 if (c == ERRKEY) /* XXX: ring bell? */
763 if (!KBD_IS_BUSY(kbd))
764 /* the device is not open, discard the input */
767 /* store the byte as is for K_RAW and K_CODE modes */
768 if (mode != K_XLATE) {
769 genkbd_putc(sc, KEYCHAR(c));
774 if (c & RELKEY) /* key release is ignored */
777 /* process special keys; most of them are just ignored... */
779 switch (KEYCHAR(c)) {
783 case BTAB: /* a backtab: ESC [ Z */
784 genkbd_putc(sc, 0x1b);
785 genkbd_putc(sc, '[');
786 genkbd_putc(sc, 'Z');
791 /* normal chars, normal chars with the META, function keys */
792 switch (KEYFLAGS(c)) {
793 case 0: /* a normal char */
794 genkbd_putc(sc, KEYCHAR(c));
796 case MKEY: /* the META flag: prepend ESC */
797 genkbd_putc(sc, 0x1b);
798 genkbd_putc(sc, KEYCHAR(c));
800 case FKEY | SPCLKEY: /* a function key, return string */
801 cp = kbdd_get_fkeystr(kbd, KEYCHAR(c), &len);
804 genkbd_putc(sc, *cp++);
810 /* wake up sleeping/polling processes */
811 if (sc->gkb_q_length > 0) {
812 if (sc->gkb_flags & KB_ASLEEP) {
813 sc->gkb_flags &= ~KB_ASLEEP;
816 selwakeuppri(&sc->gkb_rsel, PZERO);
822 #endif /* KBD_INSTALL_CDEV */
825 * Generic low-level keyboard functions
826 * The low-level functions in the keyboard subdriver may use these
830 #ifndef KBD_DISABLE_KEYMAP_LOAD
831 static int key_change_ok(struct keyent_t *, struct keyent_t *, struct thread *);
832 static int keymap_change_ok(keymap_t *, keymap_t *, struct thread *);
833 static int accent_change_ok(accentmap_t *, accentmap_t *, struct thread *);
834 static int fkey_change_ok(fkeytab_t *, fkeyarg_t *, struct thread *);
838 genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
844 #ifndef KBD_DISABLE_KEYMAP_LOAD
851 case KDGKBINFO: /* get keyboard information */
852 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
853 i = imin(strlen(kbd->kb_name) + 1,
854 sizeof(((keyboard_info_t *)arg)->kb_name));
855 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
856 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
857 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
858 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
859 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
862 case KDGKBTYPE: /* get keyboard type */
863 *(int *)arg = kbd->kb_type;
866 case KDGETREPEAT: /* get keyboard repeat rate */
867 ((int *)arg)[0] = kbd->kb_delay1;
868 ((int *)arg)[1] = kbd->kb_delay2;
871 case GIO_KEYMAP: /* get keyboard translation table */
872 bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap));
874 case PIO_KEYMAP: /* set keyboard translation table */
875 #ifndef KBD_DISABLE_KEYMAP_LOAD
876 error = keymap_change_ok(kbd->kb_keymap, (keymap_t *)arg,
882 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
883 bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
890 case GIO_KEYMAPENT: /* get keyboard translation table entry */
891 keyp = (keyarg_t *)arg;
892 if (keyp->keynum >= sizeof(kbd->kb_keymap->key) /
893 sizeof(kbd->kb_keymap->key[0])) {
897 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
900 case PIO_KEYMAPENT: /* set keyboard translation table entry */
901 #ifndef KBD_DISABLE_KEYMAP_LOAD
902 keyp = (keyarg_t *)arg;
903 if (keyp->keynum >= sizeof(kbd->kb_keymap->key) /
904 sizeof(kbd->kb_keymap->key[0])) {
908 error = key_change_ok(&kbd->kb_keymap->key[keyp->keynum],
909 &keyp->key, curthread);
914 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
922 case GIO_DEADKEYMAP: /* get accent key translation table */
923 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
925 case PIO_DEADKEYMAP: /* set accent key translation table */
926 #ifndef KBD_DISABLE_KEYMAP_LOAD
927 error = accent_change_ok(kbd->kb_accentmap,
928 (accentmap_t *)arg, curthread);
933 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
940 case GETFKEY: /* get functionkey string */
941 fkeyp = (fkeyarg_t *)arg;
942 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
946 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
947 kbd->kb_fkeytab[fkeyp->keynum].len);
948 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
950 case SETFKEY: /* set functionkey string */
951 #ifndef KBD_DISABLE_KEYMAP_LOAD
952 fkeyp = (fkeyarg_t *)arg;
953 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
957 error = fkey_change_ok(&kbd->kb_fkeytab[fkeyp->keynum],
963 kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK);
964 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
965 kbd->kb_fkeytab[fkeyp->keynum].len);
981 #ifndef KBD_DISABLE_KEYMAP_LOAD
982 #define RESTRICTED_KEY(key, i) \
983 ((key->spcl & (0x80 >> i)) && \
984 (key->map[i] == RBT || key->map[i] == SUSP || \
985 key->map[i] == STBY || key->map[i] == DBG || \
986 key->map[i] == PNC || key->map[i] == HALT || \
987 key->map[i] == PDWN))
990 key_change_ok(struct keyent_t *oldkey, struct keyent_t *newkey, struct thread *td)
994 /* Low keymap_restrict_change means any changes are OK. */
995 if (keymap_restrict_change <= 0)
998 /* High keymap_restrict_change means only root can change the keymap. */
999 if (keymap_restrict_change >= 2) {
1000 for (i = 0; i < NUM_STATES; i++)
1001 if (oldkey->map[i] != newkey->map[i])
1002 return priv_check(td, PRIV_KEYBOARD);
1003 if (oldkey->spcl != newkey->spcl)
1004 return priv_check(td, PRIV_KEYBOARD);
1005 if (oldkey->flgs != newkey->flgs)
1006 return priv_check(td, PRIV_KEYBOARD);
1010 /* Otherwise we have to see if any special keys are being changed. */
1011 for (i = 0; i < NUM_STATES; i++) {
1013 * If either the oldkey or the newkey action is restricted
1014 * then we must make sure that the action doesn't change.
1016 if (!RESTRICTED_KEY(oldkey, i) && !RESTRICTED_KEY(newkey, i))
1018 if ((oldkey->spcl & (0x80 >> i)) == (newkey->spcl & (0x80 >> i))
1019 && oldkey->map[i] == newkey->map[i])
1021 return priv_check(td, PRIV_KEYBOARD);
1028 keymap_change_ok(keymap_t *oldmap, keymap_t *newmap, struct thread *td)
1032 for (keycode = 0; keycode < NUM_KEYS; keycode++) {
1033 if ((error = key_change_ok(&oldmap->key[keycode],
1034 &newmap->key[keycode], td)) != 0)
1041 accent_change_ok(accentmap_t *oldmap, accentmap_t *newmap, struct thread *td)
1043 struct acc_t *oldacc, *newacc;
1046 if (keymap_restrict_change <= 2)
1049 if (oldmap->n_accs != newmap->n_accs)
1050 return priv_check(td, PRIV_KEYBOARD);
1052 for (accent = 0; accent < oldmap->n_accs; accent++) {
1053 oldacc = &oldmap->acc[accent];
1054 newacc = &newmap->acc[accent];
1055 if (oldacc->accchar != newacc->accchar)
1056 return priv_check(td, PRIV_KEYBOARD);
1057 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1058 if (oldacc->map[i][0] != newacc->map[i][0])
1059 return priv_check(td, PRIV_KEYBOARD);
1060 if (oldacc->map[i][0] == 0) /* end of table */
1062 if (oldacc->map[i][1] != newacc->map[i][1])
1063 return priv_check(td, PRIV_KEYBOARD);
1071 fkey_change_ok(fkeytab_t *oldkey, fkeyarg_t *newkey, struct thread *td)
1073 if (keymap_restrict_change <= 3)
1076 if (oldkey->len != newkey->flen ||
1077 bcmp(oldkey->str, newkey->keydef, oldkey->len) != 0)
1078 return priv_check(td, PRIV_KEYBOARD);
1084 /* get a pointer to the string associated with the given function key */
1086 *genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
1091 if (fkey > kbd->kb_fkeytab_size)
1093 *len = kbd->kb_fkeytab[fkey].len;
1094 return (kbd->kb_fkeytab[fkey].str);
1097 /* diagnostic dump */
1099 *get_kbd_type_name(int type)
1106 { KB_101, "AT 101/102" },
1107 { KB_OTHER, "generic" },
1111 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
1112 if (type == name_table[i].type)
1113 return (name_table[i].name);
1119 genkbd_diag(keyboard_t *kbd, int level)
1122 printf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
1123 kbd->kb_index, kbd->kb_name, kbd->kb_unit,
1124 get_kbd_type_name(kbd->kb_type), kbd->kb_type,
1125 kbd->kb_config, kbd->kb_flags);
1126 if (kbd->kb_io_base > 0)
1127 printf(", port:0x%x-0x%x", kbd->kb_io_base,
1128 kbd->kb_io_base + kbd->kb_io_size - 1);
1133 #define set_lockkey_state(k, s, l) \
1134 if (!((s) & l ## DOWN)) { \
1138 i = (s) & LOCK_MASK; \
1139 kbdd_ioctl((k), KDSETLED, (caddr_t)&i); \
1143 save_accent_key(keyboard_t *kbd, u_int key, int *accents)
1147 /* make an index into the accent map */
1148 i = key - F_ACC + 1;
1149 if ((i > kbd->kb_accentmap->n_accs)
1150 || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
1151 /* the index is out of range or pointing to an empty entry */
1157 * If the same accent key has been hit twice, produce the accent
1160 if (i == *accents) {
1161 key = kbd->kb_accentmap->acc[i - 1].accchar;
1166 /* remember the index and wait for the next key */
1172 make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
1177 acc = &kbd->kb_accentmap->acc[*accents - 1];
1181 * If the accent key is followed by the space key,
1182 * produce the accent char itself.
1185 return (acc->accchar);
1187 /* scan the accent map */
1188 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1189 if (acc->map[i][0] == 0) /* end of table */
1191 if (acc->map[i][0] == ch)
1192 return (acc->map[i][1]);
1194 /* this char cannot be accented... */
1199 genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1202 struct keyent_t *key;
1203 int state = *shiftstate;
1209 f = state & (AGRS | ALKED);
1210 if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1212 key = &kbd->kb_keymap->key[i];
1213 i = ((state & SHIFTS) ? 1 : 0)
1214 | ((state & CTLS) ? 2 : 0)
1215 | ((state & ALTS) ? 4 : 0);
1216 if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1217 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1220 if (up) { /* break: key released */
1221 action = kbd->kb_lastact[keycode];
1222 kbd->kb_lastact[keycode] = NOP;
1225 if (state & SHIFTAON) {
1226 set_lockkey_state(kbd, state, ALK);
1235 if (state & SHIFTAON) {
1236 set_lockkey_state(kbd, state, ALK);
1245 if (state & SHIFTAON) {
1246 set_lockkey_state(kbd, state, ALK);
1255 if (state & SHIFTAON) {
1256 set_lockkey_state(kbd, state, ALK);
1265 if (state & SHIFTAON) {
1266 set_lockkey_state(kbd, state, ALK);
1275 if (state & SHIFTAON) {
1276 set_lockkey_state(kbd, state, ALK);
1298 i = state & LOCK_MASK;
1299 kbdd_ioctl(kbd, KDSETLED, (caddr_t)&i);
1309 /* release events of regular keys are not reported */
1310 *shiftstate &= ~SHIFTAON;
1313 *shiftstate = state & ~SHIFTAON;
1314 return (SPCLKEY | RELKEY | action);
1315 } else { /* make: key pressed */
1316 action = key->map[i];
1318 if (key->spcl & (0x80 >> i)) {
1320 if (kbd->kb_lastact[keycode] == NOP)
1321 kbd->kb_lastact[keycode] = action;
1322 if (kbd->kb_lastact[keycode] != action)
1327 set_lockkey_state(kbd, state, NLK);
1331 set_lockkey_state(kbd, state, CLK);
1334 i = state & LOCK_MASK;
1335 kbdd_ioctl(kbd, KDSETLED, (caddr_t)&i);
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)