xref: /netbsd-src/sys/dev/adb/adb_kbd.c (revision f89f6560d453f5e37386cc7938c072d2f528b9fa)
1 /*	$NetBSD: adb_kbd.c,v 1.24 2014/11/08 17:21:51 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.24 2014/11/08 17:21:51 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 	uint32_t sc_power;
93 	uint8_t sc_buffer[16];
94 	uint8_t sc_pollbuf[16];
95 	uint8_t sc_us, 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 
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 	sc->sc_us = ADBTALK(sc->sc_adbdev->current_addr, 0);
201 
202 	sc->sc_leds = 0;	/* initially off */
203 	sc->sc_have_led_control = 0;
204 
205 	/*
206 	 * If this is != 0 then pushing the power button will not immadiately
207 	 * send a shutdown event to sysmon but instead require another key
208 	 * press within 5 seconds with a gap of at least two seconds. The
209 	 * reason to do this is the fact that some PowerBook keyboards,
210 	 * like the 2400, 3400 and original G3 have their power buttons
211 	 * right next to the backspace key and it's extremely easy to hit
212 	 * it by accident.
213 	 * On most other keyboards the power button is sufficiently far out
214 	 * of the way so we don't need this.
215 	 */
216 	sc->sc_power_button_delay = 0;
217 	sc->sc_msg_len = 0;
218 	sc->sc_poll = 0;
219 	sc->sc_capslock = 0;
220 	sc->sc_trans[1] = 103;	/* F11 */
221 	sc->sc_trans[2] = 111;	/* F12 */
222 
223 	/*
224 	 * Most ADB keyboards send 0x7f 0x7f when the power button is pressed.
225 	 * Some older PowerBooks, like the 3400c, will send a single scancode
226 	 * 0x7e instead. Unfortunately Fn-Command on some more recent *Books
227 	 * sends the same scancode, so by default sc_power is set to a value
228 	 * that can't occur as a scancode and only set to 0x7e on hardware that
229 	 * needs it
230 	 */
231 	sc->sc_power = 0xffff;
232 	sc->sc_timestamp = 0;
233 	sc->sc_emul_usb = FALSE;
234 
235 	aprint_normal(" addr %d: ", sc->sc_adbdev->current_addr);
236 
237 	switch (sc->sc_adbdev->handler_id) {
238 	case ADB_STDKBD:
239 		aprint_normal("standard keyboard\n");
240 		break;
241 	case ADB_ISOKBD:
242 		aprint_normal("standard keyboard (ISO layout)\n");
243 		break;
244 	case ADB_EXTKBD:
245 		cmd = ADBTALK(sc->sc_adbdev->current_addr, 1);
246 		sc->sc_msg_len = 0;
247 		sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 0, NULL);
248 		adbkbd_wait(sc, 10);
249 
250 		/* Ignore Logitech MouseMan/Trackman pseudo keyboard */
251 		/* XXX needs testing */
252 		if (sc->sc_buffer[2] == 0x9a && sc->sc_buffer[3] == 0x20) {
253 			aprint_normal("Mouseman (non-EMP) pseudo keyboard\n");
254 			return;
255 		} else if (sc->sc_buffer[2] == 0x9a &&
256 		    sc->sc_buffer[3] == 0x21) {
257 			aprint_normal("Trackman (non-EMP) pseudo keyboard\n");
258 			return;
259 		} else {
260 			aprint_normal("extended keyboard\n");
261 			adbkbd_initleds(sc);
262 		}
263 		break;
264 	case ADB_EXTISOKBD:
265 		aprint_normal("extended keyboard (ISO layout)\n");
266 		adbkbd_initleds(sc);
267 		break;
268 	case ADB_KBDII:
269 		aprint_normal("keyboard II\n");
270 		break;
271 	case ADB_ISOKBDII:
272 		aprint_normal("keyboard II (ISO layout)\n");
273 		break;
274 	case ADB_PBKBD:
275 		aprint_normal("PowerBook keyboard\n");
276 		sc->sc_power = 0x7e;
277 		sc->sc_power_button_delay = 1;
278 		break;
279 	case ADB_PBISOKBD:
280 		aprint_normal("PowerBook keyboard (ISO layout)\n");
281 		sc->sc_power = 0x7e;
282 		sc->sc_power_button_delay = 1;
283 		break;
284 	case ADB_ADJKPD:
285 		aprint_normal("adjustable keypad\n");
286 		break;
287 	case ADB_ADJKBD:
288 		aprint_normal("adjustable keyboard\n");
289 		break;
290 	case ADB_ADJISOKBD:
291 		aprint_normal("adjustable keyboard (ISO layout)\n");
292 		break;
293 	case ADB_ADJJAPKBD:
294 		aprint_normal("adjustable keyboard (Japanese layout)\n");
295 		break;
296 	case ADB_PBEXTISOKBD:
297 		aprint_normal("PowerBook extended keyboard (ISO layout)\n");
298 		sc->sc_power_button_delay = 1;
299 		sc->sc_power = 0x7e;
300 		break;
301 	case ADB_PBEXTJAPKBD:
302 		aprint_normal("PowerBook extended keyboard (Japanese layout)\n");
303 		sc->sc_power_button_delay = 1;
304 		sc->sc_power = 0x7e;
305 		break;
306 	case ADB_JPKBDII:
307 		aprint_normal("keyboard II (Japanese layout)\n");
308 		break;
309 	case ADB_PBEXTKBD:
310 		aprint_normal("PowerBook extended keyboard\n");
311 		sc->sc_power_button_delay = 1;
312 		sc->sc_power = 0x7e;
313 		break;
314 	case ADB_DESIGNKBD:
315 		aprint_normal("extended keyboard\n");
316 		adbkbd_initleds(sc);
317 		break;
318 	case ADB_PBJPKBD:
319 		aprint_normal("PowerBook keyboard (Japanese layout)\n");
320 		sc->sc_power_button_delay = 1;
321 		sc->sc_power = 0x7e;
322 		break;
323 	case ADB_PBG3KBD:
324 		aprint_normal("PowerBook G3 keyboard\n");
325 		break;
326 	case ADB_PBG3JPKBD:
327 		aprint_normal("PowerBook G3 keyboard (Japanese layout)\n");
328 		break;
329 	case ADB_IBOOKKBD:
330 		aprint_normal("iBook keyboard\n");
331 		break;
332 	default:
333 		aprint_normal("mapped device (%d)\n", sc->sc_adbdev->handler_id);
334 		break;
335 	}
336 
337 	/*
338 	 * try to switch to extended protocol
339 	 * as in, tell the keyboard to distinguish between left and right
340 	 * Shift, Control and Alt keys
341 	 */
342 	cmd = ADBLISTEN(sc->sc_adbdev->current_addr, 3);
343 	buffer[0] = sc->sc_adbdev->current_addr;
344 	buffer[1] = 3;
345 	sc->sc_msg_len = 0;
346 	sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 2, buffer);
347 	adbkbd_wait(sc, 10);
348 
349 	cmd = ADBTALK(sc->sc_adbdev->current_addr, 3);
350 	sc->sc_msg_len = 0;
351 	sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 0, NULL);
352 	adbkbd_wait(sc, 10);
353 	if ((sc->sc_msg_len == 4) && (sc->sc_buffer[3] == 3)) {
354 		aprint_verbose_dev(sc->sc_dev, "extended protocol enabled\n");
355 	}
356 
357 	if (adbkbd_is_console && (adbkbd_console_attached == 0)) {
358 		wskbd_cnattach(&adbkbd_consops, sc, &adbkbd_keymapdata);
359 		adbkbd_console_attached = 1;
360 		a.console = 1;
361 	} else {
362 		a.console = 0;
363 	}
364 	a.keymap = &adbkbd_keymapdata;
365 	a.accessops = &adbkbd_accessops;
366 	a.accesscookie = sc;
367 
368 	sc->sc_wskbddev = config_found_ia(self, "wskbddev", &a, wskbddevprint);
369 #ifdef ADBKBD_EMUL_USB
370 	sc->sc_emul_usb = TRUE;
371 	wskbd_set_evtrans(sc->sc_wskbddev, adb_to_usb, 128);
372 #endif /* ADBKBD_EMUL_USB */
373 
374 #if NWSMOUSE > 0
375 	/* attach the mouse device */
376 	am.accessops = &adbkms_accessops;
377 	am.accesscookie = sc;
378 	sc->sc_wsmousedev = config_found_ia(self, "wsmousedev", &am,
379 	    wsmousedevprint);
380 
381 #endif
382 	adbkbd_setup_sysctl(sc);
383 
384 	/* finally register the power button */
385 	sysmon_task_queue_init();
386 	memset(&sc->sc_sm_pbutton, 0, sizeof(struct sysmon_pswitch));
387 	sc->sc_sm_pbutton.smpsw_name = device_xname(sc->sc_dev);
388 	sc->sc_sm_pbutton.smpsw_type = PSWITCH_TYPE_POWER;
389 	if (sysmon_pswitch_register(&sc->sc_sm_pbutton) != 0)
390 		aprint_error_dev(sc->sc_dev,
391 		    "unable to register power button with sysmon\n");
392 }
393 
394 static void
395 adbkbd_handler(void *cookie, int len, uint8_t *data)
396 {
397 	struct adbkbd_softc *sc = cookie;
398 
399 #ifdef ADBKBD_DEBUG
400 	int i;
401 	printf("%s: %02x - ", device_xname(sc->sc_dev), sc->sc_us);
402 	for (i = 0; i < len; i++) {
403 		printf(" %02x", data[i]);
404 	}
405 	printf("\n");
406 #endif
407 	if (len >= 2) {
408 		if (data[1] == sc->sc_us) {
409 			adbkbd_keys(sc, data[2], data[3]);
410 			return;
411 		} else {
412 			memcpy(sc->sc_buffer, data, len);
413 		}
414 		sc->sc_msg_len = len;
415 		wakeup(&sc->sc_event);
416 	} else {
417 		DPRINTF("bogus message\n");
418 	}
419 }
420 
421 static int
422 adbkbd_wait(struct adbkbd_softc *sc, int timeout)
423 {
424 	int cnt = 0;
425 
426 	if (sc->sc_poll) {
427 		while (sc->sc_msg_len == 0) {
428 			sc->sc_ops->poll(sc->sc_ops->cookie);
429 		}
430 	} else {
431 		while ((sc->sc_msg_len == 0) && (cnt < timeout)) {
432 			tsleep(&sc->sc_event, 0, "adbkbdio", hz);
433 			cnt++;
434 		}
435 	}
436 	return (sc->sc_msg_len > 0);
437 }
438 
439 static void
440 adbkbd_keys(struct adbkbd_softc *sc, uint8_t k1, uint8_t k2)
441 {
442 
443 	/* keyboard event processing */
444 
445 	DPRINTF("[%02x %02x]", k1, k2);
446 
447 	if (((k1 == k2) && (k1 == 0x7f)) || (k1 == sc->sc_power)) {
448 		uint32_t now = time_second;
449 		uint32_t diff = now - sc->sc_timestamp;
450 
451 		sc->sc_timestamp = now;
452 		if (((diff > 1) && (diff < 5)) ||
453 		     (sc->sc_power_button_delay == 0)) {
454 
455 			/* power button, report to sysmon */
456 			sc->sc_pe = k1;
457 			sysmon_task_queue_sched(0, adbkbd_powerbutton, sc);
458 		}
459 	} else {
460 
461 		adbkbd_key(sc, k1);
462 		if (k2 != 0xff)
463 			adbkbd_key(sc, k2);
464 	}
465 }
466 
467 static void
468 adbkbd_powerbutton(void *cookie)
469 {
470 	struct adbkbd_softc *sc = cookie;
471 
472 	sysmon_pswitch_event(&sc->sc_sm_pbutton,
473 	    ADBK_PRESS(sc->sc_pe) ? PSWITCH_EVENT_PRESSED :
474 	    PSWITCH_EVENT_RELEASED);
475 }
476 
477 static inline void
478 adbkbd_key(struct adbkbd_softc *sc, uint8_t k)
479 {
480 
481 	if (sc->sc_poll) {
482 		if (sc->sc_polled_chars >= 16) {
483 			aprint_error_dev(sc->sc_dev,"polling buffer is full\n");
484 		}
485 		sc->sc_pollbuf[sc->sc_polled_chars] = k;
486 		sc->sc_polled_chars++;
487 		return;
488 	}
489 
490 #if NWSMOUSE > 0
491 	/* translate some keys to mouse events */
492 	if (sc->sc_wsmousedev != NULL) {
493 		if (ADBK_KEYVAL(k) == sc->sc_trans[1]) {
494 			wsmouse_input(sc->sc_wsmousedev, ADBK_PRESS(k) ? 2 : 0,
495 			      0, 0, 0, 0,
496 			      WSMOUSE_INPUT_DELTA);
497 			return;
498 		}
499 		if (ADBK_KEYVAL(k) == sc->sc_trans[2]) {
500 			wsmouse_input(sc->sc_wsmousedev, ADBK_PRESS(k) ? 4 : 0,
501 			      0, 0, 0, 0,
502 			      WSMOUSE_INPUT_DELTA);
503 			return;
504 		}
505 	}
506 #endif
507 
508 #ifdef WSDISPLAY_COMPAT_RAWKBD
509 	if (sc->sc_rawkbd) {
510 		char cbuf[2];
511 		int s;
512 
513 		cbuf[0] = k;
514 
515 		s = spltty();
516 		wskbd_rawinput(sc->sc_wskbddev, cbuf, 1);
517 		splx(s);
518 	} else {
519 #endif
520 
521 	if (ADBK_KEYVAL(k) == 0x39) {
522 		/* caps lock - send up and down */
523 		if (ADBK_PRESS(k) != sc->sc_capslock) {
524 			sc->sc_capslock = ADBK_PRESS(k);
525 			wskbd_input(sc->sc_wskbddev,
526 			    WSCONS_EVENT_KEY_DOWN, 0x39);
527 			wskbd_input(sc->sc_wskbddev,
528 			    WSCONS_EVENT_KEY_UP, 0x39);
529 		}
530 	} else {
531 		/* normal event */
532 		int type;
533 
534 		type = ADBK_PRESS(k) ?
535 		    WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
536 		wskbd_input(sc->sc_wskbddev, type, ADBK_KEYVAL(k));
537 	}
538 #ifdef WSDISPLAY_COMPAT_RAWKBD
539 	}
540 #endif
541 }
542 
543 /*
544  * Set the keyboard LED's.
545  *
546  * Automatically translates from ioctl/softc format to the
547  * actual keyboard register format
548  */
549 static void
550 adbkbd_set_leds(void *cookie, int leds)
551 {
552 	struct adbkbd_softc *sc = cookie;
553 	int aleds;
554 	short cmd;
555 	uint8_t buffer[2];
556 
557 	DPRINTF("adbkbd_set_leds: %02x\n", leds);
558 	if ((leds & 0x07) == (sc->sc_leds & 0x07))
559 		return;
560 
561  	if (sc->sc_have_led_control) {
562 
563 		aleds = (~leds & 0x04) | 3;
564 		if (leds & 1)
565 			aleds &= ~2;
566 		if (leds & 2)
567 			aleds &= ~1;
568 
569 		buffer[0] = 0xff;
570 		buffer[1] = aleds | 0xf8;
571 
572 		cmd = ADBLISTEN(sc->sc_adbdev->current_addr, 2);
573 		sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 2,
574 		    buffer);
575 	}
576 
577 	sc->sc_leds = leds & 7;
578 }
579 
580 static void
581 adbkbd_initleds(struct adbkbd_softc *sc)
582 {
583 	short cmd;
584 
585 	/* talk R2 */
586 	cmd = ADBTALK(sc->sc_adbdev->current_addr, 2);
587 	sc->sc_msg_len = 0;
588 	sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 0, NULL);
589 	if (!adbkbd_wait(sc, 10)) {
590 		aprint_error_dev(sc->sc_dev, "unable to read LED state\n");
591 		return;
592 	}
593 	sc->sc_have_led_control = 1;
594 	DPRINTF("have LED control\n");
595 	return;
596 }
597 
598 static int
599 adbkbd_enable(void *v, int on)
600 {
601 	return 0;
602 }
603 
604 static int
605 adbkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
606 {
607 	struct adbkbd_softc *sc = (struct adbkbd_softc *) v;
608 
609 	switch (cmd) {
610 
611 	case WSKBDIO_GTYPE:
612 		if (sc->sc_emul_usb) {
613 			*(int *)data = WSKBD_TYPE_USB;
614 		} else {
615 			*(int *)data = WSKBD_TYPE_ADB;
616 		}
617 		return 0;
618 	case WSKBDIO_SETLEDS:
619 		adbkbd_set_leds(sc, *(int *)data);
620 		return 0;
621 	case WSKBDIO_GETLEDS:
622 		*(int *)data = sc->sc_leds;
623 		return 0;
624 #ifdef WSDISPLAY_COMPAT_RAWKBD
625 	case WSKBDIO_SETMODE:
626 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
627 		return 0;
628 #endif
629 	}
630 
631 	return EPASSTHROUGH;
632 }
633 
634 int
635 adbkbd_cnattach(void)
636 {
637 
638 	adbkbd_is_console = 1;
639 	return 0;
640 }
641 
642 static void
643 adbkbd_cngetc(void *v, u_int *type, int *data)
644 {
645 	struct adbkbd_softc *sc = v;
646 	int key, press, val;
647 	int s;
648 
649 	s = splhigh();
650 
651 	KASSERT(sc->sc_poll);
652 
653 	DPRINTF("polling...");
654 	while (sc->sc_polled_chars == 0) {
655 		sc->sc_ops->poll(sc->sc_ops->cookie);
656 	}
657 	DPRINTF(" got one\n");
658 	splx(s);
659 
660 	key = sc->sc_pollbuf[0];
661 	sc->sc_polled_chars--;
662 	memmove(sc->sc_pollbuf, sc->sc_pollbuf + 1,
663 		sc->sc_polled_chars);
664 
665 	press = ADBK_PRESS(key);
666 	val = ADBK_KEYVAL(key);
667 
668 	*data = val;
669 	*type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
670 }
671 
672 static void
673 adbkbd_cnpollc(void *v, int on)
674 {
675 	struct adbkbd_softc *sc = v;
676 
677 	sc->sc_poll = on;
678 	if (!on) {
679 		int i;
680 
681 		/* feed the poll buffer's content to wskbd */
682 		for (i = 0; i < sc->sc_polled_chars; i++) {
683 			adbkbd_key(sc, sc->sc_pollbuf[i]);
684 		}
685 		sc->sc_polled_chars = 0;
686 	}
687 }
688 
689 #if NWSMOUSE > 0
690 /* stuff for the pseudo mouse */
691 static int
692 adbkms_enable(void *v)
693 {
694 	return 0;
695 }
696 
697 static int
698 adbkms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
699 {
700 
701 	switch (cmd) {
702 	case WSMOUSEIO_GTYPE:
703 		*(u_int *)data = WSMOUSE_TYPE_PSEUDO;
704 		break;
705 
706 	default:
707 		return (EPASSTHROUGH);
708 	}
709 	return (0);
710 }
711 
712 static void
713 adbkms_disable(void *v)
714 {
715 }
716 
717 static int
718 adbkbd_sysctl_mid(SYSCTLFN_ARGS)
719 {
720 	struct sysctlnode node = *rnode;
721 	struct adbkbd_softc *sc=(struct adbkbd_softc *)node.sysctl_data;
722 	const int *np = newp;
723 	int reg;
724 
725 	DPRINTF("adbkbd_sysctl_mid\n");
726 	reg = sc->sc_trans[1];
727 	if (np) {
728 		/* we're asked to write */
729 		node.sysctl_data = &reg;
730 		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
731 
732 			sc->sc_trans[1] = *(int *)node.sysctl_data;
733 			return 0;
734 		}
735 		return EINVAL;
736 	} else {
737 		node.sysctl_data = &reg;
738 		node.sysctl_size = 4;
739 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
740 	}
741 }
742 
743 static int
744 adbkbd_sysctl_right(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 	int reg;
750 
751 	DPRINTF("adbkbd_sysctl_right\n");
752 	reg = sc->sc_trans[2];
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_trans[2] = *(int *)node.sysctl_data;
759 			return 0;
760 		}
761 		return EINVAL;
762 	} else {
763 		node.sysctl_data = &reg;
764 		node.sysctl_size = 4;
765 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
766 	}
767 }
768 
769 #endif /* NWSMOUSE > 0 */
770 
771 static int
772 adbkbd_sysctl_usb(SYSCTLFN_ARGS)
773 {
774 	struct sysctlnode node = *rnode;
775 	struct adbkbd_softc *sc=(struct adbkbd_softc *)node.sysctl_data;
776 	const int *np = newp;
777 	bool reg;
778 
779 	DPRINTF("%s\n", __func__);
780 	reg = sc->sc_emul_usb;
781 	if (np) {
782 		/* we're asked to write */
783 		node.sysctl_data = &reg;
784 		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
785 
786 			sc->sc_emul_usb = *(bool *)node.sysctl_data;
787 			if (sc->sc_emul_usb) {
788 				wskbd_set_evtrans(sc->sc_wskbddev,
789 				    adb_to_usb, 128);
790 			} else {
791 				wskbd_set_evtrans(sc->sc_wskbddev, NULL, 0);
792 			}
793 			return 0;
794 		}
795 		return EINVAL;
796 	} else {
797 		node.sysctl_data = &reg;
798 		node.sysctl_size = sizeof(reg);
799 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
800 	}
801 }
802 
803 static void
804 adbkbd_setup_sysctl(struct adbkbd_softc *sc)
805 {
806 	const struct sysctlnode *me, *node;
807 	int ret;
808 
809 	DPRINTF("%s: sysctl setup\n", device_xname(sc->sc_dev));
810 	ret = sysctl_createv(NULL, 0, NULL, &me,
811 	       CTLFLAG_READWRITE,
812 	       CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
813 	       NULL, 0, NULL, 0,
814 	       CTL_MACHDEP, CTL_CREATE, CTL_EOL);
815 	ret = sysctl_createv(NULL, 0, NULL,
816 	    (void *)&node,
817 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
818 	    CTLTYPE_BOOL, "emulate_usb", "USB keyboard emulation",
819 	    adbkbd_sysctl_usb, 1, (void *)sc, 0, CTL_MACHDEP,
820 	    me->sysctl_num, CTL_CREATE, CTL_EOL);
821 #if NWSMOUSE > 0
822 	if (sc->sc_wsmousedev != NULL) {
823 		ret = sysctl_createv(NULL, 0, NULL,
824 		    (void *)&node,
825 		    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
826 		    CTLTYPE_INT, "middle", "middle mouse button",
827 		    adbkbd_sysctl_mid, 1, (void *)sc, 0, CTL_MACHDEP,
828 		    me->sysctl_num, CTL_CREATE, CTL_EOL);
829 
830 		ret = sysctl_createv(NULL, 0, NULL,
831 		    (void *)&node,
832 		    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
833 		    CTLTYPE_INT, "right", "right mouse button",
834 		    adbkbd_sysctl_right, 2, (void *)sc, 0, CTL_MACHDEP,
835 		    me->sysctl_num, CTL_CREATE, CTL_EOL);
836 	}
837 #endif /* NWSMOUSE > 0 */
838 
839 	(void)ret;
840 }
841 
842 SYSCTL_SETUP(sysctl_adbkbdtrans_setup, "adbkbd translator setup")
843 {
844 
845 	sysctl_createv(NULL, 0, NULL, NULL,
846 		       CTLFLAG_PERMANENT,
847 		       CTLTYPE_NODE, "machdep", NULL,
848 		       NULL, 0, NULL, 0,
849 		       CTL_MACHDEP, CTL_EOL);
850 }
851