xref: /linux/drivers/platform/x86/hp/hp-wmi.c (revision 40ccd6aa3e2e05be93394e3cd560c718dedfcc77)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * HP WMI hotkeys
4  *
5  * Copyright (C) 2008 Red Hat <mjg@redhat.com>
6  * Copyright (C) 2010, 2011 Anssi Hannula <anssi.hannula@iki.fi>
7  *
8  * Portions based on wistron_btns.c:
9  * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
10  * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
11  * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
12  */
13 
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 
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>
30 
31 MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>");
32 MODULE_DESCRIPTION("HP laptop WMI driver");
33 MODULE_LICENSE("GPL");
34 
35 MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
36 MODULE_ALIAS("wmi:5FB7F034-2C63-45E9-BE91-3D44E2C707E4");
37 
38 #define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
39 #define HPWMI_BIOS_GUID "5FB7F034-2C63-45E9-BE91-3D44E2C707E4"
40 
41 #define HP_OMEN_EC_THERMAL_PROFILE_FLAGS_OFFSET 0x62
42 #define HP_OMEN_EC_THERMAL_PROFILE_TIMER_OFFSET 0x63
43 #define HP_OMEN_EC_THERMAL_PROFILE_OFFSET 0x95
44 
45 #define zero_if_sup(tmp) (zero_insize_support?0:sizeof(tmp)) // use when zero insize is required
46 
47 /* DMI board names of devices that should use the omen specific path for
48  * thermal profiles.
49  * This was obtained by taking a look in the windows omen command center
50  * app and parsing a json file that they use to figure out what capabilities
51  * the device should have.
52  * A device is considered an omen if the DisplayName in that list contains
53  * "OMEN", and it can use the thermal profile stuff if the "Feature" array
54  * contains "PerformanceControl".
55  */
56 static const char * const omen_thermal_profile_boards[] = {
57 	"84DA", "84DB", "84DC", "8574", "8575", "860A", "87B5", "8572", "8573",
58 	"8600", "8601", "8602", "8605", "8606", "8607", "8746", "8747", "8749",
59 	"874A", "8603", "8604", "8748", "886B", "886C", "878A", "878B", "878C",
60 	"88C8", "88CB", "8786", "8787", "8788", "88D1", "88D2", "88F4", "88FD",
61 	"88F5", "88F6", "88F7", "88FE", "88FF", "8900", "8901", "8902", "8912",
62 	"8917", "8918", "8949", "894A", "89EB", "8BAD", "8A42"
63 };
64 
65 /* DMI Board names of Omen laptops that are specifically set to be thermal
66  * profile version 0 by the Omen Command Center app, regardless of what
67  * the get system design information WMI call returns
68  */
69 static const char * const omen_thermal_profile_force_v0_boards[] = {
70 	"8607", "8746", "8747", "8749", "874A", "8748"
71 };
72 
73 /* DMI board names of Omen laptops that have a thermal profile timer which will
74  * cause the embedded controller to set the thermal profile back to
75  * "balanced" when reaching zero.
76  */
77 static const char * const omen_timed_thermal_profile_boards[] = {
78 	"8BAD", "8A42"
79 };
80 
81 /* DMI Board names of Victus laptops */
82 static const char * const victus_thermal_profile_boards[] = {
83 	"8A25"
84 };
85 
86 enum hp_wmi_radio {
87 	HPWMI_WIFI	= 0x0,
88 	HPWMI_BLUETOOTH	= 0x1,
89 	HPWMI_WWAN	= 0x2,
90 	HPWMI_GPS	= 0x3,
91 };
92 
93 enum hp_wmi_event_ids {
94 	HPWMI_DOCK_EVENT		= 0x01,
95 	HPWMI_PARK_HDD			= 0x02,
96 	HPWMI_SMART_ADAPTER		= 0x03,
97 	HPWMI_BEZEL_BUTTON		= 0x04,
98 	HPWMI_WIRELESS			= 0x05,
99 	HPWMI_CPU_BATTERY_THROTTLE	= 0x06,
100 	HPWMI_LOCK_SWITCH		= 0x07,
101 	HPWMI_LID_SWITCH		= 0x08,
102 	HPWMI_SCREEN_ROTATION		= 0x09,
103 	HPWMI_COOLSENSE_SYSTEM_MOBILE	= 0x0A,
104 	HPWMI_COOLSENSE_SYSTEM_HOT	= 0x0B,
105 	HPWMI_PROXIMITY_SENSOR		= 0x0C,
106 	HPWMI_BACKLIT_KB_BRIGHTNESS	= 0x0D,
107 	HPWMI_PEAKSHIFT_PERIOD		= 0x0F,
108 	HPWMI_BATTERY_CHARGE_PERIOD	= 0x10,
109 	HPWMI_SANITIZATION_MODE		= 0x17,
110 	HPWMI_CAMERA_TOGGLE		= 0x1A,
111 	HPWMI_OMEN_KEY			= 0x1D,
112 	HPWMI_SMART_EXPERIENCE_APP	= 0x21,
113 };
114 
115 /*
116  * struct bios_args buffer is dynamically allocated.  New WMI command types
117  * were introduced that exceeds 128-byte data size.  Changes to handle
118  * the data size allocation scheme were kept in hp_wmi_perform_qurey function.
119  */
120 struct bios_args {
121 	u32 signature;
122 	u32 command;
123 	u32 commandtype;
124 	u32 datasize;
125 	u8 data[];
126 };
127 
128 enum hp_wmi_commandtype {
129 	HPWMI_DISPLAY_QUERY		= 0x01,
130 	HPWMI_HDDTEMP_QUERY		= 0x02,
131 	HPWMI_ALS_QUERY			= 0x03,
132 	HPWMI_HARDWARE_QUERY		= 0x04,
133 	HPWMI_WIRELESS_QUERY		= 0x05,
134 	HPWMI_BATTERY_QUERY		= 0x07,
135 	HPWMI_BIOS_QUERY		= 0x09,
136 	HPWMI_FEATURE_QUERY		= 0x0b,
137 	HPWMI_HOTKEY_QUERY		= 0x0c,
138 	HPWMI_FEATURE2_QUERY		= 0x0d,
139 	HPWMI_WIRELESS2_QUERY		= 0x1b,
140 	HPWMI_POSTCODEERROR_QUERY	= 0x2a,
141 	HPWMI_SYSTEM_DEVICE_MODE	= 0x40,
142 	HPWMI_THERMAL_PROFILE_QUERY	= 0x4c,
143 };
144 
145 enum hp_wmi_gm_commandtype {
146 	HPWMI_FAN_SPEED_GET_QUERY = 0x11,
147 	HPWMI_SET_PERFORMANCE_MODE = 0x1A,
148 	HPWMI_FAN_SPEED_MAX_GET_QUERY = 0x26,
149 	HPWMI_FAN_SPEED_MAX_SET_QUERY = 0x27,
150 	HPWMI_GET_SYSTEM_DESIGN_DATA = 0x28,
151 };
152 
153 enum hp_wmi_command {
154 	HPWMI_READ	= 0x01,
155 	HPWMI_WRITE	= 0x02,
156 	HPWMI_ODM	= 0x03,
157 	HPWMI_GM	= 0x20008,
158 };
159 
160 enum hp_wmi_hardware_mask {
161 	HPWMI_DOCK_MASK		= 0x01,
162 	HPWMI_TABLET_MASK	= 0x04,
163 };
164 
165 struct bios_return {
166 	u32 sigpass;
167 	u32 return_code;
168 };
169 
170 enum hp_return_value {
171 	HPWMI_RET_WRONG_SIGNATURE	= 0x02,
172 	HPWMI_RET_UNKNOWN_COMMAND	= 0x03,
173 	HPWMI_RET_UNKNOWN_CMDTYPE	= 0x04,
174 	HPWMI_RET_INVALID_PARAMETERS	= 0x05,
175 };
176 
177 enum hp_wireless2_bits {
178 	HPWMI_POWER_STATE	= 0x01,
179 	HPWMI_POWER_SOFT	= 0x02,
180 	HPWMI_POWER_BIOS	= 0x04,
181 	HPWMI_POWER_HARD	= 0x08,
182 	HPWMI_POWER_FW_OR_HW	= HPWMI_POWER_BIOS | HPWMI_POWER_HARD,
183 };
184 
185 enum hp_thermal_profile_omen_v0 {
186 	HP_OMEN_V0_THERMAL_PROFILE_DEFAULT     = 0x00,
187 	HP_OMEN_V0_THERMAL_PROFILE_PERFORMANCE = 0x01,
188 	HP_OMEN_V0_THERMAL_PROFILE_COOL        = 0x02,
189 };
190 
191 enum hp_thermal_profile_omen_v1 {
192 	HP_OMEN_V1_THERMAL_PROFILE_DEFAULT	= 0x30,
193 	HP_OMEN_V1_THERMAL_PROFILE_PERFORMANCE	= 0x31,
194 	HP_OMEN_V1_THERMAL_PROFILE_COOL		= 0x50,
195 };
196 
197 enum hp_thermal_profile_omen_flags {
198 	HP_OMEN_EC_FLAGS_TURBO		= 0x04,
199 	HP_OMEN_EC_FLAGS_NOTIMER	= 0x02,
200 	HP_OMEN_EC_FLAGS_JUSTSET	= 0x01,
201 };
202 
203 enum hp_thermal_profile_victus {
204 	HP_VICTUS_THERMAL_PROFILE_DEFAULT		= 0x00,
205 	HP_VICTUS_THERMAL_PROFILE_PERFORMANCE		= 0x01,
206 	HP_VICTUS_THERMAL_PROFILE_QUIET			= 0x03,
207 };
208 
209 enum hp_thermal_profile {
210 	HP_THERMAL_PROFILE_PERFORMANCE	= 0x00,
211 	HP_THERMAL_PROFILE_DEFAULT		= 0x01,
212 	HP_THERMAL_PROFILE_COOL			= 0x02,
213 	HP_THERMAL_PROFILE_QUIET		= 0x03,
214 };
215 
216 #define IS_HWBLOCKED(x) ((x & HPWMI_POWER_FW_OR_HW) != HPWMI_POWER_FW_OR_HW)
217 #define IS_SWBLOCKED(x) !(x & HPWMI_POWER_SOFT)
218 
219 struct bios_rfkill2_device_state {
220 	u8 radio_type;
221 	u8 bus_type;
222 	u16 vendor_id;
223 	u16 product_id;
224 	u16 subsys_vendor_id;
225 	u16 subsys_product_id;
226 	u8 rfkill_id;
227 	u8 power;
228 	u8 unknown[4];
229 };
230 
231 /* 7 devices fit into the 128 byte buffer */
232 #define HPWMI_MAX_RFKILL2_DEVICES	7
233 
234 struct bios_rfkill2_state {
235 	u8 unknown[7];
236 	u8 count;
237 	u8 pad[8];
238 	struct bios_rfkill2_device_state device[HPWMI_MAX_RFKILL2_DEVICES];
239 };
240 
241 static const struct key_entry hp_wmi_keymap[] = {
242 	{ KE_KEY, 0x02,    { KEY_BRIGHTNESSUP } },
243 	{ KE_KEY, 0x03,    { KEY_BRIGHTNESSDOWN } },
244 	{ KE_KEY, 0x270,   { KEY_MICMUTE } },
245 	{ KE_KEY, 0x20e6,  { KEY_PROG1 } },
246 	{ KE_KEY, 0x20e8,  { KEY_MEDIA } },
247 	{ KE_KEY, 0x2142,  { KEY_MEDIA } },
248 	{ KE_KEY, 0x213b,  { KEY_INFO } },
249 	{ KE_KEY, 0x2169,  { KEY_ROTATE_DISPLAY } },
250 	{ KE_KEY, 0x216a,  { KEY_SETUP } },
251 	{ KE_IGNORE, 0x21a4,  }, /* Win Lock On */
252 	{ KE_IGNORE, 0x121a4, }, /* Win Lock Off */
253 	{ KE_KEY, 0x21a5,  { KEY_PROG2 } }, /* HP Omen Key */
254 	{ KE_KEY, 0x21a7,  { KEY_FN_ESC } },
255 	{ KE_KEY, 0x21a8,  { KEY_PROG2 } }, /* HP Envy x360 programmable key */
256 	{ KE_KEY, 0x21a9,  { KEY_TOUCHPAD_OFF } },
257 	{ KE_KEY, 0x121a9, { KEY_TOUCHPAD_ON } },
258 	{ KE_KEY, 0x231b,  { KEY_HELP } },
259 	{ KE_END, 0 }
260 };
261 
262 static struct input_dev *hp_wmi_input_dev;
263 static struct input_dev *camera_shutter_input_dev;
264 static struct platform_device *hp_wmi_platform_dev;
265 static struct platform_profile_handler platform_profile_handler;
266 static bool platform_profile_support;
267 static bool zero_insize_support;
268 
269 static struct rfkill *wifi_rfkill;
270 static struct rfkill *bluetooth_rfkill;
271 static struct rfkill *wwan_rfkill;
272 
273 struct rfkill2_device {
274 	u8 id;
275 	int num;
276 	struct rfkill *rfkill;
277 };
278 
279 static int rfkill2_count;
280 static struct rfkill2_device rfkill2[HPWMI_MAX_RFKILL2_DEVICES];
281 
282 /*
283  * Chassis Types values were obtained from SMBIOS reference
284  * specification version 3.00. A complete list of system enclosures
285  * and chassis types is available on Table 17.
286  */
287 static const char * const tablet_chassis_types[] = {
288 	"30", /* Tablet*/
289 	"31", /* Convertible */
290 	"32"  /* Detachable */
291 };
292 
293 #define DEVICE_MODE_TABLET	0x06
294 
295 /* map output size to the corresponding WMI method id */
296 static inline int encode_outsize_for_pvsz(int outsize)
297 {
298 	if (outsize > 4096)
299 		return -EINVAL;
300 	if (outsize > 1024)
301 		return 5;
302 	if (outsize > 128)
303 		return 4;
304 	if (outsize > 4)
305 		return 3;
306 	if (outsize > 0)
307 		return 2;
308 	return 1;
309 }
310 
311 /*
312  * hp_wmi_perform_query
313  *
314  * query:	The commandtype (enum hp_wmi_commandtype)
315  * write:	The command (enum hp_wmi_command)
316  * buffer:	Buffer used as input and/or output
317  * insize:	Size of input buffer
318  * outsize:	Size of output buffer
319  *
320  * returns zero on success
321  *         an HP WMI query specific error code (which is positive)
322  *         -EINVAL if the query was not successful at all
323  *         -EINVAL if the output buffer size exceeds buffersize
324  *
325  * Note: The buffersize must at least be the maximum of the input and output
326  *       size. E.g. Battery info query is defined to have 1 byte input
327  *       and 128 byte output. The caller would do:
328  *       buffer = kzalloc(128, GFP_KERNEL);
329  *       ret = hp_wmi_perform_query(HPWMI_BATTERY_QUERY, HPWMI_READ, buffer, 1, 128)
330  */
331 static int hp_wmi_perform_query(int query, enum hp_wmi_command command,
332 				void *buffer, int insize, int outsize)
333 {
334 	struct acpi_buffer input, output = { ACPI_ALLOCATE_BUFFER, NULL };
335 	struct bios_return *bios_return;
336 	union acpi_object *obj = NULL;
337 	struct bios_args *args = NULL;
338 	int mid, actual_insize, actual_outsize;
339 	size_t bios_args_size;
340 	int ret;
341 
342 	mid = encode_outsize_for_pvsz(outsize);
343 	if (WARN_ON(mid < 0))
344 		return mid;
345 
346 	actual_insize = max(insize, 128);
347 	bios_args_size = struct_size(args, data, actual_insize);
348 	args = kmalloc(bios_args_size, GFP_KERNEL);
349 	if (!args)
350 		return -ENOMEM;
351 
352 	input.length = bios_args_size;
353 	input.pointer = args;
354 
355 	args->signature = 0x55434553;
356 	args->command = command;
357 	args->commandtype = query;
358 	args->datasize = insize;
359 	memcpy(args->data, buffer, flex_array_size(args, data, insize));
360 
361 	ret = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, mid, &input, &output);
362 	if (ret)
363 		goto out_free;
364 
365 	obj = output.pointer;
366 	if (!obj) {
367 		ret = -EINVAL;
368 		goto out_free;
369 	}
370 
371 	if (obj->type != ACPI_TYPE_BUFFER) {
372 		pr_warn("query 0x%x returned an invalid object 0x%x\n", query, ret);
373 		ret = -EINVAL;
374 		goto out_free;
375 	}
376 
377 	bios_return = (struct bios_return *)obj->buffer.pointer;
378 	ret = bios_return->return_code;
379 
380 	if (ret) {
381 		if (ret != HPWMI_RET_UNKNOWN_COMMAND &&
382 		    ret != HPWMI_RET_UNKNOWN_CMDTYPE)
383 			pr_warn("query 0x%x returned error 0x%x\n", query, ret);
384 		goto out_free;
385 	}
386 
387 	/* Ignore output data of zero size */
388 	if (!outsize)
389 		goto out_free;
390 
391 	actual_outsize = min(outsize, (int)(obj->buffer.length - sizeof(*bios_return)));
392 	memcpy(buffer, obj->buffer.pointer + sizeof(*bios_return), actual_outsize);
393 	memset(buffer + actual_outsize, 0, outsize - actual_outsize);
394 
395 out_free:
396 	kfree(obj);
397 	kfree(args);
398 	return ret;
399 }
400 
401 static int hp_wmi_get_fan_speed(int fan)
402 {
403 	u8 fsh, fsl;
404 	char fan_data[4] = { fan, 0, 0, 0 };
405 
406 	int ret = hp_wmi_perform_query(HPWMI_FAN_SPEED_GET_QUERY, HPWMI_GM,
407 				       &fan_data, sizeof(char),
408 				       sizeof(fan_data));
409 
410 	if (ret != 0)
411 		return -EINVAL;
412 
413 	fsh = fan_data[2];
414 	fsl = fan_data[3];
415 
416 	return (fsh << 8) | fsl;
417 }
418 
419 static int hp_wmi_read_int(int query)
420 {
421 	int val = 0, ret;
422 
423 	ret = hp_wmi_perform_query(query, HPWMI_READ, &val,
424 				   zero_if_sup(val), sizeof(val));
425 
426 	if (ret)
427 		return ret < 0 ? ret : -EINVAL;
428 
429 	return val;
430 }
431 
432 static int hp_wmi_get_dock_state(void)
433 {
434 	int state = hp_wmi_read_int(HPWMI_HARDWARE_QUERY);
435 
436 	if (state < 0)
437 		return state;
438 
439 	return !!(state & HPWMI_DOCK_MASK);
440 }
441 
442 static int hp_wmi_get_tablet_mode(void)
443 {
444 	char system_device_mode[4] = { 0 };
445 	const char *chassis_type;
446 	bool tablet_found;
447 	int ret;
448 
449 	chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
450 	if (!chassis_type)
451 		return -ENODEV;
452 
453 	tablet_found = match_string(tablet_chassis_types,
454 				    ARRAY_SIZE(tablet_chassis_types),
455 				    chassis_type) >= 0;
456 	if (!tablet_found)
457 		return -ENODEV;
458 
459 	ret = hp_wmi_perform_query(HPWMI_SYSTEM_DEVICE_MODE, HPWMI_READ,
460 				   system_device_mode, zero_if_sup(system_device_mode),
461 				   sizeof(system_device_mode));
462 	if (ret < 0)
463 		return ret;
464 
465 	return system_device_mode[0] == DEVICE_MODE_TABLET;
466 }
467 
468 static int omen_thermal_profile_set(int mode)
469 {
470 	/* The Omen Control Center actively sets the first byte of the buffer to
471 	 * 255, so let's mimic this behaviour to be as close as possible to
472 	 * the original software.
473 	 */
474 	char buffer[2] = {-1, mode};
475 	int ret;
476 
477 	ret = hp_wmi_perform_query(HPWMI_SET_PERFORMANCE_MODE, HPWMI_GM,
478 				   &buffer, sizeof(buffer), 0);
479 
480 	if (ret)
481 		return ret < 0 ? ret : -EINVAL;
482 
483 	return mode;
484 }
485 
486 static bool is_omen_thermal_profile(void)
487 {
488 	const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
489 
490 	if (!board_name)
491 		return false;
492 
493 	return match_string(omen_thermal_profile_boards,
494 			    ARRAY_SIZE(omen_thermal_profile_boards),
495 			    board_name) >= 0;
496 }
497 
498 static int omen_get_thermal_policy_version(void)
499 {
500 	unsigned char buffer[8] = { 0 };
501 	int ret;
502 
503 	const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
504 
505 	if (board_name) {
506 		int matches = match_string(omen_thermal_profile_force_v0_boards,
507 			ARRAY_SIZE(omen_thermal_profile_force_v0_boards),
508 			board_name);
509 		if (matches >= 0)
510 			return 0;
511 	}
512 
513 	ret = hp_wmi_perform_query(HPWMI_GET_SYSTEM_DESIGN_DATA, HPWMI_GM,
514 				   &buffer, sizeof(buffer), sizeof(buffer));
515 
516 	if (ret)
517 		return ret < 0 ? ret : -EINVAL;
518 
519 	return buffer[3];
520 }
521 
522 static int omen_thermal_profile_get(void)
523 {
524 	u8 data;
525 
526 	int ret = ec_read(HP_OMEN_EC_THERMAL_PROFILE_OFFSET, &data);
527 
528 	if (ret)
529 		return ret;
530 
531 	return data;
532 }
533 
534 static int hp_wmi_fan_speed_max_set(int enabled)
535 {
536 	int ret;
537 
538 	ret = hp_wmi_perform_query(HPWMI_FAN_SPEED_MAX_SET_QUERY, HPWMI_GM,
539 				   &enabled, sizeof(enabled), 0);
540 
541 	if (ret)
542 		return ret < 0 ? ret : -EINVAL;
543 
544 	return enabled;
545 }
546 
547 static int hp_wmi_fan_speed_max_get(void)
548 {
549 	int val = 0, ret;
550 
551 	ret = hp_wmi_perform_query(HPWMI_FAN_SPEED_MAX_GET_QUERY, HPWMI_GM,
552 				   &val, zero_if_sup(val), sizeof(val));
553 
554 	if (ret)
555 		return ret < 0 ? ret : -EINVAL;
556 
557 	return val;
558 }
559 
560 static int __init hp_wmi_bios_2008_later(void)
561 {
562 	int state = 0;
563 	int ret = hp_wmi_perform_query(HPWMI_FEATURE_QUERY, HPWMI_READ, &state,
564 				       zero_if_sup(state), sizeof(state));
565 	if (!ret)
566 		return 1;
567 
568 	return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
569 }
570 
571 static int __init hp_wmi_bios_2009_later(void)
572 {
573 	u8 state[128];
574 	int ret = hp_wmi_perform_query(HPWMI_FEATURE2_QUERY, HPWMI_READ, &state,
575 				       zero_if_sup(state), sizeof(state));
576 	if (!ret)
577 		return 1;
578 
579 	return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
580 }
581 
582 static int __init hp_wmi_enable_hotkeys(void)
583 {
584 	int value = 0x6e;
585 	int ret = hp_wmi_perform_query(HPWMI_BIOS_QUERY, HPWMI_WRITE, &value,
586 				       sizeof(value), 0);
587 
588 	return ret <= 0 ? ret : -EINVAL;
589 }
590 
591 static int hp_wmi_set_block(void *data, bool blocked)
592 {
593 	enum hp_wmi_radio r = (long)data;
594 	int query = BIT(r + 8) | ((!blocked) << r);
595 	int ret;
596 
597 	ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE,
598 				   &query, sizeof(query), 0);
599 
600 	return ret <= 0 ? ret : -EINVAL;
601 }
602 
603 static const struct rfkill_ops hp_wmi_rfkill_ops = {
604 	.set_block = hp_wmi_set_block,
605 };
606 
607 static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
608 {
609 	int mask = 0x200 << (r * 8);
610 
611 	int wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
612 
613 	/* TBD: Pass error */
614 	WARN_ONCE(wireless < 0, "error executing HPWMI_WIRELESS_QUERY");
615 
616 	return !(wireless & mask);
617 }
618 
619 static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
620 {
621 	int mask = 0x800 << (r * 8);
622 
623 	int wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
624 
625 	/* TBD: Pass error */
626 	WARN_ONCE(wireless < 0, "error executing HPWMI_WIRELESS_QUERY");
627 
628 	return !(wireless & mask);
629 }
630 
631 static int hp_wmi_rfkill2_set_block(void *data, bool blocked)
632 {
633 	int rfkill_id = (int)(long)data;
634 	char buffer[4] = { 0x01, 0x00, rfkill_id, !blocked };
635 	int ret;
636 
637 	ret = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_WRITE,
638 				   buffer, sizeof(buffer), 0);
639 
640 	return ret <= 0 ? ret : -EINVAL;
641 }
642 
643 static const struct rfkill_ops hp_wmi_rfkill2_ops = {
644 	.set_block = hp_wmi_rfkill2_set_block,
645 };
646 
647 static int hp_wmi_rfkill2_refresh(void)
648 {
649 	struct bios_rfkill2_state state;
650 	int err, i;
651 
652 	err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
653 				   zero_if_sup(state), sizeof(state));
654 	if (err)
655 		return err;
656 
657 	for (i = 0; i < rfkill2_count; i++) {
658 		int num = rfkill2[i].num;
659 		struct bios_rfkill2_device_state *devstate;
660 
661 		devstate = &state.device[num];
662 
663 		if (num >= state.count ||
664 		    devstate->rfkill_id != rfkill2[i].id) {
665 			pr_warn("power configuration of the wireless devices unexpectedly changed\n");
666 			continue;
667 		}
668 
669 		rfkill_set_states(rfkill2[i].rfkill,
670 				  IS_SWBLOCKED(devstate->power),
671 				  IS_HWBLOCKED(devstate->power));
672 	}
673 
674 	return 0;
675 }
676 
677 static ssize_t display_show(struct device *dev, struct device_attribute *attr,
678 			    char *buf)
679 {
680 	int value = hp_wmi_read_int(HPWMI_DISPLAY_QUERY);
681 
682 	if (value < 0)
683 		return value;
684 	return sysfs_emit(buf, "%d\n", value);
685 }
686 
687 static ssize_t hddtemp_show(struct device *dev, struct device_attribute *attr,
688 			    char *buf)
689 {
690 	int value = hp_wmi_read_int(HPWMI_HDDTEMP_QUERY);
691 
692 	if (value < 0)
693 		return value;
694 	return sysfs_emit(buf, "%d\n", value);
695 }
696 
697 static ssize_t als_show(struct device *dev, struct device_attribute *attr,
698 			char *buf)
699 {
700 	int value = hp_wmi_read_int(HPWMI_ALS_QUERY);
701 
702 	if (value < 0)
703 		return value;
704 	return sysfs_emit(buf, "%d\n", value);
705 }
706 
707 static ssize_t dock_show(struct device *dev, struct device_attribute *attr,
708 			 char *buf)
709 {
710 	int value = hp_wmi_get_dock_state();
711 
712 	if (value < 0)
713 		return value;
714 	return sysfs_emit(buf, "%d\n", value);
715 }
716 
717 static ssize_t tablet_show(struct device *dev, struct device_attribute *attr,
718 			   char *buf)
719 {
720 	int value = hp_wmi_get_tablet_mode();
721 
722 	if (value < 0)
723 		return value;
724 	return sysfs_emit(buf, "%d\n", value);
725 }
726 
727 static ssize_t postcode_show(struct device *dev, struct device_attribute *attr,
728 			     char *buf)
729 {
730 	/* Get the POST error code of previous boot failure. */
731 	int value = hp_wmi_read_int(HPWMI_POSTCODEERROR_QUERY);
732 
733 	if (value < 0)
734 		return value;
735 	return sysfs_emit(buf, "0x%x\n", value);
736 }
737 
738 static ssize_t als_store(struct device *dev, struct device_attribute *attr,
739 			 const char *buf, size_t count)
740 {
741 	u32 tmp;
742 	int ret;
743 
744 	ret = kstrtou32(buf, 10, &tmp);
745 	if (ret)
746 		return ret;
747 
748 	ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, HPWMI_WRITE, &tmp,
749 				       sizeof(tmp), 0);
750 	if (ret)
751 		return ret < 0 ? ret : -EINVAL;
752 
753 	return count;
754 }
755 
756 static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,
757 			      const char *buf, size_t count)
758 {
759 	u32 tmp = 1;
760 	bool clear;
761 	int ret;
762 
763 	ret = kstrtobool(buf, &clear);
764 	if (ret)
765 		return ret;
766 
767 	if (clear == false)
768 		return -EINVAL;
769 
770 	/* Clear the POST error code. It is kept until cleared. */
771 	ret = hp_wmi_perform_query(HPWMI_POSTCODEERROR_QUERY, HPWMI_WRITE, &tmp,
772 				       sizeof(tmp), 0);
773 	if (ret)
774 		return ret < 0 ? ret : -EINVAL;
775 
776 	return count;
777 }
778 
779 static int camera_shutter_input_setup(void)
780 {
781 	int err;
782 
783 	camera_shutter_input_dev = input_allocate_device();
784 	if (!camera_shutter_input_dev)
785 		return -ENOMEM;
786 
787 	camera_shutter_input_dev->name = "HP WMI camera shutter";
788 	camera_shutter_input_dev->phys = "wmi/input1";
789 	camera_shutter_input_dev->id.bustype = BUS_HOST;
790 
791 	__set_bit(EV_SW, camera_shutter_input_dev->evbit);
792 	__set_bit(SW_CAMERA_LENS_COVER, camera_shutter_input_dev->swbit);
793 
794 	err = input_register_device(camera_shutter_input_dev);
795 	if (err)
796 		goto err_free_dev;
797 
798 	return 0;
799 
800  err_free_dev:
801 	input_free_device(camera_shutter_input_dev);
802 	camera_shutter_input_dev = NULL;
803 	return err;
804 }
805 
806 static DEVICE_ATTR_RO(display);
807 static DEVICE_ATTR_RO(hddtemp);
808 static DEVICE_ATTR_RW(als);
809 static DEVICE_ATTR_RO(dock);
810 static DEVICE_ATTR_RO(tablet);
811 static DEVICE_ATTR_RW(postcode);
812 
813 static struct attribute *hp_wmi_attrs[] = {
814 	&dev_attr_display.attr,
815 	&dev_attr_hddtemp.attr,
816 	&dev_attr_als.attr,
817 	&dev_attr_dock.attr,
818 	&dev_attr_tablet.attr,
819 	&dev_attr_postcode.attr,
820 	NULL,
821 };
822 ATTRIBUTE_GROUPS(hp_wmi);
823 
824 static void hp_wmi_notify(u32 value, void *context)
825 {
826 	struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
827 	u32 event_id, event_data;
828 	union acpi_object *obj;
829 	acpi_status status;
830 	u32 *location;
831 	int key_code;
832 
833 	status = wmi_get_event_data(value, &response);
834 	if (status != AE_OK) {
835 		pr_info("bad event status 0x%x\n", status);
836 		return;
837 	}
838 
839 	obj = (union acpi_object *)response.pointer;
840 
841 	if (!obj)
842 		return;
843 	if (obj->type != ACPI_TYPE_BUFFER) {
844 		pr_info("Unknown response received %d\n", obj->type);
845 		kfree(obj);
846 		return;
847 	}
848 
849 	/*
850 	 * Depending on ACPI version the concatenation of id and event data
851 	 * inside _WED function will result in a 8 or 16 byte buffer.
852 	 */
853 	location = (u32 *)obj->buffer.pointer;
854 	if (obj->buffer.length == 8) {
855 		event_id = *location;
856 		event_data = *(location + 1);
857 	} else if (obj->buffer.length == 16) {
858 		event_id = *location;
859 		event_data = *(location + 2);
860 	} else {
861 		pr_info("Unknown buffer length %d\n", obj->buffer.length);
862 		kfree(obj);
863 		return;
864 	}
865 	kfree(obj);
866 
867 	switch (event_id) {
868 	case HPWMI_DOCK_EVENT:
869 		if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
870 			input_report_switch(hp_wmi_input_dev, SW_DOCK,
871 					    hp_wmi_get_dock_state());
872 		if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
873 			input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
874 					    hp_wmi_get_tablet_mode());
875 		input_sync(hp_wmi_input_dev);
876 		break;
877 	case HPWMI_PARK_HDD:
878 		break;
879 	case HPWMI_SMART_ADAPTER:
880 		break;
881 	case HPWMI_BEZEL_BUTTON:
882 		key_code = hp_wmi_read_int(HPWMI_HOTKEY_QUERY);
883 		if (key_code < 0)
884 			break;
885 
886 		if (!sparse_keymap_report_event(hp_wmi_input_dev,
887 						key_code, 1, true))
888 			pr_info("Unknown key code - 0x%x\n", key_code);
889 		break;
890 	case HPWMI_OMEN_KEY:
891 		if (event_data) /* Only should be true for HP Omen */
892 			key_code = event_data;
893 		else
894 			key_code = hp_wmi_read_int(HPWMI_HOTKEY_QUERY);
895 
896 		if (!sparse_keymap_report_event(hp_wmi_input_dev,
897 						key_code, 1, true))
898 			pr_info("Unknown key code - 0x%x\n", key_code);
899 		break;
900 	case HPWMI_WIRELESS:
901 		if (rfkill2_count) {
902 			hp_wmi_rfkill2_refresh();
903 			break;
904 		}
905 
906 		if (wifi_rfkill)
907 			rfkill_set_states(wifi_rfkill,
908 					  hp_wmi_get_sw_state(HPWMI_WIFI),
909 					  hp_wmi_get_hw_state(HPWMI_WIFI));
910 		if (bluetooth_rfkill)
911 			rfkill_set_states(bluetooth_rfkill,
912 					  hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
913 					  hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
914 		if (wwan_rfkill)
915 			rfkill_set_states(wwan_rfkill,
916 					  hp_wmi_get_sw_state(HPWMI_WWAN),
917 					  hp_wmi_get_hw_state(HPWMI_WWAN));
918 		break;
919 	case HPWMI_CPU_BATTERY_THROTTLE:
920 		pr_info("Unimplemented CPU throttle because of 3 Cell battery event detected\n");
921 		break;
922 	case HPWMI_LOCK_SWITCH:
923 		break;
924 	case HPWMI_LID_SWITCH:
925 		break;
926 	case HPWMI_SCREEN_ROTATION:
927 		break;
928 	case HPWMI_COOLSENSE_SYSTEM_MOBILE:
929 		break;
930 	case HPWMI_COOLSENSE_SYSTEM_HOT:
931 		break;
932 	case HPWMI_PROXIMITY_SENSOR:
933 		break;
934 	case HPWMI_BACKLIT_KB_BRIGHTNESS:
935 		break;
936 	case HPWMI_PEAKSHIFT_PERIOD:
937 		break;
938 	case HPWMI_BATTERY_CHARGE_PERIOD:
939 		break;
940 	case HPWMI_SANITIZATION_MODE:
941 		break;
942 	case HPWMI_CAMERA_TOGGLE:
943 		if (!camera_shutter_input_dev)
944 			if (camera_shutter_input_setup()) {
945 				pr_err("Failed to setup camera shutter input device\n");
946 				break;
947 			}
948 		if (event_data == 0xff)
949 			input_report_switch(camera_shutter_input_dev, SW_CAMERA_LENS_COVER, 1);
950 		else if (event_data == 0xfe)
951 			input_report_switch(camera_shutter_input_dev, SW_CAMERA_LENS_COVER, 0);
952 		else
953 			pr_warn("Unknown camera shutter state - 0x%x\n", event_data);
954 		input_sync(camera_shutter_input_dev);
955 		break;
956 	case HPWMI_SMART_EXPERIENCE_APP:
957 		break;
958 	default:
959 		pr_info("Unknown event_id - %d - 0x%x\n", event_id, event_data);
960 		break;
961 	}
962 }
963 
964 static int __init hp_wmi_input_setup(void)
965 {
966 	acpi_status status;
967 	int err, val;
968 
969 	hp_wmi_input_dev = input_allocate_device();
970 	if (!hp_wmi_input_dev)
971 		return -ENOMEM;
972 
973 	hp_wmi_input_dev->name = "HP WMI hotkeys";
974 	hp_wmi_input_dev->phys = "wmi/input0";
975 	hp_wmi_input_dev->id.bustype = BUS_HOST;
976 
977 	__set_bit(EV_SW, hp_wmi_input_dev->evbit);
978 
979 	/* Dock */
980 	val = hp_wmi_get_dock_state();
981 	if (!(val < 0)) {
982 		__set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
983 		input_report_switch(hp_wmi_input_dev, SW_DOCK, val);
984 	}
985 
986 	/* Tablet mode */
987 	val = hp_wmi_get_tablet_mode();
988 	if (!(val < 0)) {
989 		__set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
990 		input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE, val);
991 	}
992 
993 	err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
994 	if (err)
995 		goto err_free_dev;
996 
997 	/* Set initial hardware state */
998 	input_sync(hp_wmi_input_dev);
999 
1000 	if (!hp_wmi_bios_2009_later() && hp_wmi_bios_2008_later())
1001 		hp_wmi_enable_hotkeys();
1002 
1003 	status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
1004 	if (ACPI_FAILURE(status)) {
1005 		err = -EIO;
1006 		goto err_free_dev;
1007 	}
1008 
1009 	err = input_register_device(hp_wmi_input_dev);
1010 	if (err)
1011 		goto err_uninstall_notifier;
1012 
1013 	return 0;
1014 
1015  err_uninstall_notifier:
1016 	wmi_remove_notify_handler(HPWMI_EVENT_GUID);
1017  err_free_dev:
1018 	input_free_device(hp_wmi_input_dev);
1019 	return err;
1020 }
1021 
1022 static void hp_wmi_input_destroy(void)
1023 {
1024 	wmi_remove_notify_handler(HPWMI_EVENT_GUID);
1025 	input_unregister_device(hp_wmi_input_dev);
1026 }
1027 
1028 static int __init hp_wmi_rfkill_setup(struct platform_device *device)
1029 {
1030 	int err, wireless;
1031 
1032 	wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
1033 	if (wireless < 0)
1034 		return wireless;
1035 
1036 	err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE, &wireless,
1037 				   sizeof(wireless), 0);
1038 	if (err)
1039 		return err;
1040 
1041 	if (wireless & 0x1) {
1042 		wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
1043 					   RFKILL_TYPE_WLAN,
1044 					   &hp_wmi_rfkill_ops,
1045 					   (void *) HPWMI_WIFI);
1046 		if (!wifi_rfkill)
1047 			return -ENOMEM;
1048 		rfkill_init_sw_state(wifi_rfkill,
1049 				     hp_wmi_get_sw_state(HPWMI_WIFI));
1050 		rfkill_set_hw_state(wifi_rfkill,
1051 				    hp_wmi_get_hw_state(HPWMI_WIFI));
1052 		err = rfkill_register(wifi_rfkill);
1053 		if (err)
1054 			goto register_wifi_error;
1055 	}
1056 
1057 	if (wireless & 0x2) {
1058 		bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
1059 						RFKILL_TYPE_BLUETOOTH,
1060 						&hp_wmi_rfkill_ops,
1061 						(void *) HPWMI_BLUETOOTH);
1062 		if (!bluetooth_rfkill) {
1063 			err = -ENOMEM;
1064 			goto register_bluetooth_error;
1065 		}
1066 		rfkill_init_sw_state(bluetooth_rfkill,
1067 				     hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
1068 		rfkill_set_hw_state(bluetooth_rfkill,
1069 				    hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
1070 		err = rfkill_register(bluetooth_rfkill);
1071 		if (err)
1072 			goto register_bluetooth_error;
1073 	}
1074 
1075 	if (wireless & 0x4) {
1076 		wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
1077 					   RFKILL_TYPE_WWAN,
1078 					   &hp_wmi_rfkill_ops,
1079 					   (void *) HPWMI_WWAN);
1080 		if (!wwan_rfkill) {
1081 			err = -ENOMEM;
1082 			goto register_wwan_error;
1083 		}
1084 		rfkill_init_sw_state(wwan_rfkill,
1085 				     hp_wmi_get_sw_state(HPWMI_WWAN));
1086 		rfkill_set_hw_state(wwan_rfkill,
1087 				    hp_wmi_get_hw_state(HPWMI_WWAN));
1088 		err = rfkill_register(wwan_rfkill);
1089 		if (err)
1090 			goto register_wwan_error;
1091 	}
1092 
1093 	return 0;
1094 
1095 register_wwan_error:
1096 	rfkill_destroy(wwan_rfkill);
1097 	wwan_rfkill = NULL;
1098 	if (bluetooth_rfkill)
1099 		rfkill_unregister(bluetooth_rfkill);
1100 register_bluetooth_error:
1101 	rfkill_destroy(bluetooth_rfkill);
1102 	bluetooth_rfkill = NULL;
1103 	if (wifi_rfkill)
1104 		rfkill_unregister(wifi_rfkill);
1105 register_wifi_error:
1106 	rfkill_destroy(wifi_rfkill);
1107 	wifi_rfkill = NULL;
1108 	return err;
1109 }
1110 
1111 static int __init hp_wmi_rfkill2_setup(struct platform_device *device)
1112 {
1113 	struct bios_rfkill2_state state;
1114 	int err, i;
1115 
1116 	err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
1117 				   zero_if_sup(state), sizeof(state));
1118 	if (err)
1119 		return err < 0 ? err : -EINVAL;
1120 
1121 	if (state.count > HPWMI_MAX_RFKILL2_DEVICES) {
1122 		pr_warn("unable to parse 0x1b query output\n");
1123 		return -EINVAL;
1124 	}
1125 
1126 	for (i = 0; i < state.count; i++) {
1127 		struct rfkill *rfkill;
1128 		enum rfkill_type type;
1129 		char *name;
1130 
1131 		switch (state.device[i].radio_type) {
1132 		case HPWMI_WIFI:
1133 			type = RFKILL_TYPE_WLAN;
1134 			name = "hp-wifi";
1135 			break;
1136 		case HPWMI_BLUETOOTH:
1137 			type = RFKILL_TYPE_BLUETOOTH;
1138 			name = "hp-bluetooth";
1139 			break;
1140 		case HPWMI_WWAN:
1141 			type = RFKILL_TYPE_WWAN;
1142 			name = "hp-wwan";
1143 			break;
1144 		case HPWMI_GPS:
1145 			type = RFKILL_TYPE_GPS;
1146 			name = "hp-gps";
1147 			break;
1148 		default:
1149 			pr_warn("unknown device type 0x%x\n",
1150 				state.device[i].radio_type);
1151 			continue;
1152 		}
1153 
1154 		if (!state.device[i].vendor_id) {
1155 			pr_warn("zero device %d while %d reported\n",
1156 				i, state.count);
1157 			continue;
1158 		}
1159 
1160 		rfkill = rfkill_alloc(name, &device->dev, type,
1161 				      &hp_wmi_rfkill2_ops, (void *)(long)i);
1162 		if (!rfkill) {
1163 			err = -ENOMEM;
1164 			goto fail;
1165 		}
1166 
1167 		rfkill2[rfkill2_count].id = state.device[i].rfkill_id;
1168 		rfkill2[rfkill2_count].num = i;
1169 		rfkill2[rfkill2_count].rfkill = rfkill;
1170 
1171 		rfkill_init_sw_state(rfkill,
1172 				     IS_SWBLOCKED(state.device[i].power));
1173 		rfkill_set_hw_state(rfkill,
1174 				    IS_HWBLOCKED(state.device[i].power));
1175 
1176 		if (!(state.device[i].power & HPWMI_POWER_BIOS))
1177 			pr_info("device %s blocked by BIOS\n", name);
1178 
1179 		err = rfkill_register(rfkill);
1180 		if (err) {
1181 			rfkill_destroy(rfkill);
1182 			goto fail;
1183 		}
1184 
1185 		rfkill2_count++;
1186 	}
1187 
1188 	return 0;
1189 fail:
1190 	for (; rfkill2_count > 0; rfkill2_count--) {
1191 		rfkill_unregister(rfkill2[rfkill2_count - 1].rfkill);
1192 		rfkill_destroy(rfkill2[rfkill2_count - 1].rfkill);
1193 	}
1194 	return err;
1195 }
1196 
1197 static int platform_profile_omen_get(struct platform_profile_handler *pprof,
1198 				     enum platform_profile_option *profile)
1199 {
1200 	int tp;
1201 
1202 	tp = omen_thermal_profile_get();
1203 	if (tp < 0)
1204 		return tp;
1205 
1206 	switch (tp) {
1207 	case HP_OMEN_V0_THERMAL_PROFILE_PERFORMANCE:
1208 	case HP_OMEN_V1_THERMAL_PROFILE_PERFORMANCE:
1209 		*profile = PLATFORM_PROFILE_PERFORMANCE;
1210 		break;
1211 	case HP_OMEN_V0_THERMAL_PROFILE_DEFAULT:
1212 	case HP_OMEN_V1_THERMAL_PROFILE_DEFAULT:
1213 		*profile = PLATFORM_PROFILE_BALANCED;
1214 		break;
1215 	case HP_OMEN_V0_THERMAL_PROFILE_COOL:
1216 	case HP_OMEN_V1_THERMAL_PROFILE_COOL:
1217 		*profile = PLATFORM_PROFILE_COOL;
1218 		break;
1219 	default:
1220 		return -EINVAL;
1221 	}
1222 
1223 	return 0;
1224 }
1225 
1226 static bool has_omen_thermal_profile_ec_timer(void)
1227 {
1228 	const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
1229 
1230 	if (!board_name)
1231 		return false;
1232 
1233 	return match_string(omen_timed_thermal_profile_boards,
1234 			    ARRAY_SIZE(omen_timed_thermal_profile_boards),
1235 			    board_name) >= 0;
1236 }
1237 
1238 inline int omen_thermal_profile_ec_flags_set(enum hp_thermal_profile_omen_flags flags)
1239 {
1240 	return ec_write(HP_OMEN_EC_THERMAL_PROFILE_FLAGS_OFFSET, flags);
1241 }
1242 
1243 inline int omen_thermal_profile_ec_timer_set(u8 value)
1244 {
1245 	return ec_write(HP_OMEN_EC_THERMAL_PROFILE_TIMER_OFFSET, value);
1246 }
1247 
1248 static int platform_profile_omen_set(struct platform_profile_handler *pprof,
1249 				     enum platform_profile_option profile)
1250 {
1251 	int err, tp, tp_version;
1252 	enum hp_thermal_profile_omen_flags flags = 0;
1253 
1254 	tp_version = omen_get_thermal_policy_version();
1255 
1256 	if (tp_version < 0 || tp_version > 1)
1257 		return -EOPNOTSUPP;
1258 
1259 	switch (profile) {
1260 	case PLATFORM_PROFILE_PERFORMANCE:
1261 		if (tp_version == 0)
1262 			tp = HP_OMEN_V0_THERMAL_PROFILE_PERFORMANCE;
1263 		else
1264 			tp = HP_OMEN_V1_THERMAL_PROFILE_PERFORMANCE;
1265 		break;
1266 	case PLATFORM_PROFILE_BALANCED:
1267 		if (tp_version == 0)
1268 			tp = HP_OMEN_V0_THERMAL_PROFILE_DEFAULT;
1269 		else
1270 			tp = HP_OMEN_V1_THERMAL_PROFILE_DEFAULT;
1271 		break;
1272 	case PLATFORM_PROFILE_COOL:
1273 		if (tp_version == 0)
1274 			tp = HP_OMEN_V0_THERMAL_PROFILE_COOL;
1275 		else
1276 			tp = HP_OMEN_V1_THERMAL_PROFILE_COOL;
1277 		break;
1278 	default:
1279 		return -EOPNOTSUPP;
1280 	}
1281 
1282 	err = omen_thermal_profile_set(tp);
1283 	if (err < 0)
1284 		return err;
1285 
1286 	if (has_omen_thermal_profile_ec_timer()) {
1287 		err = omen_thermal_profile_ec_timer_set(0);
1288 		if (err < 0)
1289 			return err;
1290 
1291 		if (profile == PLATFORM_PROFILE_PERFORMANCE)
1292 			flags = HP_OMEN_EC_FLAGS_NOTIMER |
1293 				HP_OMEN_EC_FLAGS_TURBO;
1294 
1295 		err = omen_thermal_profile_ec_flags_set(flags);
1296 		if (err < 0)
1297 			return err;
1298 	}
1299 
1300 	return 0;
1301 }
1302 
1303 static int thermal_profile_get(void)
1304 {
1305 	return hp_wmi_read_int(HPWMI_THERMAL_PROFILE_QUERY);
1306 }
1307 
1308 static int thermal_profile_set(int thermal_profile)
1309 {
1310 	return hp_wmi_perform_query(HPWMI_THERMAL_PROFILE_QUERY, HPWMI_WRITE, &thermal_profile,
1311 							   sizeof(thermal_profile), 0);
1312 }
1313 
1314 static int hp_wmi_platform_profile_get(struct platform_profile_handler *pprof,
1315 					enum platform_profile_option *profile)
1316 {
1317 	int tp;
1318 
1319 	tp = thermal_profile_get();
1320 	if (tp < 0)
1321 		return tp;
1322 
1323 	switch (tp) {
1324 	case HP_THERMAL_PROFILE_PERFORMANCE:
1325 		*profile =  PLATFORM_PROFILE_PERFORMANCE;
1326 		break;
1327 	case HP_THERMAL_PROFILE_DEFAULT:
1328 		*profile =  PLATFORM_PROFILE_BALANCED;
1329 		break;
1330 	case HP_THERMAL_PROFILE_COOL:
1331 		*profile =  PLATFORM_PROFILE_COOL;
1332 		break;
1333 	case HP_THERMAL_PROFILE_QUIET:
1334 		*profile = PLATFORM_PROFILE_QUIET;
1335 		break;
1336 	default:
1337 		return -EINVAL;
1338 	}
1339 
1340 	return 0;
1341 }
1342 
1343 static int hp_wmi_platform_profile_set(struct platform_profile_handler *pprof,
1344 					enum platform_profile_option profile)
1345 {
1346 	int err, tp;
1347 
1348 	switch (profile) {
1349 	case PLATFORM_PROFILE_PERFORMANCE:
1350 		tp =  HP_THERMAL_PROFILE_PERFORMANCE;
1351 		break;
1352 	case PLATFORM_PROFILE_BALANCED:
1353 		tp =  HP_THERMAL_PROFILE_DEFAULT;
1354 		break;
1355 	case PLATFORM_PROFILE_COOL:
1356 		tp =  HP_THERMAL_PROFILE_COOL;
1357 		break;
1358 	case PLATFORM_PROFILE_QUIET:
1359 		tp = HP_THERMAL_PROFILE_QUIET;
1360 		break;
1361 	default:
1362 		return -EOPNOTSUPP;
1363 	}
1364 
1365 	err = thermal_profile_set(tp);
1366 	if (err)
1367 		return err;
1368 
1369 	return 0;
1370 }
1371 
1372 static bool is_victus_thermal_profile(void)
1373 {
1374 	const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
1375 
1376 	if (!board_name)
1377 		return false;
1378 
1379 	return match_string(victus_thermal_profile_boards,
1380 			    ARRAY_SIZE(victus_thermal_profile_boards),
1381 			    board_name) >= 0;
1382 }
1383 
1384 static int platform_profile_victus_get(struct platform_profile_handler *pprof,
1385 				     enum platform_profile_option *profile)
1386 {
1387 	int tp;
1388 
1389 	tp = omen_thermal_profile_get();
1390 	if (tp < 0)
1391 		return tp;
1392 
1393 	switch (tp) {
1394 	case HP_VICTUS_THERMAL_PROFILE_PERFORMANCE:
1395 		*profile = PLATFORM_PROFILE_PERFORMANCE;
1396 		break;
1397 	case HP_VICTUS_THERMAL_PROFILE_DEFAULT:
1398 		*profile = PLATFORM_PROFILE_BALANCED;
1399 		break;
1400 	case HP_VICTUS_THERMAL_PROFILE_QUIET:
1401 		*profile = PLATFORM_PROFILE_QUIET;
1402 		break;
1403 	default:
1404 		return -EOPNOTSUPP;
1405 	}
1406 
1407 	return 0;
1408 }
1409 
1410 static int platform_profile_victus_set(struct platform_profile_handler *pprof,
1411 				     enum platform_profile_option profile)
1412 {
1413 	int err, tp;
1414 
1415 	switch (profile) {
1416 	case PLATFORM_PROFILE_PERFORMANCE:
1417 		tp = HP_VICTUS_THERMAL_PROFILE_PERFORMANCE;
1418 		break;
1419 	case PLATFORM_PROFILE_BALANCED:
1420 		tp = HP_VICTUS_THERMAL_PROFILE_DEFAULT;
1421 		break;
1422 	case PLATFORM_PROFILE_QUIET:
1423 		tp = HP_VICTUS_THERMAL_PROFILE_QUIET;
1424 		break;
1425 	default:
1426 		return -EOPNOTSUPP;
1427 	}
1428 
1429 	err = omen_thermal_profile_set(tp);
1430 	if (err < 0)
1431 		return err;
1432 
1433 	return 0;
1434 }
1435 
1436 static int thermal_profile_setup(void)
1437 {
1438 	int err, tp;
1439 
1440 	if (is_omen_thermal_profile()) {
1441 		tp = omen_thermal_profile_get();
1442 		if (tp < 0)
1443 			return tp;
1444 
1445 		/*
1446 		 * call thermal profile write command to ensure that the
1447 		 * firmware correctly sets the OEM variables
1448 		 */
1449 
1450 		err = omen_thermal_profile_set(tp);
1451 		if (err < 0)
1452 			return err;
1453 
1454 		platform_profile_handler.profile_get = platform_profile_omen_get;
1455 		platform_profile_handler.profile_set = platform_profile_omen_set;
1456 
1457 		set_bit(PLATFORM_PROFILE_COOL, platform_profile_handler.choices);
1458 	} else if (is_victus_thermal_profile()) {
1459 		tp = omen_thermal_profile_get();
1460 		if (tp < 0)
1461 			return tp;
1462 
1463 		/*
1464 		 * call thermal profile write command to ensure that the
1465 		 * firmware correctly sets the OEM variables
1466 		 */
1467 		err = omen_thermal_profile_set(tp);
1468 		if (err < 0)
1469 			return err;
1470 
1471 		platform_profile_handler.profile_get = platform_profile_victus_get;
1472 		platform_profile_handler.profile_set = platform_profile_victus_set;
1473 
1474 		set_bit(PLATFORM_PROFILE_QUIET, platform_profile_handler.choices);
1475 	} else {
1476 		tp = thermal_profile_get();
1477 
1478 		if (tp < 0)
1479 			return tp;
1480 
1481 		/*
1482 		 * call thermal profile write command to ensure that the
1483 		 * firmware correctly sets the OEM variables for the DPTF
1484 		 */
1485 		err = thermal_profile_set(tp);
1486 		if (err)
1487 			return err;
1488 
1489 		platform_profile_handler.profile_get = hp_wmi_platform_profile_get;
1490 		platform_profile_handler.profile_set = hp_wmi_platform_profile_set;
1491 
1492 		set_bit(PLATFORM_PROFILE_QUIET, platform_profile_handler.choices);
1493 		set_bit(PLATFORM_PROFILE_COOL, platform_profile_handler.choices);
1494 	}
1495 
1496 	set_bit(PLATFORM_PROFILE_BALANCED, platform_profile_handler.choices);
1497 	set_bit(PLATFORM_PROFILE_PERFORMANCE, platform_profile_handler.choices);
1498 
1499 	err = platform_profile_register(&platform_profile_handler);
1500 	if (err)
1501 		return err;
1502 
1503 	platform_profile_support = true;
1504 
1505 	return 0;
1506 }
1507 
1508 static int hp_wmi_hwmon_init(void);
1509 
1510 static int __init hp_wmi_bios_setup(struct platform_device *device)
1511 {
1512 	int err;
1513 	/* clear detected rfkill devices */
1514 	wifi_rfkill = NULL;
1515 	bluetooth_rfkill = NULL;
1516 	wwan_rfkill = NULL;
1517 	rfkill2_count = 0;
1518 
1519 	/*
1520 	 * In pre-2009 BIOS, command 1Bh return 0x4 to indicate that
1521 	 * BIOS no longer controls the power for the wireless
1522 	 * devices. All features supported by this command will no
1523 	 * longer be supported.
1524 	 */
1525 	if (!hp_wmi_bios_2009_later()) {
1526 		if (hp_wmi_rfkill_setup(device))
1527 			hp_wmi_rfkill2_setup(device);
1528 	}
1529 
1530 	err = hp_wmi_hwmon_init();
1531 
1532 	if (err < 0)
1533 		return err;
1534 
1535 	thermal_profile_setup();
1536 
1537 	return 0;
1538 }
1539 
1540 static void __exit hp_wmi_bios_remove(struct platform_device *device)
1541 {
1542 	int i;
1543 
1544 	for (i = 0; i < rfkill2_count; i++) {
1545 		rfkill_unregister(rfkill2[i].rfkill);
1546 		rfkill_destroy(rfkill2[i].rfkill);
1547 	}
1548 
1549 	if (wifi_rfkill) {
1550 		rfkill_unregister(wifi_rfkill);
1551 		rfkill_destroy(wifi_rfkill);
1552 	}
1553 	if (bluetooth_rfkill) {
1554 		rfkill_unregister(bluetooth_rfkill);
1555 		rfkill_destroy(bluetooth_rfkill);
1556 	}
1557 	if (wwan_rfkill) {
1558 		rfkill_unregister(wwan_rfkill);
1559 		rfkill_destroy(wwan_rfkill);
1560 	}
1561 
1562 	if (platform_profile_support)
1563 		platform_profile_remove();
1564 }
1565 
1566 static int hp_wmi_resume_handler(struct device *device)
1567 {
1568 	/*
1569 	 * Hardware state may have changed while suspended, so trigger
1570 	 * input events for the current state. As this is a switch,
1571 	 * the input layer will only actually pass it on if the state
1572 	 * changed.
1573 	 */
1574 	if (hp_wmi_input_dev) {
1575 		if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
1576 			input_report_switch(hp_wmi_input_dev, SW_DOCK,
1577 					    hp_wmi_get_dock_state());
1578 		if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
1579 			input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
1580 					    hp_wmi_get_tablet_mode());
1581 		input_sync(hp_wmi_input_dev);
1582 	}
1583 
1584 	if (rfkill2_count)
1585 		hp_wmi_rfkill2_refresh();
1586 
1587 	if (wifi_rfkill)
1588 		rfkill_set_states(wifi_rfkill,
1589 				  hp_wmi_get_sw_state(HPWMI_WIFI),
1590 				  hp_wmi_get_hw_state(HPWMI_WIFI));
1591 	if (bluetooth_rfkill)
1592 		rfkill_set_states(bluetooth_rfkill,
1593 				  hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
1594 				  hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
1595 	if (wwan_rfkill)
1596 		rfkill_set_states(wwan_rfkill,
1597 				  hp_wmi_get_sw_state(HPWMI_WWAN),
1598 				  hp_wmi_get_hw_state(HPWMI_WWAN));
1599 
1600 	return 0;
1601 }
1602 
1603 static const struct dev_pm_ops hp_wmi_pm_ops = {
1604 	.resume  = hp_wmi_resume_handler,
1605 	.restore  = hp_wmi_resume_handler,
1606 };
1607 
1608 /*
1609  * hp_wmi_bios_remove() lives in .exit.text. For drivers registered via
1610  * module_platform_driver_probe() this is ok because they cannot get unbound at
1611  * runtime. So mark the driver struct with __refdata to prevent modpost
1612  * triggering a section mismatch warning.
1613  */
1614 static struct platform_driver hp_wmi_driver __refdata = {
1615 	.driver = {
1616 		.name = "hp-wmi",
1617 		.pm = &hp_wmi_pm_ops,
1618 		.dev_groups = hp_wmi_groups,
1619 	},
1620 	.remove_new = __exit_p(hp_wmi_bios_remove),
1621 };
1622 
1623 static umode_t hp_wmi_hwmon_is_visible(const void *data,
1624 				       enum hwmon_sensor_types type,
1625 				       u32 attr, int channel)
1626 {
1627 	switch (type) {
1628 	case hwmon_pwm:
1629 		return 0644;
1630 	case hwmon_fan:
1631 		if (hp_wmi_get_fan_speed(channel) >= 0)
1632 			return 0444;
1633 		break;
1634 	default:
1635 		return 0;
1636 	}
1637 
1638 	return 0;
1639 }
1640 
1641 static int hp_wmi_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
1642 			     u32 attr, int channel, long *val)
1643 {
1644 	int ret;
1645 
1646 	switch (type) {
1647 	case hwmon_fan:
1648 		ret = hp_wmi_get_fan_speed(channel);
1649 
1650 		if (ret < 0)
1651 			return ret;
1652 		*val = ret;
1653 		return 0;
1654 	case hwmon_pwm:
1655 		switch (hp_wmi_fan_speed_max_get()) {
1656 		case 0:
1657 			/* 0 is automatic fan, which is 2 for hwmon */
1658 			*val = 2;
1659 			return 0;
1660 		case 1:
1661 			/* 1 is max fan, which is 0
1662 			 * (no fan speed control) for hwmon
1663 			 */
1664 			*val = 0;
1665 			return 0;
1666 		default:
1667 			/* shouldn't happen */
1668 			return -ENODATA;
1669 		}
1670 	default:
1671 		return -EINVAL;
1672 	}
1673 }
1674 
1675 static int hp_wmi_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
1676 			      u32 attr, int channel, long val)
1677 {
1678 	switch (type) {
1679 	case hwmon_pwm:
1680 		switch (val) {
1681 		case 0:
1682 			/* 0 is no fan speed control (max), which is 1 for us */
1683 			return hp_wmi_fan_speed_max_set(1);
1684 		case 2:
1685 			/* 2 is automatic speed control, which is 0 for us */
1686 			return hp_wmi_fan_speed_max_set(0);
1687 		default:
1688 			/* we don't support manual fan speed control */
1689 			return -EINVAL;
1690 		}
1691 	default:
1692 		return -EOPNOTSUPP;
1693 	}
1694 }
1695 
1696 static const struct hwmon_channel_info * const info[] = {
1697 	HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT, HWMON_F_INPUT),
1698 	HWMON_CHANNEL_INFO(pwm, HWMON_PWM_ENABLE),
1699 	NULL
1700 };
1701 
1702 static const struct hwmon_ops ops = {
1703 	.is_visible = hp_wmi_hwmon_is_visible,
1704 	.read = hp_wmi_hwmon_read,
1705 	.write = hp_wmi_hwmon_write,
1706 };
1707 
1708 static const struct hwmon_chip_info chip_info = {
1709 	.ops = &ops,
1710 	.info = info,
1711 };
1712 
1713 static int hp_wmi_hwmon_init(void)
1714 {
1715 	struct device *dev = &hp_wmi_platform_dev->dev;
1716 	struct device *hwmon;
1717 
1718 	hwmon = devm_hwmon_device_register_with_info(dev, "hp", &hp_wmi_driver,
1719 						     &chip_info, NULL);
1720 
1721 	if (IS_ERR(hwmon)) {
1722 		dev_err(dev, "Could not register hp hwmon device\n");
1723 		return PTR_ERR(hwmon);
1724 	}
1725 
1726 	return 0;
1727 }
1728 
1729 static int __init hp_wmi_init(void)
1730 {
1731 	int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
1732 	int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
1733 	int err, tmp = 0;
1734 
1735 	if (!bios_capable && !event_capable)
1736 		return -ENODEV;
1737 
1738 	if (hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, HPWMI_READ, &tmp,
1739 				 sizeof(tmp), sizeof(tmp)) == HPWMI_RET_INVALID_PARAMETERS)
1740 		zero_insize_support = true;
1741 
1742 	if (event_capable) {
1743 		err = hp_wmi_input_setup();
1744 		if (err)
1745 			return err;
1746 	}
1747 
1748 	if (bios_capable) {
1749 		hp_wmi_platform_dev =
1750 			platform_device_register_simple("hp-wmi", PLATFORM_DEVID_NONE, NULL, 0);
1751 		if (IS_ERR(hp_wmi_platform_dev)) {
1752 			err = PTR_ERR(hp_wmi_platform_dev);
1753 			goto err_destroy_input;
1754 		}
1755 
1756 		err = platform_driver_probe(&hp_wmi_driver, hp_wmi_bios_setup);
1757 		if (err)
1758 			goto err_unregister_device;
1759 	}
1760 
1761 	return 0;
1762 
1763 err_unregister_device:
1764 	platform_device_unregister(hp_wmi_platform_dev);
1765 err_destroy_input:
1766 	if (event_capable)
1767 		hp_wmi_input_destroy();
1768 
1769 	return err;
1770 }
1771 module_init(hp_wmi_init);
1772 
1773 static void __exit hp_wmi_exit(void)
1774 {
1775 	if (wmi_has_guid(HPWMI_EVENT_GUID))
1776 		hp_wmi_input_destroy();
1777 
1778 	if (camera_shutter_input_dev)
1779 		input_unregister_device(camera_shutter_input_dev);
1780 
1781 	if (hp_wmi_platform_dev) {
1782 		platform_device_unregister(hp_wmi_platform_dev);
1783 		platform_driver_unregister(&hp_wmi_driver);
1784 	}
1785 }
1786 module_exit(hp_wmi_exit);
1787