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