1 1.7 rmind /* $NetBSD: lcdkp_subr.c,v 1.7 2011/05/14 02:58:27 rmind Exp $ */ 2 1.1 soren 3 1.1 soren /* 4 1.1 soren * Copyright (c) 2002 Dennis I. Chernoivanov 5 1.1 soren * All rights reserved. 6 1.1 soren * 7 1.1 soren * Redistribution and use in source and binary forms, with or without 8 1.1 soren * modification, are permitted provided that the following conditions 9 1.1 soren * are met: 10 1.1 soren * 1. Redistributions of source code must retain the above copyright 11 1.1 soren * notice, this list of conditions and the following disclaimer. 12 1.1 soren * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 soren * notice, this list of conditions and the following disclaimer in the 14 1.1 soren * documentation and/or other materials provided with the distribution. 15 1.1 soren * 3. The name of the author may not be used to endorse or promote products 16 1.1 soren * derived from this software without specific prior written permission 17 1.1 soren * 18 1.1 soren * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 1.1 soren * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 1.1 soren * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 1.1 soren * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 1.1 soren * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 1.1 soren * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 1.1 soren * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 1.1 soren * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 1.1 soren * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 1.1 soren * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 1.1 soren */ 29 1.1 soren 30 1.3 perry /* 31 1.1 soren * Subroutines for simple one-port keypad. 32 1.1 soren */ 33 1.1 soren 34 1.1 soren #include <sys/cdefs.h> 35 1.7 rmind __KERNEL_RCSID(0, "$NetBSD: lcdkp_subr.c,v 1.7 2011/05/14 02:58:27 rmind Exp $"); 36 1.1 soren 37 1.1 soren #include <sys/param.h> 38 1.1 soren #include <sys/systm.h> 39 1.1 soren #include <sys/proc.h> 40 1.1 soren #include <sys/conf.h> 41 1.1 soren #include <sys/kernel.h> 42 1.1 soren #include <sys/types.h> 43 1.1 soren 44 1.1 soren #include <machine/autoconf.h> 45 1.5 ad #include <sys/intr.h> 46 1.5 ad #include <sys/bus.h> 47 1.1 soren 48 1.1 soren #include <dev/ic/lcdkp_subr.h> 49 1.1 soren 50 1.1 soren #define HD_POLL_RATE (hz / 10) 51 1.1 soren 52 1.1 soren static int lcdkp_poll(struct lcdkp_chip *); 53 1.1 soren static u_char lcdkp_scan(struct lcdkp_chip *, u_int8_t *); 54 1.1 soren 55 1.1 soren /* 56 1.1 soren * Initialization. 57 1.1 soren */ 58 1.1 soren void 59 1.6 dsl lcdkp_attach_subr(struct lcdkp_chip *sc) 60 1.1 soren { 61 1.7 rmind 62 1.1 soren sc->sc_flags = 0x0; 63 1.7 rmind mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE); 64 1.1 soren } 65 1.1 soren 66 1.1 soren /* 67 1.1 soren * Scan whether input is pending, don't block if not. 68 1.1 soren */ 69 1.1 soren int 70 1.6 dsl lcdkp_scankey(struct lcdkp_chip *sc) 71 1.1 soren { 72 1.1 soren int ret; 73 1.1 soren 74 1.1 soren if ((sc->sc_knum == 0) || (sc->sc_kpad == NULL)) 75 1.1 soren return 0; 76 1.1 soren 77 1.7 rmind mutex_enter(&sc->sc_lock); 78 1.1 soren if (!(sc->sc_flags & LCDKP_HAS_BUF)) { 79 1.1 soren u_int8_t b; 80 1.1 soren if (lcdkp_scan(sc, &b) != 0) { 81 1.1 soren sc->sc_buf = b; 82 1.1 soren sc->sc_flags |= LCDKP_HAS_BUF; 83 1.1 soren } 84 1.1 soren } 85 1.1 soren ret = (sc->sc_flags & LCDKP_HAS_BUF); 86 1.7 rmind mutex_exit(&sc->sc_lock); 87 1.1 soren 88 1.1 soren return ret; 89 1.1 soren } 90 1.1 soren 91 1.1 soren /* 92 1.1 soren * Read new key code, block if needed. 93 1.1 soren */ 94 1.1 soren int 95 1.6 dsl lcdkp_readkey(struct lcdkp_chip *sc, u_int8_t *result) 96 1.1 soren { 97 1.1 soren int error; 98 1.1 soren 99 1.1 soren if ((sc->sc_knum == 0) || (sc->sc_kpad == NULL)) 100 1.1 soren return EIO; 101 1.1 soren 102 1.7 rmind mutex_enter(&sc->sc_lock); 103 1.1 soren if ( (error = lcdkp_poll(sc)) == 0) { 104 1.1 soren *result = sc->sc_buf; 105 1.1 soren sc->sc_flags &= ~LCDKP_HAS_BUF; 106 1.1 soren } 107 1.7 rmind mutex_exit(&sc->sc_lock); 108 1.1 soren 109 1.1 soren return 0; 110 1.1 soren } 111 1.1 soren 112 1.1 soren /* 113 1.1 soren * Scan the keypad and translate the input. 114 1.1 soren */ 115 1.1 soren static u_char 116 1.6 dsl lcdkp_scan(struct lcdkp_chip *sc, u_int8_t *b) 117 1.1 soren { 118 1.1 soren u_int8_t i; 119 1.1 soren u_int8_t c; 120 1.3 perry 121 1.1 soren c = lcdkp_dr_read(sc); 122 1.1 soren for (i = 0; i < sc->sc_knum; i++) { 123 1.1 soren if (sc->sc_kpad[i].x_keycode == c) { 124 1.1 soren *b = sc->sc_kpad[i].x_outcode; 125 1.1 soren return 1; 126 1.1 soren } 127 1.1 soren } 128 1.1 soren return 0; 129 1.1 soren } 130 1.1 soren 131 1.1 soren /* 132 1.1 soren * Block until input is available. 133 1.1 soren */ 134 1.1 soren static int 135 1.6 dsl lcdkp_poll(struct lcdkp_chip *sc) 136 1.1 soren { 137 1.7 rmind int error; 138 1.7 rmind uint8_t b; 139 1.7 rmind 140 1.7 rmind KASSERT(mutex_owned(&sc->sc_lock)); 141 1.7 rmind 142 1.7 rmind if (sc->sc_flags & LCDKP_HAS_BUF) { 143 1.7 rmind return 0; 144 1.7 rmind } 145 1.7 rmind while (lcdkp_scan(sc, &b) == 0) { 146 1.7 rmind error = mtsleep((void*)sc, PRIBIO | PCATCH, "kppoll", 147 1.7 rmind HD_POLL_RATE, &sc->sc_lock); 148 1.7 rmind if (error != EWOULDBLOCK) { 149 1.7 rmind if (lcdkp_scan(sc, &b) != 0) 150 1.7 rmind break; 151 1.7 rmind return EINTR; 152 1.1 soren } 153 1.1 soren } 154 1.7 rmind sc->sc_buf = b; 155 1.7 rmind sc->sc_flags |= LCDKP_HAS_BUF; 156 1.1 soren return 0; 157 1.1 soren } 158