blob: cd445019a6b7b6629b171de20101629d0cda8f2b [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>
32#include <acpi/acpi_drivers.h>
33#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;
83 u32 data;
84};
85
86struct bios_return {
87 u32 sigpass;
88 u32 return_code;
89 u32 value;
90};
91
92struct key_entry {
93 char type; /* See KE_* below */
Matthew Garretta8823ae2008-09-02 14:36:03 -070094 u16 code;
Matthew Garrett62ec30d2008-07-25 01:45:39 -070095 u16 keycode;
96};
97
Matthew Garrett871043b2009-06-01 15:25:45 +010098enum { KE_KEY, KE_END };
Matthew Garrett62ec30d2008-07-25 01:45:39 -070099
100static struct key_entry hp_wmi_keymap[] = {
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700101 {KE_KEY, 0x02, KEY_BRIGHTNESSUP},
102 {KE_KEY, 0x03, KEY_BRIGHTNESSDOWN},
Matthew Garretta8823ae2008-09-02 14:36:03 -0700103 {KE_KEY, 0x20e6, KEY_PROG1},
Thomas Renningerf6b2ff02010-05-21 16:18:12 +0200104 {KE_KEY, 0x20e8, KEY_MEDIA},
Matthew Garretta8823ae2008-09-02 14:36:03 -0700105 {KE_KEY, 0x2142, KEY_MEDIA},
Eric Piel5c624842008-10-18 20:27:28 -0700106 {KE_KEY, 0x213b, KEY_INFO},
Matthew Garrettcaeacf52010-02-17 10:29:39 -0500107 {KE_KEY, 0x2169, KEY_DIRECTION},
Matthew Garretta8823ae2008-09-02 14:36:03 -0700108 {KE_KEY, 0x231b, KEY_HELP},
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700109 {KE_END, 0}
110};
111
112static struct input_dev *hp_wmi_input_dev;
113static struct platform_device *hp_wmi_platform_dev;
114
115static struct rfkill *wifi_rfkill;
116static struct rfkill *bluetooth_rfkill;
117static struct rfkill *wwan_rfkill;
118
Alexey Dobriyan47145212009-12-14 18:00:08 -0800119static const struct dev_pm_ops hp_wmi_pm_ops = {
Frans Pop8dd2b422009-08-20 20:38:13 +0200120 .resume = hp_wmi_resume_handler,
121 .restore = hp_wmi_resume_handler,
122};
123
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700124static struct platform_driver hp_wmi_driver = {
125 .driver = {
Frans Pop8dd2b422009-08-20 20:38:13 +0200126 .name = "hp-wmi",
127 .owner = THIS_MODULE,
128 .pm = &hp_wmi_pm_ops,
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700129 },
130 .probe = hp_wmi_bios_setup,
131 .remove = hp_wmi_bios_remove,
132};
133
134static int hp_wmi_perform_query(int query, int write, int value)
135{
136 struct bios_return bios_return;
137 acpi_status status;
138 union acpi_object *obj;
139 struct bios_args args = {
140 .signature = 0x55434553,
141 .command = write ? 0x2 : 0x1,
142 .commandtype = query,
143 .datasize = write ? 0x4 : 0,
144 .data = value,
145 };
146 struct acpi_buffer input = { sizeof(struct bios_args), &args };
147 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
148
149 status = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
150
151 obj = output.pointer;
152
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100153 if (!obj)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700154 return -EINVAL;
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100155 else if (obj->type != ACPI_TYPE_BUFFER) {
156 kfree(obj);
157 return -EINVAL;
158 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700159
160 bios_return = *((struct bios_return *)obj->buffer.pointer);
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100161 kfree(obj);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700162 if (bios_return.return_code > 0)
163 return bios_return.return_code * -1;
164 else
165 return bios_return.value;
166}
167
168static int hp_wmi_display_state(void)
169{
170 return hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, 0);
171}
172
173static int hp_wmi_hddtemp_state(void)
174{
175 return hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, 0);
176}
177
178static int hp_wmi_als_state(void)
179{
180 return hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, 0);
181}
182
183static int hp_wmi_dock_state(void)
184{
Matthew Garrett871043b2009-06-01 15:25:45 +0100185 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, 0);
186
187 if (ret < 0)
188 return ret;
189
190 return ret & 0x1;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700191}
192
Matthew Garrett871043b2009-06-01 15:25:45 +0100193static int hp_wmi_tablet_state(void)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700194{
Matthew Garrett871043b2009-06-01 15:25:45 +0100195 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, 0);
196
197 if (ret < 0)
198 return ret;
199
200 return (ret & 0x4) ? 1 : 0;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700201}
202
Johannes Berg19d337d2009-06-02 13:01:37 +0200203static int hp_wmi_set_block(void *data, bool blocked)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700204{
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100205 enum hp_wmi_radio r = (enum hp_wmi_radio) data;
206 int query = BIT(r + 8) | ((!blocked) << r);
Johannes Berg19d337d2009-06-02 13:01:37 +0200207
208 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, query);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700209}
210
Johannes Berg19d337d2009-06-02 13:01:37 +0200211static const struct rfkill_ops hp_wmi_rfkill_ops = {
212 .set_block = hp_wmi_set_block,
213};
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700214
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100215static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700216{
217 int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100218 int mask = 0x200 << (r * 8);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700219
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100220 if (wireless & mask)
Johannes Berg19d337d2009-06-02 13:01:37 +0200221 return false;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700222 else
Johannes Berg19d337d2009-06-02 13:01:37 +0200223 return true;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700224}
225
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100226static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700227{
228 int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100229 int mask = 0x800 << (r * 8);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700230
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100231 if (wireless & mask)
Johannes Berg19d337d2009-06-02 13:01:37 +0200232 return false;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700233 else
Johannes Berg19d337d2009-06-02 13:01:37 +0200234 return true;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700235}
236
237static ssize_t show_display(struct device *dev, struct device_attribute *attr,
238 char *buf)
239{
240 int value = hp_wmi_display_state();
241 if (value < 0)
242 return -EINVAL;
243 return sprintf(buf, "%d\n", value);
244}
245
246static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
247 char *buf)
248{
249 int value = hp_wmi_hddtemp_state();
250 if (value < 0)
251 return -EINVAL;
252 return sprintf(buf, "%d\n", value);
253}
254
255static ssize_t show_als(struct device *dev, struct device_attribute *attr,
256 char *buf)
257{
258 int value = hp_wmi_als_state();
259 if (value < 0)
260 return -EINVAL;
261 return sprintf(buf, "%d\n", value);
262}
263
264static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
265 char *buf)
266{
267 int value = hp_wmi_dock_state();
268 if (value < 0)
269 return -EINVAL;
270 return sprintf(buf, "%d\n", value);
271}
272
Matthew Garrett871043b2009-06-01 15:25:45 +0100273static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
274 char *buf)
275{
276 int value = hp_wmi_tablet_state();
277 if (value < 0)
278 return -EINVAL;
279 return sprintf(buf, "%d\n", value);
280}
281
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700282static ssize_t set_als(struct device *dev, struct device_attribute *attr,
283 const char *buf, size_t count)
284{
285 u32 tmp = simple_strtoul(buf, NULL, 10);
286 hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, tmp);
287 return count;
288}
289
290static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
291static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
292static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
293static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
Matthew Garrett871043b2009-06-01 15:25:45 +0100294static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700295
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800296static struct key_entry *hp_wmi_get_entry_by_scancode(unsigned int code)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700297{
298 struct key_entry *key;
299
300 for (key = hp_wmi_keymap; key->type != KE_END; key++)
301 if (code == key->code)
302 return key;
303
304 return NULL;
305}
306
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800307static struct key_entry *hp_wmi_get_entry_by_keycode(unsigned int keycode)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700308{
309 struct key_entry *key;
310
311 for (key = hp_wmi_keymap; key->type != KE_END; key++)
312 if (key->type == KE_KEY && keycode == key->keycode)
313 return key;
314
315 return NULL;
316}
317
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800318static int hp_wmi_getkeycode(struct input_dev *dev,
319 unsigned int scancode, unsigned int *keycode)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700320{
321 struct key_entry *key = hp_wmi_get_entry_by_scancode(scancode);
322
323 if (key && key->type == KE_KEY) {
324 *keycode = key->keycode;
325 return 0;
326 }
327
328 return -EINVAL;
329}
330
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800331static int hp_wmi_setkeycode(struct input_dev *dev,
332 unsigned int scancode, unsigned int keycode)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700333{
334 struct key_entry *key;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800335 unsigned int old_keycode;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700336
337 key = hp_wmi_get_entry_by_scancode(scancode);
338 if (key && key->type == KE_KEY) {
339 old_keycode = key->keycode;
340 key->keycode = keycode;
341 set_bit(keycode, dev->keybit);
342 if (!hp_wmi_get_entry_by_keycode(old_keycode))
343 clear_bit(old_keycode, dev->keybit);
344 return 0;
345 }
346
347 return -EINVAL;
348}
349
Adrian Bunk88429a12008-10-15 22:05:17 -0700350static void hp_wmi_notify(u32 value, void *context)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700351{
352 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
353 static struct key_entry *key;
354 union acpi_object *obj;
Thomas Renninger751ae802010-05-21 16:18:09 +0200355 int eventcode, key_code;
Len Brownfda11e62009-12-26 23:02:24 -0500356 acpi_status status;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700357
Len Brownfda11e62009-12-26 23:02:24 -0500358 status = wmi_get_event_data(value, &response);
359 if (status != AE_OK) {
Thomas Renningera2806c62010-05-21 16:18:11 +0200360 printk(KERN_INFO PREFIX "bad event status 0x%x\n", status);
Len Brownfda11e62009-12-26 23:02:24 -0500361 return;
362 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700363
364 obj = (union acpi_object *)response.pointer;
365
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100366 if (!obj || obj->type != ACPI_TYPE_BUFFER || obj->buffer.length != 8) {
Thomas Renningera2806c62010-05-21 16:18:11 +0200367 printk(KERN_INFO PREFIX "Unknown response received\n");
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100368 kfree(obj);
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100369 return;
370 }
371
372 eventcode = *((u8 *) obj->buffer.pointer);
Thomas Renninger44ef00e2009-12-18 15:29:23 +0100373 kfree(obj);
Thomas Renninger751ae802010-05-21 16:18:09 +0200374 switch (eventcode) {
375 case HPWMI_DOCK_EVENT:
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100376 input_report_switch(hp_wmi_input_dev, SW_DOCK,
377 hp_wmi_dock_state());
378 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
379 hp_wmi_tablet_state());
380 input_sync(hp_wmi_input_dev);
Thomas Renninger751ae802010-05-21 16:18:09 +0200381 break;
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200382 case HPWMI_PARK_HDD:
383 break;
384 case HPWMI_SMART_ADAPTER:
385 break;
Thomas Renninger751ae802010-05-21 16:18:09 +0200386 case HPWMI_BEZEL_BUTTON:
387 key_code = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
388 0);
389 key = hp_wmi_get_entry_by_scancode(key_code);
390 if (key) {
391 switch (key->type) {
392 case KE_KEY:
393 input_report_key(hp_wmi_input_dev,
394 key->keycode, 1);
395 input_sync(hp_wmi_input_dev);
396 input_report_key(hp_wmi_input_dev,
397 key->keycode, 0);
398 input_sync(hp_wmi_input_dev);
399 break;
400 }
Thomas Renningerda9a79b2010-05-21 16:18:10 +0200401 } else
Thomas Renningera2806c62010-05-21 16:18:11 +0200402 printk(KERN_INFO PREFIX "Unknown key code - 0x%x\n",
Thomas Renningerda9a79b2010-05-21 16:18:10 +0200403 key_code);
Thomas Renninger751ae802010-05-21 16:18:09 +0200404 break;
405 case HPWMI_WIRELESS:
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100406 if (wifi_rfkill)
407 rfkill_set_states(wifi_rfkill,
408 hp_wmi_get_sw_state(HPWMI_WIFI),
409 hp_wmi_get_hw_state(HPWMI_WIFI));
410 if (bluetooth_rfkill)
411 rfkill_set_states(bluetooth_rfkill,
412 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
413 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
414 if (wwan_rfkill)
415 rfkill_set_states(wwan_rfkill,
416 hp_wmi_get_sw_state(HPWMI_WWAN),
417 hp_wmi_get_hw_state(HPWMI_WWAN));
Thomas Renninger751ae802010-05-21 16:18:09 +0200418 break;
Thomas Renninger1bbdfd52010-05-21 16:18:13 +0200419 case HPWMI_CPU_BATTERY_THROTTLE:
420 printk(KERN_INFO PREFIX UNIMP "CPU throttle because of 3 Cell"
421 " battery event detected\n");
422 break;
423 case HPWMI_LOCK_SWITCH:
424 break;
Thomas Renninger751ae802010-05-21 16:18:09 +0200425 default:
Thomas Renningera2806c62010-05-21 16:18:11 +0200426 printk(KERN_INFO PREFIX "Unknown eventcode - %d\n",
Thomas Renningerda9a79b2010-05-21 16:18:10 +0200427 eventcode);
Thomas Renninger751ae802010-05-21 16:18:09 +0200428 break;
429 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700430}
431
432static int __init hp_wmi_input_setup(void)
433{
434 struct key_entry *key;
435 int err;
436
437 hp_wmi_input_dev = input_allocate_device();
438
439 hp_wmi_input_dev->name = "HP WMI hotkeys";
440 hp_wmi_input_dev->phys = "wmi/input0";
441 hp_wmi_input_dev->id.bustype = BUS_HOST;
442 hp_wmi_input_dev->getkeycode = hp_wmi_getkeycode;
443 hp_wmi_input_dev->setkeycode = hp_wmi_setkeycode;
444
445 for (key = hp_wmi_keymap; key->type != KE_END; key++) {
446 switch (key->type) {
447 case KE_KEY:
448 set_bit(EV_KEY, hp_wmi_input_dev->evbit);
449 set_bit(key->keycode, hp_wmi_input_dev->keybit);
450 break;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700451 }
452 }
453
Matthew Garrett871043b2009-06-01 15:25:45 +0100454 set_bit(EV_SW, hp_wmi_input_dev->evbit);
455 set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
456 set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
457
458 /* Set initial hardware state */
459 input_report_switch(hp_wmi_input_dev, SW_DOCK, hp_wmi_dock_state());
460 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
461 hp_wmi_tablet_state());
462 input_sync(hp_wmi_input_dev);
463
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700464 err = input_register_device(hp_wmi_input_dev);
465
466 if (err) {
467 input_free_device(hp_wmi_input_dev);
468 return err;
469 }
470
471 return 0;
472}
473
474static void cleanup_sysfs(struct platform_device *device)
475{
476 device_remove_file(&device->dev, &dev_attr_display);
477 device_remove_file(&device->dev, &dev_attr_hddtemp);
478 device_remove_file(&device->dev, &dev_attr_als);
479 device_remove_file(&device->dev, &dev_attr_dock);
Matthew Garrett871043b2009-06-01 15:25:45 +0100480 device_remove_file(&device->dev, &dev_attr_tablet);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700481}
482
Uwe Kleine-Königea796322010-02-04 20:56:52 +0100483static int __devinit hp_wmi_bios_setup(struct platform_device *device)
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700484{
485 int err;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700486 int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700487
488 err = device_create_file(&device->dev, &dev_attr_display);
489 if (err)
490 goto add_sysfs_error;
491 err = device_create_file(&device->dev, &dev_attr_hddtemp);
492 if (err)
493 goto add_sysfs_error;
494 err = device_create_file(&device->dev, &dev_attr_als);
495 if (err)
496 goto add_sysfs_error;
497 err = device_create_file(&device->dev, &dev_attr_dock);
498 if (err)
499 goto add_sysfs_error;
Matthew Garrett871043b2009-06-01 15:25:45 +0100500 err = device_create_file(&device->dev, &dev_attr_tablet);
501 if (err)
502 goto add_sysfs_error;
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700503
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700504 if (wireless & 0x1) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200505 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
506 RFKILL_TYPE_WLAN,
507 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100508 (void *) HPWMI_WIFI);
509 rfkill_init_sw_state(wifi_rfkill,
510 hp_wmi_get_sw_state(HPWMI_WIFI));
511 rfkill_set_hw_state(wifi_rfkill,
512 hp_wmi_get_hw_state(HPWMI_WIFI));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800513 err = rfkill_register(wifi_rfkill);
514 if (err)
Johannes Berg19d337d2009-06-02 13:01:37 +0200515 goto register_wifi_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700516 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700517
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700518 if (wireless & 0x2) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200519 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
520 RFKILL_TYPE_BLUETOOTH,
521 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100522 (void *) HPWMI_BLUETOOTH);
523 rfkill_init_sw_state(bluetooth_rfkill,
524 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
525 rfkill_set_hw_state(bluetooth_rfkill,
526 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800527 err = rfkill_register(bluetooth_rfkill);
Frans Pop6989d562009-01-29 14:25:14 -0800528 if (err)
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800529 goto register_bluetooth_error;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700530 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700531
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700532 if (wireless & 0x4) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200533 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
534 RFKILL_TYPE_WWAN,
535 &hp_wmi_rfkill_ops,
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100536 (void *) HPWMI_WWAN);
537 rfkill_init_sw_state(wwan_rfkill,
538 hp_wmi_get_sw_state(HPWMI_WWAN));
539 rfkill_set_hw_state(wwan_rfkill,
540 hp_wmi_get_hw_state(HPWMI_WWAN));
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800541 err = rfkill_register(wwan_rfkill);
542 if (err)
543 goto register_wwan_err;
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700544 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700545
546 return 0;
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800547register_wwan_err:
Johannes Berg19d337d2009-06-02 13:01:37 +0200548 rfkill_destroy(wwan_rfkill);
Andrew Morton44f06062009-02-04 15:12:07 -0800549 if (bluetooth_rfkill)
550 rfkill_unregister(bluetooth_rfkill);
Larry Fingerfe8e4e02009-01-09 16:40:54 -0800551register_bluetooth_error:
Johannes Berg19d337d2009-06-02 13:01:37 +0200552 rfkill_destroy(bluetooth_rfkill);
Andrew Morton44f06062009-02-04 15:12:07 -0800553 if (wifi_rfkill)
554 rfkill_unregister(wifi_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200555register_wifi_error:
556 rfkill_destroy(wifi_rfkill);
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700557add_sysfs_error:
558 cleanup_sysfs(device);
559 return err;
560}
561
562static int __exit hp_wmi_bios_remove(struct platform_device *device)
563{
564 cleanup_sysfs(device);
565
Johannes Berg19d337d2009-06-02 13:01:37 +0200566 if (wifi_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700567 rfkill_unregister(wifi_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200568 rfkill_destroy(wifi_rfkill);
569 }
570 if (bluetooth_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700571 rfkill_unregister(bluetooth_rfkill);
Corentin Chary09729f02009-09-14 12:43:51 +0200572 rfkill_destroy(bluetooth_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200573 }
574 if (wwan_rfkill) {
Matthew Garrett3f6e2f12008-09-02 14:36:00 -0700575 rfkill_unregister(wwan_rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200576 rfkill_destroy(wwan_rfkill);
577 }
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700578
579 return 0;
580}
581
Frans Pop8dd2b422009-08-20 20:38:13 +0200582static int hp_wmi_resume_handler(struct device *device)
Frans Pop4c395bd2009-03-04 11:55:28 -0800583{
Frans Pop4c395bd2009-03-04 11:55:28 -0800584 /*
Matthew Garrett871043b2009-06-01 15:25:45 +0100585 * Hardware state may have changed while suspended, so trigger
586 * input events for the current state. As this is a switch,
Frans Pop4c395bd2009-03-04 11:55:28 -0800587 * the input layer will only actually pass it on if the state
588 * changed.
589 */
Frans Popdaed9532009-07-30 17:16:05 -0400590 if (hp_wmi_input_dev) {
591 input_report_switch(hp_wmi_input_dev, SW_DOCK,
592 hp_wmi_dock_state());
593 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
594 hp_wmi_tablet_state());
595 input_sync(hp_wmi_input_dev);
596 }
Frans Pop4c395bd2009-03-04 11:55:28 -0800597
Alan Jenkinse5fbba82009-07-21 12:14:01 +0100598 if (wifi_rfkill)
599 rfkill_set_states(wifi_rfkill,
600 hp_wmi_get_sw_state(HPWMI_WIFI),
601 hp_wmi_get_hw_state(HPWMI_WIFI));
602 if (bluetooth_rfkill)
603 rfkill_set_states(bluetooth_rfkill,
604 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
605 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
606 if (wwan_rfkill)
607 rfkill_set_states(wwan_rfkill,
608 hp_wmi_get_sw_state(HPWMI_WWAN),
609 hp_wmi_get_hw_state(HPWMI_WWAN));
610
Frans Pop4c395bd2009-03-04 11:55:28 -0800611 return 0;
612}
613
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700614static int __init hp_wmi_init(void)
615{
616 int err;
617
618 if (wmi_has_guid(HPWMI_EVENT_GUID)) {
619 err = wmi_install_notify_handler(HPWMI_EVENT_GUID,
620 hp_wmi_notify, NULL);
Len Brownf2772572009-12-26 22:04:03 -0500621 if (ACPI_SUCCESS(err))
Matthew Garrett62ec30d2008-07-25 01:45:39 -0700622 hp_wmi_input_setup();
623 }
624
625 if (wmi_has_guid(HPWMI_BIOS_GUID)) {
626 err = platform_driver_register(&hp_wmi_driver);
627 if (err)
628 return 0;
629 hp_wmi_platform_dev = platform_device_alloc("hp-wmi", -1);
630 if (!hp_wmi_platform_dev) {
631 platform_driver_unregister(&hp_wmi_driver);
632 return 0;
633 }
634 platform_device_add(hp_wmi_platform_dev);
635 }
636
637 return 0;
638}
639
640static void __exit hp_wmi_exit(void)
641{
642 if (wmi_has_guid(HPWMI_EVENT_GUID)) {
643 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
644 input_unregister_device(hp_wmi_input_dev);
645 }
646 if (hp_wmi_platform_dev) {
647 platform_device_del(hp_wmi_platform_dev);
648 platform_driver_unregister(&hp_wmi_driver);
649 }
650}
651
652module_init(hp_wmi_init);
653module_exit(hp_wmi_exit);