xref: /freebsd/sys/dev/vkbd/vkbd.c (revision 61ba55bcf70f2340f9c943c9571113b3fd8eda69)
1 /*
2  * vkbd.c
3  */
4 
5 /*-
6  * SPDX-License-Identifier: BSD-2-Clause
7  *
8  * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id: vkbd.c,v 1.20 2004/11/15 23:53:30 max Exp $
33  */
34 
35 #include "opt_kbd.h"
36 
37 #include <sys/param.h>
38 #include <sys/conf.h>
39 #include <sys/eventhandler.h>
40 #include <sys/fcntl.h>
41 #include <sys/kbio.h>
42 #include <sys/kernel.h>
43 #include <sys/limits.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/mutex.h>
48 #include <sys/poll.h>
49 #include <sys/proc.h>
50 #include <sys/queue.h>
51 #include <sys/selinfo.h>
52 #include <sys/systm.h>
53 #include <sys/taskqueue.h>
54 #include <sys/uio.h>
55 #include <dev/kbd/kbdreg.h>
56 #include <dev/kbd/kbdtables.h>
57 #include <dev/vkbd/vkbd_var.h>
58 
59 #define DEVICE_NAME	"vkbdctl"
60 #define KEYBOARD_NAME	"vkbd"
61 
62 MALLOC_DECLARE(M_VKBD);
63 MALLOC_DEFINE(M_VKBD, KEYBOARD_NAME, "Virtual AT keyboard");
64 
65 /*****************************************************************************
66  *****************************************************************************
67  **                             Keyboard state
68  *****************************************************************************
69  *****************************************************************************/
70 
71 /*
72  * XXX
73  * For now rely on Giant mutex to protect our data structures.
74  * Just like the rest of keyboard drivers and syscons(4) do.
75  */
76 
77 #if 0 /* not yet */
78 #define VKBD_LOCK_DECL		struct mtx ks_lock
79 #define VKBD_LOCK_INIT(s)	mtx_init(&(s)->ks_lock, "vkbd_lock", NULL, MTX_DEF|MTX_RECURSE)
80 #define VKBD_LOCK_DESTROY(s)	mtx_destroy(&(s)->ks_lock)
81 #define VKBD_LOCK(s)		mtx_lock(&(s)->ks_lock)
82 #define VKBD_UNLOCK(s)		mtx_unlock(&(s)->ks_lock)
83 #define VKBD_LOCK_ASSERT(s, w)	mtx_assert(&(s)->ks_lock, w)
84 #define VKBD_SLEEP(s, f, d, t) \
85 	msleep(&(s)->f, &(s)->ks_lock, PCATCH | (PZERO + 1), d, t)
86 #else
87 #define VKBD_LOCK_DECL
88 #define VKBD_LOCK_INIT(s)
89 #define VKBD_LOCK_DESTROY(s)
90 #define VKBD_LOCK(s)
91 #define VKBD_UNLOCK(s)
92 #define VKBD_LOCK_ASSERT(s, w)
93 #define VKBD_SLEEP(s, f, d, t)	tsleep(&(s)->f, PCATCH | (PZERO + 1), d, t)
94 #endif
95 
96 #define VKBD_KEYBOARD(d) \
97 	kbd_get_keyboard(kbd_find_keyboard(KEYBOARD_NAME, dev2unit(d)))
98 
99 /* vkbd queue */
100 struct vkbd_queue
101 {
102 	int		q[VKBD_Q_SIZE]; /* queue */
103 	int		head;		/* index of the first code */
104 	int		tail;		/* index of the last code */
105 	int		cc;		/* number of codes in queue */
106 };
107 
108 typedef struct vkbd_queue	vkbd_queue_t;
109 
110 /* vkbd state */
111 struct vkbd_state
112 {
113 	struct cdev	*ks_dev;	/* control device */
114 
115 	struct selinfo	 ks_rsel;	/* select(2) */
116 	struct selinfo	 ks_wsel;
117 
118 	vkbd_queue_t	 ks_inq;	/* input key codes queue */
119 	struct task	 ks_task;	/* interrupt task */
120 
121 	int		 ks_flags;	/* flags */
122 #define OPEN		(1 << 0)	/* control device is open */
123 #define COMPOSE		(1 << 1)	/* compose flag */
124 #define STATUS		(1 << 2)	/* status has changed */
125 #define TASK		(1 << 3)	/* interrupt task queued */
126 #define READ		(1 << 4)	/* read pending */
127 #define WRITE		(1 << 5)	/* write pending */
128 
129 	int		 ks_mode;	/* K_XLATE, K_RAW, K_CODE */
130 	int		 ks_polling;	/* polling flag */
131 	int		 ks_state;	/* shift/lock key state */
132 	int		 ks_accents;	/* accent key index (> 0) */
133 	u_int		 ks_composed_char; /* composed char code */
134 	u_char		 ks_prefix;	/* AT scan code prefix */
135 
136 	VKBD_LOCK_DECL;
137 };
138 
139 typedef struct vkbd_state	vkbd_state_t;
140 
141 /*****************************************************************************
142  *****************************************************************************
143  **                             Character device
144  *****************************************************************************
145  *****************************************************************************/
146 
147 static void		vkbd_dev_clone(void *, struct ucred *, char *, int,
148 			    struct cdev **);
149 static d_open_t		vkbd_dev_open;
150 static d_close_t	vkbd_dev_close;
151 static d_read_t		vkbd_dev_read;
152 static d_write_t	vkbd_dev_write;
153 static d_ioctl_t	vkbd_dev_ioctl;
154 static d_poll_t		vkbd_dev_poll;
155 static void		vkbd_dev_intr(void *, int);
156 static void		vkbd_status_changed(vkbd_state_t *);
157 static int		vkbd_data_ready(vkbd_state_t *);
158 static int		vkbd_data_read(vkbd_state_t *, int);
159 
160 static struct cdevsw	vkbd_dev_cdevsw = {
161 	.d_version =	D_VERSION,
162 	.d_flags =	D_NEEDGIANT | D_NEEDMINOR,
163 	.d_open =	vkbd_dev_open,
164 	.d_close =	vkbd_dev_close,
165 	.d_read =	vkbd_dev_read,
166 	.d_write =	vkbd_dev_write,
167 	.d_ioctl =	vkbd_dev_ioctl,
168 	.d_poll =	vkbd_dev_poll,
169 	.d_name =	DEVICE_NAME,
170 };
171 
172 static struct clonedevs	*vkbd_dev_clones = NULL;
173 
174 /* Clone device */
175 static void
176 vkbd_dev_clone(void *arg, struct ucred *cred, char *name, int namelen,
177     struct cdev **dev)
178 {
179 	int	unit;
180 
181 	if (*dev != NULL)
182 		return;
183 
184 	if (strcmp(name, DEVICE_NAME) == 0)
185 		unit = -1;
186 	else if (dev_stdclone(name, NULL, DEVICE_NAME, &unit) != 1)
187 		return; /* don't recognize the name */
188 
189 	/* find any existing device, or allocate new unit number */
190 	if (clone_create(&vkbd_dev_clones, &vkbd_dev_cdevsw, &unit, dev, 0))
191 		*dev = make_dev_credf(MAKEDEV_REF, &vkbd_dev_cdevsw, unit,
192 			cred, UID_ROOT, GID_WHEEL, 0600, DEVICE_NAME "%d",
193 			unit);
194 }
195 
196 /* Open device */
197 static int
198 vkbd_dev_open(struct cdev *dev, int flag, int mode, struct thread *td)
199 {
200 	int			 unit = dev2unit(dev), error;
201 	keyboard_switch_t	*sw = NULL;
202 	keyboard_t		*kbd = NULL;
203 	vkbd_state_t		*state = (vkbd_state_t *) dev->si_drv1;
204 
205 	/* XXX FIXME: dev->si_drv1 locking */
206 	if (state == NULL) {
207 		if ((sw = kbd_get_switch(KEYBOARD_NAME)) == NULL)
208 			return (ENXIO);
209 
210 		if ((error = (*sw->probe)(unit, NULL, 0)) != 0 ||
211 		    (error = (*sw->init)(unit, &kbd, NULL, 0)) != 0)
212 			return (error);
213 
214 		state = (vkbd_state_t *) kbd->kb_data;
215 
216 		if ((error = (*sw->enable)(kbd)) != 0) {
217 			(*sw->term)(kbd);
218 			return (error);
219 		}
220 
221 #ifdef KBD_INSTALL_CDEV
222 		if ((error = kbd_attach(kbd)) != 0) {
223 			(*sw->disable)(kbd);
224 			(*sw->term)(kbd);
225 			return (error);
226 		}
227 #endif /* def KBD_INSTALL_CDEV */
228 
229 		dev->si_drv1 = kbd->kb_data;
230 	}
231 
232 	VKBD_LOCK(state);
233 
234 	if (state->ks_flags & OPEN) {
235 		VKBD_UNLOCK(state);
236 		return (EBUSY);
237 	}
238 
239 	state->ks_flags |= OPEN;
240 	state->ks_dev = dev;
241 
242 	VKBD_UNLOCK(state);
243 
244 	return (0);
245 }
246 
247 /* Close device */
248 static int
249 vkbd_dev_close(struct cdev *dev, int foo, int bar, struct thread *td)
250 {
251 	keyboard_t	*kbd = VKBD_KEYBOARD(dev);
252 	vkbd_state_t	*state = NULL;
253 
254 	if (kbd == NULL)
255 		return (ENXIO);
256 
257 	if (kbd->kb_data == NULL || kbd->kb_data != dev->si_drv1)
258 		panic("%s: kbd->kb_data != dev->si_drv1\n", __func__);
259 
260 	state = (vkbd_state_t *) kbd->kb_data;
261 
262 	VKBD_LOCK(state);
263 
264 	/* wait for interrupt task */
265 	while (state->ks_flags & TASK)
266 		VKBD_SLEEP(state, ks_task, "vkbdc", 0);
267 
268 	/* wakeup poll()ers */
269 	selwakeuppri(&state->ks_rsel, PZERO + 1);
270 	selwakeuppri(&state->ks_wsel, PZERO + 1);
271 
272 	state->ks_flags &= ~OPEN;
273 	state->ks_dev = NULL;
274 	state->ks_inq.head = state->ks_inq.tail = state->ks_inq.cc = 0;
275 
276 	VKBD_UNLOCK(state);
277 
278 	kbdd_disable(kbd);
279 #ifdef KBD_INSTALL_CDEV
280 	kbd_detach(kbd);
281 #endif /* def KBD_INSTALL_CDEV */
282 	kbdd_term(kbd);
283 
284 	/* XXX FIXME: dev->si_drv1 locking */
285 	dev->si_drv1 = NULL;
286 
287 	return (0);
288 }
289 
290 /* Read status */
291 static int
292 vkbd_dev_read(struct cdev *dev, struct uio *uio, int flag)
293 {
294 	keyboard_t	*kbd = VKBD_KEYBOARD(dev);
295 	vkbd_state_t	*state = NULL;
296 	vkbd_status_t	 status;
297 	int		 error;
298 
299 	if (kbd == NULL)
300 		return (ENXIO);
301 
302 	if (uio->uio_resid != sizeof(status))
303 		return (EINVAL);
304 
305 	if (kbd->kb_data == NULL || kbd->kb_data != dev->si_drv1)
306 		panic("%s: kbd->kb_data != dev->si_drv1\n", __func__);
307 
308 	state = (vkbd_state_t *) kbd->kb_data;
309 
310 	VKBD_LOCK(state);
311 
312 	if (state->ks_flags & READ) {
313 		VKBD_UNLOCK(state);
314 		return (EALREADY);
315 	}
316 
317 	state->ks_flags |= READ;
318 again:
319 	if (state->ks_flags & STATUS) {
320 		state->ks_flags &= ~STATUS;
321 
322 		status.mode = state->ks_mode;
323 		status.leds = KBD_LED_VAL(kbd);
324 		status.lock = state->ks_state & LOCK_MASK;
325 		status.delay = kbd->kb_delay1;
326 		status.rate = kbd->kb_delay2;
327 		bzero(status.reserved, sizeof(status.reserved));
328 
329 		error = uiomove(&status, sizeof(status), uio);
330 	} else {
331 		if (flag & O_NONBLOCK) {
332 			error = EWOULDBLOCK;
333 			goto done;
334 		}
335 
336 		error = VKBD_SLEEP(state, ks_flags, "vkbdr", 0);
337 		if (error != 0)
338 			goto done;
339 
340 		goto again;
341 	}
342 done:
343 	state->ks_flags &= ~READ;
344 
345 	VKBD_UNLOCK(state);
346 
347 	return (error);
348 }
349 
350 /* Write scancodes */
351 static int
352 vkbd_dev_write(struct cdev *dev, struct uio *uio, int flag)
353 {
354 	keyboard_t	*kbd = VKBD_KEYBOARD(dev);
355 	vkbd_state_t	*state = NULL;
356 	vkbd_queue_t	*q = NULL;
357 	int		 error, avail, bytes;
358 
359 	if (kbd == NULL)
360 		return (ENXIO);
361 
362 	if (uio->uio_resid <= 0)
363 		return (EINVAL);
364 
365 	if (kbd->kb_data == NULL || kbd->kb_data != dev->si_drv1)
366 		panic("%s: kbd->kb_data != dev->si_drv1\n", __func__);
367 
368 	state = (vkbd_state_t *) kbd->kb_data;
369 
370 	VKBD_LOCK(state);
371 
372 	if (state->ks_flags & WRITE) {
373 		VKBD_UNLOCK(state);
374 		return (EALREADY);
375 	}
376 
377 	state->ks_flags |= WRITE;
378 	error = 0;
379 	q = &state->ks_inq;
380 
381 	while (uio->uio_resid >= sizeof(q->q[0])) {
382 		if (q->head == q->tail) {
383 			if (q->cc == 0)
384 				avail = nitems(q->q) - q->head;
385 			else
386 				avail = 0; /* queue must be full */
387 		} else if (q->head < q->tail)
388 			avail = nitems(q->q) - q->tail;
389 		else
390 			avail = q->head - q->tail;
391 
392 		if (avail == 0) {
393 			if (flag & O_NONBLOCK) {
394 				error = EWOULDBLOCK;
395 				break;
396 			}
397 
398 			error = VKBD_SLEEP(state, ks_inq, "vkbdw", 0);
399 			if (error != 0)
400 				break;
401 		} else {
402 			bytes = avail * sizeof(q->q[0]);
403 			if (bytes > uio->uio_resid) {
404 				avail = uio->uio_resid / sizeof(q->q[0]);
405 				bytes = avail * sizeof(q->q[0]);
406 			}
407 
408 			error = uiomove((void *) &q->q[q->tail], bytes, uio);
409 			if (error != 0)
410 				break;
411 
412 			q->cc += avail;
413 			q->tail += avail;
414 			if (q->tail == nitems(q->q))
415 				q->tail = 0;
416 
417 			/* queue interrupt task if needed */
418 			if (!(state->ks_flags & TASK) &&
419 			    taskqueue_enqueue(taskqueue_swi_giant, &state->ks_task) == 0)
420 				state->ks_flags |= TASK;
421 		}
422 	}
423 
424 	state->ks_flags &= ~WRITE;
425 
426 	VKBD_UNLOCK(state);
427 
428 	return (error);
429 }
430 
431 /* Process ioctl */
432 static int
433 vkbd_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
434 {
435 	keyboard_t	*kbd = VKBD_KEYBOARD(dev);
436 
437 	return ((kbd == NULL)? ENXIO : kbdd_ioctl(kbd, cmd, data));
438 }
439 
440 /* Poll device */
441 static int
442 vkbd_dev_poll(struct cdev *dev, int events, struct thread *td)
443 {
444 	vkbd_state_t	*state = (vkbd_state_t *) dev->si_drv1;
445 	vkbd_queue_t	*q = NULL;
446 	int		 revents = 0;
447 
448 	if (state == NULL)
449 		return (ENXIO);
450 
451 	VKBD_LOCK(state);
452 
453 	q = &state->ks_inq;
454 
455 	if (events & (POLLIN | POLLRDNORM)) {
456 		if (state->ks_flags & STATUS)
457 			revents |= events & (POLLIN | POLLRDNORM);
458 		else
459 			selrecord(td, &state->ks_rsel);
460 	}
461 
462 	if (events & (POLLOUT | POLLWRNORM)) {
463 		if (q->cc < nitems(q->q))
464 			revents |= events & (POLLOUT | POLLWRNORM);
465 		else
466 			selrecord(td, &state->ks_wsel);
467 	}
468 
469 	VKBD_UNLOCK(state);
470 
471 	return (revents);
472 }
473 
474 /* Interrupt handler */
475 void
476 vkbd_dev_intr(void *xkbd, int pending)
477 {
478 	keyboard_t	*kbd = (keyboard_t *) xkbd;
479 	vkbd_state_t	*state = (vkbd_state_t *) kbd->kb_data;
480 
481 	kbdd_intr(kbd, NULL);
482 
483 	VKBD_LOCK(state);
484 
485 	state->ks_flags &= ~TASK;
486 	wakeup(&state->ks_task);
487 
488 	VKBD_UNLOCK(state);
489 }
490 
491 /* Set status change flags */
492 static void
493 vkbd_status_changed(vkbd_state_t *state)
494 {
495 	VKBD_LOCK_ASSERT(state, MA_OWNED);
496 
497 	if (!(state->ks_flags & STATUS)) {
498 		state->ks_flags |= STATUS;
499 		selwakeuppri(&state->ks_rsel, PZERO + 1);
500 		wakeup(&state->ks_flags);
501 	}
502 }
503 
504 /* Check if we have data in the input queue */
505 static int
506 vkbd_data_ready(vkbd_state_t *state)
507 {
508 	VKBD_LOCK_ASSERT(state, MA_OWNED);
509 
510 	return (state->ks_inq.cc > 0);
511 }
512 
513 /* Read one code from the input queue */
514 static int
515 vkbd_data_read(vkbd_state_t *state, int wait)
516 {
517 	vkbd_queue_t	*q = &state->ks_inq;
518 	int		 c;
519 
520 	VKBD_LOCK_ASSERT(state, MA_OWNED);
521 
522 	if (q->cc == 0)
523 		return (-1);
524 
525 	/* get first code from the queue */
526 	q->cc --;
527 	c = q->q[q->head ++];
528 	if (q->head == nitems(q->q))
529 		q->head = 0;
530 
531 	/* wakeup ks_inq writers/poll()ers */
532 	selwakeuppri(&state->ks_wsel, PZERO + 1);
533 	wakeup(q);
534 
535 	return (c);
536 }
537 
538 /****************************************************************************
539  ****************************************************************************
540  **                              Keyboard driver
541  ****************************************************************************
542  ****************************************************************************/
543 
544 static int		vkbd_configure(int flags);
545 static kbd_probe_t	vkbd_probe;
546 static kbd_init_t	vkbd_init;
547 static kbd_term_t	vkbd_term;
548 static kbd_intr_t	vkbd_intr;
549 static kbd_test_if_t	vkbd_test_if;
550 static kbd_enable_t	vkbd_enable;
551 static kbd_disable_t	vkbd_disable;
552 static kbd_read_t	vkbd_read;
553 static kbd_check_t	vkbd_check;
554 static kbd_read_char_t	vkbd_read_char;
555 static kbd_check_char_t	vkbd_check_char;
556 static kbd_ioctl_t	vkbd_ioctl;
557 static kbd_lock_t	vkbd_lock;
558 static void		vkbd_clear_state_locked(vkbd_state_t *state);
559 static kbd_clear_state_t vkbd_clear_state;
560 static kbd_get_state_t	vkbd_get_state;
561 static kbd_set_state_t	vkbd_set_state;
562 static kbd_poll_mode_t	vkbd_poll;
563 
564 static keyboard_switch_t vkbdsw = {
565 	.probe =	vkbd_probe,
566 	.init =		vkbd_init,
567 	.term =		vkbd_term,
568 	.intr =		vkbd_intr,
569 	.test_if =	vkbd_test_if,
570 	.enable =	vkbd_enable,
571 	.disable =	vkbd_disable,
572 	.read =		vkbd_read,
573 	.check =	vkbd_check,
574 	.read_char =	vkbd_read_char,
575 	.check_char =	vkbd_check_char,
576 	.ioctl =	vkbd_ioctl,
577 	.lock =		vkbd_lock,
578 	.clear_state =	vkbd_clear_state,
579 	.get_state =	vkbd_get_state,
580 	.set_state =	vkbd_set_state,
581 	.poll =		vkbd_poll,
582 };
583 
584 static int	typematic(int delay, int rate);
585 static int	typematic_delay(int delay);
586 static int	typematic_rate(int rate);
587 
588 /* Return the number of found keyboards */
589 static int
590 vkbd_configure(int flags)
591 {
592 	return (1);
593 }
594 
595 /* Detect a keyboard */
596 static int
597 vkbd_probe(int unit, void *arg, int flags)
598 {
599 	return (0);
600 }
601 
602 /* Reset and initialize the keyboard (stolen from atkbd.c) */
603 static int
604 vkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
605 {
606 	keyboard_t	*kbd = NULL;
607 	vkbd_state_t	*state = NULL;
608 	keymap_t	*keymap = NULL;
609 	accentmap_t	*accmap = NULL;
610 	fkeytab_t	*fkeymap = NULL;
611 	int		 fkeymap_size, delay[2];
612 	int		 error, needfree;
613 
614 	if (*kbdp == NULL) {
615 		*kbdp = kbd = malloc(sizeof(*kbd), M_VKBD, M_NOWAIT | M_ZERO);
616 		state = malloc(sizeof(*state), M_VKBD, M_NOWAIT | M_ZERO);
617 		keymap = malloc(sizeof(key_map), M_VKBD, M_NOWAIT);
618 		accmap = malloc(sizeof(accent_map), M_VKBD, M_NOWAIT);
619 		fkeymap = malloc(sizeof(fkey_tab), M_VKBD, M_NOWAIT);
620 		fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
621 		needfree = 1;
622 		if ((kbd == NULL) || (state == NULL) || (keymap == NULL) ||
623 		    (accmap == NULL) || (fkeymap == NULL)) {
624 			error = ENOMEM;
625 			goto bad;
626 		}
627 
628 		VKBD_LOCK_INIT(state);
629 		state->ks_inq.head = state->ks_inq.tail = state->ks_inq.cc = 0;
630 		TASK_INIT(&state->ks_task, 0, vkbd_dev_intr, (void *) kbd);
631 	} else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
632 		return (0);
633 	} else {
634 		kbd = *kbdp;
635 		state = (vkbd_state_t *) kbd->kb_data;
636 		keymap = kbd->kb_keymap;
637 		accmap = kbd->kb_accentmap;
638 		fkeymap = kbd->kb_fkeytab;
639 		fkeymap_size = kbd->kb_fkeytab_size;
640 		needfree = 0;
641 	}
642 
643 	if (!KBD_IS_PROBED(kbd)) {
644 		kbd_init_struct(kbd, KEYBOARD_NAME, KB_OTHER, unit, flags, 0, 0);
645 		bcopy(&key_map, keymap, sizeof(key_map));
646 		bcopy(&accent_map, accmap, sizeof(accent_map));
647 		bcopy(fkey_tab, fkeymap,
648 			imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
649 		kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
650 		kbd->kb_data = (void *)state;
651 
652 		KBD_FOUND_DEVICE(kbd);
653 		KBD_PROBE_DONE(kbd);
654 
655 		VKBD_LOCK(state);
656 		vkbd_clear_state_locked(state);
657 		state->ks_mode = K_XLATE;
658 		/* FIXME: set the initial value for lock keys in ks_state */
659 		VKBD_UNLOCK(state);
660 	}
661 	if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
662 		kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
663 
664 		vkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
665 		delay[0] = kbd->kb_delay1;
666 		delay[1] = kbd->kb_delay2;
667 		vkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
668 
669 		KBD_INIT_DONE(kbd);
670 	}
671 	if (!KBD_IS_CONFIGURED(kbd)) {
672 		if (kbd_register(kbd) < 0) {
673 			error = ENXIO;
674 			goto bad;
675 		}
676 		KBD_CONFIG_DONE(kbd);
677 	}
678 
679 	return (0);
680 bad:
681 	if (needfree) {
682 		if (state != NULL)
683 			free(state, M_VKBD);
684 		if (keymap != NULL)
685 			free(keymap, M_VKBD);
686 		if (accmap != NULL)
687 			free(accmap, M_VKBD);
688 		if (fkeymap != NULL)
689 			free(fkeymap, M_VKBD);
690 		if (kbd != NULL) {
691 			free(kbd, M_VKBD);
692 			*kbdp = NULL;	/* insure ref doesn't leak to caller */
693 		}
694 	}
695 	return (error);
696 }
697 
698 /* Finish using this keyboard */
699 static int
700 vkbd_term(keyboard_t *kbd)
701 {
702 	vkbd_state_t	*state = (vkbd_state_t *) kbd->kb_data;
703 
704 	kbd_unregister(kbd);
705 
706 	VKBD_LOCK_DESTROY(state);
707 	bzero(state, sizeof(*state));
708 	free(state, M_VKBD);
709 
710 	free(kbd->kb_keymap, M_VKBD);
711 	free(kbd->kb_accentmap, M_VKBD);
712 	free(kbd->kb_fkeytab, M_VKBD);
713 	free(kbd, M_VKBD);
714 
715 	return (0);
716 }
717 
718 /* Keyboard interrupt routine */
719 static int
720 vkbd_intr(keyboard_t *kbd, void *arg)
721 {
722 	int	c;
723 
724 	if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
725 		/* let the callback function to process the input */
726 		(*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
727 					    kbd->kb_callback.kc_arg);
728 	} else {
729 		/* read and discard the input; no one is waiting for input */
730 		do {
731 			c = vkbd_read_char(kbd, FALSE);
732 		} while (c != NOKEY);
733 	}
734 
735 	return (0);
736 }
737 
738 /* Test the interface to the device */
739 static int
740 vkbd_test_if(keyboard_t *kbd)
741 {
742 	return (0);
743 }
744 
745 /*
746  * Enable the access to the device; until this function is called,
747  * the client cannot read from the keyboard.
748  */
749 
750 static int
751 vkbd_enable(keyboard_t *kbd)
752 {
753 	KBD_ACTIVATE(kbd);
754 	return (0);
755 }
756 
757 /* Disallow the access to the device */
758 static int
759 vkbd_disable(keyboard_t *kbd)
760 {
761 	KBD_DEACTIVATE(kbd);
762 	return (0);
763 }
764 
765 /* Read one byte from the keyboard if it's allowed */
766 static int
767 vkbd_read(keyboard_t *kbd, int wait)
768 {
769 	vkbd_state_t	*state = (vkbd_state_t *) kbd->kb_data;
770 	int		 c;
771 
772 	VKBD_LOCK(state);
773 	c = vkbd_data_read(state, wait);
774 	VKBD_UNLOCK(state);
775 
776 	if (c != -1)
777 		kbd->kb_count ++;
778 
779 	return (KBD_IS_ACTIVE(kbd)? c : -1);
780 }
781 
782 /* Check if data is waiting */
783 static int
784 vkbd_check(keyboard_t *kbd)
785 {
786 	vkbd_state_t	*state = NULL;
787 	int		 ready;
788 
789 	if (!KBD_IS_ACTIVE(kbd))
790 		return (FALSE);
791 
792 	state = (vkbd_state_t *) kbd->kb_data;
793 
794 	VKBD_LOCK(state);
795 	ready = vkbd_data_ready(state);
796 	VKBD_UNLOCK(state);
797 
798 	return (ready);
799 }
800 
801 /* Read char from the keyboard (stolen from atkbd.c) */
802 static u_int
803 vkbd_read_char(keyboard_t *kbd, int wait)
804 {
805 	vkbd_state_t	*state = (vkbd_state_t *) kbd->kb_data;
806 	u_int		 action;
807 	int		 scancode, keycode;
808 
809 	VKBD_LOCK(state);
810 
811 next_code:
812 
813 	/* do we have a composed char to return? */
814 	if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
815 		action = state->ks_composed_char;
816 		state->ks_composed_char = 0;
817 		if (action > UCHAR_MAX) {
818 			VKBD_UNLOCK(state);
819 			return (ERRKEY);
820 		}
821 
822 		VKBD_UNLOCK(state);
823 		return (action);
824 	}
825 
826 	/* see if there is something in the keyboard port */
827 	scancode = vkbd_data_read(state, wait);
828 	if (scancode == -1) {
829 		VKBD_UNLOCK(state);
830 		return (NOKEY);
831 	}
832 	/* XXX FIXME: check for -1 if wait == 1! */
833 
834 	kbd->kb_count ++;
835 
836 	/* return the byte as is for the K_RAW mode */
837 	if (state->ks_mode == K_RAW) {
838 		VKBD_UNLOCK(state);
839 		return (scancode);
840 	}
841 
842 	/* translate the scan code into a keycode */
843 	keycode = scancode & 0x7F;
844 	switch (state->ks_prefix) {
845 	case 0x00:	/* normal scancode */
846 		switch(scancode) {
847 		case 0xB8:	/* left alt (compose key) released */
848 			if (state->ks_flags & COMPOSE) {
849 				state->ks_flags &= ~COMPOSE;
850 				if (state->ks_composed_char > UCHAR_MAX)
851 					state->ks_composed_char = 0;
852 			}
853 			break;
854 		case 0x38:	/* left alt (compose key) pressed */
855 			if (!(state->ks_flags & COMPOSE)) {
856 				state->ks_flags |= COMPOSE;
857 				state->ks_composed_char = 0;
858 			}
859 			break;
860 		case 0xE0:
861 		case 0xE1:
862 			state->ks_prefix = scancode;
863 			goto next_code;
864 		}
865 		break;
866 	case 0xE0:      /* 0xE0 prefix */
867 		state->ks_prefix = 0;
868 		switch (keycode) {
869 		case 0x1C:	/* right enter key */
870 			keycode = 0x59;
871 			break;
872 		case 0x1D:	/* right ctrl key */
873 			keycode = 0x5A;
874 			break;
875 		case 0x35:	/* keypad divide key */
876 			keycode = 0x5B;
877 			break;
878 		case 0x37:	/* print scrn key */
879 			keycode = 0x5C;
880 			break;
881 		case 0x38:	/* right alt key (alt gr) */
882 			keycode = 0x5D;
883 			break;
884 		case 0x46:	/* ctrl-pause/break on AT 101 (see below) */
885 			keycode = 0x68;
886 			break;
887 		case 0x47:	/* grey home key */
888 			keycode = 0x5E;
889 			break;
890 		case 0x48:	/* grey up arrow key */
891 			keycode = 0x5F;
892 			break;
893 		case 0x49:	/* grey page up key */
894 			keycode = 0x60;
895 			break;
896 		case 0x4B:	/* grey left arrow key */
897 			keycode = 0x61;
898 			break;
899 		case 0x4D:	/* grey right arrow key */
900 			keycode = 0x62;
901 			break;
902 		case 0x4F:	/* grey end key */
903 			keycode = 0x63;
904 			break;
905 		case 0x50:	/* grey down arrow key */
906 			keycode = 0x64;
907 			break;
908 		case 0x51:	/* grey page down key */
909 			keycode = 0x65;
910 			break;
911 		case 0x52:	/* grey insert key */
912 			keycode = 0x66;
913 			break;
914 		case 0x53:	/* grey delete key */
915 			keycode = 0x67;
916 			break;
917 		/* the following 3 are only used on the MS "Natural" keyboard */
918 		case 0x5b:	/* left Window key */
919 			keycode = 0x69;
920 			break;
921 		case 0x5c:	/* right Window key */
922 			keycode = 0x6a;
923 			break;
924 		case 0x5d:	/* menu key */
925 			keycode = 0x6b;
926 			break;
927 		case 0x5e:	/* power key */
928 			keycode = 0x6d;
929 			break;
930 		case 0x5f:	/* sleep key */
931 			keycode = 0x6e;
932 			break;
933 		case 0x63:	/* wake key */
934 			keycode = 0x6f;
935 			break;
936 		default:	/* ignore everything else */
937 			goto next_code;
938 		}
939 		break;
940 	case 0xE1:	/* 0xE1 prefix */
941 		/*
942 		 * The pause/break key on the 101 keyboard produces:
943 		 * E1-1D-45 E1-9D-C5
944 		 * Ctrl-pause/break produces:
945 		 * E0-46 E0-C6 (See above.)
946 		 */
947 		state->ks_prefix = 0;
948 		if (keycode == 0x1D)
949 			state->ks_prefix = 0x1D;
950 		goto next_code;
951 		/* NOT REACHED */
952 	case 0x1D:	/* pause / break */
953 		state->ks_prefix = 0;
954 		if (keycode != 0x45)
955 			goto next_code;
956 		keycode = 0x68;
957 		break;
958 	}
959 
960 	if (kbd->kb_type == KB_84) {
961 		switch (keycode) {
962 		case 0x37:	/* *(numpad)/print screen */
963 			if (state->ks_flags & SHIFTS)
964 				keycode = 0x5c;	/* print screen */
965 			break;
966 		case 0x45:	/* num lock/pause */
967 			if (state->ks_flags & CTLS)
968 				keycode = 0x68;	/* pause */
969 			break;
970 		case 0x46:	/* scroll lock/break */
971 			if (state->ks_flags & CTLS)
972 				keycode = 0x6c;	/* break */
973 			break;
974 		}
975 	} else if (kbd->kb_type == KB_101) {
976 		switch (keycode) {
977 		case 0x5c:	/* print screen */
978 			if (state->ks_flags & ALTS)
979 				keycode = 0x54;	/* sysrq */
980 			break;
981 		case 0x68:	/* pause/break */
982 			if (state->ks_flags & CTLS)
983 				keycode = 0x6c;	/* break */
984 			break;
985 		}
986 	}
987 
988 	/* return the key code in the K_CODE mode */
989 	if (state->ks_mode == K_CODE) {
990 		VKBD_UNLOCK(state);
991 		return (keycode | (scancode & 0x80));
992 	}
993 
994 	/* compose a character code */
995 	if (state->ks_flags & COMPOSE) {
996 		switch (keycode | (scancode & 0x80)) {
997 		/* key pressed, process it */
998 		case 0x47: case 0x48: case 0x49:	/* keypad 7,8,9 */
999 			state->ks_composed_char *= 10;
1000 			state->ks_composed_char += keycode - 0x40;
1001 			if (state->ks_composed_char > UCHAR_MAX) {
1002 				VKBD_UNLOCK(state);
1003 				return (ERRKEY);
1004 			}
1005 			goto next_code;
1006 		case 0x4B: case 0x4C: case 0x4D:	/* keypad 4,5,6 */
1007 			state->ks_composed_char *= 10;
1008 			state->ks_composed_char += keycode - 0x47;
1009 			if (state->ks_composed_char > UCHAR_MAX) {
1010 				VKBD_UNLOCK(state);
1011 				return (ERRKEY);
1012 			}
1013 			goto next_code;
1014 		case 0x4F: case 0x50: case 0x51:	/* keypad 1,2,3 */
1015 			state->ks_composed_char *= 10;
1016 			state->ks_composed_char += keycode - 0x4E;
1017 			if (state->ks_composed_char > UCHAR_MAX) {
1018 				VKBD_UNLOCK(state);
1019 				return (ERRKEY);
1020 			}
1021 			goto next_code;
1022 		case 0x52:	/* keypad 0 */
1023 			state->ks_composed_char *= 10;
1024 			if (state->ks_composed_char > UCHAR_MAX) {
1025 				VKBD_UNLOCK(state);
1026 				return (ERRKEY);
1027 			}
1028 			goto next_code;
1029 
1030 		/* key released, no interest here */
1031 		case 0xC7: case 0xC8: case 0xC9:	/* keypad 7,8,9 */
1032 		case 0xCB: case 0xCC: case 0xCD:	/* keypad 4,5,6 */
1033 		case 0xCF: case 0xD0: case 0xD1:	/* keypad 1,2,3 */
1034 		case 0xD2:				/* keypad 0 */
1035 			goto next_code;
1036 
1037 		case 0x38:				/* left alt key */
1038 			break;
1039 
1040 		default:
1041 			if (state->ks_composed_char > 0) {
1042 				state->ks_flags &= ~COMPOSE;
1043 				state->ks_composed_char = 0;
1044 				VKBD_UNLOCK(state);
1045 				return (ERRKEY);
1046 			}
1047 			break;
1048 		}
1049 	}
1050 
1051 	/* keycode to key action */
1052 	action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
1053 			&state->ks_state, &state->ks_accents);
1054 	if (action == NOKEY)
1055 		goto next_code;
1056 
1057 	VKBD_UNLOCK(state);
1058 
1059 	return (action);
1060 }
1061 
1062 /* Check if char is waiting */
1063 static int
1064 vkbd_check_char(keyboard_t *kbd)
1065 {
1066 	vkbd_state_t	*state = NULL;
1067 	int		 ready;
1068 
1069 	if (!KBD_IS_ACTIVE(kbd))
1070 		return (FALSE);
1071 
1072 	state = (vkbd_state_t *) kbd->kb_data;
1073 
1074 	VKBD_LOCK(state);
1075 	if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
1076 		ready = TRUE;
1077 	else
1078 		ready = vkbd_data_ready(state);
1079 	VKBD_UNLOCK(state);
1080 
1081 	return (ready);
1082 }
1083 
1084 /* Some useful control functions (stolen from atkbd.c) */
1085 static int
1086 vkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1087 {
1088 	vkbd_state_t	*state = (vkbd_state_t *) kbd->kb_data;
1089 	int		 i;
1090 #ifdef COMPAT_FREEBSD6
1091 	int		 ival;
1092 #endif
1093 
1094 	VKBD_LOCK(state);
1095 
1096 	switch (cmd) {
1097 	case KDGKBMODE:		/* get keyboard mode */
1098 		*(int *)arg = state->ks_mode;
1099 		break;
1100 
1101 #ifdef COMPAT_FREEBSD6
1102 	case _IO('K', 7):
1103 		ival = IOCPARM_IVAL(arg);
1104 		arg = (caddr_t)&ival;
1105 		/* FALLTHROUGH */
1106 #endif
1107 	case KDSKBMODE:		/* set keyboard mode */
1108 		switch (*(int *)arg) {
1109 		case K_XLATE:
1110 			if (state->ks_mode != K_XLATE) {
1111 				/* make lock key state and LED state match */
1112 				state->ks_state &= ~LOCK_MASK;
1113 				state->ks_state |= KBD_LED_VAL(kbd);
1114 				vkbd_status_changed(state);
1115 			}
1116 			/* FALLTHROUGH */
1117 
1118 		case K_RAW:
1119 		case K_CODE:
1120 			if (state->ks_mode != *(int *)arg) {
1121 				vkbd_clear_state_locked(state);
1122 				state->ks_mode = *(int *)arg;
1123 				vkbd_status_changed(state);
1124 			}
1125 			break;
1126 
1127 		default:
1128 			VKBD_UNLOCK(state);
1129 			return (EINVAL);
1130 		}
1131 		break;
1132 
1133 	case KDGETLED:		/* get keyboard LED */
1134 		*(int *)arg = KBD_LED_VAL(kbd);
1135 		break;
1136 
1137 #ifdef COMPAT_FREEBSD6
1138 	case _IO('K', 66):
1139 		ival = IOCPARM_IVAL(arg);
1140 		arg = (caddr_t)&ival;
1141 		/* FALLTHROUGH */
1142 #endif
1143 	case KDSETLED:		/* set keyboard LED */
1144 		/* NOTE: lock key state in ks_state won't be changed */
1145 		if (*(int *)arg & ~LOCK_MASK) {
1146 			VKBD_UNLOCK(state);
1147 			return (EINVAL);
1148 		}
1149 
1150 		i = *(int *)arg;
1151 		/* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1152 		if (state->ks_mode == K_XLATE &&
1153 		    kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1154 			if (i & ALKED)
1155 				i |= CLKED;
1156 			else
1157 				i &= ~CLKED;
1158 		}
1159 
1160 		KBD_LED_VAL(kbd) = *(int *)arg;
1161 		vkbd_status_changed(state);
1162 		break;
1163 
1164 	case KDGKBSTATE:	/* get lock key state */
1165 		*(int *)arg = state->ks_state & LOCK_MASK;
1166 		break;
1167 
1168 #ifdef COMPAT_FREEBSD6
1169 	case _IO('K', 20):
1170 		ival = IOCPARM_IVAL(arg);
1171 		arg = (caddr_t)&ival;
1172 		/* FALLTHROUGH */
1173 #endif
1174 	case KDSKBSTATE:	/* set lock key state */
1175 		if (*(int *)arg & ~LOCK_MASK) {
1176 			VKBD_UNLOCK(state);
1177 			return (EINVAL);
1178 		}
1179 		state->ks_state &= ~LOCK_MASK;
1180 		state->ks_state |= *(int *)arg;
1181 		vkbd_status_changed(state);
1182 		VKBD_UNLOCK(state);
1183 		/* set LEDs and quit */
1184 		return (vkbd_ioctl(kbd, KDSETLED, arg));
1185 
1186 	case KDSETREPEAT:	/* set keyboard repeat rate (new interface) */
1187 		i = typematic(((int *)arg)[0], ((int *)arg)[1]);
1188 		kbd->kb_delay1 = typematic_delay(i);
1189 		kbd->kb_delay2 = typematic_rate(i);
1190 		vkbd_status_changed(state);
1191 		break;
1192 
1193 #ifdef COMPAT_FREEBSD6
1194 	case _IO('K', 67):
1195 		ival = IOCPARM_IVAL(arg);
1196 		arg = (caddr_t)&ival;
1197 		/* FALLTHROUGH */
1198 #endif
1199 	case KDSETRAD:		/* set keyboard repeat rate (old interface) */
1200 		kbd->kb_delay1 = typematic_delay(*(int *)arg);
1201 		kbd->kb_delay2 = typematic_rate(*(int *)arg);
1202 		vkbd_status_changed(state);
1203 		break;
1204 
1205 	case PIO_KEYMAP:	/* set keyboard translation table */
1206 	case PIO_KEYMAPENT:	/* set keyboard translation table entry */
1207 	case PIO_DEADKEYMAP:	/* set accent key translation table */
1208 #ifdef COMPAT_FREEBSD13
1209 	case OPIO_KEYMAP:	/* set keyboard translation table (compat) */
1210 	case OPIO_DEADKEYMAP:	/* set accent key translation table (compat) */
1211 #endif /* COMPAT_FREEBSD13 */
1212 		state->ks_accents = 0;
1213 		/* FALLTHROUGH */
1214 
1215 	default:
1216 		VKBD_UNLOCK(state);
1217 		return (genkbd_commonioctl(kbd, cmd, arg));
1218 	}
1219 
1220 	VKBD_UNLOCK(state);
1221 
1222 	return (0);
1223 }
1224 
1225 /* Lock the access to the keyboard */
1226 static int
1227 vkbd_lock(keyboard_t *kbd, int lock)
1228 {
1229 	return (1); /* XXX */
1230 }
1231 
1232 /* Clear the internal state of the keyboard */
1233 static void
1234 vkbd_clear_state_locked(vkbd_state_t *state)
1235 {
1236 	VKBD_LOCK_ASSERT(state, MA_OWNED);
1237 
1238 	state->ks_flags &= ~COMPOSE;
1239 	state->ks_polling = 0;
1240 	state->ks_state &= LOCK_MASK;	/* preserve locking key state */
1241 	state->ks_accents = 0;
1242 	state->ks_composed_char = 0;
1243 /*	state->ks_prefix = 0;		XXX */
1244 
1245 	/* flush ks_inq and wakeup writers/poll()ers */
1246 	state->ks_inq.head = state->ks_inq.tail = state->ks_inq.cc = 0;
1247 	selwakeuppri(&state->ks_wsel, PZERO + 1);
1248 	wakeup(&state->ks_inq);
1249 }
1250 
1251 static void
1252 vkbd_clear_state(keyboard_t *kbd)
1253 {
1254 	vkbd_state_t	*state = (vkbd_state_t *) kbd->kb_data;
1255 
1256 	VKBD_LOCK(state);
1257 	vkbd_clear_state_locked(state);
1258 	VKBD_UNLOCK(state);
1259 }
1260 
1261 /* Save the internal state */
1262 static int
1263 vkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1264 {
1265 	if (len == 0)
1266 		return (sizeof(vkbd_state_t));
1267 	if (len < sizeof(vkbd_state_t))
1268 		return (-1);
1269 	bcopy(kbd->kb_data, buf, sizeof(vkbd_state_t)); /* XXX locking? */
1270 	return (0);
1271 }
1272 
1273 /* Set the internal state */
1274 static int
1275 vkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1276 {
1277 	if (len < sizeof(vkbd_state_t))
1278 		return (ENOMEM);
1279 	bcopy(buf, kbd->kb_data, sizeof(vkbd_state_t)); /* XXX locking? */
1280 	return (0);
1281 }
1282 
1283 /* Set polling */
1284 static int
1285 vkbd_poll(keyboard_t *kbd, int on)
1286 {
1287 	vkbd_state_t	*state = NULL;
1288 
1289 	state = (vkbd_state_t *) kbd->kb_data;
1290 
1291 	VKBD_LOCK(state);
1292 
1293 	if (on)
1294 		state->ks_polling ++;
1295 	else
1296 		state->ks_polling --;
1297 
1298 	VKBD_UNLOCK(state);
1299 
1300 	return (0);
1301 }
1302 
1303 /*
1304  * Local functions
1305  */
1306 
1307 static int
1308 typematic_delay(int i)
1309 {
1310 	return (kbdelays[(i >> 5) & 3]);
1311 }
1312 
1313 static int
1314 typematic_rate(int i)
1315 {
1316 	return (kbrates[i & 0x1f]);
1317 }
1318 
1319 static int
1320 typematic(int delay, int rate)
1321 {
1322 	int value;
1323 	int i;
1324 
1325 	for (i = nitems(kbdelays) - 1; i > 0; i--) {
1326 		if (delay >= kbdelays[i])
1327 			break;
1328 	}
1329 	value = i << 5;
1330 	for (i = nitems(kbrates) - 1; i > 0; i--) {
1331 		if (rate >= kbrates[i])
1332 			break;
1333 	}
1334 	value |= i;
1335 	return (value);
1336 }
1337 
1338 /*****************************************************************************
1339  *****************************************************************************
1340  **                                    Module
1341  *****************************************************************************
1342  *****************************************************************************/
1343 
1344 KEYBOARD_DRIVER(vkbd, vkbdsw, vkbd_configure);
1345 
1346 static int
1347 vkbd_modevent(module_t mod, int type, void *data)
1348 {
1349 	static eventhandler_tag	tag;
1350 
1351 	switch (type) {
1352 	case MOD_LOAD:
1353 		clone_setup(&vkbd_dev_clones);
1354 		tag = EVENTHANDLER_REGISTER(dev_clone, vkbd_dev_clone, 0, 1000);
1355 		if (tag == NULL) {
1356 			clone_cleanup(&vkbd_dev_clones);
1357 			return (ENOMEM);
1358 		}
1359 		kbd_add_driver(&vkbd_kbd_driver);
1360 		break;
1361 
1362 	case MOD_UNLOAD:
1363 		kbd_delete_driver(&vkbd_kbd_driver);
1364 		EVENTHANDLER_DEREGISTER(dev_clone, tag);
1365 		clone_cleanup(&vkbd_dev_clones);
1366 		break;
1367 
1368 	default:
1369 		return (EOPNOTSUPP);
1370 	}
1371 
1372 	return (0);
1373 }
1374 
1375 DEV_MODULE(vkbd, vkbd_modevent, NULL);
1376