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