2 * Copyright (c) 2015-2016 Oleksandr Tymoshenko <gonzo@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_platform.h"
32 #include "opt_evdev.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
46 #include <sys/ioccom.h>
47 #include <sys/filio.h>
50 #include <dev/kbd/kbdreg.h>
51 #include <dev/kbd/kbdtables.h>
53 #include <dev/fdt/fdt_common.h>
54 #include <dev/ofw/ofw_bus.h>
55 #include <dev/ofw/ofw_bus_subr.h>
57 #include <dev/gpio/gpiobusvar.h>
58 #include <dev/gpio/gpiokeys.h>
61 #include <dev/evdev/evdev.h>
62 #include <dev/evdev/input.h>
65 #define KBD_DRIVER_NAME "gpiokeys"
67 #define GPIOKEYS_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
68 #define GPIOKEYS_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
69 #define GPIOKEYS_LOCK_INIT(_sc) \
70 mtx_init(&_sc->sc_mtx, device_get_nameunit((_sc)->sc_dev), \
72 #define GPIOKEYS_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx);
73 #define GPIOKEYS_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
75 #define GPIOKEY_LOCK(_key) mtx_lock(&(_key)->mtx)
76 #define GPIOKEY_UNLOCK(_key) mtx_unlock(&(_key)->mtx)
77 #define GPIOKEY_LOCK_INIT(_key) \
78 mtx_init(&(_key)->mtx, "gpiokey", "gpiokey", MTX_DEF)
79 #define GPIOKEY_LOCK_DESTROY(_key) mtx_destroy(&(_key)->mtx);
82 #define KEY_RELEASE 0x80
85 #define SCAN_RELEASE 0x80
86 #define SCAN_CHAR(c) ((c) & 0x7f)
88 #define GPIOKEYS_GLOBAL_NMOD 8 /* units */
89 #define GPIOKEYS_GLOBAL_NKEYCODE 6 /* units */
90 #define GPIOKEYS_GLOBAL_IN_BUF_SIZE (2*(GPIOKEYS_GLOBAL_NMOD + (2*GPIOKEYS_GLOBAL_NKEYCODE))) /* bytes */
91 #define GPIOKEYS_GLOBAL_IN_BUF_FULL (GPIOKEYS_GLOBAL_IN_BUF_SIZE / 2) /* bytes */
92 #define GPIOKEYS_GLOBAL_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */
93 #define GPIOKEYS_GLOBAL_BUFFER_SIZE 64 /* bytes */
95 #define AUTOREPEAT_DELAY 250
96 #define AUTOREPEAT_REPEAT 34
98 struct gpiokeys_softc;
102 struct gpiokeys_softc *parent_sc;
105 struct resource *irq_res;
113 struct callout debounce_callout;
114 struct callout repeat_callout;
117 int debounce_interval;
120 struct gpiokeys_softc
124 struct gpiokey *sc_keys;
128 struct evdev_dev *sc_evdev;
132 accentmap_t sc_accmap;
133 fkeytab_t sc_fkeymap[GPIOKEYS_GLOBAL_NFKEY];
135 uint32_t sc_input[GPIOKEYS_GLOBAL_IN_BUF_SIZE]; /* input buffer */
137 #define GPIOKEYS_GLOBAL_FLAG_POLLING 0x00000002
139 uint32_t sc_flags; /* flags */
141 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
142 int sc_state; /* shift/lock key state */
143 int sc_accents; /* accent key index (> 0) */
147 uint16_t sc_inputhead;
148 uint16_t sc_inputtail;
153 /* gpio-keys device */
154 static int gpiokeys_probe(device_t);
155 static int gpiokeys_attach(device_t);
156 static int gpiokeys_detach(device_t);
158 /* kbd methods prototypes */
159 static int gpiokeys_set_typematic(keyboard_t *, int);
160 static uint32_t gpiokeys_read_char(keyboard_t *, int);
161 static void gpiokeys_clear_state(keyboard_t *);
162 static int gpiokeys_ioctl(keyboard_t *, u_long, caddr_t);
163 static int gpiokeys_enable(keyboard_t *);
164 static int gpiokeys_disable(keyboard_t *);
165 static void gpiokeys_event_keyinput(struct gpiokeys_softc *);
168 gpiokeys_put_key(struct gpiokeys_softc *sc, uint32_t key)
171 GPIOKEYS_ASSERT_LOCKED(sc);
173 if (sc->sc_inputs < GPIOKEYS_GLOBAL_IN_BUF_SIZE) {
174 sc->sc_input[sc->sc_inputtail] = key;
176 ++(sc->sc_inputtail);
177 if (sc->sc_inputtail >= GPIOKEYS_GLOBAL_IN_BUF_SIZE) {
178 sc->sc_inputtail = 0;
181 device_printf(sc->sc_dev, "input buffer is full\n");
186 gpiokeys_key_event(struct gpiokeys_softc *sc, struct gpiokey *key, int pressed)
192 if (key->evcode != GPIOKEY_NONE &&
193 (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD) != 0) {
194 evdev_push_key(sc->sc_evdev, key->evcode, pressed);
195 evdev_sync(sc->sc_evdev);
198 if (key->keycode != GPIOKEY_NONE) {
199 code = key->keycode & SCAN_KEYCODE_MASK;
203 if (key->keycode & SCAN_PREFIX_E0)
204 gpiokeys_put_key(sc, 0xe0);
205 else if (key->keycode & SCAN_PREFIX_E1)
206 gpiokeys_put_key(sc, 0xe1);
208 gpiokeys_put_key(sc, code);
212 if (key->keycode != GPIOKEY_NONE)
213 gpiokeys_event_keyinput(sc);
217 gpiokey_autorepeat(void *arg)
223 gpiokeys_key_event(key->parent_sc, key, 1);
225 callout_reset(&key->repeat_callout, key->repeat,
226 gpiokey_autorepeat, key);
230 gpiokey_debounced_intr(void *arg)
237 gpio_pin_is_active(key->pin, &active);
239 gpiokeys_key_event(key->parent_sc, key, 1);
240 if (key->autorepeat) {
241 callout_reset(&key->repeat_callout, key->repeat_delay,
242 gpiokey_autorepeat, key);
246 if (key->autorepeat &&
247 callout_pending(&key->repeat_callout))
248 callout_stop(&key->repeat_callout);
249 gpiokeys_key_event(key->parent_sc, key, 0);
254 gpiokey_intr(void *arg)
262 debounce_ticks = (hz * key->debounce_interval) / 1000;
263 if (debounce_ticks == 0)
265 if (!callout_pending(&key->debounce_callout))
266 callout_reset(&key->debounce_callout, debounce_ticks,
267 gpiokey_debounced_intr, key);
272 gpiokeys_attach_key(struct gpiokeys_softc *sc, phandle_t node,
279 const char *key_name;
281 GPIOKEY_LOCK_INIT(key);
283 callout_init_mtx(&key->debounce_callout, &key->mtx, 0);
284 callout_init_mtx(&key->repeat_callout, &key->mtx, 0);
287 if (OF_getprop_alloc(node, "label", (void **)&name) == -1)
288 OF_getprop_alloc(node, "name", (void **)&name);
293 key_name = "unknown";
295 key->autorepeat = OF_hasprop(node, "autorepeat");
297 key->repeat_delay = (hz * AUTOREPEAT_DELAY) / 1000;
298 if (key->repeat_delay == 0)
299 key->repeat_delay = 1;
301 key->repeat = (hz * AUTOREPEAT_REPEAT) / 1000;
302 if (key->repeat == 0)
305 if ((OF_getprop(node, "debounce-interval", &prop, sizeof(prop))) > 0)
306 key->debounce_interval = fdt32_to_cpu(prop);
308 key->debounce_interval = 5;
310 if ((OF_getprop(node, "freebsd,code", &prop, sizeof(prop))) > 0)
311 key->keycode = fdt32_to_cpu(prop);
312 else if ((OF_getprop(node, "linux,code", &prop, sizeof(prop))) > 0) {
313 code = fdt32_to_cpu(prop);
314 key->keycode = gpiokey_map_linux_code(code);
315 if (key->keycode == GPIOKEY_NONE)
316 device_printf(sc->sc_dev, "<%s> failed to map linux,code value 0x%x\n",
320 evdev_support_key(sc->sc_evdev, code);
324 device_printf(sc->sc_dev, "<%s> no linux,code or freebsd,code property\n",
327 err = gpio_pin_get_by_ofw_idx(sc->sc_dev, node, 0, &key->pin);
329 device_printf(sc->sc_dev, "<%s> failed to map pin\n", key_name);
335 key->irq_res = gpio_alloc_intr_resource(sc->sc_dev, &key->irq_rid,
336 RF_ACTIVE, key->pin, GPIO_INTR_EDGE_BOTH);
338 device_printf(sc->sc_dev, "<%s> cannot allocate interrupt\n", key_name);
339 gpio_pin_release(key->pin);
346 if (bus_setup_intr(sc->sc_dev, key->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
347 NULL, gpiokey_intr, key,
348 &key->intr_hl) != 0) {
349 device_printf(sc->sc_dev, "<%s> unable to setup the irq handler\n", key_name);
350 bus_release_resource(sc->sc_dev, SYS_RES_IRQ, key->irq_rid,
352 gpio_pin_release(key->pin);
361 device_printf(sc->sc_dev, "<%s> code=%08x, autorepeat=%d, "\
362 "repeat=%d, repeat_delay=%d\n", key_name, key->keycode,
363 key->autorepeat, key->repeat, key->repeat_delay);
370 gpiokeys_detach_key(struct gpiokeys_softc *sc, struct gpiokey *key)
375 bus_teardown_intr(sc->sc_dev, key->irq_res, key->intr_hl);
377 bus_release_resource(sc->sc_dev, SYS_RES_IRQ,
378 key->irq_rid, key->irq_res);
379 if (callout_pending(&key->repeat_callout))
380 callout_drain(&key->repeat_callout);
381 if (callout_pending(&key->debounce_callout))
382 callout_drain(&key->debounce_callout);
384 gpio_pin_release(key->pin);
386 GPIOKEY_LOCK_DESTROY(key);
390 gpiokeys_probe(device_t dev)
392 if (!ofw_bus_is_compatible(dev, "gpio-keys"))
395 device_set_desc(dev, "GPIO keyboard");
401 gpiokeys_attach(device_t dev)
403 struct gpiokeys_softc *sc;
408 phandle_t keys, child;
412 if ((keys = ofw_bus_get_node(dev)) == -1)
415 sc = device_get_softc(dev);
419 GPIOKEYS_LOCK_INIT(sc);
420 unit = device_get_unit(dev);
421 kbd_init_struct(kbd, KBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
423 kbd->kb_data = (void *)sc;
424 sc->sc_mode = K_XLATE;
426 sc->sc_keymap = key_map;
427 sc->sc_accmap = accent_map;
429 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
430 sc->sc_fkeymap, GPIOKEYS_GLOBAL_NFKEY);
432 KBD_FOUND_DEVICE(kbd);
434 gpiokeys_clear_state(kbd);
440 if (kbd_register(kbd) < 0) {
444 KBD_CONFIG_DONE(kbd);
446 gpiokeys_enable(kbd);
448 #ifdef KBD_INSTALL_CDEV
449 if (kbd_attach(kbd)) {
459 sc->sc_evdev = evdev_alloc();
460 evdev_set_name(sc->sc_evdev, device_get_desc(dev));
462 OF_getprop_alloc(keys, "name", (void **)&name);
463 evdev_set_phys(sc->sc_evdev, name != NULL ? name : "unknown");
466 evdev_set_id(sc->sc_evdev, BUS_VIRTUAL, 0, 0, 0);
467 evdev_support_event(sc->sc_evdev, EV_SYN);
468 evdev_support_event(sc->sc_evdev, EV_KEY);
473 /* Traverse the 'gpio-keys' node and count keys */
474 for (child = OF_child(keys); child != 0; child = OF_peer(child)) {
475 if (!OF_hasprop(child, "gpios"))
481 sc->sc_keys = malloc(sizeof(struct gpiokey) * total_keys,
482 M_DEVBUF, M_WAITOK | M_ZERO);
484 sc->sc_total_keys = 0;
485 /* Traverse the 'gpio-keys' node and count keys */
486 for (child = OF_child(keys); child != 0; child = OF_peer(child)) {
487 if (!OF_hasprop(child, "gpios"))
489 gpiokeys_attach_key(sc, child ,&sc->sc_keys[sc->sc_total_keys]);
495 if (evdev_register_mtx(sc->sc_evdev, &sc->sc_mtx) != 0) {
496 device_printf(dev, "failed to register evdev device\n");
504 gpiokeys_detach(dev);
509 gpiokeys_detach(device_t dev)
511 struct gpiokeys_softc *sc;
515 sc = device_get_softc(dev);
517 for (i = 0; i < sc->sc_total_keys; i++)
518 gpiokeys_detach_key(sc, &sc->sc_keys[i]);
520 kbd = kbd_get_keyboard(kbd_find_keyboard(KBD_DRIVER_NAME,
521 device_get_unit(dev)));
523 #ifdef KBD_INSTALL_CDEV
529 evdev_free(sc->sc_evdev);
532 GPIOKEYS_LOCK_DESTROY(sc);
534 free(sc->sc_keys, M_DEVBUF);
539 /* early keyboard probe, not supported */
541 gpiokeys_configure(int flags)
546 /* detect a keyboard, not used */
548 gpiokeys__probe(int unit, void *arg, int flags)
553 /* reset and initialize the device, not used */
555 gpiokeys_init(int unit, keyboard_t **kbdp, void *arg, int flags)
560 /* test the interface to the device, not used */
562 gpiokeys_test_if(keyboard_t *kbd)
567 /* finish using this keyboard, not used */
569 gpiokeys_term(keyboard_t *kbd)
574 /* keyboard interrupt routine, not used */
576 gpiokeys_intr(keyboard_t *kbd, void *arg)
581 /* lock the access to the keyboard, not used */
583 gpiokeys_lock(keyboard_t *kbd, int lock)
589 * Enable the access to the device; until this function is called,
590 * the client cannot read from the keyboard.
593 gpiokeys_enable(keyboard_t *kbd)
595 struct gpiokeys_softc *sc;
605 /* disallow the access to the device */
607 gpiokeys_disable(keyboard_t *kbd)
609 struct gpiokeys_softc *sc;
620 gpiokeys_do_poll(struct gpiokeys_softc *sc, uint8_t wait)
623 KASSERT((sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) != 0,
624 ("gpiokeys_do_poll called when not polling\n"));
626 GPIOKEYS_ASSERT_LOCKED(sc);
628 if (!kdb_active && !SCHEDULER_STOPPED()) {
629 while (sc->sc_inputs == 0) {
630 kern_yield(PRI_UNCHANGED);
637 while ((sc->sc_inputs == 0) && wait) {
642 /* check if data is waiting */
644 gpiokeys_check(keyboard_t *kbd)
646 struct gpiokeys_softc *sc = kbd->kb_data;
648 GPIOKEYS_ASSERT_LOCKED(sc);
650 if (!KBD_IS_ACTIVE(kbd))
653 if (sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING)
654 gpiokeys_do_poll(sc, 0);
656 if (sc->sc_inputs > 0) {
662 /* check if char is waiting */
664 gpiokeys_check_char_locked(keyboard_t *kbd)
666 if (!KBD_IS_ACTIVE(kbd))
669 return (gpiokeys_check(kbd));
673 gpiokeys_check_char(keyboard_t *kbd)
676 struct gpiokeys_softc *sc = kbd->kb_data;
679 result = gpiokeys_check_char_locked(kbd);
686 gpiokeys_get_key(struct gpiokeys_softc *sc, uint8_t wait)
690 KASSERT((!kdb_active && !SCHEDULER_STOPPED())
691 || (sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) != 0,
692 ("not polling in kdb or panic\n"));
694 GPIOKEYS_ASSERT_LOCKED(sc);
696 if (sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING)
697 gpiokeys_do_poll(sc, wait);
699 if (sc->sc_inputs == 0) {
702 c = sc->sc_input[sc->sc_inputhead];
704 ++(sc->sc_inputhead);
705 if (sc->sc_inputhead >= GPIOKEYS_GLOBAL_IN_BUF_SIZE) {
706 sc->sc_inputhead = 0;
713 /* read one byte from the keyboard if it's allowed */
715 gpiokeys_read(keyboard_t *kbd, int wait)
717 struct gpiokeys_softc *sc = kbd->kb_data;
720 if (!KBD_IS_ACTIVE(kbd))
724 keycode = gpiokeys_get_key(sc, (wait == FALSE) ? 0 : 1);
725 if (!KBD_IS_ACTIVE(kbd) || (keycode == -1))
733 /* read char from the keyboard */
735 gpiokeys_read_char_locked(keyboard_t *kbd, int wait)
737 struct gpiokeys_softc *sc = kbd->kb_data;
741 if (!KBD_IS_ACTIVE(kbd))
746 /* see if there is something in the keyboard port */
748 keycode = gpiokeys_get_key(sc, (wait == FALSE) ? 0 : 1);
751 /* return the byte as is for the K_RAW mode */
752 if (sc->sc_mode == K_RAW) {
756 /* return the key code in the K_CODE mode */
757 /* XXX: keycode |= SCAN_RELEASE; */
759 if (sc->sc_mode == K_CODE) {
763 /* keycode to key action */
764 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
765 (keycode & SCAN_RELEASE),
766 &sc->sc_state, &sc->sc_accents);
767 if (action == NOKEY) {
774 /* Currently wait is always false. */
776 gpiokeys_read_char(keyboard_t *kbd, int wait)
779 struct gpiokeys_softc *sc = kbd->kb_data;
782 keycode = gpiokeys_read_char_locked(kbd, wait);
788 /* some useful control functions */
790 gpiokeys_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
792 struct gpiokeys_softc *sc = kbd->kb_data;
793 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
794 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
800 case KDGKBMODE: /* get keyboard mode */
801 *(int *)arg = sc->sc_mode;
803 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
804 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
806 ival = IOCPARM_IVAL(arg);
807 arg = (caddr_t)&ival;
810 case KDSKBMODE: /* set keyboard mode */
811 switch (*(int *)arg) {
813 if (sc->sc_mode != K_XLATE) {
814 /* make lock key state and LED state match */
815 sc->sc_state &= ~LOCK_MASK;
816 sc->sc_state |= KBD_LED_VAL(kbd);
821 if (sc->sc_mode != *(int *)arg) {
822 if ((sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) == 0)
823 gpiokeys_clear_state(kbd);
824 sc->sc_mode = *(int *)arg;
832 case KDGETLED: /* get keyboard LED */
833 *(int *)arg = KBD_LED_VAL(kbd);
835 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
836 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
838 ival = IOCPARM_IVAL(arg);
839 arg = (caddr_t)&ival;
842 case KDSETLED: /* set keyboard LED */
843 KBD_LED_VAL(kbd) = *(int *)arg;
845 case KDGKBSTATE: /* get lock key state */
846 *(int *)arg = sc->sc_state & LOCK_MASK;
848 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
849 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
851 ival = IOCPARM_IVAL(arg);
852 arg = (caddr_t)&ival;
855 case KDSKBSTATE: /* set lock key state */
856 if (*(int *)arg & ~LOCK_MASK) {
859 sc->sc_state &= ~LOCK_MASK;
860 sc->sc_state |= *(int *)arg;
863 case KDSETREPEAT: /* set keyboard repeat rate (new
865 if (!KBD_HAS_DEVICE(kbd)) {
868 if (((int *)arg)[1] < 0) {
871 if (((int *)arg)[0] < 0) {
874 if (((int *)arg)[0] < 200) /* fastest possible value */
875 kbd->kb_delay1 = 200;
877 kbd->kb_delay1 = ((int *)arg)[0];
878 kbd->kb_delay2 = ((int *)arg)[1];
881 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
882 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
884 ival = IOCPARM_IVAL(arg);
885 arg = (caddr_t)&ival;
888 case KDSETRAD: /* set keyboard repeat rate (old
890 return (gpiokeys_set_typematic(kbd, *(int *)arg));
892 case PIO_KEYMAP: /* set keyboard translation table */
893 case OPIO_KEYMAP: /* set keyboard translation table
895 case PIO_KEYMAPENT: /* set keyboard translation table
897 case PIO_DEADKEYMAP: /* set accent key translation table */
901 return (genkbd_commonioctl(kbd, cmd, arg));
908 gpiokeys_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
911 struct gpiokeys_softc *sc;
915 * XXX Check if someone is calling us from a critical section:
917 if (curthread->td_critnest != 0)
921 result = gpiokeys_ioctl_locked(kbd, cmd, arg);
927 /* clear the internal state of the keyboard */
929 gpiokeys_clear_state(keyboard_t *kbd)
931 struct gpiokeys_softc *sc = kbd->kb_data;
933 sc->sc_flags &= ~(GPIOKEYS_GLOBAL_FLAG_POLLING);
934 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
938 /* get the internal state, not used */
940 gpiokeys_get_state(keyboard_t *kbd, void *buf, size_t len)
942 return (len == 0) ? 1 : -1;
945 /* set the internal state, not used */
947 gpiokeys_set_state(keyboard_t *kbd, void *buf, size_t len)
953 gpiokeys_poll(keyboard_t *kbd, int on)
955 struct gpiokeys_softc *sc = kbd->kb_data;
959 sc->sc_flags |= GPIOKEYS_GLOBAL_FLAG_POLLING;
961 sc->sc_flags &= ~GPIOKEYS_GLOBAL_FLAG_POLLING;
968 gpiokeys_set_typematic(keyboard_t *kbd, int code)
970 static const int delays[] = {250, 500, 750, 1000};
971 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
972 68, 76, 84, 92, 100, 110, 118, 126,
973 136, 152, 168, 184, 200, 220, 236, 252,
974 272, 304, 336, 368, 400, 440, 472, 504};
979 kbd->kb_delay1 = delays[(code >> 5) & 3];
980 kbd->kb_delay2 = rates[code & 0x1f];
985 gpiokeys_event_keyinput(struct gpiokeys_softc *sc)
989 if ((sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) != 0)
992 if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
993 KBD_IS_BUSY(&sc->sc_kbd)) {
994 /* let the callback function process the input */
995 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
996 sc->sc_kbd.kb_callback.kc_arg);
998 /* read and discard the input, no one is waiting for it */
1000 c = gpiokeys_read_char(&sc->sc_kbd, 0);
1001 } while (c != NOKEY);
1005 static keyboard_switch_t gpiokeyssw = {
1006 .probe = &gpiokeys__probe,
1007 .init = &gpiokeys_init,
1008 .term = &gpiokeys_term,
1009 .intr = &gpiokeys_intr,
1010 .test_if = &gpiokeys_test_if,
1011 .enable = &gpiokeys_enable,
1012 .disable = &gpiokeys_disable,
1013 .read = &gpiokeys_read,
1014 .check = &gpiokeys_check,
1015 .read_char = &gpiokeys_read_char,
1016 .check_char = &gpiokeys_check_char,
1017 .ioctl = &gpiokeys_ioctl,
1018 .lock = &gpiokeys_lock,
1019 .clear_state = &gpiokeys_clear_state,
1020 .get_state = &gpiokeys_get_state,
1021 .set_state = &gpiokeys_set_state,
1022 .poll = &gpiokeys_poll,
1025 KEYBOARD_DRIVER(gpiokeys, gpiokeyssw, gpiokeys_configure);
1028 gpiokeys_driver_load(module_t mod, int what, void *arg)
1032 kbd_add_driver(&gpiokeys_kbd_driver);
1035 kbd_delete_driver(&gpiokeys_kbd_driver);
1041 static devclass_t gpiokeys_devclass;
1043 static device_method_t gpiokeys_methods[] = {
1044 DEVMETHOD(device_probe, gpiokeys_probe),
1045 DEVMETHOD(device_attach, gpiokeys_attach),
1046 DEVMETHOD(device_detach, gpiokeys_detach),
1051 static driver_t gpiokeys_driver = {
1054 sizeof(struct gpiokeys_softc),
1057 DRIVER_MODULE(gpiokeys, simplebus, gpiokeys_driver, gpiokeys_devclass, gpiokeys_driver_load, 0);
1058 MODULE_VERSION(gpiokeys, 1);