xref: /netbsd-src/sys/dev/hpc/hpckbd.c (revision e55cffd8e520e9b03f18a1bd98bb04223e79f69f)
1 /*	$NetBSD: hpckbd.c,v 1.3 2001/04/12 19:45:25 thorpej 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 #ifdef DEBUG
333 		printf("hpckbd: unknown scan code %#x (%d, %d)\n",
334 		       scancode, scancode >> 3,
335 		       scancode - ((scancode >> 3) << 3));
336 #endif /* DEBUG */
337 		return (0);
338 	}
339 
340 	if (key == IGN) {
341 		return (0);
342 	}
343 
344 	if (key == SPL) {
345 		if (!flag)
346 			return (0);
347 
348 		if (scancode == hc->hc_special[KEY_SPECIAL_OFF])
349 			printf("off button\n");
350 		else if (scancode == hc->hc_special[KEY_SPECIAL_LIGHT]) {
351 			static int onoff; /* XXX -uch */
352 			config_hook_call(CONFIG_HOOK_BUTTONEVENT,
353 					 CONFIG_HOOK_BUTTONEVENT_LIGHT,
354 					 (void *)(onoff ^= 1));
355 		} else
356 			printf("unknown special key %d\n", scancode);
357 
358 		return (0);
359 	}
360 
361 	if (hc->hc_polling) {
362 		if (hpckbd_putevent(hc, type, hc->hc_keymap[scancode]) == 0)
363 			printf("hpckbd: queue over flow");
364 	} else {
365 #ifdef WSDISPLAY_COMPAT_RAWKBD
366 		if (hc->hc_rawkbd) {
367 			int n;
368 			u_char data[16];
369 			n = pckbd_encode(type, hc->hc_keymap[scancode], data);
370 			wskbd_rawinput(hc->hc_wskbddev, data, n);
371 		} else
372 #endif
373 		wskbd_input(hc->hc_wskbddev, type,
374 			    hc->hc_keymap[scancode]);
375 	}
376 
377 	return (0);
378 }
379 
380 /*
381  * console support routines
382  */
383 int
384 hpckbd_cnattach(struct hpckbd_ic_if *ic)
385 {
386 	struct hpckbd_core *hc = &hpckbd_consdata;
387 
388 	hpckbd_initcore(hc, ic, 1 /* console */);
389 
390 	/* attach controller */
391 	hpckbd_initif(hc);
392 
393 	/* attach wskbd */
394 	wskbd_cnattach(&hpckbd_consops, hc, &hpckbd_keymapdata);
395 
396 	return (0);
397 }
398 
399 void
400 hpckbd_cngetc(void *arg, u_int *type, int *data)
401 {
402 	struct hpckbd_core *hc = arg;
403 	int s;
404 
405 	if (!hc->hc_console || !hc->hc_polling || !hc->hc_ic)
406 		return;
407 
408 	s = splvm();		/* XXX Questionable. */
409 	while (hpckbd_getevent(hc, type, data) == 0) /* busy loop */
410 		hpckbd_ic_poll(hc->hc_ic);
411 	splx(s);
412 }
413 
414 void
415 hpckbd_cnpollc(void *arg, int on)
416 {
417 	struct hpckbd_core *hc = arg;
418 	int s = splvm();	/* XXX See above. */
419 
420 	hc->hc_polling = on;
421 
422 	splx(s);
423 }
424 
425 int
426 hpckbd_enable(void *arg, int on)
427 {
428 	struct hpckbd_core *hc = arg;
429 
430 	if (on) {
431 		if (hc->hc_enabled)
432 			return (EBUSY);
433 		hc->hc_enabled = 1;
434 	} else {
435 		if (hc->hc_console)
436 			return (EBUSY);
437 		hc->hc_enabled = 0;
438 	}
439 
440 	return (0);
441 }
442 
443 void
444 hpckbd_set_leds(void *arg, int leds)
445 {
446 	/* Can you find any LED which tells you about keyboard? */
447 }
448 
449 int
450 hpckbd_ioctl(void *arg, u_long cmd, caddr_t data, int flag, struct proc *p)
451 {
452 #ifdef WSDISPLAY_COMPAT_RAWKBD
453 	struct hpckbd_core *hc = arg;
454 #endif
455 	switch (cmd) {
456 	case WSKBDIO_GTYPE:
457 		*(int *)data = WSKBD_TYPE_HPC_KBD;
458 		return (0);
459 	case WSKBDIO_SETLEDS:
460 		return 0;
461 	case WSKBDIO_GETLEDS:
462 		*(int *)data = 0;	/* dummy for wsconsctl(8) */
463 		return (0);
464 #ifdef WSDISPLAY_COMPAT_RAWKBD
465 	case WSKBDIO_SETMODE:
466 		hc->hc_rawkbd = (*(int *)data == WSKBD_RAW);
467 		return (0);
468 #endif
469 	}
470 	return (-1);
471 }
472