xref: /netbsd/sys/arch/mac68k/dev/akbd.c (revision c4a72b64)
1 /*	$NetBSD: akbd.c,v 1.15 2002/10/02 05:36:37 thorpej 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 "opt_adb.h"
34 
35 #include <sys/param.h>
36 #include <sys/device.h>
37 #include <sys/fcntl.h>
38 #include <sys/poll.h>
39 #include <sys/select.h>
40 #include <sys/proc.h>
41 #include <sys/signalvar.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 
45 #include "aed.h"
46 #include "wskbd.h"
47 
48 #include <dev/wscons/wsconsio.h>
49 #include <dev/wscons/wskbdvar.h>
50 #include <dev/wscons/wsksymdef.h>
51 #include <dev/wscons/wsksymvar.h>
52 
53 #include <machine/autoconf.h>
54 #include <machine/cpu.h>
55 #define KEYBOARD_ARRAY
56 #include <machine/keyboard.h>
57 #include <machine/viareg.h>
58 
59 #include <mac68k/mac68k/macrom.h>
60 #include <mac68k/dev/adbvar.h>
61 #include <mac68k/dev/aedvar.h>
62 #include <mac68k/dev/akbdmap.h>
63 #include <mac68k/dev/akbdvar.h>
64 #include <mac68k/dev/amsvar.h>
65 
66 /*
67  * Function declarations.
68  */
69 static int	akbdmatch __P((struct device *, struct cfdata *, void *));
70 static void	akbdattach __P((struct device *, struct device *, void *));
71 void		kbd_adbcomplete __P((caddr_t buffer, caddr_t data_area, int adb_command));
72 static void	kbd_processevent __P((adb_event_t *event, struct akbd_softc *));
73 #ifdef notyet
74 static u_char	getleds __P((int));
75 static int	setleds __P((struct akbd_softc *, u_char));
76 static void	blinkleds __P((struct akbd_softc *));
77 #endif
78 
79 /*
80  * Local variables.
81  */
82 
83 /* Driver definition. */
84 CFATTACH_DECL(akbd, sizeof(struct akbd_softc),
85     akbdmatch, akbdattach, NULL, NULL);
86 
87 extern struct cfdriver akbd_cd;
88 
89 int kbd_intr __P((adb_event_t *event, struct akbd_softc *));
90 int akbd_enable __P((void *, int));
91 void akbd_set_leds __P((void *, int));
92 int akbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
93 
94 struct wskbd_accessops akbd_accessops = {
95 	akbd_enable,
96 	akbd_set_leds,
97 	akbd_ioctl,
98 };
99 
100 void akbd_cngetc __P((void *, u_int *, int *));
101 void akbd_cnpollc __P((void *, int));
102 int akbd_cnattach __P((void));
103 
104 struct wskbd_consops akbd_consops = {
105 	akbd_cngetc,
106 	akbd_cnpollc,
107 };
108 
109 struct wskbd_mapdata akbd_keymapdata = {
110 	akbd_keydesctab,
111 	KB_US,
112 };
113 
114 static int akbd_is_console __P((void));
115 
116 static int
117 akbdmatch(parent, cf, aux)
118 	struct device *parent;
119 	struct cfdata *cf;
120 	void   *aux;
121 {
122 	struct adb_attach_args *aa_args = (struct adb_attach_args *)aux;
123 
124 	if (aa_args->origaddr == ADBADDR_KBD)
125 		return 1;
126 	else
127 		return 0;
128 }
129 
130 static void
131 akbdattach(parent, self, aux)
132 	struct device *parent, *self;
133 	void   *aux;
134 {
135 	ADBSetInfoBlock adbinfo;
136 	struct akbd_softc *sc = (struct akbd_softc *)self;
137 	struct adb_attach_args *aa_args = (struct adb_attach_args *)aux;
138 	int error, kbd_done;
139 	short cmd;
140 	u_char buffer[9];
141 #if NWSKBD > 0
142 	struct wskbddev_attach_args a;
143 	static int akbd_console_initted = 0;
144 	int wskbd_eligible;
145 
146 	wskbd_eligible = 1;
147 #endif
148 
149 	sc->origaddr = aa_args->origaddr;
150 	sc->adbaddr = aa_args->adbaddr;
151 	sc->handler_id = aa_args->handler_id;
152 
153 	sc->sc_leds = (u_int8_t)0x00;	/* initially off */
154 
155 	adbinfo.siServiceRtPtr = (Ptr)adb_kbd_asmcomplete;
156 	adbinfo.siDataAreaAddr = (caddr_t)sc;
157 
158 	switch (sc->handler_id) {
159 	case ADB_STDKBD:
160 		printf("standard keyboard\n");
161 		break;
162 	case ADB_ISOKBD:
163 		printf("standard keyboard (ISO layout)\n");
164 		break;
165 	case ADB_EXTKBD:
166 		cmd = ADBTALK(sc->adbaddr, 1);
167 		kbd_done =
168 		    (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0);
169 
170 		/* Ignore Logitech MouseMan/Trackman pseudo keyboard */
171 		if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x20) {
172 			printf("Mouseman (non-EMP) pseudo keyboard\n");
173 			adbinfo.siServiceRtPtr = (Ptr)0;
174 			adbinfo.siDataAreaAddr = (Ptr)0;
175 #if NWSKBD > 0
176 			wskbd_eligible = 0;
177 #endif /* NWSKBD > 0 */
178 		} else if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x21) {
179 			printf("Trackman (non-EMP) pseudo keyboard\n");
180 			adbinfo.siServiceRtPtr = (Ptr)0;
181 			adbinfo.siDataAreaAddr = (Ptr)0;
182 #if NWSKBD > 0
183 			wskbd_eligible = 0;
184 #endif /* NWSKBD > 0 */
185 		} else {
186 			printf("extended keyboard\n");
187 #ifdef notyet
188 			blinkleds(sc);
189 #endif
190 		}
191 		break;
192 	case ADB_EXTISOKBD:
193 		printf("extended keyboard (ISO layout)\n");
194 #ifdef notyet
195 		blinkleds(sc);
196 #endif
197 		break;
198 	case ADB_KBDII:
199 		printf("keyboard II\n");
200 		break;
201 	case ADB_ISOKBDII:
202 		printf("keyboard II (ISO layout)\n");
203 		break;
204 	case ADB_PBKBD:
205 		printf("PowerBook keyboard\n");
206 		break;
207 	case ADB_PBISOKBD:
208 		printf("PowerBook keyboard (ISO layout)\n");
209 		break;
210 	case ADB_ADJKPD:
211 		printf("adjustable keypad\n");
212 #if NWSKBD > 0
213 		wskbd_eligible = 0;
214 #endif /* NWSKBD > 0 */
215 		break;
216 	case ADB_ADJKBD:
217 		printf("adjustable keyboard\n");
218 		break;
219 	case ADB_ADJISOKBD:
220 		printf("adjustable keyboard (ISO layout)\n");
221 		break;
222 	case ADB_ADJJAPKBD:
223 		printf("adjustable keyboard (Japanese layout)\n");
224 		break;
225 	case ADB_PBEXTISOKBD:
226 		printf("PowerBook extended keyboard (ISO layout)\n");
227 		break;
228 	case ADB_PBEXTJAPKBD:
229 		printf("PowerBook extended keyboard (Japanese layout)\n");
230 		break;
231 	case ADB_JPKBDII:
232 		printf("keyboard II (Japanese layout)\n");
233 		break;
234 	case ADB_PBEXTKBD:
235 		printf("PowerBook extended keyboard\n");
236 		break;
237 	case ADB_DESIGNKBD:
238 		printf("extended keyboard\n");
239 #ifdef notyet
240 		blinkleds(sc);
241 #endif
242 		break;
243 	case ADB_PBJPKBD:
244 		printf("PowerBook keyboard (Japanese layout)\n");
245 		break;
246 	default:
247 		printf("mapped device (%d)\n", sc->handler_id);
248 #if NWSKBD > 0
249 		wskbd_eligible = 0;
250 #endif /* NWSKBD > 0 */
251 		break;
252 	}
253 	error = SetADBInfo(&adbinfo, sc->adbaddr);
254 #ifdef ADB_DEBUG
255 	if (adb_debug)
256 		printf("akbd: returned %d from SetADBInfo\n", error);
257 #endif
258 
259 #if NWSKBD > 0
260 	if (akbd_is_console() && wskbd_eligible)
261 		a.console = (++akbd_console_initted == 1);
262 	else
263 		a.console = 0;
264 	a.keymap = &akbd_keymapdata;
265 	a.accessops = &akbd_accessops;
266 	a.accesscookie = sc;
267 
268 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
269 #endif
270 }
271 
272 
273 /*
274  * Handle putting the keyboard data received from the ADB into
275  * an ADB event record.
276  */
277 void
278 kbd_adbcomplete(buffer, data_area, adb_command)
279 	caddr_t buffer;
280 	caddr_t data_area;
281 	int adb_command;
282 {
283 	adb_event_t event;
284 	struct akbd_softc *ksc;
285 	int adbaddr;
286 #ifdef ADB_DEBUG
287 	int i;
288 
289 	if (adb_debug)
290 		printf("adb: transaction completion\n");
291 #endif
292 
293 	adbaddr = ADB_CMDADDR(adb_command);
294 	ksc = (struct akbd_softc *)data_area;
295 
296 	event.addr = adbaddr;
297 	event.hand_id = ksc->handler_id;
298 	event.def_addr = ksc->origaddr;
299 	event.byte_count = buffer[0];
300 	memcpy(event.bytes, buffer + 1, event.byte_count);
301 
302 #ifdef ADB_DEBUG
303 	if (adb_debug) {
304 		printf("akbd: from %d at %d (org %d) %d:", event.addr,
305 		    event.hand_id, event.def_addr, buffer[0]);
306 		for (i = 1; i <= buffer[0]; i++)
307 			printf(" %x", buffer[i]);
308 		printf("\n");
309 	}
310 #endif
311 
312 	microtime(&event.timestamp);
313 
314 	kbd_processevent(&event, ksc);
315 }
316 
317 /*
318  * Given a keyboard ADB event, record the keycodes and call the key
319  * repeat handler, optionally passing the event through the mouse
320  * button emulation handler first.
321  */
322 static void
323 kbd_processevent(event, ksc)
324         adb_event_t *event;
325         struct akbd_softc *ksc;
326 {
327         adb_event_t new_event;
328 
329         new_event = *event;
330 	new_event.u.k.key = event->bytes[0];
331 	new_event.bytes[1] = 0xff;
332 #if NAED > 0
333 	if (adb_polling || !aed_input(&new_event))
334 #endif
335 #if NWSKBD > 0
336 		if (ksc->sc_wskbddev != NULL) /* wskbd is attached? */
337 			kbd_intr(&new_event, ksc);
338 #else
339 		/* do nothing */ ;
340 #endif
341 	if (event->bytes[1] != 0xff) {
342 		new_event.u.k.key = event->bytes[1];
343 		new_event.bytes[0] = event->bytes[1];
344 		new_event.bytes[1] = 0xff;
345 #if NAED > 0
346 		if (adb_polling || !aed_input(&new_event))
347 #endif
348 #if NWSKBD > 0
349 			if (ksc->sc_wskbddev != NULL) /* wskbd is attached? */
350 				kbd_intr(&new_event, ksc);
351 #else
352 			/* do nothing */ ;
353 #endif
354 	}
355 
356 }
357 
358 #ifdef notyet
359 /*
360  * Get the actual hardware LED state and convert it to softc format.
361  */
362 static u_char
363 getleds(addr)
364 	int	addr;
365 {
366 	short cmd;
367 	u_char buffer[9], leds;
368 
369 	leds = 0x00;	/* all off */
370 	buffer[0] = 0;
371 
372 	cmd = ADBTALK(addr, 2);
373 	if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0 &&
374 	    buffer[0] > 0)
375 		leds = ~(buffer[2]) & 0x07;
376 
377 	return (leds);
378 }
379 
380 /*
381  * Set the keyboard LED's.
382  *
383  * Automatically translates from ioctl/softc format to the
384  * actual keyboard register format
385  */
386 static int
387 setleds(ksc, leds)
388 	struct akbd_softc *ksc;
389 	u_char	leds;
390 {
391 	int addr;
392 	short cmd;
393 	u_char buffer[9];
394 
395 	if ((leds & 0x07) == (ksc->sc_leds & 0x07))
396 		return (0);
397 
398 	addr = ksc->adbaddr;
399 	buffer[0] = 0;
400 
401 	cmd = ADBTALK(addr, 2);
402 	if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) || buffer[0] == 0)
403 		return (EIO);
404 
405 	leds = ~leds & 0x07;
406 	buffer[2] &= 0xf8;
407 	buffer[2] |= leds;
408 
409 	cmd = ADBLISTEN(addr, 2);
410 	adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
411 
412 	/* talk R2 */
413 	cmd = ADBTALK(addr, 2);
414 	if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) || buffer[0] == 0)
415 		return (EIO);
416 
417 	ksc->sc_leds = ~((u_int8_t)buffer[2]) & 0x07;
418 
419 	if ((buffer[2] & 0xf8) != leds)
420 		return (EIO);
421 	else
422 		return (0);
423 }
424 
425 /*
426  * Toggle all of the LED's on and off, just for show.
427  */
428 static void
429 blinkleds(ksc)
430 	struct akbd_softc *ksc;
431 {
432 	int addr, i;
433 	u_char blinkleds, origleds;
434 
435 	addr = ksc->adbaddr;
436 	origleds = getleds(addr);
437 	blinkleds = LED_NUMLOCK | LED_CAPSLOCK | LED_SCROLL_LOCK;
438 
439 	(void)setleds(ksc, blinkleds);
440 
441 	for (i = 0; i < 10000; i++)
442 		delay(50);
443 
444 	/* make sure that we restore the LED settings */
445 	i = 10;
446 	do {
447 		(void)setleds(ksc, (u_char)0x00);
448 	} while (setleds(ksc, (u_char)0x00) && (i-- > 0));
449 
450 	return;
451 }
452 #endif
453 
454 int
455 akbd_is_console()
456 {
457 	extern struct mac68k_machine_S mac68k_machine;
458 
459 	return ((mac68k_machine.serial_console & 0x03) == 0);
460 }
461 
462 int
463 akbd_enable(v, on)
464 	void *v;
465 	int on;
466 {
467 	return 0;
468 }
469 
470 void
471 akbd_set_leds(v, on)
472 	void *v;
473 	int on;
474 {
475 }
476 
477 int
478 akbd_ioctl(v, cmd, data, flag, p)
479 	void *v;
480 	u_long cmd;
481 	caddr_t data;
482 	int flag;
483 	struct proc *p;
484 {
485 	switch (cmd) {
486 
487 	case WSKBDIO_GTYPE:
488 		*(int *)data = 0;		/* XXX */
489 		return 0;
490 	case WSKBDIO_SETLEDS:
491 		return 0;
492 	case WSKBDIO_GETLEDS:
493 		*(int *)data = 0;
494 		return 0;
495 	case WSKBDIO_BELL:
496 	case WSKBDIO_COMPLEXBELL:
497 #define d ((struct wskbd_bell_data *)data)
498 		mac68k_ring_bell(d->pitch, d->period * hz / 1000, 100);
499 		/* comes in as msec, goes out as ticks; volume ignored */
500 #undef d
501 		return (0);
502 	}
503 	/* kbdioctl(...); */
504 
505 	return EPASSTHROUGH;
506 }
507 
508 static int polledkey;
509 extern int adb_polling;
510 
511 int
512 kbd_intr(event, sc)
513 	adb_event_t *event;
514 	struct akbd_softc *sc;
515 {
516 	int key, press, val;
517 	int type;
518 
519 	key = event->u.k.key;
520 	press = ADBK_PRESS(key);
521 	val = ADBK_KEYVAL(key);
522 
523 	type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
524 
525 	if (key == 185) {	/* Caps Lock released */
526 		type = WSCONS_EVENT_KEY_DOWN;
527 		wskbd_input(sc->sc_wskbddev, type, val);
528 		type = WSCONS_EVENT_KEY_UP;
529 	}
530 
531 	if (adb_polling)
532 		polledkey = key;
533 	else
534 		wskbd_input(sc->sc_wskbddev, type, val);
535 
536 	return 0;
537 }
538 
539 int
540 akbd_cnattach()
541 {
542 	wskbd_cnattach(&akbd_consops, NULL, &akbd_keymapdata);
543 
544 	return 0;
545 }
546 
547 void
548 akbd_cngetc(v, type, data)
549 	void *v;
550 	u_int *type;
551 	int *data;
552 {
553 	int intbits, key, press, val;
554 	int s;
555 
556 	s = splhigh();
557 
558 	polledkey = -1;
559 	adb_polling = 1;
560 
561 	while (polledkey == -1) {
562 		intbits = via_reg(VIA1, vIFR);
563 
564 		if (intbits & V1IF_ADBRDY) {
565 			mrg_adbintr();
566 			via_reg(VIA1, vIFR) = V1IF_ADBRDY;
567 		}
568 		if (intbits & 0x10) {
569 			mrg_pmintr();
570 			via_reg(VIA1, vIFR) = 0x10;
571 		}
572 	}
573 
574 	adb_polling = 0;
575 	splx(s);
576 
577 	key = polledkey;
578 	press = ADBK_PRESS(key);
579 	val = ADBK_KEYVAL(key);
580 
581 	*data = val;
582 	*type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
583 }
584 
585 void
586 akbd_cnpollc(v, on)
587 	void *v;
588 	int on;
589 {
590 }
591