summaryrefslogtreecommitdiff
path: root/sys/dev/usb/ukbd.c
blob: 00e7ffdf4918c7a42cca8cfb97a2235b2c770381 (plain)
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*	$OpenBSD: ukbd.c,v 1.78 2017/05/12 09:16:55 mpi Exp $	*/
/*      $NetBSD: ukbd.c,v 1.85 2003/03/11 16:44:00 augustss Exp $        */

/*
 * Copyright (c) 2010 Miodrag Vallat.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
/*
 * Copyright (c) 1998 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Lennart Augustsson (lennart@augustsson.net) at
 * Carlstedt Research & Technology.
 *
 * 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.
 */

/*
 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/timeout.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/ioctl.h>

#include <machine/bus.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbhid.h>

#include <dev/usb/usbdi.h>
#include <dev/usb/usbdivar.h> /* needs_reattach() */
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/usb_quirks.h>
#include <dev/usb/uhidev.h>
#include <dev/usb/ukbdvar.h>

#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wskbdvar.h>
#include <dev/wscons/wsksymdef.h>
#include <dev/wscons/wsksymvar.h>

#include <dev/hid/hidkbdsc.h>

#ifdef UKBD_DEBUG
#define DPRINTF(x)	do { if (ukbddebug) printf x; } while (0)
#define DPRINTFN(n,x)	do { if (ukbddebug>(n)) printf x; } while (0)
int	ukbddebug = 0;
#else
#define DPRINTF(x)
#define DPRINTFN(n,x)
#endif

const kbd_t ukbd_countrylayout[1 + HCC_MAX] = {
	(kbd_t)-1,
	(kbd_t)-1,	/* arabic */
	KB_BE,		/* belgian */
	(kbd_t)-1,	/* canadian bilingual */
	KB_CF,		/* canadian french */
	(kbd_t)-1,	/* czech */
	KB_DK,		/* danish */
	(kbd_t)-1,	/* finnish */
	KB_FR,		/* french */
	KB_DE,		/* german */
	(kbd_t)-1,	/* greek */
	(kbd_t)-1,	/* hebrew */
	KB_HU,		/* hungary */
	(kbd_t)-1,	/* international (iso) */
	KB_IT,		/* italian */
	KB_JP,		/* japanese (katakana) */
	(kbd_t)-1,	/* korean */
	KB_LA,		/* latin american */
	(kbd_t)-1,	/* netherlands/dutch */
	KB_NO,		/* norwegian */
	(kbd_t)-1,	/* persian (farsi) */
	KB_PL,		/* polish */
	KB_PT,		/* portuguese */
	KB_RU,		/* russian */
	(kbd_t)-1,	/* slovakia */
	KB_ES,		/* spanish */
	KB_SV,		/* swedish */
	KB_SF,		/* swiss french */
	KB_SG,		/* swiss german */
	(kbd_t)-1,	/* switzerland */
	(kbd_t)-1,	/* taiwan */
	KB_TR,		/* turkish Q */
	KB_UK,		/* uk */
	KB_US,		/* us */
	(kbd_t)-1,	/* yugoslavia */
	(kbd_t)-1	/* turkish F */
};

struct ukbd_softc {
	struct uhidev		sc_hdev;
#define sc_ledsize		sc_hdev.sc_osize

	struct hidkbd		sc_kbd;
	int			sc_spl;
	struct hid_location	sc_apple_fn;
	void			(*sc_munge)(void *, uint8_t *, u_int);

#ifdef DDB
	struct timeout		sc_ddb;	/* for entering DDB */
#endif
};

void	ukbd_cngetc(void *, u_int *, int *);
void	ukbd_cnpollc(void *, int);
void	ukbd_cnbell(void *, u_int, u_int, u_int);
void	ukbd_debugger(void *);

const struct wskbd_consops ukbd_consops = {
	ukbd_cngetc,
	ukbd_cnpollc,
	ukbd_cnbell,
#ifdef DDB
	ukbd_debugger,
#endif
};

