xref: /netbsd-src/sys/dev/adb/adb_kbd.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*	$NetBSD: adb_kbd.c,v 1.15 2011/08/18 02:18:40 christos 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.15 2011/08/18 02:18:40 christos 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 "adbdebug.h"
65 #include "wsmouse.h"
66 
67 struct adbkbd_softc {
68 	device_t sc_dev;
69 	struct adb_device *sc_adbdev;
70 	struct adb_bus_accessops *sc_ops;
71 	device_t sc_wskbddev;
72 #if NWSMOUSE > 0
73 	device_t sc_wsmousedev;
74 #endif
75 	struct sysmon_pswitch sc_sm_pbutton;
76 	int sc_leds;
77 	int sc_have_led_control;
78 	int sc_msg_len;
79 	int sc_event;
80 	int sc_poll;
81 	int sc_polled_chars;
82 	int sc_trans[3];
83 	int sc_capslock;
84 	uint32_t sc_timestamp;
85 #ifdef WSDISPLAY_COMPAT_RAWKBD
86 	int sc_rawkbd;
87 #endif
88 	uint8_t sc_buffer[16];
89 	uint8_t sc_pollbuf[16];
90 	uint8_t sc_us;
91 	uint8_t sc_power, sc_pe;
92 };
93 
94 /*
95  * Function declarations.
96  */
97 static int	adbkbd_match(device_t, cfdata_t, void *);
98 static void	adbkbd_attach(device_t, device_t, void *);
99 
100 static void	adbkbd_initleds(struct adbkbd_softc *);
101 static void	adbkbd_keys(struct adbkbd_softc *, uint8_t, uint8_t);
102 static inline void adbkbd_key(struct adbkbd_softc *, uint8_t);
103 static int	adbkbd_wait(struct adbkbd_softc *, int);
104 
105 /* Driver definition. */
106 CFATTACH_DECL_NEW(adbkbd, sizeof(struct adbkbd_softc),
107     adbkbd_match, adbkbd_attach, NULL, NULL);
108 
109 extern struct cfdriver adbkbd_cd;
110 
111 static int adbkbd_enable(void *, int);
112 static int adbkbd_ioctl(void *, u_long, void *, int, struct lwp *);
113 static void adbkbd_set_leds(void *, int);
114 static void adbkbd_handler(void *, int, uint8_t *);
115 static void adbkbd_powerbutton(void *);
116 
117 struct wskbd_accessops adbkbd_accessops = {
118 	adbkbd_enable,
119 	adbkbd_set_leds,
120 	adbkbd_ioctl,
121 };
122 
123 static void adbkbd_cngetc(void *, u_int *, int *);
124 static void adbkbd_cnpollc(void *, int);
125 
126 struct wskbd_consops adbkbd_consops = {
127 	adbkbd_cngetc,
128 	adbkbd_cnpollc,
129 };
130 
131 struct wskbd_mapdata adbkbd_keymapdata = {
132 	akbd_keydesctab,
133 #ifdef AKBD_LAYOUT
134 	AKBD_LAYOUT,
135 #else
136 	KB_US,
137 #endif
138 };
139 
140 #if NWSMOUSE > 0
141 static int adbkms_enable(void *);
142 static int adbkms_ioctl(void *, u_long, void *, int, struct lwp *);
143 static void adbkms_disable(void *);
144 
145 const struct wsmouse_accessops adbkms_accessops = {
146 	adbkms_enable,
147 	adbkms_ioctl,
148 	adbkms_disable,
149 };
150 
151 static int  adbkbd_sysctl_mid(SYSCTLFN_ARGS);
152 static int  adbkbd_sysctl_right(SYSCTLFN_ARGS);
153 static void adbkbd_setup_sysctl(struct adbkbd_softc *);
154 
155 #endif /* NWSMOUSE > 0 */
156 
157 #ifdef ADBKBD_DEBUG
158 #define DPRINTF printf
159 #else
160 #define DPRINTF while (0) printf
161 #endif
162 
163 static int adbkbd_is_console = 0;
164 static int adbkbd_console_attached = 0;
165 
166 static int
167 adbkbd_match(device_t parent, cfdata_t cf, void *aux)
168 {
169 	struct adb_attach_args *aaa = aux;
170 
171 	if (aaa->dev->original_addr == ADBADDR_KBD)
172 		return 1;
173 	else
174 		return 0;
175 }
176 
177 static void
178 adbkbd_attach(device_t parent, device_t self, void *aux)
179 {
180 	struct adbkbd_softc *sc = device_private(self);
181 	struct adb_attach_args *aaa = aux;
182 	short cmd;
183 	struct wskbddev_attach_args a;
184 #if NWSMOUSE > 0
185 	struct wsmousedev_attach_args am;
186 #endif
187 
188 	sc->sc_dev = self;
189 	sc->sc_ops = aaa->ops;
190 	sc->sc_adbdev = aaa->dev;
191 	sc->sc_adbdev->cookie = sc;
192 	sc->sc_adbdev->handler = adbkbd_handler;
193 	sc->sc_us = ADBTALK(sc->sc_adbdev->current_addr, 0);
194 
195 	sc->sc_leds = 0;	/* initially off */
196 	sc->sc_have_led_control = 0;
197 	sc->sc_msg_len = 0;
198 	sc->sc_poll = 0;
199 	sc->sc_capslock = 0;
200 	sc->sc_trans[1] = 103;	/* F11 */
201 	sc->sc_trans[2] = 111;	/* F12 */
202 	sc->sc_power = 0x7f;
203 	sc->sc_timestamp = 0;
204 
205 	printf(" addr %d: ", sc->sc_adbdev->current_addr);
206 
207 	switch (sc->sc_adbdev->handler_id) {
208 	case ADB_STDKBD:
209 		printf("standard keyboard\n");
210 		break;
211 	case ADB_ISOKBD:
212 		printf("standard keyboard (ISO layout)\n");
213 		break;
214 	case ADB_EXTKBD:
215 		cmd = ADBTALK(sc->sc_adbdev->current_addr, 1);
216 		sc->sc_msg_len = 0;
217 		sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 0, NULL);
218 		adbkbd_wait(sc, 10);
219 
220 		/* Ignore Logitech MouseMan/Trackman pseudo keyboard */
221 		/* XXX needs testing */
222 		if (sc->sc_buffer[2] == 0x9a && sc->sc_buffer[3] == 0x20) {
223 			printf("Mouseman (non-EMP) pseudo keyboard\n");
224 			return;
225 		} else if (sc->sc_buffer[2] == 0x9a &&
226 		    sc->sc_buffer[3] == 0x21) {
227 			printf("Trackman (non-EMP) pseudo keyboard\n");
228 			return;
229 		} else {
230 			printf("extended keyboard\n");
231 			adbkbd_initleds(sc);
232 		}
233 		break;
234 	case ADB_EXTISOKBD:
235 		printf("extended keyboard (ISO layout)\n");
236 		adbkbd_initleds(sc);
237 		break;
238 	case ADB_KBDII:
239 		printf("keyboard II\n");
240 		break;
241 	case ADB_ISOKBDII:
242 		printf("keyboard II (ISO layout)\n");
243 		break;
244 	case ADB_PBKBD:
245 		printf("PowerBook keyboard\n");
246 		sc->sc_power = 0x7e;
247 		break;
248 	case ADB_PBISOKBD:
249 		printf("PowerBook keyboard (ISO layout)\n");
250 		sc->sc_power = 0x7e;
251 		break;
252 	case ADB_ADJKPD:
253 		printf("adjustable keypad\n");
254 		break;
255 	case ADB_ADJKBD:
256 		printf("adjustable keyboard\n");
257 		break;
258 	case ADB_ADJISOKBD:
259 		printf("adjustable keyboard (ISO layout)\n");
260 		break;
261 	case ADB_ADJJAPKBD:
262 		printf("adjustable keyboard (Japanese layout)\n");
263 		break;
264 	case ADB_PBEXTISOKBD:
265 		printf("PowerBook extended keyboard (ISO layout)\n");
266 		sc->sc_power = 0x7e;
267 		break;
268 	case ADB_PBEXTJAPKBD:
269 		printf("PowerBook extended keyboard (Japanese layout)\n");
270 		sc->sc_power = 0x7e;
271 		break;
272 	case ADB_JPKBDII:
273 		printf("keyboard II (Japanese layout)\n");
274 		break;
275 	case ADB_PBEXTKBD:
276 		printf("PowerBook extended keyboard\n");
277 		sc->sc_power = 0x7e;
278 		break;
279 	case ADB_DESIGNKBD:
280 		printf("extended keyboard\n");
281 		adbkbd_initleds(sc);
282 		break;
283 	case ADB_PBJPKBD:
284 		printf("PowerBook keyboard (Japanese layout)\n");
285 		sc->sc_power = 0x7e;
286 		break;
287 	case ADB_PBG3KBD:
288 		printf("PowerBook G3 keyboard\n");
289 		sc->sc_power = 0x7e;
290 		break;
291 	case ADB_PBG3JPKBD:
292 		printf("PowerBook G3 keyboard (Japanese layout)\n");
293 		sc->sc_power = 0x7e;
294 		break;
295 	case ADB_IBOOKKBD:
296 		printf("iBook keyboard\n");
297 		break;
298 	default:
299 		printf("mapped device (%d)\n", sc->sc_adbdev->handler_id);
300 		break;
301 	}
302 
303 	if (adbkbd_is_console && (adbkbd_console_attached == 0)) {
304 		wskbd_cnattach(&adbkbd_consops, sc, &adbkbd_keymapdata);
305 		adbkbd_console_attached = 1;
306 		a.console = 1;
307 	} else {
308 		a.console = 0;
309 	}
310 	a.keymap = &adbkbd_keymapdata;
311 	a.accessops = &adbkbd_accessops;
312 	a.accesscookie = sc;
313 
314 	sc->sc_wskbddev = config_found_ia(self, "wskbddev", &a, wskbddevprint);
315 
316 #if NWSMOUSE > 0
317 	/* attach the mouse device */
318 	am.accessops = &adbkms_accessops;
319 	am.accesscookie = sc;
320 	sc->sc_wsmousedev = config_found_ia(self, "wsmousedev", &am,
321 	    wsmousedevprint);
322 
323 	if (sc->sc_wsmousedev != NULL)
324 		adbkbd_setup_sysctl(sc);
325 #endif
326 
327 	/* finally register the power button */
328 	sysmon_task_queue_init();
329 	memset(&sc->sc_sm_pbutton, 0, sizeof(struct sysmon_pswitch));
330 	sc->sc_sm_pbutton.smpsw_name = device_xname(sc->sc_dev);
331 	sc->sc_sm_pbutton.smpsw_type = PSWITCH_TYPE_POWER;
332 	if (sysmon_pswitch_register(&sc->sc_sm_pbutton) != 0)
333 		aprint_error_dev(sc->sc_dev,
334 		    "unable to register power button with sysmon\n");
335 }
336 
337 static void
338 adbkbd_handler(void *cookie, int len, uint8_t *data)
339 {
340 	struct adbkbd_softc *sc = cookie;
341 
342 #ifdef ADBKBD_DEBUG
343 	int i;
344 	printf("%s: %02x - ", device_xname(sc->sc_dev), sc->sc_us);
345 	for (i = 0; i < len; i++) {
346 		printf(" %02x", data[i]);
347 	}
348 	printf("\n");
349 #endif
350 	if (len >= 2) {
351 		if (data[1] == sc->sc_us) {
352 			adbkbd_keys(sc, data[2], data[3]);
353 			return;
354 		} else {
355 			memcpy(sc->sc_buffer, data, len);
356 		}
357 		sc->sc_msg_len = len;
358 		wakeup(&sc->sc_event);
359 	} else {
360 		DPRINTF("bogus message\n");
361 	}
362 }
363 
364 static int
365 adbkbd_wait(struct adbkbd_softc *sc, int timeout)
366 {
367 	int cnt = 0;
368 
369 	if (sc->sc_poll) {
370 		while (sc->sc_msg_len == 0) {
371 			sc->sc_ops->poll(sc->sc_ops->cookie);
372 		}
373 	} else {
374 		while ((sc->sc_msg_len == 0) && (cnt < timeout)) {
375 			tsleep(&sc->sc_event, 0, "adbkbdio", hz);
376 			cnt++;
377 		}
378 	}
379 	return (sc->sc_msg_len > 0);
380 }
381 
382 static void
383 adbkbd_keys(struct adbkbd_softc *sc, uint8_t k1, uint8_t k2)
384 {
385 
386 	/* keyboard event processing */
387 
388 	DPRINTF("[%02x %02x]", k1, k2);
389 
390 	if (((k1 == k2) && (k1 == 0x7f)) || (k1 == sc->sc_power)) {
391 		uint32_t now = time_second;
392 		uint32_t diff = now - sc->sc_timestamp;
393 
394 		sc->sc_timestamp = now;
395 		if ((diff > 1) && (diff < 5)) {
396 
397 			/* power button, report to sysmon */
398 			sc->sc_pe = k1;
399 
400 			sysmon_task_queue_sched(0, adbkbd_powerbutton, sc);
401 		}
402 	} else {
403 
404 		adbkbd_key(sc, k1);
405 		if (k2 != 0xff)
406 			adbkbd_key(sc, k2);
407 	}
408 }
409 
410 static void
411 adbkbd_powerbutton(void *cookie)
412 {
413 	struct adbkbd_softc *sc = cookie;
414 
415 	sysmon_pswitch_event(&sc->sc_sm_pbutton,
416 	    ADBK_PRESS(sc->sc_pe) ? PSWITCH_EVENT_PRESSED :
417 	    PSWITCH_EVENT_RELEASED);
418 }
419 
420 static inline void
421 adbkbd_key(struct adbkbd_softc *sc, uint8_t k)
422 {
423 
424 	if (sc->sc_poll) {
425 		if (sc->sc_polled_chars >= 16) {
426 			aprint_error_dev(sc->sc_dev,"polling buffer is full\n");
427 		}
428 		sc->sc_pollbuf[sc->sc_polled_chars] = k;
429 		sc->sc_polled_chars++;
430 		return;
431 	}
432 
433 #if NWSMOUSE > 0
434 	/* translate some keys to mouse events */
435 	if (sc->sc_wsmousedev != NULL) {
436 		if (ADBK_KEYVAL(k) == sc->sc_trans[1]) {
437 			wsmouse_input(sc->sc_wsmousedev, ADBK_PRESS(k) ? 2 : 0,
438 			      0, 0, 0, 0,
439 			      WSMOUSE_INPUT_DELTA);
440 			return;
441 		}
442 		if (ADBK_KEYVAL(k) == sc->sc_trans[2]) {
443 			wsmouse_input(sc->sc_wsmousedev, ADBK_PRESS(k) ? 4 : 0,
444 			      0, 0, 0, 0,
445 			      WSMOUSE_INPUT_DELTA);
446 			return;
447 		}
448 	}
449 #endif
450 
451 #ifdef WSDISPLAY_COMPAT_RAWKBD
452 	if (sc->sc_rawkbd) {
453 		char cbuf[2];
454 		int s;
455 
456 		cbuf[0] = k;
457 
458 		s = spltty();
459 		wskbd_rawinput(sc->sc_wskbddev, cbuf, 1);
460 		splx(s);
461 	} else {
462 #endif
463 
464 	if (ADBK_KEYVAL(k) == 0x39) {
465 		/* caps lock - send up and down */
466 		if (ADBK_PRESS(k) != sc->sc_capslock) {
467 			sc->sc_capslock = ADBK_PRESS(k);
468 			wskbd_input(sc->sc_wskbddev,
469 			    WSCONS_EVENT_KEY_DOWN, 0x39);
470 			wskbd_input(sc->sc_wskbddev,
471 			    WSCONS_EVENT_KEY_UP, 0x39);
472 		}
473 	} else {
474 		/* normal event */
475 		int type;
476 
477 		type = ADBK_PRESS(k) ?
478 		    WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
479 		wskbd_input(sc->sc_wskbddev, type, ADBK_KEYVAL(k));
480 	}
481 #ifdef WSDISPLAY_COMPAT_RAWKBD
482 	}
483 #endif
484 }
485 
486 /*
487  * Set the keyboard LED's.
488  *
489  * Automatically translates from ioctl/softc format to the
490  * actual keyboard register format
491  */
492 static void
493 adbkbd_set_leds(void *cookie, int leds)
494 {
495 	struct adbkbd_softc *sc = cookie;
496 	int aleds;
497 	short cmd;
498 	uint8_t buffer[2];
499 
500 	DPRINTF("adbkbd_set_leds: %02x\n", leds);
501 	if ((leds & 0x07) == (sc->sc_leds & 0x07))
502 		return;
503 
504  	if (sc->sc_have_led_control) {
505 
506 		aleds = (~leds & 0x04) | 3;
507 		if (leds & 1)
508 			aleds &= ~2;
509 		if (leds & 2)
510 			aleds &= ~1;
511 
512 		buffer[0] = 0xff;
513 		buffer[1] = aleds | 0xf8;
514 
515 		cmd = ADBLISTEN(sc->sc_adbdev->current_addr, 2);
516 		sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 2,
517 		    buffer);
518 	}
519 
520 	sc->sc_leds = leds & 7;
521 }
522 
523 static void
524 adbkbd_initleds(struct adbkbd_softc *sc)
525 {
526 	short cmd;
527 
528 	/* talk R2 */
529 	cmd = ADBTALK(sc->sc_adbdev->current_addr, 2);
530 	sc->sc_msg_len = 0;
531 	sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 0, NULL);
532 	if (!adbkbd_wait(sc, 10)) {
533 		printf("unable to read LED state\n");
534 		return;
535 	}
536 	sc->sc_have_led_control = 1;
537 	DPRINTF("have LED control\n");
538 	return;
539 }
540 
541 static int
542 adbkbd_enable(void *v, int on)
543 {
544 	return 0;
545 }
546 
547 static int
548 adbkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
549 {
550 	struct adbkbd_softc *sc = (struct adbkbd_softc *) v;
551 
552 	switch (cmd) {
553 
554 	case WSKBDIO_GTYPE:
555 		*(int *)data = WSKBD_TYPE_ADB;
556 		return 0;
557 	case WSKBDIO_SETLEDS:
558 		adbkbd_set_leds(sc, *(int *)data);
559 		return 0;
560 	case WSKBDIO_GETLEDS:
561 		*(int *)data = sc->sc_leds;
562 		return 0;
563 #ifdef WSDISPLAY_COMPAT_RAWKBD
564 	case WSKBDIO_SETMODE:
565 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
566 		return 0;
567 #endif
568 	}
569 
570 	return EPASSTHROUGH;
571 }
572 
573 int
574 adbkbd_cnattach(void)
575 {
576 
577 	adbkbd_is_console = 1;
578 	return 0;
579 }
580 
581 static void
582 adbkbd_cngetc(void *v, u_int *type, int *data)
583 {
584 	struct adbkbd_softc *sc = v;
585 	int key, press, val;
586 	int s;
587 
588 	s = splhigh();
589 
590 	KASSERT(sc->sc_poll);
591 
592 	DPRINTF("polling...");
593 	while (sc->sc_polled_chars == 0) {
594 		sc->sc_ops->poll(sc->sc_ops->cookie);
595 	}
596 	DPRINTF(" got one\n");
597 	splx(s);
598 
599 	key = sc->sc_pollbuf[0];
600 	sc->sc_polled_chars--;
601 	memmove(sc->sc_pollbuf, sc->sc_pollbuf + 1,
602 		sc->sc_polled_chars);
603 
604 	press = ADBK_PRESS(key);
605 	val = ADBK_KEYVAL(key);
606 
607 	*data = val;
608 	*type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
609 }
610 
611 static void
612 adbkbd_cnpollc(void *v, int on)
613 {
614 	struct adbkbd_softc *sc = v;
615 
616 	sc->sc_poll = on;
617 	if (!on) {
618 		int i;
619 
620 		/* feed the poll buffer's content to wskbd */
621 		for (i = 0; i < sc->sc_polled_chars; i++) {
622 			adbkbd_key(sc, sc->sc_pollbuf[i]);
623 		}
624 		sc->sc_polled_chars = 0;
625 	}
626 }
627 
628 #if NWSMOUSE > 0
629 /* stuff for the pseudo mouse */
630 static int
631 adbkms_enable(void *v)
632 {
633 	return 0;
634 }
635 
636 static int
637 adbkms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
638 {
639 
640 	switch (cmd) {
641 	case WSMOUSEIO_GTYPE:
642 		*(u_int *)data = WSMOUSE_TYPE_PSEUDO;
643 		break;
644 
645 	default:
646 		return (EPASSTHROUGH);
647 	}
648 	return (0);
649 }
650 
651 static void
652 adbkms_disable(void *v)
653 {
654 }
655 
656 static void
657 adbkbd_setup_sysctl(struct adbkbd_softc *sc)
658 {
659 	const struct sysctlnode *me, *node;
660 	int ret;
661 
662 	DPRINTF("%s: sysctl setup\n", device_xname(sc->sc_dev));
663 	ret = sysctl_createv(NULL, 0, NULL, &me,
664 	       CTLFLAG_READWRITE,
665 	       CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
666 	       NULL, 0, NULL, 0,
667 	       CTL_MACHDEP, CTL_CREATE, CTL_EOL);
668 
669 	ret = sysctl_createv(NULL, 0, NULL,
670 	    (void *)&node,
671 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
672 	    CTLTYPE_INT, "middle", "middle mouse button", adbkbd_sysctl_mid,
673 		    1, sc, 0, CTL_MACHDEP, me->sysctl_num, CTL_CREATE,
674 		    CTL_EOL);
675 
676 	ret = sysctl_createv(NULL, 0, NULL,
677 	    (void *)&node,
678 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
679 	    CTLTYPE_INT, "right", "right mouse button", adbkbd_sysctl_right,
680 		    2, sc, 0, CTL_MACHDEP, me->sysctl_num, CTL_CREATE,
681 		    CTL_EOL);
682 }
683 
684 static int
685 adbkbd_sysctl_mid(SYSCTLFN_ARGS)
686 {
687 	struct sysctlnode node = *rnode;
688 	struct adbkbd_softc *sc=(struct adbkbd_softc *)node.sysctl_data;
689 	const int *np = newp;
690 	int reg;
691 
692 	DPRINTF("adbkbd_sysctl_mid\n");
693 	reg = sc->sc_trans[1];
694 	if (np) {
695 		/* we're asked to write */
696 		node.sysctl_data = &reg;
697 		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
698 
699 			sc->sc_trans[1] = *(int *)node.sysctl_data;
700 			return 0;
701 		}
702 		return EINVAL;
703 	} else {
704 		node.sysctl_data = &reg;
705 		node.sysctl_size = 4;
706 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
707 	}
708 }
709 
710 static int
711 adbkbd_sysctl_right(SYSCTLFN_ARGS)
712 {
713 	struct sysctlnode node = *rnode;
714 	struct adbkbd_softc *sc=(struct adbkbd_softc *)node.sysctl_data;
715 	const int *np = newp;
716 	int reg;
717 
718 	DPRINTF("adbkbd_sysctl_right\n");
719 	reg = sc->sc_trans[2];
720 	if (np) {
721 		/* we're asked to write */
722 		node.sysctl_data = &reg;
723 		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
724 
725 			sc->sc_trans[2] = *(int *)node.sysctl_data;
726 			return 0;
727 		}
728 		return EINVAL;
729 	} else {
730 		node.sysctl_data = &reg;
731 		node.sysctl_size = 4;
732 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
733 	}
734 }
735 
736 SYSCTL_SETUP(sysctl_adbkbdtrans_setup, "adbkbd translator setup")
737 {
738 
739 	sysctl_createv(NULL, 0, NULL, NULL,
740 		       CTLFLAG_PERMANENT,
741 		       CTLTYPE_NODE, "machdep", NULL,
742 		       NULL, 0, NULL, 0,
743 		       CTL_MACHDEP, CTL_EOL);
744 }
745 #endif /* NWSMOUSE > 0 */
746