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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
/* $NetBSD: btnmgr.c,v 1.31 2021/08/07 16:19:11 thorpej Exp $ */
/*-
* Copyright (c) 1999
* Shin Takemura and PocketBSD Project. 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the PocketBSD project
* and its contributors.
* 4. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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: btnmgr.c,v 1.31 2021/08/07 16:19:11 thorpej Exp $");
#ifdef _KERNEL_OPT
#include "opt_btnmgr.h"
#include "opt_wsdisplay_compat.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/ioctl.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wskbdvar.h>
#include <dev/wscons/wsksymdef.h>
#ifdef WSDISPLAY_COMPAT_RAWKBD
#include <dev/hpc/pckbd_encode.h>
#endif
#include <sys/bus.h>
#include <machine/autoconf.h>
#include <machine/config_hook.h>
#ifdef BTNMGRDEBUG
#ifndef BTNMGRDEBUG_CONF
#define BTNMGRDEBUG_CONF 0
#endif
int btnmgr_debug = BTNMGRDEBUG_CONF;
#define DPRINTF(arg) if (btnmgr_debug) printf arg;
#define DPRINTFN(n, arg) if (btnmgr_debug > (n)) printf arg;
#else
#define DPRINTF(arg)
#define DPRINTFN(n, arg)
#endif
struct btnmgr_softc {
config_hook_tag sc_hook_tag;
int sc_enabled;
device_t sc_wskbddev;
#ifdef WSDISPLAY_COMPAT_RAWKBD
int sc_rawkbd;
#endif
};
int btnmgrmatch(device_t, cfdata_t, void *);
void btnmgrattach(device_t, device_t, void *);
const char *btnmgr_name(long);
static int btnmgr_hook(void *, int, long, void *);
/*
* global/static data
*/
CFATTACH_DECL_NEW(btnmgr, sizeof(struct btnmgr_softc),
btnmgrmatch, btnmgrattach, NULL, NULL);
#ifdef notyet
dev_type_open(btnmgropen);
dev_type_close(btnmgrclose);
dev_type_read(btnmgrread);
dev_type_write(btnmgrwrite);
dev_type_ioctl(btnmgrioctl);
const struct cdevsw btnmgr_cdevsw = {
.d_open = btnmgropen,
.d_close = btnmgrclose,
.d_read = btnmgrread,
.d_write = btnmgrwrite,
.d_ioctl = btnmgrioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = 0
};
#endif /* notyet */
/* wskbd accessopts */
int btnmgr_wskbd_enable(void *, int);
void btnmgr_wskbd_set_leds(void *, int);
int btnmgr_wskbd_ioctl(void *, u_long, void *, int, struct lwp *);
const struct wskbd_accessops btnmgr_wskbd_accessops = {
btnmgr_wskbd_enable,
btnmgr_wskbd_set_leds,
btnmgr_wskbd_ioctl,
};
/* button config: index by button event id */
static const struct {
int kevent;
int keycode;
const char *name;
} button_config[] = {
/* id kevent keycode name */
[CONFIG_HOOK_BUTTONEVENT_POWER] = { 0, 0, "Power" },
[CONFIG_HOOK_BUTTONEVENT_OK] = { 1, 28, "OK" },
[CONFIG_HOOK_BUTTONEVENT_CANCEL] = { 1, 1, "Cancel" },
[CONFIG_HOOK_BUTTONEVENT_UP] = { 1, 72, "Up" },
[CONFIG_HOOK_BUTTONEVENT_DOWN] = { 1, 80, "Down" },
[CONFIG_HOOK_BUTTONEVENT_REC] = { 0, 0, "Rec" },
[CONFIG_HOOK_BUTTONEVENT_COVER] = { 0, 0, "Cover" },
[CONFIG_HOOK_BUTTONEVENT_LIGHT] = { 1, 57, "Light" },
[CONFIG_HOOK_BUTTONEVENT_CONTRAST] = { 0, 0, "Contrast" },
[CONFIG_HOOK_BUTTONEVENT_APP0] = { 1, 67, "Application 0" },
[CONFIG_HOOK_BUTTONEVENT_APP1] = { 1, 68, "Application 1" },
[CONFIG_HOOK_BUTTONEVENT_APP2] = { 1, 87, "Application 2" },
[CONFIG_HOOK_BUTTONEVENT_APP3] = { 1, 88, "Application 3" },
};
static const int n_button_config =
sizeof(button_config) / sizeof(*button_config);
#define KC(n) KS_KEYCODE(n)
static const keysym_t btnmgr_keydesc_default[] = {
/* pos normal shifted */
KC(1), KS_Escape,
KC(28), KS_Return,
KC(57), KS_Cmd, KS_Cmd_BacklightToggle,
KC(67), KS_f9,
KC(68), KS_f10,
KC(72), KS_KP_Up,
KC(80), KS_KP_Down,
KC(87), KS_f11,
KC(88), KS_f12,
};
#undef KC
#define KBD_MAP(name, base, map) \
{ name, base, sizeof(map)/sizeof(keysym_t), map }
const struct wscons_keydesc btnmgr_keydesctab[] = {
KBD_MAP(KB_US, 0, btnmgr_keydesc_default),
{0, 0, 0, 0}
};
#undef KBD_MAP
struct wskbd_mapdata btnmgr_keymapdata = {
btnmgr_keydesctab,
KB_US, /* XXX, This is bad idea... */
};
/*
* function bodies
*/
int
btnmgrmatch(device_t parent, cfdata_t match, void *aux)
{
struct mainbus_attach_args *ma = aux;
if (strcmp(ma->ma_name, match->cf_name))
return 0;
return (1);
}
void
btnmgrattach(device_t parent,
device_t self, void *aux)
{
int id;
struct btnmgr_softc *sc = device_private(self);
struct wskbddev_attach_args wa;
printf("\n");
/*
* install button event listener
*/
for (id = 0; id < n_button_config; id++)
if (button_config[id].name != NULL)
sc->sc_hook_tag = config_hook(CONFIG_HOOK_BUTTONEVENT,
id, CONFIG_HOOK_SHARE,
btnmgr_hook, sc);
/*
* attach wskbd
*/
wa.console = 0;
wa.keymap = &btnmgr_keymapdata;
wa.accessops = &btnmgr_wskbd_accessops;
wa.accesscookie = sc;
sc->sc_wskbddev = config_found(self, &wa, wskbddevprint, CFARGS_NONE);
if (!pmf_device_register(self, NULL, NULL))
aprint_error_dev(self, "unable to establish power handler\n");
}
static int
btnmgr_hook(void *ctx, int type, long id, void *msg)
{
struct btnmgr_softc *sc = ctx;
DPRINTF(("%s button: %s\n", btnmgr_name(id), msg ? "ON" : "OFF"));
if (button_config[id].kevent) {
u_int evtype;
evtype = msg ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
#ifdef WSDISPLAY_COMPAT_RAWKBD
if (sc->sc_rawkbd) {
int n;
u_char data[16];
n = pckbd_encode(evtype, button_config[id].keycode,
data);
wskbd_rawinput(sc->sc_wskbddev, data, n);
} else
#endif
wskbd_input(sc->sc_wskbddev, evtype,
button_config[id].keycode);
}
if (id == CONFIG_HOOK_BUTTONEVENT_POWER && msg)
config_hook_call(CONFIG_HOOK_PMEVENT,
CONFIG_HOOK_PMEVENT_SUSPENDREQ, NULL);
else if (id == CONFIG_HOOK_BUTTONEVENT_COVER)
config_hook_call(CONFIG_HOOK_POWERCONTROL,
CONFIG_HOOK_POWERCONTROL_LCDLIGHT, (void*)(msg ? 0: 1));
return (0);
}
const char *
btnmgr_name(long id)
{
if (id < n_button_config)
return (button_config[id].name);
return ("unknown");
}
int
btnmgr_wskbd_enable(void *scx, int on)
{
struct btnmgr_softc *sc = scx;
if (on) {
if (sc->sc_enabled)
return (EBUSY);
sc->sc_enabled = 1;
} else {
sc->sc_enabled = 0;
}
return (0);
}
void
btnmgr_wskbd_set_leds(void *scx, int leds)
{
/*
* We have nothing to do.
*/
}
int
btnmgr_wskbd_ioctl(void *scx, u_long cmd, void *data, int flag,
struct lwp *l)
{
#ifdef WSDISPLAY_COMPAT_RAWKBD
struct btnmgr_softc *sc = scx;
#endif
switch (cmd) {
case WSKBDIO_GTYPE:
*(int *)data = WSKBD_TYPE_HPC_BTN;
return (0);
case WSKBDIO_SETLEDS:
DPRINTF(("%s(%d): no LED\n", __FILE__, __LINE__));
return (0);
case WSKBDIO_GETLEDS:
DPRINTF(("%s(%d): no LED\n", __FILE__, __LINE__));
*(int *)data = 0;
return (0);
#ifdef WSDISPLAY_COMPAT_RAWKBD
case WSKBDIO_SETMODE:
sc->sc_rawkbd = (*(int *)data == WSKBD_RAW);
DPRINTF(("%s(%d): rawkbd is %s\n", __FILE__, __LINE__,
sc->sc_rawkbd ? "on" : "off"));
return (0);
#endif
}
return (EPASSTHROUGH);
}
#ifdef notyet
int
btnmgropen(dev_t dev, int flag, int mode, struct lwp *l)
{
return (EINVAL);
}
int
btnmgrclose(dev_t dev, int flag, int mode, struct lwp *l)
{
return (EINVAL);
}
int
btnmgrread(dev_t dev, struct uio *uio, int flag)
{
return (EINVAL);
}
int
btnmgrwrite(dev_t dev, struct uio *uio, int flag)
{
return (EINVAL);
}
int
btnmgrioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
return (EINVAL);
}
#endif /* notyet */
|