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