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