button.c revision 1.4.2.4 1 1.4.2.4 nathanw /* $NetBSD: button.c,v 1.4.2.4 2002/02/28 04:13:16 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.4 nathanw __KERNEL_RCSID(0, "$NetBSD: button.c,v 1.4.2.4 2002/02/28 04:13:16 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 mode = HPCIO_INTR_HOLD;
105 1.4.2.2 nathanw if (loc[HPCIOIFCF_LEVEL] != HPCIOIFCF_LEVEL_DEFAULT) {
106 1.4.2.2 nathanw mode |= HPCIO_INTR_LEVEL;
107 1.4.2.2 nathanw if (loc[HPCIOIFCF_LEVEL] == 0)
108 1.4.2.2 nathanw mode |= HPCIO_INTR_LOW;
109 1.4.2.2 nathanw else
110 1.4.2.2 nathanw mode |= HPCIO_INTR_HIGH;
111 1.4.2.2 nathanw printf(" sense=level");
112 1.4.2.2 nathanw } else {
113 1.4.2.2 nathanw mode |= HPCIO_INTR_EDGE;
114 1.4.2.2 nathanw switch (loc[HPCIOIFCF_EDGE]) {
115 1.4.2.2 nathanw case 1:
116 1.4.2.2 nathanw mode |= HPCIO_INTR_POSEDGE;
117 1.4.2.2 nathanw break;
118 1.4.2.2 nathanw case 2:
119 1.4.2.2 nathanw mode |= HPCIO_INTR_NEGEDGE;
120 1.4.2.2 nathanw break;
121 1.4.2.2 nathanw case 0:
122 1.4.2.2 nathanw case 3:
123 1.4.2.2 nathanw case HPCIOMANCF_EDGE_DEFAULT:
124 1.4.2.2 nathanw mode |= HPCIO_INTR_POSEDGE;
125 1.4.2.2 nathanw mode |= HPCIO_INTR_NEGEDGE;
126 1.4.2.2 nathanw break;
127 1.4.2.2 nathanw default:
128 1.4.2.2 nathanw printf("%s(%d): invalid configuration, edge=%d",
129 1.4.2.2 nathanw __FILE__, __LINE__, loc[HPCIOIFCF_EDGE]);
130 1.4.2.2 nathanw break;
131 1.4.2.2 nathanw }
132 1.4.2.2 nathanw printf(" sense=edge");
133 1.4.2.2 nathanw }
134 1.4.2.2 nathanw
135 1.4.2.2 nathanw if (sc->sc_port == HPCIOIFCF_PORT_DEFAULT ||
136 1.4.2.2 nathanw sc->sc_id == HPCIOIFCF_ID_DEFAULT)
137 1.4.2.2 nathanw printf(" (ignored)");
138 1.4.2.2 nathanw else
139 1.4.2.2 nathanw sc->sc_intr_handle =
140 1.4.2.2 nathanw hpcio_intr_establish(sc->sc_hc, sc->sc_port,
141 1.4.2.2 nathanw mode, button_intr, sc);
142 1.4.2.2 nathanw sc->sc_ghook_tag = config_hook(CONFIG_HOOK_GET, sc->sc_id,
143 1.4.2.2 nathanw CONFIG_HOOK_SHARE, button_state, sc);
144 1.4.2.2 nathanw printf("\n");
145 1.4.2.2 nathanw }
146 1.4.2.2 nathanw
147 1.4.2.2 nathanw int
148 1.4.2.2 nathanw button_state(void *ctx, int type, long id, void *msg)
149 1.4.2.2 nathanw {
150 1.4.2.2 nathanw struct button_softc *sc = ctx;
151 1.4.2.2 nathanw
152 1.4.2.2 nathanw if (type != CONFIG_HOOK_GET || id != sc->sc_id)
153 1.4.2.2 nathanw return (1);
154 1.4.2.2 nathanw
155 1.4.2.2 nathanw if (CONFIG_HOOK_VALUEP(msg))
156 1.4.2.2 nathanw return (1);
157 1.4.2.2 nathanw
158 1.4.2.2 nathanw *(int*)msg = (hpcio_portread(sc->sc_hc, sc->sc_port) == sc->sc_active);
159 1.4.2.2 nathanw return (0);
160 1.4.2.2 nathanw }
161 1.4.2.2 nathanw
162 1.4.2.2 nathanw int
163 1.4.2.2 nathanw button_intr(void *ctx)
164 1.4.2.2 nathanw {
165 1.4.2.2 nathanw struct button_softc *sc = ctx;
166 1.4.2.2 nathanw int on;
167 1.4.2.2 nathanw
168 1.4.2.2 nathanw on = (hpcio_portread(sc->sc_hc, sc->sc_port) == sc->sc_active);
169 1.4.2.2 nathanw
170 1.4.2.2 nathanw /* Clear interrupt */
171 1.4.2.2 nathanw hpcio_intr_clear(sc->sc_hc, sc->sc_intr_handle);
172 1.4.2.2 nathanw
173 1.4.2.2 nathanw config_hook_call(CONFIG_HOOK_BUTTONEVENT, sc->sc_id, (void*)on);
174 1.4.2.2 nathanw
175 1.4.2.2 nathanw return (0);
176 1.4.2.2 nathanw }
177