├── util.h ├── pr.h ├── dkms.conf ├── hwmon_fan.h ├── hwmon_pwm.h ├── misc.h ├── hwmon.h ├── battery.h ├── debugfs.h ├── events.h ├── pdev.h ├── led_lightbar.h ├── .gitignore ├── fan.h ├── features.h ├── wmi.h ├── hwmon.c ├── Makefile ├── misc.c ├── ec.c ├── hwmon_pwm.c ├── battery.c ├── hwmon_fan.c ├── main.c ├── features.c ├── fan.c ├── debugfs.c ├── pdev.c ├── ec.h ├── README.md ├── led_lightbar.c ├── events.c └── LICENSE /util.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #ifndef QC71_UTIL_H 3 | #define QC71_UTIL_H 4 | 5 | #define SET_BIT(value, bit, on) ((on) ? ((value) | (bit)) : ((value) & ~(bit))) 6 | 7 | #endif /* QC71_UTIL_H */ 8 | -------------------------------------------------------------------------------- /pr.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #ifndef QC71_PR_H 3 | #define QC71_PR_H 4 | 5 | #define pr_fmt(fmt) KBUILD_MODNAME "/" KBUILD_BASENAME ": %s: " fmt, __func__ 6 | 7 | #include 8 | 9 | #endif /* QC71_PR_H */ 10 | -------------------------------------------------------------------------------- /dkms.conf: -------------------------------------------------------------------------------- 1 | MAKE="make KDIR=${kernel_source_dir}" 2 | CLEAN="make KDIR=${kernel_source_dir} clean" 3 | BUILT_MODULE_NAME=qc71_laptop 4 | PACKAGE_NAME=qc71_laptop 5 | PACKAGE_VERSION=0.0 6 | DEST_MODULE_LOCATION=/kernel/drivers/platform/x86 7 | AUTOINSTALL=yes 8 | -------------------------------------------------------------------------------- /hwmon_fan.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #ifndef QC71_HWMON_FAN_H 3 | #define QC71_HWMON_FAN_H 4 | 5 | #include 6 | 7 | int __init qc71_hwmon_fan_setup(void); 8 | void qc71_hwmon_fan_cleanup(void); 9 | 10 | #endif /* QC71_HWMON_FAN_H */ 11 | -------------------------------------------------------------------------------- /hwmon_pwm.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #ifndef QC71_HWMON_PWM_H 3 | #define QC71_HWMON_PWM_H 4 | 5 | #include 6 | 7 | int __init qc71_hwmon_pwm_setup(void); 8 | void qc71_hwmon_pwm_cleanup(void); 9 | 10 | #endif /* QC71_HWMON_PWM_H */ 11 | -------------------------------------------------------------------------------- /misc.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #ifndef QC71_MISC_H 3 | #define QC71_MISC_H 4 | 5 | #include 6 | 7 | /* ========================================================================== */ 8 | 9 | int qc71_rfkill_get_wifi_state(void); 10 | 11 | int qc71_fn_lock_get_state(void); 12 | int qc71_fn_lock_set_state(bool state); 13 | 14 | #endif /* QC71_MISC_H */ 15 | -------------------------------------------------------------------------------- /hwmon.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #ifndef QC71_HWMON_H 3 | #define QC71_HWMON_H 4 | 5 | #if IS_ENABLED(CONFIG_HWMON) 6 | 7 | #include 8 | 9 | int __init qc71_hwmon_setup(void); 10 | void qc71_hwmon_cleanup(void); 11 | 12 | #else 13 | 14 | static inline int qc71_hwmon_setup(void) 15 | { 16 | return 0; 17 | } 18 | 19 | static inline void qc71_hwmon_cleanup(void) 20 | { 21 | 22 | } 23 | 24 | #endif 25 | 26 | #endif /* QC71_HWMON_H */ 27 | -------------------------------------------------------------------------------- /battery.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #ifndef QC71_BATTERY_H 3 | #define QC71_BATTERY_H 4 | 5 | #if IS_ENABLED(CONFIG_ACPI_BATTERY) 6 | 7 | #include 8 | 9 | int __init qc71_battery_setup(void); 10 | void qc71_battery_cleanup(void); 11 | 12 | #else 13 | 14 | static inline int qc71_battery_setup(void) 15 | { 16 | return 0; 17 | } 18 | 19 | static inline void qc71_battery_cleanup(void) 20 | { 21 | 22 | } 23 | 24 | #endif 25 | 26 | #endif /* QC71_BATTERY_H */ 27 | -------------------------------------------------------------------------------- /debugfs.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #ifndef QC71_DEBUGFS_H 3 | #define QC71_DEBUGFS_H 4 | 5 | #if IS_ENABLED(CONFIG_DEBUG_FS) 6 | 7 | #include 8 | 9 | int __init qc71_debugfs_setup(void); 10 | void qc71_debugfs_cleanup(void); 11 | 12 | #else 13 | 14 | static inline int qc71_debugfs_setup(void) 15 | { 16 | return 0; 17 | } 18 | 19 | static inline void qc71_debugfs_cleanup(void) 20 | { 21 | 22 | } 23 | 24 | #endif 25 | 26 | #endif /* QC71_DEBUGFS_H */ 27 | -------------------------------------------------------------------------------- /events.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #ifndef QC71_WMI_EVENTS_H 3 | #define QC71_WMI_EVENTS_H 4 | 5 | #if IS_ENABLED(CONFIG_LEDS_CLASS) 6 | 7 | #include 8 | 9 | int __init qc71_wmi_events_setup(void); 10 | void qc71_wmi_events_cleanup(void); 11 | 12 | #else 13 | 14 | static inline int qc71_wmi_events_setup(void) 15 | { 16 | return 0; 17 | } 18 | 19 | static inline void qc71_wmi_events_cleanup(void) 20 | { 21 | 22 | } 23 | 24 | #endif 25 | 26 | #endif /* QC71_WMI_EVENTS_H */ 27 | -------------------------------------------------------------------------------- /pdev.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #ifndef QC71_PDEV_H 3 | #define QC71_PDEV_H 4 | 5 | #include 6 | #include 7 | 8 | /* ========================================================================== */ 9 | 10 | extern struct platform_device *qc71_platform_dev; 11 | 12 | /* ========================================================================== */ 13 | 14 | int __init qc71_pdev_setup(void); 15 | void qc71_pdev_cleanup(void); 16 | 17 | #endif /* QC71_PDEV_H */ 18 | -------------------------------------------------------------------------------- /led_lightbar.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #ifndef QC71_LED_LIGHTBAR_H 3 | #define QC71_LED_LIGHTBAR_H 4 | 5 | #if IS_ENABLED(CONFIG_LEDS_CLASS) 6 | 7 | #include 8 | 9 | int __init qc71_led_lightbar_setup(void); 10 | void qc71_led_lightbar_cleanup(void); 11 | 12 | #else 13 | 14 | static inline int qc71_led_lightbar_setup(void) 15 | { 16 | return 0; 17 | } 18 | 19 | static inline void qc71_led_lightbar_cleanup(void) 20 | { 21 | 22 | } 23 | 24 | #endif 25 | 26 | #endif /* QC71_LED_LIGHTBAR_H */ 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .* 2 | !.gitignore 3 | 4 | # Prerequisites 5 | *.d 6 | 7 | # Object files 8 | *.o 9 | *.ko 10 | *.obj 11 | *.elf 12 | 13 | # Linker output 14 | *.ilk 15 | *.map 16 | *.exp 17 | 18 | # Precompiled Headers 19 | *.gch 20 | *.pch 21 | 22 | # Libraries 23 | *.lib 24 | *.a 25 | *.la 26 | *.lo 27 | 28 | # Shared objects (inc. Windows DLLs) 29 | *.dll 30 | *.so 31 | *.so.* 32 | *.dylib 33 | 34 | # Executables 35 | *.exe 36 | *.out 37 | *.app 38 | *.i*86 39 | *.x86_64 40 | *.hex 41 | 42 | # Debug files 43 | *.dSYM/ 44 | *.su 45 | *.idb 46 | *.pdb 47 | 48 | # Kernel Module Compile Results 49 | *.mod* 50 | *.cmd 51 | .tmp_versions/ 52 | modules.order 53 | Module.symvers 54 | Mkfile.old 55 | dkms.conf 56 | -------------------------------------------------------------------------------- /fan.h: -------------------------------------------------------------------------------- 1 | #ifndef QC71_LAPTOP_FAN_H 2 | #define QC71_LAPTOP_FAN_H 3 | 4 | #include 5 | 6 | /* ========================================================================== */ 7 | 8 | #define FAN_MAX_PWM 200 9 | #define FAN_CTRL_MAX_LEVEL 7 10 | #define FAN_CTRL_LEVEL(level) (128 + (level)) 11 | 12 | /* ========================================================================== */ 13 | 14 | int qc71_fan_get_rpm(uint8_t fan_index); 15 | int qc71_fan_query_abnorm(void); 16 | int qc71_fan_get_pwm(uint8_t fan_index); 17 | int qc71_fan_set_pwm(uint8_t fan_index, uint8_t pwm); 18 | int qc71_fan_get_temp(uint8_t fan_index); 19 | int qc71_fan_get_mode(void); 20 | int qc71_fan_set_mode(uint8_t mode); 21 | 22 | #endif /* QC71_LAPTOP_FAN_H */ 23 | -------------------------------------------------------------------------------- /features.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #ifndef QC71_FEATURES_H 3 | #define QC71_FEATURES_H 4 | 5 | #include 6 | #include 7 | 8 | struct qc71_features_struct { 9 | bool super_key_lock : 1; 10 | bool lightbar : 1; 11 | bool fan_boost : 1; 12 | bool fn_lock : 1; 13 | bool batt_charge_limit : 1; 14 | bool fan_extras : 1; /* duty cycle reduction, always on mode */ 15 | }; 16 | 17 | /* ========================================================================== */ 18 | 19 | extern struct qc71_features_struct qc71_features; 20 | 21 | /* ========================================================================== */ 22 | 23 | int __init qc71_check_features(void); 24 | 25 | #endif /* QC71_FEATURES_H */ 26 | -------------------------------------------------------------------------------- /wmi.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #ifndef QC71_LAPTOP_WMI_H 3 | #define QC71_LAPTOP_WMI_H 4 | 5 | /* ========================================================================== */ 6 | /* WMI methods */ 7 | 8 | /* AcpiTest_MULong */ 9 | #define QC71_WMI_WMBC_GUID "ABBC0F6F-8EA1-11D1-00A0-C90629100000" 10 | #define QC71_WMBC_GETSETULONG_ID 4 11 | 12 | /* ========================================================================== */ 13 | /* WMI events */ 14 | 15 | /* AcpiTest_EventPackage */ 16 | #define QC71_WMI_EVENT_70_GUID "ABBC0F70-8EA1-11D1-00A0-C90629100000" 17 | 18 | /* AcpiTest_EventString */ 19 | #define QC71_WMI_EVENT_71_GUID "ABBC0F71-8EA1-11D1-00A0-C90629100000" 20 | 21 | /* AcpiTest_EventULong */ 22 | #define QC71_WMI_EVENT_72_GUID "ABBC0F72-8EA1-11D1-00A0-C90629100000" 23 | 24 | 25 | #endif /* QC71_LAPTOP_WMI_H */ 26 | -------------------------------------------------------------------------------- /hwmon.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #include "pr.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "hwmon.h" 10 | #include "hwmon_fan.h" 11 | #include "hwmon_pwm.h" 12 | 13 | /* ========================================================================== */ 14 | 15 | static bool nohwmon; 16 | module_param(nohwmon, bool, 0444); 17 | MODULE_PARM_DESC(nohwmon, "do not report to the hardware monitoring subsystem (default=false)"); 18 | 19 | /* ========================================================================== */ 20 | 21 | int __init qc71_hwmon_setup(void) 22 | { 23 | if (nohwmon) 24 | return -ENODEV; 25 | 26 | (void) qc71_hwmon_fan_setup(); 27 | (void) qc71_hwmon_pwm_setup(); 28 | 29 | return 0; 30 | } 31 | 32 | void qc71_hwmon_cleanup(void) 33 | { 34 | (void) qc71_hwmon_fan_cleanup(); 35 | (void) qc71_hwmon_pwm_cleanup(); 36 | } 37 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: GPL-2.0 2 | MODNAME = qc71_laptop 3 | MODVER = 0.0 4 | 5 | obj-m += $(MODNAME).o 6 | 7 | # alphabetically sorted 8 | $(MODNAME)-y += ec.o \ 9 | features.o \ 10 | main.o \ 11 | misc.o \ 12 | pdev.o \ 13 | events.o \ 14 | 15 | $(MODNAME)-$(CONFIG_DEBUG_FS) += debugfs.o 16 | $(MODNAME)-$(CONFIG_ACPI_BATTERY) += battery.o 17 | $(MODNAME)-$(CONFIG_LEDS_CLASS) += led_lightbar.o 18 | $(MODNAME)-$(CONFIG_HWMON) += hwmon.o hwmon_fan.o hwmon_pwm.o fan.o 19 | 20 | KVER = $(shell uname -r) 21 | KDIR = /lib/modules/$(KVER)/build 22 | MDIR = /usr/src/$(MODNAME)-$(MODVER) 23 | 24 | all: 25 | make -C $(KDIR) M=$(PWD) modules 26 | 27 | clean: 28 | make -C $(KDIR) M=$(PWD) clean 29 | 30 | dkmsinstall: 31 | mkdir -p $(MDIR) 32 | cp Makefile dkms.conf $(wildcard *.c) $(wildcard *.h) $(MDIR)/. 33 | dkms add $(MODNAME)/$(MODVER) 34 | dkms build $(MODNAME)/$(MODVER) 35 | dkms install $(MODNAME)/$(MODVER) 36 | 37 | dkmsuninstall: 38 | -rmmod $(MODNAME) 39 | -dkms uninstall $(MODNAME)/$(MODVER) 40 | -dkms remove $(MODNAME)/$(MODVER) --all 41 | rm -rf $(MDIR) 42 | -------------------------------------------------------------------------------- /misc.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #include "pr.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "ec.h" 10 | #include "misc.h" 11 | #include "util.h" 12 | 13 | /* ========================================================================== */ 14 | 15 | int qc71_rfkill_get_wifi_state(void) 16 | { 17 | int err = ec_read_byte(DEVICE_STATUS_ADDR); 18 | 19 | if (err < 0) 20 | return err; 21 | 22 | return !!(err & DEVICE_STATUS_WIFI_ON); 23 | } 24 | 25 | /* ========================================================================== */ 26 | 27 | int qc71_fn_lock_get_state(void) 28 | { 29 | int status = ec_read_byte(BIOS_CTRL_1_ADDR); 30 | 31 | if (status < 0) 32 | return status; 33 | 34 | return !!(status & BIOS_CTRL_1_FN_LOCK_STATUS); 35 | } 36 | 37 | int qc71_fn_lock_set_state(bool state) 38 | { 39 | int status = ec_read_byte(BIOS_CTRL_1_ADDR); 40 | 41 | if (status < 0) 42 | return status; 43 | 44 | status = SET_BIT(status, BIOS_CTRL_1_FN_LOCK_STATUS, state); 45 | 46 | return ec_write_byte(BIOS_CTRL_1_ADDR, status); 47 | } 48 | -------------------------------------------------------------------------------- /ec.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #include "pr.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "ec.h" 12 | #include "wmi.h" 13 | 14 | /* ========================================================================== */ 15 | 16 | static DECLARE_RWSEM(ec_lock); 17 | 18 | /* ========================================================================== */ 19 | 20 | int __must_check qc71_ec_transaction(uint16_t addr, uint16_t data, 21 | union qc71_ec_result *result, bool read) 22 | { 23 | uint8_t buf[] = { 24 | addr & 0xFF, 25 | addr >> 8, 26 | data & 0xFF, 27 | data >> 8, 28 | 0, 29 | read ? 1 : 0, 30 | 0, 31 | 0, 32 | }; 33 | static_assert(ARRAY_SIZE(buf) == 8); 34 | 35 | /* the returned ACPI_TYPE_BUFFER is 40 bytes long for some reason ... */ 36 | __aligned(__alignof__(union acpi_object)) uint8_t output_buf[sizeof(union acpi_object) + 40] = {0}; 37 | 38 | struct acpi_buffer input = { sizeof(buf), buf }, 39 | output = { sizeof(output_buf), output_buf }; 40 | union acpi_object *obj; 41 | acpi_status status = AE_OK; 42 | int err; 43 | 44 | if (read) err = down_read_killable(&ec_lock); 45 | else err = down_write_killable(&ec_lock); 46 | 47 | if (err) 48 | goto out; 49 | 50 | status = wmi_evaluate_method(QC71_WMI_WMBC_GUID, 0, 51 | QC71_WMBC_GETSETULONG_ID, &input, &output); 52 | 53 | if (read) up_read(&ec_lock); 54 | else up_write(&ec_lock); 55 | 56 | if (ACPI_FAILURE(status)) { 57 | err = -EIO; 58 | goto out; 59 | } 60 | 61 | obj = output.pointer; 62 | 63 | if (result) { 64 | if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length >= sizeof(*result)) { 65 | memcpy(result, obj->buffer.pointer, sizeof(*result)); 66 | } else { 67 | err = -ENODATA; 68 | goto out; 69 | } 70 | } 71 | 72 | out: 73 | pr_debug( 74 | "%s(addr=%#06x, data=%#06x, result=%c, read=%c)" 75 | ": (%d) [%#010lx] %s" 76 | ": [%*ph]\n", 77 | 78 | __func__, (unsigned int) addr, (unsigned int) data, 79 | result ? 'y' : 'n', read ? 'y' : 'n', 80 | err, (unsigned long) status, acpi_format_exception(status), 81 | (obj && obj->type == ACPI_TYPE_BUFFER) ? 82 | (int) min(sizeof(*result), (size_t) obj->buffer.length) : 0, 83 | (obj && obj->type == ACPI_TYPE_BUFFER) ? 84 | obj->buffer.pointer : NULL 85 | ); 86 | 87 | return err; 88 | } 89 | ALLOW_ERROR_INJECTION(qc71_ec_transaction, ERRNO); 90 | -------------------------------------------------------------------------------- /hwmon_pwm.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #include "pr.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "fan.h" 14 | #include "features.h" 15 | #include "hwmon_pwm.h" 16 | #include "pdev.h" 17 | 18 | /* ========================================================================== */ 19 | 20 | static struct device *qc71_hwmon_pwm_dev; 21 | 22 | /* ========================================================================== */ 23 | 24 | static umode_t qc71_hwmon_pwm_is_visible(const void *data, enum hwmon_sensor_types type, 25 | u32 attr, int channel) 26 | { 27 | if (type != hwmon_pwm && attr != hwmon_pwm_enable) 28 | return -EOPNOTSUPP; 29 | 30 | return 0644; 31 | } 32 | 33 | static int qc71_hwmon_pwm_read(struct device *device, enum hwmon_sensor_types type, 34 | u32 attr, int channel, long *value) 35 | { 36 | int err; 37 | 38 | switch (type) { 39 | case hwmon_pwm: 40 | switch (attr) { 41 | case hwmon_pwm_enable: 42 | err = qc71_fan_get_mode(); 43 | if (err < 0) 44 | return err; 45 | 46 | *value = err; 47 | break; 48 | case hwmon_pwm_input: 49 | err = qc71_fan_get_pwm(channel); 50 | if (err < 0) 51 | return err; 52 | 53 | *value = err; 54 | break; 55 | default: 56 | return -EOPNOTSUPP; 57 | } 58 | break; 59 | default: 60 | return -EOPNOTSUPP; 61 | } 62 | 63 | return 0; 64 | } 65 | 66 | static int qc71_hwmon_pwm_write(struct device *device, enum hwmon_sensor_types type, 67 | u32 attr, int channel, long value) 68 | { 69 | switch (type) { 70 | case hwmon_pwm: 71 | switch (attr) { 72 | case hwmon_pwm_enable: 73 | return qc71_fan_set_mode(value); 74 | case hwmon_pwm_input: 75 | return qc71_fan_set_pwm(channel, value); 76 | default: 77 | return -EOPNOTSUPP; 78 | } 79 | default: 80 | return -EOPNOTSUPP; 81 | } 82 | 83 | return 0; 84 | } 85 | 86 | static const struct hwmon_channel_info *qc71_hwmon_pwm_ch_info[] = { 87 | HWMON_CHANNEL_INFO(pwm, HWMON_PWM_ENABLE), 88 | HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT, HWMON_PWM_INPUT), 89 | NULL 90 | }; 91 | 92 | static const struct hwmon_ops qc71_hwmon_pwm_ops = { 93 | .is_visible = qc71_hwmon_pwm_is_visible, 94 | .read = qc71_hwmon_pwm_read, 95 | .write = qc71_hwmon_pwm_write, 96 | }; 97 | 98 | static const struct hwmon_chip_info qc71_hwmon_pwm_chip_info = { 99 | .ops = &qc71_hwmon_pwm_ops, 100 | .info = qc71_hwmon_pwm_ch_info, 101 | }; 102 | 103 | /* ========================================================================== */ 104 | 105 | int __init qc71_hwmon_pwm_setup(void) 106 | { 107 | if (!qc71_features.fan_boost) 108 | return -ENODEV; 109 | 110 | qc71_hwmon_pwm_dev = hwmon_device_register_with_info( 111 | &qc71_platform_dev->dev, KBUILD_MODNAME ".hwmon.pwm", NULL, 112 | &qc71_hwmon_pwm_chip_info, NULL); 113 | 114 | if (IS_ERR(qc71_hwmon_pwm_dev)) 115 | return PTR_ERR(qc71_hwmon_pwm_dev); 116 | 117 | return 0; 118 | } 119 | 120 | void qc71_hwmon_pwm_cleanup(void) 121 | { 122 | if (!IS_ERR_OR_NULL(qc71_hwmon_pwm_dev)) 123 | hwmon_device_unregister(qc71_hwmon_pwm_dev); 124 | } 125 | -------------------------------------------------------------------------------- /battery.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #include "pr.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "battery.h" 14 | #include "ec.h" 15 | #include "features.h" 16 | 17 | /* ========================================================================== */ 18 | 19 | #if IS_ENABLED(CONFIG_ACPI_BATTERY) 20 | 21 | static bool battery_hook_registered; 22 | 23 | static bool nobattery; 24 | module_param(nobattery, bool, 0444); 25 | MODULE_PARM_DESC(nobattery, "do not expose battery related controls (default=false)"); 26 | 27 | /* ========================================================================== */ 28 | 29 | static ssize_t charge_control_end_threshold_show(struct device *dev, 30 | struct device_attribute *attr, char *buf) 31 | { 32 | int status = ec_read_byte(BATT_CHARGE_CTRL_ADDR); 33 | 34 | if (status < 0) 35 | return status; 36 | 37 | status &= BATT_CHARGE_CTRL_VALUE_MASK; 38 | 39 | if (status == 0) 40 | status = 100; 41 | 42 | return sprintf(buf, "%d\n", status); 43 | } 44 | 45 | static ssize_t charge_control_end_threshold_store(struct device *dev, struct device_attribute *attr, 46 | const char *buf, size_t count) 47 | { 48 | int status, value; 49 | 50 | if (kstrtoint(buf, 10, &value) || !(1 <= value && value <= 100)) 51 | return -EINVAL; 52 | 53 | status = ec_read_byte(BATT_CHARGE_CTRL_ADDR); 54 | if (status < 0) 55 | return status; 56 | 57 | if (value == 100) 58 | value = 0; 59 | 60 | status = (status & ~BATT_CHARGE_CTRL_VALUE_MASK) | value; 61 | 62 | status = ec_write_byte(BATT_CHARGE_CTRL_ADDR, status); 63 | 64 | if (status < 0) 65 | return status; 66 | 67 | return count; 68 | } 69 | 70 | static DEVICE_ATTR_RW(charge_control_end_threshold); 71 | static struct attribute *qc71_laptop_batt_attrs[] = { 72 | &dev_attr_charge_control_end_threshold.attr, 73 | NULL 74 | }; 75 | ATTRIBUTE_GROUPS(qc71_laptop_batt); 76 | 77 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 2, 0) 78 | static int qc71_laptop_batt_add(struct power_supply *battery, struct acpi_battery_hook *hook) 79 | #else 80 | static int qc71_laptop_batt_add(struct power_supply *battery) 81 | #endif 82 | { 83 | if (strcmp(battery->desc->name, "BAT0") != 0) 84 | return 0; 85 | 86 | return device_add_groups(&battery->dev, qc71_laptop_batt_groups); 87 | } 88 | 89 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 2, 0) 90 | static int qc71_laptop_batt_remove(struct power_supply *battery, struct acpi_battery_hook *hook) 91 | #else 92 | static int qc71_laptop_batt_remove(struct power_supply *battery) 93 | #endif 94 | { 95 | if (strcmp(battery->desc->name, "BAT0") != 0) 96 | return 0; 97 | 98 | device_remove_groups(&battery->dev, qc71_laptop_batt_groups); 99 | return 0; 100 | } 101 | 102 | static struct acpi_battery_hook qc71_laptop_batt_hook = { 103 | .add_battery = qc71_laptop_batt_add, 104 | .remove_battery = qc71_laptop_batt_remove, 105 | .name = "QC71 laptop battery extension", 106 | }; 107 | 108 | int __init qc71_battery_setup(void) 109 | { 110 | if (nobattery || !qc71_features.batt_charge_limit) 111 | return -ENODEV; 112 | 113 | battery_hook_register(&qc71_laptop_batt_hook); 114 | battery_hook_registered = true; 115 | 116 | return 0; 117 | } 118 | 119 | void qc71_battery_cleanup(void) 120 | { 121 | if (battery_hook_registered) 122 | battery_hook_unregister(&qc71_laptop_batt_hook); 123 | } 124 | 125 | #endif 126 | -------------------------------------------------------------------------------- /hwmon_fan.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #include "pr.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "ec.h" 12 | #include "fan.h" 13 | #include "features.h" 14 | #include "hwmon_fan.h" 15 | #include "pdev.h" 16 | 17 | /* ========================================================================== */ 18 | 19 | static struct device *qc71_hwmon_fan_dev; 20 | 21 | /* ========================================================================== */ 22 | 23 | static umode_t qc71_hwmon_fan_is_visible(const void *data, enum hwmon_sensor_types type, 24 | u32 attr, int channel) 25 | { 26 | switch (type) { 27 | case hwmon_fan: 28 | switch (attr) { 29 | case hwmon_fan_input: 30 | case hwmon_fan_fault: 31 | return 0444; 32 | } 33 | break; 34 | case hwmon_temp: 35 | switch (attr) { 36 | case hwmon_temp_input: 37 | case hwmon_temp_label: 38 | return 0444; 39 | } 40 | default: 41 | break; 42 | } 43 | 44 | return 0; 45 | } 46 | 47 | static int qc71_hwmon_fan_read(struct device *device, enum hwmon_sensor_types type, 48 | u32 attr, int channel, long *value) 49 | { 50 | int err; 51 | 52 | switch (type) { 53 | case hwmon_fan: 54 | switch (attr) { 55 | case hwmon_fan_input: 56 | err = qc71_fan_get_rpm(channel); 57 | if (err < 0) 58 | return err; 59 | 60 | *value = err; 61 | break; 62 | default: 63 | return -EOPNOTSUPP; 64 | } 65 | break; 66 | case hwmon_temp: 67 | switch (attr) { 68 | case hwmon_temp_input: 69 | err = qc71_fan_get_temp(channel); 70 | if (err < 0) 71 | return err; 72 | 73 | *value = err * 1000; 74 | break; 75 | default: 76 | return -EOPNOTSUPP; 77 | } 78 | break; 79 | default: 80 | return -EOPNOTSUPP; 81 | } 82 | 83 | return 0; 84 | } 85 | 86 | static int qc71_hwmon_fan_read_string(struct device *dev, enum hwmon_sensor_types type, 87 | u32 attr, int channel, const char **str) 88 | { 89 | static const char * const temp_labels[] = { 90 | "fan1_temp", 91 | "fan2_temp", 92 | }; 93 | 94 | switch (type) { 95 | case hwmon_temp: 96 | switch (attr) { 97 | case hwmon_temp_label: 98 | *str = temp_labels[channel]; 99 | break; 100 | default: 101 | return -EOPNOTSUPP; 102 | } 103 | break; 104 | default: 105 | return -EOPNOTSUPP; 106 | } 107 | 108 | return 0; 109 | } 110 | 111 | /* ========================================================================== */ 112 | 113 | static const struct hwmon_channel_info *qc71_hwmon_fan_ch_info[] = { 114 | HWMON_CHANNEL_INFO(fan, 115 | HWMON_F_INPUT, 116 | HWMON_F_INPUT), 117 | HWMON_CHANNEL_INFO(temp, 118 | HWMON_T_INPUT | HWMON_T_LABEL, 119 | HWMON_T_INPUT | HWMON_T_LABEL), 120 | NULL 121 | }; 122 | 123 | static const struct hwmon_ops qc71_hwmon_fan_ops = { 124 | .is_visible = qc71_hwmon_fan_is_visible, 125 | .read = qc71_hwmon_fan_read, 126 | .read_string = qc71_hwmon_fan_read_string, 127 | }; 128 | 129 | static const struct hwmon_chip_info qc71_hwmon_fan_chip_info = { 130 | .ops = &qc71_hwmon_fan_ops, 131 | .info = qc71_hwmon_fan_ch_info, 132 | }; 133 | 134 | /* ========================================================================== */ 135 | 136 | int __init qc71_hwmon_fan_setup(void) 137 | { 138 | qc71_hwmon_fan_dev = hwmon_device_register_with_info( 139 | &qc71_platform_dev->dev, KBUILD_MODNAME ".hwmon.fan", NULL, 140 | &qc71_hwmon_fan_chip_info, NULL); 141 | 142 | if (IS_ERR(qc71_hwmon_fan_dev)) 143 | return PTR_ERR(qc71_hwmon_fan_dev); 144 | 145 | return 0; 146 | } 147 | 148 | void qc71_hwmon_fan_cleanup(void) 149 | { 150 | if (!IS_ERR_OR_NULL(qc71_hwmon_fan_dev)) 151 | hwmon_device_unregister(qc71_hwmon_fan_dev); 152 | } 153 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | /* ========================================================================== */ 3 | /* https://www.intel.com/content/dam/support/us/en/documents/laptops/whitebook/QC71_PROD_SPEC.pdf 4 | * 5 | * 6 | * based on the following resources: 7 | * - https://lwn.net/Articles/391230/ 8 | * - http://blog.nietrzeba.pl/2011/12/mof-decompilation.html 9 | * - https://github.com/tuxedocomputers/tuxedo-cc-wmi/ 10 | * - https://github.com/tuxedocomputers/tuxedo-keyboard/ 11 | * - Control Center for Microsoft Windows 12 | * - http://forum.notebookreview.com/threads/tongfang-gk7cn6s-gk7cp0s-gk7cp7s.825461/page-54 13 | */ 14 | /* ========================================================================== */ 15 | #include "pr.h" 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "ec.h" 26 | #include "features.h" 27 | #include "wmi.h" 28 | 29 | /* submodules */ 30 | #include "pdev.h" 31 | #include "events.h" 32 | #include "hwmon.h" 33 | #include "battery.h" 34 | #include "led_lightbar.h" 35 | #include "debugfs.h" 36 | 37 | /* ========================================================================== */ 38 | 39 | #define SUBMODULE_ENTRY(_name, _req) { .name = #_name, .init = qc71_ ## _name ## _setup, .cleanup = qc71_ ## _name ## _cleanup, .required = _req } 40 | 41 | static struct qc71_submodule { 42 | const char *name; 43 | 44 | bool required : 1, 45 | initialized : 1; 46 | 47 | int (*init)(void); 48 | void (*cleanup)(void); 49 | } qc71_submodules[] __refdata = { 50 | SUBMODULE_ENTRY(pdev, true), /* must be first */ 51 | SUBMODULE_ENTRY(wmi_events, false), 52 | SUBMODULE_ENTRY(hwmon, false), 53 | SUBMODULE_ENTRY(battery, false), 54 | SUBMODULE_ENTRY(led_lightbar, false), 55 | SUBMODULE_ENTRY(debugfs, false), 56 | }; 57 | 58 | #undef SUBMODULE_ENTRY 59 | 60 | static void do_cleanup(void) 61 | { 62 | int i; 63 | 64 | for (i = ARRAY_SIZE(qc71_submodules) - 1; i >= 0; i--) { 65 | const struct qc71_submodule *sm = &qc71_submodules[i]; 66 | 67 | if (sm->initialized) 68 | sm->cleanup(); 69 | } 70 | } 71 | 72 | static int __init qc71_laptop_module_init(void) 73 | { 74 | int err = 0, i; 75 | 76 | if (!wmi_has_guid(QC71_WMI_WMBC_GUID)) { 77 | pr_err("WMI GUID not found\n"); 78 | err = -ENODEV; goto out; 79 | } 80 | 81 | err = ec_read_byte(PROJ_ID_ADDR); 82 | if (err < 0) { 83 | pr_err("failed to query project id: %d\n", err); 84 | goto out; 85 | } 86 | 87 | pr_info("project id: %d\n", err); 88 | 89 | err = ec_read_byte(PLATFORM_ID_ADDR); 90 | if (err < 0) { 91 | pr_err("failed to query platform id: %d\n", err); 92 | goto out; 93 | } 94 | 95 | pr_info("platform id: %d\n", err); 96 | 97 | err = qc71_check_features(); 98 | if (err) { 99 | pr_err("cannot check system features: %d\n", err); 100 | goto out; 101 | } 102 | 103 | pr_info("supported features:"); 104 | if (qc71_features.super_key_lock) pr_cont(" super-key-lock"); 105 | if (qc71_features.lightbar) pr_cont(" lightbar"); 106 | if (qc71_features.fan_boost) pr_cont(" fan-boost"); 107 | if (qc71_features.fn_lock) pr_cont(" fn-lock"); 108 | if (qc71_features.batt_charge_limit) pr_cont(" charge-limit"); 109 | if (qc71_features.fan_extras) pr_cont(" fan-extras"); 110 | pr_cont("\n"); 111 | 112 | for (i = 0; i < ARRAY_SIZE(qc71_submodules); i++) { 113 | struct qc71_submodule *sm = &qc71_submodules[i]; 114 | 115 | err = sm->init(); 116 | if (err) { 117 | pr_warn("failed to initialize %s submodule: %d\n", sm->name, err); 118 | if (sm->required) 119 | goto out; 120 | } else { 121 | sm->initialized = true; 122 | } 123 | } 124 | 125 | err = 0; 126 | 127 | out: 128 | if (err) 129 | do_cleanup(); 130 | else 131 | pr_info("module loaded\n"); 132 | 133 | return err; 134 | } 135 | 136 | static void __exit qc71_laptop_module_cleanup(void) 137 | { 138 | do_cleanup(); 139 | pr_info("module unloaded\n"); 140 | } 141 | 142 | /* ========================================================================== */ 143 | 144 | module_init(qc71_laptop_module_init); 145 | module_exit(qc71_laptop_module_cleanup); 146 | 147 | MODULE_LICENSE("GPL"); 148 | MODULE_AUTHOR("Barnabás Pőcze "); 149 | MODULE_DESCRIPTION("QC71 laptop platform driver"); 150 | MODULE_ALIAS("wmi:" QC71_WMI_WMBC_GUID); 151 | -------------------------------------------------------------------------------- /features.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #include "pr.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "ec.h" 12 | #include "features.h" 13 | 14 | /* ========================================================================== */ 15 | 16 | struct oem_string_walker_data { 17 | char *value; 18 | int index; 19 | }; 20 | 21 | /* ========================================================================== */ 22 | 23 | static const struct dmi_system_id qc71_dmi_table[] __initconst = { 24 | { 25 | .matches = { 26 | DMI_MATCH(DMI_BOARD_NAME, "LAPQC71"), 27 | { } 28 | } 29 | }, 30 | { 31 | /* https://avell.com.br/avell-a60-muv-295765 */ 32 | .matches = { 33 | DMI_EXACT_MATCH(DMI_CHASSIS_VENDOR, "Avell High Performance"), 34 | DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "A60 MUV"), 35 | { } 36 | } 37 | }, 38 | { } 39 | }; 40 | 41 | /* ========================================================================== */ 42 | 43 | struct qc71_features_struct qc71_features; 44 | 45 | /* ========================================================================== */ 46 | 47 | static void __init oem_string_walker(const struct dmi_header *dm, void *ptr) 48 | { 49 | int i, count; 50 | const uint8_t *s; 51 | struct oem_string_walker_data *data = ptr; 52 | 53 | if (dm->type != 11 || dm->length < 5 || !IS_ERR_OR_NULL(data->value)) 54 | return; 55 | 56 | count = *(uint8_t *)(dm + 1); 57 | 58 | if (data->index >= count) 59 | return; 60 | 61 | i = 0; 62 | s = ((uint8_t *)dm) + dm->length; 63 | 64 | while (i++ < data->index && *s) 65 | s += strlen(s) + 1; 66 | 67 | data->value = kstrdup(s, GFP_KERNEL); 68 | 69 | if (!data->value) 70 | data->value = ERR_PTR(-ENOMEM); 71 | } 72 | 73 | static char * __init read_oem_string(int index) 74 | { 75 | struct oem_string_walker_data d = {.value = ERR_PTR(-ENOENT), 76 | .index = index}; 77 | int err = dmi_walk(oem_string_walker, &d); 78 | 79 | if (err) { 80 | if (!IS_ERR_OR_NULL(d.value)) 81 | kfree(d.value); 82 | return ERR_PTR(err); 83 | } 84 | 85 | return d.value; 86 | } 87 | 88 | /* QCCFL357.0062.2020.0313.1530 -> 62 */ 89 | static int __pure __init parse_bios_version(const char *str) 90 | { 91 | const char *p = strchr(str, '.'), *p2; 92 | int bios_version; 93 | 94 | if (!p) 95 | return -EINVAL; 96 | 97 | p2 = strchr(p + 1, '.'); 98 | 99 | if (!p2) 100 | return -EINVAL; 101 | 102 | p += 1; 103 | 104 | bios_version = 0; 105 | 106 | while (p != p2) { 107 | if (!isdigit(*p)) 108 | return -EINVAL; 109 | 110 | bios_version = 10 * bios_version + *p - '0'; 111 | p += 1; 112 | } 113 | 114 | return bios_version; 115 | } 116 | 117 | static int __init check_features_ec(void) 118 | { 119 | int err = ec_read_byte(SUPPORT_1_ADDR); 120 | 121 | if (err >= 0) { 122 | qc71_features.super_key_lock = !!(err & SUPPORT_1_SUPER_KEY_LOCK); 123 | qc71_features.lightbar = !!(err & SUPPORT_1_LIGHTBAR); 124 | qc71_features.fan_boost = !!(err & SUPPORT_1_FAN_BOOST); 125 | } else { 126 | pr_warn("failed to query support_1 byte: %d\n", err); 127 | } 128 | 129 | return err; 130 | } 131 | 132 | static int __init check_features_bios(void) 133 | { 134 | const char *bios_version_str; 135 | int bios_version; 136 | 137 | if (!dmi_check_system(qc71_dmi_table)) { 138 | pr_warn("no DMI match\n"); 139 | return -ENODEV; 140 | } 141 | 142 | bios_version_str = dmi_get_system_info(DMI_BIOS_VERSION); 143 | 144 | if (!bios_version_str) { 145 | pr_warn("failed to get BIOS version DMI string\n"); 146 | return -ENOENT; 147 | } 148 | 149 | pr_info("BIOS version string: '%s'\n", bios_version_str); 150 | 151 | bios_version = parse_bios_version(bios_version_str); 152 | 153 | if (bios_version < 0) { 154 | pr_warn("cannot parse BIOS version\n"); 155 | return -EINVAL; 156 | } 157 | 158 | pr_info("BIOS version: %04d\n", bios_version); 159 | 160 | if (bios_version >= 114) { 161 | const char *s = read_oem_string(18); 162 | size_t s_len; 163 | 164 | if (IS_ERR(s)) 165 | return PTR_ERR(s); 166 | 167 | s_len = strlen(s); 168 | 169 | pr_info("OEM_STRING(18) = '%s'\n", s); 170 | 171 | /* if it is entirely spaces */ 172 | if (strspn(s, " ") == s_len) { 173 | qc71_features.fn_lock = true; 174 | qc71_features.batt_charge_limit = true; 175 | qc71_features.fan_extras = true; 176 | } else if (s_len > 0) { 177 | /* TODO */ 178 | pr_warn("cannot extract supported features"); 179 | } 180 | 181 | kfree(s); 182 | } 183 | 184 | return 0; 185 | } 186 | 187 | int __init qc71_check_features(void) 188 | { 189 | (void) check_features_ec(); 190 | (void) check_features_bios(); 191 | 192 | return 0; 193 | } 194 | -------------------------------------------------------------------------------- /fan.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #include "pr.h" 3 | 4 | #include 5 | 6 | #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0) 7 | static inline int fixp_linear_interpolate(int x0, int y0, int x1, int y1, int x) 8 | { 9 | if (y0 == y1 || x == x0) 10 | return y0; 11 | if (x1 == x0 || x == x1) 12 | return y1; 13 | 14 | return y0 + ((y1 - y0) * (x - x0) / (x1 - x0)); 15 | } 16 | #else 17 | #include /* fixp-arith.h needs it, but doesn't include it */ 18 | #include 19 | #endif 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | #include "ec.h" 26 | #include "fan.h" 27 | #include "util.h" 28 | 29 | /* ========================================================================== */ 30 | 31 | static const uint16_t qc71_fan_rpm_addrs[] = { 32 | FAN_RPM_1_ADDR, 33 | FAN_RPM_2_ADDR, 34 | }; 35 | 36 | static const uint16_t qc71_fan_pwm_addrs[] = { 37 | FAN_PWM_1_ADDR, 38 | FAN_PWM_2_ADDR, 39 | }; 40 | 41 | static const uint16_t qc71_fan_temp_addrs[] = { 42 | FAN_TEMP_1_ADDR, 43 | FAN_TEMP_2_ADDR, 44 | }; 45 | 46 | /* ========================================================================== */ 47 | 48 | static DEFINE_MUTEX(fan_lock); 49 | 50 | /* ========================================================================== */ 51 | 52 | static int qc71_fan_get_status(void) 53 | { 54 | return ec_read_byte(FAN_CTRL_ADDR); 55 | } 56 | 57 | /* 'fan_lock' must be held */ 58 | static int qc71_fan_get_mode_unlocked(void) 59 | { 60 | int err; 61 | 62 | lockdep_assert_held(&fan_lock); 63 | 64 | err = ec_read_byte(CTRL_1_ADDR); 65 | if (err < 0) 66 | return err; 67 | 68 | if (err & CTRL_1_MANUAL_MODE) { 69 | err = qc71_fan_get_status(); 70 | if (err < 0) 71 | return err; 72 | 73 | if (err & FAN_CTRL_FAN_BOOST) { 74 | err = qc71_fan_get_pwm(0); 75 | 76 | if (err < 0) 77 | return err; 78 | 79 | if (err == FAN_MAX_PWM) 80 | err = 0; /* disengaged */ 81 | else 82 | err = 1; /* manual */ 83 | 84 | } else if (err & FAN_CTRL_AUTO) { 85 | err = 2; /* automatic fan control */ 86 | } else { 87 | err = 1; /* manual */ 88 | } 89 | } else { 90 | err = 2; /* automatic fan control */ 91 | } 92 | 93 | return err; 94 | } 95 | 96 | /* ========================================================================== */ 97 | 98 | int qc71_fan_get_rpm(uint8_t fan_index) 99 | { 100 | union qc71_ec_result res; 101 | int err; 102 | 103 | if (fan_index >= ARRAY_SIZE(qc71_fan_rpm_addrs)) 104 | return -EINVAL; 105 | 106 | err = qc71_ec_read(qc71_fan_rpm_addrs[fan_index], &res); 107 | 108 | if (err) 109 | return err; 110 | 111 | return res.bytes.b1 << 8 | res.bytes.b2; 112 | } 113 | 114 | int qc71_fan_query_abnorm(void) 115 | { 116 | int res = ec_read_byte(CTRL_1_ADDR); 117 | 118 | if (res < 0) 119 | return res; 120 | 121 | return !!(res & CTRL_1_FAN_ABNORMAL); 122 | } 123 | 124 | int qc71_fan_get_pwm(uint8_t fan_index) 125 | { 126 | int err; 127 | 128 | if (fan_index >= ARRAY_SIZE(qc71_fan_pwm_addrs)) 129 | return -EINVAL; 130 | 131 | err = ec_read_byte(qc71_fan_pwm_addrs[fan_index]); 132 | if (err < 0) 133 | return err; 134 | 135 | return fixp_linear_interpolate(0, 0, FAN_MAX_PWM, U8_MAX, err); 136 | } 137 | 138 | int qc71_fan_set_pwm(uint8_t fan_index, uint8_t pwm) 139 | { 140 | if (fan_index >= ARRAY_SIZE(qc71_fan_pwm_addrs)) 141 | return -EINVAL; 142 | 143 | return ec_write_byte(qc71_fan_pwm_addrs[fan_index], 144 | fixp_linear_interpolate(0, 0, 145 | U8_MAX, FAN_MAX_PWM, 146 | pwm)); 147 | } 148 | 149 | int qc71_fan_get_temp(uint8_t fan_index) 150 | { 151 | if (fan_index >= ARRAY_SIZE(qc71_fan_temp_addrs)) 152 | return -EINVAL; 153 | 154 | return ec_read_byte(qc71_fan_temp_addrs[fan_index]); 155 | } 156 | 157 | int qc71_fan_get_mode(void) 158 | { 159 | int err = mutex_lock_interruptible(&fan_lock); 160 | 161 | if (err) 162 | return err; 163 | 164 | err = qc71_fan_get_mode_unlocked(); 165 | 166 | mutex_unlock(&fan_lock); 167 | return err; 168 | } 169 | 170 | int qc71_fan_set_mode(uint8_t mode) 171 | { 172 | int err, oldpwm; 173 | 174 | err = mutex_lock_interruptible(&fan_lock); 175 | if (err) 176 | return err; 177 | 178 | switch (mode) { 179 | case 0: 180 | err = ec_write_byte(FAN_CTRL_ADDR, FAN_CTRL_FAN_BOOST); 181 | if (err) 182 | goto out; 183 | 184 | err = qc71_fan_set_pwm(0, FAN_MAX_PWM); 185 | break; 186 | case 1: 187 | oldpwm = err = qc71_fan_get_pwm(0); 188 | if (err < 0) 189 | goto out; 190 | 191 | err = ec_write_byte(FAN_CTRL_ADDR, FAN_CTRL_FAN_BOOST); 192 | if (err < 0) 193 | goto out; 194 | 195 | err = qc71_fan_set_pwm(0, oldpwm); 196 | if (err < 0) 197 | (void) ec_write_byte(FAN_CTRL_ADDR, 0x80 | FAN_CTRL_AUTO); 198 | /* try to restore automatic fan control */ 199 | 200 | break; 201 | case 2: 202 | err = ec_write_byte(FAN_CTRL_ADDR, 0x80 | FAN_CTRL_AUTO); 203 | break; 204 | default: 205 | err = -EINVAL; 206 | break; 207 | } 208 | 209 | out: 210 | mutex_unlock(&fan_lock); 211 | return err; 212 | } 213 | -------------------------------------------------------------------------------- /debugfs.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #include "pr.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "debugfs.h" 12 | #include "ec.h" 13 | 14 | #if IS_ENABLED(CONFIG_DEBUG_FS) 15 | 16 | #define DEBUGFS_DIR_NAME KBUILD_MODNAME 17 | 18 | static const struct qc71_debugfs_attr { 19 | const char *name; 20 | uint16_t addr; 21 | } qc71_debugfs_attrs[] = { 22 | {"1108", 1108}, 23 | 24 | {"ap_bios_byte", AP_BIOS_BYTE_ADDR}, 25 | 26 | {"batt_alert", BATT_ALERT_ADDR}, 27 | {"batt_charge_ctrl", BATT_CHARGE_CTRL_ADDR}, 28 | {"batt_status", BATT_STATUS_ADDR}, 29 | {"batt_temp", BATT_TEMP_ADDR}, 30 | {"bios_ctrl_1", BIOS_CTRL_1_ADDR}, 31 | {"bios_ctrl_2", BIOS_CTRL_2_ADDR}, 32 | {"bios_ctrl_3", BIOS_CTRL_3_ADDR}, 33 | {"bios_info_1", BIOS_INFO_1_ADDR}, 34 | {"bios_info_5", BIOS_INFO_5_ADDR}, 35 | 36 | {"ctrl_1", CTRL_1_ADDR}, 37 | {"ctrl_2", CTRL_2_ADDR}, 38 | {"ctrl_3", CTRL_3_ADDR}, 39 | {"ctrl_4", CTRL_4_ADDR}, 40 | {"ctrl_5", CTRL_5_ADDR}, 41 | {"ctrl_6", CTRL_6_ADDR}, 42 | 43 | {"device_status", DEVICE_STATUS_ADDR}, 44 | 45 | {"fan_ctrl", FAN_CTRL_ADDR}, 46 | {"fan_mode_index", FAN_MODE_INDEX_ADDR}, 47 | {"fan_temp_1", FAN_TEMP_1_ADDR}, 48 | {"fan_temp_2", FAN_TEMP_2_ADDR}, 49 | {"fan_pwm_1", FAN_PWM_1_ADDR}, 50 | {"fan_pwm_2", FAN_PWM_2_ADDR}, 51 | 52 | /* setting these don't seem to work */ 53 | {"fan_l1_pwm", ADDR(0x07, 0x43)}, 54 | {"fan_l2_pwm", ADDR(0x07, 0x44)}, 55 | {"fan_l3_pwm", ADDR(0x07, 0x45)}, 56 | /* seemingly there is another level here, fan_ctrl=0x84, pwm=0x5a */ 57 | {"fan_l4_pwm", ADDR(0x07, 0x46)}, 58 | {"fan_l5_pwm", ADDR(0x07, 0x47)}, /* this is seemingly ignored, fan_ctrl=0x86, pwm=0xb4 */ 59 | 60 | {"fan_l1_pwm_default", ADDR(0x07, 0x86)}, 61 | {"fan_l2_pwm_default", ADDR(0x07, 0x87)}, 62 | {"fan_l3_pwm_default", ADDR(0x07, 0x88)}, 63 | {"fan_l4_pwm_default", ADDR(0x07, 0x89)}, 64 | {"fan_l5_pwm_default", ADDR(0x07, 0x8A)}, 65 | 66 | /* these don't seem to work */ 67 | {"fan_min_speed", 1950}, 68 | {"fan_min_temp", 1951}, 69 | {"fan_extra_speed", 1952}, 70 | 71 | {"lightbar_ctrl", LIGHTBAR_CTRL_ADDR}, 72 | {"lightbar_red", LIGHTBAR_RED_ADDR}, 73 | {"lightbar_green", LIGHTBAR_GREEN_ADDR}, 74 | {"lightbar_blue", LIGHTBAR_BLUE_ADDR}, 75 | 76 | {"keyboard_type", KEYBOARD_TYPE_ADDR}, 77 | 78 | {"support_1", SUPPORT_1_ADDR}, 79 | {"support_2", SUPPORT_2_ADDR}, 80 | {"support_5", SUPPORT_5_ADDR}, 81 | {"status_1", STATUS_1_ADDR}, 82 | 83 | {"platform_id", PLATFORM_ID_ADDR}, 84 | {"power_source", POWER_SOURCE_ADDR}, 85 | {"project_id", PROJ_ID_ADDR}, 86 | {"power_status", POWER_STATUS_ADDR}, 87 | {"pl_1", PL1_ADDR}, 88 | {"pl_2", PL2_ADDR}, 89 | {"pl_4", PL4_ADDR}, 90 | 91 | {"trigger_1", TRIGGER_1_ADDR}, 92 | {"trigger_2", TRIGGER_2_ADDR}, 93 | }; 94 | 95 | /* ========================================================================== */ 96 | 97 | static bool debugregs; 98 | module_param(debugregs, bool, 0444); 99 | MODULE_PARM_DESC(debugregs, "expose various EC registers in debugfs (default=false)"); 100 | 101 | static struct dentry *qc71_debugfs_dir, 102 | *qc71_debugfs_regs_dir; 103 | 104 | /* ========================================================================== */ 105 | 106 | static int get_debugfs_byte(void *data, u64 *value) 107 | { 108 | const struct qc71_debugfs_attr *attr = data; 109 | int status = ec_read_byte(attr->addr); 110 | 111 | if (status < 0) 112 | return status; 113 | 114 | *value = status; 115 | 116 | return 0; 117 | } 118 | 119 | static int set_debugfs_byte(void *data, u64 value) 120 | { 121 | const struct qc71_debugfs_attr *attr = data; 122 | int status; 123 | 124 | if (value > U8_MAX) 125 | return -EINVAL; 126 | 127 | status = ec_write_byte(attr->addr, (uint8_t) value); 128 | 129 | if (status < 0) 130 | return status; 131 | 132 | return status; 133 | } 134 | 135 | DEFINE_DEBUGFS_ATTRIBUTE(qc71_debugfs_fops, get_debugfs_byte, set_debugfs_byte, "0x%02llx\n"); 136 | 137 | /* ========================================================================== */ 138 | 139 | static ssize_t qc71_debugfs_ec_read(struct file *f, char __user *buf, size_t count, loff_t *offset) 140 | { 141 | size_t i; 142 | 143 | for (i = 0; *offset + i < U16_MAX && i < count; i++) { 144 | int err = ec_read_byte(*offset + i); 145 | u8 byte; 146 | 147 | if (signal_pending(current)) 148 | return -EINTR; 149 | 150 | if (err < 0) { 151 | if (i) 152 | break; 153 | 154 | return err; 155 | } 156 | 157 | byte = err; 158 | 159 | if (put_user(byte, buf + i)) 160 | return -EFAULT; 161 | } 162 | 163 | *offset += i; 164 | 165 | return i; 166 | } 167 | 168 | static ssize_t qc71_debugfs_ec_write(struct file *f, const char __user *buf, size_t count, loff_t *offset) 169 | { 170 | size_t i; 171 | 172 | for (i = 0; *offset + i < U16_MAX && i < count; i++) { 173 | int err; 174 | u8 byte; 175 | 176 | if (get_user(byte, buf + i)) 177 | return -EFAULT; 178 | 179 | err = ec_write_byte(*offset + i, byte); 180 | if (err) { 181 | if (i) 182 | break; 183 | 184 | return err; 185 | } 186 | 187 | if (signal_pending(current)) 188 | return -EINTR; 189 | } 190 | 191 | *offset += i; 192 | 193 | return i; 194 | } 195 | 196 | static const struct file_operations qc71_debugfs_ec_fops = { 197 | .owner = THIS_MODULE, 198 | .open = simple_open, 199 | .read = qc71_debugfs_ec_read, 200 | .write = qc71_debugfs_ec_write, 201 | .llseek = default_llseek, 202 | }; 203 | 204 | /* ========================================================================== */ 205 | 206 | int __init qc71_debugfs_setup(void) 207 | { 208 | struct dentry *d; 209 | int err = 0; 210 | size_t i; 211 | 212 | if (!debugregs) 213 | return -ENODEV; 214 | 215 | qc71_debugfs_dir = debugfs_create_dir(DEBUGFS_DIR_NAME, NULL); 216 | if (IS_ERR(qc71_debugfs_dir)) { 217 | err = PTR_ERR(qc71_debugfs_dir); 218 | goto out_error; 219 | } 220 | 221 | qc71_debugfs_regs_dir = debugfs_create_dir("regs", qc71_debugfs_dir); 222 | if (IS_ERR(qc71_debugfs_regs_dir)) { 223 | err = PTR_ERR(qc71_debugfs_regs_dir); 224 | goto out_error_remove_dir; 225 | } 226 | 227 | for (i = 0; i < ARRAY_SIZE(qc71_debugfs_attrs); i++) { 228 | const struct qc71_debugfs_attr *attr = &qc71_debugfs_attrs[i]; 229 | 230 | d = debugfs_create_file(attr->name, 0600, qc71_debugfs_regs_dir, 231 | (void *) attr, &qc71_debugfs_fops); 232 | if (IS_ERR(d)) { 233 | err = PTR_ERR(d); 234 | goto out_error_remove_dir; 235 | } 236 | } 237 | 238 | debugfs_create_file_size("ec", 0600, qc71_debugfs_dir, NULL, &qc71_debugfs_ec_fops, U16_MAX); 239 | 240 | return 0; 241 | 242 | out_error_remove_dir: 243 | debugfs_remove_recursive(qc71_debugfs_dir); 244 | out_error: 245 | return err; 246 | } 247 | 248 | void qc71_debugfs_cleanup(void) 249 | { 250 | /* checks if IS_ERR_OR_NULL() */ 251 | debugfs_remove_recursive(qc71_debugfs_dir); 252 | } 253 | 254 | #endif 255 | -------------------------------------------------------------------------------- /pdev.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #include "pr.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "util.h" 11 | #include "ec.h" 12 | #include "features.h" 13 | #include "misc.h" 14 | #include "pdev.h" 15 | 16 | /* ========================================================================== */ 17 | 18 | struct platform_device *qc71_platform_dev; 19 | 20 | /* ========================================================================== */ 21 | 22 | static ssize_t fan_reduced_duty_cycle_show(struct device *dev, 23 | struct device_attribute *attr, char *buf) 24 | { 25 | int status = ec_read_byte(BIOS_CTRL_3_ADDR); 26 | 27 | if (status < 0) 28 | return status; 29 | 30 | return sprintf(buf, "%d\n", !!(status & BIOS_CTRL_3_FAN_REDUCED_DUTY_CYCLE)); 31 | } 32 | 33 | static ssize_t fan_reduced_duty_cycle_store(struct device *dev, struct device_attribute *attr, 34 | const char *buf, size_t count) 35 | { 36 | int status; 37 | bool value; 38 | 39 | if (kstrtobool(buf, &value)) 40 | return -EINVAL; 41 | 42 | status = ec_read_byte(BIOS_CTRL_3_ADDR); 43 | if (status < 0) 44 | return status; 45 | 46 | status = SET_BIT(status, BIOS_CTRL_3_FAN_REDUCED_DUTY_CYCLE, value); 47 | 48 | status = ec_write_byte(BIOS_CTRL_3_ADDR, status); 49 | 50 | if (status < 0) 51 | return status; 52 | 53 | return count; 54 | } 55 | 56 | static ssize_t fan_always_on_show(struct device *dev, 57 | struct device_attribute *attr, char *buf) 58 | { 59 | int status = ec_read_byte(BIOS_CTRL_3_ADDR); 60 | 61 | if (status < 0) 62 | return status; 63 | 64 | return sprintf(buf, "%d\n", !!(status & BIOS_CTRL_3_FAN_ALWAYS_ON)); 65 | } 66 | 67 | static ssize_t fan_always_on_store(struct device *dev, struct device_attribute *attr, 68 | const char *buf, size_t count) 69 | { 70 | int status; 71 | bool value; 72 | 73 | if (kstrtobool(buf, &value)) 74 | return -EINVAL; 75 | 76 | status = ec_read_byte(BIOS_CTRL_3_ADDR); 77 | if (status < 0) 78 | return status; 79 | 80 | status = SET_BIT(status, BIOS_CTRL_3_FAN_ALWAYS_ON, value); 81 | 82 | status = ec_write_byte(BIOS_CTRL_3_ADDR, status); 83 | 84 | if (status < 0) 85 | return status; 86 | 87 | return count; 88 | } 89 | 90 | static ssize_t fn_lock_show(struct device *dev, 91 | struct device_attribute *attr, char *buf) 92 | { 93 | int status = qc71_fn_lock_get_state(); 94 | 95 | if (status < 0) 96 | return status; 97 | 98 | return sprintf(buf, "%d\n", status); 99 | } 100 | 101 | static ssize_t fn_lock_store(struct device *dev, struct device_attribute *attr, 102 | const char *buf, size_t count) 103 | { 104 | int status; 105 | bool value; 106 | 107 | if (kstrtobool(buf, &value)) 108 | return -EINVAL; 109 | 110 | status = qc71_fn_lock_set_state(value); 111 | if (status < 0) 112 | return status; 113 | 114 | return count; 115 | } 116 | 117 | static ssize_t fn_lock_switch_show(struct device *dev, 118 | struct device_attribute *attr, char *buf) 119 | { 120 | int status = ec_read_byte(AP_BIOS_BYTE_ADDR); 121 | 122 | if (status < 0) 123 | return status; 124 | 125 | return sprintf(buf, "%d\n", !!(status & AP_BIOS_BYTE_FN_LOCK_SWITCH)); 126 | } 127 | 128 | static ssize_t fn_lock_switch_store(struct device *dev, struct device_attribute *attr, 129 | const char *buf, size_t count) 130 | { 131 | int status; 132 | bool value; 133 | 134 | if (kstrtobool(buf, &value)) 135 | return -EINVAL; 136 | 137 | status = ec_read_byte(AP_BIOS_BYTE_ADDR); 138 | if (status < 0) 139 | return status; 140 | 141 | status = SET_BIT(status, AP_BIOS_BYTE_FN_LOCK_SWITCH, value); 142 | 143 | status = ec_write_byte(AP_BIOS_BYTE_ADDR, status); 144 | 145 | if (status < 0) 146 | return status; 147 | 148 | return count; 149 | } 150 | 151 | static ssize_t manual_control_show(struct device *dev, 152 | struct device_attribute *attr, char *buf) 153 | { 154 | int status = ec_read_byte(CTRL_1_ADDR); 155 | 156 | if (status < 0) 157 | return status; 158 | 159 | return sprintf(buf, "%d\n", !!(status & CTRL_1_MANUAL_MODE)); 160 | } 161 | 162 | static ssize_t manual_control_store(struct device *dev, struct device_attribute *attr, 163 | const char *buf, size_t count) 164 | { 165 | int status; 166 | bool value; 167 | 168 | if (kstrtobool(buf, &value)) 169 | return -EINVAL; 170 | 171 | status = ec_read_byte(CTRL_1_ADDR); 172 | if (status < 0) 173 | return status; 174 | 175 | status = SET_BIT(status, CTRL_1_MANUAL_MODE, value); 176 | 177 | status = ec_write_byte(CTRL_1_ADDR, status); 178 | 179 | if (status < 0) 180 | return status; 181 | 182 | return count; 183 | } 184 | 185 | static ssize_t super_key_lock_show(struct device *dev, 186 | struct device_attribute *attr, char *buf) 187 | { 188 | int status = ec_read_byte(STATUS_1_ADDR); 189 | 190 | if (status < 0) 191 | return status; 192 | 193 | return sprintf(buf, "%d\n", !!(status & STATUS_1_SUPER_KEY_LOCK)); 194 | } 195 | 196 | static ssize_t super_key_lock_store(struct device *dev, struct device_attribute *attr, 197 | const char *buf, size_t count) 198 | { 199 | int status; 200 | bool value; 201 | 202 | if (kstrtobool(buf, &value)) 203 | return -EINVAL; 204 | 205 | status = ec_read_byte(STATUS_1_ADDR); 206 | if (status < 0) 207 | return status; 208 | 209 | if (value != !!(status & STATUS_1_SUPER_KEY_LOCK)) { 210 | status = ec_write_byte(TRIGGER_1_ADDR, TRIGGER_1_SUPER_KEY_LOCK); 211 | 212 | if (status < 0) 213 | return status; 214 | } 215 | 216 | return count; 217 | } 218 | 219 | /* ========================================================================== */ 220 | 221 | static DEVICE_ATTR_RW(fn_lock); 222 | static DEVICE_ATTR_RW(fn_lock_switch); 223 | static DEVICE_ATTR_RW(fan_always_on); 224 | static DEVICE_ATTR_RW(fan_reduced_duty_cycle); 225 | static DEVICE_ATTR_RW(manual_control); 226 | static DEVICE_ATTR_RW(super_key_lock); 227 | 228 | static struct attribute *qc71_laptop_attrs[] = { 229 | &dev_attr_fn_lock.attr, 230 | &dev_attr_fn_lock_switch.attr, 231 | &dev_attr_fan_always_on.attr, 232 | &dev_attr_fan_reduced_duty_cycle.attr, 233 | &dev_attr_manual_control.attr, 234 | &dev_attr_super_key_lock.attr, 235 | NULL 236 | }; 237 | 238 | /* ========================================================================== */ 239 | 240 | static umode_t qc71_laptop_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n) 241 | { 242 | bool ok = false; 243 | 244 | if (attr == &dev_attr_fn_lock.attr || attr == &dev_attr_fn_lock_switch.attr) 245 | ok = qc71_features.fn_lock; 246 | else if (attr == &dev_attr_fan_always_on.attr || attr == &dev_attr_fan_reduced_duty_cycle.attr) 247 | ok = qc71_features.fan_extras; 248 | else if (attr == &dev_attr_manual_control.attr) 249 | ok = true; 250 | else if (attr == &dev_attr_super_key_lock.attr) 251 | ok = qc71_features.super_key_lock; 252 | 253 | return ok ? attr->mode : 0; 254 | } 255 | 256 | /* ========================================================================== */ 257 | 258 | static const struct attribute_group qc71_laptop_group = { 259 | .is_visible = qc71_laptop_attr_is_visible, 260 | .attrs = qc71_laptop_attrs, 261 | }; 262 | 263 | static const struct attribute_group *qc71_laptop_groups[] = { 264 | &qc71_laptop_group, 265 | NULL 266 | }; 267 | 268 | /* ========================================================================== */ 269 | 270 | int __init qc71_pdev_setup(void) 271 | { 272 | int err; 273 | 274 | qc71_platform_dev = platform_device_alloc(KBUILD_MODNAME, PLATFORM_DEVID_NONE); 275 | if (!qc71_platform_dev) 276 | return -ENOMEM; 277 | 278 | qc71_platform_dev->dev.groups = qc71_laptop_groups; 279 | 280 | err = platform_device_add(qc71_platform_dev); 281 | if (err) { 282 | platform_device_put(qc71_platform_dev); 283 | qc71_platform_dev = NULL; 284 | } 285 | 286 | return err; 287 | } 288 | 289 | void qc71_pdev_cleanup(void) 290 | { 291 | /* checks for IS_ERR_OR_NULL() */ 292 | platform_device_unregister(qc71_platform_dev); 293 | } 294 | -------------------------------------------------------------------------------- /ec.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #ifndef QC71_LAPTOP_EC_H 3 | #define QC71_LAPTOP_EC_H 4 | 5 | #include 6 | #include 7 | 8 | /* ========================================================================== */ 9 | /* 10 | * EC register addresses and bitmasks, 11 | * some of them are not used, 12 | * only for documentation 13 | */ 14 | 15 | #define ADDR(page, offset) (((uint16_t)(page) << 8) | ((uint16_t)(offset))) 16 | 17 | /* ========================================================================== */ 18 | 19 | #define AP_BIOS_BYTE_ADDR ADDR(0x07, 0xA4) 20 | #define AP_BIOS_BYTE_FN_LOCK_SWITCH BIT(3) 21 | 22 | /* ========================================================================== */ 23 | 24 | /* battery charger control register */ 25 | #define BATT_CHARGE_CTRL_ADDR ADDR(0x07, 0xB9) 26 | #define BATT_CHARGE_CTRL_VALUE_MASK GENMASK(6, 0) 27 | #define BATT_CHARGE_CTRL_REACHED BIT(7) 28 | 29 | #define BATT_STATUS_ADDR ADDR(0x04, 0x32) 30 | #define BATT_STATUS_DISCHARGING BIT(0) 31 | 32 | /* possibly temp (in C) = value / 10 + X */ 33 | #define BATT_TEMP_ADDR ADDR(0x04, 0xA2) 34 | 35 | #define BATT_ALERT_ADDR ADDR(0x04, 0x94) 36 | 37 | #define BIOS_CTRL_1_ADDR ADDR(0x07, 0x4E) 38 | #define BIOS_CTRL_1_FN_LOCK_STATUS BIT(4) 39 | 40 | #define BIOS_CTRL_2_ADDR ADDR(0x07, 0x82) 41 | #define BIOS_CTRL_2_FAN_V2_NEW BIT(0) 42 | #define BIOS_CTRL_2_FAN_QKEY BIT(1) 43 | #define BIOS_CTRL_2_OFFICE_MODE_FAN_TABLE_TYPE BIT(2) 44 | #define BIOS_CTRL_2_FAN_V3 BIT(3) 45 | #define BIOS_CTRL_2_DEFAULT_MODE BIT(4) 46 | 47 | /* 3rd control register of a different kind */ 48 | #define BIOS_CTRL_3_ADDR ADDR(0x7, 0xA3) 49 | #define BIOS_CTRL_3_FAN_REDUCED_DUTY_CYCLE BIT(5) 50 | #define BIOS_CTRL_3_FAN_ALWAYS_ON BIT(6) 51 | 52 | #define BIOS_INFO_1_ADDR ADDR(0x04, 0x9F) 53 | #define BIOS_INFO_5_ADDR ADDR(0x04, 0x66) 54 | 55 | /* ========================================================================== */ 56 | 57 | #define CTRL_1_ADDR ADDR(0x07, 0x41) 58 | #define CTRL_1_MANUAL_MODE BIT(0) 59 | #define CTRL_1_ITE_KBD_EFFECT_REACTIVE BIT(3) 60 | #define CTRL_1_FAN_ABNORMAL BIT(5) 61 | 62 | #define CTRL_2_ADDR ADDR(0x07, 0x8C) 63 | #define CTRL_2_SINGLE_COLOR_KEYBOARD BIT(0) 64 | #define CTRL_2_SINGLE_COLOR_KBD_BL_OFF BIT(1) 65 | #define CTRL_2_TURBO_LEVEL_MASK GENMASK(3, 2) 66 | #define CTRL_2_TURBO_LEVEL_0 0x00 67 | #define CTRL_2_TURBO_LEVEL_1 BIT(2) 68 | #define CTRL_2_TURBO_LEVEL_2 BIT(3) 69 | #define CTRL_2_TURBO_LEVEL_3 (BIT(2) | BIT(3)) 70 | // #define CTRL_2_SINGLE_COLOR_KBD_? BIT(4) 71 | #define CTRL_2_SINGLE_COLOR_KBD_BRIGHTNESS GENMASK(7, 5) 72 | 73 | #define CTRL_3_ADDR ADDR(0x07, 0xA5) 74 | #define CTRL_3_PWR_LED_MASK GENMASK(1, 0) 75 | #define CTRL_3_PWR_LED_NONE BIT(1) 76 | #define CTRL_3_PWR_LED_BOTH BIT(0) 77 | #define CTRL_3_PWR_LED_LEFT 0x00 78 | #define CTRL_3_FAN_QUIET BIT(2) 79 | #define CTRL_3_OVERBOOST BIT(4) 80 | #define CTRL_3_HIGH_PWR BIT(7) 81 | 82 | #define CTRL_4_ADDR ADDR(0x07, 0xA6) 83 | #define CTRL_4_OVERBOOST_DYN_TEMP_OFF BIT(1) 84 | #define CTRL_4_TOUCHPAD_TOGGLE_OFF BIT(6) 85 | 86 | #define CTRL_5_ADDR ADDR(0x07, 0xC5) 87 | 88 | #define CTRL_6_ADDR ADDR(0x07, 0x8E) 89 | 90 | /* ========================================================================== */ 91 | 92 | #define DEVICE_STATUS_ADDR ADDR(0x04, 0x7B) 93 | #define DEVICE_STATUS_WIFI_ON BIT(7) 94 | /* BIT(5) is seemingly also (un)set depending on the rfkill state (bluetooth?) */ 95 | 96 | /* ========================================================================== */ 97 | 98 | /* fan control register */ 99 | #define FAN_CTRL_ADDR ADDR(0x07, 0x51) 100 | #define FAN_CTRL_LEVEL_MASK GENMASK(2, 0) 101 | #define FAN_CTRL_TURBO BIT(4) 102 | #define FAN_CTRL_AUTO BIT(5) 103 | #define FAN_CTRL_FAN_BOOST BIT(6) 104 | 105 | #define FAN_RPM_1_ADDR ADDR(0x04, 0x64) 106 | #define FAN_RPM_2_ADDR ADDR(0x04, 0x6C) 107 | 108 | #define FAN_PWM_1_ADDR ADDR(0x18, 0x04) 109 | #define FAN_PWM_2_ADDR ADDR(0x18, 0x09) 110 | 111 | #define FAN_TEMP_1_ADDR ADDR(0x04, 0x3e) 112 | #define FAN_TEMP_2_ADDR ADDR(0x04, 0x4f) 113 | 114 | #define FAN_MODE_INDEX_ADDR ADDR(0x07, 0xAB) 115 | #define FAN_MODE_INDEX_LOW_MASK GENMASK(3, 0) 116 | #define FAN_MODE_INDEX_HIGH_MASK GENMASK(7, 4) 117 | 118 | /* ========================================================================== */ 119 | 120 | /* 121 | * the actual keyboard type is seemingly determined from this number, 122 | * the project id, the controller firmware version, 123 | * and the HID usage page of the descriptor of the controller 124 | */ 125 | #define KEYBOARD_TYPE_ADDR ADDR(0x07, 0x3C) 126 | #define KEYBOARD_TYPE_101 25 127 | #define KEYBOARD_TYPE_101M 41 128 | #define KEYBOARD_TYPE_102 17 129 | #define KEYBOARD_TYPE_102M 33 130 | #define KEYBOARD_TYPE_85 25 131 | #define KEYBOARD_TYPE_86 17 132 | #define KEYBOARD_TYPE_87 73 133 | #define KEYBOARD_TYPE_88 65 134 | #define KEYBOARD_TYPE_97 57 135 | #define KEYBOARD_TYPE_98 49 136 | #define KEYBOARD_TYPE_99 121 137 | #define KEYBOARD_TYPE_100 113 138 | 139 | /* ========================================================================== */ 140 | 141 | /* lightbar control register */ 142 | #define LIGHTBAR_CTRL_ADDR ADDR(0x07, 0x48) 143 | #define LIGHTBAR_CTRL_POWER_SAVE BIT(1) 144 | #define LIGHTBAR_CTRL_S0_OFF BIT(2) 145 | #define LIGHTBAR_CTRL_S3_OFF BIT(3) 146 | #define LIGHTBAR_CTRL_RAINBOW BIT(7) 147 | 148 | #define LIGHTBAR_RED_ADDR ADDR(0x07, 0x49) 149 | #define LIGHTBAR_GREEN_ADDR ADDR(0x07, 0x4A) 150 | #define LIGHTBAR_BLUE_ADDR ADDR(0x07, 0x4B) 151 | 152 | /* ========================================================================== */ 153 | 154 | #define PROJ_ID_ADDR ADDR(0x07, 0x40) 155 | #define PROJ_ID_GIxKN 1 156 | #define PROJ_ID_GJxKN 2 157 | #define PROJ_ID_GKxCN 3 158 | #define PROJ_ID_GIxCN 4 159 | #define PROJ_ID_GJxCN 5 160 | #define PROJ_ID_GK5CN_X 6 161 | #define PROJ_ID_GK7CN_S 7 162 | #define PROJ_ID_GK7CPCS_GK5CQ7Z 8 163 | #define PROJ_ID_IDP 11 164 | #define PROJ_ID_ID6Y 12 165 | #define PROJ_ID_ID7Y 13 166 | #define PROJ_ID_PF4MU_PF4MN_PF5MU 14 167 | #define PROJ_ID_CML_GAMING 15 168 | #define PROJ_ID_GK7NXXR 16 169 | #define PROJ_ID_GM5MU1Y 17 170 | 171 | /* ========================================================================== */ 172 | 173 | #define STATUS_1_ADDR ADDR(0x07, 0x68) 174 | #define STATUS_1_SUPER_KEY_LOCK BIT(0) 175 | #define STATUS_1_LIGHTBAR BIT(1) 176 | #define STATUS_1_FAN_BOOST BIT(2) 177 | 178 | #define SUPPORT_1_ADDR ADDR(0x07, 0x65) 179 | #define SUPPORT_1_AIRPLANE_MODE BIT(0) 180 | #define SUPPORT_1_GPS_SWITCH BIT(1) 181 | #define SUPPORT_1_OVERCLOCK BIT(2) 182 | #define SUPPORT_1_MACRO_KEY BIT(3) 183 | #define SUPPORT_1_SHORTCUT_KEY BIT(4) 184 | #define SUPPORT_1_SUPER_KEY_LOCK BIT(5) 185 | #define SUPPORT_1_LIGHTBAR BIT(6) 186 | #define SUPPORT_1_FAN_BOOST BIT(7) 187 | 188 | #define SUPPORT_2_ADDR ADDR(0x07, 0x66) 189 | #define SUPPORT_2_SILENT_MODE BIT(0) 190 | #define SUPPORT_2_USB_CHARGING BIT(1) 191 | #define SUPPORT_2_SINGLE_ZONE_KBD BIT(2) 192 | #define SUPPORT_2_CHINA_MODE BIT(5) 193 | #define SUPPORT_2_MY_BATTERY BIT(6) 194 | 195 | #define SUPPORT_5_ADDR ADDR(0x07, 0x42) 196 | #define SUPPORT_5_FAN_TURBO BIT(4) 197 | #define SUPPORT_5_FAN BIT(5) 198 | 199 | #define SUPPORT_6 ADDR(0x07, 0x8E) 200 | #define SUPPORT_6_FAN3P5 BIT(1) 201 | 202 | /* ========================================================================== */ 203 | 204 | #define TRIGGER_1_ADDR ADDR(0x07, 0x67) 205 | #define TRIGGER_1_SUPER_KEY_LOCK BIT(0) 206 | #define TRIGGER_1_LIGHTBAR BIT(1) 207 | #define TRIGGER_1_FAN_BOOST BIT(2) 208 | #define TRIGGER_1_SILENT_MODE BIT(3) 209 | #define TRIGGER_1_USB_CHARGING BIT(4) 210 | 211 | #define TRIGGER_2_ADDR ADDR(0x07, 0x5D) 212 | 213 | /* ========================================================================== */ 214 | 215 | #define PLATFORM_ID_ADDR ADDR(0x04, 0x56) 216 | #define POWER_STATUS_ADDR ADDR(0x04, 0x5E) 217 | #define POWER_SOURCE_ADDR ADDR(0x04, 0x90) 218 | 219 | #define PL1_ADDR ADDR(0x07, 0x83) 220 | #define PL2_ADDR ADDR(0x07, 0x84) 221 | #define PL4_ADDR ADDR(0x07, 0x85) 222 | 223 | /* ========================================================================== */ 224 | 225 | union qc71_ec_result { 226 | uint32_t dword; 227 | struct { 228 | uint16_t w1; 229 | uint16_t w2; 230 | } words; 231 | struct { 232 | uint8_t b1; 233 | uint8_t b2; 234 | uint8_t b3; 235 | uint8_t b4; 236 | } bytes; 237 | }; 238 | 239 | int __must_check qc71_ec_transaction(uint16_t addr, uint16_t data, 240 | union qc71_ec_result *result, bool read); 241 | 242 | static inline __must_check int qc71_ec_read(uint16_t addr, union qc71_ec_result *result) 243 | { 244 | return qc71_ec_transaction(addr, 0, result, true); 245 | } 246 | 247 | static inline __must_check int qc71_ec_write(uint16_t addr, uint16_t data) 248 | { 249 | return qc71_ec_transaction(addr, data, NULL, false); 250 | } 251 | 252 | static inline __must_check int ec_write_byte(uint16_t addr, uint8_t data) 253 | { 254 | return qc71_ec_write(addr, data); 255 | } 256 | 257 | static inline __must_check int ec_read_byte(uint16_t addr) 258 | { 259 | union qc71_ec_result result; 260 | int err; 261 | 262 | err = qc71_ec_read(addr, &result); 263 | 264 | if (err) 265 | return err; 266 | 267 | return result.bytes.b1; 268 | } 269 | 270 | #endif /* QC71_LAPTOP_EC_H */ 271 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # What is it? 2 | This a Linux kernel platform driver for Intel Whitebook LAPQC71X systems (XMG Fusion 15, Eluktronics MAG 15, Aftershock Vapor 15, ...). 3 | 4 | 5 | # Disclaimer 6 | **This software is in early stages of developement. Futhermore, to quote GPL: everything is provided as is. There is no warranty for the program, to the extent permitted by applicable law.** 7 | 8 | **This software is licensed under the GNU General Public License v2.0** 9 | 10 | 11 | # Compatibility 12 | It has only been tested on an XMG Fusion 15 device (BIOS 0062 up to 0120) and with the `5.4`, and `5.8`-`5.13` kernel series. 13 | Some functions have been confirmed to work on the Tongfang GK7C chassis (XMG Neo 17, PCS Recoil III, Walmart OP17) (see [#6][issue6]). 14 | 15 | 16 | # Dependencies 17 | ### Required 18 | * Your kernel has been compiled with `CONFIG_ACPI` and `CONFIG_DMI` (it probably was) 19 | * Linux headers for you current kernel 20 | 21 | ### Optional 22 | * DKMS if you don't want to recompile it manually every time the kernel is upgraded 23 | 24 | 25 | # Features 26 | ## Current 27 | * Integrate fan speeds into the Linux hardware monitoring subsystem (so that `lm_sensors` can pick it up) 28 | * Control the lightbar 29 | * Enable/disable always-on mode, reduced fan duty cycle (BIOS 0114 and above) 30 | * Fn lock (BIOS 0114 and above) 31 | * Change battery charge limit (BIOS 0114 and above) 32 | 33 | 34 | # How to install 35 | ## Downloading 36 | If you have `git` installed: 37 | ``` 38 | git clone https://github.com/pobrn/qc71_laptop 39 | ``` 40 | 41 | If you don't, then you can download it [here](https://github.com/pobrn/qc71_laptop/archive/master.zip). 42 | 43 | ## Installing 44 | ### Linux headers 45 | On Debian and its [many][debian-derivatives] [derivatives][ubuntu-derivatives] (Ubuntu, Pop OS, Linux Mint, ...) , run 46 | ``` 47 | sudo apt install linux-headers-$(uname -r) 48 | ``` 49 | to install the necessary header files. 50 | 51 | On Arch Linux and its derivatives (Manjaro, ...), run 52 | ``` 53 | sudo pacman -Syu linux-headers 54 | ``` 55 | 56 | ### DKMS (optional) 57 | DKMS should be in your distributions repositories. `sudo apt install dkms`, `sudo pacman -Syu dkms` should work depending on your distribution. 58 | 59 | ### The module 60 | #### Manually 61 | Navigate in a terminal into the directory, then execute `make`. This should compile the module. If everything went correctly, a file named `qc71_laptop.ko` should appear in the directory. 62 | 63 | To test the module try `sudo insmod qc71_laptop.ko`. Now you should see the fan speeds appear in the output of `sensors`, and the directory `/sys/devices/platform/qc71_laptop` should now exist. If you are done testing, unload the module using `sudo rmmod qc71_laptop`. 64 | 65 | Now you could create a script that inserts this module at boot from this directory, or you could install it using DKMS. 66 | 67 | #### With DKMS 68 | Run 69 | ``` 70 | sudo make dkmsinstall 71 | ``` 72 | to install the module with DKMS. Or run 73 | ``` 74 | sudo make dkmsuninstall 75 | ``` 76 | to uninstall the module. 77 | 78 | The module should automatically load at boot after this. If you want to load it immediately, run `sudo modprobe qc71_laptop`. If it reports an error, and you're convinced your device should be supported, please open an [issue][issues]. 79 | 80 | ## Upgrade 81 | 82 | If you installed the module with DKMS, and you wish to upgrade, first open the directory of the old sources, and run 83 | ``` 84 | sudo make dkmsuninstall 85 | ``` 86 | then update the sources (pull the repository, download the sources again manually, etc.), then run 87 | ``` 88 | sudo make dkmsinstall 89 | ``` 90 | 91 | 92 | # How to use 93 | ## Fan speeds 94 | After loading the module the fan speeds and temperatures should immediately appear in the output of `sensors`, and all your favourite monitoring utilities (e.g. the [Freon][gnome-ext-freon] GNOME shell extension) that use `sensors`. 95 | 96 | ## Controlling the lightbar 97 | The lightbar is integrated into the LED subsystem of the linux kernel. When the module is loaded, `/sys/class/leds/qc71_laptop::lightbar` directory should exist with the following important files: 98 | ``` 99 | /sys/class/leds/qc71_laptop::lightbar/brightness 100 | ``` 101 | 102 | It contains `1` if the lightbar is turned on in S0 sleep state (aka. when the device is powered on), `0` otherwise. You can turn on/off the lightbar by writing an appropriate number into this file: 103 | ``` 104 | # echo 1 > /sys/class/leds/qc71_laptop::lightbar/brightness 105 | ``` 106 | will turn the lightbar on. (Writing `0` will turn it off.) 107 | To check the current state: 108 | ``` 109 | $ cat /sys/class/leds/qc71_laptop::lightbar/brightness 110 | ``` 111 | 112 | ___ 113 | ``` 114 | /sys/class/leds/qc71_laptop::lightbar/brightness_s3 115 | ``` 116 | It contains `1` if the lightbar is turned on in S3 sleep state (aka. when the device is sleeping). If it is `1`, the lightbar will "breathe" when the device is sleeping. You can control it the same way as `brightness`. 117 | 118 | ___ 119 | ``` 120 | /sys/class/leds/qc71_laptop::lightbar/rainbow_mode 121 | ``` 122 | It contains `1` if the "rainbow mode" is enabled (aka. the color will continuously cycle). Controlling works the same way as before. 123 | 124 | *Note:* Enabling/disabling the rainbow mode will not turn the lightbar on/off. 125 | *Note:* The rainbow mode takes precedence over the color. 126 | 127 | ___ 128 | ``` 129 | /sys/class/leds/qc71_laptop::lightbar/color 130 | ``` 131 | This file controls the color of the lightbar. It is a three digit number (possibly with padding zeroes). The first digit is the red component, the second one is the green componenet, the third one is the blue component. 132 | ``` 133 | $ cat /sys/class/leds/qc71_laptop::lightbar/color 134 | ``` 135 | tells you the current color, while 136 | ``` 137 | # echo 591 > /sys/class/leds/qc71_laptop::lightbar/color 138 | ``` 139 | will change the current color. 140 | 141 | *Note:* Chaning the color will not turn the lightbar on. 142 | 143 | 144 | ## Controlling the fans 145 | These can be controlled directly from the BIOS as well. 146 | 147 | ### Passive cooling 148 | I call this feature "always on" because that's less confusing than "passive cooling". 149 | ``` 150 | # echo 1 > /sys/devices/platform/qc71_laptop/fan_always_on 151 | ``` 152 | will cause the fans to run continuously. Writing `0` will turn it off. 153 | 154 | ### Reduced duty cycle 155 | ``` 156 | # echo 1 > /sys/devices/platform/qc71_laptop/fan_reduced_duty_cycle 157 | ``` 158 | will cause the fans to run at 25% of their capacity (about 2300 RPM) at idle (instead of 30% - about 2700 RPM). Writing `0` will restore the 30% idle duty cycle. 159 | 160 | ## Fn lock 161 | ``` 162 | # echo 1 > /sys/devices/platform/qc71_laptop/fn_lock_switch 163 | ``` 164 | will enable changing the Fn lock state by pressing Fn+ESC. If this file contains `1`, then pressing Fn+ESC will toggle the Fn lock; if this file contains `0`, then pressing Fn+ESC will have no effect on the Fn lock. 165 | 166 | ``` 167 | # echo 1 > /sys/devices/platform/qc71_laptop/fn_lock 168 | ``` 169 | will directly enable the Fn lock. If this file contains `1`, then pressing the functions keys will trigger their secondary functions (mute, brightness up, etc.); if this file contains `0`, then pressing the functions keys will trigger their primary functions (F1, F2, ...). 170 | 171 | ## Battery charge limit 172 | The file `/sys/class/power_supply/BAT0/charge_control_end_threshold` contains the current charge threshold. Writing a number between 1 and 100 will cause the battery charging limit to be set to that percentage. (I did not test extremely low values, so I cannot say if they work). For example: 173 | ``` 174 | # echo 60 > /sys/class/power_supply/BAT0/charge_control_end_threshold 175 | ``` 176 | will cause charging to stop when the battery reaches 60% of its capacity. 177 | 178 | ## Super key (windows key) lock 179 | It is possible to disable the super (windows) key by pressing Fn+F2 (or just F2 if the Fn lock is enabled). This can be also achieved by changing the writing the appropriate value into `/sys/devices/platform/qc71_laptop/super_key_lock`. 180 | ``` 181 | # echo 1 > /sys/devices/platform/qc71_laptop/super_key_lock 182 | ``` 183 | disables the super key, while 184 | ``` 185 | # echo 0 > /sys/devices/platform/qc71_laptop/super_key_lock 186 | ``` 187 | enables it. Reading the file will provide information about the current state of the super key. `0` means enabled, `1` means disabled. 188 | 189 | ## Example use 190 | 191 | The XMG Control Center can change the color if the device is on battery or plugged in. Fortunately you can easily achieve the same using [acpid](https://wiki.archlinux.org/index.php/Acpid). Modifying the appropriate part of `/etc/acpi/handler.sh` like this: 192 | ``` 193 | ac_adapter) 194 | case "$2" in 195 | AC|ACAD|ADP0|ACPI0003:00) # the "ACPI0003:00" part was not there by default 196 | case "$4" in 197 | 00000000) 198 | logger 'AC unplugged' 199 | 200 | # change color to red 201 | echo 900 > /sys/class/leds/qc71_laptop::lightbar/color 202 | ;; 203 | 00000001) 204 | logger 'AC plugged' 205 | 206 | # change color to blue 207 | echo 009 > /sys/class/leds/qc71_laptop::lightbar/color 208 | ;; 209 | esac 210 | ;; 211 | *) 212 | logger "ACPI action undefined: $2" 213 | ;; 214 | esac 215 | ;; 216 | ``` 217 | You can use `acpi_listen` to see what events are generated when you plug the machine in or disconnect the charger. You might need to modify the third line (in this snippet). 218 | 219 | 220 | # Troubleshooting 221 | 222 | * The [TUXEDO Control Center][tcc-github] may interfere with the operation of this kernel module. I do not recommend using both at the same time. 223 | 224 | 225 | [issue6]: https://github.com/pobrn/qc71_laptop/issues/6 226 | [debian-derivatives]: https://www.debian.org/derivatives/ 227 | [ubuntu-derivatives]: https://wiki.ubuntu.com/DerivativeTeam/Derivatives 228 | [issues]: https://github.com/pobrn/qc71_laptop/issues 229 | [gnome-ext-freon]: https://extensions.gnome.org/extension/841/freon/ 230 | [tcc-github]: https://github.com/tuxedocomputers/tuxedo-control-center 231 | -------------------------------------------------------------------------------- /led_lightbar.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #include "pr.h" 3 | 4 | #include 5 | /* #include */ 6 | #include 7 | #include 8 | #include 9 | 10 | #include "util.h" 11 | #include "ec.h" 12 | #include "features.h" 13 | #include "led_lightbar.h" 14 | #include "pdev.h" 15 | 16 | /* ========================================================================== */ 17 | 18 | #if IS_ENABLED(CONFIG_LEDS_CLASS) 19 | 20 | enum qc71_lightbar_color { 21 | LIGHTBAR_RED = 0, 22 | LIGHTBAR_GREEN = 1, 23 | LIGHTBAR_BLUE = 2, 24 | LIGHTBAR_COLOR_COUNT 25 | }; 26 | 27 | static const uint16_t lightbar_color_addrs[LIGHTBAR_COLOR_COUNT] = { 28 | [LIGHTBAR_RED] = LIGHTBAR_RED_ADDR, 29 | [LIGHTBAR_GREEN] = LIGHTBAR_GREEN_ADDR, 30 | [LIGHTBAR_BLUE] = LIGHTBAR_BLUE_ADDR, 31 | }; 32 | 33 | static const uint8_t lightbar_colors[LIGHTBAR_COLOR_COUNT] = { 34 | LIGHTBAR_RED, 35 | LIGHTBAR_GREEN, 36 | LIGHTBAR_BLUE, 37 | }; 38 | 39 | /* f(x) = 4x */ 40 | static const uint8_t lightbar_color_values[LIGHTBAR_COLOR_COUNT][10] = { 41 | [LIGHTBAR_RED] = {0, 4, 8, 12, 16, 20, 24, 28, 32, 36}, 42 | [LIGHTBAR_GREEN] = {0, 4, 8, 12, 16, 20, 24, 28, 32, 36}, 43 | [LIGHTBAR_BLUE] = {0, 4, 8, 12, 16, 20, 24, 28, 32, 36}, 44 | }; 45 | 46 | /* inverse of 'lightbar_color_values' */ 47 | static const uint8_t lightbar_pwm_to_level[LIGHTBAR_COLOR_COUNT][256] = { 48 | [LIGHTBAR_RED] = { 49 | [0] = 0, 50 | [4] = 1, 51 | [8] = 2, 52 | [12] = 3, 53 | [16] = 4, 54 | [20] = 5, 55 | [24] = 6, 56 | [28] = 7, 57 | [32] = 8, 58 | [36] = 9, 59 | }, 60 | 61 | [LIGHTBAR_GREEN] = { 62 | [0] = 0, 63 | [4] = 1, 64 | [8] = 2, 65 | [12] = 3, 66 | [16] = 4, 67 | [20] = 5, 68 | [24] = 6, 69 | [28] = 7, 70 | [32] = 8, 71 | [36] = 9, 72 | }, 73 | 74 | [LIGHTBAR_BLUE] = { 75 | [0] = 0, 76 | [4] = 1, 77 | [8] = 2, 78 | [12] = 3, 79 | [16] = 4, 80 | [20] = 5, 81 | [24] = 6, 82 | [28] = 7, 83 | [32] = 8, 84 | [36] = 9, 85 | }, 86 | }; 87 | 88 | 89 | /* ========================================================================== */ 90 | 91 | static bool nolightbar; 92 | module_param(nolightbar, bool, 0444); 93 | MODULE_PARM_DESC(nolightbar, "do not register the lightbar to the leds subsystem (default=false)"); 94 | 95 | static bool lightbar_led_registered; 96 | 97 | /* ========================================================================== */ 98 | 99 | static inline int qc71_lightbar_get_status(void) 100 | { 101 | return ec_read_byte(LIGHTBAR_CTRL_ADDR); 102 | } 103 | 104 | static inline int qc71_lightbar_write_ctrl(uint8_t ctrl) 105 | { 106 | return ec_write_byte(LIGHTBAR_CTRL_ADDR, ctrl); 107 | } 108 | 109 | /* ========================================================================== */ 110 | 111 | static int qc71_lightbar_switch(uint8_t mask, bool on) 112 | { 113 | int status; 114 | 115 | if (mask != LIGHTBAR_CTRL_S0_OFF && mask != LIGHTBAR_CTRL_S3_OFF) 116 | return -EINVAL; 117 | 118 | status = qc71_lightbar_get_status(); 119 | 120 | if (status < 0) 121 | return status; 122 | 123 | return qc71_lightbar_write_ctrl(SET_BIT(status, mask, !on)); 124 | } 125 | 126 | static int qc71_lightbar_set_color_level(uint8_t color, uint8_t level) 127 | { 128 | if (color >= ARRAY_SIZE(lightbar_color_addrs)) 129 | return -EINVAL; 130 | 131 | if (level >= ARRAY_SIZE(lightbar_color_values[color])) 132 | return -EINVAL; 133 | 134 | return ec_write_byte(lightbar_color_addrs[color], lightbar_color_values[color][level]); 135 | } 136 | 137 | static int qc71_lightbar_get_color_level(uint8_t color) 138 | { 139 | int err; 140 | 141 | if (color >= ARRAY_SIZE(lightbar_color_addrs)) 142 | return -EINVAL; 143 | 144 | err = ec_read_byte(lightbar_color_addrs[color]); 145 | if (err < 0) 146 | return err; 147 | 148 | return lightbar_pwm_to_level[color][err]; 149 | } 150 | 151 | static int qc71_lightbar_set_rainbow_mode(bool on) 152 | { 153 | int status = qc71_lightbar_get_status(); 154 | 155 | if (status < 0) 156 | return status; 157 | 158 | return qc71_lightbar_write_ctrl(SET_BIT(status, LIGHTBAR_CTRL_RAINBOW, on)); 159 | } 160 | 161 | static int qc71_lightbar_set_color(unsigned int color) 162 | { 163 | int err = 0, i; 164 | 165 | if (color > 999) /* color must lie in [0, 999] */ 166 | return -EINVAL; 167 | 168 | for (i = ARRAY_SIZE(lightbar_colors) - 1; i >= 0 && !err; i--) 169 | err = qc71_lightbar_set_color_level(lightbar_colors[i], 170 | do_div(color, 10)); 171 | 172 | return err; 173 | } 174 | 175 | /* ========================================================================== */ 176 | /* lightbar attrs */ 177 | 178 | static ssize_t lightbar_s3_show(struct device *dev, 179 | struct device_attribute *attr, char *buf) 180 | { 181 | int value = qc71_lightbar_get_status(); 182 | 183 | if (value < 0) 184 | return value; 185 | 186 | return sprintf(buf, "%d\n", !(value & LIGHTBAR_CTRL_S3_OFF)); 187 | } 188 | 189 | static ssize_t lightbar_s3_store(struct device *dev, struct device_attribute *attr, 190 | const char *buf, size_t count) 191 | { 192 | int err; 193 | bool value; 194 | 195 | if (kstrtobool(buf, &value)) 196 | return -EINVAL; 197 | 198 | err = qc71_lightbar_switch(LIGHTBAR_CTRL_S3_OFF, value); 199 | 200 | if (err) 201 | return err; 202 | 203 | return count; 204 | } 205 | 206 | static ssize_t lightbar_color_show(struct device *dev, 207 | struct device_attribute *attr, char *buf) 208 | { 209 | unsigned int color = 0; 210 | size_t i; 211 | 212 | for (i = 0; i < ARRAY_SIZE(lightbar_colors); i++) { 213 | int level = qc71_lightbar_get_color_level(lightbar_colors[i]); 214 | 215 | if (level < 0) 216 | return level; 217 | 218 | color *= 10; 219 | 220 | if (0 <= level && level <= 9) 221 | color += level; 222 | } 223 | 224 | return sprintf(buf, "%03u\n", color); 225 | } 226 | 227 | static ssize_t lightbar_color_store(struct device *dev, struct device_attribute *attr, 228 | const char *buf, size_t count) 229 | { 230 | unsigned int value; 231 | int err; 232 | 233 | if (kstrtouint(buf, 10, &value)) 234 | return -EINVAL; 235 | 236 | err = qc71_lightbar_set_color(value); 237 | if (err) 238 | return err; 239 | 240 | return count; 241 | } 242 | 243 | static ssize_t lightbar_rainbow_show(struct device *dev, 244 | struct device_attribute *attr, char *buf) 245 | { 246 | int status = qc71_lightbar_get_status(); 247 | 248 | if (status < 0) 249 | return status; 250 | 251 | return sprintf(buf, "%d\n", !!(status & LIGHTBAR_CTRL_RAINBOW)); 252 | } 253 | 254 | static ssize_t lightbar_rainbow_store(struct device *dev, struct device_attribute *attr, 255 | const char *buf, size_t count) 256 | { 257 | int err; 258 | bool value; 259 | 260 | if (kstrtobool(buf, &value)) 261 | return -EINVAL; 262 | 263 | err = qc71_lightbar_set_rainbow_mode(value); 264 | 265 | if (err) 266 | return err; 267 | 268 | return count; 269 | } 270 | 271 | static enum led_brightness qc71_lightbar_led_get_brightness(struct led_classdev *led_cdev) 272 | { 273 | int err = qc71_lightbar_get_status(); 274 | 275 | if (err) 276 | return 0; 277 | 278 | return !(err & LIGHTBAR_CTRL_S0_OFF); 279 | } 280 | 281 | static int qc71_lightbar_led_set_brightness(struct led_classdev *led_cdev, 282 | enum led_brightness value) 283 | { 284 | return qc71_lightbar_switch(LIGHTBAR_CTRL_S0_OFF, !!value); 285 | } 286 | 287 | #if 0 288 | static int qc71_lightbar_led_set_brightness(struct led_classdev *led_cdev, 289 | enum led_brightness brightness) 290 | { 291 | struct led_classdev_mc *led_mc_cdev = lcdev_to_mccdev(led_cdev); 292 | unsigned int color = 0, i; 293 | int err; 294 | 295 | led_mc_calc_color_components(led_mc_cdev, brightness); 296 | 297 | for (i = 0; i < led_mc_cdev->num_colors; i++) { 298 | if (led_mc_cdev->subled_info[i].brightness > 9) 299 | return -EINVAL; 300 | 301 | color = 10 * color + led_mc_cdev->subled_info[i].brightness; 302 | } 303 | 304 | if (color) { 305 | err = qc71_lightbar_switch(LIGHTBAR_CTRL_S0_OFF, 1); 306 | 307 | if (err) 308 | goto out; 309 | 310 | err = qc71_lightbar_set_color(color); 311 | } else { 312 | err = qc71_lightbar_switch(LIGHTBAR_CTRL_S0_OFF, 0); 313 | } 314 | 315 | out: 316 | return err; 317 | } 318 | #endif 319 | 320 | /* ========================================================================== */ 321 | 322 | static DEVICE_ATTR(brightness_s3, 0644, lightbar_s3_show, lightbar_s3_store); 323 | static DEVICE_ATTR(color, 0644, lightbar_color_show, lightbar_color_store); 324 | static DEVICE_ATTR(rainbow_mode, 0644, lightbar_rainbow_show, lightbar_rainbow_store); 325 | 326 | static struct attribute *qc71_lightbar_led_attrs[] = { 327 | &dev_attr_brightness_s3.attr, 328 | &dev_attr_color.attr, 329 | &dev_attr_rainbow_mode.attr, 330 | NULL 331 | }; 332 | 333 | ATTRIBUTE_GROUPS(qc71_lightbar_led); 334 | 335 | static struct led_classdev qc71_lightbar_led = { 336 | .name = KBUILD_MODNAME "::lightbar", 337 | .max_brightness = 1, 338 | .brightness_get = qc71_lightbar_led_get_brightness, 339 | .brightness_set_blocking = qc71_lightbar_led_set_brightness, 340 | .groups = qc71_lightbar_led_groups, 341 | }; 342 | 343 | #if 0 344 | static struct mc_subled qc71_lightbar_subleds[LIGHTBAR_COLOR_COUNT] = { 345 | [LIGHTBAR_RED] = { 346 | .color_index = LED_COLOR_ID_RED, 347 | }, 348 | [LIGHTBAR_GREEN] = { 349 | .color_index = LED_COLOR_ID_GREEN, 350 | }, 351 | [LIGHTBAR_BLUE] = { 352 | .color_index = LED_COLOR_ID_BLUE, 353 | }, 354 | }; 355 | 356 | static struct led_classdev_mc qc71_lightbar_led = { 357 | .num_colors = ARRAY_SIZE(qc71_lightbar_subleds), 358 | .subled_info = qc71_lightbar_subleds, 359 | .led_cdev = { 360 | .name = KBUILD_MODNAME "::lightbar", 361 | .max_brightness = 9, 362 | .brightness_set_blocking = qc71_lightbar_led_set_brightness, 363 | }, 364 | }; 365 | #endif 366 | 367 | /* ========================================================================== */ 368 | 369 | int __init qc71_led_lightbar_setup(void) 370 | { 371 | int err; 372 | 373 | if (nolightbar || !qc71_features.lightbar) 374 | return -ENODEV; 375 | 376 | #if 0 377 | err = led_classdev_multicolor_register(&qc71_platform_dev->dev, &qc71_lightbar_led); 378 | #endif 379 | err = led_classdev_register(&qc71_platform_dev->dev, &qc71_lightbar_led); 380 | 381 | if (!err) 382 | lightbar_led_registered = true; 383 | 384 | #if 0 385 | err = device_add_groups(qc71_lightbar_led.led_cdev.dev, qc71_lightbar_led_groups); 386 | if (err) 387 | led_classdev_multicolor_unregister(&qc71_lightbar_led); 388 | #endif 389 | 390 | return err; 391 | } 392 | 393 | void qc71_led_lightbar_cleanup(void) 394 | { 395 | if (lightbar_led_registered) { 396 | #if 0 397 | device_remove_groups(qc71_lightbar_led.led_cdev.dev, qc71_lightbar_led_groups); 398 | led_classdev_multicolor_unregister(&qc71_lightbar_led); 399 | #endif 400 | led_classdev_unregister(&qc71_lightbar_led); 401 | } 402 | } 403 | 404 | #endif 405 | -------------------------------------------------------------------------------- /events.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #include "pr.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "events.h" 14 | #include "misc.h" 15 | #include "pdev.h" 16 | #include "wmi.h" 17 | 18 | /* ========================================================================== */ 19 | 20 | #define KBD_BL_LED_SUFFIX ":" LED_FUNCTION_KBD_BACKLIGHT 21 | 22 | /* ========================================================================== */ 23 | 24 | static struct { 25 | const char *guid; 26 | bool handler_installed; 27 | } qc71_wmi_event_guids[] = { 28 | { .guid = QC71_WMI_EVENT_70_GUID }, 29 | { .guid = QC71_WMI_EVENT_71_GUID }, 30 | { .guid = QC71_WMI_EVENT_72_GUID }, 31 | }; 32 | 33 | static const struct key_entry qc71_wmi_hotkeys[] = { 34 | 35 | /* reported via keyboard controller */ 36 | { KE_IGNORE, 0x01, { KEY_CAPSLOCK }}, 37 | { KE_IGNORE, 0x02, { KEY_NUMLOCK }}, 38 | { KE_IGNORE, 0x03, { KEY_SCROLLLOCK }}, 39 | 40 | /* reported via "video bus" */ 41 | { KE_IGNORE, 0x14, { KEY_BRIGHTNESSUP }}, 42 | { KE_IGNORE, 0x15, { KEY_BRIGHTNESSDOWN }}, 43 | 44 | /* reported in automatic mode when rfkill state changes */ 45 | { KE_SW, 0x1a, {.sw = { SW_RFKILL_ALL, 1 }}}, 46 | { KE_SW, 0x1b, {.sw = { SW_RFKILL_ALL, 0 }}}, 47 | 48 | /* reported via keyboard controller */ 49 | { KE_IGNORE, 0x35, { KEY_MUTE }}, 50 | { KE_IGNORE, 0x36, { KEY_VOLUMEDOWN }}, 51 | { KE_IGNORE, 0x37, { KEY_VOLUMEUP }}, 52 | 53 | /* 54 | * not reported by other means when in manual mode, 55 | * handled automatically when it automatic mode 56 | */ 57 | { KE_KEY, 0xa4, { KEY_RFKILL }}, 58 | { KE_KEY, 0xb1, { KEY_KBDILLUMDOWN }}, 59 | { KE_KEY, 0xb2, { KEY_KBDILLUMUP }}, 60 | { KE_KEY, 0xb8, { KEY_FN_ESC }}, 61 | 62 | { KE_END } 63 | }; 64 | 65 | /* ========================================================================== */ 66 | 67 | static struct input_dev *qc71_input_dev; 68 | 69 | /* ========================================================================== */ 70 | 71 | static void toggle_fn_lock_from_event_handler(void) 72 | { 73 | int status = qc71_fn_lock_get_state(); 74 | 75 | if (status < 0) 76 | return; 77 | 78 | /* seemingly the returned status in the WMI event handler is not the current */ 79 | pr_info("setting Fn lock state from %d to %d\n", !status, status); 80 | qc71_fn_lock_set_state(status); 81 | } 82 | 83 | #if IS_ENABLED(CONFIG_LEDS_BRIGHTNESS_HW_CHANGED) 84 | extern struct rw_semaphore leds_list_lock; 85 | extern struct list_head leds_list; 86 | 87 | static void emit_keyboard_led_hw_changed(void) 88 | { 89 | struct led_classdev *led; 90 | 91 | if (down_read_killable(&leds_list_lock)) 92 | return; 93 | 94 | list_for_each_entry (led, &leds_list, node) { 95 | size_t name_length; 96 | const char *suffix; 97 | 98 | if (!(led->flags & LED_BRIGHT_HW_CHANGED)) 99 | continue; 100 | 101 | name_length = strlen(led->name); 102 | 103 | if (name_length < strlen(KBD_BL_LED_SUFFIX)) 104 | continue; 105 | 106 | suffix = led->name + name_length - strlen(KBD_BL_LED_SUFFIX); 107 | 108 | if (strcmp(suffix, KBD_BL_LED_SUFFIX) == 0) { 109 | if (mutex_lock_interruptible(&led->led_access)) 110 | break; 111 | 112 | if (led_update_brightness(led) >= 0) 113 | led_classdev_notify_brightness_hw_changed(led, led->brightness); 114 | 115 | mutex_unlock(&led->led_access); 116 | break; 117 | } 118 | } 119 | 120 | up_read(&leds_list_lock); 121 | } 122 | #else 123 | static inline void emit_keyboard_led_hw_changed(void) 124 | { } 125 | #endif 126 | 127 | static void process_event_72(const union acpi_object *obj) 128 | { 129 | bool do_report = true; 130 | 131 | if (obj->type != ACPI_TYPE_INTEGER) 132 | return; 133 | 134 | switch (obj->integer.value) { 135 | /* caps lock */ 136 | case 1: 137 | pr_info("caps lock\n"); 138 | break; 139 | 140 | /* num lock */ 141 | case 2: 142 | pr_info("num lock\n"); 143 | break; 144 | 145 | /* scroll lock */ 146 | case 3: 147 | pr_info("scroll lock\n"); 148 | break; 149 | 150 | /* touchpad on */ 151 | case 4: 152 | pr_info("touchpad on\n"); 153 | break; 154 | 155 | /* touchpad off */ 156 | case 5: 157 | pr_info("touchpad off\n"); 158 | break; 159 | 160 | /* increase screen brightness */ 161 | case 20: 162 | pr_info("increase screen brightness\n"); 163 | /* do_report = !acpi_video_handles_brightness_key_presses() */ 164 | break; 165 | 166 | /* decrease screen brightness */ 167 | case 21: 168 | pr_info("decrease screen brightness\n"); 169 | /* do_report = !acpi_video_handles_brightness_key_presses() */ 170 | break; 171 | 172 | /* radio on */ 173 | case 26: 174 | /* triggered in automatic mode when the rfkill hotkey is pressed */ 175 | pr_info("radio on\n"); 176 | break; 177 | 178 | /* radio off */ 179 | case 27: 180 | /* triggered in automatic mode when the rfkill hotkey is pressed */ 181 | pr_info("radio off\n"); 182 | break; 183 | 184 | /* mute/unmute */ 185 | case 53: 186 | pr_info("toggle mute\n"); 187 | break; 188 | 189 | /* decrease volume */ 190 | case 54: 191 | pr_info("decrease volume\n"); 192 | break; 193 | 194 | /* increase volume */ 195 | case 55: 196 | pr_info("increase volume\n"); 197 | break; 198 | 199 | case 57: 200 | pr_info("lightbar on\n"); 201 | break; 202 | 203 | case 58: 204 | pr_info("lightbar off\n"); 205 | break; 206 | 207 | /* enable super key (win key) lock */ 208 | case 64: 209 | pr_info("enable super key lock\n"); 210 | break; 211 | 212 | /* decrease volume */ 213 | case 65: 214 | pr_info("disable super key lock\n"); 215 | break; 216 | 217 | /* enable/disable airplane mode */ 218 | case 164: 219 | pr_info("toggle airplane mode\n"); 220 | break; 221 | 222 | /* super key (win key) lock state changed */ 223 | case 165: 224 | pr_info("super key lock state changed\n"); 225 | sysfs_notify(&qc71_platform_dev->dev.kobj, NULL, "super_key_lock"); 226 | break; 227 | 228 | case 166: 229 | pr_info("lightbar state changed\n"); 230 | break; 231 | 232 | /* fan boost state changed */ 233 | case 167: 234 | pr_info("fan boost state changed\n"); 235 | break; 236 | 237 | /* charger unplugged/plugged in */ 238 | case 171: 239 | pr_info("AC plugged/unplugged\n"); 240 | break; 241 | 242 | /* perf mode button pressed */ 243 | case 176: 244 | pr_info("change perf mode\n"); 245 | /* TODO: should it be handled here? */ 246 | break; 247 | 248 | /* increase keyboard backlight */ 249 | case 177: 250 | pr_info("keyboard backlight decrease\n"); 251 | /* TODO: should it be handled here? */ 252 | break; 253 | 254 | /* decrease keyboard backlight */ 255 | case 178: 256 | pr_info("keyboard backlight increase\n"); 257 | /* TODO: should it be handled here? */ 258 | break; 259 | 260 | /* toggle Fn lock (Fn+ESC)*/ 261 | case 184: 262 | pr_info("toggle Fn lock\n"); 263 | toggle_fn_lock_from_event_handler(); 264 | sysfs_notify(&qc71_platform_dev->dev.kobj, NULL, "fn_lock"); 265 | break; 266 | 267 | /* keyboard backlight brightness changed */ 268 | case 240: 269 | pr_info("keyboard backlight changed\n"); 270 | emit_keyboard_led_hw_changed(); 271 | break; 272 | 273 | default: 274 | pr_warn("unknown code: %u\n", (unsigned int) obj->integer.value); 275 | break; 276 | } 277 | 278 | if (do_report && qc71_input_dev) 279 | sparse_keymap_report_event(qc71_input_dev, 280 | obj->integer.value, 1, true); 281 | 282 | } 283 | 284 | static void process_event(const union acpi_object *obj, const char *guid) 285 | { 286 | pr_info("guid=%s obj=%p\n", guid, obj); 287 | 288 | if (!obj) 289 | return; 290 | 291 | pr_info("obj->type = %d\n", (int) obj->type); 292 | if (obj->type == ACPI_TYPE_INTEGER) { 293 | pr_info("int = %u\n", (unsigned int) obj->integer.value); 294 | } else if (obj->type == ACPI_TYPE_STRING) { 295 | pr_info("string = '%s'\n", obj->string.pointer); 296 | } else if (obj->type == ACPI_TYPE_BUFFER) { 297 | pr_info("buffer = %u %*ph", obj->buffer.length, 298 | (int) obj->buffer.length, (void *) obj->buffer.pointer); 299 | } 300 | 301 | if (strcmp(guid, QC71_WMI_EVENT_72_GUID) == 0) 302 | process_event_72(obj); 303 | } 304 | 305 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 12, 0) 306 | static void qc71_wmi_event_handler(union acpi_object *obj, void *context) 307 | { 308 | process_event(obj, context); 309 | } 310 | #else 311 | static void qc71_wmi_event_handler(u32 value, void *context) 312 | { 313 | struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL }; 314 | acpi_status status; 315 | 316 | status = wmi_get_event_data(value, &response); 317 | if (ACPI_FAILURE(status)) { 318 | pr_err("bad WMI event status: %#010x\n", (unsigned int) status); 319 | return; 320 | } 321 | 322 | process_event(response.pointer, context); 323 | kfree(response.pointer); 324 | } 325 | #endif 326 | 327 | static int __init setup_input_dev(void) 328 | { 329 | int err = 0; 330 | 331 | qc71_input_dev = input_allocate_device(); 332 | if (!qc71_input_dev) 333 | return -ENOMEM; 334 | 335 | qc71_input_dev->name = "QC71 laptop input device"; 336 | qc71_input_dev->phys = "qc71_laptop/input0"; 337 | qc71_input_dev->id.bustype = BUS_HOST; 338 | qc71_input_dev->dev.parent = &qc71_platform_dev->dev; 339 | 340 | err = sparse_keymap_setup(qc71_input_dev, qc71_wmi_hotkeys, NULL); 341 | if (err) 342 | goto err_free_device; 343 | 344 | err = qc71_rfkill_get_wifi_state(); 345 | if (err >= 0) 346 | input_report_switch(qc71_input_dev, SW_RFKILL_ALL, err); 347 | else 348 | input_report_switch(qc71_input_dev, SW_RFKILL_ALL, 1); 349 | 350 | err = input_register_device(qc71_input_dev); 351 | if (err) 352 | goto err_free_device; 353 | 354 | return err; 355 | 356 | err_free_device: 357 | input_free_device(qc71_input_dev); 358 | qc71_input_dev = NULL; 359 | 360 | return err; 361 | } 362 | 363 | /* ========================================================================== */ 364 | 365 | int __init qc71_wmi_events_setup(void) 366 | { 367 | int err = 0, i; 368 | 369 | (void) setup_input_dev(); 370 | 371 | for (i = 0; i < ARRAY_SIZE(qc71_wmi_event_guids); i++) { 372 | const char *guid = qc71_wmi_event_guids[i].guid; 373 | acpi_status status = 374 | wmi_install_notify_handler(guid, qc71_wmi_event_handler, (void *) guid); 375 | 376 | if (ACPI_FAILURE(status)) { 377 | pr_warn("could not install WMI notify handler for '%s': [%#010lx] %s\n", 378 | guid, (unsigned long) status, acpi_format_exception(status)); 379 | } else { 380 | qc71_wmi_event_guids[i].handler_installed = true; 381 | } 382 | } 383 | 384 | return err; 385 | } 386 | 387 | void qc71_wmi_events_cleanup(void) 388 | { 389 | int i; 390 | 391 | for (i = 0; i < ARRAY_SIZE(qc71_wmi_event_guids); i++) { 392 | if (qc71_wmi_event_guids[i].handler_installed) { 393 | wmi_remove_notify_handler(qc71_wmi_event_guids[i].guid); 394 | qc71_wmi_event_guids[i].handler_installed = false; 395 | } 396 | } 397 | 398 | if (qc71_input_dev) 399 | input_unregister_device(qc71_input_dev); 400 | } 401 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | --------------------------------------------------------------------------------