blob: d111c8687f9bd2d370b52127074c3b7dabb7058e [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
Matthew Garrett62ec30d2008-07-25 01:45:39 -070058
Alan Jenkinse5fbba82009-07-21 12:14:01 +010059enum hp_wmi_radio {
60 HPWMI_WIFI = 0,
61 HPWMI_BLUETOOTH = 1,
62 HPWMI_WWAN = 2,
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +020063 HPWMI_GPS = 3,
Alan Jenkinse5fbba82009-07-21 12:14:01 +010064};
65
Thomas Renninger751ae802010-05-21 16:18:09 +020066enum hp_wmi_event_ids {
67 HPWMI_DOCK_EVENT = 1,
Thomas Renninger1bbdfd52010-05-21 16:18:13 +020068 HPWMI_PARK_HDD = 2,
69 HPWMI_SMART_ADAPTER = 3,
Thomas Renninger751ae802010-05-21 16:18:09 +020070 HPWMI_BEZEL_BUTTON = 4,
71 HPWMI_WIRELESS = 5,
Thomas Renninger1bbdfd52010-05-21 16:18:13 +020072 HPWMI_CPU_BATTERY_THROTTLE = 6,
73 HPWMI_LOCK_SWITCH = 7,
Alex Hungd9e290a2013-03-15 17:18:22 +080074 HPWMI_LID_SWITCH = 8,
75 HPWMI_SCREEN_ROTATION = 9,
76 HPWMI_COOLSENSE_SYSTEM_MOBILE = 0x0A,
77 HPWMI_COOLSENSE_SYSTEM_HOT = 0x0B,
78 HPWMI_PROXIMITY_SENSOR = 0x0C,
79 HPWMI_BACKLIT_KB_BRIGHTNESS = 0x0D,
80 HPWMI_PEAKSHIFT_PERIOD = 0x0F,
81 HPWMI_BATTERY_CHARGE_PERIOD = 0x10,
Thomas Renninger751ae802010-05-21 16:18:09 +020082};
83
Matthew Garrett62ec30d2008-07-25 01:45:39 -070084struct bios_args {
85 u32 signature;
86 u32 command;
87 u32 commandtype;
88 u32 datasize;
Matthew Garretta8ec1052010-08-23 15:52:34 -040089 u32 data;
Matthew Garrett62ec30d2008-07-25 01:45:39 -070090};
91
92struct bios_return {
93 u32 sigpass;
94 u32 return_code;
Matthew Garrett62ec30d2008-07-25 01:45:39 -070095};
96
Anssi Hannula9af0e0f2011-02-20 20:07:20 +020097enum hp_return_value {
98 HPWMI_RET_WRONG_SIGNATURE = 0x02,
99 HPWMI_RET_UNKNOWN_COMMAND = 0x03,
100 HPWMI_RET_UNKNOWN_CMDTYPE = 0x04,
101 HPWMI_RET_INVALID_PARAMETERS = 0x05,
102};
103
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200104enum hp_wireless2_bits {
105 HPWMI_POWER_STATE = 0x01,
106 HPWMI_POWER_SOFT = 0x02,
107 HPWMI_POWER_BIOS = 0x04,
108 HPWMI_POWER_HARD = 0x08,
109};
110
111#define IS_HWBLOCKED(x) ((x & (HPWMI_POWER_BIOS | HPWMI_POWER_HARD)) \
112 != (HPWMI_POWER_BIOS | HPWMI_POWER_HARD))
113#define IS_SWBLOCKED(x) !(x & HPWMI_POWER_SOFT)
114
115struct bios_rfkill2_device_state {
116 u8 radio_type;
117 u8 bus_type;
118 u16 vendor_id;
119 u16 product_id;
120 u16 subsys_vendor_id;
121 u16 subsys_product_id;
122 u8 rfkill_id;
123 u8 power;
124 u8 unknown[4];
125};
126
127/* 7 devices fit into the 128 byte buffer */
128#define HPWMI_MAX_RFKILL2_DEVICES 7
129
130struct bios_rfkill2_state {
131 u8 unknown[7];
132 u8 count;
133 u8 pad[8];
134 struct bios_rfkill2_device_state device[HPWMI_MAX_RFKILL2_DEVICES];
135};
136
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700137static const struct key_entry hp_wmi_keymap[] = {
138 { KE_KEY, 0x02, { KEY_BRIGHTNESSUP } },
139 { KE_KEY, 0x03, { KEY_BRIGHTNESSDOWN } },
140 { KE_KEY, 0x20e6, { KEY_PROG1 } },
141 { KE_KEY, 0x20e8, { KEY_MEDIA } },
142 { KE_KEY, 0x2142, { KEY_MEDIA } },
143 { KE_KEY, 0x213b, { KEY_INFO } },
144 { KE_KEY, 0x2169, { KEY_DIRECTION } },
145 { KE_KEY, 0x231b, { KEY_HELP } },
146 { KE_END, 0 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700147};
148
149static struct input_dev *hp_wmi_input_dev;
150static struct platform_device *hp_wmi_platform_dev;
151
152static struct rfkill *wifi_rfkill;
153static struct rfkill *bluetooth_rfkill;
154static struct rfkill *wwan_rfkill;
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200155static struct rfkill *gps_rfkill;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700156
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200157struct rfkill2_device {
158 u8 id;
159 int num;
160 struct rfkill *rfkill;
161};
162
163static int rfkill2_count;
164static struct rfkill2_device rfkill2[HPWMI_MAX_RFKILL2_DEVICES];
165
Thomas Renninger6d96e002010-05-21 16:42:40 +0200166/*
167 * hp_wmi_perform_query
168 *
169 * query: The commandtype -> What should be queried
170 * write: The command -> 0 read, 1 write, 3 ODM specific
171 * buffer: Buffer used as input and/or output
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200172 * insize: Size of input buffer
173 * outsize: Size of output buffer
Thomas Renninger6d96e002010-05-21 16:42:40 +0200174 *
175 * returns zero on success
176 * an HP WMI query specific error code (which is positive)
177 * -EINVAL if the query was not successful at all
178 * -EINVAL if the output buffer size exceeds buffersize
179 *
180 * Note: The buffersize must at least be the maximum of the input and output
181 * size. E.g. Battery info query (0x7) is defined to have 1 byte input
182 * and 128 byte output. The caller would do:
183 * buffer = kzalloc(128, GFP_KERNEL);
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200184 * ret = hp_wmi_perform_query(0x7, 0, buffer, 1, 128)
Thomas Renninger6d96e002010-05-21 16:42:40 +0200185 */
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200186static int hp_wmi_perform_query(int query, int write, void *buffer,
187 int insize, int outsize)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700188{
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200189 struct bios_return *bios_return;
190 int actual_outsize;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700191 union acpi_object *obj;
192 struct bios_args args = {
193 .signature = 0x55434553,
194 .command = write ? 0x2 : 0x1,
195 .commandtype = query,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200196 .datasize = insize,
197 .data = 0,
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700198 };
199 struct acpi_buffer input = { sizeof(struct bios_args), &args };
200 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Eric Dumazet04018462011-07-11 12:22:21 +0200201 u32 rc;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700202
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200203 if (WARN_ON(insize > sizeof(args.data)))
204 return -EINVAL;
205 memcpy(&args.data, buffer, insize);
206
Anssi Hannula25bb0672011-02-20 20:07:21 +0200207 wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700208
209 obj = output.pointer;
210
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100211 if (!obj)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700212 return -EINVAL;
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100213 else if (obj->type != ACPI_TYPE_BUFFER) {
214 kfree(obj);
215 return -EINVAL;
216 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700217
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200218 bios_return = (struct bios_return *)obj->buffer.pointer;
Eric Dumazet04018462011-07-11 12:22:21 +0200219 rc = bios_return->return_code;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200220
Eric Dumazet04018462011-07-11 12:22:21 +0200221 if (rc) {
222 if (rc != HPWMI_RET_UNKNOWN_CMDTYPE)
223 pr_warn("query 0x%x returned error 0x%x\n", query, rc);
Anssi Hannula9af0e0f2011-02-20 20:07:20 +0200224 kfree(obj);
Eric Dumazet04018462011-07-11 12:22:21 +0200225 return rc;
Anssi Hannula9af0e0f2011-02-20 20:07:20 +0200226 }
227
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200228 if (!outsize) {
229 /* ignore output data */
230 kfree(obj);
231 return 0;
232 }
Zeng Zhaoming53c96df2010-11-19 00:46:19 +0800233
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200234 actual_outsize = min(outsize, (int)(obj->buffer.length - sizeof(*bios_return)));
235 memcpy(buffer, obj->buffer.pointer + sizeof(*bios_return), actual_outsize);
236 memset(buffer + actual_outsize, 0, outsize - actual_outsize);
Zeng Zhaoming53c96df2010-11-19 00:46:19 +0800237 kfree(obj);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200238 return 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700239}
240
241static int hp_wmi_display_state(void)
242{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400243 int state = 0;
244 int ret = hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200245 sizeof(state), sizeof(state));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200246 if (ret)
247 return -EINVAL;
248 return state;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700249}
250
251static int hp_wmi_hddtemp_state(void)
252{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400253 int state = 0;
254 int ret = hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200255 sizeof(state), sizeof(state));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200256 if (ret)
257 return -EINVAL;
258 return state;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700259}
260
261static int hp_wmi_als_state(void)
262{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400263 int state = 0;
264 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200265 sizeof(state), sizeof(state));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200266 if (ret)
267 return -EINVAL;
268 return state;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700269}
270
271static int hp_wmi_dock_state(void)
272{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400273 int state = 0;
274 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200275 sizeof(state), sizeof(state));
Matthew Garrett871043b2009-06-01 15:25:45 +0100276
Thomas Renninger6d96e002010-05-21 16:42:40 +0200277 if (ret)
278 return -EINVAL;
Matthew Garrett871043b2009-06-01 15:25:45 +0100279
Thomas Renninger6d96e002010-05-21 16:42:40 +0200280 return state & 0x1;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700281}
282
Matthew Garrett871043b2009-06-01 15:25:45 +0100283static int hp_wmi_tablet_state(void)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700284{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400285 int state = 0;
286 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200287 sizeof(state), sizeof(state));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200288 if (ret)
Matthew Garrett871043b2009-06-01 15:25:45 +0100289 return ret;
290
Thomas Renninger6d96e002010-05-21 16:42:40 +0200291 return (state & 0x4) ? 1 : 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700292}
293
Johannes Berg19d337d2009-06-02 13:01:37 +0200294static int hp_wmi_set_block(void *data, bool blocked)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700295{
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100296 enum hp_wmi_radio r = (enum hp_wmi_radio) data;
297 int query = BIT(r + 8) | ((!blocked) << r);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200298 int ret;
Johannes Berg19d337d2009-06-02 13:01:37 +0200299
Thomas Renninger6d96e002010-05-21 16:42:40 +0200300 ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200301 &query, sizeof(query), 0);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200302 if (ret)
303 return -EINVAL;
304 return 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700305}
306
Johannes Berg19d337d2009-06-02 13:01:37 +0200307static const struct rfkill_ops hp_wmi_rfkill_ops = {
308 .set_block = hp_wmi_set_block,
309};
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700310
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100311static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700312{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400313 int wireless = 0;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200314 int mask;
315 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200316 &wireless, sizeof(wireless),
317 sizeof(wireless));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200318 /* TBD: Pass error */
319
320 mask = 0x200 << (r * 8);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700321
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100322 if (wireless & mask)
Johannes Berg19d337d2009-06-02 13:01:37 +0200323 return false;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700324 else
Johannes Berg19d337d2009-06-02 13:01:37 +0200325 return true;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700326}
327
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100328static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700329{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400330 int wireless = 0;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200331 int mask;
332 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200333 &wireless, sizeof(wireless),
334 sizeof(wireless));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200335 /* TBD: Pass error */
336
337 mask = 0x800 << (r * 8);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700338
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100339 if (wireless & mask)
Johannes Berg19d337d2009-06-02 13:01:37 +0200340 return false;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700341 else
Johannes Berg19d337d2009-06-02 13:01:37 +0200342 return true;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700343}
344
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200345static int hp_wmi_rfkill2_set_block(void *data, bool blocked)
346{
347 int rfkill_id = (int)(long)data;
348 char buffer[4] = { 0x01, 0x00, rfkill_id, !blocked };
349
350 if (hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, 1,
351 buffer, sizeof(buffer), 0))
352 return -EINVAL;
353 return 0;
354}
355
356static const struct rfkill_ops hp_wmi_rfkill2_ops = {
357 .set_block = hp_wmi_rfkill2_set_block,
358};
359
360static int hp_wmi_rfkill2_refresh(void)
361{
362 int err, i;
363 struct bios_rfkill2_state state;
364
365 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, 0, &state,
366 0, sizeof(state));
367 if (err)
368 return err;
369
370 for (i = 0; i < rfkill2_count; i++) {
371 int num = rfkill2[i].num;
372 struct bios_rfkill2_device_state *devstate;
373 devstate = &state.device[num];
374
375 if (num >= state.count ||
376 devstate->rfkill_id != rfkill2[i].id) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700377 pr_warn("power configuration of the wireless devices unexpectedly changed\n");
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200378 continue;
379 }
380
381 rfkill_set_states(rfkill2[i].rfkill,
382 IS_SWBLOCKED(devstate->power),
383 IS_HWBLOCKED(devstate->power));
384 }
385
386 return 0;
387}
388
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700389static ssize_t show_display(struct device *dev, struct device_attribute *attr,
390 char *buf)
391{
392 int value = hp_wmi_display_state();
393 if (value < 0)
394 return -EINVAL;
395 return sprintf(buf, "%d\n", value);
396}
397
398static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
399 char *buf)
400{
401 int value = hp_wmi_hddtemp_state();
402 if (value < 0)
403 return -EINVAL;
404 return sprintf(buf, "%d\n", value);
405}
406
407static ssize_t show_als(struct device *dev, struct device_attribute *attr,
408 char *buf)
409{
410 int value = hp_wmi_als_state();
411 if (value < 0)
412 return -EINVAL;
413 return sprintf(buf, "%d\n", value);
414}
415
416static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
417 char *buf)
418{
419 int value = hp_wmi_dock_state();
420 if (value < 0)
421 return -EINVAL;
422 return sprintf(buf, "%d\n", value);
423}
424
Matthew Garrett871043b2009-06-01 15:25:45 +0100425static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
426 char *buf)
427{
428 int value = hp_wmi_tablet_state();
429 if (value < 0)
430 return -EINVAL;
431 return sprintf(buf, "%d\n", value);
432}
433
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700434static ssize_t set_als(struct device *dev, struct device_attribute *attr,
435 const char *buf, size_t count)
436{
437 u32 tmp = simple_strtoul(buf, NULL, 10);
Matthew Garretta8ec1052010-08-23 15:52:34 -0400438 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, &tmp,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200439 sizeof(tmp), sizeof(tmp));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200440 if (ret)
441 return -EINVAL;
442
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700443 return count;
444}
445
446static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
447static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
448static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
449static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
Matthew Garrett871043b2009-06-01 15:25:45 +0100450static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700451
Adrian Bunk88429a12008-10-15 22:05:17 -0700452static void hp_wmi_notify(u32 value, void *context)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700453{
454 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700455 union acpi_object *obj;
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200456 u32 event_id, event_data;
Matthew Garretta8ec1052010-08-23 15:52:34 -0400457 int key_code = 0, ret;
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200458 u32 *location;
Len Brownfda11e62009-12-26 23:02:24 -0500459 acpi_status status;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700460
Len Brownfda11e62009-12-26 23:02:24 -0500461 status = wmi_get_event_data(value, &response);
462 if (status != AE_OK) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700463 pr_info("bad event status 0x%x\n", status);
Len Brownfda11e62009-12-26 23:02:24 -0500464 return;
465 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700466
467 obj = (union acpi_object *)response.pointer;
468
Thomas Renningerc4775062010-07-29 12:27:59 +0200469 if (!obj)
470 return;
471 if (obj->type != ACPI_TYPE_BUFFER) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700472 pr_info("Unknown response received %d\n", obj->type);
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100473 kfree(obj);
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100474 return;
475 }
476
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200477 /*
478 * Depending on ACPI version the concatenation of id and event data
479 * inside _WED function will result in a 8 or 16 byte buffer.
480 */
481 location = (u32 *)obj->buffer.pointer;
482 if (obj->buffer.length == 8) {
483 event_id = *location;
484 event_data = *(location + 1);
485 } else if (obj->buffer.length == 16) {
486 event_id = *location;
487 event_data = *(location + 2);
488 } else {
Joe Perchesb5a42232011-03-29 15:21:41 -0700489 pr_info("Unknown buffer length %d\n", obj->buffer.length);
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200490 kfree(obj);
491 return;
492 }
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100493 kfree(obj);
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200494
495 switch (event_id) {
Thomas Renninger751ae802010-05-21 16:18:09 +0200496 case HPWMI_DOCK_EVENT:
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100497 input_report_switch(hp_wmi_input_dev, SW_DOCK,
498 hp_wmi_dock_state());
499 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
500 hp_wmi_tablet_state());
501 input_sync(hp_wmi_input_dev);
Thomas Renninger751ae802010-05-21 16:18:09 +0200502 break;
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200503 case HPWMI_PARK_HDD:
504 break;
505 case HPWMI_SMART_ADAPTER:
506 break;
Thomas Renninger751ae802010-05-21 16:18:09 +0200507 case HPWMI_BEZEL_BUTTON:
Thomas Renninger6d96e002010-05-21 16:42:40 +0200508 ret = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
Matthew Garretta8ec1052010-08-23 15:52:34 -0400509 &key_code,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200510 sizeof(key_code),
Thomas Renninger6d96e002010-05-21 16:42:40 +0200511 sizeof(key_code));
512 if (ret)
513 break;
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700514
515 if (!sparse_keymap_report_event(hp_wmi_input_dev,
516 key_code, 1, true))
Joe Perchesb5a42232011-03-29 15:21:41 -0700517 pr_info("Unknown key code - 0x%x\n", key_code);
Thomas Renninger751ae802010-05-21 16:18:09 +0200518 break;
519 case HPWMI_WIRELESS:
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200520 if (rfkill2_count) {
521 hp_wmi_rfkill2_refresh();
522 break;
523 }
524
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100525 if (wifi_rfkill)
526 rfkill_set_states(wifi_rfkill,
527 hp_wmi_get_sw_state(HPWMI_WIFI),
528 hp_wmi_get_hw_state(HPWMI_WIFI));
529 if (bluetooth_rfkill)
530 rfkill_set_states(bluetooth_rfkill,
531 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
532 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
533 if (wwan_rfkill)
534 rfkill_set_states(wwan_rfkill,
535 hp_wmi_get_sw_state(HPWMI_WWAN),
536 hp_wmi_get_hw_state(HPWMI_WWAN));
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200537 if (gps_rfkill)
538 rfkill_set_states(gps_rfkill,
539 hp_wmi_get_sw_state(HPWMI_GPS),
540 hp_wmi_get_hw_state(HPWMI_GPS));
Thomas Renninger751ae802010-05-21 16:18:09 +0200541 break;
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200542 case HPWMI_CPU_BATTERY_THROTTLE:
Joe Perchesb5a42232011-03-29 15:21:41 -0700543 pr_info("Unimplemented CPU throttle because of 3 Cell battery event detected\n");
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200544 break;
545 case HPWMI_LOCK_SWITCH:
546 break;
Alex Hungd9e290a2013-03-15 17:18:22 +0800547 case HPWMI_LID_SWITCH:
548 break;
549 case HPWMI_SCREEN_ROTATION:
550 break;
551 case HPWMI_COOLSENSE_SYSTEM_MOBILE:
552 break;
553 case HPWMI_COOLSENSE_SYSTEM_HOT:
554 break;
555 case HPWMI_PROXIMITY_SENSOR:
556 break;
557 case HPWMI_BACKLIT_KB_BRIGHTNESS:
558 break;
559 case HPWMI_PEAKSHIFT_PERIOD:
560 break;
561 case HPWMI_BATTERY_CHARGE_PERIOD:
562 break;
Thomas Renninger751ae802010-05-21 16:18:09 +0200563 default:
Joe Perchesb5a42232011-03-29 15:21:41 -0700564 pr_info("Unknown event_id - %d - 0x%x\n", event_id, event_data);
Thomas Renninger751ae802010-05-21 16:18:09 +0200565 break;
566 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700567}
568
569static int __init hp_wmi_input_setup(void)
570{
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700571 acpi_status status;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700572 int err;
573
574 hp_wmi_input_dev = input_allocate_device();
Axel Linbc285962010-07-20 15:19:51 -0700575 if (!hp_wmi_input_dev)
576 return -ENOMEM;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700577
578 hp_wmi_input_dev->name = "HP WMI hotkeys";
579 hp_wmi_input_dev->phys = "wmi/input0";
580 hp_wmi_input_dev->id.bustype = BUS_HOST;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700581
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700582 __set_bit(EV_SW, hp_wmi_input_dev->evbit);
583 __set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
584 __set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700585
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700586 err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
587 if (err)
588 goto err_free_dev;
Matthew Garrett871043b2009-06-01 15:25:45 +0100589
590 /* Set initial hardware state */
591 input_report_switch(hp_wmi_input_dev, SW_DOCK, hp_wmi_dock_state());
592 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
593 hp_wmi_tablet_state());
594 input_sync(hp_wmi_input_dev);
595
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700596 status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
597 if (ACPI_FAILURE(status)) {
598 err = -EIO;
599 goto err_free_keymap;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700600 }
601
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700602 err = input_register_device(hp_wmi_input_dev);
603 if (err)
604 goto err_uninstall_notifier;
605
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700606 return 0;
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700607
608 err_uninstall_notifier:
609 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
610 err_free_keymap:
611 sparse_keymap_free(hp_wmi_input_dev);
612 err_free_dev:
613 input_free_device(hp_wmi_input_dev);
614 return err;
615}
616
617static void hp_wmi_input_destroy(void)
618{
619 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
620 sparse_keymap_free(hp_wmi_input_dev);
621 input_unregister_device(hp_wmi_input_dev);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700622}
623
624static void cleanup_sysfs(struct platform_device *device)
625{
626 device_remove_file(&device->dev, &dev_attr_display);
627 device_remove_file(&device->dev, &dev_attr_hddtemp);
628 device_remove_file(&device->dev, &dev_attr_als);
629 device_remove_file(&device->dev, &dev_attr_dock);
Matthew Garrett871043b2009-06-01 15:25:45 +0100630 device_remove_file(&device->dev, &dev_attr_tablet);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700631}
632
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800633static int hp_wmi_rfkill_setup(struct platform_device *device)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700634{
635 int err;
Matthew Garretta8ec1052010-08-23 15:52:34 -0400636 int wireless = 0;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200637
Matthew Garretta8ec1052010-08-23 15:52:34 -0400638 err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, &wireless,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200639 sizeof(wireless), sizeof(wireless));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200640 if (err)
641 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700642
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700643 if (wireless & 0x1) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200644 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
645 RFKILL_TYPE_WLAN,
646 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100647 (void *) HPWMI_WIFI);
Dan Carpenterdd258c02012-05-17 09:48:12 +0300648 if (!wifi_rfkill)
649 return -ENOMEM;
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100650 rfkill_init_sw_state(wifi_rfkill,
651 hp_wmi_get_sw_state(HPWMI_WIFI));
652 rfkill_set_hw_state(wifi_rfkill,
653 hp_wmi_get_hw_state(HPWMI_WIFI));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800654 err = rfkill_register(wifi_rfkill);
655 if (err)
Johannes Berg19d337d2009-06-02 13:01:37 +0200656 goto register_wifi_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700657 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700658
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700659 if (wireless & 0x2) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200660 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
661 RFKILL_TYPE_BLUETOOTH,
662 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100663 (void *) HPWMI_BLUETOOTH);
Dan Carpenterdd258c02012-05-17 09:48:12 +0300664 if (!bluetooth_rfkill) {
665 err = -ENOMEM;
666 goto register_wifi_error;
667 }
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100668 rfkill_init_sw_state(bluetooth_rfkill,
669 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
670 rfkill_set_hw_state(bluetooth_rfkill,
671 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800672 err = rfkill_register(bluetooth_rfkill);
Frans Pop6989d562009-01-29 14:25:14 -0800673 if (err)
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800674 goto register_bluetooth_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700675 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700676
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700677 if (wireless & 0x4) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200678 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
679 RFKILL_TYPE_WWAN,
680 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100681 (void *) HPWMI_WWAN);
Dan Carpenterdd258c02012-05-17 09:48:12 +0300682 if (!wwan_rfkill) {
683 err = -ENOMEM;
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200684 goto register_gps_error;
Dan Carpenterdd258c02012-05-17 09:48:12 +0300685 }
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100686 rfkill_init_sw_state(wwan_rfkill,
687 hp_wmi_get_sw_state(HPWMI_WWAN));
688 rfkill_set_hw_state(wwan_rfkill,
689 hp_wmi_get_hw_state(HPWMI_WWAN));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800690 err = rfkill_register(wwan_rfkill);
691 if (err)
692 goto register_wwan_err;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700693 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700694
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200695 if (wireless & 0x8) {
696 gps_rfkill = rfkill_alloc("hp-gps", &device->dev,
697 RFKILL_TYPE_GPS,
698 &hp_wmi_rfkill_ops,
699 (void *) HPWMI_GPS);
700 if (!gps_rfkill) {
701 err = -ENOMEM;
702 goto register_bluetooth_error;
703 }
704 rfkill_init_sw_state(gps_rfkill,
705 hp_wmi_get_sw_state(HPWMI_GPS));
lan,Tianyuaf1d4862013-05-28 02:25:33 +0000706 rfkill_set_hw_state(gps_rfkill,
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200707 hp_wmi_get_hw_state(HPWMI_GPS));
708 err = rfkill_register(gps_rfkill);
709 if (err)
710 goto register_gps_error;
711 }
712
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700713 return 0;
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800714register_wwan_err:
Johannes Berg19d337d2009-06-02 13:01:37 +0200715 rfkill_destroy(wwan_rfkill);
Anssi Hannula6d97db52011-02-20 20:07:24 +0200716 wwan_rfkill = NULL;
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200717 if (gps_rfkill)
718 rfkill_unregister(gps_rfkill);
719register_gps_error:
720 rfkill_destroy(gps_rfkill);
721 gps_rfkill = NULL;
Andrew Morton44f06062009-02-04 15:12:07 -0800722 if (bluetooth_rfkill)
723 rfkill_unregister(bluetooth_rfkill);
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800724register_bluetooth_error:
Johannes Berg19d337d2009-06-02 13:01:37 +0200725 rfkill_destroy(bluetooth_rfkill);
Anssi Hannula6d97db52011-02-20 20:07:24 +0200726 bluetooth_rfkill = NULL;
Andrew Morton44f06062009-02-04 15:12:07 -0800727 if (wifi_rfkill)
728 rfkill_unregister(wifi_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200729register_wifi_error:
730 rfkill_destroy(wifi_rfkill);
Anssi Hannula6d97db52011-02-20 20:07:24 +0200731 wifi_rfkill = NULL;
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200732 return err;
733}
734
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800735static int hp_wmi_rfkill2_setup(struct platform_device *device)
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200736{
737 int err, i;
738 struct bios_rfkill2_state state;
739 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, 0, &state,
740 0, sizeof(state));
741 if (err)
742 return err;
743
744 if (state.count > HPWMI_MAX_RFKILL2_DEVICES) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700745 pr_warn("unable to parse 0x1b query output\n");
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200746 return -EINVAL;
747 }
748
749 for (i = 0; i < state.count; i++) {
750 struct rfkill *rfkill;
751 enum rfkill_type type;
752 char *name;
753 switch (state.device[i].radio_type) {
754 case HPWMI_WIFI:
755 type = RFKILL_TYPE_WLAN;
756 name = "hp-wifi";
757 break;
758 case HPWMI_BLUETOOTH:
759 type = RFKILL_TYPE_BLUETOOTH;
760 name = "hp-bluetooth";
761 break;
762 case HPWMI_WWAN:
763 type = RFKILL_TYPE_WWAN;
764 name = "hp-wwan";
765 break;
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200766 case HPWMI_GPS:
767 type = RFKILL_TYPE_GPS;
768 name = "hp-gps";
769 break;
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200770 default:
Joe Perchesb5a42232011-03-29 15:21:41 -0700771 pr_warn("unknown device type 0x%x\n",
772 state.device[i].radio_type);
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200773 continue;
774 }
775
776 if (!state.device[i].vendor_id) {
Joe Perchesb5a42232011-03-29 15:21:41 -0700777 pr_warn("zero device %d while %d reported\n",
778 i, state.count);
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200779 continue;
780 }
781
782 rfkill = rfkill_alloc(name, &device->dev, type,
783 &hp_wmi_rfkill2_ops, (void *)(long)i);
784 if (!rfkill) {
785 err = -ENOMEM;
786 goto fail;
787 }
788
789 rfkill2[rfkill2_count].id = state.device[i].rfkill_id;
790 rfkill2[rfkill2_count].num = i;
791 rfkill2[rfkill2_count].rfkill = rfkill;
792
793 rfkill_init_sw_state(rfkill,
794 IS_SWBLOCKED(state.device[i].power));
795 rfkill_set_hw_state(rfkill,
796 IS_HWBLOCKED(state.device[i].power));
797
798 if (!(state.device[i].power & HPWMI_POWER_BIOS))
Joe Perchesb5a42232011-03-29 15:21:41 -0700799 pr_info("device %s blocked by BIOS\n", name);
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200800
801 err = rfkill_register(rfkill);
802 if (err) {
803 rfkill_destroy(rfkill);
804 goto fail;
805 }
806
807 rfkill2_count++;
808 }
809
810 return 0;
811fail:
812 for (; rfkill2_count > 0; rfkill2_count--) {
813 rfkill_unregister(rfkill2[rfkill2_count - 1].rfkill);
814 rfkill_destroy(rfkill2[rfkill2_count - 1].rfkill);
815 }
816 return err;
817}
818
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800819static int __init hp_wmi_bios_setup(struct platform_device *device)
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200820{
821 int err;
822
Anssi Hannula6d97db52011-02-20 20:07:24 +0200823 /* clear detected rfkill devices */
824 wifi_rfkill = NULL;
825 bluetooth_rfkill = NULL;
826 wwan_rfkill = NULL;
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200827 gps_rfkill = NULL;
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200828 rfkill2_count = 0;
Anssi Hannula6d97db52011-02-20 20:07:24 +0200829
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200830 if (hp_wmi_rfkill_setup(device))
831 hp_wmi_rfkill2_setup(device);
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200832
833 err = device_create_file(&device->dev, &dev_attr_display);
834 if (err)
835 goto add_sysfs_error;
836 err = device_create_file(&device->dev, &dev_attr_hddtemp);
837 if (err)
838 goto add_sysfs_error;
839 err = device_create_file(&device->dev, &dev_attr_als);
840 if (err)
841 goto add_sysfs_error;
842 err = device_create_file(&device->dev, &dev_attr_dock);
843 if (err)
844 goto add_sysfs_error;
845 err = device_create_file(&device->dev, &dev_attr_tablet);
846 if (err)
847 goto add_sysfs_error;
848 return 0;
849
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700850add_sysfs_error:
851 cleanup_sysfs(device);
852 return err;
853}
854
855static int __exit hp_wmi_bios_remove(struct platform_device *device)
856{
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200857 int i;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700858 cleanup_sysfs(device);
859
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200860 for (i = 0; i < rfkill2_count; i++) {
861 rfkill_unregister(rfkill2[i].rfkill);
862 rfkill_destroy(rfkill2[i].rfkill);
863 }
864
Johannes Berg19d337d2009-06-02 13:01:37 +0200865 if (wifi_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700866 rfkill_unregister(wifi_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200867 rfkill_destroy(wifi_rfkill);
868 }
869 if (bluetooth_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700870 rfkill_unregister(bluetooth_rfkill);
Corentin Chary09729f02009-09-14 12:43:51 +0200871 rfkill_destroy(bluetooth_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200872 }
873 if (wwan_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700874 rfkill_unregister(wwan_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200875 rfkill_destroy(wwan_rfkill);
876 }
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200877 if (gps_rfkill) {
878 rfkill_unregister(gps_rfkill);
879 rfkill_destroy(gps_rfkill);
880 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700881
882 return 0;
883}
884
Frans Pop8dd2b422009-08-20 20:38:13 +0200885static int hp_wmi_resume_handler(struct device *device)
Frans Pop4c395bd2009-03-04 11:55:28 -0800886{
Frans Pop4c395bd2009-03-04 11:55:28 -0800887 /*
Matthew Garrett871043b2009-06-01 15:25:45 +0100888 * Hardware state may have changed while suspended, so trigger
889 * input events for the current state. As this is a switch,
Frans Pop4c395bd2009-03-04 11:55:28 -0800890 * the input layer will only actually pass it on if the state
891 * changed.
892 */
Frans Popdaed9532009-07-30 17:16:05 -0400893 if (hp_wmi_input_dev) {
894 input_report_switch(hp_wmi_input_dev, SW_DOCK,
895 hp_wmi_dock_state());
896 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
897 hp_wmi_tablet_state());
898 input_sync(hp_wmi_input_dev);
899 }
Frans Pop4c395bd2009-03-04 11:55:28 -0800900
Anssi Hannulac0b9c642011-02-20 20:07:26 +0200901 if (rfkill2_count)
902 hp_wmi_rfkill2_refresh();
903
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100904 if (wifi_rfkill)
905 rfkill_set_states(wifi_rfkill,
906 hp_wmi_get_sw_state(HPWMI_WIFI),
907 hp_wmi_get_hw_state(HPWMI_WIFI));
908 if (bluetooth_rfkill)
909 rfkill_set_states(bluetooth_rfkill,
910 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
911 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
912 if (wwan_rfkill)
913 rfkill_set_states(wwan_rfkill,
914 hp_wmi_get_sw_state(HPWMI_WWAN),
915 hp_wmi_get_hw_state(HPWMI_WWAN));
Trepák Vilmos4fca7ce2012-10-11 12:51:00 +0200916 if (gps_rfkill)
917 rfkill_set_states(gps_rfkill,
918 hp_wmi_get_sw_state(HPWMI_GPS),
919 hp_wmi_get_hw_state(HPWMI_GPS));
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100920
Frans Pop4c395bd2009-03-04 11:55:28 -0800921 return 0;
922}
923
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800924static const struct dev_pm_ops hp_wmi_pm_ops = {
925 .resume = hp_wmi_resume_handler,
926 .restore = hp_wmi_resume_handler,
927};
928
929static struct platform_driver hp_wmi_driver = {
930 .driver = {
931 .name = "hp-wmi",
932 .owner = THIS_MODULE,
933 .pm = &hp_wmi_pm_ops,
934 },
935 .remove = __exit_p(hp_wmi_bios_remove),
936};
937
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700938static int __init hp_wmi_init(void)
939{
940 int err;
Thomas Renningerb0966672010-07-20 15:19:29 -0700941 int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
942 int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700943
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800944 if (!bios_capable && !event_capable)
945 return -ENODEV;
946
Thomas Renningerb0966672010-07-20 15:19:29 -0700947 if (event_capable) {
Axel Lindfec5c42010-06-10 16:06:40 +0800948 err = hp_wmi_input_setup();
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700949 if (err)
Axel Lindfec5c42010-06-10 16:06:40 +0800950 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700951 }
952
Thomas Renningerb0966672010-07-20 15:19:29 -0700953 if (bios_capable) {
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800954 hp_wmi_platform_dev =
955 platform_device_register_simple("hp-wmi", -1, NULL, 0);
956 if (IS_ERR(hp_wmi_platform_dev)) {
957 err = PTR_ERR(hp_wmi_platform_dev);
958 goto err_destroy_input;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700959 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700960
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800961 err = platform_driver_probe(&hp_wmi_driver, hp_wmi_bios_setup);
962 if (err)
963 goto err_unregister_device;
964 }
Thomas Renningerb0966672010-07-20 15:19:29 -0700965
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700966 return 0;
Axel Lindfec5c42010-06-10 16:06:40 +0800967
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800968err_unregister_device:
969 platform_device_unregister(hp_wmi_platform_dev);
970err_destroy_input:
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700971 if (event_capable)
972 hp_wmi_input_destroy();
Axel Lindfec5c42010-06-10 16:06:40 +0800973
974 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700975}
Dmitry Torokhovc165b802013-02-20 00:44:34 -0800976module_init(hp_wmi_init);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700977
978static void __exit hp_wmi_exit(void)
979{
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700980 if (wmi_has_guid(HPWMI_EVENT_GUID))
981 hp_wmi_input_destroy();
982
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700983 if (hp_wmi_platform_dev) {
Axel Lin97ba0af2010-06-03 15:18:03 +0800984 platform_device_unregister(hp_wmi_platform_dev);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700985 platform_driver_unregister(&hp_wmi_driver);
986 }
987}
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700988module_exit(hp_wmi_exit);