Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  WMI hotkeys support for Dell All-In-One series
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/input/sparse-keymap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) MODULE_DESCRIPTION("WMI hotkeys driver for Dell All-In-One series");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define EVENT_GUID1 "284A0E6B-380E-472A-921F-E52786257FB4"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define EVENT_GUID2 "02314822-307C-4F66-BF0E-48AEAEB26CC8"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) struct dell_wmi_event {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	u16	length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	/* 0x000: A hot key pressed or an event occurred
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	 * 0x00F: A sequence of hot keys are pressed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	u16	type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	u16	event[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static const char *dell_wmi_aio_guids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	EVENT_GUID1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	EVENT_GUID2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) MODULE_ALIAS("wmi:"EVENT_GUID1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) MODULE_ALIAS("wmi:"EVENT_GUID2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) static const struct key_entry dell_wmi_aio_keymap[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	{ KE_KEY, 0xc0, { KEY_VOLUMEUP } },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	{ KE_KEY, 0xc1, { KEY_VOLUMEDOWN } },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	{ KE_KEY, 0xe030, { KEY_VOLUMEUP } },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	{ KE_KEY, 0xe02e, { KEY_VOLUMEDOWN } },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	{ KE_KEY, 0xe020, { KEY_MUTE } },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	{ KE_KEY, 0xe027, { KEY_DISPLAYTOGGLE } },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	{ KE_KEY, 0xe006, { KEY_BRIGHTNESSUP } },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	{ KE_KEY, 0xe005, { KEY_BRIGHTNESSDOWN } },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	{ KE_KEY, 0xe00b, { KEY_SWITCHVIDEOMODE } },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	{ KE_END, 0 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static struct input_dev *dell_wmi_aio_input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * The new WMI event data format will follow the dell_wmi_event structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * So, we will check if the buffer matches the format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static bool dell_wmi_aio_event_check(u8 *buffer, int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct dell_wmi_event *event = (struct dell_wmi_event *)buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (event == NULL || length < 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	if ((event->type == 0 || event->type == 0xf) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			event->length >= 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static void dell_wmi_aio_notify(u32 value, void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	union acpi_object *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	struct dell_wmi_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	status = wmi_get_event_data(value, &response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (status != AE_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		pr_info("bad event status 0x%x\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	obj = (union acpi_object *)response.pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	if (obj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		unsigned int scancode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		switch (obj->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		case ACPI_TYPE_INTEGER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			/* Most All-In-One correctly return integer scancode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			scancode = obj->integer.value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			sparse_keymap_report_event(dell_wmi_aio_input_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 				scancode, 1, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		case ACPI_TYPE_BUFFER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			if (dell_wmi_aio_event_check(obj->buffer.pointer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 						obj->buffer.length)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 				event = (struct dell_wmi_event *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 					obj->buffer.pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 				scancode = event->event[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 				/* Broken machines return the scancode in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 				   buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 				if (obj->buffer.pointer &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 						obj->buffer.length > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 					scancode = obj->buffer.pointer[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			if (scancode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 				sparse_keymap_report_event(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 					dell_wmi_aio_input_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 					scancode, 1, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	kfree(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static int __init dell_wmi_aio_input_setup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	dell_wmi_aio_input_dev = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (!dell_wmi_aio_input_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	dell_wmi_aio_input_dev->name = "Dell AIO WMI hotkeys";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	dell_wmi_aio_input_dev->phys = "wmi/input0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	dell_wmi_aio_input_dev->id.bustype = BUS_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	err = sparse_keymap_setup(dell_wmi_aio_input_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			dell_wmi_aio_keymap, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		pr_err("Unable to setup input device keymap\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		goto err_free_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	err = input_register_device(dell_wmi_aio_input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		pr_info("Unable to register input device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		goto err_free_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) err_free_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	input_free_device(dell_wmi_aio_input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static const char *dell_wmi_aio_find(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	for (i = 0; dell_wmi_aio_guids[i] != NULL; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		if (wmi_has_guid(dell_wmi_aio_guids[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			return dell_wmi_aio_guids[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static int __init dell_wmi_aio_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	const char *guid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	guid = dell_wmi_aio_find();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	if (!guid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		pr_warn("No known WMI GUID found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	err = dell_wmi_aio_input_setup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	err = wmi_install_notify_handler(guid, dell_wmi_aio_notify, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		pr_err("Unable to register notify handler - %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		input_unregister_device(dell_wmi_aio_input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static void __exit dell_wmi_aio_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	const char *guid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	guid = dell_wmi_aio_find();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	wmi_remove_notify_handler(guid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	input_unregister_device(dell_wmi_aio_input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) module_init(dell_wmi_aio_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) module_exit(dell_wmi_aio_exit);