vkbd.c revision 139204
1137776Semax/*
2137776Semax * vkbd.c
3137776Semax *
4137776Semax * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5137776Semax * All rights reserved.
6137776Semax *
7137776Semax * Redistribution and use in source and binary forms, with or without
8137776Semax * modification, are permitted provided that the following conditions
9137776Semax * are met:
10137776Semax * 1. Redistributions of source code must retain the above copyright
11137776Semax *    notice, this list of conditions and the following disclaimer.
12137776Semax * 2. Redistributions in binary form must reproduce the above copyright
13137776Semax *    notice, this list of conditions and the following disclaimer in the
14137776Semax *    documentation and/or other materials provided with the distribution.
15137776Semax *
16137776Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17137776Semax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18137776Semax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19137776Semax * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20137776Semax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21137776Semax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22137776Semax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23137776Semax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24137776Semax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25137776Semax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26137776Semax * SUCH DAMAGE.
27137776Semax *
28137776Semax * $Id: vkbd.c,v 1.20 2004/11/15 23:53:30 max Exp $
29137776Semax * $FreeBSD: head/sys/dev/vkbd/vkbd.c 139204 2004-12-22 17:36:38Z phk $
30137776Semax */
31137776Semax
32137776Semax#include "opt_kbd.h"
33137776Semax
34137776Semax#include <sys/param.h>
35137776Semax#include <sys/conf.h>
36139204Sphk#include <sys/fcntl.h>
37137776Semax#include <sys/kbio.h>
38137776Semax#include <sys/kernel.h>
39137776Semax#include <sys/limits.h>
40137776Semax#include <sys/lock.h>
41137776Semax#include <sys/malloc.h>
42137776Semax#include <sys/module.h>
43137776Semax#include <sys/mutex.h>
44137776Semax#include <sys/poll.h>
45137776Semax#include <sys/proc.h>
46137776Semax#include <sys/queue.h>
47139204Sphk#include <sys/selinfo.h>
48137776Semax#include <sys/systm.h>
49137776Semax#include <sys/taskqueue.h>
50137776Semax#include <sys/uio.h>
51137776Semax#include <dev/kbd/kbdreg.h>
52137776Semax#include <dev/kbd/kbdtables.h>
53137776Semax#include <dev/vkbd/vkbd_var.h>
54137776Semax
55137776Semax#define DEVICE_NAME	"vkbdctl"
56137776Semax#define KEYBOARD_NAME	"vkbd"
57137776Semax
58137776SemaxMALLOC_DECLARE(M_VKBD);
59137776SemaxMALLOC_DEFINE(M_VKBD, KEYBOARD_NAME, "Virtual AT keyboard");
60137776Semax
61137776Semax/*****************************************************************************
62137776Semax *****************************************************************************
63137776Semax **                             Keyboard state
64137776Semax *****************************************************************************
65137776Semax *****************************************************************************/
66137776Semax
67137776Semax#define VKBD_LOCK_DECL		struct mtx ks_lock
68137776Semax#define VKBD_LOCK_INIT(s)	mtx_init(&(s)->ks_lock, NULL, NULL, MTX_DEF)
69137776Semax#define VKBD_LOCK_DESTROY(s)	mtx_destroy(&(s)->ks_lock)
70137776Semax#define VKBD_LOCK(s)		mtx_lock(&(s)->ks_lock)
71137776Semax#define VKBD_UNLOCK(s)		mtx_unlock(&(s)->ks_lock)
72137776Semax#define VKBD_LOCK_ASSERT(s, w)	mtx_assert(&(s)->ks_lock, w)
73137776Semax#define VKBD_SLEEP(s, f, d, t) \
74137776Semax	msleep(&(s)->f, &(s)->ks_lock, PCATCH | (PZERO + 1), d, t)
75137776Semax
76137776Semax#define VKBD_KEYBOARD(d) \
77137776Semax	kbd_get_keyboard(kbd_find_keyboard(KEYBOARD_NAME, dev2unit(d)))
78137776Semax
79137776Semax/* vkbd queue */
80137776Semaxstruct vkbd_queue
81137776Semax{
82137776Semax	int		q[VKBD_Q_SIZE]; /* queue */
83137776Semax	int		head;		/* index of the first code */
84137776Semax	int		tail;		/* index of the last code */
85137776Semax	int		cc;		/* number of codes in queue */
86137776Semax};
87137776Semax
88137776Semaxtypedef struct vkbd_queue	vkbd_queue_t;
89137776Semax
90137776Semax/* vkbd state */
91137776Semaxstruct vkbd_state
92137776Semax{
93137776Semax	struct cdev	*ks_dev;	/* control device */
94137776Semax
95137776Semax	struct selinfo	 ks_rsel;	/* select(2) */
96137776Semax	struct selinfo	 ks_wsel;
97137776Semax
98137776Semax	vkbd_queue_t	 ks_inq;	/* input key codes queue */
99137776Semax	struct task	 ks_task;	/* interrupt task */
100137776Semax
101137776Semax	int		 ks_flags;	/* flags */
102137776Semax#define OPEN		(1 << 0)	/* control device is open */
103137776Semax#define COMPOSE		(1 << 1)	/* compose flag */
104137776Semax#define STATUS		(1 << 2)	/* status has changed */
105137776Semax#define TASK		(1 << 3)	/* interrupt task queued */
106137776Semax#define READ		(1 << 4)	/* read pending */
107137776Semax#define WRITE		(1 << 5)	/* write pending */
108137776Semax
109137776Semax	int		 ks_mode;	/* K_XLATE, K_RAW, K_CODE */
110137776Semax	int		 ks_polling;	/* polling flag */
111137776Semax	int		 ks_state;	/* shift/lock key state */
112137776Semax	int		 ks_accents;	/* accent key index (> 0) */
113137776Semax	u_int		 ks_composed_char; /* composed char code */
114137776Semax	u_char		 ks_prefix;	/* AT scan code prefix */
115137776Semax
116137776Semax	VKBD_LOCK_DECL;
117137776Semax};
118137776Semax
119137776Semaxtypedef struct vkbd_state	vkbd_state_t;
120137776Semax
121137776Semax/*****************************************************************************
122137776Semax *****************************************************************************
123137776Semax **                             Character device
124137776Semax *****************************************************************************
125137776Semax *****************************************************************************/
126137776Semax
127137776Semaxstatic void		vkbd_dev_clone(void *, char *, int, struct cdev **);
128137776Semaxstatic d_open_t		vkbd_dev_open;
129137776Semaxstatic d_close_t	vkbd_dev_close;
130137776Semaxstatic d_read_t		vkbd_dev_read;
131137776Semaxstatic d_write_t	vkbd_dev_write;
132137776Semaxstatic d_ioctl_t	vkbd_dev_ioctl;
133137776Semaxstatic d_poll_t		vkbd_dev_poll;
134137776Semaxstatic void		vkbd_dev_intr(void *, int);
135137776Semaxstatic void		vkbd_status_changed(vkbd_state_t *);
136137776Semaxstatic int		vkbd_data_ready(vkbd_state_t *);
137137776Semaxstatic int		vkbd_data_read(vkbd_state_t *, int);
138137776Semax
139137776Semaxstatic struct cdevsw	vkbd_dev_cdevsw = {
140137776Semax	.d_version =	D_VERSION,
141137776Semax	.d_flags =	D_PSEUDO | D_NEEDGIANT,
142137776Semax	.d_open =	vkbd_dev_open,
143137776Semax	.d_close =	vkbd_dev_close,
144137776Semax	.d_read =	vkbd_dev_read,
145137776Semax	.d_write =	vkbd_dev_write,
146137776Semax	.d_ioctl =	vkbd_dev_ioctl,
147137776Semax	.d_poll =	vkbd_dev_poll,
148137776Semax	.d_name =	DEVICE_NAME,
149137776Semax};
150137776Semax
151137776Semaxstatic struct clonedevs	*vkbd_dev_clones = NULL;
152137776Semax
153137776Semax/* Clone device */
154137776Semaxstatic void
155137776Semaxvkbd_dev_clone(void *arg, char *name, int namelen, struct cdev **dev)
156137776Semax{
157137776Semax	int	unit;
158137776Semax
159137776Semax	if (*dev != NULL)
160137776Semax		return;
161137776Semax
162137776Semax	if (strcmp(name, DEVICE_NAME) == 0)
163137776Semax		unit = -1;
164137776Semax	else if (dev_stdclone(name, NULL, DEVICE_NAME, &unit) != 1)
165137776Semax		return; /* don't recognize the name */
166137776Semax
167137776Semax	/* find any existing device, or allocate new unit number */
168137776Semax	if (clone_create(&vkbd_dev_clones, &vkbd_dev_cdevsw, &unit, dev, 0)) {
169137776Semax		*dev = make_dev(&vkbd_dev_cdevsw, unit2minor(unit),
170137776Semax			UID_ROOT, GID_WHEEL, 0600, DEVICE_NAME "%d", unit);
171137776Semax		if (*dev != NULL)
172137776Semax			(*dev)->si_flags |= SI_CHEAPCLONE;
173137776Semax	}
174137776Semax}
175137776Semax
176137776Semax/* Open device */
177137776Semaxstatic int
178137776Semaxvkbd_dev_open(struct cdev *dev, int flag, int mode, struct thread *td)
179137776Semax{
180137776Semax	int			 unit = dev2unit(dev), error;
181137776Semax	keyboard_switch_t	*sw = NULL;
182137776Semax	keyboard_t		*kbd = NULL;
183137776Semax	vkbd_state_t		*state = (vkbd_state_t *) dev->si_drv1;
184137776Semax
185137776Semax	/* XXX FIXME: dev->si_drv1 locking */
186137776Semax	if (state == NULL) {
187137776Semax		if ((sw = kbd_get_switch(KEYBOARD_NAME)) == NULL)
188137776Semax			return (ENXIO);
189137776Semax
190137776Semax		if ((error = (*sw->probe)(unit, NULL, 0)) != 0 ||
191137776Semax		    (error = (*sw->init)(unit, &kbd, NULL, 0)) != 0)
192137776Semax			return (error);
193137776Semax
194137776Semax		state = (vkbd_state_t *) kbd->kb_data;
195137776Semax
196137776Semax		if ((error = (*sw->enable)(kbd)) != 0) {
197137776Semax			(*sw->term)(kbd);
198137776Semax			return (error);
199137776Semax		}
200137776Semax
201137776Semax#ifdef KBD_INSTALL_CDEV
202137776Semax		if ((error = kbd_attach(kbd)) != 0) {
203137776Semax			(*sw->disable)(kbd);
204137776Semax			(*sw->term)(kbd);
205137776Semax			return (error);
206137776Semax		}
207137776Semax#endif /* def KBD_INSTALL_CDEV */
208137776Semax
209137776Semax		dev->si_drv1 = kbd->kb_data;
210137776Semax	}
211137776Semax
212137776Semax	VKBD_LOCK(state);
213137776Semax
214137776Semax	if (state->ks_flags & OPEN) {
215137776Semax		VKBD_UNLOCK(state);
216137776Semax		return (EBUSY);
217137776Semax	}
218137776Semax
219137776Semax	state->ks_flags |= OPEN;
220137776Semax	state->ks_dev = dev;
221137776Semax
222137776Semax	VKBD_UNLOCK(state);
223137776Semax
224137776Semax	return (0);
225137776Semax}
226137776Semax
227137776Semax/* Close device */
228137776Semaxstatic int
229137776Semaxvkbd_dev_close(struct cdev *dev, int foo, int bar, struct thread *td)
230137776Semax{
231137776Semax	keyboard_t	*kbd = VKBD_KEYBOARD(dev);
232137776Semax	vkbd_state_t	*state = NULL;
233137776Semax
234137776Semax	if (kbd == NULL)
235137776Semax		return (ENXIO);
236137776Semax
237137776Semax	if (kbd->kb_data == NULL || kbd->kb_data != dev->si_drv1)
238137776Semax		panic("%s: kbd->kb_data != dev->si_drv1\n", __func__);
239137776Semax
240137776Semax	state = (vkbd_state_t *) kbd->kb_data;
241137776Semax
242137776Semax	VKBD_LOCK(state);
243137776Semax
244137776Semax	/* wait for interrupt task */
245137776Semax	while (state->ks_flags & TASK)
246137776Semax		VKBD_SLEEP(state, ks_task, "vkbdc", 0);
247137776Semax
248137776Semax	/* wakeup poll()ers */
249137776Semax	selwakeuppri(&state->ks_rsel, PZERO + 1);
250137776Semax	selwakeuppri(&state->ks_wsel, PZERO + 1);
251137776Semax
252137776Semax	state->ks_flags &= ~OPEN;
253137776Semax	state->ks_dev = NULL;
254137776Semax	state->ks_inq.head = state->ks_inq.tail = state->ks_inq.cc = 0;
255137776Semax
256137776Semax	VKBD_UNLOCK(state);
257137776Semax
258137776Semax	(*kbdsw[kbd->kb_index]->disable)(kbd);
259137776Semax#ifdef KBD_INSTALL_CDEV
260137776Semax	kbd_detach(kbd);
261137776Semax#endif /* def KBD_INSTALL_CDEV */
262137776Semax	(*kbdsw[kbd->kb_index]->term)(kbd);
263137776Semax
264137776Semax	/* XXX FIXME: dev->si_drv1 locking */
265137776Semax	dev->si_drv1 = NULL;
266137776Semax
267137776Semax	return (0);
268137776Semax}
269137776Semax
270137776Semax/* Read status */
271137776Semaxstatic int
272137776Semaxvkbd_dev_read(struct cdev *dev, struct uio *uio, int flag)
273137776Semax{
274137776Semax	keyboard_t	*kbd = VKBD_KEYBOARD(dev);
275137776Semax	vkbd_state_t	*state = NULL;
276137776Semax	vkbd_status_t	 status;
277137776Semax	int		 error;
278137776Semax
279137776Semax	if (kbd == NULL)
280137776Semax		return (ENXIO);
281137776Semax
282137776Semax	if (uio->uio_resid != sizeof(status))
283137776Semax		return (EINVAL);
284137776Semax
285137776Semax	if (kbd->kb_data == NULL || kbd->kb_data != dev->si_drv1)
286137776Semax		panic("%s: kbd->kb_data != dev->si_drv1\n", __func__);
287137776Semax
288137776Semax	state = (vkbd_state_t *) kbd->kb_data;
289137776Semax
290137776Semax	VKBD_LOCK(state);
291137776Semax
292137776Semax	if (state->ks_flags & READ) {
293137776Semax		VKBD_UNLOCK(state);
294137776Semax		return (EALREADY);
295137776Semax	}
296137776Semax
297137776Semax	state->ks_flags |= READ;
298137776Semaxagain:
299137776Semax	if (state->ks_flags & STATUS) {
300137776Semax		state->ks_flags &= ~STATUS;
301137776Semax
302137776Semax		status.mode = state->ks_mode;
303137776Semax		status.leds = KBD_LED_VAL(kbd);
304137776Semax		status.lock = state->ks_state & LOCK_MASK;
305137776Semax		status.delay = kbd->kb_delay1;
306137776Semax		status.rate = kbd->kb_delay2;
307137776Semax		bzero(status.reserved, sizeof(status.reserved));
308137776Semax
309137776Semax		error = uiomove(&status, sizeof(status), uio);
310137776Semax	} else {
311139204Sphk		if (flag & O_NONBLOCK) {
312137776Semax			error = EWOULDBLOCK;
313137776Semax			goto done;
314137776Semax		}
315137776Semax
316137776Semax		error = VKBD_SLEEP(state, ks_flags, "vkbdr", 0);
317137776Semax		if (error != 0)
318137776Semax			goto done;
319137776Semax
320137776Semax		goto again;
321137776Semax	}
322137776Semaxdone:
323137776Semax	state->ks_flags &= ~READ;
324137776Semax
325137776Semax	VKBD_UNLOCK(state);
326137776Semax
327137776Semax	return (error);
328137776Semax}
329137776Semax
330137776Semax/* Write scancodes */
331137776Semaxstatic int
332137776Semaxvkbd_dev_write(struct cdev *dev, struct uio *uio, int flag)
333137776Semax{
334137776Semax	keyboard_t	*kbd = VKBD_KEYBOARD(dev);
335137776Semax	vkbd_state_t	*state = NULL;
336137776Semax	vkbd_queue_t	*q = NULL;
337137776Semax	int		 error, avail, bytes;
338137776Semax
339137776Semax	if (kbd == NULL)
340137776Semax		return (ENXIO);
341137776Semax
342137776Semax	if (uio->uio_resid <= 0)
343137776Semax		return (EINVAL);
344137776Semax
345137776Semax	if (kbd->kb_data == NULL || kbd->kb_data != dev->si_drv1)
346137776Semax		panic("%s: kbd->kb_data != dev->si_drv1\n", __func__);
347137776Semax
348137776Semax	state = (vkbd_state_t *) kbd->kb_data;
349137776Semax
350137776Semax	VKBD_LOCK(state);
351137776Semax
352137776Semax	if (state->ks_flags & WRITE) {
353137776Semax		VKBD_UNLOCK(state);
354137776Semax		return (EALREADY);
355137776Semax	}
356137776Semax
357137776Semax	state->ks_flags |= WRITE;
358137776Semax	error = 0;
359137776Semax	q = &state->ks_inq;
360137776Semax
361137776Semax	while (uio->uio_resid >= sizeof(q->q[0])) {
362137776Semax		if (q->head == q->tail) {
363137776Semax			if (q->cc == 0)
364137776Semax				avail = sizeof(q->q)/sizeof(q->q[0]) - q->head;
365137776Semax			else
366137776Semax				avail = 0; /* queue must be full */
367137776Semax		} else if (q->head < q->tail)
368137776Semax			avail = sizeof(q->q)/sizeof(q->q[0]) - q->tail;
369137776Semax		else
370137776Semax			avail = q->head - q->tail;
371137776Semax
372137776Semax		if (avail == 0) {
373139204Sphk			if (flag & O_NONBLOCK) {
374137776Semax				error = EWOULDBLOCK;
375137776Semax				break;
376137776Semax			}
377137776Semax
378137776Semax			error = VKBD_SLEEP(state, ks_inq, "vkbdw", 0);
379137776Semax			if (error != 0)
380137776Semax				break;
381137776Semax		} else {
382137776Semax			bytes = avail * sizeof(q->q[0]);
383137776Semax			if (bytes > uio->uio_resid) {
384137776Semax				avail = uio->uio_resid / sizeof(q->q[0]);
385137776Semax				bytes = avail * sizeof(q->q[0]);
386137776Semax			}
387137776Semax
388137776Semax			error = uiomove((void *) &q->q[q->tail], bytes, uio);
389137776Semax			if (error != 0)
390137776Semax				break;
391137776Semax
392137776Semax			q->cc += avail;
393137776Semax			q->tail += avail;
394137776Semax			if (q->tail == sizeof(q->q)/sizeof(q->q[0]))
395137776Semax				q->tail = 0;
396137776Semax
397137776Semax			/* queue interrupt task if needed */
398137776Semax			if (!(state->ks_flags & TASK) &&
399137776Semax			    taskqueue_enqueue(taskqueue_swi_giant, &state->ks_task) == 0)
400137776Semax				state->ks_flags |= TASK;
401137776Semax		}
402137776Semax	}
403137776Semax
404137776Semax	state->ks_flags &= ~WRITE;
405137776Semax
406137776Semax	VKBD_UNLOCK(state);
407137776Semax
408137776Semax	return (error);
409137776Semax}
410137776Semax
411137776Semax/* Process ioctl */
412137776Semaxstatic int
413137776Semaxvkbd_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
414137776Semax{
415137776Semax	keyboard_t	*kbd = VKBD_KEYBOARD(dev);
416137776Semax
417137776Semax	return ((kbd == NULL)? ENXIO :
418137776Semax			(*kbdsw[kbd->kb_index]->ioctl)(kbd, cmd, data));
419137776Semax}
420137776Semax
421137776Semax/* Poll device */
422137776Semaxstatic int
423137776Semaxvkbd_dev_poll(struct cdev *dev, int events, struct thread *td)
424137776Semax{
425137776Semax	vkbd_state_t	*state = (vkbd_state_t *) dev->si_drv1;
426137776Semax	vkbd_queue_t	*q = NULL;
427137776Semax	int		 revents = 0;
428137776Semax
429137776Semax	if (state == NULL)
430137776Semax		return (ENXIO);
431137776Semax
432137776Semax	VKBD_LOCK(state);
433137776Semax
434137776Semax	q = &state->ks_inq;
435137776Semax
436137776Semax	if (events & (POLLIN | POLLRDNORM)) {
437137776Semax		if (state->ks_flags & STATUS)
438137776Semax			revents |= events & (POLLIN | POLLRDNORM);
439137776Semax		else
440137776Semax			selrecord(td, &state->ks_rsel);
441137776Semax	}
442137776Semax
443137776Semax	if (events & (POLLOUT | POLLWRNORM)) {
444137776Semax		if (q->cc < sizeof(q->q)/sizeof(q->q[0]))
445137776Semax			revents |= events & (POLLOUT | POLLWRNORM);
446137776Semax		else
447137776Semax			selrecord(td, &state->ks_wsel);
448137776Semax	}
449137776Semax
450137776Semax	VKBD_UNLOCK(state);
451137776Semax
452137776Semax	return (revents);
453137776Semax}
454137776Semax
455137776Semax/* Interrupt handler */
456137776Semaxvoid
457137776Semaxvkbd_dev_intr(void *xkbd, int pending)
458137776Semax{
459137776Semax	keyboard_t	*kbd = (keyboard_t *) xkbd;
460137776Semax	vkbd_state_t	*state = (vkbd_state_t *) kbd->kb_data;
461137776Semax
462137776Semax	(*kbdsw[kbd->kb_index]->intr)(kbd, NULL);
463137776Semax
464137776Semax	VKBD_LOCK(state);
465137776Semax
466137776Semax	state->ks_flags &= ~TASK;
467137776Semax	wakeup(&state->ks_task);
468137776Semax
469137776Semax	VKBD_UNLOCK(state);
470137776Semax}
471137776Semax
472137776Semax/* Set status change flags */
473137776Semaxstatic void
474137776Semaxvkbd_status_changed(vkbd_state_t *state)
475137776Semax{
476137776Semax	VKBD_LOCK_ASSERT(state, MA_OWNED);
477137776Semax
478137776Semax	if (!(state->ks_flags & STATUS)) {
479137776Semax		state->ks_flags |= STATUS;
480137776Semax		selwakeuppri(&state->ks_rsel, PZERO + 1);
481137776Semax		wakeup(&state->ks_flags);
482137776Semax	}
483137776Semax}
484137776Semax
485137776Semax/* Check if we have data in the input queue */
486137776Semaxstatic int
487137776Semaxvkbd_data_ready(vkbd_state_t *state)
488137776Semax{
489137776Semax	VKBD_LOCK_ASSERT(state, MA_OWNED);
490137776Semax
491137776Semax	return (state->ks_inq.cc > 0);
492137776Semax}
493137776Semax
494137776Semax/* Read one code from the input queue */
495137776Semaxstatic int
496137776Semaxvkbd_data_read(vkbd_state_t *state, int wait)
497137776Semax{
498137776Semax	vkbd_queue_t	*q = &state->ks_inq;
499137776Semax	int		 c;
500137776Semax
501137776Semax	VKBD_LOCK_ASSERT(state, MA_OWNED);
502137776Semax
503137776Semax	if (q->cc == 0)
504137776Semax		return (-1);
505137776Semax
506137776Semax	/* get first code from the queue */
507137776Semax	q->cc --;
508137776Semax	c = q->q[q->head ++];
509137776Semax	if (q->head == sizeof(q->q)/sizeof(q->q[0]))
510137776Semax		q->head = 0;
511137776Semax
512137776Semax	/* wakeup ks_inq writers/poll()ers */
513137776Semax	selwakeuppri(&state->ks_wsel, PZERO + 1);
514137776Semax	wakeup(q);
515137776Semax
516137776Semax	return (c);
517137776Semax}
518137776Semax
519137776Semax/****************************************************************************
520137776Semax ****************************************************************************
521137776Semax **                              Keyboard driver
522137776Semax ****************************************************************************
523137776Semax ****************************************************************************/
524137776Semax
525137776Semaxstatic int		vkbd_configure(int flags);
526137776Semaxstatic kbd_probe_t	vkbd_probe;
527137776Semaxstatic kbd_init_t	vkbd_init;
528137776Semaxstatic kbd_term_t	vkbd_term;
529137776Semaxstatic kbd_intr_t	vkbd_intr;
530137776Semaxstatic kbd_test_if_t	vkbd_test_if;
531137776Semaxstatic kbd_enable_t	vkbd_enable;
532137776Semaxstatic kbd_disable_t	vkbd_disable;
533137776Semaxstatic kbd_read_t	vkbd_read;
534137776Semaxstatic kbd_check_t	vkbd_check;
535137776Semaxstatic kbd_read_char_t	vkbd_read_char;
536137776Semaxstatic kbd_check_char_t	vkbd_check_char;
537137776Semaxstatic kbd_ioctl_t	vkbd_ioctl;
538137776Semaxstatic kbd_lock_t	vkbd_lock;
539137776Semaxstatic void		vkbd_clear_state_locked(vkbd_state_t *state);
540137776Semaxstatic kbd_clear_state_t vkbd_clear_state;
541137776Semaxstatic kbd_get_state_t	vkbd_get_state;
542137776Semaxstatic kbd_set_state_t	vkbd_set_state;
543137776Semaxstatic kbd_poll_mode_t	vkbd_poll;
544137776Semax
545137776Semaxstatic keyboard_switch_t vkbdsw = {
546137776Semax	.probe =	vkbd_probe,
547137776Semax	.init =		vkbd_init,
548137776Semax	.term =		vkbd_term,
549137776Semax	.intr =		vkbd_intr,
550137776Semax	.test_if =	vkbd_test_if,
551137776Semax	.enable =	vkbd_enable,
552137776Semax	.disable =	vkbd_disable,
553137776Semax	.read =		vkbd_read,
554137776Semax	.check =	vkbd_check,
555137776Semax	.read_char =	vkbd_read_char,
556137776Semax	.check_char =	vkbd_check_char,
557137776Semax	.ioctl =	vkbd_ioctl,
558137776Semax	.lock =		vkbd_lock,
559137776Semax	.clear_state =	vkbd_clear_state,
560137776Semax	.get_state =	vkbd_get_state,
561137776Semax	.set_state =	vkbd_set_state,
562137776Semax	.get_fkeystr =	genkbd_get_fkeystr,
563137776Semax	.poll =		vkbd_poll,
564137776Semax	.diag =		genkbd_diag,
565137776Semax};
566137776Semax
567137776Semaxstatic int	typematic(int delay, int rate);
568137776Semaxstatic int	typematic_delay(int delay);
569137776Semaxstatic int	typematic_rate(int rate);
570137776Semax
571137776Semax/* Return the number of found keyboards */
572137776Semaxstatic int
573137776Semaxvkbd_configure(int flags)
574137776Semax{
575137776Semax	return (1);
576137776Semax}
577137776Semax
578137776Semax/* Detect a keyboard */
579137776Semaxstatic int
580137776Semaxvkbd_probe(int unit, void *arg, int flags)
581137776Semax{
582137776Semax	return (0);
583137776Semax}
584137776Semax
585137776Semax/* Reset and initialize the keyboard (stolen from atkbd.c) */
586137776Semaxstatic int
587137776Semaxvkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
588137776Semax{
589137776Semax	keyboard_t	*kbd = NULL;
590137776Semax	vkbd_state_t	*state = NULL;
591137776Semax	keymap_t	*keymap = NULL;
592137776Semax	accentmap_t	*accmap = NULL;
593137776Semax	fkeytab_t	*fkeymap = NULL;
594137776Semax	int		 fkeymap_size, delay[2];
595137776Semax
596137776Semax	if (*kbdp == NULL) {
597137776Semax		*kbdp = kbd = malloc(sizeof(*kbd), M_VKBD, M_NOWAIT | M_ZERO);
598137776Semax		state = malloc(sizeof(*state), M_VKBD, M_NOWAIT | M_ZERO);
599137776Semax		keymap = malloc(sizeof(key_map), M_VKBD, M_NOWAIT);
600137776Semax		accmap = malloc(sizeof(accent_map), M_VKBD, M_NOWAIT);
601137776Semax		fkeymap = malloc(sizeof(fkey_tab), M_VKBD, M_NOWAIT);
602137776Semax		fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
603137776Semax		if ((kbd == NULL) || (state == NULL) || (keymap == NULL) ||
604137776Semax		    (accmap == NULL) || (fkeymap == NULL)) {
605137776Semax			if (state != NULL)
606137776Semax				free(state, M_VKBD);
607137776Semax			if (keymap != NULL)
608137776Semax				free(keymap, M_VKBD);
609137776Semax			if (accmap != NULL)
610137776Semax				free(accmap, M_VKBD);
611137776Semax			if (fkeymap != NULL)
612137776Semax				free(fkeymap, M_VKBD);
613137776Semax			if (kbd != NULL)
614137776Semax				free(kbd, M_VKBD);
615137776Semax			return (ENOMEM);
616137776Semax		}
617137776Semax
618137776Semax		VKBD_LOCK_INIT(state);
619137776Semax		state->ks_inq.head = state->ks_inq.tail = state->ks_inq.cc = 0;
620137776Semax		TASK_INIT(&state->ks_task, 0, vkbd_dev_intr, (void *) kbd);
621137776Semax	} else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
622137776Semax		return (0);
623137776Semax	} else {
624137776Semax		kbd = *kbdp;
625137776Semax		state = (vkbd_state_t *) kbd->kb_data;
626137776Semax		keymap = kbd->kb_keymap;
627137776Semax		accmap = kbd->kb_accentmap;
628137776Semax		fkeymap = kbd->kb_fkeytab;
629137776Semax		fkeymap_size = kbd->kb_fkeytab_size;
630137776Semax	}
631137776Semax
632137776Semax	if (!KBD_IS_PROBED(kbd)) {
633137776Semax		kbd_init_struct(kbd, KEYBOARD_NAME, KB_OTHER, unit, flags, 0, 0);
634137776Semax		bcopy(&key_map, keymap, sizeof(key_map));
635137776Semax		bcopy(&accent_map, accmap, sizeof(accent_map));
636137776Semax		bcopy(fkey_tab, fkeymap,
637137776Semax			imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
638137776Semax		kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
639137776Semax		kbd->kb_data = (void *)state;
640137776Semax
641137776Semax		KBD_FOUND_DEVICE(kbd);
642137776Semax		KBD_PROBE_DONE(kbd);
643137776Semax
644137776Semax		VKBD_LOCK(state);
645137776Semax		vkbd_clear_state_locked(state);
646137776Semax		state->ks_mode = K_XLATE;
647137776Semax		/* FIXME: set the initial value for lock keys in ks_state */
648137776Semax		VKBD_UNLOCK(state);
649137776Semax	}
650137776Semax	if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
651137776Semax		kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
652137776Semax
653137776Semax		vkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
654137776Semax		delay[0] = kbd->kb_delay1;
655137776Semax		delay[1] = kbd->kb_delay2;
656137776Semax		vkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
657137776Semax
658137776Semax		KBD_INIT_DONE(kbd);
659137776Semax	}
660137776Semax	if (!KBD_IS_CONFIGURED(kbd)) {
661137776Semax		if (kbd_register(kbd) < 0)
662137776Semax			return (ENXIO);
663137776Semax		KBD_CONFIG_DONE(kbd);
664137776Semax	}
665137776Semax
666137776Semax	return (0);
667137776Semax}
668137776Semax
669137776Semax/* Finish using this keyboard */
670137776Semaxstatic int
671137776Semaxvkbd_term(keyboard_t *kbd)
672137776Semax{
673137776Semax	vkbd_state_t	*state = (vkbd_state_t *) kbd->kb_data;
674137776Semax
675137776Semax	kbd_unregister(kbd);
676137776Semax
677137776Semax	VKBD_LOCK_DESTROY(state);
678137776Semax	bzero(state, sizeof(*state));
679137776Semax	free(state, M_VKBD);
680137776Semax
681137776Semax	free(kbd->kb_keymap, M_VKBD);
682137776Semax	free(kbd->kb_accentmap, M_VKBD);
683137776Semax	free(kbd->kb_fkeytab, M_VKBD);
684137776Semax	free(kbd, M_VKBD);
685137776Semax
686137776Semax	return (0);
687137776Semax}
688137776Semax
689137776Semax/* Keyboard interrupt routine */
690137776Semaxstatic int
691137776Semaxvkbd_intr(keyboard_t *kbd, void *arg)
692137776Semax{
693137776Semax	int	c;
694137776Semax
695137776Semax	if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
696137776Semax		/* let the callback function to process the input */
697137776Semax		(*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
698137776Semax					    kbd->kb_callback.kc_arg);
699137776Semax	} else {
700137776Semax		/* read and discard the input; no one is waiting for input */
701137776Semax		do {
702137776Semax			c = vkbd_read_char(kbd, FALSE);
703137776Semax		} while (c != NOKEY);
704137776Semax	}
705137776Semax
706137776Semax	return (0);
707137776Semax}
708137776Semax
709137776Semax/* Test the interface to the device */
710137776Semaxstatic int
711137776Semaxvkbd_test_if(keyboard_t *kbd)
712137776Semax{
713137776Semax	return (0);
714137776Semax}
715137776Semax
716137776Semax/*
717137776Semax * Enable the access to the device; until this function is called,
718137776Semax * the client cannot read from the keyboard.
719137776Semax */
720137776Semax
721137776Semaxstatic int
722137776Semaxvkbd_enable(keyboard_t *kbd)
723137776Semax{
724137776Semax	KBD_ACTIVATE(kbd);
725137776Semax	return (0);
726137776Semax}
727137776Semax
728137776Semax/* Disallow the access to the device */
729137776Semaxstatic int
730137776Semaxvkbd_disable(keyboard_t *kbd)
731137776Semax{
732137776Semax	KBD_DEACTIVATE(kbd);
733137776Semax	return (0);
734137776Semax}
735137776Semax
736137776Semax/* Read one byte from the keyboard if it's allowed */
737137776Semaxstatic int
738137776Semaxvkbd_read(keyboard_t *kbd, int wait)
739137776Semax{
740137776Semax	vkbd_state_t	*state = (vkbd_state_t *) kbd->kb_data;
741137776Semax	int		 c;
742137776Semax
743137776Semax	VKBD_LOCK(state);
744137776Semax	c = vkbd_data_read(state, wait);
745137776Semax	VKBD_UNLOCK(state);
746137776Semax
747137776Semax	if (c != -1)
748137776Semax		kbd->kb_count ++;
749137776Semax
750137776Semax	return (KBD_IS_ACTIVE(kbd)? c : -1);
751137776Semax}
752137776Semax
753137776Semax/* Check if data is waiting */
754137776Semaxstatic int
755137776Semaxvkbd_check(keyboard_t *kbd)
756137776Semax{
757137776Semax	vkbd_state_t	*state = NULL;
758137776Semax	int		 ready;
759137776Semax
760137776Semax	if (!KBD_IS_ACTIVE(kbd))
761137776Semax		return (FALSE);
762137776Semax
763137776Semax	state = (vkbd_state_t *) kbd->kb_data;
764137776Semax
765137776Semax	VKBD_LOCK(state);
766137776Semax	ready = vkbd_data_ready(state);
767137776Semax	VKBD_UNLOCK(state);
768137776Semax
769137776Semax	return (ready);
770137776Semax}
771137776Semax
772137776Semax/* Read char from the keyboard (stolen from atkbd.c) */
773137776Semaxstatic u_int
774137776Semaxvkbd_read_char(keyboard_t *kbd, int wait)
775137776Semax{
776137776Semax	vkbd_state_t	*state = (vkbd_state_t *) kbd->kb_data;
777137776Semax	u_int		 action;
778137776Semax	int		 scancode, keycode;
779137776Semax
780137776Semax	VKBD_LOCK(state);
781137776Semax
782137776Semaxnext_code:
783137776Semax
784137776Semax	/* do we have a composed char to return? */
785137776Semax	if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
786137776Semax		action = state->ks_composed_char;
787137776Semax		state->ks_composed_char = 0;
788137776Semax		if (action > UCHAR_MAX) {
789137776Semax			VKBD_UNLOCK(state);
790137776Semax			return (ERRKEY);
791137776Semax		}
792137776Semax
793137776Semax		VKBD_UNLOCK(state);
794137776Semax		return (action);
795137776Semax	}
796137776Semax
797137776Semax	/* see if there is something in the keyboard port */
798137776Semax	scancode = vkbd_data_read(state, wait);
799137776Semax	if (scancode == -1) {
800137776Semax		VKBD_UNLOCK(state);
801137776Semax		return (NOKEY);
802137776Semax	}
803137776Semax	/* XXX FIXME: check for -1 if wait == 1! */
804137776Semax
805137776Semax	kbd->kb_count ++;
806137776Semax
807137776Semax	/* return the byte as is for the K_RAW mode */
808137776Semax	if (state->ks_mode == K_RAW) {
809137776Semax		VKBD_UNLOCK(state);
810137776Semax		return (scancode);
811137776Semax	}
812137776Semax
813137776Semax	/* translate the scan code into a keycode */
814137776Semax	keycode = scancode & 0x7F;
815137776Semax	switch (state->ks_prefix) {
816137776Semax	case 0x00:	/* normal scancode */
817137776Semax		switch(scancode) {
818137776Semax		case 0xB8:	/* left alt (compose key) released */
819137776Semax			if (state->ks_flags & COMPOSE) {
820137776Semax				state->ks_flags &= ~COMPOSE;
821137776Semax				if (state->ks_composed_char > UCHAR_MAX)
822137776Semax					state->ks_composed_char = 0;
823137776Semax			}
824137776Semax			break;
825137776Semax		case 0x38:	/* left alt (compose key) pressed */
826137776Semax			if (!(state->ks_flags & COMPOSE)) {
827137776Semax				state->ks_flags |= COMPOSE;
828137776Semax				state->ks_composed_char = 0;
829137776Semax			}
830137776Semax			break;
831137776Semax		case 0xE0:
832137776Semax		case 0xE1:
833137776Semax			state->ks_prefix = scancode;
834137776Semax			goto next_code;
835137776Semax		}
836137776Semax		break;
837137776Semax	case 0xE0:      /* 0xE0 prefix */
838137776Semax		state->ks_prefix = 0;
839137776Semax		switch (keycode) {
840137776Semax		case 0x1C:	/* right enter key */
841137776Semax			keycode = 0x59;
842137776Semax			break;
843137776Semax		case 0x1D:	/* right ctrl key */
844137776Semax			keycode = 0x5A;
845137776Semax			break;
846137776Semax		case 0x35:	/* keypad divide key */
847137776Semax			keycode = 0x5B;
848137776Semax			break;
849137776Semax		case 0x37:	/* print scrn key */
850137776Semax			keycode = 0x5C;
851137776Semax			break;
852137776Semax		case 0x38:	/* right alt key (alt gr) */
853137776Semax			keycode = 0x5D;
854137776Semax			break;
855137776Semax		case 0x46:	/* ctrl-pause/break on AT 101 (see below) */
856137776Semax			keycode = 0x68;
857137776Semax			break;
858137776Semax		case 0x47:	/* grey home key */
859137776Semax			keycode = 0x5E;
860137776Semax			break;
861137776Semax		case 0x48:	/* grey up arrow key */
862137776Semax			keycode = 0x5F;
863137776Semax			break;
864137776Semax		case 0x49:	/* grey page up key */
865137776Semax			keycode = 0x60;
866137776Semax			break;
867137776Semax		case 0x4B:	/* grey left arrow key */
868137776Semax			keycode = 0x61;
869137776Semax			break;
870137776Semax		case 0x4D:	/* grey right arrow key */
871137776Semax			keycode = 0x62;
872137776Semax			break;
873137776Semax		case 0x4F:	/* grey end key */
874137776Semax			keycode = 0x63;
875137776Semax			break;
876137776Semax		case 0x50:	/* grey down arrow key */
877137776Semax			keycode = 0x64;
878137776Semax			break;
879137776Semax		case 0x51:	/* grey page down key */
880137776Semax			keycode = 0x65;
881137776Semax			break;
882137776Semax		case 0x52:	/* grey insert key */
883137776Semax			keycode = 0x66;
884137776Semax			break;
885137776Semax		case 0x53:	/* grey delete key */
886137776Semax			keycode = 0x67;
887137776Semax			break;
888137776Semax		/* the following 3 are only used on the MS "Natural" keyboard */
889137776Semax		case 0x5b:	/* left Window key */
890137776Semax			keycode = 0x69;
891137776Semax			break;
892137776Semax		case 0x5c:	/* right Window key */
893137776Semax			keycode = 0x6a;
894137776Semax			break;
895137776Semax		case 0x5d:	/* menu key */
896137776Semax			keycode = 0x6b;
897137776Semax			break;
898137776Semax		case 0x5e:	/* power key */
899137776Semax			keycode = 0x6d;
900137776Semax			break;
901137776Semax		case 0x5f:	/* sleep key */
902137776Semax			keycode = 0x6e;
903137776Semax			break;
904137776Semax		case 0x63:	/* wake key */
905137776Semax			keycode = 0x6f;
906137776Semax			break;
907137776Semax		default:	/* ignore everything else */
908137776Semax			goto next_code;
909137776Semax		}
910137776Semax		break;
911137776Semax	case 0xE1:	/* 0xE1 prefix */
912137776Semax		/*
913137776Semax		 * The pause/break key on the 101 keyboard produces:
914137776Semax		 * E1-1D-45 E1-9D-C5
915137776Semax		 * Ctrl-pause/break produces:
916137776Semax		 * E0-46 E0-C6 (See above.)
917137776Semax		 */
918137776Semax		state->ks_prefix = 0;
919137776Semax		if (keycode == 0x1D)
920137776Semax			state->ks_prefix = 0x1D;
921137776Semax		goto next_code;
922137776Semax		/* NOT REACHED */
923137776Semax	case 0x1D:	/* pause / break */
924137776Semax		state->ks_prefix = 0;
925137776Semax		if (keycode != 0x45)
926137776Semax			goto next_code;
927137776Semax		keycode = 0x68;
928137776Semax		break;
929137776Semax	}
930137776Semax
931137776Semax	if (kbd->kb_type == KB_84) {
932137776Semax		switch (keycode) {
933137776Semax		case 0x37:	/* *(numpad)/print screen */
934137776Semax			if (state->ks_flags & SHIFTS)
935137776Semax				keycode = 0x5c;	/* print screen */
936137776Semax			break;
937137776Semax		case 0x45:	/* num lock/pause */
938137776Semax			if (state->ks_flags & CTLS)
939137776Semax				keycode = 0x68;	/* pause */
940137776Semax			break;
941137776Semax		case 0x46:	/* scroll lock/break */
942137776Semax			if (state->ks_flags & CTLS)
943137776Semax				keycode = 0x6c;	/* break */
944137776Semax			break;
945137776Semax		}
946137776Semax	} else if (kbd->kb_type == KB_101) {
947137776Semax		switch (keycode) {
948137776Semax		case 0x5c:	/* print screen */
949137776Semax			if (state->ks_flags & ALTS)
950137776Semax				keycode = 0x54;	/* sysrq */
951137776Semax			break;
952137776Semax		case 0x68:	/* pause/break */
953137776Semax			if (state->ks_flags & CTLS)
954137776Semax				keycode = 0x6c;	/* break */
955137776Semax			break;
956137776Semax		}
957137776Semax	}
958137776Semax
959137776Semax	/* return the key code in the K_CODE mode */
960137776Semax	if (state->ks_mode == K_CODE) {
961137776Semax		VKBD_UNLOCK(state);
962137776Semax		return (keycode | (scancode & 0x80));
963137776Semax	}
964137776Semax
965137776Semax	/* compose a character code */
966137776Semax	if (state->ks_flags & COMPOSE) {
967137776Semax		switch (keycode | (scancode & 0x80)) {
968137776Semax		/* key pressed, process it */
969137776Semax		case 0x47: case 0x48: case 0x49:	/* keypad 7,8,9 */
970137776Semax			state->ks_composed_char *= 10;
971137776Semax			state->ks_composed_char += keycode - 0x40;
972137776Semax			if (state->ks_composed_char > UCHAR_MAX) {
973137776Semax				VKBD_UNLOCK(state);
974137776Semax				return (ERRKEY);
975137776Semax			}
976137776Semax			goto next_code;
977137776Semax		case 0x4B: case 0x4C: case 0x4D:	/* keypad 4,5,6 */
978137776Semax			state->ks_composed_char *= 10;
979137776Semax			state->ks_composed_char += keycode - 0x47;
980137776Semax			if (state->ks_composed_char > UCHAR_MAX) {
981137776Semax				VKBD_UNLOCK(state);
982137776Semax				return (ERRKEY);
983137776Semax			}
984137776Semax			goto next_code;
985137776Semax		case 0x4F: case 0x50: case 0x51:	/* keypad 1,2,3 */
986137776Semax			state->ks_composed_char *= 10;
987137776Semax			state->ks_composed_char += keycode - 0x4E;
988137776Semax			if (state->ks_composed_char > UCHAR_MAX) {
989137776Semax				VKBD_UNLOCK(state);
990137776Semax				return (ERRKEY);
991137776Semax			}
992137776Semax			goto next_code;
993137776Semax		case 0x52:	/* keypad 0 */
994137776Semax			state->ks_composed_char *= 10;
995137776Semax			if (state->ks_composed_char > UCHAR_MAX) {
996137776Semax				VKBD_UNLOCK(state);
997137776Semax				return (ERRKEY);
998137776Semax			}
999137776Semax			goto next_code;
1000137776Semax
1001137776Semax		/* key released, no interest here */
1002137776Semax		case 0xC7: case 0xC8: case 0xC9:	/* keypad 7,8,9 */
1003137776Semax		case 0xCB: case 0xCC: case 0xCD:	/* keypad 4,5,6 */
1004137776Semax		case 0xCF: case 0xD0: case 0xD1:	/* keypad 1,2,3 */
1005137776Semax		case 0xD2:				/* keypad 0 */
1006137776Semax			goto next_code;
1007137776Semax
1008137776Semax		case 0x38:				/* left alt key */
1009137776Semax			break;
1010137776Semax
1011137776Semax		default:
1012137776Semax			if (state->ks_composed_char > 0) {
1013137776Semax				state->ks_flags &= ~COMPOSE;
1014137776Semax				state->ks_composed_char = 0;
1015137776Semax				VKBD_UNLOCK(state);
1016137776Semax				return (ERRKEY);
1017137776Semax			}
1018137776Semax			break;
1019137776Semax		}
1020137776Semax	}
1021137776Semax
1022137776Semax	/* keycode to key action */
1023137776Semax	action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
1024137776Semax			&state->ks_state, &state->ks_accents);
1025137776Semax	if (action == NOKEY)
1026137776Semax		goto next_code;
1027137776Semax
1028137776Semax	VKBD_UNLOCK(state);
1029137776Semax
1030137776Semax	return (action);
1031137776Semax}
1032137776Semax
1033137776Semax/* Check if char is waiting */
1034137776Semaxstatic int
1035137776Semaxvkbd_check_char(keyboard_t *kbd)
1036137776Semax{
1037137776Semax	vkbd_state_t	*state = NULL;
1038137776Semax	int		 ready;
1039137776Semax
1040137776Semax	if (!KBD_IS_ACTIVE(kbd))
1041137776Semax		return (FALSE);
1042137776Semax
1043137776Semax	state = (vkbd_state_t *) kbd->kb_data;
1044137776Semax
1045137776Semax	VKBD_LOCK(state);
1046137776Semax	if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
1047137776Semax		ready = TRUE;
1048137776Semax	else
1049137776Semax		ready = vkbd_data_ready(state);
1050137776Semax	VKBD_UNLOCK(state);
1051137776Semax
1052137776Semax	return (ready);
1053137776Semax}
1054137776Semax
1055137776Semax/* Some useful control functions (stolen from atkbd.c) */
1056137776Semaxstatic int
1057137776Semaxvkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1058137776Semax{
1059137776Semax	vkbd_state_t	*state = (vkbd_state_t *) kbd->kb_data;
1060137776Semax	int		 i;
1061137776Semax
1062137776Semax	VKBD_LOCK(state);
1063137776Semax
1064137776Semax	switch (cmd) {
1065137776Semax	case KDGKBMODE:		/* get keyboard mode */
1066137776Semax		*(int *)arg = state->ks_mode;
1067137776Semax		break;
1068137776Semax
1069137776Semax	case KDSKBMODE:		/* set keyboard mode */
1070137776Semax		switch (*(int *)arg) {
1071137776Semax		case K_XLATE:
1072137776Semax			if (state->ks_mode != K_XLATE) {
1073137776Semax				/* make lock key state and LED state match */
1074137776Semax				state->ks_state &= ~LOCK_MASK;
1075137776Semax				state->ks_state |= KBD_LED_VAL(kbd);
1076137776Semax				vkbd_status_changed(state);
1077137776Semax			}
1078137776Semax			/* FALLTHROUGH */
1079137776Semax
1080137776Semax		case K_RAW:
1081137776Semax		case K_CODE:
1082137776Semax			if (state->ks_mode != *(int *)arg) {
1083137776Semax				vkbd_clear_state_locked(state);
1084137776Semax				state->ks_mode = *(int *)arg;
1085137776Semax				vkbd_status_changed(state);
1086137776Semax			}
1087137776Semax			break;
1088137776Semax
1089137776Semax		default:
1090137776Semax			VKBD_UNLOCK(state);
1091137776Semax			return (EINVAL);
1092137776Semax		}
1093137776Semax		break;
1094137776Semax
1095137776Semax	case KDGETLED:		/* get keyboard LED */
1096137776Semax		*(int *)arg = KBD_LED_VAL(kbd);
1097137776Semax		break;
1098137776Semax
1099137776Semax	case KDSETLED:		/* set keyboard LED */
1100137776Semax		/* NOTE: lock key state in ks_state won't be changed */
1101137776Semax		if (*(int *)arg & ~LOCK_MASK) {
1102137776Semax			VKBD_UNLOCK(state);
1103137776Semax			return (EINVAL);
1104137776Semax		}
1105137776Semax
1106137776Semax		i = *(int *)arg;
1107137776Semax		/* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1108137776Semax		if (state->ks_mode == K_XLATE &&
1109137776Semax		    kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1110137776Semax			if (i & ALKED)
1111137776Semax				i |= CLKED;
1112137776Semax			else
1113137776Semax				i &= ~CLKED;
1114137776Semax		}
1115137776Semax
1116137776Semax		KBD_LED_VAL(kbd) = *(int *)arg;
1117137776Semax		vkbd_status_changed(state);
1118137776Semax		break;
1119137776Semax
1120137776Semax	case KDGKBSTATE:	/* get lock key state */
1121137776Semax		*(int *)arg = state->ks_state & LOCK_MASK;
1122137776Semax		break;
1123137776Semax
1124137776Semax	case KDSKBSTATE:	/* set lock key state */
1125137776Semax		if (*(int *)arg & ~LOCK_MASK) {
1126137776Semax			VKBD_UNLOCK(state);
1127137776Semax			return (EINVAL);
1128137776Semax		}
1129137776Semax		state->ks_state &= ~LOCK_MASK;
1130137776Semax		state->ks_state |= *(int *)arg;
1131137776Semax		vkbd_status_changed(state);
1132137776Semax		VKBD_UNLOCK(state);
1133137776Semax		/* set LEDs and quit */
1134137776Semax		return (vkbd_ioctl(kbd, KDSETLED, arg));
1135137776Semax
1136137776Semax	case KDSETREPEAT:	/* set keyboard repeat rate (new interface) */
1137137776Semax		i = typematic(((int *)arg)[0], ((int *)arg)[1]);
1138137776Semax		kbd->kb_delay1 = typematic_delay(i);
1139137776Semax		kbd->kb_delay2 = typematic_rate(i);
1140137776Semax		vkbd_status_changed(state);
1141137776Semax		break;
1142137776Semax
1143137776Semax	case KDSETRAD:		/* set keyboard repeat rate (old interface) */
1144137776Semax		kbd->kb_delay1 = typematic_delay(*(int *)arg);
1145137776Semax		kbd->kb_delay2 = typematic_rate(*(int *)arg);
1146137776Semax		vkbd_status_changed(state);
1147137776Semax		break;
1148137776Semax
1149137776Semax	case PIO_KEYMAP:	/* set keyboard translation table */
1150137776Semax	case PIO_KEYMAPENT:	/* set keyboard translation table entry */
1151137776Semax	case PIO_DEADKEYMAP:	/* set accent key translation table */
1152137776Semax		state->ks_accents = 0;
1153137776Semax		/* FALLTHROUGH */
1154137776Semax
1155137776Semax	default:
1156137776Semax		VKBD_UNLOCK(state);
1157137776Semax		return (genkbd_commonioctl(kbd, cmd, arg));
1158137776Semax	}
1159137776Semax
1160137776Semax	VKBD_UNLOCK(state);
1161137776Semax
1162137776Semax	return (0);
1163137776Semax}
1164137776Semax
1165137776Semax/* Lock the access to the keyboard */
1166137776Semaxstatic int
1167137776Semaxvkbd_lock(keyboard_t *kbd, int lock)
1168137776Semax{
1169137776Semax	return (1); /* XXX */
1170137776Semax}
1171137776Semax
1172137776Semax/* Clear the internal state of the keyboard */
1173137776Semaxstatic void
1174137776Semaxvkbd_clear_state_locked(vkbd_state_t *state)
1175137776Semax{
1176137776Semax	VKBD_LOCK_ASSERT(state, MA_OWNED);
1177137776Semax
1178137776Semax	state->ks_flags = 0;
1179137776Semax	state->ks_polling = 0;
1180137776Semax	state->ks_state &= LOCK_MASK;	/* preserve locking key state */
1181137776Semax	state->ks_accents = 0;
1182137776Semax	state->ks_composed_char = 0;
1183137776Semax/*	state->ks_prefix = 0;		XXX */
1184137776Semax
1185137776Semax	/* flush ks_inq and wakeup writers/poll()ers */
1186137776Semax	state->ks_inq.head = state->ks_inq.tail = state->ks_inq.cc = 0;
1187137776Semax	selwakeuppri(&state->ks_wsel, PZERO + 1);
1188137776Semax	wakeup(&state->ks_inq);
1189137776Semax}
1190137776Semax
1191137776Semaxstatic void
1192137776Semaxvkbd_clear_state(keyboard_t *kbd)
1193137776Semax{
1194137776Semax	vkbd_state_t	*state = (vkbd_state_t *) kbd->kb_data;
1195137776Semax
1196137776Semax	VKBD_LOCK(state);
1197137776Semax	vkbd_clear_state_locked(state);
1198137776Semax	VKBD_UNLOCK(state);
1199137776Semax}
1200137776Semax
1201137776Semax/* Save the internal state */
1202137776Semaxstatic int
1203137776Semaxvkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1204137776Semax{
1205137776Semax	if (len == 0)
1206137776Semax		return (sizeof(vkbd_state_t));
1207137776Semax	if (len < sizeof(vkbd_state_t))
1208137776Semax		return (-1);
1209137776Semax	bcopy(kbd->kb_data, buf, sizeof(vkbd_state_t)); /* XXX locking? */
1210137776Semax	return (0);
1211137776Semax}
1212137776Semax
1213137776Semax/* Set the internal state */
1214137776Semaxstatic int
1215137776Semaxvkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1216137776Semax{
1217137776Semax	if (len < sizeof(vkbd_state_t))
1218137776Semax		return (ENOMEM);
1219137776Semax	bcopy(buf, kbd->kb_data, sizeof(vkbd_state_t)); /* XXX locking? */
1220137776Semax	return (0);
1221137776Semax}
1222137776Semax
1223137776Semax/* Set polling */
1224137776Semaxstatic int
1225137776Semaxvkbd_poll(keyboard_t *kbd, int on)
1226137776Semax{
1227137776Semax	vkbd_state_t	*state = NULL;
1228137776Semax
1229137776Semax	state = (vkbd_state_t *) kbd->kb_data;
1230137776Semax
1231137776Semax	VKBD_LOCK(state);
1232137776Semax
1233137776Semax	if (on)
1234137776Semax		state->ks_polling ++;
1235137776Semax	else
1236137776Semax		state->ks_polling --;
1237137776Semax
1238137776Semax	VKBD_UNLOCK(state);
1239137776Semax
1240137776Semax	return (0);
1241137776Semax}
1242137776Semax
1243137776Semax/*
1244137776Semax * Local functions
1245137776Semax */
1246137776Semax
1247137776Semaxstatic int delays[] = { 250, 500, 750, 1000 };
1248137776Semaxstatic int rates[] = {	34,  38,  42,  46,  50,  55,  59,  63,
1249137776Semax			68,  76,  84,  92, 100, 110, 118, 126,
1250137776Semax			136, 152, 168, 184, 200, 220, 236, 252,
1251137776Semax			272, 304, 336, 368, 400, 440, 472, 504 };
1252137776Semax
1253137776Semaxstatic int
1254137776Semaxtypematic_delay(int i)
1255137776Semax{
1256137776Semax	return (delays[(i >> 5) & 3]);
1257137776Semax}
1258137776Semax
1259137776Semaxstatic int
1260137776Semaxtypematic_rate(int i)
1261137776Semax{
1262137776Semax	return (rates[i & 0x1f]);
1263137776Semax}
1264137776Semax
1265137776Semaxstatic int
1266137776Semaxtypematic(int delay, int rate)
1267137776Semax{
1268137776Semax	int value;
1269137776Semax	int i;
1270137776Semax
1271137776Semax	for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; i --) {
1272137776Semax		if (delay >= delays[i])
1273137776Semax			break;
1274137776Semax	}
1275137776Semax	value = i << 5;
1276137776Semax	for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; i --) {
1277137776Semax		if (rate >= rates[i])
1278137776Semax			break;
1279137776Semax	}
1280137776Semax	value |= i;
1281137776Semax	return (value);
1282137776Semax}
1283137776Semax
1284137776Semax/*****************************************************************************
1285137776Semax *****************************************************************************
1286137776Semax **                                    Module
1287137776Semax *****************************************************************************
1288137776Semax *****************************************************************************/
1289137776Semax
1290137776SemaxKEYBOARD_DRIVER(vkbd, vkbdsw, vkbd_configure);
1291137776Semax
1292137776Semaxstatic int
1293137776Semaxvkbd_modevent(module_t mod, int type, void *data)
1294137776Semax{
1295137776Semax	static eventhandler_tag	tag;
1296137776Semax
1297137776Semax	switch (type) {
1298137776Semax	case MOD_LOAD:
1299137776Semax		clone_setup(&vkbd_dev_clones);
1300137776Semax		tag = EVENTHANDLER_REGISTER(dev_clone, vkbd_dev_clone, 0, 1000);
1301137776Semax		if (tag == NULL) {
1302137776Semax			clone_cleanup(&vkbd_dev_clones);
1303137776Semax			return (ENOMEM);
1304137776Semax		}
1305137776Semax		kbd_add_driver(&vkbd_kbd_driver);
1306137776Semax		break;
1307137776Semax
1308137776Semax	case MOD_UNLOAD:
1309137776Semax		kbd_delete_driver(&vkbd_kbd_driver);
1310137776Semax		EVENTHANDLER_DEREGISTER(dev_clone, tag);
1311137776Semax		clone_cleanup(&vkbd_dev_clones);
1312137776Semax		break;
1313137776Semax
1314137776Semax	default:
1315137776Semax		return (EOPNOTSUPP);
1316137776Semax	}
1317137776Semax
1318137776Semax	return (0);
1319137776Semax}
1320137776Semax
1321137776SemaxDEV_MODULE(vkbd, vkbd_modevent, NULL);
1322137776Semax
1323