Index: head/sys/dev/atkbdc/atkbd.c =================================================================== --- head/sys/dev/atkbdc/atkbd.c (revision 174983) +++ head/sys/dev/atkbdc/atkbd.c (revision 174984) @@ -1,1501 +1,1501 @@ /*- * Copyright (c) 1999 Kazutaka YOKOTA * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer as * the first lines of this file unmodified. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include __FBSDID("$FreeBSD$"); #include "opt_compat.h" #include "opt_kbd.h" #include "opt_atkbd.h" #include #include #include #include #include #include #include #include #include #include #ifdef __i386__ #include #include #include #include #include #include #include #include #endif /* __i386__ */ #include #include #include #include static timeout_t atkbd_timeout; static void atkbd_shutdown_final(void *v); int atkbd_probe_unit(int unit, int ctlr, 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] = ctlr; args[1] = irq; error = (*sw->probe)(unit, args, flags); if (error) return error; return 0; } int atkbd_attach_unit(int unit, keyboard_t **kbd, int ctlr, 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; /* reset, initialize and enable the device */ args[0] = ctlr; 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 */ atkbd_timeout(*kbd); if (bootverbose) (*sw->diag)(*kbd, bootverbose); EVENTHANDLER_REGISTER(shutdown_final, atkbd_shutdown_final, *kbd, SHUTDOWN_PRI_DEFAULT); return 0; } static void atkbd_timeout(void *arg) { 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 ((*kbdsw[kbd->kb_index]->lock)(kbd, TRUE)) { + 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. */ - (*kbdsw[kbd->kb_index]->lock)(kbd, FALSE); - if ((*kbdsw[kbd->kb_index]->check_char)(kbd)) - (*kbdsw[kbd->kb_index]->intr)(kbd, NULL); + kbdd_lock(kbd, FALSE); + if (kbdd_check_char(kbd)) + kbdd_intr(kbd, NULL); } splx(s); timeout(atkbd_timeout, arg, hz/10); } /* LOW-LEVEL */ #define ATKBD_DEFAULT 0 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 */ } atkbd_state_t; /* keyboard driver declaration */ static int atkbd_configure(int flags); static kbd_probe_t atkbd_probe; static kbd_init_t atkbd_init; static kbd_term_t atkbd_term; static kbd_intr_t atkbd_intr; static kbd_test_if_t atkbd_test_if; static kbd_enable_t atkbd_enable; static kbd_disable_t atkbd_disable; static kbd_read_t atkbd_read; static kbd_check_t atkbd_check; static kbd_read_char_t atkbd_read_char; static kbd_check_char_t atkbd_check_char; static kbd_ioctl_t atkbd_ioctl; static kbd_lock_t atkbd_lock; static kbd_clear_state_t atkbd_clear_state; static kbd_get_state_t atkbd_get_state; static kbd_set_state_t atkbd_set_state; static kbd_poll_mode_t atkbd_poll; static keyboard_switch_t atkbdsw = { atkbd_probe, atkbd_init, atkbd_term, atkbd_intr, atkbd_test_if, atkbd_enable, atkbd_disable, atkbd_read, atkbd_check, atkbd_read_char, atkbd_check_char, atkbd_ioctl, atkbd_lock, atkbd_clear_state, atkbd_get_state, atkbd_set_state, genkbd_get_fkeystr, atkbd_poll, genkbd_diag, }; KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure); /* local functions */ static int get_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); /* 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; /* 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 = sizeof(default_fkeytab)/sizeof(default_fkeytab[0]); 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); get_typematic(kbd); delay[0] = kbd->kb_delay1; delay[1] = kbd->kb_delay2; atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); 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) { kbd_unregister(kbd); return 0; } /* keyboard interrupt routine */ static int atkbd_intr(keyboard_t *kbd, void *arg) { atkbd_state_t *state; int delay[2]; int c; if (!KBD_HAS_DEVICE(kbd)) { /* * The keyboard was not detected before; * it must have been reconnected! */ state = (atkbd_state_t *)kbd->kb_data; init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config); KBD_FOUND_DEVICE(kbd); atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state); get_typematic(kbd); delay[0] = kbd->kb_delay1; delay[1] = kbd->kb_delay2; atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); } 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 /* 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; } } 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); } 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); } return error; case PIO_KEYMAP: /* set keyboard translation table */ case PIO_KEYMAPENT: /* set keyboard translation table entry */ case PIO_DEADKEYMAP: /* set accent key translation table */ state->ks_accents = 0; /* FALLTHROUGH */ default: splx(s); return genkbd_commonioctl(kbd, cmd, arg); } splx(s); return 0; } /* lock the access to the keyboard */ static int atkbd_lock(keyboard_t *kbd, int lock) { return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock); } /* clear the internal state of the keyboard */ static void atkbd_clear_state(keyboard_t *kbd) { atkbd_state_t *state; state = (atkbd_state_t *)kbd->kb_data; state->ks_flags = 0; state->ks_polling = 0; state->ks_state &= LOCK_MASK; /* preserve locking key state */ state->ks_accents = 0; state->ks_composed_char = 0; #if 0 state->ks_prefix = 0; /* XXX */ #endif } /* save the internal state */ static int atkbd_get_state(keyboard_t *kbd, void *buf, size_t len) { if (len == 0) return sizeof(atkbd_state_t); if (len < sizeof(atkbd_state_t)) return -1; bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t)); return 0; } /* set the internal state */ static int atkbd_set_state(keyboard_t *kbd, void *buf, size_t len) { if (len < sizeof(atkbd_state_t)) return ENOMEM; if (((atkbd_state_t *)kbd->kb_data)->kbdc != ((atkbd_state_t *)buf)->kbdc) return ENOMEM; bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t)); return 0; } static int atkbd_poll(keyboard_t *kbd, int on) { atkbd_state_t *state; int s; state = (atkbd_state_t *)kbd->kb_data; s = spltty(); if (on) ++state->ks_polling; else --state->ks_polling; splx(s); return 0; } static void atkbd_shutdown_final(void *v) { #ifdef __sparc64__ keyboard_t *kbd = v; KBDC kbdc = ((atkbd_state_t *)kbd->kb_data)->kbdc; /* * Turn off the translation in preparation for handing the keyboard * over to the OFW as the OBP driver doesn't use translation and * also doesn't disable it itself resulting in a broken keymap at * the boot prompt. Also disable the aux port and the interrupts as * the OBP driver doesn't use them, i.e. polls the keyboard. Not * disabling the interrupts doesn't cause real problems but the * responsiveness is a bit better when they are turned off. */ send_kbd_command(kbdc, KBDC_DISABLE_KBD); set_controller_command_byte(kbdc, KBD_AUX_CONTROL_BITS | KBD_KBD_CONTROL_BITS | KBD_TRANSLATION, KBD_DISABLE_AUX_PORT | KBD_DISABLE_KBD_INT | KBD_ENABLE_KBD_PORT); send_kbd_command(kbdc, KBDC_ENABLE_KBD); #endif } /* local functions */ static int get_typematic(keyboard_t *kbd) { #ifdef __i386__ /* * Only some systems allow us to retrieve the keyboard repeat * rate previously set via the BIOS... */ struct vm86frame vmf; u_int32_t p; bzero(&vmf, sizeof(vmf)); vmf.vmf_ax = 0xc000; vm86_intcall(0x15, &vmf); if ((vmf.vmf_eflags & PSL_C) || vmf.vmf_ah) return ENODEV; p = BIOS_PADDRTOVADDR(((u_int32_t)vmf.vmf_es << 4) + vmf.vmf_bx); if ((readb(p + 6) & 0x40) == 0) /* int 16, function 0x09 supported? */ return ENODEV; vmf.vmf_ax = 0x0900; vm86_intcall(0x16, &vmf); if ((vmf.vmf_al & 0x08) == 0) /* int 16, function 0x0306 supported? */ return ENODEV; vmf.vmf_ax = 0x0306; vm86_intcall(0x16, &vmf); kbd->kb_delay1 = typematic_delay(vmf.vmf_bh << 5); kbd->kb_delay2 = typematic_rate(vmf.vmf_bl); return 0; #else return ENODEV; #endif /* __i386__ */ } 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, 0xff, c); kbdc_set_device_mask(kbdc, m); } #endif kbdc_lock(kbdc, FALSE); return 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; } /* * 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); /* 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, 0xff, c); kbdc_lock(kbdc, FALSE); if (bootverbose) printf("atkbd: failed to reset the keyboard.\n"); 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, 0xff, c); kbdc_lock(kbdc, FALSE); printf("atkbd: unable to set the XT keyboard mode.\n"); return EIO; } } #if defined(__sparc64__) if (send_kbd_command_and_data( kbdc, KBDC_SET_SCANCODE_SET, 2) != KBD_ACK) { printf("atkbd: can't set translation.\n"); } c |= KBD_TRANSLATION; #endif /* 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, 0xff, 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 = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; --i) { if (delay >= delays[i]) break; } value = i << 5; for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; --i) { if (rate >= rates[i]) break; } value |= i; return value; } Index: head/sys/dev/atkbdc/atkbd_atkbdc.c =================================================================== --- head/sys/dev/atkbdc/atkbd_atkbdc.c (revision 174983) +++ head/sys/dev/atkbdc/atkbd_atkbdc.c (revision 174984) @@ -1,177 +1,177 @@ /*- * 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 #include #include #include #include #include #include #include #include #include #include #include typedef struct { struct resource *intr; void *ih; } atkbd_softc_t; static devclass_t atkbd_devclass; static void atkbdidentify(driver_t *driver, device_t dev); static int atkbdprobe(device_t dev); static int atkbdattach(device_t dev); static int atkbdresume(device_t dev); static void atkbdintr(void *arg); static device_method_t atkbd_methods[] = { DEVMETHOD(device_identify, atkbdidentify), DEVMETHOD(device_probe, atkbdprobe), DEVMETHOD(device_attach, atkbdattach), DEVMETHOD(device_resume, atkbdresume), { 0, 0 } }; static driver_t atkbd_driver = { ATKBD_DRIVER_NAME, atkbd_methods, sizeof(atkbd_softc_t), }; static void atkbdidentify(driver_t *driver, device_t parent) { /* always add at least one child */ BUS_ADD_CHILD(parent, KBDC_RID_KBD, driver->name, device_get_unit(parent)); } static int atkbdprobe(device_t dev) { struct resource *res; u_long irq; int flags; int rid; device_set_desc(dev, "AT Keyboard"); /* obtain parameters */ flags = device_get_flags(dev); /* see if IRQ is available */ rid = KBDC_RID_KBD; res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE); if (res == NULL) { if (bootverbose) device_printf(dev, "unable to allocate IRQ\n"); return ENXIO; } irq = rman_get_start(res); bus_release_resource(dev, SYS_RES_IRQ, rid, res); /* probe the device */ return atkbd_probe_unit(device_get_unit(dev), device_get_unit(device_get_parent(dev)), irq, flags); } static int atkbdattach(device_t dev) { atkbd_softc_t *sc; keyboard_t *kbd; u_long irq; int flags; int rid; int error; sc = device_get_softc(dev); rid = KBDC_RID_KBD; irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid); flags = device_get_flags(dev); error = atkbd_attach_unit(device_get_unit(dev), &kbd, device_get_unit(device_get_parent(dev)), irq, flags); if (error) return error; /* declare our interrupt handler */ sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE); if (sc->intr == NULL) return ENXIO; error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, NULL, atkbdintr, kbd, &sc->ih); if (error) bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); return error; } static int atkbdresume(device_t dev) { atkbd_softc_t *sc; keyboard_t *kbd; int args[2]; sc = device_get_softc(dev); kbd = kbd_get_keyboard(kbd_find_keyboard(ATKBD_DRIVER_NAME, device_get_unit(dev))); if (kbd) { kbd->kb_flags &= ~KB_INITIALIZED; args[0] = device_get_unit(device_get_parent(dev)); args[1] = rman_get_start(sc->intr); - (*kbdsw[kbd->kb_index]->init)(device_get_unit(dev), &kbd, - args, device_get_flags(dev)); - (*kbdsw[kbd->kb_index]->clear_state)(kbd); + kbdd_init(kbd, device_get_unit(dev), &kbd, args, + device_get_flags(dev)); + kbdd_clear_state(kbd); } return 0; } static void atkbdintr(void *arg) { keyboard_t *kbd; kbd = (keyboard_t *)arg; - (*kbdsw[kbd->kb_index]->intr)(kbd, NULL); + kbdd_intr(kbd, NULL); } DRIVER_MODULE(atkbd, atkbdc, atkbd_driver, atkbd_devclass, 0, 0); Index: head/sys/dev/kbd/kbd.c =================================================================== --- head/sys/dev/kbd/kbd.c (revision 174983) +++ head/sys/dev/kbd/kbd.c (revision 174984) @@ -1,1427 +1,1422 @@ /*- * 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #define KBD_INDEX(dev) minor(dev) typedef struct genkbd_softc { int gkb_flags; /* flag/status bits */ #define KB_ASLEEP (1 << 0) struct clist gkb_q; /* input queue */ struct selinfo gkb_rsel; } genkbd_softc_t; static SLIST_HEAD(, keyboard_driver) keyboard_drivers = SLIST_HEAD_INITIALIZER(keyboard_drivers); SET_DECLARE(kbddriver_set, const keyboard_driver_t); /* local arrays */ /* * We need at least one entry each in order to initialize a keyboard * for the kernel console. The arrays will be increased dynamically * when necessary. */ static int keyboards = 1; static keyboard_t *kbd_ini; static keyboard_t **keyboard = &kbd_ini; static keyboard_switch_t *kbdsw_ini; keyboard_switch_t **kbdsw = &kbdsw_ini; static int keymap_restrict_change; SYSCTL_NODE(_hw, OID_AUTO, kbd, CTLFLAG_RD, 0, "kbd"); SYSCTL_INT(_hw_kbd, OID_AUTO, keymap_restrict_change, CTLFLAG_RW, &keymap_restrict_change, 0, "restrict ability to change keymap"); #define ARRAY_DELTA 4 static int kbd_realloc_array(void) { keyboard_t **new_kbd; keyboard_switch_t **new_kbdsw; int newsize; int s; s = spltty(); newsize = ((keyboards + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA; new_kbd = malloc(sizeof(*new_kbd)*newsize, M_DEVBUF, M_NOWAIT|M_ZERO); if (new_kbd == NULL) { splx(s); return (ENOMEM); } new_kbdsw = malloc(sizeof(*new_kbdsw)*newsize, M_DEVBUF, M_NOWAIT|M_ZERO); if (new_kbdsw == NULL) { free(new_kbd, M_DEVBUF); splx(s); return (ENOMEM); } bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards); bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards); if (keyboards > 1) { free(keyboard, M_DEVBUF); free(kbdsw, M_DEVBUF); } keyboard = new_kbd; kbdsw = new_kbdsw; keyboards = newsize; splx(s); if (bootverbose) printf("kbd: new array size %d\n", keyboards); return (0); } /* * Low-level keyboard driver functions * Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard * driver, call these functions to initialize the keyboard_t structure * and register it to the virtual keyboard driver `kbd'. */ /* initialize the keyboard_t structure */ void kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config, int port, int port_size) { kbd->kb_flags = KB_NO_DEVICE; /* device has not been found */ kbd->kb_name = name; kbd->kb_type = type; kbd->kb_unit = unit; kbd->kb_config = config & ~KB_CONF_PROBE_ONLY; kbd->kb_led = 0; /* unknown */ kbd->kb_io_base = port; kbd->kb_io_size = port_size; kbd->kb_data = NULL; kbd->kb_keymap = NULL; kbd->kb_accentmap = NULL; kbd->kb_fkeytab = NULL; kbd->kb_fkeytab_size = 0; kbd->kb_delay1 = KB_DELAY1; /* these values are advisory only */ kbd->kb_delay2 = KB_DELAY2; kbd->kb_count = 0L; bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact)); } void kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap, fkeytab_t *fkeymap, int fkeymap_size) { kbd->kb_keymap = keymap; kbd->kb_accentmap = accmap; kbd->kb_fkeytab = fkeymap; kbd->kb_fkeytab_size = fkeymap_size; } /* declare a new keyboard driver */ int kbd_add_driver(keyboard_driver_t *driver) { if (SLIST_NEXT(driver, link)) return (EINVAL); SLIST_INSERT_HEAD(&keyboard_drivers, driver, link); return (0); } int kbd_delete_driver(keyboard_driver_t *driver) { SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link); SLIST_NEXT(driver, link) = NULL; return (0); } /* register a keyboard and associate it with a function table */ int kbd_register(keyboard_t *kbd) { const keyboard_driver_t **list; const keyboard_driver_t *p; keyboard_t *mux; keyboard_info_t ki; int index; mux = kbd_get_keyboard(kbd_find_keyboard("kbdmux", -1)); for (index = 0; index < keyboards; ++index) { if (keyboard[index] == NULL) break; } if (index >= keyboards) { if (kbd_realloc_array()) return (-1); } kbd->kb_index = index; KBD_UNBUSY(kbd); KBD_VALID(kbd); kbd->kb_active = 0; /* disabled until someone calls kbd_enable() */ kbd->kb_token = NULL; kbd->kb_callback.kc_func = NULL; kbd->kb_callback.kc_arg = NULL; SLIST_FOREACH(p, &keyboard_drivers, link) { if (strcmp(p->name, kbd->kb_name) == 0) { keyboard[index] = kbd; kbdsw[index] = p->kbdsw; if (mux != NULL) { bzero(&ki, sizeof(ki)); strcpy(ki.kb_name, kbd->kb_name); ki.kb_unit = kbd->kb_unit; - (*kbdsw[mux->kb_index]->ioctl) - (mux, KBADDKBD, (caddr_t) &ki); + kbdd_ioctl(mux, KBADDKBD, (caddr_t) &ki); } return (index); } } SET_FOREACH(list, kbddriver_set) { p = *list; if (strcmp(p->name, kbd->kb_name) == 0) { keyboard[index] = kbd; kbdsw[index] = p->kbdsw; if (mux != NULL) { bzero(&ki, sizeof(ki)); strcpy(ki.kb_name, kbd->kb_name); ki.kb_unit = kbd->kb_unit; - (*kbdsw[mux->kb_index]->ioctl) - (mux, KBADDKBD, (caddr_t) &ki); + kbdd_ioctl(mux, KBADDKBD, (caddr_t) &ki); } return (index); } } return (-1); } int kbd_unregister(keyboard_t *kbd) { int error; int s; if ((kbd->kb_index < 0) || (kbd->kb_index >= keyboards)) return (ENOENT); if (keyboard[kbd->kb_index] != kbd) return (ENOENT); s = spltty(); if (KBD_IS_BUSY(kbd)) { error = (*kbd->kb_callback.kc_func)(kbd, KBDIO_UNLOADING, kbd->kb_callback.kc_arg); if (error) { splx(s); return (error); } if (KBD_IS_BUSY(kbd)) { splx(s); return (EBUSY); } } KBD_INVALID(kbd); keyboard[kbd->kb_index] = NULL; kbdsw[kbd->kb_index] = NULL; splx(s); return (0); } /* find a funciton table by the driver name */ keyboard_switch_t *kbd_get_switch(char *driver) { const keyboard_driver_t **list; const keyboard_driver_t *p; SLIST_FOREACH(p, &keyboard_drivers, link) { if (strcmp(p->name, driver) == 0) return (p->kbdsw); } SET_FOREACH(list, kbddriver_set) { p = *list; if (strcmp(p->name, driver) == 0) return (p->kbdsw); } return (NULL); } /* * Keyboard client functions * Keyboard clients, such as the console driver `syscons' and the keyboard * cdev driver, use these functions to claim and release a keyboard for * exclusive use. */ /* * find the keyboard specified by a driver name and a unit number * starting at given index */ int kbd_find_keyboard2(char *driver, int unit, int index) { int i; if ((index < 0) || (index >= keyboards)) return (-1); for (i = index; i < keyboards; ++i) { if (keyboard[i] == NULL) continue; if (!KBD_IS_VALID(keyboard[i])) continue; if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver)) continue; if ((unit != -1) && (keyboard[i]->kb_unit != unit)) continue; return (i); } return (-1); } /* find the keyboard specified by a driver name and a unit number */ int kbd_find_keyboard(char *driver, int unit) { return (kbd_find_keyboard2(driver, unit, 0)); } /* allocate a keyboard */ int kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func, void *arg) { int index; int s; if (func == NULL) return (-1); s = spltty(); index = kbd_find_keyboard(driver, unit); if (index >= 0) { if (KBD_IS_BUSY(keyboard[index])) { splx(s); return (-1); } keyboard[index]->kb_token = id; KBD_BUSY(keyboard[index]); keyboard[index]->kb_callback.kc_func = func; keyboard[index]->kb_callback.kc_arg = arg; - (*kbdsw[index]->clear_state)(keyboard[index]); + kbdd_clear_state(keyboard[index]); } splx(s); return (index); } int kbd_release(keyboard_t *kbd, void *id) { int error; int s; s = spltty(); if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) { error = EINVAL; } else if (kbd->kb_token != id) { error = EPERM; } else { kbd->kb_token = NULL; KBD_UNBUSY(kbd); kbd->kb_callback.kc_func = NULL; kbd->kb_callback.kc_arg = NULL; - (*kbdsw[kbd->kb_index]->clear_state)(kbd); + kbdd_clear_state(kbd); error = 0; } splx(s); return (error); } int kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func, void *arg) { int error; int s; s = spltty(); if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) { error = EINVAL; } else if (kbd->kb_token != id) { error = EPERM; } else if (func == NULL) { error = EINVAL; } else { kbd->kb_callback.kc_func = func; kbd->kb_callback.kc_arg = arg; error = 0; } splx(s); return (error); } /* get a keyboard structure */ keyboard_t *kbd_get_keyboard(int index) { if ((index < 0) || (index >= keyboards)) return (NULL); if (keyboard[index] == NULL) return (NULL); if (!KBD_IS_VALID(keyboard[index])) return (NULL); return (keyboard[index]); } /* * The back door for the console driver; configure keyboards * This function is for the kernel console to initialize keyboards * at very early stage. */ int kbd_configure(int flags) { const keyboard_driver_t **list; const keyboard_driver_t *p; SLIST_FOREACH(p, &keyboard_drivers, link) { if (p->configure != NULL) (*p->configure)(flags); } SET_FOREACH(list, kbddriver_set) { p = *list; if (p->configure != NULL) (*p->configure)(flags); } return (0); } #ifdef KBD_INSTALL_CDEV /* * Virtual keyboard cdev driver functions * The virtual keyboard driver dispatches driver functions to * appropriate subdrivers. */ #define KBD_UNIT(dev) minor(dev) static d_open_t genkbdopen; static d_close_t genkbdclose; static d_read_t genkbdread; static d_write_t genkbdwrite; static d_ioctl_t genkbdioctl; static d_poll_t genkbdpoll; static struct cdevsw kbd_cdevsw = { .d_version = D_VERSION, .d_flags = D_NEEDGIANT, .d_open = genkbdopen, .d_close = genkbdclose, .d_read = genkbdread, .d_write = genkbdwrite, .d_ioctl = genkbdioctl, .d_poll = genkbdpoll, .d_name = "kbd", }; int kbd_attach(keyboard_t *kbd) { if (kbd->kb_index >= keyboards) return (EINVAL); if (keyboard[kbd->kb_index] != kbd) return (EINVAL); kbd->kb_dev = make_dev(&kbd_cdevsw, kbd->kb_index, UID_ROOT, GID_WHEEL, 0600, "%s%r", kbd->kb_name, kbd->kb_unit); make_dev_alias(kbd->kb_dev, "kbd%r", kbd->kb_index); kbd->kb_dev->si_drv1 = malloc(sizeof(genkbd_softc_t), M_DEVBUF, M_WAITOK | M_ZERO); printf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit); return (0); } int kbd_detach(keyboard_t *kbd) { if (kbd->kb_index >= keyboards) return (EINVAL); if (keyboard[kbd->kb_index] != kbd) return (EINVAL); free(kbd->kb_dev->si_drv1, M_DEVBUF); destroy_dev(kbd->kb_dev); return (0); } /* * Generic keyboard cdev driver functions * Keyboard subdrivers may call these functions to implement common * driver functions. */ #define KB_QSIZE 512 #define KB_BUFSIZE 64 static kbd_callback_func_t genkbd_event; static int genkbdopen(struct cdev *dev, int mode, int flag, struct thread *td) { keyboard_t *kbd; genkbd_softc_t *sc; int s; int i; s = spltty(); sc = dev->si_drv1; kbd = kbd_get_keyboard(KBD_INDEX(dev)); if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) { splx(s); return (ENXIO); } i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc, genkbd_event, (void *)sc); if (i < 0) { splx(s); return (EBUSY); } /* assert(i == kbd->kb_index) */ /* assert(kbd == kbd_get_keyboard(i)) */ /* * NOTE: even when we have successfully claimed a keyboard, * the device may still be missing (!KBD_HAS_DEVICE(kbd)). */ #if 0 bzero(&sc->gkb_q, sizeof(sc->gkb_q)); #endif clist_alloc_cblocks(&sc->gkb_q, KB_QSIZE, KB_QSIZE/2); /* XXX */ splx(s); return (0); } static int genkbdclose(struct cdev *dev, int mode, int flag, struct thread *td) { keyboard_t *kbd; genkbd_softc_t *sc; int s; /* * NOTE: the device may have already become invalid. * kbd == NULL || !KBD_IS_VALID(kbd) */ s = spltty(); sc = dev->si_drv1; kbd = kbd_get_keyboard(KBD_INDEX(dev)); if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) { /* XXX: we shall be forgiving and don't report error... */ } else { kbd_release(kbd, (void *)sc); #if 0 clist_free_cblocks(&sc->gkb_q); #endif } splx(s); return (0); } static int genkbdread(struct cdev *dev, struct uio *uio, int flag) { keyboard_t *kbd; genkbd_softc_t *sc; u_char buffer[KB_BUFSIZE]; int len; int error; int s; /* wait for input */ s = spltty(); sc = dev->si_drv1; kbd = kbd_get_keyboard(KBD_INDEX(dev)); if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) { splx(s); return (ENXIO); } while (sc->gkb_q.c_cc == 0) { if (flag & O_NONBLOCK) { splx(s); return (EWOULDBLOCK); } sc->gkb_flags |= KB_ASLEEP; error = tsleep(sc, PZERO | PCATCH, "kbdrea", 0); kbd = kbd_get_keyboard(KBD_INDEX(dev)); if ((kbd == NULL) || !KBD_IS_VALID(kbd)) { splx(s); return (ENXIO); /* our keyboard has gone... */ } if (error) { sc->gkb_flags &= ~KB_ASLEEP; splx(s); return (error); } } splx(s); /* copy as much input as possible */ error = 0; while (uio->uio_resid > 0) { len = imin(uio->uio_resid, sizeof(buffer)); len = q_to_b(&sc->gkb_q, buffer, len); if (len <= 0) break; error = uiomove(buffer, len, uio); if (error) break; } return (error); } static int genkbdwrite(struct cdev *dev, struct uio *uio, int flag) { keyboard_t *kbd; kbd = kbd_get_keyboard(KBD_INDEX(dev)); if ((kbd == NULL) || !KBD_IS_VALID(kbd)) return (ENXIO); return (ENODEV); } static int genkbdioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td) { keyboard_t *kbd; int error; kbd = kbd_get_keyboard(KBD_INDEX(dev)); if ((kbd == NULL) || !KBD_IS_VALID(kbd)) return (ENXIO); - error = (*kbdsw[kbd->kb_index]->ioctl)(kbd, cmd, arg); + error = kbdd_ioctl(kbd, cmd, arg); if (error == ENOIOCTL) error = ENODEV; return (error); } static int genkbdpoll(struct cdev *dev, int events, struct thread *td) { keyboard_t *kbd; genkbd_softc_t *sc; int revents; int s; revents = 0; s = spltty(); sc = dev->si_drv1; kbd = kbd_get_keyboard(KBD_INDEX(dev)); if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) { revents = POLLHUP; /* the keyboard has gone */ } else if (events & (POLLIN | POLLRDNORM)) { if (sc->gkb_q.c_cc > 0) revents = events & (POLLIN | POLLRDNORM); else selrecord(td, &sc->gkb_rsel); } splx(s); return (revents); } static int genkbd_event(keyboard_t *kbd, int event, void *arg) { genkbd_softc_t *sc; size_t len; u_char *cp; int mode; int c; /* assert(KBD_IS_VALID(kbd)) */ sc = (genkbd_softc_t *)arg; switch (event) { case KBDIO_KEYINPUT: break; case KBDIO_UNLOADING: /* the keyboard is going... */ kbd_release(kbd, (void *)sc); if (sc->gkb_flags & KB_ASLEEP) { sc->gkb_flags &= ~KB_ASLEEP; wakeup(sc); } selwakeuppri(&sc->gkb_rsel, PZERO); return (0); default: return (EINVAL); } /* obtain the current key input mode */ - if ((*kbdsw[kbd->kb_index]->ioctl)(kbd, KDGKBMODE, (caddr_t)&mode)) + if (kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode)) mode = K_XLATE; /* read all pending input */ - while ((*kbdsw[kbd->kb_index]->check_char)(kbd)) { - c = (*kbdsw[kbd->kb_index]->read_char)(kbd, FALSE); + while (kbdd_check_char(kbd)) { + c = kbdd_read_char(kbd, FALSE); if (c == NOKEY) continue; if (c == ERRKEY) /* XXX: ring bell? */ continue; if (!KBD_IS_BUSY(kbd)) /* the device is not open, discard the input */ continue; /* store the byte as is for K_RAW and K_CODE modes */ if (mode != K_XLATE) { putc(KEYCHAR(c), &sc->gkb_q); continue; } /* K_XLATE */ if (c & RELKEY) /* key release is ignored */ continue; /* process special keys; most of them are just ignored... */ if (c & SPCLKEY) { switch (KEYCHAR(c)) { default: /* ignore them... */ continue; case BTAB: /* a backtab: ESC [ Z */ putc(0x1b, &sc->gkb_q); putc('[', &sc->gkb_q); putc('Z', &sc->gkb_q); continue; } } /* normal chars, normal chars with the META, function keys */ switch (KEYFLAGS(c)) { case 0: /* a normal char */ putc(KEYCHAR(c), &sc->gkb_q); break; case MKEY: /* the META flag: prepend ESC */ putc(0x1b, &sc->gkb_q); putc(KEYCHAR(c), &sc->gkb_q); break; case FKEY | SPCLKEY: /* a function key, return string */ - cp = (*kbdsw[kbd->kb_index]->get_fkeystr)(kbd, - KEYCHAR(c), &len); + cp = kbdd_get_fkeystr(kbd, KEYCHAR(c), &len); if (cp != NULL) { while (len-- > 0) putc(*cp++, &sc->gkb_q); } break; } } /* wake up sleeping/polling processes */ if (sc->gkb_q.c_cc > 0) { if (sc->gkb_flags & KB_ASLEEP) { sc->gkb_flags &= ~KB_ASLEEP; wakeup(sc); } selwakeuppri(&sc->gkb_rsel, PZERO); } return (0); } #endif /* KBD_INSTALL_CDEV */ /* * Generic low-level keyboard functions * The low-level functions in the keyboard subdriver may use these * functions. */ #ifndef KBD_DISABLE_KEYMAP_LOAD static int key_change_ok(struct keyent_t *, struct keyent_t *, struct thread *); static int keymap_change_ok(keymap_t *, keymap_t *, struct thread *); static int accent_change_ok(accentmap_t *, accentmap_t *, struct thread *); static int fkey_change_ok(fkeytab_t *, fkeyarg_t *, struct thread *); #endif int genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) { keyarg_t *keyp; fkeyarg_t *fkeyp; int s; int i; #ifndef KBD_DISABLE_KEYMAP_LOAD int error; #endif s = spltty(); switch (cmd) { case KDGKBINFO: /* get keyboard information */ ((keyboard_info_t *)arg)->kb_index = kbd->kb_index; i = imin(strlen(kbd->kb_name) + 1, sizeof(((keyboard_info_t *)arg)->kb_name)); bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i); ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit; ((keyboard_info_t *)arg)->kb_type = kbd->kb_type; ((keyboard_info_t *)arg)->kb_config = kbd->kb_config; ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags; break; case KDGKBTYPE: /* get keyboard type */ *(int *)arg = kbd->kb_type; break; case KDGETREPEAT: /* get keyboard repeat rate */ ((int *)arg)[0] = kbd->kb_delay1; ((int *)arg)[1] = kbd->kb_delay2; break; case GIO_KEYMAP: /* get keyboard translation table */ bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap)); break; case PIO_KEYMAP: /* set keyboard translation table */ #ifndef KBD_DISABLE_KEYMAP_LOAD error = keymap_change_ok(kbd->kb_keymap, (keymap_t *)arg, curthread); if (error != 0) { splx(s); return (error); } bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap)); bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap)); break; #else splx(s); return (ENODEV); #endif case GIO_KEYMAPENT: /* get keyboard translation table entry */ keyp = (keyarg_t *)arg; if (keyp->keynum >= sizeof(kbd->kb_keymap->key) / sizeof(kbd->kb_keymap->key[0])) { splx(s); return (EINVAL); } bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key, sizeof(keyp->key)); break; case PIO_KEYMAPENT: /* set keyboard translation table entry */ #ifndef KBD_DISABLE_KEYMAP_LOAD keyp = (keyarg_t *)arg; if (keyp->keynum >= sizeof(kbd->kb_keymap->key) / sizeof(kbd->kb_keymap->key[0])) { splx(s); return (EINVAL); } error = key_change_ok(&kbd->kb_keymap->key[keyp->keynum], &keyp->key, curthread); if (error != 0) { splx(s); return (error); } bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum], sizeof(keyp->key)); break; #else splx(s); return (ENODEV); #endif case GIO_DEADKEYMAP: /* get accent key translation table */ bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap)); break; case PIO_DEADKEYMAP: /* set accent key translation table */ #ifndef KBD_DISABLE_KEYMAP_LOAD error = accent_change_ok(kbd->kb_accentmap, (accentmap_t *)arg, curthread); if (error != 0) { splx(s); return (error); } bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap)); break; #else splx(s); return (ENODEV); #endif case GETFKEY: /* get functionkey string */ fkeyp = (fkeyarg_t *)arg; if (fkeyp->keynum >= kbd->kb_fkeytab_size) { splx(s); return (EINVAL); } bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].len); fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len; break; case SETFKEY: /* set functionkey string */ #ifndef KBD_DISABLE_KEYMAP_LOAD fkeyp = (fkeyarg_t *)arg; if (fkeyp->keynum >= kbd->kb_fkeytab_size) { splx(s); return (EINVAL); } error = fkey_change_ok(&kbd->kb_fkeytab[fkeyp->keynum], fkeyp, curthread); if (error != 0) { splx(s); return (error); } kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK); bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str, kbd->kb_fkeytab[fkeyp->keynum].len); break; #else splx(s); return (ENODEV); #endif default: splx(s); return (ENOIOCTL); } splx(s); return (0); } #ifndef KBD_DISABLE_KEYMAP_LOAD #define RESTRICTED_KEY(key, i) \ ((key->spcl & (0x80 >> i)) && \ (key->map[i] == RBT || key->map[i] == SUSP || \ key->map[i] == STBY || key->map[i] == DBG || \ key->map[i] == PNC || key->map[i] == HALT || \ key->map[i] == PDWN)) static int key_change_ok(struct keyent_t *oldkey, struct keyent_t *newkey, struct thread *td) { int i; /* Low keymap_restrict_change means any changes are OK. */ if (keymap_restrict_change <= 0) return (0); /* High keymap_restrict_change means only root can change the keymap. */ if (keymap_restrict_change >= 2) { for (i = 0; i < NUM_STATES; i++) if (oldkey->map[i] != newkey->map[i]) return priv_check(td, PRIV_KEYBOARD); if (oldkey->spcl != newkey->spcl) return priv_check(td, PRIV_KEYBOARD); if (oldkey->flgs != newkey->flgs) return priv_check(td, PRIV_KEYBOARD); return (0); } /* Otherwise we have to see if any special keys are being changed. */ for (i = 0; i < NUM_STATES; i++) { /* * If either the oldkey or the newkey action is restricted * then we must make sure that the action doesn't change. */ if (!RESTRICTED_KEY(oldkey, i) && !RESTRICTED_KEY(newkey, i)) continue; if ((oldkey->spcl & (0x80 >> i)) == (newkey->spcl & (0x80 >> i)) && oldkey->map[i] == newkey->map[i]) continue; return priv_check(td, PRIV_KEYBOARD); } return (0); } static int keymap_change_ok(keymap_t *oldmap, keymap_t *newmap, struct thread *td) { int keycode, error; for (keycode = 0; keycode < NUM_KEYS; keycode++) { if ((error = key_change_ok(&oldmap->key[keycode], &newmap->key[keycode], td)) != 0) return (error); } return (0); } static int accent_change_ok(accentmap_t *oldmap, accentmap_t *newmap, struct thread *td) { struct acc_t *oldacc, *newacc; int accent, i; if (keymap_restrict_change <= 2) return (0); if (oldmap->n_accs != newmap->n_accs) return priv_check(td, PRIV_KEYBOARD); for (accent = 0; accent < oldmap->n_accs; accent++) { oldacc = &oldmap->acc[accent]; newacc = &newmap->acc[accent]; if (oldacc->accchar != newacc->accchar) return priv_check(td, PRIV_KEYBOARD); for (i = 0; i < NUM_ACCENTCHARS; ++i) { if (oldacc->map[i][0] != newacc->map[i][0]) return priv_check(td, PRIV_KEYBOARD); if (oldacc->map[i][0] == 0) /* end of table */ break; if (oldacc->map[i][1] != newacc->map[i][1]) return priv_check(td, PRIV_KEYBOARD); } } return (0); } static int fkey_change_ok(fkeytab_t *oldkey, fkeyarg_t *newkey, struct thread *td) { if (keymap_restrict_change <= 3) return (0); if (oldkey->len != newkey->flen || bcmp(oldkey->str, newkey->keydef, oldkey->len) != 0) return priv_check(td, PRIV_KEYBOARD); return (0); } #endif /* get a pointer to the string associated with the given function key */ u_char *genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len) { if (kbd == NULL) return (NULL); fkey -= F_FN; if (fkey > kbd->kb_fkeytab_size) return (NULL); *len = kbd->kb_fkeytab[fkey].len; return (kbd->kb_fkeytab[fkey].str); } /* diagnostic dump */ static char *get_kbd_type_name(int type) { static struct { int type; char *name; } name_table[] = { { KB_84, "AT 84" }, { KB_101, "AT 101/102" }, { KB_OTHER, "generic" }, }; int i; for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) { if (type == name_table[i].type) return (name_table[i].name); } return ("unknown"); } void genkbd_diag(keyboard_t *kbd, int level) { if (level > 0) { printf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x", kbd->kb_index, kbd->kb_name, kbd->kb_unit, get_kbd_type_name(kbd->kb_type), kbd->kb_type, kbd->kb_config, kbd->kb_flags); if (kbd->kb_io_base > 0) printf(", port:0x%x-0x%x", kbd->kb_io_base, kbd->kb_io_base + kbd->kb_io_size - 1); printf("\n"); } } #define set_lockkey_state(k, s, l) \ if (!((s) & l ## DOWN)) { \ int i; \ (s) |= l ## DOWN; \ (s) ^= l ## ED; \ i = (s) & LOCK_MASK; \ - (*kbdsw[(k)->kb_index]->ioctl)((k), KDSETLED, (caddr_t)&i); \ + kbdd_ioctl((k), KDSETLED, (caddr_t)&i); \ } static u_int save_accent_key(keyboard_t *kbd, u_int key, int *accents) { int i; /* make an index into the accent map */ i = key - F_ACC + 1; if ((i > kbd->kb_accentmap->n_accs) || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) { /* the index is out of range or pointing to an empty entry */ *accents = 0; return (ERRKEY); } /* * If the same accent key has been hit twice, produce the accent * char itself. */ if (i == *accents) { key = kbd->kb_accentmap->acc[i - 1].accchar; *accents = 0; return (key); } /* remember the index and wait for the next key */ *accents = i; return (NOKEY); } static u_int make_accent_char(keyboard_t *kbd, u_int ch, int *accents) { struct acc_t *acc; int i; acc = &kbd->kb_accentmap->acc[*accents - 1]; *accents = 0; /* * If the accent key is followed by the space key, * produce the accent char itself. */ if (ch == ' ') return (acc->accchar); /* scan the accent map */ for (i = 0; i < NUM_ACCENTCHARS; ++i) { if (acc->map[i][0] == 0) /* end of table */ break; if (acc->map[i][0] == ch) return (acc->map[i][1]); } /* this char cannot be accented... */ return (ERRKEY); } int genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate, int *accents) { struct keyent_t *key; int state = *shiftstate; int action; int f; int i; i = keycode; f = state & (AGRS | ALKED); if ((f == AGRS1) || (f == AGRS2) || (f == ALKED)) i += ALTGR_OFFSET; key = &kbd->kb_keymap->key[i]; i = ((state & SHIFTS) ? 1 : 0) | ((state & CTLS) ? 2 : 0) | ((state & ALTS) ? 4 : 0); if (((key->flgs & FLAG_LOCK_C) && (state & CLKED)) || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) ) i ^= 1; if (up) { /* break: key released */ action = kbd->kb_lastact[keycode]; kbd->kb_lastact[keycode] = NOP; switch (action) { case LSHA: if (state & SHIFTAON) { set_lockkey_state(kbd, state, ALK); state &= ~ALKDOWN; } action = LSH; /* FALL THROUGH */ case LSH: state &= ~SHIFTS1; break; case RSHA: if (state & SHIFTAON) { set_lockkey_state(kbd, state, ALK); state &= ~ALKDOWN; } action = RSH; /* FALL THROUGH */ case RSH: state &= ~SHIFTS2; break; case LCTRA: if (state & SHIFTAON) { set_lockkey_state(kbd, state, ALK); state &= ~ALKDOWN; } action = LCTR; /* FALL THROUGH */ case LCTR: state &= ~CTLS1; break; case RCTRA: if (state & SHIFTAON) { set_lockkey_state(kbd, state, ALK); state &= ~ALKDOWN; } action = RCTR; /* FALL THROUGH */ case RCTR: state &= ~CTLS2; break; case LALTA: if (state & SHIFTAON) { set_lockkey_state(kbd, state, ALK); state &= ~ALKDOWN; } action = LALT; /* FALL THROUGH */ case LALT: state &= ~ALTS1; break; case RALTA: if (state & SHIFTAON) { set_lockkey_state(kbd, state, ALK); state &= ~ALKDOWN; } action = RALT; /* FALL THROUGH */ case RALT: state &= ~ALTS2; break; case ASH: state &= ~AGRS1; break; case META: state &= ~METAS1; break; case NLK: state &= ~NLKDOWN; break; case CLK: #ifndef PC98 state &= ~CLKDOWN; #else state &= ~CLKED; i = state & LOCK_MASK; - (*kbdsw[kbd->kb_index]->ioctl)(kbd, KDSETLED, - (caddr_t)&i); + kbdd_ioctl(kbd, KDSETLED, (caddr_t)&i); #endif break; case SLK: state &= ~SLKDOWN; break; case ALK: state &= ~ALKDOWN; break; case NOP: /* release events of regular keys are not reported */ *shiftstate &= ~SHIFTAON; return (NOKEY); } *shiftstate = state & ~SHIFTAON; return (SPCLKEY | RELKEY | action); } else { /* make: key pressed */ action = key->map[i]; state &= ~SHIFTAON; if (key->spcl & (0x80 >> i)) { /* special keys */ if (kbd->kb_lastact[keycode] == NOP) kbd->kb_lastact[keycode] = action; if (kbd->kb_lastact[keycode] != action) action = NOP; switch (action) { /* LOCKING KEYS */ case NLK: set_lockkey_state(kbd, state, NLK); break; case CLK: #ifndef PC98 set_lockkey_state(kbd, state, CLK); #else state |= CLKED; i = state & LOCK_MASK; - (*kbdsw[kbd->kb_index]->ioctl)(kbd, KDSETLED, - (caddr_t)&i); + kbdd_ioctl(kbd, KDSETLED, (caddr_t)&i); #endif break; case SLK: set_lockkey_state(kbd, state, SLK); break; case ALK: set_lockkey_state(kbd, state, ALK); break; /* NON-LOCKING KEYS */ case SPSC: case RBT: case SUSP: case STBY: case DBG: case NEXT: case PREV: case PNC: case HALT: case PDWN: *accents = 0; break; case BTAB: *accents = 0; action |= BKEY; break; case LSHA: state |= SHIFTAON; action = LSH; /* FALL THROUGH */ case LSH: state |= SHIFTS1; break; case RSHA: state |= SHIFTAON; action = RSH; /* FALL THROUGH */ case RSH: state |= SHIFTS2; break; case LCTRA: state |= SHIFTAON; action = LCTR; /* FALL THROUGH */ case LCTR: state |= CTLS1; break; case RCTRA: state |= SHIFTAON; action = RCTR; /* FALL THROUGH */ case RCTR: state |= CTLS2; break; case LALTA: state |= SHIFTAON; action = LALT; /* FALL THROUGH */ case LALT: state |= ALTS1; break; case RALTA: state |= SHIFTAON; action = RALT; /* FALL THROUGH */ case RALT: state |= ALTS2; break; case ASH: state |= AGRS1; break; case META: state |= METAS1; break; case NOP: *shiftstate = state; return (NOKEY); default: /* is this an accent (dead) key? */ *shiftstate = state; if (action >= F_ACC && action <= L_ACC) { action = save_accent_key(kbd, action, accents); switch (action) { case NOKEY: case ERRKEY: return (action); default: if (state & METAS) return (action | MKEY); else return (action); } /* NOT REACHED */ } /* other special keys */ if (*accents > 0) { *accents = 0; return (ERRKEY); } if (action >= F_FN && action <= L_FN) action |= FKEY; /* XXX: return fkey string for the FKEY? */ return (SPCLKEY | action); } *shiftstate = state; return (SPCLKEY | action); } else { /* regular keys */ kbd->kb_lastact[keycode] = NOP; *shiftstate = state; if (*accents > 0) { /* make an accented char */ action = make_accent_char(kbd, action, accents); if (action == ERRKEY) return (action); } if (state & METAS) action |= MKEY; return (action); } } /* NOT REACHED */ } Index: head/sys/dev/kbd/kbdreg.h =================================================================== --- head/sys/dev/kbd/kbdreg.h (revision 174983) +++ head/sys/dev/kbd/kbdreg.h (revision 174984) @@ -1,259 +1,303 @@ /*- * 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. * * $FreeBSD$ */ #ifndef _DEV_KBD_KBDREG_H_ #define _DEV_KBD_KBDREG_H_ /* forward declarations */ typedef struct keyboard keyboard_t; struct keymap; struct accentmap; struct fkeytab; struct cdevsw; /* call back funcion */ typedef int kbd_callback_func_t(keyboard_t *kbd, int event, void *arg); /* event types */ #define KBDIO_KEYINPUT 0 #define KBDIO_UNLOADING 1 typedef struct keyboard_callback { kbd_callback_func_t *kc_func; void *kc_arg; } keyboard_callback_t; /* keyboard */ struct keyboard { /* the following fields are managed by kbdio */ int kb_index; /* kbdio index# */ int kb_minor; /* minor number of the sub-device */ int kb_flags; /* internal flags */ #define KB_VALID (1 << 16) /* this entry is valid */ #define KB_NO_DEVICE (1 << 17) /* device not present */ #define KB_PROBED (1 << 18) /* device probed */ #define KB_INITIALIZED (1 << 19) /* device initialized */ #define KB_REGISTERED (1 << 20) /* device registered to kbdio */ #define KB_BUSY (1 << 21) /* device used by a client */ int kb_active; /* 0: inactive */ void *kb_token; /* id of the current client */ keyboard_callback_t kb_callback;/* callback function */ /* * Device configuration flags: * The upper 16 bits are common between various keyboard devices. * The lower 16 bits are device-specific. */ int kb_config; #define KB_CONF_PROBE_ONLY (1 << 16) /* probe only, don't initialize */ /* the following fields are set up by the driver */ char *kb_name; /* driver name */ int kb_unit; /* unit # */ int kb_type; /* KB_84, KB_101, KB_OTHER,... */ int kb_io_base; /* port# if any */ int kb_io_size; /* # of occupied port */ int kb_led; /* LED status */ struct keymap *kb_keymap; /* key map */ struct accentmap *kb_accentmap; /* accent map */ struct fkeytab *kb_fkeytab; /* function key strings */ int kb_fkeytab_size;/* # of function key strings */ void *kb_data; /* the driver's private data */ int kb_delay1; int kb_delay2; #define KB_DELAY1 500 #define KB_DELAY2 100 unsigned long kb_count; /* # of processed key strokes */ u_char kb_lastact[NUM_KEYS/2]; struct cdev *kb_dev; }; #define KBD_IS_VALID(k) ((k)->kb_flags & KB_VALID) #define KBD_VALID(k) ((k)->kb_flags |= KB_VALID) #define KBD_INVALID(k) ((k)->kb_flags &= ~KB_VALID) #define KBD_HAS_DEVICE(k) (!((k)->kb_flags & KB_NO_DEVICE)) #define KBD_FOUND_DEVICE(k) ((k)->kb_flags &= ~KB_NO_DEVICE) #define KBD_IS_PROBED(k) ((k)->kb_flags & KB_PROBED) #define KBD_PROBE_DONE(k) ((k)->kb_flags |= KB_PROBED) #define KBD_IS_INITIALIZED(k) ((k)->kb_flags & KB_INITIALIZED) #define KBD_INIT_DONE(k) ((k)->kb_flags |= KB_INITIALIZED) #define KBD_IS_CONFIGURED(k) ((k)->kb_flags & KB_REGISTERED) #define KBD_CONFIG_DONE(k) ((k)->kb_flags |= KB_REGISTERED) #define KBD_IS_BUSY(k) ((k)->kb_flags & KB_BUSY) #define KBD_BUSY(k) ((k)->kb_flags |= KB_BUSY) #define KBD_UNBUSY(k) ((k)->kb_flags &= ~KB_BUSY) #define KBD_IS_ACTIVE(k) ((k)->kb_active) #define KBD_ACTIVATE(k) (++(k)->kb_active) #define KBD_DEACTIVATE(k) (--(k)->kb_active) #define KBD_LED_VAL(k) ((k)->kb_led) /* keyboard function table */ typedef int kbd_probe_t(int unit, void *arg, int flags); typedef int kbd_init_t(int unit, keyboard_t **kbdp, void *arg, int flags); typedef int kbd_term_t(keyboard_t *kbd); typedef int kbd_intr_t(keyboard_t *kbd, void *arg); typedef int kbd_test_if_t(keyboard_t *kbd); typedef int kbd_enable_t(keyboard_t *kbd); typedef int kbd_disable_t(keyboard_t *kbd); typedef int kbd_read_t(keyboard_t *kbd, int wait); typedef int kbd_check_t(keyboard_t *kbd); typedef u_int kbd_read_char_t(keyboard_t *kbd, int wait); typedef int kbd_check_char_t(keyboard_t *kbd); typedef int kbd_ioctl_t(keyboard_t *kbd, u_long cmd, caddr_t data); typedef int kbd_lock_t(keyboard_t *kbd, int lock); typedef void kbd_clear_state_t(keyboard_t *kbd); typedef int kbd_get_state_t(keyboard_t *kbd, void *buf, size_t len); typedef int kbd_set_state_t(keyboard_t *kbd, void *buf, size_t len); typedef u_char *kbd_get_fkeystr_t(keyboard_t *kbd, int fkey, size_t *len); typedef int kbd_poll_mode_t(keyboard_t *kbd, int on); typedef void kbd_diag_t(keyboard_t *kbd, int level); typedef struct keyboard_switch { kbd_probe_t *probe; kbd_init_t *init; kbd_term_t *term; kbd_intr_t *intr; kbd_test_if_t *test_if; kbd_enable_t *enable; kbd_disable_t *disable; kbd_read_t *read; kbd_check_t *check; kbd_read_char_t *read_char; kbd_check_char_t *check_char; kbd_ioctl_t *ioctl; kbd_lock_t *lock; kbd_clear_state_t *clear_state; kbd_get_state_t *get_state; kbd_set_state_t *set_state; kbd_get_fkeystr_t *get_fkeystr; kbd_poll_mode_t *poll; kbd_diag_t *diag; } keyboard_switch_t; +/* + * Keyboard disciplines: call actual handlers via kbdsw[]. + */ +#define kbdd_probe(kbd, unit, arg, flags) \ + (*kbdsw[(kbd)->kb_index]->probe)((unit), (arg), (flags)) +#define kbdd_init(kbd, unit, kbdpp, arg, flags) \ + (*kbdsw[(kbd)->kb_index]->init)((unit), (kbdpp), (arg), (flags)) +#define kbdd_term(kbd) \ + (*kbdsw[(kbd)->kb_index]->term)((kbd)) +#define kbdd_intr(kbd, arg) \ + (*kbdsw[(kbd)->kb_index]->intr)((kbd), (arg)) +#define kbdd_test_if(kbd) \ + (*kbdsw[(kbd)->kb_index]->test_if)((kbd)) +#define kbdd_enable(kbd) \ + (*kbdsw[(kbd)->kb_index]->enable)((kbd)) +#define kbdd_disable(kbd) \ + (*kbdsw[(kbd)->kb_index]->disable)((kbd)) +#define kbdd_read(kbd, wait) \ + (*kbdsw[(kbd)->kb_index]->read)((kbd), (wait)) +#define kbdd_check(kbd) \ + (*kbdsw[(kbd)->kb_index]->check)((kbd)) +#define kbdd_read_char(kbd, wait) \ + (*kbdsw[(kbd)->kb_index]->read_char)((kbd), (wait)) +#define kbdd_check_char(kbd) \ + (*kbdsw[(kbd)->kb_index]->check_char)((kbd)) +#define kbdd_ioctl(kbd, cmd, arg) \ + (((kbd) == NULL) ? \ + ENODEV : \ + (*kbdsw[(kbd)->kb_index]->ioctl)((kbd), (cmd), (arg))) +#define kbdd_lock(kbd, lockf) \ + (*kbdsw[(kbd)->kb_index]->lock)((kbd), (lockf)) +#define kbdd_clear_state(kbd) \ + (*kbdsw[(kbd)->kb_index]->clear_state)((kbd)) +#define kbdd_get_state(kbd, buf, len) \ + (*kbdsw[(kbd)->kb_index]->get_state)((kbd), (buf), (len)) +#define kbdd_set_state(kbd, buf, len) \ + (*kbdsw[(kbd)->kb_index]->set_state)((kbd), (buf), (len)) +#define kbdd_get_fkeystr(kbd, fkey, len) \ + (*kbdsw[(kbd)->kb_index]->get_fkeystr)((kbd), (fkey), (len)) +#define kbdd_poll(kbd, on) \ + (*kbdsw[(kbd)->kb_index]->poll)((kbd), (on)) +#define kbdd_diag(kbd, level) \ + (*kbdsw[(kbd)->kb_index]->diag)((kbd), (leve)) + /* keyboard driver */ typedef struct keyboard_driver { SLIST_ENTRY(keyboard_driver) link; char *name; keyboard_switch_t *kbdsw; int (*configure)(int); /* backdoor for the console driver */ } keyboard_driver_t; #ifdef _KERNEL #define KEYBOARD_DRIVER(name, sw, config) \ static struct keyboard_driver name##_kbd_driver = { \ { NULL }, #name, &sw, config \ }; \ DATA_SET(kbddriver_set, name##_kbd_driver); /* global variables */ extern keyboard_switch_t **kbdsw; /* functions for the keyboard driver */ int kbd_add_driver(keyboard_driver_t *driver); int kbd_delete_driver(keyboard_driver_t *driver); int kbd_register(keyboard_t *kbd); int kbd_unregister(keyboard_t *kbd); keyboard_switch_t *kbd_get_switch(char *driver); void kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config, int port, int port_size); void kbd_set_maps(keyboard_t *kbd, struct keymap *keymap, struct accentmap *accmap, struct fkeytab *fkeymap, int fkeymap_size); /* functions for the keyboard client */ int kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func, void *arg); int kbd_release(keyboard_t *kbd, void *id); int kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func, void *arg); int kbd_find_keyboard(char *driver, int unit); int kbd_find_keyboard2(char *driver, int unit, int index); keyboard_t *kbd_get_keyboard(int index); /* a back door for the console driver to tickle the keyboard driver XXX */ int kbd_configure(int flags); /* see `kb_config' above for flag bit definitions */ #ifdef KBD_INSTALL_CDEV /* virtual keyboard cdev driver functions */ int kbd_attach(keyboard_t *kbd); int kbd_detach(keyboard_t *kbd); #endif /* KBD_INSTALL_CDEV */ /* generic low-level keyboard functions */ /* shift key state */ #define SHIFTS1 (1 << 16) #define SHIFTS2 (1 << 17) #define SHIFTS (SHIFTS1 | SHIFTS2) #define CTLS1 (1 << 18) #define CTLS2 (1 << 19) #define CTLS (CTLS1 | CTLS2) #define ALTS1 (1 << 20) #define ALTS2 (1 << 21) #define ALTS (ALTS1 | ALTS2) #define AGRS1 (1 << 22) #define AGRS2 (1 << 23) #define AGRS (AGRS1 | AGRS2) #define METAS1 (1 << 24) #define METAS2 (1 << 25) #define METAS (METAS1 | METAS2) #define NLKDOWN (1 << 26) #define SLKDOWN (1 << 27) #define CLKDOWN (1 << 28) #define ALKDOWN (1 << 29) #define SHIFTAON (1 << 30) /* lock key state (defined in sys/kbio.h) */ /* #define CLKED LED_CAP #define NLKED LED_NUM #define SLKED LED_SCR #define ALKED (1 << 3) #define LOCK_MASK (CLKED | NLKED | SLKED | ALKED) #define LED_CAP (1 << 0) #define LED_NUM (1 << 1) #define LED_SCR (1 << 2) #define LED_MASK (LED_CAP | LED_NUM | LED_SCR) */ kbd_get_fkeystr_t genkbd_get_fkeystr; kbd_diag_t genkbd_diag; int genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg); int genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate, int *accents); #endif /* _KERNEL */ #endif /* !_DEV_KBD_KBDREG_H_ */ Index: head/sys/dev/kbdmux/kbdmux.c =================================================================== --- head/sys/dev/kbdmux/kbdmux.c (revision 174983) +++ head/sys/dev/kbdmux/kbdmux.c (revision 174984) @@ -1,1391 +1,1370 @@ /* * kbdmux.c */ /*- * 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_compat.h" #include "opt_kbd.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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)) #define KBDMUX_SLEEP(s, f, d, t) \ msleep(&(s)->f, &(s)->ks_lock, PCATCH | (PZERO + 1), (d), (t)) #define KBDMUX_CALLOUT_INIT(s) \ callout_init_mtx(&(s)->ks_timo, &(s)->ks_lock, 0) #define KBDMUX_QUEUE_INTR(s) \ taskqueue_enqueue(taskqueue_swi_giant, &(s)->ks_task) #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) #define KBDMUX_SLEEP(s, f, d, t) \ tsleep(&(s)->f, PCATCH | (PZERO + 1), (d), (t)) #define KBDMUX_CALLOUT_INIT(s) \ callout_init(&(s)->ks_timo, 0) #define KBDMUX_QUEUE_INTR(s) \ taskqueue_enqueue(taskqueue_swi_giant, &(s)->ks_task) #endif /* not yet */ -#define KBDMUX_INTR(kbd, arg) \ - (*kbdsw[(kbd)->kb_index]->intr)((kbd), (arg)) - -#define KBDMUX_IOCTL(kbd, cmd, arg) \ - (*kbdsw[(kbd)->kb_index]->ioctl)((kbd), (cmd), (caddr_t) (arg)) - -#define KBDMUX_CHECK_CHAR(kbd) \ - (*kbdsw[(kbd)->kb_index]->check_char)((kbd)) - -#define KBDMUX_READ_CHAR(kbd, wait) \ - (*kbdsw[(kbd)->kb_index]->read_char)((kbd), (wait)) - -#define KBDMUX_ENABLE(kbd) \ - (*kbdsw[(kbd)->kb_index]->enable)((kbd)) - -#define KBDMUX_POLL(kbd, on) \ - (*kbdsw[(kbd)->kb_index]->poll)((kbd), (on)) - -#define KBDMUX_CLEAR_STATE(kbd) \ - (*kbdsw[(kbd)->kb_index]->clear_state)((kbd)) - /* * 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 { struct clist ks_inq; /* input chars queue */ 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 */ #define POLLING (1 << 1) /* polling */ #define TASK (1 << 2) /* interrupt task queued */ 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 */ 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 timeout_t kbdmux_kbd_intr_timo; static kbd_callback_func_t kbdmux_kbd_event; /* * Interrupt handler task */ void kbdmux_kbd_intr(void *xkbd, int pending) { keyboard_t *kbd = (keyboard_t *) xkbd; kbdmux_state_t *state = (kbdmux_state_t *) kbd->kb_data; - KBDMUX_INTR(kbd, NULL); + kbdd_intr(kbd, NULL); KBDMUX_LOCK(state); state->ks_flags &= ~TASK; wakeup(&state->ks_task); KBDMUX_UNLOCK(state); } /* * Schedule interrupt handler on timeout. Called with locked state. */ void kbdmux_kbd_intr_timo(void *xstate) { kbdmux_state_t *state = (kbdmux_state_t *) xstate; KBDMUX_LOCK_ASSERT(state, MA_OWNED); if (callout_pending(&state->ks_timo)) return; /* callout was reset */ if (!callout_active(&state->ks_timo)) return; /* callout was stopped */ callout_deactivate(&state->ks_timo); /* queue interrupt task if needed */ if (state->ks_inq.c_cc > 0 && !(state->ks_flags & TASK) && KBDMUX_QUEUE_INTR(state) == 0) state->ks_flags |= TASK; /* re-schedule timeout */ callout_reset(&state->ks_timo, TICKS, kbdmux_kbd_intr_timo, state); } /* * 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 (KBDMUX_CHECK_CHAR(kbd)) { - c = KBDMUX_READ_CHAR(kbd, 0); + 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 */ putc(c, &state->ks_inq); } /* queue interrupt task if needed */ if (state->ks_inq.c_cc > 0 && !(state->ks_flags & TASK) && KBDMUX_QUEUE_INTR(state) == 0) state->ks_flags |= 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, .get_fkeystr = genkbd_get_fkeystr, .poll = kbdmux_poll, .diag = genkbd_diag, }; /* * 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]; 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); clist_alloc_cblocks(&state->ks_inq, KBDMUX_Q_SIZE, KBDMUX_Q_SIZE / 2); TASK_INIT(&state->ks_task, 0, kbdmux_kbd_intr, (void *) kbd); KBDMUX_CALLOUT_INIT(state); 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); KBD_INIT_DONE(kbd); } if (!KBD_IS_CONFIGURED(kbd)) { if (kbd_register(kbd) < 0) { error = ENXIO; goto bad; } KBD_CONFIG_DONE(kbd); KBDMUX_LOCK(state); callout_reset(&state->ks_timo, TICKS, kbdmux_kbd_intr_timo, state); KBDMUX_UNLOCK(state); } return (0); bad: if (needfree) { if (state != NULL) { clist_free_cblocks(&state->ks_inq); 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; KBDMUX_LOCK(state); /* kill callout */ callout_stop(&state->ks_timo); /* wait for interrupt task */ while (state->ks_flags & TASK) KBDMUX_SLEEP(state, ks_task, "kbdmuxc", 0); /* release all keyboards from the mux */ 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); } /* flush input queue */ ndflush(&state->ks_inq, state->ks_inq.c_cc); clist_free_cblocks(&state->ks_inq); KBDMUX_UNLOCK(state); kbd_unregister(kbd); 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 = getc(&state->ks_inq); 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.c_cc > 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 = getc(&state->ks_inq); if (scancode == -1) { if (state->ks_flags & POLLING) { kbdmux_kbd_t *k; SLIST_FOREACH(k, &state->ks_kbds, next) { - while (KBDMUX_CHECK_CHAR(k->kbd)) { - scancode = KBDMUX_READ_CHAR(k->kbd, 0); + 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; putc(scancode, &state->ks_inq); } } if (state->ks_inq.c_cc > 0) goto next_code; } KBDMUX_UNLOCK(state); return (NOKEY); } /* XXX FIXME: check for -1 if wait == 1! */ kbd->kb_count ++; /* 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.c_cc > 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 */ } - KBDMUX_ENABLE(k->kbd); - KBDMUX_CLEAR_STATE(k->kbd); + kbdd_enable(k->kbd); + kbdd_clear_state(k->kbd); /* set K_RAW mode on slave keyboard */ mode = K_RAW; - error = KBDMUX_IOCTL(k->kbd, KDSKBMODE, &mode); + 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 = KBDMUX_IOCTL(k->kbd, KDSKBSTATE, &mode); + 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; /* KDSETLED on all slave keyboards */ SLIST_FOREACH(k, &state->ks_kbds, next) - KBDMUX_IOCTL(k->kbd, KDSETLED, arg); + 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) - KBDMUX_IOCTL(k->kbd, KDSKBSTATE, arg); + 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]; /* perform command on all slave keyboards */ SLIST_FOREACH(k, &state->ks_kbds, next) - KBDMUX_IOCTL(k->kbd, cmd, arg); + kbdd_ioctl(k->kbd, cmd, arg); KBDMUX_UNLOCK(state); break; case PIO_KEYMAP: /* set keyboard translation table */ 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) - KBDMUX_IOCTL(k->kbd, cmd, arg); + 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|POLLING); state->ks_state &= LOCK_MASK; /* preserve locking key state */ state->ks_accents = 0; state->ks_composed_char = 0; /* state->ks_prefix = 0; XXX */ ndflush(&state->ks_inq, state->ks_inq.c_cc); } 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_flags |= POLLING; else state->ks_flags &= ~POLLING; /* set poll on slave keyboards */ SLIST_FOREACH(k, &state->ks_kbds, next) - KBDMUX_POLL(k->kbd, on); + kbdd_poll(k->kbd, on); KBDMUX_UNLOCK(state); return (0); } /***************************************************************************** ***************************************************************************** ** 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) { kbd_delete_driver(&kbdmux_kbd_driver); error = ENXIO; break; } kbd = NULL; if ((error = (*sw->probe)(0, NULL, 0)) != 0 || (error = (*sw->init)(0, &kbd, NULL, 0)) != 0) { kbd_delete_driver(&kbdmux_kbd_driver); break; } #ifdef KBD_INSTALL_CDEV if ((error = kbd_attach(kbd)) != 0) { (*sw->term)(kbd); kbd_delete_driver(&kbdmux_kbd_driver); break; } #endif if ((error = (*sw->enable)(kbd)) != 0) { (*sw->disable)(kbd); #ifdef KBD_INSTALL_CDEV kbd_detach(kbd); #endif (*sw->term)(kbd); kbd_delete_driver(&kbdmux_kbd_driver); break; } break; case MOD_UNLOAD: if ((sw = kbd_get_switch(KEYBOARD_NAME)) == NULL) panic("kbd_get_switch(" KEYBOARD_NAME ") == NULL"); kbd = kbd_get_keyboard(kbd_find_keyboard(KEYBOARD_NAME, 0)); if (kbd == NULL) panic("kbd_get_keyboard(kbd_find_keyboard(" KEYBOARD_NAME ", 0)) == 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 (0); } DEV_MODULE(kbdmux, kbdmux_modevent, NULL); Index: head/sys/dev/syscons/syscons.c =================================================================== --- head/sys/dev/syscons/syscons.c (revision 174983) +++ head/sys/dev/syscons/syscons.c (revision 174984) @@ -1,3702 +1,3702 @@ /*- * Copyright (c) 1992-1998 Søren Schmidt * All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Sascha Wildner * * 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, * without modification, immediately at the beginning of the file. * 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. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * 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_compat.h" #include "opt_syscons.h" #include "opt_splash.h" #include "opt_ddb.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(__sparc64__) || defined(__powerpc__) #include #else #include #endif #if defined( __i386__) || defined(__amd64__) #include #include #endif #include #include #include #include #define COLD 0 #define WARM 1 #define DEFAULT_BLANKTIME (5*60) /* 5 minutes */ #define MAX_BLANKTIME (7*24*60*60) /* 7 days!? */ #define KEYCODE_BS 0x0e /* "<-- Backspace" key, XXX */ typedef struct default_attr { int std_color; /* normal hardware color */ int rev_color; /* reverse hardware color */ } default_attr; static default_attr user_default = { SC_NORM_ATTR, SC_NORM_REV_ATTR, }; static default_attr kernel_default = { SC_KERNEL_CONS_ATTR, SC_KERNEL_CONS_REV_ATTR, }; static int sc_console_unit = -1; static int sc_saver_keyb_only = 1; static scr_stat *sc_console; static struct tty *sc_console_tty; static struct consdev *sc_consptr; static void *kernel_console_ts; static scr_stat main_console; static struct cdev *main_devs[MAXCONS]; static char init_done = COLD; static char shutdown_in_progress = FALSE; static char sc_malloc = FALSE; static int saver_mode = CONS_NO_SAVER; /* LKM/user saver */ static int run_scrn_saver = FALSE; /* should run the saver? */ static int enable_bell = TRUE; /* enable beeper */ #ifndef SC_DISABLE_REBOOT static int enable_reboot = TRUE; /* enable keyboard reboot */ #endif #ifndef SC_DISABLE_KDBKEY static int enable_kdbkey = TRUE; /* enable keyboard debug */ #endif static long scrn_blank_time = 0; /* screen saver timeout value */ #ifdef DEV_SPLASH static int scrn_blanked; /* # of blanked screen */ static int sticky_splash = FALSE; static void none_saver(sc_softc_t *sc, int blank) { } static void (*current_saver)(sc_softc_t *, int) = none_saver; #endif SYSCTL_NODE(_hw, OID_AUTO, syscons, CTLFLAG_RD, 0, "syscons"); SYSCTL_NODE(_hw_syscons, OID_AUTO, saver, CTLFLAG_RD, 0, "saver"); SYSCTL_INT(_hw_syscons_saver, OID_AUTO, keybonly, CTLFLAG_RW, &sc_saver_keyb_only, 0, "screen saver interrupted by input only"); SYSCTL_INT(_hw_syscons, OID_AUTO, bell, CTLFLAG_RW, &enable_bell, 0, "enable bell"); #ifndef SC_DISABLE_REBOOT SYSCTL_INT(_hw_syscons, OID_AUTO, kbd_reboot, CTLFLAG_RW|CTLFLAG_SECURE, &enable_reboot, 0, "enable keyboard reboot"); #endif #ifndef SC_DISABLE_KDBKEY SYSCTL_INT(_hw_syscons, OID_AUTO, kbd_debug, CTLFLAG_RW|CTLFLAG_SECURE, &enable_kdbkey, 0, "enable keyboard debug"); #endif #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT) #include "font.h" #endif d_ioctl_t *sc_user_ioctl; static bios_values_t bios_value; static int enable_panic_key; SYSCTL_INT(_machdep, OID_AUTO, enable_panic_key, CTLFLAG_RW, &enable_panic_key, 0, "Enable panic via keypress specified in kbdmap(5)"); #define SC_CONSOLECTL 255 #define VIRTUAL_TTY(sc, x) (SC_DEV((sc), (x)) != NULL ? \ SC_DEV((sc), (x))->si_tty : NULL) #define ISTTYOPEN(tp) ((tp) && ((tp)->t_state & TS_ISOPEN)) static int debugger; /* prototypes */ static int sc_allocate_keyboard(sc_softc_t *sc, int unit); static struct tty *sc_alloc_tty(struct cdev *dev); static int scvidprobe(int unit, int flags, int cons); static int sckbdprobe(int unit, int flags, int cons); static void scmeminit(void *arg); static int scdevtounit(struct cdev *dev); static kbd_callback_func_t sckbdevent; static int scparam(struct tty *tp, struct termios *t); static void scstart(struct tty *tp); static void scinit(int unit, int flags); static scr_stat *sc_get_stat(struct cdev *devptr); static void scterm(int unit, int flags); static void scshutdown(void *arg, int howto); static u_int scgetc(sc_softc_t *sc, u_int flags); #define SCGETC_CN 1 #define SCGETC_NONBLOCK 2 static int sccngetch(int flags); static void sccnupdate(scr_stat *scp); static scr_stat *alloc_scp(sc_softc_t *sc, int vty); static void init_scp(sc_softc_t *sc, int vty, scr_stat *scp); static timeout_t scrn_timer; static int and_region(int *s1, int *e1, int s2, int e2); static void scrn_update(scr_stat *scp, int show_cursor); #ifdef DEV_SPLASH static int scsplash_callback(int event, void *arg); static void scsplash_saver(sc_softc_t *sc, int show); static int add_scrn_saver(void (*this_saver)(sc_softc_t *, int)); static int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int)); static int set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border); static int restore_scrn_saver_mode(scr_stat *scp, int changemode); static void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int)); static int wait_scrn_saver_stop(sc_softc_t *sc); #define scsplash_stick(stick) (sticky_splash = (stick)) #else /* !DEV_SPLASH */ #define scsplash_stick(stick) #endif /* DEV_SPLASH */ static int do_switch_scr(sc_softc_t *sc, int s); static int vt_proc_alive(scr_stat *scp); static int signal_vt_rel(scr_stat *scp); static int signal_vt_acq(scr_stat *scp); static int finish_vt_rel(scr_stat *scp, int release, int *s); static int finish_vt_acq(scr_stat *scp); static void exchange_scr(sc_softc_t *sc); static void update_cursor_image(scr_stat *scp); static void change_cursor_shape(scr_stat *scp, int flags, int base, int height); static int save_kbd_state(scr_stat *scp); static int update_kbd_state(scr_stat *scp, int state, int mask); static int update_kbd_leds(scr_stat *scp, int which); static timeout_t blink_screen; static cn_probe_t sc_cnprobe; static cn_init_t sc_cninit; static cn_term_t sc_cnterm; static cn_getc_t sc_cngetc; static cn_putc_t sc_cnputc; CONSOLE_DRIVER(sc); static d_open_t scopen; static d_close_t scclose; static d_read_t scread; static d_ioctl_t scioctl; static d_mmap_t scmmap; static struct cdevsw sc_cdevsw = { .d_version = D_VERSION, .d_open = scopen, .d_close = scclose, .d_read = scread, .d_ioctl = scioctl, .d_mmap = scmmap, .d_name = "sc", .d_flags = D_TTY | D_NEEDGIANT, }; int sc_probe_unit(int unit, int flags) { if (!scvidprobe(unit, flags, FALSE)) { if (bootverbose) printf("%s%d: no video adapter found.\n", SC_DRIVER_NAME, unit); return ENXIO; } /* syscons will be attached even when there is no keyboard */ sckbdprobe(unit, flags, FALSE); return 0; } /* probe video adapters, return TRUE if found */ static int scvidprobe(int unit, int flags, int cons) { /* * Access the video adapter driver through the back door! * Video adapter drivers need to be configured before syscons. * However, when syscons is being probed as the low-level console, * they have not been initialized yet. We force them to initialize * themselves here. XXX */ vid_configure(cons ? VIO_PROBE_ONLY : 0); return (vid_find_adapter("*", unit) >= 0); } /* probe the keyboard, return TRUE if found */ static int sckbdprobe(int unit, int flags, int cons) { /* access the keyboard driver through the backdoor! */ kbd_configure(cons ? KB_CONF_PROBE_ONLY : 0); return (kbd_find_keyboard("*", unit) >= 0); } static char *adapter_name(video_adapter_t *adp) { static struct { int type; char *name[2]; } names[] = { { KD_MONO, { "MDA", "MDA" } }, { KD_HERCULES, { "Hercules", "Hercules" } }, { KD_CGA, { "CGA", "CGA" } }, { KD_EGA, { "EGA", "EGA (mono)" } }, { KD_VGA, { "VGA", "VGA (mono)" } }, { KD_PC98, { "PC-98x1", "PC-98x1" } }, { KD_TGA, { "TGA", "TGA" } }, { -1, { "Unknown", "Unknown" } }, }; int i; for (i = 0; names[i].type != -1; ++i) if (names[i].type == adp->va_type) break; return names[i].name[(adp->va_flags & V_ADP_COLOR) ? 0 : 1]; } static struct tty * sc_alloc_tty(struct cdev *dev) { struct tty *tp; tp = dev->si_tty = ttyalloc(); ttyinitmode(tp, 1, 0); tp->t_oproc = scstart; tp->t_param = scparam; tp->t_stop = nottystop; tp->t_dev = dev; return (tp); } int sc_attach_unit(int unit, int flags) { sc_softc_t *sc; scr_stat *scp; #ifdef SC_PIXEL_MODE video_info_t info; #endif int vc; struct cdev *dev; flags &= ~SC_KERNEL_CONSOLE; if (sc_console_unit == unit) { /* * If this unit is being used as the system console, we need to * adjust some variables and buffers before and after scinit(). */ /* assert(sc_console != NULL) */ flags |= SC_KERNEL_CONSOLE; scmeminit(NULL); scinit(unit, flags); if (sc_console->tsw->te_size > 0) { /* assert(sc_console->ts != NULL); */ kernel_console_ts = sc_console->ts; sc_console->ts = malloc(sc_console->tsw->te_size, M_DEVBUF, M_WAITOK); bcopy(kernel_console_ts, sc_console->ts, sc_console->tsw->te_size); (*sc_console->tsw->te_default_attr)(sc_console, user_default.std_color, user_default.rev_color); } } else { scinit(unit, flags); } sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE); sc->config = flags; scp = sc_get_stat(sc->dev[0]); if (sc_console == NULL) /* sc_console_unit < 0 */ sc_console = scp; #ifdef SC_PIXEL_MODE if ((sc->config & SC_VESA800X600) && ((*vidsw[sc->adapter]->get_info)(sc->adp, M_VESA_800x600, &info) == 0)) { #ifdef DEV_SPLASH if (sc->flags & SC_SPLASH_SCRN) splash_term(sc->adp); #endif sc_set_graphics_mode(scp, NULL, M_VESA_800x600); sc_set_pixel_mode(scp, NULL, COL, ROW, 16, 8); sc->initial_mode = M_VESA_800x600; #ifdef DEV_SPLASH /* put up the splash again! */ if (sc->flags & SC_SPLASH_SCRN) splash_init(sc->adp, scsplash_callback, sc); #endif } #endif /* SC_PIXEL_MODE */ /* initialize cursor */ if (!ISGRAPHSC(scp)) update_cursor_image(scp); /* get screen update going */ scrn_timer(sc); /* set up the keyboard */ - kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode); + kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode); update_kbd_state(scp, scp->status, LOCK_MASK); printf("%s%d: %s <%d virtual consoles, flags=0x%x>\n", SC_DRIVER_NAME, unit, adapter_name(sc->adp), sc->vtys, sc->config); if (bootverbose) { printf("%s%d:", SC_DRIVER_NAME, unit); if (sc->adapter >= 0) printf(" fb%d", sc->adapter); if (sc->keyboard >= 0) printf(", kbd%d", sc->keyboard); if (scp->tsw) printf(", terminal emulator: %s (%s)", scp->tsw->te_name, scp->tsw->te_desc); printf("\n"); } /* register a shutdown callback for the kernel console */ if (sc_console_unit == unit) EVENTHANDLER_REGISTER(shutdown_pre_sync, scshutdown, (void *)(uintptr_t)unit, SHUTDOWN_PRI_DEFAULT); for (vc = 0; vc < sc->vtys; vc++) { if (sc->dev[vc] == NULL) { sc->dev[vc] = make_dev(&sc_cdevsw, vc + unit * MAXCONS, UID_ROOT, GID_WHEEL, 0600, "ttyv%r", vc + unit * MAXCONS); sc_alloc_tty(sc->dev[vc]); if (vc == 0 && sc->dev == main_devs) SC_STAT(sc->dev[0]) = &main_console; } /* * The first vty already has struct tty and scr_stat initialized * in scinit(). The other vtys will have these structs when * first opened. */ } dev = make_dev(&sc_cdevsw, SC_CONSOLECTL, UID_ROOT, GID_WHEEL, 0600, "consolectl"); sc_console_tty = sc_alloc_tty(dev); ttyconsolemode(sc_console_tty, 0); SC_STAT(dev) = sc_console; return 0; } static void scmeminit(void *arg) { if (sc_malloc) return; sc_malloc = TRUE; /* * As soon as malloc() becomes functional, we had better allocate * various buffers for the kernel console. */ if (sc_console_unit < 0) /* sc_console == NULL */ return; /* copy the temporary buffer to the final buffer */ sc_alloc_scr_buffer(sc_console, FALSE, FALSE); #ifndef SC_NO_CUTPASTE sc_alloc_cut_buffer(sc_console, FALSE); #endif #ifndef SC_NO_HISTORY /* initialize history buffer & pointers */ sc_alloc_history_buffer(sc_console, 0, 0, FALSE); #endif } /* XXX */ SYSINIT(sc_mem, SI_SUB_KMEM, SI_ORDER_ANY, scmeminit, NULL); static int scdevtounit(struct cdev *dev) { int vty = SC_VTY(dev); if (vty == SC_CONSOLECTL) return ((sc_console != NULL) ? sc_console->sc->unit : -1); else if ((vty < 0) || (vty >= MAXCONS*sc_max_unit())) return -1; else return vty/MAXCONS; } static int scopen(struct cdev *dev, int flag, int mode, struct thread *td) { int unit = scdevtounit(dev); sc_softc_t *sc; struct tty *tp; scr_stat *scp; #ifndef __sparc64__ keyarg_t key; #endif int error; DPRINTF(5, ("scopen: dev:%s, unit:%d, vty:%d\n", devtoname(dev), unit, SC_VTY(dev))); tp = dev->si_tty; sc = sc_get_softc(unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0); if (sc == NULL) return ENXIO; if (!ISTTYOPEN(tp)) { tp->t_termios = tp->t_init_in; /* Use the current setting of the <-- key as default VERASE. */ /* If the Delete key is preferable, an stty is necessary */ #ifndef __sparc64__ if (sc->kbd != NULL) { key.keynum = KEYCODE_BS; - kbd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key); + kbdd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key); tp->t_cc[VERASE] = key.key.map[0]; } #endif scparam(tp, &tp->t_termios); ttyld_modem(tp, 1); } else if (tp->t_state & TS_XCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE)) return(EBUSY); error = ttyld_open(tp, dev); scp = sc_get_stat(dev); if (scp == NULL) { scp = SC_STAT(dev) = alloc_scp(sc, SC_VTY(dev)); if (ISGRAPHSC(scp)) sc_set_pixel_mode(scp, NULL, COL, ROW, 16, 8); } if (!tp->t_winsize.ws_col && !tp->t_winsize.ws_row) { tp->t_winsize.ws_col = scp->xsize; tp->t_winsize.ws_row = scp->ysize; } return error; } static int scclose(struct cdev *dev, int flag, int mode, struct thread *td) { struct tty *tp = dev->si_tty; scr_stat *scp; int s; if (SC_VTY(dev) != SC_CONSOLECTL) { scp = sc_get_stat(tp->t_dev); /* were we in the middle of the VT switching process? */ DPRINTF(5, ("sc%d: scclose(), ", scp->sc->unit)); s = spltty(); if ((scp == scp->sc->cur_scp) && (scp->sc->unit == sc_console_unit)) cnavailable(sc_consptr, TRUE); if (finish_vt_rel(scp, TRUE, &s) == 0) /* force release */ DPRINTF(5, ("reset WAIT_REL, ")); if (finish_vt_acq(scp) == 0) /* force acknowledge */ DPRINTF(5, ("reset WAIT_ACQ, ")); #ifdef not_yet_done if (scp == &main_console) { scp->pid = 0; scp->proc = NULL; scp->smode.mode = VT_AUTO; } else { sc_vtb_destroy(&scp->vtb); #ifndef __sparc64__ sc_vtb_destroy(&scp->scr); #endif sc_free_history_buffer(scp, scp->ysize); SC_STAT(dev) = NULL; free(scp, M_DEVBUF); } #else scp->pid = 0; scp->proc = NULL; scp->smode.mode = VT_AUTO; #endif scp->kbd_mode = K_XLATE; if (scp == scp->sc->cur_scp) - kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode); + kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode); DPRINTF(5, ("done.\n")); } spltty(); ttyld_close(tp, flag); tty_close(tp); spl0(); return(0); } static int scread(struct cdev *dev, struct uio *uio, int flag) { if (!sc_saver_keyb_only) sc_touch_scrn_saver(); return ttyread(dev, uio, flag); } static int sckbdevent(keyboard_t *thiskbd, int event, void *arg) { sc_softc_t *sc; struct tty *cur_tty; int c; size_t len; u_char *cp; sc = (sc_softc_t *)arg; /* assert(thiskbd == sc->kbd) */ switch (event) { case KBDIO_KEYINPUT: break; case KBDIO_UNLOADING: sc->kbd = NULL; sc->keyboard = -1; kbd_release(thiskbd, (void *)&sc->keyboard); return 0; default: return EINVAL; } /* * Loop while there is still input to get from the keyboard. * I don't think this is nessesary, and it doesn't fix * the Xaccel-2.1 keyboard hang, but it can't hurt. XXX */ while ((c = scgetc(sc, SCGETC_NONBLOCK)) != NOKEY) { cur_tty = VIRTUAL_TTY(sc, sc->cur_scp->index); if (!ISTTYOPEN(cur_tty)) { cur_tty = sc_console_tty; if (!ISTTYOPEN(cur_tty)) continue; } if ((*sc->cur_scp->tsw->te_input)(sc->cur_scp, c, cur_tty)) continue; switch (KEYFLAGS(c)) { case 0x0000: /* normal key */ ttyld_rint(cur_tty, KEYCHAR(c)); break; case FKEY: /* function key, return string */ - cp = kbd_get_fkeystr(thiskbd, KEYCHAR(c), &len); + cp = kbdd_get_fkeystr(thiskbd, KEYCHAR(c), &len); if (cp != NULL) { while (len-- > 0) ttyld_rint(cur_tty, *cp++); } break; case MKEY: /* meta is active, prepend ESC */ ttyld_rint(cur_tty, 0x1b); ttyld_rint(cur_tty, KEYCHAR(c)); break; case BKEY: /* backtab fixed sequence (esc [ Z) */ ttyld_rint(cur_tty, 0x1b); ttyld_rint(cur_tty, '['); ttyld_rint(cur_tty, 'Z'); break; } } sc->cur_scp->status |= MOUSE_HIDDEN; return 0; } static int scparam(struct tty *tp, struct termios *t) { tp->t_ispeed = t->c_ispeed; tp->t_ospeed = t->c_ospeed; tp->t_cflag = t->c_cflag; return 0; } static int scioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) { int error; int i; struct tty *tp; sc_softc_t *sc; scr_stat *scp; int s; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) int ival; #endif tp = dev->si_tty; /* If there is a user_ioctl function call that first */ if (sc_user_ioctl) { error = (*sc_user_ioctl)(dev, cmd, data, flag, td); if (error != ENOIOCTL) return error; } error = sc_vid_ioctl(tp, cmd, data, flag, td); if (error != ENOIOCTL) return error; #ifndef SC_NO_HISTORY error = sc_hist_ioctl(tp, cmd, data, flag, td); if (error != ENOIOCTL) return error; #endif #ifndef SC_NO_SYSMOUSE error = sc_mouse_ioctl(tp, cmd, data, flag, td); if (error != ENOIOCTL) return error; #endif scp = sc_get_stat(tp->t_dev); /* assert(scp != NULL) */ /* scp is sc_console, if SC_VTY(dev) == SC_CONSOLECTL. */ sc = scp->sc; if (scp->tsw) { error = (*scp->tsw->te_ioctl)(scp, tp, cmd, data, flag, td); if (error != ENOIOCTL) return error; } switch (cmd) { /* process console hardware related ioctl's */ case GIO_ATTR: /* get current attributes */ /* this ioctl is not processed here, but in the terminal emulator */ return ENOTTY; case GIO_COLOR: /* is this a color console ? */ *(int *)data = (sc->adp->va_flags & V_ADP_COLOR) ? 1 : 0; return 0; case CONS_BLANKTIME: /* set screen saver timeout (0 = no saver) */ if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME) return EINVAL; s = spltty(); scrn_blank_time = *(int *)data; run_scrn_saver = (scrn_blank_time != 0); splx(s); return 0; case CONS_CURSORTYPE: /* set cursor type (obsolete) */ s = spltty(); *(int *)data &= CONS_CURSOR_ATTRS; sc_change_cursor_shape(scp, *(int *)data, -1, -1); splx(s); return 0; case CONS_GETCURSORSHAPE: /* get cursor shape (new interface) */ if (((int *)data)[0] & CONS_LOCAL_CURSOR) { ((int *)data)[0] = scp->curr_curs_attr.flags; ((int *)data)[1] = scp->curr_curs_attr.base; ((int *)data)[2] = scp->curr_curs_attr.height; } else { ((int *)data)[0] = sc->curs_attr.flags; ((int *)data)[1] = sc->curs_attr.base; ((int *)data)[2] = sc->curs_attr.height; } return 0; case CONS_SETCURSORSHAPE: /* set cursor shape (new interface) */ s = spltty(); sc_change_cursor_shape(scp, ((int *)data)[0], ((int *)data)[1], ((int *)data)[2]); splx(s); return 0; case CONS_BELLTYPE: /* set bell type sound/visual */ if ((*(int *)data) & CONS_VISUAL_BELL) sc->flags |= SC_VISUAL_BELL; else sc->flags &= ~SC_VISUAL_BELL; if ((*(int *)data) & CONS_QUIET_BELL) sc->flags |= SC_QUIET_BELL; else sc->flags &= ~SC_QUIET_BELL; return 0; case CONS_GETINFO: /* get current (virtual) console info */ { vid_info_t *ptr = (vid_info_t*)data; if (ptr->size == sizeof(struct vid_info)) { ptr->m_num = sc->cur_scp->index; ptr->font_size = scp->font_size; ptr->mv_col = scp->xpos; ptr->mv_row = scp->ypos; ptr->mv_csz = scp->xsize; ptr->mv_rsz = scp->ysize; ptr->mv_hsz = (scp->history != NULL) ? scp->history->vtb_rows : 0; /* * The following fields are filled by the terminal emulator. XXX * * ptr->mv_norm.fore * ptr->mv_norm.back * ptr->mv_rev.fore * ptr->mv_rev.back */ ptr->mv_grfc.fore = 0; /* not supported */ ptr->mv_grfc.back = 0; /* not supported */ ptr->mv_ovscan = scp->border; if (scp == sc->cur_scp) save_kbd_state(scp); ptr->mk_keylock = scp->status & LOCK_MASK; return 0; } return EINVAL; } case CONS_GETVERS: /* get version number */ *(int*)data = 0x200; /* version 2.0 */ return 0; case CONS_IDLE: /* see if the screen has been idle */ /* * When the screen is in the GRAPHICS_MODE or UNKNOWN_MODE, * the user process may have been writing something on the * screen and syscons is not aware of it. Declare the screen * is NOT idle if it is in one of these modes. But there is * an exception to it; if a screen saver is running in the * graphics mode in the current screen, we should say that the * screen has been idle. */ *(int *)data = (sc->flags & SC_SCRN_IDLE) && (!ISGRAPHSC(sc->cur_scp) || (sc->cur_scp->status & SAVER_RUNNING)); return 0; case CONS_SAVERMODE: /* set saver mode */ switch(*(int *)data) { case CONS_NO_SAVER: case CONS_USR_SAVER: /* if a LKM screen saver is running, stop it first. */ scsplash_stick(FALSE); saver_mode = *(int *)data; s = spltty(); #ifdef DEV_SPLASH if ((error = wait_scrn_saver_stop(NULL))) { splx(s); return error; } #endif run_scrn_saver = TRUE; if (saver_mode == CONS_USR_SAVER) scp->status |= SAVER_RUNNING; else scp->status &= ~SAVER_RUNNING; scsplash_stick(TRUE); splx(s); break; case CONS_LKM_SAVER: s = spltty(); if ((saver_mode == CONS_USR_SAVER) && (scp->status & SAVER_RUNNING)) scp->status &= ~SAVER_RUNNING; saver_mode = *(int *)data; splx(s); break; default: return EINVAL; } return 0; case CONS_SAVERSTART: /* immediately start/stop the screen saver */ /* * Note that this ioctl does not guarantee the screen saver * actually starts or stops. It merely attempts to do so... */ s = spltty(); run_scrn_saver = (*(int *)data != 0); if (run_scrn_saver) sc->scrn_time_stamp -= scrn_blank_time; splx(s); return 0; case CONS_SCRSHOT: /* get a screen shot */ { int retval, hist_rsz; size_t lsize, csize; vm_offset_t frbp, hstp; unsigned lnum; scrshot_t *ptr = (scrshot_t *)data; void *outp = ptr->buf; if (ptr->x < 0 || ptr->y < 0 || ptr->xsize < 0 || ptr->ysize < 0) return EINVAL; s = spltty(); if (ISGRAPHSC(scp)) { splx(s); return EOPNOTSUPP; } hist_rsz = (scp->history != NULL) ? scp->history->vtb_rows : 0; if (((u_int)ptr->x + ptr->xsize) > scp->xsize || ((u_int)ptr->y + ptr->ysize) > (scp->ysize + hist_rsz)) { splx(s); return EINVAL; } lsize = scp->xsize * sizeof(u_int16_t); csize = ptr->xsize * sizeof(u_int16_t); /* Pointer to the last line of framebuffer */ frbp = scp->vtb.vtb_buffer + scp->ysize * lsize + ptr->x * sizeof(u_int16_t); /* Pointer to the last line of target buffer */ outp = (char *)outp + ptr->ysize * csize; /* Pointer to the last line of history buffer */ if (scp->history != NULL) hstp = scp->history->vtb_buffer + sc_vtb_tail(scp->history) * sizeof(u_int16_t) + ptr->x * sizeof(u_int16_t); else hstp = 0; retval = 0; for (lnum = 0; lnum < (ptr->y + ptr->ysize); lnum++) { if (lnum < scp->ysize) { frbp -= lsize; } else { hstp -= lsize; if (hstp < scp->history->vtb_buffer) hstp += scp->history->vtb_rows * lsize; frbp = hstp; } if (lnum < ptr->y) continue; outp = (char *)outp - csize; retval = copyout((void *)frbp, outp, csize); if (retval != 0) break; } splx(s); return retval; } case VT_SETMODE: /* set screen switcher mode */ { struct vt_mode *mode; struct proc *p1; mode = (struct vt_mode *)data; DPRINTF(5, ("%s%d: VT_SETMODE ", SC_DRIVER_NAME, sc->unit)); if (scp->smode.mode == VT_PROCESS) { p1 = pfind(scp->pid); if (scp->proc == p1 && scp->proc != td->td_proc) { if (p1) PROC_UNLOCK(p1); DPRINTF(5, ("error EPERM\n")); return EPERM; } if (p1) PROC_UNLOCK(p1); } s = spltty(); if (mode->mode == VT_AUTO) { scp->smode.mode = VT_AUTO; scp->proc = NULL; scp->pid = 0; DPRINTF(5, ("VT_AUTO, ")); if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit)) cnavailable(sc_consptr, TRUE); /* were we in the middle of the vty switching process? */ if (finish_vt_rel(scp, TRUE, &s) == 0) DPRINTF(5, ("reset WAIT_REL, ")); if (finish_vt_acq(scp) == 0) DPRINTF(5, ("reset WAIT_ACQ, ")); } else { if (!ISSIGVALID(mode->relsig) || !ISSIGVALID(mode->acqsig) || !ISSIGVALID(mode->frsig)) { splx(s); DPRINTF(5, ("error EINVAL\n")); return EINVAL; } DPRINTF(5, ("VT_PROCESS %d, ", td->td_proc->p_pid)); bcopy(data, &scp->smode, sizeof(struct vt_mode)); scp->proc = td->td_proc; scp->pid = scp->proc->p_pid; if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit)) cnavailable(sc_consptr, FALSE); } splx(s); DPRINTF(5, ("\n")); return 0; } case VT_GETMODE: /* get screen switcher mode */ bcopy(&scp->smode, data, sizeof(struct vt_mode)); return 0; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('v', 4): ival = IOCPARM_IVAL(data); data = (caddr_t)&ival; /* FALLTHROUGH */ #endif case VT_RELDISP: /* screen switcher ioctl */ s = spltty(); /* * This must be the current vty which is in the VT_PROCESS * switching mode... */ if ((scp != sc->cur_scp) || (scp->smode.mode != VT_PROCESS)) { splx(s); return EINVAL; } /* ...and this process is controlling it. */ if (scp->proc != td->td_proc) { splx(s); return EPERM; } error = EINVAL; switch(*(int *)data) { case VT_FALSE: /* user refuses to release screen, abort */ if ((error = finish_vt_rel(scp, FALSE, &s)) == 0) DPRINTF(5, ("%s%d: VT_FALSE\n", SC_DRIVER_NAME, sc->unit)); break; case VT_TRUE: /* user has released screen, go on */ if ((error = finish_vt_rel(scp, TRUE, &s)) == 0) DPRINTF(5, ("%s%d: VT_TRUE\n", SC_DRIVER_NAME, sc->unit)); break; case VT_ACKACQ: /* acquire acknowledged, switch completed */ if ((error = finish_vt_acq(scp)) == 0) DPRINTF(5, ("%s%d: VT_ACKACQ\n", SC_DRIVER_NAME, sc->unit)); break; default: break; } splx(s); return error; case VT_OPENQRY: /* return free virtual console */ for (i = sc->first_vty; i < sc->first_vty + sc->vtys; i++) { tp = VIRTUAL_TTY(sc, i); if (!ISTTYOPEN(tp)) { *(int *)data = i + 1; return 0; } } return EINVAL; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('v', 5): ival = IOCPARM_IVAL(data); data = (caddr_t)&ival; /* FALLTHROUGH */ #endif case VT_ACTIVATE: /* switch to screen *data */ i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1); s = spltty(); error = sc_clean_up(sc->cur_scp); splx(s); if (error) return error; return sc_switch_scr(sc, i); #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('v', 6): ival = IOCPARM_IVAL(data); data = (caddr_t)&ival; /* FALLTHROUGH */ #endif case VT_WAITACTIVE: /* wait for switch to occur */ i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1); if ((i < sc->first_vty) || (i >= sc->first_vty + sc->vtys)) return EINVAL; s = spltty(); error = sc_clean_up(sc->cur_scp); splx(s); if (error) return error; scp = sc_get_stat(SC_DEV(sc, i)); if (scp == scp->sc->cur_scp) return 0; error = tsleep(&scp->smode, PZERO | PCATCH, "waitvt", 0); return error; case VT_GETACTIVE: /* get active vty # */ *(int *)data = sc->cur_scp->index + 1; return 0; case VT_GETINDEX: /* get this vty # */ *(int *)data = scp->index + 1; return 0; case VT_LOCKSWITCH: /* prevent vty switching */ if ((*(int *)data) & 0x01) sc->flags |= SC_SCRN_VTYLOCK; else sc->flags &= ~SC_SCRN_VTYLOCK; return 0; case KDENABIO: /* allow io operations */ error = priv_check(td, PRIV_IO); if (error != 0) return error; error = securelevel_gt(td->td_ucred, 0); if (error != 0) return error; #ifdef __i386__ td->td_frame->tf_eflags |= PSL_IOPL; #elif defined(__amd64__) td->td_frame->tf_rflags |= PSL_IOPL; #endif return 0; case KDDISABIO: /* disallow io operations (default) */ #ifdef __i386__ td->td_frame->tf_eflags &= ~PSL_IOPL; #elif defined(__amd64__) td->td_frame->tf_rflags &= ~PSL_IOPL; #endif return 0; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 20): ival = IOCPARM_IVAL(data); data = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSKBSTATE: /* set keyboard state (locks) */ if (*(int *)data & ~LOCK_MASK) return EINVAL; scp->status &= ~LOCK_MASK; scp->status |= *(int *)data; if (scp == sc->cur_scp) update_kbd_state(scp, scp->status, LOCK_MASK); return 0; case KDGKBSTATE: /* get keyboard state (locks) */ if (scp == sc->cur_scp) save_kbd_state(scp); *(int *)data = scp->status & LOCK_MASK; return 0; case KDGETREPEAT: /* get keyboard repeat & delay rates */ case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */ - error = kbd_ioctl(sc->kbd, cmd, data); + error = kbdd_ioctl(sc->kbd, cmd, data); if (error == ENOIOCTL) error = ENODEV; return error; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 67): ival = IOCPARM_IVAL(data); data = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSETRAD: /* set keyboard repeat & delay rates (old) */ if (*(int *)data & ~0x7f) return EINVAL; - error = kbd_ioctl(sc->kbd, KDSETRAD, data); + error = kbdd_ioctl(sc->kbd, KDSETRAD, data); if (error == ENOIOCTL) error = ENODEV; return error; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 7): ival = IOCPARM_IVAL(data); data = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSKBMODE: /* set keyboard mode */ switch (*(int *)data) { case K_XLATE: /* switch to XLT ascii mode */ case K_RAW: /* switch to RAW scancode mode */ case K_CODE: /* switch to CODE mode */ scp->kbd_mode = *(int *)data; if (scp == sc->cur_scp) - kbd_ioctl(sc->kbd, KDSKBMODE, data); + kbdd_ioctl(sc->kbd, KDSKBMODE, data); return 0; default: return EINVAL; } /* NOT REACHED */ case KDGKBMODE: /* get keyboard mode */ *(int *)data = scp->kbd_mode; return 0; case KDGKBINFO: - error = kbd_ioctl(sc->kbd, cmd, data); + error = kbdd_ioctl(sc->kbd, cmd, data); if (error == ENOIOCTL) error = ENODEV; return error; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 8): ival = IOCPARM_IVAL(data); data = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDMKTONE: /* sound the bell */ if (*(int*)data) sc_bell(scp, (*(int*)data)&0xffff, (((*(int*)data)>>16)&0xffff)*hz/1000); else sc_bell(scp, scp->bell_pitch, scp->bell_duration); return 0; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 63): ival = IOCPARM_IVAL(data); data = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KIOCSOUND: /* make tone (*data) hz */ if (scp == sc->cur_scp) { if (*(int *)data) return sc_tone(*(int *)data); else return sc_tone(0); } return 0; case KDGKBTYPE: /* get keyboard type */ - error = kbd_ioctl(sc->kbd, cmd, data); + error = kbdd_ioctl(sc->kbd, cmd, data); if (error == ENOIOCTL) { /* always return something? XXX */ *(int *)data = 0; } return 0; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('K', 66): ival = IOCPARM_IVAL(data); data = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSETLED: /* set keyboard LED status */ if (*(int *)data & ~LED_MASK) /* FIXME: LOCK_MASK? */ return EINVAL; scp->status &= ~LED_MASK; scp->status |= *(int *)data; if (scp == sc->cur_scp) update_kbd_leds(scp, scp->status); return 0; case KDGETLED: /* get keyboard LED status */ if (scp == sc->cur_scp) save_kbd_state(scp); *(int *)data = scp->status & LED_MASK; return 0; case KBADDKBD: /* add/remove keyboard to/from mux */ case KBRELKBD: - error = kbd_ioctl(sc->kbd, cmd, data); + error = kbdd_ioctl(sc->kbd, cmd, data); if (error == ENOIOCTL) error = ENODEV; return error; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) case _IO('c', 110): ival = IOCPARM_IVAL(data); data = (caddr_t)&ival; /* FALLTHROUGH */ #endif case CONS_SETKBD: /* set the new keyboard */ { keyboard_t *newkbd; s = spltty(); newkbd = kbd_get_keyboard(*(int *)data); if (newkbd == NULL) { splx(s); return EINVAL; } error = 0; if (sc->kbd != newkbd) { i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit, (void *)&sc->keyboard, sckbdevent, sc); /* i == newkbd->kb_index */ if (i >= 0) { if (sc->kbd != NULL) { save_kbd_state(sc->cur_scp); kbd_release(sc->kbd, (void *)&sc->keyboard); } sc->kbd = kbd_get_keyboard(i); /* sc->kbd == newkbd */ sc->keyboard = i; - kbd_ioctl(sc->kbd, KDSKBMODE, + kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&sc->cur_scp->kbd_mode); update_kbd_state(sc->cur_scp, sc->cur_scp->status, LOCK_MASK); } else { error = EPERM; /* XXX */ } } splx(s); return error; } case CONS_RELKBD: /* release the current keyboard */ s = spltty(); error = 0; if (sc->kbd != NULL) { save_kbd_state(sc->cur_scp); error = kbd_release(sc->kbd, (void *)&sc->keyboard); if (error == 0) { sc->kbd = NULL; sc->keyboard = -1; } } splx(s); return error; case CONS_GETTERM: /* get the current terminal emulator info */ { sc_term_sw_t *sw; if (((term_info_t *)data)->ti_index == 0) { sw = scp->tsw; } else { sw = sc_term_match_by_number(((term_info_t *)data)->ti_index); } if (sw != NULL) { strncpy(((term_info_t *)data)->ti_name, sw->te_name, sizeof(((term_info_t *)data)->ti_name)); strncpy(((term_info_t *)data)->ti_desc, sw->te_desc, sizeof(((term_info_t *)data)->ti_desc)); ((term_info_t *)data)->ti_flags = 0; return 0; } else { ((term_info_t *)data)->ti_name[0] = '\0'; ((term_info_t *)data)->ti_desc[0] = '\0'; ((term_info_t *)data)->ti_flags = 0; return EINVAL; } } case CONS_SETTERM: /* set the current terminal emulator */ s = spltty(); error = sc_init_emulator(scp, ((term_info_t *)data)->ti_name); /* FIXME: what if scp == sc_console! XXX */ splx(s); return error; case GIO_SCRNMAP: /* get output translation table */ bcopy(&sc->scr_map, data, sizeof(sc->scr_map)); return 0; case PIO_SCRNMAP: /* set output translation table */ bcopy(data, &sc->scr_map, sizeof(sc->scr_map)); for (i=0; iscr_map); i++) { sc->scr_rmap[sc->scr_map[i]] = i; } return 0; case GIO_KEYMAP: /* get keyboard translation table */ case PIO_KEYMAP: /* set keyboard translation table */ case GIO_DEADKEYMAP: /* get accent key translation table */ case PIO_DEADKEYMAP: /* set accent key translation table */ case GETFKEY: /* get function key string */ case SETFKEY: /* set function key string */ - error = kbd_ioctl(sc->kbd, cmd, data); + error = kbdd_ioctl(sc->kbd, cmd, data); if (error == ENOIOCTL) error = ENODEV; return error; #ifndef SC_NO_FONT_LOADING case PIO_FONT8x8: /* set 8x8 dot font */ if (!ISFONTAVAIL(sc->adp->va_flags)) return ENXIO; bcopy(data, sc->font_8, 8*256); sc->fonts_loaded |= FONT_8; /* * FONT KLUDGE * Always use the font page #0. XXX * Don't load if the current font size is not 8x8. */ if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size < 14)) sc_load_font(sc->cur_scp, 0, 8, 8, sc->font_8, 0, 256); return 0; case GIO_FONT8x8: /* get 8x8 dot font */ if (!ISFONTAVAIL(sc->adp->va_flags)) return ENXIO; if (sc->fonts_loaded & FONT_8) { bcopy(sc->font_8, data, 8*256); return 0; } else return ENXIO; case PIO_FONT8x14: /* set 8x14 dot font */ if (!ISFONTAVAIL(sc->adp->va_flags)) return ENXIO; bcopy(data, sc->font_14, 14*256); sc->fonts_loaded |= FONT_14; /* * FONT KLUDGE * Always use the font page #0. XXX * Don't load if the current font size is not 8x14. */ if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size >= 14) && (sc->cur_scp->font_size < 16)) sc_load_font(sc->cur_scp, 0, 14, 8, sc->font_14, 0, 256); return 0; case GIO_FONT8x14: /* get 8x14 dot font */ if (!ISFONTAVAIL(sc->adp->va_flags)) return ENXIO; if (sc->fonts_loaded & FONT_14) { bcopy(sc->font_14, data, 14*256); return 0; } else return ENXIO; case PIO_FONT8x16: /* set 8x16 dot font */ if (!ISFONTAVAIL(sc->adp->va_flags)) return ENXIO; bcopy(data, sc->font_16, 16*256); sc->fonts_loaded |= FONT_16; /* * FONT KLUDGE * Always use the font page #0. XXX * Don't load if the current font size is not 8x16. */ if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size >= 16)) sc_load_font(sc->cur_scp, 0, 16, 8, sc->font_16, 0, 256); return 0; case GIO_FONT8x16: /* get 8x16 dot font */ if (!ISFONTAVAIL(sc->adp->va_flags)) return ENXIO; if (sc->fonts_loaded & FONT_16) { bcopy(sc->font_16, data, 16*256); return 0; } else return ENXIO; #endif /* SC_NO_FONT_LOADING */ default: break; } return (ttyioctl(dev, cmd, data, flag, td)); } static void scstart(struct tty *tp) { struct clist *rbp; int s, len; u_char buf[PCBURST]; scr_stat *scp = sc_get_stat(tp->t_dev); if (scp->status & SLKED || (scp == scp->sc->cur_scp && scp->sc->blink_in_progress)) return; s = spltty(); if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) { tp->t_state |= TS_BUSY; rbp = &tp->t_outq; while (rbp->c_cc) { len = q_to_b(rbp, buf, PCBURST); splx(s); sc_puts(scp, buf, len); s = spltty(); } tp->t_state &= ~TS_BUSY; ttwwakeup(tp); } splx(s); } static void sc_cnprobe(struct consdev *cp) { int unit; int flags; cp->cn_pri = sc_get_cons_priority(&unit, &flags); /* a video card is always required */ if (!scvidprobe(unit, flags, TRUE)) cp->cn_pri = CN_DEAD; /* syscons will become console even when there is no keyboard */ sckbdprobe(unit, flags, TRUE); if (cp->cn_pri == CN_DEAD) return; /* initialize required fields */ sprintf(cp->cn_name, "consolectl"); } static void sc_cninit(struct consdev *cp) { int unit; int flags; sc_get_cons_priority(&unit, &flags); scinit(unit, flags | SC_KERNEL_CONSOLE); sc_console_unit = unit; sc_console = sc_get_stat(sc_get_softc(unit, SC_KERNEL_CONSOLE)->dev[0]); sc_consptr = cp; } static void sc_cnterm(struct consdev *cp) { /* we are not the kernel console any more, release everything */ if (sc_console_unit < 0) return; /* shouldn't happen */ #if 0 /* XXX */ sc_clear_screen(sc_console); sccnupdate(sc_console); #endif scterm(sc_console_unit, SC_KERNEL_CONSOLE); sc_console_unit = -1; sc_console = NULL; } static void sc_cnputc(struct consdev *cd, int c) { u_char buf[1]; scr_stat *scp = sc_console; void *save; #ifndef SC_NO_HISTORY struct tty *tp; #endif /* !SC_NO_HISTORY */ int s; /* assert(sc_console != NULL) */ #ifndef SC_NO_HISTORY if (scp == scp->sc->cur_scp && scp->status & SLKED) { scp->status &= ~SLKED; update_kbd_state(scp, scp->status, SLKED); if (scp->status & BUFFER_SAVED) { if (!sc_hist_restore(scp)) sc_remove_cutmarking(scp); scp->status &= ~BUFFER_SAVED; scp->status |= CURSOR_ENABLED; sc_draw_cursor_image(scp); } tp = VIRTUAL_TTY(scp->sc, scp->index); if (ISTTYOPEN(tp)) scstart(tp); } #endif /* !SC_NO_HISTORY */ save = scp->ts; if (kernel_console_ts != NULL) scp->ts = kernel_console_ts; buf[0] = c; sc_puts(scp, buf, 1); scp->ts = save; s = spltty(); /* block sckbdevent and scrn_timer */ sccnupdate(scp); splx(s); } static int sc_cngetc(struct consdev *cd) { return sccngetch(SCGETC_NONBLOCK); } static int sccngetch(int flags) { static struct fkeytab fkey; static int fkeycp; scr_stat *scp; u_char *p; int cur_mode; int s = spltty(); /* block sckbdevent and scrn_timer while we poll */ int c; /* assert(sc_console != NULL) */ /* * Stop the screen saver and update the screen if necessary. * What if we have been running in the screen saver code... XXX */ sc_touch_scrn_saver(); scp = sc_console->sc->cur_scp; /* XXX */ sccnupdate(scp); if (fkeycp < fkey.len) { splx(s); return fkey.str[fkeycp++]; } if (scp->sc->kbd == NULL) { splx(s); return -1; } /* * Make sure the keyboard is accessible even when the kbd device * driver is disabled. */ - kbd_enable(scp->sc->kbd); + kbdd_enable(scp->sc->kbd); /* we shall always use the keyboard in the XLATE mode here */ cur_mode = scp->kbd_mode; scp->kbd_mode = K_XLATE; - kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode); + kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode); - kbd_poll(scp->sc->kbd, TRUE); + kbdd_poll(scp->sc->kbd, TRUE); c = scgetc(scp->sc, SCGETC_CN | flags); - kbd_poll(scp->sc->kbd, FALSE); + kbdd_poll(scp->sc->kbd, FALSE); scp->kbd_mode = cur_mode; - kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode); - kbd_disable(scp->sc->kbd); + kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode); + kbdd_disable(scp->sc->kbd); splx(s); switch (KEYFLAGS(c)) { case 0: /* normal char */ return KEYCHAR(c); case FKEY: /* function key */ - p = kbd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp); + p = kbdd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp); fkey.len = fkeycp; if ((p != NULL) && (fkey.len > 0)) { bcopy(p, fkey.str, fkey.len); fkeycp = 1; return fkey.str[0]; } return c; /* XXX */ case NOKEY: case ERRKEY: default: return -1; } /* NOT REACHED */ } static void sccnupdate(scr_stat *scp) { /* this is a cut-down version of scrn_timer()... */ if (scp->sc->font_loading_in_progress) return; if (debugger > 0 || panicstr || shutdown_in_progress) { sc_touch_scrn_saver(); } else if (scp != scp->sc->cur_scp) { return; } if (!run_scrn_saver) scp->sc->flags &= ~SC_SCRN_IDLE; #ifdef DEV_SPLASH if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE)) if (scp->sc->flags & SC_SCRN_BLANKED) stop_scrn_saver(scp->sc, current_saver); #endif if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress || scp->sc->switch_in_progress) return; /* * FIXME: unlike scrn_timer(), we call scrn_update() from here even * when write_in_progress is non-zero. XXX */ if (!ISGRAPHSC(scp) && !(scp->sc->flags & SC_SCRN_BLANKED)) scrn_update(scp, TRUE); } static void scrn_timer(void *arg) { #ifndef PC98 static int kbd_interval = 0; #endif struct timeval tv; sc_softc_t *sc; scr_stat *scp; int again; int s; again = (arg != NULL); if (arg != NULL) sc = (sc_softc_t *)arg; else if (sc_console != NULL) sc = sc_console->sc; else return; /* don't do anything when we are performing some I/O operations */ if (sc->font_loading_in_progress) { if (again) timeout(scrn_timer, sc, hz / 10); return; } s = spltty(); #ifndef PC98 if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) { /* try to allocate a keyboard automatically */ if (++kbd_interval >= 25) { sc->keyboard = sc_allocate_keyboard(sc, -1); if (sc->keyboard >= 0) { sc->kbd = kbd_get_keyboard(sc->keyboard); - kbd_ioctl(sc->kbd, KDSKBMODE, + kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&sc->cur_scp->kbd_mode); update_kbd_state(sc->cur_scp, sc->cur_scp->status, LOCK_MASK); } kbd_interval = 0; } } #endif /* PC98 */ /* find the vty to update */ scp = sc->cur_scp; /* should we stop the screen saver? */ getmicrouptime(&tv); if (debugger > 0 || panicstr || shutdown_in_progress) sc_touch_scrn_saver(); if (run_scrn_saver) { if (tv.tv_sec > sc->scrn_time_stamp + scrn_blank_time) sc->flags |= SC_SCRN_IDLE; else sc->flags &= ~SC_SCRN_IDLE; } else { sc->scrn_time_stamp = tv.tv_sec; sc->flags &= ~SC_SCRN_IDLE; if (scrn_blank_time > 0) run_scrn_saver = TRUE; } #ifdef DEV_SPLASH if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE)) if (sc->flags & SC_SCRN_BLANKED) stop_scrn_saver(sc, current_saver); #endif /* should we just return ? */ if (sc->blink_in_progress || sc->switch_in_progress || sc->write_in_progress) { if (again) timeout(scrn_timer, sc, hz / 10); splx(s); return; } /* Update the screen */ scp = sc->cur_scp; /* cur_scp may have changed... */ if (!ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED)) scrn_update(scp, TRUE); #ifdef DEV_SPLASH /* should we activate the screen saver? */ if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE)) if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED)) (*current_saver)(sc, TRUE); #endif if (again) timeout(scrn_timer, sc, hz / 25); splx(s); } static int and_region(int *s1, int *e1, int s2, int e2) { if (*e1 < s2 || e2 < *s1) return FALSE; *s1 = imax(*s1, s2); *e1 = imin(*e1, e2); return TRUE; } static void scrn_update(scr_stat *scp, int show_cursor) { int start; int end; int s; int e; /* assert(scp == scp->sc->cur_scp) */ SC_VIDEO_LOCK(scp->sc); #ifndef SC_NO_CUTPASTE /* remove the previous mouse pointer image if necessary */ if (scp->status & MOUSE_VISIBLE) { s = scp->mouse_pos; e = scp->mouse_pos + scp->xsize + 1; if ((scp->status & (MOUSE_MOVED | MOUSE_HIDDEN)) || and_region(&s, &e, scp->start, scp->end) || ((scp->status & CURSOR_ENABLED) && (scp->cursor_pos != scp->cursor_oldpos) && (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos) || and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos)))) { sc_remove_mouse_image(scp); if (scp->end >= scp->xsize*scp->ysize) scp->end = scp->xsize*scp->ysize - 1; } } #endif /* !SC_NO_CUTPASTE */ #if 1 /* debug: XXX */ if (scp->end >= scp->xsize*scp->ysize) { printf("scrn_update(): scp->end %d > size_of_screen!!\n", scp->end); scp->end = scp->xsize*scp->ysize - 1; } if (scp->start < 0) { printf("scrn_update(): scp->start %d < 0\n", scp->start); scp->start = 0; } #endif /* update screen image */ if (scp->start <= scp->end) { if (scp->mouse_cut_end >= 0) { /* there is a marked region for cut & paste */ if (scp->mouse_cut_start <= scp->mouse_cut_end) { start = scp->mouse_cut_start; end = scp->mouse_cut_end; } else { start = scp->mouse_cut_end; end = scp->mouse_cut_start - 1; } s = start; e = end; /* does the cut-mark region overlap with the update region? */ if (and_region(&s, &e, scp->start, scp->end)) { (*scp->rndr->draw)(scp, s, e - s + 1, TRUE); s = 0; e = start - 1; if (and_region(&s, &e, scp->start, scp->end)) (*scp->rndr->draw)(scp, s, e - s + 1, FALSE); s = end + 1; e = scp->xsize*scp->ysize - 1; if (and_region(&s, &e, scp->start, scp->end)) (*scp->rndr->draw)(scp, s, e - s + 1, FALSE); } else { (*scp->rndr->draw)(scp, scp->start, scp->end - scp->start + 1, FALSE); } } else { (*scp->rndr->draw)(scp, scp->start, scp->end - scp->start + 1, FALSE); } } /* we are not to show the cursor and the mouse pointer... */ if (!show_cursor) { scp->end = 0; scp->start = scp->xsize*scp->ysize - 1; SC_VIDEO_UNLOCK(scp->sc); return; } /* update cursor image */ if (scp->status & CURSOR_ENABLED) { s = scp->start; e = scp->end; /* did cursor move since last time ? */ if (scp->cursor_pos != scp->cursor_oldpos) { /* do we need to remove old cursor image ? */ if (!and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos)) sc_remove_cursor_image(scp); sc_draw_cursor_image(scp); } else { if (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos)) /* cursor didn't move, but has been overwritten */ sc_draw_cursor_image(scp); else if (scp->curs_attr.flags & CONS_BLINK_CURSOR) /* if it's a blinking cursor, update it */ (*scp->rndr->blink_cursor)(scp, scp->cursor_pos, sc_inside_cutmark(scp, scp->cursor_pos)); } } #ifndef SC_NO_CUTPASTE /* update "pseudo" mouse pointer image */ if (scp->sc->flags & SC_MOUSE_ENABLED) { if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) { scp->status &= ~MOUSE_MOVED; sc_draw_mouse_image(scp); } } #endif /* SC_NO_CUTPASTE */ scp->end = 0; scp->start = scp->xsize*scp->ysize - 1; SC_VIDEO_UNLOCK(scp->sc); } #ifdef DEV_SPLASH static int scsplash_callback(int event, void *arg) { sc_softc_t *sc; int error; sc = (sc_softc_t *)arg; switch (event) { case SPLASH_INIT: if (add_scrn_saver(scsplash_saver) == 0) { sc->flags &= ~SC_SAVER_FAILED; run_scrn_saver = TRUE; if (cold && !(boothowto & RB_VERBOSE)) { scsplash_stick(TRUE); (*current_saver)(sc, TRUE); } } return 0; case SPLASH_TERM: if (current_saver == scsplash_saver) { scsplash_stick(FALSE); error = remove_scrn_saver(scsplash_saver); if (error) return error; } return 0; default: return EINVAL; } } static void scsplash_saver(sc_softc_t *sc, int show) { static int busy = FALSE; scr_stat *scp; if (busy) return; busy = TRUE; scp = sc->cur_scp; if (show) { if (!(sc->flags & SC_SAVER_FAILED)) { if (!(sc->flags & SC_SCRN_BLANKED)) set_scrn_saver_mode(scp, -1, NULL, 0); switch (splash(sc->adp, TRUE)) { case 0: /* succeeded */ break; case EAGAIN: /* try later */ restore_scrn_saver_mode(scp, FALSE); sc_touch_scrn_saver(); /* XXX */ break; default: sc->flags |= SC_SAVER_FAILED; scsplash_stick(FALSE); restore_scrn_saver_mode(scp, TRUE); printf("scsplash_saver(): failed to put up the image\n"); break; } } } else if (!sticky_splash) { if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0)) restore_scrn_saver_mode(scp, TRUE); } busy = FALSE; } static int add_scrn_saver(void (*this_saver)(sc_softc_t *, int)) { #if 0 int error; if (current_saver != none_saver) { error = remove_scrn_saver(current_saver); if (error) return error; } #endif if (current_saver != none_saver) return EBUSY; run_scrn_saver = FALSE; saver_mode = CONS_LKM_SAVER; current_saver = this_saver; return 0; } static int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int)) { if (current_saver != this_saver) return EINVAL; #if 0 /* * In order to prevent `current_saver' from being called by * the timeout routine `scrn_timer()' while we manipulate * the saver list, we shall set `current_saver' to `none_saver' * before stopping the current saver, rather than blocking by `splXX()'. */ current_saver = none_saver; if (scrn_blanked) stop_scrn_saver(this_saver); #endif /* unblank all blanked screens */ wait_scrn_saver_stop(NULL); if (scrn_blanked) return EBUSY; current_saver = none_saver; return 0; } static int set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border) { int s; /* assert(scp == scp->sc->cur_scp) */ s = spltty(); if (!ISGRAPHSC(scp)) sc_remove_cursor_image(scp); scp->splash_save_mode = scp->mode; scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE); scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE); scp->status |= (UNKNOWN_MODE | SAVER_RUNNING); scp->sc->flags |= SC_SCRN_BLANKED; ++scrn_blanked; splx(s); if (mode < 0) return 0; scp->mode = mode; if (set_mode(scp) == 0) { if (scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS) scp->status |= GRAPHICS_MODE; #ifndef SC_NO_PALETTE_LOADING if (pal != NULL) load_palette(scp->sc->adp, pal); #endif sc_set_border(scp, border); return 0; } else { s = spltty(); scp->mode = scp->splash_save_mode; scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING); scp->status |= scp->splash_save_status; splx(s); return 1; } } static int restore_scrn_saver_mode(scr_stat *scp, int changemode) { int mode; int status; int s; /* assert(scp == scp->sc->cur_scp) */ s = spltty(); mode = scp->mode; status = scp->status; scp->mode = scp->splash_save_mode; scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING); scp->status |= scp->splash_save_status; scp->sc->flags &= ~SC_SCRN_BLANKED; if (!changemode) { if (!ISGRAPHSC(scp)) sc_draw_cursor_image(scp); --scrn_blanked; splx(s); return 0; } if (set_mode(scp) == 0) { #ifndef SC_NO_PALETTE_LOADING load_palette(scp->sc->adp, scp->sc->palette); #endif --scrn_blanked; splx(s); return 0; } else { scp->mode = mode; scp->status = status; splx(s); return 1; } } static void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int)) { (*saver)(sc, FALSE); run_scrn_saver = FALSE; /* the screen saver may have chosen not to stop after all... */ if (sc->flags & SC_SCRN_BLANKED) return; mark_all(sc->cur_scp); if (sc->delayed_next_scr) sc_switch_scr(sc, sc->delayed_next_scr - 1); if (debugger == 0) wakeup(&scrn_blanked); } static int wait_scrn_saver_stop(sc_softc_t *sc) { int error = 0; while (scrn_blanked > 0) { run_scrn_saver = FALSE; if (sc && !(sc->flags & SC_SCRN_BLANKED)) { error = 0; break; } error = tsleep(&scrn_blanked, PZERO | PCATCH, "scrsav", 0); if ((error != 0) && (error != ERESTART)) break; } run_scrn_saver = FALSE; return error; } #endif /* DEV_SPLASH */ void sc_touch_scrn_saver(void) { scsplash_stick(FALSE); run_scrn_saver = FALSE; } int sc_switch_scr(sc_softc_t *sc, u_int next_scr) { scr_stat *cur_scp; struct tty *tp; struct proc *p; int s; DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1)); if (sc->cur_scp == NULL) return (0); /* prevent switch if previously requested */ if (sc->flags & SC_SCRN_VTYLOCK) { sc_bell(sc->cur_scp, sc->cur_scp->bell_pitch, sc->cur_scp->bell_duration); return EPERM; } /* delay switch if the screen is blanked or being updated */ if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress || sc->blink_in_progress) { sc->delayed_next_scr = next_scr + 1; sc_touch_scrn_saver(); DPRINTF(5, ("switch delayed\n")); return 0; } sc->delayed_next_scr = 0; s = spltty(); cur_scp = sc->cur_scp; /* we are in the middle of the vty switching process... */ if (sc->switch_in_progress && (cur_scp->smode.mode == VT_PROCESS) && cur_scp->proc) { p = pfind(cur_scp->pid); if (cur_scp->proc != p) { if (p) PROC_UNLOCK(p); /* * The controlling process has died!!. Do some clean up. * NOTE:`cur_scp->proc' and `cur_scp->smode.mode' * are not reset here yet; they will be cleared later. */ DPRINTF(5, ("cur_scp controlling process %d died, ", cur_scp->pid)); if (cur_scp->status & SWITCH_WAIT_REL) { /* * Force the previous switch to finish, but return now * with error. */ DPRINTF(5, ("reset WAIT_REL, ")); finish_vt_rel(cur_scp, TRUE, &s); splx(s); DPRINTF(5, ("finishing previous switch\n")); return EINVAL; } else if (cur_scp->status & SWITCH_WAIT_ACQ) { /* let's assume screen switch has been completed. */ DPRINTF(5, ("reset WAIT_ACQ, ")); finish_vt_acq(cur_scp); } else { /* * We are in between screen release and acquisition, and * reached here via scgetc() or scrn_timer() which has * interrupted exchange_scr(). Don't do anything stupid. */ DPRINTF(5, ("waiting nothing, ")); } } else { if (p) PROC_UNLOCK(p); /* * The controlling process is alive, but not responding... * It is either buggy or it may be just taking time. * The following code is a gross kludge to cope with this * problem for which there is no clean solution. XXX */ if (cur_scp->status & SWITCH_WAIT_REL) { switch (sc->switch_in_progress++) { case 1: break; case 2: DPRINTF(5, ("sending relsig again, ")); signal_vt_rel(cur_scp); break; case 3: break; case 4: default: /* * Act as if the controlling program returned * VT_FALSE. */ DPRINTF(5, ("force reset WAIT_REL, ")); finish_vt_rel(cur_scp, FALSE, &s); splx(s); DPRINTF(5, ("act as if VT_FALSE was seen\n")); return EINVAL; } } else if (cur_scp->status & SWITCH_WAIT_ACQ) { switch (sc->switch_in_progress++) { case 1: break; case 2: DPRINTF(5, ("sending acqsig again, ")); signal_vt_acq(cur_scp); break; case 3: break; case 4: default: /* clear the flag and finish the previous switch */ DPRINTF(5, ("force reset WAIT_ACQ, ")); finish_vt_acq(cur_scp); break; } } } } /* * Return error if an invalid argument is given, or vty switch * is still in progress. */ if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys) || sc->switch_in_progress) { splx(s); sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION); DPRINTF(5, ("error 1\n")); return EINVAL; } /* * Don't allow switching away from the graphics mode vty * if the switch mode is VT_AUTO, unless the next vty is the same * as the current or the current vty has been closed (but showing). */ tp = VIRTUAL_TTY(sc, cur_scp->index); if ((cur_scp->index != next_scr) && ISTTYOPEN(tp) && (cur_scp->smode.mode == VT_AUTO) && ISGRAPHSC(cur_scp)) { splx(s); sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION); DPRINTF(5, ("error, graphics mode\n")); return EINVAL; } /* * Is the wanted vty open? Don't allow switching to a closed vty. * If we are in DDB, don't switch to a vty in the VT_PROCESS mode. * Note that we always allow the user to switch to the kernel * console even if it is closed. */ if ((sc_console == NULL) || (next_scr != sc_console->index)) { tp = VIRTUAL_TTY(sc, next_scr); if (!ISTTYOPEN(tp)) { splx(s); sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION); DPRINTF(5, ("error 2, requested vty isn't open!\n")); return EINVAL; } if ((debugger > 0) && (SC_STAT(tp->t_dev)->smode.mode == VT_PROCESS)) { splx(s); DPRINTF(5, ("error 3, requested vty is in the VT_PROCESS mode\n")); return EINVAL; } } /* this is the start of vty switching process... */ ++sc->switch_in_progress; sc->old_scp = cur_scp; sc->new_scp = sc_get_stat(SC_DEV(sc, next_scr)); if (sc->new_scp == sc->old_scp) { sc->switch_in_progress = 0; /* * XXX wakeup() locks the scheduler lock which will hang if * the lock is in an in-between state, e.g., when we stop at * a breakpoint at fork_exit. It has always been wrong to call * wakeup() when the debugger is active. In RELENG_4, wakeup() * is supposed to be locked by splhigh(), but the debugger may * be invoked at splhigh(). */ if (debugger == 0) wakeup(&sc->new_scp->smode); splx(s); DPRINTF(5, ("switch done (new == old)\n")); return 0; } /* has controlling process died? */ vt_proc_alive(sc->old_scp); vt_proc_alive(sc->new_scp); /* wait for the controlling process to release the screen, if necessary */ if (signal_vt_rel(sc->old_scp)) { splx(s); return 0; } /* go set up the new vty screen */ splx(s); exchange_scr(sc); s = spltty(); /* wake up processes waiting for this vty */ if (debugger == 0) wakeup(&sc->cur_scp->smode); /* wait for the controlling process to acknowledge, if necessary */ if (signal_vt_acq(sc->cur_scp)) { splx(s); return 0; } sc->switch_in_progress = 0; if (sc->unit == sc_console_unit) cnavailable(sc_consptr, TRUE); splx(s); DPRINTF(5, ("switch done\n")); return 0; } static int do_switch_scr(sc_softc_t *sc, int s) { vt_proc_alive(sc->new_scp); splx(s); exchange_scr(sc); s = spltty(); /* sc->cur_scp == sc->new_scp */ wakeup(&sc->cur_scp->smode); /* wait for the controlling process to acknowledge, if necessary */ if (!signal_vt_acq(sc->cur_scp)) { sc->switch_in_progress = 0; if (sc->unit == sc_console_unit) cnavailable(sc_consptr, TRUE); } return s; } static int vt_proc_alive(scr_stat *scp) { struct proc *p; if (scp->proc) { if ((p = pfind(scp->pid)) != NULL) PROC_UNLOCK(p); if (scp->proc == p) return TRUE; scp->proc = NULL; scp->smode.mode = VT_AUTO; DPRINTF(5, ("vt controlling process %d died\n", scp->pid)); } return FALSE; } static int signal_vt_rel(scr_stat *scp) { if (scp->smode.mode != VT_PROCESS) return FALSE; scp->status |= SWITCH_WAIT_REL; PROC_LOCK(scp->proc); psignal(scp->proc, scp->smode.relsig); PROC_UNLOCK(scp->proc); DPRINTF(5, ("sending relsig to %d\n", scp->pid)); return TRUE; } static int signal_vt_acq(scr_stat *scp) { if (scp->smode.mode != VT_PROCESS) return FALSE; if (scp->sc->unit == sc_console_unit) cnavailable(sc_consptr, FALSE); scp->status |= SWITCH_WAIT_ACQ; PROC_LOCK(scp->proc); psignal(scp->proc, scp->smode.acqsig); PROC_UNLOCK(scp->proc); DPRINTF(5, ("sending acqsig to %d\n", scp->pid)); return TRUE; } static int finish_vt_rel(scr_stat *scp, int release, int *s) { if (scp == scp->sc->old_scp && scp->status & SWITCH_WAIT_REL) { scp->status &= ~SWITCH_WAIT_REL; if (release) *s = do_switch_scr(scp->sc, *s); else scp->sc->switch_in_progress = 0; return 0; } return EINVAL; } static int finish_vt_acq(scr_stat *scp) { if (scp == scp->sc->new_scp && scp->status & SWITCH_WAIT_ACQ) { scp->status &= ~SWITCH_WAIT_ACQ; scp->sc->switch_in_progress = 0; return 0; } return EINVAL; } static void exchange_scr(sc_softc_t *sc) { scr_stat *scp; /* save the current state of video and keyboard */ sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos); if (!ISGRAPHSC(sc->old_scp)) sc_remove_cursor_image(sc->old_scp); if (sc->old_scp->kbd_mode == K_XLATE) save_kbd_state(sc->old_scp); /* set up the video for the new screen */ scp = sc->cur_scp = sc->new_scp; #ifdef PC98 if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp) || ISUNKNOWNSC(sc->new_scp)) #else if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp)) #endif set_mode(scp); #ifndef __sparc64__ else sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize, (void *)sc->adp->va_window, FALSE); #endif scp->status |= MOUSE_HIDDEN; sc_move_cursor(scp, scp->xpos, scp->ypos); if (!ISGRAPHSC(scp)) sc_set_cursor_image(scp); #ifndef SC_NO_PALETTE_LOADING if (ISGRAPHSC(sc->old_scp)) load_palette(sc->adp, sc->palette); #endif sc_set_border(scp, scp->border); /* set up the keyboard for the new screen */ if (sc->old_scp->kbd_mode != scp->kbd_mode) - kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode); + kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode); update_kbd_state(scp, scp->status, LOCK_MASK); mark_all(scp); } void sc_puts(scr_stat *scp, u_char *buf, int len) { int need_unlock = 0; #ifdef DEV_SPLASH /* make screensaver happy */ if (!sticky_splash && scp == scp->sc->cur_scp && !sc_saver_keyb_only) run_scrn_saver = FALSE; #endif if (scp->tsw) { if (!kdb_active && !mtx_owned(&scp->scr_lock)) { need_unlock = 1; mtx_lock_spin(&scp->scr_lock); } (*scp->tsw->te_puts)(scp, buf, len); if (need_unlock) mtx_unlock_spin(&scp->scr_lock); } if (scp->sc->delayed_next_scr) sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1); } void sc_draw_cursor_image(scr_stat *scp) { /* assert(scp == scp->sc->cur_scp); */ SC_VIDEO_LOCK(scp->sc); (*scp->rndr->draw_cursor)(scp, scp->cursor_pos, scp->curs_attr.flags & CONS_BLINK_CURSOR, TRUE, sc_inside_cutmark(scp, scp->cursor_pos)); scp->cursor_oldpos = scp->cursor_pos; SC_VIDEO_UNLOCK(scp->sc); } void sc_remove_cursor_image(scr_stat *scp) { /* assert(scp == scp->sc->cur_scp); */ SC_VIDEO_LOCK(scp->sc); (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos, scp->curs_attr.flags & CONS_BLINK_CURSOR, FALSE, sc_inside_cutmark(scp, scp->cursor_oldpos)); SC_VIDEO_UNLOCK(scp->sc); } static void update_cursor_image(scr_stat *scp) { /* assert(scp == scp->sc->cur_scp); */ sc_remove_cursor_image(scp); sc_set_cursor_image(scp); sc_draw_cursor_image(scp); } void sc_set_cursor_image(scr_stat *scp) { scp->curs_attr.flags = scp->curr_curs_attr.flags; if (scp->curs_attr.flags & CONS_HIDDEN_CURSOR) { /* hidden cursor is internally represented as zero-height underline */ scp->curs_attr.flags = CONS_CHAR_CURSOR; scp->curs_attr.base = scp->curs_attr.height = 0; } else if (scp->curs_attr.flags & CONS_CHAR_CURSOR) { scp->curs_attr.base = imin(scp->curr_curs_attr.base, scp->font_size - 1); scp->curs_attr.height = imin(scp->curr_curs_attr.height, scp->font_size - scp->curs_attr.base); } else { /* block cursor */ scp->curs_attr.base = 0; scp->curs_attr.height = scp->font_size; } /* assert(scp == scp->sc->cur_scp); */ SC_VIDEO_LOCK(scp->sc); (*scp->rndr->set_cursor)(scp, scp->curs_attr.base, scp->curs_attr.height, scp->curs_attr.flags & CONS_BLINK_CURSOR); SC_VIDEO_UNLOCK(scp->sc); } static void change_cursor_shape(scr_stat *scp, int flags, int base, int height) { if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp)) sc_remove_cursor_image(scp); if (base >= 0) scp->curr_curs_attr.base = base; if (height >= 0) scp->curr_curs_attr.height = height; if (flags & CONS_RESET_CURSOR) scp->curr_curs_attr = scp->dflt_curs_attr; else scp->curr_curs_attr.flags = flags & CONS_CURSOR_ATTRS; if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp)) { sc_set_cursor_image(scp); sc_draw_cursor_image(scp); } } void sc_change_cursor_shape(scr_stat *scp, int flags, int base, int height) { sc_softc_t *sc; struct cdev *dev; int s; int i; s = spltty(); if ((flags != -1) && (flags & CONS_LOCAL_CURSOR)) { /* local (per vty) change */ change_cursor_shape(scp, flags, base, height); splx(s); return; } /* global change */ sc = scp->sc; if (base >= 0) sc->curs_attr.base = base; if (height >= 0) sc->curs_attr.height = height; if (flags != -1) { if (flags & CONS_RESET_CURSOR) sc->curs_attr = sc->dflt_curs_attr; else sc->curs_attr.flags = flags & CONS_CURSOR_ATTRS; } for (i = sc->first_vty; i < sc->first_vty + sc->vtys; ++i) { if ((dev = SC_DEV(sc, i)) == NULL) continue; if ((scp = sc_get_stat(dev)) == NULL) continue; scp->dflt_curs_attr = sc->curs_attr; change_cursor_shape(scp, CONS_RESET_CURSOR, -1, -1); } splx(s); } static void scinit(int unit, int flags) { /* * When syscons is being initialized as the kernel console, malloc() * is not yet functional, because various kernel structures has not been * fully initialized yet. Therefore, we need to declare the following * static buffers for the console. This is less than ideal, * but is necessry evil for the time being. XXX */ #ifdef PC98 static u_short sc_buffer[ROW*COL*2];/* XXX */ #else static u_short sc_buffer[ROW*COL]; /* XXX */ #endif #ifndef SC_NO_FONT_LOADING static u_char font_8[256*8]; static u_char font_14[256*14]; static u_char font_16[256*16]; #endif sc_softc_t *sc; scr_stat *scp; video_adapter_t *adp; int col; int row; int i; /* one time initialization */ if (init_done == COLD) sc_get_bios_values(&bios_value); init_done = WARM; /* * Allocate resources. Even if we are being called for the second * time, we must allocate them again, because they might have * disappeared... */ sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE); if ((sc->flags & SC_INIT_DONE) == 0) SC_VIDEO_LOCKINIT(sc); adp = NULL; if (sc->adapter >= 0) { vid_release(sc->adp, (void *)&sc->adapter); adp = sc->adp; sc->adp = NULL; } if (sc->keyboard >= 0) { DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard)); i = kbd_release(sc->kbd, (void *)&sc->keyboard); DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i)); if (sc->kbd != NULL) { DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n", unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags)); } sc->kbd = NULL; } sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter); sc->adp = vid_get_adapter(sc->adapter); /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */ sc->keyboard = sc_allocate_keyboard(sc, unit); DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard)); sc->kbd = kbd_get_keyboard(sc->keyboard); if (sc->kbd != NULL) { DPRINTF(1, ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n", unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags)); } if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) { sc->initial_mode = sc->adp->va_initial_mode; #ifndef SC_NO_FONT_LOADING if (flags & SC_KERNEL_CONSOLE) { sc->font_8 = font_8; sc->font_14 = font_14; sc->font_16 = font_16; } else if (sc->font_8 == NULL) { /* assert(sc_malloc) */ sc->font_8 = malloc(sizeof(font_8), M_DEVBUF, M_WAITOK); sc->font_14 = malloc(sizeof(font_14), M_DEVBUF, M_WAITOK); sc->font_16 = malloc(sizeof(font_16), M_DEVBUF, M_WAITOK); } #endif /* extract the hardware cursor location and hide the cursor for now */ (*vidsw[sc->adapter]->read_hw_cursor)(sc->adp, &col, &row); (*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, -1, -1); /* set up the first console */ sc->first_vty = unit*MAXCONS; sc->vtys = MAXCONS; /* XXX: should be configurable */ if (flags & SC_KERNEL_CONSOLE) { /* * Set up devs structure but don't use it yet, calling make_dev() * might panic kernel. Wait for sc_attach_unit() to actually * create the devices. */ sc->dev = main_devs; scp = &main_console; init_scp(sc, sc->first_vty, scp); sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize, (void *)sc_buffer, FALSE); if (sc_init_emulator(scp, SC_DFLT_TERM)) sc_init_emulator(scp, "*"); (*scp->tsw->te_default_attr)(scp, kernel_default.std_color, kernel_default.rev_color); } else { /* assert(sc_malloc) */ sc->dev = malloc(sizeof(struct cdev *)*sc->vtys, M_DEVBUF, M_WAITOK|M_ZERO); sc->dev[0] = make_dev(&sc_cdevsw, unit * MAXCONS, UID_ROOT, GID_WHEEL, 0600, "ttyv%r", unit * MAXCONS); sc_alloc_tty(sc->dev[0]); scp = alloc_scp(sc, sc->first_vty); SC_STAT(sc->dev[0]) = scp; } sc->cur_scp = scp; #ifndef __sparc64__ /* copy screen to temporary buffer */ sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize, (void *)scp->sc->adp->va_window, FALSE); if (ISTEXTSC(scp)) sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize); #endif /* move cursors to the initial positions */ if (col >= scp->xsize) col = 0; if (row >= scp->ysize) row = scp->ysize - 1; scp->xpos = col; scp->ypos = row; scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col; if (bios_value.cursor_end < scp->font_size) sc->dflt_curs_attr.base = scp->font_size - bios_value.cursor_end - 1; else sc->dflt_curs_attr.base = 0; i = bios_value.cursor_end - bios_value.cursor_start + 1; sc->dflt_curs_attr.height = imin(i, scp->font_size); sc->dflt_curs_attr.flags = 0; sc->curs_attr = sc->dflt_curs_attr; scp->curr_curs_attr = scp->dflt_curs_attr = sc->curs_attr; #ifndef SC_NO_SYSMOUSE sc_mouse_move(scp, scp->xpixel/2, scp->ypixel/2); #endif if (!ISGRAPHSC(scp)) { sc_set_cursor_image(scp); sc_draw_cursor_image(scp); } /* save font and palette */ #ifndef SC_NO_FONT_LOADING sc->fonts_loaded = 0; if (ISFONTAVAIL(sc->adp->va_flags)) { #ifdef SC_DFLT_FONT bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8)); bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14)); bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16)); sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8; if (scp->font_size < 14) { sc_load_font(scp, 0, 8, 8, sc->font_8, 0, 256); } else if (scp->font_size >= 16) { sc_load_font(scp, 0, 16, 8, sc->font_16, 0, 256); } else { sc_load_font(scp, 0, 14, 8, sc->font_14, 0, 256); } #else /* !SC_DFLT_FONT */ if (scp->font_size < 14) { sc_save_font(scp, 0, 8, 8, sc->font_8, 0, 256); sc->fonts_loaded = FONT_8; } else if (scp->font_size >= 16) { sc_save_font(scp, 0, 16, 8, sc->font_16, 0, 256); sc->fonts_loaded = FONT_16; } else { sc_save_font(scp, 0, 14, 8, sc->font_14, 0, 256); sc->fonts_loaded = FONT_14; } #endif /* SC_DFLT_FONT */ /* FONT KLUDGE: always use the font page #0. XXX */ sc_show_font(scp, 0); } #endif /* !SC_NO_FONT_LOADING */ #ifndef SC_NO_PALETTE_LOADING save_palette(sc->adp, sc->palette); #endif #ifdef DEV_SPLASH if (!(sc->flags & SC_SPLASH_SCRN)) { /* we are ready to put up the splash image! */ splash_init(sc->adp, scsplash_callback, sc); sc->flags |= SC_SPLASH_SCRN; } #endif } /* the rest is not necessary, if we have done it once */ if (sc->flags & SC_INIT_DONE) return; /* initialize mapscrn arrays to a one to one map */ for (i = 0; i < sizeof(sc->scr_map); i++) sc->scr_map[i] = sc->scr_rmap[i] = i; #ifdef PC98 sc->scr_map[0x5c] = (u_char)0xfc; /* for backslash */ #endif sc->flags |= SC_INIT_DONE; } static void scterm(int unit, int flags) { sc_softc_t *sc; scr_stat *scp; sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE); if (sc == NULL) return; /* shouldn't happen */ #ifdef DEV_SPLASH /* this console is no longer available for the splash screen */ if (sc->flags & SC_SPLASH_SCRN) { splash_term(sc->adp); sc->flags &= ~SC_SPLASH_SCRN; } #endif #if 0 /* XXX */ /* move the hardware cursor to the upper-left corner */ (*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, 0, 0); #endif /* release the keyboard and the video card */ if (sc->keyboard >= 0) kbd_release(sc->kbd, &sc->keyboard); if (sc->adapter >= 0) vid_release(sc->adp, &sc->adapter); /* stop the terminal emulator, if any */ scp = sc_get_stat(sc->dev[0]); if (scp->tsw) (*scp->tsw->te_term)(scp, &scp->ts); if (scp->ts != NULL) free(scp->ts, M_DEVBUF); mtx_destroy(&scp->scr_lock); /* clear the structure */ if (!(flags & SC_KERNEL_CONSOLE)) { /* XXX: We need delete_dev() for this */ free(sc->dev, M_DEVBUF); #if 0 /* XXX: We need a ttyunregister for this */ free(sc->tty, M_DEVBUF); #endif #ifndef SC_NO_FONT_LOADING free(sc->font_8, M_DEVBUF); free(sc->font_14, M_DEVBUF); free(sc->font_16, M_DEVBUF); #endif /* XXX vtb, history */ } bzero(sc, sizeof(*sc)); sc->keyboard = -1; sc->adapter = -1; } static void scshutdown(void *arg, int howto) { /* assert(sc_console != NULL) */ sc_touch_scrn_saver(); if (!cold && sc_console && sc_console->sc->cur_scp->smode.mode == VT_AUTO && sc_console->smode.mode == VT_AUTO) sc_switch_scr(sc_console->sc, sc_console->index); shutdown_in_progress = TRUE; } int sc_clean_up(scr_stat *scp) { #ifdef DEV_SPLASH int error; #endif if (scp->sc->flags & SC_SCRN_BLANKED) { sc_touch_scrn_saver(); #ifdef DEV_SPLASH if ((error = wait_scrn_saver_stop(scp->sc))) return error; #endif } scp->status |= MOUSE_HIDDEN; sc_remove_mouse_image(scp); sc_remove_cutmarking(scp); return 0; } void sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard) { sc_vtb_t new; sc_vtb_t old; old = scp->vtb; sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait); if (!discard && (old.vtb_flags & VTB_VALID)) { /* retain the current cursor position and buffer contants */ scp->cursor_oldpos = scp->cursor_pos; /* * This works only if the old buffer has the same size as or larger * than the new one. XXX */ sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize); scp->vtb = new; } else { scp->vtb = new; sc_vtb_destroy(&old); } #ifndef SC_NO_SYSMOUSE /* move the mouse cursor at the center of the screen */ sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2); #endif } static scr_stat *alloc_scp(sc_softc_t *sc, int vty) { scr_stat *scp; /* assert(sc_malloc) */ scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK); init_scp(sc, vty, scp); sc_alloc_scr_buffer(scp, TRUE, TRUE); if (sc_init_emulator(scp, SC_DFLT_TERM)) sc_init_emulator(scp, "*"); #ifndef SC_NO_CUTPASTE sc_alloc_cut_buffer(scp, TRUE); #endif #ifndef SC_NO_HISTORY sc_alloc_history_buffer(scp, 0, 0, TRUE); #endif return scp; } static void init_scp(sc_softc_t *sc, int vty, scr_stat *scp) { video_info_t info; bzero(scp, sizeof(*scp)); scp->index = vty; scp->sc = sc; scp->status = 0; scp->mode = sc->initial_mode; (*vidsw[sc->adapter]->get_info)(sc->adp, scp->mode, &info); if (info.vi_flags & V_INFO_GRAPHICS) { scp->status |= GRAPHICS_MODE; scp->xpixel = info.vi_width; scp->ypixel = info.vi_height; scp->xsize = info.vi_width/info.vi_cwidth; scp->ysize = info.vi_height/info.vi_cheight; scp->font_size = 0; scp->font = NULL; } else { scp->xsize = info.vi_width; scp->ysize = info.vi_height; scp->xpixel = scp->xsize*info.vi_cwidth; scp->ypixel = scp->ysize*info.vi_cheight; scp->font_size = info.vi_cheight; scp->font_width = info.vi_cwidth; if (info.vi_cheight < 14) { #ifndef SC_NO_FONT_LOADING scp->font = sc->font_8; #else scp->font = NULL; #endif } else if (info.vi_cheight >= 16) { #ifndef SC_NO_FONT_LOADING scp->font = sc->font_16; #else scp->font = NULL; #endif } else { #ifndef SC_NO_FONT_LOADING scp->font = sc->font_14; #else scp->font = NULL; #endif } } sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE); #ifndef __sparc64__ sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE); #endif scp->xoff = scp->yoff = 0; scp->xpos = scp->ypos = 0; scp->start = scp->xsize * scp->ysize - 1; scp->end = 0; scp->tsw = NULL; scp->ts = NULL; scp->rndr = NULL; scp->border = (SC_NORM_ATTR >> 4) & 0x0f; scp->curr_curs_attr = scp->dflt_curs_attr = sc->curs_attr; scp->mouse_cut_start = scp->xsize*scp->ysize; scp->mouse_cut_end = -1; scp->mouse_signal = 0; scp->mouse_pid = 0; scp->mouse_proc = NULL; scp->kbd_mode = K_XLATE; scp->bell_pitch = bios_value.bell_pitch; scp->bell_duration = BELL_DURATION; scp->status |= (bios_value.shift_state & NLKED); scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN; scp->pid = 0; scp->proc = NULL; scp->smode.mode = VT_AUTO; scp->history = NULL; scp->history_pos = 0; scp->history_size = 0; mtx_init(&scp->scr_lock, "scrlock", NULL, MTX_SPIN); } int sc_init_emulator(scr_stat *scp, char *name) { sc_term_sw_t *sw; sc_rndr_sw_t *rndr; void *p; int error; if (name == NULL) /* if no name is given, use the current emulator */ sw = scp->tsw; else /* ...otherwise find the named emulator */ sw = sc_term_match(name); if (sw == NULL) return EINVAL; rndr = NULL; if (strcmp(sw->te_renderer, "*") != 0) { rndr = sc_render_match(scp, sw->te_renderer, scp->status & (GRAPHICS_MODE | PIXEL_MODE)); } if (rndr == NULL) { rndr = sc_render_match(scp, scp->sc->adp->va_name, scp->status & (GRAPHICS_MODE | PIXEL_MODE)); if (rndr == NULL) return ENODEV; } if (sw == scp->tsw) { error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT); scp->rndr = rndr; scp->rndr->init(scp); sc_clear_screen(scp); /* assert(error == 0); */ return error; } if (sc_malloc && (sw->te_size > 0)) p = malloc(sw->te_size, M_DEVBUF, M_NOWAIT); else p = NULL; error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT); if (error) return error; if (scp->tsw) (*scp->tsw->te_term)(scp, &scp->ts); if (scp->ts != NULL) free(scp->ts, M_DEVBUF); scp->tsw = sw; scp->ts = p; scp->rndr = rndr; scp->rndr->init(scp); /* XXX */ (*sw->te_default_attr)(scp, user_default.std_color, user_default.rev_color); sc_clear_screen(scp); return 0; } /* * scgetc(flags) - get character from keyboard. * If flags & SCGETC_CN, then avoid harmful side effects. * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else * return NOKEY if there is nothing there. */ static u_int scgetc(sc_softc_t *sc, u_int flags) { scr_stat *scp; #ifndef SC_NO_HISTORY struct tty *tp; #endif u_int c; int this_scr; int f; int i; if (sc->kbd == NULL) return NOKEY; next_code: #if 1 /* I don't like this, but... XXX */ if (flags & SCGETC_CN) sccnupdate(sc->cur_scp); #endif scp = sc->cur_scp; /* first see if there is something in the keyboard port */ for (;;) { - c = kbd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK)); + c = kbdd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK)); if (c == ERRKEY) { if (!(flags & SCGETC_CN)) sc_bell(scp, bios_value.bell_pitch, BELL_DURATION); } else if (c == NOKEY) return c; else break; } /* make screensaver happy */ if (!(c & RELKEY)) sc_touch_scrn_saver(); if (!(flags & SCGETC_CN)) random_harvest(&c, sizeof(c), 1, 0, RANDOM_KEYBOARD); if (scp->kbd_mode != K_XLATE) return KEYCHAR(c); /* if scroll-lock pressed allow history browsing */ if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) { scp->status &= ~CURSOR_ENABLED; sc_remove_cursor_image(scp); #ifndef SC_NO_HISTORY if (!(scp->status & BUFFER_SAVED)) { scp->status |= BUFFER_SAVED; sc_hist_save(scp); } switch (c) { /* FIXME: key codes */ case SPCLKEY | FKEY | F(49): /* home key */ sc_remove_cutmarking(scp); sc_hist_home(scp); goto next_code; case SPCLKEY | FKEY | F(57): /* end key */ sc_remove_cutmarking(scp); sc_hist_end(scp); goto next_code; case SPCLKEY | FKEY | F(50): /* up arrow key */ sc_remove_cutmarking(scp); if (sc_hist_up_line(scp)) if (!(flags & SCGETC_CN)) sc_bell(scp, bios_value.bell_pitch, BELL_DURATION); goto next_code; case SPCLKEY | FKEY | F(58): /* down arrow key */ sc_remove_cutmarking(scp); if (sc_hist_down_line(scp)) if (!(flags & SCGETC_CN)) sc_bell(scp, bios_value.bell_pitch, BELL_DURATION); goto next_code; case SPCLKEY | FKEY | F(51): /* page up key */ sc_remove_cutmarking(scp); for (i=0; iysize; i++) if (sc_hist_up_line(scp)) { if (!(flags & SCGETC_CN)) sc_bell(scp, bios_value.bell_pitch, BELL_DURATION); break; } goto next_code; case SPCLKEY | FKEY | F(59): /* page down key */ sc_remove_cutmarking(scp); for (i=0; iysize; i++) if (sc_hist_down_line(scp)) { if (!(flags & SCGETC_CN)) sc_bell(scp, bios_value.bell_pitch, BELL_DURATION); break; } goto next_code; } #endif /* SC_NO_HISTORY */ } /* * Process and consume special keys here. Return a plain char code * or a char code with the META flag or a function key code. */ if (c & RELKEY) { /* key released */ /* goto next_code */ } else { /* key pressed */ if (c & SPCLKEY) { c &= ~SPCLKEY; switch (KEYCHAR(c)) { /* LOCKING KEYS */ case NLK: case CLK: case ALK: break; case SLK: - kbd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f); + kbdd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f); if (f & SLKED) { scp->status |= SLKED; } else { if (scp->status & SLKED) { scp->status &= ~SLKED; #ifndef SC_NO_HISTORY if (scp->status & BUFFER_SAVED) { if (!sc_hist_restore(scp)) sc_remove_cutmarking(scp); scp->status &= ~BUFFER_SAVED; scp->status |= CURSOR_ENABLED; sc_draw_cursor_image(scp); } tp = VIRTUAL_TTY(sc, scp->index); if (ISTTYOPEN(tp)) scstart(tp); #endif } } break; case PASTE: #ifndef SC_NO_CUTPASTE sc_mouse_paste(scp); #endif break; /* NON-LOCKING KEYS */ case NOP: case LSH: case RSH: case LCTR: case RCTR: case LALT: case RALT: case ASH: case META: break; case BTAB: if (!(sc->flags & SC_SCRN_BLANKED)) return c; break; case SPSC: #ifdef DEV_SPLASH /* force activatation/deactivation of the screen saver */ if (!(sc->flags & SC_SCRN_BLANKED)) { run_scrn_saver = TRUE; sc->scrn_time_stamp -= scrn_blank_time; } if (cold) { /* * While devices are being probed, the screen saver need * to be invoked explictly. XXX */ if (sc->flags & SC_SCRN_BLANKED) { scsplash_stick(FALSE); stop_scrn_saver(sc, current_saver); } else { if (!ISGRAPHSC(scp)) { scsplash_stick(TRUE); (*current_saver)(sc, TRUE); } } } #endif /* DEV_SPLASH */ break; case RBT: #ifndef SC_DISABLE_REBOOT if (enable_reboot) shutdown_nice(0); #endif break; case HALT: #ifndef SC_DISABLE_REBOOT if (enable_reboot) shutdown_nice(RB_HALT); #endif break; case PDWN: #ifndef SC_DISABLE_REBOOT if (enable_reboot) shutdown_nice(RB_HALT|RB_POWEROFF); #endif break; case SUSP: power_pm_suspend(POWER_SLEEP_STATE_SUSPEND); break; case STBY: power_pm_suspend(POWER_SLEEP_STATE_STANDBY); break; case DBG: #ifndef SC_DISABLE_KDBKEY if (enable_kdbkey) kdb_enter(KDB_WHY_BREAK, "manual escape to debugger"); #endif break; case PNC: if (enable_panic_key) panic("Forced by the panic key"); break; case NEXT: this_scr = scp->index; for (i = (this_scr - sc->first_vty + 1)%sc->vtys; sc->first_vty + i != this_scr; i = (i + 1)%sc->vtys) { struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i); if (ISTTYOPEN(tp)) { sc_switch_scr(scp->sc, sc->first_vty + i); break; } } break; case PREV: this_scr = scp->index; for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys; sc->first_vty + i != this_scr; i = (i + sc->vtys - 1)%sc->vtys) { struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i); if (ISTTYOPEN(tp)) { sc_switch_scr(scp->sc, sc->first_vty + i); break; } } break; default: if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) { sc_switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR); break; } /* assert(c & FKEY) */ if (!(sc->flags & SC_SCRN_BLANKED)) return c; break; } /* goto next_code */ } else { /* regular keys (maybe MKEY is set) */ if (!(sc->flags & SC_SCRN_BLANKED)) return c; } } goto next_code; } static int scmmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot) { scr_stat *scp; scp = sc_get_stat(dev); if (scp != scp->sc->cur_scp) return -1; return (*vidsw[scp->sc->adapter]->mmap)(scp->sc->adp, offset, paddr, nprot); } static int save_kbd_state(scr_stat *scp) { int state; int error; - error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state); + error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state); if (error == ENOIOCTL) error = ENODEV; if (error == 0) { scp->status &= ~LOCK_MASK; scp->status |= state; } return error; } static int update_kbd_state(scr_stat *scp, int new_bits, int mask) { int state; int error; if (mask != LOCK_MASK) { - error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state); + error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state); if (error == ENOIOCTL) error = ENODEV; if (error) return error; state &= ~mask; state |= new_bits & mask; } else { state = new_bits & LOCK_MASK; } - error = kbd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state); + error = kbdd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state); if (error == ENOIOCTL) error = ENODEV; return error; } static int update_kbd_leds(scr_stat *scp, int which) { int error; which &= LOCK_MASK; - error = kbd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which); + error = kbdd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which); if (error == ENOIOCTL) error = ENODEV; return error; } int set_mode(scr_stat *scp) { video_info_t info; /* reject unsupported mode */ if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, scp->mode, &info)) return 1; /* if this vty is not currently showing, do nothing */ if (scp != scp->sc->cur_scp) return 0; /* setup video hardware for the given mode */ (*vidsw[scp->sc->adapter]->set_mode)(scp->sc->adp, scp->mode); scp->rndr->init(scp); #ifndef __sparc64__ sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize, (void *)scp->sc->adp->va_window, FALSE); #endif #ifndef SC_NO_FONT_LOADING /* load appropriate font */ if (!(scp->status & GRAPHICS_MODE)) { if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) { if (scp->font_size < 14) { if (scp->sc->fonts_loaded & FONT_8) sc_load_font(scp, 0, 8, 8, scp->sc->font_8, 0, 256); } else if (scp->font_size >= 16) { if (scp->sc->fonts_loaded & FONT_16) sc_load_font(scp, 0, 16, 8, scp->sc->font_16, 0, 256); } else { if (scp->sc->fonts_loaded & FONT_14) sc_load_font(scp, 0, 14, 8, scp->sc->font_14, 0, 256); } /* * FONT KLUDGE: * This is an interim kludge to display correct font. * Always use the font page #0 on the video plane 2. * Somehow we cannot show the font in other font pages on * some video cards... XXX */ sc_show_font(scp, 0); } mark_all(scp); } #endif /* !SC_NO_FONT_LOADING */ sc_set_border(scp, scp->border); sc_set_cursor_image(scp); return 0; } void sc_set_border(scr_stat *scp, int color) { SC_VIDEO_LOCK(scp->sc); (*scp->rndr->draw_border)(scp, color); SC_VIDEO_UNLOCK(scp->sc); } #ifndef SC_NO_FONT_LOADING void sc_load_font(scr_stat *scp, int page, int size, int width, u_char *buf, int base, int count) { sc_softc_t *sc; sc = scp->sc; sc->font_loading_in_progress = TRUE; (*vidsw[sc->adapter]->load_font)(sc->adp, page, size, width, buf, base, count); sc->font_loading_in_progress = FALSE; } void sc_save_font(scr_stat *scp, int page, int size, int width, u_char *buf, int base, int count) { sc_softc_t *sc; sc = scp->sc; sc->font_loading_in_progress = TRUE; (*vidsw[sc->adapter]->save_font)(sc->adp, page, size, width, buf, base, count); sc->font_loading_in_progress = FALSE; } void sc_show_font(scr_stat *scp, int page) { (*vidsw[scp->sc->adapter]->show_font)(scp->sc->adp, page); } #endif /* !SC_NO_FONT_LOADING */ void sc_paste(scr_stat *scp, u_char *p, int count) { struct tty *tp; u_char *rmap; tp = VIRTUAL_TTY(scp->sc, scp->sc->cur_scp->index); if (!ISTTYOPEN(tp)) return; rmap = scp->sc->scr_rmap; for (; count > 0; --count) ttyld_rint(tp, rmap[*p++]); } void sc_bell(scr_stat *scp, int pitch, int duration) { if (cold || shutdown_in_progress || !enable_bell) return; if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL)) return; if (scp->sc->flags & SC_VISUAL_BELL) { if (scp->sc->blink_in_progress) return; scp->sc->blink_in_progress = 3; if (scp != scp->sc->cur_scp) scp->sc->blink_in_progress += 2; blink_screen(scp->sc->cur_scp); } else if (duration != 0 && pitch != 0) { if (scp != scp->sc->cur_scp) pitch *= 2; sysbeep(pitch, duration); } } static void blink_screen(void *arg) { scr_stat *scp = arg; struct tty *tp; if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) { scp->sc->blink_in_progress = 0; mark_all(scp); tp = VIRTUAL_TTY(scp->sc, scp->index); if (ISTTYOPEN(tp)) scstart(tp); if (scp->sc->delayed_next_scr) sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1); } else { (*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize, scp->sc->blink_in_progress & 1); scp->sc->blink_in_progress--; timeout(blink_screen, scp, hz / 10); } } /* * Until sc_attach_unit() gets called no dev structures will be available * to store the per-screen current status. This is the case when the * kernel is initially booting and needs access to its console. During * this early phase of booting the console's current status is kept in * one statically defined scr_stat structure, and any pointers to the * dev structures will be NULL. */ static scr_stat * sc_get_stat(struct cdev *devptr) { if (devptr == NULL) return (&main_console); return (SC_STAT(devptr)); } /* * Allocate active keyboard. Try to allocate "kbdmux" keyboard first, and, * if found, add all non-busy keyboards to "kbdmux". Otherwise look for * any keyboard. */ static int sc_allocate_keyboard(sc_softc_t *sc, int unit) { int idx0, idx; keyboard_t *k0, *k; keyboard_info_t ki; idx0 = kbd_allocate("kbdmux", -1, (void *)&sc->keyboard, sckbdevent, sc); if (idx0 != -1) { k0 = kbd_get_keyboard(idx0); for (idx = kbd_find_keyboard2("*", -1, 0); idx != -1; idx = kbd_find_keyboard2("*", -1, idx + 1)) { k = kbd_get_keyboard(idx); if (idx == idx0 || KBD_IS_BUSY(k)) continue; bzero(&ki, sizeof(ki)); strcpy(ki.kb_name, k->kb_name); ki.kb_unit = k->kb_unit; - kbd_ioctl(k0, KBADDKBD, (caddr_t) &ki); + kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki); } } else idx0 = kbd_allocate("*", unit, (void *)&sc->keyboard, sckbdevent, sc); return (idx0); } Index: head/sys/dev/syscons/syscons.h =================================================================== --- head/sys/dev/syscons/syscons.h (revision 174983) +++ head/sys/dev/syscons/syscons.h (revision 174984) @@ -1,685 +1,665 @@ /*- * Copyright (c) 1995-1998 Søren Schmidt * All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Sascha Wildner * * 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, * without modification, immediately at the beginning of the file. * 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. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * 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. * * $FreeBSD$ */ #ifndef _DEV_SYSCONS_SYSCONS_H_ #define _DEV_SYSCONS_SYSCONS_H_ #include #include /* machine-dependent part of the header */ #ifdef PC98 #include #elif defined(__i386__) /* nothing for the moment */ #endif /* default values for configuration options */ #ifndef MAXCONS #define MAXCONS 16 #endif #ifdef SC_NO_SYSMOUSE #undef SC_NO_CUTPASTE #define SC_NO_CUTPASTE 1 #endif #ifdef SC_NO_MODE_CHANGE #undef SC_PIXEL_MODE #endif /* Always load font data if the pixel (raster text) mode is to be used. */ #ifdef SC_PIXEL_MODE #undef SC_NO_FONT_LOADING #endif /* * If font data is not available, the `arrow'-shaped mouse cursor cannot * be drawn. Use the alternative drawing method. */ #ifdef SC_NO_FONT_LOADING #undef SC_ALT_MOUSE_IMAGE #define SC_ALT_MOUSE_IMAGE 1 #endif #ifndef SC_CURSOR_CHAR #define SC_CURSOR_CHAR (0x07) #endif #ifndef SC_MOUSE_CHAR #define SC_MOUSE_CHAR (0xd0) #endif #if SC_MOUSE_CHAR <= SC_CURSOR_CHAR && SC_CURSOR_CHAR < (SC_MOUSE_CHAR + 4) #undef SC_CURSOR_CHAR #define SC_CURSOR_CHAR (SC_MOUSE_CHAR + 4) #endif #ifndef SC_DEBUG_LEVEL #define SC_DEBUG_LEVEL 0 #endif #define DPRINTF(l, p) if (SC_DEBUG_LEVEL >= (l)) printf p #ifndef __sparc64__ #define SC_DRIVER_NAME "sc" #else /* * Use a different driver name on sparc64 so it does not get confused * with the system controller devices which are also termed 'sc' in OFW. */ #define SC_DRIVER_NAME "syscons" #endif #define SC_VTY(dev) minor(dev) #define SC_DEV(sc, vty) ((sc)->dev[(vty) - (sc)->first_vty]) #define SC_STAT(dev) (*((scr_stat **)&(dev)->si_drv1)) /* printable chars */ #ifndef PRINTABLE #define PRINTABLE(ch) ((ch) > 0x1b || ((ch) > 0x0d && (ch) < 0x1b) \ || (ch) < 0x07) #endif /* macros for "intelligent" screen update */ #define mark_for_update(scp, x) {\ if ((x) < scp->start) scp->start = (x);\ else if ((x) > scp->end) scp->end = (x);\ } #define mark_all(scp) {\ scp->start = 0;\ scp->end = scp->xsize * scp->ysize - 1;\ } /* vty status flags (scp->status) */ #define UNKNOWN_MODE 0x00010 /* unknown video mode */ #define SWITCH_WAIT_REL 0x00080 /* waiting for vty release */ #define SWITCH_WAIT_ACQ 0x00100 /* waiting for vty ack */ #define BUFFER_SAVED 0x00200 /* vty buffer is saved */ #define CURSOR_ENABLED 0x00400 /* text cursor is enabled */ #define MOUSE_MOVED 0x01000 /* mouse cursor has moved */ #define MOUSE_CUTTING 0x02000 /* mouse cursor is cutting text */ #define MOUSE_VISIBLE 0x04000 /* mouse cursor is showing */ #define GRAPHICS_MODE 0x08000 /* vty is in a graphics mode */ #define PIXEL_MODE 0x10000 /* vty is in a raster text mode */ #define SAVER_RUNNING 0x20000 /* screen saver is running */ #define VR_CURSOR_BLINK 0x40000 /* blinking text cursor */ #define VR_CURSOR_ON 0x80000 /* text cursor is on */ #define MOUSE_HIDDEN 0x100000 /* mouse cursor is temporarily hidden */ /* misc defines */ #define FALSE 0 #define TRUE 1 /* The following #defines are hard-coded for a maximum text resolution corresponding to a maximum framebuffer resolution of 1600x1200 with an 8x8 font... */ #define COL 200 #define ROW 150 #define PCBURST 128 #ifndef BELL_DURATION #define BELL_DURATION ((5 * hz + 99) / 100) #define BELL_PITCH 800 #endif /* virtual terminal buffer */ typedef struct sc_vtb { int vtb_flags; #define VTB_VALID (1 << 0) #define VTB_ALLOCED (1 << 1) int vtb_type; #define VTB_INVALID 0 #define VTB_MEMORY 1 #define VTB_FRAMEBUFFER 2 #define VTB_RINGBUFFER 3 int vtb_cols; int vtb_rows; int vtb_size; vm_offset_t vtb_buffer; int vtb_tail; /* valid for VTB_RINGBUFFER only */ } sc_vtb_t; /* text cursor attributes */ struct cursor_attr { int flags; int base; int height; }; /* softc */ struct keyboard; struct video_adapter; struct scr_stat; struct tty; typedef struct sc_softc { int unit; /* unit # */ int config; /* configuration flags */ #define SC_VESA800X600 (1 << 7) #define SC_AUTODETECT_KBD (1 << 8) #define SC_KERNEL_CONSOLE (1 << 9) int flags; /* status flags */ #define SC_VISUAL_BELL (1 << 0) #define SC_QUIET_BELL (1 << 1) #if 0 /* not used anymore */ #define SC_BLINK_CURSOR (1 << 2) #define SC_CHAR_CURSOR (1 << 3) #endif #define SC_MOUSE_ENABLED (1 << 4) #define SC_SCRN_IDLE (1 << 5) #define SC_SCRN_BLANKED (1 << 6) #define SC_SAVER_FAILED (1 << 7) #define SC_SCRN_VTYLOCK (1 << 8) #define SC_INIT_DONE (1 << 16) #define SC_SPLASH_SCRN (1 << 17) int keyboard; /* -1 if unavailable */ struct keyboard *kbd; int adapter; struct video_adapter *adp; int initial_mode; /* initial video mode */ int first_vty; int vtys; struct cdev **dev; struct scr_stat *cur_scp; struct scr_stat *new_scp; struct scr_stat *old_scp; int delayed_next_scr; char font_loading_in_progress; char switch_in_progress; char write_in_progress; char blink_in_progress; struct mtx video_mtx; long scrn_time_stamp; struct cursor_attr dflt_curs_attr; struct cursor_attr curs_attr; u_char scr_map[256]; u_char scr_rmap[256]; #ifdef _SC_MD_SOFTC_DECLARED_ sc_md_softc_t md; /* machine dependent vars */ #endif #ifndef SC_NO_PALETTE_LOADING u_char palette[256*3]; #endif #ifndef SC_NO_FONT_LOADING int fonts_loaded; #define FONT_8 2 #define FONT_14 4 #define FONT_16 8 #define FONT_22 8 u_char *font_8; u_char *font_14; u_char *font_16; u_char *font_22; #endif u_char cursor_char; u_char mouse_char; } sc_softc_t; /* virtual screen */ typedef struct scr_stat { int index; /* index of this vty */ struct sc_softc *sc; /* pointer to softc */ struct sc_rndr_sw *rndr; /* renderer */ #ifndef __sparc64__ sc_vtb_t scr; #endif sc_vtb_t vtb; int xpos; /* current X position */ int ypos; /* current Y position */ int xsize; /* X text size */ int ysize; /* Y text size */ int xpixel; /* X graphics size */ int ypixel; /* Y graphics size */ int xoff; /* X offset in pixel mode */ int yoff; /* Y offset in pixel mode */ u_char *font; /* current font */ int font_size; /* fontsize in Y direction */ int font_width; /* fontsize in X direction */ int start; /* modified area start */ int end; /* modified area end */ struct sc_term_sw *tsw; void *ts; int status; /* status (bitfield) */ int kbd_mode; /* keyboard I/O mode */ int cursor_pos; /* cursor buffer position */ int cursor_oldpos; /* cursor old buffer position */ u_short cursor_saveunder_char; /* saved char under cursor */ u_short cursor_saveunder_attr; /* saved attr under cursor */ struct cursor_attr dflt_curs_attr; struct cursor_attr curr_curs_attr; struct cursor_attr curs_attr; int mouse_pos; /* mouse buffer position */ int mouse_oldpos; /* mouse old buffer position */ short mouse_xpos; /* mouse x coordinate */ short mouse_ypos; /* mouse y coordinate */ short mouse_oldxpos; /* mouse previous x coordinate */ short mouse_oldypos; /* mouse previous y coordinate */ short mouse_buttons; /* mouse buttons */ int mouse_cut_start; /* mouse cut start pos */ int mouse_cut_end; /* mouse cut end pos */ struct proc *mouse_proc; /* proc* of controlling proc */ pid_t mouse_pid; /* pid of controlling proc */ int mouse_signal; /* signal # to report with */ u_short bell_duration; u_short bell_pitch; u_char border; /* border color */ int mode; /* mode */ pid_t pid; /* pid of controlling proc */ struct proc *proc; /* proc* of controlling proc */ struct vt_mode smode; /* switch mode */ sc_vtb_t *history; /* circular history buffer */ int history_pos; /* position shown on screen */ int history_size; /* size of history buffer */ int splash_save_mode; /* saved mode for splash screen */ int splash_save_status; /* saved status for splash screen */ struct mtx scr_lock; /* mutex for sc_puts() */ #ifdef _SCR_MD_STAT_DECLARED_ scr_md_stat_t md; /* machine dependent vars */ #endif } scr_stat; #ifndef SC_NORM_ATTR #define SC_NORM_ATTR (FG_LIGHTGREY | BG_BLACK) #endif #ifndef SC_NORM_REV_ATTR #define SC_NORM_REV_ATTR (FG_BLACK | BG_LIGHTGREY) #endif #ifndef SC_KERNEL_CONS_ATTR #define SC_KERNEL_CONS_ATTR (FG_WHITE | BG_BLACK) #endif #ifndef SC_KERNEL_CONS_REV_ATTR #define SC_KERNEL_CONS_REV_ATTR (FG_BLACK | BG_LIGHTGREY) #endif /* terminal emulator */ #ifndef SC_DFLT_TERM #define SC_DFLT_TERM "*" /* any */ #endif typedef int sc_term_init_t(scr_stat *scp, void **tcp, int code); #define SC_TE_COLD_INIT 0 #define SC_TE_WARM_INIT 1 typedef int sc_term_term_t(scr_stat *scp, void **tcp); typedef void sc_term_puts_t(scr_stat *scp, u_char *buf, int len); typedef int sc_term_ioctl_t(scr_stat *scp, struct tty *tp, u_long cmd, caddr_t data, int flag, struct thread *td); typedef int sc_term_reset_t(scr_stat *scp, int code); #define SC_TE_HARD_RESET 0 #define SC_TE_SOFT_RESET 1 typedef void sc_term_default_attr_t(scr_stat *scp, int norm, int rev); typedef void sc_term_clear_t(scr_stat *scp); typedef void sc_term_notify_t(scr_stat *scp, int event); #define SC_TE_NOTIFY_VTSWITCH_IN 0 #define SC_TE_NOTIFY_VTSWITCH_OUT 1 typedef int sc_term_input_t(scr_stat *scp, int c, struct tty *tp); typedef struct sc_term_sw { LIST_ENTRY(sc_term_sw) link; char *te_name; /* name of the emulator */ char *te_desc; /* description */ char *te_renderer; /* matching renderer */ size_t te_size; /* size of internal buffer */ int te_refcount; /* reference counter */ sc_term_init_t *te_init; sc_term_term_t *te_term; sc_term_puts_t *te_puts; sc_term_ioctl_t *te_ioctl; sc_term_reset_t *te_reset; sc_term_default_attr_t *te_default_attr; sc_term_clear_t *te_clear; sc_term_notify_t *te_notify; sc_term_input_t *te_input; } sc_term_sw_t; #define SCTERM_MODULE(name, sw) \ DATA_SET(scterm_set, sw); \ static int \ scterm_##name##_event(module_t mod, int type, void *data) \ { \ switch (type) { \ case MOD_LOAD: \ return sc_term_add(&sw); \ case MOD_UNLOAD: \ if (sw.te_refcount > 0) \ return EBUSY; \ return sc_term_remove(&sw); \ default: \ return EOPNOTSUPP; \ break; \ } \ return 0; \ } \ static moduledata_t scterm_##name##_mod = { \ "scterm-" #name, \ scterm_##name##_event, \ NULL, \ }; \ DECLARE_MODULE(scterm_##name, scterm_##name##_mod, \ SI_SUB_DRIVERS, SI_ORDER_MIDDLE) /* renderer function table */ typedef void vr_init_t(scr_stat *scp); typedef void vr_clear_t(scr_stat *scp, int c, int attr); typedef void vr_draw_border_t(scr_stat *scp, int color); typedef void vr_draw_t(scr_stat *scp, int from, int count, int flip); typedef void vr_set_cursor_t(scr_stat *scp, int base, int height, int blink); typedef void vr_draw_cursor_t(scr_stat *scp, int at, int blink, int on, int flip); typedef void vr_blink_cursor_t(scr_stat *scp, int at, int flip); typedef void vr_set_mouse_t(scr_stat *scp); typedef void vr_draw_mouse_t(scr_stat *scp, int x, int y, int on); typedef struct sc_rndr_sw { vr_init_t *init; vr_clear_t *clear; vr_draw_border_t *draw_border; vr_draw_t *draw; vr_set_cursor_t *set_cursor; vr_draw_cursor_t *draw_cursor; vr_blink_cursor_t *blink_cursor; vr_set_mouse_t *set_mouse; vr_draw_mouse_t *draw_mouse; } sc_rndr_sw_t; typedef struct sc_renderer { char *name; int mode; sc_rndr_sw_t *rndrsw; LIST_ENTRY(sc_renderer) link; } sc_renderer_t; #define RENDERER(name, mode, sw, set) \ static struct sc_renderer scrndr_##name##_##mode = { \ #name, mode, &sw \ }; \ DATA_SET(scrndr_set, scrndr_##name##_##mode); \ DATA_SET(set, scrndr_##name##_##mode) #define RENDERER_MODULE(name, set) \ SET_DECLARE(set, sc_renderer_t); \ static int \ scrndr_##name##_event(module_t mod, int type, void *data) \ { \ sc_renderer_t **list; \ int error = 0; \ switch (type) { \ case MOD_LOAD: \ SET_FOREACH(list, set) { \ error = sc_render_add(*list); \ if (error) \ break; \ } \ break; \ case MOD_UNLOAD: \ SET_FOREACH(list, set) { \ error = sc_render_remove(*list);\ if (error) \ break; \ } \ break; \ default: \ return EOPNOTSUPP; \ break; \ } \ return error; \ } \ static moduledata_t scrndr_##name##_mod = { \ "scrndr-" #name, \ scrndr_##name##_event, \ NULL, \ }; \ DECLARE_MODULE(scrndr_##name, scrndr_##name##_mod, \ SI_SUB_DRIVERS, SI_ORDER_MIDDLE) typedef struct { int cursor_start; int cursor_end; int shift_state; int bell_pitch; } bios_values_t; /* other macros */ #define ISTEXTSC(scp) (!((scp)->status \ & (UNKNOWN_MODE | GRAPHICS_MODE | PIXEL_MODE))) #define ISGRAPHSC(scp) (((scp)->status \ & (UNKNOWN_MODE | GRAPHICS_MODE))) #define ISPIXELSC(scp) (((scp)->status \ & (UNKNOWN_MODE | GRAPHICS_MODE | PIXEL_MODE))\ == PIXEL_MODE) #define ISUNKNOWNSC(scp) ((scp)->status & UNKNOWN_MODE) #define ISMOUSEAVAIL(af) ((af) & V_ADP_FONT) #define ISFONTAVAIL(af) ((af) & V_ADP_FONT) #define ISPALAVAIL(af) ((af) & V_ADP_PALETTE) #define ISSIGVALID(sig) ((sig) > 0 && (sig) < NSIG) -#define kbd_read_char(kbd, wait) \ - (*kbdsw[(kbd)->kb_index]->read_char)((kbd), (wait)) -#define kbd_check_char(kbd) \ - (*kbdsw[(kbd)->kb_index]->check_char)((kbd)) -#define kbd_enable(kbd) \ - (*kbdsw[(kbd)->kb_index]->enable)((kbd)) -#define kbd_disable(kbd) \ - (*kbdsw[(kbd)->kb_index]->disable)((kbd)) -#define kbd_lock(kbd, lockf) \ - (*kbdsw[(kbd)->kb_index]->lock)((kbd), (lockf)) -#define kbd_ioctl(kbd, cmd, arg) \ - (((kbd) == NULL) ? \ - ENODEV : (*kbdsw[(kbd)->kb_index]->ioctl)((kbd), (cmd), (arg))) -#define kbd_clear_state(kbd) \ - (*kbdsw[(kbd)->kb_index]->clear_state)((kbd)) -#define kbd_get_fkeystr(kbd, fkey, len) \ - (*kbdsw[(kbd)->kb_index]->get_fkeystr)((kbd), (fkey), (len)) -#define kbd_poll(kbd, on) \ - (*kbdsw[(kbd)->kb_index]->poll)((kbd), (on)) - #define SC_VIDEO_LOCKINIT(sc) \ mtx_init(&(sc)->video_mtx, "syscons video lock", NULL,MTX_SPIN); #define SC_VIDEO_LOCK(sc) \ do { \ if (!cold) \ mtx_lock_spin(&(sc)->video_mtx); \ } while(0) #define SC_VIDEO_UNLOCK(sc) \ do { \ if (!cold) \ mtx_unlock_spin(&(sc)->video_mtx); \ } while(0) /* syscons.c */ extern int (*sc_user_ioctl)(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td); int sc_probe_unit(int unit, int flags); int sc_attach_unit(int unit, int flags); int set_mode(scr_stat *scp); void sc_set_border(scr_stat *scp, int color); void sc_load_font(scr_stat *scp, int page, int size, int width, u_char *font, int base, int count); void sc_save_font(scr_stat *scp, int page, int size, int width, u_char *font, int base, int count); void sc_show_font(scr_stat *scp, int page); void sc_touch_scrn_saver(void); void sc_puts(scr_stat *scp, u_char *buf, int len); void sc_draw_cursor_image(scr_stat *scp); void sc_remove_cursor_image(scr_stat *scp); void sc_set_cursor_image(scr_stat *scp); void sc_change_cursor_shape(scr_stat *scp, int flags, int base, int height); int sc_clean_up(scr_stat *scp); int sc_switch_scr(sc_softc_t *sc, u_int next_scr); void sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard); int sc_init_emulator(scr_stat *scp, char *name); void sc_paste(scr_stat *scp, u_char *p, int count); void sc_bell(scr_stat *scp, int pitch, int duration); /* schistory.c */ #ifndef SC_NO_HISTORY int sc_alloc_history_buffer(scr_stat *scp, int lines, int prev_ysize, int wait); void sc_free_history_buffer(scr_stat *scp, int prev_ysize); void sc_hist_save(scr_stat *scp); #define sc_hist_save_one_line(scp, from) \ sc_vtb_append(&(scp)->vtb, (from), (scp)->history, (scp)->xsize) int sc_hist_restore(scr_stat *scp); void sc_hist_home(scr_stat *scp); void sc_hist_end(scr_stat *scp); int sc_hist_up_line(scr_stat *scp); int sc_hist_down_line(scr_stat *scp); int sc_hist_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct thread *td); #endif /* SC_NO_HISTORY */ /* scmouse.c */ #ifndef SC_NO_CUTPASTE void sc_alloc_cut_buffer(scr_stat *scp, int wait); void sc_draw_mouse_image(scr_stat *scp); void sc_remove_mouse_image(scr_stat *scp); int sc_inside_cutmark(scr_stat *scp, int pos); void sc_remove_cutmarking(scr_stat *scp); void sc_remove_all_cutmarkings(sc_softc_t *scp); void sc_remove_all_mouse(sc_softc_t *scp); void sc_mouse_paste(scr_stat *scp); #else #define sc_draw_mouse_image(scp) #define sc_remove_mouse_image(scp) #define sc_inside_cutmark(scp, pos) FALSE #define sc_remove_cutmarking(scp) #define sc_remove_all_cutmarkings(scp) #define sc_remove_all_mouse(scp) #define sc_mouse_paste(scp) #endif /* SC_NO_CUTPASTE */ #ifndef SC_NO_SYSMOUSE void sc_mouse_move(scr_stat *scp, int x, int y); int sc_mouse_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct thread *td); #endif /* SC_NO_SYSMOUSE */ /* scvidctl.c */ int sc_set_text_mode(scr_stat *scp, struct tty *tp, int mode, int xsize, int ysize, int fontsize, int font_width); int sc_set_graphics_mode(scr_stat *scp, struct tty *tp, int mode); int sc_set_pixel_mode(scr_stat *scp, struct tty *tp, int xsize, int ysize, int fontsize, int font_width); int sc_vid_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct thread *td); int sc_render_add(sc_renderer_t *rndr); int sc_render_remove(sc_renderer_t *rndr); sc_rndr_sw_t *sc_render_match(scr_stat *scp, char *name, int mode); /* scvtb.c */ void sc_vtb_init(sc_vtb_t *vtb, int type, int cols, int rows, void *buffer, int wait); void sc_vtb_destroy(sc_vtb_t *vtb); size_t sc_vtb_size(int cols, int rows); void sc_vtb_clear(sc_vtb_t *vtb, int c, int attr); int sc_vtb_getc(sc_vtb_t *vtb, int at); int sc_vtb_geta(sc_vtb_t *vtb, int at); void sc_vtb_putc(sc_vtb_t *vtb, int at, int c, int a); vm_offset_t sc_vtb_putchar(sc_vtb_t *vtb, vm_offset_t p, int c, int a); vm_offset_t sc_vtb_pointer(sc_vtb_t *vtb, int at); int sc_vtb_pos(sc_vtb_t *vtb, int pos, int offset); #define sc_vtb_tail(vtb) ((vtb)->vtb_tail) #define sc_vtb_rows(vtb) ((vtb)->vtb_rows) #define sc_vtb_cols(vtb) ((vtb)->vtb_cols) void sc_vtb_copy(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int to, int count); void sc_vtb_append(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int count); void sc_vtb_seek(sc_vtb_t *vtb, int pos); void sc_vtb_erase(sc_vtb_t *vtb, int at, int count, int c, int attr); void sc_vtb_move(sc_vtb_t *vtb, int from, int to, int count); void sc_vtb_delete(sc_vtb_t *vtb, int at, int count, int c, int attr); void sc_vtb_ins(sc_vtb_t *vtb, int at, int count, int c, int attr); /* sysmouse.c */ int sysmouse_event(mouse_info_t *info); /* scterm.c */ void sc_move_cursor(scr_stat *scp, int x, int y); void sc_clear_screen(scr_stat *scp); int sc_term_add(sc_term_sw_t *sw); int sc_term_remove(sc_term_sw_t *sw); sc_term_sw_t *sc_term_match(char *name); sc_term_sw_t *sc_term_match_by_number(int index); /* machine dependent functions */ int sc_max_unit(void); sc_softc_t *sc_get_softc(int unit, int flags); sc_softc_t *sc_find_softc(struct video_adapter *adp, struct keyboard *kbd); int sc_get_cons_priority(int *unit, int *flags); void sc_get_bios_values(bios_values_t *values); int sc_tone(int herz); #endif /* !_DEV_SYSCONS_SYSCONS_H_ */ Index: head/sys/dev/usb/ukbd.c =================================================================== --- head/sys/dev/usb/ukbd.c (revision 174983) +++ head/sys/dev/usb/ukbd.c (revision 174984) @@ -1,1538 +1,1538 @@ /*- * 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the NetBSD * Foundation, Inc. and its contributors. * 4. Neither the name of The NetBSD Foundation nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * 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. * */ #include __FBSDID("$FreeBSD$"); /* * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf */ #include "opt_compat.h" #include "opt_kbd.h" #include "opt_ukbd.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "usbdevs.h" #include #include #include #include #define UKBD_EMULATE_ATSCANCODE 1 #define DRIVER_NAME "ukbd" #define delay(d) DELAY(d) #ifdef USB_DEBUG #define DPRINTF(x) if (ukbddebug) printf x #define DPRINTFN(n,x) if (ukbddebug>(n)) printf x int ukbddebug = 0; SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd"); SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW, &ukbddebug, 0, "ukbd debug level"); #else #define DPRINTF(x) #define DPRINTFN(n,x) #endif #define UPROTO_BOOT_KEYBOARD 1 #define NKEYCODE 6 struct ukbd_data { u_int8_t modifiers; #define MOD_CONTROL_L 0x01 #define MOD_CONTROL_R 0x10 #define MOD_SHIFT_L 0x02 #define MOD_SHIFT_R 0x20 #define MOD_ALT_L 0x04 #define MOD_ALT_R 0x40 #define MOD_WIN_L 0x08 #define MOD_WIN_R 0x80 u_int8_t reserved; u_int8_t keycode[NKEYCODE]; }; #define MAXKEYS (NMOD+2*NKEYCODE) typedef struct ukbd_softc { device_t sc_dev; /* base device */ } ukbd_softc_t; #define UKBD_CHUNK 128 /* chunk size for read */ #define UKBD_BSIZE 1020 /* buffer size */ typedef void usbd_intr_t(usbd_xfer_handle, usbd_private_handle, usbd_status); typedef void usbd_disco_t(void *); static usbd_intr_t ukbd_intr; static int ukbd_driver_load(module_t mod, int what, void *arg); static device_probe_t ukbd_match; static device_attach_t ukbd_attach; static device_detach_t ukbd_detach; static device_resume_t ukbd_resume; static device_method_t ukbd_methods[] = { /* Device interface */ DEVMETHOD(device_probe, ukbd_match), DEVMETHOD(device_attach, ukbd_attach), DEVMETHOD(device_detach, ukbd_detach), DEVMETHOD(device_resume, ukbd_resume), { 0, 0 } }; static driver_t ukbd_driver = { "ukbd", ukbd_methods, sizeof(struct ukbd_softc) }; static devclass_t ukbd_devclass; MODULE_DEPEND(ukbd, usb, 1, 1, 1); DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0); static int ukbd_match(device_t self) { struct usb_attach_arg *uaa = device_get_ivars(self); keyboard_switch_t *sw; void *arg[2]; int unit = device_get_unit(self); sw = kbd_get_switch(DRIVER_NAME); if (sw == NULL) return (UMATCH_NONE); arg[0] = (void *)uaa; arg[1] = (void *)ukbd_intr; if ((*sw->probe)(unit, (void *)arg, 0)) return (UMATCH_NONE); if (usbd_get_quirks(uaa->device)->uq_flags & UQ_KBD_IGNORE) return (UMATCH_NONE); return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO); } static int ukbd_attach(device_t self) { struct ukbd_softc *sc = device_get_softc(self); struct usb_attach_arg *uaa = device_get_ivars(self); usbd_interface_handle iface = uaa->iface; usb_interface_descriptor_t *id; keyboard_switch_t *sw; keyboard_t *kbd; void *arg[2]; int unit = device_get_unit(self); sc->sc_dev = self; sw = kbd_get_switch(DRIVER_NAME); if (sw == NULL) return ENXIO; id = usbd_get_interface_descriptor(iface); arg[0] = (void *)uaa; arg[1] = (void *)ukbd_intr; kbd = NULL; if ((*sw->probe)(unit, (void *)arg, 0)) return ENXIO; if ((*sw->init)(unit, &kbd, (void *)arg, 0)) return ENXIO; (*sw->enable)(kbd); #ifdef KBD_INSTALL_CDEV if (kbd_attach(kbd)) return ENXIO; #endif if (bootverbose) (*sw->diag)(kbd, bootverbose); return 0; } int ukbd_detach(device_t self) { keyboard_t *kbd; int error; kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME, device_get_unit(self))); if (kbd == NULL) { DPRINTF(("%s: keyboard not attached!?\n", device_get_nameunit(self))); return ENXIO; } - (*kbdsw[kbd->kb_index]->disable)(kbd); + kbdd_disable(kbd); #ifdef KBD_INSTALL_CDEV error = kbd_detach(kbd); if (error) return error; #endif - error = (*kbdsw[kbd->kb_index]->term)(kbd); + error = kbdd_term(kbd); if (error) return error; DPRINTF(("%s: disconnected\n", device_get_nameunit(self))); return (0); } static int ukbd_resume(device_t self) { keyboard_t *kbd; kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME, device_get_unit(self))); if (kbd) - (*kbdsw[kbd->kb_index]->clear_state)(kbd); + kbdd_clear_state(kbd); return (0); } void ukbd_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status) { keyboard_t *kbd = (keyboard_t *)addr; - (*kbdsw[kbd->kb_index]->intr)(kbd, (void *)status); + kbdd_intr(kbd, (void *)status); } #define UKBD_DEFAULT 0 #define KEY_ERROR 0x01 #define KEY_PRESS 0 #define KEY_RELEASE 0x400 #define KEY_INDEX(c) ((c) & ~KEY_RELEASE) #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 NMOD 8 static struct { int mask, key; } ukbd_mods[NMOD] = { { MOD_CONTROL_L, 0xe0 }, { MOD_CONTROL_R, 0xe4 }, { MOD_SHIFT_L, 0xe1 }, { MOD_SHIFT_R, 0xe5 }, { MOD_ALT_L, 0xe2 }, { MOD_ALT_R, 0xe6 }, { MOD_WIN_L, 0xe3 }, { MOD_WIN_R, 0xe7 }, }; #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 */ static u_int8_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, 115, /* 80 - 87 */ 112, 125, 121, 123, NN, NN, NN, NN, /* 88 - 8F */ NN, NN, 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 */ }; typedef struct ukbd_state { usbd_interface_handle ks_iface; /* interface */ usbd_pipe_handle ks_intrpipe; /* interrupt pipe */ struct usb_attach_arg *ks_uaa; int ks_ep_addr; struct ukbd_data ks_ndata; struct ukbd_data ks_odata; u_long ks_ntime[NKEYCODE]; u_long ks_otime[NKEYCODE]; #define INPUTBUFSIZE (NMOD + 2*NKEYCODE) u_int ks_input[INPUTBUFSIZE]; /* input buffer */ int ks_inputs; int ks_inputhead; int ks_inputtail; int ks_ifstate; #define INTRENABLED (1 << 0) #define DISCONNECTED (1 << 1) struct callout ks_timeout_handle; 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) */ #ifdef UKBD_EMULATE_ATSCANCODE u_int ks_buffered_char[2]; u_int8_t ks_leds; /* store for async led requests */ #endif } ukbd_state_t; /* keyboard driver declaration */ static int ukbd_configure(int flags); static kbd_probe_t ukbd_probe; static kbd_init_t ukbd_init; static kbd_term_t ukbd_term; static kbd_intr_t ukbd_interrupt; static kbd_test_if_t ukbd_test_if; static kbd_enable_t ukbd_enable; static kbd_disable_t ukbd_disable; static kbd_read_t ukbd_read; static kbd_check_t ukbd_check; static kbd_read_char_t ukbd_read_char; static kbd_check_char_t ukbd_check_char; static kbd_ioctl_t ukbd_ioctl; static kbd_lock_t ukbd_lock; static kbd_clear_state_t ukbd_clear_state; static kbd_get_state_t ukbd_get_state; static kbd_set_state_t ukbd_set_state; static kbd_poll_mode_t ukbd_poll; keyboard_switch_t ukbdsw = { ukbd_probe, ukbd_init, ukbd_term, ukbd_interrupt, ukbd_test_if, ukbd_enable, ukbd_disable, ukbd_read, ukbd_check, ukbd_read_char, ukbd_check_char, ukbd_ioctl, ukbd_lock, ukbd_clear_state, ukbd_get_state, ukbd_set_state, genkbd_get_fkeystr, ukbd_poll, genkbd_diag, }; KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure); /* local functions */ static int ukbd_enable_intr(keyboard_t *kbd, int on, usbd_intr_t *func); static void ukbd_timeout(void *arg); static int ukbd_getc(ukbd_state_t *state, int wait); static int probe_keyboard(struct usb_attach_arg *uaa, int flags); static int init_keyboard(ukbd_state_t *state, int *type, int flags); static void set_leds(ukbd_state_t *state, int leds); static int set_typematic(keyboard_t *kbd, int code); #ifdef UKBD_EMULATE_ATSCANCODE static int keycode2scancode(int keycode, int shift, int up); #endif /* local variables */ /* 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 #include /* structures for the default keyboard */ static keyboard_t default_kbd; static ukbd_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 conole is initialized, this routine * may be called more than once!! */ static int ukbd_configure(int flags) { return 0; #if 0 /* not yet */ keyboard_t *kbd; device_t device; struct usb_attach_arg *uaa; void *arg[2]; device = devclass_get_device(ukbd_devclass, UKBD_DEFAULT); if (device == NULL) return 0; uaa = (struct usb_attach_arg *)device_get_ivars(device); if (uaa == NULL) return 0; /* probe the default keyboard */ arg[0] = (void *)uaa; arg[1] = (void *)ukbd_intr; kbd = NULL; if (ukbd_probe(UKBD_DEFAULT, arg, flags)) return 0; if (ukbd_init(UKBD_DEFAULT, &kbd, arg, flags)) return 0; /* return the number of found keyboards */ return 1; #endif } /* low-level functions */ /* detect a keyboard */ static int ukbd_probe(int unit, void *arg, int flags) { void **data; struct usb_attach_arg *uaa; data = (void **)arg; uaa = (struct usb_attach_arg *)data[0]; /* XXX */ if (unit == UKBD_DEFAULT) { if (KBD_IS_PROBED(&default_kbd)) return 0; } if (probe_keyboard(uaa, flags)) return ENXIO; return 0; } /* reset and initialize the device */ static int ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) { keyboard_t *kbd; ukbd_state_t *state; keymap_t *keymap; accentmap_t *accmap; fkeytab_t *fkeymap; int fkeymap_size; void **data = (void **)arg; struct usb_attach_arg *uaa = (struct usb_attach_arg *)data[0]; /* XXX */ if (unit == UKBD_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 = sizeof(default_fkeytab)/sizeof(default_fkeytab[0]); } else if (*kbdp == NULL) { *kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_NOWAIT); if (kbd == NULL) return ENOMEM; bzero(kbd, sizeof(*kbd)); state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT); 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]); if ((state == NULL) || (keymap == NULL) || (accmap == NULL) || (fkeymap == NULL)) { 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); free(kbd, M_DEVBUF); return ENOMEM; } } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) { return 0; } else { kbd = *kbdp; state = (ukbd_state_t *)kbd->kb_data; keymap = kbd->kb_keymap; accmap = kbd->kb_accentmap; fkeymap = kbd->kb_fkeytab; fkeymap_size = kbd->kb_fkeytab_size; } if (!KBD_IS_PROBED(kbd)) { kbd_init_struct(kbd, DRIVER_NAME, KB_OTHER, unit, flags, 0, 0); bzero(state, sizeof(*state)); 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(uaa, flags)) return ENXIO; else KBD_FOUND_DEVICE(kbd); ukbd_clear_state(kbd); state->ks_mode = K_XLATE; state->ks_iface = uaa->iface; state->ks_uaa = uaa; state->ks_ifstate = 0; callout_init(&state->ks_timeout_handle, 0); /* * 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)) { if (KBD_HAS_DEVICE(kbd) && init_keyboard((ukbd_state_t *)kbd->kb_data, &kbd->kb_type, kbd->kb_flags)) { kbd->kb_flags = 0; /* XXX: Missing free()'s */ return ENXIO; } ukbd_ioctl(kbd, KDSETLED, (caddr_t)&(state->ks_state)); KBD_INIT_DONE(kbd); } if (!KBD_IS_CONFIGURED(kbd)) { if (kbd_register(kbd) < 0) { kbd->kb_flags = 0; /* XXX: Missing free()'s */ return ENXIO; } if (ukbd_enable_intr(kbd, TRUE, (usbd_intr_t *)data[1]) == 0) ukbd_timeout((void *)kbd); KBD_CONFIG_DONE(kbd); } return 0; } static int ukbd_enable_intr(keyboard_t *kbd, int on, usbd_intr_t *func) { ukbd_state_t *state = (ukbd_state_t *)kbd->kb_data; usbd_status err; if (on) { /* Set up interrupt pipe. */ if (state->ks_ifstate & INTRENABLED) return EBUSY; state->ks_ifstate |= INTRENABLED; err = usbd_open_pipe_intr(state->ks_iface, state->ks_ep_addr, USBD_SHORT_XFER_OK, &state->ks_intrpipe, kbd, &state->ks_ndata, sizeof(state->ks_ndata), func, USBD_DEFAULT_INTERVAL); if (err) return (EIO); } else { /* Disable interrupts. */ usbd_abort_pipe(state->ks_intrpipe); usbd_close_pipe(state->ks_intrpipe); state->ks_ifstate &= ~INTRENABLED; } return (0); } /* finish using this keyboard */ static int ukbd_term(keyboard_t *kbd) { ukbd_state_t *state; int error; int s; s = splusb(); state = (ukbd_state_t *)kbd->kb_data; DPRINTF(("ukbd_term: ks_ifstate=0x%x\n", state->ks_ifstate)); callout_stop(&state->ks_timeout_handle); if (state->ks_ifstate & INTRENABLED) ukbd_enable_intr(kbd, FALSE, NULL); if (state->ks_ifstate & INTRENABLED) { splx(s); DPRINTF(("ukbd_term: INTRENABLED!\n")); return ENXIO; } error = kbd_unregister(kbd); DPRINTF(("ukbd_term: kbd_unregister() %d\n", error)); if (error == 0) { kbd->kb_flags = 0; if (kbd != &default_kbd) { free(kbd->kb_keymap, M_DEVBUF); free(kbd->kb_accentmap, M_DEVBUF); free(kbd->kb_fkeytab, M_DEVBUF); free(state, M_DEVBUF); free(kbd, M_DEVBUF); } } splx(s); return error; } /* keyboard interrupt routine */ static void ukbd_timeout(void *arg) { keyboard_t *kbd; ukbd_state_t *state; int s; kbd = (keyboard_t *)arg; state = (ukbd_state_t *)kbd->kb_data; s = splusb(); - (*kbdsw[kbd->kb_index]->intr)(kbd, (void *)USBD_NORMAL_COMPLETION); + kbdd_intr(kbd, (void *)USBD_NORMAL_COMPLETION); callout_reset(&state->ks_timeout_handle, hz / 40, ukbd_timeout, arg); splx(s); } static int ukbd_interrupt(keyboard_t *kbd, void *arg) { usbd_status status = (usbd_status)arg; ukbd_state_t *state; struct ukbd_data *ud; struct timeval tv; u_long now; int mod, omod; int key, c; int i, j; DPRINTFN(5, ("ukbd_intr: status=%d\n", status)); if (status == USBD_CANCELLED) return 0; state = (ukbd_state_t *)kbd->kb_data; ud = &state->ks_ndata; if (status != USBD_NORMAL_COMPLETION) { DPRINTF(("ukbd_intr: status=%d\n", status)); if (status == USBD_STALLED) usbd_clear_endpoint_stall_async(state->ks_intrpipe); return 0; } if (ud->keycode[0] == KEY_ERROR) return 0; /* ignore */ getmicrouptime(&tv); now = (u_long)tv.tv_sec*1000 + (u_long)tv.tv_usec/1000; #define ADDKEY1(c) \ if (state->ks_inputs < INPUTBUFSIZE) { \ state->ks_input[state->ks_inputtail] = (c); \ ++state->ks_inputs; \ state->ks_inputtail = (state->ks_inputtail + 1)%INPUTBUFSIZE; \ } mod = ud->modifiers; omod = state->ks_odata.modifiers; if (mod != omod) { for (i = 0; i < NMOD; i++) if (( mod & ukbd_mods[i].mask) != (omod & ukbd_mods[i].mask)) ADDKEY1(ukbd_mods[i].key | (mod & ukbd_mods[i].mask ? KEY_PRESS : KEY_RELEASE)); } /* Check for released keys. */ for (i = 0; i < NKEYCODE; i++) { key = state->ks_odata.keycode[i]; if (key == 0) continue; for (j = 0; j < NKEYCODE; j++) { if (ud->keycode[j] == 0) continue; if (key == ud->keycode[j]) goto rfound; } ADDKEY1(key | KEY_RELEASE); rfound: ; } /* Check for pressed keys. */ for (i = 0; i < NKEYCODE; i++) { key = ud->keycode[i]; if (key == 0) continue; state->ks_ntime[i] = now + kbd->kb_delay1; for (j = 0; j < NKEYCODE; j++) { if (state->ks_odata.keycode[j] == 0) continue; if (key == state->ks_odata.keycode[j]) { state->ks_ntime[i] = state->ks_otime[j]; if (state->ks_otime[j] > now) goto pfound; state->ks_ntime[i] = now + kbd->kb_delay2; break; } } ADDKEY1(key | KEY_PRESS); /* * If any other key is presently down, force its repeat to be * well in the future (100s). This makes the last key to be * pressed do the autorepeat. */ for (j = 0; j < NKEYCODE; j++) { if (j != i) state->ks_ntime[j] = now + 100 * 1000; } pfound: ; } state->ks_odata = *ud; bcopy(state->ks_ntime, state->ks_otime, sizeof(state->ks_ntime)); if (state->ks_inputs <= 0) return 0; #ifdef USB_DEBUG for (i = state->ks_inputhead, j = 0; j < state->ks_inputs; ++j, i = (i + 1)%INPUTBUFSIZE) { c = state->ks_input[i]; DPRINTF(("0x%x (%d) %s\n", c, c, (c & KEY_RELEASE) ? "released":"pressed")); } if (ud->modifiers) DPRINTF(("mod:0x%04x ", ud->modifiers)); for (i = 0; i < NKEYCODE; i++) { if (ud->keycode[i]) DPRINTF(("%d ", ud->keycode[i])); } DPRINTF(("\n")); #endif /* USB_DEBUG */ 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 it */ do { c = ukbd_read_char(kbd, FALSE); } while (c != NOKEY); } return 0; } static int ukbd_getc(ukbd_state_t *state, int wait) { int c; int s; if (state->ks_polling) { DPRINTFN(1,("ukbd_getc: polling\n")); s = splusb(); while (state->ks_inputs <= 0) { usbd_dopoll(state->ks_iface); if (wait == FALSE) break; } splx(s); } s = splusb(); if (state->ks_inputs <= 0) { c = -1; } else { c = state->ks_input[state->ks_inputhead]; --state->ks_inputs; state->ks_inputhead = (state->ks_inputhead + 1)%INPUTBUFSIZE; } splx(s); return c; } /* test the interface to the device */ static int ukbd_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 ukbd_enable(keyboard_t *kbd) { int s; s = splusb(); KBD_ACTIVATE(kbd); splx(s); return 0; } /* disallow the access to the device */ static int ukbd_disable(keyboard_t *kbd) { int s; s = splusb(); KBD_DEACTIVATE(kbd); splx(s); return 0; } /* read one byte from the keyboard if it's allowed */ static int ukbd_read(keyboard_t *kbd, int wait) { ukbd_state_t *state; int usbcode; #ifdef UKBD_EMULATE_ATSCANCODE int keycode; int scancode; #endif state = (ukbd_state_t *)kbd->kb_data; #ifdef UKBD_EMULATE_ATSCANCODE if (state->ks_buffered_char[0]) { scancode = state->ks_buffered_char[0]; if (scancode & SCAN_PREFIX) { state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX; return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); } else { state->ks_buffered_char[0] = state->ks_buffered_char[1]; state->ks_buffered_char[1] = 0; return scancode; } } #endif /* UKBD_EMULATE_ATSCANCODE */ /* XXX */ usbcode = ukbd_getc(state, wait); if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1)) return -1; ++kbd->kb_count; #ifdef UKBD_EMULATE_ATSCANCODE keycode = ukbd_trtab[KEY_INDEX(usbcode)]; if (keycode == NN) return -1; scancode = keycode2scancode(keycode, state->ks_ndata.modifiers, usbcode & KEY_RELEASE); if (scancode & SCAN_PREFIX) { if (scancode & SCAN_PREFIX_CTL) { state->ks_buffered_char[0] = 0x1d | (scancode & SCAN_RELEASE); /* Ctrl */ state->ks_buffered_char[1] = scancode & ~SCAN_PREFIX; } else if (scancode & SCAN_PREFIX_SHIFT) { state->ks_buffered_char[0] = 0x2a | (scancode & SCAN_RELEASE); /* Shift */ state->ks_buffered_char[1] = scancode & ~SCAN_PREFIX_SHIFT; } else { state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX; state->ks_buffered_char[1] = 0; } return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); } return scancode; #else /* !UKBD_EMULATE_ATSCANCODE */ return usbcode; #endif /* UKBD_EMULATE_ATSCANCODE */ } /* check if data is waiting */ static int ukbd_check(keyboard_t *kbd) { if (!KBD_IS_ACTIVE(kbd)) return FALSE; #ifdef UKBD_EMULATE_ATSCANCODE if (((ukbd_state_t *)kbd->kb_data)->ks_buffered_char[0]) return TRUE; #endif if (((ukbd_state_t *)kbd->kb_data)->ks_inputs > 0) return TRUE; return FALSE; } /* read char from the keyboard */ static u_int ukbd_read_char(keyboard_t *kbd, int wait) { ukbd_state_t *state; u_int action; int usbcode; int keycode; #ifdef UKBD_EMULATE_ATSCANCODE int scancode; #endif state = (ukbd_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; } #ifdef UKBD_EMULATE_ATSCANCODE /* do we have a pending raw scan code? */ if (state->ks_mode == K_RAW) { if (state->ks_buffered_char[0]) { scancode = state->ks_buffered_char[0]; if (scancode & SCAN_PREFIX) { state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX; return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); } else { state->ks_buffered_char[0] = state->ks_buffered_char[1]; state->ks_buffered_char[1] = 0; return scancode; } } } #endif /* UKBD_EMULATE_ATSCANCODE */ /* see if there is something in the keyboard port */ /* XXX */ usbcode = ukbd_getc(state, wait); if (usbcode == -1) return NOKEY; ++kbd->kb_count; #ifdef UKBD_EMULATE_ATSCANCODE /* USB key index -> key code -> AT scan code */ keycode = ukbd_trtab[KEY_INDEX(usbcode)]; if (keycode == NN) return NOKEY; /* return an AT scan code for the K_RAW mode */ if (state->ks_mode == K_RAW) { scancode = keycode2scancode(keycode, state->ks_ndata.modifiers, usbcode & KEY_RELEASE); if (scancode & SCAN_PREFIX) { if (scancode & SCAN_PREFIX_CTL) { state->ks_buffered_char[0] = 0x1d | (scancode & SCAN_RELEASE); state->ks_buffered_char[1] = scancode & ~SCAN_PREFIX; } else if (scancode & SCAN_PREFIX_SHIFT) { state->ks_buffered_char[0] = 0x2a | (scancode & SCAN_RELEASE); state->ks_buffered_char[1] = scancode & ~SCAN_PREFIX_SHIFT; } else { state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX; state->ks_buffered_char[1] = 0; } return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); } return scancode; } #else /* !UKBD_EMULATE_ATSCANCODE */ /* return the byte as is for the K_RAW mode */ if (state->ks_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 (state->ks_flags & COMPOSE) { state->ks_flags &= ~COMPOSE; if (state->ks_composed_char > UCHAR_MAX) state->ks_composed_char = 0; } } else { if (!(state->ks_flags & COMPOSE)) { state->ks_flags |= COMPOSE; state->ks_composed_char = 0; } } break; /* XXX: I don't like these... */ 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 (usbcode & KEY_RELEASE) keycode |= SCAN_RELEASE; if (state->ks_mode == K_CODE) return keycode; /* compose a character code */ if (state->ks_flags & COMPOSE) { switch (keycode) { /* 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 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 (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, SCAN_CHAR(keycode), keycode & SCAN_RELEASE, &state->ks_state, &state->ks_accents); if (action == NOKEY) goto next_code; else return action; } /* check if char is waiting */ static int ukbd_check_char(keyboard_t *kbd) { ukbd_state_t *state; if (!KBD_IS_ACTIVE(kbd)) return FALSE; state = (ukbd_state_t *)kbd->kb_data; if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) return TRUE; return ukbd_check(kbd); } /* some useful control functions */ static int ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) { /* trasnlate LED_XXX bits into the device specific bits */ static u_char ledmap[8] = { 0, 2, 1, 3, 4, 6, 5, 7, }; ukbd_state_t *state = kbd->kb_data; int s; int i; #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ defined(COMPAT_FREEBSD4) || defined(COMPAT_43) int ival; #endif s = splusb(); 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) { ukbd_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)) { set_leds(state, ledmap[i & LED_MASK]); /* XXX: error check? */ } 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 ukbd_ioctl(kbd, KDSETLED, arg); case KDSETREPEAT: /* set keyboard repeat rate (new interface) */ splx(s); if (!KBD_HAS_DEVICE(kbd)) return 0; if (((int *)arg)[1] < 0) return EINVAL; if (((int *)arg)[0] < 0) return EINVAL; else if (((int *)arg)[0] == 0) /* 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) */ splx(s); return set_typematic(kbd, *(int *)arg); case PIO_KEYMAP: /* set keyboard translation table */ 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); #ifdef USB_DEBUG case USB_SETDEBUG: ukbddebug = *(int *)arg; break; #endif } splx(s); return 0; } /* lock the access to the keyboard */ static int ukbd_lock(keyboard_t *kbd, int lock) { /* XXX ? */ return TRUE; } /* clear the internal state of the keyboard */ static void ukbd_clear_state(keyboard_t *kbd) { ukbd_state_t *state; state = (ukbd_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; #ifdef UKBD_EMULATE_ATSCANCODE state->ks_buffered_char[0] = 0; state->ks_buffered_char[1] = 0; #endif bzero(&state->ks_ndata, sizeof(state->ks_ndata)); bzero(&state->ks_odata, sizeof(state->ks_odata)); bzero(&state->ks_ntime, sizeof(state->ks_ntime)); bzero(&state->ks_otime, sizeof(state->ks_otime)); } /* save the internal state */ static int ukbd_get_state(keyboard_t *kbd, void *buf, size_t len) { if (len == 0) return sizeof(ukbd_state_t); if (len < sizeof(ukbd_state_t)) return -1; bcopy(kbd->kb_data, buf, sizeof(ukbd_state_t)); return 0; } /* set the internal state */ static int ukbd_set_state(keyboard_t *kbd, void *buf, size_t len) { if (len < sizeof(ukbd_state_t)) return ENOMEM; bcopy(buf, kbd->kb_data, sizeof(ukbd_state_t)); return 0; } static int ukbd_poll(keyboard_t *kbd, int on) { ukbd_state_t *state; usbd_device_handle dev; int s; state = (ukbd_state_t *)kbd->kb_data; usbd_interface2device_handle(state->ks_iface, &dev); s = splusb(); if (on) { ++state->ks_polling; if (state->ks_polling == 1) usbd_set_polling(dev, on); } else { --state->ks_polling; if (state->ks_polling == 0) usbd_set_polling(dev, on); } splx(s); return 0; } /* local functions */ static int probe_keyboard(struct usb_attach_arg *uaa, int flags) { usb_interface_descriptor_t *id; if (!uaa->iface) /* we attach to ifaces only */ return EINVAL; /* Check that this is a keyboard that speaks the boot protocol. */ id = usbd_get_interface_descriptor(uaa->iface); if (id && id->bInterfaceClass == UICLASS_HID && id->bInterfaceSubClass == UISUBCLASS_BOOT && id->bInterfaceProtocol == UPROTO_BOOT_KEYBOARD) return 0; /* found it */ return EINVAL; } static int init_keyboard(ukbd_state_t *state, int *type, int flags) { usb_endpoint_descriptor_t *ed; *type = KB_OTHER; state->ks_ifstate |= DISCONNECTED; ed = usbd_interface2endpoint_descriptor(state->ks_iface, 0); if (!ed) { printf("ukbd: could not read endpoint descriptor\n"); return EIO; } DPRINTFN(10,("ukbd:init_keyboard: \ bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n", ed->bLength, ed->bDescriptorType, UE_GET_ADDR(ed->bEndpointAddress), UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? "in":"out", UE_GET_XFERTYPE(ed->bmAttributes), UGETW(ed->wMaxPacketSize), ed->bInterval)); if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN || UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT) { printf("ukbd: unexpected endpoint\n"); return EINVAL; } /* Ignore if SETIDLE fails since it is not crucial. */ usbd_set_idle(state->ks_iface, 0, 0); state->ks_ep_addr = ed->bEndpointAddress; state->ks_ifstate &= ~DISCONNECTED; return 0; } static void set_leds(ukbd_state_t *state, int leds) { DPRINTF(("ukbd:set_leds: state=%p leds=%d\n", state, leds)); state->ks_leds = leds; usbd_set_report_async(state->ks_iface, UHID_OUTPUT_REPORT, 0, &state->ks_leds, 1); } static int set_typematic(keyboard_t *kbd, int code) { static int delays[] = { 250, 500, 750, 1000 }; static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63, 68, 76, 84, 92, 100, 110, 118, 126, 136, 152, 168, 184, 200, 220, 236, 252, 272, 304, 336, 368, 400, 440, 472, 504 }; if (code & ~0x7f) return EINVAL; kbd->kb_delay1 = delays[(code >> 5) & 3]; kbd->kb_delay2 = rates[code & 0x1f]; return 0; } #ifdef UKBD_EMULATE_ATSCANCODE static int keycode2scancode(int keycode, int shift, int up) { static int scan[] = { 0x1c, 0x1d, 0x35, 0x37 | SCAN_PREFIX_SHIFT, /* PrintScreen */ 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x46, /* XXX Pause/Break */ 0x5b, 0x5c, 0x5d, /* SUN TYPE 6 USB KEYBOARD */ 0x68, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x25, 0x1f, 0x1e, 0x20, }; int scancode; scancode = keycode; if ((keycode >= 89) && (keycode < 89 + sizeof(scan)/sizeof(scan[0]))) scancode = scan[keycode - 89] | SCAN_PREFIX_E0; /* Pause/Break */ if ((keycode == 104) && !(shift & (MOD_CONTROL_L | MOD_CONTROL_R))) scancode = 0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL; if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) scancode &= ~SCAN_PREFIX_SHIFT; return (scancode | (up ? SCAN_RELEASE : SCAN_PRESS)); } #endif /* UKBD_EMULATE_ATSCANCODE */ 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 usbd_driver_load(mod, what, 0); } Index: head/sys/dev/vkbd/vkbd.c =================================================================== --- head/sys/dev/vkbd/vkbd.c (revision 174983) +++ head/sys/dev/vkbd/vkbd.c (revision 174984) @@ -1,1384 +1,1383 @@ /* * vkbd.c */ /*- * Copyright (c) 2004 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: vkbd.c,v 1.20 2004/11/15 23:53:30 max Exp $ * $FreeBSD$ */ #include "opt_compat.h" #include "opt_kbd.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define DEVICE_NAME "vkbdctl" #define KEYBOARD_NAME "vkbd" MALLOC_DECLARE(M_VKBD); MALLOC_DEFINE(M_VKBD, KEYBOARD_NAME, "Virtual AT keyboard"); /***************************************************************************** ***************************************************************************** ** Keyboard state ***************************************************************************** *****************************************************************************/ /* * XXX * For now rely on Giant mutex to protect our data structures. * Just like the rest of keyboard drivers and syscons(4) do. */ #if 0 /* not yet */ #define VKBD_LOCK_DECL struct mtx ks_lock #define VKBD_LOCK_INIT(s) mtx_init(&(s)->ks_lock, "vkbd_lock", NULL, MTX_DEF|MTX_RECURSE) #define VKBD_LOCK_DESTROY(s) mtx_destroy(&(s)->ks_lock) #define VKBD_LOCK(s) mtx_lock(&(s)->ks_lock) #define VKBD_UNLOCK(s) mtx_unlock(&(s)->ks_lock) #define VKBD_LOCK_ASSERT(s, w) mtx_assert(&(s)->ks_lock, w) #define VKBD_SLEEP(s, f, d, t) \ msleep(&(s)->f, &(s)->ks_lock, PCATCH | (PZERO + 1), d, t) #else #define VKBD_LOCK_DECL #define VKBD_LOCK_INIT(s) #define VKBD_LOCK_DESTROY(s) #define VKBD_LOCK(s) #define VKBD_UNLOCK(s) #define VKBD_LOCK_ASSERT(s, w) #define VKBD_SLEEP(s, f, d, t) tsleep(&(s)->f, PCATCH | (PZERO + 1), d, t) #endif #define VKBD_KEYBOARD(d) \ kbd_get_keyboard(kbd_find_keyboard(KEYBOARD_NAME, dev2unit(d))) /* vkbd queue */ struct vkbd_queue { int q[VKBD_Q_SIZE]; /* queue */ int head; /* index of the first code */ int tail; /* index of the last code */ int cc; /* number of codes in queue */ }; typedef struct vkbd_queue vkbd_queue_t; /* vkbd state */ struct vkbd_state { struct cdev *ks_dev; /* control device */ struct selinfo ks_rsel; /* select(2) */ struct selinfo ks_wsel; vkbd_queue_t ks_inq; /* input key codes queue */ struct task ks_task; /* interrupt task */ int ks_flags; /* flags */ #define OPEN (1 << 0) /* control device is open */ #define COMPOSE (1 << 1) /* compose flag */ #define STATUS (1 << 2) /* status has changed */ #define TASK (1 << 3) /* interrupt task queued */ #define READ (1 << 4) /* read pending */ #define WRITE (1 << 5) /* write pending */ int ks_mode; /* K_XLATE, K_RAW, K_CODE */ int ks_polling; /* polling flag */ int ks_state; /* shift/lock key state */ int ks_accents; /* accent key index (> 0) */ u_int ks_composed_char; /* composed char code */ u_char ks_prefix; /* AT scan code prefix */ VKBD_LOCK_DECL; }; typedef struct vkbd_state vkbd_state_t; /***************************************************************************** ***************************************************************************** ** Character device ***************************************************************************** *****************************************************************************/ static void vkbd_dev_clone(void *, struct ucred *, char *, int, struct cdev **); static d_open_t vkbd_dev_open; static d_close_t vkbd_dev_close; static d_read_t vkbd_dev_read; static d_write_t vkbd_dev_write; static d_ioctl_t vkbd_dev_ioctl; static d_poll_t vkbd_dev_poll; static void vkbd_dev_intr(void *, int); static void vkbd_status_changed(vkbd_state_t *); static int vkbd_data_ready(vkbd_state_t *); static int vkbd_data_read(vkbd_state_t *, int); static struct cdevsw vkbd_dev_cdevsw = { .d_version = D_VERSION, .d_flags = D_PSEUDO | D_NEEDGIANT, .d_open = vkbd_dev_open, .d_close = vkbd_dev_close, .d_read = vkbd_dev_read, .d_write = vkbd_dev_write, .d_ioctl = vkbd_dev_ioctl, .d_poll = vkbd_dev_poll, .d_name = DEVICE_NAME, }; static struct clonedevs *vkbd_dev_clones = NULL; /* Clone device */ static void vkbd_dev_clone(void *arg, struct ucred *cred, char *name, int namelen, struct cdev **dev) { int unit; if (*dev != NULL) return; if (strcmp(name, DEVICE_NAME) == 0) unit = -1; else if (dev_stdclone(name, NULL, DEVICE_NAME, &unit) != 1) return; /* don't recognize the name */ /* find any existing device, or allocate new unit number */ if (clone_create(&vkbd_dev_clones, &vkbd_dev_cdevsw, &unit, dev, 0)) { *dev = make_dev(&vkbd_dev_cdevsw, unit2minor(unit), UID_ROOT, GID_WHEEL, 0600, DEVICE_NAME "%d", unit); if (*dev != NULL) { dev_ref(*dev); (*dev)->si_flags |= SI_CHEAPCLONE; } } } /* Open device */ static int vkbd_dev_open(struct cdev *dev, int flag, int mode, struct thread *td) { int unit = dev2unit(dev), error; keyboard_switch_t *sw = NULL; keyboard_t *kbd = NULL; vkbd_state_t *state = (vkbd_state_t *) dev->si_drv1; /* XXX FIXME: dev->si_drv1 locking */ if (state == NULL) { if ((sw = kbd_get_switch(KEYBOARD_NAME)) == NULL) return (ENXIO); if ((error = (*sw->probe)(unit, NULL, 0)) != 0 || (error = (*sw->init)(unit, &kbd, NULL, 0)) != 0) return (error); state = (vkbd_state_t *) kbd->kb_data; if ((error = (*sw->enable)(kbd)) != 0) { (*sw->term)(kbd); return (error); } #ifdef KBD_INSTALL_CDEV if ((error = kbd_attach(kbd)) != 0) { (*sw->disable)(kbd); (*sw->term)(kbd); return (error); } #endif /* def KBD_INSTALL_CDEV */ dev->si_drv1 = kbd->kb_data; } VKBD_LOCK(state); if (state->ks_flags & OPEN) { VKBD_UNLOCK(state); return (EBUSY); } state->ks_flags |= OPEN; state->ks_dev = dev; VKBD_UNLOCK(state); return (0); } /* Close device */ static int vkbd_dev_close(struct cdev *dev, int foo, int bar, struct thread *td) { keyboard_t *kbd = VKBD_KEYBOARD(dev); vkbd_state_t *state = NULL; if (kbd == NULL) return (ENXIO); if (kbd->kb_data == NULL || kbd->kb_data != dev->si_drv1) panic("%s: kbd->kb_data != dev->si_drv1\n", __func__); state = (vkbd_state_t *) kbd->kb_data; VKBD_LOCK(state); /* wait for interrupt task */ while (state->ks_flags & TASK) VKBD_SLEEP(state, ks_task, "vkbdc", 0); /* wakeup poll()ers */ selwakeuppri(&state->ks_rsel, PZERO + 1); selwakeuppri(&state->ks_wsel, PZERO + 1); state->ks_flags &= ~OPEN; state->ks_dev = NULL; state->ks_inq.head = state->ks_inq.tail = state->ks_inq.cc = 0; VKBD_UNLOCK(state); - (*kbdsw[kbd->kb_index]->disable)(kbd); + kbdd_disable(kbd); #ifdef KBD_INSTALL_CDEV kbd_detach(kbd); #endif /* def KBD_INSTALL_CDEV */ - (*kbdsw[kbd->kb_index]->term)(kbd); + kbdd_term(kbd); /* XXX FIXME: dev->si_drv1 locking */ dev->si_drv1 = NULL; return (0); } /* Read status */ static int vkbd_dev_read(struct cdev *dev, struct uio *uio, int flag) { keyboard_t *kbd = VKBD_KEYBOARD(dev); vkbd_state_t *state = NULL; vkbd_status_t status; int error; if (kbd == NULL) return (ENXIO); if (uio->uio_resid != sizeof(status)) return (EINVAL); if (kbd->kb_data == NULL || kbd->kb_data != dev->si_drv1) panic("%s: kbd->kb_data != dev->si_drv1\n", __func__); state = (vkbd_state_t *) kbd->kb_data; VKBD_LOCK(state); if (state->ks_flags & READ) { VKBD_UNLOCK(state); return (EALREADY); } state->ks_flags |= READ; again: if (state->ks_flags & STATUS) { state->ks_flags &= ~STATUS; status.mode = state->ks_mode; status.leds = KBD_LED_VAL(kbd); status.lock = state->ks_state & LOCK_MASK; status.delay = kbd->kb_delay1; status.rate = kbd->kb_delay2; bzero(status.reserved, sizeof(status.reserved)); error = uiomove(&status, sizeof(status), uio); } else { if (flag & O_NONBLOCK) { error = EWOULDBLOCK; goto done; } error = VKBD_SLEEP(state, ks_flags, "vkbdr", 0); if (error != 0) goto done; goto again; } done: state->ks_flags &= ~READ; VKBD_UNLOCK(state); return (error); } /* Write scancodes */ static int vkbd_dev_write(struct cdev *dev, struct uio *uio, int flag) { keyboard_t *kbd = VKBD_KEYBOARD(dev); vkbd_state_t *state = NULL; vkbd_queue_t *q = NULL; int error, avail, bytes; if (kbd == NULL) return (ENXIO); if (uio->uio_resid <= 0) return (EINVAL); if (kbd->kb_data == NULL || kbd->kb_data != dev->si_drv1) panic("%s: kbd->kb_data != dev->si_drv1\n", __func__); state = (vkbd_state_t *) kbd->kb_data; VKBD_LOCK(state); if (state->ks_flags & WRITE) { VKBD_UNLOCK(state); return (EALREADY); } state->ks_flags |= WRITE; error = 0; q = &state->ks_inq; while (uio->uio_resid >= sizeof(q->q[0])) { if (q->head == q->tail) { if (q->cc == 0) avail = sizeof(q->q)/sizeof(q->q[0]) - q->head; else avail = 0; /* queue must be full */ } else if (q->head < q->tail) avail = sizeof(q->q)/sizeof(q->q[0]) - q->tail; else avail = q->head - q->tail; if (avail == 0) { if (flag & O_NONBLOCK) { error = EWOULDBLOCK; break; } error = VKBD_SLEEP(state, ks_inq, "vkbdw", 0); if (error != 0) break; } else { bytes = avail * sizeof(q->q[0]); if (bytes > uio->uio_resid) { avail = uio->uio_resid / sizeof(q->q[0]); bytes = avail * sizeof(q->q[0]); } error = uiomove((void *) &q->q[q->tail], bytes, uio); if (error != 0) break; q->cc += avail; q->tail += avail; if (q->tail == sizeof(q->q)/sizeof(q->q[0])) q->tail = 0; /* queue interrupt task if needed */ if (!(state->ks_flags & TASK) && taskqueue_enqueue(taskqueue_swi_giant, &state->ks_task) == 0) state->ks_flags |= TASK; } } state->ks_flags &= ~WRITE; VKBD_UNLOCK(state); return (error); } /* Process ioctl */ static int vkbd_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) { keyboard_t *kbd = VKBD_KEYBOARD(dev); - return ((kbd == NULL)? ENXIO : - (*kbdsw[kbd->kb_index]->ioctl)(kbd, cmd, data)); + return ((kbd == NULL)? ENXIO : kbdd_ioctl(kbd, cmd, data)); } /* Poll device */ static int vkbd_dev_poll(struct cdev *dev, int events, struct thread *td) { vkbd_state_t *state = (vkbd_state_t *) dev->si_drv1; vkbd_queue_t *q = NULL; int revents = 0; if (state == NULL) return (ENXIO); VKBD_LOCK(state); q = &state->ks_inq; if (events & (POLLIN | POLLRDNORM)) { if (state->ks_flags & STATUS) revents |= events & (POLLIN | POLLRDNORM); else selrecord(td, &state->ks_rsel); } if (events & (POLLOUT | POLLWRNORM)) { if (q->cc < sizeof(q->q)/sizeof(q->q[0])) revents |= events & (POLLOUT | POLLWRNORM); else selrecord(td, &state->ks_wsel); } VKBD_UNLOCK(state); return (revents); } /* Interrupt handler */ void vkbd_dev_intr(void *xkbd, int pending) { keyboard_t *kbd = (keyboard_t *) xkbd; vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; - (*kbdsw[kbd->kb_index]->intr)(kbd, NULL); + kbdd_intr(kbd, NULL); VKBD_LOCK(state); state->ks_flags &= ~TASK; wakeup(&state->ks_task); VKBD_UNLOCK(state); } /* Set status change flags */ static void vkbd_status_changed(vkbd_state_t *state) { VKBD_LOCK_ASSERT(state, MA_OWNED); if (!(state->ks_flags & STATUS)) { state->ks_flags |= STATUS; selwakeuppri(&state->ks_rsel, PZERO + 1); wakeup(&state->ks_flags); } } /* Check if we have data in the input queue */ static int vkbd_data_ready(vkbd_state_t *state) { VKBD_LOCK_ASSERT(state, MA_OWNED); return (state->ks_inq.cc > 0); } /* Read one code from the input queue */ static int vkbd_data_read(vkbd_state_t *state, int wait) { vkbd_queue_t *q = &state->ks_inq; int c; VKBD_LOCK_ASSERT(state, MA_OWNED); if (q->cc == 0) return (-1); /* get first code from the queue */ q->cc --; c = q->q[q->head ++]; if (q->head == sizeof(q->q)/sizeof(q->q[0])) q->head = 0; /* wakeup ks_inq writers/poll()ers */ selwakeuppri(&state->ks_wsel, PZERO + 1); wakeup(q); return (c); } /**************************************************************************** **************************************************************************** ** Keyboard driver **************************************************************************** ****************************************************************************/ static int vkbd_configure(int flags); static kbd_probe_t vkbd_probe; static kbd_init_t vkbd_init; static kbd_term_t vkbd_term; static kbd_intr_t vkbd_intr; static kbd_test_if_t vkbd_test_if; static kbd_enable_t vkbd_enable; static kbd_disable_t vkbd_disable; static kbd_read_t vkbd_read; static kbd_check_t vkbd_check; static kbd_read_char_t vkbd_read_char; static kbd_check_char_t vkbd_check_char; static kbd_ioctl_t vkbd_ioctl; static kbd_lock_t vkbd_lock; static void vkbd_clear_state_locked(vkbd_state_t *state); static kbd_clear_state_t vkbd_clear_state; static kbd_get_state_t vkbd_get_state; static kbd_set_state_t vkbd_set_state; static kbd_poll_mode_t vkbd_poll; static keyboard_switch_t vkbdsw = { .probe = vkbd_probe, .init = vkbd_init, .term = vkbd_term, .intr = vkbd_intr, .test_if = vkbd_test_if, .enable = vkbd_enable, .disable = vkbd_disable, .read = vkbd_read, .check = vkbd_check, .read_char = vkbd_read_char, .check_char = vkbd_check_char, .ioctl = vkbd_ioctl, .lock = vkbd_lock, .clear_state = vkbd_clear_state, .get_state = vkbd_get_state, .set_state = vkbd_set_state, .get_fkeystr = genkbd_get_fkeystr, .poll = vkbd_poll, .diag = genkbd_diag, }; static int typematic(int delay, int rate); static int typematic_delay(int delay); static int typematic_rate(int rate); /* Return the number of found keyboards */ static int vkbd_configure(int flags) { return (1); } /* Detect a keyboard */ static int vkbd_probe(int unit, void *arg, int flags) { return (0); } /* Reset and initialize the keyboard (stolen from atkbd.c) */ static int vkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) { keyboard_t *kbd = NULL; vkbd_state_t *state = NULL; keymap_t *keymap = NULL; accentmap_t *accmap = NULL; fkeytab_t *fkeymap = NULL; int fkeymap_size, delay[2]; int error, needfree; if (*kbdp == NULL) { *kbdp = kbd = malloc(sizeof(*kbd), M_VKBD, M_NOWAIT | M_ZERO); state = malloc(sizeof(*state), M_VKBD, M_NOWAIT | M_ZERO); keymap = malloc(sizeof(key_map), M_VKBD, M_NOWAIT); accmap = malloc(sizeof(accent_map), M_VKBD, M_NOWAIT); fkeymap = malloc(sizeof(fkey_tab), M_VKBD, 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; } VKBD_LOCK_INIT(state); state->ks_inq.head = state->ks_inq.tail = state->ks_inq.cc = 0; TASK_INIT(&state->ks_task, 0, vkbd_dev_intr, (void *) kbd); } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) { return (0); } else { kbd = *kbdp; state = (vkbd_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)) { kbd_init_struct(kbd, KEYBOARD_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; KBD_FOUND_DEVICE(kbd); KBD_PROBE_DONE(kbd); VKBD_LOCK(state); vkbd_clear_state_locked(state); state->ks_mode = K_XLATE; /* FIXME: set the initial value for lock keys in ks_state */ VKBD_UNLOCK(state); } if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) { kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY; vkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state); delay[0] = kbd->kb_delay1; delay[1] = kbd->kb_delay2; vkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); 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_VKBD); if (keymap != NULL) free(keymap, M_VKBD); if (accmap != NULL) free(accmap, M_VKBD); if (fkeymap != NULL) free(fkeymap, M_VKBD); if (kbd != NULL) { free(kbd, M_VKBD); *kbdp = NULL; /* insure ref doesn't leak to caller */ } } return (error); } /* Finish using this keyboard */ static int vkbd_term(keyboard_t *kbd) { vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; kbd_unregister(kbd); VKBD_LOCK_DESTROY(state); bzero(state, sizeof(*state)); free(state, M_VKBD); free(kbd->kb_keymap, M_VKBD); free(kbd->kb_accentmap, M_VKBD); free(kbd->kb_fkeytab, M_VKBD); free(kbd, M_VKBD); return (0); } /* Keyboard interrupt routine */ static int vkbd_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 = vkbd_read_char(kbd, FALSE); } while (c != NOKEY); } return (0); } /* Test the interface to the device */ static int vkbd_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 vkbd_enable(keyboard_t *kbd) { KBD_ACTIVATE(kbd); return (0); } /* Disallow the access to the device */ static int vkbd_disable(keyboard_t *kbd) { KBD_DEACTIVATE(kbd); return (0); } /* Read one byte from the keyboard if it's allowed */ static int vkbd_read(keyboard_t *kbd, int wait) { vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; int c; VKBD_LOCK(state); c = vkbd_data_read(state, wait); VKBD_UNLOCK(state); if (c != -1) kbd->kb_count ++; return (KBD_IS_ACTIVE(kbd)? c : -1); } /* Check if data is waiting */ static int vkbd_check(keyboard_t *kbd) { vkbd_state_t *state = NULL; int ready; if (!KBD_IS_ACTIVE(kbd)) return (FALSE); state = (vkbd_state_t *) kbd->kb_data; VKBD_LOCK(state); ready = vkbd_data_ready(state); VKBD_UNLOCK(state); return (ready); } /* Read char from the keyboard (stolen from atkbd.c) */ static u_int vkbd_read_char(keyboard_t *kbd, int wait) { vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; u_int action; int scancode, keycode; VKBD_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) { VKBD_UNLOCK(state); return (ERRKEY); } VKBD_UNLOCK(state); return (action); } /* see if there is something in the keyboard port */ scancode = vkbd_data_read(state, wait); if (scancode == -1) { VKBD_UNLOCK(state); return (NOKEY); } /* XXX FIXME: check for -1 if wait == 1! */ kbd->kb_count ++; /* return the byte as is for the K_RAW mode */ if (state->ks_mode == K_RAW) { VKBD_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; 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) { VKBD_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) { VKBD_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) { VKBD_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) { VKBD_UNLOCK(state); return (ERRKEY); } goto next_code; case 0x52: /* keypad 0 */ state->ks_composed_char *= 10; if (state->ks_composed_char > UCHAR_MAX) { VKBD_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; VKBD_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; VKBD_UNLOCK(state); return (action); } /* Check if char is waiting */ static int vkbd_check_char(keyboard_t *kbd) { vkbd_state_t *state = NULL; int ready; if (!KBD_IS_ACTIVE(kbd)) return (FALSE); state = (vkbd_state_t *) kbd->kb_data; VKBD_LOCK(state); if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) ready = TRUE; else ready = vkbd_data_ready(state); VKBD_UNLOCK(state); return (ready); } /* Some useful control functions (stolen from atkbd.c) */ static int vkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) { vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; int i; #ifdef COMPAT_FREEBSD6 int ival; #endif VKBD_LOCK(state); switch (cmd) { case KDGKBMODE: /* get keyboard mode */ *(int *)arg = state->ks_mode; break; #ifdef COMPAT_FREEBSD6 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); vkbd_status_changed(state); } /* FALLTHROUGH */ case K_RAW: case K_CODE: if (state->ks_mode != *(int *)arg) { vkbd_clear_state_locked(state); state->ks_mode = *(int *)arg; vkbd_status_changed(state); } break; default: VKBD_UNLOCK(state); return (EINVAL); } break; case KDGETLED: /* get keyboard LED */ *(int *)arg = KBD_LED_VAL(kbd); break; #ifdef COMPAT_FREEBSD6 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) { VKBD_UNLOCK(state); 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; } KBD_LED_VAL(kbd) = *(int *)arg; vkbd_status_changed(state); break; case KDGKBSTATE: /* get lock key state */ *(int *)arg = state->ks_state & LOCK_MASK; break; #ifdef COMPAT_FREEBSD6 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) { VKBD_UNLOCK(state); return (EINVAL); } state->ks_state &= ~LOCK_MASK; state->ks_state |= *(int *)arg; vkbd_status_changed(state); VKBD_UNLOCK(state); /* set LEDs and quit */ return (vkbd_ioctl(kbd, KDSETLED, arg)); case KDSETREPEAT: /* set keyboard repeat rate (new interface) */ i = typematic(((int *)arg)[0], ((int *)arg)[1]); kbd->kb_delay1 = typematic_delay(i); kbd->kb_delay2 = typematic_rate(i); vkbd_status_changed(state); break; #ifdef COMPAT_FREEBSD6 case _IO('K', 67): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSETRAD: /* set keyboard repeat rate (old interface) */ kbd->kb_delay1 = typematic_delay(*(int *)arg); kbd->kb_delay2 = typematic_rate(*(int *)arg); vkbd_status_changed(state); break; case PIO_KEYMAP: /* set keyboard translation table */ case PIO_KEYMAPENT: /* set keyboard translation table entry */ case PIO_DEADKEYMAP: /* set accent key translation table */ state->ks_accents = 0; /* FALLTHROUGH */ default: VKBD_UNLOCK(state); return (genkbd_commonioctl(kbd, cmd, arg)); } VKBD_UNLOCK(state); return (0); } /* Lock the access to the keyboard */ static int vkbd_lock(keyboard_t *kbd, int lock) { return (1); /* XXX */ } /* Clear the internal state of the keyboard */ static void vkbd_clear_state_locked(vkbd_state_t *state) { VKBD_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 */ /* flush ks_inq and wakeup writers/poll()ers */ state->ks_inq.head = state->ks_inq.tail = state->ks_inq.cc = 0; selwakeuppri(&state->ks_wsel, PZERO + 1); wakeup(&state->ks_inq); } static void vkbd_clear_state(keyboard_t *kbd) { vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; VKBD_LOCK(state); vkbd_clear_state_locked(state); VKBD_UNLOCK(state); } /* Save the internal state */ static int vkbd_get_state(keyboard_t *kbd, void *buf, size_t len) { if (len == 0) return (sizeof(vkbd_state_t)); if (len < sizeof(vkbd_state_t)) return (-1); bcopy(kbd->kb_data, buf, sizeof(vkbd_state_t)); /* XXX locking? */ return (0); } /* Set the internal state */ static int vkbd_set_state(keyboard_t *kbd, void *buf, size_t len) { if (len < sizeof(vkbd_state_t)) return (ENOMEM); bcopy(buf, kbd->kb_data, sizeof(vkbd_state_t)); /* XXX locking? */ return (0); } /* Set polling */ static int vkbd_poll(keyboard_t *kbd, int on) { vkbd_state_t *state = NULL; state = (vkbd_state_t *) kbd->kb_data; VKBD_LOCK(state); if (on) state->ks_polling ++; else state->ks_polling --; VKBD_UNLOCK(state); return (0); } /* * Local functions */ 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 = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; i --) { if (delay >= delays[i]) break; } value = i << 5; for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; i --) { if (rate >= rates[i]) break; } value |= i; return (value); } /***************************************************************************** ***************************************************************************** ** Module ***************************************************************************** *****************************************************************************/ KEYBOARD_DRIVER(vkbd, vkbdsw, vkbd_configure); static int vkbd_modevent(module_t mod, int type, void *data) { static eventhandler_tag tag; switch (type) { case MOD_LOAD: clone_setup(&vkbd_dev_clones); tag = EVENTHANDLER_REGISTER(dev_clone, vkbd_dev_clone, 0, 1000); if (tag == NULL) { clone_cleanup(&vkbd_dev_clones); return (ENOMEM); } kbd_add_driver(&vkbd_kbd_driver); break; case MOD_UNLOAD: kbd_delete_driver(&vkbd_kbd_driver); EVENTHANDLER_DEREGISTER(dev_clone, tag); clone_cleanup(&vkbd_dev_clones); break; default: return (EOPNOTSUPP); } return (0); } DEV_MODULE(vkbd, vkbd_modevent, NULL);