Home | History | Annotate | Line # | Download | only in hpc
button.c revision 1.1
      1 /*	$NetBSD: button.c,v 1.1 2001/04/30 10:10:18 takemura Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999
      5  *         Shin Takemura 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 "opt_vr41xx.h"
     50 #include <hpcmips/vr/vripvar.h>
     51 
     52 #include "locators.h"
     53 
     54 struct button_vrgiu_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_vrgiu_match __P((struct device *, struct cfdata *,
     66 				       void *));
     67 static void	button_vrgiu_attach __P((struct device *, struct device *,
     68 					void *));
     69 static int	button_vrgiu_intr __P((void*));
     70 static int	button_vrgiu_state __P((void *ctx, int type, long id,
     71 				      void *msg));
     72 
     73 struct cfattach button_vrgiu_ca = {
     74 	sizeof(struct button_vrgiu_softc), button_vrgiu_match, button_vrgiu_attach
     75 };
     76 
     77 int
     78 button_vrgiu_match(parent, match, aux)
     79 	struct device *parent;
     80 	struct cfdata *match;
     81 	void *aux;
     82 {
     83 	platid_mask_t mask;
     84 
     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_vrgiu_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_vrgiu_softc *sc = (void*)self;
    100 	int mode;
    101 
    102 	sc->sc_hc = (*haa->haa_getchip)(haa->haa_sc, VRIP_IOCHIP_VRGIU);
    103 
    104 	loc = sc->sc_dev.dv_cfdata->cf_loc;
    105 	sc->sc_port = loc[HPCIOIFCF_PORT];
    106 	sc->sc_id = loc[HPCIOIFCF_ID];
    107 	sc->sc_active = loc[HPCIOIFCF_ACTIVE];
    108 	printf(" port=%d id=%ld active=%s",
    109 	       sc->sc_port, sc->sc_id, sc->sc_active ? "high" : "low");
    110 
    111 #if 0
    112 #if 1 /* Windows CE default */
    113 	mode = VRGIU_INTR_EDGE_HOLD;
    114 #else /* XXX Don't challenge! Freestyle Only */
    115 	mode = VRGIU_INTR_LEVEL_LOW_HOLD;
    116 #endif
    117 #endif
    118 	mode = HPCIO_INTR_HOLD;
    119 	if (loc[HPCIOIFCF_LEVEL] != HPCIOIFCF_LEVEL_DEFAULT) {
    120 		mode |= HPCIO_INTR_LEVEL;
    121 		if (loc[HPCIOIFCF_LEVEL] == 0)
    122 			mode |= HPCIO_INTR_LOW;
    123 		else
    124 			mode |= HPCIO_INTR_HIGH;
    125 		printf(" sense=level");
    126 	} else {
    127 		mode |= HPCIO_INTR_EDGE;
    128 		printf(" sense=edge");
    129 	}
    130 
    131 	if (sc->sc_port == HPCIOIFCF_PORT_DEFAULT ||
    132 	    sc->sc_id == HPCIOIFCF_ID_DEFAULT)
    133 		printf(" (ignored)");
    134 	else
    135 		sc->sc_intr_handle =
    136 		    hpcio_intr_establish(sc->sc_hc, sc->sc_port,
    137 					 mode, button_vrgiu_intr, sc);
    138 	sc->sc_ghook_tag = config_hook(CONFIG_HOOK_GET,
    139 				       sc->sc_id,
    140 				       CONFIG_HOOK_SHARE,
    141 				       button_vrgiu_state,
    142 				       sc);
    143 	printf("\n");
    144 }
    145 
    146 int
    147 button_vrgiu_state(ctx, type, id, msg)
    148 	void *ctx;
    149 	int type;
    150 	long id;
    151 	void *msg;
    152 {
    153 	struct button_vrgiu_softc *sc = ctx;
    154 
    155 	if (type != CONFIG_HOOK_GET || id != sc->sc_id)
    156 		return 1;
    157 
    158 	if (CONFIG_HOOK_VALUEP(msg))
    159 		return 1;
    160 
    161 	*(int*)msg = (hpcio_portread(sc->sc_hc, sc->sc_port) == sc->sc_active);
    162 	return 0;
    163 }
    164 
    165 int
    166 button_vrgiu_intr(ctx)
    167 	void *ctx;
    168 {
    169 	struct button_vrgiu_softc *sc = ctx;
    170 	int on;
    171 
    172 	on = (hpcio_portread(sc->sc_hc, sc->sc_port) == sc->sc_active);
    173 
    174 	/* Clear interrupt */
    175 	hpcio_intr_clear(sc->sc_hc, sc->sc_intr_handle);
    176 
    177 	config_hook_call(CONFIG_HOOK_BUTTONEVENT, sc->sc_id, (void*)on);
    178 
    179 	return 0;
    180 }
    181