ukbd.c revision 358213
1169689Skan#include <sys/cdefs.h>
2169689Skan__FBSDID("$FreeBSD: stable/11/sys/dev/usb/input/ukbd.c 358213 2020-02-21 08:41:44Z hselasky $");
3169689Skan
4169689Skan
5169689Skan/*-
6169689Skan * Copyright (c) 1998 The NetBSD Foundation, Inc.
7169689Skan * All rights reserved.
8169689Skan *
9169689Skan * This code is derived from software contributed to The NetBSD Foundation
10169689Skan * by Lennart Augustsson (lennart@augustsson.net) at
11169689Skan * Carlstedt Research & Technology.
12169689Skan *
13169689Skan * Redistribution and use in source and binary forms, with or without
14169689Skan * modification, are permitted provided that the following conditions
15169689Skan * are met:
16169689Skan * 1. Redistributions of source code must retain the above copyright
17169689Skan *    notice, this list of conditions and the following disclaimer.
18169689Skan * 2. Redistributions in binary form must reproduce the above copyright
19169689Skan *    notice, this list of conditions and the following disclaimer in the
20169689Skan *    documentation and/or other materials provided with the distribution.
21169689Skan *
22169689Skan * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23169689Skan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24169689Skan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25169689Skan * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26169689Skan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27169689Skan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28169689Skan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29169689Skan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30169689Skan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31169689Skan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32169689Skan * POSSIBILITY OF SUCH DAMAGE.
33169689Skan *
34169689Skan */
35169689Skan
36169689Skan/*
37169689Skan * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
38169689Skan */
39169689Skan
40169689Skan#include "opt_compat.h"
41169689Skan#include "opt_kbd.h"
42169689Skan#include "opt_ukbd.h"
43169689Skan#include "opt_evdev.h"
44169689Skan
45169689Skan#include <sys/stdint.h>
46169689Skan#include <sys/stddef.h>
47169689Skan#include <sys/param.h>
48169689Skan#include <sys/queue.h>
49169689Skan#include <sys/types.h>
50169689Skan#include <sys/systm.h>
51169689Skan#include <sys/kernel.h>
52169689Skan#include <sys/bus.h>
53169689Skan#include <sys/module.h>
54169689Skan#include <sys/lock.h>
55169689Skan#include <sys/mutex.h>
56169689Skan#include <sys/condvar.h>
57169689Skan#include <sys/sysctl.h>
58169689Skan#include <sys/sx.h>
59169689Skan#include <sys/unistd.h>
60169689Skan#include <sys/callout.h>
61169689Skan#include <sys/malloc.h>
62169689Skan#include <sys/priv.h>
63169689Skan#include <sys/proc.h>
64169689Skan#include <sys/sched.h>
65169689Skan#include <sys/kdb.h>
66169689Skan
67169689Skan#include <dev/usb/usb.h>
68169689Skan#include <dev/usb/usbdi.h>
69169689Skan#include <dev/usb/usbdi_util.h>
70169689Skan#include <dev/usb/usbhid.h>
71169689Skan
72169689Skan#define	USB_DEBUG_VAR ukbd_debug
73169689Skan#include <dev/usb/usb_debug.h>
74169689Skan
75169689Skan#include <dev/usb/quirk/usb_quirk.h>
76169689Skan
77169689Skan#ifdef EVDEV_SUPPORT
78169689Skan#include <dev/evdev/input.h>
79169689Skan#include <dev/evdev/evdev.h>
80169689Skan#endif
81169689Skan
82169689Skan#include <sys/ioccom.h>
83169689Skan#include <sys/filio.h>
84169689Skan#include <sys/kbio.h>
85169689Skan
86169689Skan#include <dev/kbd/kbdreg.h>
87169689Skan
88169689Skan/* the initial key map, accent map and fkey strings */
89169689Skan#if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
90169689Skan#define	KBD_DFLT_KEYMAP
91169689Skan#include "ukbdmap.h"
92169689Skan#endif
93169689Skan
94169689Skan/* the following file must be included after "ukbdmap.h" */
95169689Skan#include <dev/kbd/kbdtables.h>
96169689Skan
97169689Skan#ifdef USB_DEBUG
98169689Skanstatic int ukbd_debug = 0;
99169689Skanstatic int ukbd_no_leds = 0;
100169689Skanstatic int ukbd_pollrate = 0;
101169689Skan
102169689Skanstatic SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB keyboard");
103169689SkanSYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RWTUN,
104169689Skan    &ukbd_debug, 0, "Debug level");
105169689SkanSYSCTL_INT(_hw_usb_ukbd, OID_AUTO, no_leds, CTLFLAG_RWTUN,
106169689Skan    &ukbd_no_leds, 0, "Disables setting of keyboard leds");
107169689SkanSYSCTL_INT(_hw_usb_ukbd, OID_AUTO, pollrate, CTLFLAG_RWTUN,
108169689Skan    &ukbd_pollrate, 0, "Force this polling rate, 1-1000Hz");
109169689Skan#endif
110169689Skan
111169689Skan#define	UKBD_EMULATE_ATSCANCODE	       1
112169689Skan#define	UKBD_DRIVER_NAME          "ukbd"
113169689Skan#define	UKBD_NMOD                     8	/* units */
114169689Skan#define	UKBD_NKEYCODE                 6	/* units */
115169689Skan#define	UKBD_IN_BUF_SIZE  (2*(UKBD_NMOD + (2*UKBD_NKEYCODE)))	/* bytes */
116169689Skan#define	UKBD_IN_BUF_FULL  ((UKBD_IN_BUF_SIZE / 2) - 1)	/* bytes */
117169689Skan#define	UKBD_NFKEY        (sizeof(fkey_tab)/sizeof(fkey_tab[0]))	/* units */
118169689Skan#define	UKBD_BUFFER_SIZE	      64	/* bytes */
119169689Skan
120169689Skanstruct ukbd_data {
121169689Skan	uint16_t	modifiers;
122169689Skan#define	MOD_CONTROL_L	0x01
123169689Skan#define	MOD_CONTROL_R	0x10
124169689Skan#define	MOD_SHIFT_L	0x02
125169689Skan#define	MOD_SHIFT_R	0x20
126169689Skan#define	MOD_ALT_L	0x04
127169689Skan#define	MOD_ALT_R	0x40
128169689Skan#define	MOD_WIN_L	0x08
129169689Skan#define	MOD_WIN_R	0x80
130169689Skan/* internal */
131169689Skan#define	MOD_EJECT	0x0100
132169689Skan#define	MOD_FN		0x0200
133169689Skan	uint8_t	keycode[UKBD_NKEYCODE];
134169689Skan};
135169689Skan
136169689Skanenum {
137169689Skan	UKBD_INTR_DT_0,
138169689Skan	UKBD_INTR_DT_1,
139169689Skan	UKBD_CTRL_LED,
140169689Skan	UKBD_N_TRANSFER,
141169689Skan};
142169689Skan
143169689Skanstruct ukbd_softc {
144169689Skan	keyboard_t sc_kbd;
145169689Skan	keymap_t sc_keymap;
146169689Skan	accentmap_t sc_accmap;
147169689Skan	fkeytab_t sc_fkeymap[UKBD_NFKEY];
148169689Skan	struct hid_location sc_loc_apple_eject;
149169689Skan	struct hid_location sc_loc_apple_fn;
150169689Skan	struct hid_location sc_loc_ctrl_l;
151169689Skan	struct hid_location sc_loc_ctrl_r;
152169689Skan	struct hid_location sc_loc_shift_l;
153169689Skan	struct hid_location sc_loc_shift_r;
154169689Skan	struct hid_location sc_loc_alt_l;
155169689Skan	struct hid_location sc_loc_alt_r;
156169689Skan	struct hid_location sc_loc_win_l;
157169689Skan	struct hid_location sc_loc_win_r;
158169689Skan	struct hid_location sc_loc_events;
159169689Skan	struct hid_location sc_loc_numlock;
160169689Skan	struct hid_location sc_loc_capslock;
161169689Skan	struct hid_location sc_loc_scrolllock;
162169689Skan	struct usb_callout sc_callout;
163169689Skan	struct ukbd_data sc_ndata;
164169689Skan	struct ukbd_data sc_odata;
165169689Skan
166169689Skan	struct thread *sc_poll_thread;
167169689Skan	struct usb_device *sc_udev;
168169689Skan	struct usb_interface *sc_iface;
169169689Skan	struct usb_xfer *sc_xfer[UKBD_N_TRANSFER];
170169689Skan#ifdef EVDEV_SUPPORT
171169689Skan	struct evdev_dev *sc_evdev;
172169689Skan#endif
173169689Skan
174169689Skan	uint32_t sc_ntime[UKBD_NKEYCODE];
175169689Skan	uint32_t sc_otime[UKBD_NKEYCODE];
176169689Skan	uint32_t sc_input[UKBD_IN_BUF_SIZE];	/* input buffer */
177169689Skan	uint32_t sc_time_ms;
178169689Skan	uint32_t sc_composed_char;	/* composed char code, if non-zero */
179169689Skan#ifdef UKBD_EMULATE_ATSCANCODE
180169689Skan	uint32_t sc_buffered_char[2];
181169689Skan#endif
182169689Skan	uint32_t sc_flags;		/* flags */
183169689Skan#define	UKBD_FLAG_COMPOSE	0x00000001
184169689Skan#define	UKBD_FLAG_POLLING	0x00000002
185169689Skan#define	UKBD_FLAG_SET_LEDS	0x00000004
186169689Skan#define	UKBD_FLAG_ATTACHED	0x00000010
187169689Skan#define	UKBD_FLAG_GONE		0x00000020
188169689Skan
189169689Skan#define	UKBD_FLAG_HID_MASK	0x003fffc0
190169689Skan#define	UKBD_FLAG_APPLE_EJECT	0x00000040
191169689Skan#define	UKBD_FLAG_APPLE_FN	0x00000080
192169689Skan#define	UKBD_FLAG_APPLE_SWAP	0x00000100
193169689Skan#define	UKBD_FLAG_TIMER_RUNNING	0x00000200
194169689Skan#define	UKBD_FLAG_CTRL_L	0x00000400
195169689Skan#define	UKBD_FLAG_CTRL_R	0x00000800
196169689Skan#define	UKBD_FLAG_SHIFT_L	0x00001000
197169689Skan#define	UKBD_FLAG_SHIFT_R	0x00002000
198169689Skan#define	UKBD_FLAG_ALT_L		0x00004000
199169689Skan#define	UKBD_FLAG_ALT_R		0x00008000
200169689Skan#define	UKBD_FLAG_WIN_L		0x00010000
201169689Skan#define	UKBD_FLAG_WIN_R		0x00020000
202169689Skan#define	UKBD_FLAG_EVENTS	0x00040000
203169689Skan#define	UKBD_FLAG_NUMLOCK	0x00080000
204169689Skan#define	UKBD_FLAG_CAPSLOCK	0x00100000
205169689Skan#define	UKBD_FLAG_SCROLLLOCK 	0x00200000
206169689Skan
207169689Skan	int	sc_mode;		/* input mode (K_XLATE,K_RAW,K_CODE) */
208169689Skan	int	sc_state;		/* shift/lock key state */
209169689Skan	int	sc_accents;		/* accent key index (> 0) */
210169689Skan	int	sc_polling;		/* polling recursion count */
211169689Skan	int	sc_led_size;
212169689Skan	int	sc_kbd_size;
213169689Skan
214169689Skan	uint16_t sc_inputs;
215169689Skan	uint16_t sc_inputhead;
216169689Skan	uint16_t sc_inputtail;
217169689Skan	uint16_t sc_modifiers;
218169689Skan
219169689Skan	uint8_t	sc_leds;		/* store for async led requests */
220169689Skan	uint8_t	sc_iface_index;
221169689Skan	uint8_t	sc_iface_no;
222169689Skan	uint8_t sc_id_apple_eject;
223169689Skan	uint8_t sc_id_apple_fn;
224169689Skan	uint8_t sc_id_ctrl_l;
225169689Skan	uint8_t sc_id_ctrl_r;
226169689Skan	uint8_t sc_id_shift_l;
227169689Skan	uint8_t sc_id_shift_r;
228169689Skan	uint8_t sc_id_alt_l;
229169689Skan	uint8_t sc_id_alt_r;
230169689Skan	uint8_t sc_id_win_l;
231169689Skan	uint8_t sc_id_win_r;
232169689Skan	uint8_t sc_id_event;
233169689Skan	uint8_t sc_id_numlock;
234169689Skan	uint8_t sc_id_capslock;
235169689Skan	uint8_t sc_id_scrolllock;
236169689Skan	uint8_t sc_id_events;
237169689Skan	uint8_t sc_kbd_id;
238169689Skan
239169689Skan	uint8_t sc_buffer[UKBD_BUFFER_SIZE];
240169689Skan};
241169689Skan
242169689Skan#define	KEY_ERROR	  0x01
243169689Skan
244169689Skan#define	KEY_PRESS	  0
245169689Skan#define	KEY_RELEASE	  0x400
246169689Skan#define	KEY_INDEX(c)	  ((c) & 0xFF)
247169689Skan
248169689Skan#define	SCAN_PRESS	  0
249169689Skan#define	SCAN_RELEASE	  0x80
250169689Skan#define	SCAN_PREFIX_E0	  0x100
251169689Skan#define	SCAN_PREFIX_E1	  0x200
252169689Skan#define	SCAN_PREFIX_CTL	  0x400
253169689Skan#define	SCAN_PREFIX_SHIFT 0x800
254169689Skan#define	SCAN_PREFIX	(SCAN_PREFIX_E0  | SCAN_PREFIX_E1 | \
255169689Skan			 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT)
256169689Skan#define	SCAN_CHAR(c)	((c) & 0x7f)
257169689Skan
258169689Skan#define	UKBD_LOCK()	mtx_lock(&Giant)
259169689Skan#define	UKBD_UNLOCK()	mtx_unlock(&Giant)
260169689Skan
261169689Skan#ifdef	INVARIANTS
262169689Skan
263169689Skan/*
264169689Skan * Assert that the lock is held in all contexts
265169689Skan * where the code can be executed.
266169689Skan */
267169689Skan#define	UKBD_LOCK_ASSERT()	mtx_assert(&Giant, MA_OWNED)
268169689Skan
269169689Skan/*
270169689Skan * Assert that the lock is held in the contexts
271169689Skan * where it really has to be so.
272169689Skan */
273169689Skan#define	UKBD_CTX_LOCK_ASSERT()			 	\
274169689Skan	do {						\
275169689Skan		if (!kdb_active && panicstr == NULL)	\
276169689Skan			mtx_assert(&Giant, MA_OWNED);	\
277169689Skan	} while (0)
278169689Skan#else
279169689Skan
280169689Skan#define UKBD_LOCK_ASSERT()	(void)0
281169689Skan#define UKBD_CTX_LOCK_ASSERT()	(void)0
282169689Skan
283169689Skan#endif
284169689Skan
285169689Skanstruct ukbd_mods {
286169689Skan	uint32_t mask, key;
287169689Skan};
288169689Skan
289169689Skanstatic const struct ukbd_mods ukbd_mods[UKBD_NMOD] = {
290169689Skan	{MOD_CONTROL_L, 0xe0},
291169689Skan	{MOD_CONTROL_R, 0xe4},
292169689Skan	{MOD_SHIFT_L, 0xe1},
293169689Skan	{MOD_SHIFT_R, 0xe5},
294169689Skan	{MOD_ALT_L, 0xe2},
295169689Skan	{MOD_ALT_R, 0xe6},
296169689Skan	{MOD_WIN_L, 0xe3},
297169689Skan	{MOD_WIN_R, 0xe7},
298169689Skan};
299169689Skan
300169689Skan#define	NN 0				/* no translation */
301169689Skan/*
302169689Skan * Translate USB keycodes to AT keyboard scancodes.
303169689Skan */
304169689Skan/*
305169689Skan * FIXME: Mac USB keyboard generates:
306169689Skan * 0x53: keypad NumLock/Clear
307169689Skan * 0x66: Power
308169689Skan * 0x67: keypad =
309169689Skan * 0x68: F13
310169689Skan * 0x69: F14
311169689Skan * 0x6a: F15
312169689Skan *
313169689Skan * USB Apple Keyboard JIS generates:
314169689Skan * 0x90: Kana
315169689Skan * 0x91: Eisu
316169689Skan */
317169689Skanstatic const uint8_t ukbd_trtab[256] = {
318169689Skan	0, 0, 0, 0, 30, 48, 46, 32,	/* 00 - 07 */
319169689Skan	18, 33, 34, 35, 23, 36, 37, 38,	/* 08 - 0F */
320169689Skan	50, 49, 24, 25, 16, 19, 31, 20,	/* 10 - 17 */
321169689Skan	22, 47, 17, 45, 21, 44, 2, 3,	/* 18 - 1F */
322169689Skan	4, 5, 6, 7, 8, 9, 10, 11,	/* 20 - 27 */
323169689Skan	28, 1, 14, 15, 57, 12, 13, 26,	/* 28 - 2F */
324169689Skan	27, 43, 43, 39, 40, 41, 51, 52,	/* 30 - 37 */
325169689Skan	53, 58, 59, 60, 61, 62, 63, 64,	/* 38 - 3F */
326169689Skan	65, 66, 67, 68, 87, 88, 92, 70,	/* 40 - 47 */
327169689Skan	104, 102, 94, 96, 103, 99, 101, 98,	/* 48 - 4F */
328169689Skan	97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */
329169689Skan	89, 79, 80, 81, 75, 76, 77, 71,	/* 58 - 5F */
330169689Skan	72, 73, 82, 83, 86, 107, 122, NN,	/* 60 - 67 */
331169689Skan	NN, NN, NN, NN, NN, NN, NN, NN,	/* 68 - 6F */
332169689Skan	NN, NN, NN, NN, 115, 108, 111, 113,	/* 70 - 77 */
333169689Skan	109, 110, 112, 118, 114, 116, 117, 119,	/* 78 - 7F */
334169689Skan	121, 120, NN, NN, NN, NN, NN, 123,	/* 80 - 87 */
335169689Skan	124, 125, 126, 127, 128, NN, NN, NN,	/* 88 - 8F */
336169689Skan	129, 130, NN, NN, NN, NN, NN, NN,	/* 90 - 97 */
337169689Skan	NN, NN, NN, NN, NN, NN, NN, NN,	/* 98 - 9F */
338169689Skan	NN, NN, NN, NN, NN, NN, NN, NN,	/* A0 - A7 */
339169689Skan	NN, NN, NN, NN, NN, NN, NN, NN,	/* A8 - AF */
340169689Skan	NN, NN, NN, NN, NN, NN, NN, NN,	/* B0 - B7 */
341169689Skan	NN, NN, NN, NN, NN, NN, NN, NN,	/* B8 - BF */
342169689Skan	NN, NN, NN, NN, NN, NN, NN, NN,	/* C0 - C7 */
343169689Skan	NN, NN, NN, NN, NN, NN, NN, NN,	/* C8 - CF */
344169689Skan	NN, NN, NN, NN, NN, NN, NN, NN,	/* D0 - D7 */
345169689Skan	NN, NN, NN, NN, NN, NN, NN, NN,	/* D8 - DF */
346169689Skan	29, 42, 56, 105, 90, 54, 93, 106,	/* E0 - E7 */
347169689Skan	NN, NN, NN, NN, NN, NN, NN, NN,	/* E8 - EF */
348169689Skan	NN, NN, NN, NN, NN, NN, NN, NN,	/* F0 - F7 */
349169689Skan	NN, NN, NN, NN, NN, NN, NN, NN,	/* F8 - FF */
350169689Skan};
351169689Skan
352169689Skanstatic const uint8_t ukbd_boot_desc[] = {
353169689Skan	0x05, 0x01, 0x09, 0x06, 0xa1,
354169689Skan	0x01, 0x05, 0x07, 0x19, 0xe0,
355169689Skan	0x29, 0xe7, 0x15, 0x00, 0x25,
356169689Skan	0x01, 0x75, 0x01, 0x95, 0x08,
357169689Skan	0x81, 0x02, 0x95, 0x01, 0x75,
358169689Skan	0x08, 0x81, 0x01, 0x95, 0x03,
359169689Skan	0x75, 0x01, 0x05, 0x08, 0x19,
360169689Skan	0x01, 0x29, 0x03, 0x91, 0x02,
361169689Skan	0x95, 0x05, 0x75, 0x01, 0x91,
362169689Skan	0x01, 0x95, 0x06, 0x75, 0x08,
363169689Skan	0x15, 0x00, 0x26, 0xff, 0x00,
364169689Skan	0x05, 0x07, 0x19, 0x00, 0x2a,
365169689Skan	0xff, 0x00, 0x81, 0x00, 0xc0
366169689Skan};
367169689Skan
368169689Skan/* prototypes */
369169689Skanstatic void	ukbd_timeout(void *);
370169689Skanstatic void	ukbd_set_leds(struct ukbd_softc *, uint8_t);
371169689Skanstatic int	ukbd_set_typematic(keyboard_t *, int);
372169689Skan#ifdef UKBD_EMULATE_ATSCANCODE
373169689Skanstatic uint32_t	ukbd_atkeycode(int, int);
374169689Skanstatic int	ukbd_key2scan(struct ukbd_softc *, int, int, int);
375169689Skan#endif
376169689Skanstatic uint32_t	ukbd_read_char(keyboard_t *, int);
377169689Skanstatic void	ukbd_clear_state(keyboard_t *);
378169689Skanstatic int	ukbd_ioctl(keyboard_t *, u_long, caddr_t);
379169689Skanstatic int	ukbd_enable(keyboard_t *);
380169689Skanstatic int	ukbd_disable(keyboard_t *);
381169689Skanstatic void	ukbd_interrupt(struct ukbd_softc *);
382169689Skanstatic void	ukbd_event_keyinput(struct ukbd_softc *);
383169689Skan
384169689Skanstatic device_probe_t ukbd_probe;
385169689Skanstatic device_attach_t ukbd_attach;
386169689Skanstatic device_detach_t ukbd_detach;
387169689Skanstatic device_resume_t ukbd_resume;
388169689Skan
389169689Skan#ifdef EVDEV_SUPPORT
390169689Skanstatic const struct evdev_methods ukbd_evdev_methods = {
391169689Skan	.ev_event = evdev_ev_kbd_event,
392169689Skan};
393169689Skan#endif
394169689Skan
395169689Skanstatic uint8_t
396169689Skanukbd_any_key_pressed(struct ukbd_softc *sc)
397169689Skan{
398169689Skan	uint8_t i;
399169689Skan	uint8_t j;
400169689Skan
401169689Skan	for (j = i = 0; i < UKBD_NKEYCODE; i++)
402169689Skan		j |= sc->sc_odata.keycode[i];
403169689Skan
404169689Skan	return (j ? 1 : 0);
405169689Skan}
406169689Skan
407169689Skanstatic void
408169689Skanukbd_start_timer(struct ukbd_softc *sc)
409169689Skan{
410169689Skan	sc->sc_flags |= UKBD_FLAG_TIMER_RUNNING;
411169689Skan	usb_callout_reset(&sc->sc_callout, hz / 40, &ukbd_timeout, sc);
412169689Skan}
413169689Skan
414169689Skanstatic void
415169689Skanukbd_put_key(struct ukbd_softc *sc, uint32_t key)
416169689Skan{
417169689Skan
418169689Skan	UKBD_CTX_LOCK_ASSERT();
419169689Skan
420169689Skan	DPRINTF("0x%02x (%d) %s\n", key, key,
421169689Skan	    (key & KEY_RELEASE) ? "released" : "pressed");
422169689Skan
423169689Skan#ifdef EVDEV_SUPPORT
424169689Skan	if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && sc->sc_evdev != NULL) {
425169689Skan		evdev_push_event(sc->sc_evdev, EV_KEY,
426169689Skan		    evdev_hid2key(KEY_INDEX(key)), !(key & KEY_RELEASE));
427169689Skan		evdev_sync(sc->sc_evdev);
428169689Skan	}
429169689Skan#endif
430169689Skan
431169689Skan	if (sc->sc_inputs < UKBD_IN_BUF_SIZE) {
432169689Skan		sc->sc_input[sc->sc_inputtail] = key;
433169689Skan		++(sc->sc_inputs);
434169689Skan		++(sc->sc_inputtail);
435169689Skan		if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) {
436169689Skan			sc->sc_inputtail = 0;
437169689Skan		}
438169689Skan	} else {
439169689Skan		DPRINTF("input buffer is full\n");
440169689Skan	}
441169689Skan}
442169689Skan
443169689Skanstatic void
444169689Skanukbd_do_poll(struct ukbd_softc *sc, uint8_t wait)
445169689Skan{
446169689Skan
447169689Skan	UKBD_CTX_LOCK_ASSERT();
448169689Skan	KASSERT((sc->sc_flags & UKBD_FLAG_POLLING) != 0,
449169689Skan	    ("ukbd_do_poll called when not polling\n"));
450169689Skan	DPRINTFN(2, "polling\n");
451169689Skan
452169689Skan	if (!kdb_active && !SCHEDULER_STOPPED()) {
453169689Skan		/*
454169689Skan		 * In this context the kernel is polling for input,
455169689Skan		 * but the USB subsystem works in normal interrupt-driven
456169689Skan		 * mode, so we just wait on the USB threads to do the job.
457169689Skan		 * Note that we currently hold the Giant, but it's also used
458169689Skan		 * as the transfer mtx, so we must release it while waiting.
459169689Skan		 */
460169689Skan		while (sc->sc_inputs == 0) {
461169689Skan			/*
462169689Skan			 * Give USB threads a chance to run.  Note that
463169689Skan			 * kern_yield performs DROP_GIANT + PICKUP_GIANT.
464169689Skan			 */
465169689Skan			kern_yield(PRI_UNCHANGED);
466169689Skan			if (!wait)
467169689Skan				break;
468169689Skan		}
469169689Skan		return;
470169689Skan	}
471169689Skan
472169689Skan	while (sc->sc_inputs == 0) {
473169689Skan
474169689Skan		usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER);
475169689Skan
476169689Skan		/* Delay-optimised support for repetition of keys */
477169689Skan		if (ukbd_any_key_pressed(sc)) {
478169689Skan			/* a key is pressed - need timekeeping */
479169689Skan			DELAY(1000);
480169689Skan
481169689Skan			/* 1 millisecond has passed */
482169689Skan			sc->sc_time_ms += 1;
483169689Skan		}
484169689Skan
485169689Skan		ukbd_interrupt(sc);
486169689Skan
487169689Skan		if (!wait)
488169689Skan			break;
489169689Skan	}
490169689Skan}
491169689Skan
492169689Skanstatic int32_t
493169689Skanukbd_get_key(struct ukbd_softc *sc, uint8_t wait)
494169689Skan{
495169689Skan	int32_t c;
496169689Skan
497169689Skan	UKBD_CTX_LOCK_ASSERT();
498169689Skan	KASSERT((!kdb_active && !SCHEDULER_STOPPED())
499169689Skan	    || (sc->sc_flags & UKBD_FLAG_POLLING) != 0,
500169689Skan	    ("not polling in kdb or panic\n"));
501169689Skan
502169689Skan	if (sc->sc_inputs == 0 &&
503169689Skan	    (sc->sc_flags & UKBD_FLAG_GONE) == 0) {
504169689Skan		/* start transfer, if not already started */
505169689Skan		usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_0]);
506169689Skan		usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_1]);
507169689Skan	}
508169689Skan
509169689Skan	if (sc->sc_flags & UKBD_FLAG_POLLING)
510169689Skan		ukbd_do_poll(sc, wait);
511169689Skan
512	if (sc->sc_inputs == 0) {
513		c = -1;
514	} else {
515		c = sc->sc_input[sc->sc_inputhead];
516		--(sc->sc_inputs);
517		++(sc->sc_inputhead);
518		if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) {
519			sc->sc_inputhead = 0;
520		}
521	}
522	return (c);
523}
524
525static void
526ukbd_interrupt(struct ukbd_softc *sc)
527{
528	uint32_t n_mod;
529	uint32_t o_mod;
530	uint32_t now = sc->sc_time_ms;
531	uint32_t dtime;
532	uint8_t key;
533	uint8_t i;
534	uint8_t j;
535
536	UKBD_CTX_LOCK_ASSERT();
537
538	if (sc->sc_ndata.keycode[0] == KEY_ERROR)
539		return;
540
541	n_mod = sc->sc_ndata.modifiers;
542	o_mod = sc->sc_odata.modifiers;
543	if (n_mod != o_mod) {
544		for (i = 0; i < UKBD_NMOD; i++) {
545			if ((n_mod & ukbd_mods[i].mask) !=
546			    (o_mod & ukbd_mods[i].mask)) {
547				ukbd_put_key(sc, ukbd_mods[i].key |
548				    ((n_mod & ukbd_mods[i].mask) ?
549				    KEY_PRESS : KEY_RELEASE));
550			}
551		}
552	}
553	/* Check for released keys. */
554	for (i = 0; i < UKBD_NKEYCODE; i++) {
555		key = sc->sc_odata.keycode[i];
556		if (key == 0) {
557			continue;
558		}
559		for (j = 0; j < UKBD_NKEYCODE; j++) {
560			if (sc->sc_ndata.keycode[j] == 0) {
561				continue;
562			}
563			if (key == sc->sc_ndata.keycode[j]) {
564				goto rfound;
565			}
566		}
567		ukbd_put_key(sc, key | KEY_RELEASE);
568rfound:	;
569	}
570
571	/* Check for pressed keys. */
572	for (i = 0; i < UKBD_NKEYCODE; i++) {
573		key = sc->sc_ndata.keycode[i];
574		if (key == 0) {
575			continue;
576		}
577		sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1;
578		for (j = 0; j < UKBD_NKEYCODE; j++) {
579			if (sc->sc_odata.keycode[j] == 0) {
580				continue;
581			}
582			if (key == sc->sc_odata.keycode[j]) {
583
584				/* key is still pressed */
585
586				sc->sc_ntime[i] = sc->sc_otime[j];
587				dtime = (sc->sc_otime[j] - now);
588
589				if (!(dtime & 0x80000000)) {
590					/* time has not elapsed */
591					goto pfound;
592				}
593				sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2;
594				break;
595			}
596		}
597		ukbd_put_key(sc, key | KEY_PRESS);
598
599		/*
600                 * If any other key is presently down, force its repeat to be
601                 * well in the future (100s).  This makes the last key to be
602                 * pressed do the autorepeat.
603                 */
604		for (j = 0; j != UKBD_NKEYCODE; j++) {
605			if (j != i)
606				sc->sc_ntime[j] = now + (100 * 1000);
607		}
608pfound:	;
609	}
610
611	sc->sc_odata = sc->sc_ndata;
612
613	memcpy(sc->sc_otime, sc->sc_ntime, sizeof(sc->sc_otime));
614
615	ukbd_event_keyinput(sc);
616}
617
618static void
619ukbd_event_keyinput(struct ukbd_softc *sc)
620{
621	int c;
622
623	UKBD_CTX_LOCK_ASSERT();
624
625	if ((sc->sc_flags & UKBD_FLAG_POLLING) != 0)
626		return;
627
628	if (sc->sc_inputs == 0)
629		return;
630
631	if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
632	    KBD_IS_BUSY(&sc->sc_kbd)) {
633		/* let the callback function process the input */
634		(sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
635		    sc->sc_kbd.kb_callback.kc_arg);
636	} else {
637		/* read and discard the input, no one is waiting for it */
638		do {
639			c = ukbd_read_char(&sc->sc_kbd, 0);
640		} while (c != NOKEY);
641	}
642}
643
644static void
645ukbd_timeout(void *arg)
646{
647	struct ukbd_softc *sc = arg;
648
649	UKBD_LOCK_ASSERT();
650
651	sc->sc_time_ms += 25;	/* milliseconds */
652
653	ukbd_interrupt(sc);
654
655	/* Make sure any leftover key events gets read out */
656	ukbd_event_keyinput(sc);
657
658	if (ukbd_any_key_pressed(sc) || (sc->sc_inputs != 0)) {
659		ukbd_start_timer(sc);
660	} else {
661		sc->sc_flags &= ~UKBD_FLAG_TIMER_RUNNING;
662	}
663}
664
665static uint8_t
666ukbd_apple_fn(uint8_t keycode) {
667	switch (keycode) {
668	case 0x28: return 0x49; /* RETURN -> INSERT */
669	case 0x2a: return 0x4c; /* BACKSPACE -> DEL */
670	case 0x50: return 0x4a; /* LEFT ARROW -> HOME */
671	case 0x4f: return 0x4d; /* RIGHT ARROW -> END */
672	case 0x52: return 0x4b; /* UP ARROW -> PGUP */
673	case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */
674	default: return keycode;
675	}
676}
677
678static uint8_t
679ukbd_apple_swap(uint8_t keycode) {
680	switch (keycode) {
681	case 0x35: return 0x64;
682	case 0x64: return 0x35;
683	default: return keycode;
684	}
685}
686
687static void
688ukbd_intr_callback(struct usb_xfer *xfer, usb_error_t error)
689{
690	struct ukbd_softc *sc = usbd_xfer_softc(xfer);
691	struct usb_page_cache *pc;
692	uint8_t i;
693	uint8_t offset;
694	uint8_t id;
695	int len;
696
697	UKBD_LOCK_ASSERT();
698
699	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
700	pc = usbd_xfer_get_frame(xfer, 0);
701
702	switch (USB_GET_STATE(xfer)) {
703	case USB_ST_TRANSFERRED:
704		DPRINTF("actlen=%d bytes\n", len);
705
706		if (len == 0) {
707			DPRINTF("zero length data\n");
708			goto tr_setup;
709		}
710
711		if (sc->sc_kbd_id != 0) {
712			/* check and remove HID ID byte */
713			usbd_copy_out(pc, 0, &id, 1);
714			offset = 1;
715			len--;
716			if (len == 0) {
717				DPRINTF("zero length data\n");
718				goto tr_setup;
719			}
720		} else {
721			offset = 0;
722			id = 0;
723		}
724
725		if (len > UKBD_BUFFER_SIZE)
726			len = UKBD_BUFFER_SIZE;
727
728		/* get data */
729		usbd_copy_out(pc, offset, sc->sc_buffer, len);
730
731		/* clear temporary storage */
732		memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
733
734		/* scan through HID data */
735		if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) &&
736		    (id == sc->sc_id_apple_eject)) {
737			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_eject))
738				sc->sc_modifiers |= MOD_EJECT;
739			else
740				sc->sc_modifiers &= ~MOD_EJECT;
741		}
742		if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) &&
743		    (id == sc->sc_id_apple_fn)) {
744			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_fn))
745				sc->sc_modifiers |= MOD_FN;
746			else
747				sc->sc_modifiers &= ~MOD_FN;
748		}
749		if ((sc->sc_flags & UKBD_FLAG_CTRL_L) &&
750		    (id == sc->sc_id_ctrl_l)) {
751			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_ctrl_l))
752			  sc->	sc_modifiers |= MOD_CONTROL_L;
753			else
754			  sc->	sc_modifiers &= ~MOD_CONTROL_L;
755		}
756		if ((sc->sc_flags & UKBD_FLAG_CTRL_R) &&
757		    (id == sc->sc_id_ctrl_r)) {
758			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_ctrl_r))
759				sc->sc_modifiers |= MOD_CONTROL_R;
760			else
761				sc->sc_modifiers &= ~MOD_CONTROL_R;
762		}
763		if ((sc->sc_flags & UKBD_FLAG_SHIFT_L) &&
764		    (id == sc->sc_id_shift_l)) {
765			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_shift_l))
766				sc->sc_modifiers |= MOD_SHIFT_L;
767			else
768				sc->sc_modifiers &= ~MOD_SHIFT_L;
769		}
770		if ((sc->sc_flags & UKBD_FLAG_SHIFT_R) &&
771		    (id == sc->sc_id_shift_r)) {
772			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_shift_r))
773				sc->sc_modifiers |= MOD_SHIFT_R;
774			else
775				sc->sc_modifiers &= ~MOD_SHIFT_R;
776		}
777		if ((sc->sc_flags & UKBD_FLAG_ALT_L) &&
778		    (id == sc->sc_id_alt_l)) {
779			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_alt_l))
780				sc->sc_modifiers |= MOD_ALT_L;
781			else
782				sc->sc_modifiers &= ~MOD_ALT_L;
783		}
784		if ((sc->sc_flags & UKBD_FLAG_ALT_R) &&
785		    (id == sc->sc_id_alt_r)) {
786			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_alt_r))
787				sc->sc_modifiers |= MOD_ALT_R;
788			else
789				sc->sc_modifiers &= ~MOD_ALT_R;
790		}
791		if ((sc->sc_flags & UKBD_FLAG_WIN_L) &&
792		    (id == sc->sc_id_win_l)) {
793			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_win_l))
794				sc->sc_modifiers |= MOD_WIN_L;
795			else
796				sc->sc_modifiers &= ~MOD_WIN_L;
797		}
798		if ((sc->sc_flags & UKBD_FLAG_WIN_R) &&
799		    (id == sc->sc_id_win_r)) {
800			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_win_r))
801				sc->sc_modifiers |= MOD_WIN_R;
802			else
803				sc->sc_modifiers &= ~MOD_WIN_R;
804		}
805
806		sc->sc_ndata.modifiers = sc->sc_modifiers;
807
808		if ((sc->sc_flags & UKBD_FLAG_EVENTS) &&
809		    (id == sc->sc_id_events)) {
810			i = sc->sc_loc_events.count;
811			if (i > UKBD_NKEYCODE)
812				i = UKBD_NKEYCODE;
813			if (i > len)
814				i = len;
815			while (i--) {
816				sc->sc_ndata.keycode[i] =
817				    hid_get_data(sc->sc_buffer + i, len - i,
818				    &sc->sc_loc_events);
819			}
820		}
821
822#ifdef USB_DEBUG
823		DPRINTF("modifiers = 0x%04x\n", (int)sc->sc_modifiers);
824		for (i = 0; i < UKBD_NKEYCODE; i++) {
825			if (sc->sc_ndata.keycode[i]) {
826				DPRINTF("[%d] = 0x%02x\n",
827				    (int)i, (int)sc->sc_ndata.keycode[i]);
828			}
829		}
830#endif
831		if (sc->sc_modifiers & MOD_FN) {
832			for (i = 0; i < UKBD_NKEYCODE; i++) {
833				sc->sc_ndata.keycode[i] =
834				    ukbd_apple_fn(sc->sc_ndata.keycode[i]);
835			}
836		}
837
838		if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) {
839			for (i = 0; i < UKBD_NKEYCODE; i++) {
840				sc->sc_ndata.keycode[i] =
841				    ukbd_apple_swap(sc->sc_ndata.keycode[i]);
842			}
843		}
844
845		ukbd_interrupt(sc);
846
847		if (!(sc->sc_flags & UKBD_FLAG_TIMER_RUNNING)) {
848			if (ukbd_any_key_pressed(sc)) {
849				ukbd_start_timer(sc);
850			}
851		}
852
853	case USB_ST_SETUP:
854tr_setup:
855		if (sc->sc_inputs < UKBD_IN_BUF_FULL) {
856			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
857			usbd_transfer_submit(xfer);
858		} else {
859			DPRINTF("input queue is full!\n");
860		}
861		break;
862
863	default:			/* Error */
864		DPRINTF("error=%s\n", usbd_errstr(error));
865
866		if (error != USB_ERR_CANCELLED) {
867			/* try to clear stall first */
868			usbd_xfer_set_stall(xfer);
869			goto tr_setup;
870		}
871		break;
872	}
873}
874
875static void
876ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error)
877{
878	struct ukbd_softc *sc = usbd_xfer_softc(xfer);
879	struct usb_device_request req;
880	struct usb_page_cache *pc;
881	uint8_t id;
882	uint8_t any;
883	int len;
884
885	UKBD_LOCK_ASSERT();
886
887#ifdef USB_DEBUG
888	if (ukbd_no_leds)
889		return;
890#endif
891
892	switch (USB_GET_STATE(xfer)) {
893	case USB_ST_TRANSFERRED:
894	case USB_ST_SETUP:
895		if (!(sc->sc_flags & UKBD_FLAG_SET_LEDS))
896			break;
897		sc->sc_flags &= ~UKBD_FLAG_SET_LEDS;
898
899		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
900		req.bRequest = UR_SET_REPORT;
901		USETW2(req.wValue, UHID_OUTPUT_REPORT, 0);
902		req.wIndex[0] = sc->sc_iface_no;
903		req.wIndex[1] = 0;
904		req.wLength[1] = 0;
905
906		memset(sc->sc_buffer, 0, UKBD_BUFFER_SIZE);
907
908		id = 0;
909		any = 0;
910
911		/* Assumption: All led bits must be in the same ID. */
912
913		if (sc->sc_flags & UKBD_FLAG_NUMLOCK) {
914			if (sc->sc_leds & NLKED) {
915				hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
916				    &sc->sc_loc_numlock, 1);
917			}
918			id = sc->sc_id_numlock;
919			any = 1;
920		}
921
922		if (sc->sc_flags & UKBD_FLAG_SCROLLLOCK) {
923			if (sc->sc_leds & SLKED) {
924				hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
925				    &sc->sc_loc_scrolllock, 1);
926			}
927			id = sc->sc_id_scrolllock;
928			any = 1;
929		}
930
931		if (sc->sc_flags & UKBD_FLAG_CAPSLOCK) {
932			if (sc->sc_leds & CLKED) {
933				hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
934				    &sc->sc_loc_capslock, 1);
935			}
936			id = sc->sc_id_capslock;
937			any = 1;
938		}
939
940		/* if no leds, nothing to do */
941		if (!any)
942			break;
943
944#ifdef EVDEV_SUPPORT
945		if (sc->sc_evdev != NULL)
946			evdev_push_leds(sc->sc_evdev, sc->sc_leds);
947#endif
948
949		/* range check output report length */
950		len = sc->sc_led_size;
951		if (len > (UKBD_BUFFER_SIZE - 1))
952			len = (UKBD_BUFFER_SIZE - 1);
953
954		/* check if we need to prefix an ID byte */
955		sc->sc_buffer[0] = id;
956
957		pc = usbd_xfer_get_frame(xfer, 1);
958		if (id != 0) {
959			len++;
960			usbd_copy_in(pc, 0, sc->sc_buffer, len);
961		} else {
962			usbd_copy_in(pc, 0, sc->sc_buffer + 1, len);
963		}
964		req.wLength[0] = len;
965		usbd_xfer_set_frame_len(xfer, 1, len);
966
967		DPRINTF("len=%d, id=%d\n", len, id);
968
969		/* setup control request last */
970		pc = usbd_xfer_get_frame(xfer, 0);
971		usbd_copy_in(pc, 0, &req, sizeof(req));
972		usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
973
974		/* start data transfer */
975		usbd_xfer_set_frames(xfer, 2);
976		usbd_transfer_submit(xfer);
977		break;
978
979	default:			/* Error */
980		DPRINTFN(1, "error=%s\n", usbd_errstr(error));
981		break;
982	}
983}
984
985static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = {
986
987	[UKBD_INTR_DT_0] = {
988		.type = UE_INTERRUPT,
989		.endpoint = UE_ADDR_ANY,
990		.direction = UE_DIR_IN,
991		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
992		.bufsize = 0,	/* use wMaxPacketSize */
993		.callback = &ukbd_intr_callback,
994	},
995
996	[UKBD_INTR_DT_1] = {
997		.type = UE_INTERRUPT,
998		.endpoint = UE_ADDR_ANY,
999		.direction = UE_DIR_IN,
1000		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
1001		.bufsize = 0,	/* use wMaxPacketSize */
1002		.callback = &ukbd_intr_callback,
1003	},
1004
1005	[UKBD_CTRL_LED] = {
1006		.type = UE_CONTROL,
1007		.endpoint = 0x00,	/* Control pipe */
1008		.direction = UE_DIR_ANY,
1009		.bufsize = sizeof(struct usb_device_request) + UKBD_BUFFER_SIZE,
1010		.callback = &ukbd_set_leds_callback,
1011		.timeout = 1000,	/* 1 second */
1012	},
1013};
1014
1015/* A match on these entries will load ukbd */
1016static const STRUCT_USB_HOST_ID __used ukbd_devs[] = {
1017	{USB_IFACE_CLASS(UICLASS_HID),
1018	 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
1019	 USB_IFACE_PROTOCOL(UIPROTO_BOOT_KEYBOARD),},
1020};
1021
1022static int
1023ukbd_probe(device_t dev)
1024{
1025	keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
1026	struct usb_attach_arg *uaa = device_get_ivars(dev);
1027	void *d_ptr;
1028	int error;
1029	uint16_t d_len;
1030
1031	UKBD_LOCK_ASSERT();
1032	DPRINTFN(11, "\n");
1033
1034	if (sw == NULL) {
1035		return (ENXIO);
1036	}
1037	if (uaa->usb_mode != USB_MODE_HOST) {
1038		return (ENXIO);
1039	}
1040
1041	if (uaa->info.bInterfaceClass != UICLASS_HID)
1042		return (ENXIO);
1043
1044	if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
1045		return (ENXIO);
1046
1047	if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
1048	    (uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD))
1049		return (BUS_PROBE_DEFAULT);
1050
1051	error = usbd_req_get_hid_desc(uaa->device, NULL,
1052	    &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
1053
1054	if (error)
1055		return (ENXIO);
1056
1057	if (hid_is_keyboard(d_ptr, d_len)) {
1058		if (hid_is_mouse(d_ptr, d_len)) {
1059			/*
1060			 * NOTE: We currently don't support USB mouse
1061			 * and USB keyboard on the same USB endpoint.
1062			 * Let "ums" driver win.
1063			 */
1064			error = ENXIO;
1065		} else {
1066			error = BUS_PROBE_DEFAULT;
1067		}
1068	} else {
1069		error = ENXIO;
1070	}
1071	free(d_ptr, M_TEMP);
1072	return (error);
1073}
1074
1075static void
1076ukbd_parse_hid(struct ukbd_softc *sc, const uint8_t *ptr, uint32_t len)
1077{
1078	uint32_t flags;
1079
1080	/* reset detected bits */
1081	sc->sc_flags &= ~UKBD_FLAG_HID_MASK;
1082
1083	/* check if there is an ID byte */
1084	sc->sc_kbd_size = hid_report_size(ptr, len,
1085	    hid_input, &sc->sc_kbd_id);
1086
1087	/* investigate if this is an Apple Keyboard */
1088	if (hid_locate(ptr, len,
1089	    HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT),
1090	    hid_input, 0, &sc->sc_loc_apple_eject, &flags,
1091	    &sc->sc_id_apple_eject)) {
1092		if (flags & HIO_VARIABLE)
1093			sc->sc_flags |= UKBD_FLAG_APPLE_EJECT |
1094			    UKBD_FLAG_APPLE_SWAP;
1095		DPRINTFN(1, "Found Apple eject-key\n");
1096	}
1097	if (hid_locate(ptr, len,
1098	    HID_USAGE2(0xFFFF, 0x0003),
1099	    hid_input, 0, &sc->sc_loc_apple_fn, &flags,
1100	    &sc->sc_id_apple_fn)) {
1101		if (flags & HIO_VARIABLE)
1102			sc->sc_flags |= UKBD_FLAG_APPLE_FN;
1103		DPRINTFN(1, "Found Apple FN-key\n");
1104	}
1105	/* figure out some keys */
1106	if (hid_locate(ptr, len,
1107	    HID_USAGE2(HUP_KEYBOARD, 0xE0),
1108	    hid_input, 0, &sc->sc_loc_ctrl_l, &flags,
1109	    &sc->sc_id_ctrl_l)) {
1110		if (flags & HIO_VARIABLE)
1111			sc->sc_flags |= UKBD_FLAG_CTRL_L;
1112		DPRINTFN(1, "Found left control\n");
1113	}
1114	if (hid_locate(ptr, len,
1115	    HID_USAGE2(HUP_KEYBOARD, 0xE4),
1116	    hid_input, 0, &sc->sc_loc_ctrl_r, &flags,
1117	    &sc->sc_id_ctrl_r)) {
1118		if (flags & HIO_VARIABLE)
1119			sc->sc_flags |= UKBD_FLAG_CTRL_R;
1120		DPRINTFN(1, "Found right control\n");
1121	}
1122	if (hid_locate(ptr, len,
1123	    HID_USAGE2(HUP_KEYBOARD, 0xE1),
1124	    hid_input, 0, &sc->sc_loc_shift_l, &flags,
1125	    &sc->sc_id_shift_l)) {
1126		if (flags & HIO_VARIABLE)
1127			sc->sc_flags |= UKBD_FLAG_SHIFT_L;
1128		DPRINTFN(1, "Found left shift\n");
1129	}
1130	if (hid_locate(ptr, len,
1131	    HID_USAGE2(HUP_KEYBOARD, 0xE5),
1132	    hid_input, 0, &sc->sc_loc_shift_r, &flags,
1133	    &sc->sc_id_shift_r)) {
1134		if (flags & HIO_VARIABLE)
1135			sc->sc_flags |= UKBD_FLAG_SHIFT_R;
1136		DPRINTFN(1, "Found right shift\n");
1137	}
1138	if (hid_locate(ptr, len,
1139	    HID_USAGE2(HUP_KEYBOARD, 0xE2),
1140	    hid_input, 0, &sc->sc_loc_alt_l, &flags,
1141	    &sc->sc_id_alt_l)) {
1142		if (flags & HIO_VARIABLE)
1143			sc->sc_flags |= UKBD_FLAG_ALT_L;
1144		DPRINTFN(1, "Found left alt\n");
1145	}
1146	if (hid_locate(ptr, len,
1147	    HID_USAGE2(HUP_KEYBOARD, 0xE6),
1148	    hid_input, 0, &sc->sc_loc_alt_r, &flags,
1149	    &sc->sc_id_alt_r)) {
1150		if (flags & HIO_VARIABLE)
1151			sc->sc_flags |= UKBD_FLAG_ALT_R;
1152		DPRINTFN(1, "Found right alt\n");
1153	}
1154	if (hid_locate(ptr, len,
1155	    HID_USAGE2(HUP_KEYBOARD, 0xE3),
1156	    hid_input, 0, &sc->sc_loc_win_l, &flags,
1157	    &sc->sc_id_win_l)) {
1158		if (flags & HIO_VARIABLE)
1159			sc->sc_flags |= UKBD_FLAG_WIN_L;
1160		DPRINTFN(1, "Found left GUI\n");
1161	}
1162	if (hid_locate(ptr, len,
1163	    HID_USAGE2(HUP_KEYBOARD, 0xE7),
1164	    hid_input, 0, &sc->sc_loc_win_r, &flags,
1165	    &sc->sc_id_win_r)) {
1166		if (flags & HIO_VARIABLE)
1167			sc->sc_flags |= UKBD_FLAG_WIN_R;
1168		DPRINTFN(1, "Found right GUI\n");
1169	}
1170	/* figure out event buffer */
1171	if (hid_locate(ptr, len,
1172	    HID_USAGE2(HUP_KEYBOARD, 0x00),
1173	    hid_input, 0, &sc->sc_loc_events, &flags,
1174	    &sc->sc_id_events)) {
1175		if (flags & HIO_VARIABLE) {
1176			DPRINTFN(1, "Ignoring keyboard event control\n");
1177		} else {
1178			sc->sc_flags |= UKBD_FLAG_EVENTS;
1179			DPRINTFN(1, "Found keyboard event array\n");
1180		}
1181	}
1182
1183	/* figure out leds on keyboard */
1184	sc->sc_led_size = hid_report_size(ptr, len,
1185	    hid_output, NULL);
1186
1187	if (hid_locate(ptr, len,
1188	    HID_USAGE2(HUP_LEDS, 0x01),
1189	    hid_output, 0, &sc->sc_loc_numlock, &flags,
1190	    &sc->sc_id_numlock)) {
1191		if (flags & HIO_VARIABLE)
1192			sc->sc_flags |= UKBD_FLAG_NUMLOCK;
1193		DPRINTFN(1, "Found keyboard numlock\n");
1194	}
1195	if (hid_locate(ptr, len,
1196	    HID_USAGE2(HUP_LEDS, 0x02),
1197	    hid_output, 0, &sc->sc_loc_capslock, &flags,
1198	    &sc->sc_id_capslock)) {
1199		if (flags & HIO_VARIABLE)
1200			sc->sc_flags |= UKBD_FLAG_CAPSLOCK;
1201		DPRINTFN(1, "Found keyboard capslock\n");
1202	}
1203	if (hid_locate(ptr, len,
1204	    HID_USAGE2(HUP_LEDS, 0x03),
1205	    hid_output, 0, &sc->sc_loc_scrolllock, &flags,
1206	    &sc->sc_id_scrolllock)) {
1207		if (flags & HIO_VARIABLE)
1208			sc->sc_flags |= UKBD_FLAG_SCROLLLOCK;
1209		DPRINTFN(1, "Found keyboard scrolllock\n");
1210	}
1211}
1212
1213static int
1214ukbd_attach(device_t dev)
1215{
1216	struct ukbd_softc *sc = device_get_softc(dev);
1217	struct usb_attach_arg *uaa = device_get_ivars(dev);
1218	int unit = device_get_unit(dev);
1219	keyboard_t *kbd = &sc->sc_kbd;
1220	void *hid_ptr = NULL;
1221	usb_error_t err;
1222	uint16_t n;
1223	uint16_t hid_len;
1224#ifdef EVDEV_SUPPORT
1225	struct evdev_dev *evdev;
1226	int i;
1227#endif
1228#ifdef USB_DEBUG
1229	int rate;
1230#endif
1231	UKBD_LOCK_ASSERT();
1232
1233	kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
1234
1235	kbd->kb_data = (void *)sc;
1236
1237	device_set_usb_desc(dev);
1238
1239	sc->sc_udev = uaa->device;
1240	sc->sc_iface = uaa->iface;
1241	sc->sc_iface_index = uaa->info.bIfaceIndex;
1242	sc->sc_iface_no = uaa->info.bIfaceNum;
1243	sc->sc_mode = K_XLATE;
1244
1245	usb_callout_init_mtx(&sc->sc_callout, &Giant, 0);
1246
1247#ifdef UKBD_NO_POLLING
1248	err = usbd_transfer_setup(uaa->device,
1249	    &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
1250	    UKBD_N_TRANSFER, sc, &Giant);
1251#else
1252	/*
1253	 * Setup the UKBD USB transfers one by one, so they are memory
1254	 * independent which allows for handling panics triggered by
1255	 * the keyboard driver itself, typically via CTRL+ALT+ESC
1256	 * sequences. Or if the USB keyboard driver was processing a
1257	 * key at the moment of panic.
1258	 */
1259	for (n = 0; n != UKBD_N_TRANSFER; n++) {
1260		err = usbd_transfer_setup(uaa->device,
1261		    &uaa->info.bIfaceIndex, sc->sc_xfer + n, ukbd_config + n,
1262		    1, sc, &Giant);
1263		if (err)
1264			break;
1265	}
1266#endif
1267
1268	if (err) {
1269		DPRINTF("error=%s\n", usbd_errstr(err));
1270		goto detach;
1271	}
1272	/* setup default keyboard maps */
1273
1274	sc->sc_keymap = key_map;
1275	sc->sc_accmap = accent_map;
1276	for (n = 0; n < UKBD_NFKEY; n++) {
1277		sc->sc_fkeymap[n] = fkey_tab[n];
1278	}
1279
1280	kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
1281	    sc->sc_fkeymap, UKBD_NFKEY);
1282
1283	KBD_FOUND_DEVICE(kbd);
1284
1285	ukbd_clear_state(kbd);
1286
1287	/*
1288	 * FIXME: set the initial value for lock keys in "sc_state"
1289	 * according to the BIOS data?
1290	 */
1291	KBD_PROBE_DONE(kbd);
1292
1293	/* get HID descriptor */
1294	err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr,
1295	    &hid_len, M_TEMP, uaa->info.bIfaceIndex);
1296
1297	if (err == 0) {
1298		DPRINTF("Parsing HID descriptor of %d bytes\n",
1299		    (int)hid_len);
1300
1301		ukbd_parse_hid(sc, hid_ptr, hid_len);
1302
1303		free(hid_ptr, M_TEMP);
1304	}
1305
1306	/* check if we should use the boot protocol */
1307	if (usb_test_quirk(uaa, UQ_KBD_BOOTPROTO) ||
1308	    (err != 0) || (!(sc->sc_flags & UKBD_FLAG_EVENTS))) {
1309
1310		DPRINTF("Forcing boot protocol\n");
1311
1312		err = usbd_req_set_protocol(sc->sc_udev, NULL,
1313			sc->sc_iface_index, 0);
1314
1315		if (err != 0) {
1316			DPRINTF("Set protocol error=%s (ignored)\n",
1317			    usbd_errstr(err));
1318		}
1319
1320		ukbd_parse_hid(sc, ukbd_boot_desc, sizeof(ukbd_boot_desc));
1321	}
1322
1323	/* ignore if SETIDLE fails, hence it is not crucial */
1324	usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0);
1325
1326	ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
1327
1328	KBD_INIT_DONE(kbd);
1329
1330	if (kbd_register(kbd) < 0) {
1331		goto detach;
1332	}
1333	KBD_CONFIG_DONE(kbd);
1334
1335	ukbd_enable(kbd);
1336
1337#ifdef KBD_INSTALL_CDEV
1338	if (kbd_attach(kbd)) {
1339		goto detach;
1340	}
1341#endif
1342
1343#ifdef EVDEV_SUPPORT
1344	evdev = evdev_alloc();
1345	evdev_set_name(evdev, device_get_desc(dev));
1346	evdev_set_phys(evdev, device_get_nameunit(dev));
1347	evdev_set_id(evdev, BUS_USB, uaa->info.idVendor,
1348	   uaa->info.idProduct, 0);
1349	evdev_set_serial(evdev, usb_get_serial(uaa->device));
1350	evdev_set_methods(evdev, kbd, &ukbd_evdev_methods);
1351	evdev_support_event(evdev, EV_SYN);
1352	evdev_support_event(evdev, EV_KEY);
1353	if (sc->sc_flags & (UKBD_FLAG_NUMLOCK | UKBD_FLAG_CAPSLOCK |
1354			    UKBD_FLAG_SCROLLLOCK))
1355		evdev_support_event(evdev, EV_LED);
1356	evdev_support_event(evdev, EV_REP);
1357
1358	for (i = 0x00; i <= 0xFF; i++)
1359		evdev_support_key(evdev, evdev_hid2key(i));
1360	if (sc->sc_flags & UKBD_FLAG_NUMLOCK)
1361		evdev_support_led(evdev, LED_NUML);
1362	if (sc->sc_flags & UKBD_FLAG_CAPSLOCK)
1363		evdev_support_led(evdev, LED_CAPSL);
1364	if (sc->sc_flags & UKBD_FLAG_SCROLLLOCK)
1365		evdev_support_led(evdev, LED_SCROLLL);
1366
1367	if (evdev_register(evdev))
1368		evdev_free(evdev);
1369	else
1370		sc->sc_evdev = evdev;
1371#endif
1372
1373	sc->sc_flags |= UKBD_FLAG_ATTACHED;
1374
1375	if (bootverbose) {
1376		kbdd_diag(kbd, bootverbose);
1377	}
1378
1379#ifdef USB_DEBUG
1380	/* check for polling rate override */
1381	rate = ukbd_pollrate;
1382	if (rate > 0) {
1383		if (rate > 1000)
1384			rate = 1;
1385		else
1386			rate = 1000 / rate;
1387
1388		/* set new polling interval in ms */
1389		usbd_xfer_set_interval(sc->sc_xfer[UKBD_INTR_DT_0], rate);
1390		usbd_xfer_set_interval(sc->sc_xfer[UKBD_INTR_DT_1], rate);
1391	}
1392#endif
1393	/* start the keyboard */
1394	usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_0]);
1395	usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_1]);
1396
1397	return (0);			/* success */
1398
1399detach:
1400	ukbd_detach(dev);
1401	return (ENXIO);			/* error */
1402}
1403
1404static int
1405ukbd_detach(device_t dev)
1406{
1407	struct ukbd_softc *sc = device_get_softc(dev);
1408	int error;
1409
1410	UKBD_LOCK_ASSERT();
1411
1412	DPRINTF("\n");
1413
1414	sc->sc_flags |= UKBD_FLAG_GONE;
1415
1416	usb_callout_stop(&sc->sc_callout);
1417
1418	/* kill any stuck keys */
1419	if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
1420		/* stop receiving events from the USB keyboard */
1421		usbd_transfer_stop(sc->sc_xfer[UKBD_INTR_DT_0]);
1422		usbd_transfer_stop(sc->sc_xfer[UKBD_INTR_DT_1]);
1423
1424		/* release all leftover keys, if any */
1425		memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
1426
1427		/* process releasing of all keys */
1428		ukbd_interrupt(sc);
1429	}
1430
1431	ukbd_disable(&sc->sc_kbd);
1432
1433#ifdef KBD_INSTALL_CDEV
1434	if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
1435		error = kbd_detach(&sc->sc_kbd);
1436		if (error) {
1437			/* usb attach cannot return an error */
1438			device_printf(dev, "WARNING: kbd_detach() "
1439			    "returned non-zero! (ignored)\n");
1440		}
1441	}
1442#endif
1443
1444#ifdef EVDEV_SUPPORT
1445	evdev_free(sc->sc_evdev);
1446#endif
1447
1448	if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
1449		error = kbd_unregister(&sc->sc_kbd);
1450		if (error) {
1451			/* usb attach cannot return an error */
1452			device_printf(dev, "WARNING: kbd_unregister() "
1453			    "returned non-zero! (ignored)\n");
1454		}
1455	}
1456	sc->sc_kbd.kb_flags = 0;
1457
1458	usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
1459
1460	usb_callout_drain(&sc->sc_callout);
1461
1462	DPRINTF("%s: disconnected\n",
1463	    device_get_nameunit(dev));
1464
1465	return (0);
1466}
1467
1468static int
1469ukbd_resume(device_t dev)
1470{
1471	struct ukbd_softc *sc = device_get_softc(dev);
1472
1473	UKBD_LOCK_ASSERT();
1474
1475	ukbd_clear_state(&sc->sc_kbd);
1476
1477	return (0);
1478}
1479
1480/* early keyboard probe, not supported */
1481static int
1482ukbd_configure(int flags)
1483{
1484	return (0);
1485}
1486
1487/* detect a keyboard, not used */
1488static int
1489ukbd__probe(int unit, void *arg, int flags)
1490{
1491	return (ENXIO);
1492}
1493
1494/* reset and initialize the device, not used */
1495static int
1496ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
1497{
1498	return (ENXIO);
1499}
1500
1501/* test the interface to the device, not used */
1502static int
1503ukbd_test_if(keyboard_t *kbd)
1504{
1505	return (0);
1506}
1507
1508/* finish using this keyboard, not used */
1509static int
1510ukbd_term(keyboard_t *kbd)
1511{
1512	return (ENXIO);
1513}
1514
1515/* keyboard interrupt routine, not used */
1516static int
1517ukbd_intr(keyboard_t *kbd, void *arg)
1518{
1519	return (0);
1520}
1521
1522/* lock the access to the keyboard, not used */
1523static int
1524ukbd_lock(keyboard_t *kbd, int lock)
1525{
1526	return (1);
1527}
1528
1529/*
1530 * Enable the access to the device; until this function is called,
1531 * the client cannot read from the keyboard.
1532 */
1533static int
1534ukbd_enable(keyboard_t *kbd)
1535{
1536
1537	UKBD_LOCK();
1538	KBD_ACTIVATE(kbd);
1539	UKBD_UNLOCK();
1540
1541	return (0);
1542}
1543
1544/* disallow the access to the device */
1545static int
1546ukbd_disable(keyboard_t *kbd)
1547{
1548
1549	UKBD_LOCK();
1550	KBD_DEACTIVATE(kbd);
1551	UKBD_UNLOCK();
1552
1553	return (0);
1554}
1555
1556/* check if data is waiting */
1557/* Currently unused. */
1558static int
1559ukbd_check(keyboard_t *kbd)
1560{
1561	struct ukbd_softc *sc = kbd->kb_data;
1562
1563	UKBD_CTX_LOCK_ASSERT();
1564
1565	if (!KBD_IS_ACTIVE(kbd))
1566		return (0);
1567
1568	if (sc->sc_flags & UKBD_FLAG_POLLING)
1569		ukbd_do_poll(sc, 0);
1570
1571#ifdef UKBD_EMULATE_ATSCANCODE
1572	if (sc->sc_buffered_char[0]) {
1573		return (1);
1574	}
1575#endif
1576	if (sc->sc_inputs > 0) {
1577		return (1);
1578	}
1579	return (0);
1580}
1581
1582/* check if char is waiting */
1583static int
1584ukbd_check_char_locked(keyboard_t *kbd)
1585{
1586	struct ukbd_softc *sc = kbd->kb_data;
1587
1588	UKBD_CTX_LOCK_ASSERT();
1589
1590	if (!KBD_IS_ACTIVE(kbd))
1591		return (0);
1592
1593	if ((sc->sc_composed_char > 0) &&
1594	    (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1595		return (1);
1596	}
1597	return (ukbd_check(kbd));
1598}
1599
1600static int
1601ukbd_check_char(keyboard_t *kbd)
1602{
1603	int result;
1604
1605	UKBD_LOCK();
1606	result = ukbd_check_char_locked(kbd);
1607	UKBD_UNLOCK();
1608
1609	return (result);
1610}
1611
1612/* read one byte from the keyboard if it's allowed */
1613/* Currently unused. */
1614static int
1615ukbd_read(keyboard_t *kbd, int wait)
1616{
1617	struct ukbd_softc *sc = kbd->kb_data;
1618	int32_t usbcode;
1619#ifdef UKBD_EMULATE_ATSCANCODE
1620	uint32_t keycode;
1621	uint32_t scancode;
1622
1623#endif
1624
1625	UKBD_CTX_LOCK_ASSERT();
1626
1627	if (!KBD_IS_ACTIVE(kbd))
1628		return (-1);
1629
1630#ifdef UKBD_EMULATE_ATSCANCODE
1631	if (sc->sc_buffered_char[0]) {
1632		scancode = sc->sc_buffered_char[0];
1633		if (scancode & SCAN_PREFIX) {
1634			sc->sc_buffered_char[0] &= ~SCAN_PREFIX;
1635			return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1636		}
1637		sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1638		sc->sc_buffered_char[1] = 0;
1639		return (scancode);
1640	}
1641#endif					/* UKBD_EMULATE_ATSCANCODE */
1642
1643	/* XXX */
1644	usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1645	if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
1646		return (-1);
1647
1648	++(kbd->kb_count);
1649
1650#ifdef UKBD_EMULATE_ATSCANCODE
1651	keycode = ukbd_atkeycode(usbcode, sc->sc_ndata.modifiers);
1652	if (keycode == NN) {
1653		return -1;
1654	}
1655	return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1656	    (usbcode & KEY_RELEASE)));
1657#else					/* !UKBD_EMULATE_ATSCANCODE */
1658	return (usbcode);
1659#endif					/* UKBD_EMULATE_ATSCANCODE */
1660}
1661
1662/* read char from the keyboard */
1663static uint32_t
1664ukbd_read_char_locked(keyboard_t *kbd, int wait)
1665{
1666	struct ukbd_softc *sc = kbd->kb_data;
1667	uint32_t action;
1668	uint32_t keycode;
1669	int32_t usbcode;
1670#ifdef UKBD_EMULATE_ATSCANCODE
1671	uint32_t scancode;
1672#endif
1673
1674	UKBD_CTX_LOCK_ASSERT();
1675
1676	if (!KBD_IS_ACTIVE(kbd))
1677		return (NOKEY);
1678
1679next_code:
1680
1681	/* do we have a composed char to return ? */
1682
1683	if ((sc->sc_composed_char > 0) &&
1684	    (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1685
1686		action = sc->sc_composed_char;
1687		sc->sc_composed_char = 0;
1688
1689		if (action > 0xFF) {
1690			goto errkey;
1691		}
1692		goto done;
1693	}
1694#ifdef UKBD_EMULATE_ATSCANCODE
1695
1696	/* do we have a pending raw scan code? */
1697
1698	if (sc->sc_mode == K_RAW) {
1699		scancode = sc->sc_buffered_char[0];
1700		if (scancode) {
1701			if (scancode & SCAN_PREFIX) {
1702				sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
1703				return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1704			}
1705			sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1706			sc->sc_buffered_char[1] = 0;
1707			return (scancode);
1708		}
1709	}
1710#endif					/* UKBD_EMULATE_ATSCANCODE */
1711
1712	/* see if there is something in the keyboard port */
1713	/* XXX */
1714	usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1715	if (usbcode == -1) {
1716		return (NOKEY);
1717	}
1718	++kbd->kb_count;
1719
1720#ifdef UKBD_EMULATE_ATSCANCODE
1721	/* USB key index -> key code -> AT scan code */
1722	keycode = ukbd_atkeycode(usbcode, sc->sc_ndata.modifiers);
1723	if (keycode == NN) {
1724		return (NOKEY);
1725	}
1726	/* return an AT scan code for the K_RAW mode */
1727	if (sc->sc_mode == K_RAW) {
1728		return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1729		    (usbcode & KEY_RELEASE)));
1730	}
1731#else					/* !UKBD_EMULATE_ATSCANCODE */
1732
1733	/* return the byte as is for the K_RAW mode */
1734	if (sc->sc_mode == K_RAW) {
1735		return (usbcode);
1736	}
1737	/* USB key index -> key code */
1738	keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1739	if (keycode == NN) {
1740		return (NOKEY);
1741	}
1742#endif					/* UKBD_EMULATE_ATSCANCODE */
1743
1744	switch (keycode) {
1745	case 0x38:			/* left alt (compose key) */
1746		if (usbcode & KEY_RELEASE) {
1747			if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1748				sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1749
1750				if (sc->sc_composed_char > 0xFF) {
1751					sc->sc_composed_char = 0;
1752				}
1753			}
1754		} else {
1755			if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1756				sc->sc_flags |= UKBD_FLAG_COMPOSE;
1757				sc->sc_composed_char = 0;
1758			}
1759		}
1760		break;
1761	}
1762
1763	/* return the key code in the K_CODE mode */
1764	if (usbcode & KEY_RELEASE) {
1765		keycode |= SCAN_RELEASE;
1766	}
1767	if (sc->sc_mode == K_CODE) {
1768		return (keycode);
1769	}
1770	/* compose a character code */
1771	if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1772		switch (keycode) {
1773			/* key pressed, process it */
1774		case 0x47:
1775		case 0x48:
1776		case 0x49:		/* keypad 7,8,9 */
1777			sc->sc_composed_char *= 10;
1778			sc->sc_composed_char += keycode - 0x40;
1779			goto check_composed;
1780
1781		case 0x4B:
1782		case 0x4C:
1783		case 0x4D:		/* keypad 4,5,6 */
1784			sc->sc_composed_char *= 10;
1785			sc->sc_composed_char += keycode - 0x47;
1786			goto check_composed;
1787
1788		case 0x4F:
1789		case 0x50:
1790		case 0x51:		/* keypad 1,2,3 */
1791			sc->sc_composed_char *= 10;
1792			sc->sc_composed_char += keycode - 0x4E;
1793			goto check_composed;
1794
1795		case 0x52:		/* keypad 0 */
1796			sc->sc_composed_char *= 10;
1797			goto check_composed;
1798
1799			/* key released, no interest here */
1800		case SCAN_RELEASE | 0x47:
1801		case SCAN_RELEASE | 0x48:
1802		case SCAN_RELEASE | 0x49:	/* keypad 7,8,9 */
1803		case SCAN_RELEASE | 0x4B:
1804		case SCAN_RELEASE | 0x4C:
1805		case SCAN_RELEASE | 0x4D:	/* keypad 4,5,6 */
1806		case SCAN_RELEASE | 0x4F:
1807		case SCAN_RELEASE | 0x50:
1808		case SCAN_RELEASE | 0x51:	/* keypad 1,2,3 */
1809		case SCAN_RELEASE | 0x52:	/* keypad 0 */
1810			goto next_code;
1811
1812		case 0x38:		/* left alt key */
1813			break;
1814
1815		default:
1816			if (sc->sc_composed_char > 0) {
1817				sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1818				sc->sc_composed_char = 0;
1819				goto errkey;
1820			}
1821			break;
1822		}
1823	}
1824	/* keycode to key action */
1825	action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1826	    (keycode & SCAN_RELEASE),
1827	    &sc->sc_state, &sc->sc_accents);
1828	if (action == NOKEY) {
1829		goto next_code;
1830	}
1831done:
1832	return (action);
1833
1834check_composed:
1835	if (sc->sc_composed_char <= 0xFF) {
1836		goto next_code;
1837	}
1838errkey:
1839	return (ERRKEY);
1840}
1841
1842/* Currently wait is always false. */
1843static uint32_t
1844ukbd_read_char(keyboard_t *kbd, int wait)
1845{
1846	uint32_t keycode;
1847
1848	UKBD_LOCK();
1849	keycode = ukbd_read_char_locked(kbd, wait);
1850	UKBD_UNLOCK();
1851
1852	return (keycode);
1853}
1854
1855/* some useful control functions */
1856static int
1857ukbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
1858{
1859	struct ukbd_softc *sc = kbd->kb_data;
1860	int i;
1861#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1862    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1863	int ival;
1864
1865#endif
1866
1867	UKBD_LOCK_ASSERT();
1868
1869	switch (cmd) {
1870	case KDGKBMODE:		/* get keyboard mode */
1871		*(int *)arg = sc->sc_mode;
1872		break;
1873#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1874    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1875	case _IO('K', 7):
1876		ival = IOCPARM_IVAL(arg);
1877		arg = (caddr_t)&ival;
1878		/* FALLTHROUGH */
1879#endif
1880	case KDSKBMODE:		/* set keyboard mode */
1881		switch (*(int *)arg) {
1882		case K_XLATE:
1883			if (sc->sc_mode != K_XLATE) {
1884				/* make lock key state and LED state match */
1885				sc->sc_state &= ~LOCK_MASK;
1886				sc->sc_state |= KBD_LED_VAL(kbd);
1887			}
1888			/* FALLTHROUGH */
1889		case K_RAW:
1890		case K_CODE:
1891			if (sc->sc_mode != *(int *)arg) {
1892				if ((sc->sc_flags & UKBD_FLAG_POLLING) == 0)
1893					ukbd_clear_state(kbd);
1894				sc->sc_mode = *(int *)arg;
1895			}
1896			break;
1897		default:
1898			return (EINVAL);
1899		}
1900		break;
1901
1902	case KDGETLED:			/* get keyboard LED */
1903		*(int *)arg = KBD_LED_VAL(kbd);
1904		break;
1905#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1906    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1907	case _IO('K', 66):
1908		ival = IOCPARM_IVAL(arg);
1909		arg = (caddr_t)&ival;
1910		/* FALLTHROUGH */
1911#endif
1912	case KDSETLED:			/* set keyboard LED */
1913		/* NOTE: lock key state in "sc_state" won't be changed */
1914		if (*(int *)arg & ~LOCK_MASK)
1915			return (EINVAL);
1916
1917		i = *(int *)arg;
1918
1919		/* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1920		if (sc->sc_mode == K_XLATE &&
1921		    kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1922			if (i & ALKED)
1923				i |= CLKED;
1924			else
1925				i &= ~CLKED;
1926		}
1927		if (KBD_HAS_DEVICE(kbd))
1928			ukbd_set_leds(sc, i);
1929
1930		KBD_LED_VAL(kbd) = *(int *)arg;
1931		break;
1932	case KDGKBSTATE:		/* get lock key state */
1933		*(int *)arg = sc->sc_state & LOCK_MASK;
1934		break;
1935#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1936    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1937	case _IO('K', 20):
1938		ival = IOCPARM_IVAL(arg);
1939		arg = (caddr_t)&ival;
1940		/* FALLTHROUGH */
1941#endif
1942	case KDSKBSTATE:		/* set lock key state */
1943		if (*(int *)arg & ~LOCK_MASK) {
1944			return (EINVAL);
1945		}
1946		sc->sc_state &= ~LOCK_MASK;
1947		sc->sc_state |= *(int *)arg;
1948
1949		/* set LEDs and quit */
1950		return (ukbd_ioctl(kbd, KDSETLED, arg));
1951
1952	case KDSETREPEAT:		/* set keyboard repeat rate (new
1953					 * interface) */
1954		if (!KBD_HAS_DEVICE(kbd)) {
1955			return (0);
1956		}
1957		if (((int *)arg)[1] < 0) {
1958			return (EINVAL);
1959		}
1960		if (((int *)arg)[0] < 0) {
1961			return (EINVAL);
1962		}
1963		if (((int *)arg)[0] < 200)	/* fastest possible value */
1964			kbd->kb_delay1 = 200;
1965		else
1966			kbd->kb_delay1 = ((int *)arg)[0];
1967		kbd->kb_delay2 = ((int *)arg)[1];
1968#ifdef EVDEV_SUPPORT
1969		if (sc->sc_evdev != NULL)
1970			evdev_push_repeats(sc->sc_evdev, kbd);
1971#endif
1972		return (0);
1973
1974#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1975    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1976	case _IO('K', 67):
1977		ival = IOCPARM_IVAL(arg);
1978		arg = (caddr_t)&ival;
1979		/* FALLTHROUGH */
1980#endif
1981	case KDSETRAD:			/* set keyboard repeat rate (old
1982					 * interface) */
1983		return (ukbd_set_typematic(kbd, *(int *)arg));
1984
1985	case PIO_KEYMAP:		/* set keyboard translation table */
1986	case OPIO_KEYMAP:		/* set keyboard translation table
1987					 * (compat) */
1988	case PIO_KEYMAPENT:		/* set keyboard translation table
1989					 * entry */
1990	case PIO_DEADKEYMAP:		/* set accent key translation table */
1991		sc->sc_accents = 0;
1992		/* FALLTHROUGH */
1993	default:
1994		return (genkbd_commonioctl(kbd, cmd, arg));
1995	}
1996
1997	return (0);
1998}
1999
2000static int
2001ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
2002{
2003	int result;
2004
2005	/*
2006	 * XXX Check if someone is calling us from a critical section:
2007	 */
2008	if (curthread->td_critnest != 0)
2009		return (EDEADLK);
2010
2011	/*
2012	 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
2013	 * context where printf(9) can be called, which among other things
2014	 * includes interrupt filters and threads with any kinds of locks
2015	 * already held.  For this reason it would be dangerous to acquire
2016	 * the Giant here unconditionally.  On the other hand we have to
2017	 * have it to handle the ioctl.
2018	 * So we make our best effort to auto-detect whether we can grab
2019	 * the Giant or not.  Blame syscons(4) for this.
2020	 */
2021	switch (cmd) {
2022	case KDGKBSTATE:
2023	case KDSKBSTATE:
2024	case KDSETLED:
2025		if (!mtx_owned(&Giant) && !SCHEDULER_STOPPED())
2026			return (EDEADLK);	/* best I could come up with */
2027		/* FALLTHROUGH */
2028	default:
2029		UKBD_LOCK();
2030		result = ukbd_ioctl_locked(kbd, cmd, arg);
2031		UKBD_UNLOCK();
2032		return (result);
2033	}
2034}
2035
2036
2037/* clear the internal state of the keyboard */
2038static void
2039ukbd_clear_state(keyboard_t *kbd)
2040{
2041	struct ukbd_softc *sc = kbd->kb_data;
2042
2043	UKBD_CTX_LOCK_ASSERT();
2044
2045	sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
2046	sc->sc_state &= LOCK_MASK;	/* preserve locking key state */
2047	sc->sc_accents = 0;
2048	sc->sc_composed_char = 0;
2049#ifdef UKBD_EMULATE_ATSCANCODE
2050	sc->sc_buffered_char[0] = 0;
2051	sc->sc_buffered_char[1] = 0;
2052#endif
2053	memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
2054	memset(&sc->sc_odata, 0, sizeof(sc->sc_odata));
2055	memset(&sc->sc_ntime, 0, sizeof(sc->sc_ntime));
2056	memset(&sc->sc_otime, 0, sizeof(sc->sc_otime));
2057}
2058
2059/* save the internal state, not used */
2060static int
2061ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
2062{
2063	return (len == 0) ? 1 : -1;
2064}
2065
2066/* set the internal state, not used */
2067static int
2068ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
2069{
2070	return (EINVAL);
2071}
2072
2073static int
2074ukbd_poll(keyboard_t *kbd, int on)
2075{
2076	struct ukbd_softc *sc = kbd->kb_data;
2077
2078	UKBD_LOCK();
2079	/*
2080	 * Keep a reference count on polling to allow recursive
2081	 * cngrab() during a panic for example.
2082	 */
2083	if (on)
2084		sc->sc_polling++;
2085	else if (sc->sc_polling > 0)
2086		sc->sc_polling--;
2087
2088	if (sc->sc_polling != 0) {
2089		sc->sc_flags |= UKBD_FLAG_POLLING;
2090		sc->sc_poll_thread = curthread;
2091	} else {
2092		sc->sc_flags &= ~UKBD_FLAG_POLLING;
2093		ukbd_start_timer(sc);	/* start timer */
2094	}
2095	UKBD_UNLOCK();
2096
2097	return (0);
2098}
2099
2100/* local functions */
2101
2102static void
2103ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
2104{
2105
2106	UKBD_LOCK_ASSERT();
2107	DPRINTF("leds=0x%02x\n", leds);
2108
2109	sc->sc_leds = leds;
2110	sc->sc_flags |= UKBD_FLAG_SET_LEDS;
2111
2112	/* start transfer, if not already started */
2113
2114	usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
2115}
2116
2117static int
2118ukbd_set_typematic(keyboard_t *kbd, int code)
2119{
2120#ifdef EVDEV_SUPPORT
2121	struct ukbd_softc *sc = kbd->kb_data;
2122#endif
2123	static const int delays[] = {250, 500, 750, 1000};
2124	static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
2125		68, 76, 84, 92, 100, 110, 118, 126,
2126		136, 152, 168, 184, 200, 220, 236, 252,
2127	272, 304, 336, 368, 400, 440, 472, 504};
2128
2129	if (code & ~0x7f) {
2130		return (EINVAL);
2131	}
2132	kbd->kb_delay1 = delays[(code >> 5) & 3];
2133	kbd->kb_delay2 = rates[code & 0x1f];
2134#ifdef EVDEV_SUPPORT
2135	if (sc->sc_evdev != NULL)
2136		evdev_push_repeats(sc->sc_evdev, kbd);
2137#endif
2138	return (0);
2139}
2140
2141#ifdef UKBD_EMULATE_ATSCANCODE
2142static uint32_t
2143ukbd_atkeycode(int usbcode, int shift)
2144{
2145	uint32_t keycode;
2146
2147	keycode = ukbd_trtab[KEY_INDEX(usbcode)];
2148	/*
2149	 * Translate Alt-PrintScreen to SysRq.
2150	 *
2151	 * Some or all AT keyboards connected through USB have already
2152	 * mapped Alted PrintScreens to an unusual usbcode (0x8a).
2153	 * ukbd_trtab translates this to 0x7e, and key2scan() would
2154	 * translate that to 0x79 (Intl' 4).  Assume that if we have
2155	 * an Alted 0x7e here then it actually is an Alted PrintScreen.
2156	 *
2157	 * The usual usbcode for all PrintScreens is 0x46.  ukbd_trtab
2158	 * translates this to 0x5c, so the Alt check to classify 0x5c
2159	 * is routine.
2160	 */
2161	if ((keycode == 0x5c || keycode == 0x7e) &&
2162	    shift & (MOD_ALT_L | MOD_ALT_R))
2163		return (0x54);
2164	return (keycode);
2165}
2166
2167static int
2168ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
2169{
2170	static const int scan[] = {
2171		/* 89 */
2172		0x11c,	/* Enter */
2173		/* 90-99 */
2174		0x11d,	/* Ctrl-R */
2175		0x135,	/* Divide */
2176		0x137,	/* PrintScreen */
2177		0x138,	/* Alt-R */
2178		0x147,	/* Home */
2179		0x148,	/* Up */
2180		0x149,	/* PageUp */
2181		0x14b,	/* Left */
2182		0x14d,	/* Right */
2183		0x14f,	/* End */
2184		/* 100-109 */
2185		0x150,	/* Down */
2186		0x151,	/* PageDown */
2187		0x152,	/* Insert */
2188		0x153,	/* Delete */
2189		0x146,	/* Pause/Break */
2190		0x15b,	/* Win_L(Super_L) */
2191		0x15c,	/* Win_R(Super_R) */
2192		0x15d,	/* Application(Menu) */
2193
2194		/* SUN TYPE 6 USB KEYBOARD */
2195		0x168,	/* Sun Type 6 Help */
2196		0x15e,	/* Sun Type 6 Stop */
2197		/* 110 - 119 */
2198		0x15f,	/* Sun Type 6 Again */
2199		0x160,	/* Sun Type 6 Props */
2200		0x161,	/* Sun Type 6 Undo */
2201		0x162,	/* Sun Type 6 Front */
2202		0x163,	/* Sun Type 6 Copy */
2203		0x164,	/* Sun Type 6 Open */
2204		0x165,	/* Sun Type 6 Paste */
2205		0x166,	/* Sun Type 6 Find */
2206		0x167,	/* Sun Type 6 Cut */
2207		0x125,	/* Sun Type 6 Mute */
2208		/* 120 - 130 */
2209		0x11f,	/* Sun Type 6 VolumeDown */
2210		0x11e,	/* Sun Type 6 VolumeUp */
2211		0x120,	/* Sun Type 6 PowerDown */
2212
2213		/* Japanese 106/109 keyboard */
2214		0x73,	/* Keyboard Intl' 1 (backslash / underscore) */
2215		0x70,	/* Keyboard Intl' 2 (Katakana / Hiragana) */
2216		0x7d,	/* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */
2217		0x79,	/* Keyboard Intl' 4 (Henkan) */
2218		0x7b,	/* Keyboard Intl' 5 (Muhenkan) */
2219		0x5c,	/* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */
2220		0x71,   /* Apple Keyboard JIS (Kana) */
2221		0x72,   /* Apple Keyboard JIS (Eisu) */
2222	};
2223
2224	if ((code >= 89) && (code < (int)(89 + nitems(scan)))) {
2225		code = scan[code - 89];
2226	}
2227	/* PrintScreen */
2228	if (code == 0x137 && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R |
2229	    MOD_SHIFT_L | MOD_SHIFT_R)))) {
2230		code |= SCAN_PREFIX_SHIFT;
2231	}
2232	/* Pause/Break */
2233	if ((code == 0x146) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
2234		code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
2235	}
2236	code |= (up ? SCAN_RELEASE : SCAN_PRESS);
2237
2238	if (code & SCAN_PREFIX) {
2239		if (code & SCAN_PREFIX_CTL) {
2240			/* Ctrl */
2241			sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
2242			sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
2243		} else if (code & SCAN_PREFIX_SHIFT) {
2244			/* Shift */
2245			sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
2246			sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
2247		} else {
2248			sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
2249			sc->sc_buffered_char[1] = 0;
2250		}
2251		return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
2252	}
2253	return (code);
2254
2255}
2256
2257#endif					/* UKBD_EMULATE_ATSCANCODE */
2258
2259static keyboard_switch_t ukbdsw = {
2260	.probe = &ukbd__probe,
2261	.init = &ukbd_init,
2262	.term = &ukbd_term,
2263	.intr = &ukbd_intr,
2264	.test_if = &ukbd_test_if,
2265	.enable = &ukbd_enable,
2266	.disable = &ukbd_disable,
2267	.read = &ukbd_read,
2268	.check = &ukbd_check,
2269	.read_char = &ukbd_read_char,
2270	.check_char = &ukbd_check_char,
2271	.ioctl = &ukbd_ioctl,
2272	.lock = &ukbd_lock,
2273	.clear_state = &ukbd_clear_state,
2274	.get_state = &ukbd_get_state,
2275	.set_state = &ukbd_set_state,
2276	.poll = &ukbd_poll,
2277};
2278
2279KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
2280
2281static int
2282ukbd_driver_load(module_t mod, int what, void *arg)
2283{
2284	switch (what) {
2285	case MOD_LOAD:
2286		kbd_add_driver(&ukbd_kbd_driver);
2287		break;
2288	case MOD_UNLOAD:
2289		kbd_delete_driver(&ukbd_kbd_driver);
2290		break;
2291	}
2292	return (0);
2293}
2294
2295static devclass_t ukbd_devclass;
2296
2297static device_method_t ukbd_methods[] = {
2298	DEVMETHOD(device_probe, ukbd_probe),
2299	DEVMETHOD(device_attach, ukbd_attach),
2300	DEVMETHOD(device_detach, ukbd_detach),
2301	DEVMETHOD(device_resume, ukbd_resume),
2302
2303	DEVMETHOD_END
2304};
2305
2306static driver_t ukbd_driver = {
2307	.name = "ukbd",
2308	.methods = ukbd_methods,
2309	.size = sizeof(struct ukbd_softc),
2310};
2311
2312DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
2313MODULE_DEPEND(ukbd, usb, 1, 1, 1);
2314#ifdef EVDEV_SUPPORT
2315MODULE_DEPEND(ukbd, evdev, 1, 1, 1);
2316#endif
2317MODULE_VERSION(ukbd, 1);
2318USB_PNP_HOST_INFO(ukbd_devs);
2319