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.17 2006/07/28 02:17:36 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 = malloc(sizeof(*new_kbd) * newsize, M_DEVBUF,
96 new_kbdsw = malloc(sizeof(*new_kbdsw) * newsize, M_DEVBUF,
98 bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
99 bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards);
102 free(keyboard, M_DEVBUF);
103 free(kbdsw, M_DEVBUF);
111 printf("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_ops_add(&kbd_ops, -1, kbd->kb_index);
470 dev = make_dev(&kbd_ops, kbd->kb_index, UID_ROOT, GID_WHEEL, 0600,
471 "kbd%r", kbd->kb_index);
472 if (dev->si_drv1 == NULL)
473 dev->si_drv1 = malloc(sizeof(genkbd_softc_t), M_DEVBUF,
475 bzero(dev->si_drv1, sizeof(genkbd_softc_t));
477 printf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
482 kbd_detach(keyboard_t *kbd)
486 if (kbd->kb_index >= keyboards)
488 if (keyboard[kbd->kb_index] != kbd)
492 * Deal with refs properly. The KBD driver really ought to have
493 * recorded the dev_t separately.
495 if ((dev = make_adhoc_dev(&kbd_ops, kbd->kb_index)) != NODEV) {
497 free(dev->si_drv1, M_DEVBUF);
501 dev_ops_remove(&kbd_ops, -1, kbd->kb_index);
506 * Generic keyboard cdev driver functions
507 * Keyboard subdrivers may call these functions to implement common
512 #define KB_BUFSIZE 64
514 static kbd_callback_func_t genkbd_event;
517 genkbdopen(struct dev_open_args *ap)
519 dev_t dev = ap->a_head.a_dev;
526 kbd = kbd_get_keyboard(KBD_INDEX(dev));
527 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
531 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
532 genkbd_event, (void *)sc);
537 /* assert(i == kbd->kb_index) */
538 /* assert(kbd == kbd_get_keyboard(i)) */
541 * NOTE: even when we have successfully claimed a keyboard,
542 * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
546 bzero(&sc->gkb_q, sizeof(sc->gkb_q));
548 clist_alloc_cblocks(&sc->gkb_q, KB_QSIZE, KB_QSIZE/2); /* XXX */
549 sc->gkb_rsel.si_flags = 0;
550 sc->gkb_rsel.si_pid = 0;
557 genkbdclose(struct dev_close_args *ap)
559 dev_t dev = ap->a_head.a_dev;
564 * NOTE: the device may have already become invalid.
565 * kbd == NULL || !KBD_IS_VALID(kbd)
569 kbd = kbd_get_keyboard(KBD_INDEX(dev));
570 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
571 /* XXX: we shall be forgiving and don't report error... */
573 kbd_release(kbd, (void *)sc);
575 clist_free_cblocks(&sc->gkb_q);
583 genkbdread(struct dev_read_args *ap)
585 dev_t dev = ap->a_head.a_dev;
586 struct uio *uio = ap->a_uio;
589 u_char buffer[KB_BUFSIZE];
596 kbd = kbd_get_keyboard(KBD_INDEX(dev));
597 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
601 while (sc->gkb_q.c_cc == 0) {
602 if (ap->a_ioflag & IO_NDELAY) {
606 sc->gkb_flags |= KB_ASLEEP;
607 error = tsleep((caddr_t)sc, PCATCH, "kbdrea", 0);
608 kbd = kbd_get_keyboard(KBD_INDEX(dev));
609 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
611 return ENXIO; /* our keyboard has gone... */
614 sc->gkb_flags &= ~KB_ASLEEP;
621 /* copy as much input as possible */
623 while (uio->uio_resid > 0) {
624 len = imin(uio->uio_resid, sizeof(buffer));
625 len = q_to_b(&sc->gkb_q, buffer, len);
628 error = uiomove(buffer, len, uio);
637 genkbdwrite(struct dev_write_args *ap)
639 dev_t dev = ap->a_head.a_dev;
642 kbd = kbd_get_keyboard(KBD_INDEX(dev));
643 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
649 genkbdioctl(struct dev_ioctl_args *ap)
651 dev_t dev = ap->a_head.a_dev;
655 kbd = kbd_get_keyboard(KBD_INDEX(dev));
656 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
658 error = (*kbdsw[kbd->kb_index]->ioctl)(kbd, ap->a_cmd, ap->a_data);
659 if (error == ENOIOCTL)
665 genkbdpoll(struct dev_poll_args *ap)
667 dev_t dev = ap->a_head.a_dev;
675 kbd = kbd_get_keyboard(KBD_INDEX(dev));
676 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
677 revents = POLLHUP; /* the keyboard has gone */
678 } else if (ap->a_events & (POLLIN | POLLRDNORM)) {
679 if (sc->gkb_q.c_cc > 0)
680 revents = ap->a_events & (POLLIN | POLLRDNORM);
682 selrecord(curthread, &sc->gkb_rsel);
685 ap->a_events = revents;
690 genkbd_event(keyboard_t *kbd, int event, void *arg)
698 /* assert(KBD_IS_VALID(kbd)) */
699 sc = (genkbd_softc_t *)arg;
704 case KBDIO_UNLOADING:
705 /* the keyboard is going... */
706 kbd_release(kbd, (void *)sc);
707 if (sc->gkb_flags & KB_ASLEEP) {
708 sc->gkb_flags &= ~KB_ASLEEP;
711 selwakeup(&sc->gkb_rsel);
717 /* obtain the current key input mode */
718 if ((*kbdsw[kbd->kb_index]->ioctl)(kbd, KDGKBMODE, (caddr_t)&mode))
721 /* read all pending input */
722 while ((*kbdsw[kbd->kb_index]->check_char)(kbd)) {
723 c = (*kbdsw[kbd->kb_index]->read_char)(kbd, FALSE);
726 if (c == ERRKEY) /* XXX: ring bell? */
728 if (!KBD_IS_BUSY(kbd))
729 /* the device is not open, discard the input */
732 /* store the byte as is for K_RAW and K_CODE modes */
733 if (mode != K_XLATE) {
734 putc(KEYCHAR(c), &sc->gkb_q);
739 if (c & RELKEY) /* key release is ignored */
742 /* process special keys; most of them are just ignored... */
744 switch (KEYCHAR(c)) {
748 case BTAB: /* a backtab: ESC [ Z */
749 putc(0x1b, &sc->gkb_q);
750 putc('[', &sc->gkb_q);
751 putc('Z', &sc->gkb_q);
756 /* normal chars, normal chars with the META, function keys */
757 switch (KEYFLAGS(c)) {
758 case 0: /* a normal char */
759 putc(KEYCHAR(c), &sc->gkb_q);
761 case MKEY: /* the META flag: prepend ESC */
762 putc(0x1b, &sc->gkb_q);
763 putc(KEYCHAR(c), &sc->gkb_q);
765 case FKEY | SPCLKEY: /* a function key, return string */
766 cp = (*kbdsw[kbd->kb_index]->get_fkeystr)(kbd,
770 putc(*cp++, &sc->gkb_q);
776 /* wake up sleeping/polling processes */
777 if (sc->gkb_q.c_cc > 0) {
778 if (sc->gkb_flags & KB_ASLEEP) {
779 sc->gkb_flags &= ~KB_ASLEEP;
782 selwakeup(&sc->gkb_rsel);
788 #endif /* KBD_INSTALL_CDEV */
791 * Generic low-level keyboard functions
792 * The low-level functions in the keyboard subdriver may use these
797 genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
806 case KDGKBINFO: /* get keyboard information */
807 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
808 i = imin(strlen(kbd->kb_name) + 1,
809 sizeof(((keyboard_info_t *)arg)->kb_name));
810 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
811 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
812 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
813 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
814 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
817 case KDGKBTYPE: /* get keyboard type */
818 *(int *)arg = kbd->kb_type;
821 case KDGETREPEAT: /* get keyboard repeat rate */
822 ((int *)arg)[0] = kbd->kb_delay1;
823 ((int *)arg)[1] = kbd->kb_delay2;
826 case GIO_KEYMAP: /* get keyboard translation table */
827 bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap));
829 case PIO_KEYMAP: /* set keyboard translation table */
830 #ifndef KBD_DISABLE_KEYMAP_LOAD
831 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
832 bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
839 case GIO_KEYMAPENT: /* get keyboard translation table entry */
840 keyp = (keyarg_t *)arg;
841 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
842 /sizeof(kbd->kb_keymap->key[0])) {
846 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
849 case PIO_KEYMAPENT: /* set keyboard translation table entry */
850 #ifndef KBD_DISABLE_KEYMAP_LOAD
851 keyp = (keyarg_t *)arg;
852 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
853 /sizeof(kbd->kb_keymap->key[0])) {
857 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
865 case GIO_DEADKEYMAP: /* get accent key translation table */
866 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
868 case PIO_DEADKEYMAP: /* set accent key translation table */
869 #ifndef KBD_DISABLE_KEYMAP_LOAD
870 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
877 case GETFKEY: /* get functionkey string */
878 fkeyp = (fkeyarg_t *)arg;
879 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
883 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
884 kbd->kb_fkeytab[fkeyp->keynum].len);
885 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
887 case SETFKEY: /* set functionkey string */
888 #ifndef KBD_DISABLE_KEYMAP_LOAD
889 fkeyp = (fkeyarg_t *)arg;
890 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
894 kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK);
895 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
896 kbd->kb_fkeytab[fkeyp->keynum].len);
912 /* get a pointer to the string associated with the given function key */
914 genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
919 if (fkey > kbd->kb_fkeytab_size)
921 *len = kbd->kb_fkeytab[fkey].len;
922 return kbd->kb_fkeytab[fkey].str;
925 /* diagnostic dump */
927 get_kbd_type_name(int type)
934 { KB_101, "AT 101/102" },
935 { KB_OTHER, "generic" },
939 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
940 if (type == name_table[i].type)
941 return name_table[i].name;
947 genkbd_diag(keyboard_t *kbd, int level)
950 printf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
951 kbd->kb_index, kbd->kb_name, kbd->kb_unit,
952 get_kbd_type_name(kbd->kb_type), kbd->kb_type,
953 kbd->kb_config, kbd->kb_flags);
954 if (kbd->kb_io_base > 0)
955 printf(", port:0x%x-0x%x", kbd->kb_io_base,
956 kbd->kb_io_base + kbd->kb_io_size - 1);
961 #define set_lockkey_state(k, s, l) \
962 if (!((s) & l ## DOWN)) { \
966 i = (s) & LOCK_MASK; \
967 (*kbdsw[(k)->kb_index]->ioctl)((k), KDSETLED, (caddr_t)&i); \
971 save_accent_key(keyboard_t *kbd, u_int key, int *accents)
975 /* make an index into the accent map */
977 if ((i > kbd->kb_accentmap->n_accs)
978 || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
979 /* the index is out of range or pointing to an empty entry */
985 * If the same accent key has been hit twice, produce the accent char
989 key = kbd->kb_accentmap->acc[i - 1].accchar;
994 /* remember the index and wait for the next key */
1000 make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
1005 acc = &kbd->kb_accentmap->acc[*accents - 1];
1009 * If the accent key is followed by the space key,
1010 * produce the accent char itself.
1013 return acc->accchar;
1015 /* scan the accent map */
1016 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1017 if (acc->map[i][0] == 0) /* end of table */
1019 if (acc->map[i][0] == ch)
1020 return acc->map[i][1];
1022 /* this char cannot be accented... */
1027 genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1030 struct keyent_t *key;
1031 int state = *shiftstate;
1037 f = state & (AGRS | ALKED);
1038 if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1040 key = &kbd->kb_keymap->key[i];
1041 i = ((state & SHIFTS) ? 1 : 0)
1042 | ((state & CTLS) ? 2 : 0)
1043 | ((state & ALTS) ? 4 : 0);
1044 if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1045 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1048 if (up) { /* break: key released */
1049 action = kbd->kb_lastact[keycode];
1050 kbd->kb_lastact[keycode] = NOP;
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);
1083 if (state & SHIFTAON) {
1084 set_lockkey_state(kbd, state, ALK);
1093 if (state & SHIFTAON) {
1094 set_lockkey_state(kbd, state, ALK);
1103 if (state & SHIFTAON) {
1104 set_lockkey_state(kbd, state, ALK);
1131 /* release events of regular keys are not reported */
1132 *shiftstate &= ~SHIFTAON;
1135 *shiftstate = state & ~SHIFTAON;
1136 return (SPCLKEY | RELKEY | action);
1137 } else { /* make: key pressed */
1138 action = key->map[i];
1140 if (key->spcl & (0x80 >> i)) {
1142 if (kbd->kb_lastact[keycode] == NOP)
1143 kbd->kb_lastact[keycode] = action;
1144 if (kbd->kb_lastact[keycode] != action)
1149 set_lockkey_state(kbd, state, NLK);
1152 set_lockkey_state(kbd, state, CLK);
1155 set_lockkey_state(kbd, state, SLK);
1158 set_lockkey_state(kbd, state, ALK);
1160 /* NON-LOCKING KEYS */
1161 case SPSC: case RBT: case SUSP: case STBY:
1162 case DBG: case NEXT: case PREV: case PNC:
1163 case HALT: case PDWN:
1219 *shiftstate = state;
1222 /* is this an accent (dead) key? */
1223 *shiftstate = state;
1224 if (action >= F_ACC && action <= L_ACC) {
1225 action = save_accent_key(kbd, action,
1233 return (action | MKEY);
1239 /* other special keys */
1244 if (action >= F_FN && action <= L_FN)
1246 /* XXX: return fkey string for the FKEY? */
1247 return (SPCLKEY | action);
1249 *shiftstate = state;
1250 return (SPCLKEY | action);
1253 kbd->kb_lastact[keycode] = NOP;
1254 *shiftstate = state;
1256 /* make an accented char */
1257 action = make_accent_char(kbd, action, accents);
1258 if (action == ERRKEY)