1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (lennart@augustsson.net) at 9 * Carlstedt Research & Technology. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 * 32 */ 33 34 /* 35 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf 36 */ 37 38 #include "opt_kbd.h" 39 #include "opt_ukbd.h" 40 #include "opt_evdev.h" 41 42 #include <sys/stdint.h> 43 #include <sys/stddef.h> 44 #include <sys/param.h> 45 #include <sys/queue.h> 46 #include <sys/types.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/bus.h> 50 #include <sys/module.h> 51 #include <sys/lock.h> 52 #include <sys/mutex.h> 53 #include <sys/condvar.h> 54 #include <sys/sysctl.h> 55 #include <sys/sx.h> 56 #include <sys/unistd.h> 57 #include <sys/callout.h> 58 #include <sys/malloc.h> 59 #include <sys/priv.h> 60 #include <sys/proc.h> 61 62 #include <dev/hid/hid.h> 63 64 #include <dev/usb/usb.h> 65 #include <dev/usb/usbdi.h> 66 #include <dev/usb/usbdi_util.h> 67 #include <dev/usb/usbhid.h> 68 69 #define USB_DEBUG_VAR ukbd_debug 70 #include <dev/usb/usb_debug.h> 71 72 #include <dev/usb/quirk/usb_quirk.h> 73 74 #include "usbdevs.h" 75 76 #ifdef EVDEV_SUPPORT 77 #include <dev/evdev/input.h> 78 #include <dev/evdev/evdev.h> 79 #endif 80 81 #include <sys/ioccom.h> 82 #include <sys/filio.h> 83 #include <sys/kbio.h> 84 85 #include <dev/kbd/kbdreg.h> 86 87 /* the initial key map, accent map and fkey strings */ 88 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE) 89 #define KBD_DFLT_KEYMAP 90 #include "ukbdmap.h" 91 #endif 92 93 /* the following file must be included after "ukbdmap.h" */ 94 #include <dev/kbd/kbdtables.h> 95 96 #ifdef USB_DEBUG 97 static int ukbd_debug = 0; 98 #endif 99 static int ukbd_no_leds = 0; 100 static int ukbd_pollrate = 0; 101 static int ukbd_apple_fn_mode = 0; 102 103 static SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 104 "USB keyboard"); 105 #ifdef USB_DEBUG 106 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RWTUN, 107 &ukbd_debug, 0, "Debug level"); 108 #endif 109 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, no_leds, CTLFLAG_RWTUN, 110 &ukbd_no_leds, 0, "Disables setting of keyboard leds"); 111 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, pollrate, CTLFLAG_RWTUN, 112 &ukbd_pollrate, 0, "Force this polling rate, 1-1000Hz"); 113 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, apple_fn_mode, CTLFLAG_RWTUN, 114 &ukbd_apple_fn_mode, 0, "0 = Fn + F1..12 -> media, 1 = F1..F12 -> media"); 115 116 #define UKBD_EMULATE_ATSCANCODE 1 117 #define UKBD_DRIVER_NAME "ukbd" 118 #define UKBD_NKEYCODE 256 /* units */ 119 #define UKBD_IN_BUF_SIZE (4 * UKBD_NKEYCODE) /* scancodes */ 120 #define UKBD_IN_BUF_FULL ((UKBD_IN_BUF_SIZE / 2) - 1) /* scancodes */ 121 #define UKBD_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */ 122 #define UKBD_BUFFER_SIZE 64 /* bytes */ 123 #define UKBD_KEY_PRESSED(map, key) ({ \ 124 CTASSERT((key) >= 0 && (key) < UKBD_NKEYCODE); \ 125 ((map)[(key) / 64] & (1ULL << ((key) % 64))); \ 126 }) 127 128 #define MOD_EJECT 0x01 129 #define MOD_FN 0x02 130 131 /* check evdev_usb_scancodes[] for names */ 132 #define APPLE_FN_KEY 0xff 133 #define APPLE_EJECT_KEY 0xec 134 135 struct ukbd_data { 136 uint64_t bitmap[howmany(UKBD_NKEYCODE, 64)]; 137 }; 138 139 enum { 140 UKBD_INTR_DT_0, 141 UKBD_INTR_DT_1, 142 UKBD_CTRL_LED, 143 UKBD_N_TRANSFER, 144 }; 145 146 struct ukbd_softc { 147 keyboard_t sc_kbd; 148 keymap_t sc_keymap; 149 accentmap_t sc_accmap; 150 fkeytab_t sc_fkeymap[UKBD_NFKEY]; 151 uint64_t sc_loc_key_valid[howmany(UKBD_NKEYCODE, 64)]; 152 struct hid_location sc_loc_apple_eject; 153 struct hid_location sc_loc_apple_fn; 154 struct hid_location sc_loc_key[UKBD_NKEYCODE]; 155 struct hid_location sc_loc_numlock; 156 struct hid_location sc_loc_capslock; 157 struct hid_location sc_loc_scrolllock; 158 struct usb_callout sc_callout; 159 struct ukbd_data sc_ndata; 160 struct ukbd_data sc_odata; 161 162 struct thread *sc_poll_thread; 163 struct usb_device *sc_udev; 164 struct usb_interface *sc_iface; 165 struct usb_xfer *sc_xfer[UKBD_N_TRANSFER]; 166 #ifdef EVDEV_SUPPORT 167 struct evdev_dev *sc_evdev; 168 #endif 169 170 sbintime_t sc_co_basetime; 171 int sc_delay; 172 uint32_t sc_repeat_time; 173 uint32_t sc_input[UKBD_IN_BUF_SIZE]; /* input buffer */ 174 uint32_t sc_time_ms; 175 uint32_t sc_composed_char; /* composed char code, if non-zero */ 176 #ifdef UKBD_EMULATE_ATSCANCODE 177 uint32_t sc_buffered_char[2]; 178 #endif 179 uint32_t sc_flags; /* flags */ 180 #define UKBD_FLAG_COMPOSE 0x00000001 181 #define UKBD_FLAG_POLLING 0x00000002 182 #define UKBD_FLAG_SET_LEDS 0x00000004 183 #define UKBD_FLAG_ATTACHED 0x00000010 184 #define UKBD_FLAG_GONE 0x00000020 185 186 /* set in ukbd_attach */ 187 #define UKBD_FLAG_APPLE_SWAP 0x00000040 188 /* set in ukbd_parse_hid */ 189 #define UKBD_FLAG_APPLE_EJECT 0x00000080 190 #define UKBD_FLAG_APPLE_FN 0x00000100 191 #define UKBD_FLAG_NUMLOCK 0x00080000 192 #define UKBD_FLAG_CAPSLOCK 0x00100000 193 #define UKBD_FLAG_SCROLLLOCK 0x00200000 194 #define UKBD_FLAG_HID_MASK UKBD_FLAG_APPLE_EJECT | \ 195 UKBD_FLAG_APPLE_FN | \ 196 UKBD_FLAG_NUMLOCK | \ 197 UKBD_FLAG_CAPSLOCK | \ 198 UKBD_FLAG_SCROLLLOCK 199 200 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */ 201 int sc_state; /* shift/lock key state */ 202 int sc_accents; /* accent key index (> 0) */ 203 int sc_polling; /* polling recursion count */ 204 int sc_led_size; 205 int sc_kbd_size; 206 207 uint16_t sc_inputs; 208 uint16_t sc_inputhead; 209 uint16_t sc_inputtail; 210 uint16_t sc_vendor_id; 211 212 uint8_t sc_leds; /* store for async led requests */ 213 uint8_t sc_iface_index; 214 uint8_t sc_iface_no; 215 uint8_t sc_id_apple_eject; 216 uint8_t sc_id_apple_fn; 217 uint8_t sc_id_loc_key[UKBD_NKEYCODE]; 218 uint8_t sc_id_numlock; 219 uint8_t sc_id_capslock; 220 uint8_t sc_id_scrolllock; 221 uint8_t sc_kbd_id; 222 uint8_t sc_repeat_key; 223 224 uint8_t sc_buffer[UKBD_BUFFER_SIZE]; 225 }; 226 227 #define KEY_NONE 0x00 228 #define KEY_ERROR 0x01 229 230 #define KEY_PRESS 0 231 #define KEY_RELEASE 0x400 232 #define KEY_INDEX(c) ((c) & 0xFF) 233 234 #define SCAN_PRESS 0 235 #define SCAN_RELEASE 0x80 236 #define SCAN_PREFIX_E0 0x100 237 #define SCAN_PREFIX_E1 0x200 238 #define SCAN_PREFIX_CTL 0x400 239 #define SCAN_PREFIX_SHIFT 0x800 240 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \ 241 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT) 242 #define SCAN_CHAR(c) ((c) & 0x7f) 243 244 #define UKBD_LOCK() USB_MTX_LOCK(&Giant) 245 #define UKBD_UNLOCK() USB_MTX_UNLOCK(&Giant) 246 #define UKBD_LOCK_ASSERT() USB_MTX_ASSERT(&Giant, MA_OWNED) 247 248 #define NN 0 /* no translation */ 249 /* 250 * Translate USB keycodes to AT keyboard scancodes. 251 */ 252 /* 253 * FIXME: Mac USB keyboard generates: 254 * 0x53: keypad NumLock/Clear 255 * 0x66: Power 256 * 0x67: keypad = 257 * 0x68: F13 258 * 0x69: F14 259 * 0x6a: F15 260 * 261 * USB Apple Keyboard JIS generates: 262 * 0x90: Kana 263 * 0x91: Eisu 264 */ 265 static const uint8_t ukbd_trtab[256] = { 266 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */ 267 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */ 268 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */ 269 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */ 270 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */ 271 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */ 272 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */ 273 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */ 274 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */ 275 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */ 276 97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */ 277 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */ 278 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */ 279 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */ 280 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */ 281 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */ 282 121, 120, NN, NN, NN, NN, NN, 123, /* 80 - 87 */ 283 124, 125, 126, 127, 128, NN, NN, NN, /* 88 - 8F */ 284 129, 130, NN, NN, NN, NN, NN, NN, /* 90 - 97 */ 285 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */ 286 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */ 287 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */ 288 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */ 289 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */ 290 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */ 291 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */ 292 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */ 293 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */ 294 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */ 295 NN, NN, NN, NN, 254, NN, NN, NN, /* E8 - EF */ 296 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */ 297 NN, NN, NN, NN, NN, NN, NN, 255, /* F8 - FF */ 298 }; 299 300 static const uint8_t ukbd_boot_desc[] = { 301 0x05, 0x01, 0x09, 0x06, 0xa1, 302 0x01, 0x05, 0x07, 0x19, 0xe0, 303 0x29, 0xe7, 0x15, 0x00, 0x25, 304 0x01, 0x75, 0x01, 0x95, 0x08, 305 0x81, 0x02, 0x95, 0x01, 0x75, 306 0x08, 0x81, 0x01, 0x95, 0x03, 307 0x75, 0x01, 0x05, 0x08, 0x19, 308 0x01, 0x29, 0x03, 0x91, 0x02, 309 0x95, 0x05, 0x75, 0x01, 0x91, 310 0x01, 0x95, 0x06, 0x75, 0x08, 311 0x15, 0x00, 0x26, 0xff, 0x00, 312 0x05, 0x07, 0x19, 0x00, 0x2a, 313 0xff, 0x00, 0x81, 0x00, 0xc0 314 }; 315 316 static const STRUCT_USB_HOST_ID ukbd_apple_iso_models[] = { 317 /* PowerBooks Feb 2005, iBooks G4 */ 318 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_FOUNTAIN_ISO) }, 319 /* PowerBooks Oct 2005 */ 320 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_GEYSER_ISO) }, 321 /* Core Duo MacBook & MacBook Pro */ 322 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_GEYSER3_ISO) }, 323 /* Core2 Duo MacBook & MacBook Pro */ 324 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_GEYSER4_ISO) }, 325 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_GEYSER4_HF_ISO) }, 326 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_ALU_MINI_ISO) }, 327 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_ALU_ISO) }, 328 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_ALU_REVB_ISO) }, 329 /* MacbookAir, aka wellspring */ 330 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_WELLSPRING_ISO) }, 331 /* MacbookProPenryn, aka wellspring2 */ 332 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_WELLSPRING2_ISO) }, 333 /* Macbook5,1 (unibody), aka wellspring3 */ 334 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_WELLSPRING3_ISO) }, 335 /* MacbookAir3,2 (unibody), aka wellspring4 */ 336 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_WELLSPRING4_ISO) }, 337 /* MacbookAir3,1 (unibody), aka wellspring4 */ 338 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_WELLSPRING4A_ISO) }, 339 /* Macbook8 (unibody, March 2011) */ 340 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_WELLSPRING5_ISO) }, 341 /* Macbook8,2 (unibody) */ 342 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_WELLSPRING5A_ISO) }, 343 /* MacbookAir4,2 (unibody, July 2011) */ 344 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_WELLSPRING6_ISO) }, 345 /* MacbookAir4,1 (unibody, July 2011) */ 346 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_WELLSPRING6A_ISO) }, 347 /* MacbookPro10,1 (unibody, June 2012) */ 348 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_WELLSPRING7_ISO) }, 349 /* MacbookPro10,2 (unibody, October 2012) */ 350 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_WELLSPRING7A_ISO) }, 351 /* MacbookAir6,2 (unibody, June 2013) */ 352 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_WELLSPRING8_ISO) }, 353 /* MacbookPro12,1 */ 354 { USB_VP(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_WELLSPRING9_ISO) }, 355 }; 356 357 358 /* prototypes */ 359 static void ukbd_timeout(void *); 360 static void ukbd_set_leds(struct ukbd_softc *, uint8_t); 361 static int ukbd_set_typematic(keyboard_t *, int); 362 #ifdef UKBD_EMULATE_ATSCANCODE 363 static uint32_t ukbd_atkeycode(int, const uint64_t *); 364 static int ukbd_key2scan(struct ukbd_softc *, int, const uint64_t *, int); 365 #endif 366 static uint32_t ukbd_read_char(keyboard_t *, int); 367 static void ukbd_clear_state(keyboard_t *); 368 static int ukbd_ioctl(keyboard_t *, u_long, caddr_t); 369 static int ukbd_enable(keyboard_t *); 370 static int ukbd_disable(keyboard_t *); 371 static void ukbd_interrupt(struct ukbd_softc *); 372 static void ukbd_event_keyinput(struct ukbd_softc *); 373 374 static device_probe_t ukbd_probe; 375 static device_attach_t ukbd_attach; 376 static device_detach_t ukbd_detach; 377 static device_resume_t ukbd_resume; 378 379 #ifdef EVDEV_SUPPORT 380 static evdev_event_t ukbd_ev_event; 381 382 static const struct evdev_methods ukbd_evdev_methods = { 383 .ev_event = ukbd_ev_event, 384 }; 385 #endif 386 387 static bool 388 ukbd_any_key_pressed(struct ukbd_softc *sc) 389 { 390 bool ret = false; 391 unsigned i; 392 393 for (i = 0; i != howmany(UKBD_NKEYCODE, 64); i++) 394 ret |= (sc->sc_odata.bitmap[i] != 0); 395 return (ret); 396 } 397 398 static bool 399 ukbd_any_key_valid(struct ukbd_softc *sc) 400 { 401 bool ret = false; 402 unsigned i; 403 404 for (i = 0; i != howmany(UKBD_NKEYCODE, 64); i++) 405 ret |= (sc->sc_loc_key_valid[i] != 0); 406 return (ret); 407 } 408 409 static bool 410 ukbd_is_modifier_key(uint32_t key) 411 { 412 413 return (key >= 0xe0 && key <= 0xe7); 414 } 415 416 static void 417 ukbd_start_timer(struct ukbd_softc *sc) 418 { 419 sbintime_t delay, now, prec; 420 421 now = sbinuptime(); 422 423 /* check if initial delay passed and fallback to key repeat delay */ 424 if (sc->sc_delay == 0) 425 sc->sc_delay = sc->sc_kbd.kb_delay2; 426 427 /* compute timeout */ 428 delay = SBT_1MS * sc->sc_delay; 429 sc->sc_co_basetime += delay; 430 431 /* check if we are running behind */ 432 if (sc->sc_co_basetime < now) 433 sc->sc_co_basetime = now; 434 435 /* This is rarely called, so prefer precision to efficiency. */ 436 prec = qmin(delay >> 7, SBT_1MS * 10); 437 usb_callout_reset_sbt(&sc->sc_callout, sc->sc_co_basetime, prec, 438 ukbd_timeout, sc, C_ABSOLUTE); 439 } 440 441 static void 442 ukbd_put_key(struct ukbd_softc *sc, uint32_t key) 443 { 444 445 UKBD_LOCK_ASSERT(); 446 447 DPRINTF("0x%02x (%d) %s\n", key, key, 448 (key & KEY_RELEASE) ? "released" : "pressed"); 449 450 #ifdef EVDEV_SUPPORT 451 if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && sc->sc_evdev != NULL) 452 evdev_push_event(sc->sc_evdev, EV_KEY, 453 evdev_hid2key(KEY_INDEX(key)), !(key & KEY_RELEASE)); 454 if (sc->sc_evdev != NULL && evdev_is_grabbed(sc->sc_evdev)) 455 return; 456 #endif 457 458 if (sc->sc_inputs < UKBD_IN_BUF_SIZE) { 459 sc->sc_input[sc->sc_inputtail] = key; 460 ++(sc->sc_inputs); 461 ++(sc->sc_inputtail); 462 if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) { 463 sc->sc_inputtail = 0; 464 } 465 } else { 466 DPRINTF("input buffer is full\n"); 467 } 468 } 469 470 static void 471 ukbd_do_poll(struct ukbd_softc *sc, uint8_t wait) 472 { 473 474 UKBD_LOCK_ASSERT(); 475 KASSERT((sc->sc_flags & UKBD_FLAG_POLLING) != 0, 476 ("ukbd_do_poll called when not polling\n")); 477 DPRINTFN(2, "polling\n"); 478 479 if (USB_IN_POLLING_MODE_FUNC() == 0) { 480 /* 481 * In this context the kernel is polling for input, 482 * but the USB subsystem works in normal interrupt-driven 483 * mode, so we just wait on the USB threads to do the job. 484 * Note that we currently hold the Giant, but it's also used 485 * as the transfer mtx, so we must release it while waiting. 486 */ 487 while (sc->sc_inputs == 0) { 488 /* 489 * Give USB threads a chance to run. Note that 490 * kern_yield performs DROP_GIANT + PICKUP_GIANT. 491 */ 492 kern_yield(PRI_UNCHANGED); 493 if (!wait) 494 break; 495 } 496 return; 497 } 498 499 while (sc->sc_inputs == 0) { 500 usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER); 501 502 /* Delay-optimised support for repetition of keys */ 503 if (ukbd_any_key_pressed(sc)) { 504 /* a key is pressed - need timekeeping */ 505 DELAY(1000); 506 507 /* 1 millisecond has passed */ 508 sc->sc_time_ms += 1; 509 } 510 511 ukbd_interrupt(sc); 512 513 if (!wait) 514 break; 515 } 516 } 517 518 static int32_t 519 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait) 520 { 521 int32_t c; 522 523 UKBD_LOCK_ASSERT(); 524 KASSERT((USB_IN_POLLING_MODE_FUNC() == 0) || 525 (sc->sc_flags & UKBD_FLAG_POLLING) != 0, 526 ("not polling in kdb or panic\n")); 527 528 if (sc->sc_inputs == 0 && 529 (sc->sc_flags & UKBD_FLAG_GONE) == 0) { 530 /* start transfer, if not already started */ 531 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_0]); 532 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_1]); 533 } 534 535 if (sc->sc_flags & UKBD_FLAG_POLLING) 536 ukbd_do_poll(sc, wait); 537 538 if (sc->sc_inputs == 0) { 539 c = -1; 540 } else { 541 c = sc->sc_input[sc->sc_inputhead]; 542 --(sc->sc_inputs); 543 ++(sc->sc_inputhead); 544 if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) { 545 sc->sc_inputhead = 0; 546 } 547 } 548 return (c); 549 } 550 551 static void 552 ukbd_interrupt(struct ukbd_softc *sc) 553 { 554 const uint32_t now = sc->sc_time_ms; 555 unsigned key; 556 557 UKBD_LOCK_ASSERT(); 558 559 /* Check for modifier key changes first */ 560 for (key = 0xe0; key != 0xe8; key++) { 561 const uint64_t mask = 1ULL << (key % 64); 562 const uint64_t delta = 563 sc->sc_odata.bitmap[key / 64] ^ 564 sc->sc_ndata.bitmap[key / 64]; 565 566 if (delta & mask) { 567 if (sc->sc_odata.bitmap[key / 64] & mask) 568 ukbd_put_key(sc, key | KEY_RELEASE); 569 else 570 ukbd_put_key(sc, key | KEY_PRESS); 571 } 572 } 573 574 /* Check for key changes */ 575 for (key = 0; key != UKBD_NKEYCODE; key++) { 576 const uint64_t mask = 1ULL << (key % 64); 577 const uint64_t delta = 578 sc->sc_odata.bitmap[key / 64] ^ 579 sc->sc_ndata.bitmap[key / 64]; 580 581 if (mask == 1 && delta == 0) { 582 key += 63; 583 continue; /* skip empty areas */ 584 } else if (ukbd_is_modifier_key(key)) { 585 continue; 586 } else if (delta & mask) { 587 if (sc->sc_odata.bitmap[key / 64] & mask) { 588 ukbd_put_key(sc, key | KEY_RELEASE); 589 590 /* clear repeating key, if any */ 591 if (sc->sc_repeat_key == key) 592 sc->sc_repeat_key = 0; 593 } else { 594 ukbd_put_key(sc, key | KEY_PRESS); 595 if (key != APPLE_FN_KEY) { 596 sc->sc_co_basetime = sbinuptime(); 597 sc->sc_delay = sc->sc_kbd.kb_delay1; 598 ukbd_start_timer(sc); 599 /* set repeat time for last key */ 600 sc->sc_repeat_time = now + sc->sc_kbd.kb_delay1; 601 sc->sc_repeat_key = key; 602 } 603 } 604 } 605 } 606 607 /* synchronize old data with new data */ 608 sc->sc_odata = sc->sc_ndata; 609 610 /* check if last key is still pressed */ 611 if (sc->sc_repeat_key != 0) { 612 const int32_t dtime = (sc->sc_repeat_time - now); 613 614 /* check if time has elapsed */ 615 if (dtime <= 0) { 616 ukbd_put_key(sc, sc->sc_repeat_key | KEY_PRESS); 617 sc->sc_repeat_time = now + sc->sc_kbd.kb_delay2; 618 } 619 } 620 621 #ifdef EVDEV_SUPPORT 622 if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && sc->sc_evdev != NULL) 623 evdev_sync(sc->sc_evdev); 624 if (sc->sc_evdev != NULL && evdev_is_grabbed(sc->sc_evdev)) 625 return; 626 #endif 627 628 /* wakeup keyboard system */ 629 ukbd_event_keyinput(sc); 630 } 631 632 static void 633 ukbd_event_keyinput(struct ukbd_softc *sc) 634 { 635 int c; 636 637 UKBD_LOCK_ASSERT(); 638 639 if ((sc->sc_flags & UKBD_FLAG_POLLING) != 0) 640 return; 641 642 if (sc->sc_inputs == 0) 643 return; 644 645 if (KBD_IS_ACTIVE(&sc->sc_kbd) && 646 KBD_IS_BUSY(&sc->sc_kbd)) { 647 /* let the callback function process the input */ 648 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT, 649 sc->sc_kbd.kb_callback.kc_arg); 650 } else { 651 /* read and discard the input, no one is waiting for it */ 652 do { 653 c = ukbd_read_char(&sc->sc_kbd, 0); 654 } while (c != NOKEY); 655 } 656 } 657 658 static void 659 ukbd_timeout(void *arg) 660 { 661 struct ukbd_softc *sc = arg; 662 663 UKBD_LOCK_ASSERT(); 664 665 sc->sc_time_ms += sc->sc_delay; 666 sc->sc_delay = 0; 667 668 ukbd_interrupt(sc); 669 670 /* Make sure any leftover key events gets read out */ 671 ukbd_event_keyinput(sc); 672 673 if (ukbd_any_key_pressed(sc) || (sc->sc_inputs != 0)) { 674 ukbd_start_timer(sc); 675 } 676 } 677 678 static uint32_t 679 ukbd_apple_fn(uint32_t keycode) 680 { 681 switch (keycode) { 682 case 0x0b: return 0x50; /* H -> LEFT ARROW */ 683 case 0x0d: return 0x51; /* J -> DOWN ARROW */ 684 case 0x0e: return 0x52; /* K -> UP ARROW */ 685 case 0x0f: return 0x4f; /* L -> RIGHT ARROW */ 686 case 0x36: return 0x4a; /* COMMA -> HOME */ 687 case 0x37: return 0x4d; /* DOT -> END */ 688 case 0x18: return 0x4b; /* U -> PGUP */ 689 case 0x07: return 0x4e; /* D -> PGDN */ 690 case 0x16: return 0x47; /* S -> SCROLLLOCK */ 691 case 0x13: return 0x46; /* P -> SYSRQ/PRTSC */ 692 case 0x28: return 0x49; /* RETURN -> INSERT */ 693 case 0x2a: return 0x4c; /* BACKSPACE -> DEL */ 694 case 0x50: return 0x4a; /* LEFT ARROW -> HOME */ 695 case 0x4f: return 0x4d; /* RIGHT ARROW -> END */ 696 case 0x52: return 0x4b; /* UP ARROW -> PGUP */ 697 case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */ 698 default: return keycode; 699 } 700 } 701 702 /* separate so the sysctl doesn't butcher non-fn keys */ 703 static uint32_t 704 ukbd_apple_fn_media(uint32_t keycode) 705 { 706 switch (keycode) { 707 case 0x3a: return 0xc0; /* F1 -> BRIGHTNESS DOWN */ 708 case 0x3b: return 0xc1; /* F2 -> BRIGHTNESS UP */ 709 case 0x3c: return 0xc2; /* F3 -> SCALE (MISSION CTRL)*/ 710 case 0x3d: return 0xc3; /* F4 -> DASHBOARD (LAUNCHPAD) */ 711 case 0x3e: return 0xc4; /* F5 -> KBD BACKLIGHT DOWN */ 712 case 0x3f: return 0xc5; /* F6 -> KBD BACKLIGHT UP */ 713 case 0x40: return 0xea; /* F7 -> MEDIA PREV */ 714 case 0x41: return 0xe8; /* F8 -> PLAY/PAUSE */ 715 case 0x42: return 0xeb; /* F9 -> MEDIA NEXT */ 716 case 0x43: return 0xef; /* F10 -> MUTE */ 717 case 0x44: return 0xee; /* F11 -> VOLUME DOWN */ 718 case 0x45: return 0xed; /* F12 -> VOLUME UP */ 719 default: return keycode; 720 } 721 } 722 723 static uint32_t 724 ukbd_apple_swap(uint32_t keycode) 725 { 726 switch (keycode) { 727 case 0x35: return 0x64; 728 case 0x64: return 0x35; 729 default: return keycode; 730 } 731 } 732 733 static void 734 ukbd_intr_callback(struct usb_xfer *xfer, usb_error_t error) 735 { 736 struct ukbd_softc *sc = usbd_xfer_softc(xfer); 737 struct usb_page_cache *pc; 738 uint32_t i; 739 uint8_t id; 740 uint8_t modifiers; 741 int offset; 742 int len; 743 744 UKBD_LOCK_ASSERT(); 745 746 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 747 pc = usbd_xfer_get_frame(xfer, 0); 748 749 switch (USB_GET_STATE(xfer)) { 750 case USB_ST_TRANSFERRED: 751 DPRINTF("actlen=%d bytes\n", len); 752 753 if (len == 0) { 754 DPRINTF("zero length data\n"); 755 goto tr_setup; 756 } 757 758 if (sc->sc_kbd_id != 0) { 759 /* check and remove HID ID byte */ 760 usbd_copy_out(pc, 0, &id, 1); 761 offset = 1; 762 len--; 763 if (len == 0) { 764 DPRINTF("zero length data\n"); 765 goto tr_setup; 766 } 767 } else { 768 offset = 0; 769 id = 0; 770 } 771 772 if (len > UKBD_BUFFER_SIZE) 773 len = UKBD_BUFFER_SIZE; 774 775 /* get data */ 776 usbd_copy_out(pc, offset, sc->sc_buffer, len); 777 778 /* clear temporary storage */ 779 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata)); 780 781 /* clear modifiers */ 782 modifiers = 0; 783 784 /* scan through HID data and expose magic apple keys */ 785 if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) && 786 (id == sc->sc_id_apple_eject)) { 787 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_eject)) { 788 sc->sc_ndata.bitmap[APPLE_EJECT_KEY / 64] |= 789 1ULL << (APPLE_EJECT_KEY % 64); 790 modifiers |= MOD_EJECT; 791 } else { 792 sc->sc_ndata.bitmap[APPLE_EJECT_KEY / 64] &= 793 ~(1ULL << (APPLE_EJECT_KEY % 64)); 794 } 795 } 796 if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) && 797 (id == sc->sc_id_apple_fn)) { 798 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_fn)) { 799 sc->sc_ndata.bitmap[APPLE_FN_KEY / 64] |= 800 1ULL << (APPLE_FN_KEY % 64); 801 modifiers |= MOD_FN; 802 } else { 803 sc->sc_ndata.bitmap[APPLE_FN_KEY / 64] &= 804 ~(1ULL << (APPLE_FN_KEY % 64)); 805 } 806 } 807 808 int apply_apple_fn_media = (modifiers & MOD_FN) ? 1 : 0; 809 if (ukbd_apple_fn_mode) /* toggle from sysctl value */ 810 apply_apple_fn_media = !apply_apple_fn_media; 811 812 for (i = 0; i != UKBD_NKEYCODE; i++) { 813 const uint64_t valid = sc->sc_loc_key_valid[i / 64]; 814 const uint64_t mask = 1ULL << (i % 64); 815 816 if (mask == 1 && valid == 0) { 817 i += 63; 818 continue; /* skip empty areas */ 819 } else if (~valid & mask) { 820 continue; /* location is not valid */ 821 } else if (id != sc->sc_id_loc_key[i]) { 822 continue; /* invalid HID ID */ 823 } else if (i == 0) { 824 struct hid_location tmp_loc = sc->sc_loc_key[0]; 825 /* range check array size */ 826 if (tmp_loc.count > UKBD_NKEYCODE) 827 tmp_loc.count = UKBD_NKEYCODE; 828 while (tmp_loc.count--) { 829 uint32_t key = 830 hid_get_udata(sc->sc_buffer, len, &tmp_loc); 831 /* advance to next location */ 832 tmp_loc.pos += tmp_loc.size; 833 if (key == KEY_ERROR) { 834 DPRINTF("KEY_ERROR\n"); 835 sc->sc_ndata = sc->sc_odata; 836 goto tr_setup; /* ignore */ 837 } 838 if (modifiers & MOD_FN) 839 key = ukbd_apple_fn(key); 840 if (apply_apple_fn_media) 841 key = ukbd_apple_fn_media(key); 842 if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) 843 key = ukbd_apple_swap(key); 844 if (key == KEY_NONE || key >= UKBD_NKEYCODE) 845 continue; 846 /* set key in bitmap */ 847 sc->sc_ndata.bitmap[key / 64] |= 1ULL << (key % 64); 848 } 849 } else if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_key[i])) { 850 uint32_t key = i; 851 852 if (modifiers & MOD_FN) 853 key = ukbd_apple_fn(key); 854 if (apply_apple_fn_media) 855 key = ukbd_apple_fn_media(key); 856 if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) 857 key = ukbd_apple_swap(key); 858 if (key == KEY_NONE || key == KEY_ERROR || key >= UKBD_NKEYCODE) 859 continue; 860 /* set key in bitmap */ 861 sc->sc_ndata.bitmap[key / 64] |= 1ULL << (key % 64); 862 } 863 } 864 #ifdef USB_DEBUG 865 DPRINTF("modifiers = 0x%04x\n", modifiers); 866 for (i = 0; i != UKBD_NKEYCODE; i++) { 867 const uint64_t valid = sc->sc_ndata.bitmap[i / 64]; 868 const uint64_t mask = 1ULL << (i % 64); 869 870 if (valid & mask) 871 DPRINTF("Key 0x%02x pressed\n", i); 872 } 873 #endif 874 ukbd_interrupt(sc); 875 876 case USB_ST_SETUP: 877 tr_setup: 878 if (sc->sc_inputs < UKBD_IN_BUF_FULL) { 879 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 880 usbd_transfer_submit(xfer); 881 } else { 882 DPRINTF("input queue is full!\n"); 883 } 884 break; 885 886 default: /* Error */ 887 DPRINTF("error=%s\n", usbd_errstr(error)); 888 889 if (error != USB_ERR_CANCELLED) { 890 /* try to clear stall first */ 891 usbd_xfer_set_stall(xfer); 892 goto tr_setup; 893 } 894 break; 895 } 896 } 897 898 static void 899 ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error) 900 { 901 struct ukbd_softc *sc = usbd_xfer_softc(xfer); 902 struct usb_device_request req; 903 struct usb_page_cache *pc; 904 uint8_t id; 905 uint8_t any; 906 int len; 907 908 UKBD_LOCK_ASSERT(); 909 910 #ifdef USB_DEBUG 911 if (ukbd_no_leds) 912 return; 913 #endif 914 915 switch (USB_GET_STATE(xfer)) { 916 case USB_ST_TRANSFERRED: 917 case USB_ST_SETUP: 918 if (!(sc->sc_flags & UKBD_FLAG_SET_LEDS)) 919 break; 920 sc->sc_flags &= ~UKBD_FLAG_SET_LEDS; 921 922 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 923 req.bRequest = UR_SET_REPORT; 924 USETW2(req.wValue, UHID_OUTPUT_REPORT, 0); 925 req.wIndex[0] = sc->sc_iface_no; 926 req.wIndex[1] = 0; 927 req.wLength[1] = 0; 928 929 memset(sc->sc_buffer, 0, UKBD_BUFFER_SIZE); 930 931 id = 0; 932 any = 0; 933 934 /* Assumption: All led bits must be in the same ID. */ 935 936 if (sc->sc_flags & UKBD_FLAG_NUMLOCK) { 937 if (sc->sc_leds & NLKED) { 938 hid_put_udata(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1, 939 &sc->sc_loc_numlock, 1); 940 } 941 id = sc->sc_id_numlock; 942 any = 1; 943 } 944 945 if (sc->sc_flags & UKBD_FLAG_SCROLLLOCK) { 946 if (sc->sc_leds & SLKED) { 947 hid_put_udata(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1, 948 &sc->sc_loc_scrolllock, 1); 949 } 950 id = sc->sc_id_scrolllock; 951 any = 1; 952 } 953 954 if (sc->sc_flags & UKBD_FLAG_CAPSLOCK) { 955 if (sc->sc_leds & CLKED) { 956 hid_put_udata(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1, 957 &sc->sc_loc_capslock, 1); 958 } 959 id = sc->sc_id_capslock; 960 any = 1; 961 } 962 963 /* if no leds, nothing to do */ 964 if (!any) 965 break; 966 967 /* range check output report length */ 968 len = sc->sc_led_size; 969 if (len > (UKBD_BUFFER_SIZE - 1)) 970 len = (UKBD_BUFFER_SIZE - 1); 971 972 /* check if we need to prefix an ID byte */ 973 sc->sc_buffer[0] = id; 974 975 pc = usbd_xfer_get_frame(xfer, 1); 976 if (id != 0) { 977 len++; 978 usbd_copy_in(pc, 0, sc->sc_buffer, len); 979 } else { 980 usbd_copy_in(pc, 0, sc->sc_buffer + 1, len); 981 } 982 req.wLength[0] = len; 983 usbd_xfer_set_frame_len(xfer, 1, len); 984 985 DPRINTF("len=%d, id=%d\n", len, id); 986 987 /* setup control request last */ 988 pc = usbd_xfer_get_frame(xfer, 0); 989 usbd_copy_in(pc, 0, &req, sizeof(req)); 990 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 991 992 /* start data transfer */ 993 usbd_xfer_set_frames(xfer, 2); 994 usbd_transfer_submit(xfer); 995 break; 996 997 default: /* Error */ 998 DPRINTFN(1, "error=%s\n", usbd_errstr(error)); 999 break; 1000 } 1001 } 1002 1003 static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = { 1004 [UKBD_INTR_DT_0] = { 1005 .type = UE_INTERRUPT, 1006 .endpoint = UE_ADDR_ANY, 1007 .direction = UE_DIR_IN, 1008 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 1009 .bufsize = 0, /* use wMaxPacketSize */ 1010 .callback = &ukbd_intr_callback, 1011 }, 1012 1013 [UKBD_INTR_DT_1] = { 1014 .type = UE_INTERRUPT, 1015 .endpoint = UE_ADDR_ANY, 1016 .direction = UE_DIR_IN, 1017 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 1018 .bufsize = 0, /* use wMaxPacketSize */ 1019 .callback = &ukbd_intr_callback, 1020 }, 1021 1022 [UKBD_CTRL_LED] = { 1023 .type = UE_CONTROL, 1024 .endpoint = 0x00, /* Control pipe */ 1025 .direction = UE_DIR_ANY, 1026 .bufsize = sizeof(struct usb_device_request) + UKBD_BUFFER_SIZE, 1027 .callback = &ukbd_set_leds_callback, 1028 .timeout = 1000, /* 1 second */ 1029 }, 1030 }; 1031 1032 /* A match on these entries will load ukbd */ 1033 static const STRUCT_USB_HOST_ID __used ukbd_devs[] = { 1034 {USB_IFACE_CLASS(UICLASS_HID), 1035 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT), 1036 USB_IFACE_PROTOCOL(UIPROTO_BOOT_KEYBOARD),}, 1037 }; 1038 1039 static int 1040 ukbd_probe(device_t dev) 1041 { 1042 keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME); 1043 struct usb_attach_arg *uaa = device_get_ivars(dev); 1044 void *d_ptr; 1045 int error; 1046 uint16_t d_len; 1047 1048 UKBD_LOCK_ASSERT(); 1049 DPRINTFN(11, "\n"); 1050 1051 if (sw == NULL) { 1052 return (ENXIO); 1053 } 1054 if (uaa->usb_mode != USB_MODE_HOST) { 1055 return (ENXIO); 1056 } 1057 1058 if (uaa->info.bInterfaceClass != UICLASS_HID) 1059 return (ENXIO); 1060 1061 if (usb_test_quirk(uaa, UQ_KBD_IGNORE)) 1062 return (ENXIO); 1063 1064 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) && 1065 (uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD)) 1066 return (BUS_PROBE_DEFAULT); 1067 1068 error = usbd_req_get_hid_desc(uaa->device, NULL, 1069 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex); 1070 1071 if (error) 1072 return (ENXIO); 1073 1074 if (hid_is_keyboard(d_ptr, d_len)) { 1075 if (hid_is_mouse(d_ptr, d_len)) { 1076 /* 1077 * NOTE: We currently don't support USB mouse 1078 * and USB keyboard on the same USB endpoint. 1079 * Let "ums" driver win. 1080 */ 1081 error = ENXIO; 1082 } else { 1083 error = BUS_PROBE_DEFAULT; 1084 } 1085 } else { 1086 error = ENXIO; 1087 } 1088 free(d_ptr, M_TEMP); 1089 return (error); 1090 } 1091 1092 static void 1093 ukbd_parse_hid(struct ukbd_softc *sc, const uint8_t *ptr, uint32_t len) 1094 { 1095 uint32_t flags; 1096 uint32_t key; 1097 1098 /* reset detected bits */ 1099 sc->sc_flags &= ~UKBD_FLAG_HID_MASK; 1100 1101 /* reset detected keys */ 1102 memset(sc->sc_loc_key_valid, 0, sizeof(sc->sc_loc_key_valid)); 1103 1104 /* check if there is an ID byte */ 1105 sc->sc_kbd_size = hid_report_size_max(ptr, len, 1106 hid_input, &sc->sc_kbd_id); 1107 1108 /* investigate if this is an Apple Keyboard */ 1109 if (sc->sc_vendor_id == USB_VENDOR_APPLE) { 1110 if (hid_locate(ptr, len, 1111 HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT), 1112 hid_input, 0, &sc->sc_loc_apple_eject, &flags, 1113 &sc->sc_id_apple_eject)) { 1114 if (flags & HIO_VARIABLE) 1115 sc->sc_flags |= UKBD_FLAG_APPLE_EJECT; 1116 DPRINTFN(1, "Found Apple eject-key\n"); 1117 } 1118 /* 1119 * check the same vendor pages that linux does to find the one 1120 * apple uses for the function key. 1121 */ 1122 static const uint16_t apple_pages[] = { 1123 HUP_APPLE, /* HID_UP_CUSTOM in linux */ 1124 HUP_MICROSOFT, /* HID_UP_MSVENDOR in linux */ 1125 HUP_HP, /* HID_UP_HPVENDOR2 in linux */ 1126 0xFFFF /* Original FreeBSD check (Remove?) */ 1127 }; 1128 for (int i = 0; i < (int)nitems(apple_pages); i++) { 1129 if (hid_locate(ptr, len, 1130 HID_USAGE2(apple_pages[i], 0x0003), 1131 hid_input, 0, &sc->sc_loc_apple_fn, &flags, 1132 &sc->sc_id_apple_fn)) { 1133 if (flags & HIO_VARIABLE) 1134 sc->sc_flags |= UKBD_FLAG_APPLE_FN; 1135 DPRINTFN(1, "Found Apple FN-key on page 0x%04x\n", 1136 apple_pages[i]); 1137 break; 1138 } 1139 } 1140 } 1141 1142 /* figure out event buffer */ 1143 if (hid_locate(ptr, len, 1144 HID_USAGE2(HUP_KEYBOARD, 0x00), 1145 hid_input, 0, &sc->sc_loc_key[0], &flags, 1146 &sc->sc_id_loc_key[0])) { 1147 if (flags & HIO_VARIABLE) { 1148 DPRINTFN(1, "Ignoring keyboard event control\n"); 1149 } else { 1150 sc->sc_loc_key_valid[0] |= 1; 1151 DPRINTFN(1, "Found keyboard event array\n"); 1152 } 1153 } 1154 1155 /* figure out the keys */ 1156 for (key = 1; key != UKBD_NKEYCODE; key++) { 1157 if (hid_locate(ptr, len, 1158 HID_USAGE2(HUP_KEYBOARD, key), 1159 hid_input, 0, &sc->sc_loc_key[key], &flags, 1160 &sc->sc_id_loc_key[key])) { 1161 if (flags & HIO_VARIABLE) { 1162 sc->sc_loc_key_valid[key / 64] |= 1163 1ULL << (key % 64); 1164 DPRINTFN(1, "Found key 0x%02x\n", key); 1165 } 1166 } 1167 } 1168 1169 /* figure out leds on keyboard */ 1170 sc->sc_led_size = hid_report_size_max(ptr, len, 1171 hid_output, NULL); 1172 1173 if (hid_locate(ptr, len, 1174 HID_USAGE2(HUP_LEDS, 0x01), 1175 hid_output, 0, &sc->sc_loc_numlock, &flags, 1176 &sc->sc_id_numlock)) { 1177 if (flags & HIO_VARIABLE) 1178 sc->sc_flags |= UKBD_FLAG_NUMLOCK; 1179 DPRINTFN(1, "Found keyboard numlock\n"); 1180 } 1181 if (hid_locate(ptr, len, 1182 HID_USAGE2(HUP_LEDS, 0x02), 1183 hid_output, 0, &sc->sc_loc_capslock, &flags, 1184 &sc->sc_id_capslock)) { 1185 if (flags & HIO_VARIABLE) 1186 sc->sc_flags |= UKBD_FLAG_CAPSLOCK; 1187 DPRINTFN(1, "Found keyboard capslock\n"); 1188 } 1189 if (hid_locate(ptr, len, 1190 HID_USAGE2(HUP_LEDS, 0x03), 1191 hid_output, 0, &sc->sc_loc_scrolllock, &flags, 1192 &sc->sc_id_scrolllock)) { 1193 if (flags & HIO_VARIABLE) 1194 sc->sc_flags |= UKBD_FLAG_SCROLLLOCK; 1195 DPRINTFN(1, "Found keyboard scrolllock\n"); 1196 } 1197 } 1198 1199 static int 1200 ukbd_attach(device_t dev) 1201 { 1202 struct ukbd_softc *sc = device_get_softc(dev); 1203 struct usb_attach_arg *uaa = device_get_ivars(dev); 1204 int unit = device_get_unit(dev); 1205 keyboard_t *kbd = &sc->sc_kbd; 1206 void *hid_ptr = NULL; 1207 usb_error_t err; 1208 uint16_t n; 1209 uint16_t hid_len; 1210 #ifdef EVDEV_SUPPORT 1211 struct evdev_dev *evdev; 1212 int i; 1213 #endif 1214 #ifdef USB_DEBUG 1215 int rate; 1216 #endif 1217 UKBD_LOCK_ASSERT(); 1218 1219 kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0); 1220 1221 kbd->kb_data = (void *)sc; 1222 1223 device_set_usb_desc(dev); 1224 1225 sc->sc_udev = uaa->device; 1226 sc->sc_iface = uaa->iface; 1227 sc->sc_vendor_id = uaa->info.idVendor; 1228 sc->sc_iface_index = uaa->info.bIfaceIndex; 1229 sc->sc_iface_no = uaa->info.bIfaceNum; 1230 sc->sc_mode = K_XLATE; 1231 1232 usb_callout_init_mtx(&sc->sc_callout, &Giant, 0); 1233 1234 #ifdef UKBD_NO_POLLING 1235 err = usbd_transfer_setup(uaa->device, 1236 &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config, 1237 UKBD_N_TRANSFER, sc, &Giant); 1238 #else 1239 /* 1240 * Setup the UKBD USB transfers one by one, so they are memory 1241 * independent which allows for handling panics triggered by 1242 * the keyboard driver itself, typically via CTRL+ALT+ESC 1243 * sequences. Or if the USB keyboard driver was processing a 1244 * key at the moment of panic. 1245 */ 1246 for (n = 0; n != UKBD_N_TRANSFER; n++) { 1247 err = usbd_transfer_setup(uaa->device, 1248 &uaa->info.bIfaceIndex, sc->sc_xfer + n, ukbd_config + n, 1249 1, sc, &Giant); 1250 if (err) 1251 break; 1252 } 1253 #endif 1254 1255 if (err) { 1256 DPRINTF("error=%s\n", usbd_errstr(err)); 1257 goto detach; 1258 } 1259 /* setup default keyboard maps */ 1260 1261 sc->sc_keymap = key_map; 1262 sc->sc_accmap = accent_map; 1263 for (n = 0; n < UKBD_NFKEY; n++) { 1264 sc->sc_fkeymap[n] = fkey_tab[n]; 1265 } 1266 1267 /* check if this is an Apple keyboard with swapped key codes 1268 * apparently, these are the ISO layout models 1269 */ 1270 DPRINTF("uaa vendor: 0x%04x, uaa product 0x%04x\n", uaa->info.idVendor, uaa->info.idProduct ); 1271 if (usbd_lookup_id_by_uaa(ukbd_apple_iso_models, sizeof(ukbd_apple_iso_models), uaa) == 0) { 1272 sc->sc_flags |= UKBD_FLAG_APPLE_SWAP; 1273 DPRINTF("UKBD_FLAG_APPLE_SWAP set\n"); 1274 } else { 1275 DPRINTF("UKBD_FLAG_APPLE_SWAP not set\n"); 1276 } 1277 1278 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap, 1279 sc->sc_fkeymap, UKBD_NFKEY); 1280 1281 KBD_FOUND_DEVICE(kbd); 1282 1283 ukbd_clear_state(kbd); 1284 1285 /* 1286 * FIXME: set the initial value for lock keys in "sc_state" 1287 * according to the BIOS data? 1288 */ 1289 KBD_PROBE_DONE(kbd); 1290 1291 /* get HID descriptor */ 1292 err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr, 1293 &hid_len, M_TEMP, uaa->info.bIfaceIndex); 1294 1295 if (err == 0) { 1296 DPRINTF("Parsing HID descriptor of %d bytes\n", 1297 (int)hid_len); 1298 1299 ukbd_parse_hid(sc, hid_ptr, hid_len); 1300 1301 free(hid_ptr, M_TEMP); 1302 } 1303 1304 /* check if we should use the boot protocol */ 1305 if (usb_test_quirk(uaa, UQ_KBD_BOOTPROTO) || 1306 (err != 0) || ukbd_any_key_valid(sc) == false) { 1307 DPRINTF("Forcing boot protocol\n"); 1308 1309 err = usbd_req_set_protocol(sc->sc_udev, NULL, 1310 sc->sc_iface_index, 0); 1311 1312 if (err != 0) { 1313 DPRINTF("Set protocol error=%s (ignored)\n", 1314 usbd_errstr(err)); 1315 } 1316 1317 ukbd_parse_hid(sc, ukbd_boot_desc, sizeof(ukbd_boot_desc)); 1318 } 1319 1320 /* ignore if SETIDLE fails, hence it is not crucial */ 1321 usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0); 1322 1323 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state); 1324 1325 KBD_INIT_DONE(kbd); 1326 1327 if (kbd_register(kbd) < 0) { 1328 goto detach; 1329 } 1330 KBD_CONFIG_DONE(kbd); 1331 1332 ukbd_enable(kbd); 1333 1334 #ifdef KBD_INSTALL_CDEV 1335 if (kbd_attach(kbd)) { 1336 goto detach; 1337 } 1338 #endif 1339 1340 #ifdef EVDEV_SUPPORT 1341 evdev = evdev_alloc(); 1342 evdev_set_name(evdev, device_get_desc(dev)); 1343 evdev_set_phys(evdev, device_get_nameunit(dev)); 1344 evdev_set_id(evdev, BUS_USB, uaa->info.idVendor, 1345 uaa->info.idProduct, 0); 1346 evdev_set_serial(evdev, usb_get_serial(uaa->device)); 1347 evdev_set_methods(evdev, kbd, &ukbd_evdev_methods); 1348 evdev_support_event(evdev, EV_SYN); 1349 evdev_support_event(evdev, EV_KEY); 1350 if (sc->sc_flags & (UKBD_FLAG_NUMLOCK | UKBD_FLAG_CAPSLOCK | 1351 UKBD_FLAG_SCROLLLOCK)) 1352 evdev_support_event(evdev, EV_LED); 1353 evdev_support_event(evdev, EV_REP); 1354 1355 for (i = 0x00; i <= 0xFF; i++) 1356 evdev_support_key(evdev, evdev_hid2key(i)); 1357 if (sc->sc_flags & UKBD_FLAG_NUMLOCK) 1358 evdev_support_led(evdev, LED_NUML); 1359 if (sc->sc_flags & UKBD_FLAG_CAPSLOCK) 1360 evdev_support_led(evdev, LED_CAPSL); 1361 if (sc->sc_flags & UKBD_FLAG_SCROLLLOCK) 1362 evdev_support_led(evdev, LED_SCROLLL); 1363 1364 if (evdev_register_mtx(evdev, &Giant)) 1365 evdev_free(evdev); 1366 else 1367 sc->sc_evdev = evdev; 1368 #endif 1369 1370 sc->sc_flags |= UKBD_FLAG_ATTACHED; 1371 1372 if (bootverbose) { 1373 kbdd_diag(kbd, bootverbose); 1374 } 1375 1376 #ifdef USB_DEBUG 1377 /* check for polling rate override */ 1378 rate = ukbd_pollrate; 1379 if (rate > 0) { 1380 if (rate > 1000) 1381 rate = 1; 1382 else 1383 rate = 1000 / rate; 1384 1385 /* set new polling interval in ms */ 1386 usbd_xfer_set_interval(sc->sc_xfer[UKBD_INTR_DT_0], rate); 1387 usbd_xfer_set_interval(sc->sc_xfer[UKBD_INTR_DT_1], rate); 1388 } 1389 #endif 1390 /* start the keyboard */ 1391 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_0]); 1392 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_1]); 1393 1394 return (0); /* success */ 1395 1396 detach: 1397 ukbd_detach(dev); 1398 return (ENXIO); /* error */ 1399 } 1400 1401 static int 1402 ukbd_detach(device_t dev) 1403 { 1404 struct ukbd_softc *sc = device_get_softc(dev); 1405 int error; 1406 1407 UKBD_LOCK_ASSERT(); 1408 1409 DPRINTF("\n"); 1410 1411 sc->sc_flags |= UKBD_FLAG_GONE; 1412 1413 usb_callout_stop(&sc->sc_callout); 1414 1415 /* kill any stuck keys */ 1416 if (sc->sc_flags & UKBD_FLAG_ATTACHED) { 1417 /* stop receiving events from the USB keyboard */ 1418 usbd_transfer_stop(sc->sc_xfer[UKBD_INTR_DT_0]); 1419 usbd_transfer_stop(sc->sc_xfer[UKBD_INTR_DT_1]); 1420 1421 /* release all leftover keys, if any */ 1422 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata)); 1423 1424 /* process releasing of all keys */ 1425 ukbd_interrupt(sc); 1426 } 1427 1428 ukbd_disable(&sc->sc_kbd); 1429 1430 #ifdef KBD_INSTALL_CDEV 1431 if (sc->sc_flags & UKBD_FLAG_ATTACHED) { 1432 error = kbd_detach(&sc->sc_kbd); 1433 if (error) { 1434 /* usb attach cannot return an error */ 1435 device_printf(dev, "WARNING: kbd_detach() " 1436 "returned non-zero! (ignored)\n"); 1437 } 1438 } 1439 #endif 1440 1441 #ifdef EVDEV_SUPPORT 1442 evdev_free(sc->sc_evdev); 1443 #endif 1444 1445 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) { 1446 error = kbd_unregister(&sc->sc_kbd); 1447 if (error) { 1448 /* usb attach cannot return an error */ 1449 device_printf(dev, "WARNING: kbd_unregister() " 1450 "returned non-zero! (ignored)\n"); 1451 } 1452 } 1453 sc->sc_kbd.kb_flags = 0; 1454 1455 usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER); 1456 1457 usb_callout_drain(&sc->sc_callout); 1458 1459 DPRINTF("%s: disconnected\n", 1460 device_get_nameunit(dev)); 1461 1462 return (0); 1463 } 1464 1465 static int 1466 ukbd_resume(device_t dev) 1467 { 1468 struct ukbd_softc *sc = device_get_softc(dev); 1469 1470 UKBD_LOCK_ASSERT(); 1471 1472 ukbd_clear_state(&sc->sc_kbd); 1473 1474 return (0); 1475 } 1476 1477 #ifdef EVDEV_SUPPORT 1478 static void 1479 ukbd_ev_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 1480 int32_t value) 1481 { 1482 keyboard_t *kbd = evdev_get_softc(evdev); 1483 1484 if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && 1485 (type == EV_LED || type == EV_REP)) { 1486 mtx_lock(&Giant); 1487 kbd_ev_event(kbd, type, code, value); 1488 mtx_unlock(&Giant); 1489 } 1490 } 1491 #endif 1492 1493 /* early keyboard probe, not supported */ 1494 static int 1495 ukbd_configure(int flags) 1496 { 1497 return (0); 1498 } 1499 1500 /* detect a keyboard, not used */ 1501 static int 1502 ukbd__probe(int unit, void *arg, int flags) 1503 { 1504 return (ENXIO); 1505 } 1506 1507 /* reset and initialize the device, not used */ 1508 static int 1509 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) 1510 { 1511 return (ENXIO); 1512 } 1513 1514 /* test the interface to the device, not used */ 1515 static int 1516 ukbd_test_if(keyboard_t *kbd) 1517 { 1518 return (0); 1519 } 1520 1521 /* finish using this keyboard, not used */ 1522 static int 1523 ukbd_term(keyboard_t *kbd) 1524 { 1525 return (ENXIO); 1526 } 1527 1528 /* keyboard interrupt routine, not used */ 1529 static int 1530 ukbd_intr(keyboard_t *kbd, void *arg) 1531 { 1532 return (0); 1533 } 1534 1535 /* lock the access to the keyboard, not used */ 1536 static int 1537 ukbd_lock(keyboard_t *kbd, int lock) 1538 { 1539 return (1); 1540 } 1541 1542 /* 1543 * Enable the access to the device; until this function is called, 1544 * the client cannot read from the keyboard. 1545 */ 1546 static int 1547 ukbd_enable(keyboard_t *kbd) 1548 { 1549 1550 UKBD_LOCK(); 1551 KBD_ACTIVATE(kbd); 1552 UKBD_UNLOCK(); 1553 1554 return (0); 1555 } 1556 1557 /* disallow the access to the device */ 1558 static int 1559 ukbd_disable(keyboard_t *kbd) 1560 { 1561 1562 UKBD_LOCK(); 1563 KBD_DEACTIVATE(kbd); 1564 UKBD_UNLOCK(); 1565 1566 return (0); 1567 } 1568 1569 /* check if data is waiting */ 1570 /* Currently unused. */ 1571 static int 1572 ukbd_check(keyboard_t *kbd) 1573 { 1574 struct ukbd_softc *sc = kbd->kb_data; 1575 1576 UKBD_LOCK_ASSERT(); 1577 1578 if (!KBD_IS_ACTIVE(kbd)) 1579 return (0); 1580 1581 if (sc->sc_flags & UKBD_FLAG_POLLING) 1582 ukbd_do_poll(sc, 0); 1583 1584 #ifdef UKBD_EMULATE_ATSCANCODE 1585 if (sc->sc_buffered_char[0]) { 1586 return (1); 1587 } 1588 #endif 1589 if (sc->sc_inputs > 0) { 1590 return (1); 1591 } 1592 return (0); 1593 } 1594 1595 /* check if char is waiting */ 1596 static int 1597 ukbd_check_char_locked(keyboard_t *kbd) 1598 { 1599 struct ukbd_softc *sc = kbd->kb_data; 1600 1601 UKBD_LOCK_ASSERT(); 1602 1603 if (!KBD_IS_ACTIVE(kbd)) 1604 return (0); 1605 1606 if ((sc->sc_composed_char > 0) && 1607 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) { 1608 return (1); 1609 } 1610 return (ukbd_check(kbd)); 1611 } 1612 1613 static int 1614 ukbd_check_char(keyboard_t *kbd) 1615 { 1616 int result; 1617 1618 UKBD_LOCK(); 1619 result = ukbd_check_char_locked(kbd); 1620 UKBD_UNLOCK(); 1621 1622 return (result); 1623 } 1624 1625 /* read one byte from the keyboard if it's allowed */ 1626 /* Currently unused. */ 1627 static int 1628 ukbd_read(keyboard_t *kbd, int wait) 1629 { 1630 struct ukbd_softc *sc = kbd->kb_data; 1631 int32_t usbcode; 1632 #ifdef UKBD_EMULATE_ATSCANCODE 1633 uint32_t keycode; 1634 uint32_t scancode; 1635 1636 #endif 1637 1638 UKBD_LOCK_ASSERT(); 1639 1640 if (!KBD_IS_ACTIVE(kbd)) 1641 return (-1); 1642 1643 #ifdef UKBD_EMULATE_ATSCANCODE 1644 if (sc->sc_buffered_char[0]) { 1645 scancode = sc->sc_buffered_char[0]; 1646 if (scancode & SCAN_PREFIX) { 1647 sc->sc_buffered_char[0] &= ~SCAN_PREFIX; 1648 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); 1649 } 1650 sc->sc_buffered_char[0] = sc->sc_buffered_char[1]; 1651 sc->sc_buffered_char[1] = 0; 1652 return (scancode); 1653 } 1654 #endif /* UKBD_EMULATE_ATSCANCODE */ 1655 1656 /* XXX */ 1657 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1); 1658 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1)) 1659 return (-1); 1660 1661 ++(kbd->kb_count); 1662 1663 #ifdef UKBD_EMULATE_ATSCANCODE 1664 keycode = ukbd_atkeycode(usbcode, sc->sc_ndata.bitmap); 1665 if (keycode == NN) { 1666 return -1; 1667 } 1668 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.bitmap, 1669 (usbcode & KEY_RELEASE))); 1670 #else /* !UKBD_EMULATE_ATSCANCODE */ 1671 return (usbcode); 1672 #endif /* UKBD_EMULATE_ATSCANCODE */ 1673 } 1674 1675 /* read char from the keyboard */ 1676 static uint32_t 1677 ukbd_read_char_locked(keyboard_t *kbd, int wait) 1678 { 1679 struct ukbd_softc *sc = kbd->kb_data; 1680 uint32_t action; 1681 uint32_t keycode; 1682 int32_t usbcode; 1683 #ifdef UKBD_EMULATE_ATSCANCODE 1684 uint32_t scancode; 1685 #endif 1686 1687 UKBD_LOCK_ASSERT(); 1688 1689 if (!KBD_IS_ACTIVE(kbd)) 1690 return (NOKEY); 1691 1692 next_code: 1693 1694 /* do we have a composed char to return ? */ 1695 1696 if ((sc->sc_composed_char > 0) && 1697 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) { 1698 action = sc->sc_composed_char; 1699 sc->sc_composed_char = 0; 1700 1701 if (action > 0xFF) { 1702 goto errkey; 1703 } 1704 goto done; 1705 } 1706 #ifdef UKBD_EMULATE_ATSCANCODE 1707 1708 /* do we have a pending raw scan code? */ 1709 1710 if (sc->sc_mode == K_RAW) { 1711 scancode = sc->sc_buffered_char[0]; 1712 if (scancode) { 1713 if (scancode & SCAN_PREFIX) { 1714 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX); 1715 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); 1716 } 1717 sc->sc_buffered_char[0] = sc->sc_buffered_char[1]; 1718 sc->sc_buffered_char[1] = 0; 1719 return (scancode); 1720 } 1721 } 1722 #endif /* UKBD_EMULATE_ATSCANCODE */ 1723 1724 /* see if there is something in the keyboard port */ 1725 /* XXX */ 1726 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1); 1727 if (usbcode == -1) { 1728 return (NOKEY); 1729 } 1730 ++kbd->kb_count; 1731 1732 #ifdef UKBD_EMULATE_ATSCANCODE 1733 /* USB key index -> key code -> AT scan code */ 1734 keycode = ukbd_atkeycode(usbcode, sc->sc_ndata.bitmap); 1735 if (keycode == NN) { 1736 return (NOKEY); 1737 } 1738 /* return an AT scan code for the K_RAW mode */ 1739 if (sc->sc_mode == K_RAW) { 1740 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.bitmap, 1741 (usbcode & KEY_RELEASE))); 1742 } 1743 #else /* !UKBD_EMULATE_ATSCANCODE */ 1744 1745 /* return the byte as is for the K_RAW mode */ 1746 if (sc->sc_mode == K_RAW) { 1747 return (usbcode); 1748 } 1749 /* USB key index -> key code */ 1750 keycode = ukbd_trtab[KEY_INDEX(usbcode)]; 1751 if (keycode == NN) { 1752 return (NOKEY); 1753 } 1754 #endif /* UKBD_EMULATE_ATSCANCODE */ 1755 1756 switch (keycode) { 1757 case 0x38: /* left alt (compose key) */ 1758 if (usbcode & KEY_RELEASE) { 1759 if (sc->sc_flags & UKBD_FLAG_COMPOSE) { 1760 sc->sc_flags &= ~UKBD_FLAG_COMPOSE; 1761 1762 if (sc->sc_composed_char > 0xFF) { 1763 sc->sc_composed_char = 0; 1764 } 1765 } 1766 } else { 1767 if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) { 1768 sc->sc_flags |= UKBD_FLAG_COMPOSE; 1769 sc->sc_composed_char = 0; 1770 } 1771 } 1772 break; 1773 } 1774 1775 /* return the key code in the K_CODE mode */ 1776 if (usbcode & KEY_RELEASE) { 1777 keycode |= SCAN_RELEASE; 1778 } 1779 if (sc->sc_mode == K_CODE) { 1780 return (keycode); 1781 } 1782 /* compose a character code */ 1783 if (sc->sc_flags & UKBD_FLAG_COMPOSE) { 1784 switch (keycode) { 1785 /* key pressed, process it */ 1786 case 0x47: 1787 case 0x48: 1788 case 0x49: /* keypad 7,8,9 */ 1789 sc->sc_composed_char *= 10; 1790 sc->sc_composed_char += keycode - 0x40; 1791 goto check_composed; 1792 1793 case 0x4B: 1794 case 0x4C: 1795 case 0x4D: /* keypad 4,5,6 */ 1796 sc->sc_composed_char *= 10; 1797 sc->sc_composed_char += keycode - 0x47; 1798 goto check_composed; 1799 1800 case 0x4F: 1801 case 0x50: 1802 case 0x51: /* keypad 1,2,3 */ 1803 sc->sc_composed_char *= 10; 1804 sc->sc_composed_char += keycode - 0x4E; 1805 goto check_composed; 1806 1807 case 0x52: /* keypad 0 */ 1808 sc->sc_composed_char *= 10; 1809 goto check_composed; 1810 1811 /* key released, no interest here */ 1812 case SCAN_RELEASE | 0x47: 1813 case SCAN_RELEASE | 0x48: 1814 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */ 1815 case SCAN_RELEASE | 0x4B: 1816 case SCAN_RELEASE | 0x4C: 1817 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */ 1818 case SCAN_RELEASE | 0x4F: 1819 case SCAN_RELEASE | 0x50: 1820 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */ 1821 case SCAN_RELEASE | 0x52: /* keypad 0 */ 1822 goto next_code; 1823 1824 case 0x38: /* left alt key */ 1825 break; 1826 1827 default: 1828 if (sc->sc_composed_char > 0) { 1829 sc->sc_flags &= ~UKBD_FLAG_COMPOSE; 1830 sc->sc_composed_char = 0; 1831 goto errkey; 1832 } 1833 break; 1834 } 1835 } 1836 /* keycode to key action */ 1837 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode), 1838 (keycode & SCAN_RELEASE), 1839 &sc->sc_state, &sc->sc_accents); 1840 if (action == NOKEY) { 1841 goto next_code; 1842 } 1843 done: 1844 return (action); 1845 1846 check_composed: 1847 if (sc->sc_composed_char <= 0xFF) { 1848 goto next_code; 1849 } 1850 errkey: 1851 return (ERRKEY); 1852 } 1853 1854 /* Currently wait is always false. */ 1855 static uint32_t 1856 ukbd_read_char(keyboard_t *kbd, int wait) 1857 { 1858 uint32_t keycode; 1859 1860 UKBD_LOCK(); 1861 keycode = ukbd_read_char_locked(kbd, wait); 1862 UKBD_UNLOCK(); 1863 1864 return (keycode); 1865 } 1866 1867 /* some useful control functions */ 1868 static int 1869 ukbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg) 1870 { 1871 struct ukbd_softc *sc = kbd->kb_data; 1872 int i; 1873 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1874 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1875 int ival; 1876 1877 #endif 1878 1879 UKBD_LOCK_ASSERT(); 1880 1881 switch (cmd) { 1882 case KDGKBMODE: /* get keyboard mode */ 1883 *(int *)arg = sc->sc_mode; 1884 break; 1885 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1886 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1887 case _IO('K', 7): 1888 ival = IOCPARM_IVAL(arg); 1889 arg = (caddr_t)&ival; 1890 /* FALLTHROUGH */ 1891 #endif 1892 case KDSKBMODE: /* set keyboard mode */ 1893 switch (*(int *)arg) { 1894 case K_XLATE: 1895 if (sc->sc_mode != K_XLATE) { 1896 /* make lock key state and LED state match */ 1897 sc->sc_state &= ~LOCK_MASK; 1898 sc->sc_state |= KBD_LED_VAL(kbd); 1899 } 1900 /* FALLTHROUGH */ 1901 case K_RAW: 1902 case K_CODE: 1903 if (sc->sc_mode != *(int *)arg) { 1904 if ((sc->sc_flags & UKBD_FLAG_POLLING) == 0) 1905 ukbd_clear_state(kbd); 1906 sc->sc_mode = *(int *)arg; 1907 } 1908 break; 1909 default: 1910 return (EINVAL); 1911 } 1912 break; 1913 1914 case KDGETLED: /* get keyboard LED */ 1915 *(int *)arg = KBD_LED_VAL(kbd); 1916 break; 1917 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1918 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1919 case _IO('K', 66): 1920 ival = IOCPARM_IVAL(arg); 1921 arg = (caddr_t)&ival; 1922 /* FALLTHROUGH */ 1923 #endif 1924 case KDSETLED: /* set keyboard LED */ 1925 /* NOTE: lock key state in "sc_state" won't be changed */ 1926 if (*(int *)arg & ~LOCK_MASK) 1927 return (EINVAL); 1928 1929 i = *(int *)arg; 1930 1931 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */ 1932 if (sc->sc_mode == K_XLATE && 1933 kbd->kb_keymap->n_keys > ALTGR_OFFSET) { 1934 if (i & ALKED) 1935 i |= CLKED; 1936 else 1937 i &= ~CLKED; 1938 } 1939 if (KBD_HAS_DEVICE(kbd)) 1940 ukbd_set_leds(sc, i); 1941 1942 KBD_LED_VAL(kbd) = *(int *)arg; 1943 break; 1944 case KDGKBSTATE: /* get lock key state */ 1945 *(int *)arg = sc->sc_state & LOCK_MASK; 1946 break; 1947 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1948 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1949 case _IO('K', 20): 1950 ival = IOCPARM_IVAL(arg); 1951 arg = (caddr_t)&ival; 1952 /* FALLTHROUGH */ 1953 #endif 1954 case KDSKBSTATE: /* set lock key state */ 1955 if (*(int *)arg & ~LOCK_MASK) { 1956 return (EINVAL); 1957 } 1958 sc->sc_state &= ~LOCK_MASK; 1959 sc->sc_state |= *(int *)arg; 1960 1961 /* set LEDs and quit */ 1962 return (ukbd_ioctl(kbd, KDSETLED, arg)); 1963 1964 case KDSETREPEAT: /* set keyboard repeat rate (new 1965 * interface) */ 1966 if (!KBD_HAS_DEVICE(kbd)) { 1967 return (0); 1968 } 1969 /* 1970 * Convert negative, zero and tiny args to the same limits 1971 * as atkbd. We could support delays of 1 msec, but 1972 * anything much shorter than the shortest atkbd value 1973 * of 250.34 is almost unusable as well as incompatible. 1974 */ 1975 kbd->kb_delay1 = imax(((int *)arg)[0], 250); 1976 kbd->kb_delay2 = imax(((int *)arg)[1], 34); 1977 #ifdef EVDEV_SUPPORT 1978 if (sc->sc_evdev != NULL) 1979 evdev_push_repeats(sc->sc_evdev, kbd); 1980 #endif 1981 return (0); 1982 1983 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1984 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1985 case _IO('K', 67): 1986 ival = IOCPARM_IVAL(arg); 1987 arg = (caddr_t)&ival; 1988 /* FALLTHROUGH */ 1989 #endif 1990 case KDSETRAD: /* set keyboard repeat rate (old 1991 * interface) */ 1992 return (ukbd_set_typematic(kbd, *(int *)arg)); 1993 1994 case PIO_KEYMAP: /* set keyboard translation table */ 1995 case PIO_KEYMAPENT: /* set keyboard translation table 1996 * entry */ 1997 case PIO_DEADKEYMAP: /* set accent key translation table */ 1998 #ifdef COMPAT_FREEBSD13 1999 case OPIO_KEYMAP: /* set keyboard translation table 2000 * (compat) */ 2001 case OPIO_DEADKEYMAP: /* set accent key translation table 2002 * (compat) */ 2003 #endif /* COMPAT_FREEBSD13 */ 2004 sc->sc_accents = 0; 2005 /* FALLTHROUGH */ 2006 default: 2007 return (genkbd_commonioctl(kbd, cmd, arg)); 2008 } 2009 2010 return (0); 2011 } 2012 2013 static int 2014 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) 2015 { 2016 int result; 2017 2018 /* 2019 * XXX Check if someone is calling us from a critical section: 2020 */ 2021 if (curthread->td_critnest != 0) 2022 return (EDEADLK); 2023 2024 /* 2025 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any 2026 * context where printf(9) can be called, which among other things 2027 * includes interrupt filters and threads with any kinds of locks 2028 * already held. For this reason it would be dangerous to acquire 2029 * the Giant here unconditionally. On the other hand we have to 2030 * have it to handle the ioctl. 2031 * So we make our best effort to auto-detect whether we can grab 2032 * the Giant or not. Blame syscons(4) for this. 2033 */ 2034 switch (cmd) { 2035 case KDGKBSTATE: 2036 case KDSKBSTATE: 2037 case KDSETLED: 2038 if (!mtx_owned(&Giant) && !USB_IN_POLLING_MODE_FUNC()) 2039 return (EDEADLK); /* best I could come up with */ 2040 /* FALLTHROUGH */ 2041 default: 2042 UKBD_LOCK(); 2043 result = ukbd_ioctl_locked(kbd, cmd, arg); 2044 UKBD_UNLOCK(); 2045 return (result); 2046 } 2047 } 2048 2049 /* clear the internal state of the keyboard */ 2050 static void 2051 ukbd_clear_state(keyboard_t *kbd) 2052 { 2053 struct ukbd_softc *sc = kbd->kb_data; 2054 2055 UKBD_LOCK_ASSERT(); 2056 2057 sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING); 2058 sc->sc_state &= LOCK_MASK; /* preserve locking key state */ 2059 sc->sc_accents = 0; 2060 sc->sc_composed_char = 0; 2061 #ifdef UKBD_EMULATE_ATSCANCODE 2062 sc->sc_buffered_char[0] = 0; 2063 sc->sc_buffered_char[1] = 0; 2064 #endif 2065 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata)); 2066 memset(&sc->sc_odata, 0, sizeof(sc->sc_odata)); 2067 sc->sc_repeat_time = 0; 2068 sc->sc_repeat_key = 0; 2069 } 2070 2071 /* save the internal state, not used */ 2072 static int 2073 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len) 2074 { 2075 return (len == 0) ? 1 : -1; 2076 } 2077 2078 /* set the internal state, not used */ 2079 static int 2080 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len) 2081 { 2082 return (EINVAL); 2083 } 2084 2085 static int 2086 ukbd_poll(keyboard_t *kbd, int on) 2087 { 2088 struct ukbd_softc *sc = kbd->kb_data; 2089 2090 UKBD_LOCK(); 2091 /* 2092 * Keep a reference count on polling to allow recursive 2093 * cngrab() during a panic for example. 2094 */ 2095 if (on) 2096 sc->sc_polling++; 2097 else if (sc->sc_polling > 0) 2098 sc->sc_polling--; 2099 2100 if (sc->sc_polling != 0) { 2101 sc->sc_flags |= UKBD_FLAG_POLLING; 2102 sc->sc_poll_thread = curthread; 2103 } else { 2104 sc->sc_flags &= ~UKBD_FLAG_POLLING; 2105 sc->sc_delay = 0; 2106 } 2107 UKBD_UNLOCK(); 2108 2109 return (0); 2110 } 2111 2112 /* local functions */ 2113 2114 static void 2115 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds) 2116 { 2117 2118 UKBD_LOCK_ASSERT(); 2119 DPRINTF("leds=0x%02x\n", leds); 2120 2121 #ifdef EVDEV_SUPPORT 2122 if (sc->sc_evdev != NULL) 2123 evdev_push_leds(sc->sc_evdev, leds); 2124 #endif 2125 2126 sc->sc_leds = leds; 2127 sc->sc_flags |= UKBD_FLAG_SET_LEDS; 2128 2129 /* start transfer, if not already started */ 2130 2131 usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]); 2132 } 2133 2134 static int 2135 ukbd_set_typematic(keyboard_t *kbd, int code) 2136 { 2137 #ifdef EVDEV_SUPPORT 2138 struct ukbd_softc *sc = kbd->kb_data; 2139 #endif 2140 if (code & ~0x7f) { 2141 return (EINVAL); 2142 } 2143 kbd->kb_delay1 = kbdelays[(code >> 5) & 3]; 2144 kbd->kb_delay2 = kbrates[code & 0x1f]; 2145 #ifdef EVDEV_SUPPORT 2146 if (sc->sc_evdev != NULL) 2147 evdev_push_repeats(sc->sc_evdev, kbd); 2148 #endif 2149 return (0); 2150 } 2151 2152 #ifdef UKBD_EMULATE_ATSCANCODE 2153 static uint32_t 2154 ukbd_atkeycode(int usbcode, const uint64_t *bitmap) 2155 { 2156 uint32_t keycode; 2157 2158 keycode = ukbd_trtab[KEY_INDEX(usbcode)]; 2159 2160 /* 2161 * Translate Alt-PrintScreen to SysRq. 2162 * 2163 * Some or all AT keyboards connected through USB have already 2164 * mapped Alted PrintScreens to an unusual usbcode (0x8a). 2165 * ukbd_trtab translates this to 0x7e, and key2scan() would 2166 * translate that to 0x79 (Intl' 4). Assume that if we have 2167 * an Alted 0x7e here then it actually is an Alted PrintScreen. 2168 * 2169 * The usual usbcode for all PrintScreens is 0x46. ukbd_trtab 2170 * translates this to 0x5c, so the Alt check to classify 0x5c 2171 * is routine. 2172 */ 2173 if ((keycode == 0x5c || keycode == 0x7e) && 2174 (UKBD_KEY_PRESSED(bitmap, 0xe2 /* ALT-L */) || 2175 UKBD_KEY_PRESSED(bitmap, 0xe6 /* ALT-R */))) 2176 return (0x54); 2177 return (keycode); 2178 } 2179 2180 static int 2181 ukbd_key2scan(struct ukbd_softc *sc, int code, const uint64_t *bitmap, int up) 2182 { 2183 static const int scan[] = { 2184 /* 89 */ 2185 0x11c, /* Enter */ 2186 /* 90-99 */ 2187 0x11d, /* Ctrl-R */ 2188 0x135, /* Divide */ 2189 0x137, /* PrintScreen */ 2190 0x138, /* Alt-R */ 2191 0x147, /* Home */ 2192 0x148, /* Up */ 2193 0x149, /* PageUp */ 2194 0x14b, /* Left */ 2195 0x14d, /* Right */ 2196 0x14f, /* End */ 2197 /* 100-109 */ 2198 0x150, /* Down */ 2199 0x151, /* PageDown */ 2200 0x152, /* Insert */ 2201 0x153, /* Delete */ 2202 0x146, /* Pause/Break */ 2203 0x15b, /* Win_L(Super_L) */ 2204 0x15c, /* Win_R(Super_R) */ 2205 0x15d, /* Application(Menu) */ 2206 2207 /* SUN TYPE 6 USB KEYBOARD */ 2208 0x168, /* Sun Type 6 Help */ 2209 0x15e, /* Sun Type 6 Stop */ 2210 /* 110 - 119 */ 2211 0x15f, /* Sun Type 6 Again */ 2212 0x160, /* Sun Type 6 Props */ 2213 0x161, /* Sun Type 6 Undo */ 2214 0x162, /* Sun Type 6 Front */ 2215 0x163, /* Sun Type 6 Copy */ 2216 0x164, /* Sun Type 6 Open */ 2217 0x165, /* Sun Type 6 Paste */ 2218 0x166, /* Sun Type 6 Find */ 2219 0x167, /* Sun Type 6 Cut */ 2220 0x125, /* Sun Type 6 Mute */ 2221 /* 120 - 130 */ 2222 0x11f, /* Sun Type 6 VolumeDown */ 2223 0x11e, /* Sun Type 6 VolumeUp */ 2224 0x120, /* Sun Type 6 PowerDown */ 2225 2226 /* Japanese 106/109 keyboard */ 2227 0x73, /* Keyboard Intl' 1 (backslash / underscore) */ 2228 0x70, /* Keyboard Intl' 2 (Katakana / Hiragana) */ 2229 0x7d, /* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */ 2230 0x79, /* Keyboard Intl' 4 (Henkan) */ 2231 0x7b, /* Keyboard Intl' 5 (Muhenkan) */ 2232 0x5c, /* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */ 2233 0x71, /* Apple Keyboard JIS (Kana) */ 2234 0x72, /* Apple Keyboard JIS (Eisu) */ 2235 }; 2236 2237 if ((code >= 89) && (code < (int)(89 + nitems(scan)))) { 2238 code = scan[code - 89]; 2239 } 2240 /* PrintScreen */ 2241 if (code == 0x137 && (!( 2242 UKBD_KEY_PRESSED(bitmap, 0xe0 /* CTRL-L */) || 2243 UKBD_KEY_PRESSED(bitmap, 0xe4 /* CTRL-R */) || 2244 UKBD_KEY_PRESSED(bitmap, 0xe1 /* SHIFT-L */) || 2245 UKBD_KEY_PRESSED(bitmap, 0xe5 /* SHIFT-R */)))) { 2246 code |= SCAN_PREFIX_SHIFT; 2247 } 2248 /* Pause/Break */ 2249 if ((code == 0x146) && (!( 2250 UKBD_KEY_PRESSED(bitmap, 0xe0 /* CTRL-L */) || 2251 UKBD_KEY_PRESSED(bitmap, 0xe4 /* CTRL-R */)))) { 2252 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL); 2253 } 2254 code |= (up ? SCAN_RELEASE : SCAN_PRESS); 2255 2256 if (code & SCAN_PREFIX) { 2257 if (code & SCAN_PREFIX_CTL) { 2258 /* Ctrl */ 2259 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE)); 2260 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX); 2261 } else if (code & SCAN_PREFIX_SHIFT) { 2262 /* Shift */ 2263 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE)); 2264 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT); 2265 } else { 2266 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX); 2267 sc->sc_buffered_char[1] = 0; 2268 } 2269 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); 2270 } 2271 return (code); 2272 2273 } 2274 2275 #endif /* UKBD_EMULATE_ATSCANCODE */ 2276 2277 static keyboard_switch_t ukbdsw = { 2278 .probe = &ukbd__probe, 2279 .init = &ukbd_init, 2280 .term = &ukbd_term, 2281 .intr = &ukbd_intr, 2282 .test_if = &ukbd_test_if, 2283 .enable = &ukbd_enable, 2284 .disable = &ukbd_disable, 2285 .read = &ukbd_read, 2286 .check = &ukbd_check, 2287 .read_char = &ukbd_read_char, 2288 .check_char = &ukbd_check_char, 2289 .ioctl = &ukbd_ioctl, 2290 .lock = &ukbd_lock, 2291 .clear_state = &ukbd_clear_state, 2292 .get_state = &ukbd_get_state, 2293 .set_state = &ukbd_set_state, 2294 .poll = &ukbd_poll, 2295 }; 2296 2297 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure); 2298 2299 static int 2300 ukbd_driver_load(module_t mod, int what, void *arg) 2301 { 2302 switch (what) { 2303 case MOD_LOAD: 2304 kbd_add_driver(&ukbd_kbd_driver); 2305 break; 2306 case MOD_UNLOAD: 2307 kbd_delete_driver(&ukbd_kbd_driver); 2308 break; 2309 } 2310 return (0); 2311 } 2312 2313 static device_method_t ukbd_methods[] = { 2314 DEVMETHOD(device_probe, ukbd_probe), 2315 DEVMETHOD(device_attach, ukbd_attach), 2316 DEVMETHOD(device_detach, ukbd_detach), 2317 DEVMETHOD(device_resume, ukbd_resume), 2318 2319 DEVMETHOD_END 2320 }; 2321 2322 static driver_t ukbd_driver = { 2323 .name = "ukbd", 2324 .methods = ukbd_methods, 2325 .size = sizeof(struct ukbd_softc), 2326 }; 2327 2328 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_driver_load, NULL); 2329 MODULE_DEPEND(ukbd, usb, 1, 1, 1); 2330 MODULE_DEPEND(ukbd, hid, 1, 1, 1); 2331 #ifdef EVDEV_SUPPORT 2332 MODULE_DEPEND(ukbd, evdev, 1, 1, 1); 2333 #endif 2334 MODULE_VERSION(ukbd, 1); 2335 USB_PNP_HOST_INFO(ukbd_devs); 2336