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