summaryrefslogtreecommitdiff
path: root/sys/arch/evbarm/netwalker/netwalker_btn.c
blob: adf060f12f67ecf08e8b23d512ff09f06325dc65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*	$NetBSD: netwalker_btn.c,v 1.4 2021/08/07 16:18:50 thorpej Exp $	*/

/*
 * Copyright (c) 2014  Genetec Corporation.  All rights reserved.
 * Written by Hashimoto Kenichi for Genetec Corporation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: netwalker_btn.c,v 1.4 2021/08/07 16:18:50 thorpej Exp $");

#include "opt_imxspi.h"
#include "opt_mousebtn.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/lock.h>
#include <sys/callout.h>
#include <sys/gpio.h>
#include <sys/bus.h>
#include <sys/mutex.h>

#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsmousevar.h>

#include <dev/gpio/gpiovar.h>

#define GPIO1_BASE		160

#define MOUSEBTN_PIN_LEFT	0
#define MOUSEBTN_PIN_RIGHT	1
#define MOUSEBTN_NPINS		2

#define POLLRATE (hz/10)

struct mousebtn_softc
{
	device_t sc_dev;
	void *sc_gpio;
	void *sc_intr[MOUSEBTN_NPINS];

	struct gpio_pinmap sc_map;
	int sc_map_pins[MOUSEBTN_NPINS];

	int sc_buttons;

	struct callout sc_c;
	kmutex_t sc_lock;

	int sc_enabled;
	struct device *sc_wsmousedev;
};

static int mousebtn_match(device_t, cfdata_t, void *);
static void mousebtn_attach(device_t, device_t, void *);
static int mousebtn_detach(device_t, int);

CFATTACH_DECL_NEW(netwalker_btn, sizeof(struct mousebtn_softc),
    mousebtn_match, mousebtn_attach, mousebtn_detach, NULL);

static void mousebtn_poll(void *);
static int mousebtn_intr(void *);

static int mousebtn_enable(void *v);
static void mousebtn_disable(void *v);
static int mousebtn_ioctl(void *, u_long, void *, int, struct lwp *);

static bool mousebtn_resume(device_t, const pmf_qual_t *);
static bool mousebtn_suspend(device_t, const pmf_qual_t *);

static const struct wsmouse_accessops mousebtn_accessops = {
	.enable	 = mousebtn_enable,
	.ioctl	 = mousebtn_ioctl,
	.disable = mousebtn_disable
};

static int
mousebtn_match(device_t parent, cfdata_t cf, void * aux)
{
	struct gpio_attach_args *ga = aux;

	if (strcmp(ga->ga_dvname, cf->cf_name))
		return 0;
	if (ga->ga_offset == -1)
		return 0;

	/* check that we have enough pins */
	if (gpio_npins(ga->ga_mask) != MOUSEBTN_NPINS) {
		aprint_debug("%s: invalid pin mask 0x%02x\n", cf->cf_name,
		    ga->ga_mask);
		return 0;
	}

	return 1;
}

static void
mousebtn_attach(device_t parent, device_t self, void *aux)
{
	struct mousebtn_softc *sc = device_private(self);
	struct gpio_attach_args *ga = aux;
	int caps;
	struct wsmousedev_attach_args a;

	sc->sc_dev = self;
	sc->sc_gpio = ga->ga_gpio;

	/* map pins */
	sc->sc_map.pm_map = sc->sc_map_pins;
	if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
		&sc->sc_map)) {
		aprint_error(": couldn't map the pins\n");
		return;
	}

	/* configure left pin */
	caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, MOUSEBTN_PIN_LEFT);
	if (!(caps & GPIO_PIN_INPUT)) {
		aprint_error(": pin is unable to read input\n");
		gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
		return;
	}
	gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, MOUSEBTN_PIN_LEFT,
	    GPIO_PIN_INPUT);

	/* configure right pin */
	caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, MOUSEBTN_PIN_RIGHT);
	if (!(caps & GPIO_PIN_INPUT)) {
		aprint_error(": pin is unable to read input\n");
		gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
		return;
	}
	gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, MOUSEBTN_PIN_RIGHT,
	    GPIO_PIN_INPUT);

	/* interrupt settings */
	sc->sc_intr[0] = intr_establish(GPIO1_BASE + ga->ga_offset,
	    IPL_VM, IST_EDGE_BOTH, mousebtn_intr, sc);
	if (sc->sc_intr[0] == NULL) {
		aprint_error(": couldn't establish interrupt\n");
		return;
	}
	sc->sc_intr[1] = intr_establish(GPIO1_BASE + ga->ga_offset + 1,
	     IPL_VM, IST_EDGE_BOTH, mousebtn_intr, sc);
	if (sc->sc_intr[1] == NULL) {
		aprint_error(": couldn't establish interrupt\n");
		intr_disestablish(sc->sc_intr[0]);
		return;
	}

	aprint_normal(": NetWalker mouse button\n");
	aprint_naive(": NetWalker mouse button\n");

	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
	callout_init(&sc->sc_c, 0);

	a.accessops = &mousebtn_accessops;
	a.accesscookie = sc;

	sc->sc_buttons = 0;
	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint, CFARGS_NONE);
}

