diff --git a/sys/arm/conf/BEAGLEBONE b/sys/arm/conf/BEAGLEBONE index 3950e17..1883f8e 100644 --- a/sys/arm/conf/BEAGLEBONE +++ b/sys/arm/conf/BEAGLEBONE @@ -145,3 +145,4 @@ device hdmi device ums device ukbd device kbdmux +device gpiokeys diff --git a/sys/arm/ti/ti_gpio.c b/sys/arm/ti/ti_gpio.c index 2818bab..27a0182 100644 --- a/sys/arm/ti/ti_gpio.c +++ b/sys/arm/ti/ti_gpio.c @@ -690,7 +690,7 @@ ti_gpio_attach(device_t dev) sizeof(*sc->sc_irq_polarity) * sc->sc_maxpin, M_DEVBUF, M_WAITOK | M_ZERO); for (i = 0; i < sc->sc_maxpin; i++) { - sc->sc_irq_trigger[i] = INTR_TRIGGER_LEVEL; + sc->sc_irq_trigger[i] = INTR_TRIGGER_EDGE; sc->sc_irq_polarity[i] = INTR_POLARITY_LOW; } @@ -807,6 +807,15 @@ ti_gpio_mask_irq_internal(struct ti_gpio_softc *sc, int irq) val &= ~TI_GPIO_MASK(irq); ti_gpio_write_4(sc, reg, val); } + + val = ti_gpio_read_4(sc, TI_GPIO_FALLINGDETECT); + val &= ~TI_GPIO_MASK(irq); + ti_gpio_write_4(sc, TI_GPIO_FALLINGDETECT, val); + + val = ti_gpio_read_4(sc, TI_GPIO_RISINGDETECT); + val &= ~TI_GPIO_MASK(irq); + ti_gpio_write_4(sc, TI_GPIO_RISINGDETECT, val); + TI_GPIO_UNLOCK(sc); } @@ -819,6 +828,15 @@ ti_gpio_unmask_irq_internal(struct ti_gpio_softc *sc, int irq) return; TI_GPIO_LOCK(sc); + + val = ti_gpio_read_4(sc, TI_GPIO_FALLINGDETECT); + val |= TI_GPIO_MASK(irq); + ti_gpio_write_4(sc, TI_GPIO_FALLINGDETECT, val); + + val = ti_gpio_read_4(sc, TI_GPIO_RISINGDETECT); + val |= TI_GPIO_MASK(irq); + ti_gpio_write_4(sc, TI_GPIO_RISINGDETECT, val); + reg = ti_gpio_intr_reg(sc, irq); if (reg != 0) { val = ti_gpio_read_4(sc, reg); diff --git a/sys/conf/files b/sys/conf/files index 9345e3c..59a61e3 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -1448,6 +1448,8 @@ dev/gem/if_gem.c optional gem dev/gem/if_gem_pci.c optional gem pci dev/gem/if_gem_sbus.c optional gem sbus dev/gpio/gpiobacklight.c optional gpiobacklight fdt +dev/gpio/gpiokey.c optional gpiokeys fdt +dev/gpio/gpiokeys.c optional gpiokeys fdt dev/gpio/gpiobus.c optional gpio \ dependency "gpiobus_if.h" dev/gpio/gpioc.c optional gpio \ diff --git a/sys/dev/gpio/gpiobus.c b/sys/dev/gpio/gpiobus.c index 67106ea..a119417 100644 --- a/sys/dev/gpio/gpiobus.c +++ b/sys/dev/gpio/gpiobus.c @@ -512,6 +512,7 @@ gpiobus_alloc_resource(device_t bus, device_t child, int type, int *rid, struct resource *rv; struct resource_list *rl; struct resource_list_entry *rle; + struct gpiobus_ivar *devi; int isdefault; if (type != SYS_RES_IRQ) @@ -520,16 +521,23 @@ gpiobus_alloc_resource(device_t bus, device_t child, int type, int *rid, rle = NULL; if (isdefault) { rl = BUS_GET_RESOURCE_LIST(bus, child); - if (rl == NULL) - return (NULL); - rle = resource_list_find(rl, type, *rid); - if (rle == NULL) - return (NULL); - if (rle->res != NULL) - panic("%s: resource entry is busy", __func__); - start = rle->start; - count = rle->count; - end = rle->end; + if (rl != NULL) + rle = resource_list_find(rl, type, *rid); + if (rle != NULL) { + if (rle->res != NULL) + panic("%s: resource entry is busy", __func__); + start = rle->start; + count = rle->count; + end = rle->end; + } + else + { + devi = GPIOBUS_IVAR(child); + if (*rid >= devi->npins) + return (NULL); + end = start = devi->pins[*rid]; + count = 1; + } } sc = device_get_softc(bus); rv = rman_reserve_resource(&sc->sc_intr_rman, start, end, count, flags, diff --git a/sys/dev/gpio/gpiokey.c b/sys/dev/gpio/gpiokey.c new file mode 100644 index 0000000..5d20ec5 --- /dev/null +++ b/sys/dev/gpio/gpiokey.c @@ -0,0 +1,323 @@ +/*- + * Copyright (c) 2015 Oleksandr Tymoshenko + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include "opt_platform.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include "gpiobus_if.h" + +#define AUTOREPEAT_DELAY 250 +#define AUTOREPEAT_REPEAT 34 + +struct gpiokey_softc +{ + device_t sc_dev; + device_t sc_busdev; + int sc_irq_rid; + struct resource *sc_irq_res; + void *sc_intr_hl; + struct mtx sc_mtx; + uint32_t sc_keycode; + int sc_autorepeat; + struct callout sc_debounce_callout; + struct callout sc_repeat_callout; + int sc_repeat_delay; + int sc_repeat; +}; + +#define GPIOKEY_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) +#define GPIOKEY_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) +#define GPIOKEY_LOCK_INIT(_sc) \ + mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \ + "gpiokey", MTX_DEF) +#define GPIOKEY_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); + +/* No key code */ +#define GPIOKEY_NONE 0 + +#define GPIOKEY_E0(k) (SCAN_PREFIX_E0 | k) + +/* Single key device */ +static int gpiokey_probe(device_t); +static int gpiokey_attach(device_t); +static int gpiokey_detach(device_t); + +static uint32_t +gpiokey_map_linux_code(uint32_t linux_code) +{ + switch (linux_code) { + case 28: /* ENTER */ + return 28; + break; + case 105: /* LEFT */ + return GPIOKEY_E0(0x4b); + break; + case 106: /* RIGHT */ + return GPIOKEY_E0(0x4d); + break; + case 103: /* UP */ + return GPIOKEY_E0(0x48); + break; + case 108: /* DOWN */ + return GPIOKEY_E0(0x50); + break; + + default: + return GPIOKEY_NONE; + } +} + +static void +gpiokey_autorepeat(void *arg) +{ + struct gpiokey_softc *sc; + + sc = arg; + + if (sc->sc_keycode == GPIOKEY_NONE) + return; + + gpiokeys_key_event(sc->sc_keycode, 1); + + callout_reset(&sc->sc_repeat_callout, sc->sc_repeat, + gpiokey_autorepeat, sc); +} + +static void +gpiokey_debounced_intr(void *arg) +{ + struct gpiokey_softc *sc; + int error; + int val; + + sc = arg; + + if (sc->sc_keycode == GPIOKEY_NONE) + return; + + error = GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev, + GPIOBUS_DONTWAIT); + if (error != 0) + return; + GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, 0, &val); + GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev); + if (val == 0) { + gpiokeys_key_event(sc->sc_keycode, 1); + if (sc->sc_autorepeat) { + callout_reset(&sc->sc_repeat_callout, sc->sc_repeat_delay, + gpiokey_autorepeat, sc); + } + } + else { + if (sc->sc_autorepeat && + callout_pending(&sc->sc_repeat_callout)) + callout_stop(&sc->sc_repeat_callout); + gpiokeys_key_event(sc->sc_keycode, 0); + } +} + +static void +gpiokey_intr(void *arg) +{ + struct gpiokey_softc *sc; + int debounce_ticks; + + sc = arg; + + GPIOKEY_LOCK(sc); + debounce_ticks = hz*5/1000; + if (debounce_ticks == 0) + debounce_ticks = 1; + if (!callout_pending(&sc->sc_debounce_callout)) + callout_reset(&sc->sc_debounce_callout, debounce_ticks, + gpiokey_debounced_intr, sc); + GPIOKEY_UNLOCK(sc); +} + + +static void +gpiokey_identify(driver_t *driver, device_t bus) +{ + phandle_t child, keys, root; + + root = OF_finddevice("/"); + if (root == 0) + return; + for (keys = OF_child(root); keys != 0; keys = OF_peer(keys)) { + if (!fdt_is_compatible_strict(keys, "gpio-keys")) + continue; + /* Traverse the 'gpio-keys' node and add its children. */ + for (child = OF_child(keys); child != 0; child = OF_peer(child)) { + if (!OF_hasprop(child, "gpios")) + continue; + if (ofw_gpiobus_add_fdt_child(bus, driver->name, child) == NULL) + continue; + } + } +} + +static int +gpiokey_probe(device_t dev) +{ + struct gpiokey_softc *sc; + phandle_t node; + pcell_t prop; + char *name; + uint32_t code; + + sc = device_get_softc(dev); + if ((node = ofw_bus_get_node(dev)) == -1) + return (ENXIO); + + name = NULL; + if (OF_getprop_alloc(node, "label", 1, (void **)&name) == -1) + OF_getprop_alloc(node, "name", 1, (void **)&name); + + if (name != NULL) { + device_set_desc_copy(dev, name); + free(name, M_OFWPROP); + } + + sc->sc_autorepeat = OF_hasprop(node, "autorepeat"); + if ((OF_getprop(node, "freebsd,code", &prop, sizeof(prop))) > 0) + sc->sc_keycode = fdt32_to_cpu(prop); + else if ((OF_getprop(node, "linux,code", &prop, sizeof(prop))) > 0) { + code = fdt32_to_cpu(prop); + sc->sc_keycode = gpiokey_map_linux_code(code); + if (sc->sc_keycode == GPIOKEY_NONE) + device_printf(dev, "failed to map linux,code value %d\n", code); + } + else + device_printf(dev, "no key code property\n"); + + return (0); +} + +static int +gpiokey_attach(device_t dev) +{ + struct gpiokey_softc *sc; + + sc = device_get_softc(dev); + sc->sc_dev = dev; + sc->sc_busdev = device_get_parent(dev); + + sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irq_rid, + RF_ACTIVE); + if (!sc->sc_irq_res) { + device_printf(dev, "cannot allocate interrupt\n"); + return (ENXIO); + } + + if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE, + NULL, gpiokey_intr, sc, + &sc->sc_intr_hl) != 0) { + bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid, + sc->sc_irq_res); + device_printf(dev, "Unable to setup the irq handler.\n"); + return (ENXIO); + } + + GPIOKEY_LOCK_INIT(sc); + + sc->sc_repeat_delay = hz*AUTOREPEAT_DELAY/1000; + if (sc->sc_repeat_delay == 0) + sc->sc_repeat_delay = 1; + + sc->sc_repeat = hz*AUTOREPEAT_REPEAT/1000; + if (sc->sc_repeat == 0) + sc->sc_repeat = 1; + + callout_init_mtx(&sc->sc_debounce_callout, &sc->sc_mtx, 0); + callout_init_mtx(&sc->sc_repeat_callout, &sc->sc_mtx, 0); + + return (0); +} + +static int +gpiokey_detach(device_t dev) +{ + struct gpiokey_softc *sc; + + sc = device_get_softc(dev); + + GPIOKEY_LOCK(sc); + if (sc->sc_intr_hl) { + bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr_hl); + bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid, + sc->sc_irq_res); + } + + if (callout_pending(&sc->sc_repeat_callout)) + callout_stop(&sc->sc_repeat_callout); + if (callout_pending(&sc->sc_debounce_callout)) + callout_stop(&sc->sc_debounce_callout); + GPIOKEY_UNLOCK(sc); + + GPIOKEY_LOCK_DESTROY(sc); + + return (0); +} + +static devclass_t gpiokey_devclass; + +static device_method_t gpiokey_methods[] = { + /* Device interface */ + DEVMETHOD(device_identify, gpiokey_identify), + + DEVMETHOD(device_probe, gpiokey_probe), + DEVMETHOD(device_attach, gpiokey_attach), + DEVMETHOD(device_detach, gpiokey_detach), + + { 0, 0 } +}; + +static driver_t gpiokey_driver = { + "gpiokey", + gpiokey_methods, + sizeof(struct gpiokey_softc), +}; + +DRIVER_MODULE(gpiokey, gpiobus, gpiokey_driver, gpiokey_devclass, 0, 0); diff --git a/sys/dev/gpio/gpiokeys.c b/sys/dev/gpio/gpiokeys.c new file mode 100644 index 0000000..717e3e5 --- /dev/null +++ b/sys/dev/gpio/gpiokeys.c @@ -0,0 +1,764 @@ +/*- + * Copyright (c) 2015 Oleksandr Tymoshenko + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include "opt_platform.h" +#include "opt_kbd.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include + +#define GPIOKEYS_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) +#define GPIOKEYS_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) +#define GPIOKEYS_LOCK_INIT(_sc) \ + mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \ + "gpiokeys", MTX_DEF) +#define GPIOKEYS_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); + +#define KEY_PRESS 0 +#define KEY_RELEASE 0x80 + +#define SCAN_PRESS 0 +#define SCAN_RELEASE 0x80 +#define SCAN_CHAR(c) ((c) & 0x7f) + +#define GPIOKEYS_GLOBAL_NMOD 8 /* units */ +#define GPIOKEYS_GLOBAL_NKEYCODE 6 /* units */ +#define GPIOKEYS_GLOBAL_IN_BUF_SIZE (2*(GPIOKEYS_GLOBAL_NMOD + (2*GPIOKEYS_GLOBAL_NKEYCODE))) /* bytes */ +#define GPIOKEYS_GLOBAL_IN_BUF_FULL (GPIOKEYS_GLOBAL_IN_BUF_SIZE / 2) /* bytes */ +#define GPIOKEYS_GLOBAL_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */ +#define GPIOKEYS_GLOBAL_BUFFER_SIZE 64 /* bytes */ + +struct gpiokeys_softc +{ + device_t sc_dev; + struct mtx sc_mtx; + + keyboard_t sc_kbd; + keymap_t sc_keymap; + accentmap_t sc_accmap; + fkeytab_t sc_fkeymap[GPIOKEYS_GLOBAL_NFKEY]; + + uint32_t sc_input[GPIOKEYS_GLOBAL_IN_BUF_SIZE]; /* input buffer */ + uint32_t sc_time_ms; +#define GPIOKEYS_GLOBAL_FLAG_POLLING 0x00000002 + + uint32_t sc_flags; /* flags */ + + int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */ + int sc_state; /* shift/lock key state */ + int sc_accents; /* accent key index (> 0) */ + int sc_kbd_size; + + uint16_t sc_inputs; + uint16_t sc_inputhead; + uint16_t sc_inputtail; + + uint8_t sc_kbd_id; +}; + +struct gpiokeys_softc *gpiokeys_sc; + +/* gpio-keys device */ +static int gpiokeys_probe(device_t); +static int gpiokeys_attach(device_t); +static int gpiokeys_detach(device_t); + +/* kbd methods prototypes */ +static int gpiokeys_set_typematic(keyboard_t *, int); +static uint32_t gpiokeys_read_char(keyboard_t *, int); +static void gpiokeys_clear_state(keyboard_t *); +static int gpiokeys_ioctl(keyboard_t *, u_long, caddr_t); +static int gpiokeys_enable(keyboard_t *); +static int gpiokeys_disable(keyboard_t *); +static void gpiokeys_event_keyinput(struct gpiokeys_softc *); + +static int +gpiokeys_probe(device_t dev) +{ + if (!ofw_bus_is_compatible(dev, "gpio-keys")) + return (ENXIO); + + device_set_desc(dev, "GPIO keyboard"); + + return (0); +} + +static int +gpiokeys_attach(device_t dev) +{ + int unit; + struct gpiokeys_softc *sc; + keyboard_t *kbd; + + sc = device_get_softc(dev); + sc->sc_dev = dev; + kbd = &sc->sc_kbd; + + GPIOKEYS_LOCK_INIT(sc); + unit = device_get_unit(dev); + kbd_init_struct(kbd, "gpiokeys", KB_OTHER, unit, 0, 0, 0); + + kbd->kb_data = (void *)sc; + sc->sc_mode = K_XLATE; + + sc->sc_keymap = key_map; + sc->sc_accmap = accent_map; + + kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap, + sc->sc_fkeymap, GPIOKEYS_GLOBAL_NFKEY); + + KBD_FOUND_DEVICE(kbd); + + gpiokeys_clear_state(kbd); + + KBD_PROBE_DONE(kbd); + + KBD_INIT_DONE(kbd); + + if (kbd_register(kbd) < 0) { + goto detach; + } + + KBD_CONFIG_DONE(kbd); + + gpiokeys_enable(kbd); + +#ifdef KBD_INSTALL_CDEV + if (kbd_attach(kbd)) { + goto detach; + } +#endif + + if (bootverbose) { + genkbd_diag(kbd, 1); + } + + gpiokeys_sc = sc; + + return (0); +detach: + gpiokeys_detach(dev); + return (ENXIO); +} + +static int +gpiokeys_detach(device_t dev) +{ + struct gpiokeys_softc *sc; + + sc = device_get_softc(dev); + GPIOKEYS_LOCK_DESTROY(sc); + + return (0); +} + +static void +gpiokeys_put_key(struct gpiokeys_softc *sc, uint32_t key) +{ + if (sc->sc_inputs < GPIOKEYS_GLOBAL_IN_BUF_SIZE) { + sc->sc_input[sc->sc_inputtail] = key; + ++(sc->sc_inputs); + ++(sc->sc_inputtail); + if (sc->sc_inputtail >= GPIOKEYS_GLOBAL_IN_BUF_SIZE) { + sc->sc_inputtail = 0; + } + } else { + device_printf(sc->sc_dev, "input buffer is full\n"); + } +} + + + +void +gpiokeys_key_event(uint16_t keycode, int pressed) +{ + uint32_t key; + + key = keycode & SCAN_KEYCODE_MASK; + + if (!pressed) + key |= KEY_RELEASE; + + if (keycode & SCAN_PREFIX_E0) + gpiokeys_put_key(gpiokeys_sc, 0xe0); + else if (keycode & SCAN_PREFIX_E1) + gpiokeys_put_key(gpiokeys_sc, 0xe1); + + gpiokeys_put_key(gpiokeys_sc, key); + + gpiokeys_event_keyinput(gpiokeys_sc); +} + + + + +/* early keyboard probe, not supported */ +static int +gpiokeys_configure(int flags) +{ + return (0); +} + +/* detect a keyboard, not used */ +static int +gpiokeys__probe(int unit, void *arg, int flags) +{ + return (ENXIO); +} + +/* reset and initialize the device, not used */ +static int +gpiokeys_init(int unit, keyboard_t **kbdp, void *arg, int flags) +{ + return (ENXIO); +} + +/* test the interface to the device, not used */ +static int +gpiokeys_test_if(keyboard_t *kbd) +{ + return (0); +} + +/* finish using this keyboard, not used */ +static int +gpiokeys_term(keyboard_t *kbd) +{ + return (ENXIO); +} + +/* keyboard interrupt routine, not used */ +static int +gpiokeys_intr(keyboard_t *kbd, void *arg) +{ + return (0); +} + +/* lock the access to the keyboard, not used */ +static int +gpiokeys_lock(keyboard_t *kbd, int lock) +{ + return (1); +} + +/* + * Enable the access to the device; until this function is called, + * the client cannot read from the keyboard. + */ +static int +gpiokeys_enable(keyboard_t *kbd) +{ + struct gpiokeys_softc *sc; + + sc = kbd->kb_data; + GPIOKEYS_LOCK(sc); + KBD_ACTIVATE(kbd); + GPIOKEYS_UNLOCK(sc); + + return (0); +} + +/* disallow the access to the device */ +static int +gpiokeys_disable(keyboard_t *kbd) +{ + struct gpiokeys_softc *sc; + + sc = kbd->kb_data; + GPIOKEYS_LOCK(sc); + KBD_DEACTIVATE(kbd); + GPIOKEYS_UNLOCK(sc); + + return (0); +} + +static void +gpiokeys_do_poll(struct gpiokeys_softc *sc, uint8_t wait) +{ + + KASSERT((sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) != 0, + ("gpiokeys_do_poll called when not polling\n")); + + if (!kdb_active && !SCHEDULER_STOPPED()) { + while (sc->sc_inputs == 0) { + kern_yield(PRI_UNCHANGED); + if (!wait) + break; + } + return; + } + + while ((sc->sc_inputs == 0) && wait) { + printf("POLL!\n"); + } +} + +/* check if data is waiting */ +static int +gpiokeys_check(keyboard_t *kbd) +{ + struct gpiokeys_softc *sc = kbd->kb_data; + + + if (!KBD_IS_ACTIVE(kbd)) + return (0); + + if (sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) + gpiokeys_do_poll(sc, 0); + + if (sc->sc_inputs > 0) { + return (1); + } + return (0); +} + +/* check if char is waiting */ +static int +gpiokeys_check_char_locked(keyboard_t *kbd) +{ + if (!KBD_IS_ACTIVE(kbd)) + return (0); + + return (gpiokeys_check(kbd)); +} + +static int +gpiokeys_check_char(keyboard_t *kbd) +{ + int result; + struct gpiokeys_softc *sc = kbd->kb_data; + + GPIOKEYS_LOCK(sc); + result = gpiokeys_check_char_locked(kbd); + GPIOKEYS_UNLOCK(sc); + + return (result); +} + +static int32_t +gpiokeys_get_key(struct gpiokeys_softc *sc, uint8_t wait) +{ + int32_t c; + + KASSERT((!kdb_active && !SCHEDULER_STOPPED()) + || (sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) != 0, + ("not polling in kdb or panic\n")); + + if (sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) + gpiokeys_do_poll(sc, wait); + + if (sc->sc_inputs == 0) { + c = -1; + } else { + c = sc->sc_input[sc->sc_inputhead]; + --(sc->sc_inputs); + ++(sc->sc_inputhead); + if (sc->sc_inputhead >= GPIOKEYS_GLOBAL_IN_BUF_SIZE) { + sc->sc_inputhead = 0; + } + } + + return (c); +} + +/* read one byte from the keyboard if it's allowed */ +static int +gpiokeys_read(keyboard_t *kbd, int wait) +{ + struct gpiokeys_softc *sc = kbd->kb_data; + int32_t keycode; + + if (!KBD_IS_ACTIVE(kbd)) + return (-1); + + /* XXX */ + keycode = gpiokeys_get_key(sc, (wait == FALSE) ? 0 : 1); + if (!KBD_IS_ACTIVE(kbd) || (keycode == -1)) + return (-1); + + ++(kbd->kb_count); + + return (keycode); +} + +/* read char from the keyboard */ +static uint32_t +gpiokeys_read_char_locked(keyboard_t *kbd, int wait) +{ + struct gpiokeys_softc *sc = kbd->kb_data; + uint32_t action; + uint32_t keycode; + + if (!KBD_IS_ACTIVE(kbd)) + return (NOKEY); + +next_code: + + /* see if there is something in the keyboard port */ + /* XXX */ + keycode = gpiokeys_get_key(sc, (wait == FALSE) ? 0 : 1); + ++kbd->kb_count; + + /* return the byte as is for the K_RAW mode */ + if (sc->sc_mode == K_RAW) { + return (keycode); + } + + /* return the key code in the K_CODE mode */ + /* XXX: keycode |= SCAN_RELEASE; */ + + if (sc->sc_mode == K_CODE) { + return (keycode); + } + + /* keycode to key action */ + action = genkbd_keyaction(kbd, SCAN_CHAR(keycode), + (keycode & SCAN_RELEASE), + &sc->sc_state, &sc->sc_accents); + if (action == NOKEY) { + goto next_code; + } + + return (action); +} + +/* Currently wait is always false. */ +static uint32_t +gpiokeys_read_char(keyboard_t *kbd, int wait) +{ + uint32_t keycode; + struct gpiokeys_softc *sc = kbd->kb_data; + + GPIOKEYS_LOCK(sc); + keycode = gpiokeys_read_char_locked(kbd, wait); + GPIOKEYS_UNLOCK(sc); + + return (keycode); +} + +/* some useful control functions */ +static int +gpiokeys_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg) +{ + struct gpiokeys_softc *sc = kbd->kb_data; +#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ + defined(COMPAT_FREEBSD4) || defined(COMPAT_43) + int ival; + +#endif + + switch (cmd) { + case KDGKBMODE: /* get keyboard mode */ + *(int *)arg = sc->sc_mode; + break; +#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ + defined(COMPAT_FREEBSD4) || defined(COMPAT_43) + case _IO('K', 7): + ival = IOCPARM_IVAL(arg); + arg = (caddr_t)&ival; + /* FALLTHROUGH */ +#endif + case KDSKBMODE: /* set keyboard mode */ + switch (*(int *)arg) { + case K_XLATE: + if (sc->sc_mode != K_XLATE) { + /* make lock key state and LED state match */ + sc->sc_state &= ~LOCK_MASK; + sc->sc_state |= KBD_LED_VAL(kbd); + } + /* FALLTHROUGH */ + case K_RAW: + case K_CODE: + if (sc->sc_mode != *(int *)arg) { + if ((sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) == 0) + gpiokeys_clear_state(kbd); + sc->sc_mode = *(int *)arg; + } + break; + default: + return (EINVAL); + } + break; + + case KDGETLED: /* get keyboard LED */ + *(int *)arg = KBD_LED_VAL(kbd); + break; +#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ + defined(COMPAT_FREEBSD4) || defined(COMPAT_43) + case _IO('K', 66): + ival = IOCPARM_IVAL(arg); + arg = (caddr_t)&ival; + /* FALLTHROUGH */ +#endif + case KDSETLED: /* set keyboard LED */ + KBD_LED_VAL(kbd) = *(int *)arg; + break; + case KDGKBSTATE: /* get lock key state */ + *(int *)arg = sc->sc_state & LOCK_MASK; + break; +#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ + defined(COMPAT_FREEBSD4) || defined(COMPAT_43) + case _IO('K', 20): + ival = IOCPARM_IVAL(arg); + arg = (caddr_t)&ival; + /* FALLTHROUGH */ +#endif + case KDSKBSTATE: /* set lock key state */ + if (*(int *)arg & ~LOCK_MASK) { + return (EINVAL); + } + sc->sc_state &= ~LOCK_MASK; + sc->sc_state |= *(int *)arg; + return (0); + + case KDSETREPEAT: /* set keyboard repeat rate (new + * interface) */ + if (!KBD_HAS_DEVICE(kbd)) { + return (0); + } + if (((int *)arg)[1] < 0) { + return (EINVAL); + } + if (((int *)arg)[0] < 0) { + return (EINVAL); + } + if (((int *)arg)[0] < 200) /* fastest possible value */ + kbd->kb_delay1 = 200; + else + kbd->kb_delay1 = ((int *)arg)[0]; + kbd->kb_delay2 = ((int *)arg)[1]; + return (0); + +#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ + defined(COMPAT_FREEBSD4) || defined(COMPAT_43) + case _IO('K', 67): + ival = IOCPARM_IVAL(arg); + arg = (caddr_t)&ival; + /* FALLTHROUGH */ +#endif + case KDSETRAD: /* set keyboard repeat rate (old + * interface) */ + return (gpiokeys_set_typematic(kbd, *(int *)arg)); + + case PIO_KEYMAP: /* set keyboard translation table */ + case OPIO_KEYMAP: /* set keyboard translation table + * (compat) */ + case PIO_KEYMAPENT: /* set keyboard translation table + * entry */ + case PIO_DEADKEYMAP: /* set accent key translation table */ + sc->sc_accents = 0; + /* FALLTHROUGH */ + default: + return (genkbd_commonioctl(kbd, cmd, arg)); + } + + return (0); +} + +static int +gpiokeys_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) +{ + int result; + struct gpiokeys_softc *sc; + + sc = kbd->kb_data; + /* + * XXX Check if someone is calling us from a critical section: + */ + if (curthread->td_critnest != 0) + return (EDEADLK); + + GPIOKEYS_LOCK(sc); + result = gpiokeys_ioctl_locked(kbd, cmd, arg); + GPIOKEYS_UNLOCK(sc); + + return (result); +} + + +/* clear the internal state of the keyboard */ +static void +gpiokeys_clear_state(keyboard_t *kbd) +{ + struct gpiokeys_softc *sc = kbd->kb_data; + + sc->sc_flags &= ~(GPIOKEYS_GLOBAL_FLAG_POLLING); + sc->sc_state &= LOCK_MASK; /* preserve locking key state */ + sc->sc_accents = 0; +} + +/* get the internal state, not used */ +static int +gpiokeys_get_state(keyboard_t *kbd, void *buf, size_t len) +{ + return (len == 0) ? 1 : -1; +} + +/* set the internal state, not used */ +static int +gpiokeys_set_state(keyboard_t *kbd, void *buf, size_t len) +{ + return (EINVAL); +} + +static int +gpiokeys_poll(keyboard_t *kbd, int on) +{ + struct gpiokeys_softc *sc = kbd->kb_data; + + GPIOKEYS_LOCK(sc); + if (on) + sc->sc_flags |= GPIOKEYS_GLOBAL_FLAG_POLLING; + else + sc->sc_flags &= ~GPIOKEYS_GLOBAL_FLAG_POLLING; + GPIOKEYS_UNLOCK(sc); + + return (0); +} + +static int +gpiokeys_set_typematic(keyboard_t *kbd, int code) +{ + static const int delays[] = {250, 500, 750, 1000}; + static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63, + 68, 76, 84, 92, 100, 110, 118, 126, + 136, 152, 168, 184, 200, 220, 236, 252, + 272, 304, 336, 368, 400, 440, 472, 504}; + + if (code & ~0x7f) { + return (EINVAL); + } + kbd->kb_delay1 = delays[(code >> 5) & 3]; + kbd->kb_delay2 = rates[code & 0x1f]; + return (0); +} + +static void +gpiokeys_event_keyinput(struct gpiokeys_softc *sc) +{ + int c; + + if ((sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) != 0) + return; + + if (sc->sc_inputs == 0) + return; + + if (KBD_IS_ACTIVE(&sc->sc_kbd) && + KBD_IS_BUSY(&sc->sc_kbd)) { + /* let the callback function process the input */ + (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT, + sc->sc_kbd.kb_callback.kc_arg); + } else { + /* read and discard the input, no one is waiting for it */ + do { + c = gpiokeys_read_char(&sc->sc_kbd, 0); + } while (c != NOKEY); + } +} + +static keyboard_switch_t gpiokeyssw = { + .probe = &gpiokeys__probe, + .init = &gpiokeys_init, + .term = &gpiokeys_term, + .intr = &gpiokeys_intr, + .test_if = &gpiokeys_test_if, + .enable = &gpiokeys_enable, + .disable = &gpiokeys_disable, + .read = &gpiokeys_read, + .check = &gpiokeys_check, + .read_char = &gpiokeys_read_char, + .check_char = &gpiokeys_check_char, + .ioctl = &gpiokeys_ioctl, + .lock = &gpiokeys_lock, + .clear_state = &gpiokeys_clear_state, + .get_state = &gpiokeys_get_state, + .set_state = &gpiokeys_set_state, + .get_fkeystr = &genkbd_get_fkeystr, + .poll = &gpiokeys_poll, + .diag = &genkbd_diag, +}; + +KEYBOARD_DRIVER(gpiokeys, gpiokeyssw, gpiokeys_configure); + +static int +gpiokeys_driver_load(module_t mod, int what, void *arg) +{ + switch (what) { + case MOD_LOAD: + kbd_add_driver(&gpiokeys_kbd_driver); + break; + case MOD_UNLOAD: + kbd_delete_driver(&gpiokeys_kbd_driver); + break; + } + return (0); +} + +static devclass_t gpiokeys_devclass; + +static device_method_t gpiokeys_methods[] = { + DEVMETHOD(device_probe, gpiokeys_probe), + DEVMETHOD(device_attach, gpiokeys_attach), + DEVMETHOD(device_detach, gpiokeys_detach), + + { 0, 0 } +}; + +static driver_t gpiokeys_driver = { + "gpiokeys", + gpiokeys_methods, + sizeof(struct gpiokeys_softc), +}; + +DRIVER_MODULE(gpiokeys, simplebus, gpiokeys_driver, gpiokeys_devclass, gpiokeys_driver_load, 0); +MODULE_VERSION(gpiokeys, 1); diff --git a/sys/dev/gpio/gpiokeys.h b/sys/dev/gpio/gpiokeys.h new file mode 100644 index 0000000..0a116c8 --- /dev/null +++ b/sys/dev/gpio/gpiokeys.h @@ -0,0 +1,37 @@ +/*- + * Copyright (c) 2015 Oleksandr Tymoshenko + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ +#ifndef __GPIOKEYS_H__ +#define __GPIOKEYS_H__ + +#define SCAN_KEYCODE_MASK 0xff +#define SCAN_PREFIX_E0 0x10000000 +#define SCAN_PREFIX_E1 0x20000000 + +void gpiokeys_key_event(uint16_t keycode, int pressed); + +#endif /* __GPIOKEYS_H__ */