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