adb_kbd.c revision 1.23
1/*	$NetBSD: adb_kbd.c,v 1.23 2014/11/08 16:52:35 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.23 2014/11/08 16:52:35 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
68struct 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 */
101static int	adbkbd_match(device_t, cfdata_t, void *);
102static void	adbkbd_attach(device_t, device_t, void *);
103
104static void	adbkbd_initleds(struct adbkbd_softc *);
105static void	adbkbd_keys(struct adbkbd_softc *, uint8_t, uint8_t);
106static inline void adbkbd_key(struct adbkbd_softc *, uint8_t);
107static int	adbkbd_wait(struct adbkbd_softc *, int);
108
109/* Driver definition. */
110CFATTACH_DECL_NEW(adbkbd, sizeof(struct adbkbd_softc),
111    adbkbd_match, adbkbd_attach, NULL, NULL);
112
113extern struct cfdriver adbkbd_cd;
114
115static int adbkbd_enable(void *, int);
116static int adbkbd_ioctl(void *, u_long, void *, int, struct lwp *);
117static void adbkbd_set_leds(void *, int);
118static void adbkbd_handler(void *, int, uint8_t *);
119static void adbkbd_powerbutton(void *);
120
121struct wskbd_accessops adbkbd_accessops = {
122	adbkbd_enable,
123	adbkbd_set_leds,
124	adbkbd_ioctl,
125};
126
127static void adbkbd_cngetc(void *, u_int *, int *);
128static void adbkbd_cnpollc(void *, int);
129
130struct wskbd_consops adbkbd_consops = {
131	adbkbd_cngetc,
132	adbkbd_cnpollc,
133};
134
135struct 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
145static int adbkms_enable(void *);
146static int adbkms_ioctl(void *, u_long, void *, int, struct lwp *);
147static void adbkms_disable(void *);
148
149const struct wsmouse_accessops adbkms_accessops = {
150	adbkms_enable,
151	adbkms_ioctl,
152	adbkms_disable,
153};
154
155static int  adbkbd_sysctl_mid(SYSCTLFN_ARGS);
156static int  adbkbd_sysctl_right(SYSCTLFN_ARGS);
157static int  adbkbd_sysctl_usb(SYSCTLFN_ARGS);
158
159#endif /* NWSMOUSE > 0 */
160
161static 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
169static int adbkbd_is_console = 0;
170static int adbkbd_console_attached = 0;
171
172static int
173adbkbd_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
183static void
184adbkbd_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	printf(" addr %d: ", sc->sc_adbdev->current_addr);
236
237	switch (sc->sc_adbdev->handler_id) {
238	case ADB_STDKBD:
239		printf("standard keyboard\n");
240		break;
241	case ADB_ISOKBD:
242		printf("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			printf("Mouseman (non-EMP) pseudo keyboard\n");
254			return;
255		} else if (sc->sc_buffer[2] == 0x9a &&
256		    sc->sc_buffer[3] == 0x21) {
257			printf("Trackman (non-EMP) pseudo keyboard\n");
258			return;
259		} else {
260			printf("extended keyboard\n");
261			adbkbd_initleds(sc);
262		}
263		break;
264	case ADB_EXTISOKBD:
265		printf("extended keyboard (ISO layout)\n");
266		adbkbd_initleds(sc);
267		break;
268	case ADB_KBDII:
269		printf("keyboard II\n");
270		break;
271	case ADB_ISOKBDII:
272		printf("keyboard II (ISO layout)\n");
273		break;
274	case ADB_PBKBD:
275		printf("PowerBook keyboard\n");
276		sc->sc_power = 0x7e;
277		sc->sc_power_button_delay = 1;
278		break;
279	case ADB_PBISOKBD:
280		printf("PowerBook keyboard (ISO layout)\n");
281		sc->sc_power = 0x7e;
282		sc->sc_power_button_delay = 1;
283		break;
284	case ADB_ADJKPD:
285		printf("adjustable keypad\n");
286		break;
287	case ADB_ADJKBD:
288		printf("adjustable keyboard\n");
289		break;
290	case ADB_ADJISOKBD:
291		printf("adjustable keyboard (ISO layout)\n");
292		break;
293	case ADB_ADJJAPKBD:
294		printf("adjustable keyboard (Japanese layout)\n");
295		break;
296	case ADB_PBEXTISOKBD:
297		printf("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		printf("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		printf("keyboard II (Japanese layout)\n");
308		break;
309	case ADB_PBEXTKBD:
310		printf("PowerBook extended keyboard\n");
311		sc->sc_power_button_delay = 1;
312		sc->sc_power = 0x7e;
313		break;
314	case ADB_DESIGNKBD:
315		printf("extended keyboard\n");
316		adbkbd_initleds(sc);
317		break;
318	case ADB_PBJPKBD:
319		printf("PowerBook keyboard (Japanese layout)\n");
320		sc->sc_power_button_delay = 1;
321		sc->sc_power = 0x7e;
322		break;
323	case ADB_PBG3KBD:
324		printf("PowerBook G3 keyboard\n");
325		break;
326	case ADB_PBG3JPKBD:
327		printf("PowerBook G3 keyboard (Japanese layout)\n");
328		break;
329	case ADB_IBOOKKBD:
330		printf("iBook keyboard\n");
331		break;
332	default:
333		printf("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		printf("%s: extended protocol enabled\n",
355		    device_xname(sc->sc_dev));
356	}
357
358	if (adbkbd_is_console && (adbkbd_console_attached == 0)) {
359		wskbd_cnattach(&adbkbd_consops, sc, &adbkbd_keymapdata);
360		adbkbd_console_attached = 1;
361		a.console = 1;
362	} else {
363		a.console = 0;
364	}
365	a.keymap = &adbkbd_keymapdata;
366	a.accessops = &adbkbd_accessops;
367	a.accesscookie = sc;
368
369	sc->sc_wskbddev = config_found_ia(self, "wskbddev", &a, wskbddevprint);
370#ifdef ADBKBD_EMUL_USB
371	sc->sc_emul_usb = TRUE;
372	wskbd_set_evtrans(sc->sc_wskbddev, adb_to_usb, 128);
373#endif /* ADBKBD_EMUL_USB */
374
375#if NWSMOUSE > 0
376	/* attach the mouse device */
377	am.accessops = &adbkms_accessops;
378	am.accesscookie = sc;
379	sc->sc_wsmousedev = config_found_ia(self, "wsmousedev", &am,
380	    wsmousedevprint);
381
382#endif
383	adbkbd_setup_sysctl(sc);
384
385	/* finally register the power button */
386	sysmon_task_queue_init();
387	memset(&sc->sc_sm_pbutton, 0, sizeof(struct sysmon_pswitch));
388	sc->sc_sm_pbutton.smpsw_name = device_xname(sc->sc_dev);
389	sc->sc_sm_pbutton.smpsw_type = PSWITCH_TYPE_POWER;
390	if (sysmon_pswitch_register(&sc->sc_sm_pbutton) != 0)
391		aprint_error_dev(sc->sc_dev,
392		    "unable to register power button with sysmon\n");
393}
394
395static void
396adbkbd_handler(void *cookie, int len, uint8_t *data)
397{
398	struct adbkbd_softc *sc = cookie;
399
400#ifdef ADBKBD_DEBUG
401	int i;
402	printf("%s: %02x - ", device_xname(sc->sc_dev), sc->sc_us);
403	for (i = 0; i < len; i++) {
404		printf(" %02x", data[i]);
405	}
406	printf("\n");
407#endif
408	if (len >= 2) {
409		if (data[1] == sc->sc_us) {
410			adbkbd_keys(sc, data[2], data[3]);
411			return;
412		} else {
413			memcpy(sc->sc_buffer, data, len);
414		}
415		sc->sc_msg_len = len;
416		wakeup(&sc->sc_event);
417	} else {
418		DPRINTF("bogus message\n");
419	}
420}
421
422static int
423adbkbd_wait(struct adbkbd_softc *sc, int timeout)
424{
425	int cnt = 0;
426
427	if (sc->sc_poll) {
428		while (sc->sc_msg_len == 0) {
429			sc->sc_ops->poll(sc->sc_ops->cookie);
430		}
431	} else {
432		while ((sc->sc_msg_len == 0) && (cnt < timeout)) {
433			tsleep(&sc->sc_event, 0, "adbkbdio", hz);
434			cnt++;
435		}
436	}
437	return (sc->sc_msg_len > 0);
438}
439
440static void
441adbkbd_keys(struct adbkbd_softc *sc, uint8_t k1, uint8_t k2)
442{
443
444	/* keyboard event processing */
445
446	DPRINTF("[%02x %02x]", k1, k2);
447
448	if (((k1 == k2) && (k1 == 0x7f)) || (k1 == sc->sc_power)) {
449		uint32_t now = time_second;
450		uint32_t diff = now - sc->sc_timestamp;
451
452		sc->sc_timestamp = now;
453		if (((diff > 1) && (diff < 5)) ||
454		     (sc->sc_power_button_delay == 0)) {
455
456			/* power button, report to sysmon */
457			sc->sc_pe = k1;
458			sysmon_task_queue_sched(0, adbkbd_powerbutton, sc);
459		}
460	} else {
461
462		adbkbd_key(sc, k1);
463		if (k2 != 0xff)
464			adbkbd_key(sc, k2);
465	}
466}
467
468static void
469adbkbd_powerbutton(void *cookie)
470{
471	struct adbkbd_softc *sc = cookie;
472
473	sysmon_pswitch_event(&sc->sc_sm_pbutton,
474	    ADBK_PRESS(sc->sc_pe) ? PSWITCH_EVENT_PRESSED :
475	    PSWITCH_EVENT_RELEASED);
476}
477
478static inline void
479adbkbd_key(struct adbkbd_softc *sc, uint8_t k)
480{
481
482	if (sc->sc_poll) {
483		if (sc->sc_polled_chars >= 16) {
484			aprint_error_dev(sc->sc_dev,"polling buffer is full\n");
485		}
486		sc->sc_pollbuf[sc->sc_polled_chars] = k;
487		sc->sc_polled_chars++;
488		return;
489	}
490
491#if NWSMOUSE > 0
492	/* translate some keys to mouse events */
493	if (sc->sc_wsmousedev != NULL) {
494		if (ADBK_KEYVAL(k) == sc->sc_trans[1]) {
495			wsmouse_input(sc->sc_wsmousedev, ADBK_PRESS(k) ? 2 : 0,
496			      0, 0, 0, 0,
497			      WSMOUSE_INPUT_DELTA);
498			return;
499		}
500		if (ADBK_KEYVAL(k) == sc->sc_trans[2]) {
501			wsmouse_input(sc->sc_wsmousedev, ADBK_PRESS(k) ? 4 : 0,
502			      0, 0, 0, 0,
503			      WSMOUSE_INPUT_DELTA);
504			return;
505		}
506	}
507#endif
508
509#ifdef WSDISPLAY_COMPAT_RAWKBD
510	if (sc->sc_rawkbd) {
511		char cbuf[2];
512		int s;
513
514		cbuf[0] = k;
515
516		s = spltty();
517		wskbd_rawinput(sc->sc_wskbddev, cbuf, 1);
518		splx(s);
519	} else {
520#endif
521
522	if (ADBK_KEYVAL(k) == 0x39) {
523		/* caps lock - send up and down */
524		if (ADBK_PRESS(k) != sc->sc_capslock) {
525			sc->sc_capslock = ADBK_PRESS(k);
526			wskbd_input(sc->sc_wskbddev,
527			    WSCONS_EVENT_KEY_DOWN, 0x39);
528			wskbd_input(sc->sc_wskbddev,
529			    WSCONS_EVENT_KEY_UP, 0x39);
530		}
531	} else {
532		/* normal event */
533		int type;
534
535		type = ADBK_PRESS(k) ?
536		    WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
537		wskbd_input(sc->sc_wskbddev, type, ADBK_KEYVAL(k));
538	}
539#ifdef WSDISPLAY_COMPAT_RAWKBD
540	}
541#endif
542}
543
544/*
545 * Set the keyboard LED's.
546 *
547 * Automatically translates from ioctl/softc format to the
548 * actual keyboard register format
549 */
550static void
551adbkbd_set_leds(void *cookie, int leds)
552{
553	struct adbkbd_softc *sc = cookie;
554	int aleds;
555	short cmd;
556	uint8_t buffer[2];
557
558	DPRINTF("adbkbd_set_leds: %02x\n", leds);
559	if ((leds & 0x07) == (sc->sc_leds & 0x07))
560		return;
561
562 	if (sc->sc_have_led_control) {
563
564		aleds = (~leds & 0x04) | 3;
565		if (leds & 1)
566			aleds &= ~2;
567		if (leds & 2)
568			aleds &= ~1;
569
570		buffer[0] = 0xff;
571		buffer[1] = aleds | 0xf8;
572
573		cmd = ADBLISTEN(sc->sc_adbdev->current_addr, 2);
574		sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 2,
575		    buffer);
576	}
577
578	sc->sc_leds = leds & 7;
579}
580
581static void
582adbkbd_initleds(struct adbkbd_softc *sc)
583{
584	short cmd;
585
586	/* talk R2 */
587	cmd = ADBTALK(sc->sc_adbdev->current_addr, 2);
588	sc->sc_msg_len = 0;
589	sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 0, NULL);
590	if (!adbkbd_wait(sc, 10)) {
591		printf("unable to read LED state\n");
592		return;
593	}
594	sc->sc_have_led_control = 1;
595	DPRINTF("have LED control\n");
596	return;
597}
598
599static int
600adbkbd_enable(void *v, int on)
601{
602	return 0;
603}
604
605static int
606adbkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
607{
608	struct adbkbd_softc *sc = (struct adbkbd_softc *) v;
609
610	switch (cmd) {
611
612	case WSKBDIO_GTYPE:
613		if (sc->sc_emul_usb) {
614			*(int *)data = WSKBD_TYPE_USB;
615		} else {
616			*(int *)data = WSKBD_TYPE_ADB;
617		}
618		return 0;
619	case WSKBDIO_SETLEDS:
620		adbkbd_set_leds(sc, *(int *)data);
621		return 0;
622	case WSKBDIO_GETLEDS:
623		*(int *)data = sc->sc_leds;
624		return 0;
625#ifdef WSDISPLAY_COMPAT_RAWKBD
626	case WSKBDIO_SETMODE:
627		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
628		return 0;
629#endif
630	}
631
632	return EPASSTHROUGH;
633}
634
635int
636adbkbd_cnattach(void)
637{
638
639	adbkbd_is_console = 1;
640	return 0;
641}
642
643static void
644adbkbd_cngetc(void *v, u_int *type, int *data)
645{
646	struct adbkbd_softc *sc = v;
647	int key, press, val;
648	int s;
649
650	s = splhigh();
651
652	KASSERT(sc->sc_poll);
653
654	DPRINTF("polling...");
655	while (sc->sc_polled_chars == 0) {
656		sc->sc_ops->poll(sc->sc_ops->cookie);
657	}
658	DPRINTF(" got one\n");
659	splx(s);
660
661	key = sc->sc_pollbuf[0];
662	sc->sc_polled_chars--;
663	memmove(sc->sc_pollbuf, sc->sc_pollbuf + 1,
664		sc->sc_polled_chars);
665
666	press = ADBK_PRESS(key);
667	val = ADBK_KEYVAL(key);
668
669	*data = val;
670	*type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
671}
672
673static void
674adbkbd_cnpollc(void *v, int on)
675{
676	struct adbkbd_softc *sc = v;
677
678	sc->sc_poll = on;
679	if (!on) {
680		int i;
681
682		/* feed the poll buffer's content to wskbd */
683		for (i = 0; i < sc->sc_polled_chars; i++) {
684			adbkbd_key(sc, sc->sc_pollbuf[i]);
685		}
686		sc->sc_polled_chars = 0;
687	}
688}
689
690#if NWSMOUSE > 0
691/* stuff for the pseudo mouse */
692static int
693adbkms_enable(void *v)
694{
695	return 0;
696}
697
698static int
699adbkms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
700{
701
702	switch (cmd) {
703	case WSMOUSEIO_GTYPE:
704		*(u_int *)data = WSMOUSE_TYPE_PSEUDO;
705		break;
706
707	default:
708		return (EPASSTHROUGH);
709	}
710	return (0);
711}
712
713static void
714adbkms_disable(void *v)
715{
716}
717
718static int
719adbkbd_sysctl_mid(SYSCTLFN_ARGS)
720{
721	struct sysctlnode node = *rnode;
722	struct adbkbd_softc *sc=(struct adbkbd_softc *)node.sysctl_data;
723	const int *np = newp;
724	int reg;
725
726	DPRINTF("adbkbd_sysctl_mid\n");
727	reg = sc->sc_trans[1];
728	if (np) {
729		/* we're asked to write */
730		node.sysctl_data = &reg;
731		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
732
733			sc->sc_trans[1] = *(int *)node.sysctl_data;
734			return 0;
735		}
736		return EINVAL;
737	} else {
738		node.sysctl_data = &reg;
739		node.sysctl_size = 4;
740		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
741	}
742}
743
744static int
745adbkbd_sysctl_right(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_right\n");
753	reg = sc->sc_trans[2];
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[2] = *(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#endif /* NWSMOUSE > 0 */
771
772static int
773adbkbd_sysctl_usb(SYSCTLFN_ARGS)
774{
775	struct sysctlnode node = *rnode;
776	struct adbkbd_softc *sc=(struct adbkbd_softc *)node.sysctl_data;
777	const int *np = newp;
778	bool reg;
779
780	DPRINTF("%s\n", __func__);
781	reg = sc->sc_emul_usb;
782	if (np) {
783		/* we're asked to write */
784		node.sysctl_data = &reg;
785		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
786
787			sc->sc_emul_usb = *(bool *)node.sysctl_data;
788			if (sc->sc_emul_usb) {
789				wskbd_set_evtrans(sc->sc_wskbddev,
790				    adb_to_usb, 128);
791			} else {
792				wskbd_set_evtrans(sc->sc_wskbddev, NULL, 0);
793			}
794			return 0;
795		}
796		return EINVAL;
797	} else {
798		node.sysctl_data = &reg;
799		node.sysctl_size = sizeof(reg);
800		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
801	}
802}
803
804static void
805adbkbd_setup_sysctl(struct adbkbd_softc *sc)
806{
807	const struct sysctlnode *me, *node;
808	int ret;
809
810	DPRINTF("%s: sysctl setup\n", device_xname(sc->sc_dev));
811	ret = sysctl_createv(NULL, 0, NULL, &me,
812	       CTLFLAG_READWRITE,
813	       CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
814	       NULL, 0, NULL, 0,
815	       CTL_MACHDEP, CTL_CREATE, CTL_EOL);
816	ret = sysctl_createv(NULL, 0, NULL,
817	    (void *)&node,
818	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
819	    CTLTYPE_BOOL, "emulate_usb", "USB keyboard emulation",
820	    adbkbd_sysctl_usb, 1, (void *)sc, 0, CTL_MACHDEP,
821	    me->sysctl_num, CTL_CREATE, CTL_EOL);
822#if NWSMOUSE > 0
823	if (sc->sc_wsmousedev != NULL) {
824		ret = sysctl_createv(NULL, 0, NULL,
825		    (void *)&node,
826		    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
827		    CTLTYPE_INT, "middle", "middle mouse button",
828		    adbkbd_sysctl_mid, 1, (void *)sc, 0, CTL_MACHDEP,
829		    me->sysctl_num, CTL_CREATE, CTL_EOL);
830
831		ret = sysctl_createv(NULL, 0, NULL,
832		    (void *)&node,
833		    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
834		    CTLTYPE_INT, "right", "right mouse button",
835		    adbkbd_sysctl_right, 2, (void *)sc, 0, CTL_MACHDEP,
836		    me->sysctl_num, CTL_CREATE, CTL_EOL);
837	}
838#endif /* NWSMOUSE > 0 */
839
840	(void)ret;
841}
842
843SYSCTL_SETUP(sysctl_adbkbdtrans_setup, "adbkbd translator setup")
844{
845
846	sysctl_createv(NULL, 0, NULL, NULL,
847		       CTLFLAG_PERMANENT,
848		       CTLTYPE_NODE, "machdep", NULL,
849		       NULL, 0, NULL, 0,
850		       CTL_MACHDEP, CTL_EOL);
851}
852