ukbd.c revision 1.109 1 1.109 phx /* $NetBSD: ukbd.c,v 1.109 2010/11/30 11:35:30 phx Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.11 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.59 augustss * by Lennart Augustsson (lennart (at) augustsson.net) at
9 1.11 augustss * Carlstedt Research & Technology.
10 1.1 augustss *
11 1.1 augustss * Redistribution and use in source and binary forms, with or without
12 1.1 augustss * modification, are permitted provided that the following conditions
13 1.1 augustss * are met:
14 1.1 augustss * 1. Redistributions of source code must retain the above copyright
15 1.1 augustss * notice, this list of conditions and the following disclaimer.
16 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 augustss * notice, this list of conditions and the following disclaimer in the
18 1.1 augustss * documentation and/or other materials provided with the distribution.
19 1.1 augustss *
20 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
31 1.1 augustss */
32 1.1 augustss
33 1.17 augustss /*
34 1.85 augustss * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
35 1.17 augustss */
36 1.72 lukem
37 1.72 lukem #include <sys/cdefs.h>
38 1.109 phx __KERNEL_RCSID(0, "$NetBSD: ukbd.c,v 1.109 2010/11/30 11:35:30 phx Exp $");
39 1.17 augustss
40 1.1 augustss #include <sys/param.h>
41 1.1 augustss #include <sys/systm.h>
42 1.57 thorpej #include <sys/callout.h>
43 1.1 augustss #include <sys/kernel.h>
44 1.1 augustss #include <sys/device.h>
45 1.1 augustss #include <sys/ioctl.h>
46 1.1 augustss #include <sys/file.h>
47 1.1 augustss #include <sys/select.h>
48 1.1 augustss #include <sys/proc.h>
49 1.1 augustss #include <sys/vnode.h>
50 1.1 augustss #include <sys/poll.h>
51 1.1 augustss
52 1.1 augustss #include <dev/usb/usb.h>
53 1.2 augustss #include <dev/usb/usbhid.h>
54 1.37 augustss
55 1.1 augustss #include <dev/usb/usbdi.h>
56 1.1 augustss #include <dev/usb/usbdi_util.h>
57 1.1 augustss #include <dev/usb/usbdevs.h>
58 1.1 augustss #include <dev/usb/usb_quirks.h>
59 1.75 augustss #include <dev/usb/uhidev.h>
60 1.1 augustss #include <dev/usb/hid.h>
61 1.31 thorpej #include <dev/usb/ukbdvar.h>
62 1.1 augustss
63 1.2 augustss #include <dev/wscons/wsconsio.h>
64 1.2 augustss #include <dev/wscons/wskbdvar.h>
65 1.2 augustss #include <dev/wscons/wsksymdef.h>
66 1.2 augustss #include <dev/wscons/wsksymvar.h>
67 1.2 augustss
68 1.106 pooka #ifdef _KERNEL_OPT
69 1.89 cube #include "opt_ukbd_layout.h"
70 1.2 augustss #include "opt_wsdisplay_compat.h"
71 1.69 augustss #include "opt_ddb.h"
72 1.106 pooka #endif /* _KERNEL_OPT */
73 1.21 augustss
74 1.62 augustss #ifdef UKBD_DEBUG
75 1.108 dyoung #define DPRINTF(x) if (ukbddebug) printf x
76 1.108 dyoung #define DPRINTFN(n,x) if (ukbddebug>(n)) printf x
77 1.1 augustss int ukbddebug = 0;
78 1.1 augustss #else
79 1.1 augustss #define DPRINTF(x)
80 1.1 augustss #define DPRINTFN(n,x)
81 1.1 augustss #endif
82 1.1 augustss
83 1.75 augustss #define MAXKEYCODE 6
84 1.75 augustss #define MAXMOD 8 /* max 32 */
85 1.2 augustss
86 1.1 augustss struct ukbd_data {
87 1.75 augustss u_int32_t modifiers;
88 1.75 augustss u_int8_t keycode[MAXKEYCODE];
89 1.1 augustss };
90 1.1 augustss
91 1.23 augustss #define PRESS 0x000
92 1.23 augustss #define RELEASE 0x100
93 1.23 augustss #define CODEMASK 0x0ff
94 1.1 augustss
95 1.109 phx struct ukbd_keycodetrans {
96 1.109 phx u_int8_t from;
97 1.109 phx u_int8_t to;
98 1.109 phx };
99 1.109 phx
100 1.109 phx Static const struct ukbd_keycodetrans trtab_apple_fn[] = {
101 1.109 phx { 0x0c, 0x5d }, /* i -> KP 5 */
102 1.109 phx { 0x0d, 0x59 }, /* j -> KP 1 */
103 1.109 phx { 0x0e, 0x5a }, /* k -> KP 2 */
104 1.109 phx { 0x0f, 0x5b }, /* l -> KP 3 */
105 1.109 phx { 0x10, 0x62 }, /* m -> KP 0 */
106 1.109 phx { 0x12, 0x5e }, /* o -> KP 6 */
107 1.109 phx { 0x13, 0x55 }, /* o -> KP * */
108 1.109 phx { 0x18, 0x5c }, /* u -> KP 4 */
109 1.109 phx { 0x0c, 0x5d }, /* i -> KP 5 */
110 1.109 phx { 0x2a, 0x4c }, /* Backspace -> Delete */
111 1.109 phx { 0x28, 0x49 }, /* Return -> Insert */
112 1.109 phx { 0x24, 0x5f }, /* 7 -> KP 7 */
113 1.109 phx { 0x25, 0x60 }, /* 8 -> KP 8 */
114 1.109 phx { 0x26, 0x61 }, /* 9 -> KP 9 */
115 1.109 phx { 0x27, 0x54 }, /* 0 -> KP / */
116 1.109 phx { 0x2d, 0x67 }, /* - -> KP = */
117 1.109 phx { 0x33, 0x56 }, /* ; -> KP - */
118 1.109 phx { 0x37, 0x63 }, /* . -> KP . */
119 1.109 phx { 0x38, 0x57 }, /* / -> KP + */
120 1.109 phx { 0x3a, 0xd1 }, /* F1..F12 mapped to reserved codes 0xd1..0xdc */
121 1.109 phx { 0x3b, 0xd2 },
122 1.109 phx { 0x3c, 0xd3 },
123 1.109 phx { 0x3d, 0xd4 },
124 1.109 phx { 0x3e, 0xd5 },
125 1.109 phx { 0x3f, 0xd6 },
126 1.109 phx { 0x40, 0xd7 },
127 1.109 phx { 0x41, 0xd8 },
128 1.109 phx { 0x42, 0xd9 },
129 1.109 phx { 0x43, 0xda },
130 1.109 phx { 0x44, 0xdb },
131 1.109 phx { 0x45, 0xdc },
132 1.109 phx { 0x4f, 0x4d }, /* Right -> End */
133 1.109 phx { 0x50, 0x4a }, /* Left -> Home */
134 1.109 phx { 0x51, 0x4e }, /* Down -> PageDown */
135 1.109 phx { 0x52, 0x4b }, /* Up -> PageUp */
136 1.109 phx { 0x00, 0x00 }
137 1.109 phx };
138 1.109 phx
139 1.109 phx Static const struct ukbd_keycodetrans trtab_apple_iso[] = {
140 1.109 phx { 0x35, 0x64 }, /* swap the key above tab with key right of shift */
141 1.109 phx { 0x64, 0x35 },
142 1.109 phx { 0x31, 0x32 }, /* key left of return is Europe1, not "\|" */
143 1.109 phx { 0x00, 0x00 }
144 1.109 phx };
145 1.109 phx
146 1.23 augustss #if defined(__NetBSD__) && defined(WSDISPLAY_COMPAT_RAWKBD)
147 1.5 augustss #define NN 0 /* no translation */
148 1.82 augustss /*
149 1.23 augustss * Translate USB keycodes to US keyboard XT scancodes.
150 1.76 augustss * Scancodes >= 0x80 represent EXTENDED keycodes.
151 1.76 augustss *
152 1.104 jakllsch * See http://www.microsoft.com/whdc/archive/scancode.mspx
153 1.104 jakllsch *
154 1.107 sborrill * Note: a real pckbd(4) has more complexity in its
155 1.104 jakllsch * protocol for some keys than this translation implements.
156 1.107 sborrill * For example, some keys generate Fake ShiftL events (e0 2a)
157 1.107 sborrill * before the actual key sequence.
158 1.19 augustss */
159 1.65 jdolecek Static const u_int8_t ukbd_trtab[256] = {
160 1.76 augustss NN, NN, NN, NN, 0x1e, 0x30, 0x2e, 0x20, /* 00 - 07 */
161 1.76 augustss 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, /* 08 - 0f */
162 1.76 augustss 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, /* 10 - 17 */
163 1.76 augustss 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, /* 18 - 1f */
164 1.76 augustss 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 20 - 27 */
165 1.76 augustss 0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, /* 28 - 2f */
166 1.76 augustss 0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, /* 30 - 37 */
167 1.76 augustss 0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, /* 38 - 3f */
168 1.104 jakllsch 0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xb7, 0x46, /* 40 - 47 */
169 1.76 augustss 0x7f, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd, /* 48 - 4f */
170 1.76 augustss 0xcb, 0xd0, 0xc8, 0x45, 0xb5, 0x37, 0x4a, 0x4e, /* 50 - 57 */
171 1.76 augustss 0x9c, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47, /* 58 - 5f */
172 1.76 augustss 0x48, 0x49, 0x52, 0x53, 0x56, 0xdd, NN, 0x59, /* 60 - 67 */
173 1.96 tron 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, NN, /* 68 - 6f */
174 1.76 augustss NN, NN, NN, NN, NN, NN, NN, NN, /* 70 - 77 */
175 1.76 augustss NN, NN, NN, NN, NN, NN, NN, NN, /* 78 - 7f */
176 1.76 augustss NN, NN, NN, NN, NN, 0x7e, NN, 0x73, /* 80 - 87 */
177 1.76 augustss 0x70, 0x7d, 0x79, 0x7b, 0x5c, NN, NN, NN, /* 88 - 8f */
178 1.76 augustss NN, NN, 0x78, 0x77, 0x76, NN, NN, NN, /* 90 - 97 */
179 1.76 augustss NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9f */
180 1.76 augustss NN, NN, NN, NN, NN, NN, NN, NN, /* a0 - a7 */
181 1.76 augustss NN, NN, NN, NN, NN, NN, NN, NN, /* a8 - af */
182 1.76 augustss NN, NN, NN, NN, NN, NN, NN, NN, /* b0 - b7 */
183 1.76 augustss NN, NN, NN, NN, NN, NN, NN, NN, /* b8 - bf */
184 1.76 augustss NN, NN, NN, NN, NN, NN, NN, NN, /* c0 - c7 */
185 1.76 augustss NN, NN, NN, NN, NN, NN, NN, NN, /* c8 - cf */
186 1.76 augustss NN, NN, NN, NN, NN, NN, NN, NN, /* d0 - d7 */
187 1.76 augustss NN, NN, NN, NN, NN, NN, NN, NN, /* d8 - df */
188 1.76 augustss 0x1d, 0x2a, 0x38, 0xdb, 0x9d, 0x36, 0xb8, 0xdc, /* e0 - e7 */
189 1.76 augustss NN, NN, NN, NN, NN, NN, NN, NN, /* e8 - ef */
190 1.76 augustss NN, NN, NN, NN, NN, NN, NN, NN, /* f0 - f7 */
191 1.76 augustss NN, NN, NN, NN, NN, NN, NN, NN, /* f8 - ff */
192 1.1 augustss };
193 1.23 augustss #endif /* defined(__NetBSD__) && defined(WSDISPLAY_COMPAT_RAWKBD) */
194 1.1 augustss
195 1.1 augustss #define KEY_ERROR 0x01
196 1.1 augustss
197 1.75 augustss #define MAXKEYS (MAXMOD+2*MAXKEYCODE)
198 1.20 augustss
199 1.1 augustss struct ukbd_softc {
200 1.75 augustss struct uhidev sc_hdev;
201 1.1 augustss
202 1.1 augustss struct ukbd_data sc_ndata;
203 1.1 augustss struct ukbd_data sc_odata;
204 1.75 augustss struct hid_location sc_modloc[MAXMOD];
205 1.75 augustss u_int sc_nmod;
206 1.75 augustss struct {
207 1.75 augustss u_int32_t mask;
208 1.75 augustss u_int8_t key;
209 1.75 augustss } sc_mods[MAXMOD];
210 1.75 augustss
211 1.75 augustss struct hid_location sc_keycodeloc;
212 1.75 augustss u_int sc_nkeycode;
213 1.1 augustss
214 1.109 phx u_int sc_flags; /* flags */
215 1.109 phx #define FLAG_ENABLED 0x0001
216 1.109 phx #define FLAG_POLLING 0x0002
217 1.109 phx #define FLAG_DEBOUNCE 0x0004 /* for quirk handling */
218 1.109 phx #define FLAG_APPLE_FIX_ISO 0x0008
219 1.109 phx #define FLAG_APPLE_FN 0x0010
220 1.109 phx #define FLAG_FN_PRESSED 0x0100 /* FN key is held down */
221 1.109 phx #define FLAG_FN_ALT 0x0200 /* Last Alt key was FN-Alt = AltGr */
222 1.1 augustss
223 1.29 thorpej int sc_console_keyboard; /* we are the console keyboard */
224 1.29 thorpej
225 1.109 phx struct callout sc_delay; /* for quirk handling */
226 1.62 augustss struct ukbd_data sc_data; /* for quirk handling */
227 1.62 augustss
228 1.109 phx struct hid_location sc_apple_fn;
229 1.75 augustss struct hid_location sc_numloc;
230 1.75 augustss struct hid_location sc_capsloc;
231 1.75 augustss struct hid_location sc_scroloc;
232 1.2 augustss int sc_leds;
233 1.16 augustss #if defined(__NetBSD__)
234 1.97 dyoung device_t sc_wskbddev;
235 1.57 thorpej
236 1.23 augustss #if defined(WSDISPLAY_COMPAT_RAWKBD)
237 1.87 augustss int sc_rawkbd;
238 1.87 augustss #if defined(UKBD_REPEAT)
239 1.108 dyoung struct callout sc_rawrepeat_ch;
240 1.20 augustss #define REP_DELAY1 400
241 1.20 augustss #define REP_DELAYN 100
242 1.20 augustss int sc_nrep;
243 1.20 augustss char sc_rep[MAXKEYS];
244 1.87 augustss #endif /* defined(UKBD_REPEAT) */
245 1.23 augustss #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
246 1.3 augustss
247 1.84 augustss int sc_spl;
248 1.23 augustss int sc_npollchar;
249 1.23 augustss u_int16_t sc_pollchars[MAXKEYS];
250 1.23 augustss #endif /* defined(__NetBSD__) */
251 1.40 augustss
252 1.40 augustss u_char sc_dying;
253 1.1 augustss };
254 1.1 augustss
255 1.62 augustss #ifdef UKBD_DEBUG
256 1.62 augustss #define UKBDTRACESIZE 64
257 1.62 augustss struct ukbdtraceinfo {
258 1.62 augustss int unit;
259 1.62 augustss struct timeval tv;
260 1.62 augustss struct ukbd_data ud;
261 1.62 augustss };
262 1.62 augustss struct ukbdtraceinfo ukbdtracedata[UKBDTRACESIZE];
263 1.62 augustss int ukbdtraceindex = 0;
264 1.62 augustss int ukbdtrace = 0;
265 1.62 augustss void ukbdtracedump(void);
266 1.62 augustss void
267 1.62 augustss ukbdtracedump(void)
268 1.62 augustss {
269 1.62 augustss int i;
270 1.62 augustss for (i = 0; i < UKBDTRACESIZE; i++) {
271 1.82 augustss struct ukbdtraceinfo *p =
272 1.62 augustss &ukbdtracedata[(i+ukbdtraceindex)%UKBDTRACESIZE];
273 1.102 cegger printf("%"PRIu64".%06"PRIu64": mod=0x%02x key0=0x%02x key1=0x%02x "
274 1.62 augustss "key2=0x%02x key3=0x%02x\n",
275 1.102 cegger p->tv.tv_sec, (uint64_t)p->tv.tv_usec,
276 1.62 augustss p->ud.modifiers, p->ud.keycode[0], p->ud.keycode[1],
277 1.62 augustss p->ud.keycode[2], p->ud.keycode[3]);
278 1.62 augustss }
279 1.62 augustss }
280 1.62 augustss #endif
281 1.62 augustss
282 1.1 augustss #define UKBDUNIT(dev) (minor(dev))
283 1.1 augustss #define UKBD_CHUNK 128 /* chunk size for read */
284 1.1 augustss #define UKBD_BSIZE 1020 /* buffer size */
285 1.1 augustss
286 1.58 augustss Static int ukbd_is_console;
287 1.31 thorpej
288 1.60 augustss Static void ukbd_cngetc(void *, u_int *, int *);
289 1.60 augustss Static void ukbd_cnpollc(void *, int);
290 1.3 augustss
291 1.16 augustss #if defined(__NetBSD__)
292 1.8 drochner const struct wskbd_consops ukbd_consops = {
293 1.8 drochner ukbd_cngetc,
294 1.8 drochner ukbd_cnpollc,
295 1.92 christos NULL, /* bell */
296 1.8 drochner };
297 1.16 augustss #endif
298 1.8 drochner
299 1.75 augustss Static const char *ukbd_parse_desc(struct ukbd_softc *sc);
300 1.75 augustss
301 1.75 augustss Static void ukbd_intr(struct uhidev *addr, void *ibuf, u_int len);
302 1.62 augustss Static void ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud);
303 1.62 augustss Static void ukbd_delayed_decode(void *addr);
304 1.2 augustss
305 1.60 augustss Static int ukbd_enable(void *, int);
306 1.60 augustss Static void ukbd_set_leds(void *, int);
307 1.23 augustss
308 1.16 augustss #if defined(__NetBSD__)
309 1.95 christos Static int ukbd_ioctl(void *, u_long, void *, int, struct lwp *);
310 1.88 jonathan #if defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT)
311 1.60 augustss Static void ukbd_rawrepeat(void *v);
312 1.48 mjacob #endif
313 1.1 augustss
314 1.8 drochner const struct wskbd_accessops ukbd_accessops = {
315 1.8 drochner ukbd_enable,
316 1.8 drochner ukbd_set_leds,
317 1.8 drochner ukbd_ioctl,
318 1.8 drochner };
319 1.8 drochner
320 1.23 augustss extern const struct wscons_keydesc ukbd_keydesctab[];
321 1.23 augustss
322 1.8 drochner const struct wskbd_mapdata ukbd_keymapdata = {
323 1.23 augustss ukbd_keydesctab,
324 1.86 augustss #if defined(UKBD_LAYOUT)
325 1.66 augustss UKBD_LAYOUT,
326 1.86 augustss #elif defined(PCKBD_LAYOUT)
327 1.86 augustss PCKBD_LAYOUT,
328 1.66 augustss #else
329 1.8 drochner KB_US,
330 1.66 augustss #endif
331 1.8 drochner };
332 1.16 augustss #endif
333 1.8 drochner
334 1.100 cube static int ukbd_match(device_t, cfdata_t, void *);
335 1.97 dyoung static void ukbd_attach(device_t, device_t, void *);
336 1.97 dyoung static int ukbd_detach(device_t, int);
337 1.97 dyoung static int ukbd_activate(device_t, enum devact);
338 1.97 dyoung static void ukbd_childdet(device_t, device_t);
339 1.97 dyoung
340 1.97 dyoung extern struct cfdriver ukbd_cd;
341 1.97 dyoung
342 1.100 cube CFATTACH_DECL2_NEW(ukbd, sizeof(struct ukbd_softc), ukbd_match, ukbd_attach,
343 1.97 dyoung ukbd_detach, ukbd_activate, NULL, ukbd_childdet);
344 1.1 augustss
345 1.75 augustss int
346 1.100 cube ukbd_match(device_t parent, cfdata_t match, void *aux)
347 1.1 augustss {
348 1.75 augustss struct uhidev_attach_arg *uha = aux;
349 1.75 augustss int size;
350 1.75 augustss void *desc;
351 1.82 augustss
352 1.75 augustss uhidev_get_report_desc(uha->parent, &desc, &size);
353 1.75 augustss if (!hid_is_collection(desc, size, uha->reportid,
354 1.75 augustss HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD)))
355 1.1 augustss return (UMATCH_NONE);
356 1.75 augustss
357 1.75 augustss return (UMATCH_IFACECLASS);
358 1.1 augustss }
359 1.1 augustss
360 1.75 augustss void
361 1.97 dyoung ukbd_attach(device_t parent, device_t self, void *aux)
362 1.1 augustss {
363 1.97 dyoung struct ukbd_softc *sc = device_private(self);
364 1.75 augustss struct uhidev_attach_arg *uha = aux;
365 1.62 augustss u_int32_t qflags;
366 1.75 augustss const char *parseerr;
367 1.16 augustss #if defined(__NetBSD__)
368 1.2 augustss struct wskbddev_attach_args a;
369 1.16 augustss #else
370 1.16 augustss int i;
371 1.16 augustss #endif
372 1.82 augustss
373 1.100 cube sc->sc_hdev.sc_dev = self;
374 1.75 augustss sc->sc_hdev.sc_intr = ukbd_intr;
375 1.75 augustss sc->sc_hdev.sc_parent = uha->parent;
376 1.75 augustss sc->sc_hdev.sc_report_id = uha->reportid;
377 1.109 phx sc->sc_flags = 0;
378 1.75 augustss
379 1.101 jmcneill if (!pmf_device_register(self, NULL, NULL)) {
380 1.101 jmcneill aprint_normal("\n");
381 1.101 jmcneill aprint_error_dev(self, "couldn't establish power handler\n");
382 1.101 jmcneill }
383 1.101 jmcneill
384 1.75 augustss parseerr = ukbd_parse_desc(sc);
385 1.75 augustss if (parseerr != NULL) {
386 1.98 cegger aprint_normal("\n");
387 1.100 cube aprint_error_dev(self, "attach failed, %s\n", parseerr);
388 1.108 dyoung return;
389 1.1 augustss }
390 1.82 augustss
391 1.109 phx /* Quirks */
392 1.109 phx qflags = usbd_get_quirks(uha->parent->sc_udev)->uq_flags;
393 1.109 phx if (qflags & UQ_SPUR_BUT_UP)
394 1.109 phx sc->sc_flags |= FLAG_DEBOUNCE;
395 1.109 phx if (qflags & UQ_APPLE_ISO)
396 1.109 phx sc->sc_flags |= FLAG_APPLE_FIX_ISO;
397 1.109 phx
398 1.75 augustss #ifdef DIAGNOSTIC
399 1.100 cube aprint_normal(": %d modifier keys, %d key codes", sc->sc_nmod,
400 1.75 augustss sc->sc_nkeycode);
401 1.109 phx if (sc->sc_flags & FLAG_APPLE_FN)
402 1.109 phx aprint_normal(", apple fn key");
403 1.109 phx if (sc->sc_flags & FLAG_APPLE_FIX_ISO)
404 1.109 phx aprint_normal(", fix apple iso");
405 1.75 augustss #endif
406 1.100 cube aprint_normal("\n");
407 1.1 augustss
408 1.29 thorpej /*
409 1.29 thorpej * Remember if we're the console keyboard.
410 1.29 thorpej *
411 1.31 thorpej * XXX This always picks the first keyboard on the
412 1.31 thorpej * first USB bus, but what else can we really do?
413 1.29 thorpej */
414 1.31 thorpej if ((sc->sc_console_keyboard = ukbd_is_console) != 0) {
415 1.29 thorpej /* Don't let any other keyboard have it. */
416 1.31 thorpej ukbd_is_console = 0;
417 1.29 thorpej }
418 1.29 thorpej
419 1.31 thorpej if (sc->sc_console_keyboard) {
420 1.32 augustss DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc));
421 1.31 thorpej wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
422 1.34 wrstuden ukbd_enable(sc, 1);
423 1.31 thorpej }
424 1.29 thorpej
425 1.29 thorpej a.console = sc->sc_console_keyboard;
426 1.8 drochner
427 1.8 drochner a.keymap = &ukbd_keymapdata;
428 1.8 drochner
429 1.8 drochner a.accessops = &ukbd_accessops;
430 1.2 augustss a.accesscookie = sc;
431 1.8 drochner
432 1.87 augustss #ifdef UKBD_REPEAT
433 1.108 dyoung callout_init(&sc->sc_rawrepeat_ch, 0);
434 1.87 augustss #endif
435 1.57 thorpej
436 1.108 dyoung callout_init(&sc->sc_delay, 0);
437 1.62 augustss
438 1.19 augustss /* Flash the leds; no real purpose, just shows we're alive. */
439 1.19 augustss ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS);
440 1.75 augustss usbd_delay_ms(uha->parent->sc_udev, 400);
441 1.19 augustss ukbd_set_leds(sc, 0);
442 1.19 augustss
443 1.64 augustss sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
444 1.55 augustss
445 1.108 dyoung return;
446 1.1 augustss }
447 1.1 augustss
448 1.8 drochner int
449 1.60 augustss ukbd_enable(void *v, int on)
450 1.8 drochner {
451 1.9 augustss struct ukbd_softc *sc = v;
452 1.9 augustss
453 1.40 augustss if (on && sc->sc_dying)
454 1.40 augustss return (EIO);
455 1.40 augustss
456 1.38 augustss /* Should only be called to change state */
457 1.109 phx if ((sc->sc_flags & FLAG_ENABLED) != 0 && on != 0) {
458 1.38 augustss #ifdef DIAGNOSTIC
459 1.82 augustss printf("ukbd_enable: %s: bad call on=%d\n",
460 1.108 dyoung device_xname(sc->sc_hdev.sc_dev), on);
461 1.38 augustss #endif
462 1.38 augustss return (EBUSY);
463 1.38 augustss }
464 1.38 augustss
465 1.33 augustss DPRINTF(("ukbd_enable: sc=%p on=%d\n", sc, on));
466 1.9 augustss if (on) {
467 1.109 phx sc->sc_flags |= FLAG_ENABLED;
468 1.75 augustss return (uhidev_open(&sc->sc_hdev));
469 1.9 augustss } else {
470 1.109 phx sc->sc_flags &= ~FLAG_ENABLED;
471 1.75 augustss uhidev_close(&sc->sc_hdev);
472 1.75 augustss return (0);
473 1.9 augustss }
474 1.37 augustss }
475 1.37 augustss
476 1.97 dyoung
477 1.97 dyoung static void
478 1.97 dyoung ukbd_childdet(device_t self, device_t child)
479 1.97 dyoung {
480 1.97 dyoung struct ukbd_softc *sc = device_private(self);
481 1.97 dyoung
482 1.97 dyoung KASSERT(sc->sc_wskbddev == child);
483 1.97 dyoung sc->sc_wskbddev = NULL;
484 1.97 dyoung }
485 1.97 dyoung
486 1.37 augustss int
487 1.97 dyoung ukbd_activate(device_t self, enum devact act)
488 1.37 augustss {
489 1.97 dyoung struct ukbd_softc *sc = device_private(self);
490 1.40 augustss
491 1.40 augustss switch (act) {
492 1.40 augustss case DVACT_DEACTIVATE:
493 1.40 augustss sc->sc_dying = 1;
494 1.105 dyoung return 0;
495 1.105 dyoung default:
496 1.105 dyoung return EOPNOTSUPP;
497 1.40 augustss }
498 1.37 augustss }
499 1.37 augustss
500 1.75 augustss int
501 1.97 dyoung ukbd_detach(device_t self, int flags)
502 1.37 augustss {
503 1.97 dyoung struct ukbd_softc *sc = device_private(self);
504 1.37 augustss int rv = 0;
505 1.37 augustss
506 1.37 augustss DPRINTF(("ukbd_detach: sc=%p flags=%d\n", sc, flags));
507 1.46 augustss
508 1.101 jmcneill pmf_device_deregister(self);
509 1.101 jmcneill
510 1.37 augustss if (sc->sc_console_keyboard) {
511 1.50 augustss #if 0
512 1.37 augustss /*
513 1.37 augustss * XXX Should probably disconnect our consops,
514 1.37 augustss * XXX and either notify some other keyboard that
515 1.37 augustss * XXX it can now be the console, or if there aren't
516 1.37 augustss * XXX any more USB keyboards, set ukbd_is_console
517 1.37 augustss * XXX back to 1 so that the next USB keyboard attached
518 1.37 augustss * XXX to the system will get it.
519 1.37 augustss */
520 1.37 augustss panic("ukbd_detach: console keyboard");
521 1.50 augustss #else
522 1.51 augustss /*
523 1.51 augustss * Disconnect our consops and set ukbd_is_console
524 1.51 augustss * back to 1 so that the next USB keyboard attached
525 1.51 augustss * to the system will get it.
526 1.51 augustss * XXX Should notify some other keyboard that it can be
527 1.51 augustss * XXX console, if there are any other keyboards.
528 1.51 augustss */
529 1.75 augustss printf("%s: was console keyboard\n",
530 1.108 dyoung device_xname(sc->sc_hdev.sc_dev));
531 1.50 augustss wskbd_cndetach();
532 1.50 augustss ukbd_is_console = 1;
533 1.50 augustss #endif
534 1.37 augustss }
535 1.45 augustss /* No need to do reference counting of ukbd, wskbd has all the goo. */
536 1.47 augustss if (sc->sc_wskbddev != NULL)
537 1.37 augustss rv = config_detach(sc->sc_wskbddev, flags);
538 1.68 augustss
539 1.68 augustss /* The console keyboard does not get a disable call, so check pipe. */
540 1.75 augustss if (sc->sc_hdev.sc_state & UHIDEV_OPEN)
541 1.75 augustss uhidev_close(&sc->sc_hdev);
542 1.55 augustss
543 1.37 augustss return (rv);
544 1.1 augustss }
545 1.1 augustss
546 1.109 phx static void
547 1.109 phx ukbd_translate_keycodes(struct ukbd_softc *sc, struct ukbd_data *ud,
548 1.109 phx const struct ukbd_keycodetrans *tab)
549 1.109 phx {
550 1.109 phx const struct ukbd_keycodetrans *tp;
551 1.109 phx int i;
552 1.109 phx u_int8_t key;
553 1.109 phx
554 1.109 phx for (i = 0; i < sc->sc_nkeycode; i++) {
555 1.109 phx key = ud->keycode[i];
556 1.109 phx if (key)
557 1.109 phx for (tp = tab; tp->from; tp++)
558 1.109 phx if (tp->from == key) {
559 1.109 phx ud->keycode[i] = tp->to;
560 1.109 phx break;
561 1.109 phx }
562 1.109 phx }
563 1.109 phx }
564 1.109 phx
565 1.109 phx static u_int16_t
566 1.109 phx ukbd_translate_modifier(struct ukbd_softc *sc, u_int16_t key)
567 1.109 phx {
568 1.109 phx if ((sc->sc_flags & FLAG_APPLE_FN) && (key & CODEMASK) == 0x00e2) {
569 1.109 phx if ((key & ~CODEMASK) == PRESS) {
570 1.109 phx if (sc->sc_flags & FLAG_FN_PRESSED) {
571 1.109 phx /* pressed FN-Alt, translate to AltGr */
572 1.109 phx key = 0x00e6 | PRESS;
573 1.109 phx sc->sc_flags |= FLAG_FN_ALT;
574 1.109 phx }
575 1.109 phx } else {
576 1.109 phx if (sc->sc_flags & FLAG_FN_ALT) {
577 1.109 phx /* released Alt, which was treated as FN-Alt */
578 1.109 phx key = 0x00e6 | RELEASE;
579 1.109 phx sc->sc_flags &= ~FLAG_FN_ALT;
580 1.109 phx }
581 1.109 phx }
582 1.109 phx }
583 1.109 phx return key;
584 1.109 phx }
585 1.109 phx
586 1.1 augustss void
587 1.94 christos ukbd_intr(struct uhidev *addr, void *ibuf, u_int len)
588 1.1 augustss {
589 1.75 augustss struct ukbd_softc *sc = (struct ukbd_softc *)addr;
590 1.1 augustss struct ukbd_data *ud = &sc->sc_ndata;
591 1.75 augustss int i;
592 1.76 augustss
593 1.76 augustss #ifdef UKBD_DEBUG
594 1.76 augustss if (ukbddebug > 5) {
595 1.76 augustss printf("ukbd_intr: data");
596 1.76 augustss for (i = 0; i < len; i++)
597 1.78 augustss printf(" 0x%02x", ((u_char *)ibuf)[i]);
598 1.76 augustss printf("\n");
599 1.76 augustss }
600 1.76 augustss #endif
601 1.76 augustss
602 1.75 augustss ud->modifiers = 0;
603 1.75 augustss for (i = 0; i < sc->sc_nmod; i++)
604 1.75 augustss if (hid_get_data(ibuf, &sc->sc_modloc[i]))
605 1.80 augustss ud->modifiers |= sc->sc_mods[i].mask;
606 1.75 augustss memcpy(ud->keycode, (char *)ibuf + sc->sc_keycodeloc.pos / 8,
607 1.75 augustss sc->sc_nkeycode);
608 1.1 augustss
609 1.109 phx if (sc->sc_flags & FLAG_APPLE_FN) {
610 1.109 phx if (hid_get_data(ibuf, &sc->sc_apple_fn)) {
611 1.109 phx sc->sc_flags |= FLAG_FN_PRESSED;
612 1.109 phx ukbd_translate_keycodes(sc, ud, trtab_apple_fn);
613 1.109 phx }
614 1.109 phx else
615 1.109 phx sc->sc_flags &= ~FLAG_FN_PRESSED;
616 1.109 phx }
617 1.109 phx
618 1.109 phx if ((sc->sc_flags & FLAG_DEBOUNCE) && !(sc->sc_flags & FLAG_POLLING)) {
619 1.62 augustss /*
620 1.62 augustss * Some keyboards have a peculiar quirk. They sometimes
621 1.62 augustss * generate a key up followed by a key down for the same
622 1.62 augustss * key after about 10 ms.
623 1.62 augustss * We avoid this bug by holding off decoding for 20 ms.
624 1.62 augustss */
625 1.62 augustss sc->sc_data = *ud;
626 1.108 dyoung callout_reset(&sc->sc_delay, hz / 50, ukbd_delayed_decode, sc);
627 1.74 lukem #ifdef DDB
628 1.109 phx } else if (sc->sc_console_keyboard && !(sc->sc_flags & FLAG_POLLING)) {
629 1.69 augustss /*
630 1.69 augustss * For the console keyboard we can't deliver CTL-ALT-ESC
631 1.69 augustss * from the interrupt routine. Doing so would start
632 1.69 augustss * polling from inside the interrupt routine and that
633 1.69 augustss * loses bigtime.
634 1.69 augustss */
635 1.69 augustss sc->sc_data = *ud;
636 1.108 dyoung callout_reset(&sc->sc_delay, 1, ukbd_delayed_decode, sc);
637 1.69 augustss #endif
638 1.62 augustss } else {
639 1.62 augustss ukbd_decode(sc, ud);
640 1.62 augustss }
641 1.62 augustss }
642 1.62 augustss
643 1.62 augustss void
644 1.62 augustss ukbd_delayed_decode(void *addr)
645 1.62 augustss {
646 1.62 augustss struct ukbd_softc *sc = addr;
647 1.62 augustss
648 1.62 augustss ukbd_decode(sc, &sc->sc_data);
649 1.62 augustss }
650 1.62 augustss
651 1.62 augustss void
652 1.62 augustss ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud)
653 1.62 augustss {
654 1.62 augustss int mod, omod;
655 1.62 augustss u_int16_t ibuf[MAXKEYS]; /* chars events */
656 1.62 augustss int s;
657 1.62 augustss int nkeys, i, j;
658 1.62 augustss int key;
659 1.62 augustss #define ADDKEY(c) ibuf[nkeys++] = (c)
660 1.62 augustss
661 1.62 augustss #ifdef UKBD_DEBUG
662 1.82 augustss /*
663 1.62 augustss * Keep a trace of the last events. Using printf changes the
664 1.62 augustss * timing, so this can be useful sometimes.
665 1.62 augustss */
666 1.62 augustss if (ukbdtrace) {
667 1.62 augustss struct ukbdtraceinfo *p = &ukbdtracedata[ukbdtraceindex];
668 1.100 cube p->unit = device_unit(sc->sc_hdev.sc_dev);
669 1.62 augustss microtime(&p->tv);
670 1.62 augustss p->ud = *ud;
671 1.62 augustss if (++ukbdtraceindex >= UKBDTRACESIZE)
672 1.62 augustss ukbdtraceindex = 0;
673 1.62 augustss }
674 1.62 augustss if (ukbddebug > 5) {
675 1.62 augustss struct timeval tv;
676 1.62 augustss microtime(&tv);
677 1.102 cegger DPRINTF((" at %"PRIu64".%06"PRIu64" mod=0x%02x key0=0x%02x key1=0x%02x "
678 1.62 augustss "key2=0x%02x key3=0x%02x\n",
679 1.102 cegger tv.tv_sec, (uint64_t)tv.tv_usec,
680 1.62 augustss ud->modifiers, ud->keycode[0], ud->keycode[1],
681 1.62 augustss ud->keycode[2], ud->keycode[3]));
682 1.62 augustss }
683 1.62 augustss #endif
684 1.1 augustss
685 1.53 augustss if (ud->keycode[0] == KEY_ERROR) {
686 1.53 augustss DPRINTF(("ukbd_intr: KEY_ERROR\n"));
687 1.1 augustss return; /* ignore */
688 1.53 augustss }
689 1.109 phx
690 1.109 phx if (sc->sc_flags & FLAG_APPLE_FIX_ISO)
691 1.109 phx ukbd_translate_keycodes(sc, ud, trtab_apple_iso);
692 1.109 phx
693 1.1 augustss nkeys = 0;
694 1.1 augustss mod = ud->modifiers;
695 1.1 augustss omod = sc->sc_odata.modifiers;
696 1.1 augustss if (mod != omod)
697 1.75 augustss for (i = 0; i < sc->sc_nmod; i++)
698 1.82 augustss if (( mod & sc->sc_mods[i].mask) !=
699 1.109 phx (omod & sc->sc_mods[i].mask)) {
700 1.109 phx key = sc->sc_mods[i].key |
701 1.109 phx ((mod & sc->sc_mods[i].mask) ?
702 1.109 phx PRESS : RELEASE);
703 1.109 phx ADDKEY(ukbd_translate_modifier(sc, key));
704 1.109 phx }
705 1.75 augustss if (memcmp(ud->keycode, sc->sc_odata.keycode, sc->sc_nkeycode) != 0) {
706 1.1 augustss /* Check for released keys. */
707 1.75 augustss for (i = 0; i < sc->sc_nkeycode; i++) {
708 1.1 augustss key = sc->sc_odata.keycode[i];
709 1.1 augustss if (key == 0)
710 1.1 augustss continue;
711 1.75 augustss for (j = 0; j < sc->sc_nkeycode; j++)
712 1.1 augustss if (key == ud->keycode[j])
713 1.1 augustss goto rfound;
714 1.62 augustss DPRINTFN(3,("ukbd_intr: relse key=0x%02x\n", key));
715 1.23 augustss ADDKEY(key | RELEASE);
716 1.1 augustss rfound:
717 1.1 augustss ;
718 1.1 augustss }
719 1.82 augustss
720 1.1 augustss /* Check for pressed keys. */
721 1.75 augustss for (i = 0; i < sc->sc_nkeycode; i++) {
722 1.1 augustss key = ud->keycode[i];
723 1.1 augustss if (key == 0)
724 1.1 augustss continue;
725 1.75 augustss for (j = 0; j < sc->sc_nkeycode; j++)
726 1.1 augustss if (key == sc->sc_odata.keycode[j])
727 1.1 augustss goto pfound;
728 1.23 augustss DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key));
729 1.23 augustss ADDKEY(key | PRESS);
730 1.1 augustss pfound:
731 1.1 augustss ;
732 1.1 augustss }
733 1.1 augustss }
734 1.1 augustss sc->sc_odata = *ud;
735 1.1 augustss
736 1.20 augustss if (nkeys == 0)
737 1.20 augustss return;
738 1.20 augustss
739 1.109 phx if (sc->sc_flags & FLAG_POLLING) {
740 1.23 augustss DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0]));
741 1.23 augustss memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
742 1.23 augustss sc->sc_npollchar = nkeys;
743 1.3 augustss return;
744 1.3 augustss }
745 1.20 augustss #ifdef WSDISPLAY_COMPAT_RAWKBD
746 1.19 augustss if (sc->sc_rawkbd) {
747 1.79 augustss u_char cbuf[MAXKEYS * 2];
748 1.24 augustss int c;
749 1.20 augustss int npress;
750 1.20 augustss
751 1.23 augustss for (npress = i = j = 0; i < nkeys; i++) {
752 1.23 augustss key = ibuf[i];
753 1.23 augustss c = ukbd_trtab[key & CODEMASK];
754 1.23 augustss if (c == NN)
755 1.23 augustss continue;
756 1.104 jakllsch if (c == 0x7f) {
757 1.104 jakllsch /* pause key */
758 1.104 jakllsch cbuf[j++] = 0xe1;
759 1.104 jakllsch cbuf[j++] = 0x1d;
760 1.104 jakllsch cbuf[j-1] |= (key & RELEASE) ? 0x80 : 0;
761 1.104 jakllsch cbuf[j] = 0x45;
762 1.104 jakllsch } else {
763 1.104 jakllsch if (c & 0x80)
764 1.104 jakllsch cbuf[j++] = 0xe0;
765 1.104 jakllsch cbuf[j] = c & 0x7f;
766 1.104 jakllsch }
767 1.23 augustss if (key & RELEASE)
768 1.19 augustss cbuf[j] |= 0x80;
769 1.87 augustss #if defined(UKBD_REPEAT)
770 1.20 augustss else {
771 1.23 augustss /* remember pressed keys for autorepeat */
772 1.20 augustss if (c & 0x80)
773 1.20 augustss sc->sc_rep[npress++] = 0xe0;
774 1.20 augustss sc->sc_rep[npress++] = c & 0x7f;
775 1.20 augustss }
776 1.87 augustss #endif
777 1.82 augustss DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n",
778 1.27 augustss c & 0x80 ? "0xe0 " : "",
779 1.27 augustss cbuf[j]));
780 1.23 augustss j++;
781 1.19 augustss }
782 1.30 augustss s = spltty();
783 1.19 augustss wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
784 1.30 augustss splx(s);
785 1.87 augustss #ifdef UKBD_REPEAT
786 1.108 dyoung callout_stop(&sc->sc_rawrepeat_ch);
787 1.20 augustss if (npress != 0) {
788 1.20 augustss sc->sc_nrep = npress;
789 1.108 dyoung callout_reset(&sc->sc_rawrepeat_ch,
790 1.57 thorpej hz * REP_DELAY1 / 1000, ukbd_rawrepeat, sc);
791 1.20 augustss }
792 1.87 augustss #endif
793 1.19 augustss return;
794 1.19 augustss }
795 1.19 augustss #endif
796 1.19 augustss
797 1.30 augustss s = spltty();
798 1.2 augustss for (i = 0; i < nkeys; i++) {
799 1.23 augustss key = ibuf[i];
800 1.82 augustss wskbd_input(sc->sc_wskbddev,
801 1.23 augustss key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
802 1.23 augustss key&CODEMASK);
803 1.20 augustss }
804 1.30 augustss splx(s);
805 1.1 augustss }
806 1.1 augustss
807 1.2 augustss void
808 1.60 augustss ukbd_set_leds(void *v, int leds)
809 1.1 augustss {
810 1.2 augustss struct ukbd_softc *sc = v;
811 1.2 augustss u_int8_t res;
812 1.1 augustss
813 1.71 augustss DPRINTF(("ukbd_set_leds: sc=%p leds=%d, sc_leds=%d\n",
814 1.71 augustss sc, leds, sc->sc_leds));
815 1.40 augustss
816 1.40 augustss if (sc->sc_dying)
817 1.40 augustss return;
818 1.1 augustss
819 1.71 augustss if (sc->sc_leds == leds)
820 1.71 augustss return;
821 1.2 augustss sc->sc_leds = leds;
822 1.2 augustss res = 0;
823 1.75 augustss /* XXX not really right */
824 1.75 augustss if ((leds & WSKBD_LED_SCROLL) && sc->sc_scroloc.size == 1)
825 1.75 augustss res |= 1 << sc->sc_scroloc.pos;
826 1.75 augustss if ((leds & WSKBD_LED_NUM) && sc->sc_numloc.size == 1)
827 1.75 augustss res |= 1 << sc->sc_numloc.pos;
828 1.75 augustss if ((leds & WSKBD_LED_CAPS) && sc->sc_capsloc.size == 1)
829 1.75 augustss res |= 1 << sc->sc_capsloc.pos;
830 1.75 augustss uhidev_set_report_async(&sc->sc_hdev, UHID_OUTPUT_REPORT, &res, 1);
831 1.1 augustss }
832 1.1 augustss
833 1.87 augustss #if defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT)
834 1.20 augustss void
835 1.60 augustss ukbd_rawrepeat(void *v)
836 1.20 augustss {
837 1.20 augustss struct ukbd_softc *sc = v;
838 1.30 augustss int s;
839 1.20 augustss
840 1.30 augustss s = spltty();
841 1.20 augustss wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
842 1.30 augustss splx(s);
843 1.108 dyoung callout_reset(&sc->sc_rawrepeat_ch, hz * REP_DELAYN / 1000,
844 1.57 thorpej ukbd_rawrepeat, sc);
845 1.20 augustss }
846 1.87 augustss #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT) */
847 1.20 augustss
848 1.1 augustss int
849 1.95 christos ukbd_ioctl(void *v, u_long cmd, void *data, int flag,
850 1.94 christos struct lwp *l)
851 1.1 augustss {
852 1.2 augustss struct ukbd_softc *sc = v;
853 1.1 augustss
854 1.2 augustss switch (cmd) {
855 1.2 augustss case WSKBDIO_GTYPE:
856 1.20 augustss *(int *)data = WSKBD_TYPE_USB;
857 1.17 augustss return (0);
858 1.2 augustss case WSKBDIO_SETLEDS:
859 1.2 augustss ukbd_set_leds(v, *(int *)data);
860 1.17 augustss return (0);
861 1.2 augustss case WSKBDIO_GETLEDS:
862 1.2 augustss *(int *)data = sc->sc_leds;
863 1.2 augustss return (0);
864 1.87 augustss #if defined(WSDISPLAY_COMPAT_RAWKBD)
865 1.2 augustss case WSKBDIO_SETMODE:
866 1.19 augustss DPRINTF(("ukbd_ioctl: set raw = %d\n", *(int *)data));
867 1.2 augustss sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
868 1.87 augustss #if defined(UKBD_REPEAT)
869 1.108 dyoung callout_stop(&sc->sc_rawrepeat_ch);
870 1.87 augustss #endif
871 1.2 augustss return (0);
872 1.2 augustss #endif
873 1.2 augustss }
874 1.81 atatat return (EPASSTHROUGH);
875 1.1 augustss }
876 1.7 augustss
877 1.71 augustss /*
878 1.71 augustss * This is a hack to work around some broken ports that don't call
879 1.71 augustss * cnpollc() before cngetc().
880 1.71 augustss */
881 1.71 augustss static int pollenter, warned;
882 1.71 augustss
883 1.7 augustss /* Console interface. */
884 1.3 augustss void
885 1.60 augustss ukbd_cngetc(void *v, u_int *type, int *data)
886 1.3 augustss {
887 1.3 augustss struct ukbd_softc *sc = v;
888 1.4 augustss int c;
889 1.71 augustss int broken;
890 1.71 augustss
891 1.71 augustss if (pollenter == 0) {
892 1.71 augustss if (!warned) {
893 1.71 augustss printf("\n"
894 1.71 augustss "This port is broken, it does not call cnpollc() before calling cngetc().\n"
895 1.71 augustss "This should be fixed, but it will work anyway (for now).\n");
896 1.71 augustss warned = 1;
897 1.71 augustss }
898 1.71 augustss broken = 1;
899 1.71 augustss ukbd_cnpollc(v, 1);
900 1.71 augustss } else
901 1.71 augustss broken = 0;
902 1.3 augustss
903 1.50 augustss DPRINTFN(0,("ukbd_cngetc: enter\n"));
904 1.109 phx sc->sc_flags |= FLAG_POLLING;
905 1.23 augustss while(sc->sc_npollchar <= 0)
906 1.75 augustss usbd_dopoll(sc->sc_hdev.sc_parent->sc_iface);
907 1.109 phx sc->sc_flags &= ~FLAG_POLLING;
908 1.23 augustss c = sc->sc_pollchars[0];
909 1.23 augustss sc->sc_npollchar--;
910 1.82 augustss memcpy(sc->sc_pollchars, sc->sc_pollchars+1,
911 1.23 augustss sc->sc_npollchar * sizeof(u_int16_t));
912 1.6 augustss *type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
913 1.23 augustss *data = c & CODEMASK;
914 1.50 augustss DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", c));
915 1.71 augustss if (broken)
916 1.71 augustss ukbd_cnpollc(v, 0);
917 1.3 augustss }
918 1.3 augustss
919 1.3 augustss void
920 1.60 augustss ukbd_cnpollc(void *v, int on)
921 1.3 augustss {
922 1.6 augustss struct ukbd_softc *sc = v;
923 1.52 augustss usbd_device_handle dev;
924 1.6 augustss
925 1.19 augustss DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
926 1.6 augustss
927 1.75 augustss usbd_interface2device_handle(sc->sc_hdev.sc_parent->sc_iface, &dev);
928 1.84 augustss if (on) {
929 1.84 augustss sc->sc_spl = splusb();
930 1.84 augustss pollenter++;
931 1.84 augustss } else {
932 1.84 augustss splx(sc->sc_spl);
933 1.84 augustss pollenter--;
934 1.84 augustss }
935 1.52 augustss usbd_set_polling(dev, on);
936 1.3 augustss }
937 1.18 augustss
938 1.18 augustss int
939 1.60 augustss ukbd_cnattach(void)
940 1.18 augustss {
941 1.18 augustss
942 1.31 thorpej /*
943 1.31 thorpej * XXX USB requires too many parts of the kernel to be running
944 1.31 thorpej * XXX in order to work, so we can't do much for the console
945 1.31 thorpej * XXX keyboard until autconfiguration has run its course.
946 1.31 thorpej */
947 1.31 thorpej ukbd_is_console = 1;
948 1.18 augustss return (0);
949 1.75 augustss }
950 1.75 augustss
951 1.75 augustss const char *
952 1.75 augustss ukbd_parse_desc(struct ukbd_softc *sc)
953 1.75 augustss {
954 1.75 augustss struct hid_data *d;
955 1.75 augustss struct hid_item h;
956 1.75 augustss int size;
957 1.75 augustss void *desc;
958 1.75 augustss int imod;
959 1.75 augustss
960 1.75 augustss uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size);
961 1.75 augustss imod = 0;
962 1.75 augustss sc->sc_nkeycode = 0;
963 1.75 augustss d = hid_start_parse(desc, size, hid_input);
964 1.75 augustss while (hid_get_item(d, &h)) {
965 1.75 augustss /*printf("ukbd: id=%d kind=%d usage=0x%x flags=0x%x pos=%d size=%d cnt=%d\n",
966 1.75 augustss h.report_ID, h.kind, h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count);*/
967 1.109 phx
968 1.109 phx /* Check for special Apple notebook FN key */
969 1.109 phx if (HID_GET_USAGE_PAGE(h.usage) == 0x00ff &&
970 1.109 phx HID_GET_USAGE(h.usage) == 0x0003 &&
971 1.109 phx h.kind == hid_input && (h.flags & HIO_VARIABLE)) {
972 1.109 phx sc->sc_flags |= FLAG_APPLE_FN;
973 1.109 phx sc->sc_apple_fn = h.loc;
974 1.109 phx }
975 1.109 phx
976 1.75 augustss if (h.kind != hid_input || (h.flags & HIO_CONST) ||
977 1.75 augustss HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
978 1.75 augustss h.report_ID != sc->sc_hdev.sc_report_id)
979 1.75 augustss continue;
980 1.79 augustss DPRINTF(("ukbd: imod=%d usage=0x%x flags=0x%x pos=%d size=%d "
981 1.79 augustss "cnt=%d\n", imod,
982 1.79 augustss h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count));
983 1.75 augustss if (h.flags & HIO_VARIABLE) {
984 1.80 augustss if (h.loc.size != 1)
985 1.80 augustss return ("bad modifier size");
986 1.75 augustss /* Single item */
987 1.75 augustss if (imod < MAXMOD) {
988 1.75 augustss sc->sc_modloc[imod] = h.loc;
989 1.75 augustss sc->sc_mods[imod].mask = 1 << imod;
990 1.75 augustss sc->sc_mods[imod].key = HID_GET_USAGE(h.usage);
991 1.75 augustss imod++;
992 1.75 augustss } else
993 1.75 augustss return ("too many modifier keys");
994 1.75 augustss } else {
995 1.75 augustss /* Array */
996 1.75 augustss if (h.loc.size != 8)
997 1.75 augustss return ("key code size != 8");
998 1.75 augustss if (h.loc.count > MAXKEYCODE)
999 1.75 augustss return ("too many key codes");
1000 1.75 augustss if (h.loc.pos % 8 != 0)
1001 1.75 augustss return ("key codes not on byte boundary");
1002 1.75 augustss if (sc->sc_nkeycode != 0)
1003 1.75 augustss return ("multiple key code arrays\n");
1004 1.75 augustss sc->sc_keycodeloc = h.loc;
1005 1.75 augustss sc->sc_nkeycode = h.loc.count;
1006 1.75 augustss }
1007 1.75 augustss }
1008 1.75 augustss sc->sc_nmod = imod;
1009 1.75 augustss hid_end_parse(d);
1010 1.75 augustss
1011 1.75 augustss hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_NUM_LOCK),
1012 1.75 augustss sc->sc_hdev.sc_report_id, hid_output, &sc->sc_numloc, NULL);
1013 1.75 augustss hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_CAPS_LOCK),
1014 1.75 augustss sc->sc_hdev.sc_report_id, hid_output, &sc->sc_capsloc, NULL);
1015 1.75 augustss hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_SCROLL_LOCK),
1016 1.75 augustss sc->sc_hdev.sc_report_id, hid_output, &sc->sc_scroloc, NULL);
1017 1.75 augustss
1018 1.75 augustss return (NULL);
1019 1.18 augustss }
1020