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/fdt/fdt_common.h>
50 #include <dev/ofw/openfirm.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
54 #include <sys/ioccom.h>
55 #include <sys/filio.h>
59 #include <machine/bus.h>
60 #include <machine/fdt.h>
61 #include <machine/cpu.h>
62 #include <machine/intr.h>
66 #include <arm/samsung/exynos/chrome_ec.h>
67 #include <arm/samsung/exynos/chrome_kb.h>
69 #include <arm/samsung/exynos/exynos5_combiner.h>
70 #include <arm/samsung/exynos/exynos5_pad.h>
72 #define CKB_LOCK() mtx_lock(&Giant)
73 #define CKB_UNLOCK() mtx_unlock(&Giant)
77 * Assert that the lock is held in all contexts
78 * where the code can be executed.
80 #define CKB_LOCK_ASSERT() mtx_assert(&Giant, MA_OWNED)
82 * Assert that the lock is held in the contexts
83 * where it really has to be so.
85 #define CKB_CTX_LOCK_ASSERT() \
87 if (!kdb_active && panicstr == NULL) \
88 mtx_assert(&Giant, MA_OWNED); \
91 #define CKB_LOCK_ASSERT() (void)0
92 #define CKB_CTX_LOCK_ASSERT() (void)0
96 * Define a stub keyboard driver in case one hasn't been
97 * compiled into the kernel
100 #include <dev/kbd/kbdreg.h>
101 #include <dev/kbd/kbdtables.h>
104 #define CKB_FLAG_COMPOSE 0x1
105 #define CKB_FLAG_POLLING 0x2
106 #define KBD_DRIVER_NAME "ckbd"
108 /* TODO: take interrupt from DTS */
109 #define KB_GPIO_INT 146
114 accentmap_t sc_accmap;
115 fkeytab_t sc_fkeymap[CKB_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 int sc_flags; /* flags */
126 struct callout sc_repeat_callout;
134 struct thread *sc_poll_thread;
141 static int ckb_set_typematic(keyboard_t *, int);
142 static uint32_t ckb_read_char(keyboard_t *, int);
143 static void ckb_clear_state(keyboard_t *);
144 static int ckb_ioctl(keyboard_t *, u_long, caddr_t);
145 static int ckb_enable(keyboard_t *);
146 static int ckb_disable(keyboard_t *);
149 ckb_repeat(void *arg)
151 struct ckb_softc *sc;
155 if (KBD_IS_ACTIVE(&sc->sc_kbd) && KBD_IS_BUSY(&sc->sc_kbd)) {
156 if (sc->sc_repeat_key != -1) {
157 sc->sc_repeating = 1;
158 sc->sc_kbd.kb_callback.kc_func(&sc->sc_kbd,
159 KBDIO_KEYINPUT, sc->sc_kbd.kb_callback.kc_arg);
164 /* detect a keyboard, not used */
166 ckb__probe(int unit, void *arg, int flags)
172 /* reset and initialize the device, not used */
174 ckb_init(int unit, keyboard_t **kbdp, void *arg, int flags)
180 /* test the interface to the device, not used */
182 ckb_test_if(keyboard_t *kbd)
188 /* finish using this keyboard, not used */
190 ckb_term(keyboard_t *kbd)
196 /* keyboard interrupt routine, not used */
198 ckb_intr(keyboard_t *kbd, void *arg)
204 /* lock the access to the keyboard, not used */
206 ckb_lock(keyboard_t *kbd, int lock)
212 /* clear the internal state of the keyboard */
214 ckb_clear_state(keyboard_t *kbd)
216 struct ckb_softc *sc;
220 CKB_CTX_LOCK_ASSERT();
222 sc->sc_flags &= ~(CKB_FLAG_COMPOSE | CKB_FLAG_POLLING);
223 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
227 /* save the internal state, not used */
229 ckb_get_state(keyboard_t *kbd, void *buf, size_t len)
232 return (len == 0) ? 1 : -1;
235 /* set the internal state, not used */
237 ckb_set_state(keyboard_t *kbd, void *buf, size_t len)
244 /* check if data is waiting */
246 ckb_check(keyboard_t *kbd)
248 struct ckb_softc *sc;
253 CKB_CTX_LOCK_ASSERT();
255 if (!KBD_IS_ACTIVE(kbd))
258 if (sc->sc_flags & CKB_FLAG_POLLING) {
262 for (i = 0; i < sc->cols; i++)
263 if (sc->scan_local[i] != sc->scan[i]) {
267 if (sc->sc_repeating)
273 /* check if char is waiting */
275 ckb_check_char_locked(keyboard_t *kbd)
277 CKB_CTX_LOCK_ASSERT();
279 if (!KBD_IS_ACTIVE(kbd))
282 return (ckb_check(kbd));
286 ckb_check_char(keyboard_t *kbd)
291 result = ckb_check_char_locked(kbd);
297 /* read one byte from the keyboard if it's allowed */
298 /* Currently unused. */
300 ckb_read(keyboard_t *kbd, int wait)
302 CKB_CTX_LOCK_ASSERT();
304 if (!KBD_IS_ACTIVE(kbd))
307 printf("Implement ME: %s\n", __func__);
311 int scantokey(int i, int j);
314 scantokey(int i, int j)
318 for (k = 0; k < KEYMAP_LEN; k++)
319 if ((keymap[k].col == i) && (keymap[k].row == j))
320 return (keymap[k].key);
325 /* read char from the keyboard */
327 ckb_read_char_locked(keyboard_t *kbd, int wait)
329 struct ckb_softc *sc;
337 CKB_CTX_LOCK_ASSERT();
339 if (!KBD_IS_ACTIVE(kbd))
342 if (sc->sc_repeating) {
343 sc->sc_repeating = 0;
344 callout_reset(&sc->sc_repeat_callout, hz / 10,
346 return (sc->sc_repeat_key);
349 if (sc->sc_flags & CKB_FLAG_POLLING) {
353 for (i = 0; i < sc->cols; i++) {
354 for (j = 0; j < sc->rows; j++) {
355 oldbit = (sc->scan_local[i] & (1 << j));
356 newbit = (sc->scan[i] & (1 << j));
358 if (oldbit == newbit)
361 key = scantokey(i,j);
368 sc->scan_local[i] |= (1 << j);
370 /* setup repeating */
371 sc->sc_repeat_key = key;
372 callout_reset(&sc->sc_repeat_callout,
373 hz / 2, ckb_repeat, sc);
377 sc->scan_local[i] &= ~(1 << j);
382 /* unsetup repeating */
383 sc->sc_repeat_key = -1;
384 callout_stop(&sc->sc_repeat_callout);
394 /* Currently wait is always false. */
396 ckb_read_char(keyboard_t *kbd, int wait)
401 keycode = ckb_read_char_locked(kbd, wait);
408 /* some useful control functions */
410 ckb_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
412 struct ckb_softc *sc;
420 case KDGKBMODE: /* get keyboard mode */
421 *(int *)arg = sc->sc_mode;
424 case KDSKBMODE: /* set keyboard mode */
425 switch (*(int *)arg) {
427 if (sc->sc_mode != K_XLATE) {
428 /* make lock key state and LED state match */
429 sc->sc_state &= ~LOCK_MASK;
430 sc->sc_state |= KBD_LED_VAL(kbd);
435 if (sc->sc_mode != *(int *)arg) {
436 if ((sc->sc_flags & CKB_FLAG_POLLING) == 0)
437 ckb_clear_state(kbd);
438 sc->sc_mode = *(int *)arg;
446 case KDGETLED: /* get keyboard LED */
447 *(int *)arg = KBD_LED_VAL(kbd);
450 case KDSETLED: /* set keyboard LED */
451 /* NOTE: lock key state in "sc_state" won't be changed */
452 if (*(int *)arg & ~LOCK_MASK)
457 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
458 if (sc->sc_mode == K_XLATE &&
459 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
465 if (KBD_HAS_DEVICE(kbd)) {
469 KBD_LED_VAL(kbd) = *(int *)arg;
471 case KDGKBSTATE: /* get lock key state */
472 *(int *)arg = sc->sc_state & LOCK_MASK;
475 case KDSKBSTATE: /* set lock key state */
476 if (*(int *)arg & ~LOCK_MASK) {
479 sc->sc_state &= ~LOCK_MASK;
480 sc->sc_state |= *(int *)arg;
482 /* set LEDs and quit */
483 return (ckb_ioctl(kbd, KDSETLED, arg));
485 case KDSETREPEAT: /* set keyboard repeat rate (new
488 if (!KBD_HAS_DEVICE(kbd)) {
491 if (((int *)arg)[1] < 0) {
494 if (((int *)arg)[0] < 0) {
497 if (((int *)arg)[0] < 200) /* fastest possible value */
498 kbd->kb_delay1 = 200;
500 kbd->kb_delay1 = ((int *)arg)[0];
501 kbd->kb_delay2 = ((int *)arg)[1];
504 case KDSETRAD: /* set keyboard repeat rate (old
506 return (ckb_set_typematic(kbd, *(int *)arg));
508 case PIO_KEYMAP: /* set keyboard translation table */
509 case OPIO_KEYMAP: /* set keyboard translation table
511 case PIO_KEYMAPENT: /* set keyboard translation table
513 case PIO_DEADKEYMAP: /* set accent key translation table */
517 return (genkbd_commonioctl(kbd, cmd, arg));
524 ckb_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
529 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
530 * context where printf(9) can be called, which among other things
531 * includes interrupt filters and threads with any kinds of locks
532 * already held. For this reason it would be dangerous to acquire
533 * the Giant here unconditionally. On the other hand we have to
534 * have it to handle the ioctl.
535 * So we make our best effort to auto-detect whether we can grab
536 * the Giant or not. Blame syscons(4) for this.
542 if (!mtx_owned(&Giant) && !SCHEDULER_STOPPED())
543 return (EDEADLK); /* best I could come up with */
547 result = ckb_ioctl_locked(kbd, cmd, arg);
555 * Enable the access to the device; until this function is called,
556 * the client cannot read from the keyboard.
559 ckb_enable(keyboard_t *kbd)
569 /* disallow the access to the device */
571 ckb_disable(keyboard_t *kbd)
581 /* local functions */
584 ckb_set_typematic(keyboard_t *kbd, int code)
586 static const int delays[] = {250, 500, 750, 1000};
587 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
588 68, 76, 84, 92, 100, 110, 118, 126,
589 136, 152, 168, 184, 200, 220, 236, 252,
590 272, 304, 336, 368, 400, 440, 472, 504};
595 kbd->kb_delay1 = delays[(code >> 5) & 3];
596 kbd->kb_delay2 = rates[code & 0x1f];
601 ckb_poll(keyboard_t *kbd, int on)
603 struct ckb_softc *sc;
609 sc->sc_flags |= CKB_FLAG_POLLING;
610 sc->sc_poll_thread = curthread;
612 sc->sc_flags &= ~CKB_FLAG_POLLING;
619 /* local functions */
621 static int dummy_kbd_configure(int flags);
623 keyboard_switch_t ckbdsw = {
624 .probe = &ckb__probe,
628 .test_if = &ckb_test_if,
629 .enable = &ckb_enable,
630 .disable = &ckb_disable,
633 .read_char = &ckb_read_char,
634 .check_char = &ckb_check_char,
637 .clear_state = &ckb_clear_state,
638 .get_state = &ckb_get_state,
639 .set_state = &ckb_set_state,
640 .get_fkeystr = &genkbd_get_fkeystr,
642 .diag = &genkbd_diag,
646 dummy_kbd_configure(int flags)
652 KEYBOARD_DRIVER(ckbd, ckbdsw, dummy_kbd_configure);
655 parse_dts(struct ckb_softc *sc)
661 if ((node = ofw_bus_get_node(sc->dev)) == -1)
664 if ((len = OF_getproplen(node, "keypad,num-rows")) <= 0)
666 OF_getprop(node, "keypad,num-rows", &dts_value, len);
667 sc->rows = fdt32_to_cpu(dts_value);
669 if ((len = OF_getproplen(node, "keypad,num-columns")) <= 0)
671 OF_getprop(node, "keypad,num-columns", &dts_value, len);
672 sc->cols = fdt32_to_cpu(dts_value);
674 if ((sc->rows == 0) || (sc->cols == 0))
681 ckb_ec_intr(void *arg)
683 struct ckb_softc *sc;
687 if (sc->sc_flags & CKB_FLAG_POLLING)
690 ec_command(EC_CMD_MKBP_STATE, sc->scan, sc->cols,
693 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
694 sc->sc_kbd.kb_callback.kc_arg);
698 chrome_kb_attach(device_t dev)
700 struct ckb_softc *sc;
706 sc = device_get_softc(dev);
710 if ((error = parse_dts(sc)) != 0)
714 device_printf(sc->dev, "Keyboard matrix [%dx%d]\n",
718 /* TODO: take interrupt from DTS */
719 pad_setup_intr(KB_GPIO_INT, ckb_ec_intr, sc);
724 sc->scan_local = malloc(sc->cols, M_DEVBUF, M_NOWAIT);
725 sc->scan = malloc(sc->cols, M_DEVBUF, M_NOWAIT);
727 for (i = 0; i < sc->cols; i++) {
728 sc->scan_local[i] = 0;
732 kbd_init_struct(kbd, KBD_DRIVER_NAME, KB_OTHER,
733 device_get_unit(dev), 0, 0, 0);
734 kbd->kb_data = (void *)sc;
736 sc->sc_keymap = key_map;
737 sc->sc_accmap = accent_map;
738 for (i = 0; i < CKB_NFKEY; i++) {
739 sc->sc_fkeymap[i] = fkey_tab[i];
742 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
743 sc->sc_fkeymap, CKB_NFKEY);
745 KBD_FOUND_DEVICE(kbd);
746 ckb_clear_state(kbd);
749 callout_init(&sc->sc_repeat_callout, 0);
753 if (kbd_register(kbd) < 0) {
756 KBD_CONFIG_DONE(kbd);
762 chrome_kb_probe(device_t dev)
765 if (!ofw_bus_status_okay(dev))
768 if (ofw_bus_is_compatible(dev, "google,cros-ec-keyb")) {
769 device_set_desc(dev, "Chrome EC Keyboard");
770 return (BUS_PROBE_DEFAULT);
776 static device_method_t chrome_kb_methods[] = {
777 DEVMETHOD(device_probe, chrome_kb_probe),
778 DEVMETHOD(device_attach, chrome_kb_attach),
782 static driver_t chrome_kb_driver = {
785 sizeof(struct ckb_softc),
788 static devclass_t chrome_kb_devclass;
790 DRIVER_MODULE(chrome_kb, simplebus, chrome_kb_driver,
791 chrome_kb_devclass, 0, 0);