blob: 1dac659b5e0c84e7bc3ccefea144d6a3dc88993c [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 Garretta8ec1052010-08-23 15:52:34 -040089 u32 value;
Matthew Garrett62ec30d2008-07-25 01:45:39 -070090};
91
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -070092static const struct key_entry hp_wmi_keymap[] = {
93 { KE_KEY, 0x02, { KEY_BRIGHTNESSUP } },
94 { KE_KEY, 0x03, { KEY_BRIGHTNESSDOWN } },
95 { KE_KEY, 0x20e6, { KEY_PROG1 } },
96 { KE_KEY, 0x20e8, { KEY_MEDIA } },
97 { KE_KEY, 0x2142, { KEY_MEDIA } },
98 { KE_KEY, 0x213b, { KEY_INFO } },
99 { KE_KEY, 0x2169, { KEY_DIRECTION } },
100 { KE_KEY, 0x231b, { KEY_HELP } },
101 { KE_END, 0 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700102};
103
104static struct input_dev *hp_wmi_input_dev;
105static struct platform_device *hp_wmi_platform_dev;
106
107static struct rfkill *wifi_rfkill;
108static struct rfkill *bluetooth_rfkill;
109static struct rfkill *wwan_rfkill;
110
Alexey Dobriyan47145212009-12-14 18:00:08 -0800111static const struct dev_pm_ops hp_wmi_pm_ops = {
Frans Pop8dd2b422009-08-20 20:38:13 +0200112 .resume = hp_wmi_resume_handler,
113 .restore = hp_wmi_resume_handler,
114};
115
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700116static struct platform_driver hp_wmi_driver = {
117 .driver = {
Frans Pop8dd2b422009-08-20 20:38:13 +0200118 .name = "hp-wmi",
119 .owner = THIS_MODULE,
120 .pm = &hp_wmi_pm_ops,
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700121 },
122 .probe = hp_wmi_bios_setup,
123 .remove = hp_wmi_bios_remove,
124};
125
Thomas Renninger6d96e002010-05-21 16:42:40 +0200126/*
127 * hp_wmi_perform_query
128 *
129 * query: The commandtype -> What should be queried
130 * write: The command -> 0 read, 1 write, 3 ODM specific
131 * buffer: Buffer used as input and/or output
132 * buffersize: Size of buffer
133 *
134 * returns zero on success
135 * an HP WMI query specific error code (which is positive)
136 * -EINVAL if the query was not successful at all
137 * -EINVAL if the output buffer size exceeds buffersize
138 *
139 * Note: The buffersize must at least be the maximum of the input and output
140 * size. E.g. Battery info query (0x7) is defined to have 1 byte input
141 * and 128 byte output. The caller would do:
142 * buffer = kzalloc(128, GFP_KERNEL);
143 * ret = hp_wmi_perform_query(0x7, 0, buffer, 128)
144 */
Matthew Garretta8ec1052010-08-23 15:52:34 -0400145static int hp_wmi_perform_query(int query, int write, u32 *buffer,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200146 int buffersize)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700147{
148 struct bios_return bios_return;
149 acpi_status status;
150 union acpi_object *obj;
151 struct bios_args args = {
152 .signature = 0x55434553,
153 .command = write ? 0x2 : 0x1,
154 .commandtype = query,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200155 .datasize = buffersize,
Matthew Garretta8ec1052010-08-23 15:52:34 -0400156 .data = *buffer,
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700157 };
158 struct acpi_buffer input = { sizeof(struct bios_args), &args };
159 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
160
161 status = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
162
163 obj = output.pointer;
164
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100165 if (!obj)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700166 return -EINVAL;
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100167 else if (obj->type != ACPI_TYPE_BUFFER) {
168 kfree(obj);
169 return -EINVAL;
170 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700171
172 bios_return = *((struct bios_return *)obj->buffer.pointer);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200173
Matthew Garretta8ec1052010-08-23 15:52:34 -0400174 memcpy(buffer, &bios_return.value, sizeof(bios_return.value));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200175 return 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700176}
177
178static int hp_wmi_display_state(void)
179{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400180 int state = 0;
181 int ret = hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, &state,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200182 sizeof(state));
183 if (ret)
184 return -EINVAL;
185 return state;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700186}
187
188static int hp_wmi_hddtemp_state(void)
189{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400190 int state = 0;
191 int ret = hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, &state,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200192 sizeof(state));
193 if (ret)
194 return -EINVAL;
195 return state;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700196}
197
198static int hp_wmi_als_state(void)
199{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400200 int state = 0;
201 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, &state,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200202 sizeof(state));
203 if (ret)
204 return -EINVAL;
205 return state;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700206}
207
208static int hp_wmi_dock_state(void)
209{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400210 int state = 0;
211 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, &state,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200212 sizeof(state));
Matthew Garrett871043b2009-06-01 15:25:45 +0100213
Thomas Renninger6d96e002010-05-21 16:42:40 +0200214 if (ret)
215 return -EINVAL;
Matthew Garrett871043b2009-06-01 15:25:45 +0100216
Thomas Renninger6d96e002010-05-21 16:42:40 +0200217 return state & 0x1;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700218}
219
Matthew Garrett871043b2009-06-01 15:25:45 +0100220static int hp_wmi_tablet_state(void)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700221{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400222 int state = 0;
223 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, &state,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200224 sizeof(state));
225 if (ret)
Matthew Garrett871043b2009-06-01 15:25:45 +0100226 return ret;
227
Thomas Renninger6d96e002010-05-21 16:42:40 +0200228 return (state & 0x4) ? 1 : 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700229}
230
Johannes Berg19d337d2009-06-02 13:01:37 +0200231static int hp_wmi_set_block(void *data, bool blocked)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700232{
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100233 enum hp_wmi_radio r = (enum hp_wmi_radio) data;
234 int query = BIT(r + 8) | ((!blocked) << r);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200235 int ret;
Johannes Berg19d337d2009-06-02 13:01:37 +0200236
Thomas Renninger6d96e002010-05-21 16:42:40 +0200237 ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1,
Matthew Garretta8ec1052010-08-23 15:52:34 -0400238 &query, sizeof(query));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200239 if (ret)
240 return -EINVAL;
241 return 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700242}
243
Johannes Berg19d337d2009-06-02 13:01:37 +0200244static const struct rfkill_ops hp_wmi_rfkill_ops = {
245 .set_block = hp_wmi_set_block,
246};
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700247
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100248static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700249{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400250 int wireless = 0;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200251 int mask;
252 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
Matthew Garretta8ec1052010-08-23 15:52:34 -0400253 &wireless, sizeof(wireless));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200254 /* TBD: Pass error */
255
256 mask = 0x200 << (r * 8);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700257
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100258 if (wireless & mask)
Johannes Berg19d337d2009-06-02 13:01:37 +0200259 return false;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700260 else
Johannes Berg19d337d2009-06-02 13:01:37 +0200261 return true;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700262}
263
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100264static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700265{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400266 int wireless = 0;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200267 int mask;
268 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
Matthew Garretta8ec1052010-08-23 15:52:34 -0400269 &wireless, sizeof(wireless));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200270 /* TBD: Pass error */
271
272 mask = 0x800 << (r * 8);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700273
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100274 if (wireless & mask)
Johannes Berg19d337d2009-06-02 13:01:37 +0200275 return false;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700276 else
Johannes Berg19d337d2009-06-02 13:01:37 +0200277 return true;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700278}
279
280static ssize_t show_display(struct device *dev, struct device_attribute *attr,
281 char *buf)
282{
283 int value = hp_wmi_display_state();
284 if (value < 0)
285 return -EINVAL;
286 return sprintf(buf, "%d\n", value);
287}
288
289static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
290 char *buf)
291{
292 int value = hp_wmi_hddtemp_state();
293 if (value < 0)
294 return -EINVAL;
295 return sprintf(buf, "%d\n", value);
296}
297
298static ssize_t show_als(struct device *dev, struct device_attribute *attr,
299 char *buf)
300{
301 int value = hp_wmi_als_state();
302 if (value < 0)
303 return -EINVAL;
304 return sprintf(buf, "%d\n", value);
305}
306
307static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
308 char *buf)
309{
310 int value = hp_wmi_dock_state();
311 if (value < 0)
312 return -EINVAL;
313 return sprintf(buf, "%d\n", value);
314}
315
Matthew Garrett871043b2009-06-01 15:25:45 +0100316static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
317 char *buf)
318{
319 int value = hp_wmi_tablet_state();
320 if (value < 0)
321 return -EINVAL;
322 return sprintf(buf, "%d\n", value);
323}
324
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700325static ssize_t set_als(struct device *dev, struct device_attribute *attr,
326 const char *buf, size_t count)
327{
328 u32 tmp = simple_strtoul(buf, NULL, 10);
Matthew Garretta8ec1052010-08-23 15:52:34 -0400329 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, &tmp,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200330 sizeof(tmp));
331 if (ret)
332 return -EINVAL;
333
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700334 return count;
335}
336
337static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
338static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
339static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
340static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
Matthew Garrett871043b2009-06-01 15:25:45 +0100341static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700342
Adrian Bunk88429a12008-10-15 22:05:17 -0700343static void hp_wmi_notify(u32 value, void *context)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700344{
345 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700346 union acpi_object *obj;
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200347 u32 event_id, event_data;
Matthew Garretta8ec1052010-08-23 15:52:34 -0400348 int key_code = 0, ret;
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200349 u32 *location;
Len Brownfda11e62009-12-26 23:02:24 -0500350 acpi_status status;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700351
Len Brownfda11e62009-12-26 23:02:24 -0500352 status = wmi_get_event_data(value, &response);
353 if (status != AE_OK) {
Thomas Renningera2806c62010-05-21 16:18:11 +0200354 printk(KERN_INFO PREFIX "bad event status 0x%x\n", status);
Len Brownfda11e62009-12-26 23:02:24 -0500355 return;
356 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700357
358 obj = (union acpi_object *)response.pointer;
359
Thomas Renningerc4775062010-07-29 12:27:59 +0200360 if (!obj)
361 return;
362 if (obj->type != ACPI_TYPE_BUFFER) {
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200363 printk(KERN_INFO "hp-wmi: Unknown response received %d\n",
364 obj->type);
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100365 kfree(obj);
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100366 return;
367 }
368
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200369 /*
370 * Depending on ACPI version the concatenation of id and event data
371 * inside _WED function will result in a 8 or 16 byte buffer.
372 */
373 location = (u32 *)obj->buffer.pointer;
374 if (obj->buffer.length == 8) {
375 event_id = *location;
376 event_data = *(location + 1);
377 } else if (obj->buffer.length == 16) {
378 event_id = *location;
379 event_data = *(location + 2);
380 } else {
381 printk(KERN_INFO "hp-wmi: Unknown buffer length %d\n",
382 obj->buffer.length);
383 kfree(obj);
384 return;
385 }
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100386 kfree(obj);
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200387
388 switch (event_id) {
Thomas Renninger751ae802010-05-21 16:18:09 +0200389 case HPWMI_DOCK_EVENT:
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100390 input_report_switch(hp_wmi_input_dev, SW_DOCK,
391 hp_wmi_dock_state());
392 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
393 hp_wmi_tablet_state());
394 input_sync(hp_wmi_input_dev);
Thomas Renninger751ae802010-05-21 16:18:09 +0200395 break;
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200396 case HPWMI_PARK_HDD:
397 break;
398 case HPWMI_SMART_ADAPTER:
399 break;
Thomas Renninger751ae802010-05-21 16:18:09 +0200400 case HPWMI_BEZEL_BUTTON:
Thomas Renninger6d96e002010-05-21 16:42:40 +0200401 ret = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
Matthew Garretta8ec1052010-08-23 15:52:34 -0400402 &key_code,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200403 sizeof(key_code));
404 if (ret)
405 break;
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700406
407 if (!sparse_keymap_report_event(hp_wmi_input_dev,
408 key_code, 1, true))
Thomas Renningera2806c62010-05-21 16:18:11 +0200409 printk(KERN_INFO PREFIX "Unknown key code - 0x%x\n",
Thomas Renningerda9a79b2010-05-21 16:18:10 +0200410 key_code);
Thomas Renninger751ae802010-05-21 16:18:09 +0200411 break;
412 case HPWMI_WIRELESS:
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100413 if (wifi_rfkill)
414 rfkill_set_states(wifi_rfkill,
415 hp_wmi_get_sw_state(HPWMI_WIFI),
416 hp_wmi_get_hw_state(HPWMI_WIFI));
417 if (bluetooth_rfkill)
418 rfkill_set_states(bluetooth_rfkill,
419 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
420 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
421 if (wwan_rfkill)
422 rfkill_set_states(wwan_rfkill,
423 hp_wmi_get_sw_state(HPWMI_WWAN),
424 hp_wmi_get_hw_state(HPWMI_WWAN));
Thomas Renninger751ae802010-05-21 16:18:09 +0200425 break;
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200426 case HPWMI_CPU_BATTERY_THROTTLE:
427 printk(KERN_INFO PREFIX UNIMP "CPU throttle because of 3 Cell"
428 " battery event detected\n");
429 break;
430 case HPWMI_LOCK_SWITCH:
431 break;
Thomas Renninger751ae802010-05-21 16:18:09 +0200432 default:
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200433 printk(KERN_INFO PREFIX "Unknown event_id - %d - 0x%x\n",
434 event_id, event_data);
Thomas Renninger751ae802010-05-21 16:18:09 +0200435 break;
436 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700437}
438
439static int __init hp_wmi_input_setup(void)
440{
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700441 acpi_status status;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700442 int err;
443
444 hp_wmi_input_dev = input_allocate_device();
Axel Linbc285962010-07-20 15:19:51 -0700445 if (!hp_wmi_input_dev)
446 return -ENOMEM;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700447
448 hp_wmi_input_dev->name = "HP WMI hotkeys";
449 hp_wmi_input_dev->phys = "wmi/input0";
450 hp_wmi_input_dev->id.bustype = BUS_HOST;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700451
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700452 __set_bit(EV_SW, hp_wmi_input_dev->evbit);
453 __set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
454 __set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700455
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700456 err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
457 if (err)
458 goto err_free_dev;
Matthew Garrett871043b2009-06-01 15:25:45 +0100459
460 /* Set initial hardware state */
461 input_report_switch(hp_wmi_input_dev, SW_DOCK, hp_wmi_dock_state());
462 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
463 hp_wmi_tablet_state());
464 input_sync(hp_wmi_input_dev);
465
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700466 status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
467 if (ACPI_FAILURE(status)) {
468 err = -EIO;
469 goto err_free_keymap;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700470 }
471
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700472 err = input_register_device(hp_wmi_input_dev);
473 if (err)
474 goto err_uninstall_notifier;
475
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700476 return 0;
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700477
478 err_uninstall_notifier:
479 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
480 err_free_keymap:
481 sparse_keymap_free(hp_wmi_input_dev);
482 err_free_dev:
483 input_free_device(hp_wmi_input_dev);
484 return err;
485}
486
487static void hp_wmi_input_destroy(void)
488{
489 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
490 sparse_keymap_free(hp_wmi_input_dev);
491 input_unregister_device(hp_wmi_input_dev);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700492}
493
494static void cleanup_sysfs(struct platform_device *device)
495{
496 device_remove_file(&device->dev, &dev_attr_display);
497 device_remove_file(&device->dev, &dev_attr_hddtemp);
498 device_remove_file(&device->dev, &dev_attr_als);
499 device_remove_file(&device->dev, &dev_attr_dock);
Matthew Garrett871043b2009-06-01 15:25:45 +0100500 device_remove_file(&device->dev, &dev_attr_tablet);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700501}
502
Uwe Kleine-Königea796322010-02-04 20:56:52 +0100503static int __devinit hp_wmi_bios_setup(struct platform_device *device)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700504{
505 int err;
Matthew Garretta8ec1052010-08-23 15:52:34 -0400506 int wireless = 0;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200507
Matthew Garretta8ec1052010-08-23 15:52:34 -0400508 err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, &wireless,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200509 sizeof(wireless));
510 if (err)
511 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700512
513 err = device_create_file(&device->dev, &dev_attr_display);
514 if (err)
515 goto add_sysfs_error;
516 err = device_create_file(&device->dev, &dev_attr_hddtemp);
517 if (err)
518 goto add_sysfs_error;
519 err = device_create_file(&device->dev, &dev_attr_als);
520 if (err)
521 goto add_sysfs_error;
522 err = device_create_file(&device->dev, &dev_attr_dock);
523 if (err)
524 goto add_sysfs_error;
Matthew Garrett871043b2009-06-01 15:25:45 +0100525 err = device_create_file(&device->dev, &dev_attr_tablet);
526 if (err)
527 goto add_sysfs_error;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700528
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700529 if (wireless & 0x1) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200530 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
531 RFKILL_TYPE_WLAN,
532 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100533 (void *) HPWMI_WIFI);
534 rfkill_init_sw_state(wifi_rfkill,
535 hp_wmi_get_sw_state(HPWMI_WIFI));
536 rfkill_set_hw_state(wifi_rfkill,
537 hp_wmi_get_hw_state(HPWMI_WIFI));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800538 err = rfkill_register(wifi_rfkill);
539 if (err)
Johannes Berg19d337d2009-06-02 13:01:37 +0200540 goto register_wifi_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700541 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700542
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700543 if (wireless & 0x2) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200544 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
545 RFKILL_TYPE_BLUETOOTH,
546 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100547 (void *) HPWMI_BLUETOOTH);
548 rfkill_init_sw_state(bluetooth_rfkill,
549 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
550 rfkill_set_hw_state(bluetooth_rfkill,
551 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800552 err = rfkill_register(bluetooth_rfkill);
Frans Pop6989d562009-01-29 14:25:14 -0800553 if (err)
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800554 goto register_bluetooth_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700555 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700556
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700557 if (wireless & 0x4) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200558 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
559 RFKILL_TYPE_WWAN,
560 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100561 (void *) HPWMI_WWAN);
562 rfkill_init_sw_state(wwan_rfkill,
563 hp_wmi_get_sw_state(HPWMI_WWAN));
564 rfkill_set_hw_state(wwan_rfkill,
565 hp_wmi_get_hw_state(HPWMI_WWAN));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800566 err = rfkill_register(wwan_rfkill);
567 if (err)
568 goto register_wwan_err;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700569 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700570
571 return 0;
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800572register_wwan_err:
Johannes Berg19d337d2009-06-02 13:01:37 +0200573 rfkill_destroy(wwan_rfkill);
Andrew Morton44f06062009-02-04 15:12:07 -0800574 if (bluetooth_rfkill)
575 rfkill_unregister(bluetooth_rfkill);
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800576register_bluetooth_error:
Johannes Berg19d337d2009-06-02 13:01:37 +0200577 rfkill_destroy(bluetooth_rfkill);
Andrew Morton44f06062009-02-04 15:12:07 -0800578 if (wifi_rfkill)
579 rfkill_unregister(wifi_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200580register_wifi_error:
581 rfkill_destroy(wifi_rfkill);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700582add_sysfs_error:
583 cleanup_sysfs(device);
584 return err;
585}
586
587static int __exit hp_wmi_bios_remove(struct platform_device *device)
588{
589 cleanup_sysfs(device);
590
Johannes Berg19d337d2009-06-02 13:01:37 +0200591 if (wifi_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700592 rfkill_unregister(wifi_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200593 rfkill_destroy(wifi_rfkill);
594 }
595 if (bluetooth_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700596 rfkill_unregister(bluetooth_rfkill);
Corentin Chary09729f02009-09-14 12:43:51 +0200597 rfkill_destroy(bluetooth_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200598 }
599 if (wwan_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700600 rfkill_unregister(wwan_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200601 rfkill_destroy(wwan_rfkill);
602 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700603
604 return 0;
605}
606
Frans Pop8dd2b422009-08-20 20:38:13 +0200607static int hp_wmi_resume_handler(struct device *device)
Frans Pop4c395bd2009-03-04 11:55:28 -0800608{
Frans Pop4c395bd2009-03-04 11:55:28 -0800609 /*
Matthew Garrett871043b2009-06-01 15:25:45 +0100610 * Hardware state may have changed while suspended, so trigger
611 * input events for the current state. As this is a switch,
Frans Pop4c395bd2009-03-04 11:55:28 -0800612 * the input layer will only actually pass it on if the state
613 * changed.
614 */
Frans Popdaed9532009-07-30 17:16:05 -0400615 if (hp_wmi_input_dev) {
616 input_report_switch(hp_wmi_input_dev, SW_DOCK,
617 hp_wmi_dock_state());
618 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
619 hp_wmi_tablet_state());
620 input_sync(hp_wmi_input_dev);
621 }
Frans Pop4c395bd2009-03-04 11:55:28 -0800622
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100623 if (wifi_rfkill)
624 rfkill_set_states(wifi_rfkill,
625 hp_wmi_get_sw_state(HPWMI_WIFI),
626 hp_wmi_get_hw_state(HPWMI_WIFI));
627 if (bluetooth_rfkill)
628 rfkill_set_states(bluetooth_rfkill,
629 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
630 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
631 if (wwan_rfkill)
632 rfkill_set_states(wwan_rfkill,
633 hp_wmi_get_sw_state(HPWMI_WWAN),
634 hp_wmi_get_hw_state(HPWMI_WWAN));
635
Frans Pop4c395bd2009-03-04 11:55:28 -0800636 return 0;
637}
638
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700639static int __init hp_wmi_init(void)
640{
641 int err;
Thomas Renningerb0966672010-07-20 15:19:29 -0700642 int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
643 int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700644
Thomas Renningerb0966672010-07-20 15:19:29 -0700645 if (event_capable) {
Axel Lindfec5c42010-06-10 16:06:40 +0800646 err = hp_wmi_input_setup();
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700647 if (err)
Axel Lindfec5c42010-06-10 16:06:40 +0800648 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700649 }
650
Thomas Renningerb0966672010-07-20 15:19:29 -0700651 if (bios_capable) {
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700652 err = platform_driver_register(&hp_wmi_driver);
653 if (err)
Axel Lindfec5c42010-06-10 16:06:40 +0800654 goto err_driver_reg;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700655 hp_wmi_platform_dev = platform_device_alloc("hp-wmi", -1);
656 if (!hp_wmi_platform_dev) {
Axel Lindfec5c42010-06-10 16:06:40 +0800657 err = -ENOMEM;
658 goto err_device_alloc;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700659 }
Axel Lindfec5c42010-06-10 16:06:40 +0800660 err = platform_device_add(hp_wmi_platform_dev);
661 if (err)
662 goto err_device_add;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700663 }
664
Thomas Renningerb0966672010-07-20 15:19:29 -0700665 if (!bios_capable && !event_capable)
666 return -ENODEV;
667
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700668 return 0;
Axel Lindfec5c42010-06-10 16:06:40 +0800669
670err_device_add:
671 platform_device_put(hp_wmi_platform_dev);
672err_device_alloc:
673 platform_driver_unregister(&hp_wmi_driver);
674err_driver_reg:
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700675 if (event_capable)
676 hp_wmi_input_destroy();
Axel Lindfec5c42010-06-10 16:06:40 +0800677
678 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700679}
680
681static void __exit hp_wmi_exit(void)
682{
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700683 if (wmi_has_guid(HPWMI_EVENT_GUID))
684 hp_wmi_input_destroy();
685
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700686 if (hp_wmi_platform_dev) {
Axel Lin97ba0af2010-06-03 15:18:03 +0800687 platform_device_unregister(hp_wmi_platform_dev);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700688 platform_driver_unregister(&hp_wmi_driver);
689 }
690}
691
692module_init(hp_wmi_init);
693module_exit(hp_wmi_exit);