hpckbd.c revision 1.22 1 /* $NetBSD: hpckbd.c,v 1.22 2007/03/04 06:01:47 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1999-2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: hpckbd.c,v 1.22 2007/03/04 06:01:47 christos Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45
46 #include <sys/tty.h>
47
48 #include <machine/bus.h>
49 #include <machine/intr.h>
50
51 #include <machine/config_hook.h>
52 #include <machine/platid.h>
53 #include <machine/platid_mask.h>
54
55 #include "opt_wsdisplay_compat.h"
56 #include "opt_pckbd_layout.h"
57 #include <dev/wscons/wsksymdef.h>
58 #include <dev/wscons/wsconsio.h>
59 #include <dev/wscons/wskbdvar.h>
60 #include <dev/wscons/wsksymvar.h>
61 #include <dev/pckbport/wskbdmap_mfii.h>
62 #ifdef WSDISPLAY_COMPAT_RAWKBD
63 #include <dev/hpc/pckbd_encode.h>
64 #endif
65
66 #include <dev/hpc/hpckbdvar.h>
67 #include <dev/hpc/hpckbdkeymap.h>
68
69 struct hpckbd_softc;
70
71 #define NEVENTQ 32
72 struct hpckbd_eventq {
73 u_int hq_type;
74 int hq_data;
75 };
76
77 struct hpckbd_core {
78 struct hpckbd_if hc_if;
79 struct hpckbd_ic_if *hc_ic;
80 const uint8_t *hc_keymap;
81 const int *hc_special;
82 int hc_polling;
83 int hc_console;
84 #define NEVENTQ 32
85 struct hpckbd_eventq hc_eventq[NEVENTQ];
86 struct hpckbd_eventq *hc_head, *hc_tail;
87 int hc_nevents;
88 int hc_enabled;
89 struct device *hc_wskbddev;
90 struct hpckbd_softc* hc_sc; /* back link */
91 #ifdef WSDISPLAY_COMPAT_RAWKBD
92 int hc_rawkbd;
93 #endif
94 };
95
96 struct hpckbd_softc {
97 struct device sc_dev;
98 struct hpckbd_core *sc_core;
99 struct hpckbd_core sc_coredata;
100 };
101
102 int hpckbd_match(struct device *, struct cfdata *, void *);
103 void hpckbd_attach(struct device *, struct device *, void *);
104
105 void hpckbd_initcore(struct hpckbd_core *, struct hpckbd_ic_if *, int);
106 void hpckbd_initif(struct hpckbd_core *);
107 int hpckbd_getevent(struct hpckbd_core *, u_int *, int *);
108 int hpckbd_putevent(struct hpckbd_core *, u_int, int);
109 void hpckbd_keymap_lookup(struct hpckbd_core*);
110 void hpckbd_keymap_setup(struct hpckbd_core *, const keysym_t *, int);
111 int __hpckbd_input(void *, int, int);
112 void __hpckbd_input_hook(void *);
113
114 CFATTACH_DECL(hpckbd, sizeof(struct hpckbd_softc),
115 hpckbd_match, hpckbd_attach, NULL, NULL);
116
117 /* wskbd accessopts */
118 int hpckbd_enable(void *, int);
119 void hpckbd_set_leds(void *, int);
120 int hpckbd_ioctl(void *, u_long, void *, int, struct lwp *);
121
122 /* consopts */
123 struct hpckbd_core hpckbd_consdata;
124 void hpckbd_cngetc(void *, u_int *, int*);
125 void hpckbd_cnpollc(void *, int);
126
127 const struct wskbd_accessops hpckbd_accessops = {
128 hpckbd_enable,
129 hpckbd_set_leds,
130 hpckbd_ioctl,
131 };
132
133 const struct wskbd_consops hpckbd_consops = {
134 hpckbd_cngetc,
135 hpckbd_cnpollc,
136 NULL,
137 };
138
139 struct wskbd_mapdata hpckbd_keymapdata = {
140 pckbd_keydesctab,
141 #ifdef PCKBD_LAYOUT
142 PCKBD_LAYOUT
143 #else
144 KB_US
145 #endif
146 };
147
148 int
149 hpckbd_match(struct device *parent,
150 struct cfdata *cf, void *aux)
151 {
152 return (1);
153 }
154
155 void
156 hpckbd_attach(struct device *parent, struct device *self, void *aux)
157 {
158 struct hpckbd_attach_args *haa = aux;
159 struct hpckbd_softc *sc = device_private(self);
160 struct hpckbd_ic_if *ic = haa->haa_ic;
161 struct wskbddev_attach_args wa;
162
163 /*
164 * Initialize core if it isn't console
165 */
166 if (hpckbd_consdata.hc_ic == ic) {
167 sc->sc_core = &hpckbd_consdata;
168 /* The core has been initialized in hpckbd_cnattach. */
169 } else {
170 sc->sc_core = &sc->sc_coredata;
171 hpckbd_initcore(sc->sc_core, ic, 0 /* not console */);
172 }
173
174 if (sc->sc_core->hc_keymap == default_keymap)
175 printf(": no keymap.");
176
177 printf("\n");
178
179 /*
180 * setup hpckbd public interface for parent controller.
181 */
182 hpckbd_initif(sc->sc_core);
183
184 /*
185 * attach wskbd
186 */
187 wa.console = sc->sc_core->hc_console;
188 wa.keymap = &hpckbd_keymapdata;
189 wa.accessops = &hpckbd_accessops;
190 wa.accesscookie = sc->sc_core;
191 sc->sc_core->hc_wskbddev = config_found(self, &wa, wskbddevprint);
192 }
193
194 int
195 hpckbd_print(void *aux, const char *pnp)
196 {
197 return (pnp ? QUIET : UNCONF);
198 }
199
200 void
201 hpckbd_initcore(struct hpckbd_core *hc, struct hpckbd_ic_if *ic, int console)
202 {
203 hc->hc_polling = 0;
204 hc->hc_console = console;
205 hc->hc_ic = ic;
206
207 /* setup event queue */
208 hc->hc_head = hc->hc_tail = hc->hc_eventq;
209 hc->hc_nevents = 0;
210
211 hpckbd_keymap_lookup(hc);
212 }
213
214 void
215 hpckbd_initif(struct hpckbd_core *hc)
216 {
217 struct hpckbd_if *kbdif = &hc->hc_if;
218
219 kbdif->hi_ctx = hc;
220 kbdif->hi_input = __hpckbd_input;
221 kbdif->hi_input_hook = __hpckbd_input_hook;
222 hpckbd_ic_establish(hc->hc_ic, &hc->hc_if);
223 }
224
225 int
226 hpckbd_putevent(struct hpckbd_core* hc, u_int type, int data)
227 {
228 int s = spltty();
229
230 if (hc->hc_nevents == NEVENTQ) {
231 splx(s);
232 return (0); /* queue is full */
233 }
234
235 hc->hc_nevents++;
236 hc->hc_tail->hq_type = type;
237 hc->hc_tail->hq_data = data;
238 if (&hc->hc_eventq[NEVENTQ] <= ++hc->hc_tail)
239 hc->hc_tail = hc->hc_eventq;
240 splx(s);
241
242 return (1);
243 }
244
245 int
246 hpckbd_getevent(struct hpckbd_core* hc, u_int *type, int *data)
247 {
248 int s = spltty();
249
250 if (hc->hc_nevents == 0) {
251 splx(s);
252 return (0); /* queue is empty */
253 }
254
255 *type = hc->hc_head->hq_type;
256 *data = hc->hc_head->hq_data;
257 hc->hc_nevents--;
258 if (&hc->hc_eventq[NEVENTQ] <= ++hc->hc_head)
259 hc->hc_head = hc->hc_eventq;
260 splx(s);
261
262 return (1);
263 }
264
265 void
266 hpckbd_keymap_setup(struct hpckbd_core *hc,
267 const keysym_t *map, int mapsize)
268 {
269 int i;
270 struct wscons_keydesc *desc;
271
272 /* fix keydesc table */
273 /*
274 * XXX The way this is done is really wrong. The __UNCONST()
275 * is a hint as to what is wrong. This actually ends up modifying
276 * initialized data which is marked "const".
277 * The reason we get away with it here is apparently that text
278 * and read-only data gets mapped read/write on the platforms
279 * using this code.
280 */
281 desc = (struct wscons_keydesc *)__UNCONST(hpckbd_keymapdata.keydesc);
282 for (i = 0; desc[i].name != 0; i++) {
283 if ((desc[i].name & KB_MACHDEP) && desc[i].map == NULL) {
284 desc[i].map = map;
285 desc[i].map_size = mapsize;
286 }
287 }
288
289 return;
290 }
291
292 void
293 hpckbd_keymap_lookup(struct hpckbd_core *hc)
294 {
295 const struct hpckbd_keymap_table *tab;
296 platid_mask_t mask;
297
298 for (tab = hpckbd_keymap_table; tab->ht_platform != NULL; tab++) {
299
300 mask = PLATID_DEREF(tab->ht_platform);
301
302 if (platid_match(&platid, &mask)) {
303 hc->hc_keymap = tab->ht_keymap;
304 hc->hc_special = tab->ht_special;
305 #if !defined(PCKBD_LAYOUT)
306 hpckbd_keymapdata.layout = tab->ht_layout;
307 #endif
308 if (tab->ht_cmdmap.map) {
309 hpckbd_keymap_setup(hc, tab->ht_cmdmap.map,
310 tab->ht_cmdmap.size);
311 #if !defined(PCKBD_LAYOUT)
312 hpckbd_keymapdata.layout |= KB_MACHDEP;
313 #endif
314 } else {
315 hpckbd_keymapdata.layout &= ~KB_MACHDEP;
316 }
317 return;
318 }
319 }
320
321 /* no keymap. use default. */
322 hc->hc_keymap = default_keymap;
323 hc->hc_special = default_special_keymap;
324 #if !defined(PCKBD_LAYOUT)
325 hpckbd_keymapdata.layout = KB_US;
326 #endif
327 }
328
329 void
330 __hpckbd_input_hook(void *arg)
331 {
332 #if 0
333 struct hpckbd_core *hc = arg;
334
335 if (hc->hc_polling) {
336 hc->hc_type = WSCONS_EVENT_ALL_KEYS_UP;
337 }
338 #endif
339 }
340
341 int
342 __hpckbd_input(void *arg, int flag, int scancode)
343 {
344 struct hpckbd_core *hc = arg;
345 int type, key;
346
347 if (flag) {
348 type = WSCONS_EVENT_KEY_DOWN;
349 } else {
350 type = WSCONS_EVENT_KEY_UP;
351 }
352
353 key = hc->hc_keymap[scancode];
354 if (key == UNK) {
355 #ifdef DEBUG
356 printf("hpckbd: unknown scan code %#x (%d, %d)\n",
357 scancode, scancode >> 3,
358 scancode - ((scancode >> 3) << 3));
359 #endif /* DEBUG */
360 return (0);
361 }
362
363 if (key == IGN) {
364 return (0);
365 }
366
367 if (key == SPL) {
368 if (!flag)
369 return (0);
370
371 if (scancode == hc->hc_special[KEY_SPECIAL_OFF]) {
372 config_hook_call(CONFIG_HOOK_BUTTONEVENT,
373 CONFIG_HOOK_BUTTONEVENT_POWER, NULL);
374 } else if (scancode == hc->hc_special[KEY_SPECIAL_LIGHT]) {
375 static int onoff; /* XXX -uch */
376 config_hook_call(CONFIG_HOOK_BUTTONEVENT,
377 CONFIG_HOOK_BUTTONEVENT_LIGHT,
378 (void *)(onoff ^= 1));
379 } else {
380 #ifdef DEBUG
381 printf("unknown special key %d\n", scancode);
382 #endif
383 }
384
385 return (0);
386 }
387
388 if (hc->hc_polling) {
389 if (hpckbd_putevent(hc, type, key) == 0)
390 printf("hpckbd: queue over flow\n");
391 } else {
392 #ifdef WSDISPLAY_COMPAT_RAWKBD
393 if (hc->hc_rawkbd) {
394 int n;
395 u_char data[16];
396 n = pckbd_encode(type, key, data);
397 wskbd_rawinput(hc->hc_wskbddev, data, n);
398 } else
399 #endif
400 wskbd_input(hc->hc_wskbddev, type, key);
401 }
402
403 return (0);
404 }
405
406 /*
407 * console support routines
408 */
409 int
410 hpckbd_cnattach(struct hpckbd_ic_if *ic)
411 {
412 struct hpckbd_core *hc = &hpckbd_consdata;
413
414 hpckbd_initcore(hc, ic, 1 /* console */);
415
416 /* attach controller */
417 hpckbd_initif(hc);
418
419 /* attach wskbd */
420 wskbd_cnattach(&hpckbd_consops, hc, &hpckbd_keymapdata);
421
422 return (0);
423 }
424
425 void
426 hpckbd_cngetc(void *arg, u_int *type, int *data)
427 {
428 struct hpckbd_core *hc = arg;
429
430 if (!hc->hc_console || !hc->hc_polling || !hc->hc_ic)
431 return;
432
433 while (hpckbd_getevent(hc, type, data) == 0) /* busy loop */
434 hpckbd_ic_poll(hc->hc_ic);
435 }
436
437 void
438 hpckbd_cnpollc(void *arg, int on)
439 {
440 struct hpckbd_core *hc = arg;
441
442 hc->hc_polling = on;
443 }
444
445 int
446 hpckbd_enable(void *arg, int on)
447 {
448 struct hpckbd_core *hc = arg;
449
450 if (on) {
451 if (hc->hc_enabled)
452 return (EBUSY);
453 hc->hc_enabled = 1;
454 } else {
455 if (hc->hc_console)
456 return (EBUSY);
457 hc->hc_enabled = 0;
458 }
459
460 return (0);
461 }
462
463 void
464 hpckbd_set_leds(void *arg, int leds)
465 {
466 /* Can you find any LED which tells you about keyboard? */
467 }
468
469 int
470 hpckbd_ioctl(void *arg, u_long cmd, void *data, int flag,
471 struct lwp *l)
472 {
473 #ifdef WSDISPLAY_COMPAT_RAWKBD
474 struct hpckbd_core *hc = arg;
475 #endif
476 switch (cmd) {
477 case WSKBDIO_GTYPE:
478 *(int *)data = WSKBD_TYPE_HPC_KBD;
479 return (0);
480 case WSKBDIO_SETLEDS:
481 return 0;
482 case WSKBDIO_GETLEDS:
483 *(int *)data = 0; /* dummy for wsconsctl(8) */
484 return (0);
485 #ifdef WSDISPLAY_COMPAT_RAWKBD
486 case WSKBDIO_SETMODE:
487 hc->hc_rawkbd = (*(int *)data == WSKBD_RAW);
488 return (0);
489 #endif
490 }
491 return (EPASSTHROUGH);
492 }
493