1 /* $NetBSD: ukbd.c,v 1.84 2002/10/09 01:02:26 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/cdefs.h> 45 __KERNEL_RCSID(0, "$NetBSD: ukbd.c,v 1.84 2002/10/09 01:02:26 augustss Exp $"); 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/callout.h> 50 #include <sys/kernel.h> 51 #include <sys/device.h> 52 #include <sys/ioctl.h> 53 #include <sys/tty.h> 54 #include <sys/file.h> 55 #include <sys/select.h> 56 #include <sys/proc.h> 57 #include <sys/vnode.h> 58 #include <sys/poll.h> 59 60 #include <dev/usb/usb.h> 61 #include <dev/usb/usbhid.h> 62 63 #include <dev/usb/usbdi.h> 64 #include <dev/usb/usbdi_util.h> 65 #include <dev/usb/usbdevs.h> 66 #include <dev/usb/usb_quirks.h> 67 #include <dev/usb/uhidev.h> 68 #include <dev/usb/hid.h> 69 #include <dev/usb/ukbdvar.h> 70 71 #include <dev/wscons/wsconsio.h> 72 #include <dev/wscons/wskbdvar.h> 73 #include <dev/wscons/wsksymdef.h> 74 #include <dev/wscons/wsksymvar.h> 75 76 #include "opt_wsdisplay_compat.h" 77 #include "opt_ddb.h" 78 79 #ifdef UKBD_DEBUG 80 #define DPRINTF(x) if (ukbddebug) logprintf x 81 #define DPRINTFN(n,x) if (ukbddebug>(n)) logprintf x 82 int ukbddebug = 0; 83 #else 84 #define DPRINTF(x) 85 #define DPRINTFN(n,x) 86 #endif 87 88 #define MAXKEYCODE 6 89 #define MAXMOD 8 /* max 32 */ 90 91 struct ukbd_data { 92 u_int32_t modifiers; 93 u_int8_t keycode[MAXKEYCODE]; 94 }; 95 96 #define PRESS 0x000 97 #define RELEASE 0x100 98 #define CODEMASK 0x0ff 99 100 #if defined(__NetBSD__) && defined(WSDISPLAY_COMPAT_RAWKBD) 101 #define NN 0 /* no translation */ 102 /* 103 * Translate USB keycodes to US keyboard XT scancodes. 104 * Scancodes >= 0x80 represent EXTENDED keycodes. 105 * 106 * See http://www.microsoft.com/HWDEV/TECH/input/Scancode.asp 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, 0xaa, 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 0x5d, 0x5e, 0x5f, NN, NN, NN, NN, 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 usb_callout_t sc_rawrepeat_ch; 177 178 struct device *sc_wskbddev; 179 #if defined(WSDISPLAY_COMPAT_RAWKBD) 180 #define REP_DELAY1 400 181 #define REP_DELAYN 100 182 int sc_rawkbd; 183 int sc_nrep; 184 char sc_rep[MAXKEYS]; 185 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */ 186 187 int sc_spl; 188 int sc_polling; 189 int sc_npollchar; 190 u_int16_t sc_pollchars[MAXKEYS]; 191 #endif /* defined(__NetBSD__) */ 192 193 u_char sc_dying; 194 }; 195 196 #ifdef UKBD_DEBUG 197 #define UKBDTRACESIZE 64 198 struct ukbdtraceinfo { 199 int unit; 200 struct timeval tv; 201 struct ukbd_data ud; 202 }; 203 struct ukbdtraceinfo ukbdtracedata[UKBDTRACESIZE]; 204 int ukbdtraceindex = 0; 205 int ukbdtrace = 0; 206 void ukbdtracedump(void); 207 void 208 ukbdtracedump(void) 209 { 210 int i; 211 for (i = 0; i < UKBDTRACESIZE; i++) { 212 struct ukbdtraceinfo *p = 213 &ukbdtracedata[(i+ukbdtraceindex)%UKBDTRACESIZE]; 214 printf("%lu.%06lu: mod=0x%02x key0=0x%02x key1=0x%02x " 215 "key2=0x%02x key3=0x%02x\n", 216 p->tv.tv_sec, p->tv.tv_usec, 217 p->ud.modifiers, p->ud.keycode[0], p->ud.keycode[1], 218 p->ud.keycode[2], p->ud.keycode[3]); 219 } 220 } 221 #endif 222 223 #define UKBDUNIT(dev) (minor(dev)) 224 #define UKBD_CHUNK 128 /* chunk size for read */ 225 #define UKBD_BSIZE 1020 /* buffer size */ 226 227 Static int ukbd_is_console; 228 229 Static void ukbd_cngetc(void *, u_int *, int *); 230 Static void ukbd_cnpollc(void *, int); 231 232 #if defined(__NetBSD__) 233 const struct wskbd_consops ukbd_consops = { 234 ukbd_cngetc, 235 ukbd_cnpollc, 236 }; 237 #endif 238 239 Static const char *ukbd_parse_desc(struct ukbd_softc *sc); 240 241 Static void ukbd_intr(struct uhidev *addr, void *ibuf, u_int len); 242 Static void ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud); 243 Static void ukbd_delayed_decode(void *addr); 244 245 Static int ukbd_enable(void *, int); 246 Static void ukbd_set_leds(void *, int); 247 248 #if defined(__NetBSD__) 249 Static int ukbd_ioctl(void *, u_long, caddr_t, int, usb_proc_ptr ); 250 #ifdef WSDISPLAY_COMPAT_RAWKBD 251 Static void ukbd_rawrepeat(void *v); 252 #endif 253 254 const struct wskbd_accessops ukbd_accessops = { 255 ukbd_enable, 256 ukbd_set_leds, 257 ukbd_ioctl, 258 }; 259 260 extern const struct wscons_keydesc ukbd_keydesctab[]; 261 262 const struct wskbd_mapdata ukbd_keymapdata = { 263 ukbd_keydesctab, 264 #ifdef UKBD_LAYOUT 265 UKBD_LAYOUT, 266 #else 267 KB_US, 268 #endif 269 }; 270 #endif 271 272 USB_DECLARE_DRIVER(ukbd); 273 274 int 275 ukbd_match(struct device *parent, struct cfdata *match, void *aux) 276 { 277 struct uhidev_attach_arg *uha = aux; 278 int size; 279 void *desc; 280 281 uhidev_get_report_desc(uha->parent, &desc, &size); 282 if (!hid_is_collection(desc, size, uha->reportid, 283 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) 284 return (UMATCH_NONE); 285 286 return (UMATCH_IFACECLASS); 287 } 288 289 void 290 ukbd_attach(struct device *parent, struct device *self, void *aux) 291 { 292 struct ukbd_softc *sc = (struct ukbd_softc *)self; 293 struct uhidev_attach_arg *uha = aux; 294 u_int32_t qflags; 295 const char *parseerr; 296 #if defined(__NetBSD__) 297 struct wskbddev_attach_args a; 298 #else 299 int i; 300 #endif 301 302 sc->sc_hdev.sc_intr = ukbd_intr; 303 sc->sc_hdev.sc_parent = uha->parent; 304 sc->sc_hdev.sc_report_id = uha->reportid; 305 306 parseerr = ukbd_parse_desc(sc); 307 if (parseerr != NULL) { 308 printf("\n%s: attach failed, %s\n", 309 sc->sc_hdev.sc_dev.dv_xname, parseerr); 310 USB_ATTACH_ERROR_RETURN; 311 } 312 313 #ifdef DIAGNOSTIC 314 printf(": %d modifier keys, %d key codes", sc->sc_nmod, 315 sc->sc_nkeycode); 316 #endif 317 printf("\n"); 318 319 320 qflags = usbd_get_quirks(uha->parent->sc_udev)->uq_flags; 321 sc->sc_debounce = (qflags & UQ_SPUR_BUT_UP) != 0; 322 323 /* 324 * Remember if we're the console keyboard. 325 * 326 * XXX This always picks the first keyboard on the 327 * first USB bus, but what else can we really do? 328 */ 329 if ((sc->sc_console_keyboard = ukbd_is_console) != 0) { 330 /* Don't let any other keyboard have it. */ 331 ukbd_is_console = 0; 332 } 333 334 if (sc->sc_console_keyboard) { 335 DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc)); 336 wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata); 337 ukbd_enable(sc, 1); 338 } 339 340 a.console = sc->sc_console_keyboard; 341 342 a.keymap = &ukbd_keymapdata; 343 344 a.accessops = &ukbd_accessops; 345 a.accesscookie = sc; 346 347 usb_callout_init(sc->sc_rawrepeat_ch); 348 349 usb_callout_init(sc->sc_delay); 350 351 /* Flash the leds; no real purpose, just shows we're alive. */ 352 ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS); 353 usbd_delay_ms(uha->parent->sc_udev, 400); 354 ukbd_set_leds(sc, 0); 355 356 sc->sc_wskbddev = config_found(self, &a, wskbddevprint); 357 358 USB_ATTACH_SUCCESS_RETURN; 359 } 360 361 int 362 ukbd_enable(void *v, int on) 363 { 364 struct ukbd_softc *sc = v; 365 366 if (on && sc->sc_dying) 367 return (EIO); 368 369 /* Should only be called to change state */ 370 if (sc->sc_enabled == on) { 371 #ifdef DIAGNOSTIC 372 printf("ukbd_enable: %s: bad call on=%d\n", 373 USBDEVNAME(sc->sc_hdev.sc_dev), on); 374 #endif 375 return (EBUSY); 376 } 377 378 DPRINTF(("ukbd_enable: sc=%p on=%d\n", sc, on)); 379 sc->sc_enabled = on; 380 if (on) { 381 return (uhidev_open(&sc->sc_hdev)); 382 } else { 383 uhidev_close(&sc->sc_hdev); 384 return (0); 385 } 386 } 387 388 int 389 ukbd_activate(device_ptr_t self, enum devact act) 390 { 391 struct ukbd_softc *sc = (struct ukbd_softc *)self; 392 int rv = 0; 393 394 switch (act) { 395 case DVACT_ACTIVATE: 396 return (EOPNOTSUPP); 397 398 case DVACT_DEACTIVATE: 399 if (sc->sc_wskbddev != NULL) 400 rv = config_deactivate(sc->sc_wskbddev); 401 sc->sc_dying = 1; 402 break; 403 } 404 return (rv); 405 } 406 407 int 408 ukbd_detach(struct device *self, int flags) 409 { 410 struct ukbd_softc *sc = (struct ukbd_softc *)self; 411 int rv = 0; 412 413 DPRINTF(("ukbd_detach: sc=%p flags=%d\n", sc, flags)); 414 415 if (sc->sc_console_keyboard) { 416 #if 0 417 /* 418 * XXX Should probably disconnect our consops, 419 * XXX and either notify some other keyboard that 420 * XXX it can now be the console, or if there aren't 421 * XXX any more USB keyboards, set ukbd_is_console 422 * XXX back to 1 so that the next USB keyboard attached 423 * XXX to the system will get it. 424 */ 425 panic("ukbd_detach: console keyboard"); 426 #else 427 /* 428 * Disconnect our consops and set ukbd_is_console 429 * back to 1 so that the next USB keyboard attached 430 * to the system will get it. 431 * XXX Should notify some other keyboard that it can be 432 * XXX console, if there are any other keyboards. 433 */ 434 printf("%s: was console keyboard\n", 435 USBDEVNAME(sc->sc_hdev.sc_dev)); 436 wskbd_cndetach(); 437 ukbd_is_console = 1; 438 #endif 439 } 440 /* No need to do reference counting of ukbd, wskbd has all the goo. */ 441 if (sc->sc_wskbddev != NULL) 442 rv = config_detach(sc->sc_wskbddev, flags); 443 444 /* The console keyboard does not get a disable call, so check pipe. */ 445 if (sc->sc_hdev.sc_state & UHIDEV_OPEN) 446 uhidev_close(&sc->sc_hdev); 447 448 return (rv); 449 } 450 451 void 452 ukbd_intr(struct uhidev *addr, void *ibuf, u_int len) 453 { 454 struct ukbd_softc *sc = (struct ukbd_softc *)addr; 455 struct ukbd_data *ud = &sc->sc_ndata; 456 int i; 457 458 #ifdef UKBD_DEBUG 459 if (ukbddebug > 5) { 460 printf("ukbd_intr: data"); 461 for (i = 0; i < len; i++) 462 printf(" 0x%02x", ((u_char *)ibuf)[i]); 463 printf("\n"); 464 } 465 #endif 466 467 ud->modifiers = 0; 468 for (i = 0; i < sc->sc_nmod; i++) 469 if (hid_get_data(ibuf, &sc->sc_modloc[i])) 470 ud->modifiers |= sc->sc_mods[i].mask; 471 memcpy(ud->keycode, (char *)ibuf + sc->sc_keycodeloc.pos / 8, 472 sc->sc_nkeycode); 473 474 if (sc->sc_debounce && !sc->sc_polling) { 475 /* 476 * Some keyboards have a peculiar quirk. They sometimes 477 * generate a key up followed by a key down for the same 478 * key after about 10 ms. 479 * We avoid this bug by holding off decoding for 20 ms. 480 */ 481 sc->sc_data = *ud; 482 usb_callout(sc->sc_delay, hz / 50, ukbd_delayed_decode, sc); 483 #ifdef DDB 484 } else if (sc->sc_console_keyboard && !sc->sc_polling) { 485 /* 486 * For the console keyboard we can't deliver CTL-ALT-ESC 487 * from the interrupt routine. Doing so would start 488 * polling from inside the interrupt routine and that 489 * loses bigtime. 490 */ 491 sc->sc_data = *ud; 492 usb_callout(sc->sc_delay, 1, ukbd_delayed_decode, sc); 493 #endif 494 } else { 495 ukbd_decode(sc, ud); 496 } 497 } 498 499 void 500 ukbd_delayed_decode(void *addr) 501 { 502 struct ukbd_softc *sc = addr; 503 504 ukbd_decode(sc, &sc->sc_data); 505 } 506 507 void 508 ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud) 509 { 510 int mod, omod; 511 u_int16_t ibuf[MAXKEYS]; /* chars events */ 512 int s; 513 int nkeys, i, j; 514 int key; 515 #define ADDKEY(c) ibuf[nkeys++] = (c) 516 517 #ifdef UKBD_DEBUG 518 /* 519 * Keep a trace of the last events. Using printf changes the 520 * timing, so this can be useful sometimes. 521 */ 522 if (ukbdtrace) { 523 struct ukbdtraceinfo *p = &ukbdtracedata[ukbdtraceindex]; 524 p->unit = sc->sc_hdev.sc_dev.dv_unit; 525 microtime(&p->tv); 526 p->ud = *ud; 527 if (++ukbdtraceindex >= UKBDTRACESIZE) 528 ukbdtraceindex = 0; 529 } 530 if (ukbddebug > 5) { 531 struct timeval tv; 532 microtime(&tv); 533 DPRINTF((" at %lu.%06lu mod=0x%02x key0=0x%02x key1=0x%02x " 534 "key2=0x%02x key3=0x%02x\n", 535 tv.tv_sec, tv.tv_usec, 536 ud->modifiers, ud->keycode[0], ud->keycode[1], 537 ud->keycode[2], ud->keycode[3])); 538 } 539 #endif 540 541 if (ud->keycode[0] == KEY_ERROR) { 542 DPRINTF(("ukbd_intr: KEY_ERROR\n")); 543 return; /* ignore */ 544 } 545 nkeys = 0; 546 mod = ud->modifiers; 547 omod = sc->sc_odata.modifiers; 548 if (mod != omod) 549 for (i = 0; i < sc->sc_nmod; i++) 550 if (( mod & sc->sc_mods[i].mask) != 551 (omod & sc->sc_mods[i].mask)) 552 ADDKEY(sc->sc_mods[i].key | 553 (mod & sc->sc_mods[i].mask 554 ? PRESS : RELEASE)); 555 if (memcmp(ud->keycode, sc->sc_odata.keycode, sc->sc_nkeycode) != 0) { 556 /* Check for released keys. */ 557 for (i = 0; i < sc->sc_nkeycode; i++) { 558 key = sc->sc_odata.keycode[i]; 559 if (key == 0) 560 continue; 561 for (j = 0; j < sc->sc_nkeycode; j++) 562 if (key == ud->keycode[j]) 563 goto rfound; 564 DPRINTFN(3,("ukbd_intr: relse key=0x%02x\n", key)); 565 ADDKEY(key | RELEASE); 566 rfound: 567 ; 568 } 569 570 /* Check for pressed keys. */ 571 for (i = 0; i < sc->sc_nkeycode; i++) { 572 key = ud->keycode[i]; 573 if (key == 0) 574 continue; 575 for (j = 0; j < sc->sc_nkeycode; j++) 576 if (key == sc->sc_odata.keycode[j]) 577 goto pfound; 578 DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key)); 579 ADDKEY(key | PRESS); 580 pfound: 581 ; 582 } 583 } 584 sc->sc_odata = *ud; 585 586 if (nkeys == 0) 587 return; 588 589 if (sc->sc_polling) { 590 DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0])); 591 memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t)); 592 sc->sc_npollchar = nkeys; 593 return; 594 } 595 #ifdef WSDISPLAY_COMPAT_RAWKBD 596 if (sc->sc_rawkbd) { 597 u_char cbuf[MAXKEYS * 2]; 598 int c; 599 int npress; 600 601 for (npress = i = j = 0; i < nkeys; i++) { 602 key = ibuf[i]; 603 c = ukbd_trtab[key & CODEMASK]; 604 if (c == NN) 605 continue; 606 if (c & 0x80) 607 cbuf[j++] = 0xe0; 608 cbuf[j] = c & 0x7f; 609 if (key & RELEASE) 610 cbuf[j] |= 0x80; 611 else { 612 /* remember pressed keys for autorepeat */ 613 if (c & 0x80) 614 sc->sc_rep[npress++] = 0xe0; 615 sc->sc_rep[npress++] = c & 0x7f; 616 } 617 DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n", 618 c & 0x80 ? "0xe0 " : "", 619 cbuf[j])); 620 j++; 621 } 622 s = spltty(); 623 wskbd_rawinput(sc->sc_wskbddev, cbuf, j); 624 splx(s); 625 usb_uncallout(sc->sc_rawrepeat_ch, ukbd_rawrepeat, sc); 626 if (npress != 0) { 627 sc->sc_nrep = npress; 628 usb_callout(sc->sc_rawrepeat_ch, 629 hz * REP_DELAY1 / 1000, ukbd_rawrepeat, sc); 630 } 631 return; 632 } 633 #endif 634 635 s = spltty(); 636 for (i = 0; i < nkeys; i++) { 637 key = ibuf[i]; 638 wskbd_input(sc->sc_wskbddev, 639 key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN, 640 key&CODEMASK); 641 } 642 splx(s); 643 } 644 645 void 646 ukbd_set_leds(void *v, int leds) 647 { 648 struct ukbd_softc *sc = v; 649 u_int8_t res; 650 651 DPRINTF(("ukbd_set_leds: sc=%p leds=%d, sc_leds=%d\n", 652 sc, leds, sc->sc_leds)); 653 654 if (sc->sc_dying) 655 return; 656 657 if (sc->sc_leds == leds) 658 return; 659 sc->sc_leds = leds; 660 res = 0; 661 /* XXX not really right */ 662 if ((leds & WSKBD_LED_SCROLL) && sc->sc_scroloc.size == 1) 663 res |= 1 << sc->sc_scroloc.pos; 664 if ((leds & WSKBD_LED_NUM) && sc->sc_numloc.size == 1) 665 res |= 1 << sc->sc_numloc.pos; 666 if ((leds & WSKBD_LED_CAPS) && sc->sc_capsloc.size == 1) 667 res |= 1 << sc->sc_capsloc.pos; 668 uhidev_set_report_async(&sc->sc_hdev, UHID_OUTPUT_REPORT, &res, 1); 669 } 670 671 #ifdef WSDISPLAY_COMPAT_RAWKBD 672 void 673 ukbd_rawrepeat(void *v) 674 { 675 struct ukbd_softc *sc = v; 676 int s; 677 678 s = spltty(); 679 wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep); 680 splx(s); 681 usb_callout(sc->sc_rawrepeat_ch, hz * REP_DELAYN / 1000, 682 ukbd_rawrepeat, sc); 683 } 684 #endif 685 686 int 687 ukbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, usb_proc_ptr p) 688 { 689 struct ukbd_softc *sc = v; 690 691 switch (cmd) { 692 case WSKBDIO_GTYPE: 693 *(int *)data = WSKBD_TYPE_USB; 694 return (0); 695 case WSKBDIO_SETLEDS: 696 ukbd_set_leds(v, *(int *)data); 697 return (0); 698 case WSKBDIO_GETLEDS: 699 *(int *)data = sc->sc_leds; 700 return (0); 701 #ifdef WSDISPLAY_COMPAT_RAWKBD 702 case WSKBDIO_SETMODE: 703 DPRINTF(("ukbd_ioctl: set raw = %d\n", *(int *)data)); 704 sc->sc_rawkbd = *(int *)data == WSKBD_RAW; 705 usb_uncallout(sc->sc_rawrepeat_ch, ukbd_rawrepeat, sc); 706 return (0); 707 #endif 708 } 709 return (EPASSTHROUGH); 710 } 711 712 /* 713 * This is a hack to work around some broken ports that don't call 714 * cnpollc() before cngetc(). 715 */ 716 static int pollenter, warned; 717 718 /* Console interface. */ 719 void 720 ukbd_cngetc(void *v, u_int *type, int *data) 721 { 722 struct ukbd_softc *sc = v; 723 int c; 724 int broken; 725 726 if (pollenter == 0) { 727 if (!warned) { 728 printf("\n" 729 "This port is broken, it does not call cnpollc() before calling cngetc().\n" 730 "This should be fixed, but it will work anyway (for now).\n"); 731 warned = 1; 732 } 733 broken = 1; 734 ukbd_cnpollc(v, 1); 735 } else 736 broken = 0; 737 738 DPRINTFN(0,("ukbd_cngetc: enter\n")); 739 sc->sc_polling = 1; 740 while(sc->sc_npollchar <= 0) 741 usbd_dopoll(sc->sc_hdev.sc_parent->sc_iface); 742 sc->sc_polling = 0; 743 c = sc->sc_pollchars[0]; 744 sc->sc_npollchar--; 745 memcpy(sc->sc_pollchars, sc->sc_pollchars+1, 746 sc->sc_npollchar * sizeof(u_int16_t)); 747 *type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN; 748 *data = c & CODEMASK; 749 DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", c)); 750 if (broken) 751 ukbd_cnpollc(v, 0); 752 } 753 754 void 755 ukbd_cnpollc(void *v, int on) 756 { 757 struct ukbd_softc *sc = v; 758 usbd_device_handle dev; 759 760 DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on)); 761 762 usbd_interface2device_handle(sc->sc_hdev.sc_parent->sc_iface, &dev); 763 if (on) { 764 sc->sc_spl = splusb(); 765 pollenter++; 766 } else { 767 splx(sc->sc_spl); 768 pollenter--; 769 } 770 usbd_set_polling(dev, on); 771 } 772 773 int 774 ukbd_cnattach(void) 775 { 776 777 /* 778 * XXX USB requires too many parts of the kernel to be running 779 * XXX in order to work, so we can't do much for the console 780 * XXX keyboard until autconfiguration has run its course. 781 */ 782 ukbd_is_console = 1; 783 return (0); 784 } 785 786 const char * 787 ukbd_parse_desc(struct ukbd_softc *sc) 788 { 789 struct hid_data *d; 790 struct hid_item h; 791 int size; 792 void *desc; 793 int imod; 794 795 uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size); 796 imod = 0; 797 sc->sc_nkeycode = 0; 798 d = hid_start_parse(desc, size, hid_input); 799 while (hid_get_item(d, &h)) { 800 /*printf("ukbd: id=%d kind=%d usage=0x%x flags=0x%x pos=%d size=%d cnt=%d\n", 801 h.report_ID, h.kind, h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count);*/ 802 if (h.kind != hid_input || (h.flags & HIO_CONST) || 803 HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD || 804 h.report_ID != sc->sc_hdev.sc_report_id) 805 continue; 806 DPRINTF(("ukbd: imod=%d usage=0x%x flags=0x%x pos=%d size=%d " 807 "cnt=%d\n", imod, 808 h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count)); 809 if (h.flags & HIO_VARIABLE) { 810 if (h.loc.size != 1) 811 return ("bad modifier size"); 812 /* Single item */ 813 if (imod < MAXMOD) { 814 sc->sc_modloc[imod] = h.loc; 815 sc->sc_mods[imod].mask = 1 << imod; 816 sc->sc_mods[imod].key = HID_GET_USAGE(h.usage); 817 imod++; 818 } else 819 return ("too many modifier keys"); 820 } else { 821 /* Array */ 822 if (h.loc.size != 8) 823 return ("key code size != 8"); 824 if (h.loc.count > MAXKEYCODE) 825 return ("too many key codes"); 826 if (h.loc.pos % 8 != 0) 827 return ("key codes not on byte boundary"); 828 if (sc->sc_nkeycode != 0) 829 return ("multiple key code arrays\n"); 830 sc->sc_keycodeloc = h.loc; 831 sc->sc_nkeycode = h.loc.count; 832 } 833 } 834 sc->sc_nmod = imod; 835 hid_end_parse(d); 836 837 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_NUM_LOCK), 838 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_numloc, NULL); 839 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_CAPS_LOCK), 840 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_capsloc, NULL); 841 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_SCROLL_LOCK), 842 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_scroloc, NULL); 843 844 return (NULL); 845 } 846