button.c revision 1.4.2.2 1 1.4.2.2 nathanw /* $NetBSD: button.c,v 1.4.2.2 2001/06/21 20:01:35 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.2 nathanw #include <sys/param.h>
38 1.4.2.2 nathanw #include <sys/systm.h>
39 1.4.2.2 nathanw #include <sys/device.h>
40 1.4.2.2 nathanw
41 1.4.2.2 nathanw #include <machine/bus.h>
42 1.4.2.2 nathanw
43 1.4.2.2 nathanw #include <machine/config_hook.h>
44 1.4.2.2 nathanw #include <machine/platid.h>
45 1.4.2.2 nathanw #include <machine/platid_mask.h>
46 1.4.2.2 nathanw
47 1.4.2.2 nathanw #include <dev/hpc/hpciovar.h>
48 1.4.2.2 nathanw
49 1.4.2.2 nathanw #include "locators.h"
50 1.4.2.2 nathanw
51 1.4.2.2 nathanw struct button_softc {
52 1.4.2.2 nathanw struct device sc_dev;
53 1.4.2.2 nathanw hpcio_chip_t sc_hc;
54 1.4.2.2 nathanw hpcio_intr_handle_t sc_intr_handle;
55 1.4.2.2 nathanw int sc_port;
56 1.4.2.2 nathanw long sc_id;
57 1.4.2.2 nathanw int sc_active;
58 1.4.2.2 nathanw config_hook_tag sc_hook_tag;
59 1.4.2.2 nathanw config_hook_tag sc_ghook_tag;
60 1.4.2.2 nathanw };
61 1.4.2.2 nathanw
62 1.4.2.2 nathanw static int button_match(struct device *, struct cfdata *, void *);
63 1.4.2.2 nathanw static void button_attach(struct device *, struct device *, void *);
64 1.4.2.2 nathanw static int button_intr(void *);
65 1.4.2.2 nathanw static int button_state(void *, int, long, void *);
66 1.4.2.2 nathanw
67 1.4.2.2 nathanw struct cfattach button_ca = {
68 1.4.2.2 nathanw sizeof(struct button_softc), button_match, button_attach
69 1.4.2.2 nathanw };
70 1.4.2.2 nathanw
71 1.4.2.2 nathanw int
72 1.4.2.2 nathanw button_match(struct device *parent, struct cfdata *match, void *aux)
73 1.4.2.2 nathanw {
74 1.4.2.2 nathanw struct hpcio_attach_args *haa = aux;
75 1.4.2.2 nathanw platid_mask_t mask;
76 1.4.2.2 nathanw
77 1.4.2.2 nathanw if (strcmp(haa->haa_busname, HPCIO_BUSNAME))
78 1.4.2.2 nathanw return (0);
79 1.4.2.2 nathanw if (match->cf_loc[HPCIOIFCF_PLATFORM] == 0)
80 1.4.2.2 nathanw return (0);
81 1.4.2.2 nathanw mask = PLATID_DEREF(match->cf_loc[HPCIOIFCF_PLATFORM]);
82 1.4.2.2 nathanw return (platid_match(&platid, &mask));
83 1.4.2.2 nathanw }
84 1.4.2.2 nathanw
85 1.4.2.2 nathanw void
86 1.4.2.2 nathanw button_attach(struct device *parent, struct device *self, void *aux)
87 1.4.2.2 nathanw {
88 1.4.2.2 nathanw struct hpcio_attach_args *haa = aux;
89 1.4.2.2 nathanw int *loc;
90 1.4.2.2 nathanw struct button_softc *sc = (void*)self;
91 1.4.2.2 nathanw int mode;
92 1.4.2.2 nathanw
93 1.4.2.2 nathanw loc = sc->sc_dev.dv_cfdata->cf_loc;
94 1.4.2.2 nathanw sc->sc_hc = (*haa->haa_getchip)(haa->haa_sc, loc[HPCIOIFCF_IOCHIP]);
95 1.4.2.2 nathanw sc->sc_port = loc[HPCIOIFCF_PORT];
96 1.4.2.2 nathanw sc->sc_id = loc[HPCIOIFCF_ID];
97 1.4.2.2 nathanw sc->sc_active = loc[HPCIOIFCF_ACTIVE];
98 1.4.2.2 nathanw printf(" port=%d id=%ld active=%s",
99 1.4.2.2 nathanw sc->sc_port, sc->sc_id, sc->sc_active ? "high" : "low");
100 1.4.2.2 nathanw
101 1.4.2.2 nathanw #if 0
102 1.4.2.2 nathanw #if 1 /* Windows CE default */
103 1.4.2.2 nathanw mode = VRGIU_INTR_EDGE_HOLD;
104 1.4.2.2 nathanw #else /* XXX Don't challenge! Freestyle Only */
105 1.4.2.2 nathanw mode = VRGIU_INTR_LEVEL_LOW_HOLD;
106 1.4.2.2 nathanw #endif
107 1.4.2.2 nathanw #endif
108 1.4.2.2 nathanw mode = HPCIO_INTR_HOLD;
109 1.4.2.2 nathanw if (loc[HPCIOIFCF_LEVEL] != HPCIOIFCF_LEVEL_DEFAULT) {
110 1.4.2.2 nathanw mode |= HPCIO_INTR_LEVEL;
111 1.4.2.2 nathanw if (loc[HPCIOIFCF_LEVEL] == 0)
112 1.4.2.2 nathanw mode |= HPCIO_INTR_LOW;
113 1.4.2.2 nathanw else
114 1.4.2.2 nathanw mode |= HPCIO_INTR_HIGH;
115 1.4.2.2 nathanw printf(" sense=level");
116 1.4.2.2 nathanw } else {
117 1.4.2.2 nathanw mode |= HPCIO_INTR_EDGE;
118 1.4.2.2 nathanw switch (loc[HPCIOIFCF_EDGE]) {
119 1.4.2.2 nathanw case 1:
120 1.4.2.2 nathanw mode |= HPCIO_INTR_POSEDGE;
121 1.4.2.2 nathanw break;
122 1.4.2.2 nathanw case 2:
123 1.4.2.2 nathanw mode |= HPCIO_INTR_NEGEDGE;
124 1.4.2.2 nathanw break;
125 1.4.2.2 nathanw case 0:
126 1.4.2.2 nathanw case 3:
127 1.4.2.2 nathanw case HPCIOMANCF_EDGE_DEFAULT:
128 1.4.2.2 nathanw mode |= HPCIO_INTR_POSEDGE;
129 1.4.2.2 nathanw mode |= HPCIO_INTR_NEGEDGE;
130 1.4.2.2 nathanw break;
131 1.4.2.2 nathanw default:
132 1.4.2.2 nathanw printf("%s(%d): invalid configuration, edge=%d",
133 1.4.2.2 nathanw __FILE__, __LINE__, loc[HPCIOIFCF_EDGE]);
134 1.4.2.2 nathanw break;
135 1.4.2.2 nathanw }
136 1.4.2.2 nathanw printf(" sense=edge");
137 1.4.2.2 nathanw }
138 1.4.2.2 nathanw
139 1.4.2.2 nathanw if (sc->sc_port == HPCIOIFCF_PORT_DEFAULT ||
140 1.4.2.2 nathanw sc->sc_id == HPCIOIFCF_ID_DEFAULT)
141 1.4.2.2 nathanw printf(" (ignored)");
142 1.4.2.2 nathanw else
143 1.4.2.2 nathanw sc->sc_intr_handle =
144 1.4.2.2 nathanw hpcio_intr_establish(sc->sc_hc, sc->sc_port,
145 1.4.2.2 nathanw mode, button_intr, sc);
146 1.4.2.2 nathanw sc->sc_ghook_tag = config_hook(CONFIG_HOOK_GET, sc->sc_id,
147 1.4.2.2 nathanw CONFIG_HOOK_SHARE, button_state, sc);
148 1.4.2.2 nathanw printf("\n");
149 1.4.2.2 nathanw }
150 1.4.2.2 nathanw
151 1.4.2.2 nathanw int
152 1.4.2.2 nathanw button_state(void *ctx, int type, long id, void *msg)
153 1.4.2.2 nathanw {
154 1.4.2.2 nathanw struct button_softc *sc = ctx;
155 1.4.2.2 nathanw
156 1.4.2.2 nathanw if (type != CONFIG_HOOK_GET || id != sc->sc_id)
157 1.4.2.2 nathanw return (1);
158 1.4.2.2 nathanw
159 1.4.2.2 nathanw if (CONFIG_HOOK_VALUEP(msg))
160 1.4.2.2 nathanw return (1);
161 1.4.2.2 nathanw
162 1.4.2.2 nathanw *(int*)msg = (hpcio_portread(sc->sc_hc, sc->sc_port) == sc->sc_active);
163 1.4.2.2 nathanw return (0);
164 1.4.2.2 nathanw }
165 1.4.2.2 nathanw
166 1.4.2.2 nathanw int
167 1.4.2.2 nathanw button_intr(void *ctx)
168 1.4.2.2 nathanw {
169 1.4.2.2 nathanw struct button_softc *sc = ctx;
170 1.4.2.2 nathanw int on;
171 1.4.2.2 nathanw
172 1.4.2.2 nathanw on = (hpcio_portread(sc->sc_hc, sc->sc_port) == sc->sc_active);
173 1.4.2.2 nathanw
174 1.4.2.2 nathanw /* Clear interrupt */
175 1.4.2.2 nathanw hpcio_intr_clear(sc->sc_hc, sc->sc_intr_handle);
176 1.4.2.2 nathanw
177 1.4.2.2 nathanw config_hook_call(CONFIG_HOOK_BUTTONEVENT, sc->sc_id, (void*)on);
178 1.4.2.2 nathanw
179 1.4.2.2 nathanw return (0);
180 1.4.2.2 nathanw }
181