x86 / platform / hp_wmi: Fix bluetooth_rfkill misuse in hp_wmi_rfkill_setup()
[deliverable/linux.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
3f6e2f13 643 if (wireless & 0x1) {
19d337df
JB
644 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
645 RFKILL_TYPE_WLAN,
646 &hp_wmi_rfkill_ops,
e5fbba85 647 (void *) HPWMI_WIFI);
dd258c00
DC
648 if (!wifi_rfkill)
649 return -ENOMEM;
e5fbba85
AJ
650 rfkill_init_sw_state(wifi_rfkill,
651 hp_wmi_get_sw_state(HPWMI_WIFI));
652 rfkill_set_hw_state(wifi_rfkill,
653 hp_wmi_get_hw_state(HPWMI_WIFI));
fe8e4e03
LF
654 err = rfkill_register(wifi_rfkill);
655 if (err)
19d337df 656 goto register_wifi_error;
3f6e2f13
MG
657 }
658
659 if (wireless & 0x2) {
19d337df
JB
660 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
661 RFKILL_TYPE_BLUETOOTH,
662 &hp_wmi_rfkill_ops,
e5fbba85 663 (void *) HPWMI_BLUETOOTH);
dd258c00
DC
664 if (!bluetooth_rfkill) {
665 err = -ENOMEM;
666 goto register_wifi_error;
667 }
e5fbba85
AJ
668 rfkill_init_sw_state(bluetooth_rfkill,
669 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
670 rfkill_set_hw_state(bluetooth_rfkill,
671 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
fe8e4e03 672 err = rfkill_register(bluetooth_rfkill);
6989d565 673 if (err)
fe8e4e03 674 goto register_bluetooth_error;
3f6e2f13
MG
675 }
676
677 if (wireless & 0x4) {
19d337df
JB
678 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
679 RFKILL_TYPE_WWAN,
680 &hp_wmi_rfkill_ops,
e5fbba85 681 (void *) HPWMI_WWAN);
dd258c00
DC
682 if (!wwan_rfkill) {
683 err = -ENOMEM;
4fca7ce3 684 goto register_gps_error;
dd258c00 685 }
e5fbba85
AJ
686 rfkill_init_sw_state(wwan_rfkill,
687 hp_wmi_get_sw_state(HPWMI_WWAN));
688 rfkill_set_hw_state(wwan_rfkill,
689 hp_wmi_get_hw_state(HPWMI_WWAN));
fe8e4e03
LF
690 err = rfkill_register(wwan_rfkill);
691 if (err)
692 goto register_wwan_err;
3f6e2f13 693 }
62ec30d4 694
4fca7ce3
TV
695 if (wireless & 0x8) {
696 gps_rfkill = rfkill_alloc("hp-gps", &device->dev,
697 RFKILL_TYPE_GPS,
698 &hp_wmi_rfkill_ops,
699 (void *) HPWMI_GPS);
700 if (!gps_rfkill) {
701 err = -ENOMEM;
702 goto register_bluetooth_error;
703 }
704 rfkill_init_sw_state(gps_rfkill,
705 hp_wmi_get_sw_state(HPWMI_GPS));
af1d486c 706 rfkill_set_hw_state(gps_rfkill,
4fca7ce3
TV
707 hp_wmi_get_hw_state(HPWMI_GPS));
708 err = rfkill_register(gps_rfkill);
709 if (err)
710 goto register_gps_error;
711 }
712
62ec30d4 713 return 0;
fe8e4e03 714register_wwan_err:
19d337df 715 rfkill_destroy(wwan_rfkill);
6d97db59 716 wwan_rfkill = NULL;
4fca7ce3
TV
717 if (gps_rfkill)
718 rfkill_unregister(gps_rfkill);
719register_gps_error:
720 rfkill_destroy(gps_rfkill);
721 gps_rfkill = NULL;
44f0606d
AM
722 if (bluetooth_rfkill)
723 rfkill_unregister(bluetooth_rfkill);
fe8e4e03 724register_bluetooth_error:
19d337df 725 rfkill_destroy(bluetooth_rfkill);
6d97db59 726 bluetooth_rfkill = NULL;
44f0606d
AM
727 if (wifi_rfkill)
728 rfkill_unregister(wifi_rfkill);
19d337df
JB
729register_wifi_error:
730 rfkill_destroy(wifi_rfkill);
6d97db59 731 wifi_rfkill = NULL;
eceb7bdf
AH
732 return err;
733}
734
b859f159 735static int hp_wmi_rfkill2_setup(struct platform_device *device)
c0b9c649
AH
736{
737 int err, i;
738 struct bios_rfkill2_state state;
739 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, 0, &state,
740 0, sizeof(state));
741 if (err)
742 return err;
743
744 if (state.count > HPWMI_MAX_RFKILL2_DEVICES) {
b5a4223c 745 pr_warn("unable to parse 0x1b query output\n");
c0b9c649
AH
746 return -EINVAL;
747 }
748
749 for (i = 0; i < state.count; i++) {
750 struct rfkill *rfkill;
751 enum rfkill_type type;
752 char *name;
753 switch (state.device[i].radio_type) {
754 case HPWMI_WIFI:
755 type = RFKILL_TYPE_WLAN;
756 name = "hp-wifi";
757 break;
758 case HPWMI_BLUETOOTH:
759 type = RFKILL_TYPE_BLUETOOTH;
760 name = "hp-bluetooth";
761 break;
762 case HPWMI_WWAN:
763 type = RFKILL_TYPE_WWAN;
764 name = "hp-wwan";
765 break;
4fca7ce3
TV
766 case HPWMI_GPS:
767 type = RFKILL_TYPE_GPS;
768 name = "hp-gps";
769 break;
c0b9c649 770 default:
b5a4223c
JP
771 pr_warn("unknown device type 0x%x\n",
772 state.device[i].radio_type);
c0b9c649
AH
773 continue;
774 }
775
776 if (!state.device[i].vendor_id) {
b5a4223c
JP
777 pr_warn("zero device %d while %d reported\n",
778 i, state.count);
c0b9c649
AH
779 continue;
780 }
781
782 rfkill = rfkill_alloc(name, &device->dev, type,
783 &hp_wmi_rfkill2_ops, (void *)(long)i);
784 if (!rfkill) {
785 err = -ENOMEM;
786 goto fail;
787 }
788
789 rfkill2[rfkill2_count].id = state.device[i].rfkill_id;
790 rfkill2[rfkill2_count].num = i;
791 rfkill2[rfkill2_count].rfkill = rfkill;
792
793 rfkill_init_sw_state(rfkill,
794 IS_SWBLOCKED(state.device[i].power));
795 rfkill_set_hw_state(rfkill,
796 IS_HWBLOCKED(state.device[i].power));
797
798 if (!(state.device[i].power & HPWMI_POWER_BIOS))
b5a4223c 799 pr_info("device %s blocked by BIOS\n", name);
c0b9c649
AH
800
801 err = rfkill_register(rfkill);
802 if (err) {
803 rfkill_destroy(rfkill);
804 goto fail;
805 }
806
807 rfkill2_count++;
808 }
809
810 return 0;
811fail:
812 for (; rfkill2_count > 0; rfkill2_count--) {
813 rfkill_unregister(rfkill2[rfkill2_count - 1].rfkill);
814 rfkill_destroy(rfkill2[rfkill2_count - 1].rfkill);
815 }
816 return err;
817}
818
c165b80c 819static int __init hp_wmi_bios_setup(struct platform_device *device)
eceb7bdf
AH
820{
821 int err;
822
6d97db59
AH
823 /* clear detected rfkill devices */
824 wifi_rfkill = NULL;
825 bluetooth_rfkill = NULL;
826 wwan_rfkill = NULL;
4fca7ce3 827 gps_rfkill = NULL;
c0b9c649 828 rfkill2_count = 0;
6d97db59 829
c0b9c649
AH
830 if (hp_wmi_rfkill_setup(device))
831 hp_wmi_rfkill2_setup(device);
eceb7bdf
AH
832
833 err = device_create_file(&device->dev, &dev_attr_display);
834 if (err)
835 goto add_sysfs_error;
836 err = device_create_file(&device->dev, &dev_attr_hddtemp);
837 if (err)
838 goto add_sysfs_error;
839 err = device_create_file(&device->dev, &dev_attr_als);
840 if (err)
841 goto add_sysfs_error;
842 err = device_create_file(&device->dev, &dev_attr_dock);
843 if (err)
844 goto add_sysfs_error;
845 err = device_create_file(&device->dev, &dev_attr_tablet);
846 if (err)
847 goto add_sysfs_error;
848 return 0;
849
62ec30d4
MG
850add_sysfs_error:
851 cleanup_sysfs(device);
852 return err;
853}
854
855static int __exit hp_wmi_bios_remove(struct platform_device *device)
856{
c0b9c649 857 int i;
62ec30d4
MG
858 cleanup_sysfs(device);
859
c0b9c649
AH
860 for (i = 0; i < rfkill2_count; i++) {
861 rfkill_unregister(rfkill2[i].rfkill);
862 rfkill_destroy(rfkill2[i].rfkill);
863 }
864
19d337df 865 if (wifi_rfkill) {
3f6e2f13 866 rfkill_unregister(wifi_rfkill);
19d337df
JB
867 rfkill_destroy(wifi_rfkill);
868 }
869 if (bluetooth_rfkill) {
3f6e2f13 870 rfkill_unregister(bluetooth_rfkill);
09729f0b 871 rfkill_destroy(bluetooth_rfkill);
19d337df
JB
872 }
873 if (wwan_rfkill) {
3f6e2f13 874 rfkill_unregister(wwan_rfkill);
19d337df
JB
875 rfkill_destroy(wwan_rfkill);
876 }
4fca7ce3
TV
877 if (gps_rfkill) {
878 rfkill_unregister(gps_rfkill);
879 rfkill_destroy(gps_rfkill);
880 }
62ec30d4
MG
881
882 return 0;
883}
884
8dd2b426 885static int hp_wmi_resume_handler(struct device *device)
4c395bdd 886{
4c395bdd 887 /*
871043bc
MG
888 * Hardware state may have changed while suspended, so trigger
889 * input events for the current state. As this is a switch,
4c395bdd
FP
890 * the input layer will only actually pass it on if the state
891 * changed.
892 */
daed9537
FP
893 if (hp_wmi_input_dev) {
894 input_report_switch(hp_wmi_input_dev, SW_DOCK,
895 hp_wmi_dock_state());
896 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
897 hp_wmi_tablet_state());
898 input_sync(hp_wmi_input_dev);
899 }
4c395bdd 900
c0b9c649
AH
901 if (rfkill2_count)
902 hp_wmi_rfkill2_refresh();
903
e5fbba85
AJ
904 if (wifi_rfkill)
905 rfkill_set_states(wifi_rfkill,
906 hp_wmi_get_sw_state(HPWMI_WIFI),
907 hp_wmi_get_hw_state(HPWMI_WIFI));
908 if (bluetooth_rfkill)
909 rfkill_set_states(bluetooth_rfkill,
910 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
911 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
912 if (wwan_rfkill)
913 rfkill_set_states(wwan_rfkill,
914 hp_wmi_get_sw_state(HPWMI_WWAN),
915 hp_wmi_get_hw_state(HPWMI_WWAN));
4fca7ce3
TV
916 if (gps_rfkill)
917 rfkill_set_states(gps_rfkill,
918 hp_wmi_get_sw_state(HPWMI_GPS),
919 hp_wmi_get_hw_state(HPWMI_GPS));
e5fbba85 920
4c395bdd
FP
921 return 0;
922}
923
c165b80c
DT
924static const struct dev_pm_ops hp_wmi_pm_ops = {
925 .resume = hp_wmi_resume_handler,
926 .restore = hp_wmi_resume_handler,
927};
928
929static struct platform_driver hp_wmi_driver = {
930 .driver = {
931 .name = "hp-wmi",
932 .owner = THIS_MODULE,
933 .pm = &hp_wmi_pm_ops,
934 },
935 .remove = __exit_p(hp_wmi_bios_remove),
936};
937
62ec30d4
MG
938static int __init hp_wmi_init(void)
939{
940 int err;
b096667b
TR
941 int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
942 int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
62ec30d4 943
c165b80c
DT
944 if (!bios_capable && !event_capable)
945 return -ENODEV;
946
b096667b 947 if (event_capable) {
dfec5c48 948 err = hp_wmi_input_setup();
4d291ed7 949 if (err)
dfec5c48 950 return err;
62ec30d4
MG
951 }
952
b096667b 953 if (bios_capable) {
c165b80c
DT
954 hp_wmi_platform_dev =
955 platform_device_register_simple("hp-wmi", -1, NULL, 0);
956 if (IS_ERR(hp_wmi_platform_dev)) {
957 err = PTR_ERR(hp_wmi_platform_dev);
958 goto err_destroy_input;
62ec30d4 959 }
c165b80c
DT
960
961 err = platform_driver_probe(&hp_wmi_driver, hp_wmi_bios_setup);
dfec5c48 962 if (err)
c165b80c 963 goto err_unregister_device;
62ec30d4
MG
964 }
965
966 return 0;
dfec5c48 967
c165b80c
DT
968err_unregister_device:
969 platform_device_unregister(hp_wmi_platform_dev);
970err_destroy_input:
4d291ed7
DT
971 if (event_capable)
972 hp_wmi_input_destroy();
dfec5c48
AL
973
974 return err;
62ec30d4 975}
c165b80c 976module_init(hp_wmi_init);
62ec30d4
MG
977
978static void __exit hp_wmi_exit(void)
979{
4d291ed7
DT
980 if (wmi_has_guid(HPWMI_EVENT_GUID))
981 hp_wmi_input_destroy();
982
62ec30d4 983 if (hp_wmi_platform_dev) {
97ba0af0 984 platform_device_unregister(hp_wmi_platform_dev);
62ec30d4
MG
985 platform_driver_unregister(&hp_wmi_driver);
986 }
987}
62ec30d4 988module_exit(hp_wmi_exit);
This page took 1.075649 seconds and 5 git commands to generate.