GNU Linux-libre 4.14.324-gnu1
[releases.git] / drivers / platform / x86 / hp-wmi.c
1 /*
2  * HP WMI hotkeys
3  *
4  * Copyright (C) 2008 Red Hat <mjg@redhat.com>
5  * Copyright (C) 2010, 2011 Anssi Hannula <anssi.hannula@iki.fi>
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
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/slab.h>
33 #include <linux/types.h>
34 #include <linux/input.h>
35 #include <linux/input/sparse-keymap.h>
36 #include <linux/platform_device.h>
37 #include <linux/acpi.h>
38 #include <linux/rfkill.h>
39 #include <linux/string.h>
40
41 MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>");
42 MODULE_DESCRIPTION("HP laptop WMI hotkeys driver");
43 MODULE_LICENSE("GPL");
44
45 MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
46 MODULE_ALIAS("wmi:5FB7F034-2C63-45e9-BE91-3D44E2C707E4");
47
48 static int enable_tablet_mode_sw = -1;
49 module_param(enable_tablet_mode_sw, int, 0444);
50 MODULE_PARM_DESC(enable_tablet_mode_sw, "Enable SW_TABLET_MODE reporting (-1=auto, 0=no, 1=yes)");
51
52 #define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
53 #define HPWMI_BIOS_GUID "5FB7F034-2C63-45e9-BE91-3D44E2C707E4"
54
55 enum hp_wmi_radio {
56         HPWMI_WIFI      = 0x0,
57         HPWMI_BLUETOOTH = 0x1,
58         HPWMI_WWAN      = 0x2,
59         HPWMI_GPS       = 0x3,
60 };
61
62 enum hp_wmi_event_ids {
63         HPWMI_DOCK_EVENT                = 0x01,
64         HPWMI_PARK_HDD                  = 0x02,
65         HPWMI_SMART_ADAPTER             = 0x03,
66         HPWMI_BEZEL_BUTTON              = 0x04,
67         HPWMI_WIRELESS                  = 0x05,
68         HPWMI_CPU_BATTERY_THROTTLE      = 0x06,
69         HPWMI_LOCK_SWITCH               = 0x07,
70         HPWMI_LID_SWITCH                = 0x08,
71         HPWMI_SCREEN_ROTATION           = 0x09,
72         HPWMI_COOLSENSE_SYSTEM_MOBILE   = 0x0A,
73         HPWMI_COOLSENSE_SYSTEM_HOT      = 0x0B,
74         HPWMI_PROXIMITY_SENSOR          = 0x0C,
75         HPWMI_BACKLIT_KB_BRIGHTNESS     = 0x0D,
76         HPWMI_PEAKSHIFT_PERIOD          = 0x0F,
77         HPWMI_BATTERY_CHARGE_PERIOD     = 0x10,
78         HPWMI_SANITIZATION_MODE         = 0x17,
79         HPWMI_SMART_EXPERIENCE_APP      = 0x21,
80 };
81
82 struct bios_args {
83         u32 signature;
84         u32 command;
85         u32 commandtype;
86         u32 datasize;
87         u8 data[128];
88 };
89
90 enum hp_wmi_commandtype {
91         HPWMI_DISPLAY_QUERY             = 0x01,
92         HPWMI_HDDTEMP_QUERY             = 0x02,
93         HPWMI_ALS_QUERY                 = 0x03,
94         HPWMI_HARDWARE_QUERY            = 0x04,
95         HPWMI_WIRELESS_QUERY            = 0x05,
96         HPWMI_BATTERY_QUERY             = 0x07,
97         HPWMI_BIOS_QUERY                = 0x09,
98         HPWMI_FEATURE_QUERY             = 0x0b,
99         HPWMI_HOTKEY_QUERY              = 0x0c,
100         HPWMI_FEATURE2_QUERY            = 0x0d,
101         HPWMI_WIRELESS2_QUERY           = 0x1b,
102         HPWMI_POSTCODEERROR_QUERY       = 0x2a,
103 };
104
105 enum hp_wmi_command {
106         HPWMI_READ      = 0x01,
107         HPWMI_WRITE     = 0x02,
108         HPWMI_ODM       = 0x03,
109 };
110
111 enum hp_wmi_hardware_mask {
112         HPWMI_DOCK_MASK         = 0x01,
113         HPWMI_TABLET_MASK       = 0x04,
114 };
115
116 struct bios_return {
117         u32 sigpass;
118         u32 return_code;
119 };
120
121 enum hp_return_value {
122         HPWMI_RET_WRONG_SIGNATURE       = 0x02,
123         HPWMI_RET_UNKNOWN_COMMAND       = 0x03,
124         HPWMI_RET_UNKNOWN_CMDTYPE       = 0x04,
125         HPWMI_RET_INVALID_PARAMETERS    = 0x05,
126 };
127
128 enum hp_wireless2_bits {
129         HPWMI_POWER_STATE       = 0x01,
130         HPWMI_POWER_SOFT        = 0x02,
131         HPWMI_POWER_BIOS        = 0x04,
132         HPWMI_POWER_HARD        = 0x08,
133 };
134
135 #define IS_HWBLOCKED(x) ((x & (HPWMI_POWER_BIOS | HPWMI_POWER_HARD)) \
136                          != (HPWMI_POWER_BIOS | HPWMI_POWER_HARD))
137 #define IS_SWBLOCKED(x) !(x & HPWMI_POWER_SOFT)
138
139 struct bios_rfkill2_device_state {
140         u8 radio_type;
141         u8 bus_type;
142         u16 vendor_id;
143         u16 product_id;
144         u16 subsys_vendor_id;
145         u16 subsys_product_id;
146         u8 rfkill_id;
147         u8 power;
148         u8 unknown[4];
149 };
150
151 /* 7 devices fit into the 128 byte buffer */
152 #define HPWMI_MAX_RFKILL2_DEVICES       7
153
154 struct bios_rfkill2_state {
155         u8 unknown[7];
156         u8 count;
157         u8 pad[8];
158         struct bios_rfkill2_device_state device[HPWMI_MAX_RFKILL2_DEVICES];
159 };
160
161 static const struct key_entry hp_wmi_keymap[] = {
162         { KE_KEY, 0x02,   { KEY_BRIGHTNESSUP } },
163         { KE_KEY, 0x03,   { KEY_BRIGHTNESSDOWN } },
164         { KE_KEY, 0x20e6, { KEY_PROG1 } },
165         { KE_KEY, 0x20e8, { KEY_MEDIA } },
166         { KE_KEY, 0x2142, { KEY_MEDIA } },
167         { KE_KEY, 0x213b, { KEY_INFO } },
168         { KE_KEY, 0x2169, { KEY_ROTATE_DISPLAY } },
169         { KE_KEY, 0x216a, { KEY_SETUP } },
170         { KE_KEY, 0x231b, { KEY_HELP } },
171         { KE_END, 0 }
172 };
173
174 static struct input_dev *hp_wmi_input_dev;
175 static struct platform_device *hp_wmi_platform_dev;
176
177 static struct rfkill *wifi_rfkill;
178 static struct rfkill *bluetooth_rfkill;
179 static struct rfkill *wwan_rfkill;
180
181 struct rfkill2_device {
182         u8 id;
183         int num;
184         struct rfkill *rfkill;
185 };
186
187 static int rfkill2_count;
188 static struct rfkill2_device rfkill2[HPWMI_MAX_RFKILL2_DEVICES];
189
190 /* map output size to the corresponding WMI method id */
191 static inline int encode_outsize_for_pvsz(int outsize)
192 {
193         if (outsize > 4096)
194                 return -EINVAL;
195         if (outsize > 1024)
196                 return 5;
197         if (outsize > 128)
198                 return 4;
199         if (outsize > 4)
200                 return 3;
201         if (outsize > 0)
202                 return 2;
203         return 1;
204 }
205
206 /*
207  * hp_wmi_perform_query
208  *
209  * query:       The commandtype (enum hp_wmi_commandtype)
210  * write:       The command (enum hp_wmi_command)
211  * buffer:      Buffer used as input and/or output
212  * insize:      Size of input buffer
213  * outsize:     Size of output buffer
214  *
215  * returns zero on success
216  *         an HP WMI query specific error code (which is positive)
217  *         -EINVAL if the query was not successful at all
218  *         -EINVAL if the output buffer size exceeds buffersize
219  *
220  * Note: The buffersize must at least be the maximum of the input and output
221  *       size. E.g. Battery info query is defined to have 1 byte input
222  *       and 128 byte output. The caller would do:
223  *       buffer = kzalloc(128, GFP_KERNEL);
224  *       ret = hp_wmi_perform_query(HPWMI_BATTERY_QUERY, HPWMI_READ, buffer, 1, 128)
225  */
226 static int hp_wmi_perform_query(int query, enum hp_wmi_command command,
227                                 void *buffer, int insize, int outsize)
228 {
229         int mid;
230         struct bios_return *bios_return;
231         int actual_outsize;
232         union acpi_object *obj;
233         struct bios_args args = {
234                 .signature = 0x55434553,
235                 .command = command,
236                 .commandtype = query,
237                 .datasize = insize,
238                 .data = { 0 },
239         };
240         struct acpi_buffer input = { sizeof(struct bios_args), &args };
241         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
242         int ret = 0;
243
244         mid = encode_outsize_for_pvsz(outsize);
245         if (WARN_ON(mid < 0))
246                 return mid;
247
248         if (WARN_ON(insize > sizeof(args.data)))
249                 return -EINVAL;
250         memcpy(&args.data[0], buffer, insize);
251
252         wmi_evaluate_method(HPWMI_BIOS_GUID, 0, mid, &input, &output);
253
254         obj = output.pointer;
255
256         if (!obj)
257                 return -EINVAL;
258
259         if (obj->type != ACPI_TYPE_BUFFER) {
260                 ret = -EINVAL;
261                 goto out_free;
262         }
263
264         bios_return = (struct bios_return *)obj->buffer.pointer;
265         ret = bios_return->return_code;
266
267         if (ret) {
268                 if (ret != HPWMI_RET_UNKNOWN_CMDTYPE)
269                         pr_warn("query 0x%x returned error 0x%x\n", query, ret);
270                 goto out_free;
271         }
272
273         /* Ignore output data of zero size */
274         if (!outsize)
275                 goto out_free;
276
277         actual_outsize = min(outsize, (int)(obj->buffer.length - sizeof(*bios_return)));
278         memcpy(buffer, obj->buffer.pointer + sizeof(*bios_return), actual_outsize);
279         memset(buffer + actual_outsize, 0, outsize - actual_outsize);
280
281 out_free:
282         kfree(obj);
283         return ret;
284 }
285
286 static int hp_wmi_read_int(int query)
287 {
288         int val = 0, ret;
289
290         ret = hp_wmi_perform_query(query, HPWMI_READ, &val,
291                                    sizeof(val), sizeof(val));
292
293         if (ret)
294                 return ret < 0 ? ret : -EINVAL;
295
296         return val;
297 }
298
299 static int hp_wmi_hw_state(int mask)
300 {
301         int state = hp_wmi_read_int(HPWMI_HARDWARE_QUERY);
302
303         if (state < 0)
304                 return state;
305
306         return !!(state & mask);
307 }
308
309 static int __init hp_wmi_bios_2008_later(void)
310 {
311         int state = 0;
312         int ret = hp_wmi_perform_query(HPWMI_FEATURE_QUERY, HPWMI_READ, &state,
313                                        sizeof(state), sizeof(state));
314         if (!ret)
315                 return 1;
316
317         return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
318 }
319
320 static int __init hp_wmi_bios_2009_later(void)
321 {
322         u8 state[128];
323         int ret = hp_wmi_perform_query(HPWMI_FEATURE2_QUERY, HPWMI_READ, &state,
324                                        sizeof(state), sizeof(state));
325         if (!ret)
326                 return 1;
327
328         return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
329 }
330
331 static int __init hp_wmi_enable_hotkeys(void)
332 {
333         int value = 0x6e;
334         int ret = hp_wmi_perform_query(HPWMI_BIOS_QUERY, HPWMI_WRITE, &value,
335                                        sizeof(value), 0);
336
337         return ret <= 0 ? ret : -EINVAL;
338 }
339
340 static int hp_wmi_set_block(void *data, bool blocked)
341 {
342         enum hp_wmi_radio r = (enum hp_wmi_radio) data;
343         int query = BIT(r + 8) | ((!blocked) << r);
344         int ret;
345
346         ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE,
347                                    &query, sizeof(query), 0);
348
349         return ret <= 0 ? ret : -EINVAL;
350 }
351
352 static const struct rfkill_ops hp_wmi_rfkill_ops = {
353         .set_block = hp_wmi_set_block,
354 };
355
356 static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
357 {
358         int mask = 0x200 << (r * 8);
359
360         int wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
361
362         /* TBD: Pass error */
363         WARN_ONCE(wireless < 0, "error executing HPWMI_WIRELESS_QUERY");
364
365         return !(wireless & mask);
366 }
367
368 static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
369 {
370         int mask = 0x800 << (r * 8);
371
372         int wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
373
374         /* TBD: Pass error */
375         WARN_ONCE(wireless < 0, "error executing HPWMI_WIRELESS_QUERY");
376
377         return !(wireless & mask);
378 }
379
380 static int hp_wmi_rfkill2_set_block(void *data, bool blocked)
381 {
382         int rfkill_id = (int)(long)data;
383         char buffer[4] = { 0x01, 0x00, rfkill_id, !blocked };
384         int ret;
385
386         ret = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_WRITE,
387                                    buffer, sizeof(buffer), 0);
388
389         return ret <= 0 ? ret : -EINVAL;
390 }
391
392 static const struct rfkill_ops hp_wmi_rfkill2_ops = {
393         .set_block = hp_wmi_rfkill2_set_block,
394 };
395
396 static int hp_wmi_rfkill2_refresh(void)
397 {
398         struct bios_rfkill2_state state;
399         int err, i;
400
401         err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
402                                    sizeof(state), sizeof(state));
403         if (err)
404                 return err;
405
406         for (i = 0; i < rfkill2_count; i++) {
407                 int num = rfkill2[i].num;
408                 struct bios_rfkill2_device_state *devstate;
409                 devstate = &state.device[num];
410
411                 if (num >= state.count ||
412                     devstate->rfkill_id != rfkill2[i].id) {
413                         pr_warn("power configuration of the wireless devices unexpectedly changed\n");
414                         continue;
415                 }
416
417                 rfkill_set_states(rfkill2[i].rfkill,
418                                   IS_SWBLOCKED(devstate->power),
419                                   IS_HWBLOCKED(devstate->power));
420         }
421
422         return 0;
423 }
424
425 static ssize_t display_show(struct device *dev, struct device_attribute *attr,
426                             char *buf)
427 {
428         int value = hp_wmi_read_int(HPWMI_DISPLAY_QUERY);
429         if (value < 0)
430                 return value;
431         return sprintf(buf, "%d\n", value);
432 }
433
434 static ssize_t hddtemp_show(struct device *dev, struct device_attribute *attr,
435                             char *buf)
436 {
437         int value = hp_wmi_read_int(HPWMI_HDDTEMP_QUERY);
438         if (value < 0)
439                 return value;
440         return sprintf(buf, "%d\n", value);
441 }
442
443 static ssize_t als_show(struct device *dev, struct device_attribute *attr,
444                         char *buf)
445 {
446         int value = hp_wmi_read_int(HPWMI_ALS_QUERY);
447         if (value < 0)
448                 return value;
449         return sprintf(buf, "%d\n", value);
450 }
451
452 static ssize_t dock_show(struct device *dev, struct device_attribute *attr,
453                          char *buf)
454 {
455         int value = hp_wmi_hw_state(HPWMI_DOCK_MASK);
456         if (value < 0)
457                 return value;
458         return sprintf(buf, "%d\n", value);
459 }
460
461 static ssize_t tablet_show(struct device *dev, struct device_attribute *attr,
462                            char *buf)
463 {
464         int value = hp_wmi_hw_state(HPWMI_TABLET_MASK);
465         if (value < 0)
466                 return value;
467         return sprintf(buf, "%d\n", value);
468 }
469
470 static ssize_t postcode_show(struct device *dev, struct device_attribute *attr,
471                              char *buf)
472 {
473         /* Get the POST error code of previous boot failure. */
474         int value = hp_wmi_read_int(HPWMI_POSTCODEERROR_QUERY);
475         if (value < 0)
476                 return value;
477         return sprintf(buf, "0x%x\n", value);
478 }
479
480 static ssize_t als_store(struct device *dev, struct device_attribute *attr,
481                          const char *buf, size_t count)
482 {
483         u32 tmp;
484         int ret;
485
486         ret = kstrtou32(buf, 10, &tmp);
487         if (ret)
488                 return ret;
489
490         ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, HPWMI_WRITE, &tmp,
491                                        sizeof(tmp), sizeof(tmp));
492         if (ret)
493                 return ret < 0 ? ret : -EINVAL;
494
495         return count;
496 }
497
498 static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,
499                               const char *buf, size_t count)
500 {
501         long unsigned int tmp2;
502         int ret;
503         u32 tmp;
504
505         ret = kstrtoul(buf, 10, &tmp2);
506         if (!ret && tmp2 != 1)
507                 ret = -EINVAL;
508         if (ret)
509                 goto out;
510
511         /* Clear the POST error code. It is kept until until cleared. */
512         tmp = (u32) tmp2;
513         ret = hp_wmi_perform_query(HPWMI_POSTCODEERROR_QUERY, HPWMI_WRITE, &tmp,
514                                        sizeof(tmp), sizeof(tmp));
515
516 out:
517         if (ret)
518                 return ret < 0 ? ret : -EINVAL;
519
520         return count;
521 }
522
523 static DEVICE_ATTR_RO(display);
524 static DEVICE_ATTR_RO(hddtemp);
525 static DEVICE_ATTR_RW(als);
526 static DEVICE_ATTR_RO(dock);
527 static DEVICE_ATTR_RO(tablet);
528 static DEVICE_ATTR_RW(postcode);
529
530 static void hp_wmi_notify(u32 value, void *context)
531 {
532         struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
533         u32 event_id, event_data;
534         union acpi_object *obj;
535         acpi_status status;
536         u32 *location;
537         int key_code;
538
539         status = wmi_get_event_data(value, &response);
540         if (status != AE_OK) {
541                 pr_info("bad event status 0x%x\n", status);
542                 return;
543         }
544
545         obj = (union acpi_object *)response.pointer;
546
547         if (!obj)
548                 return;
549         if (obj->type != ACPI_TYPE_BUFFER) {
550                 pr_info("Unknown response received %d\n", obj->type);
551                 kfree(obj);
552                 return;
553         }
554
555         /*
556          * Depending on ACPI version the concatenation of id and event data
557          * inside _WED function will result in a 8 or 16 byte buffer.
558          */
559         location = (u32 *)obj->buffer.pointer;
560         if (obj->buffer.length == 8) {
561                 event_id = *location;
562                 event_data = *(location + 1);
563         } else if (obj->buffer.length == 16) {
564                 event_id = *location;
565                 event_data = *(location + 2);
566         } else {
567                 pr_info("Unknown buffer length %d\n", obj->buffer.length);
568                 kfree(obj);
569                 return;
570         }
571         kfree(obj);
572
573         switch (event_id) {
574         case HPWMI_DOCK_EVENT:
575                 if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
576                         input_report_switch(hp_wmi_input_dev, SW_DOCK,
577                                             hp_wmi_hw_state(HPWMI_DOCK_MASK));
578                 if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
579                         input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
580                                             hp_wmi_hw_state(HPWMI_TABLET_MASK));
581                 input_sync(hp_wmi_input_dev);
582                 break;
583         case HPWMI_PARK_HDD:
584                 break;
585         case HPWMI_SMART_ADAPTER:
586                 break;
587         case HPWMI_BEZEL_BUTTON:
588                 key_code = hp_wmi_read_int(HPWMI_HOTKEY_QUERY);
589                 if (key_code < 0)
590                         break;
591
592                 if (!sparse_keymap_report_event(hp_wmi_input_dev,
593                                                 key_code, 1, true))
594                         pr_info("Unknown key code - 0x%x\n", key_code);
595                 break;
596         case HPWMI_WIRELESS:
597                 if (rfkill2_count) {
598                         hp_wmi_rfkill2_refresh();
599                         break;
600                 }
601
602                 if (wifi_rfkill)
603                         rfkill_set_states(wifi_rfkill,
604                                           hp_wmi_get_sw_state(HPWMI_WIFI),
605                                           hp_wmi_get_hw_state(HPWMI_WIFI));
606                 if (bluetooth_rfkill)
607                         rfkill_set_states(bluetooth_rfkill,
608                                           hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
609                                           hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
610                 if (wwan_rfkill)
611                         rfkill_set_states(wwan_rfkill,
612                                           hp_wmi_get_sw_state(HPWMI_WWAN),
613                                           hp_wmi_get_hw_state(HPWMI_WWAN));
614                 break;
615         case HPWMI_CPU_BATTERY_THROTTLE:
616                 pr_info("Unimplemented CPU throttle because of 3 Cell battery event detected\n");
617                 break;
618         case HPWMI_LOCK_SWITCH:
619                 break;
620         case HPWMI_LID_SWITCH:
621                 break;
622         case HPWMI_SCREEN_ROTATION:
623                 break;
624         case HPWMI_COOLSENSE_SYSTEM_MOBILE:
625                 break;
626         case HPWMI_COOLSENSE_SYSTEM_HOT:
627                 break;
628         case HPWMI_PROXIMITY_SENSOR:
629                 break;
630         case HPWMI_BACKLIT_KB_BRIGHTNESS:
631                 break;
632         case HPWMI_PEAKSHIFT_PERIOD:
633                 break;
634         case HPWMI_BATTERY_CHARGE_PERIOD:
635                 break;
636         case HPWMI_SANITIZATION_MODE:
637                 break;
638         case HPWMI_SMART_EXPERIENCE_APP:
639                 break;
640         default:
641                 pr_info("Unknown event_id - %d - 0x%x\n", event_id, event_data);
642                 break;
643         }
644 }
645
646 static int __init hp_wmi_input_setup(void)
647 {
648         acpi_status status;
649         int err, val;
650
651         hp_wmi_input_dev = input_allocate_device();
652         if (!hp_wmi_input_dev)
653                 return -ENOMEM;
654
655         hp_wmi_input_dev->name = "HP WMI hotkeys";
656         hp_wmi_input_dev->phys = "wmi/input0";
657         hp_wmi_input_dev->id.bustype = BUS_HOST;
658
659         __set_bit(EV_SW, hp_wmi_input_dev->evbit);
660
661         /* Dock */
662         val = hp_wmi_hw_state(HPWMI_DOCK_MASK);
663         if (!(val < 0)) {
664                 __set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
665                 input_report_switch(hp_wmi_input_dev, SW_DOCK, val);
666         }
667
668         /* Tablet mode */
669         if (enable_tablet_mode_sw > 0) {
670                 val = hp_wmi_hw_state(HPWMI_TABLET_MASK);
671                 if (val >= 0) {
672                         __set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
673                         input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE, val);
674                 }
675         }
676
677         err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
678         if (err)
679                 goto err_free_dev;
680
681         /* Set initial hardware state */
682         input_sync(hp_wmi_input_dev);
683
684         if (!hp_wmi_bios_2009_later() && hp_wmi_bios_2008_later())
685                 hp_wmi_enable_hotkeys();
686
687         status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
688         if (ACPI_FAILURE(status)) {
689                 err = -EIO;
690                 goto err_free_dev;
691         }
692
693         err = input_register_device(hp_wmi_input_dev);
694         if (err)
695                 goto err_uninstall_notifier;
696
697         return 0;
698
699  err_uninstall_notifier:
700         wmi_remove_notify_handler(HPWMI_EVENT_GUID);
701  err_free_dev:
702         input_free_device(hp_wmi_input_dev);
703         return err;
704 }
705
706 static void hp_wmi_input_destroy(void)
707 {
708         wmi_remove_notify_handler(HPWMI_EVENT_GUID);
709         input_unregister_device(hp_wmi_input_dev);
710 }
711
712 static void cleanup_sysfs(struct platform_device *device)
713 {
714         device_remove_file(&device->dev, &dev_attr_display);
715         device_remove_file(&device->dev, &dev_attr_hddtemp);
716         device_remove_file(&device->dev, &dev_attr_als);
717         device_remove_file(&device->dev, &dev_attr_dock);
718         device_remove_file(&device->dev, &dev_attr_tablet);
719         device_remove_file(&device->dev, &dev_attr_postcode);
720 }
721
722 static int __init hp_wmi_rfkill_setup(struct platform_device *device)
723 {
724         int err, wireless;
725
726         wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
727         if (wireless < 0)
728                 return wireless;
729
730         err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE, &wireless,
731                                    sizeof(wireless), 0);
732         if (err)
733                 return err;
734
735         if (wireless & 0x1) {
736                 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
737                                            RFKILL_TYPE_WLAN,
738                                            &hp_wmi_rfkill_ops,
739                                            (void *) HPWMI_WIFI);
740                 if (!wifi_rfkill)
741                         return -ENOMEM;
742                 rfkill_init_sw_state(wifi_rfkill,
743                                      hp_wmi_get_sw_state(HPWMI_WIFI));
744                 rfkill_set_hw_state(wifi_rfkill,
745                                     hp_wmi_get_hw_state(HPWMI_WIFI));
746                 err = rfkill_register(wifi_rfkill);
747                 if (err)
748                         goto register_wifi_error;
749         }
750
751         if (wireless & 0x2) {
752                 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
753                                                 RFKILL_TYPE_BLUETOOTH,
754                                                 &hp_wmi_rfkill_ops,
755                                                 (void *) HPWMI_BLUETOOTH);
756                 if (!bluetooth_rfkill) {
757                         err = -ENOMEM;
758                         goto register_bluetooth_error;
759                 }
760                 rfkill_init_sw_state(bluetooth_rfkill,
761                                      hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
762                 rfkill_set_hw_state(bluetooth_rfkill,
763                                     hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
764                 err = rfkill_register(bluetooth_rfkill);
765                 if (err)
766                         goto register_bluetooth_error;
767         }
768
769         if (wireless & 0x4) {
770                 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
771                                            RFKILL_TYPE_WWAN,
772                                            &hp_wmi_rfkill_ops,
773                                            (void *) HPWMI_WWAN);
774                 if (!wwan_rfkill) {
775                         err = -ENOMEM;
776                         goto register_wwan_error;
777                 }
778                 rfkill_init_sw_state(wwan_rfkill,
779                                      hp_wmi_get_sw_state(HPWMI_WWAN));
780                 rfkill_set_hw_state(wwan_rfkill,
781                                     hp_wmi_get_hw_state(HPWMI_WWAN));
782                 err = rfkill_register(wwan_rfkill);
783                 if (err)
784                         goto register_wwan_error;
785         }
786
787         return 0;
788
789 register_wwan_error:
790         rfkill_destroy(wwan_rfkill);
791         wwan_rfkill = NULL;
792         if (bluetooth_rfkill)
793                 rfkill_unregister(bluetooth_rfkill);
794 register_bluetooth_error:
795         rfkill_destroy(bluetooth_rfkill);
796         bluetooth_rfkill = NULL;
797         if (wifi_rfkill)
798                 rfkill_unregister(wifi_rfkill);
799 register_wifi_error:
800         rfkill_destroy(wifi_rfkill);
801         wifi_rfkill = NULL;
802         return err;
803 }
804
805 static int __init hp_wmi_rfkill2_setup(struct platform_device *device)
806 {
807         struct bios_rfkill2_state state;
808         int err, i;
809
810         err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
811                                    sizeof(state), sizeof(state));
812         if (err)
813                 return err < 0 ? err : -EINVAL;
814
815         if (state.count > HPWMI_MAX_RFKILL2_DEVICES) {
816                 pr_warn("unable to parse 0x1b query output\n");
817                 return -EINVAL;
818         }
819
820         for (i = 0; i < state.count; i++) {
821                 struct rfkill *rfkill;
822                 enum rfkill_type type;
823                 char *name;
824                 switch (state.device[i].radio_type) {
825                 case HPWMI_WIFI:
826                         type = RFKILL_TYPE_WLAN;
827                         name = "hp-wifi";
828                         break;
829                 case HPWMI_BLUETOOTH:
830                         type = RFKILL_TYPE_BLUETOOTH;
831                         name = "hp-bluetooth";
832                         break;
833                 case HPWMI_WWAN:
834                         type = RFKILL_TYPE_WWAN;
835                         name = "hp-wwan";
836                         break;
837                 case HPWMI_GPS:
838                         type = RFKILL_TYPE_GPS;
839                         name = "hp-gps";
840                         break;
841                 default:
842                         pr_warn("unknown device type 0x%x\n",
843                                 state.device[i].radio_type);
844                         continue;
845                 }
846
847                 if (!state.device[i].vendor_id) {
848                         pr_warn("zero device %d while %d reported\n",
849                                 i, state.count);
850                         continue;
851                 }
852
853                 rfkill = rfkill_alloc(name, &device->dev, type,
854                                       &hp_wmi_rfkill2_ops, (void *)(long)i);
855                 if (!rfkill) {
856                         err = -ENOMEM;
857                         goto fail;
858                 }
859
860                 rfkill2[rfkill2_count].id = state.device[i].rfkill_id;
861                 rfkill2[rfkill2_count].num = i;
862                 rfkill2[rfkill2_count].rfkill = rfkill;
863
864                 rfkill_init_sw_state(rfkill,
865                                      IS_SWBLOCKED(state.device[i].power));
866                 rfkill_set_hw_state(rfkill,
867                                     IS_HWBLOCKED(state.device[i].power));
868
869                 if (!(state.device[i].power & HPWMI_POWER_BIOS))
870                         pr_info("device %s blocked by BIOS\n", name);
871
872                 err = rfkill_register(rfkill);
873                 if (err) {
874                         rfkill_destroy(rfkill);
875                         goto fail;
876                 }
877
878                 rfkill2_count++;
879         }
880
881         return 0;
882 fail:
883         for (; rfkill2_count > 0; rfkill2_count--) {
884                 rfkill_unregister(rfkill2[rfkill2_count - 1].rfkill);
885                 rfkill_destroy(rfkill2[rfkill2_count - 1].rfkill);
886         }
887         return err;
888 }
889
890 static int __init hp_wmi_bios_setup(struct platform_device *device)
891 {
892         int err;
893
894         /* clear detected rfkill devices */
895         wifi_rfkill = NULL;
896         bluetooth_rfkill = NULL;
897         wwan_rfkill = NULL;
898         rfkill2_count = 0;
899
900         /*
901          * In pre-2009 BIOS, command 1Bh return 0x4 to indicate that
902          * BIOS no longer controls the power for the wireless
903          * devices. All features supported by this command will no
904          * longer be supported.
905          */
906         if (!hp_wmi_bios_2009_later()) {
907                 if (hp_wmi_rfkill_setup(device))
908                         hp_wmi_rfkill2_setup(device);
909         }
910
911         err = device_create_file(&device->dev, &dev_attr_display);
912         if (err)
913                 goto add_sysfs_error;
914         err = device_create_file(&device->dev, &dev_attr_hddtemp);
915         if (err)
916                 goto add_sysfs_error;
917         err = device_create_file(&device->dev, &dev_attr_als);
918         if (err)
919                 goto add_sysfs_error;
920         err = device_create_file(&device->dev, &dev_attr_dock);
921         if (err)
922                 goto add_sysfs_error;
923         err = device_create_file(&device->dev, &dev_attr_tablet);
924         if (err)
925                 goto add_sysfs_error;
926         err = device_create_file(&device->dev, &dev_attr_postcode);
927         if (err)
928                 goto add_sysfs_error;
929         return 0;
930
931 add_sysfs_error:
932         cleanup_sysfs(device);
933         return err;
934 }
935
936 static int __exit hp_wmi_bios_remove(struct platform_device *device)
937 {
938         int i;
939         cleanup_sysfs(device);
940
941         for (i = 0; i < rfkill2_count; i++) {
942                 rfkill_unregister(rfkill2[i].rfkill);
943                 rfkill_destroy(rfkill2[i].rfkill);
944         }
945
946         if (wifi_rfkill) {
947                 rfkill_unregister(wifi_rfkill);
948                 rfkill_destroy(wifi_rfkill);
949         }
950         if (bluetooth_rfkill) {
951                 rfkill_unregister(bluetooth_rfkill);
952                 rfkill_destroy(bluetooth_rfkill);
953         }
954         if (wwan_rfkill) {
955                 rfkill_unregister(wwan_rfkill);
956                 rfkill_destroy(wwan_rfkill);
957         }
958
959         return 0;
960 }
961
962 static int hp_wmi_resume_handler(struct device *device)
963 {
964         /*
965          * Hardware state may have changed while suspended, so trigger
966          * input events for the current state. As this is a switch,
967          * the input layer will only actually pass it on if the state
968          * changed.
969          */
970         if (hp_wmi_input_dev) {
971                 if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
972                         input_report_switch(hp_wmi_input_dev, SW_DOCK,
973                                             hp_wmi_hw_state(HPWMI_DOCK_MASK));
974                 if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
975                         input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
976                                             hp_wmi_hw_state(HPWMI_TABLET_MASK));
977                 input_sync(hp_wmi_input_dev);
978         }
979
980         if (rfkill2_count)
981                 hp_wmi_rfkill2_refresh();
982
983         if (wifi_rfkill)
984                 rfkill_set_states(wifi_rfkill,
985                                   hp_wmi_get_sw_state(HPWMI_WIFI),
986                                   hp_wmi_get_hw_state(HPWMI_WIFI));
987         if (bluetooth_rfkill)
988                 rfkill_set_states(bluetooth_rfkill,
989                                   hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
990                                   hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
991         if (wwan_rfkill)
992                 rfkill_set_states(wwan_rfkill,
993                                   hp_wmi_get_sw_state(HPWMI_WWAN),
994                                   hp_wmi_get_hw_state(HPWMI_WWAN));
995
996         return 0;
997 }
998
999 static const struct dev_pm_ops hp_wmi_pm_ops = {
1000         .resume  = hp_wmi_resume_handler,
1001         .restore  = hp_wmi_resume_handler,
1002 };
1003
1004 static struct platform_driver hp_wmi_driver = {
1005         .driver = {
1006                 .name = "hp-wmi",
1007                 .pm = &hp_wmi_pm_ops,
1008         },
1009         .remove = __exit_p(hp_wmi_bios_remove),
1010 };
1011
1012 static int __init hp_wmi_init(void)
1013 {
1014         int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
1015         int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
1016         int err;
1017
1018         if (!bios_capable && !event_capable)
1019                 return -ENODEV;
1020
1021         if (event_capable) {
1022                 err = hp_wmi_input_setup();
1023                 if (err)
1024                         return err;
1025         }
1026
1027         if (bios_capable) {
1028                 hp_wmi_platform_dev =
1029                         platform_device_register_simple("hp-wmi", -1, NULL, 0);
1030                 if (IS_ERR(hp_wmi_platform_dev)) {
1031                         err = PTR_ERR(hp_wmi_platform_dev);
1032                         goto err_destroy_input;
1033                 }
1034
1035                 err = platform_driver_probe(&hp_wmi_driver, hp_wmi_bios_setup);
1036                 if (err)
1037                         goto err_unregister_device;
1038         }
1039
1040         return 0;
1041
1042 err_unregister_device:
1043         platform_device_unregister(hp_wmi_platform_dev);
1044 err_destroy_input:
1045         if (event_capable)
1046                 hp_wmi_input_destroy();
1047
1048         return err;
1049 }
1050 module_init(hp_wmi_init);
1051
1052 static void __exit hp_wmi_exit(void)
1053 {
1054         if (wmi_has_guid(HPWMI_EVENT_GUID))
1055                 hp_wmi_input_destroy();
1056
1057         if (hp_wmi_platform_dev) {
1058                 platform_device_unregister(hp_wmi_platform_dev);
1059                 platform_driver_unregister(&hp_wmi_driver);
1060         }
1061 }
1062 module_exit(hp_wmi_exit);