1 // SPDX-License-Identifier: GPL-2.0-or-later
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"
40 HPWMI_BLUETOOTH
= 0x1,
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,
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,
93 enum hp_wmi_hardware_mask
{
94 HPWMI_DOCK_MASK
= 0x01,
95 HPWMI_TABLET_MASK
= 0x04,
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
{
126 u16 subsys_vendor_id
;
127 u16 subsys_product_id
;
133 /* 7 devices fit into the 128 byte buffer */
134 #define HPWMI_MAX_RFKILL2_DEVICES 7
136 struct bios_rfkill2_state
{
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
} },
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
{
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
)
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
)
212 struct bios_return
*bios_return
;
214 union acpi_object
*obj
;
215 struct bios_args args
= {
216 .signature
= 0x55434553,
218 .commandtype
= query
,
222 struct acpi_buffer input
= { sizeof(struct bios_args
), &args
};
223 struct acpi_buffer output
= { ACPI_ALLOCATE_BUFFER
, NULL
};
226 mid
= encode_outsize_for_pvsz(outsize
);
227 if (WARN_ON(mid
< 0))
230 if (WARN_ON(insize
> sizeof(args
.data
)))
232 memcpy(&args
.data
[0], buffer
, insize
);
234 wmi_evaluate_method(HPWMI_BIOS_GUID
, 0, mid
, &input
, &output
);
236 obj
= output
.pointer
;
241 if (obj
->type
!= ACPI_TYPE_BUFFER
) {
246 bios_return
= (struct bios_return
*)obj
->buffer
.pointer
;
247 ret
= bios_return
->return_code
;
250 if (ret
!= HPWMI_RET_UNKNOWN_CMDTYPE
)
251 pr_warn("query 0x%x returned error 0x%x\n", query
, ret
);
255 /* Ignore output data of zero size */
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
);
268 static int hp_wmi_read_int(int query
)
272 ret
= hp_wmi_perform_query(query
, HPWMI_READ
, &val
,
273 sizeof(val
), sizeof(val
));
276 return ret
< 0 ? ret
: -EINVAL
;
281 static int hp_wmi_hw_state(int mask
)
283 int state
= hp_wmi_read_int(HPWMI_HARDWARE_QUERY
);
288 return !!(state
& mask
);
291 static int __init
hp_wmi_bios_2008_later(void)
294 int ret
= hp_wmi_perform_query(HPWMI_FEATURE_QUERY
, HPWMI_READ
, &state
,
295 sizeof(state
), sizeof(state
));
299 return (ret
== HPWMI_RET_UNKNOWN_CMDTYPE
) ? 0 : -ENXIO
;
302 static int __init
hp_wmi_bios_2009_later(void)
305 int ret
= hp_wmi_perform_query(HPWMI_FEATURE2_QUERY
, HPWMI_READ
, &state
,
306 sizeof(state
), sizeof(state
));
310 return (ret
== HPWMI_RET_UNKNOWN_CMDTYPE
) ? 0 : -ENXIO
;
313 static int __init
hp_wmi_enable_hotkeys(void)
316 int ret
= hp_wmi_perform_query(HPWMI_BIOS_QUERY
, HPWMI_WRITE
, &value
,
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
);
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
};
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
;
383 err
= hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY
, HPWMI_READ
, &state
,
384 sizeof(state
), sizeof(state
));
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");
399 rfkill_set_states(rfkill2
[i
].rfkill
,
400 IS_SWBLOCKED(devstate
->power
),
401 IS_HWBLOCKED(devstate
->power
));
407 static ssize_t
display_show(struct device
*dev
, struct device_attribute
*attr
,
410 int value
= hp_wmi_read_int(HPWMI_DISPLAY_QUERY
);
413 return sprintf(buf
, "%d\n", value
);
416 static ssize_t
hddtemp_show(struct device
*dev
, struct device_attribute
*attr
,
419 int value
= hp_wmi_read_int(HPWMI_HDDTEMP_QUERY
);
422 return sprintf(buf
, "%d\n", value
);
425 static ssize_t
als_show(struct device
*dev
, struct device_attribute
*attr
,
428 int value
= hp_wmi_read_int(HPWMI_ALS_QUERY
);
431 return sprintf(buf
, "%d\n", value
);
434 static ssize_t
dock_show(struct device
*dev
, struct device_attribute
*attr
,
437 int value
= hp_wmi_hw_state(HPWMI_DOCK_MASK
);
440 return sprintf(buf
, "%d\n", value
);
443 static ssize_t
tablet_show(struct device
*dev
, struct device_attribute
*attr
,
446 int value
= hp_wmi_hw_state(HPWMI_TABLET_MASK
);
449 return sprintf(buf
, "%d\n", value
);
452 static ssize_t
postcode_show(struct device
*dev
, struct device_attribute
*attr
,
455 /* Get the POST error code of previous boot failure. */
456 int value
= hp_wmi_read_int(HPWMI_POSTCODEERROR_QUERY
);
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
)
468 ret
= kstrtou32(buf
, 10, &tmp
);
472 ret
= hp_wmi_perform_query(HPWMI_ALS_QUERY
, HPWMI_WRITE
, &tmp
,
473 sizeof(tmp
), sizeof(tmp
));
475 return ret
< 0 ? ret
: -EINVAL
;
480 static ssize_t
postcode_store(struct device
*dev
, struct device_attribute
*attr
,
481 const char *buf
, size_t count
)
487 ret
= kstrtobool(buf
, &clear
);
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
));
498 return ret
< 0 ? ret
: -EINVAL
;
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
,
515 &dev_attr_tablet
.attr
,
516 &dev_attr_postcode
.attr
,
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
;
530 status
= wmi_get_event_data(value
, &response
);
531 if (status
!= AE_OK
) {
532 pr_info("bad event status 0x%x\n", status
);
536 obj
= (union acpi_object
*)response
.pointer
;
540 if (obj
->type
!= ACPI_TYPE_BUFFER
) {
541 pr_info("Unknown response received %d\n", obj
->type
);
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);
558 pr_info("Unknown buffer length %d\n", obj
->buffer
.length
);
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
);
576 case HPWMI_SMART_ADAPTER
:
578 case HPWMI_BEZEL_BUTTON
:
579 key_code
= hp_wmi_read_int(HPWMI_HOTKEY_QUERY
);
583 if (!sparse_keymap_report_event(hp_wmi_input_dev
,
585 pr_info("Unknown key code - 0x%x\n", key_code
);
589 hp_wmi_rfkill2_refresh();
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
));
602 rfkill_set_states(wwan_rfkill
,
603 hp_wmi_get_sw_state(HPWMI_WWAN
),
604 hp_wmi_get_hw_state(HPWMI_WWAN
));
606 case HPWMI_CPU_BATTERY_THROTTLE
:
607 pr_info("Unimplemented CPU throttle because of 3 Cell battery event detected\n");
609 case HPWMI_LOCK_SWITCH
:
611 case HPWMI_LID_SWITCH
:
613 case HPWMI_SCREEN_ROTATION
:
615 case HPWMI_COOLSENSE_SYSTEM_MOBILE
:
617 case HPWMI_COOLSENSE_SYSTEM_HOT
:
619 case HPWMI_PROXIMITY_SENSOR
:
621 case HPWMI_BACKLIT_KB_BRIGHTNESS
:
623 case HPWMI_PEAKSHIFT_PERIOD
:
625 case HPWMI_BATTERY_CHARGE_PERIOD
:
628 pr_info("Unknown event_id - %d - 0x%x\n", event_id
, event_data
);
633 static int __init
hp_wmi_input_setup(void)
638 hp_wmi_input_dev
= input_allocate_device();
639 if (!hp_wmi_input_dev
)
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
);
649 val
= hp_wmi_hw_state(HPWMI_DOCK_MASK
);
651 __set_bit(SW_DOCK
, hp_wmi_input_dev
->swbit
);
652 input_report_switch(hp_wmi_input_dev
, SW_DOCK
, val
);
656 val
= hp_wmi_hw_state(HPWMI_TABLET_MASK
);
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
);
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
)) {
678 err
= input_register_device(hp_wmi_input_dev
);
680 goto err_uninstall_notifier
;
684 err_uninstall_notifier
:
685 wmi_remove_notify_handler(HPWMI_EVENT_GUID
);
687 input_free_device(hp_wmi_input_dev
);
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
)
701 wireless
= hp_wmi_read_int(HPWMI_WIRELESS_QUERY
);
705 err
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, HPWMI_WRITE
, &wireless
,
706 sizeof(wireless
), 0);
710 if (wireless
& 0x1) {
711 wifi_rfkill
= rfkill_alloc("hp-wifi", &device
->dev
,
714 (void *) HPWMI_WIFI
);
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
);
723 goto register_wifi_error
;
726 if (wireless
& 0x2) {
727 bluetooth_rfkill
= rfkill_alloc("hp-bluetooth", &device
->dev
,
728 RFKILL_TYPE_BLUETOOTH
,
730 (void *) HPWMI_BLUETOOTH
);
731 if (!bluetooth_rfkill
) {
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
);
741 goto register_bluetooth_error
;
744 if (wireless
& 0x4) {
745 wwan_rfkill
= rfkill_alloc("hp-wwan", &device
->dev
,
748 (void *) HPWMI_WWAN
);
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
);
759 goto register_wwan_error
;
765 rfkill_destroy(wwan_rfkill
);
767 if (bluetooth_rfkill
)
768 rfkill_unregister(bluetooth_rfkill
);
769 register_bluetooth_error
:
770 rfkill_destroy(bluetooth_rfkill
);
771 bluetooth_rfkill
= NULL
;
773 rfkill_unregister(wifi_rfkill
);
775 rfkill_destroy(wifi_rfkill
);
780 static int __init
hp_wmi_rfkill2_setup(struct platform_device
*device
)
782 struct bios_rfkill2_state state
;
785 err
= hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY
, HPWMI_READ
, &state
,
786 sizeof(state
), sizeof(state
));
788 return err
< 0 ? err
: -EINVAL
;
790 if (state
.count
> HPWMI_MAX_RFKILL2_DEVICES
) {
791 pr_warn("unable to parse 0x1b query output\n");
795 for (i
= 0; i
< state
.count
; i
++) {
796 struct rfkill
*rfkill
;
797 enum rfkill_type type
;
799 switch (state
.device
[i
].radio_type
) {
801 type
= RFKILL_TYPE_WLAN
;
804 case HPWMI_BLUETOOTH
:
805 type
= RFKILL_TYPE_BLUETOOTH
;
806 name
= "hp-bluetooth";
809 type
= RFKILL_TYPE_WWAN
;
813 type
= RFKILL_TYPE_GPS
;
817 pr_warn("unknown device type 0x%x\n",
818 state
.device
[i
].radio_type
);
822 if (!state
.device
[i
].vendor_id
) {
823 pr_warn("zero device %d while %d reported\n",
828 rfkill
= rfkill_alloc(name
, &device
->dev
, type
,
829 &hp_wmi_rfkill2_ops
, (void *)(long)i
);
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
);
849 rfkill_destroy(rfkill
);
858 for (; rfkill2_count
> 0; rfkill2_count
--) {
859 rfkill_unregister(rfkill2
[rfkill2_count
- 1].rfkill
);
860 rfkill_destroy(rfkill2
[rfkill2_count
- 1].rfkill
);
865 static int thermal_policy_setup(struct platform_device
*device
)
869 tp
= hp_wmi_read_int(HPWMI_THERMAL_POLICY_QUERY
);
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
,
885 static int __init
hp_wmi_bios_setup(struct platform_device
*device
)
887 /* clear detected rfkill devices */
889 bluetooth_rfkill
= NULL
;
893 if (hp_wmi_rfkill_setup(device
))
894 hp_wmi_rfkill2_setup(device
);
896 thermal_policy_setup(device
);
901 static int __exit
hp_wmi_bios_remove(struct platform_device
*device
)
905 for (i
= 0; i
< rfkill2_count
; i
++) {
906 rfkill_unregister(rfkill2
[i
].rfkill
);
907 rfkill_destroy(rfkill2
[i
].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
);
919 rfkill_unregister(wwan_rfkill
);
920 rfkill_destroy(wwan_rfkill
);
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
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
);
945 hp_wmi_rfkill2_refresh();
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
));
956 rfkill_set_states(wwan_rfkill
,
957 hp_wmi_get_sw_state(HPWMI_WWAN
),
958 hp_wmi_get_hw_state(HPWMI_WWAN
));
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
= {
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
);
983 if (!bios_capable
&& !event_capable
)
987 err
= hp_wmi_input_setup();
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
);
1002 goto err_unregister_device
;
1007 err_unregister_device
:
1008 platform_device_unregister(hp_wmi_platform_dev
);
1011 hp_wmi_input_destroy();
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
);