Home | History | Annotate | Line # | Download | only in dev
adb.c revision 1.1
      1  1.1  tsubai /*	$NetBSD: adb.c,v 1.1 1998/05/15 10:15:47 tsubai Exp $	*/
      2  1.1  tsubai 
      3  1.1  tsubai /*-
      4  1.1  tsubai  * Copyright (C) 1994	Bradley A. Grantham
      5  1.1  tsubai  * All rights reserved.
      6  1.1  tsubai  *
      7  1.1  tsubai  * Redistribution and use in source and binary forms, with or without
      8  1.1  tsubai  * modification, are permitted provided that the following conditions
      9  1.1  tsubai  * are met:
     10  1.1  tsubai  * 1. Redistributions of source code must retain the above copyright
     11  1.1  tsubai  *    notice, this list of conditions and the following disclaimer.
     12  1.1  tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  tsubai  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  tsubai  *    documentation and/or other materials provided with the distribution.
     15  1.1  tsubai  * 3. All advertising materials mentioning features or use of this software
     16  1.1  tsubai  *    must display the following acknowledgement:
     17  1.1  tsubai  *	This product includes software developed by Bradley A. Grantham.
     18  1.1  tsubai  * 4. The name of the author may not be used to endorse or promote products
     19  1.1  tsubai  *    derived from this software without specific prior written permission.
     20  1.1  tsubai  *
     21  1.1  tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1  tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1  tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1  tsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1  tsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1  tsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1  tsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1  tsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1  tsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30  1.1  tsubai  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1  tsubai  */
     32  1.1  tsubai 
     33  1.1  tsubai #include <sys/param.h>
     34  1.1  tsubai #include <sys/device.h>
     35  1.1  tsubai #include <sys/fcntl.h>
     36  1.1  tsubai #include <sys/poll.h>
     37  1.1  tsubai #include <sys/select.h>
     38  1.1  tsubai #include <sys/proc.h>
     39  1.1  tsubai #include <sys/signalvar.h>
     40  1.1  tsubai #include <sys/systm.h>
     41  1.1  tsubai 
     42  1.1  tsubai #include <machine/autoconf.h>
     43  1.1  tsubai #include <machine/keyboard.h>
     44  1.1  tsubai 
     45  1.1  tsubai #include <macppc/dev/adbvar.h>
     46  1.1  tsubai #include <macppc/dev/viareg.h>
     47  1.1  tsubai 
     48  1.1  tsubai #define spladb splhigh		/* XXX */
     49  1.1  tsubai 
     50  1.1  tsubai /*
     51  1.1  tsubai  * Function declarations.
     52  1.1  tsubai  */
     53  1.1  tsubai static int	adbmatch __P((struct device *, struct cfdata *, void *));
     54  1.1  tsubai static void	adbattach __P((struct device *, struct device *, void *));
     55  1.1  tsubai 
     56  1.1  tsubai /*
     57  1.1  tsubai  * Global variables.
     58  1.1  tsubai  */
     59  1.1  tsubai int     adb_polling = 0;	/* Are we polling?  (Debugger mode) */
     60  1.1  tsubai int     adb_initted = 0;	/* adb_init() has completed successfully */
     61  1.1  tsubai #ifdef ADB_DEBUG
     62  1.1  tsubai int	adb_debug = 0;		/* Output debugging messages */
     63  1.1  tsubai #endif /* ADB_DEBUG */
     64  1.1  tsubai 
     65  1.1  tsubai volatile u_char *Via1Base;
     66  1.1  tsubai 
     67  1.1  tsubai /*
     68  1.1  tsubai  * Local variables.
     69  1.1  tsubai  */
     70  1.1  tsubai 
     71  1.1  tsubai /* External keyboard translation matrix */
     72  1.1  tsubai extern unsigned char keyboard[128][3];
     73  1.1  tsubai 
     74  1.1  tsubai /* Event queue definitions */
     75  1.1  tsubai #if !defined(ADB_MAX_EVENTS)
     76  1.1  tsubai #define ADB_MAX_EVENTS 200	/* Maximum events to be kept in queue */
     77  1.1  tsubai 				/* maybe should be higher for slower macs? */
     78  1.1  tsubai #endif				/* !defined(ADB_MAX_EVENTS) */
     79  1.1  tsubai static adb_event_t adb_evq[ADB_MAX_EVENTS];	/* ADB event queue */
     80  1.1  tsubai static int adb_evq_tail = 0;	/* event queue tail */
     81  1.1  tsubai static int adb_evq_len = 0;	/* event queue length */
     82  1.1  tsubai 
     83  1.1  tsubai /* ADB device state information */
     84  1.1  tsubai static int adb_isopen = 0;	/* Are we queuing events for adb_read? */
     85  1.1  tsubai static struct selinfo adb_selinfo;	/* select() info */
     86  1.1  tsubai static struct proc *adb_ioproc = NULL;	/* process to wakeup */
     87  1.1  tsubai 
     88  1.1  tsubai /* Key repeat parameters */
     89  1.1  tsubai static int adb_rptdelay = 20;	/* ticks before auto-repeat */
     90  1.1  tsubai static int adb_rptinterval = 6;	/* ticks between auto-repeat */
     91  1.1  tsubai static int adb_repeating = -1;	/* key that is auto-repeating */
     92  1.1  tsubai static adb_event_t adb_rptevent;/* event to auto-repeat */
     93  1.1  tsubai 
     94  1.1  tsubai /* Mouse button state */
     95  1.1  tsubai static int adb_ms_buttons = 0;
     96  1.1  tsubai 
     97  1.1  tsubai /* Driver definition.  -- This should probably be a bus...  */
     98  1.1  tsubai struct cfattach adb_ca = {
     99  1.1  tsubai 	sizeof(struct adb_softc), adbmatch, adbattach
    100  1.1  tsubai };
    101  1.1  tsubai 
    102  1.1  tsubai static int
    103  1.1  tsubai adbmatch(parent, cf, aux)
    104  1.1  tsubai 	struct device *parent;
    105  1.1  tsubai 	struct cfdata *cf;
    106  1.1  tsubai 	void *aux;
    107  1.1  tsubai {
    108  1.1  tsubai 	struct confargs *ca = aux;
    109  1.1  tsubai 
    110  1.1  tsubai 	if (strcmp(ca->ca_name, "via-cuda") != 0)
    111  1.1  tsubai 		return 0;
    112  1.1  tsubai 
    113  1.1  tsubai 	if (ca->ca_nreg < 8)
    114  1.1  tsubai 		return 0;
    115  1.1  tsubai 
    116  1.1  tsubai 	if (ca->ca_nintr < 4)
    117  1.1  tsubai 		return 0;
    118  1.1  tsubai 
    119  1.1  tsubai 	return 1;
    120  1.1  tsubai }
    121  1.1  tsubai 
    122  1.1  tsubai static void
    123  1.1  tsubai adbattach(parent, self, aux)
    124  1.1  tsubai 	struct device *parent, *self;
    125  1.1  tsubai 	void   *aux;
    126  1.1  tsubai {
    127  1.1  tsubai 	struct adb_softc *sc = (struct adb_softc *)self;
    128  1.1  tsubai 	struct confargs *ca = aux;
    129  1.1  tsubai 	u_long time = -1;
    130  1.1  tsubai 	extern adb_intr();
    131  1.1  tsubai 
    132  1.1  tsubai 	ca->ca_reg[0] += ca->ca_baseaddr;
    133  1.1  tsubai 
    134  1.1  tsubai 	sc->sc_regbase = mapiodev(ca->ca_reg[0], ca->ca_reg[1]);
    135  1.1  tsubai 	Via1Base = sc->sc_regbase;
    136  1.1  tsubai 
    137  1.1  tsubai 	printf(" irq %d\n", ca->ca_intr[0]);
    138  1.1  tsubai 
    139  1.1  tsubai 	adb_polling = 1;
    140  1.1  tsubai 	adb_init();
    141  1.1  tsubai 	kbd_init();
    142  1.1  tsubai 	adb_polling = 0;
    143  1.1  tsubai 
    144  1.1  tsubai 	intr_establish(ca->ca_intr[0], IST_LEVEL, IPL_HIGH, adb_intr, sc);
    145  1.1  tsubai }
    146  1.1  tsubai 
    147  1.1  tsubai void
    148  1.1  tsubai adb_enqevent(event)
    149  1.1  tsubai 	adb_event_t *event;
    150  1.1  tsubai {
    151  1.1  tsubai 	int s;
    152  1.1  tsubai 
    153  1.1  tsubai 	s = spladb();
    154  1.1  tsubai 
    155  1.1  tsubai #ifdef DIAGNOSTIC
    156  1.1  tsubai 	if (adb_evq_tail < 0 || adb_evq_tail >= ADB_MAX_EVENTS)
    157  1.1  tsubai 		panic("adb: event queue tail is out of bounds");
    158  1.1  tsubai 
    159  1.1  tsubai 	if (adb_evq_len < 0 || adb_evq_len > ADB_MAX_EVENTS)
    160  1.1  tsubai 		panic("adb: event queue len is out of bounds");
    161  1.1  tsubai #endif
    162  1.1  tsubai 
    163  1.1  tsubai 	if (adb_evq_len == ADB_MAX_EVENTS) {
    164  1.1  tsubai 		splx(s);
    165  1.1  tsubai 		return;		/* Oh, well... */
    166  1.1  tsubai 	}
    167  1.1  tsubai 	adb_evq[(adb_evq_len + adb_evq_tail) % ADB_MAX_EVENTS] =
    168  1.1  tsubai 	    *event;
    169  1.1  tsubai 	adb_evq_len++;
    170  1.1  tsubai 
    171  1.1  tsubai 	selwakeup(&adb_selinfo);
    172  1.1  tsubai 	if (adb_ioproc)
    173  1.1  tsubai 		psignal(adb_ioproc, SIGIO);
    174  1.1  tsubai 
    175  1.1  tsubai 	splx(s);
    176  1.1  tsubai }
    177  1.1  tsubai 
    178  1.1  tsubai void
    179  1.1  tsubai adb_handoff(event)
    180  1.1  tsubai 	adb_event_t *event;
    181  1.1  tsubai {
    182  1.1  tsubai 	if (adb_isopen && !adb_polling) {
    183  1.1  tsubai 		adb_enqevent(event);
    184  1.1  tsubai 	} else {
    185  1.1  tsubai 		if (event->def_addr == 2)
    186  1.1  tsubai 			ite_intr(event);
    187  1.1  tsubai 	}
    188  1.1  tsubai }
    189  1.1  tsubai 
    190  1.1  tsubai 
    191  1.1  tsubai void
    192  1.1  tsubai adb_autorepeat(keyp)
    193  1.1  tsubai 	void *keyp;
    194  1.1  tsubai {
    195  1.1  tsubai 	int key = (int)keyp;
    196  1.1  tsubai 
    197  1.1  tsubai 	adb_rptevent.bytes[0] |= 0x80;
    198  1.1  tsubai 	microtime(&adb_rptevent.timestamp);
    199  1.1  tsubai 	adb_handoff(&adb_rptevent);	/* do key up */
    200  1.1  tsubai 
    201  1.1  tsubai 	adb_rptevent.bytes[0] &= 0x7f;
    202  1.1  tsubai 	microtime(&adb_rptevent.timestamp);
    203  1.1  tsubai 	adb_handoff(&adb_rptevent);	/* do key down */
    204  1.1  tsubai 
    205  1.1  tsubai 	if (adb_repeating == key) {
    206  1.1  tsubai 		timeout(adb_autorepeat, keyp, adb_rptinterval);
    207  1.1  tsubai 	}
    208  1.1  tsubai }
    209  1.1  tsubai 
    210  1.1  tsubai 
    211  1.1  tsubai void
    212  1.1  tsubai adb_dokeyupdown(event)
    213  1.1  tsubai 	adb_event_t *event;
    214  1.1  tsubai {
    215  1.1  tsubai 	int adb_key;
    216  1.1  tsubai 
    217  1.1  tsubai 	if (event->def_addr == 2) {
    218  1.1  tsubai 		adb_key = event->u.k.key & 0x7f;
    219  1.1  tsubai 		if (!(event->u.k.key & 0x80) &&
    220  1.1  tsubai 		    keyboard[event->u.k.key & 0x7f][0] != 0) {
    221  1.1  tsubai 			/* ignore shift & control */
    222  1.1  tsubai 			if (adb_repeating != -1) {
    223  1.1  tsubai 				untimeout(adb_autorepeat,
    224  1.1  tsubai 				    (void *)adb_rptevent.u.k.key);
    225  1.1  tsubai 			}
    226  1.1  tsubai 			adb_rptevent = *event;
    227  1.1  tsubai 			adb_repeating = adb_key;
    228  1.1  tsubai 			timeout(adb_autorepeat,
    229  1.1  tsubai 			    (void *)adb_key, adb_rptdelay);
    230  1.1  tsubai 		} else {
    231  1.1  tsubai 			if (adb_repeating != -1) {
    232  1.1  tsubai 				adb_repeating = -1;
    233  1.1  tsubai 				untimeout(adb_autorepeat,
    234  1.1  tsubai 				    (void *)adb_rptevent.u.k.key);
    235  1.1  tsubai 			}
    236  1.1  tsubai 			adb_rptevent = *event;
    237  1.1  tsubai 		}
    238  1.1  tsubai 	}
    239  1.1  tsubai 	adb_handoff(event);
    240  1.1  tsubai }
    241  1.1  tsubai 
    242  1.1  tsubai void
    243  1.1  tsubai adb_keymaybemouse(event)
    244  1.1  tsubai 	adb_event_t *event;
    245  1.1  tsubai {
    246  1.1  tsubai 	static int optionkey_down = 0;
    247  1.1  tsubai 	adb_event_t new_event;
    248  1.1  tsubai 
    249  1.1  tsubai 	if (event->u.k.key == ADBK_KEYDOWN(ADBK_OPTION)) {
    250  1.1  tsubai 		optionkey_down = 1;
    251  1.1  tsubai 	} else if (event->u.k.key == ADBK_KEYUP(ADBK_OPTION)) {
    252  1.1  tsubai 		/* key up */
    253  1.1  tsubai 		optionkey_down = 0;
    254  1.1  tsubai 		if (adb_ms_buttons & 0xfe) {
    255  1.1  tsubai 			adb_ms_buttons &= 1;
    256  1.1  tsubai 			new_event.def_addr = ADBADDR_MS;
    257  1.1  tsubai 			new_event.u.m.buttons = adb_ms_buttons;
    258  1.1  tsubai 			new_event.u.m.dx = new_event.u.m.dy = 0;
    259  1.1  tsubai 			microtime(&new_event.timestamp);
    260  1.1  tsubai 			adb_dokeyupdown(&new_event);
    261  1.1  tsubai 		}
    262  1.1  tsubai 	} else if (optionkey_down) {
    263  1.1  tsubai #ifdef ALTXBUTTONS
    264  1.1  tsubai 		if (event->u.k.key == ADBK_KEYDOWN(ADBK_1)) {
    265  1.1  tsubai 			adb_ms_buttons |= 1;	/* left down */
    266  1.1  tsubai 			new_event.def_addr = ADBADDR_MS;
    267  1.1  tsubai 			new_event.u.m.buttons = adb_ms_buttons;
    268  1.1  tsubai 			new_event.u.m.dx = new_event.u.m.dy = 0;
    269  1.1  tsubai 			microtime(&new_event.timestamp);
    270  1.1  tsubai 			adb_dokeyupdown(&new_event);
    271  1.1  tsubai 		} else if (event->u.k.key == ADBK_KEYUP(ADBK_1)) {
    272  1.1  tsubai 			adb_ms_buttons &= ~1;	/* left up */
    273  1.1  tsubai 			new_event.def_addr = ADBADDR_MS;
    274  1.1  tsubai 			new_event.u.m.buttons = adb_ms_buttons;
    275  1.1  tsubai 			new_event.u.m.dx = new_event.u.m.dy = 0;
    276  1.1  tsubai 			microtime(&new_event.timestamp);
    277  1.1  tsubai 			adb_dokeyupdown(&new_event);
    278  1.1  tsubai 		} else
    279  1.1  tsubai #endif
    280  1.1  tsubai 		if (event->u.k.key == ADBK_KEYDOWN(ADBK_LEFT)
    281  1.1  tsubai #ifdef ALTXBUTTONS
    282  1.1  tsubai 		    || event->u.k.key == ADBK_KEYDOWN(ADBK_2)
    283  1.1  tsubai #endif
    284  1.1  tsubai 		    ) {
    285  1.1  tsubai 			adb_ms_buttons |= 2;	/* middle down */
    286  1.1  tsubai 			new_event.def_addr = ADBADDR_MS;
    287  1.1  tsubai 			new_event.u.m.buttons = adb_ms_buttons;
    288  1.1  tsubai 			new_event.u.m.dx = new_event.u.m.dy = 0;
    289  1.1  tsubai 			microtime(&new_event.timestamp);
    290  1.1  tsubai 			adb_dokeyupdown(&new_event);
    291  1.1  tsubai 		} else if (event->u.k.key == ADBK_KEYUP(ADBK_LEFT)
    292  1.1  tsubai #ifdef ALTXBUTTONS
    293  1.1  tsubai 		    || event->u.k.key == ADBK_KEYUP(ADBK_2)
    294  1.1  tsubai #endif
    295  1.1  tsubai 		    ) {
    296  1.1  tsubai 			adb_ms_buttons &= ~2;	/* middle up */
    297  1.1  tsubai 			new_event.def_addr = ADBADDR_MS;
    298  1.1  tsubai 			new_event.u.m.buttons = adb_ms_buttons;
    299  1.1  tsubai 			new_event.u.m.dx = new_event.u.m.dy = 0;
    300  1.1  tsubai 			microtime(&new_event.timestamp);
    301  1.1  tsubai 			adb_dokeyupdown(&new_event);
    302  1.1  tsubai 		} else if (event->u.k.key == ADBK_KEYDOWN(ADBK_RIGHT)
    303  1.1  tsubai #ifdef ALTXBUTTONS
    304  1.1  tsubai 		    || event->u.k.key == ADBK_KEYDOWN(ADBK_3)
    305  1.1  tsubai #endif
    306  1.1  tsubai 		    ) {
    307  1.1  tsubai 			adb_ms_buttons |= 4;	/* right down */
    308  1.1  tsubai 			new_event.def_addr = ADBADDR_MS;
    309  1.1  tsubai 			new_event.u.m.buttons = adb_ms_buttons;
    310  1.1  tsubai 			new_event.u.m.dx = new_event.u.m.dy = 0;
    311  1.1  tsubai 			microtime(&new_event.timestamp);
    312  1.1  tsubai 			adb_dokeyupdown(&new_event);
    313  1.1  tsubai 		} else if (event->u.k.key == ADBK_KEYUP(ADBK_RIGHT)
    314  1.1  tsubai #ifdef ALTXBUTTONS
    315  1.1  tsubai 		    || event->u.k.key == ADBK_KEYUP(ADBK_3)
    316  1.1  tsubai #endif
    317  1.1  tsubai 		    ) {
    318  1.1  tsubai 			adb_ms_buttons &= ~4;	/* right up */
    319  1.1  tsubai 			new_event.def_addr = ADBADDR_MS;
    320  1.1  tsubai 			new_event.u.m.buttons = adb_ms_buttons;
    321  1.1  tsubai 			new_event.u.m.dx = new_event.u.m.dy = 0;
    322  1.1  tsubai 			microtime(&new_event.timestamp);
    323  1.1  tsubai 			adb_dokeyupdown(&new_event);
    324  1.1  tsubai 		} else if (ADBK_MODIFIER(event->u.k.key)) {
    325  1.1  tsubai 		/* ctrl, shift, cmd */
    326  1.1  tsubai 			adb_dokeyupdown(event);
    327  1.1  tsubai 		} else if (!(event->u.k.key & 0x80)) {
    328  1.1  tsubai 		/* key down */
    329  1.1  tsubai 			new_event = *event;
    330  1.1  tsubai 
    331  1.1  tsubai 			/* send option-down */
    332  1.1  tsubai 			new_event.u.k.key = ADBK_KEYDOWN(ADBK_OPTION);
    333  1.1  tsubai 			new_event.bytes[0] = new_event.u.k.key;
    334  1.1  tsubai 			microtime(&new_event.timestamp);
    335  1.1  tsubai 			adb_dokeyupdown(&new_event);
    336  1.1  tsubai 
    337  1.1  tsubai 			/* send key-down */
    338  1.1  tsubai 			new_event.u.k.key = event->bytes[0];
    339  1.1  tsubai 			new_event.bytes[0] = new_event.u.k.key;
    340  1.1  tsubai 			microtime(&new_event.timestamp);
    341  1.1  tsubai 			adb_dokeyupdown(&new_event);
    342  1.1  tsubai 
    343  1.1  tsubai 			/* send key-up */
    344  1.1  tsubai 			new_event.u.k.key =
    345  1.1  tsubai 				ADBK_KEYUP(ADBK_KEYVAL(event->bytes[0]));
    346  1.1  tsubai 			microtime(&new_event.timestamp);
    347  1.1  tsubai 			new_event.bytes[0] = new_event.u.k.key;
    348  1.1  tsubai 			adb_dokeyupdown(&new_event);
    349  1.1  tsubai 
    350  1.1  tsubai 			/* send option-up */
    351  1.1  tsubai 			new_event.u.k.key = ADBK_KEYUP(ADBK_OPTION);
    352  1.1  tsubai 			new_event.bytes[0] = new_event.u.k.key;
    353  1.1  tsubai 			microtime(&new_event.timestamp);
    354  1.1  tsubai 			adb_dokeyupdown(&new_event);
    355  1.1  tsubai 		} else {
    356  1.1  tsubai 			/* option-keyup -- do nothing. */
    357  1.1  tsubai 		}
    358  1.1  tsubai 	} else {
    359  1.1  tsubai 		adb_dokeyupdown(event);
    360  1.1  tsubai 	}
    361  1.1  tsubai }
    362  1.1  tsubai 
    363  1.1  tsubai 
    364  1.1  tsubai void
    365  1.1  tsubai adb_processevent(event)
    366  1.1  tsubai 	adb_event_t *event;
    367  1.1  tsubai {
    368  1.1  tsubai 	adb_event_t new_event;
    369  1.1  tsubai 	int i, button_bit, max_byte, mask, buttons;
    370  1.1  tsubai 
    371  1.1  tsubai 	new_event = *event;
    372  1.1  tsubai 	buttons = 0;
    373  1.1  tsubai 
    374  1.1  tsubai 	switch (event->def_addr) {
    375  1.1  tsubai 	case ADBADDR_KBD:
    376  1.1  tsubai 		new_event.u.k.key = event->bytes[0];
    377  1.1  tsubai 		new_event.bytes[1] = 0xff;
    378  1.1  tsubai 		adb_keymaybemouse(&new_event);
    379  1.1  tsubai 		if (event->bytes[1] != 0xff) {
    380  1.1  tsubai 			new_event.u.k.key = event->bytes[1];
    381  1.1  tsubai 			new_event.bytes[0] = event->bytes[1];
    382  1.1  tsubai 			new_event.bytes[1] = 0xff;
    383  1.1  tsubai 			adb_keymaybemouse(&new_event);
    384  1.1  tsubai 		}
    385  1.1  tsubai 		break;
    386  1.1  tsubai 	case ADBADDR_MS:
    387  1.1  tsubai 		/*
    388  1.1  tsubai 		 * This should handle both plain ol' Apple mice and mice
    389  1.1  tsubai 		 * that claim to support the Extended Apple Mouse Protocol.
    390  1.1  tsubai 		 */
    391  1.1  tsubai 		max_byte = event->byte_count;
    392  1.1  tsubai 		button_bit = 1;
    393  1.1  tsubai 		switch (event->hand_id) {
    394  1.1  tsubai 		case ADBMS_USPEED:
    395  1.1  tsubai 			/* MicroSpeed mouse */
    396  1.1  tsubai 			if (max_byte == 4)
    397  1.1  tsubai 				buttons = (~event->bytes[2]) & 0xff;
    398  1.1  tsubai 			else
    399  1.1  tsubai 				buttons = (event->bytes[0] & 0x80) ? 0 : 1;
    400  1.1  tsubai 			break;
    401  1.1  tsubai 		case ADBMS_MSA3:
    402  1.1  tsubai 			/* Mouse Systems A3 mouse */
    403  1.1  tsubai 			if (max_byte == 3)
    404  1.1  tsubai 				buttons = (~event->bytes[2]) & 0x07;
    405  1.1  tsubai 			else
    406  1.1  tsubai 				buttons = (event->bytes[0] & 0x80) ? 0 : 1;
    407  1.1  tsubai 			break;
    408  1.1  tsubai 		default:
    409  1.1  tsubai 			/* Classic Mouse Protocol (up to 2 buttons) */
    410  1.1  tsubai 			for (i = 0; i < 2; i++, button_bit <<= 1)
    411  1.1  tsubai 				/* 0 when button down */
    412  1.1  tsubai 				if (!(event->bytes[i] & 0x80))
    413  1.1  tsubai 					buttons |= button_bit;
    414  1.1  tsubai 				else
    415  1.1  tsubai 					buttons &= ~button_bit;
    416  1.1  tsubai 			/* Extended Protocol (up to 6 more buttons) */
    417  1.1  tsubai 			for (mask = 0x80; i < max_byte;
    418  1.1  tsubai 			     i += (mask == 0x80), button_bit <<= 1) {
    419  1.1  tsubai 				/* 0 when button down */
    420  1.1  tsubai 				if (!(event->bytes[i] & mask))
    421  1.1  tsubai 					buttons |= button_bit;
    422  1.1  tsubai 				else
    423  1.1  tsubai 					buttons &= ~button_bit;
    424  1.1  tsubai 				mask = ((mask >> 4) & 0xf)
    425  1.1  tsubai 					| ((mask & 0xf) << 4);
    426  1.1  tsubai 			}
    427  1.1  tsubai 			break;
    428  1.1  tsubai 		}
    429  1.1  tsubai 		new_event.u.m.buttons = adb_ms_buttons | buttons;
    430  1.1  tsubai 		new_event.u.m.dx = ((signed int) (event->bytes[1] & 0x3f)) -
    431  1.1  tsubai 					((event->bytes[1] & 0x40) ? 64 : 0);
    432  1.1  tsubai 		new_event.u.m.dy = ((signed int) (event->bytes[0] & 0x3f)) -
    433  1.1  tsubai 					((event->bytes[0] & 0x40) ? 64 : 0);
    434  1.1  tsubai 		adb_dokeyupdown(&new_event);
    435  1.1  tsubai 		break;
    436  1.1  tsubai 	default:		/* God only knows. */
    437  1.1  tsubai 		adb_dokeyupdown(event);
    438  1.1  tsubai 	}
    439  1.1  tsubai }
    440  1.1  tsubai 
    441  1.1  tsubai 
    442  1.1  tsubai int
    443  1.1  tsubai adbopen(dev, flag, mode, p)
    444  1.1  tsubai 	dev_t dev;
    445  1.1  tsubai 	int flag, mode;
    446  1.1  tsubai 	struct proc *p;
    447  1.1  tsubai {
    448  1.1  tsubai 	register int unit;
    449  1.1  tsubai 	int error = 0;
    450  1.1  tsubai 	int s;
    451  1.1  tsubai 
    452  1.1  tsubai 	unit = minor(dev);
    453  1.1  tsubai 	if (unit != 0 || !adb_initted)
    454  1.1  tsubai 		return (ENXIO);
    455  1.1  tsubai 
    456  1.1  tsubai 	s = spladb();
    457  1.1  tsubai 	if (adb_isopen) {
    458  1.1  tsubai 		splx(s);
    459  1.1  tsubai 		return (EBUSY);
    460  1.1  tsubai 	}
    461  1.1  tsubai 	adb_evq_tail = 0;
    462  1.1  tsubai 	adb_evq_len = 0;
    463  1.1  tsubai 	adb_isopen = 1;
    464  1.1  tsubai 	adb_ioproc = p;
    465  1.1  tsubai 	splx(s);
    466  1.1  tsubai 
    467  1.1  tsubai 	return (error);
    468  1.1  tsubai }
    469  1.1  tsubai 
    470  1.1  tsubai 
    471  1.1  tsubai int
    472  1.1  tsubai adbclose(dev, flag, mode, p)
    473  1.1  tsubai 	dev_t dev;
    474  1.1  tsubai 	int flag, mode;
    475  1.1  tsubai 	struct proc *p;
    476  1.1  tsubai {
    477  1.1  tsubai 	int s = spladb();
    478  1.1  tsubai 
    479  1.1  tsubai 	adb_isopen = 0;
    480  1.1  tsubai 	adb_ioproc = NULL;
    481  1.1  tsubai 	splx(s);
    482  1.1  tsubai 
    483  1.1  tsubai 	return (0);
    484  1.1  tsubai }
    485  1.1  tsubai 
    486  1.1  tsubai 
    487  1.1  tsubai int
    488  1.1  tsubai adbread(dev, uio, flag)
    489  1.1  tsubai 	dev_t dev;
    490  1.1  tsubai 	struct uio *uio;
    491  1.1  tsubai 	int flag;
    492  1.1  tsubai {
    493  1.1  tsubai 	int s, error;
    494  1.1  tsubai 	int willfit;
    495  1.1  tsubai 	int total;
    496  1.1  tsubai 	int firstmove;
    497  1.1  tsubai 	int moremove;
    498  1.1  tsubai 
    499  1.1  tsubai 	if (uio->uio_resid < sizeof(adb_event_t))
    500  1.1  tsubai 		return (EMSGSIZE);	/* close enough. */
    501  1.1  tsubai 
    502  1.1  tsubai 	s = spladb();
    503  1.1  tsubai 	if (adb_evq_len == 0) {
    504  1.1  tsubai 		splx(s);
    505  1.1  tsubai 		return (0);
    506  1.1  tsubai 	}
    507  1.1  tsubai 	willfit = howmany(uio->uio_resid, sizeof(adb_event_t));
    508  1.1  tsubai 	total = (adb_evq_len < willfit) ? adb_evq_len : willfit;
    509  1.1  tsubai 
    510  1.1  tsubai 	firstmove = (adb_evq_tail + total > ADB_MAX_EVENTS)
    511  1.1  tsubai 	    ? (ADB_MAX_EVENTS - adb_evq_tail) : total;
    512  1.1  tsubai 
    513  1.1  tsubai 	error = uiomove((caddr_t) & adb_evq[adb_evq_tail],
    514  1.1  tsubai 	    firstmove * sizeof(adb_event_t), uio);
    515  1.1  tsubai 	if (error) {
    516  1.1  tsubai 		splx(s);
    517  1.1  tsubai 		return (error);
    518  1.1  tsubai 	}
    519  1.1  tsubai 	moremove = total - firstmove;
    520  1.1  tsubai 
    521  1.1  tsubai 	if (moremove > 0) {
    522  1.1  tsubai 		error = uiomove((caddr_t) & adb_evq[0],
    523  1.1  tsubai 		    moremove * sizeof(adb_event_t), uio);
    524  1.1  tsubai 		if (error) {
    525  1.1  tsubai 			splx(s);
    526  1.1  tsubai 			return (error);
    527  1.1  tsubai 		}
    528  1.1  tsubai 	}
    529  1.1  tsubai 	adb_evq_tail = (adb_evq_tail + total) % ADB_MAX_EVENTS;
    530  1.1  tsubai 	adb_evq_len -= total;
    531  1.1  tsubai 	splx(s);
    532  1.1  tsubai 	return (0);
    533  1.1  tsubai }
    534  1.1  tsubai 
    535  1.1  tsubai 
    536  1.1  tsubai int
    537  1.1  tsubai adbwrite(dev, uio, flag)
    538  1.1  tsubai 	dev_t dev;
    539  1.1  tsubai 	struct uio *uio;
    540  1.1  tsubai 	int flag;
    541  1.1  tsubai {
    542  1.1  tsubai 	return 0;
    543  1.1  tsubai }
    544  1.1  tsubai 
    545  1.1  tsubai 
    546  1.1  tsubai int
    547  1.1  tsubai adbioctl(dev, cmd, data, flag, p)
    548  1.1  tsubai 	dev_t dev;
    549  1.1  tsubai 	int cmd;
    550  1.1  tsubai 	caddr_t data;
    551  1.1  tsubai 	int flag;
    552  1.1  tsubai 	struct proc *p;
    553  1.1  tsubai {
    554  1.1  tsubai 	switch (cmd) {
    555  1.1  tsubai 	case ADBIOCDEVSINFO: {
    556  1.1  tsubai 		adb_devinfo_t *di;
    557  1.1  tsubai 		ADBDataBlock adbdata;
    558  1.1  tsubai 		int totaldevs;
    559  1.1  tsubai 		int adbaddr;
    560  1.1  tsubai 		int i;
    561  1.1  tsubai 
    562  1.1  tsubai 		di = (void *)data;
    563  1.1  tsubai 
    564  1.1  tsubai 		/* Initialize to no devices */
    565  1.1  tsubai 		for (i = 0; i < 16; i++)
    566  1.1  tsubai 			di->dev[i].addr = -1;
    567  1.1  tsubai 
    568  1.1  tsubai 		totaldevs = CountADBs();
    569  1.1  tsubai 		for (i = 1; i <= totaldevs; i++) {
    570  1.1  tsubai 			adbaddr = GetIndADB(&adbdata, i);
    571  1.1  tsubai 			di->dev[adbaddr].addr = adbaddr;
    572  1.1  tsubai 			di->dev[adbaddr].default_addr = adbdata.origADBAddr;
    573  1.1  tsubai 			di->dev[adbaddr].handler_id = adbdata.devType;
    574  1.1  tsubai 			}
    575  1.1  tsubai 
    576  1.1  tsubai 		/* Must call ADB Manager to get devices now */
    577  1.1  tsubai 		break;
    578  1.1  tsubai 	}
    579  1.1  tsubai 
    580  1.1  tsubai 	case ADBIOCGETREPEAT:{
    581  1.1  tsubai 		adb_rptinfo_t *ri;
    582  1.1  tsubai 
    583  1.1  tsubai 		ri = (void *)data;
    584  1.1  tsubai 		ri->delay_ticks = adb_rptdelay;
    585  1.1  tsubai 		ri->interval_ticks = adb_rptinterval;
    586  1.1  tsubai 		break;
    587  1.1  tsubai 	}
    588  1.1  tsubai 
    589  1.1  tsubai 	case ADBIOCSETREPEAT:{
    590  1.1  tsubai 		adb_rptinfo_t *ri;
    591  1.1  tsubai 
    592  1.1  tsubai 		ri = (void *)data;
    593  1.1  tsubai 		adb_rptdelay = ri->delay_ticks;
    594  1.1  tsubai 		adb_rptinterval = ri->interval_ticks;
    595  1.1  tsubai 		break;
    596  1.1  tsubai 	}
    597  1.1  tsubai 
    598  1.1  tsubai 	case ADBIOCRESET:
    599  1.1  tsubai 		adb_init();
    600  1.1  tsubai 		break;
    601  1.1  tsubai 
    602  1.1  tsubai 	case ADBIOCLISTENCMD:{
    603  1.1  tsubai 		adb_listencmd_t *lc;
    604  1.1  tsubai 
    605  1.1  tsubai 		lc = (void *)data;
    606  1.1  tsubai 	}
    607  1.1  tsubai 
    608  1.1  tsubai 	default:
    609  1.1  tsubai 		return (EINVAL);
    610  1.1  tsubai 	}
    611  1.1  tsubai 	return (0);
    612  1.1  tsubai }
    613  1.1  tsubai 
    614  1.1  tsubai 
    615  1.1  tsubai int
    616  1.1  tsubai adbpoll(dev, events, p)
    617  1.1  tsubai 	dev_t dev;
    618  1.1  tsubai 	int events;
    619  1.1  tsubai 	struct proc *p;
    620  1.1  tsubai {
    621  1.1  tsubai 	int s, revents;
    622  1.1  tsubai 
    623  1.1  tsubai 	revents = events & (POLLOUT | POLLWRNORM);
    624  1.1  tsubai 
    625  1.1  tsubai 	if ((events & (POLLIN | POLLRDNORM)) == 0)
    626  1.1  tsubai 		return (revents);
    627  1.1  tsubai 
    628  1.1  tsubai 	s = spladb();
    629  1.1  tsubai 	if (adb_evq_len > 0)
    630  1.1  tsubai 		revents |= events & (POLLIN | POLLRDNORM);
    631  1.1  tsubai 	else
    632  1.1  tsubai 		selrecord(p, &adb_selinfo);
    633  1.1  tsubai 	splx(s);
    634  1.1  tsubai 
    635  1.1  tsubai 	return (revents);
    636  1.1  tsubai }
    637