Home | History | Annotate | Line # | Download | only in usb
ukbd.c revision 1.98
      1  1.98    cegger /*      $NetBSD: ukbd.c,v 1.98 2008/04/05 16:35:35 cegger 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.59  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) 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.85  augustss  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
     42  1.17  augustss  */
     43  1.72     lukem 
     44  1.72     lukem #include <sys/cdefs.h>
     45  1.98    cegger __KERNEL_RCSID(0, "$NetBSD: ukbd.c,v 1.98 2008/04/05 16:35:35 cegger Exp $");
     46  1.17  augustss 
     47   1.1  augustss #include <sys/param.h>
     48   1.1  augustss #include <sys/systm.h>
     49  1.57   thorpej #include <sys/callout.h>
     50   1.1  augustss #include <sys/kernel.h>
     51   1.1  augustss #include <sys/device.h>
     52   1.1  augustss #include <sys/ioctl.h>
     53   1.1  augustss #include <sys/tty.h>
     54   1.1  augustss #include <sys/file.h>
     55   1.1  augustss #include <sys/select.h>
     56   1.1  augustss #include <sys/proc.h>
     57   1.1  augustss #include <sys/vnode.h>
     58   1.1  augustss #include <sys/poll.h>
     59   1.1  augustss 
     60   1.1  augustss #include <dev/usb/usb.h>
     61   1.2  augustss #include <dev/usb/usbhid.h>
     62  1.37  augustss 
     63   1.1  augustss #include <dev/usb/usbdi.h>
     64   1.1  augustss #include <dev/usb/usbdi_util.h>
     65   1.1  augustss #include <dev/usb/usbdevs.h>
     66   1.1  augustss #include <dev/usb/usb_quirks.h>
     67  1.75  augustss #include <dev/usb/uhidev.h>
     68   1.1  augustss #include <dev/usb/hid.h>
     69  1.31   thorpej #include <dev/usb/ukbdvar.h>
     70   1.1  augustss 
     71   1.2  augustss #include <dev/wscons/wsconsio.h>
     72   1.2  augustss #include <dev/wscons/wskbdvar.h>
     73   1.2  augustss #include <dev/wscons/wsksymdef.h>
     74   1.2  augustss #include <dev/wscons/wsksymvar.h>
     75   1.2  augustss 
     76  1.89      cube #include "opt_ukbd_layout.h"
     77   1.2  augustss #include "opt_wsdisplay_compat.h"
     78  1.69  augustss #include "opt_ddb.h"
     79  1.21  augustss 
     80  1.62  augustss #ifdef UKBD_DEBUG
     81  1.39  augustss #define DPRINTF(x)	if (ukbddebug) logprintf x
     82  1.39  augustss #define DPRINTFN(n,x)	if (ukbddebug>(n)) logprintf x
     83   1.1  augustss int	ukbddebug = 0;
     84   1.1  augustss #else
     85   1.1  augustss #define DPRINTF(x)
     86   1.1  augustss #define DPRINTFN(n,x)
     87   1.1  augustss #endif
     88   1.1  augustss 
     89  1.75  augustss #define MAXKEYCODE 6
     90  1.75  augustss #define MAXMOD 8		/* max 32 */
     91   1.2  augustss 
     92   1.1  augustss struct ukbd_data {
     93  1.75  augustss 	u_int32_t	modifiers;
     94  1.75  augustss 	u_int8_t	keycode[MAXKEYCODE];
     95   1.1  augustss };
     96   1.1  augustss 
     97  1.23  augustss #define PRESS    0x000
     98  1.23  augustss #define RELEASE  0x100
     99  1.23  augustss #define CODEMASK 0x0ff
    100   1.1  augustss 
    101  1.23  augustss #if defined(__NetBSD__) && defined(WSDISPLAY_COMPAT_RAWKBD)
    102   1.5  augustss #define NN 0			/* no translation */
    103  1.82  augustss /*
    104  1.23  augustss  * Translate USB keycodes to US keyboard XT scancodes.
    105  1.76  augustss  * Scancodes >= 0x80 represent EXTENDED keycodes.
    106  1.76  augustss  *
    107  1.96      tron  * See http://www.microsoft.com/whdc/device/input/Scancode.mspx
    108  1.19  augustss  */
    109  1.65  jdolecek Static const u_int8_t ukbd_trtab[256] = {
    110  1.76  augustss       NN,   NN,   NN,   NN, 0x1e, 0x30, 0x2e, 0x20, /* 00 - 07 */
    111  1.76  augustss     0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, /* 08 - 0f */
    112  1.76  augustss     0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, /* 10 - 17 */
    113  1.76  augustss     0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, /* 18 - 1f */
    114  1.76  augustss     0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 20 - 27 */
    115  1.76  augustss     0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, /* 28 - 2f */
    116  1.76  augustss     0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, /* 30 - 37 */
    117  1.76  augustss     0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, /* 38 - 3f */
    118  1.76  augustss     0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xaa, 0x46, /* 40 - 47 */
    119  1.76  augustss     0x7f, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd, /* 48 - 4f */
    120  1.76  augustss     0xcb, 0xd0, 0xc8, 0x45, 0xb5, 0x37, 0x4a, 0x4e, /* 50 - 57 */
    121  1.76  augustss     0x9c, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47, /* 58 - 5f */
    122  1.76  augustss     0x48, 0x49, 0x52, 0x53, 0x56, 0xdd,   NN, 0x59, /* 60 - 67 */
    123  1.96      tron     0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a,   NN, /* 68 - 6f */
    124  1.76  augustss       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* 70 - 77 */
    125  1.76  augustss       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* 78 - 7f */
    126  1.76  augustss       NN,   NN,   NN,   NN,   NN, 0x7e,   NN, 0x73, /* 80 - 87 */
    127  1.76  augustss     0x70, 0x7d, 0x79, 0x7b, 0x5c,   NN,   NN,   NN, /* 88 - 8f */
    128  1.76  augustss       NN,   NN, 0x78, 0x77, 0x76,   NN,   NN,   NN, /* 90 - 97 */
    129  1.76  augustss       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* 98 - 9f */
    130  1.76  augustss       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* a0 - a7 */
    131  1.76  augustss       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* a8 - af */
    132  1.76  augustss       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* b0 - b7 */
    133  1.76  augustss       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* b8 - bf */
    134  1.76  augustss       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* c0 - c7 */
    135  1.76  augustss       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* c8 - cf */
    136  1.76  augustss       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* d0 - d7 */
    137  1.76  augustss       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* d8 - df */
    138  1.76  augustss     0x1d, 0x2a, 0x38, 0xdb, 0x9d, 0x36, 0xb8, 0xdc, /* e0 - e7 */
    139  1.76  augustss       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* e8 - ef */
    140  1.76  augustss       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* f0 - f7 */
    141  1.76  augustss       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* f8 - ff */
    142   1.1  augustss };
    143  1.23  augustss #endif /* defined(__NetBSD__) && defined(WSDISPLAY_COMPAT_RAWKBD) */
    144   1.1  augustss 
    145   1.1  augustss #define KEY_ERROR 0x01
    146   1.1  augustss 
    147  1.75  augustss #define MAXKEYS (MAXMOD+2*MAXKEYCODE)
    148  1.20  augustss 
    149   1.1  augustss struct ukbd_softc {
    150  1.75  augustss 	struct uhidev sc_hdev;
    151   1.1  augustss 
    152   1.1  augustss 	struct ukbd_data sc_ndata;
    153   1.1  augustss 	struct ukbd_data sc_odata;
    154  1.75  augustss 	struct hid_location sc_modloc[MAXMOD];
    155  1.75  augustss 	u_int sc_nmod;
    156  1.75  augustss 	struct {
    157  1.75  augustss 		u_int32_t mask;
    158  1.75  augustss 		u_int8_t key;
    159  1.75  augustss 	} sc_mods[MAXMOD];
    160  1.75  augustss 
    161  1.75  augustss 	struct hid_location sc_keycodeloc;
    162  1.75  augustss 	u_int sc_nkeycode;
    163   1.1  augustss 
    164   1.9  augustss 	char sc_enabled;
    165   1.1  augustss 
    166  1.29   thorpej 	int sc_console_keyboard;	/* we are the console keyboard */
    167  1.29   thorpej 
    168  1.62  augustss 	char sc_debounce;		/* for quirk handling */
    169  1.70  augustss 	usb_callout_t sc_delay;		/* for quirk handling */
    170  1.62  augustss 	struct ukbd_data sc_data;	/* for quirk handling */
    171  1.62  augustss 
    172  1.75  augustss 	struct hid_location sc_numloc;
    173  1.75  augustss 	struct hid_location sc_capsloc;
    174  1.75  augustss 	struct hid_location sc_scroloc;
    175   1.2  augustss 	int sc_leds;
    176  1.16  augustss #if defined(__NetBSD__)
    177  1.97    dyoung 	device_t sc_wskbddev;
    178  1.57   thorpej 
    179  1.23  augustss #if defined(WSDISPLAY_COMPAT_RAWKBD)
    180  1.87  augustss 	int sc_rawkbd;
    181  1.87  augustss #if defined(UKBD_REPEAT)
    182  1.87  augustss 	usb_callout_t sc_rawrepeat_ch;
    183  1.20  augustss #define REP_DELAY1 400
    184  1.20  augustss #define REP_DELAYN 100
    185  1.20  augustss 	int sc_nrep;
    186  1.20  augustss 	char sc_rep[MAXKEYS];
    187  1.87  augustss #endif /* defined(UKBD_REPEAT) */
    188  1.23  augustss #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
    189   1.3  augustss 
    190  1.84  augustss 	int sc_spl;
    191   1.3  augustss 	int sc_polling;
    192  1.23  augustss 	int sc_npollchar;
    193  1.23  augustss 	u_int16_t sc_pollchars[MAXKEYS];
    194  1.23  augustss #endif /* defined(__NetBSD__) */
    195  1.40  augustss 
    196  1.40  augustss 	u_char sc_dying;
    197   1.1  augustss };
    198   1.1  augustss 
    199  1.62  augustss #ifdef UKBD_DEBUG
    200  1.62  augustss #define UKBDTRACESIZE 64
    201  1.62  augustss struct ukbdtraceinfo {
    202  1.62  augustss 	int unit;
    203  1.62  augustss 	struct timeval tv;
    204  1.62  augustss 	struct ukbd_data ud;
    205  1.62  augustss };
    206  1.62  augustss struct ukbdtraceinfo ukbdtracedata[UKBDTRACESIZE];
    207  1.62  augustss int ukbdtraceindex = 0;
    208  1.62  augustss int ukbdtrace = 0;
    209  1.62  augustss void ukbdtracedump(void);
    210  1.62  augustss void
    211  1.62  augustss ukbdtracedump(void)
    212  1.62  augustss {
    213  1.62  augustss 	int i;
    214  1.62  augustss 	for (i = 0; i < UKBDTRACESIZE; i++) {
    215  1.82  augustss 		struct ukbdtraceinfo *p =
    216  1.62  augustss 		    &ukbdtracedata[(i+ukbdtraceindex)%UKBDTRACESIZE];
    217  1.62  augustss 		printf("%lu.%06lu: mod=0x%02x key0=0x%02x key1=0x%02x "
    218  1.62  augustss 		       "key2=0x%02x key3=0x%02x\n",
    219  1.62  augustss 		       p->tv.tv_sec, p->tv.tv_usec,
    220  1.62  augustss 		       p->ud.modifiers, p->ud.keycode[0], p->ud.keycode[1],
    221  1.62  augustss 		       p->ud.keycode[2], p->ud.keycode[3]);
    222  1.62  augustss 	}
    223  1.62  augustss }
    224  1.62  augustss #endif
    225  1.62  augustss 
    226   1.1  augustss #define	UKBDUNIT(dev)	(minor(dev))
    227   1.1  augustss #define	UKBD_CHUNK	128	/* chunk size for read */
    228   1.1  augustss #define	UKBD_BSIZE	1020	/* buffer size */
    229   1.1  augustss 
    230  1.58  augustss Static int	ukbd_is_console;
    231  1.31   thorpej 
    232  1.60  augustss Static void	ukbd_cngetc(void *, u_int *, int *);
    233  1.60  augustss Static void	ukbd_cnpollc(void *, int);
    234   1.3  augustss 
    235  1.16  augustss #if defined(__NetBSD__)
    236   1.8  drochner const struct wskbd_consops ukbd_consops = {
    237   1.8  drochner 	ukbd_cngetc,
    238   1.8  drochner 	ukbd_cnpollc,
    239  1.92  christos 	NULL,	/* bell */
    240   1.8  drochner };
    241  1.16  augustss #endif
    242   1.8  drochner 
    243  1.75  augustss Static const char *ukbd_parse_desc(struct ukbd_softc *sc);
    244  1.75  augustss 
    245  1.75  augustss Static void	ukbd_intr(struct uhidev *addr, void *ibuf, u_int len);
    246  1.62  augustss Static void	ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud);
    247  1.62  augustss Static void	ukbd_delayed_decode(void *addr);
    248   1.2  augustss 
    249  1.60  augustss Static int	ukbd_enable(void *, int);
    250  1.60  augustss Static void	ukbd_set_leds(void *, int);
    251  1.23  augustss 
    252  1.16  augustss #if defined(__NetBSD__)
    253  1.95  christos Static int	ukbd_ioctl(void *, u_long, void *, int, struct lwp *);
    254  1.88  jonathan #if  defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT)
    255  1.60  augustss Static void	ukbd_rawrepeat(void *v);
    256  1.48    mjacob #endif
    257   1.1  augustss 
    258   1.8  drochner const struct wskbd_accessops ukbd_accessops = {
    259   1.8  drochner 	ukbd_enable,
    260   1.8  drochner 	ukbd_set_leds,
    261   1.8  drochner 	ukbd_ioctl,
    262   1.8  drochner };
    263   1.8  drochner 
    264  1.23  augustss extern const struct wscons_keydesc ukbd_keydesctab[];
    265  1.23  augustss 
    266   1.8  drochner const struct wskbd_mapdata ukbd_keymapdata = {
    267  1.23  augustss 	ukbd_keydesctab,
    268  1.86  augustss #if defined(UKBD_LAYOUT)
    269  1.66  augustss 	UKBD_LAYOUT,
    270  1.86  augustss #elif defined(PCKBD_LAYOUT)
    271  1.86  augustss 	PCKBD_LAYOUT,
    272  1.66  augustss #else
    273   1.8  drochner 	KB_US,
    274  1.66  augustss #endif
    275   1.8  drochner };
    276  1.16  augustss #endif
    277   1.8  drochner 
    278  1.97    dyoung static int ukbd_match(device_t, struct cfdata *, void *);
    279  1.97    dyoung static void ukbd_attach(device_t, device_t, void *);
    280  1.97    dyoung static int ukbd_detach(device_t, int);
    281  1.97    dyoung static int ukbd_activate(device_t, enum devact);
    282  1.97    dyoung static void ukbd_childdet(device_t, device_t);
    283  1.97    dyoung 
    284  1.97    dyoung extern struct cfdriver ukbd_cd;
    285  1.97    dyoung 
    286  1.97    dyoung CFATTACH_DECL2(ukbd, sizeof(struct ukbd_softc), ukbd_match, ukbd_attach,
    287  1.97    dyoung     ukbd_detach, ukbd_activate, NULL, ukbd_childdet);
    288   1.1  augustss 
    289  1.75  augustss int
    290  1.97    dyoung ukbd_match(device_t parent, struct cfdata *match, void *aux)
    291   1.1  augustss {
    292  1.75  augustss 	struct uhidev_attach_arg *uha = aux;
    293  1.75  augustss 	int size;
    294  1.75  augustss 	void *desc;
    295  1.82  augustss 
    296  1.75  augustss 	uhidev_get_report_desc(uha->parent, &desc, &size);
    297  1.75  augustss 	if (!hid_is_collection(desc, size, uha->reportid,
    298  1.75  augustss 			       HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD)))
    299   1.1  augustss 		return (UMATCH_NONE);
    300  1.75  augustss 
    301  1.75  augustss 	return (UMATCH_IFACECLASS);
    302   1.1  augustss }
    303   1.1  augustss 
    304  1.75  augustss void
    305  1.97    dyoung ukbd_attach(device_t parent, device_t self, void *aux)
    306   1.1  augustss {
    307  1.97    dyoung 	struct ukbd_softc *sc = device_private(self);
    308  1.75  augustss 	struct uhidev_attach_arg *uha = aux;
    309  1.62  augustss 	u_int32_t qflags;
    310  1.75  augustss 	const char *parseerr;
    311  1.16  augustss #if defined(__NetBSD__)
    312   1.2  augustss 	struct wskbddev_attach_args a;
    313  1.16  augustss #else
    314  1.16  augustss 	int i;
    315  1.16  augustss #endif
    316  1.82  augustss 
    317  1.75  augustss 	sc->sc_hdev.sc_intr = ukbd_intr;
    318  1.75  augustss 	sc->sc_hdev.sc_parent = uha->parent;
    319  1.75  augustss 	sc->sc_hdev.sc_report_id = uha->reportid;
    320  1.75  augustss 
    321  1.75  augustss 	parseerr = ukbd_parse_desc(sc);
    322  1.75  augustss 	if (parseerr != NULL) {
    323  1.98    cegger 		aprint_normal("\n");
    324  1.98    cegger 		aprint_error_dev(&sc->sc_hdev.sc_dev, "attach failed, %s\n",
    325  1.98    cegger 		       parseerr);
    326  1.16  augustss 		USB_ATTACH_ERROR_RETURN;
    327   1.1  augustss 	}
    328  1.82  augustss 
    329  1.75  augustss #ifdef DIAGNOSTIC
    330  1.75  augustss 	printf(": %d modifier keys, %d key codes", sc->sc_nmod,
    331  1.75  augustss 	       sc->sc_nkeycode);
    332  1.75  augustss #endif
    333  1.75  augustss 	printf("\n");
    334   1.1  augustss 
    335   1.1  augustss 
    336  1.75  augustss 	qflags = usbd_get_quirks(uha->parent->sc_udev)->uq_flags;
    337  1.62  augustss 	sc->sc_debounce = (qflags & UQ_SPUR_BUT_UP) != 0;
    338  1.20  augustss 
    339  1.29   thorpej 	/*
    340  1.29   thorpej 	 * Remember if we're the console keyboard.
    341  1.29   thorpej 	 *
    342  1.31   thorpej 	 * XXX This always picks the first keyboard on the
    343  1.31   thorpej 	 * first USB bus, but what else can we really do?
    344  1.29   thorpej 	 */
    345  1.31   thorpej 	if ((sc->sc_console_keyboard = ukbd_is_console) != 0) {
    346  1.29   thorpej 		/* Don't let any other keyboard have it. */
    347  1.31   thorpej 		ukbd_is_console = 0;
    348  1.29   thorpej 	}
    349  1.29   thorpej 
    350  1.31   thorpej 	if (sc->sc_console_keyboard) {
    351  1.32  augustss 		DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc));
    352  1.31   thorpej 		wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
    353  1.34  wrstuden 		ukbd_enable(sc, 1);
    354  1.31   thorpej 	}
    355  1.29   thorpej 
    356  1.29   thorpej 	a.console = sc->sc_console_keyboard;
    357   1.8  drochner 
    358   1.8  drochner 	a.keymap = &ukbd_keymapdata;
    359   1.8  drochner 
    360   1.8  drochner 	a.accessops = &ukbd_accessops;
    361   1.2  augustss 	a.accesscookie = sc;
    362   1.8  drochner 
    363  1.87  augustss #ifdef UKBD_REPEAT
    364  1.70  augustss 	usb_callout_init(sc->sc_rawrepeat_ch);
    365  1.87  augustss #endif
    366  1.57   thorpej 
    367  1.70  augustss 	usb_callout_init(sc->sc_delay);
    368  1.62  augustss 
    369  1.19  augustss 	/* Flash the leds; no real purpose, just shows we're alive. */
    370  1.19  augustss 	ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS);
    371  1.75  augustss 	usbd_delay_ms(uha->parent->sc_udev, 400);
    372  1.19  augustss 	ukbd_set_leds(sc, 0);
    373  1.19  augustss 
    374  1.64  augustss 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
    375  1.55  augustss 
    376  1.16  augustss 	USB_ATTACH_SUCCESS_RETURN;
    377   1.1  augustss }
    378   1.1  augustss 
    379   1.8  drochner int
    380  1.60  augustss ukbd_enable(void *v, int on)
    381   1.8  drochner {
    382   1.9  augustss 	struct ukbd_softc *sc = v;
    383   1.9  augustss 
    384  1.40  augustss 	if (on && sc->sc_dying)
    385  1.40  augustss 		return (EIO);
    386  1.40  augustss 
    387  1.38  augustss 	/* Should only be called to change state */
    388  1.38  augustss 	if (sc->sc_enabled == on) {
    389  1.38  augustss #ifdef DIAGNOSTIC
    390  1.82  augustss 		printf("ukbd_enable: %s: bad call on=%d\n",
    391  1.75  augustss 		       USBDEVNAME(sc->sc_hdev.sc_dev), on);
    392  1.38  augustss #endif
    393  1.38  augustss 		return (EBUSY);
    394  1.38  augustss 	}
    395  1.38  augustss 
    396  1.33  augustss 	DPRINTF(("ukbd_enable: sc=%p on=%d\n", sc, on));
    397  1.75  augustss 	sc->sc_enabled = on;
    398   1.9  augustss 	if (on) {
    399  1.75  augustss 		return (uhidev_open(&sc->sc_hdev));
    400   1.9  augustss 	} else {
    401  1.75  augustss 		uhidev_close(&sc->sc_hdev);
    402  1.75  augustss 		return (0);
    403   1.9  augustss 	}
    404  1.37  augustss }
    405  1.37  augustss 
    406  1.97    dyoung 
    407  1.97    dyoung static void
    408  1.97    dyoung ukbd_childdet(device_t self, device_t child)
    409  1.97    dyoung {
    410  1.97    dyoung 	struct ukbd_softc *sc = device_private(self);
    411  1.97    dyoung 
    412  1.97    dyoung 	KASSERT(sc->sc_wskbddev == child);
    413  1.97    dyoung 	sc->sc_wskbddev = NULL;
    414  1.97    dyoung }
    415  1.97    dyoung 
    416  1.37  augustss int
    417  1.97    dyoung ukbd_activate(device_t self, enum devact act)
    418  1.37  augustss {
    419  1.97    dyoung 	struct ukbd_softc *sc = device_private(self);
    420  1.44  augustss 	int rv = 0;
    421  1.40  augustss 
    422  1.40  augustss 	switch (act) {
    423  1.40  augustss 	case DVACT_ACTIVATE:
    424  1.40  augustss 		return (EOPNOTSUPP);
    425  1.40  augustss 
    426  1.40  augustss 	case DVACT_DEACTIVATE:
    427  1.47  augustss 		if (sc->sc_wskbddev != NULL)
    428  1.44  augustss 			rv = config_deactivate(sc->sc_wskbddev);
    429  1.40  augustss 		sc->sc_dying = 1;
    430  1.40  augustss 		break;
    431  1.40  augustss 	}
    432  1.44  augustss 	return (rv);
    433  1.37  augustss }
    434  1.37  augustss 
    435  1.75  augustss int
    436  1.97    dyoung ukbd_detach(device_t self, int flags)
    437  1.37  augustss {
    438  1.97    dyoung 	struct ukbd_softc *sc = device_private(self);
    439  1.37  augustss 	int rv = 0;
    440  1.37  augustss 
    441  1.37  augustss 	DPRINTF(("ukbd_detach: sc=%p flags=%d\n", sc, flags));
    442  1.46  augustss 
    443  1.37  augustss 	if (sc->sc_console_keyboard) {
    444  1.50  augustss #if 0
    445  1.37  augustss 		/*
    446  1.37  augustss 		 * XXX Should probably disconnect our consops,
    447  1.37  augustss 		 * XXX and either notify some other keyboard that
    448  1.37  augustss 		 * XXX it can now be the console, or if there aren't
    449  1.37  augustss 		 * XXX any more USB keyboards, set ukbd_is_console
    450  1.37  augustss 		 * XXX back to 1 so that the next USB keyboard attached
    451  1.37  augustss 		 * XXX to the system will get it.
    452  1.37  augustss 		 */
    453  1.37  augustss 		panic("ukbd_detach: console keyboard");
    454  1.50  augustss #else
    455  1.51  augustss 		/*
    456  1.51  augustss 		 * Disconnect our consops and set ukbd_is_console
    457  1.51  augustss 		 * back to 1 so that the next USB keyboard attached
    458  1.51  augustss 		 * to the system will get it.
    459  1.51  augustss 		 * XXX Should notify some other keyboard that it can be
    460  1.51  augustss 		 * XXX console, if there are any other keyboards.
    461  1.51  augustss 		 */
    462  1.75  augustss 		printf("%s: was console keyboard\n",
    463  1.75  augustss 		       USBDEVNAME(sc->sc_hdev.sc_dev));
    464  1.50  augustss 		wskbd_cndetach();
    465  1.50  augustss 		ukbd_is_console = 1;
    466  1.50  augustss #endif
    467  1.37  augustss 	}
    468  1.45  augustss 	/* No need to do reference counting of ukbd, wskbd has all the goo. */
    469  1.47  augustss 	if (sc->sc_wskbddev != NULL)
    470  1.37  augustss 		rv = config_detach(sc->sc_wskbddev, flags);
    471  1.68  augustss 
    472  1.68  augustss 	/* The console keyboard does not get a disable call, so check pipe. */
    473  1.75  augustss 	if (sc->sc_hdev.sc_state & UHIDEV_OPEN)
    474  1.75  augustss 		uhidev_close(&sc->sc_hdev);
    475  1.55  augustss 
    476  1.37  augustss 	return (rv);
    477   1.1  augustss }
    478   1.1  augustss 
    479   1.1  augustss void
    480  1.94  christos ukbd_intr(struct uhidev *addr, void *ibuf, u_int len)
    481   1.1  augustss {
    482  1.75  augustss 	struct ukbd_softc *sc = (struct ukbd_softc *)addr;
    483   1.1  augustss 	struct ukbd_data *ud = &sc->sc_ndata;
    484  1.75  augustss 	int i;
    485  1.76  augustss 
    486  1.76  augustss #ifdef UKBD_DEBUG
    487  1.76  augustss 	if (ukbddebug > 5) {
    488  1.76  augustss 		printf("ukbd_intr: data");
    489  1.76  augustss 		for (i = 0; i < len; i++)
    490  1.78  augustss 			printf(" 0x%02x", ((u_char *)ibuf)[i]);
    491  1.76  augustss 		printf("\n");
    492  1.76  augustss 	}
    493  1.76  augustss #endif
    494  1.76  augustss 
    495  1.75  augustss 	ud->modifiers = 0;
    496  1.75  augustss 	for (i = 0; i < sc->sc_nmod; i++)
    497  1.75  augustss 		if (hid_get_data(ibuf, &sc->sc_modloc[i]))
    498  1.80  augustss 			ud->modifiers |= sc->sc_mods[i].mask;
    499  1.75  augustss 	memcpy(ud->keycode, (char *)ibuf + sc->sc_keycodeloc.pos / 8,
    500  1.75  augustss 	       sc->sc_nkeycode);
    501   1.1  augustss 
    502  1.69  augustss 	if (sc->sc_debounce && !sc->sc_polling) {
    503  1.62  augustss 		/*
    504  1.62  augustss 		 * Some keyboards have a peculiar quirk.  They sometimes
    505  1.62  augustss 		 * generate a key up followed by a key down for the same
    506  1.62  augustss 		 * key after about 10 ms.
    507  1.62  augustss 		 * We avoid this bug by holding off decoding for 20 ms.
    508  1.62  augustss 		 */
    509  1.62  augustss 		sc->sc_data = *ud;
    510  1.70  augustss 		usb_callout(sc->sc_delay, hz / 50, ukbd_delayed_decode, sc);
    511  1.74     lukem #ifdef DDB
    512  1.69  augustss 	} else if (sc->sc_console_keyboard && !sc->sc_polling) {
    513  1.69  augustss 		/*
    514  1.69  augustss 		 * For the console keyboard we can't deliver CTL-ALT-ESC
    515  1.69  augustss 		 * from the interrupt routine.  Doing so would start
    516  1.69  augustss 		 * polling from inside the interrupt routine and that
    517  1.69  augustss 		 * loses bigtime.
    518  1.69  augustss 		 */
    519  1.69  augustss 		sc->sc_data = *ud;
    520  1.70  augustss 		usb_callout(sc->sc_delay, 1, ukbd_delayed_decode, sc);
    521  1.69  augustss #endif
    522  1.62  augustss 	} else {
    523  1.62  augustss 		ukbd_decode(sc, ud);
    524  1.62  augustss 	}
    525  1.62  augustss }
    526  1.62  augustss 
    527  1.62  augustss void
    528  1.62  augustss ukbd_delayed_decode(void *addr)
    529  1.62  augustss {
    530  1.62  augustss 	struct ukbd_softc *sc = addr;
    531  1.62  augustss 
    532  1.62  augustss 	ukbd_decode(sc, &sc->sc_data);
    533  1.62  augustss }
    534  1.62  augustss 
    535  1.62  augustss void
    536  1.62  augustss ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud)
    537  1.62  augustss {
    538  1.62  augustss 	int mod, omod;
    539  1.62  augustss 	u_int16_t ibuf[MAXKEYS];	/* chars events */
    540  1.62  augustss 	int s;
    541  1.62  augustss 	int nkeys, i, j;
    542  1.62  augustss 	int key;
    543  1.62  augustss #define ADDKEY(c) ibuf[nkeys++] = (c)
    544  1.62  augustss 
    545  1.62  augustss #ifdef UKBD_DEBUG
    546  1.82  augustss 	/*
    547  1.62  augustss 	 * Keep a trace of the last events.  Using printf changes the
    548  1.62  augustss 	 * timing, so this can be useful sometimes.
    549  1.62  augustss 	 */
    550  1.62  augustss 	if (ukbdtrace) {
    551  1.62  augustss 		struct ukbdtraceinfo *p = &ukbdtracedata[ukbdtraceindex];
    552  1.91   thorpej 		p->unit = device_unit(&sc->sc_hdev.sc_dev);
    553  1.62  augustss 		microtime(&p->tv);
    554  1.62  augustss 		p->ud = *ud;
    555  1.62  augustss 		if (++ukbdtraceindex >= UKBDTRACESIZE)
    556  1.62  augustss 			ukbdtraceindex = 0;
    557  1.62  augustss 	}
    558  1.62  augustss 	if (ukbddebug > 5) {
    559  1.62  augustss 		struct timeval tv;
    560  1.62  augustss 		microtime(&tv);
    561  1.62  augustss 		DPRINTF((" at %lu.%06lu  mod=0x%02x key0=0x%02x key1=0x%02x "
    562  1.62  augustss 			 "key2=0x%02x key3=0x%02x\n",
    563  1.62  augustss 			 tv.tv_sec, tv.tv_usec,
    564  1.62  augustss 			 ud->modifiers, ud->keycode[0], ud->keycode[1],
    565  1.62  augustss 			 ud->keycode[2], ud->keycode[3]));
    566  1.62  augustss 	}
    567  1.62  augustss #endif
    568   1.1  augustss 
    569  1.53  augustss 	if (ud->keycode[0] == KEY_ERROR) {
    570  1.53  augustss 		DPRINTF(("ukbd_intr: KEY_ERROR\n"));
    571   1.1  augustss 		return;		/* ignore  */
    572  1.53  augustss 	}
    573   1.1  augustss 	nkeys = 0;
    574   1.1  augustss 	mod = ud->modifiers;
    575   1.1  augustss 	omod = sc->sc_odata.modifiers;
    576   1.1  augustss 	if (mod != omod)
    577  1.75  augustss 		for (i = 0; i < sc->sc_nmod; i++)
    578  1.82  augustss 			if (( mod & sc->sc_mods[i].mask) !=
    579  1.75  augustss 			    (omod & sc->sc_mods[i].mask))
    580  1.82  augustss 				ADDKEY(sc->sc_mods[i].key |
    581  1.82  augustss 				       (mod & sc->sc_mods[i].mask
    582   1.1  augustss 					  ? PRESS : RELEASE));
    583  1.75  augustss 	if (memcmp(ud->keycode, sc->sc_odata.keycode, sc->sc_nkeycode) != 0) {
    584   1.1  augustss 		/* Check for released keys. */
    585  1.75  augustss 		for (i = 0; i < sc->sc_nkeycode; i++) {
    586   1.1  augustss 			key = sc->sc_odata.keycode[i];
    587   1.1  augustss 			if (key == 0)
    588   1.1  augustss 				continue;
    589  1.75  augustss 			for (j = 0; j < sc->sc_nkeycode; j++)
    590   1.1  augustss 				if (key == ud->keycode[j])
    591   1.1  augustss 					goto rfound;
    592  1.62  augustss 			DPRINTFN(3,("ukbd_intr: relse key=0x%02x\n", key));
    593  1.23  augustss 			ADDKEY(key | RELEASE);
    594   1.1  augustss 		rfound:
    595   1.1  augustss 			;
    596   1.1  augustss 		}
    597  1.82  augustss 
    598   1.1  augustss 		/* Check for pressed keys. */
    599  1.75  augustss 		for (i = 0; i < sc->sc_nkeycode; i++) {
    600   1.1  augustss 			key = ud->keycode[i];
    601   1.1  augustss 			if (key == 0)
    602   1.1  augustss 				continue;
    603  1.75  augustss 			for (j = 0; j < sc->sc_nkeycode; j++)
    604   1.1  augustss 				if (key == sc->sc_odata.keycode[j])
    605   1.1  augustss 					goto pfound;
    606  1.23  augustss 			DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key));
    607  1.23  augustss 			ADDKEY(key | PRESS);
    608   1.1  augustss 		pfound:
    609   1.1  augustss 			;
    610   1.1  augustss 		}
    611   1.1  augustss 	}
    612   1.1  augustss 	sc->sc_odata = *ud;
    613   1.1  augustss 
    614  1.20  augustss 	if (nkeys == 0)
    615  1.20  augustss 		return;
    616  1.20  augustss 
    617   1.3  augustss 	if (sc->sc_polling) {
    618  1.23  augustss 		DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0]));
    619  1.23  augustss 		memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
    620  1.23  augustss 		sc->sc_npollchar = nkeys;
    621   1.3  augustss 		return;
    622   1.3  augustss 	}
    623  1.20  augustss #ifdef WSDISPLAY_COMPAT_RAWKBD
    624  1.19  augustss 	if (sc->sc_rawkbd) {
    625  1.79  augustss 		u_char cbuf[MAXKEYS * 2];
    626  1.24  augustss 		int c;
    627  1.20  augustss 		int npress;
    628  1.20  augustss 
    629  1.23  augustss 		for (npress = i = j = 0; i < nkeys; i++) {
    630  1.23  augustss 			key = ibuf[i];
    631  1.23  augustss 			c = ukbd_trtab[key & CODEMASK];
    632  1.23  augustss 			if (c == NN)
    633  1.23  augustss 				continue;
    634  1.19  augustss 			if (c & 0x80)
    635  1.19  augustss 				cbuf[j++] = 0xe0;
    636  1.19  augustss 			cbuf[j] = c & 0x7f;
    637  1.23  augustss 			if (key & RELEASE)
    638  1.19  augustss 				cbuf[j] |= 0x80;
    639  1.87  augustss #if defined(UKBD_REPEAT)
    640  1.20  augustss 			else {
    641  1.23  augustss 				/* remember pressed keys for autorepeat */
    642  1.20  augustss 				if (c & 0x80)
    643  1.20  augustss 					sc->sc_rep[npress++] = 0xe0;
    644  1.20  augustss 				sc->sc_rep[npress++] = c & 0x7f;
    645  1.20  augustss 			}
    646  1.87  augustss #endif
    647  1.82  augustss 			DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n",
    648  1.27  augustss 				    c & 0x80 ? "0xe0 " : "",
    649  1.27  augustss 				    cbuf[j]));
    650  1.23  augustss 			j++;
    651  1.19  augustss 		}
    652  1.30  augustss 		s = spltty();
    653  1.19  augustss 		wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
    654  1.30  augustss 		splx(s);
    655  1.87  augustss #ifdef UKBD_REPEAT
    656  1.70  augustss 		usb_uncallout(sc->sc_rawrepeat_ch, ukbd_rawrepeat, sc);
    657  1.20  augustss 		if (npress != 0) {
    658  1.20  augustss 			sc->sc_nrep = npress;
    659  1.70  augustss 			usb_callout(sc->sc_rawrepeat_ch,
    660  1.57   thorpej 			    hz * REP_DELAY1 / 1000, ukbd_rawrepeat, sc);
    661  1.20  augustss 		}
    662  1.87  augustss #endif
    663  1.19  augustss 		return;
    664  1.19  augustss 	}
    665  1.19  augustss #endif
    666  1.19  augustss 
    667  1.30  augustss 	s = spltty();
    668   1.2  augustss 	for (i = 0; i < nkeys; i++) {
    669  1.23  augustss 		key = ibuf[i];
    670  1.82  augustss 		wskbd_input(sc->sc_wskbddev,
    671  1.23  augustss 		    key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
    672  1.23  augustss 		    key&CODEMASK);
    673  1.20  augustss 	}
    674  1.30  augustss 	splx(s);
    675   1.1  augustss }
    676   1.1  augustss 
    677   1.2  augustss void
    678  1.60  augustss ukbd_set_leds(void *v, int leds)
    679   1.1  augustss {
    680   1.2  augustss 	struct ukbd_softc *sc = v;
    681   1.2  augustss 	u_int8_t res;
    682   1.1  augustss 
    683  1.71  augustss 	DPRINTF(("ukbd_set_leds: sc=%p leds=%d, sc_leds=%d\n",
    684  1.71  augustss 		 sc, leds, sc->sc_leds));
    685  1.40  augustss 
    686  1.40  augustss 	if (sc->sc_dying)
    687  1.40  augustss 		return;
    688   1.1  augustss 
    689  1.71  augustss 	if (sc->sc_leds == leds)
    690  1.71  augustss 		return;
    691   1.2  augustss 	sc->sc_leds = leds;
    692   1.2  augustss 	res = 0;
    693  1.75  augustss 	/* XXX not really right */
    694  1.75  augustss 	if ((leds & WSKBD_LED_SCROLL) && sc->sc_scroloc.size == 1)
    695  1.75  augustss 		res |= 1 << sc->sc_scroloc.pos;
    696  1.75  augustss 	if ((leds & WSKBD_LED_NUM) && sc->sc_numloc.size == 1)
    697  1.75  augustss 		res |= 1 << sc->sc_numloc.pos;
    698  1.75  augustss 	if ((leds & WSKBD_LED_CAPS) && sc->sc_capsloc.size == 1)
    699  1.75  augustss 		res |= 1 << sc->sc_capsloc.pos;
    700  1.75  augustss 	uhidev_set_report_async(&sc->sc_hdev, UHID_OUTPUT_REPORT, &res, 1);
    701   1.1  augustss }
    702   1.1  augustss 
    703  1.87  augustss #if defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT)
    704  1.20  augustss void
    705  1.60  augustss ukbd_rawrepeat(void *v)
    706  1.20  augustss {
    707  1.20  augustss 	struct ukbd_softc *sc = v;
    708  1.30  augustss 	int s;
    709  1.20  augustss 
    710  1.30  augustss 	s = spltty();
    711  1.20  augustss 	wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
    712  1.30  augustss 	splx(s);
    713  1.70  augustss 	usb_callout(sc->sc_rawrepeat_ch, hz * REP_DELAYN / 1000,
    714  1.57   thorpej 	    ukbd_rawrepeat, sc);
    715  1.20  augustss }
    716  1.87  augustss #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT) */
    717  1.20  augustss 
    718   1.1  augustss int
    719  1.95  christos ukbd_ioctl(void *v, u_long cmd, void *data, int flag,
    720  1.94  christos     struct lwp *l)
    721   1.1  augustss {
    722   1.2  augustss 	struct ukbd_softc *sc = v;
    723   1.1  augustss 
    724   1.2  augustss 	switch (cmd) {
    725   1.2  augustss 	case WSKBDIO_GTYPE:
    726  1.20  augustss 		*(int *)data = WSKBD_TYPE_USB;
    727  1.17  augustss 		return (0);
    728   1.2  augustss 	case WSKBDIO_SETLEDS:
    729   1.2  augustss 		ukbd_set_leds(v, *(int *)data);
    730  1.17  augustss 		return (0);
    731   1.2  augustss 	case WSKBDIO_GETLEDS:
    732   1.2  augustss 		*(int *)data = sc->sc_leds;
    733   1.2  augustss 		return (0);
    734  1.87  augustss #if defined(WSDISPLAY_COMPAT_RAWKBD)
    735   1.2  augustss 	case WSKBDIO_SETMODE:
    736  1.19  augustss 		DPRINTF(("ukbd_ioctl: set raw = %d\n", *(int *)data));
    737   1.2  augustss 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
    738  1.87  augustss #if defined(UKBD_REPEAT)
    739  1.70  augustss 		usb_uncallout(sc->sc_rawrepeat_ch, ukbd_rawrepeat, sc);
    740  1.87  augustss #endif
    741   1.2  augustss 		return (0);
    742   1.2  augustss #endif
    743   1.2  augustss 	}
    744  1.81    atatat 	return (EPASSTHROUGH);
    745   1.1  augustss }
    746   1.7  augustss 
    747  1.71  augustss /*
    748  1.71  augustss  * This is a hack to work around some broken ports that don't call
    749  1.71  augustss  * cnpollc() before cngetc().
    750  1.71  augustss  */
    751  1.71  augustss static int pollenter, warned;
    752  1.71  augustss 
    753   1.7  augustss /* Console interface. */
    754   1.3  augustss void
    755  1.60  augustss ukbd_cngetc(void *v, u_int *type, int *data)
    756   1.3  augustss {
    757   1.3  augustss 	struct ukbd_softc *sc = v;
    758   1.4  augustss 	int c;
    759  1.71  augustss 	int broken;
    760  1.71  augustss 
    761  1.71  augustss 	if (pollenter == 0) {
    762  1.71  augustss 		if (!warned) {
    763  1.71  augustss 			printf("\n"
    764  1.71  augustss "This port is broken, it does not call cnpollc() before calling cngetc().\n"
    765  1.71  augustss "This should be fixed, but it will work anyway (for now).\n");
    766  1.71  augustss 			warned = 1;
    767  1.71  augustss 		}
    768  1.71  augustss 		broken = 1;
    769  1.71  augustss 		ukbd_cnpollc(v, 1);
    770  1.71  augustss 	} else
    771  1.71  augustss 		broken = 0;
    772   1.3  augustss 
    773  1.50  augustss 	DPRINTFN(0,("ukbd_cngetc: enter\n"));
    774   1.3  augustss 	sc->sc_polling = 1;
    775  1.23  augustss 	while(sc->sc_npollchar <= 0)
    776  1.75  augustss 		usbd_dopoll(sc->sc_hdev.sc_parent->sc_iface);
    777   1.3  augustss 	sc->sc_polling = 0;
    778  1.23  augustss 	c = sc->sc_pollchars[0];
    779  1.23  augustss 	sc->sc_npollchar--;
    780  1.82  augustss 	memcpy(sc->sc_pollchars, sc->sc_pollchars+1,
    781  1.23  augustss 	       sc->sc_npollchar * sizeof(u_int16_t));
    782   1.6  augustss 	*type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    783  1.23  augustss 	*data = c & CODEMASK;
    784  1.50  augustss 	DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", c));
    785  1.71  augustss 	if (broken)
    786  1.71  augustss 		ukbd_cnpollc(v, 0);
    787   1.3  augustss }
    788   1.3  augustss 
    789   1.3  augustss void
    790  1.60  augustss ukbd_cnpollc(void *v, int on)
    791   1.3  augustss {
    792   1.6  augustss 	struct ukbd_softc *sc = v;
    793  1.52  augustss 	usbd_device_handle dev;
    794   1.6  augustss 
    795  1.19  augustss 	DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
    796   1.6  augustss 
    797  1.75  augustss 	usbd_interface2device_handle(sc->sc_hdev.sc_parent->sc_iface, &dev);
    798  1.84  augustss 	if (on) {
    799  1.84  augustss 		sc->sc_spl = splusb();
    800  1.84  augustss 		pollenter++;
    801  1.84  augustss 	} else {
    802  1.84  augustss 		splx(sc->sc_spl);
    803  1.84  augustss 		pollenter--;
    804  1.84  augustss 	}
    805  1.52  augustss 	usbd_set_polling(dev, on);
    806   1.3  augustss }
    807  1.18  augustss 
    808  1.18  augustss int
    809  1.60  augustss ukbd_cnattach(void)
    810  1.18  augustss {
    811  1.18  augustss 
    812  1.31   thorpej 	/*
    813  1.31   thorpej 	 * XXX USB requires too many parts of the kernel to be running
    814  1.31   thorpej 	 * XXX in order to work, so we can't do much for the console
    815  1.31   thorpej 	 * XXX keyboard until autconfiguration has run its course.
    816  1.31   thorpej 	 */
    817  1.31   thorpej 	ukbd_is_console = 1;
    818  1.18  augustss 	return (0);
    819  1.75  augustss }
    820  1.75  augustss 
    821  1.75  augustss const char *
    822  1.75  augustss ukbd_parse_desc(struct ukbd_softc *sc)
    823  1.75  augustss {
    824  1.75  augustss 	struct hid_data *d;
    825  1.75  augustss 	struct hid_item h;
    826  1.75  augustss 	int size;
    827  1.75  augustss 	void *desc;
    828  1.75  augustss 	int imod;
    829  1.75  augustss 
    830  1.75  augustss 	uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size);
    831  1.75  augustss 	imod = 0;
    832  1.75  augustss 	sc->sc_nkeycode = 0;
    833  1.75  augustss 	d = hid_start_parse(desc, size, hid_input);
    834  1.75  augustss 	while (hid_get_item(d, &h)) {
    835  1.75  augustss 		/*printf("ukbd: id=%d kind=%d usage=0x%x flags=0x%x pos=%d size=%d cnt=%d\n",
    836  1.75  augustss 		  h.report_ID, h.kind, h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count);*/
    837  1.75  augustss 		if (h.kind != hid_input || (h.flags & HIO_CONST) ||
    838  1.75  augustss 		    HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
    839  1.75  augustss 		    h.report_ID != sc->sc_hdev.sc_report_id)
    840  1.75  augustss 			continue;
    841  1.79  augustss 		DPRINTF(("ukbd: imod=%d usage=0x%x flags=0x%x pos=%d size=%d "
    842  1.79  augustss 			 "cnt=%d\n", imod,
    843  1.79  augustss 			 h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count));
    844  1.75  augustss 		if (h.flags & HIO_VARIABLE) {
    845  1.80  augustss 			if (h.loc.size != 1)
    846  1.80  augustss 				return ("bad modifier size");
    847  1.75  augustss 			/* Single item */
    848  1.75  augustss 			if (imod < MAXMOD) {
    849  1.75  augustss 				sc->sc_modloc[imod] = h.loc;
    850  1.75  augustss 				sc->sc_mods[imod].mask = 1 << imod;
    851  1.75  augustss 				sc->sc_mods[imod].key = HID_GET_USAGE(h.usage);
    852  1.75  augustss 				imod++;
    853  1.75  augustss 			} else
    854  1.75  augustss 				return ("too many modifier keys");
    855  1.75  augustss 		} else {
    856  1.75  augustss 			/* Array */
    857  1.75  augustss 			if (h.loc.size != 8)
    858  1.75  augustss 				return ("key code size != 8");
    859  1.75  augustss 			if (h.loc.count > MAXKEYCODE)
    860  1.75  augustss 				return ("too many key codes");
    861  1.75  augustss 			if (h.loc.pos % 8 != 0)
    862  1.75  augustss 				return ("key codes not on byte boundary");
    863  1.75  augustss 			if (sc->sc_nkeycode != 0)
    864  1.75  augustss 				return ("multiple key code arrays\n");
    865  1.75  augustss 			sc->sc_keycodeloc = h.loc;
    866  1.75  augustss 			sc->sc_nkeycode = h.loc.count;
    867  1.75  augustss 		}
    868  1.75  augustss 	}
    869  1.75  augustss 	sc->sc_nmod = imod;
    870  1.75  augustss 	hid_end_parse(d);
    871  1.75  augustss 
    872  1.75  augustss 	hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_NUM_LOCK),
    873  1.75  augustss 		   sc->sc_hdev.sc_report_id, hid_output, &sc->sc_numloc, NULL);
    874  1.75  augustss 	hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_CAPS_LOCK),
    875  1.75  augustss 		   sc->sc_hdev.sc_report_id, hid_output, &sc->sc_capsloc, NULL);
    876  1.75  augustss 	hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_SCROLL_LOCK),
    877  1.75  augustss 		   sc->sc_hdev.sc_report_id, hid_output, &sc->sc_scroloc, NULL);
    878  1.75  augustss 
    879  1.75  augustss 	return (NULL);
    880  1.18  augustss }
    881