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