chrome_kb.c revision 356013
1/*-
2 * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27/*
28 * Samsung Chromebook Keyboard
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: stable/11/sys/arm/samsung/exynos/chrome_kb.c 356013 2019-12-22 17:15:48Z kevans $");
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/bus.h>
37#include <sys/kernel.h>
38#include <sys/module.h>
39#include <sys/malloc.h>
40#include <sys/rman.h>
41#include <sys/proc.h>
42#include <sys/sched.h>
43#include <sys/kdb.h>
44#include <sys/timeet.h>
45#include <sys/timetc.h>
46#include <sys/mutex.h>
47#include <sys/gpio.h>
48
49#include <dev/ofw/openfirm.h>
50#include <dev/ofw/ofw_bus.h>
51#include <dev/ofw/ofw_bus_subr.h>
52
53#include <sys/ioccom.h>
54#include <sys/filio.h>
55#include <sys/tty.h>
56#include <sys/kbio.h>
57
58#include <machine/bus.h>
59#include <machine/cpu.h>
60#include <machine/intr.h>
61
62#include "gpio_if.h"
63
64#include <arm/samsung/exynos/chrome_ec.h>
65#include <arm/samsung/exynos/chrome_kb.h>
66
67#include <arm/samsung/exynos/exynos5_combiner.h>
68#include <arm/samsung/exynos/exynos5_pad.h>
69
70#define	CKB_LOCK()	mtx_lock(&Giant)
71#define	CKB_UNLOCK()	mtx_unlock(&Giant)
72
73#ifdef	INVARIANTS
74/*
75 * Assert that the lock is held in all contexts
76 * where the code can be executed.
77 */
78#define	CKB_LOCK_ASSERT()	mtx_assert(&Giant, MA_OWNED)
79/*
80 * Assert that the lock is held in the contexts
81 * where it really has to be so.
82 */
83#define	CKB_CTX_LOCK_ASSERT()			 	\
84	do {						\
85		if (!kdb_active && panicstr == NULL)	\
86			mtx_assert(&Giant, MA_OWNED);	\
87	} while (0)
88#else
89#define CKB_LOCK_ASSERT()	(void)0
90#define CKB_CTX_LOCK_ASSERT()	(void)0
91#endif
92
93/*
94 * Define a stub keyboard driver in case one hasn't been
95 * compiled into the kernel
96 */
97#include <sys/kbio.h>
98#include <dev/kbd/kbdreg.h>
99#include <dev/kbd/kbdtables.h>
100
101#define	CKB_NFKEY		12
102#define	CKB_FLAG_COMPOSE	0x1
103#define	CKB_FLAG_POLLING	0x2
104#define	KBD_DRIVER_NAME		"ckbd"
105
106struct ckb_softc {
107	keyboard_t sc_kbd;
108	keymap_t sc_keymap;
109	accentmap_t sc_accmap;
110	fkeytab_t sc_fkeymap[CKB_NFKEY];
111
112	struct resource*	sc_mem_res;
113	struct resource*	sc_irq_res;
114	void*			sc_intr_hl;
115
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 */
120
121	struct callout		sc_repeat_callout;
122	int			sc_repeat_key;
123	int			sc_repeating;
124
125	int			flag;
126	int			rows;
127	int			cols;
128	int			gpio;
129	device_t		dev;
130	device_t		gpio_dev;
131	struct thread		*sc_poll_thread;
132	uint16_t		*keymap;
133
134	uint8_t			*scan_local;
135	uint8_t			*scan;
136};
137
138/* prototypes */
139static int	ckb_set_typematic(keyboard_t *, int);
140static uint32_t	ckb_read_char(keyboard_t *, int);
141static void	ckb_clear_state(keyboard_t *);
142static int	ckb_ioctl(keyboard_t *, u_long, caddr_t);
143static int	ckb_enable(keyboard_t *);
144static int	ckb_disable(keyboard_t *);
145
146static void
147ckb_repeat(void *arg)
148{
149	struct ckb_softc *sc;
150
151	sc = arg;
152
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);
158		}
159	}
160}
161
162/* detect a keyboard, not used */
163static int
164ckb__probe(int unit, void *arg, int flags)
165{
166
167	return (ENXIO);
168}
169
170/* reset and initialize the device, not used */
171static int
172ckb_init(int unit, keyboard_t **kbdp, void *arg, int flags)
173{
174
175	return (ENXIO);
176}
177
178/* test the interface to the device, not used */
179static int
180ckb_test_if(keyboard_t *kbd)
181{
182
183	return (0);
184}
185
186/* finish using this keyboard, not used */
187static int
188ckb_term(keyboard_t *kbd)
189{
190
191	return (ENXIO);
192}
193
194/* keyboard interrupt routine, not used */
195static int
196ckb_intr(keyboard_t *kbd, void *arg)
197{
198
199	return (0);
200}
201
202/* lock the access to the keyboard, not used */
203static int
204ckb_lock(keyboard_t *kbd, int lock)
205{
206
207	return (1);
208}
209
210/* clear the internal state of the keyboard */
211static void
212ckb_clear_state(keyboard_t *kbd)
213{
214	struct ckb_softc *sc;
215
216	sc = kbd->kb_data;
217
218	CKB_CTX_LOCK_ASSERT();
219
220	sc->sc_flags &= ~(CKB_FLAG_COMPOSE | CKB_FLAG_POLLING);
221	sc->sc_state &= LOCK_MASK; /* preserve locking key state */
222	sc->sc_accents = 0;
223}
224
225/* save the internal state, not used */
226static int
227ckb_get_state(keyboard_t *kbd, void *buf, size_t len)
228{
229
230	return (len == 0) ? 1 : -1;
231}
232
233/* set the internal state, not used */
234static int
235ckb_set_state(keyboard_t *kbd, void *buf, size_t len)
236{
237
238	return (EINVAL);
239}
240
241
242/* check if data is waiting */
243static int
244ckb_check(keyboard_t *kbd)
245{
246	struct ckb_softc *sc;
247	int i;
248
249	sc = kbd->kb_data;
250
251	CKB_CTX_LOCK_ASSERT();
252
253	if (!KBD_IS_ACTIVE(kbd))
254		return (0);
255
256	if (sc->sc_flags & CKB_FLAG_POLLING) {
257		return (1);
258	}
259
260	for (i = 0; i < sc->cols; i++)
261		if (sc->scan_local[i] != sc->scan[i]) {
262			return (1);
263		}
264
265	if (sc->sc_repeating)
266		return (1);
267
268	return (0);
269}
270
271/* check if char is waiting */
272static int
273ckb_check_char_locked(keyboard_t *kbd)
274{
275	CKB_CTX_LOCK_ASSERT();
276
277	if (!KBD_IS_ACTIVE(kbd))
278		return (0);
279
280	return (ckb_check(kbd));
281}
282
283static int
284ckb_check_char(keyboard_t *kbd)
285{
286	int result;
287
288	CKB_LOCK();
289	result = ckb_check_char_locked(kbd);
290	CKB_UNLOCK();
291
292	return (result);
293}
294
295/* read one byte from the keyboard if it's allowed */
296/* Currently unused. */
297static int
298ckb_read(keyboard_t *kbd, int wait)
299{
300	CKB_CTX_LOCK_ASSERT();
301
302	if (!KBD_IS_ACTIVE(kbd))
303		return (-1);
304
305	printf("Implement ME: %s\n", __func__);
306	return (0);
307}
308
309static uint16_t
310keymap_read(struct ckb_softc *sc, int col, int row)
311{
312
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];
317	}
318
319	return (0);
320}
321
322static int
323keymap_write(struct ckb_softc *sc, int col, int row, uint16_t key)
324{
325
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;
330		return (0);
331	}
332
333	return (-1);
334}
335
336/* read char from the keyboard */
337static uint32_t
338ckb_read_char_locked(keyboard_t *kbd, int wait)
339{
340	struct ckb_softc *sc;
341	int i,j;
342	uint16_t key;
343	int oldbit;
344	int newbit;
345	int status;
346
347	sc = kbd->kb_data;
348
349	CKB_CTX_LOCK_ASSERT();
350
351	if (!KBD_IS_ACTIVE(kbd))
352		return (NOKEY);
353
354	if (sc->sc_repeating) {
355		sc->sc_repeating = 0;
356		callout_reset(&sc->sc_repeat_callout, hz / 10,
357                    ckb_repeat, sc);
358		return (sc->sc_repeat_key);
359	}
360
361	if (sc->sc_flags & CKB_FLAG_POLLING) {
362		for (;;) {
363			GPIO_PIN_GET(sc->gpio_dev, sc->gpio, &status);
364			if (status == 0) {
365				if (ec_command(EC_CMD_MKBP_STATE, sc->scan,
366					sc->cols,
367				    sc->scan, sc->cols)) {
368					return (NOKEY);
369				}
370				break;
371			}
372			if (!wait) {
373				return (NOKEY);
374			}
375			DELAY(1000);
376		}
377	}
378
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));
383
384			if (oldbit == newbit)
385				continue;
386
387			key = keymap_read(sc, i, j);
388			if (key == 0) {
389				continue;
390			}
391
392			if (newbit > 0) {
393				/* key pressed */
394				sc->scan_local[i] |= (1 << j);
395
396				/* setup repeating */
397				sc->sc_repeat_key = key;
398				callout_reset(&sc->sc_repeat_callout,
399				    hz / 2, ckb_repeat, sc);
400
401			} else {
402				/* key released */
403				sc->scan_local[i] &= ~(1 << j);
404
405				/* release flag */
406				key |= 0x80;
407
408				/* unsetup repeating */
409				sc->sc_repeat_key = -1;
410				callout_stop(&sc->sc_repeat_callout);
411			}
412
413			return (key);
414		}
415	}
416
417	return (NOKEY);
418}
419
420/* Currently wait is always false. */
421static uint32_t
422ckb_read_char(keyboard_t *kbd, int wait)
423{
424	uint32_t keycode;
425
426	CKB_LOCK();
427	keycode = ckb_read_char_locked(kbd, wait);
428	CKB_UNLOCK();
429
430	return (keycode);
431}
432
433
434/* some useful control functions */
435static int
436ckb_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
437{
438	struct ckb_softc *sc;
439	int i;
440
441	sc = kbd->kb_data;
442
443	CKB_LOCK_ASSERT();
444
445	switch (cmd) {
446	case KDGKBMODE:		/* get keyboard mode */
447		*(int *)arg = sc->sc_mode;
448		break;
449
450	case KDSKBMODE:		/* set keyboard mode */
451		switch (*(int *)arg) {
452		case K_XLATE:
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);
457			}
458			/* FALLTHROUGH */
459		case K_RAW:
460		case K_CODE:
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;
465			}
466			break;
467		default:
468			return (EINVAL);
469		}
470		break;
471
472	case KDGETLED:			/* get keyboard LED */
473		*(int *)arg = KBD_LED_VAL(kbd);
474		break;
475
476	case KDSETLED:			/* set keyboard LED */
477		/* NOTE: lock key state in "sc_state" won't be changed */
478		if (*(int *)arg & ~LOCK_MASK)
479			return (EINVAL);
480
481		i = *(int *)arg;
482
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) {
486			if (i & ALKED)
487				i |= CLKED;
488			else
489				i &= ~CLKED;
490		}
491		if (KBD_HAS_DEVICE(kbd)) {
492			/* Configure LED */
493		}
494
495		KBD_LED_VAL(kbd) = *(int *)arg;
496		break;
497	case KDGKBSTATE:		/* get lock key state */
498		*(int *)arg = sc->sc_state & LOCK_MASK;
499		break;
500
501	case KDSKBSTATE:		/* set lock key state */
502		if (*(int *)arg & ~LOCK_MASK) {
503			return (EINVAL);
504		}
505		sc->sc_state &= ~LOCK_MASK;
506		sc->sc_state |= *(int *)arg;
507
508		/* set LEDs and quit */
509		return (ckb_ioctl(kbd, KDSETLED, arg));
510
511	case KDSETREPEAT:		/* set keyboard repeat rate (new
512					 * interface) */
513
514		if (!KBD_HAS_DEVICE(kbd)) {
515			return (0);
516		}
517		if (((int *)arg)[1] < 0) {
518			return (EINVAL);
519		}
520		if (((int *)arg)[0] < 0) {
521			return (EINVAL);
522		}
523		if (((int *)arg)[0] < 200)	/* fastest possible value */
524			kbd->kb_delay1 = 200;
525		else
526			kbd->kb_delay1 = ((int *)arg)[0];
527		kbd->kb_delay2 = ((int *)arg)[1];
528		return (0);
529
530	case KDSETRAD:			/* set keyboard repeat rate (old
531					 * interface) */
532		return (ckb_set_typematic(kbd, *(int *)arg));
533
534	case PIO_KEYMAP:		/* set keyboard translation table */
535	case OPIO_KEYMAP:		/* set keyboard translation table
536					 * (compat) */
537	case PIO_KEYMAPENT:		/* set keyboard translation table
538					 * entry */
539	case PIO_DEADKEYMAP:		/* set accent key translation table */
540		sc->sc_accents = 0;
541		/* FALLTHROUGH */
542	default:
543		return (genkbd_commonioctl(kbd, cmd, arg));
544	}
545
546	return (0);
547}
548
549static int
550ckb_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
551{
552	int result;
553
554	/*
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.
563	 */
564	switch (cmd) {
565	case KDGKBSTATE:
566	case KDSKBSTATE:
567	case KDSETLED:
568		if (!mtx_owned(&Giant) && !SCHEDULER_STOPPED())
569			return (EDEADLK);	/* best I could come up with */
570		/* FALLTHROUGH */
571	default:
572		CKB_LOCK();
573		result = ckb_ioctl_locked(kbd, cmd, arg);
574		CKB_UNLOCK();
575		return (result);
576	}
577}
578
579
580/*
581 * Enable the access to the device; until this function is called,
582 * the client cannot read from the keyboard.
583 */
584static int
585ckb_enable(keyboard_t *kbd)
586{
587
588	CKB_LOCK();
589	KBD_ACTIVATE(kbd);
590	CKB_UNLOCK();
591
592	return (0);
593}
594
595/* disallow the access to the device */
596static int
597ckb_disable(keyboard_t *kbd)
598{
599
600	CKB_LOCK();
601	KBD_DEACTIVATE(kbd);
602	CKB_UNLOCK();
603
604	return (0);
605}
606
607/* local functions */
608
609static int
610ckb_set_typematic(keyboard_t *kbd, int code)
611{
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};
617
618	if (code & ~0x7f) {
619		return (EINVAL);
620	}
621	kbd->kb_delay1 = delays[(code >> 5) & 3];
622	kbd->kb_delay2 = rates[code & 0x1f];
623	return (0);
624}
625
626static int
627ckb_poll(keyboard_t *kbd, int on)
628{
629	struct ckb_softc *sc;
630
631	sc = kbd->kb_data;
632
633	CKB_LOCK();
634	if (on) {
635		sc->sc_flags |= CKB_FLAG_POLLING;
636		sc->sc_poll_thread = curthread;
637	} else {
638		sc->sc_flags &= ~CKB_FLAG_POLLING;
639	}
640	CKB_UNLOCK();
641
642	return (0);
643}
644
645/* local functions */
646
647static int dummy_kbd_configure(int flags);
648
649keyboard_switch_t ckbdsw = {
650	.probe = &ckb__probe,
651	.init = &ckb_init,
652	.term = &ckb_term,
653	.intr = &ckb_intr,
654	.test_if = &ckb_test_if,
655	.enable = &ckb_enable,
656	.disable = &ckb_disable,
657	.read = &ckb_read,
658	.check = &ckb_check,
659	.read_char = &ckb_read_char,
660	.check_char = &ckb_check_char,
661	.ioctl = &ckb_ioctl,
662	.lock = &ckb_lock,
663	.clear_state = &ckb_clear_state,
664	.get_state = &ckb_get_state,
665	.set_state = &ckb_set_state,
666	.poll = &ckb_poll,
667};
668
669static int
670dummy_kbd_configure(int flags)
671{
672
673	return (0);
674}
675
676KEYBOARD_DRIVER(ckbd, ckbdsw, dummy_kbd_configure);
677
678/*
679 * Parses 'keymap' into sc->keymap.
680 * Requires sc->cols and sc->rows to be set.
681 */
682static int
683parse_keymap(struct ckb_softc *sc, pcell_t *keymap, size_t len)
684{
685	int i;
686
687	sc->keymap = malloc(sc->cols * sc->rows * sizeof(sc->keymap[0]),
688	    M_DEVBUF, M_NOWAIT | M_ZERO);
689	if (sc->keymap == NULL) {
690		return (ENOMEM);
691	}
692
693	for (i = 0; i < len; i++) {
694		/*
695		 * Return value is ignored, we just write whatever fits into
696		 * specified number of rows and columns and silently ignore
697		 * everything else.
698		 * Keymap entries follow this format: 0xRRCCKKKK
699		 * RR - row number, CC - column number, KKKK - key code
700		 */
701		keymap_write(sc, (keymap[i] >> 16) & 0xff,
702		    (keymap[i] >> 24) & 0xff,
703		    keymap[i] & 0xffff);
704	}
705
706	return (0);
707}
708
709/* Allocates a new array for keymap and returns it in 'keymap'. */
710static int
711read_keymap(phandle_t node, const char *prop, pcell_t **keymap, size_t *len)
712{
713
714	if ((*len = OF_getproplen(node, prop)) <= 0) {
715		return (ENXIO);
716	}
717	if ((*keymap = malloc(*len, M_DEVBUF, M_NOWAIT)) == NULL) {
718		return (ENOMEM);
719	}
720	if (OF_getencprop(node, prop, *keymap, *len) != *len) {
721		return (ENXIO);
722	}
723	return (0);
724}
725
726static int
727parse_dts(struct ckb_softc *sc)
728{
729	phandle_t node;
730	pcell_t dts_value;
731	pcell_t *keymap;
732	int len, ret;
733	const char *keymap_prop = NULL;
734
735	if ((node = ofw_bus_get_node(sc->dev)) == -1)
736		return (ENXIO);
737
738	if ((len = OF_getproplen(node, "google,key-rows")) <= 0)
739		return (ENXIO);
740	OF_getencprop(node, "google,key-rows", &dts_value, len);
741	sc->rows = dts_value;
742
743	if ((len = OF_getproplen(node, "google,key-columns")) <= 0)
744		return (ENXIO);
745	OF_getencprop(node, "google,key-columns", &dts_value, len);
746	sc->cols = dts_value;
747
748	if ((len = OF_getproplen(node, "freebsd,intr-gpio")) <= 0)
749		return (ENXIO);
750	OF_getencprop(node, "freebsd,intr-gpio", &dts_value, len);
751	sc->gpio = dts_value;
752
753	if (OF_hasprop(node, "freebsd,keymap")) {
754		keymap_prop = "freebsd,keymap";
755		device_printf(sc->dev, "using FreeBSD-specific keymap from FDT\n");
756	} else if (OF_hasprop(node, "linux,keymap")) {
757		keymap_prop = "linux,keymap";
758		device_printf(sc->dev, "using Linux keymap from FDT\n");
759	} else {
760		device_printf(sc->dev, "using built-in keymap\n");
761	}
762
763	if (keymap_prop != NULL) {
764		if ((ret = read_keymap(node, keymap_prop, &keymap, &len))) {
765			device_printf(sc->dev,
766			     "failed to read keymap from FDT: %d\n", ret);
767			return (ret);
768		}
769		ret = parse_keymap(sc, keymap, len);
770		free(keymap, M_DEVBUF);
771		if (ret) {
772			return (ret);
773		}
774	} else {
775		if ((ret = parse_keymap(sc, default_keymap, KEYMAP_LEN))) {
776			return (ret);
777		}
778	}
779
780	if ((sc->rows == 0) || (sc->cols == 0) || (sc->gpio == 0))
781		return (ENXIO);
782
783	return (0);
784}
785
786void
787ckb_ec_intr(void *arg)
788{
789	struct ckb_softc *sc;
790
791	sc = arg;
792
793	if (sc->sc_flags & CKB_FLAG_POLLING)
794		return;
795
796	ec_command(EC_CMD_MKBP_STATE, sc->scan, sc->cols,
797	    sc->scan, sc->cols);
798
799	(sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
800	    sc->sc_kbd.kb_callback.kc_arg);
801};
802
803static int
804chrome_kb_attach(device_t dev)
805{
806	struct ckb_softc *sc;
807	keyboard_t *kbd;
808	int error;
809	int rid;
810	int i;
811
812	sc = device_get_softc(dev);
813
814	sc->dev = dev;
815	sc->keymap = NULL;
816
817	if ((error = parse_dts(sc)) != 0)
818		return error;
819
820	sc->gpio_dev = devclass_get_device(devclass_find("gpio"), 0);
821	if (sc->gpio_dev == NULL) {
822		device_printf(sc->dev, "Can't find gpio device.\n");
823		return (ENXIO);
824	}
825
826#if 0
827	device_printf(sc->dev, "Keyboard matrix [%dx%d]\n",
828	    sc->cols, sc->rows);
829#endif
830
831	pad_setup_intr(sc->gpio, ckb_ec_intr, sc);
832
833	kbd = &sc->sc_kbd;
834	rid = 0;
835
836	sc->scan_local = malloc(sc->cols, M_DEVBUF, M_NOWAIT);
837	sc->scan = malloc(sc->cols, M_DEVBUF, M_NOWAIT);
838
839	for (i = 0; i < sc->cols; i++) {
840		sc->scan_local[i] = 0;
841		sc->scan[i] = 0;
842	}
843
844	kbd_init_struct(kbd, KBD_DRIVER_NAME, KB_OTHER,
845	    device_get_unit(dev), 0, 0, 0);
846	kbd->kb_data = (void *)sc;
847
848	sc->sc_keymap = key_map;
849        sc->sc_accmap = accent_map;
850	for (i = 0; i < CKB_NFKEY; i++) {
851		sc->sc_fkeymap[i] = fkey_tab[i];
852        }
853
854	kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
855	    sc->sc_fkeymap, CKB_NFKEY);
856
857	KBD_FOUND_DEVICE(kbd);
858	ckb_clear_state(kbd);
859	KBD_PROBE_DONE(kbd);
860
861	callout_init(&sc->sc_repeat_callout, 0);
862
863	KBD_INIT_DONE(kbd);
864
865	if (kbd_register(kbd) < 0) {
866		return (ENXIO);
867	}
868	KBD_CONFIG_DONE(kbd);
869
870	return (0);
871}
872
873static int
874chrome_kb_probe(device_t dev)
875{
876
877	if (!ofw_bus_status_okay(dev))
878		return (ENXIO);
879
880	if (ofw_bus_is_compatible(dev, "google,cros-ec-keyb") ||
881	    ofw_bus_is_compatible(dev, "google,mkbp-keyb")) {
882		device_set_desc(dev, "Chrome EC Keyboard");
883		return (BUS_PROBE_DEFAULT);
884	}
885
886	return (ENXIO);
887}
888
889static int
890chrome_kb_detach(device_t dev)
891{
892	struct ckb_softc *sc;
893
894	sc = device_get_softc(dev);
895
896	if (sc->keymap != NULL) {
897		free(sc->keymap, M_DEVBUF);
898	}
899
900	return 0;
901}
902
903static device_method_t chrome_kb_methods[] = {
904	DEVMETHOD(device_probe,		chrome_kb_probe),
905	DEVMETHOD(device_attach,	chrome_kb_attach),
906	DEVMETHOD(device_detach,	chrome_kb_detach),
907	{ 0, 0 }
908};
909
910static driver_t chrome_kb_driver = {
911	"chrome_kb",
912	chrome_kb_methods,
913	sizeof(struct ckb_softc),
914};
915
916static devclass_t chrome_kb_devclass;
917
918DRIVER_MODULE(chrome_kb, simplebus, chrome_kb_driver,
919    chrome_kb_devclass, 0, 0);
920