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