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)
240 /* check if data is waiting */
242 ckb_check(keyboard_t *kbd)
244 struct ckb_softc *sc;
249 CKB_CTX_LOCK_ASSERT();
251 if (!KBD_IS_ACTIVE(kbd))
254 if (sc->sc_flags & CKB_FLAG_POLLING) {
258 for (i = 0; i < sc->cols; i++)
259 if (sc->scan_local[i] != sc->scan[i]) {
263 if (sc->sc_repeating)
269 /* check if char is waiting */
271 ckb_check_char_locked(keyboard_t *kbd)
273 CKB_CTX_LOCK_ASSERT();
275 if (!KBD_IS_ACTIVE(kbd))
278 return (ckb_check(kbd));
282 ckb_check_char(keyboard_t *kbd)
287 result = ckb_check_char_locked(kbd);
293 /* read one byte from the keyboard if it's allowed */
294 /* Currently unused. */
296 ckb_read(keyboard_t *kbd, int wait)
298 CKB_CTX_LOCK_ASSERT();
300 if (!KBD_IS_ACTIVE(kbd))
303 printf("Implement ME: %s\n", __func__);
308 keymap_read(struct ckb_softc *sc, int col, int row)
311 KASSERT(sc->keymap != NULL, ("keymap_read: no keymap"));
312 if (col >= 0 && col < sc->cols &&
313 row >= 0 && row < sc->rows) {
314 return sc->keymap[row * sc->cols + col];
321 keymap_write(struct ckb_softc *sc, int col, int row, uint16_t key)
324 KASSERT(sc->keymap != NULL, ("keymap_write: no keymap"));
325 if (col >= 0 && col < sc->cols &&
326 row >= 0 && row < sc->rows) {
327 sc->keymap[row * sc->cols + col] = key;
334 /* read char from the keyboard */
336 ckb_read_char_locked(keyboard_t *kbd, int wait)
338 struct ckb_softc *sc;
347 CKB_CTX_LOCK_ASSERT();
349 if (!KBD_IS_ACTIVE(kbd))
352 if (sc->sc_repeating) {
353 sc->sc_repeating = 0;
354 callout_reset(&sc->sc_repeat_callout, hz / 10,
356 return (sc->sc_repeat_key);
359 if (sc->sc_flags & CKB_FLAG_POLLING) {
361 GPIO_PIN_GET(sc->gpio_dev, sc->gpio, &status);
363 if (ec_command(EC_CMD_MKBP_STATE, sc->scan,
365 sc->scan, sc->cols)) {
377 for (i = 0; i < sc->cols; i++) {
378 for (j = 0; j < sc->rows; j++) {
379 oldbit = (sc->scan_local[i] & (1 << j));
380 newbit = (sc->scan[i] & (1 << j));
382 if (oldbit == newbit)
385 key = keymap_read(sc, i, j);
392 sc->scan_local[i] |= (1 << j);
394 /* setup repeating */
395 sc->sc_repeat_key = key;
396 callout_reset(&sc->sc_repeat_callout,
397 hz / 2, ckb_repeat, sc);
401 sc->scan_local[i] &= ~(1 << j);
406 /* unsetup repeating */
407 sc->sc_repeat_key = -1;
408 callout_stop(&sc->sc_repeat_callout);
418 /* Currently wait is always false. */
420 ckb_read_char(keyboard_t *kbd, int wait)
425 keycode = ckb_read_char_locked(kbd, wait);
431 /* some useful control functions */
433 ckb_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
435 struct ckb_softc *sc;
443 case KDGKBMODE: /* get keyboard mode */
444 *(int *)arg = sc->sc_mode;
447 case KDSKBMODE: /* set keyboard mode */
448 switch (*(int *)arg) {
450 if (sc->sc_mode != K_XLATE) {
451 /* make lock key state and LED state match */
452 sc->sc_state &= ~LOCK_MASK;
453 sc->sc_state |= KBD_LED_VAL(kbd);
458 if (sc->sc_mode != *(int *)arg) {
459 if ((sc->sc_flags & CKB_FLAG_POLLING) == 0)
460 ckb_clear_state(kbd);
461 sc->sc_mode = *(int *)arg;
469 case KDGETLED: /* get keyboard LED */
470 *(int *)arg = KBD_LED_VAL(kbd);
473 case KDSETLED: /* set keyboard LED */
474 /* NOTE: lock key state in "sc_state" won't be changed */
475 if (*(int *)arg & ~LOCK_MASK)
480 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
481 if (sc->sc_mode == K_XLATE &&
482 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
488 if (KBD_HAS_DEVICE(kbd)) {
492 KBD_LED_VAL(kbd) = *(int *)arg;
494 case KDGKBSTATE: /* get lock key state */
495 *(int *)arg = sc->sc_state & LOCK_MASK;
498 case KDSKBSTATE: /* set lock key state */
499 if (*(int *)arg & ~LOCK_MASK) {
502 sc->sc_state &= ~LOCK_MASK;
503 sc->sc_state |= *(int *)arg;
505 /* set LEDs and quit */
506 return (ckb_ioctl(kbd, KDSETLED, arg));
508 case KDSETREPEAT: /* set keyboard repeat rate (new
511 if (!KBD_HAS_DEVICE(kbd)) {
514 if (((int *)arg)[1] < 0) {
517 if (((int *)arg)[0] < 0) {
520 if (((int *)arg)[0] < 200) /* fastest possible value */
521 kbd->kb_delay1 = 200;
523 kbd->kb_delay1 = ((int *)arg)[0];
524 kbd->kb_delay2 = ((int *)arg)[1];
527 case KDSETRAD: /* set keyboard repeat rate (old
529 return (ckb_set_typematic(kbd, *(int *)arg));
531 case PIO_KEYMAP: /* set keyboard translation table */
532 case OPIO_KEYMAP: /* set keyboard translation table
534 case PIO_KEYMAPENT: /* set keyboard translation table
536 case PIO_DEADKEYMAP: /* set accent key translation table */
540 return (genkbd_commonioctl(kbd, cmd, arg));
547 ckb_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
552 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
553 * context where printf(9) can be called, which among other things
554 * includes interrupt filters and threads with any kinds of locks
555 * already held. For this reason it would be dangerous to acquire
556 * the Giant here unconditionally. On the other hand we have to
557 * have it to handle the ioctl.
558 * So we make our best effort to auto-detect whether we can grab
559 * the Giant or not. Blame syscons(4) for this.
565 if (!mtx_owned(&Giant) && !SCHEDULER_STOPPED())
566 return (EDEADLK); /* best I could come up with */
570 result = ckb_ioctl_locked(kbd, cmd, arg);
577 * Enable the access to the device; until this function is called,
578 * the client cannot read from the keyboard.
581 ckb_enable(keyboard_t *kbd)
591 /* disallow the access to the device */
593 ckb_disable(keyboard_t *kbd)
603 /* local functions */
606 ckb_set_typematic(keyboard_t *kbd, int code)
608 static const int delays[] = {250, 500, 750, 1000};
609 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
610 68, 76, 84, 92, 100, 110, 118, 126,
611 136, 152, 168, 184, 200, 220, 236, 252,
612 272, 304, 336, 368, 400, 440, 472, 504};
617 kbd->kb_delay1 = delays[(code >> 5) & 3];
618 kbd->kb_delay2 = rates[code & 0x1f];
623 ckb_poll(keyboard_t *kbd, int on)
625 struct ckb_softc *sc;
631 sc->sc_flags |= CKB_FLAG_POLLING;
632 sc->sc_poll_thread = curthread;
634 sc->sc_flags &= ~CKB_FLAG_POLLING;
641 /* local functions */
643 static int dummy_kbd_configure(int flags);
645 keyboard_switch_t ckbdsw = {
646 .probe = &ckb__probe,
650 .test_if = &ckb_test_if,
651 .enable = &ckb_enable,
652 .disable = &ckb_disable,
655 .read_char = &ckb_read_char,
656 .check_char = &ckb_check_char,
659 .clear_state = &ckb_clear_state,
660 .get_state = &ckb_get_state,
661 .set_state = &ckb_set_state,
666 dummy_kbd_configure(int flags)
672 KEYBOARD_DRIVER(ckbd, ckbdsw, dummy_kbd_configure);
675 * Parses 'keymap' into sc->keymap.
676 * Requires sc->cols and sc->rows to be set.
679 parse_keymap(struct ckb_softc *sc, pcell_t *keymap, size_t len)
683 sc->keymap = malloc(sc->cols * sc->rows * sizeof(sc->keymap[0]),
684 M_DEVBUF, M_NOWAIT | M_ZERO);
685 if (sc->keymap == NULL) {
689 for (i = 0; i < len; i++) {
691 * Return value is ignored, we just write whatever fits into
692 * specified number of rows and columns and silently ignore
694 * Keymap entries follow this format: 0xRRCCKKKK
695 * RR - row number, CC - column number, KKKK - key code
697 keymap_write(sc, (keymap[i] >> 16) & 0xff,
698 (keymap[i] >> 24) & 0xff,
705 /* Allocates a new array for keymap and returns it in 'keymap'. */
707 read_keymap(phandle_t node, const char *prop, pcell_t **keymap, size_t *len)
710 if ((*len = OF_getproplen(node, prop)) <= 0) {
713 if ((*keymap = malloc(*len, M_DEVBUF, M_NOWAIT)) == NULL) {
716 if (OF_getencprop(node, prop, *keymap, *len) != *len) {
723 parse_dts(struct ckb_softc *sc)
729 const char *keymap_prop = NULL;
731 if ((node = ofw_bus_get_node(sc->dev)) == -1)
734 if ((len = OF_getproplen(node, "google,key-rows")) <= 0)
736 OF_getencprop(node, "google,key-rows", &dts_value, len);
737 sc->rows = dts_value;
739 if ((len = OF_getproplen(node, "google,key-columns")) <= 0)
741 OF_getencprop(node, "google,key-columns", &dts_value, len);
742 sc->cols = dts_value;
744 if ((len = OF_getproplen(node, "freebsd,intr-gpio")) <= 0)
746 OF_getencprop(node, "freebsd,intr-gpio", &dts_value, len);
747 sc->gpio = dts_value;
749 if (OF_hasprop(node, "freebsd,keymap")) {
750 keymap_prop = "freebsd,keymap";
751 device_printf(sc->dev, "using FreeBSD-specific keymap from FDT\n");
752 } else if (OF_hasprop(node, "linux,keymap")) {
753 keymap_prop = "linux,keymap";
754 device_printf(sc->dev, "using Linux keymap from FDT\n");
756 device_printf(sc->dev, "using built-in keymap\n");
759 if (keymap_prop != NULL) {
760 if ((ret = read_keymap(node, keymap_prop, &keymap, &len))) {
761 device_printf(sc->dev,
762 "failed to read keymap from FDT: %d\n", ret);
765 ret = parse_keymap(sc, keymap, len);
766 free(keymap, M_DEVBUF);
771 if ((ret = parse_keymap(sc, default_keymap, KEYMAP_LEN))) {
776 if ((sc->rows == 0) || (sc->cols == 0) || (sc->gpio == 0))
783 ckb_ec_intr(void *arg)
785 struct ckb_softc *sc;
789 if (sc->sc_flags & CKB_FLAG_POLLING)
792 ec_command(EC_CMD_MKBP_STATE, sc->scan, sc->cols,
795 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
796 sc->sc_kbd.kb_callback.kc_arg);
800 chrome_kb_attach(device_t dev)
802 struct ckb_softc *sc;
808 sc = device_get_softc(dev);
813 if ((error = parse_dts(sc)) != 0)
816 sc->gpio_dev = devclass_get_device(devclass_find("gpio"), 0);
817 if (sc->gpio_dev == NULL) {
818 device_printf(sc->dev, "Can't find gpio device.\n");
823 device_printf(sc->dev, "Keyboard matrix [%dx%d]\n",
827 pad_setup_intr(sc->gpio, ckb_ec_intr, sc);
832 sc->scan_local = malloc(sc->cols, M_DEVBUF, M_NOWAIT);
833 sc->scan = malloc(sc->cols, M_DEVBUF, M_NOWAIT);
835 for (i = 0; i < sc->cols; i++) {
836 sc->scan_local[i] = 0;
840 kbd_init_struct(kbd, KBD_DRIVER_NAME, KB_OTHER,
841 device_get_unit(dev), 0, 0, 0);
842 kbd->kb_data = (void *)sc;
844 sc->sc_keymap = key_map;
845 sc->sc_accmap = accent_map;
846 for (i = 0; i < CKB_NFKEY; i++) {
847 sc->sc_fkeymap[i] = fkey_tab[i];
850 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
851 sc->sc_fkeymap, CKB_NFKEY);
853 KBD_FOUND_DEVICE(kbd);
854 ckb_clear_state(kbd);
857 callout_init(&sc->sc_repeat_callout, 0);
861 if (kbd_register(kbd) < 0) {
864 KBD_CONFIG_DONE(kbd);
870 chrome_kb_probe(device_t dev)
873 if (!ofw_bus_status_okay(dev))
876 if (ofw_bus_is_compatible(dev, "google,cros-ec-keyb") ||
877 ofw_bus_is_compatible(dev, "google,mkbp-keyb")) {
878 device_set_desc(dev, "Chrome EC Keyboard");
879 return (BUS_PROBE_DEFAULT);
886 chrome_kb_detach(device_t dev)
888 struct ckb_softc *sc;
890 sc = device_get_softc(dev);
892 if (sc->keymap != NULL) {
893 free(sc->keymap, M_DEVBUF);
899 static device_method_t chrome_kb_methods[] = {
900 DEVMETHOD(device_probe, chrome_kb_probe),
901 DEVMETHOD(device_attach, chrome_kb_attach),
902 DEVMETHOD(device_detach, chrome_kb_detach),
906 static driver_t chrome_kb_driver = {
909 sizeof(struct ckb_softc),
912 static devclass_t chrome_kb_devclass;
914 DRIVER_MODULE(chrome_kb, simplebus, chrome_kb_driver,
915 chrome_kb_devclass, 0, 0);