1330897Seadler/*- 2330897Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3330897Seadler * 4244197Sgonzo * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org> 5244197Sgonzo * All rights reserved. 6244197Sgonzo * 7244197Sgonzo * Based on dev/usb/input/ukbd.c 8244197Sgonzo * 9244197Sgonzo * Redistribution and use in source and binary forms, with or without 10244197Sgonzo * modification, are permitted provided that the following conditions 11244197Sgonzo * are met: 12244197Sgonzo * 1. Redistributions of source code must retain the above copyright 13244197Sgonzo * notice, this list of conditions and the following disclaimer. 14244197Sgonzo * 2. Redistributions in binary form must reproduce the above copyright 15244197Sgonzo * notice, this list of conditions and the following disclaimer in the 16244197Sgonzo * documentation and/or other materials provided with the distribution. 17244197Sgonzo * 18244197Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19244197Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20244197Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21244197Sgonzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22244197Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23244197Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24244197Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25244197Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26244197Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27244197Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28244197Sgonzo * SUCH DAMAGE. 29244197Sgonzo */ 30244197Sgonzo 31244197Sgonzo#include <sys/cdefs.h> 32244197Sgonzo__FBSDID("$FreeBSD: stable/11/sys/arm/versatile/pl050.c 356021 2019-12-22 19:14:33Z kevans $"); 33244197Sgonzo 34244197Sgonzo#include <sys/param.h> 35244197Sgonzo#include <sys/systm.h> 36244197Sgonzo#include <sys/bus.h> 37244197Sgonzo#include <sys/kernel.h> 38356020Skevans#include <sys/lock.h> 39244197Sgonzo#include <sys/module.h> 40244197Sgonzo#include <sys/malloc.h> 41356020Skevans#include <sys/mutex.h> 42244197Sgonzo#include <sys/rman.h> 43244197Sgonzo#include <sys/proc.h> 44244197Sgonzo#include <sys/sched.h> 45244197Sgonzo#include <sys/kdb.h> 46244197Sgonzo 47244197Sgonzo#include <machine/bus.h> 48244197Sgonzo#include <machine/cpu.h> 49244197Sgonzo#include <machine/intr.h> 50244197Sgonzo 51244197Sgonzo#include <dev/fdt/fdt_common.h> 52244197Sgonzo#include <dev/ofw/openfirm.h> 53244197Sgonzo#include <dev/ofw/ofw_bus.h> 54244197Sgonzo#include <dev/ofw/ofw_bus_subr.h> 55244197Sgonzo 56244197Sgonzo#include <sys/ioccom.h> 57244197Sgonzo#include <sys/filio.h> 58244197Sgonzo#include <sys/kbio.h> 59244197Sgonzo 60244197Sgonzo#include <dev/kbd/kbdreg.h> 61244197Sgonzo 62244197Sgonzo#include <machine/bus.h> 63244197Sgonzo 64244197Sgonzo#include <dev/kbd/kbdtables.h> 65244197Sgonzo 66244197Sgonzo#define KMI_LOCK() mtx_lock(&Giant) 67244197Sgonzo#define KMI_UNLOCK() mtx_unlock(&Giant) 68244197Sgonzo 69244197Sgonzo#ifdef INVARIANTS 70244197Sgonzo/* 71244197Sgonzo * Assert that the lock is held in all contexts 72244197Sgonzo * where the code can be executed. 73244197Sgonzo */ 74244197Sgonzo#define KMI_LOCK_ASSERT() mtx_assert(&Giant, MA_OWNED) 75244197Sgonzo/* 76244197Sgonzo * Assert that the lock is held in the contexts 77244197Sgonzo * where it really has to be so. 78244197Sgonzo */ 79244197Sgonzo#define KMI_CTX_LOCK_ASSERT() \ 80244197Sgonzo do { \ 81244197Sgonzo if (!kdb_active && panicstr == NULL) \ 82244197Sgonzo mtx_assert(&Giant, MA_OWNED); \ 83244197Sgonzo } while (0) 84244197Sgonzo#else 85244197Sgonzo#define KMI_LOCK_ASSERT() (void)0 86244197Sgonzo#define KMI_CTX_LOCK_ASSERT() (void)0 87244197Sgonzo#endif 88244197Sgonzo 89244197Sgonzo#define KMICR 0x00 90244197Sgonzo#define KMICR_TYPE_NONPS2 (1 << 5) 91244197Sgonzo#define KMICR_RXINTREN (1 << 4) 92244197Sgonzo#define KMICR_TXINTREN (1 << 3) 93244197Sgonzo#define KMICR_EN (1 << 2) 94244197Sgonzo#define KMICR_FKMID (1 << 1) 95244197Sgonzo#define KMICR_FKMIC (1 << 0) 96244197Sgonzo#define KMISTAT 0x04 97244197Sgonzo#define KMISTAT_TXEMPTY (1 << 6) 98244197Sgonzo#define KMISTAT_TXBUSY (1 << 5) 99244197Sgonzo#define KMISTAT_RXFULL (1 << 4) 100244197Sgonzo#define KMISTAT_RXBUSY (1 << 3) 101244197Sgonzo#define KMISTAT_RXPARITY (1 << 2) 102244197Sgonzo#define KMISTAT_KMIC (1 << 1) 103244197Sgonzo#define KMISTAT_KMID (1 << 0) 104244197Sgonzo#define KMIDATA 0x08 105244197Sgonzo#define KMICLKDIV 0x0C 106244197Sgonzo#define KMIIR 0x10 107244197Sgonzo#define KMIIR_TXINTR (1 << 1) 108244197Sgonzo#define KMIIR_RXINTR (1 << 0) 109244197Sgonzo 110244197Sgonzo#define KMI_DRIVER_NAME "kmi" 111244197Sgonzo#define KMI_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */ 112244197Sgonzo 113331402Sgonzo#define SET_SCANCODE_SET 0xf0 114331402Sgonzo 115244197Sgonzostruct kmi_softc { 116331402Sgonzo device_t sc_dev; 117244197Sgonzo keyboard_t sc_kbd; 118244197Sgonzo keymap_t sc_keymap; 119244197Sgonzo accentmap_t sc_accmap; 120244197Sgonzo fkeytab_t sc_fkeymap[KMI_NFKEY]; 121244197Sgonzo 122244197Sgonzo struct resource* sc_mem_res; 123244197Sgonzo struct resource* sc_irq_res; 124244197Sgonzo void* sc_intr_hl; 125244197Sgonzo 126244197Sgonzo int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */ 127244197Sgonzo int sc_state; /* shift/lock key state */ 128244197Sgonzo int sc_accents; /* accent key index (> 0) */ 129244197Sgonzo uint32_t sc_flags; /* flags */ 130244197Sgonzo#define KMI_FLAG_COMPOSE 0x00000001 131244197Sgonzo#define KMI_FLAG_POLLING 0x00000002 132244197Sgonzo 133244197Sgonzo struct thread *sc_poll_thread; 134244197Sgonzo}; 135244197Sgonzo 136244197Sgonzo/* Read/Write macros for Timer used as timecounter */ 137244197Sgonzo#define pl050_kmi_read_4(sc, reg) \ 138244197Sgonzo bus_read_4((sc)->sc_mem_res, (reg)) 139244197Sgonzo 140244197Sgonzo#define pl050_kmi_write_4(sc, reg, val) \ 141244197Sgonzo bus_write_4((sc)->sc_mem_res, (reg), (val)) 142244197Sgonzo 143244197Sgonzo/* prototypes */ 144244197Sgonzostatic void kmi_set_leds(struct kmi_softc *, uint8_t); 145244197Sgonzostatic int kmi_set_typematic(keyboard_t *, int); 146244197Sgonzostatic uint32_t kmi_read_char(keyboard_t *, int); 147244197Sgonzostatic void kmi_clear_state(keyboard_t *); 148244197Sgonzostatic int kmi_ioctl(keyboard_t *, u_long, caddr_t); 149244197Sgonzostatic int kmi_enable(keyboard_t *); 150244197Sgonzostatic int kmi_disable(keyboard_t *); 151244197Sgonzo 152331402Sgonzostatic int kmi_attached = 0; 153331402Sgonzo 154244197Sgonzo/* early keyboard probe, not supported */ 155244197Sgonzostatic int 156244197Sgonzokmi_configure(int flags) 157244197Sgonzo{ 158244197Sgonzo return (0); 159244197Sgonzo} 160244197Sgonzo 161244197Sgonzo/* detect a keyboard, not used */ 162244197Sgonzostatic int 163244197Sgonzokmi_probe(int unit, void *arg, int flags) 164244197Sgonzo{ 165244197Sgonzo return (ENXIO); 166244197Sgonzo} 167244197Sgonzo 168244197Sgonzo/* reset and initialize the device, not used */ 169244197Sgonzostatic int 170244197Sgonzokmi_init(int unit, keyboard_t **kbdp, void *arg, int flags) 171244197Sgonzo{ 172244197Sgonzo return (ENXIO); 173244197Sgonzo} 174244197Sgonzo 175244197Sgonzo/* test the interface to the device, not used */ 176244197Sgonzostatic int 177244197Sgonzokmi_test_if(keyboard_t *kbd) 178244197Sgonzo{ 179244197Sgonzo return (0); 180244197Sgonzo} 181244197Sgonzo 182244197Sgonzo/* finish using this keyboard, not used */ 183244197Sgonzostatic int 184244197Sgonzokmi_term(keyboard_t *kbd) 185244197Sgonzo{ 186244197Sgonzo return (ENXIO); 187244197Sgonzo} 188244197Sgonzo 189244197Sgonzo/* keyboard interrupt routine, not used */ 190244197Sgonzostatic int 191244197Sgonzokmi_intr(keyboard_t *kbd, void *arg) 192244197Sgonzo{ 193244197Sgonzo 194244197Sgonzo return (0); 195244197Sgonzo} 196244197Sgonzo 197244197Sgonzo/* lock the access to the keyboard, not used */ 198244197Sgonzostatic int 199244197Sgonzokmi_lock(keyboard_t *kbd, int lock) 200244197Sgonzo{ 201244197Sgonzo return (1); 202244197Sgonzo} 203244197Sgonzo 204244197Sgonzo/* 205244197Sgonzo * Enable the access to the device; until this function is called, 206244197Sgonzo * the client cannot read from the keyboard. 207244197Sgonzo */ 208244197Sgonzostatic int 209244197Sgonzokmi_enable(keyboard_t *kbd) 210244197Sgonzo{ 211244197Sgonzo 212244197Sgonzo KMI_LOCK(); 213244197Sgonzo KBD_ACTIVATE(kbd); 214244197Sgonzo KMI_UNLOCK(); 215244197Sgonzo 216244197Sgonzo return (0); 217244197Sgonzo} 218244197Sgonzo 219244197Sgonzo/* disallow the access to the device */ 220244197Sgonzostatic int 221244197Sgonzokmi_disable(keyboard_t *kbd) 222244197Sgonzo{ 223244197Sgonzo 224244197Sgonzo KMI_LOCK(); 225244197Sgonzo KBD_DEACTIVATE(kbd); 226244197Sgonzo KMI_UNLOCK(); 227244197Sgonzo 228244197Sgonzo return (0); 229244197Sgonzo} 230244197Sgonzo 231244197Sgonzo/* check if data is waiting */ 232244197Sgonzostatic int 233244197Sgonzokmi_check(keyboard_t *kbd) 234244197Sgonzo{ 235244197Sgonzo struct kmi_softc *sc = kbd->kb_data; 236244197Sgonzo uint32_t reg; 237244197Sgonzo 238244197Sgonzo KMI_CTX_LOCK_ASSERT(); 239244197Sgonzo 240244197Sgonzo if (!KBD_IS_ACTIVE(kbd)) 241244197Sgonzo return (0); 242244197Sgonzo 243244197Sgonzo reg = pl050_kmi_read_4(sc, KMIIR); 244244197Sgonzo return (reg & KMIIR_RXINTR); 245244197Sgonzo} 246244197Sgonzo 247244197Sgonzo/* check if char is waiting */ 248244197Sgonzostatic int 249244197Sgonzokmi_check_char_locked(keyboard_t *kbd) 250244197Sgonzo{ 251244197Sgonzo KMI_CTX_LOCK_ASSERT(); 252244197Sgonzo 253244197Sgonzo if (!KBD_IS_ACTIVE(kbd)) 254244197Sgonzo return (0); 255244197Sgonzo 256244197Sgonzo return (kmi_check(kbd)); 257244197Sgonzo} 258244197Sgonzo 259244197Sgonzostatic int 260244197Sgonzokmi_check_char(keyboard_t *kbd) 261244197Sgonzo{ 262244197Sgonzo int result; 263244197Sgonzo 264244197Sgonzo KMI_LOCK(); 265244197Sgonzo result = kmi_check_char_locked(kbd); 266244197Sgonzo KMI_UNLOCK(); 267244197Sgonzo 268244197Sgonzo return (result); 269244197Sgonzo} 270244197Sgonzo 271244197Sgonzo/* read one byte from the keyboard if it's allowed */ 272244197Sgonzo/* Currently unused. */ 273244197Sgonzostatic int 274244197Sgonzokmi_read(keyboard_t *kbd, int wait) 275244197Sgonzo{ 276244197Sgonzo KMI_CTX_LOCK_ASSERT(); 277244197Sgonzo 278244197Sgonzo if (!KBD_IS_ACTIVE(kbd)) 279244197Sgonzo return (-1); 280244197Sgonzo 281244197Sgonzo ++(kbd->kb_count); 282244197Sgonzo printf("Implement ME: %s\n", __func__); 283244197Sgonzo return (0); 284244197Sgonzo} 285244197Sgonzo 286244197Sgonzo/* read char from the keyboard */ 287244197Sgonzostatic uint32_t 288244197Sgonzokmi_read_char_locked(keyboard_t *kbd, int wait) 289244197Sgonzo{ 290244197Sgonzo struct kmi_softc *sc = kbd->kb_data; 291244197Sgonzo uint32_t reg, data; 292244197Sgonzo 293244197Sgonzo KMI_CTX_LOCK_ASSERT(); 294244197Sgonzo 295244197Sgonzo if (!KBD_IS_ACTIVE(kbd)) 296244197Sgonzo return (NOKEY); 297244197Sgonzo 298244197Sgonzo reg = pl050_kmi_read_4(sc, KMIIR); 299244197Sgonzo if (reg & KMIIR_RXINTR) { 300244197Sgonzo data = pl050_kmi_read_4(sc, KMIDATA); 301244197Sgonzo return (data); 302244197Sgonzo } 303244197Sgonzo 304244197Sgonzo ++kbd->kb_count; 305244197Sgonzo return (NOKEY); 306244197Sgonzo} 307244197Sgonzo 308244197Sgonzo/* Currently wait is always false. */ 309244197Sgonzostatic uint32_t 310244197Sgonzokmi_read_char(keyboard_t *kbd, int wait) 311244197Sgonzo{ 312244197Sgonzo uint32_t keycode; 313244197Sgonzo 314244197Sgonzo KMI_LOCK(); 315244197Sgonzo keycode = kmi_read_char_locked(kbd, wait); 316244197Sgonzo KMI_UNLOCK(); 317244197Sgonzo 318244197Sgonzo return (keycode); 319244197Sgonzo} 320244197Sgonzo 321244197Sgonzo/* some useful control functions */ 322244197Sgonzostatic int 323244197Sgonzokmi_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg) 324244197Sgonzo{ 325244197Sgonzo struct kmi_softc *sc = kbd->kb_data; 326244197Sgonzo int i; 327244197Sgonzo#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 328244197Sgonzo defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 329244197Sgonzo int ival; 330244197Sgonzo 331244197Sgonzo#endif 332244197Sgonzo 333244197Sgonzo KMI_LOCK_ASSERT(); 334244197Sgonzo 335244197Sgonzo switch (cmd) { 336244197Sgonzo case KDGKBMODE: /* get keyboard mode */ 337244197Sgonzo *(int *)arg = sc->sc_mode; 338244197Sgonzo break; 339244197Sgonzo#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 340244197Sgonzo defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 341244197Sgonzo case _IO('K', 7): 342244197Sgonzo ival = IOCPARM_IVAL(arg); 343244197Sgonzo arg = (caddr_t)&ival; 344244197Sgonzo /* FALLTHROUGH */ 345244197Sgonzo#endif 346244197Sgonzo case KDSKBMODE: /* set keyboard mode */ 347244197Sgonzo switch (*(int *)arg) { 348244197Sgonzo case K_XLATE: 349244197Sgonzo if (sc->sc_mode != K_XLATE) { 350244197Sgonzo /* make lock key state and LED state match */ 351244197Sgonzo sc->sc_state &= ~LOCK_MASK; 352244197Sgonzo sc->sc_state |= KBD_LED_VAL(kbd); 353244197Sgonzo } 354244197Sgonzo /* FALLTHROUGH */ 355244197Sgonzo case K_RAW: 356244197Sgonzo case K_CODE: 357244197Sgonzo if (sc->sc_mode != *(int *)arg) { 358244197Sgonzo if ((sc->sc_flags & KMI_FLAG_POLLING) == 0) 359244197Sgonzo kmi_clear_state(kbd); 360244197Sgonzo sc->sc_mode = *(int *)arg; 361244197Sgonzo } 362244197Sgonzo break; 363244197Sgonzo default: 364244197Sgonzo return (EINVAL); 365244197Sgonzo } 366244197Sgonzo break; 367244197Sgonzo 368244197Sgonzo case KDGETLED: /* get keyboard LED */ 369244197Sgonzo *(int *)arg = KBD_LED_VAL(kbd); 370244197Sgonzo break; 371244197Sgonzo#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 372244197Sgonzo defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 373244197Sgonzo case _IO('K', 66): 374244197Sgonzo ival = IOCPARM_IVAL(arg); 375244197Sgonzo arg = (caddr_t)&ival; 376244197Sgonzo /* FALLTHROUGH */ 377244197Sgonzo#endif 378244197Sgonzo case KDSETLED: /* set keyboard LED */ 379244197Sgonzo /* NOTE: lock key state in "sc_state" won't be changed */ 380244197Sgonzo if (*(int *)arg & ~LOCK_MASK) 381244197Sgonzo return (EINVAL); 382244197Sgonzo 383244197Sgonzo i = *(int *)arg; 384244197Sgonzo 385244197Sgonzo /* replace CAPS LED with ALTGR LED for ALTGR keyboards */ 386244197Sgonzo if (sc->sc_mode == K_XLATE && 387244197Sgonzo kbd->kb_keymap->n_keys > ALTGR_OFFSET) { 388244197Sgonzo if (i & ALKED) 389244197Sgonzo i |= CLKED; 390244197Sgonzo else 391244197Sgonzo i &= ~CLKED; 392244197Sgonzo } 393244197Sgonzo if (KBD_HAS_DEVICE(kbd)) 394244197Sgonzo kmi_set_leds(sc, i); 395244197Sgonzo 396244197Sgonzo KBD_LED_VAL(kbd) = *(int *)arg; 397244197Sgonzo break; 398244197Sgonzo case KDGKBSTATE: /* get lock key state */ 399244197Sgonzo *(int *)arg = sc->sc_state & LOCK_MASK; 400244197Sgonzo break; 401244197Sgonzo#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 402244197Sgonzo defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 403244197Sgonzo case _IO('K', 20): 404244197Sgonzo ival = IOCPARM_IVAL(arg); 405244197Sgonzo arg = (caddr_t)&ival; 406244197Sgonzo /* FALLTHROUGH */ 407244197Sgonzo#endif 408244197Sgonzo case KDSKBSTATE: /* set lock key state */ 409244197Sgonzo if (*(int *)arg & ~LOCK_MASK) { 410244197Sgonzo return (EINVAL); 411244197Sgonzo } 412244197Sgonzo sc->sc_state &= ~LOCK_MASK; 413244197Sgonzo sc->sc_state |= *(int *)arg; 414244197Sgonzo 415244197Sgonzo /* set LEDs and quit */ 416244197Sgonzo return (kmi_ioctl(kbd, KDSETLED, arg)); 417244197Sgonzo 418244197Sgonzo case KDSETREPEAT: /* set keyboard repeat rate (new 419244197Sgonzo * interface) */ 420244197Sgonzo if (!KBD_HAS_DEVICE(kbd)) { 421244197Sgonzo return (0); 422244197Sgonzo } 423244197Sgonzo if (((int *)arg)[1] < 0) { 424244197Sgonzo return (EINVAL); 425244197Sgonzo } 426244197Sgonzo if (((int *)arg)[0] < 0) { 427244197Sgonzo return (EINVAL); 428244197Sgonzo } 429244197Sgonzo if (((int *)arg)[0] < 200) /* fastest possible value */ 430244197Sgonzo kbd->kb_delay1 = 200; 431244197Sgonzo else 432244197Sgonzo kbd->kb_delay1 = ((int *)arg)[0]; 433244197Sgonzo kbd->kb_delay2 = ((int *)arg)[1]; 434244197Sgonzo return (0); 435244197Sgonzo 436244197Sgonzo#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 437244197Sgonzo defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 438244197Sgonzo case _IO('K', 67): 439244197Sgonzo ival = IOCPARM_IVAL(arg); 440244197Sgonzo arg = (caddr_t)&ival; 441244197Sgonzo /* FALLTHROUGH */ 442244197Sgonzo#endif 443244197Sgonzo case KDSETRAD: /* set keyboard repeat rate (old 444244197Sgonzo * interface) */ 445244197Sgonzo return (kmi_set_typematic(kbd, *(int *)arg)); 446244197Sgonzo 447244197Sgonzo case PIO_KEYMAP: /* set keyboard translation table */ 448244197Sgonzo case OPIO_KEYMAP: /* set keyboard translation table 449244197Sgonzo * (compat) */ 450244197Sgonzo case PIO_KEYMAPENT: /* set keyboard translation table 451244197Sgonzo * entry */ 452244197Sgonzo case PIO_DEADKEYMAP: /* set accent key translation table */ 453244197Sgonzo sc->sc_accents = 0; 454244197Sgonzo /* FALLTHROUGH */ 455244197Sgonzo default: 456244197Sgonzo return (genkbd_commonioctl(kbd, cmd, arg)); 457244197Sgonzo } 458244197Sgonzo 459244197Sgonzo return (0); 460244197Sgonzo} 461244197Sgonzo 462244197Sgonzostatic int 463244197Sgonzokmi_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) 464244197Sgonzo{ 465244197Sgonzo int result; 466244197Sgonzo 467244197Sgonzo /* 468244197Sgonzo * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any 469244197Sgonzo * context where printf(9) can be called, which among other things 470244197Sgonzo * includes interrupt filters and threads with any kinds of locks 471244197Sgonzo * already held. For this reason it would be dangerous to acquire 472244197Sgonzo * the Giant here unconditionally. On the other hand we have to 473244197Sgonzo * have it to handle the ioctl. 474244197Sgonzo * So we make our best effort to auto-detect whether we can grab 475244197Sgonzo * the Giant or not. Blame syscons(4) for this. 476244197Sgonzo */ 477244197Sgonzo switch (cmd) { 478244197Sgonzo case KDGKBSTATE: 479244197Sgonzo case KDSKBSTATE: 480244197Sgonzo case KDSETLED: 481244197Sgonzo if (!mtx_owned(&Giant) && !SCHEDULER_STOPPED()) 482244197Sgonzo return (EDEADLK); /* best I could come up with */ 483244197Sgonzo /* FALLTHROUGH */ 484244197Sgonzo default: 485244197Sgonzo KMI_LOCK(); 486244197Sgonzo result = kmi_ioctl_locked(kbd, cmd, arg); 487244197Sgonzo KMI_UNLOCK(); 488244197Sgonzo return (result); 489244197Sgonzo } 490244197Sgonzo} 491244197Sgonzo 492244197Sgonzo/* clear the internal state of the keyboard */ 493244197Sgonzostatic void 494244197Sgonzokmi_clear_state(keyboard_t *kbd) 495244197Sgonzo{ 496244197Sgonzo struct kmi_softc *sc = kbd->kb_data; 497244197Sgonzo 498244197Sgonzo KMI_CTX_LOCK_ASSERT(); 499244197Sgonzo 500244197Sgonzo sc->sc_flags &= ~(KMI_FLAG_COMPOSE | KMI_FLAG_POLLING); 501244197Sgonzo sc->sc_state &= LOCK_MASK; /* preserve locking key state */ 502244197Sgonzo sc->sc_accents = 0; 503244197Sgonzo} 504244197Sgonzo 505244197Sgonzo/* save the internal state, not used */ 506244197Sgonzostatic int 507244197Sgonzokmi_get_state(keyboard_t *kbd, void *buf, size_t len) 508244197Sgonzo{ 509244197Sgonzo return (len == 0) ? 1 : -1; 510244197Sgonzo} 511244197Sgonzo 512244197Sgonzo/* set the internal state, not used */ 513244197Sgonzostatic int 514244197Sgonzokmi_set_state(keyboard_t *kbd, void *buf, size_t len) 515244197Sgonzo{ 516244197Sgonzo return (EINVAL); 517244197Sgonzo} 518244197Sgonzo 519244197Sgonzostatic int 520244197Sgonzokmi_poll(keyboard_t *kbd, int on) 521244197Sgonzo{ 522244197Sgonzo struct kmi_softc *sc = kbd->kb_data; 523244197Sgonzo 524244197Sgonzo KMI_LOCK(); 525244197Sgonzo if (on) { 526244197Sgonzo sc->sc_flags |= KMI_FLAG_POLLING; 527244197Sgonzo sc->sc_poll_thread = curthread; 528244197Sgonzo } else { 529244197Sgonzo sc->sc_flags &= ~KMI_FLAG_POLLING; 530244197Sgonzo } 531244197Sgonzo KMI_UNLOCK(); 532244197Sgonzo 533244197Sgonzo return (0); 534244197Sgonzo} 535244197Sgonzo 536244197Sgonzo/* local functions */ 537244197Sgonzo 538244197Sgonzostatic void 539244197Sgonzokmi_set_leds(struct kmi_softc *sc, uint8_t leds) 540244197Sgonzo{ 541244197Sgonzo 542244197Sgonzo KMI_LOCK_ASSERT(); 543244197Sgonzo 544244197Sgonzo /* start transfer, if not already started */ 545244197Sgonzo printf("Implement me: %s\n", __func__); 546244197Sgonzo} 547244197Sgonzo 548244197Sgonzostatic int 549244197Sgonzokmi_set_typematic(keyboard_t *kbd, int code) 550244197Sgonzo{ 551244197Sgonzo static const int delays[] = {250, 500, 750, 1000}; 552244197Sgonzo static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63, 553244197Sgonzo 68, 76, 84, 92, 100, 110, 118, 126, 554244197Sgonzo 136, 152, 168, 184, 200, 220, 236, 252, 555244197Sgonzo 272, 304, 336, 368, 400, 440, 472, 504}; 556244197Sgonzo 557244197Sgonzo if (code & ~0x7f) { 558244197Sgonzo return (EINVAL); 559244197Sgonzo } 560244197Sgonzo kbd->kb_delay1 = delays[(code >> 5) & 3]; 561244197Sgonzo kbd->kb_delay2 = rates[code & 0x1f]; 562244197Sgonzo return (0); 563244197Sgonzo} 564244197Sgonzo 565244197Sgonzostatic keyboard_switch_t kmisw = { 566244197Sgonzo .probe = &kmi_probe, 567244197Sgonzo .init = &kmi_init, 568244197Sgonzo .term = &kmi_term, 569244197Sgonzo .intr = &kmi_intr, 570244197Sgonzo .test_if = &kmi_test_if, 571244197Sgonzo .enable = &kmi_enable, 572244197Sgonzo .disable = &kmi_disable, 573244197Sgonzo .read = &kmi_read, 574244197Sgonzo .check = &kmi_check, 575244197Sgonzo .read_char = &kmi_read_char, 576244197Sgonzo .check_char = &kmi_check_char, 577244197Sgonzo .ioctl = &kmi_ioctl, 578244197Sgonzo .lock = &kmi_lock, 579244197Sgonzo .clear_state = &kmi_clear_state, 580244197Sgonzo .get_state = &kmi_get_state, 581244197Sgonzo .set_state = &kmi_set_state, 582244197Sgonzo .poll = &kmi_poll, 583244197Sgonzo}; 584244197Sgonzo 585244197SgonzoKEYBOARD_DRIVER(kmi, kmisw, kmi_configure); 586244197Sgonzo 587244197Sgonzostatic void 588244197Sgonzopl050_kmi_intr(void *arg) 589244197Sgonzo{ 590244197Sgonzo struct kmi_softc *sc = arg; 591244197Sgonzo uint32_t c; 592244197Sgonzo 593244197Sgonzo KMI_CTX_LOCK_ASSERT(); 594244197Sgonzo 595244197Sgonzo if ((sc->sc_flags & KMI_FLAG_POLLING) != 0) 596244197Sgonzo return; 597244197Sgonzo 598244197Sgonzo if (KBD_IS_ACTIVE(&sc->sc_kbd) && 599244197Sgonzo KBD_IS_BUSY(&sc->sc_kbd)) { 600244197Sgonzo /* let the callback function process the input */ 601244197Sgonzo (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT, 602244197Sgonzo sc->sc_kbd.kb_callback.kc_arg); 603244197Sgonzo } else { 604244197Sgonzo /* read and discard the input, no one is waiting for it */ 605244197Sgonzo do { 606244197Sgonzo c = kmi_read_char_locked(&sc->sc_kbd, 0); 607244197Sgonzo } while (c != NOKEY); 608244197Sgonzo } 609244197Sgonzo 610244197Sgonzo} 611244197Sgonzo 612244197Sgonzostatic int 613244197Sgonzopl050_kmi_probe(device_t dev) 614244197Sgonzo{ 615244197Sgonzo 616261410Sian if (!ofw_bus_status_okay(dev)) 617261410Sian return (ENXIO); 618261410Sian 619331402Sgonzo /* 620331402Sgonzo * PL050 is plain PS2 port that pushes bytes to/from computer 621331402Sgonzo * VersatilePB has two such ports and QEMU simulates keyboard 622331402Sgonzo * connected to port #0 and mouse connected to port #1. This 623331402Sgonzo * information can't be obtained from device tree so we just 624331402Sgonzo * hardcode this knowledge here. We attach keyboard driver to 625331402Sgonzo * port #0 and ignore port #1 626331402Sgonzo */ 627331402Sgonzo if (kmi_attached) 628331402Sgonzo return (ENXIO); 629331402Sgonzo 630244197Sgonzo if (ofw_bus_is_compatible(dev, "arm,pl050")) { 631244197Sgonzo device_set_desc(dev, "PL050 Keyboard/Mouse Interface"); 632244197Sgonzo return (BUS_PROBE_DEFAULT); 633244197Sgonzo } 634244197Sgonzo 635244197Sgonzo return (ENXIO); 636244197Sgonzo} 637244197Sgonzo 638244197Sgonzostatic int 639244197Sgonzopl050_kmi_attach(device_t dev) 640244197Sgonzo{ 641244197Sgonzo struct kmi_softc *sc = device_get_softc(dev); 642244197Sgonzo keyboard_t *kbd; 643244197Sgonzo int rid; 644244197Sgonzo int i; 645331402Sgonzo uint32_t ack; 646244197Sgonzo 647331402Sgonzo sc->sc_dev = dev; 648244197Sgonzo kbd = &sc->sc_kbd; 649244197Sgonzo rid = 0; 650244197Sgonzo 651244197Sgonzo sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 652244197Sgonzo if (sc->sc_mem_res == NULL) { 653244197Sgonzo device_printf(dev, "could not allocate memory resource\n"); 654244197Sgonzo return (ENXIO); 655244197Sgonzo } 656244197Sgonzo 657244197Sgonzo /* Request the IRQ resources */ 658244197Sgonzo sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 659244197Sgonzo if (sc->sc_irq_res == NULL) { 660244197Sgonzo device_printf(dev, "Error: could not allocate irq resources\n"); 661244197Sgonzo return (ENXIO); 662244197Sgonzo } 663244197Sgonzo 664244197Sgonzo /* Setup and enable the timer */ 665244197Sgonzo if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_CLK, 666244197Sgonzo NULL, pl050_kmi_intr, sc, 667244197Sgonzo &sc->sc_intr_hl) != 0) { 668244197Sgonzo bus_release_resource(dev, SYS_RES_IRQ, rid, 669244197Sgonzo sc->sc_irq_res); 670244197Sgonzo device_printf(dev, "Unable to setup the clock irq handler.\n"); 671244197Sgonzo return (ENXIO); 672244197Sgonzo } 673244197Sgonzo 674244197Sgonzo /* TODO: clock & divisor */ 675244197Sgonzo 676331402Sgonzo pl050_kmi_write_4(sc, KMICR, KMICR_EN); 677331402Sgonzo 678331402Sgonzo pl050_kmi_write_4(sc, KMIDATA, SET_SCANCODE_SET); 679331402Sgonzo /* read out ACK */ 680331402Sgonzo ack = pl050_kmi_read_4(sc, KMIDATA); 681331402Sgonzo /* Set Scan Code set 1 (XT) */ 682331402Sgonzo pl050_kmi_write_4(sc, KMIDATA, 1); 683331402Sgonzo /* read out ACK */ 684331402Sgonzo ack = pl050_kmi_read_4(sc, KMIDATA); 685331402Sgonzo 686244197Sgonzo pl050_kmi_write_4(sc, KMICR, KMICR_EN | KMICR_RXINTREN); 687244197Sgonzo 688244197Sgonzo kbd_init_struct(kbd, KMI_DRIVER_NAME, KB_OTHER, 689244197Sgonzo device_get_unit(dev), 0, 0, 0); 690244197Sgonzo kbd->kb_data = (void *)sc; 691244197Sgonzo 692244197Sgonzo sc->sc_keymap = key_map; 693244197Sgonzo sc->sc_accmap = accent_map; 694244197Sgonzo for (i = 0; i < KMI_NFKEY; i++) { 695244197Sgonzo sc->sc_fkeymap[i] = fkey_tab[i]; 696244197Sgonzo } 697244197Sgonzo 698244197Sgonzo kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap, 699244197Sgonzo sc->sc_fkeymap, KMI_NFKEY); 700244197Sgonzo 701244197Sgonzo KBD_FOUND_DEVICE(kbd); 702244197Sgonzo kmi_clear_state(kbd); 703244197Sgonzo KBD_PROBE_DONE(kbd); 704244197Sgonzo 705244197Sgonzo KBD_INIT_DONE(kbd); 706244197Sgonzo 707244197Sgonzo if (kbd_register(kbd) < 0) { 708244197Sgonzo goto detach; 709244197Sgonzo } 710244197Sgonzo KBD_CONFIG_DONE(kbd); 711244197Sgonzo 712244197Sgonzo#ifdef KBD_INSTALL_CDEV 713244197Sgonzo if (kbd_attach(kbd)) { 714244197Sgonzo goto detach; 715244197Sgonzo } 716244197Sgonzo#endif 717244197Sgonzo 718244197Sgonzo if (bootverbose) { 719356012Skevans kbdd_diag(kbd, bootverbose); 720244197Sgonzo } 721331402Sgonzo kmi_attached = 1; 722244197Sgonzo return (0); 723244197Sgonzo 724244197Sgonzodetach: 725244197Sgonzo return (ENXIO); 726244197Sgonzo 727244197Sgonzo} 728244197Sgonzo 729244197Sgonzostatic device_method_t pl050_kmi_methods[] = { 730244197Sgonzo DEVMETHOD(device_probe, pl050_kmi_probe), 731244197Sgonzo DEVMETHOD(device_attach, pl050_kmi_attach), 732244197Sgonzo { 0, 0 } 733244197Sgonzo}; 734244197Sgonzo 735244197Sgonzostatic driver_t pl050_kmi_driver = { 736244197Sgonzo "kmi", 737244197Sgonzo pl050_kmi_methods, 738244197Sgonzo sizeof(struct kmi_softc), 739244197Sgonzo}; 740244197Sgonzo 741244197Sgonzostatic devclass_t pl050_kmi_devclass; 742244197Sgonzo 743244197SgonzoDRIVER_MODULE(pl050_kmi, simplebus, pl050_kmi_driver, pl050_kmi_devclass, 0, 0); 744