Home | History | Annotate | Line # | Download | only in dev
aed.c revision 1.26
      1  1.26      matt /*	$NetBSD: aed.c,v 1.26 2011/06/18 08:08:28 matt 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  *
     16   1.1    tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1    tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1    tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1    tsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1    tsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21   1.1    tsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22   1.1    tsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23   1.1    tsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24   1.1    tsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     25   1.1    tsubai  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26   1.1    tsubai  */
     27  1.14     lukem 
     28  1.14     lukem #include <sys/cdefs.h>
     29  1.26      matt __KERNEL_RCSID(0, "$NetBSD: aed.c,v 1.26 2011/06/18 08:08:28 matt Exp $");
     30   1.1    tsubai 
     31   1.1    tsubai #include <sys/param.h>
     32   1.1    tsubai #include <sys/device.h>
     33   1.1    tsubai #include <sys/fcntl.h>
     34   1.1    tsubai #include <sys/poll.h>
     35   1.1    tsubai #include <sys/select.h>
     36   1.1    tsubai #include <sys/proc.h>
     37   1.1    tsubai #include <sys/signalvar.h>
     38   1.1    tsubai #include <sys/systm.h>
     39   1.6   gehenna #include <sys/conf.h>
     40   1.1    tsubai 
     41   1.1    tsubai #include <machine/autoconf.h>
     42   1.1    tsubai #include <machine/cpu.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/aedvar.h>
     47   1.1    tsubai #include <macppc/dev/akbdvar.h>
     48   1.1    tsubai 
     49   1.1    tsubai #define spladb splhigh
     50   1.1    tsubai 
     51   1.1    tsubai /*
     52   1.1    tsubai  * Function declarations.
     53   1.1    tsubai  */
     54  1.26      matt static int	aedmatch(device_t, cfdata_t, void *);
     55  1.26      matt static void	aedattach(device_t, device_t, void *);
     56  1.22       dsl static void	aed_emulate_mouse(adb_event_t *event);
     57  1.22       dsl static void	aed_kbdrpt(void *kstate);
     58  1.22       dsl static void	aed_dokeyupdown(adb_event_t *event);
     59  1.22       dsl static void	aed_handoff(adb_event_t *event);
     60  1.22       dsl static void	aed_enqevent(adb_event_t *event);
     61   1.1    tsubai 
     62   1.1    tsubai /*
     63   1.1    tsubai  * Global variables.
     64   1.1    tsubai  */
     65   1.1    tsubai extern int adb_polling;			/* Are we polling?  (Debugger mode) */
     66   1.1    tsubai 
     67   1.1    tsubai /*
     68   1.1    tsubai  * Local variables.
     69   1.1    tsubai  */
     70   1.4    tsubai static struct aed_softc *aed_sc = NULL;
     71   1.3    tsubai static int aed_options = 0; /* | AED_MSEMUL; */
     72   1.1    tsubai 
     73   1.1    tsubai /* Driver definition */
     74   1.9   thorpej CFATTACH_DECL(aed, sizeof(struct aed_softc),
     75   1.9   thorpej     aedmatch, aedattach, NULL, NULL);
     76   1.1    tsubai 
     77   1.1    tsubai extern struct cfdriver aed_cd;
     78   1.1    tsubai 
     79   1.6   gehenna dev_type_open(aedopen);
     80   1.6   gehenna dev_type_close(aedclose);
     81   1.6   gehenna dev_type_read(aedread);
     82   1.6   gehenna dev_type_ioctl(aedioctl);
     83   1.6   gehenna dev_type_poll(aedpoll);
     84  1.10  jdolecek dev_type_kqfilter(aedkqfilter);
     85   1.6   gehenna 
     86   1.6   gehenna const struct cdevsw aed_cdevsw = {
     87   1.6   gehenna 	aedopen, aedclose, aedread, nullwrite, aedioctl,
     88  1.10  jdolecek 	nostop, notty, aedpoll, nommap, aedkqfilter,
     89   1.6   gehenna };
     90   1.6   gehenna 
     91   1.1    tsubai static int
     92  1.26      matt aedmatch(device_t parent, cfdata_t cf, void *aux)
     93   1.1    tsubai {
     94   1.4    tsubai 	struct adb_attach_args *aa_args = (struct adb_attach_args *)aux;
     95   1.1    tsubai 	static int aed_matched = 0;
     96   1.1    tsubai 
     97   1.1    tsubai 	/* Allow only one instance. */
     98   1.1    tsubai         if ((aa_args->origaddr == 0) && (!aed_matched)) {
     99   1.1    tsubai 		aed_matched = 1;
    100   1.1    tsubai                 return (1);
    101   1.1    tsubai         } else
    102   1.1    tsubai                 return (0);
    103   1.1    tsubai }
    104   1.1    tsubai 
    105   1.1    tsubai static void
    106  1.26      matt aedattach(device_t parent, device_t self, void *aux)
    107   1.1    tsubai {
    108   1.4    tsubai 	struct adb_attach_args *aa_args = (struct adb_attach_args *)aux;
    109  1.26      matt 	struct aed_softc *sc = device_private(self);
    110   1.1    tsubai 
    111  1.19        ad 	callout_init(&sc->sc_repeat_ch, 0);
    112  1.21     rmind 	selinit(&sc->sc_selinfo);
    113   1.5   thorpej 
    114   1.1    tsubai 	sc->origaddr = aa_args->origaddr;
    115   1.1    tsubai 	sc->adbaddr = aa_args->adbaddr;
    116   1.1    tsubai 	sc->handler_id = aa_args->handler_id;
    117   1.1    tsubai 
    118   1.1    tsubai 	sc->sc_evq_tail = 0;
    119   1.1    tsubai 	sc->sc_evq_len = 0;
    120   1.1    tsubai 
    121   1.1    tsubai 	sc->sc_rptdelay = 20;
    122   1.1    tsubai 	sc->sc_rptinterval = 6;
    123   1.1    tsubai 	sc->sc_repeating = -1;          /* not repeating */
    124   1.1    tsubai 
    125   1.1    tsubai 	/* Pull in the options flags. */
    126  1.17   thorpej 	sc->sc_options = (device_cfdata(&sc->sc_dev)->cf_flags | aed_options);
    127   1.1    tsubai 
    128   1.1    tsubai 	sc->sc_ioproc = NULL;
    129   1.1    tsubai 
    130   1.1    tsubai 	sc->sc_buttons = 0;
    131   1.1    tsubai 
    132   1.1    tsubai 	sc->sc_open = 0;
    133   1.1    tsubai 
    134   1.1    tsubai 	aed_sc = sc;
    135   1.1    tsubai 
    136   1.1    tsubai 	printf("ADB Event device\n");
    137   1.1    tsubai 
    138   1.1    tsubai 	return;
    139   1.1    tsubai }
    140   1.1    tsubai 
    141   1.1    tsubai /*
    142   1.1    tsubai  * Given a keyboard ADB event, record the keycode and call the key
    143   1.1    tsubai  * repeat handler, optionally passing the event through the mouse
    144   1.1    tsubai  * button emulation handler first.  Pass mouse events directly to
    145   1.1    tsubai  * the handoff function.
    146   1.1    tsubai  */
    147   1.1    tsubai void
    148  1.23       dsl aed_input(adb_event_t *event)
    149   1.1    tsubai {
    150   1.1    tsubai         adb_event_t new_event = *event;
    151   1.1    tsubai 
    152   1.1    tsubai 	switch (event->def_addr) {
    153   1.1    tsubai 	case ADBADDR_KBD:
    154   1.1    tsubai 		if (aed_sc->sc_options & AED_MSEMUL)
    155   1.1    tsubai 			aed_emulate_mouse(&new_event);
    156   1.1    tsubai 		else
    157   1.1    tsubai 			aed_dokeyupdown(&new_event);
    158   1.1    tsubai 		break;
    159   1.1    tsubai 	case ADBADDR_MS:
    160   1.4    tsubai 		new_event.u.m.buttons |= aed_sc->sc_buttons;
    161   1.1    tsubai 		aed_handoff(&new_event);
    162   1.1    tsubai 		break;
    163   1.1    tsubai 	default:                /* God only knows. */
    164   1.1    tsubai #ifdef DIAGNOSTIC
    165   1.7    provos 		panic("aed: received event from unsupported device!");
    166   1.1    tsubai #endif
    167   1.1    tsubai 		break;
    168   1.1    tsubai 	}
    169   1.1    tsubai 
    170   1.1    tsubai }
    171   1.1    tsubai 
    172   1.1    tsubai /*
    173   1.1    tsubai  * Handles mouse button emulation via the keyboard.  If the emulation
    174   1.1    tsubai  * modifier key is down, left and right arrows will generate 2nd and
    175   1.1    tsubai  * 3rd mouse button events while the 1, 2, and 3 keys will generate
    176   1.1    tsubai  * the corresponding mouse button event.
    177   1.1    tsubai  */
    178   1.1    tsubai static void
    179  1.23       dsl aed_emulate_mouse(adb_event_t *event)
    180   1.1    tsubai {
    181   1.1    tsubai 	static int emulmodkey_down = 0;
    182   1.1    tsubai 	adb_event_t new_event;
    183   1.1    tsubai 
    184   1.1    tsubai 	if (event->u.k.key == ADBK_KEYDOWN(ADBK_OPTION)) {
    185   1.1    tsubai 		emulmodkey_down = 1;
    186   1.1    tsubai 	} else if (event->u.k.key == ADBK_KEYUP(ADBK_OPTION)) {
    187   1.1    tsubai 		/* key up */
    188   1.1    tsubai 		emulmodkey_down = 0;
    189   1.1    tsubai 		if (aed_sc->sc_buttons & 0xfe) {
    190   1.1    tsubai 			aed_sc->sc_buttons &= 1;
    191   1.1    tsubai 			new_event.def_addr = ADBADDR_MS;
    192   1.1    tsubai 			new_event.u.m.buttons = aed_sc->sc_buttons;
    193   1.1    tsubai 			new_event.u.m.dx = new_event.u.m.dy = 0;
    194   1.1    tsubai 			microtime(&new_event.timestamp);
    195   1.1    tsubai 			aed_handoff(&new_event);
    196   1.1    tsubai 		}
    197   1.1    tsubai 	} else if (emulmodkey_down) {
    198   1.1    tsubai 		switch(event->u.k.key) {
    199   1.1    tsubai #ifdef ALTXBUTTONS
    200   1.1    tsubai 		case ADBK_KEYDOWN(ADBK_1):
    201   1.1    tsubai 			aed_sc->sc_buttons |= 1;	/* left down */
    202   1.1    tsubai 			new_event.def_addr = ADBADDR_MS;
    203   1.1    tsubai 			new_event.u.m.buttons = aed_sc->sc_buttons;
    204   1.1    tsubai 			new_event.u.m.dx = new_event.u.m.dy = 0;
    205   1.1    tsubai 			microtime(&new_event.timestamp);
    206   1.1    tsubai 			aed_handoff(&new_event);
    207   1.1    tsubai 			break;
    208   1.1    tsubai 		case ADBK_KEYUP(ADBK_1):
    209   1.4    tsubai 			aed_sc->sc_buttons &= ~1;	/* left up */
    210   1.1    tsubai 			new_event.def_addr = ADBADDR_MS;
    211   1.1    tsubai 			new_event.u.m.buttons = aed_sc->sc_buttons;
    212   1.1    tsubai 			new_event.u.m.dx = new_event.u.m.dy = 0;
    213   1.1    tsubai 			microtime(&new_event.timestamp);
    214   1.1    tsubai 			aed_handoff(&new_event);
    215   1.1    tsubai 			break;
    216   1.1    tsubai #endif
    217   1.1    tsubai 		case ADBK_KEYDOWN(ADBK_LEFT):
    218   1.1    tsubai #ifdef ALTXBUTTONS
    219   1.1    tsubai 		case ADBK_KEYDOWN(ADBK_2):
    220   1.1    tsubai #endif
    221   1.1    tsubai 			aed_sc->sc_buttons |= 2;	/* middle down */
    222   1.1    tsubai 			new_event.def_addr = ADBADDR_MS;
    223   1.1    tsubai 			new_event.u.m.buttons = aed_sc->sc_buttons;
    224   1.1    tsubai 			new_event.u.m.dx = new_event.u.m.dy = 0;
    225   1.1    tsubai 			microtime(&new_event.timestamp);
    226   1.1    tsubai 			aed_handoff(&new_event);
    227   1.1    tsubai 			break;
    228   1.1    tsubai 		case ADBK_KEYUP(ADBK_LEFT):
    229   1.1    tsubai #ifdef ALTXBUTTONS
    230   1.1    tsubai 		case ADBK_KEYUP(ADBK_2):
    231   1.1    tsubai #endif
    232   1.1    tsubai 			aed_sc->sc_buttons &= ~2;	/* middle up */
    233   1.1    tsubai 			new_event.def_addr = ADBADDR_MS;
    234   1.1    tsubai 			new_event.u.m.buttons = aed_sc->sc_buttons;
    235   1.1    tsubai 			new_event.u.m.dx = new_event.u.m.dy = 0;
    236   1.1    tsubai 			microtime(&new_event.timestamp);
    237   1.1    tsubai 			aed_handoff(&new_event);
    238   1.1    tsubai 			break;
    239   1.1    tsubai 		case ADBK_KEYDOWN(ADBK_RIGHT):
    240   1.1    tsubai #ifdef ALTXBUTTONS
    241   1.1    tsubai 		case ADBK_KEYDOWN(ADBK_3):
    242   1.1    tsubai #endif
    243   1.1    tsubai 			aed_sc->sc_buttons |= 4;	/* right down */
    244   1.1    tsubai 			new_event.def_addr = ADBADDR_MS;
    245   1.1    tsubai 			new_event.u.m.buttons = aed_sc->sc_buttons;
    246   1.1    tsubai 			new_event.u.m.dx = new_event.u.m.dy = 0;
    247   1.1    tsubai 			microtime(&new_event.timestamp);
    248   1.1    tsubai 			aed_handoff(&new_event);
    249   1.1    tsubai 			break;
    250   1.1    tsubai 		case ADBK_KEYUP(ADBK_RIGHT):
    251   1.4    tsubai #ifdef ALTXBUTTONS
    252   1.1    tsubai 		case ADBK_KEYUP(ADBK_3):
    253   1.4    tsubai #endif
    254   1.1    tsubai 			aed_sc->sc_buttons &= ~4;	/* right up */
    255   1.1    tsubai 			new_event.def_addr = ADBADDR_MS;
    256   1.1    tsubai 			new_event.u.m.buttons = aed_sc->sc_buttons;
    257   1.1    tsubai 			new_event.u.m.dx = new_event.u.m.dy = 0;
    258   1.1    tsubai 			microtime(&new_event.timestamp);
    259   1.1    tsubai 			aed_handoff(&new_event);
    260   1.1    tsubai 			break;
    261   1.4    tsubai 		case ADBK_KEYUP(ADBK_SHIFT):
    262   1.4    tsubai 		case ADBK_KEYDOWN(ADBK_SHIFT):
    263   1.4    tsubai 		case ADBK_KEYUP(ADBK_CONTROL):
    264   1.4    tsubai 		case ADBK_KEYDOWN(ADBK_CONTROL):
    265   1.4    tsubai 		case ADBK_KEYUP(ADBK_FLOWER):
    266   1.4    tsubai 		case ADBK_KEYDOWN(ADBK_FLOWER):
    267   1.1    tsubai 			/* ctrl, shift, cmd */
    268   1.1    tsubai 			aed_dokeyupdown(event);
    269   1.1    tsubai 			break;
    270   1.1    tsubai 		default:
    271   1.1    tsubai 			if (event->u.k.key & 0x80)
    272   1.1    tsubai 				/* ignore keyup */
    273   1.1    tsubai 				break;
    274   1.1    tsubai 
    275   1.1    tsubai 			/* key down */
    276   1.1    tsubai 			new_event = *event;
    277   1.1    tsubai 
    278   1.1    tsubai 			/* send option-down */
    279   1.1    tsubai 			new_event.u.k.key = ADBK_KEYDOWN(ADBK_OPTION);
    280   1.1    tsubai 			new_event.bytes[0] = new_event.u.k.key;
    281   1.1    tsubai 			microtime(&new_event.timestamp);
    282   1.1    tsubai 			aed_dokeyupdown(&new_event);
    283   1.1    tsubai 
    284   1.1    tsubai 			/* send key-down */
    285   1.1    tsubai 			new_event.u.k.key = event->bytes[0];
    286   1.1    tsubai 			new_event.bytes[0] = new_event.u.k.key;
    287   1.1    tsubai 			microtime(&new_event.timestamp);
    288   1.1    tsubai 			aed_dokeyupdown(&new_event);
    289   1.1    tsubai 
    290   1.1    tsubai 			/* send key-up */
    291   1.1    tsubai 			new_event.u.k.key =
    292   1.1    tsubai 				ADBK_KEYUP(ADBK_KEYVAL(event->bytes[0]));
    293   1.1    tsubai 			microtime(&new_event.timestamp);
    294   1.1    tsubai 			new_event.bytes[0] = new_event.u.k.key;
    295   1.1    tsubai 			aed_dokeyupdown(&new_event);
    296   1.1    tsubai 
    297   1.1    tsubai 			/* send option-up */
    298   1.1    tsubai 			new_event.u.k.key = ADBK_KEYUP(ADBK_OPTION);
    299   1.1    tsubai 			new_event.bytes[0] = new_event.u.k.key;
    300   1.1    tsubai 			microtime(&new_event.timestamp);
    301   1.1    tsubai 			aed_dokeyupdown(&new_event);
    302   1.1    tsubai 			break;
    303   1.1    tsubai 		}
    304   1.1    tsubai 	} else {
    305   1.1    tsubai 		aed_dokeyupdown(event);
    306   1.1    tsubai 	}
    307   1.1    tsubai }
    308   1.1    tsubai 
    309   1.1    tsubai /*
    310   1.1    tsubai  * Keyboard autorepeat timeout function.  Sends key up/down events
    311   1.1    tsubai  * for the repeating key and schedules the next call at sc_rptinterval
    312   1.1    tsubai  * ticks in the future.
    313   1.1    tsubai  */
    314   1.1    tsubai static void
    315  1.23       dsl aed_kbdrpt(void *kstate)
    316   1.1    tsubai {
    317  1.15   nathanw 	struct aed_softc *sc = (struct aed_softc *)kstate;
    318   1.1    tsubai 
    319  1.15   nathanw 	sc->sc_rptevent.bytes[0] |= 0x80;
    320  1.15   nathanw 	microtime(&sc->sc_rptevent.timestamp);
    321  1.15   nathanw 	aed_handoff(&sc->sc_rptevent);	/* do key up */
    322  1.15   nathanw 
    323  1.15   nathanw 	sc->sc_rptevent.bytes[0] &= 0x7f;
    324  1.15   nathanw 	microtime(&sc->sc_rptevent.timestamp);
    325  1.15   nathanw 	aed_handoff(&sc->sc_rptevent);	/* do key down */
    326   1.1    tsubai 
    327  1.15   nathanw 	if (sc->sc_repeating == sc->sc_rptevent.u.k.key) {
    328  1.15   nathanw 		callout_reset(&sc->sc_repeat_ch, sc->sc_rptinterval,
    329   1.5   thorpej 		    aed_kbdrpt, kstate);
    330   1.1    tsubai 	}
    331   1.1    tsubai }
    332   1.1    tsubai 
    333   1.1    tsubai 
    334   1.1    tsubai /*
    335   1.1    tsubai  * Cancels the currently repeating key event if there is one, schedules
    336   1.1    tsubai  * a new repeating key event if needed, and hands the event off to the
    337   1.1    tsubai  * appropriate subsystem.
    338   1.1    tsubai  */
    339   1.1    tsubai static void
    340  1.23       dsl aed_dokeyupdown(adb_event_t *event)
    341   1.1    tsubai {
    342   1.1    tsubai 	int     kbd_key;
    343   1.1    tsubai 
    344   1.1    tsubai 	kbd_key = ADBK_KEYVAL(event->u.k.key);
    345   1.1    tsubai 	if (ADBK_PRESS(event->u.k.key) && keyboard[kbd_key][0] != 0) {
    346   1.1    tsubai 		/* ignore shift & control */
    347   1.1    tsubai 		if (aed_sc->sc_repeating != -1) {
    348   1.5   thorpej 			callout_stop(&aed_sc->sc_repeat_ch);
    349   1.1    tsubai 		}
    350   1.1    tsubai 		aed_sc->sc_rptevent = *event;
    351   1.1    tsubai 		aed_sc->sc_repeating = kbd_key;
    352   1.5   thorpej 		callout_reset(&aed_sc->sc_repeat_ch, aed_sc->sc_rptdelay,
    353   1.5   thorpej 		    aed_kbdrpt, (void *)aed_sc);
    354   1.1    tsubai 	} else {
    355   1.1    tsubai 		if (aed_sc->sc_repeating != -1) {
    356   1.1    tsubai 			aed_sc->sc_repeating = -1;
    357   1.5   thorpej 			callout_stop(&aed_sc->sc_repeat_ch);
    358   1.1    tsubai 		}
    359   1.1    tsubai 		aed_sc->sc_rptevent = *event;
    360   1.1    tsubai 	}
    361   1.1    tsubai 	aed_handoff(event);
    362   1.1    tsubai }
    363   1.1    tsubai 
    364   1.1    tsubai /*
    365   1.1    tsubai  * Place the event in the event queue if a requesting device is open
    366   1.3    tsubai  * and we are not polling.
    367   1.1    tsubai  */
    368   1.1    tsubai static void
    369  1.23       dsl aed_handoff(adb_event_t *event)
    370   1.1    tsubai {
    371   1.1    tsubai 	if (aed_sc->sc_open && !adb_polling)
    372   1.1    tsubai 		aed_enqevent(event);
    373   1.1    tsubai }
    374   1.1    tsubai 
    375   1.1    tsubai /*
    376   1.1    tsubai  * Place the event in the event queue and wakeup any waiting processes.
    377   1.1    tsubai  */
    378   1.1    tsubai static void
    379  1.23       dsl aed_enqevent(adb_event_t *event)
    380   1.1    tsubai {
    381   1.1    tsubai 	int     s;
    382   1.1    tsubai 
    383   1.1    tsubai 	s = spladb();
    384   1.1    tsubai 
    385   1.1    tsubai #ifdef DIAGNOSTIC
    386   1.1    tsubai 	if (aed_sc->sc_evq_tail < 0 || aed_sc->sc_evq_tail >= AED_MAX_EVENTS)
    387   1.1    tsubai 		panic("adb: event queue tail is out of bounds");
    388   1.1    tsubai 
    389   1.1    tsubai 	if (aed_sc->sc_evq_len < 0 || aed_sc->sc_evq_len > AED_MAX_EVENTS)
    390   1.1    tsubai 		panic("adb: event queue len is out of bounds");
    391   1.1    tsubai #endif
    392   1.1    tsubai 
    393   1.1    tsubai 	if (aed_sc->sc_evq_len == AED_MAX_EVENTS) {
    394   1.1    tsubai 		splx(s);
    395   1.1    tsubai 		return;		/* Oh, well... */
    396   1.1    tsubai 	}
    397   1.1    tsubai 	aed_sc->sc_evq[(aed_sc->sc_evq_len + aed_sc->sc_evq_tail) %
    398   1.1    tsubai 	    AED_MAX_EVENTS] = *event;
    399   1.1    tsubai 	aed_sc->sc_evq_len++;
    400   1.1    tsubai 
    401  1.21     rmind 	selnotify(&aed_sc->sc_selinfo, 0, 0);
    402   1.1    tsubai 	if (aed_sc->sc_ioproc)
    403   1.1    tsubai 		psignal(aed_sc->sc_ioproc, SIGIO);
    404   1.1    tsubai 
    405   1.1    tsubai 	splx(s);
    406   1.1    tsubai }
    407   1.1    tsubai 
    408   1.1    tsubai int
    409  1.24       dsl aedopen(dev_t dev, int flag, int mode, struct lwp *l)
    410   1.1    tsubai {
    411   1.1    tsubai 	int unit;
    412   1.1    tsubai 	int error = 0;
    413   1.1    tsubai 	int s;
    414   1.1    tsubai 
    415   1.1    tsubai 	unit = minor(dev);
    416   1.1    tsubai 
    417   1.1    tsubai 	if (unit != 0)
    418   1.1    tsubai 		return (ENXIO);
    419   1.1    tsubai 
    420   1.1    tsubai 	s = spladb();
    421   1.1    tsubai 	if (aed_sc->sc_open) {
    422   1.1    tsubai 		splx(s);
    423   1.1    tsubai 		return (EBUSY);
    424   1.1    tsubai 	}
    425   1.1    tsubai 	aed_sc->sc_evq_tail = 0;
    426   1.1    tsubai 	aed_sc->sc_evq_len = 0;
    427   1.1    tsubai 	aed_sc->sc_open = 1;
    428  1.16  christos 	aed_sc->sc_ioproc = l->l_proc;
    429   1.1    tsubai 	splx(s);
    430   1.1    tsubai 
    431   1.1    tsubai 	return (error);
    432   1.1    tsubai }
    433   1.1    tsubai 
    434   1.1    tsubai 
    435   1.1    tsubai int
    436  1.24       dsl aedclose(dev_t dev, int flag, int mode, struct lwp *l)
    437   1.1    tsubai {
    438   1.1    tsubai 	int s = spladb();
    439   1.1    tsubai 
    440   1.1    tsubai 	aed_sc->sc_open = 0;
    441   1.1    tsubai 	aed_sc->sc_ioproc = NULL;
    442   1.1    tsubai 	splx(s);
    443   1.1    tsubai 
    444   1.1    tsubai 	return (0);
    445   1.1    tsubai }
    446   1.1    tsubai 
    447   1.1    tsubai 
    448   1.1    tsubai int
    449  1.23       dsl aedread(dev_t dev, struct uio *uio, int flag)
    450   1.1    tsubai {
    451   1.1    tsubai 	int s, error;
    452   1.1    tsubai 	int willfit;
    453   1.1    tsubai 	int total;
    454   1.1    tsubai 	int firstmove;
    455   1.1    tsubai 	int moremove;
    456   1.1    tsubai 
    457   1.1    tsubai 	if (uio->uio_resid < sizeof(adb_event_t))
    458   1.1    tsubai 		return (EMSGSIZE);	/* close enough. */
    459   1.1    tsubai 
    460   1.1    tsubai 	s = spladb();
    461   1.1    tsubai 	if (aed_sc->sc_evq_len == 0) {
    462   1.1    tsubai 		splx(s);
    463   1.1    tsubai 		return (0);
    464   1.1    tsubai 	}
    465   1.1    tsubai 	willfit = howmany(uio->uio_resid, sizeof(adb_event_t));
    466   1.1    tsubai 	total = (aed_sc->sc_evq_len < willfit) ? aed_sc->sc_evq_len : willfit;
    467   1.1    tsubai 
    468   1.1    tsubai 	firstmove = (aed_sc->sc_evq_tail + total > AED_MAX_EVENTS)
    469   1.1    tsubai 	    ? (AED_MAX_EVENTS - aed_sc->sc_evq_tail) : total;
    470   1.1    tsubai 
    471  1.18  christos 	error = uiomove((void *) & aed_sc->sc_evq[aed_sc->sc_evq_tail],
    472   1.1    tsubai 	    firstmove * sizeof(adb_event_t), uio);
    473   1.1    tsubai 	if (error) {
    474   1.1    tsubai 		splx(s);
    475   1.1    tsubai 		return (error);
    476   1.1    tsubai 	}
    477   1.1    tsubai 	moremove = total - firstmove;
    478   1.1    tsubai 
    479   1.1    tsubai 	if (moremove > 0) {
    480  1.18  christos 		error = uiomove((void *) & aed_sc->sc_evq[0],
    481   1.1    tsubai 		    moremove * sizeof(adb_event_t), uio);
    482   1.1    tsubai 		if (error) {
    483   1.1    tsubai 			splx(s);
    484   1.1    tsubai 			return (error);
    485   1.1    tsubai 		}
    486   1.1    tsubai 	}
    487   1.1    tsubai 	aed_sc->sc_evq_tail = (aed_sc->sc_evq_tail + total) % AED_MAX_EVENTS;
    488   1.1    tsubai 	aed_sc->sc_evq_len -= total;
    489   1.1    tsubai 	splx(s);
    490   1.1    tsubai 	return (0);
    491   1.1    tsubai }
    492   1.1    tsubai 
    493   1.1    tsubai int
    494  1.23       dsl aedioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    495   1.1    tsubai {
    496   1.1    tsubai 	switch (cmd) {
    497   1.1    tsubai 	case ADBIOCDEVSINFO: {
    498   1.1    tsubai 		adb_devinfo_t *di;
    499   1.1    tsubai 		ADBDataBlock adbdata;
    500   1.1    tsubai 		int totaldevs;
    501   1.1    tsubai 		int adbaddr;
    502   1.1    tsubai 		int i;
    503   1.1    tsubai 
    504   1.1    tsubai 		di = (void *)data;
    505   1.1    tsubai 
    506   1.1    tsubai 		/* Initialize to no devices */
    507   1.1    tsubai 		for (i = 0; i < 16; i++)
    508   1.1    tsubai 			di->dev[i].addr = -1;
    509   1.1    tsubai 
    510   1.1    tsubai 		totaldevs = CountADBs();
    511   1.1    tsubai 		for (i = 1; i <= totaldevs; i++) {
    512   1.1    tsubai 			adbaddr = GetIndADB(&adbdata, i);
    513   1.1    tsubai 			di->dev[adbaddr].addr = adbaddr;
    514   1.4    tsubai 			di->dev[adbaddr].default_addr = (int)(adbdata.origADBAddr);
    515   1.4    tsubai 			di->dev[adbaddr].handler_id = (int)(adbdata.devType);
    516   1.1    tsubai 			}
    517   1.1    tsubai 
    518   1.1    tsubai 		/* Must call ADB Manager to get devices now */
    519   1.1    tsubai 		break;
    520   1.1    tsubai 	}
    521   1.1    tsubai 
    522   1.1    tsubai 	case ADBIOCGETREPEAT:{
    523   1.1    tsubai 		adb_rptinfo_t *ri;
    524   1.1    tsubai 
    525   1.1    tsubai 		ri = (void *)data;
    526   1.1    tsubai 		ri->delay_ticks = aed_sc->sc_rptdelay;
    527   1.1    tsubai 		ri->interval_ticks = aed_sc->sc_rptinterval;
    528   1.1    tsubai 		break;
    529   1.1    tsubai 	}
    530   1.1    tsubai 
    531   1.1    tsubai 	case ADBIOCSETREPEAT:{
    532   1.1    tsubai 		adb_rptinfo_t *ri;
    533   1.1    tsubai 
    534   1.1    tsubai 		ri = (void *) data;
    535   1.1    tsubai 		aed_sc->sc_rptdelay = ri->delay_ticks;
    536   1.1    tsubai 		aed_sc->sc_rptinterval = ri->interval_ticks;
    537   1.1    tsubai 		break;
    538   1.1    tsubai 	}
    539   1.1    tsubai 
    540   1.1    tsubai 	case ADBIOCRESET:
    541   1.1    tsubai 		/* Do nothing for now */
    542   1.1    tsubai 		break;
    543   1.1    tsubai 
    544   1.1    tsubai 	case ADBIOCLISTENCMD:{
    545   1.1    tsubai 		adb_listencmd_t *lc;
    546   1.1    tsubai 
    547   1.1    tsubai 		lc = (void *)data;
    548   1.1    tsubai 	}
    549   1.1    tsubai 
    550   1.1    tsubai 	default:
    551   1.1    tsubai 		return (EINVAL);
    552   1.1    tsubai 	}
    553   1.1    tsubai 	return (0);
    554   1.1    tsubai }
    555   1.1    tsubai 
    556   1.1    tsubai 
    557   1.1    tsubai int
    558  1.23       dsl aedpoll(dev_t dev, int events, struct lwp *l)
    559   1.1    tsubai {
    560   1.1    tsubai 	int s, revents;
    561   1.1    tsubai 
    562   1.1    tsubai 	revents = events & (POLLOUT | POLLWRNORM);
    563   1.1    tsubai 
    564   1.1    tsubai 	if ((events & (POLLIN | POLLRDNORM)) == 0)
    565   1.1    tsubai 		return (revents);
    566   1.1    tsubai 
    567   1.1    tsubai 	s = spladb();
    568   1.1    tsubai 	if (aed_sc->sc_evq_len > 0)
    569   1.1    tsubai 		revents |= events & (POLLIN | POLLRDNORM);
    570   1.1    tsubai 	else
    571  1.16  christos 		selrecord(l, &aed_sc->sc_selinfo);
    572   1.1    tsubai 	splx(s);
    573   1.1    tsubai 
    574   1.1    tsubai 	return (revents);
    575  1.10  jdolecek }
    576  1.10  jdolecek 
    577  1.10  jdolecek static void
    578  1.10  jdolecek filt_aedrdetach(struct knote *kn)
    579  1.10  jdolecek {
    580  1.10  jdolecek 	int s;
    581  1.10  jdolecek 
    582  1.10  jdolecek 	s = spladb();
    583  1.11  christos 	SLIST_REMOVE(&aed_sc->sc_selinfo.sel_klist, kn, knote, kn_selnext);
    584  1.10  jdolecek 	splx(s);
    585  1.10  jdolecek }
    586  1.10  jdolecek 
    587  1.10  jdolecek static int
    588  1.10  jdolecek filt_aedread(struct knote *kn, long hint)
    589  1.10  jdolecek {
    590  1.10  jdolecek 
    591  1.10  jdolecek 	kn->kn_data = aed_sc->sc_evq_len * sizeof(adb_event_t);
    592  1.10  jdolecek 	return (kn->kn_data > 0);
    593  1.10  jdolecek }
    594  1.10  jdolecek 
    595  1.10  jdolecek static const struct filterops aedread_filtops =
    596  1.10  jdolecek 	{ 1, NULL, filt_aedrdetach, filt_aedread };
    597  1.10  jdolecek 
    598  1.10  jdolecek static const struct filterops aed_seltrue_filtops =
    599  1.10  jdolecek 	{ 1, NULL, filt_aedrdetach, filt_seltrue };
    600  1.10  jdolecek 
    601  1.10  jdolecek int
    602  1.10  jdolecek aedkqfilter(dev_t dev, struct knote *kn)
    603  1.10  jdolecek {
    604  1.10  jdolecek 	struct klist *klist;
    605  1.10  jdolecek 	int s;
    606  1.10  jdolecek 
    607  1.10  jdolecek 	switch (kn->kn_filter) {
    608  1.10  jdolecek 	case EVFILT_READ:
    609  1.11  christos 		klist = &aed_sc->sc_selinfo.sel_klist;
    610  1.10  jdolecek 		kn->kn_fop = &aedread_filtops;
    611  1.10  jdolecek 		break;
    612  1.10  jdolecek 
    613  1.10  jdolecek 	case EVFILT_WRITE:
    614  1.11  christos 		klist = &aed_sc->sc_selinfo.sel_klist;
    615  1.10  jdolecek 		kn->kn_fop = &aed_seltrue_filtops;
    616  1.10  jdolecek 		break;
    617  1.10  jdolecek 
    618  1.10  jdolecek 	default:
    619  1.10  jdolecek 		return (1);
    620  1.10  jdolecek 	}
    621  1.10  jdolecek 
    622  1.10  jdolecek 	kn->kn_hook = NULL;
    623  1.10  jdolecek 
    624  1.10  jdolecek 	s = spladb();
    625  1.10  jdolecek 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    626  1.10  jdolecek 	splx(s);
    627  1.10  jdolecek 
    628  1.10  jdolecek 	return (0);
    629   1.1    tsubai }
    630