void	ukbd_intr(struct uhidev *addr, void *ibuf, u_int len);

void	ukbd_db_enter(void *);
int	ukbd_enable(void *, int);
void	ukbd_set_leds(void *, int);
int	ukbd_ioctl(void *, u_long, caddr_t, int, struct proc *);

const struct wskbd_accessops ukbd_accessops = {
	ukbd_enable,
	ukbd_set_leds,
	ukbd_ioctl,
};

int	ukbd_match(struct device *, void *, void *);
void	ukbd_attach(struct device *, struct device *, void *);
int	ukbd_detach(struct device *, int);

struct cfdriver ukbd_cd = {
	NULL, "ukbd", DV_DULL
};

const struct cfattach ukbd_ca = {
	sizeof(struct ukbd_softc), ukbd_match, ukbd_attach, ukbd_detach
};

struct ukbd_translation {
	uint8_t original;
	uint8_t translation;
};

#ifdef __loongson__
void	ukbd_gdium_munge(void *, uint8_t *, u_int);
#endif
void	ukbd_apple_munge(void *, uint8_t *, u_int);
void	ukbd_apple_mba_munge(void *, uint8_t *, u_int);
void	ukbd_apple_iso_munge(void *, uint8_t *, u_int);
void	ukbd_apple_iso_mba_munge(void *, uint8_t *, u_int);
void	ukbd_apple_translate(void *, uint8_t *, u_int,
	    const struct ukbd_translation *, u_int);
uint8_t	ukbd_translate(const struct ukbd_translation *, size_t, uint8_t);

int
ukbd_match(struct device *parent, void *match, void *aux)
{
	struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)aux;
	int size;
	void *desc;

	uhidev_get_report_desc(uha->parent, &desc, &size);
	if (!hid_is_collection(desc, size, uha->reportid,
	    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD)))
		return (UMATCH_NONE);

	return (UMATCH_IFACECLASS);
}

