Index: stable/11/sys/dev/adb/adb_kbd.c =================================================================== --- stable/11/sys/dev/adb/adb_kbd.c (revision 356006) +++ stable/11/sys/dev/adb/adb_kbd.c (revision 356007) @@ -1,890 +1,890 @@ /*- * Copyright (C) 2008 Nathan Whitehorn * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include #include "opt_kbd.h" #include #include #include #include #include #include #include "adb.h" #define KBD_DRIVER_NAME "akbd" #define AKBD_EMULATE_ATKBD 1 static int adb_kbd_probe(device_t dev); static int adb_kbd_attach(device_t dev); static int adb_kbd_detach(device_t dev); static void akbd_repeat(void *xsc); static int adb_fn_keys(SYSCTL_HANDLER_ARGS); static u_int adb_kbd_receive_packet(device_t dev, u_char status, u_char command, u_char reg, int len, u_char *data); struct adb_kbd_softc { keyboard_t sc_kbd; device_t sc_dev; struct mtx sc_mutex; struct cv sc_cv; int sc_mode; int sc_state; int have_led_control; uint8_t buffer[8]; #ifdef AKBD_EMULATE_ATKBD uint8_t at_buffered_char[2]; #endif volatile int buffers; struct callout sc_repeater; int sc_repeatstart; int sc_repeatcontinue; uint8_t last_press; }; static device_method_t adb_kbd_methods[] = { /* Device interface */ DEVMETHOD(device_probe, adb_kbd_probe), DEVMETHOD(device_attach, adb_kbd_attach), DEVMETHOD(device_detach, adb_kbd_detach), DEVMETHOD(device_shutdown, bus_generic_shutdown), DEVMETHOD(device_suspend, bus_generic_suspend), DEVMETHOD(device_resume, bus_generic_resume), /* ADB interface */ DEVMETHOD(adb_receive_packet, adb_kbd_receive_packet), { 0, 0 } }; static driver_t adb_kbd_driver = { "akbd", adb_kbd_methods, sizeof(struct adb_kbd_softc), }; static devclass_t adb_kbd_devclass; DRIVER_MODULE(akbd, adb, adb_kbd_driver, adb_kbd_devclass, 0, 0); #ifdef AKBD_EMULATE_ATKBD #define SCAN_PRESS 0x000 #define SCAN_RELEASE 0x080 #define SCAN_PREFIX_E0 0x100 #define SCAN_PREFIX_E1 0x200 #define SCAN_PREFIX_CTL 0x400 #define SCAN_PREFIX_SHIFT 0x800 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \ SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT) static const uint8_t adb_to_at_scancode_map[128] = { 30, 31, 32, 33, 35, 34, 44, 45, 46, 47, 0, 48, 16, 17, 18, 19, 21, 20, 2, 3, 4, 5, 7, 6, 13, 10, 8, 12, 9, 11, 27, 24, 22, 26, 23, 25, 28, 38, 36, 40, 37, 39, 43, 51, 53, 49, 50, 52, 15, 57, 41, 14, 0, 1, 29, 0, 42, 58, 56, 97, 98, 100, 95, 0, 0, 83, 0, 55, 0, 78, 0, 69, 0, 0, 0, 91, 89, 0, 74, 13, 0, 0, 82, 79, 80, 81, 75, 76, 77, 71, 0, 72, 73, 0, 0, 0, 63, 64, 65, 61, 66, 67, 0, 87, 0, 105, 0, 70, 0, 68, 0, 88, 0, 107, 102, 94, 96, 103, 62, 99, 60, 101, 59, 54, 93, 90, 0, 0 }; static int keycode2scancode(int keycode, int shift, int up) { static const int scan[] = { /* KP enter, right ctrl, KP divide */ 0x1c , 0x1d , 0x35 , /* print screen */ 0x37 | SCAN_PREFIX_SHIFT, /* right alt, home, up, page up, left, right, end */ 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f, /* down, page down, insert, delete */ 0x50, 0x51, 0x52, 0x53, /* pause/break (see also below) */ 0x46, /* * MS: left window, right window, menu * also Sun: left meta, right meta, compose */ 0x5b, 0x5c, 0x5d, /* Sun type 6 USB */ /* help, stop, again, props, undo, front, copy */ 0x68, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, /* open, paste, find, cut, audiomute, audiolower, audioraise */ 0x64, 0x65, 0x66, 0x67, 0x25, 0x1f, 0x1e, /* power */ 0x20 }; int scancode; scancode = keycode; if ((keycode >= 89) && (keycode < 89 + nitems(scan))) scancode = scan[keycode - 89] | SCAN_PREFIX_E0; /* pause/break */ if ((keycode == 104) && !(shift & CTLS)) scancode = 0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL; if (shift & SHIFTS) scancode &= ~SCAN_PREFIX_SHIFT; return (scancode | (up ? SCAN_RELEASE : SCAN_PRESS)); } #endif /* keyboard driver declaration */ static int akbd_configure(int flags); static kbd_probe_t akbd_probe; static kbd_init_t akbd_init; static kbd_term_t akbd_term; static kbd_intr_t akbd_interrupt; static kbd_test_if_t akbd_test_if; static kbd_enable_t akbd_enable; static kbd_disable_t akbd_disable; static kbd_read_t akbd_read; static kbd_check_t akbd_check; static kbd_read_char_t akbd_read_char; static kbd_check_char_t akbd_check_char; static kbd_ioctl_t akbd_ioctl; static kbd_lock_t akbd_lock; static kbd_clear_state_t akbd_clear_state; static kbd_get_state_t akbd_get_state; static kbd_set_state_t akbd_set_state; static kbd_poll_mode_t akbd_poll; keyboard_switch_t akbdsw = { - akbd_probe, - akbd_init, - akbd_term, - akbd_interrupt, - akbd_test_if, - akbd_enable, - akbd_disable, - akbd_read, - akbd_check, - akbd_read_char, - akbd_check_char, - akbd_ioctl, - akbd_lock, - akbd_clear_state, - akbd_get_state, - akbd_set_state, - genkbd_get_fkeystr, - akbd_poll, - genkbd_diag, + .probe = akbd_probe, + .init = akbd_init, + .term = akbd_term, + .intr = akbd_interrupt, + .test_if = akbd_test_if, + .enable = akbd_enable, + .disable = akbd_disable, + .read = akbd_read, + .check = akbd_check, + .read_char = akbd_read_char, + .check_char = akbd_check_char, + .ioctl = akbd_ioctl, + .lock = akbd_lock, + .clear_state = akbd_clear_state, + .get_state = akbd_get_state, + .set_state = akbd_set_state, + .get_fkeystr = genkbd_get_fkeystr, + .poll = akbd_poll, + .diag = genkbd_diag, }; KEYBOARD_DRIVER(akbd, akbdsw, akbd_configure); static int adb_kbd_probe(device_t dev) { uint8_t type; type = adb_get_device_type(dev); if (type != ADB_DEVICE_KEYBOARD) return (ENXIO); switch(adb_get_device_handler(dev)) { case 1: device_set_desc(dev,"Apple Standard Keyboard"); break; case 2: device_set_desc(dev,"Apple Extended Keyboard"); break; case 4: device_set_desc(dev,"Apple ISO Keyboard"); break; case 5: device_set_desc(dev,"Apple Extended ISO Keyboard"); break; case 8: device_set_desc(dev,"Apple Keyboard II"); break; case 9: device_set_desc(dev,"Apple ISO Keyboard II"); break; case 12: device_set_desc(dev,"PowerBook Keyboard"); break; case 13: device_set_desc(dev,"PowerBook ISO Keyboard"); break; case 24: device_set_desc(dev,"PowerBook Extended Keyboard"); break; case 27: device_set_desc(dev,"Apple Design Keyboard"); break; case 195: device_set_desc(dev,"PowerBook G3 Keyboard"); break; case 196: device_set_desc(dev,"iBook Keyboard"); break; default: device_set_desc(dev,"ADB Keyboard"); break; } return (0); } static int ms_to_ticks(int ms) { if (hz > 1000) return ms*(hz/1000); return ms/(1000/hz); } static int adb_kbd_attach(device_t dev) { struct adb_kbd_softc *sc; keyboard_switch_t *sw; uint32_t fkeys; phandle_t handle; sw = kbd_get_switch(KBD_DRIVER_NAME); if (sw == NULL) { return ENXIO; } sc = device_get_softc(dev); sc->sc_dev = dev; sc->sc_mode = K_RAW; sc->sc_state = 0; sc->have_led_control = 0; sc->buffers = 0; /* Try stepping forward to the extended keyboard protocol */ adb_set_device_handler(dev,3); mtx_init(&sc->sc_mutex, KBD_DRIVER_NAME, NULL, MTX_DEF); cv_init(&sc->sc_cv,KBD_DRIVER_NAME); callout_init(&sc->sc_repeater, 0); #ifdef AKBD_EMULATE_ATKBD kbd_init_struct(&sc->sc_kbd, KBD_DRIVER_NAME, KB_101, 0, 0, 0, 0); kbd_set_maps(&sc->sc_kbd, &key_map, &accent_map, fkey_tab, sizeof(fkey_tab) / sizeof(fkey_tab[0])); #else #error ADB raw mode not implemented #endif KBD_FOUND_DEVICE(&sc->sc_kbd); KBD_PROBE_DONE(&sc->sc_kbd); KBD_INIT_DONE(&sc->sc_kbd); KBD_CONFIG_DONE(&sc->sc_kbd); (*sw->enable)(&sc->sc_kbd); kbd_register(&sc->sc_kbd); #ifdef KBD_INSTALL_CDEV if (kbd_attach(&sc->sc_kbd)) { adb_kbd_detach(dev); return ENXIO; } #endif /* Check if we can read out the LED state from this keyboard by reading the key state register */ if (adb_read_register(dev, 2, NULL) == 2) sc->have_led_control = 1; adb_set_autopoll(dev,1); handle = OF_finddevice("mac-io/via-pmu/adb/keyboard"); if (handle != -1 && OF_getprop(handle, "AAPL,has-embedded-fn-keys", &fkeys, sizeof(fkeys)) != -1) { static const char *key_names[] = {"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12"}; struct sysctl_ctx_list *ctx; struct sysctl_oid *tree; int i; if (bootverbose) device_printf(dev, "Keyboard has embedded Fn keys\n"); for (i = 0; i < 12; i++) { uint32_t keyval; char buf[3]; if (OF_getprop(handle, key_names[i], &keyval, sizeof(keyval)) < 0) continue; buf[0] = 1; buf[1] = i+1; buf[2] = keyval; adb_write_register(dev, 0, 3, buf); } adb_write_register(dev, 1, 2, &(uint16_t){0}); ctx = device_get_sysctl_ctx(dev); tree = device_get_sysctl_tree(dev); SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "fn_keys_function_as_primary", CTLTYPE_INT | CTLFLAG_RW, sc, 0, adb_fn_keys, "I", "Set the Fn keys to be their F-key type as default"); } return (0); } static int adb_kbd_detach(device_t dev) { struct adb_kbd_softc *sc; keyboard_t *kbd; sc = device_get_softc(dev); adb_set_autopoll(dev,0); callout_stop(&sc->sc_repeater); mtx_lock(&sc->sc_mutex); kbd = kbd_get_keyboard(kbd_find_keyboard(KBD_DRIVER_NAME, device_get_unit(dev))); kbdd_disable(kbd); #ifdef KBD_INSTALL_CDEV kbd_detach(kbd); #endif kbdd_term(kbd); mtx_unlock(&sc->sc_mutex); mtx_destroy(&sc->sc_mutex); cv_destroy(&sc->sc_cv); return (0); } static u_int adb_kbd_receive_packet(device_t dev, u_char status, u_char command, u_char reg, int len, u_char *data) { struct adb_kbd_softc *sc; sc = device_get_softc(dev); if (command != ADB_COMMAND_TALK) return 0; if (reg != 0 || len != 2) return (0); mtx_lock(&sc->sc_mutex); /* 0x7f is always the power button */ if (data[0] == 0x7f) { devctl_notify("PMU", "Button", "pressed", NULL); mtx_unlock(&sc->sc_mutex); return (0); } else if (data[0] == 0xff) { mtx_unlock(&sc->sc_mutex); return (0); /* Ignore power button release. */ } if ((data[0] & 0x7f) == 57 && sc->buffers < 7) { /* Fake the down/up cycle for caps lock */ sc->buffer[sc->buffers++] = data[0] & 0x7f; sc->buffer[sc->buffers++] = (data[0] & 0x7f) | (1 << 7); } else { sc->buffer[sc->buffers++] = data[0]; } if (sc->buffer[sc->buffers-1] < 0xff) sc->last_press = sc->buffer[sc->buffers-1]; if ((data[1] & 0x7f) == 57 && sc->buffers < 7) { /* Fake the down/up cycle for caps lock */ sc->buffer[sc->buffers++] = data[1] & 0x7f; sc->buffer[sc->buffers++] = (data[1] & 0x7f) | (1 << 7); } else { sc->buffer[sc->buffers++] = data[1]; } if (sc->buffer[sc->buffers-1] < 0xff) sc->last_press = sc->buffer[sc->buffers-1]; /* Stop any existing key repeating */ callout_stop(&sc->sc_repeater); /* Schedule a repeat callback on keydown */ if (!(sc->last_press & (1 << 7))) { callout_reset(&sc->sc_repeater, ms_to_ticks(sc->sc_kbd.kb_delay1), akbd_repeat, sc); } mtx_unlock(&sc->sc_mutex); cv_broadcast(&sc->sc_cv); if (KBD_IS_ACTIVE(&sc->sc_kbd) && KBD_IS_BUSY(&sc->sc_kbd)) { sc->sc_kbd.kb_callback.kc_func(&sc->sc_kbd, KBDIO_KEYINPUT, sc->sc_kbd.kb_callback.kc_arg); } return (0); } static void akbd_repeat(void *xsc) { struct adb_kbd_softc *sc = xsc; int notify_kbd = 0; /* Fake an up/down key repeat so long as we have the free buffers */ mtx_lock(&sc->sc_mutex); if (sc->buffers < 7) { sc->buffer[sc->buffers++] = sc->last_press | (1 << 7); sc->buffer[sc->buffers++] = sc->last_press; notify_kbd = 1; } mtx_unlock(&sc->sc_mutex); if (notify_kbd && KBD_IS_ACTIVE(&sc->sc_kbd) && KBD_IS_BUSY(&sc->sc_kbd)) { sc->sc_kbd.kb_callback.kc_func(&sc->sc_kbd, KBDIO_KEYINPUT, sc->sc_kbd.kb_callback.kc_arg); } /* Reschedule the callout */ callout_reset(&sc->sc_repeater, ms_to_ticks(sc->sc_kbd.kb_delay2), akbd_repeat, sc); } static int akbd_configure(int flags) { return 0; } static int akbd_probe(int unit, void *arg, int flags) { return 0; } static int akbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) { return 0; } static int akbd_term(keyboard_t *kbd) { return 0; } static int akbd_interrupt(keyboard_t *kbd, void *arg) { return 0; } static int akbd_test_if(keyboard_t *kbd) { return 0; } static int akbd_enable(keyboard_t *kbd) { KBD_ACTIVATE(kbd); return (0); } static int akbd_disable(keyboard_t *kbd) { struct adb_kbd_softc *sc; sc = (struct adb_kbd_softc *)(kbd); callout_stop(&sc->sc_repeater); KBD_DEACTIVATE(kbd); return (0); } static int akbd_read(keyboard_t *kbd, int wait) { return (0); } static int akbd_check(keyboard_t *kbd) { struct adb_kbd_softc *sc; if (!KBD_IS_ACTIVE(kbd)) return (FALSE); sc = (struct adb_kbd_softc *)(kbd); mtx_lock(&sc->sc_mutex); #ifdef AKBD_EMULATE_ATKBD if (sc->at_buffered_char[0]) { mtx_unlock(&sc->sc_mutex); return (TRUE); } #endif if (sc->buffers > 0) { mtx_unlock(&sc->sc_mutex); return (TRUE); } mtx_unlock(&sc->sc_mutex); return (FALSE); } static u_int akbd_read_char(keyboard_t *kbd, int wait) { struct adb_kbd_softc *sc; uint16_t key; uint8_t adb_code; int i; sc = (struct adb_kbd_softc *)(kbd); mtx_lock(&sc->sc_mutex); #if defined(AKBD_EMULATE_ATKBD) if (sc->sc_mode == K_RAW && sc->at_buffered_char[0]) { key = sc->at_buffered_char[0]; if (key & SCAN_PREFIX) { sc->at_buffered_char[0] = key & ~SCAN_PREFIX; key = (key & SCAN_PREFIX_E0) ? 0xe0 : 0xe1; } else { sc->at_buffered_char[0] = sc->at_buffered_char[1]; sc->at_buffered_char[1] = 0; } mtx_unlock(&sc->sc_mutex); return (key); } #endif if (!sc->buffers && wait) cv_wait(&sc->sc_cv,&sc->sc_mutex); if (!sc->buffers) { mtx_unlock(&sc->sc_mutex); return (NOKEY); } adb_code = sc->buffer[0]; for (i = 1; i < sc->buffers; i++) sc->buffer[i-1] = sc->buffer[i]; sc->buffers--; #ifdef AKBD_EMULATE_ATKBD key = adb_to_at_scancode_map[adb_code & 0x7f]; if (sc->sc_mode == K_CODE) { /* Add the key-release bit */ key |= adb_code & 0x80; } else if (sc->sc_mode == K_RAW) { /* * In the raw case, we have to emulate the gross * variable-length AT keyboard thing. Since this code * is copied from sunkbd, which is the same code * as ukbd, it might be nice to have this centralized. */ key = keycode2scancode(key, 0, adb_code & 0x80); if (key & SCAN_PREFIX) { if (key & SCAN_PREFIX_CTL) { sc->at_buffered_char[0] = 0x1d | (key & SCAN_RELEASE); sc->at_buffered_char[1] = key & ~SCAN_PREFIX; } else if (key & SCAN_PREFIX_SHIFT) { sc->at_buffered_char[0] = 0x2a | (key & SCAN_RELEASE); sc->at_buffered_char[1] = key & ~SCAN_PREFIX_SHIFT; } else { sc->at_buffered_char[0] = key & ~SCAN_PREFIX; sc->at_buffered_char[1] = 0; } key = (key & SCAN_PREFIX_E0) ? 0xe0 : 0xe1; } } #else key = adb_code; #endif mtx_unlock(&sc->sc_mutex); return (key); } static int akbd_check_char(keyboard_t *kbd) { if (!KBD_IS_ACTIVE(kbd)) return (FALSE); return (akbd_check(kbd)); } static int set_typematic(keyboard_t *kbd, int code) { /* These numbers are in microseconds, so convert to ticks */ static int delays[] = { 250, 500, 750, 1000 }; static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63, 68, 76, 84, 92, 100, 110, 118, 126, 136, 152, 168, 184, 200, 220, 236, 252, 272, 304, 336, 368, 400, 440, 472, 504 }; if (code & ~0x7f) return EINVAL; kbd->kb_delay1 = delays[(code >> 5) & 3]; kbd->kb_delay2 = rates[code & 0x1f]; return 0; } static int akbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t data) { struct adb_kbd_softc *sc; uint16_t r2; int error; sc = (struct adb_kbd_softc *)(kbd); error = 0; switch (cmd) { case KDGKBMODE: *(int *)data = sc->sc_mode; break; case KDSKBMODE: switch (*(int *)data) { case K_XLATE: if (sc->sc_mode != K_XLATE) { /* make lock key state and LED state match */ sc->sc_state &= ~LOCK_MASK; sc->sc_state |= KBD_LED_VAL(kbd); } /* FALLTHROUGH */ case K_RAW: case K_CODE: if (sc->sc_mode != *(int *)data) sc->sc_mode = *(int *)data; break; default: error = EINVAL; break; } break; case KDGETLED: *(int *)data = KBD_LED_VAL(kbd); break; case KDSKBSTATE: if (*(int *)data & ~LOCK_MASK) { error = EINVAL; break; } sc->sc_state &= ~LOCK_MASK; sc->sc_state |= *(int *)data; /* FALLTHROUGH */ case KDSETLED: KBD_LED_VAL(kbd) = *(int *)data; if (!sc->have_led_control) break; r2 = (~0 & 0x04) | 3; if (*(int *)data & NLKED) r2 &= ~1; if (*(int *)data & CLKED) r2 &= ~2; if (*(int *)data & SLKED) r2 &= ~4; adb_send_packet(sc->sc_dev,ADB_COMMAND_LISTEN,2, sizeof(uint16_t),(u_char *)&r2); break; case KDGKBSTATE: *(int *)data = sc->sc_state & LOCK_MASK; break; case KDSETREPEAT: if (!KBD_HAS_DEVICE(kbd)) return 0; if (((int *)data)[1] < 0) return EINVAL; if (((int *)data)[0] < 0) return EINVAL; else if (((int *)data)[0] == 0) /* fastest possible value */ kbd->kb_delay1 = 200; else kbd->kb_delay1 = ((int *)data)[0]; kbd->kb_delay2 = ((int *)data)[1]; break; case KDSETRAD: error = set_typematic(kbd, *(int *)data); break; case PIO_KEYMAP: case OPIO_KEYMAP: case PIO_KEYMAPENT: case PIO_DEADKEYMAP: default: return (genkbd_commonioctl(kbd, cmd, data)); } return (error); } static int akbd_lock(keyboard_t *kbd, int lock) { return (0); } static void akbd_clear_state(keyboard_t *kbd) { struct adb_kbd_softc *sc; sc = (struct adb_kbd_softc *)(kbd); mtx_lock(&sc->sc_mutex); sc->buffers = 0; callout_stop(&sc->sc_repeater); #if defined(AKBD_EMULATE_ATKBD) sc->at_buffered_char[0] = 0; sc->at_buffered_char[1] = 0; #endif mtx_unlock(&sc->sc_mutex); } static int akbd_get_state(keyboard_t *kbd, void *buf, size_t len) { return (0); } static int akbd_set_state(keyboard_t *kbd, void *buf, size_t len) { return (0); } static int akbd_poll(keyboard_t *kbd, int on) { return (0); } static int akbd_modevent(module_t mod, int type, void *data) { switch (type) { case MOD_LOAD: kbd_add_driver(&akbd_kbd_driver); break; case MOD_UNLOAD: kbd_delete_driver(&akbd_kbd_driver); break; default: return (EOPNOTSUPP); } return (0); } static int adb_fn_keys(SYSCTL_HANDLER_ARGS) { struct adb_kbd_softc *sc = arg1; int error; uint16_t is_fn_enabled; unsigned int is_fn_enabled_sysctl; adb_read_register(sc->sc_dev, 1, &is_fn_enabled); is_fn_enabled &= 1; is_fn_enabled_sysctl = is_fn_enabled; error = sysctl_handle_int(oidp, &is_fn_enabled_sysctl, 0, req); if (error || !req->newptr) return (error); is_fn_enabled = is_fn_enabled_sysctl; if (is_fn_enabled != 1 && is_fn_enabled != 0) return (EINVAL); adb_write_register(sc->sc_dev, 1, 2, &is_fn_enabled); return (0); } DEV_MODULE(akbd, akbd_modevent, NULL); Index: stable/11/sys/dev/atkbdc/atkbd.c =================================================================== --- stable/11/sys/dev/atkbdc/atkbd.c (revision 356006) +++ stable/11/sys/dev/atkbdc/atkbd.c (revision 356007) @@ -1,1602 +1,1602 @@ /*- * Copyright (c) 1999 Kazutaka YOKOTA * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer as * the first lines of this file unmodified. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include __FBSDID("$FreeBSD$"); #include "opt_compat.h" #include "opt_kbd.h" #include "opt_atkbd.h" #include "opt_evdev.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef EVDEV_SUPPORT #include #include #endif typedef struct atkbd_state { KBDC kbdc; /* keyboard controller */ int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */ int ks_flags; /* flags */ #define COMPOSE (1 << 0) int ks_polling; int ks_state; /* shift/lock key state */ int ks_accents; /* accent key index (> 0) */ u_int ks_composed_char; /* composed char code (> 0) */ u_char ks_prefix; /* AT scan code prefix */ struct callout ks_timer; #ifdef EVDEV_SUPPORT struct evdev_dev *ks_evdev; int ks_evdev_state; #endif } atkbd_state_t; static void atkbd_timeout(void *arg); static void atkbd_shutdown_final(void *v); static int atkbd_reset(KBDC kbdc, int flags, int c); #define HAS_QUIRK(p, q) (((atkbdc_softc_t *)(p))->quirks & q) #define ALLOW_DISABLE_KBD(kbdc) !HAS_QUIRK(kbdc, KBDC_QUIRK_KEEP_ACTIVATED) #define DEFAULT_DELAY 0x1 /* 500ms */ #define DEFAULT_RATE 0x10 /* 14Hz */ #ifdef EVDEV_SUPPORT #define PS2_KEYBOARD_VENDOR 1 #define PS2_KEYBOARD_PRODUCT 1 #endif int atkbd_probe_unit(device_t dev, int irq, int flags) { keyboard_switch_t *sw; int args[2]; int error; sw = kbd_get_switch(ATKBD_DRIVER_NAME); if (sw == NULL) return ENXIO; args[0] = device_get_unit(device_get_parent(dev)); args[1] = irq; error = (*sw->probe)(device_get_unit(dev), args, flags); if (error) return error; return 0; } int atkbd_attach_unit(device_t dev, keyboard_t **kbd, int irq, int flags) { keyboard_switch_t *sw; atkbd_state_t *state; int args[2]; int error; int unit; sw = kbd_get_switch(ATKBD_DRIVER_NAME); if (sw == NULL) return ENXIO; /* reset, initialize and enable the device */ unit = device_get_unit(dev); args[0] = device_get_unit(device_get_parent(dev)); args[1] = irq; *kbd = NULL; error = (*sw->probe)(unit, args, flags); if (error) return error; error = (*sw->init)(unit, kbd, args, flags); if (error) return error; (*sw->enable)(*kbd); #ifdef KBD_INSTALL_CDEV /* attach a virtual keyboard cdev */ error = kbd_attach(*kbd); if (error) return error; #endif /* * This is a kludge to compensate for lost keyboard interrupts. * A similar code used to be in syscons. See below. XXX */ state = (atkbd_state_t *)(*kbd)->kb_data; callout_init(&state->ks_timer, 0); atkbd_timeout(*kbd); if (bootverbose) (*sw->diag)(*kbd, bootverbose); EVENTHANDLER_REGISTER(shutdown_final, atkbd_shutdown_final, *kbd, SHUTDOWN_PRI_DEFAULT); return 0; } static void atkbd_timeout(void *arg) { atkbd_state_t *state; keyboard_t *kbd; int s; /* * The original text of the following comments are extracted * from syscons.c (1.287) * * With release 2.1 of the Xaccel server, the keyboard is left * hanging pretty often. Apparently an interrupt from the * keyboard is lost, and I don't know why (yet). * This ugly hack calls the low-level interrupt routine if input * is ready for the keyboard and conveniently hides the problem. XXX * * Try removing anything stuck in the keyboard controller; whether * it's a keyboard scan code or mouse data. The low-level * interrupt routine doesn't read the mouse data directly, * but the keyboard controller driver will, as a side effect. */ /* * And here is bde's original comment about this: * * This is necessary to handle edge triggered interrupts - if we * returned when our IRQ is high due to unserviced input, then there * would be no more keyboard IRQs until the keyboard is reset by * external powers. * * The keyboard apparently unwedges the irq in most cases. */ s = spltty(); kbd = (keyboard_t *)arg; if (kbdd_lock(kbd, TRUE)) { /* * We have seen the lock flag is not set. Let's reset * the flag early, otherwise the LED update routine fails * which may want the lock during the interrupt routine. */ kbdd_lock(kbd, FALSE); if (kbdd_check_char(kbd)) kbdd_intr(kbd, NULL); } splx(s); state = (atkbd_state_t *)kbd->kb_data; callout_reset(&state->ks_timer, hz / 10, atkbd_timeout, arg); } /* LOW-LEVEL */ #define ATKBD_DEFAULT 0 /* keyboard driver declaration */ static int atkbd_configure(int flags); static kbd_probe_t atkbd_probe; static kbd_init_t atkbd_init; static kbd_term_t atkbd_term; static kbd_intr_t atkbd_intr; static kbd_test_if_t atkbd_test_if; static kbd_enable_t atkbd_enable; static kbd_disable_t atkbd_disable; static kbd_read_t atkbd_read; static kbd_check_t atkbd_check; static kbd_read_char_t atkbd_read_char; static kbd_check_char_t atkbd_check_char; static kbd_ioctl_t atkbd_ioctl; static kbd_lock_t atkbd_lock; static kbd_clear_state_t atkbd_clear_state; static kbd_get_state_t atkbd_get_state; static kbd_set_state_t atkbd_set_state; static kbd_poll_mode_t atkbd_poll; static keyboard_switch_t atkbdsw = { - atkbd_probe, - atkbd_init, - atkbd_term, - atkbd_intr, - atkbd_test_if, - atkbd_enable, - atkbd_disable, - atkbd_read, - atkbd_check, - atkbd_read_char, - atkbd_check_char, - atkbd_ioctl, - atkbd_lock, - atkbd_clear_state, - atkbd_get_state, - atkbd_set_state, - genkbd_get_fkeystr, - atkbd_poll, - genkbd_diag, + .probe = atkbd_probe, + .init = atkbd_init, + .term = atkbd_term, + .intr = atkbd_intr, + .test_if = atkbd_test_if, + .enable = atkbd_enable, + .disable = atkbd_disable, + .read = atkbd_read, + .check = atkbd_check, + .read_char = atkbd_read_char, + .check_char = atkbd_check_char, + .ioctl = atkbd_ioctl, + .lock = atkbd_lock, + .clear_state = atkbd_clear_state, + .get_state = atkbd_get_state, + .set_state = atkbd_set_state, + .get_fkeystr = genkbd_get_fkeystr, + .poll = atkbd_poll, + .diag = genkbd_diag, }; KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure); /* local functions */ static int set_typematic(keyboard_t *kbd); static int setup_kbd_port(KBDC kbdc, int port, int intr); static int get_kbd_echo(KBDC kbdc); static int probe_keyboard(KBDC kbdc, int flags); static int init_keyboard(KBDC kbdc, int *type, int flags); static int write_kbd(KBDC kbdc, int command, int data); static int get_kbd_id(KBDC kbdc); static int typematic(int delay, int rate); static int typematic_delay(int delay); static int typematic_rate(int rate); #ifdef EVDEV_SUPPORT static const struct evdev_methods atkbd_evdev_methods = { .ev_event = evdev_ev_kbd_event, }; #endif /* local variables */ /* the initial key map, accent map and fkey strings */ #ifdef ATKBD_DFLT_KEYMAP #define KBD_DFLT_KEYMAP #include "atkbdmap.h" #endif #include /* structures for the default keyboard */ static keyboard_t default_kbd; static atkbd_state_t default_kbd_state; static keymap_t default_keymap; static accentmap_t default_accentmap; static fkeytab_t default_fkeytab[NUM_FKEYS]; /* * The back door to the keyboard driver! * This function is called by the console driver, via the kbdio module, * to tickle keyboard drivers when the low-level console is being initialized. * Almost nothing in the kernel has been initialied yet. Try to probe * keyboards if possible. * NOTE: because of the way the low-level console is initialized, this routine * may be called more than once!! */ static int atkbd_configure(int flags) { keyboard_t *kbd; int arg[2]; int i; /* * Probe the keyboard controller, if not present or if the driver * is disabled, unregister the keyboard if any. */ if (atkbdc_configure() != 0 || resource_disabled("atkbd", ATKBD_DEFAULT)) { i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT); if (i >= 0) { kbd = kbd_get_keyboard(i); kbd_unregister(kbd); kbd->kb_flags &= ~KB_REGISTERED; } return 0; } /* XXX: a kludge to obtain the device configuration flags */ if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0) flags |= i; /* probe the default keyboard */ arg[0] = -1; arg[1] = -1; kbd = NULL; if (atkbd_probe(ATKBD_DEFAULT, arg, flags)) return 0; if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags)) return 0; /* return the number of found keyboards */ return 1; } /* low-level functions */ /* detect a keyboard */ static int atkbd_probe(int unit, void *arg, int flags) { KBDC kbdc; int *data = (int *)arg; /* data[0]: controller, data[1]: irq */ /* XXX */ if (unit == ATKBD_DEFAULT) { if (KBD_IS_PROBED(&default_kbd)) return 0; } kbdc = atkbdc_open(data[0]); if (kbdc == NULL) return ENXIO; if (probe_keyboard(kbdc, flags)) { if (flags & KB_CONF_FAIL_IF_NO_KBD) return ENXIO; } return 0; } /* reset and initialize the device */ static int atkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) { keyboard_t *kbd; atkbd_state_t *state; keymap_t *keymap; accentmap_t *accmap; fkeytab_t *fkeymap; int fkeymap_size; int delay[2]; int *data = (int *)arg; /* data[0]: controller, data[1]: irq */ int error, needfree; #ifdef EVDEV_SUPPORT struct evdev_dev *evdev; char phys_loc[8]; #endif /* XXX */ if (unit == ATKBD_DEFAULT) { *kbdp = kbd = &default_kbd; if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd)) return 0; state = &default_kbd_state; keymap = &default_keymap; accmap = &default_accentmap; fkeymap = default_fkeytab; fkeymap_size = nitems(default_fkeytab); needfree = 0; } else if (*kbdp == NULL) { *kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_NOWAIT | M_ZERO); state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT | M_ZERO); /* NB: these will always be initialized 'cuz !KBD_IS_PROBED */ keymap = malloc(sizeof(key_map), M_DEVBUF, M_NOWAIT); accmap = malloc(sizeof(accent_map), M_DEVBUF, M_NOWAIT); fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_NOWAIT); fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]); needfree = 1; if ((kbd == NULL) || (state == NULL) || (keymap == NULL) || (accmap == NULL) || (fkeymap == NULL)) { error = ENOMEM; goto bad; } } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) { return 0; } else { kbd = *kbdp; state = (atkbd_state_t *)kbd->kb_data; bzero(state, sizeof(*state)); keymap = kbd->kb_keymap; accmap = kbd->kb_accentmap; fkeymap = kbd->kb_fkeytab; fkeymap_size = kbd->kb_fkeytab_size; needfree = 0; } if (!KBD_IS_PROBED(kbd)) { state->kbdc = atkbdc_open(data[0]); if (state->kbdc == NULL) { error = ENXIO; goto bad; } kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags, 0, 0); bcopy(&key_map, keymap, sizeof(key_map)); bcopy(&accent_map, accmap, sizeof(accent_map)); bcopy(fkey_tab, fkeymap, imin(fkeymap_size * sizeof(fkeymap[0]), sizeof(fkey_tab))); kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size); kbd->kb_data = (void *)state; if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */ if (flags & KB_CONF_FAIL_IF_NO_KBD) { error = ENXIO; goto bad; } } else { KBD_FOUND_DEVICE(kbd); } atkbd_clear_state(kbd); state->ks_mode = K_XLATE; /* * FIXME: set the initial value for lock keys in ks_state * according to the BIOS data? */ KBD_PROBE_DONE(kbd); } if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) { kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY; if (KBD_HAS_DEVICE(kbd) && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config) && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD)) { kbd_unregister(kbd); error = ENXIO; goto bad; } atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state); set_typematic(kbd); delay[0] = kbd->kb_delay1; delay[1] = kbd->kb_delay2; atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); #ifdef EVDEV_SUPPORT /* register as evdev provider on first init */ if (state->ks_evdev == NULL) { snprintf(phys_loc, sizeof(phys_loc), "atkbd%d", unit); evdev = evdev_alloc(); evdev_set_name(evdev, "AT keyboard"); evdev_set_phys(evdev, phys_loc); evdev_set_id(evdev, BUS_I8042, PS2_KEYBOARD_VENDOR, PS2_KEYBOARD_PRODUCT, 0); evdev_set_methods(evdev, kbd, &atkbd_evdev_methods); evdev_support_event(evdev, EV_SYN); evdev_support_event(evdev, EV_KEY); evdev_support_event(evdev, EV_LED); evdev_support_event(evdev, EV_REP); evdev_support_all_known_keys(evdev); evdev_support_led(evdev, LED_NUML); evdev_support_led(evdev, LED_CAPSL); evdev_support_led(evdev, LED_SCROLLL); if (evdev_register(evdev)) evdev_free(evdev); else state->ks_evdev = evdev; state->ks_evdev_state = 0; } #endif KBD_INIT_DONE(kbd); } if (!KBD_IS_CONFIGURED(kbd)) { if (kbd_register(kbd) < 0) { error = ENXIO; goto bad; } KBD_CONFIG_DONE(kbd); } return 0; bad: if (needfree) { if (state != NULL) free(state, M_DEVBUF); if (keymap != NULL) free(keymap, M_DEVBUF); if (accmap != NULL) free(accmap, M_DEVBUF); if (fkeymap != NULL) free(fkeymap, M_DEVBUF); if (kbd != NULL) { free(kbd, M_DEVBUF); *kbdp = NULL; /* insure ref doesn't leak to caller */ } } return error; } /* finish using this keyboard */ static int atkbd_term(keyboard_t *kbd) { atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data; kbd_unregister(kbd); callout_drain(&state->ks_timer); return 0; } /* keyboard interrupt routine */ static int atkbd_intr(keyboard_t *kbd, void *arg) { atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data; int delay[2]; int c; if (!KBD_HAS_DEVICE(kbd)) { /* * The keyboard was not detected before; * it must have been reconnected! */ init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config); KBD_FOUND_DEVICE(kbd); atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state); set_typematic(kbd); delay[0] = kbd->kb_delay1; delay[1] = kbd->kb_delay2; atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); } if (state->ks_polling) return 0; if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) { /* let the callback function to process the input */ (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT, kbd->kb_callback.kc_arg); } else { /* read and discard the input; no one is waiting for input */ do { c = atkbd_read_char(kbd, FALSE); } while (c != NOKEY); } return 0; } /* test the interface to the device */ static int atkbd_test_if(keyboard_t *kbd) { int error; int s; error = 0; empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10); s = spltty(); if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc)) error = EIO; else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0) error = EIO; splx(s); return error; } /* * Enable the access to the device; until this function is called, * the client cannot read from the keyboard. */ static int atkbd_enable(keyboard_t *kbd) { int s; s = spltty(); KBD_ACTIVATE(kbd); splx(s); return 0; } /* disallow the access to the device */ static int atkbd_disable(keyboard_t *kbd) { int s; s = spltty(); KBD_DEACTIVATE(kbd); splx(s); return 0; } /* read one byte from the keyboard if it's allowed */ static int atkbd_read(keyboard_t *kbd, int wait) { int c; if (wait) c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc); else c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc); if (c != -1) ++kbd->kb_count; return (KBD_IS_ACTIVE(kbd) ? c : -1); } /* check if data is waiting */ static int atkbd_check(keyboard_t *kbd) { if (!KBD_IS_ACTIVE(kbd)) return FALSE; return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc); } /* read char from the keyboard */ static u_int atkbd_read_char(keyboard_t *kbd, int wait) { atkbd_state_t *state; u_int action; int scancode; int keycode; state = (atkbd_state_t *)kbd->kb_data; next_code: /* do we have a composed char to return? */ if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) { action = state->ks_composed_char; state->ks_composed_char = 0; if (action > UCHAR_MAX) return ERRKEY; return action; } /* see if there is something in the keyboard port */ if (wait) { do { scancode = read_kbd_data(state->kbdc); } while (scancode == -1); } else { scancode = read_kbd_data_no_wait(state->kbdc); if (scancode == -1) return NOKEY; } ++kbd->kb_count; #if KBDIO_DEBUG >= 10 printf("atkbd_read_char(): scancode:0x%x\n", scancode); #endif #ifdef EVDEV_SUPPORT /* push evdev event */ if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && state->ks_evdev != NULL) { keycode = evdev_scancode2key(&state->ks_evdev_state, scancode); if (keycode != KEY_RESERVED) { evdev_push_event(state->ks_evdev, EV_KEY, (uint16_t)keycode, scancode & 0x80 ? 0 : 1); evdev_sync(state->ks_evdev); } } #endif /* return the byte as is for the K_RAW mode */ if (state->ks_mode == K_RAW) return scancode; /* translate the scan code into a keycode */ keycode = scancode & 0x7F; switch (state->ks_prefix) { case 0x00: /* normal scancode */ switch(scancode) { case 0xB8: /* left alt (compose key) released */ if (state->ks_flags & COMPOSE) { state->ks_flags &= ~COMPOSE; if (state->ks_composed_char > UCHAR_MAX) state->ks_composed_char = 0; } break; case 0x38: /* left alt (compose key) pressed */ if (!(state->ks_flags & COMPOSE)) { state->ks_flags |= COMPOSE; state->ks_composed_char = 0; } break; case 0xE0: case 0xE1: state->ks_prefix = scancode; goto next_code; } break; case 0xE0: /* 0xE0 prefix */ state->ks_prefix = 0; switch (keycode) { case 0x1C: /* right enter key */ keycode = 0x59; break; case 0x1D: /* right ctrl key */ keycode = 0x5A; break; case 0x35: /* keypad divide key */ keycode = 0x5B; break; case 0x37: /* print scrn key */ keycode = 0x5C; break; case 0x38: /* right alt key (alt gr) */ keycode = 0x5D; break; case 0x46: /* ctrl-pause/break on AT 101 (see below) */ keycode = 0x68; break; case 0x47: /* grey home key */ keycode = 0x5E; break; case 0x48: /* grey up arrow key */ keycode = 0x5F; break; case 0x49: /* grey page up key */ keycode = 0x60; break; case 0x4B: /* grey left arrow key */ keycode = 0x61; break; case 0x4D: /* grey right arrow key */ keycode = 0x62; break; case 0x4F: /* grey end key */ keycode = 0x63; break; case 0x50: /* grey down arrow key */ keycode = 0x64; break; case 0x51: /* grey page down key */ keycode = 0x65; break; case 0x52: /* grey insert key */ keycode = 0x66; break; case 0x53: /* grey delete key */ keycode = 0x67; break; /* the following 3 are only used on the MS "Natural" keyboard */ case 0x5b: /* left Window key */ keycode = 0x69; break; case 0x5c: /* right Window key */ keycode = 0x6a; break; case 0x5d: /* menu key */ keycode = 0x6b; break; case 0x5e: /* power key */ keycode = 0x6d; break; case 0x5f: /* sleep key */ keycode = 0x6e; break; case 0x63: /* wake key */ keycode = 0x6f; break; default: /* ignore everything else */ goto next_code; } break; case 0xE1: /* 0xE1 prefix */ /* * The pause/break key on the 101 keyboard produces: * E1-1D-45 E1-9D-C5 * Ctrl-pause/break produces: * E0-46 E0-C6 (See above.) */ state->ks_prefix = 0; if (keycode == 0x1D) state->ks_prefix = 0x1D; goto next_code; /* NOT REACHED */ case 0x1D: /* pause / break */ state->ks_prefix = 0; if (keycode != 0x45) goto next_code; keycode = 0x68; break; } if (kbd->kb_type == KB_84) { switch (keycode) { case 0x37: /* *(numpad)/print screen */ if (state->ks_flags & SHIFTS) keycode = 0x5c; /* print screen */ break; case 0x45: /* num lock/pause */ if (state->ks_flags & CTLS) keycode = 0x68; /* pause */ break; case 0x46: /* scroll lock/break */ if (state->ks_flags & CTLS) keycode = 0x6c; /* break */ break; } } else if (kbd->kb_type == KB_101) { switch (keycode) { case 0x5c: /* print screen */ if (state->ks_flags & ALTS) keycode = 0x54; /* sysrq */ break; case 0x68: /* pause/break */ if (state->ks_flags & CTLS) keycode = 0x6c; /* break */ break; } } /* return the key code in the K_CODE mode */ if (state->ks_mode == K_CODE) return (keycode | (scancode & 0x80)); /* compose a character code */ if (state->ks_flags & COMPOSE) { switch (keycode | (scancode & 0x80)) { /* key pressed, process it */ case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */ state->ks_composed_char *= 10; state->ks_composed_char += keycode - 0x40; if (state->ks_composed_char > UCHAR_MAX) return ERRKEY; goto next_code; case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */ state->ks_composed_char *= 10; state->ks_composed_char += keycode - 0x47; if (state->ks_composed_char > UCHAR_MAX) return ERRKEY; goto next_code; case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */ state->ks_composed_char *= 10; state->ks_composed_char += keycode - 0x4E; if (state->ks_composed_char > UCHAR_MAX) return ERRKEY; goto next_code; case 0x52: /* keypad 0 */ state->ks_composed_char *= 10; if (state->ks_composed_char > UCHAR_MAX) return ERRKEY; goto next_code; /* key released, no interest here */ case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */ case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */ case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */ case 0xD2: /* keypad 0 */ goto next_code; case 0x38: /* left alt key */ break; default: if (state->ks_composed_char > 0) { state->ks_flags &= ~COMPOSE; state->ks_composed_char = 0; return ERRKEY; } break; } } /* keycode to key action */ action = genkbd_keyaction(kbd, keycode, scancode & 0x80, &state->ks_state, &state->ks_accents); if (action == NOKEY) goto next_code; else return action; } /* check if char is waiting */ static int atkbd_check_char(keyboard_t *kbd) { atkbd_state_t *state; if (!KBD_IS_ACTIVE(kbd)) return FALSE; state = (atkbd_state_t *)kbd->kb_data; if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) return TRUE; return kbdc_data_ready(state->kbdc); } /* some useful control functions */ static int atkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) { /* translate LED_XXX bits into the device specific bits */ static u_char ledmap[8] = { 0, 4, 2, 6, 1, 5, 3, 7, }; atkbd_state_t *state = kbd->kb_data; int error; int s; int i; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) int ival; #endif s = spltty(); switch (cmd) { case KDGKBMODE: /* get keyboard mode */ *(int *)arg = state->ks_mode; break; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 7): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSKBMODE: /* set keyboard mode */ switch (*(int *)arg) { case K_XLATE: if (state->ks_mode != K_XLATE) { /* make lock key state and LED state match */ state->ks_state &= ~LOCK_MASK; state->ks_state |= KBD_LED_VAL(kbd); } /* FALLTHROUGH */ case K_RAW: case K_CODE: if (state->ks_mode != *(int *)arg) { atkbd_clear_state(kbd); state->ks_mode = *(int *)arg; } break; default: splx(s); return EINVAL; } break; case KDGETLED: /* get keyboard LED */ *(int *)arg = KBD_LED_VAL(kbd); break; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 66): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSETLED: /* set keyboard LED */ /* NOTE: lock key state in ks_state won't be changed */ if (*(int *)arg & ~LOCK_MASK) { splx(s); return EINVAL; } i = *(int *)arg; /* replace CAPS LED with ALTGR LED for ALTGR keyboards */ if (state->ks_mode == K_XLATE && kbd->kb_keymap->n_keys > ALTGR_OFFSET) { if (i & ALKED) i |= CLKED; else i &= ~CLKED; } if (KBD_HAS_DEVICE(kbd)) { error = write_kbd(state->kbdc, KBDC_SET_LEDS, ledmap[i & LED_MASK]); if (error) { splx(s); return error; } } #ifdef EVDEV_SUPPORT /* push LED states to evdev */ if (state->ks_evdev != NULL && evdev_rcpt_mask & EVDEV_RCPT_HW_KBD) evdev_push_leds(state->ks_evdev, *(int *)arg); #endif KBD_LED_VAL(kbd) = *(int *)arg; break; case KDGKBSTATE: /* get lock key state */ *(int *)arg = state->ks_state & LOCK_MASK; break; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 20): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSKBSTATE: /* set lock key state */ if (*(int *)arg & ~LOCK_MASK) { splx(s); return EINVAL; } state->ks_state &= ~LOCK_MASK; state->ks_state |= *(int *)arg; splx(s); /* set LEDs and quit */ return atkbd_ioctl(kbd, KDSETLED, arg); case KDSETREPEAT: /* set keyboard repeat rate (new interface) */ splx(s); if (!KBD_HAS_DEVICE(kbd)) return 0; i = typematic(((int *)arg)[0], ((int *)arg)[1]); error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i); if (error == 0) { kbd->kb_delay1 = typematic_delay(i); kbd->kb_delay2 = typematic_rate(i); #ifdef EVDEV_SUPPORT if (state->ks_evdev != NULL && evdev_rcpt_mask & EVDEV_RCPT_HW_KBD) evdev_push_repeats(state->ks_evdev, kbd); #endif } return error; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 67): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSETRAD: /* set keyboard repeat rate (old interface) */ splx(s); if (!KBD_HAS_DEVICE(kbd)) return 0; error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg); if (error == 0) { kbd->kb_delay1 = typematic_delay(*(int *)arg); kbd->kb_delay2 = typematic_rate(*(int *)arg); #ifdef EVDEV_SUPPORT if (state->ks_evdev != NULL && evdev_rcpt_mask & EVDEV_RCPT_HW_KBD) evdev_push_repeats(state->ks_evdev, kbd); #endif } return error; case PIO_KEYMAP: /* set keyboard translation table */ case OPIO_KEYMAP: /* set keyboard translation table (compat) */ case PIO_KEYMAPENT: /* set keyboard translation table entry */ case PIO_DEADKEYMAP: /* set accent key translation table */ state->ks_accents = 0; /* FALLTHROUGH */ default: splx(s); return genkbd_commonioctl(kbd, cmd, arg); } splx(s); return 0; } /* lock the access to the keyboard */ static int atkbd_lock(keyboard_t *kbd, int lock) { return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock); } /* clear the internal state of the keyboard */ static void atkbd_clear_state(keyboard_t *kbd) { atkbd_state_t *state; state = (atkbd_state_t *)kbd->kb_data; state->ks_flags = 0; state->ks_polling = 0; state->ks_state &= LOCK_MASK; /* preserve locking key state */ state->ks_accents = 0; state->ks_composed_char = 0; #if 0 state->ks_prefix = 0; /* XXX */ #endif } /* save the internal state */ static int atkbd_get_state(keyboard_t *kbd, void *buf, size_t len) { if (len == 0) return sizeof(atkbd_state_t); if (len < sizeof(atkbd_state_t)) return -1; bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t)); return 0; } /* set the internal state */ static int atkbd_set_state(keyboard_t *kbd, void *buf, size_t len) { if (len < sizeof(atkbd_state_t)) return ENOMEM; if (((atkbd_state_t *)kbd->kb_data)->kbdc != ((atkbd_state_t *)buf)->kbdc) return ENOMEM; bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t)); return 0; } static int atkbd_poll(keyboard_t *kbd, int on) { atkbd_state_t *state; int s; state = (atkbd_state_t *)kbd->kb_data; s = spltty(); if (on) ++state->ks_polling; else --state->ks_polling; splx(s); return 0; } static void atkbd_shutdown_final(void *v) { #ifdef __sparc64__ keyboard_t *kbd = v; KBDC kbdc = ((atkbd_state_t *)kbd->kb_data)->kbdc; /* * Turn off the translation in preparation for handing the keyboard * over to the OFW as the OBP driver doesn't use translation and * also doesn't disable it itself resulting in a broken keymap at * the boot prompt. Also disable the aux port and the interrupts as * the OBP driver doesn't use them, i.e. polls the keyboard. Not * disabling the interrupts doesn't cause real problems but the * responsiveness is a bit better when they are turned off. */ send_kbd_command(kbdc, KBDC_DISABLE_KBD); set_controller_command_byte(kbdc, KBD_AUX_CONTROL_BITS | KBD_KBD_CONTROL_BITS | KBD_TRANSLATION, KBD_DISABLE_AUX_PORT | KBD_DISABLE_KBD_INT | KBD_ENABLE_KBD_PORT); send_kbd_command(kbdc, KBDC_ENABLE_KBD); #endif } static int atkbd_reset(KBDC kbdc, int flags, int c) { /* reset keyboard hardware */ if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) { /* * KEYBOARD ERROR * Keyboard reset may fail either because the keyboard * doen't exist, or because the keyboard doesn't pass * the self-test, or the keyboard controller on the * motherboard and the keyboard somehow fail to shake hands. * It is just possible, particularly in the last case, * that the keyboard controller may be left in a hung state. * test_controller() and test_kbd_port() appear to bring * the keyboard controller back (I don't know why and how, * though.) */ empty_both_buffers(kbdc, 10); test_controller(kbdc); test_kbd_port(kbdc); /* * We could disable the keyboard port and interrupt... but, * the keyboard may still exist (see above). */ set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c); if (bootverbose) printf("atkbd: failed to reset the keyboard.\n"); return (EIO); } return (0); } /* local functions */ static int set_typematic(keyboard_t *kbd) { int val, error; atkbd_state_t *state = kbd->kb_data; val = typematic(DEFAULT_DELAY, DEFAULT_RATE); error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, val); if (error == 0) { kbd->kb_delay1 = typematic_delay(val); kbd->kb_delay2 = typematic_rate(val); } return (error); } static int setup_kbd_port(KBDC kbdc, int port, int intr) { if (!set_controller_command_byte(kbdc, KBD_KBD_CONTROL_BITS, ((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT) | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT))) return 1; return 0; } static int get_kbd_echo(KBDC kbdc) { /* enable the keyboard port, but disable the keyboard intr. */ if (setup_kbd_port(kbdc, TRUE, FALSE)) /* CONTROLLER ERROR: there is very little we can do... */ return ENXIO; /* see if something is present */ write_kbd_command(kbdc, KBDC_ECHO); if (read_kbd_data(kbdc) != KBD_ECHO) { empty_both_buffers(kbdc, 10); test_controller(kbdc); test_kbd_port(kbdc); return ENXIO; } /* enable the keyboard port and intr. */ if (setup_kbd_port(kbdc, TRUE, TRUE)) { /* * CONTROLLER ERROR * This is serious; the keyboard intr is left disabled! */ return ENXIO; } return 0; } static int probe_keyboard(KBDC kbdc, int flags) { /* * Don't try to print anything in this function. The low-level * console may not have been initialized yet... */ int err; int c; int m; if (!kbdc_lock(kbdc, TRUE)) { /* driver error? */ return ENXIO; } /* temporarily block data transmission from the keyboard */ write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT); /* flush any noise in the buffer */ empty_both_buffers(kbdc, 100); /* save the current keyboard controller command byte */ m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS; c = get_controller_command_byte(kbdc); if (c == -1) { /* CONTROLLER ERROR */ kbdc_set_device_mask(kbdc, m); kbdc_lock(kbdc, FALSE); return ENXIO; } /* * The keyboard may have been screwed up by the boot block. * We may just be able to recover from error by testing the controller * and the keyboard port. The controller command byte needs to be * saved before this recovery operation, as some controllers seem * to set the command byte to particular values. */ test_controller(kbdc); if (!(flags & KB_CONF_NO_PROBE_TEST)) test_kbd_port(kbdc); err = get_kbd_echo(kbdc); /* * Even if the keyboard doesn't seem to be present (err != 0), * we shall enable the keyboard port and interrupt so that * the driver will be operable when the keyboard is attached * to the system later. It is NOT recommended to hot-plug * the AT keyboard, but many people do so... */ kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS); setup_kbd_port(kbdc, TRUE, TRUE); #if 0 if (err == 0) { kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS); } else { /* try to restore the command byte as before */ set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c); kbdc_set_device_mask(kbdc, m); } #endif kbdc_lock(kbdc, FALSE); return (HAS_QUIRK(kbdc, KBDC_QUIRK_IGNORE_PROBE_RESULT) ? 0 : err); } static int init_keyboard(KBDC kbdc, int *type, int flags) { int codeset; int id; int c; if (!kbdc_lock(kbdc, TRUE)) { /* driver error? */ return EIO; } /* temporarily block data transmission from the keyboard */ write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT); /* save the current controller command byte */ empty_both_buffers(kbdc, 200); c = get_controller_command_byte(kbdc); if (c == -1) { /* CONTROLLER ERROR */ kbdc_lock(kbdc, FALSE); printf("atkbd: unable to get the current command byte value.\n"); return EIO; } if (bootverbose) printf("atkbd: the current kbd controller command byte %04x\n", c); #if 0 /* override the keyboard lock switch */ c |= KBD_OVERRIDE_KBD_LOCK; #endif /* enable the keyboard port, but disable the keyboard intr. */ if (setup_kbd_port(kbdc, TRUE, FALSE)) { /* CONTROLLER ERROR: there is very little we can do... */ printf("atkbd: unable to set the command byte.\n"); kbdc_lock(kbdc, FALSE); return EIO; } if (HAS_QUIRK(kbdc, KBDC_QUIRK_RESET_AFTER_PROBE) && atkbd_reset(kbdc, flags, c)) { kbdc_lock(kbdc, FALSE); return EIO; } /* * Check if we have an XT keyboard before we attempt to reset it. * The procedure assumes that the keyboard and the controller have * been set up properly by BIOS and have not been messed up * during the boot process. */ codeset = -1; if (flags & KB_CONF_ALT_SCANCODESET) /* the user says there is a XT keyboard */ codeset = 1; #ifdef KBD_DETECT_XT_KEYBOARD else if ((c & KBD_TRANSLATION) == 0) { /* SET_SCANCODE_SET is not always supported; ignore error */ if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0) == KBD_ACK) codeset = read_kbd_data(kbdc); } if (bootverbose) printf("atkbd: scancode set %d\n", codeset); #endif /* KBD_DETECT_XT_KEYBOARD */ *type = KB_OTHER; id = get_kbd_id(kbdc); switch(id) { case 0x41ab: /* 101/102/... Enhanced */ case 0x83ab: /* ditto */ case 0x54ab: /* SpaceSaver */ case 0x84ab: /* ditto */ #if 0 case 0x90ab: /* 'G' */ case 0x91ab: /* 'P' */ case 0x92ab: /* 'A' */ #endif *type = KB_101; break; case -1: /* AT 84 keyboard doesn't return ID */ *type = KB_84; break; default: break; } if (bootverbose) printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type); if (!HAS_QUIRK(kbdc, KBDC_QUIRK_RESET_AFTER_PROBE) && atkbd_reset(kbdc, flags, c)) { kbdc_lock(kbdc, FALSE); return EIO; } /* * Allow us to set the XT_KEYBD flag so that keyboards * such as those on the IBM ThinkPad laptop computers can be used * with the standard console driver. */ if (codeset == 1) { if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) { /* XT kbd doesn't need scan code translation */ c &= ~KBD_TRANSLATION; } else { /* * KEYBOARD ERROR * The XT kbd isn't usable unless the proper scan * code set is selected. */ set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c); kbdc_lock(kbdc, FALSE); printf("atkbd: unable to set the XT keyboard mode.\n"); return EIO; } } #if defined(__sparc64__) if (send_kbd_command_and_data( kbdc, KBDC_SET_SCANCODE_SET, 2) != KBD_ACK) { printf("atkbd: can't set translation.\n"); } c |= KBD_TRANSLATION; #endif /* * Some keyboards require a SETLEDS command to be sent after * the reset command before they will send keystrokes to us */ if (HAS_QUIRK(kbdc, KBDC_QUIRK_SETLEDS_ON_INIT) && send_kbd_command_and_data(kbdc, KBDC_SET_LEDS, 0) != KBD_ACK) { printf("atkbd: setleds failed\n"); } if (!ALLOW_DISABLE_KBD(kbdc)) send_kbd_command(kbdc, KBDC_ENABLE_KBD); /* enable the keyboard port and intr. */ if (!set_controller_command_byte(kbdc, KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK, (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK)) | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) { /* * CONTROLLER ERROR * This is serious; we are left with the disabled * keyboard intr. */ set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc) ? 0xff : (KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK), c); kbdc_lock(kbdc, FALSE); printf("atkbd: unable to enable the keyboard port and intr.\n"); return EIO; } kbdc_lock(kbdc, FALSE); return 0; } static int write_kbd(KBDC kbdc, int command, int data) { int s; /* prevent the timeout routine from polling the keyboard */ if (!kbdc_lock(kbdc, TRUE)) return EBUSY; /* disable the keyboard and mouse interrupt */ s = spltty(); #if 0 c = get_controller_command_byte(kbdc); if ((c == -1) || !set_controller_command_byte(kbdc, kbdc_get_device_mask(kbdc), KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { /* CONTROLLER ERROR */ kbdc_lock(kbdc, FALSE); splx(s); return EIO; } /* * Now that the keyboard controller is told not to generate * the keyboard and mouse interrupts, call `splx()' to allow * the other tty interrupts. The clock interrupt may also occur, * but the timeout routine (`scrn_timer()') will be blocked * by the lock flag set via `kbdc_lock()' */ splx(s); #endif if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK) send_kbd_command(kbdc, KBDC_ENABLE_KBD); #if 0 /* restore the interrupts */ if (!set_controller_command_byte(kbdc, kbdc_get_device_mask(kbdc), c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) { /* CONTROLLER ERROR */ } #else splx(s); #endif kbdc_lock(kbdc, FALSE); return 0; } static int get_kbd_id(KBDC kbdc) { int id1, id2; empty_both_buffers(kbdc, 10); id1 = id2 = -1; if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK) return -1; DELAY(10000); /* 10 msec delay */ id1 = read_kbd_data(kbdc); if (id1 != -1) id2 = read_kbd_data(kbdc); if ((id1 == -1) || (id2 == -1)) { empty_both_buffers(kbdc, 10); test_controller(kbdc); test_kbd_port(kbdc); return -1; } return ((id2 << 8) | id1); } static int delays[] = { 250, 500, 750, 1000 }; static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63, 68, 76, 84, 92, 100, 110, 118, 126, 136, 152, 168, 184, 200, 220, 236, 252, 272, 304, 336, 368, 400, 440, 472, 504 }; static int typematic_delay(int i) { return delays[(i >> 5) & 3]; } static int typematic_rate(int i) { return rates[i & 0x1f]; } static int typematic(int delay, int rate) { int value; int i; for (i = nitems(delays) - 1; i > 0; --i) { if (delay >= delays[i]) break; } value = i << 5; for (i = nitems(rates) - 1; i > 0; --i) { if (rate >= rates[i]) break; } value |= i; return value; } Index: stable/11/sys/dev/hyperv/input/hv_kbd.c =================================================================== --- stable/11/sys/dev/hyperv/input/hv_kbd.c (revision 356006) +++ stable/11/sys/dev/hyperv/input/hv_kbd.c (revision 356007) @@ -1,564 +1,564 @@ /*- * Copyright (c) 2017 Microsoft Corp. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "dev/hyperv/input/hv_kbdc.h" #define HVKBD_MTX_LOCK(_m) do { \ mtx_lock(_m); \ } while (0) #define HVKBD_MTX_UNLOCK(_m) do { \ mtx_unlock(_m); \ } while (0) #define HVKBD_MTX_ASSERT(_m, _t) do { \ mtx_assert(_m, _t); \ } while (0) #define HVKBD_LOCK() HVKBD_MTX_LOCK(&Giant) #define HVKBD_UNLOCK() HVKBD_MTX_UNLOCK(&Giant) #define HVKBD_LOCK_ASSERT() HVKBD_MTX_ASSERT(&Giant, MA_OWNED) #define HVKBD_FLAG_POLLING 0x00000002 /* early keyboard probe, not supported */ static int hvkbd_configure(int flags) { return (0); } /* detect a keyboard, not used */ static int hvkbd_probe(int unit, void *arg, int flags) { return (ENXIO); } /* reset and initialize the device, not used */ static int hvkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) { DEBUG_HVKBD(*kbdp, "%s\n", __func__); return (ENXIO); } /* test the interface to the device, not used */ static int hvkbd_test_if(keyboard_t *kbd) { DEBUG_HVKBD(kbd, "%s\n", __func__); return (0); } /* finish using this keyboard, not used */ static int hvkbd_term(keyboard_t *kbd) { DEBUG_HVKBD(kbd, "%s\n", __func__); return (ENXIO); } /* keyboard interrupt routine, not used */ static int hvkbd_intr(keyboard_t *kbd, void *arg) { DEBUG_HVKBD(kbd, "%s\n", __func__); return (0); } /* lock the access to the keyboard, not used */ static int hvkbd_lock(keyboard_t *kbd, int lock) { DEBUG_HVKBD(kbd, "%s\n", __func__); return (1); } /* save the internal state, not used */ static int hvkbd_get_state(keyboard_t *kbd, void *buf, size_t len) { DEBUG_HVKBD(kbd,"%s\n", __func__); return (len == 0) ? 1 : -1; } /* set the internal state, not used */ static int hvkbd_set_state(keyboard_t *kbd, void *buf, size_t len) { DEBUG_HVKBD(kbd, "%s\n", __func__); return (EINVAL); } static int hvkbd_poll(keyboard_t *kbd, int on) { hv_kbd_sc *sc = kbd->kb_data; HVKBD_LOCK(); /* * Keep a reference count on polling to allow recursive * cngrab() during a panic for example. */ if (on) sc->sc_polling++; else if (sc->sc_polling > 0) sc->sc_polling--; if (sc->sc_polling != 0) { sc->sc_flags |= HVKBD_FLAG_POLLING; } else { sc->sc_flags &= ~HVKBD_FLAG_POLLING; } HVKBD_UNLOCK(); return (0); } /* * Enable the access to the device; until this function is called, * the client cannot read from the keyboard. */ static int hvkbd_enable(keyboard_t *kbd) { HVKBD_LOCK(); KBD_ACTIVATE(kbd); HVKBD_UNLOCK(); return (0); } /* disallow the access to the device */ static int hvkbd_disable(keyboard_t *kbd) { DEBUG_HVKBD(kbd, "%s\n", __func__); HVKBD_LOCK(); KBD_DEACTIVATE(kbd); HVKBD_UNLOCK(); return (0); } static void hvkbd_do_poll(hv_kbd_sc *sc, uint8_t wait) { while (!hv_kbd_prod_is_ready(sc)) { hv_kbd_read_channel(sc->hs_chan, sc); if (!wait) break; } } /* check if data is waiting */ /* Currently unused. */ static int hvkbd_check(keyboard_t *kbd) { DEBUG_HVKBD(kbd, "%s\n", __func__); return (0); } /* check if char is waiting */ static int hvkbd_check_char_locked(keyboard_t *kbd) { HVKBD_LOCK_ASSERT(); if (!KBD_IS_ACTIVE(kbd)) return (FALSE); hv_kbd_sc *sc = kbd->kb_data; if (sc->sc_flags & HVKBD_FLAG_POLLING) hvkbd_do_poll(sc, 0); if (hv_kbd_prod_is_ready(sc)) { return (TRUE); } return (FALSE); } static int hvkbd_check_char(keyboard_t *kbd) { int result; HVKBD_LOCK(); result = hvkbd_check_char_locked(kbd); HVKBD_UNLOCK(); return (result); } /* read char from the keyboard */ static uint32_t hvkbd_read_char_locked(keyboard_t *kbd, int wait) { uint32_t scancode = NOKEY; keystroke ks; hv_kbd_sc *sc = kbd->kb_data; HVKBD_LOCK_ASSERT(); if (!KBD_IS_ACTIVE(kbd) || !hv_kbd_prod_is_ready(sc)) return (NOKEY); if (sc->sc_mode == K_RAW) { if (hv_kbd_fetch_top(sc, &ks)) { return (NOKEY); } if ((ks.info & IS_E0) || (ks.info & IS_E1)) { /** * Emulate the generation of E0 or E1 scancode, * the real scancode will be consumed next time. */ if (ks.info & IS_E0) { scancode = XTKBD_EMUL0; ks.info &= ~IS_E0; } else if (ks.info & IS_E1) { scancode = XTKBD_EMUL1; ks.info &= ~IS_E1; } /** * Change the top item to avoid encountering * E0 or E1 twice. */ hv_kbd_modify_top(sc, &ks); } else if (ks.info & IS_UNICODE) { /** * XXX: Hyperv host send unicode to VM through * 'Type clipboard text', the mapping from * unicode to scancode depends on the keymap. * It is so complicated that we do not plan to * support it yet. */ if (bootverbose) device_printf(sc->dev, "Unsupported unicode\n"); hv_kbd_remove_top(sc); return (NOKEY); } else { scancode = ks.makecode; if (ks.info & IS_BREAK) { scancode |= XTKBD_RELEASE; } hv_kbd_remove_top(sc); } } else { if (bootverbose) device_printf(sc->dev, "Unsupported mode: %d\n", sc->sc_mode); } ++kbd->kb_count; DEBUG_HVKBD(kbd, "read scan: 0x%x\n", scancode); return scancode; } /* Currently wait is always false. */ static uint32_t hvkbd_read_char(keyboard_t *kbd, int wait) { uint32_t keycode; HVKBD_LOCK(); keycode = hvkbd_read_char_locked(kbd, wait); HVKBD_UNLOCK(); return (keycode); } /* clear the internal state of the keyboard */ static void hvkbd_clear_state(keyboard_t *kbd) { hv_kbd_sc *sc = kbd->kb_data; sc->sc_state &= LOCK_MASK; /* preserve locking key state */ sc->sc_flags &= ~HVKBD_FLAG_POLLING; } static int hvkbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg) { int i; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) int ival; #endif hv_kbd_sc *sc = kbd->kb_data; switch (cmd) { case KDGKBMODE: *(int *)arg = sc->sc_mode; break; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 7): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSKBMODE: /* set keyboard mode */ DEBUG_HVKBD(kbd, "expected mode: %x\n", *(int *)arg); switch (*(int *)arg) { case K_XLATE: if (sc->sc_mode != K_XLATE) { /* make lock key state and LED state match */ sc->sc_state &= ~LOCK_MASK; sc->sc_state |= KBD_LED_VAL(kbd); } /* FALLTHROUGH */ case K_RAW: case K_CODE: if (sc->sc_mode != *(int *)arg) { DEBUG_HVKBD(kbd, "mod changed to %x\n", *(int *)arg); if ((sc->sc_flags & HVKBD_FLAG_POLLING) == 0) hvkbd_clear_state(kbd); sc->sc_mode = *(int *)arg; } break; default: return (EINVAL); } break; case KDGKBSTATE: /* get lock key state */ *(int *)arg = sc->sc_state & LOCK_MASK; break; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 20): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSKBSTATE: /* set lock key state */ if (*(int *)arg & ~LOCK_MASK) { return (EINVAL); } sc->sc_state &= ~LOCK_MASK; sc->sc_state |= *(int *)arg; return hvkbd_ioctl_locked(kbd, KDSETLED, arg); case KDGETLED: /* get keyboard LED */ *(int *)arg = KBD_LED_VAL(kbd); break; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 66): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSETLED: /* set keyboard LED */ /* NOTE: lock key state in "sc_state" won't be changed */ if (*(int *)arg & ~LOCK_MASK) return (EINVAL); i = *(int *)arg; /* replace CAPS LED with ALTGR LED for ALTGR keyboards */ if (sc->sc_mode == K_XLATE && kbd->kb_keymap->n_keys > ALTGR_OFFSET) { if (i & ALKED) i |= CLKED; else i &= ~CLKED; } if (KBD_HAS_DEVICE(kbd)) { DEBUG_HVSC(sc, "setled 0x%x\n", *(int *)arg); } KBD_LED_VAL(kbd) = *(int *)arg; break; default: return (genkbd_commonioctl(kbd, cmd, arg)); } return (0); } /* some useful control functions */ static int hvkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) { DEBUG_HVKBD(kbd, "%s: %lx start\n", __func__, cmd); HVKBD_LOCK(); int ret = hvkbd_ioctl_locked(kbd, cmd, arg); HVKBD_UNLOCK(); DEBUG_HVKBD(kbd, "%s: %lx end %d\n", __func__, cmd, ret); return (ret); } /* read one byte from the keyboard if it's allowed */ /* Currently unused. */ static int hvkbd_read(keyboard_t *kbd, int wait) { DEBUG_HVKBD(kbd, "%s\n", __func__); HVKBD_LOCK_ASSERT(); if (!KBD_IS_ACTIVE(kbd)) return (-1); return hvkbd_read_char_locked(kbd, wait); } static keyboard_switch_t hvkbdsw = { - hvkbd_probe, /* not used */ - hvkbd_init, - hvkbd_term, /* not used */ - hvkbd_intr, /* not used */ - hvkbd_test_if, /* not used */ - hvkbd_enable, - hvkbd_disable, - hvkbd_read, - hvkbd_check, - hvkbd_read_char, - hvkbd_check_char, - hvkbd_ioctl, - hvkbd_lock, /* not used */ - hvkbd_clear_state, - hvkbd_get_state, /* not used */ - hvkbd_set_state, /* not used */ - genkbd_get_fkeystr, - hvkbd_poll, - genkbd_diag, + .probe = hvkbd_probe, /* not used */ + .init = hvkbd_init, + .term = hvkbd_term, /* not used */ + .intr = hvkbd_intr, /* not used */ + .test_if = hvkbd_test_if, /* not used */ + .enable = hvkbd_enable, + .disable = hvkbd_disable, + .read = hvkbd_read, + .check = hvkbd_check, + .read_char = hvkbd_read_char, + .check_char = hvkbd_check_char, + .ioctl = hvkbd_ioctl, + .lock = hvkbd_lock, /* not used */ + .clear_state = hvkbd_clear_state, + .get_state = hvkbd_get_state, /* not used */ + .set_state = hvkbd_set_state, /* not used */ + .get_fkeystr = genkbd_get_fkeystr, + .poll = hvkbd_poll, + .diag = genkbd_diag, }; KEYBOARD_DRIVER(hvkbd, hvkbdsw, hvkbd_configure); void hv_kbd_intr(hv_kbd_sc *sc) { uint32_t c; if ((sc->sc_flags & HVKBD_FLAG_POLLING) != 0) return; if (KBD_IS_ACTIVE(&sc->sc_kbd) && KBD_IS_BUSY(&sc->sc_kbd)) { /* let the callback function process the input */ (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT, sc->sc_kbd.kb_callback.kc_arg); } else { /* read and discard the input, no one is waiting for it */ do { c = hvkbd_read_char(&sc->sc_kbd, 0); } while (c != NOKEY); } } int hvkbd_driver_load(module_t mod, int what, void *arg) { switch (what) { case MOD_LOAD: kbd_add_driver(&hvkbd_kbd_driver); break; case MOD_UNLOAD: kbd_delete_driver(&hvkbd_kbd_driver); break; } return (0); } int hv_kbd_drv_attach(device_t dev) { hv_kbd_sc *sc = device_get_softc(dev); int unit = device_get_unit(dev); keyboard_t *kbd = &sc->sc_kbd; keyboard_switch_t *sw; sw = kbd_get_switch(HVKBD_DRIVER_NAME); if (sw == NULL) { return (ENXIO); } kbd_init_struct(kbd, HVKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0); kbd->kb_data = (void *)sc; kbd_set_maps(kbd, &key_map, &accent_map, fkey_tab, nitems(fkey_tab)); KBD_FOUND_DEVICE(kbd); hvkbd_clear_state(kbd); KBD_PROBE_DONE(kbd); KBD_INIT_DONE(kbd); sc->sc_mode = K_RAW; (*sw->enable)(kbd); if (kbd_register(kbd) < 0) { goto detach; } KBD_CONFIG_DONE(kbd); #ifdef KBD_INSTALL_CDEV if (kbd_attach(kbd)) { goto detach; } #endif if (bootverbose) { genkbd_diag(kbd, bootverbose); } return (0); detach: hv_kbd_drv_detach(dev); return (ENXIO); } int hv_kbd_drv_detach(device_t dev) { int error = 0; hv_kbd_sc *sc = device_get_softc(dev); hvkbd_disable(&sc->sc_kbd); if (KBD_IS_CONFIGURED(&sc->sc_kbd)) { error = kbd_unregister(&sc->sc_kbd); if (error) { device_printf(dev, "WARNING: kbd_unregister() " "returned non-zero! (ignored)\n"); } } #ifdef KBD_INSTALL_CDEV error = kbd_detach(&sc->sc_kbd); #endif return (error); } Index: stable/11/sys/dev/uart/uart_kbd_sun.c =================================================================== --- stable/11/sys/dev/uart/uart_kbd_sun.c (revision 356006) +++ stable/11/sys/dev/uart/uart_kbd_sun.c (revision 356007) @@ -1,869 +1,869 @@ /*- * Copyright (c) 2003 Jake Burkholder. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include "opt_compat.h" #include "opt_kbd.h" #include "opt_sunkbd.h" #if (defined(SUNKBD_EMULATE_ATKBD) && defined(SUNKBD_DFLT_KEYMAP)) || \ !defined(SUNKBD_EMULATE_ATKBD) #define KBD_DFLT_KEYMAP #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if !defined(SUNKBD_EMULATE_ATKBD) #include #endif #if defined(SUNKBD_EMULATE_ATKBD) && defined(SUNKBD_DFLT_KEYMAP) #include "sunkbdmap.h" #endif #include "uart_if.h" #define SUNKBD_DRIVER_NAME "sunkbd" #define TODO printf("%s: unimplemented", __func__) struct sunkbd_softc { keyboard_t sc_kbd; struct uart_softc *sc_uart; struct uart_devinfo *sc_sysdev; struct callout sc_repeat_callout; int sc_repeat_key; int sc_accents; int sc_composed_char; int sc_flags; #define KPCOMPOSE (1 << 0) int sc_mode; int sc_polling; int sc_repeating; int sc_state; #if defined(SUNKBD_EMULATE_ATKBD) int sc_buffered_char[2]; #endif }; static int sunkbd_configure(int flags); static int sunkbd_probe_keyboard(struct uart_devinfo *di); static int sunkbd_probe(int unit, void *arg, int flags); static int sunkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags); static int sunkbd_term(keyboard_t *kbd); static int sunkbd_intr(keyboard_t *kbd, void *arg); static int sunkbd_test_if(keyboard_t *kbd); static int sunkbd_enable(keyboard_t *kbd); static int sunkbd_disable(keyboard_t *kbd); static int sunkbd_read(keyboard_t *kbd, int wait); static int sunkbd_check(keyboard_t *kbd); static u_int sunkbd_read_char(keyboard_t *kbd, int wait); static int sunkbd_check_char(keyboard_t *kbd); static int sunkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t data); static int sunkbd_lock(keyboard_t *kbd, int lock); static void sunkbd_clear_state(keyboard_t *kbd); static int sunkbd_get_state(keyboard_t *kbd, void *buf, size_t len); static int sunkbd_set_state(keyboard_t *kbd, void *buf, size_t len); static int sunkbd_poll_mode(keyboard_t *kbd, int on); static void sunkbd_diag(keyboard_t *kbd, int level); static void sunkbd_repeat(void *v); #if defined(SUNKBD_EMULATE_ATKBD) static int keycode2scancode(int keycode, int shift, int up); #endif static keyboard_switch_t sunkbdsw = { - sunkbd_probe, - sunkbd_init, - sunkbd_term, - sunkbd_intr, - sunkbd_test_if, - sunkbd_enable, - sunkbd_disable, - sunkbd_read, - sunkbd_check, - sunkbd_read_char, - sunkbd_check_char, - sunkbd_ioctl, - sunkbd_lock, - sunkbd_clear_state, - sunkbd_get_state, - sunkbd_set_state, - genkbd_get_fkeystr, - sunkbd_poll_mode, - sunkbd_diag + .probe = sunkbd_probe, + .init = sunkbd_init, + .term = sunkbd_term, + .intr = sunkbd_intr, + .test_if = sunkbd_test_if, + .enable = sunkbd_enable, + .disable = sunkbd_disable, + .read = sunkbd_read, + .check = sunkbd_check, + .read_char = sunkbd_read_char, + .check_char = sunkbd_check_char, + .ioctl = sunkbd_ioctl, + .lock = sunkbd_lock, + .clear_state = sunkbd_clear_state, + .get_state = sunkbd_get_state, + .set_state = sunkbd_set_state, + .get_fkeystr = genkbd_get_fkeystr, + .poll = sunkbd_poll_mode, + .diag = sunkbd_diag }; KEYBOARD_DRIVER(sunkbd, sunkbdsw, sunkbd_configure); static struct sunkbd_softc sunkbd_softc; static struct uart_devinfo uart_keyboard; #if defined(SUNKBD_EMULATE_ATKBD) #define SCAN_PRESS 0x000 #define SCAN_RELEASE 0x080 #define SCAN_PREFIX_E0 0x100 #define SCAN_PREFIX_E1 0x200 #define SCAN_PREFIX_CTL 0x400 #define SCAN_PREFIX_SHIFT 0x800 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \ SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT) #define NOTR 0x0 /* no translation */ static const uint8_t sunkbd_trtab[] = { NOTR, 0x6d, 0x78, 0x6e, 0x79, 0x3b, 0x3c, 0x44, /* 0x00 - 0x07 */ 0x3d, 0x57, 0x3e, 0x58, 0x3f, 0x5d, 0x40, NOTR, /* 0x08 - 0x0f */ 0x41, 0x42, 0x43, 0x38, 0x5f, 0x68, 0x5c, 0x46, /* 0x10 - 0x17 */ 0x61, 0x6f, 0x70, 0x64, 0x62, 0x01, 0x02, 0x03, /* 0x18 - 0x1f */ 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 0x20 - 0x27 */ 0x0c, 0x0d, 0x29, 0x0e, 0x66, 0x77, 0x5b, 0x37, /* 0x28 - 0x2f */ 0x7a, 0x71, 0x53, 0x74, 0x5e, 0x0f, 0x10, 0x11, /* 0x30 - 0x37 */ 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, /* 0x38 - 0x3f */ 0x1a, 0x1b, 0x67, 0x6b, 0x47, 0x48, 0x49, 0x4a, /* 0x40 - 0x47 */ 0x73, 0x72, 0x63, NOTR, 0x1d, 0x1e, 0x1f, 0x20, /* 0x48 - 0x4f */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, /* 0x50 - 0x57 */ 0x2b, 0x1c, 0x59, 0x4b, 0x4c, 0x4d, 0x52, 0x75, /* 0x58 - 0x5f */ 0x60, 0x76, 0x45, 0x2a, 0x2c, 0x2d, 0x2e, 0x2f, /* 0x60 - 0x67 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, NOTR, /* 0x68 - 0x6f */ 0x4f, 0x50, 0x51, NOTR, NOTR, NOTR, 0x6c, 0x3a, /* 0x70 - 0x77 */ 0x69, 0x39, 0x6a, 0x65, 0x56, 0x4e, NOTR, NOTR /* 0x78 - 0x7f */ }; #endif static int sunkbd_probe_keyboard(struct uart_devinfo *di) { int c, id, ltries, tries; for (tries = 5; tries != 0; tries--) { uart_putc(di, SKBD_CMD_RESET); for (ltries = 1000; ltries != 0; ltries--) { if (uart_poll(di) == SKBD_RSP_RESET) break; DELAY(1000); } if (ltries == 0) continue; id = -1; for (ltries = 1000; ltries != 0; ltries--) { switch (c = uart_poll(di)) { case -1: break; case SKBD_RSP_IDLE: return (id); default: id = c; } DELAY(1000); } } return (-1); } static int sunkbd_attach(struct uart_softc *sc); static void sunkbd_uart_intr(void *arg); static int sunkbd_configure(int flags) { struct sunkbd_softc *sc; /* * We are only prepared to be used for the high-level console * when the keyboard is both configured and attached. */ if (!(flags & KB_CONF_PROBE_ONLY)) { if (KBD_IS_INITIALIZED(&sunkbd_softc.sc_kbd)) goto found; else return (0); } if (uart_cpu_getdev(UART_DEV_KEYBOARD, &uart_keyboard)) return (0); if (uart_probe(&uart_keyboard)) return (0); uart_init(&uart_keyboard); uart_keyboard.type = UART_DEV_KEYBOARD; uart_keyboard.attach = sunkbd_attach; uart_add_sysdev(&uart_keyboard); if (sunkbd_probe_keyboard(&uart_keyboard) != KB_SUN4) return (0); sc = &sunkbd_softc; callout_init(&sc->sc_repeat_callout, 0); sunkbd_clear_state(&sc->sc_kbd); #if defined(SUNKBD_EMULATE_ATKBD) kbd_init_struct(&sc->sc_kbd, SUNKBD_DRIVER_NAME, KB_101, 0, 0, 0, 0); kbd_set_maps(&sc->sc_kbd, &key_map, &accent_map, fkey_tab, sizeof(fkey_tab) / sizeof(fkey_tab[0])); #else kbd_init_struct(&sc->sc_kbd, SUNKBD_DRIVER_NAME, KB_OTHER, 0, 0, 0, 0); kbd_set_maps(&sc->sc_kbd, &keymap_sun_us_unix_kbd, &accentmap_sun_us_unix_kbd, fkey_tab, sizeof(fkey_tab) / sizeof(fkey_tab[0])); #endif sc->sc_mode = K_XLATE; kbd_register(&sc->sc_kbd); sc->sc_sysdev = &uart_keyboard; found: /* Return number of found keyboards. */ return (1); } static int sunkbd_attach(struct uart_softc *sc) { /* * Don't attach if we didn't probe the keyboard. Note that * the UART is still marked as a system device in that case. */ if (sunkbd_softc.sc_sysdev == NULL) { device_printf(sc->sc_dev, "keyboard not present\n"); return (0); } if (sc->sc_sysdev != NULL) { sunkbd_softc.sc_uart = sc; #ifdef KBD_INSTALL_CDEV kbd_attach(&sunkbd_softc.sc_kbd); #endif sunkbd_enable(&sunkbd_softc.sc_kbd); swi_add(&tty_intr_event, uart_driver_name, sunkbd_uart_intr, &sunkbd_softc, SWI_TTY, INTR_TYPE_TTY, &sc->sc_softih); sc->sc_opened = 1; KBD_INIT_DONE(&sunkbd_softc.sc_kbd); } return (0); } static void sunkbd_uart_intr(void *arg) { struct sunkbd_softc *sc = arg; int pend; if (sc->sc_uart->sc_leaving) return; pend = atomic_readandclear_32(&sc->sc_uart->sc_ttypend); if (!(pend & SER_INT_MASK)) return; if (pend & SER_INT_RXREADY) { if (KBD_IS_ACTIVE(&sc->sc_kbd) && KBD_IS_BUSY(&sc->sc_kbd)) { sc->sc_kbd.kb_callback.kc_func(&sc->sc_kbd, KBDIO_KEYINPUT, sc->sc_kbd.kb_callback.kc_arg); } } } static int sunkbd_probe(int unit, void *arg, int flags) { TODO; return (0); } static int sunkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) { TODO; return (0); } static int sunkbd_term(keyboard_t *kbd) { TODO; return (0); } static int sunkbd_intr(keyboard_t *kbd, void *arg) { TODO; return (0); } static int sunkbd_test_if(keyboard_t *kbd) { TODO; return (0); } static int sunkbd_enable(keyboard_t *kbd) { KBD_ACTIVATE(kbd); return (0); } static int sunkbd_disable(keyboard_t *kbd) { KBD_DEACTIVATE(kbd); return (0); } static int sunkbd_read(keyboard_t *kbd, int wait) { TODO; return (0); } static int sunkbd_check(keyboard_t *kbd) { struct sunkbd_softc *sc; if (!KBD_IS_ACTIVE(kbd)) return (FALSE); sc = (struct sunkbd_softc *)kbd; #if defined(SUNKBD_EMULATE_ATKBD) if (sc->sc_buffered_char[0]) return (TRUE); #endif if (sc->sc_repeating) return (TRUE); if (sc->sc_uart != NULL && !uart_rx_empty(sc->sc_uart)) return (TRUE); if (sc->sc_polling != 0 && sc->sc_sysdev != NULL && uart_rxready(sc->sc_sysdev)) return (TRUE); return (FALSE); } static u_int sunkbd_read_char(keyboard_t *kbd, int wait) { struct sunkbd_softc *sc; int key, release, repeated, suncode; sc = (struct sunkbd_softc *)kbd; #if defined(SUNKBD_EMULATE_ATKBD) if (sc->sc_mode == K_RAW && sc->sc_buffered_char[0]) { key = sc->sc_buffered_char[0]; if (key & SCAN_PREFIX) { sc->sc_buffered_char[0] = key & ~SCAN_PREFIX; return ((key & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); } else { sc->sc_buffered_char[0] = sc->sc_buffered_char[1]; sc->sc_buffered_char[1] = 0; return (key); } } #endif repeated = 0; if (sc->sc_repeating) { repeated = 1; sc->sc_repeating = 0; callout_reset(&sc->sc_repeat_callout, hz / 10, sunkbd_repeat, sc); suncode = sc->sc_repeat_key; goto process_code; } for (;;) { next_code: if (!(sc->sc_flags & KPCOMPOSE) && (sc->sc_composed_char > 0)) { key = sc->sc_composed_char; sc->sc_composed_char = 0; if (key > UCHAR_MAX) return (ERRKEY); return (key); } if (sc->sc_uart != NULL && !uart_rx_empty(sc->sc_uart)) { suncode = uart_rx_get(sc->sc_uart); } else if (sc->sc_polling != 0 && sc->sc_sysdev != NULL) { if (wait) suncode = uart_getc(sc->sc_sysdev); else if ((suncode = uart_poll(sc->sc_sysdev)) == -1) return (NOKEY); } else { return (NOKEY); } switch (suncode) { case SKBD_RSP_IDLE: break; default: process_code: ++kbd->kb_count; key = SKBD_KEY_CHAR(suncode); release = suncode & SKBD_KEY_RELEASE; if (!repeated) { if (release == 0) { callout_reset(&sc->sc_repeat_callout, hz / 2, sunkbd_repeat, sc); sc->sc_repeat_key = suncode; } else if (sc->sc_repeat_key == key) { callout_stop(&sc->sc_repeat_callout); sc->sc_repeat_key = -1; } } #if defined(SUNKBD_EMULATE_ATKBD) key = sunkbd_trtab[key]; if (key == NOTR) return (NOKEY); if (!repeated) { switch (key) { case 0x1d: /* ctrl */ if (release != 0) sc->sc_flags &= ~CTLS; else sc->sc_flags |= CTLS; break; case 0x2a: /* left shift */ case 0x36: /* right shift */ if (release != 0) sc->sc_flags &= ~SHIFTS; else sc->sc_flags |= SHIFTS; break; case 0x38: /* alt */ case 0x5d: /* altgr */ if (release != 0) sc->sc_flags &= ~ALTS; else sc->sc_flags |= ALTS; break; } } if (sc->sc_mode == K_RAW) { key = keycode2scancode(key, sc->sc_flags, release); if (key & SCAN_PREFIX) { if (key & SCAN_PREFIX_CTL) { sc->sc_buffered_char[0] = 0x1d | (key & SCAN_RELEASE); sc->sc_buffered_char[1] = key & ~SCAN_PREFIX; } else if (key & SCAN_PREFIX_SHIFT) { sc->sc_buffered_char[0] = 0x2a | (key & SCAN_RELEASE); sc->sc_buffered_char[1] = key & ~SCAN_PREFIX_SHIFT; } else { sc->sc_buffered_char[0] = key & ~SCAN_PREFIX; sc->sc_buffered_char[1] = 0; } return ((key & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); } return (key); } switch (key) { case 0x5c: /* print screen */ if (sc->sc_flags & ALTS) key = 0x54; /* sysrq */ break; case 0x68: /* pause/break */ if (sc->sc_flags & CTLS) key = 0x6c; /* break */ break; } if (sc->sc_mode == K_CODE) return (key | release); #else if (sc->sc_mode == K_RAW || sc->sc_mode == K_CODE) return (suncode); #endif #if defined(SUNKBD_EMULATE_ATKBD) if (key == 0x38) { /* left alt (KP compose key) */ #else if (key == 0x13) { /* left alt (KP compose key) */ #endif if (release != 0) { if (sc->sc_flags & KPCOMPOSE) { sc->sc_flags &= ~KPCOMPOSE; if (sc->sc_composed_char > UCHAR_MAX) sc->sc_composed_char = 0; } } else { if (!(sc->sc_flags & KPCOMPOSE)) { sc->sc_flags |= KPCOMPOSE; sc->sc_composed_char = 0; } } } if (sc->sc_flags & KPCOMPOSE) { switch (suncode) { case 0x44: /* KP 7 */ case 0x45: /* KP 8 */ case 0x46: /* KP 9 */ sc->sc_composed_char *= 10; sc->sc_composed_char += suncode - 0x3d; if (sc->sc_composed_char > UCHAR_MAX) return (ERRKEY); goto next_code; case 0x5b: /* KP 4 */ case 0x5c: /* KP 5 */ case 0x5d: /* KP 6 */ sc->sc_composed_char *= 10; sc->sc_composed_char += suncode - 0x58; if (sc->sc_composed_char > UCHAR_MAX) return (ERRKEY); goto next_code; case 0x70: /* KP 1 */ case 0x71: /* KP 2 */ case 0x72: /* KP 3 */ sc->sc_composed_char *= 10; sc->sc_composed_char += suncode - 0x6f; if (sc->sc_composed_char > UCHAR_MAX) return (ERRKEY); goto next_code; case 0x5e: /* KP 0 */ sc->sc_composed_char *= 10; if (sc->sc_composed_char > UCHAR_MAX) return (ERRKEY); goto next_code; case 0x44 | SKBD_KEY_RELEASE: /* KP 7 */ case 0x45 | SKBD_KEY_RELEASE: /* KP 8 */ case 0x46 | SKBD_KEY_RELEASE: /* KP 9 */ case 0x5b | SKBD_KEY_RELEASE: /* KP 4 */ case 0x5c | SKBD_KEY_RELEASE: /* KP 5 */ case 0x5d | SKBD_KEY_RELEASE: /* KP 6 */ case 0x70 | SKBD_KEY_RELEASE: /* KP 1 */ case 0x71 | SKBD_KEY_RELEASE: /* KP 2 */ case 0x72 | SKBD_KEY_RELEASE: /* KP 3 */ case 0x5e | SKBD_KEY_RELEASE: /* KP 0 */ goto next_code; default: if (sc->sc_composed_char > 0) { sc->sc_flags &= ~KPCOMPOSE; sc->sc_composed_char = 0; return (ERRKEY); } } } key = genkbd_keyaction(kbd, key, release, &sc->sc_state, &sc->sc_accents); if (key != NOKEY || repeated) return (key); } } return (0); } static int sunkbd_check_char(keyboard_t *kbd) { struct sunkbd_softc *sc; if (!KBD_IS_ACTIVE(kbd)) return (FALSE); sc = (struct sunkbd_softc *)kbd; if (!(sc->sc_flags & KPCOMPOSE) && (sc->sc_composed_char > 0)) return (TRUE); return (sunkbd_check(kbd)); } static int sunkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t data) { struct sunkbd_softc *sc; int c, error; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) int ival; #endif sc = (struct sunkbd_softc *)kbd; error = 0; switch (cmd) { case KDGKBMODE: *(int *)data = sc->sc_mode; break; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) case _IO('K', 7): ival = IOCPARM_IVAL(data); data = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSKBMODE: switch (*(int *)data) { case K_XLATE: if (sc->sc_mode != K_XLATE) { /* make lock key state and LED state match */ sc->sc_state &= ~LOCK_MASK; sc->sc_state |= KBD_LED_VAL(kbd); } /* FALLTHROUGH */ case K_RAW: case K_CODE: if (sc->sc_mode != *(int *)data) { sunkbd_clear_state(kbd); sc->sc_mode = *(int *)data; } break; default: error = EINVAL; break; } break; case KDGETLED: *(int *)data = KBD_LED_VAL(kbd); break; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) case _IO('K', 66): ival = IOCPARM_IVAL(data); data = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSETLED: if (*(int *)data & ~LOCK_MASK) { error = EINVAL; break; } if (sc->sc_sysdev == NULL) break; c = 0; if (*(int *)data & CLKED) c |= SKBD_LED_CAPSLOCK; if (*(int *)data & NLKED) c |= SKBD_LED_NUMLOCK; if (*(int *)data & SLKED) c |= SKBD_LED_SCROLLLOCK; uart_lock(sc->sc_sysdev->hwmtx); sc->sc_sysdev->ops->putc(&sc->sc_sysdev->bas, SKBD_CMD_SETLED); sc->sc_sysdev->ops->putc(&sc->sc_sysdev->bas, c); uart_unlock(sc->sc_sysdev->hwmtx); KBD_LED_VAL(kbd) = *(int *)data; break; case KDGKBSTATE: *(int *)data = sc->sc_state & LOCK_MASK; break; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) case _IO('K', 20): ival = IOCPARM_IVAL(data); data = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSKBSTATE: if (*(int *)data & ~LOCK_MASK) { error = EINVAL; break; } sc->sc_state &= ~LOCK_MASK; sc->sc_state |= *(int *)data; /* set LEDs and quit */ return (sunkbd_ioctl(kbd, KDSETLED, data)); case KDSETREPEAT: case KDSETRAD: break; case PIO_KEYMAP: case OPIO_KEYMAP: case PIO_KEYMAPENT: case PIO_DEADKEYMAP: default: return (genkbd_commonioctl(kbd, cmd, data)); } return (error); } static int sunkbd_lock(keyboard_t *kbd, int lock) { TODO; return (0); } static void sunkbd_clear_state(keyboard_t *kbd) { struct sunkbd_softc *sc; sc = (struct sunkbd_softc *)kbd; sc->sc_repeat_key = -1; sc->sc_accents = 0; sc->sc_composed_char = 0; sc->sc_flags = 0; sc->sc_polling = 0; sc->sc_repeating = 0; sc->sc_state &= LOCK_MASK; /* Preserve locking key state. */ #if defined(SUNKBD_EMULATE_ATKBD) sc->sc_buffered_char[0] = 0; sc->sc_buffered_char[1] = 0; #endif } static int sunkbd_get_state(keyboard_t *kbd, void *buf, size_t len) { TODO; return (0); } static int sunkbd_set_state(keyboard_t *kbd, void *buf, size_t len) { TODO; return (0); } static int sunkbd_poll_mode(keyboard_t *kbd, int on) { struct sunkbd_softc *sc; sc = (struct sunkbd_softc *)kbd; if (on) sc->sc_polling++; else sc->sc_polling--; return (0); } static void sunkbd_diag(keyboard_t *kbd, int level) { TODO; } static void sunkbd_repeat(void *v) { struct sunkbd_softc *sc = v; if (KBD_IS_ACTIVE(&sc->sc_kbd) && KBD_IS_BUSY(&sc->sc_kbd)) { if (sc->sc_repeat_key != -1) { sc->sc_repeating = 1; sc->sc_kbd.kb_callback.kc_func(&sc->sc_kbd, KBDIO_KEYINPUT, sc->sc_kbd.kb_callback.kc_arg); } } } #if defined(SUNKBD_EMULATE_ATKBD) static int keycode2scancode(int keycode, int shift, int up) { static const int scan[] = { /* KP enter, right ctrl, KP divide */ 0x1c , 0x1d , 0x35 , /* print screen */ 0x37 | SCAN_PREFIX_SHIFT, /* right alt, home, up, page up, left, right, end */ 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f, /* down, page down, insert, delete */ 0x50, 0x51, 0x52, 0x53, /* pause/break (see also below) */ 0x46, /* * MS: left window, right window, menu * also Sun: left meta, right meta, compose */ 0x5b, 0x5c, 0x5d, /* Sun type 6 USB */ /* help, stop, again, props, undo, front, copy */ 0x68, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, /* open, paste, find, cut, audiomute, audiolower, audioraise */ 0x64, 0x65, 0x66, 0x67, 0x25, 0x1f, 0x1e, /* power */ 0x20 }; int scancode; scancode = keycode; if ((keycode >= 89) && (keycode < 89 + nitems(scan))) scancode = scan[keycode - 89] | SCAN_PREFIX_E0; /* pause/break */ if ((keycode == 104) && !(shift & CTLS)) scancode = 0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL; if (shift & SHIFTS) scancode &= ~SCAN_PREFIX_SHIFT; return (scancode | (up ? SCAN_RELEASE : SCAN_PRESS)); } #endif Index: stable/11 =================================================================== --- stable/11 (revision 356006) +++ stable/11 (revision 356007) Property changes on: stable/11 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head:r355794 Index: stable/12/sys/dev/adb/adb_kbd.c =================================================================== --- stable/12/sys/dev/adb/adb_kbd.c (revision 356006) +++ stable/12/sys/dev/adb/adb_kbd.c (revision 356007) @@ -1,892 +1,892 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (C) 2008 Nathan Whitehorn * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include #include "opt_kbd.h" #include #include #include #include #include #include #include "adb.h" #define KBD_DRIVER_NAME "akbd" #define AKBD_EMULATE_ATKBD 1 static int adb_kbd_probe(device_t dev); static int adb_kbd_attach(device_t dev); static int adb_kbd_detach(device_t dev); static void akbd_repeat(void *xsc); static int adb_fn_keys(SYSCTL_HANDLER_ARGS); static u_int adb_kbd_receive_packet(device_t dev, u_char status, u_char command, u_char reg, int len, u_char *data); struct adb_kbd_softc { keyboard_t sc_kbd; device_t sc_dev; struct mtx sc_mutex; struct cv sc_cv; int sc_mode; int sc_state; int have_led_control; uint8_t buffer[8]; #ifdef AKBD_EMULATE_ATKBD uint8_t at_buffered_char[2]; #endif volatile int buffers; struct callout sc_repeater; int sc_repeatstart; int sc_repeatcontinue; uint8_t last_press; }; static device_method_t adb_kbd_methods[] = { /* Device interface */ DEVMETHOD(device_probe, adb_kbd_probe), DEVMETHOD(device_attach, adb_kbd_attach), DEVMETHOD(device_detach, adb_kbd_detach), DEVMETHOD(device_shutdown, bus_generic_shutdown), DEVMETHOD(device_suspend, bus_generic_suspend), DEVMETHOD(device_resume, bus_generic_resume), /* ADB interface */ DEVMETHOD(adb_receive_packet, adb_kbd_receive_packet), { 0, 0 } }; static driver_t adb_kbd_driver = { "akbd", adb_kbd_methods, sizeof(struct adb_kbd_softc), }; static devclass_t adb_kbd_devclass; DRIVER_MODULE(akbd, adb, adb_kbd_driver, adb_kbd_devclass, 0, 0); #ifdef AKBD_EMULATE_ATKBD #define SCAN_PRESS 0x000 #define SCAN_RELEASE 0x080 #define SCAN_PREFIX_E0 0x100 #define SCAN_PREFIX_E1 0x200 #define SCAN_PREFIX_CTL 0x400 #define SCAN_PREFIX_SHIFT 0x800 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \ SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT) static const uint8_t adb_to_at_scancode_map[128] = { 30, 31, 32, 33, 35, 34, 44, 45, 46, 47, 0, 48, 16, 17, 18, 19, 21, 20, 2, 3, 4, 5, 7, 6, 13, 10, 8, 12, 9, 11, 27, 24, 22, 26, 23, 25, 28, 38, 36, 40, 37, 39, 43, 51, 53, 49, 50, 52, 15, 57, 41, 14, 0, 1, 29, 0, 42, 58, 56, 97, 98, 100, 95, 0, 0, 83, 0, 55, 0, 78, 0, 69, 0, 0, 0, 91, 89, 0, 74, 13, 0, 0, 82, 79, 80, 81, 75, 76, 77, 71, 0, 72, 73, 0, 0, 0, 63, 64, 65, 61, 66, 67, 0, 87, 0, 105, 0, 70, 0, 68, 0, 88, 0, 107, 102, 94, 96, 103, 62, 99, 60, 101, 59, 54, 93, 90, 0, 0 }; static int keycode2scancode(int keycode, int shift, int up) { static const int scan[] = { /* KP enter, right ctrl, KP divide */ 0x1c , 0x1d , 0x35 , /* print screen */ 0x37 | SCAN_PREFIX_SHIFT, /* right alt, home, up, page up, left, right, end */ 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f, /* down, page down, insert, delete */ 0x50, 0x51, 0x52, 0x53, /* pause/break (see also below) */ 0x46, /* * MS: left window, right window, menu * also Sun: left meta, right meta, compose */ 0x5b, 0x5c, 0x5d, /* Sun type 6 USB */ /* help, stop, again, props, undo, front, copy */ 0x68, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, /* open, paste, find, cut, audiomute, audiolower, audioraise */ 0x64, 0x65, 0x66, 0x67, 0x25, 0x1f, 0x1e, /* power */ 0x20 }; int scancode; scancode = keycode; if ((keycode >= 89) && (keycode < 89 + nitems(scan))) scancode = scan[keycode - 89] | SCAN_PREFIX_E0; /* pause/break */ if ((keycode == 104) && !(shift & CTLS)) scancode = 0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL; if (shift & SHIFTS) scancode &= ~SCAN_PREFIX_SHIFT; return (scancode | (up ? SCAN_RELEASE : SCAN_PRESS)); } #endif /* keyboard driver declaration */ static int akbd_configure(int flags); static kbd_probe_t akbd_probe; static kbd_init_t akbd_init; static kbd_term_t akbd_term; static kbd_intr_t akbd_interrupt; static kbd_test_if_t akbd_test_if; static kbd_enable_t akbd_enable; static kbd_disable_t akbd_disable; static kbd_read_t akbd_read; static kbd_check_t akbd_check; static kbd_read_char_t akbd_read_char; static kbd_check_char_t akbd_check_char; static kbd_ioctl_t akbd_ioctl; static kbd_lock_t akbd_lock; static kbd_clear_state_t akbd_clear_state; static kbd_get_state_t akbd_get_state; static kbd_set_state_t akbd_set_state; static kbd_poll_mode_t akbd_poll; keyboard_switch_t akbdsw = { - akbd_probe, - akbd_init, - akbd_term, - akbd_interrupt, - akbd_test_if, - akbd_enable, - akbd_disable, - akbd_read, - akbd_check, - akbd_read_char, - akbd_check_char, - akbd_ioctl, - akbd_lock, - akbd_clear_state, - akbd_get_state, - akbd_set_state, - genkbd_get_fkeystr, - akbd_poll, - genkbd_diag, + .probe = akbd_probe, + .init = akbd_init, + .term = akbd_term, + .intr = akbd_interrupt, + .test_if = akbd_test_if, + .enable = akbd_enable, + .disable = akbd_disable, + .read = akbd_read, + .check = akbd_check, + .read_char = akbd_read_char, + .check_char = akbd_check_char, + .ioctl = akbd_ioctl, + .lock = akbd_lock, + .clear_state = akbd_clear_state, + .get_state = akbd_get_state, + .set_state = akbd_set_state, + .get_fkeystr = genkbd_get_fkeystr, + .poll = akbd_poll, + .diag = genkbd_diag, }; KEYBOARD_DRIVER(akbd, akbdsw, akbd_configure); static int adb_kbd_probe(device_t dev) { uint8_t type; type = adb_get_device_type(dev); if (type != ADB_DEVICE_KEYBOARD) return (ENXIO); switch(adb_get_device_handler(dev)) { case 1: device_set_desc(dev,"Apple Standard Keyboard"); break; case 2: device_set_desc(dev,"Apple Extended Keyboard"); break; case 4: device_set_desc(dev,"Apple ISO Keyboard"); break; case 5: device_set_desc(dev,"Apple Extended ISO Keyboard"); break; case 8: device_set_desc(dev,"Apple Keyboard II"); break; case 9: device_set_desc(dev,"Apple ISO Keyboard II"); break; case 12: device_set_desc(dev,"PowerBook Keyboard"); break; case 13: device_set_desc(dev,"PowerBook ISO Keyboard"); break; case 24: device_set_desc(dev,"PowerBook Extended Keyboard"); break; case 27: device_set_desc(dev,"Apple Design Keyboard"); break; case 195: device_set_desc(dev,"PowerBook G3 Keyboard"); break; case 196: device_set_desc(dev,"iBook Keyboard"); break; default: device_set_desc(dev,"ADB Keyboard"); break; } return (0); } static int ms_to_ticks(int ms) { if (hz > 1000) return ms*(hz/1000); return ms/(1000/hz); } static int adb_kbd_attach(device_t dev) { struct adb_kbd_softc *sc; keyboard_switch_t *sw; uint32_t fkeys; phandle_t handle; sw = kbd_get_switch(KBD_DRIVER_NAME); if (sw == NULL) { return ENXIO; } sc = device_get_softc(dev); sc->sc_dev = dev; sc->sc_mode = K_RAW; sc->sc_state = 0; sc->have_led_control = 0; sc->buffers = 0; /* Try stepping forward to the extended keyboard protocol */ adb_set_device_handler(dev,3); mtx_init(&sc->sc_mutex, KBD_DRIVER_NAME, NULL, MTX_DEF); cv_init(&sc->sc_cv,KBD_DRIVER_NAME); callout_init(&sc->sc_repeater, 0); #ifdef AKBD_EMULATE_ATKBD kbd_init_struct(&sc->sc_kbd, KBD_DRIVER_NAME, KB_101, 0, 0, 0, 0); kbd_set_maps(&sc->sc_kbd, &key_map, &accent_map, fkey_tab, sizeof(fkey_tab) / sizeof(fkey_tab[0])); #else #error ADB raw mode not implemented #endif KBD_FOUND_DEVICE(&sc->sc_kbd); KBD_PROBE_DONE(&sc->sc_kbd); KBD_INIT_DONE(&sc->sc_kbd); KBD_CONFIG_DONE(&sc->sc_kbd); (*sw->enable)(&sc->sc_kbd); kbd_register(&sc->sc_kbd); #ifdef KBD_INSTALL_CDEV if (kbd_attach(&sc->sc_kbd)) { adb_kbd_detach(dev); return ENXIO; } #endif /* Check if we can read out the LED state from this keyboard by reading the key state register */ if (adb_read_register(dev, 2, NULL) == 2) sc->have_led_control = 1; adb_set_autopoll(dev,1); handle = OF_finddevice("mac-io/via-pmu/adb/keyboard"); if (handle != -1 && OF_getprop(handle, "AAPL,has-embedded-fn-keys", &fkeys, sizeof(fkeys)) != -1) { static const char *key_names[] = {"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12"}; struct sysctl_ctx_list *ctx; struct sysctl_oid *tree; int i; if (bootverbose) device_printf(dev, "Keyboard has embedded Fn keys\n"); for (i = 0; i < 12; i++) { uint32_t keyval; char buf[3]; if (OF_getprop(handle, key_names[i], &keyval, sizeof(keyval)) < 0) continue; buf[0] = 1; buf[1] = i+1; buf[2] = keyval; adb_write_register(dev, 0, 3, buf); } adb_write_register(dev, 1, 2, &(uint16_t){0}); ctx = device_get_sysctl_ctx(dev); tree = device_get_sysctl_tree(dev); SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "fn_keys_function_as_primary", CTLTYPE_INT | CTLFLAG_RW, sc, 0, adb_fn_keys, "I", "Set the Fn keys to be their F-key type as default"); } return (0); } static int adb_kbd_detach(device_t dev) { struct adb_kbd_softc *sc; keyboard_t *kbd; sc = device_get_softc(dev); adb_set_autopoll(dev,0); callout_stop(&sc->sc_repeater); mtx_lock(&sc->sc_mutex); kbd = kbd_get_keyboard(kbd_find_keyboard(KBD_DRIVER_NAME, device_get_unit(dev))); kbdd_disable(kbd); #ifdef KBD_INSTALL_CDEV kbd_detach(kbd); #endif kbdd_term(kbd); mtx_unlock(&sc->sc_mutex); mtx_destroy(&sc->sc_mutex); cv_destroy(&sc->sc_cv); return (0); } static u_int adb_kbd_receive_packet(device_t dev, u_char status, u_char command, u_char reg, int len, u_char *data) { struct adb_kbd_softc *sc; sc = device_get_softc(dev); if (command != ADB_COMMAND_TALK) return 0; if (reg != 0 || len != 2) return (0); mtx_lock(&sc->sc_mutex); /* 0x7f is always the power button */ if (data[0] == 0x7f) { devctl_notify("PMU", "Button", "pressed", NULL); mtx_unlock(&sc->sc_mutex); return (0); } else if (data[0] == 0xff) { mtx_unlock(&sc->sc_mutex); return (0); /* Ignore power button release. */ } if ((data[0] & 0x7f) == 57 && sc->buffers < 7) { /* Fake the down/up cycle for caps lock */ sc->buffer[sc->buffers++] = data[0] & 0x7f; sc->buffer[sc->buffers++] = (data[0] & 0x7f) | (1 << 7); } else { sc->buffer[sc->buffers++] = data[0]; } if (sc->buffer[sc->buffers-1] < 0xff) sc->last_press = sc->buffer[sc->buffers-1]; if ((data[1] & 0x7f) == 57 && sc->buffers < 7) { /* Fake the down/up cycle for caps lock */ sc->buffer[sc->buffers++] = data[1] & 0x7f; sc->buffer[sc->buffers++] = (data[1] & 0x7f) | (1 << 7); } else { sc->buffer[sc->buffers++] = data[1]; } if (sc->buffer[sc->buffers-1] < 0xff) sc->last_press = sc->buffer[sc->buffers-1]; /* Stop any existing key repeating */ callout_stop(&sc->sc_repeater); /* Schedule a repeat callback on keydown */ if (!(sc->last_press & (1 << 7))) { callout_reset(&sc->sc_repeater, ms_to_ticks(sc->sc_kbd.kb_delay1), akbd_repeat, sc); } mtx_unlock(&sc->sc_mutex); cv_broadcast(&sc->sc_cv); if (KBD_IS_ACTIVE(&sc->sc_kbd) && KBD_IS_BUSY(&sc->sc_kbd)) { sc->sc_kbd.kb_callback.kc_func(&sc->sc_kbd, KBDIO_KEYINPUT, sc->sc_kbd.kb_callback.kc_arg); } return (0); } static void akbd_repeat(void *xsc) { struct adb_kbd_softc *sc = xsc; int notify_kbd = 0; /* Fake an up/down key repeat so long as we have the free buffers */ mtx_lock(&sc->sc_mutex); if (sc->buffers < 7) { sc->buffer[sc->buffers++] = sc->last_press | (1 << 7); sc->buffer[sc->buffers++] = sc->last_press; notify_kbd = 1; } mtx_unlock(&sc->sc_mutex); if (notify_kbd && KBD_IS_ACTIVE(&sc->sc_kbd) && KBD_IS_BUSY(&sc->sc_kbd)) { sc->sc_kbd.kb_callback.kc_func(&sc->sc_kbd, KBDIO_KEYINPUT, sc->sc_kbd.kb_callback.kc_arg); } /* Reschedule the callout */ callout_reset(&sc->sc_repeater, ms_to_ticks(sc->sc_kbd.kb_delay2), akbd_repeat, sc); } static int akbd_configure(int flags) { return 0; } static int akbd_probe(int unit, void *arg, int flags) { return 0; } static int akbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) { return 0; } static int akbd_term(keyboard_t *kbd) { return 0; } static int akbd_interrupt(keyboard_t *kbd, void *arg) { return 0; } static int akbd_test_if(keyboard_t *kbd) { return 0; } static int akbd_enable(keyboard_t *kbd) { KBD_ACTIVATE(kbd); return (0); } static int akbd_disable(keyboard_t *kbd) { struct adb_kbd_softc *sc; sc = (struct adb_kbd_softc *)(kbd); callout_stop(&sc->sc_repeater); KBD_DEACTIVATE(kbd); return (0); } static int akbd_read(keyboard_t *kbd, int wait) { return (0); } static int akbd_check(keyboard_t *kbd) { struct adb_kbd_softc *sc; if (!KBD_IS_ACTIVE(kbd)) return (FALSE); sc = (struct adb_kbd_softc *)(kbd); mtx_lock(&sc->sc_mutex); #ifdef AKBD_EMULATE_ATKBD if (sc->at_buffered_char[0]) { mtx_unlock(&sc->sc_mutex); return (TRUE); } #endif if (sc->buffers > 0) { mtx_unlock(&sc->sc_mutex); return (TRUE); } mtx_unlock(&sc->sc_mutex); return (FALSE); } static u_int akbd_read_char(keyboard_t *kbd, int wait) { struct adb_kbd_softc *sc; uint16_t key; uint8_t adb_code; int i; sc = (struct adb_kbd_softc *)(kbd); mtx_lock(&sc->sc_mutex); #if defined(AKBD_EMULATE_ATKBD) if (sc->sc_mode == K_RAW && sc->at_buffered_char[0]) { key = sc->at_buffered_char[0]; if (key & SCAN_PREFIX) { sc->at_buffered_char[0] = key & ~SCAN_PREFIX; key = (key & SCAN_PREFIX_E0) ? 0xe0 : 0xe1; } else { sc->at_buffered_char[0] = sc->at_buffered_char[1]; sc->at_buffered_char[1] = 0; } mtx_unlock(&sc->sc_mutex); return (key); } #endif if (!sc->buffers && wait) cv_wait(&sc->sc_cv,&sc->sc_mutex); if (!sc->buffers) { mtx_unlock(&sc->sc_mutex); return (NOKEY); } adb_code = sc->buffer[0]; for (i = 1; i < sc->buffers; i++) sc->buffer[i-1] = sc->buffer[i]; sc->buffers--; #ifdef AKBD_EMULATE_ATKBD key = adb_to_at_scancode_map[adb_code & 0x7f]; if (sc->sc_mode == K_CODE) { /* Add the key-release bit */ key |= adb_code & 0x80; } else if (sc->sc_mode == K_RAW) { /* * In the raw case, we have to emulate the gross * variable-length AT keyboard thing. Since this code * is copied from sunkbd, which is the same code * as ukbd, it might be nice to have this centralized. */ key = keycode2scancode(key, 0, adb_code & 0x80); if (key & SCAN_PREFIX) { if (key & SCAN_PREFIX_CTL) { sc->at_buffered_char[0] = 0x1d | (key & SCAN_RELEASE); sc->at_buffered_char[1] = key & ~SCAN_PREFIX; } else if (key & SCAN_PREFIX_SHIFT) { sc->at_buffered_char[0] = 0x2a | (key & SCAN_RELEASE); sc->at_buffered_char[1] = key & ~SCAN_PREFIX_SHIFT; } else { sc->at_buffered_char[0] = key & ~SCAN_PREFIX; sc->at_buffered_char[1] = 0; } key = (key & SCAN_PREFIX_E0) ? 0xe0 : 0xe1; } } #else key = adb_code; #endif mtx_unlock(&sc->sc_mutex); return (key); } static int akbd_check_char(keyboard_t *kbd) { if (!KBD_IS_ACTIVE(kbd)) return (FALSE); return (akbd_check(kbd)); } static int set_typematic(keyboard_t *kbd, int code) { /* These numbers are in microseconds, so convert to ticks */ static int delays[] = { 250, 500, 750, 1000 }; static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63, 68, 76, 84, 92, 100, 110, 118, 126, 136, 152, 168, 184, 200, 220, 236, 252, 272, 304, 336, 368, 400, 440, 472, 504 }; if (code & ~0x7f) return EINVAL; kbd->kb_delay1 = delays[(code >> 5) & 3]; kbd->kb_delay2 = rates[code & 0x1f]; return 0; } static int akbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t data) { struct adb_kbd_softc *sc; uint16_t r2; int error; sc = (struct adb_kbd_softc *)(kbd); error = 0; switch (cmd) { case KDGKBMODE: *(int *)data = sc->sc_mode; break; case KDSKBMODE: switch (*(int *)data) { case K_XLATE: if (sc->sc_mode != K_XLATE) { /* make lock key state and LED state match */ sc->sc_state &= ~LOCK_MASK; sc->sc_state |= KBD_LED_VAL(kbd); } /* FALLTHROUGH */ case K_RAW: case K_CODE: if (sc->sc_mode != *(int *)data) sc->sc_mode = *(int *)data; break; default: error = EINVAL; break; } break; case KDGETLED: *(int *)data = KBD_LED_VAL(kbd); break; case KDSKBSTATE: if (*(int *)data & ~LOCK_MASK) { error = EINVAL; break; } sc->sc_state &= ~LOCK_MASK; sc->sc_state |= *(int *)data; /* FALLTHROUGH */ case KDSETLED: KBD_LED_VAL(kbd) = *(int *)data; if (!sc->have_led_control) break; r2 = (~0 & 0x04) | 3; if (*(int *)data & NLKED) r2 &= ~1; if (*(int *)data & CLKED) r2 &= ~2; if (*(int *)data & SLKED) r2 &= ~4; adb_send_packet(sc->sc_dev,ADB_COMMAND_LISTEN,2, sizeof(uint16_t),(u_char *)&r2); break; case KDGKBSTATE: *(int *)data = sc->sc_state & LOCK_MASK; break; case KDSETREPEAT: if (!KBD_HAS_DEVICE(kbd)) return 0; if (((int *)data)[1] < 0) return EINVAL; if (((int *)data)[0] < 0) return EINVAL; else if (((int *)data)[0] == 0) /* fastest possible value */ kbd->kb_delay1 = 200; else kbd->kb_delay1 = ((int *)data)[0]; kbd->kb_delay2 = ((int *)data)[1]; break; case KDSETRAD: error = set_typematic(kbd, *(int *)data); break; case PIO_KEYMAP: case OPIO_KEYMAP: case PIO_KEYMAPENT: case PIO_DEADKEYMAP: default: return (genkbd_commonioctl(kbd, cmd, data)); } return (error); } static int akbd_lock(keyboard_t *kbd, int lock) { return (0); } static void akbd_clear_state(keyboard_t *kbd) { struct adb_kbd_softc *sc; sc = (struct adb_kbd_softc *)(kbd); mtx_lock(&sc->sc_mutex); sc->buffers = 0; callout_stop(&sc->sc_repeater); #if defined(AKBD_EMULATE_ATKBD) sc->at_buffered_char[0] = 0; sc->at_buffered_char[1] = 0; #endif mtx_unlock(&sc->sc_mutex); } static int akbd_get_state(keyboard_t *kbd, void *buf, size_t len) { return (0); } static int akbd_set_state(keyboard_t *kbd, void *buf, size_t len) { return (0); } static int akbd_poll(keyboard_t *kbd, int on) { return (0); } static int akbd_modevent(module_t mod, int type, void *data) { switch (type) { case MOD_LOAD: kbd_add_driver(&akbd_kbd_driver); break; case MOD_UNLOAD: kbd_delete_driver(&akbd_kbd_driver); break; default: return (EOPNOTSUPP); } return (0); } static int adb_fn_keys(SYSCTL_HANDLER_ARGS) { struct adb_kbd_softc *sc = arg1; int error; uint16_t is_fn_enabled; unsigned int is_fn_enabled_sysctl; adb_read_register(sc->sc_dev, 1, &is_fn_enabled); is_fn_enabled &= 1; is_fn_enabled_sysctl = is_fn_enabled; error = sysctl_handle_int(oidp, &is_fn_enabled_sysctl, 0, req); if (error || !req->newptr) return (error); is_fn_enabled = is_fn_enabled_sysctl; if (is_fn_enabled != 1 && is_fn_enabled != 0) return (EINVAL); adb_write_register(sc->sc_dev, 1, 2, &is_fn_enabled); return (0); } DEV_MODULE(akbd, akbd_modevent, NULL); Index: stable/12/sys/dev/atkbdc/atkbd.c =================================================================== --- stable/12/sys/dev/atkbdc/atkbd.c (revision 356006) +++ stable/12/sys/dev/atkbdc/atkbd.c (revision 356007) @@ -1,1621 +1,1621 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 1999 Kazutaka YOKOTA * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer as * the first lines of this file unmodified. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include __FBSDID("$FreeBSD$"); #include "opt_kbd.h" #include "opt_atkbd.h" #include "opt_evdev.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef EVDEV_SUPPORT #include #include #endif typedef struct atkbd_state { KBDC kbdc; /* keyboard controller */ int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */ int ks_flags; /* flags */ #define COMPOSE (1 << 0) int ks_polling; int ks_state; /* shift/lock key state */ int ks_accents; /* accent key index (> 0) */ u_int ks_composed_char; /* composed char code (> 0) */ u_char ks_prefix; /* AT scan code prefix */ struct callout ks_timer; #ifdef EVDEV_SUPPORT struct evdev_dev *ks_evdev; int ks_evdev_state; #endif } atkbd_state_t; static void atkbd_timeout(void *arg); static void atkbd_shutdown_final(void *v); static int atkbd_reset(KBDC kbdc, int flags, int c); #define HAS_QUIRK(p, q) (((atkbdc_softc_t *)(p))->quirks & q) #define ALLOW_DISABLE_KBD(kbdc) !HAS_QUIRK(kbdc, KBDC_QUIRK_KEEP_ACTIVATED) #define DEFAULT_DELAY 0x1 /* 500ms */ #define DEFAULT_RATE 0x10 /* 14Hz */ #ifdef EVDEV_SUPPORT #define PS2_KEYBOARD_VENDOR 1 #define PS2_KEYBOARD_PRODUCT 1 #endif int atkbd_probe_unit(device_t dev, int irq, int flags) { keyboard_switch_t *sw; int args[2]; int error; sw = kbd_get_switch(ATKBD_DRIVER_NAME); if (sw == NULL) return ENXIO; args[0] = device_get_unit(device_get_parent(dev)); args[1] = irq; error = (*sw->probe)(device_get_unit(dev), args, flags); if (error) return error; return 0; } int atkbd_attach_unit(device_t dev, keyboard_t **kbd, int irq, int flags) { keyboard_switch_t *sw; atkbd_state_t *state; int args[2]; int error; int unit; sw = kbd_get_switch(ATKBD_DRIVER_NAME); if (sw == NULL) return ENXIO; /* reset, initialize and enable the device */ unit = device_get_unit(dev); args[0] = device_get_unit(device_get_parent(dev)); args[1] = irq; *kbd = NULL; error = (*sw->probe)(unit, args, flags); if (error) return error; error = (*sw->init)(unit, kbd, args, flags); if (error) return error; (*sw->enable)(*kbd); #ifdef KBD_INSTALL_CDEV /* attach a virtual keyboard cdev */ error = kbd_attach(*kbd); if (error) return error; #endif /* * This is a kludge to compensate for lost keyboard interrupts. * A similar code used to be in syscons. See below. XXX */ state = (atkbd_state_t *)(*kbd)->kb_data; callout_init(&state->ks_timer, 0); atkbd_timeout(*kbd); if (bootverbose) (*sw->diag)(*kbd, bootverbose); EVENTHANDLER_REGISTER(shutdown_final, atkbd_shutdown_final, *kbd, SHUTDOWN_PRI_DEFAULT); return 0; } static void atkbd_timeout(void *arg) { atkbd_state_t *state; keyboard_t *kbd; int s; /* * The original text of the following comments are extracted * from syscons.c (1.287) * * With release 2.1 of the Xaccel server, the keyboard is left * hanging pretty often. Apparently an interrupt from the * keyboard is lost, and I don't know why (yet). * This ugly hack calls the low-level interrupt routine if input * is ready for the keyboard and conveniently hides the problem. XXX * * Try removing anything stuck in the keyboard controller; whether * it's a keyboard scan code or mouse data. The low-level * interrupt routine doesn't read the mouse data directly, * but the keyboard controller driver will, as a side effect. */ /* * And here is bde's original comment about this: * * This is necessary to handle edge triggered interrupts - if we * returned when our IRQ is high due to unserviced input, then there * would be no more keyboard IRQs until the keyboard is reset by * external powers. * * The keyboard apparently unwedges the irq in most cases. */ s = spltty(); kbd = (keyboard_t *)arg; if (kbdd_lock(kbd, TRUE)) { /* * We have seen the lock flag is not set. Let's reset * the flag early, otherwise the LED update routine fails * which may want the lock during the interrupt routine. */ kbdd_lock(kbd, FALSE); if (kbdd_check_char(kbd)) kbdd_intr(kbd, NULL); } splx(s); state = (atkbd_state_t *)kbd->kb_data; callout_reset(&state->ks_timer, hz / 10, atkbd_timeout, arg); } /* LOW-LEVEL */ #define ATKBD_DEFAULT 0 /* keyboard driver declaration */ static int atkbd_configure(int flags); static kbd_probe_t atkbd_probe; static kbd_init_t atkbd_init; static kbd_term_t atkbd_term; static kbd_intr_t atkbd_intr; static kbd_test_if_t atkbd_test_if; static kbd_enable_t atkbd_enable; static kbd_disable_t atkbd_disable; static kbd_read_t atkbd_read; static kbd_check_t atkbd_check; static kbd_read_char_t atkbd_read_char; static kbd_check_char_t atkbd_check_char; static kbd_ioctl_t atkbd_ioctl; static kbd_lock_t atkbd_lock; static kbd_clear_state_t atkbd_clear_state; static kbd_get_state_t atkbd_get_state; static kbd_set_state_t atkbd_set_state; static kbd_poll_mode_t atkbd_poll; static keyboard_switch_t atkbdsw = { - atkbd_probe, - atkbd_init, - atkbd_term, - atkbd_intr, - atkbd_test_if, - atkbd_enable, - atkbd_disable, - atkbd_read, - atkbd_check, - atkbd_read_char, - atkbd_check_char, - atkbd_ioctl, - atkbd_lock, - atkbd_clear_state, - atkbd_get_state, - atkbd_set_state, - genkbd_get_fkeystr, - atkbd_poll, - genkbd_diag, + .probe = atkbd_probe, + .init = atkbd_init, + .term = atkbd_term, + .intr = atkbd_intr, + .test_if = atkbd_test_if, + .enable = atkbd_enable, + .disable = atkbd_disable, + .read = atkbd_read, + .check = atkbd_check, + .read_char = atkbd_read_char, + .check_char = atkbd_check_char, + .ioctl = atkbd_ioctl, + .lock = atkbd_lock, + .clear_state = atkbd_clear_state, + .get_state = atkbd_get_state, + .set_state = atkbd_set_state, + .get_fkeystr = genkbd_get_fkeystr, + .poll = atkbd_poll, + .diag = genkbd_diag, }; KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure); /* local functions */ static int set_typematic(keyboard_t *kbd); static int setup_kbd_port(KBDC kbdc, int port, int intr); static int get_kbd_echo(KBDC kbdc); static int probe_keyboard(KBDC kbdc, int flags); static int init_keyboard(KBDC kbdc, int *type, int flags); static int write_kbd(KBDC kbdc, int command, int data); static int get_kbd_id(KBDC kbdc); static int typematic(int delay, int rate); static int typematic_delay(int delay); static int typematic_rate(int rate); #ifdef EVDEV_SUPPORT static evdev_event_t atkbd_ev_event; static const struct evdev_methods atkbd_evdev_methods = { .ev_event = atkbd_ev_event, }; #endif /* local variables */ /* the initial key map, accent map and fkey strings */ #ifdef ATKBD_DFLT_KEYMAP #define KBD_DFLT_KEYMAP #include "atkbdmap.h" #endif #include /* structures for the default keyboard */ static keyboard_t default_kbd; static atkbd_state_t default_kbd_state; static keymap_t default_keymap; static accentmap_t default_accentmap; static fkeytab_t default_fkeytab[NUM_FKEYS]; /* * The back door to the keyboard driver! * This function is called by the console driver, via the kbdio module, * to tickle keyboard drivers when the low-level console is being initialized. * Almost nothing in the kernel has been initialied yet. Try to probe * keyboards if possible. * NOTE: because of the way the low-level console is initialized, this routine * may be called more than once!! */ static int atkbd_configure(int flags) { keyboard_t *kbd; int arg[2]; int i; /* * Probe the keyboard controller, if not present or if the driver * is disabled, unregister the keyboard if any. */ if (atkbdc_configure() != 0 || resource_disabled("atkbd", ATKBD_DEFAULT)) { i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT); if (i >= 0) { kbd = kbd_get_keyboard(i); kbd_unregister(kbd); kbd->kb_flags &= ~KB_REGISTERED; } return 0; } /* XXX: a kludge to obtain the device configuration flags */ if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0) flags |= i; /* probe the default keyboard */ arg[0] = -1; arg[1] = -1; kbd = NULL; if (atkbd_probe(ATKBD_DEFAULT, arg, flags)) return 0; if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags)) return 0; /* return the number of found keyboards */ return 1; } /* low-level functions */ /* detect a keyboard */ static int atkbd_probe(int unit, void *arg, int flags) { KBDC kbdc; int *data = (int *)arg; /* data[0]: controller, data[1]: irq */ /* XXX */ if (unit == ATKBD_DEFAULT) { if (KBD_IS_PROBED(&default_kbd)) return 0; } kbdc = atkbdc_open(data[0]); if (kbdc == NULL) return ENXIO; if (probe_keyboard(kbdc, flags)) { if (flags & KB_CONF_FAIL_IF_NO_KBD) return ENXIO; } return 0; } /* reset and initialize the device */ static int atkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) { keyboard_t *kbd; atkbd_state_t *state; keymap_t *keymap; accentmap_t *accmap; fkeytab_t *fkeymap; int fkeymap_size; int delay[2]; int *data = (int *)arg; /* data[0]: controller, data[1]: irq */ int error, needfree; #ifdef EVDEV_SUPPORT struct evdev_dev *evdev; char phys_loc[8]; #endif /* XXX */ if (unit == ATKBD_DEFAULT) { *kbdp = kbd = &default_kbd; if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd)) return 0; state = &default_kbd_state; keymap = &default_keymap; accmap = &default_accentmap; fkeymap = default_fkeytab; fkeymap_size = nitems(default_fkeytab); needfree = 0; } else if (*kbdp == NULL) { *kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_NOWAIT | M_ZERO); state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT | M_ZERO); /* NB: these will always be initialized 'cuz !KBD_IS_PROBED */ keymap = malloc(sizeof(key_map), M_DEVBUF, M_NOWAIT); accmap = malloc(sizeof(accent_map), M_DEVBUF, M_NOWAIT); fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_NOWAIT); fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]); needfree = 1; if ((kbd == NULL) || (state == NULL) || (keymap == NULL) || (accmap == NULL) || (fkeymap == NULL)) { error = ENOMEM; goto bad; } } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) { return 0; } else { kbd = *kbdp; state = (atkbd_state_t *)kbd->kb_data; bzero(state, sizeof(*state)); keymap = kbd->kb_keymap; accmap = kbd->kb_accentmap; fkeymap = kbd->kb_fkeytab; fkeymap_size = kbd->kb_fkeytab_size; needfree = 0; } if (!KBD_IS_PROBED(kbd)) { state->kbdc = atkbdc_open(data[0]); if (state->kbdc == NULL) { error = ENXIO; goto bad; } kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags, 0, 0); bcopy(&key_map, keymap, sizeof(key_map)); bcopy(&accent_map, accmap, sizeof(accent_map)); bcopy(fkey_tab, fkeymap, imin(fkeymap_size * sizeof(fkeymap[0]), sizeof(fkey_tab))); kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size); kbd->kb_data = (void *)state; if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */ if (flags & KB_CONF_FAIL_IF_NO_KBD) { error = ENXIO; goto bad; } } else { KBD_FOUND_DEVICE(kbd); } atkbd_clear_state(kbd); state->ks_mode = K_XLATE; /* * FIXME: set the initial value for lock keys in ks_state * according to the BIOS data? */ KBD_PROBE_DONE(kbd); } if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) { kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY; if (KBD_HAS_DEVICE(kbd) && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config) && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD)) { kbd_unregister(kbd); error = ENXIO; goto bad; } atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state); set_typematic(kbd); delay[0] = kbd->kb_delay1; delay[1] = kbd->kb_delay2; atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); #ifdef EVDEV_SUPPORT /* register as evdev provider on first init */ if (state->ks_evdev == NULL) { snprintf(phys_loc, sizeof(phys_loc), "atkbd%d", unit); evdev = evdev_alloc(); evdev_set_name(evdev, "AT keyboard"); evdev_set_phys(evdev, phys_loc); evdev_set_id(evdev, BUS_I8042, PS2_KEYBOARD_VENDOR, PS2_KEYBOARD_PRODUCT, 0); evdev_set_methods(evdev, kbd, &atkbd_evdev_methods); evdev_support_event(evdev, EV_SYN); evdev_support_event(evdev, EV_KEY); evdev_support_event(evdev, EV_LED); evdev_support_event(evdev, EV_REP); evdev_support_all_known_keys(evdev); evdev_support_led(evdev, LED_NUML); evdev_support_led(evdev, LED_CAPSL); evdev_support_led(evdev, LED_SCROLLL); if (evdev_register_mtx(evdev, &Giant)) evdev_free(evdev); else state->ks_evdev = evdev; state->ks_evdev_state = 0; } #endif KBD_INIT_DONE(kbd); } if (!KBD_IS_CONFIGURED(kbd)) { if (kbd_register(kbd) < 0) { error = ENXIO; goto bad; } KBD_CONFIG_DONE(kbd); } return 0; bad: if (needfree) { if (state != NULL) free(state, M_DEVBUF); if (keymap != NULL) free(keymap, M_DEVBUF); if (accmap != NULL) free(accmap, M_DEVBUF); if (fkeymap != NULL) free(fkeymap, M_DEVBUF); if (kbd != NULL) { free(kbd, M_DEVBUF); *kbdp = NULL; /* insure ref doesn't leak to caller */ } } return error; } /* finish using this keyboard */ static int atkbd_term(keyboard_t *kbd) { atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data; kbd_unregister(kbd); callout_drain(&state->ks_timer); return 0; } /* keyboard interrupt routine */ static int atkbd_intr(keyboard_t *kbd, void *arg) { atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data; int delay[2]; int c; if (!KBD_HAS_DEVICE(kbd)) { /* * The keyboard was not detected before; * it must have been reconnected! */ init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config); KBD_FOUND_DEVICE(kbd); atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state); set_typematic(kbd); delay[0] = kbd->kb_delay1; delay[1] = kbd->kb_delay2; atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); } if (state->ks_polling) return 0; if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) { /* let the callback function to process the input */ (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT, kbd->kb_callback.kc_arg); } else { /* read and discard the input; no one is waiting for input */ do { c = atkbd_read_char(kbd, FALSE); } while (c != NOKEY); } return 0; } /* test the interface to the device */ static int atkbd_test_if(keyboard_t *kbd) { int error; int s; error = 0; empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10); s = spltty(); if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc)) error = EIO; else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0) error = EIO; splx(s); return error; } /* * Enable the access to the device; until this function is called, * the client cannot read from the keyboard. */ static int atkbd_enable(keyboard_t *kbd) { int s; s = spltty(); KBD_ACTIVATE(kbd); splx(s); return 0; } /* disallow the access to the device */ static int atkbd_disable(keyboard_t *kbd) { int s; s = spltty(); KBD_DEACTIVATE(kbd); splx(s); return 0; } /* read one byte from the keyboard if it's allowed */ static int atkbd_read(keyboard_t *kbd, int wait) { int c; if (wait) c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc); else c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc); if (c != -1) ++kbd->kb_count; return (KBD_IS_ACTIVE(kbd) ? c : -1); } /* check if data is waiting */ static int atkbd_check(keyboard_t *kbd) { if (!KBD_IS_ACTIVE(kbd)) return FALSE; return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc); } /* read char from the keyboard */ static u_int atkbd_read_char(keyboard_t *kbd, int wait) { atkbd_state_t *state; u_int action; int scancode; int keycode; state = (atkbd_state_t *)kbd->kb_data; next_code: /* do we have a composed char to return? */ if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) { action = state->ks_composed_char; state->ks_composed_char = 0; if (action > UCHAR_MAX) return ERRKEY; return action; } /* see if there is something in the keyboard port */ if (wait) { do { scancode = read_kbd_data(state->kbdc); } while (scancode == -1); } else { scancode = read_kbd_data_no_wait(state->kbdc); if (scancode == -1) return NOKEY; } ++kbd->kb_count; #if KBDIO_DEBUG >= 10 printf("atkbd_read_char(): scancode:0x%x\n", scancode); #endif #ifdef EVDEV_SUPPORT /* push evdev event */ if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && state->ks_evdev != NULL) { keycode = evdev_scancode2key(&state->ks_evdev_state, scancode); if (keycode != KEY_RESERVED) { evdev_push_event(state->ks_evdev, EV_KEY, (uint16_t)keycode, scancode & 0x80 ? 0 : 1); evdev_sync(state->ks_evdev); } } #endif /* return the byte as is for the K_RAW mode */ if (state->ks_mode == K_RAW) return scancode; /* translate the scan code into a keycode */ keycode = scancode & 0x7F; switch (state->ks_prefix) { case 0x00: /* normal scancode */ switch(scancode) { case 0xB8: /* left alt (compose key) released */ if (state->ks_flags & COMPOSE) { state->ks_flags &= ~COMPOSE; if (state->ks_composed_char > UCHAR_MAX) state->ks_composed_char = 0; } break; case 0x38: /* left alt (compose key) pressed */ if (!(state->ks_flags & COMPOSE)) { state->ks_flags |= COMPOSE; state->ks_composed_char = 0; } break; case 0xE0: case 0xE1: state->ks_prefix = scancode; goto next_code; } break; case 0xE0: /* 0xE0 prefix */ state->ks_prefix = 0; switch (keycode) { case 0x1C: /* right enter key */ keycode = 0x59; break; case 0x1D: /* right ctrl key */ keycode = 0x5A; break; case 0x35: /* keypad divide key */ keycode = 0x5B; break; case 0x37: /* print scrn key */ keycode = 0x5C; break; case 0x38: /* right alt key (alt gr) */ keycode = 0x5D; break; case 0x46: /* ctrl-pause/break on AT 101 (see below) */ keycode = 0x68; break; case 0x47: /* grey home key */ keycode = 0x5E; break; case 0x48: /* grey up arrow key */ keycode = 0x5F; break; case 0x49: /* grey page up key */ keycode = 0x60; break; case 0x4B: /* grey left arrow key */ keycode = 0x61; break; case 0x4D: /* grey right arrow key */ keycode = 0x62; break; case 0x4F: /* grey end key */ keycode = 0x63; break; case 0x50: /* grey down arrow key */ keycode = 0x64; break; case 0x51: /* grey page down key */ keycode = 0x65; break; case 0x52: /* grey insert key */ keycode = 0x66; break; case 0x53: /* grey delete key */ keycode = 0x67; break; /* the following 3 are only used on the MS "Natural" keyboard */ case 0x5b: /* left Window key */ keycode = 0x69; break; case 0x5c: /* right Window key */ keycode = 0x6a; break; case 0x5d: /* menu key */ keycode = 0x6b; break; case 0x5e: /* power key */ keycode = 0x6d; break; case 0x5f: /* sleep key */ keycode = 0x6e; break; case 0x63: /* wake key */ keycode = 0x6f; break; default: /* ignore everything else */ goto next_code; } break; case 0xE1: /* 0xE1 prefix */ /* * The pause/break key on the 101 keyboard produces: * E1-1D-45 E1-9D-C5 * Ctrl-pause/break produces: * E0-46 E0-C6 (See above.) */ state->ks_prefix = 0; if (keycode == 0x1D) state->ks_prefix = 0x1D; goto next_code; /* NOT REACHED */ case 0x1D: /* pause / break */ state->ks_prefix = 0; if (keycode != 0x45) goto next_code; keycode = 0x68; break; } if (kbd->kb_type == KB_84) { switch (keycode) { case 0x37: /* *(numpad)/print screen */ if (state->ks_flags & SHIFTS) keycode = 0x5c; /* print screen */ break; case 0x45: /* num lock/pause */ if (state->ks_flags & CTLS) keycode = 0x68; /* pause */ break; case 0x46: /* scroll lock/break */ if (state->ks_flags & CTLS) keycode = 0x6c; /* break */ break; } } else if (kbd->kb_type == KB_101) { switch (keycode) { case 0x5c: /* print screen */ if (state->ks_flags & ALTS) keycode = 0x54; /* sysrq */ break; case 0x68: /* pause/break */ if (state->ks_flags & CTLS) keycode = 0x6c; /* break */ break; } } /* return the key code in the K_CODE mode */ if (state->ks_mode == K_CODE) return (keycode | (scancode & 0x80)); /* compose a character code */ if (state->ks_flags & COMPOSE) { switch (keycode | (scancode & 0x80)) { /* key pressed, process it */ case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */ state->ks_composed_char *= 10; state->ks_composed_char += keycode - 0x40; if (state->ks_composed_char > UCHAR_MAX) return ERRKEY; goto next_code; case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */ state->ks_composed_char *= 10; state->ks_composed_char += keycode - 0x47; if (state->ks_composed_char > UCHAR_MAX) return ERRKEY; goto next_code; case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */ state->ks_composed_char *= 10; state->ks_composed_char += keycode - 0x4E; if (state->ks_composed_char > UCHAR_MAX) return ERRKEY; goto next_code; case 0x52: /* keypad 0 */ state->ks_composed_char *= 10; if (state->ks_composed_char > UCHAR_MAX) return ERRKEY; goto next_code; /* key released, no interest here */ case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */ case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */ case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */ case 0xD2: /* keypad 0 */ goto next_code; case 0x38: /* left alt key */ break; default: if (state->ks_composed_char > 0) { state->ks_flags &= ~COMPOSE; state->ks_composed_char = 0; return ERRKEY; } break; } } /* keycode to key action */ action = genkbd_keyaction(kbd, keycode, scancode & 0x80, &state->ks_state, &state->ks_accents); if (action == NOKEY) goto next_code; else return action; } /* check if char is waiting */ static int atkbd_check_char(keyboard_t *kbd) { atkbd_state_t *state; if (!KBD_IS_ACTIVE(kbd)) return FALSE; state = (atkbd_state_t *)kbd->kb_data; if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) return TRUE; return kbdc_data_ready(state->kbdc); } /* some useful control functions */ static int atkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) { /* translate LED_XXX bits into the device specific bits */ static u_char ledmap[8] = { 0, 4, 2, 6, 1, 5, 3, 7, }; atkbd_state_t *state = kbd->kb_data; int error; int s; int i; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) int ival; #endif s = spltty(); switch (cmd) { case KDGKBMODE: /* get keyboard mode */ *(int *)arg = state->ks_mode; break; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 7): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSKBMODE: /* set keyboard mode */ switch (*(int *)arg) { case K_XLATE: if (state->ks_mode != K_XLATE) { /* make lock key state and LED state match */ state->ks_state &= ~LOCK_MASK; state->ks_state |= KBD_LED_VAL(kbd); } /* FALLTHROUGH */ case K_RAW: case K_CODE: if (state->ks_mode != *(int *)arg) { atkbd_clear_state(kbd); state->ks_mode = *(int *)arg; } break; default: splx(s); return EINVAL; } break; case KDGETLED: /* get keyboard LED */ *(int *)arg = KBD_LED_VAL(kbd); break; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 66): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSETLED: /* set keyboard LED */ /* NOTE: lock key state in ks_state won't be changed */ if (*(int *)arg & ~LOCK_MASK) { splx(s); return EINVAL; } i = *(int *)arg; /* replace CAPS LED with ALTGR LED for ALTGR keyboards */ if (state->ks_mode == K_XLATE && kbd->kb_keymap->n_keys > ALTGR_OFFSET) { if (i & ALKED) i |= CLKED; else i &= ~CLKED; } if (KBD_HAS_DEVICE(kbd)) { error = write_kbd(state->kbdc, KBDC_SET_LEDS, ledmap[i & LED_MASK]); if (error) { splx(s); return error; } } #ifdef EVDEV_SUPPORT /* push LED states to evdev */ if (state->ks_evdev != NULL && evdev_rcpt_mask & EVDEV_RCPT_HW_KBD) evdev_push_leds(state->ks_evdev, *(int *)arg); #endif KBD_LED_VAL(kbd) = *(int *)arg; break; case KDGKBSTATE: /* get lock key state */ *(int *)arg = state->ks_state & LOCK_MASK; break; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 20): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSKBSTATE: /* set lock key state */ if (*(int *)arg & ~LOCK_MASK) { splx(s); return EINVAL; } state->ks_state &= ~LOCK_MASK; state->ks_state |= *(int *)arg; splx(s); /* set LEDs and quit */ return atkbd_ioctl(kbd, KDSETLED, arg); case KDSETREPEAT: /* set keyboard repeat rate (new interface) */ splx(s); if (!KBD_HAS_DEVICE(kbd)) return 0; i = typematic(((int *)arg)[0], ((int *)arg)[1]); error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i); if (error == 0) { kbd->kb_delay1 = typematic_delay(i); kbd->kb_delay2 = typematic_rate(i); #ifdef EVDEV_SUPPORT if (state->ks_evdev != NULL && evdev_rcpt_mask & EVDEV_RCPT_HW_KBD) evdev_push_repeats(state->ks_evdev, kbd); #endif } return error; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 67): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSETRAD: /* set keyboard repeat rate (old interface) */ splx(s); if (!KBD_HAS_DEVICE(kbd)) return 0; error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg); if (error == 0) { kbd->kb_delay1 = typematic_delay(*(int *)arg); kbd->kb_delay2 = typematic_rate(*(int *)arg); #ifdef EVDEV_SUPPORT if (state->ks_evdev != NULL && evdev_rcpt_mask & EVDEV_RCPT_HW_KBD) evdev_push_repeats(state->ks_evdev, kbd); #endif } return error; case PIO_KEYMAP: /* set keyboard translation table */ case OPIO_KEYMAP: /* set keyboard translation table (compat) */ case PIO_KEYMAPENT: /* set keyboard translation table entry */ case PIO_DEADKEYMAP: /* set accent key translation table */ state->ks_accents = 0; /* FALLTHROUGH */ default: splx(s); return genkbd_commonioctl(kbd, cmd, arg); } splx(s); return 0; } /* lock the access to the keyboard */ static int atkbd_lock(keyboard_t *kbd, int lock) { return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock); } /* clear the internal state of the keyboard */ static void atkbd_clear_state(keyboard_t *kbd) { atkbd_state_t *state; state = (atkbd_state_t *)kbd->kb_data; state->ks_flags = 0; state->ks_polling = 0; state->ks_state &= LOCK_MASK; /* preserve locking key state */ state->ks_accents = 0; state->ks_composed_char = 0; #if 0 state->ks_prefix = 0; /* XXX */ #endif } /* save the internal state */ static int atkbd_get_state(keyboard_t *kbd, void *buf, size_t len) { if (len == 0) return sizeof(atkbd_state_t); if (len < sizeof(atkbd_state_t)) return -1; bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t)); return 0; } /* set the internal state */ static int atkbd_set_state(keyboard_t *kbd, void *buf, size_t len) { if (len < sizeof(atkbd_state_t)) return ENOMEM; if (((atkbd_state_t *)kbd->kb_data)->kbdc != ((atkbd_state_t *)buf)->kbdc) return ENOMEM; bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t)); return 0; } static int atkbd_poll(keyboard_t *kbd, int on) { atkbd_state_t *state; int s; state = (atkbd_state_t *)kbd->kb_data; s = spltty(); if (on) ++state->ks_polling; else --state->ks_polling; splx(s); return 0; } static void atkbd_shutdown_final(void *v) { #ifdef __sparc64__ keyboard_t *kbd = v; KBDC kbdc = ((atkbd_state_t *)kbd->kb_data)->kbdc; /* * Turn off the translation in preparation for handing the keyboard * over to the OFW as the OBP driver doesn't use translation and * also doesn't disable it itself resulting in a broken keymap at * the boot prompt. Also disable the aux port and the interrupts as * the OBP driver doesn't use them, i.e. polls the keyboard. Not * disabling the interrupts doesn't cause real problems but the * responsiveness is a bit better when they are turned off. */ send_kbd_command(kbdc, KBDC_DISABLE_KBD); set_controller_command_byte(kbdc, KBD_AUX_CONTROL_BITS | KBD_KBD_CONTROL_BITS | KBD_TRANSLATION, KBD_DISABLE_AUX_PORT | KBD_DISABLE_KBD_INT | KBD_ENABLE_KBD_PORT); send_kbd_command(kbdc, KBDC_ENABLE_KBD); #endif } static int atkbd_reset(KBDC kbdc, int flags, int c) { /* reset keyboard hardware */ if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) { /* * KEYBOARD ERROR * Keyboard reset may fail either because the keyboard * doen't exist, or because the keyboard doesn't pass * the self-test, or the keyboard controller on the * motherboard and the keyboard somehow fail to shake hands. * It is just possible, particularly in the last case, * that the keyboard controller may be left in a hung state. * test_controller() and test_kbd_port() appear to bring * the keyboard controller back (I don't know why and how, * though.) */ empty_both_buffers(kbdc, 10); test_controller(kbdc); test_kbd_port(kbdc); /* * We could disable the keyboard port and interrupt... but, * the keyboard may still exist (see above). */ set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c); if (bootverbose) printf("atkbd: failed to reset the keyboard.\n"); return (EIO); } return (0); } #ifdef EVDEV_SUPPORT static void atkbd_ev_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, int32_t value) { keyboard_t *kbd = evdev_get_softc(evdev); if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && (type == EV_LED || type == EV_REP)) { mtx_lock(&Giant); kbd_ev_event(kbd, type, code, value); mtx_unlock(&Giant); } } #endif /* local functions */ static int set_typematic(keyboard_t *kbd) { int val, error; atkbd_state_t *state = kbd->kb_data; val = typematic(DEFAULT_DELAY, DEFAULT_RATE); error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, val); if (error == 0) { kbd->kb_delay1 = typematic_delay(val); kbd->kb_delay2 = typematic_rate(val); } return (error); } static int setup_kbd_port(KBDC kbdc, int port, int intr) { if (!set_controller_command_byte(kbdc, KBD_KBD_CONTROL_BITS, ((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT) | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT))) return 1; return 0; } static int get_kbd_echo(KBDC kbdc) { /* enable the keyboard port, but disable the keyboard intr. */ if (setup_kbd_port(kbdc, TRUE, FALSE)) /* CONTROLLER ERROR: there is very little we can do... */ return ENXIO; /* see if something is present */ write_kbd_command(kbdc, KBDC_ECHO); if (read_kbd_data(kbdc) != KBD_ECHO) { empty_both_buffers(kbdc, 10); test_controller(kbdc); test_kbd_port(kbdc); return ENXIO; } /* enable the keyboard port and intr. */ if (setup_kbd_port(kbdc, TRUE, TRUE)) { /* * CONTROLLER ERROR * This is serious; the keyboard intr is left disabled! */ return ENXIO; } return 0; } static int probe_keyboard(KBDC kbdc, int flags) { /* * Don't try to print anything in this function. The low-level * console may not have been initialized yet... */ int err; int c; int m; if (!kbdc_lock(kbdc, TRUE)) { /* driver error? */ return ENXIO; } /* temporarily block data transmission from the keyboard */ write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT); /* flush any noise in the buffer */ empty_both_buffers(kbdc, 100); /* save the current keyboard controller command byte */ m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS; c = get_controller_command_byte(kbdc); if (c == -1) { /* CONTROLLER ERROR */ kbdc_set_device_mask(kbdc, m); kbdc_lock(kbdc, FALSE); return ENXIO; } /* * The keyboard may have been screwed up by the boot block. * We may just be able to recover from error by testing the controller * and the keyboard port. The controller command byte needs to be * saved before this recovery operation, as some controllers seem * to set the command byte to particular values. */ test_controller(kbdc); if (!(flags & KB_CONF_NO_PROBE_TEST)) test_kbd_port(kbdc); err = get_kbd_echo(kbdc); /* * Even if the keyboard doesn't seem to be present (err != 0), * we shall enable the keyboard port and interrupt so that * the driver will be operable when the keyboard is attached * to the system later. It is NOT recommended to hot-plug * the AT keyboard, but many people do so... */ kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS); setup_kbd_port(kbdc, TRUE, TRUE); #if 0 if (err == 0) { kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS); } else { /* try to restore the command byte as before */ set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c); kbdc_set_device_mask(kbdc, m); } #endif kbdc_lock(kbdc, FALSE); return (HAS_QUIRK(kbdc, KBDC_QUIRK_IGNORE_PROBE_RESULT) ? 0 : err); } static int init_keyboard(KBDC kbdc, int *type, int flags) { int codeset; int id; int c; if (!kbdc_lock(kbdc, TRUE)) { /* driver error? */ return EIO; } /* temporarily block data transmission from the keyboard */ write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT); /* save the current controller command byte */ empty_both_buffers(kbdc, 200); c = get_controller_command_byte(kbdc); if (c == -1) { /* CONTROLLER ERROR */ kbdc_lock(kbdc, FALSE); printf("atkbd: unable to get the current command byte value.\n"); return EIO; } if (bootverbose) printf("atkbd: the current kbd controller command byte %04x\n", c); #if 0 /* override the keyboard lock switch */ c |= KBD_OVERRIDE_KBD_LOCK; #endif /* enable the keyboard port, but disable the keyboard intr. */ if (setup_kbd_port(kbdc, TRUE, FALSE)) { /* CONTROLLER ERROR: there is very little we can do... */ printf("atkbd: unable to set the command byte.\n"); kbdc_lock(kbdc, FALSE); return EIO; } if (HAS_QUIRK(kbdc, KBDC_QUIRK_RESET_AFTER_PROBE) && atkbd_reset(kbdc, flags, c)) { kbdc_lock(kbdc, FALSE); return EIO; } /* * Check if we have an XT keyboard before we attempt to reset it. * The procedure assumes that the keyboard and the controller have * been set up properly by BIOS and have not been messed up * during the boot process. */ codeset = -1; if (flags & KB_CONF_ALT_SCANCODESET) /* the user says there is a XT keyboard */ codeset = 1; #ifdef KBD_DETECT_XT_KEYBOARD else if ((c & KBD_TRANSLATION) == 0) { /* SET_SCANCODE_SET is not always supported; ignore error */ if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0) == KBD_ACK) codeset = read_kbd_data(kbdc); } if (bootverbose) printf("atkbd: scancode set %d\n", codeset); #endif /* KBD_DETECT_XT_KEYBOARD */ *type = KB_OTHER; id = get_kbd_id(kbdc); switch(id) { case 0x41ab: /* 101/102/... Enhanced */ case 0x83ab: /* ditto */ case 0x54ab: /* SpaceSaver */ case 0x84ab: /* ditto */ #if 0 case 0x90ab: /* 'G' */ case 0x91ab: /* 'P' */ case 0x92ab: /* 'A' */ #endif *type = KB_101; break; case -1: /* AT 84 keyboard doesn't return ID */ *type = KB_84; break; default: break; } if (bootverbose) printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type); if (!HAS_QUIRK(kbdc, KBDC_QUIRK_RESET_AFTER_PROBE) && atkbd_reset(kbdc, flags, c)) { kbdc_lock(kbdc, FALSE); return EIO; } /* * Allow us to set the XT_KEYBD flag so that keyboards * such as those on the IBM ThinkPad laptop computers can be used * with the standard console driver. */ if (codeset == 1) { if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) { /* XT kbd doesn't need scan code translation */ c &= ~KBD_TRANSLATION; } else { /* * KEYBOARD ERROR * The XT kbd isn't usable unless the proper scan * code set is selected. */ set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c); kbdc_lock(kbdc, FALSE); printf("atkbd: unable to set the XT keyboard mode.\n"); return EIO; } } #if defined(__sparc64__) if (send_kbd_command_and_data( kbdc, KBDC_SET_SCANCODE_SET, 2) != KBD_ACK) { printf("atkbd: can't set translation.\n"); } c |= KBD_TRANSLATION; #endif /* * Some keyboards require a SETLEDS command to be sent after * the reset command before they will send keystrokes to us */ if (HAS_QUIRK(kbdc, KBDC_QUIRK_SETLEDS_ON_INIT) && send_kbd_command_and_data(kbdc, KBDC_SET_LEDS, 0) != KBD_ACK) { printf("atkbd: setleds failed\n"); } if (!ALLOW_DISABLE_KBD(kbdc)) send_kbd_command(kbdc, KBDC_ENABLE_KBD); /* enable the keyboard port and intr. */ if (!set_controller_command_byte(kbdc, KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK, (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK)) | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) { /* * CONTROLLER ERROR * This is serious; we are left with the disabled * keyboard intr. */ set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc) ? 0xff : (KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK), c); kbdc_lock(kbdc, FALSE); printf("atkbd: unable to enable the keyboard port and intr.\n"); return EIO; } kbdc_lock(kbdc, FALSE); return 0; } static int write_kbd(KBDC kbdc, int command, int data) { int s; /* prevent the timeout routine from polling the keyboard */ if (!kbdc_lock(kbdc, TRUE)) return EBUSY; /* disable the keyboard and mouse interrupt */ s = spltty(); #if 0 c = get_controller_command_byte(kbdc); if ((c == -1) || !set_controller_command_byte(kbdc, kbdc_get_device_mask(kbdc), KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { /* CONTROLLER ERROR */ kbdc_lock(kbdc, FALSE); splx(s); return EIO; } /* * Now that the keyboard controller is told not to generate * the keyboard and mouse interrupts, call `splx()' to allow * the other tty interrupts. The clock interrupt may also occur, * but the timeout routine (`scrn_timer()') will be blocked * by the lock flag set via `kbdc_lock()' */ splx(s); #endif if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK) send_kbd_command(kbdc, KBDC_ENABLE_KBD); #if 0 /* restore the interrupts */ if (!set_controller_command_byte(kbdc, kbdc_get_device_mask(kbdc), c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) { /* CONTROLLER ERROR */ } #else splx(s); #endif kbdc_lock(kbdc, FALSE); return 0; } static int get_kbd_id(KBDC kbdc) { int id1, id2; empty_both_buffers(kbdc, 10); id1 = id2 = -1; if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK) return -1; DELAY(10000); /* 10 msec delay */ id1 = read_kbd_data(kbdc); if (id1 != -1) id2 = read_kbd_data(kbdc); if ((id1 == -1) || (id2 == -1)) { empty_both_buffers(kbdc, 10); test_controller(kbdc); test_kbd_port(kbdc); return -1; } return ((id2 << 8) | id1); } static int delays[] = { 250, 500, 750, 1000 }; static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63, 68, 76, 84, 92, 100, 110, 118, 126, 136, 152, 168, 184, 200, 220, 236, 252, 272, 304, 336, 368, 400, 440, 472, 504 }; static int typematic_delay(int i) { return delays[(i >> 5) & 3]; } static int typematic_rate(int i) { return rates[i & 0x1f]; } static int typematic(int delay, int rate) { int value; int i; for (i = nitems(delays) - 1; i > 0; --i) { if (delay >= delays[i]) break; } value = i << 5; for (i = nitems(rates) - 1; i > 0; --i) { if (rate >= rates[i]) break; } value |= i; return value; } Index: stable/12/sys/dev/hyperv/input/hv_kbd.c =================================================================== --- stable/12/sys/dev/hyperv/input/hv_kbd.c (revision 356006) +++ stable/12/sys/dev/hyperv/input/hv_kbd.c (revision 356007) @@ -1,564 +1,564 @@ /*- * Copyright (c) 2017 Microsoft Corp. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "dev/hyperv/input/hv_kbdc.h" #define HVKBD_MTX_LOCK(_m) do { \ mtx_lock(_m); \ } while (0) #define HVKBD_MTX_UNLOCK(_m) do { \ mtx_unlock(_m); \ } while (0) #define HVKBD_MTX_ASSERT(_m, _t) do { \ mtx_assert(_m, _t); \ } while (0) #define HVKBD_LOCK() HVKBD_MTX_LOCK(&Giant) #define HVKBD_UNLOCK() HVKBD_MTX_UNLOCK(&Giant) #define HVKBD_LOCK_ASSERT() HVKBD_MTX_ASSERT(&Giant, MA_OWNED) #define HVKBD_FLAG_POLLING 0x00000002 /* early keyboard probe, not supported */ static int hvkbd_configure(int flags) { return (0); } /* detect a keyboard, not used */ static int hvkbd_probe(int unit, void *arg, int flags) { return (ENXIO); } /* reset and initialize the device, not used */ static int hvkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) { DEBUG_HVKBD(*kbdp, "%s\n", __func__); return (ENXIO); } /* test the interface to the device, not used */ static int hvkbd_test_if(keyboard_t *kbd) { DEBUG_HVKBD(kbd, "%s\n", __func__); return (0); } /* finish using this keyboard, not used */ static int hvkbd_term(keyboard_t *kbd) { DEBUG_HVKBD(kbd, "%s\n", __func__); return (ENXIO); } /* keyboard interrupt routine, not used */ static int hvkbd_intr(keyboard_t *kbd, void *arg) { DEBUG_HVKBD(kbd, "%s\n", __func__); return (0); } /* lock the access to the keyboard, not used */ static int hvkbd_lock(keyboard_t *kbd, int lock) { DEBUG_HVKBD(kbd, "%s\n", __func__); return (1); } /* save the internal state, not used */ static int hvkbd_get_state(keyboard_t *kbd, void *buf, size_t len) { DEBUG_HVKBD(kbd,"%s\n", __func__); return (len == 0) ? 1 : -1; } /* set the internal state, not used */ static int hvkbd_set_state(keyboard_t *kbd, void *buf, size_t len) { DEBUG_HVKBD(kbd, "%s\n", __func__); return (EINVAL); } static int hvkbd_poll(keyboard_t *kbd, int on) { hv_kbd_sc *sc = kbd->kb_data; HVKBD_LOCK(); /* * Keep a reference count on polling to allow recursive * cngrab() during a panic for example. */ if (on) sc->sc_polling++; else if (sc->sc_polling > 0) sc->sc_polling--; if (sc->sc_polling != 0) { sc->sc_flags |= HVKBD_FLAG_POLLING; } else { sc->sc_flags &= ~HVKBD_FLAG_POLLING; } HVKBD_UNLOCK(); return (0); } /* * Enable the access to the device; until this function is called, * the client cannot read from the keyboard. */ static int hvkbd_enable(keyboard_t *kbd) { HVKBD_LOCK(); KBD_ACTIVATE(kbd); HVKBD_UNLOCK(); return (0); } /* disallow the access to the device */ static int hvkbd_disable(keyboard_t *kbd) { DEBUG_HVKBD(kbd, "%s\n", __func__); HVKBD_LOCK(); KBD_DEACTIVATE(kbd); HVKBD_UNLOCK(); return (0); } static void hvkbd_do_poll(hv_kbd_sc *sc, uint8_t wait) { while (!hv_kbd_prod_is_ready(sc)) { hv_kbd_read_channel(sc->hs_chan, sc); if (!wait) break; } } /* check if data is waiting */ /* Currently unused. */ static int hvkbd_check(keyboard_t *kbd) { DEBUG_HVKBD(kbd, "%s\n", __func__); return (0); } /* check if char is waiting */ static int hvkbd_check_char_locked(keyboard_t *kbd) { HVKBD_LOCK_ASSERT(); if (!KBD_IS_ACTIVE(kbd)) return (FALSE); hv_kbd_sc *sc = kbd->kb_data; if (sc->sc_flags & HVKBD_FLAG_POLLING) hvkbd_do_poll(sc, 0); if (hv_kbd_prod_is_ready(sc)) { return (TRUE); } return (FALSE); } static int hvkbd_check_char(keyboard_t *kbd) { int result; HVKBD_LOCK(); result = hvkbd_check_char_locked(kbd); HVKBD_UNLOCK(); return (result); } /* read char from the keyboard */ static uint32_t hvkbd_read_char_locked(keyboard_t *kbd, int wait) { uint32_t scancode = NOKEY; keystroke ks; hv_kbd_sc *sc = kbd->kb_data; HVKBD_LOCK_ASSERT(); if (!KBD_IS_ACTIVE(kbd) || !hv_kbd_prod_is_ready(sc)) return (NOKEY); if (sc->sc_mode == K_RAW) { if (hv_kbd_fetch_top(sc, &ks)) { return (NOKEY); } if ((ks.info & IS_E0) || (ks.info & IS_E1)) { /** * Emulate the generation of E0 or E1 scancode, * the real scancode will be consumed next time. */ if (ks.info & IS_E0) { scancode = XTKBD_EMUL0; ks.info &= ~IS_E0; } else if (ks.info & IS_E1) { scancode = XTKBD_EMUL1; ks.info &= ~IS_E1; } /** * Change the top item to avoid encountering * E0 or E1 twice. */ hv_kbd_modify_top(sc, &ks); } else if (ks.info & IS_UNICODE) { /** * XXX: Hyperv host send unicode to VM through * 'Type clipboard text', the mapping from * unicode to scancode depends on the keymap. * It is so complicated that we do not plan to * support it yet. */ if (bootverbose) device_printf(sc->dev, "Unsupported unicode\n"); hv_kbd_remove_top(sc); return (NOKEY); } else { scancode = ks.makecode; if (ks.info & IS_BREAK) { scancode |= XTKBD_RELEASE; } hv_kbd_remove_top(sc); } } else { if (bootverbose) device_printf(sc->dev, "Unsupported mode: %d\n", sc->sc_mode); } ++kbd->kb_count; DEBUG_HVKBD(kbd, "read scan: 0x%x\n", scancode); return scancode; } /* Currently wait is always false. */ static uint32_t hvkbd_read_char(keyboard_t *kbd, int wait) { uint32_t keycode; HVKBD_LOCK(); keycode = hvkbd_read_char_locked(kbd, wait); HVKBD_UNLOCK(); return (keycode); } /* clear the internal state of the keyboard */ static void hvkbd_clear_state(keyboard_t *kbd) { hv_kbd_sc *sc = kbd->kb_data; sc->sc_state &= LOCK_MASK; /* preserve locking key state */ sc->sc_flags &= ~HVKBD_FLAG_POLLING; } static int hvkbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg) { int i; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) int ival; #endif hv_kbd_sc *sc = kbd->kb_data; switch (cmd) { case KDGKBMODE: *(int *)arg = sc->sc_mode; break; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 7): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSKBMODE: /* set keyboard mode */ DEBUG_HVKBD(kbd, "expected mode: %x\n", *(int *)arg); switch (*(int *)arg) { case K_XLATE: if (sc->sc_mode != K_XLATE) { /* make lock key state and LED state match */ sc->sc_state &= ~LOCK_MASK; sc->sc_state |= KBD_LED_VAL(kbd); } /* FALLTHROUGH */ case K_RAW: case K_CODE: if (sc->sc_mode != *(int *)arg) { DEBUG_HVKBD(kbd, "mod changed to %x\n", *(int *)arg); if ((sc->sc_flags & HVKBD_FLAG_POLLING) == 0) hvkbd_clear_state(kbd); sc->sc_mode = *(int *)arg; } break; default: return (EINVAL); } break; case KDGKBSTATE: /* get lock key state */ *(int *)arg = sc->sc_state & LOCK_MASK; break; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 20): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSKBSTATE: /* set lock key state */ if (*(int *)arg & ~LOCK_MASK) { return (EINVAL); } sc->sc_state &= ~LOCK_MASK; sc->sc_state |= *(int *)arg; return hvkbd_ioctl_locked(kbd, KDSETLED, arg); case KDGETLED: /* get keyboard LED */ *(int *)arg = KBD_LED_VAL(kbd); break; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 66): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSETLED: /* set keyboard LED */ /* NOTE: lock key state in "sc_state" won't be changed */ if (*(int *)arg & ~LOCK_MASK) return (EINVAL); i = *(int *)arg; /* replace CAPS LED with ALTGR LED for ALTGR keyboards */ if (sc->sc_mode == K_XLATE && kbd->kb_keymap->n_keys > ALTGR_OFFSET) { if (i & ALKED) i |= CLKED; else i &= ~CLKED; } if (KBD_HAS_DEVICE(kbd)) { DEBUG_HVSC(sc, "setled 0x%x\n", *(int *)arg); } KBD_LED_VAL(kbd) = *(int *)arg; break; default: return (genkbd_commonioctl(kbd, cmd, arg)); } return (0); } /* some useful control functions */ static int hvkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) { DEBUG_HVKBD(kbd, "%s: %lx start\n", __func__, cmd); HVKBD_LOCK(); int ret = hvkbd_ioctl_locked(kbd, cmd, arg); HVKBD_UNLOCK(); DEBUG_HVKBD(kbd, "%s: %lx end %d\n", __func__, cmd, ret); return (ret); } /* read one byte from the keyboard if it's allowed */ /* Currently unused. */ static int hvkbd_read(keyboard_t *kbd, int wait) { DEBUG_HVKBD(kbd, "%s\n", __func__); HVKBD_LOCK_ASSERT(); if (!KBD_IS_ACTIVE(kbd)) return (-1); return hvkbd_read_char_locked(kbd, wait); } static keyboard_switch_t hvkbdsw = { - hvkbd_probe, /* not used */ - hvkbd_init, - hvkbd_term, /* not used */ - hvkbd_intr, /* not used */ - hvkbd_test_if, /* not used */ - hvkbd_enable, - hvkbd_disable, - hvkbd_read, - hvkbd_check, - hvkbd_read_char, - hvkbd_check_char, - hvkbd_ioctl, - hvkbd_lock, /* not used */ - hvkbd_clear_state, - hvkbd_get_state, /* not used */ - hvkbd_set_state, /* not used */ - genkbd_get_fkeystr, - hvkbd_poll, - genkbd_diag, + .probe = hvkbd_probe, /* not used */ + .init = hvkbd_init, + .term = hvkbd_term, /* not used */ + .intr = hvkbd_intr, /* not used */ + .test_if = hvkbd_test_if, /* not used */ + .enable = hvkbd_enable, + .disable = hvkbd_disable, + .read = hvkbd_read, + .check = hvkbd_check, + .read_char = hvkbd_read_char, + .check_char = hvkbd_check_char, + .ioctl = hvkbd_ioctl, + .lock = hvkbd_lock, /* not used */ + .clear_state = hvkbd_clear_state, + .get_state = hvkbd_get_state, /* not used */ + .set_state = hvkbd_set_state, /* not used */ + .get_fkeystr = genkbd_get_fkeystr, + .poll = hvkbd_poll, + .diag = genkbd_diag, }; KEYBOARD_DRIVER(hvkbd, hvkbdsw, hvkbd_configure); void hv_kbd_intr(hv_kbd_sc *sc) { uint32_t c; if ((sc->sc_flags & HVKBD_FLAG_POLLING) != 0) return; if (KBD_IS_ACTIVE(&sc->sc_kbd) && KBD_IS_BUSY(&sc->sc_kbd)) { /* let the callback function process the input */ (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT, sc->sc_kbd.kb_callback.kc_arg); } else { /* read and discard the input, no one is waiting for it */ do { c = hvkbd_read_char(&sc->sc_kbd, 0); } while (c != NOKEY); } } int hvkbd_driver_load(module_t mod, int what, void *arg) { switch (what) { case MOD_LOAD: kbd_add_driver(&hvkbd_kbd_driver); break; case MOD_UNLOAD: kbd_delete_driver(&hvkbd_kbd_driver); break; } return (0); } int hv_kbd_drv_attach(device_t dev) { hv_kbd_sc *sc = device_get_softc(dev); int unit = device_get_unit(dev); keyboard_t *kbd = &sc->sc_kbd; keyboard_switch_t *sw; sw = kbd_get_switch(HVKBD_DRIVER_NAME); if (sw == NULL) { return (ENXIO); } kbd_init_struct(kbd, HVKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0); kbd->kb_data = (void *)sc; kbd_set_maps(kbd, &key_map, &accent_map, fkey_tab, nitems(fkey_tab)); KBD_FOUND_DEVICE(kbd); hvkbd_clear_state(kbd); KBD_PROBE_DONE(kbd); KBD_INIT_DONE(kbd); sc->sc_mode = K_RAW; (*sw->enable)(kbd); if (kbd_register(kbd) < 0) { goto detach; } KBD_CONFIG_DONE(kbd); #ifdef KBD_INSTALL_CDEV if (kbd_attach(kbd)) { goto detach; } #endif if (bootverbose) { genkbd_diag(kbd, bootverbose); } return (0); detach: hv_kbd_drv_detach(dev); return (ENXIO); } int hv_kbd_drv_detach(device_t dev) { int error = 0; hv_kbd_sc *sc = device_get_softc(dev); hvkbd_disable(&sc->sc_kbd); if (KBD_IS_CONFIGURED(&sc->sc_kbd)) { error = kbd_unregister(&sc->sc_kbd); if (error) { device_printf(dev, "WARNING: kbd_unregister() " "returned non-zero! (ignored)\n"); } } #ifdef KBD_INSTALL_CDEV error = kbd_detach(&sc->sc_kbd); #endif return (error); } Index: stable/12/sys/dev/uart/uart_kbd_sun.c =================================================================== --- stable/12/sys/dev/uart/uart_kbd_sun.c (revision 356006) +++ stable/12/sys/dev/uart/uart_kbd_sun.c (revision 356007) @@ -1,870 +1,870 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2003 Jake Burkholder. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include "opt_kbd.h" #include "opt_sunkbd.h" #if (defined(SUNKBD_EMULATE_ATKBD) && defined(SUNKBD_DFLT_KEYMAP)) || \ !defined(SUNKBD_EMULATE_ATKBD) #define KBD_DFLT_KEYMAP #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if !defined(SUNKBD_EMULATE_ATKBD) #include #endif #if defined(SUNKBD_EMULATE_ATKBD) && defined(SUNKBD_DFLT_KEYMAP) #include "sunkbdmap.h" #endif #include "uart_if.h" #define SUNKBD_DRIVER_NAME "sunkbd" #define TODO printf("%s: unimplemented", __func__) struct sunkbd_softc { keyboard_t sc_kbd; struct uart_softc *sc_uart; struct uart_devinfo *sc_sysdev; struct callout sc_repeat_callout; int sc_repeat_key; int sc_accents; int sc_composed_char; int sc_flags; #define KPCOMPOSE (1 << 0) int sc_mode; int sc_polling; int sc_repeating; int sc_state; #if defined(SUNKBD_EMULATE_ATKBD) int sc_buffered_char[2]; #endif }; static int sunkbd_configure(int flags); static int sunkbd_probe_keyboard(struct uart_devinfo *di); static int sunkbd_probe(int unit, void *arg, int flags); static int sunkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags); static int sunkbd_term(keyboard_t *kbd); static int sunkbd_intr(keyboard_t *kbd, void *arg); static int sunkbd_test_if(keyboard_t *kbd); static int sunkbd_enable(keyboard_t *kbd); static int sunkbd_disable(keyboard_t *kbd); static int sunkbd_read(keyboard_t *kbd, int wait); static int sunkbd_check(keyboard_t *kbd); static u_int sunkbd_read_char(keyboard_t *kbd, int wait); static int sunkbd_check_char(keyboard_t *kbd); static int sunkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t data); static int sunkbd_lock(keyboard_t *kbd, int lock); static void sunkbd_clear_state(keyboard_t *kbd); static int sunkbd_get_state(keyboard_t *kbd, void *buf, size_t len); static int sunkbd_set_state(keyboard_t *kbd, void *buf, size_t len); static int sunkbd_poll_mode(keyboard_t *kbd, int on); static void sunkbd_diag(keyboard_t *kbd, int level); static void sunkbd_repeat(void *v); #if defined(SUNKBD_EMULATE_ATKBD) static int keycode2scancode(int keycode, int shift, int up); #endif static keyboard_switch_t sunkbdsw = { - sunkbd_probe, - sunkbd_init, - sunkbd_term, - sunkbd_intr, - sunkbd_test_if, - sunkbd_enable, - sunkbd_disable, - sunkbd_read, - sunkbd_check, - sunkbd_read_char, - sunkbd_check_char, - sunkbd_ioctl, - sunkbd_lock, - sunkbd_clear_state, - sunkbd_get_state, - sunkbd_set_state, - genkbd_get_fkeystr, - sunkbd_poll_mode, - sunkbd_diag + .probe = sunkbd_probe, + .init = sunkbd_init, + .term = sunkbd_term, + .intr = sunkbd_intr, + .test_if = sunkbd_test_if, + .enable = sunkbd_enable, + .disable = sunkbd_disable, + .read = sunkbd_read, + .check = sunkbd_check, + .read_char = sunkbd_read_char, + .check_char = sunkbd_check_char, + .ioctl = sunkbd_ioctl, + .lock = sunkbd_lock, + .clear_state = sunkbd_clear_state, + .get_state = sunkbd_get_state, + .set_state = sunkbd_set_state, + .get_fkeystr = genkbd_get_fkeystr, + .poll = sunkbd_poll_mode, + .diag = sunkbd_diag }; KEYBOARD_DRIVER(sunkbd, sunkbdsw, sunkbd_configure); static struct sunkbd_softc sunkbd_softc; static struct uart_devinfo uart_keyboard; #if defined(SUNKBD_EMULATE_ATKBD) #define SCAN_PRESS 0x000 #define SCAN_RELEASE 0x080 #define SCAN_PREFIX_E0 0x100 #define SCAN_PREFIX_E1 0x200 #define SCAN_PREFIX_CTL 0x400 #define SCAN_PREFIX_SHIFT 0x800 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \ SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT) #define NOTR 0x0 /* no translation */ static const uint8_t sunkbd_trtab[] = { NOTR, 0x6d, 0x78, 0x6e, 0x79, 0x3b, 0x3c, 0x44, /* 0x00 - 0x07 */ 0x3d, 0x57, 0x3e, 0x58, 0x3f, 0x5d, 0x40, NOTR, /* 0x08 - 0x0f */ 0x41, 0x42, 0x43, 0x38, 0x5f, 0x68, 0x5c, 0x46, /* 0x10 - 0x17 */ 0x61, 0x6f, 0x70, 0x64, 0x62, 0x01, 0x02, 0x03, /* 0x18 - 0x1f */ 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 0x20 - 0x27 */ 0x0c, 0x0d, 0x29, 0x0e, 0x66, 0x77, 0x5b, 0x37, /* 0x28 - 0x2f */ 0x7a, 0x71, 0x53, 0x74, 0x5e, 0x0f, 0x10, 0x11, /* 0x30 - 0x37 */ 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, /* 0x38 - 0x3f */ 0x1a, 0x1b, 0x67, 0x6b, 0x47, 0x48, 0x49, 0x4a, /* 0x40 - 0x47 */ 0x73, 0x72, 0x63, NOTR, 0x1d, 0x1e, 0x1f, 0x20, /* 0x48 - 0x4f */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, /* 0x50 - 0x57 */ 0x2b, 0x1c, 0x59, 0x4b, 0x4c, 0x4d, 0x52, 0x75, /* 0x58 - 0x5f */ 0x60, 0x76, 0x45, 0x2a, 0x2c, 0x2d, 0x2e, 0x2f, /* 0x60 - 0x67 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, NOTR, /* 0x68 - 0x6f */ 0x4f, 0x50, 0x51, NOTR, NOTR, NOTR, 0x6c, 0x3a, /* 0x70 - 0x77 */ 0x69, 0x39, 0x6a, 0x65, 0x56, 0x4e, NOTR, NOTR /* 0x78 - 0x7f */ }; #endif static int sunkbd_probe_keyboard(struct uart_devinfo *di) { int c, id, ltries, tries; for (tries = 5; tries != 0; tries--) { uart_putc(di, SKBD_CMD_RESET); for (ltries = 1000; ltries != 0; ltries--) { if (uart_poll(di) == SKBD_RSP_RESET) break; DELAY(1000); } if (ltries == 0) continue; id = -1; for (ltries = 1000; ltries != 0; ltries--) { switch (c = uart_poll(di)) { case -1: break; case SKBD_RSP_IDLE: return (id); default: id = c; } DELAY(1000); } } return (-1); } static int sunkbd_attach(struct uart_softc *sc); static void sunkbd_uart_intr(void *arg); static int sunkbd_configure(int flags) { struct sunkbd_softc *sc; /* * We are only prepared to be used for the high-level console * when the keyboard is both configured and attached. */ if (!(flags & KB_CONF_PROBE_ONLY)) { if (KBD_IS_INITIALIZED(&sunkbd_softc.sc_kbd)) goto found; else return (0); } if (uart_cpu_getdev(UART_DEV_KEYBOARD, &uart_keyboard)) return (0); if (uart_probe(&uart_keyboard)) return (0); uart_init(&uart_keyboard); uart_keyboard.type = UART_DEV_KEYBOARD; uart_keyboard.attach = sunkbd_attach; uart_add_sysdev(&uart_keyboard); if (sunkbd_probe_keyboard(&uart_keyboard) != KB_SUN4) return (0); sc = &sunkbd_softc; callout_init(&sc->sc_repeat_callout, 0); sunkbd_clear_state(&sc->sc_kbd); #if defined(SUNKBD_EMULATE_ATKBD) kbd_init_struct(&sc->sc_kbd, SUNKBD_DRIVER_NAME, KB_101, 0, 0, 0, 0); kbd_set_maps(&sc->sc_kbd, &key_map, &accent_map, fkey_tab, sizeof(fkey_tab) / sizeof(fkey_tab[0])); #else kbd_init_struct(&sc->sc_kbd, SUNKBD_DRIVER_NAME, KB_OTHER, 0, 0, 0, 0); kbd_set_maps(&sc->sc_kbd, &keymap_sun_us_unix_kbd, &accentmap_sun_us_unix_kbd, fkey_tab, sizeof(fkey_tab) / sizeof(fkey_tab[0])); #endif sc->sc_mode = K_XLATE; kbd_register(&sc->sc_kbd); sc->sc_sysdev = &uart_keyboard; found: /* Return number of found keyboards. */ return (1); } static int sunkbd_attach(struct uart_softc *sc) { /* * Don't attach if we didn't probe the keyboard. Note that * the UART is still marked as a system device in that case. */ if (sunkbd_softc.sc_sysdev == NULL) { device_printf(sc->sc_dev, "keyboard not present\n"); return (0); } if (sc->sc_sysdev != NULL) { sunkbd_softc.sc_uart = sc; #ifdef KBD_INSTALL_CDEV kbd_attach(&sunkbd_softc.sc_kbd); #endif sunkbd_enable(&sunkbd_softc.sc_kbd); swi_add(&tty_intr_event, uart_driver_name, sunkbd_uart_intr, &sunkbd_softc, SWI_TTY, INTR_TYPE_TTY, &sc->sc_softih); sc->sc_opened = 1; KBD_INIT_DONE(&sunkbd_softc.sc_kbd); } return (0); } static void sunkbd_uart_intr(void *arg) { struct sunkbd_softc *sc = arg; int pend; if (sc->sc_uart->sc_leaving) return; pend = atomic_readandclear_32(&sc->sc_uart->sc_ttypend); if (!(pend & SER_INT_MASK)) return; if (pend & SER_INT_RXREADY) { if (KBD_IS_ACTIVE(&sc->sc_kbd) && KBD_IS_BUSY(&sc->sc_kbd)) { sc->sc_kbd.kb_callback.kc_func(&sc->sc_kbd, KBDIO_KEYINPUT, sc->sc_kbd.kb_callback.kc_arg); } } } static int sunkbd_probe(int unit, void *arg, int flags) { TODO; return (0); } static int sunkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) { TODO; return (0); } static int sunkbd_term(keyboard_t *kbd) { TODO; return (0); } static int sunkbd_intr(keyboard_t *kbd, void *arg) { TODO; return (0); } static int sunkbd_test_if(keyboard_t *kbd) { TODO; return (0); } static int sunkbd_enable(keyboard_t *kbd) { KBD_ACTIVATE(kbd); return (0); } static int sunkbd_disable(keyboard_t *kbd) { KBD_DEACTIVATE(kbd); return (0); } static int sunkbd_read(keyboard_t *kbd, int wait) { TODO; return (0); } static int sunkbd_check(keyboard_t *kbd) { struct sunkbd_softc *sc; if (!KBD_IS_ACTIVE(kbd)) return (FALSE); sc = (struct sunkbd_softc *)kbd; #if defined(SUNKBD_EMULATE_ATKBD) if (sc->sc_buffered_char[0]) return (TRUE); #endif if (sc->sc_repeating) return (TRUE); if (sc->sc_uart != NULL && !uart_rx_empty(sc->sc_uart)) return (TRUE); if (sc->sc_polling != 0 && sc->sc_sysdev != NULL && uart_rxready(sc->sc_sysdev)) return (TRUE); return (FALSE); } static u_int sunkbd_read_char(keyboard_t *kbd, int wait) { struct sunkbd_softc *sc; int key, release, repeated, suncode; sc = (struct sunkbd_softc *)kbd; #if defined(SUNKBD_EMULATE_ATKBD) if (sc->sc_mode == K_RAW && sc->sc_buffered_char[0]) { key = sc->sc_buffered_char[0]; if (key & SCAN_PREFIX) { sc->sc_buffered_char[0] = key & ~SCAN_PREFIX; return ((key & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); } else { sc->sc_buffered_char[0] = sc->sc_buffered_char[1]; sc->sc_buffered_char[1] = 0; return (key); } } #endif repeated = 0; if (sc->sc_repeating) { repeated = 1; sc->sc_repeating = 0; callout_reset(&sc->sc_repeat_callout, hz / 10, sunkbd_repeat, sc); suncode = sc->sc_repeat_key; goto process_code; } for (;;) { next_code: if (!(sc->sc_flags & KPCOMPOSE) && (sc->sc_composed_char > 0)) { key = sc->sc_composed_char; sc->sc_composed_char = 0; if (key > UCHAR_MAX) return (ERRKEY); return (key); } if (sc->sc_uart != NULL && !uart_rx_empty(sc->sc_uart)) { suncode = uart_rx_get(sc->sc_uart); } else if (sc->sc_polling != 0 && sc->sc_sysdev != NULL) { if (wait) suncode = uart_getc(sc->sc_sysdev); else if ((suncode = uart_poll(sc->sc_sysdev)) == -1) return (NOKEY); } else { return (NOKEY); } switch (suncode) { case SKBD_RSP_IDLE: break; default: process_code: ++kbd->kb_count; key = SKBD_KEY_CHAR(suncode); release = suncode & SKBD_KEY_RELEASE; if (!repeated) { if (release == 0) { callout_reset(&sc->sc_repeat_callout, hz / 2, sunkbd_repeat, sc); sc->sc_repeat_key = suncode; } else if (sc->sc_repeat_key == key) { callout_stop(&sc->sc_repeat_callout); sc->sc_repeat_key = -1; } } #if defined(SUNKBD_EMULATE_ATKBD) key = sunkbd_trtab[key]; if (key == NOTR) return (NOKEY); if (!repeated) { switch (key) { case 0x1d: /* ctrl */ if (release != 0) sc->sc_flags &= ~CTLS; else sc->sc_flags |= CTLS; break; case 0x2a: /* left shift */ case 0x36: /* right shift */ if (release != 0) sc->sc_flags &= ~SHIFTS; else sc->sc_flags |= SHIFTS; break; case 0x38: /* alt */ case 0x5d: /* altgr */ if (release != 0) sc->sc_flags &= ~ALTS; else sc->sc_flags |= ALTS; break; } } if (sc->sc_mode == K_RAW) { key = keycode2scancode(key, sc->sc_flags, release); if (key & SCAN_PREFIX) { if (key & SCAN_PREFIX_CTL) { sc->sc_buffered_char[0] = 0x1d | (key & SCAN_RELEASE); sc->sc_buffered_char[1] = key & ~SCAN_PREFIX; } else if (key & SCAN_PREFIX_SHIFT) { sc->sc_buffered_char[0] = 0x2a | (key & SCAN_RELEASE); sc->sc_buffered_char[1] = key & ~SCAN_PREFIX_SHIFT; } else { sc->sc_buffered_char[0] = key & ~SCAN_PREFIX; sc->sc_buffered_char[1] = 0; } return ((key & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); } return (key); } switch (key) { case 0x5c: /* print screen */ if (sc->sc_flags & ALTS) key = 0x54; /* sysrq */ break; case 0x68: /* pause/break */ if (sc->sc_flags & CTLS) key = 0x6c; /* break */ break; } if (sc->sc_mode == K_CODE) return (key | release); #else if (sc->sc_mode == K_RAW || sc->sc_mode == K_CODE) return (suncode); #endif #if defined(SUNKBD_EMULATE_ATKBD) if (key == 0x38) { /* left alt (KP compose key) */ #else if (key == 0x13) { /* left alt (KP compose key) */ #endif if (release != 0) { if (sc->sc_flags & KPCOMPOSE) { sc->sc_flags &= ~KPCOMPOSE; if (sc->sc_composed_char > UCHAR_MAX) sc->sc_composed_char = 0; } } else { if (!(sc->sc_flags & KPCOMPOSE)) { sc->sc_flags |= KPCOMPOSE; sc->sc_composed_char = 0; } } } if (sc->sc_flags & KPCOMPOSE) { switch (suncode) { case 0x44: /* KP 7 */ case 0x45: /* KP 8 */ case 0x46: /* KP 9 */ sc->sc_composed_char *= 10; sc->sc_composed_char += suncode - 0x3d; if (sc->sc_composed_char > UCHAR_MAX) return (ERRKEY); goto next_code; case 0x5b: /* KP 4 */ case 0x5c: /* KP 5 */ case 0x5d: /* KP 6 */ sc->sc_composed_char *= 10; sc->sc_composed_char += suncode - 0x58; if (sc->sc_composed_char > UCHAR_MAX) return (ERRKEY); goto next_code; case 0x70: /* KP 1 */ case 0x71: /* KP 2 */ case 0x72: /* KP 3 */ sc->sc_composed_char *= 10; sc->sc_composed_char += suncode - 0x6f; if (sc->sc_composed_char > UCHAR_MAX) return (ERRKEY); goto next_code; case 0x5e: /* KP 0 */ sc->sc_composed_char *= 10; if (sc->sc_composed_char > UCHAR_MAX) return (ERRKEY); goto next_code; case 0x44 | SKBD_KEY_RELEASE: /* KP 7 */ case 0x45 | SKBD_KEY_RELEASE: /* KP 8 */ case 0x46 | SKBD_KEY_RELEASE: /* KP 9 */ case 0x5b | SKBD_KEY_RELEASE: /* KP 4 */ case 0x5c | SKBD_KEY_RELEASE: /* KP 5 */ case 0x5d | SKBD_KEY_RELEASE: /* KP 6 */ case 0x70 | SKBD_KEY_RELEASE: /* KP 1 */ case 0x71 | SKBD_KEY_RELEASE: /* KP 2 */ case 0x72 | SKBD_KEY_RELEASE: /* KP 3 */ case 0x5e | SKBD_KEY_RELEASE: /* KP 0 */ goto next_code; default: if (sc->sc_composed_char > 0) { sc->sc_flags &= ~KPCOMPOSE; sc->sc_composed_char = 0; return (ERRKEY); } } } key = genkbd_keyaction(kbd, key, release, &sc->sc_state, &sc->sc_accents); if (key != NOKEY || repeated) return (key); } } return (0); } static int sunkbd_check_char(keyboard_t *kbd) { struct sunkbd_softc *sc; if (!KBD_IS_ACTIVE(kbd)) return (FALSE); sc = (struct sunkbd_softc *)kbd; if (!(sc->sc_flags & KPCOMPOSE) && (sc->sc_composed_char > 0)) return (TRUE); return (sunkbd_check(kbd)); } static int sunkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t data) { struct sunkbd_softc *sc; int c, error; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) int ival; #endif sc = (struct sunkbd_softc *)kbd; error = 0; switch (cmd) { case KDGKBMODE: *(int *)data = sc->sc_mode; break; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) case _IO('K', 7): ival = IOCPARM_IVAL(data); data = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSKBMODE: switch (*(int *)data) { case K_XLATE: if (sc->sc_mode != K_XLATE) { /* make lock key state and LED state match */ sc->sc_state &= ~LOCK_MASK; sc->sc_state |= KBD_LED_VAL(kbd); } /* FALLTHROUGH */ case K_RAW: case K_CODE: if (sc->sc_mode != *(int *)data) { sunkbd_clear_state(kbd); sc->sc_mode = *(int *)data; } break; default: error = EINVAL; break; } break; case KDGETLED: *(int *)data = KBD_LED_VAL(kbd); break; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) case _IO('K', 66): ival = IOCPARM_IVAL(data); data = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSETLED: if (*(int *)data & ~LOCK_MASK) { error = EINVAL; break; } if (sc->sc_sysdev == NULL) break; c = 0; if (*(int *)data & CLKED) c |= SKBD_LED_CAPSLOCK; if (*(int *)data & NLKED) c |= SKBD_LED_NUMLOCK; if (*(int *)data & SLKED) c |= SKBD_LED_SCROLLLOCK; uart_lock(sc->sc_sysdev->hwmtx); sc->sc_sysdev->ops->putc(&sc->sc_sysdev->bas, SKBD_CMD_SETLED); sc->sc_sysdev->ops->putc(&sc->sc_sysdev->bas, c); uart_unlock(sc->sc_sysdev->hwmtx); KBD_LED_VAL(kbd) = *(int *)data; break; case KDGKBSTATE: *(int *)data = sc->sc_state & LOCK_MASK; break; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) case _IO('K', 20): ival = IOCPARM_IVAL(data); data = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSKBSTATE: if (*(int *)data & ~LOCK_MASK) { error = EINVAL; break; } sc->sc_state &= ~LOCK_MASK; sc->sc_state |= *(int *)data; /* set LEDs and quit */ return (sunkbd_ioctl(kbd, KDSETLED, data)); case KDSETREPEAT: case KDSETRAD: break; case PIO_KEYMAP: case OPIO_KEYMAP: case PIO_KEYMAPENT: case PIO_DEADKEYMAP: default: return (genkbd_commonioctl(kbd, cmd, data)); } return (error); } static int sunkbd_lock(keyboard_t *kbd, int lock) { TODO; return (0); } static void sunkbd_clear_state(keyboard_t *kbd) { struct sunkbd_softc *sc; sc = (struct sunkbd_softc *)kbd; sc->sc_repeat_key = -1; sc->sc_accents = 0; sc->sc_composed_char = 0; sc->sc_flags = 0; sc->sc_polling = 0; sc->sc_repeating = 0; sc->sc_state &= LOCK_MASK; /* Preserve locking key state. */ #if defined(SUNKBD_EMULATE_ATKBD) sc->sc_buffered_char[0] = 0; sc->sc_buffered_char[1] = 0; #endif } static int sunkbd_get_state(keyboard_t *kbd, void *buf, size_t len) { TODO; return (0); } static int sunkbd_set_state(keyboard_t *kbd, void *buf, size_t len) { TODO; return (0); } static int sunkbd_poll_mode(keyboard_t *kbd, int on) { struct sunkbd_softc *sc; sc = (struct sunkbd_softc *)kbd; if (on) sc->sc_polling++; else sc->sc_polling--; return (0); } static void sunkbd_diag(keyboard_t *kbd, int level) { TODO; } static void sunkbd_repeat(void *v) { struct sunkbd_softc *sc = v; if (KBD_IS_ACTIVE(&sc->sc_kbd) && KBD_IS_BUSY(&sc->sc_kbd)) { if (sc->sc_repeat_key != -1) { sc->sc_repeating = 1; sc->sc_kbd.kb_callback.kc_func(&sc->sc_kbd, KBDIO_KEYINPUT, sc->sc_kbd.kb_callback.kc_arg); } } } #if defined(SUNKBD_EMULATE_ATKBD) static int keycode2scancode(int keycode, int shift, int up) { static const int scan[] = { /* KP enter, right ctrl, KP divide */ 0x1c , 0x1d , 0x35 , /* print screen */ 0x37 | SCAN_PREFIX_SHIFT, /* right alt, home, up, page up, left, right, end */ 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f, /* down, page down, insert, delete */ 0x50, 0x51, 0x52, 0x53, /* pause/break (see also below) */ 0x46, /* * MS: left window, right window, menu * also Sun: left meta, right meta, compose */ 0x5b, 0x5c, 0x5d, /* Sun type 6 USB */ /* help, stop, again, props, undo, front, copy */ 0x68, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, /* open, paste, find, cut, audiomute, audiolower, audioraise */ 0x64, 0x65, 0x66, 0x67, 0x25, 0x1f, 0x1e, /* power */ 0x20 }; int scancode; scancode = keycode; if ((keycode >= 89) && (keycode < 89 + nitems(scan))) scancode = scan[keycode - 89] | SCAN_PREFIX_E0; /* pause/break */ if ((keycode == 104) && !(shift & CTLS)) scancode = 0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL; if (shift & SHIFTS) scancode &= ~SCAN_PREFIX_SHIFT; return (scancode | (up ? SCAN_RELEASE : SCAN_PRESS)); } #endif Index: stable/12 =================================================================== --- stable/12 (revision 356006) +++ stable/12 (revision 356007) Property changes on: stable/12 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head:r355794