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