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