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