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