xref: /netbsd-src/sys/arch/macppc/dev/akbd.c (revision 3643cdbb622649fce65d914f70de0040c6169b50)
1 /*	$NetBSD: akbd.c,v 1.49 2025/01/13 16:17:36 riastradh Exp $	*/
2 
3 /*
4  * Copyright (C) 1998	Colin Wood
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Colin Wood.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: akbd.c,v 1.49 2025/01/13 16:17:36 riastradh Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/device.h>
38 #include <sys/fcntl.h>
39 #include <sys/poll.h>
40 #include <sys/select.h>
41 #include <sys/proc.h>
42 #include <sys/signalvar.h>
43 #include <sys/systm.h>
44 
45 #include <dev/wscons/wsconsio.h>
46 #include <dev/wscons/wskbdvar.h>
47 #include <dev/wscons/wsksymdef.h>
48 #include <dev/wscons/wsksymvar.h>
49 #include <dev/ofw/openfirm.h>
50 
51 #include <dev/adb/adb_keymap.h>
52 
53 #include <machine/autoconf.h>
54 #define KEYBOARD_ARRAY
55 #include <machine/keyboard.h>
56 
57 #include <macppc/dev/adbvar.h>
58 #include <macppc/dev/aedvar.h>
59 #include <macppc/dev/akbdvar.h>
60 #include <macppc/dev/pm_direct.h>
61 
62 #include "aed.h"
63 
64 /*
65  * Function declarations.
66  */
67 static int	akbdmatch(device_t, cfdata_t, void *);
68 static void	akbdattach(device_t, device_t, void *);
69 static void	kbd_processevent(adb_event_t *event, struct akbd_softc *);
70 #ifdef notyet
71 static u_char	getleds(int);
72 static int	setleds(struct akbd_softc *, u_char);
73 static void	blinkleds(struct akbd_softc *);
74 #endif
75 
76 /* Driver definition. */
77 CFATTACH_DECL_NEW(akbd, sizeof(struct akbd_softc),
78     akbdmatch, akbdattach, NULL, NULL);
79 
80 extern struct cfdriver akbd_cd;
81 
82 int akbd_enable(void *, int);
83 void akbd_set_leds(void *, int);
84 int akbd_ioctl(void *, u_long, void *, int, struct lwp *);
85 
86 struct wskbd_accessops akbd_accessops = {
87 	akbd_enable,
88 	akbd_set_leds,
89 	akbd_ioctl,
90 };
91 
92 void akbd_cngetc(void *, u_int *, int *);
93 void akbd_cnpollc(void *, int);
94 
95 struct wskbd_consops akbd_consops = {
96 	akbd_cngetc,
97 	akbd_cnpollc,
98 };
99 
100 struct wskbd_mapdata akbd_keymapdata = {
101 	akbd_keydesctab,
102 #ifdef AKBD_LAYOUT
103 	AKBD_LAYOUT,
104 #else
105 	KB_US,
106 #endif
107 };
108 
109 static int akbd_is_console;
110 static int akbd_console_attached;
111 static int pcmcia_soft_eject;
112 
113 static int
114 akbdmatch(device_t parent, cfdata_t cf, void *aux)
115 {
116 	struct adb_attach_args *aa_args = aux;
117 
118 	if (aa_args->origaddr == ADBADDR_KBD)
119 		return 1;
120 	else
121 		return 0;
122 }
123 
124 static void
125 akbdattach(device_t parent, device_t self, void *aux)
126 {
127 	ADBSetInfoBlock adbinfo;
128 	struct akbd_softc *sc = device_private(self);
129 	struct adb_attach_args *aa_args = aux;
130 	int error, kbd_done;
131 	short cmd;
132 	u_char buffer[9];
133 	struct wskbddev_attach_args a;
134 
135 	/* ohare based models have soft ejectable card slot. */
136 	if (OF_finddevice("/bandit/ohare") != -1)
137 		pcmcia_soft_eject = 1;
138 
139 	sc->origaddr = aa_args->origaddr;
140 	sc->adbaddr = aa_args->adbaddr;
141 	sc->handler_id = aa_args->handler_id;
142 
143 	sc->sc_leds = (u_int8_t)0x00;	/* initially off */
144 
145 	adbinfo.siServiceRtPtr = (Ptr)kbd_adbcomplete;
146 	adbinfo.siDataAreaAddr = (void *)sc;
147 
148 	switch (sc->handler_id) {
149 	case ADB_STDKBD:
150 		printf("standard keyboard\n");
151 		break;
152 	case ADB_ISOKBD:
153 		printf("standard keyboard (ISO layout)\n");
154 		break;
155 	case ADB_EXTKBD:
156 		cmd = ADBTALK(sc->adbaddr, 1);
157 		kbd_done =
158 		    (adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd) == 0);
159 
160 		/* Ignore Logitech MouseMan/Trackman pseudo keyboard */
161 		if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x20) {
162 			printf("Mouseman (non-EMP) pseudo keyboard\n");
163 			adbinfo.siServiceRtPtr = (Ptr)0;
164 			adbinfo.siDataAreaAddr = (Ptr)0;
165 		} else if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x21) {
166 			printf("Trackman (non-EMP) pseudo keyboard\n");
167 			adbinfo.siServiceRtPtr = (Ptr)0;
168 			adbinfo.siDataAreaAddr = (Ptr)0;
169 		} else {
170 			printf("extended keyboard\n");
171 #ifdef notyet
172 			blinkleds(sc);
173 #endif
174 		}
175 		break;
176 	case ADB_EXTISOKBD:
177 		printf("extended keyboard (ISO layout)\n");
178 #ifdef notyet
179 		blinkleds(sc);
180 #endif
181 		break;
182 	case ADB_KBDII:
183 		printf("keyboard II\n");
184 		break;
185 	case ADB_ISOKBDII:
186 		printf("keyboard II (ISO layout)\n");
187 		break;
188 	case ADB_PBKBD:
189 		printf("PowerBook keyboard\n");
190 		break;
191 	case ADB_PBISOKBD:
192 		printf("PowerBook keyboard (ISO layout)\n");
193 		break;
194 	case ADB_ADJKPD:
195 		printf("adjustable keypad\n");
196 		break;
197 	case ADB_ADJKBD:
198 		printf("adjustable keyboard\n");
199 		break;
200 	case ADB_ADJISOKBD:
201 		printf("adjustable keyboard (ISO layout)\n");
202 		break;
203 	case ADB_ADJJAPKBD:
204 		printf("adjustable keyboard (Japanese layout)\n");
205 		break;
206 	case ADB_PBEXTISOKBD:
207 		printf("PowerBook extended keyboard (ISO layout)\n");
208 		break;
209 	case ADB_PBEXTJAPKBD:
210 		printf("PowerBook extended keyboard (Japanese layout)\n");
211 		break;
212 	case ADB_JPKBDII:
213 		printf("keyboard II (Japanese layout)\n");
214 		break;
215 	case ADB_PBEXTKBD:
216 		printf("PowerBook extended keyboard\n");
217 		break;
218 	case ADB_DESIGNKBD:
219 		printf("extended keyboard\n");
220 #ifdef notyet
221 		blinkleds(sc);
222 #endif
223 		break;
224 	case ADB_PBJPKBD:
225 		printf("PowerBook keyboard (Japanese layout)\n");
226 		break;
227 	case ADB_PBG3KBD:
228 		printf("PowerBook G3 keyboard\n");
229 		break;
230 	case ADB_PBG3JPKBD:
231 		printf("PowerBook G3 keyboard (Japanese layout)\n");
232 		break;
233 	default:
234 		printf("mapped device (%d)\n", sc->handler_id);
235 		break;
236 	}
237 	error = SetADBInfo(&adbinfo, sc->adbaddr);
238 #ifdef ADB_DEBUG
239 	if (adb_debug)
240 		printf("akbd: returned %d from SetADBInfo\n", error);
241 #endif
242 
243 	if (akbd_is_console && !akbd_console_attached) {
244 		wskbd_cnattach(&akbd_consops, sc, &akbd_keymapdata);
245 		akbd_console_attached = 1;
246 	}
247 
248 	a.console = akbd_is_console;
249 	a.keymap = &akbd_keymapdata;
250 	a.accessops = &akbd_accessops;
251 	a.accesscookie = sc;
252 
253 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint, CFARGS_NONE);
254 }
255 
256 /*
257  * Handle putting the keyboard data received from the ADB into
258  * an ADB event record.
259  */
260 void
261 kbd_adbcomplete(uint8_t *buffer, uint8_t *data_area, int adb_command)
262 {
263 	adb_event_t event;
264 	struct akbd_softc *ksc;
265 	int adbaddr;
266 #ifdef ADB_DEBUG
267 	int i;
268 
269 	if (adb_debug)
270 		printf("adb: transaction completion\n");
271 #endif
272 
273 	adbaddr = ADB_CMDADDR(adb_command);
274 	ksc = (struct akbd_softc *)data_area;
275 
276 	event.addr = adbaddr;
277 	event.hand_id = ksc->handler_id;
278 	event.def_addr = ksc->origaddr;
279 	event.byte_count = buffer[0];
280 	memcpy(event.bytes, buffer + 1, event.byte_count);
281 
282 #ifdef ADB_DEBUG
283 	if (adb_debug) {
284 		printf("akbd: from %d at %d (org %d) %d:", event.addr,
285 		    event.hand_id, event.def_addr, buffer[0]);
286 		for (i = 1; i <= buffer[0]; i++)
287 			printf(" %x", buffer[i]);
288 		printf("\n");
289 	}
290 #endif
291 
292 	microtime(&event.timestamp);
293 
294 	kbd_processevent(&event, ksc);
295 }
296 
297 /*
298  * Given a keyboard ADB event, record the keycodes and call the key
299  * repeat handler, optionally passing the event through the mouse
300  * button emulation handler first.
301  */
302 static void
303 kbd_processevent(adb_event_t *event, struct akbd_softc *ksc)
304 {
305         adb_event_t new_event;
306 
307         new_event = *event;
308 	new_event.u.k.key = event->bytes[0];
309 	new_event.bytes[1] = 0xff;
310 #if NAED > 0
311 	int result;
312 
313 	if ((result = aed_input(&new_event)) != 0)
314 		return;
315 #endif
316 	kbd_intr(&new_event);
317 #if NAED > 0
318 	aed_input(&new_event);
319 #endif
320 	if (event->bytes[1] != 0xff) {
321 		new_event.u.k.key = event->bytes[1];
322 		new_event.bytes[0] = event->bytes[1];
323 		new_event.bytes[1] = 0xff;
324 		kbd_intr(&new_event);
325 #if NAED > 0
326 		aed_input(&new_event);
327 #endif
328 	}
329 
330 }
331 
332 #ifdef notyet
333 /*
334  * Get the actual hardware LED state and convert it to softc format.
335  */
336 static u_char
337 getleds(int addr)
338 {
339 	short cmd;
340 	u_char buffer[9], leds;
341 
342 	leds = 0x00;	/* all off */
343 	buffer[0] = 0;
344 
345 	/* talk R2 */
346 	cmd = ADBTALK(addr, 2);
347 	if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0 &&
348 	    buffer[0] > 0)
349 		leds = ~(buffer[2]) & 0x07;
350 
351 	return (leds);
352 }
353 
354 /*
355  * Set the keyboard LED's.
356  *
357  * Automatically translates from ioctl/softc format to the
358  * actual keyboard register format
359  */
360 static int
361 setleds(struct akbd_softc *ksc, u_char leds)
362 {
363 	int addr;
364 	short cmd;
365 	u_char buffer[9];
366 
367 	if ((leds & 0x07) == (ksc->sc_leds & 0x07))
368 		return (0);
369 
370 	addr = ksc->adbaddr;
371 	buffer[0] = 0;
372 
373 	cmd = ADBTALK(addr, 2);
374 	if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) || buffer[0] == 0)
375 		return (EIO);
376 
377 	leds = ~leds & 0x07;
378 	buffer[2] &= 0xf8;
379 	buffer[2] |= leds;
380 
381 	cmd = ADBLISTEN(addr, 2);
382 	adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
383 
384 	cmd = ADBTALK(addr, 2);
385 	if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) || buffer[0] == 0)
386 		return (EIO);
387 
388 	ksc->sc_leds = ~((u_int8_t)buffer[2]) & 0x07;
389 
390 	if ((buffer[2] & 0xf8) != leds)
391 		return (EIO);
392 	else
393 		return (0);
394 }
395 
396 /*
397  * Toggle all of the LED's on and off, just for show.
398  */
399 static void
400 blinkleds(struct akbd_softc *ksc)
401 {
402 	int addr, i;
403 	u_char blinkleds, origleds;
404 
405 	addr = ksc->adbaddr;
406 	origleds = getleds(addr);
407 	blinkleds = LED_NUMLOCK | LED_CAPSLOCK | LED_SCROLL_LOCK;
408 
409 	(void)setleds(ksc, blinkleds);
410 
411 	for (i = 0; i < 10000; i++)
412 		delay(50);
413 
414 	/* make sure that we restore the LED settings */
415 	i = 10;
416 	do {
417 		(void)setleds(ksc, (u_char)0x00);
418 	} while (setleds(ksc, (u_char)0x00) && (i-- > 0));
419 
420 	return;
421 }
422 #endif
423 
424 int
425 akbd_enable(void *v, int on)
426 {
427 	return 0;
428 }
429 
430 void
431 akbd_set_leds(void *v, int on)
432 {
433 }
434 
435 int
436 akbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
437 {
438 #ifdef WSDISPLAY_COMPAT_RAWKBD
439 	struct akbd_softc *sc = (struct akbd_softc *) v;
440 #endif
441 
442 	switch (cmd) {
443 
444 	case WSKBDIO_GTYPE:
445 		*(int *)data = WSKBD_TYPE_ADB;
446 		return 0;
447 	case WSKBDIO_SETLEDS:
448 		return 0;
449 	case WSKBDIO_GETLEDS:
450 		*(int *)data = 0;
451 		return 0;
452 #ifdef WSDISPLAY_COMPAT_RAWKBD
453 	case WSKBDIO_SETMODE:
454 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
455 		return 0;
456 #endif
457 	}
458 	/* kbdioctl(...); */
459 
460 	return EPASSTHROUGH;
461 }
462 
463 extern int adb_polling;
464 
465 void
466 kbd_passup(struct akbd_softc *sc,int key)
467 {
468 	if (sc->sc_polling) {
469 		if (sc->sc_npolledkeys <
470 			(sizeof(sc->sc_polledkeys)/sizeof(unsigned char))) {
471 			sc->sc_polledkeys[sc->sc_npolledkeys++] = key;
472 		}
473 #ifdef ADB_DEBUG
474 		else {
475 			printf("akbd: dumping polled key 0x%02x\n",key);
476 		}
477 #endif
478 #ifdef WSDISPLAY_COMPAT_RAWKBD
479 	} else if (sc->sc_rawkbd) {
480 		char cbuf[2];
481 		int s;
482 		int j = 0;
483 		int c = keyboard[ADBK_KEYVAL(key)][3];
484 
485 		if (c == 0)			/* XXX */
486 			return;
487 
488 		if (c & 0x80)
489 			cbuf[j++] = 0xe0;
490 
491 		cbuf[j++] = (c & 0x7f) | (ADBK_PRESS(key)? 0 : 0x80);
492 
493 		s = spltty();
494 		wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
495 		splx(s);
496 #endif
497 	} else {
498 		int press, val;
499 		int type;
500 
501 		press = ADBK_PRESS(key);
502 		val = ADBK_KEYVAL(key);
503 
504 		type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
505 
506 		wskbd_input(sc->sc_wskbddev, type, val);
507 	}
508 }
509 
510 int
511 kbd_intr(void *arg)
512 {
513 	adb_event_t *event = arg;
514 	int key;
515 #ifdef CAPS_IS_CONTROL
516 	static int shift;
517 #endif
518 
519 	struct akbd_softc *sc = device_lookup_private(&akbd_cd, 0);
520 
521 	key = event->u.k.key;
522 
523 #ifdef CAPS_IS_CONTROL
524 	/*
525 	 * Caps lock is weird. The key sequence generated is:
526 	 * press:   down(57) [57]  (LED turns on)
527 	 * release: up(127)  [255]
528 	 * press:   up(127)  [255]
529 	 * release: up(57)   [185] (LED turns off)
530 	 */
531 	if ((key == 57) || (key == 185))
532 		shift = 0;
533 
534 	if (key == 255) {
535 		if (shift == 0) {
536 			key = 185;
537 			shift = 1;
538 		} else {
539 			key = 57;
540 			shift = 0;
541 		}
542 	}
543 #endif
544 
545 	switch (key) {
546 #ifndef CAPS_IS_CONTROL
547 	case 57:	/* Caps Lock pressed */
548 	case 185:	/* Caps Lock released */
549 		key = ADBK_KEYDOWN(ADBK_KEYVAL(key));
550 		kbd_passup(sc,key);
551 		key = ADBK_KEYUP(ADBK_KEYVAL(key));
552 		break;
553 #endif
554 	case 245:
555 		if (pcmcia_soft_eject)
556 			pm_eject_pcmcia(0);
557 		break;
558 	case 244:
559 		if (pcmcia_soft_eject)
560 			pm_eject_pcmcia(1);
561 		break;
562 	}
563 
564 	kbd_passup(sc,key);
565 
566 	return 0;
567 }
568 
569 int
570 akbd_cnattach(void)
571 {
572 
573 	akbd_is_console = 1;
574 	return 0;
575 }
576 
577 void
578 akbd_cngetc(void *v, u_int *type, int *data)
579 {
580 	int key, press, val;
581 	int s;
582 	struct akbd_softc *sc = v;
583 
584 	s = splhigh();
585 
586 	KASSERT(sc->sc_polling);
587 	KASSERT(adb_polling);
588 
589 	while (sc->sc_npolledkeys == 0) {
590 		adb_intr(NULL);
591 		DELAY(10000);				/* XXX */
592 	}
593 
594 	splx(s);
595 
596 	key = sc->sc_polledkeys[0];
597 	sc->sc_npolledkeys--;
598 	memmove(sc->sc_polledkeys,sc->sc_polledkeys+1,
599 		sc->sc_npolledkeys * sizeof(unsigned char));
600 
601 	press = ADBK_PRESS(key);
602 	val = ADBK_KEYVAL(key);
603 
604 	*data = val;
605 	*type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
606 }
607 
608 void
609 akbd_cnpollc(void *v, int on)
610 {
611 	struct akbd_softc *sc = v;
612 	sc->sc_polling = on;
613 	if (!on) {
614 		int i;
615 		for(i=0;i<sc->sc_npolledkeys;i++) {
616 			kbd_passup(sc,sc->sc_polledkeys[i]);
617 		}
618 		sc->sc_npolledkeys = 0;
619 	}
620 	adb_polling = on;
621 }
622