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