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