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