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>
43 #include <sys/sysctl.h>
48 #include <dev/kbd/kbdreg.h>
50 #define KBD_INDEX(dev) minor(dev)
52 typedef struct genkbd_softc {
53 int gkb_flags; /* flag/status bits */
54 #define KB_ASLEEP (1 << 0)
55 struct clist gkb_q; /* input queue */
56 struct selinfo gkb_rsel;
59 static SLIST_HEAD(, keyboard_driver) keyboard_drivers =
60 SLIST_HEAD_INITIALIZER(keyboard_drivers);
62 SET_DECLARE(kbddriver_set, const keyboard_driver_t);
67 * We need at least one entry each in order to initialize a keyboard
68 * for the kernel console. The arrays will be increased dynamically
72 static int keyboards = 1;
73 static keyboard_t *kbd_ini;
74 static keyboard_t **keyboard = &kbd_ini;
75 static keyboard_switch_t *kbdsw_ini;
76 keyboard_switch_t **kbdsw = &kbdsw_ini;
78 static int keymap_restrict_change;
79 SYSCTL_NODE(_hw, OID_AUTO, kbd, CTLFLAG_RD, 0, "kbd");
80 SYSCTL_INT(_hw_kbd, OID_AUTO, keymap_restrict_change, CTLFLAG_RW,
81 &keymap_restrict_change, 0, "restrict ability to change keymap");
86 kbd_realloc_array(void)
89 keyboard_switch_t **new_kbdsw;
94 newsize = ((keyboards + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
95 new_kbd = malloc(sizeof(*new_kbd)*newsize, M_DEVBUF, M_NOWAIT|M_ZERO);
96 if (new_kbd == NULL) {
100 new_kbdsw = malloc(sizeof(*new_kbdsw)*newsize, M_DEVBUF,
102 if (new_kbdsw == NULL) {
103 free(new_kbd, M_DEVBUF);
107 bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
108 bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards);
110 free(keyboard, M_DEVBUF);
111 free(kbdsw, M_DEVBUF);
119 printf("kbd: new array size %d\n", keyboards);
125 * Low-level keyboard driver functions
126 * Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard
127 * driver, call these functions to initialize the keyboard_t structure
128 * and register it to the virtual keyboard driver `kbd'.
131 /* initialize the keyboard_t structure */
133 kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config,
134 int port, int port_size)
136 kbd->kb_flags = KB_NO_DEVICE; /* device has not been found */
140 kbd->kb_config = config & ~KB_CONF_PROBE_ONLY;
141 kbd->kb_led = 0; /* unknown */
142 kbd->kb_io_base = port;
143 kbd->kb_io_size = port_size;
145 kbd->kb_keymap = NULL;
146 kbd->kb_accentmap = NULL;
147 kbd->kb_fkeytab = NULL;
148 kbd->kb_fkeytab_size = 0;
149 kbd->kb_delay1 = KB_DELAY1; /* these values are advisory only */
150 kbd->kb_delay2 = KB_DELAY2;
152 bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact));
156 kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap,
157 fkeytab_t *fkeymap, int fkeymap_size)
159 kbd->kb_keymap = keymap;
160 kbd->kb_accentmap = accmap;
161 kbd->kb_fkeytab = fkeymap;
162 kbd->kb_fkeytab_size = fkeymap_size;
165 /* declare a new keyboard driver */
167 kbd_add_driver(keyboard_driver_t *driver)
169 if (SLIST_NEXT(driver, link))
171 SLIST_INSERT_HEAD(&keyboard_drivers, driver, link);
176 kbd_delete_driver(keyboard_driver_t *driver)
178 SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link);
179 SLIST_NEXT(driver, link) = NULL;
183 /* register a keyboard and associate it with a function table */
185 kbd_register(keyboard_t *kbd)
187 const keyboard_driver_t **list;
188 const keyboard_driver_t *p;
193 mux = kbd_get_keyboard(kbd_find_keyboard("kbdmux", -1));
195 for (index = 0; index < keyboards; ++index) {
196 if (keyboard[index] == NULL)
199 if (index >= keyboards) {
200 if (kbd_realloc_array())
204 kbd->kb_index = index;
207 kbd->kb_active = 0; /* disabled until someone calls kbd_enable() */
208 kbd->kb_token = NULL;
209 kbd->kb_callback.kc_func = NULL;
210 kbd->kb_callback.kc_arg = NULL;
212 SLIST_FOREACH(p, &keyboard_drivers, link) {
213 if (strcmp(p->name, kbd->kb_name) == 0) {
214 keyboard[index] = kbd;
215 kbdsw[index] = p->kbdsw;
218 bzero(&ki, sizeof(ki));
219 strcpy(ki.kb_name, kbd->kb_name);
220 ki.kb_unit = kbd->kb_unit;
222 (*kbdsw[mux->kb_index]->ioctl)
223 (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 (*kbdsw[mux->kb_index]->ioctl)
241 (mux, KBADDKBD, (caddr_t) &ki);
252 kbd_unregister(keyboard_t *kbd)
257 if ((kbd->kb_index < 0) || (kbd->kb_index >= keyboards))
259 if (keyboard[kbd->kb_index] != kbd)
263 if (KBD_IS_BUSY(kbd)) {
264 error = (*kbd->kb_callback.kc_func)(kbd, KBDIO_UNLOADING,
265 kbd->kb_callback.kc_arg);
270 if (KBD_IS_BUSY(kbd)) {
276 keyboard[kbd->kb_index] = NULL;
277 kbdsw[kbd->kb_index] = NULL;
283 /* find a funciton table by the driver name */
285 *kbd_get_switch(char *driver)
287 const keyboard_driver_t **list;
288 const keyboard_driver_t *p;
290 SLIST_FOREACH(p, &keyboard_drivers, link) {
291 if (strcmp(p->name, driver) == 0)
294 SET_FOREACH(list, kbddriver_set) {
296 if (strcmp(p->name, driver) == 0)
304 * Keyboard client functions
305 * Keyboard clients, such as the console driver `syscons' and the keyboard
306 * cdev driver, use these functions to claim and release a keyboard for
311 * find the keyboard specified by a driver name and a unit number
312 * starting at given index
315 kbd_find_keyboard2(char *driver, int unit, int index)
319 if ((index < 0) || (index >= keyboards))
322 for (i = index; i < keyboards; ++i) {
323 if (keyboard[i] == NULL)
325 if (!KBD_IS_VALID(keyboard[i]))
327 if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver))
329 if ((unit != -1) && (keyboard[i]->kb_unit != unit))
337 /* find the keyboard specified by a driver name and a unit number */
339 kbd_find_keyboard(char *driver, int unit)
341 return (kbd_find_keyboard2(driver, unit, 0));
344 /* allocate a keyboard */
346 kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func,
356 index = kbd_find_keyboard(driver, unit);
358 if (KBD_IS_BUSY(keyboard[index])) {
362 keyboard[index]->kb_token = id;
363 KBD_BUSY(keyboard[index]);
364 keyboard[index]->kb_callback.kc_func = func;
365 keyboard[index]->kb_callback.kc_arg = arg;
366 (*kbdsw[index]->clear_state)(keyboard[index]);
373 kbd_release(keyboard_t *kbd, void *id)
379 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
381 } else if (kbd->kb_token != id) {
384 kbd->kb_token = NULL;
386 kbd->kb_callback.kc_func = NULL;
387 kbd->kb_callback.kc_arg = NULL;
388 (*kbdsw[kbd->kb_index]->clear_state)(kbd);
396 kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func,
403 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
405 } else if (kbd->kb_token != id) {
407 } else if (func == NULL) {
410 kbd->kb_callback.kc_func = func;
411 kbd->kb_callback.kc_arg = arg;
418 /* get a keyboard structure */
420 *kbd_get_keyboard(int index)
422 if ((index < 0) || (index >= keyboards))
424 if (keyboard[index] == NULL)
426 if (!KBD_IS_VALID(keyboard[index]))
428 return (keyboard[index]);
432 * The back door for the console driver; configure keyboards
433 * This function is for the kernel console to initialize keyboards
434 * at very early stage.
438 kbd_configure(int flags)
440 const keyboard_driver_t **list;
441 const keyboard_driver_t *p;
443 SLIST_FOREACH(p, &keyboard_drivers, link) {
444 if (p->configure != NULL)
445 (*p->configure)(flags);
447 SET_FOREACH(list, kbddriver_set) {
449 if (p->configure != NULL)
450 (*p->configure)(flags);
456 #ifdef KBD_INSTALL_CDEV
459 * Virtual keyboard cdev driver functions
460 * The virtual keyboard driver dispatches driver functions to
461 * appropriate subdrivers.
464 #define KBD_UNIT(dev) minor(dev)
466 static d_open_t genkbdopen;
467 static d_close_t genkbdclose;
468 static d_read_t genkbdread;
469 static d_write_t genkbdwrite;
470 static d_ioctl_t genkbdioctl;
471 static d_poll_t genkbdpoll;
474 static struct cdevsw kbd_cdevsw = {
475 .d_version = D_VERSION,
476 .d_flags = D_NEEDGIANT,
477 .d_open = genkbdopen,
478 .d_close = genkbdclose,
479 .d_read = genkbdread,
480 .d_write = genkbdwrite,
481 .d_ioctl = genkbdioctl,
482 .d_poll = genkbdpoll,
487 kbd_attach(keyboard_t *kbd)
490 if (kbd->kb_index >= keyboards)
492 if (keyboard[kbd->kb_index] != kbd)
495 kbd->kb_dev = make_dev(&kbd_cdevsw, kbd->kb_index, UID_ROOT, GID_WHEEL,
496 0600, "%s%r", kbd->kb_name, kbd->kb_unit);
497 make_dev_alias(kbd->kb_dev, "kbd%r", kbd->kb_index);
498 kbd->kb_dev->si_drv1 = malloc(sizeof(genkbd_softc_t), M_DEVBUF,
500 printf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
505 kbd_detach(keyboard_t *kbd)
508 if (kbd->kb_index >= keyboards)
510 if (keyboard[kbd->kb_index] != kbd)
513 free(kbd->kb_dev->si_drv1, M_DEVBUF);
514 destroy_dev(kbd->kb_dev);
520 * Generic keyboard cdev driver functions
521 * Keyboard subdrivers may call these functions to implement common
526 #define KB_BUFSIZE 64
528 static kbd_callback_func_t genkbd_event;
531 genkbdopen(struct cdev *dev, int mode, int flag, struct thread *td)
540 kbd = kbd_get_keyboard(KBD_INDEX(dev));
541 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
545 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
546 genkbd_event, (void *)sc);
551 /* assert(i == kbd->kb_index) */
552 /* assert(kbd == kbd_get_keyboard(i)) */
555 * NOTE: even when we have successfully claimed a keyboard,
556 * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
560 bzero(&sc->gkb_q, sizeof(sc->gkb_q));
562 clist_alloc_cblocks(&sc->gkb_q, KB_QSIZE, KB_QSIZE/2); /* XXX */
569 genkbdclose(struct cdev *dev, int mode, int flag, struct thread *td)
576 * NOTE: the device may have already become invalid.
577 * kbd == NULL || !KBD_IS_VALID(kbd)
581 kbd = kbd_get_keyboard(KBD_INDEX(dev));
582 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
583 /* XXX: we shall be forgiving and don't report error... */
585 kbd_release(kbd, (void *)sc);
587 clist_free_cblocks(&sc->gkb_q);
595 genkbdread(struct cdev *dev, struct uio *uio, int flag)
599 u_char buffer[KB_BUFSIZE];
607 kbd = kbd_get_keyboard(KBD_INDEX(dev));
608 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
612 while (sc->gkb_q.c_cc == 0) {
613 if (flag & O_NONBLOCK) {
615 return (EWOULDBLOCK);
617 sc->gkb_flags |= KB_ASLEEP;
618 error = tsleep(sc, PZERO | PCATCH, "kbdrea", 0);
619 kbd = kbd_get_keyboard(KBD_INDEX(dev));
620 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
622 return (ENXIO); /* our keyboard has gone... */
625 sc->gkb_flags &= ~KB_ASLEEP;
632 /* copy as much input as possible */
634 while (uio->uio_resid > 0) {
635 len = imin(uio->uio_resid, sizeof(buffer));
636 len = q_to_b(&sc->gkb_q, buffer, len);
639 error = uiomove(buffer, len, uio);
648 genkbdwrite(struct cdev *dev, struct uio *uio, int flag)
652 kbd = kbd_get_keyboard(KBD_INDEX(dev));
653 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
659 genkbdioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
664 kbd = kbd_get_keyboard(KBD_INDEX(dev));
665 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
667 error = (*kbdsw[kbd->kb_index]->ioctl)(kbd, cmd, arg);
668 if (error == ENOIOCTL)
674 genkbdpoll(struct cdev *dev, int events, struct thread *td)
684 kbd = kbd_get_keyboard(KBD_INDEX(dev));
685 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
686 revents = POLLHUP; /* the keyboard has gone */
687 } else if (events & (POLLIN | POLLRDNORM)) {
688 if (sc->gkb_q.c_cc > 0)
689 revents = events & (POLLIN | POLLRDNORM);
691 selrecord(td, &sc->gkb_rsel);
698 genkbd_event(keyboard_t *kbd, int event, void *arg)
706 /* assert(KBD_IS_VALID(kbd)) */
707 sc = (genkbd_softc_t *)arg;
712 case KBDIO_UNLOADING:
713 /* the keyboard is going... */
714 kbd_release(kbd, (void *)sc);
715 if (sc->gkb_flags & KB_ASLEEP) {
716 sc->gkb_flags &= ~KB_ASLEEP;
719 selwakeuppri(&sc->gkb_rsel, PZERO);
725 /* obtain the current key input mode */
726 if ((*kbdsw[kbd->kb_index]->ioctl)(kbd, KDGKBMODE, (caddr_t)&mode))
729 /* read all pending input */
730 while ((*kbdsw[kbd->kb_index]->check_char)(kbd)) {
731 c = (*kbdsw[kbd->kb_index]->read_char)(kbd, FALSE);
734 if (c == ERRKEY) /* XXX: ring bell? */
736 if (!KBD_IS_BUSY(kbd))
737 /* the device is not open, discard the input */
740 /* store the byte as is for K_RAW and K_CODE modes */
741 if (mode != K_XLATE) {
742 putc(KEYCHAR(c), &sc->gkb_q);
747 if (c & RELKEY) /* key release is ignored */
750 /* process special keys; most of them are just ignored... */
752 switch (KEYCHAR(c)) {
756 case BTAB: /* a backtab: ESC [ Z */
757 putc(0x1b, &sc->gkb_q);
758 putc('[', &sc->gkb_q);
759 putc('Z', &sc->gkb_q);
764 /* normal chars, normal chars with the META, function keys */
765 switch (KEYFLAGS(c)) {
766 case 0: /* a normal char */
767 putc(KEYCHAR(c), &sc->gkb_q);
769 case MKEY: /* the META flag: prepend ESC */
770 putc(0x1b, &sc->gkb_q);
771 putc(KEYCHAR(c), &sc->gkb_q);
773 case FKEY | SPCLKEY: /* a function key, return string */
774 cp = (*kbdsw[kbd->kb_index]->get_fkeystr)(kbd,
778 putc(*cp++, &sc->gkb_q);
784 /* wake up sleeping/polling processes */
785 if (sc->gkb_q.c_cc > 0) {
786 if (sc->gkb_flags & KB_ASLEEP) {
787 sc->gkb_flags &= ~KB_ASLEEP;
790 selwakeuppri(&sc->gkb_rsel, PZERO);
796 #endif /* KBD_INSTALL_CDEV */
799 * Generic low-level keyboard functions
800 * The low-level functions in the keyboard subdriver may use these
804 #ifndef KBD_DISABLE_KEYMAP_LOAD
805 static int key_change_ok(struct keyent_t *, struct keyent_t *, struct thread *);
806 static int keymap_change_ok(keymap_t *, keymap_t *, struct thread *);
807 static int accent_change_ok(accentmap_t *, accentmap_t *, struct thread *);
808 static int fkey_change_ok(fkeytab_t *, fkeyarg_t *, struct thread *);
812 genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
818 #ifndef KBD_DISABLE_KEYMAP_LOAD
825 case KDGKBINFO: /* get keyboard information */
826 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
827 i = imin(strlen(kbd->kb_name) + 1,
828 sizeof(((keyboard_info_t *)arg)->kb_name));
829 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
830 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
831 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
832 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
833 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
836 case KDGKBTYPE: /* get keyboard type */
837 *(int *)arg = kbd->kb_type;
840 case KDGETREPEAT: /* get keyboard repeat rate */
841 ((int *)arg)[0] = kbd->kb_delay1;
842 ((int *)arg)[1] = kbd->kb_delay2;
845 case GIO_KEYMAP: /* get keyboard translation table */
846 bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap));
848 case PIO_KEYMAP: /* set keyboard translation table */
849 #ifndef KBD_DISABLE_KEYMAP_LOAD
850 error = keymap_change_ok(kbd->kb_keymap, (keymap_t *)arg,
856 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
857 bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
864 case GIO_KEYMAPENT: /* get keyboard translation table entry */
865 keyp = (keyarg_t *)arg;
866 if (keyp->keynum >= sizeof(kbd->kb_keymap->key) /
867 sizeof(kbd->kb_keymap->key[0])) {
871 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
874 case PIO_KEYMAPENT: /* set keyboard translation table entry */
875 #ifndef KBD_DISABLE_KEYMAP_LOAD
876 keyp = (keyarg_t *)arg;
877 if (keyp->keynum >= sizeof(kbd->kb_keymap->key) /
878 sizeof(kbd->kb_keymap->key[0])) {
882 error = key_change_ok(&kbd->kb_keymap->key[keyp->keynum],
883 &keyp->key, curthread);
888 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
896 case GIO_DEADKEYMAP: /* get accent key translation table */
897 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
899 case PIO_DEADKEYMAP: /* set accent key translation table */
900 #ifndef KBD_DISABLE_KEYMAP_LOAD
901 error = accent_change_ok(kbd->kb_accentmap,
902 (accentmap_t *)arg, curthread);
907 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
914 case GETFKEY: /* get functionkey string */
915 fkeyp = (fkeyarg_t *)arg;
916 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
920 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
921 kbd->kb_fkeytab[fkeyp->keynum].len);
922 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
924 case SETFKEY: /* set functionkey string */
925 #ifndef KBD_DISABLE_KEYMAP_LOAD
926 fkeyp = (fkeyarg_t *)arg;
927 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
931 error = fkey_change_ok(&kbd->kb_fkeytab[fkeyp->keynum],
937 kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK);
938 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
939 kbd->kb_fkeytab[fkeyp->keynum].len);
955 #ifndef KBD_DISABLE_KEYMAP_LOAD
956 #define RESTRICTED_KEY(key, i) \
957 ((key->spcl & (0x80 >> i)) && \
958 (key->map[i] == RBT || key->map[i] == SUSP || \
959 key->map[i] == STBY || key->map[i] == DBG || \
960 key->map[i] == PNC || key->map[i] == HALT || \
961 key->map[i] == PDWN))
964 key_change_ok(struct keyent_t *oldkey, struct keyent_t *newkey, struct thread *td)
968 /* Low keymap_restrict_change means any changes are OK. */
969 if (keymap_restrict_change <= 0)
972 /* High keymap_restrict_change means only root can change the keymap. */
973 if (keymap_restrict_change >= 2) {
974 for (i = 0; i < NUM_STATES; i++)
975 if (oldkey->map[i] != newkey->map[i])
976 return priv_check(td, PRIV_KEYBOARD);
977 if (oldkey->spcl != newkey->spcl)
978 return priv_check(td, PRIV_KEYBOARD);
979 if (oldkey->flgs != newkey->flgs)
980 return priv_check(td, PRIV_KEYBOARD);
984 /* Otherwise we have to see if any special keys are being changed. */
985 for (i = 0; i < NUM_STATES; i++) {
987 * If either the oldkey or the newkey action is restricted
988 * then we must make sure that the action doesn't change.
990 if (!RESTRICTED_KEY(oldkey, i) && !RESTRICTED_KEY(newkey, i))
992 if ((oldkey->spcl & (0x80 >> i)) == (newkey->spcl & (0x80 >> i))
993 && oldkey->map[i] == newkey->map[i])
995 return priv_check(td, PRIV_KEYBOARD);
1002 keymap_change_ok(keymap_t *oldmap, keymap_t *newmap, struct thread *td)
1006 for (keycode = 0; keycode < NUM_KEYS; keycode++) {
1007 if ((error = key_change_ok(&oldmap->key[keycode],
1008 &newmap->key[keycode], td)) != 0)
1015 accent_change_ok(accentmap_t *oldmap, accentmap_t *newmap, struct thread *td)
1017 struct acc_t *oldacc, *newacc;
1020 if (keymap_restrict_change <= 2)
1023 if (oldmap->n_accs != newmap->n_accs)
1024 return priv_check(td, PRIV_KEYBOARD);
1026 for (accent = 0; accent < oldmap->n_accs; accent++) {
1027 oldacc = &oldmap->acc[accent];
1028 newacc = &newmap->acc[accent];
1029 if (oldacc->accchar != newacc->accchar)
1030 return priv_check(td, PRIV_KEYBOARD);
1031 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1032 if (oldacc->map[i][0] != newacc->map[i][0])
1033 return priv_check(td, PRIV_KEYBOARD);
1034 if (oldacc->map[i][0] == 0) /* end of table */
1036 if (oldacc->map[i][1] != newacc->map[i][1])
1037 return priv_check(td, PRIV_KEYBOARD);
1045 fkey_change_ok(fkeytab_t *oldkey, fkeyarg_t *newkey, struct thread *td)
1047 if (keymap_restrict_change <= 3)
1050 if (oldkey->len != newkey->flen ||
1051 bcmp(oldkey->str, newkey->keydef, oldkey->len) != 0)
1052 return priv_check(td, PRIV_KEYBOARD);
1058 /* get a pointer to the string associated with the given function key */
1060 *genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
1065 if (fkey > kbd->kb_fkeytab_size)
1067 *len = kbd->kb_fkeytab[fkey].len;
1068 return (kbd->kb_fkeytab[fkey].str);
1071 /* diagnostic dump */
1073 *get_kbd_type_name(int type)
1080 { KB_101, "AT 101/102" },
1081 { KB_OTHER, "generic" },
1085 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
1086 if (type == name_table[i].type)
1087 return (name_table[i].name);
1093 genkbd_diag(keyboard_t *kbd, int level)
1096 printf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
1097 kbd->kb_index, kbd->kb_name, kbd->kb_unit,
1098 get_kbd_type_name(kbd->kb_type), kbd->kb_type,
1099 kbd->kb_config, kbd->kb_flags);
1100 if (kbd->kb_io_base > 0)
1101 printf(", port:0x%x-0x%x", kbd->kb_io_base,
1102 kbd->kb_io_base + kbd->kb_io_size - 1);
1107 #define set_lockkey_state(k, s, l) \
1108 if (!((s) & l ## DOWN)) { \
1112 i = (s) & LOCK_MASK; \
1113 (*kbdsw[(k)->kb_index]->ioctl)((k), KDSETLED, (caddr_t)&i); \
1117 save_accent_key(keyboard_t *kbd, u_int key, int *accents)
1121 /* make an index into the accent map */
1122 i = key - F_ACC + 1;
1123 if ((i > kbd->kb_accentmap->n_accs)
1124 || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
1125 /* the index is out of range or pointing to an empty entry */
1131 * If the same accent key has been hit twice, produce the accent
1134 if (i == *accents) {
1135 key = kbd->kb_accentmap->acc[i - 1].accchar;
1140 /* remember the index and wait for the next key */
1146 make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
1151 acc = &kbd->kb_accentmap->acc[*accents - 1];
1155 * If the accent key is followed by the space key,
1156 * produce the accent char itself.
1159 return (acc->accchar);
1161 /* scan the accent map */
1162 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1163 if (acc->map[i][0] == 0) /* end of table */
1165 if (acc->map[i][0] == ch)
1166 return (acc->map[i][1]);
1168 /* this char cannot be accented... */
1173 genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1176 struct keyent_t *key;
1177 int state = *shiftstate;
1183 f = state & (AGRS | ALKED);
1184 if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1186 key = &kbd->kb_keymap->key[i];
1187 i = ((state & SHIFTS) ? 1 : 0)
1188 | ((state & CTLS) ? 2 : 0)
1189 | ((state & ALTS) ? 4 : 0);
1190 if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1191 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1194 if (up) { /* break: key released */
1195 action = kbd->kb_lastact[keycode];
1196 kbd->kb_lastact[keycode] = NOP;
1199 if (state & SHIFTAON) {
1200 set_lockkey_state(kbd, state, ALK);
1209 if (state & SHIFTAON) {
1210 set_lockkey_state(kbd, state, ALK);
1219 if (state & SHIFTAON) {
1220 set_lockkey_state(kbd, state, ALK);
1229 if (state & SHIFTAON) {
1230 set_lockkey_state(kbd, state, ALK);
1239 if (state & SHIFTAON) {
1240 set_lockkey_state(kbd, state, ALK);
1249 if (state & SHIFTAON) {
1250 set_lockkey_state(kbd, state, ALK);
1272 i = state & LOCK_MASK;
1273 (*kbdsw[kbd->kb_index]->ioctl)(kbd, KDSETLED,
1284 /* release events of regular keys are not reported */
1285 *shiftstate &= ~SHIFTAON;
1288 *shiftstate = state & ~SHIFTAON;
1289 return (SPCLKEY | RELKEY | action);
1290 } else { /* make: key pressed */
1291 action = key->map[i];
1293 if (key->spcl & (0x80 >> i)) {
1295 if (kbd->kb_lastact[keycode] == NOP)
1296 kbd->kb_lastact[keycode] = action;
1297 if (kbd->kb_lastact[keycode] != action)
1302 set_lockkey_state(kbd, state, NLK);
1306 set_lockkey_state(kbd, state, CLK);
1309 i = state & LOCK_MASK;
1310 (*kbdsw[kbd->kb_index]->ioctl)(kbd, KDSETLED,
1315 set_lockkey_state(kbd, state, SLK);
1318 set_lockkey_state(kbd, state, ALK);
1320 /* NON-LOCKING KEYS */
1321 case SPSC: case RBT: case SUSP: case STBY:
1322 case DBG: case NEXT: case PREV: case PNC:
1323 case HALT: case PDWN:
1379 *shiftstate = state;
1382 /* is this an accent (dead) key? */
1383 *shiftstate = state;
1384 if (action >= F_ACC && action <= L_ACC) {
1385 action = save_accent_key(kbd, action,
1393 return (action | MKEY);
1399 /* other special keys */
1404 if (action >= F_FN && action <= L_FN)
1406 /* XXX: return fkey string for the FKEY? */
1407 return (SPCLKEY | action);
1409 *shiftstate = state;
1410 return (SPCLKEY | action);
1413 kbd->kb_lastact[keycode] = NOP;
1414 *shiftstate = state;
1416 /* make an accented char */
1417 action = make_accent_char(kbd, action, accents);
1418 if (action == ERRKEY)