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