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