hpckbd.c revision 1.1 1 /* $NetBSD: hpckbd.c,v 1.1 2001/02/22 18:37:55 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 if (hc->hc_nevents == NEVENTQ)
226 return (0); /* queue is full */
227
228 hc->hc_nevents++;
229 hc->hc_tail->hq_type = type;
230 hc->hc_tail->hq_data = data;
231 if (&hc->hc_eventq[NEVENTQ] <= ++hc->hc_tail)
232 hc->hc_tail = hc->hc_eventq;
233
234 return (1);
235 }
236
237 int
238 hpckbd_getevent(struct hpckbd_core* hc, u_int *type, int *data)
239 {
240 if (hc->hc_nevents == 0)
241 return (0); /* queue is empty */
242
243 *type = hc->hc_head->hq_type;
244 *data = hc->hc_head->hq_data;
245 hc->hc_nevents--;
246 if (&hc->hc_eventq[NEVENTQ] <= ++hc->hc_head)
247 hc->hc_head = hc->hc_eventq;
248
249 return (1);
250 }
251
252 void
253 hpckbd_keymap_setup(struct hpckbd_core *hc, const keysym_t *map, int mapsize)
254 {
255 int i;
256 struct wscons_keydesc *desc;
257
258 /* fix keydesc table */
259 desc = (struct wscons_keydesc *)hpckbd_keymapdata.keydesc;
260 for (i = 0; desc[i].name != 0; i++) {
261 if ((desc[i].name & KB_MACHDEP) && desc[i].map == NULL) {
262 desc[i].map = map;
263 desc[i].map_size = mapsize;
264 }
265 }
266
267 return;
268 }
269
270 void
271 hpckbd_keymap_lookup(struct hpckbd_core *hc)
272 {
273 const struct hpckbd_keymap_table *tab;
274 platid_mask_t mask;
275
276 for (tab = hpckbd_keymap_table; tab->ht_platform != NULL; tab++) {
277
278 mask = PLATID_DEREF(tab->ht_platform);
279
280 if (platid_match(&platid, &mask)) {
281 hc->hc_keymap = tab->ht_keymap;
282 hc->hc_special = tab->ht_special;
283 #if !defined(PCKBD_LAYOUT)
284 hpckbd_keymapdata.layout = tab->ht_layout;
285 #endif
286 if (tab->ht_cmdmap.map) {
287 hpckbd_keymap_setup(hc, tab->ht_cmdmap.map,
288 tab->ht_cmdmap.size);
289 #if !defined(PCKBD_LAYOUT)
290 hpckbd_keymapdata.layout |= KB_MACHDEP;
291 #endif
292 } else {
293 hpckbd_keymapdata.layout &= ~KB_MACHDEP;
294 }
295 return;
296 }
297 }
298
299 /* no keymap. use default. */
300 hc->hc_keymap = default_keymap;
301 hc->hc_special = default_special_keymap;
302 #if !defined(PCKBD_LAYOUT)
303 hpckbd_keymapdata.layout = KB_US;
304 #endif
305 }
306
307 void
308 __hpckbd_input_hook(void *arg)
309 {
310 #if 0
311 struct hpckbd_core *hc = arg;
312
313 if (hc->hc_polling) {
314 hc->hc_type = WSCONS_EVENT_ALL_KEYS_UP;
315 }
316 #endif
317 }
318
319 int
320 __hpckbd_input(void *arg, int flag, int scancode)
321 {
322 struct hpckbd_core *hc = arg;
323 int type, key;
324
325 if (flag) {
326 type = WSCONS_EVENT_KEY_DOWN;
327 } else {
328 type = WSCONS_EVENT_KEY_UP;
329 }
330
331 if ((key = hc->hc_keymap[scancode]) == UNK) {
332 printf("hpckbd: unknown scan code %#x\n", scancode);
333
334 return (0);
335 }
336
337 if (key == IGN) {
338 return (0);
339 }
340
341 if (key == SPL) {
342 if (!flag)
343 return (0);
344
345 if (scancode == hc->hc_special[KEY_SPECIAL_OFF])
346 printf("off button\n");
347 else if (scancode == hc->hc_special[KEY_SPECIAL_LIGHT]) {
348 static int onoff; /* XXX -uch */
349 config_hook_call(CONFIG_HOOK_BUTTONEVENT,
350 CONFIG_HOOK_BUTTONEVENT_LIGHT,
351 (void *)(onoff ^= 1));
352 } else
353 printf("unknown special key %d\n", scancode);
354
355 return (0);
356 }
357
358 if (hc->hc_polling) {
359 if (hpckbd_putevent(hc, type, hc->hc_keymap[scancode]) == 0)
360 printf("hpckbd: queue over flow");
361 } else {
362 #ifdef WSDISPLAY_COMPAT_RAWKBD
363 if (hc->hc_rawkbd) {
364 int n;
365 u_char data[16];
366 n = pckbd_encode(type, hc->hc_keymap[scancode], data);
367 wskbd_rawinput(hc->hc_wskbddev, data, n);
368 } else
369 #endif
370 wskbd_input(hc->hc_wskbddev, type,
371 hc->hc_keymap[scancode]);
372 }
373
374 return (0);
375 }
376
377 /*
378 * console support routines
379 */
380 int
381 hpckbd_cnattach(struct hpckbd_ic_if *ic)
382 {
383 struct hpckbd_core *hc = &hpckbd_consdata;
384
385 hpckbd_initcore(hc, ic, 1 /* console */);
386
387 /* attach controller */
388 hpckbd_initif(hc);
389
390 /* attach wskbd */
391 wskbd_cnattach(&hpckbd_consops, hc, &hpckbd_keymapdata);
392
393 return (0);
394 }
395
396 void
397 hpckbd_cngetc(void *arg, u_int *type, int *data)
398 {
399 struct hpckbd_core *hc = arg;
400 int s;
401
402 if (!hc->hc_console || !hc->hc_polling || !hc->hc_ic)
403 return;
404
405 s = splimp();
406 while (hpckbd_getevent(hc, type, data) == 0) /* busy loop */
407 hpckbd_ic_poll(hc->hc_ic);
408 splx(s);
409 }
410
411 void
412 hpckbd_cnpollc(void *arg, int on)
413 {
414 struct hpckbd_core *hc = arg;
415 int s = splimp();
416
417 hc->hc_polling = on;
418
419 splx(s);
420 }
421
422 int
423 hpckbd_enable(void *arg, int on)
424 {
425 struct hpckbd_core *hc = arg;
426
427 if (on) {
428 if (hc->hc_enabled)
429 return (EBUSY);
430 hc->hc_enabled = 1;
431 } else {
432 if (hc->hc_console)
433 return (EBUSY);
434 hc->hc_enabled = 0;
435 }
436
437 return (0);
438 }
439
440 void
441 hpckbd_set_leds(void *arg, int leds)
442 {
443 /* Can you find any LED which tells you about keyboard? */
444 }
445
446 int
447 hpckbd_ioctl(void *arg, u_long cmd, caddr_t data, int flag, struct proc *p)
448 {
449 #ifdef WSDISPLAY_COMPAT_RAWKBD
450 struct hpckbd_core *hc = arg;
451 #endif
452 switch (cmd) {
453 case WSKBDIO_GTYPE:
454 *(int *)data = WSKBD_TYPE_HPC_KBD;
455 return (0);
456 case WSKBDIO_SETLEDS:
457 return 0;
458 case WSKBDIO_GETLEDS:
459 *(int *)data = 0; /* dummy for wsconsctl(8) */
460 return (0);
461 #ifdef WSDISPLAY_COMPAT_RAWKBD
462 case WSKBDIO_SETMODE:
463 hc->hc_rawkbd = (*(int *)data == WSKBD_RAW);
464 return (0);
465 #endif
466 }
467 return (-1);
468 }
469