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