2 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
5 * Based on dev/usb/input/ukbd.c
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/malloc.h>
40 #include <sys/sched.h>
43 #include <machine/bus.h>
44 #include <machine/cpu.h>
45 #include <machine/intr.h>
47 #include <dev/fdt/fdt_common.h>
48 #include <dev/ofw/openfirm.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
52 #include <sys/ioccom.h>
53 #include <sys/filio.h>
57 #include <dev/kbd/kbdreg.h>
59 #include <machine/bus.h>
60 #include <machine/fdt.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 */
114 accentmap_t sc_accmap;
115 fkeytab_t sc_fkeymap[KMI_NFKEY];
117 struct resource* sc_mem_res;
118 struct resource* sc_irq_res;
121 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
122 int sc_state; /* shift/lock key state */
123 int sc_accents; /* accent key index (> 0) */
124 uint32_t sc_flags; /* flags */
125 #define KMI_FLAG_COMPOSE 0x00000001
126 #define KMI_FLAG_POLLING 0x00000002
128 struct thread *sc_poll_thread;
131 /* Read/Write macros for Timer used as timecounter */
132 #define pl050_kmi_read_4(sc, reg) \
133 bus_read_4((sc)->sc_mem_res, (reg))
135 #define pl050_kmi_write_4(sc, reg, val) \
136 bus_write_4((sc)->sc_mem_res, (reg), (val))
139 static void kmi_set_leds(struct kmi_softc *, uint8_t);
140 static int kmi_set_typematic(keyboard_t *, int);
141 static uint32_t kmi_read_char(keyboard_t *, int);
142 static void kmi_clear_state(keyboard_t *);
143 static int kmi_ioctl(keyboard_t *, u_long, caddr_t);
144 static int kmi_enable(keyboard_t *);
145 static int kmi_disable(keyboard_t *);
147 /* early keyboard probe, not supported */
149 kmi_configure(int flags)
154 /* detect a keyboard, not used */
156 kmi_probe(int unit, void *arg, int flags)
161 /* reset and initialize the device, not used */
163 kmi_init(int unit, keyboard_t **kbdp, void *arg, int flags)
168 /* test the interface to the device, not used */
170 kmi_test_if(keyboard_t *kbd)
175 /* finish using this keyboard, not used */
177 kmi_term(keyboard_t *kbd)
182 /* keyboard interrupt routine, not used */
184 kmi_intr(keyboard_t *kbd, void *arg)
190 /* lock the access to the keyboard, not used */
192 kmi_lock(keyboard_t *kbd, int lock)
198 * Enable the access to the device; until this function is called,
199 * the client cannot read from the keyboard.
202 kmi_enable(keyboard_t *kbd)
212 /* disallow the access to the device */
214 kmi_disable(keyboard_t *kbd)
224 /* check if data is waiting */
226 kmi_check(keyboard_t *kbd)
228 struct kmi_softc *sc = kbd->kb_data;
231 KMI_CTX_LOCK_ASSERT();
233 if (!KBD_IS_ACTIVE(kbd))
236 reg = pl050_kmi_read_4(sc, KMIIR);
237 return (reg & KMIIR_RXINTR);
240 /* check if char is waiting */
242 kmi_check_char_locked(keyboard_t *kbd)
244 KMI_CTX_LOCK_ASSERT();
246 if (!KBD_IS_ACTIVE(kbd))
249 return (kmi_check(kbd));
253 kmi_check_char(keyboard_t *kbd)
258 result = kmi_check_char_locked(kbd);
264 /* read one byte from the keyboard if it's allowed */
265 /* Currently unused. */
267 kmi_read(keyboard_t *kbd, int wait)
269 KMI_CTX_LOCK_ASSERT();
271 if (!KBD_IS_ACTIVE(kbd))
275 printf("Implement ME: %s\n", __func__);
279 /* read char from the keyboard */
281 kmi_read_char_locked(keyboard_t *kbd, int wait)
283 struct kmi_softc *sc = kbd->kb_data;
286 KMI_CTX_LOCK_ASSERT();
288 if (!KBD_IS_ACTIVE(kbd))
291 reg = pl050_kmi_read_4(sc, KMIIR);
292 if (reg & KMIIR_RXINTR) {
293 data = pl050_kmi_read_4(sc, KMIDATA);
301 /* Currently wait is always false. */
303 kmi_read_char(keyboard_t *kbd, int wait)
308 keycode = kmi_read_char_locked(kbd, wait);
314 /* some useful control functions */
316 kmi_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
318 struct kmi_softc *sc = kbd->kb_data;
320 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
321 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
329 case KDGKBMODE: /* get keyboard mode */
330 *(int *)arg = sc->sc_mode;
332 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
333 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
335 ival = IOCPARM_IVAL(arg);
336 arg = (caddr_t)&ival;
339 case KDSKBMODE: /* set keyboard mode */
340 switch (*(int *)arg) {
342 if (sc->sc_mode != K_XLATE) {
343 /* make lock key state and LED state match */
344 sc->sc_state &= ~LOCK_MASK;
345 sc->sc_state |= KBD_LED_VAL(kbd);
350 if (sc->sc_mode != *(int *)arg) {
351 if ((sc->sc_flags & KMI_FLAG_POLLING) == 0)
352 kmi_clear_state(kbd);
353 sc->sc_mode = *(int *)arg;
361 case KDGETLED: /* get keyboard LED */
362 *(int *)arg = KBD_LED_VAL(kbd);
364 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
365 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
367 ival = IOCPARM_IVAL(arg);
368 arg = (caddr_t)&ival;
371 case KDSETLED: /* set keyboard LED */
372 /* NOTE: lock key state in "sc_state" won't be changed */
373 if (*(int *)arg & ~LOCK_MASK)
378 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
379 if (sc->sc_mode == K_XLATE &&
380 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
386 if (KBD_HAS_DEVICE(kbd))
389 KBD_LED_VAL(kbd) = *(int *)arg;
391 case KDGKBSTATE: /* get lock key state */
392 *(int *)arg = sc->sc_state & LOCK_MASK;
394 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
395 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
397 ival = IOCPARM_IVAL(arg);
398 arg = (caddr_t)&ival;
401 case KDSKBSTATE: /* set lock key state */
402 if (*(int *)arg & ~LOCK_MASK) {
405 sc->sc_state &= ~LOCK_MASK;
406 sc->sc_state |= *(int *)arg;
408 /* set LEDs and quit */
409 return (kmi_ioctl(kbd, KDSETLED, arg));
411 case KDSETREPEAT: /* set keyboard repeat rate (new
413 if (!KBD_HAS_DEVICE(kbd)) {
416 if (((int *)arg)[1] < 0) {
419 if (((int *)arg)[0] < 0) {
422 if (((int *)arg)[0] < 200) /* fastest possible value */
423 kbd->kb_delay1 = 200;
425 kbd->kb_delay1 = ((int *)arg)[0];
426 kbd->kb_delay2 = ((int *)arg)[1];
429 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
430 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
432 ival = IOCPARM_IVAL(arg);
433 arg = (caddr_t)&ival;
436 case KDSETRAD: /* set keyboard repeat rate (old
438 return (kmi_set_typematic(kbd, *(int *)arg));
440 case PIO_KEYMAP: /* set keyboard translation table */
441 case OPIO_KEYMAP: /* set keyboard translation table
443 case PIO_KEYMAPENT: /* set keyboard translation table
445 case PIO_DEADKEYMAP: /* set accent key translation table */
449 return (genkbd_commonioctl(kbd, cmd, arg));
456 kmi_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
461 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
462 * context where printf(9) can be called, which among other things
463 * includes interrupt filters and threads with any kinds of locks
464 * already held. For this reason it would be dangerous to acquire
465 * the Giant here unconditionally. On the other hand we have to
466 * have it to handle the ioctl.
467 * So we make our best effort to auto-detect whether we can grab
468 * the Giant or not. Blame syscons(4) for this.
474 if (!mtx_owned(&Giant) && !SCHEDULER_STOPPED())
475 return (EDEADLK); /* best I could come up with */
479 result = kmi_ioctl_locked(kbd, cmd, arg);
486 /* clear the internal state of the keyboard */
488 kmi_clear_state(keyboard_t *kbd)
490 struct kmi_softc *sc = kbd->kb_data;
492 KMI_CTX_LOCK_ASSERT();
494 sc->sc_flags &= ~(KMI_FLAG_COMPOSE | KMI_FLAG_POLLING);
495 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
499 /* save the internal state, not used */
501 kmi_get_state(keyboard_t *kbd, void *buf, size_t len)
503 return (len == 0) ? 1 : -1;
506 /* set the internal state, not used */
508 kmi_set_state(keyboard_t *kbd, void *buf, size_t len)
514 kmi_poll(keyboard_t *kbd, int on)
516 struct kmi_softc *sc = kbd->kb_data;
520 sc->sc_flags |= KMI_FLAG_POLLING;
521 sc->sc_poll_thread = curthread;
523 sc->sc_flags &= ~KMI_FLAG_POLLING;
530 /* local functions */
533 kmi_set_leds(struct kmi_softc *sc, uint8_t leds)
538 /* start transfer, if not already started */
539 printf("Implement me: %s\n", __func__);
543 kmi_set_typematic(keyboard_t *kbd, int code)
545 static const int delays[] = {250, 500, 750, 1000};
546 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
547 68, 76, 84, 92, 100, 110, 118, 126,
548 136, 152, 168, 184, 200, 220, 236, 252,
549 272, 304, 336, 368, 400, 440, 472, 504};
554 kbd->kb_delay1 = delays[(code >> 5) & 3];
555 kbd->kb_delay2 = rates[code & 0x1f];
559 static keyboard_switch_t kmisw = {
564 .test_if = &kmi_test_if,
565 .enable = &kmi_enable,
566 .disable = &kmi_disable,
569 .read_char = &kmi_read_char,
570 .check_char = &kmi_check_char,
573 .clear_state = &kmi_clear_state,
574 .get_state = &kmi_get_state,
575 .set_state = &kmi_set_state,
576 .get_fkeystr = &genkbd_get_fkeystr,
578 .diag = &genkbd_diag,
581 KEYBOARD_DRIVER(kmi, kmisw, kmi_configure);
584 pl050_kmi_intr(void *arg)
586 struct kmi_softc *sc = arg;
589 KMI_CTX_LOCK_ASSERT();
591 if ((sc->sc_flags & KMI_FLAG_POLLING) != 0)
594 if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
595 KBD_IS_BUSY(&sc->sc_kbd)) {
596 /* let the callback function process the input */
597 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
598 sc->sc_kbd.kb_callback.kc_arg);
600 /* read and discard the input, no one is waiting for it */
602 c = kmi_read_char_locked(&sc->sc_kbd, 0);
603 } while (c != NOKEY);
609 pl050_kmi_probe(device_t dev)
612 if (!ofw_bus_status_okay(dev))
615 if (ofw_bus_is_compatible(dev, "arm,pl050")) {
616 device_set_desc(dev, "PL050 Keyboard/Mouse Interface");
617 return (BUS_PROBE_DEFAULT);
624 pl050_kmi_attach(device_t dev)
626 struct kmi_softc *sc = device_get_softc(dev);
634 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
635 if (sc->sc_mem_res == NULL) {
636 device_printf(dev, "could not allocate memory resource\n");
640 /* Request the IRQ resources */
641 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
642 if (sc->sc_irq_res == NULL) {
643 device_printf(dev, "Error: could not allocate irq resources\n");
647 /* Setup and enable the timer */
648 if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_CLK,
649 NULL, pl050_kmi_intr, sc,
650 &sc->sc_intr_hl) != 0) {
651 bus_release_resource(dev, SYS_RES_IRQ, rid,
653 device_printf(dev, "Unable to setup the clock irq handler.\n");
657 /* TODO: clock & divisor */
659 pl050_kmi_write_4(sc, KMICR, KMICR_EN | KMICR_RXINTREN);
661 kbd_init_struct(kbd, KMI_DRIVER_NAME, KB_OTHER,
662 device_get_unit(dev), 0, 0, 0);
663 kbd->kb_data = (void *)sc;
665 sc->sc_keymap = key_map;
666 sc->sc_accmap = accent_map;
667 for (i = 0; i < KMI_NFKEY; i++) {
668 sc->sc_fkeymap[i] = fkey_tab[i];
671 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
672 sc->sc_fkeymap, KMI_NFKEY);
674 KBD_FOUND_DEVICE(kbd);
675 kmi_clear_state(kbd);
680 if (kbd_register(kbd) < 0) {
683 KBD_CONFIG_DONE(kbd);
685 #ifdef KBD_INSTALL_CDEV
686 if (kbd_attach(kbd)) {
692 genkbd_diag(kbd, bootverbose);
701 static device_method_t pl050_kmi_methods[] = {
702 DEVMETHOD(device_probe, pl050_kmi_probe),
703 DEVMETHOD(device_attach, pl050_kmi_attach),
707 static driver_t pl050_kmi_driver = {
710 sizeof(struct kmi_softc),
713 static devclass_t pl050_kmi_devclass;
715 DRIVER_MODULE(pl050_kmi, simplebus, pl050_kmi_driver, pl050_kmi_devclass, 0, 0);