summaryrefslogtreecommitdiff
path: root/sys/dev/fdt/gpiokeys.c
blob: ef3d34cf22b5ed3ab316fa11586bbfaa7143d705 (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
/* $NetBSD: gpiokeys.c,v 1.11 2021/08/07 16:19:10 thorpej Exp $ */

/*-
 * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
 * All rights reserved.
 *
 * 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 THE AUTHOR ``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 THE AUTHOR 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: gpiokeys.c,v 1.11 2021/08/07 16:19:10 thorpej Exp $");

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kmem.h>
#include <sys/bus.h>
#include <sys/gpio.h>

#include <dev/sysmon/sysmonvar.h>
#include <dev/sysmon/sysmon_taskq.h>

#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wskbdvar.h>
#include <dev/wscons/wsksymdef.h>
#include <dev/wscons/wsksymvar.h>
#include <dev/wscons/linux_keymap.h>

#include <dev/fdt/fdtvar.h>

#define GPIOKEYS_POLL_INTERVAL	mstohz(200)

/* Event types */
#define	EV_KEY		1
#define	EV_SW		5

/* Key and button events */
#define	KEY_POWER	116
#define	KEY_SLEEP	142

/* Switch events */
#define	SW_LID		0

static int	gpiokeys_match(device_t, cfdata_t, void *);
static void	gpiokeys_attach(device_t, device_t, void *);

static void	gpiokeys_tick(void *);
static void	gpiokeys_task(void *);

extern const struct wscons_keydesc hidkbd_keydesctab[];
static const struct wskbd_mapdata gpiokeys_keymapdata = {
	hidkbd_keydesctab,
	KB_US,
};

struct gpiokeys_softc;

struct gpiokeys_key {
	struct gpiokeys_softc	*key_sc;
	int			key_phandle;
	char	 		*key_label;
	struct fdtbus_gpio_pin	*key_pin;
	u_int			key_debounce;
	u_int			key_code;
	struct sysmon_pswitch	key_pswitch;
	uint8_t			key_usbcode;
	int			key_state;

	struct gpiokeys_key	*key_next;
};

struct gpiokeys_softc {
	device_t	sc_dev;
	int		sc_phandle;

	struct fdtbus_gpio_pin *sc_pin;
	bool		sc_always_on;
	bool		sc_enable_val;

	struct gpiokeys_key *sc_keys;
	callout_t	sc_tick;

	device_t	sc_wskbddev;
	int		sc_enabled;
};

CFATTACH_DECL_NEW(gpiokeys, sizeof(struct gpiokeys_softc),
    gpiokeys_match, gpiokeys_attach, NULL, NULL);

static int
gpiokeys_enable(void *v, int on)
{
	struct gpiokeys_softc * const sc = v;

	sc->sc_enabled = on;

	return 0;
}

static void
gpiokeys_set_leds(void *v, int leds)
{
}

static int
gpiokeys_ioctl(void *v, u_long cmd, void *data, int flag, lwp_t *l)
{
	switch (cmd) {
	case WSKBDIO_GTYPE:
		*(int *)data = WSKBD_TYPE_USB;
		return 0;
	}

	return EPASSTHROUGH;
}

static const struct wskbd_accessops gpiokeys_accessops = {
	.enable = gpiokeys_enable,
	.set_leds = gpiokeys_set_leds,
	.ioctl = gpiokeys_ioctl
};

static const struct device_compatible_entry compat_data[] = {
	{ .compat = "gpio-keys" },
	DEVICE_COMPAT_EOL
};

static int
gpiokeys_match(device_t parent, cfdata_t cf, void *aux)
{
	const struct fdt_attach_args *faa = aux;

	return of_compatible_match(faa->faa_phandle, compat_data);
}

static void
gpiokeys_attach(device_t parent, device_t self, void *aux)
{
	struct gpiokeys_softc * const sc = device_private(self);
	const struct fdt_attach_args *faa = aux;
	const int phandle = faa->faa_phandle;
	struct gpiokeys_key *key;
	u_int debounce, input_type, code;
	int use_wskbddev = 0;
	int child, len;

	sc->sc_dev = self;
	sc->sc_phandle = phandle;

	aprint_naive("\n");
	aprint_normal(":");

	for (child = OF_child(phandle); child; child = OF_peer(child)) {
		if (of_getprop_uint32(child, "linux,input-type", &input_type))
			input_type = EV_KEY;	/* default */
		if (of_getprop_uint32(child, "linux,code", &code))
			continue;
		if (of_getprop_uint32(child, "debounce-interval", &debounce))
			debounce = 5;	/* default */
		len = OF_getproplen(child, "label");
		if (len <= 0) {
			continue;
		}
		key = kmem_zalloc(sizeof(*key), KM_SLEEP);
		key->key_sc = sc;
		key->key_phandle = child;
		key->key_code = code;
		key->key_label = kmem_zalloc(len, KM_SLEEP);
		if (OF_getprop(child, "label", key->key_label, len) != len) {
			kmem_free(key->key_label, len);
			kmem_free(key, sizeof(*key));
			continue;
		}
		key->key_debounce = debounce;
		key->key_pin = fdtbus_gpio_acquire(child, "gpios",
		    GPIO_PIN_INPUT);
		if (key->key_pin)
			key->key_state = fdtbus_gpio_read(key->key_pin);

		switch (input_type) {
		case EV_KEY:
			switch (code) {
			case KEY_POWER:
				key->key_pswitch.smpsw_name = key->key_label;
				key->key_pswitch.smpsw_type = PSWITCH_TYPE_POWER;
				break;
			case KEY_SLEEP:
				key->key_pswitch.smpsw_name = key->key_label;
				key->key_pswitch.smpsw_type = PSWITCH_TYPE_SLEEP;
				break;
			default:
				key->key_usbcode = linux_key_to_usb(code);
				if (key->key_usbcode != 0) {
					use_wskbddev++;
				} else {
					key->key_pswitch.smpsw_name = key->key_label;
					key->key_pswitch.smpsw_type = PSWITCH_TYPE_HOTKEY;
				}
				break;
			}
			break;
		case EV_SW:
			key->key_pswitch.smpsw_name = key->key_label;
			switch (code) {
			case SW_LID:
				key->key_state = -1;	/* Send notification on attach */
				key->key_pswitch.smpsw_type = PSWITCH_TYPE_LID;
				break;
			default:
				key->key_pswitch.smpsw_type = PSWITCH_TYPE_HOTKEY;
				break;
			}
			break;
		}

		if (key->key_pswitch.smpsw_name != NULL &&
		    sysmon_pswitch_register(&key->key_pswitch) != 0) {
			aprint_error(" %s:ERROR", key->key_label);
			kmem_free(key->key_label, len);
			kmem_free(key, sizeof(*key));
			continue;
		}

		if (sc->sc_keys) {
			aprint_normal(", %s", key->key_label);
		} else {
			aprint_normal(" %s", key->key_label);
		}

		key->key_next = sc->sc_keys;
		sc->sc_keys = key;
	}

	if (sc->sc_keys == NULL) {
		aprint_normal(" no keys configured\n");
		return;
	}

	aprint_normal("\n");

	if (use_wskbddev > 0) {
		struct wskbddev_attach_args a;
		memset(&a, 0, sizeof(a));
		a.console = false;
		a.keymap = &gpiokeys_keymapdata;
		a.accessops = &gpiokeys_accessops;
		a.accesscookie = sc;
		sc->sc_wskbddev =
		    config_found(self, &a, wskbddevprint, CFARGS_NONE);
	}

	callout_init(&sc->sc_tick, CALLOUT_MPSAFE);
	callout_setfunc(&sc->sc_tick, gpiokeys_tick, sc);

	gpiokeys_tick(sc);
}

static void
gpiokeys_tick(void *priv)
{
	struct gpiokeys_softc * const sc = priv;
	struct gpiokeys_key *key;

	for (key = sc->sc_keys; key; key = key->key_next) {
		if (key->key_pin == NULL) {
			continue;
		}
		const int new_state = fdtbus_gpio_read(key->key_pin);
		if (new_state != key->key_state) {
			key->key_state = new_state;
			sysmon_task_queue_sched(0, gpiokeys_task, key);
		}
	}
	callout_schedule(&sc->sc_tick, GPIOKEYS_POLL_INTERVAL);
}

static void
gpiokeys_task(void *priv)
{
	struct gpiokeys_key *key = priv;
	struct gpiokeys_softc *sc = key->key_sc;

	if (key->key_pswitch.smpsw_name) {
		sysmon_pswitch_event(&key->key_pswitch,
		    key->key_state ? PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
	} else if (sc->sc_enabled && sc->sc_wskbddev != NULL && key->key_usbcode != 0) {
		wskbd_input(sc->sc_wskbddev,
		    key->key_state ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP,
		    key->key_usbcode);
	}
}