Home | History | Annotate | Line # | Download | only in hpc
button.c revision 1.3
      1 /*	$NetBSD: button.c,v 1.3 2001/05/01 00:25:16 takemura 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/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/device.h>
     40 
     41 #include <machine/bus.h>
     42 
     43 #include <machine/config_hook.h>
     44 #include <machine/platid.h>
     45 #include <machine/platid_mask.h>
     46 
     47 #include <dev/hpc/hpciovar.h>
     48 
     49 #include "locators.h"
     50 
     51 struct button_softc {
     52 	struct device sc_dev;
     53 	hpcio_chip_t sc_hc;
     54 	hpcio_intr_handle_t sc_intr_handle;
     55 	int sc_port;
     56 	long sc_id;
     57 	int sc_active;
     58 	config_hook_tag sc_hook_tag;
     59 	config_hook_tag sc_ghook_tag;
     60 };
     61 
     62 static int	button_match __P((struct device *, struct cfdata *,
     63 				       void *));
     64 static void	button_attach __P((struct device *, struct device *,
     65 					void *));
     66 static int	button_intr __P((void*));
     67 static int	button_state __P((void *ctx, int type, long id,
     68 				      void *msg));
     69 
     70 struct cfattach button_ca = {
     71 	sizeof(struct button_softc), button_match, button_attach
     72 };
     73 
     74 int
     75 button_match(parent, match, aux)
     76 	struct device *parent;
     77 	struct cfdata *match;
     78 	void *aux;
     79 {
     80 	struct hpcio_attach_args *haa = aux;
     81 	platid_mask_t mask;
     82 
     83 	if (strcmp(haa->haa_busname, HPCIO_BUSNAME))
     84 		return 0;
     85 	if (match->cf_loc[HPCIOIFCF_PLATFORM] == 0)
     86 		return 0;
     87 	mask = PLATID_DEREF(match->cf_loc[HPCIOIFCF_PLATFORM]);
     88 	return platid_match(&platid, &mask);
     89 }
     90 
     91 void
     92 button_attach(parent, self, aux)
     93 	struct device *parent;
     94 	struct device *self;
     95 	void *aux;
     96 {
     97 	struct hpcio_attach_args *haa = aux;
     98 	int *loc;
     99 	struct button_softc *sc = (void*)self;
    100 	int mode;
    101 
    102 	loc = sc->sc_dev.dv_cfdata->cf_loc;
    103 	sc->sc_hc = (*haa->haa_getchip)(haa->haa_sc, loc[HPCIOIFCF_IOCHIP]);
    104 	sc->sc_port = loc[HPCIOIFCF_PORT];
    105 	sc->sc_id = loc[HPCIOIFCF_ID];
    106 	sc->sc_active = loc[HPCIOIFCF_ACTIVE];
    107 	printf(" port=%d id=%ld active=%s",
    108 	       sc->sc_port, sc->sc_id, sc->sc_active ? "high" : "low");
    109 
    110 #if 0
    111 #if 1 /* Windows CE default */
    112 	mode = VRGIU_INTR_EDGE_HOLD;
    113 #else /* XXX Don't challenge! Freestyle Only */
    114 	mode = VRGIU_INTR_LEVEL_LOW_HOLD;
    115 #endif
    116 #endif
    117 	mode = HPCIO_INTR_HOLD;
    118 	if (loc[HPCIOIFCF_LEVEL] != HPCIOIFCF_LEVEL_DEFAULT) {
    119 		mode |= HPCIO_INTR_LEVEL;
    120 		if (loc[HPCIOIFCF_LEVEL] == 0)
    121 			mode |= HPCIO_INTR_LOW;
    122 		else
    123 			mode |= HPCIO_INTR_HIGH;
    124 		printf(" sense=level");
    125 	} else {
    126 		mode |= HPCIO_INTR_EDGE;
    127 		switch (loc[HPCIOIFCF_EDGE]) {
    128 		case 1:
    129 			mode |= HPCIO_INTR_POSEDGE;
    130 			break;
    131 		case 2:
    132 			mode |= HPCIO_INTR_NEGEDGE;
    133 			break;
    134 		case 0:
    135 		case 3:
    136 		case HPCIOMANCF_EDGE_DEFAULT:
    137 			mode |= HPCIO_INTR_POSEDGE;
    138 			mode |= HPCIO_INTR_NEGEDGE;
    139 			break;
    140 		default:
    141 			printf("%s(%d): invalid configuration, edge=%d",
    142 			    __FILE__, __LINE__, loc[HPCIOIFCF_EDGE]);
    143 			break;
    144 		}
    145 		printf(" sense=edge");
    146 	}
    147 
    148 	if (sc->sc_port == HPCIOIFCF_PORT_DEFAULT ||
    149 	    sc->sc_id == HPCIOIFCF_ID_DEFAULT)
    150 		printf(" (ignored)");
    151 	else
    152 		sc->sc_intr_handle =
    153 		    hpcio_intr_establish(sc->sc_hc, sc->sc_port,
    154 					 mode, button_intr, sc);
    155 	sc->sc_ghook_tag = config_hook(CONFIG_HOOK_GET,
    156 				       sc->sc_id,
    157 				       CONFIG_HOOK_SHARE,
    158 				       button_state,
    159 				       sc);
    160 	printf("\n");
    161 }
    162 
    163 int
    164 button_state(ctx, type, id, msg)
    165 	void *ctx;
    166 	int type;
    167 	long id;
    168 	void *msg;
    169 {
    170 	struct button_softc *sc = ctx;
    171 
    172 	if (type != CONFIG_HOOK_GET || id != sc->sc_id)
    173 		return 1;
    174 
    175 	if (CONFIG_HOOK_VALUEP(msg))
    176 		return 1;
    177 
    178 	*(int*)msg = (hpcio_portread(sc->sc_hc, sc->sc_port) == sc->sc_active);
    179 	return 0;
    180 }
    181 
    182 int
    183 button_intr(ctx)
    184 	void *ctx;
    185 {
    186 	struct button_softc *sc = ctx;
    187 	int on;
    188 
    189 	on = (hpcio_portread(sc->sc_hc, sc->sc_port) == sc->sc_active);
    190 
    191 	/* Clear interrupt */
    192 	hpcio_intr_clear(sc->sc_hc, sc->sc_intr_handle);
    193 
    194 	config_hook_call(CONFIG_HOOK_BUTTONEVENT, sc->sc_id, (void*)on);
    195 
    196 	return 0;
    197 }
    198