1 /* $NetBSD: button.c,v 1.5 2001/11/13 12:47:56 lukem Exp $ */ 2 3 /*- 4 * Copyright (c) 1999-2001 5 * TAKEMURA Shin1 and PocketBSD Project. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the PocketBSD project 18 * and its contributors. 19 * 4. Neither the name of the project nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: button.c,v 1.5 2001/11/13 12:47:56 lukem Exp $"); 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/device.h> 43 44 #include <machine/bus.h> 45 46 #include <machine/config_hook.h> 47 #include <machine/platid.h> 48 #include <machine/platid_mask.h> 49 50 #include <dev/hpc/hpciovar.h> 51 52 #include "locators.h" 53 54 struct button_softc { 55 struct device sc_dev; 56 hpcio_chip_t sc_hc; 57 hpcio_intr_handle_t sc_intr_handle; 58 int sc_port; 59 long sc_id; 60 int sc_active; 61 config_hook_tag sc_hook_tag; 62 config_hook_tag sc_ghook_tag; 63 }; 64 65 static int button_match(struct device *, struct cfdata *, void *); 66 static void button_attach(struct device *, struct device *, void *); 67 static int button_intr(void *); 68 static int button_state(void *, int, long, void *); 69 70 struct cfattach button_ca = { 71 sizeof(struct button_softc), button_match, button_attach 72 }; 73 74 int 75 button_match(struct device *parent, struct cfdata *match, void *aux) 76 { 77 struct hpcio_attach_args *haa = aux; 78 platid_mask_t mask; 79 80 if (strcmp(haa->haa_busname, HPCIO_BUSNAME)) 81 return (0); 82 if (match->cf_loc[HPCIOIFCF_PLATFORM] == 0) 83 return (0); 84 mask = PLATID_DEREF(match->cf_loc[HPCIOIFCF_PLATFORM]); 85 return (platid_match(&platid, &mask)); 86 } 87 88 void 89 button_attach(struct device *parent, struct device *self, void *aux) 90 { 91 struct hpcio_attach_args *haa = aux; 92 int *loc; 93 struct button_softc *sc = (void*)self; 94 int mode; 95 96 loc = sc->sc_dev.dv_cfdata->cf_loc; 97 sc->sc_hc = (*haa->haa_getchip)(haa->haa_sc, loc[HPCIOIFCF_IOCHIP]); 98 sc->sc_port = loc[HPCIOIFCF_PORT]; 99 sc->sc_id = loc[HPCIOIFCF_ID]; 100 sc->sc_active = loc[HPCIOIFCF_ACTIVE]; 101 printf(" port=%d id=%ld active=%s", 102 sc->sc_port, sc->sc_id, sc->sc_active ? "high" : "low"); 103 104 #if 0 105 #if 1 /* Windows CE default */ 106 mode = VRGIU_INTR_EDGE_HOLD; 107 #else /* XXX Don't challenge! Freestyle Only */ 108 mode = VRGIU_INTR_LEVEL_LOW_HOLD; 109 #endif 110 #endif 111 mode = HPCIO_INTR_HOLD; 112 if (loc[HPCIOIFCF_LEVEL] != HPCIOIFCF_LEVEL_DEFAULT) { 113 mode |= HPCIO_INTR_LEVEL; 114 if (loc[HPCIOIFCF_LEVEL] == 0) 115 mode |= HPCIO_INTR_LOW; 116 else 117 mode |= HPCIO_INTR_HIGH; 118 printf(" sense=level"); 119 } else { 120 mode |= HPCIO_INTR_EDGE; 121 switch (loc[HPCIOIFCF_EDGE]) { 122 case 1: 123 mode |= HPCIO_INTR_POSEDGE; 124 break; 125 case 2: 126 mode |= HPCIO_INTR_NEGEDGE; 127 break; 128 case 0: 129 case 3: 130 case HPCIOMANCF_EDGE_DEFAULT: 131 mode |= HPCIO_INTR_POSEDGE; 132 mode |= HPCIO_INTR_NEGEDGE; 133 break; 134 default: 135 printf("%s(%d): invalid configuration, edge=%d", 136 __FILE__, __LINE__, loc[HPCIOIFCF_EDGE]); 137 break; 138 } 139 printf(" sense=edge"); 140 } 141 142 if (sc->sc_port == HPCIOIFCF_PORT_DEFAULT || 143 sc->sc_id == HPCIOIFCF_ID_DEFAULT) 144 printf(" (ignored)"); 145 else 146 sc->sc_intr_handle = 147 hpcio_intr_establish(sc->sc_hc, sc->sc_port, 148 mode, button_intr, sc); 149 sc->sc_ghook_tag = config_hook(CONFIG_HOOK_GET, sc->sc_id, 150 CONFIG_HOOK_SHARE, button_state, sc); 151 printf("\n"); 152 } 153 154 int 155 button_state(void *ctx, int type, long id, void *msg) 156 { 157 struct button_softc *sc = ctx; 158 159 if (type != CONFIG_HOOK_GET || id != sc->sc_id) 160 return (1); 161 162 if (CONFIG_HOOK_VALUEP(msg)) 163 return (1); 164 165 *(int*)msg = (hpcio_portread(sc->sc_hc, sc->sc_port) == sc->sc_active); 166 return (0); 167 } 168 169 int 170 button_intr(void *ctx) 171 { 172 struct button_softc *sc = ctx; 173 int on; 174 175 on = (hpcio_portread(sc->sc_hc, sc->sc_port) == sc->sc_active); 176 177 /* Clear interrupt */ 178 hpcio_intr_clear(sc->sc_hc, sc->sc_intr_handle); 179 180 config_hook_call(CONFIG_HOOK_BUTTONEVENT, sc->sc_id, (void*)on); 181 182 return (0); 183 } 184