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