netwalker_btn.c revision 1.2.8.2 1 1.2.8.2 tls /* $NetBSD: netwalker_btn.c,v 1.2.8.2 2014/08/20 00:02:55 tls Exp $ */
2 1.2.8.2 tls
3 1.2.8.2 tls /*
4 1.2.8.2 tls * Copyright (c) 2014 Genetec Corporation. All rights reserved.
5 1.2.8.2 tls * Written by Hashimoto Kenichi for Genetec Corporation.
6 1.2.8.2 tls *
7 1.2.8.2 tls * Redistribution and use in source and binary forms, with or without
8 1.2.8.2 tls * modification, are permitted provided that the following conditions
9 1.2.8.2 tls * are met:
10 1.2.8.2 tls * 1. Redistributions of source code must retain the above copyright
11 1.2.8.2 tls * notice, this list of conditions and the following disclaimer.
12 1.2.8.2 tls * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.8.2 tls * notice, this list of conditions and the following disclaimer in the
14 1.2.8.2 tls * documentation and/or other materials provided with the distribution.
15 1.2.8.2 tls *
16 1.2.8.2 tls * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
17 1.2.8.2 tls * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.2.8.2 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.2.8.2 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION
20 1.2.8.2 tls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.2.8.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.2.8.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.2.8.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.2.8.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.2.8.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.2.8.2 tls * POSSIBILITY OF SUCH DAMAGE.
27 1.2.8.2 tls */
28 1.2.8.2 tls
29 1.2.8.2 tls #include <sys/cdefs.h>
30 1.2.8.2 tls __KERNEL_RCSID(0, "$NetBSD: netwalker_btn.c,v 1.2.8.2 2014/08/20 00:02:55 tls Exp $");
31 1.2.8.2 tls
32 1.2.8.2 tls #include "opt_imxspi.h"
33 1.2.8.2 tls #include "opt_mousebtn.h"
34 1.2.8.2 tls
35 1.2.8.2 tls #include <sys/param.h>
36 1.2.8.2 tls #include <sys/systm.h>
37 1.2.8.2 tls #include <sys/kernel.h>
38 1.2.8.2 tls #include <sys/device.h>
39 1.2.8.2 tls #include <sys/lock.h>
40 1.2.8.2 tls #include <sys/callout.h>
41 1.2.8.2 tls #include <sys/gpio.h>
42 1.2.8.2 tls #include <sys/bus.h>
43 1.2.8.2 tls #include <sys/mutex.h>
44 1.2.8.2 tls
45 1.2.8.2 tls #include <dev/wscons/wsconsio.h>
46 1.2.8.2 tls #include <dev/wscons/wsmousevar.h>
47 1.2.8.2 tls
48 1.2.8.2 tls #include <dev/gpio/gpiovar.h>
49 1.2.8.2 tls
50 1.2.8.2 tls #define GPIO1_BASE 160
51 1.2.8.2 tls
52 1.2.8.2 tls #define MOUSEBTN_PIN_LEFT 0
53 1.2.8.2 tls #define MOUSEBTN_PIN_RIGHT 1
54 1.2.8.2 tls #define MOUSEBTN_NPINS 2
55 1.2.8.2 tls
56 1.2.8.2 tls #define POLLRATE (hz/10)
57 1.2.8.2 tls
58 1.2.8.2 tls struct mousebtn_softc
59 1.2.8.2 tls {
60 1.2.8.2 tls device_t sc_dev;
61 1.2.8.2 tls void *sc_gpio;
62 1.2.8.2 tls void *sc_intr[MOUSEBTN_NPINS];
63 1.2.8.2 tls
64 1.2.8.2 tls struct gpio_pinmap sc_map;
65 1.2.8.2 tls int sc_map_pins[MOUSEBTN_NPINS];
66 1.2.8.2 tls
67 1.2.8.2 tls int sc_buttons;
68 1.2.8.2 tls
69 1.2.8.2 tls struct callout sc_c;
70 1.2.8.2 tls kmutex_t sc_lock;
71 1.2.8.2 tls
72 1.2.8.2 tls int sc_enabled;
73 1.2.8.2 tls struct device *sc_wsmousedev;
74 1.2.8.2 tls };
75 1.2.8.2 tls
76 1.2.8.2 tls static int mousebtn_match(device_t, cfdata_t, void *);
77 1.2.8.2 tls static void mousebtn_attach(device_t, device_t, void *);
78 1.2.8.2 tls static int mousebtn_detach(device_t, int);
79 1.2.8.2 tls
80 1.2.8.2 tls CFATTACH_DECL_NEW(netwalker_btn, sizeof(struct mousebtn_softc),
81 1.2.8.2 tls mousebtn_match, mousebtn_attach, mousebtn_detach, NULL);
82 1.2.8.2 tls
83 1.2.8.2 tls static void mousebtn_poll(void *);
84 1.2.8.2 tls static int mousebtn_intr(void *);
85 1.2.8.2 tls
86 1.2.8.2 tls static int mousebtn_enable(void *v);
87 1.2.8.2 tls static void mousebtn_disable(void *v);
88 1.2.8.2 tls static int mousebtn_ioctl(void *, u_long, void *, int, struct lwp *);
89 1.2.8.2 tls
90 1.2.8.2 tls static bool mousebtn_resume(device_t, const pmf_qual_t *);
91 1.2.8.2 tls static bool mousebtn_suspend(device_t, const pmf_qual_t *);
92 1.2.8.2 tls
93 1.2.8.2 tls static const struct wsmouse_accessops mousebtn_accessops = {
94 1.2.8.2 tls .enable = mousebtn_enable,
95 1.2.8.2 tls .ioctl = mousebtn_ioctl,
96 1.2.8.2 tls .disable = mousebtn_disable
97 1.2.8.2 tls };
98 1.2.8.2 tls
99 1.2.8.2 tls static int
100 1.2.8.2 tls mousebtn_match(device_t parent, cfdata_t cf, void * aux)
101 1.2.8.2 tls {
102 1.2.8.2 tls struct gpio_attach_args *ga = aux;
103 1.2.8.2 tls
104 1.2.8.2 tls if (strcmp(ga->ga_dvname, cf->cf_name))
105 1.2.8.2 tls return 0;
106 1.2.8.2 tls if (ga->ga_offset == -1)
107 1.2.8.2 tls return 0;
108 1.2.8.2 tls
109 1.2.8.2 tls /* check that we have enough pins */
110 1.2.8.2 tls if (gpio_npins(ga->ga_mask) != MOUSEBTN_NPINS) {
111 1.2.8.2 tls aprint_debug("%s: invalid pin mask 0x%02x\n", cf->cf_name,
112 1.2.8.2 tls ga->ga_mask);
113 1.2.8.2 tls return 0;
114 1.2.8.2 tls }
115 1.2.8.2 tls
116 1.2.8.2 tls return 1;
117 1.2.8.2 tls }
118 1.2.8.2 tls
119 1.2.8.2 tls static void
120 1.2.8.2 tls mousebtn_attach(device_t parent, device_t self, void *aux)
121 1.2.8.2 tls {
122 1.2.8.2 tls struct mousebtn_softc *sc = device_private(self);
123 1.2.8.2 tls struct gpio_attach_args *ga = aux;
124 1.2.8.2 tls int caps;
125 1.2.8.2 tls struct wsmousedev_attach_args a;
126 1.2.8.2 tls
127 1.2.8.2 tls sc->sc_dev = self;
128 1.2.8.2 tls sc->sc_gpio = ga->ga_gpio;
129 1.2.8.2 tls
130 1.2.8.2 tls /* map pins */
131 1.2.8.2 tls sc->sc_map.pm_map = sc->sc_map_pins;
132 1.2.8.2 tls if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
133 1.2.8.2 tls &sc->sc_map)) {
134 1.2.8.2 tls aprint_error(": couldn't map the pins\n");
135 1.2.8.2 tls return;
136 1.2.8.2 tls }
137 1.2.8.2 tls
138 1.2.8.2 tls /* configure left pin */
139 1.2.8.2 tls caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, MOUSEBTN_PIN_LEFT);
140 1.2.8.2 tls if (!(caps & GPIO_PIN_INPUT)) {
141 1.2.8.2 tls aprint_error(": pin is unable to read input\n");
142 1.2.8.2 tls gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
143 1.2.8.2 tls return;
144 1.2.8.2 tls }
145 1.2.8.2 tls gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, MOUSEBTN_PIN_LEFT,
146 1.2.8.2 tls GPIO_PIN_INPUT);
147 1.2.8.2 tls
148 1.2.8.2 tls /* configure right pin */
149 1.2.8.2 tls caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, MOUSEBTN_PIN_RIGHT);
150 1.2.8.2 tls if (!(caps & GPIO_PIN_INPUT)) {
151 1.2.8.2 tls aprint_error(": pin is unable to read input\n");
152 1.2.8.2 tls gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
153 1.2.8.2 tls return;
154 1.2.8.2 tls }
155 1.2.8.2 tls gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, MOUSEBTN_PIN_RIGHT,
156 1.2.8.2 tls GPIO_PIN_INPUT);
157 1.2.8.2 tls
158 1.2.8.2 tls /* interrupt settings */
159 1.2.8.2 tls sc->sc_intr[0] = intr_establish(GPIO1_BASE + ga->ga_offset,
160 1.2.8.2 tls IPL_VM, IST_EDGE_BOTH, mousebtn_intr, sc);
161 1.2.8.2 tls if (sc->sc_intr[0] == NULL) {
162 1.2.8.2 tls aprint_error(": couldn't establish interrupt\n");
163 1.2.8.2 tls return;
164 1.2.8.2 tls }
165 1.2.8.2 tls sc->sc_intr[1] = intr_establish(GPIO1_BASE + ga->ga_offset + 1,
166 1.2.8.2 tls IPL_VM, IST_EDGE_BOTH, mousebtn_intr, sc);
167 1.2.8.2 tls if (sc->sc_intr[1] == NULL) {
168 1.2.8.2 tls aprint_error(": couldn't establish interrupt\n");
169 1.2.8.2 tls intr_disestablish(sc->sc_intr[0]);
170 1.2.8.2 tls return;
171 1.2.8.2 tls }
172 1.2.8.2 tls
173 1.2.8.2 tls aprint_normal(": NetWalker mouse button\n");
174 1.2.8.2 tls aprint_naive(": NetWalker mouse button\n");
175 1.2.8.2 tls
176 1.2.8.2 tls mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
177 1.2.8.2 tls callout_init(&sc->sc_c, 0);
178 1.2.8.2 tls
179 1.2.8.2 tls a.accessops = &mousebtn_accessops;
180 1.2.8.2 tls a.accesscookie = sc;
181 1.2.8.2 tls
182 1.2.8.2 tls sc->sc_buttons = 0;
183 1.2.8.2 tls sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
184 1.2.8.2 tls }
185 1.2.8.2 tls
186 1.2.8.2 tls static int
187 1.2.8.2 tls mousebtn_detach(device_t self, int flags)
188 1.2.8.2 tls {
189 1.2.8.2 tls struct mousebtn_softc *sc = device_private(self);
190 1.2.8.2 tls
191 1.2.8.2 tls if (sc->sc_intr[0] != NULL)
192 1.2.8.2 tls intr_disestablish(sc->sc_intr[0]);
193 1.2.8.2 tls if (sc->sc_intr[1] != NULL)
194 1.2.8.2 tls intr_disestablish(sc->sc_intr[1]);
195 1.2.8.2 tls
196 1.2.8.2 tls gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
197 1.2.8.2 tls
198 1.2.8.2 tls return 0;
199 1.2.8.2 tls }
200 1.2.8.2 tls
201 1.2.8.2 tls static void
202 1.2.8.2 tls mousebtn_poll(void *arg)
203 1.2.8.2 tls {
204 1.2.8.2 tls struct mousebtn_softc *sc = (struct mousebtn_softc *)arg;
205 1.2.8.2 tls uint32_t buttons = 0;
206 1.2.8.2 tls int s;
207 1.2.8.2 tls int left;
208 1.2.8.2 tls int right;
209 1.2.8.2 tls
210 1.2.8.2 tls mutex_enter(&sc->sc_lock);
211 1.2.8.2 tls
212 1.2.8.2 tls left = !gpio_pin_read(sc->sc_gpio, &sc->sc_map, MOUSEBTN_PIN_LEFT);
213 1.2.8.2 tls right = !gpio_pin_read(sc->sc_gpio, &sc->sc_map, MOUSEBTN_PIN_RIGHT);
214 1.2.8.2 tls buttons = (right << 2) | left;
215 1.2.8.2 tls
216 1.2.8.2 tls if (sc->sc_buttons != buttons) {
217 1.2.8.2 tls s = spltty();
218 1.2.8.2 tls wsmouse_input(sc->sc_wsmousedev, buttons, 0, 0, 0, 0,
219 1.2.8.2 tls WSMOUSE_INPUT_DELTA);
220 1.2.8.2 tls sc->sc_buttons = buttons;
221 1.2.8.2 tls splx(s);
222 1.2.8.2 tls }
223 1.2.8.2 tls
224 1.2.8.2 tls mutex_exit(&sc->sc_lock);
225 1.2.8.2 tls
226 1.2.8.2 tls #if defined(MOUSEBTN_POLLING)
227 1.2.8.2 tls if (sc->sc_enabled)
228 1.2.8.2 tls callout_reset(&sc->sc_c, POLLRATE, mousebtn_poll, sc);
229 1.2.8.2 tls #endif
230 1.2.8.2 tls return;
231 1.2.8.2 tls }
232 1.2.8.2 tls
233 1.2.8.2 tls static int
234 1.2.8.2 tls mousebtn_intr(void *v)
235 1.2.8.2 tls {
236 1.2.8.2 tls struct mousebtn_softc *sc = v;
237 1.2.8.2 tls
238 1.2.8.2 tls if (sc->sc_enabled)
239 1.2.8.2 tls callout_reset(&sc->sc_c, POLLRATE, mousebtn_poll, sc);
240 1.2.8.2 tls
241 1.2.8.2 tls return 1;
242 1.2.8.2 tls }
243 1.2.8.2 tls
244 1.2.8.2 tls
245 1.2.8.2 tls int
246 1.2.8.2 tls mousebtn_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
247 1.2.8.2 tls {
248 1.2.8.2 tls struct wsmouse_id *id;
249 1.2.8.2 tls
250 1.2.8.2 tls switch (cmd) {
251 1.2.8.2 tls case WSMOUSEIO_GTYPE:
252 1.2.8.2 tls *(u_int *)data = WSMOUSE_TYPE_PS2;
253 1.2.8.2 tls return 0;
254 1.2.8.2 tls case WSMOUSEIO_GETID:
255 1.2.8.2 tls id = (struct wsmouse_id *)data;
256 1.2.8.2 tls if (id->type != WSMOUSE_ID_TYPE_UIDSTR)
257 1.2.8.2 tls return EINVAL;
258 1.2.8.2 tls strlcpy(id->data, "NetWalker Mouse Button", WSMOUSE_ID_MAXLEN);
259 1.2.8.2 tls id->length = strlen(id->data);
260 1.2.8.2 tls return 0;
261 1.2.8.2 tls }
262 1.2.8.2 tls
263 1.2.8.2 tls return EPASSTHROUGH;
264 1.2.8.2 tls }
265 1.2.8.2 tls
266 1.2.8.2 tls int
267 1.2.8.2 tls mousebtn_enable(void *v)
268 1.2.8.2 tls {
269 1.2.8.2 tls struct mousebtn_softc *sc = (struct mousebtn_softc *)v;
270 1.2.8.2 tls
271 1.2.8.2 tls if (sc->sc_enabled)
272 1.2.8.2 tls return EBUSY;
273 1.2.8.2 tls
274 1.2.8.2 tls if (!pmf_device_register(sc->sc_dev, mousebtn_suspend, mousebtn_resume))
275 1.2.8.2 tls aprint_error_dev(sc->sc_dev,
276 1.2.8.2 tls "couldn't establish power handler\n");
277 1.2.8.2 tls
278 1.2.8.2 tls sc->sc_enabled = 1;
279 1.2.8.2 tls #if defined(MOUSEBTN_POLLING)
280 1.2.8.2 tls callout_reset(&sc->sc_c, POLLRATE, mousebtn_poll, sc);
281 1.2.8.2 tls #endif
282 1.2.8.2 tls
283 1.2.8.2 tls return 0;
284 1.2.8.2 tls }
285 1.2.8.2 tls
286 1.2.8.2 tls void
287 1.2.8.2 tls mousebtn_disable(void *v)
288 1.2.8.2 tls {
289 1.2.8.2 tls struct mousebtn_softc *sc = (struct mousebtn_softc *)v;
290 1.2.8.2 tls
291 1.2.8.2 tls if (!sc->sc_enabled)
292 1.2.8.2 tls return;
293 1.2.8.2 tls
294 1.2.8.2 tls pmf_device_deregister(sc->sc_dev);
295 1.2.8.2 tls
296 1.2.8.2 tls sc->sc_enabled = 0;
297 1.2.8.2 tls
298 1.2.8.2 tls return;
299 1.2.8.2 tls }
300 1.2.8.2 tls
301 1.2.8.2 tls static bool
302 1.2.8.2 tls mousebtn_suspend(device_t dv, const pmf_qual_t *qual)
303 1.2.8.2 tls {
304 1.2.8.2 tls struct mousebtn_softc *sc = device_private(dv);
305 1.2.8.2 tls
306 1.2.8.2 tls #if defined(MOUSEBTN_POLLING)
307 1.2.8.2 tls callout_stop(&sc->sc_c);
308 1.2.8.2 tls #endif
309 1.2.8.2 tls sc->sc_enabled = 0;
310 1.2.8.2 tls
311 1.2.8.2 tls return true;
312 1.2.8.2 tls }
313 1.2.8.2 tls
314 1.2.8.2 tls static bool
315 1.2.8.2 tls mousebtn_resume(device_t dv, const pmf_qual_t *qual)
316 1.2.8.2 tls {
317 1.2.8.2 tls struct mousebtn_softc *sc = device_private(dv);
318 1.2.8.2 tls
319 1.2.8.2 tls sc->sc_enabled = 1;
320 1.2.8.2 tls #if defined(MOUSEBTN_POLLING)
321 1.2.8.2 tls callout_reset(&sc->sc_c, POLLRATE, mousebtn_poll, sc);
322 1.2.8.2 tls #endif
323 1.2.8.2 tls return true;
324 1.2.8.2 tls }
325 1.2.8.2 tls
326