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 static 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 (void)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 (void)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)
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 error = copyout(kbd->kb_keymap, *(void **)arg,
876 case OGIO_KEYMAP: /* get keyboard translation table (compat) */
877 mapp = kbd->kb_keymap;
878 omapp = (okeymap_t *)arg;
879 omapp->n_keys = mapp->n_keys;
880 for (i = 0; i < NUM_KEYS; i++) {
881 for (j = 0; j < NUM_STATES; j++)
882 omapp->key[i].map[j] =
884 omapp->key[i].spcl = mapp->key[i].spcl;
885 omapp->key[i].flgs = mapp->key[i].flgs;
888 case PIO_KEYMAP: /* set keyboard translation table */
889 case OPIO_KEYMAP: /* set keyboard translation table (compat) */
890 #ifndef KBD_DISABLE_KEYMAP_LOAD
891 mapp = malloc(sizeof *mapp, M_TEMP, M_NOWAIT);
892 if (cmd == OPIO_KEYMAP) {
893 omapp = (okeymap_t *)arg;
894 mapp->n_keys = omapp->n_keys;
895 for (i = 0; i < NUM_KEYS; i++) {
896 for (j = 0; j < NUM_STATES; j++)
897 mapp->key[i].map[j] =
898 omapp->key[i].map[j];
899 mapp->key[i].spcl = omapp->key[i].spcl;
900 mapp->key[i].flgs = omapp->key[i].flgs;
903 error = copyin(*(void **)arg, mapp, sizeof *mapp);
911 error = keymap_change_ok(kbd->kb_keymap, mapp, curthread);
917 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
918 bcopy(mapp, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
926 case GIO_KEYMAPENT: /* get keyboard translation table entry */
927 keyp = (keyarg_t *)arg;
928 if (keyp->keynum >= sizeof(kbd->kb_keymap->key) /
929 sizeof(kbd->kb_keymap->key[0])) {
933 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
936 case PIO_KEYMAPENT: /* set keyboard translation table entry */
937 #ifndef KBD_DISABLE_KEYMAP_LOAD
938 keyp = (keyarg_t *)arg;
939 if (keyp->keynum >= sizeof(kbd->kb_keymap->key) /
940 sizeof(kbd->kb_keymap->key[0])) {
944 error = key_change_ok(&kbd->kb_keymap->key[keyp->keynum],
945 &keyp->key, curthread);
950 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
958 case GIO_DEADKEYMAP: /* get accent key translation table */
959 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
961 case PIO_DEADKEYMAP: /* set accent key translation table */
962 #ifndef KBD_DISABLE_KEYMAP_LOAD
963 error = accent_change_ok(kbd->kb_accentmap,
964 (accentmap_t *)arg, curthread);
969 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
976 case GETFKEY: /* get functionkey string */
977 fkeyp = (fkeyarg_t *)arg;
978 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
982 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
983 kbd->kb_fkeytab[fkeyp->keynum].len);
984 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
986 case SETFKEY: /* set functionkey string */
987 #ifndef KBD_DISABLE_KEYMAP_LOAD
988 fkeyp = (fkeyarg_t *)arg;
989 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
993 error = fkey_change_ok(&kbd->kb_fkeytab[fkeyp->keynum],
999 kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK);
1000 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
1001 kbd->kb_fkeytab[fkeyp->keynum].len);
1017 #ifndef KBD_DISABLE_KEYMAP_LOAD
1018 #define RESTRICTED_KEY(key, i) \
1019 ((key->spcl & (0x80 >> i)) && \
1020 (key->map[i] == RBT || key->map[i] == SUSP || \
1021 key->map[i] == STBY || key->map[i] == DBG || \
1022 key->map[i] == PNC || key->map[i] == HALT || \
1023 key->map[i] == PDWN))
1026 key_change_ok(struct keyent_t *oldkey, struct keyent_t *newkey, struct thread *td)
1030 /* Low keymap_restrict_change means any changes are OK. */
1031 if (keymap_restrict_change <= 0)
1034 /* High keymap_restrict_change means only root can change the keymap. */
1035 if (keymap_restrict_change >= 2) {
1036 for (i = 0; i < NUM_STATES; i++)
1037 if (oldkey->map[i] != newkey->map[i])
1038 return priv_check(td, PRIV_KEYBOARD);
1039 if (oldkey->spcl != newkey->spcl)
1040 return priv_check(td, PRIV_KEYBOARD);
1041 if (oldkey->flgs != newkey->flgs)
1042 return priv_check(td, PRIV_KEYBOARD);
1046 /* Otherwise we have to see if any special keys are being changed. */
1047 for (i = 0; i < NUM_STATES; i++) {
1049 * If either the oldkey or the newkey action is restricted
1050 * then we must make sure that the action doesn't change.
1052 if (!RESTRICTED_KEY(oldkey, i) && !RESTRICTED_KEY(newkey, i))
1054 if ((oldkey->spcl & (0x80 >> i)) == (newkey->spcl & (0x80 >> i))
1055 && oldkey->map[i] == newkey->map[i])
1057 return priv_check(td, PRIV_KEYBOARD);
1064 keymap_change_ok(keymap_t *oldmap, keymap_t *newmap, struct thread *td)
1068 for (keycode = 0; keycode < NUM_KEYS; keycode++) {
1069 if ((error = key_change_ok(&oldmap->key[keycode],
1070 &newmap->key[keycode], td)) != 0)
1077 accent_change_ok(accentmap_t *oldmap, accentmap_t *newmap, struct thread *td)
1079 struct acc_t *oldacc, *newacc;
1082 if (keymap_restrict_change <= 2)
1085 if (oldmap->n_accs != newmap->n_accs)
1086 return priv_check(td, PRIV_KEYBOARD);
1088 for (accent = 0; accent < oldmap->n_accs; accent++) {
1089 oldacc = &oldmap->acc[accent];
1090 newacc = &newmap->acc[accent];
1091 if (oldacc->accchar != newacc->accchar)
1092 return priv_check(td, PRIV_KEYBOARD);
1093 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1094 if (oldacc->map[i][0] != newacc->map[i][0])
1095 return priv_check(td, PRIV_KEYBOARD);
1096 if (oldacc->map[i][0] == 0) /* end of table */
1098 if (oldacc->map[i][1] != newacc->map[i][1])
1099 return priv_check(td, PRIV_KEYBOARD);
1107 fkey_change_ok(fkeytab_t *oldkey, fkeyarg_t *newkey, struct thread *td)
1109 if (keymap_restrict_change <= 3)
1112 if (oldkey->len != newkey->flen ||
1113 bcmp(oldkey->str, newkey->keydef, oldkey->len) != 0)
1114 return priv_check(td, PRIV_KEYBOARD);
1120 /* get a pointer to the string associated with the given function key */
1122 *genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
1127 if (fkey > kbd->kb_fkeytab_size)
1129 *len = kbd->kb_fkeytab[fkey].len;
1130 return (kbd->kb_fkeytab[fkey].str);
1133 /* diagnostic dump */
1135 *get_kbd_type_name(int type)
1142 { KB_101, "AT 101/102" },
1143 { KB_OTHER, "generic" },
1147 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
1148 if (type == name_table[i].type)
1149 return (name_table[i].name);
1155 genkbd_diag(keyboard_t *kbd, int level)
1158 printf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
1159 kbd->kb_index, kbd->kb_name, kbd->kb_unit,
1160 get_kbd_type_name(kbd->kb_type), kbd->kb_type,
1161 kbd->kb_config, kbd->kb_flags);
1162 if (kbd->kb_io_base > 0)
1163 printf(", port:0x%x-0x%x", kbd->kb_io_base,
1164 kbd->kb_io_base + kbd->kb_io_size - 1);
1169 #define set_lockkey_state(k, s, l) \
1170 if (!((s) & l ## DOWN)) { \
1174 i = (s) & LOCK_MASK; \
1175 (void)kbdd_ioctl((k), KDSETLED, (caddr_t)&i); \
1179 save_accent_key(keyboard_t *kbd, u_int key, int *accents)
1183 /* make an index into the accent map */
1184 i = key - F_ACC + 1;
1185 if ((i > kbd->kb_accentmap->n_accs)
1186 || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
1187 /* the index is out of range or pointing to an empty entry */
1193 * If the same accent key has been hit twice, produce the accent
1196 if (i == *accents) {
1197 key = kbd->kb_accentmap->acc[i - 1].accchar;
1202 /* remember the index and wait for the next key */
1208 make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
1213 acc = &kbd->kb_accentmap->acc[*accents - 1];
1217 * If the accent key is followed by the space key,
1218 * produce the accent char itself.
1221 return (acc->accchar);
1223 /* scan the accent map */
1224 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1225 if (acc->map[i][0] == 0) /* end of table */
1227 if (acc->map[i][0] == ch)
1228 return (acc->map[i][1]);
1230 /* this char cannot be accented... */
1235 genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1238 struct keyent_t *key;
1239 int state = *shiftstate;
1245 f = state & (AGRS | ALKED);
1246 if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1248 key = &kbd->kb_keymap->key[i];
1249 i = ((state & SHIFTS) ? 1 : 0)
1250 | ((state & CTLS) ? 2 : 0)
1251 | ((state & ALTS) ? 4 : 0);
1252 if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1253 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1256 if (up) { /* break: key released */
1257 action = kbd->kb_lastact[keycode];
1258 kbd->kb_lastact[keycode] = NOP;
1261 if (state & SHIFTAON) {
1262 set_lockkey_state(kbd, state, ALK);
1271 if (state & SHIFTAON) {
1272 set_lockkey_state(kbd, state, ALK);
1281 if (state & SHIFTAON) {
1282 set_lockkey_state(kbd, state, ALK);
1291 if (state & SHIFTAON) {
1292 set_lockkey_state(kbd, state, ALK);
1301 if (state & SHIFTAON) {
1302 set_lockkey_state(kbd, state, ALK);
1311 if (state & SHIFTAON) {
1312 set_lockkey_state(kbd, state, ALK);
1334 i = state & LOCK_MASK;
1335 (void)kbdd_ioctl(kbd, KDSETLED, (caddr_t)&i);
1345 /* release events of regular keys are not reported */
1346 *shiftstate &= ~SHIFTAON;
1349 *shiftstate = state & ~SHIFTAON;
1350 return (SPCLKEY | RELKEY | action);
1351 } else { /* make: key pressed */
1352 action = key->map[i];
1354 if (key->spcl & (0x80 >> i)) {
1356 if (kbd->kb_lastact[keycode] == NOP)
1357 kbd->kb_lastact[keycode] = action;
1358 if (kbd->kb_lastact[keycode] != action)
1363 set_lockkey_state(kbd, state, NLK);
1367 set_lockkey_state(kbd, state, CLK);
1370 i = state & LOCK_MASK;
1371 (void)kbdd_ioctl(kbd, KDSETLED, (caddr_t)&i);
1375 set_lockkey_state(kbd, state, SLK);
1378 set_lockkey_state(kbd, state, ALK);
1380 /* NON-LOCKING KEYS */
1381 case SPSC: case RBT: case SUSP: case STBY:
1382 case DBG: case NEXT: case PREV: case PNC:
1383 case HALT: case PDWN:
1439 *shiftstate = state;
1442 /* is this an accent (dead) key? */
1443 *shiftstate = state;
1444 if (action >= F_ACC && action <= L_ACC) {
1445 action = save_accent_key(kbd, action,
1453 return (action | MKEY);
1459 /* other special keys */
1464 if (action >= F_FN && action <= L_FN)
1466 /* XXX: return fkey string for the FKEY? */
1467 return (SPCLKEY | action);
1469 *shiftstate = state;
1470 return (SPCLKEY | action);
1473 kbd->kb_lastact[keycode] = NOP;
1474 *shiftstate = state;
1476 /* make an accented char */
1477 action = make_accent_char(kbd, action, accents);
1478 if (action == ERRKEY)