| 1 | /* $NetBSD: ukbd.c,v 1.133 2016/04/30 14:33:16 skrll Exp $ */ |
| 2 | |
| 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 | * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf |
| 35 | */ |
| 36 | |
| 37 | #include <sys/cdefs.h> |
| 38 | __KERNEL_RCSID(0, "$NetBSD: ukbd.c,v 1.133 2016/04/30 14:33:16 skrll Exp $" ); |
| 39 | |
| 40 | #ifdef _KERNEL_OPT |
| 41 | #include "opt_ukbd.h" |
| 42 | #include "opt_ukbd_layout.h" |
| 43 | #include "opt_usb.h" |
| 44 | #include "opt_wsdisplay_compat.h" |
| 45 | #include "opt_ddb.h" |
| 46 | #endif /* _KERNEL_OPT */ |
| 47 | |
| 48 | #include <sys/param.h> |
| 49 | #include <sys/systm.h> |
| 50 | #include <sys/callout.h> |
| 51 | #include <sys/kernel.h> |
| 52 | #include <sys/device.h> |
| 53 | #include <sys/ioctl.h> |
| 54 | #include <sys/file.h> |
| 55 | #include <sys/select.h> |
| 56 | #include <sys/proc.h> |
| 57 | #include <sys/vnode.h> |
| 58 | #include <sys/poll.h> |
| 59 | |
| 60 | #include <dev/usb/usb.h> |
| 61 | #include <dev/usb/usbhid.h> |
| 62 | |
| 63 | #include <dev/usb/usbdi.h> |
| 64 | #include <dev/usb/usbdi_util.h> |
| 65 | #include <dev/usb/usbdivar.h> |
| 66 | #include <dev/usb/usbdevs.h> |
| 67 | #include <dev/usb/usb_quirks.h> |
| 68 | #include <dev/usb/uhidev.h> |
| 69 | #include <dev/usb/hid.h> |
| 70 | #include <dev/usb/ukbdvar.h> |
| 71 | |
| 72 | #include <dev/wscons/wsconsio.h> |
| 73 | #include <dev/wscons/wskbdvar.h> |
| 74 | #include <dev/wscons/wsksymdef.h> |
| 75 | #include <dev/wscons/wsksymvar.h> |
| 76 | |
| 77 | #ifdef UKBD_DEBUG |
| 78 | #define DPRINTF(x) if (ukbddebug) printf x |
| 79 | #define DPRINTFN(n,x) if (ukbddebug>(n)) printf x |
| 80 | int ukbddebug = 0; |
| 81 | #else |
| 82 | #define DPRINTF(x) |
| 83 | #define DPRINTFN(n,x) |
| 84 | #endif |
| 85 | |
| 86 | #define MAXKEYCODE 6 |
| 87 | #define MAXMOD 8 /* max 32 */ |
| 88 | |
| 89 | struct ukbd_data { |
| 90 | uint32_t modifiers; |
| 91 | uint8_t keycode[MAXKEYCODE]; |
| 92 | }; |
| 93 | |
| 94 | #define PRESS 0x000 |
| 95 | #define RELEASE 0x100 |
| 96 | #define CODEMASK 0x0ff |
| 97 | |
| 98 | struct ukbd_keycodetrans { |
| 99 | uint16_t from; |
| 100 | uint16_t to; |
| 101 | }; |
| 102 | |
| 103 | #define IS_PMF 0x8000 |
| 104 | |
| 105 | Static const struct ukbd_keycodetrans trtab_apple_fn[] = { |
| 106 | { 0x0c, 0x5d }, /* i -> KP 5 */ |
| 107 | { 0x0d, 0x59 }, /* j -> KP 1 */ |
| 108 | { 0x0e, 0x5a }, /* k -> KP 2 */ |
| 109 | { 0x0f, 0x5b }, /* l -> KP 3 */ |
| 110 | { 0x10, 0x62 }, /* m -> KP 0 */ |
| 111 | { 0x12, 0x5e }, /* o -> KP 6 */ |
| 112 | { 0x13, 0x55 }, /* o -> KP * */ |
| 113 | { 0x18, 0x5c }, /* u -> KP 4 */ |
| 114 | { 0x0c, 0x5d }, /* i -> KP 5 */ |
| 115 | { 0x2a, 0x4c }, /* Backspace -> Delete */ |
| 116 | { 0x28, 0x49 }, /* Return -> Insert */ |
| 117 | { 0x24, 0x5f }, /* 7 -> KP 7 */ |
| 118 | { 0x25, 0x60 }, /* 8 -> KP 8 */ |
| 119 | { 0x26, 0x61 }, /* 9 -> KP 9 */ |
| 120 | { 0x27, 0x54 }, /* 0 -> KP / */ |
| 121 | { 0x2d, 0x67 }, /* - -> KP = */ |
| 122 | { 0x33, 0x56 }, /* ; -> KP - */ |
| 123 | { 0x37, 0x63 }, /* . -> KP . */ |
| 124 | { 0x38, 0x57 }, /* / -> KP + */ |
| 125 | { 0x3a, 0xd1 }, /* F1..F12 mapped to reserved codes 0xd1..0xdc */ |
| 126 | { 0x3b, 0xd2 }, |
| 127 | { 0x3c, 0xd3 }, |
| 128 | { 0x3d, 0xd4 }, |
| 129 | { 0x3e, 0xd5 }, |
| 130 | { 0x3f, 0xd6 }, |
| 131 | { 0x40, 0xd7 }, |
| 132 | { 0x41, 0xd8 }, |
| 133 | { 0x42, 0xd9 }, |
| 134 | { 0x43, 0xda }, |
| 135 | { 0x44, 0xdb }, |
| 136 | { 0x45, 0xdc }, |
| 137 | { 0x4f, 0x4d }, /* Right -> End */ |
| 138 | { 0x50, 0x4a }, /* Left -> Home */ |
| 139 | { 0x51, 0x4e }, /* Down -> PageDown */ |
| 140 | { 0x52, 0x4b }, /* Up -> PageUp */ |
| 141 | { 0x00, 0x00 } |
| 142 | }; |
| 143 | |
| 144 | Static const struct ukbd_keycodetrans trtab_apple_iso[] = { |
| 145 | { 0x35, 0x64 }, /* swap the key above tab with key right of shift */ |
| 146 | { 0x64, 0x35 }, |
| 147 | { 0x31, 0x32 }, /* key left of return is Europe1, not "\|" */ |
| 148 | { 0x00, 0x00 } |
| 149 | }; |
| 150 | |
| 151 | #ifdef GDIUM_KEYBOARD_HACK |
| 152 | Static const struct ukbd_keycodetrans trtab_gdium_fn[] = { |
| 153 | #ifdef notyet |
| 154 | { 58, 0 }, /* F1 -> toggle camera */ |
| 155 | { 59, 0 }, /* F2 -> toggle wireless */ |
| 156 | #endif |
| 157 | { 60, IS_PMF | PMFE_AUDIO_VOLUME_TOGGLE }, |
| 158 | { 61, IS_PMF | PMFE_AUDIO_VOLUME_UP }, |
| 159 | { 62, IS_PMF | PMFE_AUDIO_VOLUME_DOWN }, |
| 160 | #ifdef notyet |
| 161 | { 63, 0 }, /* F6 -> toggle ext. video */ |
| 162 | { 64, 0 }, /* F7 -> toggle mouse */ |
| 163 | #endif |
| 164 | { 65, IS_PMF | PMFE_DISPLAY_BRIGHTNESS_UP }, |
| 165 | { 66, IS_PMF | PMFE_DISPLAY_BRIGHTNESS_DOWN }, |
| 166 | #ifdef notyet |
| 167 | { 67, 0 }, /* F10 -> suspend */ |
| 168 | { 68, 0 }, /* F11 -> user1 */ |
| 169 | { 69, 0 }, /* F12 -> user2 */ |
| 170 | { 70, 0 }, /* print screen -> sysrq */ |
| 171 | #endif |
| 172 | { 76, 71 }, /* delete -> scroll lock */ |
| 173 | { 81, 78 }, /* down -> page down */ |
| 174 | { 82, 75 }, /* up -> page up */ |
| 175 | { 0, 0 } |
| 176 | }; |
| 177 | #endif |
| 178 | |
| 179 | Static const struct ukbd_keycodetrans trtab_generic[] = { |
| 180 | { 0x7f, IS_PMF | PMFE_AUDIO_VOLUME_TOGGLE }, |
| 181 | { 0x80, IS_PMF | PMFE_AUDIO_VOLUME_UP }, |
| 182 | { 0x81, IS_PMF | PMFE_AUDIO_VOLUME_DOWN }, |
| 183 | { 0x00, 0x00 } |
| 184 | }; |
| 185 | |
| 186 | #if defined(WSDISPLAY_COMPAT_RAWKBD) |
| 187 | #define NN 0 /* no translation */ |
| 188 | /* |
| 189 | * Translate USB keycodes to US keyboard XT scancodes. |
| 190 | * Scancodes >= 0x80 represent EXTENDED keycodes. |
| 191 | * |
| 192 | * See http://www.microsoft.com/whdc/archive/scancode.mspx |
| 193 | * |
| 194 | * Note: a real pckbd(4) has more complexity in its |
| 195 | * protocol for some keys than this translation implements. |
| 196 | * For example, some keys generate Fake ShiftL events (e0 2a) |
| 197 | * before the actual key sequence. |
| 198 | */ |
| 199 | Static const uint8_t ukbd_trtab[256] = { |
| 200 | NN, NN, NN, NN, 0x1e, 0x30, 0x2e, 0x20, /* 00 - 07 */ |
| 201 | 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, /* 08 - 0f */ |
| 202 | 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, /* 10 - 17 */ |
| 203 | 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, /* 18 - 1f */ |
| 204 | 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 20 - 27 */ |
| 205 | 0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, /* 28 - 2f */ |
| 206 | 0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, /* 30 - 37 */ |
| 207 | 0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, /* 38 - 3f */ |
| 208 | 0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xb7, 0x46, /* 40 - 47 */ |
| 209 | 0x7f, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd, /* 48 - 4f */ |
| 210 | 0xcb, 0xd0, 0xc8, 0x45, 0xb5, 0x37, 0x4a, 0x4e, /* 50 - 57 */ |
| 211 | 0x9c, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47, /* 58 - 5f */ |
| 212 | 0x48, 0x49, 0x52, 0x53, 0x56, 0xdd, 0xdf, 0x59, /* 60 - 67 */ |
| 213 | 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, NN, /* 68 - 6f */ |
| 214 | NN, NN, NN, NN, 0x84, 0x85, 0x87, 0x88, /* 70 - 77 */ |
| 215 | 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, NN, /* 78 - 7f */ |
| 216 | NN, NN, NN, NN, NN, 0x7e, NN, 0x73, /* 80 - 87 */ |
| 217 | 0x70, 0x7d, 0x79, 0x7b, 0x5c, NN, NN, NN, /* 88 - 8f */ |
| 218 | NN, NN, 0x78, 0x77, 0x76, NN, NN, NN, /* 90 - 97 */ |
| 219 | NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9f */ |
| 220 | NN, NN, NN, NN, NN, NN, NN, NN, /* a0 - a7 */ |
| 221 | NN, NN, NN, NN, NN, NN, NN, NN, /* a8 - af */ |
| 222 | NN, NN, NN, NN, NN, NN, NN, NN, /* b0 - b7 */ |
| 223 | NN, NN, NN, NN, NN, NN, NN, NN, /* b8 - bf */ |
| 224 | NN, NN, NN, NN, NN, NN, NN, NN, /* c0 - c7 */ |
| 225 | NN, NN, NN, NN, NN, NN, NN, NN, /* c8 - cf */ |
| 226 | NN, NN, NN, NN, NN, NN, NN, NN, /* d0 - d7 */ |
| 227 | NN, NN, NN, NN, NN, NN, NN, NN, /* d8 - df */ |
| 228 | 0x1d, 0x2a, 0x38, 0xdb, 0x9d, 0x36, 0xb8, 0xdc, /* e0 - e7 */ |
| 229 | NN, NN, NN, NN, NN, NN, NN, NN, /* e8 - ef */ |
| 230 | NN, NN, NN, NN, NN, NN, NN, NN, /* f0 - f7 */ |
| 231 | NN, NN, NN, NN, NN, NN, NN, NN, /* f8 - ff */ |
| 232 | }; |
| 233 | #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */ |
| 234 | |
| 235 | #define KEY_ERROR 0x01 |
| 236 | |
| 237 | #define MAXKEYS (MAXMOD+2*MAXKEYCODE) |
| 238 | |
| 239 | struct ukbd_softc { |
| 240 | struct uhidev sc_hdev; |
| 241 | |
| 242 | struct ukbd_data sc_ndata; |
| 243 | struct ukbd_data sc_odata; |
| 244 | struct hid_location sc_modloc[MAXMOD]; |
| 245 | u_int sc_nmod; |
| 246 | struct { |
| 247 | uint32_t mask; |
| 248 | uint8_t key; |
| 249 | } sc_mods[MAXMOD]; |
| 250 | |
| 251 | struct hid_location sc_keycodeloc; |
| 252 | u_int sc_nkeycode; |
| 253 | |
| 254 | u_int sc_flags; /* flags */ |
| 255 | #define FLAG_ENABLED 0x0001 |
| 256 | #define FLAG_POLLING 0x0002 |
| 257 | #define FLAG_DEBOUNCE 0x0004 /* for quirk handling */ |
| 258 | #define FLAG_APPLE_FIX_ISO 0x0008 |
| 259 | #define FLAG_APPLE_FN 0x0010 |
| 260 | #define FLAG_GDIUM_FN 0x0020 |
| 261 | #define FLAG_FN_PRESSED 0x0100 /* FN key is held down */ |
| 262 | #define FLAG_FN_ALT 0x0200 /* Last Alt key was FN-Alt = AltGr */ |
| 263 | |
| 264 | int sc_console_keyboard; /* we are the console keyboard */ |
| 265 | |
| 266 | struct callout sc_delay; /* for quirk handling */ |
| 267 | struct ukbd_data sc_data; /* for quirk handling */ |
| 268 | |
| 269 | struct hid_location sc_apple_fn; |
| 270 | struct hid_location sc_numloc; |
| 271 | struct hid_location sc_capsloc; |
| 272 | struct hid_location sc_scroloc; |
| 273 | struct hid_location sc_compose; |
| 274 | int sc_leds; |
| 275 | struct usb_task sc_ledtask; |
| 276 | device_t sc_wskbddev; |
| 277 | |
| 278 | #if defined(WSDISPLAY_COMPAT_RAWKBD) |
| 279 | int sc_rawkbd; |
| 280 | #if defined(UKBD_REPEAT) |
| 281 | struct callout sc_rawrepeat_ch; |
| 282 | #define REP_DELAY1 400 |
| 283 | #define REP_DELAYN 100 |
| 284 | int sc_nrep; |
| 285 | char sc_rep[MAXKEYS]; |
| 286 | #endif /* defined(UKBD_REPEAT) */ |
| 287 | #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */ |
| 288 | |
| 289 | int sc_spl; |
| 290 | int sc_npollchar; |
| 291 | uint16_t sc_pollchars[MAXKEYS]; |
| 292 | |
| 293 | u_char sc_dying; |
| 294 | }; |
| 295 | |
| 296 | #ifdef UKBD_DEBUG |
| 297 | #define UKBDTRACESIZE 64 |
| 298 | struct ukbdtraceinfo { |
| 299 | int unit; |
| 300 | struct timeval tv; |
| 301 | struct ukbd_data ud; |
| 302 | }; |
| 303 | struct ukbdtraceinfo ukbdtracedata[UKBDTRACESIZE]; |
| 304 | int ukbdtraceindex = 0; |
| 305 | int ukbdtrace = 0; |
| 306 | void ukbdtracedump(void); |
| 307 | void |
| 308 | ukbdtracedump(void) |
| 309 | { |
| 310 | int i; |
| 311 | for (i = 0; i < UKBDTRACESIZE; i++) { |
| 312 | struct ukbdtraceinfo *p = |
| 313 | &ukbdtracedata[(i+ukbdtraceindex)%UKBDTRACESIZE]; |
| 314 | printf("%" PRIu64".%06" PRIu64": mod=0x%02x key0=0x%02x key1=0x%02x " |
| 315 | "key2=0x%02x key3=0x%02x\n" , |
| 316 | p->tv.tv_sec, (uint64_t)p->tv.tv_usec, |
| 317 | p->ud.modifiers, p->ud.keycode[0], p->ud.keycode[1], |
| 318 | p->ud.keycode[2], p->ud.keycode[3]); |
| 319 | } |
| 320 | } |
| 321 | #endif |
| 322 | |
| 323 | #define UKBDUNIT(dev) (minor(dev)) |
| 324 | #define UKBD_CHUNK 128 /* chunk size for read */ |
| 325 | #define UKBD_BSIZE 1020 /* buffer size */ |
| 326 | |
| 327 | Static int ukbd_is_console; |
| 328 | |
| 329 | Static void ukbd_cngetc(void *, u_int *, int *); |
| 330 | Static void ukbd_cnpollc(void *, int); |
| 331 | |
| 332 | const struct wskbd_consops ukbd_consops = { |
| 333 | .getc = ukbd_cngetc, |
| 334 | .pollc = ukbd_cnpollc, |
| 335 | .bell = NULL, |
| 336 | }; |
| 337 | |
| 338 | Static const char *ukbd_parse_desc(struct ukbd_softc *); |
| 339 | |
| 340 | Static void ukbd_intr(struct uhidev *, void *, u_int); |
| 341 | Static void ukbd_decode(struct ukbd_softc *, struct ukbd_data *); |
| 342 | Static void ukbd_delayed_decode(void *); |
| 343 | |
| 344 | Static int ukbd_enable(void *, int); |
| 345 | Static void ukbd_set_leds(void *, int); |
| 346 | Static void ukbd_set_leds_task(void *); |
| 347 | |
| 348 | Static int ukbd_ioctl(void *, u_long, void *, int, struct lwp *); |
| 349 | #if defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT) |
| 350 | Static void ukbd_rawrepeat(void *v); |
| 351 | #endif |
| 352 | |
| 353 | const struct wskbd_accessops ukbd_accessops = { |
| 354 | ukbd_enable, |
| 355 | ukbd_set_leds, |
| 356 | ukbd_ioctl, |
| 357 | }; |
| 358 | |
| 359 | extern const struct wscons_keydesc ukbd_keydesctab[]; |
| 360 | |
| 361 | const struct wskbd_mapdata ukbd_keymapdata = { |
| 362 | ukbd_keydesctab, |
| 363 | #if defined(UKBD_LAYOUT) |
| 364 | UKBD_LAYOUT, |
| 365 | #elif defined(PCKBD_LAYOUT) |
| 366 | PCKBD_LAYOUT, |
| 367 | #else |
| 368 | KB_US, |
| 369 | #endif |
| 370 | }; |
| 371 | |
| 372 | static int ukbd_match(device_t, cfdata_t, void *); |
| 373 | static void ukbd_attach(device_t, device_t, void *); |
| 374 | static int ukbd_detach(device_t, int); |
| 375 | static int ukbd_activate(device_t, enum devact); |
| 376 | static void ukbd_childdet(device_t, device_t); |
| 377 | |
| 378 | extern struct cfdriver ukbd_cd; |
| 379 | |
| 380 | CFATTACH_DECL2_NEW(ukbd, sizeof(struct ukbd_softc), ukbd_match, ukbd_attach, |
| 381 | ukbd_detach, ukbd_activate, NULL, ukbd_childdet); |
| 382 | |
| 383 | int |
| 384 | ukbd_match(device_t parent, cfdata_t match, void *aux) |
| 385 | { |
| 386 | struct uhidev_attach_arg *uha = aux; |
| 387 | int size; |
| 388 | void *desc; |
| 389 | |
| 390 | uhidev_get_report_desc(uha->parent, &desc, &size); |
| 391 | if (!hid_is_collection(desc, size, uha->reportid, |
| 392 | HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) |
| 393 | return UMATCH_NONE; |
| 394 | |
| 395 | return UMATCH_IFACECLASS; |
| 396 | } |
| 397 | |
| 398 | void |
| 399 | ukbd_attach(device_t parent, device_t self, void *aux) |
| 400 | { |
| 401 | struct ukbd_softc *sc = device_private(self); |
| 402 | struct uhidev_attach_arg *uha = aux; |
| 403 | uint32_t qflags; |
| 404 | const char *parseerr; |
| 405 | struct wskbddev_attach_args a; |
| 406 | |
| 407 | sc->sc_hdev.sc_dev = self; |
| 408 | sc->sc_hdev.sc_intr = ukbd_intr; |
| 409 | sc->sc_hdev.sc_parent = uha->parent; |
| 410 | sc->sc_hdev.sc_report_id = uha->reportid; |
| 411 | sc->sc_flags = 0; |
| 412 | |
| 413 | aprint_naive("\n" ); |
| 414 | |
| 415 | if (!pmf_device_register(self, NULL, NULL)) { |
| 416 | aprint_normal("\n" ); |
| 417 | aprint_error_dev(self, "couldn't establish power handler\n" ); |
| 418 | } |
| 419 | |
| 420 | parseerr = ukbd_parse_desc(sc); |
| 421 | if (parseerr != NULL) { |
| 422 | aprint_normal("\n" ); |
| 423 | aprint_error_dev(self, "attach failed, %s\n" , parseerr); |
| 424 | return; |
| 425 | } |
| 426 | |
| 427 | /* Quirks */ |
| 428 | qflags = usbd_get_quirks(uha->parent->sc_udev)->uq_flags; |
| 429 | if (qflags & UQ_SPUR_BUT_UP) |
| 430 | sc->sc_flags |= FLAG_DEBOUNCE; |
| 431 | if (qflags & UQ_APPLE_ISO) |
| 432 | sc->sc_flags |= FLAG_APPLE_FIX_ISO; |
| 433 | |
| 434 | #ifdef GDIUM_KEYBOARD_HACK |
| 435 | if (uha->uiaa->uiaa_vendor == USB_VENDOR_CYPRESS && |
| 436 | uha->uiaa->uiaa_product == USB_PRODUCT_CYPRESS_LPRDK) |
| 437 | sc->sc_flags = FLAG_GDIUM_FN; |
| 438 | #endif |
| 439 | |
| 440 | #ifdef DIAGNOSTIC |
| 441 | aprint_normal(": %d modifier keys, %d key codes" , sc->sc_nmod, |
| 442 | sc->sc_nkeycode); |
| 443 | if (sc->sc_flags & FLAG_APPLE_FN) |
| 444 | aprint_normal(", apple fn key" ); |
| 445 | if (sc->sc_flags & FLAG_APPLE_FIX_ISO) |
| 446 | aprint_normal(", fix apple iso" ); |
| 447 | if (sc->sc_flags & FLAG_GDIUM_FN) |
| 448 | aprint_normal(", Gdium fn key" ); |
| 449 | #endif |
| 450 | aprint_normal("\n" ); |
| 451 | |
| 452 | /* |
| 453 | * Remember if we're the console keyboard. |
| 454 | * |
| 455 | * XXX This always picks the first keyboard on the |
| 456 | * first USB bus, but what else can we really do? |
| 457 | */ |
| 458 | if ((sc->sc_console_keyboard = ukbd_is_console) != 0) { |
| 459 | /* Don't let any other keyboard have it. */ |
| 460 | ukbd_is_console = 0; |
| 461 | } |
| 462 | |
| 463 | if (sc->sc_console_keyboard) { |
| 464 | DPRINTF(("ukbd_attach: console keyboard sc=%p\n" , sc)); |
| 465 | wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata); |
| 466 | ukbd_enable(sc, 1); |
| 467 | } |
| 468 | |
| 469 | a.console = sc->sc_console_keyboard; |
| 470 | |
| 471 | a.keymap = &ukbd_keymapdata; |
| 472 | |
| 473 | a.accessops = &ukbd_accessops; |
| 474 | a.accesscookie = sc; |
| 475 | |
| 476 | #ifdef UKBD_REPEAT |
| 477 | callout_init(&sc->sc_rawrepeat_ch, 0); |
| 478 | #endif |
| 479 | |
| 480 | callout_init(&sc->sc_delay, 0); |
| 481 | |
| 482 | usb_init_task(&sc->sc_ledtask, ukbd_set_leds_task, sc, 0); |
| 483 | |
| 484 | /* Flash the leds; no real purpose, just shows we're alive. */ |
| 485 | ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS |
| 486 | | WSKBD_LED_COMPOSE); |
| 487 | usbd_delay_ms(uha->parent->sc_udev, 400); |
| 488 | ukbd_set_leds(sc, 0); |
| 489 | |
| 490 | sc->sc_wskbddev = config_found(self, &a, wskbddevprint); |
| 491 | |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | int |
| 496 | ukbd_enable(void *v, int on) |
| 497 | { |
| 498 | struct ukbd_softc *sc = v; |
| 499 | |
| 500 | if (on && sc->sc_dying) |
| 501 | return EIO; |
| 502 | |
| 503 | /* Should only be called to change state */ |
| 504 | if ((sc->sc_flags & FLAG_ENABLED) != 0 && on != 0) { |
| 505 | #ifdef DIAGNOSTIC |
| 506 | printf("ukbd_enable: %s: bad call on=%d\n" , |
| 507 | device_xname(sc->sc_hdev.sc_dev), on); |
| 508 | #endif |
| 509 | return EBUSY; |
| 510 | } |
| 511 | |
| 512 | DPRINTF(("ukbd_enable: sc=%p on=%d\n" , sc, on)); |
| 513 | if (on) { |
| 514 | sc->sc_flags |= FLAG_ENABLED; |
| 515 | return uhidev_open(&sc->sc_hdev); |
| 516 | } else { |
| 517 | sc->sc_flags &= ~FLAG_ENABLED; |
| 518 | uhidev_close(&sc->sc_hdev); |
| 519 | return 0; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | |
| 524 | static void |
| 525 | ukbd_childdet(device_t self, device_t child) |
| 526 | { |
| 527 | struct ukbd_softc *sc = device_private(self); |
| 528 | |
| 529 | KASSERT(sc->sc_wskbddev == child); |
| 530 | sc->sc_wskbddev = NULL; |
| 531 | } |
| 532 | |
| 533 | int |
| 534 | ukbd_activate(device_t self, enum devact act) |
| 535 | { |
| 536 | struct ukbd_softc *sc = device_private(self); |
| 537 | |
| 538 | switch (act) { |
| 539 | case DVACT_DEACTIVATE: |
| 540 | sc->sc_dying = 1; |
| 541 | return 0; |
| 542 | default: |
| 543 | return EOPNOTSUPP; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | int |
| 548 | ukbd_detach(device_t self, int flags) |
| 549 | { |
| 550 | struct ukbd_softc *sc = device_private(self); |
| 551 | int rv = 0; |
| 552 | |
| 553 | DPRINTF(("ukbd_detach: sc=%p flags=%d\n" , sc, flags)); |
| 554 | |
| 555 | pmf_device_deregister(self); |
| 556 | |
| 557 | if (sc->sc_console_keyboard) { |
| 558 | #if 0 |
| 559 | /* |
| 560 | * XXX Should probably disconnect our consops, |
| 561 | * XXX and either notify some other keyboard that |
| 562 | * XXX it can now be the console, or if there aren't |
| 563 | * XXX any more USB keyboards, set ukbd_is_console |
| 564 | * XXX back to 1 so that the next USB keyboard attached |
| 565 | * XXX to the system will get it. |
| 566 | */ |
| 567 | panic("ukbd_detach: console keyboard" ); |
| 568 | #else |
| 569 | /* |
| 570 | * Disconnect our consops and set ukbd_is_console |
| 571 | * back to 1 so that the next USB keyboard attached |
| 572 | * to the system will get it. |
| 573 | * XXX Should notify some other keyboard that it can be |
| 574 | * XXX console, if there are any other keyboards. |
| 575 | */ |
| 576 | printf("%s: was console keyboard\n" , |
| 577 | device_xname(sc->sc_hdev.sc_dev)); |
| 578 | wskbd_cndetach(); |
| 579 | ukbd_is_console = 1; |
| 580 | #endif |
| 581 | } |
| 582 | /* No need to do reference counting of ukbd, wskbd has all the goo. */ |
| 583 | if (sc->sc_wskbddev != NULL) |
| 584 | rv = config_detach(sc->sc_wskbddev, flags); |
| 585 | |
| 586 | /* The console keyboard does not get a disable call, so check pipe. */ |
| 587 | if (sc->sc_hdev.sc_state & UHIDEV_OPEN) |
| 588 | uhidev_close(&sc->sc_hdev); |
| 589 | |
| 590 | return rv; |
| 591 | } |
| 592 | |
| 593 | static void |
| 594 | ukbd_translate_keycodes(struct ukbd_softc *sc, struct ukbd_data *ud, |
| 595 | const struct ukbd_keycodetrans *tab) |
| 596 | { |
| 597 | const struct ukbd_keycodetrans *tp; |
| 598 | int i; |
| 599 | uint8_t key; |
| 600 | |
| 601 | for (i = 0; i < sc->sc_nkeycode; i++) { |
| 602 | key = ud->keycode[i]; |
| 603 | if (key) |
| 604 | for (tp = tab; tp->from; tp++) |
| 605 | if (tp->from == key) { |
| 606 | if (tp->to & IS_PMF) { |
| 607 | pmf_event_inject( |
| 608 | sc->sc_hdev.sc_dev, |
| 609 | tp->to & 0xff); |
| 610 | ud->keycode[i] = 0; |
| 611 | } else |
| 612 | ud->keycode[i] = tp->to; |
| 613 | break; |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | static uint16_t |
| 619 | ukbd_translate_modifier(struct ukbd_softc *sc, uint16_t key) |
| 620 | { |
| 621 | if ((sc->sc_flags & FLAG_APPLE_FN) && (key & CODEMASK) == 0x00e2) { |
| 622 | if ((key & ~CODEMASK) == PRESS) { |
| 623 | if (sc->sc_flags & FLAG_FN_PRESSED) { |
| 624 | /* pressed FN-Alt, translate to AltGr */ |
| 625 | key = 0x00e6 | PRESS; |
| 626 | sc->sc_flags |= FLAG_FN_ALT; |
| 627 | } |
| 628 | } else { |
| 629 | if (sc->sc_flags & FLAG_FN_ALT) { |
| 630 | /* released Alt, which was treated as FN-Alt */ |
| 631 | key = 0x00e6 | RELEASE; |
| 632 | sc->sc_flags &= ~FLAG_FN_ALT; |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | return key; |
| 637 | } |
| 638 | |
| 639 | void |
| 640 | ukbd_intr(struct uhidev *addr, void *ibuf, u_int len) |
| 641 | { |
| 642 | struct ukbd_softc *sc = (struct ukbd_softc *)addr; |
| 643 | struct ukbd_data *ud = &sc->sc_ndata; |
| 644 | int i; |
| 645 | |
| 646 | #ifdef UKBD_DEBUG |
| 647 | if (ukbddebug > 5) { |
| 648 | printf("ukbd_intr: data" ); |
| 649 | for (i = 0; i < len; i++) |
| 650 | printf(" 0x%02x" , ((u_char *)ibuf)[i]); |
| 651 | printf("\n" ); |
| 652 | } |
| 653 | #endif |
| 654 | |
| 655 | ud->modifiers = 0; |
| 656 | for (i = 0; i < sc->sc_nmod; i++) |
| 657 | if (hid_get_data(ibuf, &sc->sc_modloc[i])) |
| 658 | ud->modifiers |= sc->sc_mods[i].mask; |
| 659 | memcpy(ud->keycode, (char *)ibuf + sc->sc_keycodeloc.pos / 8, |
| 660 | sc->sc_nkeycode); |
| 661 | |
| 662 | if (sc->sc_flags & FLAG_APPLE_FN) { |
| 663 | if (hid_get_data(ibuf, &sc->sc_apple_fn)) { |
| 664 | sc->sc_flags |= FLAG_FN_PRESSED; |
| 665 | ukbd_translate_keycodes(sc, ud, trtab_apple_fn); |
| 666 | } |
| 667 | else |
| 668 | sc->sc_flags &= ~FLAG_FN_PRESSED; |
| 669 | } |
| 670 | |
| 671 | #ifdef GDIUM_KEYBOARD_HACK |
| 672 | if (sc->sc_flags & FLAG_GDIUM_FN) { |
| 673 | if (sc->sc_flags & FLAG_FN_PRESSED) { |
| 674 | ukbd_translate_keycodes(sc, ud, trtab_gdium_fn); |
| 675 | } |
| 676 | } |
| 677 | #endif |
| 678 | |
| 679 | ukbd_translate_keycodes(sc, ud, trtab_generic); |
| 680 | |
| 681 | if ((sc->sc_flags & FLAG_DEBOUNCE) && !(sc->sc_flags & FLAG_POLLING)) { |
| 682 | /* |
| 683 | * Some keyboards have a peculiar quirk. They sometimes |
| 684 | * generate a key up followed by a key down for the same |
| 685 | * key after about 10 ms. |
| 686 | * We avoid this bug by holding off decoding for 20 ms. |
| 687 | */ |
| 688 | sc->sc_data = *ud; |
| 689 | callout_reset(&sc->sc_delay, hz / 50, ukbd_delayed_decode, sc); |
| 690 | #ifdef DDB |
| 691 | } else if (sc->sc_console_keyboard && !(sc->sc_flags & FLAG_POLLING)) { |
| 692 | /* |
| 693 | * For the console keyboard we can't deliver CTL-ALT-ESC |
| 694 | * from the interrupt routine. Doing so would start |
| 695 | * polling from inside the interrupt routine and that |
| 696 | * loses bigtime. |
| 697 | */ |
| 698 | sc->sc_data = *ud; |
| 699 | callout_reset(&sc->sc_delay, 1, ukbd_delayed_decode, sc); |
| 700 | #endif |
| 701 | } else { |
| 702 | ukbd_decode(sc, ud); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | void |
| 707 | ukbd_delayed_decode(void *addr) |
| 708 | { |
| 709 | struct ukbd_softc *sc = addr; |
| 710 | |
| 711 | ukbd_decode(sc, &sc->sc_data); |
| 712 | } |
| 713 | |
| 714 | void |
| 715 | ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud) |
| 716 | { |
| 717 | int mod, omod; |
| 718 | uint16_t ibuf[MAXKEYS]; /* chars events */ |
| 719 | int s; |
| 720 | int nkeys, i, j; |
| 721 | int key; |
| 722 | #define ADDKEY(c) ibuf[nkeys++] = (c) |
| 723 | |
| 724 | #ifdef UKBD_DEBUG |
| 725 | /* |
| 726 | * Keep a trace of the last events. Using printf changes the |
| 727 | * timing, so this can be useful sometimes. |
| 728 | */ |
| 729 | if (ukbdtrace) { |
| 730 | struct ukbdtraceinfo *p = &ukbdtracedata[ukbdtraceindex]; |
| 731 | p->unit = device_unit(sc->sc_hdev.sc_dev); |
| 732 | microtime(&p->tv); |
| 733 | p->ud = *ud; |
| 734 | if (++ukbdtraceindex >= UKBDTRACESIZE) |
| 735 | ukbdtraceindex = 0; |
| 736 | } |
| 737 | if (ukbddebug > 5) { |
| 738 | struct timeval tv; |
| 739 | microtime(&tv); |
| 740 | DPRINTF((" at %" PRIu64".%06" PRIu64" mod=0x%02x key0=0x%02x key1=0x%02x " |
| 741 | "key2=0x%02x key3=0x%02x\n" , |
| 742 | tv.tv_sec, (uint64_t)tv.tv_usec, |
| 743 | ud->modifiers, ud->keycode[0], ud->keycode[1], |
| 744 | ud->keycode[2], ud->keycode[3])); |
| 745 | } |
| 746 | #endif |
| 747 | |
| 748 | if (ud->keycode[0] == KEY_ERROR) { |
| 749 | DPRINTF(("ukbd_intr: KEY_ERROR\n" )); |
| 750 | return; /* ignore */ |
| 751 | } |
| 752 | |
| 753 | if (sc->sc_flags & FLAG_APPLE_FIX_ISO) |
| 754 | ukbd_translate_keycodes(sc, ud, trtab_apple_iso); |
| 755 | |
| 756 | nkeys = 0; |
| 757 | mod = ud->modifiers; |
| 758 | omod = sc->sc_odata.modifiers; |
| 759 | if (mod != omod) |
| 760 | for (i = 0; i < sc->sc_nmod; i++) |
| 761 | if (( mod & sc->sc_mods[i].mask) != |
| 762 | (omod & sc->sc_mods[i].mask)) { |
| 763 | key = sc->sc_mods[i].key | |
| 764 | ((mod & sc->sc_mods[i].mask) ? |
| 765 | PRESS : RELEASE); |
| 766 | ADDKEY(ukbd_translate_modifier(sc, key)); |
| 767 | } |
| 768 | if (memcmp(ud->keycode, sc->sc_odata.keycode, sc->sc_nkeycode) != 0) { |
| 769 | /* Check for released keys. */ |
| 770 | for (i = 0; i < sc->sc_nkeycode; i++) { |
| 771 | key = sc->sc_odata.keycode[i]; |
| 772 | if (key == 0) |
| 773 | continue; |
| 774 | for (j = 0; j < sc->sc_nkeycode; j++) |
| 775 | if (key == ud->keycode[j]) |
| 776 | goto rfound; |
| 777 | DPRINTFN(3,("ukbd_intr: relse key=0x%02x\n" , key)); |
| 778 | #ifdef GDIUM_KEYBOARD_HACK |
| 779 | if (sc->sc_flags & FLAG_GDIUM_FN) { |
| 780 | if (key == 0x82) { |
| 781 | sc->sc_flags &= ~FLAG_FN_PRESSED; |
| 782 | goto rfound; |
| 783 | } |
| 784 | } |
| 785 | #endif |
| 786 | ADDKEY(key | RELEASE); |
| 787 | rfound: |
| 788 | ; |
| 789 | } |
| 790 | |
| 791 | /* Check for pressed keys. */ |
| 792 | for (i = 0; i < sc->sc_nkeycode; i++) { |
| 793 | key = ud->keycode[i]; |
| 794 | if (key == 0) |
| 795 | continue; |
| 796 | for (j = 0; j < sc->sc_nkeycode; j++) |
| 797 | if (key == sc->sc_odata.keycode[j]) |
| 798 | goto pfound; |
| 799 | DPRINTFN(2,("ukbd_intr: press key=0x%02x\n" , key)); |
| 800 | #ifdef GDIUM_KEYBOARD_HACK |
| 801 | if (sc->sc_flags & FLAG_GDIUM_FN) { |
| 802 | if (key == 0x82) { |
| 803 | sc->sc_flags |= FLAG_FN_PRESSED; |
| 804 | goto pfound; |
| 805 | } |
| 806 | } |
| 807 | #endif |
| 808 | ADDKEY(key | PRESS); |
| 809 | pfound: |
| 810 | ; |
| 811 | } |
| 812 | } |
| 813 | sc->sc_odata = *ud; |
| 814 | |
| 815 | if (nkeys == 0) |
| 816 | return; |
| 817 | |
| 818 | if (sc->sc_flags & FLAG_POLLING) { |
| 819 | DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n" , ibuf[0])); |
| 820 | memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(uint16_t)); |
| 821 | sc->sc_npollchar = nkeys; |
| 822 | return; |
| 823 | } |
| 824 | #ifdef WSDISPLAY_COMPAT_RAWKBD |
| 825 | if (sc->sc_rawkbd) { |
| 826 | u_char cbuf[MAXKEYS * 2]; |
| 827 | int c; |
| 828 | #if defined(UKBD_REPEAT) |
| 829 | int npress = 0; |
| 830 | #endif |
| 831 | |
| 832 | for (i = j = 0; i < nkeys; i++) { |
| 833 | key = ibuf[i]; |
| 834 | c = ukbd_trtab[key & CODEMASK]; |
| 835 | if (c == NN) |
| 836 | continue; |
| 837 | if (c == 0x7f) { |
| 838 | /* pause key */ |
| 839 | cbuf[j++] = 0xe1; |
| 840 | cbuf[j++] = 0x1d; |
| 841 | cbuf[j-1] |= (key & RELEASE) ? 0x80 : 0; |
| 842 | cbuf[j] = 0x45; |
| 843 | } else { |
| 844 | if (c & 0x80) |
| 845 | cbuf[j++] = 0xe0; |
| 846 | cbuf[j] = c & 0x7f; |
| 847 | } |
| 848 | if (key & RELEASE) |
| 849 | cbuf[j] |= 0x80; |
| 850 | #if defined(UKBD_REPEAT) |
| 851 | else { |
| 852 | /* remember pressed keys for autorepeat */ |
| 853 | if (c & 0x80) |
| 854 | sc->sc_rep[npress++] = 0xe0; |
| 855 | sc->sc_rep[npress++] = c & 0x7f; |
| 856 | } |
| 857 | #endif |
| 858 | DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n" , |
| 859 | c & 0x80 ? "0xe0 " : "" , |
| 860 | cbuf[j])); |
| 861 | j++; |
| 862 | } |
| 863 | s = spltty(); |
| 864 | wskbd_rawinput(sc->sc_wskbddev, cbuf, j); |
| 865 | splx(s); |
| 866 | #ifdef UKBD_REPEAT |
| 867 | callout_stop(&sc->sc_rawrepeat_ch); |
| 868 | if (npress != 0) { |
| 869 | sc->sc_nrep = npress; |
| 870 | callout_reset(&sc->sc_rawrepeat_ch, |
| 871 | hz * REP_DELAY1 / 1000, ukbd_rawrepeat, sc); |
| 872 | } |
| 873 | #endif |
| 874 | return; |
| 875 | } |
| 876 | #endif |
| 877 | |
| 878 | s = spltty(); |
| 879 | for (i = 0; i < nkeys; i++) { |
| 880 | key = ibuf[i]; |
| 881 | wskbd_input(sc->sc_wskbddev, |
| 882 | key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN, |
| 883 | key&CODEMASK); |
| 884 | } |
| 885 | splx(s); |
| 886 | } |
| 887 | |
| 888 | void |
| 889 | ukbd_set_leds(void *v, int leds) |
| 890 | { |
| 891 | struct ukbd_softc *sc = v; |
| 892 | struct usbd_device *udev = sc->sc_hdev.sc_parent->sc_udev; |
| 893 | |
| 894 | DPRINTF(("ukbd_set_leds: sc=%p leds=%d, sc_leds=%d\n" , |
| 895 | sc, leds, sc->sc_leds)); |
| 896 | |
| 897 | if (sc->sc_dying) |
| 898 | return; |
| 899 | |
| 900 | if (sc->sc_leds == leds) |
| 901 | return; |
| 902 | |
| 903 | sc->sc_leds = leds; |
| 904 | usb_add_task(udev, &sc->sc_ledtask, USB_TASKQ_DRIVER); |
| 905 | } |
| 906 | |
| 907 | void |
| 908 | ukbd_set_leds_task(void *v) |
| 909 | { |
| 910 | struct ukbd_softc *sc = v; |
| 911 | int leds = sc->sc_leds; |
| 912 | uint8_t res = 0; |
| 913 | |
| 914 | /* XXX not really right */ |
| 915 | if ((leds & WSKBD_LED_COMPOSE) && sc->sc_compose.size == 1) |
| 916 | res |= 1 << sc->sc_compose.pos; |
| 917 | if ((leds & WSKBD_LED_SCROLL) && sc->sc_scroloc.size == 1) |
| 918 | res |= 1 << sc->sc_scroloc.pos; |
| 919 | if ((leds & WSKBD_LED_NUM) && sc->sc_numloc.size == 1) |
| 920 | res |= 1 << sc->sc_numloc.pos; |
| 921 | if ((leds & WSKBD_LED_CAPS) && sc->sc_capsloc.size == 1) |
| 922 | res |= 1 << sc->sc_capsloc.pos; |
| 923 | |
| 924 | uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT, &res, 1); |
| 925 | } |
| 926 | |
| 927 | #if defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT) |
| 928 | void |
| 929 | ukbd_rawrepeat(void *v) |
| 930 | { |
| 931 | struct ukbd_softc *sc = v; |
| 932 | int s; |
| 933 | |
| 934 | s = spltty(); |
| 935 | wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep); |
| 936 | splx(s); |
| 937 | callout_reset(&sc->sc_rawrepeat_ch, hz * REP_DELAYN / 1000, |
| 938 | ukbd_rawrepeat, sc); |
| 939 | } |
| 940 | #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT) */ |
| 941 | |
| 942 | int |
| 943 | ukbd_ioctl(void *v, u_long cmd, void *data, int flag, |
| 944 | struct lwp *l) |
| 945 | { |
| 946 | struct ukbd_softc *sc = v; |
| 947 | |
| 948 | switch (cmd) { |
| 949 | case WSKBDIO_GTYPE: |
| 950 | *(int *)data = WSKBD_TYPE_USB; |
| 951 | return 0; |
| 952 | case WSKBDIO_SETLEDS: |
| 953 | ukbd_set_leds(v, *(int *)data); |
| 954 | return 0; |
| 955 | case WSKBDIO_GETLEDS: |
| 956 | *(int *)data = sc->sc_leds; |
| 957 | return 0; |
| 958 | #if defined(WSDISPLAY_COMPAT_RAWKBD) |
| 959 | case WSKBDIO_SETMODE: |
| 960 | DPRINTF(("ukbd_ioctl: set raw = %d\n" , *(int *)data)); |
| 961 | sc->sc_rawkbd = *(int *)data == WSKBD_RAW; |
| 962 | #if defined(UKBD_REPEAT) |
| 963 | callout_stop(&sc->sc_rawrepeat_ch); |
| 964 | #endif |
| 965 | return 0; |
| 966 | #endif |
| 967 | } |
| 968 | return EPASSTHROUGH; |
| 969 | } |
| 970 | |
| 971 | /* |
| 972 | * This is a hack to work around some broken ports that don't call |
| 973 | * cnpollc() before cngetc(). |
| 974 | */ |
| 975 | static int pollenter, warned; |
| 976 | |
| 977 | /* Console interface. */ |
| 978 | void |
| 979 | ukbd_cngetc(void *v, u_int *type, int *data) |
| 980 | { |
| 981 | struct ukbd_softc *sc = v; |
| 982 | int c; |
| 983 | int broken; |
| 984 | |
| 985 | if (pollenter == 0) { |
| 986 | if (!warned) { |
| 987 | printf("\n" |
| 988 | "This port is broken, it does not call cnpollc() before calling cngetc().\n" |
| 989 | "This should be fixed, but it will work anyway (for now).\n" ); |
| 990 | warned = 1; |
| 991 | } |
| 992 | broken = 1; |
| 993 | ukbd_cnpollc(v, 1); |
| 994 | } else |
| 995 | broken = 0; |
| 996 | |
| 997 | DPRINTFN(0,("ukbd_cngetc: enter\n" )); |
| 998 | sc->sc_flags |= FLAG_POLLING; |
| 999 | while (sc->sc_npollchar <= 0) |
| 1000 | usbd_dopoll(sc->sc_hdev.sc_parent->sc_iface); |
| 1001 | sc->sc_flags &= ~FLAG_POLLING; |
| 1002 | c = sc->sc_pollchars[0]; |
| 1003 | sc->sc_npollchar--; |
| 1004 | memcpy(sc->sc_pollchars, sc->sc_pollchars+1, |
| 1005 | sc->sc_npollchar * sizeof(uint16_t)); |
| 1006 | *type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN; |
| 1007 | *data = c & CODEMASK; |
| 1008 | DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n" , c)); |
| 1009 | if (broken) |
| 1010 | ukbd_cnpollc(v, 0); |
| 1011 | } |
| 1012 | |
| 1013 | void |
| 1014 | ukbd_cnpollc(void *v, int on) |
| 1015 | { |
| 1016 | struct ukbd_softc *sc = v; |
| 1017 | struct usbd_device *dev; |
| 1018 | |
| 1019 | DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n" , v, on)); |
| 1020 | |
| 1021 | usbd_interface2device_handle(sc->sc_hdev.sc_parent->sc_iface, &dev); |
| 1022 | if (on) { |
| 1023 | sc->sc_spl = splusb(); |
| 1024 | pollenter++; |
| 1025 | } else { |
| 1026 | splx(sc->sc_spl); |
| 1027 | pollenter--; |
| 1028 | } |
| 1029 | usbd_set_polling(dev, on); |
| 1030 | } |
| 1031 | |
| 1032 | int |
| 1033 | ukbd_cnattach(void) |
| 1034 | { |
| 1035 | |
| 1036 | /* |
| 1037 | * XXX USB requires too many parts of the kernel to be running |
| 1038 | * XXX in order to work, so we can't do much for the console |
| 1039 | * XXX keyboard until autconfiguration has run its course. |
| 1040 | */ |
| 1041 | ukbd_is_console = 1; |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | const char * |
| 1046 | ukbd_parse_desc(struct ukbd_softc *sc) |
| 1047 | { |
| 1048 | struct hid_data *d; |
| 1049 | struct hid_item h; |
| 1050 | int size; |
| 1051 | void *desc; |
| 1052 | int imod; |
| 1053 | |
| 1054 | uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size); |
| 1055 | imod = 0; |
| 1056 | sc->sc_nkeycode = 0; |
| 1057 | d = hid_start_parse(desc, size, hid_input); |
| 1058 | while (hid_get_item(d, &h)) { |
| 1059 | /*printf("ukbd: id=%d kind=%d usage=0x%x flags=0x%x pos=%d size=%d cnt=%d\n", |
| 1060 | h.report_ID, h.kind, h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count);*/ |
| 1061 | |
| 1062 | /* Check for special Apple notebook FN key */ |
| 1063 | if (HID_GET_USAGE_PAGE(h.usage) == 0x00ff && |
| 1064 | HID_GET_USAGE(h.usage) == 0x0003 && |
| 1065 | h.kind == hid_input && (h.flags & HIO_VARIABLE)) { |
| 1066 | sc->sc_flags |= FLAG_APPLE_FN; |
| 1067 | sc->sc_apple_fn = h.loc; |
| 1068 | } |
| 1069 | |
| 1070 | if (h.kind != hid_input || (h.flags & HIO_CONST) || |
| 1071 | HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD || |
| 1072 | h.report_ID != sc->sc_hdev.sc_report_id) |
| 1073 | continue; |
| 1074 | DPRINTF(("ukbd: imod=%d usage=0x%x flags=0x%x pos=%d size=%d " |
| 1075 | "cnt=%d\n" , imod, |
| 1076 | h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count)); |
| 1077 | if (h.flags & HIO_VARIABLE) { |
| 1078 | if (h.loc.size != 1) |
| 1079 | return "bad modifier size" ; |
| 1080 | /* Single item */ |
| 1081 | if (imod < MAXMOD) { |
| 1082 | sc->sc_modloc[imod] = h.loc; |
| 1083 | sc->sc_mods[imod].mask = 1 << imod; |
| 1084 | sc->sc_mods[imod].key = HID_GET_USAGE(h.usage); |
| 1085 | imod++; |
| 1086 | } else |
| 1087 | return "too many modifier keys" ; |
| 1088 | } else { |
| 1089 | /* Array */ |
| 1090 | if (h.loc.size != 8) |
| 1091 | return "key code size != 8" ; |
| 1092 | if (h.loc.count > MAXKEYCODE) |
| 1093 | h.loc.count = MAXKEYCODE; |
| 1094 | if (h.loc.pos % 8 != 0) |
| 1095 | return "key codes not on byte boundary" ; |
| 1096 | if (sc->sc_nkeycode != 0) |
| 1097 | return "multiple key code arrays" ; |
| 1098 | sc->sc_keycodeloc = h.loc; |
| 1099 | sc->sc_nkeycode = h.loc.count; |
| 1100 | } |
| 1101 | } |
| 1102 | sc->sc_nmod = imod; |
| 1103 | hid_end_parse(d); |
| 1104 | |
| 1105 | hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_NUM_LOCK), |
| 1106 | sc->sc_hdev.sc_report_id, hid_output, &sc->sc_numloc, NULL); |
| 1107 | hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_CAPS_LOCK), |
| 1108 | sc->sc_hdev.sc_report_id, hid_output, &sc->sc_capsloc, NULL); |
| 1109 | hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_SCROLL_LOCK), |
| 1110 | sc->sc_hdev.sc_report_id, hid_output, &sc->sc_scroloc, NULL); |
| 1111 | hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_COMPOSE), |
| 1112 | sc->sc_hdev.sc_report_id, hid_output, &sc->sc_compose, NULL); |
| 1113 | |
| 1114 | return NULL; |
| 1115 | } |
| 1116 | |