2 * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Samsung Chromebook Keyboard
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>
44 #include <sys/timeet.h>
45 #include <sys/timetc.h>
46 #include <sys/mutex.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 <machine/bus.h>
59 #include <machine/cpu.h>
60 #include <machine/intr.h>
64 #include <arm/samsung/exynos/chrome_ec.h>
65 #include <arm/samsung/exynos/chrome_kb.h>
67 #include <arm/samsung/exynos/exynos5_combiner.h>
68 #include <arm/samsung/exynos/exynos5_pad.h>
70 #define CKB_LOCK() mtx_lock(&Giant)
71 #define CKB_UNLOCK() mtx_unlock(&Giant)
75 * Assert that the lock is held in all contexts
76 * where the code can be executed.
78 #define CKB_LOCK_ASSERT() mtx_assert(&Giant, MA_OWNED)
80 * Assert that the lock is held in the contexts
81 * where it really has to be so.
83 #define CKB_CTX_LOCK_ASSERT() \
85 if (!kdb_active && panicstr == NULL) \
86 mtx_assert(&Giant, MA_OWNED); \
89 #define CKB_LOCK_ASSERT() (void)0
90 #define CKB_CTX_LOCK_ASSERT() (void)0
94 * Define a stub keyboard driver in case one hasn't been
95 * compiled into the kernel
98 #include <dev/kbd/kbdreg.h>
99 #include <dev/kbd/kbdtables.h>
102 #define CKB_FLAG_COMPOSE 0x1
103 #define CKB_FLAG_POLLING 0x2
104 #define KBD_DRIVER_NAME "ckbd"
109 accentmap_t sc_accmap;
110 fkeytab_t sc_fkeymap[CKB_NFKEY];
112 struct resource* sc_mem_res;
113 struct resource* sc_irq_res;
116 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
117 int sc_state; /* shift/lock key state */
118 int sc_accents; /* accent key index (> 0) */
119 int sc_flags; /* flags */
121 struct callout sc_repeat_callout;
131 struct thread *sc_poll_thread;
139 static int ckb_set_typematic(keyboard_t *, int);
140 static uint32_t ckb_read_char(keyboard_t *, int);
141 static void ckb_clear_state(keyboard_t *);
142 static int ckb_ioctl(keyboard_t *, u_long, caddr_t);
143 static int ckb_enable(keyboard_t *);
144 static int ckb_disable(keyboard_t *);
147 ckb_repeat(void *arg)
149 struct ckb_softc *sc;
153 if (KBD_IS_ACTIVE(&sc->sc_kbd) && KBD_IS_BUSY(&sc->sc_kbd)) {
154 if (sc->sc_repeat_key != -1) {
155 sc->sc_repeating = 1;
156 sc->sc_kbd.kb_callback.kc_func(&sc->sc_kbd,
157 KBDIO_KEYINPUT, sc->sc_kbd.kb_callback.kc_arg);
162 /* detect a keyboard, not used */
164 ckb__probe(int unit, void *arg, int flags)
170 /* reset and initialize the device, not used */
172 ckb_init(int unit, keyboard_t **kbdp, void *arg, int flags)
178 /* test the interface to the device, not used */
180 ckb_test_if(keyboard_t *kbd)
186 /* finish using this keyboard, not used */
188 ckb_term(keyboard_t *kbd)
194 /* keyboard interrupt routine, not used */
196 ckb_intr(keyboard_t *kbd, void *arg)
202 /* lock the access to the keyboard, not used */
204 ckb_lock(keyboard_t *kbd, int lock)
210 /* clear the internal state of the keyboard */
212 ckb_clear_state(keyboard_t *kbd)
214 struct ckb_softc *sc;
218 CKB_CTX_LOCK_ASSERT();
220 sc->sc_flags &= ~(CKB_FLAG_COMPOSE | CKB_FLAG_POLLING);
221 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
225 /* save the internal state, not used */
227 ckb_get_state(keyboard_t *kbd, void *buf, size_t len)
230 return (len == 0) ? 1 : -1;
233 /* set the internal state, not used */
235 ckb_set_state(keyboard_t *kbd, void *buf, size_t len)
242 /* check if data is waiting */
244 ckb_check(keyboard_t *kbd)
246 struct ckb_softc *sc;
251 CKB_CTX_LOCK_ASSERT();
253 if (!KBD_IS_ACTIVE(kbd))
256 if (sc->sc_flags & CKB_FLAG_POLLING) {
260 for (i = 0; i < sc->cols; i++)
261 if (sc->scan_local[i] != sc->scan[i]) {
265 if (sc->sc_repeating)
271 /* check if char is waiting */
273 ckb_check_char_locked(keyboard_t *kbd)
275 CKB_CTX_LOCK_ASSERT();
277 if (!KBD_IS_ACTIVE(kbd))
280 return (ckb_check(kbd));
284 ckb_check_char(keyboard_t *kbd)
289 result = ckb_check_char_locked(kbd);
295 /* read one byte from the keyboard if it's allowed */
296 /* Currently unused. */
298 ckb_read(keyboard_t *kbd, int wait)
300 CKB_CTX_LOCK_ASSERT();
302 if (!KBD_IS_ACTIVE(kbd))
305 printf("Implement ME: %s\n", __func__);
310 keymap_read(struct ckb_softc *sc, int col, int row)
313 KASSERT(sc->keymap != NULL, ("keymap_read: no keymap"));
314 if (col >= 0 && col < sc->cols &&
315 row >= 0 && row < sc->rows) {
316 return sc->keymap[row * sc->cols + col];
323 keymap_write(struct ckb_softc *sc, int col, int row, uint16_t key)
326 KASSERT(sc->keymap != NULL, ("keymap_write: no keymap"));
327 if (col >= 0 && col < sc->cols &&
328 row >= 0 && row < sc->rows) {
329 sc->keymap[row * sc->cols + col] = key;
336 /* read char from the keyboard */
338 ckb_read_char_locked(keyboard_t *kbd, int wait)
340 struct ckb_softc *sc;
349 CKB_CTX_LOCK_ASSERT();
351 if (!KBD_IS_ACTIVE(kbd))
354 if (sc->sc_repeating) {
355 sc->sc_repeating = 0;
356 callout_reset(&sc->sc_repeat_callout, hz / 10,
358 return (sc->sc_repeat_key);
361 if (sc->sc_flags & CKB_FLAG_POLLING) {
363 GPIO_PIN_GET(sc->gpio_dev, sc->gpio, &status);
365 if (ec_command(EC_CMD_MKBP_STATE, sc->scan,
367 sc->scan, sc->cols)) {
379 for (i = 0; i < sc->cols; i++) {
380 for (j = 0; j < sc->rows; j++) {
381 oldbit = (sc->scan_local[i] & (1 << j));
382 newbit = (sc->scan[i] & (1 << j));
384 if (oldbit == newbit)
387 key = keymap_read(sc, i, j);
394 sc->scan_local[i] |= (1 << j);
396 /* setup repeating */
397 sc->sc_repeat_key = key;
398 callout_reset(&sc->sc_repeat_callout,
399 hz / 2, ckb_repeat, sc);
403 sc->scan_local[i] &= ~(1 << j);
408 /* unsetup repeating */
409 sc->sc_repeat_key = -1;
410 callout_stop(&sc->sc_repeat_callout);
420 /* Currently wait is always false. */
422 ckb_read_char(keyboard_t *kbd, int wait)
427 keycode = ckb_read_char_locked(kbd, wait);
434 /* some useful control functions */
436 ckb_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
438 struct ckb_softc *sc;
446 case KDGKBMODE: /* get keyboard mode */
447 *(int *)arg = sc->sc_mode;
450 case KDSKBMODE: /* set keyboard mode */
451 switch (*(int *)arg) {
453 if (sc->sc_mode != K_XLATE) {
454 /* make lock key state and LED state match */
455 sc->sc_state &= ~LOCK_MASK;
456 sc->sc_state |= KBD_LED_VAL(kbd);
461 if (sc->sc_mode != *(int *)arg) {
462 if ((sc->sc_flags & CKB_FLAG_POLLING) == 0)
463 ckb_clear_state(kbd);
464 sc->sc_mode = *(int *)arg;
472 case KDGETLED: /* get keyboard LED */
473 *(int *)arg = KBD_LED_VAL(kbd);
476 case KDSETLED: /* set keyboard LED */
477 /* NOTE: lock key state in "sc_state" won't be changed */
478 if (*(int *)arg & ~LOCK_MASK)
483 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
484 if (sc->sc_mode == K_XLATE &&
485 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
491 if (KBD_HAS_DEVICE(kbd)) {
495 KBD_LED_VAL(kbd) = *(int *)arg;
497 case KDGKBSTATE: /* get lock key state */
498 *(int *)arg = sc->sc_state & LOCK_MASK;
501 case KDSKBSTATE: /* set lock key state */
502 if (*(int *)arg & ~LOCK_MASK) {
505 sc->sc_state &= ~LOCK_MASK;
506 sc->sc_state |= *(int *)arg;
508 /* set LEDs and quit */
509 return (ckb_ioctl(kbd, KDSETLED, arg));
511 case KDSETREPEAT: /* set keyboard repeat rate (new
514 if (!KBD_HAS_DEVICE(kbd)) {
517 if (((int *)arg)[1] < 0) {
520 if (((int *)arg)[0] < 0) {
523 if (((int *)arg)[0] < 200) /* fastest possible value */
524 kbd->kb_delay1 = 200;
526 kbd->kb_delay1 = ((int *)arg)[0];
527 kbd->kb_delay2 = ((int *)arg)[1];
530 case KDSETRAD: /* set keyboard repeat rate (old
532 return (ckb_set_typematic(kbd, *(int *)arg));
534 case PIO_KEYMAP: /* set keyboard translation table */
535 case OPIO_KEYMAP: /* set keyboard translation table
537 case PIO_KEYMAPENT: /* set keyboard translation table
539 case PIO_DEADKEYMAP: /* set accent key translation table */
543 return (genkbd_commonioctl(kbd, cmd, arg));
550 ckb_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
555 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
556 * context where printf(9) can be called, which among other things
557 * includes interrupt filters and threads with any kinds of locks
558 * already held. For this reason it would be dangerous to acquire
559 * the Giant here unconditionally. On the other hand we have to
560 * have it to handle the ioctl.
561 * So we make our best effort to auto-detect whether we can grab
562 * the Giant or not. Blame syscons(4) for this.
568 if (!mtx_owned(&Giant) && !SCHEDULER_STOPPED())
569 return (EDEADLK); /* best I could come up with */
573 result = ckb_ioctl_locked(kbd, cmd, arg);
581 * Enable the access to the device; until this function is called,
582 * the client cannot read from the keyboard.
585 ckb_enable(keyboard_t *kbd)
595 /* disallow the access to the device */
597 ckb_disable(keyboard_t *kbd)
607 /* local functions */
610 ckb_set_typematic(keyboard_t *kbd, int code)
612 static const int delays[] = {250, 500, 750, 1000};
613 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
614 68, 76, 84, 92, 100, 110, 118, 126,
615 136, 152, 168, 184, 200, 220, 236, 252,
616 272, 304, 336, 368, 400, 440, 472, 504};
621 kbd->kb_delay1 = delays[(code >> 5) & 3];
622 kbd->kb_delay2 = rates[code & 0x1f];
627 ckb_poll(keyboard_t *kbd, int on)
629 struct ckb_softc *sc;
635 sc->sc_flags |= CKB_FLAG_POLLING;
636 sc->sc_poll_thread = curthread;
638 sc->sc_flags &= ~CKB_FLAG_POLLING;
645 /* local functions */
647 static int dummy_kbd_configure(int flags);
649 keyboard_switch_t ckbdsw = {
650 .probe = &ckb__probe,
654 .test_if = &ckb_test_if,
655 .enable = &ckb_enable,
656 .disable = &ckb_disable,
659 .read_char = &ckb_read_char,
660 .check_char = &ckb_check_char,
663 .clear_state = &ckb_clear_state,
664 .get_state = &ckb_get_state,
665 .set_state = &ckb_set_state,
666 .get_fkeystr = &genkbd_get_fkeystr,
668 .diag = &genkbd_diag,
672 dummy_kbd_configure(int flags)
678 KEYBOARD_DRIVER(ckbd, ckbdsw, dummy_kbd_configure);
681 * Parses 'keymap' into sc->keymap.
682 * Requires sc->cols and sc->rows to be set.
685 parse_keymap(struct ckb_softc *sc, pcell_t *keymap, size_t len)
689 sc->keymap = malloc(sc->cols * sc->rows * sizeof(sc->keymap[0]),
690 M_DEVBUF, M_NOWAIT | M_ZERO);
691 if (sc->keymap == NULL) {
695 for (i = 0; i < len; i++) {
697 * Return value is ignored, we just write whatever fits into
698 * specified number of rows and columns and silently ignore
700 * Keymap entries follow this format: 0xRRCCKKKK
701 * RR - row number, CC - column number, KKKK - key code
703 keymap_write(sc, (keymap[i] >> 16) & 0xff,
704 (keymap[i] >> 24) & 0xff,
711 /* Allocates a new array for keymap and returns it in 'keymap'. */
713 read_keymap(phandle_t node, const char *prop, pcell_t **keymap, size_t *len)
716 if ((*len = OF_getproplen(node, prop)) <= 0) {
719 if ((*keymap = malloc(*len, M_DEVBUF, M_NOWAIT)) == NULL) {
722 if (OF_getencprop(node, prop, *keymap, *len) != *len) {
729 parse_dts(struct ckb_softc *sc)
735 const char *keymap_prop = NULL;
737 if ((node = ofw_bus_get_node(sc->dev)) == -1)
740 if ((len = OF_getproplen(node, "google,key-rows")) <= 0)
742 OF_getencprop(node, "google,key-rows", &dts_value, len);
743 sc->rows = dts_value;
745 if ((len = OF_getproplen(node, "google,key-columns")) <= 0)
747 OF_getencprop(node, "google,key-columns", &dts_value, len);
748 sc->cols = dts_value;
750 if ((len = OF_getproplen(node, "freebsd,intr-gpio")) <= 0)
752 OF_getencprop(node, "freebsd,intr-gpio", &dts_value, len);
753 sc->gpio = dts_value;
755 if (OF_hasprop(node, "freebsd,keymap")) {
756 keymap_prop = "freebsd,keymap";
757 device_printf(sc->dev, "using FreeBSD-specific keymap from FDT\n");
758 } else if (OF_hasprop(node, "linux,keymap")) {
759 keymap_prop = "linux,keymap";
760 device_printf(sc->dev, "using Linux keymap from FDT\n");
762 device_printf(sc->dev, "using built-in keymap\n");
765 if (keymap_prop != NULL) {
766 if ((ret = read_keymap(node, keymap_prop, &keymap, &len))) {
767 device_printf(sc->dev,
768 "failed to read keymap from FDT: %d\n", ret);
771 ret = parse_keymap(sc, keymap, len);
772 free(keymap, M_DEVBUF);
777 if ((ret = parse_keymap(sc, default_keymap, KEYMAP_LEN))) {
782 if ((sc->rows == 0) || (sc->cols == 0) || (sc->gpio == 0))
789 ckb_ec_intr(void *arg)
791 struct ckb_softc *sc;
795 if (sc->sc_flags & CKB_FLAG_POLLING)
798 ec_command(EC_CMD_MKBP_STATE, sc->scan, sc->cols,
801 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
802 sc->sc_kbd.kb_callback.kc_arg);
806 chrome_kb_attach(device_t dev)
808 struct ckb_softc *sc;
814 sc = device_get_softc(dev);
819 if ((error = parse_dts(sc)) != 0)
822 sc->gpio_dev = devclass_get_device(devclass_find("gpio"), 0);
823 if (sc->gpio_dev == NULL) {
824 device_printf(sc->dev, "Can't find gpio device.\n");
829 device_printf(sc->dev, "Keyboard matrix [%dx%d]\n",
833 pad_setup_intr(sc->gpio, ckb_ec_intr, sc);
838 sc->scan_local = malloc(sc->cols, M_DEVBUF, M_NOWAIT);
839 sc->scan = malloc(sc->cols, M_DEVBUF, M_NOWAIT);
841 for (i = 0; i < sc->cols; i++) {
842 sc->scan_local[i] = 0;
846 kbd_init_struct(kbd, KBD_DRIVER_NAME, KB_OTHER,
847 device_get_unit(dev), 0, 0, 0);
848 kbd->kb_data = (void *)sc;
850 sc->sc_keymap = key_map;
851 sc->sc_accmap = accent_map;
852 for (i = 0; i < CKB_NFKEY; i++) {
853 sc->sc_fkeymap[i] = fkey_tab[i];
856 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
857 sc->sc_fkeymap, CKB_NFKEY);
859 KBD_FOUND_DEVICE(kbd);
860 ckb_clear_state(kbd);
863 callout_init(&sc->sc_repeat_callout, 0);
867 if (kbd_register(kbd) < 0) {
870 KBD_CONFIG_DONE(kbd);
876 chrome_kb_probe(device_t dev)
879 if (!ofw_bus_status_okay(dev))
882 if (ofw_bus_is_compatible(dev, "google,cros-ec-keyb") ||
883 ofw_bus_is_compatible(dev, "google,mkbp-keyb")) {
884 device_set_desc(dev, "Chrome EC Keyboard");
885 return (BUS_PROBE_DEFAULT);
892 chrome_kb_detach(device_t dev)
894 struct ckb_softc *sc;
896 sc = device_get_softc(dev);
898 if (sc->keymap != NULL) {
899 free(sc->keymap, M_DEVBUF);
905 static device_method_t chrome_kb_methods[] = {
906 DEVMETHOD(device_probe, chrome_kb_probe),
907 DEVMETHOD(device_attach, chrome_kb_attach),
908 DEVMETHOD(device_detach, chrome_kb_detach),
912 static driver_t chrome_kb_driver = {
915 sizeof(struct ckb_softc),
918 static devclass_t chrome_kb_devclass;
920 DRIVER_MODULE(chrome_kb, simplebus, chrome_kb_driver,
921 chrome_kb_devclass, 0, 0);