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