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