xref: /openbsd-src/sys/dev/adb/akbd.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: akbd.c,v 1.10 2011/06/15 21:32:05 miod Exp $	*/
2 /*	$NetBSD: akbd.c,v 1.17 2005/01/15 16:00:59 chs Exp $	*/
3 
4 /*
5  * Copyright (C) 1998	Colin Wood
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Colin Wood.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/timeout.h>
36 #include <sys/kernel.h>
37 #include <sys/device.h>
38 #include <sys/systm.h>
39 
40 #include <dev/wscons/wsconsio.h>
41 #include <dev/wscons/wskbdvar.h>
42 #include <dev/wscons/wsksymdef.h>
43 #include <dev/wscons/wsksymvar.h>
44 
45 #include <machine/autoconf.h>
46 #include <machine/cpu.h>
47 
48 #include <dev/adb/adb.h>
49 #include <dev/adb/akbdmap.h>
50 #include <dev/adb/akbdvar.h>
51 #include <dev/adb/keyboard.h>
52 
53 /*
54  * Function declarations.
55  */
56 int	akbdmatch(struct device *, void *, void *);
57 void	akbdattach(struct device *, struct device *, void *);
58 
59 /* Driver definition. */
60 struct cfattach akbd_ca = {
61 	sizeof(struct akbd_softc), akbdmatch, akbdattach
62 };
63 struct cfdriver akbd_cd = {
64 	NULL, "akbd", DV_DULL
65 };
66 
67 int	akbd_enable(void *, int);
68 void	akbd_set_leds(void *, int);
69 int	akbd_ioctl(void *, u_long, caddr_t, int, struct proc *);
70 
71 
72 struct wskbd_accessops akbd_accessops = {
73 	akbd_enable,
74 	akbd_set_leds,
75 	akbd_ioctl,
76 };
77 
78 struct wskbd_mapdata akbd_keymapdata = {
79 	akbd_keydesctab,
80 #ifdef AKBD_LAYOUT
81 	AKBD_LAYOUT,
82 #else
83 	KB_US,
84 #endif
85 };
86 
87 void	akbd_adbcomplete(caddr_t, caddr_t, int);
88 void	akbd_capslockwrapper(struct akbd_softc *, int);
89 void	akbd_input(struct akbd_softc *, int);
90 void	akbd_processevent(struct akbd_softc *, adb_event_t *);
91 void	akbd_rawrepeat(void *v);
92 #ifdef notyet
93 u_char	getleds(int);
94 int	setleds(struct akbd_softc *, u_char);
95 void	blinkleds(struct akbd_softc *);
96 #endif
97 
98 int
99 akbdmatch(struct device *parent, void *vcf, void *aux)
100 {
101 	struct adb_attach_args *aa_args = (struct adb_attach_args *)aux;
102 
103 	if (strcmp(aa_args->name, adb_device_name) != 0)
104 		return (0);
105 
106 	if (aa_args->origaddr == ADBADDR_KBD)
107 		return (1);
108 	else
109 		return (0);
110 }
111 
112 void
113 akbdattach(struct device *parent, struct device *self, void *aux)
114 {
115 	ADBSetInfoBlock adbinfo;
116 	struct akbd_softc *sc = (struct akbd_softc *)self;
117 	struct adb_attach_args *aa_args = (struct adb_attach_args *)aux;
118 	int error, kbd_done;
119 	short cmd;
120 	u_char buffer[9];
121 	struct wskbddev_attach_args a;
122 	static int akbd_console_initted;
123 	int wskbd_eligible = 1;
124 
125 	sc->origaddr = aa_args->origaddr;
126 	sc->adbaddr = aa_args->adbaddr;
127 	sc->handler_id = aa_args->handler_id;
128 
129 	sc->sc_leds = (u_int8_t)0x00;	/* initially off */
130 	sc->sc_caps = 0;
131 
132 	adbinfo.siServiceRtPtr = (Ptr)akbd_adbcomplete;
133 	adbinfo.siDataAreaAddr = (caddr_t)sc;
134 
135 	printf(": ");
136 	switch (sc->handler_id) {
137 	case ADB_STDKBD:
138 		printf("standard keyboard\n");
139 		break;
140 	case ADB_ISOKBD:
141 		printf("standard keyboard (ISO layout)\n");
142 		break;
143 	case ADB_EXTKBD:
144 		cmd = ADBTALK(sc->adbaddr, 1);
145 		kbd_done =
146 		    (adb_op_sync((Ptr)buffer, cmd) == 0);
147 
148 		/* Ignore Logitech MouseMan/Trackman pseudo keyboard */
149 		if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x20) {
150 			printf("Mouseman (non-EMP) pseudo keyboard\n");
151 			adbinfo.siServiceRtPtr = (Ptr)0;
152 			adbinfo.siDataAreaAddr = (Ptr)0;
153 			wskbd_eligible = 0;
154 		} else if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x21) {
155 			printf("Trackman (non-EMP) pseudo keyboard\n");
156 			adbinfo.siServiceRtPtr = (Ptr)0;
157 			adbinfo.siDataAreaAddr = (Ptr)0;
158 			wskbd_eligible = 0;
159 		} else {
160 			printf("extended keyboard\n");
161 #ifdef notyet
162 			blinkleds(sc);
163 #endif
164 		}
165 		break;
166 	case ADB_EXTISOKBD:
167 		printf("extended keyboard (ISO layout)\n");
168 #ifdef notyet
169 		blinkleds(sc);
170 #endif
171 		break;
172 	case ADB_KBDII:
173 		printf("keyboard II\n");
174 		break;
175 	case ADB_ISOKBDII:
176 		printf("keyboard II (ISO layout)\n");
177 		break;
178 	case ADB_PBKBD:
179 		printf("PowerBook keyboard\n");
180 		break;
181 	case ADB_PBISOKBD:
182 		printf("PowerBook keyboard (ISO layout)\n");
183 		break;
184 	case ADB_ADJKPD:
185 		printf("adjustable keypad\n");
186 		wskbd_eligible = 0;
187 		break;
188 	case ADB_ADJKBD:
189 		printf("adjustable keyboard\n");
190 		break;
191 	case ADB_ADJISOKBD:
192 		printf("adjustable keyboard (ISO layout)\n");
193 		break;
194 	case ADB_ADJJAPKBD:
195 		printf("adjustable keyboard (Japanese layout)\n");
196 		break;
197 	case ADB_PBEXTISOKBD:
198 		printf("PowerBook extended keyboard (ISO layout)\n");
199 		break;
200 	case ADB_PBEXTJAPKBD:
201 		printf("PowerBook extended keyboard (Japanese layout)\n");
202 		break;
203 	case ADB_JPKBDII:
204 		printf("keyboard II (Japanese layout)\n");
205 		break;
206 	case ADB_PBEXTKBD:
207 		printf("PowerBook extended keyboard\n");
208 		break;
209 	case ADB_DESIGNKBD:
210 		printf("extended keyboard\n");
211 #ifdef notyet
212 		blinkleds(sc);
213 #endif
214 		break;
215 	case ADB_PBJPKBD:
216 		printf("PowerBook keyboard (Japanese layout)\n");
217 		break;
218 	case ADB_PBG3JPKBD:
219 		printf("PowerBook G3 keyboard (Japanese layout)\n");
220 		break;
221 	case ADB_PBG4KBD:
222 		printf("PowerBook G4 keyboard (Inverted T)\n");
223 		break;
224 	case ADB_IBITISOKBD:
225 		printf("iBook keyboard with inverted T (ISO layout)\n");
226 		break;
227 	default:
228 		printf("mapped device (%d)\n", sc->handler_id);
229 #if 0
230 		wskbd_eligible = 0;
231 #endif
232 		break;
233 	}
234 	error = set_adb_info(&adbinfo, sc->adbaddr);
235 #ifdef ADB_DEBUG
236 	if (adb_debug)
237 		printf("akbd: returned %d from set_adb_info\n", error);
238 #endif
239 
240 #ifdef WSDISPLAY_COMPAT_RAWKBD
241 	timeout_set(&sc->sc_rawrepeat_ch, akbd_rawrepeat, sc);
242 #endif
243 
244 	if (akbd_is_console() && wskbd_eligible)
245 		a.console = (++akbd_console_initted == 1);
246 	else
247 		a.console = 0;
248 	a.keymap = &akbd_keymapdata;
249 	a.accessops = &akbd_accessops;
250 	a.accesscookie = sc;
251 
252 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
253 }
254 
255 
256 /*
257  * Handle putting the keyboard data received from the ADB into
258  * an ADB event record.
259  */
260 void
261 akbd_adbcomplete(caddr_t buffer, caddr_t data_area, int adb_command)
262 {
263 	adb_event_t event;
264 	struct akbd_softc *sc;
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 	sc = (struct akbd_softc *)data_area;
275 
276 	event.byte_count = buffer[0];
277 	memcpy(event.bytes, buffer + 1, event.byte_count);
278 
279 #ifdef ADB_DEBUG
280 	if (adb_debug) {
281 		printf("akbd: from %d at %d (org %d) %d:", adbaddr,
282 		    sc->handler_id, sc->origaddr, buffer[0]);
283 		for (i = 1; i <= buffer[0]; i++)
284 			printf(" %x", buffer[i]);
285 		printf("\n");
286 	}
287 #endif
288 
289 	if (sc->sc_wskbddev != NULL)
290 		akbd_processevent(sc, &event);
291 }
292 
293 #ifdef notyet
294 /*
295  * Get the actual hardware LED state and convert it to softc format.
296  */
297 u_char
298 getleds(int addr)
299 {
300 	short cmd;
301 	u_char buffer[9], leds;
302 
303 	leds = 0x00;	/* all off */
304 	buffer[0] = 0;
305 
306 	cmd = ADBTALK(addr, 2);
307 	if (adb_op_sync((Ptr)buffer, cmd) == 0 &&
308 	    buffer[0] > 0)
309 		leds = ~(buffer[2]) & 0x07;
310 
311 	return (leds);
312 }
313 
314 /*
315  * Set the keyboard LED's.
316  *
317  * Automatically translates from ioctl/softc format to the
318  * actual keyboard register format
319  */
320 int
321 setleds(struct akbd_softc *sc, u_char leds)
322 {
323 	int addr;
324 	short cmd;
325 	u_char buffer[9];
326 
327 	addr = sc->adbaddr;
328 	buffer[0] = 0;
329 
330 	cmd = ADBTALK(addr, 2);
331 	if (adb_op_sync((Ptr)buffer, cmd) || buffer[0] == 0)
332 		return (EIO);
333 
334 	leds = ~leds & 0x07;
335 	buffer[2] &= 0xf8;
336 	buffer[2] |= leds;
337 
338 	cmd = ADBLISTEN(addr, 2);
339 	adb_op_sync((Ptr)buffer, cmd);
340 
341 	/* talk R2 */
342 	cmd = ADBTALK(addr, 2);
343 	if (adb_op_sync((Ptr)buffer, cmd) || buffer[0] == 0)
344 		return (EIO);
345 
346 	if ((buffer[2] & 0xf8) != leds)
347 		return (EIO);
348 	else
349 		return (0);
350 }
351 
352 /*
353  * Toggle all of the LED's on and off, just for show.
354  */
355 void
356 blinkleds(struct akbd_softc *sc)
357 {
358 	u_char origleds;
359 
360 	origleds = getleds(sc->adbaddr);
361 	setleds(sc, LED_NUMLOCK | LED_CAPSLOCK | LED_SCROLL_LOCK);
362 	delay(400000);
363 	setleds(sc, origleds);
364 
365 	if (origleds & LED_NUMLOCK)
366 		sc->sc_leds |= WSKBD_LED_NUM;
367 	if (origleds & LED_CAPSLOCK)
368 		sc->sc_leds |= WSKBD_LED_CAPS;
369 	if (origleds & LED_SCROLL_LOCK)
370 		sc->sc_leds |= WSKBD_LED_SCROLL;
371 }
372 #endif
373 
374 int
375 akbd_enable(void *v, int on)
376 {
377 	return 0;
378 }
379 
380 void
381 akbd_set_leds(void *v, int on)
382 {
383 #ifdef notyet
384 	struct akbd_softc *sc = v;
385 	int leds;
386 
387 	if (sc->sc_extended) {
388 		if (sc->sc_leds == on)
389 			return;
390 
391 		leds = 0;
392 		if (on & WSKBD_LED_NUM)
393 			leds |= LED_NUMLOCK;
394 		if (on & WSKBD_LED_CAPS)
395 			leds |= LED_CAPSLOCK;
396 		if (on & WSKBD_LED_SCROLL)
397 			leds |= LED_SCROLL_LOCK;
398 
399 		setleds(sc, leds);
400 	}
401 #endif
402 }
403 
404 int
405 akbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
406 {
407 	struct akbd_softc *sc = v;
408 
409 	switch (cmd) {
410 
411 	case WSKBDIO_GTYPE:
412 		*(int *)data = WSKBD_TYPE_ADB;
413 		return 0;
414 	case WSKBDIO_SETLEDS:
415 		akbd_set_leds(v, *(int *)data);
416 		return 0;
417 	case WSKBDIO_GETLEDS:
418 		*(int *)data = sc->sc_leds;
419 		return 0;
420 #ifdef WSDISPLAY_COMPAT_RAWKBD
421 	case WSKBDIO_SETMODE:
422 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
423 		timeout_del(&sc->sc_rawrepeat_ch);
424 		return (0);
425 #endif
426 
427 #if defined(__mac68k__)	/* XXX not worth creating akbd_machdep_ioctl() */
428 	case WSKBDIO_BELL:
429 	case WSKBDIO_COMPLEXBELL:
430 #define d ((struct wskbd_bell_data *)data)
431 		mac68k_ring_bell(d->pitch, d->period * hz / 1000, d->volume);
432 #undef d
433 		return (0);
434 #endif
435 
436 	default:
437 		return (-1);
438 	}
439 }
440 
441 #ifdef WSDISPLAY_COMPAT_RAWKBD
442 void
443 akbd_rawrepeat(void *v)
444 {
445 	struct akbd_softc *sc = v;
446 	int s;
447 
448 	s = spltty();
449 	wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
450 	splx(s);
451 	timeout_add_msec(&sc->sc_rawrepeat_ch, REP_DELAYN);
452 }
453 #endif
454 
455 /*
456  * The ``caps lock'' key is special: since on earlier keyboards, the physical
457  * key stays down when pressed, we will get a notification of the key press,
458  * but not of the key release. Then, when it is pressed again, we will not get
459  * a notification of the key press, but will see the key release.
460  *
461  * This is not exactly true. We see the missing release and press events both
462  * as the release of the power (reset) key.
463  *
464  * To avoid confusing them with real power key presses, we maintain two
465  * states for the caps lock key: logically down (from wscons' point of view),
466  * and ``physically'' down (from the adb messages point of view), to ignore
467  * the power key. But since one may press the power key while the caps lock
468  * is held down, we also have to remember the state of the power key... this
469  * is quite messy.
470  */
471 
472 /*
473  * Values for caps lock state machine
474  */
475 #define	CL_DOWN_ADB	0x01
476 #define	CL_DOWN_LOGICAL	0x02
477 #define	CL_DOWN_RESET	0x04
478 
479 /*
480  * Given a keyboard ADB event, decode the keycodes and pass them to wskbd.
481  */
482 void
483 akbd_processevent(struct akbd_softc *sc, adb_event_t *event)
484 {
485 	switch (event->byte_count) {
486 	case 1:
487 		akbd_capslockwrapper(sc, event->bytes[0]);
488 		break;
489 	case 2:
490 		/*
491 		 * The reset (or power) key sends 0x7f7f on press and
492 		 * 0xffff on release, and we ignore it.
493 		 */
494 		if (event->bytes[0] == event->bytes[1] &&
495 		    ADBK_KEYVAL(event->bytes[0]) == ADBK_RESET) {
496 			if (event->bytes[0] == ADBK_KEYDOWN(ADBK_RESET))
497 				SET(sc->sc_caps, CL_DOWN_RESET);
498 			else {
499 				if (ISSET(sc->sc_caps, CL_DOWN_RESET))
500 					CLR(sc->sc_caps, CL_DOWN_RESET);
501 				else if (ISSET(sc->sc_caps, CL_DOWN_ADB)) {
502 					akbd_input(sc, ISSET(sc->sc_caps,
503 					    CL_DOWN_LOGICAL) ?
504 					      ADBK_KEYDOWN(ADBK_CAPSLOCK) :
505 					      ADBK_KEYUP(ADBK_CAPSLOCK));
506 					sc->sc_caps ^= CL_DOWN_LOGICAL;
507 				}
508 			}
509 		} else {
510 			akbd_capslockwrapper(sc, event->bytes[0]);
511 			akbd_capslockwrapper(sc, event->bytes[1]);
512 		}
513 		break;
514 	default:
515 #ifdef DIAGNOSTIC
516 		printf("%s: unexpected message length %d\n",
517 		    sc->sc_dev.dv_xname, event->byte_count);
518 #endif
519 		break;
520 	}
521 
522 }
523 
524 void
525 akbd_capslockwrapper(struct akbd_softc *sc, int key)
526 {
527 	if (ADBK_KEYVAL(key) == ADBK_CAPSLOCK)
528 		sc->sc_caps ^= CL_DOWN_ADB;
529 
530 	if (key != 0xff)
531 		akbd_input(sc, key);
532 }
533 
534 int adb_polledkey;
535 void
536 akbd_input(struct akbd_softc *sc, int key)
537 {
538 	int press, val;
539 	int type;
540 
541 	press = ADBK_PRESS(key);
542 	val = ADBK_KEYVAL(key);
543 
544 	type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
545 
546 	if (adb_polling) {
547 		adb_polledkey = key;
548 #ifdef WSDISPLAY_COMPAT_RAWKBD
549 	} else if (sc->sc_rawkbd) {
550 		char cbuf[MAXKEYS *2];
551 		int c, j, s;
552 		int npress;
553 
554 		j = npress = 0;
555 
556 		c = keyboard[val];
557 		if (c == 0) {
558 			return; /* XXX */
559 		}
560 		if (c & 0x80)
561 			cbuf[j++] = 0xe0;
562 		cbuf[j] = c & 0x7f;
563 		if (type == WSCONS_EVENT_KEY_UP) {
564 			cbuf[j] |= 0x80;
565 		} else {
566 			/* this only records last key pressed */
567 			if (c & 0x80)
568 				sc->sc_rep[npress++] = 0xe0;
569 			sc->sc_rep[npress++] = c & 0x7f;
570 		}
571 		j++;
572 		s = spltty();
573 		wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
574 		splx(s);
575 		timeout_del(&sc->sc_rawrepeat_ch);
576 		sc->sc_nrep = npress;
577 		if (npress != 0)
578 			timeout_add_msec(&sc->sc_rawrepeat_ch, REP_DELAY1);
579 #endif
580 	} else {
581 		wskbd_input(sc->sc_wskbddev, type, val);
582 	}
583 }
584