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>
57 #include <machine/bus.h>
58 #include <machine/cpu.h>
59 #include <machine/intr.h>
63 #include <arm/samsung/exynos/chrome_ec.h>
64 #include <arm/samsung/exynos/chrome_kb.h>
66 #include <arm/samsung/exynos/exynos5_combiner.h>
67 #include <arm/samsung/exynos/exynos5_pad.h>
69 #define CKB_LOCK() mtx_lock(&Giant)
70 #define CKB_UNLOCK() mtx_unlock(&Giant)
74 * Assert that the lock is held in all contexts
75 * where the code can be executed.
77 #define CKB_LOCK_ASSERT() mtx_assert(&Giant, MA_OWNED)
79 * Assert that the lock is held in the contexts
80 * where it really has to be so.
82 #define CKB_CTX_LOCK_ASSERT() \
84 if (!kdb_active && !KERNEL_PANICKED()) \
85 mtx_assert(&Giant, MA_OWNED); \
88 #define CKB_LOCK_ASSERT() (void)0
89 #define CKB_CTX_LOCK_ASSERT() (void)0
93 * Define a stub keyboard driver in case one hasn't been
94 * compiled into the kernel
97 #include <dev/kbd/kbdreg.h>
98 #include <dev/kbd/kbdtables.h>
101 #define CKB_FLAG_COMPOSE 0x1
102 #define CKB_FLAG_POLLING 0x2
103 #define KBD_DRIVER_NAME "ckbd"
108 accentmap_t sc_accmap;
109 fkeytab_t sc_fkeymap[CKB_NFKEY];
111 struct resource* sc_mem_res;
112 struct resource* sc_irq_res;
115 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
116 int sc_state; /* shift/lock key state */
117 int sc_accents; /* accent key index (> 0) */
118 int sc_flags; /* flags */
120 struct callout sc_repeat_callout;
130 struct thread *sc_poll_thread;
138 static int ckb_set_typematic(keyboard_t *, int);
139 static uint32_t ckb_read_char(keyboard_t *, int);
140 static void ckb_clear_state(keyboard_t *);
141 static int ckb_ioctl(keyboard_t *, u_long, caddr_t);
142 static int ckb_enable(keyboard_t *);
143 static int ckb_disable(keyboard_t *);
146 ckb_repeat(void *arg)
148 struct ckb_softc *sc;
152 if (KBD_IS_ACTIVE(&sc->sc_kbd) && KBD_IS_BUSY(&sc->sc_kbd)) {
153 if (sc->sc_repeat_key != -1) {
154 sc->sc_repeating = 1;
155 sc->sc_kbd.kb_callback.kc_func(&sc->sc_kbd,
156 KBDIO_KEYINPUT, sc->sc_kbd.kb_callback.kc_arg);
161 /* detect a keyboard, not used */
163 ckb__probe(int unit, void *arg, int flags)
169 /* reset and initialize the device, not used */
171 ckb_init(int unit, keyboard_t **kbdp, void *arg, int flags)
177 /* test the interface to the device, not used */
179 ckb_test_if(keyboard_t *kbd)
185 /* finish using this keyboard, not used */
187 ckb_term(keyboard_t *kbd)
193 /* keyboard interrupt routine, not used */
195 ckb_intr(keyboard_t *kbd, void *arg)
201 /* lock the access to the keyboard, not used */
203 ckb_lock(keyboard_t *kbd, int lock)
209 /* clear the internal state of the keyboard */
211 ckb_clear_state(keyboard_t *kbd)
213 struct ckb_softc *sc;
217 CKB_CTX_LOCK_ASSERT();
219 sc->sc_flags &= ~(CKB_FLAG_COMPOSE | CKB_FLAG_POLLING);
220 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
224 /* save the internal state, not used */
226 ckb_get_state(keyboard_t *kbd, void *buf, size_t len)
229 return (len == 0) ? 1 : -1;
232 /* set the internal state, not used */
234 ckb_set_state(keyboard_t *kbd, void *buf, size_t len)
241 /* check if data is waiting */
243 ckb_check(keyboard_t *kbd)
245 struct ckb_softc *sc;
250 CKB_CTX_LOCK_ASSERT();
252 if (!KBD_IS_ACTIVE(kbd))
255 if (sc->sc_flags & CKB_FLAG_POLLING) {
259 for (i = 0; i < sc->cols; i++)
260 if (sc->scan_local[i] != sc->scan[i]) {
264 if (sc->sc_repeating)
270 /* check if char is waiting */
272 ckb_check_char_locked(keyboard_t *kbd)
274 CKB_CTX_LOCK_ASSERT();
276 if (!KBD_IS_ACTIVE(kbd))
279 return (ckb_check(kbd));
283 ckb_check_char(keyboard_t *kbd)
288 result = ckb_check_char_locked(kbd);
294 /* read one byte from the keyboard if it's allowed */
295 /* Currently unused. */
297 ckb_read(keyboard_t *kbd, int wait)
299 CKB_CTX_LOCK_ASSERT();
301 if (!KBD_IS_ACTIVE(kbd))
304 printf("Implement ME: %s\n", __func__);
309 keymap_read(struct ckb_softc *sc, int col, int row)
312 KASSERT(sc->keymap != NULL, ("keymap_read: no keymap"));
313 if (col >= 0 && col < sc->cols &&
314 row >= 0 && row < sc->rows) {
315 return sc->keymap[row * sc->cols + col];
322 keymap_write(struct ckb_softc *sc, int col, int row, uint16_t key)
325 KASSERT(sc->keymap != NULL, ("keymap_write: no keymap"));
326 if (col >= 0 && col < sc->cols &&
327 row >= 0 && row < sc->rows) {
328 sc->keymap[row * sc->cols + col] = key;
335 /* read char from the keyboard */
337 ckb_read_char_locked(keyboard_t *kbd, int wait)
339 struct ckb_softc *sc;
348 CKB_CTX_LOCK_ASSERT();
350 if (!KBD_IS_ACTIVE(kbd))
353 if (sc->sc_repeating) {
354 sc->sc_repeating = 0;
355 callout_reset(&sc->sc_repeat_callout, hz / 10,
357 return (sc->sc_repeat_key);
360 if (sc->sc_flags & CKB_FLAG_POLLING) {
362 GPIO_PIN_GET(sc->gpio_dev, sc->gpio, &status);
364 if (ec_command(EC_CMD_MKBP_STATE, sc->scan,
366 sc->scan, sc->cols)) {
378 for (i = 0; i < sc->cols; i++) {
379 for (j = 0; j < sc->rows; j++) {
380 oldbit = (sc->scan_local[i] & (1 << j));
381 newbit = (sc->scan[i] & (1 << j));
383 if (oldbit == newbit)
386 key = keymap_read(sc, i, j);
393 sc->scan_local[i] |= (1 << j);
395 /* setup repeating */
396 sc->sc_repeat_key = key;
397 callout_reset(&sc->sc_repeat_callout,
398 hz / 2, ckb_repeat, sc);
402 sc->scan_local[i] &= ~(1 << j);
407 /* unsetup repeating */
408 sc->sc_repeat_key = -1;
409 callout_stop(&sc->sc_repeat_callout);
419 /* Currently wait is always false. */
421 ckb_read_char(keyboard_t *kbd, int wait)
426 keycode = ckb_read_char_locked(kbd, wait);
433 /* some useful control functions */
435 ckb_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
437 struct ckb_softc *sc;
445 case KDGKBMODE: /* get keyboard mode */
446 *(int *)arg = sc->sc_mode;
449 case KDSKBMODE: /* set keyboard mode */
450 switch (*(int *)arg) {
452 if (sc->sc_mode != K_XLATE) {
453 /* make lock key state and LED state match */
454 sc->sc_state &= ~LOCK_MASK;
455 sc->sc_state |= KBD_LED_VAL(kbd);
460 if (sc->sc_mode != *(int *)arg) {
461 if ((sc->sc_flags & CKB_FLAG_POLLING) == 0)
462 ckb_clear_state(kbd);
463 sc->sc_mode = *(int *)arg;
471 case KDGETLED: /* get keyboard LED */
472 *(int *)arg = KBD_LED_VAL(kbd);
475 case KDSETLED: /* set keyboard LED */
476 /* NOTE: lock key state in "sc_state" won't be changed */
477 if (*(int *)arg & ~LOCK_MASK)
482 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
483 if (sc->sc_mode == K_XLATE &&
484 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
490 if (KBD_HAS_DEVICE(kbd)) {
494 KBD_LED_VAL(kbd) = *(int *)arg;
496 case KDGKBSTATE: /* get lock key state */
497 *(int *)arg = sc->sc_state & LOCK_MASK;
500 case KDSKBSTATE: /* set lock key state */
501 if (*(int *)arg & ~LOCK_MASK) {
504 sc->sc_state &= ~LOCK_MASK;
505 sc->sc_state |= *(int *)arg;
507 /* set LEDs and quit */
508 return (ckb_ioctl(kbd, KDSETLED, arg));
510 case KDSETREPEAT: /* set keyboard repeat rate (new
513 if (!KBD_HAS_DEVICE(kbd)) {
516 if (((int *)arg)[1] < 0) {
519 if (((int *)arg)[0] < 0) {
522 if (((int *)arg)[0] < 200) /* fastest possible value */
523 kbd->kb_delay1 = 200;
525 kbd->kb_delay1 = ((int *)arg)[0];
526 kbd->kb_delay2 = ((int *)arg)[1];
529 case KDSETRAD: /* set keyboard repeat rate (old
531 return (ckb_set_typematic(kbd, *(int *)arg));
533 case PIO_KEYMAP: /* set keyboard translation table */
534 case OPIO_KEYMAP: /* set keyboard translation table
536 case PIO_KEYMAPENT: /* set keyboard translation table
538 case PIO_DEADKEYMAP: /* set accent key translation table */
542 return (genkbd_commonioctl(kbd, cmd, arg));
549 ckb_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
554 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
555 * context where printf(9) can be called, which among other things
556 * includes interrupt filters and threads with any kinds of locks
557 * already held. For this reason it would be dangerous to acquire
558 * the Giant here unconditionally. On the other hand we have to
559 * have it to handle the ioctl.
560 * So we make our best effort to auto-detect whether we can grab
561 * the Giant or not. Blame syscons(4) for this.
567 if (!mtx_owned(&Giant) && !SCHEDULER_STOPPED())
568 return (EDEADLK); /* best I could come up with */
572 result = ckb_ioctl_locked(kbd, cmd, arg);
580 * Enable the access to the device; until this function is called,
581 * the client cannot read from the keyboard.
584 ckb_enable(keyboard_t *kbd)
594 /* disallow the access to the device */
596 ckb_disable(keyboard_t *kbd)
606 /* local functions */
609 ckb_set_typematic(keyboard_t *kbd, int code)
611 static const int delays[] = {250, 500, 750, 1000};
612 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
613 68, 76, 84, 92, 100, 110, 118, 126,
614 136, 152, 168, 184, 200, 220, 236, 252,
615 272, 304, 336, 368, 400, 440, 472, 504};
620 kbd->kb_delay1 = delays[(code >> 5) & 3];
621 kbd->kb_delay2 = rates[code & 0x1f];
626 ckb_poll(keyboard_t *kbd, int on)
628 struct ckb_softc *sc;
634 sc->sc_flags |= CKB_FLAG_POLLING;
635 sc->sc_poll_thread = curthread;
637 sc->sc_flags &= ~CKB_FLAG_POLLING;
644 /* local functions */
646 static int dummy_kbd_configure(int flags);
648 keyboard_switch_t ckbdsw = {
649 .probe = &ckb__probe,
653 .test_if = &ckb_test_if,
654 .enable = &ckb_enable,
655 .disable = &ckb_disable,
658 .read_char = &ckb_read_char,
659 .check_char = &ckb_check_char,
662 .clear_state = &ckb_clear_state,
663 .get_state = &ckb_get_state,
664 .set_state = &ckb_set_state,
669 dummy_kbd_configure(int flags)
675 KEYBOARD_DRIVER(ckbd, ckbdsw, dummy_kbd_configure);
678 * Parses 'keymap' into sc->keymap.
679 * Requires sc->cols and sc->rows to be set.
682 parse_keymap(struct ckb_softc *sc, pcell_t *keymap, size_t len)
686 sc->keymap = malloc(sc->cols * sc->rows * sizeof(sc->keymap[0]),
687 M_DEVBUF, M_NOWAIT | M_ZERO);
688 if (sc->keymap == NULL) {
692 for (i = 0; i < len; i++) {
694 * Return value is ignored, we just write whatever fits into
695 * specified number of rows and columns and silently ignore
697 * Keymap entries follow this format: 0xRRCCKKKK
698 * RR - row number, CC - column number, KKKK - key code
700 keymap_write(sc, (keymap[i] >> 16) & 0xff,
701 (keymap[i] >> 24) & 0xff,
708 /* Allocates a new array for keymap and returns it in 'keymap'. */
710 read_keymap(phandle_t node, const char *prop, pcell_t **keymap, size_t *len)
713 if ((*len = OF_getproplen(node, prop)) <= 0) {
716 if ((*keymap = malloc(*len, M_DEVBUF, M_NOWAIT)) == NULL) {
719 if (OF_getencprop(node, prop, *keymap, *len) != *len) {
726 parse_dts(struct ckb_softc *sc)
732 const char *keymap_prop = NULL;
734 if ((node = ofw_bus_get_node(sc->dev)) == -1)
737 if ((len = OF_getproplen(node, "google,key-rows")) <= 0)
739 OF_getencprop(node, "google,key-rows", &dts_value, len);
740 sc->rows = dts_value;
742 if ((len = OF_getproplen(node, "google,key-columns")) <= 0)
744 OF_getencprop(node, "google,key-columns", &dts_value, len);
745 sc->cols = dts_value;
747 if ((len = OF_getproplen(node, "freebsd,intr-gpio")) <= 0)
749 OF_getencprop(node, "freebsd,intr-gpio", &dts_value, len);
750 sc->gpio = dts_value;
752 if (OF_hasprop(node, "freebsd,keymap")) {
753 keymap_prop = "freebsd,keymap";
754 device_printf(sc->dev, "using FreeBSD-specific keymap from FDT\n");
755 } else if (OF_hasprop(node, "linux,keymap")) {
756 keymap_prop = "linux,keymap";
757 device_printf(sc->dev, "using Linux keymap from FDT\n");
759 device_printf(sc->dev, "using built-in keymap\n");
762 if (keymap_prop != NULL) {
763 if ((ret = read_keymap(node, keymap_prop, &keymap, &len))) {
764 device_printf(sc->dev,
765 "failed to read keymap from FDT: %d\n", ret);
768 ret = parse_keymap(sc, keymap, len);
769 free(keymap, M_DEVBUF);
774 if ((ret = parse_keymap(sc, default_keymap, KEYMAP_LEN))) {
779 if ((sc->rows == 0) || (sc->cols == 0) || (sc->gpio == 0))
786 ckb_ec_intr(void *arg)
788 struct ckb_softc *sc;
792 if (sc->sc_flags & CKB_FLAG_POLLING)
795 ec_command(EC_CMD_MKBP_STATE, sc->scan, sc->cols,
798 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
799 sc->sc_kbd.kb_callback.kc_arg);
803 chrome_kb_attach(device_t dev)
805 struct ckb_softc *sc;
811 sc = device_get_softc(dev);
816 if ((error = parse_dts(sc)) != 0)
819 sc->gpio_dev = devclass_get_device(devclass_find("gpio"), 0);
820 if (sc->gpio_dev == NULL) {
821 device_printf(sc->dev, "Can't find gpio device.\n");
826 device_printf(sc->dev, "Keyboard matrix [%dx%d]\n",
830 pad_setup_intr(sc->gpio, ckb_ec_intr, sc);
835 sc->scan_local = malloc(sc->cols, M_DEVBUF, M_NOWAIT);
836 sc->scan = malloc(sc->cols, M_DEVBUF, M_NOWAIT);
838 for (i = 0; i < sc->cols; i++) {
839 sc->scan_local[i] = 0;
843 kbd_init_struct(kbd, KBD_DRIVER_NAME, KB_OTHER,
844 device_get_unit(dev), 0, 0, 0);
845 kbd->kb_data = (void *)sc;
847 sc->sc_keymap = key_map;
848 sc->sc_accmap = accent_map;
849 for (i = 0; i < CKB_NFKEY; i++) {
850 sc->sc_fkeymap[i] = fkey_tab[i];
853 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
854 sc->sc_fkeymap, CKB_NFKEY);
856 KBD_FOUND_DEVICE(kbd);
857 ckb_clear_state(kbd);
860 callout_init(&sc->sc_repeat_callout, 0);
864 if (kbd_register(kbd) < 0) {
867 KBD_CONFIG_DONE(kbd);
873 chrome_kb_probe(device_t dev)
876 if (!ofw_bus_status_okay(dev))
879 if (ofw_bus_is_compatible(dev, "google,cros-ec-keyb") ||
880 ofw_bus_is_compatible(dev, "google,mkbp-keyb")) {
881 device_set_desc(dev, "Chrome EC Keyboard");
882 return (BUS_PROBE_DEFAULT);
889 chrome_kb_detach(device_t dev)
891 struct ckb_softc *sc;
893 sc = device_get_softc(dev);
895 if (sc->keymap != NULL) {
896 free(sc->keymap, M_DEVBUF);
902 static device_method_t chrome_kb_methods[] = {
903 DEVMETHOD(device_probe, chrome_kb_probe),
904 DEVMETHOD(device_attach, chrome_kb_attach),
905 DEVMETHOD(device_detach, chrome_kb_detach),
909 static driver_t chrome_kb_driver = {
912 sizeof(struct ckb_softc),
915 static devclass_t chrome_kb_devclass;
917 DRIVER_MODULE(chrome_kb, simplebus, chrome_kb_driver,
918 chrome_kb_devclass, 0, 0);