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