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