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