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>
37 #include <sys/clist.h>
39 #include <sys/fcntl.h>
43 #include <sys/selinfo.h>
44 #include <sys/sysctl.h>
49 #include <dev/kbd/kbdreg.h>
51 #define KBD_INDEX(dev) dev2unit(dev)
53 typedef struct genkbd_softc {
54 int gkb_flags; /* flag/status bits */
55 #define KB_ASLEEP (1 << 0)
56 struct clist gkb_q; /* input queue */
57 struct selinfo gkb_rsel;
60 static SLIST_HEAD(, keyboard_driver) keyboard_drivers =
61 SLIST_HEAD_INITIALIZER(keyboard_drivers);
63 SET_DECLARE(kbddriver_set, const keyboard_driver_t);
68 * We need at least one entry each in order to initialize a keyboard
69 * for the kernel console. The arrays will be increased dynamically
73 static int keyboards = 1;
74 static keyboard_t *kbd_ini;
75 static keyboard_t **keyboard = &kbd_ini;
76 static keyboard_switch_t *kbdsw_ini;
77 keyboard_switch_t **kbdsw = &kbdsw_ini;
79 static int keymap_restrict_change;
80 SYSCTL_NODE(_hw, OID_AUTO, kbd, CTLFLAG_RD, 0, "kbd");
81 SYSCTL_INT(_hw_kbd, OID_AUTO, keymap_restrict_change, CTLFLAG_RW,
82 &keymap_restrict_change, 0, "restrict ability to change keymap");
87 kbd_realloc_array(void)
90 keyboard_switch_t **new_kbdsw;
95 newsize = ((keyboards + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
96 new_kbd = malloc(sizeof(*new_kbd)*newsize, M_DEVBUF, M_NOWAIT|M_ZERO);
97 if (new_kbd == NULL) {
101 new_kbdsw = malloc(sizeof(*new_kbdsw)*newsize, M_DEVBUF,
103 if (new_kbdsw == NULL) {
104 free(new_kbd, M_DEVBUF);
108 bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
109 bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards);
111 free(keyboard, M_DEVBUF);
112 free(kbdsw, M_DEVBUF);
120 printf("kbd: new array size %d\n", keyboards);
126 * Low-level keyboard driver functions
127 * Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard
128 * driver, call these functions to initialize the keyboard_t structure
129 * and register it to the virtual keyboard driver `kbd'.
132 /* initialize the keyboard_t structure */
134 kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config,
135 int port, int port_size)
137 kbd->kb_flags = KB_NO_DEVICE; /* device has not been found */
141 kbd->kb_config = config & ~KB_CONF_PROBE_ONLY;
142 kbd->kb_led = 0; /* unknown */
143 kbd->kb_io_base = port;
144 kbd->kb_io_size = port_size;
146 kbd->kb_keymap = NULL;
147 kbd->kb_accentmap = NULL;
148 kbd->kb_fkeytab = NULL;
149 kbd->kb_fkeytab_size = 0;
150 kbd->kb_delay1 = KB_DELAY1; /* these values are advisory only */
151 kbd->kb_delay2 = KB_DELAY2;
153 bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact));
157 kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap,
158 fkeytab_t *fkeymap, int fkeymap_size)
160 kbd->kb_keymap = keymap;
161 kbd->kb_accentmap = accmap;
162 kbd->kb_fkeytab = fkeymap;
163 kbd->kb_fkeytab_size = fkeymap_size;
166 /* declare a new keyboard driver */
168 kbd_add_driver(keyboard_driver_t *driver)
170 if (SLIST_NEXT(driver, link))
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) {
215 keyboard[index] = kbd;
216 kbdsw[index] = p->kbdsw;
219 bzero(&ki, sizeof(ki));
220 strcpy(ki.kb_name, kbd->kb_name);
221 ki.kb_unit = kbd->kb_unit;
223 kbdd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
229 SET_FOREACH(list, kbddriver_set) {
231 if (strcmp(p->name, kbd->kb_name) == 0) {
232 keyboard[index] = kbd;
233 kbdsw[index] = p->kbdsw;
236 bzero(&ki, sizeof(ki));
237 strcpy(ki.kb_name, kbd->kb_name);
238 ki.kb_unit = kbd->kb_unit;
240 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;
276 kbdsw[kbd->kb_index] = NULL;
282 /* find a funciton table by the driver name */
284 *kbd_get_switch(char *driver)
286 const keyboard_driver_t **list;
287 const keyboard_driver_t *p;
289 SLIST_FOREACH(p, &keyboard_drivers, link) {
290 if (strcmp(p->name, driver) == 0)
293 SET_FOREACH(list, kbddriver_set) {
295 if (strcmp(p->name, driver) == 0)
303 * Keyboard client functions
304 * Keyboard clients, such as the console driver `syscons' and the keyboard
305 * cdev driver, use these functions to claim and release a keyboard for
310 * find the keyboard specified by a driver name and a unit number
311 * starting at given index
314 kbd_find_keyboard2(char *driver, int unit, int index)
318 if ((index < 0) || (index >= keyboards))
321 for (i = index; i < keyboards; ++i) {
322 if (keyboard[i] == NULL)
324 if (!KBD_IS_VALID(keyboard[i]))
326 if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver))
328 if ((unit != -1) && (keyboard[i]->kb_unit != unit))
336 /* find the keyboard specified by a driver name and a unit number */
338 kbd_find_keyboard(char *driver, int unit)
340 return (kbd_find_keyboard2(driver, unit, 0));
343 /* allocate a keyboard */
345 kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func,
355 index = kbd_find_keyboard(driver, unit);
357 if (KBD_IS_BUSY(keyboard[index])) {
361 keyboard[index]->kb_token = id;
362 KBD_BUSY(keyboard[index]);
363 keyboard[index]->kb_callback.kc_func = func;
364 keyboard[index]->kb_callback.kc_arg = arg;
365 kbdd_clear_state(keyboard[index]);
372 kbd_release(keyboard_t *kbd, void *id)
378 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
380 } else if (kbd->kb_token != id) {
383 kbd->kb_token = NULL;
385 kbd->kb_callback.kc_func = NULL;
386 kbd->kb_callback.kc_arg = NULL;
387 kbdd_clear_state(kbd);
395 kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func,
402 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
404 } else if (kbd->kb_token != id) {
406 } else if (func == NULL) {
409 kbd->kb_callback.kc_func = func;
410 kbd->kb_callback.kc_arg = arg;
417 /* get a keyboard structure */
419 *kbd_get_keyboard(int index)
421 if ((index < 0) || (index >= keyboards))
423 if (keyboard[index] == NULL)
425 if (!KBD_IS_VALID(keyboard[index]))
427 return (keyboard[index]);
431 * The back door for the console driver; configure keyboards
432 * This function is for the kernel console to initialize keyboards
433 * at very early stage.
437 kbd_configure(int flags)
439 const keyboard_driver_t **list;
440 const keyboard_driver_t *p;
442 SLIST_FOREACH(p, &keyboard_drivers, link) {
443 if (p->configure != NULL)
444 (*p->configure)(flags);
446 SET_FOREACH(list, kbddriver_set) {
448 if (p->configure != NULL)
449 (*p->configure)(flags);
455 #ifdef KBD_INSTALL_CDEV
458 * Virtual keyboard cdev driver functions
459 * The virtual keyboard driver dispatches driver functions to
460 * appropriate subdrivers.
463 #define KBD_UNIT(dev) dev2unit(dev)
465 static d_open_t genkbdopen;
466 static d_close_t genkbdclose;
467 static d_read_t genkbdread;
468 static d_write_t genkbdwrite;
469 static d_ioctl_t genkbdioctl;
470 static d_poll_t genkbdpoll;
473 static struct cdevsw kbd_cdevsw = {
474 .d_version = D_VERSION,
475 .d_flags = D_NEEDGIANT,
476 .d_open = genkbdopen,
477 .d_close = genkbdclose,
478 .d_read = genkbdread,
479 .d_write = genkbdwrite,
480 .d_ioctl = genkbdioctl,
481 .d_poll = genkbdpoll,
486 kbd_attach(keyboard_t *kbd)
489 if (kbd->kb_index >= keyboards)
491 if (keyboard[kbd->kb_index] != kbd)
494 kbd->kb_dev = make_dev(&kbd_cdevsw, kbd->kb_index, UID_ROOT, GID_WHEEL,
495 0600, "%s%r", kbd->kb_name, kbd->kb_unit);
496 make_dev_alias(kbd->kb_dev, "kbd%r", kbd->kb_index);
497 kbd->kb_dev->si_drv1 = malloc(sizeof(genkbd_softc_t), M_DEVBUF,
499 printf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
504 kbd_detach(keyboard_t *kbd)
507 if (kbd->kb_index >= keyboards)
509 if (keyboard[kbd->kb_index] != kbd)
512 free(kbd->kb_dev->si_drv1, M_DEVBUF);
513 destroy_dev(kbd->kb_dev);
519 * Generic keyboard cdev driver functions
520 * Keyboard subdrivers may call these functions to implement common
525 #define KB_BUFSIZE 64
527 static kbd_callback_func_t genkbd_event;
530 genkbdopen(struct cdev *dev, int mode, int flag, struct thread *td)
539 kbd = kbd_get_keyboard(KBD_INDEX(dev));
540 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
544 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
545 genkbd_event, (void *)sc);
550 /* assert(i == kbd->kb_index) */
551 /* assert(kbd == kbd_get_keyboard(i)) */
554 * NOTE: even when we have successfully claimed a keyboard,
555 * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
559 bzero(&sc->gkb_q, sizeof(sc->gkb_q));
561 clist_alloc_cblocks(&sc->gkb_q, KB_QSIZE, KB_QSIZE/2); /* XXX */
568 genkbdclose(struct cdev *dev, int mode, int flag, struct thread *td)
575 * NOTE: the device may have already become invalid.
576 * kbd == NULL || !KBD_IS_VALID(kbd)
580 kbd = kbd_get_keyboard(KBD_INDEX(dev));
581 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
582 /* XXX: we shall be forgiving and don't report error... */
584 kbd_release(kbd, (void *)sc);
586 clist_free_cblocks(&sc->gkb_q);
594 genkbdread(struct cdev *dev, struct uio *uio, int flag)
598 u_char buffer[KB_BUFSIZE];
606 kbd = kbd_get_keyboard(KBD_INDEX(dev));
607 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
611 while (sc->gkb_q.c_cc == 0) {
612 if (flag & O_NONBLOCK) {
614 return (EWOULDBLOCK);
616 sc->gkb_flags |= KB_ASLEEP;
617 error = tsleep(sc, PZERO | PCATCH, "kbdrea", 0);
618 kbd = kbd_get_keyboard(KBD_INDEX(dev));
619 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
621 return (ENXIO); /* our keyboard has gone... */
624 sc->gkb_flags &= ~KB_ASLEEP;
631 /* copy as much input as possible */
633 while (uio->uio_resid > 0) {
634 len = imin(uio->uio_resid, sizeof(buffer));
635 len = q_to_b(&sc->gkb_q, buffer, len);
638 error = uiomove(buffer, len, uio);
647 genkbdwrite(struct cdev *dev, struct uio *uio, int flag)
651 kbd = kbd_get_keyboard(KBD_INDEX(dev));
652 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
658 genkbdioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
663 kbd = kbd_get_keyboard(KBD_INDEX(dev));
664 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
666 error = kbdd_ioctl(kbd, cmd, arg);
667 if (error == ENOIOCTL)
673 genkbdpoll(struct cdev *dev, int events, struct thread *td)
683 kbd = kbd_get_keyboard(KBD_INDEX(dev));
684 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
685 revents = POLLHUP; /* the keyboard has gone */
686 } else if (events & (POLLIN | POLLRDNORM)) {
687 if (sc->gkb_q.c_cc > 0)
688 revents = events & (POLLIN | POLLRDNORM);
690 selrecord(td, &sc->gkb_rsel);
697 genkbd_event(keyboard_t *kbd, int event, void *arg)
705 /* assert(KBD_IS_VALID(kbd)) */
706 sc = (genkbd_softc_t *)arg;
711 case KBDIO_UNLOADING:
712 /* the keyboard is going... */
713 kbd_release(kbd, (void *)sc);
714 if (sc->gkb_flags & KB_ASLEEP) {
715 sc->gkb_flags &= ~KB_ASLEEP;
718 selwakeuppri(&sc->gkb_rsel, PZERO);
724 /* obtain the current key input mode */
725 if (kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode))
728 /* read all pending input */
729 while (kbdd_check_char(kbd)) {
730 c = kbdd_read_char(kbd, FALSE);
733 if (c == ERRKEY) /* XXX: ring bell? */
735 if (!KBD_IS_BUSY(kbd))
736 /* the device is not open, discard the input */
739 /* store the byte as is for K_RAW and K_CODE modes */
740 if (mode != K_XLATE) {
741 putc(KEYCHAR(c), &sc->gkb_q);
746 if (c & RELKEY) /* key release is ignored */
749 /* process special keys; most of them are just ignored... */
751 switch (KEYCHAR(c)) {
755 case BTAB: /* a backtab: ESC [ Z */
756 putc(0x1b, &sc->gkb_q);
757 putc('[', &sc->gkb_q);
758 putc('Z', &sc->gkb_q);
763 /* normal chars, normal chars with the META, function keys */
764 switch (KEYFLAGS(c)) {
765 case 0: /* a normal char */
766 putc(KEYCHAR(c), &sc->gkb_q);
768 case MKEY: /* the META flag: prepend ESC */
769 putc(0x1b, &sc->gkb_q);
770 putc(KEYCHAR(c), &sc->gkb_q);
772 case FKEY | SPCLKEY: /* a function key, return string */
773 cp = kbdd_get_fkeystr(kbd, KEYCHAR(c), &len);
776 putc(*cp++, &sc->gkb_q);
782 /* wake up sleeping/polling processes */
783 if (sc->gkb_q.c_cc > 0) {
784 if (sc->gkb_flags & KB_ASLEEP) {
785 sc->gkb_flags &= ~KB_ASLEEP;
788 selwakeuppri(&sc->gkb_rsel, PZERO);
794 #endif /* KBD_INSTALL_CDEV */
797 * Generic low-level keyboard functions
798 * The low-level functions in the keyboard subdriver may use these
802 #ifndef KBD_DISABLE_KEYMAP_LOAD
803 static int key_change_ok(struct keyent_t *, struct keyent_t *, struct thread *);
804 static int keymap_change_ok(keymap_t *, keymap_t *, struct thread *);
805 static int accent_change_ok(accentmap_t *, accentmap_t *, struct thread *);
806 static int fkey_change_ok(fkeytab_t *, fkeyarg_t *, struct thread *);
810 genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
816 #ifndef KBD_DISABLE_KEYMAP_LOAD
823 case KDGKBINFO: /* get keyboard information */
824 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
825 i = imin(strlen(kbd->kb_name) + 1,
826 sizeof(((keyboard_info_t *)arg)->kb_name));
827 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
828 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
829 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
830 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
831 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
834 case KDGKBTYPE: /* get keyboard type */
835 *(int *)arg = kbd->kb_type;
838 case KDGETREPEAT: /* get keyboard repeat rate */
839 ((int *)arg)[0] = kbd->kb_delay1;
840 ((int *)arg)[1] = kbd->kb_delay2;
843 case GIO_KEYMAP: /* get keyboard translation table */
844 bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap));
846 case PIO_KEYMAP: /* set keyboard translation table */
847 #ifndef KBD_DISABLE_KEYMAP_LOAD
848 error = keymap_change_ok(kbd->kb_keymap, (keymap_t *)arg,
854 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
855 bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
862 case GIO_KEYMAPENT: /* get keyboard translation table entry */
863 keyp = (keyarg_t *)arg;
864 if (keyp->keynum >= sizeof(kbd->kb_keymap->key) /
865 sizeof(kbd->kb_keymap->key[0])) {
869 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
872 case PIO_KEYMAPENT: /* set keyboard translation table entry */
873 #ifndef KBD_DISABLE_KEYMAP_LOAD
874 keyp = (keyarg_t *)arg;
875 if (keyp->keynum >= sizeof(kbd->kb_keymap->key) /
876 sizeof(kbd->kb_keymap->key[0])) {
880 error = key_change_ok(&kbd->kb_keymap->key[keyp->keynum],
881 &keyp->key, curthread);
886 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
894 case GIO_DEADKEYMAP: /* get accent key translation table */
895 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
897 case PIO_DEADKEYMAP: /* set accent key translation table */
898 #ifndef KBD_DISABLE_KEYMAP_LOAD
899 error = accent_change_ok(kbd->kb_accentmap,
900 (accentmap_t *)arg, curthread);
905 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
912 case GETFKEY: /* get functionkey string */
913 fkeyp = (fkeyarg_t *)arg;
914 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
918 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
919 kbd->kb_fkeytab[fkeyp->keynum].len);
920 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
922 case SETFKEY: /* set functionkey string */
923 #ifndef KBD_DISABLE_KEYMAP_LOAD
924 fkeyp = (fkeyarg_t *)arg;
925 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
929 error = fkey_change_ok(&kbd->kb_fkeytab[fkeyp->keynum],
935 kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK);
936 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
937 kbd->kb_fkeytab[fkeyp->keynum].len);
953 #ifndef KBD_DISABLE_KEYMAP_LOAD
954 #define RESTRICTED_KEY(key, i) \
955 ((key->spcl & (0x80 >> i)) && \
956 (key->map[i] == RBT || key->map[i] == SUSP || \
957 key->map[i] == STBY || key->map[i] == DBG || \
958 key->map[i] == PNC || key->map[i] == HALT || \
959 key->map[i] == PDWN))
962 key_change_ok(struct keyent_t *oldkey, struct keyent_t *newkey, struct thread *td)
966 /* Low keymap_restrict_change means any changes are OK. */
967 if (keymap_restrict_change <= 0)
970 /* High keymap_restrict_change means only root can change the keymap. */
971 if (keymap_restrict_change >= 2) {
972 for (i = 0; i < NUM_STATES; i++)
973 if (oldkey->map[i] != newkey->map[i])
974 return priv_check(td, PRIV_KEYBOARD);
975 if (oldkey->spcl != newkey->spcl)
976 return priv_check(td, PRIV_KEYBOARD);
977 if (oldkey->flgs != newkey->flgs)
978 return priv_check(td, PRIV_KEYBOARD);
982 /* Otherwise we have to see if any special keys are being changed. */
983 for (i = 0; i < NUM_STATES; i++) {
985 * If either the oldkey or the newkey action is restricted
986 * then we must make sure that the action doesn't change.
988 if (!RESTRICTED_KEY(oldkey, i) && !RESTRICTED_KEY(newkey, i))
990 if ((oldkey->spcl & (0x80 >> i)) == (newkey->spcl & (0x80 >> i))
991 && oldkey->map[i] == newkey->map[i])
993 return priv_check(td, PRIV_KEYBOARD);
1000 keymap_change_ok(keymap_t *oldmap, keymap_t *newmap, struct thread *td)
1004 for (keycode = 0; keycode < NUM_KEYS; keycode++) {
1005 if ((error = key_change_ok(&oldmap->key[keycode],
1006 &newmap->key[keycode], td)) != 0)
1013 accent_change_ok(accentmap_t *oldmap, accentmap_t *newmap, struct thread *td)
1015 struct acc_t *oldacc, *newacc;
1018 if (keymap_restrict_change <= 2)
1021 if (oldmap->n_accs != newmap->n_accs)
1022 return priv_check(td, PRIV_KEYBOARD);
1024 for (accent = 0; accent < oldmap->n_accs; accent++) {
1025 oldacc = &oldmap->acc[accent];
1026 newacc = &newmap->acc[accent];
1027 if (oldacc->accchar != newacc->accchar)
1028 return priv_check(td, PRIV_KEYBOARD);
1029 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1030 if (oldacc->map[i][0] != newacc->map[i][0])
1031 return priv_check(td, PRIV_KEYBOARD);
1032 if (oldacc->map[i][0] == 0) /* end of table */
1034 if (oldacc->map[i][1] != newacc->map[i][1])
1035 return priv_check(td, PRIV_KEYBOARD);
1043 fkey_change_ok(fkeytab_t *oldkey, fkeyarg_t *newkey, struct thread *td)
1045 if (keymap_restrict_change <= 3)
1048 if (oldkey->len != newkey->flen ||
1049 bcmp(oldkey->str, newkey->keydef, oldkey->len) != 0)
1050 return priv_check(td, PRIV_KEYBOARD);
1056 /* get a pointer to the string associated with the given function key */
1058 *genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
1063 if (fkey > kbd->kb_fkeytab_size)
1065 *len = kbd->kb_fkeytab[fkey].len;
1066 return (kbd->kb_fkeytab[fkey].str);
1069 /* diagnostic dump */
1071 *get_kbd_type_name(int type)
1078 { KB_101, "AT 101/102" },
1079 { KB_OTHER, "generic" },
1083 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
1084 if (type == name_table[i].type)
1085 return (name_table[i].name);
1091 genkbd_diag(keyboard_t *kbd, int level)
1094 printf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
1095 kbd->kb_index, kbd->kb_name, kbd->kb_unit,
1096 get_kbd_type_name(kbd->kb_type), kbd->kb_type,
1097 kbd->kb_config, kbd->kb_flags);
1098 if (kbd->kb_io_base > 0)
1099 printf(", port:0x%x-0x%x", kbd->kb_io_base,
1100 kbd->kb_io_base + kbd->kb_io_size - 1);
1105 #define set_lockkey_state(k, s, l) \
1106 if (!((s) & l ## DOWN)) { \
1110 i = (s) & LOCK_MASK; \
1111 kbdd_ioctl((k), KDSETLED, (caddr_t)&i); \
1115 save_accent_key(keyboard_t *kbd, u_int key, int *accents)
1119 /* make an index into the accent map */
1120 i = key - F_ACC + 1;
1121 if ((i > kbd->kb_accentmap->n_accs)
1122 || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
1123 /* the index is out of range or pointing to an empty entry */
1129 * If the same accent key has been hit twice, produce the accent
1132 if (i == *accents) {
1133 key = kbd->kb_accentmap->acc[i - 1].accchar;
1138 /* remember the index and wait for the next key */
1144 make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
1149 acc = &kbd->kb_accentmap->acc[*accents - 1];
1153 * If the accent key is followed by the space key,
1154 * produce the accent char itself.
1157 return (acc->accchar);
1159 /* scan the accent map */
1160 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1161 if (acc->map[i][0] == 0) /* end of table */
1163 if (acc->map[i][0] == ch)
1164 return (acc->map[i][1]);
1166 /* this char cannot be accented... */
1171 genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1174 struct keyent_t *key;
1175 int state = *shiftstate;
1181 f = state & (AGRS | ALKED);
1182 if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1184 key = &kbd->kb_keymap->key[i];
1185 i = ((state & SHIFTS) ? 1 : 0)
1186 | ((state & CTLS) ? 2 : 0)
1187 | ((state & ALTS) ? 4 : 0);
1188 if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1189 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1192 if (up) { /* break: key released */
1193 action = kbd->kb_lastact[keycode];
1194 kbd->kb_lastact[keycode] = NOP;
1197 if (state & SHIFTAON) {
1198 set_lockkey_state(kbd, state, ALK);
1207 if (state & SHIFTAON) {
1208 set_lockkey_state(kbd, state, ALK);
1217 if (state & SHIFTAON) {
1218 set_lockkey_state(kbd, state, ALK);
1227 if (state & SHIFTAON) {
1228 set_lockkey_state(kbd, state, ALK);
1237 if (state & SHIFTAON) {
1238 set_lockkey_state(kbd, state, ALK);
1247 if (state & SHIFTAON) {
1248 set_lockkey_state(kbd, state, ALK);
1270 i = state & LOCK_MASK;
1271 kbdd_ioctl(kbd, KDSETLED, (caddr_t)&i);
1281 /* release events of regular keys are not reported */
1282 *shiftstate &= ~SHIFTAON;
1285 *shiftstate = state & ~SHIFTAON;
1286 return (SPCLKEY | RELKEY | action);
1287 } else { /* make: key pressed */
1288 action = key->map[i];
1290 if (key->spcl & (0x80 >> i)) {
1292 if (kbd->kb_lastact[keycode] == NOP)
1293 kbd->kb_lastact[keycode] = action;
1294 if (kbd->kb_lastact[keycode] != action)
1299 set_lockkey_state(kbd, state, NLK);
1303 set_lockkey_state(kbd, state, CLK);
1306 i = state & LOCK_MASK;
1307 kbdd_ioctl(kbd, KDSETLED, (caddr_t)&i);
1311 set_lockkey_state(kbd, state, SLK);
1314 set_lockkey_state(kbd, state, ALK);
1316 /* NON-LOCKING KEYS */
1317 case SPSC: case RBT: case SUSP: case STBY:
1318 case DBG: case NEXT: case PREV: case PNC:
1319 case HALT: case PDWN:
1375 *shiftstate = state;
1378 /* is this an accent (dead) key? */
1379 *shiftstate = state;
1380 if (action >= F_ACC && action <= L_ACC) {
1381 action = save_accent_key(kbd, action,
1389 return (action | MKEY);
1395 /* other special keys */
1400 if (action >= F_FN && action <= L_FN)
1402 /* XXX: return fkey string for the FKEY? */
1403 return (SPCLKEY | action);
1405 *shiftstate = state;
1406 return (SPCLKEY | action);
1409 kbd->kb_lastact[keycode] = NOP;
1410 *shiftstate = state;
1412 /* make an accented char */
1413 action = make_accent_char(kbd, action, accents);
1414 if (action == ERRKEY)