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.
26 * $FreeBSD: src/sys/dev/kbd/kbd.c,v 1.17.2.2 2001/07/30 16:46:43 yokota Exp $
27 * $DragonFly: src/sys/dev/misc/kbd/kbd.c,v 1.8 2004/05/13 19:44:32 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
40 #include <sys/vnode.h>
43 #include <machine/console.h>
47 #define KBD_INDEX(dev) minor(dev)
49 typedef struct genkbd_softc {
50 int gkb_flags; /* flag/status bits */
51 #define KB_ASLEEP (1 << 0)
52 struct clist gkb_q; /* input queue */
53 struct selinfo gkb_rsel;
56 static SLIST_HEAD(, keyboard_driver) keyboard_drivers =
57 SLIST_HEAD_INITIALIZER(keyboard_drivers);
59 SET_DECLARE(kbddriver_set, const keyboard_driver_t);
64 * We need at least one entry each in order to initialize a keyboard
65 * for the kernel console. The arrays will be increased dynamically
69 static int keyboards = 1;
70 static keyboard_t *kbd_ini;
71 static keyboard_t **keyboard = &kbd_ini;
72 static keyboard_switch_t *kbdsw_ini;
73 keyboard_switch_t **kbdsw = &kbdsw_ini;
78 kbd_realloc_array(void)
81 keyboard_switch_t **new_kbdsw;
86 newsize = ((keyboards + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
87 new_kbd = malloc(sizeof(*new_kbd) * newsize, M_DEVBUF,
89 new_kbdsw = malloc(sizeof(*new_kbdsw) * newsize, M_DEVBUF,
91 bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
92 bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards);
94 free(keyboard, M_DEVBUF);
95 free(kbdsw, M_DEVBUF);
103 printf("kbd: new array size %d\n", keyboards);
109 * Low-level keyboard driver functions
110 * Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard
111 * driver, call these functions to initialize the keyboard_t structure
112 * and register it to the virtual keyboard driver `kbd'.
115 /* initialize the keyboard_t structure */
117 kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config,
118 int port, int port_size)
120 kbd->kb_flags = KB_NO_DEVICE; /* device has not been found */
124 kbd->kb_config = config & ~KB_CONF_PROBE_ONLY;
125 kbd->kb_led = 0; /* unknown */
126 kbd->kb_io_base = port;
127 kbd->kb_io_size = port_size;
129 kbd->kb_keymap = NULL;
130 kbd->kb_accentmap = NULL;
131 kbd->kb_fkeytab = NULL;
132 kbd->kb_fkeytab_size = 0;
133 kbd->kb_delay1 = KB_DELAY1; /* these values are advisory only */
134 kbd->kb_delay2 = KB_DELAY2;
136 bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact));
140 kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap,
141 fkeytab_t *fkeymap, int fkeymap_size)
143 kbd->kb_keymap = keymap;
144 kbd->kb_accentmap = accmap;
145 kbd->kb_fkeytab = fkeymap;
146 kbd->kb_fkeytab_size = fkeymap_size;
149 /* declare a new keyboard driver */
151 kbd_add_driver(keyboard_driver_t *driver)
153 if (SLIST_NEXT(driver, link))
155 SLIST_INSERT_HEAD(&keyboard_drivers, driver, link);
160 kbd_delete_driver(keyboard_driver_t *driver)
162 SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link);
163 SLIST_NEXT(driver, link) = NULL;
167 /* register a keyboard and associate it with a function table */
169 kbd_register(keyboard_t *kbd)
171 const keyboard_driver_t **list;
172 const keyboard_driver_t *p;
175 for (index = 0; index < keyboards; ++index) {
176 if (keyboard[index] == NULL)
179 if (index >= keyboards) {
180 if (kbd_realloc_array())
184 kbd->kb_index = index;
187 kbd->kb_active = 0; /* disabled until someone calls kbd_enable() */
188 kbd->kb_token = NULL;
189 kbd->kb_callback.kc_func = NULL;
190 kbd->kb_callback.kc_arg = NULL;
192 SLIST_FOREACH(p, &keyboard_drivers, link) {
193 if (strcmp(p->name, kbd->kb_name) == 0) {
194 keyboard[index] = kbd;
195 kbdsw[index] = p->kbdsw;
199 SET_FOREACH(list, kbddriver_set) {
201 if (strcmp(p->name, kbd->kb_name) == 0) {
202 keyboard[index] = kbd;
203 kbdsw[index] = p->kbdsw;
212 kbd_unregister(keyboard_t *kbd)
217 if ((kbd->kb_index < 0) || (kbd->kb_index >= keyboards))
219 if (keyboard[kbd->kb_index] != kbd)
223 if (KBD_IS_BUSY(kbd)) {
224 error = (*kbd->kb_callback.kc_func)(kbd, KBDIO_UNLOADING,
225 kbd->kb_callback.kc_arg);
230 if (KBD_IS_BUSY(kbd)) {
236 keyboard[kbd->kb_index] = NULL;
237 kbdsw[kbd->kb_index] = NULL;
243 /* find a funciton table by the driver name */
245 *kbd_get_switch(char *driver)
247 const keyboard_driver_t **list;
248 const keyboard_driver_t *p;
250 SLIST_FOREACH(p, &keyboard_drivers, link) {
251 if (strcmp(p->name, driver) == 0)
254 SET_FOREACH(list, kbddriver_set) {
256 if (strcmp(p->name, driver) == 0)
264 * Keyboard client functions
265 * Keyboard clients, such as the console driver `syscons' and the keyboard
266 * cdev driver, use these functions to claim and release a keyboard for
270 /* find the keyboard specified by a driver name and a unit number */
272 kbd_find_keyboard(char *driver, int unit)
276 for (i = 0; i < keyboards; ++i) {
277 if (keyboard[i] == NULL)
279 if (!KBD_IS_VALID(keyboard[i]))
281 if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver))
283 if ((unit != -1) && (keyboard[i]->kb_unit != unit))
290 /* allocate a keyboard */
292 kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func,
302 index = kbd_find_keyboard(driver, unit);
304 if (KBD_IS_BUSY(keyboard[index])) {
308 keyboard[index]->kb_token = id;
309 KBD_BUSY(keyboard[index]);
310 keyboard[index]->kb_callback.kc_func = func;
311 keyboard[index]->kb_callback.kc_arg = arg;
312 (*kbdsw[index]->clear_state)(keyboard[index]);
319 kbd_release(keyboard_t *kbd, void *id)
325 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
327 } else if (kbd->kb_token != id) {
330 kbd->kb_token = NULL;
332 kbd->kb_callback.kc_func = NULL;
333 kbd->kb_callback.kc_arg = NULL;
334 (*kbdsw[kbd->kb_index]->clear_state)(kbd);
342 kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func,
349 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
351 } else if (kbd->kb_token != id) {
353 } else if (func == NULL) {
356 kbd->kb_callback.kc_func = func;
357 kbd->kb_callback.kc_arg = arg;
364 /* get a keyboard structure */
366 *kbd_get_keyboard(int index)
368 if ((index < 0) || (index >= keyboards))
370 if (keyboard[index] == NULL)
372 if (!KBD_IS_VALID(keyboard[index]))
374 return keyboard[index];
378 * The back door for the console driver; configure keyboards
379 * This function is for the kernel console to initialize keyboards
380 * at very early stage.
384 kbd_configure(int flags)
386 const keyboard_driver_t **list;
387 const keyboard_driver_t *p;
389 SLIST_FOREACH(p, &keyboard_drivers, link) {
390 if (p->configure != NULL)
391 (*p->configure)(flags);
393 SET_FOREACH(list, kbddriver_set) {
395 if (p->configure != NULL)
396 (*p->configure)(flags);
402 #ifdef KBD_INSTALL_CDEV
405 * Virtual keyboard cdev driver functions
406 * The virtual keyboard driver dispatches driver functions to
407 * appropriate subdrivers.
410 #define KBD_UNIT(dev) minor(dev)
412 static d_open_t genkbdopen;
413 static d_close_t genkbdclose;
414 static d_read_t genkbdread;
415 static d_write_t genkbdwrite;
416 static d_ioctl_t genkbdioctl;
417 static d_poll_t genkbdpoll;
419 #define CDEV_MAJOR 112
421 static struct cdevsw kbd_cdevsw = {
423 /* maj */ CDEV_MAJOR,
428 /* open */ genkbdopen,
429 /* close */ genkbdclose,
430 /* read */ genkbdread,
431 /* write */ genkbdwrite,
432 /* ioctl */ genkbdioctl,
433 /* poll */ genkbdpoll,
435 /* strategy */ nostrategy,
441 kbd_attach(keyboard_t *kbd)
445 if (kbd->kb_index >= keyboards)
447 if (keyboard[kbd->kb_index] != kbd)
450 dev = make_dev(&kbd_cdevsw, kbd->kb_index, UID_ROOT, GID_WHEEL, 0600,
451 "kbd%r", kbd->kb_index);
452 if (dev->si_drv1 == NULL)
453 dev->si_drv1 = malloc(sizeof(genkbd_softc_t), M_DEVBUF,
455 bzero(dev->si_drv1, sizeof(genkbd_softc_t));
457 printf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
462 kbd_detach(keyboard_t *kbd)
466 if (kbd->kb_index >= keyboards)
468 if (keyboard[kbd->kb_index] != kbd)
471 dev = makedev(kbd_cdevsw.d_maj, kbd->kb_index);
473 free(dev->si_drv1, M_DEVBUF);
479 * Generic keyboard cdev driver functions
480 * Keyboard subdrivers may call these functions to implement common
485 #define KB_BUFSIZE 64
487 static kbd_callback_func_t genkbd_event;
490 genkbdopen(dev_t dev, int mode, int flag, d_thread_t *td)
499 kbd = kbd_get_keyboard(KBD_INDEX(dev));
500 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
504 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
505 genkbd_event, (void *)sc);
510 /* assert(i == kbd->kb_index) */
511 /* assert(kbd == kbd_get_keyboard(i)) */
514 * NOTE: even when we have successfully claimed a keyboard,
515 * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
519 bzero(&sc->gkb_q, sizeof(sc->gkb_q));
521 clist_alloc_cblocks(&sc->gkb_q, KB_QSIZE, KB_QSIZE/2); /* XXX */
522 sc->gkb_rsel.si_flags = 0;
523 sc->gkb_rsel.si_pid = 0;
530 genkbdclose(dev_t dev, int mode, int flag, d_thread_t *td)
537 * NOTE: the device may have already become invalid.
538 * kbd == NULL || !KBD_IS_VALID(kbd)
542 kbd = kbd_get_keyboard(KBD_INDEX(dev));
543 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
544 /* XXX: we shall be forgiving and don't report error... */
546 kbd_release(kbd, (void *)sc);
548 clist_free_cblocks(&sc->gkb_q);
556 genkbdread(dev_t dev, struct uio *uio, int flag)
560 u_char buffer[KB_BUFSIZE];
568 kbd = kbd_get_keyboard(KBD_INDEX(dev));
569 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
573 while (sc->gkb_q.c_cc == 0) {
574 if (flag & IO_NDELAY) {
578 sc->gkb_flags |= KB_ASLEEP;
579 error = tsleep((caddr_t)sc, PCATCH, "kbdrea", 0);
580 kbd = kbd_get_keyboard(KBD_INDEX(dev));
581 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
583 return ENXIO; /* our keyboard has gone... */
586 sc->gkb_flags &= ~KB_ASLEEP;
593 /* copy as much input as possible */
595 while (uio->uio_resid > 0) {
596 len = imin(uio->uio_resid, sizeof(buffer));
597 len = q_to_b(&sc->gkb_q, buffer, len);
600 error = uiomove(buffer, len, uio);
609 genkbdwrite(dev_t dev, struct uio *uio, int flag)
613 kbd = kbd_get_keyboard(KBD_INDEX(dev));
614 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
620 genkbdioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, d_thread_t *td)
625 kbd = kbd_get_keyboard(KBD_INDEX(dev));
626 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
628 error = (*kbdsw[kbd->kb_index]->ioctl)(kbd, cmd, arg);
629 if (error == ENOIOCTL)
635 genkbdpoll(dev_t dev, int events, d_thread_t *td)
645 kbd = kbd_get_keyboard(KBD_INDEX(dev));
646 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
647 revents = POLLHUP; /* the keyboard has gone */
648 } else if (events & (POLLIN | POLLRDNORM)) {
649 if (sc->gkb_q.c_cc > 0)
650 revents = events & (POLLIN | POLLRDNORM);
652 selrecord(td, &sc->gkb_rsel);
659 genkbd_event(keyboard_t *kbd, int event, void *arg)
667 /* assert(KBD_IS_VALID(kbd)) */
668 sc = (genkbd_softc_t *)arg;
673 case KBDIO_UNLOADING:
674 /* the keyboard is going... */
675 kbd_release(kbd, (void *)sc);
676 if (sc->gkb_flags & KB_ASLEEP) {
677 sc->gkb_flags &= ~KB_ASLEEP;
680 selwakeup(&sc->gkb_rsel);
686 /* obtain the current key input mode */
687 if ((*kbdsw[kbd->kb_index]->ioctl)(kbd, KDGKBMODE, (caddr_t)&mode))
690 /* read all pending input */
691 while ((*kbdsw[kbd->kb_index]->check_char)(kbd)) {
692 c = (*kbdsw[kbd->kb_index]->read_char)(kbd, FALSE);
695 if (c == ERRKEY) /* XXX: ring bell? */
697 if (!KBD_IS_BUSY(kbd))
698 /* the device is not open, discard the input */
701 /* store the byte as is for K_RAW and K_CODE modes */
702 if (mode != K_XLATE) {
703 putc(KEYCHAR(c), &sc->gkb_q);
708 if (c & RELKEY) /* key release is ignored */
711 /* process special keys; most of them are just ignored... */
713 switch (KEYCHAR(c)) {
717 case BTAB: /* a backtab: ESC [ Z */
718 putc(0x1b, &sc->gkb_q);
719 putc('[', &sc->gkb_q);
720 putc('Z', &sc->gkb_q);
725 /* normal chars, normal chars with the META, function keys */
726 switch (KEYFLAGS(c)) {
727 case 0: /* a normal char */
728 putc(KEYCHAR(c), &sc->gkb_q);
730 case MKEY: /* the META flag: prepend ESC */
731 putc(0x1b, &sc->gkb_q);
732 putc(KEYCHAR(c), &sc->gkb_q);
734 case FKEY | SPCLKEY: /* a function key, return string */
735 cp = (*kbdsw[kbd->kb_index]->get_fkeystr)(kbd,
739 putc(*cp++, &sc->gkb_q);
745 /* wake up sleeping/polling processes */
746 if (sc->gkb_q.c_cc > 0) {
747 if (sc->gkb_flags & KB_ASLEEP) {
748 sc->gkb_flags &= ~KB_ASLEEP;
751 selwakeup(&sc->gkb_rsel);
757 #endif /* KBD_INSTALL_CDEV */
760 * Generic low-level keyboard functions
761 * The low-level functions in the keyboard subdriver may use these
766 genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
776 case KDGKBINFO: /* get keyboard information */
777 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
778 i = imin(strlen(kbd->kb_name) + 1,
779 sizeof(((keyboard_info_t *)arg)->kb_name));
780 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
781 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
782 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
783 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
784 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
787 case KDGKBTYPE: /* get keyboard type */
788 *(int *)arg = kbd->kb_type;
791 case KDGETREPEAT: /* get keyboard repeat rate */
792 ((int *)arg)[0] = kbd->kb_delay1;
793 ((int *)arg)[1] = kbd->kb_delay2;
796 case GIO_KEYMAP: /* get keyboard translation table */
797 bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap));
799 case PIO_KEYMAP: /* set keyboard translation table */
800 #ifndef KBD_DISABLE_KEYMAP_LOAD
801 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
802 bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
809 case GIO_KEYMAPENT: /* get keyboard translation table entry */
810 keyp = (keyarg_t *)arg;
811 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
812 /sizeof(kbd->kb_keymap->key[0])) {
816 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
819 case PIO_KEYMAPENT: /* set keyboard translation table entry */
820 #ifndef KBD_DISABLE_KEYMAP_LOAD
821 keyp = (keyarg_t *)arg;
822 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
823 /sizeof(kbd->kb_keymap->key[0])) {
827 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
835 case GIO_DEADKEYMAP: /* get accent key translation table */
836 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
838 case PIO_DEADKEYMAP: /* set accent key translation table */
839 #ifndef KBD_DISABLE_KEYMAP_LOAD
840 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
847 case GETFKEY: /* get functionkey string */
848 fkeyp = (fkeyarg_t *)arg;
849 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
853 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
854 kbd->kb_fkeytab[fkeyp->keynum].len);
855 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
857 case SETFKEY: /* set functionkey string */
858 #ifndef KBD_DISABLE_KEYMAP_LOAD
859 fkeyp = (fkeyarg_t *)arg;
860 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
864 kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK);
865 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
866 kbd->kb_fkeytab[fkeyp->keynum].len);
882 /* get a pointer to the string associated with the given function key */
884 *genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
889 if (fkey > kbd->kb_fkeytab_size)
891 *len = kbd->kb_fkeytab[fkey].len;
892 return kbd->kb_fkeytab[fkey].str;
895 /* diagnostic dump */
897 *get_kbd_type_name(int type)
904 { KB_101, "AT 101/102" },
905 { KB_OTHER, "generic" },
909 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
910 if (type == name_table[i].type)
911 return name_table[i].name;
917 genkbd_diag(keyboard_t *kbd, int level)
920 printf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
921 kbd->kb_index, kbd->kb_name, kbd->kb_unit,
922 get_kbd_type_name(kbd->kb_type), kbd->kb_type,
923 kbd->kb_config, kbd->kb_flags);
924 if (kbd->kb_io_base > 0)
925 printf(", port:0x%x-0x%x", kbd->kb_io_base,
926 kbd->kb_io_base + kbd->kb_io_size - 1);
931 #define set_lockkey_state(k, s, l) \
932 if (!((s) & l ## DOWN)) { \
936 i = (s) & LOCK_MASK; \
937 (*kbdsw[(k)->kb_index]->ioctl)((k), KDSETLED, (caddr_t)&i); \
941 save_accent_key(keyboard_t *kbd, u_int key, int *accents)
945 /* make an index into the accent map */
947 if ((i > kbd->kb_accentmap->n_accs)
948 || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
949 /* the index is out of range or pointing to an empty entry */
955 * If the same accent key has been hit twice, produce the accent char
959 key = kbd->kb_accentmap->acc[i - 1].accchar;
964 /* remember the index and wait for the next key */
970 make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
975 acc = &kbd->kb_accentmap->acc[*accents - 1];
979 * If the accent key is followed by the space key,
980 * produce the accent char itself.
985 /* scan the accent map */
986 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
987 if (acc->map[i][0] == 0) /* end of table */
989 if (acc->map[i][0] == ch)
990 return acc->map[i][1];
992 /* this char cannot be accented... */
997 genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1000 struct keyent_t *key;
1001 int state = *shiftstate;
1007 f = state & (AGRS | ALKED);
1008 if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1010 key = &kbd->kb_keymap->key[i];
1011 i = ((state & SHIFTS) ? 1 : 0)
1012 | ((state & CTLS) ? 2 : 0)
1013 | ((state & ALTS) ? 4 : 0);
1014 if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1015 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1018 if (up) { /* break: key released */
1019 action = kbd->kb_lastact[keycode];
1020 kbd->kb_lastact[keycode] = NOP;
1023 if (state & SHIFTAON) {
1024 set_lockkey_state(kbd, state, ALK);
1033 if (state & SHIFTAON) {
1034 set_lockkey_state(kbd, state, ALK);
1043 if (state & SHIFTAON) {
1044 set_lockkey_state(kbd, state, ALK);
1053 if (state & SHIFTAON) {
1054 set_lockkey_state(kbd, state, ALK);
1063 if (state & SHIFTAON) {
1064 set_lockkey_state(kbd, state, ALK);
1073 if (state & SHIFTAON) {
1074 set_lockkey_state(kbd, state, ALK);
1096 i = state & LOCK_MASK;
1097 (*kbdsw[kbd->kb_index]->ioctl)(kbd, KDSETLED,
1108 /* release events of regular keys are not reported */
1109 *shiftstate &= ~SHIFTAON;
1112 *shiftstate = state & ~SHIFTAON;
1113 return (SPCLKEY | RELKEY | action);
1114 } else { /* make: key pressed */
1115 action = key->map[i];
1117 if (key->spcl & (0x80 >> i)) {
1119 if (kbd->kb_lastact[keycode] == NOP)
1120 kbd->kb_lastact[keycode] = action;
1121 if (kbd->kb_lastact[keycode] != action)
1126 set_lockkey_state(kbd, state, NLK);
1130 set_lockkey_state(kbd, state, CLK);
1133 i = state & LOCK_MASK;
1134 (*kbdsw[kbd->kb_index]->ioctl)(kbd, KDSETLED,
1139 set_lockkey_state(kbd, state, SLK);
1142 set_lockkey_state(kbd, state, ALK);
1144 /* NON-LOCKING KEYS */
1145 case SPSC: case RBT: case SUSP: case STBY:
1146 case DBG: case NEXT: case PREV: case PNC:
1147 case HALT: case PDWN:
1203 *shiftstate = state;
1206 /* is this an accent (dead) key? */
1207 *shiftstate = state;
1208 if (action >= F_ACC && action <= L_ACC) {
1209 action = save_accent_key(kbd, action,
1217 return (action | MKEY);
1223 /* other special keys */
1228 if (action >= F_FN && action <= L_FN)
1230 /* XXX: return fkey string for the FKEY? */
1231 return (SPCLKEY | action);
1233 *shiftstate = state;
1234 return (SPCLKEY | action);
1237 kbd->kb_lastact[keycode] = NOP;
1238 *shiftstate = state;
1240 /* make an accented char */
1241 action = make_accent_char(kbd, action, accents);
1242 if (action == ERRKEY)