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