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