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