Home | History | Annotate | Line # | Download | only in dev
akbd.c revision 1.12
      1 /*	$NetBSD: akbd.c,v 1.12 2000/12/19 03:13:40 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 
     56 #include "aed.h"
     57 
     58 /*
     59  * Function declarations.
     60  */
     61 static int	akbdmatch __P((struct device *, struct cfdata *, void *));
     62 static void	akbdattach __P((struct device *, struct device *, void *));
     63 void		kbd_adbcomplete __P((caddr_t buffer, caddr_t data_area, int adb_command));
     64 static void	kbd_processevent __P((adb_event_t *event, struct akbd_softc *));
     65 #ifdef notyet
     66 static u_char	getleds __P((int));
     67 static int	setleds __P((struct akbd_softc *, u_char));
     68 static void	blinkleds __P((struct akbd_softc *));
     69 #endif
     70 
     71 /* Driver definition. */
     72 struct cfattach akbd_ca = {
     73 	sizeof(struct akbd_softc), akbdmatch, akbdattach
     74 };
     75 
     76 extern struct cfdriver akbd_cd;
     77 
     78 int akbd_enable __P((void *, int));
     79 void akbd_set_leds __P((void *, int));
     80 int akbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
     81 
     82 struct wskbd_accessops akbd_accessops = {
     83 	akbd_enable,
     84 	akbd_set_leds,
     85 	akbd_ioctl,
     86 };
     87 
     88 void akbd_cngetc __P((void *, u_int *, int *));
     89 void akbd_cnpollc __P((void *, int));
     90 
     91 struct wskbd_consops akbd_consops = {
     92 	akbd_cngetc,
     93 	akbd_cnpollc,
     94 };
     95 
     96 struct wskbd_mapdata akbd_keymapdata = {
     97 	akbd_keydesctab,
     98 #ifdef AKBD_LAYOUT
     99 	AKBD_LAYOUT,
    100 #else
    101 	KB_US,
    102 #endif
    103 };
    104 
    105 static int akbd_is_console;
    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 = 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 = aux;
    129 	int error, kbd_done;
    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 		cmd = ADBTALK(sc->adbaddr, 1);
    152 		kbd_done =
    153 		    (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0);
    154 
    155 		/* Ignore Logitech MouseMan/Trackman pseudo keyboard */
    156 		if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x20) {
    157 			printf("Mouseman (non-EMP) pseudo keyboard\n");
    158 			adbinfo.siServiceRtPtr = (Ptr)0;
    159 			adbinfo.siDataAreaAddr = (Ptr)0;
    160 		} else if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x21) {
    161 			printf("Trackman (non-EMP) pseudo keyboard\n");
    162 			adbinfo.siServiceRtPtr = (Ptr)0;
    163 			adbinfo.siDataAreaAddr = (Ptr)0;
    164 		} else {
    165 			printf("extended keyboard\n");
    166 #ifdef notyet
    167 			blinkleds(sc);
    168 #endif
    169 		}
    170 		break;
    171 	case ADB_EXTISOKBD:
    172 		printf("extended keyboard (ISO layout)\n");
    173 #ifdef notyet
    174 		blinkleds(sc);
    175 #endif
    176 		break;
    177 	case ADB_KBDII:
    178 		printf("keyboard II\n");
    179 		break;
    180 	case ADB_ISOKBDII:
    181 		printf("keyboard II (ISO layout)\n");
    182 		break;
    183 	case ADB_PBKBD:
    184 		printf("PowerBook keyboard\n");
    185 		break;
    186 	case ADB_PBISOKBD:
    187 		printf("PowerBook keyboard (ISO layout)\n");
    188 		break;
    189 	case ADB_ADJKPD:
    190 		printf("adjustable keypad\n");
    191 		break;
    192 	case ADB_ADJKBD:
    193 		printf("adjustable keyboard\n");
    194 		break;
    195 	case ADB_ADJISOKBD:
    196 		printf("adjustable keyboard (ISO layout)\n");
    197 		break;
    198 	case ADB_ADJJAPKBD:
    199 		printf("adjustable keyboard (Japanese layout)\n");
    200 		break;
    201 	case ADB_PBEXTISOKBD:
    202 		printf("PowerBook extended keyboard (ISO layout)\n");
    203 		break;
    204 	case ADB_PBEXTJAPKBD:
    205 		printf("PowerBook extended keyboard (Japanese layout)\n");
    206 		break;
    207 	case ADB_JPKBDII:
    208 		printf("keyboard II (Japanese layout)\n");
    209 		break;
    210 	case ADB_PBEXTKBD:
    211 		printf("PowerBook extended keyboard\n");
    212 		break;
    213 	case ADB_DESIGNKBD:
    214 		printf("extended keyboard\n");
    215 #ifdef notyet
    216 		blinkleds(sc);
    217 #endif
    218 		break;
    219 	case ADB_PBJPKBD:
    220 		printf("PowerBook keyboard (Japanese layout)\n");
    221 		break;
    222 	case ADB_PBG3JPKBD:
    223 		printf("PowerBook G3 keyboard (Japanese layout)\n");
    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("akbd: 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_CMDADDR(adb_command);
    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("akbd: 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 	kbd_intr(&new_event);
    304 #if NAED > 0
    305 	aed_input(&new_event);
    306 #endif
    307 	if (event->bytes[1] != 0xff) {
    308 		new_event.u.k.key = event->bytes[1];
    309 		new_event.bytes[0] = event->bytes[1];
    310 		new_event.bytes[1] = 0xff;
    311 		kbd_intr(&new_event);
    312 #if NAED > 0
    313 		aed_input(&new_event);
    314 #endif
    315 	}
    316 
    317 }
    318 
    319 #ifdef notyet
    320 /*
    321  * Get the actual hardware LED state and convert it to softc format.
    322  */
    323 static u_char
    324 getleds(addr)
    325 	int	addr;
    326 {
    327 	short cmd;
    328 	u_char buffer[9], leds;
    329 
    330 	leds = 0x00;	/* all off */
    331 	buffer[0] = 0;
    332 
    333 	/* talk R2 */
    334 	cmd = ADBTALK(addr, 2);
    335 	if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0 &&
    336 	    buffer[0] > 0)
    337 		leds = ~(buffer[2]) & 0x07;
    338 
    339 	return (leds);
    340 }
    341 
    342 /*
    343  * Set the keyboard LED's.
    344  *
    345  * Automatically translates from ioctl/softc format to the
    346  * actual keyboard register format
    347  */
    348 static int
    349 setleds(ksc, leds)
    350 	struct akbd_softc *ksc;
    351 	u_char	leds;
    352 {
    353 	int addr;
    354 	short cmd;
    355 	u_char buffer[9];
    356 
    357 	if ((leds & 0x07) == (ksc->sc_leds & 0x07))
    358 		return (0);
    359 
    360 	addr = ksc->adbaddr;
    361 	buffer[0] = 0;
    362 
    363 	cmd = ADBTALK(addr, 2);
    364 	if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) || buffer[0] == 0)
    365 		return (EIO);
    366 
    367 	leds = ~leds & 0x07;
    368 	buffer[2] &= 0xf8;
    369 	buffer[2] |= leds;
    370 
    371 	cmd = ADBLISTEN(addr, 2);
    372 	adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
    373 
    374 	cmd = ADBTALK(addr, 2);
    375 	if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) || buffer[0] == 0)
    376 		return (EIO);
    377 
    378 	ksc->sc_leds = ~((u_int8_t)buffer[2]) & 0x07;
    379 
    380 	if ((buffer[2] & 0xf8) != leds)
    381 		return (EIO);
    382 	else
    383 		return (0);
    384 }
    385 
    386 /*
    387  * Toggle all of the LED's on and off, just for show.
    388  */
    389 static void
    390 blinkleds(ksc)
    391 	struct akbd_softc *ksc;
    392 {
    393 	int addr, i;
    394 	u_char blinkleds, origleds;
    395 
    396 	addr = ksc->adbaddr;
    397 	origleds = getleds(addr);
    398 	blinkleds = LED_NUMLOCK | LED_CAPSLOCK | LED_SCROLL_LOCK;
    399 
    400 	(void)setleds(ksc, blinkleds);
    401 
    402 	for (i = 0; i < 10000; i++)
    403 		delay(50);
    404 
    405 	/* make sure that we restore the LED settings */
    406 	i = 10;
    407 	do {
    408 		(void)setleds(ksc, (u_char)0x00);
    409 	} while (setleds(ksc, (u_char)0x00) && (i-- > 0));
    410 
    411 	return;
    412 }
    413 #endif
    414 
    415 int
    416 akbd_enable(v, on)
    417 	void *v;
    418 	int on;
    419 {
    420 	return 0;
    421 }
    422 
    423 void
    424 akbd_set_leds(v, on)
    425 	void *v;
    426 	int on;
    427 {
    428 }
    429 
    430 int
    431 akbd_ioctl(v, cmd, data, flag, p)
    432 	void *v;
    433 	u_long cmd;
    434 	caddr_t data;
    435 	int flag;
    436 	struct proc *p;
    437 {
    438 	switch (cmd) {
    439 
    440 	case WSKBDIO_GTYPE:
    441 		*(int *)data = 0;		/* XXX */
    442 		return 0;
    443 	case WSKBDIO_SETLEDS:
    444 		return 0;
    445 	case WSKBDIO_GETLEDS:
    446 		*(int *)data = 0;
    447 		return 0;
    448 	}
    449 	/* kbdioctl(...); */
    450 
    451 	return -1;
    452 }
    453 
    454 static int polledkey;
    455 extern int adb_polling;
    456 
    457 int
    458 kbd_intr(event)
    459 	adb_event_t *event;
    460 {
    461 	int key, press, val;
    462 	int type;
    463 
    464 	struct akbd_softc *sc = akbd_cd.cd_devs[0];
    465 
    466 	key = event->u.k.key;
    467 	press = ADBK_PRESS(key);
    468 	val = ADBK_KEYVAL(key);
    469 
    470 	type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
    471 
    472 	switch (key) {
    473 	case 185:	/* Caps Lock released */
    474 		type = WSCONS_EVENT_KEY_DOWN;
    475 		wskbd_input(sc->sc_wskbddev, type, val);
    476 		type = WSCONS_EVENT_KEY_UP;
    477 		break;
    478 	case 245:
    479 		pm_eject_pcmcia(0);
    480 		break;
    481 	case 244:
    482 		pm_eject_pcmcia(1);
    483 		break;
    484 	}
    485 
    486 	if (adb_polling)
    487 		polledkey = key;
    488 	else
    489 		wskbd_input(sc->sc_wskbddev, type, val);
    490 
    491 	return 0;
    492 }
    493 
    494 int
    495 akbd_cnattach()
    496 {
    497 
    498 	akbd_is_console = 1;
    499 	wskbd_cnattach(&akbd_consops, NULL, &akbd_keymapdata);
    500 	return 0;
    501 }
    502 
    503 void
    504 akbd_cngetc(v, type, data)
    505 	void *v;
    506 	u_int *type;
    507 	int *data;
    508 {
    509 	int key, press, val;
    510 	int s;
    511 
    512 	s = splhigh();
    513 
    514 	polledkey = -1;
    515 	adb_polling = 1;
    516 
    517 	while (polledkey == -1) {
    518 		adb_intr();
    519 		DELAY(10000);				/* XXX */
    520 	}
    521 
    522 	adb_polling = 0;
    523 	splx(s);
    524 
    525 	key = polledkey;
    526 	press = ADBK_PRESS(key);
    527 	val = ADBK_KEYVAL(key);
    528 
    529 	*data = val;
    530 	*type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
    531 }
    532 
    533 void
    534 akbd_cnpollc(v, on)
    535 	void *v;
    536 	int on;
    537 {
    538 }
    539