Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*	$NetBSD: hpckbd.c,v 1.35 2018/09/18 14:57:32 uwe Exp $ */

/*-
 * Copyright (c) 1999-2001 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by UCHIYAMA Yasushi.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: hpckbd.c,v 1.35 2018/09/18 14:57:32 uwe Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>

#include <sys/tty.h>

#include <sys/bus.h>
#include <sys/intr.h>

#include <machine/config_hook.h>
#include <machine/platid.h>
#include <machine/platid_mask.h>

#include "opt_wsdisplay_compat.h"
#include "opt_pckbd_layout.h"
#include <dev/wscons/wsksymdef.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wskbdvar.h>
#include <dev/wscons/wsksymvar.h>
#include <dev/pckbport/wskbdmap_mfii.h>
#ifdef WSDISPLAY_COMPAT_RAWKBD
#include <dev/hpc/pckbd_encode.h>
#endif

#include <dev/hpc/hpckbdvar.h>
#include <dev/hpc/hpckbdkeymap.h>

struct hpckbd_softc;

#define NEVENTQ 32
struct hpckbd_eventq {
	u_int	hq_type;
	int	hq_data;
};

struct hpckbd_core {
	struct hpckbd_if	hc_if;
	struct hpckbd_ic_if	*hc_ic;
	const uint8_t		*hc_keymap;
	const int		*hc_special;
	int			hc_polling;
	int			hc_console;
#define NEVENTQ 32
	struct hpckbd_eventq	hc_eventq[NEVENTQ];
	struct hpckbd_eventq	*hc_head, *hc_tail;
	int			hc_nevents;
	int			hc_enabled;
	device_t		hc_wskbddev;
	struct hpckbd_softc	*hc_sc;	/* back link */
#ifdef WSDISPLAY_COMPAT_RAWKBD
	int			hc_rawkbd;
#endif
};

struct hpckbd_softc {
	device_t		sc_dev;
	struct hpckbd_core	*sc_core;
	struct hpckbd_core	sc_coredata;
};

int	hpckbd_match(device_t, cfdata_t, void *);
void	hpckbd_attach(device_t, device_t, void *);

void	hpckbd_initcore(struct hpckbd_core *, struct hpckbd_ic_if *, int);
void	hpckbd_initif(struct hpckbd_core *);
int	hpckbd_getevent(struct hpckbd_core *, u_int *, int *);
int	hpckbd_putevent(struct hpckbd_core *, u_int, int);
void	hpckbd_keymap_lookup(struct hpckbd_core*);
void	hpckbd_keymap_setup(struct hpckbd_core *, const keysym_t *, int);
int	__hpckbd_input(void *, int, int);
void	__hpckbd_input_hook(void *);

CFATTACH_DECL_NEW(hpckbd, sizeof(struct hpckbd_softc),
    hpckbd_match, hpckbd_attach, NULL, NULL);

/* wskbd accessopts */
int	hpckbd_enable(void *, int);
void	hpckbd_set_leds(void *, int);
int	hpckbd_ioctl(void *, u_long, void *, int, struct lwp *);

/* consopts */
struct	hpckbd_core hpckbd_consdata;
void	hpckbd_cngetc(void *, u_int *, int*);
void	hpckbd_cnpollc(void *, int);

const struct wskbd_accessops hpckbd_accessops = {
	hpckbd_enable,
	hpckbd_set_leds,
	hpckbd_ioctl,
};

const struct wskbd_consops hpckbd_consops = {
	hpckbd_cngetc,
	hpckbd_cnpollc,
	NULL,
};

struct wskbd_mapdata hpckbd_keymapdata = {
	pckbd_keydesctab,
#ifdef PCKBD_LAYOUT
	PCKBD_LAYOUT
#else
	KB_US
#endif
};

int
hpckbd_match(device_t parent, cfdata_t cf, void *aux)
{
	return (1);
}

void
hpckbd_attach(device_t parent, device_t self, void *aux)
{
	struct hpckbd_attach_args *haa = aux;
	struct hpckbd_softc *sc = device_private(self);
	struct hpckbd_ic_if *ic = haa->haa_ic;
	struct wskbddev_attach_args wa;

	sc->sc_dev = self;

	/*
	 * Initialize core if it isn't console
	 */
	if (hpckbd_consdata.hc_ic == ic) {
		sc->sc_core = &hpckbd_consdata;
		/* The core has been initialized in hpckbd_cnattach. */
	} else {
		sc->sc_core = &sc->sc_coredata;
		hpckbd_initcore(sc->sc_core, ic, 0 /* not console */);
	}

	if (sc->sc_core->hc_keymap == default_keymap)
		printf(": no keymap.");

	printf("\n");

	/*
	 * setup hpckbd public interface for parent controller.
	 */
	hpckbd_initif(sc->sc_core);

	/*
	 * attach wskbd
	 */
	wa.console = sc->sc_core->hc_console;
	wa.keymap = &hpckbd_keymapdata;
	wa.accessops = &hpckbd_accessops;
	wa.accesscookie = sc->sc_core;
	sc->sc_core->hc_wskbddev = config_found(self, &wa, wskbddevprint);

	if (!pmf_device_register(self, NULL, NULL))
		aprint_error_dev(self, "unable to establish power handler\n");
}

