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