hpckbd.c revision 1.4 1 /* $NetBSD: hpckbd.c,v 1.4 2001/05/08 17:23:09 uch 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/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42
43 #include <sys/tty.h>
44
45 #include <machine/bus.h>
46 #include <machine/intr.h>
47
48 #include <machine/config_hook.h>
49 #include <machine/platid.h>
50 #include <machine/platid_mask.h>
51
52 #include "opt_wsdisplay_compat.h"
53 #include "opt_pckbd_layout.h"
54 #include <dev/wscons/wsksymdef.h>
55 #include <dev/wscons/wsconsio.h>
56 #include <dev/wscons/wskbdvar.h>
57 #include <dev/wscons/wsksymdef.h>
58 #include <dev/wscons/wsksymvar.h>
59 #include <dev/pckbc/wskbdmap_mfii.h>
60 #ifdef WSDISPLAY_COMPAT_RAWKBD
61 #include <dev/hpc/pckbd_encode.h>
62 #endif
63
64 #include <dev/hpc/hpckbdvar.h>
65 #include <dev/hpc/hpckbdkeymap.h>
66
67 struct hpckbd_softc;
68
69 #define NEVENTQ 32
70 struct hpckbd_eventq {
71 u_int hq_type;
72 int hq_data;
73 };
74
75 struct hpckbd_core {
76 struct hpckbd_if hc_if;
77 struct hpckbd_ic_if *hc_ic;
78 const u_int8_t *hc_keymap;
79 const int *hc_special;
80 int hc_polling;
81 int hc_console;
82 #define NEVENTQ 32
83 struct hpckbd_eventq hc_eventq[NEVENTQ];
84 struct hpckbd_eventq *hc_head, *hc_tail;
85 int hc_nevents;
86 int hc_enabled;
87 struct device *hc_wskbddev;
88 struct hpckbd_softc* hc_sc; /* back link */
89 #ifdef WSDISPLAY_COMPAT_RAWKBD
90 int hc_rawkbd;
91 #endif
92 };
93
94 struct hpckbd_softc {
95 struct device sc_dev;
96 struct hpckbd_core *sc_core;
97 struct hpckbd_core sc_coredata;
98 };
99
100 int hpckbd_match(struct device *, struct cfdata *, void *);
101 void hpckbd_attach(struct device *, struct device *, void *);
102
103 void hpckbd_initcore(struct hpckbd_core *, struct hpckbd_ic_if *, int);
104 void hpckbd_initif(struct hpckbd_core *);
105 int hpckbd_getevent(struct hpckbd_core *, u_int *, int *);
106 int hpckbd_putevent(struct hpckbd_core *, u_int, int);
107 void hpckbd_keymap_lookup(struct hpckbd_core*);
108 void hpckbd_keymap_setup(struct hpckbd_core *, const keysym_t *, int);
109 int __hpckbd_input(void *, int, int);
110 void __hpckbd_input_hook(void*);
111
112 struct cfattach hpckbd_ca = {
113 sizeof(struct hpckbd_softc), hpckbd_match, hpckbd_attach
114 };
115
116 /* wskbd accessopts */
117 int hpckbd_enable(void *, int);
118 void hpckbd_set_leds(void *, int);
119 int hpckbd_ioctl(void *, u_long, caddr_t, int, struct proc *);
120
121 /* consopts */
122 struct hpckbd_core hpckbd_consdata;
123 void hpckbd_cngetc(void *, u_int *, int*);
124 void hpckbd_cnpollc(void *, int);
125
126 const struct wskbd_accessops hpckbd_accessops = {
127 hpckbd_enable,
128 hpckbd_set_leds,
129 hpckbd_ioctl,
130 };
131
132 const struct wskbd_consops hpckbd_consops = {
133 hpckbd_cngetc,
134 hpckbd_cnpollc,
135 };
136
137 struct wskbd_mapdata hpckbd_keymapdata = {
138 pckbd_keydesctab,
139 #ifdef PCKBD_LAYOUT
140 PCKBD_LAYOUT
141 #else
142 KB_US
143 #endif
144 };
145
146 int
147 hpckbd_match(struct device *parent, struct cfdata *cf, void *aux)
148 {
149 return (1);
150 }
151
152 void
153 hpckbd_attach(struct device *parent, struct device *self, void *aux)
154 {
155 struct hpckbd_attach_args *haa = aux;
156 struct hpckbd_softc *sc = (void*)self;
157 struct hpckbd_ic_if *ic = haa->haa_ic;
158 struct wskbddev_attach_args wa;
159
160 /*
161 * Initialize core if it isn't console
162 */
163 if (hpckbd_consdata.hc_ic == ic) {
164 sc->sc_core = &hpckbd_consdata;
165 /* The core has been initialized in hpckbd_cnattach. */
166 } else {
167 sc->sc_core = &sc->sc_coredata;
168 hpckbd_initcore(sc->sc_core, ic, 0 /* not console */);
169 }
170
171 if (sc->sc_core->hc_keymap == default_keymap)
172 printf(": no keymap.");
173
174 printf("\n");
175
176 /*
177 * setup hpckbd public interface for parent controller.
178 */
179 hpckbd_initif(sc->sc_core);
180
181 /*
182 * attach wskbd
183 */
184 wa.console = sc->sc_core->hc_console;
185 wa.keymap = &hpckbd_keymapdata;
186 wa.accessops = &hpckbd_accessops;
187 wa.accesscookie = sc->sc_core;
188 sc->sc_core->hc_wskbddev = config_found(self, &wa, wskbddevprint);
189 }
190
191 int
192 hpckbd_print(void *aux, const char *pnp)
193 {
194 return (pnp ? QUIET : UNCONF);
195 }
196
197 void
198 hpckbd_initcore(struct hpckbd_core *hc, struct hpckbd_ic_if *ic, int console)
199 {
200 hc->hc_polling = 0;
201 hc->hc_console = console;
202 hc->hc_ic = ic;
203
204 /* setup event queue */
205 hc->hc_head = hc->hc_tail = hc->hc_eventq;
206 hc->hc_nevents = 0;
207
208 hpckbd_keymap_lookup(hc);
209 }
210
211 void
212 hpckbd_initif(struct hpckbd_core *hc)
213 {
214 struct hpckbd_if *kbdif = &hc->hc_if;
215
216 kbdif->hi_ctx = hc;
217 kbdif->hi_input = __hpckbd_input;
218 kbdif->hi_input_hook = __hpckbd_input_hook;
219 hpckbd_ic_establish(hc->hc_ic, &hc->hc_if);
220 }
221
222 int
223 hpckbd_putevent(struct hpckbd_core* hc, u_int type, int data)
224 {
225 int s = spltty();
226
227 if (hc->hc_nevents == NEVENTQ) {
228 splx(s);
229 return (0); /* queue is full */
230 }
231
232 hc->hc_nevents++;
233 hc->hc_tail->hq_type = type;
234 hc->hc_tail->hq_data = data;
235 if (&hc->hc_eventq[NEVENTQ] <= ++hc->hc_tail)
236 hc->hc_tail = hc->hc_eventq;
237 splx(s);
238
239 return (1);
240 }
241
242 int
243 hpckbd_getevent(struct hpckbd_core* hc, u_int *type, int *data)
244 {
245 int s = spltty();
246
247 if (hc->hc_nevents == 0) {
248 splx(s);
249 return (0); /* queue is empty */
250 }
251
252 *type = hc->hc_head->hq_type;
253 *data = hc->hc_head->hq_data;
254 hc->hc_nevents--;
255 if (&hc->hc_eventq[NEVENTQ] <= ++hc->hc_head)
256 hc->hc_head = hc->hc_eventq;
257 splx(s);
258
259 return (1);
260 }
261
262 void
263 hpckbd_keymap_setup(struct hpckbd_core *hc, const keysym_t *map, int mapsize)
264 {
265 int i;
266 struct wscons_keydesc *desc;
267
268 /* fix keydesc table */
269 desc = (struct wscons_keydesc *)hpckbd_keymapdata.keydesc;
270 for (i = 0; desc[i].name != 0; i++) {
271 if ((desc[i].name & KB_MACHDEP) && desc[i].map == NULL) {
272 desc[i].map = map;
273 desc[i].map_size = mapsize;
274 }
275 }
276
277 return;
278 }
279
280 void
281 hpckbd_keymap_lookup(struct hpckbd_core *hc)
282 {
283 const struct hpckbd_keymap_table *tab;
284 platid_mask_t mask;
285
286 for (tab = hpckbd_keymap_table; tab->ht_platform != NULL; tab++) {
287
288 mask = PLATID_DEREF(tab->ht_platform);
289
290 if (platid_match(&platid, &mask)) {
291 hc->hc_keymap = tab->ht_keymap;
292 hc->hc_special = tab->ht_special;
293 #if !defined(PCKBD_LAYOUT)
294 hpckbd_keymapdata.layout = tab->ht_layout;
295 #endif
296 if (tab->ht_cmdmap.map) {
297 hpckbd_keymap_setup(hc, tab->ht_cmdmap.map,
298 tab->ht_cmdmap.size);
299 #if !defined(PCKBD_LAYOUT)
300 hpckbd_keymapdata.layout |= KB_MACHDEP;
301 #endif
302 } else {
303 hpckbd_keymapdata.layout &= ~KB_MACHDEP;
304 }
305 return;
306 }
307 }
308
309 /* no keymap. use default. */
310 hc->hc_keymap = default_keymap;
311 hc->hc_special = default_special_keymap;
312 #if !defined(PCKBD_LAYOUT)
313 hpckbd_keymapdata.layout = KB_US;
314 #endif
315 }
316
317 void
318 __hpckbd_input_hook(void *arg)
319 {
320 #if 0
321 struct hpckbd_core *hc = arg;
322
323 if (hc->hc_polling) {
324 hc->hc_type = WSCONS_EVENT_ALL_KEYS_UP;
325 }
326 #endif
327 }
328
329 int
330 __hpckbd_input(void *arg, int flag, int scancode)
331 {
332 struct hpckbd_core *hc = arg;
333 int type, key;
334
335 if (flag) {
336 type = WSCONS_EVENT_KEY_DOWN;
337 } else {
338 type = WSCONS_EVENT_KEY_UP;
339 }
340
341 if ((key = hc->hc_keymap[scancode]) == UNK) {
342 #ifdef DEBUG
343 printf("hpckbd: unknown scan code %#x (%d, %d)\n",
344 scancode, scancode >> 3,
345 scancode - ((scancode >> 3) << 3));
346 #endif /* DEBUG */
347 return (0);
348 }
349
350 if (key == IGN) {
351 return (0);
352 }
353
354 if (key == SPL) {
355 if (!flag)
356 return (0);
357
358 if (scancode == hc->hc_special[KEY_SPECIAL_OFF]) {
359 #ifdef DEBUG
360 printf("off button\n"); // XXX notyet -uch
361 #endif
362 } else if (scancode == hc->hc_special[KEY_SPECIAL_LIGHT]) {
363 static int onoff; /* XXX -uch */
364 config_hook_call(CONFIG_HOOK_BUTTONEVENT,
365 CONFIG_HOOK_BUTTONEVENT_LIGHT,
366 (void *)(onoff ^= 1));
367 } else {
368 #ifdef DEBUG
369 printf("unknown special key %d\n", scancode);
370 #endif
371 }
372
373 return (0);
374 }
375
376 if (hc->hc_polling) {
377 if (hpckbd_putevent(hc, type, hc->hc_keymap[scancode]) == 0)
378 printf("hpckbd: queue over flow");
379 } else {
380 #ifdef WSDISPLAY_COMPAT_RAWKBD
381 if (hc->hc_rawkbd) {
382 int n;
383 u_char data[16];
384 n = pckbd_encode(type, hc->hc_keymap[scancode], data);
385 wskbd_rawinput(hc->hc_wskbddev, data, n);
386 } else
387 #endif
388 wskbd_input(hc->hc_wskbddev, type, hc->hc_keymap[scancode]);
389 }
390
391 return (0);
392 }
393
394 /*
395 * console support routines
396 */
397 int
398 hpckbd_cnattach(struct hpckbd_ic_if *ic)
399 {
400 struct hpckbd_core *hc = &hpckbd_consdata;
401
402 hpckbd_initcore(hc, ic, 1 /* console */);
403
404 /* attach controller */
405 hpckbd_initif(hc);
406
407 /* attach wskbd */
408 wskbd_cnattach(&hpckbd_consops, hc, &hpckbd_keymapdata);
409
410 return (0);
411 }
412
413 void
414 hpckbd_cngetc(void *arg, u_int *type, int *data)
415 {
416 struct hpckbd_core *hc = arg;
417
418 if (!hc->hc_console || !hc->hc_polling || !hc->hc_ic)
419 return;
420
421 while (hpckbd_getevent(hc, type, data) == 0) /* busy loop */
422 hpckbd_ic_poll(hc->hc_ic);
423 }
424
425 void
426 hpckbd_cnpollc(void *arg, int on)
427 {
428 struct hpckbd_core *hc = arg;
429
430 hc->hc_polling = on;
431 }
432
433 int
434 hpckbd_enable(void *arg, int on)
435 {
436 struct hpckbd_core *hc = arg;
437
438 if (on) {
439 if (hc->hc_enabled)
440 return (EBUSY);
441 hc->hc_enabled = 1;
442 } else {
443 if (hc->hc_console)
444 return (EBUSY);
445 hc->hc_enabled = 0;
446 }
447
448 return (0);
449 }
450
451 void
452 hpckbd_set_leds(void *arg, int leds)
453 {
454 /* Can you find any LED which tells you about keyboard? */
455 }
456
457 int
458 hpckbd_ioctl(void *arg, u_long cmd, caddr_t data, int flag, struct proc *p)
459 {
460 #ifdef WSDISPLAY_COMPAT_RAWKBD
461 struct hpckbd_core *hc = arg;
462 #endif
463 switch (cmd) {
464 case WSKBDIO_GTYPE:
465 *(int *)data = WSKBD_TYPE_HPC_KBD;
466 return (0);
467 case WSKBDIO_SETLEDS:
468 return 0;
469 case WSKBDIO_GETLEDS:
470 *(int *)data = 0; /* dummy for wsconsctl(8) */
471 return (0);
472 #ifdef WSDISPLAY_COMPAT_RAWKBD
473 case WSKBDIO_SETMODE:
474 hc->hc_rawkbd = (*(int *)data == WSKBD_RAW);
475 return (0);
476 #endif
477 }
478 return (-1);
479 }
480