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>
61 #include <dev/kbd/kbdtables.h>
63 #define KMI_LOCK() mtx_lock(&Giant)
64 #define KMI_UNLOCK() mtx_unlock(&Giant)
68 * Assert that the lock is held in all contexts
69 * where the code can be executed.
71 #define KMI_LOCK_ASSERT() mtx_assert(&Giant, MA_OWNED)
73 * Assert that the lock is held in the contexts
74 * where it really has to be so.
76 #define KMI_CTX_LOCK_ASSERT() \
78 if (!kdb_active && panicstr == NULL) \
79 mtx_assert(&Giant, MA_OWNED); \
82 #define KMI_LOCK_ASSERT() (void)0
83 #define KMI_CTX_LOCK_ASSERT() (void)0
87 #define KMICR_TYPE_NONPS2 (1 << 5)
88 #define KMICR_RXINTREN (1 << 4)
89 #define KMICR_TXINTREN (1 << 3)
90 #define KMICR_EN (1 << 2)
91 #define KMICR_FKMID (1 << 1)
92 #define KMICR_FKMIC (1 << 0)
94 #define KMISTAT_TXEMPTY (1 << 6)
95 #define KMISTAT_TXBUSY (1 << 5)
96 #define KMISTAT_RXFULL (1 << 4)
97 #define KMISTAT_RXBUSY (1 << 3)
98 #define KMISTAT_RXPARITY (1 << 2)
99 #define KMISTAT_KMIC (1 << 1)
100 #define KMISTAT_KMID (1 << 0)
102 #define KMICLKDIV 0x0C
104 #define KMIIR_TXINTR (1 << 1)
105 #define KMIIR_RXINTR (1 << 0)
107 #define KMI_DRIVER_NAME "kmi"
108 #define KMI_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */
113 accentmap_t sc_accmap;
114 fkeytab_t sc_fkeymap[KMI_NFKEY];
116 struct resource* sc_mem_res;
117 struct resource* sc_irq_res;
120 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
121 int sc_state; /* shift/lock key state */
122 int sc_accents; /* accent key index (> 0) */
123 uint32_t sc_flags; /* flags */
124 #define KMI_FLAG_COMPOSE 0x00000001
125 #define KMI_FLAG_POLLING 0x00000002
127 struct thread *sc_poll_thread;
130 /* Read/Write macros for Timer used as timecounter */
131 #define pl050_kmi_read_4(sc, reg) \
132 bus_read_4((sc)->sc_mem_res, (reg))
134 #define pl050_kmi_write_4(sc, reg, val) \
135 bus_write_4((sc)->sc_mem_res, (reg), (val))
138 static void kmi_set_leds(struct kmi_softc *, uint8_t);
139 static int kmi_set_typematic(keyboard_t *, int);
140 static uint32_t kmi_read_char(keyboard_t *, int);
141 static void kmi_clear_state(keyboard_t *);
142 static int kmi_ioctl(keyboard_t *, u_long, caddr_t);
143 static int kmi_enable(keyboard_t *);
144 static int kmi_disable(keyboard_t *);
146 /* early keyboard probe, not supported */
148 kmi_configure(int flags)
153 /* detect a keyboard, not used */
155 kmi_probe(int unit, void *arg, int flags)
160 /* reset and initialize the device, not used */
162 kmi_init(int unit, keyboard_t **kbdp, void *arg, int flags)
167 /* test the interface to the device, not used */
169 kmi_test_if(keyboard_t *kbd)
174 /* finish using this keyboard, not used */
176 kmi_term(keyboard_t *kbd)
181 /* keyboard interrupt routine, not used */
183 kmi_intr(keyboard_t *kbd, void *arg)
189 /* lock the access to the keyboard, not used */
191 kmi_lock(keyboard_t *kbd, int lock)
197 * Enable the access to the device; until this function is called,
198 * the client cannot read from the keyboard.
201 kmi_enable(keyboard_t *kbd)
211 /* disallow the access to the device */
213 kmi_disable(keyboard_t *kbd)
223 /* check if data is waiting */
225 kmi_check(keyboard_t *kbd)
227 struct kmi_softc *sc = kbd->kb_data;
230 KMI_CTX_LOCK_ASSERT();
232 if (!KBD_IS_ACTIVE(kbd))
235 reg = pl050_kmi_read_4(sc, KMIIR);
236 return (reg & KMIIR_RXINTR);
239 /* check if char is waiting */
241 kmi_check_char_locked(keyboard_t *kbd)
243 KMI_CTX_LOCK_ASSERT();
245 if (!KBD_IS_ACTIVE(kbd))
248 return (kmi_check(kbd));
252 kmi_check_char(keyboard_t *kbd)
257 result = kmi_check_char_locked(kbd);
263 /* read one byte from the keyboard if it's allowed */
264 /* Currently unused. */
266 kmi_read(keyboard_t *kbd, int wait)
268 KMI_CTX_LOCK_ASSERT();
270 if (!KBD_IS_ACTIVE(kbd))
274 printf("Implement ME: %s\n", __func__);
278 /* read char from the keyboard */
280 kmi_read_char_locked(keyboard_t *kbd, int wait)
282 struct kmi_softc *sc = kbd->kb_data;
285 KMI_CTX_LOCK_ASSERT();
287 if (!KBD_IS_ACTIVE(kbd))
290 reg = pl050_kmi_read_4(sc, KMIIR);
291 if (reg & KMIIR_RXINTR) {
292 data = pl050_kmi_read_4(sc, KMIDATA);
300 /* Currently wait is always false. */
302 kmi_read_char(keyboard_t *kbd, int wait)
307 keycode = kmi_read_char_locked(kbd, wait);
313 /* some useful control functions */
315 kmi_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
317 struct kmi_softc *sc = kbd->kb_data;
319 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
320 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
328 case KDGKBMODE: /* get keyboard mode */
329 *(int *)arg = sc->sc_mode;
331 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
332 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
334 ival = IOCPARM_IVAL(arg);
335 arg = (caddr_t)&ival;
338 case KDSKBMODE: /* set keyboard mode */
339 switch (*(int *)arg) {
341 if (sc->sc_mode != K_XLATE) {
342 /* make lock key state and LED state match */
343 sc->sc_state &= ~LOCK_MASK;
344 sc->sc_state |= KBD_LED_VAL(kbd);
349 if (sc->sc_mode != *(int *)arg) {
350 if ((sc->sc_flags & KMI_FLAG_POLLING) == 0)
351 kmi_clear_state(kbd);
352 sc->sc_mode = *(int *)arg;
360 case KDGETLED: /* get keyboard LED */
361 *(int *)arg = KBD_LED_VAL(kbd);
363 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
364 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
366 ival = IOCPARM_IVAL(arg);
367 arg = (caddr_t)&ival;
370 case KDSETLED: /* set keyboard LED */
371 /* NOTE: lock key state in "sc_state" won't be changed */
372 if (*(int *)arg & ~LOCK_MASK)
377 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
378 if (sc->sc_mode == K_XLATE &&
379 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
385 if (KBD_HAS_DEVICE(kbd))
388 KBD_LED_VAL(kbd) = *(int *)arg;
390 case KDGKBSTATE: /* get lock key state */
391 *(int *)arg = sc->sc_state & LOCK_MASK;
393 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
394 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
396 ival = IOCPARM_IVAL(arg);
397 arg = (caddr_t)&ival;
400 case KDSKBSTATE: /* set lock key state */
401 if (*(int *)arg & ~LOCK_MASK) {
404 sc->sc_state &= ~LOCK_MASK;
405 sc->sc_state |= *(int *)arg;
407 /* set LEDs and quit */
408 return (kmi_ioctl(kbd, KDSETLED, arg));
410 case KDSETREPEAT: /* set keyboard repeat rate (new
412 if (!KBD_HAS_DEVICE(kbd)) {
415 if (((int *)arg)[1] < 0) {
418 if (((int *)arg)[0] < 0) {
421 if (((int *)arg)[0] < 200) /* fastest possible value */
422 kbd->kb_delay1 = 200;
424 kbd->kb_delay1 = ((int *)arg)[0];
425 kbd->kb_delay2 = ((int *)arg)[1];
428 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
429 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
431 ival = IOCPARM_IVAL(arg);
432 arg = (caddr_t)&ival;
435 case KDSETRAD: /* set keyboard repeat rate (old
437 return (kmi_set_typematic(kbd, *(int *)arg));
439 case PIO_KEYMAP: /* set keyboard translation table */
440 case OPIO_KEYMAP: /* set keyboard translation table
442 case PIO_KEYMAPENT: /* set keyboard translation table
444 case PIO_DEADKEYMAP: /* set accent key translation table */
448 return (genkbd_commonioctl(kbd, cmd, arg));
455 kmi_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
460 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
461 * context where printf(9) can be called, which among other things
462 * includes interrupt filters and threads with any kinds of locks
463 * already held. For this reason it would be dangerous to acquire
464 * the Giant here unconditionally. On the other hand we have to
465 * have it to handle the ioctl.
466 * So we make our best effort to auto-detect whether we can grab
467 * the Giant or not. Blame syscons(4) for this.
473 if (!mtx_owned(&Giant) && !SCHEDULER_STOPPED())
474 return (EDEADLK); /* best I could come up with */
478 result = kmi_ioctl_locked(kbd, cmd, arg);
485 /* clear the internal state of the keyboard */
487 kmi_clear_state(keyboard_t *kbd)
489 struct kmi_softc *sc = kbd->kb_data;
491 KMI_CTX_LOCK_ASSERT();
493 sc->sc_flags &= ~(KMI_FLAG_COMPOSE | KMI_FLAG_POLLING);
494 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
498 /* save the internal state, not used */
500 kmi_get_state(keyboard_t *kbd, void *buf, size_t len)
502 return (len == 0) ? 1 : -1;
505 /* set the internal state, not used */
507 kmi_set_state(keyboard_t *kbd, void *buf, size_t len)
513 kmi_poll(keyboard_t *kbd, int on)
515 struct kmi_softc *sc = kbd->kb_data;
519 sc->sc_flags |= KMI_FLAG_POLLING;
520 sc->sc_poll_thread = curthread;
522 sc->sc_flags &= ~KMI_FLAG_POLLING;
529 /* local functions */
532 kmi_set_leds(struct kmi_softc *sc, uint8_t leds)
537 /* start transfer, if not already started */
538 printf("Implement me: %s\n", __func__);
542 kmi_set_typematic(keyboard_t *kbd, int code)
544 static const int delays[] = {250, 500, 750, 1000};
545 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
546 68, 76, 84, 92, 100, 110, 118, 126,
547 136, 152, 168, 184, 200, 220, 236, 252,
548 272, 304, 336, 368, 400, 440, 472, 504};
553 kbd->kb_delay1 = delays[(code >> 5) & 3];
554 kbd->kb_delay2 = rates[code & 0x1f];
558 static keyboard_switch_t kmisw = {
563 .test_if = &kmi_test_if,
564 .enable = &kmi_enable,
565 .disable = &kmi_disable,
568 .read_char = &kmi_read_char,
569 .check_char = &kmi_check_char,
572 .clear_state = &kmi_clear_state,
573 .get_state = &kmi_get_state,
574 .set_state = &kmi_set_state,
575 .get_fkeystr = &genkbd_get_fkeystr,
577 .diag = &genkbd_diag,
580 KEYBOARD_DRIVER(kmi, kmisw, kmi_configure);
583 pl050_kmi_intr(void *arg)
585 struct kmi_softc *sc = arg;
588 KMI_CTX_LOCK_ASSERT();
590 if ((sc->sc_flags & KMI_FLAG_POLLING) != 0)
593 if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
594 KBD_IS_BUSY(&sc->sc_kbd)) {
595 /* let the callback function process the input */
596 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
597 sc->sc_kbd.kb_callback.kc_arg);
599 /* read and discard the input, no one is waiting for it */
601 c = kmi_read_char_locked(&sc->sc_kbd, 0);
602 } while (c != NOKEY);
608 pl050_kmi_probe(device_t dev)
611 if (!ofw_bus_status_okay(dev))
614 if (ofw_bus_is_compatible(dev, "arm,pl050")) {
615 device_set_desc(dev, "PL050 Keyboard/Mouse Interface");
616 return (BUS_PROBE_DEFAULT);
623 pl050_kmi_attach(device_t dev)
625 struct kmi_softc *sc = device_get_softc(dev);
633 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
634 if (sc->sc_mem_res == NULL) {
635 device_printf(dev, "could not allocate memory resource\n");
639 /* Request the IRQ resources */
640 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
641 if (sc->sc_irq_res == NULL) {
642 device_printf(dev, "Error: could not allocate irq resources\n");
646 /* Setup and enable the timer */
647 if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_CLK,
648 NULL, pl050_kmi_intr, sc,
649 &sc->sc_intr_hl) != 0) {
650 bus_release_resource(dev, SYS_RES_IRQ, rid,
652 device_printf(dev, "Unable to setup the clock irq handler.\n");
656 /* TODO: clock & divisor */
658 pl050_kmi_write_4(sc, KMICR, KMICR_EN | KMICR_RXINTREN);
660 kbd_init_struct(kbd, KMI_DRIVER_NAME, KB_OTHER,
661 device_get_unit(dev), 0, 0, 0);
662 kbd->kb_data = (void *)sc;
664 sc->sc_keymap = key_map;
665 sc->sc_accmap = accent_map;
666 for (i = 0; i < KMI_NFKEY; i++) {
667 sc->sc_fkeymap[i] = fkey_tab[i];
670 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
671 sc->sc_fkeymap, KMI_NFKEY);
673 KBD_FOUND_DEVICE(kbd);
674 kmi_clear_state(kbd);
679 if (kbd_register(kbd) < 0) {
682 KBD_CONFIG_DONE(kbd);
684 #ifdef KBD_INSTALL_CDEV
685 if (kbd_attach(kbd)) {
691 genkbd_diag(kbd, bootverbose);
700 static device_method_t pl050_kmi_methods[] = {
701 DEVMETHOD(device_probe, pl050_kmi_probe),
702 DEVMETHOD(device_attach, pl050_kmi_attach),
706 static driver_t pl050_kmi_driver = {
709 sizeof(struct kmi_softc),
712 static devclass_t pl050_kmi_devclass;
714 DRIVER_MODULE(pl050_kmi, simplebus, pl050_kmi_driver, pl050_kmi_devclass, 0, 0);