void
ukbd_attach(struct device *parent, struct device *self, void *aux)
{
	struct ukbd_softc *sc = (struct ukbd_softc *)self;
	struct hidkbd *kbd = &sc->sc_kbd;
	struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)aux;
	struct usb_hid_descriptor *hid;
	u_int32_t quirks, qflags = 0;
	int dlen, repid;
	int console = 1;
	void *desc;
	kbd_t layout = (kbd_t)-1;

	sc->sc_hdev.sc_intr = ukbd_intr;
	sc->sc_hdev.sc_parent = uha->parent;
	sc->sc_hdev.sc_udev = uha->uaa->device;
	sc->sc_hdev.sc_report_id = uha->reportid;

	uhidev_get_report_desc(uha->parent, &desc, &dlen);
	repid = uha->reportid;
	sc->sc_hdev.sc_isize = hid_report_size(desc, dlen, hid_input, repid);
	sc->sc_hdev.sc_osize = hid_report_size(desc, dlen, hid_output, repid);
	sc->sc_hdev.sc_fsize = hid_report_size(desc, dlen, hid_feature, repid);

	 /*
	  * Since the HID-Proxy is always detected before any
	  * real keyboard, do not let it grab the console.
	  */
	if (uha->uaa->vendor == USB_VENDOR_APPLE &&
	    uha->uaa->product == USB_PRODUCT_APPLE_BLUETOOTH_HCI)
		console = 0;

	quirks = usbd_get_quirks(sc->sc_hdev.sc_udev)->uq_flags;
	if (quirks & UQ_SPUR_BUT_UP)
		qflags |= HIDKBD_SPUR_BUT_UP;

	if (hidkbd_attach(self, kbd, console, qflags, repid, desc, dlen) != 0)
		return;

	if (uha->uaa->vendor == USB_VENDOR_APPLE) {
		if (hid_locate(desc, dlen, HID_USAGE2(HUP_APPLE, HUG_FN_KEY),
		    uha->reportid, hid_input, &sc->sc_apple_fn, &qflags)) {
			if (qflags & HIO_VARIABLE) {
				switch (uha->uaa->product) {
				case USB_PRODUCT_APPLE_FOUNTAIN_ISO:
				case USB_PRODUCT_APPLE_GEYSER_ISO:
				case USB_PRODUCT_APPLE_WELLSPRING6_ISO:
				case USB_PRODUCT_APPLE_WELLSPRING8_ISO:
					sc->sc_munge = ukbd_apple_iso_munge;
					break;
				case USB_PRODUCT_APPLE_WELLSPRING_ISO:
				case USB_PRODUCT_APPLE_WELLSPRING4_ISO:
				case USB_PRODUCT_APPLE_WELLSPRING4A_ISO:
					sc->sc_munge = ukbd_apple_iso_mba_munge;
					break;
				case USB_PRODUCT_APPLE_WELLSPRING_ANSI:
				case USB_PRODUCT_APPLE_WELLSPRING_JIS:
				case USB_PRODUCT_APPLE_WELLSPRING4_ANSI:
				case USB_PRODUCT_APPLE_WELLSPRING4_JIS:
				case USB_PRODUCT_APPLE_WELLSPRING4A_ANSI:
				case USB_PRODUCT_APPLE_WELLSPRING4A_JIS:
					sc->sc_munge = ukbd_apple_mba_munge;
					break;
				default:
					sc->sc_munge = ukbd_apple_munge;
					break;
				}
			}
		}
	}

	if (uha->uaa->vendor == USB_VENDOR_TOPRE &&
	    uha->uaa->product == USB_PRODUCT_TOPRE_HHKB) {
		/* ignore country code on purpose */
	} else {
		usb_interface_descriptor_t *id;

		id = usbd_get_interface_descriptor(uha->uaa->iface);
		hid = usbd_get_hid_descriptor(uha->uaa->device, id);

		if (hid->bCountryCode <= HCC_MAX)
			layout = ukbd_countrylayout[hid->bCountryCode];
#ifdef DIAGNOSTIC
		if (hid->bCountryCode != 0)
			printf(", country code %d", hid->bCountryCode);
#endif
	}
	if (layout == (kbd_t)-1) {
#ifdef UKBD_LAYOUT
		layout = UKBD_LAYOUT;
#else
		layout = KB_US | KB_DEFAULT;
#endif
	}

	printf("\n");

#ifdef __loongson__
	if (uha->uaa->vendor == USB_VENDOR_CYPRESS &&
	    uha->uaa->product == USB_PRODUCT_CYPRESS_LPRDK)
		sc->sc_munge = ukbd_gdium_munge;
#endif

	if (kbd->sc_console_keyboard) {
		extern struct wskbd_mapdata ukbd_keymapdata;

		DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc));
		ukbd_keymapdata.layout = layout;
		wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
		ukbd_enable(sc, 1);
	}

	/* Flash the leds; no real purpose, just shows we're alive. */
	ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM |
		          WSKBD_LED_CAPS | WSKBD_LED_COMPOSE);
	usbd_delay_ms(sc->sc_hdev.sc_udev, 400);
	ukbd_set_leds(sc, 0);

	hidkbd_attach_wskbd(kbd, layout, &ukbd_accessops);

#ifdef DDB
	timeout_set(&sc->sc_ddb, ukbd_db_enter, sc);
#endif
}

int
ukbd_detach(struct device *self, int flags)
{
	struct ukbd_softc *sc = (struct ukbd_softc *)self;
	struct hidkbd *kbd = &sc->sc_kbd;
	int rv;

	rv = hidkbd_detach(kbd, flags);

	/* The console keyboard does not get a disable call, so check pipe. */
	if (sc->sc_hdev.sc_state & UHIDEV_OPEN)
		uhidev_close(&sc->sc_hdev);

	return (rv);
}

