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