Home | History | Annotate | Line # | Download | only in dev
akbd.c revision 1.3.2.4
      1 /*	$NetBSD: akbd.c,v 1.3.2.4 1999/03/11 19:21:43 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 NWSKBD > 0
    321 	kbd_intr(&new_event);
    322 #endif
    323 #if NAED > 0
    324 	aed_input(&new_event);
    325 #endif
    326 	if (event->bytes[1] != 0xff) {
    327 		new_event.u.k.key = event->bytes[1];
    328 		new_event.bytes[0] = event->bytes[1];
    329 		new_event.bytes[1] = 0xff;
    330 #if NWSKBD > 0
    331 		kbd_intr(&new_event);
    332 #endif
    333 #if NAED > 0
    334 		aed_input(&new_event);
    335 #endif
    336 	}
    337 
    338 }
    339 
    340 #ifdef notyet
    341 /*
    342  * Get the actual hardware LED state and convert it to softc format.
    343  */
    344 static u_char
    345 getleds(addr)
    346 	int	addr;
    347 {
    348 	short cmd;
    349 	u_char buffer[9], leds;
    350 
    351 	leds = 0x00;	/* all off */
    352 	buffer[0] = 0;
    353 	kbd_done = 0;
    354 
    355 	/* talk R2 */
    356 	cmd = ((addr & 0xf) << 4) | 0x0c | 0x02;
    357 	ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
    358 	while (!kbd_done)
    359 		/* busy-wait until done */ ;
    360 
    361 	if (buffer[0] > 0)
    362 		leds = ~(buffer[2]) & 0x07;
    363 
    364 	return (leds);
    365 }
    366 
    367 /*
    368  * Set the keyboard LED's.
    369  *
    370  * Automatically translates from ioctl/softc format to the
    371  * actual keyboard register format
    372  */
    373 static int
    374 setleds(ksc, leds)
    375 	struct akbd_softc *ksc;
    376 	u_char	leds;
    377 {
    378 	int addr;
    379 	short cmd;
    380 	u_char buffer[9];
    381 
    382 	if ((leds & 0x07) == (ksc->sc_leds & 0x07))
    383 		return (0);
    384 
    385 	addr = ksc->adbaddr;
    386 	buffer[0] = 0;
    387 	kbd_done = 0;
    388 
    389 	/* talk R2 */
    390 	cmd = ((addr & 0xf) << 4) | 0x0c | 0x02;
    391 	ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
    392 	while (!kbd_done)
    393 		/* busy-wait until done */ ;
    394 
    395 	if (buffer[0] == 0)
    396 		return (EIO);
    397 
    398 	leds = ~leds & 0x07;
    399 	buffer[2] &= 0xf8;
    400 	buffer[2] |= leds;
    401 
    402 	/* listen R2 */
    403 	cmd = ((addr & 0xf) << 4) | 0x08 | 0x02;
    404 	ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
    405 	while (!kbd_done)
    406 		/* busy-wait until done */ ;
    407 
    408 	/* talk R2 */
    409 	cmd = ((addr & 0xf) << 4) | 0x0c | 0x02;
    410 	ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
    411 	while (!kbd_done)
    412 		/* busy-wait until done */ ;
    413 
    414 	if (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 -1;
    506 }
    507 
    508 static int polledkey;
    509 extern int adb_polling;
    510 
    511 int
    512 kbd_intr(event)
    513 	adb_event_t *event;
    514 {
    515 	int key, press, val;
    516 	int type;
    517 
    518 	struct akbd_softc *sc = akbd_cd.cd_devs[0];
    519 
    520 	key = event->u.k.key;
    521 	press = ADBK_PRESS(key);
    522 	val = ADBK_KEYVAL(key);
    523 
    524 	type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
    525 
    526 	if (key == 185) {	/* Caps Lock released */
    527 		type = WSCONS_EVENT_KEY_DOWN;
    528 		wskbd_input(sc->sc_wskbddev, type, val);
    529 		type = WSCONS_EVENT_KEY_UP;
    530 	}
    531 
    532 	if (adb_polling)
    533 		polledkey = key;
    534 	else
    535 		wskbd_input(sc->sc_wskbddev, type, val);
    536 
    537 	return 0;
    538 }
    539 
    540 int
    541 akbd_cnattach()
    542 {
    543 	if (!akbd_is_console())
    544 		return -1;
    545 
    546 	wskbd_cnattach(&akbd_consops, NULL, &akbd_keymapdata);
    547 
    548 	return 0;
    549 }
    550 
    551 void
    552 akbd_cngetc(v, type, data)
    553 	void *v;
    554 	u_int *type;
    555 	int *data;
    556 {
    557 	int intbits, key, press, val;
    558 	int s;
    559 
    560 	s = splhigh();
    561 
    562 	polledkey = -1;
    563 	adb_polling = 1;
    564 
    565 	while (polledkey == -1) {
    566 		intbits = via_reg(VIA1, vIFR);
    567 
    568 		if (intbits & V1IF_ADBRDY) {
    569 			mrg_adbintr();
    570 			via_reg(VIA1, vIFR) = V1IF_ADBRDY;
    571 		}
    572 		if (intbits & 0x10) {
    573 			mrg_pmintr();
    574 			via_reg(VIA1, vIFR) = 0x10;
    575 		}
    576 	}
    577 
    578 	adb_polling = 0;
    579 	splx(s);
    580 
    581 	key = polledkey;
    582 	press = ADBK_PRESS(key);
    583 	val = ADBK_KEYVAL(key);
    584 
    585 	*data = val;
    586 	*type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
    587 }
    588 
    589 void
    590 akbd_cnpollc(v, on)
    591 	void *v;
    592 	int on;
    593 {
    594 }
    595