Home | History | Annotate | Line # | Download | only in adb
adb_kbd.c revision 1.6
      1 /*	$NetBSD: adb_kbd.c,v 1.6 2007/04/16 00:16:43 macallan 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.6 2007/04/16 00:16:43 macallan 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 	uint8_t sc_power;
     87 };
     88 
     89 /*
     90  * Function declarations.
     91  */
     92 static int	adbkbd_match(struct device *, struct cfdata *, void *);
     93 static void	adbkbd_attach(struct device *, struct device *, void *);
     94 
     95 static void	adbkbd_initleds(struct adbkbd_softc *);
     96 static void	adbkbd_keys(struct adbkbd_softc *, uint8_t, uint8_t);
     97 static inline void adbkbd_key(struct adbkbd_softc *, uint8_t);
     98 static int	adbkbd_wait(struct adbkbd_softc *, int);
     99 
    100 /* Driver definition. */
    101 CFATTACH_DECL(adbkbd, sizeof(struct adbkbd_softc),
    102     adbkbd_match, adbkbd_attach, NULL, NULL);
    103 
    104 extern struct cfdriver akbd_cd;
    105 
    106 static int adbkbd_enable(void *, int);
    107 static int adbkbd_ioctl(void *, u_long, void *, int, struct lwp *);
    108 static void adbkbd_set_leds(void *, int);
    109 static void adbkbd_handler(void *, int, uint8_t *);
    110 
    111 struct wskbd_accessops adbkbd_accessops = {
    112 	adbkbd_enable,
    113 	adbkbd_set_leds,
    114 	adbkbd_ioctl,
    115 };
    116 
    117 static void adbkbd_cngetc(void *, u_int *, int *);
    118 static void adbkbd_cnpollc(void *, int);
    119 
    120 struct wskbd_consops adbkbd_consops = {
    121 	adbkbd_cngetc,
    122 	adbkbd_cnpollc,
    123 };
    124 
    125 struct wskbd_mapdata adbkbd_keymapdata = {
    126 	akbd_keydesctab,
    127 #ifdef AKBD_LAYOUT
    128 	AKBD_LAYOUT,
    129 #else
    130 	KB_US,
    131 #endif
    132 };
    133 
    134 static int adbkms_enable(void *);
    135 static int adbkms_ioctl(void *, u_long, void *, int, struct lwp *);
    136 static void adbkms_disable(void *);
    137 
    138 const struct wsmouse_accessops adbkms_accessops = {
    139 	adbkms_enable,
    140 	adbkms_ioctl,
    141 	adbkms_disable,
    142 };
    143 
    144 static int  adbkbd_sysctl_button(SYSCTLFN_ARGS);
    145 static void adbkbd_setup_sysctl(struct adbkbd_softc *);
    146 
    147 #ifdef ADBKBD_DEBUG
    148 #define DPRINTF printf
    149 #else
    150 #define DPRINTF while (0) printf
    151 #endif
    152 
    153 static int adbkbd_is_console = 0;
    154 static int adbkbd_console_attached = 0;
    155 
    156 static int
    157 adbkbd_match(parent, cf, aux)
    158 	struct device *parent;
    159 	struct cfdata *cf;
    160 	void   *aux;
    161 {
    162 	struct adb_attach_args *aaa = aux;
    163 
    164 	if (aaa->dev->original_addr == ADBADDR_KBD)
    165 		return 1;
    166 	else
    167 		return 0;
    168 }
    169 
    170 static void
    171 adbkbd_attach(struct device *parent, struct device *self, void *aux)
    172 {
    173 	struct adbkbd_softc *sc = (struct adbkbd_softc *)self;
    174 	struct adb_attach_args *aaa = aux;
    175 	short cmd;
    176 	struct wskbddev_attach_args a;
    177 	struct wsmousedev_attach_args am;
    178 
    179 	sc->sc_ops = aaa->ops;
    180 	sc->sc_adbdev = aaa->dev;
    181 	sc->sc_adbdev->cookie = sc;
    182 	sc->sc_adbdev->handler = adbkbd_handler;
    183 	sc->sc_us = ADBTALK(sc->sc_adbdev->current_addr, 0);
    184 
    185 	sc->sc_leds = 0;	/* initially off */
    186 	sc->sc_have_led_control = 0;
    187 	sc->sc_msg_len = 0;
    188 	sc->sc_poll = 0;
    189 	sc->sc_capslock = 0;
    190 	sc->sc_trans[1] = 103;	/* F11 */
    191 	sc->sc_trans[2] = 111;	/* F12 */
    192 	sc->sc_power = 0x7f;
    193 
    194 	printf(" addr %d ", sc->sc_adbdev->current_addr);
    195 
    196 	switch (sc->sc_adbdev->handler_id) {
    197 	case ADB_STDKBD:
    198 		printf("standard keyboard\n");
    199 		break;
    200 	case ADB_ISOKBD:
    201 		printf("standard keyboard (ISO layout)\n");
    202 		break;
    203 	case ADB_EXTKBD:
    204 		cmd = ADBTALK(sc->sc_adbdev->current_addr, 1);
    205 		sc->sc_msg_len = 0;
    206 		sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 0, NULL);
    207 		adbkbd_wait(sc, 10);
    208 
    209 		/* Ignore Logitech MouseMan/Trackman pseudo keyboard */
    210 		/* XXX needs testing */
    211 		if (sc->sc_buffer[2] == 0x9a && sc->sc_buffer[3] == 0x20) {
    212 			printf("Mouseman (non-EMP) pseudo keyboard\n");
    213 			return;
    214 		} else if (sc->sc_buffer[2] == 0x9a &&
    215 		    sc->sc_buffer[3] == 0x21) {
    216 			printf("Trackman (non-EMP) pseudo keyboard\n");
    217 			return;
    218 		} else {
    219 			printf("extended keyboard\n");
    220 			adbkbd_initleds(sc);
    221 		}
    222 		break;
    223 	case ADB_EXTISOKBD:
    224 		printf("extended keyboard (ISO layout)\n");
    225 		adbkbd_initleds(sc);
    226 		break;
    227 	case ADB_KBDII:
    228 		printf("keyboard II\n");
    229 		break;
    230 	case ADB_ISOKBDII:
    231 		printf("keyboard II (ISO layout)\n");
    232 		break;
    233 	case ADB_PBKBD:
    234 		printf("PowerBook keyboard\n");
    235 		sc->sc_power = 0x7e;
    236 		break;
    237 	case ADB_PBISOKBD:
    238 		printf("PowerBook keyboard (ISO layout)\n");
    239 		sc->sc_power = 0x7e;
    240 		break;
    241 	case ADB_ADJKPD:
    242 		printf("adjustable keypad\n");
    243 		break;
    244 	case ADB_ADJKBD:
    245 		printf("adjustable keyboard\n");
    246 		break;
    247 	case ADB_ADJISOKBD:
    248 		printf("adjustable keyboard (ISO layout)\n");
    249 		break;
    250 	case ADB_ADJJAPKBD:
    251 		printf("adjustable keyboard (Japanese layout)\n");
    252 		break;
    253 	case ADB_PBEXTISOKBD:
    254 		printf("PowerBook extended keyboard (ISO layout)\n");
    255 		sc->sc_power = 0x7e;
    256 		break;
    257 	case ADB_PBEXTJAPKBD:
    258 		printf("PowerBook extended keyboard (Japanese layout)\n");
    259 		sc->sc_power = 0x7e;
    260 		break;
    261 	case ADB_JPKBDII:
    262 		printf("keyboard II (Japanese layout)\n");
    263 		break;
    264 	case ADB_PBEXTKBD:
    265 		printf("PowerBook extended keyboard\n");
    266 		sc->sc_power = 0x7e;
    267 		break;
    268 	case ADB_DESIGNKBD:
    269 		printf("extended keyboard\n");
    270 		adbkbd_initleds(sc);
    271 		break;
    272 	case ADB_PBJPKBD:
    273 		printf("PowerBook keyboard (Japanese layout)\n");
    274 		sc->sc_power = 0x7e;
    275 		break;
    276 	case ADB_PBG3KBD:
    277 		printf("PowerBook G3 keyboard\n");
    278 		sc->sc_power = 0x7e;
    279 		break;
    280 	case ADB_PBG3JPKBD:
    281 		printf("PowerBook G3 keyboard (Japanese layout)\n");
    282 		sc->sc_power = 0x7e;
    283 		break;
    284 	case ADB_IBOOKKBD:
    285 		printf("iBook keyboard\n");
    286 		break;
    287 	default:
    288 		printf("mapped device (%d)\n", sc->sc_adbdev->handler_id);
    289 		break;
    290 	}
    291 
    292 	if (adbkbd_is_console && (adbkbd_console_attached == 0)) {
    293 		wskbd_cnattach(&adbkbd_consops, sc, &adbkbd_keymapdata);
    294 		adbkbd_console_attached = 1;
    295 		a.console = 1;
    296 	} else {
    297 		a.console = 0;
    298 	}
    299 	a.keymap = &adbkbd_keymapdata;
    300 	a.accessops = &adbkbd_accessops;
    301 	a.accesscookie = sc;
    302 
    303 	sc->sc_wskbddev = config_found_ia(self, "wskbddev", &a, wskbddevprint);
    304 
    305 	/* attach the mouse device */
    306 	am.accessops = &adbkms_accessops;
    307 	am.accesscookie = sc;
    308 	sc->sc_wsmousedev = config_found_ia(self, "wsmousedev", &am, wsmousedevprint);
    309 
    310 	if (sc->sc_wsmousedev != NULL)
    311 		adbkbd_setup_sysctl(sc);
    312 
    313 	/* finally register the power button */
    314 	sysmon_task_queue_init();
    315 	memset(&sc->sc_sm_pbutton, 0, sizeof(struct sysmon_pswitch));
    316 	sc->sc_sm_pbutton.smpsw_name = sc->sc_dev.dv_xname;
    317 	sc->sc_sm_pbutton.smpsw_type = PSWITCH_TYPE_POWER;
    318 	if (sysmon_pswitch_register(&sc->sc_sm_pbutton) != 0)
    319 		printf("%s: unable to register power button with sysmon\n",
    320 		    sc->sc_dev.dv_xname);
    321 }
    322 
    323 static void
    324 adbkbd_handler(void *cookie, int len, uint8_t *data)
    325 {
    326 	struct adbkbd_softc *sc = cookie;
    327 
    328 #ifdef ADBKBD_DEBUG
    329 	int i;
    330 	printf("%s: %02x - ", sc->sc_dev.dv_xname, sc->sc_us);
    331 	for (i = 0; i < len; i++) {
    332 		printf(" %02x", data[i]);
    333 	}
    334 	printf("\n");
    335 #endif
    336 	if (len >= 2) {
    337 		if (data[1] == sc->sc_us) {
    338 			adbkbd_keys(sc, data[2], data[3]);
    339 			return;
    340 		} else {
    341 			memcpy(sc->sc_buffer, data, len);
    342 		}
    343 		sc->sc_msg_len = len;
    344 		wakeup(&sc->sc_event);
    345 	} else {
    346 		DPRINTF("bogus message\n");
    347 	}
    348 }
    349 
    350 static int
    351 adbkbd_wait(struct adbkbd_softc *sc, int timeout)
    352 {
    353 	int cnt = 0;
    354 
    355 	if (sc->sc_poll) {
    356 		while (sc->sc_msg_len == 0) {
    357 			sc->sc_ops->poll(sc->sc_ops->cookie);
    358 		}
    359 	} else {
    360 		while ((sc->sc_msg_len == 0) && (cnt < timeout)) {
    361 			tsleep(&sc->sc_event, 0, "adbkbdio", hz);
    362 			cnt++;
    363 		}
    364 	}
    365 	return (sc->sc_msg_len > 0);
    366 }
    367 
    368 static void
    369 adbkbd_keys(struct adbkbd_softc *sc, uint8_t k1, uint8_t k2)
    370 {
    371 
    372 	/* keyboard event processing */
    373 
    374 	DPRINTF("[%02x %02x]", k1, k2);
    375 
    376 	if (((k1 == k2) && (k1 == 0x7f)) || (k1 == sc->sc_power)) {
    377 
    378 		/* power button, report to sysmon */
    379 #if 1
    380 		sysmon_pswitch_event(&sc->sc_sm_pbutton,
    381 		    ADBK_PRESS(k1) ? PSWITCH_EVENT_PRESSED :
    382 		    PSWITCH_EVENT_RELEASED);
    383 #else
    384 		printf("[%02x %02x]", k1, k2);
    385 #endif
    386 	} else {
    387 
    388 		adbkbd_key(sc, k1);
    389 		if (k2 != 0xff)
    390 			adbkbd_key(sc, k2);
    391 	}
    392 }
    393 
    394 static inline void
    395 adbkbd_key(struct adbkbd_softc *sc, uint8_t k)
    396 {
    397 
    398 	if (sc->sc_poll) {
    399 		if (sc->sc_polled_chars >= 16) {
    400 			printf("%s: polling buffer is full\n",
    401 			    sc->sc_dev.dv_xname);
    402 		}
    403 		sc->sc_pollbuf[sc->sc_polled_chars] = k;
    404 		sc->sc_polled_chars++;
    405 		return;
    406 	}
    407 
    408 	/* translate some keys to mouse events */
    409 	if (sc->sc_wsmousedev != NULL) {
    410 		if (ADBK_KEYVAL(k) == sc->sc_trans[1]) {
    411 			wsmouse_input(sc->sc_wsmousedev, ADBK_PRESS(k) ? 2 : 0,
    412 			      0, 0, 0, 0,
    413 			      WSMOUSE_INPUT_DELTA);
    414 			return;
    415 		}
    416 		if (ADBK_KEYVAL(k) == sc->sc_trans[2]) {
    417 			wsmouse_input(sc->sc_wsmousedev, ADBK_PRESS(k) ? 4 : 0,
    418 			      0, 0, 0, 0,
    419 			      WSMOUSE_INPUT_DELTA);
    420 			return;
    421 		}
    422 	}
    423 #ifdef WSDISPLAY_COMPAT_RAWKBD
    424 	if (sc->sc_rawkbd) {
    425 		char cbuf[2];
    426 		int s;
    427 		int j = 0;
    428 		int c = keyboard[ADBK_KEYVAL(k)][3]
    429 
    430 		if (k & 0x80)
    431 			cbuf[j++] = 0xe0;
    432 
    433 		cbuf[j++] = (c & 0x7f) | (ADBK_PRESS(k)? 0 : 0x80);
    434 
    435 		s = spltty();
    436 		wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
    437 		splx(s);
    438 	} else {
    439 #endif
    440 
    441 	if (ADBK_KEYVAL(k) == 0x39) {
    442 		/* caps lock - send up and down */
    443 		if (ADBK_PRESS(k) != sc->sc_capslock) {
    444 			sc->sc_capslock = ADBK_PRESS(k);
    445 			wskbd_input(sc->sc_wskbddev,
    446 			    WSCONS_EVENT_KEY_DOWN, 0x39);
    447 			wskbd_input(sc->sc_wskbddev,
    448 			    WSCONS_EVENT_KEY_UP, 0x39);
    449 		}
    450 	} else {
    451 		/* normal event */
    452 		int type;
    453 
    454 		type = ADBK_PRESS(k) ?
    455 		    WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
    456 		wskbd_input(sc->sc_wskbddev, type, ADBK_KEYVAL(k));
    457 	}
    458 #ifdef WSDISPLAY_COMPAT_RAWKBD
    459 	}
    460 #endif
    461 }
    462 
    463 /*
    464  * Set the keyboard LED's.
    465  *
    466  * Automatically translates from ioctl/softc format to the
    467  * actual keyboard register format
    468  */
    469 static void
    470 adbkbd_set_leds(void *cookie, int leds)
    471 {
    472 	struct adbkbd_softc *sc = cookie;
    473 	int aleds;
    474 	short cmd;
    475 	uint8_t buffer[2];
    476 
    477 	DPRINTF("adbkbd_set_leds: %02x\n", leds);
    478 	if ((leds & 0x07) == (sc->sc_leds & 0x07))
    479 		return;
    480 
    481  	if (sc->sc_have_led_control) {
    482 
    483 		aleds = (~leds & 0x04) | 3;
    484 		if (leds & 1)
    485 			aleds &= ~2;
    486 		if (leds & 2)
    487 			aleds &= ~1;
    488 
    489 		buffer[0] = 0xff;
    490 		buffer[1] = aleds | 0xf8;
    491 
    492 		cmd = ADBLISTEN(sc->sc_adbdev->current_addr, 2);
    493 		sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 2, buffer);
    494 	}
    495 
    496 	sc->sc_leds = leds & 7;
    497 }
    498 
    499 static void
    500 adbkbd_initleds(struct adbkbd_softc *sc)
    501 {
    502 	short cmd;
    503 
    504 	/* talk R2 */
    505 	cmd = ADBTALK(sc->sc_adbdev->current_addr, 2);
    506 	sc->sc_msg_len = 0;
    507 	sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 0, NULL);
    508 	if (!adbkbd_wait(sc, 10)) {
    509 		printf("unable to read LED state\n");
    510 		return;
    511 	}
    512 	sc->sc_have_led_control = 1;
    513 	DPRINTF("have LED control\n");
    514 	return;
    515 }
    516 
    517 static int
    518 adbkbd_enable(void *v, int on)
    519 {
    520 	return 0;
    521 }
    522 
    523 static int
    524 adbkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    525 {
    526 	struct adbkbd_softc *sc = (struct adbkbd_softc *) v;
    527 
    528 	switch (cmd) {
    529 
    530 	case WSKBDIO_GTYPE:
    531 		*(int *)data = WSKBD_TYPE_ADB;
    532 		return 0;
    533 	case WSKBDIO_SETLEDS:
    534 		adbkbd_set_leds(sc, *(int *)data);
    535 		return 0;
    536 	case WSKBDIO_GETLEDS:
    537 		*(int *)data = sc->sc_leds;
    538 		return 0;
    539 #ifdef WSDISPLAY_COMPAT_RAWKBD
    540 	case WSKBDIO_SETMODE:
    541 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
    542 		return 0;
    543 #endif
    544 	}
    545 
    546 	return EPASSTHROUGH;
    547 }
    548 
    549 int
    550 adbkbd_cnattach()
    551 {
    552 
    553 	adbkbd_is_console = 1;
    554 	return 0;
    555 }
    556 
    557 static void
    558 adbkbd_cngetc(void *v, u_int *type, int *data)
    559 {
    560 	struct adbkbd_softc *sc = v;
    561 	int key, press, val;
    562 	int s;
    563 
    564 	s = splhigh();
    565 
    566 	KASSERT(sc->sc_poll);
    567 
    568 	DPRINTF("polling...");
    569 	while (sc->sc_polled_chars == 0) {
    570 		sc->sc_ops->poll(sc->sc_ops->cookie);
    571 	}
    572 	DPRINTF(" got one\n");
    573 	splx(s);
    574 
    575 	key = sc->sc_pollbuf[0];
    576 	sc->sc_polled_chars--;
    577 	memmove(sc->sc_pollbuf, sc->sc_pollbuf + 1,
    578 		sc->sc_polled_chars);
    579 
    580 	press = ADBK_PRESS(key);
    581 	val = ADBK_KEYVAL(key);
    582 
    583 	*data = val;
    584 	*type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
    585 }
    586 
    587 static void
    588 adbkbd_cnpollc(void *v, int on)
    589 {
    590 	struct adbkbd_softc *sc = v;
    591 
    592 	sc->sc_poll = on;
    593 	if (!on) {
    594 		int i;
    595 
    596 		/* feed the poll buffer's content to wskbd */
    597 		for (i = 0; i < sc->sc_polled_chars; i++) {
    598 			adbkbd_key(sc, sc->sc_pollbuf[i]);
    599 		}
    600 		sc->sc_polled_chars = 0;
    601 	}
    602 }
    603 
    604 /* stuff for the pseudo mouse */
    605 static int
    606 adbkms_enable(void *v)
    607 {
    608 	return 0;
    609 }
    610 
    611 static int
    612 adbkms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    613 {
    614 
    615 	switch (cmd) {
    616 	case WSMOUSEIO_GTYPE:
    617 		*(u_int *)data = WSMOUSE_TYPE_PSEUDO;
    618 		break;
    619 
    620 	default:
    621 		return (EPASSTHROUGH);
    622 	}
    623 	return (0);
    624 }
    625 
    626 static void
    627 adbkms_disable(void *v)
    628 {
    629 }
    630 
    631 static void
    632 adbkbd_setup_sysctl(struct adbkbd_softc *sc)
    633 {
    634 	struct sysctlnode *node, *me;
    635 	int ret;
    636 
    637 	DPRINTF("%s: sysctl setup\n", sc->sc_dev.dv_xname);
    638 	ret = sysctl_createv(NULL, 0, NULL, (const struct sysctlnode **)&me,
    639 	       CTLFLAG_READWRITE,
    640 	       CTLTYPE_NODE, sc->sc_dev.dv_xname, NULL,
    641 	       NULL, 0, NULL, 0,
    642 	       CTL_MACHDEP, CTL_CREATE, CTL_EOL);
    643 
    644 	ret = sysctl_createv(NULL, 0, NULL,
    645 	    (const struct sysctlnode **)&node,
    646 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC | CTLFLAG_IMMEDIATE,
    647 	    CTLTYPE_INT, "middle", "middle mouse button", adbkbd_sysctl_button,
    648 		    1, NULL, 0, CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL);
    649 	node->sysctl_data = sc;
    650 
    651 	ret = sysctl_createv(NULL, 0, NULL,
    652 	    (const struct sysctlnode **)&node,
    653 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC | CTLFLAG_IMMEDIATE,
    654 	    CTLTYPE_INT, "right", "right mouse button", adbkbd_sysctl_button,
    655 		    2, NULL, 0, CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL);
    656 	node->sysctl_data = sc;
    657 }
    658 
    659 static int
    660 adbkbd_sysctl_button(SYSCTLFN_ARGS)
    661 {
    662 	struct sysctlnode node = *rnode;
    663 	struct adbkbd_softc *sc=(struct adbkbd_softc *)node.sysctl_data;
    664 	const int *np = newp;
    665 	int btn = node.sysctl_idata, reg;
    666 
    667 	DPRINTF("adbkbd_sysctl_button %d\n", btn);
    668 	node.sysctl_idata = sc->sc_trans[btn];
    669 	reg = sc->sc_trans[btn];
    670 	if (np) {
    671 		/* we're asked to write */
    672 		node.sysctl_data = &reg;
    673 		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
    674 
    675 			sc->sc_trans[btn] = node.sysctl_idata;
    676 			return 0;
    677 		}
    678 		return EINVAL;
    679 	} else {
    680 		node.sysctl_size = 4;
    681 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    682 	}
    683 }
    684 
    685 SYSCTL_SETUP(sysctl_adbkbdtrans_setup, "adbkbd translator setup")
    686 {
    687 
    688 	sysctl_createv(NULL, 0, NULL, NULL,
    689 		       CTLFLAG_PERMANENT,
    690 		       CTLTYPE_NODE, "machdep", NULL,
    691 		       NULL, 0, NULL, 0,
    692 		       CTL_MACHDEP, CTL_EOL);
    693 }
    694