Home | History | Annotate | Line # | Download | only in usb
ukbd.c revision 1.14
      1  1.14  augustss /*      $NetBSD: ukbd.c,v 1.14 1998/12/09 00:18:11 augustss Exp $        */
      2   1.1  augustss 
      3   1.1  augustss /*
      4   1.1  augustss  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5   1.1  augustss  * All rights reserved.
      6   1.1  augustss  *
      7  1.11  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8  1.11  augustss  * by Lennart Augustsson (augustss (at) carlstedt.se) at
      9  1.11  augustss  * Carlstedt Research & Technology.
     10   1.1  augustss  *
     11   1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12   1.1  augustss  * modification, are permitted provided that the following conditions
     13   1.1  augustss  * are met:
     14   1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15   1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16   1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18   1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19   1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     20   1.1  augustss  *    must display the following acknowledgement:
     21   1.1  augustss  *        This product includes software developed by the NetBSD
     22   1.1  augustss  *        Foundation, Inc. and its contributors.
     23   1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24   1.1  augustss  *    contributors may be used to endorse or promote products derived
     25   1.1  augustss  *    from this software without specific prior written permission.
     26   1.1  augustss  *
     27   1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28   1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29   1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30   1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31   1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32   1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33   1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34   1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35   1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36   1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37   1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     38   1.1  augustss  */
     39   1.1  augustss 
     40   1.1  augustss 
     41   1.1  augustss #include <sys/param.h>
     42   1.1  augustss #include <sys/systm.h>
     43   1.1  augustss #include <sys/kernel.h>
     44   1.1  augustss #include <sys/malloc.h>
     45   1.1  augustss #include <sys/device.h>
     46   1.1  augustss #include <sys/ioctl.h>
     47   1.1  augustss #include <sys/tty.h>
     48   1.1  augustss #include <sys/file.h>
     49   1.1  augustss #include <sys/select.h>
     50   1.1  augustss #include <sys/proc.h>
     51   1.1  augustss #include <sys/vnode.h>
     52   1.1  augustss #include <sys/device.h>
     53   1.1  augustss #include <sys/poll.h>
     54   1.1  augustss 
     55   1.1  augustss #include <dev/usb/usb.h>
     56   1.2  augustss #include <dev/usb/usbhid.h>
     57   1.1  augustss #include <dev/usb/usbdi.h>
     58   1.1  augustss #include <dev/usb/usbdi_util.h>
     59   1.1  augustss #include <dev/usb/usbdevs.h>
     60   1.1  augustss #include <dev/usb/usb_quirks.h>
     61   1.1  augustss #include <dev/usb/hid.h>
     62   1.1  augustss 
     63   1.2  augustss #include <dev/wscons/wsconsio.h>
     64   1.2  augustss #include <dev/wscons/wskbdvar.h>
     65   1.2  augustss #include <dev/wscons/wsksymdef.h>
     66   1.2  augustss #include <dev/wscons/wsksymvar.h>
     67   1.2  augustss #include <dev/wscons/wskbdmap_mfii.h>
     68   1.2  augustss 
     69   1.2  augustss #include "opt_pckbd_layout.h"
     70   1.2  augustss #include "opt_wsdisplay_compat.h"
     71   1.2  augustss 
     72   1.1  augustss #ifdef USB_DEBUG
     73   1.1  augustss #define DPRINTF(x)	if (ukbddebug) printf x
     74   1.1  augustss #define DPRINTFN(n,x)	if (ukbddebug>(n)) printf x
     75   1.1  augustss int	ukbddebug = 0;
     76   1.1  augustss #else
     77   1.1  augustss #define DPRINTF(x)
     78   1.1  augustss #define DPRINTFN(n,x)
     79   1.1  augustss #endif
     80   1.1  augustss 
     81   1.1  augustss #define UPROTO_BOOT_KEYBOARD 1
     82   1.1  augustss 
     83   1.1  augustss #define NKEYCODE 6
     84   1.1  augustss 
     85   1.2  augustss #define NUM_LOCK 0x01
     86   1.2  augustss #define CAPS_LOCK 0x02
     87   1.2  augustss #define SCROLL_LOCK 0x04
     88   1.2  augustss 
     89   1.1  augustss struct ukbd_data {
     90   1.1  augustss 	u_int8_t	modifiers;
     91   1.1  augustss #define MOD_CONTROL_L	0x01
     92   1.1  augustss #define MOD_CONTROL_R	0x10
     93   1.1  augustss #define MOD_SHIFT_L	0x02
     94   1.1  augustss #define MOD_SHIFT_R	0x20
     95   1.1  augustss #define MOD_ALT_L	0x04
     96   1.1  augustss #define MOD_ALT_R	0x40
     97   1.1  augustss #define MOD_WIN_L	0x08
     98   1.1  augustss #define MOD_WIN_R	0x80
     99   1.1  augustss 	u_int8_t	reserved;
    100   1.1  augustss 	u_int8_t	keycode[NKEYCODE];
    101   1.1  augustss };
    102   1.1  augustss 
    103   1.1  augustss #define PRESS 0
    104   1.5  augustss #define RELEASE 0x100
    105   1.1  augustss 
    106   1.1  augustss #define NMOD 6
    107   1.1  augustss static struct {
    108   1.1  augustss 	int mask, key;
    109   1.1  augustss } ukbd_mods[NMOD] = {
    110   1.1  augustss 	{ MOD_CONTROL_L, 29 },
    111   1.1  augustss 	{ MOD_CONTROL_R, 58 },
    112   1.1  augustss 	{ MOD_SHIFT_L,   42 },
    113   1.1  augustss 	{ MOD_SHIFT_R,   54 },
    114   1.1  augustss 	{ MOD_ALT_L,     56 },
    115   1.5  augustss 	{ MOD_ALT_R,    184 },
    116   1.1  augustss };
    117   1.1  augustss 
    118   1.5  augustss #define NN 0			/* no translation */
    119   1.5  augustss /* Translate USB keycodes to US keyboard AT scancodes. */
    120   1.5  augustss static u_int8_t ukbd_trtab[256] = {
    121   1.5  augustss 	   0,   0,   0,   0,  30,  48,  46,  32, /* 00 - 07 */
    122   1.5  augustss 	  18,  33,  34,  35,  23,  36,  37,  38, /* 08 - 0F */
    123   1.5  augustss 	  50,  49,  24,  25,  16,  19,  31,  20, /* 10 - 17 */
    124   1.5  augustss 	  22,  47,  17,  45,  21,  44,   2,   3, /* 18 - 1F */
    125   1.5  augustss 	   4,   5,   6,   7,   8,   9,  10,  11, /* 20 - 27 */
    126   1.5  augustss 	  28,   1,  14,  15,  57,  12,  13,  26, /* 28 - 2F */
    127   1.5  augustss 	  27,  43,  NN,  39,  40,  41,  51,  52, /* 30 - 37 */
    128   1.5  augustss 	  53,  58,  59,  60,  61,  62,  63,  64, /* 38 - 3F */
    129   1.5  augustss 	  65,  66,  67,  68,  87,  88, 170,  70, /* 40 - 47 */
    130   1.5  augustss 	 127, 210, 199, 201, 211, 207, 209, 205, /* 48 - 4F */
    131   1.5  augustss 	 203, 208, 200,  69, 181,  55,  74,  78, /* 50 - 57 */
    132   1.5  augustss 	 156,  79,  80,  81,  75,  76,  77,  71, /* 58 - 5F */
    133   1.5  augustss           72,  73,  82,  83,  NN,  NN,  NN,  NN, /* 60 - 67 */
    134   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 68 - 6F */
    135   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN, 221,  NN, /* 70 - 77 */
    136   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 78 - 7F */
    137   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 80 - 87 */
    138   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 88 - 8F */
    139   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 90 - 97 */
    140   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 98 - 9F */
    141   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* A0 - A7 */
    142   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* A8 - AF */
    143   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* B0 - B7 */
    144   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* B8 - BF */
    145   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* C0 - C7 */
    146   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* C8 - CF */
    147   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* D0 - D7 */
    148   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* D8 - DF */
    149   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* E0 - E7 */
    150   1.5  augustss           NN,  NN,  NN, 219,  NN,  NN,  NN, 220, /* E8 - EF */
    151   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* F0 - F7 */
    152   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* F8 - FF */
    153   1.1  augustss };
    154   1.1  augustss 
    155   1.1  augustss #define KEY_ERROR 0x01
    156   1.1  augustss 
    157   1.1  augustss struct ukbd_softc {
    158   1.1  augustss 	struct device sc_dev;		/* base device */
    159   1.1  augustss 	usbd_interface_handle sc_iface;	/* interface */
    160   1.1  augustss 	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */
    161   1.1  augustss 	int sc_ep_addr;
    162   1.1  augustss 
    163   1.1  augustss 	struct ukbd_data sc_ndata;
    164   1.1  augustss 	struct ukbd_data sc_odata;
    165   1.1  augustss 
    166   1.9  augustss 	char sc_enabled;
    167   1.9  augustss 	char sc_disconnected;		/* device is gone */
    168   1.1  augustss 
    169   1.2  augustss 	int sc_leds;
    170   1.2  augustss 	struct device *sc_wskbddev;
    171   1.2  augustss #ifdef WSDISPLAY_COMPAT_RAWKBD
    172   1.2  augustss 	int sc_rawkbd;
    173   1.2  augustss #endif
    174   1.3  augustss 
    175   1.3  augustss 	int sc_polling;
    176   1.3  augustss 	int sc_pollchar;
    177   1.1  augustss };
    178   1.1  augustss 
    179   1.1  augustss #define	UKBDUNIT(dev)	(minor(dev))
    180   1.1  augustss #define	UKBD_CHUNK	128	/* chunk size for read */
    181   1.1  augustss #define	UKBD_BSIZE	1020	/* buffer size */
    182   1.1  augustss 
    183   1.2  augustss int	ukbd_match __P((struct device *, struct cfdata *, void *));
    184   1.2  augustss void	ukbd_attach __P((struct device *, struct device *, void *));
    185   1.1  augustss 
    186   1.3  augustss void	ukbd_cngetc __P((void *, u_int *, int *));
    187   1.3  augustss void	ukbd_cnpollc __P((void *, int));
    188   1.3  augustss 
    189   1.8  drochner const struct wskbd_consops ukbd_consops = {
    190   1.8  drochner 	ukbd_cngetc,
    191   1.8  drochner 	ukbd_cnpollc,
    192   1.8  drochner };
    193   1.8  drochner 
    194   1.2  augustss void	ukbd_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
    195   1.2  augustss void	ukbd_disco __P((void *));
    196   1.2  augustss 
    197   1.8  drochner int	ukbd_enable __P((void *, int));
    198   1.2  augustss void	ukbd_set_leds __P((void *, int));
    199   1.2  augustss int	ukbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
    200   1.1  augustss 
    201   1.8  drochner const struct wskbd_accessops ukbd_accessops = {
    202   1.8  drochner 	ukbd_enable,
    203   1.8  drochner 	ukbd_set_leds,
    204   1.8  drochner 	ukbd_ioctl,
    205   1.8  drochner };
    206   1.8  drochner 
    207   1.8  drochner const struct wskbd_mapdata ukbd_keymapdata = {
    208   1.8  drochner 	pckbd_keydesctab,
    209   1.8  drochner #ifdef PCKBD_LAYOUT
    210   1.8  drochner 	PCKBD_LAYOUT,
    211   1.8  drochner #else
    212   1.8  drochner 	KB_US,
    213   1.8  drochner #endif
    214   1.8  drochner };
    215   1.8  drochner 
    216   1.1  augustss extern struct cfdriver ukbd_cd;
    217   1.1  augustss 
    218   1.1  augustss struct cfattach ukbd_ca = {
    219   1.1  augustss 	sizeof(struct ukbd_softc), ukbd_match, ukbd_attach
    220   1.1  augustss };
    221   1.1  augustss 
    222   1.1  augustss int
    223   1.1  augustss ukbd_match(parent, match, aux)
    224   1.1  augustss 	struct device *parent;
    225   1.1  augustss 	struct cfdata *match;
    226   1.1  augustss 	void *aux;
    227   1.1  augustss {
    228   1.1  augustss 	struct usb_attach_arg *uaa = (struct usb_attach_arg *)aux;
    229   1.1  augustss 	usb_interface_descriptor_t *id;
    230   1.1  augustss 
    231   1.1  augustss 	/* Check that this is a keyboard that speaks the boot protocol. */
    232   1.1  augustss 	if (!uaa->iface)
    233   1.1  augustss 		return (UMATCH_NONE);
    234   1.1  augustss 	id = usbd_get_interface_descriptor(uaa->iface);
    235  1.14  augustss 	if (!id ||
    236  1.14  augustss 	    id->bInterfaceClass != UCLASS_HID ||
    237   1.1  augustss 	    id->bInterfaceSubClass != USUBCLASS_BOOT ||
    238   1.1  augustss 	    id->bInterfaceProtocol != UPROTO_BOOT_KEYBOARD)
    239   1.1  augustss 		return (UMATCH_NONE);
    240   1.1  augustss 	return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    241   1.1  augustss }
    242   1.1  augustss 
    243   1.1  augustss void
    244   1.1  augustss ukbd_attach(parent, self, aux)
    245   1.1  augustss 	struct device *parent;
    246   1.1  augustss 	struct device *self;
    247   1.1  augustss 	void *aux;
    248   1.1  augustss {
    249   1.1  augustss 	struct ukbd_softc *sc = (struct ukbd_softc *)self;
    250   1.1  augustss 	struct usb_attach_arg *uaa = aux;
    251   1.1  augustss 	usbd_interface_handle iface = uaa->iface;
    252   1.1  augustss 	usb_interface_descriptor_t *id;
    253   1.1  augustss 	usb_endpoint_descriptor_t *ed;
    254   1.1  augustss 	usbd_status r;
    255   1.1  augustss 	char devinfo[1024];
    256   1.2  augustss 	struct wskbddev_attach_args a;
    257   1.1  augustss 
    258   1.1  augustss 	sc->sc_disconnected = 1;
    259   1.1  augustss 	sc->sc_iface = iface;
    260   1.1  augustss 	id = usbd_get_interface_descriptor(iface);
    261   1.1  augustss 	usbd_devinfo(uaa->device, 0, devinfo);
    262  1.13  augustss 	printf("\n%s: %s, iclass %d/%d\n", sc->sc_dev.dv_xname,
    263  1.12  augustss 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    264   1.1  augustss 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    265   1.1  augustss 	if (!ed) {
    266   1.1  augustss 		printf("%s: could not read endpoint descriptor\n",
    267   1.1  augustss 		       sc->sc_dev.dv_xname);
    268   1.1  augustss 		return;
    269   1.1  augustss 	}
    270   1.1  augustss 
    271   1.1  augustss 	DPRINTFN(10,("ukbd_attach: \
    272   1.1  augustss bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
    273   1.1  augustss 	       ed->bLength, ed->bDescriptorType, ed->bEndpointAddress & UE_ADDR,
    274   1.1  augustss 	       ed->bEndpointAddress & UE_IN ? "in" : "out",
    275   1.1  augustss 	       ed->bmAttributes & UE_XFERTYPE,
    276   1.1  augustss 	       UGETW(ed->wMaxPacketSize), ed->bInterval));
    277   1.1  augustss 
    278   1.1  augustss 	if ((ed->bEndpointAddress & UE_IN) != UE_IN ||
    279   1.1  augustss 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    280   1.1  augustss 		printf("%s: unexpected endpoint\n",
    281   1.1  augustss 		       sc->sc_dev.dv_xname);
    282   1.1  augustss 		return;
    283   1.1  augustss 	}
    284   1.1  augustss 
    285   1.1  augustss 	if ((usbd_get_quirks(uaa->device)->uq_flags & UQ_NO_SET_PROTO) == 0) {
    286   1.1  augustss 		r = usbd_set_protocol(iface, 0);
    287   1.1  augustss 		DPRINTFN(5, ("ukbd_attach: protocol set\n"));
    288   1.1  augustss 		if (r != USBD_NORMAL_COMPLETION) {
    289   1.1  augustss 			printf("%s: set protocol failed\n",
    290   1.1  augustss 			       sc->sc_dev.dv_xname);
    291   1.1  augustss 			return;
    292   1.1  augustss 		}
    293   1.1  augustss 	}
    294   1.6  augustss 	/* Ignore if SETIDLE fails since it is not crucial. */
    295   1.5  augustss 	usbd_set_idle(iface, 0, 0);
    296   1.1  augustss 
    297   1.1  augustss 	sc->sc_ep_addr = ed->bEndpointAddress;
    298   1.1  augustss 	sc->sc_disconnected = 0;
    299   1.2  augustss 
    300   1.2  augustss 	a.console = 0;	/* XXX */
    301   1.8  drochner 
    302   1.8  drochner 	a.keymap = &ukbd_keymapdata;
    303   1.8  drochner 
    304   1.8  drochner 	a.accessops = &ukbd_accessops;
    305   1.2  augustss 	a.accesscookie = sc;
    306   1.8  drochner 
    307   1.2  augustss 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
    308   1.1  augustss }
    309   1.1  augustss 
    310   1.1  augustss void
    311   1.1  augustss ukbd_disco(p)
    312   1.1  augustss 	void *p;
    313   1.1  augustss {
    314   1.1  augustss 	struct ukbd_softc *sc = p;
    315   1.7  augustss 
    316   1.7  augustss 	DPRINTF(("ukbd_disco: sc=%p\n", sc));
    317   1.7  augustss 	usbd_abort_pipe(sc->sc_intrpipe);
    318   1.1  augustss 	sc->sc_disconnected = 1;
    319   1.8  drochner }
    320   1.8  drochner 
    321   1.8  drochner int
    322   1.8  drochner ukbd_enable(v, on)
    323   1.8  drochner 	void *v;
    324   1.8  drochner 	int on;
    325   1.8  drochner {
    326   1.9  augustss 	struct ukbd_softc *sc = v;
    327   1.9  augustss 	usbd_status r;
    328   1.9  augustss 
    329   1.9  augustss 	if (on) {
    330   1.9  augustss 		/* Set up interrupt pipe. */
    331   1.9  augustss 		if (sc->sc_enabled)
    332   1.9  augustss 			return EBUSY;
    333   1.9  augustss 
    334   1.9  augustss 		sc->sc_enabled = 1;
    335   1.9  augustss 		r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    336   1.9  augustss 					USBD_SHORT_XFER_OK,
    337   1.9  augustss 					&sc->sc_intrpipe, sc, &sc->sc_ndata,
    338   1.9  augustss 					sizeof(sc->sc_ndata), ukbd_intr);
    339   1.9  augustss 		if (r != USBD_NORMAL_COMPLETION)
    340   1.9  augustss 			return (EIO);
    341   1.9  augustss 		usbd_set_disco(sc->sc_intrpipe, ukbd_disco, sc);
    342   1.9  augustss 	} else {
    343   1.9  augustss 		/* Disable interrupts. */
    344   1.9  augustss 		usbd_abort_pipe(sc->sc_intrpipe);
    345   1.9  augustss 		usbd_close_pipe(sc->sc_intrpipe);
    346   1.9  augustss 
    347   1.9  augustss 		sc->sc_enabled = 0;
    348   1.9  augustss 	}
    349   1.9  augustss 
    350   1.8  drochner 	return (0);
    351   1.1  augustss }
    352   1.1  augustss 
    353   1.1  augustss void
    354   1.1  augustss ukbd_intr(reqh, addr, status)
    355   1.1  augustss 	usbd_request_handle reqh;
    356   1.1  augustss 	usbd_private_handle addr;
    357   1.1  augustss 	usbd_status status;
    358   1.1  augustss {
    359   1.1  augustss 	struct ukbd_softc *sc = addr;
    360   1.1  augustss 	struct ukbd_data *ud = &sc->sc_ndata;
    361   1.1  augustss 	int mod, omod;
    362   1.5  augustss 	int ibuf[NMOD+2*NKEYCODE];	/* chars events */
    363   1.1  augustss 	int nkeys, i, j;
    364   1.2  augustss 	int key, c;
    365   1.1  augustss #define ADDKEY(c) ibuf[nkeys++] = (c)
    366   1.1  augustss 
    367   1.1  augustss 	DPRINTFN(5, ("ukbd_intr: status=%d\n", status));
    368   1.1  augustss 	if (status == USBD_CANCELLED)
    369   1.1  augustss 		return;
    370   1.1  augustss 
    371   1.1  augustss 	if (status != USBD_NORMAL_COMPLETION) {
    372   1.1  augustss 		DPRINTF(("ukbd_intr: status=%d\n", status));
    373   1.4  augustss 		usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
    374   1.1  augustss 		return;
    375   1.1  augustss 	}
    376   1.1  augustss 
    377   1.1  augustss 	DPRINTFN(5, ("          mod=0x%02x key0=0x%02x key1=0x%02x\n",
    378   1.1  augustss 		     ud->modifiers, ud->keycode[0], ud->keycode[1]));
    379   1.1  augustss 
    380   1.1  augustss 	if (ud->keycode[0] == KEY_ERROR)
    381   1.1  augustss 		return;		/* ignore  */
    382   1.1  augustss 	nkeys = 0;
    383   1.1  augustss 	mod = ud->modifiers;
    384   1.1  augustss 	omod = sc->sc_odata.modifiers;
    385   1.1  augustss 	if (mod != omod)
    386   1.1  augustss 		for (i = 0; i < NMOD; i++)
    387   1.1  augustss 			if (( mod & ukbd_mods[i].mask) !=
    388   1.1  augustss 			    (omod & ukbd_mods[i].mask))
    389   1.1  augustss 				ADDKEY(ukbd_mods[i].key |
    390   1.1  augustss 				       (mod & ukbd_mods[i].mask
    391   1.1  augustss 					  ? PRESS : RELEASE));
    392   1.1  augustss 	if (memcmp(ud->keycode, sc->sc_odata.keycode, NKEYCODE) != 0) {
    393   1.1  augustss 		/* Check for released keys. */
    394   1.1  augustss 		for (i = 0; i < NKEYCODE; i++) {
    395   1.1  augustss 			key = sc->sc_odata.keycode[i];
    396   1.1  augustss 			if (key == 0)
    397   1.1  augustss 				continue;
    398   1.1  augustss 			for (j = 0; j < NKEYCODE; j++)
    399   1.1  augustss 				if (key == ud->keycode[j])
    400   1.1  augustss 					goto rfound;
    401   1.2  augustss 			c = ukbd_trtab[key];
    402   1.2  augustss 			if (c)
    403   1.2  augustss 				ADDKEY(c | RELEASE);
    404   1.1  augustss 		rfound:
    405   1.1  augustss 			;
    406   1.1  augustss 		}
    407   1.1  augustss 
    408   1.1  augustss 		/* Check for pressed keys. */
    409   1.1  augustss 		for (i = 0; i < NKEYCODE; i++) {
    410   1.1  augustss 			key = ud->keycode[i];
    411   1.1  augustss 			if (key == 0)
    412   1.1  augustss 				continue;
    413   1.1  augustss 			for (j = 0; j < NKEYCODE; j++)
    414   1.1  augustss 				if (key == sc->sc_odata.keycode[j])
    415   1.1  augustss 					goto pfound;
    416   1.2  augustss 			c = ukbd_trtab[key];
    417   1.5  augustss 			DPRINTFN(2,("ukbd_intr: press key=0x%02x -> 0x%02x\n",
    418   1.5  augustss 				    key, c));
    419   1.2  augustss 			if (c)
    420   1.2  augustss 				ADDKEY(c | PRESS);
    421   1.1  augustss 		pfound:
    422   1.1  augustss 			;
    423   1.1  augustss 		}
    424   1.1  augustss 	}
    425   1.1  augustss 	sc->sc_odata = *ud;
    426   1.1  augustss 
    427   1.3  augustss 	if (sc->sc_polling) {
    428   1.3  augustss 		if (nkeys > 0)
    429   1.6  augustss 			sc->sc_pollchar = ibuf[0]; /* XXX lost keys? */
    430   1.3  augustss 		return;
    431   1.3  augustss 	}
    432   1.2  augustss 	for (i = 0; i < nkeys; i++) {
    433   1.2  augustss 		c = ibuf[i];
    434   1.2  augustss 		wskbd_input(sc->sc_wskbddev,
    435   1.5  augustss 		    c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
    436   1.5  augustss 		    c & 0xff);
    437   1.1  augustss 	}
    438   1.1  augustss }
    439   1.1  augustss 
    440   1.2  augustss void
    441   1.2  augustss ukbd_set_leds(v, leds)
    442   1.2  augustss 	void *v;
    443   1.2  augustss 	int leds;
    444   1.1  augustss {
    445   1.2  augustss 	struct ukbd_softc *sc = v;
    446   1.2  augustss 	u_int8_t res;
    447   1.1  augustss 
    448   1.2  augustss 	DPRINTF(("ukbd_set_leds: sc=%p leds=%d\n", sc, leds));
    449   1.1  augustss 
    450   1.2  augustss 	sc->sc_leds = leds;
    451   1.2  augustss 	res = 0;
    452   1.2  augustss 	if (leds & WSKBD_LED_SCROLL)
    453   1.2  augustss 		res |= SCROLL_LOCK;
    454   1.2  augustss 	if (leds & WSKBD_LED_NUM)
    455   1.2  augustss 		res |= NUM_LOCK;
    456   1.2  augustss 	if (leds & WSKBD_LED_CAPS)
    457   1.2  augustss 		res |= CAPS_LOCK;
    458   1.4  augustss 	usbd_set_report_async(sc->sc_iface, UHID_OUTPUT_REPORT, 0, &res, 1);
    459   1.1  augustss }
    460   1.1  augustss 
    461   1.1  augustss int
    462   1.2  augustss ukbd_ioctl(v, cmd, data, flag, p)
    463   1.2  augustss 	void *v;
    464   1.1  augustss 	u_long cmd;
    465   1.2  augustss 	caddr_t data;
    466   1.1  augustss 	int flag;
    467   1.1  augustss 	struct proc *p;
    468   1.1  augustss {
    469   1.2  augustss 	struct ukbd_softc *sc = v;
    470   1.1  augustss 
    471   1.2  augustss 	switch (cmd) {
    472   1.2  augustss 	case WSKBDIO_GTYPE:
    473   1.2  augustss 		*(int *)data = WSKBD_TYPE_PC_XT;
    474   1.2  augustss 		return 0;
    475   1.2  augustss 	case WSKBDIO_SETLEDS:
    476   1.2  augustss 		ukbd_set_leds(v, *(int *)data);
    477   1.2  augustss 		return 0;
    478   1.2  augustss 	case WSKBDIO_GETLEDS:
    479   1.2  augustss 		*(int *)data = sc->sc_leds;
    480   1.2  augustss 		return (0);
    481   1.2  augustss #ifdef WSDISPLAY_COMPAT_RAWKBD
    482   1.2  augustss 	case WSKBDIO_SETMODE:
    483   1.2  augustss 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
    484   1.2  augustss 		return (0);
    485   1.2  augustss #endif
    486   1.2  augustss 	}
    487   1.2  augustss 	return -1;
    488   1.1  augustss }
    489   1.7  augustss 
    490   1.7  augustss /* Console interface. */
    491   1.7  augustss /* XXX does not work. */
    492   1.3  augustss void
    493   1.3  augustss ukbd_cngetc(v, type, data)
    494   1.3  augustss 	void *v;
    495   1.3  augustss 	u_int *type;
    496   1.3  augustss 	int *data;
    497   1.3  augustss {
    498   1.3  augustss 	struct ukbd_softc *sc = v;
    499   1.3  augustss 	usbd_lock_token s;
    500   1.4  augustss 	int c;
    501   1.3  augustss 
    502   1.4  augustss 	DPRINTFN(1,("ukbd_cngetc: enter\n"));
    503   1.3  augustss 	s = usbd_lock();
    504   1.3  augustss 	sc->sc_polling = 1;
    505   1.3  augustss 	sc->sc_pollchar = -1;
    506   1.3  augustss 	while(sc->sc_pollchar == -1)
    507   1.3  augustss 		usbd_dopoll(sc->sc_iface);
    508   1.3  augustss 	sc->sc_polling = 0;
    509   1.4  augustss 	c = sc->sc_pollchar;
    510   1.6  augustss 	*type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    511   1.6  augustss 	*data = c & 0xff;
    512   1.3  augustss 	usbd_unlock(s);
    513   1.4  augustss 	DPRINTFN(1,("ukbd_cngetc: return 0x%02x\n", c));
    514   1.3  augustss }
    515   1.3  augustss 
    516   1.3  augustss void
    517   1.3  augustss ukbd_cnpollc(v, on)
    518   1.3  augustss 	void *v;
    519   1.3  augustss         int on;
    520   1.3  augustss {
    521   1.6  augustss 	struct ukbd_softc *sc = v;
    522   1.6  augustss 
    523   1.4  augustss 	DPRINTFN(1,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
    524   1.6  augustss 
    525   1.6  augustss 	usbd_set_polling(sc->sc_iface, on);
    526   1.3  augustss }
    527