blob: eb6d0a0e139219c13d18300b96e5cfeb09bcd5f1 [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);
Matthew Garretta8ec1052010-08-23 15:52:34 -0400346 int wireless = 0;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700347
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700348 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_READ,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200349 &wireless, sizeof(wireless),
350 sizeof(wireless));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200351 /* TBD: Pass error */
352
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100353 if (wireless & mask)
Johannes Berg19d337d2009-06-02 13:01:37 +0200354 return false;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700355 else
Johannes Berg19d337d2009-06-02 13:01:37 +0200356 return true;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700357}
358
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100359static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700360{
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700361 int mask = 0x800 << (r * 8);
Matthew Garretta8ec1052010-08-23 15:52:34 -0400362 int wireless = 0;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700363
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700364 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_READ,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200365 &wireless, sizeof(wireless),
366 sizeof(wireless));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200367 /* TBD: Pass error */
368
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100369 if (wireless & mask)
Johannes Berg19d337d2009-06-02 13:01:37 +0200370 return false;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700371 else
Johannes Berg19d337d2009-06-02 13:01:37 +0200372 return true;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700373}
374
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200375static int hp_wmi_rfkill2_set_block(void *data, bool blocked)
376{
377 int rfkill_id = (int)(long)data;
378 char buffer[4] = { 0x01, 0x00, rfkill_id, !blocked };
379
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700380 if (hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_WRITE,
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200381 buffer, sizeof(buffer), 0))
382 return -EINVAL;
383 return 0;
384}
385
386static const struct rfkill_ops hp_wmi_rfkill2_ops = {
387 .set_block = hp_wmi_rfkill2_set_block,
388};
389
390static int hp_wmi_rfkill2_refresh(void)
391{
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200392 struct bios_rfkill2_state state;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700393 int err, i;
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200394
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700395 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200396 0, sizeof(state));
397 if (err)
398 return err;
399
400 for (i = 0; i < rfkill2_count; i++) {
401 int num = rfkill2[i].num;
402 struct bios_rfkill2_device_state *devstate;
403 devstate = &state.device[num];
404
405 if (num >= state.count ||
406 devstate->rfkill_id != rfkill2[i].id) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700407 pr_warn("power configuration of the wireless devices unexpectedly changed\n");
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200408 continue;
409 }
410
411 rfkill_set_states(rfkill2[i].rfkill,
412 IS_SWBLOCKED(devstate->power),
413 IS_HWBLOCKED(devstate->power));
414 }
415
416 return 0;
417}
418
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700419static ssize_t show_display(struct device *dev, struct device_attribute *attr,
420 char *buf)
421{
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700422 int value = hp_wmi_read_int(HPWMI_DISPLAY_QUERY);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700423 if (value < 0)
424 return -EINVAL;
425 return sprintf(buf, "%d\n", value);
426}
427
428static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
429 char *buf)
430{
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700431 int value = hp_wmi_read_int(HPWMI_HDDTEMP_QUERY);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700432 if (value < 0)
433 return -EINVAL;
434 return sprintf(buf, "%d\n", value);
435}
436
437static ssize_t show_als(struct device *dev, struct device_attribute *attr,
438 char *buf)
439{
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700440 int value = hp_wmi_read_int(HPWMI_ALS_QUERY);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700441 if (value < 0)
442 return -EINVAL;
443 return sprintf(buf, "%d\n", value);
444}
445
446static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
447 char *buf)
448{
449 int value = hp_wmi_dock_state();
450 if (value < 0)
451 return -EINVAL;
452 return sprintf(buf, "%d\n", value);
453}
454
Matthew Garrett871043b2009-06-01 15:25:45 +0100455static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
456 char *buf)
457{
458 int value = hp_wmi_tablet_state();
459 if (value < 0)
460 return -EINVAL;
461 return sprintf(buf, "%d\n", value);
462}
463
Alex Hung8667ca92013-06-13 16:46:25 +0800464static ssize_t show_postcode(struct device *dev, struct device_attribute *attr,
465 char *buf)
466{
467 /* Get the POST error code of previous boot failure. */
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700468 int value = hp_wmi_read_int(HPWMI_POSTCODEERROR_QUERY);
Alex Hung8667ca92013-06-13 16:46:25 +0800469 if (value < 0)
470 return -EINVAL;
471 return sprintf(buf, "0x%x\n", value);
472}
473
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700474static ssize_t set_als(struct device *dev, struct device_attribute *attr,
475 const char *buf, size_t count)
476{
477 u32 tmp = simple_strtoul(buf, NULL, 10);
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700478 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, HPWMI_WRITE, &tmp,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200479 sizeof(tmp), sizeof(tmp));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200480 if (ret)
Carlo Caioned3138762017-04-19 22:36:39 +0200481 return ret < 0 ? ret : -EINVAL;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200482
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700483 return count;
484}
485
Alex Hung8667ca92013-06-13 16:46:25 +0800486static ssize_t set_postcode(struct device *dev, struct device_attribute *attr,
487 const char *buf, size_t count)
488{
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700489 long unsigned int tmp2;
Alex Hung8667ca92013-06-13 16:46:25 +0800490 int ret;
491 u32 tmp;
Alex Hung8667ca92013-06-13 16:46:25 +0800492
493 ret = kstrtoul(buf, 10, &tmp2);
494 if (ret || tmp2 != 1)
495 return -EINVAL;
496
497 /* Clear the POST error code. It is kept until until cleared. */
498 tmp = (u32) tmp2;
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700499 ret = hp_wmi_perform_query(HPWMI_POSTCODEERROR_QUERY, HPWMI_WRITE, &tmp,
Alex Hung8667ca92013-06-13 16:46:25 +0800500 sizeof(tmp), sizeof(tmp));
501 if (ret)
Carlo Caioned3138762017-04-19 22:36:39 +0200502 return ret < 0 ? ret : -EINVAL;
Alex Hung8667ca92013-06-13 16:46:25 +0800503
504 return count;
505}
506
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700507static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
508static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
509static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
510static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
Matthew Garrett871043b2009-06-01 15:25:45 +0100511static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
Alex Hung8667ca92013-06-13 16:46:25 +0800512static DEVICE_ATTR(postcode, S_IRUGO | S_IWUSR, show_postcode, set_postcode);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700513
Adrian Bunk88429a12008-10-15 22:05:17 -0700514static void hp_wmi_notify(u32 value, void *context)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700515{
516 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200517 u32 event_id, event_data;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700518 union acpi_object *obj;
Len Brownfda11e62009-12-26 23:02:24 -0500519 acpi_status status;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700520 u32 *location;
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700521 int key_code;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700522
Len Brownfda11e62009-12-26 23:02:24 -0500523 status = wmi_get_event_data(value, &response);
524 if (status != AE_OK) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700525 pr_info("bad event status 0x%x\n", status);
Len Brownfda11e62009-12-26 23:02:24 -0500526 return;
527 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700528
529 obj = (union acpi_object *)response.pointer;
530
Thomas Renningerc4775062010-07-29 12:27:59 +0200531 if (!obj)
532 return;
533 if (obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700534 pr_info("Unknown response received %d\n", obj->type);
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100535 kfree(obj);
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100536 return;
537 }
538
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200539 /*
540 * Depending on ACPI version the concatenation of id and event data
541 * inside _WED function will result in a 8 or 16 byte buffer.
542 */
543 location = (u32 *)obj->buffer.pointer;
544 if (obj->buffer.length == 8) {
545 event_id = *location;
546 event_data = *(location + 1);
547 } else if (obj->buffer.length == 16) {
548 event_id = *location;
549 event_data = *(location + 2);
550 } else {
Joe Perchesb5a42232011-03-29 15:21:41 -0700551 pr_info("Unknown buffer length %d\n", obj->buffer.length);
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200552 kfree(obj);
553 return;
554 }
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100555 kfree(obj);
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200556
557 switch (event_id) {
Thomas Renninger751ae802010-05-21 16:18:09 +0200558 case HPWMI_DOCK_EVENT:
Carlo Caione298747b2017-04-09 15:56:08 +0200559 if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
560 input_report_switch(hp_wmi_input_dev, SW_DOCK,
561 hp_wmi_dock_state());
562 if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
563 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
564 hp_wmi_tablet_state());
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100565 input_sync(hp_wmi_input_dev);
Thomas Renninger751ae802010-05-21 16:18:09 +0200566 break;
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200567 case HPWMI_PARK_HDD:
568 break;
569 case HPWMI_SMART_ADAPTER:
570 break;
Thomas Renninger751ae802010-05-21 16:18:09 +0200571 case HPWMI_BEZEL_BUTTON:
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700572 key_code = hp_wmi_read_int(HPWMI_HOTKEY_QUERY);
573 if (key_code < 0)
Thomas Renninger6d96e002010-05-21 16:42:40 +0200574 break;
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700575
576 if (!sparse_keymap_report_event(hp_wmi_input_dev,
577 key_code, 1, true))
Joe Perchesb5a42232011-03-29 15:21:41 -0700578 pr_info("Unknown key code - 0x%x\n", key_code);
Thomas Renninger751ae802010-05-21 16:18:09 +0200579 break;
580 case HPWMI_WIRELESS:
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200581 if (rfkill2_count) {
582 hp_wmi_rfkill2_refresh();
583 break;
584 }
585
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100586 if (wifi_rfkill)
587 rfkill_set_states(wifi_rfkill,
588 hp_wmi_get_sw_state(HPWMI_WIFI),
589 hp_wmi_get_hw_state(HPWMI_WIFI));
590 if (bluetooth_rfkill)
591 rfkill_set_states(bluetooth_rfkill,
592 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
593 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
594 if (wwan_rfkill)
595 rfkill_set_states(wwan_rfkill,
596 hp_wmi_get_sw_state(HPWMI_WWAN),
597 hp_wmi_get_hw_state(HPWMI_WWAN));
Thomas Renninger751ae802010-05-21 16:18:09 +0200598 break;
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200599 case HPWMI_CPU_BATTERY_THROTTLE:
Joe Perchesb5a42232011-03-29 15:21:41 -0700600 pr_info("Unimplemented CPU throttle because of 3 Cell battery event detected\n");
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200601 break;
602 case HPWMI_LOCK_SWITCH:
603 break;
Alex Hungd9e290a2013-03-15 17:18:22 +0800604 case HPWMI_LID_SWITCH:
605 break;
606 case HPWMI_SCREEN_ROTATION:
607 break;
608 case HPWMI_COOLSENSE_SYSTEM_MOBILE:
609 break;
610 case HPWMI_COOLSENSE_SYSTEM_HOT:
611 break;
612 case HPWMI_PROXIMITY_SENSOR:
613 break;
614 case HPWMI_BACKLIT_KB_BRIGHTNESS:
615 break;
616 case HPWMI_PEAKSHIFT_PERIOD:
617 break;
618 case HPWMI_BATTERY_CHARGE_PERIOD:
619 break;
Thomas Renninger751ae802010-05-21 16:18:09 +0200620 default:
Joe Perchesb5a42232011-03-29 15:21:41 -0700621 pr_info("Unknown event_id - %d - 0x%x\n", event_id, event_data);
Thomas Renninger751ae802010-05-21 16:18:09 +0200622 break;
623 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700624}
625
626static int __init hp_wmi_input_setup(void)
627{
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700628 acpi_status status;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700629 int err, val;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700630
631 hp_wmi_input_dev = input_allocate_device();
Axel Linbc285962010-07-20 15:19:51 -0700632 if (!hp_wmi_input_dev)
633 return -ENOMEM;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700634
635 hp_wmi_input_dev->name = "HP WMI hotkeys";
636 hp_wmi_input_dev->phys = "wmi/input0";
637 hp_wmi_input_dev->id.bustype = BUS_HOST;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700638
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700639 __set_bit(EV_SW, hp_wmi_input_dev->evbit);
Carlo Caione298747b2017-04-09 15:56:08 +0200640
641 /* Dock */
642 val = hp_wmi_dock_state();
643 if (!(val < 0)) {
644 __set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
645 input_report_switch(hp_wmi_input_dev, SW_DOCK, val);
646 }
647
648 /* Tablet mode */
649 val = hp_wmi_tablet_state();
650 if (!(val < 0)) {
651 __set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
652 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE, val);
653 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700654
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700655 err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
656 if (err)
657 goto err_free_dev;
Matthew Garrett871043b2009-06-01 15:25:45 +0100658
659 /* Set initial hardware state */
Matthew Garrett871043b2009-06-01 15:25:45 +0100660 input_sync(hp_wmi_input_dev);
661
Kyle Evans8a1513b2015-09-11 10:40:17 -0500662 if (!hp_wmi_bios_2009_later() && hp_wmi_bios_2008_later())
Kyle Evansf82bdd02014-06-09 12:26:06 -0500663 hp_wmi_enable_hotkeys();
664
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700665 status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
666 if (ACPI_FAILURE(status)) {
667 err = -EIO;
Michał Kępień3a3c1112017-03-09 13:11:43 +0100668 goto err_free_dev;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700669 }
670
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700671 err = input_register_device(hp_wmi_input_dev);
672 if (err)
673 goto err_uninstall_notifier;
674
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700675 return 0;
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700676
677 err_uninstall_notifier:
678 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700679 err_free_dev:
680 input_free_device(hp_wmi_input_dev);
681 return err;
682}
683
684static void hp_wmi_input_destroy(void)
685{
686 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700687 input_unregister_device(hp_wmi_input_dev);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700688}
689
690static void cleanup_sysfs(struct platform_device *device)
691{
692 device_remove_file(&device->dev, &dev_attr_display);
693 device_remove_file(&device->dev, &dev_attr_hddtemp);
694 device_remove_file(&device->dev, &dev_attr_als);
695 device_remove_file(&device->dev, &dev_attr_dock);
Matthew Garrett871043b2009-06-01 15:25:45 +0100696 device_remove_file(&device->dev, &dev_attr_tablet);
Alex Hung8667ca92013-06-13 16:46:25 +0800697 device_remove_file(&device->dev, &dev_attr_postcode);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700698}
699
Mathias Krause702ef542014-07-16 19:43:13 +0200700static int __init hp_wmi_rfkill_setup(struct platform_device *device)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700701{
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700702 int err, wireless;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200703
Darren Hart (VMware)ea621d92017-04-19 16:26:06 -0700704 wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
705 if (wireless)
706 return wireless;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700707
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700708 err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE, &wireless,
Alex Hungfc8a6012016-06-13 19:44:00 +0800709 sizeof(wireless), 0);
710 if (err)
711 return err;
712
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700713 if (wireless & 0x1) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200714 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
715 RFKILL_TYPE_WLAN,
716 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100717 (void *) HPWMI_WIFI);
Dan Carpenterdd258c02012-05-17 09:48:12 +0300718 if (!wifi_rfkill)
719 return -ENOMEM;
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100720 rfkill_init_sw_state(wifi_rfkill,
721 hp_wmi_get_sw_state(HPWMI_WIFI));
722 rfkill_set_hw_state(wifi_rfkill,
723 hp_wmi_get_hw_state(HPWMI_WIFI));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800724 err = rfkill_register(wifi_rfkill);
725 if (err)
Johannes Berg19d337d2009-06-02 13:01:37 +0200726 goto register_wifi_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700727 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700728
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700729 if (wireless & 0x2) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200730 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
731 RFKILL_TYPE_BLUETOOTH,
732 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100733 (void *) HPWMI_BLUETOOTH);
Dan Carpenterdd258c02012-05-17 09:48:12 +0300734 if (!bluetooth_rfkill) {
735 err = -ENOMEM;
Maciej S. Szmigieroc7805e52016-03-06 23:38:36 +0100736 goto register_bluetooth_error;
Dan Carpenterdd258c02012-05-17 09:48:12 +0300737 }
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100738 rfkill_init_sw_state(bluetooth_rfkill,
739 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
740 rfkill_set_hw_state(bluetooth_rfkill,
741 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800742 err = rfkill_register(bluetooth_rfkill);
Frans Pop6989d562009-01-29 14:25:14 -0800743 if (err)
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800744 goto register_bluetooth_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700745 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700746
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700747 if (wireless & 0x4) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200748 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
749 RFKILL_TYPE_WWAN,
750 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100751 (void *) HPWMI_WWAN);
Dan Carpenterdd258c02012-05-17 09:48:12 +0300752 if (!wwan_rfkill) {
753 err = -ENOMEM;
Maciej S. Szmigieroc7805e52016-03-06 23:38:36 +0100754 goto register_wwan_error;
Dan Carpenterdd258c02012-05-17 09:48:12 +0300755 }
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100756 rfkill_init_sw_state(wwan_rfkill,
757 hp_wmi_get_sw_state(HPWMI_WWAN));
758 rfkill_set_hw_state(wwan_rfkill,
759 hp_wmi_get_hw_state(HPWMI_WWAN));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800760 err = rfkill_register(wwan_rfkill);
761 if (err)
Kirill Tkhai5b5c2b32013-07-29 17:34:37 +0400762 goto register_wwan_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700763 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700764
765 return 0;
Maciej S. Szmigierofffcad82016-03-06 23:40:19 +0100766
Kirill Tkhai5b5c2b32013-07-29 17:34:37 +0400767register_wwan_error:
768 rfkill_destroy(wwan_rfkill);
769 wwan_rfkill = NULL;
Maciej S. Szmigieroc7805e52016-03-06 23:38:36 +0100770 if (bluetooth_rfkill)
771 rfkill_unregister(bluetooth_rfkill);
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800772register_bluetooth_error:
Johannes Berg19d337d2009-06-02 13:01:37 +0200773 rfkill_destroy(bluetooth_rfkill);
Anssi Hannula6d97db52011-02-20 20:07:24 +0200774 bluetooth_rfkill = NULL;
Andrew Morton44f06062009-02-04 15:12:07 -0800775 if (wifi_rfkill)
776 rfkill_unregister(wifi_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200777register_wifi_error:
778 rfkill_destroy(wifi_rfkill);
Anssi Hannula6d97db52011-02-20 20:07:24 +0200779 wifi_rfkill = NULL;
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200780 return err;
781}
782
Mathias Krause702ef542014-07-16 19:43:13 +0200783static int __init hp_wmi_rfkill2_setup(struct platform_device *device)
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200784{
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200785 struct bios_rfkill2_state state;
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700786 int err, i;
787
Darren Hart (VMware)d8193cf2017-04-19 15:42:01 -0700788 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200789 0, sizeof(state));
790 if (err)
791 return err;
792
793 if (state.count > HPWMI_MAX_RFKILL2_DEVICES) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700794 pr_warn("unable to parse 0x1b query output\n");
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200795 return -EINVAL;
796 }
797
798 for (i = 0; i < state.count; i++) {
799 struct rfkill *rfkill;
800 enum rfkill_type type;
801 char *name;
802 switch (state.device[i].radio_type) {
803 case HPWMI_WIFI:
804 type = RFKILL_TYPE_WLAN;
805 name = "hp-wifi";
806 break;
807 case HPWMI_BLUETOOTH:
808 type = RFKILL_TYPE_BLUETOOTH;
809 name = "hp-bluetooth";
810 break;
811 case HPWMI_WWAN:
812 type = RFKILL_TYPE_WWAN;
813 name = "hp-wwan";
814 break;
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200815 case HPWMI_GPS:
816 type = RFKILL_TYPE_GPS;
817 name = "hp-gps";
818 break;
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200819 default:
Joe Perchesb5a42232011-03-29 15:21:41 -0700820 pr_warn("unknown device type 0x%x\n",
821 state.device[i].radio_type);
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200822 continue;
823 }
824
825 if (!state.device[i].vendor_id) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700826 pr_warn("zero device %d while %d reported\n",
827 i, state.count);
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200828 continue;
829 }
830
831 rfkill = rfkill_alloc(name, &device->dev, type,
832 &hp_wmi_rfkill2_ops, (void *)(long)i);
833 if (!rfkill) {
834 err = -ENOMEM;
835 goto fail;
836 }
837
838 rfkill2[rfkill2_count].id = state.device[i].rfkill_id;
839 rfkill2[rfkill2_count].num = i;
840 rfkill2[rfkill2_count].rfkill = rfkill;
841
842 rfkill_init_sw_state(rfkill,
843 IS_SWBLOCKED(state.device[i].power));
844 rfkill_set_hw_state(rfkill,
845 IS_HWBLOCKED(state.device[i].power));
846
847 if (!(state.device[i].power & HPWMI_POWER_BIOS))
Joe Perchesb5a42232011-03-29 15:21:41 -0700848 pr_info("device %s blocked by BIOS\n", name);
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200849
850 err = rfkill_register(rfkill);
851 if (err) {
852 rfkill_destroy(rfkill);
853 goto fail;
854 }
855
856 rfkill2_count++;
857 }
858
859 return 0;
860fail:
861 for (; rfkill2_count > 0; rfkill2_count--) {
862 rfkill_unregister(rfkill2[rfkill2_count - 1].rfkill);
863 rfkill_destroy(rfkill2[rfkill2_count - 1].rfkill);
864 }
865 return err;
866}
867
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800868static int __init hp_wmi_bios_setup(struct platform_device *device)
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200869{
870 int err;
871
Anssi Hannula6d97db52011-02-20 20:07:24 +0200872 /* clear detected rfkill devices */
873 wifi_rfkill = NULL;
874 bluetooth_rfkill = NULL;
875 wwan_rfkill = NULL;
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200876 rfkill2_count = 0;
Anssi Hannula6d97db52011-02-20 20:07:24 +0200877
Alex Hungfc8a6012016-06-13 19:44:00 +0800878 if (hp_wmi_rfkill_setup(device))
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200879 hp_wmi_rfkill2_setup(device);
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200880
881 err = device_create_file(&device->dev, &dev_attr_display);
882 if (err)
883 goto add_sysfs_error;
884 err = device_create_file(&device->dev, &dev_attr_hddtemp);
885 if (err)
886 goto add_sysfs_error;
887 err = device_create_file(&device->dev, &dev_attr_als);
888 if (err)
889 goto add_sysfs_error;
890 err = device_create_file(&device->dev, &dev_attr_dock);
891 if (err)
892 goto add_sysfs_error;
893 err = device_create_file(&device->dev, &dev_attr_tablet);
894 if (err)
895 goto add_sysfs_error;
Alex Hung8667ca92013-06-13 16:46:25 +0800896 err = device_create_file(&device->dev, &dev_attr_postcode);
897 if (err)
898 goto add_sysfs_error;
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200899 return 0;
900
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700901add_sysfs_error:
902 cleanup_sysfs(device);
903 return err;
904}
905
906static int __exit hp_wmi_bios_remove(struct platform_device *device)
907{
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200908 int i;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700909 cleanup_sysfs(device);
910
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200911 for (i = 0; i < rfkill2_count; i++) {
912 rfkill_unregister(rfkill2[i].rfkill);
913 rfkill_destroy(rfkill2[i].rfkill);
914 }
915
Johannes Berg19d337d2009-06-02 13:01:37 +0200916 if (wifi_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700917 rfkill_unregister(wifi_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200918 rfkill_destroy(wifi_rfkill);
919 }
920 if (bluetooth_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700921 rfkill_unregister(bluetooth_rfkill);
Corentin Chary09729f02009-09-14 12:43:51 +0200922 rfkill_destroy(bluetooth_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200923 }
924 if (wwan_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700925 rfkill_unregister(wwan_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200926 rfkill_destroy(wwan_rfkill);
927 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700928
929 return 0;
930}
931
Frans Pop8dd2b422009-08-20 20:38:13 +0200932static int hp_wmi_resume_handler(struct device *device)
Frans Pop4c395bd2009-03-04 11:55:28 -0800933{
Frans Pop4c395bd2009-03-04 11:55:28 -0800934 /*
Matthew Garrett871043b2009-06-01 15:25:45 +0100935 * Hardware state may have changed while suspended, so trigger
936 * input events for the current state. As this is a switch,
Frans Pop4c395bd2009-03-04 11:55:28 -0800937 * the input layer will only actually pass it on if the state
938 * changed.
939 */
Frans Popdaed9532009-07-30 17:16:05 -0400940 if (hp_wmi_input_dev) {
Carlo Caione298747b2017-04-09 15:56:08 +0200941 if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
942 input_report_switch(hp_wmi_input_dev, SW_DOCK,
943 hp_wmi_dock_state());
944 if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
945 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
946 hp_wmi_tablet_state());
Frans Popdaed9532009-07-30 17:16:05 -0400947 input_sync(hp_wmi_input_dev);
948 }
Frans Pop4c395bd2009-03-04 11:55:28 -0800949
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200950 if (rfkill2_count)
951 hp_wmi_rfkill2_refresh();
952
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100953 if (wifi_rfkill)
954 rfkill_set_states(wifi_rfkill,
955 hp_wmi_get_sw_state(HPWMI_WIFI),
956 hp_wmi_get_hw_state(HPWMI_WIFI));
957 if (bluetooth_rfkill)
958 rfkill_set_states(bluetooth_rfkill,
959 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
960 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
961 if (wwan_rfkill)
962 rfkill_set_states(wwan_rfkill,
963 hp_wmi_get_sw_state(HPWMI_WWAN),
964 hp_wmi_get_hw_state(HPWMI_WWAN));
965
Frans Pop4c395bd2009-03-04 11:55:28 -0800966 return 0;
967}
968
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800969static const struct dev_pm_ops hp_wmi_pm_ops = {
970 .resume = hp_wmi_resume_handler,
971 .restore = hp_wmi_resume_handler,
972};
973
974static struct platform_driver hp_wmi_driver = {
975 .driver = {
976 .name = "hp-wmi",
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800977 .pm = &hp_wmi_pm_ops,
978 },
979 .remove = __exit_p(hp_wmi_bios_remove),
980};
981
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700982static int __init hp_wmi_init(void)
983{
Thomas Renningerb0966672010-07-20 15:19:29 -0700984 int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
985 int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
Darren Hart (VMware)3bf93102017-04-13 11:05:42 -0700986 int err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700987
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800988 if (!bios_capable && !event_capable)
989 return -ENODEV;
990
Thomas Renningerb0966672010-07-20 15:19:29 -0700991 if (event_capable) {
Axel Lindfec5c42010-06-10 16:06:40 +0800992 err = hp_wmi_input_setup();
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700993 if (err)
Axel Lindfec5c42010-06-10 16:06:40 +0800994 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700995 }
996
Thomas Renningerb0966672010-07-20 15:19:29 -0700997 if (bios_capable) {
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800998 hp_wmi_platform_dev =
999 platform_device_register_simple("hp-wmi", -1, NULL, 0);
1000 if (IS_ERR(hp_wmi_platform_dev)) {
1001 err = PTR_ERR(hp_wmi_platform_dev);
1002 goto err_destroy_input;
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001003 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001004
Dmitry Torokhovc165b802013-02-20 00:44:34 -08001005 err = platform_driver_probe(&hp_wmi_driver, hp_wmi_bios_setup);
1006 if (err)
1007 goto err_unregister_device;
1008 }
Thomas Renningerb0966672010-07-20 15:19:29 -07001009
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001010 return 0;
Axel Lindfec5c42010-06-10 16:06:40 +08001011
Dmitry Torokhovc165b802013-02-20 00:44:34 -08001012err_unregister_device:
1013 platform_device_unregister(hp_wmi_platform_dev);
1014err_destroy_input:
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -07001015 if (event_capable)
1016 hp_wmi_input_destroy();
Axel Lindfec5c42010-06-10 16:06:40 +08001017
1018 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001019}
Dmitry Torokhovc165b802013-02-20 00:44:34 -08001020module_init(hp_wmi_init);
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001021
1022static void __exit hp_wmi_exit(void)
1023{
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -07001024 if (wmi_has_guid(HPWMI_EVENT_GUID))
1025 hp_wmi_input_destroy();
1026
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001027 if (hp_wmi_platform_dev) {
Axel Lin97ba0af2010-06-03 15:18:03 +08001028 platform_device_unregister(hp_wmi_platform_dev);
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001029 platform_driver_unregister(&hp_wmi_driver);
1030 }
1031}
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001032module_exit(hp_wmi_exit);