blob: 2073f1e97970d146720609438fefed41f96e12bd [file] [log] [blame]
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001/*
2 * HP WMI hotkeys
3 *
4 * Copyright (C) 2008 Red Hat <mjg@redhat.com>
Anssi Hannulac0b9c642011-02-20 20:07:26 +02005 * Copyright (C) 2010, 2011 Anssi Hannula <anssi.hannula@iki.fi>
Matthew Garrett62ec30d2008-07-25 01:45:39 -07006 *
7 * Portions based on wistron_btns.c:
8 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
9 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
10 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
Joe Perchesb5a42232011-03-29 15:21:41 -070027#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
Matthew Garrett62ec30d2008-07-25 01:45:39 -070029#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Matthew Garrett62ec30d2008-07-25 01:45:39 -070033#include <linux/types.h>
34#include <linux/input.h>
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -070035#include <linux/input/sparse-keymap.h>
Matthew Garrett62ec30d2008-07-25 01:45:39 -070036#include <linux/platform_device.h>
37#include <linux/acpi.h>
38#include <linux/rfkill.h>
39#include <linux/string.h>
40
41MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>");
42MODULE_DESCRIPTION("HP laptop WMI hotkeys driver");
43MODULE_LICENSE("GPL");
44
45MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
46MODULE_ALIAS("wmi:5FB7F034-2C63-45e9-BE91-3D44E2C707E4");
47
48#define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
49#define HPWMI_BIOS_GUID "5FB7F034-2C63-45e9-BE91-3D44E2C707E4"
50
Alan Jenkinse5fbba82009-07-21 12:14:01 +010051enum hp_wmi_radio {
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -070052 HPWMI_WIFI = 0x0,
53 HPWMI_BLUETOOTH = 0x1,
54 HPWMI_WWAN = 0x2,
55 HPWMI_GPS = 0x3,
Alan Jenkinse5fbba82009-07-21 12:14:01 +010056};
57
Thomas Renninger751ae802010-05-21 16:18:09 +020058enum hp_wmi_event_ids {
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -070059 HPWMI_DOCK_EVENT = 0x01,
60 HPWMI_PARK_HDD = 0x02,
61 HPWMI_SMART_ADAPTER = 0x03,
62 HPWMI_BEZEL_BUTTON = 0x04,
63 HPWMI_WIRELESS = 0x05,
64 HPWMI_CPU_BATTERY_THROTTLE = 0x06,
65 HPWMI_LOCK_SWITCH = 0x07,
66 HPWMI_LID_SWITCH = 0x08,
67 HPWMI_SCREEN_ROTATION = 0x09,
68 HPWMI_COOLSENSE_SYSTEM_MOBILE = 0x0A,
69 HPWMI_COOLSENSE_SYSTEM_HOT = 0x0B,
70 HPWMI_PROXIMITY_SENSOR = 0x0C,
71 HPWMI_BACKLIT_KB_BRIGHTNESS = 0x0D,
72 HPWMI_PEAKSHIFT_PERIOD = 0x0F,
73 HPWMI_BATTERY_CHARGE_PERIOD = 0x10,
Thomas Renninger751ae802010-05-21 16:18:09 +020074};
75
Matthew Garrett62ec30d2008-07-25 01:45:39 -070076struct bios_args {
77 u32 signature;
78 u32 command;
79 u32 commandtype;
80 u32 datasize;
Matthew Garretta8ec1052010-08-23 15:52:34 -040081 u32 data;
Matthew Garrett62ec30d2008-07-25 01:45:39 -070082};
83
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -070084enum hp_wmi_commandtype {
85 HPWMI_DISPLAY_QUERY = 0x01,
86 HPWMI_HDDTEMP_QUERY = 0x02,
87 HPWMI_ALS_QUERY = 0x03,
88 HPWMI_HARDWARE_QUERY = 0x04,
89 HPWMI_WIRELESS_QUERY = 0x05,
90 HPWMI_BATTERY_QUERY = 0x07,
91 HPWMI_BIOS_QUERY = 0x09,
92 HPWMI_FEATURE_QUERY = 0x0b,
93 HPWMI_HOTKEY_QUERY = 0x0c,
94 HPWMI_FEATURE2_QUERY = 0x0d,
95 HPWMI_WIRELESS2_QUERY = 0x1b,
96 HPWMI_POSTCODEERROR_QUERY = 0x2a,
97};
98
99enum hp_wmi_command {
100 HPWMI_READ = 0x01,
101 HPWMI_WRITE = 0x02,
102 HPWMI_ODM = 0x03,
103};
104
105#define BIOS_ARGS_INIT(write, ctype, size) \
106 (struct bios_args) { .signature = 0x55434553, \
107 .command = (write) ? 0x2 : 0x1, \
108 .commandtype = (ctype), \
109 .datasize = (size), \
110 .data = 0 }
111
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700112struct bios_return {
113 u32 sigpass;
114 u32 return_code;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700115};
116
Anssi Hannula9af0e0f2011-02-20 20:07:20 +0200117enum hp_return_value {
118 HPWMI_RET_WRONG_SIGNATURE = 0x02,
119 HPWMI_RET_UNKNOWN_COMMAND = 0x03,
120 HPWMI_RET_UNKNOWN_CMDTYPE = 0x04,
121 HPWMI_RET_INVALID_PARAMETERS = 0x05,
122};
123
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200124enum hp_wireless2_bits {
125 HPWMI_POWER_STATE = 0x01,
126 HPWMI_POWER_SOFT = 0x02,
127 HPWMI_POWER_BIOS = 0x04,
128 HPWMI_POWER_HARD = 0x08,
129};
130
131#define IS_HWBLOCKED(x) ((x & (HPWMI_POWER_BIOS | HPWMI_POWER_HARD)) \
132 != (HPWMI_POWER_BIOS | HPWMI_POWER_HARD))
133#define IS_SWBLOCKED(x) !(x & HPWMI_POWER_SOFT)
134
135struct bios_rfkill2_device_state {
136 u8 radio_type;
137 u8 bus_type;
138 u16 vendor_id;
139 u16 product_id;
140 u16 subsys_vendor_id;
141 u16 subsys_product_id;
142 u8 rfkill_id;
143 u8 power;
144 u8 unknown[4];
145};
146
147/* 7 devices fit into the 128 byte buffer */
148#define HPWMI_MAX_RFKILL2_DEVICES 7
149
150struct bios_rfkill2_state {
151 u8 unknown[7];
152 u8 count;
153 u8 pad[8];
154 struct bios_rfkill2_device_state device[HPWMI_MAX_RFKILL2_DEVICES];
155};
156
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700157static const struct key_entry hp_wmi_keymap[] = {
158 { KE_KEY, 0x02, { KEY_BRIGHTNESSUP } },
159 { KE_KEY, 0x03, { KEY_BRIGHTNESSDOWN } },
160 { KE_KEY, 0x20e6, { KEY_PROG1 } },
161 { KE_KEY, 0x20e8, { KEY_MEDIA } },
162 { KE_KEY, 0x2142, { KEY_MEDIA } },
163 { KE_KEY, 0x213b, { KEY_INFO } },
Stefan Brünse92865e2015-03-05 23:25:51 -0800164 { KE_KEY, 0x2169, { KEY_ROTATE_DISPLAY } },
Kyle Evansf82bdd02014-06-09 12:26:06 -0500165 { KE_KEY, 0x216a, { KEY_SETUP } },
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700166 { KE_KEY, 0x231b, { KEY_HELP } },
167 { KE_END, 0 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700168};
169
170static struct input_dev *hp_wmi_input_dev;
171static struct platform_device *hp_wmi_platform_dev;
172
173static struct rfkill *wifi_rfkill;
174static struct rfkill *bluetooth_rfkill;
175static struct rfkill *wwan_rfkill;
176
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200177struct rfkill2_device {
178 u8 id;
179 int num;
180 struct rfkill *rfkill;
181};
182
183static int rfkill2_count;
184static struct rfkill2_device rfkill2[HPWMI_MAX_RFKILL2_DEVICES];
185
Thomas Renninger6d96e002010-05-21 16:42:40 +0200186/*
187 * hp_wmi_perform_query
188 *
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700189 * query: The commandtype (enum hp_wmi_commandtype)
190 * write: The command (enum hp_wmi_command)
Thomas Renninger6d96e002010-05-21 16:42:40 +0200191 * buffer: Buffer used as input and/or output
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200192 * insize: Size of input buffer
193 * outsize: Size of output buffer
Thomas Renninger6d96e002010-05-21 16:42:40 +0200194 *
195 * returns zero on success
196 * an HP WMI query specific error code (which is positive)
197 * -EINVAL if the query was not successful at all
198 * -EINVAL if the output buffer size exceeds buffersize
199 *
200 * Note: The buffersize must at least be the maximum of the input and output
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700201 * size. E.g. Battery info query is defined to have 1 byte input
Thomas Renninger6d96e002010-05-21 16:42:40 +0200202 * and 128 byte output. The caller would do:
203 * buffer = kzalloc(128, GFP_KERNEL);
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700204 * ret = hp_wmi_perform_query(HPWMI_BATTERY_QUERY, HPWMI_READ, buffer, 1, 128)
Thomas Renninger6d96e002010-05-21 16:42:40 +0200205 */
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700206static int hp_wmi_perform_query(int query, enum hp_wmi_command command,
207 void *buffer, int insize, int outsize)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700208{
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200209 struct bios_return *bios_return;
210 int actual_outsize;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700211 union acpi_object *obj;
212 struct bios_args args = {
213 .signature = 0x55434553,
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700214 .command = command,
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700215 .commandtype = query,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200216 .datasize = insize,
217 .data = 0,
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700218 };
219 struct acpi_buffer input = { sizeof(struct bios_args), &args };
220 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Eric Dumazet04018462011-07-11 12:22:21 +0200221 u32 rc;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700222
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200223 if (WARN_ON(insize > sizeof(args.data)))
224 return -EINVAL;
225 memcpy(&args.data, buffer, insize);
226
Anssi Hannula25bb0672011-02-20 20:07:21 +0200227 wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700228
229 obj = output.pointer;
230
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100231 if (!obj)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700232 return -EINVAL;
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100233 else if (obj->type != ACPI_TYPE_BUFFER) {
234 kfree(obj);
235 return -EINVAL;
236 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700237
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200238 bios_return = (struct bios_return *)obj->buffer.pointer;
Eric Dumazet04018462011-07-11 12:22:21 +0200239 rc = bios_return->return_code;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200240
Eric Dumazet04018462011-07-11 12:22:21 +0200241 if (rc) {
242 if (rc != HPWMI_RET_UNKNOWN_CMDTYPE)
243 pr_warn("query 0x%x returned error 0x%x\n", query, rc);
Anssi Hannula9af0e0f2011-02-20 20:07:20 +0200244 kfree(obj);
Eric Dumazet04018462011-07-11 12:22:21 +0200245 return rc;
Anssi Hannula9af0e0f2011-02-20 20:07:20 +0200246 }
247
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200248 if (!outsize) {
249 /* ignore output data */
250 kfree(obj);
251 return 0;
252 }
Zeng Zhaoming53c96df2010-11-19 00:46:19 +0800253
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200254 actual_outsize = min(outsize, (int)(obj->buffer.length - sizeof(*bios_return)));
255 memcpy(buffer, obj->buffer.pointer + sizeof(*bios_return), actual_outsize);
256 memset(buffer + actual_outsize, 0, outsize - actual_outsize);
Zeng Zhaoming53c96df2010-11-19 00:46:19 +0800257 kfree(obj);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200258 return 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700259}
260
261static int hp_wmi_display_state(void)
262{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400263 int state = 0;
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700264 int ret = hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, HPWMI_READ, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200265 sizeof(state), sizeof(state));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200266 if (ret)
Carlo Caioned3138762017-04-19 22:36:39 +0200267 return ret < 0 ? ret : -EINVAL;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200268 return state;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700269}
270
271static int hp_wmi_hddtemp_state(void)
272{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400273 int state = 0;
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700274 int ret = hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, HPWMI_READ, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200275 sizeof(state), sizeof(state));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200276 if (ret)
Carlo Caioned3138762017-04-19 22:36:39 +0200277 return ret < 0 ? ret : -EINVAL;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200278 return state;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700279}
280
281static int hp_wmi_als_state(void)
282{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400283 int state = 0;
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700284 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, HPWMI_READ, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200285 sizeof(state), sizeof(state));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200286 if (ret)
Carlo Caioned3138762017-04-19 22:36:39 +0200287 return ret < 0 ? ret : -EINVAL;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200288 return state;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700289}
290
291static int hp_wmi_dock_state(void)
292{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400293 int state = 0;
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700294 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, HPWMI_READ, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200295 sizeof(state), sizeof(state));
Matthew Garrett871043b2009-06-01 15:25:45 +0100296
Thomas Renninger6d96e002010-05-21 16:42:40 +0200297 if (ret)
Carlo Caioned3138762017-04-19 22:36:39 +0200298 return ret < 0 ? ret : -EINVAL;
Matthew Garrett871043b2009-06-01 15:25:45 +0100299
Thomas Renninger6d96e002010-05-21 16:42:40 +0200300 return state & 0x1;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700301}
302
Matthew Garrett871043b2009-06-01 15:25:45 +0100303static int hp_wmi_tablet_state(void)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700304{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400305 int state = 0;
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700306 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, HPWMI_READ, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200307 sizeof(state), sizeof(state));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200308 if (ret)
Carlo Caioned3138762017-04-19 22:36:39 +0200309 return ret < 0 ? ret : -EINVAL;
Matthew Garrett871043b2009-06-01 15:25:45 +0100310
Thomas Renninger6d96e002010-05-21 16:42:40 +0200311 return (state & 0x4) ? 1 : 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700312}
313
Kyle Evans8a1513b2015-09-11 10:40:17 -0500314static int __init hp_wmi_bios_2008_later(void)
Alex Hung997daa12013-10-29 14:57:58 +0800315{
316 int state = 0;
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700317 int ret = hp_wmi_perform_query(HPWMI_FEATURE_QUERY, HPWMI_READ, &state,
Alex Hung997daa12013-10-29 14:57:58 +0800318 sizeof(state), sizeof(state));
Kyle Evans8a1513b2015-09-11 10:40:17 -0500319 if (!ret)
320 return 1;
Alex Hung997daa12013-10-29 14:57:58 +0800321
Kyle Evans8a1513b2015-09-11 10:40:17 -0500322 return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
Alex Hung997daa12013-10-29 14:57:58 +0800323}
324
Kyle Evans8a1513b2015-09-11 10:40:17 -0500325static int __init hp_wmi_bios_2009_later(void)
Kyle Evansf82bdd02014-06-09 12:26:06 -0500326{
Kyle Evans8a1513b2015-09-11 10:40:17 -0500327 int state = 0;
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700328 int ret = hp_wmi_perform_query(HPWMI_FEATURE2_QUERY, HPWMI_READ, &state,
Kyle Evans8a1513b2015-09-11 10:40:17 -0500329 sizeof(state), sizeof(state));
330 if (!ret)
331 return 1;
Kyle Evansf82bdd02014-06-09 12:26:06 -0500332
Kyle Evans8a1513b2015-09-11 10:40:17 -0500333 return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
334}
Kyle Evansf82bdd02014-06-09 12:26:06 -0500335
Kyle Evans8a1513b2015-09-11 10:40:17 -0500336static int __init hp_wmi_enable_hotkeys(void)
337{
338 int value = 0x6e;
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700339 int ret = hp_wmi_perform_query(HPWMI_BIOS_QUERY, HPWMI_WRITE, &value,
Kyle Evans8a1513b2015-09-11 10:40:17 -0500340 sizeof(value), 0);
Kyle Evansf82bdd02014-06-09 12:26:06 -0500341 if (ret)
Carlo Caioned3138762017-04-19 22:36:39 +0200342 return ret < 0 ? ret : -EINVAL;
Kyle Evansf82bdd02014-06-09 12:26:06 -0500343 return 0;
344}
345
Johannes Berg19d337d2009-06-02 13:01:37 +0200346static int hp_wmi_set_block(void *data, bool blocked)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700347{
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100348 enum hp_wmi_radio r = (enum hp_wmi_radio) data;
349 int query = BIT(r + 8) | ((!blocked) << r);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200350 int ret;
Johannes Berg19d337d2009-06-02 13:01:37 +0200351
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700352 ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200353 &query, sizeof(query), 0);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200354 if (ret)
Carlo Caioned3138762017-04-19 22:36:39 +0200355 return ret < 0 ? ret : -EINVAL;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200356 return 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700357}
358
Johannes Berg19d337d2009-06-02 13:01:37 +0200359static const struct rfkill_ops hp_wmi_rfkill_ops = {
360 .set_block = hp_wmi_set_block,
361};
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700362
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100363static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700364{
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700365 int mask = 0x200 << (r * 8);
Matthew Garretta8ec1052010-08-23 15:52:34 -0400366 int wireless = 0;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700367
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700368 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_READ,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200369 &wireless, sizeof(wireless),
370 sizeof(wireless));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200371 /* TBD: Pass error */
372
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100373 if (wireless & mask)
Johannes Berg19d337d2009-06-02 13:01:37 +0200374 return false;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700375 else
Johannes Berg19d337d2009-06-02 13:01:37 +0200376 return true;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700377}
378
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100379static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700380{
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700381 int mask = 0x800 << (r * 8);
Matthew Garretta8ec1052010-08-23 15:52:34 -0400382 int wireless = 0;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700383
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700384 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_READ,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200385 &wireless, sizeof(wireless),
386 sizeof(wireless));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200387 /* TBD: Pass error */
388
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100389 if (wireless & mask)
Johannes Berg19d337d2009-06-02 13:01:37 +0200390 return false;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700391 else
Johannes Berg19d337d2009-06-02 13:01:37 +0200392 return true;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700393}
394
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200395static int hp_wmi_rfkill2_set_block(void *data, bool blocked)
396{
397 int rfkill_id = (int)(long)data;
398 char buffer[4] = { 0x01, 0x00, rfkill_id, !blocked };
399
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700400 if (hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_WRITE,
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200401 buffer, sizeof(buffer), 0))
402 return -EINVAL;
403 return 0;
404}
405
406static const struct rfkill_ops hp_wmi_rfkill2_ops = {
407 .set_block = hp_wmi_rfkill2_set_block,
408};
409
410static int hp_wmi_rfkill2_refresh(void)
411{
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200412 struct bios_rfkill2_state state;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700413 int err, i;
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200414
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700415 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200416 0, sizeof(state));
417 if (err)
418 return err;
419
420 for (i = 0; i < rfkill2_count; i++) {
421 int num = rfkill2[i].num;
422 struct bios_rfkill2_device_state *devstate;
423 devstate = &state.device[num];
424
425 if (num >= state.count ||
426 devstate->rfkill_id != rfkill2[i].id) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700427 pr_warn("power configuration of the wireless devices unexpectedly changed\n");
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200428 continue;
429 }
430
431 rfkill_set_states(rfkill2[i].rfkill,
432 IS_SWBLOCKED(devstate->power),
433 IS_HWBLOCKED(devstate->power));
434 }
435
436 return 0;
437}
438
Alex Hung8667ca92013-06-13 16:46:25 +0800439static int hp_wmi_post_code_state(void)
440{
441 int state = 0;
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700442 int ret = hp_wmi_perform_query(HPWMI_POSTCODEERROR_QUERY, HPWMI_READ, &state,
Alex Hung8667ca92013-06-13 16:46:25 +0800443 sizeof(state), sizeof(state));
444 if (ret)
Carlo Caioned3138762017-04-19 22:36:39 +0200445 return ret < 0 ? ret : -EINVAL;
Alex Hung8667ca92013-06-13 16:46:25 +0800446 return state;
447}
448
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700449static ssize_t show_display(struct device *dev, struct device_attribute *attr,
450 char *buf)
451{
452 int value = hp_wmi_display_state();
453 if (value < 0)
454 return -EINVAL;
455 return sprintf(buf, "%d\n", value);
456}
457
458static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
459 char *buf)
460{
461 int value = hp_wmi_hddtemp_state();
462 if (value < 0)
463 return -EINVAL;
464 return sprintf(buf, "%d\n", value);
465}
466
467static ssize_t show_als(struct device *dev, struct device_attribute *attr,
468 char *buf)
469{
470 int value = hp_wmi_als_state();
471 if (value < 0)
472 return -EINVAL;
473 return sprintf(buf, "%d\n", value);
474}
475
476static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
477 char *buf)
478{
479 int value = hp_wmi_dock_state();
480 if (value < 0)
481 return -EINVAL;
482 return sprintf(buf, "%d\n", value);
483}
484
Matthew Garrett871043b2009-06-01 15:25:45 +0100485static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
486 char *buf)
487{
488 int value = hp_wmi_tablet_state();
489 if (value < 0)
490 return -EINVAL;
491 return sprintf(buf, "%d\n", value);
492}
493
Alex Hung8667ca92013-06-13 16:46:25 +0800494static ssize_t show_postcode(struct device *dev, struct device_attribute *attr,
495 char *buf)
496{
497 /* Get the POST error code of previous boot failure. */
498 int value = hp_wmi_post_code_state();
499 if (value < 0)
500 return -EINVAL;
501 return sprintf(buf, "0x%x\n", value);
502}
503
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700504static ssize_t set_als(struct device *dev, struct device_attribute *attr,
505 const char *buf, size_t count)
506{
507 u32 tmp = simple_strtoul(buf, NULL, 10);
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700508 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, HPWMI_WRITE, &tmp,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200509 sizeof(tmp), sizeof(tmp));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200510 if (ret)
Carlo Caioned3138762017-04-19 22:36:39 +0200511 return ret < 0 ? ret : -EINVAL;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200512
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700513 return count;
514}
515
Alex Hung8667ca92013-06-13 16:46:25 +0800516static ssize_t set_postcode(struct device *dev, struct device_attribute *attr,
517 const char *buf, size_t count)
518{
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700519 long unsigned int tmp2;
Alex Hung8667ca92013-06-13 16:46:25 +0800520 int ret;
521 u32 tmp;
Alex Hung8667ca92013-06-13 16:46:25 +0800522
523 ret = kstrtoul(buf, 10, &tmp2);
524 if (ret || tmp2 != 1)
525 return -EINVAL;
526
527 /* Clear the POST error code. It is kept until until cleared. */
528 tmp = (u32) tmp2;
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700529 ret = hp_wmi_perform_query(HPWMI_POSTCODEERROR_QUERY, HPWMI_WRITE, &tmp,
Alex Hung8667ca92013-06-13 16:46:25 +0800530 sizeof(tmp), sizeof(tmp));
531 if (ret)
Carlo Caioned3138762017-04-19 22:36:39 +0200532 return ret < 0 ? ret : -EINVAL;
Alex Hung8667ca92013-06-13 16:46:25 +0800533
534 return count;
535}
536
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700537static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
538static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
539static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
540static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
Matthew Garrett871043b2009-06-01 15:25:45 +0100541static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
Alex Hung8667ca92013-06-13 16:46:25 +0800542static DEVICE_ATTR(postcode, S_IRUGO | S_IWUSR, show_postcode, set_postcode);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700543
Adrian Bunk88429a12008-10-15 22:05:17 -0700544static void hp_wmi_notify(u32 value, void *context)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700545{
546 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200547 u32 event_id, event_data;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700548 union acpi_object *obj;
Matthew Garretta8ec1052010-08-23 15:52:34 -0400549 int key_code = 0, ret;
Len Brownfda11e62009-12-26 23:02:24 -0500550 acpi_status status;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700551 u32 *location;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700552
Len Brownfda11e62009-12-26 23:02:24 -0500553 status = wmi_get_event_data(value, &response);
554 if (status != AE_OK) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700555 pr_info("bad event status 0x%x\n", status);
Len Brownfda11e62009-12-26 23:02:24 -0500556 return;
557 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700558
559 obj = (union acpi_object *)response.pointer;
560
Thomas Renningerc4775062010-07-29 12:27:59 +0200561 if (!obj)
562 return;
563 if (obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700564 pr_info("Unknown response received %d\n", obj->type);
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100565 kfree(obj);
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100566 return;
567 }
568
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200569 /*
570 * Depending on ACPI version the concatenation of id and event data
571 * inside _WED function will result in a 8 or 16 byte buffer.
572 */
573 location = (u32 *)obj->buffer.pointer;
574 if (obj->buffer.length == 8) {
575 event_id = *location;
576 event_data = *(location + 1);
577 } else if (obj->buffer.length == 16) {
578 event_id = *location;
579 event_data = *(location + 2);
580 } else {
Joe Perchesb5a42232011-03-29 15:21:41 -0700581 pr_info("Unknown buffer length %d\n", obj->buffer.length);
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200582 kfree(obj);
583 return;
584 }
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100585 kfree(obj);
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200586
587 switch (event_id) {
Thomas Renninger751ae802010-05-21 16:18:09 +0200588 case HPWMI_DOCK_EVENT:
Carlo Caione298747b2017-04-09 15:56:08 +0200589 if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
590 input_report_switch(hp_wmi_input_dev, SW_DOCK,
591 hp_wmi_dock_state());
592 if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
593 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
594 hp_wmi_tablet_state());
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100595 input_sync(hp_wmi_input_dev);
Thomas Renninger751ae802010-05-21 16:18:09 +0200596 break;
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200597 case HPWMI_PARK_HDD:
598 break;
599 case HPWMI_SMART_ADAPTER:
600 break;
Thomas Renninger751ae802010-05-21 16:18:09 +0200601 case HPWMI_BEZEL_BUTTON:
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700602 ret = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, HPWMI_READ,
Matthew Garretta8ec1052010-08-23 15:52:34 -0400603 &key_code,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200604 sizeof(key_code),
Thomas Renninger6d96e002010-05-21 16:42:40 +0200605 sizeof(key_code));
606 if (ret)
607 break;
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700608
609 if (!sparse_keymap_report_event(hp_wmi_input_dev,
610 key_code, 1, true))
Joe Perchesb5a42232011-03-29 15:21:41 -0700611 pr_info("Unknown key code - 0x%x\n", key_code);
Thomas Renninger751ae802010-05-21 16:18:09 +0200612 break;
613 case HPWMI_WIRELESS:
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200614 if (rfkill2_count) {
615 hp_wmi_rfkill2_refresh();
616 break;
617 }
618
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100619 if (wifi_rfkill)
620 rfkill_set_states(wifi_rfkill,
621 hp_wmi_get_sw_state(HPWMI_WIFI),
622 hp_wmi_get_hw_state(HPWMI_WIFI));
623 if (bluetooth_rfkill)
624 rfkill_set_states(bluetooth_rfkill,
625 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
626 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
627 if (wwan_rfkill)
628 rfkill_set_states(wwan_rfkill,
629 hp_wmi_get_sw_state(HPWMI_WWAN),
630 hp_wmi_get_hw_state(HPWMI_WWAN));
Thomas Renninger751ae802010-05-21 16:18:09 +0200631 break;
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200632 case HPWMI_CPU_BATTERY_THROTTLE:
Joe Perchesb5a42232011-03-29 15:21:41 -0700633 pr_info("Unimplemented CPU throttle because of 3 Cell battery event detected\n");
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200634 break;
635 case HPWMI_LOCK_SWITCH:
636 break;
Alex Hungd9e290a2013-03-15 17:18:22 +0800637 case HPWMI_LID_SWITCH:
638 break;
639 case HPWMI_SCREEN_ROTATION:
640 break;
641 case HPWMI_COOLSENSE_SYSTEM_MOBILE:
642 break;
643 case HPWMI_COOLSENSE_SYSTEM_HOT:
644 break;
645 case HPWMI_PROXIMITY_SENSOR:
646 break;
647 case HPWMI_BACKLIT_KB_BRIGHTNESS:
648 break;
649 case HPWMI_PEAKSHIFT_PERIOD:
650 break;
651 case HPWMI_BATTERY_CHARGE_PERIOD:
652 break;
Thomas Renninger751ae802010-05-21 16:18:09 +0200653 default:
Joe Perchesb5a42232011-03-29 15:21:41 -0700654 pr_info("Unknown event_id - %d - 0x%x\n", event_id, event_data);
Thomas Renninger751ae802010-05-21 16:18:09 +0200655 break;
656 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700657}
658
659static int __init hp_wmi_input_setup(void)
660{
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700661 acpi_status status;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700662 int err, val;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700663
664 hp_wmi_input_dev = input_allocate_device();
Axel Linbc285962010-07-20 15:19:51 -0700665 if (!hp_wmi_input_dev)
666 return -ENOMEM;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700667
668 hp_wmi_input_dev->name = "HP WMI hotkeys";
669 hp_wmi_input_dev->phys = "wmi/input0";
670 hp_wmi_input_dev->id.bustype = BUS_HOST;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700671
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700672 __set_bit(EV_SW, hp_wmi_input_dev->evbit);
Carlo Caione298747b2017-04-09 15:56:08 +0200673
674 /* Dock */
675 val = hp_wmi_dock_state();
676 if (!(val < 0)) {
677 __set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
678 input_report_switch(hp_wmi_input_dev, SW_DOCK, val);
679 }
680
681 /* Tablet mode */
682 val = hp_wmi_tablet_state();
683 if (!(val < 0)) {
684 __set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
685 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE, val);
686 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700687
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700688 err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
689 if (err)
690 goto err_free_dev;
Matthew Garrett871043b2009-06-01 15:25:45 +0100691
692 /* Set initial hardware state */
Matthew Garrett871043b2009-06-01 15:25:45 +0100693 input_sync(hp_wmi_input_dev);
694
Kyle Evans8a1513b2015-09-11 10:40:17 -0500695 if (!hp_wmi_bios_2009_later() && hp_wmi_bios_2008_later())
Kyle Evansf82bdd02014-06-09 12:26:06 -0500696 hp_wmi_enable_hotkeys();
697
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700698 status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
699 if (ACPI_FAILURE(status)) {
700 err = -EIO;
Michał Kępień3a3c1112017-03-09 13:11:43 +0100701 goto err_free_dev;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700702 }
703
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700704 err = input_register_device(hp_wmi_input_dev);
705 if (err)
706 goto err_uninstall_notifier;
707
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700708 return 0;
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700709
710 err_uninstall_notifier:
711 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700712 err_free_dev:
713 input_free_device(hp_wmi_input_dev);
714 return err;
715}
716
717static void hp_wmi_input_destroy(void)
718{
719 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700720 input_unregister_device(hp_wmi_input_dev);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700721}
722
723static void cleanup_sysfs(struct platform_device *device)
724{
725 device_remove_file(&device->dev, &dev_attr_display);
726 device_remove_file(&device->dev, &dev_attr_hddtemp);
727 device_remove_file(&device->dev, &dev_attr_als);
728 device_remove_file(&device->dev, &dev_attr_dock);
Matthew Garrett871043b2009-06-01 15:25:45 +0100729 device_remove_file(&device->dev, &dev_attr_tablet);
Alex Hung8667ca92013-06-13 16:46:25 +0800730 device_remove_file(&device->dev, &dev_attr_postcode);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700731}
732
Mathias Krause702ef542014-07-16 19:43:13 +0200733static int __init hp_wmi_rfkill_setup(struct platform_device *device)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700734{
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700735 int err, wireless = 0;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200736
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700737 err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_READ, &wireless,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200738 sizeof(wireless), sizeof(wireless));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200739 if (err)
740 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700741
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700742 err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE, &wireless,
Alex Hungfc8a6012016-06-13 19:44:00 +0800743 sizeof(wireless), 0);
744 if (err)
745 return err;
746
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700747 if (wireless & 0x1) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200748 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
749 RFKILL_TYPE_WLAN,
750 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100751 (void *) HPWMI_WIFI);
Dan Carpenterdd258c02012-05-17 09:48:12 +0300752 if (!wifi_rfkill)
753 return -ENOMEM;
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100754 rfkill_init_sw_state(wifi_rfkill,
755 hp_wmi_get_sw_state(HPWMI_WIFI));
756 rfkill_set_hw_state(wifi_rfkill,
757 hp_wmi_get_hw_state(HPWMI_WIFI));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800758 err = rfkill_register(wifi_rfkill);
759 if (err)
Johannes Berg19d337d2009-06-02 13:01:37 +0200760 goto register_wifi_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700761 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700762
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700763 if (wireless & 0x2) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200764 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
765 RFKILL_TYPE_BLUETOOTH,
766 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100767 (void *) HPWMI_BLUETOOTH);
Dan Carpenterdd258c02012-05-17 09:48:12 +0300768 if (!bluetooth_rfkill) {
769 err = -ENOMEM;
Maciej S. Szmigieroc7805e52016-03-06 23:38:36 +0100770 goto register_bluetooth_error;
Dan Carpenterdd258c02012-05-17 09:48:12 +0300771 }
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100772 rfkill_init_sw_state(bluetooth_rfkill,
773 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
774 rfkill_set_hw_state(bluetooth_rfkill,
775 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800776 err = rfkill_register(bluetooth_rfkill);
Frans Pop6989d562009-01-29 14:25:14 -0800777 if (err)
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800778 goto register_bluetooth_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700779 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700780
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700781 if (wireless & 0x4) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200782 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
783 RFKILL_TYPE_WWAN,
784 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100785 (void *) HPWMI_WWAN);
Dan Carpenterdd258c02012-05-17 09:48:12 +0300786 if (!wwan_rfkill) {
787 err = -ENOMEM;
Maciej S. Szmigieroc7805e52016-03-06 23:38:36 +0100788 goto register_wwan_error;
Dan Carpenterdd258c02012-05-17 09:48:12 +0300789 }
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100790 rfkill_init_sw_state(wwan_rfkill,
791 hp_wmi_get_sw_state(HPWMI_WWAN));
792 rfkill_set_hw_state(wwan_rfkill,
793 hp_wmi_get_hw_state(HPWMI_WWAN));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800794 err = rfkill_register(wwan_rfkill);
795 if (err)
Kirill Tkhai5b5c2b32013-07-29 17:34:37 +0400796 goto register_wwan_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700797 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700798
799 return 0;
Maciej S. Szmigierofffcad82016-03-06 23:40:19 +0100800
Kirill Tkhai5b5c2b32013-07-29 17:34:37 +0400801register_wwan_error:
802 rfkill_destroy(wwan_rfkill);
803 wwan_rfkill = NULL;
Maciej S. Szmigieroc7805e52016-03-06 23:38:36 +0100804 if (bluetooth_rfkill)
805 rfkill_unregister(bluetooth_rfkill);
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800806register_bluetooth_error:
Johannes Berg19d337d2009-06-02 13:01:37 +0200807 rfkill_destroy(bluetooth_rfkill);
Anssi Hannula6d97db52011-02-20 20:07:24 +0200808 bluetooth_rfkill = NULL;
Andrew Morton44f06062009-02-04 15:12:07 -0800809 if (wifi_rfkill)
810 rfkill_unregister(wifi_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200811register_wifi_error:
812 rfkill_destroy(wifi_rfkill);
Anssi Hannula6d97db52011-02-20 20:07:24 +0200813 wifi_rfkill = NULL;
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200814 return err;
815}
816
Mathias Krause702ef542014-07-16 19:43:13 +0200817static int __init hp_wmi_rfkill2_setup(struct platform_device *device)
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200818{
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200819 struct bios_rfkill2_state state;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700820 int err, i;
821
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700822 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200823 0, sizeof(state));
824 if (err)
825 return err;
826
827 if (state.count > HPWMI_MAX_RFKILL2_DEVICES) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700828 pr_warn("unable to parse 0x1b query output\n");
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200829 return -EINVAL;
830 }
831
832 for (i = 0; i < state.count; i++) {
833 struct rfkill *rfkill;
834 enum rfkill_type type;
835 char *name;
836 switch (state.device[i].radio_type) {
837 case HPWMI_WIFI:
838 type = RFKILL_TYPE_WLAN;
839 name = "hp-wifi";
840 break;
841 case HPWMI_BLUETOOTH:
842 type = RFKILL_TYPE_BLUETOOTH;
843 name = "hp-bluetooth";
844 break;
845 case HPWMI_WWAN:
846 type = RFKILL_TYPE_WWAN;
847 name = "hp-wwan";
848 break;
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200849 case HPWMI_GPS:
850 type = RFKILL_TYPE_GPS;
851 name = "hp-gps";
852 break;
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200853 default:
Joe Perchesb5a42232011-03-29 15:21:41 -0700854 pr_warn("unknown device type 0x%x\n",
855 state.device[i].radio_type);
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200856 continue;
857 }
858
859 if (!state.device[i].vendor_id) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700860 pr_warn("zero device %d while %d reported\n",
861 i, state.count);
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200862 continue;
863 }
864
865 rfkill = rfkill_alloc(name, &device->dev, type,
866 &hp_wmi_rfkill2_ops, (void *)(long)i);
867 if (!rfkill) {
868 err = -ENOMEM;
869 goto fail;
870 }
871
872 rfkill2[rfkill2_count].id = state.device[i].rfkill_id;
873 rfkill2[rfkill2_count].num = i;
874 rfkill2[rfkill2_count].rfkill = rfkill;
875
876 rfkill_init_sw_state(rfkill,
877 IS_SWBLOCKED(state.device[i].power));
878 rfkill_set_hw_state(rfkill,
879 IS_HWBLOCKED(state.device[i].power));
880
881 if (!(state.device[i].power & HPWMI_POWER_BIOS))
Joe Perchesb5a42232011-03-29 15:21:41 -0700882 pr_info("device %s blocked by BIOS\n", name);
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200883
884 err = rfkill_register(rfkill);
885 if (err) {
886 rfkill_destroy(rfkill);
887 goto fail;
888 }
889
890 rfkill2_count++;
891 }
892
893 return 0;
894fail:
895 for (; rfkill2_count > 0; rfkill2_count--) {
896 rfkill_unregister(rfkill2[rfkill2_count - 1].rfkill);
897 rfkill_destroy(rfkill2[rfkill2_count - 1].rfkill);
898 }
899 return err;
900}
901
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800902static int __init hp_wmi_bios_setup(struct platform_device *device)
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200903{
904 int err;
905
Anssi Hannula6d97db52011-02-20 20:07:24 +0200906 /* clear detected rfkill devices */
907 wifi_rfkill = NULL;
908 bluetooth_rfkill = NULL;
909 wwan_rfkill = NULL;
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200910 rfkill2_count = 0;
Anssi Hannula6d97db52011-02-20 20:07:24 +0200911
Alex Hungfc8a6012016-06-13 19:44:00 +0800912 if (hp_wmi_rfkill_setup(device))
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200913 hp_wmi_rfkill2_setup(device);
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200914
915 err = device_create_file(&device->dev, &dev_attr_display);
916 if (err)
917 goto add_sysfs_error;
918 err = device_create_file(&device->dev, &dev_attr_hddtemp);
919 if (err)
920 goto add_sysfs_error;
921 err = device_create_file(&device->dev, &dev_attr_als);
922 if (err)
923 goto add_sysfs_error;
924 err = device_create_file(&device->dev, &dev_attr_dock);
925 if (err)
926 goto add_sysfs_error;
927 err = device_create_file(&device->dev, &dev_attr_tablet);
928 if (err)
929 goto add_sysfs_error;
Alex Hung8667ca92013-06-13 16:46:25 +0800930 err = device_create_file(&device->dev, &dev_attr_postcode);
931 if (err)
932 goto add_sysfs_error;
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200933 return 0;
934
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700935add_sysfs_error:
936 cleanup_sysfs(device);
937 return err;
938}
939
940static int __exit hp_wmi_bios_remove(struct platform_device *device)
941{
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200942 int i;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700943 cleanup_sysfs(device);
944
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200945 for (i = 0; i < rfkill2_count; i++) {
946 rfkill_unregister(rfkill2[i].rfkill);
947 rfkill_destroy(rfkill2[i].rfkill);
948 }
949
Johannes Berg19d337d2009-06-02 13:01:37 +0200950 if (wifi_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700951 rfkill_unregister(wifi_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200952 rfkill_destroy(wifi_rfkill);
953 }
954 if (bluetooth_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700955 rfkill_unregister(bluetooth_rfkill);
Corentin Chary09729f02009-09-14 12:43:51 +0200956 rfkill_destroy(bluetooth_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200957 }
958 if (wwan_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700959 rfkill_unregister(wwan_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200960 rfkill_destroy(wwan_rfkill);
961 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700962
963 return 0;
964}
965
Frans Pop8dd2b422009-08-20 20:38:13 +0200966static int hp_wmi_resume_handler(struct device *device)
Frans Pop4c395bd2009-03-04 11:55:28 -0800967{
Frans Pop4c395bd2009-03-04 11:55:28 -0800968 /*
Matthew Garrett871043b2009-06-01 15:25:45 +0100969 * Hardware state may have changed while suspended, so trigger
970 * input events for the current state. As this is a switch,
Frans Pop4c395bd2009-03-04 11:55:28 -0800971 * the input layer will only actually pass it on if the state
972 * changed.
973 */
Frans Popdaed9532009-07-30 17:16:05 -0400974 if (hp_wmi_input_dev) {
Carlo Caione298747b2017-04-09 15:56:08 +0200975 if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
976 input_report_switch(hp_wmi_input_dev, SW_DOCK,
977 hp_wmi_dock_state());
978 if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
979 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
980 hp_wmi_tablet_state());
Frans Popdaed9532009-07-30 17:16:05 -0400981 input_sync(hp_wmi_input_dev);
982 }
Frans Pop4c395bd2009-03-04 11:55:28 -0800983
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200984 if (rfkill2_count)
985 hp_wmi_rfkill2_refresh();
986
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100987 if (wifi_rfkill)
988 rfkill_set_states(wifi_rfkill,
989 hp_wmi_get_sw_state(HPWMI_WIFI),
990 hp_wmi_get_hw_state(HPWMI_WIFI));
991 if (bluetooth_rfkill)
992 rfkill_set_states(bluetooth_rfkill,
993 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
994 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
995 if (wwan_rfkill)
996 rfkill_set_states(wwan_rfkill,
997 hp_wmi_get_sw_state(HPWMI_WWAN),
998 hp_wmi_get_hw_state(HPWMI_WWAN));
999
Frans Pop4c395bd2009-03-04 11:55:28 -08001000 return 0;
1001}
1002
Dmitry Torokhovc165b802013-02-20 00:44:34 -08001003static const struct dev_pm_ops hp_wmi_pm_ops = {
1004 .resume = hp_wmi_resume_handler,
1005 .restore = hp_wmi_resume_handler,
1006};
1007
1008static struct platform_driver hp_wmi_driver = {
1009 .driver = {
1010 .name = "hp-wmi",
Dmitry Torokhovc165b802013-02-20 00:44:34 -08001011 .pm = &hp_wmi_pm_ops,
1012 },
1013 .remove = __exit_p(hp_wmi_bios_remove),
1014};
1015
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001016static int __init hp_wmi_init(void)
1017{
Thomas Renningerb0966672010-07-20 15:19:29 -07001018 int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
1019 int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -07001020 int err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001021
Dmitry Torokhovc165b802013-02-20 00:44:34 -08001022 if (!bios_capable && !event_capable)
1023 return -ENODEV;
1024
Thomas Renningerb0966672010-07-20 15:19:29 -07001025 if (event_capable) {
Axel Lindfec5c42010-06-10 16:06:40 +08001026 err = hp_wmi_input_setup();
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -07001027 if (err)
Axel Lindfec5c42010-06-10 16:06:40 +08001028 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001029 }
1030
Thomas Renningerb0966672010-07-20 15:19:29 -07001031 if (bios_capable) {
Dmitry Torokhovc165b802013-02-20 00:44:34 -08001032 hp_wmi_platform_dev =
1033 platform_device_register_simple("hp-wmi", -1, NULL, 0);
1034 if (IS_ERR(hp_wmi_platform_dev)) {
1035 err = PTR_ERR(hp_wmi_platform_dev);
1036 goto err_destroy_input;
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001037 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001038
Dmitry Torokhovc165b802013-02-20 00:44:34 -08001039 err = platform_driver_probe(&hp_wmi_driver, hp_wmi_bios_setup);
1040 if (err)
1041 goto err_unregister_device;
1042 }
Thomas Renningerb0966672010-07-20 15:19:29 -07001043
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001044 return 0;
Axel Lindfec5c42010-06-10 16:06:40 +08001045
Dmitry Torokhovc165b802013-02-20 00:44:34 -08001046err_unregister_device:
1047 platform_device_unregister(hp_wmi_platform_dev);
1048err_destroy_input:
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -07001049 if (event_capable)
1050 hp_wmi_input_destroy();
Axel Lindfec5c42010-06-10 16:06:40 +08001051
1052 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001053}
Dmitry Torokhovc165b802013-02-20 00:44:34 -08001054module_init(hp_wmi_init);
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001055
1056static void __exit hp_wmi_exit(void)
1057{
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -07001058 if (wmi_has_guid(HPWMI_EVENT_GUID))
1059 hp_wmi_input_destroy();
1060
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001061 if (hp_wmi_platform_dev) {
Axel Lin97ba0af2010-06-03 15:18:03 +08001062 platform_device_unregister(hp_wmi_platform_dev);
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001063 platform_driver_unregister(&hp_wmi_driver);
1064 }
1065}
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001066module_exit(hp_wmi_exit);