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