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