xref: /freebsd/sys/dev/hid/hsctrl.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020 Vladimir Kondratyev <wulf@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 /*
30  * General Desktop/System Controls usage page driver
31  * https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf
32  */
33 
34 #include <sys/param.h>
35 #include <sys/bitstring.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/sysctl.h>
40 
41 #include <dev/evdev/input.h>
42 #include <dev/evdev/evdev.h>
43 
44 #include <dev/hid/hid.h>
45 #include <dev/hid/hidbus.h>
46 #include <dev/hid/hidmap.h>
47 
48 #define	HSCTRL_MAP(usage, code)	\
49 	{ HIDMAP_KEY(HUP_GENERIC_DESKTOP, HUG_SYSTEM_##usage, code) }
50 
51 static const struct hidmap_item hsctrl_map[] = {
52 	HSCTRL_MAP(POWER_DOWN,		KEY_POWER),
53 	HSCTRL_MAP(SLEEP,		KEY_SLEEP),
54 	HSCTRL_MAP(WAKEUP,		KEY_WAKEUP),
55 	HSCTRL_MAP(CONTEXT_MENU,	KEY_CONTEXT_MENU),
56 	HSCTRL_MAP(MAIN_MENU,		KEY_MENU),
57 	HSCTRL_MAP(APP_MENU,		KEY_PROG1),
58 	HSCTRL_MAP(MENU_HELP,		KEY_HELP),
59 	HSCTRL_MAP(MENU_EXIT,		KEY_EXIT),
60 	HSCTRL_MAP(MENU_SELECT,		KEY_SELECT),
61 	HSCTRL_MAP(MENU_RIGHT,		KEY_RIGHT),
62 	HSCTRL_MAP(MENU_LEFT,		KEY_LEFT),
63 	HSCTRL_MAP(MENU_UP,		KEY_UP),
64 	HSCTRL_MAP(MENU_DOWN,		KEY_DOWN),
65 	HSCTRL_MAP(POWER_UP,		KEY_POWER2),
66 	HSCTRL_MAP(RESTART,		KEY_RESTART),
67 };
68 
69 static const struct hid_device_id hsctrl_devs[] = {
70 	{ HID_TLC(HUP_GENERIC_DESKTOP, HUG_SYSTEM_CONTROL) },
71 };
72 
73 static int
74 hsctrl_probe(device_t dev)
75 {
76 	return (HIDMAP_PROBE(device_get_softc(dev), dev,
77 	    hsctrl_devs, hsctrl_map, "System Control"));
78 }
79 
80 static int
81 hsctrl_attach(device_t dev)
82 {
83 	return (hidmap_attach(device_get_softc(dev)));
84 }
85 
86 static int
87 hsctrl_detach(device_t dev)
88 {
89 	return (hidmap_detach(device_get_softc(dev)));
90 }
91 
92 static device_method_t hsctrl_methods[] = {
93 	DEVMETHOD(device_probe,		hsctrl_probe),
94 	DEVMETHOD(device_attach,	hsctrl_attach),
95 	DEVMETHOD(device_detach,	hsctrl_detach),
96 
97 	DEVMETHOD_END
98 };
99 
100 DEFINE_CLASS_0(hsctrl, hsctrl_driver, hsctrl_methods, sizeof(struct hidmap));
101 DRIVER_MODULE(hsctrl, hidbus, hsctrl_driver, NULL, NULL);
102 MODULE_DEPEND(hsctrl, hid, 1, 1, 1);
103 MODULE_DEPEND(hsctrl, hidbus, 1, 1, 1);
104 MODULE_DEPEND(hsctrl, hidmap, 1, 1, 1);
105 MODULE_DEPEND(hsctrl, evdev, 1, 1, 1);
106 MODULE_VERSION(hsctrl, 1);
107 HID_PNP_INFO(hsctrl_devs);
108