button.c revision 1.2 1 /* $NetBSD: button.c,v 1.2 2001/04/30 11:42:17 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 platid_mask_t mask;
81
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(parent, self, aux)
90 struct device *parent;
91 struct device *self;
92 void *aux;
93 {
94 struct hpcio_attach_args *haa = aux;
95 int *loc;
96 struct button_softc *sc = (void*)self;
97 int mode;
98
99 loc = sc->sc_dev.dv_cfdata->cf_loc;
100 sc->sc_hc = (*haa->haa_getchip)(haa->haa_sc, loc[HPCIOIFCF_IOCHIP]);
101 sc->sc_port = loc[HPCIOIFCF_PORT];
102 sc->sc_id = loc[HPCIOIFCF_ID];
103 sc->sc_active = loc[HPCIOIFCF_ACTIVE];
104 printf(" port=%d id=%ld active=%s",
105 sc->sc_port, sc->sc_id, sc->sc_active ? "high" : "low");
106
107 #if 0
108 #if 1 /* Windows CE default */
109 mode = VRGIU_INTR_EDGE_HOLD;
110 #else /* XXX Don't challenge! Freestyle Only */
111 mode = VRGIU_INTR_LEVEL_LOW_HOLD;
112 #endif
113 #endif
114 mode = HPCIO_INTR_HOLD;
115 if (loc[HPCIOIFCF_LEVEL] != HPCIOIFCF_LEVEL_DEFAULT) {
116 mode |= HPCIO_INTR_LEVEL;
117 if (loc[HPCIOIFCF_LEVEL] == 0)
118 mode |= HPCIO_INTR_LOW;
119 else
120 mode |= HPCIO_INTR_HIGH;
121 printf(" sense=level");
122 } else {
123 mode |= HPCIO_INTR_EDGE;
124 switch (loc[HPCIOIFCF_EDGE]) {
125 case 1:
126 mode |= HPCIO_INTR_POSEDGE;
127 break;
128 case 2:
129 mode |= HPCIO_INTR_NEGEDGE;
130 break;
131 case 0:
132 case 3:
133 case HPCIOMANCF_EDGE_DEFAULT:
134 mode |= HPCIO_INTR_POSEDGE;
135 mode |= HPCIO_INTR_NEGEDGE;
136 break;
137 default:
138 printf("%s(%d): invalid configuration, edge=%d",
139 __FILE__, __LINE__, loc[HPCIOIFCF_EDGE]);
140 break;
141 }
142 printf(" sense=edge");
143 }
144
145 if (sc->sc_port == HPCIOIFCF_PORT_DEFAULT ||
146 sc->sc_id == HPCIOIFCF_ID_DEFAULT)
147 printf(" (ignored)");
148 else
149 sc->sc_intr_handle =
150 hpcio_intr_establish(sc->sc_hc, sc->sc_port,
151 mode, button_intr, sc);
152 sc->sc_ghook_tag = config_hook(CONFIG_HOOK_GET,
153 sc->sc_id,
154 CONFIG_HOOK_SHARE,
155 button_state,
156 sc);
157 printf("\n");
158 }
159
160 int
161 button_state(ctx, type, id, msg)
162 void *ctx;
163 int type;
164 long id;
165 void *msg;
166 {
167 struct button_softc *sc = ctx;
168
169 if (type != CONFIG_HOOK_GET || id != sc->sc_id)
170 return 1;
171
172 if (CONFIG_HOOK_VALUEP(msg))
173 return 1;
174
175 *(int*)msg = (hpcio_portread(sc->sc_hc, sc->sc_port) == sc->sc_active);
176 return 0;
177 }
178
179 int
180 button_intr(ctx)
181 void *ctx;
182 {
183 struct button_softc *sc = ctx;
184 int on;
185
186 on = (hpcio_portread(sc->sc_hc, sc->sc_port) == sc->sc_active);
187
188 /* Clear interrupt */
189 hpcio_intr_clear(sc->sc_hc, sc->sc_intr_handle);
190
191 config_hook_call(CONFIG_HOOK_BUTTONEVENT, sc->sc_id, (void*)on);
192
193 return 0;
194 }
195