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