The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/ic/matrixkp_subr.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 2005 Jesse Off.  All rights reserved.
    3  *
    4  * Redistribution and use in source and binary forms, with or without
    5  * modification, are permitted provided that the following conditions
    6  * are met:
    7  * 1. Redistributions of source code must retain the above copyright
    8  *    notice, this list of conditions and the following disclaimer.
    9  * 2. Redistributions in binary form must reproduce the above copyright
   10  *    notice, this list of conditions and the following disclaimer in the
   11  *    documentation and/or other materials provided with the distribution.
   12  *
   13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   23  * SUCH DAMAGE.
   24  *
   25  *
   26  * The matrix keypad is a primitive type of keying device
   27  * commonly used in systems as a small, cheap, easy-to-build and rugged
   28  * way to get user input in a variety of embedded environments.  This
   29  * driver can work for any size of keypad.  A one key keypad (aka
   30  * button) can also be used.  The theory of operation is described
   31  * thusly:
   32  *
   33  *      1) The keypad is connected to the NetBSD embedded system
   34  *      with digital I/O (DIO) pins connected to each column of
   35  *      the keypad and also to each row of the keypad.
   36  *
   37  *      2) When a button is pressed, a short is made between a
   38  *      column line and the intersecting row line.
   39  *
   40  *      3) Software is responsible to poll each row/column individually
   41  *      and also to debounce any key presses.
   42  *
   43  * To correctly wire up such a thing requires the input DIO
   44  * lines to have pull-up resistors, otherwise an input may be read as a random
   45  * value if not currently being shorted by a button press.
   46  */
   47 
   48 #include <sys/cdefs.h>
   49 __KERNEL_RCSID(0, "$NetBSD: matrixkp_subr.c,v 1.4 2005/12/12 01:20:26 christos Exp $");
   50 
   51 #include <sys/param.h>
   52 #include <sys/systm.h>
   53 #include <sys/proc.h>
   54 #include <sys/callout.h>
   55 #include <sys/conf.h>
   56 #include <sys/kernel.h>
   57 #include <sys/types.h>
   58 
   59 #include <machine/autoconf.h>
   60 #include <machine/intr.h>
   61 #include <machine/bus.h>
   62 
   63 #include <dev/wscons/wsconsio.h>
   64 #include <dev/wscons/wskbdvar.h>
   65 #include <dev/wscons/wsksymdef.h>
   66 #include <dev/wscons/wsksymvar.h>
   67 
   68 #include <dev/ic/matrixkpvar.h>
   69 
   70 #define TV_ELAPSED_US(x, y)     (((x).tv_sec - (y).tv_sec) * 1000000 + \
   71         ((x).tv_usec - (y).tv_usec))
   72 
   73 const struct wskbd_accessops mxkp_accessops = {
   74         mxkp_enable,
   75         mxkp_set_leds,
   76         mxkp_ioctl,
   77 };
   78 
   79 void
   80 mxkp_attach(struct matrixkp_softc *sc)
   81 {
   82         u_int32_t i;
   83 
   84         callout_init(&sc->sc_callout);
   85         callout_setfunc(&sc->sc_callout, mxkp_poll, sc);
   86         if (sc->poll_freq > hz || sc->poll_freq == 0)
   87                 sc->poll_freq = hz;
   88         sc->sc_enabled = 0;
   89         if (sc->debounce_stable_ms == 0)
   90                 sc->sc_flags |= MXKP_NODEBOUNCE;
   91         if (sc->mxkp_event == NULL)
   92                 sc->mxkp_event = mxkp_wskbd_event;
   93         FOR_KEYS(i, sc->mxkp_pressed[i] = 0);
   94 }
   95 
   96 void
   97 mxkp_poll(void *arg)
   98 {
   99         struct matrixkp_softc *sc = (struct matrixkp_softc *)arg;
  100         u_int32_t i, anychanged;
  101         u_int32_t scanned[(MAXNKEYS + 31) / 32];
  102         u_int32_t changed[(MAXNKEYS + 31) / 32];
  103         u_int32_t set[(MAXNKEYS + 31) / 32];
  104         u_int32_t cleared[(MAXNKEYS + 31) / 32];
  105 
  106 rescan:
  107         anychanged = 0;
  108         FOR_KEYS(i, scanned[i] = 0);
  109         sc->mxkp_scankeys(sc, scanned);
  110         FOR_KEYS(i, changed[i] = sc->mxkp_pressed[i] ^ scanned[i]);
  111         FOR_KEYS(i, anychanged |= changed[i]);
  112 
  113         if (!(sc->sc_flags & MXKP_NODEBOUNCE) && anychanged) {
  114                 mxkp_debounce(sc, changed, scanned);
  115                 anychanged = 0;
  116                 FOR_KEYS(i, changed[i] &= sc->mxkp_pressed[i] ^ scanned[i]);
  117                 FOR_KEYS(i, anychanged |= changed[i]);
  118         }
  119         if (anychanged) {
  120                 FOR_KEYS(i, set[i] = changed[i] & scanned[i]);
  121                 FOR_KEYS(i, cleared[i] = changed[i] & sc->mxkp_pressed[i]);
  122                 sc->mxkp_event(sc, set, cleared);
  123                 FOR_KEYS(i, sc->mxkp_pressed[i] &= ~cleared[i]);
  124                 FOR_KEYS(i, sc->mxkp_pressed[i] |= set[i]);
  125                 goto rescan;
  126         }
  127         if (sc->sc_enabled)
  128                 callout_schedule(&sc->sc_callout, hz / sc->poll_freq);
  129 }
  130 
  131 /*
  132  * debounce will return when masked keys have been stable
  133  * for sc->debounce_stable_ms
  134  */
  135 void
  136 mxkp_debounce(struct matrixkp_softc *sc, u_int32_t *mask, u_int32_t *scan) {
  137         struct timeval verystart, start, now;
  138         u_int32_t last_val[(MAXNKEYS + 31) / 32];
  139         u_int32_t anyset, i;
  140 
  141         FOR_KEYS(i, last_val[i] = scan[i]);
  142         microtime(&verystart);
  143         start = verystart;
  144         do {
  145                 FOR_KEYS(i, scan[i] = 0);
  146                 sc->mxkp_scankeys(sc, scan);
  147                 microtime(&now);
  148                 anyset = 0;
  149                 FOR_KEYS(i, anyset |= (scan[i] ^ last_val[i]) & mask[i]);
  150                 if (anyset) /* bounce detected */
  151                         start = now;
  152                 FOR_KEYS(i, last_val[i] = scan[i]);
  153         } while (TV_ELAPSED_US(now, start) <= (sc->debounce_stable_ms * 1000));
  154 }
  155 
  156 void
  157 mxkp_wskbd_event(struct matrixkp_softc *sc, u_int32_t *on, u_int32_t *off)
  158 {
  159         unsigned int i;
  160 
  161         for(i = 0; i < sc->mxkp_nkeys; i++) {
  162                 if (off[i / 32] & (1 << (i % 32))) {
  163                         wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_UP, i);
  164                 }
  165         }
  166         for(i = 0; i < sc->mxkp_nkeys; i++) {
  167                 if (on[i / 32] & (1 << (i % 32))) {
  168                         wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_DOWN, i);
  169                 }
  170         }
  171 }
  172 
  173 int
  174 mxkp_enable(void *v, int on)
  175 {
  176         struct matrixkp_softc *sc = v;
  177 
  178         if (on) {
  179                 if (sc->sc_enabled)
  180                         return EBUSY;
  181 
  182                 sc->sc_enabled = 1;
  183                 callout_schedule(&sc->sc_callout, hz / sc->poll_freq);
  184         } else {
  185                 sc->sc_enabled = 0;
  186         }
  187 
  188         return 0;
  189 }
  190 
  191 void
  192 mxkp_set_leds(void *v, int leds)
  193 {
  194 }
  195 
  196 int
  197 mxkp_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct lwp *l)
  198 {
  199         switch (cmd) {
  200         case WSKBDIO_GTYPE:
  201                 *(int *)data = WSKBD_TYPE_MATRIXKP;
  202                 return 0;
  203         case WSKBDIO_SETLEDS:
  204                 return 0;
  205         case WSKBDIO_GETLEDS:
  206                 *(int *)data = 0;
  207                 return 0;
  208         case WSKBDIO_COMPLEXBELL:
  209                 return 0;
  210         }
  211         return EPASSTHROUGH;
  212 }

Cache object: 50d159d0a4b31d8c572a6da91a2c8977


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.