Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / platform / x86 / hp-wmi.c
blobecd477964d117a13235c5bc212217daa803fff7c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * HP WMI hotkeys
5 * Copyright (C) 2008 Red Hat <mjg@redhat.com>
6 * Copyright (C) 2010, 2011 Anssi Hannula <anssi.hannula@iki.fi>
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>
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
21 #include <linux/input.h>
22 #include <linux/input/sparse-keymap.h>
23 #include <linux/platform_device.h>
24 #include <linux/acpi.h>
25 #include <linux/rfkill.h>
26 #include <linux/string.h>
28 MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>");
29 MODULE_DESCRIPTION("HP laptop WMI hotkeys driver");
30 MODULE_LICENSE("GPL");
32 MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
33 MODULE_ALIAS("wmi:5FB7F034-2C63-45e9-BE91-3D44E2C707E4");
35 #define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
36 #define HPWMI_BIOS_GUID "5FB7F034-2C63-45e9-BE91-3D44E2C707E4"
38 enum hp_wmi_radio {
39 HPWMI_WIFI = 0x0,
40 HPWMI_BLUETOOTH = 0x1,
41 HPWMI_WWAN = 0x2,
42 HPWMI_GPS = 0x3,
45 enum hp_wmi_event_ids {
46 HPWMI_DOCK_EVENT = 0x01,
47 HPWMI_PARK_HDD = 0x02,
48 HPWMI_SMART_ADAPTER = 0x03,
49 HPWMI_BEZEL_BUTTON = 0x04,
50 HPWMI_WIRELESS = 0x05,
51 HPWMI_CPU_BATTERY_THROTTLE = 0x06,
52 HPWMI_LOCK_SWITCH = 0x07,
53 HPWMI_LID_SWITCH = 0x08,
54 HPWMI_SCREEN_ROTATION = 0x09,
55 HPWMI_COOLSENSE_SYSTEM_MOBILE = 0x0A,
56 HPWMI_COOLSENSE_SYSTEM_HOT = 0x0B,
57 HPWMI_PROXIMITY_SENSOR = 0x0C,
58 HPWMI_BACKLIT_KB_BRIGHTNESS = 0x0D,
59 HPWMI_PEAKSHIFT_PERIOD = 0x0F,
60 HPWMI_BATTERY_CHARGE_PERIOD = 0x10,
63 struct bios_args {
64 u32 signature;
65 u32 command;
66 u32 commandtype;
67 u32 datasize;
68 u8 data[128];
71 enum hp_wmi_commandtype {
72 HPWMI_DISPLAY_QUERY = 0x01,
73 HPWMI_HDDTEMP_QUERY = 0x02,
74 HPWMI_ALS_QUERY = 0x03,
75 HPWMI_HARDWARE_QUERY = 0x04,
76 HPWMI_WIRELESS_QUERY = 0x05,
77 HPWMI_BATTERY_QUERY = 0x07,
78 HPWMI_BIOS_QUERY = 0x09,
79 HPWMI_FEATURE_QUERY = 0x0b,
80 HPWMI_HOTKEY_QUERY = 0x0c,
81 HPWMI_FEATURE2_QUERY = 0x0d,
82 HPWMI_WIRELESS2_QUERY = 0x1b,
83 HPWMI_POSTCODEERROR_QUERY = 0x2a,
84 HPWMI_THERMAL_POLICY_QUERY = 0x4c,
87 enum hp_wmi_command {
88 HPWMI_READ = 0x01,
89 HPWMI_WRITE = 0x02,
90 HPWMI_ODM = 0x03,
93 enum hp_wmi_hardware_mask {
94 HPWMI_DOCK_MASK = 0x01,
95 HPWMI_TABLET_MASK = 0x04,
98 struct bios_return {
99 u32 sigpass;
100 u32 return_code;
103 enum hp_return_value {
104 HPWMI_RET_WRONG_SIGNATURE = 0x02,
105 HPWMI_RET_UNKNOWN_COMMAND = 0x03,
106 HPWMI_RET_UNKNOWN_CMDTYPE = 0x04,
107 HPWMI_RET_INVALID_PARAMETERS = 0x05,
110 enum hp_wireless2_bits {
111 HPWMI_POWER_STATE = 0x01,
112 HPWMI_POWER_SOFT = 0x02,
113 HPWMI_POWER_BIOS = 0x04,
114 HPWMI_POWER_HARD = 0x08,
115 HPWMI_POWER_FW_OR_HW = HPWMI_POWER_BIOS | HPWMI_POWER_HARD,
118 #define IS_HWBLOCKED(x) ((x & HPWMI_POWER_FW_OR_HW) != HPWMI_POWER_FW_OR_HW)
119 #define IS_SWBLOCKED(x) !(x & HPWMI_POWER_SOFT)
121 struct bios_rfkill2_device_state {
122 u8 radio_type;
123 u8 bus_type;
124 u16 vendor_id;
125 u16 product_id;
126 u16 subsys_vendor_id;
127 u16 subsys_product_id;
128 u8 rfkill_id;
129 u8 power;
130 u8 unknown[4];
133 /* 7 devices fit into the 128 byte buffer */
134 #define HPWMI_MAX_RFKILL2_DEVICES 7
136 struct bios_rfkill2_state {
137 u8 unknown[7];
138 u8 count;
139 u8 pad[8];
140 struct bios_rfkill2_device_state device[HPWMI_MAX_RFKILL2_DEVICES];
143 static const struct key_entry hp_wmi_keymap[] = {
144 { KE_KEY, 0x02, { KEY_BRIGHTNESSUP } },
145 { KE_KEY, 0x03, { KEY_BRIGHTNESSDOWN } },
146 { KE_KEY, 0x20e6, { KEY_PROG1 } },
147 { KE_KEY, 0x20e8, { KEY_MEDIA } },
148 { KE_KEY, 0x2142, { KEY_MEDIA } },
149 { KE_KEY, 0x213b, { KEY_INFO } },
150 { KE_KEY, 0x2169, { KEY_ROTATE_DISPLAY } },
151 { KE_KEY, 0x216a, { KEY_SETUP } },
152 { KE_KEY, 0x231b, { KEY_HELP } },
153 { KE_END, 0 }
156 static struct input_dev *hp_wmi_input_dev;
157 static struct platform_device *hp_wmi_platform_dev;
159 static struct rfkill *wifi_rfkill;
160 static struct rfkill *bluetooth_rfkill;
161 static struct rfkill *wwan_rfkill;
163 struct rfkill2_device {
164 u8 id;
165 int num;
166 struct rfkill *rfkill;
169 static int rfkill2_count;
170 static struct rfkill2_device rfkill2[HPWMI_MAX_RFKILL2_DEVICES];
172 /* map output size to the corresponding WMI method id */
173 static inline int encode_outsize_for_pvsz(int outsize)
175 if (outsize > 4096)
176 return -EINVAL;
177 if (outsize > 1024)
178 return 5;
179 if (outsize > 128)
180 return 4;
181 if (outsize > 4)
182 return 3;
183 if (outsize > 0)
184 return 2;
185 return 1;
189 * hp_wmi_perform_query
191 * query: The commandtype (enum hp_wmi_commandtype)
192 * write: The command (enum hp_wmi_command)
193 * buffer: Buffer used as input and/or output
194 * insize: Size of input buffer
195 * outsize: Size of output buffer
197 * returns zero on success
198 * an HP WMI query specific error code (which is positive)
199 * -EINVAL if the query was not successful at all
200 * -EINVAL if the output buffer size exceeds buffersize
202 * Note: The buffersize must at least be the maximum of the input and output
203 * size. E.g. Battery info query is defined to have 1 byte input
204 * and 128 byte output. The caller would do:
205 * buffer = kzalloc(128, GFP_KERNEL);
206 * ret = hp_wmi_perform_query(HPWMI_BATTERY_QUERY, HPWMI_READ, buffer, 1, 128)
208 static int hp_wmi_perform_query(int query, enum hp_wmi_command command,
209 void *buffer, int insize, int outsize)
211 int mid;
212 struct bios_return *bios_return;
213 int actual_outsize;
214 union acpi_object *obj;
215 struct bios_args args = {
216 .signature = 0x55434553,
217 .command = command,
218 .commandtype = query,
219 .datasize = insize,
220 .data = { 0 },
222 struct acpi_buffer input = { sizeof(struct bios_args), &args };
223 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
224 int ret = 0;
226 mid = encode_outsize_for_pvsz(outsize);
227 if (WARN_ON(mid < 0))
228 return mid;
230 if (WARN_ON(insize > sizeof(args.data)))
231 return -EINVAL;
232 memcpy(&args.data[0], buffer, insize);
234 wmi_evaluate_method(HPWMI_BIOS_GUID, 0, mid, &input, &output);
236 obj = output.pointer;
238 if (!obj)
239 return -EINVAL;
241 if (obj->type != ACPI_TYPE_BUFFER) {
242 ret = -EINVAL;
243 goto out_free;
246 bios_return = (struct bios_return *)obj->buffer.pointer;
247 ret = bios_return->return_code;
249 if (ret) {
250 if (ret != HPWMI_RET_UNKNOWN_CMDTYPE)
251 pr_warn("query 0x%x returned error 0x%x\n", query, ret);
252 goto out_free;
255 /* Ignore output data of zero size */
256 if (!outsize)
257 goto out_free;
259 actual_outsize = min(outsize, (int)(obj->buffer.length - sizeof(*bios_return)));
260 memcpy(buffer, obj->buffer.pointer + sizeof(*bios_return), actual_outsize);
261 memset(buffer + actual_outsize, 0, outsize - actual_outsize);
263 out_free:
264 kfree(obj);
265 return ret;
268 static int hp_wmi_read_int(int query)
270 int val = 0, ret;
272 ret = hp_wmi_perform_query(query, HPWMI_READ, &val,
273 sizeof(val), sizeof(val));
275 if (ret)
276 return ret < 0 ? ret : -EINVAL;
278 return val;
281 static int hp_wmi_hw_state(int mask)
283 int state = hp_wmi_read_int(HPWMI_HARDWARE_QUERY);
285 if (state < 0)
286 return state;
288 return !!(state & mask);
291 static int __init hp_wmi_bios_2008_later(void)
293 int state = 0;
294 int ret = hp_wmi_perform_query(HPWMI_FEATURE_QUERY, HPWMI_READ, &state,
295 sizeof(state), sizeof(state));
296 if (!ret)
297 return 1;
299 return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
302 static int __init hp_wmi_bios_2009_later(void)
304 u8 state[128];
305 int ret = hp_wmi_perform_query(HPWMI_FEATURE2_QUERY, HPWMI_READ, &state,
306 sizeof(state), sizeof(state));
307 if (!ret)
308 return 1;
310 return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
313 static int __init hp_wmi_enable_hotkeys(void)
315 int value = 0x6e;
316 int ret = hp_wmi_perform_query(HPWMI_BIOS_QUERY, HPWMI_WRITE, &value,
317 sizeof(value), 0);
319 return ret <= 0 ? ret : -EINVAL;
322 static int hp_wmi_set_block(void *data, bool blocked)
324 enum hp_wmi_radio r = (enum hp_wmi_radio) data;
325 int query = BIT(r + 8) | ((!blocked) << r);
326 int ret;
328 ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE,
329 &query, sizeof(query), 0);
331 return ret <= 0 ? ret : -EINVAL;
334 static const struct rfkill_ops hp_wmi_rfkill_ops = {
335 .set_block = hp_wmi_set_block,
338 static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
340 int mask = 0x200 << (r * 8);
342 int wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
344 /* TBD: Pass error */
345 WARN_ONCE(wireless < 0, "error executing HPWMI_WIRELESS_QUERY");
347 return !(wireless & mask);
350 static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
352 int mask = 0x800 << (r * 8);
354 int wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
356 /* TBD: Pass error */
357 WARN_ONCE(wireless < 0, "error executing HPWMI_WIRELESS_QUERY");
359 return !(wireless & mask);
362 static int hp_wmi_rfkill2_set_block(void *data, bool blocked)
364 int rfkill_id = (int)(long)data;
365 char buffer[4] = { 0x01, 0x00, rfkill_id, !blocked };
366 int ret;
368 ret = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_WRITE,
369 buffer, sizeof(buffer), 0);
371 return ret <= 0 ? ret : -EINVAL;
374 static const struct rfkill_ops hp_wmi_rfkill2_ops = {
375 .set_block = hp_wmi_rfkill2_set_block,
378 static int hp_wmi_rfkill2_refresh(void)
380 struct bios_rfkill2_state state;
381 int err, i;
383 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
384 sizeof(state), sizeof(state));
385 if (err)
386 return err;
388 for (i = 0; i < rfkill2_count; i++) {
389 int num = rfkill2[i].num;
390 struct bios_rfkill2_device_state *devstate;
391 devstate = &state.device[num];
393 if (num >= state.count ||
394 devstate->rfkill_id != rfkill2[i].id) {
395 pr_warn("power configuration of the wireless devices unexpectedly changed\n");
396 continue;
399 rfkill_set_states(rfkill2[i].rfkill,
400 IS_SWBLOCKED(devstate->power),
401 IS_HWBLOCKED(devstate->power));
404 return 0;
407 static ssize_t display_show(struct device *dev, struct device_attribute *attr,
408 char *buf)
410 int value = hp_wmi_read_int(HPWMI_DISPLAY_QUERY);
411 if (value < 0)
412 return value;
413 return sprintf(buf, "%d\n", value);
416 static ssize_t hddtemp_show(struct device *dev, struct device_attribute *attr,
417 char *buf)
419 int value = hp_wmi_read_int(HPWMI_HDDTEMP_QUERY);
420 if (value < 0)
421 return value;
422 return sprintf(buf, "%d\n", value);
425 static ssize_t als_show(struct device *dev, struct device_attribute *attr,
426 char *buf)
428 int value = hp_wmi_read_int(HPWMI_ALS_QUERY);
429 if (value < 0)
430 return value;
431 return sprintf(buf, "%d\n", value);
434 static ssize_t dock_show(struct device *dev, struct device_attribute *attr,
435 char *buf)
437 int value = hp_wmi_hw_state(HPWMI_DOCK_MASK);
438 if (value < 0)
439 return value;
440 return sprintf(buf, "%d\n", value);
443 static ssize_t tablet_show(struct device *dev, struct device_attribute *attr,
444 char *buf)
446 int value = hp_wmi_hw_state(HPWMI_TABLET_MASK);
447 if (value < 0)
448 return value;
449 return sprintf(buf, "%d\n", value);
452 static ssize_t postcode_show(struct device *dev, struct device_attribute *attr,
453 char *buf)
455 /* Get the POST error code of previous boot failure. */
456 int value = hp_wmi_read_int(HPWMI_POSTCODEERROR_QUERY);
457 if (value < 0)
458 return value;
459 return sprintf(buf, "0x%x\n", value);
462 static ssize_t als_store(struct device *dev, struct device_attribute *attr,
463 const char *buf, size_t count)
465 u32 tmp;
466 int ret;
468 ret = kstrtou32(buf, 10, &tmp);
469 if (ret)
470 return ret;
472 ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, HPWMI_WRITE, &tmp,
473 sizeof(tmp), sizeof(tmp));
474 if (ret)
475 return ret < 0 ? ret : -EINVAL;
477 return count;
480 static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,
481 const char *buf, size_t count)
483 u32 tmp = 1;
484 bool clear;
485 int ret;
487 ret = kstrtobool(buf, &clear);
488 if (ret)
489 return ret;
491 if (clear == false)
492 return -EINVAL;
494 /* Clear the POST error code. It is kept until until cleared. */
495 ret = hp_wmi_perform_query(HPWMI_POSTCODEERROR_QUERY, HPWMI_WRITE, &tmp,
496 sizeof(tmp), sizeof(tmp));
497 if (ret)
498 return ret < 0 ? ret : -EINVAL;
500 return count;
503 static DEVICE_ATTR_RO(display);
504 static DEVICE_ATTR_RO(hddtemp);
505 static DEVICE_ATTR_RW(als);
506 static DEVICE_ATTR_RO(dock);
507 static DEVICE_ATTR_RO(tablet);
508 static DEVICE_ATTR_RW(postcode);
510 static struct attribute *hp_wmi_attrs[] = {
511 &dev_attr_display.attr,
512 &dev_attr_hddtemp.attr,
513 &dev_attr_als.attr,
514 &dev_attr_dock.attr,
515 &dev_attr_tablet.attr,
516 &dev_attr_postcode.attr,
517 NULL,
519 ATTRIBUTE_GROUPS(hp_wmi);
521 static void hp_wmi_notify(u32 value, void *context)
523 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
524 u32 event_id, event_data;
525 union acpi_object *obj;
526 acpi_status status;
527 u32 *location;
528 int key_code;
530 status = wmi_get_event_data(value, &response);
531 if (status != AE_OK) {
532 pr_info("bad event status 0x%x\n", status);
533 return;
536 obj = (union acpi_object *)response.pointer;
538 if (!obj)
539 return;
540 if (obj->type != ACPI_TYPE_BUFFER) {
541 pr_info("Unknown response received %d\n", obj->type);
542 kfree(obj);
543 return;
547 * Depending on ACPI version the concatenation of id and event data
548 * inside _WED function will result in a 8 or 16 byte buffer.
550 location = (u32 *)obj->buffer.pointer;
551 if (obj->buffer.length == 8) {
552 event_id = *location;
553 event_data = *(location + 1);
554 } else if (obj->buffer.length == 16) {
555 event_id = *location;
556 event_data = *(location + 2);
557 } else {
558 pr_info("Unknown buffer length %d\n", obj->buffer.length);
559 kfree(obj);
560 return;
562 kfree(obj);
564 switch (event_id) {
565 case HPWMI_DOCK_EVENT:
566 if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
567 input_report_switch(hp_wmi_input_dev, SW_DOCK,
568 hp_wmi_hw_state(HPWMI_DOCK_MASK));
569 if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
570 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
571 hp_wmi_hw_state(HPWMI_TABLET_MASK));
572 input_sync(hp_wmi_input_dev);
573 break;
574 case HPWMI_PARK_HDD:
575 break;
576 case HPWMI_SMART_ADAPTER:
577 break;
578 case HPWMI_BEZEL_BUTTON:
579 key_code = hp_wmi_read_int(HPWMI_HOTKEY_QUERY);
580 if (key_code < 0)
581 break;
583 if (!sparse_keymap_report_event(hp_wmi_input_dev,
584 key_code, 1, true))
585 pr_info("Unknown key code - 0x%x\n", key_code);
586 break;
587 case HPWMI_WIRELESS:
588 if (rfkill2_count) {
589 hp_wmi_rfkill2_refresh();
590 break;
593 if (wifi_rfkill)
594 rfkill_set_states(wifi_rfkill,
595 hp_wmi_get_sw_state(HPWMI_WIFI),
596 hp_wmi_get_hw_state(HPWMI_WIFI));
597 if (bluetooth_rfkill)
598 rfkill_set_states(bluetooth_rfkill,
599 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
600 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
601 if (wwan_rfkill)
602 rfkill_set_states(wwan_rfkill,
603 hp_wmi_get_sw_state(HPWMI_WWAN),
604 hp_wmi_get_hw_state(HPWMI_WWAN));
605 break;
606 case HPWMI_CPU_BATTERY_THROTTLE:
607 pr_info("Unimplemented CPU throttle because of 3 Cell battery event detected\n");
608 break;
609 case HPWMI_LOCK_SWITCH:
610 break;
611 case HPWMI_LID_SWITCH:
612 break;
613 case HPWMI_SCREEN_ROTATION:
614 break;
615 case HPWMI_COOLSENSE_SYSTEM_MOBILE:
616 break;
617 case HPWMI_COOLSENSE_SYSTEM_HOT:
618 break;
619 case HPWMI_PROXIMITY_SENSOR:
620 break;
621 case HPWMI_BACKLIT_KB_BRIGHTNESS:
622 break;
623 case HPWMI_PEAKSHIFT_PERIOD:
624 break;
625 case HPWMI_BATTERY_CHARGE_PERIOD:
626 break;
627 default:
628 pr_info("Unknown event_id - %d - 0x%x\n", event_id, event_data);
629 break;
633 static int __init hp_wmi_input_setup(void)
635 acpi_status status;
636 int err, val;
638 hp_wmi_input_dev = input_allocate_device();
639 if (!hp_wmi_input_dev)
640 return -ENOMEM;
642 hp_wmi_input_dev->name = "HP WMI hotkeys";
643 hp_wmi_input_dev->phys = "wmi/input0";
644 hp_wmi_input_dev->id.bustype = BUS_HOST;
646 __set_bit(EV_SW, hp_wmi_input_dev->evbit);
648 /* Dock */
649 val = hp_wmi_hw_state(HPWMI_DOCK_MASK);
650 if (!(val < 0)) {
651 __set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
652 input_report_switch(hp_wmi_input_dev, SW_DOCK, val);
655 /* Tablet mode */
656 val = hp_wmi_hw_state(HPWMI_TABLET_MASK);
657 if (!(val < 0)) {
658 __set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
659 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE, val);
662 err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
663 if (err)
664 goto err_free_dev;
666 /* Set initial hardware state */
667 input_sync(hp_wmi_input_dev);
669 if (!hp_wmi_bios_2009_later() && hp_wmi_bios_2008_later())
670 hp_wmi_enable_hotkeys();
672 status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
673 if (ACPI_FAILURE(status)) {
674 err = -EIO;
675 goto err_free_dev;
678 err = input_register_device(hp_wmi_input_dev);
679 if (err)
680 goto err_uninstall_notifier;
682 return 0;
684 err_uninstall_notifier:
685 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
686 err_free_dev:
687 input_free_device(hp_wmi_input_dev);
688 return err;
691 static void hp_wmi_input_destroy(void)
693 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
694 input_unregister_device(hp_wmi_input_dev);
697 static int __init hp_wmi_rfkill_setup(struct platform_device *device)
699 int err, wireless;
701 wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
702 if (wireless < 0)
703 return wireless;
705 err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE, &wireless,
706 sizeof(wireless), 0);
707 if (err)
708 return err;
710 if (wireless & 0x1) {
711 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
712 RFKILL_TYPE_WLAN,
713 &hp_wmi_rfkill_ops,
714 (void *) HPWMI_WIFI);
715 if (!wifi_rfkill)
716 return -ENOMEM;
717 rfkill_init_sw_state(wifi_rfkill,
718 hp_wmi_get_sw_state(HPWMI_WIFI));
719 rfkill_set_hw_state(wifi_rfkill,
720 hp_wmi_get_hw_state(HPWMI_WIFI));
721 err = rfkill_register(wifi_rfkill);
722 if (err)
723 goto register_wifi_error;
726 if (wireless & 0x2) {
727 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
728 RFKILL_TYPE_BLUETOOTH,
729 &hp_wmi_rfkill_ops,
730 (void *) HPWMI_BLUETOOTH);
731 if (!bluetooth_rfkill) {
732 err = -ENOMEM;
733 goto register_bluetooth_error;
735 rfkill_init_sw_state(bluetooth_rfkill,
736 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
737 rfkill_set_hw_state(bluetooth_rfkill,
738 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
739 err = rfkill_register(bluetooth_rfkill);
740 if (err)
741 goto register_bluetooth_error;
744 if (wireless & 0x4) {
745 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
746 RFKILL_TYPE_WWAN,
747 &hp_wmi_rfkill_ops,
748 (void *) HPWMI_WWAN);
749 if (!wwan_rfkill) {
750 err = -ENOMEM;
751 goto register_wwan_error;
753 rfkill_init_sw_state(wwan_rfkill,
754 hp_wmi_get_sw_state(HPWMI_WWAN));
755 rfkill_set_hw_state(wwan_rfkill,
756 hp_wmi_get_hw_state(HPWMI_WWAN));
757 err = rfkill_register(wwan_rfkill);
758 if (err)
759 goto register_wwan_error;
762 return 0;
764 register_wwan_error:
765 rfkill_destroy(wwan_rfkill);
766 wwan_rfkill = NULL;
767 if (bluetooth_rfkill)
768 rfkill_unregister(bluetooth_rfkill);
769 register_bluetooth_error:
770 rfkill_destroy(bluetooth_rfkill);
771 bluetooth_rfkill = NULL;
772 if (wifi_rfkill)
773 rfkill_unregister(wifi_rfkill);
774 register_wifi_error:
775 rfkill_destroy(wifi_rfkill);
776 wifi_rfkill = NULL;
777 return err;
780 static int __init hp_wmi_rfkill2_setup(struct platform_device *device)
782 struct bios_rfkill2_state state;
783 int err, i;
785 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
786 sizeof(state), sizeof(state));
787 if (err)
788 return err < 0 ? err : -EINVAL;
790 if (state.count > HPWMI_MAX_RFKILL2_DEVICES) {
791 pr_warn("unable to parse 0x1b query output\n");
792 return -EINVAL;
795 for (i = 0; i < state.count; i++) {
796 struct rfkill *rfkill;
797 enum rfkill_type type;
798 char *name;
799 switch (state.device[i].radio_type) {
800 case HPWMI_WIFI:
801 type = RFKILL_TYPE_WLAN;
802 name = "hp-wifi";
803 break;
804 case HPWMI_BLUETOOTH:
805 type = RFKILL_TYPE_BLUETOOTH;
806 name = "hp-bluetooth";
807 break;
808 case HPWMI_WWAN:
809 type = RFKILL_TYPE_WWAN;
810 name = "hp-wwan";
811 break;
812 case HPWMI_GPS:
813 type = RFKILL_TYPE_GPS;
814 name = "hp-gps";
815 break;
816 default:
817 pr_warn("unknown device type 0x%x\n",
818 state.device[i].radio_type);
819 continue;
822 if (!state.device[i].vendor_id) {
823 pr_warn("zero device %d while %d reported\n",
824 i, state.count);
825 continue;
828 rfkill = rfkill_alloc(name, &device->dev, type,
829 &hp_wmi_rfkill2_ops, (void *)(long)i);
830 if (!rfkill) {
831 err = -ENOMEM;
832 goto fail;
835 rfkill2[rfkill2_count].id = state.device[i].rfkill_id;
836 rfkill2[rfkill2_count].num = i;
837 rfkill2[rfkill2_count].rfkill = rfkill;
839 rfkill_init_sw_state(rfkill,
840 IS_SWBLOCKED(state.device[i].power));
841 rfkill_set_hw_state(rfkill,
842 IS_HWBLOCKED(state.device[i].power));
844 if (!(state.device[i].power & HPWMI_POWER_BIOS))
845 pr_info("device %s blocked by BIOS\n", name);
847 err = rfkill_register(rfkill);
848 if (err) {
849 rfkill_destroy(rfkill);
850 goto fail;
853 rfkill2_count++;
856 return 0;
857 fail:
858 for (; rfkill2_count > 0; rfkill2_count--) {
859 rfkill_unregister(rfkill2[rfkill2_count - 1].rfkill);
860 rfkill_destroy(rfkill2[rfkill2_count - 1].rfkill);
862 return err;
865 static int thermal_policy_setup(struct platform_device *device)
867 int err, tp;
869 tp = hp_wmi_read_int(HPWMI_THERMAL_POLICY_QUERY);
870 if (tp < 0)
871 return tp;
874 * call thermal policy write command to ensure that the firmware correctly
875 * sets the OEM variables for the DPTF
877 err = hp_wmi_perform_query(HPWMI_THERMAL_POLICY_QUERY, HPWMI_WRITE, &tp,
878 sizeof(tp), 0);
879 if (err)
880 return err;
882 return 0;
885 static int __init hp_wmi_bios_setup(struct platform_device *device)
887 /* clear detected rfkill devices */
888 wifi_rfkill = NULL;
889 bluetooth_rfkill = NULL;
890 wwan_rfkill = NULL;
891 rfkill2_count = 0;
893 if (hp_wmi_rfkill_setup(device))
894 hp_wmi_rfkill2_setup(device);
896 thermal_policy_setup(device);
898 return 0;
901 static int __exit hp_wmi_bios_remove(struct platform_device *device)
903 int i;
905 for (i = 0; i < rfkill2_count; i++) {
906 rfkill_unregister(rfkill2[i].rfkill);
907 rfkill_destroy(rfkill2[i].rfkill);
910 if (wifi_rfkill) {
911 rfkill_unregister(wifi_rfkill);
912 rfkill_destroy(wifi_rfkill);
914 if (bluetooth_rfkill) {
915 rfkill_unregister(bluetooth_rfkill);
916 rfkill_destroy(bluetooth_rfkill);
918 if (wwan_rfkill) {
919 rfkill_unregister(wwan_rfkill);
920 rfkill_destroy(wwan_rfkill);
923 return 0;
926 static int hp_wmi_resume_handler(struct device *device)
929 * Hardware state may have changed while suspended, so trigger
930 * input events for the current state. As this is a switch,
931 * the input layer will only actually pass it on if the state
932 * changed.
934 if (hp_wmi_input_dev) {
935 if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
936 input_report_switch(hp_wmi_input_dev, SW_DOCK,
937 hp_wmi_hw_state(HPWMI_DOCK_MASK));
938 if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
939 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
940 hp_wmi_hw_state(HPWMI_TABLET_MASK));
941 input_sync(hp_wmi_input_dev);
944 if (rfkill2_count)
945 hp_wmi_rfkill2_refresh();
947 if (wifi_rfkill)
948 rfkill_set_states(wifi_rfkill,
949 hp_wmi_get_sw_state(HPWMI_WIFI),
950 hp_wmi_get_hw_state(HPWMI_WIFI));
951 if (bluetooth_rfkill)
952 rfkill_set_states(bluetooth_rfkill,
953 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
954 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
955 if (wwan_rfkill)
956 rfkill_set_states(wwan_rfkill,
957 hp_wmi_get_sw_state(HPWMI_WWAN),
958 hp_wmi_get_hw_state(HPWMI_WWAN));
960 return 0;
963 static const struct dev_pm_ops hp_wmi_pm_ops = {
964 .resume = hp_wmi_resume_handler,
965 .restore = hp_wmi_resume_handler,
968 static struct platform_driver hp_wmi_driver = {
969 .driver = {
970 .name = "hp-wmi",
971 .pm = &hp_wmi_pm_ops,
972 .dev_groups = hp_wmi_groups,
974 .remove = __exit_p(hp_wmi_bios_remove),
977 static int __init hp_wmi_init(void)
979 int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
980 int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
981 int err;
983 if (!bios_capable && !event_capable)
984 return -ENODEV;
986 if (event_capable) {
987 err = hp_wmi_input_setup();
988 if (err)
989 return err;
992 if (bios_capable) {
993 hp_wmi_platform_dev =
994 platform_device_register_simple("hp-wmi", -1, NULL, 0);
995 if (IS_ERR(hp_wmi_platform_dev)) {
996 err = PTR_ERR(hp_wmi_platform_dev);
997 goto err_destroy_input;
1000 err = platform_driver_probe(&hp_wmi_driver, hp_wmi_bios_setup);
1001 if (err)
1002 goto err_unregister_device;
1005 return 0;
1007 err_unregister_device:
1008 platform_device_unregister(hp_wmi_platform_dev);
1009 err_destroy_input:
1010 if (event_capable)
1011 hp_wmi_input_destroy();
1013 return err;
1015 module_init(hp_wmi_init);
1017 static void __exit hp_wmi_exit(void)
1019 if (wmi_has_guid(HPWMI_EVENT_GUID))
1020 hp_wmi_input_destroy();
1022 if (hp_wmi_platform_dev) {
1023 platform_device_unregister(hp_wmi_platform_dev);
1024 platform_driver_unregister(&hp_wmi_driver);
1027 module_exit(hp_wmi_exit);