^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * PEAQ 2-in-1 WMI hotkey driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/dmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define PEAQ_DOLBY_BUTTON_GUID "ABBC0F6F-8EA1-11D1-00A0-C90629100000"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define PEAQ_DOLBY_BUTTON_METHOD_ID 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define PEAQ_POLL_INTERVAL_MS 250
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define PEAQ_POLL_IGNORE_MS 500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define PEAQ_POLL_MAX_MS 1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) MODULE_ALIAS("wmi:"PEAQ_DOLBY_BUTTON_GUID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static struct input_dev *peaq_poll_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * The Dolby button (yes really a Dolby button) causes an ACPI variable to get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * set on both press and release. The WMI method checks and clears that flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * So for a press + release we will get back One from the WMI method either once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * (if polling after the release) or twice (polling between press and release).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * We ignore events for 0.5s after the first event to avoid reporting 2 presses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static void peaq_wmi_poll(struct input_dev *input_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static unsigned long last_event_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static bool had_events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) union acpi_object obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) u32 dummy = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct acpi_buffer input = { sizeof(dummy), &dummy };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct acpi_buffer output = { sizeof(obj), &obj };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) status = wmi_evaluate_method(PEAQ_DOLBY_BUTTON_GUID, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) PEAQ_DOLBY_BUTTON_METHOD_ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) &input, &output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (obj.type != ACPI_TYPE_INTEGER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) dev_err(&input_dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) "Error WMBC did not return an integer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return;
^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) if (!obj.integer.value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (had_events && time_before(jiffies, last_event_time +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) msecs_to_jiffies(PEAQ_POLL_IGNORE_MS)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) input_event(input_dev, EV_KEY, KEY_SOUND, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) input_sync(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) input_event(input_dev, EV_KEY, KEY_SOUND, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) input_sync(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) last_event_time = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) had_events = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /* Some other devices (Shuttle XS35) use the same WMI GUID for other purposes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static const struct dmi_system_id peaq_dmi_table[] __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .matches = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) DMI_MATCH(DMI_SYS_VENDOR, "PEAQ"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) DMI_MATCH(DMI_PRODUCT_NAME, "PEAQ PMM C1010 MD99187"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static int __init peaq_wmi_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /* WMI GUID is not unique, also check for a DMI match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!dmi_check_system(peaq_dmi_table))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (!wmi_has_guid(PEAQ_DOLBY_BUTTON_GUID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) peaq_poll_dev = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (!peaq_poll_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) peaq_poll_dev->name = "PEAQ WMI hotkeys";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) peaq_poll_dev->phys = "wmi/input0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) peaq_poll_dev->id.bustype = BUS_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) input_set_capability(peaq_poll_dev, EV_KEY, KEY_SOUND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) err = input_setup_polling(peaq_poll_dev, peaq_wmi_poll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) input_set_poll_interval(peaq_poll_dev, PEAQ_POLL_INTERVAL_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) input_set_max_poll_interval(peaq_poll_dev, PEAQ_POLL_MAX_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) err = input_register_device(peaq_poll_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) input_free_device(peaq_poll_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static void __exit peaq_wmi_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) input_unregister_device(peaq_poll_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) module_init(peaq_wmi_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) module_exit(peaq_wmi_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) MODULE_DESCRIPTION("PEAQ 2-in-1 WMI hotkey driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) MODULE_LICENSE("GPL");