Lines Matching +full:wait +full:- +full:pin

1 /*-
2 * Copyright (c) 2015-2016 Oleksandr Tymoshenko <gonzo@freebsd.org>
65 #define GPIOKEYS_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
66 #define GPIOKEYS_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
68 mtx_init(&_sc->sc_mtx, device_get_nameunit((_sc)->sc_dev), \
70 #define GPIOKEYS_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx);
71 #define GPIOKEYS_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
73 #define GPIOKEY_LOCK(_key) mtx_lock(&(_key)->mtx)
74 #define GPIOKEY_UNLOCK(_key) mtx_unlock(&(_key)->mtx)
76 mtx_init(&(_key)->mtx, "gpiokey", "gpiokey", MTX_DEF)
77 #define GPIOKEY_LOCK_DESTROY(_key) mtx_destroy(&(_key)->mtx);
101 gpio_pin_t pin; member
151 /* gpio-keys device */
171 if (sc->sc_inputs < GPIOKEYS_GLOBAL_IN_BUF_SIZE) { in gpiokeys_put_key()
172 sc->sc_input[sc->sc_inputtail] = key; in gpiokeys_put_key()
173 ++(sc->sc_inputs); in gpiokeys_put_key()
174 ++(sc->sc_inputtail); in gpiokeys_put_key()
175 if (sc->sc_inputtail >= GPIOKEYS_GLOBAL_IN_BUF_SIZE) { in gpiokeys_put_key()
176 sc->sc_inputtail = 0; in gpiokeys_put_key()
179 device_printf(sc->sc_dev, "input buffer is full\n"); in gpiokeys_put_key()
190 if (key->evcode != GPIOKEY_NONE && in gpiokeys_key_event()
192 evdev_push_key(sc->sc_evdev, key->evcode, pressed); in gpiokeys_key_event()
193 evdev_sync(sc->sc_evdev); in gpiokeys_key_event()
195 if (evdev_is_grabbed(sc->sc_evdev)) { in gpiokeys_key_event()
200 if (key->keycode != GPIOKEY_NONE) { in gpiokeys_key_event()
201 code = key->keycode & SCAN_KEYCODE_MASK; in gpiokeys_key_event()
205 if (key->keycode & SCAN_PREFIX_E0) in gpiokeys_key_event()
207 else if (key->keycode & SCAN_PREFIX_E1) in gpiokeys_key_event()
214 if (key->keycode != GPIOKEY_NONE) in gpiokeys_key_event()
225 gpiokeys_key_event(key->parent_sc, key, 1); in gpiokey_autorepeat()
227 callout_reset(&key->repeat_callout, key->repeat, in gpiokey_autorepeat()
239 gpio_pin_is_active(key->pin, &active); in gpiokey_debounced_intr()
241 gpiokeys_key_event(key->parent_sc, key, 1); in gpiokey_debounced_intr()
242 if (key->autorepeat) { in gpiokey_debounced_intr()
243 callout_reset(&key->repeat_callout, key->repeat_delay, in gpiokey_debounced_intr()
248 if (key->autorepeat && in gpiokey_debounced_intr()
249 callout_pending(&key->repeat_callout)) in gpiokey_debounced_intr()
250 callout_stop(&key->repeat_callout); in gpiokey_debounced_intr()
251 gpiokeys_key_event(key->parent_sc, key, 0); in gpiokey_debounced_intr()
264 debounce_ticks = (hz * key->debounce_interval) / 1000; in gpiokey_intr()
267 if (!callout_pending(&key->debounce_callout)) in gpiokey_intr()
268 callout_reset(&key->debounce_callout, debounce_ticks, in gpiokey_intr()
284 key->parent_sc = sc; in gpiokeys_attach_key()
285 callout_init_mtx(&key->debounce_callout, &key->mtx, 0); in gpiokeys_attach_key()
286 callout_init_mtx(&key->repeat_callout, &key->mtx, 0); in gpiokeys_attach_key()
289 if (OF_getprop_alloc(node, "label", (void **)&name) == -1) in gpiokeys_attach_key()
297 key->autorepeat = OF_hasprop(node, "autorepeat"); in gpiokeys_attach_key()
299 key->repeat_delay = (hz * AUTOREPEAT_DELAY) / 1000; in gpiokeys_attach_key()
300 if (key->repeat_delay == 0) in gpiokeys_attach_key()
301 key->repeat_delay = 1; in gpiokeys_attach_key()
303 key->repeat = (hz * AUTOREPEAT_REPEAT) / 1000; in gpiokeys_attach_key()
304 if (key->repeat == 0) in gpiokeys_attach_key()
305 key->repeat = 1; in gpiokeys_attach_key()
307 if ((OF_getprop(node, "debounce-interval", &prop, sizeof(prop))) > 0) in gpiokeys_attach_key()
308 key->debounce_interval = fdt32_to_cpu(prop); in gpiokeys_attach_key()
310 key->debounce_interval = 5; in gpiokeys_attach_key()
313 key->keycode = fdt32_to_cpu(prop); in gpiokeys_attach_key()
316 key->keycode = gpiokey_map_linux_code(code); in gpiokeys_attach_key()
317 if (key->keycode == GPIOKEY_NONE) in gpiokeys_attach_key()
318 device_printf(sc->sc_dev, "<%s> failed to map linux,code value 0x%x\n", in gpiokeys_attach_key()
321 key->evcode = code; in gpiokeys_attach_key()
322 evdev_support_key(sc->sc_evdev, code); in gpiokeys_attach_key()
326 device_printf(sc->sc_dev, "<%s> no linux,code or freebsd,code property\n", in gpiokeys_attach_key()
329 err = gpio_pin_get_by_ofw_idx(sc->sc_dev, node, 0, &key->pin); in gpiokeys_attach_key()
331 device_printf(sc->sc_dev, "<%s> failed to map pin\n", key_name); in gpiokeys_attach_key()
337 key->irq_res = gpio_alloc_intr_resource(sc->sc_dev, &key->irq_rid, in gpiokeys_attach_key()
338 RF_ACTIVE, key->pin, GPIO_INTR_EDGE_BOTH); in gpiokeys_attach_key()
339 if (!key->irq_res) { in gpiokeys_attach_key()
340 device_printf(sc->sc_dev, "<%s> cannot allocate interrupt\n", key_name); in gpiokeys_attach_key()
341 gpio_pin_release(key->pin); in gpiokeys_attach_key()
342 key->pin = NULL; in gpiokeys_attach_key()
348 if (bus_setup_intr(sc->sc_dev, key->irq_res, INTR_TYPE_MISC | INTR_MPSAFE, in gpiokeys_attach_key()
350 &key->intr_hl) != 0) { in gpiokeys_attach_key()
351 device_printf(sc->sc_dev, "<%s> unable to setup the irq handler\n", key_name); in gpiokeys_attach_key()
352 bus_release_resource(sc->sc_dev, SYS_RES_IRQ, key->irq_rid, in gpiokeys_attach_key()
353 key->irq_res); in gpiokeys_attach_key()
354 gpio_pin_release(key->pin); in gpiokeys_attach_key()
355 key->pin = NULL; in gpiokeys_attach_key()
356 key->irq_res = NULL; in gpiokeys_attach_key()
363 device_printf(sc->sc_dev, "<%s> code=%08x, autorepeat=%d, "\ in gpiokeys_attach_key()
364 "repeat=%d, repeat_delay=%d\n", key_name, key->keycode, in gpiokeys_attach_key()
365 key->autorepeat, key->repeat, key->repeat_delay); in gpiokeys_attach_key()
376 if (key->intr_hl) in gpiokeys_detach_key()
377 bus_teardown_intr(sc->sc_dev, key->irq_res, key->intr_hl); in gpiokeys_detach_key()
378 if (key->irq_res) in gpiokeys_detach_key()
379 bus_release_resource(sc->sc_dev, SYS_RES_IRQ, in gpiokeys_detach_key()
380 key->irq_rid, key->irq_res); in gpiokeys_detach_key()
381 if (callout_pending(&key->repeat_callout)) in gpiokeys_detach_key()
382 callout_drain(&key->repeat_callout); in gpiokeys_detach_key()
383 if (callout_pending(&key->debounce_callout)) in gpiokeys_detach_key()
384 callout_drain(&key->debounce_callout); in gpiokeys_detach_key()
385 if (key->pin) in gpiokeys_detach_key()
386 gpio_pin_release(key->pin); in gpiokeys_detach_key()
394 if (!ofw_bus_is_compatible(dev, "gpio-keys")) in gpiokeys_probe()
414 if ((keys = ofw_bus_get_node(dev)) == -1) in gpiokeys_attach()
418 sc->sc_dev = dev; in gpiokeys_attach()
419 kbd = &sc->sc_kbd; in gpiokeys_attach()
425 kbd->kb_data = (void *)sc; in gpiokeys_attach()
426 sc->sc_mode = K_XLATE; in gpiokeys_attach()
428 sc->sc_keymap = key_map; in gpiokeys_attach()
429 sc->sc_accmap = accent_map; in gpiokeys_attach()
431 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap, in gpiokeys_attach()
432 sc->sc_fkeymap, GPIOKEYS_GLOBAL_NFKEY); in gpiokeys_attach()
461 sc->sc_evdev = evdev_alloc(); in gpiokeys_attach()
462 evdev_set_name(sc->sc_evdev, device_get_desc(dev)); in gpiokeys_attach()
465 evdev_set_phys(sc->sc_evdev, name != NULL ? name : "unknown"); in gpiokeys_attach()
468 evdev_set_id(sc->sc_evdev, BUS_VIRTUAL, 0, 0, 0); in gpiokeys_attach()
469 evdev_support_event(sc->sc_evdev, EV_SYN); in gpiokeys_attach()
470 evdev_support_event(sc->sc_evdev, EV_KEY); in gpiokeys_attach()
475 /* Traverse the 'gpio-keys' node and count keys */ in gpiokeys_attach()
483 sc->sc_keys = malloc(sizeof(struct gpiokey) * total_keys, in gpiokeys_attach()
486 sc->sc_total_keys = 0; in gpiokeys_attach()
487 /* Traverse the 'gpio-keys' node and count keys */ in gpiokeys_attach()
491 gpiokeys_attach_key(sc, child ,&sc->sc_keys[sc->sc_total_keys]); in gpiokeys_attach()
492 sc->sc_total_keys++; in gpiokeys_attach()
497 if (evdev_register_mtx(sc->sc_evdev, &sc->sc_mtx) != 0) { in gpiokeys_attach()
519 for (i = 0; i < sc->sc_total_keys; i++) in gpiokeys_detach()
520 gpiokeys_detach_key(sc, &sc->sc_keys[i]); in gpiokeys_detach()
531 evdev_free(sc->sc_evdev); in gpiokeys_detach()
535 if (sc->sc_keys) in gpiokeys_detach()
536 free(sc->sc_keys, M_DEVBUF); in gpiokeys_detach()
599 sc = kbd->kb_data; in gpiokeys_enable()
613 sc = kbd->kb_data; in gpiokeys_disable()
622 gpiokeys_do_poll(struct gpiokeys_softc *sc, uint8_t wait) in gpiokeys_do_poll() argument
625 KASSERT((sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) != 0, in gpiokeys_do_poll()
631 while (sc->sc_inputs == 0) { in gpiokeys_do_poll()
633 if (!wait) in gpiokeys_do_poll()
639 while ((sc->sc_inputs == 0) && wait) { in gpiokeys_do_poll()
648 struct gpiokeys_softc *sc = kbd->kb_data; in gpiokeys_check()
655 if (sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) in gpiokeys_check()
658 if (sc->sc_inputs > 0) { in gpiokeys_check()
678 struct gpiokeys_softc *sc = kbd->kb_data; in gpiokeys_check_char()
688 gpiokeys_get_key(struct gpiokeys_softc *sc, uint8_t wait) in gpiokeys_get_key() argument
693 || (sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) != 0, in gpiokeys_get_key()
698 if (sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) in gpiokeys_get_key()
699 gpiokeys_do_poll(sc, wait); in gpiokeys_get_key()
701 if (sc->sc_inputs == 0) { in gpiokeys_get_key()
702 c = -1; in gpiokeys_get_key()
704 c = sc->sc_input[sc->sc_inputhead]; in gpiokeys_get_key()
705 --(sc->sc_inputs); in gpiokeys_get_key()
706 ++(sc->sc_inputhead); in gpiokeys_get_key()
707 if (sc->sc_inputhead >= GPIOKEYS_GLOBAL_IN_BUF_SIZE) { in gpiokeys_get_key()
708 sc->sc_inputhead = 0; in gpiokeys_get_key()
717 gpiokeys_read(keyboard_t *kbd, int wait) in gpiokeys_read() argument
719 struct gpiokeys_softc *sc = kbd->kb_data; in gpiokeys_read()
723 return (-1); in gpiokeys_read()
726 keycode = gpiokeys_get_key(sc, (wait == FALSE) ? 0 : 1); in gpiokeys_read()
727 if (!KBD_IS_ACTIVE(kbd) || (keycode == -1)) in gpiokeys_read()
728 return (-1); in gpiokeys_read()
730 ++(kbd->kb_count); in gpiokeys_read()
737 gpiokeys_read_char_locked(keyboard_t *kbd, int wait) in gpiokeys_read_char_locked() argument
739 struct gpiokeys_softc *sc = kbd->kb_data; in gpiokeys_read_char_locked()
750 keycode = gpiokeys_get_key(sc, (wait == FALSE) ? 0 : 1); in gpiokeys_read_char_locked()
751 ++kbd->kb_count; in gpiokeys_read_char_locked()
754 if (sc->sc_mode == K_RAW) { in gpiokeys_read_char_locked()
761 if (sc->sc_mode == K_CODE) { in gpiokeys_read_char_locked()
768 &sc->sc_state, &sc->sc_accents); in gpiokeys_read_char_locked()
776 /* Currently wait is always false. */
778 gpiokeys_read_char(keyboard_t *kbd, int wait) in gpiokeys_read_char() argument
781 struct gpiokeys_softc *sc = kbd->kb_data; in gpiokeys_read_char()
784 keycode = gpiokeys_read_char_locked(kbd, wait); in gpiokeys_read_char()
794 struct gpiokeys_softc *sc = kbd->kb_data; in gpiokeys_ioctl_locked()
803 *(int *)arg = sc->sc_mode; in gpiokeys_ioctl_locked()
815 if (sc->sc_mode != K_XLATE) { in gpiokeys_ioctl_locked()
817 sc->sc_state &= ~LOCK_MASK; in gpiokeys_ioctl_locked()
818 sc->sc_state |= KBD_LED_VAL(kbd); in gpiokeys_ioctl_locked()
823 if (sc->sc_mode != *(int *)arg) { in gpiokeys_ioctl_locked()
824 if ((sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) == 0) in gpiokeys_ioctl_locked()
826 sc->sc_mode = *(int *)arg; in gpiokeys_ioctl_locked()
848 *(int *)arg = sc->sc_state & LOCK_MASK; in gpiokeys_ioctl_locked()
861 sc->sc_state &= ~LOCK_MASK; in gpiokeys_ioctl_locked()
862 sc->sc_state |= *(int *)arg; in gpiokeys_ioctl_locked()
877 kbd->kb_delay1 = 200; in gpiokeys_ioctl_locked()
879 kbd->kb_delay1 = ((int *)arg)[0]; in gpiokeys_ioctl_locked()
880 kbd->kb_delay2 = ((int *)arg)[1]; in gpiokeys_ioctl_locked()
904 sc->sc_accents = 0; in gpiokeys_ioctl_locked()
919 sc = kbd->kb_data; in gpiokeys_ioctl()
923 if (curthread->td_critnest != 0) in gpiokeys_ioctl()
937 struct gpiokeys_softc *sc = kbd->kb_data; in gpiokeys_clear_state()
939 sc->sc_flags &= ~(GPIOKEYS_GLOBAL_FLAG_POLLING); in gpiokeys_clear_state()
940 sc->sc_state &= LOCK_MASK; /* preserve locking key state */ in gpiokeys_clear_state()
941 sc->sc_accents = 0; in gpiokeys_clear_state()
948 return (len == 0) ? 1 : -1; in gpiokeys_get_state()
961 struct gpiokeys_softc *sc = kbd->kb_data; in gpiokeys_poll()
965 sc->sc_flags |= GPIOKEYS_GLOBAL_FLAG_POLLING; in gpiokeys_poll()
967 sc->sc_flags &= ~GPIOKEYS_GLOBAL_FLAG_POLLING; in gpiokeys_poll()
979 kbd->kb_delay1 = kbdelays[(code >> 5) & 3]; in gpiokeys_set_typematic()
980 kbd->kb_delay2 = kbrates[code & 0x1f]; in gpiokeys_set_typematic()
989 if ((sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) != 0) in gpiokeys_event_keyinput()
992 if (KBD_IS_ACTIVE(&sc->sc_kbd) && in gpiokeys_event_keyinput()
993 KBD_IS_BUSY(&sc->sc_kbd)) { in gpiokeys_event_keyinput()
995 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT, in gpiokeys_event_keyinput()
996 sc->sc_kbd.kb_callback.kc_arg); in gpiokeys_event_keyinput()
1000 c = gpiokeys_read_char(&sc->sc_kbd, 0); in gpiokeys_event_keyinput()