Home | History | Annotate | Line # | Download | only in dev
akbd.c revision 1.9
      1 /*	$NetBSD: akbd.c,v 1.9 1999/09/05 05:30:30 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;
    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_JPKBDII:
    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 	case ADB_PBJPKBD:
    228 		printf("PowerBook keyboard (Japanese layout)\n");
    229 		break;
    230 	case ADB_PBG3JPKBD:
    231 		printf("PowerBook G3 keyboard (Japanese layout)\n");
    232 		break;
    233 	default:
    234 		printf("mapped device (%d)\n", sc->handler_id);
    235 		break;
    236 	}
    237 	error = SetADBInfo(&adbinfo, sc->adbaddr);
    238 #ifdef ADB_DEBUG
    239 	if (adb_debug)
    240 		printf("kbd: returned %d from SetADBInfo\n", error);
    241 #endif
    242 
    243 	a.console = akbd_is_console;
    244 	a.keymap = &akbd_keymapdata;
    245 	a.accessops = &akbd_accessops;
    246 	a.accesscookie = sc;
    247 
    248 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
    249 }
    250 
    251 
    252 /*
    253  * Handle putting the keyboard data received from the ADB into
    254  * an ADB event record.
    255  */
    256 void
    257 kbd_adbcomplete(buffer, data_area, adb_command)
    258 	caddr_t buffer;
    259 	caddr_t data_area;
    260 	int adb_command;
    261 {
    262 	adb_event_t event;
    263 	struct akbd_softc *ksc;
    264 	int adbaddr;
    265 #ifdef ADB_DEBUG
    266 	int i;
    267 
    268 	if (adb_debug)
    269 		printf("adb: transaction completion\n");
    270 #endif
    271 
    272 	adbaddr = (adb_command & 0xf0) >> 4;
    273 	ksc = (struct akbd_softc *)data_area;
    274 
    275 	event.addr = adbaddr;
    276 	event.hand_id = ksc->handler_id;
    277 	event.def_addr = ksc->origaddr;
    278 	event.byte_count = buffer[0];
    279 	memcpy(event.bytes, buffer + 1, event.byte_count);
    280 
    281 #ifdef ADB_DEBUG
    282 	if (adb_debug) {
    283 		printf("kbd: from %d at %d (org %d) %d:", event.addr,
    284 		    event.hand_id, event.def_addr, buffer[0]);
    285 		for (i = 1; i <= buffer[0]; i++)
    286 			printf(" %x", buffer[i]);
    287 		printf("\n");
    288 	}
    289 #endif
    290 
    291 	microtime(&event.timestamp);
    292 
    293 	kbd_processevent(&event, ksc);
    294 }
    295 
    296 /*
    297  * Given a keyboard ADB event, record the keycodes and call the key
    298  * repeat handler, optionally passing the event through the mouse
    299  * button emulation handler first.
    300  */
    301 static void
    302 kbd_processevent(event, ksc)
    303         adb_event_t *event;
    304         struct akbd_softc *ksc;
    305 {
    306         adb_event_t new_event;
    307 
    308         new_event = *event;
    309 	new_event.u.k.key = event->bytes[0];
    310 	new_event.bytes[1] = 0xff;
    311 	kbd_intr(&new_event);
    312 #if NAED > 0
    313 	aed_input(&new_event);
    314 #endif
    315 	if (event->bytes[1] != 0xff) {
    316 		new_event.u.k.key = event->bytes[1];
    317 		new_event.bytes[0] = event->bytes[1];
    318 		new_event.bytes[1] = 0xff;
    319 		kbd_intr(&new_event);
    320 #if NAED > 0
    321 		aed_input(&new_event);
    322 #endif
    323 	}
    324 
    325 }
    326 
    327 #ifdef notyet
    328 /*
    329  * Get the actual hardware LED state and convert it to softc format.
    330  */
    331 static u_char
    332 getleds(addr)
    333 	int	addr;
    334 {
    335 	short cmd;
    336 	u_char buffer[9], leds;
    337 
    338 	leds = 0x00;	/* all off */
    339 	buffer[0] = 0;
    340 	kbd_done = 0;
    341 
    342 	/* talk R2 */
    343 	cmd = ((addr & 0xf) << 4) | 0x0c | 0x02;
    344 	ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
    345 	while (!kbd_done)
    346 		/* busy-wait until done */ ;
    347 
    348 	if (buffer[0] > 0)
    349 		leds = ~(buffer[2]) & 0x07;
    350 
    351 	return (leds);
    352 }
    353 
    354 /*
    355  * Set the keyboard LED's.
    356  *
    357  * Automatically translates from ioctl/softc format to the
    358  * actual keyboard register format
    359  */
    360 static int
    361 setleds(ksc, leds)
    362 	struct akbd_softc *ksc;
    363 	u_char	leds;
    364 {
    365 	int addr;
    366 	short cmd;
    367 	u_char buffer[9];
    368 
    369 	if ((leds & 0x07) == (ksc->sc_leds & 0x07))
    370 		return (0);
    371 
    372 	addr = ksc->adbaddr;
    373 	buffer[0] = 0;
    374 	kbd_done = 0;
    375 
    376 	/* talk R2 */
    377 	cmd = ((addr & 0xf) << 4) | 0x0c | 0x02;
    378 	ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
    379 	while (!kbd_done)
    380 		/* busy-wait until done */ ;
    381 
    382 	if (buffer[0] == 0)
    383 		return (EIO);
    384 
    385 	leds = ~leds & 0x07;
    386 	buffer[2] &= 0xf8;
    387 	buffer[2] |= leds;
    388 
    389 	/* listen R2 */
    390 	cmd = ((addr & 0xf) << 4) | 0x08 | 0x02;
    391 	ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
    392 	while (!kbd_done)
    393 		/* busy-wait until done */ ;
    394 
    395 	/* talk R2 */
    396 	cmd = ((addr & 0xf) << 4) | 0x0c | 0x02;
    397 	ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
    398 	while (!kbd_done)
    399 		/* busy-wait until done */ ;
    400 
    401 	if (buffer[0] == 0)
    402 		return (EIO);
    403 
    404 	ksc->sc_leds = ~((u_int8_t)buffer[2]) & 0x07;
    405 
    406 	if ((buffer[2] & 0xf8) != leds)
    407 		return (EIO);
    408 	else
    409 		return (0);
    410 }
    411 
    412 /*
    413  * Toggle all of the LED's on and off, just for show.
    414  */
    415 static void
    416 blinkleds(ksc)
    417 	struct akbd_softc *ksc;
    418 {
    419 	int addr, i;
    420 	u_char blinkleds, origleds;
    421 
    422 	addr = ksc->adbaddr;
    423 	origleds = getleds(addr);
    424 	blinkleds = LED_NUMLOCK | LED_CAPSLOCK | LED_SCROLL_LOCK;
    425 
    426 	(void)setleds(ksc, blinkleds);
    427 
    428 	for (i = 0; i < 10000; i++)
    429 		delay(50);
    430 
    431 	/* make sure that we restore the LED settings */
    432 	i = 10;
    433 	do {
    434 		(void)setleds(ksc, (u_char)0x00);
    435 	} while (setleds(ksc, (u_char)0x00) && (i-- > 0));
    436 
    437 	return;
    438 }
    439 #endif
    440 
    441 int
    442 akbd_enable(v, on)
    443 	void *v;
    444 	int on;
    445 {
    446 	return 0;
    447 }
    448 
    449 void
    450 akbd_set_leds(v, on)
    451 	void *v;
    452 	int on;
    453 {
    454 }
    455 
    456 int
    457 akbd_ioctl(v, cmd, data, flag, p)
    458 	void *v;
    459 	u_long cmd;
    460 	caddr_t data;
    461 	int flag;
    462 	struct proc *p;
    463 {
    464 	switch (cmd) {
    465 
    466 	case WSKBDIO_GTYPE:
    467 		*(int *)data = 0;		/* XXX */
    468 		return 0;
    469 	case WSKBDIO_SETLEDS:
    470 		return 0;
    471 	case WSKBDIO_GETLEDS:
    472 		*(int *)data = 0;
    473 		return 0;
    474 	}
    475 	/* kbdioctl(...); */
    476 
    477 	return -1;
    478 }
    479 
    480 static int polledkey;
    481 extern int adb_polling;
    482 
    483 int
    484 kbd_intr(event)
    485 	adb_event_t *event;
    486 {
    487 	int key, press, val;
    488 	int type;
    489 
    490 	struct akbd_softc *sc = akbd_cd.cd_devs[0];
    491 
    492 	key = event->u.k.key;
    493 	press = ADBK_PRESS(key);
    494 	val = ADBK_KEYVAL(key);
    495 
    496 	type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
    497 
    498 	switch (key) {
    499 	case 185:	/* Caps Lock released */
    500 		type = WSCONS_EVENT_KEY_DOWN;
    501 		wskbd_input(sc->sc_wskbddev, type, val);
    502 		type = WSCONS_EVENT_KEY_UP;
    503 		break;
    504 	case 245:
    505 		pm_eject_pcmcia(0);
    506 		break;
    507 	case 244:
    508 		pm_eject_pcmcia(1);
    509 		break;
    510 	}
    511 
    512 	if (adb_polling)
    513 		polledkey = key;
    514 	else
    515 		wskbd_input(sc->sc_wskbddev, type, val);
    516 
    517 	return 0;
    518 }
    519 
    520 int
    521 akbd_cnattach()
    522 {
    523 
    524 	akbd_is_console = 1;
    525 	wskbd_cnattach(&akbd_consops, NULL, &akbd_keymapdata);
    526 	return 0;
    527 }
    528 
    529 void
    530 akbd_cngetc(v, type, data)
    531 	void *v;
    532 	u_int *type;
    533 	int *data;
    534 {
    535 	int key, press, val;
    536 	int s;
    537 
    538 	s = splhigh();
    539 
    540 	polledkey = -1;
    541 	adb_polling = 1;
    542 
    543 	while (polledkey == -1) {
    544 		adb_intr();
    545 		DELAY(10000);				/* XXX */
    546 	}
    547 
    548 	adb_polling = 0;
    549 	splx(s);
    550 
    551 	key = polledkey;
    552 	press = ADBK_PRESS(key);
    553 	val = ADBK_KEYVAL(key);
    554 
    555 	*data = val;
    556 	*type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
    557 }
    558 
    559 void
    560 akbd_cnpollc(v, on)
    561 	void *v;
    562 	int on;
    563 {
    564 }
    565