void
ukbd_intr(struct uhidev *addr, void *ibuf, u_int len)
{
	struct ukbd_softc *sc = (struct ukbd_softc *)addr;
	struct hidkbd *kbd = &sc->sc_kbd;

	if (kbd->sc_enabled != 0) {
		if (sc->sc_munge != NULL)
			(*sc->sc_munge)(sc, (uint8_t *)ibuf, len);
		hidkbd_input(kbd, (uint8_t *)ibuf, len);
	}
}

int
ukbd_enable(void *v, int on)
{
	struct ukbd_softc *sc = v;
	struct hidkbd *kbd = &sc->sc_kbd;
	int rv;

	if (on && usbd_is_dying(sc->sc_hdev.sc_udev))
		return EIO;

	if ((rv = hidkbd_enable(kbd, on)) != 0)
		return rv;

	if (on) {
		return uhidev_open(&sc->sc_hdev);
	} else {
		uhidev_close(&sc->sc_hdev);
		return 0;
	}
}

void
ukbd_set_leds(void *v, int leds)
{
	struct ukbd_softc *sc = v;
	struct hidkbd *kbd = &sc->sc_kbd;
	u_int8_t res;

	if (usbd_is_dying(sc->sc_hdev.sc_udev))
		return;

	if (sc->sc_ledsize && hidkbd_set_leds(kbd, leds, &res) != 0)
		uhidev_set_report_async(sc->sc_hdev.sc_parent,
		    UHID_OUTPUT_REPORT, sc->sc_hdev.sc_report_id, &res, 1);
}

int
ukbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
{
	struct ukbd_softc *sc = v;
	struct hidkbd *kbd = &sc->sc_kbd;
	int rc;

	switch (cmd) {
	case WSKBDIO_GTYPE:
		*(int *)data = WSKBD_TYPE_USB;
		return (0);
	case WSKBDIO_SETLEDS:
		ukbd_set_leds(v, *(int *)data);
		return (0);
	default:
		rc = uhidev_ioctl(&sc->sc_hdev, cmd, data, flag, p);
		if (rc != -1)
			return rc;
		else
			return hidkbd_ioctl(kbd, cmd, data, flag, p);
	}
}

/* Console interface. */
void
ukbd_cngetc(void *v, u_int *type, int *data)
{
	struct ukbd_softc *sc = v;
	struct hidkbd *kbd = &sc->sc_kbd;

	DPRINTFN(0,("ukbd_cngetc: enter\n"));
	kbd->sc_polling = 1;
	while (kbd->sc_npollchar <= 0)
		usbd_dopoll(sc->sc_hdev.sc_udev);
	kbd->sc_polling = 0;
	hidkbd_cngetc(kbd, type, data);
	DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", *data));
}

void
ukbd_cnpollc(void *v, int on)
{
	struct ukbd_softc *sc = v;

	DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));

	if (on)
		sc->sc_spl = splusb();
	else
		splx(sc->sc_spl);
	usbd_set_polling(sc->sc_hdev.sc_udev, on);
}

void
ukbd_cnbell(void *v, u_int pitch, u_int period, u_int volume)
{
	hidkbd_bell(pitch, period, volume, 1);
}

#ifdef DDB
void
ukbd_debugger(void *v)
{
	struct ukbd_softc *sc = v;

	/*
	 * For the console keyboard we can't deliver CTL-ALT-ESC
	 * from the interrupt routine.  Doing so would start
	 * polling from inside the interrupt routine and that
	 * loses bigtime.
	 */
	timeout_add(&sc->sc_ddb, 1);
}

void
ukbd_db_enter(void *xsc)
{
	db_enter();
}
#endif

