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