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