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