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