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