Home | History | Annotate | Line # | Download | only in usb
ukbd.c revision 1.39
      1  1.39  augustss /*      $NetBSD: ukbd.c,v 1.39 1999/08/14 14:49:32 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.17  augustss /*
     41  1.25  augustss  * HID spec: http://www.usb.org/developers/data/usbhid10.pdf
     42  1.17  augustss  */
     43  1.17  augustss 
     44   1.1  augustss #include <sys/param.h>
     45   1.1  augustss #include <sys/systm.h>
     46   1.1  augustss #include <sys/kernel.h>
     47   1.1  augustss #include <sys/device.h>
     48   1.1  augustss #include <sys/ioctl.h>
     49   1.1  augustss #include <sys/tty.h>
     50   1.1  augustss #include <sys/file.h>
     51   1.1  augustss #include <sys/select.h>
     52   1.1  augustss #include <sys/proc.h>
     53   1.1  augustss #include <sys/vnode.h>
     54   1.1  augustss #include <sys/poll.h>
     55   1.1  augustss 
     56   1.1  augustss #include <dev/usb/usb.h>
     57   1.2  augustss #include <dev/usb/usbhid.h>
     58  1.37  augustss 
     59   1.1  augustss #include <dev/usb/usbdi.h>
     60   1.1  augustss #include <dev/usb/usbdi_util.h>
     61   1.1  augustss #include <dev/usb/usbdevs.h>
     62   1.1  augustss #include <dev/usb/usb_quirks.h>
     63   1.1  augustss #include <dev/usb/hid.h>
     64  1.31   thorpej #include <dev/usb/ukbdvar.h>
     65   1.1  augustss 
     66   1.2  augustss #include <dev/wscons/wsconsio.h>
     67   1.2  augustss #include <dev/wscons/wskbdvar.h>
     68   1.2  augustss #include <dev/wscons/wsksymdef.h>
     69   1.2  augustss #include <dev/wscons/wsksymvar.h>
     70   1.2  augustss 
     71   1.2  augustss #include "opt_wsdisplay_compat.h"
     72  1.21  augustss 
     73   1.1  augustss #ifdef USB_DEBUG
     74  1.39  augustss #define DPRINTF(x)	if (ukbddebug) logprintf x
     75  1.39  augustss #define DPRINTFN(n,x)	if (ukbddebug>(n)) logprintf x
     76   1.1  augustss int	ukbddebug = 0;
     77   1.1  augustss #else
     78   1.1  augustss #define DPRINTF(x)
     79   1.1  augustss #define DPRINTFN(n,x)
     80   1.1  augustss #endif
     81   1.1  augustss 
     82   1.1  augustss #define UPROTO_BOOT_KEYBOARD 1
     83   1.1  augustss 
     84   1.1  augustss #define NKEYCODE 6
     85   1.1  augustss 
     86   1.2  augustss #define NUM_LOCK 0x01
     87   1.2  augustss #define CAPS_LOCK 0x02
     88   1.2  augustss #define SCROLL_LOCK 0x04
     89   1.2  augustss 
     90   1.1  augustss struct ukbd_data {
     91   1.1  augustss 	u_int8_t	modifiers;
     92   1.1  augustss #define MOD_CONTROL_L	0x01
     93   1.1  augustss #define MOD_CONTROL_R	0x10
     94   1.1  augustss #define MOD_SHIFT_L	0x02
     95   1.1  augustss #define MOD_SHIFT_R	0x20
     96   1.1  augustss #define MOD_ALT_L	0x04
     97   1.1  augustss #define MOD_ALT_R	0x40
     98   1.1  augustss #define MOD_WIN_L	0x08
     99   1.1  augustss #define MOD_WIN_R	0x80
    100   1.1  augustss 	u_int8_t	reserved;
    101   1.1  augustss 	u_int8_t	keycode[NKEYCODE];
    102   1.1  augustss };
    103   1.1  augustss 
    104  1.23  augustss #define PRESS    0x000
    105  1.23  augustss #define RELEASE  0x100
    106  1.23  augustss #define CODEMASK 0x0ff
    107   1.1  augustss 
    108  1.23  augustss /* Translate USB bitmap to USB keycode. */
    109  1.27  augustss #define NMOD 8
    110   1.1  augustss static struct {
    111   1.1  augustss 	int mask, key;
    112   1.1  augustss } ukbd_mods[NMOD] = {
    113  1.23  augustss 	{ MOD_CONTROL_L, 224 },
    114  1.23  augustss 	{ MOD_CONTROL_R, 228 },
    115  1.23  augustss 	{ MOD_SHIFT_L,   225 },
    116  1.23  augustss 	{ MOD_SHIFT_R,   229 },
    117  1.23  augustss 	{ MOD_ALT_L,     226 },
    118  1.23  augustss 	{ MOD_ALT_R,     230 },
    119  1.27  augustss 	{ MOD_WIN_L,     227 },
    120  1.27  augustss 	{ MOD_WIN_R,     231 },
    121   1.1  augustss };
    122   1.1  augustss 
    123  1.23  augustss #if defined(__NetBSD__) && defined(WSDISPLAY_COMPAT_RAWKBD)
    124   1.5  augustss #define NN 0			/* no translation */
    125  1.19  augustss /*
    126  1.23  augustss  * Translate USB keycodes to US keyboard XT scancodes.
    127  1.19  augustss  * Scancodes >= 128 represent EXTENDED keycodes.
    128  1.19  augustss  */
    129   1.5  augustss static u_int8_t ukbd_trtab[256] = {
    130  1.23  augustss 	  NN,  NN,  NN,  NN,  30,  48,  46,  32, /* 00 - 07 */
    131   1.5  augustss 	  18,  33,  34,  35,  23,  36,  37,  38, /* 08 - 0F */
    132   1.5  augustss 	  50,  49,  24,  25,  16,  19,  31,  20, /* 10 - 17 */
    133   1.5  augustss 	  22,  47,  17,  45,  21,  44,   2,   3, /* 18 - 1F */
    134   1.5  augustss 	   4,   5,   6,   7,   8,   9,  10,  11, /* 20 - 27 */
    135   1.5  augustss 	  28,   1,  14,  15,  57,  12,  13,  26, /* 28 - 2F */
    136   1.5  augustss 	  27,  43,  NN,  39,  40,  41,  51,  52, /* 30 - 37 */
    137   1.5  augustss 	  53,  58,  59,  60,  61,  62,  63,  64, /* 38 - 3F */
    138   1.5  augustss 	  65,  66,  67,  68,  87,  88, 170,  70, /* 40 - 47 */
    139   1.5  augustss 	 127, 210, 199, 201, 211, 207, 209, 205, /* 48 - 4F */
    140   1.5  augustss 	 203, 208, 200,  69, 181,  55,  74,  78, /* 50 - 57 */
    141   1.5  augustss 	 156,  79,  80,  81,  75,  76,  77,  71, /* 58 - 5F */
    142  1.28  augustss           72,  73,  82,  83,  NN, 221,  NN,  NN, /* 60 - 67 */
    143   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 68 - 6F */
    144  1.27  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 70 - 77 */
    145   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 78 - 7F */
    146   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 80 - 87 */
    147   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 88 - 8F */
    148   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 90 - 97 */
    149   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 98 - 9F */
    150   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* A0 - A7 */
    151   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* A8 - AF */
    152   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* B0 - B7 */
    153   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* B8 - BF */
    154   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* C0 - C7 */
    155   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* C8 - CF */
    156   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* D0 - D7 */
    157   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* D8 - DF */
    158  1.26  augustss           29,  42,  56, 219,  157, 54,  184,220, /* E0 - E7 */
    159  1.26  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* E8 - EF */
    160   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* F0 - F7 */
    161   1.5  augustss           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* F8 - FF */
    162   1.1  augustss };
    163  1.23  augustss #endif /* defined(__NetBSD__) && defined(WSDISPLAY_COMPAT_RAWKBD) */
    164   1.1  augustss 
    165   1.1  augustss #define KEY_ERROR 0x01
    166   1.1  augustss 
    167  1.20  augustss #define MAXKEYS (NMOD+2*NKEYCODE)
    168  1.20  augustss 
    169   1.1  augustss struct ukbd_softc {
    170  1.16  augustss 	bdevice		sc_dev;		/* base device */
    171   1.1  augustss 	usbd_interface_handle sc_iface;	/* interface */
    172   1.1  augustss 	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */
    173   1.1  augustss 	int sc_ep_addr;
    174   1.1  augustss 
    175   1.1  augustss 	struct ukbd_data sc_ndata;
    176   1.1  augustss 	struct ukbd_data sc_odata;
    177   1.1  augustss 
    178   1.9  augustss 	char sc_enabled;
    179   1.1  augustss 
    180  1.29   thorpej 	int sc_console_keyboard;	/* we are the console keyboard */
    181  1.29   thorpej 
    182   1.2  augustss 	int sc_leds;
    183  1.16  augustss #if defined(__NetBSD__)
    184   1.2  augustss 	struct device *sc_wskbddev;
    185  1.23  augustss #if defined(WSDISPLAY_COMPAT_RAWKBD)
    186  1.20  augustss #define REP_DELAY1 400
    187  1.20  augustss #define REP_DELAYN 100
    188   1.2  augustss 	int sc_rawkbd;
    189  1.20  augustss 	int sc_nrep;
    190  1.20  augustss 	char sc_rep[MAXKEYS];
    191  1.23  augustss #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
    192   1.3  augustss 
    193   1.3  augustss 	int sc_polling;
    194  1.23  augustss 	int sc_npollchar;
    195  1.23  augustss 	u_int16_t sc_pollchars[MAXKEYS];
    196  1.23  augustss #endif /* defined(__NetBSD__) */
    197   1.1  augustss };
    198   1.1  augustss 
    199   1.1  augustss #define	UKBDUNIT(dev)	(minor(dev))
    200   1.1  augustss #define	UKBD_CHUNK	128	/* chunk size for read */
    201   1.1  augustss #define	UKBD_BSIZE	1020	/* buffer size */
    202   1.1  augustss 
    203  1.31   thorpej int	ukbd_is_console;
    204  1.31   thorpej 
    205   1.3  augustss void	ukbd_cngetc __P((void *, u_int *, int *));
    206   1.3  augustss void	ukbd_cnpollc __P((void *, int));
    207   1.3  augustss 
    208  1.16  augustss #if defined(__NetBSD__)
    209   1.8  drochner const struct wskbd_consops ukbd_consops = {
    210   1.8  drochner 	ukbd_cngetc,
    211   1.8  drochner 	ukbd_cnpollc,
    212   1.8  drochner };
    213  1.16  augustss #endif
    214   1.8  drochner 
    215   1.2  augustss void	ukbd_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
    216   1.2  augustss 
    217   1.8  drochner int	ukbd_enable __P((void *, int));
    218   1.2  augustss void	ukbd_set_leds __P((void *, int));
    219  1.23  augustss 
    220  1.16  augustss #if defined(__NetBSD__)
    221   1.2  augustss int	ukbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
    222  1.20  augustss void	ukbd_rawrepeat __P((void *v));
    223   1.1  augustss 
    224   1.8  drochner const struct wskbd_accessops ukbd_accessops = {
    225   1.8  drochner 	ukbd_enable,
    226   1.8  drochner 	ukbd_set_leds,
    227   1.8  drochner 	ukbd_ioctl,
    228   1.8  drochner };
    229   1.8  drochner 
    230  1.23  augustss extern const struct wscons_keydesc ukbd_keydesctab[];
    231  1.23  augustss 
    232   1.8  drochner const struct wskbd_mapdata ukbd_keymapdata = {
    233  1.23  augustss 	ukbd_keydesctab,
    234   1.8  drochner 	KB_US,
    235   1.8  drochner };
    236  1.16  augustss #endif
    237   1.8  drochner 
    238  1.16  augustss USB_DECLARE_DRIVER(ukbd);
    239   1.1  augustss 
    240  1.16  augustss USB_MATCH(ukbd)
    241   1.1  augustss {
    242  1.16  augustss 	USB_MATCH_START(ukbd, uaa);
    243   1.1  augustss 	usb_interface_descriptor_t *id;
    244   1.1  augustss 
    245   1.1  augustss 	/* Check that this is a keyboard that speaks the boot protocol. */
    246   1.1  augustss 	if (!uaa->iface)
    247   1.1  augustss 		return (UMATCH_NONE);
    248   1.1  augustss 	id = usbd_get_interface_descriptor(uaa->iface);
    249  1.14  augustss 	if (!id ||
    250  1.14  augustss 	    id->bInterfaceClass != UCLASS_HID ||
    251   1.1  augustss 	    id->bInterfaceSubClass != USUBCLASS_BOOT ||
    252   1.1  augustss 	    id->bInterfaceProtocol != UPROTO_BOOT_KEYBOARD)
    253   1.1  augustss 		return (UMATCH_NONE);
    254   1.1  augustss 	return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    255   1.1  augustss }
    256   1.1  augustss 
    257  1.16  augustss USB_ATTACH(ukbd)
    258   1.1  augustss {
    259  1.16  augustss 	USB_ATTACH_START(ukbd, sc, uaa);
    260   1.1  augustss 	usbd_interface_handle iface = uaa->iface;
    261   1.1  augustss 	usb_interface_descriptor_t *id;
    262   1.1  augustss 	usb_endpoint_descriptor_t *ed;
    263   1.1  augustss 	usbd_status r;
    264   1.1  augustss 	char devinfo[1024];
    265  1.16  augustss #if defined(__NetBSD__)
    266   1.2  augustss 	struct wskbddev_attach_args a;
    267  1.16  augustss #else
    268  1.16  augustss 	int i;
    269  1.16  augustss #endif
    270   1.1  augustss 
    271   1.1  augustss 	sc->sc_iface = iface;
    272   1.1  augustss 	id = usbd_get_interface_descriptor(iface);
    273   1.1  augustss 	usbd_devinfo(uaa->device, 0, devinfo);
    274  1.16  augustss 	USB_ATTACH_SETUP;
    275  1.16  augustss 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    276  1.12  augustss 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    277  1.16  augustss 
    278   1.1  augustss 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    279   1.1  augustss 	if (!ed) {
    280   1.1  augustss 		printf("%s: could not read endpoint descriptor\n",
    281  1.16  augustss 		       USBDEVNAME(sc->sc_dev));
    282  1.16  augustss 		USB_ATTACH_ERROR_RETURN;
    283   1.1  augustss 	}
    284   1.1  augustss 
    285  1.15  augustss 	DPRINTFN(10,("ukbd_attach: bLength=%d bDescriptorType=%d "
    286  1.15  augustss 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    287  1.15  augustss 		     " bInterval=%d\n",
    288  1.15  augustss 		     ed->bLength, ed->bDescriptorType,
    289  1.15  augustss 		     ed->bEndpointAddress & UE_ADDR,
    290  1.15  augustss 		     ed->bEndpointAddress & UE_IN ? "in" : "out",
    291  1.15  augustss 		     ed->bmAttributes & UE_XFERTYPE,
    292  1.15  augustss 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
    293   1.1  augustss 
    294   1.1  augustss 	if ((ed->bEndpointAddress & UE_IN) != UE_IN ||
    295   1.1  augustss 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    296   1.1  augustss 		printf("%s: unexpected endpoint\n",
    297  1.16  augustss 		       USBDEVNAME(sc->sc_dev));
    298  1.16  augustss 		USB_ATTACH_ERROR_RETURN;
    299   1.1  augustss 	}
    300   1.1  augustss 
    301   1.1  augustss 	if ((usbd_get_quirks(uaa->device)->uq_flags & UQ_NO_SET_PROTO) == 0) {
    302   1.1  augustss 		r = usbd_set_protocol(iface, 0);
    303   1.1  augustss 		DPRINTFN(5, ("ukbd_attach: protocol set\n"));
    304   1.1  augustss 		if (r != USBD_NORMAL_COMPLETION) {
    305   1.1  augustss 			printf("%s: set protocol failed\n",
    306  1.16  augustss 			       USBDEVNAME(sc->sc_dev));
    307  1.16  augustss 			USB_ATTACH_ERROR_RETURN;
    308   1.1  augustss 		}
    309   1.1  augustss 	}
    310  1.20  augustss 
    311   1.6  augustss 	/* Ignore if SETIDLE fails since it is not crucial. */
    312   1.5  augustss 	usbd_set_idle(iface, 0, 0);
    313   1.1  augustss 
    314   1.1  augustss 	sc->sc_ep_addr = ed->bEndpointAddress;
    315   1.2  augustss 
    316  1.29   thorpej 	/*
    317  1.29   thorpej 	 * Remember if we're the console keyboard.
    318  1.29   thorpej 	 *
    319  1.31   thorpej 	 * XXX This always picks the first keyboard on the
    320  1.31   thorpej 	 * first USB bus, but what else can we really do?
    321  1.29   thorpej 	 */
    322  1.31   thorpej 	if ((sc->sc_console_keyboard = ukbd_is_console) != 0) {
    323  1.29   thorpej 		/* Don't let any other keyboard have it. */
    324  1.31   thorpej 		ukbd_is_console = 0;
    325  1.29   thorpej 	}
    326  1.29   thorpej 
    327  1.31   thorpej 	if (sc->sc_console_keyboard) {
    328  1.32  augustss 		DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc));
    329  1.31   thorpej 		wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
    330  1.34  wrstuden 		ukbd_enable(sc, 1);
    331  1.31   thorpej 	}
    332  1.29   thorpej 
    333  1.29   thorpej 	a.console = sc->sc_console_keyboard;
    334   1.8  drochner 
    335   1.8  drochner 	a.keymap = &ukbd_keymapdata;
    336   1.8  drochner 
    337   1.8  drochner 	a.accessops = &ukbd_accessops;
    338   1.2  augustss 	a.accesscookie = sc;
    339   1.8  drochner 
    340  1.19  augustss 	/* Flash the leds; no real purpose, just shows we're alive. */
    341  1.19  augustss 	ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS);
    342  1.23  augustss 	usbd_delay_ms(uaa->device, 400);
    343  1.19  augustss 	ukbd_set_leds(sc, 0);
    344  1.19  augustss 
    345  1.20  augustss 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
    346  1.20  augustss 
    347  1.16  augustss 	USB_ATTACH_SUCCESS_RETURN;
    348   1.1  augustss }
    349   1.1  augustss 
    350   1.8  drochner int
    351   1.8  drochner ukbd_enable(v, on)
    352   1.8  drochner 	void *v;
    353   1.8  drochner 	int on;
    354   1.8  drochner {
    355   1.9  augustss 	struct ukbd_softc *sc = v;
    356   1.9  augustss 	usbd_status r;
    357   1.9  augustss 
    358  1.38  augustss 	/* Should only be called to change state */
    359  1.38  augustss 	if (sc->sc_enabled == on) {
    360  1.38  augustss #ifdef DIAGNOSTIC
    361  1.38  augustss 		printf("ukbd_enable: %s: bad call on=%d\n",
    362  1.38  augustss 		       USBDEVNAME(sc->sc_dev), on);
    363  1.38  augustss #endif
    364  1.38  augustss 		return (EBUSY);
    365  1.38  augustss 	}
    366  1.38  augustss 
    367  1.33  augustss 	DPRINTF(("ukbd_enable: sc=%p on=%d\n", sc, on));
    368   1.9  augustss 	if (on) {
    369   1.9  augustss 		/* Set up interrupt pipe. */
    370   1.9  augustss 		r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    371   1.9  augustss 					USBD_SHORT_XFER_OK,
    372   1.9  augustss 					&sc->sc_intrpipe, sc, &sc->sc_ndata,
    373   1.9  augustss 					sizeof(sc->sc_ndata), ukbd_intr);
    374   1.9  augustss 		if (r != USBD_NORMAL_COMPLETION)
    375   1.9  augustss 			return (EIO);
    376   1.9  augustss 	} else {
    377   1.9  augustss 		/* Disable interrupts. */
    378   1.9  augustss 		usbd_abort_pipe(sc->sc_intrpipe);
    379   1.9  augustss 		usbd_close_pipe(sc->sc_intrpipe);
    380   1.9  augustss 	}
    381  1.38  augustss 	sc->sc_enabled = on;
    382   1.9  augustss 
    383   1.8  drochner 	return (0);
    384  1.37  augustss }
    385  1.37  augustss 
    386  1.37  augustss int
    387  1.37  augustss ukbd_activate(self, act)
    388  1.37  augustss 	struct device *self;
    389  1.37  augustss 	enum devact act;
    390  1.37  augustss {
    391  1.37  augustss 	return (0);
    392  1.37  augustss }
    393  1.37  augustss 
    394  1.37  augustss int
    395  1.37  augustss ukbd_detach(self, flags)
    396  1.37  augustss 	struct device  *self;
    397  1.37  augustss 	int flags;
    398  1.37  augustss {
    399  1.37  augustss 	struct ukbd_softc *sc = (struct ukbd_softc *)self;
    400  1.37  augustss 	int rv = 0;
    401  1.37  augustss 
    402  1.37  augustss 	DPRINTF(("ukbd_detach: sc=%p flags=%d\n", sc, flags));
    403  1.37  augustss 	if (sc->sc_console_keyboard) {
    404  1.37  augustss 		/*
    405  1.37  augustss 		 * XXX Should probably disconnect our consops,
    406  1.37  augustss 		 * XXX and either notify some other keyboard that
    407  1.37  augustss 		 * XXX it can now be the console, or if there aren't
    408  1.37  augustss 		 * XXX any more USB keyboards, set ukbd_is_console
    409  1.37  augustss 		 * XXX back to 1 so that the next USB keyboard attached
    410  1.37  augustss 		 * XXX to the system will get it.
    411  1.37  augustss 		 */
    412  1.37  augustss 		panic("ukbd_detach: console keyboard");
    413  1.37  augustss 	}
    414  1.37  augustss 	/* No need to do reference counting of ums, wskbd has all the goo. */
    415  1.37  augustss 	if (sc->sc_wskbddev)
    416  1.37  augustss 		rv = config_detach(sc->sc_wskbddev, flags);
    417  1.37  augustss 	return (rv);
    418   1.1  augustss }
    419   1.1  augustss 
    420   1.1  augustss void
    421   1.1  augustss ukbd_intr(reqh, addr, status)
    422   1.1  augustss 	usbd_request_handle reqh;
    423   1.1  augustss 	usbd_private_handle addr;
    424   1.1  augustss 	usbd_status status;
    425   1.1  augustss {
    426   1.1  augustss 	struct ukbd_softc *sc = addr;
    427   1.1  augustss 	struct ukbd_data *ud = &sc->sc_ndata;
    428   1.1  augustss 	int mod, omod;
    429  1.23  augustss 	u_int16_t ibuf[MAXKEYS];	/* chars events */
    430  1.30  augustss 	int s;
    431   1.1  augustss 	int nkeys, i, j;
    432  1.24  augustss 	int key;
    433   1.1  augustss #define ADDKEY(c) ibuf[nkeys++] = (c)
    434   1.1  augustss 
    435   1.1  augustss 	DPRINTFN(5, ("ukbd_intr: status=%d\n", status));
    436   1.1  augustss 	if (status == USBD_CANCELLED)
    437   1.1  augustss 		return;
    438   1.1  augustss 
    439   1.1  augustss 	if (status != USBD_NORMAL_COMPLETION) {
    440   1.1  augustss 		DPRINTF(("ukbd_intr: status=%d\n", status));
    441   1.4  augustss 		usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
    442   1.1  augustss 		return;
    443   1.1  augustss 	}
    444   1.1  augustss 
    445   1.1  augustss 	DPRINTFN(5, ("          mod=0x%02x key0=0x%02x key1=0x%02x\n",
    446   1.1  augustss 		     ud->modifiers, ud->keycode[0], ud->keycode[1]));
    447   1.1  augustss 
    448   1.1  augustss 	if (ud->keycode[0] == KEY_ERROR)
    449   1.1  augustss 		return;		/* ignore  */
    450   1.1  augustss 	nkeys = 0;
    451   1.1  augustss 	mod = ud->modifiers;
    452   1.1  augustss 	omod = sc->sc_odata.modifiers;
    453   1.1  augustss 	if (mod != omod)
    454   1.1  augustss 		for (i = 0; i < NMOD; i++)
    455   1.1  augustss 			if (( mod & ukbd_mods[i].mask) !=
    456   1.1  augustss 			    (omod & ukbd_mods[i].mask))
    457   1.1  augustss 				ADDKEY(ukbd_mods[i].key |
    458   1.1  augustss 				       (mod & ukbd_mods[i].mask
    459   1.1  augustss 					  ? PRESS : RELEASE));
    460   1.1  augustss 	if (memcmp(ud->keycode, sc->sc_odata.keycode, NKEYCODE) != 0) {
    461   1.1  augustss 		/* Check for released keys. */
    462   1.1  augustss 		for (i = 0; i < NKEYCODE; i++) {
    463   1.1  augustss 			key = sc->sc_odata.keycode[i];
    464   1.1  augustss 			if (key == 0)
    465   1.1  augustss 				continue;
    466   1.1  augustss 			for (j = 0; j < NKEYCODE; j++)
    467   1.1  augustss 				if (key == ud->keycode[j])
    468   1.1  augustss 					goto rfound;
    469  1.23  augustss 			ADDKEY(key | RELEASE);
    470   1.1  augustss 		rfound:
    471   1.1  augustss 			;
    472   1.1  augustss 		}
    473   1.1  augustss 
    474   1.1  augustss 		/* Check for pressed keys. */
    475   1.1  augustss 		for (i = 0; i < NKEYCODE; i++) {
    476   1.1  augustss 			key = ud->keycode[i];
    477   1.1  augustss 			if (key == 0)
    478   1.1  augustss 				continue;
    479   1.1  augustss 			for (j = 0; j < NKEYCODE; j++)
    480   1.1  augustss 				if (key == sc->sc_odata.keycode[j])
    481   1.1  augustss 					goto pfound;
    482  1.23  augustss 			DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key));
    483  1.23  augustss 			ADDKEY(key | PRESS);
    484   1.1  augustss 		pfound:
    485   1.1  augustss 			;
    486   1.1  augustss 		}
    487   1.1  augustss 	}
    488   1.1  augustss 	sc->sc_odata = *ud;
    489   1.1  augustss 
    490  1.20  augustss 	if (nkeys == 0)
    491  1.20  augustss 		return;
    492  1.20  augustss 
    493   1.3  augustss 	if (sc->sc_polling) {
    494  1.23  augustss 		DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0]));
    495  1.23  augustss 		memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
    496  1.23  augustss 		sc->sc_npollchar = nkeys;
    497   1.3  augustss 		return;
    498   1.3  augustss 	}
    499  1.20  augustss #ifdef WSDISPLAY_COMPAT_RAWKBD
    500  1.19  augustss 	if (sc->sc_rawkbd) {
    501  1.19  augustss 		char cbuf[MAXKEYS * 2];
    502  1.24  augustss 		int c;
    503  1.20  augustss 		int npress;
    504  1.20  augustss 
    505  1.23  augustss 		for (npress = i = j = 0; i < nkeys; i++) {
    506  1.23  augustss 			key = ibuf[i];
    507  1.23  augustss 			c = ukbd_trtab[key & CODEMASK];
    508  1.23  augustss 			if (c == NN)
    509  1.23  augustss 				continue;
    510  1.19  augustss 			if (c & 0x80)
    511  1.19  augustss 				cbuf[j++] = 0xe0;
    512  1.19  augustss 			cbuf[j] = c & 0x7f;
    513  1.23  augustss 			if (key & RELEASE)
    514  1.19  augustss 				cbuf[j] |= 0x80;
    515  1.20  augustss 			else {
    516  1.23  augustss 				/* remember pressed keys for autorepeat */
    517  1.20  augustss 				if (c & 0x80)
    518  1.20  augustss 					sc->sc_rep[npress++] = 0xe0;
    519  1.20  augustss 				sc->sc_rep[npress++] = c & 0x7f;
    520  1.20  augustss 			}
    521  1.27  augustss 			DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n",
    522  1.27  augustss 				    c & 0x80 ? "0xe0 " : "",
    523  1.27  augustss 				    cbuf[j]));
    524  1.23  augustss 			j++;
    525  1.19  augustss 		}
    526  1.30  augustss 		s = spltty();
    527  1.19  augustss 		wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
    528  1.30  augustss 		splx(s);
    529  1.20  augustss 		untimeout(ukbd_rawrepeat, sc);
    530  1.20  augustss 		if (npress != 0) {
    531  1.20  augustss 			sc->sc_nrep = npress;
    532  1.20  augustss 			timeout(ukbd_rawrepeat, sc, hz * REP_DELAY1 / 1000);
    533  1.20  augustss 		}
    534  1.19  augustss 		return;
    535  1.19  augustss 	}
    536  1.19  augustss #endif
    537  1.19  augustss 
    538  1.30  augustss 	s = spltty();
    539   1.2  augustss 	for (i = 0; i < nkeys; i++) {
    540  1.23  augustss 		key = ibuf[i];
    541   1.2  augustss 		wskbd_input(sc->sc_wskbddev,
    542  1.23  augustss 		    key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
    543  1.23  augustss 		    key&CODEMASK);
    544  1.20  augustss 	}
    545  1.30  augustss 	splx(s);
    546   1.1  augustss }
    547   1.1  augustss 
    548   1.2  augustss void
    549   1.2  augustss ukbd_set_leds(v, leds)
    550   1.2  augustss 	void *v;
    551   1.2  augustss 	int leds;
    552   1.1  augustss {
    553   1.2  augustss 	struct ukbd_softc *sc = v;
    554   1.2  augustss 	u_int8_t res;
    555   1.1  augustss 
    556   1.2  augustss 	DPRINTF(("ukbd_set_leds: sc=%p leds=%d\n", sc, leds));
    557   1.1  augustss 
    558   1.2  augustss 	sc->sc_leds = leds;
    559   1.2  augustss 	res = 0;
    560   1.2  augustss 	if (leds & WSKBD_LED_SCROLL)
    561   1.2  augustss 		res |= SCROLL_LOCK;
    562   1.2  augustss 	if (leds & WSKBD_LED_NUM)
    563   1.2  augustss 		res |= NUM_LOCK;
    564   1.2  augustss 	if (leds & WSKBD_LED_CAPS)
    565   1.2  augustss 		res |= CAPS_LOCK;
    566  1.34  wrstuden 	res |= leds & 0xf8;
    567   1.4  augustss 	usbd_set_report_async(sc->sc_iface, UHID_OUTPUT_REPORT, 0, &res, 1);
    568   1.1  augustss }
    569   1.1  augustss 
    570  1.20  augustss #ifdef WSDISPLAY_COMPAT_RAWKBD
    571  1.20  augustss void
    572  1.20  augustss ukbd_rawrepeat(v)
    573  1.20  augustss 	void *v;
    574  1.20  augustss {
    575  1.20  augustss 	struct ukbd_softc *sc = v;
    576  1.30  augustss 	int s;
    577  1.20  augustss 
    578  1.30  augustss 	s = spltty();
    579  1.20  augustss 	wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
    580  1.30  augustss 	splx(s);
    581  1.20  augustss 	timeout(ukbd_rawrepeat, sc, hz * REP_DELAYN / 1000);
    582  1.20  augustss }
    583  1.20  augustss #endif
    584  1.20  augustss 
    585   1.1  augustss int
    586   1.2  augustss ukbd_ioctl(v, cmd, data, flag, p)
    587   1.2  augustss 	void *v;
    588   1.1  augustss 	u_long cmd;
    589   1.2  augustss 	caddr_t data;
    590   1.1  augustss 	int flag;
    591   1.1  augustss 	struct proc *p;
    592   1.1  augustss {
    593   1.2  augustss 	struct ukbd_softc *sc = v;
    594   1.1  augustss 
    595   1.2  augustss 	switch (cmd) {
    596   1.2  augustss 	case WSKBDIO_GTYPE:
    597  1.20  augustss 		*(int *)data = WSKBD_TYPE_USB;
    598  1.17  augustss 		return (0);
    599   1.2  augustss 	case WSKBDIO_SETLEDS:
    600   1.2  augustss 		ukbd_set_leds(v, *(int *)data);
    601  1.17  augustss 		return (0);
    602   1.2  augustss 	case WSKBDIO_GETLEDS:
    603   1.2  augustss 		*(int *)data = sc->sc_leds;
    604   1.2  augustss 		return (0);
    605   1.2  augustss #ifdef WSDISPLAY_COMPAT_RAWKBD
    606   1.2  augustss 	case WSKBDIO_SETMODE:
    607  1.19  augustss 		DPRINTF(("ukbd_ioctl: set raw = %d\n", *(int *)data));
    608   1.2  augustss 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
    609  1.20  augustss 		untimeout(ukbd_rawrepeat, sc);
    610   1.2  augustss 		return (0);
    611   1.2  augustss #endif
    612   1.2  augustss 	}
    613  1.17  augustss 	return (-1);
    614   1.1  augustss }
    615   1.7  augustss 
    616   1.7  augustss /* Console interface. */
    617   1.3  augustss void
    618   1.3  augustss ukbd_cngetc(v, type, data)
    619   1.3  augustss 	void *v;
    620   1.3  augustss 	u_int *type;
    621   1.3  augustss 	int *data;
    622   1.3  augustss {
    623   1.3  augustss 	struct ukbd_softc *sc = v;
    624  1.35  augustss 	int s;
    625   1.4  augustss 	int c;
    626   1.3  augustss 
    627  1.23  augustss 	DPRINTFN(-1,("ukbd_cngetc: enter\n"));
    628  1.35  augustss 	s = splusb();
    629   1.3  augustss 	sc->sc_polling = 1;
    630  1.23  augustss 	while(sc->sc_npollchar <= 0)
    631   1.3  augustss 		usbd_dopoll(sc->sc_iface);
    632   1.3  augustss 	sc->sc_polling = 0;
    633  1.23  augustss 	c = sc->sc_pollchars[0];
    634  1.23  augustss 	sc->sc_npollchar--;
    635  1.23  augustss 	memcpy(sc->sc_pollchars, sc->sc_pollchars+1,
    636  1.23  augustss 	       sc->sc_npollchar * sizeof(u_int16_t));
    637   1.6  augustss 	*type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    638  1.23  augustss 	*data = c & CODEMASK;
    639  1.35  augustss 	splx(s);
    640  1.23  augustss 	DPRINTFN(-1,("ukbd_cngetc: return 0x%02x\n", c));
    641   1.3  augustss }
    642   1.3  augustss 
    643   1.3  augustss void
    644   1.3  augustss ukbd_cnpollc(v, on)
    645   1.3  augustss 	void *v;
    646   1.3  augustss         int on;
    647   1.3  augustss {
    648   1.6  augustss 	struct ukbd_softc *sc = v;
    649   1.6  augustss 
    650  1.19  augustss 	DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
    651   1.6  augustss 
    652   1.6  augustss 	usbd_set_polling(sc->sc_iface, on);
    653   1.3  augustss }
    654  1.18  augustss 
    655  1.18  augustss int
    656  1.31   thorpej ukbd_cnattach()
    657  1.18  augustss {
    658  1.18  augustss 
    659  1.31   thorpej 	/*
    660  1.31   thorpej 	 * XXX USB requires too many parts of the kernel to be running
    661  1.31   thorpej 	 * XXX in order to work, so we can't do much for the console
    662  1.31   thorpej 	 * XXX keyboard until autconfiguration has run its course.
    663  1.31   thorpej 	 */
    664  1.31   thorpej 	ukbd_is_console = 1;
    665  1.18  augustss 	return (0);
    666  1.18  augustss }
    667