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