xref: /netbsd-src/sys/dev/hpc/button.c (revision 27578b9aac214cc7796ead81dcc5427e79d5f2a0)
1 /*	$NetBSD: button.c,v 1.4 2001/06/04 18:59:31 uch 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(struct device *, struct cfdata *, void *);
63 static void	button_attach(struct device *, struct device *, void *);
64 static int	button_intr(void *);
65 static int	button_state(void *, int, long, void *);
66 
67 struct cfattach button_ca = {
68 	sizeof(struct button_softc), button_match, button_attach
69 };
70 
71 int
72 button_match(struct device *parent, struct cfdata *match, void *aux)
73 {
74 	struct hpcio_attach_args *haa = aux;
75 	platid_mask_t mask;
76 
77 	if (strcmp(haa->haa_busname, HPCIO_BUSNAME))
78 		return (0);
79 	if (match->cf_loc[HPCIOIFCF_PLATFORM] == 0)
80 		return (0);
81 	mask = PLATID_DEREF(match->cf_loc[HPCIOIFCF_PLATFORM]);
82 	return (platid_match(&platid, &mask));
83 }
84 
85 void
86 button_attach(struct device *parent, struct device *self, void *aux)
87 {
88 	struct hpcio_attach_args *haa = aux;
89 	int *loc;
90 	struct button_softc *sc = (void*)self;
91 	int mode;
92 
93 	loc = sc->sc_dev.dv_cfdata->cf_loc;
94 	sc->sc_hc = (*haa->haa_getchip)(haa->haa_sc, loc[HPCIOIFCF_IOCHIP]);
95 	sc->sc_port = loc[HPCIOIFCF_PORT];
96 	sc->sc_id = loc[HPCIOIFCF_ID];
97 	sc->sc_active = loc[HPCIOIFCF_ACTIVE];
98 	printf(" port=%d id=%ld active=%s",
99 	    sc->sc_port, sc->sc_id, sc->sc_active ? "high" : "low");
100 
101 #if 0
102 #if 1 /* Windows CE default */
103 	mode = VRGIU_INTR_EDGE_HOLD;
104 #else /* XXX Don't challenge! Freestyle Only */
105 	mode = VRGIU_INTR_LEVEL_LOW_HOLD;
106 #endif
107 #endif
108 	mode = HPCIO_INTR_HOLD;
109 	if (loc[HPCIOIFCF_LEVEL] != HPCIOIFCF_LEVEL_DEFAULT) {
110 		mode |= HPCIO_INTR_LEVEL;
111 		if (loc[HPCIOIFCF_LEVEL] == 0)
112 			mode |= HPCIO_INTR_LOW;
113 		else
114 			mode |= HPCIO_INTR_HIGH;
115 		printf(" sense=level");
116 	} else {
117 		mode |= HPCIO_INTR_EDGE;
118 		switch (loc[HPCIOIFCF_EDGE]) {
119 		case 1:
120 			mode |= HPCIO_INTR_POSEDGE;
121 			break;
122 		case 2:
123 			mode |= HPCIO_INTR_NEGEDGE;
124 			break;
125 		case 0:
126 		case 3:
127 		case HPCIOMANCF_EDGE_DEFAULT:
128 			mode |= HPCIO_INTR_POSEDGE;
129 			mode |= HPCIO_INTR_NEGEDGE;
130 			break;
131 		default:
132 			printf("%s(%d): invalid configuration, edge=%d",
133 			    __FILE__, __LINE__, loc[HPCIOIFCF_EDGE]);
134 			break;
135 		}
136 		printf(" sense=edge");
137 	}
138 
139 	if (sc->sc_port == HPCIOIFCF_PORT_DEFAULT ||
140 	    sc->sc_id == HPCIOIFCF_ID_DEFAULT)
141 		printf(" (ignored)");
142 	else
143 		sc->sc_intr_handle =
144 		    hpcio_intr_establish(sc->sc_hc, sc->sc_port,
145 			mode, button_intr, sc);
146 	sc->sc_ghook_tag = config_hook(CONFIG_HOOK_GET, sc->sc_id,
147 	    CONFIG_HOOK_SHARE, button_state, sc);
148 	printf("\n");
149 }
150 
151 int
152 button_state(void *ctx, int type, long id, void *msg)
153 {
154 	struct button_softc *sc = ctx;
155 
156 	if (type != CONFIG_HOOK_GET || id != sc->sc_id)
157 		return (1);
158 
159 	if (CONFIG_HOOK_VALUEP(msg))
160 		return (1);
161 
162 	*(int*)msg = (hpcio_portread(sc->sc_hc, sc->sc_port) == sc->sc_active);
163 	return (0);
164 }
165 
166 int
167 button_intr(void *ctx)
168 {
169 	struct button_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