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 void ckb_set_leds(struct ckb_softc *, uint8_t);
142 static int ckb_set_typematic(keyboard_t *, int);
143 static uint32_t ckb_read_char(keyboard_t *, int);
144 static void ckb_clear_state(keyboard_t *);
145 static int ckb_ioctl(keyboard_t *, u_long, caddr_t);
146 static int ckb_enable(keyboard_t *);
147 static int ckb_disable(keyboard_t *);
150 ckb_repeat(void *arg)
152 struct ckb_softc *sc;
156 if (KBD_IS_ACTIVE(&sc->sc_kbd) && KBD_IS_BUSY(&sc->sc_kbd)) {
157 if (sc->sc_repeat_key != -1) {
158 sc->sc_repeating = 1;
159 sc->sc_kbd.kb_callback.kc_func(&sc->sc_kbd,
160 KBDIO_KEYINPUT, sc->sc_kbd.kb_callback.kc_arg);
165 /* detect a keyboard, not used */
167 ckb__probe(int unit, void *arg, int flags)
173 /* reset and initialize the device, not used */
175 ckb_init(int unit, keyboard_t **kbdp, void *arg, int flags)
181 /* test the interface to the device, not used */
183 ckb_test_if(keyboard_t *kbd)
189 /* finish using this keyboard, not used */
191 ckb_term(keyboard_t *kbd)
197 /* keyboard interrupt routine, not used */
199 ckb_intr(keyboard_t *kbd, void *arg)
205 /* lock the access to the keyboard, not used */
207 ckb_lock(keyboard_t *kbd, int lock)
213 /* clear the internal state of the keyboard */
215 ckb_clear_state(keyboard_t *kbd)
217 struct ckb_softc *sc;
221 CKB_CTX_LOCK_ASSERT();
223 sc->sc_flags &= ~(CKB_FLAG_COMPOSE | CKB_FLAG_POLLING);
224 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
228 /* save the internal state, not used */
230 ckb_get_state(keyboard_t *kbd, void *buf, size_t len)
233 return (len == 0) ? 1 : -1;
236 /* set the internal state, not used */
238 ckb_set_state(keyboard_t *kbd, void *buf, size_t len)
245 /* check if data is waiting */
247 ckb_check(keyboard_t *kbd)
249 struct ckb_softc *sc;
254 CKB_CTX_LOCK_ASSERT();
256 if (!KBD_IS_ACTIVE(kbd))
259 if (sc->sc_flags & CKB_FLAG_POLLING) {
263 for (i = 0; i < sc->cols; i++)
264 if (sc->scan_local[i] != sc->scan[i]) {
268 if (sc->sc_repeating)
274 /* check if char is waiting */
276 ckb_check_char_locked(keyboard_t *kbd)
278 CKB_CTX_LOCK_ASSERT();
280 if (!KBD_IS_ACTIVE(kbd))
283 return (ckb_check(kbd));
287 ckb_check_char(keyboard_t *kbd)
292 result = ckb_check_char_locked(kbd);
298 /* read one byte from the keyboard if it's allowed */
299 /* Currently unused. */
301 ckb_read(keyboard_t *kbd, int wait)
303 CKB_CTX_LOCK_ASSERT();
305 if (!KBD_IS_ACTIVE(kbd))
308 printf("Implement ME: %s\n", __func__);
312 int scantokey(int i, int j);
315 scantokey(int i, int j)
319 for (k = 0; k < KEYMAP_LEN; k++)
320 if ((keymap[k].col == i) && (keymap[k].row == j))
321 return (keymap[k].key);
326 /* read char from the keyboard */
328 ckb_read_char_locked(keyboard_t *kbd, int wait)
330 struct ckb_softc *sc;
338 CKB_CTX_LOCK_ASSERT();
340 if (!KBD_IS_ACTIVE(kbd))
343 if (sc->sc_repeating) {
344 sc->sc_repeating = 0;
345 callout_reset(&sc->sc_repeat_callout, hz / 10,
347 return (sc->sc_repeat_key);
350 if (sc->sc_flags & CKB_FLAG_POLLING) {
354 for (i = 0; i < sc->cols; i++) {
355 for (j = 0; j < sc->rows; j++) {
356 oldbit = (sc->scan_local[i] & (1 << j));
357 newbit = (sc->scan[i] & (1 << j));
359 if (oldbit == newbit)
362 key = scantokey(i,j);
369 sc->scan_local[i] |= (1 << j);
371 /* setup repeating */
372 sc->sc_repeat_key = key;
373 callout_reset(&sc->sc_repeat_callout,
374 hz / 2, ckb_repeat, sc);
378 sc->scan_local[i] &= ~(1 << j);
383 /* unsetup repeating */
384 sc->sc_repeat_key = -1;
385 callout_stop(&sc->sc_repeat_callout);
395 /* Currently wait is always false. */
397 ckb_read_char(keyboard_t *kbd, int wait)
402 keycode = ckb_read_char_locked(kbd, wait);
409 /* some useful control functions */
411 ckb_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
413 struct ckb_softc *sc;
421 case KDGKBMODE: /* get keyboard mode */
422 *(int *)arg = sc->sc_mode;
425 case KDSKBMODE: /* set keyboard mode */
426 switch (*(int *)arg) {
428 if (sc->sc_mode != K_XLATE) {
429 /* make lock key state and LED state match */
430 sc->sc_state &= ~LOCK_MASK;
431 sc->sc_state |= KBD_LED_VAL(kbd);
436 if (sc->sc_mode != *(int *)arg) {
437 if ((sc->sc_flags & CKB_FLAG_POLLING) == 0)
438 ckb_clear_state(kbd);
439 sc->sc_mode = *(int *)arg;
447 case KDGETLED: /* get keyboard LED */
448 *(int *)arg = KBD_LED_VAL(kbd);
451 case KDSETLED: /* set keyboard LED */
452 /* NOTE: lock key state in "sc_state" won't be changed */
453 if (*(int *)arg & ~LOCK_MASK)
458 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
459 if (sc->sc_mode == K_XLATE &&
460 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
466 if (KBD_HAS_DEVICE(kbd)) {
470 KBD_LED_VAL(kbd) = *(int *)arg;
472 case KDGKBSTATE: /* get lock key state */
473 *(int *)arg = sc->sc_state & LOCK_MASK;
476 case KDSKBSTATE: /* set lock key state */
477 if (*(int *)arg & ~LOCK_MASK) {
480 sc->sc_state &= ~LOCK_MASK;
481 sc->sc_state |= *(int *)arg;
483 /* set LEDs and quit */
484 return (ckb_ioctl(kbd, KDSETLED, arg));
486 case KDSETREPEAT: /* set keyboard repeat rate (new
489 if (!KBD_HAS_DEVICE(kbd)) {
492 if (((int *)arg)[1] < 0) {
495 if (((int *)arg)[0] < 0) {
498 if (((int *)arg)[0] < 200) /* fastest possible value */
499 kbd->kb_delay1 = 200;
501 kbd->kb_delay1 = ((int *)arg)[0];
502 kbd->kb_delay2 = ((int *)arg)[1];
505 case KDSETRAD: /* set keyboard repeat rate (old
507 return (ckb_set_typematic(kbd, *(int *)arg));
509 case PIO_KEYMAP: /* set keyboard translation table */
510 case OPIO_KEYMAP: /* set keyboard translation table
512 case PIO_KEYMAPENT: /* set keyboard translation table
514 case PIO_DEADKEYMAP: /* set accent key translation table */
518 return (genkbd_commonioctl(kbd, cmd, arg));
525 ckb_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
530 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
531 * context where printf(9) can be called, which among other things
532 * includes interrupt filters and threads with any kinds of locks
533 * already held. For this reason it would be dangerous to acquire
534 * the Giant here unconditionally. On the other hand we have to
535 * have it to handle the ioctl.
536 * So we make our best effort to auto-detect whether we can grab
537 * the Giant or not. Blame syscons(4) for this.
543 if (!mtx_owned(&Giant) && !SCHEDULER_STOPPED())
544 return (EDEADLK); /* best I could come up with */
548 result = ckb_ioctl_locked(kbd, cmd, arg);
556 * Enable the access to the device; until this function is called,
557 * the client cannot read from the keyboard.
560 ckb_enable(keyboard_t *kbd)
570 /* disallow the access to the device */
572 ckb_disable(keyboard_t *kbd)
582 /* local functions */
585 ckb_set_typematic(keyboard_t *kbd, int code)
587 static const int delays[] = {250, 500, 750, 1000};
588 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
589 68, 76, 84, 92, 100, 110, 118, 126,
590 136, 152, 168, 184, 200, 220, 236, 252,
591 272, 304, 336, 368, 400, 440, 472, 504};
596 kbd->kb_delay1 = delays[(code >> 5) & 3];
597 kbd->kb_delay2 = rates[code & 0x1f];
602 ckb_poll(keyboard_t *kbd, int on)
604 struct ckb_softc *sc;
610 sc->sc_flags |= CKB_FLAG_POLLING;
611 sc->sc_poll_thread = curthread;
613 sc->sc_flags &= ~CKB_FLAG_POLLING;
620 /* local functions */
622 static int dummy_kbd_configure(int flags);
624 keyboard_switch_t ckbdsw = {
625 .probe = &ckb__probe,
629 .test_if = &ckb_test_if,
630 .enable = &ckb_enable,
631 .disable = &ckb_disable,
634 .read_char = &ckb_read_char,
635 .check_char = &ckb_check_char,
638 .clear_state = &ckb_clear_state,
639 .get_state = &ckb_get_state,
640 .set_state = &ckb_set_state,
641 .get_fkeystr = &genkbd_get_fkeystr,
643 .diag = &genkbd_diag,
647 dummy_kbd_configure(int flags)
653 KEYBOARD_DRIVER(ckbd, ckbdsw, dummy_kbd_configure);
656 parse_dts(struct ckb_softc *sc)
662 if ((node = ofw_bus_get_node(sc->dev)) == -1)
665 if ((len = OF_getproplen(node, "keypad,num-rows")) <= 0)
667 OF_getprop(node, "keypad,num-rows", &dts_value, len);
668 sc->rows = fdt32_to_cpu(dts_value);
670 if ((len = OF_getproplen(node, "keypad,num-columns")) <= 0)
672 OF_getprop(node, "keypad,num-columns", &dts_value, len);
673 sc->cols = fdt32_to_cpu(dts_value);
675 if ((sc->rows == 0) || (sc->cols == 0))
682 ckb_ec_intr(void *arg)
684 struct ckb_softc *sc;
688 if (sc->sc_flags & CKB_FLAG_POLLING)
691 ec_command(EC_CMD_MKBP_STATE, sc->scan, sc->cols,
694 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
695 sc->sc_kbd.kb_callback.kc_arg);
699 chrome_kb_attach(device_t dev)
701 struct ckb_softc *sc;
707 sc = device_get_softc(dev);
711 if ((error = parse_dts(sc)) != 0)
715 device_printf(sc->dev, "Keyboard matrix [%dx%d]\n",
719 /* TODO: take interrupt from DTS */
720 pad_setup_intr(KB_GPIO_INT, ckb_ec_intr, sc);
725 sc->scan_local = malloc(sc->cols, M_DEVBUF, M_NOWAIT);
726 sc->scan = malloc(sc->cols, M_DEVBUF, M_NOWAIT);
728 for (i = 0; i < sc->cols; i++) {
729 sc->scan_local[i] = 0;
733 kbd_init_struct(kbd, KBD_DRIVER_NAME, KB_OTHER,
734 device_get_unit(dev), 0, 0, 0);
735 kbd->kb_data = (void *)sc;
737 sc->sc_keymap = key_map;
738 sc->sc_accmap = accent_map;
739 for (i = 0; i < CKB_NFKEY; i++) {
740 sc->sc_fkeymap[i] = fkey_tab[i];
743 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
744 sc->sc_fkeymap, CKB_NFKEY);
746 KBD_FOUND_DEVICE(kbd);
747 ckb_clear_state(kbd);
750 callout_init(&sc->sc_repeat_callout, 0);
754 if (kbd_register(kbd) < 0) {
757 KBD_CONFIG_DONE(kbd);
763 chrome_kb_probe(device_t dev)
766 if (!ofw_bus_status_okay(dev))
769 if (ofw_bus_is_compatible(dev, "google,cros-ec-keyb")) {
770 device_set_desc(dev, "Chrome EC Keyboard");
771 return (BUS_PROBE_DEFAULT);
777 static device_method_t chrome_kb_methods[] = {
778 DEVMETHOD(device_probe, chrome_kb_probe),
779 DEVMETHOD(device_attach, chrome_kb_attach),
783 static driver_t chrome_kb_driver = {
786 sizeof(struct ckb_softc),
789 static devclass_t chrome_kb_devclass;
791 DRIVER_MODULE(chrome_kb, simplebus, chrome_kb_driver,
792 chrome_kb_devclass, 0, 0);