xref: /openbsd/sys/dev/usb/ukbd.c (revision 4bdff4be)
1 /*	$OpenBSD: ukbd.c,v 1.89 2023/12/05 20:49:31 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 const struct usb_devno ukbd_never_console[] = {
184 	/* Apple HID-proxy is always detected before any real USB keyboard */
185 	{ USB_VENDOR_APPLE, USB_PRODUCT_APPLE_BLUETOOTH_HCI },
186 	/* ugold(4) devices, which also present themselves as ukbd */
187 	{ USB_VENDOR_MICRODIA, USB_PRODUCT_MICRODIA_TEMPER },
188 	{ USB_VENDOR_MICRODIA, USB_PRODUCT_MICRODIA_TEMPERHUM },
189 	{ USB_VENDOR_PCSENSORS, USB_PRODUCT_PCSENSORS_TEMPER },
190 	{ USB_VENDOR_RDING, USB_PRODUCT_RDING_TEMPER },
191 	{ USB_VENDOR_WCH2, USB_PRODUCT_WCH2_TEMPER },
192 };
193 
194 int
195 ukbd_match(struct device *parent, void *match, void *aux)
196 {
197 	struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)aux;
198 	int size;
199 	void *desc;
200 
201 	if (UHIDEV_CLAIM_MULTIPLE_REPORTID(uha))
202 		return (UMATCH_NONE);
203 
204 	uhidev_get_report_desc(uha->parent, &desc, &size);
205 	if (!hid_is_collection(desc, size, uha->reportid,
206 	    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD)))
207 		return (UMATCH_NONE);
208 
209 	return (UMATCH_IFACECLASS);
210 }
211 
212 void
213 ukbd_attach(struct device *parent, struct device *self, void *aux)
214 {
215 	struct ukbd_softc *sc = (struct ukbd_softc *)self;
216 	struct hidkbd *kbd = &sc->sc_kbd;
217 	struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)aux;
218 	struct usb_hid_descriptor *hid;
219 	u_int32_t quirks, qflags = 0;
220 	int dlen, repid;
221 	int console = 1;
222 	void *desc;
223 	kbd_t layout = (kbd_t)-1;
224 
225 	sc->sc_hdev.sc_intr = ukbd_intr;
226 	sc->sc_hdev.sc_parent = uha->parent;
227 	sc->sc_hdev.sc_udev = uha->uaa->device;
228 	sc->sc_hdev.sc_report_id = uha->reportid;
229 
230 	usbd_set_idle(uha->parent->sc_udev, uha->parent->sc_ifaceno, 0, 0);
231 
232 	uhidev_get_report_desc(uha->parent, &desc, &dlen);
233 	repid = uha->reportid;
234 	sc->sc_hdev.sc_isize = hid_report_size(desc, dlen, hid_input, repid);
235 	sc->sc_hdev.sc_osize = hid_report_size(desc, dlen, hid_output, repid);
236 	sc->sc_hdev.sc_fsize = hid_report_size(desc, dlen, hid_feature, repid);
237 
238 	 /*
239 	  * Do not allow unwanted devices to claim the console.
240 	  */
241 	if (usb_lookup(ukbd_never_console, uha->uaa->vendor, uha->uaa->product))
242 		console = 0;
243 
244 	quirks = usbd_get_quirks(sc->sc_hdev.sc_udev)->uq_flags;
245 	if (quirks & UQ_SPUR_BUT_UP)
246 		qflags |= HIDKBD_SPUR_BUT_UP;
247 
248 	if (hidkbd_attach(self, kbd, console, qflags, repid, desc, dlen) != 0)
249 		return;
250 
251 	if (uha->uaa->vendor == USB_VENDOR_APPLE) {
252 		if (hid_locate(desc, dlen, HID_USAGE2(HUP_APPLE, HUG_FN_KEY),
253 		    uha->reportid, hid_input, &kbd->sc_fn, &qflags)) {
254 			if (qflags & HIO_VARIABLE) {
255 				switch (uha->uaa->product) {
256 				case USB_PRODUCT_APPLE_FOUNTAIN_ISO:
257 				case USB_PRODUCT_APPLE_GEYSER_ISO:
258 				case USB_PRODUCT_APPLE_GEYSER3_ISO:
259 				case USB_PRODUCT_APPLE_WELLSPRING6_ISO:
260 				case USB_PRODUCT_APPLE_WELLSPRING8_ISO:
261 					kbd->sc_munge = hidkbd_apple_iso_munge;
262 					break;
263 				case USB_PRODUCT_APPLE_WELLSPRING_ISO:
264 				case USB_PRODUCT_APPLE_WELLSPRING4_ISO:
265 				case USB_PRODUCT_APPLE_WELLSPRING4A_ISO:
266 					kbd->sc_munge = hidkbd_apple_iso_mba_munge;
267 					break;
268 				case USB_PRODUCT_APPLE_WELLSPRING_ANSI:
269 				case USB_PRODUCT_APPLE_WELLSPRING_JIS:
270 				case USB_PRODUCT_APPLE_WELLSPRING4_ANSI:
271 				case USB_PRODUCT_APPLE_WELLSPRING4_JIS:
272 				case USB_PRODUCT_APPLE_WELLSPRING4A_ANSI:
273 				case USB_PRODUCT_APPLE_WELLSPRING4A_JIS:
274 					kbd->sc_munge = hidkbd_apple_mba_munge;
275 					break;
276 				default:
277 					kbd->sc_munge = hidkbd_apple_munge;
278 					break;
279 				}
280 			}
281 		}
282 	}
283 
284 	if (uha->uaa->vendor == USB_VENDOR_TOPRE &&
285 	    uha->uaa->product == USB_PRODUCT_TOPRE_HHKB) {
286 		/* ignore country code on purpose */
287 	} else {
288 		usb_interface_descriptor_t *id;
289 
290 		id = usbd_get_interface_descriptor(uha->uaa->iface);
291 		hid = usbd_get_hid_descriptor(uha->uaa->device, id);
292 
293 		if (hid->bCountryCode <= HCC_MAX)
294 			layout = ukbd_countrylayout[hid->bCountryCode];
295 #ifdef DIAGNOSTIC
296 		if (hid->bCountryCode != 0)
297 			printf(", country code %d", hid->bCountryCode);
298 #endif
299 	}
300 	if (layout == (kbd_t)-1) {
301 #ifdef UKBD_LAYOUT
302 		layout = UKBD_LAYOUT;
303 #else
304 		layout = KB_US | KB_DEFAULT;
305 #endif
306 	}
307 
308 	printf("\n");
309 
310 #ifdef __loongson__
311 	if (uha->uaa->vendor == USB_VENDOR_CYPRESS &&
312 	    uha->uaa->product == USB_PRODUCT_CYPRESS_LPRDK)
313 		kbd->sc_munge = ukbd_gdium_munge;
314 #endif
315 
316 	if (kbd->sc_console_keyboard) {
317 		extern struct wskbd_mapdata ukbd_keymapdata;
318 
319 		DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc));
320 		ukbd_keymapdata.layout = layout;
321 		wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
322 		ukbd_enable(sc, 1);
323 	}
324 
325 	/* Flash the leds; no real purpose, just shows we're alive. */
326 	ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM |
327 		          WSKBD_LED_CAPS | WSKBD_LED_COMPOSE);
328 	usbd_delay_ms(sc->sc_hdev.sc_udev, 400);
329 	ukbd_set_leds(sc, 0);
330 
331 	hidkbd_attach_wskbd(kbd, layout, &ukbd_accessops);
332 
333 #ifdef DDB
334 	timeout_set(&sc->sc_ddb, ukbd_db_enter, sc);
335 #endif
336 }
337 
338 int
339 ukbd_detach(struct device *self, int flags)
340 {
341 	struct ukbd_softc *sc = (struct ukbd_softc *)self;
342 	struct hidkbd *kbd = &sc->sc_kbd;
343 	int rv;
344 
345 	rv = hidkbd_detach(kbd, flags);
346 
347 	/* The console keyboard does not get a disable call, so check pipe. */
348 	if (sc->sc_hdev.sc_state & UHIDEV_OPEN)
349 		uhidev_close(&sc->sc_hdev);
350 
351 	return (rv);
352 }
353 
354 void
355 ukbd_intr(struct uhidev *addr, void *ibuf, u_int len)
356 {
357 	struct ukbd_softc *sc = (struct ukbd_softc *)addr;
358 	struct hidkbd *kbd = &sc->sc_kbd;
359 
360 	if (kbd->sc_enabled != 0)
361 		hidkbd_input(kbd, (uint8_t *)ibuf, len);
362 }
363 
364 int
365 ukbd_enable(void *v, int on)
366 {
367 	struct ukbd_softc *sc = v;
368 	struct hidkbd *kbd = &sc->sc_kbd;
369 	int rv;
370 
371 	if (on && usbd_is_dying(sc->sc_hdev.sc_udev))
372 		return EIO;
373 
374 	if ((rv = hidkbd_enable(kbd, on)) != 0)
375 		return rv;
376 
377 	if (on) {
378 		return uhidev_open(&sc->sc_hdev);
379 	} else {
380 		uhidev_close(&sc->sc_hdev);
381 		return 0;
382 	}
383 }
384 
385 void
386 ukbd_set_leds(void *v, int leds)
387 {
388 	struct ukbd_softc *sc = v;
389 	struct hidkbd *kbd = &sc->sc_kbd;
390 	u_int8_t res;
391 
392 	if (usbd_is_dying(sc->sc_hdev.sc_udev))
393 		return;
394 
395 	if (sc->sc_ledsize && hidkbd_set_leds(kbd, leds, &res) != 0)
396 		uhidev_set_report_async(sc->sc_hdev.sc_parent,
397 		    UHID_OUTPUT_REPORT, sc->sc_hdev.sc_report_id, &res, 1);
398 }
399 
400 int
401 ukbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
402 {
403 	struct ukbd_softc *sc = v;
404 	struct hidkbd *kbd = &sc->sc_kbd;
405 	int rc;
406 
407 	switch (cmd) {
408 	case WSKBDIO_GTYPE:
409 		*(int *)data = WSKBD_TYPE_USB;
410 		return (0);
411 	case WSKBDIO_SETLEDS:
412 		ukbd_set_leds(v, *(int *)data);
413 		return (0);
414 	default:
415 		rc = uhidev_ioctl(&sc->sc_hdev, cmd, data, flag, p);
416 		if (rc != -1)
417 			return rc;
418 		else
419 			return hidkbd_ioctl(kbd, cmd, data, flag, p);
420 	}
421 }
422 
423 /* Console interface. */
424 void
425 ukbd_cngetc(void *v, u_int *type, int *data)
426 {
427 	struct ukbd_softc *sc = v;
428 	struct hidkbd *kbd = &sc->sc_kbd;
429 
430 	DPRINTFN(0,("ukbd_cngetc: enter\n"));
431 	kbd->sc_polling = 1;
432 	while (kbd->sc_npollchar <= 0)
433 		usbd_dopoll(sc->sc_hdev.sc_udev);
434 	kbd->sc_polling = 0;
435 	hidkbd_cngetc(kbd, type, data);
436 	DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", *data));
437 }
438 
439 void
440 ukbd_cnpollc(void *v, int on)
441 {
442 	struct ukbd_softc *sc = v;
443 
444 	DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
445 
446 	if (on)
447 		sc->sc_spl = splusb();
448 	else
449 		splx(sc->sc_spl);
450 	usbd_set_polling(sc->sc_hdev.sc_udev, on);
451 }
452 
453 void
454 ukbd_cnbell(void *v, u_int pitch, u_int period, u_int volume)
455 {
456 	hidkbd_bell(pitch, period, volume, 1);
457 }
458 
459 #ifdef DDB
460 void
461 ukbd_debugger(void *v)
462 {
463 	struct ukbd_softc *sc = v;
464 
465 	/*
466 	 * For the console keyboard we can't deliver CTL-ALT-ESC
467 	 * from the interrupt routine.  Doing so would start
468 	 * polling from inside the interrupt routine and that
469 	 * loses bigtime.
470 	 */
471 	timeout_add(&sc->sc_ddb, 1);
472 }
473 
474 void
475 ukbd_db_enter(void *xsc)
476 {
477 	db_enter();
478 }
479 #endif
480 
481 int
482 ukbd_cnattach(void)
483 {
484 	struct ukbd_softc *sc;
485 	int i;
486 
487 	/*
488 	 * XXX USB requires too many parts of the kernel to be running
489 	 * XXX in order to work, so we can't do much for the console
490 	 * XXX keyboard until autoconfiguration has run its course.
491 	 */
492 	hidkbd_is_console = 1;
493 
494 	if (!cold) {
495 		/*
496 		 * When switching console dynamically force all USB keyboards
497 		 * to re-attach and possibly became the 'console' keyboard.
498 		 */
499 		for (i = 0; i < ukbd_cd.cd_ndevs; i++) {
500 			if ((sc = ukbd_cd.cd_devs[i]) != NULL) {
501 				usb_needs_reattach(sc->sc_hdev.sc_udev);
502 				break;
503 			}
504 		}
505 	}
506 
507 	return (0);
508 }
509 
510 #ifdef __loongson__
511 /*
512  * Software Fn- translation for Gdium Liberty keyboard.
513  */
514 #define	GDIUM_FN_CODE	0x82
515 void
516 ukbd_gdium_munge(void *vsc, uint8_t *ibuf, u_int ilen)
517 {
518 	struct ukbd_softc *sc = vsc;
519 	struct hidkbd *kbd = &sc->sc_kbd;
520 	uint8_t *pos, *spos, *epos, xlat;
521 	int fn;
522 
523 	static const struct hidkbd_translation gdium_fn_trans[] = {
524 #ifdef notyet
525 		{ 58, 0 },	/* F1 -> toggle camera */
526 		{ 59, 0 },	/* F2 -> toggle wireless */
527 #endif
528 		{ 60, 127 },	/* F3 -> audio mute */
529 		{ 61, 128 },	/* F4 -> audio raise */
530 		{ 62, 129 },	/* F5 -> audio lower */
531 #ifdef notyet
532 		{ 63, 0 },	/* F6 -> toggle ext. video */
533 		{ 64, 0 },	/* F7 -> toggle mouse */
534 		{ 65, 0 },	/* F8 -> brightness up */
535 		{ 66, 0 },	/* F9 -> brightness down */
536 		{ 67, 0 },	/* F10 -> suspend */
537 		{ 68, 0 },	/* F11 -> user1 */
538 		{ 69, 0 },	/* F12 -> user2 */
539 		{ 70, 0 },	/* print screen -> sysrq */
540 #endif
541 		{ 76, 71 },	/* delete -> scroll lock */
542 		{ 81, 78 },	/* down -> page down */
543 		{ 82, 75 }	/* up -> page up */
544 	};
545 
546 	spos = ibuf + kbd->sc_keycodeloc.pos / 8;
547 	epos = spos + kbd->sc_nkeycode;
548 
549 	/*
550 	 * Check for Fn key being down and remove it from the report.
551 	 */
552 
553 	fn = 0;
554 	for (pos = spos; pos != epos; pos++)
555 		if (*pos == GDIUM_FN_CODE) {
556 			fn = 1;
557 			*pos = 0;
558 			break;
559 		}
560 
561 	/*
562 	 * Rewrite keycodes on the fly to perform Fn-key translation.
563 	 * Keycodes without a translation are passed unaffected.
564 	 */
565 
566 	if (fn != 0)
567 		for (pos = spos; pos != epos; pos++) {
568 			xlat = hidkbd_translate(gdium_fn_trans,
569 			    nitems(gdium_fn_trans), *pos);
570 			if (xlat != 0)
571 				*pos = xlat;
572 		}
573 
574 }
575 #endif
576