1 #include <sys/cdefs.h> 2 __FBSDID("$FreeBSD$"); 3 4 5 /*- 6 * Copyright (c) 1998 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Lennart Augustsson (lennart@augustsson.net) at 11 * Carlstedt Research & Technology. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the NetBSD 24 * Foundation, Inc. and its contributors. 25 * 4. Neither the name of The NetBSD Foundation nor the names of its 26 * contributors may be used to endorse or promote products derived 27 * from this software without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39 * POSSIBILITY OF SUCH DAMAGE. 40 * 41 */ 42 43 /* 44 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf 45 */ 46 47 #include "opt_compat.h" 48 #include "opt_kbd.h" 49 #include "opt_ukbd.h" 50 51 #include <sys/stdint.h> 52 #include <sys/stddef.h> 53 #include <sys/param.h> 54 #include <sys/queue.h> 55 #include <sys/types.h> 56 #include <sys/systm.h> 57 #include <sys/kernel.h> 58 #include <sys/bus.h> 59 #include <sys/linker_set.h> 60 #include <sys/module.h> 61 #include <sys/lock.h> 62 #include <sys/mutex.h> 63 #include <sys/condvar.h> 64 #include <sys/sysctl.h> 65 #include <sys/sx.h> 66 #include <sys/unistd.h> 67 #include <sys/callout.h> 68 #include <sys/malloc.h> 69 #include <sys/priv.h> 70 71 #include <dev/usb/usb.h> 72 #include <dev/usb/usbdi.h> 73 #include <dev/usb/usbdi_util.h> 74 #include <dev/usb/usbhid.h> 75 76 #define USB_DEBUG_VAR ukbd_debug 77 #include <dev/usb/usb_debug.h> 78 79 #include <dev/usb/quirk/usb_quirk.h> 80 81 #include <sys/ioccom.h> 82 #include <sys/filio.h> 83 #include <sys/tty.h> 84 #include <sys/kbio.h> 85 86 #include <dev/kbd/kbdreg.h> 87 88 /* the initial key map, accent map and fkey strings */ 89 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE) 90 #define KBD_DFLT_KEYMAP 91 #include "ukbdmap.h" 92 #endif 93 94 /* the following file must be included after "ukbdmap.h" */ 95 #include <dev/kbd/kbdtables.h> 96 97 #if USB_DEBUG 98 static int ukbd_debug = 0; 99 100 SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd"); 101 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW, 102 &ukbd_debug, 0, "Debug level"); 103 #endif 104 105 #define UPROTO_BOOT_KEYBOARD 1 106 107 #define UKBD_EMULATE_ATSCANCODE 1 108 #define UKBD_DRIVER_NAME "ukbd" 109 #define UKBD_NMOD 8 /* units */ 110 #define UKBD_NKEYCODE 6 /* units */ 111 #define UKBD_IN_BUF_SIZE (2*(UKBD_NMOD + (2*UKBD_NKEYCODE))) /* bytes */ 112 #define UKBD_IN_BUF_FULL (UKBD_IN_BUF_SIZE / 2) /* bytes */ 113 #define UKBD_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */ 114 115 struct ukbd_data { 116 uint8_t modifiers; 117 #define MOD_CONTROL_L 0x01 118 #define MOD_CONTROL_R 0x10 119 #define MOD_SHIFT_L 0x02 120 #define MOD_SHIFT_R 0x20 121 #define MOD_ALT_L 0x04 122 #define MOD_ALT_R 0x40 123 #define MOD_WIN_L 0x08 124 #define MOD_WIN_R 0x80 125 uint8_t reserved; 126 uint8_t keycode[UKBD_NKEYCODE]; 127 uint8_t exten[8]; 128 }; 129 130 enum { 131 UKBD_INTR_DT, 132 UKBD_CTRL_LED, 133 UKBD_N_TRANSFER, 134 }; 135 136 struct ukbd_softc { 137 keyboard_t sc_kbd; 138 keymap_t sc_keymap; 139 accentmap_t sc_accmap; 140 fkeytab_t sc_fkeymap[UKBD_NFKEY]; 141 struct hid_location sc_loc_apple_eject; 142 struct hid_location sc_loc_apple_fn; 143 struct usb_callout sc_callout; 144 struct ukbd_data sc_ndata; 145 struct ukbd_data sc_odata; 146 147 struct usb_device *sc_udev; 148 struct usb_interface *sc_iface; 149 struct usb_xfer *sc_xfer[UKBD_N_TRANSFER]; 150 151 uint32_t sc_ntime[UKBD_NKEYCODE]; 152 uint32_t sc_otime[UKBD_NKEYCODE]; 153 uint32_t sc_input[UKBD_IN_BUF_SIZE]; /* input buffer */ 154 uint32_t sc_time_ms; 155 uint32_t sc_composed_char; /* composed char code, if non-zero */ 156 #ifdef UKBD_EMULATE_ATSCANCODE 157 uint32_t sc_buffered_char[2]; 158 #endif 159 uint32_t sc_flags; /* flags */ 160 #define UKBD_FLAG_COMPOSE 0x0001 161 #define UKBD_FLAG_POLLING 0x0002 162 #define UKBD_FLAG_SET_LEDS 0x0004 163 #define UKBD_FLAG_ATTACHED 0x0010 164 #define UKBD_FLAG_GONE 0x0020 165 #define UKBD_FLAG_APPLE_EJECT 0x0040 166 #define UKBD_FLAG_APPLE_FN 0x0080 167 #define UKBD_FLAG_APPLE_SWAP 0x0100 168 169 int32_t sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */ 170 int32_t sc_state; /* shift/lock key state */ 171 int32_t sc_accents; /* accent key index (> 0) */ 172 173 uint16_t sc_inputs; 174 uint16_t sc_inputhead; 175 uint16_t sc_inputtail; 176 177 uint8_t sc_leds; /* store for async led requests */ 178 uint8_t sc_iface_index; 179 uint8_t sc_iface_no; 180 uint8_t sc_kbd_id; 181 uint8_t sc_led_id; 182 }; 183 184 #define KEY_ERROR 0x01 185 186 #define KEY_PRESS 0 187 #define KEY_RELEASE 0x400 188 #define KEY_INDEX(c) ((c) & 0xFF) 189 190 #define SCAN_PRESS 0 191 #define SCAN_RELEASE 0x80 192 #define SCAN_PREFIX_E0 0x100 193 #define SCAN_PREFIX_E1 0x200 194 #define SCAN_PREFIX_CTL 0x400 195 #define SCAN_PREFIX_SHIFT 0x800 196 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \ 197 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT) 198 #define SCAN_CHAR(c) ((c) & 0x7f) 199 200 struct ukbd_mods { 201 uint32_t mask, key; 202 }; 203 204 static const struct ukbd_mods ukbd_mods[UKBD_NMOD] = { 205 {MOD_CONTROL_L, 0xe0}, 206 {MOD_CONTROL_R, 0xe4}, 207 {MOD_SHIFT_L, 0xe1}, 208 {MOD_SHIFT_R, 0xe5}, 209 {MOD_ALT_L, 0xe2}, 210 {MOD_ALT_R, 0xe6}, 211 {MOD_WIN_L, 0xe3}, 212 {MOD_WIN_R, 0xe7}, 213 }; 214 215 #define NN 0 /* no translation */ 216 /* 217 * Translate USB keycodes to AT keyboard scancodes. 218 */ 219 /* 220 * FIXME: Mac USB keyboard generates: 221 * 0x53: keypad NumLock/Clear 222 * 0x66: Power 223 * 0x67: keypad = 224 * 0x68: F13 225 * 0x69: F14 226 * 0x6a: F15 227 */ 228 static const uint8_t ukbd_trtab[256] = { 229 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */ 230 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */ 231 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */ 232 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */ 233 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */ 234 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */ 235 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */ 236 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */ 237 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */ 238 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */ 239 97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */ 240 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */ 241 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */ 242 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */ 243 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */ 244 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */ 245 121, 120, NN, NN, NN, NN, NN, 115, /* 80 - 87 */ 246 112, 125, 121, 123, NN, NN, NN, NN, /* 88 - 8F */ 247 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */ 248 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */ 249 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */ 250 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */ 251 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */ 252 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */ 253 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */ 254 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */ 255 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */ 256 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */ 257 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */ 258 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */ 259 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */ 260 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */ 261 }; 262 263 /* prototypes */ 264 static void ukbd_timeout(void *); 265 static void ukbd_set_leds(struct ukbd_softc *, uint8_t); 266 static int ukbd_set_typematic(keyboard_t *, int); 267 #ifdef UKBD_EMULATE_ATSCANCODE 268 static int ukbd_key2scan(struct ukbd_softc *, int, int, int); 269 #endif 270 static uint32_t ukbd_read_char(keyboard_t *, int); 271 static void ukbd_clear_state(keyboard_t *); 272 static int ukbd_ioctl(keyboard_t *, u_long, caddr_t); 273 static int ukbd_enable(keyboard_t *); 274 static int ukbd_disable(keyboard_t *); 275 static void ukbd_interrupt(struct ukbd_softc *); 276 277 static device_probe_t ukbd_probe; 278 static device_attach_t ukbd_attach; 279 static device_detach_t ukbd_detach; 280 static device_resume_t ukbd_resume; 281 282 static void 283 ukbd_put_key(struct ukbd_softc *sc, uint32_t key) 284 { 285 mtx_assert(&Giant, MA_OWNED); 286 287 DPRINTF("0x%02x (%d) %s\n", key, key, 288 (key & KEY_RELEASE) ? "released" : "pressed"); 289 290 if (sc->sc_inputs < UKBD_IN_BUF_SIZE) { 291 sc->sc_input[sc->sc_inputtail] = key; 292 ++(sc->sc_inputs); 293 ++(sc->sc_inputtail); 294 if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) { 295 sc->sc_inputtail = 0; 296 } 297 } else { 298 DPRINTF("input buffer is full\n"); 299 } 300 } 301 302 static void 303 ukbd_do_poll(struct ukbd_softc *sc, uint8_t wait) 304 { 305 DPRINTFN(2, "polling\n"); 306 307 while (sc->sc_inputs == 0) { 308 309 usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER); 310 311 DELAY(1000); /* delay 1 ms */ 312 313 sc->sc_time_ms++; 314 315 /* support repetition of keys: */ 316 317 ukbd_interrupt(sc); 318 319 if (!wait) 320 break; 321 } 322 } 323 324 static int32_t 325 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait) 326 { 327 int32_t c; 328 329 mtx_assert(&Giant, MA_OWNED); 330 331 if (sc->sc_inputs == 0) { 332 /* start transfer, if not already started */ 333 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]); 334 } 335 if (sc->sc_flags & UKBD_FLAG_POLLING) { 336 ukbd_do_poll(sc, wait); 337 } 338 if (sc->sc_inputs == 0) { 339 c = -1; 340 } else { 341 c = sc->sc_input[sc->sc_inputhead]; 342 --(sc->sc_inputs); 343 ++(sc->sc_inputhead); 344 if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) { 345 sc->sc_inputhead = 0; 346 } 347 } 348 return (c); 349 } 350 351 static void 352 ukbd_interrupt(struct ukbd_softc *sc) 353 { 354 uint32_t n_mod; 355 uint32_t o_mod; 356 uint32_t now = sc->sc_time_ms; 357 uint32_t dtime; 358 uint32_t c; 359 uint8_t key; 360 uint8_t i; 361 uint8_t j; 362 363 if (sc->sc_ndata.keycode[0] == KEY_ERROR) { 364 goto done; 365 } 366 n_mod = sc->sc_ndata.modifiers; 367 o_mod = sc->sc_odata.modifiers; 368 if (n_mod != o_mod) { 369 for (i = 0; i < UKBD_NMOD; i++) { 370 if ((n_mod & ukbd_mods[i].mask) != 371 (o_mod & ukbd_mods[i].mask)) { 372 ukbd_put_key(sc, ukbd_mods[i].key | 373 ((n_mod & ukbd_mods[i].mask) ? 374 KEY_PRESS : KEY_RELEASE)); 375 } 376 } 377 } 378 /* Check for released keys. */ 379 for (i = 0; i < UKBD_NKEYCODE; i++) { 380 key = sc->sc_odata.keycode[i]; 381 if (key == 0) { 382 continue; 383 } 384 for (j = 0; j < UKBD_NKEYCODE; j++) { 385 if (sc->sc_ndata.keycode[j] == 0) { 386 continue; 387 } 388 if (key == sc->sc_ndata.keycode[j]) { 389 goto rfound; 390 } 391 } 392 ukbd_put_key(sc, key | KEY_RELEASE); 393 rfound: ; 394 } 395 396 /* Check for pressed keys. */ 397 for (i = 0; i < UKBD_NKEYCODE; i++) { 398 key = sc->sc_ndata.keycode[i]; 399 if (key == 0) { 400 continue; 401 } 402 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1; 403 for (j = 0; j < UKBD_NKEYCODE; j++) { 404 if (sc->sc_odata.keycode[j] == 0) { 405 continue; 406 } 407 if (key == sc->sc_odata.keycode[j]) { 408 409 /* key is still pressed */ 410 411 sc->sc_ntime[i] = sc->sc_otime[j]; 412 dtime = (sc->sc_otime[j] - now); 413 414 if (!(dtime & 0x80000000)) { 415 /* time has not elapsed */ 416 goto pfound; 417 } 418 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2; 419 break; 420 } 421 } 422 ukbd_put_key(sc, key | KEY_PRESS); 423 424 /* 425 * If any other key is presently down, force its repeat to be 426 * well in the future (100s). This makes the last key to be 427 * pressed do the autorepeat. 428 */ 429 for (j = 0; j != UKBD_NKEYCODE; j++) { 430 if (j != i) 431 sc->sc_ntime[j] = now + (100 * 1000); 432 } 433 pfound: ; 434 } 435 436 sc->sc_odata = sc->sc_ndata; 437 438 bcopy(sc->sc_ntime, sc->sc_otime, sizeof(sc->sc_otime)); 439 440 if (sc->sc_inputs == 0) { 441 goto done; 442 } 443 if (sc->sc_flags & UKBD_FLAG_POLLING) { 444 goto done; 445 } 446 if (KBD_IS_ACTIVE(&sc->sc_kbd) && 447 KBD_IS_BUSY(&sc->sc_kbd)) { 448 /* let the callback function process the input */ 449 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT, 450 sc->sc_kbd.kb_callback.kc_arg); 451 } else { 452 /* read and discard the input, no one is waiting for it */ 453 do { 454 c = ukbd_read_char(&sc->sc_kbd, 0); 455 } while (c != NOKEY); 456 } 457 done: 458 return; 459 } 460 461 static void 462 ukbd_timeout(void *arg) 463 { 464 struct ukbd_softc *sc = arg; 465 466 mtx_assert(&Giant, MA_OWNED); 467 468 if (!(sc->sc_flags & UKBD_FLAG_POLLING)) { 469 sc->sc_time_ms += 25; /* milliseconds */ 470 } 471 ukbd_interrupt(sc); 472 473 usb_callout_reset(&sc->sc_callout, hz / 40, &ukbd_timeout, sc); 474 } 475 476 static uint8_t 477 ukbd_apple_fn(uint8_t keycode) { 478 switch (keycode) { 479 case 0x28: return 0x49; /* RETURN -> INSERT */ 480 case 0x2a: return 0x4c; /* BACKSPACE -> DEL */ 481 case 0x50: return 0x4a; /* LEFT ARROW -> HOME */ 482 case 0x4f: return 0x4d; /* RIGHT ARROW -> END */ 483 case 0x52: return 0x4b; /* UP ARROW -> PGUP */ 484 case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */ 485 default: return keycode; 486 } 487 } 488 489 static uint8_t 490 ukbd_apple_swap(uint8_t keycode) { 491 switch (keycode) { 492 case 0x35: return 0x64; 493 case 0x64: return 0x35; 494 default: return keycode; 495 } 496 } 497 498 static void 499 ukbd_intr_callback(struct usb_xfer *xfer, usb_error_t error) 500 { 501 struct ukbd_softc *sc = usbd_xfer_softc(xfer); 502 struct usb_page_cache *pc; 503 uint8_t i; 504 uint8_t offset; 505 uint8_t id; 506 uint8_t apple_fn; 507 uint8_t apple_eject; 508 int len; 509 510 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 511 pc = usbd_xfer_get_frame(xfer, 0); 512 513 switch (USB_GET_STATE(xfer)) { 514 case USB_ST_TRANSFERRED: 515 DPRINTF("actlen=%d bytes\n", len); 516 517 if (len == 0) { 518 DPRINTF("zero length data\n"); 519 goto tr_setup; 520 } 521 522 if (sc->sc_kbd_id != 0) { 523 /* check and remove HID ID byte */ 524 usbd_copy_out(pc, 0, &id, 1); 525 if (id != sc->sc_kbd_id) { 526 DPRINTF("wrong HID ID\n"); 527 goto tr_setup; 528 } 529 offset = 1; 530 len--; 531 } else { 532 offset = 0; 533 } 534 535 if (len > sizeof(sc->sc_ndata)) { 536 len = sizeof(sc->sc_ndata); 537 } 538 539 if (len) { 540 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata)); 541 usbd_copy_out(pc, offset, &sc->sc_ndata, len); 542 543 if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) && 544 hid_get_data((uint8_t *)&sc->sc_ndata, 545 len, &sc->sc_loc_apple_eject)) 546 apple_eject = 1; 547 else 548 apple_eject = 0; 549 550 if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) && 551 hid_get_data((uint8_t *)&sc->sc_ndata, 552 len, &sc->sc_loc_apple_fn)) 553 apple_fn = 1; 554 else 555 apple_fn = 0; 556 #if USB_DEBUG 557 DPRINTF("apple_eject=%u apple_fn=%u\n", 558 apple_eject, apple_fn); 559 560 if (sc->sc_ndata.modifiers) { 561 DPRINTF("mod: 0x%04x\n", sc->sc_ndata.modifiers); 562 } 563 for (i = 0; i < UKBD_NKEYCODE; i++) { 564 if (sc->sc_ndata.keycode[i]) { 565 DPRINTF("[%d] = %d\n", i, sc->sc_ndata.keycode[i]); 566 } 567 } 568 #endif /* USB_DEBUG */ 569 570 if (apple_fn) { 571 for (i = 0; i < UKBD_NKEYCODE; i++) { 572 sc->sc_ndata.keycode[i] = 573 ukbd_apple_fn(sc->sc_ndata.keycode[i]); 574 } 575 } 576 577 if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) { 578 for (i = 0; i < UKBD_NKEYCODE; i++) { 579 sc->sc_ndata.keycode[i] = 580 ukbd_apple_swap(sc->sc_ndata.keycode[i]); 581 } 582 } 583 584 ukbd_interrupt(sc); 585 } 586 case USB_ST_SETUP: 587 tr_setup: 588 if (sc->sc_inputs < UKBD_IN_BUF_FULL) { 589 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 590 usbd_transfer_submit(xfer); 591 } else { 592 DPRINTF("input queue is full!\n"); 593 } 594 break; 595 596 default: /* Error */ 597 DPRINTF("error=%s\n", usbd_errstr(error)); 598 599 if (error != USB_ERR_CANCELLED) { 600 /* try to clear stall first */ 601 usbd_xfer_set_stall(xfer); 602 goto tr_setup; 603 } 604 break; 605 } 606 } 607 608 static void 609 ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error) 610 { 611 struct usb_device_request req; 612 struct usb_page_cache *pc; 613 uint8_t buf[2]; 614 struct ukbd_softc *sc = usbd_xfer_softc(xfer); 615 616 switch (USB_GET_STATE(xfer)) { 617 case USB_ST_TRANSFERRED: 618 case USB_ST_SETUP: 619 if (sc->sc_flags & UKBD_FLAG_SET_LEDS) { 620 sc->sc_flags &= ~UKBD_FLAG_SET_LEDS; 621 622 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 623 req.bRequest = UR_SET_REPORT; 624 USETW2(req.wValue, UHID_OUTPUT_REPORT, 0); 625 req.wIndex[0] = sc->sc_iface_no; 626 req.wIndex[1] = 0; 627 req.wLength[1] = 0; 628 629 /* check if we need to prefix an ID byte */ 630 if (sc->sc_led_id != 0) { 631 req.wLength[0] = 2; 632 buf[0] = sc->sc_led_id; 633 buf[1] = sc->sc_leds; 634 } else { 635 req.wLength[0] = 1; 636 buf[0] = sc->sc_leds; 637 buf[1] = 0; 638 } 639 640 pc = usbd_xfer_get_frame(xfer, 0); 641 usbd_copy_in(pc, 0, &req, sizeof(req)); 642 pc = usbd_xfer_get_frame(xfer, 1); 643 usbd_copy_in(pc, 0, buf, sizeof(buf)); 644 645 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 646 usbd_xfer_set_frame_len(xfer, 1, req.wLength[0]); 647 usbd_xfer_set_frames(xfer, 2); 648 usbd_transfer_submit(xfer); 649 } 650 return; 651 652 default: /* Error */ 653 DPRINTFN(0, "error=%s\n", usbd_errstr(error)); 654 return; 655 } 656 } 657 658 static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = { 659 660 [UKBD_INTR_DT] = { 661 .type = UE_INTERRUPT, 662 .endpoint = UE_ADDR_ANY, 663 .direction = UE_DIR_IN, 664 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 665 .bufsize = 0, /* use wMaxPacketSize */ 666 .callback = &ukbd_intr_callback, 667 }, 668 669 [UKBD_CTRL_LED] = { 670 .type = UE_CONTROL, 671 .endpoint = 0x00, /* Control pipe */ 672 .direction = UE_DIR_ANY, 673 .bufsize = sizeof(struct usb_device_request) + 8, 674 .callback = &ukbd_set_leds_callback, 675 .timeout = 1000, /* 1 second */ 676 }, 677 }; 678 679 static int 680 ukbd_probe(device_t dev) 681 { 682 keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME); 683 struct usb_attach_arg *uaa = device_get_ivars(dev); 684 void *d_ptr; 685 int error; 686 uint16_t d_len; 687 688 DPRINTFN(11, "\n"); 689 690 if (sw == NULL) { 691 return (ENXIO); 692 } 693 if (uaa->usb_mode != USB_MODE_HOST) { 694 return (ENXIO); 695 } 696 697 if (uaa->info.bInterfaceClass != UICLASS_HID) 698 return (ENXIO); 699 700 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) && 701 (uaa->info.bInterfaceProtocol == UPROTO_BOOT_KEYBOARD)) { 702 if (usb_test_quirk(uaa, UQ_KBD_IGNORE)) 703 return (ENXIO); 704 else 705 return (0); 706 } 707 708 error = usbd_req_get_hid_desc(uaa->device, NULL, 709 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex); 710 711 if (error) 712 return (ENXIO); 713 714 /* 715 * NOTE: we currently don't support USB mouse and USB keyboard 716 * on the same USB endpoint. 717 */ 718 if (hid_is_collection(d_ptr, d_len, 719 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) { 720 /* most likely a mouse */ 721 error = ENXIO; 722 } else if (hid_is_collection(d_ptr, d_len, 723 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) { 724 if (usb_test_quirk(uaa, UQ_KBD_IGNORE)) 725 error = ENXIO; 726 else 727 error = 0; 728 } else 729 error = ENXIO; 730 731 free(d_ptr, M_TEMP); 732 return (error); 733 } 734 735 static int 736 ukbd_attach(device_t dev) 737 { 738 struct ukbd_softc *sc = device_get_softc(dev); 739 struct usb_attach_arg *uaa = device_get_ivars(dev); 740 int32_t unit = device_get_unit(dev); 741 keyboard_t *kbd = &sc->sc_kbd; 742 void *hid_ptr = NULL; 743 usb_error_t err; 744 uint32_t flags; 745 uint16_t n; 746 uint16_t hid_len; 747 748 mtx_assert(&Giant, MA_OWNED); 749 750 kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0); 751 752 kbd->kb_data = (void *)sc; 753 754 device_set_usb_desc(dev); 755 756 sc->sc_udev = uaa->device; 757 sc->sc_iface = uaa->iface; 758 sc->sc_iface_index = uaa->info.bIfaceIndex; 759 sc->sc_iface_no = uaa->info.bIfaceNum; 760 sc->sc_mode = K_XLATE; 761 762 usb_callout_init_mtx(&sc->sc_callout, &Giant, 0); 763 764 err = usbd_transfer_setup(uaa->device, 765 &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config, 766 UKBD_N_TRANSFER, sc, &Giant); 767 768 if (err) { 769 DPRINTF("error=%s\n", usbd_errstr(err)); 770 goto detach; 771 } 772 /* setup default keyboard maps */ 773 774 sc->sc_keymap = key_map; 775 sc->sc_accmap = accent_map; 776 for (n = 0; n < UKBD_NFKEY; n++) { 777 sc->sc_fkeymap[n] = fkey_tab[n]; 778 } 779 780 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap, 781 sc->sc_fkeymap, UKBD_NFKEY); 782 783 KBD_FOUND_DEVICE(kbd); 784 785 ukbd_clear_state(kbd); 786 787 /* 788 * FIXME: set the initial value for lock keys in "sc_state" 789 * according to the BIOS data? 790 */ 791 KBD_PROBE_DONE(kbd); 792 793 /* figure out if there is an ID byte in the data */ 794 err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr, 795 &hid_len, M_TEMP, uaa->info.bIfaceIndex); 796 if (err == 0) { 797 uint8_t temp_id; 798 799 /* investigate if this is an Apple Keyboard */ 800 if (hid_locate(hid_ptr, hid_len, 801 HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT), 802 hid_input, 0, &sc->sc_loc_apple_eject, &flags, 803 &sc->sc_kbd_id)) { 804 if (flags & HIO_VARIABLE) 805 sc->sc_flags |= UKBD_FLAG_APPLE_EJECT | 806 UKBD_FLAG_APPLE_SWAP; 807 if (hid_locate(hid_ptr, hid_len, 808 HID_USAGE2(0xFFFF, 0x0003), 809 hid_input, 0, &sc->sc_loc_apple_fn, &flags, 810 &temp_id)) { 811 if (flags & HIO_VARIABLE) 812 sc->sc_flags |= UKBD_FLAG_APPLE_FN | 813 UKBD_FLAG_APPLE_SWAP; 814 if (temp_id != sc->sc_kbd_id) { 815 DPRINTF("HID IDs mismatch\n"); 816 } 817 } 818 } else { 819 /* 820 * Assume the first HID ID contains the 821 * keyboard data 822 */ 823 hid_report_size(hid_ptr, hid_len, 824 hid_input, &sc->sc_kbd_id); 825 } 826 827 /* investigate if we need an ID-byte for the leds */ 828 hid_report_size(hid_ptr, hid_len, hid_output, &sc->sc_led_id); 829 830 free(hid_ptr, M_TEMP); 831 } 832 833 /* ignore if SETIDLE fails, hence it is not crucial */ 834 err = usbd_req_set_idle(sc->sc_udev, &Giant, sc->sc_iface_index, 0, 0); 835 836 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state); 837 838 KBD_INIT_DONE(kbd); 839 840 if (kbd_register(kbd) < 0) { 841 goto detach; 842 } 843 KBD_CONFIG_DONE(kbd); 844 845 ukbd_enable(kbd); 846 847 #ifdef KBD_INSTALL_CDEV 848 if (kbd_attach(kbd)) { 849 goto detach; 850 } 851 #endif 852 sc->sc_flags |= UKBD_FLAG_ATTACHED; 853 854 if (bootverbose) { 855 genkbd_diag(kbd, bootverbose); 856 } 857 /* lock keyboard mutex */ 858 859 mtx_lock(&Giant); 860 861 /* start the keyboard */ 862 863 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]); 864 865 /* start the timer */ 866 867 ukbd_timeout(sc); 868 mtx_unlock(&Giant); 869 return (0); /* success */ 870 871 detach: 872 ukbd_detach(dev); 873 return (ENXIO); /* error */ 874 } 875 876 static int 877 ukbd_detach(device_t dev) 878 { 879 struct ukbd_softc *sc = device_get_softc(dev); 880 int error; 881 882 mtx_assert(&Giant, MA_OWNED); 883 884 DPRINTF("\n"); 885 886 if (sc->sc_flags & UKBD_FLAG_POLLING) { 887 panic("cannot detach polled keyboard!\n"); 888 } 889 sc->sc_flags |= UKBD_FLAG_GONE; 890 891 usb_callout_stop(&sc->sc_callout); 892 893 ukbd_disable(&sc->sc_kbd); 894 895 #ifdef KBD_INSTALL_CDEV 896 if (sc->sc_flags & UKBD_FLAG_ATTACHED) { 897 error = kbd_detach(&sc->sc_kbd); 898 if (error) { 899 /* usb attach cannot return an error */ 900 device_printf(dev, "WARNING: kbd_detach() " 901 "returned non-zero! (ignored)\n"); 902 } 903 } 904 #endif 905 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) { 906 error = kbd_unregister(&sc->sc_kbd); 907 if (error) { 908 /* usb attach cannot return an error */ 909 device_printf(dev, "WARNING: kbd_unregister() " 910 "returned non-zero! (ignored)\n"); 911 } 912 } 913 sc->sc_kbd.kb_flags = 0; 914 915 usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER); 916 917 usb_callout_drain(&sc->sc_callout); 918 919 DPRINTF("%s: disconnected\n", 920 device_get_nameunit(dev)); 921 922 return (0); 923 } 924 925 static int 926 ukbd_resume(device_t dev) 927 { 928 struct ukbd_softc *sc = device_get_softc(dev); 929 930 mtx_assert(&Giant, MA_OWNED); 931 932 ukbd_clear_state(&sc->sc_kbd); 933 934 return (0); 935 } 936 937 /* early keyboard probe, not supported */ 938 static int 939 ukbd_configure(int flags) 940 { 941 return (0); 942 } 943 944 /* detect a keyboard, not used */ 945 static int 946 ukbd__probe(int unit, void *arg, int flags) 947 { 948 mtx_assert(&Giant, MA_OWNED); 949 return (ENXIO); 950 } 951 952 /* reset and initialize the device, not used */ 953 static int 954 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) 955 { 956 mtx_assert(&Giant, MA_OWNED); 957 return (ENXIO); 958 } 959 960 /* test the interface to the device, not used */ 961 static int 962 ukbd_test_if(keyboard_t *kbd) 963 { 964 mtx_assert(&Giant, MA_OWNED); 965 return (0); 966 } 967 968 /* finish using this keyboard, not used */ 969 static int 970 ukbd_term(keyboard_t *kbd) 971 { 972 mtx_assert(&Giant, MA_OWNED); 973 return (ENXIO); 974 } 975 976 /* keyboard interrupt routine, not used */ 977 static int 978 ukbd_intr(keyboard_t *kbd, void *arg) 979 { 980 mtx_assert(&Giant, MA_OWNED); 981 return (0); 982 } 983 984 /* lock the access to the keyboard, not used */ 985 static int 986 ukbd_lock(keyboard_t *kbd, int lock) 987 { 988 mtx_assert(&Giant, MA_OWNED); 989 return (1); 990 } 991 992 /* 993 * Enable the access to the device; until this function is called, 994 * the client cannot read from the keyboard. 995 */ 996 static int 997 ukbd_enable(keyboard_t *kbd) 998 { 999 if (!mtx_owned(&Giant)) { 1000 /* XXX cludge */ 1001 int retval; 1002 mtx_lock(&Giant); 1003 retval = ukbd_enable(kbd); 1004 mtx_unlock(&Giant); 1005 return (retval); 1006 } 1007 mtx_assert(&Giant, MA_OWNED); 1008 KBD_ACTIVATE(kbd); 1009 return (0); 1010 } 1011 1012 /* disallow the access to the device */ 1013 static int 1014 ukbd_disable(keyboard_t *kbd) 1015 { 1016 if (!mtx_owned(&Giant)) { 1017 /* XXX cludge */ 1018 int retval; 1019 mtx_lock(&Giant); 1020 retval = ukbd_disable(kbd); 1021 mtx_unlock(&Giant); 1022 return (retval); 1023 } 1024 mtx_assert(&Giant, MA_OWNED); 1025 KBD_DEACTIVATE(kbd); 1026 return (0); 1027 } 1028 1029 /* check if data is waiting */ 1030 static int 1031 ukbd_check(keyboard_t *kbd) 1032 { 1033 struct ukbd_softc *sc = kbd->kb_data; 1034 1035 if (!KBD_IS_ACTIVE(kbd)) 1036 return (0); 1037 1038 if (sc->sc_flags & UKBD_FLAG_POLLING) { 1039 if (!mtx_owned(&Giant)) { 1040 /* XXX cludge */ 1041 int retval; 1042 mtx_lock(&Giant); 1043 retval = ukbd_check(kbd); 1044 mtx_unlock(&Giant); 1045 return (retval); 1046 } 1047 ukbd_do_poll(sc, 0); 1048 } else { 1049 /* XXX the keyboard layer requires Giant */ 1050 if (!mtx_owned(&Giant)) 1051 return (0); 1052 } 1053 mtx_assert(&Giant, MA_OWNED); 1054 1055 #ifdef UKBD_EMULATE_ATSCANCODE 1056 if (sc->sc_buffered_char[0]) { 1057 return (1); 1058 } 1059 #endif 1060 if (sc->sc_inputs > 0) { 1061 return (1); 1062 } 1063 return (0); 1064 } 1065 1066 /* check if char is waiting */ 1067 static int 1068 ukbd_check_char(keyboard_t *kbd) 1069 { 1070 struct ukbd_softc *sc = kbd->kb_data; 1071 1072 if (!KBD_IS_ACTIVE(kbd)) 1073 return (0); 1074 1075 if (sc->sc_flags & UKBD_FLAG_POLLING) { 1076 if (!mtx_owned(&Giant)) { 1077 /* XXX cludge */ 1078 int retval; 1079 mtx_lock(&Giant); 1080 retval = ukbd_check_char(kbd); 1081 mtx_unlock(&Giant); 1082 return (retval); 1083 } 1084 } else { 1085 /* XXX the keyboard layer requires Giant */ 1086 if (!mtx_owned(&Giant)) 1087 return (0); 1088 } 1089 mtx_assert(&Giant, MA_OWNED); 1090 1091 if ((sc->sc_composed_char > 0) && 1092 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) { 1093 return (1); 1094 } 1095 return (ukbd_check(kbd)); 1096 } 1097 1098 1099 /* read one byte from the keyboard if it's allowed */ 1100 static int 1101 ukbd_read(keyboard_t *kbd, int wait) 1102 { 1103 struct ukbd_softc *sc = kbd->kb_data; 1104 int32_t usbcode; 1105 1106 #ifdef UKBD_EMULATE_ATSCANCODE 1107 uint32_t keycode; 1108 uint32_t scancode; 1109 1110 #endif 1111 if (!KBD_IS_ACTIVE(kbd)) 1112 return (-1); 1113 1114 if (sc->sc_flags & UKBD_FLAG_POLLING) { 1115 if (!mtx_owned(&Giant)) { 1116 /* XXX cludge */ 1117 int retval; 1118 mtx_lock(&Giant); 1119 retval = ukbd_read(kbd, wait); 1120 mtx_unlock(&Giant); 1121 return (retval); 1122 } 1123 } else { 1124 /* XXX the keyboard layer requires Giant */ 1125 if (!mtx_owned(&Giant)) 1126 return (-1); 1127 } 1128 mtx_assert(&Giant, MA_OWNED); 1129 1130 #ifdef UKBD_EMULATE_ATSCANCODE 1131 if (sc->sc_buffered_char[0]) { 1132 scancode = sc->sc_buffered_char[0]; 1133 if (scancode & SCAN_PREFIX) { 1134 sc->sc_buffered_char[0] &= ~SCAN_PREFIX; 1135 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); 1136 } 1137 sc->sc_buffered_char[0] = sc->sc_buffered_char[1]; 1138 sc->sc_buffered_char[1] = 0; 1139 return (scancode); 1140 } 1141 #endif /* UKBD_EMULATE_ATSCANCODE */ 1142 1143 /* XXX */ 1144 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1); 1145 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1)) 1146 return (-1); 1147 1148 ++(kbd->kb_count); 1149 1150 #ifdef UKBD_EMULATE_ATSCANCODE 1151 keycode = ukbd_trtab[KEY_INDEX(usbcode)]; 1152 if (keycode == NN) { 1153 return -1; 1154 } 1155 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers, 1156 (usbcode & KEY_RELEASE))); 1157 #else /* !UKBD_EMULATE_ATSCANCODE */ 1158 return (usbcode); 1159 #endif /* UKBD_EMULATE_ATSCANCODE */ 1160 } 1161 1162 /* read char from the keyboard */ 1163 static uint32_t 1164 ukbd_read_char(keyboard_t *kbd, int wait) 1165 { 1166 struct ukbd_softc *sc = kbd->kb_data; 1167 uint32_t action; 1168 uint32_t keycode; 1169 int32_t usbcode; 1170 1171 #ifdef UKBD_EMULATE_ATSCANCODE 1172 uint32_t scancode; 1173 1174 #endif 1175 1176 if (!KBD_IS_ACTIVE(kbd)) 1177 return (NOKEY); 1178 1179 if (sc->sc_flags & UKBD_FLAG_POLLING) { 1180 if (!mtx_owned(&Giant)) { 1181 /* XXX cludge */ 1182 int retval; 1183 mtx_lock(&Giant); 1184 retval = ukbd_read_char(kbd, wait); 1185 mtx_unlock(&Giant); 1186 return (retval); 1187 } 1188 } else { 1189 /* XXX the keyboard layer requires Giant */ 1190 if (!mtx_owned(&Giant)) 1191 return (NOKEY); 1192 } 1193 mtx_assert(&Giant, MA_OWNED); 1194 1195 next_code: 1196 1197 /* do we have a composed char to return ? */ 1198 1199 if ((sc->sc_composed_char > 0) && 1200 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) { 1201 1202 action = sc->sc_composed_char; 1203 sc->sc_composed_char = 0; 1204 1205 if (action > 0xFF) { 1206 goto errkey; 1207 } 1208 goto done; 1209 } 1210 #ifdef UKBD_EMULATE_ATSCANCODE 1211 1212 /* do we have a pending raw scan code? */ 1213 1214 if (sc->sc_mode == K_RAW) { 1215 scancode = sc->sc_buffered_char[0]; 1216 if (scancode) { 1217 if (scancode & SCAN_PREFIX) { 1218 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX); 1219 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); 1220 } 1221 sc->sc_buffered_char[0] = sc->sc_buffered_char[1]; 1222 sc->sc_buffered_char[1] = 0; 1223 return (scancode); 1224 } 1225 } 1226 #endif /* UKBD_EMULATE_ATSCANCODE */ 1227 1228 /* see if there is something in the keyboard port */ 1229 /* XXX */ 1230 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1); 1231 if (usbcode == -1) { 1232 return (NOKEY); 1233 } 1234 ++kbd->kb_count; 1235 1236 #ifdef UKBD_EMULATE_ATSCANCODE 1237 /* USB key index -> key code -> AT scan code */ 1238 keycode = ukbd_trtab[KEY_INDEX(usbcode)]; 1239 if (keycode == NN) { 1240 return (NOKEY); 1241 } 1242 /* return an AT scan code for the K_RAW mode */ 1243 if (sc->sc_mode == K_RAW) { 1244 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers, 1245 (usbcode & KEY_RELEASE))); 1246 } 1247 #else /* !UKBD_EMULATE_ATSCANCODE */ 1248 1249 /* return the byte as is for the K_RAW mode */ 1250 if (sc->sc_mode == K_RAW) { 1251 return (usbcode); 1252 } 1253 /* USB key index -> key code */ 1254 keycode = ukbd_trtab[KEY_INDEX(usbcode)]; 1255 if (keycode == NN) { 1256 return (NOKEY); 1257 } 1258 #endif /* UKBD_EMULATE_ATSCANCODE */ 1259 1260 switch (keycode) { 1261 case 0x38: /* left alt (compose key) */ 1262 if (usbcode & KEY_RELEASE) { 1263 if (sc->sc_flags & UKBD_FLAG_COMPOSE) { 1264 sc->sc_flags &= ~UKBD_FLAG_COMPOSE; 1265 1266 if (sc->sc_composed_char > 0xFF) { 1267 sc->sc_composed_char = 0; 1268 } 1269 } 1270 } else { 1271 if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) { 1272 sc->sc_flags |= UKBD_FLAG_COMPOSE; 1273 sc->sc_composed_char = 0; 1274 } 1275 } 1276 break; 1277 /* XXX: I don't like these... */ 1278 case 0x5c: /* print screen */ 1279 if (sc->sc_flags & ALTS) { 1280 keycode = 0x54; /* sysrq */ 1281 } 1282 break; 1283 case 0x68: /* pause/break */ 1284 if (sc->sc_flags & CTLS) { 1285 keycode = 0x6c; /* break */ 1286 } 1287 break; 1288 } 1289 1290 /* return the key code in the K_CODE mode */ 1291 if (usbcode & KEY_RELEASE) { 1292 keycode |= SCAN_RELEASE; 1293 } 1294 if (sc->sc_mode == K_CODE) { 1295 return (keycode); 1296 } 1297 /* compose a character code */ 1298 if (sc->sc_flags & UKBD_FLAG_COMPOSE) { 1299 switch (keycode) { 1300 /* key pressed, process it */ 1301 case 0x47: 1302 case 0x48: 1303 case 0x49: /* keypad 7,8,9 */ 1304 sc->sc_composed_char *= 10; 1305 sc->sc_composed_char += keycode - 0x40; 1306 goto check_composed; 1307 1308 case 0x4B: 1309 case 0x4C: 1310 case 0x4D: /* keypad 4,5,6 */ 1311 sc->sc_composed_char *= 10; 1312 sc->sc_composed_char += keycode - 0x47; 1313 goto check_composed; 1314 1315 case 0x4F: 1316 case 0x50: 1317 case 0x51: /* keypad 1,2,3 */ 1318 sc->sc_composed_char *= 10; 1319 sc->sc_composed_char += keycode - 0x4E; 1320 goto check_composed; 1321 1322 case 0x52: /* keypad 0 */ 1323 sc->sc_composed_char *= 10; 1324 goto check_composed; 1325 1326 /* key released, no interest here */ 1327 case SCAN_RELEASE | 0x47: 1328 case SCAN_RELEASE | 0x48: 1329 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */ 1330 case SCAN_RELEASE | 0x4B: 1331 case SCAN_RELEASE | 0x4C: 1332 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */ 1333 case SCAN_RELEASE | 0x4F: 1334 case SCAN_RELEASE | 0x50: 1335 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */ 1336 case SCAN_RELEASE | 0x52: /* keypad 0 */ 1337 goto next_code; 1338 1339 case 0x38: /* left alt key */ 1340 break; 1341 1342 default: 1343 if (sc->sc_composed_char > 0) { 1344 sc->sc_flags &= ~UKBD_FLAG_COMPOSE; 1345 sc->sc_composed_char = 0; 1346 goto errkey; 1347 } 1348 break; 1349 } 1350 } 1351 /* keycode to key action */ 1352 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode), 1353 (keycode & SCAN_RELEASE), 1354 &sc->sc_state, &sc->sc_accents); 1355 if (action == NOKEY) { 1356 goto next_code; 1357 } 1358 done: 1359 return (action); 1360 1361 check_composed: 1362 if (sc->sc_composed_char <= 0xFF) { 1363 goto next_code; 1364 } 1365 errkey: 1366 return (ERRKEY); 1367 } 1368 1369 /* some useful control functions */ 1370 static int 1371 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) 1372 { 1373 /* translate LED_XXX bits into the device specific bits */ 1374 static const uint8_t ledmap[8] = { 1375 0, 2, 1, 3, 4, 6, 5, 7, 1376 }; 1377 struct ukbd_softc *sc = kbd->kb_data; 1378 int i; 1379 1380 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1381 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1382 int ival; 1383 1384 #endif 1385 if (!mtx_owned(&Giant)) { 1386 /* 1387 * XXX big problem: If scroll lock is pressed and "printf()" 1388 * is called, the CPU will get here, to un-scroll lock the 1389 * keyboard. But if "printf()" acquires the "Giant" lock, 1390 * there will be a locking order reversal problem, so the 1391 * keyboard system must get out of "Giant" first, before the 1392 * CPU can proceed here ... 1393 */ 1394 return (EINVAL); 1395 } 1396 mtx_assert(&Giant, MA_OWNED); 1397 1398 switch (cmd) { 1399 case KDGKBMODE: /* get keyboard mode */ 1400 *(int *)arg = sc->sc_mode; 1401 break; 1402 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1403 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1404 case _IO('K', 7): 1405 ival = IOCPARM_IVAL(arg); 1406 arg = (caddr_t)&ival; 1407 /* FALLTHROUGH */ 1408 #endif 1409 case KDSKBMODE: /* set keyboard mode */ 1410 switch (*(int *)arg) { 1411 case K_XLATE: 1412 if (sc->sc_mode != K_XLATE) { 1413 /* make lock key state and LED state match */ 1414 sc->sc_state &= ~LOCK_MASK; 1415 sc->sc_state |= KBD_LED_VAL(kbd); 1416 } 1417 /* FALLTHROUGH */ 1418 case K_RAW: 1419 case K_CODE: 1420 if (sc->sc_mode != *(int *)arg) { 1421 ukbd_clear_state(kbd); 1422 sc->sc_mode = *(int *)arg; 1423 } 1424 break; 1425 default: 1426 return (EINVAL); 1427 } 1428 break; 1429 1430 case KDGETLED: /* get keyboard LED */ 1431 *(int *)arg = KBD_LED_VAL(kbd); 1432 break; 1433 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1434 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1435 case _IO('K', 66): 1436 ival = IOCPARM_IVAL(arg); 1437 arg = (caddr_t)&ival; 1438 /* FALLTHROUGH */ 1439 #endif 1440 case KDSETLED: /* set keyboard LED */ 1441 /* NOTE: lock key state in "sc_state" won't be changed */ 1442 if (*(int *)arg & ~LOCK_MASK) { 1443 return (EINVAL); 1444 } 1445 i = *(int *)arg; 1446 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */ 1447 if (sc->sc_mode == K_XLATE && 1448 kbd->kb_keymap->n_keys > ALTGR_OFFSET) { 1449 if (i & ALKED) 1450 i |= CLKED; 1451 else 1452 i &= ~CLKED; 1453 } 1454 if (KBD_HAS_DEVICE(kbd)) { 1455 ukbd_set_leds(sc, ledmap[i & LED_MASK]); 1456 } 1457 KBD_LED_VAL(kbd) = *(int *)arg; 1458 break; 1459 case KDGKBSTATE: /* get lock key state */ 1460 *(int *)arg = sc->sc_state & LOCK_MASK; 1461 break; 1462 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1463 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1464 case _IO('K', 20): 1465 ival = IOCPARM_IVAL(arg); 1466 arg = (caddr_t)&ival; 1467 /* FALLTHROUGH */ 1468 #endif 1469 case KDSKBSTATE: /* set lock key state */ 1470 if (*(int *)arg & ~LOCK_MASK) { 1471 return (EINVAL); 1472 } 1473 sc->sc_state &= ~LOCK_MASK; 1474 sc->sc_state |= *(int *)arg; 1475 1476 /* set LEDs and quit */ 1477 return (ukbd_ioctl(kbd, KDSETLED, arg)); 1478 1479 case KDSETREPEAT: /* set keyboard repeat rate (new 1480 * interface) */ 1481 if (!KBD_HAS_DEVICE(kbd)) { 1482 return (0); 1483 } 1484 if (((int *)arg)[1] < 0) { 1485 return (EINVAL); 1486 } 1487 if (((int *)arg)[0] < 0) { 1488 return (EINVAL); 1489 } 1490 if (((int *)arg)[0] < 200) /* fastest possible value */ 1491 kbd->kb_delay1 = 200; 1492 else 1493 kbd->kb_delay1 = ((int *)arg)[0]; 1494 kbd->kb_delay2 = ((int *)arg)[1]; 1495 return (0); 1496 1497 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1498 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1499 case _IO('K', 67): 1500 ival = IOCPARM_IVAL(arg); 1501 arg = (caddr_t)&ival; 1502 /* FALLTHROUGH */ 1503 #endif 1504 case KDSETRAD: /* set keyboard repeat rate (old 1505 * interface) */ 1506 return (ukbd_set_typematic(kbd, *(int *)arg)); 1507 1508 case PIO_KEYMAP: /* set keyboard translation table */ 1509 case PIO_KEYMAPENT: /* set keyboard translation table 1510 * entry */ 1511 case PIO_DEADKEYMAP: /* set accent key translation table */ 1512 sc->sc_accents = 0; 1513 /* FALLTHROUGH */ 1514 default: 1515 return (genkbd_commonioctl(kbd, cmd, arg)); 1516 } 1517 1518 return (0); 1519 } 1520 1521 /* clear the internal state of the keyboard */ 1522 static void 1523 ukbd_clear_state(keyboard_t *kbd) 1524 { 1525 struct ukbd_softc *sc = kbd->kb_data; 1526 1527 if (!mtx_owned(&Giant)) { 1528 return; /* XXX */ 1529 } 1530 mtx_assert(&Giant, MA_OWNED); 1531 1532 sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING); 1533 sc->sc_state &= LOCK_MASK; /* preserve locking key state */ 1534 sc->sc_accents = 0; 1535 sc->sc_composed_char = 0; 1536 #ifdef UKBD_EMULATE_ATSCANCODE 1537 sc->sc_buffered_char[0] = 0; 1538 sc->sc_buffered_char[1] = 0; 1539 #endif 1540 bzero(&sc->sc_ndata, sizeof(sc->sc_ndata)); 1541 bzero(&sc->sc_odata, sizeof(sc->sc_odata)); 1542 bzero(&sc->sc_ntime, sizeof(sc->sc_ntime)); 1543 bzero(&sc->sc_otime, sizeof(sc->sc_otime)); 1544 } 1545 1546 /* save the internal state, not used */ 1547 static int 1548 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len) 1549 { 1550 mtx_assert(&Giant, MA_OWNED); 1551 return (len == 0) ? 1 : -1; 1552 } 1553 1554 /* set the internal state, not used */ 1555 static int 1556 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len) 1557 { 1558 mtx_assert(&Giant, MA_OWNED); 1559 return (EINVAL); 1560 } 1561 1562 static int 1563 ukbd_poll(keyboard_t *kbd, int on) 1564 { 1565 struct ukbd_softc *sc = kbd->kb_data; 1566 1567 if (!mtx_owned(&Giant)) { 1568 /* XXX cludge */ 1569 int retval; 1570 mtx_lock(&Giant); 1571 retval = ukbd_poll(kbd, on); 1572 mtx_unlock(&Giant); 1573 return (retval); 1574 } 1575 mtx_assert(&Giant, MA_OWNED); 1576 1577 if (on) { 1578 sc->sc_flags |= UKBD_FLAG_POLLING; 1579 } else { 1580 sc->sc_flags &= ~UKBD_FLAG_POLLING; 1581 } 1582 return (0); 1583 } 1584 1585 /* local functions */ 1586 1587 static void 1588 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds) 1589 { 1590 DPRINTF("leds=0x%02x\n", leds); 1591 1592 sc->sc_leds = leds; 1593 sc->sc_flags |= UKBD_FLAG_SET_LEDS; 1594 1595 /* start transfer, if not already started */ 1596 1597 usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]); 1598 } 1599 1600 static int 1601 ukbd_set_typematic(keyboard_t *kbd, int code) 1602 { 1603 static const int delays[] = {250, 500, 750, 1000}; 1604 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63, 1605 68, 76, 84, 92, 100, 110, 118, 126, 1606 136, 152, 168, 184, 200, 220, 236, 252, 1607 272, 304, 336, 368, 400, 440, 472, 504}; 1608 1609 if (code & ~0x7f) { 1610 return (EINVAL); 1611 } 1612 kbd->kb_delay1 = delays[(code >> 5) & 3]; 1613 kbd->kb_delay2 = rates[code & 0x1f]; 1614 return (0); 1615 } 1616 1617 #ifdef UKBD_EMULATE_ATSCANCODE 1618 static int 1619 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up) 1620 { 1621 static const int scan[] = { 1622 0x1c, 0x1d, 0x35, 1623 0x37 | SCAN_PREFIX_SHIFT, /* PrintScreen */ 1624 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f, 1625 0x50, 0x51, 0x52, 0x53, 1626 0x46, /* XXX Pause/Break */ 1627 0x5b, 0x5c, 0x5d, 1628 /* SUN TYPE 6 USB KEYBOARD */ 1629 0x68, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 1630 0x64, 0x65, 0x66, 0x67, 0x25, 0x1f, 0x1e, 1631 0x20, 1632 }; 1633 1634 if ((code >= 89) && (code < (89 + (sizeof(scan) / sizeof(scan[0]))))) { 1635 code = scan[code - 89] | SCAN_PREFIX_E0; 1636 } 1637 /* Pause/Break */ 1638 if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) { 1639 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL); 1640 } 1641 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) { 1642 code &= ~SCAN_PREFIX_SHIFT; 1643 } 1644 code |= (up ? SCAN_RELEASE : SCAN_PRESS); 1645 1646 if (code & SCAN_PREFIX) { 1647 if (code & SCAN_PREFIX_CTL) { 1648 /* Ctrl */ 1649 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE)); 1650 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX); 1651 } else if (code & SCAN_PREFIX_SHIFT) { 1652 /* Shift */ 1653 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE)); 1654 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT); 1655 } else { 1656 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX); 1657 sc->sc_buffered_char[1] = 0; 1658 } 1659 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); 1660 } 1661 return (code); 1662 1663 } 1664 1665 #endif /* UKBD_EMULATE_ATSCANCODE */ 1666 1667 static keyboard_switch_t ukbdsw = { 1668 .probe = &ukbd__probe, 1669 .init = &ukbd_init, 1670 .term = &ukbd_term, 1671 .intr = &ukbd_intr, 1672 .test_if = &ukbd_test_if, 1673 .enable = &ukbd_enable, 1674 .disable = &ukbd_disable, 1675 .read = &ukbd_read, 1676 .check = &ukbd_check, 1677 .read_char = &ukbd_read_char, 1678 .check_char = &ukbd_check_char, 1679 .ioctl = &ukbd_ioctl, 1680 .lock = &ukbd_lock, 1681 .clear_state = &ukbd_clear_state, 1682 .get_state = &ukbd_get_state, 1683 .set_state = &ukbd_set_state, 1684 .get_fkeystr = &genkbd_get_fkeystr, 1685 .poll = &ukbd_poll, 1686 .diag = &genkbd_diag, 1687 }; 1688 1689 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure); 1690 1691 static int 1692 ukbd_driver_load(module_t mod, int what, void *arg) 1693 { 1694 switch (what) { 1695 case MOD_LOAD: 1696 kbd_add_driver(&ukbd_kbd_driver); 1697 break; 1698 case MOD_UNLOAD: 1699 kbd_delete_driver(&ukbd_kbd_driver); 1700 break; 1701 } 1702 return (0); 1703 } 1704 1705 static devclass_t ukbd_devclass; 1706 1707 static device_method_t ukbd_methods[] = { 1708 DEVMETHOD(device_probe, ukbd_probe), 1709 DEVMETHOD(device_attach, ukbd_attach), 1710 DEVMETHOD(device_detach, ukbd_detach), 1711 DEVMETHOD(device_resume, ukbd_resume), 1712 {0, 0} 1713 }; 1714 1715 static driver_t ukbd_driver = { 1716 .name = "ukbd", 1717 .methods = ukbd_methods, 1718 .size = sizeof(struct ukbd_softc), 1719 }; 1720 1721 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0); 1722 MODULE_DEPEND(ukbd, usb, 1, 1, 1); 1723