int
ukbd_cnattach(void)
{
	struct ukbd_softc *sc;
	int i;

	/*
	 * XXX USB requires too many parts of the kernel to be running
	 * XXX in order to work, so we can't do much for the console
	 * XXX keyboard until autconfiguration has run its course.
	 */
	hidkbd_is_console = 1;

	if (!cold) {
		/*
		 * When switching console dynamically force all USB keyboards
		 * to re-attach and possibly became the 'console' keyboard.
		 */
		for (i = 0; i < ukbd_cd.cd_ndevs; i++) {
			if ((sc = ukbd_cd.cd_devs[i]) != NULL) {
				usb_needs_reattach(sc->sc_hdev.sc_udev);
				break;
			}
		}
	}

	return (0);
}

uint8_t
ukbd_translate(const struct ukbd_translation *table, size_t tsize,
    uint8_t keycode)
{
	for (; tsize != 0; table++, tsize--)
		if (table->original == keycode)
			return table->translation;
	return 0;
}

void
ukbd_apple_translate(void *vsc, uint8_t *ibuf, u_int ilen,
    const struct ukbd_translation* trans, u_int tlen)
{
	struct ukbd_softc *sc = vsc;
	struct hidkbd *kbd = &sc->sc_kbd;
	uint8_t *pos, *spos, *epos, xlat;

	spos = ibuf + kbd->sc_keycodeloc.pos / 8;
	epos = spos + kbd->sc_nkeycode;

	for (pos = spos; pos != epos; pos++) {
		xlat = ukbd_translate(trans, tlen, *pos);
		if (xlat != 0)
			*pos = xlat;
	}
}

void
ukbd_apple_munge(void *vsc, uint8_t *ibuf, u_int ilen)
{
	struct ukbd_softc *sc = vsc;

	static const struct ukbd_translation apple_fn_trans[] = {
		{ 40, 73 },	/* return -> insert */
		{ 42, 76 },	/* backspace -> delete */
#ifdef notyet
		{ 58, 0 },	/* F1 -> screen brightness down */
		{ 59, 0 },	/* F2 -> screen brightness up */
		{ 60, 0 },	/* F3 */
		{ 61, 0 },	/* F4 */
		{ 62, 0 },	/* F5 -> keyboard backlight down */
		{ 63, 0 },	/* F6 -> keyboard backlight up */
		{ 64, 0 },	/* F7 -> audio back */
		{ 65, 0 },	/* F8 -> audio pause/play */
		{ 66, 0 },	/* F9 -> audio next */
#endif
#ifdef __macppc__
		{ 60, 127 },	/* F3 -> audio mute */
		{ 61, 129 },	/* F4 -> audio lower */
		{ 62, 128 },	/* F5 -> audio raise */
#else
		{ 67, 127 },	/* F10 -> audio mute */
		{ 68, 129 },	/* F11 -> audio lower */
		{ 69, 128 },	/* F12 -> audio raise */
#endif
		{ 79, 77 },	/* right -> end */
		{ 80, 74 },	/* left -> home */
		{ 81, 78 },	/* down -> page down */
		{ 82, 75 }	/* up -> page up */
	};

	if (!hid_get_data(ibuf, ilen, &sc->sc_apple_fn))
		return;

	ukbd_apple_translate(vsc, ibuf, ilen, apple_fn_trans,
	    nitems(apple_fn_trans));
}

void
ukbd_apple_mba_munge(void *vsc, uint8_t *ibuf, u_int ilen)
{
	struct ukbd_softc *sc = vsc;

	static const struct ukbd_translation apple_fn_trans[] = {
		{ 40, 73 },	/* return -> insert */
		{ 42, 76 },	/* backspace -> delete */
#ifdef notyet
		{ 58, 0 },	/* F1 -> screen brightness down */
		{ 59, 0 },	/* F2 -> screen brightness up */
		{ 60, 0 },	/* F3 */
		{ 61, 0 },	/* F4 */
		{ 62, 0 },	/* F5 */
		{ 63, 0 },	/* F6 -> audio back */
		{ 64, 0 },	/* F7 -> audio pause/play */
		{ 65, 0 },	/* F8 -> audio next */
#endif
		{ 66, 127 },	/* F9 -> audio mute */
		{ 67, 129 },	/* F10 -> audio lower */
		{ 68, 128 },	/* F11 -> audio raise */
#ifdef notyet
		{ 69, 0 },	/* F12 -> eject */
#endif
		{ 79, 77 },	/* right -> end */
		{ 80, 74 },	/* left -> home */
		{ 81, 78 },	/* down -> page down */
		{ 82, 75 }	/* up -> page up */
	};

	if (!hid_get_data(ibuf, ilen, &sc->sc_apple_fn))
		return;

	ukbd_apple_translate(vsc, ibuf, ilen, apple_fn_trans,
	    nitems(apple_fn_trans));
}

