xref: /openbsd-src/sys/dev/usb/ukbd.c (revision fc405d53b73a2d73393cb97f684863d17b583e38)
1 /*	$OpenBSD: ukbd.c,v 1.88 2022/10/04 19:38:20 miod Exp $	*/
2 /*      $NetBSD: ukbd.c,v 1.85 2003/03/11 16:44:00 augustss Exp $        */
3 
4 /*
5  * Copyright (c) 2010 Miodrag Vallat.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 /*
20  * Copyright (c) 1998 The NetBSD Foundation, Inc.
21  * All rights reserved.
22  *
23  * This code is derived from software contributed to The NetBSD Foundation
24  * by Lennart Augustsson (lennart@augustsson.net) at
25  * Carlstedt Research & Technology.
26  *
27  * Redistribution and use in source and binary forms, with or without
28  * modification, are permitted provided that the following conditions
29  * are met:
30  * 1. Redistributions of source code must retain the above copyright
31  *    notice, this list of conditions and the following disclaimer.
32  * 2. Redistributions in binary form must reproduce the above copyright
33  *    notice, this list of conditions and the following disclaimer in the
34  *    documentation and/or other materials provided with the distribution.
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
37  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
38  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
44  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  */
48 
49 /*
50  * HID spec: https://www.usb.org/sites/default/files/hid1_11.pdf
51  */
52 
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/timeout.h>
56 #include <sys/kernel.h>
57 #include <sys/device.h>
58 #include <sys/ioctl.h>
59 
60 #include <machine/bus.h>
61 
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbhid.h>
64 
65 #include <dev/usb/usbdi.h>
66 #include <dev/usb/usbdivar.h> /* needs_reattach() */
67 #include <dev/usb/usbdi_util.h>
68 #include <dev/usb/usbdevs.h>
69 #include <dev/usb/usb_quirks.h>
70 #include <dev/usb/uhidev.h>
71 #include <dev/usb/ukbdvar.h>
72 
73 #include <dev/wscons/wsconsio.h>
74 #include <dev/wscons/wskbdvar.h>
75 #include <dev/wscons/wsksymdef.h>
76 #include <dev/wscons/wsksymvar.h>
77 
78 #include <dev/hid/hidkbdsc.h>
79 
80 #ifdef UKBD_DEBUG
81 #define DPRINTF(x)	do { if (ukbddebug) printf x; } while (0)
82 #define DPRINTFN(n,x)	do { if (ukbddebug>(n)) printf x; } while (0)
83 int	ukbddebug = 0;
84 #else
85 #define DPRINTF(x)
86 #define DPRINTFN(n,x)
87 #endif
88 
89 const kbd_t ukbd_countrylayout[1 + HCC_MAX] = {
90 	(kbd_t)-1,
91 	(kbd_t)-1,	/* arabic */
92 	KB_BE,		/* belgian */
93 	(kbd_t)-1,	/* canadian bilingual */
94 	KB_CF,		/* canadian french */
95 	(kbd_t)-1,	/* czech */
96 	KB_DK,		/* danish */
97 	(kbd_t)-1,	/* finnish */
98 	KB_FR,		/* french */
99 	KB_DE,		/* german */
100 	(kbd_t)-1,	/* greek */
101 	(kbd_t)-1,	/* hebrew */
102 	KB_HU,		/* hungary */
103 	(kbd_t)-1,	/* international (iso) */
104 	KB_IT,		/* italian */
105 	KB_JP,		/* japanese (katakana) */
106 	(kbd_t)-1,	/* korean */
107 	KB_LA,		/* latin american */
108 	(kbd_t)-1,	/* netherlands/dutch */
109 	KB_NO,		/* norwegian */
110 	(kbd_t)-1,	/* persian (farsi) */
111 	KB_PL,		/* polish */
112 	KB_PT,		/* portuguese */
113 	KB_RU,		/* russian */
114 	(kbd_t)-1,	/* slovakia */
115 	KB_ES,		/* spanish */
116 	KB_SV,		/* swedish */
117 	KB_SF,		/* swiss french */
118 	KB_SG,		/* swiss german */
119 	(kbd_t)-1,	/* switzerland */
120 	(kbd_t)-1,	/* taiwan */
121 	KB_TR,		/* turkish Q */
122 	KB_UK,		/* uk */
123 	KB_US,		/* us */
124 	(kbd_t)-1,	/* yugoslavia */
125 	(kbd_t)-1	/* turkish F */
126 };
127 
128 struct ukbd_softc {
129 	struct uhidev		sc_hdev;
130 #define sc_ledsize		sc_hdev.sc_osize
131 
132 	struct hidkbd		sc_kbd;
133 	int			sc_spl;
134 
135 #ifdef DDB
136 	struct timeout		sc_ddb;	/* for entering DDB */
137 #endif
138 };
139 
140 void	ukbd_cngetc(void *, u_int *, int *);
141 void	ukbd_cnpollc(void *, int);
142 void	ukbd_cnbell(void *, u_int, u_int, u_int);
143 void	ukbd_debugger(void *);
144 
145 const struct wskbd_consops ukbd_consops = {
146 	ukbd_cngetc,
147 	ukbd_cnpollc,
148 	ukbd_cnbell,
149 #ifdef DDB
150 	ukbd_debugger,
151 #endif
152 };
153 
154 void	ukbd_intr(struct uhidev *addr, void *ibuf, u_int len);
155 
156 void	ukbd_db_enter(void *);
157 int	ukbd_enable(void *, int);
158 void	ukbd_set_leds(void *, int);
159 int	ukbd_ioctl(void *, u_long, caddr_t, int, struct proc *);
160 
161 const struct wskbd_accessops ukbd_accessops = {
162 	ukbd_enable,
163 	ukbd_set_leds,
164 	ukbd_ioctl,
165 };
166 
167 int	ukbd_match(struct device *, void *, void *);
168 void	ukbd_attach(struct device *, struct device *, void *);
169 int	ukbd_detach(struct device *, int);
170 
171 struct cfdriver ukbd_cd = {
172 	NULL, "ukbd", DV_DULL
173 };
174 
175 const struct cfattach ukbd_ca = {
176 	sizeof(struct ukbd_softc), ukbd_match, ukbd_attach, ukbd_detach
177 };
178 
179 #ifdef __loongson__
180 void	ukbd_gdium_munge(void *, uint8_t *, u_int);
181 #endif
182 
183 int
184 ukbd_match(struct device *parent, void *match, void *aux)
185 {
186 	struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)aux;
187 	int size;
188 	void *desc;
189 
190 	if (UHIDEV_CLAIM_MULTIPLE_REPORTID(uha))
191 		return (UMATCH_NONE);
192 
193 	uhidev_get_report_desc(uha->parent, &desc, &size);
194 	if (!hid_is_collection(desc, size, uha->reportid,
195 	    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD)))
196 		return (UMATCH_NONE);
197 
198 	return (UMATCH_IFACECLASS);
199 }
200 
201 void
202 ukbd_attach(struct device *parent, struct device *self, void *aux)
203 {
204 	struct ukbd_softc *sc = (struct ukbd_softc *)self;
205 	struct hidkbd *kbd = &sc->sc_kbd;
206 	struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)aux;
207 	struct usb_hid_descriptor *hid;
208 	u_int32_t quirks, qflags = 0;
209 	int dlen, repid;
210 	int console = 1;
211 	void *desc;
212 	kbd_t layout = (kbd_t)-1;
213 
214 	sc->sc_hdev.sc_intr = ukbd_intr;
215 	sc->sc_hdev.sc_parent = uha->parent;
216 	sc->sc_hdev.sc_udev = uha->uaa->device;
217 	sc->sc_hdev.sc_report_id = uha->reportid;
218 
219 	usbd_set_idle(uha->parent->sc_udev, uha->parent->sc_ifaceno, 0, 0);
220 
221 	uhidev_get_report_desc(uha->parent, &desc, &dlen);
222 	repid = uha->reportid;
223 	sc->sc_hdev.sc_isize = hid_report_size(desc, dlen, hid_input, repid);
224 	sc->sc_hdev.sc_osize = hid_report_size(desc, dlen, hid_output, repid);
225 	sc->sc_hdev.sc_fsize = hid_report_size(desc, dlen, hid_feature, repid);
226 
227 	 /*
228 	  * Since the HID-Proxy is always detected before any
229 	  * real keyboard, do not let it grab the console.
230 	  */
231 	if (uha->uaa->vendor == USB_VENDOR_APPLE &&
232 	    uha->uaa->product == USB_PRODUCT_APPLE_BLUETOOTH_HCI)
233 		console = 0;
234 
235 	quirks = usbd_get_quirks(sc->sc_hdev.sc_udev)->uq_flags;
236 	if (quirks & UQ_SPUR_BUT_UP)
237 		qflags |= HIDKBD_SPUR_BUT_UP;
238 
239 	if (hidkbd_attach(self, kbd, console, qflags, repid, desc, dlen) != 0)
240 		return;
241 
242 	if (uha->uaa->vendor == USB_VENDOR_APPLE) {
243 		if (hid_locate(desc, dlen, HID_USAGE2(HUP_APPLE, HUG_FN_KEY),
244 		    uha->reportid, hid_input, &kbd->sc_fn, &qflags)) {
245 			if (qflags & HIO_VARIABLE) {
246 				switch (uha->uaa->product) {
247 				case USB_PRODUCT_APPLE_FOUNTAIN_ISO:
248 				case USB_PRODUCT_APPLE_GEYSER_ISO:
249 				case USB_PRODUCT_APPLE_GEYSER3_ISO:
250 				case USB_PRODUCT_APPLE_WELLSPRING6_ISO:
251 				case USB_PRODUCT_APPLE_WELLSPRING8_ISO:
252 					kbd->sc_munge = hidkbd_apple_iso_munge;
253 					break;
254 				case USB_PRODUCT_APPLE_WELLSPRING_ISO:
255 				case USB_PRODUCT_APPLE_WELLSPRING4_ISO:
256 				case USB_PRODUCT_APPLE_WELLSPRING4A_ISO:
257 					kbd->sc_munge = hidkbd_apple_iso_mba_munge;
258 					break;
259 				case USB_PRODUCT_APPLE_WELLSPRING_ANSI:
260 				case USB_PRODUCT_APPLE_WELLSPRING_JIS:
261 				case USB_PRODUCT_APPLE_WELLSPRING4_ANSI:
262 				case USB_PRODUCT_APPLE_WELLSPRING4_JIS:
263 				case USB_PRODUCT_APPLE_WELLSPRING4A_ANSI:
264 				case USB_PRODUCT_APPLE_WELLSPRING4A_JIS:
265 					kbd->sc_munge = hidkbd_apple_mba_munge;
266 					break;
267 				default:
268 					kbd->sc_munge = hidkbd_apple_munge;
269 					break;
270 				}
271 			}
272 		}
273 	}
274 
275 	if (uha->uaa->vendor == USB_VENDOR_TOPRE &&
276 	    uha->uaa->product == USB_PRODUCT_TOPRE_HHKB) {
277 		/* ignore country code on purpose */
278 	} else {
279 		usb_interface_descriptor_t *id;
280 
281 		id = usbd_get_interface_descriptor(uha->uaa->iface);
282 		hid = usbd_get_hid_descriptor(uha->uaa->device, id);
283 
284 		if (hid->bCountryCode <= HCC_MAX)
285 			layout = ukbd_countrylayout[hid->bCountryCode];
286 #ifdef DIAGNOSTIC
287 		if (hid->bCountryCode != 0)
288 			printf(", country code %d", hid->bCountryCode);
289 #endif
290 	}
291 	if (layout == (kbd_t)-1) {
292 #ifdef UKBD_LAYOUT
293 		layout = UKBD_LAYOUT;
294 #else
295 		layout = KB_US | KB_DEFAULT;
296 #endif
297 	}
298 
299 	printf("\n");
300 
301 #ifdef __loongson__
302 	if (uha->uaa->vendor == USB_VENDOR_CYPRESS &&
303 	    uha->uaa->product == USB_PRODUCT_CYPRESS_LPRDK)
304 		kbd->sc_munge = ukbd_gdium_munge;
305 #endif
306 
307 	if (kbd->sc_console_keyboard) {
308 		extern struct wskbd_mapdata ukbd_keymapdata;
309 
310 		DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc));
311 		ukbd_keymapdata.layout = layout;
312 		wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
313 		ukbd_enable(sc, 1);
314 	}
315 
316 	/* Flash the leds; no real purpose, just shows we're alive. */
317 	ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM |
318 		          WSKBD_LED_CAPS | WSKBD_LED_COMPOSE);
319 	usbd_delay_ms(sc->sc_hdev.sc_udev, 400);
320 	ukbd_set_leds(sc, 0);
321 
322 	hidkbd_attach_wskbd(kbd, layout, &ukbd_accessops);
323 
324 #ifdef DDB
325 	timeout_set(&sc->sc_ddb, ukbd_db_enter, sc);
326 #endif
327 }
328 
329 int
330 ukbd_detach(struct device *self, int flags)
331 {
332 	struct ukbd_softc *sc = (struct ukbd_softc *)self;
333 	struct hidkbd *kbd = &sc->sc_kbd;
334 	int rv;
335 
336 	rv = hidkbd_detach(kbd, flags);
337 
338 	/* The console keyboard does not get a disable call, so check pipe. */
339 	if (sc->sc_hdev.sc_state & UHIDEV_OPEN)
340 		uhidev_close(&sc->sc_hdev);
341 
342 	return (rv);
343 }
344 
345 void
346 ukbd_intr(struct uhidev *addr, void *ibuf, u_int len)
347 {
348 	struct ukbd_softc *sc = (struct ukbd_softc *)addr;
349 	struct hidkbd *kbd = &sc->sc_kbd;
350 
351 	if (kbd->sc_enabled != 0)
352 		hidkbd_input(kbd, (uint8_t *)ibuf, len);
353 }
354 
355 int
356 ukbd_enable(void *v, int on)
357 {
358 	struct ukbd_softc *sc = v;
359 	struct hidkbd *kbd = &sc->sc_kbd;
360 	int rv;
361 
362 	if (on && usbd_is_dying(sc->sc_hdev.sc_udev))
363 		return EIO;
364 
365 	if ((rv = hidkbd_enable(kbd, on)) != 0)
366 		return rv;
367 
368 	if (on) {
369 		return uhidev_open(&sc->sc_hdev);
370 	} else {
371 		uhidev_close(&sc->sc_hdev);
372 		return 0;
373 	}
374 }
375 
376 void
377 ukbd_set_leds(void *v, int leds)
378 {
379 	struct ukbd_softc *sc = v;
380 	struct hidkbd *kbd = &sc->sc_kbd;
381 	u_int8_t res;
382 
383 	if (usbd_is_dying(sc->sc_hdev.sc_udev))
384 		return;
385 
386 	if (sc->sc_ledsize && hidkbd_set_leds(kbd, leds, &res) != 0)
387 		uhidev_set_report_async(sc->sc_hdev.sc_parent,
388 		    UHID_OUTPUT_REPORT, sc->sc_hdev.sc_report_id, &res, 1);
389 }
390 
391 int
392 ukbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
393 {
394 	struct ukbd_softc *sc = v;
395 	struct hidkbd *kbd = &sc->sc_kbd;
396 	int rc;
397 
398 	switch (cmd) {
399 	case WSKBDIO_GTYPE:
400 		*(int *)data = WSKBD_TYPE_USB;
401 		return (0);
402 	case WSKBDIO_SETLEDS:
403 		ukbd_set_leds(v, *(int *)data);
404 		return (0);
405 	default:
406 		rc = uhidev_ioctl(&sc->sc_hdev, cmd, data, flag, p);
407 		if (rc != -1)
408 			return rc;
409 		else
410 			return hidkbd_ioctl(kbd, cmd, data, flag, p);
411 	}
412 }
413 
414 /* Console interface. */
415 void
416 ukbd_cngetc(void *v, u_int *type, int *data)
417 {
418 	struct ukbd_softc *sc = v;
419 	struct hidkbd *kbd = &sc->sc_kbd;
420 
421 	DPRINTFN(0,("ukbd_cngetc: enter\n"));
422 	kbd->sc_polling = 1;
423 	while (kbd->sc_npollchar <= 0)
424 		usbd_dopoll(sc->sc_hdev.sc_udev);
425 	kbd->sc_polling = 0;
426 	hidkbd_cngetc(kbd, type, data);
427 	DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", *data));
428 }
429 
430 void
431 ukbd_cnpollc(void *v, int on)
432 {
433 	struct ukbd_softc *sc = v;
434 
435 	DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
436 
437 	if (on)
438 		sc->sc_spl = splusb();
439 	else
440 		splx(sc->sc_spl);
441 	usbd_set_polling(sc->sc_hdev.sc_udev, on);
442 }
443 
444 void
445 ukbd_cnbell(void *v, u_int pitch, u_int period, u_int volume)
446 {
447 	hidkbd_bell(pitch, period, volume, 1);
448 }
449 
450 #ifdef DDB
451 void
452 ukbd_debugger(void *v)
453 {
454 	struct ukbd_softc *sc = v;
455 
456 	/*
457 	 * For the console keyboard we can't deliver CTL-ALT-ESC
458 	 * from the interrupt routine.  Doing so would start
459 	 * polling from inside the interrupt routine and that
460 	 * loses bigtime.
461 	 */
462 	timeout_add(&sc->sc_ddb, 1);
463 }
464 
465 void
466 ukbd_db_enter(void *xsc)
467 {
468 	db_enter();
469 }
470 #endif
471 
472 int
473 ukbd_cnattach(void)
474 {
475 	struct ukbd_softc *sc;
476 	int i;
477 
478 	/*
479 	 * XXX USB requires too many parts of the kernel to be running
480 	 * XXX in order to work, so we can't do much for the console
481 	 * XXX keyboard until autoconfiguration has run its course.
482 	 */
483 	hidkbd_is_console = 1;
484 
485 	if (!cold) {
486 		/*
487 		 * When switching console dynamically force all USB keyboards
488 		 * to re-attach and possibly became the 'console' keyboard.
489 		 */
490 		for (i = 0; i < ukbd_cd.cd_ndevs; i++) {
491 			if ((sc = ukbd_cd.cd_devs[i]) != NULL) {
492 				usb_needs_reattach(sc->sc_hdev.sc_udev);
493 				break;
494 			}
495 		}
496 	}
497 
498 	return (0);
499 }
500 
501 #ifdef __loongson__
502 /*
503  * Software Fn- translation for Gdium Liberty keyboard.
504  */
505 #define	GDIUM_FN_CODE	0x82
506 void
507 ukbd_gdium_munge(void *vsc, uint8_t *ibuf, u_int ilen)
508 {
509 	struct ukbd_softc *sc = vsc;
510 	struct hidkbd *kbd = &sc->sc_kbd;
511 	uint8_t *pos, *spos, *epos, xlat;
512 	int fn;
513 
514 	static const struct hidkbd_translation gdium_fn_trans[] = {
515 #ifdef notyet
516 		{ 58, 0 },	/* F1 -> toggle camera */
517 		{ 59, 0 },	/* F2 -> toggle wireless */
518 #endif
519 		{ 60, 127 },	/* F3 -> audio mute */
520 		{ 61, 128 },	/* F4 -> audio raise */
521 		{ 62, 129 },	/* F5 -> audio lower */
522 #ifdef notyet
523 		{ 63, 0 },	/* F6 -> toggle ext. video */
524 		{ 64, 0 },	/* F7 -> toggle mouse */
525 		{ 65, 0 },	/* F8 -> brightness up */
526 		{ 66, 0 },	/* F9 -> brightness down */
527 		{ 67, 0 },	/* F10 -> suspend */
528 		{ 68, 0 },	/* F11 -> user1 */
529 		{ 69, 0 },	/* F12 -> user2 */
530 		{ 70, 0 },	/* print screen -> sysrq */
531 #endif
532 		{ 76, 71 },	/* delete -> scroll lock */
533 		{ 81, 78 },	/* down -> page down */
534 		{ 82, 75 }	/* up -> page up */
535 	};
536 
537 	spos = ibuf + kbd->sc_keycodeloc.pos / 8;
538 	epos = spos + kbd->sc_nkeycode;
539 
540 	/*
541 	 * Check for Fn key being down and remove it from the report.
542 	 */
543 
544 	fn = 0;
545 	for (pos = spos; pos != epos; pos++)
546 		if (*pos == GDIUM_FN_CODE) {
547 			fn = 1;
548 			*pos = 0;
549 			break;
550 		}
551 
552 	/*
553 	 * Rewrite keycodes on the fly to perform Fn-key translation.
554 	 * Keycodes without a translation are passed unaffected.
555 	 */
556 
557 	if (fn != 0)
558 		for (pos = spos; pos != epos; pos++) {
559 			xlat = hidkbd_translate(gdium_fn_trans,
560 			    nitems(gdium_fn_trans), *pos);
561 			if (xlat != 0)
562 				*pos = xlat;
563 		}
564 
565 }
566 #endif
567