blob: 8e27c27900a3780567b11fe77618a4b332fb65b1 [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
Anssi Hannula9af0e0f2011-02-20 20:07:20 +020092enum hp_return_value {
93 HPWMI_RET_WRONG_SIGNATURE = 0x02,
94 HPWMI_RET_UNKNOWN_COMMAND = 0x03,
95 HPWMI_RET_UNKNOWN_CMDTYPE = 0x04,
96 HPWMI_RET_INVALID_PARAMETERS = 0x05,
97};
98
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -070099static const struct key_entry hp_wmi_keymap[] = {
100 { KE_KEY, 0x02, { KEY_BRIGHTNESSUP } },
101 { KE_KEY, 0x03, { KEY_BRIGHTNESSDOWN } },
102 { KE_KEY, 0x20e6, { KEY_PROG1 } },
103 { KE_KEY, 0x20e8, { KEY_MEDIA } },
104 { KE_KEY, 0x2142, { KEY_MEDIA } },
105 { KE_KEY, 0x213b, { KEY_INFO } },
106 { KE_KEY, 0x2169, { KEY_DIRECTION } },
107 { KE_KEY, 0x231b, { KEY_HELP } },
108 { KE_END, 0 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700109};
110
111static struct input_dev *hp_wmi_input_dev;
112static struct platform_device *hp_wmi_platform_dev;
113
114static struct rfkill *wifi_rfkill;
115static struct rfkill *bluetooth_rfkill;
116static struct rfkill *wwan_rfkill;
117
Alexey Dobriyan47145212009-12-14 18:00:08 -0800118static const struct dev_pm_ops hp_wmi_pm_ops = {
Frans Pop8dd2b422009-08-20 20:38:13 +0200119 .resume = hp_wmi_resume_handler,
120 .restore = hp_wmi_resume_handler,
121};
122
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700123static struct platform_driver hp_wmi_driver = {
124 .driver = {
Frans Pop8dd2b422009-08-20 20:38:13 +0200125 .name = "hp-wmi",
126 .owner = THIS_MODULE,
127 .pm = &hp_wmi_pm_ops,
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700128 },
129 .probe = hp_wmi_bios_setup,
130 .remove = hp_wmi_bios_remove,
131};
132
Thomas Renninger6d96e002010-05-21 16:42:40 +0200133/*
134 * hp_wmi_perform_query
135 *
136 * query: The commandtype -> What should be queried
137 * write: The command -> 0 read, 1 write, 3 ODM specific
138 * buffer: Buffer used as input and/or output
139 * buffersize: Size of buffer
140 *
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);
150 * ret = hp_wmi_perform_query(0x7, 0, buffer, 128)
151 */
Matthew Garretta8ec1052010-08-23 15:52:34 -0400152static int hp_wmi_perform_query(int query, int write, u32 *buffer,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200153 int buffersize)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700154{
155 struct bios_return bios_return;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700156 union acpi_object *obj;
157 struct bios_args args = {
158 .signature = 0x55434553,
159 .command = write ? 0x2 : 0x1,
160 .commandtype = query,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200161 .datasize = buffersize,
Matthew Garretta8ec1052010-08-23 15:52:34 -0400162 .data = *buffer,
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700163 };
164 struct acpi_buffer input = { sizeof(struct bios_args), &args };
165 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
166
Anssi Hannula25bb0672011-02-20 20:07:21 +0200167 wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700168
169 obj = output.pointer;
170
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100171 if (!obj)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700172 return -EINVAL;
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100173 else if (obj->type != ACPI_TYPE_BUFFER) {
174 kfree(obj);
175 return -EINVAL;
176 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700177
178 bios_return = *((struct bios_return *)obj->buffer.pointer);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200179
Anssi Hannula9af0e0f2011-02-20 20:07:20 +0200180 if (bios_return.return_code) {
181 if (bios_return.return_code != HPWMI_RET_UNKNOWN_CMDTYPE)
182 printk(KERN_WARNING PREFIX "query 0x%x returned "
183 "error 0x%x\n",
184 query, bios_return.return_code);
185 kfree(obj);
186 return bios_return.return_code;
187 }
188
Matthew Garretta8ec1052010-08-23 15:52:34 -0400189 memcpy(buffer, &bios_return.value, sizeof(bios_return.value));
Zeng Zhaoming53c96df2010-11-19 00:46:19 +0800190
191 kfree(obj);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200192 return 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700193}
194
195static int hp_wmi_display_state(void)
196{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400197 int state = 0;
198 int ret = hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, &state,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200199 sizeof(state));
200 if (ret)
201 return -EINVAL;
202 return state;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700203}
204
205static int hp_wmi_hddtemp_state(void)
206{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400207 int state = 0;
208 int ret = hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, &state,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200209 sizeof(state));
210 if (ret)
211 return -EINVAL;
212 return state;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700213}
214
215static int hp_wmi_als_state(void)
216{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400217 int state = 0;
218 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, &state,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200219 sizeof(state));
220 if (ret)
221 return -EINVAL;
222 return state;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700223}
224
225static int hp_wmi_dock_state(void)
226{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400227 int state = 0;
228 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, &state,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200229 sizeof(state));
Matthew Garrett871043b2009-06-01 15:25:45 +0100230
Thomas Renninger6d96e002010-05-21 16:42:40 +0200231 if (ret)
232 return -EINVAL;
Matthew Garrett871043b2009-06-01 15:25:45 +0100233
Thomas Renninger6d96e002010-05-21 16:42:40 +0200234 return state & 0x1;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700235}
236
Matthew Garrett871043b2009-06-01 15:25:45 +0100237static int hp_wmi_tablet_state(void)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700238{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400239 int state = 0;
240 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, &state,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200241 sizeof(state));
242 if (ret)
Matthew Garrett871043b2009-06-01 15:25:45 +0100243 return ret;
244
Thomas Renninger6d96e002010-05-21 16:42:40 +0200245 return (state & 0x4) ? 1 : 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700246}
247
Johannes Berg19d337d2009-06-02 13:01:37 +0200248static int hp_wmi_set_block(void *data, bool blocked)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700249{
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100250 enum hp_wmi_radio r = (enum hp_wmi_radio) data;
251 int query = BIT(r + 8) | ((!blocked) << r);
Thomas Renninger6d96e002010-05-21 16:42:40 +0200252 int ret;
Johannes Berg19d337d2009-06-02 13:01:37 +0200253
Thomas Renninger6d96e002010-05-21 16:42:40 +0200254 ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1,
Matthew Garretta8ec1052010-08-23 15:52:34 -0400255 &query, sizeof(query));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200256 if (ret)
257 return -EINVAL;
258 return 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700259}
260
Johannes Berg19d337d2009-06-02 13:01:37 +0200261static const struct rfkill_ops hp_wmi_rfkill_ops = {
262 .set_block = hp_wmi_set_block,
263};
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700264
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100265static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700266{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400267 int wireless = 0;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200268 int mask;
269 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
Matthew Garretta8ec1052010-08-23 15:52:34 -0400270 &wireless, sizeof(wireless));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200271 /* TBD: Pass error */
272
273 mask = 0x200 << (r * 8);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700274
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100275 if (wireless & mask)
Johannes Berg19d337d2009-06-02 13:01:37 +0200276 return false;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700277 else
Johannes Berg19d337d2009-06-02 13:01:37 +0200278 return true;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700279}
280
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100281static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700282{
Matthew Garretta8ec1052010-08-23 15:52:34 -0400283 int wireless = 0;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200284 int mask;
285 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
Matthew Garretta8ec1052010-08-23 15:52:34 -0400286 &wireless, sizeof(wireless));
Thomas Renninger6d96e002010-05-21 16:42:40 +0200287 /* TBD: Pass error */
288
289 mask = 0x800 << (r * 8);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700290
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100291 if (wireless & mask)
Johannes Berg19d337d2009-06-02 13:01:37 +0200292 return false;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700293 else
Johannes Berg19d337d2009-06-02 13:01:37 +0200294 return true;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700295}
296
297static ssize_t show_display(struct device *dev, struct device_attribute *attr,
298 char *buf)
299{
300 int value = hp_wmi_display_state();
301 if (value < 0)
302 return -EINVAL;
303 return sprintf(buf, "%d\n", value);
304}
305
306static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
307 char *buf)
308{
309 int value = hp_wmi_hddtemp_state();
310 if (value < 0)
311 return -EINVAL;
312 return sprintf(buf, "%d\n", value);
313}
314
315static ssize_t show_als(struct device *dev, struct device_attribute *attr,
316 char *buf)
317{
318 int value = hp_wmi_als_state();
319 if (value < 0)
320 return -EINVAL;
321 return sprintf(buf, "%d\n", value);
322}
323
324static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
325 char *buf)
326{
327 int value = hp_wmi_dock_state();
328 if (value < 0)
329 return -EINVAL;
330 return sprintf(buf, "%d\n", value);
331}
332
Matthew Garrett871043b2009-06-01 15:25:45 +0100333static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
334 char *buf)
335{
336 int value = hp_wmi_tablet_state();
337 if (value < 0)
338 return -EINVAL;
339 return sprintf(buf, "%d\n", value);
340}
341
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700342static ssize_t set_als(struct device *dev, struct device_attribute *attr,
343 const char *buf, size_t count)
344{
345 u32 tmp = simple_strtoul(buf, NULL, 10);
Matthew Garretta8ec1052010-08-23 15:52:34 -0400346 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, &tmp,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200347 sizeof(tmp));
348 if (ret)
349 return -EINVAL;
350
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700351 return count;
352}
353
354static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
355static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
356static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
357static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
Matthew Garrett871043b2009-06-01 15:25:45 +0100358static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700359
Adrian Bunk88429a12008-10-15 22:05:17 -0700360static void hp_wmi_notify(u32 value, void *context)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700361{
362 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700363 union acpi_object *obj;
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200364 u32 event_id, event_data;
Matthew Garretta8ec1052010-08-23 15:52:34 -0400365 int key_code = 0, ret;
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200366 u32 *location;
Len Brownfda11e62009-12-26 23:02:24 -0500367 acpi_status status;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700368
Len Brownfda11e62009-12-26 23:02:24 -0500369 status = wmi_get_event_data(value, &response);
370 if (status != AE_OK) {
Thomas Renningera2806c62010-05-21 16:18:11 +0200371 printk(KERN_INFO PREFIX "bad event status 0x%x\n", status);
Len Brownfda11e62009-12-26 23:02:24 -0500372 return;
373 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700374
375 obj = (union acpi_object *)response.pointer;
376
Thomas Renningerc4775062010-07-29 12:27:59 +0200377 if (!obj)
378 return;
379 if (obj->type != ACPI_TYPE_BUFFER) {
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200380 printk(KERN_INFO "hp-wmi: Unknown response received %d\n",
381 obj->type);
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100382 kfree(obj);
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100383 return;
384 }
385
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200386 /*
387 * Depending on ACPI version the concatenation of id and event data
388 * inside _WED function will result in a 8 or 16 byte buffer.
389 */
390 location = (u32 *)obj->buffer.pointer;
391 if (obj->buffer.length == 8) {
392 event_id = *location;
393 event_data = *(location + 1);
394 } else if (obj->buffer.length == 16) {
395 event_id = *location;
396 event_data = *(location + 2);
397 } else {
398 printk(KERN_INFO "hp-wmi: Unknown buffer length %d\n",
399 obj->buffer.length);
400 kfree(obj);
401 return;
402 }
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100403 kfree(obj);
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200404
405 switch (event_id) {
Thomas Renninger751ae802010-05-21 16:18:09 +0200406 case HPWMI_DOCK_EVENT:
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100407 input_report_switch(hp_wmi_input_dev, SW_DOCK,
408 hp_wmi_dock_state());
409 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
410 hp_wmi_tablet_state());
411 input_sync(hp_wmi_input_dev);
Thomas Renninger751ae802010-05-21 16:18:09 +0200412 break;
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200413 case HPWMI_PARK_HDD:
414 break;
415 case HPWMI_SMART_ADAPTER:
416 break;
Thomas Renninger751ae802010-05-21 16:18:09 +0200417 case HPWMI_BEZEL_BUTTON:
Thomas Renninger6d96e002010-05-21 16:42:40 +0200418 ret = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
Matthew Garretta8ec1052010-08-23 15:52:34 -0400419 &key_code,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200420 sizeof(key_code));
421 if (ret)
422 break;
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700423
424 if (!sparse_keymap_report_event(hp_wmi_input_dev,
425 key_code, 1, true))
Thomas Renningera2806c62010-05-21 16:18:11 +0200426 printk(KERN_INFO PREFIX "Unknown key code - 0x%x\n",
Thomas Renningerda9a79b2010-05-21 16:18:10 +0200427 key_code);
Thomas Renninger751ae802010-05-21 16:18:09 +0200428 break;
429 case HPWMI_WIRELESS:
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100430 if (wifi_rfkill)
431 rfkill_set_states(wifi_rfkill,
432 hp_wmi_get_sw_state(HPWMI_WIFI),
433 hp_wmi_get_hw_state(HPWMI_WIFI));
434 if (bluetooth_rfkill)
435 rfkill_set_states(bluetooth_rfkill,
436 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
437 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
438 if (wwan_rfkill)
439 rfkill_set_states(wwan_rfkill,
440 hp_wmi_get_sw_state(HPWMI_WWAN),
441 hp_wmi_get_hw_state(HPWMI_WWAN));
Thomas Renninger751ae802010-05-21 16:18:09 +0200442 break;
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200443 case HPWMI_CPU_BATTERY_THROTTLE:
444 printk(KERN_INFO PREFIX UNIMP "CPU throttle because of 3 Cell"
445 " battery event detected\n");
446 break;
447 case HPWMI_LOCK_SWITCH:
448 break;
Thomas Renninger751ae802010-05-21 16:18:09 +0200449 default:
Thomas Renninger8dda6b02010-05-21 16:41:43 +0200450 printk(KERN_INFO PREFIX "Unknown event_id - %d - 0x%x\n",
451 event_id, event_data);
Thomas Renninger751ae802010-05-21 16:18:09 +0200452 break;
453 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700454}
455
456static int __init hp_wmi_input_setup(void)
457{
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700458 acpi_status status;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700459 int err;
460
461 hp_wmi_input_dev = input_allocate_device();
Axel Linbc285962010-07-20 15:19:51 -0700462 if (!hp_wmi_input_dev)
463 return -ENOMEM;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700464
465 hp_wmi_input_dev->name = "HP WMI hotkeys";
466 hp_wmi_input_dev->phys = "wmi/input0";
467 hp_wmi_input_dev->id.bustype = BUS_HOST;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700468
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700469 __set_bit(EV_SW, hp_wmi_input_dev->evbit);
470 __set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
471 __set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700472
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700473 err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
474 if (err)
475 goto err_free_dev;
Matthew Garrett871043b2009-06-01 15:25:45 +0100476
477 /* Set initial hardware state */
478 input_report_switch(hp_wmi_input_dev, SW_DOCK, hp_wmi_dock_state());
479 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
480 hp_wmi_tablet_state());
481 input_sync(hp_wmi_input_dev);
482
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700483 status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
484 if (ACPI_FAILURE(status)) {
485 err = -EIO;
486 goto err_free_keymap;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700487 }
488
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700489 err = input_register_device(hp_wmi_input_dev);
490 if (err)
491 goto err_uninstall_notifier;
492
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700493 return 0;
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700494
495 err_uninstall_notifier:
496 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
497 err_free_keymap:
498 sparse_keymap_free(hp_wmi_input_dev);
499 err_free_dev:
500 input_free_device(hp_wmi_input_dev);
501 return err;
502}
503
504static void hp_wmi_input_destroy(void)
505{
506 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
507 sparse_keymap_free(hp_wmi_input_dev);
508 input_unregister_device(hp_wmi_input_dev);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700509}
510
511static void cleanup_sysfs(struct platform_device *device)
512{
513 device_remove_file(&device->dev, &dev_attr_display);
514 device_remove_file(&device->dev, &dev_attr_hddtemp);
515 device_remove_file(&device->dev, &dev_attr_als);
516 device_remove_file(&device->dev, &dev_attr_dock);
Matthew Garrett871043b2009-06-01 15:25:45 +0100517 device_remove_file(&device->dev, &dev_attr_tablet);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700518}
519
Uwe Kleine-Königea796322010-02-04 20:56:52 +0100520static int __devinit hp_wmi_bios_setup(struct platform_device *device)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700521{
522 int err;
Matthew Garretta8ec1052010-08-23 15:52:34 -0400523 int wireless = 0;
Thomas Renninger6d96e002010-05-21 16:42:40 +0200524
Matthew Garretta8ec1052010-08-23 15:52:34 -0400525 err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, &wireless,
Thomas Renninger6d96e002010-05-21 16:42:40 +0200526 sizeof(wireless));
527 if (err)
528 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700529
530 err = device_create_file(&device->dev, &dev_attr_display);
531 if (err)
532 goto add_sysfs_error;
533 err = device_create_file(&device->dev, &dev_attr_hddtemp);
534 if (err)
535 goto add_sysfs_error;
536 err = device_create_file(&device->dev, &dev_attr_als);
537 if (err)
538 goto add_sysfs_error;
539 err = device_create_file(&device->dev, &dev_attr_dock);
540 if (err)
541 goto add_sysfs_error;
Matthew Garrett871043b2009-06-01 15:25:45 +0100542 err = device_create_file(&device->dev, &dev_attr_tablet);
543 if (err)
544 goto add_sysfs_error;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700545
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700546 if (wireless & 0x1) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200547 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
548 RFKILL_TYPE_WLAN,
549 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100550 (void *) HPWMI_WIFI);
551 rfkill_init_sw_state(wifi_rfkill,
552 hp_wmi_get_sw_state(HPWMI_WIFI));
553 rfkill_set_hw_state(wifi_rfkill,
554 hp_wmi_get_hw_state(HPWMI_WIFI));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800555 err = rfkill_register(wifi_rfkill);
556 if (err)
Johannes Berg19d337d2009-06-02 13:01:37 +0200557 goto register_wifi_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700558 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700559
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700560 if (wireless & 0x2) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200561 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
562 RFKILL_TYPE_BLUETOOTH,
563 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100564 (void *) HPWMI_BLUETOOTH);
565 rfkill_init_sw_state(bluetooth_rfkill,
566 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
567 rfkill_set_hw_state(bluetooth_rfkill,
568 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800569 err = rfkill_register(bluetooth_rfkill);
Frans Pop6989d562009-01-29 14:25:14 -0800570 if (err)
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800571 goto register_bluetooth_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700572 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700573
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700574 if (wireless & 0x4) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200575 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
576 RFKILL_TYPE_WWAN,
577 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100578 (void *) HPWMI_WWAN);
579 rfkill_init_sw_state(wwan_rfkill,
580 hp_wmi_get_sw_state(HPWMI_WWAN));
581 rfkill_set_hw_state(wwan_rfkill,
582 hp_wmi_get_hw_state(HPWMI_WWAN));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800583 err = rfkill_register(wwan_rfkill);
584 if (err)
585 goto register_wwan_err;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700586 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700587
588 return 0;
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800589register_wwan_err:
Johannes Berg19d337d2009-06-02 13:01:37 +0200590 rfkill_destroy(wwan_rfkill);
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);
Andrew Morton44f06062009-02-04 15:12:07 -0800595 if (wifi_rfkill)
596 rfkill_unregister(wifi_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200597register_wifi_error:
598 rfkill_destroy(wifi_rfkill);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700599add_sysfs_error:
600 cleanup_sysfs(device);
601 return err;
602}
603
604static int __exit hp_wmi_bios_remove(struct platform_device *device)
605{
606 cleanup_sysfs(device);
607
Johannes Berg19d337d2009-06-02 13:01:37 +0200608 if (wifi_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700609 rfkill_unregister(wifi_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200610 rfkill_destroy(wifi_rfkill);
611 }
612 if (bluetooth_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700613 rfkill_unregister(bluetooth_rfkill);
Corentin Chary09729f02009-09-14 12:43:51 +0200614 rfkill_destroy(bluetooth_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200615 }
616 if (wwan_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700617 rfkill_unregister(wwan_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200618 rfkill_destroy(wwan_rfkill);
619 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700620
621 return 0;
622}
623
Frans Pop8dd2b422009-08-20 20:38:13 +0200624static int hp_wmi_resume_handler(struct device *device)
Frans Pop4c395bd2009-03-04 11:55:28 -0800625{
Frans Pop4c395bd2009-03-04 11:55:28 -0800626 /*
Matthew Garrett871043b2009-06-01 15:25:45 +0100627 * Hardware state may have changed while suspended, so trigger
628 * input events for the current state. As this is a switch,
Frans Pop4c395bd2009-03-04 11:55:28 -0800629 * the input layer will only actually pass it on if the state
630 * changed.
631 */
Frans Popdaed9532009-07-30 17:16:05 -0400632 if (hp_wmi_input_dev) {
633 input_report_switch(hp_wmi_input_dev, SW_DOCK,
634 hp_wmi_dock_state());
635 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
636 hp_wmi_tablet_state());
637 input_sync(hp_wmi_input_dev);
638 }
Frans Pop4c395bd2009-03-04 11:55:28 -0800639
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100640 if (wifi_rfkill)
641 rfkill_set_states(wifi_rfkill,
642 hp_wmi_get_sw_state(HPWMI_WIFI),
643 hp_wmi_get_hw_state(HPWMI_WIFI));
644 if (bluetooth_rfkill)
645 rfkill_set_states(bluetooth_rfkill,
646 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
647 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
648 if (wwan_rfkill)
649 rfkill_set_states(wwan_rfkill,
650 hp_wmi_get_sw_state(HPWMI_WWAN),
651 hp_wmi_get_hw_state(HPWMI_WWAN));
652
Frans Pop4c395bd2009-03-04 11:55:28 -0800653 return 0;
654}
655
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700656static int __init hp_wmi_init(void)
657{
658 int err;
Thomas Renningerb0966672010-07-20 15:19:29 -0700659 int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
660 int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700661
Thomas Renningerb0966672010-07-20 15:19:29 -0700662 if (event_capable) {
Axel Lindfec5c42010-06-10 16:06:40 +0800663 err = hp_wmi_input_setup();
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700664 if (err)
Axel Lindfec5c42010-06-10 16:06:40 +0800665 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700666 }
667
Thomas Renningerb0966672010-07-20 15:19:29 -0700668 if (bios_capable) {
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700669 err = platform_driver_register(&hp_wmi_driver);
670 if (err)
Axel Lindfec5c42010-06-10 16:06:40 +0800671 goto err_driver_reg;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700672 hp_wmi_platform_dev = platform_device_alloc("hp-wmi", -1);
673 if (!hp_wmi_platform_dev) {
Axel Lindfec5c42010-06-10 16:06:40 +0800674 err = -ENOMEM;
675 goto err_device_alloc;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700676 }
Axel Lindfec5c42010-06-10 16:06:40 +0800677 err = platform_device_add(hp_wmi_platform_dev);
678 if (err)
679 goto err_device_add;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700680 }
681
Thomas Renningerb0966672010-07-20 15:19:29 -0700682 if (!bios_capable && !event_capable)
683 return -ENODEV;
684
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700685 return 0;
Axel Lindfec5c42010-06-10 16:06:40 +0800686
687err_device_add:
688 platform_device_put(hp_wmi_platform_dev);
689err_device_alloc:
690 platform_driver_unregister(&hp_wmi_driver);
691err_driver_reg:
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700692 if (event_capable)
693 hp_wmi_input_destroy();
Axel Lindfec5c42010-06-10 16:06:40 +0800694
695 return err;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700696}
697
698static void __exit hp_wmi_exit(void)
699{
Dmitry Torokhov4d291ed2010-08-04 22:30:13 -0700700 if (wmi_has_guid(HPWMI_EVENT_GUID))
701 hp_wmi_input_destroy();
702
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700703 if (hp_wmi_platform_dev) {
Axel Lin97ba0af2010-06-03 15:18:03 +0800704 platform_device_unregister(hp_wmi_platform_dev);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700705 platform_driver_unregister(&hp_wmi_driver);
706 }
707}
708
709module_init(hp_wmi_init);
710module_exit(hp_wmi_exit);