Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 13 Jun 2005 11:19:28 +0200
From:      "Norbert Koch" <NKoch@demig.de>
To:        "Maksim Yevmenkin" <maksim.yevmenkin@savvis.net>
Cc:        "Freebsd-Hackers@Freebsd. Org" <freebsd-hackers@freebsd.org>
Subject:   RE: using vkbd device
Message-ID:  <000001c56ff8$ffda5b40$4801a8c0@ws-ew-3.W2KDEMIG>
In-Reply-To: <42A0891F.8080800@savvis.net>

index | next in thread | previous in thread | raw e-mail

[-- Attachment #1 --]
> you also might want to look at experimental keyboard mux drivers. it is 
> based on vkbd(4) and uses the idea of one super-keyboard that consumes 
> scancodes from other keyboards. there are still a few issues i need to 
> fix, but, in general, it works.
> 
> http://www.geocities.com/m_evmenkin/kbdmux-2.tar.gz
> 
> thanks,
> max
> 

Hello Max,

I back-ported kbdmux to FreeBSD 4.11 (and stopped work on vkbd).
It seems to work, although I still have some stability issues
with dynamically attaching/detaching a usb keyboard.
For your reference I have enclosed a patch file.
Sorry for some diffs due to slightly different coding
and debugging style.

Thank you for any comments,

Norbert
[-- Attachment #2 --]
--- kbdmux/kbdmux.c	Sat May 21 01:51:47 2005
+++ kbdmux.c	Mon Jun 13 11:08:18 2005
@@ -27,7 +27,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $Id$
+ * $Id: kbdmux.c,v 1.4 2005/06/13 09:06:30 nk Exp $
  * $FreeBSD$
  */
 
@@ -36,25 +36,108 @@
 #include <sys/param.h>
 #include <sys/conf.h>
 #include <sys/consio.h>
+#if __FreeBSD_version >= 600000
 #include <sys/fcntl.h>
+#else
+#include <sys/vnode.h>
+#endif
 #include <sys/kbio.h>
 #include <sys/kernel.h>
+#if __FreeBSD_version >= 500000
 #include <sys/limits.h>
+#else
+#include <machine/limits.h>
+#endif
 #include <sys/lock.h>
 #include <sys/malloc.h>
 #include <sys/module.h>
+#if __FreeBSD_version >= 500014
 #include <sys/mutex.h>
+#endif
 #include <sys/poll.h>
 #include <sys/proc.h>
 #include <sys/queue.h>
+#if __FreeBSD_version >= 500014
 #include <sys/selinfo.h>
+#else
+#include <sys/select.h>
+#endif
 #include <sys/systm.h>
 #include <sys/taskqueue.h>
 #include <sys/tty.h>
 #include <sys/uio.h>
 #include <dev/kbd/kbdreg.h>
 #include <dev/kbd/kbdtables.h>
-#include <dev/kbdmux/kbdmux_var.h>
+#include "kbdmux_var.h"
+
+#ifndef DEBUG
+#define DEBUG           1
+#endif
+#ifndef VERBOSE
+#define VERBOSE         1
+#endif
+#ifndef DEBUG_USESYSLOG
+#define DEBUG_USESYSLOG 0
+#endif
+#define DEBUG_TAG       vkbd
+
+#define DEBUG_QUEUE     DEBUG
+
+#include <dev_debug/dev_debug.h>
+
+
+/*
+ * declare uint sysctl debug.vkbd_debug
+ * with initial value of 9
+ */
+
+debug_var_decl(9);
+debug_sysctl();
+
+
+/*
+ * OS specific
+ */
+
+#define PPRIO                   (PZERO+1)
+
+#if __FreeBSD_version >= 500014
+
+#define TASKQUEUE               taskqueue_swi_giant
+
+#define KBDMUX_LOCK_DECL_GLOBAL struct mtx_lock ks_lock
+#define KBDMUX_LOCL_DECL_LOCAL	/* only for FreeBSD 4.X */
+#define KBDMUX_LOCK_INIT(s)     mtx_init (& (s)->ks_lock, NULL, NULL, MTX_DEF)
+#define KBDMUX_LOCK_DESTROY(s)  mtx_destroy (& (s)->ks_lock)
+#define KBDMUX_LOCK(s)          mtx_lock (& (s)->ks_lock)
+#define KBDMUX_UNLOCK(s)        mtx_unlock (& (s)->ks_lock)
+#define KBDMUX_LOCK_ASSERT(s, w)mtx_assert (& (s)->ks_lock, w)
+#define KBDMUX_SLEEP(s, f, d, t, e)  \
+  e = msleep (& (s)->f, & (s)->ks_lock, PCATCH|PPRIO, d, t)
+
+#else
+
+#define TASKQUEUE               taskqueue_swi
+
+#define KBDMUX_LOCK_DECL_GLOBAL	/* only for FreeBSD 5.X and up */
+#define KBDMUX_LOCK_DECL_LOCAL  intrmask_t ipl
+#define KBDMUX_LOCK_INIT(s)	/* only for FreeBSD 5.X and up */
+#define KBDMUX_LOCK_DESTROY(s)	/* only for FreeBSD 5.X and up */
+#define KBDMUX_LOCK(s)          ipl = splhigh ()
+#define KBDMUX_UNLOCK(s)        splx (ipl)
+#define KBDMUX_LOCK_ASSERT(s, w)	/* only by FreeBSD 5.X and up
+					 * supported */
+#define KBDMUX_SLEEP(s, f, d, t)                \
+  do                                            \
+  {                                             \
+    KBDMUX_UNLOCK (s);                          \
+    tsleep (& (s)->f, PCATCH|PPRIO, d, t);      \
+    KBDMUX_LOCK (s);                            \
+  }                                             \
+  while (0)
+
+#endif
+
 
 #define KEYBOARD_NAME	"kbdmux"
 
@@ -67,15 +150,6 @@
  *****************************************************************************
  *****************************************************************************/
 
-#define KBDMUX_LOCK_DECL	struct mtx ks_lock
-#define KBDMUX_LOCK_INIT(s)	mtx_init(&(s)->ks_lock, NULL, NULL, MTX_DEF)
-#define KBDMUX_LOCK_DESTROY(s)	mtx_destroy(&(s)->ks_lock)
-#define KBDMUX_LOCK(s)		mtx_lock(&(s)->ks_lock)
-#define KBDMUX_UNLOCK(s)	mtx_unlock(&(s)->ks_lock)
-#define KBDMUX_LOCK_ASSERT(s, w)	mtx_assert(&(s)->ks_lock, w)
-#define KBDMUX_SLEEP(s, f, d, t) \
-	msleep(&(s)->f, &(s)->ks_lock, PCATCH | (PZERO + 1), d, t)
-
 #define	KBDMUX_INTR(kbd, arg) \
 	(*kbdsw[(kbd)->kb_index]->intr)((kbd), (arg))
 
@@ -91,9 +165,10 @@
 #define	KBDMUX_ENABLE(kbd) \
 	(*kbdsw[(kbd)->kb_index]->enable)((kbd))
 
-/* kbdmux keyboard */
-struct kbdmux_kbd
-{
+/*
+ * kbdmux keyboard 
+ */
+struct kbdmux_kbd {
 	keyboard_t		*kbd;	/* keyboard */
 	int			 idx;	/* keyboard index */
 	SLIST_ENTRY(kbdmux_kbd)	 next;	/* link to next */
@@ -101,9 +176,10 @@
 
 typedef struct kbdmux_kbd	kbdmux_kbd_t;
 
-/* kbdmux state */
-struct kbdmux_state
-{
+/*
+ * kbdmux state 
+ */
+struct kbdmux_state {
 	struct clist		 ks_inq;	/* input chars queue */
 	struct task		 ks_task;	/* interrupt task */
 
@@ -120,7 +196,7 @@
 
 	SLIST_HEAD(, kbdmux_kbd) ks_kbds;	/* keyboards */
 
-	KBDMUX_LOCK_DECL;
+	                KBDMUX_LOCK_DECL_GLOBAL;
 };
 
 typedef struct kbdmux_state	kbdmux_state_t;
@@ -134,12 +210,15 @@
 static void			kbdmux_kbd_intr(void *, int);
 static kbd_callback_func_t	kbdmux_kbd_event;
 
-/* Interrupt handler task */
+/*
+ * Interrupt handler task 
+ */
 void
 kbdmux_kbd_intr(void *xkbd, int pending)
 {
 	keyboard_t	*kbd = (keyboard_t *) xkbd;
 	kbdmux_state_t	*state = (kbdmux_state_t *) kbd->kb_data;
+	KBDMUX_LOCK_DECL_LOCAL;
 
 	KBDMUX_INTR(kbd, NULL);
 
@@ -151,19 +230,25 @@
 	KBDMUX_UNLOCK(state);
 }
 
-/* Process event from one of our keyboards */
+/*
+ * Process event from one of our keyboards 
+ */
 static int
 kbdmux_kbd_event(keyboard_t *kbd, int event, void *arg)
 {
 	kbdmux_state_t	*state = (kbdmux_state_t *) arg;
 
 	switch (event) {
-	case KBDIO_KEYINPUT: {
+	case KBDIO_KEYINPUT:
+		{
 		int	c;
+			KBDMUX_LOCK_DECL_LOCAL;
 
 		KBDMUX_LOCK(state);
 
-		/* read all chars from the keyboard */
+			/*
+			 * read all chars from the keyboard 
+			 */
 		while (KBDMUX_CHECK_CHAR(kbd)) {
 			c = KBDMUX_READ_CHAR(kbd, 0);
 			if (c == NOKEY)
@@ -171,31 +256,39 @@
 			if (c == ERRKEY)
 				continue; /* XXX ring bell */
 			if (!KBD_IS_BUSY(kbd))
-				continue; /* not open - discard the input */
+					continue;	/* not open - discard
+							 * the input */
 
 			putc(c, &state->ks_inq);
 		}
 
-		/* queue interrupt task if needed */
-		if (state->ks_inq.c_cc > 0 && !(state->ks_flags & TASK) &&
-		    taskqueue_enqueue(taskqueue_swi_giant, &state->ks_task) == 0)
+			/*
+			 * queue interrupt task if needed 
+			 */
+			if (state->ks_inq.c_cc > 0 && !(state->ks_flags & TASK)
+			    && taskqueue_enqueue(TASKQUEUE,
+						 &state->ks_task) == 0)
 			state->ks_flags |= TASK;
 
 		KBDMUX_UNLOCK(state);
-		} break;
+		}
+		break;
 
-	case KBDIO_UNLOADING: {
+	case KBDIO_UNLOADING:
+		{
 		kbdmux_kbd_t	*k;
+			KBDMUX_LOCK_DECL_LOCAL;
 
 		KBDMUX_LOCK(state);
 
-		SLIST_FOREACH(k, &state->ks_kbds, next)
-			if (k->kbd == kbd)
+			SLIST_FOREACH(k, &state->ks_kbds,
+				      next) if (k->kbd == kbd)
 				break;
 
 		if (k != NULL) {
 			kbd_release(k->kbd, &k->kbd);
-			SLIST_REMOVE(&state->ks_kbds, k, kbdmux_kbd, next);
+				SLIST_REMOVE(&state->ks_kbds, k, kbdmux_kbd,
+					     next);
 
 			k->kbd = NULL;
 			k->idx = -1;
@@ -204,11 +297,14 @@
 		}
 
 		KBDMUX_UNLOCK(state);
-		} break;
+		}
+		break;
 
 	default:
 		return (EINVAL);
-		/* NOT REACHED */
+		/*
+		 * NOT REACHED 
+		 */
 	}
 
 	return (0);
@@ -240,6 +336,8 @@
 static kbd_set_state_t	kbdmux_set_state;
 static kbd_poll_mode_t	kbdmux_poll;
 
+#if __FreeBSD_version >= 500014
+
 static keyboard_switch_t kbdmuxsw = {
 	.probe =	kbdmux_probe,
 	.init =		kbdmux_init,
@@ -262,21 +360,53 @@
 	.diag =		genkbd_diag,
 };
 
-/* Return the number of found keyboards */
+#else
+
+static keyboard_switch_t kbdmuxsw = {
+	kbdmux_probe,
+	kbdmux_init,
+	kbdmux_term,
+	kbdmux_intr,
+	kbdmux_test_if,
+	kbdmux_enable,
+	kbdmux_disable,
+	kbdmux_read,
+	kbdmux_check,
+	kbdmux_read_char,
+	kbdmux_check_char,
+	kbdmux_ioctl,
+	kbdmux_lock,
+	kbdmux_clear_state,
+	kbdmux_get_state,
+	kbdmux_set_state,
+	genkbd_get_fkeystr,
+	kbdmux_poll,
+	genkbd_diag
+};
+
+#endif
+
+/*
+ * Return the number of found keyboards 
+ */
 static int
 kbdmux_configure(int flags)
 {
 	return (1);
 }
 
-/* Detect a keyboard */
+/*
+ * Detect a keyboard 
+ */
 static int
 kbdmux_probe(int unit, void *arg, int flags)
 {
 	return (0);
 }
 
-/* Reset and initialize the keyboard (stolen from atkbd.c) */
+/*
+ * Reset and initialize the keyboard (stolen from atkbd.c) 
+ */
 static int
 kbdmux_init(int unit, keyboard_t **kbdp, void *arg, int flags)
 {
@@ -286,9 +416,11 @@
         accentmap_t	*accmap = NULL;
         fkeytab_t	*fkeymap = NULL;
 	int		 error, needfree, fkeymap_size;
+	KBDMUX_LOCK_DECL_LOCAL;
 
 	if (*kbdp == NULL) {
-		*kbdp = kbd = malloc(sizeof(*kbd), M_KBDMUX, M_NOWAIT | M_ZERO);
+		*kbdp = kbd =
+		    malloc(sizeof(*kbd), M_KBDMUX, M_NOWAIT | M_ZERO);
 		state = malloc(sizeof(*state), M_KBDMUX, M_NOWAIT | M_ZERO);
 		keymap = malloc(sizeof(key_map), M_KBDMUX, M_NOWAIT);
 		accmap = malloc(sizeof(accent_map), M_KBDMUX, M_NOWAIT);
@@ -303,7 +435,8 @@
 		}
 
 		KBDMUX_LOCK_INIT(state);
-		clist_alloc_cblocks(&state->ks_inq, KBDMUX_Q_SIZE, KBDMUX_Q_SIZE / 2); /* XXX */
+		clist_alloc_cblocks(&state->ks_inq, KBDMUX_Q_SIZE, KBDMUX_Q_SIZE / 2);	/* XXX 
+											 */
 		TASK_INIT(&state->ks_task, 0, kbdmux_kbd_intr, (void *) kbd);
 		SLIST_INIT(&state->ks_kbds);
 	} else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
@@ -319,12 +452,15 @@
 	}
 
 	if (!KBD_IS_PROBED(kbd)) {
-		/* XXX assume 101/102 keys keyboard */
+		/*
+		 * XXX assume 101/102 keys keyboard 
+		 */
 		kbd_init_struct(kbd, KEYBOARD_NAME, KB_101, unit, flags, 0, 0);
 		bcopy(&key_map, keymap, sizeof(key_map));
 		bcopy(&accent_map, accmap, sizeof(accent_map));
 		bcopy(fkey_tab, fkeymap,
-			imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
+		      imin(fkeymap_size * sizeof(fkeymap[0]),
+			   sizeof(fkey_tab)));
 		kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
 		kbd->kb_data = (void *)state;
 	
@@ -340,12 +476,11 @@
 	if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
 		kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
 
-/* XXX
-		kbdmux_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
-		delay[0] = kbd->kb_delay1;
-		delay[1] = kbd->kb_delay2;
-		kbdmux_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
-XXX */
+		/*
+		 * XXX kbdmux_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
+		 * delay[0] = kbd->kb_delay1; delay[1] = kbd->kb_delay2;
+		 * kbdmux_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); XXX 
+		 */
 
 		KBD_INIT_DONE(kbd);
 	}
@@ -381,20 +516,27 @@
 	return (error);
 }
 
-/* Finish using this keyboard */
+/*
+ * Finish using this keyboard 
+ */
 static int
 kbdmux_term(keyboard_t *kbd)
 {
 	kbdmux_state_t	*state = (kbdmux_state_t *) kbd->kb_data;
 	kbdmux_kbd_t	*k;
+	KBDMUX_LOCK_DECL_LOCAL;
 
 	KBDMUX_LOCK(state);
 
-	/* wait for interrupt task */
+	/*
+	 * wait for interrupt task 
+	 */
 	while (state->ks_flags & TASK)
 		KBDMUX_SLEEP(state, ks_task, "kbdmuxc", 0);
 
-	/* release all keyboards from the mux */
+	/*
+	 * release all keyboards from the mux 
+	 */
 	while ((k = SLIST_FIRST(&state->ks_kbds)) != NULL) {
 		kbd_release(k->kbd, &k->kbd);
 		SLIST_REMOVE_HEAD(&state->ks_kbds, next);
@@ -405,7 +547,9 @@
 		free(k, M_KBDMUX);
 	}
 
-	/* flush input queue */
+	/*
+	 * flush input queue 
+	 */
 	ndflush(&state->ks_inq, state->ks_inq.c_cc);
 	clist_free_cblocks(&state->ks_inq);
 
@@ -421,27 +565,36 @@
 	return (0);
 }
 
-/* Keyboard interrupt routine */
+/*
+ * Keyboard interrupt routine 
+ */
 static int
 kbdmux_intr(keyboard_t *kbd, void *arg)
 {
 	int	c;
 
 	if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
-		/* let the callback function to process the input */
+		/*
+		 * let the callback function to process the input 
+		 */
 		(*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
 					    kbd->kb_callback.kc_arg);
 	} else {
-		/* read and discard the input; no one is waiting for input */
+		/*
+		 * read and discard the input; no one is waiting for input 
+		 */
 		do {
 			c = kbdmux_read_char(kbd, FALSE);
-		} while (c != NOKEY);
+		}
+		while (c != NOKEY);
 	}
 
 	return (0);
 }
 
-/* Test the interface to the device */
+/*
+ * Test the interface to the device 
+ */
 static int
 kbdmux_test_if(keyboard_t *kbd)
 {
@@ -460,7 +613,9 @@
 	return (0);
 }
 
-/* Disallow the access to the device */
+/*
+ * Disallow the access to the device 
+ */
 static int
 kbdmux_disable(keyboard_t *kbd)
 {
@@ -468,12 +623,15 @@
 	return (0);
 }
 
-/* Read one byte from the keyboard if it's allowed */
+/*
+ * Read one byte from the keyboard if it's allowed 
+ */
 static int
 kbdmux_read(keyboard_t *kbd, int wait)
 {
 	kbdmux_state_t	*state = (kbdmux_state_t *) kbd->kb_data;
 	int		 c;
+	KBDMUX_LOCK_DECL_LOCAL;
 
 	KBDMUX_LOCK(state);
 	c = getc(&state->ks_inq);
@@ -485,12 +643,15 @@
 	return (KBD_IS_ACTIVE(kbd)? c : -1);
 }
 
-/* Check if data is waiting */
+/*
+ * Check if data is waiting 
+ */
 static int
 kbdmux_check(keyboard_t *kbd)
 {
 	kbdmux_state_t	*state = NULL;
 	int		 ready;
+	KBDMUX_LOCK_DECL_LOCAL;
 
 	if (!KBD_IS_ACTIVE(kbd))
 		return (FALSE);
@@ -504,19 +665,24 @@
 	return (ready);
 }
 
-/* Read char from the keyboard (stolen from atkbd.c) */
+/*
+ * Read char from the keyboard (stolen from atkbd.c) 
+ */
 static u_int
 kbdmux_read_char(keyboard_t *kbd, int wait)
 {
 	kbdmux_state_t	*state = (kbdmux_state_t *) kbd->kb_data;
 	u_int		 action;
 	int		 scancode, keycode;
+	KBDMUX_LOCK_DECL_LOCAL;
 
 	KBDMUX_LOCK(state);
 
 next_code:
 
-	/* do we have a composed char to return? */
+	/*
+	 * do we have a composed char to return? 
+	 */
 	if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
 		action = state->ks_composed_char;
 		state->ks_composed_char = 0;
@@ -531,23 +697,31 @@
 		return (action);
 	}
 
-	/* see if there is something in the keyboard queue */
+	/*
+	 * see if there is something in the keyboard queue 
+	 */
 	scancode = getc(&state->ks_inq);
 	if (scancode == -1) {
 		KBDMUX_UNLOCK(state);
 		return (NOKEY);
 	}
-	/* XXX FIXME: check for -1 if wait == 1! */
+	/*
+	 * XXX FIXME: check for -1 if wait == 1! 
+	 */
 
 	kbd->kb_count ++;
 
-	/* return the byte as is for the K_RAW mode */
+	/*
+	 * return the byte as is for the K_RAW mode 
+	 */
 	if (state->ks_mode == K_RAW) {
 		KBDMUX_UNLOCK(state);
 		return (scancode);
 	}
 
-	/* translate the scan code into a keycode */
+	/*
+	 * translate the scan code into a keycode 
+	 */
 	keycode = scancode & 0x7F;
 	switch (state->ks_prefix) {
 	case 0x00:	/* normal scancode */
@@ -622,7 +796,9 @@
 		case 0x53:	/* grey delete key */
 			keycode = 0x67;
 			break;
-		/* the following 3 are only used on the MS "Natural" keyboard */
+			/*
+			 * the following 3 are only used on the MS "Natural" keyboard 
+			 */
 		case 0x5b:	/* left Window key */
 			keycode = 0x69;
 			break;
@@ -656,7 +832,9 @@
 		if (keycode == 0x1D)
 			state->ks_prefix = 0x1D;
 		goto next_code;
-		/* NOT REACHED */
+		/*
+		 * NOT REACHED 
+		 */
 	case 0x1D:	/* pause / break */
 		state->ks_prefix = 0;
 		if (keycode != 0x45)
@@ -665,7 +843,9 @@
 		break;
 	}
 
-	/* XXX assume 101/102 keys AT keyboard */
+	/*
+	 * XXX assume 101/102 keys AT keyboard 
+	 */
 	switch (keycode) {
 	case 0x5c:	/* print screen */
 		if (state->ks_flags & ALTS)
@@ -677,17 +857,25 @@
 		break;
 	}
 
-	/* return the key code in the K_CODE mode */
+	/*
+	 * return the key code in the K_CODE mode 
+	 */
 	if (state->ks_mode == K_CODE) {
 		KBDMUX_UNLOCK(state);
 		return (keycode | (scancode & 0x80));
 	}
 
-	/* compose a character code */
+	/*
+	 * compose a character code 
+	 */
 	if (state->ks_flags & COMPOSE) {
 		switch (keycode | (scancode & 0x80)) {
-		/* key pressed, process it */
-		case 0x47: case 0x48: case 0x49:	/* keypad 7,8,9 */
+			/*
+			 * key pressed, process it 
+			 */
+		case 0x47:
+		case 0x48:
+		case 0x49:	/* keypad 7,8,9 */
 			state->ks_composed_char *= 10;
 			state->ks_composed_char += keycode - 0x40;
 			if (state->ks_composed_char > UCHAR_MAX) {
@@ -695,7 +883,9 @@
 				return (ERRKEY);
 			}
 			goto next_code;
-		case 0x4B: case 0x4C: case 0x4D:	/* keypad 4,5,6 */
+		case 0x4B:
+		case 0x4C:
+		case 0x4D:	/* keypad 4,5,6 */
 			state->ks_composed_char *= 10;
 			state->ks_composed_char += keycode - 0x47;
 			if (state->ks_composed_char > UCHAR_MAX) {
@@ -703,7 +893,9 @@
 				return (ERRKEY);
 			}
 			goto next_code;
-		case 0x4F: case 0x50: case 0x51:	/* keypad 1,2,3 */
+		case 0x4F:
+		case 0x50:
+		case 0x51:	/* keypad 1,2,3 */
 			state->ks_composed_char *= 10;
 			state->ks_composed_char += keycode - 0x4E;
 			if (state->ks_composed_char > UCHAR_MAX) {
@@ -719,10 +911,18 @@
 			}
 			goto next_code;
 
-		/* key released, no interest here */
-		case 0xC7: case 0xC8: case 0xC9:	/* keypad 7,8,9 */
-		case 0xCB: case 0xCC: case 0xCD:	/* keypad 4,5,6 */
-		case 0xCF: case 0xD0: case 0xD1:	/* keypad 1,2,3 */
+			/*
+			 * key released, no interest here 
+			 */
+		case 0xC7:
+		case 0xC8:
+		case 0xC9:	/* keypad 7,8,9 */
+		case 0xCB:
+		case 0xCC:
+		case 0xCD:	/* keypad 4,5,6 */
+		case 0xCF:
+		case 0xD0:
+		case 0xD1:	/* keypad 1,2,3 */
 		case 0xD2:				/* keypad 0 */
 			goto next_code;
 
@@ -740,7 +940,9 @@
 		}
 	}
 
-	/* keycode to key action */
+	/*
+	 * keycode to key action 
+	 */
 	action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
 			&state->ks_state, &state->ks_accents);
 	if (action == NOKEY)
@@ -751,12 +953,15 @@
 	return (action);
 }
 
