blob: 524ffabc28661035e154ea39e90923b0a8573ad5 [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>
5 *
6 * Portions based on wistron_btns.c:
7 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
8 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
9 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Matthew Garrett62ec30d2008-07-25 01:45:39 -070030#include <linux/types.h>
31#include <linux/input.h>
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -070032#include <linux/input/sparse-keymap.h>
Matthew Garrett62ec30d2008-07-25 01:45:39 -070033#include <linux/platform_device.h>
34#include <linux/acpi.h>
35#include <linux/rfkill.h>
36#include <linux/string.h>
37
38MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>");
39MODULE_DESCRIPTION("HP laptop WMI hotkeys driver");
40MODULE_LICENSE("GPL");
41
42MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
43MODULE_ALIAS("wmi:5FB7F034-2C63-45e9-BE91-3D44E2C707E4");
44
45#define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
46#define HPWMI_BIOS_GUID "5FB7F034-2C63-45e9-BE91-3D44E2C707E4"
47
48#define HPWMI_DISPLAY_QUERY 0x1
49#define HPWMI_HDDTEMP_QUERY 0x2
50#define HPWMI_ALS_QUERY 0x3
Matthew Garrett871043b2009-06-01 15:25:45 +010051#define HPWMI_HARDWARE_QUERY 0x4
Matthew Garrett62ec30d2008-07-25 01:45:39 -070052#define HPWMI_WIRELESS_QUERY 0x5
Matthew Garretta8823ae2008-09-02 14:36:03 -070053#define HPWMI_HOTKEY_QUERY 0xc
Matthew Garrett62ec30d2008-07-25 01:45:39 -070054
Thomas Renningera2806c62010-05-21 16:18:11 +020055#define PREFIX "HP WMI: "
Thomas Renninger1bbdfd52010-05-21 16:18:13 +020056#define UNIMP "Unimplemented "
Thomas Renningera2806c62010-05-21 16:18:11 +020057
Alan Jenkinse5fbba82009-07-21 12:14:01 +010058enum hp_wmi_radio {
59 HPWMI_WIFI = 0,
60 HPWMI_BLUETOOTH = 1,
61 HPWMI_WWAN = 2,
62};
63
Thomas Renninger751ae802010-05-21 16:18:09 +020064enum hp_wmi_event_ids {
65 HPWMI_DOCK_EVENT = 1,
Thomas Renninger1bbdfd52010-05-21 16:18:13 +020066 HPWMI_PARK_HDD = 2,
67 HPWMI_SMART_ADAPTER = 3,
Thomas Renninger751ae802010-05-21 16:18:09 +020068 HPWMI_BEZEL_BUTTON = 4,
69 HPWMI_WIRELESS = 5,
Thomas Renninger1bbdfd52010-05-21 16:18:13 +020070 HPWMI_CPU_BATTERY_THROTTLE = 6,
71 HPWMI_LOCK_SWITCH = 7,
Thomas Renninger751ae802010-05-21 16:18:09 +020072};
73
Uwe Kleine-Königea796322010-02-04 20:56:52 +010074static int __devinit hp_wmi_bios_setup(struct platform_device *device);
Matthew Garrett62ec30d2008-07-25 01:45:39 -070075static int __exit hp_wmi_bios_remove(struct platform_device *device);
Frans Pop8dd2b422009-08-20 20:38:13 +020076static int hp_wmi_resume_handler(struct device *device);
Matthew Garrett62ec30d2008-07-25 01:45:39 -070077
78struct bios_args {
79 u32 signature;
80 u32 command;
81 u32 commandtype;
82 u32 datasize;
Matthew Garretta8ec1052010-08-23 15:52:34 -040083 u32 data;
Matthew Garrett62ec30d2008-07-25 01:45:39 -070084};
85
86struct bios_return {
87 u32 sigpass;
88 u32 return_code;
Matthew Garrett62ec30d2008-07-25 01:45:39 -070089};
90
Anssi Hannula9af0e0f2011-02-20 20:07:20 +020091enum hp_return_value {
92 HPWMI_RET_WRONG_SIGNATURE = 0x02,
93 HPWMI_RET_UNKNOWN_COMMAND = 0x03,
94 HPWMI_RET_UNKNOWN_CMDTYPE = 0x04,
95 HPWMI_RET_INVALID_PARAMETERS = 0x05,
96};
97
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -070098static const struct key_entry hp_wmi_keymap[] = {
99 { KE_KEY, 0x02, { KEY_BRIGHTNESSUP } },
100 { KE_KEY, 0x03, { KEY_BRIGHTNESSDOWN } },
101 { KE_KEY, 0x20e6, { KEY_PROG1 } },
102 { KE_KEY, 0x20e8, { KEY_MEDIA } },
103 { KE_KEY, 0x2142, { KEY_MEDIA } },
104 { KE_KEY, 0x213b, { KEY_INFO } },
105 { KE_KEY, 0x2169, { KEY_DIRECTION } },
106 { KE_KEY, 0x231b, { KEY_HELP } },
107 { KE_END, 0 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700108};
109
110static struct input_dev *hp_wmi_input_dev;
111static struct platform_device *hp_wmi_platform_dev;
112
113static struct rfkill *wifi_rfkill;
114static struct rfkill *bluetooth_rfkill;
115static struct rfkill *wwan_rfkill;
116
Alexey Dobriyan47145212009-12-14 18:00:08 -0800117static const struct dev_pm_ops hp_wmi_pm_ops = {
Frans Pop8dd2b422009-08-20 20:38:13 +0200118 .resume = hp_wmi_resume_handler,
119 .restore = hp_wmi_resume_handler,
120};
121
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700122static struct platform_driver hp_wmi_driver = {
123 .driver = {
Frans Pop8dd2b422009-08-20 20:38:13 +0200124 .name = "hp-wmi",
125 .owner = THIS_MODULE,
126 .pm = &hp_wmi_pm_ops,
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700127 },
128 .probe = hp_wmi_bios_setup,
129 .remove = hp_wmi_bios_remove,
130};
131
Thomas Renninger6d96e002010-05-21 16:42:40 +0200132/*
133 * hp_wmi_perform_query
134 *
135 * query: The commandtype -> What should be queried
136 * write: The command -> 0 read, 1 write, 3 ODM specific
137 * buffer: Buffer used as input and/or output
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200138 * insize: Size of input buffer
139 * outsize: Size of output buffer
Thomas Renninger6d96e002010-05-21 16:42:40 +0200140 *
141 * returns zero on success
142 * an HP WMI query specific error code (which is positive)
143 * -EINVAL if the query was not successful at all
144 * -EINVAL if the output buffer size exceeds buffersize
145 *
146 * Note: The buffersize must at least be the maximum of the input and output
147 * size. E.g. Battery info query (0x7) is defined to have 1 byte input
148 * and 128 byte output. The caller would do:
149 * buffer = kzalloc(128, GFP_KERNEL);
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200150 * ret = hp_wmi_perform_query(0x7, 0, buffer, 1, 128)
Thomas Renninger6d96e002010-05-21 16:42:40 +0200151 */
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200152static int hp_wmi_perform_query(int query, int write, void *buffer,
153 int insize, int outsize)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700154{
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200155 struct bios_return *bios_return;
156 int actual_outsize;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700157 union acpi_object *obj;
158 struct bios_args args = {
159 .signature = 0x55434553,
160 .command = write ? 0x2 : 0x1,
161 .commandtype = query,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200162 .datasize = insize,
163 .data = 0,
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700164 };
165 struct acpi_buffer input = { sizeof(struct bios_args), &args };
166 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
167
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200168 if (WARN_ON(insize > sizeof(args.data)))
169 return -EINVAL;
170 memcpy(&args.data, buffer, insize);
171
Anssi Hannula25bb0672011-02-20 20:07:21 +0200172 wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700173
174 obj = output.pointer;
175
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100176 if (!obj)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700177 return -EINVAL;
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100178 else if (obj->type != ACPI_TYPE_BUFFER) {
179 kfree(obj);
180 return -EINVAL;
181 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700182
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200183 bios_return = (struct bios_return *)obj->buffer.pointer;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200184
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200185 if (bios_return->return_code) {
186 if (bios_return->return_code != HPWMI_RET_UNKNOWN_CMDTYPE)
Anssi Hannula9af0e0f2011-02-20 20:07:20 +0200187 printk(KERN_WARNING PREFIX "query 0x%x returned "
188 "error 0x%x\n",
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200189 query, bios_return->return_code);
Anssi Hannula9af0e0f2011-02-20 20:07:20 +0200190 kfree(obj);
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200191 return bios_return->return_code;
Anssi Hannula9af0e0f2011-02-20 20:07:20 +0200192 }
193
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200194 if (!outsize) {
195 /* ignore output data */
196 kfree(obj);
197 return 0;
198 }
Zeng Zhaoming53c96df2010-11-19 00:46:19 +0800199
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200200 actual_outsize = min(outsize, (int)(obj->buffer.length - sizeof(*bios_return)));
201 memcpy(buffer, obj->buffer.pointer + sizeof(*bios_return), actual_outsize);
202 memset(buffer + actual_outsize, 0, outsize - actual_outsize);
Zeng Zhaoming53c96df2010-11-19 00:46:19 +0800203 kfree(obj);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200204 return 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700205}
206
207static int hp_wmi_display_state(void)
208{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400209 int state = 0;
210 int ret = hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200211 sizeof(state), sizeof(state));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200212 if (ret)
213 return -EINVAL;
214 return state;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700215}
216
217static int hp_wmi_hddtemp_state(void)
218{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400219 int state = 0;
220 int ret = hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200221 sizeof(state), sizeof(state));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200222 if (ret)
223 return -EINVAL;
224 return state;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700225}
226
227static int hp_wmi_als_state(void)
228{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400229 int state = 0;
230 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200231 sizeof(state), sizeof(state));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200232 if (ret)
233 return -EINVAL;
234 return state;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700235}
236
237static int hp_wmi_dock_state(void)
238{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400239 int state = 0;
240 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200241 sizeof(state), sizeof(state));
Matthew Garrett871043b2009-06-01 15:25:45 +0100242
Thomas Renninger6d96e002010-05-21 16:42:40 +0200243 if (ret)
244 return -EINVAL;
Matthew Garrett871043b2009-06-01 15:25:45 +0100245
Thomas Renninger6d96e002010-05-21 16:42:40 +0200246 return state & 0x1;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700247}
248
Matthew Garrett871043b2009-06-01 15:25:45 +0100249static int hp_wmi_tablet_state(void)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700250{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400251 int state = 0;
252 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, &state,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200253 sizeof(state), sizeof(state));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200254 if (ret)
Matthew Garrett871043b2009-06-01 15:25:45 +0100255 return ret;
256
Thomas Renninger6d96e002010-05-21 16:42:40 +0200257 return (state & 0x4) ? 1 : 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700258}
259
Johannes Berg19d337d2009-06-02 13:01:37 +0200260static int hp_wmi_set_block(void *data, bool blocked)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700261{
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100262 enum hp_wmi_radio r = (enum hp_wmi_radio) data;
263 int query = BIT(r + 8) | ((!blocked) << r);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200264 int ret;
Johannes Berg19d337d2009-06-02 13:01:37 +0200265
Thomas Renninger6d96e002010-05-21 16:42:40 +0200266 ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200267 &query, sizeof(query), 0);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200268 if (ret)
269 return -EINVAL;
270 return 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700271}
272
Johannes Berg19d337d2009-06-02 13:01:37 +0200273static const struct rfkill_ops hp_wmi_rfkill_ops = {
274 .set_block = hp_wmi_set_block,
275};
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700276
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100277static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700278{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400279 int wireless = 0;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200280 int mask;
281 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200282 &wireless, sizeof(wireless),
283 sizeof(wireless));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200284 /* TBD: Pass error */
285
286 mask = 0x200 << (r * 8);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700287
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100288 if (wireless & mask)
Johannes Berg19d337d2009-06-02 13:01:37 +0200289 return false;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700290 else
Johannes Berg19d337d2009-06-02 13:01:37 +0200291 return true;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700292}
293
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100294static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700295{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400296 int wireless = 0;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200297 int mask;
298 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200299 &wireless, sizeof(wireless),
300 sizeof(wireless));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200301 /* TBD: Pass error */
302
303 mask = 0x800 << (r * 8);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700304
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100305 if (wireless & mask)
Johannes Berg19d337d2009-06-02 13:01:37 +0200306 return false;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700307 else
Johannes Berg19d337d2009-06-02 13:01:37 +0200308 return true;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700309}
310
311static ssize_t show_display(struct device *dev, struct device_attribute *attr,
312 char *buf)
313{
314 int value = hp_wmi_display_state();
315 if (value < 0)
316 return -EINVAL;
317 return sprintf(buf, "%d\n", value);
318}
319
320static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
321 char *buf)
322{
323 int value = hp_wmi_hddtemp_state();
324 if (value < 0)
325 return -EINVAL;
326 return sprintf(buf, "%d\n", value);
327}
328
329static ssize_t show_als(struct device *dev, struct device_attribute *attr,
330 char *buf)
331{
332 int value = hp_wmi_als_state();
333 if (value < 0)
334 return -EINVAL;
335 return sprintf(buf, "%d\n", value);
336}
337
338static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
339 char *buf)
340{
341 int value = hp_wmi_dock_state();
342 if (value < 0)
343 return -EINVAL;
344 return sprintf(buf, "%d\n", value);
345}
346
Matthew Garrett871043b2009-06-01 15:25:45 +0100347static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
348 char *buf)
349{
350 int value = hp_wmi_tablet_state();
351 if (value < 0)
352 return -EINVAL;
353 return sprintf(buf, "%d\n", value);
354}
355
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700356static ssize_t set_als(struct device *dev, struct device_attribute *attr,
357 const char *buf, size_t count)
358{
359 u32 tmp = simple_strtoul(buf, NULL, 10);
Matthew Garretta8ec1052010-08-23 15:52:34 -0400360 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, &tmp,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200361 sizeof(tmp), sizeof(tmp));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200362 if (ret)
363 return -EINVAL;
364
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700365 return count;
366}
367
368static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
369static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
370static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
371static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
Matthew Garrett871043b2009-06-01 15:25:45 +0100372static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700373
Adrian Bunk88429a12008-10-15 22:05:17 -0700374static void hp_wmi_notify(u32 value, void *context)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700375{
376 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700377 union acpi_object *obj;
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200378 u32 event_id, event_data;
Matthew Garretta8ec1052010-08-23 15:52:34 -0400379 int key_code = 0, ret;
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200380 u32 *location;
Len Brownfda11e62009-12-26 23:02:24 -0500381 acpi_status status;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700382
Len Brownfda11e62009-12-26 23:02:24 -0500383 status = wmi_get_event_data(value, &response);
384 if (status != AE_OK) {
Thomas Renningera2806c62010-05-21 16:18:11 +0200385 printk(KERN_INFO PREFIX "bad event status 0x%x\n", status);
Len Brownfda11e62009-12-26 23:02:24 -0500386 return;
387 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700388
389 obj = (union acpi_object *)response.pointer;
390
Thomas Renningerc4775062010-07-29 12:27:59 +0200391 if (!obj)
392 return;
393 if (obj->type != ACPI_TYPE_BUFFER) {
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200394 printk(KERN_INFO "hp-wmi: Unknown response received %d\n",
395 obj->type);
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100396 kfree(obj);
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100397 return;
398 }
399
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200400 /*
401 * Depending on ACPI version the concatenation of id and event data
402 * inside _WED function will result in a 8 or 16 byte buffer.
403 */
404 location = (u32 *)obj->buffer.pointer;
405 if (obj->buffer.length == 8) {
406 event_id = *location;
407 event_data = *(location + 1);
408 } else if (obj->buffer.length == 16) {
409 event_id = *location;
410 event_data = *(location + 2);
411 } else {
412 printk(KERN_INFO "hp-wmi: Unknown buffer length %d\n",
413 obj->buffer.length);
414 kfree(obj);
415 return;
416 }
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100417 kfree(obj);
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200418
419 switch (event_id) {
Thomas Renninger751ae802010-05-21 16:18:09 +0200420 case HPWMI_DOCK_EVENT:
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100421 input_report_switch(hp_wmi_input_dev, SW_DOCK,
422 hp_wmi_dock_state());
423 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
424 hp_wmi_tablet_state());
425 input_sync(hp_wmi_input_dev);
Thomas Renninger751ae802010-05-21 16:18:09 +0200426 break;
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200427 case HPWMI_PARK_HDD:
428 break;
429 case HPWMI_SMART_ADAPTER:
430 break;
Thomas Renninger751ae802010-05-21 16:18:09 +0200431 case HPWMI_BEZEL_BUTTON:
Thomas Renninger6d96e002010-05-21 16:42:40 +0200432 ret = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
Matthew Garretta8ec1052010-08-23 15:52:34 -0400433 &key_code,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200434 sizeof(key_code),
Thomas Renninger6d96e002010-05-21 16:42:40 +0200435 sizeof(key_code));
436 if (ret)
437 break;
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700438
439 if (!sparse_keymap_report_event(hp_wmi_input_dev,
440 key_code, 1, true))
Thomas Renningera2806c62010-05-21 16:18:11 +0200441 printk(KERN_INFO PREFIX "Unknown key code - 0x%x\n",
Thomas Renningerda9a79b2010-05-21 16:18:10 +0200442 key_code);
Thomas Renninger751ae802010-05-21 16:18:09 +0200443 break;
444 case HPWMI_WIRELESS:
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100445 if (wifi_rfkill)
446 rfkill_set_states(wifi_rfkill,
447 hp_wmi_get_sw_state(HPWMI_WIFI),
448 hp_wmi_get_hw_state(HPWMI_WIFI));
449 if (bluetooth_rfkill)
450 rfkill_set_states(bluetooth_rfkill,
451 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
452 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
453 if (wwan_rfkill)
454 rfkill_set_states(wwan_rfkill,
455 hp_wmi_get_sw_state(HPWMI_WWAN),
456 hp_wmi_get_hw_state(HPWMI_WWAN));
Thomas Renninger751ae802010-05-21 16:18:09 +0200457 break;
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200458 case HPWMI_CPU_BATTERY_THROTTLE:
459 printk(KERN_INFO PREFIX UNIMP "CPU throttle because of 3 Cell"
460 " battery event detected\n");
461 break;
462 case HPWMI_LOCK_SWITCH:
463 break;
Thomas Renninger751ae802010-05-21 16:18:09 +0200464 default:
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200465 printk(KERN_INFO PREFIX "Unknown event_id - %d - 0x%x\n",
466 event_id, event_data);
Thomas Renninger751ae802010-05-21 16:18:09 +0200467 break;
468 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700469}
470
471static int __init hp_wmi_input_setup(void)
472{
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700473 acpi_status status;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700474 int err;
475
476 hp_wmi_input_dev = input_allocate_device();
Axel Linbc285962010-07-20 15:19:51 -0700477 if (!hp_wmi_input_dev)
478 return -ENOMEM;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700479
480 hp_wmi_input_dev->name = "HP WMI hotkeys";
481 hp_wmi_input_dev->phys = "wmi/input0";
482 hp_wmi_input_dev->id.bustype = BUS_HOST;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700483
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700484 __set_bit(EV_SW, hp_wmi_input_dev->evbit);
485 __set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
486 __set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700487
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700488 err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
489 if (err)
490 goto err_free_dev;
Matthew Garrett871043b2009-06-01 15:25:45 +0100491
492 /* Set initial hardware state */
493 input_report_switch(hp_wmi_input_dev, SW_DOCK, hp_wmi_dock_state());
494 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
495 hp_wmi_tablet_state());
496 input_sync(hp_wmi_input_dev);
497
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700498 status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
499 if (ACPI_FAILURE(status)) {
500 err = -EIO;
501 goto err_free_keymap;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700502 }
503
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700504 err = input_register_device(hp_wmi_input_dev);
505 if (err)
506 goto err_uninstall_notifier;
507
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700508 return 0;
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700509
510 err_uninstall_notifier:
511 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
512 err_free_keymap:
513 sparse_keymap_free(hp_wmi_input_dev);
514 err_free_dev:
515 input_free_device(hp_wmi_input_dev);
516 return err;
517}
518
519static void hp_wmi_input_destroy(void)
520{
521 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
522 sparse_keymap_free(hp_wmi_input_dev);
523 input_unregister_device(hp_wmi_input_dev);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700524}
525
526static void cleanup_sysfs(struct platform_device *device)
527{
528 device_remove_file(&device->dev, &dev_attr_display);
529 device_remove_file(&device->dev, &dev_attr_hddtemp);
530 device_remove_file(&device->dev, &dev_attr_als);
531 device_remove_file(&device->dev, &dev_attr_dock);
Matthew Garrett871043b2009-06-01 15:25:45 +0100532 device_remove_file(&device->dev, &dev_attr_tablet);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700533}
534
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200535static int __devinit hp_wmi_rfkill_setup(struct platform_device *device)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700536{
537 int err;
Matthew Garretta8ec1052010-08-23 15:52:34 -0400538 int wireless = 0;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200539
Matthew Garretta8ec1052010-08-23 15:52:34 -0400540 err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, &wireless,
Anssi Hannulac3021ea2011-02-20 20:07:22 +0200541 sizeof(wireless), sizeof(wireless));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200542 if (err)
543 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700544
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700545 if (wireless & 0x1) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200546 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
547 RFKILL_TYPE_WLAN,
548 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100549 (void *) HPWMI_WIFI);
550 rfkill_init_sw_state(wifi_rfkill,
551 hp_wmi_get_sw_state(HPWMI_WIFI));
552 rfkill_set_hw_state(wifi_rfkill,
553 hp_wmi_get_hw_state(HPWMI_WIFI));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800554 err = rfkill_register(wifi_rfkill);
555 if (err)
Johannes Berg19d337d2009-06-02 13:01:37 +0200556 goto register_wifi_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700557 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700558
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700559 if (wireless & 0x2) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200560 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
561 RFKILL_TYPE_BLUETOOTH,
562 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100563 (void *) HPWMI_BLUETOOTH);
564 rfkill_init_sw_state(bluetooth_rfkill,
565 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
566 rfkill_set_hw_state(bluetooth_rfkill,
567 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800568 err = rfkill_register(bluetooth_rfkill);
Frans Pop6989d562009-01-29 14:25:14 -0800569 if (err)
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800570 goto register_bluetooth_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700571 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700572
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700573 if (wireless & 0x4) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200574 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
575 RFKILL_TYPE_WWAN,
576 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100577 (void *) HPWMI_WWAN);
578 rfkill_init_sw_state(wwan_rfkill,
579 hp_wmi_get_sw_state(HPWMI_WWAN));
580 rfkill_set_hw_state(wwan_rfkill,
581 hp_wmi_get_hw_state(HPWMI_WWAN));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800582 err = rfkill_register(wwan_rfkill);
583 if (err)
584 goto register_wwan_err;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700585 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700586
587 return 0;
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800588register_wwan_err:
Johannes Berg19d337d2009-06-02 13:01:37 +0200589 rfkill_destroy(wwan_rfkill);
Anssi Hannula6d97db52011-02-20 20:07:24 +0200590 wwan_rfkill = NULL;
Andrew Morton44f06062009-02-04 15:12:07 -0800591 if (bluetooth_rfkill)
592 rfkill_unregister(bluetooth_rfkill);
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800593register_bluetooth_error:
Johannes Berg19d337d2009-06-02 13:01:37 +0200594 rfkill_destroy(bluetooth_rfkill);
Anssi Hannula6d97db52011-02-20 20:07:24 +0200595 bluetooth_rfkill = NULL;
Andrew Morton44f06062009-02-04 15:12:07 -0800596 if (wifi_rfkill)
597 rfkill_unregister(wifi_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200598register_wifi_error:
599 rfkill_destroy(wifi_rfkill);
Anssi Hannula6d97db52011-02-20 20:07:24 +0200600 wifi_rfkill = NULL;
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200601 return err;
602}
603
604static int __devinit hp_wmi_bios_setup(struct platform_device *device)
605{
606 int err;
607
Anssi Hannula6d97db52011-02-20 20:07:24 +0200608 /* clear detected rfkill devices */
609 wifi_rfkill = NULL;
610 bluetooth_rfkill = NULL;
611 wwan_rfkill = NULL;
612
Anssi Hannula7cd635d2011-02-20 20:07:25 +0200613 hp_wmi_rfkill_setup(device);
Anssi Hannulaeceb7bd2011-02-20 20:07:23 +0200614
615 err = device_create_file(&device->dev, &dev_attr_display);
616 if (err)
617 goto add_sysfs_error;
618 err = device_create_file(&device->dev, &dev_attr_hddtemp);
619 if (err)
620 goto add_sysfs_error;
621 err = device_create_file(&device->dev, &dev_attr_als);
622 if (err)
623 goto add_sysfs_error;
624 err = device_create_file(&device->dev, &dev_attr_dock);
625 if (err)
626 goto add_sysfs_error;
627 err = device_create_file(&device->dev, &dev_attr_tablet);
628 if (err)
629 goto add_sysfs_error;
630 return 0;
631
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700632add_sysfs_error:
633 cleanup_sysfs(device);
634 return err;
635}
636
637static int __exit hp_wmi_bios_remove(struct platform_device *device)
638{
639 cleanup_sysfs(device);
640
Johannes Berg19d337d2009-06-02 13:01:37 +0200641 if (wifi_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700642 rfkill_unregister(wifi_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200643 rfkill_destroy(wifi_rfkill);
644 }
645 if (bluetooth_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700646 rfkill_unregister(bluetooth_rfkill);
Corentin Chary09729f02009-09-14 12:43:51 +0200647 rfkill_destroy(bluetooth_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200648 }
649 if (wwan_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700650 rfkill_unregister(wwan_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200651 rfkill_destroy(wwan_rfkill);
652 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700653
654 return 0;
655}
656
Frans Pop8dd2b422009-08-20 20:38:13 +0200657static int hp_wmi_resume_handler(struct device *device)
Frans Pop4c395bd2009-03-04 11:55:28 -0800658{
Frans Pop4c395bd2009-03-04 11:55:28 -0800659 /*
Matthew Garrett871043b2009-06-01 15:25:45 +0100660 * Hardware state may have changed while suspended, so trigger
661 * input events for the current state. As this is a switch,
Frans Pop4c395bd2009-03-04 11:55:28 -0800662 * the input layer will only actually pass it on if the state
663 * changed.
664 */
Frans Popdaed9532009-07-30 17:16:05 -0400665 if (hp_wmi_input_dev) {
666 input_report_switch(hp_wmi_input_dev, SW_DOCK,
667 hp_wmi_dock_state());
668 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
669 hp_wmi_tablet_state());
670 input_sync(hp_wmi_input_dev);
671 }
Frans Pop4c395bd2009-03-04 11:55:28 -0800672
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100673 if (wifi_rfkill)
674 rfkill_set_states(wifi_rfkill,
675 hp_wmi_get_sw_state(HPWMI_WIFI),
676 hp_wmi_get_hw_state(HPWMI_WIFI));
677 if (bluetooth_rfkill)
678 rfkill_set_states(bluetooth_rfkill,
679 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
680 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
681 if (wwan_rfkill)
682 rfkill_set_states(wwan_rfkill,
683 hp_wmi_get_sw_state(HPWMI_WWAN),
684 hp_wmi_get_hw_state(HPWMI_WWAN));
685
Frans Pop4c395bd2009-03-04 11:55:28 -0800686 return 0;
687}
688
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700689static int __init hp_wmi_init(void)
690{
691 int err;
Thomas Renningerb0966672010-07-20 15:19:29 -0700692 int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
693 int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700694
Thomas Renningerb0966672010-07-20 15:19:29 -0700695 if (event_capable) {
Axel Lindfec5c42010-06-10 16:06:40 +0800696 err = hp_wmi_input_setup();
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700697 if (err)
Axel Lindfec5c42010-06-10 16:06:40 +0800698 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700699 }
700
Thomas Renningerb0966672010-07-20 15:19:29 -0700701 if (bios_capable) {
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700702 err = platform_driver_register(&hp_wmi_driver);
703 if (err)
Axel Lindfec5c42010-06-10 16:06:40 +0800704 goto err_driver_reg;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700705 hp_wmi_platform_dev = platform_device_alloc("hp-wmi", -1);
706 if (!hp_wmi_platform_dev) {
Axel Lindfec5c42010-06-10 16:06:40 +0800707 err = -ENOMEM;
708 goto err_device_alloc;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700709 }
Axel Lindfec5c42010-06-10 16:06:40 +0800710 err = platform_device_add(hp_wmi_platform_dev);
711 if (err)
712 goto err_device_add;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700713 }
714
Thomas Renningerb0966672010-07-20 15:19:29 -0700715 if (!bios_capable && !event_capable)
716 return -ENODEV;
717
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700718 return 0;
Axel Lindfec5c42010-06-10 16:06:40 +0800719
720err_device_add:
721 platform_device_put(hp_wmi_platform_dev);
722err_device_alloc:
723 platform_driver_unregister(&hp_wmi_driver);
724err_driver_reg:
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700725 if (event_capable)
726 hp_wmi_input_destroy();
Axel Lindfec5c42010-06-10 16:06:40 +0800727
728 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700729}
730
731static void __exit hp_wmi_exit(void)
732{
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700733 if (wmi_has_guid(HPWMI_EVENT_GUID))
734 hp_wmi_input_destroy();
735
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700736 if (hp_wmi_platform_dev) {
Axel Lin97ba0af2010-06-03 15:18:03 +0800737 platform_device_unregister(hp_wmi_platform_dev);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700738 platform_driver_unregister(&hp_wmi_driver);
739 }
740}
741
742module_init(hp_wmi_init);
743module_exit(hp_wmi_exit);