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