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