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 usb2_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 sc->sc_iface = uaa->iface; 710 711 usb2_callout_init_mtx(&sc->sc_callout, &Giant, 0); 712 713 err = usb2_transfer_setup(uaa->device, 714 &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config, 715 UKBD_N_TRANSFER, sc, &Giant); 716 717 if (err) { 718 DPRINTF("error=%s\n", usb2_errstr(err)); 719 goto detach; 720 } 721 /* setup default keyboard maps */ 722 723 sc->sc_keymap = key_map; 724 sc->sc_accmap = accent_map; 725 for (n = 0; n < UKBD_NFKEY; n++) { 726 sc->sc_fkeymap[n] = fkey_tab[n]; 727 } 728 729 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap, 730 sc->sc_fkeymap, UKBD_NFKEY); 731 732 KBD_FOUND_DEVICE(kbd); 733 734 ukbd_clear_state(kbd); 735 736 /* 737 * FIXME: set the initial value for lock keys in "sc_state" 738 * according to the BIOS data? 739 */ 740 KBD_PROBE_DONE(kbd); 741 742 /* figure out if there is an ID byte in the data */ 743 err = usb2_req_get_hid_desc(uaa->device, NULL, &hid_ptr, 744 &hid_len, M_TEMP, uaa->info.bIfaceIndex); 745 if (err == 0) { 746 uint8_t temp_id; 747 748 /* investigate if this is an Apple Keyboard */ 749 if (hid_locate(hid_ptr, hid_len, 750 HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT), 751 hid_input, 0, &sc->sc_loc_apple_eject, &flags, 752 &sc->sc_kbd_id)) { 753 if (flags & HIO_VARIABLE) 754 sc->sc_flags |= UKBD_FLAG_APPLE_EJECT | 755 UKBD_FLAG_APPLE_SWAP; 756 if (hid_locate(hid_ptr, hid_len, 757 HID_USAGE2(0xFFFF, 0x0003), 758 hid_input, 0, &sc->sc_loc_apple_fn, &flags, 759 &temp_id)) { 760 if (flags & HIO_VARIABLE) 761 sc->sc_flags |= UKBD_FLAG_APPLE_FN | 762 UKBD_FLAG_APPLE_SWAP; 763 if (temp_id != sc->sc_kbd_id) { 764 DPRINTF("HID IDs mismatch\n"); 765 } 766 } 767 } else { 768 /* 769 * Assume the first HID ID contains the 770 * keyboard data 771 */ 772 hid_report_size(hid_ptr, hid_len, 773 hid_input, &sc->sc_kbd_id); 774 } 775 776 /* investigate if we need an ID-byte for the leds */ 777 hid_report_size(hid_ptr, hid_len, hid_output, &sc->sc_led_id); 778 779 free(hid_ptr, M_TEMP); 780 } 781 782 /* ignore if SETIDLE fails, hence it is not crucial */ 783 err = usb2_req_set_idle(sc->sc_udev, &Giant, sc->sc_iface_index, 0, 0); 784 785 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state); 786 787 KBD_INIT_DONE(kbd); 788 789 if (kbd_register(kbd) < 0) { 790 goto detach; 791 } 792 KBD_CONFIG_DONE(kbd); 793 794 ukbd_enable(kbd); 795 796 #ifdef KBD_INSTALL_CDEV 797 if (kbd_attach(kbd)) { 798 goto detach; 799 } 800 #endif 801 sc->sc_flags |= UKBD_FLAG_ATTACHED; 802 803 if (bootverbose) { 804 genkbd_diag(kbd, bootverbose); 805 } 806 /* lock keyboard mutex */ 807 808 mtx_lock(&Giant); 809 810 /* start the keyboard */ 811 812 usb2_transfer_start(sc->sc_xfer[UKBD_INTR_DT]); 813 814 /* start the timer */ 815 816 ukbd_timeout(sc); 817 mtx_unlock(&Giant); 818 return (0); /* success */ 819 820 detach: 821 ukbd_detach(dev); 822 return (ENXIO); /* error */ 823 } 824 825 int 826 ukbd_detach(device_t dev) 827 { 828 struct ukbd_softc *sc = device_get_softc(dev); 829 int error; 830 831 mtx_assert(&Giant, MA_OWNED); 832 833 DPRINTF("\n"); 834 835 if (sc->sc_flags & UKBD_FLAG_POLLING) { 836 panic("cannot detach polled keyboard!\n"); 837 } 838 sc->sc_flags |= UKBD_FLAG_GONE; 839 840 usb2_callout_stop(&sc->sc_callout); 841 842 ukbd_disable(&sc->sc_kbd); 843 844 #ifdef KBD_INSTALL_CDEV 845 if (sc->sc_flags & UKBD_FLAG_ATTACHED) { 846 error = kbd_detach(&sc->sc_kbd); 847 if (error) { 848 /* usb attach cannot return an error */ 849 device_printf(dev, "WARNING: kbd_detach() " 850 "returned non-zero! (ignored)\n"); 851 } 852 } 853 #endif 854 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) { 855 error = kbd_unregister(&sc->sc_kbd); 856 if (error) { 857 /* usb attach cannot return an error */ 858 device_printf(dev, "WARNING: kbd_unregister() " 859 "returned non-zero! (ignored)\n"); 860 } 861 } 862 sc->sc_kbd.kb_flags = 0; 863 864 usb2_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER); 865 866 usb2_callout_drain(&sc->sc_callout); 867 868 DPRINTF("%s: disconnected\n", 869 device_get_nameunit(dev)); 870 871 return (0); 872 } 873 874 static int 875 ukbd_resume(device_t dev) 876 { 877 struct ukbd_softc *sc = device_get_softc(dev); 878 879 mtx_assert(&Giant, MA_OWNED); 880 881 ukbd_clear_state(&sc->sc_kbd); 882 883 return (0); 884 } 885 886 /* early keyboard probe, not supported */ 887 static int 888 ukbd_configure(int flags) 889 { 890 return (0); 891 } 892 893 /* detect a keyboard, not used */ 894 static int 895 ukbd__probe(int unit, void *arg, int flags) 896 { 897 mtx_assert(&Giant, MA_OWNED); 898 return (ENXIO); 899 } 900 901 /* reset and initialize the device, not used */ 902 static int 903 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) 904 { 905 mtx_assert(&Giant, MA_OWNED); 906 return (ENXIO); 907 } 908 909 /* test the interface to the device, not used */ 910 static int 911 ukbd_test_if(keyboard_t *kbd) 912 { 913 mtx_assert(&Giant, MA_OWNED); 914 return (0); 915 } 916 917 /* finish using this keyboard, not used */ 918 static int 919 ukbd_term(keyboard_t *kbd) 920 { 921 mtx_assert(&Giant, MA_OWNED); 922 return (ENXIO); 923 } 924 925 /* keyboard interrupt routine, not used */ 926 static int 927 ukbd_intr(keyboard_t *kbd, void *arg) 928 { 929 mtx_assert(&Giant, MA_OWNED); 930 return (0); 931 } 932 933 /* lock the access to the keyboard, not used */ 934 static int 935 ukbd_lock(keyboard_t *kbd, int lock) 936 { 937 mtx_assert(&Giant, MA_OWNED); 938 return (1); 939 } 940 941 /* 942 * Enable the access to the device; until this function is called, 943 * the client cannot read from the keyboard. 944 */ 945 static int 946 ukbd_enable(keyboard_t *kbd) 947 { 948 mtx_assert(&Giant, MA_OWNED); 949 KBD_ACTIVATE(kbd); 950 return (0); 951 } 952 953 /* disallow the access to the device */ 954 static int 955 ukbd_disable(keyboard_t *kbd) 956 { 957 mtx_assert(&Giant, MA_OWNED); 958 KBD_DEACTIVATE(kbd); 959 return (0); 960 } 961 962 /* check if data is waiting */ 963 static int 964 ukbd_check(keyboard_t *kbd) 965 { 966 struct ukbd_softc *sc = kbd->kb_data; 967 968 if (!mtx_owned(&Giant)) { 969 return (0); /* XXX */ 970 } 971 mtx_assert(&Giant, MA_OWNED); 972 973 if (!KBD_IS_ACTIVE(kbd)) { 974 return (0); 975 } 976 #ifdef UKBD_EMULATE_ATSCANCODE 977 if (sc->sc_buffered_char[0]) { 978 return (1); 979 } 980 #endif 981 if (sc->sc_inputs > 0) { 982 return (1); 983 } 984 return (0); 985 } 986 987 /* check if char is waiting */ 988 static int 989 ukbd_check_char(keyboard_t *kbd) 990 { 991 struct ukbd_softc *sc = kbd->kb_data; 992 993 if (!mtx_owned(&Giant)) { 994 return (0); /* XXX */ 995 } 996 mtx_assert(&Giant, MA_OWNED); 997 998 if (!KBD_IS_ACTIVE(kbd)) { 999 return (0); 1000 } 1001 if ((sc->sc_composed_char > 0) && 1002 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) { 1003 return (1); 1004 } 1005 return (ukbd_check(kbd)); 1006 } 1007 1008 1009 /* read one byte from the keyboard if it's allowed */ 1010 static int 1011 ukbd_read(keyboard_t *kbd, int wait) 1012 { 1013 struct ukbd_softc *sc = kbd->kb_data; 1014 int32_t usbcode; 1015 1016 #ifdef UKBD_EMULATE_ATSCANCODE 1017 uint32_t keycode; 1018 uint32_t scancode; 1019 1020 #endif 1021 1022 if (!mtx_owned(&Giant)) { 1023 return -1; /* XXX */ 1024 } 1025 mtx_assert(&Giant, MA_OWNED); 1026 1027 #ifdef UKBD_EMULATE_ATSCANCODE 1028 if (sc->sc_buffered_char[0]) { 1029 scancode = sc->sc_buffered_char[0]; 1030 if (scancode & SCAN_PREFIX) { 1031 sc->sc_buffered_char[0] &= ~SCAN_PREFIX; 1032 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); 1033 } 1034 sc->sc_buffered_char[0] = sc->sc_buffered_char[1]; 1035 sc->sc_buffered_char[1] = 0; 1036 return (scancode); 1037 } 1038 #endif /* UKBD_EMULATE_ATSCANCODE */ 1039 1040 /* XXX */ 1041 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1); 1042 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1)) { 1043 return -1; 1044 } 1045 ++(kbd->kb_count); 1046 1047 #ifdef UKBD_EMULATE_ATSCANCODE 1048 keycode = ukbd_trtab[KEY_INDEX(usbcode)]; 1049 if (keycode == NN) { 1050 return -1; 1051 } 1052 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers, 1053 (usbcode & KEY_RELEASE))); 1054 #else /* !UKBD_EMULATE_ATSCANCODE */ 1055 return (usbcode); 1056 #endif /* UKBD_EMULATE_ATSCANCODE */ 1057 } 1058 1059 /* read char from the keyboard */ 1060 static uint32_t 1061 ukbd_read_char(keyboard_t *kbd, int wait) 1062 { 1063 struct ukbd_softc *sc = kbd->kb_data; 1064 uint32_t action; 1065 uint32_t keycode; 1066 int32_t usbcode; 1067 1068 #ifdef UKBD_EMULATE_ATSCANCODE 1069 uint32_t scancode; 1070 1071 #endif 1072 if (!mtx_owned(&Giant)) { 1073 return (NOKEY); /* XXX */ 1074 } 1075 mtx_assert(&Giant, MA_OWNED); 1076 1077 next_code: 1078 1079 /* do we have a composed char to return ? */ 1080 1081 if ((sc->sc_composed_char > 0) && 1082 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) { 1083 1084 action = sc->sc_composed_char; 1085 sc->sc_composed_char = 0; 1086 1087 if (action > 0xFF) { 1088 goto errkey; 1089 } 1090 goto done; 1091 } 1092 #ifdef UKBD_EMULATE_ATSCANCODE 1093 1094 /* do we have a pending raw scan code? */ 1095 1096 if (sc->sc_mode == K_RAW) { 1097 scancode = sc->sc_buffered_char[0]; 1098 if (scancode) { 1099 if (scancode & SCAN_PREFIX) { 1100 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX); 1101 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); 1102 } 1103 sc->sc_buffered_char[0] = sc->sc_buffered_char[1]; 1104 sc->sc_buffered_char[1] = 0; 1105 return (scancode); 1106 } 1107 } 1108 #endif /* UKBD_EMULATE_ATSCANCODE */ 1109 1110 /* see if there is something in the keyboard port */ 1111 /* XXX */ 1112 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1); 1113 if (usbcode == -1) { 1114 return (NOKEY); 1115 } 1116 ++kbd->kb_count; 1117 1118 #ifdef UKBD_EMULATE_ATSCANCODE 1119 /* USB key index -> key code -> AT scan code */ 1120 keycode = ukbd_trtab[KEY_INDEX(usbcode)]; 1121 if (keycode == NN) { 1122 return (NOKEY); 1123 } 1124 /* return an AT scan code for the K_RAW mode */ 1125 if (sc->sc_mode == K_RAW) { 1126 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers, 1127 (usbcode & KEY_RELEASE))); 1128 } 1129 #else /* !UKBD_EMULATE_ATSCANCODE */ 1130 1131 /* return the byte as is for the K_RAW mode */ 1132 if (sc->sc_mode == K_RAW) { 1133 return (usbcode); 1134 } 1135 /* USB key index -> key code */ 1136 keycode = ukbd_trtab[KEY_INDEX(usbcode)]; 1137 if (keycode == NN) { 1138 return (NOKEY); 1139 } 1140 #endif /* UKBD_EMULATE_ATSCANCODE */ 1141 1142 switch (keycode) { 1143 case 0x38: /* left alt (compose key) */ 1144 if (usbcode & KEY_RELEASE) { 1145 if (sc->sc_flags & UKBD_FLAG_COMPOSE) { 1146 sc->sc_flags &= ~UKBD_FLAG_COMPOSE; 1147 1148 if (sc->sc_composed_char > 0xFF) { 1149 sc->sc_composed_char = 0; 1150 } 1151 } 1152 } else { 1153 if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) { 1154 sc->sc_flags |= UKBD_FLAG_COMPOSE; 1155 sc->sc_composed_char = 0; 1156 } 1157 } 1158 break; 1159 /* XXX: I don't like these... */ 1160 case 0x5c: /* print screen */ 1161 if (sc->sc_flags & ALTS) { 1162 keycode = 0x54; /* sysrq */ 1163 } 1164 break; 1165 case 0x68: /* pause/break */ 1166 if (sc->sc_flags & CTLS) { 1167 keycode = 0x6c; /* break */ 1168 } 1169 break; 1170 } 1171 1172 /* return the key code in the K_CODE mode */ 1173 if (usbcode & KEY_RELEASE) { 1174 keycode |= SCAN_RELEASE; 1175 } 1176 if (sc->sc_mode == K_CODE) { 1177 return (keycode); 1178 } 1179 /* compose a character code */ 1180 if (sc->sc_flags & UKBD_FLAG_COMPOSE) { 1181 switch (keycode) { 1182 /* key pressed, process it */ 1183 case 0x47: 1184 case 0x48: 1185 case 0x49: /* keypad 7,8,9 */ 1186 sc->sc_composed_char *= 10; 1187 sc->sc_composed_char += keycode - 0x40; 1188 goto check_composed; 1189 1190 case 0x4B: 1191 case 0x4C: 1192 case 0x4D: /* keypad 4,5,6 */ 1193 sc->sc_composed_char *= 10; 1194 sc->sc_composed_char += keycode - 0x47; 1195 goto check_composed; 1196 1197 case 0x4F: 1198 case 0x50: 1199 case 0x51: /* keypad 1,2,3 */ 1200 sc->sc_composed_char *= 10; 1201 sc->sc_composed_char += keycode - 0x4E; 1202 goto check_composed; 1203 1204 case 0x52: /* keypad 0 */ 1205 sc->sc_composed_char *= 10; 1206 goto check_composed; 1207 1208 /* key released, no interest here */ 1209 case SCAN_RELEASE | 0x47: 1210 case SCAN_RELEASE | 0x48: 1211 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */ 1212 case SCAN_RELEASE | 0x4B: 1213 case SCAN_RELEASE | 0x4C: 1214 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */ 1215 case SCAN_RELEASE | 0x4F: 1216 case SCAN_RELEASE | 0x50: 1217 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */ 1218 case SCAN_RELEASE | 0x52: /* keypad 0 */ 1219 goto next_code; 1220 1221 case 0x38: /* left alt key */ 1222 break; 1223 1224 default: 1225 if (sc->sc_composed_char > 0) { 1226 sc->sc_flags &= ~UKBD_FLAG_COMPOSE; 1227 sc->sc_composed_char = 0; 1228 goto errkey; 1229 } 1230 break; 1231 } 1232 } 1233 /* keycode to key action */ 1234 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode), 1235 (keycode & SCAN_RELEASE), 1236 &sc->sc_state, &sc->sc_accents); 1237 if (action == NOKEY) { 1238 goto next_code; 1239 } 1240 done: 1241 return (action); 1242 1243 check_composed: 1244 if (sc->sc_composed_char <= 0xFF) { 1245 goto next_code; 1246 } 1247 errkey: 1248 return (ERRKEY); 1249 } 1250 1251 /* some useful control functions */ 1252 static int 1253 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) 1254 { 1255 /* translate LED_XXX bits into the device specific bits */ 1256 static const uint8_t ledmap[8] = { 1257 0, 2, 1, 3, 4, 6, 5, 7, 1258 }; 1259 struct ukbd_softc *sc = kbd->kb_data; 1260 int i; 1261 1262 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1263 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1264 int ival; 1265 1266 #endif 1267 if (!mtx_owned(&Giant)) { 1268 /* 1269 * XXX big problem: If scroll lock is pressed and "printf()" 1270 * is called, the CPU will get here, to un-scroll lock the 1271 * keyboard. But if "printf()" acquires the "Giant" lock, 1272 * there will be a locking order reversal problem, so the 1273 * keyboard system must get out of "Giant" first, before the 1274 * CPU can proceed here ... 1275 */ 1276 return (EINVAL); 1277 } 1278 mtx_assert(&Giant, MA_OWNED); 1279 1280 switch (cmd) { 1281 case KDGKBMODE: /* get keyboard mode */ 1282 *(int *)arg = sc->sc_mode; 1283 break; 1284 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1285 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1286 case _IO('K', 7): 1287 ival = IOCPARM_IVAL(arg); 1288 arg = (caddr_t)&ival; 1289 /* FALLTHROUGH */ 1290 #endif 1291 case KDSKBMODE: /* set keyboard mode */ 1292 switch (*(int *)arg) { 1293 case K_XLATE: 1294 if (sc->sc_mode != K_XLATE) { 1295 /* make lock key state and LED state match */ 1296 sc->sc_state &= ~LOCK_MASK; 1297 sc->sc_state |= KBD_LED_VAL(kbd); 1298 } 1299 /* FALLTHROUGH */ 1300 case K_RAW: 1301 case K_CODE: 1302 if (sc->sc_mode != *(int *)arg) { 1303 ukbd_clear_state(kbd); 1304 sc->sc_mode = *(int *)arg; 1305 } 1306 break; 1307 default: 1308 return (EINVAL); 1309 } 1310 break; 1311 1312 case KDGETLED: /* get keyboard LED */ 1313 *(int *)arg = KBD_LED_VAL(kbd); 1314 break; 1315 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1316 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1317 case _IO('K', 66): 1318 ival = IOCPARM_IVAL(arg); 1319 arg = (caddr_t)&ival; 1320 /* FALLTHROUGH */ 1321 #endif 1322 case KDSETLED: /* set keyboard LED */ 1323 /* NOTE: lock key state in "sc_state" won't be changed */ 1324 if (*(int *)arg & ~LOCK_MASK) { 1325 return (EINVAL); 1326 } 1327 i = *(int *)arg; 1328 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */ 1329 if (sc->sc_mode == K_XLATE && 1330 kbd->kb_keymap->n_keys > ALTGR_OFFSET) { 1331 if (i & ALKED) 1332 i |= CLKED; 1333 else 1334 i &= ~CLKED; 1335 } 1336 if (KBD_HAS_DEVICE(kbd)) { 1337 ukbd_set_leds(sc, ledmap[i & LED_MASK]); 1338 } 1339 KBD_LED_VAL(kbd) = *(int *)arg; 1340 break; 1341 case KDGKBSTATE: /* get lock key state */ 1342 *(int *)arg = sc->sc_state & LOCK_MASK; 1343 break; 1344 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1345 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1346 case _IO('K', 20): 1347 ival = IOCPARM_IVAL(arg); 1348 arg = (caddr_t)&ival; 1349 /* FALLTHROUGH */ 1350 #endif 1351 case KDSKBSTATE: /* set lock key state */ 1352 if (*(int *)arg & ~LOCK_MASK) { 1353 return (EINVAL); 1354 } 1355 sc->sc_state &= ~LOCK_MASK; 1356 sc->sc_state |= *(int *)arg; 1357 1358 /* set LEDs and quit */ 1359 return (ukbd_ioctl(kbd, KDSETLED, arg)); 1360 1361 case KDSETREPEAT: /* set keyboard repeat rate (new 1362 * interface) */ 1363 if (!KBD_HAS_DEVICE(kbd)) { 1364 return (0); 1365 } 1366 if (((int *)arg)[1] < 0) { 1367 return (EINVAL); 1368 } 1369 if (((int *)arg)[0] < 0) { 1370 return (EINVAL); 1371 } 1372 if (((int *)arg)[0] < 200) /* fastest possible value */ 1373 kbd->kb_delay1 = 200; 1374 else 1375 kbd->kb_delay1 = ((int *)arg)[0]; 1376 kbd->kb_delay2 = ((int *)arg)[1]; 1377 return (0); 1378 1379 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1380 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1381 case _IO('K', 67): 1382 ival = IOCPARM_IVAL(arg); 1383 arg = (caddr_t)&ival; 1384 /* FALLTHROUGH */ 1385 #endif 1386 case KDSETRAD: /* set keyboard repeat rate (old 1387 * interface) */ 1388 return (ukbd_set_typematic(kbd, *(int *)arg)); 1389 1390 case PIO_KEYMAP: /* set keyboard translation table */ 1391 case PIO_KEYMAPENT: /* set keyboard translation table 1392 * entry */ 1393 case PIO_DEADKEYMAP: /* set accent key translation table */ 1394 sc->sc_accents = 0; 1395 /* FALLTHROUGH */ 1396 default: 1397 return (genkbd_commonioctl(kbd, cmd, arg)); 1398 } 1399 1400 return (0); 1401 } 1402 1403 /* clear the internal state of the keyboard */ 1404 static void 1405 ukbd_clear_state(keyboard_t *kbd) 1406 { 1407 struct ukbd_softc *sc = kbd->kb_data; 1408 1409 if (!mtx_owned(&Giant)) { 1410 return; /* XXX */ 1411 } 1412 mtx_assert(&Giant, MA_OWNED); 1413 1414 sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING); 1415 sc->sc_state &= LOCK_MASK; /* preserve locking key state */ 1416 sc->sc_accents = 0; 1417 sc->sc_composed_char = 0; 1418 #ifdef UKBD_EMULATE_ATSCANCODE 1419 sc->sc_buffered_char[0] = 0; 1420 sc->sc_buffered_char[1] = 0; 1421 #endif 1422 bzero(&sc->sc_ndata, sizeof(sc->sc_ndata)); 1423 bzero(&sc->sc_odata, sizeof(sc->sc_odata)); 1424 bzero(&sc->sc_ntime, sizeof(sc->sc_ntime)); 1425 bzero(&sc->sc_otime, sizeof(sc->sc_otime)); 1426 } 1427 1428 /* save the internal state, not used */ 1429 static int 1430 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len) 1431 { 1432 mtx_assert(&Giant, MA_OWNED); 1433 return (len == 0) ? 1 : -1; 1434 } 1435 1436 /* set the internal state, not used */ 1437 static int 1438 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len) 1439 { 1440 mtx_assert(&Giant, MA_OWNED); 1441 return (EINVAL); 1442 } 1443 1444 static int 1445 ukbd_poll(keyboard_t *kbd, int on) 1446 { 1447 struct ukbd_softc *sc = kbd->kb_data; 1448 1449 if (!mtx_owned(&Giant)) { 1450 return (0); /* XXX */ 1451 } 1452 mtx_assert(&Giant, MA_OWNED); 1453 1454 if (on) { 1455 sc->sc_flags |= UKBD_FLAG_POLLING; 1456 } else { 1457 sc->sc_flags &= ~UKBD_FLAG_POLLING; 1458 } 1459 return (0); 1460 } 1461 1462 /* local functions */ 1463 1464 static void 1465 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds) 1466 { 1467 DPRINTF("leds=0x%02x\n", leds); 1468 1469 sc->sc_leds = leds; 1470 sc->sc_flags |= UKBD_FLAG_SET_LEDS; 1471 1472 /* start transfer, if not already started */ 1473 1474 usb2_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]); 1475 } 1476 1477 static int 1478 ukbd_set_typematic(keyboard_t *kbd, int code) 1479 { 1480 static const int delays[] = {250, 500, 750, 1000}; 1481 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63, 1482 68, 76, 84, 92, 100, 110, 118, 126, 1483 136, 152, 168, 184, 200, 220, 236, 252, 1484 272, 304, 336, 368, 400, 440, 472, 504}; 1485 1486 if (code & ~0x7f) { 1487 return (EINVAL); 1488 } 1489 kbd->kb_delay1 = delays[(code >> 5) & 3]; 1490 kbd->kb_delay2 = rates[code & 0x1f]; 1491 return (0); 1492 } 1493 1494 #ifdef UKBD_EMULATE_ATSCANCODE 1495 static int 1496 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up) 1497 { 1498 static const int scan[] = { 1499 0x1c, 0x1d, 0x35, 1500 0x37 | SCAN_PREFIX_SHIFT, /* PrintScreen */ 1501 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f, 1502 0x50, 0x51, 0x52, 0x53, 1503 0x46, /* XXX Pause/Break */ 1504 0x5b, 0x5c, 0x5d, 1505 /* SUN TYPE 6 USB KEYBOARD */ 1506 0x68, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 1507 0x64, 0x65, 0x66, 0x67, 0x25, 0x1f, 0x1e, 1508 0x20, 1509 }; 1510 1511 if ((code >= 89) && (code < (89 + (sizeof(scan) / sizeof(scan[0]))))) { 1512 code = scan[code - 89] | SCAN_PREFIX_E0; 1513 } 1514 /* Pause/Break */ 1515 if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) { 1516 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL); 1517 } 1518 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) { 1519 code &= ~SCAN_PREFIX_SHIFT; 1520 } 1521 code |= (up ? SCAN_RELEASE : SCAN_PRESS); 1522 1523 if (code & SCAN_PREFIX) { 1524 if (code & SCAN_PREFIX_CTL) { 1525 /* Ctrl */ 1526 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE)); 1527 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX); 1528 } else if (code & SCAN_PREFIX_SHIFT) { 1529 /* Shift */ 1530 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE)); 1531 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT); 1532 } else { 1533 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX); 1534 sc->sc_buffered_char[1] = 0; 1535 } 1536 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); 1537 } 1538 return (code); 1539 1540 } 1541 1542 #endif /* UKBD_EMULATE_ATSCANCODE */ 1543 1544 keyboard_switch_t ukbdsw = { 1545 .probe = &ukbd__probe, 1546 .init = &ukbd_init, 1547 .term = &ukbd_term, 1548 .intr = &ukbd_intr, 1549 .test_if = &ukbd_test_if, 1550 .enable = &ukbd_enable, 1551 .disable = &ukbd_disable, 1552 .read = &ukbd_read, 1553 .check = &ukbd_check, 1554 .read_char = &ukbd_read_char, 1555 .check_char = &ukbd_check_char, 1556 .ioctl = &ukbd_ioctl, 1557 .lock = &ukbd_lock, 1558 .clear_state = &ukbd_clear_state, 1559 .get_state = &ukbd_get_state, 1560 .set_state = &ukbd_set_state, 1561 .get_fkeystr = &genkbd_get_fkeystr, 1562 .poll = &ukbd_poll, 1563 .diag = &genkbd_diag, 1564 }; 1565 1566 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure); 1567 1568 static int 1569 ukbd_driver_load(module_t mod, int what, void *arg) 1570 { 1571 switch (what) { 1572 case MOD_LOAD: 1573 kbd_add_driver(&ukbd_kbd_driver); 1574 break; 1575 case MOD_UNLOAD: 1576 kbd_delete_driver(&ukbd_kbd_driver); 1577 break; 1578 } 1579 return (0); 1580 } 1581 1582 static devclass_t ukbd_devclass; 1583 1584 static device_method_t ukbd_methods[] = { 1585 DEVMETHOD(device_probe, ukbd_probe), 1586 DEVMETHOD(device_attach, ukbd_attach), 1587 DEVMETHOD(device_detach, ukbd_detach), 1588 DEVMETHOD(device_resume, ukbd_resume), 1589 {0, 0} 1590 }; 1591 1592 static driver_t ukbd_driver = { 1593 .name = "ukbd", 1594 .methods = ukbd_methods, 1595 .size = sizeof(struct ukbd_softc), 1596 }; 1597 1598 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0); 1599 MODULE_DEPEND(ukbd, usb, 1, 1, 1); 1600