xref: /freebsd/sys/dev/gpio/gpiokeys.c (revision 61e21613)
1 /*-
2  * Copyright (c) 2015-2016 Oleksandr Tymoshenko <gonzo@freebsd.org>
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 #include <sys/cdefs.h>
28 #include "opt_platform.h"
29 #include "opt_kbd.h"
30 #include "opt_evdev.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/gpio.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/proc.h>
42 #include <sys/kdb.h>
43 
44 #include <sys/ioccom.h>
45 #include <sys/filio.h>
46 #include <sys/kbio.h>
47 
48 #include <dev/kbd/kbdreg.h>
49 #include <dev/kbd/kbdtables.h>
50 
51 #include <dev/fdt/fdt_common.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
54 
55 #include <dev/gpio/gpiobusvar.h>
56 #include <dev/gpio/gpiokeys.h>
57 
58 #ifdef EVDEV_SUPPORT
59 #include <dev/evdev/evdev.h>
60 #include <dev/evdev/input.h>
61 #endif
62 
63 #define	KBD_DRIVER_NAME	"gpiokeys"
64 
65 #define	GPIOKEYS_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
66 #define	GPIOKEYS_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
67 #define	GPIOKEYS_LOCK_INIT(_sc) \
68 	mtx_init(&_sc->sc_mtx, device_get_nameunit((_sc)->sc_dev), \
69 	    "gpiokeys", MTX_DEF)
70 #define	GPIOKEYS_LOCK_DESTROY(_sc)	mtx_destroy(&(_sc)->sc_mtx);
71 #define	GPIOKEYS_ASSERT_LOCKED(_sc)	mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
72 
73 #define	GPIOKEY_LOCK(_key)		mtx_lock(&(_key)->mtx)
74 #define	GPIOKEY_UNLOCK(_key)		mtx_unlock(&(_key)->mtx)
75 #define	GPIOKEY_LOCK_INIT(_key) \
76 	mtx_init(&(_key)->mtx, "gpiokey", "gpiokey", MTX_DEF)
77 #define	GPIOKEY_LOCK_DESTROY(_key)	mtx_destroy(&(_key)->mtx);
78 
79 #define	KEY_PRESS	  0
80 #define	KEY_RELEASE	  0x80
81 
82 #define	SCAN_PRESS	  0
83 #define	SCAN_RELEASE	  0x80
84 #define	SCAN_CHAR(c)	((c) & 0x7f)
85 
86 #define	GPIOKEYS_GLOBAL_NMOD                     8	/* units */
87 #define	GPIOKEYS_GLOBAL_NKEYCODE                 6	/* units */
88 #define	GPIOKEYS_GLOBAL_IN_BUF_SIZE  (2*(GPIOKEYS_GLOBAL_NMOD + (2*GPIOKEYS_GLOBAL_NKEYCODE)))	/* bytes */
89 #define	GPIOKEYS_GLOBAL_IN_BUF_FULL  (GPIOKEYS_GLOBAL_IN_BUF_SIZE / 2)	/* bytes */
90 #define	GPIOKEYS_GLOBAL_NFKEY        (sizeof(fkey_tab)/sizeof(fkey_tab[0]))	/* units */
91 #define	GPIOKEYS_GLOBAL_BUFFER_SIZE	      64	/* bytes */
92 
93 #define	AUTOREPEAT_DELAY	250
94 #define	AUTOREPEAT_REPEAT	34
95 
96 struct gpiokeys_softc;
97 
98 struct gpiokey
99 {
100 	struct gpiokeys_softc	*parent_sc;
101 	gpio_pin_t		pin;
102 	int			irq_rid;
103 	struct resource		*irq_res;
104 	void			*intr_hl;
105 	struct mtx		mtx;
106 #ifdef EVDEV_SUPPORT
107 	uint32_t		evcode;
108 #endif
109 	uint32_t		keycode;
110 	int			autorepeat;
111 	struct callout		debounce_callout;
112 	struct callout		repeat_callout;
113 	int			repeat_delay;
114 	int			repeat;
115 	int			debounce_interval;
116 };
117 
118 struct gpiokeys_softc
119 {
120 	device_t	sc_dev;
121 	struct mtx	sc_mtx;
122 	struct gpiokey	*sc_keys;
123 	int		sc_total_keys;
124 
125 #ifdef EVDEV_SUPPORT
126 	struct evdev_dev	*sc_evdev;
127 #endif
128 	keyboard_t	sc_kbd;
129 	keymap_t	sc_keymap;
130 	accentmap_t	sc_accmap;
131 	fkeytab_t	sc_fkeymap[GPIOKEYS_GLOBAL_NFKEY];
132 
133 	uint32_t	sc_input[GPIOKEYS_GLOBAL_IN_BUF_SIZE];	/* input buffer */
134 	uint32_t	sc_time_ms;
135 #define	GPIOKEYS_GLOBAL_FLAG_POLLING	0x00000002
136 
137 	uint32_t	sc_flags;		/* flags */
138 
139 	int		sc_mode;		/* input mode (K_XLATE,K_RAW,K_CODE) */
140 	int		sc_state;		/* shift/lock key state */
141 	int		sc_accents;		/* accent key index (> 0) */
142 	int		sc_kbd_size;
143 
144 	uint16_t	sc_inputs;
145 	uint16_t	sc_inputhead;
146 	uint16_t	sc_inputtail;
147 
148 	uint8_t		sc_kbd_id;
149 };
150 
151 /* gpio-keys device */
152 static int gpiokeys_probe(device_t);
153 static int gpiokeys_attach(device_t);
154 static int gpiokeys_detach(device_t);
155 
156 /* kbd methods prototypes */
157 static int	gpiokeys_set_typematic(keyboard_t *, int);
158 static uint32_t	gpiokeys_read_char(keyboard_t *, int);
159 static void	gpiokeys_clear_state(keyboard_t *);
160 static int	gpiokeys_ioctl(keyboard_t *, u_long, caddr_t);
161 static int	gpiokeys_enable(keyboard_t *);
162 static int	gpiokeys_disable(keyboard_t *);
163 static void	gpiokeys_event_keyinput(struct gpiokeys_softc *);
164 
165 static void
166 gpiokeys_put_key(struct gpiokeys_softc *sc, uint32_t key)
167 {
168 
169 	GPIOKEYS_ASSERT_LOCKED(sc);
170 
171 	if (sc->sc_inputs < GPIOKEYS_GLOBAL_IN_BUF_SIZE) {
172 		sc->sc_input[sc->sc_inputtail] = key;
173 		++(sc->sc_inputs);
174 		++(sc->sc_inputtail);
175 		if (sc->sc_inputtail >= GPIOKEYS_GLOBAL_IN_BUF_SIZE) {
176 			sc->sc_inputtail = 0;
177 		}
178 	} else {
179 		device_printf(sc->sc_dev, "input buffer is full\n");
180 	}
181 }
182 
183 static void
184 gpiokeys_key_event(struct gpiokeys_softc *sc, struct gpiokey *key, int pressed)
185 {
186 	uint32_t code;
187 
188 	GPIOKEYS_LOCK(sc);
189 #ifdef EVDEV_SUPPORT
190 	if (key->evcode != GPIOKEY_NONE &&
191 	    (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD) != 0) {
192 		evdev_push_key(sc->sc_evdev, key->evcode, pressed);
193 		evdev_sync(sc->sc_evdev);
194 	}
195 	if (evdev_is_grabbed(sc->sc_evdev)) {
196 		GPIOKEYS_UNLOCK(sc);
197 		return;
198 	}
199 #endif
200 	if (key->keycode != GPIOKEY_NONE) {
201 		code = key->keycode & SCAN_KEYCODE_MASK;
202 		if (!pressed)
203 			code |= KEY_RELEASE;
204 
205 		if (key->keycode & SCAN_PREFIX_E0)
206 			gpiokeys_put_key(sc, 0xe0);
207 		else if (key->keycode & SCAN_PREFIX_E1)
208 			gpiokeys_put_key(sc, 0xe1);
209 
210 		gpiokeys_put_key(sc, code);
211 	}
212 	GPIOKEYS_UNLOCK(sc);
213 
214 	if (key->keycode != GPIOKEY_NONE)
215 		gpiokeys_event_keyinput(sc);
216 }
217 
218 static void
219 gpiokey_autorepeat(void *arg)
220 {
221 	struct gpiokey *key;
222 
223 	key = arg;
224 
225 	gpiokeys_key_event(key->parent_sc, key, 1);
226 
227 	callout_reset(&key->repeat_callout, key->repeat,
228 		    gpiokey_autorepeat, key);
229 }
230 
231 static void
232 gpiokey_debounced_intr(void *arg)
233 {
234 	struct gpiokey *key;
235 	bool active;
236 
237 	key = arg;
238 
239 	gpio_pin_is_active(key->pin, &active);
240 	if (active) {
241 		gpiokeys_key_event(key->parent_sc, key, 1);
242 		if (key->autorepeat) {
243 			callout_reset(&key->repeat_callout, key->repeat_delay,
244 			    gpiokey_autorepeat, key);
245 		}
246 	}
247 	else {
248 		if (key->autorepeat &&
249 		    callout_pending(&key->repeat_callout))
250 			callout_stop(&key->repeat_callout);
251 		gpiokeys_key_event(key->parent_sc, key, 0);
252 	}
253 }
254 
255 static void
256 gpiokey_intr(void *arg)
257 {
258 	struct gpiokey *key;
259 	int debounce_ticks;
260 
261 	key = arg;
262 
263 	GPIOKEY_LOCK(key);
264 	debounce_ticks = (hz * key->debounce_interval) / 1000;
265 	if (debounce_ticks == 0)
266 		debounce_ticks = 1;
267 	if (!callout_pending(&key->debounce_callout))
268 		callout_reset(&key->debounce_callout, debounce_ticks,
269 		    gpiokey_debounced_intr, key);
270 	GPIOKEY_UNLOCK(key);
271 }
272 
273 static void
274 gpiokeys_attach_key(struct gpiokeys_softc *sc, phandle_t node,
275     struct gpiokey *key)
276 {
277 	pcell_t prop;
278 	char *name;
279 	uint32_t code;
280 	int err;
281 	const char *key_name;
282 
283 	GPIOKEY_LOCK_INIT(key);
284 	key->parent_sc = sc;
285 	callout_init_mtx(&key->debounce_callout, &key->mtx, 0);
286 	callout_init_mtx(&key->repeat_callout, &key->mtx, 0);
287 
288 	name = NULL;
289 	if (OF_getprop_alloc(node, "label", (void **)&name) == -1)
290 		OF_getprop_alloc(node, "name", (void **)&name);
291 
292 	if (name != NULL)
293 		key_name = name;
294 	else
295 		key_name = "unknown";
296 
297 	key->autorepeat = OF_hasprop(node, "autorepeat");
298 
299 	key->repeat_delay = (hz * AUTOREPEAT_DELAY) / 1000;
300 	if (key->repeat_delay == 0)
301 		key->repeat_delay = 1;
302 
303 	key->repeat = (hz * AUTOREPEAT_REPEAT) / 1000;
304 	if (key->repeat == 0)
305 		key->repeat = 1;
306 
307 	if ((OF_getprop(node, "debounce-interval", &prop, sizeof(prop))) > 0)
308 		key->debounce_interval = fdt32_to_cpu(prop);
309 	else
310 		key->debounce_interval = 5;
311 
312 	if ((OF_getprop(node, "freebsd,code", &prop, sizeof(prop))) > 0)
313 		key->keycode = fdt32_to_cpu(prop);
314 	else if ((OF_getprop(node, "linux,code", &prop, sizeof(prop))) > 0) {
315 		code = fdt32_to_cpu(prop);
316 		key->keycode = gpiokey_map_linux_code(code);
317 		if (key->keycode == GPIOKEY_NONE)
318 			device_printf(sc->sc_dev, "<%s> failed to map linux,code value 0x%x\n",
319 			    key_name, code);
320 #ifdef EVDEV_SUPPORT
321 		key->evcode = code;
322 		evdev_support_key(sc->sc_evdev, code);
323 #endif
324 	}
325 	else
326 		device_printf(sc->sc_dev, "<%s> no linux,code or freebsd,code property\n",
327 		    key_name);
328 
329 	err = gpio_pin_get_by_ofw_idx(sc->sc_dev, node, 0, &key->pin);
330 	if (err) {
331 		device_printf(sc->sc_dev, "<%s> failed to map pin\n", key_name);
332 		if (name)
333 			OF_prop_free(name);
334 		return;
335 	}
336 
337 	key->irq_res = gpio_alloc_intr_resource(sc->sc_dev, &key->irq_rid,
338 	    RF_ACTIVE, key->pin, GPIO_INTR_EDGE_BOTH);
339 	if (!key->irq_res) {
340 		device_printf(sc->sc_dev, "<%s> cannot allocate interrupt\n", key_name);
341 		gpio_pin_release(key->pin);
342 		key->pin = NULL;
343 		if (name)
344 			OF_prop_free(name);
345 		return;
346 	}
347 
348 	if (bus_setup_intr(sc->sc_dev, key->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
349 			NULL, gpiokey_intr, key,
350 			&key->intr_hl) != 0) {
351 		device_printf(sc->sc_dev, "<%s> unable to setup the irq handler\n", key_name);
352 		bus_release_resource(sc->sc_dev, SYS_RES_IRQ, key->irq_rid,
353 		    key->irq_res);
354 		gpio_pin_release(key->pin);
355 		key->pin = NULL;
356 		key->irq_res = NULL;
357 		if (name)
358 			OF_prop_free(name);
359 		return;
360 	}
361 
362 	if (bootverbose)
363 		device_printf(sc->sc_dev, "<%s> code=%08x, autorepeat=%d, "\
364 		    "repeat=%d, repeat_delay=%d\n", key_name, key->keycode,
365 		    key->autorepeat, key->repeat, key->repeat_delay);
366 
367 	if (name)
368 		OF_prop_free(name);
369 }
370 
371 static void
372 gpiokeys_detach_key(struct gpiokeys_softc *sc, struct gpiokey *key)
373 {
374 
375 	GPIOKEY_LOCK(key);
376 	if (key->intr_hl)
377 		bus_teardown_intr(sc->sc_dev, key->irq_res, key->intr_hl);
378 	if (key->irq_res)
379 		bus_release_resource(sc->sc_dev, SYS_RES_IRQ,
380 		    key->irq_rid, key->irq_res);
381 	if (callout_pending(&key->repeat_callout))
382 		callout_drain(&key->repeat_callout);
383 	if (callout_pending(&key->debounce_callout))
384 		callout_drain(&key->debounce_callout);
385 	if (key->pin)
386 		gpio_pin_release(key->pin);
387 	GPIOKEY_UNLOCK(key);
388 	GPIOKEY_LOCK_DESTROY(key);
389 }
390 
391 static int
392 gpiokeys_probe(device_t dev)
393 {
394 	if (!ofw_bus_is_compatible(dev, "gpio-keys"))
395 		return (ENXIO);
396 
397 	device_set_desc(dev, "GPIO keyboard");
398 
399 	return (0);
400 }
401 
402 static int
403 gpiokeys_attach(device_t dev)
404 {
405 	struct gpiokeys_softc *sc;
406 	keyboard_t *kbd;
407 #ifdef EVDEV_SUPPORT
408 	char *name;
409 #endif
410 	phandle_t keys, child;
411 	int total_keys;
412 	int unit;
413 
414 	if ((keys = ofw_bus_get_node(dev)) == -1)
415 		return (ENXIO);
416 
417 	sc = device_get_softc(dev);
418 	sc->sc_dev = dev;
419 	kbd = &sc->sc_kbd;
420 
421 	GPIOKEYS_LOCK_INIT(sc);
422 	unit = device_get_unit(dev);
423 	kbd_init_struct(kbd, KBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
424 
425 	kbd->kb_data = (void *)sc;
426 	sc->sc_mode = K_XLATE;
427 
428 	sc->sc_keymap = key_map;
429 	sc->sc_accmap = accent_map;
430 
431 	kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
432 	    sc->sc_fkeymap, GPIOKEYS_GLOBAL_NFKEY);
433 
434 	KBD_FOUND_DEVICE(kbd);
435 
436 	gpiokeys_clear_state(kbd);
437 
438 	KBD_PROBE_DONE(kbd);
439 
440 	KBD_INIT_DONE(kbd);
441 
442 	if (kbd_register(kbd) < 0) {
443 		goto detach;
444 	}
445 
446 	KBD_CONFIG_DONE(kbd);
447 
448 	gpiokeys_enable(kbd);
449 
450 #ifdef KBD_INSTALL_CDEV
451 	if (kbd_attach(kbd)) {
452 		goto detach;
453 	}
454 #endif
455 
456 	if (bootverbose) {
457 		kbdd_diag(kbd, 1);
458 	}
459 
460 #ifdef EVDEV_SUPPORT
461 	sc->sc_evdev = evdev_alloc();
462 	evdev_set_name(sc->sc_evdev, device_get_desc(dev));
463 
464 	OF_getprop_alloc(keys, "name", (void **)&name);
465 	evdev_set_phys(sc->sc_evdev, name != NULL ? name : "unknown");
466 	OF_prop_free(name);
467 
468 	evdev_set_id(sc->sc_evdev, BUS_VIRTUAL, 0, 0, 0);
469 	evdev_support_event(sc->sc_evdev, EV_SYN);
470 	evdev_support_event(sc->sc_evdev, EV_KEY);
471 #endif
472 
473 	total_keys = 0;
474 
475 	/* Traverse the 'gpio-keys' node and count keys */
476 	for (child = OF_child(keys); child != 0; child = OF_peer(child)) {
477 		if (!OF_hasprop(child, "gpios"))
478 			continue;
479 		total_keys++;
480 	}
481 
482 	if (total_keys) {
483 		sc->sc_keys =  malloc(sizeof(struct gpiokey) * total_keys,
484 		    M_DEVBUF, M_WAITOK | M_ZERO);
485 
486 		sc->sc_total_keys = 0;
487 		/* Traverse the 'gpio-keys' node and count keys */
488 		for (child = OF_child(keys); child != 0; child = OF_peer(child)) {
489 			if (!OF_hasprop(child, "gpios"))
490 				continue;
491 			gpiokeys_attach_key(sc, child ,&sc->sc_keys[sc->sc_total_keys]);
492 			sc->sc_total_keys++;
493 		}
494 	}
495 
496 #ifdef EVDEV_SUPPORT
497 	if (evdev_register_mtx(sc->sc_evdev, &sc->sc_mtx) != 0) {
498 		device_printf(dev, "failed to register evdev device\n");
499 		goto detach;
500 	}
501 #endif
502 
503 	return (0);
504 
505 detach:
506 	gpiokeys_detach(dev);
507 	return (ENXIO);
508 }
509 
510 static int
511 gpiokeys_detach(device_t dev)
512 {
513 	struct gpiokeys_softc *sc;
514 	keyboard_t *kbd;
515 	int i;
516 
517 	sc = device_get_softc(dev);
518 
519 	for (i = 0; i < sc->sc_total_keys; i++)
520 		gpiokeys_detach_key(sc, &sc->sc_keys[i]);
521 
522 	kbd = kbd_get_keyboard(kbd_find_keyboard(KBD_DRIVER_NAME,
523 	    device_get_unit(dev)));
524 
525 #ifdef KBD_INSTALL_CDEV
526 	kbd_detach(kbd);
527 #endif
528 	kbd_unregister(kbd);
529 
530 #ifdef EVDEV_SUPPORT
531 	evdev_free(sc->sc_evdev);
532 #endif
533 
534 	GPIOKEYS_LOCK_DESTROY(sc);
535 	if (sc->sc_keys)
536 		free(sc->sc_keys, M_DEVBUF);
537 
538 	return (0);
539 }
540 
541 /* early keyboard probe, not supported */
542 static int
543 gpiokeys_configure(int flags)
544 {
545 	return (0);
546 }
547 
548 /* detect a keyboard, not used */
549 static int
550 gpiokeys__probe(int unit, void *arg, int flags)
551 {
552 	return (ENXIO);
553 }
554 
555 /* reset and initialize the device, not used */
556 static int
557 gpiokeys_init(int unit, keyboard_t **kbdp, void *arg, int flags)
558 {
559 	return (ENXIO);
560 }
561 
562 /* test the interface to the device, not used */
563 static int
564 gpiokeys_test_if(keyboard_t *kbd)
565 {
566 	return (0);
567 }
568 
569 /* finish using this keyboard, not used */
570 static int
571 gpiokeys_term(keyboard_t *kbd)
572 {
573 	return (ENXIO);
574 }
575 
576 /* keyboard interrupt routine, not used */
577 static int
578 gpiokeys_intr(keyboard_t *kbd, void *arg)
579 {
580 	return (0);
581 }
582 
583 /* lock the access to the keyboard, not used */
584 static int
585 gpiokeys_lock(keyboard_t *kbd, int lock)
586 {
587 	return (1);
588 }
589 
590 /*
591  * Enable the access to the device; until this function is called,
592  * the client cannot read from the keyboard.
593  */
594 static int
595 gpiokeys_enable(keyboard_t *kbd)
596 {
597 	struct gpiokeys_softc *sc;
598 
599 	sc = kbd->kb_data;
600 	GPIOKEYS_LOCK(sc);
601 	KBD_ACTIVATE(kbd);
602 	GPIOKEYS_UNLOCK(sc);
603 
604 	return (0);
605 }
606 
607 /* disallow the access to the device */
608 static int
609 gpiokeys_disable(keyboard_t *kbd)
610 {
611 	struct gpiokeys_softc *sc;
612 
613 	sc = kbd->kb_data;
614 	GPIOKEYS_LOCK(sc);
615 	KBD_DEACTIVATE(kbd);
616 	GPIOKEYS_UNLOCK(sc);
617 
618 	return (0);
619 }
620 
621 static void
622 gpiokeys_do_poll(struct gpiokeys_softc *sc, uint8_t wait)
623 {
624 
625 	KASSERT((sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) != 0,
626 	    ("gpiokeys_do_poll called when not polling\n"));
627 
628 	GPIOKEYS_ASSERT_LOCKED(sc);
629 
630 	if (!kdb_active && !SCHEDULER_STOPPED()) {
631 		while (sc->sc_inputs == 0) {
632 			kern_yield(PRI_UNCHANGED);
633 			if (!wait)
634 				break;
635 		}
636 		return;
637 	}
638 
639 	while ((sc->sc_inputs == 0) && wait) {
640 		printf("POLL!\n");
641 	}
642 }
643 
644 /* check if data is waiting */
645 static int
646 gpiokeys_check(keyboard_t *kbd)
647 {
648 	struct gpiokeys_softc *sc = kbd->kb_data;
649 
650 	GPIOKEYS_ASSERT_LOCKED(sc);
651 
652 	if (!KBD_IS_ACTIVE(kbd))
653 		return (0);
654 
655 	if (sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING)
656 		gpiokeys_do_poll(sc, 0);
657 
658 	if (sc->sc_inputs > 0) {
659 		return (1);
660 	}
661 	return (0);
662 }
663 
664 /* check if char is waiting */
665 static int
666 gpiokeys_check_char_locked(keyboard_t *kbd)
667 {
668 	if (!KBD_IS_ACTIVE(kbd))
669 		return (0);
670 
671 	return (gpiokeys_check(kbd));
672 }
673 
674 static int
675 gpiokeys_check_char(keyboard_t *kbd)
676 {
677 	int result;
678 	struct gpiokeys_softc *sc = kbd->kb_data;
679 
680 	GPIOKEYS_LOCK(sc);
681 	result = gpiokeys_check_char_locked(kbd);
682 	GPIOKEYS_UNLOCK(sc);
683 
684 	return (result);
685 }
686 
687 static int32_t
688 gpiokeys_get_key(struct gpiokeys_softc *sc, uint8_t wait)
689 {
690 	int32_t c;
691 
692 	KASSERT((!kdb_active && !SCHEDULER_STOPPED())
693 	    || (sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) != 0,
694 	    ("not polling in kdb or panic\n"));
695 
696 	GPIOKEYS_ASSERT_LOCKED(sc);
697 
698 	if (sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING)
699 		gpiokeys_do_poll(sc, wait);
700 
701 	if (sc->sc_inputs == 0) {
702 		c = -1;
703 	} else {
704 		c = sc->sc_input[sc->sc_inputhead];
705 		--(sc->sc_inputs);
706 		++(sc->sc_inputhead);
707 		if (sc->sc_inputhead >= GPIOKEYS_GLOBAL_IN_BUF_SIZE) {
708 			sc->sc_inputhead = 0;
709 		}
710 	}
711 
712 	return (c);
713 }
714 
715 /* read one byte from the keyboard if it's allowed */
716 static int
717 gpiokeys_read(keyboard_t *kbd, int wait)
718 {
719 	struct gpiokeys_softc *sc = kbd->kb_data;
720 	int32_t keycode;
721 
722 	if (!KBD_IS_ACTIVE(kbd))
723 		return (-1);
724 
725 	/* XXX */
726 	keycode = gpiokeys_get_key(sc, (wait == FALSE) ? 0 : 1);
727 	if (!KBD_IS_ACTIVE(kbd) || (keycode == -1))
728 		return (-1);
729 
730 	++(kbd->kb_count);
731 
732 	return (keycode);
733 }
734 
735 /* read char from the keyboard */
736 static uint32_t
737 gpiokeys_read_char_locked(keyboard_t *kbd, int wait)
738 {
739 	struct gpiokeys_softc *sc = kbd->kb_data;
740 	uint32_t action;
741 	uint32_t keycode;
742 
743 	if (!KBD_IS_ACTIVE(kbd))
744 		return (NOKEY);
745 
746 next_code:
747 
748 	/* see if there is something in the keyboard port */
749 	/* XXX */
750 	keycode = gpiokeys_get_key(sc, (wait == FALSE) ? 0 : 1);
751 	++kbd->kb_count;
752 
753 	/* return the byte as is for the K_RAW mode */
754 	if (sc->sc_mode == K_RAW) {
755 		return (keycode);
756 	}
757 
758 	/* return the key code in the K_CODE mode */
759 	/* XXX: keycode |= SCAN_RELEASE; */
760 
761 	if (sc->sc_mode == K_CODE) {
762 		return (keycode);
763 	}
764 
765 	/* keycode to key action */
766 	action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
767 	    (keycode & SCAN_RELEASE),
768 	    &sc->sc_state, &sc->sc_accents);
769 	if (action == NOKEY) {
770 		goto next_code;
771 	}
772 
773 	return (action);
774 }
775 
776 /* Currently wait is always false. */
777 static uint32_t
778 gpiokeys_read_char(keyboard_t *kbd, int wait)
779 {
780 	uint32_t keycode;
781 	struct gpiokeys_softc *sc = kbd->kb_data;
782 
783 	GPIOKEYS_LOCK(sc);
784 	keycode = gpiokeys_read_char_locked(kbd, wait);
785 	GPIOKEYS_UNLOCK(sc);
786 
787 	return (keycode);
788 }
789 
790 /* some useful control functions */
791 static int
792 gpiokeys_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
793 {
794 	struct gpiokeys_softc *sc = kbd->kb_data;
795 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
796     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
797 	int ival;
798 
799 #endif
800 
801 	switch (cmd) {
802 	case KDGKBMODE:		/* get keyboard mode */
803 		*(int *)arg = sc->sc_mode;
804 		break;
805 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
806     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
807 	case _IO('K', 7):
808 		ival = IOCPARM_IVAL(arg);
809 		arg = (caddr_t)&ival;
810 		/* FALLTHROUGH */
811 #endif
812 	case KDSKBMODE:		/* set keyboard mode */
813 		switch (*(int *)arg) {
814 		case K_XLATE:
815 			if (sc->sc_mode != K_XLATE) {
816 				/* make lock key state and LED state match */
817 				sc->sc_state &= ~LOCK_MASK;
818 				sc->sc_state |= KBD_LED_VAL(kbd);
819 			}
820 			/* FALLTHROUGH */
821 		case K_RAW:
822 		case K_CODE:
823 			if (sc->sc_mode != *(int *)arg) {
824 				if ((sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) == 0)
825 					gpiokeys_clear_state(kbd);
826 				sc->sc_mode = *(int *)arg;
827 			}
828 			break;
829 		default:
830 			return (EINVAL);
831 		}
832 		break;
833 
834 	case KDGETLED:			/* get keyboard LED */
835 		*(int *)arg = KBD_LED_VAL(kbd);
836 		break;
837 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
838     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
839 	case _IO('K', 66):
840 		ival = IOCPARM_IVAL(arg);
841 		arg = (caddr_t)&ival;
842 		/* FALLTHROUGH */
843 #endif
844 	case KDSETLED:			/* set keyboard LED */
845 		KBD_LED_VAL(kbd) = *(int *)arg;
846 		break;
847 	case KDGKBSTATE:		/* get lock key state */
848 		*(int *)arg = sc->sc_state & LOCK_MASK;
849 		break;
850 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
851     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
852 	case _IO('K', 20):
853 		ival = IOCPARM_IVAL(arg);
854 		arg = (caddr_t)&ival;
855 		/* FALLTHROUGH */
856 #endif
857 	case KDSKBSTATE:		/* set lock key state */
858 		if (*(int *)arg & ~LOCK_MASK) {
859 			return (EINVAL);
860 		}
861 		sc->sc_state &= ~LOCK_MASK;
862 		sc->sc_state |= *(int *)arg;
863 		return (0);
864 
865 	case KDSETREPEAT:		/* set keyboard repeat rate (new
866 					 * interface) */
867 		if (!KBD_HAS_DEVICE(kbd)) {
868 			return (0);
869 		}
870 		if (((int *)arg)[1] < 0) {
871 			return (EINVAL);
872 		}
873 		if (((int *)arg)[0] < 0) {
874 			return (EINVAL);
875 		}
876 		if (((int *)arg)[0] < 200)	/* fastest possible value */
877 			kbd->kb_delay1 = 200;
878 		else
879 			kbd->kb_delay1 = ((int *)arg)[0];
880 		kbd->kb_delay2 = ((int *)arg)[1];
881 		return (0);
882 
883 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
884     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
885 	case _IO('K', 67):
886 		ival = IOCPARM_IVAL(arg);
887 		arg = (caddr_t)&ival;
888 		/* FALLTHROUGH */
889 #endif
890 	case KDSETRAD:			/* set keyboard repeat rate (old
891 					 * interface) */
892 		return (gpiokeys_set_typematic(kbd, *(int *)arg));
893 
894 	case PIO_KEYMAP:		/* set keyboard translation table */
895 	case PIO_KEYMAPENT:		/* set keyboard translation table
896 					 * entry */
897 	case PIO_DEADKEYMAP:		/* set accent key translation table */
898 #ifdef COMPAT_FREEBSD13
899 	case OPIO_KEYMAP:		/* set keyboard translation table
900 					 * (compat) */
901 	case OPIO_DEADKEYMAP:		/* set accent key translation table
902 					 * (compat) */
903 #endif /* COMPAT_FREEBSD13 */
904 		sc->sc_accents = 0;
905 		/* FALLTHROUGH */
906 	default:
907 		return (genkbd_commonioctl(kbd, cmd, arg));
908 	}
909 
910 	return (0);
911 }
912 
913 static int
914 gpiokeys_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
915 {
916 	int result;
917 	struct gpiokeys_softc *sc;
918 
919 	sc = kbd->kb_data;
920 	/*
921 	 * XXX Check if someone is calling us from a critical section:
922 	 */
923 	if (curthread->td_critnest != 0)
924 		return (EDEADLK);
925 
926 	GPIOKEYS_LOCK(sc);
927 	result = gpiokeys_ioctl_locked(kbd, cmd, arg);
928 	GPIOKEYS_UNLOCK(sc);
929 
930 	return (result);
931 }
932 
933 /* clear the internal state of the keyboard */
934 static void
935 gpiokeys_clear_state(keyboard_t *kbd)
936 {
937 	struct gpiokeys_softc *sc = kbd->kb_data;
938 
939 	sc->sc_flags &= ~(GPIOKEYS_GLOBAL_FLAG_POLLING);
940 	sc->sc_state &= LOCK_MASK;	/* preserve locking key state */
941 	sc->sc_accents = 0;
942 }
943 
944 /* get the internal state, not used */
945 static int
946 gpiokeys_get_state(keyboard_t *kbd, void *buf, size_t len)
947 {
948 	return (len == 0) ? 1 : -1;
949 }
950 
951 /* set the internal state, not used */
952 static int
953 gpiokeys_set_state(keyboard_t *kbd, void *buf, size_t len)
954 {
955 	return (EINVAL);
956 }
957 
958 static int
959 gpiokeys_poll(keyboard_t *kbd, int on)
960 {
961 	struct gpiokeys_softc *sc = kbd->kb_data;
962 
963 	GPIOKEYS_LOCK(sc);
964 	if (on)
965 		sc->sc_flags |= GPIOKEYS_GLOBAL_FLAG_POLLING;
966 	else
967 		sc->sc_flags &= ~GPIOKEYS_GLOBAL_FLAG_POLLING;
968 	GPIOKEYS_UNLOCK(sc);
969 
970 	return (0);
971 }
972 
973 static int
974 gpiokeys_set_typematic(keyboard_t *kbd, int code)
975 {
976 	if (code & ~0x7f) {
977 		return (EINVAL);
978 	}
979 	kbd->kb_delay1 = kbdelays[(code >> 5) & 3];
980 	kbd->kb_delay2 = kbrates[code & 0x1f];
981 	return (0);
982 }
983 
984 static void
985 gpiokeys_event_keyinput(struct gpiokeys_softc *sc)
986 {
987 	int c;
988 
989 	if ((sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) != 0)
990 		return;
991 
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);
997 	} else {
998 		/* read and discard the input, no one is waiting for it */
999 		do {
1000 			c = gpiokeys_read_char(&sc->sc_kbd, 0);
1001 		} while (c != NOKEY);
1002 	}
1003 }
1004 
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,
1023 };
1024 
1025 KEYBOARD_DRIVER(gpiokeys, gpiokeyssw, gpiokeys_configure);
1026 
1027 static int
1028 gpiokeys_driver_load(module_t mod, int what, void *arg)
1029 {
1030 	switch (what) {
1031 	case MOD_LOAD:
1032 		kbd_add_driver(&gpiokeys_kbd_driver);
1033 		break;
1034 	case MOD_UNLOAD:
1035 		kbd_delete_driver(&gpiokeys_kbd_driver);
1036 		break;
1037 	}
1038 	return (0);
1039 }
1040 
1041 static device_method_t gpiokeys_methods[] = {
1042 	DEVMETHOD(device_probe,		gpiokeys_probe),
1043 	DEVMETHOD(device_attach,	gpiokeys_attach),
1044 	DEVMETHOD(device_detach,	gpiokeys_detach),
1045 
1046 	DEVMETHOD_END
1047 };
1048 
1049 static driver_t gpiokeys_driver = {
1050 	"gpiokeys",
1051 	gpiokeys_methods,
1052 	sizeof(struct gpiokeys_softc),
1053 };
1054 
1055 DRIVER_MODULE(gpiokeys, simplebus, gpiokeys_driver, gpiokeys_driver_load, NULL);
1056 MODULE_VERSION(gpiokeys, 1);
1057