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