chrome_kb.c revision 302915
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 302915 2016-07-15 21:30:19Z ian $");
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	.get_fkeystr = &genkbd_get_fkeystr,
667	.poll = &ckb_poll,
668	.diag = &genkbd_diag,
669};
670
671static int
672dummy_kbd_configure(int flags)
673{
674
675	return (0);
676}
677
678KEYBOARD_DRIVER(ckbd, ckbdsw, dummy_kbd_configure);
679
680/*
681 * Parses 'keymap' into sc->keymap.
682 * Requires sc->cols and sc->rows to be set.
683 */
684static int
685parse_keymap(struct ckb_softc *sc, pcell_t *keymap, size_t len)
686{
687	int i;
688
689	sc->keymap = malloc(sc->cols * sc->rows * sizeof(sc->keymap[0]),
690	    M_DEVBUF, M_NOWAIT | M_ZERO);
691	if (sc->keymap == NULL) {
692		return (ENOMEM);
693	}
694
695	for (i = 0; i < len; i++) {
696		/*
697		 * Return value is ignored, we just write whatever fits into
698		 * specified number of rows and columns and silently ignore
699		 * everything else.
700		 * Keymap entries follow this format: 0xRRCCKKKK
701		 * RR - row number, CC - column number, KKKK - key code
702		 */
703		keymap_write(sc, (keymap[i] >> 16) & 0xff,
704		    (keymap[i] >> 24) & 0xff,
705		    keymap[i] & 0xffff);
706	}
707
708	return (0);
709}
710
711/* Allocates a new array for keymap and returns it in 'keymap'. */
712static int
713read_keymap(phandle_t node, const char *prop, pcell_t **keymap, size_t *len)
714{
715
716	if ((*len = OF_getproplen(node, prop)) <= 0) {
717		return (ENXIO);
718	}
719	if ((*keymap = malloc(*len, M_DEVBUF, M_NOWAIT)) == NULL) {
720		return (ENOMEM);
721	}
722	if (OF_getencprop(node, prop, *keymap, *len) != *len) {
723		return (ENXIO);
724	}
725	return (0);
726}
727
728static int
729parse_dts(struct ckb_softc *sc)
730{
731	phandle_t node;
732	pcell_t dts_value;
733	pcell_t *keymap;
734	int len, ret;
735	const char *keymap_prop = NULL;
736
737	if ((node = ofw_bus_get_node(sc->dev)) == -1)
738		return (ENXIO);
739
740	if ((len = OF_getproplen(node, "google,key-rows")) <= 0)
741		return (ENXIO);
742	OF_getencprop(node, "google,key-rows", &dts_value, len);
743	sc->rows = dts_value;
744
745	if ((len = OF_getproplen(node, "google,key-columns")) <= 0)
746		return (ENXIO);
747	OF_getencprop(node, "google,key-columns", &dts_value, len);
748	sc->cols = dts_value;
749
750	if ((len = OF_getproplen(node, "freebsd,intr-gpio")) <= 0)
751		return (ENXIO);
752	OF_getencprop(node, "freebsd,intr-gpio", &dts_value, len);
753	sc->gpio = dts_value;
754
755	if (OF_hasprop(node, "freebsd,keymap")) {
756		keymap_prop = "freebsd,keymap";
757		device_printf(sc->dev, "using FreeBSD-specific keymap from FDT\n");
758	} else if (OF_hasprop(node, "linux,keymap")) {
759		keymap_prop = "linux,keymap";
760		device_printf(sc->dev, "using Linux keymap from FDT\n");
761	} else {
762		device_printf(sc->dev, "using built-in keymap\n");
763	}
764
765	if (keymap_prop != NULL) {
766		if ((ret = read_keymap(node, keymap_prop, &keymap, &len))) {
767			device_printf(sc->dev,
768			     "failed to read keymap from FDT: %d\n", ret);
769			return (ret);
770		}
771		ret = parse_keymap(sc, keymap, len);
772		free(keymap, M_DEVBUF);
773		if (ret) {
774			return (ret);
775		}
776	} else {
777		if ((ret = parse_keymap(sc, default_keymap, KEYMAP_LEN))) {
778			return (ret);
779		}
780	}
781
782	if ((sc->rows == 0) || (sc->cols == 0) || (sc->gpio == 0))
783		return (ENXIO);
784
785	return (0);
786}
787
788void
789ckb_ec_intr(void *arg)
790{
791	struct ckb_softc *sc;
792
793	sc = arg;
794
795	if (sc->sc_flags & CKB_FLAG_POLLING)
796		return;
797
798	ec_command(EC_CMD_MKBP_STATE, sc->scan, sc->cols,
799	    sc->scan, sc->cols);
800
801	(sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
802	    sc->sc_kbd.kb_callback.kc_arg);
803};
804
805static int
806chrome_kb_attach(device_t dev)
807{
808	struct ckb_softc *sc;
809	keyboard_t *kbd;
810	int error;
811	int rid;
812	int i;
813
814	sc = device_get_softc(dev);
815
816	sc->dev = dev;
817	sc->keymap = NULL;
818
819	if ((error = parse_dts(sc)) != 0)
820		return error;
821
822	sc->gpio_dev = devclass_get_device(devclass_find("gpio"), 0);
823	if (sc->gpio_dev == NULL) {
824		device_printf(sc->dev, "Can't find gpio device.\n");
825		return (ENXIO);
826	}
827
828#if 0
829	device_printf(sc->dev, "Keyboard matrix [%dx%d]\n",
830	    sc->cols, sc->rows);
831#endif
832
833	pad_setup_intr(sc->gpio, ckb_ec_intr, sc);
834
835	kbd = &sc->sc_kbd;
836	rid = 0;
837
838	sc->scan_local = malloc(sc->cols, M_DEVBUF, M_NOWAIT);
839	sc->scan = malloc(sc->cols, M_DEVBUF, M_NOWAIT);
840
841	for (i = 0; i < sc->cols; i++) {
842		sc->scan_local[i] = 0;
843		sc->scan[i] = 0;
844	}
845
846	kbd_init_struct(kbd, KBD_DRIVER_NAME, KB_OTHER,
847	    device_get_unit(dev), 0, 0, 0);
848	kbd->kb_data = (void *)sc;
849
850	sc->sc_keymap = key_map;
851        sc->sc_accmap = accent_map;
852	for (i = 0; i < CKB_NFKEY; i++) {
853		sc->sc_fkeymap[i] = fkey_tab[i];
854        }
855
856	kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
857	    sc->sc_fkeymap, CKB_NFKEY);
858
859	KBD_FOUND_DEVICE(kbd);
860	ckb_clear_state(kbd);
861	KBD_PROBE_DONE(kbd);
862
863	callout_init(&sc->sc_repeat_callout, 0);
864
865	KBD_INIT_DONE(kbd);
866
867	if (kbd_register(kbd) < 0) {
868		return (ENXIO);
869	}
870	KBD_CONFIG_DONE(kbd);
871
872	return (0);
873}
874
875static int
876chrome_kb_probe(device_t dev)
877{
878
879	if (!ofw_bus_status_okay(dev))
880		return (ENXIO);
881
882	if (ofw_bus_is_compatible(dev, "google,cros-ec-keyb") ||
883	    ofw_bus_is_compatible(dev, "google,mkbp-keyb")) {
884		device_set_desc(dev, "Chrome EC Keyboard");
885		return (BUS_PROBE_DEFAULT);
886	}
887
888	return (ENXIO);
889}
890
891static int
892chrome_kb_detach(device_t dev)
893{
894	struct ckb_softc *sc;
895
896	sc = device_get_softc(dev);
897
898	if (sc->keymap != NULL) {
899		free(sc->keymap, M_DEVBUF);
900	}
901
902	return 0;
903}
904
905static device_method_t chrome_kb_methods[] = {
906	DEVMETHOD(device_probe,		chrome_kb_probe),
907	DEVMETHOD(device_attach,	chrome_kb_attach),
908	DEVMETHOD(device_detach,	chrome_kb_detach),
909	{ 0, 0 }
910};
911
912static driver_t chrome_kb_driver = {
913	"chrome_kb",
914	chrome_kb_methods,
915	sizeof(struct ckb_softc),
916};
917
918static devclass_t chrome_kb_devclass;
919
920DRIVER_MODULE(chrome_kb, simplebus, chrome_kb_driver,
921    chrome_kb_devclass, 0, 0);
922