Home | History | Annotate | Line # | Download | only in adb
adb_kbd.c revision 1.5
      1 /*	$NetBSD: adb_kbd.c,v 1.5 2007/03/04 06:01:44 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1998	Colin Wood
      5  * Copyright (C) 2006, 2007 Michael Lorenz
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Colin Wood.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: adb_kbd.c,v 1.5 2007/03/04 06:01:44 christos Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/device.h>
     39 #include <sys/fcntl.h>
     40 #include <sys/poll.h>
     41 #include <sys/select.h>
     42 #include <sys/proc.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/sysctl.h>
     46 
     47 #include <dev/wscons/wsconsio.h>
     48 #include <dev/wscons/wskbdvar.h>
     49 #include <dev/wscons/wsksymdef.h>
     50 #include <dev/wscons/wsksymvar.h>
     51 #include <dev/wscons/wsmousevar.h>
     52 
     53 #include <dev/sysmon/sysmonvar.h>
     54 #include <dev/sysmon/sysmon_taskq.h>
     55 
     56 #include <machine/autoconf.h>
     57 #include <machine/keyboard.h>
     58 #include <machine/adbsys.h>
     59 
     60 #include <dev/adb/adbvar.h>
     61 #include <dev/adb/adb_keymap.h>
     62 
     63 #include "adbdebug.h"
     64 
     65 struct adbkbd_softc {
     66 	struct device sc_dev;
     67 	struct adb_device *sc_adbdev;
     68 	struct adb_bus_accessops *sc_ops;
     69 	struct device *sc_wskbddev;
     70 	struct device *sc_wsmousedev;
     71 	struct sysmon_pswitch sc_sm_pbutton;
     72 	int sc_leds;
     73 	int sc_have_led_control;
     74 	int sc_msg_len;
     75 	int sc_event;
     76 	int sc_poll;
     77 	int sc_polled_chars;
     78 	int sc_trans[3];
     79 	int sc_capslock;
     80 #ifdef WSDISPLAY_COMPAT_RAWKBD
     81 	int sc_rawkbd;
     82 #endif
     83 	uint8_t sc_buffer[16];
     84 	uint8_t sc_pollbuf[16];
     85 	uint8_t sc_us;
     86 };
     87 
     88 /*
     89  * Function declarations.
     90  */
     91 static int	adbkbd_match(struct device *, struct cfdata *, void *);
     92 static void	adbkbd_attach(struct device *, struct device *, void *);
     93 
     94 static void	adbkbd_initleds(struct adbkbd_softc *);
     95 static void	adbkbd_keys(struct adbkbd_softc *, uint8_t, uint8_t);
     96 static inline void adbkbd_key(struct adbkbd_softc *, uint8_t);
     97 static int	adbkbd_wait(struct adbkbd_softc *, int);
     98 
     99 /* Driver definition. */
    100 CFATTACH_DECL(adbkbd, sizeof(struct adbkbd_softc),
    101     adbkbd_match, adbkbd_attach, NULL, NULL);
    102 
    103 extern struct cfdriver akbd_cd;
    104 
    105 static int adbkbd_enable(void *, int);
    106 static int adbkbd_ioctl(void *, u_long, void *, int, struct lwp *);
    107 static void adbkbd_set_leds(void *, int);
    108 static void adbkbd_handler(void *, int, uint8_t *);
    109 
    110 struct wskbd_accessops adbkbd_accessops = {
    111 	adbkbd_enable,
    112 	adbkbd_set_leds,
    113 	adbkbd_ioctl,
    114 };
    115 
    116 static void adbkbd_cngetc(void *, u_int *, int *);
    117 static void adbkbd_cnpollc(void *, int);
    118 
    119 struct wskbd_consops adbkbd_consops = {
    120 	adbkbd_cngetc,
    121 	adbkbd_cnpollc,
    122 };
    123 
    124 struct wskbd_mapdata adbkbd_keymapdata = {
    125 	akbd_keydesctab,
    126 #ifdef AKBD_LAYOUT
    127 	AKBD_LAYOUT,
    128 #else
    129 	KB_US,
    130 #endif
    131 };
    132 
    133 static int adbkms_enable(void *);
    134 static int adbkms_ioctl(void *, u_long, void *, int, struct lwp *);
    135 static void adbkms_disable(void *);
    136 
    137 const struct wsmouse_accessops adbkms_accessops = {
    138 	adbkms_enable,
    139 	adbkms_ioctl,
    140 	adbkms_disable,
    141 };
    142 
    143 static int  adbkbd_sysctl_button(SYSCTLFN_ARGS);
    144 static void adbkbd_setup_sysctl(struct adbkbd_softc *);
    145 
    146 #ifdef ADBKBD_DEBUG
    147 #define DPRINTF printf
    148 #else
    149 #define DPRINTF while (0) printf
    150 #endif
    151 
    152 static int adbkbd_is_console = 0;
    153 static int adbkbd_console_attached = 0;
    154 
    155 static int
    156 adbkbd_match(parent, cf, aux)
    157 	struct device *parent;
    158 	struct cfdata *cf;
    159 	void   *aux;
    160 {
    161 	struct adb_attach_args *aaa = aux;
    162 
    163 	if (aaa->dev->original_addr == ADBADDR_KBD)
    164 		return 1;
    165 	else
    166 		return 0;
    167 }
    168 
    169 static void
    170 adbkbd_attach(struct device *parent, struct device *self, void *aux)
    171 {
    172 	struct adbkbd_softc *sc = (struct adbkbd_softc *)self;
    173 	struct adb_attach_args *aaa = aux;
    174 	short cmd;
    175 	struct wskbddev_attach_args a;
    176 	struct wsmousedev_attach_args am;
    177 
    178 	sc->sc_ops = aaa->ops;
    179 	sc->sc_adbdev = aaa->dev;
    180 	sc->sc_adbdev->cookie = sc;
    181 	sc->sc_adbdev->handler = adbkbd_handler;
    182 	sc->sc_us = ADBTALK(sc->sc_adbdev->current_addr, 0);
    183 
    184 	sc->sc_leds = 0;	/* initially off */
    185 	sc->sc_have_led_control = 0;
    186 	sc->sc_msg_len = 0;
    187 	sc->sc_poll = 0;
    188 	sc->sc_capslock = 0;
    189 	sc->sc_trans[1] = 103;	/* F11 */
    190 	sc->sc_trans[2] = 111;	/* F12 */
    191 
    192 	printf(" addr %d ", sc->sc_adbdev->current_addr);
    193 
    194 	switch (sc->sc_adbdev->handler_id) {
    195 	case ADB_STDKBD:
    196 		printf("standard keyboard\n");
    197 		break;
    198 	case ADB_ISOKBD:
    199 		printf("standard keyboard (ISO layout)\n");
    200 		break;
    201 	case ADB_EXTKBD:
    202 		cmd = ADBTALK(sc->sc_adbdev->current_addr, 1);
    203 		sc->sc_msg_len = 0;
    204 		sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 0, NULL);
    205 		adbkbd_wait(sc, 10);
    206 
    207 		/* Ignore Logitech MouseMan/Trackman pseudo keyboard */
    208 		/* XXX needs testing */
    209 		if (sc->sc_buffer[2] == 0x9a && sc->sc_buffer[3] == 0x20) {
    210 			printf("Mouseman (non-EMP) pseudo keyboard\n");
    211 			return;
    212 		} else if (sc->sc_buffer[2] == 0x9a &&
    213 		    sc->sc_buffer[3] == 0x21) {
    214 			printf("Trackman (non-EMP) pseudo keyboard\n");
    215 			return;
    216 		} else {
    217 			printf("extended keyboard\n");
    218 			adbkbd_initleds(sc);
    219 		}
    220 		break;
    221 	case ADB_EXTISOKBD:
    222 		printf("extended keyboard (ISO layout)\n");
    223 		adbkbd_initleds(sc);
    224 		break;
    225 	case ADB_KBDII:
    226 		printf("keyboard II\n");
    227 		break;
    228 	case ADB_ISOKBDII:
    229 		printf("keyboard II (ISO layout)\n");
    230 		break;
    231 	case ADB_PBKBD:
    232 		printf("PowerBook keyboard\n");
    233 		break;
    234 	case ADB_PBISOKBD:
    235 		printf("PowerBook keyboard (ISO layout)\n");
    236 		break;
    237 	case ADB_ADJKPD:
    238 		printf("adjustable keypad\n");
    239 		break;
    240 	case ADB_ADJKBD:
    241 		printf("adjustable keyboard\n");
    242 		break;
    243 	case ADB_ADJISOKBD:
    244 		printf("adjustable keyboard (ISO layout)\n");
    245 		break;
    246 	case ADB_ADJJAPKBD:
    247 		printf("adjustable keyboard (Japanese layout)\n");
    248 		break;
    249 	case ADB_PBEXTISOKBD:
    250 		printf("PowerBook extended keyboard (ISO layout)\n");
    251 		break;
    252 	case ADB_PBEXTJAPKBD:
    253 		printf("PowerBook extended keyboard (Japanese layout)\n");
    254 		break;
    255 	case ADB_JPKBDII:
    256 		printf("keyboard II (Japanese layout)\n");
    257 		break;
    258 	case ADB_PBEXTKBD:
    259 		printf("PowerBook extended keyboard\n");
    260 		break;
    261 	case ADB_DESIGNKBD:
    262 		printf("extended keyboard\n");
    263 		adbkbd_initleds(sc);
    264 		break;
    265 	case ADB_PBJPKBD:
    266 		printf("PowerBook keyboard (Japanese layout)\n");
    267 		break;
    268 	case ADB_PBG3KBD:
    269 		printf("PowerBook G3 keyboard\n");
    270 		break;
    271 	case ADB_PBG3JPKBD:
    272 		printf("PowerBook G3 keyboard (Japanese layout)\n");
    273 		break;
    274 	case ADB_IBOOKKBD:
    275 		printf("iBook keyboard\n");
    276 		break;
    277 	default:
    278 		printf("mapped device (%d)\n", sc->sc_adbdev->handler_id);
    279 		break;
    280 	}
    281 
    282 	if (adbkbd_is_console && (adbkbd_console_attached == 0)) {
    283 		wskbd_cnattach(&adbkbd_consops, sc, &adbkbd_keymapdata);
    284 		adbkbd_console_attached = 1;
    285 		a.console = 1;
    286 	} else {
    287 		a.console = 0;
    288 	}
    289 	a.keymap = &adbkbd_keymapdata;
    290 	a.accessops = &adbkbd_accessops;
    291 	a.accesscookie = sc;
    292 
    293 	sc->sc_wskbddev = config_found_ia(self, "wskbddev", &a, wskbddevprint);
    294 
    295 	/* attach the mouse device */
    296 	am.accessops = &adbkms_accessops;
    297 	am.accesscookie = sc;
    298 	sc->sc_wsmousedev = config_found_ia(self, "wsmousedev", &am, wsmousedevprint);
    299 
    300 	if (sc->sc_wsmousedev != NULL)
    301 		adbkbd_setup_sysctl(sc);
    302 
    303 	/* finally register the power button */
    304 	sysmon_task_queue_init();
    305 	memset(&sc->sc_sm_pbutton, 0, sizeof(struct sysmon_pswitch));
    306 	sc->sc_sm_pbutton.smpsw_name = sc->sc_dev.dv_xname;
    307 	sc->sc_sm_pbutton.smpsw_type = PSWITCH_TYPE_POWER;
    308 	if (sysmon_pswitch_register(&sc->sc_sm_pbutton) != 0)
    309 		printf("%s: unable to register power button with sysmon\n",
    310 		    sc->sc_dev.dv_xname);
    311 }
    312 
    313 static void
    314 adbkbd_handler(void *cookie, int len, uint8_t *data)
    315 {
    316 	struct adbkbd_softc *sc = cookie;
    317 
    318 #ifdef ADBKBD_DEBUG
    319 	int i;
    320 	printf("%s: %02x - ", sc->sc_dev.dv_xname, sc->sc_us);
    321 	for (i = 0; i < len; i++) {
    322 		printf(" %02x", data[i]);
    323 	}
    324 	printf("\n");
    325 #endif
    326 	if (len >= 2) {
    327 		if (data[1] == sc->sc_us) {
    328 			adbkbd_keys(sc, data[2], data[3]);
    329 			return;
    330 		} else {
    331 			memcpy(sc->sc_buffer, data, len);
    332 		}
    333 		sc->sc_msg_len = len;
    334 		wakeup(&sc->sc_event);
    335 	} else {
    336 		DPRINTF("bogus message\n");
    337 	}
    338 }
    339 
    340 static int
    341 adbkbd_wait(struct adbkbd_softc *sc, int timeout)
    342 {
    343 	int cnt = 0;
    344 
    345 	if (sc->sc_poll) {
    346 		while (sc->sc_msg_len == 0) {
    347 			sc->sc_ops->poll(sc->sc_ops->cookie);
    348 		}
    349 	} else {
    350 		while ((sc->sc_msg_len == 0) && (cnt < timeout)) {
    351 			tsleep(&sc->sc_event, 0, "adbkbdio", hz);
    352 			cnt++;
    353 		}
    354 	}
    355 	return (sc->sc_msg_len > 0);
    356 }
    357 
    358 static void
    359 adbkbd_keys(struct adbkbd_softc *sc, uint8_t k1, uint8_t k2)
    360 {
    361 
    362 	/* keyboard event processing */
    363 
    364 	DPRINTF("[%02x %02x]", k1, k2);
    365 
    366 	if (((k1 == k2) && (k1 == 0x7f)) || (k1 == 0x7e)) {
    367 
    368 		/* power button, report to sysmon */
    369 		sysmon_pswitch_event(&sc->sc_sm_pbutton,
    370 		    ADBK_PRESS(k1) ? PSWITCH_EVENT_PRESSED :
    371 		    PSWITCH_EVENT_RELEASED);
    372 	} else {
    373 
    374 		adbkbd_key(sc, k1);
    375 		if (k2 != 0xff)
    376 			adbkbd_key(sc, k2);
    377 	}
    378 }
    379 
    380 static inline void
    381 adbkbd_key(struct adbkbd_softc *sc, uint8_t k)
    382 {
    383 
    384 	if (sc->sc_poll) {
    385 		if (sc->sc_polled_chars >= 16) {
    386 			printf("%s: polling buffer is full\n",
    387 			    sc->sc_dev.dv_xname);
    388 		}
    389 		sc->sc_pollbuf[sc->sc_polled_chars] = k;
    390 		sc->sc_polled_chars++;
    391 		return;
    392 	}
    393 
    394 	/* translate some keys to mouse events */
    395 	if (sc->sc_wsmousedev != NULL) {
    396 		if (ADBK_KEYVAL(k) == sc->sc_trans[1]) {
    397 			wsmouse_input(sc->sc_wsmousedev, ADBK_PRESS(k) ? 2 : 0,
    398 			      0, 0, 0, 0,
    399 			      WSMOUSE_INPUT_DELTA);
    400 			return;
    401 		}
    402 		if (ADBK_KEYVAL(k) == sc->sc_trans[2]) {
    403 			wsmouse_input(sc->sc_wsmousedev, ADBK_PRESS(k) ? 4 : 0,
    404 			      0, 0, 0, 0,
    405 			      WSMOUSE_INPUT_DELTA);
    406 			return;
    407 		}
    408 	}
    409 #ifdef WSDISPLAY_COMPAT_RAWKBD
    410 	if (sc->sc_rawkbd) {
    411 		char cbuf[2];
    412 		int s;
    413 		int j = 0;
    414 		int c = keyboard[ADBK_KEYVAL(k)][3]
    415 
    416 		if (k & 0x80)
    417 			cbuf[j++] = 0xe0;
    418 
    419 		cbuf[j++] = (c & 0x7f) | (ADBK_PRESS(k)? 0 : 0x80);
    420 
    421 		s = spltty();
    422 		wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
    423 		splx(s);
    424 	} else {
    425 #endif
    426 
    427 	if (ADBK_KEYVAL(k) == 0x39) {
    428 		/* caps lock - send up and down */
    429 		if (ADBK_PRESS(k) != sc->sc_capslock) {
    430 			sc->sc_capslock = ADBK_PRESS(k);
    431 			wskbd_input(sc->sc_wskbddev,
    432 			    WSCONS_EVENT_KEY_DOWN, 0x39);
    433 			wskbd_input(sc->sc_wskbddev,
    434 			    WSCONS_EVENT_KEY_UP, 0x39);
    435 		}
    436 	} else {
    437 		/* normal event */
    438 		int type;
    439 
    440 		type = ADBK_PRESS(k) ?
    441 		    WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
    442 		wskbd_input(sc->sc_wskbddev, type, ADBK_KEYVAL(k));
    443 	}
    444 #ifdef WSDISPLAY_COMPAT_RAWKBD
    445 	}
    446 #endif
    447 }
    448 
    449 /*
    450  * Set the keyboard LED's.
    451  *
    452  * Automatically translates from ioctl/softc format to the
    453  * actual keyboard register format
    454  */
    455 static void
    456 adbkbd_set_leds(void *cookie, int leds)
    457 {
    458 	struct adbkbd_softc *sc = cookie;
    459 	int aleds;
    460 	short cmd;
    461 	uint8_t buffer[2];
    462 
    463 	DPRINTF("adbkbd_set_leds: %02x\n", leds);
    464 	if ((leds & 0x07) == (sc->sc_leds & 0x07))
    465 		return;
    466 
    467  	if (sc->sc_have_led_control) {
    468 
    469 		aleds = (~leds & 0x04) | 3;
    470 		if (leds & 1)
    471 			aleds &= ~2;
    472 		if (leds & 2)
    473 			aleds &= ~1;
    474 
    475 		buffer[0] = 0xff;
    476 		buffer[1] = aleds | 0xf8;
    477 
    478 		cmd = ADBLISTEN(sc->sc_adbdev->current_addr, 2);
    479 		sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 2, buffer);
    480 	}
    481 
    482 	sc->sc_leds = leds & 7;
    483 }
    484 
    485 static void
    486 adbkbd_initleds(struct adbkbd_softc *sc)
    487 {
    488 	short cmd;
    489 
    490 	/* talk R2 */
    491 	cmd = ADBTALK(sc->sc_adbdev->current_addr, 2);
    492 	sc->sc_msg_len = 0;
    493 	sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 0, NULL);
    494 	if (!adbkbd_wait(sc, 10)) {
    495 		printf("unable to read LED state\n");
    496 		return;
    497 	}
    498 	sc->sc_have_led_control = 1;
    499 	DPRINTF("have LED control\n");
    500 	return;
    501 }
    502 
    503 static int
    504 adbkbd_enable(void *v, int on)
    505 {
    506 	return 0;
    507 }
    508 
    509 static int
    510 adbkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    511 {
    512 	struct adbkbd_softc *sc = (struct adbkbd_softc *) v;
    513 
    514 	switch (cmd) {
    515 
    516 	case WSKBDIO_GTYPE:
    517 		*(int *)data = WSKBD_TYPE_ADB;
    518 		return 0;
    519 	case WSKBDIO_SETLEDS:
    520 		adbkbd_set_leds(sc, *(int *)data);
    521 		return 0;
    522 	case WSKBDIO_GETLEDS:
    523 		*(int *)data = sc->sc_leds;
    524 		return 0;
    525 #ifdef WSDISPLAY_COMPAT_RAWKBD
    526 	case WSKBDIO_SETMODE:
    527 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
    528 		return 0;
    529 #endif
    530 	}
    531 
    532 	return EPASSTHROUGH;
    533 }
    534 
    535 int
    536 adbkbd_cnattach()
    537 {
    538 
    539 	adbkbd_is_console = 1;
    540 	return 0;
    541 }
    542 
    543 static void
    544 adbkbd_cngetc(void *v, u_int *type, int *data)
    545 {
    546 	struct adbkbd_softc *sc = v;
    547 	int key, press, val;
    548 	int s;
    549 
    550 	s = splhigh();
    551 
    552 	KASSERT(sc->sc_poll);
    553 
    554 	DPRINTF("polling...");
    555 	while (sc->sc_polled_chars == 0) {
    556 		sc->sc_ops->poll(sc->sc_ops->cookie);
    557 	}
    558 	DPRINTF(" got one\n");
    559 	splx(s);
    560 
    561 	key = sc->sc_pollbuf[0];
    562 	sc->sc_polled_chars--;
    563 	memmove(sc->sc_pollbuf, sc->sc_pollbuf + 1,
    564 		sc->sc_polled_chars);
    565 
    566 	press = ADBK_PRESS(key);
    567 	val = ADBK_KEYVAL(key);
    568 
    569 	*data = val;
    570 	*type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
    571 }
    572 
    573 static void
    574 adbkbd_cnpollc(void *v, int on)
    575 {
    576 	struct adbkbd_softc *sc = v;
    577 
    578 	sc->sc_poll = on;
    579 	if (!on) {
    580 		int i;
    581 
    582 		/* feed the poll buffer's content to wskbd */
    583 		for (i = 0; i < sc->sc_polled_chars; i++) {
    584 			adbkbd_key(sc, sc->sc_pollbuf[i]);
    585 		}
    586 		sc->sc_polled_chars = 0;
    587 	}
    588 }
    589 
    590 /* stuff for the pseudo mouse */
    591 static int
    592 adbkms_enable(void *v)
    593 {
    594 	return 0;
    595 }
    596 
    597 static int
    598 adbkms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    599 {
    600 
    601 	switch (cmd) {
    602 	case WSMOUSEIO_GTYPE:
    603 		*(u_int *)data = WSMOUSE_TYPE_PSEUDO;
    604 		break;
    605 
    606 	default:
    607 		return (EPASSTHROUGH);
    608 	}
    609 	return (0);
    610 }
    611 
    612 static void
    613 adbkms_disable(void *v)
    614 {
    615 }
    616 
    617 static void
    618 adbkbd_setup_sysctl(struct adbkbd_softc *sc)
    619 {
    620 	struct sysctlnode *node, *me;
    621 	int ret;
    622 
    623 	DPRINTF("%s: sysctl setup\n", sc->sc_dev.dv_xname);
    624 	ret = sysctl_createv(NULL, 0, NULL, (const struct sysctlnode **)&me,
    625 	       CTLFLAG_READWRITE,
    626 	       CTLTYPE_NODE, sc->sc_dev.dv_xname, NULL,
    627 	       NULL, 0, NULL, 0,
    628 	       CTL_MACHDEP, CTL_CREATE, CTL_EOL);
    629 
    630 	ret = sysctl_createv(NULL, 0, NULL,
    631 	    (const struct sysctlnode **)&node,
    632 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC | CTLFLAG_IMMEDIATE,
    633 	    CTLTYPE_INT, "middle", "middle mouse button", adbkbd_sysctl_button,
    634 		    1, NULL, 0, CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL);
    635 	node->sysctl_data = sc;
    636 
    637 	ret = sysctl_createv(NULL, 0, NULL,
    638 	    (const struct sysctlnode **)&node,
    639 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC | CTLFLAG_IMMEDIATE,
    640 	    CTLTYPE_INT, "right", "right mouse button", adbkbd_sysctl_button,
    641 		    2, NULL, 0, CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL);
    642 	node->sysctl_data = sc;
    643 }
    644 
    645 static int
    646 adbkbd_sysctl_button(SYSCTLFN_ARGS)
    647 {
    648 	struct sysctlnode node = *rnode;
    649 	struct adbkbd_softc *sc=(struct adbkbd_softc *)node.sysctl_data;
    650 	const int *np = newp;
    651 	int btn = node.sysctl_idata, reg;
    652 
    653 	DPRINTF("adbkbd_sysctl_button %d\n", btn);
    654 	node.sysctl_idata = sc->sc_trans[btn];
    655 	reg = sc->sc_trans[btn];
    656 	if (np) {
    657 		/* we're asked to write */
    658 		node.sysctl_data = &reg;
    659 		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
    660 
    661 			sc->sc_trans[btn] = node.sysctl_idata;
    662 			return 0;
    663 		}
    664 		return EINVAL;
    665 	} else {
    666 		node.sysctl_size = 4;
    667 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    668 	}
    669 }
    670 
    671 SYSCTL_SETUP(sysctl_adbkbdtrans_setup, "adbkbd translator setup")
    672 {
    673 
    674 	sysctl_createv(NULL, 0, NULL, NULL,
    675 		       CTLFLAG_PERMANENT,
    676 		       CTLTYPE_NODE, "machdep", NULL,
    677 		       NULL, 0, NULL, 0,
    678 		       CTL_MACHDEP, CTL_EOL);
    679 }
    680