xref: /freebsd/sys/dev/kbd/kbd.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
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/proc.h>
37 #include <sys/tty.h>
38 #include <sys/poll.h>
39 #include <sys/vnode.h>
40 #include <sys/uio.h>
41 
42 #include <machine/console.h>
43 
44 #include <dev/kbd/kbdreg.h>
45 
46 #define KBD_INDEX(dev)	minor(dev)
47 
48 typedef struct genkbd_softc {
49 	int		gkb_flags;	/* flag/status bits */
50 #define KB_ASLEEP	(1 << 0)
51 	struct clist	gkb_q;		/* input queue */
52 	struct selinfo	gkb_rsel;
53 } genkbd_softc_t;
54 
55 static	SLIST_HEAD(, keyboard_driver) keyboard_drivers =
56  	SLIST_HEAD_INITIALIZER(keyboard_drivers);
57 
58 /* local arrays */
59 
60 /*
61  * We need at least one entry each in order to initialize a keyboard
62  * for the kernel console.  The arrays will be increased dynamically
63  * when necessary.
64  */
65 
66 static int		keyboards = 1;
67 static keyboard_t	*kbd_ini;
68 static keyboard_t	**keyboard = &kbd_ini;
69 static keyboard_switch_t *kbdsw_ini;
70        keyboard_switch_t **kbdsw = &kbdsw_ini;
71 
72 #define ARRAY_DELTA	4
73 
74 static int
75 kbd_realloc_array(void)
76 {
77 	keyboard_t **new_kbd;
78 	keyboard_switch_t **new_kbdsw;
79 	int newsize;
80 	int s;
81 
82 	s = spltty();
83 	newsize = ((keyboards + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
84 	new_kbd = malloc(sizeof(*new_kbd)*newsize, M_DEVBUF, M_NOWAIT);
85 	if (new_kbd == NULL) {
86 		splx(s);
87 		return ENOMEM;
88 	}
89 	new_kbdsw = malloc(sizeof(*new_kbdsw)*newsize, M_DEVBUF, M_NOWAIT);
90 	if (new_kbdsw == NULL) {
91 		free(new_kbd, M_DEVBUF);
92 		splx(s);
93 		return ENOMEM;
94 	}
95 	bzero(new_kbd, sizeof(*new_kbd)*newsize);
96 	bzero(new_kbdsw, sizeof(*new_kbdsw)*newsize);
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 	list = (const keyboard_driver_t **)kbddriver_set.ls_items;
205 	while ((p = *list++) != NULL) {
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 	list = (const keyboard_driver_t **)kbddriver_set.ls_items;
260 	while ((p = *list++) != NULL) {
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 	list = (const keyboard_driver_t **)kbddriver_set.ls_items;
399 	while ((p = *list++) != NULL) {
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 	/* bmaj */	-1
441 };
442 
443 int
444 kbd_attach(keyboard_t *kbd)
445 {
446 	dev_t dev;
447 
448 	if (kbd->kb_index >= keyboards)
449 		return EINVAL;
450 	if (keyboard[kbd->kb_index] != kbd)
451 		return EINVAL;
452 
453 	dev = make_dev(&kbd_cdevsw, kbd->kb_index, UID_ROOT, GID_WHEEL, 0600,
454 		       "kbd%r", kbd->kb_index);
455 	if (dev->si_drv1 == NULL)
456 		dev->si_drv1 = malloc(sizeof(genkbd_softc_t), M_DEVBUF,
457 				      M_WAITOK);
458 	bzero(dev->si_drv1, sizeof(genkbd_softc_t));
459 
460 	printf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
461 	return 0;
462 }
463 
464 int
465 kbd_detach(keyboard_t *kbd)
466 {
467 	dev_t dev;
468 
469 	if (kbd->kb_index >= keyboards)
470 		return EINVAL;
471 	if (keyboard[kbd->kb_index] != kbd)
472 		return EINVAL;
473 
474 	dev = makedev(kbd_cdevsw.d_maj, kbd->kb_index);
475 	if (dev->si_drv1)
476 		free(dev->si_drv1, M_DEVBUF);
477 	destroy_dev(dev);
478 
479 	return 0;
480 }
481 
482 /*
483  * Generic keyboard cdev driver functions
484  * Keyboard subdrivers may call these functions to implement common
485  * driver functions.
486  */
487 
488 #define KB_QSIZE	512
489 #define KB_BUFSIZE	64
490 
491 static kbd_callback_func_t genkbd_event;
492 
493 static int
494 genkbdopen(dev_t dev, int mode, int flag, struct proc *p)
495 {
496 	keyboard_t *kbd;
497 	genkbd_softc_t *sc;
498 	int s;
499 	int i;
500 
501 	s = spltty();
502 	sc = dev->si_drv1;
503 	kbd = kbd_get_keyboard(KBD_INDEX(dev));
504 	if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
505 		splx(s);
506 		return ENXIO;
507 	}
508 	i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
509 			 genkbd_event, (void *)sc);
510 	if (i < 0) {
511 		splx(s);
512 		return EBUSY;
513 	}
514 	/* assert(i == kbd->kb_index) */
515 	/* assert(kbd == kbd_get_keyboard(i)) */
516 
517 	/*
518 	 * NOTE: even when we have successfully claimed a keyboard,
519 	 * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
520 	 */
521 
522 #if 0
523 	bzero(&sc->gkb_q, sizeof(sc->gkb_q));
524 #endif
525 	clist_alloc_cblocks(&sc->gkb_q, KB_QSIZE, KB_QSIZE/2); /* XXX */
526 	sc->gkb_rsel.si_flags = 0;
527 	sc->gkb_rsel.si_pid = 0;
528 	splx(s);
529 
530 	return 0;
531 }
532 
533 static int
534 genkbdclose(dev_t dev, int mode, int flag, struct proc *p)
535 {
536 	keyboard_t *kbd;
537 	genkbd_softc_t *sc;
538 	int s;
539 
540 	/*
541 	 * NOTE: the device may have already become invalid.
542 	 * kbd == NULL || !KBD_IS_VALID(kbd)
543 	 */
544 	s = spltty();
545 	sc = dev->si_drv1;
546 	kbd = kbd_get_keyboard(KBD_INDEX(dev));
547 	if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
548 		/* XXX: we shall be forgiving and don't report error... */
549 	} else {
550 		kbd_release(kbd, (void *)sc);
551 #if 0
552 		clist_free_cblocks(&sc->gkb_q);
553 #endif
554 	}
555 	splx(s);
556 	return 0;
557 }
558 
559 static int
560 genkbdread(dev_t dev, struct uio *uio, int flag)
561 {
562 	keyboard_t *kbd;
563 	genkbd_softc_t *sc;
564 	u_char buffer[KB_BUFSIZE];
565 	int len;
566 	int error;
567 	int s;
568 
569 	/* wait for input */
570 	s = spltty();
571 	sc = dev->si_drv1;
572 	kbd = kbd_get_keyboard(KBD_INDEX(dev));
573 	if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
574 		splx(s);
575 		return ENXIO;
576 	}
577 	while (sc->gkb_q.c_cc == 0) {
578 		if (flag & IO_NDELAY) {
579 			splx(s);
580 			return EWOULDBLOCK;
581 		}
582 		sc->gkb_flags |= KB_ASLEEP;
583 		error = tsleep((caddr_t)sc, PZERO | PCATCH, "kbdrea", 0);
584 		kbd = kbd_get_keyboard(KBD_INDEX(dev));
585 		if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
586 			splx(s);
587 			return ENXIO;	/* our keyboard has gone... */
588 		}
589 		if (error) {
590 			sc->gkb_flags &= ~KB_ASLEEP;
591 			splx(s);
592 			return error;
593 		}
594 	}
595 	splx(s);
596 
597 	/* copy as much input as possible */
598 	error = 0;
599 	while (uio->uio_resid > 0) {
600 		len = imin(uio->uio_resid, sizeof(buffer));
601 		len = q_to_b(&sc->gkb_q, buffer, len);
602 		if (len <= 0)
603 			break;
604 		error = uiomove(buffer, len, uio);
605 		if (error)
606 			break;
607 	}
608 
609 	return error;
610 }
611 
612 static int
613 genkbdwrite(dev_t dev, struct uio *uio, int flag)
614 {
615 	keyboard_t *kbd;
616 
617 	kbd = kbd_get_keyboard(KBD_INDEX(dev));
618 	if ((kbd == NULL) || !KBD_IS_VALID(kbd))
619 		return ENXIO;
620 	return ENODEV;
621 }
622 
623 static int
624 genkbdioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct proc *p)
625 {
626 	keyboard_t *kbd;
627 	int error;
628 
629 	kbd = kbd_get_keyboard(KBD_INDEX(dev));
630 	if ((kbd == NULL) || !KBD_IS_VALID(kbd))
631 		return ENXIO;
632 	error = (*kbdsw[kbd->kb_index]->ioctl)(kbd, cmd, arg);
633 	if (error == ENOIOCTL)
634 		error = ENODEV;
635 	return error;
636 }
637 
638 static int
639 genkbdpoll(dev_t dev, int events, struct proc *p)
640 {
641 	keyboard_t *kbd;
642 	genkbd_softc_t *sc;
643 	int revents;
644 	int s;
645 
646 	revents = 0;
647 	s = spltty();
648 	sc = dev->si_drv1;
649 	kbd = kbd_get_keyboard(KBD_INDEX(dev));
650 	if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
651 		revents =  POLLHUP;	/* the keyboard has gone */
652 	} else if (events & (POLLIN | POLLRDNORM)) {
653 		if (sc->gkb_q.c_cc > 0)
654 			revents = events & (POLLIN | POLLRDNORM);
655 		else
656 			selrecord(p, &sc->gkb_rsel);
657 	}
658 	splx(s);
659 	return revents;
660 }
661 
662 static int
663 genkbd_event(keyboard_t *kbd, int event, void *arg)
664 {
665 	genkbd_softc_t *sc;
666 	size_t len;
667 	u_char *cp;
668 	int mode;
669 	int c;
670 
671 	/* assert(KBD_IS_VALID(kbd)) */
672 	sc = (genkbd_softc_t *)arg;
673 
674 	switch (event) {
675 	case KBDIO_KEYINPUT:
676 		break;
677 	case KBDIO_UNLOADING:
678 		/* the keyboard is going... */
679 		kbd_release(kbd, (void *)sc);
680 		if (sc->gkb_flags & KB_ASLEEP) {
681 			sc->gkb_flags &= ~KB_ASLEEP;
682 			wakeup((caddr_t)sc);
683 		}
684 		selwakeup(&sc->gkb_rsel);
685 		return 0;
686 	default:
687 		return EINVAL;
688 	}
689 
690 	/* obtain the current key input mode */
691 	if ((*kbdsw[kbd->kb_index]->ioctl)(kbd, KDGKBMODE, (caddr_t)&mode))
692 		mode = K_XLATE;
693 
694 	/* read all pending input */
695 	while ((*kbdsw[kbd->kb_index]->check_char)(kbd)) {
696 		c = (*kbdsw[kbd->kb_index]->read_char)(kbd, FALSE);
697 		if (c == NOKEY)
698 			continue;
699 		if (c == ERRKEY)	/* XXX: ring bell? */
700 			continue;
701 		if (!KBD_IS_BUSY(kbd))
702 			/* the device is not open, discard the input */
703 			continue;
704 
705 		/* store the byte as is for K_RAW and K_CODE modes */
706 		if (mode != K_XLATE) {
707 			putc(KEYCHAR(c), &sc->gkb_q);
708 			continue;
709 		}
710 
711 		/* K_XLATE */
712 		if (c & RELKEY)	/* key release is ignored */
713 			continue;
714 
715 		/* process special keys; most of them are just ignored... */
716 		if (c & SPCLKEY) {
717 			switch (KEYCHAR(c)) {
718 			default:
719 				/* ignore them... */
720 				continue;
721 			case BTAB:	/* a backtab: ESC [ Z */
722 				putc(0x1b, &sc->gkb_q);
723 				putc('[', &sc->gkb_q);
724 				putc('Z', &sc->gkb_q);
725 				continue;
726 			}
727 		}
728 
729 		/* normal chars, normal chars with the META, function keys */
730 		switch (KEYFLAGS(c)) {
731 		case 0:			/* a normal char */
732 			putc(KEYCHAR(c), &sc->gkb_q);
733 			break;
734 		case MKEY:		/* the META flag: prepend ESC */
735 			putc(0x1b, &sc->gkb_q);
736 			putc(KEYCHAR(c), &sc->gkb_q);
737 			break;
738 		case FKEY | SPCLKEY:	/* a function key, return string */
739 			cp = (*kbdsw[kbd->kb_index]->get_fkeystr)(kbd,
740 							KEYCHAR(c), &len);
741 			if (cp != NULL) {
742 				while (len-- >  0)
743 					putc(*cp++, &sc->gkb_q);
744 			}
745 			break;
746 		}
747 	}
748 
749 	/* wake up sleeping/polling processes */
750 	if (sc->gkb_q.c_cc > 0) {
751 		if (sc->gkb_flags & KB_ASLEEP) {
752 			sc->gkb_flags &= ~KB_ASLEEP;
753 			wakeup((caddr_t)sc);
754 		}
755 		selwakeup(&sc->gkb_rsel);
756 	}
757 
758 	return 0;
759 }
760 
761 #endif /* KBD_INSTALL_CDEV */
762 
763 /*
764  * Generic low-level keyboard functions
765  * The low-level functions in the keyboard subdriver may use these
766  * functions.
767  */
768 
769 int
770 genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
771 {
772 	keyarg_t *keyp;
773 	fkeyarg_t *fkeyp;
774 	int s;
775 	int i;
776 
777 	s = spltty();
778 	switch (cmd) {
779 
780 	case KDGKBINFO:		/* get keyboard information */
781 		((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
782 		i = imin(strlen(kbd->kb_name) + 1,
783 			 sizeof(((keyboard_info_t *)arg)->kb_name));
784 		bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
785 		((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
786 		((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
787 		((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
788 		((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
789 		break;
790 
791 	case KDGKBTYPE:		/* get keyboard type */
792 		*(int *)arg = kbd->kb_type;
793 		break;
794 
795 	case KDGETREPEAT:	/* get keyboard repeat rate */
796 		((int *)arg)[0] = kbd->kb_delay1;
797 		((int *)arg)[1] = kbd->kb_delay2;
798 		break;
799 
800 	case GIO_KEYMAP:	/* get keyboard translation table */
801 		bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap));
802 		break;
803 	case PIO_KEYMAP:	/* set keyboard translation table */
804 #ifndef KBD_DISABLE_KEYMAP_LOAD
805 		bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
806 		bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
807 		break;
808 #else
809 		splx(s);
810 		return ENODEV;
811 #endif
812 
813 	case GIO_KEYMAPENT:	/* get keyboard translation table entry */
814 		keyp = (keyarg_t *)arg;
815 		if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
816 					/sizeof(kbd->kb_keymap->key[0])) {
817 			splx(s);
818 			return EINVAL;
819 		}
820 		bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
821 		      sizeof(keyp->key));
822 		break;
823 	case PIO_KEYMAPENT:	/* set keyboard translation table entry */
824 #ifndef KBD_DISABLE_KEYMAP_LOAD
825 		keyp = (keyarg_t *)arg;
826 		if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
827 					/sizeof(kbd->kb_keymap->key[0])) {
828 			splx(s);
829 			return EINVAL;
830 		}
831 		bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
832 		      sizeof(keyp->key));
833 		break;
834 #else
835 		splx(s);
836 		return ENODEV;
837 #endif
838 
839 	case GIO_DEADKEYMAP:	/* get accent key translation table */
840 		bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
841 		break;
842 	case PIO_DEADKEYMAP:	/* set accent key translation table */
843 #ifndef KBD_DISABLE_KEYMAP_LOAD
844 		bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
845 		break;
846 #else
847 		splx(s);
848 		return ENODEV;
849 #endif
850 
851 	case GETFKEY:		/* get functionkey string */
852 		fkeyp = (fkeyarg_t *)arg;
853 		if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
854 			splx(s);
855 			return EINVAL;
856 		}
857 		bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
858 		      kbd->kb_fkeytab[fkeyp->keynum].len);
859 		fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
860 		break;
861 	case SETFKEY:		/* set functionkey string */
862 #ifndef KBD_DISABLE_KEYMAP_LOAD
863 		fkeyp = (fkeyarg_t *)arg;
864 		if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
865 			splx(s);
866 			return EINVAL;
867 		}
868 		kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK);
869 		bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
870 		      kbd->kb_fkeytab[fkeyp->keynum].len);
871 		break;
872 #else
873 		splx(s);
874 		return ENODEV;
875 #endif
876 
877 	default:
878 		splx(s);
879 		return ENOIOCTL;
880 	}
881 
882 	splx(s);
883 	return 0;
884 }
885 
886 /* get a pointer to the string associated with the given function key */
887 u_char
888 *genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
889 {
890 	if (kbd == NULL)
891 		return NULL;
892 	fkey -= F_FN;
893 	if (fkey > kbd->kb_fkeytab_size)
894 		return NULL;
895 	*len = kbd->kb_fkeytab[fkey].len;
896 	return kbd->kb_fkeytab[fkey].str;
897 }
898 
899 /* diagnostic dump */
900 static char
901 *get_kbd_type_name(int type)
902 {
903 	static struct {
904 		int type;
905 		char *name;
906 	} name_table[] = {
907 		{ KB_84,	"AT 84" },
908 		{ KB_101,	"AT 101/102" },
909 		{ KB_OTHER,	"generic" },
910 	};
911 	int i;
912 
913 	for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
914 		if (type == name_table[i].type)
915 			return name_table[i].name;
916 	}
917 	return "unknown";
918 }
919 
920 void
921 genkbd_diag(keyboard_t *kbd, int level)
922 {
923 	if (level > 0) {
924 		printf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
925 		       kbd->kb_index, kbd->kb_name, kbd->kb_unit,
926 		       get_kbd_type_name(kbd->kb_type), kbd->kb_type,
927 		       kbd->kb_config, kbd->kb_flags);
928 		if (kbd->kb_io_base > 0)
929 			printf(", port:0x%x-0x%x", kbd->kb_io_base,
930 			       kbd->kb_io_base + kbd->kb_io_size - 1);
931 		printf("\n");
932 	}
933 }
934 
935 #define set_lockkey_state(k, s, l)				\
936 	if (!((s) & l ## DOWN)) {				\
937 		int i;						\
938 		(s) |= l ## DOWN;				\
939 		(s) ^= l ## ED;					\
940 		i = (s) & LOCK_MASK;				\
941 		(*kbdsw[(k)->kb_index]->ioctl)((k), KDSETLED, (caddr_t)&i); \
942 	}
943 
944 static u_int
945 save_accent_key(keyboard_t *kbd, u_int key, int *accents)
946 {
947 	int i;
948 
949 	/* make an index into the accent map */
950 	i = key - F_ACC + 1;
951 	if ((i > kbd->kb_accentmap->n_accs)
952 	    || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
953 		/* the index is out of range or pointing to an empty entry */
954 		*accents = 0;
955 		return ERRKEY;
956 	}
957 
958 	/*
959 	 * If the same accent key has been hit twice, produce the accent char
960 	 * itself.
961 	 */
962 	if (i == *accents) {
963 		key = kbd->kb_accentmap->acc[i - 1].accchar;
964 		*accents = 0;
965 		return key;
966 	}
967 
968 	/* remember the index and wait for the next key  */
969 	*accents = i;
970 	return NOKEY;
971 }
972 
973 static u_int
974 make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
975 {
976 	struct acc_t *acc;
977 	int i;
978 
979 	acc = &kbd->kb_accentmap->acc[*accents - 1];
980 	*accents = 0;
981 
982 	/*
983 	 * If the accent key is followed by the space key,
984 	 * produce the accent char itself.
985 	 */
986 	if (ch == ' ')
987 		return acc->accchar;
988 
989 	/* scan the accent map */
990 	for (i = 0; i < NUM_ACCENTCHARS; ++i) {
991 		if (acc->map[i][0] == 0)	/* end of table */
992 			break;
993 		if (acc->map[i][0] == ch)
994 			return acc->map[i][1];
995 	}
996 	/* this char cannot be accented... */
997 	return ERRKEY;
998 }
999 
1000 int
1001 genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1002 		 int *accents)
1003 {
1004 	struct keyent_t *key;
1005 	int state = *shiftstate;
1006 	int action;
1007 	int f;
1008 	int i;
1009 
1010 	i = keycode;
1011 	f = state & (AGRS | ALKED);
1012 	if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1013 		i += ALTGR_OFFSET;
1014 	key = &kbd->kb_keymap->key[i];
1015 	i = ((state & SHIFTS) ? 1 : 0)
1016 	    | ((state & CTLS) ? 2 : 0)
1017 	    | ((state & ALTS) ? 4 : 0);
1018 	if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1019 		|| ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1020 		i ^= 1;
1021 
1022 	action = key->map[i];
1023 	if (up) {	/* break: key released */
1024 		if (key->spcl & (0x80 >> i)) {
1025 			/* special keys */
1026 			switch (action) {
1027 			case LSHA:
1028 				if (state & SHIFTAON) {
1029 					set_lockkey_state(kbd, state, ALK);
1030 					state &= ~ALKDOWN;
1031 				}
1032 				action = LSH;
1033 				/* FALL THROUGH */
1034 			case LSH:
1035 				state &= ~SHIFTS1;
1036 				break;
1037 			case RSHA:
1038 				if (state & SHIFTAON) {
1039 					set_lockkey_state(kbd, state, ALK);
1040 					state &= ~ALKDOWN;
1041 				}
1042 				action = RSH;
1043 				/* FALL THROUGH */
1044 			case RSH:
1045 				state &= ~SHIFTS2;
1046 				break;
1047 			case LCTRA:
1048 				if (state & SHIFTAON) {
1049 					set_lockkey_state(kbd, state, ALK);
1050 					state &= ~ALKDOWN;
1051 				}
1052 				action = LCTR;
1053 				/* FALL THROUGH */
1054 			case LCTR:
1055 				state &= ~CTLS1;
1056 				break;
1057 			case RCTRA:
1058 				if (state & SHIFTAON) {
1059 					set_lockkey_state(kbd, state, ALK);
1060 					state &= ~ALKDOWN;
1061 				}
1062 				action = RCTR;
1063 				/* FALL THROUGH */
1064 			case RCTR:
1065 				state &= ~CTLS2;
1066 				break;
1067 			case LALTA:
1068 				if (state & SHIFTAON) {
1069 					set_lockkey_state(kbd, state, ALK);
1070 					state &= ~ALKDOWN;
1071 				}
1072 				action = LALT;
1073 				/* FALL THROUGH */
1074 			case LALT:
1075 				state &= ~ALTS1;
1076 				break;
1077 			case RALTA:
1078 				if (state & SHIFTAON) {
1079 					set_lockkey_state(kbd, state, ALK);
1080 					state &= ~ALKDOWN;
1081 				}
1082 				action = RALT;
1083 				/* FALL THROUGH */
1084 			case RALT:
1085 				state &= ~ALTS2;
1086 				break;
1087 			case ASH:
1088 				state &= ~AGRS1;
1089 				break;
1090 			case META:
1091 				state &= ~METAS1;
1092 				break;
1093 			case NLK:
1094 				state &= ~NLKDOWN;
1095 				break;
1096 			case CLK:
1097 #ifndef PC98
1098 				state &= ~CLKDOWN;
1099 #else
1100 				state &= ~CLKED;
1101 				i = state & LOCK_MASK;
1102 				(*kbdsw[kbd->kb_index]->ioctl)(kbd, KDSETLED,
1103 							       (caddr_t)&i);
1104 #endif
1105 				break;
1106 			case SLK:
1107 				state &= ~SLKDOWN;
1108 				break;
1109 			case ALK:
1110 				state &= ~ALKDOWN;
1111 				break;
1112 			}
1113 			*shiftstate = state & ~SHIFTAON;
1114 			return (SPCLKEY | RELKEY | action);
1115 		}
1116 		/* release events of regular keys are not reported */
1117 		*shiftstate &= ~SHIFTAON;
1118 		return NOKEY;
1119 	} else {	/* make: key pressed */
1120 		state &= ~SHIFTAON;
1121 		if (key->spcl & (0x80 >> i)) {
1122 			/* special keys */
1123 			switch (action) {
1124 			/* LOCKING KEYS */
1125 			case NLK:
1126 				set_lockkey_state(kbd, state, NLK);
1127 				break;
1128 			case CLK:
1129 #ifndef PC98
1130 				set_lockkey_state(kbd, state, CLK);
1131 #else
1132 				state |= CLKED;
1133 				i = state & LOCK_MASK;
1134 				(*kbdsw[kbd->kb_index]->ioctl)(kbd, KDSETLED,
1135 							       (caddr_t)&i);
1136 #endif
1137 				break;
1138 			case SLK:
1139 				set_lockkey_state(kbd, state, SLK);
1140 				break;
1141 			case ALK:
1142 				set_lockkey_state(kbd, state, ALK);
1143 				break;
1144 			/* NON-LOCKING KEYS */
1145 			case SPSC: case RBT:  case SUSP: case STBY:
1146 			case DBG:  case NEXT: case PREV: case PNC:
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