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