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