From 9ae7952cc5055a49f0c0d80ce1d1e5535da26c0a Mon Sep 17 00:00:00 2001 From: kevans Date: Fri, 20 Dec 2019 17:15:09 +0000 Subject: [PATCH] vt: store a pointer to the keyboard instead of index This effectively reverts r355935, but is functionally equivalent. We gain no benefit from storing the index and repeatedly fetching the keyboard with `kbd_get_keyboard` when we need it. We'll be notified when it's going away so we can clean up the pointer. All existing references were trivially converted. Only once instance actually needed the index. --- sys/dev/vt/vt.h | 2 +- sys/dev/vt/vt_core.c | 69 +++++++++++++++----------------------------- 2 files changed, 24 insertions(+), 47 deletions(-) diff --git a/sys/dev/vt/vt.h b/sys/dev/vt/vt.h index da308cd035c4..ec939f2341cd 100644 --- a/sys/dev/vt/vt.h +++ b/sys/dev/vt/vt.h @@ -158,7 +158,7 @@ struct vt_device { #define VDF_QUIET_BELL 0x80 /* Disable bell. */ #define VDF_SUSPENDED 0x100 /* Device has been suspended. */ #define VDF_DOWNGRADE 0x8000 /* The driver is being downgraded. */ - int vd_keyboard; /* (G) Keyboard index. */ + struct keyboard *vd_keyboard; /* (G) Keyboard. */ unsigned int vd_kbstate; /* (?) Device unit. */ unsigned int vd_unit; /* (c) Device unit. */ int vd_altbrk; /* (?) Alt break seq. state */ diff --git a/sys/dev/vt/vt_core.c b/sys/dev/vt/vt_core.c index 78cd5d60b1cf..3d7eecdb1b10 100644 --- a/sys/dev/vt/vt_core.c +++ b/sys/dev/vt/vt_core.c @@ -605,8 +605,7 @@ vt_window_switch(struct vt_window *vw) /* Restore per-window keyboard mode. */ mtx_lock(&Giant); - kbd = kbd_get_keyboard(vd->vd_keyboard); - if (kbd != NULL) { + if ((kbd = vd->vd_keyboard) != NULL) { if (curvw->vw_kbdmode == K_XLATE) vt_save_kbd_state(curvw, kbd); @@ -982,7 +981,7 @@ vt_kbdevent(keyboard_t *kbd, int event, void *arg) break; case KBDIO_UNLOADING: mtx_lock(&Giant); - vd->vd_keyboard = -1; + vd->vd_keyboard = NULL; kbd_release(kbd, (void *)vd); mtx_unlock(&Giant); return (0); @@ -1039,19 +1038,14 @@ vt_allocate_keyboard(struct vt_device *vd) DPRINTF(20, "%s: no kbdmux allocated\n", __func__); idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd); if (idx0 < 0) { - /* - * We don't have a keyboard yet, so we must invalidate - * vd->vd_keyboard so that later keyboard attachment can - * succeed. Any value >= 0 can accidentally match a - * keyboard. - */ - vd->vd_keyboard = -1; DPRINTF(10, "%s: No keyboard found.\n", __func__); return (-1); } + k0 = kbd_get_keyboard(idx0); } - vd->vd_keyboard = idx0; - DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard); + vd->vd_keyboard = k0; + DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, + vd->vd_keyboard->kb_index); if (vd->vd_curwindow == &vt_conswindow) { for (i = 0; i < grabbed; ++i) @@ -1540,8 +1534,7 @@ vtterm_cngetc(struct terminal *tm) } /* Stripped down keyboard handler. */ - kbd = kbd_get_keyboard(vd->vd_keyboard); - if (kbd == NULL) + if ((kbd = vd->vd_keyboard) == NULL) return (-1); /* Force keyboard input mode to K_XLATE */ @@ -1617,8 +1610,7 @@ vtterm_cngrab(struct terminal *tm) if (!cold) vt_window_switch(vw); - kbd = kbd_get_keyboard(vd->vd_keyboard); - if (kbd == NULL) + if ((kbd = vd->vd_keyboard) == NULL) return; if (vw->vw_grabbed++ > 0) @@ -1648,8 +1640,7 @@ vtterm_cnungrab(struct terminal *tm) vw = tm->tm_softc; vd = vw->vw_device; - kbd = kbd_get_keyboard(vd->vd_keyboard); - if (kbd == NULL) + if ((kbd = vd->vd_keyboard) == NULL) return; if (--vw->vw_grabbed > 0) @@ -2189,8 +2180,7 @@ vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data, error = 0; mtx_lock(&Giant); - kbd = kbd_get_keyboard(vd->vd_keyboard); - if (kbd != NULL) + if ((kbd = vd->vd_keyboard) != NULL) error = kbdd_ioctl(kbd, cmd, data); mtx_unlock(&Giant); if (error == ENOIOCTL) { @@ -2208,8 +2198,7 @@ vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data, if (vw == vd->vd_curwindow) { mtx_lock(&Giant); - kbd = kbd_get_keyboard(vd->vd_keyboard); - if (kbd != NULL) + if ((kbd = vd->vd_keyboard) != NULL) error = vt_save_kbd_state(vw, kbd); mtx_unlock(&Giant); @@ -2234,8 +2223,7 @@ vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data, error = 0; if (vw == vd->vd_curwindow) { mtx_lock(&Giant); - kbd = kbd_get_keyboard(vd->vd_keyboard); - if (kbd != NULL) + if ((kbd = vd->vd_keyboard) != NULL) error = vt_update_kbd_state(vw, kbd); mtx_unlock(&Giant); } @@ -2247,8 +2235,7 @@ vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data, if (vw == vd->vd_curwindow) { mtx_lock(&Giant); - kbd = kbd_get_keyboard(vd->vd_keyboard); - if (kbd != NULL) + if ((kbd = vd->vd_keyboard) != NULL) error = vt_save_kbd_leds(vw, kbd); mtx_unlock(&Giant); @@ -2273,8 +2260,7 @@ vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data, error = 0; if (vw == vd->vd_curwindow) { mtx_lock(&Giant); - kbd = kbd_get_keyboard(vd->vd_keyboard); - if (kbd != NULL) + if ((kbd = vd->vd_keyboard) != NULL) error = vt_update_kbd_leds(vw, kbd); mtx_unlock(&Giant); } @@ -2290,8 +2276,7 @@ vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data, if (vw == vd->vd_curwindow) { mtx_lock(&Giant); - kbd = kbd_get_keyboard(vd->vd_keyboard); - if (kbd != NULL) + if ((kbd = vd->vd_keyboard) != NULL) error = vt_save_kbd_mode(vw, kbd); mtx_unlock(&Giant); @@ -2316,8 +2301,7 @@ vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data, error = 0; if (vw == vd->vd_curwindow) { mtx_lock(&Giant); - kbd = kbd_get_keyboard(vd->vd_keyboard); - if (kbd != NULL) + if ((kbd = vd->vd_keyboard) != NULL) error = vt_update_kbd_mode(vw, kbd); mtx_unlock(&Giant); } @@ -2362,8 +2346,7 @@ vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data, if (vw == vd->vd_curwindow) { mtx_lock(&Giant); - kbd = kbd_get_keyboard(vd->vd_keyboard); - if (kbd != NULL) + if ((kbd = vd->vd_keyboard) != NULL) vt_save_kbd_state(vw, kbd); mtx_unlock(&Giant); } @@ -2482,7 +2465,8 @@ vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data, case CONS_SETKBD: /* set the new keyboard */ mtx_lock(&Giant); error = 0; - if (vd->vd_keyboard != *(int *)data) { + if (vd->vd_keyboard == NULL || + vd->vd_keyboard->kb_index != *(int *)data) { kbd = kbd_get_keyboard(*(int *)data); if (kbd == NULL) { mtx_unlock(&Giant); @@ -2491,13 +2475,11 @@ vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data, i = kbd_allocate(kbd->kb_name, kbd->kb_unit, (void *)vd, vt_kbdevent, vd); if (i >= 0) { - if (vd->vd_keyboard != -1) { - kbd = kbd_get_keyboard(vd->vd_keyboard); + if ((kbd = vd->vd_keyboard) != NULL) { vt_save_kbd_state(vd->vd_curwindow, kbd); kbd_release(kbd, (void *)vd); } - kbd = kbd_get_keyboard(i); - vd->vd_keyboard = i; + kbd = vd->vd_keyboard = kbd_get_keyboard(i); vt_update_kbd_mode(vd->vd_curwindow, kbd); vt_update_kbd_state(vd->vd_curwindow, kbd); @@ -2510,16 +2492,11 @@ vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data, case CONS_RELKBD: /* release the current keyboard */ mtx_lock(&Giant); error = 0; - if (vd->vd_keyboard != -1) { - kbd = kbd_get_keyboard(vd->vd_keyboard); - if (kbd == NULL) { - mtx_unlock(&Giant); - return (EINVAL); - } + if ((kbd = vd->vd_keyboard) != NULL) { vt_save_kbd_state(vd->vd_curwindow, kbd); error = kbd_release(kbd, (void *)vd); if (error == 0) { - vd->vd_keyboard = -1; + vd->vd_keyboard = NULL; } } mtx_unlock(&Giant);