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