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