drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / platform / x86 / hp-wmi.c
CommitLineData
62ec30d4
MG
1/*
2 * HP WMI hotkeys
3 *
4 * Copyright (C) 2008 Red Hat <mjg@redhat.com>
c0b9c649 5 * Copyright (C) 2010, 2011 Anssi Hannula <anssi.hannula@iki.fi>
62ec30d4
MG
6 *
7 * Portions based on wistron_btns.c:
8 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
9 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
10 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
b5a4223c
JP
27#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
62ec30d4
MG
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
5a0e3ad6 32#include <linux/slab.h>
62ec30d4
MG
33#include <linux/types.h>
34#include <linux/input.h>
4d291ed7 35#include <linux/input/sparse-keymap.h>
62ec30d4
MG
36#include <linux/platform_device.h>
37#include <linux/acpi.h>
38#include <linux/rfkill.h>
39#include <linux/string.h>
40
41MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>");
42MODULE_DESCRIPTION("HP laptop WMI hotkeys driver");
43MODULE_LICENSE("GPL");
44
45MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
46MODULE_ALIAS("wmi:5FB7F034-2C63-45e9-BE91-3D44E2C707E4");
47
48#define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
49#define HPWMI_BIOS_GUID "5FB7F034-2C63-45e9-BE91-3D44E2C707E4"
50
51#define HPWMI_DISPLAY_QUERY 0x1
52#define HPWMI_HDDTEMP_QUERY 0x2
53#define HPWMI_ALS_QUERY 0x3
871043bc 54#define HPWMI_HARDWARE_QUERY 0x4
62ec30d4 55#define HPWMI_WIRELESS_QUERY 0x5
a8823aef 56#define HPWMI_HOTKEY_QUERY 0xc
c0b9c649 57#define HPWMI_WIRELESS2_QUERY 0x1b
62ec30d4 58
e5fbba85
AJ
59enum hp_wmi_radio {
60 HPWMI_WIFI = 0,
61 HPWMI_BLUETOOTH = 1,
62 HPWMI_WWAN = 2,
4fca7ce3 63 HPWMI_GPS = 3,
e5fbba85
AJ
64};
65
751ae808
TR
66enum hp_wmi_event_ids {
67 HPWMI_DOCK_EVENT = 1,
1bbdfd59
TR
68 HPWMI_PARK_HDD = 2,
69 HPWMI_SMART_ADAPTER = 3,
751ae808
TR
70 HPWMI_BEZEL_BUTTON = 4,
71 HPWMI_WIRELESS = 5,
1bbdfd59
TR
72 HPWMI_CPU_BATTERY_THROTTLE = 6,
73 HPWMI_LOCK_SWITCH = 7,
d9e290a0
AH
74 HPWMI_LID_SWITCH = 8,
75 HPWMI_SCREEN_ROTATION = 9,
76 HPWMI_COOLSENSE_SYSTEM_MOBILE = 0x0A,
77 HPWMI_COOLSENSE_SYSTEM_HOT = 0x0B,
78 HPWMI_PROXIMITY_SENSOR = 0x0C,
79 HPWMI_BACKLIT_KB_BRIGHTNESS = 0x0D,
80 HPWMI_PEAKSHIFT_PERIOD = 0x0F,
81 HPWMI_BATTERY_CHARGE_PERIOD = 0x10,
751ae808
TR
82};
83
62ec30d4
MG
84struct bios_args {
85 u32 signature;
86 u32 command;
87 u32 commandtype;
88 u32 datasize;
a8ec105c 89 u32 data;
62ec30d4
MG
90};
91
92struct bios_return {
93 u32 sigpass;
94 u32 return_code;
62ec30d4
MG
95};
96
9af0e0fb
AH
97enum hp_return_value {
98 HPWMI_RET_WRONG_SIGNATURE = 0x02,
99 HPWMI_RET_UNKNOWN_COMMAND = 0x03,
100 HPWMI_RET_UNKNOWN_CMDTYPE = 0x04,
101 HPWMI_RET_INVALID_PARAMETERS = 0x05,
102};
103
c0b9c649
AH
104enum hp_wireless2_bits {
105 HPWMI_POWER_STATE = 0x01,
106 HPWMI_POWER_SOFT = 0x02,
107 HPWMI_POWER_BIOS = 0x04,
108 HPWMI_POWER_HARD = 0x08,
109};
110
111#define IS_HWBLOCKED(x) ((x & (HPWMI_POWER_BIOS | HPWMI_POWER_HARD)) \
112 != (HPWMI_POWER_BIOS | HPWMI_POWER_HARD))
113#define IS_SWBLOCKED(x) !(x & HPWMI_POWER_SOFT)
114
115struct bios_rfkill2_device_state {
116 u8 radio_type;
117 u8 bus_type;
118 u16 vendor_id;
119 u16 product_id;
120 u16 subsys_vendor_id;
121 u16 subsys_product_id;
122 u8 rfkill_id;
123 u8 power;
124 u8 unknown[4];
125};
126
127/* 7 devices fit into the 128 byte buffer */
128#define HPWMI_MAX_RFKILL2_DEVICES 7
129
130struct bios_rfkill2_state {
131 u8 unknown[7];
132 u8 count;
133 u8 pad[8];
134 struct bios_rfkill2_device_state device[HPWMI_MAX_RFKILL2_DEVICES];
135};
136
4d291ed7
DT
137static const struct key_entry hp_wmi_keymap[] = {
138 { KE_KEY, 0x02, { KEY_BRIGHTNESSUP } },
139 { KE_KEY, 0x03, { KEY_BRIGHTNESSDOWN } },
140 { KE_KEY, 0x20e6, { KEY_PROG1 } },
141 { KE_KEY, 0x20e8, { KEY_MEDIA } },
142 { KE_KEY, 0x2142, { KEY_MEDIA } },
143 { KE_KEY, 0x213b, { KEY_INFO } },
144 { KE_KEY, 0x2169, { KEY_DIRECTION } },
145 { KE_KEY, 0x231b, { KEY_HELP } },
146 { KE_END, 0 }
62ec30d4
MG
147};
148
149static struct input_dev *hp_wmi_input_dev;
150static struct platform_device *hp_wmi_platform_dev;
151
152static struct rfkill *wifi_rfkill;
153static struct rfkill *bluetooth_rfkill;
154static struct rfkill *wwan_rfkill;
4fca7ce3 155static struct rfkill *gps_rfkill;
62ec30d4 156
c0b9c649
AH
157struct rfkill2_device {
158 u8 id;
159 int num;
160 struct rfkill *rfkill;
161};
162
163static int rfkill2_count;
164static struct rfkill2_device rfkill2[HPWMI_MAX_RFKILL2_DEVICES];
165
6d96e00c
TR
166/*
167 * hp_wmi_perform_query
168 *
169 * query: The commandtype -> What should be queried
170 * write: The command -> 0 read, 1 write, 3 ODM specific
171 * buffer: Buffer used as input and/or output
c3021ea1
AH
172 * insize: Size of input buffer
173 * outsize: Size of output buffer
6d96e00c
TR
174 *
175 * returns zero on success
176 * an HP WMI query specific error code (which is positive)
177 * -EINVAL if the query was not successful at all
178 * -EINVAL if the output buffer size exceeds buffersize
179 *
180 * Note: The buffersize must at least be the maximum of the input and output
181 * size. E.g. Battery info query (0x7) is defined to have 1 byte input
182 * and 128 byte output. The caller would do:
183 * buffer = kzalloc(128, GFP_KERNEL);
c3021ea1 184 * ret = hp_wmi_perform_query(0x7, 0, buffer, 1, 128)
6d96e00c 185 */
c3021ea1
AH
186static int hp_wmi_perform_query(int query, int write, void *buffer,
187 int insize, int outsize)
62ec30d4 188{
c3021ea1
AH
189 struct bios_return *bios_return;
190 int actual_outsize;
62ec30d4
MG
191 union acpi_object *obj;
192 struct bios_args args = {
193 .signature = 0x55434553,
194 .command = write ? 0x2 : 0x1,
195 .commandtype = query,
c3021ea1
AH
196 .datasize = insize,
197 .data = 0,
62ec30d4
MG
198 };
199 struct acpi_buffer input = { sizeof(struct bios_args), &args };
200 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
0401846c 201 u32 rc;
62ec30d4 202
c3021ea1
AH
203 if (WARN_ON(insize > sizeof(args.data)))
204 return -EINVAL;
205 memcpy(&args.data, buffer, insize);
206
25bb067a 207 wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
62ec30d4
MG
208
209 obj = output.pointer;
210
44ef00e6 211 if (!obj)
62ec30d4 212 return -EINVAL;
44ef00e6
TR
213 else if (obj->type != ACPI_TYPE_BUFFER) {
214 kfree(obj);
215 return -EINVAL;
216 }
62ec30d4 217
c3021ea1 218 bios_return = (struct bios_return *)obj->buffer.pointer;
0401846c 219 rc = bios_return->return_code;
6d96e00c 220
0401846c
ED
221 if (rc) {
222 if (rc != HPWMI_RET_UNKNOWN_CMDTYPE)
223 pr_warn("query 0x%x returned error 0x%x\n", query, rc);
9af0e0fb 224 kfree(obj);
0401846c 225 return rc;
9af0e0fb
AH
226 }
227
c3021ea1
AH
228 if (!outsize) {
229 /* ignore output data */
230 kfree(obj);
231 return 0;
232 }
53c96dfd 233
c3021ea1
AH
234 actual_outsize = min(outsize, (int)(obj->buffer.length - sizeof(*bios_return)));
235 memcpy(buffer, obj->buffer.pointer + sizeof(*bios_return), actual_outsize);
236 memset(buffer + actual_outsize, 0, outsize - actual_outsize);
53c96dfd 237 kfree(obj);
6d96e00c 238 return 0;
62ec30d4
MG
239}
240
241static int hp_wmi_display_state(void)
242{
a8ec105c
MG
243 int state = 0;
244 int ret = hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, &state,
c3021ea1 245 sizeof(state), sizeof(state));
6d96e00c
TR
246 if (ret)
247 return -EINVAL;
248 return state;
62ec30d4
MG
249}
250
251static int hp_wmi_hddtemp_state(void)
252{
a8ec105c
MG
253 int state = 0;
254 int ret = hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, &state,
c3021ea1 255 sizeof(state), sizeof(state));
6d96e00c
TR
256 if (ret)
257 return -EINVAL;
258 return state;
62ec30d4
MG
259}
260
261static int hp_wmi_als_state(void)
262{
a8ec105c
MG
263 int state = 0;
264 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, &state,
c3021ea1 265 sizeof(state), sizeof(state));
6d96e00c
TR
266 if (ret)
267 return -EINVAL;
268 return state;
62ec30d4
MG
269}
270
271static int hp_wmi_dock_state(void)
272{
a8ec105c
MG
273 int state = 0;
274 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, &state,
c3021ea1 275 sizeof(state), sizeof(state));
62ec30d4 276
6d96e00c
TR
277 if (ret)
278 return -EINVAL;
871043bc 279
6d96e00c 280 return state & 0x1;
62ec30d4
MG
281}
282
871043bc 283static int hp_wmi_tablet_state(void)
62ec30d4 284{
a8ec105c
MG
285 int state = 0;
286 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, &state,
c3021ea1 287 sizeof(state), sizeof(state));
6d96e00c 288 if (ret)
871043bc
MG
289 return ret;
290
6d96e00c 291 return (state & 0x4) ? 1 : 0;
62ec30d4
MG
292}
293
19d337df 294static int hp_wmi_set_block(void *data, bool blocked)
62ec30d4 295{
e5fbba85
AJ
296 enum hp_wmi_radio r = (enum hp_wmi_radio) data;
297 int query = BIT(r + 8) | ((!blocked) << r);
6d96e00c 298 int ret;
62ec30d4 299
6d96e00c 300 ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1,
c3021ea1 301 &query, sizeof(query), 0);
6d96e00c
TR
302 if (ret)
303 return -EINVAL;
304 return 0;
62ec30d4
MG
305}
306
19d337df
JB
307static const struct rfkill_ops hp_wmi_rfkill_ops = {
308 .set_block = hp_wmi_set_block,
309};
62ec30d4 310
e5fbba85 311static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
62ec30d4 312{
a8ec105c 313 int wireless = 0;
6d96e00c
TR
314 int mask;
315 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
c3021ea1
AH
316 &wireless, sizeof(wireless),
317 sizeof(wireless));
6d96e00c
TR
318 /* TBD: Pass error */
319
320 mask = 0x200 << (r * 8);
62ec30d4 321
e5fbba85 322 if (wireless & mask)
19d337df 323 return false;
62ec30d4 324 else
19d337df 325 return true;
62ec30d4
MG
326}
327
e5fbba85 328static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
62ec30d4 329{
a8ec105c 330 int wireless = 0;
6d96e00c
TR
331 int mask;
332 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
c3021ea1
AH
333 &wireless, sizeof(wireless),
334 sizeof(wireless));
6d96e00c
TR
335 /* TBD: Pass error */
336
337 mask = 0x800 << (r * 8);
62ec30d4 338
e5fbba85 339 if (wireless & mask)
19d337df 340 return false;
62ec30d4 341 else
19d337df 342 return true;
62ec30d4
MG
343}
344
c0b9c649
AH
345static int hp_wmi_rfkill2_set_block(void *data, bool blocked)
346{
347 int rfkill_id = (int)(long)data;
348 char buffer[4] = { 0x01, 0x00, rfkill_id, !blocked };
349
350 if (hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, 1,
351 buffer, sizeof(buffer), 0))
352 return -EINVAL;
353 return 0;
354}
355
356static const struct rfkill_ops hp_wmi_rfkill2_ops = {
357 .set_block = hp_wmi_rfkill2_set_block,
358};
359
360static int hp_wmi_rfkill2_refresh(void)
361{
362 int err, i;
363 struct bios_rfkill2_state state;
364
365 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, 0, &state,
366 0, sizeof(state));
367 if (err)
368 return err;
369
370 for (i = 0; i < rfkill2_count; i++) {
371 int num = rfkill2[i].num;
372 struct bios_rfkill2_device_state *devstate;
373 devstate = &state.device[num];
374
375 if (num >= state.count ||
376 devstate->rfkill_id != rfkill2[i].id) {
b5a4223c 377 pr_warn("power configuration of the wireless devices unexpectedly changed\n");
c0b9c649
AH
378 continue;
379 }
380
381 rfkill_set_states(rfkill2[i].rfkill,
382 IS_SWBLOCKED(devstate->power),
383 IS_HWBLOCKED(devstate->power));
384 }
385
386 return 0;
387}
388
62ec30d4
MG
389static ssize_t show_display(struct device *dev, struct device_attribute *attr,
390 char *buf)
391{
392 int value = hp_wmi_display_state();
393 if (value < 0)
394 return -EINVAL;
395 return sprintf(buf, "%d\n", value);
396}
397
398static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
399 char *buf)
400{
401 int value = hp_wmi_hddtemp_state();
402 if (value < 0)
403 return -EINVAL;
404 return sprintf(buf, "%d\n", value);
405}
406
407static ssize_t show_als(struct device *dev, struct device_attribute *attr,
408 char *buf)
409{
410 int value = hp_wmi_als_state();
411 if (value < 0)
412 return -EINVAL;
413 return sprintf(buf, "%d\n", value);
414}
415
416static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
417 char *buf)
418{
419 int value = hp_wmi_dock_state();
420 if (value < 0)
421 return -EINVAL;
422 return sprintf(buf, "%d\n", value);
423}
424
871043bc
MG
425static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
426 char *buf)
427{
428 int value = hp_wmi_tablet_state();
429 if (value < 0)
430 return -EINVAL;
431 return sprintf(buf, "%d\n", value);
432}
433
62ec30d4
MG
434static ssize_t set_als(struct device *dev, struct device_attribute *attr,
435 const char *buf, size_t count)
436{
437 u32 tmp = simple_strtoul(buf, NULL, 10);
a8ec105c 438 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, &tmp,
c3021ea1 439 sizeof(tmp), sizeof(tmp));
6d96e00c
TR
440 if (ret)
441 return -EINVAL;
442
62ec30d4
MG
443 return count;
444}
445
446static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
447static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
448static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
449static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
871043bc 450static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
62ec30d4 451
88429a10 452static void hp_wmi_notify(u32 value, void *context)
62ec30d4
MG
453{
454 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
62ec30d4 455 union acpi_object *obj;
8dda6b04 456 u32 event_id, event_data;
a8ec105c 457 int key_code = 0, ret;
8dda6b04 458 u32 *location;
fda11e61 459 acpi_status status;
62ec30d4 460
fda11e61
LB
461 status = wmi_get_event_data(value, &response);
462 if (status != AE_OK) {
b5a4223c 463 pr_info("bad event status 0x%x\n", status);
fda11e61
LB
464 return;
465 }
62ec30d4
MG
466
467 obj = (union acpi_object *)response.pointer;
468
c4775062
TR
469 if (!obj)
470 return;
471 if (obj->type != ACPI_TYPE_BUFFER) {
b5a4223c 472 pr_info("Unknown response received %d\n", obj->type);
44ef00e6 473 kfree(obj);
e5fbba85
AJ
474 return;
475 }
476
8dda6b04
TR
477 /*
478 * Depending on ACPI version the concatenation of id and event data
479 * inside _WED function will result in a 8 or 16 byte buffer.
480 */
481 location = (u32 *)obj->buffer.pointer;
482 if (obj->buffer.length == 8) {
483 event_id = *location;
484 event_data = *(location + 1);
485 } else if (obj->buffer.length == 16) {
486 event_id = *location;
487 event_data = *(location + 2);
488 } else {
b5a4223c 489 pr_info("Unknown buffer length %d\n", obj->buffer.length);
8dda6b04
TR
490 kfree(obj);
491 return;
492 }
44ef00e6 493 kfree(obj);
8dda6b04
TR
494
495 switch (event_id) {
751ae808 496 case HPWMI_DOCK_EVENT:
e5fbba85
AJ
497 input_report_switch(hp_wmi_input_dev, SW_DOCK,
498 hp_wmi_dock_state());
499 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
500 hp_wmi_tablet_state());
501 input_sync(hp_wmi_input_dev);
751ae808 502 break;
1bbdfd59
TR
503 case HPWMI_PARK_HDD:
504 break;
505 case HPWMI_SMART_ADAPTER:
506 break;
751ae808 507 case HPWMI_BEZEL_BUTTON:
6d96e00c 508 ret = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
a8ec105c 509 &key_code,
c3021ea1 510 sizeof(key_code),
6d96e00c
TR
511 sizeof(key_code));
512 if (ret)
513 break;
4d291ed7
DT
514
515 if (!sparse_keymap_report_event(hp_wmi_input_dev,
516 key_code, 1, true))
b5a4223c 517 pr_info("Unknown key code - 0x%x\n", key_code);
751ae808
TR
518 break;
519 case HPWMI_WIRELESS:
c0b9c649
AH
520 if (rfkill2_count) {
521 hp_wmi_rfkill2_refresh();
522 break;
523 }
524
e5fbba85
AJ
525 if (wifi_rfkill)
526 rfkill_set_states(wifi_rfkill,
527 hp_wmi_get_sw_state(HPWMI_WIFI),
528 hp_wmi_get_hw_state(HPWMI_WIFI));
529 if (bluetooth_rfkill)
530 rfkill_set_states(bluetooth_rfkill,
531 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
532 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
533 if (wwan_rfkill)
534 rfkill_set_states(wwan_rfkill,
535 hp_wmi_get_sw_state(HPWMI_WWAN),
536 hp_wmi_get_hw_state(HPWMI_WWAN));
4fca7ce3
TV
537 if (gps_rfkill)
538 rfkill_set_states(gps_rfkill,
539 hp_wmi_get_sw_state(HPWMI_GPS),
540 hp_wmi_get_hw_state(HPWMI_GPS));
751ae808 541 break;
1bbdfd59 542 case HPWMI_CPU_BATTERY_THROTTLE:
b5a4223c 543 pr_info("Unimplemented CPU throttle because of 3 Cell battery event detected\n");
1bbdfd59
TR
544 break;
545 case HPWMI_LOCK_SWITCH:
546 break;
d9e290a0
AH
547 case HPWMI_LID_SWITCH:
548 break;
549 case HPWMI_SCREEN_ROTATION:
550 break;
551 case HPWMI_COOLSENSE_SYSTEM_MOBILE:
552 break;
553 case HPWMI_COOLSENSE_SYSTEM_HOT:
554 break;
555 case HPWMI_PROXIMITY_SENSOR:
556 break;
557 case HPWMI_BACKLIT_KB_BRIGHTNESS:
558 break;
559 case HPWMI_PEAKSHIFT_PERIOD:
560 break;
561 case HPWMI_BATTERY_CHARGE_PERIOD:
562 break;
751ae808 563 default:
b5a4223c 564 pr_info("Unknown event_id - %d - 0x%x\n", event_id, event_data);
751ae808
TR
565 break;
566 }
62ec30d4
MG
567}
568
569static int __init hp_wmi_input_setup(void)
570{
4d291ed7 571 acpi_status status;
62ec30d4
MG
572 int err;
573
574 hp_wmi_input_dev = input_allocate_device();
bc28596a
AL
575 if (!hp_wmi_input_dev)
576 return -ENOMEM;
62ec30d4
MG
577
578 hp_wmi_input_dev->name = "HP WMI hotkeys";
579 hp_wmi_input_dev->phys = "wmi/input0";
580 hp_wmi_input_dev->id.bustype = BUS_HOST;
62ec30d4 581
4d291ed7
DT
582 __set_bit(EV_SW, hp_wmi_input_dev->evbit);
583 __set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
584 __set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
585
586 err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
587 if (err)
588 goto err_free_dev;
871043bc
MG
589
590 /* Set initial hardware state */
591 input_report_switch(hp_wmi_input_dev, SW_DOCK, hp_wmi_dock_state());
592 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
593 hp_wmi_tablet_state());
594 input_sync(hp_wmi_input_dev);
595
4d291ed7
DT
596 status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
597 if (ACPI_FAILURE(status)) {
598 err = -EIO;
599 goto err_free_keymap;
62ec30d4
MG
600 }
601
4d291ed7
DT
602 err = input_register_device(hp_wmi_input_dev);
603 if (err)
604 goto err_uninstall_notifier;
605
62ec30d4 606 return 0;
4d291ed7
DT
607
608 err_uninstall_notifier:
609 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
610 err_free_keymap:
611 sparse_keymap_free(hp_wmi_input_dev);
612 err_free_dev:
613 input_free_device(hp_wmi_input_dev);
614 return err;
615}
616
617static void hp_wmi_input_destroy(void)
618{
619 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
620 sparse_keymap_free(hp_wmi_input_dev);
621 input_unregister_device(hp_wmi_input_dev);
62ec30d4
MG
622}
623
624static void cleanup_sysfs(struct platform_device *device)
625{
626 device_remove_file(&device->dev, &dev_attr_display);
627 device_remove_file(&device->dev, &dev_attr_hddtemp);
628 device_remove_file(&device->dev, &dev_attr_als);
629 device_remove_file(&device->dev, &dev_attr_dock);
871043bc 630 device_remove_file(&device->dev, &dev_attr_tablet);
62ec30d4
MG
631}
632
b859f159 633static int hp_wmi_rfkill_setup(struct platform_device *device)
62ec30d4
MG
634{
635 int err;
a8ec105c 636 int wireless = 0;
6d96e00c 637
a8ec105c 638 err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, &wireless,
c3021ea1 639 sizeof(wireless), sizeof(wireless));
6d96e00c
TR
640 if (err)
641 return err;
62ec30d4 642
21fc94d7
AH
643 err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, &wireless,
644 sizeof(wireless), 0);
645 if (err)
646 return err;
647
3f6e2f13 648 if (wireless & 0x1) {
19d337df
JB
649 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
650 RFKILL_TYPE_WLAN,
651 &hp_wmi_rfkill_ops,
e5fbba85 652 (void *) HPWMI_WIFI);
dd258c00
DC
653 if (!wifi_rfkill)
654 return -ENOMEM;
e5fbba85
AJ
655 rfkill_init_sw_state(wifi_rfkill,
656 hp_wmi_get_sw_state(HPWMI_WIFI));
657 rfkill_set_hw_state(wifi_rfkill,
658 hp_wmi_get_hw_state(HPWMI_WIFI));
fe8e4e03
LF
659 err = rfkill_register(wifi_rfkill);
660 if (err)
19d337df 661 goto register_wifi_error;
3f6e2f13
MG
662 }
663
664 if (wireless & 0x2) {
19d337df
JB
665 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
666 RFKILL_TYPE_BLUETOOTH,
667 &hp_wmi_rfkill_ops,
e5fbba85 668 (void *) HPWMI_BLUETOOTH);
dd258c00
DC
669 if (!bluetooth_rfkill) {
670 err = -ENOMEM;
671 goto register_wifi_error;
672 }
e5fbba85
AJ
673 rfkill_init_sw_state(bluetooth_rfkill,
674 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
675 rfkill_set_hw_state(bluetooth_rfkill,
676 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
fe8e4e03 677 err = rfkill_register(bluetooth_rfkill);
6989d565 678 if (err)
fe8e4e03 679 goto register_bluetooth_error;
3f6e2f13
MG
680 }
681
682 if (wireless & 0x4) {
19d337df
JB
683 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
684 RFKILL_TYPE_WWAN,
685 &hp_wmi_rfkill_ops,
e5fbba85 686 (void *) HPWMI_WWAN);
dd258c00
DC
687 if (!wwan_rfkill) {
688 err = -ENOMEM;
4fca7ce3 689 goto register_gps_error;
dd258c00 690 }
e5fbba85
AJ
691 rfkill_init_sw_state(wwan_rfkill,
692 hp_wmi_get_sw_state(HPWMI_WWAN));
693 rfkill_set_hw_state(wwan_rfkill,
694 hp_wmi_get_hw_state(HPWMI_WWAN));
fe8e4e03
LF
695 err = rfkill_register(wwan_rfkill);
696 if (err)
697 goto register_wwan_err;
3f6e2f13 698 }
62ec30d4 699
4fca7ce3
TV
700 if (wireless & 0x8) {
701 gps_rfkill = rfkill_alloc("hp-gps", &device->dev,
702 RFKILL_TYPE_GPS,
703 &hp_wmi_rfkill_ops,
704 (void *) HPWMI_GPS);
705 if (!gps_rfkill) {
706 err = -ENOMEM;
707 goto register_bluetooth_error;
708 }
709 rfkill_init_sw_state(gps_rfkill,
710 hp_wmi_get_sw_state(HPWMI_GPS));
af1d486c 711 rfkill_set_hw_state(gps_rfkill,
4fca7ce3
TV
712 hp_wmi_get_hw_state(HPWMI_GPS));
713 err = rfkill_register(gps_rfkill);
714 if (err)
715 goto register_gps_error;
716 }
717
62ec30d4 718 return 0;
fe8e4e03 719register_wwan_err:
19d337df 720 rfkill_destroy(wwan_rfkill);
6d97db59 721 wwan_rfkill = NULL;
4fca7ce3
TV
722 if (gps_rfkill)
723 rfkill_unregister(gps_rfkill);
724register_gps_error:
725 rfkill_destroy(gps_rfkill);
726 gps_rfkill = NULL;
44f0606d
AM
727 if (bluetooth_rfkill)
728 rfkill_unregister(bluetooth_rfkill);
fe8e4e03 729register_bluetooth_error:
19d337df 730 rfkill_destroy(bluetooth_rfkill);
6d97db59 731 bluetooth_rfkill = NULL;
44f0606d
AM
732 if (wifi_rfkill)
733 rfkill_unregister(wifi_rfkill);
19d337df
JB
734register_wifi_error:
735 rfkill_destroy(wifi_rfkill);
6d97db59 736 wifi_rfkill = NULL;
eceb7bdf
AH
737 return err;
738}
739
b859f159 740static int hp_wmi_rfkill2_setup(struct platform_device *device)
c0b9c649
AH
741{
742 int err, i;
743 struct bios_rfkill2_state state;
744 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, 0, &state,
745 0, sizeof(state));
746 if (err)
747 return err;
748
749 if (state.count > HPWMI_MAX_RFKILL2_DEVICES) {
b5a4223c 750 pr_warn("unable to parse 0x1b query output\n");
c0b9c649
AH
751 return -EINVAL;
752 }
753
754 for (i = 0; i < state.count; i++) {
755 struct rfkill *rfkill;
756 enum rfkill_type type;
757 char *name;
758 switch (state.device[i].radio_type) {
759 case HPWMI_WIFI:
760 type = RFKILL_TYPE_WLAN;
761 name = "hp-wifi";
762 break;
763 case HPWMI_BLUETOOTH:
764 type = RFKILL_TYPE_BLUETOOTH;
765 name = "hp-bluetooth";
766 break;
767 case HPWMI_WWAN:
768 type = RFKILL_TYPE_WWAN;
769 name = "hp-wwan";
770 break;
4fca7ce3
TV
771 case HPWMI_GPS:
772 type = RFKILL_TYPE_GPS;
773 name = "hp-gps";
774 break;
c0b9c649 775 default:
b5a4223c
JP
776 pr_warn("unknown device type 0x%x\n",
777 state.device[i].radio_type);
c0b9c649
AH
778 continue;
779 }
780
781 if (!state.device[i].vendor_id) {
b5a4223c
JP
782 pr_warn("zero device %d while %d reported\n",
783 i, state.count);
c0b9c649
AH
784 continue;
785 }
786
787 rfkill = rfkill_alloc(name, &device->dev, type,
788 &hp_wmi_rfkill2_ops, (void *)(long)i);
789 if (!rfkill) {
790 err = -ENOMEM;
791 goto fail;
792 }
793
794 rfkill2[rfkill2_count].id = state.device[i].rfkill_id;
795 rfkill2[rfkill2_count].num = i;
796 rfkill2[rfkill2_count].rfkill = rfkill;
797
798 rfkill_init_sw_state(rfkill,
799 IS_SWBLOCKED(state.device[i].power));
800 rfkill_set_hw_state(rfkill,
801 IS_HWBLOCKED(state.device[i].power));
802
803 if (!(state.device[i].power & HPWMI_POWER_BIOS))
b5a4223c 804 pr_info("device %s blocked by BIOS\n", name);
c0b9c649
AH
805
806 err = rfkill_register(rfkill);
807 if (err) {
808 rfkill_destroy(rfkill);
809 goto fail;
810 }
811
812 rfkill2_count++;
813 }
814
815 return 0;
816fail:
817 for (; rfkill2_count > 0; rfkill2_count--) {
818 rfkill_unregister(rfkill2[rfkill2_count - 1].rfkill);
819 rfkill_destroy(rfkill2[rfkill2_count - 1].rfkill);
820 }
821 return err;
822}
823
c165b80c 824static int __init hp_wmi_bios_setup(struct platform_device *device)
eceb7bdf
AH
825{
826 int err;
827
6d97db59
AH
828 /* clear detected rfkill devices */
829 wifi_rfkill = NULL;
830 bluetooth_rfkill = NULL;
831 wwan_rfkill = NULL;
4fca7ce3 832 gps_rfkill = NULL;
c0b9c649 833 rfkill2_count = 0;
6d97db59 834
c0b9c649
AH
835 if (hp_wmi_rfkill_setup(device))
836 hp_wmi_rfkill2_setup(device);
eceb7bdf
AH
837
838 err = device_create_file(&device->dev, &dev_attr_display);
839 if (err)
840 goto add_sysfs_error;
841 err = device_create_file(&device->dev, &dev_attr_hddtemp);
842 if (err)
843 goto add_sysfs_error;
844 err = device_create_file(&device->dev, &dev_attr_als);
845 if (err)
846 goto add_sysfs_error;
847 err = device_create_file(&device->dev, &dev_attr_dock);
848 if (err)
849 goto add_sysfs_error;
850 err = device_create_file(&device->dev, &dev_attr_tablet);
851 if (err)
852 goto add_sysfs_error;
853 return 0;
854
62ec30d4
MG
855add_sysfs_error:
856 cleanup_sysfs(device);
857 return err;
858}
859
860static int __exit hp_wmi_bios_remove(struct platform_device *device)
861{
c0b9c649 862 int i;
62ec30d4
MG
863 cleanup_sysfs(device);
864
c0b9c649
AH
865 for (i = 0; i < rfkill2_count; i++) {
866 rfkill_unregister(rfkill2[i].rfkill);
867 rfkill_destroy(rfkill2[i].rfkill);
868 }
869
19d337df 870 if (wifi_rfkill) {
3f6e2f13 871 rfkill_unregister(wifi_rfkill);
19d337df
JB
872 rfkill_destroy(wifi_rfkill);
873 }
874 if (bluetooth_rfkill) {
3f6e2f13 875 rfkill_unregister(bluetooth_rfkill);
09729f0b 876 rfkill_destroy(bluetooth_rfkill);
19d337df
JB
877 }
878 if (wwan_rfkill) {
3f6e2f13 879 rfkill_unregister(wwan_rfkill);
19d337df
JB
880 rfkill_destroy(wwan_rfkill);
881 }
4fca7ce3
TV
882 if (gps_rfkill) {
883 rfkill_unregister(gps_rfkill);
884 rfkill_destroy(gps_rfkill);
885 }
62ec30d4
MG
886
887 return 0;
888}
889
8dd2b426 890static int hp_wmi_resume_handler(struct device *device)
4c395bdd 891{
4c395bdd 892 /*
871043bc
MG
893 * Hardware state may have changed while suspended, so trigger
894 * input events for the current state. As this is a switch,
4c395bdd
FP
895 * the input layer will only actually pass it on if the state
896 * changed.
897 */
daed9537
FP
898 if (hp_wmi_input_dev) {
899 input_report_switch(hp_wmi_input_dev, SW_DOCK,
900 hp_wmi_dock_state());
901 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
902 hp_wmi_tablet_state());
903 input_sync(hp_wmi_input_dev);
904 }
4c395bdd 905
c0b9c649
AH
906 if (rfkill2_count)
907 hp_wmi_rfkill2_refresh();
908
e5fbba85
AJ
909 if (wifi_rfkill)
910 rfkill_set_states(wifi_rfkill,
911 hp_wmi_get_sw_state(HPWMI_WIFI),
912 hp_wmi_get_hw_state(HPWMI_WIFI));
913 if (bluetooth_rfkill)
914 rfkill_set_states(bluetooth_rfkill,
915 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
916 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
917 if (wwan_rfkill)
918 rfkill_set_states(wwan_rfkill,
919 hp_wmi_get_sw_state(HPWMI_WWAN),
920 hp_wmi_get_hw_state(HPWMI_WWAN));
4fca7ce3
TV
921 if (gps_rfkill)
922 rfkill_set_states(gps_rfkill,
923 hp_wmi_get_sw_state(HPWMI_GPS),
924 hp_wmi_get_hw_state(HPWMI_GPS));
e5fbba85 925
4c395bdd
FP
926 return 0;
927}
928
c165b80c
DT
929static const struct dev_pm_ops hp_wmi_pm_ops = {
930 .resume = hp_wmi_resume_handler,
931 .restore = hp_wmi_resume_handler,
932};
933
934static struct platform_driver hp_wmi_driver = {
935 .driver = {
936 .name = "hp-wmi",
937 .owner = THIS_MODULE,
938 .pm = &hp_wmi_pm_ops,
939 },
940 .remove = __exit_p(hp_wmi_bios_remove),
941};
942
62ec30d4
MG
943static int __init hp_wmi_init(void)
944{
945 int err;
b096667b
TR
946 int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
947 int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
62ec30d4 948
c165b80c
DT
949 if (!bios_capable && !event_capable)
950 return -ENODEV;
951
b096667b 952 if (event_capable) {
dfec5c48 953 err = hp_wmi_input_setup();
4d291ed7 954 if (err)
dfec5c48 955 return err;
62ec30d4
MG
956 }
957
b096667b 958 if (bios_capable) {
c165b80c
DT
959 hp_wmi_platform_dev =
960 platform_device_register_simple("hp-wmi", -1, NULL, 0);
961 if (IS_ERR(hp_wmi_platform_dev)) {
962 err = PTR_ERR(hp_wmi_platform_dev);
963 goto err_destroy_input;
62ec30d4 964 }
c165b80c
DT
965
966 err = platform_driver_probe(&hp_wmi_driver, hp_wmi_bios_setup);
dfec5c48 967 if (err)
c165b80c 968 goto err_unregister_device;
62ec30d4
MG
969 }
970
971 return 0;
dfec5c48 972
c165b80c
DT
973err_unregister_device:
974 platform_device_unregister(hp_wmi_platform_dev);
975err_destroy_input:
4d291ed7
DT
976 if (event_capable)
977 hp_wmi_input_destroy();
dfec5c48
AL
978
979 return err;
62ec30d4 980}
c165b80c 981module_init(hp_wmi_init);
62ec30d4
MG
982
983static void __exit hp_wmi_exit(void)
984{
4d291ed7
DT
985 if (wmi_has_guid(HPWMI_EVENT_GUID))
986 hp_wmi_input_destroy();
987
62ec30d4 988 if (hp_wmi_platform_dev) {
97ba0af0 989 platform_device_unregister(hp_wmi_platform_dev);
62ec30d4
MG
990 platform_driver_unregister(&hp_wmi_driver);
991 }
992}
62ec30d4 993module_exit(hp_wmi_exit);