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