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