xref: /freebsd/sys/dev/hid/hsctrl.c (revision 848ee2a3a8b47c9905fc51fefcf60eb371edbb98)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
30 
31 /*
32  * General Desktop/System Controls usage page driver
33  * https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf
34  */
35 
36 #include <sys/param.h>
37 #include <sys/bitstring.h>
38 #include <sys/bus.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/sysctl.h>
42 
43 #include <dev/evdev/input.h>
44 #include <dev/evdev/evdev.h>
45 
46 #include <dev/hid/hid.h>
47 #include <dev/hid/hidbus.h>
48 #include <dev/hid/hidmap.h>
49 
50 #define	HSCTRL_MAP(usage, code)	\
51 	{ HIDMAP_KEY(HUP_GENERIC_DESKTOP, HUG_SYSTEM_##usage, code) }
52 
53 static const struct hidmap_item hsctrl_map[] = {
54 	HSCTRL_MAP(POWER_DOWN,		KEY_POWER),
55 	HSCTRL_MAP(SLEEP,		KEY_SLEEP),
56 	HSCTRL_MAP(WAKEUP,		KEY_WAKEUP),
57 	HSCTRL_MAP(CONTEXT_MENU,	KEY_CONTEXT_MENU),
58 	HSCTRL_MAP(MAIN_MENU,		KEY_MENU),
59 	HSCTRL_MAP(APP_MENU,		KEY_PROG1),
60 	HSCTRL_MAP(MENU_HELP,		KEY_HELP),
61 	HSCTRL_MAP(MENU_EXIT,		KEY_EXIT),
62 	HSCTRL_MAP(MENU_SELECT,		KEY_SELECT),
63 	HSCTRL_MAP(MENU_RIGHT,		KEY_RIGHT),
64 	HSCTRL_MAP(MENU_LEFT,		KEY_LEFT),
65 	HSCTRL_MAP(MENU_UP,		KEY_UP),
66 	HSCTRL_MAP(MENU_DOWN,		KEY_DOWN),
67 	HSCTRL_MAP(POWER_UP,		KEY_POWER2),
68 	HSCTRL_MAP(RESTART,		KEY_RESTART),
69 };
70 
71 static const struct hid_device_id hsctrl_devs[] = {
72 	{ HID_TLC(HUP_GENERIC_DESKTOP, HUG_SYSTEM_CONTROL) },
73 };
74 
75 static int
76 hsctrl_probe(device_t dev)
77 {
78 	return (HIDMAP_PROBE(device_get_softc(dev), dev,
79 	    hsctrl_devs, hsctrl_map, "System Control"));
80 }
81 
82 static int
83 hsctrl_attach(device_t dev)
84 {
85 	return (hidmap_attach(device_get_softc(dev)));
86 }
87 
88 static int
89 hsctrl_detach(device_t dev)
90 {
91 	return (hidmap_detach(device_get_softc(dev)));
92 }
93 
94 static devclass_t hsctrl_devclass;
95 static device_method_t hsctrl_methods[] = {
96 	DEVMETHOD(device_probe,		hsctrl_probe),
97 	DEVMETHOD(device_attach,	hsctrl_attach),
98 	DEVMETHOD(device_detach,	hsctrl_detach),
99 
100 	DEVMETHOD_END
101 };
102 
103 DEFINE_CLASS_0(hsctrl, hsctrl_driver, hsctrl_methods, sizeof(struct hidmap));
104 DRIVER_MODULE(hsctrl, hidbus, hsctrl_driver, hsctrl_devclass, NULL, 0);
105 MODULE_DEPEND(hsctrl, hid, 1, 1, 1);
106 MODULE_DEPEND(hsctrl, hidbus, 1, 1, 1);
107 MODULE_DEPEND(hsctrl, hidmap, 1, 1, 1);
108 MODULE_DEPEND(hsctrl, evdev, 1, 1, 1);
109 MODULE_VERSION(hsctrl, 1);
110 HID_PNP_INFO(hsctrl_devs);
111