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