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