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