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>
39 #include <sys/module.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
44 #include <sys/sched.h>
47 #include <machine/bus.h>
48 #include <machine/cpu.h>
49 #include <machine/intr.h>
51 #include <dev/ofw/openfirm.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
55 #include <sys/ioccom.h>
56 #include <sys/filio.h>
59 #include <dev/kbd/kbdreg.h>
61 #include <machine/bus.h>
63 #include <dev/kbd/kbdtables.h>
65 #define KMI_LOCK() mtx_lock(&Giant)
66 #define KMI_UNLOCK() mtx_unlock(&Giant)
70 * Assert that the lock is held in all contexts
71 * where the code can be executed.
73 #define KMI_LOCK_ASSERT() mtx_assert(&Giant, MA_OWNED)
75 * Assert that the lock is held in the contexts
76 * where it really has to be so.
78 #define KMI_CTX_LOCK_ASSERT() \
80 if (!kdb_active && !KERNEL_PANICKED()) \
81 mtx_assert(&Giant, MA_OWNED); \
84 #define KMI_LOCK_ASSERT() (void)0
85 #define KMI_CTX_LOCK_ASSERT() (void)0
89 #define KMICR_TYPE_NONPS2 (1 << 5)
90 #define KMICR_RXINTREN (1 << 4)
91 #define KMICR_TXINTREN (1 << 3)
92 #define KMICR_EN (1 << 2)
93 #define KMICR_FKMID (1 << 1)
94 #define KMICR_FKMIC (1 << 0)
96 #define KMISTAT_TXEMPTY (1 << 6)
97 #define KMISTAT_TXBUSY (1 << 5)
98 #define KMISTAT_RXFULL (1 << 4)
99 #define KMISTAT_RXBUSY (1 << 3)
100 #define KMISTAT_RXPARITY (1 << 2)
101 #define KMISTAT_KMIC (1 << 1)
102 #define KMISTAT_KMID (1 << 0)
104 #define KMICLKDIV 0x0C
106 #define KMIIR_TXINTR (1 << 1)
107 #define KMIIR_RXINTR (1 << 0)
109 #define KMI_DRIVER_NAME "kmi"
110 #define KMI_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */
112 #define SET_SCANCODE_SET 0xf0
118 accentmap_t sc_accmap;
119 fkeytab_t sc_fkeymap[KMI_NFKEY];
121 struct resource* sc_mem_res;
122 struct resource* sc_irq_res;
125 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
126 int sc_state; /* shift/lock key state */
127 int sc_accents; /* accent key index (> 0) */
128 uint32_t sc_flags; /* flags */
129 #define KMI_FLAG_COMPOSE 0x00000001
130 #define KMI_FLAG_POLLING 0x00000002
132 struct thread *sc_poll_thread;
135 /* Read/Write macros for Timer used as timecounter */
136 #define pl050_kmi_read_4(sc, reg) \
137 bus_read_4((sc)->sc_mem_res, (reg))
139 #define pl050_kmi_write_4(sc, reg, val) \
140 bus_write_4((sc)->sc_mem_res, (reg), (val))
143 static void kmi_set_leds(struct kmi_softc *, uint8_t);
144 static int kmi_set_typematic(keyboard_t *, int);
145 static uint32_t kmi_read_char(keyboard_t *, int);
146 static void kmi_clear_state(keyboard_t *);
147 static int kmi_ioctl(keyboard_t *, u_long, caddr_t);
148 static int kmi_enable(keyboard_t *);
149 static int kmi_disable(keyboard_t *);
151 static int kmi_attached = 0;
153 /* early keyboard probe, not supported */
155 kmi_configure(int flags)
160 /* detect a keyboard, not used */
162 kmi_probe(int unit, void *arg, int flags)
167 /* reset and initialize the device, not used */
169 kmi_init(int unit, keyboard_t **kbdp, void *arg, int flags)
174 /* test the interface to the device, not used */
176 kmi_test_if(keyboard_t *kbd)
181 /* finish using this keyboard, not used */
183 kmi_term(keyboard_t *kbd)
188 /* keyboard interrupt routine, not used */
190 kmi_intr(keyboard_t *kbd, void *arg)
196 /* lock the access to the keyboard, not used */
198 kmi_lock(keyboard_t *kbd, int lock)
204 * Enable the access to the device; until this function is called,
205 * the client cannot read from the keyboard.
208 kmi_enable(keyboard_t *kbd)
218 /* disallow the access to the device */
220 kmi_disable(keyboard_t *kbd)
230 /* check if data is waiting */
232 kmi_check(keyboard_t *kbd)
234 struct kmi_softc *sc = kbd->kb_data;
237 KMI_CTX_LOCK_ASSERT();
239 if (!KBD_IS_ACTIVE(kbd))
242 reg = pl050_kmi_read_4(sc, KMIIR);
243 return (reg & KMIIR_RXINTR);
246 /* check if char is waiting */
248 kmi_check_char_locked(keyboard_t *kbd)
250 KMI_CTX_LOCK_ASSERT();
252 if (!KBD_IS_ACTIVE(kbd))
255 return (kmi_check(kbd));
259 kmi_check_char(keyboard_t *kbd)
264 result = kmi_check_char_locked(kbd);
270 /* read one byte from the keyboard if it's allowed */
271 /* Currently unused. */
273 kmi_read(keyboard_t *kbd, int wait)
275 KMI_CTX_LOCK_ASSERT();
277 if (!KBD_IS_ACTIVE(kbd))
281 printf("Implement ME: %s\n", __func__);
285 /* read char from the keyboard */
287 kmi_read_char_locked(keyboard_t *kbd, int wait)
289 struct kmi_softc *sc = kbd->kb_data;
292 KMI_CTX_LOCK_ASSERT();
294 if (!KBD_IS_ACTIVE(kbd))
297 reg = pl050_kmi_read_4(sc, KMIIR);
298 if (reg & KMIIR_RXINTR) {
299 data = pl050_kmi_read_4(sc, KMIDATA);
307 /* Currently wait is always false. */
309 kmi_read_char(keyboard_t *kbd, int wait)
314 keycode = kmi_read_char_locked(kbd, wait);
320 /* some useful control functions */
322 kmi_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
324 struct kmi_softc *sc = kbd->kb_data;
326 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
327 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
335 case KDGKBMODE: /* get keyboard mode */
336 *(int *)arg = sc->sc_mode;
338 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
339 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
341 ival = IOCPARM_IVAL(arg);
342 arg = (caddr_t)&ival;
345 case KDSKBMODE: /* set keyboard mode */
346 switch (*(int *)arg) {
348 if (sc->sc_mode != K_XLATE) {
349 /* make lock key state and LED state match */
350 sc->sc_state &= ~LOCK_MASK;
351 sc->sc_state |= KBD_LED_VAL(kbd);
356 if (sc->sc_mode != *(int *)arg) {
357 if ((sc->sc_flags & KMI_FLAG_POLLING) == 0)
358 kmi_clear_state(kbd);
359 sc->sc_mode = *(int *)arg;
367 case KDGETLED: /* get keyboard LED */
368 *(int *)arg = KBD_LED_VAL(kbd);
370 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
371 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
373 ival = IOCPARM_IVAL(arg);
374 arg = (caddr_t)&ival;
377 case KDSETLED: /* set keyboard LED */
378 /* NOTE: lock key state in "sc_state" won't be changed */
379 if (*(int *)arg & ~LOCK_MASK)
384 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
385 if (sc->sc_mode == K_XLATE &&
386 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
392 if (KBD_HAS_DEVICE(kbd))
395 KBD_LED_VAL(kbd) = *(int *)arg;
397 case KDGKBSTATE: /* get lock key state */
398 *(int *)arg = sc->sc_state & LOCK_MASK;
400 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
401 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
403 ival = IOCPARM_IVAL(arg);
404 arg = (caddr_t)&ival;
407 case KDSKBSTATE: /* set lock key state */
408 if (*(int *)arg & ~LOCK_MASK) {
411 sc->sc_state &= ~LOCK_MASK;
412 sc->sc_state |= *(int *)arg;
414 /* set LEDs and quit */
415 return (kmi_ioctl(kbd, KDSETLED, arg));
417 case KDSETREPEAT: /* set keyboard repeat rate (new
419 if (!KBD_HAS_DEVICE(kbd)) {
422 if (((int *)arg)[1] < 0) {
425 if (((int *)arg)[0] < 0) {
428 if (((int *)arg)[0] < 200) /* fastest possible value */
429 kbd->kb_delay1 = 200;
431 kbd->kb_delay1 = ((int *)arg)[0];
432 kbd->kb_delay2 = ((int *)arg)[1];
435 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
436 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
438 ival = IOCPARM_IVAL(arg);
439 arg = (caddr_t)&ival;
442 case KDSETRAD: /* set keyboard repeat rate (old
444 return (kmi_set_typematic(kbd, *(int *)arg));
446 case PIO_KEYMAP: /* set keyboard translation table */
447 case OPIO_KEYMAP: /* set keyboard translation table
449 case PIO_KEYMAPENT: /* set keyboard translation table
451 case PIO_DEADKEYMAP: /* set accent key translation table */
455 return (genkbd_commonioctl(kbd, cmd, arg));
462 kmi_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
467 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
468 * context where printf(9) can be called, which among other things
469 * includes interrupt filters and threads with any kinds of locks
470 * already held. For this reason it would be dangerous to acquire
471 * the Giant here unconditionally. On the other hand we have to
472 * have it to handle the ioctl.
473 * So we make our best effort to auto-detect whether we can grab
474 * the Giant or not. Blame syscons(4) for this.
480 if (!mtx_owned(&Giant) && !SCHEDULER_STOPPED())
481 return (EDEADLK); /* best I could come up with */
485 result = kmi_ioctl_locked(kbd, cmd, arg);
491 /* clear the internal state of the keyboard */
493 kmi_clear_state(keyboard_t *kbd)
495 struct kmi_softc *sc = kbd->kb_data;
497 KMI_CTX_LOCK_ASSERT();
499 sc->sc_flags &= ~(KMI_FLAG_COMPOSE | KMI_FLAG_POLLING);
500 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
504 /* save the internal state, not used */
506 kmi_get_state(keyboard_t *kbd, void *buf, size_t len)
508 return (len == 0) ? 1 : -1;
511 /* set the internal state, not used */
513 kmi_set_state(keyboard_t *kbd, void *buf, size_t len)
519 kmi_poll(keyboard_t *kbd, int on)
521 struct kmi_softc *sc = kbd->kb_data;
525 sc->sc_flags |= KMI_FLAG_POLLING;
526 sc->sc_poll_thread = curthread;
528 sc->sc_flags &= ~KMI_FLAG_POLLING;
535 /* local functions */
538 kmi_set_leds(struct kmi_softc *sc, uint8_t leds)
543 /* start transfer, if not already started */
544 printf("Implement me: %s\n", __func__);
548 kmi_set_typematic(keyboard_t *kbd, int code)
550 static const int delays[] = {250, 500, 750, 1000};
551 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
552 68, 76, 84, 92, 100, 110, 118, 126,
553 136, 152, 168, 184, 200, 220, 236, 252,
554 272, 304, 336, 368, 400, 440, 472, 504};
559 kbd->kb_delay1 = delays[(code >> 5) & 3];
560 kbd->kb_delay2 = rates[code & 0x1f];
564 static keyboard_switch_t kmisw = {
569 .test_if = &kmi_test_if,
570 .enable = &kmi_enable,
571 .disable = &kmi_disable,
574 .read_char = &kmi_read_char,
575 .check_char = &kmi_check_char,
578 .clear_state = &kmi_clear_state,
579 .get_state = &kmi_get_state,
580 .set_state = &kmi_set_state,
584 KEYBOARD_DRIVER(kmi, kmisw, kmi_configure);
587 pl050_kmi_intr(void *arg)
589 struct kmi_softc *sc = arg;
592 KMI_CTX_LOCK_ASSERT();
594 if ((sc->sc_flags & KMI_FLAG_POLLING) != 0)
597 if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
598 KBD_IS_BUSY(&sc->sc_kbd)) {
599 /* let the callback function process the input */
600 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
601 sc->sc_kbd.kb_callback.kc_arg);
603 /* read and discard the input, no one is waiting for it */
605 c = kmi_read_char_locked(&sc->sc_kbd, 0);
606 } while (c != NOKEY);
612 pl050_kmi_probe(device_t dev)
615 if (!ofw_bus_status_okay(dev))
619 * PL050 is plain PS2 port that pushes bytes to/from computer
620 * VersatilePB has two such ports and QEMU simulates keyboard
621 * connected to port #0 and mouse connected to port #1. This
622 * information can't be obtained from device tree so we just
623 * hardcode this knowledge here. We attach keyboard driver to
624 * port #0 and ignore port #1
629 if (ofw_bus_is_compatible(dev, "arm,pl050")) {
630 device_set_desc(dev, "PL050 Keyboard/Mouse Interface");
631 return (BUS_PROBE_DEFAULT);
638 pl050_kmi_attach(device_t dev)
640 struct kmi_softc *sc = device_get_softc(dev);
650 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
651 if (sc->sc_mem_res == NULL) {
652 device_printf(dev, "could not allocate memory resource\n");
656 /* Request the IRQ resources */
657 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
658 if (sc->sc_irq_res == NULL) {
659 device_printf(dev, "Error: could not allocate irq resources\n");
663 /* Setup and enable the timer */
664 if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_CLK,
665 NULL, pl050_kmi_intr, sc,
666 &sc->sc_intr_hl) != 0) {
667 bus_release_resource(dev, SYS_RES_IRQ, rid,
669 device_printf(dev, "Unable to setup the clock irq handler.\n");
673 /* TODO: clock & divisor */
675 pl050_kmi_write_4(sc, KMICR, KMICR_EN);
677 pl050_kmi_write_4(sc, KMIDATA, SET_SCANCODE_SET);
679 ack = pl050_kmi_read_4(sc, KMIDATA);
680 /* Set Scan Code set 1 (XT) */
681 pl050_kmi_write_4(sc, KMIDATA, 1);
683 ack = pl050_kmi_read_4(sc, KMIDATA);
685 pl050_kmi_write_4(sc, KMICR, KMICR_EN | KMICR_RXINTREN);
687 kbd_init_struct(kbd, KMI_DRIVER_NAME, KB_OTHER,
688 device_get_unit(dev), 0, 0, 0);
689 kbd->kb_data = (void *)sc;
691 sc->sc_keymap = key_map;
692 sc->sc_accmap = accent_map;
693 for (i = 0; i < KMI_NFKEY; i++) {
694 sc->sc_fkeymap[i] = fkey_tab[i];
697 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
698 sc->sc_fkeymap, KMI_NFKEY);
700 KBD_FOUND_DEVICE(kbd);
701 kmi_clear_state(kbd);
706 if (kbd_register(kbd) < 0) {
709 KBD_CONFIG_DONE(kbd);
711 #ifdef KBD_INSTALL_CDEV
712 if (kbd_attach(kbd)) {
718 kbdd_diag(kbd, bootverbose);
728 static device_method_t pl050_kmi_methods[] = {
729 DEVMETHOD(device_probe, pl050_kmi_probe),
730 DEVMETHOD(device_attach, pl050_kmi_attach),
734 static driver_t pl050_kmi_driver = {
737 sizeof(struct kmi_softc),
740 static devclass_t pl050_kmi_devclass;
742 DRIVER_MODULE(pl050_kmi, simplebus, pl050_kmi_driver, pl050_kmi_devclass, 0, 0);