adb_kbd.c revision 1.30
1/*	$NetBSD: adb_kbd.c,v 1.30 2020/08/31 17:54:18 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.30 2020/08/31 17:54:18 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/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
74struct 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 */
108static int	adbkbd_match(device_t, cfdata_t, void *);
109static void	adbkbd_attach(device_t, device_t, void *);
110
111static void	adbkbd_initleds(struct adbkbd_softc *);
112static void	adbkbd_keys(struct adbkbd_softc *, uint8_t, uint8_t);
113static inline void adbkbd_key(struct adbkbd_softc *, uint8_t);
114static int	adbkbd_wait(struct adbkbd_softc *, int);
115
116/* Driver definition. */
117CFATTACH_DECL_NEW(adbkbd, sizeof(struct adbkbd_softc),
118    adbkbd_match, adbkbd_attach, NULL, NULL);
119
120static int adbkbd_enable(void *, int);
121static int adbkbd_ioctl(void *, u_long, void *, int, struct lwp *);
122static void adbkbd_set_leds(void *, int);
123static void adbkbd_handler(void *, int, uint8_t *);
124static void adbkbd_powerbutton(void *);
125
126struct wskbd_accessops adbkbd_accessops = {
127	adbkbd_enable,
128	adbkbd_set_leds,
129	adbkbd_ioctl,
130};
131
132static void adbkbd_cngetc(void *, u_int *, int *);
133static void adbkbd_cnpollc(void *, int);
134
135struct wskbd_consops adbkbd_consops = {
136	adbkbd_cngetc,
137	adbkbd_cnpollc,
138};
139
140struct 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
150static int adbkms_enable(void *);
151static int adbkms_ioctl(void *, u_long, void *, int, struct lwp *);
152static void adbkms_disable(void *);
153
154const struct wsmouse_accessops adbkms_accessops = {
155	adbkms_enable,
156	adbkms_ioctl,
157	adbkms_disable,
158};
159
160static int  adbkbd_sysctl_mid(SYSCTLFN_ARGS);
161static int  adbkbd_sysctl_right(SYSCTLFN_ARGS);
162static int  adbkbd_sysctl_usb(SYSCTLFN_ARGS);
163
164#endif /* NWSMOUSE > 0 */
165
166static 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
174static int adbkbd_is_console = 0;
175static int adbkbd_console_attached = 0;
176
177static int
178adbkbd_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
188static void
189adbkbd_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_ia(self, "wskbddev", &a, wskbddevprint);
387#ifdef ADBKBD_EMUL_USB
388	sc->sc_emul_usb = TRUE;
389	wskbd_set_evtrans(sc->sc_wskbddev, adb_to_usb, 128);
390#endif /* ADBKBD_EMUL_USB */
391
392#if NWSMOUSE > 0
393	/* attach the mouse device */
394	am.accessops = &adbkms_accessops;
395	am.accesscookie = sc;
396	sc->sc_wsmousedev = config_found_ia(self, "wsmousedev", &am,
397	    wsmousedevprint);
398
399#endif
400	adbkbd_setup_sysctl(sc);
401
402	/* finally register the power button */
403	sysmon_task_queue_init();
404	memset(&sc->sc_sm_pbutton, 0, sizeof(struct sysmon_pswitch));
405	sc->sc_sm_pbutton.smpsw_name = device_xname(sc->sc_dev);
406	sc->sc_sm_pbutton.smpsw_type = PSWITCH_TYPE_POWER;
407	if (sysmon_pswitch_register(&sc->sc_sm_pbutton) != 0)
408		aprint_error_dev(sc->sc_dev,
409		    "unable to register power button with sysmon\n");
410}
411
412static void
413adbkbd_handler(void *cookie, int len, uint8_t *data)
414{
415	struct adbkbd_softc *sc = cookie;
416
417#ifdef ADBKBD_DEBUG
418	int i;
419	printf("%s: %02x - ", device_xname(sc->sc_dev), sc->sc_us);
420	for (i = 0; i < len; i++) {
421		printf(" %02x", data[i]);
422	}
423	printf("\n");
424#endif
425	if (len >= 2) {
426		if (data[1] == sc->sc_us) {
427			adbkbd_keys(sc, data[2], data[3]);
428			return;
429		} else {
430			memcpy(sc->sc_buffer, data, len);
431		}
432		sc->sc_msg_len = len;
433		wakeup(&sc->sc_event);
434	} else {
435		DPRINTF("bogus message\n");
436	}
437}
438
439static int
440adbkbd_wait(struct adbkbd_softc *sc, int timeout)
441{
442	int cnt = 0;
443
444	if (sc->sc_poll) {
445		while (sc->sc_msg_len == 0) {
446			sc->sc_ops->poll(sc->sc_ops->cookie);
447		}
448	} else {
449		while ((sc->sc_msg_len == 0) && (cnt < timeout)) {
450			tsleep(&sc->sc_event, 0, "adbkbdio", hz);
451			cnt++;
452		}
453	}
454	return (sc->sc_msg_len > 0);
455}
456
457static void
458adbkbd_keys(struct adbkbd_softc *sc, uint8_t k1, uint8_t k2)
459{
460
461	/* keyboard event processing */
462
463	DPRINTF("[%02x %02x]", k1, k2);
464
465	if (((k1 == k2) && (k1 == 0x7f)) || (k1 == sc->sc_power)) {
466		uint32_t now = time_second;
467		uint32_t diff = now - sc->sc_timestamp;
468
469		sc->sc_timestamp = now;
470		if (((diff > 1) && (diff < 5)) ||
471		     (sc->sc_power_button_delay == 0)) {
472#ifdef DDB
473			if (sc->sc_power_dbg) {
474				Debugger();
475				return;
476			}
477#endif
478			/* power button, report to sysmon */
479			sc->sc_pe = k1;
480			sysmon_task_queue_sched(0, adbkbd_powerbutton, sc);
481		}
482	} else {
483
484		adbkbd_key(sc, k1);
485		if (k2 != 0xff)
486			adbkbd_key(sc, k2);
487	}
488}
489
490static void
491adbkbd_powerbutton(void *cookie)
492{
493	struct adbkbd_softc *sc = cookie;
494
495	sysmon_pswitch_event(&sc->sc_sm_pbutton,
496	    ADBK_PRESS(sc->sc_pe) ? PSWITCH_EVENT_PRESSED :
497	    PSWITCH_EVENT_RELEASED);
498
499}
500
501static inline void
502adbkbd_key(struct adbkbd_softc *sc, uint8_t k)
503{
504
505	if (sc->sc_poll) {
506		if (sc->sc_polled_chars >= 16) {
507			aprint_error_dev(sc->sc_dev,"polling buffer is full\n");
508		}
509		sc->sc_pollbuf[sc->sc_polled_chars] = k;
510		sc->sc_polled_chars++;
511		return;
512	}
513
514#if NWSMOUSE > 0
515	/* translate some keys to mouse events */
516	if (sc->sc_wsmousedev != NULL) {
517		if (ADBK_KEYVAL(k) == sc->sc_trans[1]) {
518			wsmouse_input(sc->sc_wsmousedev, ADBK_PRESS(k) ? 2 : 0,
519			      0, 0, 0, 0,
520			      WSMOUSE_INPUT_DELTA);
521			return;
522		}
523		if (ADBK_KEYVAL(k) == sc->sc_trans[2]) {
524			wsmouse_input(sc->sc_wsmousedev, ADBK_PRESS(k) ? 4 : 0,
525			      0, 0, 0, 0,
526			      WSMOUSE_INPUT_DELTA);
527			return;
528		}
529	}
530#endif
531
532#ifdef WSDISPLAY_COMPAT_RAWKBD
533	if (sc->sc_rawkbd) {
534		char cbuf[2];
535		int s;
536
537		cbuf[0] = k;
538
539		s = spltty();
540		wskbd_rawinput(sc->sc_wskbddev, cbuf, 1);
541		splx(s);
542	} else {
543#endif
544
545	if (ADBK_KEYVAL(k) == 0x39) {
546		/* caps lock - send up and down */
547		if (ADBK_PRESS(k) != sc->sc_capslock) {
548			sc->sc_capslock = ADBK_PRESS(k);
549			wskbd_input(sc->sc_wskbddev,
550			    WSCONS_EVENT_KEY_DOWN, 0x39);
551			wskbd_input(sc->sc_wskbddev,
552			    WSCONS_EVENT_KEY_UP, 0x39);
553		}
554	} else {
555		/* normal event */
556		int type;
557
558		type = ADBK_PRESS(k) ?
559		    WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
560		wskbd_input(sc->sc_wskbddev, type, ADBK_KEYVAL(k));
561	}
562#ifdef WSDISPLAY_COMPAT_RAWKBD
563	}
564#endif
565}
566
567/*
568 * Set the keyboard LED's.
569 *
570 * Automatically translates from ioctl/softc format to the
571 * actual keyboard register format
572 */
573static void
574adbkbd_set_leds(void *cookie, int leds)
575{
576	struct adbkbd_softc *sc = cookie;
577	int aleds;
578	short cmd;
579	uint8_t buffer[2];
580
581	DPRINTF("adbkbd_set_leds: %02x\n", leds);
582	if ((leds & 0x07) == (sc->sc_leds & 0x07))
583		return;
584
585 	if (sc->sc_have_led_control) {
586
587		aleds = (~leds & 0x04) | 3;
588		if (leds & 1)
589			aleds &= ~2;
590		if (leds & 2)
591			aleds &= ~1;
592
593		buffer[0] = 0xff;
594		buffer[1] = aleds | 0xf8;
595
596		cmd = ADBLISTEN(sc->sc_adbdev->current_addr, 2);
597		sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 2,
598		    buffer);
599	}
600
601	sc->sc_leds = leds & 7;
602}
603
604static void
605adbkbd_initleds(struct adbkbd_softc *sc)
606{
607	short cmd;
608
609	/* talk R2 */
610	cmd = ADBTALK(sc->sc_adbdev->current_addr, 2);
611	sc->sc_msg_len = 0;
612	sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 0, NULL);
613	if (!adbkbd_wait(sc, 10)) {
614		aprint_error_dev(sc->sc_dev, "unable to read LED state\n");
615		return;
616	}
617	sc->sc_have_led_control = 1;
618	DPRINTF("have LED control\n");
619	return;
620}
621
622static int
623adbkbd_enable(void *v, int on)
624{
625	return 0;
626}
627
628static int
629adbkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
630{
631	struct adbkbd_softc *sc = (struct adbkbd_softc *) v;
632
633	switch (cmd) {
634
635	case WSKBDIO_GTYPE:
636		if (sc->sc_emul_usb) {
637			*(int *)data = WSKBD_TYPE_USB;
638		} else {
639			*(int *)data = WSKBD_TYPE_ADB;
640		}
641		return 0;
642	case WSKBDIO_SETLEDS:
643		adbkbd_set_leds(sc, *(int *)data);
644		return 0;
645	case WSKBDIO_GETLEDS:
646		*(int *)data = sc->sc_leds;
647		return 0;
648#ifdef WSDISPLAY_COMPAT_RAWKBD
649	case WSKBDIO_SETMODE:
650		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
651		return 0;
652#endif
653	}
654
655	return EPASSTHROUGH;
656}
657
658int
659adbkbd_cnattach(void)
660{
661
662	adbkbd_is_console = 1;
663	return 0;
664}
665
666static void
667adbkbd_cngetc(void *v, u_int *type, int *data)
668{
669	struct adbkbd_softc *sc = v;
670	int key, press, val;
671	int s;
672
673	s = splhigh();
674
675	KASSERT(sc->sc_poll);
676
677	DPRINTF("polling...");
678	while (sc->sc_polled_chars == 0) {
679		sc->sc_ops->poll(sc->sc_ops->cookie);
680	}
681	DPRINTF(" got one\n");
682	splx(s);
683
684	key = sc->sc_pollbuf[0];
685	sc->sc_polled_chars--;
686	memmove(sc->sc_pollbuf, sc->sc_pollbuf + 1,
687		sc->sc_polled_chars);
688
689	press = ADBK_PRESS(key);
690	val = ADBK_KEYVAL(key);
691
692	*data = val;
693	*type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
694}
695
696static void
697adbkbd_cnpollc(void *v, int on)
698{
699	struct adbkbd_softc *sc = v;
700
701	sc->sc_poll = on;
702	if (!on) {
703		int i;
704
705		/* feed the poll buffer's content to wskbd */
706		for (i = 0; i < sc->sc_polled_chars; i++) {
707			adbkbd_key(sc, sc->sc_pollbuf[i]);
708		}
709		sc->sc_polled_chars = 0;
710	}
711}
712
713#if NWSMOUSE > 0
714/* stuff for the pseudo mouse */
715static int
716adbkms_enable(void *v)
717{
718	return 0;
719}
720
721static int
722adbkms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
723{
724
725	switch (cmd) {
726	case WSMOUSEIO_GTYPE:
727		*(u_int *)data = WSMOUSE_TYPE_PSEUDO;
728		break;
729
730	default:
731		return (EPASSTHROUGH);
732	}
733	return (0);
734}
735
736static void
737adbkms_disable(void *v)
738{
739}
740
741static int
742adbkbd_sysctl_mid(SYSCTLFN_ARGS)
743{
744	struct sysctlnode node = *rnode;
745	struct adbkbd_softc *sc=(struct adbkbd_softc *)node.sysctl_data;
746	const int *np = newp;
747	int reg;
748
749	DPRINTF("adbkbd_sysctl_mid\n");
750	reg = sc->sc_trans[1];
751	if (np) {
752		/* we're asked to write */
753		node.sysctl_data = &reg;
754		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
755
756			sc->sc_trans[1] = *(int *)node.sysctl_data;
757			return 0;
758		}
759		return EINVAL;
760	} else {
761		node.sysctl_data = &reg;
762		node.sysctl_size = 4;
763		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
764	}
765}
766
767static int
768adbkbd_sysctl_right(SYSCTLFN_ARGS)
769{
770	struct sysctlnode node = *rnode;
771	struct adbkbd_softc *sc=(struct adbkbd_softc *)node.sysctl_data;
772	const int *np = newp;
773	int reg;
774
775	DPRINTF("adbkbd_sysctl_right\n");
776	reg = sc->sc_trans[2];
777	if (np) {
778		/* we're asked to write */
779		node.sysctl_data = &reg;
780		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
781
782			sc->sc_trans[2] = *(int *)node.sysctl_data;
783			return 0;
784		}
785		return EINVAL;
786	} else {
787		node.sysctl_data = &reg;
788		node.sysctl_size = 4;
789		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
790	}
791}
792
793#endif /* NWSMOUSE > 0 */
794
795static int
796adbkbd_sysctl_usb(SYSCTLFN_ARGS)
797{
798	struct sysctlnode node = *rnode;
799	struct adbkbd_softc *sc=(struct adbkbd_softc *)node.sysctl_data;
800	const int *np = newp;
801	bool reg;
802
803	DPRINTF("%s\n", __func__);
804	reg = sc->sc_emul_usb;
805	if (np) {
806		/* we're asked to write */
807		node.sysctl_data = &reg;
808		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
809
810			sc->sc_emul_usb = *(bool *)node.sysctl_data;
811			if (sc->sc_emul_usb) {
812				wskbd_set_evtrans(sc->sc_wskbddev,
813				    adb_to_usb, 128);
814			} else {
815				wskbd_set_evtrans(sc->sc_wskbddev, NULL, 0);
816			}
817			return 0;
818		}
819		return EINVAL;
820	} else {
821		node.sysctl_data = &reg;
822		node.sysctl_size = sizeof(reg);
823		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
824	}
825}
826
827static int
828adbkbd_sysctl_dbg(SYSCTLFN_ARGS)
829{
830	struct sysctlnode node = *rnode;
831	struct adbkbd_softc *sc=(struct adbkbd_softc *)node.sysctl_data;
832	const int *np = newp;
833	bool reg;
834
835	DPRINTF("%s\n", __func__);
836	reg = sc->sc_power_dbg;
837	if (np) {
838		/* we're asked to write */
839		node.sysctl_data = &reg;
840		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
841
842			sc->sc_power_dbg = *(bool *)node.sysctl_data;
843			return 0;
844		}
845		return EINVAL;
846	} else {
847		node.sysctl_data = &reg;
848		node.sysctl_size = sizeof(reg);
849		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
850	}
851}
852
853static void
854adbkbd_setup_sysctl(struct adbkbd_softc *sc)
855{
856	const struct sysctlnode *me, *node;
857	int ret;
858
859	DPRINTF("%s: sysctl setup\n", device_xname(sc->sc_dev));
860	ret = sysctl_createv(NULL, 0, NULL, &me,
861	       CTLFLAG_READWRITE,
862	       CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
863	       NULL, 0, NULL, 0,
864	       CTL_MACHDEP, CTL_CREATE, CTL_EOL);
865	ret = sysctl_createv(NULL, 0, NULL,
866	    (void *)&node,
867	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
868	    CTLTYPE_BOOL, "emulate_usb", "USB keyboard emulation",
869	    adbkbd_sysctl_usb, 1, (void *)sc, 0, CTL_MACHDEP,
870	    me->sysctl_num, CTL_CREATE, CTL_EOL);
871	ret = sysctl_createv(NULL, 0, NULL,
872	    (void *)&node,
873	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
874	    CTLTYPE_BOOL, "power_ddb", "power button triggers ddb",
875	    adbkbd_sysctl_dbg, 1, (void *)sc, 0, CTL_MACHDEP,
876	    me->sysctl_num, CTL_CREATE, CTL_EOL);
877#if NWSMOUSE > 0
878	if (sc->sc_wsmousedev != NULL) {
879		ret = sysctl_createv(NULL, 0, NULL,
880		    (void *)&node,
881		    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
882		    CTLTYPE_INT, "middle", "middle mouse button",
883		    adbkbd_sysctl_mid, 1, (void *)sc, 0, CTL_MACHDEP,
884		    me->sysctl_num, CTL_CREATE, CTL_EOL);
885
886		ret = sysctl_createv(NULL, 0, NULL,
887		    (void *)&node,
888		    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
889		    CTLTYPE_INT, "right", "right mouse button",
890		    adbkbd_sysctl_right, 2, (void *)sc, 0, CTL_MACHDEP,
891		    me->sysctl_num, CTL_CREATE, CTL_EOL);
892	}
893#endif /* NWSMOUSE > 0 */
894
895	(void)ret;
896}
897
898SYSCTL_SETUP(sysctl_adbkbdtrans_setup, "adbkbd translator setup")
899{
900
901	sysctl_createv(NULL, 0, NULL, NULL,
902		       CTLFLAG_PERMANENT,
903		       CTLTYPE_NODE, "machdep", NULL,
904		       NULL, 0, NULL, 0,
905		       CTL_MACHDEP, CTL_EOL);
906}
907