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.23 2007/05/08 02:31:39 dillon Exp $
30 * Generic keyboard driver.
32 * Interrupt note: keyboards use clist functions and since usb keyboard
33 * interrupts are not protected by spltty(), we must use a critical section
34 * to protect against corruption.
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
47 #include <sys/vnode.h>
49 #include <sys/thread.h>
50 #include <sys/thread2.h>
52 #include <machine/console.h>
56 #define KBD_INDEX(dev) minor(dev)
58 typedef struct genkbd_softc {
59 int gkb_flags; /* flag/status bits */
60 #define KB_ASLEEP (1 << 0)
61 struct clist gkb_q; /* input queue */
62 struct selinfo gkb_rsel;
65 static SLIST_HEAD(, keyboard_driver) keyboard_drivers =
66 SLIST_HEAD_INITIALIZER(keyboard_drivers);
68 SET_DECLARE(kbddriver_set, const keyboard_driver_t);
73 * We need at least one entry each in order to initialize a keyboard
74 * for the kernel console. The arrays will be increased dynamically
78 static int keyboards = 1;
79 static keyboard_t *kbd_ini;
80 static keyboard_t **keyboard = &kbd_ini;
81 static keyboard_switch_t *kbdsw_ini;
82 keyboard_switch_t **kbdsw = &kbdsw_ini;
87 kbd_realloc_array(void)
90 keyboard_switch_t **new_kbdsw;
93 newsize = ((keyboards + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
94 new_kbd = kmalloc(sizeof(*new_kbd) * newsize, M_DEVBUF,
96 new_kbdsw = kmalloc(sizeof(*new_kbdsw) * newsize, M_DEVBUF,
98 bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
99 bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards);
102 kfree(keyboard, M_DEVBUF);
103 kfree(kbdsw, M_DEVBUF);
111 kprintf("kbd: new array size %d\n", keyboards);
117 * Low-level keyboard driver functions.
119 * Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard
120 * driver, call these functions to initialize the keyboard_t structure
121 * and register it to the virtual keyboard driver `kbd'.
123 * The reinit call is made when a driver has partially detached a keyboard
124 * but does not unregistered it, then wishes to reinitialize it later on.
125 * This is how the USB keyboard driver handles the 'default' keyboard,
126 * because unregistering the keyboard associated with the console will
127 * destroy its console association forever.
130 kbd_reinit_struct(keyboard_t *kbd, int config, int pref)
132 kbd->kb_flags |= KB_NO_DEVICE; /* device has not been found */
133 kbd->kb_config = config & ~KB_CONF_PROBE_ONLY;
134 kbd->kb_led = 0; /* unknown */
136 kbd->kb_keymap = NULL;
137 kbd->kb_accentmap = NULL;
138 kbd->kb_fkeytab = NULL;
139 kbd->kb_fkeytab_size = 0;
140 kbd->kb_delay1 = KB_DELAY1; /* these values are advisory only */
141 kbd->kb_delay2 = KB_DELAY2;
144 bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact));
147 /* initialize the keyboard_t structure */
149 kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config,
150 int pref, int port, int port_size)
156 kbd->kb_io_base = port;
157 kbd->kb_io_size = port_size;
158 kbd_reinit_struct(kbd, config, pref);
162 kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap,
163 fkeytab_t *fkeymap, int fkeymap_size)
165 kbd->kb_keymap = keymap;
166 kbd->kb_accentmap = accmap;
167 kbd->kb_fkeytab = fkeymap;
168 kbd->kb_fkeytab_size = fkeymap_size;
171 /* declare a new keyboard driver */
173 kbd_add_driver(keyboard_driver_t *driver)
175 if (SLIST_NEXT(driver, link))
177 SLIST_INSERT_HEAD(&keyboard_drivers, driver, link);
182 kbd_delete_driver(keyboard_driver_t *driver)
184 SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link);
185 SLIST_NEXT(driver, link) = NULL;
189 /* register a keyboard and associate it with a function table */
191 kbd_register(keyboard_t *kbd)
193 const keyboard_driver_t **list;
194 const keyboard_driver_t *p;
197 for (index = 0; index < keyboards; ++index) {
198 if (keyboard[index] == NULL)
201 if (index >= keyboards) {
202 if (kbd_realloc_array())
206 kbd->kb_index = index;
209 kbd->kb_active = 0; /* disabled until someone calls kbd_enable() */
210 kbd->kb_token = NULL;
211 kbd->kb_callback.kc_func = NULL;
212 kbd->kb_callback.kc_arg = NULL;
213 callout_init(&kbd->kb_atkbd_timeout_ch);
215 SLIST_FOREACH(p, &keyboard_drivers, link) {
216 if (strcmp(p->name, kbd->kb_name) == 0) {
217 keyboard[index] = kbd;
218 kbdsw[index] = p->kbdsw;
222 SET_FOREACH(list, kbddriver_set) {
224 if (strcmp(p->name, kbd->kb_name) == 0) {
225 keyboard[index] = kbd;
226 kbdsw[index] = p->kbdsw;
235 kbd_unregister(keyboard_t *kbd)
239 if ((kbd->kb_index < 0) || (kbd->kb_index >= keyboards))
241 if (keyboard[kbd->kb_index] != kbd)
245 callout_stop(&kbd->kb_atkbd_timeout_ch);
246 if (KBD_IS_BUSY(kbd)) {
247 error = (*kbd->kb_callback.kc_func)(kbd, KBDIO_UNLOADING,
248 kbd->kb_callback.kc_arg);
253 if (KBD_IS_BUSY(kbd)) {
259 keyboard[kbd->kb_index] = NULL;
260 kbdsw[kbd->kb_index] = NULL;
266 /* find a funciton table by the driver name */
268 kbd_get_switch(char *driver)
270 const keyboard_driver_t **list;
271 const keyboard_driver_t *p;
273 SLIST_FOREACH(p, &keyboard_drivers, link) {
274 if (strcmp(p->name, driver) == 0)
277 SET_FOREACH(list, kbddriver_set) {
279 if (strcmp(p->name, driver) == 0)
287 * Keyboard client functions
288 * Keyboard clients, such as the console driver `syscons' and the keyboard
289 * cdev driver, use these functions to claim and release a keyboard for
293 /* find the keyboard specified by a driver name and a unit number */
295 kbd_find_keyboard(char *driver, int unit)
304 for (i = 0; i < keyboards; ++i) {
305 if (keyboard[i] == NULL)
307 if (!KBD_IS_VALID(keyboard[i]))
309 if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver))
311 if ((unit != -1) && (keyboard[i]->kb_unit != unit))
313 if (pref <= keyboard[i]->kb_pref) {
314 pref = keyboard[i]->kb_pref;
321 /* allocate a keyboard */
323 kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func,
332 index = kbd_find_keyboard(driver, unit);
334 if (KBD_IS_BUSY(keyboard[index])) {
338 keyboard[index]->kb_token = id;
339 KBD_BUSY(keyboard[index]);
340 keyboard[index]->kb_callback.kc_func = func;
341 keyboard[index]->kb_callback.kc_arg = arg;
342 (*kbdsw[index]->clear_state)(keyboard[index]);
349 kbd_release(keyboard_t *kbd, void *id)
354 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
356 } else if (kbd->kb_token != id) {
359 kbd->kb_token = NULL;
361 kbd->kb_callback.kc_func = NULL;
362 kbd->kb_callback.kc_arg = NULL;
363 (*kbdsw[kbd->kb_index]->clear_state)(kbd);
371 kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func,
377 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
379 } else if (kbd->kb_token != id) {
381 } else if (func == NULL) {
384 kbd->kb_callback.kc_func = func;
385 kbd->kb_callback.kc_arg = arg;
392 /* get a keyboard structure */
394 kbd_get_keyboard(int index)
396 if ((index < 0) || (index >= keyboards))
398 if (keyboard[index] == NULL)
400 if (!KBD_IS_VALID(keyboard[index]))
402 return keyboard[index];
406 * The back door for the console driver; configure keyboards
407 * This function is for the kernel console to initialize keyboards
408 * at very early stage.
412 kbd_configure(int flags)
414 const keyboard_driver_t **list;
415 const keyboard_driver_t *p;
417 SLIST_FOREACH(p, &keyboard_drivers, link) {
418 if (p->configure != NULL)
419 (*p->configure)(flags);
421 SET_FOREACH(list, kbddriver_set) {
423 if (p->configure != NULL)
424 (*p->configure)(flags);
430 #ifdef KBD_INSTALL_CDEV
433 * Virtual keyboard cdev driver functions
434 * The virtual keyboard driver dispatches driver functions to
435 * appropriate subdrivers.
438 #define KBD_UNIT(dev) minor(dev)
440 static d_open_t genkbdopen;
441 static d_close_t genkbdclose;
442 static d_read_t genkbdread;
443 static d_write_t genkbdwrite;
444 static d_ioctl_t genkbdioctl;
445 static d_poll_t genkbdpoll;
447 #define CDEV_MAJOR 112
449 static struct dev_ops kbd_ops = {
450 { "kbd", CDEV_MAJOR, 0 },
451 .d_open = genkbdopen,
452 .d_close = genkbdclose,
453 .d_read = genkbdread,
454 .d_write = genkbdwrite,
455 .d_ioctl = genkbdioctl,
456 .d_poll = genkbdpoll,
460 kbd_attach(keyboard_t *kbd)
464 if (kbd->kb_index >= keyboards)
466 if (keyboard[kbd->kb_index] != kbd)
469 dev = make_dev(&kbd_ops, kbd->kb_index, UID_ROOT, GID_WHEEL, 0600,
470 "kbd%r", kbd->kb_index);
471 if (dev->si_drv1 == NULL)
472 dev->si_drv1 = kmalloc(sizeof(genkbd_softc_t), M_DEVBUF,
474 bzero(dev->si_drv1, sizeof(genkbd_softc_t));
476 kprintf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
481 kbd_detach(keyboard_t *kbd)
485 if (kbd->kb_index >= keyboards)
487 if (keyboard[kbd->kb_index] != kbd)
491 * Deal with refs properly. The KBD driver really ought to have
492 * recorded the cdev_t separately.
494 if ((dev = make_adhoc_dev(&kbd_ops, kbd->kb_index)) != NULL) {
496 kfree(dev->si_drv1, M_DEVBUF);
500 dev_ops_remove_minor(&kbd_ops, kbd->kb_index);
505 * Generic keyboard cdev driver functions
506 * Keyboard subdrivers may call these functions to implement common
511 #define KB_BUFSIZE 64
513 static kbd_callback_func_t genkbd_event;
516 genkbdopen(struct dev_open_args *ap)
518 cdev_t dev = ap->a_head.a_dev;
525 kbd = kbd_get_keyboard(KBD_INDEX(dev));
526 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
530 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
531 genkbd_event, (void *)sc);
536 /* assert(i == kbd->kb_index) */
537 /* assert(kbd == kbd_get_keyboard(i)) */
540 * NOTE: even when we have successfully claimed a keyboard,
541 * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
545 bzero(&sc->gkb_q, sizeof(sc->gkb_q));
547 clist_alloc_cblocks(&sc->gkb_q, KB_QSIZE, KB_QSIZE/2); /* XXX */
548 sc->gkb_rsel.si_flags = 0;
549 sc->gkb_rsel.si_pid = 0;
556 genkbdclose(struct dev_close_args *ap)
558 cdev_t dev = ap->a_head.a_dev;
563 * NOTE: the device may have already become invalid.
564 * kbd == NULL || !KBD_IS_VALID(kbd)
568 kbd = kbd_get_keyboard(KBD_INDEX(dev));
569 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
570 /* XXX: we shall be forgiving and don't report error... */
572 kbd_release(kbd, (void *)sc);
574 clist_free_cblocks(&sc->gkb_q);
582 genkbdread(struct dev_read_args *ap)
584 cdev_t dev = ap->a_head.a_dev;
585 struct uio *uio = ap->a_uio;
588 u_char buffer[KB_BUFSIZE];
595 kbd = kbd_get_keyboard(KBD_INDEX(dev));
596 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
600 while (sc->gkb_q.c_cc == 0) {
601 if (ap->a_ioflag & IO_NDELAY) {
605 sc->gkb_flags |= KB_ASLEEP;
606 error = tsleep((caddr_t)sc, PCATCH, "kbdrea", 0);
607 kbd = kbd_get_keyboard(KBD_INDEX(dev));
608 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
610 return ENXIO; /* our keyboard has gone... */
613 sc->gkb_flags &= ~KB_ASLEEP;
620 /* copy as much input as possible */
622 while (uio->uio_resid > 0) {
623 len = imin(uio->uio_resid, sizeof(buffer));
624 len = q_to_b(&sc->gkb_q, buffer, len);
627 error = uiomove(buffer, len, uio);
636 genkbdwrite(struct dev_write_args *ap)
638 cdev_t dev = ap->a_head.a_dev;
641 kbd = kbd_get_keyboard(KBD_INDEX(dev));
642 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
648 genkbdioctl(struct dev_ioctl_args *ap)
650 cdev_t dev = ap->a_head.a_dev;
654 kbd = kbd_get_keyboard(KBD_INDEX(dev));
655 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
657 error = (*kbdsw[kbd->kb_index]->ioctl)(kbd, ap->a_cmd, ap->a_data);
658 if (error == ENOIOCTL)
664 genkbdpoll(struct dev_poll_args *ap)
666 cdev_t dev = ap->a_head.a_dev;
674 kbd = kbd_get_keyboard(KBD_INDEX(dev));
675 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
676 revents = POLLHUP; /* the keyboard has gone */
677 } else if (ap->a_events & (POLLIN | POLLRDNORM)) {
678 if (sc->gkb_q.c_cc > 0)
679 revents = ap->a_events & (POLLIN | POLLRDNORM);
681 selrecord(curthread, &sc->gkb_rsel);
684 ap->a_events = revents;
689 genkbd_event(keyboard_t *kbd, int event, void *arg)
697 /* assert(KBD_IS_VALID(kbd)) */
698 sc = (genkbd_softc_t *)arg;
703 case KBDIO_UNLOADING:
704 /* the keyboard is going... */
705 kbd_release(kbd, (void *)sc);
706 if (sc->gkb_flags & KB_ASLEEP) {
707 sc->gkb_flags &= ~KB_ASLEEP;
710 selwakeup(&sc->gkb_rsel);
716 /* obtain the current key input mode */
717 if ((*kbdsw[kbd->kb_index]->ioctl)(kbd, KDGKBMODE, (caddr_t)&mode))
720 /* read all pending input */
721 while ((*kbdsw[kbd->kb_index]->check_char)(kbd)) {
722 c = (*kbdsw[kbd->kb_index]->read_char)(kbd, FALSE);
725 if (c == ERRKEY) /* XXX: ring bell? */
727 if (!KBD_IS_BUSY(kbd))
728 /* the device is not open, discard the input */
731 /* store the byte as is for K_RAW and K_CODE modes */
732 if (mode != K_XLATE) {
733 clist_putc(KEYCHAR(c), &sc->gkb_q);
738 if (c & RELKEY) /* key release is ignored */
741 /* process special keys; most of them are just ignored... */
743 switch (KEYCHAR(c)) {
747 case BTAB: /* a backtab: ESC [ Z */
748 clist_putc(0x1b, &sc->gkb_q);
749 clist_putc('[', &sc->gkb_q);
750 clist_putc('Z', &sc->gkb_q);
755 /* normal chars, normal chars with the META, function keys */
756 switch (KEYFLAGS(c)) {
757 case 0: /* a normal char */
758 clist_putc(KEYCHAR(c), &sc->gkb_q);
760 case MKEY: /* the META flag: prepend ESC */
761 clist_putc(0x1b, &sc->gkb_q);
762 clist_putc(KEYCHAR(c), &sc->gkb_q);
764 case FKEY | SPCLKEY: /* a function key, return string */
765 cp = (*kbdsw[kbd->kb_index]->get_fkeystr)(kbd,
769 clist_putc(*cp++, &sc->gkb_q);
775 /* wake up sleeping/polling processes */
776 if (sc->gkb_q.c_cc > 0) {
777 if (sc->gkb_flags & KB_ASLEEP) {
778 sc->gkb_flags &= ~KB_ASLEEP;
781 selwakeup(&sc->gkb_rsel);
787 #endif /* KBD_INSTALL_CDEV */
790 * Generic low-level keyboard functions
791 * The low-level functions in the keyboard subdriver may use these
796 genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
805 case KDGKBINFO: /* get keyboard information */
806 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
807 i = imin(strlen(kbd->kb_name) + 1,
808 sizeof(((keyboard_info_t *)arg)->kb_name));
809 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
810 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
811 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
812 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
813 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
816 case KDGKBTYPE: /* get keyboard type */
817 *(int *)arg = kbd->kb_type;
820 case KDGETREPEAT: /* get keyboard repeat rate */
821 ((int *)arg)[0] = kbd->kb_delay1;
822 ((int *)arg)[1] = kbd->kb_delay2;
825 case GIO_KEYMAP: /* get keyboard translation table */
826 bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap));
828 case PIO_KEYMAP: /* set keyboard translation table */
829 #ifndef KBD_DISABLE_KEYMAP_LOAD
830 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
831 bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
838 case GIO_KEYMAPENT: /* get keyboard translation table entry */
839 keyp = (keyarg_t *)arg;
840 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
841 /sizeof(kbd->kb_keymap->key[0])) {
845 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
848 case PIO_KEYMAPENT: /* set keyboard translation table entry */
849 #ifndef KBD_DISABLE_KEYMAP_LOAD
850 keyp = (keyarg_t *)arg;
851 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
852 /sizeof(kbd->kb_keymap->key[0])) {
856 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
864 case GIO_DEADKEYMAP: /* get accent key translation table */
865 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
867 case PIO_DEADKEYMAP: /* set accent key translation table */
868 #ifndef KBD_DISABLE_KEYMAP_LOAD
869 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
876 case GETFKEY: /* get functionkey string */
877 fkeyp = (fkeyarg_t *)arg;
878 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
882 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
883 kbd->kb_fkeytab[fkeyp->keynum].len);
884 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
886 case SETFKEY: /* set functionkey string */
887 #ifndef KBD_DISABLE_KEYMAP_LOAD
888 fkeyp = (fkeyarg_t *)arg;
889 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
893 kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK);
894 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
895 kbd->kb_fkeytab[fkeyp->keynum].len);
911 /* get a pointer to the string associated with the given function key */
913 genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
918 if (fkey > kbd->kb_fkeytab_size)
920 *len = kbd->kb_fkeytab[fkey].len;
921 return kbd->kb_fkeytab[fkey].str;
924 /* diagnostic dump */
926 get_kbd_type_name(int type)
933 { KB_101, "AT 101/102" },
934 { KB_OTHER, "generic" },
938 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
939 if (type == name_table[i].type)
940 return name_table[i].name;
946 genkbd_diag(keyboard_t *kbd, int level)
949 kprintf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
950 kbd->kb_index, kbd->kb_name, kbd->kb_unit,
951 get_kbd_type_name(kbd->kb_type), kbd->kb_type,
952 kbd->kb_config, kbd->kb_flags);
953 if (kbd->kb_io_base > 0)
954 kprintf(", port:0x%x-0x%x", kbd->kb_io_base,
955 kbd->kb_io_base + kbd->kb_io_size - 1);
960 #define set_lockkey_state(k, s, l) \
961 if (!((s) & l ## DOWN)) { \
965 i = (s) & LOCK_MASK; \
966 (*kbdsw[(k)->kb_index]->ioctl)((k), KDSETLED, (caddr_t)&i); \
970 save_accent_key(keyboard_t *kbd, u_int key, int *accents)
974 /* make an index into the accent map */
976 if ((i > kbd->kb_accentmap->n_accs)
977 || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
978 /* the index is out of range or pointing to an empty entry */
984 * If the same accent key has been hit twice, produce the accent char
988 key = kbd->kb_accentmap->acc[i - 1].accchar;
993 /* remember the index and wait for the next key */
999 make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
1004 acc = &kbd->kb_accentmap->acc[*accents - 1];
1008 * If the accent key is followed by the space key,
1009 * produce the accent char itself.
1012 return acc->accchar;
1014 /* scan the accent map */
1015 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1016 if (acc->map[i][0] == 0) /* end of table */
1018 if (acc->map[i][0] == ch)
1019 return acc->map[i][1];
1021 /* this char cannot be accented... */
1026 genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1029 struct keyent_t *key;
1030 int state = *shiftstate;
1036 f = state & (AGRS | ALKED);
1037 if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1039 key = &kbd->kb_keymap->key[i];
1040 i = ((state & SHIFTS) ? 1 : 0)
1041 | ((state & CTLS) ? 2 : 0)
1042 | ((state & ALTS) ? 4 : 0);
1043 if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1044 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1047 if (up) { /* break: key released */
1048 action = kbd->kb_lastact[keycode];
1049 kbd->kb_lastact[keycode] = NOP;
1052 if (state & SHIFTAON) {
1053 set_lockkey_state(kbd, state, ALK);
1062 if (state & SHIFTAON) {
1063 set_lockkey_state(kbd, state, ALK);
1072 if (state & SHIFTAON) {
1073 set_lockkey_state(kbd, state, ALK);
1082 if (state & SHIFTAON) {
1083 set_lockkey_state(kbd, state, ALK);
1092 if (state & SHIFTAON) {
1093 set_lockkey_state(kbd, state, ALK);
1102 if (state & SHIFTAON) {
1103 set_lockkey_state(kbd, state, ALK);
1130 /* release events of regular keys are not reported */
1131 *shiftstate &= ~SHIFTAON;
1134 *shiftstate = state & ~SHIFTAON;
1135 return (SPCLKEY | RELKEY | action);
1136 } else { /* make: key pressed */
1137 action = key->map[i];
1139 if (key->spcl & (0x80 >> i)) {
1141 if (kbd->kb_lastact[keycode] == NOP)
1142 kbd->kb_lastact[keycode] = action;
1143 if (kbd->kb_lastact[keycode] != action)
1148 set_lockkey_state(kbd, state, NLK);
1151 set_lockkey_state(kbd, state, CLK);
1154 set_lockkey_state(kbd, state, SLK);
1157 set_lockkey_state(kbd, state, ALK);
1159 /* NON-LOCKING KEYS */
1160 case SPSC: case RBT: case SUSP: case STBY:
1161 case DBG: case NEXT: case PREV: case PNC:
1162 case HALT: case PDWN:
1218 *shiftstate = state;
1221 /* is this an accent (dead) key? */
1222 *shiftstate = state;
1223 if (action >= F_ACC && action <= L_ACC) {
1224 action = save_accent_key(kbd, action,
1232 return (action | MKEY);
1238 /* other special keys */
1243 if (action >= F_FN && action <= L_FN)
1245 /* XXX: return fkey string for the FKEY? */
1246 return (SPCLKEY | action);
1248 *shiftstate = state;
1249 return (SPCLKEY | action);
1252 kbd->kb_lastact[keycode] = NOP;
1253 *shiftstate = state;
1255 /* make an accented char */
1256 action = make_accent_char(kbd, action, accents);
1257 if (action == ERRKEY)