blob: d6970f47ae72f639d648c924a27bea8b97cbdfb6 [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
51#define HPWMI_DISPLAY_QUERY 0x1
52#define HPWMI_HDDTEMP_QUERY 0x2
53#define HPWMI_ALS_QUERY 0x3
Matthew Garrett871043b2009-06-01 15:25:45 +010054#define HPWMI_HARDWARE_QUERY 0x4
Matthew Garrett62ec30d2008-07-25 01:45:39 -070055#define HPWMI_WIRELESS_QUERY 0x5
Matthew Garretta8823ae2008-09-02 14:36:03 -070056#define HPWMI_HOTKEY_QUERY 0xc
Anssi Hannulac0b9c642011-02-20 20:07:26 +020057#define HPWMI_WIRELESS2_QUERY 0x1b
Alex Hung8667ca92013-06-13 16:46:25 +080058#define HPWMI_POSTCODEERROR_QUERY 0x2a
Matthew Garrett62ec30d2008-07-25 01:45:39 -070059
Alan Jenkinse5fbba82009-07-21 12:14:01 +010060enum hp_wmi_radio {
61 HPWMI_WIFI = 0,
62 HPWMI_BLUETOOTH = 1,
63 HPWMI_WWAN = 2,
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +020064 HPWMI_GPS = 3,
Alan Jenkinse5fbba82009-07-21 12:14:01 +010065};
66
Thomas Renninger751ae802010-05-21 16:18:09 +020067enum hp_wmi_event_ids {
68 HPWMI_DOCK_EVENT = 1,
Thomas Renninger1bbdfd52010-05-21 16:18:13 +020069 HPWMI_PARK_HDD = 2,
70 HPWMI_SMART_ADAPTER = 3,
Thomas Renninger751ae802010-05-21 16:18:09 +020071 HPWMI_BEZEL_BUTTON = 4,
72 HPWMI_WIRELESS = 5,
Thomas Renninger1bbdfd52010-05-21 16:18:13 +020073 HPWMI_CPU_BATTERY_THROTTLE = 6,
74 HPWMI_LOCK_SWITCH = 7,
Alex Hungd9e290a2013-03-15 17:18:22 +080075 HPWMI_LID_SWITCH = 8,
76 HPWMI_SCREEN_ROTATION = 9,
77 HPWMI_COOLSENSE_SYSTEM_MOBILE = 0x0A,
78 HPWMI_COOLSENSE_SYSTEM_HOT = 0x0B,
79 HPWMI_PROXIMITY_SENSOR = 0x0C,
80 HPWMI_BACKLIT_KB_BRIGHTNESS = 0x0D,
81 HPWMI_PEAKSHIFT_PERIOD = 0x0F,
82 HPWMI_BATTERY_CHARGE_PERIOD = 0x10,
Thomas Renninger751ae802010-05-21 16:18:09 +020083};
84
Matthew Garrett62ec30d2008-07-25 01:45:39 -070085struct bios_args {
86 u32 signature;
87 u32 command;
88 u32 commandtype;
89 u32 datasize;
Matthew Garretta8ec1052010-08-23 15:52:34 -040090 u32 data;
Matthew Garrett62ec30d2008-07-25 01:45:39 -070091};
92
93struct bios_return {
94 u32 sigpass;
95 u32 return_code;
Matthew Garrett62ec30d2008-07-25 01:45:39 -070096};
97
Anssi Hannula9af0e0f2011-02-20 20:07:20 +020098enum hp_return_value {
99 HPWMI_RET_WRONG_SIGNATURE = 0x02,
100 HPWMI_RET_UNKNOWN_COMMAND = 0x03,
101 HPWMI_RET_UNKNOWN_CMDTYPE = 0x04,
102 HPWMI_RET_INVALID_PARAMETERS = 0x05,
103};
104
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200105enum hp_wireless2_bits {
106 HPWMI_POWER_STATE = 0x01,
107 HPWMI_POWER_SOFT = 0x02,
108 HPWMI_POWER_BIOS = 0x04,
109 HPWMI_POWER_HARD = 0x08,
110};
111
112#define IS_HWBLOCKED(x) ((x & (HPWMI_POWER_BIOS | HPWMI_POWER_HARD)) \
113 != (HPWMI_POWER_BIOS | HPWMI_POWER_HARD))
114#define IS_SWBLOCKED(x) !(x & HPWMI_POWER_SOFT)
115
116struct bios_rfkill2_device_state {
117 u8 radio_type;
118 u8 bus_type;
119 u16 vendor_id;
120 u16 product_id;
121 u16 subsys_vendor_id;
122 u16 subsys_product_id;
123 u8 rfkill_id;
124 u8 power;
125 u8 unknown[4];
126};
127
128/* 7 devices fit into the 128 byte buffer */
129#define HPWMI_MAX_RFKILL2_DEVICES 7
130
131struct bios_rfkill2_state {
132 u8 unknown[7];
133 u8 count;
134 u8 pad[8];
135 struct bios_rfkill2_device_state device[HPWMI_MAX_RFKILL2_DEVICES];
136};
137
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700138static const struct key_entry hp_wmi_keymap[] = {
139 { KE_KEY, 0x02, { KEY_BRIGHTNESSUP } },
140 { KE_KEY, 0x03, { KEY_BRIGHTNESSDOWN } },
141 { KE_KEY, 0x20e6, { KEY_PROG1 } },
142 { KE_KEY, 0x20e8, { KEY_MEDIA } },
143 { KE_KEY, 0x2142, { KEY_MEDIA } },
144 { KE_KEY, 0x213b, { KEY_INFO } },
145 { KE_KEY, 0x2169, { KEY_DIRECTION } },
146 { KE_KEY, 0x231b, { KEY_HELP } },
147 { KE_END, 0 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700148};
149
150static struct input_dev *hp_wmi_input_dev;
151static struct platform_device *hp_wmi_platform_dev;
152
153static struct rfkill *wifi_rfkill;
154static struct rfkill *bluetooth_rfkill;
155static struct rfkill *wwan_rfkill;
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200156static struct rfkill *gps_rfkill;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700157
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200158struct rfkill2_device {
159 u8 id;
160 int num;
161 struct rfkill *rfkill;
162};
163
164static int rfkill2_count;
165static struct rfkill2_device rfkill2[HPWMI_MAX_RFKILL2_DEVICES];
166
Thomas Renninger6d96e002010-05-21 16:42:40 +0200167/*
168 * hp_wmi_perform_query
169 *
170 * query: The commandtype -> What should be queried
171 * write: The command -> 0 read, 1 write, 3 ODM specific
172 * buffer: Buffer used as input and/or output
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200173 * insize: Size of input buffer
174 * outsize: Size of output buffer
Thomas Renninger6d96e002010-05-21 16:42:40 +0200175 *
176 * returns zero on success
177 * an HP WMI query specific error code (which is positive)
178 * -EINVAL if the query was not successful at all
179 * -EINVAL if the output buffer size exceeds buffersize
180 *
181 * Note: The buffersize must at least be the maximum of the input and output
182 * size. E.g. Battery info query (0x7) is defined to have 1 byte input
183 * and 128 byte output. The caller would do:
184 * buffer = kzalloc(128, GFP_KERNEL);
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200185 * ret = hp_wmi_perform_query(0x7, 0, buffer, 1, 128)
Thomas Renninger6d96e002010-05-21 16:42:40 +0200186 */
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200187static int hp_wmi_perform_query(int query, int write, void *buffer,
188 int insize, int outsize)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700189{
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200190 struct bios_return *bios_return;
191 int actual_outsize;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700192 union acpi_object *obj;
193 struct bios_args args = {
194 .signature = 0x55434553,
195 .command = write ? 0x2 : 0x1,
196 .commandtype = query,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200197 .datasize = insize,
198 .data = 0,
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700199 };
200 struct acpi_buffer input = { sizeof(struct bios_args), &args };
201 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Eric Dumazet04018462011-07-11 12:22:21 +0200202 u32 rc;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700203
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200204 if (WARN_ON(insize > sizeof(args.data)))
205 return -EINVAL;
206 memcpy(&args.data, buffer, insize);
207
Anssi Hannula25bb0672011-02-20 20:07:21 +0200208 wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700209
210 obj = output.pointer;
211
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100212 if (!obj)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700213 return -EINVAL;
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100214 else if (obj->type != ACPI_TYPE_BUFFER) {
215 kfree(obj);
216 return -EINVAL;
217 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700218
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200219 bios_return = (struct bios_return *)obj->buffer.pointer;
Eric Dumazet04018462011-07-11 12:22:21 +0200220 rc = bios_return->return_code;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200221
Eric Dumazet04018462011-07-11 12:22:21 +0200222 if (rc) {
223 if (rc != HPWMI_RET_UNKNOWN_CMDTYPE)
224 pr_warn("query 0x%x returned error 0x%x\n", query, rc);
Anssi Hannula9af0e0f2011-02-20 20:07:20 +0200225 kfree(obj);
Eric Dumazet04018462011-07-11 12:22:21 +0200226 return rc;
Anssi Hannula9af0e0f2011-02-20 20:07:20 +0200227 }
228
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200229 if (!outsize) {
230 /* ignore output data */
231 kfree(obj);
232 return 0;
233 }
Zeng Zhaoming53c96df2010-11-19 00:46:19 +0800234
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200235 actual_outsize = min(outsize, (int)(obj->buffer.length - sizeof(*bios_return)));
236 memcpy(buffer, obj->buffer.pointer + sizeof(*bios_return), actual_outsize);
237 memset(buffer + actual_outsize, 0, outsize - actual_outsize);
Zeng Zhaoming53c96df2010-11-19 00:46:19 +0800238 kfree(obj);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200239 return 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700240}
241
242static int hp_wmi_display_state(void)
243{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400244 int state = 0;
245 int ret = hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200246 sizeof(state), sizeof(state));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200247 if (ret)
248 return -EINVAL;
249 return state;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700250}
251
252static int hp_wmi_hddtemp_state(void)
253{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400254 int state = 0;
255 int ret = hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200256 sizeof(state), sizeof(state));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200257 if (ret)
258 return -EINVAL;
259 return state;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700260}
261
262static int hp_wmi_als_state(void)
263{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400264 int state = 0;
265 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200266 sizeof(state), sizeof(state));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200267 if (ret)
268 return -EINVAL;
269 return state;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700270}
271
272static int hp_wmi_dock_state(void)
273{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400274 int state = 0;
275 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200276 sizeof(state), sizeof(state));
Matthew Garrett871043b2009-06-01 15:25:45 +0100277
Thomas Renninger6d96e002010-05-21 16:42:40 +0200278 if (ret)
279 return -EINVAL;
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{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400286 int state = 0;
287 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200288 sizeof(state), sizeof(state));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200289 if (ret)
Matthew Garrett871043b2009-06-01 15:25:45 +0100290 return ret;
291
Thomas Renninger6d96e002010-05-21 16:42:40 +0200292 return (state & 0x4) ? 1 : 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700293}
294
Johannes Berg19d337d2009-06-02 13:01:37 +0200295static int hp_wmi_set_block(void *data, bool blocked)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700296{
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100297 enum hp_wmi_radio r = (enum hp_wmi_radio) data;
298 int query = BIT(r + 8) | ((!blocked) << r);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200299 int ret;
Johannes Berg19d337d2009-06-02 13:01:37 +0200300
Thomas Renninger6d96e002010-05-21 16:42:40 +0200301 ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200302 &query, sizeof(query), 0);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200303 if (ret)
304 return -EINVAL;
305 return 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700306}
307
Johannes Berg19d337d2009-06-02 13:01:37 +0200308static const struct rfkill_ops hp_wmi_rfkill_ops = {
309 .set_block = hp_wmi_set_block,
310};
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700311
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100312static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700313{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400314 int wireless = 0;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200315 int mask;
316 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200317 &wireless, sizeof(wireless),
318 sizeof(wireless));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200319 /* TBD: Pass error */
320
321 mask = 0x200 << (r * 8);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700322
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100323 if (wireless & mask)
Johannes Berg19d337d2009-06-02 13:01:37 +0200324 return false;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700325 else
Johannes Berg19d337d2009-06-02 13:01:37 +0200326 return true;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700327}
328
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100329static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700330{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400331 int wireless = 0;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200332 int mask;
333 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200334 &wireless, sizeof(wireless),
335 sizeof(wireless));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200336 /* TBD: Pass error */
337
338 mask = 0x800 << (r * 8);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700339
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100340 if (wireless & mask)
Johannes Berg19d337d2009-06-02 13:01:37 +0200341 return false;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700342 else
Johannes Berg19d337d2009-06-02 13:01:37 +0200343 return true;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700344}
345
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200346static int hp_wmi_rfkill2_set_block(void *data, bool blocked)
347{
348 int rfkill_id = (int)(long)data;
349 char buffer[4] = { 0x01, 0x00, rfkill_id, !blocked };
350
351 if (hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, 1,
352 buffer, sizeof(buffer), 0))
353 return -EINVAL;
354 return 0;
355}
356
357static const struct rfkill_ops hp_wmi_rfkill2_ops = {
358 .set_block = hp_wmi_rfkill2_set_block,
359};
360
361static int hp_wmi_rfkill2_refresh(void)
362{
363 int err, i;
364 struct bios_rfkill2_state state;
365
366 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, 0, &state,
367 0, sizeof(state));
368 if (err)
369 return err;
370
371 for (i = 0; i < rfkill2_count; i++) {
372 int num = rfkill2[i].num;
373 struct bios_rfkill2_device_state *devstate;
374 devstate = &state.device[num];
375
376 if (num >= state.count ||
377 devstate->rfkill_id != rfkill2[i].id) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700378 pr_warn("power configuration of the wireless devices unexpectedly changed\n");
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200379 continue;
380 }
381
382 rfkill_set_states(rfkill2[i].rfkill,
383 IS_SWBLOCKED(devstate->power),
384 IS_HWBLOCKED(devstate->power));
385 }
386
387 return 0;
388}
389
Alex Hung8667ca92013-06-13 16:46:25 +0800390static int hp_wmi_post_code_state(void)
391{
392 int state = 0;
393 int ret = hp_wmi_perform_query(HPWMI_POSTCODEERROR_QUERY, 0, &state,
394 sizeof(state), sizeof(state));
395 if (ret)
396 return -EINVAL;
397 return state;
398}
399
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700400static ssize_t show_display(struct device *dev, struct device_attribute *attr,
401 char *buf)
402{
403 int value = hp_wmi_display_state();
404 if (value < 0)
405 return -EINVAL;
406 return sprintf(buf, "%d\n", value);
407}
408
409static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
410 char *buf)
411{
412 int value = hp_wmi_hddtemp_state();
413 if (value < 0)
414 return -EINVAL;
415 return sprintf(buf, "%d\n", value);
416}
417
418static ssize_t show_als(struct device *dev, struct device_attribute *attr,
419 char *buf)
420{
421 int value = hp_wmi_als_state();
422 if (value < 0)
423 return -EINVAL;
424 return sprintf(buf, "%d\n", value);
425}
426
427static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
428 char *buf)
429{
430 int value = hp_wmi_dock_state();
431 if (value < 0)
432 return -EINVAL;
433 return sprintf(buf, "%d\n", value);
434}
435
Matthew Garrett871043b2009-06-01 15:25:45 +0100436static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
437 char *buf)
438{
439 int value = hp_wmi_tablet_state();
440 if (value < 0)
441 return -EINVAL;
442 return sprintf(buf, "%d\n", value);
443}
444
Alex Hung8667ca92013-06-13 16:46:25 +0800445static ssize_t show_postcode(struct device *dev, struct device_attribute *attr,
446 char *buf)
447{
448 /* Get the POST error code of previous boot failure. */
449 int value = hp_wmi_post_code_state();
450 if (value < 0)
451 return -EINVAL;
452 return sprintf(buf, "0x%x\n", value);
453}
454
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700455static ssize_t set_als(struct device *dev, struct device_attribute *attr,
456 const char *buf, size_t count)
457{
458 u32 tmp = simple_strtoul(buf, NULL, 10);
Matthew Garretta8ec1052010-08-23 15:52:34 -0400459 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, &tmp,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200460 sizeof(tmp), sizeof(tmp));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200461 if (ret)
462 return -EINVAL;
463
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700464 return count;
465}
466
Alex Hung8667ca92013-06-13 16:46:25 +0800467static ssize_t set_postcode(struct device *dev, struct device_attribute *attr,
468 const char *buf, size_t count)
469{
470 int ret;
471 u32 tmp;
472 long unsigned int tmp2;
473
474 ret = kstrtoul(buf, 10, &tmp2);
475 if (ret || tmp2 != 1)
476 return -EINVAL;
477
478 /* Clear the POST error code. It is kept until until cleared. */
479 tmp = (u32) tmp2;
480 ret = hp_wmi_perform_query(HPWMI_POSTCODEERROR_QUERY, 1, &tmp,
481 sizeof(tmp), sizeof(tmp));
482 if (ret)
483 return -EINVAL;
484
485 return count;
486}
487
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700488static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
489static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
490static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
491static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
Matthew Garrett871043b2009-06-01 15:25:45 +0100492static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
Alex Hung8667ca92013-06-13 16:46:25 +0800493static DEVICE_ATTR(postcode, S_IRUGO | S_IWUSR, show_postcode, set_postcode);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700494
Adrian Bunk88429a12008-10-15 22:05:17 -0700495static void hp_wmi_notify(u32 value, void *context)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700496{
497 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700498 union acpi_object *obj;
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200499 u32 event_id, event_data;
Matthew Garretta8ec1052010-08-23 15:52:34 -0400500 int key_code = 0, ret;
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200501 u32 *location;
Len Brownfda11e62009-12-26 23:02:24 -0500502 acpi_status status;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700503
Len Brownfda11e62009-12-26 23:02:24 -0500504 status = wmi_get_event_data(value, &response);
505 if (status != AE_OK) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700506 pr_info("bad event status 0x%x\n", status);
Len Brownfda11e62009-12-26 23:02:24 -0500507 return;
508 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700509
510 obj = (union acpi_object *)response.pointer;
511
Thomas Renningerc4775062010-07-29 12:27:59 +0200512 if (!obj)
513 return;
514 if (obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700515 pr_info("Unknown response received %d\n", obj->type);
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100516 kfree(obj);
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100517 return;
518 }
519
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200520 /*
521 * Depending on ACPI version the concatenation of id and event data
522 * inside _WED function will result in a 8 or 16 byte buffer.
523 */
524 location = (u32 *)obj->buffer.pointer;
525 if (obj->buffer.length == 8) {
526 event_id = *location;
527 event_data = *(location + 1);
528 } else if (obj->buffer.length == 16) {
529 event_id = *location;
530 event_data = *(location + 2);
531 } else {
Joe Perchesb5a42232011-03-29 15:21:41 -0700532 pr_info("Unknown buffer length %d\n", obj->buffer.length);
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200533 kfree(obj);
534 return;
535 }
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100536 kfree(obj);
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200537
538 switch (event_id) {
Thomas Renninger751ae802010-05-21 16:18:09 +0200539 case HPWMI_DOCK_EVENT:
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100540 input_report_switch(hp_wmi_input_dev, SW_DOCK,
541 hp_wmi_dock_state());
542 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
543 hp_wmi_tablet_state());
544 input_sync(hp_wmi_input_dev);
Thomas Renninger751ae802010-05-21 16:18:09 +0200545 break;
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200546 case HPWMI_PARK_HDD:
547 break;
548 case HPWMI_SMART_ADAPTER:
549 break;
Thomas Renninger751ae802010-05-21 16:18:09 +0200550 case HPWMI_BEZEL_BUTTON:
Thomas Renninger6d96e002010-05-21 16:42:40 +0200551 ret = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
Matthew Garretta8ec1052010-08-23 15:52:34 -0400552 &key_code,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200553 sizeof(key_code),
Thomas Renninger6d96e002010-05-21 16:42:40 +0200554 sizeof(key_code));
555 if (ret)
556 break;
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700557
558 if (!sparse_keymap_report_event(hp_wmi_input_dev,
559 key_code, 1, true))
Joe Perchesb5a42232011-03-29 15:21:41 -0700560 pr_info("Unknown key code - 0x%x\n", key_code);
Thomas Renninger751ae802010-05-21 16:18:09 +0200561 break;
562 case HPWMI_WIRELESS:
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200563 if (rfkill2_count) {
564 hp_wmi_rfkill2_refresh();
565 break;
566 }
567
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100568 if (wifi_rfkill)
569 rfkill_set_states(wifi_rfkill,
570 hp_wmi_get_sw_state(HPWMI_WIFI),
571 hp_wmi_get_hw_state(HPWMI_WIFI));
572 if (bluetooth_rfkill)
573 rfkill_set_states(bluetooth_rfkill,
574 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
575 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
576 if (wwan_rfkill)
577 rfkill_set_states(wwan_rfkill,
578 hp_wmi_get_sw_state(HPWMI_WWAN),
579 hp_wmi_get_hw_state(HPWMI_WWAN));
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200580 if (gps_rfkill)
581 rfkill_set_states(gps_rfkill,
582 hp_wmi_get_sw_state(HPWMI_GPS),
583 hp_wmi_get_hw_state(HPWMI_GPS));
Thomas Renninger751ae802010-05-21 16:18:09 +0200584 break;
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200585 case HPWMI_CPU_BATTERY_THROTTLE:
Joe Perchesb5a42232011-03-29 15:21:41 -0700586 pr_info("Unimplemented CPU throttle because of 3 Cell battery event detected\n");
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200587 break;
588 case HPWMI_LOCK_SWITCH:
589 break;
Alex Hungd9e290a2013-03-15 17:18:22 +0800590 case HPWMI_LID_SWITCH:
591 break;
592 case HPWMI_SCREEN_ROTATION:
593 break;
594 case HPWMI_COOLSENSE_SYSTEM_MOBILE:
595 break;
596 case HPWMI_COOLSENSE_SYSTEM_HOT:
597 break;
598 case HPWMI_PROXIMITY_SENSOR:
599 break;
600 case HPWMI_BACKLIT_KB_BRIGHTNESS:
601 break;
602 case HPWMI_PEAKSHIFT_PERIOD:
603 break;
604 case HPWMI_BATTERY_CHARGE_PERIOD:
605 break;
Thomas Renninger751ae802010-05-21 16:18:09 +0200606 default:
Joe Perchesb5a42232011-03-29 15:21:41 -0700607 pr_info("Unknown event_id - %d - 0x%x\n", event_id, event_data);
Thomas Renninger751ae802010-05-21 16:18:09 +0200608 break;
609 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700610}
611
612static int __init hp_wmi_input_setup(void)
613{
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700614 acpi_status status;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700615 int err;
616
617 hp_wmi_input_dev = input_allocate_device();
Axel Linbc285962010-07-20 15:19:51 -0700618 if (!hp_wmi_input_dev)
619 return -ENOMEM;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700620
621 hp_wmi_input_dev->name = "HP WMI hotkeys";
622 hp_wmi_input_dev->phys = "wmi/input0";
623 hp_wmi_input_dev->id.bustype = BUS_HOST;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700624
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700625 __set_bit(EV_SW, hp_wmi_input_dev->evbit);
626 __set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
627 __set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700628
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700629 err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
630 if (err)
631 goto err_free_dev;
Matthew Garrett871043b2009-06-01 15:25:45 +0100632
633 /* Set initial hardware state */
634 input_report_switch(hp_wmi_input_dev, SW_DOCK, hp_wmi_dock_state());
635 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
636 hp_wmi_tablet_state());
637 input_sync(hp_wmi_input_dev);
638
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700639 status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
640 if (ACPI_FAILURE(status)) {
641 err = -EIO;
642 goto err_free_keymap;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700643 }
644
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700645 err = input_register_device(hp_wmi_input_dev);
646 if (err)
647 goto err_uninstall_notifier;
648
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700649 return 0;
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700650
651 err_uninstall_notifier:
652 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
653 err_free_keymap:
654 sparse_keymap_free(hp_wmi_input_dev);
655 err_free_dev:
656 input_free_device(hp_wmi_input_dev);
657 return err;
658}
659
660static void hp_wmi_input_destroy(void)
661{
662 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
663 sparse_keymap_free(hp_wmi_input_dev);
664 input_unregister_device(hp_wmi_input_dev);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700665}
666
667static void cleanup_sysfs(struct platform_device *device)
668{
669 device_remove_file(&device->dev, &dev_attr_display);
670 device_remove_file(&device->dev, &dev_attr_hddtemp);
671 device_remove_file(&device->dev, &dev_attr_als);
672 device_remove_file(&device->dev, &dev_attr_dock);
Matthew Garrett871043b2009-06-01 15:25:45 +0100673 device_remove_file(&device->dev, &dev_attr_tablet);
Alex Hung8667ca92013-06-13 16:46:25 +0800674 device_remove_file(&device->dev, &dev_attr_postcode);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700675}
676
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800677static int hp_wmi_rfkill_setup(struct platform_device *device)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700678{
679 int err;
Matthew Garretta8ec1052010-08-23 15:52:34 -0400680 int wireless = 0;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200681
Matthew Garretta8ec1052010-08-23 15:52:34 -0400682 err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, &wireless,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200683 sizeof(wireless), sizeof(wireless));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200684 if (err)
685 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700686
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700687 if (wireless & 0x1) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200688 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
689 RFKILL_TYPE_WLAN,
690 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100691 (void *) HPWMI_WIFI);
Dan Carpenterdd258c02012-05-17 09:48:12 +0300692 if (!wifi_rfkill)
693 return -ENOMEM;
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100694 rfkill_init_sw_state(wifi_rfkill,
695 hp_wmi_get_sw_state(HPWMI_WIFI));
696 rfkill_set_hw_state(wifi_rfkill,
697 hp_wmi_get_hw_state(HPWMI_WIFI));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800698 err = rfkill_register(wifi_rfkill);
699 if (err)
Johannes Berg19d337d2009-06-02 13:01:37 +0200700 goto register_wifi_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700701 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700702
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700703 if (wireless & 0x2) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200704 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
705 RFKILL_TYPE_BLUETOOTH,
706 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100707 (void *) HPWMI_BLUETOOTH);
Dan Carpenterdd258c02012-05-17 09:48:12 +0300708 if (!bluetooth_rfkill) {
709 err = -ENOMEM;
710 goto register_wifi_error;
711 }
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100712 rfkill_init_sw_state(bluetooth_rfkill,
713 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
714 rfkill_set_hw_state(bluetooth_rfkill,
715 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800716 err = rfkill_register(bluetooth_rfkill);
Frans Pop6989d562009-01-29 14:25:14 -0800717 if (err)
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800718 goto register_bluetooth_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 & 0x4) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200722 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
723 RFKILL_TYPE_WWAN,
724 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100725 (void *) HPWMI_WWAN);
Dan Carpenterdd258c02012-05-17 09:48:12 +0300726 if (!wwan_rfkill) {
727 err = -ENOMEM;
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200728 goto register_gps_error;
Dan Carpenterdd258c02012-05-17 09:48:12 +0300729 }
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100730 rfkill_init_sw_state(wwan_rfkill,
731 hp_wmi_get_sw_state(HPWMI_WWAN));
732 rfkill_set_hw_state(wwan_rfkill,
733 hp_wmi_get_hw_state(HPWMI_WWAN));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800734 err = rfkill_register(wwan_rfkill);
735 if (err)
736 goto register_wwan_err;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700737 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700738
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200739 if (wireless & 0x8) {
740 gps_rfkill = rfkill_alloc("hp-gps", &device->dev,
741 RFKILL_TYPE_GPS,
742 &hp_wmi_rfkill_ops,
743 (void *) HPWMI_GPS);
744 if (!gps_rfkill) {
745 err = -ENOMEM;
746 goto register_bluetooth_error;
747 }
748 rfkill_init_sw_state(gps_rfkill,
749 hp_wmi_get_sw_state(HPWMI_GPS));
lan,Tianyuaf1d4862013-05-28 02:25:33 +0000750 rfkill_set_hw_state(gps_rfkill,
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200751 hp_wmi_get_hw_state(HPWMI_GPS));
752 err = rfkill_register(gps_rfkill);
753 if (err)
754 goto register_gps_error;
755 }
756
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700757 return 0;
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800758register_wwan_err:
Johannes Berg19d337d2009-06-02 13:01:37 +0200759 rfkill_destroy(wwan_rfkill);
Anssi Hannula6d97db52011-02-20 20:07:24 +0200760 wwan_rfkill = NULL;
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200761 if (gps_rfkill)
762 rfkill_unregister(gps_rfkill);
763register_gps_error:
764 rfkill_destroy(gps_rfkill);
765 gps_rfkill = NULL;
Andrew Morton44f06062009-02-04 15:12:07 -0800766 if (bluetooth_rfkill)
767 rfkill_unregister(bluetooth_rfkill);
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800768register_bluetooth_error:
Johannes Berg19d337d2009-06-02 13:01:37 +0200769 rfkill_destroy(bluetooth_rfkill);
Anssi Hannula6d97db52011-02-20 20:07:24 +0200770 bluetooth_rfkill = NULL;
Andrew Morton44f06062009-02-04 15:12:07 -0800771 if (wifi_rfkill)
772 rfkill_unregister(wifi_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200773register_wifi_error:
774 rfkill_destroy(wifi_rfkill);
Anssi Hannula6d97db52011-02-20 20:07:24 +0200775 wifi_rfkill = NULL;
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200776 return err;
777}
778
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800779static int hp_wmi_rfkill2_setup(struct platform_device *device)
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200780{
781 int err, i;
782 struct bios_rfkill2_state state;
783 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, 0, &state,
784 0, sizeof(state));
785 if (err)
786 return err;
787
788 if (state.count > HPWMI_MAX_RFKILL2_DEVICES) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700789 pr_warn("unable to parse 0x1b query output\n");
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200790 return -EINVAL;
791 }
792
793 for (i = 0; i < state.count; i++) {
794 struct rfkill *rfkill;
795 enum rfkill_type type;
796 char *name;
797 switch (state.device[i].radio_type) {
798 case HPWMI_WIFI:
799 type = RFKILL_TYPE_WLAN;
800 name = "hp-wifi";
801 break;
802 case HPWMI_BLUETOOTH:
803 type = RFKILL_TYPE_BLUETOOTH;
804 name = "hp-bluetooth";
805 break;
806 case HPWMI_WWAN:
807 type = RFKILL_TYPE_WWAN;
808 name = "hp-wwan";
809 break;
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200810 case HPWMI_GPS:
811 type = RFKILL_TYPE_GPS;
812 name = "hp-gps";
813 break;
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200814 default:
Joe Perchesb5a42232011-03-29 15:21:41 -0700815 pr_warn("unknown device type 0x%x\n",
816 state.device[i].radio_type);
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200817 continue;
818 }
819
820 if (!state.device[i].vendor_id) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700821 pr_warn("zero device %d while %d reported\n",
822 i, state.count);
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200823 continue;
824 }
825
826 rfkill = rfkill_alloc(name, &device->dev, type,
827 &hp_wmi_rfkill2_ops, (void *)(long)i);
828 if (!rfkill) {
829 err = -ENOMEM;
830 goto fail;
831 }
832
833 rfkill2[rfkill2_count].id = state.device[i].rfkill_id;
834 rfkill2[rfkill2_count].num = i;
835 rfkill2[rfkill2_count].rfkill = rfkill;
836
837 rfkill_init_sw_state(rfkill,
838 IS_SWBLOCKED(state.device[i].power));
839 rfkill_set_hw_state(rfkill,
840 IS_HWBLOCKED(state.device[i].power));
841
842 if (!(state.device[i].power & HPWMI_POWER_BIOS))
Joe Perchesb5a42232011-03-29 15:21:41 -0700843 pr_info("device %s blocked by BIOS\n", name);
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200844
845 err = rfkill_register(rfkill);
846 if (err) {
847 rfkill_destroy(rfkill);
848 goto fail;
849 }
850
851 rfkill2_count++;
852 }
853
854 return 0;
855fail:
856 for (; rfkill2_count > 0; rfkill2_count--) {
857 rfkill_unregister(rfkill2[rfkill2_count - 1].rfkill);
858 rfkill_destroy(rfkill2[rfkill2_count - 1].rfkill);
859 }
860 return err;
861}
862
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800863static int __init hp_wmi_bios_setup(struct platform_device *device)
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200864{
865 int err;
866
Anssi Hannula6d97db52011-02-20 20:07:24 +0200867 /* clear detected rfkill devices */
868 wifi_rfkill = NULL;
869 bluetooth_rfkill = NULL;
870 wwan_rfkill = NULL;
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200871 gps_rfkill = NULL;
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200872 rfkill2_count = 0;
Anssi Hannula6d97db52011-02-20 20:07:24 +0200873
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200874 if (hp_wmi_rfkill_setup(device))
875 hp_wmi_rfkill2_setup(device);
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200876
877 err = device_create_file(&device->dev, &dev_attr_display);
878 if (err)
879 goto add_sysfs_error;
880 err = device_create_file(&device->dev, &dev_attr_hddtemp);
881 if (err)
882 goto add_sysfs_error;
883 err = device_create_file(&device->dev, &dev_attr_als);
884 if (err)
885 goto add_sysfs_error;
886 err = device_create_file(&device->dev, &dev_attr_dock);
887 if (err)
888 goto add_sysfs_error;
889 err = device_create_file(&device->dev, &dev_attr_tablet);
890 if (err)
891 goto add_sysfs_error;
Alex Hung8667ca92013-06-13 16:46:25 +0800892 err = device_create_file(&device->dev, &dev_attr_postcode);
893 if (err)
894 goto add_sysfs_error;
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200895 return 0;
896
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700897add_sysfs_error:
898 cleanup_sysfs(device);
899 return err;
900}
901
902static int __exit hp_wmi_bios_remove(struct platform_device *device)
903{
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200904 int i;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700905 cleanup_sysfs(device);
906
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200907 for (i = 0; i < rfkill2_count; i++) {
908 rfkill_unregister(rfkill2[i].rfkill);
909 rfkill_destroy(rfkill2[i].rfkill);
910 }
911
Johannes Berg19d337d2009-06-02 13:01:37 +0200912 if (wifi_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700913 rfkill_unregister(wifi_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200914 rfkill_destroy(wifi_rfkill);
915 }
916 if (bluetooth_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700917 rfkill_unregister(bluetooth_rfkill);
Corentin Chary09729f02009-09-14 12:43:51 +0200918 rfkill_destroy(bluetooth_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200919 }
920 if (wwan_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700921 rfkill_unregister(wwan_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200922 rfkill_destroy(wwan_rfkill);
923 }
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200924 if (gps_rfkill) {
925 rfkill_unregister(gps_rfkill);
926 rfkill_destroy(gps_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) {
941 input_report_switch(hp_wmi_input_dev, SW_DOCK,
942 hp_wmi_dock_state());
943 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
944 hp_wmi_tablet_state());
945 input_sync(hp_wmi_input_dev);
946 }
Frans Pop4c395bd2009-03-04 11:55:28 -0800947
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200948 if (rfkill2_count)
949 hp_wmi_rfkill2_refresh();
950
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100951 if (wifi_rfkill)
952 rfkill_set_states(wifi_rfkill,
953 hp_wmi_get_sw_state(HPWMI_WIFI),
954 hp_wmi_get_hw_state(HPWMI_WIFI));
955 if (bluetooth_rfkill)
956 rfkill_set_states(bluetooth_rfkill,
957 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
958 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
959 if (wwan_rfkill)
960 rfkill_set_states(wwan_rfkill,
961 hp_wmi_get_sw_state(HPWMI_WWAN),
962 hp_wmi_get_hw_state(HPWMI_WWAN));
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200963 if (gps_rfkill)
964 rfkill_set_states(gps_rfkill,
965 hp_wmi_get_sw_state(HPWMI_GPS),
966 hp_wmi_get_hw_state(HPWMI_GPS));
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100967
Frans Pop4c395bd2009-03-04 11:55:28 -0800968 return 0;
969}
970
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800971static const struct dev_pm_ops hp_wmi_pm_ops = {
972 .resume = hp_wmi_resume_handler,
973 .restore = hp_wmi_resume_handler,
974};
975
976static struct platform_driver hp_wmi_driver = {
977 .driver = {
978 .name = "hp-wmi",
979 .owner = THIS_MODULE,
980 .pm = &hp_wmi_pm_ops,
981 },
982 .remove = __exit_p(hp_wmi_bios_remove),
983};
984
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700985static int __init hp_wmi_init(void)
986{
987 int err;
Thomas Renningerb0966672010-07-20 15:19:29 -0700988 int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
989 int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700990
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800991 if (!bios_capable && !event_capable)
992 return -ENODEV;
993
Thomas Renningerb0966672010-07-20 15:19:29 -0700994 if (event_capable) {
Axel Lindfec5c42010-06-10 16:06:40 +0800995 err = hp_wmi_input_setup();
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700996 if (err)
Axel Lindfec5c42010-06-10 16:06:40 +0800997 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700998 }
999
Thomas Renningerb0966672010-07-20 15:19:29 -07001000 if (bios_capable) {
Dmitry Torokhovc165b802013-02-20 00:44:34 -08001001 hp_wmi_platform_dev =
1002 platform_device_register_simple("hp-wmi", -1, NULL, 0);
1003 if (IS_ERR(hp_wmi_platform_dev)) {
1004 err = PTR_ERR(hp_wmi_platform_dev);
1005 goto err_destroy_input;
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001006 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001007
Dmitry Torokhovc165b802013-02-20 00:44:34 -08001008 err = platform_driver_probe(&hp_wmi_driver, hp_wmi_bios_setup);
1009 if (err)
1010 goto err_unregister_device;
1011 }
Thomas Renningerb0966672010-07-20 15:19:29 -07001012
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001013 return 0;
Axel Lindfec5c42010-06-10 16:06:40 +08001014
Dmitry Torokhovc165b802013-02-20 00:44:34 -08001015err_unregister_device:
1016 platform_device_unregister(hp_wmi_platform_dev);
1017err_destroy_input:
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -07001018 if (event_capable)
1019 hp_wmi_input_destroy();
Axel Lindfec5c42010-06-10 16:06:40 +08001020
1021 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001022}
Dmitry Torokhovc165b802013-02-20 00:44:34 -08001023module_init(hp_wmi_init);
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001024
1025static void __exit hp_wmi_exit(void)
1026{
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -07001027 if (wmi_has_guid(HPWMI_EVENT_GUID))
1028 hp_wmi_input_destroy();
1029
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001030 if (hp_wmi_platform_dev) {
Axel Lin97ba0af2010-06-03 15:18:03 +08001031 platform_device_unregister(hp_wmi_platform_dev);
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001032 platform_driver_unregister(&hp_wmi_driver);
1033 }
1034}
Matthew Garrett62ec30d2008-07-25 01:45:39 -07001035module_exit(hp_wmi_exit);