btnmgr.c revision 1.29 1 1.29 skrll /* $NetBSD: btnmgr.c,v 1.29 2016/09/06 06:27:17 skrll Exp $ */
2 1.1 uch
3 1.1 uch /*-
4 1.1 uch * Copyright (c) 1999
5 1.1 uch * Shin Takemura and PocketBSD Project. All rights reserved.
6 1.1 uch *
7 1.1 uch * Redistribution and use in source and binary forms, with or without
8 1.1 uch * modification, are permitted provided that the following conditions
9 1.1 uch * are met:
10 1.1 uch * 1. Redistributions of source code must retain the above copyright
11 1.1 uch * notice, this list of conditions and the following disclaimer.
12 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 uch * notice, this list of conditions and the following disclaimer in the
14 1.1 uch * documentation and/or other materials provided with the distribution.
15 1.1 uch * 3. All advertising materials mentioning features or use of this software
16 1.1 uch * must display the following acknowledgement:
17 1.1 uch * This product includes software developed by the PocketBSD project
18 1.1 uch * and its contributors.
19 1.1 uch * 4. Neither the name of the project nor the names of its contributors
20 1.1 uch * may be used to endorse or promote products derived from this software
21 1.1 uch * without specific prior written permission.
22 1.1 uch *
23 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 uch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 uch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 uch * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 uch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 uch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 uch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 uch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 uch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 uch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 uch * SUCH DAMAGE.
34 1.1 uch *
35 1.1 uch */
36 1.3 lukem
37 1.3 lukem #include <sys/cdefs.h>
38 1.29 skrll __KERNEL_RCSID(0, "$NetBSD: btnmgr.c,v 1.29 2016/09/06 06:27:17 skrll Exp $");
39 1.3 lukem
40 1.18 peter #ifdef _KERNEL_OPT
41 1.18 peter #include "opt_btnmgr.h"
42 1.18 peter #include "opt_wsdisplay_compat.h"
43 1.18 peter #endif
44 1.1 uch
45 1.1 uch #include <sys/param.h>
46 1.1 uch #include <sys/systm.h>
47 1.1 uch #include <sys/ioctl.h>
48 1.1 uch #include <sys/conf.h>
49 1.1 uch #include <sys/device.h>
50 1.1 uch #include <sys/malloc.h>
51 1.1 uch #include <sys/kernel.h>
52 1.1 uch
53 1.1 uch #include <dev/wscons/wsconsio.h>
54 1.1 uch #include <dev/wscons/wskbdvar.h>
55 1.1 uch #include <dev/wscons/wsksymdef.h>
56 1.1 uch #ifdef WSDISPLAY_COMPAT_RAWKBD
57 1.1 uch #include <dev/hpc/pckbd_encode.h>
58 1.1 uch #endif
59 1.1 uch
60 1.22 ad #include <sys/bus.h>
61 1.1 uch #include <machine/autoconf.h>
62 1.1 uch #include <machine/config_hook.h>
63 1.1 uch
64 1.1 uch #ifdef BTNMGRDEBUG
65 1.1 uch #ifndef BTNMGRDEBUG_CONF
66 1.1 uch #define BTNMGRDEBUG_CONF 0
67 1.1 uch #endif
68 1.1 uch int btnmgr_debug = BTNMGRDEBUG_CONF;
69 1.1 uch #define DPRINTF(arg) if (btnmgr_debug) printf arg;
70 1.1 uch #define DPRINTFN(n, arg) if (btnmgr_debug > (n)) printf arg;
71 1.1 uch #else
72 1.1 uch #define DPRINTF(arg)
73 1.1 uch #define DPRINTFN(n, arg)
74 1.1 uch #endif
75 1.1 uch
76 1.1 uch struct btnmgr_softc {
77 1.1 uch config_hook_tag sc_hook_tag;
78 1.1 uch int sc_enabled;
79 1.25 cegger device_t sc_wskbddev;
80 1.1 uch #ifdef WSDISPLAY_COMPAT_RAWKBD
81 1.1 uch int sc_rawkbd;
82 1.1 uch #endif
83 1.1 uch };
84 1.1 uch
85 1.25 cegger int btnmgrmatch(device_t, cfdata_t, void *);
86 1.25 cegger void btnmgrattach(device_t, device_t, void *);
87 1.14 uwe const char *btnmgr_name(long);
88 1.2 uch static int btnmgr_hook(void *, int, long, void *);
89 1.1 uch
90 1.1 uch /*
91 1.1 uch * global/static data
92 1.1 uch */
93 1.26 chs CFATTACH_DECL_NEW(btnmgr, sizeof(struct btnmgr_softc),
94 1.10 thorpej btnmgrmatch, btnmgrattach, NULL, NULL);
95 1.6 gehenna
96 1.6 gehenna #ifdef notyet
97 1.6 gehenna dev_type_open(btnmgropen);
98 1.6 gehenna dev_type_close(btnmgrclose);
99 1.6 gehenna dev_type_read(btnmgrread);
100 1.6 gehenna dev_type_write(btnmgrwrite);
101 1.6 gehenna dev_type_ioctl(btnmgrioctl);
102 1.6 gehenna
103 1.6 gehenna const struct cdevsw btnmgr_cdevsw = {
104 1.27 dholland .d_open = btnmgropen,
105 1.27 dholland .d_close = btnmgrclose,
106 1.27 dholland .d_read = btnmgrread,
107 1.27 dholland .d_write = btnmgrwrite,
108 1.27 dholland .d_ioctl = btnmgrioctl,
109 1.27 dholland .d_stop = nostop,
110 1.27 dholland .d_tty = notty,
111 1.27 dholland .d_poll = nopoll,
112 1.27 dholland .d_mmap = nommap,
113 1.27 dholland .d_kqfilter = nokqfilter,
114 1.28 dholland .d_discard = nodiscard,
115 1.27 dholland .d_flag = 0
116 1.6 gehenna };
117 1.6 gehenna #endif /* notyet */
118 1.1 uch
119 1.1 uch /* wskbd accessopts */
120 1.2 uch int btnmgr_wskbd_enable(void *, int);
121 1.2 uch void btnmgr_wskbd_set_leds(void *, int);
122 1.21 christos int btnmgr_wskbd_ioctl(void *, u_long, void *, int, struct lwp *);
123 1.1 uch
124 1.1 uch const struct wskbd_accessops btnmgr_wskbd_accessops = {
125 1.1 uch btnmgr_wskbd_enable,
126 1.1 uch btnmgr_wskbd_set_leds,
127 1.1 uch btnmgr_wskbd_ioctl,
128 1.1 uch };
129 1.1 uch
130 1.17 peter /* button config: index by button event id */
131 1.1 uch static const struct {
132 1.1 uch int kevent;
133 1.1 uch int keycode;
134 1.14 uwe const char *name;
135 1.1 uch } button_config[] = {
136 1.1 uch /* id kevent keycode name */
137 1.1 uch [CONFIG_HOOK_BUTTONEVENT_POWER] = { 0, 0, "Power" },
138 1.1 uch [CONFIG_HOOK_BUTTONEVENT_OK] = { 1, 28, "OK" },
139 1.1 uch [CONFIG_HOOK_BUTTONEVENT_CANCEL] = { 1, 1, "Cancel" },
140 1.1 uch [CONFIG_HOOK_BUTTONEVENT_UP] = { 1, 72, "Up" },
141 1.1 uch [CONFIG_HOOK_BUTTONEVENT_DOWN] = { 1, 80, "Down" },
142 1.1 uch [CONFIG_HOOK_BUTTONEVENT_REC] = { 0, 0, "Rec" },
143 1.1 uch [CONFIG_HOOK_BUTTONEVENT_COVER] = { 0, 0, "Cover" },
144 1.1 uch [CONFIG_HOOK_BUTTONEVENT_LIGHT] = { 1, 57, "Light" },
145 1.1 uch [CONFIG_HOOK_BUTTONEVENT_CONTRAST] = { 0, 0, "Contrast" },
146 1.1 uch [CONFIG_HOOK_BUTTONEVENT_APP0] = { 1, 67, "Application 0" },
147 1.1 uch [CONFIG_HOOK_BUTTONEVENT_APP1] = { 1, 68, "Application 1" },
148 1.1 uch [CONFIG_HOOK_BUTTONEVENT_APP2] = { 1, 87, "Application 2" },
149 1.1 uch [CONFIG_HOOK_BUTTONEVENT_APP3] = { 1, 88, "Application 3" },
150 1.1 uch };
151 1.13 perry static const int n_button_config =
152 1.1 uch sizeof(button_config) / sizeof(*button_config);
153 1.1 uch
154 1.1 uch #define KC(n) KS_KEYCODE(n)
155 1.1 uch static const keysym_t btnmgr_keydesc_default[] = {
156 1.1 uch /* pos normal shifted */
157 1.2 uch KC(1), KS_Escape,
158 1.2 uch KC(28), KS_Return,
159 1.2 uch KC(57), KS_Cmd, KS_Cmd_BacklightToggle,
160 1.2 uch KC(67), KS_f9,
161 1.2 uch KC(68), KS_f10,
162 1.2 uch KC(72), KS_KP_Up,
163 1.2 uch KC(80), KS_KP_Down,
164 1.2 uch KC(87), KS_f11,
165 1.2 uch KC(88), KS_f12,
166 1.1 uch };
167 1.1 uch #undef KC
168 1.1 uch #define KBD_MAP(name, base, map) \
169 1.1 uch { name, base, sizeof(map)/sizeof(keysym_t), map }
170 1.1 uch const struct wscons_keydesc btnmgr_keydesctab[] = {
171 1.1 uch KBD_MAP(KB_US, 0, btnmgr_keydesc_default),
172 1.1 uch {0, 0, 0, 0}
173 1.1 uch };
174 1.1 uch #undef KBD_MAP
175 1.1 uch
176 1.1 uch struct wskbd_mapdata btnmgr_keymapdata = {
177 1.1 uch btnmgr_keydesctab,
178 1.1 uch KB_US, /* XXX, This is bad idea... */
179 1.1 uch };
180 1.1 uch
181 1.1 uch /*
182 1.1 uch * function bodies
183 1.1 uch */
184 1.1 uch int
185 1.25 cegger btnmgrmatch(device_t parent, cfdata_t match, void *aux)
186 1.1 uch {
187 1.1 uch struct mainbus_attach_args *ma = aux;
188 1.13 perry
189 1.7 thorpej if (strcmp(ma->ma_name, match->cf_name))
190 1.1 uch return 0;
191 1.1 uch
192 1.1 uch return (1);
193 1.1 uch }
194 1.1 uch
195 1.1 uch void
196 1.25 cegger btnmgrattach(device_t parent,
197 1.25 cegger device_t self, void *aux)
198 1.1 uch {
199 1.1 uch int id;
200 1.16 thorpej struct btnmgr_softc *sc = device_private(self);
201 1.1 uch struct wskbddev_attach_args wa;
202 1.1 uch
203 1.1 uch printf("\n");
204 1.1 uch
205 1.1 uch /*
206 1.1 uch * install button event listener
207 1.1 uch */
208 1.29 skrll for (id = 0; id < n_button_config; id++)
209 1.1 uch if (button_config[id].name != NULL)
210 1.1 uch sc->sc_hook_tag = config_hook(CONFIG_HOOK_BUTTONEVENT,
211 1.2 uch id, CONFIG_HOOK_SHARE,
212 1.2 uch btnmgr_hook, sc);
213 1.1 uch
214 1.1 uch /*
215 1.1 uch * attach wskbd
216 1.1 uch */
217 1.1 uch wa.console = 0;
218 1.1 uch wa.keymap = &btnmgr_keymapdata;
219 1.1 uch wa.accessops = &btnmgr_wskbd_accessops;
220 1.1 uch wa.accesscookie = sc;
221 1.1 uch
222 1.1 uch sc->sc_wskbddev = config_found(self, &wa, wskbddevprint);
223 1.23 uwe
224 1.23 uwe if (!pmf_device_register(self, NULL, NULL))
225 1.23 uwe aprint_error_dev(self, "unable to establish power handler\n");
226 1.1 uch }
227 1.1 uch
228 1.1 uch static int
229 1.20 christos btnmgr_hook(void *ctx, int type, long id, void *msg)
230 1.1 uch {
231 1.1 uch struct btnmgr_softc *sc = ctx;
232 1.1 uch
233 1.1 uch DPRINTF(("%s button: %s\n", btnmgr_name(id), msg ? "ON" : "OFF"));
234 1.1 uch
235 1.1 uch if (button_config[id].kevent) {
236 1.1 uch u_int evtype;
237 1.1 uch evtype = msg ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
238 1.1 uch #ifdef WSDISPLAY_COMPAT_RAWKBD
239 1.1 uch if (sc->sc_rawkbd) {
240 1.1 uch int n;
241 1.1 uch u_char data[16];
242 1.1 uch n = pckbd_encode(evtype, button_config[id].keycode,
243 1.2 uch data);
244 1.1 uch wskbd_rawinput(sc->sc_wskbddev, data, n);
245 1.1 uch } else
246 1.1 uch #endif
247 1.13 perry wskbd_input(sc->sc_wskbddev, evtype,
248 1.1 uch button_config[id].keycode);
249 1.1 uch }
250 1.1 uch
251 1.1 uch if (id == CONFIG_HOOK_BUTTONEVENT_POWER && msg)
252 1.13 perry config_hook_call(CONFIG_HOOK_PMEVENT,
253 1.2 uch CONFIG_HOOK_PMEVENT_SUSPENDREQ, NULL);
254 1.12 hamajima else if (id == CONFIG_HOOK_BUTTONEVENT_COVER)
255 1.12 hamajima config_hook_call(CONFIG_HOOK_POWERCONTROL,
256 1.12 hamajima CONFIG_HOOK_POWERCONTROL_LCDLIGHT, (void*)(msg ? 0: 1));
257 1.1 uch
258 1.1 uch return (0);
259 1.1 uch }
260 1.1 uch
261 1.14 uwe const char *
262 1.2 uch btnmgr_name(long id)
263 1.1 uch {
264 1.1 uch if (id < n_button_config)
265 1.2 uch return (button_config[id].name);
266 1.1 uch return ("unknown");
267 1.1 uch }
268 1.1 uch
269 1.1 uch int
270 1.2 uch btnmgr_wskbd_enable(void *scx, int on)
271 1.1 uch {
272 1.1 uch struct btnmgr_softc *sc = scx;
273 1.1 uch
274 1.1 uch if (on) {
275 1.1 uch if (sc->sc_enabled)
276 1.1 uch return (EBUSY);
277 1.1 uch sc->sc_enabled = 1;
278 1.1 uch } else {
279 1.1 uch sc->sc_enabled = 0;
280 1.1 uch }
281 1.1 uch
282 1.1 uch return (0);
283 1.1 uch }
284 1.1 uch
285 1.1 uch void
286 1.20 christos btnmgr_wskbd_set_leds(void *scx, int leds)
287 1.1 uch {
288 1.1 uch /*
289 1.1 uch * We have nothing to do.
290 1.1 uch */
291 1.1 uch }
292 1.1 uch
293 1.1 uch int
294 1.21 christos btnmgr_wskbd_ioctl(void *scx, u_long cmd, void *data, int flag,
295 1.20 christos struct lwp *l)
296 1.1 uch {
297 1.1 uch #ifdef WSDISPLAY_COMPAT_RAWKBD
298 1.1 uch struct btnmgr_softc *sc = scx;
299 1.1 uch #endif
300 1.1 uch switch (cmd) {
301 1.1 uch case WSKBDIO_GTYPE:
302 1.1 uch *(int *)data = WSKBD_TYPE_HPC_BTN;
303 1.2 uch return (0);
304 1.1 uch case WSKBDIO_SETLEDS:
305 1.1 uch DPRINTF(("%s(%d): no LED\n", __FILE__, __LINE__));
306 1.2 uch return (0);
307 1.1 uch case WSKBDIO_GETLEDS:
308 1.1 uch DPRINTF(("%s(%d): no LED\n", __FILE__, __LINE__));
309 1.1 uch *(int *)data = 0;
310 1.1 uch return (0);
311 1.1 uch #ifdef WSDISPLAY_COMPAT_RAWKBD
312 1.1 uch case WSKBDIO_SETMODE:
313 1.1 uch sc->sc_rawkbd = (*(int *)data == WSKBD_RAW);
314 1.1 uch DPRINTF(("%s(%d): rawkbd is %s\n", __FILE__, __LINE__,
315 1.2 uch sc->sc_rawkbd ? "on" : "off"));
316 1.1 uch return (0);
317 1.1 uch #endif
318 1.1 uch }
319 1.5 atatat return (EPASSTHROUGH);
320 1.1 uch }
321 1.1 uch
322 1.1 uch #ifdef notyet
323 1.1 uch int
324 1.15 christos btnmgropen(dev_t dev, int flag, int mode, struct lwp *l)
325 1.1 uch {
326 1.1 uch return (EINVAL);
327 1.1 uch }
328 1.1 uch
329 1.1 uch int
330 1.15 christos btnmgrclose(dev_t dev, int flag, int mode, struct lwp *l)
331 1.1 uch {
332 1.1 uch return (EINVAL);
333 1.1 uch }
334 1.1 uch
335 1.1 uch int
336 1.2 uch btnmgrread(dev_t dev, struct uio *uio, int flag)
337 1.1 uch {
338 1.1 uch return (EINVAL);
339 1.1 uch }
340 1.1 uch
341 1.1 uch int
342 1.2 uch btnmgrwrite(dev_t dev, struct uio *uio, int flag)
343 1.1 uch {
344 1.1 uch return (EINVAL);
345 1.1 uch }
346 1.1 uch
347 1.1 uch int
348 1.21 christos btnmgrioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
349 1.1 uch {
350 1.1 uch return (EINVAL);
351 1.1 uch }
352 1.1 uch #endif /* notyet */
353