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