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