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