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