Add a field to the keyboard abstraction structure that allows the USB
[dragonfly/vkernel-mp.git] / sys / dev / usbmisc / ukbd / ukbd.c
blobdd306fef0d5123745c0acb0cf1b111416ae0cf1e
1 /*
2 * $FreeBSD: src/sys/dev/usb/ukbd.c,v 1.45 2003/10/04 21:41:01 joe Exp $
3 * $DragonFly: src/sys/dev/usbmisc/ukbd/ukbd.c,v 1.18 2007/06/06 18:49:28 dillon Exp $
4 */
6 /*
7 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * All rights reserved.
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Lennart Augustsson (lennart@augustsson.net) at
12 * Carlstedt Research & Technology.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the NetBSD
25 * Foundation, Inc. and its contributors.
26 * 4. Neither the name of The NetBSD Foundation nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
44 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
47 #include "opt_kbd.h"
48 #include "opt_ukbd.h"
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/ioccom.h>
54 #include <sys/module.h>
55 #include <sys/bus.h>
56 #include <sys/file.h>
57 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
58 #include <sys/limits.h>
59 #else
60 #include <machine/limits.h>
61 #endif
62 #if defined(__FreeBSD__) && __FreeBSD_version >= 500014
63 #include <sys/selinfo.h>
64 #else
65 #include <sys/select.h>
66 #endif
67 #include <sys/vnode.h>
68 #include <sys/sysctl.h>
69 #include <sys/thread2.h>
71 #include <bus/usb/usb.h>
72 #include <bus/usb/usbhid.h>
73 #include <bus/usb/usbdi.h>
74 #include <bus/usb/usbdi_util.h>
75 #include <bus/usb/usbdevs.h>
76 #include <bus/usb/usb_quirks.h>
77 #include <bus/usb/hid.h>
79 #include <sys/kbio.h>
80 #include <dev/misc/kbd/kbdreg.h>
82 #define UKBD_EMULATE_ATSCANCODE 1
84 #define DRIVER_NAME "ukbd"
86 #define delay(d) DELAY(d)
88 #ifdef USB_DEBUG
89 #define DPRINTF(x) if (ukbddebug) logprintf x
90 #define DPRINTFN(n,x) if (ukbddebug>(n)) logprintf x
91 int ukbddebug = 0;
92 SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd");
93 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW,
94 &ukbddebug, 0, "ukbd debug level");
95 #else
96 #define DPRINTF(x)
97 #define DPRINTFN(n,x)
98 #endif
100 #define UPROTO_BOOT_KEYBOARD 1
102 #define NKEYCODE 6
104 struct ukbd_data {
105 u_int8_t modifiers;
106 #define MOD_CONTROL_L 0x01
107 #define MOD_CONTROL_R 0x10
108 #define MOD_SHIFT_L 0x02
109 #define MOD_SHIFT_R 0x20
110 #define MOD_ALT_L 0x04
111 #define MOD_ALT_R 0x40
112 #define MOD_WIN_L 0x08
113 #define MOD_WIN_R 0x80
114 u_int8_t reserved;
115 u_int8_t keycode[NKEYCODE];
118 #define MAXKEYS (NMOD+2*NKEYCODE)
120 typedef struct ukbd_softc {
121 device_t sc_dev; /* base device */
122 } ukbd_softc_t;
124 #define UKBD_CHUNK 128 /* chunk size for read */
125 #define UKBD_BSIZE 1020 /* buffer size */
127 typedef void usbd_intr_t(usbd_xfer_handle, usbd_private_handle, usbd_status);
128 typedef void usbd_disco_t(void *);
130 Static int ukbd_resume(device_t self);
131 Static usbd_intr_t ukbd_intr;
132 Static int ukbd_driver_load(module_t mod, int what, void *arg);
133 Static int ukbd_default_term(keyboard_t *kbd);
135 Static keyboard_t default_kbd;
137 USB_DECLARE_DRIVER_INIT(ukbd, DEVMETHOD(device_resume, ukbd_resume));
139 USB_MATCH(ukbd)
141 USB_MATCH_START(ukbd, uaa);
143 keyboard_switch_t *sw;
144 void *arg[2];
145 int unit = device_get_unit(self);
147 sw = kbd_get_switch(DRIVER_NAME);
148 if (sw == NULL)
149 return (UMATCH_NONE);
151 arg[0] = (void *)uaa;
152 arg[1] = (void *)ukbd_intr;
153 if ((*sw->probe)(unit, (void *)arg, 0))
154 return (UMATCH_NONE);
156 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
159 USB_ATTACH(ukbd)
161 USB_ATTACH_START(ukbd, sc, uaa);
162 usbd_interface_handle iface = uaa->iface;
163 usb_interface_descriptor_t *id;
164 char devinfo[1024];
166 keyboard_switch_t *sw;
167 keyboard_t *kbd;
168 void *arg[2];
169 int unit = device_get_unit(self);
171 sw = kbd_get_switch(DRIVER_NAME);
172 if (sw == NULL)
173 USB_ATTACH_ERROR_RETURN;
175 id = usbd_get_interface_descriptor(iface);
176 usbd_devinfo(uaa->device, 0, devinfo);
177 USB_ATTACH_SETUP;
178 kprintf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
179 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
181 arg[0] = (void *)uaa;
182 arg[1] = (void *)ukbd_intr;
183 kbd = NULL;
184 if ((*sw->probe)(unit, (void *)arg, 0))
185 USB_ATTACH_ERROR_RETURN;
186 if ((*sw->init)(unit, &kbd, (void *)arg, 0))
187 USB_ATTACH_ERROR_RETURN;
188 (*sw->enable)(kbd);
190 #ifdef KBD_INSTALL_CDEV
191 if (kbd_attach(kbd))
192 USB_ATTACH_ERROR_RETURN;
193 #endif
194 if (bootverbose)
195 (*sw->diag)(kbd, bootverbose);
197 USB_ATTACH_SUCCESS_RETURN;
201 ukbd_detach(device_t self)
203 keyboard_t *kbd;
204 int error;
206 kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME,
207 device_get_unit(self)));
208 if (kbd == NULL) {
209 DPRINTF(("%s: keyboard not attached!?\n", USBDEVNAME(self)));
210 return ENXIO;
212 (*kbdsw[kbd->kb_index]->disable)(kbd);
214 #ifdef KBD_INSTALL_CDEV
215 if (kbd != &default_kbd) {
216 error = kbd_detach(kbd);
217 if (error)
218 return error;
220 #endif
221 if (kbd == &default_kbd) {
222 ukbd_default_term(kbd);
223 } else {
224 error = (*kbdsw[kbd->kb_index]->term)(kbd);
225 if (error)
226 return error;
229 DPRINTF(("%s: disconnected\n", USBDEVNAME(self)));
231 return (0);
234 Static int
235 ukbd_resume(device_t self)
237 keyboard_t *kbd;
239 kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME,
240 device_get_unit(self)));
241 if (kbd)
242 (*kbdsw[kbd->kb_index]->clear_state)(kbd);
243 return (0);
246 void
247 ukbd_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
249 keyboard_t *kbd = (keyboard_t *)addr;
251 (*kbdsw[kbd->kb_index]->intr)(kbd, (void *)status);
254 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
257 #define UKBD_DEFAULT 0
259 #define KEY_ERROR 0x01
261 #define KEY_PRESS 0
262 #define KEY_RELEASE 0x400
263 #define KEY_INDEX(c) ((c) & ~KEY_RELEASE)
265 #define SCAN_PRESS 0
266 #define SCAN_RELEASE 0x80
267 #define SCAN_PREFIX_E0 0x100
268 #define SCAN_PREFIX_E1 0x200
269 #define SCAN_PREFIX_CTL 0x400
270 #define SCAN_PREFIX_SHIFT 0x800
271 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL \
272 | SCAN_PREFIX_SHIFT)
273 #define SCAN_CHAR(c) ((c) & 0x7f)
275 #define NMOD 8
276 Static struct {
277 int mask, key;
278 } ukbd_mods[NMOD] = {
279 { MOD_CONTROL_L, 0xe0 },
280 { MOD_CONTROL_R, 0xe4 },
281 { MOD_SHIFT_L, 0xe1 },
282 { MOD_SHIFT_R, 0xe5 },
283 { MOD_ALT_L, 0xe2 },
284 { MOD_ALT_R, 0xe6 },
285 { MOD_WIN_L, 0xe3 },
286 { MOD_WIN_R, 0xe7 },
289 #define NN 0 /* no translation */
291 * Translate USB keycodes to AT keyboard scancodes.
294 * FIXME: Mac USB keyboard generates:
295 * 0x53: keypad NumLock/Clear
296 * 0x66: Power
297 * 0x67: keypad =
298 * 0x68: F13
299 * 0x69: F14
300 * 0x6a: F15
302 Static u_int8_t ukbd_trtab[256] = {
303 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
304 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
305 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
306 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
307 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
308 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
309 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
310 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
311 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
312 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
313 97, 100, 95, 69, 91, 55, 74, 78, /* 50 - 57 */
314 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
315 72, 73, 82, 83, 86, 107, NN, NN, /* 60 - 67 */
316 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
317 NN, NN, NN, NN, NN, NN, NN, NN, /* 70 - 77 */
318 NN, NN, NN, NN, NN, NN, NN, NN, /* 78 - 7F */
319 NN, NN, NN, NN, NN, NN, NN, 115, /* 80 - 87 */
320 112, 125, 121, 123, NN, NN, NN, NN, /* 88 - 8F */
321 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
322 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
323 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
324 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
325 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
326 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
327 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
328 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
329 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
330 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
331 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
332 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
333 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
334 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
337 typedef struct ukbd_state {
338 usbd_interface_handle ks_iface; /* interface */
339 usbd_pipe_handle ks_intrpipe; /* interrupt pipe */
340 struct usb_attach_arg *ks_uaa;
341 int ks_ep_addr;
343 struct ukbd_data ks_ndata;
344 struct ukbd_data ks_odata;
345 u_long ks_ntime[NKEYCODE];
346 u_long ks_otime[NKEYCODE];
348 #define INPUTBUFSIZE (NMOD + 2*NKEYCODE)
349 u_int ks_input[INPUTBUFSIZE]; /* input buffer */
350 int ks_inputs;
351 int ks_inputhead;
352 int ks_inputtail;
354 int ks_ifstate;
355 #define INTRENABLED (1 << 0)
356 #define DISCONNECTED (1 << 1)
358 struct callout ks_timeout;
360 int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
361 int ks_flags; /* flags */
362 #define COMPOSE (1 << 0)
363 int ks_polling;
364 int ks_state; /* shift/lock key state */
365 int ks_accents; /* accent key index (> 0) */
366 u_int ks_composed_char; /* composed char code (> 0) */
367 #ifdef UKBD_EMULATE_ATSCANCODE
368 u_int ks_buffered_char[2];
369 #endif
370 } ukbd_state_t;
372 /* keyboard driver declaration */
373 Static int ukbd_configure(int flags);
374 Static kbd_probe_t ukbd_probe;
375 Static kbd_init_t ukbd_init;
376 Static kbd_term_t ukbd_term;
377 Static kbd_intr_t ukbd_interrupt;
378 Static kbd_test_if_t ukbd_test_if;
379 Static kbd_enable_t ukbd_enable;
380 Static kbd_disable_t ukbd_disable;
381 Static kbd_read_t ukbd_read;
382 Static kbd_check_t ukbd_check;
383 Static kbd_read_char_t ukbd_read_char;
384 Static kbd_check_char_t ukbd_check_char;
385 Static kbd_ioctl_t ukbd_ioctl;
386 Static kbd_lock_t ukbd_lock;
387 Static kbd_clear_state_t ukbd_clear_state;
388 Static kbd_get_state_t ukbd_get_state;
389 Static kbd_set_state_t ukbd_set_state;
390 Static kbd_poll_mode_t ukbd_poll;
392 keyboard_switch_t ukbdsw = {
393 ukbd_probe,
394 ukbd_init,
395 ukbd_term,
396 ukbd_interrupt,
397 ukbd_test_if,
398 ukbd_enable,
399 ukbd_disable,
400 ukbd_read,
401 ukbd_check,
402 ukbd_read_char,
403 ukbd_check_char,
404 ukbd_ioctl,
405 ukbd_lock,
406 ukbd_clear_state,
407 ukbd_get_state,
408 ukbd_set_state,
409 genkbd_get_fkeystr,
410 ukbd_poll,
411 genkbd_diag,
414 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
416 /* local functions */
417 Static int ukbd_enable_intr(keyboard_t *kbd, int on,
418 usbd_intr_t *func);
419 Static timeout_t ukbd_timeout;
421 Static int ukbd_getc(ukbd_state_t *state);
422 Static int probe_keyboard(struct usb_attach_arg *uaa, int flags);
423 Static int init_keyboard(ukbd_state_t *state, int *type,
424 int flags);
425 Static void set_leds(ukbd_state_t *state, int leds);
426 Static int set_typematic(keyboard_t *kbd, int code);
427 #ifdef UKBD_EMULATE_ATSCANCODE
428 Static int keycode2scancode(int keycode, int shift, int up);
429 #endif
431 /* local variables */
433 /* the initial key map, accent map and fkey strings */
434 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
435 #define KBD_DFLT_KEYMAP
436 #include "ukbdmap.h"
437 #endif
438 #include <dev/misc/kbd/kbdtables.h>
440 /* structures for the default keyboard */
441 Static ukbd_state_t default_kbd_state;
442 Static keymap_t default_keymap;
443 Static accentmap_t default_accentmap;
444 Static fkeytab_t default_fkeytab[NUM_FKEYS];
447 * The back door to the keyboard driver!
448 * This function is called by the console driver, via the kbdio module,
449 * to tickle keyboard drivers when the low-level console is being initialized.
450 * Almost nothing in the kernel has been initialied yet. Try to probe
451 * keyboards if possible.
452 * NOTE: because of the way the low-level conole is initialized, this routine
453 * may be called more than once!!
455 Static int
456 ukbd_configure(int flags)
458 return 0;
460 #if 0 /* not yet */
461 keyboard_t *kbd;
462 device_t device;
463 struct usb_attach_arg *uaa;
464 void *arg[2];
466 device = devclass_get_device(ukbd_devclass, UKBD_DEFAULT);
467 if (device == NULL)
468 return 0;
469 uaa = (struct usb_attach_arg *)device_get_ivars(device);
470 if (uaa == NULL)
471 return 0;
473 /* probe the default keyboard */
474 arg[0] = (void *)uaa;
475 arg[1] = (void *)ukbd_intr;
476 kbd = NULL;
477 if (ukbd_probe(UKBD_DEFAULT, arg, flags))
478 return 0;
479 if (ukbd_init(UKBD_DEFAULT, &kbd, arg, flags))
480 return 0;
482 /* return the number of found keyboards */
483 return 1;
484 #endif
487 /* low-level functions */
489 /* detect a keyboard */
490 Static int
491 ukbd_probe(int unit, void *arg, int flags)
493 void **data;
494 struct usb_attach_arg *uaa;
496 data = (void **)arg;
497 uaa = (struct usb_attach_arg *)data[0];
499 if (unit == UKBD_DEFAULT) {
500 if (KBD_IS_PROBED(&default_kbd))
501 return 0;
503 if (probe_keyboard(uaa, flags))
504 return ENXIO;
505 return 0;
509 * Reset and initialize the device. Note that unit 0 (UKBD_DEFAULT) is an
510 * always-connected device once it has been initially detected. We do not
511 * deregister it if the usb keyboard is unplugged to avoid losing the
512 * connection to the console. This feature also handles the USB bus reset
513 * which detaches and reattaches USB devices during boot.
515 Static int
516 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
518 keyboard_t *kbd;
519 ukbd_state_t *state;
520 keymap_t *keymap;
521 accentmap_t *accmap;
522 fkeytab_t *fkeymap;
523 int fkeymap_size;
524 void **data = (void **)arg;
525 struct usb_attach_arg *uaa = (struct usb_attach_arg *)data[0];
527 if (unit == UKBD_DEFAULT) {
528 *kbdp = kbd = &default_kbd;
529 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
530 return 0;
531 state = &default_kbd_state;
532 keymap = &default_keymap;
533 accmap = &default_accentmap;
534 fkeymap = default_fkeytab;
535 fkeymap_size =
536 sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
537 } else if (*kbdp == NULL) {
538 *kbdp = kbd = kmalloc(sizeof(*kbd), M_DEVBUF, M_INTWAIT | M_ZERO);
539 state = kmalloc(sizeof(*state), M_DEVBUF, M_INTWAIT);
540 keymap = kmalloc(sizeof(key_map), M_DEVBUF, M_INTWAIT);
541 accmap = kmalloc(sizeof(accent_map), M_DEVBUF, M_INTWAIT);
542 fkeymap = kmalloc(sizeof(fkey_tab), M_DEVBUF, M_INTWAIT);
543 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
544 if ((state == NULL) || (keymap == NULL) || (accmap == NULL)
545 || (fkeymap == NULL)) {
546 if (state != NULL)
547 kfree(state, M_DEVBUF);
548 if (keymap != NULL)
549 kfree(keymap, M_DEVBUF);
550 if (accmap != NULL)
551 kfree(accmap, M_DEVBUF);
552 if (fkeymap != NULL)
553 kfree(fkeymap, M_DEVBUF);
554 kfree(kbd, M_DEVBUF);
555 return ENOMEM;
557 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
558 return 0;
559 } else {
560 kbd = *kbdp;
561 state = (ukbd_state_t *)kbd->kb_data;
562 keymap = kbd->kb_keymap;
563 accmap = kbd->kb_accentmap;
564 fkeymap = kbd->kb_fkeytab;
565 fkeymap_size = kbd->kb_fkeytab_size;
568 if (!KBD_IS_PROBED(kbd)) {
569 if (KBD_IS_CONFIGURED(kbd)) {
570 kbd_reinit_struct(kbd, flags, KB_PRI_USB);
571 } else {
572 kbd_init_struct(kbd, DRIVER_NAME, KB_OTHER,
573 unit, flags, KB_PRI_USB,
574 0, 0);
576 bzero(state, sizeof(*state));
577 bcopy(&key_map, keymap, sizeof(key_map));
578 bcopy(&accent_map, accmap, sizeof(accent_map));
579 bcopy(fkey_tab, fkeymap,
580 imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
581 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
582 kbd->kb_data = (void *)state;
584 if (probe_keyboard(uaa, flags))
585 return ENXIO;
586 else
587 KBD_FOUND_DEVICE(kbd);
588 ukbd_clear_state(kbd);
591 * If reattatching to an already open keyboard (e.g. console),
592 * try to restore the translation mode. Otherwise set the
593 * translation mode to, well, translation mode so we don't
594 * get garbage.
596 if (!KBD_IS_CONFIGURED(kbd)) {
597 state->ks_mode = K_XLATE;
598 kbd->kb_savemode = state->ks_mode;
599 } else {
600 state->ks_mode = kbd->kb_savemode;
602 state->ks_iface = uaa->iface;
603 state->ks_uaa = uaa;
604 state->ks_ifstate = 0;
605 callout_init(&state->ks_timeout);
607 * FIXME: set the initial value for lock keys in ks_state
608 * according to the BIOS data?
610 KBD_PROBE_DONE(kbd);
612 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
613 if (KBD_HAS_DEVICE(kbd)
614 && init_keyboard((ukbd_state_t *)kbd->kb_data,
615 &kbd->kb_type, kbd->kb_flags))
616 return ENXIO;
617 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&(state->ks_state));
619 if (!KBD_IS_CONFIGURED(kbd)) {
620 if (kbd_register(kbd) < 0)
621 return ENXIO;
622 KBD_CONFIG_DONE(kbd);
624 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
625 if (ukbd_enable_intr(kbd, TRUE, (usbd_intr_t *)data[1]) == 0)
626 ukbd_timeout((void *)kbd);
627 KBD_INIT_DONE(kbd);
629 return 0;
632 Static int
633 ukbd_enable_intr(keyboard_t *kbd, int on, usbd_intr_t *func)
635 ukbd_state_t *state = (ukbd_state_t *)kbd->kb_data;
636 usbd_status err;
638 if (on) {
639 /* Set up interrupt pipe. */
640 if (state->ks_ifstate & INTRENABLED)
641 return EBUSY;
643 state->ks_ifstate |= INTRENABLED;
644 err = usbd_open_pipe_intr(state->ks_iface, state->ks_ep_addr,
645 USBD_SHORT_XFER_OK,
646 &state->ks_intrpipe, kbd,
647 &state->ks_ndata,
648 sizeof(state->ks_ndata), func,
649 USBD_DEFAULT_INTERVAL);
650 if (err)
651 return (EIO);
652 } else {
653 /* Disable interrupts. */
654 usbd_abort_pipe(state->ks_intrpipe);
655 usbd_close_pipe(state->ks_intrpipe);
657 state->ks_ifstate &= ~INTRENABLED;
660 return (0);
663 /* finish using this keyboard */
664 Static int
665 ukbd_term(keyboard_t *kbd)
667 ukbd_state_t *state;
668 int error;
670 crit_enter();
671 state = (ukbd_state_t *)kbd->kb_data;
672 DPRINTF(("ukbd_term: ks_ifstate=0x%x\n", state->ks_ifstate));
674 callout_stop(&state->ks_timeout);
676 if (state->ks_ifstate & INTRENABLED)
677 ukbd_enable_intr(kbd, FALSE, NULL);
678 if (state->ks_ifstate & INTRENABLED) {
679 crit_exit();
680 DPRINTF(("ukbd_term: INTRENABLED!\n"));
681 return ENXIO;
684 error = kbd_unregister(kbd);
685 DPRINTF(("ukbd_term: kbd_unregister() %d\n", error));
686 if (error == 0) {
687 kbd->kb_flags = 0;
688 if (kbd != &default_kbd) {
689 kfree(kbd->kb_keymap, M_DEVBUF);
690 kfree(kbd->kb_accentmap, M_DEVBUF);
691 kfree(kbd->kb_fkeytab, M_DEVBUF);
692 kfree(state, M_DEVBUF);
693 kfree(kbd, M_DEVBUF);
696 crit_exit();
697 return error;
701 * Finish using the default keyboard. Shutdown the USB side of the keyboard
702 * but do not unregister it.
704 Static int
705 ukbd_default_term(keyboard_t *kbd)
707 ukbd_state_t *state;
709 crit_enter();
711 state = (ukbd_state_t *)kbd->kb_data;
712 DPRINTF(("ukbd_default_term: ks_ifstate=0x%x\n", state->ks_ifstate));
714 callout_stop(&state->ks_timeout);
716 if (state->ks_ifstate & INTRENABLED)
717 ukbd_enable_intr(kbd, FALSE, NULL);
718 if (state->ks_ifstate & INTRENABLED) {
719 crit_exit();
720 DPRINTF(("ukbd_term: INTRENABLED!\n"));
721 return ENXIO;
723 KBD_LOST_DEVICE(kbd);
724 KBD_LOST_PROBE(kbd);
725 KBD_LOST_INIT(kbd);
726 crit_exit();
727 return (0);
730 /* keyboard interrupt routine */
732 Static void
733 ukbd_timeout(void *arg)
735 keyboard_t *kbd;
736 ukbd_state_t *state;
738 kbd = (keyboard_t *)arg;
739 state = (ukbd_state_t *)kbd->kb_data;
740 crit_enter();
741 (*kbdsw[kbd->kb_index]->intr)(kbd, (void *)USBD_NORMAL_COMPLETION);
742 callout_reset(&state->ks_timeout, hz / 40, ukbd_timeout, arg);
743 crit_exit();
746 Static int
747 ukbd_interrupt(keyboard_t *kbd, void *arg)
749 usbd_status status = (usbd_status)arg;
750 ukbd_state_t *state;
751 struct ukbd_data *ud;
752 struct timeval tv;
753 u_long now;
754 int mod, omod;
755 int key, c;
756 int i, j;
758 DPRINTFN(5, ("ukbd_intr: status=%d\n", status));
759 if (status == USBD_CANCELLED)
760 return 0;
762 state = (ukbd_state_t *)kbd->kb_data;
763 ud = &state->ks_ndata;
765 if (status != USBD_NORMAL_COMPLETION) {
766 DPRINTF(("ukbd_intr: status=%d\n", status));
767 if (status == USBD_STALLED)
768 usbd_clear_endpoint_stall_async(state->ks_intrpipe);
769 return 0;
772 if (ud->keycode[0] == KEY_ERROR)
773 return 0; /* ignore */
775 getmicrouptime(&tv);
776 now = (u_long)tv.tv_sec*1000 + (u_long)tv.tv_usec/1000;
778 #define ADDKEY1(c) \
779 if (state->ks_inputs < INPUTBUFSIZE) { \
780 state->ks_input[state->ks_inputtail] = (c); \
781 ++state->ks_inputs; \
782 state->ks_inputtail = (state->ks_inputtail + 1)%INPUTBUFSIZE; \
785 mod = ud->modifiers;
786 omod = state->ks_odata.modifiers;
787 if (mod != omod) {
788 for (i = 0; i < NMOD; i++)
789 if (( mod & ukbd_mods[i].mask) !=
790 (omod & ukbd_mods[i].mask))
791 ADDKEY1(ukbd_mods[i].key |
792 (mod & ukbd_mods[i].mask
793 ? KEY_PRESS : KEY_RELEASE));
796 /* Check for released keys. */
797 for (i = 0; i < NKEYCODE; i++) {
798 key = state->ks_odata.keycode[i];
799 if (key == 0)
800 continue;
801 for (j = 0; j < NKEYCODE; j++) {
802 if (ud->keycode[j] == 0)
803 continue;
804 if (key == ud->keycode[j])
805 goto rfound;
807 ADDKEY1(key | KEY_RELEASE);
808 rfound:
812 /* Check for pressed keys. */
813 for (i = 0; i < NKEYCODE; i++) {
814 key = ud->keycode[i];
815 if (key == 0)
816 continue;
817 state->ks_ntime[i] = now + kbd->kb_delay1;
818 for (j = 0; j < NKEYCODE; j++) {
819 if (state->ks_odata.keycode[j] == 0)
820 continue;
821 if (key == state->ks_odata.keycode[j]) {
822 state->ks_ntime[i] = state->ks_otime[j];
823 if (state->ks_otime[j] > now)
824 goto pfound;
825 state->ks_ntime[i] = now + kbd->kb_delay2;
826 break;
829 ADDKEY1(key | KEY_PRESS);
830 pfound:
834 state->ks_odata = *ud;
835 bcopy(state->ks_ntime, state->ks_otime, sizeof(state->ks_ntime));
836 if (state->ks_inputs <= 0)
837 return 0;
839 #ifdef USB_DEBUG
840 for (i = state->ks_inputhead, j = 0; j < state->ks_inputs; ++j,
841 i = (i + 1)%INPUTBUFSIZE) {
842 c = state->ks_input[i];
843 DPRINTF(("0x%x (%d) %s\n", c, c,
844 (c & KEY_RELEASE) ? "released":"pressed"));
846 if (ud->modifiers)
847 DPRINTF(("mod:0x%04x ", ud->modifiers));
848 for (i = 0; i < NKEYCODE; i++) {
849 if (ud->keycode[i])
850 DPRINTF(("%d ", ud->keycode[i]));
852 DPRINTF(("\n"));
853 #endif /* USB_DEBUG */
855 if (state->ks_polling)
856 return 0;
858 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
859 /* let the callback function to process the input */
860 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
861 kbd->kb_callback.kc_arg);
862 } else {
863 /* read and discard the input; no one is waiting for it */
864 do {
865 c = ukbd_read_char(kbd, FALSE);
866 } while (c != NOKEY);
869 return 0;
872 Static int
873 ukbd_getc(ukbd_state_t *state)
875 int c;
877 if (state->ks_polling) {
878 DPRINTFN(1,("ukbd_getc: polling\n"));
879 crit_enter();
880 while (state->ks_inputs <= 0)
881 usbd_dopoll(state->ks_iface);
882 crit_exit();
884 crit_enter();
885 if (state->ks_inputs <= 0) {
886 c = -1;
887 } else {
888 c = state->ks_input[state->ks_inputhead];
889 --state->ks_inputs;
890 state->ks_inputhead = (state->ks_inputhead + 1)%INPUTBUFSIZE;
892 crit_exit();
893 return c;
896 /* test the interface to the device */
897 Static int
898 ukbd_test_if(keyboard_t *kbd)
900 return 0;
904 * Enable the access to the device; until this function is called,
905 * the client cannot read from the keyboard.
907 Static int
908 ukbd_enable(keyboard_t *kbd)
910 crit_enter();
911 KBD_ACTIVATE(kbd);
912 crit_exit();
913 return 0;
916 /* disallow the access to the device */
917 Static int
918 ukbd_disable(keyboard_t *kbd)
920 crit_enter();
921 KBD_DEACTIVATE(kbd);
922 crit_exit();
923 return 0;
926 /* read one byte from the keyboard if it's allowed */
927 Static int
928 ukbd_read(keyboard_t *kbd, int wait)
930 ukbd_state_t *state;
931 int usbcode;
932 #ifdef UKBD_EMULATE_ATSCANCODE
933 int keycode;
934 int scancode;
935 #endif
937 state = (ukbd_state_t *)kbd->kb_data;
938 #ifdef UKBD_EMULATE_ATSCANCODE
939 if (state->ks_buffered_char[0]) {
940 scancode = state->ks_buffered_char[0];
941 if (scancode & SCAN_PREFIX) {
942 state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX;
943 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
944 } else {
945 state->ks_buffered_char[0] = state->ks_buffered_char[1];
946 state->ks_buffered_char[1] = 0;
947 return scancode;
950 #endif /* UKBD_EMULATE_ATSCANCODE */
952 /* XXX */
953 usbcode = ukbd_getc(state);
954 if (!KBD_IS_ACTIVE(kbd) || !KBD_HAS_DEVICE(kbd) || (usbcode == -1))
955 return -1;
956 ++kbd->kb_count;
957 #ifdef UKBD_EMULATE_ATSCANCODE
958 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
959 if (keycode == NN)
960 return -1;
962 scancode = keycode2scancode(keycode, state->ks_ndata.modifiers,
963 usbcode & KEY_RELEASE);
964 if (scancode & SCAN_PREFIX) {
965 if (scancode & SCAN_PREFIX_CTL) {
966 state->ks_buffered_char[0] =
967 0x1d | (scancode & SCAN_RELEASE); /* Ctrl */
968 state->ks_buffered_char[1] = scancode & ~SCAN_PREFIX;
969 } else if (scancode & SCAN_PREFIX_SHIFT) {
970 state->ks_buffered_char[0] =
971 0x2a | (scancode & SCAN_RELEASE); /* Shift */
972 state->ks_buffered_char[1] =
973 scancode & ~SCAN_PREFIX_SHIFT;
974 } else {
975 state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX;
976 state->ks_buffered_char[1] = 0;
978 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
980 return scancode;
981 #else /* !UKBD_EMULATE_ATSCANCODE */
982 return usbcode;
983 #endif /* UKBD_EMULATE_ATSCANCODE */
986 /* check if data is waiting */
987 Static int
988 ukbd_check(keyboard_t *kbd)
990 if (!KBD_IS_ACTIVE(kbd) || !KBD_HAS_DEVICE(kbd))
991 return FALSE;
992 #ifdef UKBD_EMULATE_ATSCANCODE
993 if (((ukbd_state_t *)kbd->kb_data)->ks_buffered_char[0])
994 return TRUE;
995 #endif
996 if (((ukbd_state_t *)kbd->kb_data)->ks_inputs > 0)
997 return TRUE;
998 return FALSE;
1001 /* read char from the keyboard */
1002 Static u_int
1003 ukbd_read_char(keyboard_t *kbd, int wait)
1005 ukbd_state_t *state;
1006 u_int action;
1007 int usbcode;
1008 int keycode;
1009 #ifdef UKBD_EMULATE_ATSCANCODE
1010 int scancode;
1011 #endif
1013 state = (ukbd_state_t *)kbd->kb_data;
1014 next_code:
1015 /* do we have a composed char to return? */
1016 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
1017 action = state->ks_composed_char;
1018 state->ks_composed_char = 0;
1019 if (action > UCHAR_MAX)
1020 return ERRKEY;
1021 return action;
1024 #ifdef UKBD_EMULATE_ATSCANCODE
1025 /* do we have a pending raw scan code? */
1026 if (state->ks_mode == K_RAW) {
1027 if (state->ks_buffered_char[0]) {
1028 scancode = state->ks_buffered_char[0];
1029 if (scancode & SCAN_PREFIX) {
1030 state->ks_buffered_char[0] =
1031 scancode & ~SCAN_PREFIX;
1032 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1033 } else {
1034 state->ks_buffered_char[0] =
1035 state->ks_buffered_char[1];
1036 state->ks_buffered_char[1] = 0;
1037 return scancode;
1041 #endif /* UKBD_EMULATE_ATSCANCODE */
1043 /* see if there is something in the keyboard port */
1044 /* XXX */
1045 usbcode = ukbd_getc(state);
1046 if (usbcode == -1)
1047 return NOKEY;
1048 ++kbd->kb_count;
1050 #ifdef UKBD_EMULATE_ATSCANCODE
1051 /* USB key index -> key code -> AT scan code */
1052 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1053 if (keycode == NN)
1054 return NOKEY;
1056 /* return an AT scan code for the K_RAW mode */
1057 if (state->ks_mode == K_RAW) {
1058 scancode = keycode2scancode(keycode, state->ks_ndata.modifiers,
1059 usbcode & KEY_RELEASE);
1060 if (scancode & SCAN_PREFIX) {
1061 if (scancode & SCAN_PREFIX_CTL) {
1062 state->ks_buffered_char[0] =
1063 0x1d | (scancode & SCAN_RELEASE);
1064 state->ks_buffered_char[1] =
1065 scancode & ~SCAN_PREFIX;
1066 } else if (scancode & SCAN_PREFIX_SHIFT) {
1067 state->ks_buffered_char[0] =
1068 0x2a | (scancode & SCAN_RELEASE);
1069 state->ks_buffered_char[1] =
1070 scancode & ~SCAN_PREFIX_SHIFT;
1071 } else {
1072 state->ks_buffered_char[0] =
1073 scancode & ~SCAN_PREFIX;
1074 state->ks_buffered_char[1] = 0;
1076 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1078 return scancode;
1080 #else /* !UKBD_EMULATE_ATSCANCODE */
1081 /* return the byte as is for the K_RAW mode */
1082 if (state->ks_mode == K_RAW)
1083 return usbcode;
1085 /* USB key index -> key code */
1086 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1087 if (keycode == NN)
1088 return NOKEY;
1089 #endif /* UKBD_EMULATE_ATSCANCODE */
1091 switch (keycode) {
1092 case 0x38: /* left alt (compose key) */
1093 if (usbcode & KEY_RELEASE) {
1094 if (state->ks_flags & COMPOSE) {
1095 state->ks_flags &= ~COMPOSE;
1096 if (state->ks_composed_char > UCHAR_MAX)
1097 state->ks_composed_char = 0;
1099 } else {
1100 if (!(state->ks_flags & COMPOSE)) {
1101 state->ks_flags |= COMPOSE;
1102 state->ks_composed_char = 0;
1105 break;
1106 /* XXX: I don't like these... */
1107 case 0x5c: /* print screen */
1108 if (state->ks_flags & ALTS)
1109 keycode = 0x54; /* sysrq */
1110 break;
1111 case 0x68: /* pause/break */
1112 if (state->ks_flags & CTLS)
1113 keycode = 0x6c; /* break */
1114 break;
1117 /* return the key code in the K_CODE mode */
1118 if (usbcode & KEY_RELEASE)
1119 keycode |= SCAN_RELEASE;
1120 if (state->ks_mode == K_CODE)
1121 return keycode;
1123 /* compose a character code */
1124 if (state->ks_flags & COMPOSE) {
1125 switch (keycode) {
1126 /* key pressed, process it */
1127 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
1128 state->ks_composed_char *= 10;
1129 state->ks_composed_char += keycode - 0x40;
1130 if (state->ks_composed_char > UCHAR_MAX)
1131 return ERRKEY;
1132 goto next_code;
1133 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
1134 state->ks_composed_char *= 10;
1135 state->ks_composed_char += keycode - 0x47;
1136 if (state->ks_composed_char > UCHAR_MAX)
1137 return ERRKEY;
1138 goto next_code;
1139 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
1140 state->ks_composed_char *= 10;
1141 state->ks_composed_char += keycode - 0x4E;
1142 if (state->ks_composed_char > UCHAR_MAX)
1143 return ERRKEY;
1144 goto next_code;
1145 case 0x52: /* keypad 0 */
1146 state->ks_composed_char *= 10;
1147 if (state->ks_composed_char > UCHAR_MAX)
1148 return ERRKEY;
1149 goto next_code;
1151 /* key released, no interest here */
1152 case SCAN_RELEASE | 0x47:
1153 case SCAN_RELEASE | 0x48:
1154 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1155 case SCAN_RELEASE | 0x4B:
1156 case SCAN_RELEASE | 0x4C:
1157 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1158 case SCAN_RELEASE | 0x4F:
1159 case SCAN_RELEASE | 0x50:
1160 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1161 case SCAN_RELEASE | 0x52: /* keypad 0 */
1162 goto next_code;
1164 case 0x38: /* left alt key */
1165 break;
1167 default:
1168 if (state->ks_composed_char > 0) {
1169 state->ks_flags &= ~COMPOSE;
1170 state->ks_composed_char = 0;
1171 return ERRKEY;
1173 break;
1177 /* keycode to key action */
1178 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1179 keycode & SCAN_RELEASE, &state->ks_state,
1180 &state->ks_accents);
1181 if (action == NOKEY)
1182 goto next_code;
1183 else
1184 return action;
1187 /* check if char is waiting */
1188 Static int
1189 ukbd_check_char(keyboard_t *kbd)
1191 ukbd_state_t *state;
1193 if (!KBD_IS_ACTIVE(kbd) || !KBD_HAS_DEVICE(kbd))
1194 return FALSE;
1195 state = (ukbd_state_t *)kbd->kb_data;
1196 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
1197 return TRUE;
1198 return (ukbd_check(kbd));
1201 /* some useful control functions */
1202 Static int
1203 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1205 /* trasnlate LED_XXX bits into the device specific bits */
1206 static u_char ledmap[8] = {
1207 0, 2, 1, 3, 4, 6, 5, 7,
1209 ukbd_state_t *state = kbd->kb_data;
1210 int i;
1212 crit_enter();
1213 switch (cmd) {
1214 case KDGKBMODE: /* get keyboard mode */
1215 *(int *)arg = state->ks_mode;
1216 break;
1217 case KDSKBMODE: /* set keyboard mode */
1218 switch (*(int *)arg) {
1219 case K_XLATE:
1220 if (state->ks_mode != K_XLATE) {
1221 /* make lock key state and LED state match */
1222 state->ks_state &= ~LOCK_MASK;
1223 state->ks_state |= KBD_LED_VAL(kbd);
1225 /* FALLTHROUGH */
1226 case K_RAW:
1227 case K_CODE:
1228 if (state->ks_mode != *(int *)arg) {
1229 ukbd_clear_state(kbd);
1230 state->ks_mode = *(int *)arg;
1231 kbd->kb_savemode = state->ks_mode;
1233 break;
1234 default:
1235 crit_exit();
1236 return EINVAL;
1238 break;
1240 case KDGETLED: /* get keyboard LED */
1241 *(int *)arg = KBD_LED_VAL(kbd);
1242 break;
1243 case KDSETLED: /* set keyboard LED */
1244 /* NOTE: lock key state in ks_state won't be changed */
1245 if (*(int *)arg & ~LOCK_MASK) {
1246 crit_exit();
1247 return EINVAL;
1249 i = *(int *)arg;
1250 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1251 if (kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1252 if (i & ALKED)
1253 i |= CLKED;
1254 else
1255 i &= ~CLKED;
1257 if (KBD_HAS_DEVICE(kbd)) {
1258 set_leds(state, ledmap[i & LED_MASK]);
1259 /* XXX: error check? */
1261 KBD_LED_VAL(kbd) = *(int *)arg;
1262 break;
1264 case KDGKBSTATE: /* get lock key state */
1265 *(int *)arg = state->ks_state & LOCK_MASK;
1266 break;
1267 case KDSKBSTATE: /* set lock key state */
1268 if (*(int *)arg & ~LOCK_MASK) {
1269 crit_exit();
1270 return EINVAL;
1272 state->ks_state &= ~LOCK_MASK;
1273 state->ks_state |= *(int *)arg;
1274 crit_exit();
1275 /* set LEDs and quit */
1276 return ukbd_ioctl(kbd, KDSETLED, arg);
1278 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
1279 crit_exit();
1280 if (!KBD_HAS_DEVICE(kbd))
1281 return 0;
1282 if (((int *)arg)[1] < 0)
1283 return EINVAL;
1284 if (((int *)arg)[0] < 0)
1285 return EINVAL;
1286 else if (((int *)arg)[0] == 0) /* fastest possible value */
1287 kbd->kb_delay1 = 200;
1288 else
1289 kbd->kb_delay1 = ((int *)arg)[0];
1290 kbd->kb_delay2 = ((int *)arg)[1];
1291 return 0;
1293 case KDSETRAD: /* set keyboard repeat rate (old interface) */
1294 crit_exit();
1295 return set_typematic(kbd, *(int *)arg);
1297 case PIO_KEYMAP: /* set keyboard translation table */
1298 case PIO_KEYMAPENT: /* set keyboard translation table entry */
1299 case PIO_DEADKEYMAP: /* set accent key translation table */
1300 state->ks_accents = 0;
1301 /* FALLTHROUGH */
1302 default:
1303 crit_exit();
1304 return genkbd_commonioctl(kbd, cmd, arg);
1306 #ifdef USB_DEBUG
1307 case USB_SETDEBUG:
1308 ukbddebug = *(int *)arg;
1309 break;
1310 #endif
1313 crit_exit();
1314 return 0;
1317 /* lock the access to the keyboard */
1318 Static int
1319 ukbd_lock(keyboard_t *kbd, int lock)
1321 /* XXX ? */
1322 return TRUE;
1325 /* clear the internal state of the keyboard */
1326 Static void
1327 ukbd_clear_state(keyboard_t *kbd)
1329 ukbd_state_t *state;
1331 state = (ukbd_state_t *)kbd->kb_data;
1332 state->ks_flags = 0;
1333 state->ks_polling = 0;
1334 state->ks_state &= LOCK_MASK; /* preserve locking key state */
1335 state->ks_accents = 0;
1336 state->ks_composed_char = 0;
1337 #ifdef UKBD_EMULATE_ATSCANCODE
1338 state->ks_buffered_char[0] = 0;
1339 state->ks_buffered_char[1] = 0;
1340 #endif
1341 bzero(&state->ks_ndata, sizeof(state->ks_ndata));
1342 bzero(&state->ks_odata, sizeof(state->ks_odata));
1343 bzero(&state->ks_ntime, sizeof(state->ks_ntime));
1344 bzero(&state->ks_otime, sizeof(state->ks_otime));
1347 /* save the internal state */
1348 Static int
1349 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1351 if (len == 0)
1352 return sizeof(ukbd_state_t);
1353 if (len < sizeof(ukbd_state_t))
1354 return -1;
1355 bcopy(kbd->kb_data, buf, sizeof(ukbd_state_t));
1356 return 0;
1359 /* set the internal state */
1360 Static int
1361 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1363 if (len < sizeof(ukbd_state_t))
1364 return ENOMEM;
1365 bcopy(buf, kbd->kb_data, sizeof(ukbd_state_t));
1366 return 0;
1369 Static int
1370 ukbd_poll(keyboard_t *kbd, int on)
1372 ukbd_state_t *state;
1373 usbd_device_handle dev;
1375 state = (ukbd_state_t *)kbd->kb_data;
1376 usbd_interface2device_handle(state->ks_iface, &dev);
1378 crit_enter();
1379 if (on) {
1380 if (state->ks_polling == 0)
1381 usbd_set_polling(dev, on);
1382 ++state->ks_polling;
1383 } else {
1384 --state->ks_polling;
1385 if (state->ks_polling == 0)
1386 usbd_set_polling(dev, on);
1388 crit_exit();
1389 return 0;
1392 /* local functions */
1394 Static int
1395 probe_keyboard(struct usb_attach_arg *uaa, int flags)
1397 usb_interface_descriptor_t *id;
1399 if (!uaa->iface) /* we attach to ifaces only */
1400 return EINVAL;
1402 /* Check that this is a keyboard that speaks the boot protocol. */
1403 id = usbd_get_interface_descriptor(uaa->iface);
1404 if (id
1405 && id->bInterfaceClass == UICLASS_HID
1406 && id->bInterfaceSubClass == UISUBCLASS_BOOT
1407 && id->bInterfaceProtocol == UPROTO_BOOT_KEYBOARD)
1408 return 0; /* found it */
1410 return EINVAL;
1413 Static int
1414 init_keyboard(ukbd_state_t *state, int *type, int flags)
1416 usb_endpoint_descriptor_t *ed;
1417 usbd_status err;
1419 *type = KB_OTHER;
1421 state->ks_ifstate |= DISCONNECTED;
1423 ed = usbd_interface2endpoint_descriptor(state->ks_iface, 0);
1424 if (!ed) {
1425 kprintf("ukbd: could not read endpoint descriptor\n");
1426 return EIO;
1429 DPRINTFN(10,("ukbd:init_keyboard: \
1430 bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
1431 ed->bLength, ed->bDescriptorType,
1432 UE_GET_ADDR(ed->bEndpointAddress),
1433 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? "in":"out",
1434 UE_GET_XFERTYPE(ed->bmAttributes),
1435 UGETW(ed->wMaxPacketSize), ed->bInterval));
1437 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
1438 UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT) {
1439 kprintf("ukbd: unexpected endpoint\n");
1440 return EINVAL;
1443 if ((usbd_get_quirks(state->ks_uaa->device)->uq_flags & UQ_NO_SET_PROTO) == 0) {
1444 err = usbd_set_protocol(state->ks_iface, 0);
1445 DPRINTFN(5, ("ukbd:init_keyboard: protocol set\n"));
1446 if (err) {
1447 kprintf("ukbd: set protocol failed\n");
1448 return EIO;
1451 /* Ignore if SETIDLE fails since it is not crucial. */
1452 usbd_set_idle(state->ks_iface, 0, 0);
1454 state->ks_ep_addr = ed->bEndpointAddress;
1455 state->ks_ifstate &= ~DISCONNECTED;
1457 return 0;
1460 Static void
1461 set_leds(ukbd_state_t *state, int leds)
1463 u_int8_t res = leds;
1465 DPRINTF(("ukbd:set_leds: state=%p leds=%d\n", state, leds));
1467 usbd_set_report_async(state->ks_iface, UHID_OUTPUT_REPORT, 0, &res, 1);
1470 Static int
1471 set_typematic(keyboard_t *kbd, int code)
1473 static int delays[] = { 250, 500, 750, 1000 };
1474 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1475 68, 76, 84, 92, 100, 110, 118, 126,
1476 136, 152, 168, 184, 200, 220, 236, 252,
1477 272, 304, 336, 368, 400, 440, 472, 504 };
1479 if (code & ~0x7f)
1480 return EINVAL;
1481 kbd->kb_delay1 = delays[(code >> 5) & 3];
1482 kbd->kb_delay2 = rates[code & 0x1f];
1483 return 0;
1486 #ifdef UKBD_EMULATE_ATSCANCODE
1487 Static int
1488 keycode2scancode(int keycode, int shift, int up)
1490 static int scan[] = {
1491 0x1c, 0x1d, 0x35,
1492 0x37 | SCAN_PREFIX_SHIFT, /* PrintScreen */
1493 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f,
1494 0x50, 0x51, 0x52, 0x53,
1495 0x46, /* XXX Pause/Break */
1496 0x5b, 0x5c, 0x5d,
1498 int scancode;
1500 scancode = keycode;
1501 if ((keycode >= 89) && (keycode < 89 + sizeof(scan)/sizeof(scan[0])))
1502 scancode = scan[keycode - 89] | SCAN_PREFIX_E0;
1503 /* Pause/Break */
1504 if ((keycode == 104) && !(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))
1505 scancode = 0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL;
1506 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R))
1507 scancode &= ~SCAN_PREFIX_SHIFT;
1508 return (scancode | (up ? SCAN_RELEASE : SCAN_PRESS));
1510 #endif /* UKBD_EMULATE_ATSCANCODE */
1512 Static int
1513 ukbd_driver_load(module_t mod, int what, void *arg)
1515 switch (what) {
1516 case MOD_LOAD:
1517 kbd_add_driver(&ukbd_kbd_driver);
1518 break;
1519 case MOD_UNLOAD:
1520 kbd_delete_driver(&ukbd_kbd_driver);
1521 break;
1523 return usbd_driver_load(mod, what, 0);