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/acpi.h>
26 #include <linux/rfkill.h>
27 #include <linux/string.h>
28
29 MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>");
30 MODULE_DESCRIPTION("HP laptop WMI hotkeys driver");
31 MODULE_LICENSE("GPL");
32
33 MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
34 MODULE_ALIAS("wmi:5FB7F034-2C63-45e9-BE91-3D44E2C707E4");
35
36 static int enable_tablet_mode_sw = -1;
37 module_param(enable_tablet_mode_sw, int, 0444);
38 MODULE_PARM_DESC(enable_tablet_mode_sw, "Enable SW_TABLET_MODE reporting (-1=auto, 0=no, 1=yes)");
39
40 #define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
41 #define HPWMI_BIOS_GUID "5FB7F034-2C63-45e9-BE91-3D44E2C707E4"
42
43 enum hp_wmi_radio {
44 HPWMI_WIFI = 0x0,
45 HPWMI_BLUETOOTH = 0x1,
46 HPWMI_WWAN = 0x2,
47 HPWMI_GPS = 0x3,
48 };
49
50 enum hp_wmi_event_ids {
51 HPWMI_DOCK_EVENT = 0x01,
52 HPWMI_PARK_HDD = 0x02,
53 HPWMI_SMART_ADAPTER = 0x03,
54 HPWMI_BEZEL_BUTTON = 0x04,
55 HPWMI_WIRELESS = 0x05,
56 HPWMI_CPU_BATTERY_THROTTLE = 0x06,
57 HPWMI_LOCK_SWITCH = 0x07,
58 HPWMI_LID_SWITCH = 0x08,
59 HPWMI_SCREEN_ROTATION = 0x09,
60 HPWMI_COOLSENSE_SYSTEM_MOBILE = 0x0A,
61 HPWMI_COOLSENSE_SYSTEM_HOT = 0x0B,
62 HPWMI_PROXIMITY_SENSOR = 0x0C,
63 HPWMI_BACKLIT_KB_BRIGHTNESS = 0x0D,
64 HPWMI_PEAKSHIFT_PERIOD = 0x0F,
65 HPWMI_BATTERY_CHARGE_PERIOD = 0x10,
66 HPWMI_SANITIZATION_MODE = 0x17,
67 HPWMI_SMART_EXPERIENCE_APP = 0x21,
68 };
69
70 struct bios_args {
71 u32 signature;
72 u32 command;
73 u32 commandtype;
74 u32 datasize;
75 u8 data[128];
76 };
77
78 enum hp_wmi_commandtype {
79 HPWMI_DISPLAY_QUERY = 0x01,
80 HPWMI_HDDTEMP_QUERY = 0x02,
81 HPWMI_ALS_QUERY = 0x03,
82 HPWMI_HARDWARE_QUERY = 0x04,
83 HPWMI_WIRELESS_QUERY = 0x05,
84 HPWMI_BATTERY_QUERY = 0x07,
85 HPWMI_BIOS_QUERY = 0x09,
86 HPWMI_FEATURE_QUERY = 0x0b,
87 HPWMI_HOTKEY_QUERY = 0x0c,
88 HPWMI_FEATURE2_QUERY = 0x0d,
89 HPWMI_WIRELESS2_QUERY = 0x1b,
90 HPWMI_POSTCODEERROR_QUERY = 0x2a,
91 HPWMI_THERMAL_PROFILE_QUERY = 0x4c,
92 };
93
94 enum hp_wmi_command {
95 HPWMI_READ = 0x01,
96 HPWMI_WRITE = 0x02,
97 HPWMI_ODM = 0x03,
98 };
99
100 enum hp_wmi_hardware_mask {
101 HPWMI_DOCK_MASK = 0x01,
102 HPWMI_TABLET_MASK = 0x04,
103 };
104
105 struct bios_return {
106 u32 sigpass;
107 u32 return_code;
108 };
109
110 enum hp_return_value {
111 HPWMI_RET_WRONG_SIGNATURE = 0x02,
112 HPWMI_RET_UNKNOWN_COMMAND = 0x03,
113 HPWMI_RET_UNKNOWN_CMDTYPE = 0x04,
114 HPWMI_RET_INVALID_PARAMETERS = 0x05,
115 };
116
117 enum hp_wireless2_bits {
118 HPWMI_POWER_STATE = 0x01,
119 HPWMI_POWER_SOFT = 0x02,
120 HPWMI_POWER_BIOS = 0x04,
121 HPWMI_POWER_HARD = 0x08,
122 HPWMI_POWER_FW_OR_HW = HPWMI_POWER_BIOS | HPWMI_POWER_HARD,
123 };
124
125 enum hp_thermal_profile {
126 HP_THERMAL_PROFILE_PERFORMANCE = 0x00,
127 HP_THERMAL_PROFILE_DEFAULT = 0x01,
128 HP_THERMAL_PROFILE_COOL = 0x02
129 };
130
131 #define IS_HWBLOCKED(x) ((x & HPWMI_POWER_FW_OR_HW) != HPWMI_POWER_FW_OR_HW)
132 #define IS_SWBLOCKED(x) !(x & HPWMI_POWER_SOFT)
133
134 struct bios_rfkill2_device_state {
135 u8 radio_type;
136 u8 bus_type;
137 u16 vendor_id;
138 u16 product_id;
139 u16 subsys_vendor_id;
140 u16 subsys_product_id;
141 u8 rfkill_id;
142 u8 power;
143 u8 unknown[4];
144 };
145
146 /* 7 devices fit into the 128 byte buffer */
147 #define HPWMI_MAX_RFKILL2_DEVICES 7
148
149 struct bios_rfkill2_state {
150 u8 unknown[7];
151 u8 count;
152 u8 pad[8];
153 struct bios_rfkill2_device_state device[HPWMI_MAX_RFKILL2_DEVICES];
154 };
155
156 static const struct key_entry hp_wmi_keymap[] = {
157 { KE_KEY, 0x02, { KEY_BRIGHTNESSUP } },
158 { KE_KEY, 0x03, { KEY_BRIGHTNESSDOWN } },
159 { KE_KEY, 0x270, { KEY_MICMUTE } },
160 { KE_KEY, 0x20e6, { KEY_PROG1 } },
161 { KE_KEY, 0x20e8, { KEY_MEDIA } },
162 { KE_KEY, 0x2142, { KEY_MEDIA } },
163 { KE_KEY, 0x213b, { KEY_INFO } },
164 { KE_KEY, 0x2169, { KEY_ROTATE_DISPLAY } },
165 { KE_KEY, 0x216a, { KEY_SETUP } },
166 { KE_KEY, 0x21a9, { KEY_TOUCHPAD_OFF } },
167 { KE_KEY, 0x121a9, { KEY_TOUCHPAD_ON } },
168 { KE_KEY, 0x231b, { KEY_HELP } },
169 { KE_END, 0 }
170 };
171
172 static struct input_dev *hp_wmi_input_dev;
173 static struct platform_device *hp_wmi_platform_dev;
174 static struct platform_profile_handler platform_profile_handler;
175 static bool platform_profile_support;
176
177 static struct rfkill *wifi_rfkill;
178 static struct rfkill *bluetooth_rfkill;
179 static struct rfkill *wwan_rfkill;
180
181 struct rfkill2_device {
182 u8 id;
183 int num;
184 struct rfkill *rfkill;
185 };
186
187 static int rfkill2_count;
188 static struct rfkill2_device rfkill2[HPWMI_MAX_RFKILL2_DEVICES];
189
190 /* map output size to the corresponding WMI method id */
encode_outsize_for_pvsz(int outsize)191 static inline int encode_outsize_for_pvsz(int outsize)
192 {
193 if (outsize > 4096)
194 return -EINVAL;
195 if (outsize > 1024)
196 return 5;
197 if (outsize > 128)
198 return 4;
199 if (outsize > 4)
200 return 3;
201 if (outsize > 0)
202 return 2;
203 return 1;
204 }
205
206 /*
207 * hp_wmi_perform_query
208 *
209 * query: The commandtype (enum hp_wmi_commandtype)
210 * write: The command (enum hp_wmi_command)
211 * buffer: Buffer used as input and/or output
212 * insize: Size of input buffer
213 * outsize: Size of output buffer
214 *
215 * returns zero on success
216 * an HP WMI query specific error code (which is positive)
217 * -EINVAL if the query was not successful at all
218 * -EINVAL if the output buffer size exceeds buffersize
219 *
220 * Note: The buffersize must at least be the maximum of the input and output
221 * size. E.g. Battery info query is defined to have 1 byte input
222 * and 128 byte output. The caller would do:
223 * buffer = kzalloc(128, GFP_KERNEL);
224 * ret = hp_wmi_perform_query(HPWMI_BATTERY_QUERY, HPWMI_READ, buffer, 1, 128)
225 */
hp_wmi_perform_query(int query,enum hp_wmi_command command,void * buffer,int insize,int outsize)226 static int hp_wmi_perform_query(int query, enum hp_wmi_command command,
227 void *buffer, int insize, int outsize)
228 {
229 int mid;
230 struct bios_return *bios_return;
231 int actual_outsize;
232 union acpi_object *obj;
233 struct bios_args args = {
234 .signature = 0x55434553,
235 .command = command,
236 .commandtype = query,
237 .datasize = insize,
238 .data = { 0 },
239 };
240 struct acpi_buffer input = { sizeof(struct bios_args), &args };
241 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
242 int ret = 0;
243
244 mid = encode_outsize_for_pvsz(outsize);
245 if (WARN_ON(mid < 0))
246 return mid;
247
248 if (WARN_ON(insize > sizeof(args.data)))
249 return -EINVAL;
250 memcpy(&args.data[0], buffer, insize);
251
252 wmi_evaluate_method(HPWMI_BIOS_GUID, 0, mid, &input, &output);
253
254 obj = output.pointer;
255
256 if (!obj)
257 return -EINVAL;
258
259 if (obj->type != ACPI_TYPE_BUFFER) {
260 ret = -EINVAL;
261 goto out_free;
262 }
263
264 bios_return = (struct bios_return *)obj->buffer.pointer;
265 ret = bios_return->return_code;
266
267 if (ret) {
268 if (ret != HPWMI_RET_UNKNOWN_COMMAND &&
269 ret != HPWMI_RET_UNKNOWN_CMDTYPE)
270 pr_warn("query 0x%x returned error 0x%x\n", query, ret);
271 goto out_free;
272 }
273
274 /* Ignore output data of zero size */
275 if (!outsize)
276 goto out_free;
277
278 actual_outsize = min(outsize, (int)(obj->buffer.length - sizeof(*bios_return)));
279 memcpy(buffer, obj->buffer.pointer + sizeof(*bios_return), actual_outsize);
280 memset(buffer + actual_outsize, 0, outsize - actual_outsize);
281
282 out_free:
283 kfree(obj);
284 return ret;
285 }
286
hp_wmi_read_int(int query)287 static int hp_wmi_read_int(int query)
288 {
289 int val = 0, ret;
290
291 ret = hp_wmi_perform_query(query, HPWMI_READ, &val,
292 sizeof(val), sizeof(val));
293
294 if (ret)
295 return ret < 0 ? ret : -EINVAL;
296
297 return val;
298 }
299
hp_wmi_hw_state(int mask)300 static int hp_wmi_hw_state(int mask)
301 {
302 int state = hp_wmi_read_int(HPWMI_HARDWARE_QUERY);
303
304 if (state < 0)
305 return state;
306
307 return !!(state & mask);
308 }
309
hp_wmi_bios_2008_later(void)310 static int __init hp_wmi_bios_2008_later(void)
311 {
312 int state = 0;
313 int ret = hp_wmi_perform_query(HPWMI_FEATURE_QUERY, HPWMI_READ, &state,
314 sizeof(state), sizeof(state));
315 if (!ret)
316 return 1;
317
318 return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
319 }
320
hp_wmi_bios_2009_later(void)321 static int __init hp_wmi_bios_2009_later(void)
322 {
323 u8 state[128];
324 int ret = hp_wmi_perform_query(HPWMI_FEATURE2_QUERY, HPWMI_READ, &state,
325 sizeof(state), sizeof(state));
326 if (!ret)
327 return 1;
328
329 return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
330 }
331
hp_wmi_enable_hotkeys(void)332 static int __init hp_wmi_enable_hotkeys(void)
333 {
334 int value = 0x6e;
335 int ret = hp_wmi_perform_query(HPWMI_BIOS_QUERY, HPWMI_WRITE, &value,
336 sizeof(value), 0);
337
338 return ret <= 0 ? ret : -EINVAL;
339 }
340
hp_wmi_set_block(void * data,bool blocked)341 static int hp_wmi_set_block(void *data, bool blocked)
342 {
343 enum hp_wmi_radio r = (enum hp_wmi_radio) data;
344 int query = BIT(r + 8) | ((!blocked) << r);
345 int ret;
346
347 ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE,
348 &query, sizeof(query), 0);
349
350 return ret <= 0 ? ret : -EINVAL;
351 }
352
353 static const struct rfkill_ops hp_wmi_rfkill_ops = {
354 .set_block = hp_wmi_set_block,
355 };
356
hp_wmi_get_sw_state(enum hp_wmi_radio r)357 static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
358 {
359 int mask = 0x200 << (r * 8);
360
361 int wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
362
363 /* TBD: Pass error */
364 WARN_ONCE(wireless < 0, "error executing HPWMI_WIRELESS_QUERY");
365
366 return !(wireless & mask);
367 }
368
hp_wmi_get_hw_state(enum hp_wmi_radio r)369 static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
370 {
371 int mask = 0x800 << (r * 8);
372
373 int wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
374
375 /* TBD: Pass error */
376 WARN_ONCE(wireless < 0, "error executing HPWMI_WIRELESS_QUERY");
377
378 return !(wireless & mask);
379 }
380
hp_wmi_rfkill2_set_block(void * data,bool blocked)381 static int hp_wmi_rfkill2_set_block(void *data, bool blocked)
382 {
383 int rfkill_id = (int)(long)data;
384 char buffer[4] = { 0x01, 0x00, rfkill_id, !blocked };
385 int ret;
386
387 ret = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_WRITE,
388 buffer, sizeof(buffer), 0);
389
390 return ret <= 0 ? ret : -EINVAL;
391 }
392
393 static const struct rfkill_ops hp_wmi_rfkill2_ops = {
394 .set_block = hp_wmi_rfkill2_set_block,
395 };
396
hp_wmi_rfkill2_refresh(void)397 static int hp_wmi_rfkill2_refresh(void)
398 {
399 struct bios_rfkill2_state state;
400 int err, i;
401
402 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
403 sizeof(state), sizeof(state));
404 if (err)
405 return err;
406
407 for (i = 0; i < rfkill2_count; i++) {
408 int num = rfkill2[i].num;
409 struct bios_rfkill2_device_state *devstate;
410 devstate = &state.device[num];
411
412 if (num >= state.count ||
413 devstate->rfkill_id != rfkill2[i].id) {
414 pr_warn("power configuration of the wireless devices unexpectedly changed\n");
415 continue;
416 }
417
418 rfkill_set_states(rfkill2[i].rfkill,
419 IS_SWBLOCKED(devstate->power),
420 IS_HWBLOCKED(devstate->power));
421 }
422
423 return 0;
424 }
425
display_show(struct device * dev,struct device_attribute * attr,char * buf)426 static ssize_t display_show(struct device *dev, struct device_attribute *attr,
427 char *buf)
428 {
429 int value = hp_wmi_read_int(HPWMI_DISPLAY_QUERY);
430 if (value < 0)
431 return value;
432 return sprintf(buf, "%d\n", value);
433 }
434
hddtemp_show(struct device * dev,struct device_attribute * attr,char * buf)435 static ssize_t hddtemp_show(struct device *dev, struct device_attribute *attr,
436 char *buf)
437 {
438 int value = hp_wmi_read_int(HPWMI_HDDTEMP_QUERY);
439 if (value < 0)
440 return value;
441 return sprintf(buf, "%d\n", value);
442 }
443
als_show(struct device * dev,struct device_attribute * attr,char * buf)444 static ssize_t als_show(struct device *dev, struct device_attribute *attr,
445 char *buf)
446 {
447 int value = hp_wmi_read_int(HPWMI_ALS_QUERY);
448 if (value < 0)
449 return value;
450 return sprintf(buf, "%d\n", value);
451 }
452
dock_show(struct device * dev,struct device_attribute * attr,char * buf)453 static ssize_t dock_show(struct device *dev, struct device_attribute *attr,
454 char *buf)
455 {
456 int value = hp_wmi_hw_state(HPWMI_DOCK_MASK);
457 if (value < 0)
458 return value;
459 return sprintf(buf, "%d\n", value);
460 }
461
tablet_show(struct device * dev,struct device_attribute * attr,char * buf)462 static ssize_t tablet_show(struct device *dev, struct device_attribute *attr,
463 char *buf)
464 {
465 int value = hp_wmi_hw_state(HPWMI_TABLET_MASK);
466 if (value < 0)
467 return value;
468 return sprintf(buf, "%d\n", value);
469 }
470
postcode_show(struct device * dev,struct device_attribute * attr,char * buf)471 static ssize_t postcode_show(struct device *dev, struct device_attribute *attr,
472 char *buf)
473 {
474 /* Get the POST error code of previous boot failure. */
475 int value = hp_wmi_read_int(HPWMI_POSTCODEERROR_QUERY);
476 if (value < 0)
477 return value;
478 return sprintf(buf, "0x%x\n", value);
479 }
480
als_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)481 static ssize_t als_store(struct device *dev, struct device_attribute *attr,
482 const char *buf, size_t count)
483 {
484 u32 tmp;
485 int ret;
486
487 ret = kstrtou32(buf, 10, &tmp);
488 if (ret)
489 return ret;
490
491 ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, HPWMI_WRITE, &tmp,
492 sizeof(tmp), sizeof(tmp));
493 if (ret)
494 return ret < 0 ? ret : -EINVAL;
495
496 return count;
497 }
498
postcode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)499 static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,
500 const char *buf, size_t count)
501 {
502 u32 tmp = 1;
503 bool clear;
504 int ret;
505
506 ret = kstrtobool(buf, &clear);
507 if (ret)
508 return ret;
509
510 if (clear == false)
511 return -EINVAL;
512
513 /* Clear the POST error code. It is kept until until cleared. */
514 ret = hp_wmi_perform_query(HPWMI_POSTCODEERROR_QUERY, HPWMI_WRITE, &tmp,
515 sizeof(tmp), sizeof(tmp));
516 if (ret)
517 return ret < 0 ? ret : -EINVAL;
518
519 return count;
520 }
521
522 static DEVICE_ATTR_RO(display);
523 static DEVICE_ATTR_RO(hddtemp);
524 static DEVICE_ATTR_RW(als);
525 static DEVICE_ATTR_RO(dock);
526 static DEVICE_ATTR_RO(tablet);
527 static DEVICE_ATTR_RW(postcode);
528
529 static struct attribute *hp_wmi_attrs[] = {
530 &dev_attr_display.attr,
531 &dev_attr_hddtemp.attr,
532 &dev_attr_als.attr,
533 &dev_attr_dock.attr,
534 &dev_attr_tablet.attr,
535 &dev_attr_postcode.attr,
536 NULL,
537 };
538 ATTRIBUTE_GROUPS(hp_wmi);
539
hp_wmi_notify(u32 value,void * context)540 static void hp_wmi_notify(u32 value, void *context)
541 {
542 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
543 u32 event_id, event_data;
544 union acpi_object *obj;
545 acpi_status status;
546 u32 *location;
547 int key_code;
548
549 status = wmi_get_event_data(value, &response);
550 if (status != AE_OK) {
551 pr_info("bad event status 0x%x\n", status);
552 return;
553 }
554
555 obj = (union acpi_object *)response.pointer;
556
557 if (!obj)
558 return;
559 if (obj->type != ACPI_TYPE_BUFFER) {
560 pr_info("Unknown response received %d\n", obj->type);
561 kfree(obj);
562 return;
563 }
564
565 /*
566 * Depending on ACPI version the concatenation of id and event data
567 * inside _WED function will result in a 8 or 16 byte buffer.
568 */
569 location = (u32 *)obj->buffer.pointer;
570 if (obj->buffer.length == 8) {
571 event_id = *location;
572 event_data = *(location + 1);
573 } else if (obj->buffer.length == 16) {
574 event_id = *location;
575 event_data = *(location + 2);
576 } else {
577 pr_info("Unknown buffer length %d\n", obj->buffer.length);
578 kfree(obj);
579 return;
580 }
581 kfree(obj);
582
583 switch (event_id) {
584 case HPWMI_DOCK_EVENT:
585 if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
586 input_report_switch(hp_wmi_input_dev, SW_DOCK,
587 hp_wmi_hw_state(HPWMI_DOCK_MASK));
588 if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
589 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
590 hp_wmi_hw_state(HPWMI_TABLET_MASK));
591 input_sync(hp_wmi_input_dev);
592 break;
593 case HPWMI_PARK_HDD:
594 break;
595 case HPWMI_SMART_ADAPTER:
596 break;
597 case HPWMI_BEZEL_BUTTON:
598 key_code = hp_wmi_read_int(HPWMI_HOTKEY_QUERY);
599 if (key_code < 0)
600 break;
601
602 if (!sparse_keymap_report_event(hp_wmi_input_dev,
603 key_code, 1, true))
604 pr_info("Unknown key code - 0x%x\n", key_code);
605 break;
606 case HPWMI_WIRELESS:
607 if (rfkill2_count) {
608 hp_wmi_rfkill2_refresh();
609 break;
610 }
611
612 if (wifi_rfkill)
613 rfkill_set_states(wifi_rfkill,
614 hp_wmi_get_sw_state(HPWMI_WIFI),
615 hp_wmi_get_hw_state(HPWMI_WIFI));
616 if (bluetooth_rfkill)
617 rfkill_set_states(bluetooth_rfkill,
618 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
619 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
620 if (wwan_rfkill)
621 rfkill_set_states(wwan_rfkill,
622 hp_wmi_get_sw_state(HPWMI_WWAN),
623 hp_wmi_get_hw_state(HPWMI_WWAN));
624 break;
625 case HPWMI_CPU_BATTERY_THROTTLE:
626 pr_info("Unimplemented CPU throttle because of 3 Cell battery event detected\n");
627 break;
628 case HPWMI_LOCK_SWITCH:
629 break;
630 case HPWMI_LID_SWITCH:
631 break;
632 case HPWMI_SCREEN_ROTATION:
633 break;
634 case HPWMI_COOLSENSE_SYSTEM_MOBILE:
635 break;
636 case HPWMI_COOLSENSE_SYSTEM_HOT:
637 break;
638 case HPWMI_PROXIMITY_SENSOR:
639 break;
640 case HPWMI_BACKLIT_KB_BRIGHTNESS:
641 break;
642 case HPWMI_PEAKSHIFT_PERIOD:
643 break;
644 case HPWMI_BATTERY_CHARGE_PERIOD:
645 break;
646 case HPWMI_SANITIZATION_MODE:
647 break;
648 case HPWMI_SMART_EXPERIENCE_APP:
649 break;
650 default:
651 pr_info("Unknown event_id - %d - 0x%x\n", event_id, event_data);
652 break;
653 }
654 }
655
hp_wmi_input_setup(void)656 static int __init hp_wmi_input_setup(void)
657 {
658 acpi_status status;
659 int err, val;
660
661 hp_wmi_input_dev = input_allocate_device();
662 if (!hp_wmi_input_dev)
663 return -ENOMEM;
664
665 hp_wmi_input_dev->name = "HP WMI hotkeys";
666 hp_wmi_input_dev->phys = "wmi/input0";
667 hp_wmi_input_dev->id.bustype = BUS_HOST;
668
669 __set_bit(EV_SW, hp_wmi_input_dev->evbit);
670
671 /* Dock */
672 val = hp_wmi_hw_state(HPWMI_DOCK_MASK);
673 if (!(val < 0)) {
674 __set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
675 input_report_switch(hp_wmi_input_dev, SW_DOCK, val);
676 }
677
678 /* Tablet mode */
679 if (enable_tablet_mode_sw > 0) {
680 val = hp_wmi_hw_state(HPWMI_TABLET_MASK);
681 if (val >= 0) {
682 __set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
683 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE, val);
684 }
685 }
686
687 err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
688 if (err)
689 goto err_free_dev;
690
691 /* Set initial hardware state */
692 input_sync(hp_wmi_input_dev);
693
694 if (!hp_wmi_bios_2009_later() && hp_wmi_bios_2008_later())
695 hp_wmi_enable_hotkeys();
696
697 status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
698 if (ACPI_FAILURE(status)) {
699 err = -EIO;
700 goto err_free_dev;
701 }
702
703 err = input_register_device(hp_wmi_input_dev);
704 if (err)
705 goto err_uninstall_notifier;
706
707 return 0;
708
709 err_uninstall_notifier:
710 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
711 err_free_dev:
712 input_free_device(hp_wmi_input_dev);
713 return err;
714 }
715
hp_wmi_input_destroy(void)716 static void hp_wmi_input_destroy(void)
717 {
718 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
719 input_unregister_device(hp_wmi_input_dev);
720 }
721
hp_wmi_rfkill_setup(struct platform_device * device)722 static int __init hp_wmi_rfkill_setup(struct platform_device *device)
723 {
724 int err, wireless;
725
726 wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
727 if (wireless < 0)
728 return wireless;
729
730 err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE, &wireless,
731 sizeof(wireless), 0);
732 if (err)
733 return err;
734
735 if (wireless & 0x1) {
736 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
737 RFKILL_TYPE_WLAN,
738 &hp_wmi_rfkill_ops,
739 (void *) HPWMI_WIFI);
740 if (!wifi_rfkill)
741 return -ENOMEM;
742 rfkill_init_sw_state(wifi_rfkill,
743 hp_wmi_get_sw_state(HPWMI_WIFI));
744 rfkill_set_hw_state(wifi_rfkill,
745 hp_wmi_get_hw_state(HPWMI_WIFI));
746 err = rfkill_register(wifi_rfkill);
747 if (err)
748 goto register_wifi_error;
749 }
750
751 if (wireless & 0x2) {
752 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
753 RFKILL_TYPE_BLUETOOTH,
754 &hp_wmi_rfkill_ops,
755 (void *) HPWMI_BLUETOOTH);
756 if (!bluetooth_rfkill) {
757 err = -ENOMEM;
758 goto register_bluetooth_error;
759 }
760 rfkill_init_sw_state(bluetooth_rfkill,
761 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
762 rfkill_set_hw_state(bluetooth_rfkill,
763 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
764 err = rfkill_register(bluetooth_rfkill);
765 if (err)
766 goto register_bluetooth_error;
767 }
768
769 if (wireless & 0x4) {
770 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
771 RFKILL_TYPE_WWAN,
772 &hp_wmi_rfkill_ops,
773 (void *) HPWMI_WWAN);
774 if (!wwan_rfkill) {
775 err = -ENOMEM;
776 goto register_wwan_error;
777 }
778 rfkill_init_sw_state(wwan_rfkill,
779 hp_wmi_get_sw_state(HPWMI_WWAN));
780 rfkill_set_hw_state(wwan_rfkill,
781 hp_wmi_get_hw_state(HPWMI_WWAN));
782 err = rfkill_register(wwan_rfkill);
783 if (err)
784 goto register_wwan_error;
785 }
786
787 return 0;
788
789 register_wwan_error:
790 rfkill_destroy(wwan_rfkill);
791 wwan_rfkill = NULL;
792 if (bluetooth_rfkill)
793 rfkill_unregister(bluetooth_rfkill);
794 register_bluetooth_error:
795 rfkill_destroy(bluetooth_rfkill);
796 bluetooth_rfkill = NULL;
797 if (wifi_rfkill)
798 rfkill_unregister(wifi_rfkill);
799 register_wifi_error:
800 rfkill_destroy(wifi_rfkill);
801 wifi_rfkill = NULL;
802 return err;
803 }
804
hp_wmi_rfkill2_setup(struct platform_device * device)805 static int __init hp_wmi_rfkill2_setup(struct platform_device *device)
806 {
807 struct bios_rfkill2_state state;
808 int err, i;
809
810 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
811 sizeof(state), sizeof(state));
812 if (err)
813 return err < 0 ? err : -EINVAL;
814
815 if (state.count > HPWMI_MAX_RFKILL2_DEVICES) {
816 pr_warn("unable to parse 0x1b query output\n");
817 return -EINVAL;
818 }
819
820 for (i = 0; i < state.count; i++) {
821 struct rfkill *rfkill;
822 enum rfkill_type type;
823 char *name;
824 switch (state.device[i].radio_type) {
825 case HPWMI_WIFI:
826 type = RFKILL_TYPE_WLAN;
827 name = "hp-wifi";
828 break;
829 case HPWMI_BLUETOOTH:
830 type = RFKILL_TYPE_BLUETOOTH;
831 name = "hp-bluetooth";
832 break;
833 case HPWMI_WWAN:
834 type = RFKILL_TYPE_WWAN;
835 name = "hp-wwan";
836 break;
837 case HPWMI_GPS:
838 type = RFKILL_TYPE_GPS;
839 name = "hp-gps";
840 break;
841 default:
842 pr_warn("unknown device type 0x%x\n",
843 state.device[i].radio_type);
844 continue;
845 }
846
847 if (!state.device[i].vendor_id) {
848 pr_warn("zero device %d while %d reported\n",
849 i, state.count);
850 continue;
851 }
852
853 rfkill = rfkill_alloc(name, &device->dev, type,
854 &hp_wmi_rfkill2_ops, (void *)(long)i);
855 if (!rfkill) {
856 err = -ENOMEM;
857 goto fail;
858 }
859
860 rfkill2[rfkill2_count].id = state.device[i].rfkill_id;
861 rfkill2[rfkill2_count].num = i;
862 rfkill2[rfkill2_count].rfkill = rfkill;
863
864 rfkill_init_sw_state(rfkill,
865 IS_SWBLOCKED(state.device[i].power));
866 rfkill_set_hw_state(rfkill,
867 IS_HWBLOCKED(state.device[i].power));
868
869 if (!(state.device[i].power & HPWMI_POWER_BIOS))
870 pr_info("device %s blocked by BIOS\n", name);
871
872 err = rfkill_register(rfkill);
873 if (err) {
874 rfkill_destroy(rfkill);
875 goto fail;
876 }
877
878 rfkill2_count++;
879 }
880
881 return 0;
882 fail:
883 for (; rfkill2_count > 0; rfkill2_count--) {
884 rfkill_unregister(rfkill2[rfkill2_count - 1].rfkill);
885 rfkill_destroy(rfkill2[rfkill2_count - 1].rfkill);
886 }
887 return err;
888 }
889
thermal_profile_get(void)890 static int thermal_profile_get(void)
891 {
892 return hp_wmi_read_int(HPWMI_THERMAL_PROFILE_QUERY);
893 }
894
thermal_profile_set(int thermal_profile)895 static int thermal_profile_set(int thermal_profile)
896 {
897 return hp_wmi_perform_query(HPWMI_THERMAL_PROFILE_QUERY, HPWMI_WRITE, &thermal_profile,
898 sizeof(thermal_profile), 0);
899 }
900
platform_profile_get(struct platform_profile_handler * pprof,enum platform_profile_option * profile)901 static int platform_profile_get(struct platform_profile_handler *pprof,
902 enum platform_profile_option *profile)
903 {
904 int tp;
905
906 tp = thermal_profile_get();
907 if (tp < 0)
908 return tp;
909
910 switch (tp) {
911 case HP_THERMAL_PROFILE_PERFORMANCE:
912 *profile = PLATFORM_PROFILE_PERFORMANCE;
913 break;
914 case HP_THERMAL_PROFILE_DEFAULT:
915 *profile = PLATFORM_PROFILE_BALANCED;
916 break;
917 case HP_THERMAL_PROFILE_COOL:
918 *profile = PLATFORM_PROFILE_COOL;
919 break;
920 default:
921 return -EINVAL;
922 }
923
924 return 0;
925 }
926
platform_profile_set(struct platform_profile_handler * pprof,enum platform_profile_option profile)927 static int platform_profile_set(struct platform_profile_handler *pprof,
928 enum platform_profile_option profile)
929 {
930 int err, tp;
931
932 switch (profile) {
933 case PLATFORM_PROFILE_PERFORMANCE:
934 tp = HP_THERMAL_PROFILE_PERFORMANCE;
935 break;
936 case PLATFORM_PROFILE_BALANCED:
937 tp = HP_THERMAL_PROFILE_DEFAULT;
938 break;
939 case PLATFORM_PROFILE_COOL:
940 tp = HP_THERMAL_PROFILE_COOL;
941 break;
942 default:
943 return -EOPNOTSUPP;
944 }
945
946 err = thermal_profile_set(tp);
947 if (err)
948 return err;
949
950 return 0;
951 }
952
thermal_profile_setup(void)953 static int thermal_profile_setup(void)
954 {
955 int err, tp;
956
957 tp = thermal_profile_get();
958 if (tp < 0)
959 return tp;
960
961 /*
962 * call thermal profile write command to ensure that the firmware correctly
963 * sets the OEM variables for the DPTF
964 */
965 err = thermal_profile_set(tp);
966 if (err)
967 return err;
968
969 platform_profile_handler.profile_get = platform_profile_get,
970 platform_profile_handler.profile_set = platform_profile_set,
971
972 set_bit(PLATFORM_PROFILE_COOL, platform_profile_handler.choices);
973 set_bit(PLATFORM_PROFILE_BALANCED, platform_profile_handler.choices);
974 set_bit(PLATFORM_PROFILE_PERFORMANCE, platform_profile_handler.choices);
975
976 err = platform_profile_register(&platform_profile_handler);
977 if (err)
978 return err;
979
980 platform_profile_support = true;
981
982 return 0;
983 }
984
hp_wmi_bios_setup(struct platform_device * device)985 static int __init hp_wmi_bios_setup(struct platform_device *device)
986 {
987 /* clear detected rfkill devices */
988 wifi_rfkill = NULL;
989 bluetooth_rfkill = NULL;
990 wwan_rfkill = NULL;
991 rfkill2_count = 0;
992
993 /*
994 * In pre-2009 BIOS, command 1Bh return 0x4 to indicate that
995 * BIOS no longer controls the power for the wireless
996 * devices. All features supported by this command will no
997 * longer be supported.
998 */
999 if (!hp_wmi_bios_2009_later()) {
1000 if (hp_wmi_rfkill_setup(device))
1001 hp_wmi_rfkill2_setup(device);
1002 }
1003
1004 thermal_profile_setup();
1005
1006 return 0;
1007 }
1008
hp_wmi_bios_remove(struct platform_device * device)1009 static int __exit hp_wmi_bios_remove(struct platform_device *device)
1010 {
1011 int i;
1012
1013 for (i = 0; i < rfkill2_count; i++) {
1014 rfkill_unregister(rfkill2[i].rfkill);
1015 rfkill_destroy(rfkill2[i].rfkill);
1016 }
1017
1018 if (wifi_rfkill) {
1019 rfkill_unregister(wifi_rfkill);
1020 rfkill_destroy(wifi_rfkill);
1021 }
1022 if (bluetooth_rfkill) {
1023 rfkill_unregister(bluetooth_rfkill);
1024 rfkill_destroy(bluetooth_rfkill);
1025 }
1026 if (wwan_rfkill) {
1027 rfkill_unregister(wwan_rfkill);
1028 rfkill_destroy(wwan_rfkill);
1029 }
1030
1031 if (platform_profile_support)
1032 platform_profile_remove();
1033
1034 return 0;
1035 }
1036
hp_wmi_resume_handler(struct device * device)1037 static int hp_wmi_resume_handler(struct device *device)
1038 {
1039 /*
1040 * Hardware state may have changed while suspended, so trigger
1041 * input events for the current state. As this is a switch,
1042 * the input layer will only actually pass it on if the state
1043 * changed.
1044 */
1045 if (hp_wmi_input_dev) {
1046 if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
1047 input_report_switch(hp_wmi_input_dev, SW_DOCK,
1048 hp_wmi_hw_state(HPWMI_DOCK_MASK));
1049 if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
1050 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
1051 hp_wmi_hw_state(HPWMI_TABLET_MASK));
1052 input_sync(hp_wmi_input_dev);
1053 }
1054
1055 if (rfkill2_count)
1056 hp_wmi_rfkill2_refresh();
1057
1058 if (wifi_rfkill)
1059 rfkill_set_states(wifi_rfkill,
1060 hp_wmi_get_sw_state(HPWMI_WIFI),
1061 hp_wmi_get_hw_state(HPWMI_WIFI));
1062 if (bluetooth_rfkill)
1063 rfkill_set_states(bluetooth_rfkill,
1064 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
1065 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
1066 if (wwan_rfkill)
1067 rfkill_set_states(wwan_rfkill,
1068 hp_wmi_get_sw_state(HPWMI_WWAN),
1069 hp_wmi_get_hw_state(HPWMI_WWAN));
1070
1071 return 0;
1072 }
1073
1074 static const struct dev_pm_ops hp_wmi_pm_ops = {
1075 .resume = hp_wmi_resume_handler,
1076 .restore = hp_wmi_resume_handler,
1077 };
1078
1079 /*
1080 * hp_wmi_bios_remove() lives in .exit.text. For drivers registered via
1081 * module_platform_driver_probe() this is ok because they cannot get unbound at
1082 * runtime. So mark the driver struct with __refdata to prevent modpost
1083 * triggering a section mismatch warning.
1084 */
1085 static struct platform_driver hp_wmi_driver __refdata = {
1086 .driver = {
1087 .name = "hp-wmi",
1088 .pm = &hp_wmi_pm_ops,
1089 .dev_groups = hp_wmi_groups,
1090 },
1091 .remove = __exit_p(hp_wmi_bios_remove),
1092 };
1093
hp_wmi_init(void)1094 static int __init hp_wmi_init(void)
1095 {
1096 int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
1097 int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
1098 int err;
1099
1100 if (!bios_capable && !event_capable)
1101 return -ENODEV;
1102
1103 if (event_capable) {
1104 err = hp_wmi_input_setup();
1105 if (err)
1106 return err;
1107 }
1108
1109 if (bios_capable) {
1110 hp_wmi_platform_dev =
1111 platform_device_register_simple("hp-wmi", -1, NULL, 0);
1112 if (IS_ERR(hp_wmi_platform_dev)) {
1113 err = PTR_ERR(hp_wmi_platform_dev);
1114 goto err_destroy_input;
1115 }
1116
1117 err = platform_driver_probe(&hp_wmi_driver, hp_wmi_bios_setup);
1118 if (err)
1119 goto err_unregister_device;
1120 }
1121
1122 return 0;
1123
1124 err_unregister_device:
1125 platform_device_unregister(hp_wmi_platform_dev);
1126 err_destroy_input:
1127 if (event_capable)
1128 hp_wmi_input_destroy();
1129
1130 return err;
1131 }
1132 module_init(hp_wmi_init);
1133
hp_wmi_exit(void)1134 static void __exit hp_wmi_exit(void)
1135 {
1136 if (wmi_has_guid(HPWMI_EVENT_GUID))
1137 hp_wmi_input_destroy();
1138
1139 if (hp_wmi_platform_dev) {
1140 platform_device_unregister(hp_wmi_platform_dev);
1141 platform_driver_unregister(&hp_wmi_driver);
1142 }
1143 }
1144 module_exit(hp_wmi_exit);
1145