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