1 /* $NetBSD: hpckbd.c,v 1.38 2022/10/26 22:09:37 andvar 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.38 2022/10/26 22:09:37 andvar 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
hpckbd_match(device_t parent,cfdata_t cf,void * aux)143 hpckbd_match(device_t parent, cfdata_t cf, void *aux)
144 {
145 return (1);
146 }
147
148 void
hpckbd_attach(device_t parent,device_t self,void * aux)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 CFARGS_NONE);
188
189 if (!pmf_device_register(self, NULL, NULL))
190 aprint_error_dev(self, "unable to establish power handler\n");
191 }
192
193 int
hpckbd_print(void * aux,const char * pnp)194 hpckbd_print(void *aux, const char *pnp)
195 {
196 return (pnp ? QUIET : UNCONF);
197 }
198
199 void
hpckbd_initcore(struct hpckbd_core * hc,struct hpckbd_ic_if * ic,int console)200 hpckbd_initcore(struct hpckbd_core *hc, struct hpckbd_ic_if *ic, int console)
201 {
202 hc->hc_polling = 0;
203 hc->hc_console = console;
204 hc->hc_ic = ic;
205
206 /* setup event queue */
207 hc->hc_head = hc->hc_tail = hc->hc_eventq;
208 hc->hc_nevents = 0;
209
210 hpckbd_keymap_lookup(hc);
211 }
212
213 void
hpckbd_initif(struct hpckbd_core * hc)214 hpckbd_initif(struct hpckbd_core *hc)
215 {
216 struct hpckbd_if *kbdif = &hc->hc_if;
217
218 kbdif->hi_ctx = hc;
219 kbdif->hi_input = __hpckbd_input;
220 kbdif->hi_input_hook = __hpckbd_input_hook;
221 hpckbd_ic_establish(hc->hc_ic, &hc->hc_if);
222 }
223
224 int
hpckbd_putevent(struct hpckbd_core * hc,u_int type,int data)225 hpckbd_putevent(struct hpckbd_core* hc, u_int type, int data)
226 {
227 int s = spltty();
228
229 if (hc->hc_nevents == NEVENTQ) {
230 splx(s);
231 return (0); /* queue is full */
232 }
233
234 hc->hc_nevents++;
235 hc->hc_tail->hq_type = type;
236 hc->hc_tail->hq_data = data;
237 if (&hc->hc_eventq[NEVENTQ] <= ++hc->hc_tail)
238 hc->hc_tail = hc->hc_eventq;
239 splx(s);
240
241 return (1);
242 }
243
244 int
hpckbd_getevent(struct hpckbd_core * hc,u_int * type,int * data)245 hpckbd_getevent(struct hpckbd_core* hc, u_int *type, int *data)
246 {
247 int s = spltty();
248
249 if (hc->hc_nevents == 0) {
250 splx(s);
251 return (0); /* queue is empty */
252 }
253
254 *type = hc->hc_head->hq_type;
255 *data = hc->hc_head->hq_data;
256 hc->hc_nevents--;
257 if (&hc->hc_eventq[NEVENTQ] <= ++hc->hc_head)
258 hc->hc_head = hc->hc_eventq;
259 splx(s);
260
261 return (1);
262 }
263
264
265 #if defined(hpcsh) || defined(hpcmips)
266 /*
267 * XXX: Use the old wrong code for now as hpcsh and hpcmips attach
268 * console very early when malloc(9) is not yet available. It is
269 * convenient to be able to do early DDB on wscons.
270 */
271 void
hpckbd_keymap_setup(struct hpckbd_core * hc,const keysym_t * map,int mapsize)272 hpckbd_keymap_setup(struct hpckbd_core *hc,
273 const keysym_t *map, int mapsize)
274 {
275 int i;
276 struct wscons_keydesc *desc;
277
278 /* fix keydesc table */
279 /*
280 * XXX The way this is done is really wrong. The __UNCONST()
281 * is a hint as to what is wrong. This actually ends up modifying
282 * initialized data which is marked "const".
283 *
284 * The reason we get away with it here is that on sh3 and mips
285 * the kernel is directly mapped.
286 */
287 desc = (struct wscons_keydesc *)__UNCONST(hpckbd_keymapdata.keydesc);
288 for (i = 0; desc[i].name != 0; i++) {
289 if ((desc[i].name & KB_MACHDEP) && desc[i].map == NULL) {
290 desc[i].map = map;
291 desc[i].map_size = mapsize;
292 }
293 }
294
295 return;
296 }
297
298 #else
299
300 void
hpckbd_keymap_setup(struct hpckbd_core * hc,const keysym_t * map,int mapsize)301 hpckbd_keymap_setup(struct hpckbd_core *hc,
302 const keysym_t *map, int mapsize)
303 {
304 int i;
305 const struct wscons_keydesc *desc;
306 static struct wscons_keydesc *ndesc = NULL;
307
308 /*
309 * fix keydesc table. Since it is const data, we must
310 * copy it once before changing it.
311 */
312
313 if (ndesc == NULL) {
314 size_t sz;
315
316 for (sz = 0; hpckbd_keymapdata.keydesc[sz].name != 0; sz++);
317
318 ndesc = malloc(sz * sizeof(*ndesc), M_DEVBUF, M_WAITOK);
319 memcpy(ndesc, hpckbd_keymapdata.keydesc, sz * sizeof(*ndesc));
320
321 hpckbd_keymapdata.keydesc = ndesc;
322 }
323
324 desc = hpckbd_keymapdata.keydesc;
325 for (i = 0; desc[i].name != 0; i++) {
326 if ((desc[i].name & KB_MACHDEP) && desc[i].map == NULL) {
327 ndesc[i].map = map;
328 ndesc[i].map_size = mapsize;
329 }
330 }
331
332 return;
333 }
334 #endif
335
336 void
hpckbd_keymap_lookup(struct hpckbd_core * hc)337 hpckbd_keymap_lookup(struct hpckbd_core *hc)
338 {
339 const struct hpckbd_keymap_table *tab;
340 platid_mask_t mask;
341
342 for (tab = hpckbd_keymap_table; tab->ht_platform != NULL; tab++) {
343
344 mask = PLATID_DEREF(tab->ht_platform);
345
346 if (platid_match(&platid, &mask)) {
347 hc->hc_keymap = tab->ht_keymap;
348 hc->hc_special = tab->ht_special;
349 #if !defined(PCKBD_LAYOUT)
350 hpckbd_keymapdata.layout = tab->ht_layout;
351 #endif
352 if (tab->ht_cmdmap.map) {
353 hpckbd_keymap_setup(hc, tab->ht_cmdmap.map,
354 tab->ht_cmdmap.size);
355 #if !defined(PCKBD_LAYOUT)
356 hpckbd_keymapdata.layout |= KB_MACHDEP;
357 #endif
358 } else {
359 hpckbd_keymapdata.layout &= ~KB_MACHDEP;
360 }
361 return;
362 }
363 }
364
365 /* no keymap. use default. */
366 hc->hc_keymap = default_keymap;
367 hc->hc_special = default_special_keymap;
368 #if !defined(PCKBD_LAYOUT)
369 hpckbd_keymapdata.layout = KB_US;
370 #endif
371 }
372
373 void
__hpckbd_input_hook(void * arg)374 __hpckbd_input_hook(void *arg)
375 {
376 #if 0
377 struct hpckbd_core *hc = arg;
378
379 if (hc->hc_polling) {
380 hc->hc_type = WSCONS_EVENT_ALL_KEYS_UP;
381 }
382 #endif
383 }
384
385 int
__hpckbd_input(void * arg,int flag,int scancode)386 __hpckbd_input(void *arg, int flag, int scancode)
387 {
388 struct hpckbd_core *hc = arg;
389 int type, key;
390
391 if (flag) {
392 type = WSCONS_EVENT_KEY_DOWN;
393 } else {
394 type = WSCONS_EVENT_KEY_UP;
395 }
396
397 key = hc->hc_keymap[scancode];
398 if (key == UNK) {
399 #ifdef DEBUG
400 printf("hpckbd: unknown scan code %#x (%d, %d)\n",
401 scancode, scancode >> 3,
402 scancode - ((scancode >> 3) << 3));
403 #endif /* DEBUG */
404 return (0);
405 }
406
407 if (key == IGN) {
408 return (0);
409 }
410
411 if (key == SPL) {
412 if (!flag)
413 return (0);
414
415 if (scancode == hc->hc_special[KEY_SPECIAL_OFF]) {
416 config_hook_call(CONFIG_HOOK_BUTTONEVENT,
417 CONFIG_HOOK_BUTTONEVENT_POWER, (void *)1 /* on */);
418 } else if (scancode == hc->hc_special[KEY_SPECIAL_LIGHT]) {
419 static int onoff; /* XXX -uch */
420 config_hook_call(CONFIG_HOOK_BUTTONEVENT,
421 CONFIG_HOOK_BUTTONEVENT_LIGHT,
422 (void *)(onoff ^= 1));
423 } else {
424 #ifdef DEBUG
425 printf("unknown special key %d\n", scancode);
426 #endif
427 }
428
429 return (0);
430 }
431
432 if (hc->hc_polling) {
433 if (hpckbd_putevent(hc, type, key) == 0)
434 printf("hpckbd: queue over flow\n");
435 } else {
436 #ifdef WSDISPLAY_COMPAT_RAWKBD
437 if (hc->hc_rawkbd) {
438 int n;
439 u_char data[16];
440 n = pckbd_encode(type, key, data);
441 wskbd_rawinput(hc->hc_wskbddev, data, n);
442 } else
443 #endif
444 wskbd_input(hc->hc_wskbddev, type, key);
445 }
446
447 return (0);
448 }
449
450 /*
451 * console support routines
452 */
453 int
hpckbd_cnattach(struct hpckbd_ic_if * ic)454 hpckbd_cnattach(struct hpckbd_ic_if *ic)
455 {
456 struct hpckbd_core *hc = &hpckbd_consdata;
457
458 hpckbd_initcore(hc, ic, 1 /* console */);
459
460 /* attach controller */
461 hpckbd_initif(hc);
462
463 /* attach wskbd */
464 wskbd_cnattach(&hpckbd_consops, hc, &hpckbd_keymapdata);
465
466 return (0);
467 }
468
469 void
hpckbd_cngetc(void * arg,u_int * type,int * data)470 hpckbd_cngetc(void *arg, u_int *type, int *data)
471 {
472 struct hpckbd_core *hc = arg;
473
474 if (!hc->hc_console || !hc->hc_polling || !hc->hc_ic)
475 return;
476
477 while (hpckbd_getevent(hc, type, data) == 0) /* busy loop */
478 hpckbd_ic_poll(hc->hc_ic);
479 }
480
481 void
hpckbd_cnpollc(void * arg,int on)482 hpckbd_cnpollc(void *arg, int on)
483 {
484 struct hpckbd_core *hc = arg;
485
486 hc->hc_polling = on;
487 }
488
489 int
hpckbd_enable(void * arg,int on)490 hpckbd_enable(void *arg, int on)
491 {
492 struct hpckbd_core *hc = arg;
493
494 if (on) {
495 if (hc->hc_enabled)
496 return (EBUSY);
497 hc->hc_enabled = 1;
498 } else {
499 if (hc->hc_console)
500 return (EBUSY);
501 hc->hc_enabled = 0;
502 }
503
504 return (0);
505 }
506
507 void
hpckbd_set_leds(void * arg,int leds)508 hpckbd_set_leds(void *arg, int leds)
509 {
510 /* Can you find any LED which tells you about keyboard? */
511 }
512
513 int
hpckbd_ioctl(void * arg,u_long cmd,void * data,int flag,struct lwp * l)514 hpckbd_ioctl(void *arg, u_long cmd, void *data, int flag,
515 struct lwp *l)
516 {
517 #ifdef WSDISPLAY_COMPAT_RAWKBD
518 struct hpckbd_core *hc = arg;
519 #endif
520 switch (cmd) {
521 case WSKBDIO_GTYPE:
522 *(int *)data = WSKBD_TYPE_HPC_KBD;
523 return (0);
524 case WSKBDIO_SETLEDS:
525 return 0;
526 case WSKBDIO_GETLEDS:
527 *(int *)data = 0; /* dummy for wsconsctl(8) */
528 return (0);
529 #ifdef WSDISPLAY_COMPAT_RAWKBD
530 case WSKBDIO_SETMODE:
531 hc->hc_rawkbd = (*(int *)data == WSKBD_RAW);
532 return (0);
533 #endif
534 }
535 return (EPASSTHROUGH);
536 }
537