1 // SPDX-License-Identifier: GPL-2.0-or-later
8 * Portions based on wistron_btns.c:
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
21 #include <linux/input.h>
22 #include <linux/input/sparse-keymap.h>
23 #include <linux/platform_device.h>
24 #include <linux/platform_profile.h>
25 #include <linux/hwmon.h>
26 #include <linux/acpi.h>
27 #include <linux/rfkill.h>
28 #include <linux/string.h>
29 #include <linux/dmi.h>
32 MODULE_DESCRIPTION("HP laptop WMI hotkeys driver");
33 MODULE_LICENSE("GPL");
35 MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
36 MODULE_ALIAS("wmi:5FB7F034-2C63-45e9-BE91-3D44E2C707E4");
38 #define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
39 #define HPWMI_BIOS_GUID "5FB7F034-2C63-45e9-BE91-3D44E2C707E4"
40 #define HP_OMEN_EC_THERMAL_PROFILE_OFFSET 0x95
41 #define zero_if_sup(tmp) (zero_insize_support?0:sizeof(tmp)) // use when zero insize is required
43 /* DMI board names of devices that should use the omen specific path for
45 * This was obtained by taking a look in the windows omen command center
46 * app and parsing a json file that they use to figure out what capabilities
47 * the device should have.
48 * A device is considered an omen if the DisplayName in that list contains
49 * "OMEN", and it can use the thermal profile stuff if the "Feature" array
50 * contains "PerformanceControl".
52 static const char * const omen_thermal_profile_boards[] = {
53 "84DA", "84DB", "84DC", "8574", "8575", "860A", "87B5", "8572", "8573",
54 "8600", "8601", "8602", "8605", "8606", "8607", "8746", "8747", "8749",
55 "874A", "8603", "8604", "8748", "886B", "886C", "878A", "878B", "878C",
56 "88C8", "88CB", "8786", "8787", "8788", "88D1", "88D2", "88F4", "88FD",
57 "88F5", "88F6", "88F7", "88FE", "88FF", "8900", "8901", "8902", "8912",
58 "8917", "8918", "8949", "894A", "89EB"
61 /* DMI Board names of Omen laptops that are specifically set to be thermal
62 * profile version 0 by the Omen Command Center app, regardless of what
63 * the get system design information WMI call returns
65 static const char *const omen_thermal_profile_force_v0_boards[] = {
66 "8607", "8746", "8747", "8749", "874A", "8748"
71 HPWMI_BLUETOOTH = 0x1,
76 enum hp_wmi_event_ids {
77 HPWMI_DOCK_EVENT = 0x01,
78 HPWMI_PARK_HDD = 0x02,
79 HPWMI_SMART_ADAPTER = 0x03,
80 HPWMI_BEZEL_BUTTON = 0x04,
81 HPWMI_WIRELESS = 0x05,
82 HPWMI_CPU_BATTERY_THROTTLE = 0x06,
83 HPWMI_LOCK_SWITCH = 0x07,
84 HPWMI_LID_SWITCH = 0x08,
85 HPWMI_SCREEN_ROTATION = 0x09,
86 HPWMI_COOLSENSE_SYSTEM_MOBILE = 0x0A,
87 HPWMI_COOLSENSE_SYSTEM_HOT = 0x0B,
88 HPWMI_PROXIMITY_SENSOR = 0x0C,
89 HPWMI_BACKLIT_KB_BRIGHTNESS = 0x0D,
90 HPWMI_PEAKSHIFT_PERIOD = 0x0F,
91 HPWMI_BATTERY_CHARGE_PERIOD = 0x10,
92 HPWMI_SANITIZATION_MODE = 0x17,
96 * struct bios_args buffer is dynamically allocated. New WMI command types
97 * were introduced that exceeds 128-byte data size. Changes to handle
98 * the data size allocation scheme were kept in hp_wmi_perform_qurey function.
108 enum hp_wmi_commandtype {
109 HPWMI_DISPLAY_QUERY = 0x01,
110 HPWMI_HDDTEMP_QUERY = 0x02,
111 HPWMI_ALS_QUERY = 0x03,
112 HPWMI_HARDWARE_QUERY = 0x04,
113 HPWMI_WIRELESS_QUERY = 0x05,
114 HPWMI_BATTERY_QUERY = 0x07,
115 HPWMI_BIOS_QUERY = 0x09,
116 HPWMI_FEATURE_QUERY = 0x0b,
117 HPWMI_HOTKEY_QUERY = 0x0c,
118 HPWMI_FEATURE2_QUERY = 0x0d,
119 HPWMI_WIRELESS2_QUERY = 0x1b,
120 HPWMI_POSTCODEERROR_QUERY = 0x2a,
121 HPWMI_SYSTEM_DEVICE_MODE = 0x40,
122 HPWMI_THERMAL_PROFILE_QUERY = 0x4c,
125 enum hp_wmi_gm_commandtype {
126 HPWMI_FAN_SPEED_GET_QUERY = 0x11,
127 HPWMI_SET_PERFORMANCE_MODE = 0x1A,
128 HPWMI_FAN_SPEED_MAX_GET_QUERY = 0x26,
129 HPWMI_FAN_SPEED_MAX_SET_QUERY = 0x27,
130 HPWMI_GET_SYSTEM_DESIGN_DATA = 0x28,
133 enum hp_wmi_command {
140 enum hp_wmi_hardware_mask {
141 HPWMI_DOCK_MASK = 0x01,
142 HPWMI_TABLET_MASK = 0x04,
150 enum hp_return_value {
151 HPWMI_RET_WRONG_SIGNATURE = 0x02,
152 HPWMI_RET_UNKNOWN_COMMAND = 0x03,
153 HPWMI_RET_UNKNOWN_CMDTYPE = 0x04,
154 HPWMI_RET_INVALID_PARAMETERS = 0x05,
157 enum hp_wireless2_bits {
158 HPWMI_POWER_STATE = 0x01,
159 HPWMI_POWER_SOFT = 0x02,
160 HPWMI_POWER_BIOS = 0x04,
161 HPWMI_POWER_HARD = 0x08,
162 HPWMI_POWER_FW_OR_HW = HPWMI_POWER_BIOS | HPWMI_POWER_HARD,
165 enum hp_thermal_profile_omen_v0 {
166 HP_OMEN_V0_THERMAL_PROFILE_DEFAULT = 0x00,
167 HP_OMEN_V0_THERMAL_PROFILE_PERFORMANCE = 0x01,
168 HP_OMEN_V0_THERMAL_PROFILE_COOL = 0x02,
171 enum hp_thermal_profile_omen_v1 {
172 HP_OMEN_V1_THERMAL_PROFILE_DEFAULT = 0x30,
173 HP_OMEN_V1_THERMAL_PROFILE_PERFORMANCE = 0x31,
174 HP_OMEN_V1_THERMAL_PROFILE_COOL = 0x50,
177 enum hp_thermal_profile {
178 HP_THERMAL_PROFILE_PERFORMANCE = 0x00,
179 HP_THERMAL_PROFILE_DEFAULT = 0x01,
180 HP_THERMAL_PROFILE_COOL = 0x02
183 #define IS_HWBLOCKED(x) ((x & HPWMI_POWER_FW_OR_HW) != HPWMI_POWER_FW_OR_HW)
184 #define IS_SWBLOCKED(x) !(x & HPWMI_POWER_SOFT)
186 struct bios_rfkill2_device_state {
191 u16 subsys_vendor_id;
192 u16 subsys_product_id;
198 /* 7 devices fit into the 128 byte buffer */
199 #define HPWMI_MAX_RFKILL2_DEVICES 7
201 struct bios_rfkill2_state {
205 struct bios_rfkill2_device_state device[HPWMI_MAX_RFKILL2_DEVICES];
208 static const struct key_entry hp_wmi_keymap[] = {
209 { KE_KEY, 0x02, { KEY_BRIGHTNESSUP } },
210 { KE_KEY, 0x03, { KEY_BRIGHTNESSDOWN } },
211 { KE_KEY, 0x20e6, { KEY_PROG1 } },
212 { KE_KEY, 0x20e8, { KEY_MEDIA } },
213 { KE_KEY, 0x2142, { KEY_MEDIA } },
214 { KE_KEY, 0x213b, { KEY_INFO } },
215 { KE_KEY, 0x2169, { KEY_ROTATE_DISPLAY } },
216 { KE_KEY, 0x216a, { KEY_SETUP } },
217 { KE_KEY, 0x231b, { KEY_HELP } },
221 static struct input_dev *hp_wmi_input_dev;
222 static struct platform_device *hp_wmi_platform_dev;
223 static struct platform_profile_handler platform_profile_handler;
224 static bool platform_profile_support;
225 static bool zero_insize_support;
227 static struct rfkill *wifi_rfkill;
228 static struct rfkill *bluetooth_rfkill;
229 static struct rfkill *wwan_rfkill;
231 struct rfkill2_device {
234 struct rfkill *rfkill;
237 static int rfkill2_count;
238 static struct rfkill2_device rfkill2[HPWMI_MAX_RFKILL2_DEVICES];
241 * Chassis Types values were obtained from SMBIOS reference
242 * specification version 3.00. A complete list of system enclosures
243 * and chassis types is available on Table 17.
245 static const char * const tablet_chassis_types[] = {
247 "31", /* Convertible */
248 "32" /* Detachable */
251 #define DEVICE_MODE_TABLET 0x06
253 /* map output size to the corresponding WMI method id */
254 static inline int encode_outsize_for_pvsz(int outsize)
270 * hp_wmi_perform_query
272 * query: The commandtype (enum hp_wmi_commandtype)
273 * write: The command (enum hp_wmi_command)
274 * buffer: Buffer used as input and/or output
275 * insize: Size of input buffer
276 * outsize: Size of output buffer
278 * returns zero on success
279 * an HP WMI query specific error code (which is positive)
280 * -EINVAL if the query was not successful at all
281 * -EINVAL if the output buffer size exceeds buffersize
283 * Note: The buffersize must at least be the maximum of the input and output
284 * size. E.g. Battery info query is defined to have 1 byte input
285 * and 128 byte output. The caller would do:
286 * buffer = kzalloc(128, GFP_KERNEL);
287 * ret = hp_wmi_perform_query(HPWMI_BATTERY_QUERY, HPWMI_READ, buffer, 1, 128)
289 static int hp_wmi_perform_query(int query, enum hp_wmi_command command,
290 void *buffer, int insize, int outsize)
292 struct acpi_buffer input, output = { ACPI_ALLOCATE_BUFFER, NULL };
293 struct bios_return *bios_return;
294 union acpi_object *obj = NULL;
295 struct bios_args *args = NULL;
296 int mid, actual_insize, actual_outsize;
297 size_t bios_args_size;
300 mid = encode_outsize_for_pvsz(outsize);
301 if (WARN_ON(mid < 0))
304 actual_insize = max(insize, 128);
305 bios_args_size = struct_size(args, data, actual_insize);
306 args = kmalloc(bios_args_size, GFP_KERNEL);
310 input.length = bios_args_size;
311 input.pointer = args;
313 args->signature = 0x55434553;
314 args->command = command;
315 args->commandtype = query;
316 args->datasize = insize;
317 memcpy(args->data, buffer, flex_array_size(args, data, insize));
319 ret = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, mid, &input, &output);
323 obj = output.pointer;
329 if (obj->type != ACPI_TYPE_BUFFER) {
330 pr_warn("query 0x%x returned an invalid object 0x%x\n", query, ret);
335 bios_return = (struct bios_return *)obj->buffer.pointer;
336 ret = bios_return->return_code;
339 if (ret != HPWMI_RET_UNKNOWN_COMMAND &&
340 ret != HPWMI_RET_UNKNOWN_CMDTYPE)
341 pr_warn("query 0x%x returned error 0x%x\n", query, ret);
345 /* Ignore output data of zero size */
349 actual_outsize = min(outsize, (int)(obj->buffer.length - sizeof(*bios_return)));
350 memcpy(buffer, obj->buffer.pointer + sizeof(*bios_return), actual_outsize);
351 memset(buffer + actual_outsize, 0, outsize - actual_outsize);
359 static int hp_wmi_get_fan_speed(int fan)
362 char fan_data[4] = { fan, 0, 0, 0 };
364 int ret = hp_wmi_perform_query(HPWMI_FAN_SPEED_GET_QUERY, HPWMI_GM,
365 &fan_data, sizeof(char),
374 return (fsh << 8) | fsl;
377 static int hp_wmi_read_int(int query)
381 ret = hp_wmi_perform_query(query, HPWMI_READ, &val,
382 zero_if_sup(val), sizeof(val));
385 return ret < 0 ? ret : -EINVAL;
390 static int hp_wmi_get_dock_state(void)
392 int state = hp_wmi_read_int(HPWMI_HARDWARE_QUERY);
397 return !!(state & HPWMI_DOCK_MASK);
400 static int hp_wmi_get_tablet_mode(void)
402 char system_device_mode[4] = { 0 };
403 const char *chassis_type;
407 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
411 tablet_found = match_string(tablet_chassis_types,
412 ARRAY_SIZE(tablet_chassis_types),
417 ret = hp_wmi_perform_query(HPWMI_SYSTEM_DEVICE_MODE, HPWMI_READ,
418 system_device_mode, zero_if_sup(system_device_mode),
419 sizeof(system_device_mode));
423 return system_device_mode[0] == DEVICE_MODE_TABLET;
426 static int omen_thermal_profile_set(int mode)
428 char buffer[2] = {0, mode};
431 ret = hp_wmi_perform_query(HPWMI_SET_PERFORMANCE_MODE, HPWMI_GM,
432 &buffer, sizeof(buffer), 0);
435 return ret < 0 ? ret : -EINVAL;
440 static bool is_omen_thermal_profile(void)
442 const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
447 return match_string(omen_thermal_profile_boards,
448 ARRAY_SIZE(omen_thermal_profile_boards),
452 static int omen_get_thermal_policy_version(void)
454 unsigned char buffer[8] = { 0 };
457 const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
460 int matches = match_string(omen_thermal_profile_force_v0_boards,
461 ARRAY_SIZE(omen_thermal_profile_force_v0_boards),
467 ret = hp_wmi_perform_query(HPWMI_GET_SYSTEM_DESIGN_DATA, HPWMI_GM,
468 &buffer, sizeof(buffer), sizeof(buffer));
471 return ret < 0 ? ret : -EINVAL;
476 static int omen_thermal_profile_get(void)
480 int ret = ec_read(HP_OMEN_EC_THERMAL_PROFILE_OFFSET, &data);
488 static int hp_wmi_fan_speed_max_set(int enabled)
492 ret = hp_wmi_perform_query(HPWMI_FAN_SPEED_MAX_SET_QUERY, HPWMI_GM,
493 &enabled, sizeof(enabled), 0);
496 return ret < 0 ? ret : -EINVAL;
501 static int hp_wmi_fan_speed_max_get(void)
505 ret = hp_wmi_perform_query(HPWMI_FAN_SPEED_MAX_GET_QUERY, HPWMI_GM,
506 &val, zero_if_sup(val), sizeof(val));
509 return ret < 0 ? ret : -EINVAL;
514 static int __init hp_wmi_bios_2008_later(void)
517 int ret = hp_wmi_perform_query(HPWMI_FEATURE_QUERY, HPWMI_READ, &state,
518 zero_if_sup(state), sizeof(state));
522 return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
525 static int __init hp_wmi_bios_2009_later(void)
528 int ret = hp_wmi_perform_query(HPWMI_FEATURE2_QUERY, HPWMI_READ, &state,
529 zero_if_sup(state), sizeof(state));
533 return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
536 static int __init hp_wmi_enable_hotkeys(void)
539 int ret = hp_wmi_perform_query(HPWMI_BIOS_QUERY, HPWMI_WRITE, &value,
542 return ret <= 0 ? ret : -EINVAL;
545 static int hp_wmi_set_block(void *data, bool blocked)
547 enum hp_wmi_radio r = (enum hp_wmi_radio) data;
548 int query = BIT(r + 8) | ((!blocked) << r);
551 ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE,
552 &query, sizeof(query), 0);
554 return ret <= 0 ? ret : -EINVAL;
557 static const struct rfkill_ops hp_wmi_rfkill_ops = {
558 .set_block = hp_wmi_set_block,
561 static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
563 int mask = 0x200 << (r * 8);
565 int wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
567 /* TBD: Pass error */
568 WARN_ONCE(wireless < 0, "error executing HPWMI_WIRELESS_QUERY");
570 return !(wireless & mask);
573 static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
575 int mask = 0x800 << (r * 8);
577 int wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
579 /* TBD: Pass error */
580 WARN_ONCE(wireless < 0, "error executing HPWMI_WIRELESS_QUERY");
582 return !(wireless & mask);
585 static int hp_wmi_rfkill2_set_block(void *data, bool blocked)
587 int rfkill_id = (int)(long)data;
588 char buffer[4] = { 0x01, 0x00, rfkill_id, !blocked };
591 ret = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_WRITE,
592 buffer, sizeof(buffer), 0);
594 return ret <= 0 ? ret : -EINVAL;
597 static const struct rfkill_ops hp_wmi_rfkill2_ops = {
598 .set_block = hp_wmi_rfkill2_set_block,
601 static int hp_wmi_rfkill2_refresh(void)
603 struct bios_rfkill2_state state;
606 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
607 zero_if_sup(state), sizeof(state));
611 for (i = 0; i < rfkill2_count; i++) {
612 int num = rfkill2[i].num;
613 struct bios_rfkill2_device_state *devstate;
615 devstate = &state.device[num];
617 if (num >= state.count ||
618 devstate->rfkill_id != rfkill2[i].id) {
619 pr_warn("power configuration of the wireless devices unexpectedly changed\n");
623 rfkill_set_states(rfkill2[i].rfkill,
624 IS_SWBLOCKED(devstate->power),
625 IS_HWBLOCKED(devstate->power));
631 static ssize_t display_show(struct device *dev, struct device_attribute *attr,
634 int value = hp_wmi_read_int(HPWMI_DISPLAY_QUERY);
638 return sprintf(buf, "%d\n", value);
641 static ssize_t hddtemp_show(struct device *dev, struct device_attribute *attr,
644 int value = hp_wmi_read_int(HPWMI_HDDTEMP_QUERY);
648 return sprintf(buf, "%d\n", value);
651 static ssize_t als_show(struct device *dev, struct device_attribute *attr,
654 int value = hp_wmi_read_int(HPWMI_ALS_QUERY);
658 return sprintf(buf, "%d\n", value);
661 static ssize_t dock_show(struct device *dev, struct device_attribute *attr,
664 int value = hp_wmi_get_dock_state();
668 return sprintf(buf, "%d\n", value);
671 static ssize_t tablet_show(struct device *dev, struct device_attribute *attr,
674 int value = hp_wmi_get_tablet_mode();
678 return sprintf(buf, "%d\n", value);
681 static ssize_t postcode_show(struct device *dev, struct device_attribute *attr,
684 /* Get the POST error code of previous boot failure. */
685 int value = hp_wmi_read_int(HPWMI_POSTCODEERROR_QUERY);
689 return sprintf(buf, "0x%x\n", value);
692 static ssize_t als_store(struct device *dev, struct device_attribute *attr,
693 const char *buf, size_t count)
698 ret = kstrtou32(buf, 10, &tmp);
702 ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, HPWMI_WRITE, &tmp,
705 return ret < 0 ? ret : -EINVAL;
710 static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,
711 const char *buf, size_t count)
717 ret = kstrtobool(buf, &clear);
724 /* Clear the POST error code. It is kept until cleared. */
725 ret = hp_wmi_perform_query(HPWMI_POSTCODEERROR_QUERY, HPWMI_WRITE, &tmp,
728 return ret < 0 ? ret : -EINVAL;
733 static DEVICE_ATTR_RO(display);
734 static DEVICE_ATTR_RO(hddtemp);
735 static DEVICE_ATTR_RW(als);
736 static DEVICE_ATTR_RO(dock);
737 static DEVICE_ATTR_RO(tablet);
738 static DEVICE_ATTR_RW(postcode);
740 static struct attribute *hp_wmi_attrs[] = {
741 &dev_attr_display.attr,
742 &dev_attr_hddtemp.attr,
745 &dev_attr_tablet.attr,
746 &dev_attr_postcode.attr,
749 ATTRIBUTE_GROUPS(hp_wmi);
751 static void hp_wmi_notify(u32 value, void *context)
753 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
754 u32 event_id, event_data;
755 union acpi_object *obj;
760 status = wmi_get_event_data(value, &response);
761 if (status != AE_OK) {
762 pr_info("bad event status 0x%x\n", status);
766 obj = (union acpi_object *)response.pointer;
770 if (obj->type != ACPI_TYPE_BUFFER) {
771 pr_info("Unknown response received %d\n", obj->type);
777 * Depending on ACPI version the concatenation of id and event data
778 * inside _WED function will result in a 8 or 16 byte buffer.
780 location = (u32 *)obj->buffer.pointer;
781 if (obj->buffer.length == 8) {
782 event_id = *location;
783 event_data = *(location + 1);
784 } else if (obj->buffer.length == 16) {
785 event_id = *location;
786 event_data = *(location + 2);
788 pr_info("Unknown buffer length %d\n", obj->buffer.length);
795 case HPWMI_DOCK_EVENT:
796 if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
797 input_report_switch(hp_wmi_input_dev, SW_DOCK,
798 hp_wmi_get_dock_state());
799 if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
800 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
801 hp_wmi_get_tablet_mode());
802 input_sync(hp_wmi_input_dev);
806 case HPWMI_SMART_ADAPTER:
808 case HPWMI_BEZEL_BUTTON:
809 key_code = hp_wmi_read_int(HPWMI_HOTKEY_QUERY);
813 if (!sparse_keymap_report_event(hp_wmi_input_dev,
815 pr_info("Unknown key code - 0x%x\n", key_code);
819 hp_wmi_rfkill2_refresh();
824 rfkill_set_states(wifi_rfkill,
825 hp_wmi_get_sw_state(HPWMI_WIFI),
826 hp_wmi_get_hw_state(HPWMI_WIFI));
827 if (bluetooth_rfkill)
828 rfkill_set_states(bluetooth_rfkill,
829 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
830 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
832 rfkill_set_states(wwan_rfkill,
833 hp_wmi_get_sw_state(HPWMI_WWAN),
834 hp_wmi_get_hw_state(HPWMI_WWAN));
836 case HPWMI_CPU_BATTERY_THROTTLE:
837 pr_info("Unimplemented CPU throttle because of 3 Cell battery event detected\n");
839 case HPWMI_LOCK_SWITCH:
841 case HPWMI_LID_SWITCH:
843 case HPWMI_SCREEN_ROTATION:
845 case HPWMI_COOLSENSE_SYSTEM_MOBILE:
847 case HPWMI_COOLSENSE_SYSTEM_HOT:
849 case HPWMI_PROXIMITY_SENSOR:
851 case HPWMI_BACKLIT_KB_BRIGHTNESS:
853 case HPWMI_PEAKSHIFT_PERIOD:
855 case HPWMI_BATTERY_CHARGE_PERIOD:
857 case HPWMI_SANITIZATION_MODE:
860 pr_info("Unknown event_id - %d - 0x%x\n", event_id, event_data);
865 static int __init hp_wmi_input_setup(void)
870 hp_wmi_input_dev = input_allocate_device();
871 if (!hp_wmi_input_dev)
874 hp_wmi_input_dev->name = "HP WMI hotkeys";
875 hp_wmi_input_dev->phys = "wmi/input0";
876 hp_wmi_input_dev->id.bustype = BUS_HOST;
878 __set_bit(EV_SW, hp_wmi_input_dev->evbit);
881 val = hp_wmi_get_dock_state();
883 __set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
884 input_report_switch(hp_wmi_input_dev, SW_DOCK, val);
888 val = hp_wmi_get_tablet_mode();
890 __set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
891 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE, val);
894 err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
898 /* Set initial hardware state */
899 input_sync(hp_wmi_input_dev);
901 if (!hp_wmi_bios_2009_later() && hp_wmi_bios_2008_later())
902 hp_wmi_enable_hotkeys();
904 status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
905 if (ACPI_FAILURE(status)) {
910 err = input_register_device(hp_wmi_input_dev);
912 goto err_uninstall_notifier;
916 err_uninstall_notifier:
917 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
919 input_free_device(hp_wmi_input_dev);
923 static void hp_wmi_input_destroy(void)
925 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
926 input_unregister_device(hp_wmi_input_dev);
929 static int __init hp_wmi_rfkill_setup(struct platform_device *device)
933 wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
937 err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE, &wireless,
938 sizeof(wireless), 0);
942 if (wireless & 0x1) {
943 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
946 (void *) HPWMI_WIFI);
949 rfkill_init_sw_state(wifi_rfkill,
950 hp_wmi_get_sw_state(HPWMI_WIFI));
951 rfkill_set_hw_state(wifi_rfkill,
952 hp_wmi_get_hw_state(HPWMI_WIFI));
953 err = rfkill_register(wifi_rfkill);
955 goto register_wifi_error;
958 if (wireless & 0x2) {
959 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
960 RFKILL_TYPE_BLUETOOTH,
962 (void *) HPWMI_BLUETOOTH);
963 if (!bluetooth_rfkill) {
965 goto register_bluetooth_error;
967 rfkill_init_sw_state(bluetooth_rfkill,
968 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
969 rfkill_set_hw_state(bluetooth_rfkill,
970 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
971 err = rfkill_register(bluetooth_rfkill);
973 goto register_bluetooth_error;
976 if (wireless & 0x4) {
977 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
980 (void *) HPWMI_WWAN);
983 goto register_wwan_error;
985 rfkill_init_sw_state(wwan_rfkill,
986 hp_wmi_get_sw_state(HPWMI_WWAN));
987 rfkill_set_hw_state(wwan_rfkill,
988 hp_wmi_get_hw_state(HPWMI_WWAN));
989 err = rfkill_register(wwan_rfkill);
991 goto register_wwan_error;
997 rfkill_destroy(wwan_rfkill);
999 if (bluetooth_rfkill)
1000 rfkill_unregister(bluetooth_rfkill);
1001 register_bluetooth_error:
1002 rfkill_destroy(bluetooth_rfkill);
1003 bluetooth_rfkill = NULL;
1005 rfkill_unregister(wifi_rfkill);
1006 register_wifi_error:
1007 rfkill_destroy(wifi_rfkill);
1012 static int __init hp_wmi_rfkill2_setup(struct platform_device *device)
1014 struct bios_rfkill2_state state;
1017 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
1018 zero_if_sup(state), sizeof(state));
1020 return err < 0 ? err : -EINVAL;
1022 if (state.count > HPWMI_MAX_RFKILL2_DEVICES) {
1023 pr_warn("unable to parse 0x1b query output\n");
1027 for (i = 0; i < state.count; i++) {
1028 struct rfkill *rfkill;
1029 enum rfkill_type type;
1032 switch (state.device[i].radio_type) {
1034 type = RFKILL_TYPE_WLAN;
1037 case HPWMI_BLUETOOTH:
1038 type = RFKILL_TYPE_BLUETOOTH;
1039 name = "hp-bluetooth";
1042 type = RFKILL_TYPE_WWAN;
1046 type = RFKILL_TYPE_GPS;
1050 pr_warn("unknown device type 0x%x\n",
1051 state.device[i].radio_type);
1055 if (!state.device[i].vendor_id) {
1056 pr_warn("zero device %d while %d reported\n",
1061 rfkill = rfkill_alloc(name, &device->dev, type,
1062 &hp_wmi_rfkill2_ops, (void *)(long)i);
1068 rfkill2[rfkill2_count].id = state.device[i].rfkill_id;
1069 rfkill2[rfkill2_count].num = i;
1070 rfkill2[rfkill2_count].rfkill = rfkill;
1072 rfkill_init_sw_state(rfkill,
1073 IS_SWBLOCKED(state.device[i].power));
1074 rfkill_set_hw_state(rfkill,
1075 IS_HWBLOCKED(state.device[i].power));
1077 if (!(state.device[i].power & HPWMI_POWER_BIOS))
1078 pr_info("device %s blocked by BIOS\n", name);
1080 err = rfkill_register(rfkill);
1082 rfkill_destroy(rfkill);
1091 for (; rfkill2_count > 0; rfkill2_count--) {
1092 rfkill_unregister(rfkill2[rfkill2_count - 1].rfkill);
1093 rfkill_destroy(rfkill2[rfkill2_count - 1].rfkill);
1098 static int platform_profile_omen_get(struct platform_profile_handler *pprof,
1099 enum platform_profile_option *profile)
1103 tp = omen_thermal_profile_get();
1108 case HP_OMEN_V0_THERMAL_PROFILE_PERFORMANCE:
1109 case HP_OMEN_V1_THERMAL_PROFILE_PERFORMANCE:
1110 *profile = PLATFORM_PROFILE_PERFORMANCE;
1112 case HP_OMEN_V0_THERMAL_PROFILE_DEFAULT:
1113 case HP_OMEN_V1_THERMAL_PROFILE_DEFAULT:
1114 *profile = PLATFORM_PROFILE_BALANCED;
1116 case HP_OMEN_V0_THERMAL_PROFILE_COOL:
1117 case HP_OMEN_V1_THERMAL_PROFILE_COOL:
1118 *profile = PLATFORM_PROFILE_COOL;
1127 static int platform_profile_omen_set(struct platform_profile_handler *pprof,
1128 enum platform_profile_option profile)
1130 int err, tp, tp_version;
1132 tp_version = omen_get_thermal_policy_version();
1134 if (tp_version < 0 || tp_version > 1)
1138 case PLATFORM_PROFILE_PERFORMANCE:
1139 if (tp_version == 0)
1140 tp = HP_OMEN_V0_THERMAL_PROFILE_PERFORMANCE;
1142 tp = HP_OMEN_V1_THERMAL_PROFILE_PERFORMANCE;
1144 case PLATFORM_PROFILE_BALANCED:
1145 if (tp_version == 0)
1146 tp = HP_OMEN_V0_THERMAL_PROFILE_DEFAULT;
1148 tp = HP_OMEN_V1_THERMAL_PROFILE_DEFAULT;
1150 case PLATFORM_PROFILE_COOL:
1151 if (tp_version == 0)
1152 tp = HP_OMEN_V0_THERMAL_PROFILE_COOL;
1154 tp = HP_OMEN_V1_THERMAL_PROFILE_COOL;
1160 err = omen_thermal_profile_set(tp);
1167 static int thermal_profile_get(void)
1169 return hp_wmi_read_int(HPWMI_THERMAL_PROFILE_QUERY);
1172 static int thermal_profile_set(int thermal_profile)
1174 return hp_wmi_perform_query(HPWMI_THERMAL_PROFILE_QUERY, HPWMI_WRITE, &thermal_profile,
1175 sizeof(thermal_profile), 0);
1178 static int hp_wmi_platform_profile_get(struct platform_profile_handler *pprof,
1179 enum platform_profile_option *profile)
1183 tp = thermal_profile_get();
1188 case HP_THERMAL_PROFILE_PERFORMANCE:
1189 *profile = PLATFORM_PROFILE_PERFORMANCE;
1191 case HP_THERMAL_PROFILE_DEFAULT:
1192 *profile = PLATFORM_PROFILE_BALANCED;
1194 case HP_THERMAL_PROFILE_COOL:
1195 *profile = PLATFORM_PROFILE_COOL;
1204 static int hp_wmi_platform_profile_set(struct platform_profile_handler *pprof,
1205 enum platform_profile_option profile)
1210 case PLATFORM_PROFILE_PERFORMANCE:
1211 tp = HP_THERMAL_PROFILE_PERFORMANCE;
1213 case PLATFORM_PROFILE_BALANCED:
1214 tp = HP_THERMAL_PROFILE_DEFAULT;
1216 case PLATFORM_PROFILE_COOL:
1217 tp = HP_THERMAL_PROFILE_COOL;
1223 err = thermal_profile_set(tp);
1230 static int thermal_profile_setup(void)
1234 if (is_omen_thermal_profile()) {
1235 tp = omen_thermal_profile_get();
1240 * call thermal profile write command to ensure that the
1241 * firmware correctly sets the OEM variables
1244 err = omen_thermal_profile_set(tp);
1248 platform_profile_handler.profile_get = platform_profile_omen_get;
1249 platform_profile_handler.profile_set = platform_profile_omen_set;
1251 tp = thermal_profile_get();
1257 * call thermal profile write command to ensure that the
1258 * firmware correctly sets the OEM variables for the DPTF
1260 err = thermal_profile_set(tp);
1264 platform_profile_handler.profile_get = hp_wmi_platform_profile_get;
1265 platform_profile_handler.profile_set = hp_wmi_platform_profile_set;
1268 set_bit(PLATFORM_PROFILE_COOL, platform_profile_handler.choices);
1269 set_bit(PLATFORM_PROFILE_BALANCED, platform_profile_handler.choices);
1270 set_bit(PLATFORM_PROFILE_PERFORMANCE, platform_profile_handler.choices);
1272 err = platform_profile_register(&platform_profile_handler);
1276 platform_profile_support = true;
1281 static int hp_wmi_hwmon_init(void);
1283 static int __init hp_wmi_bios_setup(struct platform_device *device)
1286 /* clear detected rfkill devices */
1288 bluetooth_rfkill = NULL;
1292 if (hp_wmi_rfkill_setup(device))
1293 hp_wmi_rfkill2_setup(device);
1295 err = hp_wmi_hwmon_init();
1300 thermal_profile_setup();
1305 static int __exit hp_wmi_bios_remove(struct platform_device *device)
1309 for (i = 0; i < rfkill2_count; i++) {
1310 rfkill_unregister(rfkill2[i].rfkill);
1311 rfkill_destroy(rfkill2[i].rfkill);
1315 rfkill_unregister(wifi_rfkill);
1316 rfkill_destroy(wifi_rfkill);
1318 if (bluetooth_rfkill) {
1319 rfkill_unregister(bluetooth_rfkill);
1320 rfkill_destroy(bluetooth_rfkill);
1323 rfkill_unregister(wwan_rfkill);
1324 rfkill_destroy(wwan_rfkill);
1327 if (platform_profile_support)
1328 platform_profile_remove();
1333 static int hp_wmi_resume_handler(struct device *device)
1336 * Hardware state may have changed while suspended, so trigger
1337 * input events for the current state. As this is a switch,
1338 * the input layer will only actually pass it on if the state
1341 if (hp_wmi_input_dev) {
1342 if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
1343 input_report_switch(hp_wmi_input_dev, SW_DOCK,
1344 hp_wmi_get_dock_state());
1345 if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
1346 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
1347 hp_wmi_get_tablet_mode());
1348 input_sync(hp_wmi_input_dev);
1352 hp_wmi_rfkill2_refresh();
1355 rfkill_set_states(wifi_rfkill,
1356 hp_wmi_get_sw_state(HPWMI_WIFI),
1357 hp_wmi_get_hw_state(HPWMI_WIFI));
1358 if (bluetooth_rfkill)
1359 rfkill_set_states(bluetooth_rfkill,
1360 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
1361 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
1363 rfkill_set_states(wwan_rfkill,
1364 hp_wmi_get_sw_state(HPWMI_WWAN),
1365 hp_wmi_get_hw_state(HPWMI_WWAN));
1370 static const struct dev_pm_ops hp_wmi_pm_ops = {
1371 .resume = hp_wmi_resume_handler,
1372 .restore = hp_wmi_resume_handler,
1375 static struct platform_driver hp_wmi_driver = {
1378 .pm = &hp_wmi_pm_ops,
1379 .dev_groups = hp_wmi_groups,
1381 .remove = __exit_p(hp_wmi_bios_remove),
1384 static umode_t hp_wmi_hwmon_is_visible(const void *data,
1385 enum hwmon_sensor_types type,
1386 u32 attr, int channel)
1392 if (hp_wmi_get_fan_speed(channel) >= 0)
1402 static int hp_wmi_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
1403 u32 attr, int channel, long *val)
1409 ret = hp_wmi_get_fan_speed(channel);
1416 switch (hp_wmi_fan_speed_max_get()) {
1418 /* 0 is automatic fan, which is 2 for hwmon */
1422 /* 1 is max fan, which is 0
1423 * (no fan speed control) for hwmon
1428 /* shouldn't happen */
1436 static int hp_wmi_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
1437 u32 attr, int channel, long val)
1443 /* 0 is no fan speed control (max), which is 1 for us */
1444 return hp_wmi_fan_speed_max_set(1);
1446 /* 2 is automatic speed control, which is 0 for us */
1447 return hp_wmi_fan_speed_max_set(0);
1449 /* we don't support manual fan speed control */
1457 static const struct hwmon_channel_info *info[] = {
1458 HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT, HWMON_F_INPUT),
1459 HWMON_CHANNEL_INFO(pwm, HWMON_PWM_ENABLE),
1463 static const struct hwmon_ops ops = {
1464 .is_visible = hp_wmi_hwmon_is_visible,
1465 .read = hp_wmi_hwmon_read,
1466 .write = hp_wmi_hwmon_write,
1469 static const struct hwmon_chip_info chip_info = {
1474 static int hp_wmi_hwmon_init(void)
1476 struct device *dev = &hp_wmi_platform_dev->dev;
1477 struct device *hwmon;
1479 hwmon = devm_hwmon_device_register_with_info(dev, "hp", &hp_wmi_driver,
1482 if (IS_ERR(hwmon)) {
1483 dev_err(dev, "Could not register hp hwmon device\n");
1484 return PTR_ERR(hwmon);
1490 static int __init hp_wmi_init(void)
1492 int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
1493 int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
1496 if (!bios_capable && !event_capable)
1499 if (hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, HPWMI_READ, &tmp,
1500 sizeof(tmp), sizeof(tmp)) == HPWMI_RET_INVALID_PARAMETERS)
1501 zero_insize_support = true;
1503 if (event_capable) {
1504 err = hp_wmi_input_setup();
1510 hp_wmi_platform_dev =
1511 platform_device_register_simple("hp-wmi", -1, NULL, 0);
1512 if (IS_ERR(hp_wmi_platform_dev)) {
1513 err = PTR_ERR(hp_wmi_platform_dev);
1514 goto err_destroy_input;
1517 err = platform_driver_probe(&hp_wmi_driver, hp_wmi_bios_setup);
1519 goto err_unregister_device;
1524 err_unregister_device:
1525 platform_device_unregister(hp_wmi_platform_dev);
1528 hp_wmi_input_destroy();
1532 module_init(hp_wmi_init);
1534 static void __exit hp_wmi_exit(void)
1536 if (wmi_has_guid(HPWMI_EVENT_GUID))
1537 hp_wmi_input_destroy();
1539 if (hp_wmi_platform_dev) {
1540 platform_device_unregister(hp_wmi_platform_dev);
1541 platform_driver_unregister(&hp_wmi_driver);
1544 module_exit(hp_wmi_exit);