blob: 577805987d355dc7c4ba6d32b3279e2393c53822 [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
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700261static int hp_wmi_read_int(int query)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700262{
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700263 int val = 0, ret;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700264
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700265 ret = hp_wmi_perform_query(query, HPWMI_READ, &val,
266 sizeof(val), sizeof(val));
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700267
Thomas Renninger6d96e002010-05-21 16:42:40 +0200268 if (ret)
Carlo Caioned3138762017-04-19 22:36:39 +0200269 return ret < 0 ? ret : -EINVAL;
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700270
271 return val;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700272}
273
274static int hp_wmi_dock_state(void)
275{
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700276 int state = hp_wmi_read_int(HPWMI_HARDWARE_QUERY);
Matthew Garrett871043b2009-06-01 15:25:45 +0100277
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700278 if (state < 0)
279 return state;
Matthew Garrett871043b2009-06-01 15:25:45 +0100280
Thomas Renninger6d96e002010-05-21 16:42:40 +0200281 return state & 0x1;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700282}
283
Matthew Garrett871043b2009-06-01 15:25:45 +0100284static int hp_wmi_tablet_state(void)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700285{
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700286 int state = hp_wmi_read_int(HPWMI_HARDWARE_QUERY);
287
288 if (state < 0)
289 return state;
Matthew Garrett871043b2009-06-01 15:25:45 +0100290
Thomas Renninger6d96e002010-05-21 16:42:40 +0200291 return (state & 0x4) ? 1 : 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700292}
293
Kyle Evans8a1513b2015-09-11 10:40:17 -0500294static int __init hp_wmi_bios_2008_later(void)
Alex Hung997daa12013-10-29 14:57:58 +0800295{
296 int state = 0;
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700297 int ret = hp_wmi_perform_query(HPWMI_FEATURE_QUERY, HPWMI_READ, &state,
Alex Hung997daa12013-10-29 14:57:58 +0800298 sizeof(state), sizeof(state));
Kyle Evans8a1513b2015-09-11 10:40:17 -0500299 if (!ret)
300 return 1;
Alex Hung997daa12013-10-29 14:57:58 +0800301
Kyle Evans8a1513b2015-09-11 10:40:17 -0500302 return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
Alex Hung997daa12013-10-29 14:57:58 +0800303}
304
Kyle Evans8a1513b2015-09-11 10:40:17 -0500305static int __init hp_wmi_bios_2009_later(void)
Kyle Evansf82bdd02014-06-09 12:26:06 -0500306{
Kyle Evans8a1513b2015-09-11 10:40:17 -0500307 int state = 0;
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700308 int ret = hp_wmi_perform_query(HPWMI_FEATURE2_QUERY, HPWMI_READ, &state,
Kyle Evans8a1513b2015-09-11 10:40:17 -0500309 sizeof(state), sizeof(state));
310 if (!ret)
311 return 1;
Kyle Evansf82bdd02014-06-09 12:26:06 -0500312
Kyle Evans8a1513b2015-09-11 10:40:17 -0500313 return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
314}
Kyle Evansf82bdd02014-06-09 12:26:06 -0500315
Kyle Evans8a1513b2015-09-11 10:40:17 -0500316static int __init hp_wmi_enable_hotkeys(void)
317{
318 int value = 0x6e;
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700319 int ret = hp_wmi_perform_query(HPWMI_BIOS_QUERY, HPWMI_WRITE, &value,
Kyle Evans8a1513b2015-09-11 10:40:17 -0500320 sizeof(value), 0);
Kyle Evansf82bdd02014-06-09 12:26:06 -0500321 if (ret)
Carlo Caioned3138762017-04-19 22:36:39 +0200322 return ret < 0 ? ret : -EINVAL;
Kyle Evansf82bdd02014-06-09 12:26:06 -0500323 return 0;
324}
325
Johannes Berg19d337d2009-06-02 13:01:37 +0200326static int hp_wmi_set_block(void *data, bool blocked)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700327{
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100328 enum hp_wmi_radio r = (enum hp_wmi_radio) data;
329 int query = BIT(r + 8) | ((!blocked) << r);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200330 int ret;
Johannes Berg19d337d2009-06-02 13:01:37 +0200331
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700332 ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200333 &query, sizeof(query), 0);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200334 if (ret)
Carlo Caioned3138762017-04-19 22:36:39 +0200335 return ret < 0 ? ret : -EINVAL;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200336 return 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700337}
338
Johannes Berg19d337d2009-06-02 13:01:37 +0200339static const struct rfkill_ops hp_wmi_rfkill_ops = {
340 .set_block = hp_wmi_set_block,
341};
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700342
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100343static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700344{
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700345 int mask = 0x200 << (r * 8);
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700346
Darren Hart (VMware)c7ef1442017-04-19 18:00:01 -0700347 int wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
348
Thomas Renninger6d96e002010-05-21 16:42:40 +0200349 /* TBD: Pass error */
Darren Hart (VMware)c7ef1442017-04-19 18:00:01 -0700350 WARN_ONCE(wireless < 0, "error executing HPWMI_WIRELESS_QUERY");
Thomas Renninger6d96e002010-05-21 16:42:40 +0200351
Darren Hart (VMware)c7ef1442017-04-19 18:00:01 -0700352 return !(wireless & mask);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700353}
354
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100355static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700356{
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700357 int mask = 0x800 << (r * 8);
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700358
Darren Hart (VMware)c7ef1442017-04-19 18:00:01 -0700359 int wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
360
Thomas Renninger6d96e002010-05-21 16:42:40 +0200361 /* TBD: Pass error */
Darren Hart (VMware)c7ef1442017-04-19 18:00:01 -0700362 WARN_ONCE(wireless < 0, "error executing HPWMI_WIRELESS_QUERY");
Thomas Renninger6d96e002010-05-21 16:42:40 +0200363
Darren Hart (VMware)c7ef1442017-04-19 18:00:01 -0700364 return !(wireless & mask);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700365}
366
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200367static int hp_wmi_rfkill2_set_block(void *data, bool blocked)
368{
369 int rfkill_id = (int)(long)data;
370 char buffer[4] = { 0x01, 0x00, rfkill_id, !blocked };
371
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700372 if (hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_WRITE,
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200373 buffer, sizeof(buffer), 0))
374 return -EINVAL;
375 return 0;
376}
377
378static const struct rfkill_ops hp_wmi_rfkill2_ops = {
379 .set_block = hp_wmi_rfkill2_set_block,
380};
381
382static int hp_wmi_rfkill2_refresh(void)
383{
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200384 struct bios_rfkill2_state state;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700385 int err, i;
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200386
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700387 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200388 0, sizeof(state));
389 if (err)
390 return err;
391
392 for (i = 0; i < rfkill2_count; i++) {
393 int num = rfkill2[i].num;
394 struct bios_rfkill2_device_state *devstate;
395 devstate = &state.device[num];
396
397 if (num >= state.count ||
398 devstate->rfkill_id != rfkill2[i].id) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700399 pr_warn("power configuration of the wireless devices unexpectedly changed\n");
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200400 continue;
401 }
402
403 rfkill_set_states(rfkill2[i].rfkill,
404 IS_SWBLOCKED(devstate->power),
405 IS_HWBLOCKED(devstate->power));
406 }
407
408 return 0;
409}
410
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700411static ssize_t show_display(struct device *dev, struct device_attribute *attr,
412 char *buf)
413{
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700414 int value = hp_wmi_read_int(HPWMI_DISPLAY_QUERY);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700415 if (value < 0)
416 return -EINVAL;
417 return sprintf(buf, "%d\n", value);
418}
419
420static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
421 char *buf)
422{
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700423 int value = hp_wmi_read_int(HPWMI_HDDTEMP_QUERY);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700424 if (value < 0)
425 return -EINVAL;
426 return sprintf(buf, "%d\n", value);
427}
428
429static ssize_t show_als(struct device *dev, struct device_attribute *attr,
430 char *buf)
431{
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700432 int value = hp_wmi_read_int(HPWMI_ALS_QUERY);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700433 if (value < 0)
434 return -EINVAL;
435 return sprintf(buf, "%d\n", value);
436}
437
438static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
439 char *buf)
440{
441 int value = hp_wmi_dock_state();
442 if (value < 0)
443 return -EINVAL;
444 return sprintf(buf, "%d\n", value);
445}
446
Matthew Garrett871043b2009-06-01 15:25:45 +0100447static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
448 char *buf)
449{
450 int value = hp_wmi_tablet_state();
451 if (value < 0)
452 return -EINVAL;
453 return sprintf(buf, "%d\n", value);
454}
455
Alex Hung8667ca92013-06-13 16:46:25 +0800456static ssize_t show_postcode(struct device *dev, struct device_attribute *attr,
457 char *buf)
458{
459 /* Get the POST error code of previous boot failure. */
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700460 int value = hp_wmi_read_int(HPWMI_POSTCODEERROR_QUERY);
Alex Hung8667ca92013-06-13 16:46:25 +0800461 if (value < 0)
462 return -EINVAL;
463 return sprintf(buf, "0x%x\n", value);
464}
465
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700466static ssize_t set_als(struct device *dev, struct device_attribute *attr,
467 const char *buf, size_t count)
468{
469 u32 tmp = simple_strtoul(buf, NULL, 10);
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700470 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, HPWMI_WRITE, &tmp,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200471 sizeof(tmp), sizeof(tmp));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200472 if (ret)
Carlo Caioned3138762017-04-19 22:36:39 +0200473 return ret < 0 ? ret : -EINVAL;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200474
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700475 return count;
476}
477
Alex Hung8667ca92013-06-13 16:46:25 +0800478static ssize_t set_postcode(struct device *dev, struct device_attribute *attr,
479 const char *buf, size_t count)
480{
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700481 long unsigned int tmp2;
Alex Hung8667ca92013-06-13 16:46:25 +0800482 int ret;
483 u32 tmp;
Alex Hung8667ca92013-06-13 16:46:25 +0800484
485 ret = kstrtoul(buf, 10, &tmp2);
486 if (ret || tmp2 != 1)
487 return -EINVAL;
488
489 /* Clear the POST error code. It is kept until until cleared. */
490 tmp = (u32) tmp2;
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700491 ret = hp_wmi_perform_query(HPWMI_POSTCODEERROR_QUERY, HPWMI_WRITE, &tmp,
Alex Hung8667ca92013-06-13 16:46:25 +0800492 sizeof(tmp), sizeof(tmp));
493 if (ret)
Carlo Caioned3138762017-04-19 22:36:39 +0200494 return ret < 0 ? ret : -EINVAL;
Alex Hung8667ca92013-06-13 16:46:25 +0800495
496 return count;
497}
498
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700499static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
500static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
501static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
502static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
Matthew Garrett871043b2009-06-01 15:25:45 +0100503static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
Alex Hung8667ca92013-06-13 16:46:25 +0800504static DEVICE_ATTR(postcode, S_IRUGO | S_IWUSR, show_postcode, set_postcode);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700505
Adrian Bunk88429a12008-10-15 22:05:17 -0700506static void hp_wmi_notify(u32 value, void *context)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700507{
508 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200509 u32 event_id, event_data;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700510 union acpi_object *obj;
Len Brownfda11e62009-12-26 23:02:24 -0500511 acpi_status status;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700512 u32 *location;
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700513 int key_code;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700514
Len Brownfda11e62009-12-26 23:02:24 -0500515 status = wmi_get_event_data(value, &response);
516 if (status != AE_OK) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700517 pr_info("bad event status 0x%x\n", status);
Len Brownfda11e62009-12-26 23:02:24 -0500518 return;
519 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700520
521 obj = (union acpi_object *)response.pointer;
522
Thomas Renningerc4775062010-07-29 12:27:59 +0200523 if (!obj)
524 return;
525 if (obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700526 pr_info("Unknown response received %d\n", obj->type);
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100527 kfree(obj);
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100528 return;
529 }
530
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200531 /*
532 * Depending on ACPI version the concatenation of id and event data
533 * inside _WED function will result in a 8 or 16 byte buffer.
534 */
535 location = (u32 *)obj->buffer.pointer;
536 if (obj->buffer.length == 8) {
537 event_id = *location;
538 event_data = *(location + 1);
539 } else if (obj->buffer.length == 16) {
540 event_id = *location;
541 event_data = *(location + 2);
542 } else {
Joe Perchesb5a42232011-03-29 15:21:41 -0700543 pr_info("Unknown buffer length %d\n", obj->buffer.length);
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200544 kfree(obj);
545 return;
546 }
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100547 kfree(obj);
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200548
549 switch (event_id) {
Thomas Renninger751ae802010-05-21 16:18:09 +0200550 case HPWMI_DOCK_EVENT:
Carlo Caione298747b2017-04-09 15:56:08 +0200551 if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
552 input_report_switch(hp_wmi_input_dev, SW_DOCK,
553 hp_wmi_dock_state());
554 if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
555 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
556 hp_wmi_tablet_state());
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100557 input_sync(hp_wmi_input_dev);
Thomas Renninger751ae802010-05-21 16:18:09 +0200558 break;
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200559 case HPWMI_PARK_HDD:
560 break;
561 case HPWMI_SMART_ADAPTER:
562 break;
Thomas Renninger751ae802010-05-21 16:18:09 +0200563 case HPWMI_BEZEL_BUTTON:
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700564 key_code = hp_wmi_read_int(HPWMI_HOTKEY_QUERY);
565 if (key_code < 0)
Thomas Renninger6d96e002010-05-21 16:42:40 +0200566 break;
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700567
568 if (!sparse_keymap_report_event(hp_wmi_input_dev,
569 key_code, 1, true))
Joe Perchesb5a42232011-03-29 15:21:41 -0700570 pr_info("Unknown key code - 0x%x\n", key_code);
Thomas Renninger751ae802010-05-21 16:18:09 +0200571 break;
572 case HPWMI_WIRELESS:
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200573 if (rfkill2_count) {
574 hp_wmi_rfkill2_refresh();
575 break;
576 }
577
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100578 if (wifi_rfkill)
579 rfkill_set_states(wifi_rfkill,
580 hp_wmi_get_sw_state(HPWMI_WIFI),
581 hp_wmi_get_hw_state(HPWMI_WIFI));
582 if (bluetooth_rfkill)
583 rfkill_set_states(bluetooth_rfkill,
584 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
585 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
586 if (wwan_rfkill)
587 rfkill_set_states(wwan_rfkill,
588 hp_wmi_get_sw_state(HPWMI_WWAN),
589 hp_wmi_get_hw_state(HPWMI_WWAN));
Thomas Renninger751ae802010-05-21 16:18:09 +0200590 break;
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200591 case HPWMI_CPU_BATTERY_THROTTLE:
Joe Perchesb5a42232011-03-29 15:21:41 -0700592 pr_info("Unimplemented CPU throttle because of 3 Cell battery event detected\n");
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200593 break;
594 case HPWMI_LOCK_SWITCH:
595 break;
Alex Hungd9e290a2013-03-15 17:18:22 +0800596 case HPWMI_LID_SWITCH:
597 break;
598 case HPWMI_SCREEN_ROTATION:
599 break;
600 case HPWMI_COOLSENSE_SYSTEM_MOBILE:
601 break;
602 case HPWMI_COOLSENSE_SYSTEM_HOT:
603 break;
604 case HPWMI_PROXIMITY_SENSOR:
605 break;
606 case HPWMI_BACKLIT_KB_BRIGHTNESS:
607 break;
608 case HPWMI_PEAKSHIFT_PERIOD:
609 break;
610 case HPWMI_BATTERY_CHARGE_PERIOD:
611 break;
Thomas Renninger751ae802010-05-21 16:18:09 +0200612 default:
Joe Perchesb5a42232011-03-29 15:21:41 -0700613 pr_info("Unknown event_id - %d - 0x%x\n", event_id, event_data);
Thomas Renninger751ae802010-05-21 16:18:09 +0200614 break;
615 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700616}
617
618static int __init hp_wmi_input_setup(void)
619{
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700620 acpi_status status;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700621 int err, val;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700622
623 hp_wmi_input_dev = input_allocate_device();
Axel Linbc285962010-07-20 15:19:51 -0700624 if (!hp_wmi_input_dev)
625 return -ENOMEM;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700626
627 hp_wmi_input_dev->name = "HP WMI hotkeys";
628 hp_wmi_input_dev->phys = "wmi/input0";
629 hp_wmi_input_dev->id.bustype = BUS_HOST;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700630
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700631 __set_bit(EV_SW, hp_wmi_input_dev->evbit);
Carlo Caione298747b2017-04-09 15:56:08 +0200632
633 /* Dock */
634 val = hp_wmi_dock_state();
635 if (!(val < 0)) {
636 __set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
637 input_report_switch(hp_wmi_input_dev, SW_DOCK, val);
638 }
639
640 /* Tablet mode */
641 val = hp_wmi_tablet_state();
642 if (!(val < 0)) {
643 __set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
644 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE, val);
645 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700646
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700647 err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
648 if (err)
649 goto err_free_dev;
Matthew Garrett871043b2009-06-01 15:25:45 +0100650
651 /* Set initial hardware state */
Matthew Garrett871043b2009-06-01 15:25:45 +0100652 input_sync(hp_wmi_input_dev);
653
Kyle Evans8a1513b2015-09-11 10:40:17 -0500654 if (!hp_wmi_bios_2009_later() && hp_wmi_bios_2008_later())
Kyle Evansf82bdd02014-06-09 12:26:06 -0500655 hp_wmi_enable_hotkeys();
656
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700657 status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
658 if (ACPI_FAILURE(status)) {
659 err = -EIO;
Michał Kępień3a3c1112017-03-09 13:11:43 +0100660 goto err_free_dev;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700661 }
662
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700663 err = input_register_device(hp_wmi_input_dev);
664 if (err)
665 goto err_uninstall_notifier;
666
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700667 return 0;
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700668
669 err_uninstall_notifier:
670 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700671 err_free_dev:
672 input_free_device(hp_wmi_input_dev);
673 return err;
674}
675
676static void hp_wmi_input_destroy(void)
677{
678 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700679 input_unregister_device(hp_wmi_input_dev);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700680}
681
682static void cleanup_sysfs(struct platform_device *device)
683{
684 device_remove_file(&device->dev, &dev_attr_display);
685 device_remove_file(&device->dev, &dev_attr_hddtemp);
686 device_remove_file(&device->dev, &dev_attr_als);
687 device_remove_file(&device->dev, &dev_attr_dock);
Matthew Garrett871043b2009-06-01 15:25:45 +0100688 device_remove_file(&device->dev, &dev_attr_tablet);
Alex Hung8667ca92013-06-13 16:46:25 +0800689 device_remove_file(&device->dev, &dev_attr_postcode);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700690}
691
Mathias Krause702ef542014-07-16 19:43:13 +0200692static int __init hp_wmi_rfkill_setup(struct platform_device *device)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700693{
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700694 int err, wireless;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200695
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700696 wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
697 if (wireless)
698 return wireless;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700699
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700700 err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE, &wireless,
Alex Hungfc8a6012016-06-13 19:44:00 +0800701 sizeof(wireless), 0);
702 if (err)
703 return err;
704
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700705 if (wireless & 0x1) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200706 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
707 RFKILL_TYPE_WLAN,
708 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100709 (void *) HPWMI_WIFI);
Dan Carpenterdd258c02012-05-17 09:48:12 +0300710 if (!wifi_rfkill)
711 return -ENOMEM;
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100712 rfkill_init_sw_state(wifi_rfkill,
713 hp_wmi_get_sw_state(HPWMI_WIFI));
714 rfkill_set_hw_state(wifi_rfkill,
715 hp_wmi_get_hw_state(HPWMI_WIFI));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800716 err = rfkill_register(wifi_rfkill);
717 if (err)
Johannes Berg19d337d2009-06-02 13:01:37 +0200718 goto register_wifi_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700719 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700720
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700721 if (wireless & 0x2) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200722 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
723 RFKILL_TYPE_BLUETOOTH,
724 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100725 (void *) HPWMI_BLUETOOTH);
Dan Carpenterdd258c02012-05-17 09:48:12 +0300726 if (!bluetooth_rfkill) {
727 err = -ENOMEM;
Maciej S. Szmigieroc7805e52016-03-06 23:38:36 +0100728 goto register_bluetooth_error;
Dan Carpenterdd258c02012-05-17 09:48:12 +0300729 }
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100730 rfkill_init_sw_state(bluetooth_rfkill,
731 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
732 rfkill_set_hw_state(bluetooth_rfkill,
733 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800734 err = rfkill_register(bluetooth_rfkill);
Frans Pop6989d562009-01-29 14:25:14 -0800735 if (err)
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800736 goto register_bluetooth_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700737 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700738
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700739 if (wireless & 0x4) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200740 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
741 RFKILL_TYPE_WWAN,
742 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100743 (void *) HPWMI_WWAN);
Dan Carpenterdd258c02012-05-17 09:48:12 +0300744 if (!wwan_rfkill) {
745 err = -ENOMEM;
Maciej S. Szmigieroc7805e52016-03-06 23:38:36 +0100746 goto register_wwan_error;
Dan Carpenterdd258c02012-05-17 09:48:12 +0300747 }
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100748 rfkill_init_sw_state(wwan_rfkill,
749 hp_wmi_get_sw_state(HPWMI_WWAN));
750 rfkill_set_hw_state(wwan_rfkill,
751 hp_wmi_get_hw_state(HPWMI_WWAN));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800752 err = rfkill_register(wwan_rfkill);
753 if (err)
Kirill Tkhai5b5c2b32013-07-29 17:34:37 +0400754 goto register_wwan_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700755 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700756
757 return 0;
Maciej S. Szmigierofffcad82016-03-06 23:40:19 +0100758
Kirill Tkhai5b5c2b32013-07-29 17:34:37 +0400759register_wwan_error:
760 rfkill_destroy(wwan_rfkill);
761 wwan_rfkill = NULL;
Maciej S. Szmigieroc7805e52016-03-06 23:38:36 +0100762 if (bluetooth_rfkill)
763 rfkill_unregister(bluetooth_rfkill);
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800764register_bluetooth_error:
Johannes Berg19d337d2009-06-02 13:01:37 +0200765 rfkill_destroy(bluetooth_rfkill);
Anssi Hannula6d97db52011-02-20 20:07:24 +0200766 bluetooth_rfkill = NULL;
Andrew Morton44f06062009-02-04 15:12:07 -0800767 if (wifi_rfkill)
768 rfkill_unregister(wifi_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200769register_wifi_error:
770 rfkill_destroy(wifi_rfkill);
Anssi Hannula6d97db52011-02-20 20:07:24 +0200771 wifi_rfkill = NULL;
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200772 return err;
773}
774
Mathias Krause702ef542014-07-16 19:43:13 +0200775static int __init hp_wmi_rfkill2_setup(struct platform_device *device)
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200776{
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200777 struct bios_rfkill2_state state;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700778 int err, i;
779
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700780 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200781 0, sizeof(state));
782 if (err)
783 return err;
784
785 if (state.count > HPWMI_MAX_RFKILL2_DEVICES) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700786 pr_warn("unable to parse 0x1b query output\n");
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200787 return -EINVAL;
788 }
789
790 for (i = 0; i < state.count; i++) {
791 struct rfkill *rfkill;
792 enum rfkill_type type;
793 char *name;
794 switch (state.device[i].radio_type) {
795 case HPWMI_WIFI:
796 type = RFKILL_TYPE_WLAN;
797 name = "hp-wifi";
798 break;
799 case HPWMI_BLUETOOTH:
800 type = RFKILL_TYPE_BLUETOOTH;
801 name = "hp-bluetooth";
802 break;
803 case HPWMI_WWAN:
804 type = RFKILL_TYPE_WWAN;
805 name = "hp-wwan";
806 break;
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200807 case HPWMI_GPS:
808 type = RFKILL_TYPE_GPS;
809 name = "hp-gps";
810 break;
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200811 default:
Joe Perchesb5a42232011-03-29 15:21:41 -0700812 pr_warn("unknown device type 0x%x\n",
813 state.device[i].radio_type);
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200814 continue;
815 }
816
817 if (!state.device[i].vendor_id) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700818 pr_warn("zero device %d while %d reported\n",
819 i, state.count);
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200820 continue;
821 }
822
823 rfkill = rfkill_alloc(name, &device->dev, type,
824 &hp_wmi_rfkill2_ops, (void *)(long)i);
825 if (!rfkill) {
826 err = -ENOMEM;
827 goto fail;
828 }
829
830 rfkill2[rfkill2_count].id = state.device[i].rfkill_id;
831 rfkill2[rfkill2_count].num = i;
832 rfkill2[rfkill2_count].rfkill = rfkill;
833
834 rfkill_init_sw_state(rfkill,
835 IS_SWBLOCKED(state.device[i].power));
836 rfkill_set_hw_state(rfkill,
837 IS_HWBLOCKED(state.device[i].power));
838
839 if (!(state.device[i].power & HPWMI_POWER_BIOS))
Joe Perchesb5a42232011-03-29 15:21:41 -0700840 pr_info("device %s blocked by BIOS\n", name);
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200841
842 err = rfkill_register(rfkill);
843 if (err) {
844 rfkill_destroy(rfkill);
845 goto fail;
846 }
847
848 rfkill2_count++;
849 }
850
851 return 0;
852fail:
853 for (; rfkill2_count > 0; rfkill2_count--) {
854 rfkill_unregister(rfkill2[rfkill2_count - 1].rfkill);
855 rfkill_destroy(rfkill2[rfkill2_count - 1].rfkill);
856 }
857 return err;
858}
859
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800860static int __init hp_wmi_bios_setup(struct platform_device *device)
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200861{
862 int err;
863
Anssi Hannula6d97db52011-02-20 20:07:24 +0200864 /* clear detected rfkill devices */
865 wifi_rfkill = NULL;
866 bluetooth_rfkill = NULL;
867 wwan_rfkill = NULL;
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200868 rfkill2_count = 0;
Anssi Hannula6d97db52011-02-20 20:07:24 +0200869
Alex Hungfc8a6012016-06-13 19:44:00 +0800870 if (hp_wmi_rfkill_setup(device))
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200871 hp_wmi_rfkill2_setup(device);
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200872
873 err = device_create_file(&device->dev, &dev_attr_display);
874 if (err)
875 goto add_sysfs_error;
876 err = device_create_file(&device->dev, &dev_attr_hddtemp);
877 if (err)
878 goto add_sysfs_error;
879 err = device_create_file(&device->dev, &dev_attr_als);
880 if (err)
881 goto add_sysfs_error;
882 err = device_create_file(&device->dev, &dev_attr_dock);
883 if (err)
884 goto add_sysfs_error;
885 err = device_create_file(&device->dev, &dev_attr_tablet);
886 if (err)
887 goto add_sysfs_error;
Alex Hung8667ca92013-06-13 16:46:25 +0800888 err = device_create_file(&device->dev, &dev_attr_postcode);
889 if (err)
890 goto add_sysfs_error;
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200891 return 0;
892
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700893add_sysfs_error:
894 cleanup_sysfs(device);
895 return err;
896}
897
898static int __exit hp_wmi_bios_remove(struct platform_device *device)
899{
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200900 int i;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700901 cleanup_sysfs(device);
902
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200903 for (i = 0; i < rfkill2_count; i++) {
904 rfkill_unregister(rfkill2[i].rfkill);
905 rfkill_destroy(rfkill2[i].rfkill);
906 }
907
Johannes Berg19d337d2009-06-02 13:01:37 +0200908 if (wifi_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700909 rfkill_unregister(wifi_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200910 rfkill_destroy(wifi_rfkill);
911 }
912 if (bluetooth_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700913 rfkill_unregister(bluetooth_rfkill);
Corentin Chary09729f02009-09-14 12:43:51 +0200914 rfkill_destroy(bluetooth_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200915 }
916 if (wwan_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700917 rfkill_unregister(wwan_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200918 rfkill_destroy(wwan_rfkill);
919 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700920
921 return 0;
922}
923
Frans Pop8dd2b422009-08-20 20:38:13 +0200924static int hp_wmi_resume_handler(struct device *device)
Frans Pop4c395bd2009-03-04 11:55:28 -0800925{
Frans Pop4c395bd2009-03-04 11:55:28 -0800926 /*
Matthew Garrett871043b2009-06-01 15:25:45 +0100927 * Hardware state may have changed while suspended, so trigger
928 * input events for the current state. As this is a switch,
Frans Pop4c395bd2009-03-04 11:55:28 -0800929 * the input layer will only actually pass it on if the state
930 * changed.
931 */
Frans Popdaed9532009-07-30 17:16:05 -0400932 if (hp_wmi_input_dev) {
Carlo Caione298747b2017-04-09 15:56:08 +0200933 if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
934 input_report_switch(hp_wmi_input_dev, SW_DOCK,
935 hp_wmi_dock_state());
936 if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
937 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
938 hp_wmi_tablet_state());
Frans Popdaed9532009-07-30 17:16:05 -0400939 input_sync(hp_wmi_input_dev);
940 }
Frans Pop4c395bd2009-03-04 11:55:28 -0800941
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200942 if (rfkill2_count)
943 hp_wmi_rfkill2_refresh();
944
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100945 if (wifi_rfkill)
946 rfkill_set_states(wifi_rfkill,
947 hp_wmi_get_sw_state(HPWMI_WIFI),
948 hp_wmi_get_hw_state(HPWMI_WIFI));
949 if (bluetooth_rfkill)
950 rfkill_set_states(bluetooth_rfkill,
951 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
952 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
953 if (wwan_rfkill)
954 rfkill_set_states(wwan_rfkill,
955 hp_wmi_get_sw_state(HPWMI_WWAN),
956 hp_wmi_get_hw_state(HPWMI_WWAN));
957
Frans Pop4c395bd2009-03-04 11:55:28 -0800958 return 0;
959}
960
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800961static const struct dev_pm_ops hp_wmi_pm_ops = {
962 .resume = hp_wmi_resume_handler,
963 .restore = hp_wmi_resume_handler,
964};
965
966static struct platform_driver hp_wmi_driver = {
967 .driver = {
968 .name = "hp-wmi",
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800969 .pm = &hp_wmi_pm_ops,
970 },
971 .remove = __exit_p(hp_wmi_bios_remove),
972};
973
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700974static int __init hp_wmi_init(void)
975{
Thomas Renningerb0966672010-07-20 15:19:29 -0700976 int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
977 int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700978 int err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700979
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800980 if (!bios_capable && !event_capable)
981 return -ENODEV;
982
Thomas Renningerb0966672010-07-20 15:19:29 -0700983 if (event_capable) {
Axel Lindfec5c42010-06-10 16:06:40 +0800984 err = hp_wmi_input_setup();
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700985 if (err)
Axel Lindfec5c42010-06-10 16:06:40 +0800986 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700987 }
988
Thomas Renningerb0966672010-07-20 15:19:29 -0700989 if (bios_capable) {
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800990 hp_wmi_platform_dev =
991 platform_device_register_simple("hp-wmi", -1, NULL, 0);
992 if (IS_ERR(hp_wmi_platform_dev)) {
993 err = PTR_ERR(hp_wmi_platform_dev);
994 goto err_destroy_input;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700995 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700996
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800997 err = platform_driver_probe(&hp_wmi_driver, hp_wmi_bios_setup);
998 if (err)
999 goto err_unregister_device;
1000 }
Thomas Renningerb0966672010-07-20 15:19:29 -07001001
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001002 return 0;
Axel Lindfec5c42010-06-10 16:06:40 +08001003
Dmitry Torokhovc165b802013-02-20 00:44:34 -08001004err_unregister_device:
1005 platform_device_unregister(hp_wmi_platform_dev);
1006err_destroy_input:
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -07001007 if (event_capable)
1008 hp_wmi_input_destroy();
Axel Lindfec5c42010-06-10 16:06:40 +08001009
1010 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001011}
Dmitry Torokhovc165b802013-02-20 00:44:34 -08001012module_init(hp_wmi_init);
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001013
1014static void __exit hp_wmi_exit(void)
1015{
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -07001016 if (wmi_has_guid(HPWMI_EVENT_GUID))
1017 hp_wmi_input_destroy();
1018
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001019 if (hp_wmi_platform_dev) {
Axel Lin97ba0af2010-06-03 15:18:03 +08001020 platform_device_unregister(hp_wmi_platform_dev);
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001021 platform_driver_unregister(&hp_wmi_driver);
1022 }
1023}
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001024module_exit(hp_wmi_exit);