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