diff --git a/sys/dev/atkbdc/atkbd.c b/sys/dev/atkbdc/atkbd.c index 3e261115527b..eb76882bf9f7 100644 --- a/sys/dev/atkbdc/atkbd.c +++ b/sys/dev/atkbdc/atkbd.c @@ -1,1603 +1,1606 @@ /*- * 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 #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 SYSCTL_NODE(_hw, OID_AUTO, atkbd, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "AT keyboard"); static int atkbdhz = 0; SYSCTL_INT(_hw_atkbd, OID_AUTO, hz, CTLFLAG_RWTUN, &atkbdhz, 0, "Polling frequency (in hz)"); static void atkbd_timeout(void *arg); 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); 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); if (atkbdhz > 0) { state = (atkbd_state_t *)kbd->kb_data; callout_reset_sbt(&state->ks_timer, SBT_1S / atkbdhz, 0, atkbd_timeout, arg, C_PREL(1)); } } /* 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 = { .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, .poll = atkbd_poll, }; 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) { /* "hancha" and "han/yong" korean keys handling */ if (state->ks_evdev_state == 0 && (scancode == 0xF1 || scancode == 0xF2)) { keycode = evdev_scancode2key(&state->ks_evdev_state, scancode & 0x7F); evdev_push_event(state->ks_evdev, EV_KEY, (uint16_t)keycode, 1); evdev_sync(state->ks_evdev); } 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); } } + + if (state->ks_evdev != NULL && evdev_is_grabbed(state->ks_evdev)) + return (NOKEY); #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 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; } } /* * 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; } diff --git a/sys/dev/evdev/evdev.c b/sys/dev/evdev/evdev.c index a0a039f0b691..2bb365125b5a 100644 --- a/sys/dev/evdev/evdev.c +++ b/sys/dev/evdev/evdev.c @@ -1,1155 +1,1157 @@ /*- * Copyright (c) 2014 Jakub Wojciech Klama * Copyright (c) 2015-2016 Vladimir Kondratyev * 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. * * $FreeBSD$ */ #include "opt_evdev.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef EVDEV_DEBUG #define debugf(evdev, fmt, args...) printf("evdev: " fmt "\n", ##args) #else #define debugf(evdev, fmt, args...) #endif #ifdef FEATURE FEATURE(evdev, "Input event devices support"); #ifdef EVDEV_SUPPORT FEATURE(evdev_support, "Evdev support in hybrid drivers"); #endif #endif enum evdev_sparse_result { EV_SKIP_EVENT, /* Event value not changed */ EV_REPORT_EVENT, /* Event value changed */ EV_REPORT_MT_SLOT, /* Event value and MT slot number changed */ }; MALLOC_DEFINE(M_EVDEV, "evdev", "evdev memory"); /* adb keyboard driver used on powerpc does not support evdev yet */ #if defined(__powerpc__) && !defined(__powerpc64__) int evdev_rcpt_mask = EVDEV_RCPT_KBDMUX | EVDEV_RCPT_HW_MOUSE; #else int evdev_rcpt_mask = EVDEV_RCPT_HW_MOUSE | EVDEV_RCPT_HW_KBD; #endif int evdev_sysmouse_t_axis = 0; SYSCTL_NODE(_kern, OID_AUTO, evdev, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, "Evdev args"); #ifdef EVDEV_SUPPORT SYSCTL_INT(_kern_evdev, OID_AUTO, rcpt_mask, CTLFLAG_RWTUN, &evdev_rcpt_mask, 0, "Who is receiving events: bit0 - sysmouse, bit1 - kbdmux, " "bit2 - mouse hardware, bit3 - keyboard hardware"); SYSCTL_INT(_kern_evdev, OID_AUTO, sysmouse_t_axis, CTLFLAG_RWTUN, &evdev_sysmouse_t_axis, 0, "Extract T-axis from 0-none, 1-ums, 2-psm"); #endif SYSCTL_NODE(_kern_evdev, OID_AUTO, input, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Evdev input devices"); static void evdev_start_repeat(struct evdev_dev *, uint16_t); static void evdev_stop_repeat(struct evdev_dev *); static int evdev_check_event(struct evdev_dev *, uint16_t, uint16_t, int32_t); struct evdev_dev * evdev_alloc(void) { return malloc(sizeof(struct evdev_dev), M_EVDEV, M_WAITOK | M_ZERO); } void evdev_free(struct evdev_dev *evdev) { if (evdev != NULL && evdev->ev_cdev != NULL && evdev->ev_cdev->si_drv1 != NULL) evdev_unregister(evdev); free(evdev, M_EVDEV); } static struct input_absinfo * evdev_alloc_absinfo(void) { return (malloc(sizeof(struct input_absinfo) * ABS_CNT, M_EVDEV, M_WAITOK | M_ZERO)); } static void evdev_free_absinfo(struct input_absinfo *absinfo) { free(absinfo, M_EVDEV); } int evdev_set_report_size(struct evdev_dev *evdev, size_t report_size) { if (report_size > KEY_CNT + REL_CNT + ABS_CNT + MAX_MT_SLOTS * MT_CNT + MSC_CNT + LED_CNT + SND_CNT + SW_CNT + FF_CNT) return (EINVAL); evdev->ev_report_size = report_size; return (0); } static size_t evdev_estimate_report_size(struct evdev_dev *evdev) { size_t size = 0; int res; /* * Keyboards generate one event per report but other devices with * buttons like mouses can report events simultaneously */ bit_ffs_at(evdev->ev_key_flags, KEY_OK, KEY_CNT - KEY_OK, &res); if (res == -1) bit_ffs(evdev->ev_key_flags, BTN_MISC, &res); size += (res != -1); bit_count(evdev->ev_key_flags, BTN_MISC, KEY_OK - BTN_MISC, &res); size += res; /* All relative axes can be reported simultaneously */ bit_count(evdev->ev_rel_flags, 0, REL_CNT, &res); size += res; /* * All absolute axes can be reported simultaneously. * Multitouch axes can be reported ABS_MT_SLOT times */ if (evdev->ev_absinfo != NULL) { bit_count(evdev->ev_abs_flags, 0, ABS_CNT, &res); size += res; bit_count(evdev->ev_abs_flags, ABS_MT_FIRST, MT_CNT, &res); if (res > 0) { res++; /* ABS_MT_SLOT or SYN_MT_REPORT */ if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) /* MT type B */ size += res * MAXIMAL_MT_SLOT(evdev); else /* MT type A */ size += res * (MAX_MT_REPORTS - 1); } } /* All misc events can be reported simultaneously */ bit_count(evdev->ev_msc_flags, 0, MSC_CNT, &res); size += res; /* All leds can be reported simultaneously */ bit_count(evdev->ev_led_flags, 0, LED_CNT, &res); size += res; /* Assume other events are generated once per report */ bit_ffs(evdev->ev_snd_flags, SND_CNT, &res); size += (res != -1); bit_ffs(evdev->ev_sw_flags, SW_CNT, &res); size += (res != -1); /* XXX: FF part is not implemented yet */ size++; /* SYN_REPORT */ return (size); } static void evdev_sysctl_create(struct evdev_dev *evdev) { struct sysctl_oid *ev_sysctl_tree; char ev_unit_str[8]; snprintf(ev_unit_str, sizeof(ev_unit_str), "%d", evdev->ev_unit); sysctl_ctx_init(&evdev->ev_sysctl_ctx); ev_sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&evdev->ev_sysctl_ctx, SYSCTL_STATIC_CHILDREN(_kern_evdev_input), OID_AUTO, ev_unit_str, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "", "device index"); SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "name", CTLFLAG_RD, evdev->ev_name, 0, "Input device name"); SYSCTL_ADD_STRUCT(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "id", CTLFLAG_RD, &evdev->ev_id, input_id, "Input device identification"); /* ioctl returns ENOENT if phys is not set. sysctl returns "" here */ SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "phys", CTLFLAG_RD, evdev->ev_shortname, 0, "Input device short name"); /* ioctl returns ENOENT if uniq is not set. sysctl returns "" here */ SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "uniq", CTLFLAG_RD, evdev->ev_serial, 0, "Input device unique number"); SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "props", CTLFLAG_RD, evdev->ev_prop_flags, sizeof(evdev->ev_prop_flags), "", "Input device properties"); SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "type_bits", CTLFLAG_RD, evdev->ev_type_flags, sizeof(evdev->ev_type_flags), "", "Input device supported events types"); SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "key_bits", CTLFLAG_RD, evdev->ev_key_flags, sizeof(evdev->ev_key_flags), "", "Input device supported keys"); SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "rel_bits", CTLFLAG_RD, evdev->ev_rel_flags, sizeof(evdev->ev_rel_flags), "", "Input device supported relative events"); SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "abs_bits", CTLFLAG_RD, evdev->ev_abs_flags, sizeof(evdev->ev_abs_flags), "", "Input device supported absolute events"); SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "msc_bits", CTLFLAG_RD, evdev->ev_msc_flags, sizeof(evdev->ev_msc_flags), "", "Input device supported miscellaneous events"); SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "led_bits", CTLFLAG_RD, evdev->ev_led_flags, sizeof(evdev->ev_led_flags), "", "Input device supported LED events"); SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "snd_bits", CTLFLAG_RD, evdev->ev_snd_flags, sizeof(evdev->ev_snd_flags), "", "Input device supported sound events"); SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "sw_bits", CTLFLAG_RD, evdev->ev_sw_flags, sizeof(evdev->ev_sw_flags), "", "Input device supported switch events"); } static int evdev_register_common(struct evdev_dev *evdev) { int ret; debugf(evdev, "%s: registered evdev provider: %s <%s>\n", evdev->ev_shortname, evdev->ev_name, evdev->ev_serial); /* Initialize internal structures */ CK_SLIST_INIT(&evdev->ev_clients); sx_init(&evdev->ev_list_lock, "evsx"); if (evdev_event_supported(evdev, EV_REP) && bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) { /* Initialize callout */ callout_init_mtx(&evdev->ev_rep_callout, evdev->ev_state_lock, 0); if (evdev->ev_rep[REP_DELAY] == 0 && evdev->ev_rep[REP_PERIOD] == 0) { /* Supply default values */ evdev->ev_rep[REP_DELAY] = 250; evdev->ev_rep[REP_PERIOD] = 33; } } /* Initialize multitouch protocol type B states or A to B converter */ if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT) || bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK)) evdev_mt_init(evdev); /* Estimate maximum report size */ if (evdev->ev_report_size == 0) { ret = evdev_set_report_size(evdev, evdev_estimate_report_size(evdev)); if (ret != 0) goto bail_out; } /* Create char device node */ ret = evdev_cdev_create(evdev); if (ret != 0) goto bail_out; /* Create sysctls (for device enumeration without /dev/input access rights) */ evdev_sysctl_create(evdev); bail_out: if (ret != 0) sx_destroy(&evdev->ev_list_lock); return (ret); } int evdev_register(struct evdev_dev *evdev) { int ret; if (bit_test(evdev->ev_flags, EVDEV_FLAG_EXT_EPOCH)) evdev->ev_lock_type = EV_LOCK_EXT_EPOCH; else evdev->ev_lock_type = EV_LOCK_INTERNAL; evdev->ev_state_lock = &evdev->ev_mtx; mtx_init(&evdev->ev_mtx, "evmtx", NULL, MTX_DEF); ret = evdev_register_common(evdev); if (ret != 0) mtx_destroy(&evdev->ev_mtx); return (ret); } int evdev_register_mtx(struct evdev_dev *evdev, struct mtx *mtx) { evdev->ev_lock_type = EV_LOCK_MTX; evdev->ev_state_lock = mtx; return (evdev_register_common(evdev)); } int evdev_unregister(struct evdev_dev *evdev) { struct evdev_client *client, *tmp; int ret; debugf(evdev, "%s: unregistered evdev provider: %s\n", evdev->ev_shortname, evdev->ev_name); sysctl_ctx_free(&evdev->ev_sysctl_ctx); EVDEV_LIST_LOCK(evdev); evdev->ev_cdev->si_drv1 = NULL; /* Wake up sleepers */ CK_SLIST_FOREACH_SAFE(client, &evdev->ev_clients, ec_link, tmp) { evdev_revoke_client(client); evdev_dispose_client(evdev, client); EVDEV_CLIENT_LOCKQ(client); evdev_notify_event(client); EVDEV_CLIENT_UNLOCKQ(client); } EVDEV_LIST_UNLOCK(evdev); /* release lock to avoid deadlock with evdev_dtor */ ret = evdev_cdev_destroy(evdev); evdev->ev_cdev = NULL; sx_destroy(&evdev->ev_list_lock); if (ret == 0 && evdev->ev_lock_type != EV_LOCK_MTX) mtx_destroy(&evdev->ev_mtx); evdev_free_absinfo(evdev->ev_absinfo); evdev_mt_free(evdev); return (ret); } inline void evdev_set_name(struct evdev_dev *evdev, const char *name) { snprintf(evdev->ev_name, NAMELEN, "%s", name); } inline void evdev_set_id(struct evdev_dev *evdev, uint16_t bustype, uint16_t vendor, uint16_t product, uint16_t version) { evdev->ev_id = (struct input_id) { .bustype = bustype, .vendor = vendor, .product = product, .version = version }; } inline void evdev_set_phys(struct evdev_dev *evdev, const char *name) { snprintf(evdev->ev_shortname, NAMELEN, "%s", name); } inline void evdev_set_serial(struct evdev_dev *evdev, const char *serial) { snprintf(evdev->ev_serial, NAMELEN, "%s", serial); } inline void evdev_set_methods(struct evdev_dev *evdev, void *softc, const struct evdev_methods *methods) { evdev->ev_methods = methods; evdev->ev_softc = softc; } inline void * evdev_get_softc(struct evdev_dev *evdev) { return (evdev->ev_softc); } inline void evdev_support_prop(struct evdev_dev *evdev, uint16_t prop) { KASSERT(prop < INPUT_PROP_CNT, ("invalid evdev input property")); bit_set(evdev->ev_prop_flags, prop); } inline void evdev_support_event(struct evdev_dev *evdev, uint16_t type) { KASSERT(type < EV_CNT, ("invalid evdev event property")); bit_set(evdev->ev_type_flags, type); } inline void evdev_support_key(struct evdev_dev *evdev, uint16_t code) { KASSERT(code < KEY_CNT, ("invalid evdev key property")); bit_set(evdev->ev_key_flags, code); } inline void evdev_support_rel(struct evdev_dev *evdev, uint16_t code) { KASSERT(code < REL_CNT, ("invalid evdev rel property")); bit_set(evdev->ev_rel_flags, code); } inline void evdev_support_abs(struct evdev_dev *evdev, uint16_t code, int32_t minimum, int32_t maximum, int32_t fuzz, int32_t flat, int32_t resolution) { struct input_absinfo absinfo; KASSERT(code < ABS_CNT, ("invalid evdev abs property")); absinfo = (struct input_absinfo) { .value = 0, .minimum = minimum, .maximum = maximum, .fuzz = fuzz, .flat = flat, .resolution = resolution, }; evdev_set_abs_bit(evdev, code); evdev_set_absinfo(evdev, code, &absinfo); } inline void evdev_set_abs_bit(struct evdev_dev *evdev, uint16_t code) { KASSERT(code < ABS_CNT, ("invalid evdev abs property")); if (evdev->ev_absinfo == NULL) evdev->ev_absinfo = evdev_alloc_absinfo(); bit_set(evdev->ev_abs_flags, code); } inline void evdev_support_msc(struct evdev_dev *evdev, uint16_t code) { KASSERT(code < MSC_CNT, ("invalid evdev msc property")); bit_set(evdev->ev_msc_flags, code); } inline void evdev_support_led(struct evdev_dev *evdev, uint16_t code) { KASSERT(code < LED_CNT, ("invalid evdev led property")); bit_set(evdev->ev_led_flags, code); } inline void evdev_support_snd(struct evdev_dev *evdev, uint16_t code) { KASSERT(code < SND_CNT, ("invalid evdev snd property")); bit_set(evdev->ev_snd_flags, code); } inline void evdev_support_sw(struct evdev_dev *evdev, uint16_t code) { KASSERT(code < SW_CNT, ("invalid evdev sw property")); bit_set(evdev->ev_sw_flags, code); } bool evdev_event_supported(struct evdev_dev *evdev, uint16_t type) { KASSERT(type < EV_CNT, ("invalid evdev event property")); return (bit_test(evdev->ev_type_flags, type)); } inline void evdev_set_absinfo(struct evdev_dev *evdev, uint16_t axis, struct input_absinfo *absinfo) { KASSERT(axis < ABS_CNT, ("invalid evdev abs property")); if (axis == ABS_MT_SLOT && (absinfo->maximum < 1 || absinfo->maximum >= MAX_MT_SLOTS)) return; if (evdev->ev_absinfo == NULL) evdev->ev_absinfo = evdev_alloc_absinfo(); if (axis == ABS_MT_SLOT) evdev->ev_absinfo[ABS_MT_SLOT].maximum = absinfo->maximum; else memcpy(&evdev->ev_absinfo[axis], absinfo, sizeof(struct input_absinfo)); } inline void evdev_set_repeat_params(struct evdev_dev *evdev, uint16_t property, int value) { KASSERT(property < REP_CNT, ("invalid evdev repeat property")); evdev->ev_rep[property] = value; } inline void evdev_set_flag(struct evdev_dev *evdev, uint16_t flag) { KASSERT(flag < EVDEV_FLAG_CNT, ("invalid evdev flag property")); bit_set(evdev->ev_flags, flag); } static int evdev_check_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, int32_t value) { if (type >= EV_CNT) return (EINVAL); /* Allow SYN events implicitly */ if (type != EV_SYN && !evdev_event_supported(evdev, type)) return (EINVAL); switch (type) { case EV_SYN: if (code >= SYN_CNT) return (EINVAL); break; case EV_KEY: if (code >= KEY_CNT) return (EINVAL); if (!bit_test(evdev->ev_key_flags, code)) return (EINVAL); break; case EV_REL: if (code >= REL_CNT) return (EINVAL); if (!bit_test(evdev->ev_rel_flags, code)) return (EINVAL); break; case EV_ABS: if (code >= ABS_CNT) return (EINVAL); if (!bit_test(evdev->ev_abs_flags, code)) return (EINVAL); if (code == ABS_MT_SLOT && (value < 0 || value > MAXIMAL_MT_SLOT(evdev))) return (EINVAL); if (ABS_IS_MT(code) && evdev->ev_mt == NULL && bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) return (EINVAL); break; case EV_MSC: if (code >= MSC_CNT) return (EINVAL); if (!bit_test(evdev->ev_msc_flags, code)) return (EINVAL); break; case EV_LED: if (code >= LED_CNT) return (EINVAL); if (!bit_test(evdev->ev_led_flags, code)) return (EINVAL); break; case EV_SND: if (code >= SND_CNT) return (EINVAL); if (!bit_test(evdev->ev_snd_flags, code)) return (EINVAL); break; case EV_SW: if (code >= SW_CNT) return (EINVAL); if (!bit_test(evdev->ev_sw_flags, code)) return (EINVAL); break; case EV_REP: if (code >= REP_CNT) return (EINVAL); break; default: return (EINVAL); } return (0); } static void evdev_modify_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, int32_t *value) { int32_t fuzz, old_value, abs_change; EVDEV_LOCK_ASSERT(evdev); switch (type) { case EV_KEY: if (!evdev_event_supported(evdev, EV_REP)) break; if (!bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) { /* Detect driver key repeats. */ if (bit_test(evdev->ev_key_states, code) && *value == KEY_EVENT_DOWN) *value = KEY_EVENT_REPEAT; } else { /* Start/stop callout for evdev repeats */ if (bit_test(evdev->ev_key_states, code) == !*value && !CK_SLIST_EMPTY(&evdev->ev_clients)) { if (*value == KEY_EVENT_DOWN) evdev_start_repeat(evdev, code); else evdev_stop_repeat(evdev); } } break; case EV_ABS: if (code == ABS_MT_SLOT) break; else if (!ABS_IS_MT(code)) old_value = evdev->ev_absinfo[code].value; else if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) /* Pass MT protocol type A events as is */ break; else if (code == ABS_MT_TRACKING_ID) { *value = evdev_mt_reassign_id(evdev, evdev_mt_get_last_slot(evdev), *value); break; } else old_value = evdev_mt_get_value(evdev, evdev_mt_get_last_slot(evdev), code); fuzz = evdev->ev_absinfo[code].fuzz; if (fuzz == 0) break; abs_change = abs(*value - old_value); if (abs_change < fuzz / 2) *value = old_value; else if (abs_change < fuzz) *value = (old_value * 3 + *value) / 4; else if (abs_change < fuzz * 2) *value = (old_value + *value) / 2; break; } } static enum evdev_sparse_result evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, int32_t value) { int32_t last_mt_slot; EVDEV_LOCK_ASSERT(evdev); /* * For certain event types, update device state bits * and convert level reporting to edge reporting */ switch (type) { case EV_KEY: switch (value) { case KEY_EVENT_UP: case KEY_EVENT_DOWN: if (bit_test(evdev->ev_key_states, code) == value) return (EV_SKIP_EVENT); bit_change(evdev->ev_key_states, code, value); break; case KEY_EVENT_REPEAT: if (bit_test(evdev->ev_key_states, code) == 0 || !evdev_event_supported(evdev, EV_REP)) return (EV_SKIP_EVENT); break; default: return (EV_SKIP_EVENT); } break; case EV_LED: if (bit_test(evdev->ev_led_states, code) == value) return (EV_SKIP_EVENT); bit_change(evdev->ev_led_states, code, value); break; case EV_SND: bit_change(evdev->ev_snd_states, code, value); break; case EV_SW: if (bit_test(evdev->ev_sw_states, code) == value) return (EV_SKIP_EVENT); bit_change(evdev->ev_sw_states, code, value); break; case EV_REP: if (evdev->ev_rep[code] == value) return (EV_SKIP_EVENT); evdev_set_repeat_params(evdev, code, value); break; case EV_REL: if (value == 0) return (EV_SKIP_EVENT); break; /* For EV_ABS, save last value in absinfo and ev_mt_states */ case EV_ABS: switch (code) { case ABS_MT_SLOT: /* Postpone ABS_MT_SLOT till next event */ evdev_mt_set_last_slot(evdev, value); return (EV_SKIP_EVENT); case ABS_MT_FIRST ... ABS_MT_LAST: /* Pass MT protocol type A events as is */ if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) break; /* Don`t repeat MT protocol type B events */ last_mt_slot = evdev_mt_get_last_slot(evdev); if (evdev_mt_get_value(evdev, last_mt_slot, code) == value) return (EV_SKIP_EVENT); evdev_mt_set_value(evdev, last_mt_slot, code, value); if (last_mt_slot != CURRENT_MT_SLOT(evdev)) { CURRENT_MT_SLOT(evdev) = last_mt_slot; evdev->ev_report_opened = true; return (EV_REPORT_MT_SLOT); } break; default: if (evdev->ev_absinfo[code].value == value) return (EV_SKIP_EVENT); evdev->ev_absinfo[code].value = value; } break; case EV_SYN: if (code == SYN_REPORT) { /* Count empty reports as well as non empty */ evdev->ev_report_count++; /* Skip empty reports */ if (!evdev->ev_report_opened) return (EV_SKIP_EVENT); evdev->ev_report_opened = false; return (EV_REPORT_EVENT); } break; } evdev->ev_report_opened = true; return (EV_REPORT_EVENT); } static void evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, int32_t value) { struct epoch_tracker et; struct evdev_client *client; debugf(evdev, "%s pushed event %d/%d/%d", evdev->ev_shortname, type, code, value); EVDEV_LOCK_ASSERT(evdev); /* Propagate event through all clients */ if (evdev->ev_lock_type == EV_LOCK_INTERNAL) epoch_enter_preempt(INPUT_EPOCH, &et); KASSERT( evdev->ev_lock_type == EV_LOCK_MTX || in_epoch(INPUT_EPOCH) != 0, ("Input epoch has not been entered\n")); CK_SLIST_FOREACH(client, &evdev->ev_clients, ec_link) { if (evdev->ev_grabber != NULL && evdev->ev_grabber != client) continue; EVDEV_CLIENT_LOCKQ(client); evdev_client_push(client, type, code, value); if (type == EV_SYN && code == SYN_REPORT) evdev_notify_event(client); EVDEV_CLIENT_UNLOCKQ(client); } if (evdev->ev_lock_type == EV_LOCK_INTERNAL) epoch_exit_preempt(INPUT_EPOCH, &et); evdev->ev_event_count++; } void evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, int32_t value) { enum evdev_sparse_result sparse; EVDEV_LOCK_ASSERT(evdev); evdev_modify_event(evdev, type, code, &value); sparse = evdev_sparse_event(evdev, type, code, value); switch (sparse) { case EV_REPORT_MT_SLOT: /* report postponed ABS_MT_SLOT */ evdev_propagate_event(evdev, EV_ABS, ABS_MT_SLOT, CURRENT_MT_SLOT(evdev)); /* FALLTHROUGH */ case EV_REPORT_EVENT: evdev_propagate_event(evdev, type, code, value); /* FALLTHROUGH */ case EV_SKIP_EVENT: break; } } void evdev_restore_after_kdb(struct evdev_dev *evdev) { int code; EVDEV_LOCK_ASSERT(evdev); /* Report postponed leds */ bit_foreach(evdev->ev_kdb_led_states, LED_CNT, code) evdev_send_event(evdev, EV_LED, code, !bit_test(evdev->ev_led_states, code)); bit_nclear(evdev->ev_kdb_led_states, 0, LED_MAX); /* Release stuck keys (CTRL + ALT + ESC) */ evdev_stop_repeat(evdev); bit_foreach(evdev->ev_key_states, KEY_CNT, code) evdev_send_event(evdev, EV_KEY, code, KEY_EVENT_UP); evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1); } int evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, int32_t value) { if (evdev_check_event(evdev, type, code, value) != 0) return (EINVAL); /* * Discard all but LEDs kdb events as unrelated to userspace. * Aggregate LED updates and postpone reporting until kdb deactivation. */ if (kdb_active || SCHEDULER_STOPPED()) { evdev->ev_kdb_active = true; if (type == EV_LED) bit_set(evdev->ev_kdb_led_states, bit_test(evdev->ev_led_states, code) != value); return (0); } EVDEV_ENTER(evdev); /* Fix evdev state corrupted with discarding of kdb events */ if (evdev->ev_kdb_active) { evdev->ev_kdb_active = false; evdev_restore_after_kdb(evdev); } if (type == EV_SYN && code == SYN_REPORT && bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) evdev_mt_sync_frame(evdev); else if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK) && evdev_mt_record_event(evdev, type, code, value)) goto exit; evdev_send_event(evdev, type, code, value); exit: EVDEV_EXIT(evdev); return (0); } int evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, int32_t value) { struct epoch_tracker et; int ret = 0; switch (type) { case EV_REP: /* evdev repeats should not be processed by hardware driver */ if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) goto push; /* FALLTHROUGH */ case EV_LED: case EV_MSC: case EV_SND: case EV_FF: if (evdev->ev_methods != NULL && evdev->ev_methods->ev_event != NULL) evdev->ev_methods->ev_event(evdev, type, code, value); /* * Leds and driver repeats should be reported in ev_event * method body to interoperate with kbdmux states and rates * propagation so both ways (ioctl and evdev) of changing it * will produce only one evdev event report to client. */ if (type == EV_LED || type == EV_REP) break; /* FALLTHROUGH */ case EV_SYN: case EV_KEY: case EV_REL: case EV_ABS: case EV_SW: push: if (evdev->ev_lock_type == EV_LOCK_MTX) EVDEV_LOCK(evdev); else if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH) epoch_enter_preempt(INPUT_EPOCH, &et); ret = evdev_push_event(evdev, type, code, value); if (evdev->ev_lock_type == EV_LOCK_MTX) EVDEV_UNLOCK(evdev); else if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH) epoch_exit_preempt(INPUT_EPOCH, &et); break; default: ret = EINVAL; } return (ret); } int evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client) { int ret = 0; debugf(evdev, "adding new client for device %s", evdev->ev_shortname); EVDEV_LIST_LOCK_ASSERT(evdev); if (CK_SLIST_EMPTY(&evdev->ev_clients) && evdev->ev_methods != NULL && evdev->ev_methods->ev_open != NULL) { debugf(evdev, "calling ev_open() on device %s", evdev->ev_shortname); ret = evdev->ev_methods->ev_open(evdev); } if (ret == 0) CK_SLIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link); return (ret); } void evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client) { debugf(evdev, "removing client for device %s", evdev->ev_shortname); EVDEV_LIST_LOCK_ASSERT(evdev); CK_SLIST_REMOVE(&evdev->ev_clients, client, evdev_client, ec_link); if (CK_SLIST_EMPTY(&evdev->ev_clients)) { if (evdev->ev_methods != NULL && evdev->ev_methods->ev_close != NULL) (void)evdev->ev_methods->ev_close(evdev); if (evdev_event_supported(evdev, EV_REP) && bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) { if (evdev->ev_lock_type != EV_LOCK_MTX) EVDEV_LOCK(evdev); evdev_stop_repeat(evdev); if (evdev->ev_lock_type != EV_LOCK_MTX) EVDEV_UNLOCK(evdev); } } if (evdev->ev_lock_type != EV_LOCK_MTX) EVDEV_LOCK(evdev); evdev_release_client(evdev, client); if (evdev->ev_lock_type != EV_LOCK_MTX) EVDEV_UNLOCK(evdev); } int evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client) { EVDEV_LOCK_ASSERT(evdev); if (evdev->ev_grabber != NULL) return (EBUSY); evdev->ev_grabber = client; return (0); } int evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client) { EVDEV_LOCK_ASSERT(evdev); if (evdev->ev_grabber != client) return (EINVAL); evdev->ev_grabber = NULL; return (0); } bool evdev_is_grabbed(struct evdev_dev *evdev) { + if (kdb_active || SCHEDULER_STOPPED()) + return (false); /* * The function is intended to be called from evdev-unrelated parts of * code like syscons-compatible parts of mouse and keyboard drivers. * That makes unlocked read-only access acceptable. */ return (evdev->ev_grabber != NULL); } static void evdev_repeat_callout(void *arg) { struct epoch_tracker et; struct evdev_dev *evdev = (struct evdev_dev *)arg; if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH) epoch_enter_preempt(INPUT_EPOCH, &et); evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT); evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1); if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH) epoch_exit_preempt(INPUT_EPOCH, &et); if (evdev->ev_rep[REP_PERIOD]) callout_reset(&evdev->ev_rep_callout, evdev->ev_rep[REP_PERIOD] * hz / 1000, evdev_repeat_callout, evdev); else evdev->ev_rep_key = KEY_RESERVED; } static void evdev_start_repeat(struct evdev_dev *evdev, uint16_t key) { EVDEV_LOCK_ASSERT(evdev); if (evdev->ev_rep[REP_DELAY]) { evdev->ev_rep_key = key; callout_reset(&evdev->ev_rep_callout, evdev->ev_rep[REP_DELAY] * hz / 1000, evdev_repeat_callout, evdev); } } static void evdev_stop_repeat(struct evdev_dev *evdev) { EVDEV_LOCK_ASSERT(evdev); if (evdev->ev_rep_key != KEY_RESERVED) { callout_stop(&evdev->ev_rep_callout); evdev->ev_rep_key = KEY_RESERVED; } } MODULE_VERSION(evdev, 1); diff --git a/sys/dev/gpio/gpiokeys.c b/sys/dev/gpio/gpiokeys.c index bea5844bad5e..36746923f01b 100644 --- a/sys/dev/gpio/gpiokeys.c +++ b/sys/dev/gpio/gpiokeys.c @@ -1,1056 +1,1060 @@ /*- * Copyright (c) 2015-2016 Oleksandr Tymoshenko * 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_platform.h" #include "opt_kbd.h" #include "opt_evdev.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef EVDEV_SUPPORT #include #include #endif #define KBD_DRIVER_NAME "gpiokeys" #define GPIOKEYS_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) #define GPIOKEYS_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) #define GPIOKEYS_LOCK_INIT(_sc) \ mtx_init(&_sc->sc_mtx, device_get_nameunit((_sc)->sc_dev), \ "gpiokeys", MTX_DEF) #define GPIOKEYS_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx); #define GPIOKEYS_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED) #define GPIOKEY_LOCK(_key) mtx_lock(&(_key)->mtx) #define GPIOKEY_UNLOCK(_key) mtx_unlock(&(_key)->mtx) #define GPIOKEY_LOCK_INIT(_key) \ mtx_init(&(_key)->mtx, "gpiokey", "gpiokey", MTX_DEF) #define GPIOKEY_LOCK_DESTROY(_key) mtx_destroy(&(_key)->mtx); #define KEY_PRESS 0 #define KEY_RELEASE 0x80 #define SCAN_PRESS 0 #define SCAN_RELEASE 0x80 #define SCAN_CHAR(c) ((c) & 0x7f) #define GPIOKEYS_GLOBAL_NMOD 8 /* units */ #define GPIOKEYS_GLOBAL_NKEYCODE 6 /* units */ #define GPIOKEYS_GLOBAL_IN_BUF_SIZE (2*(GPIOKEYS_GLOBAL_NMOD + (2*GPIOKEYS_GLOBAL_NKEYCODE))) /* bytes */ #define GPIOKEYS_GLOBAL_IN_BUF_FULL (GPIOKEYS_GLOBAL_IN_BUF_SIZE / 2) /* bytes */ #define GPIOKEYS_GLOBAL_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */ #define GPIOKEYS_GLOBAL_BUFFER_SIZE 64 /* bytes */ #define AUTOREPEAT_DELAY 250 #define AUTOREPEAT_REPEAT 34 struct gpiokeys_softc; struct gpiokey { struct gpiokeys_softc *parent_sc; gpio_pin_t pin; int irq_rid; struct resource *irq_res; void *intr_hl; struct mtx mtx; #ifdef EVDEV_SUPPORT uint32_t evcode; #endif uint32_t keycode; int autorepeat; struct callout debounce_callout; struct callout repeat_callout; int repeat_delay; int repeat; int debounce_interval; }; struct gpiokeys_softc { device_t sc_dev; struct mtx sc_mtx; struct gpiokey *sc_keys; int sc_total_keys; #ifdef EVDEV_SUPPORT struct evdev_dev *sc_evdev; #endif keyboard_t sc_kbd; keymap_t sc_keymap; accentmap_t sc_accmap; fkeytab_t sc_fkeymap[GPIOKEYS_GLOBAL_NFKEY]; uint32_t sc_input[GPIOKEYS_GLOBAL_IN_BUF_SIZE]; /* input buffer */ uint32_t sc_time_ms; #define GPIOKEYS_GLOBAL_FLAG_POLLING 0x00000002 uint32_t sc_flags; /* flags */ int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */ int sc_state; /* shift/lock key state */ int sc_accents; /* accent key index (> 0) */ int sc_kbd_size; uint16_t sc_inputs; uint16_t sc_inputhead; uint16_t sc_inputtail; uint8_t sc_kbd_id; }; /* gpio-keys device */ static int gpiokeys_probe(device_t); static int gpiokeys_attach(device_t); static int gpiokeys_detach(device_t); /* kbd methods prototypes */ static int gpiokeys_set_typematic(keyboard_t *, int); static uint32_t gpiokeys_read_char(keyboard_t *, int); static void gpiokeys_clear_state(keyboard_t *); static int gpiokeys_ioctl(keyboard_t *, u_long, caddr_t); static int gpiokeys_enable(keyboard_t *); static int gpiokeys_disable(keyboard_t *); static void gpiokeys_event_keyinput(struct gpiokeys_softc *); static void gpiokeys_put_key(struct gpiokeys_softc *sc, uint32_t key) { GPIOKEYS_ASSERT_LOCKED(sc); if (sc->sc_inputs < GPIOKEYS_GLOBAL_IN_BUF_SIZE) { sc->sc_input[sc->sc_inputtail] = key; ++(sc->sc_inputs); ++(sc->sc_inputtail); if (sc->sc_inputtail >= GPIOKEYS_GLOBAL_IN_BUF_SIZE) { sc->sc_inputtail = 0; } } else { device_printf(sc->sc_dev, "input buffer is full\n"); } } static void gpiokeys_key_event(struct gpiokeys_softc *sc, struct gpiokey *key, int pressed) { uint32_t code; GPIOKEYS_LOCK(sc); #ifdef EVDEV_SUPPORT if (key->evcode != GPIOKEY_NONE && (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD) != 0) { evdev_push_key(sc->sc_evdev, key->evcode, pressed); evdev_sync(sc->sc_evdev); } + if (evdev_is_grabbed(sc->sc_evdev)) { + GPIOKEYS_UNLOCK(sc); + return; + } #endif if (key->keycode != GPIOKEY_NONE) { code = key->keycode & SCAN_KEYCODE_MASK; if (!pressed) code |= KEY_RELEASE; if (key->keycode & SCAN_PREFIX_E0) gpiokeys_put_key(sc, 0xe0); else if (key->keycode & SCAN_PREFIX_E1) gpiokeys_put_key(sc, 0xe1); gpiokeys_put_key(sc, code); } GPIOKEYS_UNLOCK(sc); if (key->keycode != GPIOKEY_NONE) gpiokeys_event_keyinput(sc); } static void gpiokey_autorepeat(void *arg) { struct gpiokey *key; key = arg; gpiokeys_key_event(key->parent_sc, key, 1); callout_reset(&key->repeat_callout, key->repeat, gpiokey_autorepeat, key); } static void gpiokey_debounced_intr(void *arg) { struct gpiokey *key; bool active; key = arg; gpio_pin_is_active(key->pin, &active); if (active) { gpiokeys_key_event(key->parent_sc, key, 1); if (key->autorepeat) { callout_reset(&key->repeat_callout, key->repeat_delay, gpiokey_autorepeat, key); } } else { if (key->autorepeat && callout_pending(&key->repeat_callout)) callout_stop(&key->repeat_callout); gpiokeys_key_event(key->parent_sc, key, 0); } } static void gpiokey_intr(void *arg) { struct gpiokey *key; int debounce_ticks; key = arg; GPIOKEY_LOCK(key); debounce_ticks = (hz * key->debounce_interval) / 1000; if (debounce_ticks == 0) debounce_ticks = 1; if (!callout_pending(&key->debounce_callout)) callout_reset(&key->debounce_callout, debounce_ticks, gpiokey_debounced_intr, key); GPIOKEY_UNLOCK(key); } static void gpiokeys_attach_key(struct gpiokeys_softc *sc, phandle_t node, struct gpiokey *key) { pcell_t prop; char *name; uint32_t code; int err; const char *key_name; GPIOKEY_LOCK_INIT(key); key->parent_sc = sc; callout_init_mtx(&key->debounce_callout, &key->mtx, 0); callout_init_mtx(&key->repeat_callout, &key->mtx, 0); name = NULL; if (OF_getprop_alloc(node, "label", (void **)&name) == -1) OF_getprop_alloc(node, "name", (void **)&name); if (name != NULL) key_name = name; else key_name = "unknown"; key->autorepeat = OF_hasprop(node, "autorepeat"); key->repeat_delay = (hz * AUTOREPEAT_DELAY) / 1000; if (key->repeat_delay == 0) key->repeat_delay = 1; key->repeat = (hz * AUTOREPEAT_REPEAT) / 1000; if (key->repeat == 0) key->repeat = 1; if ((OF_getprop(node, "debounce-interval", &prop, sizeof(prop))) > 0) key->debounce_interval = fdt32_to_cpu(prop); else key->debounce_interval = 5; if ((OF_getprop(node, "freebsd,code", &prop, sizeof(prop))) > 0) key->keycode = fdt32_to_cpu(prop); else if ((OF_getprop(node, "linux,code", &prop, sizeof(prop))) > 0) { code = fdt32_to_cpu(prop); key->keycode = gpiokey_map_linux_code(code); if (key->keycode == GPIOKEY_NONE) device_printf(sc->sc_dev, "<%s> failed to map linux,code value 0x%x\n", key_name, code); #ifdef EVDEV_SUPPORT key->evcode = code; evdev_support_key(sc->sc_evdev, code); #endif } else device_printf(sc->sc_dev, "<%s> no linux,code or freebsd,code property\n", key_name); err = gpio_pin_get_by_ofw_idx(sc->sc_dev, node, 0, &key->pin); if (err) { device_printf(sc->sc_dev, "<%s> failed to map pin\n", key_name); if (name) OF_prop_free(name); return; } key->irq_res = gpio_alloc_intr_resource(sc->sc_dev, &key->irq_rid, RF_ACTIVE, key->pin, GPIO_INTR_EDGE_BOTH); if (!key->irq_res) { device_printf(sc->sc_dev, "<%s> cannot allocate interrupt\n", key_name); gpio_pin_release(key->pin); key->pin = NULL; if (name) OF_prop_free(name); return; } if (bus_setup_intr(sc->sc_dev, key->irq_res, INTR_TYPE_MISC | INTR_MPSAFE, NULL, gpiokey_intr, key, &key->intr_hl) != 0) { device_printf(sc->sc_dev, "<%s> unable to setup the irq handler\n", key_name); bus_release_resource(sc->sc_dev, SYS_RES_IRQ, key->irq_rid, key->irq_res); gpio_pin_release(key->pin); key->pin = NULL; key->irq_res = NULL; if (name) OF_prop_free(name); return; } if (bootverbose) device_printf(sc->sc_dev, "<%s> code=%08x, autorepeat=%d, "\ "repeat=%d, repeat_delay=%d\n", key_name, key->keycode, key->autorepeat, key->repeat, key->repeat_delay); if (name) OF_prop_free(name); } static void gpiokeys_detach_key(struct gpiokeys_softc *sc, struct gpiokey *key) { GPIOKEY_LOCK(key); if (key->intr_hl) bus_teardown_intr(sc->sc_dev, key->irq_res, key->intr_hl); if (key->irq_res) bus_release_resource(sc->sc_dev, SYS_RES_IRQ, key->irq_rid, key->irq_res); if (callout_pending(&key->repeat_callout)) callout_drain(&key->repeat_callout); if (callout_pending(&key->debounce_callout)) callout_drain(&key->debounce_callout); if (key->pin) gpio_pin_release(key->pin); GPIOKEY_UNLOCK(key); GPIOKEY_LOCK_DESTROY(key); } static int gpiokeys_probe(device_t dev) { if (!ofw_bus_is_compatible(dev, "gpio-keys")) return (ENXIO); device_set_desc(dev, "GPIO keyboard"); return (0); } static int gpiokeys_attach(device_t dev) { struct gpiokeys_softc *sc; keyboard_t *kbd; #ifdef EVDEV_SUPPORT char *name; #endif phandle_t keys, child; int total_keys; int unit; if ((keys = ofw_bus_get_node(dev)) == -1) return (ENXIO); sc = device_get_softc(dev); sc->sc_dev = dev; kbd = &sc->sc_kbd; GPIOKEYS_LOCK_INIT(sc); unit = device_get_unit(dev); kbd_init_struct(kbd, KBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0); kbd->kb_data = (void *)sc; sc->sc_mode = K_XLATE; sc->sc_keymap = key_map; sc->sc_accmap = accent_map; kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap, sc->sc_fkeymap, GPIOKEYS_GLOBAL_NFKEY); KBD_FOUND_DEVICE(kbd); gpiokeys_clear_state(kbd); KBD_PROBE_DONE(kbd); KBD_INIT_DONE(kbd); if (kbd_register(kbd) < 0) { goto detach; } KBD_CONFIG_DONE(kbd); gpiokeys_enable(kbd); #ifdef KBD_INSTALL_CDEV if (kbd_attach(kbd)) { goto detach; } #endif if (bootverbose) { kbdd_diag(kbd, 1); } #ifdef EVDEV_SUPPORT sc->sc_evdev = evdev_alloc(); evdev_set_name(sc->sc_evdev, device_get_desc(dev)); OF_getprop_alloc(keys, "name", (void **)&name); evdev_set_phys(sc->sc_evdev, name != NULL ? name : "unknown"); OF_prop_free(name); evdev_set_id(sc->sc_evdev, BUS_VIRTUAL, 0, 0, 0); evdev_support_event(sc->sc_evdev, EV_SYN); evdev_support_event(sc->sc_evdev, EV_KEY); #endif total_keys = 0; /* Traverse the 'gpio-keys' node and count keys */ for (child = OF_child(keys); child != 0; child = OF_peer(child)) { if (!OF_hasprop(child, "gpios")) continue; total_keys++; } if (total_keys) { sc->sc_keys = malloc(sizeof(struct gpiokey) * total_keys, M_DEVBUF, M_WAITOK | M_ZERO); sc->sc_total_keys = 0; /* Traverse the 'gpio-keys' node and count keys */ for (child = OF_child(keys); child != 0; child = OF_peer(child)) { if (!OF_hasprop(child, "gpios")) continue; gpiokeys_attach_key(sc, child ,&sc->sc_keys[sc->sc_total_keys]); sc->sc_total_keys++; } } #ifdef EVDEV_SUPPORT if (evdev_register_mtx(sc->sc_evdev, &sc->sc_mtx) != 0) { device_printf(dev, "failed to register evdev device\n"); goto detach; } #endif return (0); detach: gpiokeys_detach(dev); return (ENXIO); } static int gpiokeys_detach(device_t dev) { struct gpiokeys_softc *sc; keyboard_t *kbd; int i; sc = device_get_softc(dev); for (i = 0; i < sc->sc_total_keys; i++) gpiokeys_detach_key(sc, &sc->sc_keys[i]); kbd = kbd_get_keyboard(kbd_find_keyboard(KBD_DRIVER_NAME, device_get_unit(dev))); #ifdef KBD_INSTALL_CDEV kbd_detach(kbd); #endif kbd_unregister(kbd); #ifdef EVDEV_SUPPORT evdev_free(sc->sc_evdev); #endif GPIOKEYS_LOCK_DESTROY(sc); if (sc->sc_keys) free(sc->sc_keys, M_DEVBUF); return (0); } /* early keyboard probe, not supported */ static int gpiokeys_configure(int flags) { return (0); } /* detect a keyboard, not used */ static int gpiokeys__probe(int unit, void *arg, int flags) { return (ENXIO); } /* reset and initialize the device, not used */ static int gpiokeys_init(int unit, keyboard_t **kbdp, void *arg, int flags) { return (ENXIO); } /* test the interface to the device, not used */ static int gpiokeys_test_if(keyboard_t *kbd) { return (0); } /* finish using this keyboard, not used */ static int gpiokeys_term(keyboard_t *kbd) { return (ENXIO); } /* keyboard interrupt routine, not used */ static int gpiokeys_intr(keyboard_t *kbd, void *arg) { return (0); } /* lock the access to the keyboard, not used */ static int gpiokeys_lock(keyboard_t *kbd, int lock) { return (1); } /* * Enable the access to the device; until this function is called, * the client cannot read from the keyboard. */ static int gpiokeys_enable(keyboard_t *kbd) { struct gpiokeys_softc *sc; sc = kbd->kb_data; GPIOKEYS_LOCK(sc); KBD_ACTIVATE(kbd); GPIOKEYS_UNLOCK(sc); return (0); } /* disallow the access to the device */ static int gpiokeys_disable(keyboard_t *kbd) { struct gpiokeys_softc *sc; sc = kbd->kb_data; GPIOKEYS_LOCK(sc); KBD_DEACTIVATE(kbd); GPIOKEYS_UNLOCK(sc); return (0); } static void gpiokeys_do_poll(struct gpiokeys_softc *sc, uint8_t wait) { KASSERT((sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) != 0, ("gpiokeys_do_poll called when not polling\n")); GPIOKEYS_ASSERT_LOCKED(sc); if (!kdb_active && !SCHEDULER_STOPPED()) { while (sc->sc_inputs == 0) { kern_yield(PRI_UNCHANGED); if (!wait) break; } return; } while ((sc->sc_inputs == 0) && wait) { printf("POLL!\n"); } } /* check if data is waiting */ static int gpiokeys_check(keyboard_t *kbd) { struct gpiokeys_softc *sc = kbd->kb_data; GPIOKEYS_ASSERT_LOCKED(sc); if (!KBD_IS_ACTIVE(kbd)) return (0); if (sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) gpiokeys_do_poll(sc, 0); if (sc->sc_inputs > 0) { return (1); } return (0); } /* check if char is waiting */ static int gpiokeys_check_char_locked(keyboard_t *kbd) { if (!KBD_IS_ACTIVE(kbd)) return (0); return (gpiokeys_check(kbd)); } static int gpiokeys_check_char(keyboard_t *kbd) { int result; struct gpiokeys_softc *sc = kbd->kb_data; GPIOKEYS_LOCK(sc); result = gpiokeys_check_char_locked(kbd); GPIOKEYS_UNLOCK(sc); return (result); } static int32_t gpiokeys_get_key(struct gpiokeys_softc *sc, uint8_t wait) { int32_t c; KASSERT((!kdb_active && !SCHEDULER_STOPPED()) || (sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) != 0, ("not polling in kdb or panic\n")); GPIOKEYS_ASSERT_LOCKED(sc); if (sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) gpiokeys_do_poll(sc, wait); if (sc->sc_inputs == 0) { c = -1; } else { c = sc->sc_input[sc->sc_inputhead]; --(sc->sc_inputs); ++(sc->sc_inputhead); if (sc->sc_inputhead >= GPIOKEYS_GLOBAL_IN_BUF_SIZE) { sc->sc_inputhead = 0; } } return (c); } /* read one byte from the keyboard if it's allowed */ static int gpiokeys_read(keyboard_t *kbd, int wait) { struct gpiokeys_softc *sc = kbd->kb_data; int32_t keycode; if (!KBD_IS_ACTIVE(kbd)) return (-1); /* XXX */ keycode = gpiokeys_get_key(sc, (wait == FALSE) ? 0 : 1); if (!KBD_IS_ACTIVE(kbd) || (keycode == -1)) return (-1); ++(kbd->kb_count); return (keycode); } /* read char from the keyboard */ static uint32_t gpiokeys_read_char_locked(keyboard_t *kbd, int wait) { struct gpiokeys_softc *sc = kbd->kb_data; uint32_t action; uint32_t keycode; if (!KBD_IS_ACTIVE(kbd)) return (NOKEY); next_code: /* see if there is something in the keyboard port */ /* XXX */ keycode = gpiokeys_get_key(sc, (wait == FALSE) ? 0 : 1); ++kbd->kb_count; /* return the byte as is for the K_RAW mode */ if (sc->sc_mode == K_RAW) { return (keycode); } /* return the key code in the K_CODE mode */ /* XXX: keycode |= SCAN_RELEASE; */ if (sc->sc_mode == K_CODE) { return (keycode); } /* keycode to key action */ action = genkbd_keyaction(kbd, SCAN_CHAR(keycode), (keycode & SCAN_RELEASE), &sc->sc_state, &sc->sc_accents); if (action == NOKEY) { goto next_code; } return (action); } /* Currently wait is always false. */ static uint32_t gpiokeys_read_char(keyboard_t *kbd, int wait) { uint32_t keycode; struct gpiokeys_softc *sc = kbd->kb_data; GPIOKEYS_LOCK(sc); keycode = gpiokeys_read_char_locked(kbd, wait); GPIOKEYS_UNLOCK(sc); return (keycode); } /* some useful control functions */ static int gpiokeys_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg) { struct gpiokeys_softc *sc = kbd->kb_data; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) int ival; #endif switch (cmd) { case KDGKBMODE: /* get keyboard mode */ *(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 */ 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) { if ((sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) == 0) gpiokeys_clear_state(kbd); sc->sc_mode = *(int *)arg; } break; default: 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 */ KBD_LED_VAL(kbd) = *(int *)arg; 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 (0); case KDSETREPEAT: /* set keyboard repeat rate (new * interface) */ if (!KBD_HAS_DEVICE(kbd)) { return (0); } if (((int *)arg)[1] < 0) { return (EINVAL); } if (((int *)arg)[0] < 0) { return (EINVAL); } if (((int *)arg)[0] < 200) /* fastest possible value */ kbd->kb_delay1 = 200; else kbd->kb_delay1 = ((int *)arg)[0]; kbd->kb_delay2 = ((int *)arg)[1]; return (0); #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) */ return (gpiokeys_set_typematic(kbd, *(int *)arg)); 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 */ sc->sc_accents = 0; /* FALLTHROUGH */ default: return (genkbd_commonioctl(kbd, cmd, arg)); } return (0); } static int gpiokeys_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) { int result; struct gpiokeys_softc *sc; sc = kbd->kb_data; /* * XXX Check if someone is calling us from a critical section: */ if (curthread->td_critnest != 0) return (EDEADLK); GPIOKEYS_LOCK(sc); result = gpiokeys_ioctl_locked(kbd, cmd, arg); GPIOKEYS_UNLOCK(sc); return (result); } /* clear the internal state of the keyboard */ static void gpiokeys_clear_state(keyboard_t *kbd) { struct gpiokeys_softc *sc = kbd->kb_data; sc->sc_flags &= ~(GPIOKEYS_GLOBAL_FLAG_POLLING); sc->sc_state &= LOCK_MASK; /* preserve locking key state */ sc->sc_accents = 0; } /* get the internal state, not used */ static int gpiokeys_get_state(keyboard_t *kbd, void *buf, size_t len) { return (len == 0) ? 1 : -1; } /* set the internal state, not used */ static int gpiokeys_set_state(keyboard_t *kbd, void *buf, size_t len) { return (EINVAL); } static int gpiokeys_poll(keyboard_t *kbd, int on) { struct gpiokeys_softc *sc = kbd->kb_data; GPIOKEYS_LOCK(sc); if (on) sc->sc_flags |= GPIOKEYS_GLOBAL_FLAG_POLLING; else sc->sc_flags &= ~GPIOKEYS_GLOBAL_FLAG_POLLING; GPIOKEYS_UNLOCK(sc); return (0); } static int gpiokeys_set_typematic(keyboard_t *kbd, int code) { static const int delays[] = {250, 500, 750, 1000}; static const 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 void gpiokeys_event_keyinput(struct gpiokeys_softc *sc) { int c; if ((sc->sc_flags & GPIOKEYS_GLOBAL_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 = gpiokeys_read_char(&sc->sc_kbd, 0); } while (c != NOKEY); } } static keyboard_switch_t gpiokeyssw = { .probe = &gpiokeys__probe, .init = &gpiokeys_init, .term = &gpiokeys_term, .intr = &gpiokeys_intr, .test_if = &gpiokeys_test_if, .enable = &gpiokeys_enable, .disable = &gpiokeys_disable, .read = &gpiokeys_read, .check = &gpiokeys_check, .read_char = &gpiokeys_read_char, .check_char = &gpiokeys_check_char, .ioctl = &gpiokeys_ioctl, .lock = &gpiokeys_lock, .clear_state = &gpiokeys_clear_state, .get_state = &gpiokeys_get_state, .set_state = &gpiokeys_set_state, .poll = &gpiokeys_poll, }; KEYBOARD_DRIVER(gpiokeys, gpiokeyssw, gpiokeys_configure); static int gpiokeys_driver_load(module_t mod, int what, void *arg) { switch (what) { case MOD_LOAD: kbd_add_driver(&gpiokeys_kbd_driver); break; case MOD_UNLOAD: kbd_delete_driver(&gpiokeys_kbd_driver); break; } return (0); } static device_method_t gpiokeys_methods[] = { DEVMETHOD(device_probe, gpiokeys_probe), DEVMETHOD(device_attach, gpiokeys_attach), DEVMETHOD(device_detach, gpiokeys_detach), DEVMETHOD_END }; static driver_t gpiokeys_driver = { "gpiokeys", gpiokeys_methods, sizeof(struct gpiokeys_softc), }; DRIVER_MODULE(gpiokeys, simplebus, gpiokeys_driver, gpiokeys_driver_load, NULL); MODULE_VERSION(gpiokeys, 1); diff --git a/sys/dev/hid/hkbd.c b/sys/dev/hid/hkbd.c index 6fee3b0094a5..09339a762679 100644 --- a/sys/dev/hid/hkbd.c +++ b/sys/dev/hid/hkbd.c @@ -1,2028 +1,2032 @@ #include __FBSDID("$FreeBSD$"); /*- * SPDX-License-Identifier: BSD-2-Clause-NetBSD * * Copyright (c) 1998 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Lennart Augustsson (lennart@augustsson.net) at * Carlstedt Research & Technology. * * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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. * */ /* * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf */ #include "opt_hid.h" #include "opt_kbd.h" #include "opt_hkbd.h" #include "opt_evdev.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define HID_DEBUG_VAR hkbd_debug #include #include #include #include #ifdef EVDEV_SUPPORT #include #include #endif #include #include #include #include /* the initial key map, accent map and fkey strings */ #if defined(HKBD_DFLT_KEYMAP) && !defined(KLD_MODULE) #define KBD_DFLT_KEYMAP #include "ukbdmap.h" #endif /* the following file must be included after "ukbdmap.h" */ #include #ifdef HID_DEBUG static int hkbd_debug = 0; static int hkbd_no_leds = 0; static SYSCTL_NODE(_hw_hid, OID_AUTO, hkbd, CTLFLAG_RW, 0, "USB keyboard"); SYSCTL_INT(_hw_hid_hkbd, OID_AUTO, debug, CTLFLAG_RWTUN, &hkbd_debug, 0, "Debug level"); SYSCTL_INT(_hw_hid_hkbd, OID_AUTO, no_leds, CTLFLAG_RWTUN, &hkbd_no_leds, 0, "Disables setting of keyboard leds"); #endif #define INPUT_EPOCH global_epoch_preempt #define HKBD_EMULATE_ATSCANCODE 1 #define HKBD_DRIVER_NAME "hkbd" #define HKBD_NKEYCODE 256 /* units */ #define HKBD_IN_BUF_SIZE (4 * HKBD_NKEYCODE) /* scancodes */ #define HKBD_IN_BUF_FULL ((HKBD_IN_BUF_SIZE / 2) - 1) /* scancodes */ #define HKBD_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */ #define HKBD_BUFFER_SIZE 64 /* bytes */ #define HKBD_KEY_PRESSED(map, key) ({ \ CTASSERT((key) >= 0 && (key) < HKBD_NKEYCODE); \ bit_test(map, key); \ }) #define MOD_EJECT 0x01 #define MOD_FN 0x02 #define MOD_MIN 0xe0 #define MOD_MAX 0xe7 struct hkbd_softc { device_t sc_dev; keyboard_t sc_kbd; keymap_t sc_keymap; accentmap_t sc_accmap; fkeytab_t sc_fkeymap[HKBD_NFKEY]; bitstr_t bit_decl(sc_loc_key_valid, HKBD_NKEYCODE); struct hid_location sc_loc_apple_eject; struct hid_location sc_loc_apple_fn; struct hid_location sc_loc_key[HKBD_NKEYCODE]; struct hid_location sc_loc_numlock; struct hid_location sc_loc_capslock; struct hid_location sc_loc_scrolllock; struct mtx sc_mtx; struct task sc_task; struct callout sc_callout; /* All reported keycodes */ bitstr_t bit_decl(sc_ndata, HKBD_NKEYCODE); bitstr_t bit_decl(sc_odata, HKBD_NKEYCODE); /* Keycodes reported in array fields only */ bitstr_t bit_decl(sc_ndata0, HKBD_NKEYCODE); bitstr_t bit_decl(sc_odata0, HKBD_NKEYCODE); struct thread *sc_poll_thread; #ifdef EVDEV_SUPPORT struct evdev_dev *sc_evdev; #endif sbintime_t sc_co_basetime; int sc_delay; uint32_t sc_repeat_time; uint32_t sc_input[HKBD_IN_BUF_SIZE]; /* input buffer */ uint32_t sc_time_ms; uint32_t sc_composed_char; /* composed char code, if non-zero */ #ifdef HKBD_EMULATE_ATSCANCODE uint32_t sc_buffered_char[2]; #endif uint32_t sc_flags; /* flags */ #define HKBD_FLAG_COMPOSE 0x00000001 #define HKBD_FLAG_POLLING 0x00000002 #define HKBD_FLAG_ATTACHED 0x00000010 #define HKBD_FLAG_GONE 0x00000020 #define HKBD_FLAG_HID_MASK 0x003fffc0 #define HKBD_FLAG_APPLE_EJECT 0x00000040 #define HKBD_FLAG_APPLE_FN 0x00000080 #define HKBD_FLAG_APPLE_SWAP 0x00000100 #define HKBD_FLAG_NUMLOCK 0x00080000 #define HKBD_FLAG_CAPSLOCK 0x00100000 #define HKBD_FLAG_SCROLLLOCK 0x00200000 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */ int sc_state; /* shift/lock key state */ int sc_accents; /* accent key index (> 0) */ int sc_polling; /* polling recursion count */ int sc_led_size; int sc_kbd_size; uint32_t sc_inputhead; uint32_t sc_inputtail; uint8_t sc_iface_index; uint8_t sc_iface_no; uint8_t sc_id_apple_eject; uint8_t sc_id_apple_fn; uint8_t sc_id_loc_key[HKBD_NKEYCODE]; uint8_t sc_id_leds; uint8_t sc_kbd_id; uint8_t sc_repeat_key; uint8_t sc_buffer[HKBD_BUFFER_SIZE]; }; #define KEY_NONE 0x00 #define KEY_ERROR 0x01 #define KEY_PRESS 0 #define KEY_RELEASE 0x400 #define KEY_INDEX(c) ((c) & 0xFF) #define SCAN_PRESS 0 #define SCAN_RELEASE 0x80 #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 SCAN_CHAR(c) ((c) & 0x7f) #define HKBD_LOCK(sc) do { \ if (!HID_IN_POLLING_MODE()) \ mtx_lock(&(sc)->sc_mtx); \ } while (0) #define HKBD_UNLOCK(sc) do { \ if (!HID_IN_POLLING_MODE()) \ mtx_unlock(&(sc)->sc_mtx); \ } while (0) #define HKBD_LOCK_ASSERT(sc) do { \ if (!HID_IN_POLLING_MODE()) \ mtx_assert(&(sc)->sc_mtx, MA_OWNED); \ } while (0) #define SYSCONS_LOCK() do { \ if (!HID_IN_POLLING_MODE()) \ mtx_lock(&Giant); \ } while (0) #define SYSCONS_UNLOCK() do { \ if (!HID_IN_POLLING_MODE()) \ mtx_unlock(&Giant); \ } while (0) #define SYSCONS_LOCK_ASSERT() do { \ if (!HID_IN_POLLING_MODE()) \ mtx_assert(&Giant, MA_OWNED); \ } while (0) #define NN 0 /* no translation */ /* * Translate USB keycodes to AT keyboard scancodes. */ /* * FIXME: Mac USB keyboard generates: * 0x53: keypad NumLock/Clear * 0x66: Power * 0x67: keypad = * 0x68: F13 * 0x69: F14 * 0x6a: F15 * * USB Apple Keyboard JIS generates: * 0x90: Kana * 0x91: Eisu */ static const uint8_t hkbd_trtab[256] = { 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */ 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */ 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */ 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */ 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */ 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */ 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */ 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */ 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */ 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */ 97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */ 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */ 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */ NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */ NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */ 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */ 121, 120, NN, NN, NN, NN, NN, 123, /* 80 - 87 */ 124, 125, 126, 127, 128, NN, NN, NN, /* 88 - 8F */ 129, 130, NN, NN, NN, NN, NN, NN, /* 90 - 97 */ NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */ NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */ NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */ NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */ NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */ NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */ NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */ NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */ NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */ 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */ NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */ NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */ NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */ }; static const uint8_t hkbd_boot_desc[] = { HID_KBD_BOOTPROTO_DESCR() }; /* prototypes */ static void hkbd_timeout(void *); static int hkbd_set_leds(struct hkbd_softc *, uint8_t); static int hkbd_set_typematic(keyboard_t *, int); #ifdef HKBD_EMULATE_ATSCANCODE static uint32_t hkbd_atkeycode(int, const bitstr_t *); static int hkbd_key2scan(struct hkbd_softc *, int, const bitstr_t *, int); #endif static uint32_t hkbd_read_char(keyboard_t *, int); static void hkbd_clear_state(keyboard_t *); static int hkbd_ioctl(keyboard_t *, u_long, caddr_t); static int hkbd_enable(keyboard_t *); static int hkbd_disable(keyboard_t *); static void hkbd_interrupt(struct hkbd_softc *); static task_fn_t hkbd_event_keyinput; static device_probe_t hkbd_probe; static device_attach_t hkbd_attach; static device_detach_t hkbd_detach; static device_resume_t hkbd_resume; #ifdef EVDEV_SUPPORT static evdev_event_t hkbd_ev_event; static const struct evdev_methods hkbd_evdev_methods = { .ev_event = hkbd_ev_event, }; #endif static bool hkbd_any_key_pressed(struct hkbd_softc *sc) { int result; bit_ffs(sc->sc_odata, HKBD_NKEYCODE, &result); return (result != -1); } static bool hkbd_any_key_valid(struct hkbd_softc *sc) { int result; bit_ffs(sc->sc_loc_key_valid, HKBD_NKEYCODE, &result); return (result != -1); } static bool hkbd_is_modifier_key(uint32_t key) { return (key >= MOD_MIN && key <= MOD_MAX); } static void hkbd_start_timer(struct hkbd_softc *sc) { sbintime_t delay, now, prec; now = sbinuptime(); /* check if initial delay passed and fallback to key repeat delay */ if (sc->sc_delay == 0) sc->sc_delay = sc->sc_kbd.kb_delay2; /* compute timeout */ delay = SBT_1MS * sc->sc_delay; sc->sc_co_basetime += delay; /* check if we are running behind */ if (sc->sc_co_basetime < now) sc->sc_co_basetime = now; /* This is rarely called, so prefer precision to efficiency. */ prec = qmin(delay >> 7, SBT_1MS * 10); if (!HID_IN_POLLING_MODE()) callout_reset_sbt(&sc->sc_callout, sc->sc_co_basetime, prec, hkbd_timeout, sc, C_ABSOLUTE); } static void hkbd_put_key(struct hkbd_softc *sc, uint32_t key) { uint32_t tail; HKBD_LOCK_ASSERT(sc); DPRINTF("0x%02x (%d) %s\n", key, key, (key & KEY_RELEASE) ? "released" : "pressed"); #ifdef EVDEV_SUPPORT if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && sc->sc_evdev != NULL) evdev_push_event(sc->sc_evdev, EV_KEY, evdev_hid2key(KEY_INDEX(key)), !(key & KEY_RELEASE)); + if (sc->sc_evdev != NULL && evdev_is_grabbed(sc->sc_evdev)) + return; #endif tail = (sc->sc_inputtail + 1) % HKBD_IN_BUF_SIZE; if (tail != atomic_load_acq_32(&sc->sc_inputhead)) { sc->sc_input[sc->sc_inputtail] = key; atomic_store_rel_32(&sc->sc_inputtail, tail); } else { DPRINTF("input buffer is full\n"); } } static void hkbd_do_poll(struct hkbd_softc *sc, uint8_t wait) { SYSCONS_LOCK_ASSERT(); KASSERT((sc->sc_flags & HKBD_FLAG_POLLING) != 0, ("hkbd_do_poll called when not polling\n")); DPRINTFN(2, "polling\n"); if (!HID_IN_POLLING_MODE()) { /* * In this context the kernel is polling for input, * but the USB subsystem works in normal interrupt-driven * mode, so we just wait on the USB threads to do the job. * Note that we currently hold the Giant, but it's also used * as the transfer mtx, so we must release it while waiting. */ while (sc->sc_inputhead == atomic_load_acq_32(&sc->sc_inputtail)) { /* * Give USB threads a chance to run. Note that * kern_yield performs DROP_GIANT + PICKUP_GIANT. */ kern_yield(PRI_UNCHANGED); if (!wait) break; } return; } while (sc->sc_inputhead == sc->sc_inputtail) { hidbus_intr_poll(sc->sc_dev); /* Delay-optimised support for repetition of keys */ if (hkbd_any_key_pressed(sc)) { /* a key is pressed - need timekeeping */ DELAY(1000); /* 1 millisecond has passed */ sc->sc_time_ms += 1; } hkbd_interrupt(sc); if (!wait) break; } } static int32_t hkbd_get_key(struct hkbd_softc *sc, uint8_t wait) { uint32_t head; int32_t c; SYSCONS_LOCK_ASSERT(); KASSERT(!HID_IN_POLLING_MODE() || (sc->sc_flags & HKBD_FLAG_POLLING) != 0, ("not polling in kdb or panic\n")); if (sc->sc_flags & HKBD_FLAG_POLLING) hkbd_do_poll(sc, wait); head = sc->sc_inputhead; if (head == atomic_load_acq_32(&sc->sc_inputtail)) { c = -1; } else { c = sc->sc_input[head]; head = (head + 1) % HKBD_IN_BUF_SIZE; atomic_store_rel_32(&sc->sc_inputhead, head); } return (c); } static void hkbd_interrupt(struct hkbd_softc *sc) { const uint32_t now = sc->sc_time_ms; unsigned key; HKBD_LOCK_ASSERT(sc); /* * Check for key changes, the order is: * 1. Regular keys up * 2. Modifier keys up * 3. Modifier keys down * 4. Regular keys down * * This allows devices which send events changing the state of * both a modifier key and a regular key, to be correctly * translated. */ bit_foreach(sc->sc_odata, HKBD_NKEYCODE, key) { if (hkbd_is_modifier_key(key) || bit_test(sc->sc_ndata, key)) continue; hkbd_put_key(sc, key | KEY_RELEASE); /* clear repeating key, if any */ if (sc->sc_repeat_key == key) sc->sc_repeat_key = 0; } bit_foreach_at(sc->sc_odata, MOD_MIN, MOD_MAX + 1, key) if (!bit_test(sc->sc_ndata, key)) hkbd_put_key(sc, key | KEY_RELEASE); bit_foreach_at(sc->sc_ndata, MOD_MIN, MOD_MAX + 1, key) if (!bit_test(sc->sc_odata, key)) hkbd_put_key(sc, key | KEY_PRESS); bit_foreach(sc->sc_ndata, HKBD_NKEYCODE, key) { if (hkbd_is_modifier_key(key) || bit_test(sc->sc_odata, key)) continue; hkbd_put_key(sc, key | KEY_PRESS); sc->sc_co_basetime = sbinuptime(); sc->sc_delay = sc->sc_kbd.kb_delay1; hkbd_start_timer(sc); /* set repeat time for last key */ sc->sc_repeat_time = now + sc->sc_kbd.kb_delay1; sc->sc_repeat_key = key; } /* synchronize old data with new data */ memcpy(sc->sc_odata0, sc->sc_ndata0, bitstr_size(HKBD_NKEYCODE)); memcpy(sc->sc_odata, sc->sc_ndata, bitstr_size(HKBD_NKEYCODE)); /* check if last key is still pressed */ if (sc->sc_repeat_key != 0) { const int32_t dtime = (sc->sc_repeat_time - now); /* check if time has elapsed */ if (dtime <= 0) { hkbd_put_key(sc, sc->sc_repeat_key | KEY_PRESS); sc->sc_repeat_time = now + sc->sc_kbd.kb_delay2; } } #ifdef EVDEV_SUPPORT if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && sc->sc_evdev != NULL) evdev_sync(sc->sc_evdev); + if (sc->sc_evdev != NULL && evdev_is_grabbed(sc->sc_evdev)) + return; #endif /* wakeup keyboard system */ if (!HID_IN_POLLING_MODE()) taskqueue_enqueue(taskqueue_swi_giant, &sc->sc_task); } static void hkbd_event_keyinput(void *context, int pending) { struct hkbd_softc *sc = context; int c; SYSCONS_LOCK_ASSERT(); if ((sc->sc_flags & HKBD_FLAG_POLLING) != 0) return; if (sc->sc_inputhead == atomic_load_acq_32(&sc->sc_inputtail)) 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 = hkbd_read_char(&sc->sc_kbd, 0); } while (c != NOKEY); } } static void hkbd_timeout(void *arg) { struct hkbd_softc *sc = arg; #ifdef EVDEV_SUPPORT struct epoch_tracker et; #endif HKBD_LOCK_ASSERT(sc); sc->sc_time_ms += sc->sc_delay; sc->sc_delay = 0; #ifdef EVDEV_SUPPORT epoch_enter_preempt(INPUT_EPOCH, &et); #endif hkbd_interrupt(sc); #ifdef EVDEV_SUPPORT epoch_exit_preempt(INPUT_EPOCH, &et); #endif /* Make sure any leftover key events gets read out */ taskqueue_enqueue(taskqueue_swi_giant, &sc->sc_task); if (hkbd_any_key_pressed(sc) || atomic_load_acq_32(&sc->sc_inputhead) != sc->sc_inputtail) { hkbd_start_timer(sc); } } static uint32_t hkbd_apple_fn(uint32_t keycode) { switch (keycode) { case 0x28: return 0x49; /* RETURN -> INSERT */ case 0x2a: return 0x4c; /* BACKSPACE -> DEL */ case 0x50: return 0x4a; /* LEFT ARROW -> HOME */ case 0x4f: return 0x4d; /* RIGHT ARROW -> END */ case 0x52: return 0x4b; /* UP ARROW -> PGUP */ case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */ default: return keycode; } } static uint32_t hkbd_apple_swap(uint32_t keycode) { switch (keycode) { case 0x35: return 0x64; case 0x64: return 0x35; default: return keycode; } } static void hkbd_intr_callback(void *context, void *data, hid_size_t len) { struct hkbd_softc *sc = context; uint8_t *buf = data; uint32_t i; uint8_t id = 0; uint8_t modifiers; HKBD_LOCK_ASSERT(sc); DPRINTF("actlen=%d bytes\n", len); if (len == 0) { DPRINTF("zero length data\n"); return; } if (sc->sc_kbd_id != 0) { /* check and remove HID ID byte */ id = buf[0]; buf++; len--; if (len == 0) { DPRINTF("zero length data\n"); return; } } /* clear temporary storage */ if (bit_test(sc->sc_loc_key_valid, 0) && id == sc->sc_id_loc_key[0]) { bit_foreach(sc->sc_ndata0, HKBD_NKEYCODE, i) bit_clear(sc->sc_ndata, i); memset(&sc->sc_ndata0, 0, bitstr_size(HKBD_NKEYCODE)); } bit_foreach(sc->sc_ndata, HKBD_NKEYCODE, i) if (id == sc->sc_id_loc_key[i]) bit_clear(sc->sc_ndata, i); /* clear modifiers */ modifiers = 0; /* scan through HID data */ if ((sc->sc_flags & HKBD_FLAG_APPLE_EJECT) && (id == sc->sc_id_apple_eject)) { if (hid_get_data(buf, len, &sc->sc_loc_apple_eject)) modifiers |= MOD_EJECT; } if ((sc->sc_flags & HKBD_FLAG_APPLE_FN) && (id == sc->sc_id_apple_fn)) { if (hid_get_data(buf, len, &sc->sc_loc_apple_fn)) modifiers |= MOD_FN; } bit_foreach(sc->sc_loc_key_valid, HKBD_NKEYCODE, i) { if (id != sc->sc_id_loc_key[i]) { continue; /* invalid HID ID */ } else if (i == 0) { struct hid_location tmp_loc = sc->sc_loc_key[0]; /* range check array size */ if (tmp_loc.count > HKBD_NKEYCODE) tmp_loc.count = HKBD_NKEYCODE; while (tmp_loc.count--) { uint32_t key = hid_get_udata(buf, len, &tmp_loc); /* advance to next location */ tmp_loc.pos += tmp_loc.size; if (key == KEY_ERROR) { DPRINTF("KEY_ERROR\n"); memcpy(sc->sc_ndata0, sc->sc_odata0, bitstr_size(HKBD_NKEYCODE)); memcpy(sc->sc_ndata, sc->sc_odata, bitstr_size(HKBD_NKEYCODE)); return; /* ignore */ } if (modifiers & MOD_FN) key = hkbd_apple_fn(key); if (sc->sc_flags & HKBD_FLAG_APPLE_SWAP) key = hkbd_apple_swap(key); if (key == KEY_NONE || key >= HKBD_NKEYCODE) continue; /* set key in bitmap */ bit_set(sc->sc_ndata, key); bit_set(sc->sc_ndata0, key); } } else if (hid_get_data(buf, len, &sc->sc_loc_key[i])) { uint32_t key = i; if (modifiers & MOD_FN) key = hkbd_apple_fn(key); if (sc->sc_flags & HKBD_FLAG_APPLE_SWAP) key = hkbd_apple_swap(key); if (key == KEY_NONE || key == KEY_ERROR || key >= HKBD_NKEYCODE) continue; /* set key in bitmap */ bit_set(sc->sc_ndata, key); } } #ifdef HID_DEBUG DPRINTF("modifiers = 0x%04x\n", modifiers); bit_foreach(sc->sc_ndata, HKBD_NKEYCODE, i) DPRINTF("Key 0x%02x pressed\n", i); #endif hkbd_interrupt(sc); } /* A match on these entries will load ukbd */ static const struct hid_device_id __used hkbd_devs[] = { { HID_TLC(HUP_GENERIC_DESKTOP, HUG_KEYBOARD) }, }; static int hkbd_probe(device_t dev) { keyboard_switch_t *sw = kbd_get_switch(HKBD_DRIVER_NAME); int error; DPRINTFN(11, "\n"); if (sw == NULL) { return (ENXIO); } error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hkbd_devs); if (error != 0) return (error); hidbus_set_desc(dev, "Keyboard"); return (BUS_PROBE_DEFAULT); } static void hkbd_parse_hid(struct hkbd_softc *sc, const uint8_t *ptr, uint32_t len, uint8_t tlc_index) { uint32_t flags; uint32_t key; uint8_t id; /* reset detected bits */ sc->sc_flags &= ~HKBD_FLAG_HID_MASK; /* reset detected keys */ memset(sc->sc_loc_key_valid, 0, bitstr_size(HKBD_NKEYCODE)); /* check if there is an ID byte */ sc->sc_kbd_size = hid_report_size_max(ptr, len, hid_input, &sc->sc_kbd_id); /* investigate if this is an Apple Keyboard */ if (hidbus_locate(ptr, len, HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT), hid_input, tlc_index, 0, &sc->sc_loc_apple_eject, &flags, &sc->sc_id_apple_eject, NULL)) { if (flags & HIO_VARIABLE) sc->sc_flags |= HKBD_FLAG_APPLE_EJECT | HKBD_FLAG_APPLE_SWAP; DPRINTFN(1, "Found Apple eject-key\n"); } if (hidbus_locate(ptr, len, HID_USAGE2(0xFFFF, 0x0003), hid_input, tlc_index, 0, &sc->sc_loc_apple_fn, &flags, &sc->sc_id_apple_fn, NULL)) { if (flags & HIO_VARIABLE) sc->sc_flags |= HKBD_FLAG_APPLE_FN; DPRINTFN(1, "Found Apple FN-key\n"); } /* figure out event buffer */ if (hidbus_locate(ptr, len, HID_USAGE2(HUP_KEYBOARD, 0x00), hid_input, tlc_index, 0, &sc->sc_loc_key[0], &flags, &sc->sc_id_loc_key[0], NULL)) { if (flags & HIO_VARIABLE) { DPRINTFN(1, "Ignoring keyboard event control\n"); } else { bit_set(sc->sc_loc_key_valid, 0); DPRINTFN(1, "Found keyboard event array\n"); } } /* figure out the keys */ for (key = 1; key != HKBD_NKEYCODE; key++) { if (hidbus_locate(ptr, len, HID_USAGE2(HUP_KEYBOARD, key), hid_input, tlc_index, 0, &sc->sc_loc_key[key], &flags, &sc->sc_id_loc_key[key], NULL)) { if (flags & HIO_VARIABLE) { bit_set(sc->sc_loc_key_valid, key); DPRINTFN(1, "Found key 0x%02x\n", key); } } } /* figure out leds on keyboard */ if (hidbus_locate(ptr, len, HID_USAGE2(HUP_LEDS, 0x01), hid_output, tlc_index, 0, &sc->sc_loc_numlock, &flags, &sc->sc_id_leds, NULL)) { if (flags & HIO_VARIABLE) sc->sc_flags |= HKBD_FLAG_NUMLOCK; DPRINTFN(1, "Found keyboard numlock\n"); } if (hidbus_locate(ptr, len, HID_USAGE2(HUP_LEDS, 0x02), hid_output, tlc_index, 0, &sc->sc_loc_capslock, &flags, &id, NULL)) { if ((sc->sc_flags & HKBD_FLAG_NUMLOCK) == 0) sc->sc_id_leds = id; if (flags & HIO_VARIABLE && sc->sc_id_leds == id) sc->sc_flags |= HKBD_FLAG_CAPSLOCK; DPRINTFN(1, "Found keyboard capslock\n"); } if (hidbus_locate(ptr, len, HID_USAGE2(HUP_LEDS, 0x03), hid_output, tlc_index, 0, &sc->sc_loc_scrolllock, &flags, &id, NULL)) { if ((sc->sc_flags & (HKBD_FLAG_NUMLOCK | HKBD_FLAG_CAPSLOCK)) == 0) sc->sc_id_leds = id; if (flags & HIO_VARIABLE && sc->sc_id_leds == id) sc->sc_flags |= HKBD_FLAG_SCROLLLOCK; DPRINTFN(1, "Found keyboard scrolllock\n"); } if ((sc->sc_flags & (HKBD_FLAG_NUMLOCK | HKBD_FLAG_CAPSLOCK | HKBD_FLAG_SCROLLLOCK)) != 0) sc->sc_led_size = hid_report_size(ptr, len, hid_output, sc->sc_id_leds); } static int hkbd_attach(device_t dev) { struct hkbd_softc *sc = device_get_softc(dev); const struct hid_device_info *hw = hid_get_device_info(dev); int unit = device_get_unit(dev); keyboard_t *kbd = &sc->sc_kbd; void *hid_ptr = NULL; int err; uint16_t n; hid_size_t hid_len; uint8_t tlc_index = hidbus_get_index(dev); #ifdef EVDEV_SUPPORT struct evdev_dev *evdev; int i; #endif sc->sc_dev = dev; SYSCONS_LOCK_ASSERT(); kbd_init_struct(kbd, HKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0); kbd->kb_data = (void *)sc; sc->sc_mode = K_XLATE; mtx_init(&sc->sc_mtx, "hkbd lock", NULL, MTX_DEF); TASK_INIT(&sc->sc_task, 0, hkbd_event_keyinput, sc); callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0); hidbus_set_intr(dev, hkbd_intr_callback, sc); /* interrupt handler will be called with hkbd mutex taken */ hidbus_set_lock(dev, &sc->sc_mtx); /* interrupt handler can be called during panic */ hidbus_set_flags(dev, hidbus_get_flags(dev) | HIDBUS_FLAG_CAN_POLL); /* setup default keyboard maps */ sc->sc_keymap = key_map; sc->sc_accmap = accent_map; for (n = 0; n < HKBD_NFKEY; n++) { sc->sc_fkeymap[n] = fkey_tab[n]; } kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap, sc->sc_fkeymap, HKBD_NFKEY); KBD_FOUND_DEVICE(kbd); hkbd_clear_state(kbd); /* * FIXME: set the initial value for lock keys in "sc_state" * according to the BIOS data? */ KBD_PROBE_DONE(kbd); /* get HID descriptor */ err = hid_get_report_descr(dev, &hid_ptr, &hid_len); if (err == 0) { DPRINTF("Parsing HID descriptor of %d bytes\n", (int)hid_len); hkbd_parse_hid(sc, hid_ptr, hid_len, tlc_index); } /* check if we should use the boot protocol */ if (hid_test_quirk(hw, HQ_KBD_BOOTPROTO) || (err != 0) || hkbd_any_key_valid(sc) == false) { DPRINTF("Forcing boot protocol\n"); err = hid_set_protocol(dev, 0); if (err != 0) { DPRINTF("Set protocol error=%d (ignored)\n", err); } hkbd_parse_hid(sc, hkbd_boot_desc, sizeof(hkbd_boot_desc), 0); } /* ignore if SETIDLE fails, hence it is not crucial */ hid_set_idle(dev, 0, 0); hkbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state); KBD_INIT_DONE(kbd); if (kbd_register(kbd) < 0) { goto detach; } KBD_CONFIG_DONE(kbd); hkbd_enable(kbd); #ifdef KBD_INSTALL_CDEV if (kbd_attach(kbd)) { goto detach; } #endif #ifdef EVDEV_SUPPORT evdev = evdev_alloc(); evdev_set_name(evdev, device_get_desc(dev)); evdev_set_phys(evdev, device_get_nameunit(dev)); evdev_set_id(evdev, hw->idBus, hw->idVendor, hw->idProduct, hw->idVersion); evdev_set_serial(evdev, hw->serial); evdev_set_methods(evdev, kbd, &hkbd_evdev_methods); evdev_set_flag(evdev, EVDEV_FLAG_EXT_EPOCH); /* hidbus child */ evdev_support_event(evdev, EV_SYN); evdev_support_event(evdev, EV_KEY); if (sc->sc_flags & (HKBD_FLAG_NUMLOCK | HKBD_FLAG_CAPSLOCK | HKBD_FLAG_SCROLLLOCK)) evdev_support_event(evdev, EV_LED); evdev_support_event(evdev, EV_REP); for (i = 0x00; i <= 0xFF; i++) evdev_support_key(evdev, evdev_hid2key(i)); if (sc->sc_flags & HKBD_FLAG_NUMLOCK) evdev_support_led(evdev, LED_NUML); if (sc->sc_flags & HKBD_FLAG_CAPSLOCK) evdev_support_led(evdev, LED_CAPSL); if (sc->sc_flags & HKBD_FLAG_SCROLLLOCK) evdev_support_led(evdev, LED_SCROLLL); if (evdev_register(evdev)) evdev_free(evdev); else sc->sc_evdev = evdev; #endif sc->sc_flags |= HKBD_FLAG_ATTACHED; if (bootverbose) { kbdd_diag(kbd, bootverbose); } /* start the keyboard */ hidbus_intr_start(dev); return (0); /* success */ detach: hkbd_detach(dev); return (ENXIO); /* error */ } static int hkbd_detach(device_t dev) { struct hkbd_softc *sc = device_get_softc(dev); #ifdef EVDEV_SUPPORT struct epoch_tracker et; #endif int error; SYSCONS_LOCK_ASSERT(); DPRINTF("\n"); sc->sc_flags |= HKBD_FLAG_GONE; HKBD_LOCK(sc); callout_stop(&sc->sc_callout); HKBD_UNLOCK(sc); /* kill any stuck keys */ if (sc->sc_flags & HKBD_FLAG_ATTACHED) { /* stop receiving events from the USB keyboard */ hidbus_intr_stop(dev); /* release all leftover keys, if any */ memset(&sc->sc_ndata, 0, bitstr_size(HKBD_NKEYCODE)); /* process releasing of all keys */ HKBD_LOCK(sc); #ifdef EVDEV_SUPPORT epoch_enter_preempt(INPUT_EPOCH, &et); #endif hkbd_interrupt(sc); #ifdef EVDEV_SUPPORT epoch_exit_preempt(INPUT_EPOCH, &et); #endif HKBD_UNLOCK(sc); taskqueue_drain(taskqueue_swi_giant, &sc->sc_task); } mtx_destroy(&sc->sc_mtx); hkbd_disable(&sc->sc_kbd); #ifdef KBD_INSTALL_CDEV if (sc->sc_flags & HKBD_FLAG_ATTACHED) { error = kbd_detach(&sc->sc_kbd); if (error) { /* usb attach cannot return an error */ device_printf(dev, "WARNING: kbd_detach() " "returned non-zero! (ignored)\n"); } } #endif #ifdef EVDEV_SUPPORT evdev_free(sc->sc_evdev); #endif if (KBD_IS_CONFIGURED(&sc->sc_kbd)) { error = kbd_unregister(&sc->sc_kbd); if (error) { /* usb attach cannot return an error */ device_printf(dev, "WARNING: kbd_unregister() " "returned non-zero! (ignored)\n"); } } sc->sc_kbd.kb_flags = 0; DPRINTF("%s: disconnected\n", device_get_nameunit(dev)); return (0); } static int hkbd_resume(device_t dev) { struct hkbd_softc *sc = device_get_softc(dev); SYSCONS_LOCK_ASSERT(); hkbd_clear_state(&sc->sc_kbd); return (0); } #ifdef EVDEV_SUPPORT static void hkbd_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 /* early keyboard probe, not supported */ static int hkbd_configure(int flags) { return (0); } /* detect a keyboard, not used */ static int hkbd__probe(int unit, void *arg, int flags) { return (ENXIO); } /* reset and initialize the device, not used */ static int hkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) { return (ENXIO); } /* test the interface to the device, not used */ static int hkbd_test_if(keyboard_t *kbd) { return (0); } /* finish using this keyboard, not used */ static int hkbd_term(keyboard_t *kbd) { return (ENXIO); } /* keyboard interrupt routine, not used */ static int hkbd_intr(keyboard_t *kbd, void *arg) { return (0); } /* lock the access to the keyboard, not used */ static int hkbd_lock(keyboard_t *kbd, int lock) { return (1); } /* * Enable the access to the device; until this function is called, * the client cannot read from the keyboard. */ static int hkbd_enable(keyboard_t *kbd) { SYSCONS_LOCK(); KBD_ACTIVATE(kbd); SYSCONS_UNLOCK(); return (0); } /* disallow the access to the device */ static int hkbd_disable(keyboard_t *kbd) { SYSCONS_LOCK(); KBD_DEACTIVATE(kbd); SYSCONS_UNLOCK(); return (0); } /* check if data is waiting */ /* Currently unused. */ static int hkbd_check(keyboard_t *kbd) { struct hkbd_softc *sc = kbd->kb_data; SYSCONS_LOCK_ASSERT(); if (!KBD_IS_ACTIVE(kbd)) return (0); if (sc->sc_flags & HKBD_FLAG_POLLING) hkbd_do_poll(sc, 0); #ifdef HKBD_EMULATE_ATSCANCODE if (sc->sc_buffered_char[0]) { return (1); } #endif if (sc->sc_inputhead != atomic_load_acq_32(&sc->sc_inputtail)) { return (1); } return (0); } /* check if char is waiting */ static int hkbd_check_char_locked(keyboard_t *kbd) { struct hkbd_softc *sc = kbd->kb_data; SYSCONS_LOCK_ASSERT(); if (!KBD_IS_ACTIVE(kbd)) return (0); if ((sc->sc_composed_char > 0) && (!(sc->sc_flags & HKBD_FLAG_COMPOSE))) { return (1); } return (hkbd_check(kbd)); } static int hkbd_check_char(keyboard_t *kbd) { int result; SYSCONS_LOCK(); result = hkbd_check_char_locked(kbd); SYSCONS_UNLOCK(); return (result); } /* read one byte from the keyboard if it's allowed */ /* Currently unused. */ static int hkbd_read(keyboard_t *kbd, int wait) { struct hkbd_softc *sc = kbd->kb_data; int32_t usbcode; #ifdef HKBD_EMULATE_ATSCANCODE uint32_t keycode; uint32_t scancode; #endif SYSCONS_LOCK_ASSERT(); if (!KBD_IS_ACTIVE(kbd)) return (-1); #ifdef HKBD_EMULATE_ATSCANCODE if (sc->sc_buffered_char[0]) { scancode = sc->sc_buffered_char[0]; if (scancode & SCAN_PREFIX) { sc->sc_buffered_char[0] &= ~SCAN_PREFIX; return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); } sc->sc_buffered_char[0] = sc->sc_buffered_char[1]; sc->sc_buffered_char[1] = 0; return (scancode); } #endif /* HKBD_EMULATE_ATSCANCODE */ /* XXX */ usbcode = hkbd_get_key(sc, (wait == FALSE) ? 0 : 1); if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1)) return (-1); ++(kbd->kb_count); #ifdef HKBD_EMULATE_ATSCANCODE keycode = hkbd_atkeycode(usbcode, sc->sc_ndata); if (keycode == NN) { return -1; } return (hkbd_key2scan(sc, keycode, sc->sc_ndata, (usbcode & KEY_RELEASE))); #else /* !HKBD_EMULATE_ATSCANCODE */ return (usbcode); #endif /* HKBD_EMULATE_ATSCANCODE */ } /* read char from the keyboard */ static uint32_t hkbd_read_char_locked(keyboard_t *kbd, int wait) { struct hkbd_softc *sc = kbd->kb_data; uint32_t action; uint32_t keycode; int32_t usbcode; #ifdef HKBD_EMULATE_ATSCANCODE uint32_t scancode; #endif SYSCONS_LOCK_ASSERT(); if (!KBD_IS_ACTIVE(kbd)) return (NOKEY); next_code: /* do we have a composed char to return ? */ if ((sc->sc_composed_char > 0) && (!(sc->sc_flags & HKBD_FLAG_COMPOSE))) { action = sc->sc_composed_char; sc->sc_composed_char = 0; if (action > 0xFF) { goto errkey; } goto done; } #ifdef HKBD_EMULATE_ATSCANCODE /* do we have a pending raw scan code? */ if (sc->sc_mode == K_RAW) { scancode = sc->sc_buffered_char[0]; if (scancode) { if (scancode & SCAN_PREFIX) { sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX); return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); } sc->sc_buffered_char[0] = sc->sc_buffered_char[1]; sc->sc_buffered_char[1] = 0; return (scancode); } } #endif /* HKBD_EMULATE_ATSCANCODE */ /* see if there is something in the keyboard port */ /* XXX */ usbcode = hkbd_get_key(sc, (wait == FALSE) ? 0 : 1); if (usbcode == -1) { return (NOKEY); } ++kbd->kb_count; #ifdef HKBD_EMULATE_ATSCANCODE /* USB key index -> key code -> AT scan code */ keycode = hkbd_atkeycode(usbcode, sc->sc_ndata); if (keycode == NN) { return (NOKEY); } /* return an AT scan code for the K_RAW mode */ if (sc->sc_mode == K_RAW) { return (hkbd_key2scan(sc, keycode, sc->sc_ndata, (usbcode & KEY_RELEASE))); } #else /* !HKBD_EMULATE_ATSCANCODE */ /* return the byte as is for the K_RAW mode */ if (sc->sc_mode == K_RAW) { return (usbcode); } /* USB key index -> key code */ keycode = hkbd_trtab[KEY_INDEX(usbcode)]; if (keycode == NN) { return (NOKEY); } #endif /* HKBD_EMULATE_ATSCANCODE */ switch (keycode) { case 0x38: /* left alt (compose key) */ if (usbcode & KEY_RELEASE) { if (sc->sc_flags & HKBD_FLAG_COMPOSE) { sc->sc_flags &= ~HKBD_FLAG_COMPOSE; if (sc->sc_composed_char > 0xFF) { sc->sc_composed_char = 0; } } } else { if (!(sc->sc_flags & HKBD_FLAG_COMPOSE)) { sc->sc_flags |= HKBD_FLAG_COMPOSE; sc->sc_composed_char = 0; } } break; } /* return the key code in the K_CODE mode */ if (usbcode & KEY_RELEASE) { keycode |= SCAN_RELEASE; } if (sc->sc_mode == K_CODE) { return (keycode); } /* compose a character code */ if (sc->sc_flags & HKBD_FLAG_COMPOSE) { switch (keycode) { /* key pressed, process it */ case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */ sc->sc_composed_char *= 10; sc->sc_composed_char += keycode - 0x40; goto check_composed; case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */ sc->sc_composed_char *= 10; sc->sc_composed_char += keycode - 0x47; goto check_composed; case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */ sc->sc_composed_char *= 10; sc->sc_composed_char += keycode - 0x4E; goto check_composed; case 0x52: /* keypad 0 */ sc->sc_composed_char *= 10; goto check_composed; /* key released, no interest here */ case SCAN_RELEASE | 0x47: case SCAN_RELEASE | 0x48: case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */ case SCAN_RELEASE | 0x4B: case SCAN_RELEASE | 0x4C: case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */ case SCAN_RELEASE | 0x4F: case SCAN_RELEASE | 0x50: case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */ case SCAN_RELEASE | 0x52: /* keypad 0 */ goto next_code; case 0x38: /* left alt key */ break; default: if (sc->sc_composed_char > 0) { sc->sc_flags &= ~HKBD_FLAG_COMPOSE; sc->sc_composed_char = 0; goto errkey; } break; } } /* keycode to key action */ action = genkbd_keyaction(kbd, SCAN_CHAR(keycode), (keycode & SCAN_RELEASE), &sc->sc_state, &sc->sc_accents); if (action == NOKEY) { goto next_code; } done: return (action); check_composed: if (sc->sc_composed_char <= 0xFF) { goto next_code; } errkey: return (ERRKEY); } /* Currently wait is always false. */ static uint32_t hkbd_read_char(keyboard_t *kbd, int wait) { uint32_t keycode; SYSCONS_LOCK(); keycode = hkbd_read_char_locked(kbd, wait); SYSCONS_UNLOCK(); return (keycode); } /* some useful control functions */ static int hkbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg) { struct hkbd_softc *sc = kbd->kb_data; #ifdef EVDEV_SUPPORT struct epoch_tracker et; #endif int error; int i; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) int ival; #endif SYSCONS_LOCK_ASSERT(); switch (cmd) { case KDGKBMODE: /* get keyboard mode */ *(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 */ 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) { if ((sc->sc_flags & HKBD_FLAG_POLLING) == 0) hkbd_clear_state(kbd); sc->sc_mode = *(int *)arg; } break; default: 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 "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)) { error = hkbd_set_leds(sc, i); if (error) return (error); } #ifdef EVDEV_SUPPORT if (sc->sc_evdev != NULL && !HID_IN_POLLING_MODE()) { epoch_enter_preempt(INPUT_EPOCH, &et); evdev_push_leds(sc->sc_evdev, i); epoch_exit_preempt(INPUT_EPOCH, &et); } #endif KBD_LED_VAL(kbd) = *(int *)arg; 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; /* set LEDs and quit */ return (hkbd_ioctl_locked(kbd, KDSETLED, arg)); case KDSETREPEAT: /* set keyboard repeat rate (new * interface) */ if (!KBD_HAS_DEVICE(kbd)) { return (0); } /* * Convert negative, zero and tiny args to the same limits * as atkbd. We could support delays of 1 msec, but * anything much shorter than the shortest atkbd value * of 250.34 is almost unusable as well as incompatible. */ kbd->kb_delay1 = imax(((int *)arg)[0], 250); kbd->kb_delay2 = imax(((int *)arg)[1], 34); #ifdef EVDEV_SUPPORT if (sc->sc_evdev != NULL && !HID_IN_POLLING_MODE()) { epoch_enter_preempt(INPUT_EPOCH, &et); evdev_push_repeats(sc->sc_evdev, kbd); epoch_exit_preempt(INPUT_EPOCH, &et); } #endif return (0); #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) */ return (hkbd_set_typematic(kbd, *(int *)arg)); 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 */ sc->sc_accents = 0; /* FALLTHROUGH */ default: return (genkbd_commonioctl(kbd, cmd, arg)); } return (0); } static int hkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) { int result; /* * XXX Check if someone is calling us from a critical section: */ if (curthread->td_critnest != 0) return (EDEADLK); /* * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any * context where printf(9) can be called, which among other things * includes interrupt filters and threads with any kinds of locks * already held. For this reason it would be dangerous to acquire * the Giant here unconditionally. On the other hand we have to * have it to handle the ioctl. * So we make our best effort to auto-detect whether we can grab * the Giant or not. Blame syscons(4) for this. */ switch (cmd) { case KDGKBSTATE: case KDSKBSTATE: case KDSETLED: if (!mtx_owned(&Giant) && !HID_IN_POLLING_MODE()) return (EDEADLK); /* best I could come up with */ /* FALLTHROUGH */ default: SYSCONS_LOCK(); result = hkbd_ioctl_locked(kbd, cmd, arg); SYSCONS_UNLOCK(); return (result); } } /* clear the internal state of the keyboard */ static void hkbd_clear_state(keyboard_t *kbd) { struct hkbd_softc *sc = kbd->kb_data; SYSCONS_LOCK_ASSERT(); sc->sc_flags &= ~(HKBD_FLAG_COMPOSE | HKBD_FLAG_POLLING); sc->sc_state &= LOCK_MASK; /* preserve locking key state */ sc->sc_accents = 0; sc->sc_composed_char = 0; #ifdef HKBD_EMULATE_ATSCANCODE sc->sc_buffered_char[0] = 0; sc->sc_buffered_char[1] = 0; #endif memset(&sc->sc_ndata, 0, bitstr_size(HKBD_NKEYCODE)); memset(&sc->sc_odata, 0, bitstr_size(HKBD_NKEYCODE)); memset(&sc->sc_ndata0, 0, bitstr_size(HKBD_NKEYCODE)); memset(&sc->sc_odata0, 0, bitstr_size(HKBD_NKEYCODE)); sc->sc_repeat_time = 0; sc->sc_repeat_key = 0; } /* save the internal state, not used */ static int hkbd_get_state(keyboard_t *kbd, void *buf, size_t len) { return (len == 0) ? 1 : -1; } /* set the internal state, not used */ static int hkbd_set_state(keyboard_t *kbd, void *buf, size_t len) { return (EINVAL); } static int hkbd_poll(keyboard_t *kbd, int on) { struct hkbd_softc *sc = kbd->kb_data; SYSCONS_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 |= HKBD_FLAG_POLLING; sc->sc_poll_thread = curthread; } else { sc->sc_flags &= ~HKBD_FLAG_POLLING; sc->sc_delay = 0; } SYSCONS_UNLOCK(); return (0); } /* local functions */ static int hkbd_set_leds(struct hkbd_softc *sc, uint8_t leds) { uint8_t id; uint8_t any; uint8_t *buf; int len; int error; SYSCONS_LOCK_ASSERT(); DPRINTF("leds=0x%02x\n", leds); #ifdef HID_DEBUG if (hkbd_no_leds) return (0); #endif memset(sc->sc_buffer, 0, HKBD_BUFFER_SIZE); id = sc->sc_id_leds; any = 0; /* Assumption: All led bits must be in the same ID. */ if (sc->sc_flags & HKBD_FLAG_NUMLOCK) { hid_put_udata(sc->sc_buffer + 1, HKBD_BUFFER_SIZE - 1, &sc->sc_loc_numlock, leds & NLKED ? 1 : 0); any = 1; } if (sc->sc_flags & HKBD_FLAG_SCROLLLOCK) { hid_put_udata(sc->sc_buffer + 1, HKBD_BUFFER_SIZE - 1, &sc->sc_loc_scrolllock, leds & SLKED ? 1 : 0); any = 1; } if (sc->sc_flags & HKBD_FLAG_CAPSLOCK) { hid_put_udata(sc->sc_buffer + 1, HKBD_BUFFER_SIZE - 1, &sc->sc_loc_capslock, leds & CLKED ? 1 : 0); any = 1; } /* if no leds, nothing to do */ if (!any) return (0); /* range check output report length */ len = sc->sc_led_size; if (len > (HKBD_BUFFER_SIZE - 1)) len = (HKBD_BUFFER_SIZE - 1); /* check if we need to prefix an ID byte */ if (id != 0) { sc->sc_buffer[0] = id; buf = sc->sc_buffer; } else { buf = sc->sc_buffer + 1; } DPRINTF("len=%d, id=%d\n", len, id); /* start data transfer */ SYSCONS_UNLOCK(); error = hid_write(sc->sc_dev, buf, len); SYSCONS_LOCK(); return (error); } static int hkbd_set_typematic(keyboard_t *kbd, int code) { #ifdef EVDEV_SUPPORT struct hkbd_softc *sc = kbd->kb_data; #endif static const int delays[] = {250, 500, 750, 1000}; static const 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]; #ifdef EVDEV_SUPPORT if (sc->sc_evdev != NULL) evdev_push_repeats(sc->sc_evdev, kbd); #endif return (0); } #ifdef HKBD_EMULATE_ATSCANCODE static uint32_t hkbd_atkeycode(int usbcode, const bitstr_t *bitmap) { uint32_t keycode; keycode = hkbd_trtab[KEY_INDEX(usbcode)]; /* * Translate Alt-PrintScreen to SysRq. * * Some or all AT keyboards connected through USB have already * mapped Alted PrintScreens to an unusual usbcode (0x8a). * hkbd_trtab translates this to 0x7e, and key2scan() would * translate that to 0x79 (Intl' 4). Assume that if we have * an Alted 0x7e here then it actually is an Alted PrintScreen. * * The usual usbcode for all PrintScreens is 0x46. hkbd_trtab * translates this to 0x5c, so the Alt check to classify 0x5c * is routine. */ if ((keycode == 0x5c || keycode == 0x7e) && (HKBD_KEY_PRESSED(bitmap, 0xe2 /* ALT-L */) || HKBD_KEY_PRESSED(bitmap, 0xe6 /* ALT-R */))) return (0x54); return (keycode); } static int hkbd_key2scan(struct hkbd_softc *sc, int code, const bitstr_t *bitmap, int up) { static const int scan[] = { /* 89 */ 0x11c, /* Enter */ /* 90-99 */ 0x11d, /* Ctrl-R */ 0x135, /* Divide */ 0x137, /* PrintScreen */ 0x138, /* Alt-R */ 0x147, /* Home */ 0x148, /* Up */ 0x149, /* PageUp */ 0x14b, /* Left */ 0x14d, /* Right */ 0x14f, /* End */ /* 100-109 */ 0x150, /* Down */ 0x151, /* PageDown */ 0x152, /* Insert */ 0x153, /* Delete */ 0x146, /* Pause/Break */ 0x15b, /* Win_L(Super_L) */ 0x15c, /* Win_R(Super_R) */ 0x15d, /* Application(Menu) */ /* SUN TYPE 6 USB KEYBOARD */ 0x168, /* Sun Type 6 Help */ 0x15e, /* Sun Type 6 Stop */ /* 110 - 119 */ 0x15f, /* Sun Type 6 Again */ 0x160, /* Sun Type 6 Props */ 0x161, /* Sun Type 6 Undo */ 0x162, /* Sun Type 6 Front */ 0x163, /* Sun Type 6 Copy */ 0x164, /* Sun Type 6 Open */ 0x165, /* Sun Type 6 Paste */ 0x166, /* Sun Type 6 Find */ 0x167, /* Sun Type 6 Cut */ 0x125, /* Sun Type 6 Mute */ /* 120 - 130 */ 0x11f, /* Sun Type 6 VolumeDown */ 0x11e, /* Sun Type 6 VolumeUp */ 0x120, /* Sun Type 6 PowerDown */ /* Japanese 106/109 keyboard */ 0x73, /* Keyboard Intl' 1 (backslash / underscore) */ 0x70, /* Keyboard Intl' 2 (Katakana / Hiragana) */ 0x7d, /* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */ 0x79, /* Keyboard Intl' 4 (Henkan) */ 0x7b, /* Keyboard Intl' 5 (Muhenkan) */ 0x5c, /* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */ 0x71, /* Apple Keyboard JIS (Kana) */ 0x72, /* Apple Keyboard JIS (Eisu) */ }; if ((code >= 89) && (code < (int)(89 + nitems(scan)))) { code = scan[code - 89]; } /* PrintScreen */ if (code == 0x137 && (!( HKBD_KEY_PRESSED(bitmap, 0xe0 /* CTRL-L */) || HKBD_KEY_PRESSED(bitmap, 0xe4 /* CTRL-R */) || HKBD_KEY_PRESSED(bitmap, 0xe1 /* SHIFT-L */) || HKBD_KEY_PRESSED(bitmap, 0xe5 /* SHIFT-R */)))) { code |= SCAN_PREFIX_SHIFT; } /* Pause/Break */ if ((code == 0x146) && (!( HKBD_KEY_PRESSED(bitmap, 0xe0 /* CTRL-L */) || HKBD_KEY_PRESSED(bitmap, 0xe4 /* CTRL-R */)))) { code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL); } code |= (up ? SCAN_RELEASE : SCAN_PRESS); if (code & SCAN_PREFIX) { if (code & SCAN_PREFIX_CTL) { /* Ctrl */ sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE)); sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX); } else if (code & SCAN_PREFIX_SHIFT) { /* Shift */ sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE)); sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT); } else { sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX); sc->sc_buffered_char[1] = 0; } return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); } return (code); } #endif /* HKBD_EMULATE_ATSCANCODE */ static keyboard_switch_t hkbdsw = { .probe = &hkbd__probe, .init = &hkbd_init, .term = &hkbd_term, .intr = &hkbd_intr, .test_if = &hkbd_test_if, .enable = &hkbd_enable, .disable = &hkbd_disable, .read = &hkbd_read, .check = &hkbd_check, .read_char = &hkbd_read_char, .check_char = &hkbd_check_char, .ioctl = &hkbd_ioctl, .lock = &hkbd_lock, .clear_state = &hkbd_clear_state, .get_state = &hkbd_get_state, .set_state = &hkbd_set_state, .poll = &hkbd_poll, }; KEYBOARD_DRIVER(hkbd, hkbdsw, hkbd_configure); static int hkbd_driver_load(module_t mod, int what, void *arg) { switch (what) { case MOD_LOAD: kbd_add_driver(&hkbd_kbd_driver); break; case MOD_UNLOAD: kbd_delete_driver(&hkbd_kbd_driver); break; } return (0); } static device_method_t hkbd_methods[] = { DEVMETHOD(device_probe, hkbd_probe), DEVMETHOD(device_attach, hkbd_attach), DEVMETHOD(device_detach, hkbd_detach), DEVMETHOD(device_resume, hkbd_resume), DEVMETHOD_END }; static driver_t hkbd_driver = { .name = "hkbd", .methods = hkbd_methods, .size = sizeof(struct hkbd_softc), }; DRIVER_MODULE(hkbd, hidbus, hkbd_driver, hkbd_driver_load, NULL); MODULE_DEPEND(hkbd, hid, 1, 1, 1); MODULE_DEPEND(hkbd, hidbus, 1, 1, 1); #ifdef EVDEV_SUPPORT MODULE_DEPEND(hkbd, evdev, 1, 1, 1); #endif MODULE_VERSION(hkbd, 1); HID_PNP_INFO(hkbd_devs); diff --git a/sys/dev/hyperv/input/hv_kbd.c b/sys/dev/hyperv/input/hv_kbd.c index 53aacda7fbcb..ea1a8b883fbb 100644 --- a/sys/dev/hyperv/input/hv_kbd.c +++ b/sys/dev/hyperv/input/hv_kbd.c @@ -1,857 +1,859 @@ /*- * 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 "opt_evdev.h" #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 #ifdef EVDEV_SUPPORT #include #include #endif #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_COMPOSE 0x00000001 /* compose char flag */ #define HVKBD_FLAG_POLLING 0x00000002 #ifdef EVDEV_SUPPORT static evdev_event_t hvkbd_ev_event; static const struct evdev_methods hvkbd_evdev_methods = { .ev_event = hvkbd_ev_event, }; #endif /* 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_COMPOSE) && sc->sc_composed_char != 0) return (TRUE); 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; uint32_t action; keystroke ks; hv_kbd_sc *sc = kbd->kb_data; int keycode; HVKBD_LOCK_ASSERT(); if (!KBD_IS_ACTIVE(kbd) || !hv_kbd_prod_is_ready(sc)) return (NOKEY); next_code: /* do we have a composed char to return? */ if (!(sc->sc_flags & HVKBD_FLAG_COMPOSE) && sc->sc_composed_char > 0) { action = sc->sc_composed_char; sc->sc_composed_char = 0; if (action > UCHAR_MAX) { return (ERRKEY); } return (action); } 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); } #ifdef EVDEV_SUPPORT /* push evdev event */ if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && sc->ks_evdev != NULL) { keycode = evdev_scancode2key(&sc->ks_evdev_state, scancode); if (keycode != KEY_RESERVED) { evdev_push_event(sc->ks_evdev, EV_KEY, (uint16_t)keycode, scancode & 0x80 ? 0 : 1); evdev_sync(sc->ks_evdev); } } + if (sc->ks_evdev != NULL && evdev_is_grabbed(sc->ks_evdev)) + return (NOKEY); #endif ++kbd->kb_count; DEBUG_HVKBD(kbd, "read scan: 0x%x\n", scancode); /* return the byte as is for the K_RAW mode */ if (sc->sc_mode == K_RAW) return scancode; /* translate the scan code into a keycode */ keycode = scancode & 0x7F; switch (sc->sc_prefix) { case 0x00: /* normal scancode */ switch(scancode) { case 0xB8: /* left alt (compose key) released */ if (sc->sc_flags & HVKBD_FLAG_COMPOSE) { sc->sc_flags &= ~HVKBD_FLAG_COMPOSE; if (sc->sc_composed_char > UCHAR_MAX) sc->sc_composed_char = 0; } break; case 0x38: /* left alt (compose key) pressed */ if (!(sc->sc_flags & HVKBD_FLAG_COMPOSE)) { sc->sc_flags |= HVKBD_FLAG_COMPOSE; sc->sc_composed_char = 0; } break; case 0xE0: case 0xE1: sc->sc_prefix = scancode; goto next_code; } break; case 0xE0: /* 0xE0 prefix */ sc->sc_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.) */ sc->sc_prefix = 0; if (keycode == 0x1D) sc->sc_prefix = 0x1D; goto next_code; /* NOT REACHED */ case 0x1D: /* pause / break */ sc->sc_prefix = 0; if (keycode != 0x45) goto next_code; keycode = 0x68; break; } /* XXX assume 101/102 keys AT keyboard */ switch (keycode) { case 0x5c: /* print screen */ if (sc->sc_flags & ALTS) keycode = 0x54; /* sysrq */ break; case 0x68: /* pause/break */ if (sc->sc_flags & CTLS) keycode = 0x6c; /* break */ break; } /* return the key code in the K_CODE mode */ if (sc->sc_mode == K_CODE) return (keycode | (scancode & 0x80)); /* compose a character code */ if (sc->sc_flags & HVKBD_FLAG_COMPOSE) { switch (keycode | (scancode & 0x80)) { /* key pressed, process it */ case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */ sc->sc_composed_char *= 10; sc->sc_composed_char += keycode - 0x40; if (sc->sc_composed_char > UCHAR_MAX) return ERRKEY; goto next_code; case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */ sc->sc_composed_char *= 10; sc->sc_composed_char += keycode - 0x47; if (sc->sc_composed_char > UCHAR_MAX) return ERRKEY; goto next_code; case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */ sc->sc_composed_char *= 10; sc->sc_composed_char += keycode - 0x4E; if (sc->sc_composed_char > UCHAR_MAX) return ERRKEY; goto next_code; case 0x52: /* keypad 0 */ sc->sc_composed_char *= 10; if (sc->sc_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 (sc->sc_composed_char > 0) { sc->sc_flags &= ~HVKBD_FLAG_COMPOSE; sc->sc_composed_char = 0; return (ERRKEY); } break; } } /* keycode to key action */ action = genkbd_keyaction(kbd, keycode, scancode & 0x80, &sc->sc_state, &sc->sc_accents); if (action == NOKEY) goto next_code; else return (action); } /* 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 | HVKBD_FLAG_COMPOSE); sc->sc_accents = 0; sc->sc_composed_char = 0; } 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); } #ifdef EVDEV_SUPPORT /* push LED states to evdev */ if (sc->ks_evdev != NULL && evdev_rcpt_mask & EVDEV_RCPT_HW_KBD) evdev_push_leds(sc->ks_evdev, *(int *)arg); #endif KBD_LED_VAL(kbd) = *(int *)arg; break; 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 */ sc->sc_accents = 0; /* FALLTHROUGH */ 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); } #ifdef EVDEV_SUPPORT static void hvkbd_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 static keyboard_switch_t hvkbdsw = { .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 */ .poll = hvkbd_poll, }; 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; #ifdef EVDEV_SUPPORT struct evdev_dev *evdev; #endif 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_XLATE; (*sw->enable)(kbd); #ifdef EVDEV_SUPPORT evdev = evdev_alloc(); evdev_set_name(evdev, "Hyper-V keyboard"); evdev_set_phys(evdev, device_get_nameunit(dev)); evdev_set_id(evdev, BUS_VIRTUAL, 0, 0, 0); evdev_set_methods(evdev, kbd, &hvkbd_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 sc->ks_evdev = evdev; sc->ks_evdev_state = 0; #endif if (kbd_register(kbd) < 0) { goto detach; } KBD_CONFIG_DONE(kbd); #ifdef KBD_INSTALL_CDEV if (kbd_attach(kbd)) { goto detach; } #endif if (bootverbose) { kbdd_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); #ifdef EVDEV_SUPPORT evdev_free(sc->ks_evdev); #endif 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); } diff --git a/sys/dev/kbdmux/kbdmux.c b/sys/dev/kbdmux/kbdmux.c index 3bbdc78b7c1a..b70e956830e5 100644 --- a/sys/dev/kbdmux/kbdmux.c +++ b/sys/dev/kbdmux/kbdmux.c @@ -1,1439 +1,1442 @@ /* * kbdmux.c */ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2005 Maksim Yevmenkin * 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. * * $Id: kbdmux.c,v 1.4 2005/07/14 17:38:35 max Exp $ * $FreeBSD$ */ #include "opt_evdev.h" #include "opt_kbd.h" #include "opt_kbdmux.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* the initial key map, accent map and fkey strings */ #ifdef KBDMUX_DFLT_KEYMAP #define KBD_DFLT_KEYMAP #include "kbdmuxmap.h" #endif #include #ifdef EVDEV_SUPPORT #include #include #endif #define KEYBOARD_NAME "kbdmux" MALLOC_DECLARE(M_KBDMUX); MALLOC_DEFINE(M_KBDMUX, KEYBOARD_NAME, "Keyboard multiplexor"); /***************************************************************************** ***************************************************************************** ** Keyboard state ***************************************************************************** *****************************************************************************/ #define KBDMUX_Q_SIZE 512 /* input queue size */ /* * XXX * For now rely on Giant mutex to protect our data structures. * Just like the rest of keyboard drivers and syscons(4) do. * Note that callout is initialized as not MP-safe to make sure * Giant is held. */ #if 0 /* not yet */ #define KBDMUX_LOCK_DECL_GLOBAL \ struct mtx ks_lock #define KBDMUX_LOCK_INIT(s) \ mtx_init(&(s)->ks_lock, "kbdmux", NULL, MTX_DEF|MTX_RECURSE) #define KBDMUX_LOCK_DESTROY(s) \ mtx_destroy(&(s)->ks_lock) #define KBDMUX_LOCK(s) \ mtx_lock(&(s)->ks_lock) #define KBDMUX_UNLOCK(s) \ mtx_unlock(&(s)->ks_lock) #define KBDMUX_LOCK_ASSERT(s, w) \ mtx_assert(&(s)->ks_lock, (w)) #else #define KBDMUX_LOCK_DECL_GLOBAL #define KBDMUX_LOCK_INIT(s) #define KBDMUX_LOCK_DESTROY(s) #define KBDMUX_LOCK(s) #define KBDMUX_UNLOCK(s) #define KBDMUX_LOCK_ASSERT(s, w) #endif /* not yet */ /* * kbdmux keyboard */ struct kbdmux_kbd { keyboard_t *kbd; /* keyboard */ SLIST_ENTRY(kbdmux_kbd) next; /* link to next */ }; typedef struct kbdmux_kbd kbdmux_kbd_t; /* * kbdmux state */ struct kbdmux_state { char ks_inq[KBDMUX_Q_SIZE]; /* input chars queue */ unsigned int ks_inq_start; unsigned int ks_inq_length; struct task ks_task; /* interrupt task */ struct callout ks_timo; /* timeout handler */ #define TICKS (hz) /* rate */ int ks_flags; /* flags */ #define COMPOSE (1 << 0) /* compose char flag */ int ks_polling; /* poll nesting count */ int ks_mode; /* K_XLATE, K_RAW, K_CODE */ int ks_state; /* state */ int ks_accents; /* accent key index (> 0) */ u_int ks_composed_char; /* composed char code */ u_char ks_prefix; /* AT scan code prefix */ #ifdef EVDEV_SUPPORT struct evdev_dev * ks_evdev; int ks_evdev_state; #endif SLIST_HEAD(, kbdmux_kbd) ks_kbds; /* keyboards */ KBDMUX_LOCK_DECL_GLOBAL; }; typedef struct kbdmux_state kbdmux_state_t; /***************************************************************************** ***************************************************************************** ** Helper functions ***************************************************************************** *****************************************************************************/ static task_fn_t kbdmux_kbd_intr; static callout_func_t kbdmux_kbd_intr_timo; static kbd_callback_func_t kbdmux_kbd_event; static void kbdmux_kbd_putc(kbdmux_state_t *state, char c) { unsigned int p; if (state->ks_inq_length == KBDMUX_Q_SIZE) return; p = (state->ks_inq_start + state->ks_inq_length) % KBDMUX_Q_SIZE; state->ks_inq[p] = c; state->ks_inq_length++; } static int kbdmux_kbd_getc(kbdmux_state_t *state) { unsigned char c; if (state->ks_inq_length == 0) return (-1); c = state->ks_inq[state->ks_inq_start]; state->ks_inq_start = (state->ks_inq_start + 1) % KBDMUX_Q_SIZE; state->ks_inq_length--; return (c); } /* * Interrupt handler task */ void kbdmux_kbd_intr(void *xkbd, int pending) { keyboard_t *kbd = (keyboard_t *) xkbd; kbdd_intr(kbd, NULL); } /* * Schedule interrupt handler on timeout. Called with locked state. */ void kbdmux_kbd_intr_timo(void *xstate) { kbdmux_state_t *state = (kbdmux_state_t *) xstate; /* queue interrupt task if needed */ if (state->ks_inq_length > 0) taskqueue_enqueue(taskqueue_swi_giant, &state->ks_task); /* re-schedule timeout */ callout_schedule(&state->ks_timo, TICKS); } /* * Process event from one of our keyboards */ static int kbdmux_kbd_event(keyboard_t *kbd, int event, void *arg) { kbdmux_state_t *state = (kbdmux_state_t *) arg; switch (event) { case KBDIO_KEYINPUT: { int c; KBDMUX_LOCK(state); /* * Read all chars from the keyboard * * Turns out that atkbd(4) check_char() method may return * "true" while read_char() method returns NOKEY. If this * happens we could stuck in the loop below. Avoid this * by breaking out of the loop if read_char() method returns * NOKEY. */ while (kbdd_check_char(kbd)) { c = kbdd_read_char(kbd, 0); if (c == NOKEY) break; if (c == ERRKEY) continue; /* XXX ring bell */ if (!KBD_IS_BUSY(kbd)) continue; /* not open - discard the input */ kbdmux_kbd_putc(state, c); } /* queue interrupt task if needed */ if (state->ks_inq_length > 0) taskqueue_enqueue(taskqueue_swi_giant, &state->ks_task); KBDMUX_UNLOCK(state); } break; case KBDIO_UNLOADING: { kbdmux_kbd_t *k; KBDMUX_LOCK(state); SLIST_FOREACH(k, &state->ks_kbds, next) if (k->kbd == kbd) break; if (k != NULL) { kbd_release(k->kbd, &k->kbd); SLIST_REMOVE(&state->ks_kbds, k, kbdmux_kbd, next); k->kbd = NULL; free(k, M_KBDMUX); } KBDMUX_UNLOCK(state); } break; default: return (EINVAL); /* NOT REACHED */ } return (0); } /**************************************************************************** **************************************************************************** ** Keyboard driver **************************************************************************** ****************************************************************************/ static int kbdmux_configure(int flags); static kbd_probe_t kbdmux_probe; static kbd_init_t kbdmux_init; static kbd_term_t kbdmux_term; static kbd_intr_t kbdmux_intr; static kbd_test_if_t kbdmux_test_if; static kbd_enable_t kbdmux_enable; static kbd_disable_t kbdmux_disable; static kbd_read_t kbdmux_read; static kbd_check_t kbdmux_check; static kbd_read_char_t kbdmux_read_char; static kbd_check_char_t kbdmux_check_char; static kbd_ioctl_t kbdmux_ioctl; static kbd_lock_t kbdmux_lock; static void kbdmux_clear_state_locked(kbdmux_state_t *state); static kbd_clear_state_t kbdmux_clear_state; static kbd_get_state_t kbdmux_get_state; static kbd_set_state_t kbdmux_set_state; static kbd_poll_mode_t kbdmux_poll; static keyboard_switch_t kbdmuxsw = { .probe = kbdmux_probe, .init = kbdmux_init, .term = kbdmux_term, .intr = kbdmux_intr, .test_if = kbdmux_test_if, .enable = kbdmux_enable, .disable = kbdmux_disable, .read = kbdmux_read, .check = kbdmux_check, .read_char = kbdmux_read_char, .check_char = kbdmux_check_char, .ioctl = kbdmux_ioctl, .lock = kbdmux_lock, .clear_state = kbdmux_clear_state, .get_state = kbdmux_get_state, .set_state = kbdmux_set_state, .poll = kbdmux_poll, }; #ifdef EVDEV_SUPPORT static evdev_event_t kbdmux_ev_event; static const struct evdev_methods kbdmux_evdev_methods = { .ev_event = kbdmux_ev_event, }; #endif /* * Return the number of found keyboards */ static int kbdmux_configure(int flags) { return (1); } /* * Detect a keyboard */ static int kbdmux_probe(int unit, void *arg, int flags) { if (resource_disabled(KEYBOARD_NAME, unit)) return (ENXIO); return (0); } /* * Reset and initialize the keyboard (stolen from atkbd.c) */ static int kbdmux_init(int unit, keyboard_t **kbdp, void *arg, int flags) { keyboard_t *kbd = NULL; kbdmux_state_t *state = NULL; keymap_t *keymap = NULL; accentmap_t *accmap = NULL; fkeytab_t *fkeymap = NULL; int error, needfree, fkeymap_size, delay[2]; #ifdef EVDEV_SUPPORT struct evdev_dev *evdev; char phys_loc[NAMELEN]; #endif if (*kbdp == NULL) { *kbdp = kbd = malloc(sizeof(*kbd), M_KBDMUX, M_NOWAIT | M_ZERO); state = malloc(sizeof(*state), M_KBDMUX, M_NOWAIT | M_ZERO); keymap = malloc(sizeof(key_map), M_KBDMUX, M_NOWAIT); accmap = malloc(sizeof(accent_map), M_KBDMUX, M_NOWAIT); fkeymap = malloc(sizeof(fkey_tab), M_KBDMUX, 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; } KBDMUX_LOCK_INIT(state); TASK_INIT(&state->ks_task, 0, kbdmux_kbd_intr, (void *) kbd); callout_init(&state->ks_timo, 1); SLIST_INIT(&state->ks_kbds); } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) { return (0); } else { kbd = *kbdp; state = (kbdmux_state_t *) kbd->kb_data; 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)) { /* XXX assume 101/102 keys keyboard */ kbd_init_struct(kbd, KEYBOARD_NAME, KB_101, 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; KBD_FOUND_DEVICE(kbd); KBD_PROBE_DONE(kbd); KBDMUX_LOCK(state); kbdmux_clear_state_locked(state); state->ks_mode = K_XLATE; KBDMUX_UNLOCK(state); } if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) { kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY; kbdmux_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state); delay[0] = kbd->kb_delay1; delay[1] = kbd->kb_delay2; kbdmux_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); #ifdef EVDEV_SUPPORT /* register as evdev provider */ evdev = evdev_alloc(); evdev_set_name(evdev, "System keyboard multiplexer"); snprintf(phys_loc, NAMELEN, KEYBOARD_NAME"%d", unit); evdev_set_phys(evdev, phys_loc); evdev_set_id(evdev, BUS_VIRTUAL, 0, 0, 0); evdev_set_methods(evdev, kbd, &kbdmux_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); callout_reset(&state->ks_timo, TICKS, kbdmux_kbd_intr_timo, state); } return (0); bad: if (needfree) { if (state != NULL) free(state, M_KBDMUX); if (keymap != NULL) free(keymap, M_KBDMUX); if (accmap != NULL) free(accmap, M_KBDMUX); if (fkeymap != NULL) free(fkeymap, M_KBDMUX); if (kbd != NULL) { free(kbd, M_KBDMUX); *kbdp = NULL; /* insure ref doesn't leak to caller */ } } return (error); } /* * Finish using this keyboard */ static int kbdmux_term(keyboard_t *kbd) { kbdmux_state_t *state = (kbdmux_state_t *) kbd->kb_data; kbdmux_kbd_t *k; /* release all keyboards from the mux */ KBDMUX_LOCK(state); while ((k = SLIST_FIRST(&state->ks_kbds)) != NULL) { kbd_release(k->kbd, &k->kbd); SLIST_REMOVE_HEAD(&state->ks_kbds, next); k->kbd = NULL; free(k, M_KBDMUX); } KBDMUX_UNLOCK(state); callout_drain(&state->ks_timo); taskqueue_drain(taskqueue_swi_giant, &state->ks_task); kbd_unregister(kbd); #ifdef EVDEV_SUPPORT evdev_free(state->ks_evdev); #endif KBDMUX_LOCK_DESTROY(state); bzero(state, sizeof(*state)); free(state, M_KBDMUX); free(kbd->kb_keymap, M_KBDMUX); free(kbd->kb_accentmap, M_KBDMUX); free(kbd->kb_fkeytab, M_KBDMUX); free(kbd, M_KBDMUX); return (0); } /* * Keyboard interrupt routine */ static int kbdmux_intr(keyboard_t *kbd, void *arg) { int c; 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 = kbdmux_read_char(kbd, FALSE); } while (c != NOKEY); } return (0); } /* * Test the interface to the device */ static int kbdmux_test_if(keyboard_t *kbd) { return (0); } /* * Enable the access to the device; until this function is called, * the client cannot read from the keyboard. */ static int kbdmux_enable(keyboard_t *kbd) { KBD_ACTIVATE(kbd); return (0); } /* * Disallow the access to the device */ static int kbdmux_disable(keyboard_t *kbd) { KBD_DEACTIVATE(kbd); return (0); } /* * Read one byte from the keyboard if it's allowed */ static int kbdmux_read(keyboard_t *kbd, int wait) { kbdmux_state_t *state = (kbdmux_state_t *) kbd->kb_data; int c; KBDMUX_LOCK(state); c = kbdmux_kbd_getc(state); KBDMUX_UNLOCK(state); if (c != -1) kbd->kb_count ++; return (KBD_IS_ACTIVE(kbd)? c : -1); } /* * Check if data is waiting */ static int kbdmux_check(keyboard_t *kbd) { kbdmux_state_t *state = (kbdmux_state_t *) kbd->kb_data; int ready; if (!KBD_IS_ACTIVE(kbd)) return (FALSE); KBDMUX_LOCK(state); ready = (state->ks_inq_length > 0) ? TRUE : FALSE; KBDMUX_UNLOCK(state); return (ready); } /* * Read char from the keyboard (stolen from atkbd.c) */ static u_int kbdmux_read_char(keyboard_t *kbd, int wait) { kbdmux_state_t *state = (kbdmux_state_t *) kbd->kb_data; u_int action; int scancode, keycode; KBDMUX_LOCK(state); 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) { KBDMUX_UNLOCK(state); return (ERRKEY); } KBDMUX_UNLOCK(state); return (action); } /* see if there is something in the keyboard queue */ scancode = kbdmux_kbd_getc(state); if (scancode == -1) { if (state->ks_polling != 0) { kbdmux_kbd_t *k; SLIST_FOREACH(k, &state->ks_kbds, next) { while (kbdd_check_char(k->kbd)) { scancode = kbdd_read_char(k->kbd, 0); if (scancode == NOKEY) break; if (scancode == ERRKEY) continue; if (!KBD_IS_BUSY(k->kbd)) continue; kbdmux_kbd_putc(state, scancode); } } if (state->ks_inq_length > 0) goto next_code; } KBDMUX_UNLOCK(state); return (NOKEY); } /* XXX FIXME: check for -1 if wait == 1! */ kbd->kb_count ++; #ifdef EVDEV_SUPPORT /* push evdev event */ if (evdev_rcpt_mask & EVDEV_RCPT_KBDMUX && state->ks_evdev != NULL) { uint16_t key = evdev_scancode2key(&state->ks_evdev_state, scancode); if (key != KEY_RESERVED) { evdev_push_event(state->ks_evdev, EV_KEY, key, scancode & 0x80 ? 0 : 1); evdev_sync(state->ks_evdev); } } + + if (state->ks_evdev != NULL && evdev_is_grabbed(state->ks_evdev)) + return (NOKEY); #endif /* return the byte as is for the K_RAW mode */ if (state->ks_mode == K_RAW) { KBDMUX_UNLOCK(state); 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; case 0x64: /* [JP106USB] backslash, underscore */ keycode = 0x73; 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; } /* XXX assume 101/102 keys AT keyboard */ 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) { KBDMUX_UNLOCK(state); 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) { KBDMUX_UNLOCK(state); 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) { KBDMUX_UNLOCK(state); 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) { KBDMUX_UNLOCK(state); return (ERRKEY); } goto next_code; case 0x52: /* keypad 0 */ state->ks_composed_char *= 10; if (state->ks_composed_char > UCHAR_MAX) { KBDMUX_UNLOCK(state); 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; KBDMUX_UNLOCK(state); 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; KBDMUX_UNLOCK(state); return (action); } /* * Check if char is waiting */ static int kbdmux_check_char(keyboard_t *kbd) { kbdmux_state_t *state = (kbdmux_state_t *) kbd->kb_data; int ready; if (!KBD_IS_ACTIVE(kbd)) return (FALSE); KBDMUX_LOCK(state); if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char != 0)) ready = TRUE; else ready = (state->ks_inq_length > 0) ? TRUE : FALSE; KBDMUX_UNLOCK(state); return (ready); } /* * Keyboard ioctl's */ static int kbdmux_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) { 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 }; kbdmux_state_t *state = (kbdmux_state_t *) kbd->kb_data; kbdmux_kbd_t *k; keyboard_info_t *ki; int error = 0, mode; #ifdef COMPAT_FREEBSD6 int ival; #endif if (state == NULL) return (ENXIO); switch (cmd) { case KBADDKBD: /* add keyboard to the mux */ ki = (keyboard_info_t *) arg; if (ki == NULL || ki->kb_unit < 0 || ki->kb_name[0] == '\0' || strcmp(ki->kb_name, "*") == 0) return (EINVAL); /* bad input */ KBDMUX_LOCK(state); SLIST_FOREACH(k, &state->ks_kbds, next) if (k->kbd->kb_unit == ki->kb_unit && strcmp(k->kbd->kb_name, ki->kb_name) == 0) break; if (k != NULL) { KBDMUX_UNLOCK(state); return (0); /* keyboard already in the mux */ } k = malloc(sizeof(*k), M_KBDMUX, M_NOWAIT | M_ZERO); if (k == NULL) { KBDMUX_UNLOCK(state); return (ENOMEM); /* out of memory */ } k->kbd = kbd_get_keyboard( kbd_allocate( ki->kb_name, ki->kb_unit, (void *) &k->kbd, kbdmux_kbd_event, (void *) state)); if (k->kbd == NULL) { KBDMUX_UNLOCK(state); free(k, M_KBDMUX); return (EINVAL); /* bad keyboard */ } kbdd_enable(k->kbd); kbdd_clear_state(k->kbd); /* set K_RAW mode on slave keyboard */ mode = K_RAW; error = kbdd_ioctl(k->kbd, KDSKBMODE, (caddr_t)&mode); if (error == 0) { /* set lock keys state on slave keyboard */ mode = state->ks_state & LOCK_MASK; error = kbdd_ioctl(k->kbd, KDSKBSTATE, (caddr_t)&mode); } if (error != 0) { KBDMUX_UNLOCK(state); kbd_release(k->kbd, &k->kbd); k->kbd = NULL; free(k, M_KBDMUX); return (error); /* could not set mode */ } SLIST_INSERT_HEAD(&state->ks_kbds, k, next); KBDMUX_UNLOCK(state); break; case KBRELKBD: /* release keyboard from the mux */ ki = (keyboard_info_t *) arg; if (ki == NULL || ki->kb_unit < 0 || ki->kb_name[0] == '\0' || strcmp(ki->kb_name, "*") == 0) return (EINVAL); /* bad input */ KBDMUX_LOCK(state); SLIST_FOREACH(k, &state->ks_kbds, next) if (k->kbd->kb_unit == ki->kb_unit && strcmp(k->kbd->kb_name, ki->kb_name) == 0) break; if (k != NULL) { error = kbd_release(k->kbd, &k->kbd); if (error == 0) { SLIST_REMOVE(&state->ks_kbds, k, kbdmux_kbd, next); k->kbd = NULL; free(k, M_KBDMUX); } } else error = ENXIO; /* keyboard is not in the mux */ KBDMUX_UNLOCK(state); break; case KDGKBMODE: /* get kyboard mode */ KBDMUX_LOCK(state); *(int *)arg = state->ks_mode; KBDMUX_UNLOCK(state); break; #ifdef COMPAT_FREEBSD6 case _IO('K', 7): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSKBMODE: /* set keyboard mode */ KBDMUX_LOCK(state); 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) { kbdmux_clear_state_locked(state); state->ks_mode = *(int *)arg; } break; default: error = EINVAL; break; } KBDMUX_UNLOCK(state); break; case KDGETLED: /* get keyboard LED */ KBDMUX_LOCK(state); *(int *)arg = KBD_LED_VAL(kbd); KBDMUX_UNLOCK(state); break; #ifdef COMPAT_FREEBSD6 case _IO('K', 66): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSETLED: /* set keyboard LED */ KBDMUX_LOCK(state); /* NOTE: lock key state in ks_state won't be changed */ if (*(int *)arg & ~LOCK_MASK) { KBDMUX_UNLOCK(state); return (EINVAL); } KBD_LED_VAL(kbd) = *(int *)arg; #ifdef EVDEV_SUPPORT if (state->ks_evdev != NULL && evdev_rcpt_mask & EVDEV_RCPT_KBDMUX) evdev_push_leds(state->ks_evdev, *(int *)arg); #endif /* KDSETLED on all slave keyboards */ SLIST_FOREACH(k, &state->ks_kbds, next) (void)kbdd_ioctl(k->kbd, KDSETLED, arg); KBDMUX_UNLOCK(state); break; case KDGKBSTATE: /* get lock key state */ KBDMUX_LOCK(state); *(int *)arg = state->ks_state & LOCK_MASK; KBDMUX_UNLOCK(state); break; #ifdef COMPAT_FREEBSD6 case _IO('K', 20): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSKBSTATE: /* set lock key state */ KBDMUX_LOCK(state); if (*(int *)arg & ~LOCK_MASK) { KBDMUX_UNLOCK(state); return (EINVAL); } state->ks_state &= ~LOCK_MASK; state->ks_state |= *(int *)arg; /* KDSKBSTATE on all slave keyboards */ SLIST_FOREACH(k, &state->ks_kbds, next) (void)kbdd_ioctl(k->kbd, KDSKBSTATE, arg); KBDMUX_UNLOCK(state); return (kbdmux_ioctl(kbd, KDSETLED, arg)); /* NOT REACHED */ #ifdef COMPAT_FREEBSD6 case _IO('K', 67): cmd = KDSETRAD; ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSETREPEAT: /* set keyboard repeat rate (new interface) */ case KDSETRAD: /* set keyboard repeat rate (old interface) */ KBDMUX_LOCK(state); if (cmd == KDSETREPEAT) { int i; /* lookup delay */ for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; i --) if (((int *)arg)[0] >= delays[i]) break; mode = i << 5; /* lookup rate */ for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; i --) if (((int *)arg)[1] >= rates[i]) break; mode |= i; } else mode = *(int *)arg; if (mode & ~0x7f) { KBDMUX_UNLOCK(state); return (EINVAL); } kbd->kb_delay1 = delays[(mode >> 5) & 3]; kbd->kb_delay2 = rates[mode & 0x1f]; #ifdef EVDEV_SUPPORT if (state->ks_evdev != NULL && evdev_rcpt_mask & EVDEV_RCPT_KBDMUX) evdev_push_repeats(state->ks_evdev, kbd); #endif /* perform command on all slave keyboards */ SLIST_FOREACH(k, &state->ks_kbds, next) (void)kbdd_ioctl(k->kbd, cmd, arg); KBDMUX_UNLOCK(state); break; 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 */ KBDMUX_LOCK(state); state->ks_accents = 0; /* perform command on all slave keyboards */ SLIST_FOREACH(k, &state->ks_kbds, next) (void)kbdd_ioctl(k->kbd, cmd, arg); KBDMUX_UNLOCK(state); /* FALLTHROUGH */ default: error = genkbd_commonioctl(kbd, cmd, arg); break; } return (error); } /* * Lock the access to the keyboard */ static int kbdmux_lock(keyboard_t *kbd, int lock) { return (1); /* XXX */ } /* * Clear the internal state of the keyboard */ static void kbdmux_clear_state_locked(kbdmux_state_t *state) { KBDMUX_LOCK_ASSERT(state, MA_OWNED); state->ks_flags &= ~COMPOSE; state->ks_polling = 0; state->ks_state &= LOCK_MASK; /* preserve locking key state */ state->ks_accents = 0; state->ks_composed_char = 0; /* state->ks_prefix = 0; XXX */ state->ks_inq_length = 0; } static void kbdmux_clear_state(keyboard_t *kbd) { kbdmux_state_t *state = (kbdmux_state_t *) kbd->kb_data; KBDMUX_LOCK(state); kbdmux_clear_state_locked(state); KBDMUX_UNLOCK(state); } /* * Save the internal state */ static int kbdmux_get_state(keyboard_t *kbd, void *buf, size_t len) { if (len == 0) return (sizeof(kbdmux_state_t)); if (len < sizeof(kbdmux_state_t)) return (-1); bcopy(kbd->kb_data, buf, sizeof(kbdmux_state_t)); /* XXX locking? */ return (0); } /* * Set the internal state */ static int kbdmux_set_state(keyboard_t *kbd, void *buf, size_t len) { if (len < sizeof(kbdmux_state_t)) return (ENOMEM); bcopy(buf, kbd->kb_data, sizeof(kbdmux_state_t)); /* XXX locking? */ return (0); } /* * Set polling */ static int kbdmux_poll(keyboard_t *kbd, int on) { kbdmux_state_t *state = (kbdmux_state_t *) kbd->kb_data; kbdmux_kbd_t *k; KBDMUX_LOCK(state); if (on) state->ks_polling++; else state->ks_polling--; /* set poll on slave keyboards */ SLIST_FOREACH(k, &state->ks_kbds, next) kbdd_poll(k->kbd, on); KBDMUX_UNLOCK(state); return (0); } #ifdef EVDEV_SUPPORT static void kbdmux_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_KBDMUX && (type == EV_LED || type == EV_REP)) { mtx_lock(&Giant); kbd_ev_event(kbd, type, code, value); mtx_unlock(&Giant); } } #endif /***************************************************************************** ***************************************************************************** ** Module ***************************************************************************** *****************************************************************************/ KEYBOARD_DRIVER(kbdmux, kbdmuxsw, kbdmux_configure); static int kbdmux_modevent(module_t mod, int type, void *data) { keyboard_switch_t *sw; keyboard_t *kbd; int error; switch (type) { case MOD_LOAD: if ((error = kbd_add_driver(&kbdmux_kbd_driver)) != 0) break; if ((sw = kbd_get_switch(KEYBOARD_NAME)) == NULL) { error = ENXIO; break; } kbd = NULL; if ((error = (*sw->probe)(0, NULL, 0)) != 0 || (error = (*sw->init)(0, &kbd, NULL, 0)) != 0) break; #ifdef KBD_INSTALL_CDEV if ((error = kbd_attach(kbd)) != 0) { (*sw->term)(kbd); break; } #endif if ((error = (*sw->enable)(kbd)) != 0) break; break; case MOD_UNLOAD: if ((sw = kbd_get_switch(KEYBOARD_NAME)) == NULL) { error = 0; break; } kbd = kbd_get_keyboard(kbd_find_keyboard(KEYBOARD_NAME, 0)); if (kbd != NULL) { (*sw->disable)(kbd); #ifdef KBD_INSTALL_CDEV kbd_detach(kbd); #endif (*sw->term)(kbd); } kbd_delete_driver(&kbdmux_kbd_driver); error = 0; break; default: error = EOPNOTSUPP; break; } return (error); } DEV_MODULE(kbdmux, kbdmux_modevent, NULL); #ifdef EVDEV_SUPPORT MODULE_DEPEND(kbdmux, evdev, 1, 1, 1); #endif diff --git a/sys/dev/usb/input/ukbd.c b/sys/dev/usb/input/ukbd.c index 4353888505da..c8658f9b62da 100644 --- a/sys/dev/usb/input/ukbd.c +++ b/sys/dev/usb/input/ukbd.c @@ -1,2198 +1,2202 @@ #include __FBSDID("$FreeBSD$"); /*- * SPDX-License-Identifier: BSD-2-Clause-NetBSD * * Copyright (c) 1998 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Lennart Augustsson (lennart@augustsson.net) at * Carlstedt Research & Technology. * * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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. * */ /* * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf */ #include "opt_kbd.h" #include "opt_ukbd.h" #include "opt_evdev.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define USB_DEBUG_VAR ukbd_debug #include #include #ifdef EVDEV_SUPPORT #include #include #endif #include #include #include #include /* the initial key map, accent map and fkey strings */ #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE) #define KBD_DFLT_KEYMAP #include "ukbdmap.h" #endif /* the following file must be included after "ukbdmap.h" */ #include #ifdef USB_DEBUG static int ukbd_debug = 0; static int ukbd_no_leds = 0; static int ukbd_pollrate = 0; static SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, "USB keyboard"); SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RWTUN, &ukbd_debug, 0, "Debug level"); SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, no_leds, CTLFLAG_RWTUN, &ukbd_no_leds, 0, "Disables setting of keyboard leds"); SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, pollrate, CTLFLAG_RWTUN, &ukbd_pollrate, 0, "Force this polling rate, 1-1000Hz"); #endif #define UKBD_EMULATE_ATSCANCODE 1 #define UKBD_DRIVER_NAME "ukbd" #define UKBD_NKEYCODE 256 /* units */ #define UKBD_IN_BUF_SIZE (4 * UKBD_NKEYCODE) /* scancodes */ #define UKBD_IN_BUF_FULL ((UKBD_IN_BUF_SIZE / 2) - 1) /* scancodes */ #define UKBD_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */ #define UKBD_BUFFER_SIZE 64 /* bytes */ #define UKBD_KEY_PRESSED(map, key) ({ \ CTASSERT((key) >= 0 && (key) < UKBD_NKEYCODE); \ ((map)[(key) / 64] & (1ULL << ((key) % 64))); \ }) #define MOD_EJECT 0x01 #define MOD_FN 0x02 struct ukbd_data { uint64_t bitmap[howmany(UKBD_NKEYCODE, 64)]; }; enum { UKBD_INTR_DT_0, UKBD_INTR_DT_1, UKBD_CTRL_LED, UKBD_N_TRANSFER, }; struct ukbd_softc { keyboard_t sc_kbd; keymap_t sc_keymap; accentmap_t sc_accmap; fkeytab_t sc_fkeymap[UKBD_NFKEY]; uint64_t sc_loc_key_valid[howmany(UKBD_NKEYCODE, 64)]; struct hid_location sc_loc_apple_eject; struct hid_location sc_loc_apple_fn; struct hid_location sc_loc_key[UKBD_NKEYCODE]; struct hid_location sc_loc_numlock; struct hid_location sc_loc_capslock; struct hid_location sc_loc_scrolllock; struct usb_callout sc_callout; struct ukbd_data sc_ndata; struct ukbd_data sc_odata; struct thread *sc_poll_thread; struct usb_device *sc_udev; struct usb_interface *sc_iface; struct usb_xfer *sc_xfer[UKBD_N_TRANSFER]; #ifdef EVDEV_SUPPORT struct evdev_dev *sc_evdev; #endif sbintime_t sc_co_basetime; int sc_delay; uint32_t sc_repeat_time; uint32_t sc_input[UKBD_IN_BUF_SIZE]; /* input buffer */ uint32_t sc_time_ms; uint32_t sc_composed_char; /* composed char code, if non-zero */ #ifdef UKBD_EMULATE_ATSCANCODE uint32_t sc_buffered_char[2]; #endif uint32_t sc_flags; /* flags */ #define UKBD_FLAG_COMPOSE 0x00000001 #define UKBD_FLAG_POLLING 0x00000002 #define UKBD_FLAG_SET_LEDS 0x00000004 #define UKBD_FLAG_ATTACHED 0x00000010 #define UKBD_FLAG_GONE 0x00000020 #define UKBD_FLAG_HID_MASK 0x003fffc0 #define UKBD_FLAG_APPLE_EJECT 0x00000040 #define UKBD_FLAG_APPLE_FN 0x00000080 #define UKBD_FLAG_APPLE_SWAP 0x00000100 #define UKBD_FLAG_NUMLOCK 0x00080000 #define UKBD_FLAG_CAPSLOCK 0x00100000 #define UKBD_FLAG_SCROLLLOCK 0x00200000 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */ int sc_state; /* shift/lock key state */ int sc_accents; /* accent key index (> 0) */ int sc_polling; /* polling recursion count */ int sc_led_size; int sc_kbd_size; uint16_t sc_inputs; uint16_t sc_inputhead; uint16_t sc_inputtail; uint8_t sc_leds; /* store for async led requests */ uint8_t sc_iface_index; uint8_t sc_iface_no; uint8_t sc_id_apple_eject; uint8_t sc_id_apple_fn; uint8_t sc_id_loc_key[UKBD_NKEYCODE]; uint8_t sc_id_numlock; uint8_t sc_id_capslock; uint8_t sc_id_scrolllock; uint8_t sc_kbd_id; uint8_t sc_repeat_key; uint8_t sc_buffer[UKBD_BUFFER_SIZE]; }; #define KEY_NONE 0x00 #define KEY_ERROR 0x01 #define KEY_PRESS 0 #define KEY_RELEASE 0x400 #define KEY_INDEX(c) ((c) & 0xFF) #define SCAN_PRESS 0 #define SCAN_RELEASE 0x80 #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 SCAN_CHAR(c) ((c) & 0x7f) #define UKBD_LOCK() USB_MTX_LOCK(&Giant) #define UKBD_UNLOCK() USB_MTX_UNLOCK(&Giant) #define UKBD_LOCK_ASSERT() USB_MTX_ASSERT(&Giant, MA_OWNED) #define NN 0 /* no translation */ /* * Translate USB keycodes to AT keyboard scancodes. */ /* * FIXME: Mac USB keyboard generates: * 0x53: keypad NumLock/Clear * 0x66: Power * 0x67: keypad = * 0x68: F13 * 0x69: F14 * 0x6a: F15 * * USB Apple Keyboard JIS generates: * 0x90: Kana * 0x91: Eisu */ static const uint8_t ukbd_trtab[256] = { 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */ 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */ 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */ 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */ 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */ 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */ 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */ 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */ 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */ 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */ 97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */ 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */ 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */ NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */ NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */ 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */ 121, 120, NN, NN, NN, NN, NN, 123, /* 80 - 87 */ 124, 125, 126, 127, 128, NN, NN, NN, /* 88 - 8F */ 129, 130, NN, NN, NN, NN, NN, NN, /* 90 - 97 */ NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */ NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */ NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */ NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */ NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */ NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */ NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */ NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */ NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */ 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */ NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */ NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */ NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */ }; static const uint8_t ukbd_boot_desc[] = { 0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x05, 0x07, 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x08, 0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x01, 0x95, 0x03, 0x75, 0x01, 0x05, 0x08, 0x19, 0x01, 0x29, 0x03, 0x91, 0x02, 0x95, 0x05, 0x75, 0x01, 0x91, 0x01, 0x95, 0x06, 0x75, 0x08, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05, 0x07, 0x19, 0x00, 0x2a, 0xff, 0x00, 0x81, 0x00, 0xc0 }; /* prototypes */ static void ukbd_timeout(void *); static void ukbd_set_leds(struct ukbd_softc *, uint8_t); static int ukbd_set_typematic(keyboard_t *, int); #ifdef UKBD_EMULATE_ATSCANCODE static uint32_t ukbd_atkeycode(int, const uint64_t *); static int ukbd_key2scan(struct ukbd_softc *, int, const uint64_t *, int); #endif static uint32_t ukbd_read_char(keyboard_t *, int); static void ukbd_clear_state(keyboard_t *); static int ukbd_ioctl(keyboard_t *, u_long, caddr_t); static int ukbd_enable(keyboard_t *); static int ukbd_disable(keyboard_t *); static void ukbd_interrupt(struct ukbd_softc *); static void ukbd_event_keyinput(struct ukbd_softc *); static device_probe_t ukbd_probe; static device_attach_t ukbd_attach; static device_detach_t ukbd_detach; static device_resume_t ukbd_resume; #ifdef EVDEV_SUPPORT static evdev_event_t ukbd_ev_event; static const struct evdev_methods ukbd_evdev_methods = { .ev_event = ukbd_ev_event, }; #endif static bool ukbd_any_key_pressed(struct ukbd_softc *sc) { bool ret = false; unsigned i; for (i = 0; i != howmany(UKBD_NKEYCODE, 64); i++) ret |= (sc->sc_odata.bitmap[i] != 0); return (ret); } static bool ukbd_any_key_valid(struct ukbd_softc *sc) { bool ret = false; unsigned i; for (i = 0; i != howmany(UKBD_NKEYCODE, 64); i++) ret |= (sc->sc_loc_key_valid[i] != 0); return (ret); } static bool ukbd_is_modifier_key(uint32_t key) { return (key >= 0xe0 && key <= 0xe7); } static void ukbd_start_timer(struct ukbd_softc *sc) { sbintime_t delay, now, prec; now = sbinuptime(); /* check if initial delay passed and fallback to key repeat delay */ if (sc->sc_delay == 0) sc->sc_delay = sc->sc_kbd.kb_delay2; /* compute timeout */ delay = SBT_1MS * sc->sc_delay; sc->sc_co_basetime += delay; /* check if we are running behind */ if (sc->sc_co_basetime < now) sc->sc_co_basetime = now; /* This is rarely called, so prefer precision to efficiency. */ prec = qmin(delay >> 7, SBT_1MS * 10); usb_callout_reset_sbt(&sc->sc_callout, sc->sc_co_basetime, prec, ukbd_timeout, sc, C_ABSOLUTE); } static void ukbd_put_key(struct ukbd_softc *sc, uint32_t key) { UKBD_LOCK_ASSERT(); DPRINTF("0x%02x (%d) %s\n", key, key, (key & KEY_RELEASE) ? "released" : "pressed"); #ifdef EVDEV_SUPPORT if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && sc->sc_evdev != NULL) evdev_push_event(sc->sc_evdev, EV_KEY, evdev_hid2key(KEY_INDEX(key)), !(key & KEY_RELEASE)); + if (sc->sc_evdev != NULL && evdev_is_grabbed(sc->sc_evdev)) + return; #endif if (sc->sc_inputs < UKBD_IN_BUF_SIZE) { sc->sc_input[sc->sc_inputtail] = key; ++(sc->sc_inputs); ++(sc->sc_inputtail); if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) { sc->sc_inputtail = 0; } } else { DPRINTF("input buffer is full\n"); } } static void ukbd_do_poll(struct ukbd_softc *sc, uint8_t wait) { UKBD_LOCK_ASSERT(); KASSERT((sc->sc_flags & UKBD_FLAG_POLLING) != 0, ("ukbd_do_poll called when not polling\n")); DPRINTFN(2, "polling\n"); if (USB_IN_POLLING_MODE_FUNC() == 0) { /* * In this context the kernel is polling for input, * but the USB subsystem works in normal interrupt-driven * mode, so we just wait on the USB threads to do the job. * Note that we currently hold the Giant, but it's also used * as the transfer mtx, so we must release it while waiting. */ while (sc->sc_inputs == 0) { /* * Give USB threads a chance to run. Note that * kern_yield performs DROP_GIANT + PICKUP_GIANT. */ kern_yield(PRI_UNCHANGED); if (!wait) break; } return; } while (sc->sc_inputs == 0) { usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER); /* Delay-optimised support for repetition of keys */ if (ukbd_any_key_pressed(sc)) { /* a key is pressed - need timekeeping */ DELAY(1000); /* 1 millisecond has passed */ sc->sc_time_ms += 1; } ukbd_interrupt(sc); if (!wait) break; } } static int32_t ukbd_get_key(struct ukbd_softc *sc, uint8_t wait) { int32_t c; UKBD_LOCK_ASSERT(); KASSERT((USB_IN_POLLING_MODE_FUNC() == 0) || (sc->sc_flags & UKBD_FLAG_POLLING) != 0, ("not polling in kdb or panic\n")); if (sc->sc_inputs == 0 && (sc->sc_flags & UKBD_FLAG_GONE) == 0) { /* start transfer, if not already started */ usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_0]); usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_1]); } if (sc->sc_flags & UKBD_FLAG_POLLING) ukbd_do_poll(sc, wait); if (sc->sc_inputs == 0) { c = -1; } else { c = sc->sc_input[sc->sc_inputhead]; --(sc->sc_inputs); ++(sc->sc_inputhead); if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) { sc->sc_inputhead = 0; } } return (c); } static void ukbd_interrupt(struct ukbd_softc *sc) { const uint32_t now = sc->sc_time_ms; unsigned key; UKBD_LOCK_ASSERT(); /* Check for modifier key changes first */ for (key = 0xe0; key != 0xe8; key++) { const uint64_t mask = 1ULL << (key % 64); const uint64_t delta = sc->sc_odata.bitmap[key / 64] ^ sc->sc_ndata.bitmap[key / 64]; if (delta & mask) { if (sc->sc_odata.bitmap[key / 64] & mask) ukbd_put_key(sc, key | KEY_RELEASE); else ukbd_put_key(sc, key | KEY_PRESS); } } /* Check for key changes */ for (key = 0; key != UKBD_NKEYCODE; key++) { const uint64_t mask = 1ULL << (key % 64); const uint64_t delta = sc->sc_odata.bitmap[key / 64] ^ sc->sc_ndata.bitmap[key / 64]; if (mask == 1 && delta == 0) { key += 63; continue; /* skip empty areas */ } else if (ukbd_is_modifier_key(key)) { continue; } else if (delta & mask) { if (sc->sc_odata.bitmap[key / 64] & mask) { ukbd_put_key(sc, key | KEY_RELEASE); /* clear repeating key, if any */ if (sc->sc_repeat_key == key) sc->sc_repeat_key = 0; } else { ukbd_put_key(sc, key | KEY_PRESS); sc->sc_co_basetime = sbinuptime(); sc->sc_delay = sc->sc_kbd.kb_delay1; ukbd_start_timer(sc); /* set repeat time for last key */ sc->sc_repeat_time = now + sc->sc_kbd.kb_delay1; sc->sc_repeat_key = key; } } } /* synchronize old data with new data */ sc->sc_odata = sc->sc_ndata; /* check if last key is still pressed */ if (sc->sc_repeat_key != 0) { const int32_t dtime = (sc->sc_repeat_time - now); /* check if time has elapsed */ if (dtime <= 0) { ukbd_put_key(sc, sc->sc_repeat_key | KEY_PRESS); sc->sc_repeat_time = now + sc->sc_kbd.kb_delay2; } } #ifdef EVDEV_SUPPORT if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && sc->sc_evdev != NULL) evdev_sync(sc->sc_evdev); + if (sc->sc_evdev != NULL && evdev_is_grabbed(sc->sc_evdev)) + return; #endif /* wakeup keyboard system */ ukbd_event_keyinput(sc); } static void ukbd_event_keyinput(struct ukbd_softc *sc) { int c; UKBD_LOCK_ASSERT(); if ((sc->sc_flags & UKBD_FLAG_POLLING) != 0) return; if (sc->sc_inputs == 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 = ukbd_read_char(&sc->sc_kbd, 0); } while (c != NOKEY); } } static void ukbd_timeout(void *arg) { struct ukbd_softc *sc = arg; UKBD_LOCK_ASSERT(); sc->sc_time_ms += sc->sc_delay; sc->sc_delay = 0; ukbd_interrupt(sc); /* Make sure any leftover key events gets read out */ ukbd_event_keyinput(sc); if (ukbd_any_key_pressed(sc) || (sc->sc_inputs != 0)) { ukbd_start_timer(sc); } } static uint32_t ukbd_apple_fn(uint32_t keycode) { switch (keycode) { case 0x28: return 0x49; /* RETURN -> INSERT */ case 0x2a: return 0x4c; /* BACKSPACE -> DEL */ case 0x50: return 0x4a; /* LEFT ARROW -> HOME */ case 0x4f: return 0x4d; /* RIGHT ARROW -> END */ case 0x52: return 0x4b; /* UP ARROW -> PGUP */ case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */ default: return keycode; } } static uint32_t ukbd_apple_swap(uint32_t keycode) { switch (keycode) { case 0x35: return 0x64; case 0x64: return 0x35; default: return keycode; } } static void ukbd_intr_callback(struct usb_xfer *xfer, usb_error_t error) { struct ukbd_softc *sc = usbd_xfer_softc(xfer); struct usb_page_cache *pc; uint32_t i; uint8_t id; uint8_t modifiers; int offset; int len; UKBD_LOCK_ASSERT(); usbd_xfer_status(xfer, &len, NULL, NULL, NULL); pc = usbd_xfer_get_frame(xfer, 0); switch (USB_GET_STATE(xfer)) { case USB_ST_TRANSFERRED: DPRINTF("actlen=%d bytes\n", len); if (len == 0) { DPRINTF("zero length data\n"); goto tr_setup; } if (sc->sc_kbd_id != 0) { /* check and remove HID ID byte */ usbd_copy_out(pc, 0, &id, 1); offset = 1; len--; if (len == 0) { DPRINTF("zero length data\n"); goto tr_setup; } } else { offset = 0; id = 0; } if (len > UKBD_BUFFER_SIZE) len = UKBD_BUFFER_SIZE; /* get data */ usbd_copy_out(pc, offset, sc->sc_buffer, len); /* clear temporary storage */ memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata)); /* clear modifiers */ modifiers = 0; /* scan through HID data */ if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) && (id == sc->sc_id_apple_eject)) { if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_eject)) modifiers |= MOD_EJECT; } if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) && (id == sc->sc_id_apple_fn)) { if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_fn)) modifiers |= MOD_FN; } for (i = 0; i != UKBD_NKEYCODE; i++) { const uint64_t valid = sc->sc_loc_key_valid[i / 64]; const uint64_t mask = 1ULL << (i % 64); if (mask == 1 && valid == 0) { i += 63; continue; /* skip empty areas */ } else if (~valid & mask) { continue; /* location is not valid */ } else if (id != sc->sc_id_loc_key[i]) { continue; /* invalid HID ID */ } else if (i == 0) { struct hid_location tmp_loc = sc->sc_loc_key[0]; /* range check array size */ if (tmp_loc.count > UKBD_NKEYCODE) tmp_loc.count = UKBD_NKEYCODE; while (tmp_loc.count--) { uint32_t key = hid_get_udata(sc->sc_buffer, len, &tmp_loc); /* advance to next location */ tmp_loc.pos += tmp_loc.size; if (key == KEY_ERROR) { DPRINTF("KEY_ERROR\n"); sc->sc_ndata = sc->sc_odata; goto tr_setup; /* ignore */ } if (modifiers & MOD_FN) key = ukbd_apple_fn(key); if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) key = ukbd_apple_swap(key); if (key == KEY_NONE || key >= UKBD_NKEYCODE) continue; /* set key in bitmap */ sc->sc_ndata.bitmap[key / 64] |= 1ULL << (key % 64); } } else if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_key[i])) { uint32_t key = i; if (modifiers & MOD_FN) key = ukbd_apple_fn(key); if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) key = ukbd_apple_swap(key); if (key == KEY_NONE || key == KEY_ERROR || key >= UKBD_NKEYCODE) continue; /* set key in bitmap */ sc->sc_ndata.bitmap[key / 64] |= 1ULL << (key % 64); } } #ifdef USB_DEBUG DPRINTF("modifiers = 0x%04x\n", modifiers); for (i = 0; i != UKBD_NKEYCODE; i++) { const uint64_t valid = sc->sc_ndata.bitmap[i / 64]; const uint64_t mask = 1ULL << (i % 64); if (valid & mask) DPRINTF("Key 0x%02x pressed\n", i); } #endif ukbd_interrupt(sc); case USB_ST_SETUP: tr_setup: if (sc->sc_inputs < UKBD_IN_BUF_FULL) { usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); usbd_transfer_submit(xfer); } else { DPRINTF("input queue is full!\n"); } break; default: /* Error */ DPRINTF("error=%s\n", usbd_errstr(error)); if (error != USB_ERR_CANCELLED) { /* try to clear stall first */ usbd_xfer_set_stall(xfer); goto tr_setup; } break; } } static void ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error) { struct ukbd_softc *sc = usbd_xfer_softc(xfer); struct usb_device_request req; struct usb_page_cache *pc; uint8_t id; uint8_t any; int len; UKBD_LOCK_ASSERT(); #ifdef USB_DEBUG if (ukbd_no_leds) return; #endif switch (USB_GET_STATE(xfer)) { case USB_ST_TRANSFERRED: case USB_ST_SETUP: if (!(sc->sc_flags & UKBD_FLAG_SET_LEDS)) break; sc->sc_flags &= ~UKBD_FLAG_SET_LEDS; req.bmRequestType = UT_WRITE_CLASS_INTERFACE; req.bRequest = UR_SET_REPORT; USETW2(req.wValue, UHID_OUTPUT_REPORT, 0); req.wIndex[0] = sc->sc_iface_no; req.wIndex[1] = 0; req.wLength[1] = 0; memset(sc->sc_buffer, 0, UKBD_BUFFER_SIZE); id = 0; any = 0; /* Assumption: All led bits must be in the same ID. */ if (sc->sc_flags & UKBD_FLAG_NUMLOCK) { if (sc->sc_leds & NLKED) { hid_put_udata(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1, &sc->sc_loc_numlock, 1); } id = sc->sc_id_numlock; any = 1; } if (sc->sc_flags & UKBD_FLAG_SCROLLLOCK) { if (sc->sc_leds & SLKED) { hid_put_udata(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1, &sc->sc_loc_scrolllock, 1); } id = sc->sc_id_scrolllock; any = 1; } if (sc->sc_flags & UKBD_FLAG_CAPSLOCK) { if (sc->sc_leds & CLKED) { hid_put_udata(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1, &sc->sc_loc_capslock, 1); } id = sc->sc_id_capslock; any = 1; } /* if no leds, nothing to do */ if (!any) break; /* range check output report length */ len = sc->sc_led_size; if (len > (UKBD_BUFFER_SIZE - 1)) len = (UKBD_BUFFER_SIZE - 1); /* check if we need to prefix an ID byte */ sc->sc_buffer[0] = id; pc = usbd_xfer_get_frame(xfer, 1); if (id != 0) { len++; usbd_copy_in(pc, 0, sc->sc_buffer, len); } else { usbd_copy_in(pc, 0, sc->sc_buffer + 1, len); } req.wLength[0] = len; usbd_xfer_set_frame_len(xfer, 1, len); DPRINTF("len=%d, id=%d\n", len, id); /* setup control request last */ pc = usbd_xfer_get_frame(xfer, 0); usbd_copy_in(pc, 0, &req, sizeof(req)); usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); /* start data transfer */ usbd_xfer_set_frames(xfer, 2); usbd_transfer_submit(xfer); break; default: /* Error */ DPRINTFN(1, "error=%s\n", usbd_errstr(error)); break; } } static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = { [UKBD_INTR_DT_0] = { .type = UE_INTERRUPT, .endpoint = UE_ADDR_ANY, .direction = UE_DIR_IN, .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, .bufsize = 0, /* use wMaxPacketSize */ .callback = &ukbd_intr_callback, }, [UKBD_INTR_DT_1] = { .type = UE_INTERRUPT, .endpoint = UE_ADDR_ANY, .direction = UE_DIR_IN, .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, .bufsize = 0, /* use wMaxPacketSize */ .callback = &ukbd_intr_callback, }, [UKBD_CTRL_LED] = { .type = UE_CONTROL, .endpoint = 0x00, /* Control pipe */ .direction = UE_DIR_ANY, .bufsize = sizeof(struct usb_device_request) + UKBD_BUFFER_SIZE, .callback = &ukbd_set_leds_callback, .timeout = 1000, /* 1 second */ }, }; /* A match on these entries will load ukbd */ static const STRUCT_USB_HOST_ID __used ukbd_devs[] = { {USB_IFACE_CLASS(UICLASS_HID), USB_IFACE_SUBCLASS(UISUBCLASS_BOOT), USB_IFACE_PROTOCOL(UIPROTO_BOOT_KEYBOARD),}, }; static int ukbd_probe(device_t dev) { keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME); struct usb_attach_arg *uaa = device_get_ivars(dev); void *d_ptr; int error; uint16_t d_len; UKBD_LOCK_ASSERT(); DPRINTFN(11, "\n"); if (sw == NULL) { return (ENXIO); } if (uaa->usb_mode != USB_MODE_HOST) { return (ENXIO); } if (uaa->info.bInterfaceClass != UICLASS_HID) return (ENXIO); if (usb_test_quirk(uaa, UQ_KBD_IGNORE)) return (ENXIO); if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) && (uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD)) return (BUS_PROBE_DEFAULT); error = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex); if (error) return (ENXIO); if (hid_is_keyboard(d_ptr, d_len)) { if (hid_is_mouse(d_ptr, d_len)) { /* * NOTE: We currently don't support USB mouse * and USB keyboard on the same USB endpoint. * Let "ums" driver win. */ error = ENXIO; } else { error = BUS_PROBE_DEFAULT; } } else { error = ENXIO; } free(d_ptr, M_TEMP); return (error); } static void ukbd_parse_hid(struct ukbd_softc *sc, const uint8_t *ptr, uint32_t len) { uint32_t flags; uint32_t key; /* reset detected bits */ sc->sc_flags &= ~UKBD_FLAG_HID_MASK; /* reset detected keys */ memset(sc->sc_loc_key_valid, 0, sizeof(sc->sc_loc_key_valid)); /* check if there is an ID byte */ sc->sc_kbd_size = hid_report_size_max(ptr, len, hid_input, &sc->sc_kbd_id); /* investigate if this is an Apple Keyboard */ if (hid_locate(ptr, len, HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT), hid_input, 0, &sc->sc_loc_apple_eject, &flags, &sc->sc_id_apple_eject)) { if (flags & HIO_VARIABLE) sc->sc_flags |= UKBD_FLAG_APPLE_EJECT | UKBD_FLAG_APPLE_SWAP; DPRINTFN(1, "Found Apple eject-key\n"); } if (hid_locate(ptr, len, HID_USAGE2(0xFFFF, 0x0003), hid_input, 0, &sc->sc_loc_apple_fn, &flags, &sc->sc_id_apple_fn)) { if (flags & HIO_VARIABLE) sc->sc_flags |= UKBD_FLAG_APPLE_FN; DPRINTFN(1, "Found Apple FN-key\n"); } /* figure out event buffer */ if (hid_locate(ptr, len, HID_USAGE2(HUP_KEYBOARD, 0x00), hid_input, 0, &sc->sc_loc_key[0], &flags, &sc->sc_id_loc_key[0])) { if (flags & HIO_VARIABLE) { DPRINTFN(1, "Ignoring keyboard event control\n"); } else { sc->sc_loc_key_valid[0] |= 1; DPRINTFN(1, "Found keyboard event array\n"); } } /* figure out the keys */ for (key = 1; key != UKBD_NKEYCODE; key++) { if (hid_locate(ptr, len, HID_USAGE2(HUP_KEYBOARD, key), hid_input, 0, &sc->sc_loc_key[key], &flags, &sc->sc_id_loc_key[key])) { if (flags & HIO_VARIABLE) { sc->sc_loc_key_valid[key / 64] |= 1ULL << (key % 64); DPRINTFN(1, "Found key 0x%02x\n", key); } } } /* figure out leds on keyboard */ sc->sc_led_size = hid_report_size_max(ptr, len, hid_output, NULL); if (hid_locate(ptr, len, HID_USAGE2(HUP_LEDS, 0x01), hid_output, 0, &sc->sc_loc_numlock, &flags, &sc->sc_id_numlock)) { if (flags & HIO_VARIABLE) sc->sc_flags |= UKBD_FLAG_NUMLOCK; DPRINTFN(1, "Found keyboard numlock\n"); } if (hid_locate(ptr, len, HID_USAGE2(HUP_LEDS, 0x02), hid_output, 0, &sc->sc_loc_capslock, &flags, &sc->sc_id_capslock)) { if (flags & HIO_VARIABLE) sc->sc_flags |= UKBD_FLAG_CAPSLOCK; DPRINTFN(1, "Found keyboard capslock\n"); } if (hid_locate(ptr, len, HID_USAGE2(HUP_LEDS, 0x03), hid_output, 0, &sc->sc_loc_scrolllock, &flags, &sc->sc_id_scrolllock)) { if (flags & HIO_VARIABLE) sc->sc_flags |= UKBD_FLAG_SCROLLLOCK; DPRINTFN(1, "Found keyboard scrolllock\n"); } } static int ukbd_attach(device_t dev) { struct ukbd_softc *sc = device_get_softc(dev); struct usb_attach_arg *uaa = device_get_ivars(dev); int unit = device_get_unit(dev); keyboard_t *kbd = &sc->sc_kbd; void *hid_ptr = NULL; usb_error_t err; uint16_t n; uint16_t hid_len; #ifdef EVDEV_SUPPORT struct evdev_dev *evdev; int i; #endif #ifdef USB_DEBUG int rate; #endif UKBD_LOCK_ASSERT(); kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0); kbd->kb_data = (void *)sc; device_set_usb_desc(dev); sc->sc_udev = uaa->device; sc->sc_iface = uaa->iface; sc->sc_iface_index = uaa->info.bIfaceIndex; sc->sc_iface_no = uaa->info.bIfaceNum; sc->sc_mode = K_XLATE; usb_callout_init_mtx(&sc->sc_callout, &Giant, 0); #ifdef UKBD_NO_POLLING err = usbd_transfer_setup(uaa->device, &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config, UKBD_N_TRANSFER, sc, &Giant); #else /* * Setup the UKBD USB transfers one by one, so they are memory * independent which allows for handling panics triggered by * the keyboard driver itself, typically via CTRL+ALT+ESC * sequences. Or if the USB keyboard driver was processing a * key at the moment of panic. */ for (n = 0; n != UKBD_N_TRANSFER; n++) { err = usbd_transfer_setup(uaa->device, &uaa->info.bIfaceIndex, sc->sc_xfer + n, ukbd_config + n, 1, sc, &Giant); if (err) break; } #endif if (err) { DPRINTF("error=%s\n", usbd_errstr(err)); goto detach; } /* setup default keyboard maps */ sc->sc_keymap = key_map; sc->sc_accmap = accent_map; for (n = 0; n < UKBD_NFKEY; n++) { sc->sc_fkeymap[n] = fkey_tab[n]; } kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap, sc->sc_fkeymap, UKBD_NFKEY); KBD_FOUND_DEVICE(kbd); ukbd_clear_state(kbd); /* * FIXME: set the initial value for lock keys in "sc_state" * according to the BIOS data? */ KBD_PROBE_DONE(kbd); /* get HID descriptor */ err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr, &hid_len, M_TEMP, uaa->info.bIfaceIndex); if (err == 0) { DPRINTF("Parsing HID descriptor of %d bytes\n", (int)hid_len); ukbd_parse_hid(sc, hid_ptr, hid_len); free(hid_ptr, M_TEMP); } /* check if we should use the boot protocol */ if (usb_test_quirk(uaa, UQ_KBD_BOOTPROTO) || (err != 0) || ukbd_any_key_valid(sc) == false) { DPRINTF("Forcing boot protocol\n"); err = usbd_req_set_protocol(sc->sc_udev, NULL, sc->sc_iface_index, 0); if (err != 0) { DPRINTF("Set protocol error=%s (ignored)\n", usbd_errstr(err)); } ukbd_parse_hid(sc, ukbd_boot_desc, sizeof(ukbd_boot_desc)); } /* ignore if SETIDLE fails, hence it is not crucial */ usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0); ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state); KBD_INIT_DONE(kbd); if (kbd_register(kbd) < 0) { goto detach; } KBD_CONFIG_DONE(kbd); ukbd_enable(kbd); #ifdef KBD_INSTALL_CDEV if (kbd_attach(kbd)) { goto detach; } #endif #ifdef EVDEV_SUPPORT evdev = evdev_alloc(); evdev_set_name(evdev, device_get_desc(dev)); evdev_set_phys(evdev, device_get_nameunit(dev)); evdev_set_id(evdev, BUS_USB, uaa->info.idVendor, uaa->info.idProduct, 0); evdev_set_serial(evdev, usb_get_serial(uaa->device)); evdev_set_methods(evdev, kbd, &ukbd_evdev_methods); evdev_support_event(evdev, EV_SYN); evdev_support_event(evdev, EV_KEY); if (sc->sc_flags & (UKBD_FLAG_NUMLOCK | UKBD_FLAG_CAPSLOCK | UKBD_FLAG_SCROLLLOCK)) evdev_support_event(evdev, EV_LED); evdev_support_event(evdev, EV_REP); for (i = 0x00; i <= 0xFF; i++) evdev_support_key(evdev, evdev_hid2key(i)); if (sc->sc_flags & UKBD_FLAG_NUMLOCK) evdev_support_led(evdev, LED_NUML); if (sc->sc_flags & UKBD_FLAG_CAPSLOCK) evdev_support_led(evdev, LED_CAPSL); if (sc->sc_flags & UKBD_FLAG_SCROLLLOCK) evdev_support_led(evdev, LED_SCROLLL); if (evdev_register_mtx(evdev, &Giant)) evdev_free(evdev); else sc->sc_evdev = evdev; #endif sc->sc_flags |= UKBD_FLAG_ATTACHED; if (bootverbose) { kbdd_diag(kbd, bootverbose); } #ifdef USB_DEBUG /* check for polling rate override */ rate = ukbd_pollrate; if (rate > 0) { if (rate > 1000) rate = 1; else rate = 1000 / rate; /* set new polling interval in ms */ usbd_xfer_set_interval(sc->sc_xfer[UKBD_INTR_DT_0], rate); usbd_xfer_set_interval(sc->sc_xfer[UKBD_INTR_DT_1], rate); } #endif /* start the keyboard */ usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_0]); usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_1]); return (0); /* success */ detach: ukbd_detach(dev); return (ENXIO); /* error */ } static int ukbd_detach(device_t dev) { struct ukbd_softc *sc = device_get_softc(dev); int error; UKBD_LOCK_ASSERT(); DPRINTF("\n"); sc->sc_flags |= UKBD_FLAG_GONE; usb_callout_stop(&sc->sc_callout); /* kill any stuck keys */ if (sc->sc_flags & UKBD_FLAG_ATTACHED) { /* stop receiving events from the USB keyboard */ usbd_transfer_stop(sc->sc_xfer[UKBD_INTR_DT_0]); usbd_transfer_stop(sc->sc_xfer[UKBD_INTR_DT_1]); /* release all leftover keys, if any */ memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata)); /* process releasing of all keys */ ukbd_interrupt(sc); } ukbd_disable(&sc->sc_kbd); #ifdef KBD_INSTALL_CDEV if (sc->sc_flags & UKBD_FLAG_ATTACHED) { error = kbd_detach(&sc->sc_kbd); if (error) { /* usb attach cannot return an error */ device_printf(dev, "WARNING: kbd_detach() " "returned non-zero! (ignored)\n"); } } #endif #ifdef EVDEV_SUPPORT evdev_free(sc->sc_evdev); #endif if (KBD_IS_CONFIGURED(&sc->sc_kbd)) { error = kbd_unregister(&sc->sc_kbd); if (error) { /* usb attach cannot return an error */ device_printf(dev, "WARNING: kbd_unregister() " "returned non-zero! (ignored)\n"); } } sc->sc_kbd.kb_flags = 0; usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER); usb_callout_drain(&sc->sc_callout); DPRINTF("%s: disconnected\n", device_get_nameunit(dev)); return (0); } static int ukbd_resume(device_t dev) { struct ukbd_softc *sc = device_get_softc(dev); UKBD_LOCK_ASSERT(); ukbd_clear_state(&sc->sc_kbd); return (0); } #ifdef EVDEV_SUPPORT static void ukbd_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 /* early keyboard probe, not supported */ static int ukbd_configure(int flags) { return (0); } /* detect a keyboard, not used */ static int ukbd__probe(int unit, void *arg, int flags) { return (ENXIO); } /* reset and initialize the device, not used */ static int ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) { return (ENXIO); } /* test the interface to the device, not used */ static int ukbd_test_if(keyboard_t *kbd) { return (0); } /* finish using this keyboard, not used */ static int ukbd_term(keyboard_t *kbd) { return (ENXIO); } /* keyboard interrupt routine, not used */ static int ukbd_intr(keyboard_t *kbd, void *arg) { return (0); } /* lock the access to the keyboard, not used */ static int ukbd_lock(keyboard_t *kbd, int lock) { return (1); } /* * Enable the access to the device; until this function is called, * the client cannot read from the keyboard. */ static int ukbd_enable(keyboard_t *kbd) { UKBD_LOCK(); KBD_ACTIVATE(kbd); UKBD_UNLOCK(); return (0); } /* disallow the access to the device */ static int ukbd_disable(keyboard_t *kbd) { UKBD_LOCK(); KBD_DEACTIVATE(kbd); UKBD_UNLOCK(); return (0); } /* check if data is waiting */ /* Currently unused. */ static int ukbd_check(keyboard_t *kbd) { struct ukbd_softc *sc = kbd->kb_data; UKBD_LOCK_ASSERT(); if (!KBD_IS_ACTIVE(kbd)) return (0); if (sc->sc_flags & UKBD_FLAG_POLLING) ukbd_do_poll(sc, 0); #ifdef UKBD_EMULATE_ATSCANCODE if (sc->sc_buffered_char[0]) { return (1); } #endif if (sc->sc_inputs > 0) { return (1); } return (0); } /* check if char is waiting */ static int ukbd_check_char_locked(keyboard_t *kbd) { struct ukbd_softc *sc = kbd->kb_data; UKBD_LOCK_ASSERT(); if (!KBD_IS_ACTIVE(kbd)) return (0); if ((sc->sc_composed_char > 0) && (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) { return (1); } return (ukbd_check(kbd)); } static int ukbd_check_char(keyboard_t *kbd) { int result; UKBD_LOCK(); result = ukbd_check_char_locked(kbd); UKBD_UNLOCK(); return (result); } /* read one byte from the keyboard if it's allowed */ /* Currently unused. */ static int ukbd_read(keyboard_t *kbd, int wait) { struct ukbd_softc *sc = kbd->kb_data; int32_t usbcode; #ifdef UKBD_EMULATE_ATSCANCODE uint32_t keycode; uint32_t scancode; #endif UKBD_LOCK_ASSERT(); if (!KBD_IS_ACTIVE(kbd)) return (-1); #ifdef UKBD_EMULATE_ATSCANCODE if (sc->sc_buffered_char[0]) { scancode = sc->sc_buffered_char[0]; if (scancode & SCAN_PREFIX) { sc->sc_buffered_char[0] &= ~SCAN_PREFIX; return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); } sc->sc_buffered_char[0] = sc->sc_buffered_char[1]; sc->sc_buffered_char[1] = 0; return (scancode); } #endif /* UKBD_EMULATE_ATSCANCODE */ /* XXX */ usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1); if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1)) return (-1); ++(kbd->kb_count); #ifdef UKBD_EMULATE_ATSCANCODE keycode = ukbd_atkeycode(usbcode, sc->sc_ndata.bitmap); if (keycode == NN) { return -1; } return (ukbd_key2scan(sc, keycode, sc->sc_ndata.bitmap, (usbcode & KEY_RELEASE))); #else /* !UKBD_EMULATE_ATSCANCODE */ return (usbcode); #endif /* UKBD_EMULATE_ATSCANCODE */ } /* read char from the keyboard */ static uint32_t ukbd_read_char_locked(keyboard_t *kbd, int wait) { struct ukbd_softc *sc = kbd->kb_data; uint32_t action; uint32_t keycode; int32_t usbcode; #ifdef UKBD_EMULATE_ATSCANCODE uint32_t scancode; #endif UKBD_LOCK_ASSERT(); if (!KBD_IS_ACTIVE(kbd)) return (NOKEY); next_code: /* do we have a composed char to return ? */ if ((sc->sc_composed_char > 0) && (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) { action = sc->sc_composed_char; sc->sc_composed_char = 0; if (action > 0xFF) { goto errkey; } goto done; } #ifdef UKBD_EMULATE_ATSCANCODE /* do we have a pending raw scan code? */ if (sc->sc_mode == K_RAW) { scancode = sc->sc_buffered_char[0]; if (scancode) { if (scancode & SCAN_PREFIX) { sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX); return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); } sc->sc_buffered_char[0] = sc->sc_buffered_char[1]; sc->sc_buffered_char[1] = 0; return (scancode); } } #endif /* UKBD_EMULATE_ATSCANCODE */ /* see if there is something in the keyboard port */ /* XXX */ usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1); if (usbcode == -1) { return (NOKEY); } ++kbd->kb_count; #ifdef UKBD_EMULATE_ATSCANCODE /* USB key index -> key code -> AT scan code */ keycode = ukbd_atkeycode(usbcode, sc->sc_ndata.bitmap); if (keycode == NN) { return (NOKEY); } /* return an AT scan code for the K_RAW mode */ if (sc->sc_mode == K_RAW) { return (ukbd_key2scan(sc, keycode, sc->sc_ndata.bitmap, (usbcode & KEY_RELEASE))); } #else /* !UKBD_EMULATE_ATSCANCODE */ /* return the byte as is for the K_RAW mode */ if (sc->sc_mode == K_RAW) { return (usbcode); } /* USB key index -> key code */ keycode = ukbd_trtab[KEY_INDEX(usbcode)]; if (keycode == NN) { return (NOKEY); } #endif /* UKBD_EMULATE_ATSCANCODE */ switch (keycode) { case 0x38: /* left alt (compose key) */ if (usbcode & KEY_RELEASE) { if (sc->sc_flags & UKBD_FLAG_COMPOSE) { sc->sc_flags &= ~UKBD_FLAG_COMPOSE; if (sc->sc_composed_char > 0xFF) { sc->sc_composed_char = 0; } } } else { if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) { sc->sc_flags |= UKBD_FLAG_COMPOSE; sc->sc_composed_char = 0; } } break; } /* return the key code in the K_CODE mode */ if (usbcode & KEY_RELEASE) { keycode |= SCAN_RELEASE; } if (sc->sc_mode == K_CODE) { return (keycode); } /* compose a character code */ if (sc->sc_flags & UKBD_FLAG_COMPOSE) { switch (keycode) { /* key pressed, process it */ case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */ sc->sc_composed_char *= 10; sc->sc_composed_char += keycode - 0x40; goto check_composed; case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */ sc->sc_composed_char *= 10; sc->sc_composed_char += keycode - 0x47; goto check_composed; case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */ sc->sc_composed_char *= 10; sc->sc_composed_char += keycode - 0x4E; goto check_composed; case 0x52: /* keypad 0 */ sc->sc_composed_char *= 10; goto check_composed; /* key released, no interest here */ case SCAN_RELEASE | 0x47: case SCAN_RELEASE | 0x48: case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */ case SCAN_RELEASE | 0x4B: case SCAN_RELEASE | 0x4C: case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */ case SCAN_RELEASE | 0x4F: case SCAN_RELEASE | 0x50: case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */ case SCAN_RELEASE | 0x52: /* keypad 0 */ goto next_code; case 0x38: /* left alt key */ break; default: if (sc->sc_composed_char > 0) { sc->sc_flags &= ~UKBD_FLAG_COMPOSE; sc->sc_composed_char = 0; goto errkey; } break; } } /* keycode to key action */ action = genkbd_keyaction(kbd, SCAN_CHAR(keycode), (keycode & SCAN_RELEASE), &sc->sc_state, &sc->sc_accents); if (action == NOKEY) { goto next_code; } done: return (action); check_composed: if (sc->sc_composed_char <= 0xFF) { goto next_code; } errkey: return (ERRKEY); } /* Currently wait is always false. */ static uint32_t ukbd_read_char(keyboard_t *kbd, int wait) { uint32_t keycode; UKBD_LOCK(); keycode = ukbd_read_char_locked(kbd, wait); UKBD_UNLOCK(); return (keycode); } /* some useful control functions */ static int ukbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg) { struct ukbd_softc *sc = kbd->kb_data; int i; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) int ival; #endif UKBD_LOCK_ASSERT(); switch (cmd) { case KDGKBMODE: /* get keyboard mode */ *(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 */ 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) { if ((sc->sc_flags & UKBD_FLAG_POLLING) == 0) ukbd_clear_state(kbd); sc->sc_mode = *(int *)arg; } break; default: 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 "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)) ukbd_set_leds(sc, i); KBD_LED_VAL(kbd) = *(int *)arg; 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; /* set LEDs and quit */ return (ukbd_ioctl(kbd, KDSETLED, arg)); case KDSETREPEAT: /* set keyboard repeat rate (new * interface) */ if (!KBD_HAS_DEVICE(kbd)) { return (0); } /* * Convert negative, zero and tiny args to the same limits * as atkbd. We could support delays of 1 msec, but * anything much shorter than the shortest atkbd value * of 250.34 is almost unusable as well as incompatible. */ kbd->kb_delay1 = imax(((int *)arg)[0], 250); kbd->kb_delay2 = imax(((int *)arg)[1], 34); #ifdef EVDEV_SUPPORT if (sc->sc_evdev != NULL) evdev_push_repeats(sc->sc_evdev, kbd); #endif return (0); #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) */ return (ukbd_set_typematic(kbd, *(int *)arg)); 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 */ sc->sc_accents = 0; /* FALLTHROUGH */ default: return (genkbd_commonioctl(kbd, cmd, arg)); } return (0); } static int ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) { int result; /* * XXX Check if someone is calling us from a critical section: */ if (curthread->td_critnest != 0) return (EDEADLK); /* * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any * context where printf(9) can be called, which among other things * includes interrupt filters and threads with any kinds of locks * already held. For this reason it would be dangerous to acquire * the Giant here unconditionally. On the other hand we have to * have it to handle the ioctl. * So we make our best effort to auto-detect whether we can grab * the Giant or not. Blame syscons(4) for this. */ switch (cmd) { case KDGKBSTATE: case KDSKBSTATE: case KDSETLED: if (!mtx_owned(&Giant) && !USB_IN_POLLING_MODE_FUNC()) return (EDEADLK); /* best I could come up with */ /* FALLTHROUGH */ default: UKBD_LOCK(); result = ukbd_ioctl_locked(kbd, cmd, arg); UKBD_UNLOCK(); return (result); } } /* clear the internal state of the keyboard */ static void ukbd_clear_state(keyboard_t *kbd) { struct ukbd_softc *sc = kbd->kb_data; UKBD_LOCK_ASSERT(); sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING); sc->sc_state &= LOCK_MASK; /* preserve locking key state */ sc->sc_accents = 0; sc->sc_composed_char = 0; #ifdef UKBD_EMULATE_ATSCANCODE sc->sc_buffered_char[0] = 0; sc->sc_buffered_char[1] = 0; #endif memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata)); memset(&sc->sc_odata, 0, sizeof(sc->sc_odata)); sc->sc_repeat_time = 0; sc->sc_repeat_key = 0; } /* save the internal state, not used */ static int ukbd_get_state(keyboard_t *kbd, void *buf, size_t len) { return (len == 0) ? 1 : -1; } /* set the internal state, not used */ static int ukbd_set_state(keyboard_t *kbd, void *buf, size_t len) { return (EINVAL); } static int ukbd_poll(keyboard_t *kbd, int on) { struct ukbd_softc *sc = kbd->kb_data; UKBD_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 |= UKBD_FLAG_POLLING; sc->sc_poll_thread = curthread; } else { sc->sc_flags &= ~UKBD_FLAG_POLLING; sc->sc_delay = 0; } UKBD_UNLOCK(); return (0); } /* local functions */ static void ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds) { UKBD_LOCK_ASSERT(); DPRINTF("leds=0x%02x\n", leds); #ifdef EVDEV_SUPPORT if (sc->sc_evdev != NULL) evdev_push_leds(sc->sc_evdev, leds); #endif sc->sc_leds = leds; sc->sc_flags |= UKBD_FLAG_SET_LEDS; /* start transfer, if not already started */ usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]); } static int ukbd_set_typematic(keyboard_t *kbd, int code) { #ifdef EVDEV_SUPPORT struct ukbd_softc *sc = kbd->kb_data; #endif static const int delays[] = {250, 500, 750, 1000}; static const 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]; #ifdef EVDEV_SUPPORT if (sc->sc_evdev != NULL) evdev_push_repeats(sc->sc_evdev, kbd); #endif return (0); } #ifdef UKBD_EMULATE_ATSCANCODE static uint32_t ukbd_atkeycode(int usbcode, const uint64_t *bitmap) { uint32_t keycode; keycode = ukbd_trtab[KEY_INDEX(usbcode)]; /* * Translate Alt-PrintScreen to SysRq. * * Some or all AT keyboards connected through USB have already * mapped Alted PrintScreens to an unusual usbcode (0x8a). * ukbd_trtab translates this to 0x7e, and key2scan() would * translate that to 0x79 (Intl' 4). Assume that if we have * an Alted 0x7e here then it actually is an Alted PrintScreen. * * The usual usbcode for all PrintScreens is 0x46. ukbd_trtab * translates this to 0x5c, so the Alt check to classify 0x5c * is routine. */ if ((keycode == 0x5c || keycode == 0x7e) && (UKBD_KEY_PRESSED(bitmap, 0xe2 /* ALT-L */) || UKBD_KEY_PRESSED(bitmap, 0xe6 /* ALT-R */))) return (0x54); return (keycode); } static int ukbd_key2scan(struct ukbd_softc *sc, int code, const uint64_t *bitmap, int up) { static const int scan[] = { /* 89 */ 0x11c, /* Enter */ /* 90-99 */ 0x11d, /* Ctrl-R */ 0x135, /* Divide */ 0x137, /* PrintScreen */ 0x138, /* Alt-R */ 0x147, /* Home */ 0x148, /* Up */ 0x149, /* PageUp */ 0x14b, /* Left */ 0x14d, /* Right */ 0x14f, /* End */ /* 100-109 */ 0x150, /* Down */ 0x151, /* PageDown */ 0x152, /* Insert */ 0x153, /* Delete */ 0x146, /* Pause/Break */ 0x15b, /* Win_L(Super_L) */ 0x15c, /* Win_R(Super_R) */ 0x15d, /* Application(Menu) */ /* SUN TYPE 6 USB KEYBOARD */ 0x168, /* Sun Type 6 Help */ 0x15e, /* Sun Type 6 Stop */ /* 110 - 119 */ 0x15f, /* Sun Type 6 Again */ 0x160, /* Sun Type 6 Props */ 0x161, /* Sun Type 6 Undo */ 0x162, /* Sun Type 6 Front */ 0x163, /* Sun Type 6 Copy */ 0x164, /* Sun Type 6 Open */ 0x165, /* Sun Type 6 Paste */ 0x166, /* Sun Type 6 Find */ 0x167, /* Sun Type 6 Cut */ 0x125, /* Sun Type 6 Mute */ /* 120 - 130 */ 0x11f, /* Sun Type 6 VolumeDown */ 0x11e, /* Sun Type 6 VolumeUp */ 0x120, /* Sun Type 6 PowerDown */ /* Japanese 106/109 keyboard */ 0x73, /* Keyboard Intl' 1 (backslash / underscore) */ 0x70, /* Keyboard Intl' 2 (Katakana / Hiragana) */ 0x7d, /* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */ 0x79, /* Keyboard Intl' 4 (Henkan) */ 0x7b, /* Keyboard Intl' 5 (Muhenkan) */ 0x5c, /* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */ 0x71, /* Apple Keyboard JIS (Kana) */ 0x72, /* Apple Keyboard JIS (Eisu) */ }; if ((code >= 89) && (code < (int)(89 + nitems(scan)))) { code = scan[code - 89]; } /* PrintScreen */ if (code == 0x137 && (!( UKBD_KEY_PRESSED(bitmap, 0xe0 /* CTRL-L */) || UKBD_KEY_PRESSED(bitmap, 0xe4 /* CTRL-R */) || UKBD_KEY_PRESSED(bitmap, 0xe1 /* SHIFT-L */) || UKBD_KEY_PRESSED(bitmap, 0xe5 /* SHIFT-R */)))) { code |= SCAN_PREFIX_SHIFT; } /* Pause/Break */ if ((code == 0x146) && (!( UKBD_KEY_PRESSED(bitmap, 0xe0 /* CTRL-L */) || UKBD_KEY_PRESSED(bitmap, 0xe4 /* CTRL-R */)))) { code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL); } code |= (up ? SCAN_RELEASE : SCAN_PRESS); if (code & SCAN_PREFIX) { if (code & SCAN_PREFIX_CTL) { /* Ctrl */ sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE)); sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX); } else if (code & SCAN_PREFIX_SHIFT) { /* Shift */ sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE)); sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT); } else { sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX); sc->sc_buffered_char[1] = 0; } return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); } return (code); } #endif /* UKBD_EMULATE_ATSCANCODE */ static keyboard_switch_t ukbdsw = { .probe = &ukbd__probe, .init = &ukbd_init, .term = &ukbd_term, .intr = &ukbd_intr, .test_if = &ukbd_test_if, .enable = &ukbd_enable, .disable = &ukbd_disable, .read = &ukbd_read, .check = &ukbd_check, .read_char = &ukbd_read_char, .check_char = &ukbd_check_char, .ioctl = &ukbd_ioctl, .lock = &ukbd_lock, .clear_state = &ukbd_clear_state, .get_state = &ukbd_get_state, .set_state = &ukbd_set_state, .poll = &ukbd_poll, }; KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure); static int ukbd_driver_load(module_t mod, int what, void *arg) { switch (what) { case MOD_LOAD: kbd_add_driver(&ukbd_kbd_driver); break; case MOD_UNLOAD: kbd_delete_driver(&ukbd_kbd_driver); break; } return (0); } static device_method_t ukbd_methods[] = { DEVMETHOD(device_probe, ukbd_probe), DEVMETHOD(device_attach, ukbd_attach), DEVMETHOD(device_detach, ukbd_detach), DEVMETHOD(device_resume, ukbd_resume), DEVMETHOD_END }; static driver_t ukbd_driver = { .name = "ukbd", .methods = ukbd_methods, .size = sizeof(struct ukbd_softc), }; DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_driver_load, NULL); MODULE_DEPEND(ukbd, usb, 1, 1, 1); MODULE_DEPEND(ukbd, hid, 1, 1, 1); #ifdef EVDEV_SUPPORT MODULE_DEPEND(ukbd, evdev, 1, 1, 1); #endif MODULE_VERSION(ukbd, 1); USB_PNP_HOST_INFO(ukbd_devs);