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