static int
mousebtn_detach(device_t self, int flags)
{
	struct mousebtn_softc *sc = device_private(self);

	if (sc->sc_intr[0] != NULL)
		intr_disestablish(sc->sc_intr[0]);
	if (sc->sc_intr[1] != NULL)
		intr_disestablish(sc->sc_intr[1]);

	gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);

	return 0;
}

static void
mousebtn_poll(void *arg)
{
	struct mousebtn_softc *sc = (struct mousebtn_softc *)arg;
	uint32_t buttons = 0;
	int s;
	int left;
	int right;

	mutex_enter(&sc->sc_lock);

	left = !gpio_pin_read(sc->sc_gpio, &sc->sc_map, MOUSEBTN_PIN_LEFT);
	right = !gpio_pin_read(sc->sc_gpio, &sc->sc_map, MOUSEBTN_PIN_RIGHT);
	buttons = (right << 2) | left;

	if (sc->sc_buttons != buttons) {
		s = spltty();
		wsmouse_input(sc->sc_wsmousedev, buttons, 0, 0, 0, 0,
		    WSMOUSE_INPUT_DELTA);
		sc->sc_buttons = buttons;
		splx(s);
	}

	mutex_exit(&sc->sc_lock);

#if defined(MOUSEBTN_POLLING)
	if (sc->sc_enabled)
		callout_reset(&sc->sc_c, POLLRATE, mousebtn_poll, sc);
#endif
	return;
}

static int
mousebtn_intr(void *v)
{
	struct mousebtn_softc *sc = v;

	if (sc->sc_enabled)
		callout_reset(&sc->sc_c, POLLRATE, mousebtn_poll, sc);

	return 1;
}


int
mousebtn_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
{
	struct wsmouse_id *id;

	switch (cmd) {
	case WSMOUSEIO_GTYPE:
		*(u_int *)data = WSMOUSE_TYPE_PS2;
		return 0;
	case WSMOUSEIO_GETID:
		id = (struct wsmouse_id *)data;
		if (id->type != WSMOUSE_ID_TYPE_UIDSTR)
			return EINVAL;
		strlcpy(id->data, "NetWalker Mouse Button", WSMOUSE_ID_MAXLEN);
		id->length = strlen(id->data);
		return 0;
	}

	return EPASSTHROUGH;
}

int
mousebtn_enable(void *v)
{
	struct mousebtn_softc *sc = (struct mousebtn_softc *)v;

	if (sc->sc_enabled)
		return EBUSY;

	if (!pmf_device_register(sc->sc_dev, mousebtn_suspend, mousebtn_resume))
		aprint_error_dev(sc->sc_dev,
		    "couldn't establish power handler\n");

	sc->sc_enabled = 1;
#if defined(MOUSEBTN_POLLING)
	callout_reset(&sc->sc_c, POLLRATE, mousebtn_poll, sc);
#endif

	return 0;
}

void
mousebtn_disable(void *v)
{
	struct mousebtn_softc *sc = (struct mousebtn_softc *)v;

	if (!sc->sc_enabled)
		return;

	pmf_device_deregister(sc->sc_dev);

	sc->sc_enabled = 0;

	return;
}

static bool
mousebtn_suspend(device_t dv, const pmf_qual_t *qual)
{
	struct mousebtn_softc *sc = device_private(dv);

#if defined(MOUSEBTN_POLLING)
	callout_stop(&sc->sc_c);
#endif
	sc->sc_enabled = 0;

	return true;
}

static bool
mousebtn_resume(device_t dv, const pmf_qual_t *qual)
{
	struct mousebtn_softc *sc = device_private(dv);

	sc->sc_enabled = 1;
#if defined(MOUSEBTN_POLLING)
	callout_reset(&sc->sc_c, POLLRATE, mousebtn_poll, sc);
#endif
	return true;
}