5babafaf2796c2fcce340569ec897abc040aad2a
[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  * $DragonFly: src/sys/dev/misc/kbd/kbd.c,v 1.23 2007/05/08 02:31:39 dillon Exp $
30  */
31 /*
32  * Generic keyboard driver.
33  *
34  * Interrupt note: keyboards use clist functions and since usb keyboard
35  * interrupts are not protected by spltty(), we must use a critical section
36  * to protect against corruption.
37  * XXX: this keyboard driver doesn't use clist functions anymore!
38  *
39  * MPSAFE NOTE: all keyboards could easily be put under a different global token.
40  */
41
42 #include "opt_kbd.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/conf.h>
49 #include <sys/proc.h>
50 #include <sys/tty.h>
51 #include <sys/event.h>
52 #include <sys/vnode.h>
53 #include <sys/uio.h>
54 #include <sys/thread.h>
55 #include <sys/thread2.h>
56
57 #include <machine/console.h>
58
59 #include "kbdreg.h"
60
61 #if 0
62 #define lwkt_gettoken(x)
63 #define lwkt_reltoken(x)
64 #endif
65
66 #define KBD_INDEX(dev)  minor(dev)
67
68 #define KB_QSIZE        512
69 #define KB_BUFSIZE      64
70
71 struct genkbd_softc {
72         int             gkb_flags;      /* flag/status bits */
73 #define KB_ASLEEP       (1 << 0)
74         struct kqinfo   gkb_rkq;
75         char            gkb_q[KB_QSIZE];                /* input queue */
76         unsigned int    gkb_q_start;
77         unsigned int    gkb_q_length;
78 };
79
80 typedef struct genkbd_softc *genkbd_softc_t;
81
82 static  SLIST_HEAD(, keyboard_driver) keyboard_drivers =
83         SLIST_HEAD_INITIALIZER(keyboard_drivers);
84
85 SET_DECLARE(kbddriver_set, const keyboard_driver_t);
86
87 /* local arrays */
88
89 /*
90  * We need at least one entry each in order to initialize a keyboard
91  * for the kernel console.  The arrays will be increased dynamically
92  * when necessary.
93  */
94
95 static int              keyboards = 1;
96 static keyboard_t       *kbd_ini;
97 static keyboard_t       **keyboard = &kbd_ini;
98 static keyboard_switch_t *kbdsw_ini;
99        keyboard_switch_t **kbdsw = &kbdsw_ini;
100
101 #define ARRAY_DELTA     4
102
103 static int
104 kbd_realloc_array(void)
105 {
106         keyboard_t **new_kbd;
107         keyboard_switch_t **new_kbdsw;
108         int newsize;
109
110         lwkt_gettoken(&tty_token);
111         newsize = ((keyboards + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
112         new_kbd = kmalloc(sizeof(*new_kbd) * newsize, M_DEVBUF,
113                                 M_WAITOK | M_ZERO);
114         new_kbdsw = kmalloc(sizeof(*new_kbdsw) * newsize, M_DEVBUF,
115                                 M_WAITOK | M_ZERO);
116         bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
117         bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards);
118         crit_enter();
119         if (keyboards > 1) {
120                 kfree(keyboard, M_DEVBUF);
121                 kfree(kbdsw, M_DEVBUF);
122         }
123         keyboard = new_kbd;
124         kbdsw = new_kbdsw;
125         keyboards = newsize;
126         crit_exit();
127
128         if (bootverbose)
129                 kprintf("kbd: new array size %d\n", keyboards);
130
131         lwkt_reltoken(&tty_token);
132         return 0;
133 }
134
135 /*
136  * Low-level keyboard driver functions.
137  *
138  * Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard
139  * driver, call these functions to initialize the keyboard_t structure
140  * and register it to the virtual keyboard driver `kbd'.
141  *
142  * The reinit call is made when a driver has partially detached a keyboard
143  * but does not unregistered it, then wishes to reinitialize it later on.
144  * This is how the USB keyboard driver handles the 'default' keyboard,
145  * because unregistering the keyboard associated with the console will
146  * destroy its console association forever.
147  */
148 void
149 kbd_reinit_struct(keyboard_t *kbd, int config, int pref)
150 {
151         lwkt_gettoken(&tty_token);
152         kbd->kb_flags |= KB_NO_DEVICE;  /* device has not been found */
153         kbd->kb_config = config & ~KB_CONF_PROBE_ONLY;
154         kbd->kb_led = 0;                /* unknown */
155         kbd->kb_data = NULL;
156         kbd->kb_keymap = NULL;
157         kbd->kb_accentmap = NULL;
158         kbd->kb_fkeytab = NULL;
159         kbd->kb_fkeytab_size = 0;
160         kbd->kb_delay1 = KB_DELAY1;     /* these values are advisory only */
161         kbd->kb_delay2 = KB_DELAY2;
162         kbd->kb_count = 0;
163         kbd->kb_pref = pref;
164         bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact));
165         lwkt_reltoken(&tty_token);
166 }
167
168 /* initialize the keyboard_t structure */
169 void
170 kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config,
171                 int pref, int port, int port_size)
172 {
173         lwkt_gettoken(&tty_token);
174         kbd->kb_flags = 0;
175         kbd->kb_name = name;
176         kbd->kb_type = type;
177         kbd->kb_unit = unit;
178         kbd->kb_io_base = port;
179         kbd->kb_io_size = port_size;
180         kbd_reinit_struct(kbd, config, pref);
181         lockinit(&kbd->kb_lock, name, 0, LK_CANRECURSE);
182         lwkt_reltoken(&tty_token);
183 }
184
185 void
186 kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap,
187              fkeytab_t *fkeymap, int fkeymap_size)
188 {
189         lwkt_gettoken(&tty_token);
190         kbd->kb_keymap = keymap;
191         kbd->kb_accentmap = accmap;
192         kbd->kb_fkeytab = fkeymap;
193         kbd->kb_fkeytab_size = fkeymap_size;
194         lwkt_reltoken(&tty_token);
195 }
196
197 /* declare a new keyboard driver */
198 int
199 kbd_add_driver(keyboard_driver_t *driver)
200 {
201         lwkt_gettoken(&tty_token);
202         if (SLIST_NEXT(driver, link)) {
203                 lwkt_reltoken(&tty_token);
204                 return EINVAL;
205         }
206         SLIST_INSERT_HEAD(&keyboard_drivers, driver, link);
207         lwkt_reltoken(&tty_token);
208         return 0;
209 }
210
211 int
212 kbd_delete_driver(keyboard_driver_t *driver)
213 {
214         lwkt_gettoken(&tty_token);
215         SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link);
216         SLIST_NEXT(driver, link) = NULL;
217         lwkt_reltoken(&tty_token);
218         return 0;
219 }
220
221 /* register a keyboard and associate it with a function table */
222 int
223 kbd_register(keyboard_t *kbd)
224 {
225         const keyboard_driver_t **list;
226         const keyboard_driver_t *p;
227         keyboard_t *mux;
228         keyboard_info_t ki;
229         int index;
230
231         lwkt_gettoken(&tty_token);
232         mux = kbd_get_keyboard(kbd_find_keyboard("kbdmux", -1));
233
234         for (index = 0; index < keyboards; ++index) {
235                 if (keyboard[index] == NULL)
236                         break;
237         }
238         if (index >= keyboards) {
239                 if (kbd_realloc_array()) {
240                         lwkt_reltoken(&tty_token);
241                         return -1;
242                 }
243         }
244
245         kbd->kb_index = index;
246         KBD_UNBUSY(kbd);
247         KBD_VALID(kbd);
248         kbd->kb_active = 0;     /* disabled until someone calls kbd_enable() */
249         kbd->kb_token = NULL;
250         kbd->kb_callback.kc_func = NULL;
251         kbd->kb_callback.kc_arg = NULL;
252         callout_init_mp(&kbd->kb_atkbd_timeout_ch);
253
254         SLIST_FOREACH(p, &keyboard_drivers, link) {
255                 if (strcmp(p->name, kbd->kb_name) == 0) {
256                         keyboard[index] = kbd;
257                         kbdsw[index] = p->kbdsw;
258
259                         if (mux != NULL) {
260                                 bzero(&ki, sizeof(ki));
261                                 strcpy(ki.kb_name, kbd->kb_name);
262                                 ki.kb_unit = kbd->kb_unit;
263                                 kbd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
264                         }
265
266                         lwkt_reltoken(&tty_token);
267                         return index;
268                 }
269         }
270         SET_FOREACH(list, kbddriver_set) {
271                 p = *list;
272                 if (strcmp(p->name, kbd->kb_name) == 0) {
273                         keyboard[index] = kbd;
274                         kbdsw[index] = p->kbdsw;
275
276                         if (mux != NULL) {
277                                 bzero(&ki, sizeof(ki));
278                                 strcpy(ki.kb_name, kbd->kb_name);
279                                 ki.kb_unit = kbd->kb_unit;
280                                 kbd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
281                         }
282
283                         lwkt_reltoken(&tty_token);
284                         return index;
285                 }
286         }
287
288         lwkt_reltoken(&tty_token);
289         return -1;
290 }
291
292 int
293 kbd_unregister(keyboard_t *kbd)
294 {
295         int error;
296
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                         return -1;
445                 }
446                 keyboard[index]->kb_token = id;
447                 KBD_BUSY(keyboard[index]);
448                 keyboard[index]->kb_callback.kc_func = func;
449                 keyboard[index]->kb_callback.kc_arg = arg;
450                 kbd_clear_state(keyboard[index]);
451         }
452
453         lwkt_reltoken(&tty_token);
454         crit_exit();
455         return index;
456 }
457
458 int
459 kbd_release(keyboard_t *kbd, void *id)
460 {
461         int error;
462
463         crit_enter();
464         lwkt_gettoken(&tty_token);
465
466         if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
467                 error = EINVAL;
468         } else if (kbd->kb_token != id) {
469                 error = EPERM;
470         } else {
471                 kbd->kb_token = NULL;
472                 KBD_UNBUSY(kbd);
473                 kbd->kb_callback.kc_func = NULL;
474                 kbd->kb_callback.kc_arg = NULL;
475                 kbd_clear_state(kbd);
476                 error = 0;
477         }
478
479         lwkt_reltoken(&tty_token);
480         crit_exit();
481         return error;
482 }
483
484 int
485 kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func,
486                     void *arg)
487 {
488         int error;
489
490         crit_enter();
491         lwkt_gettoken(&tty_token);
492
493         if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
494                 error = EINVAL;
495         } else if (kbd->kb_token != id) {
496                 error = EPERM;
497         } else if (func == NULL) {
498                 error = EINVAL;
499         } else {
500                 kbd->kb_callback.kc_func = func;
501                 kbd->kb_callback.kc_arg = arg;
502                 error = 0;
503         }
504
505         lwkt_reltoken(&tty_token);
506         crit_exit();
507         return error;
508 }
509
510 /* get a keyboard structure */
511 keyboard_t *
512 kbd_get_keyboard(int index)
513 {
514         keyboard_t *kbd;
515
516         lwkt_gettoken(&tty_token);
517         if ((index < 0) || (index >= keyboards)) {
518                 lwkt_reltoken(&tty_token);
519                 return NULL;
520         }
521         if (keyboard[index] == NULL) {
522                 lwkt_reltoken(&tty_token);
523                 return NULL;
524         }
525         if (!KBD_IS_VALID(keyboard[index])) {
526                 lwkt_reltoken(&tty_token);
527                 return NULL;
528         }
529         kbd = keyboard[index];
530         lwkt_reltoken(&tty_token);
531
532         return kbd;
533 }
534
535 /*
536  * The back door for the console driver; configure keyboards
537  * This function is for the kernel console to initialize keyboards
538  * at very early stage.
539  */
540
541 int
542 kbd_configure(int flags)
543 {
544         const keyboard_driver_t **list;
545         const keyboard_driver_t *p;
546
547         lwkt_gettoken(&tty_token);
548
549         SLIST_FOREACH(p, &keyboard_drivers, link) {
550                 if (p->configure != NULL)
551                         (*p->configure)(flags);
552         }
553         SET_FOREACH(list, kbddriver_set) {
554                 p = *list;
555                 if (p->configure != NULL)
556                         (*p->configure)(flags);
557         }
558
559         lwkt_reltoken(&tty_token);
560         return 0;
561 }
562
563 #ifdef KBD_INSTALL_CDEV
564
565 /*
566  * Virtual keyboard cdev driver functions
567  * The virtual keyboard driver dispatches driver functions to
568  * appropriate subdrivers.
569  */
570
571 #define KBD_UNIT(dev)   minor(dev)
572
573 static d_open_t         genkbdopen;
574 static d_close_t        genkbdclose;
575 static d_read_t         genkbdread;
576 static d_write_t        genkbdwrite;
577 static d_ioctl_t        genkbdioctl;
578 static d_kqfilter_t     genkbdkqfilter;
579
580 static void genkbdfiltdetach(struct knote *);
581 static int genkbdfilter(struct knote *, long);
582
583 #define CDEV_MAJOR      112
584
585 static struct dev_ops kbd_ops = {
586         { "kbd", CDEV_MAJOR, 0 },
587         .d_open =       genkbdopen,
588         .d_close =      genkbdclose,
589         .d_read =       genkbdread,
590         .d_write =      genkbdwrite,
591         .d_ioctl =      genkbdioctl,
592         .d_kqfilter =   genkbdkqfilter
593 };
594
595 /*
596  * Attach a keyboard.
597  *
598  * NOTE: The usb driver does not detach the default keyboard if it is
599  *       unplugged, but calls kbd_attach() when it is plugged back in.
600  */
601 int
602 kbd_attach(keyboard_t *kbd)
603 {
604         cdev_t dev;
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,
619                                        "kbd%r", 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, (void *)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, (void *)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                 kn->kn_flags |= EV_EOF; /* the keyboard has gone */
930                 ready = 1;
931         } else {
932                 if (sc->gkb_q_length > 0)
933                         ready = 1;
934         }
935         lwkt_reltoken(&tty_token);
936         crit_exit();
937
938         return (ready);
939 }
940
941 static int
942 genkbd_event(keyboard_t *kbd, int event, void *arg)
943 {
944         genkbd_softc_t sc;
945         size_t len;
946         u_char *cp;
947         int mode;
948         int c;
949
950         lwkt_gettoken(&tty_token);
951         /* assert(KBD_IS_VALID(kbd)) */
952         sc = (genkbd_softc_t)arg;
953
954         switch (event) {
955         case KBDIO_KEYINPUT:
956                 break;
957         case KBDIO_UNLOADING:
958                 /* the keyboard is going... */
959                 kbd_release(kbd, (void *)sc);
960                 if (sc->gkb_flags & KB_ASLEEP) {
961                         sc->gkb_flags &= ~KB_ASLEEP;
962                         wakeup((caddr_t)sc);
963                 }
964                 KNOTE(&sc->gkb_rkq.ki_note, 0);
965                 lwkt_reltoken(&tty_token);
966                 return 0;
967         default:
968                 lwkt_reltoken(&tty_token);
969                 return EINVAL;
970         }
971
972         /* obtain the current key input mode */
973         if (kbd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode))
974                 mode = K_XLATE;
975
976         /* read all pending input */
977         while (kbd_check_char(kbd)) {
978                 c = kbd_read_char(kbd, FALSE);
979                 if (c == NOKEY)
980                         continue;
981                 if (c == ERRKEY)        /* XXX: ring bell? */
982                         continue;
983                 if (!KBD_IS_BUSY(kbd))
984                         /* the device is not open, discard the input */
985                         continue;
986
987                 /* store the byte as is for K_RAW and K_CODE modes */
988                 if (mode != K_XLATE) {
989                         genkbd_putc(sc, KEYCHAR(c));
990                         continue;
991                 }
992
993                 /* K_XLATE */
994                 if (c & RELKEY) /* key release is ignored */
995                         continue;
996
997                 /* process special keys; most of them are just ignored... */
998                 if (c & SPCLKEY) {
999                         switch (KEYCHAR(c)) {
1000                         default:
1001                                 /* ignore them... */
1002                                 continue;
1003                         case BTAB:      /* a backtab: ESC [ Z */
1004                                 genkbd_putc(sc, 0x1b);
1005                                 genkbd_putc(sc, '[');
1006                                 genkbd_putc(sc, 'Z');
1007                                 continue;
1008                         }
1009                 }
1010
1011                 /* normal chars, normal chars with the META, function keys */
1012                 switch (KEYFLAGS(c)) {
1013                 case 0:                 /* a normal char */
1014                         genkbd_putc(sc, KEYCHAR(c));
1015                         break;
1016                 case MKEY:              /* the META flag: prepend ESC */
1017                         genkbd_putc(sc, 0x1b);
1018                         genkbd_putc(sc, KEYCHAR(c));
1019                         break;
1020                 case FKEY | SPCLKEY:    /* a function key, return string */
1021                         cp = kbd_get_fkeystr(kbd, KEYCHAR(c), &len);
1022                         if (cp != NULL) {
1023                                 while (len-- >  0)
1024                                         genkbd_putc(sc, *cp++);
1025                         }
1026                         break;
1027                 }
1028         }
1029
1030         /* wake up sleeping/polling processes */
1031         if (sc->gkb_q_length > 0) {
1032                 if (sc->gkb_flags & KB_ASLEEP) {
1033                         sc->gkb_flags &= ~KB_ASLEEP;
1034                         wakeup((caddr_t)sc);
1035                 }
1036                 KNOTE(&sc->gkb_rkq.ki_note, 0);
1037         }
1038
1039         lwkt_reltoken(&tty_token);
1040         return 0;
1041 }
1042
1043 #endif /* KBD_INSTALL_CDEV */
1044
1045 /*
1046  * Generic low-level keyboard functions
1047  * The low-level functions in the keyboard subdriver may use these
1048  * functions.
1049  */
1050
1051 int
1052 genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1053 {
1054         keyarg_t *keyp;
1055         fkeyarg_t *fkeyp;
1056         int i;
1057
1058         crit_enter();
1059         lwkt_gettoken(&tty_token);
1060         switch (cmd) {
1061
1062         case KDGKBINFO:         /* get keyboard information */
1063                 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
1064                 i = imin(strlen(kbd->kb_name) + 1,
1065                          sizeof(((keyboard_info_t *)arg)->kb_name));
1066                 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
1067                 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
1068                 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
1069                 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
1070                 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
1071                 break;
1072
1073         case KDGKBTYPE:         /* get keyboard type */
1074                 *(int *)arg = kbd->kb_type;
1075                 break;
1076
1077         case KDGETREPEAT:       /* get keyboard repeat rate */
1078                 ((int *)arg)[0] = kbd->kb_delay1;
1079                 ((int *)arg)[1] = kbd->kb_delay2; 
1080                 break;
1081
1082         case GIO_KEYMAP:        /* get keyboard translation table */
1083                 bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap));
1084                 break;
1085         case PIO_KEYMAP:        /* set keyboard translation table */
1086 #ifndef KBD_DISABLE_KEYMAP_LOAD
1087                 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
1088                 bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
1089                 break;
1090 #else
1091                 lwkt_reltoken(&tty_token);
1092                 crit_exit();
1093                 return ENODEV;
1094 #endif
1095
1096         case GIO_KEYMAPENT:     /* get keyboard translation table entry */
1097                 keyp = (keyarg_t *)arg;
1098                 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
1099                                         /sizeof(kbd->kb_keymap->key[0])) {
1100                         lwkt_reltoken(&tty_token);
1101                         crit_exit();
1102                         return EINVAL;
1103                 }
1104                 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
1105                       sizeof(keyp->key));
1106                 break;
1107         case PIO_KEYMAPENT:     /* set keyboard translation table entry */
1108 #ifndef KBD_DISABLE_KEYMAP_LOAD
1109                 keyp = (keyarg_t *)arg;
1110                 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
1111                                         /sizeof(kbd->kb_keymap->key[0])) {
1112                         lwkt_reltoken(&tty_token);
1113                         crit_exit();
1114                         return EINVAL;
1115                 }
1116                 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
1117                       sizeof(keyp->key));
1118                 break;
1119 #else
1120                 lwkt_reltoken(&tty_token);
1121                 crit_exit();
1122                 return ENODEV;
1123 #endif
1124
1125         case GIO_DEADKEYMAP:    /* get accent key translation table */
1126                 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
1127                 break;
1128         case PIO_DEADKEYMAP:    /* set accent key translation table */
1129 #ifndef KBD_DISABLE_KEYMAP_LOAD
1130                 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
1131                 break;
1132 #else
1133                 lwkt_reltoken(&tty_token);
1134                 crit_exit();
1135                 return ENODEV;
1136 #endif
1137
1138         case GETFKEY:           /* get functionkey string */
1139                 fkeyp = (fkeyarg_t *)arg;
1140                 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
1141                         lwkt_reltoken(&tty_token);
1142                         crit_exit();
1143                         return EINVAL;
1144                 }
1145                 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
1146                       kbd->kb_fkeytab[fkeyp->keynum].len);
1147                 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
1148                 break;
1149         case SETFKEY:           /* set functionkey string */
1150 #ifndef KBD_DISABLE_KEYMAP_LOAD
1151                 fkeyp = (fkeyarg_t *)arg;
1152                 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
1153                         lwkt_reltoken(&tty_token);
1154                         crit_exit();
1155                         return EINVAL;
1156                 }
1157                 kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK);
1158                 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
1159                       kbd->kb_fkeytab[fkeyp->keynum].len);
1160                 break;
1161 #else
1162                 lwkt_reltoken(&tty_token);
1163                 crit_exit();
1164                 return ENODEV;
1165 #endif
1166
1167         default:
1168                 lwkt_reltoken(&tty_token);
1169                 crit_exit();
1170                 return ENOIOCTL;
1171         }
1172
1173         lwkt_reltoken(&tty_token);
1174         crit_exit();
1175         return 0;
1176 }
1177
1178 /* get a pointer to the string associated with the given function key */
1179 u_char *
1180 genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
1181 {
1182         u_char *ch;
1183
1184         if (kbd == NULL)
1185                 return NULL;
1186
1187         lwkt_gettoken(&tty_token);
1188         fkey -= F_FN;
1189         if (fkey > kbd->kb_fkeytab_size) {
1190                 lwkt_reltoken(&tty_token);
1191                 return NULL;
1192         }
1193         *len = kbd->kb_fkeytab[fkey].len;
1194         ch = kbd->kb_fkeytab[fkey].str;
1195
1196         lwkt_reltoken(&tty_token);
1197         return ch;
1198 }
1199
1200 /* diagnostic dump */
1201 static char *
1202 get_kbd_type_name(int type)
1203 {
1204         static struct {
1205                 int type;
1206                 char *name;
1207         } name_table[] = {
1208                 { KB_84,        "AT 84" },
1209                 { KB_101,       "AT 101/102" },
1210                 { KB_OTHER,     "generic" },
1211         };
1212         int i;
1213
1214         for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
1215                 if (type == name_table[i].type)
1216                         return name_table[i].name;
1217         }
1218         return "unknown";
1219 }
1220
1221 void
1222 genkbd_diag(keyboard_t *kbd, int level)
1223 {
1224         if (level > 0) {
1225                 kprintf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x", 
1226                        kbd->kb_index, kbd->kb_name, kbd->kb_unit,
1227                        get_kbd_type_name(kbd->kb_type), kbd->kb_type,
1228                        kbd->kb_config, kbd->kb_flags);
1229                 if (kbd->kb_io_base > 0)
1230                         kprintf(", port:0x%x-0x%x", kbd->kb_io_base, 
1231                                kbd->kb_io_base + kbd->kb_io_size - 1);
1232                 kprintf("\n");
1233         }
1234 }
1235
1236 #define set_lockkey_state(k, s, l)                              \
1237         if (!((s) & l ## DOWN)) {                               \
1238                 int i;                                          \
1239                 (s) |= l ## DOWN;                               \
1240                 (s) ^= l ## ED;                                 \
1241                 i = (s) & LOCK_MASK;                            \
1242                 kbd_ioctl((k), KDSETLED, (caddr_t)&i); \
1243         }
1244
1245 static u_int
1246 save_accent_key(keyboard_t *kbd, u_int key, int *accents)
1247 {
1248         int i;
1249
1250         lwkt_gettoken(&tty_token);
1251         /* make an index into the accent map */
1252         i = key - F_ACC + 1;
1253         if ((i > kbd->kb_accentmap->n_accs)
1254             || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
1255                 /* the index is out of range or pointing to an empty entry */
1256                 *accents = 0;
1257                 lwkt_reltoken(&tty_token);
1258                 return ERRKEY;
1259         }
1260
1261         /* 
1262          * If the same accent key has been hit twice, produce the accent char
1263          * itself.
1264          */
1265         if (i == *accents) {
1266                 key = kbd->kb_accentmap->acc[i - 1].accchar;
1267                 *accents = 0;
1268                 lwkt_reltoken(&tty_token);
1269                 return key;
1270         }
1271
1272         /* remember the index and wait for the next key  */
1273         *accents = i; 
1274         lwkt_reltoken(&tty_token);
1275         return NOKEY;
1276 }
1277
1278 static u_int
1279 make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
1280 {
1281         struct acc_t *acc;
1282         int i;
1283
1284         lwkt_gettoken(&tty_token);
1285         acc = &kbd->kb_accentmap->acc[*accents - 1];
1286         *accents = 0;
1287
1288         /* 
1289          * If the accent key is followed by the space key,
1290          * produce the accent char itself.
1291          */
1292         if (ch == ' ') {
1293                 lwkt_reltoken(&tty_token);
1294                 return acc->accchar;
1295         }
1296
1297         /* scan the accent map */
1298         for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1299                 if (acc->map[i][0] == 0)        /* end of table */
1300                         break;
1301                 if (acc->map[i][0] == ch)
1302                         return acc->map[i][1];
1303         }
1304         lwkt_reltoken(&tty_token);
1305         /* this char cannot be accented... */
1306         return ERRKEY;
1307 }
1308
1309 int
1310 genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1311                  int *accents)
1312 {
1313         struct keyent_t *key;
1314         int state = *shiftstate;
1315         int action;
1316         int f;
1317         int i;
1318
1319         lwkt_gettoken(&tty_token);
1320         i = keycode;
1321         f = state & (AGRS | ALKED);
1322         if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1323                 i += ALTGR_OFFSET;
1324         key = &kbd->kb_keymap->key[i];
1325         i = ((state & SHIFTS) ? 1 : 0)
1326             | ((state & CTLS) ? 2 : 0)
1327             | ((state & ALTS) ? 4 : 0);
1328         if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1329                 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1330                 i ^= 1;
1331
1332         if (up) {       /* break: key released */
1333                 action = kbd->kb_lastact[keycode];
1334                 kbd->kb_lastact[keycode] = NOP;
1335                 switch (action) {
1336                 case LSHA:
1337                         if (state & SHIFTAON) {
1338                                 set_lockkey_state(kbd, state, ALK);
1339                                 state &= ~ALKDOWN;
1340                         }
1341                         action = LSH;
1342                         /* FALL THROUGH */
1343                 case LSH:
1344                         state &= ~SHIFTS1;
1345                         break;
1346                 case RSHA:
1347                         if (state & SHIFTAON) {
1348                                 set_lockkey_state(kbd, state, ALK);
1349                                 state &= ~ALKDOWN;
1350                         }
1351                         action = RSH;
1352                         /* FALL THROUGH */
1353                 case RSH:
1354                         state &= ~SHIFTS2;
1355                         break;
1356                 case LCTRA:
1357                         if (state & SHIFTAON) {
1358                                 set_lockkey_state(kbd, state, ALK);
1359                                 state &= ~ALKDOWN;
1360                         }
1361                         action = LCTR;
1362                         /* FALL THROUGH */
1363                 case LCTR:
1364                         state &= ~CTLS1;
1365                         break;
1366                 case RCTRA:
1367                         if (state & SHIFTAON) {
1368                                 set_lockkey_state(kbd, state, ALK);
1369                                 state &= ~ALKDOWN;
1370                         }
1371                         action = RCTR;
1372                         /* FALL THROUGH */
1373                 case RCTR:
1374                         state &= ~CTLS2;
1375                         break;
1376                 case LALTA:
1377                         if (state & SHIFTAON) {
1378                                 set_lockkey_state(kbd, state, ALK);
1379                                 state &= ~ALKDOWN;
1380                         }
1381                         action = LALT;
1382                         /* FALL THROUGH */
1383                 case LALT:
1384                         state &= ~ALTS1;
1385                         break;
1386                 case RALTA:
1387                         if (state & SHIFTAON) {
1388                                 set_lockkey_state(kbd, state, ALK);
1389                                 state &= ~ALKDOWN;
1390                         }
1391                         action = RALT;
1392                         /* FALL THROUGH */
1393                 case RALT:
1394                         state &= ~ALTS2;
1395                         break;
1396                 case ASH:
1397                         state &= ~AGRS1;
1398                         break;
1399                 case META:
1400                         state &= ~METAS1;
1401                         break;
1402                 case NLK:
1403                         state &= ~NLKDOWN;
1404                         break;
1405                 case CLK:
1406                         state &= ~CLKDOWN;
1407                         break;
1408                 case SLK:
1409                         state &= ~SLKDOWN;
1410                         break;
1411                 case ALK:
1412                         state &= ~ALKDOWN;
1413                         break;
1414                 case NOP:
1415                         /* release events of regular keys are not reported */
1416                         *shiftstate &= ~SHIFTAON;
1417                         lwkt_reltoken(&tty_token);
1418                         return NOKEY;
1419                 }
1420                 *shiftstate = state & ~SHIFTAON;
1421                 lwkt_reltoken(&tty_token);
1422                 return (SPCLKEY | RELKEY | action);
1423         } else {        /* make: key pressed */
1424                 action = key->map[i];
1425                 state &= ~SHIFTAON;
1426                 if (key->spcl & (0x80 >> i)) {
1427                         /* special keys */
1428                         if (kbd->kb_lastact[keycode] == NOP)
1429                                 kbd->kb_lastact[keycode] = action;
1430                         if (kbd->kb_lastact[keycode] != action)
1431                                 action = NOP;
1432                         switch (action) {
1433                         /* LOCKING KEYS */
1434                         case NLK:
1435                                 set_lockkey_state(kbd, state, NLK);
1436                                 break;
1437                         case CLK:
1438                                 set_lockkey_state(kbd, state, CLK);
1439                                 break;
1440                         case SLK:
1441                                 set_lockkey_state(kbd, state, SLK);
1442                                 break;
1443                         case ALK:
1444                                 set_lockkey_state(kbd, state, ALK);
1445                                 break;
1446                         /* NON-LOCKING KEYS */
1447                         case SPSC: case RBT:  case SUSP: case STBY:
1448                         case DBG:  case NEXT: case PREV: case PNC:
1449                         case HALT: case PDWN:
1450                                 *accents = 0;
1451                                 break;
1452                         case BTAB:
1453                                 *accents = 0;
1454                                 action |= BKEY;
1455                                 break;
1456                         case LSHA:
1457                                 state |= SHIFTAON;
1458                                 action = LSH;
1459                                 /* FALL THROUGH */
1460                         case LSH:
1461                                 state |= SHIFTS1;
1462                                 break;
1463                         case RSHA:
1464                                 state |= SHIFTAON;
1465                                 action = RSH;
1466                                 /* FALL THROUGH */
1467                         case RSH:
1468                                 state |= SHIFTS2;
1469                                 break;
1470                         case LCTRA:
1471                                 state |= SHIFTAON;
1472                                 action = LCTR;
1473                                 /* FALL THROUGH */
1474                         case LCTR:
1475                                 state |= CTLS1;
1476                                 break;
1477                         case RCTRA:
1478                                 state |= SHIFTAON;
1479                                 action = RCTR;
1480                                 /* FALL THROUGH */
1481                         case RCTR:
1482                                 state |= CTLS2;
1483                                 break;
1484                         case LALTA:
1485                                 state |= SHIFTAON;
1486                                 action = LALT;
1487                                 /* FALL THROUGH */
1488                         case LALT:
1489                                 state |= ALTS1;
1490                                 break;
1491                         case RALTA:
1492                                 state |= SHIFTAON;
1493                                 action = RALT;
1494                                 /* FALL THROUGH */
1495                         case RALT:
1496                                 state |= ALTS2;
1497                                 break;
1498                         case ASH:
1499                                 state |= AGRS1;
1500                                 break;
1501                         case META:
1502                                 state |= METAS1;
1503                                 break;
1504                         case NOP:
1505                                 *shiftstate = state;
1506                                 lwkt_reltoken(&tty_token);
1507                                 return NOKEY;
1508                         default:
1509                                 /* is this an accent (dead) key? */
1510                                 *shiftstate = state;
1511                                 if (action >= F_ACC && action <= L_ACC) {
1512                                         action = save_accent_key(kbd, action,
1513                                                                  accents);
1514                                         switch (action) {
1515                                         case NOKEY:
1516                                         case ERRKEY:
1517                                                 lwkt_reltoken(&tty_token);
1518                                                 return action;
1519                                         default:
1520                                                 if (state & METAS) {
1521                                                         lwkt_reltoken(&tty_token);
1522                                                         return (action | MKEY);
1523                                                 } else { 
1524                                                         lwkt_reltoken(&tty_token);
1525                                                         return action;
1526                                                 }
1527                                         }
1528                                         /* NOT REACHED */
1529                                 }
1530                                 /* other special keys */
1531                                 if (*accents > 0) {
1532                                         *accents = 0;
1533                                         lwkt_reltoken(&tty_token);
1534                                         return ERRKEY;
1535                                 }
1536                                 if (action >= F_FN && action <= L_FN)
1537                                         action |= FKEY;
1538                                 /* XXX: return fkey string for the FKEY? */
1539                                 lwkt_reltoken(&tty_token);
1540                                 return (SPCLKEY | action);
1541                         }
1542                         *shiftstate = state;
1543                         lwkt_reltoken(&tty_token);
1544                         return (SPCLKEY | action);
1545                 } else {
1546                         /* regular keys */
1547                         kbd->kb_lastact[keycode] = NOP;
1548                         *shiftstate = state;
1549                         if (*accents > 0) {
1550                                 /* make an accented char */
1551                                 action = make_accent_char(kbd, action, accents);
1552                                 if (action == ERRKEY) {
1553                                         lwkt_reltoken(&tty_token);
1554                                         return action;
1555                                 }
1556                         }
1557                         if (state & METAS)
1558                                 action |= MKEY;
1559                         lwkt_reltoken(&tty_token);
1560                         return action;
1561                 }
1562         }
1563         /* NOT REACHED */
1564         lwkt_reltoken(&tty_token);
1565 }