int
hpckbd_print(void *aux, const char *pnp)
{
	return (pnp ? QUIET : UNCONF);
}

void
hpckbd_initcore(struct hpckbd_core *hc, struct hpckbd_ic_if *ic, int console)
{
	hc->hc_polling = 0;
	hc->hc_console = console;
	hc->hc_ic = ic;

	/* setup event queue */
	hc->hc_head = hc->hc_tail = hc->hc_eventq;
	hc->hc_nevents = 0;

	hpckbd_keymap_lookup(hc);
}

void
hpckbd_initif(struct hpckbd_core *hc)
{
	struct hpckbd_if *kbdif = &hc->hc_if;

	kbdif->hi_ctx = hc;
	kbdif->hi_input = __hpckbd_input;
	kbdif->hi_input_hook = __hpckbd_input_hook;
	hpckbd_ic_establish(hc->hc_ic, &hc->hc_if);
}

int
hpckbd_putevent(struct hpckbd_core* hc, u_int type, int data)
{
	int s = spltty();

	if (hc->hc_nevents == NEVENTQ) {
		splx(s);
		return (0); /* queue is full */
	}

	hc->hc_nevents++;
	hc->hc_tail->hq_type = type;
	hc->hc_tail->hq_data = data;
	if (&hc->hc_eventq[NEVENTQ] <= ++hc->hc_tail)
		hc->hc_tail = hc->hc_eventq;
	splx(s);

	return (1);
}

int
hpckbd_getevent(struct hpckbd_core* hc, u_int *type, int *data)
{
	int s = spltty();

	if (hc->hc_nevents == 0) {
		splx(s);
		return (0); /* queue is empty */
	}

	*type = hc->hc_head->hq_type;
	*data = hc->hc_head->hq_data;
	hc->hc_nevents--;
	if (&hc->hc_eventq[NEVENTQ] <= ++hc->hc_head)
		hc->hc_head = hc->hc_eventq;
	splx(s);

	return (1);
}


#if defined(hpcsh) || defined(hpcmips)
/*
 * XXX: Use the old wrong code for now as hpcsh and hpcmips attach
 * console very early when malloc(9) is not yet available.  It is
 * convenient to be able to do early DDB on wscons.
 */
void
hpckbd_keymap_setup(struct hpckbd_core *hc,
		    const keysym_t *map, int mapsize)
{
	int i;
	struct wscons_keydesc *desc;

	/* fix keydesc table */
	/* 
	 * XXX The way this is done is really wrong.  The __UNCONST()
	 * is a hint as to what is wrong.  This actually ends up modifying
	 * initialized data which is marked "const".
	 *
	 * The reason we get away with it here is that on sh3 and mips
	 * the kernel is directly mapped.
	 */
	desc = (struct wscons_keydesc *)__UNCONST(hpckbd_keymapdata.keydesc);
	for (i = 0; desc[i].name != 0; i++) {
		if ((desc[i].name & KB_MACHDEP) && desc[i].map == NULL) {
			desc[i].map = map;
			desc[i].map_size = mapsize;
		}
	}

	return;
}

#else

void
hpckbd_keymap_setup(struct hpckbd_core *hc,
		    const keysym_t *map, int mapsize)
{
	int i;
	const struct wscons_keydesc *desc;
	static struct wscons_keydesc *ndesc = NULL;

	/* 
	 * fix keydesc table. Since it is const data, we must 
	 * copy it once before changingg it.
	 */

	if (ndesc == NULL) {
		size_t sz;

		for (sz = 0; hpckbd_keymapdata.keydesc[sz].name != 0; sz++);

		ndesc = malloc(sz * sizeof(*ndesc), M_DEVBUF, M_WAITOK);
		memcpy(ndesc, hpckbd_keymapdata.keydesc, sz * sizeof(*ndesc));

		hpckbd_keymapdata.keydesc = ndesc;
	}

	desc = hpckbd_keymapdata.keydesc;
	for (i = 0; desc[i].name != 0; i++) {
		if ((desc[i].name & KB_MACHDEP) && desc[i].map == NULL) {
			ndesc[i].map = map;
			ndesc[i].map_size = mapsize;
		}
	}

	return;
}
#endif

void
hpckbd_keymap_lookup(struct hpckbd_core *hc)
{
	const struct hpckbd_keymap_table *tab;
	platid_mask_t mask;

	for (tab = hpckbd_keymap_table; tab->ht_platform != NULL; tab++) {

		mask = PLATID_DEREF(tab->ht_platform);

		if (platid_match(&platid, &mask)) {
			hc->hc_keymap = tab->ht_keymap;
			hc->hc_special = tab->ht_special;
#if !defined(PCKBD_LAYOUT)
			hpckbd_keymapdata.layout = tab->ht_layout;
#endif
			if (tab->ht_cmdmap.map) {
				hpckbd_keymap_setup(hc, tab->ht_cmdmap.map,
				    tab->ht_cmdmap.size);
#if !defined(PCKBD_LAYOUT)
				hpckbd_keymapdata.layout |= KB_MACHDEP;
#endif
			} else {
				hpckbd_keymapdata.layout &= ~KB_MACHDEP;
			}
			return;
		}
	}

	/* no keymap. use default. */
	hc->hc_keymap = default_keymap;
	hc->hc_special = default_special_keymap;
#if !defined(PCKBD_LAYOUT)
	hpckbd_keymapdata.layout = KB_US;
#endif
}

