xref: /freebsd/sys/dev/kbd/kbd.c (revision aa0a1e58)
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 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 #ifndef KBD_DISABLE_KEYMAP_LOAD
841 	keymap_t *mapp;
842 #endif
843 	keyarg_t *keyp;
844 	fkeyarg_t *fkeyp;
845 	int s;
846 	int i;
847 	int error;
848 
849 	s = spltty();
850 	switch (cmd) {
851 
852 	case KDGKBINFO:		/* get keyboard information */
853 		((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
854 		i = imin(strlen(kbd->kb_name) + 1,
855 		    sizeof(((keyboard_info_t *)arg)->kb_name));
856 		bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
857 		((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
858 		((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
859 		((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
860 		((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
861 		break;
862 
863 	case KDGKBTYPE:		/* get keyboard type */
864 		*(int *)arg = kbd->kb_type;
865 		break;
866 
867 	case KDGETREPEAT:	/* get keyboard repeat rate */
868 		((int *)arg)[0] = kbd->kb_delay1;
869 		((int *)arg)[1] = kbd->kb_delay2;
870 		break;
871 
872 	case GIO_KEYMAP:	/* get keyboard translation table */
873 		error = copyout(kbd->kb_keymap, *(void **)arg,
874 		    sizeof(keymap_t));
875 		splx(s);
876 		return (error);
877 	case PIO_KEYMAP:	/* set keyboard translation table */
878 #ifndef KBD_DISABLE_KEYMAP_LOAD
879 		mapp = malloc(sizeof *mapp, M_TEMP, M_NOWAIT);
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 		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 = imin(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 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 < sizeof(name_table)/sizeof(name_table[0]); ++i) {
1124 		if (type == name_table[i].type)
1125 			return (name_table[i].name);
1126 	}
1127 	return ("unknown");
1128 }
1129 
1130 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 #ifndef PC98
1307 			state &= ~CLKDOWN;
1308 #else
1309 			state &= ~CLKED;
1310 			i = state & LOCK_MASK;
1311 			(void)kbdd_ioctl(kbd, KDSETLED, (caddr_t)&i);
1312 #endif
1313 			break;
1314 		case SLK:
1315 			state &= ~SLKDOWN;
1316 			break;
1317 		case ALK:
1318 			state &= ~ALKDOWN;
1319 			break;
1320 		case NOP:
1321 			/* release events of regular keys are not reported */
1322 			*shiftstate &= ~SHIFTAON;
1323 			return (NOKEY);
1324 		}
1325 		*shiftstate = state & ~SHIFTAON;
1326 		return (SPCLKEY | RELKEY | action);
1327 	} else {	/* make: key pressed */
1328 		action = key->map[i];
1329 		state &= ~SHIFTAON;
1330 		if (key->spcl & (0x80 >> i)) {
1331 			/* special keys */
1332 			if (kbd->kb_lastact[keycode] == NOP)
1333 				kbd->kb_lastact[keycode] = action;
1334 			if (kbd->kb_lastact[keycode] != action)
1335 				action = NOP;
1336 			switch (action) {
1337 			/* LOCKING KEYS */
1338 			case NLK:
1339 				set_lockkey_state(kbd, state, NLK);
1340 				break;
1341 			case CLK:
1342 #ifndef PC98
1343 				set_lockkey_state(kbd, state, CLK);
1344 #else
1345 				state |= CLKED;
1346 				i = state & LOCK_MASK;
1347 				(void)kbdd_ioctl(kbd, KDSETLED, (caddr_t)&i);
1348 #endif
1349 				break;
1350 			case SLK:
1351 				set_lockkey_state(kbd, state, SLK);
1352 				break;
1353 			case ALK:
1354 				set_lockkey_state(kbd, state, ALK);
1355 				break;
1356 			/* NON-LOCKING KEYS */
1357 			case SPSC: case RBT:  case SUSP: case STBY:
1358 			case DBG:  case NEXT: case PREV: case PNC:
1359 			case HALT: case PDWN:
1360 				*accents = 0;
1361 				break;
1362 			case BTAB:
1363 				*accents = 0;
1364 				action |= BKEY;
1365 				break;
1366 			case LSHA:
1367 				state |= SHIFTAON;
1368 				action = LSH;
1369 				/* FALL THROUGH */
1370 			case LSH:
1371 				state |= SHIFTS1;
1372 				break;
1373 			case RSHA:
1374 				state |= SHIFTAON;
1375 				action = RSH;
1376 				/* FALL THROUGH */
1377 			case RSH:
1378 				state |= SHIFTS2;
1379 				break;
1380 			case LCTRA:
1381 				state |= SHIFTAON;
1382 				action = LCTR;
1383 				/* FALL THROUGH */
1384 			case LCTR:
1385 				state |= CTLS1;
1386 				break;
1387 			case RCTRA:
1388 				state |= SHIFTAON;
1389 				action = RCTR;
1390 				/* FALL THROUGH */
1391 			case RCTR:
1392 				state |= CTLS2;
1393 				break;
1394 			case LALTA:
1395 				state |= SHIFTAON;
1396 				action = LALT;
1397 				/* FALL THROUGH */
1398 			case LALT:
1399 				state |= ALTS1;
1400 				break;
1401 			case RALTA:
1402 				state |= SHIFTAON;
1403 				action = RALT;
1404 				/* FALL THROUGH */
1405 			case RALT:
1406 				state |= ALTS2;
1407 				break;
1408 			case ASH:
1409 				state |= AGRS1;
1410 				break;
1411 			case META:
1412 				state |= METAS1;
1413 				break;
1414 			case NOP:
1415 				*shiftstate = state;
1416 				return (NOKEY);
1417 			default:
1418 				/* is this an accent (dead) key? */
1419 				*shiftstate = state;
1420 				if (action >= F_ACC && action <= L_ACC) {
1421 					action = save_accent_key(kbd, action,
1422 								 accents);
1423 					switch (action) {
1424 					case NOKEY:
1425 					case ERRKEY:
1426 						return (action);
1427 					default:
1428 						if (state & METAS)
1429 							return (action | MKEY);
1430 						else
1431 							return (action);
1432 					}
1433 					/* NOT REACHED */
1434 				}
1435 				/* other special keys */
1436 				if (*accents > 0) {
1437 					*accents = 0;
1438 					return (ERRKEY);
1439 				}
1440 				if (action >= F_FN && action <= L_FN)
1441 					action |= FKEY;
1442 				/* XXX: return fkey string for the FKEY? */
1443 				return (SPCLKEY | action);
1444 			}
1445 			*shiftstate = state;
1446 			return (SPCLKEY | action);
1447 		} else {
1448 			/* regular keys */
1449 			kbd->kb_lastact[keycode] = NOP;
1450 			*shiftstate = state;
1451 			if (*accents > 0) {
1452 				/* make an accented char */
1453 				action = make_accent_char(kbd, action, accents);
1454 				if (action == ERRKEY)
1455 					return (action);
1456 			}
1457 			if (state & METAS)
1458 				action |= MKEY;
1459 			return (action);
1460 		}
1461 	}
1462 	/* NOT REACHED */
1463 }
1464