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