void
__hpckbd_input_hook(void *arg)
{
#if 0
	struct hpckbd_core *hc = arg;

	if (hc->hc_polling) {
		hc->hc_type = WSCONS_EVENT_ALL_KEYS_UP;
	}
#endif
}

int
__hpckbd_input(void *arg, int flag, int scancode)
{
	struct hpckbd_core *hc = arg;
	int type, key;

	if (flag) {
		type = WSCONS_EVENT_KEY_DOWN;
	} else {
		type = WSCONS_EVENT_KEY_UP;
	}

	key = hc->hc_keymap[scancode];
	if (key == UNK) {
#ifdef DEBUG
		printf("hpckbd: unknown scan code %#x (%d, %d)\n",
		    scancode, scancode >> 3,
		    scancode - ((scancode >> 3) << 3));
#endif /* DEBUG */
		return (0);
	}

	if (key == IGN) {
		return (0);
	}

	if (key == SPL) {
		if (!flag)
			return (0);

		if (scancode == hc->hc_special[KEY_SPECIAL_OFF]) {
			config_hook_call(CONFIG_HOOK_BUTTONEVENT,
			    CONFIG_HOOK_BUTTONEVENT_POWER, (void *)1 /* on */);
		} else if (scancode == hc->hc_special[KEY_SPECIAL_LIGHT]) {
			static int onoff; /* XXX -uch */
			config_hook_call(CONFIG_HOOK_BUTTONEVENT,
			    CONFIG_HOOK_BUTTONEVENT_LIGHT,
			    (void *)(onoff ^= 1));
		} else {
#ifdef DEBUG
			printf("unknown special key %d\n", scancode);
#endif
		}

		return (0);
	}

	if (hc->hc_polling) {
		if (hpckbd_putevent(hc, type, key) == 0)
			printf("hpckbd: queue over flow\n");
	} else {
#ifdef WSDISPLAY_COMPAT_RAWKBD
		if (hc->hc_rawkbd) {
			int n;
			u_char data[16];
			n = pckbd_encode(type, key, data);
			wskbd_rawinput(hc->hc_wskbddev, data, n);
		} else
#endif
			wskbd_input(hc->hc_wskbddev, type, key);
	}

	return (0);
}

/*
 * console support routines
 */
int
hpckbd_cnattach(struct hpckbd_ic_if *ic)
{
	struct hpckbd_core *hc = &hpckbd_consdata;

	hpckbd_initcore(hc, ic, 1 /* console */);

	/* attach controller */
	hpckbd_initif(hc);

	/* attach wskbd */
	wskbd_cnattach(&hpckbd_consops, hc, &hpckbd_keymapdata);

	return (0);
}

void
hpckbd_cngetc(void *arg, u_int *type, int *data)
{
	struct hpckbd_core *hc = arg;

	if (!hc->hc_console || !hc->hc_polling || !hc->hc_ic)
		return;

	while (hpckbd_getevent(hc, type, data) == 0) /* busy loop */
		hpckbd_ic_poll(hc->hc_ic);
}

void
hpckbd_cnpollc(void *arg, int on)
{
	struct hpckbd_core *hc = arg;

	hc->hc_polling = on;
}

int
hpckbd_enable(void *arg, int on)
{
	struct hpckbd_core *hc = arg;

	if (on) {
		if (hc->hc_enabled)
			return (EBUSY);
		hc->hc_enabled = 1;
	} else {
		if (hc->hc_console)
			return (EBUSY);
		hc->hc_enabled = 0;
	}

	return (0);
}

void
hpckbd_set_leds(void *arg, int leds)
{
	/* Can you find any LED which tells you about keyboard? */
}

int
hpckbd_ioctl(void *arg, u_long cmd, void *data, int flag,
	     struct lwp *l)
{
#ifdef WSDISPLAY_COMPAT_RAWKBD
	struct hpckbd_core *hc = arg;
#endif
	switch (cmd) {
	case WSKBDIO_GTYPE:
		*(int *)data = WSKBD_TYPE_HPC_KBD;
		return (0);
	case WSKBDIO_SETLEDS:
		return 0;
	case WSKBDIO_GETLEDS:
		*(int *)data = 0;	/* dummy for wsconsctl(8) */
		return (0);
#ifdef WSDISPLAY_COMPAT_RAWKBD
	case WSKBDIO_SETMODE:
		hc->hc_rawkbd = (*(int *)data == WSKBD_RAW);
		return (0);
#endif
	}
	return (EPASSTHROUGH);
}