-/* Check if char is waiting */
+/*
+ * Check if char is waiting 
+ */
 static int
 kbdmux_check_char(keyboard_t *kbd)
 {
 	kbdmux_state_t	*state = (kbdmux_state_t *) kbd->kb_data;
 	int		 ready;
+	KBDMUX_LOCK_DECL_LOCAL;
 
 	if (!KBD_IS_ACTIVE(kbd))
 		return (FALSE);
@@ -773,13 +978,16 @@
 	return (ready);
 }
 
-/* Keyboard ioctl's */
+/*
+ * Keyboard ioctl's 
+ */
 static int
 kbdmux_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
 {
 	kbdmux_state_t	*state = (kbdmux_state_t *) kbd->kb_data;
 	kbdmux_kbd_t	*k = NULL;
 	int		 error = 0, mode;
+	KBDMUX_LOCK_DECL_LOCAL;
 
 	if (state == NULL)
 		return (ENXIO);
@@ -788,8 +996,8 @@
 	case KBDMUX_ADDKBD: /* add keyboard to the mux */
 		KBDMUX_LOCK(state);
 
-		SLIST_FOREACH(k, &state->ks_kbds, next)
-			if (k->idx == *((int *) arg))
+		SLIST_FOREACH(k, &state->ks_kbds,
+			      next) if (k->idx == *((int *)arg))
 				break;
 
 		if (k != NULL) {
@@ -847,14 +1055,15 @@
 	case KBDMUX_RELKBD: /* release keyboard from the mux */
 		KBDMUX_LOCK(state);
 
-		SLIST_FOREACH(k, &state->ks_kbds, next)
-			if (k->idx == *((int *) arg))
+		SLIST_FOREACH(k, &state->ks_kbds,
+			      next) if (k->idx == *((int *)arg))
 				break;
 
 		if (k != NULL) {
 			error = kbd_release(k->kbd, &k->kbd);
 			if (error == 0) {
-				SLIST_REMOVE(&state->ks_kbds, k, kbdmux_kbd, next);
+				SLIST_REMOVE(&state->ks_kbds, k, kbdmux_kbd,
+					     next);
 
 				k->kbd = NULL;
 				k->idx = -1;
@@ -879,11 +1088,15 @@
 		switch (*((int *) arg)) {
 		case K_XLATE:
 			if (state->ks_mode != K_XLATE) {
-				/* make lock key state and LED state match */
+				/*
+				 * make lock key state and LED state match 
+				 */
 				state->ks_state &= ~LOCK_MASK;
 				state->ks_state |= KBD_LED_VAL(kbd);
                         }
-                        /* FALLTHROUGH */
+			/*
+			 * FALLTHROUGH 
+			 */
 
 		case K_RAW:
 		case K_CODE:
@@ -910,7 +1123,9 @@
 	case KDSETLED: /* set keyboard LED */
 		KBDMUX_LOCK(state);
 
-		/* NOTE: lock key state in ks_state won't be changed */
+		/*
+		 * NOTE: lock key state in ks_state won't be changed 
+		 */
 		if (*((int *) arg) & ~LOCK_MASK) {
 			KBDMUX_UNLOCK(state);
 
@@ -943,7 +1158,9 @@
 		KBDMUX_UNLOCK(state);
 
 		return (kbdmux_ioctl(kbd, KDSETLED, arg));
-		/* NOT REACHED */
+		/*
+		 * NOT REACHED 
+		 */
 
 	case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
 		break;
@@ -957,7 +1174,9 @@
 		KBDMUX_LOCK(state);
                 state->ks_accents = 0;
 		KBDMUX_UNLOCK(state);
-                /* FALLTHROUGH */
+		/*
+		 * FALLTHROUGH 
+		 */
 
 	default:
 		error = genkbd_commonioctl(kbd, cmd, arg);
@@ -967,14 +1186,18 @@
 	return (error);
 }
 
-/* Lock the access to the keyboard */
+/*
+ * Lock the access to the keyboard 
+ */
 static int
 kbdmux_lock(keyboard_t *kbd, int lock)
 {
 	return (1); /* XXX */
 }
 
-/* Clear the internal state of the keyboard */
+/*
+ * Clear the internal state of the keyboard 
+ */
 static void
 kbdmux_clear_state_locked(kbdmux_state_t *state)
 {
@@ -984,7 +1207,9 @@
 	state->ks_state &= LOCK_MASK;	/* preserve locking key state */
 	state->ks_accents = 0;
 	state->ks_composed_char = 0;
-/*	state->ks_prefix = 0;		XXX */
+	/*
+	 * state->ks_prefix = 0; XXX 
+	 */
 
 	ndflush(&state->ks_inq, state->ks_inq.c_cc);
 }
@@ -993,13 +1218,16 @@
 kbdmux_clear_state(keyboard_t *kbd)
 {
 	kbdmux_state_t	*state = (kbdmux_state_t *) kbd->kb_data;
+	KBDMUX_LOCK_DECL_LOCAL;
 
 	KBDMUX_LOCK(state);
 	kbdmux_clear_state_locked(state);
 	KBDMUX_UNLOCK(state);
 }
 
-/* Save the internal state */
+/*
+ * Save the internal state 
+ */
 static int
 kbdmux_get_state(keyboard_t *kbd, void *buf, size_t len)
 {
@@ -1008,28 +1236,35 @@
 	if (len < sizeof(kbdmux_state_t))
 		return (-1);
 
-	bcopy(kbd->kb_data, buf, sizeof(kbdmux_state_t)); /* XXX locking? */
+	bcopy(kbd->kb_data, buf, sizeof(kbdmux_state_t));	/* XXX
+								 * locking? */
 
 	return (0);
 }
 
-/* Set the internal state */
+/*
+ * Set the internal state 
+ */
 static int
 kbdmux_set_state(keyboard_t *kbd, void *buf, size_t len)
 {
 	if (len < sizeof(kbdmux_state_t))
 		return (ENOMEM);
 
-	bcopy(buf, kbd->kb_data, sizeof(kbdmux_state_t)); /* XXX locking? */
+	bcopy(buf, kbd->kb_data, sizeof(kbdmux_state_t));	/* XXX
+								 * locking? */
 
 	return (0);
 }
 
-/* Set polling */
+/*
+ * Set polling 
+ */
 static int
 kbdmux_poll(keyboard_t *kbd, int on)
 {
 	kbdmux_state_t	*state = NULL;
+	KBDMUX_LOCK_DECL_LOCAL;
 
 	state = (kbdmux_state_t *) kbd->kb_data;
 
@@ -1102,12 +1337,14 @@
 		break;
 
 	case MOD_UNLOAD:
-		if ((sw = kbd_get_switch(KEYBOARD_NAME)) == NULL)
-			panic("kbd_get_switch(" KEYBOARD_NAME ") == NULL");
+		sw = kbd_get_switch(KEYBOARD_NAME);
+		ASSERT(sw != NULL,
+		       "kbd_get_switch(" KEYBOARD_NAME ") == NULL");
 
 		kbd = kbd_get_keyboard(kbd_find_keyboard(KEYBOARD_NAME, 0));
-		if (kbd == NULL)
-			 panic("kbd_get_keyboard(kbd_find_keyboard(" KEYBOARD_NAME ", 0)) == NULL");
+		ASSERT(kbd != NULL,
+		       "kbd_get_keyboard(kbd_find_keyboard(" KEYBOARD_NAME
+		       ", 0)) == NULL");
 
 		(*sw->disable)(kbd);
 		kbd_detach(kbd);
@@ -1125,4 +1362,3 @@
 }
 
 DEV_MODULE(kbdmux, kbdmux_modevent, NULL);
-

[-- Attachment #3 --]
/*
 * $Header: /usr/local/cvs/dev_debug/dev_debug.h,v 1.6 2005/05/31 14:29:22 nk Exp $
 *
 *    COPYRIGHT(c) 2003,2004,2005 by demig Prozessautomatisierung GmbH 
 *      |\  /|  Haardtstrasse 40                                  
 *      |_\/ |  D-57076 Siegen                                    
 *      | /\ |  Germany                                           
 *      |/  \|  http://www.demig.de  info@demig.de                
 */


#ifndef DEV_DEBUG_H
#define DEV_DEBUG_H


#ifndef DEBUG
#error  "DEBUG not defined"
#endif
#ifndef VERBOSE
#error  "VERBOSE not defined"
#endif
#ifndef DEBUG_USESYSLOG
#error  "DEBUG_USESYSLOG not defined"
#endif
#ifndef DEBUG_TAG
#error  "DEBUG_TAG not defined"
#endif


#define CAT(x,y)        x##y
#define XCAT(x,y)       CAT (x,y)
#define STR(x)  #x
#define XSTR(x) STR (x)


#if DEBUG
#define INLINE
#else
#define INLINE  __inline
#endif


#if DEBUG_USESYSLOG

#include <sys/syslog.h>
#define LOG(txt, args...)       log (LOG_DEBUG, txt, ## args)

#else

#define LOG(txt, args...)       printf (txt, ## args)

#endif


#if defined (NDEBUG)
#define DEBUGPRINTF0(expr, func, txt, args...)          \
  do                                                    \
  {                                                     \
    if (expr)                                           \
    {                                                   \
      func (XSTR (DEBUG_TAG) ": " txt, ## args);        \
    }                                                   \
  }                                                     \
  while (FALSE)
#else
#define DEBUGPRINTF0(expr, func, txt, args...)          \
  do                                                    \
  {                                                     \
    if (expr)                                           \
    {                                                   \
      func (XSTR (__FILE__)", " XSTR (__LINE__) ": "    \
            txt, ## args);                              \
    }                                                   \
  }                                                     \
  while (FALSE)
#endif


#if defined (NDEBUG)
#define ASSERT(expr, txt, args...)
#else
#define ASSERT(expr, txt, args...) \
  DEBUGPRINTF0 (! (expr), panic, txt, ## args)
#endif


#if VERBOSE
#define vprintf(txt, args...) \
  DEBUGPRINTF0 (TRUE, printf, txt, ## args)
#else
#define vprintf(txt, args...)
#endif


#define DEBUGPRINTF(lvl, cmp, txt, args...)       \
  DEBUGPRINTF0 (cmp >= lvl, LOG, txt, ## args)


#define DEBUG_VAR_SCOPE         static
#define DEBUG_VAR_TYPE          int
#define DEBUG_VAR_SYSCTL        SYSCTL_UINT
#define DEBUG_VAR_NAME(tag)     XCAT (tag, _debug)


#if DEBUG

#if defined (NDEBUG)
#define DEBUG_VAR_DECL(tag, lvl)                \
DEBUG_VAR_SCOPE DEBUG_VAR_TYPE DEBUG_VAR_NAME (tag) = 0
#else
#define DEBUG_VAR_DECL(tag, lvl)                \
DEBUG_VAR_SCOPE DEBUG_VAR_TYPE DEBUG_VAR_NAME (tag) = lvl
#endif

#define DPRINTF(tag, lvl, txt, args...)         \
  DEBUGPRINTF (lvl, DEBUG_VAR_NAME (tag), txt, ## args)

#include <sys/sysctl.h>

#define DEBUG_SYSCTL(tag)                       \
DEBUG_VAR_SYSCTL (_debug,                       \
                  OID_AUTO,                     \
                  XCAT (tag, _debug),           \
                  CTLFLAG_RW,                   \
                  & DEBUG_VAR_NAME (tag),       \
                  0,                            \
                  XCAT (XSTR (tag), " debug"))


#else

#define DEBUG_VAR_DECL(tag, lvl)
#define DEBUG_SYSCTL(tag)
#define DPRINTF(tag, lvl, txt, args...)

#endif


#define debug_var_decl(lvl)     DEBUG_VAR_DECL (DEBUG_TAG, lvl)
#define debug_sysctl()          DEBUG_SYSCTL (DEBUG_TAG)
#define dprintf(lvl, txt, args...)              \
  DPRINTF (DEBUG_TAG, lvl, txt, ## args)


#endif
help

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?000001c56ff8$ffda5b40$4801a8c0>