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