2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
7 * Based on dev/usb/input/ukbd.c
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/malloc.h>
42 #include <sys/sched.h>
45 #include <machine/bus.h>
46 #include <machine/cpu.h>
47 #include <machine/intr.h>
49 #include <dev/ofw/openfirm.h>
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
53 #include <sys/ioccom.h>
54 #include <sys/filio.h>
58 #include <dev/kbd/kbdreg.h>
60 #include <machine/bus.h>
62 #include <dev/kbd/kbdtables.h>
64 #define KMI_LOCK() mtx_lock(&Giant)
65 #define KMI_UNLOCK() mtx_unlock(&Giant)
69 * Assert that the lock is held in all contexts
70 * where the code can be executed.
72 #define KMI_LOCK_ASSERT() mtx_assert(&Giant, MA_OWNED)
74 * Assert that the lock is held in the contexts
75 * where it really has to be so.
77 #define KMI_CTX_LOCK_ASSERT() \
79 if (!kdb_active && panicstr == NULL) \
80 mtx_assert(&Giant, MA_OWNED); \
83 #define KMI_LOCK_ASSERT() (void)0
84 #define KMI_CTX_LOCK_ASSERT() (void)0
88 #define KMICR_TYPE_NONPS2 (1 << 5)
89 #define KMICR_RXINTREN (1 << 4)
90 #define KMICR_TXINTREN (1 << 3)
91 #define KMICR_EN (1 << 2)
92 #define KMICR_FKMID (1 << 1)
93 #define KMICR_FKMIC (1 << 0)
95 #define KMISTAT_TXEMPTY (1 << 6)
96 #define KMISTAT_TXBUSY (1 << 5)
97 #define KMISTAT_RXFULL (1 << 4)
98 #define KMISTAT_RXBUSY (1 << 3)
99 #define KMISTAT_RXPARITY (1 << 2)
100 #define KMISTAT_KMIC (1 << 1)
101 #define KMISTAT_KMID (1 << 0)
103 #define KMICLKDIV 0x0C
105 #define KMIIR_TXINTR (1 << 1)
106 #define KMIIR_RXINTR (1 << 0)
108 #define KMI_DRIVER_NAME "kmi"
109 #define KMI_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */
111 #define SET_SCANCODE_SET 0xf0
117 accentmap_t sc_accmap;
118 fkeytab_t sc_fkeymap[KMI_NFKEY];
120 struct resource* sc_mem_res;
121 struct resource* sc_irq_res;
124 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
125 int sc_state; /* shift/lock key state */
126 int sc_accents; /* accent key index (> 0) */
127 uint32_t sc_flags; /* flags */
128 #define KMI_FLAG_COMPOSE 0x00000001
129 #define KMI_FLAG_POLLING 0x00000002
131 struct thread *sc_poll_thread;
134 /* Read/Write macros for Timer used as timecounter */
135 #define pl050_kmi_read_4(sc, reg) \
136 bus_read_4((sc)->sc_mem_res, (reg))
138 #define pl050_kmi_write_4(sc, reg, val) \
139 bus_write_4((sc)->sc_mem_res, (reg), (val))
142 static void kmi_set_leds(struct kmi_softc *, uint8_t);
143 static int kmi_set_typematic(keyboard_t *, int);
144 static uint32_t kmi_read_char(keyboard_t *, int);
145 static void kmi_clear_state(keyboard_t *);
146 static int kmi_ioctl(keyboard_t *, u_long, caddr_t);
147 static int kmi_enable(keyboard_t *);
148 static int kmi_disable(keyboard_t *);
150 static int kmi_attached = 0;
152 /* early keyboard probe, not supported */
154 kmi_configure(int flags)
159 /* detect a keyboard, not used */
161 kmi_probe(int unit, void *arg, int flags)
166 /* reset and initialize the device, not used */
168 kmi_init(int unit, keyboard_t **kbdp, void *arg, int flags)
173 /* test the interface to the device, not used */
175 kmi_test_if(keyboard_t *kbd)
180 /* finish using this keyboard, not used */
182 kmi_term(keyboard_t *kbd)
187 /* keyboard interrupt routine, not used */
189 kmi_intr(keyboard_t *kbd, void *arg)
195 /* lock the access to the keyboard, not used */
197 kmi_lock(keyboard_t *kbd, int lock)
203 * Enable the access to the device; until this function is called,
204 * the client cannot read from the keyboard.
207 kmi_enable(keyboard_t *kbd)
217 /* disallow the access to the device */
219 kmi_disable(keyboard_t *kbd)
229 /* check if data is waiting */
231 kmi_check(keyboard_t *kbd)
233 struct kmi_softc *sc = kbd->kb_data;
236 KMI_CTX_LOCK_ASSERT();
238 if (!KBD_IS_ACTIVE(kbd))
241 reg = pl050_kmi_read_4(sc, KMIIR);
242 return (reg & KMIIR_RXINTR);
245 /* check if char is waiting */
247 kmi_check_char_locked(keyboard_t *kbd)
249 KMI_CTX_LOCK_ASSERT();
251 if (!KBD_IS_ACTIVE(kbd))
254 return (kmi_check(kbd));
258 kmi_check_char(keyboard_t *kbd)
263 result = kmi_check_char_locked(kbd);
269 /* read one byte from the keyboard if it's allowed */
270 /* Currently unused. */
272 kmi_read(keyboard_t *kbd, int wait)
274 KMI_CTX_LOCK_ASSERT();
276 if (!KBD_IS_ACTIVE(kbd))
280 printf("Implement ME: %s\n", __func__);
284 /* read char from the keyboard */
286 kmi_read_char_locked(keyboard_t *kbd, int wait)
288 struct kmi_softc *sc = kbd->kb_data;
291 KMI_CTX_LOCK_ASSERT();
293 if (!KBD_IS_ACTIVE(kbd))
296 reg = pl050_kmi_read_4(sc, KMIIR);
297 if (reg & KMIIR_RXINTR) {
298 data = pl050_kmi_read_4(sc, KMIDATA);
306 /* Currently wait is always false. */
308 kmi_read_char(keyboard_t *kbd, int wait)
313 keycode = kmi_read_char_locked(kbd, wait);
319 /* some useful control functions */
321 kmi_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
323 struct kmi_softc *sc = kbd->kb_data;
325 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
326 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
334 case KDGKBMODE: /* get keyboard mode */
335 *(int *)arg = sc->sc_mode;
337 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
338 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
340 ival = IOCPARM_IVAL(arg);
341 arg = (caddr_t)&ival;
344 case KDSKBMODE: /* set keyboard mode */
345 switch (*(int *)arg) {
347 if (sc->sc_mode != K_XLATE) {
348 /* make lock key state and LED state match */
349 sc->sc_state &= ~LOCK_MASK;
350 sc->sc_state |= KBD_LED_VAL(kbd);
355 if (sc->sc_mode != *(int *)arg) {
356 if ((sc->sc_flags & KMI_FLAG_POLLING) == 0)
357 kmi_clear_state(kbd);
358 sc->sc_mode = *(int *)arg;
366 case KDGETLED: /* get keyboard LED */
367 *(int *)arg = KBD_LED_VAL(kbd);
369 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
370 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
372 ival = IOCPARM_IVAL(arg);
373 arg = (caddr_t)&ival;
376 case KDSETLED: /* set keyboard LED */
377 /* NOTE: lock key state in "sc_state" won't be changed */
378 if (*(int *)arg & ~LOCK_MASK)
383 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
384 if (sc->sc_mode == K_XLATE &&
385 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
391 if (KBD_HAS_DEVICE(kbd))
394 KBD_LED_VAL(kbd) = *(int *)arg;
396 case KDGKBSTATE: /* get lock key state */
397 *(int *)arg = sc->sc_state & LOCK_MASK;
399 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
400 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
402 ival = IOCPARM_IVAL(arg);
403 arg = (caddr_t)&ival;
406 case KDSKBSTATE: /* set lock key state */
407 if (*(int *)arg & ~LOCK_MASK) {
410 sc->sc_state &= ~LOCK_MASK;
411 sc->sc_state |= *(int *)arg;
413 /* set LEDs and quit */
414 return (kmi_ioctl(kbd, KDSETLED, arg));
416 case KDSETREPEAT: /* set keyboard repeat rate (new
418 if (!KBD_HAS_DEVICE(kbd)) {
421 if (((int *)arg)[1] < 0) {
424 if (((int *)arg)[0] < 0) {
427 if (((int *)arg)[0] < 200) /* fastest possible value */
428 kbd->kb_delay1 = 200;
430 kbd->kb_delay1 = ((int *)arg)[0];
431 kbd->kb_delay2 = ((int *)arg)[1];
434 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
435 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
437 ival = IOCPARM_IVAL(arg);
438 arg = (caddr_t)&ival;
441 case KDSETRAD: /* set keyboard repeat rate (old
443 return (kmi_set_typematic(kbd, *(int *)arg));
445 case PIO_KEYMAP: /* set keyboard translation table */
446 case OPIO_KEYMAP: /* set keyboard translation table
448 case PIO_KEYMAPENT: /* set keyboard translation table
450 case PIO_DEADKEYMAP: /* set accent key translation table */
454 return (genkbd_commonioctl(kbd, cmd, arg));
461 kmi_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
466 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
467 * context where printf(9) can be called, which among other things
468 * includes interrupt filters and threads with any kinds of locks
469 * already held. For this reason it would be dangerous to acquire
470 * the Giant here unconditionally. On the other hand we have to
471 * have it to handle the ioctl.
472 * So we make our best effort to auto-detect whether we can grab
473 * the Giant or not. Blame syscons(4) for this.
479 if (!mtx_owned(&Giant) && !SCHEDULER_STOPPED())
480 return (EDEADLK); /* best I could come up with */
484 result = kmi_ioctl_locked(kbd, cmd, arg);
490 /* clear the internal state of the keyboard */
492 kmi_clear_state(keyboard_t *kbd)
494 struct kmi_softc *sc = kbd->kb_data;
496 KMI_CTX_LOCK_ASSERT();
498 sc->sc_flags &= ~(KMI_FLAG_COMPOSE | KMI_FLAG_POLLING);
499 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
503 /* save the internal state, not used */
505 kmi_get_state(keyboard_t *kbd, void *buf, size_t len)
507 return (len == 0) ? 1 : -1;
510 /* set the internal state, not used */
512 kmi_set_state(keyboard_t *kbd, void *buf, size_t len)
518 kmi_poll(keyboard_t *kbd, int on)
520 struct kmi_softc *sc = kbd->kb_data;
524 sc->sc_flags |= KMI_FLAG_POLLING;
525 sc->sc_poll_thread = curthread;
527 sc->sc_flags &= ~KMI_FLAG_POLLING;
534 /* local functions */
537 kmi_set_leds(struct kmi_softc *sc, uint8_t leds)
542 /* start transfer, if not already started */
543 printf("Implement me: %s\n", __func__);
547 kmi_set_typematic(keyboard_t *kbd, int code)
549 static const int delays[] = {250, 500, 750, 1000};
550 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
551 68, 76, 84, 92, 100, 110, 118, 126,
552 136, 152, 168, 184, 200, 220, 236, 252,
553 272, 304, 336, 368, 400, 440, 472, 504};
558 kbd->kb_delay1 = delays[(code >> 5) & 3];
559 kbd->kb_delay2 = rates[code & 0x1f];
563 static keyboard_switch_t kmisw = {
568 .test_if = &kmi_test_if,
569 .enable = &kmi_enable,
570 .disable = &kmi_disable,
573 .read_char = &kmi_read_char,
574 .check_char = &kmi_check_char,
577 .clear_state = &kmi_clear_state,
578 .get_state = &kmi_get_state,
579 .set_state = &kmi_set_state,
580 .get_fkeystr = &genkbd_get_fkeystr,
582 .diag = &genkbd_diag,
585 KEYBOARD_DRIVER(kmi, kmisw, kmi_configure);
588 pl050_kmi_intr(void *arg)
590 struct kmi_softc *sc = arg;
593 KMI_CTX_LOCK_ASSERT();
595 if ((sc->sc_flags & KMI_FLAG_POLLING) != 0)
598 if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
599 KBD_IS_BUSY(&sc->sc_kbd)) {
600 /* let the callback function process the input */
601 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
602 sc->sc_kbd.kb_callback.kc_arg);
604 /* read and discard the input, no one is waiting for it */
606 c = kmi_read_char_locked(&sc->sc_kbd, 0);
607 } while (c != NOKEY);
613 pl050_kmi_probe(device_t dev)
616 if (!ofw_bus_status_okay(dev))
620 * PL050 is plain PS2 port that pushes bytes to/from computer
621 * VersatilePB has two such ports and QEMU simulates keyboard
622 * connected to port #0 and mouse connected to port #1. This
623 * information can't be obtained from device tree so we just
624 * hardcode this knowledge here. We attach keyboard driver to
625 * port #0 and ignore port #1
630 if (ofw_bus_is_compatible(dev, "arm,pl050")) {
631 device_set_desc(dev, "PL050 Keyboard/Mouse Interface");
632 return (BUS_PROBE_DEFAULT);
639 pl050_kmi_attach(device_t dev)
641 struct kmi_softc *sc = device_get_softc(dev);
651 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
652 if (sc->sc_mem_res == NULL) {
653 device_printf(dev, "could not allocate memory resource\n");
657 /* Request the IRQ resources */
658 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
659 if (sc->sc_irq_res == NULL) {
660 device_printf(dev, "Error: could not allocate irq resources\n");
664 /* Setup and enable the timer */
665 if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_CLK,
666 NULL, pl050_kmi_intr, sc,
667 &sc->sc_intr_hl) != 0) {
668 bus_release_resource(dev, SYS_RES_IRQ, rid,
670 device_printf(dev, "Unable to setup the clock irq handler.\n");
674 /* TODO: clock & divisor */
676 pl050_kmi_write_4(sc, KMICR, KMICR_EN);
678 pl050_kmi_write_4(sc, KMIDATA, SET_SCANCODE_SET);
680 ack = pl050_kmi_read_4(sc, KMIDATA);
681 /* Set Scan Code set 1 (XT) */
682 pl050_kmi_write_4(sc, KMIDATA, 1);
684 ack = pl050_kmi_read_4(sc, KMIDATA);
686 pl050_kmi_write_4(sc, KMICR, KMICR_EN | KMICR_RXINTREN);
688 kbd_init_struct(kbd, KMI_DRIVER_NAME, KB_OTHER,
689 device_get_unit(dev), 0, 0, 0);
690 kbd->kb_data = (void *)sc;
692 sc->sc_keymap = key_map;
693 sc->sc_accmap = accent_map;
694 for (i = 0; i < KMI_NFKEY; i++) {
695 sc->sc_fkeymap[i] = fkey_tab[i];
698 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
699 sc->sc_fkeymap, KMI_NFKEY);
701 KBD_FOUND_DEVICE(kbd);
702 kmi_clear_state(kbd);
707 if (kbd_register(kbd) < 0) {
710 KBD_CONFIG_DONE(kbd);
712 #ifdef KBD_INSTALL_CDEV
713 if (kbd_attach(kbd)) {
719 genkbd_diag(kbd, bootverbose);
729 static device_method_t pl050_kmi_methods[] = {
730 DEVMETHOD(device_probe, pl050_kmi_probe),
731 DEVMETHOD(device_attach, pl050_kmi_attach),
735 static driver_t pl050_kmi_driver = {
738 sizeof(struct kmi_softc),
741 static devclass_t pl050_kmi_devclass;
743 DRIVER_MODULE(pl050_kmi, simplebus, pl050_kmi_driver, pl050_kmi_devclass, 0, 0);