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