kernel: Remove use of deprecated "%r" radix format.
[dragonfly.git] / sys / dev / misc / kbd / kbd.c
1 /*-
2  * (MPSAFE)
3  *
4  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer as
12  *    the first lines of this file unmodified.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/dev/kbd/kbd.c,v 1.17.2.2 2001/07/30 16:46:43 yokota Exp $
29  */
30 /*
31  * Generic keyboard driver.
32  *
33  * Interrupt note: keyboards use clist functions and since usb keyboard
34  * interrupts are not protected by spltty(), we must use a critical section
35  * to protect against corruption.
36  * XXX: this keyboard driver doesn't use clist functions anymore!
37  *
38  * MPSAFE NOTE: all keyboards could easily be put under a different global token.
39  */
40
41 #include "opt_kbd.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/conf.h>
48 #include <sys/proc.h>
49 #include <sys/tty.h>
50 #include <sys/event.h>
51 #include <sys/vnode.h>
52 #include <sys/uio.h>
53 #include <sys/thread.h>
54 #include <sys/thread2.h>
55
56 #include <machine/console.h>
57
58 #include "kbdreg.h"
59
60 #if 0
61 #define lwkt_gettoken(x)
62 #define lwkt_reltoken(x)
63 #endif
64
65 #define KBD_INDEX(dev)  minor(dev)
66
67 #define KB_QSIZE        512
68 #define KB_BUFSIZE      64
69
70 struct genkbd_softc {
71         int             gkb_flags;      /* flag/status bits */
72 #define KB_ASLEEP       (1 << 0)
73         struct kqinfo   gkb_rkq;
74         char            gkb_q[KB_QSIZE];                /* input queue */
75         unsigned int    gkb_q_start;
76         unsigned int    gkb_q_length;
77 };
78
79 typedef struct genkbd_softc *genkbd_softc_t;
80
81 static  SLIST_HEAD(, keyboard_driver) keyboard_drivers =
82         SLIST_HEAD_INITIALIZER(keyboard_drivers);
83
84 SET_DECLARE(kbddriver_set, const keyboard_driver_t);
85
86 /* local arrays */
87
88 /*
89  * We need at least one entry each in order to initialize a keyboard
90  * for the kernel console.  The arrays will be increased dynamically
91  * when necessary.
92  */
93
94 static int              keyboards = 1;
95 static keyboard_t       *kbd_ini;
96 static keyboard_t       **keyboard = &kbd_ini;
97 static keyboard_switch_t *kbdsw_ini;
98        keyboard_switch_t **kbdsw = &kbdsw_ini;
99
100 #define ARRAY_DELTA     4
101
102 static int
103 kbd_realloc_array(void)
104 {
105         keyboard_t **new_kbd;
106         keyboard_switch_t **new_kbdsw;
107         int newsize;
108
109         lwkt_gettoken(&tty_token);
110         newsize = ((keyboards + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
111         new_kbd = kmalloc(sizeof(*new_kbd) * newsize, M_DEVBUF,
112                                 M_WAITOK | M_ZERO);
113         new_kbdsw = kmalloc(sizeof(*new_kbdsw) * newsize, M_DEVBUF,
114                                 M_WAITOK | M_ZERO);
115         bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
116         bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards);
117         crit_enter();
118         if (keyboards > 1) {
119                 kfree(keyboard, M_DEVBUF);
120                 kfree(kbdsw, M_DEVBUF);
121         }
122         keyboard = new_kbd;
123         kbdsw = new_kbdsw;
124         keyboards = newsize;
125         crit_exit();
126
127         if (bootverbose)
128                 kprintf("kbd: new array size %d\n", keyboards);
129
130         lwkt_reltoken(&tty_token);
131         return 0;
132 }
133
134 /*
135  * Low-level keyboard driver functions.
136  *
137  * Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard
138  * driver, call these functions to initialize the keyboard_t structure
139  * and register it to the virtual keyboard driver `kbd'.
140  *
141  * The reinit call is made when a driver has partially detached a keyboard
142  * but does not unregistered it, then wishes to reinitialize it later on.
143  * This is how the USB keyboard driver handles the 'default' keyboard,
144  * because unregistering the keyboard associated with the console will
145  * destroy its console association forever.
146  */
147 void
148 kbd_reinit_struct(keyboard_t *kbd, int config, int pref)
149 {
150         lwkt_gettoken(&tty_token);
151         kbd->kb_flags |= KB_NO_DEVICE;  /* device has not been found */
152         kbd->kb_config = config & ~KB_CONF_PROBE_ONLY;
153         kbd->kb_led = 0;                /* unknown */
154         kbd->kb_data = NULL;
155         kbd->kb_keymap = NULL;
156         kbd->kb_accentmap = NULL;
157         kbd->kb_fkeytab = NULL;
158         kbd->kb_fkeytab_size = 0;
159         kbd->kb_delay1 = KB_DELAY1;     /* these values are advisory only */
160         kbd->kb_delay2 = KB_DELAY2;
161         kbd->kb_count = 0;
162         kbd->kb_pref = pref;
163         bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact));
164         lwkt_reltoken(&tty_token);
165 }
166
167 /* initialize the keyboard_t structure */
168 void
169 kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config,
170                 int pref, int port, int port_size)
171 {
172         lwkt_gettoken(&tty_token);
173         kbd->kb_flags = 0;
174         kbd->kb_name = name;
175         kbd->kb_type = type;
176         kbd->kb_unit = unit;
177         kbd->kb_io_base = port;
178         kbd->kb_io_size = port_size;
179         kbd_reinit_struct(kbd, config, pref);
180         lockinit(&kbd->kb_lock, name, 0, LK_CANRECURSE);
181         lwkt_reltoken(&tty_token);
182 }
183
184 void
185 kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap,
186              fkeytab_t *fkeymap, int fkeymap_size)
187 {
188         lwkt_gettoken(&tty_token);
189         kbd->kb_keymap = keymap;
190         kbd->kb_accentmap = accmap;
191         kbd->kb_fkeytab = fkeymap;
192         kbd->kb_fkeytab_size = fkeymap_size;
193         lwkt_reltoken(&tty_token);
194 }
195
196 /* declare a new keyboard driver */
197 int
198 kbd_add_driver(keyboard_driver_t *driver)
199 {
200         lwkt_gettoken(&tty_token);
201         if (SLIST_NEXT(driver, link)) {
202                 lwkt_reltoken(&tty_token);
203                 return EINVAL;
204         }
205         SLIST_INSERT_HEAD(&keyboard_drivers, driver, link);
206         lwkt_reltoken(&tty_token);
207         return 0;
208 }
209
210 int
211 kbd_delete_driver(keyboard_driver_t *driver)
212 {
213         lwkt_gettoken(&tty_token);
214         SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link);
215         SLIST_NEXT(driver, link) = NULL;
216         lwkt_reltoken(&tty_token);
217         return 0;
218 }
219
220 /* register a keyboard and associate it with a function table */
221 int
222 kbd_register(keyboard_t *kbd)
223 {
224         const keyboard_driver_t **list;
225         const keyboard_driver_t *p;
226         keyboard_t *mux;
227         keyboard_info_t ki;
228         int index;
229
230         lwkt_gettoken(&tty_token);
231         mux = kbd_get_keyboard(kbd_find_keyboard("kbdmux", -1));
232
233         for (index = 0; index < keyboards; ++index) {
234                 if (keyboard[index] == NULL)
235                         break;
236         }
237         if (index >= keyboards) {
238                 if (kbd_realloc_array()) {
239                         lwkt_reltoken(&tty_token);
240                         return -1;
241                 }
242         }
243
244         kbd->kb_index = index;
245         KBD_UNBUSY(kbd);
246         KBD_VALID(kbd);
247         kbd->kb_active = 0;     /* disabled until someone calls kbd_enable() */
248         kbd->kb_token = NULL;
249         kbd->kb_callback.kc_func = NULL;
250         kbd->kb_callback.kc_arg = NULL;
251         callout_init_mp(&kbd->kb_atkbd_timeout_ch);
252
253         SLIST_FOREACH(p, &keyboard_drivers, link) {
254                 if (strcmp(p->name, kbd->kb_name) == 0) {
255                         keyboard[index] = kbd;
256                         kbdsw[index] = p->kbdsw;
257
258                         if (mux != NULL) {
259                                 bzero(&ki, sizeof(ki));
260                                 strcpy(ki.kb_name, kbd->kb_name);
261                                 ki.kb_unit = kbd->kb_unit;
262                                 kbd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
263                         }
264
265                         lwkt_reltoken(&tty_token);
266                         return index;
267                 }
268         }
269         SET_FOREACH(list, kbddriver_set) {
270                 p = *list;
271                 if (strcmp(p->name, kbd->kb_name) == 0) {
272                         keyboard[index] = kbd;
273                         kbdsw[index] = p->kbdsw;
274
275                         if (mux != NULL) {
276                                 bzero(&ki, sizeof(ki));
277                                 strcpy(ki.kb_name, kbd->kb_name);
278                                 ki.kb_unit = kbd->kb_unit;
279                                 kbd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
280                         }
281
282                         lwkt_reltoken(&tty_token);
283                         return index;
284                 }
285         }
286
287         lwkt_reltoken(&tty_token);
288         return -1;
289 }
290
291 int
292 kbd_unregister(keyboard_t *kbd)
293 {
294         int error;
295
296         KBD_LOCK_ASSERT(kbd);
297         lwkt_gettoken(&tty_token);
298         if ((kbd->kb_index < 0) || (kbd->kb_index >= keyboards)) {
299                 lwkt_reltoken(&tty_token);
300                 return ENOENT;
301         }
302         if (keyboard[kbd->kb_index] != kbd) {
303                 lwkt_reltoken(&tty_token);
304                 return ENOENT;
305         }
306
307         crit_enter();
308         callout_stop(&kbd->kb_atkbd_timeout_ch);
309         if (KBD_IS_BUSY(kbd)) {
310                 error = (*kbd->kb_callback.kc_func)(kbd, KBDIO_UNLOADING,
311                                                     kbd->kb_callback.kc_arg);
312                 if (error) {
313                         crit_exit();
314                         lwkt_reltoken(&tty_token);
315                         return error;
316                 }
317                 if (KBD_IS_BUSY(kbd)) {
318                         crit_exit();
319                         lwkt_reltoken(&tty_token);
320                         return EBUSY;
321                 }
322         }
323         KBD_CONFIG_LOST(kbd);
324         KBD_INVALID(kbd);
325         keyboard[kbd->kb_index] = NULL;
326         kbdsw[kbd->kb_index] = NULL;
327
328         KBD_ALWAYS_UNLOCK(kbd);
329         lockuninit(&kbd->kb_lock);
330
331         crit_exit();
332         lwkt_reltoken(&tty_token);
333         return 0;
334 }
335
336 /* find a funciton table by the driver name */
337 keyboard_switch_t *
338 kbd_get_switch(char *driver)
339 {
340         const keyboard_driver_t **list;
341         const keyboard_driver_t *p;
342
343         lwkt_gettoken(&tty_token);
344
345         SLIST_FOREACH(p, &keyboard_drivers, link) {
346                 if (strcmp(p->name, driver) == 0) {
347                         lwkt_reltoken(&tty_token);
348                         return p->kbdsw;
349                 }
350         }
351         SET_FOREACH(list, kbddriver_set) {
352                 p = *list;
353                 if (strcmp(p->name, driver) == 0) {
354                         lwkt_reltoken(&tty_token);
355                         return p->kbdsw;
356                 }
357         }
358
359         lwkt_reltoken(&tty_token);
360         return NULL;
361 }
362
363 /*
364  * Keyboard client functions
365  * Keyboard clients, such as the console driver `syscons' and the keyboard
366  * cdev driver, use these functions to claim and release a keyboard for
367  * exclusive use.
368  */
369 /*
370  * find the keyboard specified by a driver name and a unit number
371  * starting at given index
372  */
373 int
374 kbd_find_keyboard2(char *driver, int unit, int index, int legacy)
375 {
376         int i;
377         int pref;
378         int pref_index;
379
380         pref = 0;
381         pref_index = -1;
382
383         lwkt_gettoken(&tty_token);
384         if ((index < 0) || (index >= keyboards)) {
385                 lwkt_reltoken(&tty_token);
386                 return (-1);
387         }
388
389         for (i = index; i < keyboards; ++i) {
390                 if (keyboard[i] == NULL)
391                         continue;
392                 if (!KBD_IS_VALID(keyboard[i]))
393                         continue;
394                 if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver))
395                         continue;
396                 if ((unit != -1) && (keyboard[i]->kb_unit != unit))
397                         continue;
398                 /*
399                  * If we are in legacy mode, we do the old preference magic and
400                  * don't return on the first found unit.
401                  */
402                 if (legacy) {
403                         if (pref <= keyboard[i]->kb_pref) {
404                                 pref = keyboard[i]->kb_pref;
405                                 pref_index = i;
406                         }
407                 } else {
408                         lwkt_reltoken(&tty_token);
409                         return i;
410                 }
411         }
412
413         if (!legacy)
414                 KKASSERT(pref_index == -1);
415
416         lwkt_reltoken(&tty_token);
417         return (pref_index);
418 }
419
420 /* find the keyboard specified by a driver name and a unit number */
421 int
422 kbd_find_keyboard(char *driver, int unit)
423 {
424         return (kbd_find_keyboard2(driver, unit, 0, 1));
425 }
426
427 /* allocate a keyboard */
428 int
429 kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func,
430              void *arg)
431 {
432         int index;
433
434         if (func == NULL)
435                 return -1;
436
437         crit_enter();
438         lwkt_gettoken(&tty_token);
439
440         index = kbd_find_keyboard(driver, unit);
441         if (index >= 0) {
442                 if (KBD_IS_BUSY(keyboard[index])) {
443                         crit_exit();
444                         lwkt_reltoken(&tty_token);
445                         return -1;
446                 }
447                 keyboard[index]->kb_token = id;
448                 KBD_BUSY(keyboard[index]);
449                 keyboard[index]->kb_callback.kc_func = func;
450                 keyboard[index]->kb_callback.kc_arg = arg;
451                 kbd_clear_state(keyboard[index]);
452         }
453
454         lwkt_reltoken(&tty_token);
455         crit_exit();
456         return index;
457 }
458
459 int
460 kbd_release(keyboard_t *kbd, void *id)
461 {
462         int error;
463
464         crit_enter();
465         lwkt_gettoken(&tty_token);
466
467         if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
468                 error = EINVAL;
469         } else if (kbd->kb_token != id) {
470                 error = EPERM;
471         } else {
472                 kbd->kb_token = NULL;
473                 KBD_UNBUSY(kbd);
474                 kbd->kb_callback.kc_func = NULL;
475                 kbd->kb_callback.kc_arg = NULL;
476                 kbd_clear_state(kbd);
477                 error = 0;
478         }
479
480         lwkt_reltoken(&tty_token);
481         crit_exit();
482         return error;
483 }
484
485 int
486 kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func,
487                     void *arg)
488 {
489         int error;
490
491         crit_enter();
492         lwkt_gettoken(&tty_token);
493
494         if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
495                 error = EINVAL;
496         } else if (kbd->kb_token != id) {
497                 error = EPERM;
498         } else if (func == NULL) {
499                 error = EINVAL;
500         } else {
501                 kbd->kb_callback.kc_func = func;
502                 kbd->kb_callback.kc_arg = arg;
503                 error = 0;
504         }
505
506         lwkt_reltoken(&tty_token);
507         crit_exit();
508         return error;
509 }
510
511 /* get a keyboard structure */
512 keyboard_t *
513 kbd_get_keyboard(int index)
514 {
515         keyboard_t *kbd;
516
517         lwkt_gettoken(&tty_token);
518         if ((index < 0) || (index >= keyboards)) {
519                 lwkt_reltoken(&tty_token);
520                 return NULL;
521         }
522         if (keyboard[index] == NULL) {
523                 lwkt_reltoken(&tty_token);
524                 return NULL;
525         }
526         if (!KBD_IS_VALID(keyboard[index])) {
527                 lwkt_reltoken(&tty_token);
528                 return NULL;
529         }
530         kbd = keyboard[index];
531         lwkt_reltoken(&tty_token);
532
533         return kbd;
534 }
535
536 /*
537  * The back door for the console driver; configure keyboards
538  * This function is for the kernel console to initialize keyboards
539  * at very early stage.
540  */
541
542 int
543 kbd_configure(int flags)
544 {
545         const keyboard_driver_t **list;
546         const keyboard_driver_t *p;
547
548         lwkt_gettoken(&tty_token);
549
550         SLIST_FOREACH(p, &keyboard_drivers, link) {
551                 if (p->configure != NULL)
552                         (*p->configure)(flags);
553         }
554         SET_FOREACH(list, kbddriver_set) {
555                 p = *list;
556                 if (p->configure != NULL)
557                         (*p->configure)(flags);
558         }
559
560         lwkt_reltoken(&tty_token);
561         return 0;
562 }
563
564 #ifdef KBD_INSTALL_CDEV
565
566 /*
567  * Virtual keyboard cdev driver functions
568  * The virtual keyboard driver dispatches driver functions to
569  * appropriate subdrivers.
570  */
571
572 #define KBD_UNIT(dev)   minor(dev)
573
574 static d_open_t         genkbdopen;
575 static d_close_t        genkbdclose;
576 static d_read_t         genkbdread;
577 static d_write_t        genkbdwrite;
578 static d_ioctl_t        genkbdioctl;
579 static d_kqfilter_t     genkbdkqfilter;
580
581 static void genkbdfiltdetach(struct knote *);
582 static int genkbdfilter(struct knote *, long);
583
584 static struct dev_ops kbd_ops = {
585         { "kbd", 0, D_MPSAFE },
586         .d_open =       genkbdopen,
587         .d_close =      genkbdclose,
588         .d_read =       genkbdread,
589         .d_write =      genkbdwrite,
590         .d_ioctl =      genkbdioctl,
591         .d_kqfilter =   genkbdkqfilter
592 };
593
594 /*
595  * Attach a keyboard.
596  *
597  * NOTE: The usb driver does not detach the default keyboard if it is
598  *       unplugged, but calls kbd_attach() when it is plugged back in.
599  */
600 int
601 kbd_attach(keyboard_t *kbd)
602 {
603         cdev_t dev;
604         char tbuf[MAKEDEV_MINNBUF];
605
606         lwkt_gettoken(&tty_token);
607         if (kbd->kb_index >= keyboards) {
608                 lwkt_reltoken(&tty_token);
609                 return EINVAL;
610         }
611         if (keyboard[kbd->kb_index] != kbd) {
612                 lwkt_reltoken(&tty_token);
613                 return EINVAL;
614         }
615
616         if (kbd->kb_dev == NULL) {
617                 kbd->kb_dev = make_dev(&kbd_ops, kbd->kb_index,
618                                        UID_ROOT, GID_WHEEL, 0600, "kbd%s",
619                                        makedev_unit_b32(tbuf, kbd->kb_index));
620         }
621         dev = kbd->kb_dev;
622         if (dev->si_drv1 == NULL) {
623                 dev->si_drv1 = kmalloc(sizeof(struct genkbd_softc), M_DEVBUF,
624                                        M_WAITOK);
625         }
626         bzero(dev->si_drv1, sizeof(struct genkbd_softc));
627
628         kprintf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
629         lwkt_reltoken(&tty_token);
630         return 0;
631 }
632
633 int
634 kbd_detach(keyboard_t *kbd)
635 {
636         cdev_t dev;
637
638         lwkt_gettoken(&tty_token);
639
640         if (kbd->kb_index >= keyboards) {
641                 lwkt_reltoken(&tty_token);
642                 return EINVAL;
643         }
644         if (keyboard[kbd->kb_index] != kbd) {
645                 lwkt_reltoken(&tty_token);
646                 return EINVAL;
647         }
648
649         if ((dev = kbd->kb_dev) != NULL) {
650                 if (dev->si_drv1) {
651                         kfree(dev->si_drv1, M_DEVBUF);
652                         dev->si_drv1 = NULL;
653                 }
654                 kbd->kb_dev = NULL;
655         }
656         dev_ops_remove_minor(&kbd_ops, kbd->kb_index);
657         lwkt_reltoken(&tty_token);
658         return 0;
659 }
660
661 /*
662  * Generic keyboard cdev driver functions
663  * Keyboard subdrivers may call these functions to implement common
664  * driver functions.
665  */
666
667 static void
668 genkbd_putc(genkbd_softc_t sc, char c)
669 {
670         unsigned int p;
671
672         lwkt_gettoken(&tty_token);
673
674         if (sc->gkb_q_length == KB_QSIZE) {
675                 lwkt_reltoken(&tty_token);
676                 return;
677         }
678
679         p = (sc->gkb_q_start + sc->gkb_q_length) % KB_QSIZE;
680         sc->gkb_q[p] = c;
681         sc->gkb_q_length++;
682
683         lwkt_reltoken(&tty_token);
684 }
685
686 static size_t
687 genkbd_getc(genkbd_softc_t sc, char *buf, size_t len)
688 {
689
690         lwkt_gettoken(&tty_token);
691
692         /* Determine copy size. */
693         if (sc->gkb_q_length == 0) {
694                 lwkt_reltoken(&tty_token);
695                 return (0);
696         }
697         if (len >= sc->gkb_q_length)
698                 len = sc->gkb_q_length;
699         if (len >= KB_QSIZE - sc->gkb_q_start)
700                 len = KB_QSIZE - sc->gkb_q_start;
701
702         /* Copy out data and progress offset. */
703         memcpy(buf, sc->gkb_q + sc->gkb_q_start, len);
704         sc->gkb_q_start = (sc->gkb_q_start + len) % KB_QSIZE;
705         sc->gkb_q_length -= len;
706
707         lwkt_reltoken(&tty_token);
708         return (len);
709 }
710
711 static kbd_callback_func_t genkbd_event;
712
713 static int
714 genkbdopen(struct dev_open_args *ap)
715 {
716         cdev_t dev = ap->a_head.a_dev;
717         keyboard_t *kbd;
718         genkbd_softc_t sc;
719         int i;
720
721         crit_enter();
722         lwkt_gettoken(&tty_token);
723         sc = dev->si_drv1;
724         kbd = kbd_get_keyboard(KBD_INDEX(dev));
725         if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
726                 lwkt_reltoken(&tty_token);
727                 crit_exit();
728                 return ENXIO;
729         }
730         i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
731                          genkbd_event, sc);
732         if (i < 0) {
733                 lwkt_reltoken(&tty_token);
734                 crit_exit();
735                 return EBUSY;
736         }
737         /* assert(i == kbd->kb_index) */
738         /* assert(kbd == kbd_get_keyboard(i)) */
739
740         /*
741          * NOTE: even when we have successfully claimed a keyboard,
742          * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
743          */
744
745         sc->gkb_q_length = 0;
746         lwkt_reltoken(&tty_token);
747         crit_exit();
748
749         return 0;
750 }
751
752 static int
753 genkbdclose(struct dev_close_args *ap)
754 {
755         cdev_t dev = ap->a_head.a_dev;
756         keyboard_t *kbd;
757         genkbd_softc_t sc;
758
759         /*
760          * NOTE: the device may have already become invalid.
761          * kbd == NULL || !KBD_IS_VALID(kbd)
762          */
763         crit_enter();
764         lwkt_gettoken(&tty_token);
765         sc = dev->si_drv1;
766         kbd = kbd_get_keyboard(KBD_INDEX(dev));
767         if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
768                 /* XXX: we shall be forgiving and don't report error... */
769         } else {
770                 kbd_release(kbd, sc);
771         }
772         lwkt_reltoken(&tty_token);
773         crit_exit();
774         return 0;
775 }
776
777 static int
778 genkbdread(struct dev_read_args *ap)
779 {
780         cdev_t dev = ap->a_head.a_dev;
781         struct uio *uio = ap->a_uio;
782         keyboard_t *kbd;
783         genkbd_softc_t sc;
784         u_char buffer[KB_BUFSIZE];
785         int len;
786         int error;
787
788         /* wait for input */
789         crit_enter();
790         lwkt_gettoken(&tty_token);
791         sc = dev->si_drv1;
792         kbd = kbd_get_keyboard(KBD_INDEX(dev));
793         if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
794                 lwkt_reltoken(&tty_token);
795                 crit_exit();
796                 return ENXIO;
797         }
798         while (sc->gkb_q_length == 0) {
799                 if (ap->a_ioflag & IO_NDELAY) { /* O_NONBLOCK? */
800                         lwkt_reltoken(&tty_token);
801                         crit_exit();
802                         return EWOULDBLOCK;
803                 }
804                 sc->gkb_flags |= KB_ASLEEP;
805                 error = tsleep((caddr_t)sc, PCATCH, "kbdrea", 0);
806                 kbd = kbd_get_keyboard(KBD_INDEX(dev));
807                 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
808                         lwkt_reltoken(&tty_token);
809                         crit_exit();
810                         return ENXIO;   /* our keyboard has gone... */
811                 }
812                 if (error) {
813                         sc->gkb_flags &= ~KB_ASLEEP;
814                         lwkt_reltoken(&tty_token);
815                         crit_exit();
816                         return error;
817                 }
818         }
819         lwkt_reltoken(&tty_token);
820         crit_exit();
821
822         /* copy as much input as possible */
823         error = 0;
824         while (uio->uio_resid > 0) {
825                 len = (int)szmin(uio->uio_resid, sizeof(buffer));
826                 len = genkbd_getc(sc, buffer, len);
827                 if (len <= 0)
828                         break;
829                 error = uiomove(buffer, (size_t)len, uio);
830                 if (error)
831                         break;
832         }
833
834         return error;
835 }
836
837 static int
838 genkbdwrite(struct dev_write_args *ap)
839 {
840         cdev_t dev = ap->a_head.a_dev;
841         keyboard_t *kbd;
842
843         lwkt_gettoken(&tty_token);
844         kbd = kbd_get_keyboard(KBD_INDEX(dev));
845         if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
846                 lwkt_reltoken(&tty_token);
847                 return ENXIO;
848         }
849         lwkt_reltoken(&tty_token);
850         return ENODEV;
851 }
852
853 static int
854 genkbdioctl(struct dev_ioctl_args *ap)
855 {
856         cdev_t dev = ap->a_head.a_dev;
857         keyboard_t *kbd;
858         int error;
859
860         lwkt_gettoken(&tty_token);
861         kbd = kbd_get_keyboard(KBD_INDEX(dev));
862         if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
863                 lwkt_reltoken(&tty_token);
864                 return ENXIO;
865         }
866         error = kbd_ioctl(kbd, ap->a_cmd, ap->a_data);
867         if (error == ENOIOCTL)
868                 error = ENODEV;
869
870         lwkt_reltoken(&tty_token);
871         return error;
872 }
873
874 static struct filterops genkbdfiltops =
875         { FILTEROP_ISFD, NULL, genkbdfiltdetach, genkbdfilter };
876
877 static int
878 genkbdkqfilter(struct dev_kqfilter_args *ap)
879 {
880         cdev_t dev = ap->a_head.a_dev;
881         struct knote *kn = ap->a_kn;
882         genkbd_softc_t sc;
883         struct klist *klist;
884
885         ap->a_result = 0;
886
887         switch (kn->kn_filter) {
888         case EVFILT_READ:
889                 kn->kn_fop = &genkbdfiltops;
890                 kn->kn_hook = (caddr_t)dev;
891                 break;
892         default:
893                 ap->a_result = EOPNOTSUPP;
894                 return (0);
895         }
896
897         sc = dev->si_drv1;
898         klist = &sc->gkb_rkq.ki_note;
899         knote_insert(klist, kn);
900
901         return (0);
902 }
903
904 static void
905 genkbdfiltdetach(struct knote *kn)
906 {
907         cdev_t dev = (cdev_t)kn->kn_hook;
908         genkbd_softc_t sc;
909         struct klist *klist;
910
911         sc = dev->si_drv1;
912         klist = &sc->gkb_rkq.ki_note;
913         knote_remove(klist, kn);
914 }
915
916 static int
917 genkbdfilter(struct knote *kn, long hint)
918 {
919         cdev_t dev = (cdev_t)kn->kn_hook;
920         keyboard_t *kbd;
921         genkbd_softc_t sc;
922         int ready = 0;
923
924         crit_enter();
925         lwkt_gettoken(&tty_token);
926         sc = dev->si_drv1;
927         kbd = kbd_get_keyboard(KBD_INDEX(dev));
928         if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
929                 /* The keyboard has gone */
930                 kn->kn_flags |= (EV_EOF | EV_NODATA);
931                 ready = 1;
932         } else {
933                 if (sc->gkb_q_length > 0)
934                         ready = 1;
935         }
936         lwkt_reltoken(&tty_token);
937         crit_exit();
938
939         return (ready);
940 }
941
942 static int
943 genkbd_event(keyboard_t *kbd, int event, void *arg)
944 {
945         genkbd_softc_t sc;
946         size_t len;
947         u_char *cp;
948         int mode;
949         int c;
950
951         lwkt_gettoken(&tty_token);
952         /* assert(KBD_IS_VALID(kbd)) */
953         sc = (genkbd_softc_t)arg;
954
955         switch (event) {
956         case KBDIO_KEYINPUT:
957                 break;
958         case KBDIO_UNLOADING:
959                 /* the keyboard is going... */
960                 kbd_release(kbd, sc);
961                 if (sc->gkb_flags & KB_ASLEEP) {
962                         sc->gkb_flags &= ~KB_ASLEEP;
963                         wakeup((caddr_t)sc);
964                 }
965                 KNOTE(&sc->gkb_rkq.ki_note, 0);
966                 lwkt_reltoken(&tty_token);
967                 return 0;
968         default:
969                 lwkt_reltoken(&tty_token);
970                 return EINVAL;
971         }
972
973         /* obtain the current key input mode */
974         if (kbd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode))
975                 mode = K_XLATE;
976
977         /* read all pending input */
978         while (kbd_check_char(kbd)) {
979                 c = kbd_read_char(kbd, FALSE);
980                 if (c == NOKEY)
981                         continue;
982                 if (c == ERRKEY)        /* XXX: ring bell? */
983                         continue;
984                 if (!KBD_IS_BUSY(kbd))
985                         /* the device is not open, discard the input */
986                         continue;
987
988                 /* store the byte as is for K_RAW and K_CODE modes */
989                 if (mode != K_XLATE) {
990                         genkbd_putc(sc, KEYCHAR(c));
991                         continue;
992                 }
993
994                 /* K_XLATE */
995                 if (c & RELKEY) /* key release is ignored */
996                         continue;
997
998                 /* process special keys; most of them are just ignored... */
999                 if (c & SPCLKEY) {
1000                         switch (KEYCHAR(c)) {
1001                         default:
1002                                 /* ignore them... */
1003                                 continue;
1004                         case BTAB:      /* a backtab: ESC [ Z */
1005                                 genkbd_putc(sc, 0x1b);
1006                                 genkbd_putc(sc, '[');
1007                                 genkbd_putc(sc, 'Z');
1008                                 continue;
1009                         }
1010                 }
1011
1012                 /* normal chars, normal chars with the META, function keys */
1013                 switch (KEYFLAGS(c)) {
1014                 case 0:                 /* a normal char */
1015                         genkbd_putc(sc, KEYCHAR(c));
1016                         break;
1017                 case MKEY:              /* the META flag: prepend ESC */
1018                         genkbd_putc(sc, 0x1b);
1019                         genkbd_putc(sc, KEYCHAR(c));
1020                         break;
1021                 case FKEY | SPCLKEY:    /* a function key, return string */
1022                         cp = kbd_get_fkeystr(kbd, KEYCHAR(c), &len);
1023                         if (cp != NULL) {
1024                                 while (len-- >  0)
1025                                         genkbd_putc(sc, *cp++);
1026                         }
1027                         break;
1028                 }
1029         }
1030
1031         /* wake up sleeping/polling processes */
1032         if (sc->gkb_q_length > 0) {
1033                 if (sc->gkb_flags & KB_ASLEEP) {
1034                         sc->gkb_flags &= ~KB_ASLEEP;
1035                         wakeup((caddr_t)sc);
1036                 }
1037                 KNOTE(&sc->gkb_rkq.ki_note, 0);
1038         }
1039
1040         lwkt_reltoken(&tty_token);
1041         return 0;
1042 }
1043
1044 #endif /* KBD_INSTALL_CDEV */
1045
1046 /*
1047  * Generic low-level keyboard functions
1048  * The low-level functions in the keyboard subdriver may use these
1049  * functions.
1050  */
1051
1052 int
1053 genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1054 {
1055         keyarg_t *keyp;
1056         fkeyarg_t *fkeyp;
1057         int i;
1058
1059         crit_enter();
1060         lwkt_gettoken(&tty_token);
1061         switch (cmd) {
1062
1063         case KDGKBINFO:         /* get keyboard information */
1064                 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
1065                 i = imin(strlen(kbd->kb_name) + 1,
1066                          sizeof(((keyboard_info_t *)arg)->kb_name));
1067                 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
1068                 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
1069                 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
1070                 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
1071                 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
1072                 break;
1073
1074         case KDGKBTYPE:         /* get keyboard type */
1075                 *(int *)arg = kbd->kb_type;
1076                 break;
1077
1078         case KDGETREPEAT:       /* get keyboard repeat rate */
1079                 ((int *)arg)[0] = kbd->kb_delay1;
1080                 ((int *)arg)[1] = kbd->kb_delay2; 
1081                 break;
1082
1083         case GIO_KEYMAP:        /* get keyboard translation table */
1084                 bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap));
1085                 break;
1086         case PIO_KEYMAP:        /* set keyboard translation table */
1087 #ifndef KBD_DISABLE_KEYMAP_LOAD
1088                 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
1089                 bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
1090                 break;
1091 #else
1092                 lwkt_reltoken(&tty_token);
1093                 crit_exit();
1094                 return ENODEV;
1095 #endif
1096
1097         case GIO_KEYMAPENT:     /* get keyboard translation table entry */
1098                 keyp = (keyarg_t *)arg;
1099                 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
1100                                         /sizeof(kbd->kb_keymap->key[0])) {
1101                         lwkt_reltoken(&tty_token);
1102                         crit_exit();
1103                         return EINVAL;
1104                 }
1105                 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
1106                       sizeof(keyp->key));
1107                 break;
1108         case PIO_KEYMAPENT:     /* set keyboard translation table entry */
1109 #ifndef KBD_DISABLE_KEYMAP_LOAD
1110                 keyp = (keyarg_t *)arg;
1111                 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
1112                                         /sizeof(kbd->kb_keymap->key[0])) {
1113                         lwkt_reltoken(&tty_token);
1114                         crit_exit();
1115                         return EINVAL;
1116                 }
1117                 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
1118                       sizeof(keyp->key));
1119                 break;
1120 #else
1121                 lwkt_reltoken(&tty_token);
1122                 crit_exit();
1123                 return ENODEV;
1124 #endif
1125
1126         case GIO_DEADKEYMAP:    /* get accent key translation table */
1127                 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
1128                 break;
1129         case PIO_DEADKEYMAP:    /* set accent key translation table */
1130 #ifndef KBD_DISABLE_KEYMAP_LOAD
1131                 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
1132                 break;
1133 #else
1134                 lwkt_reltoken(&tty_token);
1135                 crit_exit();
1136                 return ENODEV;
1137 #endif
1138
1139         case GETFKEY:           /* get functionkey string */
1140                 fkeyp = (fkeyarg_t *)arg;
1141                 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
1142                         lwkt_reltoken(&tty_token);
1143                         crit_exit();
1144                         return EINVAL;
1145                 }
1146                 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
1147                       kbd->kb_fkeytab[fkeyp->keynum].len);
1148                 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
1149                 break;
1150         case SETFKEY:           /* set functionkey string */
1151 #ifndef KBD_DISABLE_KEYMAP_LOAD
1152                 fkeyp = (fkeyarg_t *)arg;
1153                 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
1154                         lwkt_reltoken(&tty_token);
1155                         crit_exit();
1156                         return EINVAL;
1157                 }
1158                 kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK);
1159                 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
1160                       kbd->kb_fkeytab[fkeyp->keynum].len);
1161                 break;
1162 #else
1163                 lwkt_reltoken(&tty_token);
1164                 crit_exit();
1165                 return ENODEV;
1166 #endif
1167
1168         default:
1169                 lwkt_reltoken(&tty_token);
1170                 crit_exit();
1171                 return ENOIOCTL;
1172         }
1173
1174         lwkt_reltoken(&tty_token);
1175         crit_exit();
1176         return 0;
1177 }
1178
1179 /* get a pointer to the string associated with the given function key */
1180 u_char *
1181 genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
1182 {
1183         u_char *ch;
1184
1185         if (kbd == NULL)
1186                 return NULL;
1187
1188         lwkt_gettoken(&tty_token);
1189         fkey -= F_FN;
1190         if (fkey > kbd->kb_fkeytab_size) {
1191                 lwkt_reltoken(&tty_token);
1192                 return NULL;
1193         }
1194         *len = kbd->kb_fkeytab[fkey].len;
1195         ch = kbd->kb_fkeytab[fkey].str;
1196
1197         lwkt_reltoken(&tty_token);
1198         return ch;
1199 }
1200
1201 /* diagnostic dump */
1202 static char *
1203 get_kbd_type_name(int type)
1204 {
1205         static struct {
1206                 int type;
1207                 char *name;
1208         } name_table[] = {
1209                 { KB_84,        "AT 84" },
1210                 { KB_101,       "AT 101/102" },
1211                 { KB_OTHER,     "generic" },
1212         };
1213         int i;
1214
1215         for (i = 0; i < NELEM(name_table); ++i) {
1216                 if (type == name_table[i].type)
1217                         return name_table[i].name;
1218         }
1219         return "unknown";
1220 }
1221
1222 void
1223 genkbd_diag(keyboard_t *kbd, int level)
1224 {
1225         if (level > 0) {
1226                 kprintf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x", 
1227                        kbd->kb_index, kbd->kb_name, kbd->kb_unit,
1228                        get_kbd_type_name(kbd->kb_type), kbd->kb_type,
1229                        kbd->kb_config, kbd->kb_flags);
1230                 if (kbd->kb_io_base > 0)
1231                         kprintf(", port:0x%x-0x%x", kbd->kb_io_base, 
1232                                kbd->kb_io_base + kbd->kb_io_size - 1);
1233                 kprintf("\n");
1234         }
1235 }
1236
1237 #define set_lockkey_state(k, s, l)                              \
1238         if (!((s) & l ## DOWN)) {                               \
1239                 int i;                                          \
1240                 (s) |= l ## DOWN;                               \
1241                 (s) ^= l ## ED;                                 \
1242                 i = (s) & LOCK_MASK;                            \
1243                 kbd_ioctl((k), KDSETLED, (caddr_t)&i); \
1244         }
1245
1246 static u_int
1247 save_accent_key(keyboard_t *kbd, u_int key, int *accents)
1248 {
1249         int i;
1250
1251         lwkt_gettoken(&tty_token);
1252         /* make an index into the accent map */
1253         i = key - F_ACC + 1;
1254         if ((i > kbd->kb_accentmap->n_accs)
1255             || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
1256                 /* the index is out of range or pointing to an empty entry */
1257                 *accents = 0;
1258                 lwkt_reltoken(&tty_token);
1259                 return ERRKEY;
1260         }
1261
1262         /* 
1263          * If the same accent key has been hit twice, produce the accent char
1264          * itself.
1265          */
1266         if (i == *accents) {
1267                 key = kbd->kb_accentmap->acc[i - 1].accchar;
1268                 *accents = 0;
1269                 lwkt_reltoken(&tty_token);
1270                 return key;
1271         }
1272
1273         /* remember the index and wait for the next key  */
1274         *accents = i; 
1275         lwkt_reltoken(&tty_token);
1276         return NOKEY;
1277 }
1278
1279 static u_int
1280 make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
1281 {
1282         struct acc_t *acc;
1283         int i;
1284
1285         lwkt_gettoken(&tty_token);
1286         acc = &kbd->kb_accentmap->acc[*accents - 1];
1287         *accents = 0;
1288
1289         /* 
1290          * If the accent key is followed by the space key,
1291          * produce the accent char itself.
1292          */
1293         if (ch == ' ') {
1294                 lwkt_reltoken(&tty_token);
1295                 return acc->accchar;
1296         }
1297
1298         /* scan the accent map */
1299         for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1300                 if (acc->map[i][0] == 0)        /* end of table */
1301                         break;
1302                 if (acc->map[i][0] == ch) {
1303                         lwkt_reltoken(&tty_token);
1304                         return acc->map[i][1];
1305                 }
1306         }
1307         lwkt_reltoken(&tty_token);
1308         /* this char cannot be accented... */
1309         return ERRKEY;
1310 }
1311
1312 int
1313 genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1314                  int *accents)
1315 {
1316         struct keyent_t *key;
1317         int state = *shiftstate;
1318         int action;
1319         int f;
1320         int i;
1321
1322         lwkt_gettoken(&tty_token);
1323         i = keycode;
1324         f = state & (AGRS | ALKED);
1325         if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1326                 i += ALTGR_OFFSET;
1327         key = &kbd->kb_keymap->key[i];
1328         i = ((state & SHIFTS) ? 1 : 0)
1329             | ((state & CTLS) ? 2 : 0)
1330             | ((state & ALTS) ? 4 : 0);
1331         if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1332                 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1333                 i ^= 1;
1334
1335         if (up) {       /* break: key released */
1336                 action = kbd->kb_lastact[keycode];
1337                 kbd->kb_lastact[keycode] = NOP;
1338                 switch (action) {
1339                 case LSHA:
1340                         if (state & SHIFTAON) {
1341                                 set_lockkey_state(kbd, state, ALK);
1342                                 state &= ~ALKDOWN;
1343                         }
1344                         action = LSH;
1345                         /* FALL THROUGH */
1346                 case LSH:
1347                         state &= ~SHIFTS1;
1348                         break;
1349                 case RSHA:
1350                         if (state & SHIFTAON) {
1351                                 set_lockkey_state(kbd, state, ALK);
1352                                 state &= ~ALKDOWN;
1353                         }
1354                         action = RSH;
1355                         /* FALL THROUGH */
1356                 case RSH:
1357                         state &= ~SHIFTS2;
1358                         break;
1359                 case LCTRA:
1360                         if (state & SHIFTAON) {
1361                                 set_lockkey_state(kbd, state, ALK);
1362                                 state &= ~ALKDOWN;
1363                         }
1364                         action = LCTR;
1365                         /* FALL THROUGH */
1366                 case LCTR:
1367                         state &= ~CTLS1;
1368                         break;
1369                 case RCTRA:
1370                         if (state & SHIFTAON) {
1371                                 set_lockkey_state(kbd, state, ALK);
1372                                 state &= ~ALKDOWN;
1373                         }
1374                         action = RCTR;
1375                         /* FALL THROUGH */
1376                 case RCTR:
1377                         state &= ~CTLS2;
1378                         break;
1379                 case LALTA:
1380                         if (state & SHIFTAON) {
1381                                 set_lockkey_state(kbd, state, ALK);
1382                                 state &= ~ALKDOWN;
1383                         }
1384                         action = LALT;
1385                         /* FALL THROUGH */
1386                 case LALT:
1387                         state &= ~ALTS1;
1388                         break;
1389                 case RALTA:
1390                         if (state & SHIFTAON) {
1391                                 set_lockkey_state(kbd, state, ALK);
1392                                 state &= ~ALKDOWN;
1393                         }
1394                         action = RALT;
1395                         /* FALL THROUGH */
1396                 case RALT:
1397                         state &= ~ALTS2;
1398                         break;
1399                 case ASH:
1400                         state &= ~AGRS1;
1401                         break;
1402                 case META:
1403                         state &= ~METAS1;
1404                         break;
1405                 case NLK:
1406                         state &= ~NLKDOWN;
1407                         break;
1408                 case CLK:
1409                         state &= ~CLKDOWN;
1410                         break;
1411                 case SLK:
1412                         state &= ~SLKDOWN;
1413                         break;
1414                 case ALK:
1415                         state &= ~ALKDOWN;
1416                         break;
1417                 case NOP:
1418                         /* release events of regular keys are not reported */
1419                         *shiftstate &= ~SHIFTAON;
1420                         lwkt_reltoken(&tty_token);
1421                         return NOKEY;
1422                 }
1423                 *shiftstate = state & ~SHIFTAON;
1424                 lwkt_reltoken(&tty_token);
1425                 return (SPCLKEY | RELKEY | action);
1426         } else {        /* make: key pressed */
1427                 action = key->map[i];
1428                 state &= ~SHIFTAON;
1429                 if (key->spcl & (0x80 >> i)) {
1430                         /* special keys */
1431                         if (kbd->kb_lastact[keycode] == NOP)
1432                                 kbd->kb_lastact[keycode] = action;
1433                         if (kbd->kb_lastact[keycode] != action)
1434                                 action = NOP;
1435                         switch (action) {
1436                         /* LOCKING KEYS */
1437                         case NLK:
1438                                 set_lockkey_state(kbd, state, NLK);
1439                                 break;
1440                         case CLK:
1441                                 set_lockkey_state(kbd, state, CLK);
1442                                 break;
1443                         case SLK:
1444                                 set_lockkey_state(kbd, state, SLK);
1445                                 break;
1446                         case ALK:
1447                                 set_lockkey_state(kbd, state, ALK);
1448                                 break;
1449                         /* NON-LOCKING KEYS */
1450                         case SPSC: case RBT:  case SUSP: case STBY:
1451                         case DBG:  case NEXT: case PREV: case PNC:
1452                         case HALT: case PDWN:
1453                                 *accents = 0;
1454                                 break;
1455                         case BTAB:
1456                                 *accents = 0;
1457                                 action |= BKEY;
1458                                 break;
1459                         case LSHA:
1460                                 state |= SHIFTAON;
1461                                 action = LSH;
1462                                 /* FALL THROUGH */
1463                         case LSH:
1464                                 state |= SHIFTS1;
1465                                 break;
1466                         case RSHA:
1467                                 state |= SHIFTAON;
1468                                 action = RSH;
1469                                 /* FALL THROUGH */
1470                         case RSH:
1471                                 state |= SHIFTS2;
1472                                 break;
1473                         case LCTRA:
1474                                 state |= SHIFTAON;
1475                                 action = LCTR;
1476                                 /* FALL THROUGH */
1477                         case LCTR:
1478                                 state |= CTLS1;
1479                                 break;
1480                         case RCTRA:
1481                                 state |= SHIFTAON;
1482                                 action = RCTR;
1483                                 /* FALL THROUGH */
1484                         case RCTR:
1485                                 state |= CTLS2;
1486                                 break;
1487                         case LALTA:
1488                                 state |= SHIFTAON;
1489                                 action = LALT;
1490                                 /* FALL THROUGH */
1491                         case LALT:
1492                                 state |= ALTS1;
1493                                 break;
1494                         case RALTA:
1495                                 state |= SHIFTAON;
1496                                 action = RALT;
1497                                 /* FALL THROUGH */
1498                         case RALT:
1499                                 state |= ALTS2;
1500                                 break;
1501                         case ASH:
1502                                 state |= AGRS1;
1503                                 break;
1504                         case META:
1505                                 state |= METAS1;
1506                                 break;
1507                         case NOP:
1508                                 *shiftstate = state;
1509                                 lwkt_reltoken(&tty_token);
1510                                 return NOKEY;
1511                         default:
1512                                 /* is this an accent (dead) key? */
1513                                 *shiftstate = state;
1514                                 if (action >= F_ACC && action <= L_ACC) {
1515                                         action = save_accent_key(kbd, action,
1516                                                                  accents);
1517                                         switch (action) {
1518                                         case NOKEY:
1519                                         case ERRKEY:
1520                                                 lwkt_reltoken(&tty_token);
1521                                                 return action;
1522                                         default:
1523                                                 if (state & METAS) {
1524                                                         lwkt_reltoken(&tty_token);
1525                                                         return (action | MKEY);
1526                                                 } else { 
1527                                                         lwkt_reltoken(&tty_token);
1528                                                         return action;
1529                                                 }
1530                                         }
1531                                         /* NOT REACHED */
1532                                 }
1533                                 /* other special keys */
1534                                 if (*accents > 0) {
1535                                         *accents = 0;
1536                                         lwkt_reltoken(&tty_token);
1537                                         return ERRKEY;
1538                                 }
1539                                 if (action >= F_FN && action <= L_FN)
1540                                         action |= FKEY;
1541                                 /* XXX: return fkey string for the FKEY? */
1542                                 lwkt_reltoken(&tty_token);
1543                                 return (SPCLKEY | action);
1544                         }
1545                         *shiftstate = state;
1546                         lwkt_reltoken(&tty_token);
1547                         return (SPCLKEY | action);
1548                 } else {
1549                         /* regular keys */
1550                         kbd->kb_lastact[keycode] = NOP;
1551                         *shiftstate = state;
1552                         if (*accents > 0) {
1553                                 /* make an accented char */
1554                                 action = make_accent_char(kbd, action, accents);
1555                                 if (action == ERRKEY) {
1556                                         lwkt_reltoken(&tty_token);
1557                                         return action;
1558                                 }
1559                         }
1560                         if (state & METAS)
1561                                 action |= MKEY;
1562                         lwkt_reltoken(&tty_token);
1563                         return action;
1564                 }
1565         }
1566         /* NOT REACHED */
1567         lwkt_reltoken(&tty_token);
1568 }