adb_kbd.c revision 1.27 1 /* $NetBSD: adb_kbd.c,v 1.27 2017/10/28 04:53:55 riastradh 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.27 2017/10/28 04:53:55 riastradh 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 "ioconf.h"
64
65 #include "opt_wsdisplay_compat.h"
66 #include "opt_adbkbd.h"
67 #include "adbdebug.h"
68 #include "wsmouse.h"
69
70 struct adbkbd_softc {
71 device_t sc_dev;
72 struct adb_device *sc_adbdev;
73 struct adb_bus_accessops *sc_ops;
74 device_t sc_wskbddev;
75 #if NWSMOUSE > 0
76 device_t sc_wsmousedev;
77 #endif
78 struct sysmon_pswitch sc_sm_pbutton;
79 int sc_leds;
80 int sc_have_led_control;
81 int sc_power_button_delay;
82 int sc_msg_len;
83 int sc_event;
84 int sc_poll;
85 int sc_polled_chars;
86 int sc_trans[3];
87 int sc_capslock;
88 uint32_t sc_timestamp;
89 #ifdef WSDISPLAY_COMPAT_RAWKBD
90 int sc_rawkbd;
91 #endif
92 bool sc_emul_usb;
93 bool sc_power_dbg;
94
95 uint32_t sc_power;
96 uint8_t sc_buffer[16];
97 uint8_t sc_pollbuf[16];
98 uint8_t sc_us, sc_pe;
99 };
100
101 /*
102 * Function declarations.
103 */
104 static int adbkbd_match(device_t, cfdata_t, void *);
105 static void adbkbd_attach(device_t, device_t, void *);
106
107 static void adbkbd_initleds(struct adbkbd_softc *);
108 static void adbkbd_keys(struct adbkbd_softc *, uint8_t, uint8_t);
109 static inline void adbkbd_key(struct adbkbd_softc *, uint8_t);
110 static int adbkbd_wait(struct adbkbd_softc *, int);
111
112 /* Driver definition. */
113 CFATTACH_DECL_NEW(adbkbd, sizeof(struct adbkbd_softc),
114 adbkbd_match, adbkbd_attach, NULL, NULL);
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 #ifdef DDB
484 Debugger();
485 #else
486 printf("kernel is not compiled with DDB support\n");
487 #endif
488 } else {
489 sysmon_pswitch_event(&sc->sc_sm_pbutton,
490 ADBK_PRESS(sc->sc_pe) ? PSWITCH_EVENT_PRESSED :
491 PSWITCH_EVENT_RELEASED);
492 }
493 }
494
495 static inline void
496 adbkbd_key(struct adbkbd_softc *sc, uint8_t k)
497 {
498
499 if (sc->sc_poll) {
500 if (sc->sc_polled_chars >= 16) {
501 aprint_error_dev(sc->sc_dev,"polling buffer is full\n");
502 }
503 sc->sc_pollbuf[sc->sc_polled_chars] = k;
504 sc->sc_polled_chars++;
505 return;
506 }
507
508 #if NWSMOUSE > 0
509 /* translate some keys to mouse events */
510 if (sc->sc_wsmousedev != NULL) {
511 if (ADBK_KEYVAL(k) == sc->sc_trans[1]) {
512 wsmouse_input(sc->sc_wsmousedev, ADBK_PRESS(k) ? 2 : 0,
513 0, 0, 0, 0,
514 WSMOUSE_INPUT_DELTA);
515 return;
516 }
517 if (ADBK_KEYVAL(k) == sc->sc_trans[2]) {
518 wsmouse_input(sc->sc_wsmousedev, ADBK_PRESS(k) ? 4 : 0,
519 0, 0, 0, 0,
520 WSMOUSE_INPUT_DELTA);
521 return;
522 }
523 }
524 #endif
525
526 #ifdef WSDISPLAY_COMPAT_RAWKBD
527 if (sc->sc_rawkbd) {
528 char cbuf[2];
529 int s;
530
531 cbuf[0] = k;
532
533 s = spltty();
534 wskbd_rawinput(sc->sc_wskbddev, cbuf, 1);
535 splx(s);
536 } else {
537 #endif
538
539 if (ADBK_KEYVAL(k) == 0x39) {
540 /* caps lock - send up and down */
541 if (ADBK_PRESS(k) != sc->sc_capslock) {
542 sc->sc_capslock = ADBK_PRESS(k);
543 wskbd_input(sc->sc_wskbddev,
544 WSCONS_EVENT_KEY_DOWN, 0x39);
545 wskbd_input(sc->sc_wskbddev,
546 WSCONS_EVENT_KEY_UP, 0x39);
547 }
548 } else {
549 /* normal event */
550 int type;
551
552 type = ADBK_PRESS(k) ?
553 WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
554 wskbd_input(sc->sc_wskbddev, type, ADBK_KEYVAL(k));
555 }
556 #ifdef WSDISPLAY_COMPAT_RAWKBD
557 }
558 #endif
559 }
560
561 /*
562 * Set the keyboard LED's.
563 *
564 * Automatically translates from ioctl/softc format to the
565 * actual keyboard register format
566 */
567 static void
568 adbkbd_set_leds(void *cookie, int leds)
569 {
570 struct adbkbd_softc *sc = cookie;
571 int aleds;
572 short cmd;
573 uint8_t buffer[2];
574
575 DPRINTF("adbkbd_set_leds: %02x\n", leds);
576 if ((leds & 0x07) == (sc->sc_leds & 0x07))
577 return;
578
579 if (sc->sc_have_led_control) {
580
581 aleds = (~leds & 0x04) | 3;
582 if (leds & 1)
583 aleds &= ~2;
584 if (leds & 2)
585 aleds &= ~1;
586
587 buffer[0] = 0xff;
588 buffer[1] = aleds | 0xf8;
589
590 cmd = ADBLISTEN(sc->sc_adbdev->current_addr, 2);
591 sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 2,
592 buffer);
593 }
594
595 sc->sc_leds = leds & 7;
596 }
597
598 static void
599 adbkbd_initleds(struct adbkbd_softc *sc)
600 {
601 short cmd;
602
603 /* talk R2 */
604 cmd = ADBTALK(sc->sc_adbdev->current_addr, 2);
605 sc->sc_msg_len = 0;
606 sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, 0, NULL);
607 if (!adbkbd_wait(sc, 10)) {
608 aprint_error_dev(sc->sc_dev, "unable to read LED state\n");
609 return;
610 }
611 sc->sc_have_led_control = 1;
612 DPRINTF("have LED control\n");
613 return;
614 }
615
616 static int
617 adbkbd_enable(void *v, int on)
618 {
619 return 0;
620 }
621
622 static int
623 adbkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
624 {
625 struct adbkbd_softc *sc = (struct adbkbd_softc *) v;
626
627 switch (cmd) {
628
629 case WSKBDIO_GTYPE:
630 if (sc->sc_emul_usb) {
631 *(int *)data = WSKBD_TYPE_USB;
632 } else {
633 *(int *)data = WSKBD_TYPE_ADB;
634 }
635 return 0;
636 case WSKBDIO_SETLEDS:
637 adbkbd_set_leds(sc, *(int *)data);
638 return 0;
639 case WSKBDIO_GETLEDS:
640 *(int *)data = sc->sc_leds;
641 return 0;
642 #ifdef WSDISPLAY_COMPAT_RAWKBD
643 case WSKBDIO_SETMODE:
644 sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
645 return 0;
646 #endif
647 }
648
649 return EPASSTHROUGH;
650 }
651
652 int
653 adbkbd_cnattach(void)
654 {
655
656 adbkbd_is_console = 1;
657 return 0;
658 }
659
660 static void
661 adbkbd_cngetc(void *v, u_int *type, int *data)
662 {
663 struct adbkbd_softc *sc = v;
664 int key, press, val;
665 int s;
666
667 s = splhigh();
668
669 KASSERT(sc->sc_poll);
670
671 DPRINTF("polling...");
672 while (sc->sc_polled_chars == 0) {
673 sc->sc_ops->poll(sc->sc_ops->cookie);
674 }
675 DPRINTF(" got one\n");
676 splx(s);
677
678 key = sc->sc_pollbuf[0];
679 sc->sc_polled_chars--;
680 memmove(sc->sc_pollbuf, sc->sc_pollbuf + 1,
681 sc->sc_polled_chars);
682
683 press = ADBK_PRESS(key);
684 val = ADBK_KEYVAL(key);
685
686 *data = val;
687 *type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
688 }
689
690 static void
691 adbkbd_cnpollc(void *v, int on)
692 {
693 struct adbkbd_softc *sc = v;
694
695 sc->sc_poll = on;
696 if (!on) {
697 int i;
698
699 /* feed the poll buffer's content to wskbd */
700 for (i = 0; i < sc->sc_polled_chars; i++) {
701 adbkbd_key(sc, sc->sc_pollbuf[i]);
702 }
703 sc->sc_polled_chars = 0;
704 }
705 }
706
707 #if NWSMOUSE > 0
708 /* stuff for the pseudo mouse */
709 static int
710 adbkms_enable(void *v)
711 {
712 return 0;
713 }
714
715 static int
716 adbkms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
717 {
718
719 switch (cmd) {
720 case WSMOUSEIO_GTYPE:
721 *(u_int *)data = WSMOUSE_TYPE_PSEUDO;
722 break;
723
724 default:
725 return (EPASSTHROUGH);
726 }
727 return (0);
728 }
729
730 static void
731 adbkms_disable(void *v)
732 {
733 }
734
735 static int
736 adbkbd_sysctl_mid(SYSCTLFN_ARGS)
737 {
738 struct sysctlnode node = *rnode;
739 struct adbkbd_softc *sc=(struct adbkbd_softc *)node.sysctl_data;
740 const int *np = newp;
741 int reg;
742
743 DPRINTF("adbkbd_sysctl_mid\n");
744 reg = sc->sc_trans[1];
745 if (np) {
746 /* we're asked to write */
747 node.sysctl_data = ®
748 if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
749
750 sc->sc_trans[1] = *(int *)node.sysctl_data;
751 return 0;
752 }
753 return EINVAL;
754 } else {
755 node.sysctl_data = ®
756 node.sysctl_size = 4;
757 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
758 }
759 }
760
761 static int
762 adbkbd_sysctl_right(SYSCTLFN_ARGS)
763 {
764 struct sysctlnode node = *rnode;
765 struct adbkbd_softc *sc=(struct adbkbd_softc *)node.sysctl_data;
766 const int *np = newp;
767 int reg;
768
769 DPRINTF("adbkbd_sysctl_right\n");
770 reg = sc->sc_trans[2];
771 if (np) {
772 /* we're asked to write */
773 node.sysctl_data = ®
774 if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
775
776 sc->sc_trans[2] = *(int *)node.sysctl_data;
777 return 0;
778 }
779 return EINVAL;
780 } else {
781 node.sysctl_data = ®
782 node.sysctl_size = 4;
783 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
784 }
785 }
786
787 #endif /* NWSMOUSE > 0 */
788
789 static int
790 adbkbd_sysctl_usb(SYSCTLFN_ARGS)
791 {
792 struct sysctlnode node = *rnode;
793 struct adbkbd_softc *sc=(struct adbkbd_softc *)node.sysctl_data;
794 const int *np = newp;
795 bool reg;
796
797 DPRINTF("%s\n", __func__);
798 reg = sc->sc_emul_usb;
799 if (np) {
800 /* we're asked to write */
801 node.sysctl_data = ®
802 if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
803
804 sc->sc_emul_usb = *(bool *)node.sysctl_data;
805 if (sc->sc_emul_usb) {
806 wskbd_set_evtrans(sc->sc_wskbddev,
807 adb_to_usb, 128);
808 } else {
809 wskbd_set_evtrans(sc->sc_wskbddev, NULL, 0);
810 }
811 return 0;
812 }
813 return EINVAL;
814 } else {
815 node.sysctl_data = ®
816 node.sysctl_size = sizeof(reg);
817 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
818 }
819 }
820
821 static int
822 adbkbd_sysctl_dbg(SYSCTLFN_ARGS)
823 {
824 struct sysctlnode node = *rnode;
825 struct adbkbd_softc *sc=(struct adbkbd_softc *)node.sysctl_data;
826 const int *np = newp;
827 bool reg;
828
829 DPRINTF("%s\n", __func__);
830 reg = sc->sc_power_dbg;
831 if (np) {
832 /* we're asked to write */
833 node.sysctl_data = ®
834 if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
835
836 sc->sc_power_dbg = *(bool *)node.sysctl_data;
837 return 0;
838 }
839 return EINVAL;
840 } else {
841 node.sysctl_data = ®
842 node.sysctl_size = sizeof(reg);
843 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
844 }
845 }
846
847 static void
848 adbkbd_setup_sysctl(struct adbkbd_softc *sc)
849 {
850 const struct sysctlnode *me, *node;
851 int ret;
852
853 DPRINTF("%s: sysctl setup\n", device_xname(sc->sc_dev));
854 ret = sysctl_createv(NULL, 0, NULL, &me,
855 CTLFLAG_READWRITE,
856 CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
857 NULL, 0, NULL, 0,
858 CTL_MACHDEP, CTL_CREATE, CTL_EOL);
859 ret = sysctl_createv(NULL, 0, NULL,
860 (void *)&node,
861 CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
862 CTLTYPE_BOOL, "emulate_usb", "USB keyboard emulation",
863 adbkbd_sysctl_usb, 1, (void *)sc, 0, CTL_MACHDEP,
864 me->sysctl_num, CTL_CREATE, CTL_EOL);
865 ret = sysctl_createv(NULL, 0, NULL,
866 (void *)&node,
867 CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
868 CTLTYPE_BOOL, "power_ddb", "power button triggers ddb",
869 adbkbd_sysctl_dbg, 1, (void *)sc, 0, CTL_MACHDEP,
870 me->sysctl_num, CTL_CREATE, CTL_EOL);
871 #if NWSMOUSE > 0
872 if (sc->sc_wsmousedev != NULL) {
873 ret = sysctl_createv(NULL, 0, NULL,
874 (void *)&node,
875 CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
876 CTLTYPE_INT, "middle", "middle mouse button",
877 adbkbd_sysctl_mid, 1, (void *)sc, 0, CTL_MACHDEP,
878 me->sysctl_num, CTL_CREATE, CTL_EOL);
879
880 ret = sysctl_createv(NULL, 0, NULL,
881 (void *)&node,
882 CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
883 CTLTYPE_INT, "right", "right mouse button",
884 adbkbd_sysctl_right, 2, (void *)sc, 0, CTL_MACHDEP,
885 me->sysctl_num, CTL_CREATE, CTL_EOL);
886 }
887 #endif /* NWSMOUSE > 0 */
888
889 (void)ret;
890 }
891
892 SYSCTL_SETUP(sysctl_adbkbdtrans_setup, "adbkbd translator setup")
893 {
894
895 sysctl_createv(NULL, 0, NULL, NULL,
896 CTLFLAG_PERMANENT,
897 CTLTYPE_NODE, "machdep", NULL,
898 NULL, 0, NULL, 0,
899 CTL_MACHDEP, CTL_EOL);
900 }
901