void
ukbd_apple_iso_munge(void *vsc, uint8_t *ibuf, u_int ilen)
{
	static const struct ukbd_translation apple_iso_trans[] = {
		{ 53, 100 },	/* less -> grave */
		{ 100, 53 },
	};

	ukbd_apple_translate(vsc, ibuf, ilen, apple_iso_trans,
	    nitems(apple_iso_trans));
	ukbd_apple_munge(vsc, ibuf, ilen);
}

void
ukbd_apple_iso_mba_munge(void *vsc, uint8_t *ibuf, u_int ilen)
{
	static const struct ukbd_translation apple_iso_trans[] = {
		{ 53, 100 },	/* less -> grave */
		{ 100, 53 },
	};

	ukbd_apple_translate(vsc, ibuf, ilen, apple_iso_trans,
	    nitems(apple_iso_trans));
	ukbd_apple_mba_munge(vsc, ibuf, ilen);
}

#ifdef __loongson__
/*
 * Software Fn- translation for Gdium Liberty keyboard.
 */
#define	GDIUM_FN_CODE	0x82
void
ukbd_gdium_munge(void *vsc, uint8_t *ibuf, u_int ilen)
{
	struct ukbd_softc *sc = vsc;
	struct hidkbd *kbd = &sc->sc_kbd;
	uint8_t *pos, *spos, *epos, xlat;
	int fn;

	static const struct ukbd_translation gdium_fn_trans[] = {
#ifdef notyet
		{ 58, 0 },	/* F1 -> toggle camera */
		{ 59, 0 },	/* F2 -> toggle wireless */
#endif
		{ 60, 127 },	/* F3 -> audio mute */
		{ 61, 128 },	/* F4 -> audio raise */
		{ 62, 129 },	/* F5 -> audio lower */
#ifdef notyet
		{ 63, 0 },	/* F6 -> toggle ext. video */
		{ 64, 0 },	/* F7 -> toggle mouse */
		{ 65, 0 },	/* F8 -> brightness up */
		{ 66, 0 },	/* F9 -> brightness down */
		{ 67, 0 },	/* F10 -> suspend */
		{ 68, 0 },	/* F11 -> user1 */
		{ 69, 0 },	/* F12 -> user2 */
		{ 70, 0 },	/* print screen -> sysrq */
#endif
		{ 76, 71 },	/* delete -> scroll lock */
		{ 81, 78 },	/* down -> page down */
		{ 82, 75 }	/* up -> page up */
	};

	spos = ibuf + kbd->sc_keycodeloc.pos / 8;
	epos = spos + kbd->sc_nkeycode;

	/*
	 * Check for Fn key being down and remove it from the report.
	 */

	fn = 0;
	for (pos = spos; pos != epos; pos++)
		if (*pos == GDIUM_FN_CODE) {
			fn = 1;
			*pos = 0;
			break;
		}

	/*
	 * Rewrite keycodes on the fly to perform Fn-key translation.
	 * Keycodes without a translation are passed unaffected.
	 */

	if (fn != 0)
		for (pos = spos; pos != epos; pos++) {
			xlat = ukbd_translate(gdium_fn_trans,
			    nitems(gdium_fn_trans), *pos);
			if (xlat != 0)
				*pos = xlat;
		}

}
#endif