eeepc-wmi: add keys found on EeePC 1215T
[linux-flexiantxendom0-natty.git] / drivers / platform / x86 / eeepc-wmi.c
1 /*
2  * Eee PC WMI hotkey driver
3  *
4  * Copyright(C) 2010 Intel Corporation.
5  * Copyright(C) 2010 Corentin Chary <corentin.chary@gmail.com>
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/types.h>
33 #include <linux/slab.h>
34 #include <linux/input.h>
35 #include <linux/input/sparse-keymap.h>
36 #include <linux/fb.h>
37 #include <linux/backlight.h>
38 #include <linux/leds.h>
39 #include <linux/rfkill.h>
40 #include <linux/pci.h>
41 #include <linux/pci_hotplug.h>
42 #include <linux/debugfs.h>
43 #include <linux/seq_file.h>
44 #include <linux/platform_device.h>
45 #include <linux/dmi.h>
46 #include <acpi/acpi_bus.h>
47 #include <acpi/acpi_drivers.h>
48
49 #define EEEPC_WMI_FILE  "eeepc-wmi"
50
51 MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
52 MODULE_DESCRIPTION("Eee PC WMI Hotkey Driver");
53 MODULE_LICENSE("GPL");
54
55 #define EEEPC_ACPI_HID          "ASUS010" /* old _HID used in eeepc-laptop */
56
57 #define EEEPC_WMI_EVENT_GUID    "ABBC0F72-8EA1-11D1-00A0-C90629100000"
58 #define EEEPC_WMI_MGMT_GUID     "97845ED0-4E6D-11DE-8A39-0800200C9A66"
59
60 MODULE_ALIAS("wmi:"EEEPC_WMI_EVENT_GUID);
61 MODULE_ALIAS("wmi:"EEEPC_WMI_MGMT_GUID);
62
63 #define NOTIFY_BRNUP_MIN                0x11
64 #define NOTIFY_BRNUP_MAX                0x1f
65 #define NOTIFY_BRNDOWN_MIN              0x20
66 #define NOTIFY_BRNDOWN_MAX              0x2e
67
68 /* WMI Methods */
69 #define EEEPC_WMI_METHODID_DSTS         0x53544344
70 #define EEEPC_WMI_METHODID_DEVS         0x53564544
71 #define EEEPC_WMI_METHODID_CFVS         0x53564643
72
73 /* Wireless */
74 #define EEEPC_WMI_DEVID_WLAN            0x00010011
75 #define EEEPC_WMI_DEVID_BLUETOOTH       0x00010013
76 #define EEEPC_WMI_DEVID_WIMAX           0x00010017
77 #define EEEPC_WMI_DEVID_WWAN3G          0x00010019
78
79 /* Backlight and Brightness */
80 #define EEEPC_WMI_DEVID_BACKLIGHT       0x00050011
81 #define EEEPC_WMI_DEVID_BRIGHTNESS      0x00050012
82
83 /* Misc */
84 #define EEEPC_WMI_DEVID_CAMERA          0x00060013
85
86 /* Storage */
87 #define EEEPC_WMI_DEVID_CARDREADER      0x00080013
88
89 /* Input */
90 #define EEEPC_WMI_DEVID_TOUCHPAD        0x00100011
91 #define EEEPC_WMI_DEVID_TOUCHPAD_LED    0x00100012
92
93 /* DSTS masks */
94 #define EEEPC_WMI_DSTS_STATUS_BIT       0x00000001
95 #define EEEPC_WMI_DSTS_PRESENCE_BIT     0x00010000
96 #define EEEPC_WMI_DSTS_BRIGHTNESS_MASK  0x000000FF
97 #define EEEPC_WMI_DSTS_MAX_BRIGTH_MASK  0x0000FF00
98
99 static bool hotplug_wireless;
100
101 module_param(hotplug_wireless, bool, 0444);
102 MODULE_PARM_DESC(hotplug_wireless,
103                  "Enable hotplug for wireless device. "
104                  "If your laptop needs that, please report to "
105                  "acpi4asus-user@lists.sourceforge.net.");
106
107 /* Values for T101MT "Home" key */
108 #define HOME_PRESS                      0xe4
109 #define HOME_HOLD                       0xea
110 #define HOME_RELEASE                    0xe5
111
112 #define EEEPC_WMI_KEY_IGNORE (-1)
113
114 static const struct key_entry eeepc_wmi_keymap[] = {
115         /* Sleep already handled via generic ACPI code */
116         { KE_IGNORE, NOTIFY_BRNDOWN_MIN, { KEY_BRIGHTNESSDOWN } },
117         { KE_IGNORE, NOTIFY_BRNUP_MIN, { KEY_BRIGHTNESSUP } },
118         { KE_KEY, 0x30, { KEY_VOLUMEUP } },
119         { KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
120         { KE_KEY, 0x32, { KEY_MUTE } },
121         { KE_KEY, 0x5c, { KEY_F15 } }, /* Power Gear key */
122         { KE_KEY, 0x5d, { KEY_WLAN } },
123         { KE_KEY, 0x6b, { KEY_F13 } }, /* Disable Touchpad */
124         { KE_KEY, 0x88, { KEY_WLAN } },
125         { KE_KEY, 0xbd, { KEY_CAMERA } },
126         { KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } },
127         { KE_KEY, 0xe0, { KEY_PROG1 } }, /* Task Manager */
128         { KE_KEY, 0xe1, { KEY_F14 } }, /* Change Resolution */
129         { KE_KEY, HOME_PRESS, { KEY_CONFIG } },
130         { KE_KEY, 0xe8, { KEY_SCREENLOCK } },
131         { KE_KEY, 0xe9, { KEY_BRIGHTNESS_ZERO } },
132         { KE_END, 0},
133 };
134
135 struct bios_args {
136         u32     dev_id;
137         u32     ctrl_param;
138 };
139
140 /*
141  * eeepc-wmi/    - debugfs root directory
142  *   dev_id      - current dev_id
143  *   ctrl_param  - current ctrl_param
144  *   devs        - call DEVS(dev_id, ctrl_param) and print result
145  *   dsts        - call DSTS(dev_id)  and print result
146  */
147 struct eeepc_wmi_debug {
148         struct dentry *root;
149         u32 dev_id;
150         u32 ctrl_param;
151 };
152
153 struct eeepc_wmi {
154         bool hotplug_wireless;
155
156         struct input_dev *inputdev;
157         struct backlight_device *backlight_device;
158         struct platform_device *platform_device;
159
160         struct led_classdev tpd_led;
161         int tpd_led_wk;
162         struct workqueue_struct *led_workqueue;
163         struct work_struct tpd_led_work;
164
165         struct rfkill *wlan_rfkill;
166         struct rfkill *bluetooth_rfkill;
167         struct rfkill *wimax_rfkill;
168         struct rfkill *wwan3g_rfkill;
169
170         struct hotplug_slot *hotplug_slot;
171         struct mutex hotplug_lock;
172         struct mutex wmi_lock;
173         struct workqueue_struct *hotplug_workqueue;
174         struct work_struct hotplug_work;
175
176         struct eeepc_wmi_debug debug;
177 };
178
179 static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc)
180 {
181         int err;
182
183         eeepc->inputdev = input_allocate_device();
184         if (!eeepc->inputdev)
185                 return -ENOMEM;
186
187         eeepc->inputdev->name = "Eee PC WMI hotkeys";
188         eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0";
189         eeepc->inputdev->id.bustype = BUS_HOST;
190         eeepc->inputdev->dev.parent = &eeepc->platform_device->dev;
191         __set_bit(EV_REP, eeepc->inputdev->evbit);
192
193         err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL);
194         if (err)
195                 goto err_free_dev;
196
197         err = input_register_device(eeepc->inputdev);
198         if (err)
199                 goto err_free_keymap;
200
201         return 0;
202
203 err_free_keymap:
204         sparse_keymap_free(eeepc->inputdev);
205 err_free_dev:
206         input_free_device(eeepc->inputdev);
207         return err;
208 }
209
210 static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc)
211 {
212         if (eeepc->inputdev) {
213                 sparse_keymap_free(eeepc->inputdev);
214                 input_unregister_device(eeepc->inputdev);
215         }
216
217         eeepc->inputdev = NULL;
218 }
219
220 static acpi_status eeepc_wmi_get_devstate(u32 dev_id, u32 *retval)
221 {
222         struct acpi_buffer input = { (acpi_size)sizeof(u32), &dev_id };
223         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
224         union acpi_object *obj;
225         acpi_status status;
226         u32 tmp;
227
228         status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
229                                      1, EEEPC_WMI_METHODID_DSTS,
230                                      &input, &output);
231
232         if (ACPI_FAILURE(status))
233                 return status;
234
235         obj = (union acpi_object *)output.pointer;
236         if (obj && obj->type == ACPI_TYPE_INTEGER)
237                 tmp = (u32)obj->integer.value;
238         else
239                 tmp = 0;
240
241         if (retval)
242                 *retval = tmp;
243
244         kfree(obj);
245
246         return status;
247
248 }
249
250 static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
251                                           u32 *retval)
252 {
253         struct bios_args args = {
254                 .dev_id = dev_id,
255                 .ctrl_param = ctrl_param,
256         };
257         struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
258         acpi_status status;
259
260         if (!retval) {
261                 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
262                                              EEEPC_WMI_METHODID_DEVS,
263                                              &input, NULL);
264         } else {
265                 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
266                 union acpi_object *obj;
267                 u32 tmp;
268
269                 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
270                                              EEEPC_WMI_METHODID_DEVS,
271                                              &input, &output);
272
273                 if (ACPI_FAILURE(status))
274                         return status;
275
276                 obj = (union acpi_object *)output.pointer;
277                 if (obj && obj->type == ACPI_TYPE_INTEGER)
278                         tmp = (u32)obj->integer.value;
279                 else
280                         tmp = 0;
281
282                 *retval = tmp;
283
284                 kfree(obj);
285         }
286
287         return status;
288 }
289
290 /* Helper for special devices with magic return codes */
291 static int eeepc_wmi_get_devstate_bits(u32 dev_id, u32 mask)
292 {
293         u32 retval = 0;
294         acpi_status status;
295
296         status = eeepc_wmi_get_devstate(dev_id, &retval);
297
298         if (ACPI_FAILURE(status))
299                 return -EINVAL;
300
301         if (!(retval & EEEPC_WMI_DSTS_PRESENCE_BIT))
302                 return -ENODEV;
303
304         return retval & mask;
305 }
306
307 static int eeepc_wmi_get_devstate_simple(u32 dev_id)
308 {
309         return eeepc_wmi_get_devstate_bits(dev_id, EEEPC_WMI_DSTS_STATUS_BIT);
310 }
311
312 /*
313  * LEDs
314  */
315 /*
316  * These functions actually update the LED's, and are called from a
317  * workqueue. By doing this as separate work rather than when the LED
318  * subsystem asks, we avoid messing with the Eeepc ACPI stuff during a
319  * potentially bad time, such as a timer interrupt.
320  */
321 static void tpd_led_update(struct work_struct *work)
322 {
323         int ctrl_param;
324         struct eeepc_wmi *eeepc;
325
326         eeepc = container_of(work, struct eeepc_wmi, tpd_led_work);
327
328         ctrl_param = eeepc->tpd_led_wk;
329         eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
330 }
331
332 static void tpd_led_set(struct led_classdev *led_cdev,
333                         enum led_brightness value)
334 {
335         struct eeepc_wmi *eeepc;
336
337         eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
338
339         eeepc->tpd_led_wk = !!value;
340         queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
341 }
342
343 static int read_tpd_led_state(struct eeepc_wmi *eeepc)
344 {
345         return eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_TOUCHPAD_LED);
346 }
347
348 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
349 {
350         struct eeepc_wmi *eeepc;
351
352         eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
353
354         return read_tpd_led_state(eeepc);
355 }
356
357 static int eeepc_wmi_led_init(struct eeepc_wmi *eeepc)
358 {
359         int rv;
360
361         if (read_tpd_led_state(eeepc) < 0)
362                 return 0;
363
364         eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
365         if (!eeepc->led_workqueue)
366                 return -ENOMEM;
367         INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
368
369         eeepc->tpd_led.name = "eeepc::touchpad";
370         eeepc->tpd_led.brightness_set = tpd_led_set;
371         eeepc->tpd_led.brightness_get = tpd_led_get;
372         eeepc->tpd_led.max_brightness = 1;
373
374         rv = led_classdev_register(&eeepc->platform_device->dev,
375                                    &eeepc->tpd_led);
376         if (rv) {
377                 destroy_workqueue(eeepc->led_workqueue);
378                 return rv;
379         }
380
381         return 0;
382 }
383
384 static void eeepc_wmi_led_exit(struct eeepc_wmi *eeepc)
385 {
386         if (eeepc->tpd_led.dev)
387                 led_classdev_unregister(&eeepc->tpd_led);
388         if (eeepc->led_workqueue)
389                 destroy_workqueue(eeepc->led_workqueue);
390 }
391
392 /*
393  * PCI hotplug (for wlan rfkill)
394  */
395 static bool eeepc_wlan_rfkill_blocked(struct eeepc_wmi *eeepc)
396 {
397         int result = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
398
399         if (result < 0)
400                 return false;
401         return !result;
402 }
403
404 static void eeepc_rfkill_hotplug(struct eeepc_wmi *eeepc)
405 {
406         struct pci_dev *dev;
407         struct pci_bus *bus;
408         bool blocked;
409         bool absent;
410         u32 l;
411
412         mutex_lock(&eeepc->wmi_lock);
413         blocked = eeepc_wlan_rfkill_blocked(eeepc);
414         mutex_unlock(&eeepc->wmi_lock);
415
416         mutex_lock(&eeepc->hotplug_lock);
417
418         if (eeepc->wlan_rfkill)
419                 rfkill_set_sw_state(eeepc->wlan_rfkill, blocked);
420
421         if (eeepc->hotplug_slot) {
422                 bus = pci_find_bus(0, 1);
423                 if (!bus) {
424                         pr_warning("Unable to find PCI bus 1?\n");
425                         goto out_unlock;
426                 }
427
428                 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
429                         pr_err("Unable to read PCI config space?\n");
430                         goto out_unlock;
431                 }
432                 absent = (l == 0xffffffff);
433
434                 if (blocked != absent) {
435                         pr_warning("BIOS says wireless lan is %s, "
436                                         "but the pci device is %s\n",
437                                 blocked ? "blocked" : "unblocked",
438                                 absent ? "absent" : "present");
439                         pr_warning("skipped wireless hotplug as probably "
440                                         "inappropriate for this model\n");
441                         goto out_unlock;
442                 }
443
444                 if (!blocked) {
445                         dev = pci_get_slot(bus, 0);
446                         if (dev) {
447                                 /* Device already present */
448                                 pci_dev_put(dev);
449                                 goto out_unlock;
450                         }
451                         dev = pci_scan_single_device(bus, 0);
452                         if (dev) {
453                                 pci_bus_assign_resources(bus);
454                                 if (pci_bus_add_device(dev))
455                                         pr_err("Unable to hotplug wifi\n");
456                         }
457                 } else {
458                         dev = pci_get_slot(bus, 0);
459                         if (dev) {
460                                 pci_remove_bus_device(dev);
461                                 pci_dev_put(dev);
462                         }
463                 }
464         }
465
466 out_unlock:
467         mutex_unlock(&eeepc->hotplug_lock);
468 }
469
470 static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
471 {
472         struct eeepc_wmi *eeepc = data;
473
474         if (event != ACPI_NOTIFY_BUS_CHECK)
475                 return;
476
477         /*
478          * We can't call directly eeepc_rfkill_hotplug because most
479          * of the time WMBC is still being executed and not reetrant.
480          * There is currently no way to tell ACPICA that  we want this
481          * method to be serialized, we schedule a eeepc_rfkill_hotplug
482          * call later, in a safer context.
483          */
484         queue_work(eeepc->hotplug_workqueue, &eeepc->hotplug_work);
485 }
486
487 static int eeepc_register_rfkill_notifier(struct eeepc_wmi *eeepc,
488                                           char *node)
489 {
490         acpi_status status;
491         acpi_handle handle;
492
493         status = acpi_get_handle(NULL, node, &handle);
494
495         if (ACPI_SUCCESS(status)) {
496                 status = acpi_install_notify_handler(handle,
497                                                      ACPI_SYSTEM_NOTIFY,
498                                                      eeepc_rfkill_notify,
499                                                      eeepc);
500                 if (ACPI_FAILURE(status))
501                         pr_warning("Failed to register notify on %s\n", node);
502         } else
503                 return -ENODEV;
504
505         return 0;
506 }
507
508 static void eeepc_unregister_rfkill_notifier(struct eeepc_wmi *eeepc,
509                                              char *node)
510 {
511         acpi_status status = AE_OK;
512         acpi_handle handle;
513
514         status = acpi_get_handle(NULL, node, &handle);
515
516         if (ACPI_SUCCESS(status)) {
517                 status = acpi_remove_notify_handler(handle,
518                                                      ACPI_SYSTEM_NOTIFY,
519                                                      eeepc_rfkill_notify);
520                 if (ACPI_FAILURE(status))
521                         pr_err("Error removing rfkill notify handler %s\n",
522                                 node);
523         }
524 }
525
526 static int eeepc_get_adapter_status(struct hotplug_slot *hotplug_slot,
527                                     u8 *value)
528 {
529         int result = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
530
531         if (result < 0)
532                 return result;
533
534         *value = !!result;
535         return 0;
536 }
537
538 static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
539 {
540         kfree(hotplug_slot->info);
541         kfree(hotplug_slot);
542 }
543
544 static struct hotplug_slot_ops eeepc_hotplug_slot_ops = {
545         .owner = THIS_MODULE,
546         .get_adapter_status = eeepc_get_adapter_status,
547         .get_power_status = eeepc_get_adapter_status,
548 };
549
550 static void eeepc_hotplug_work(struct work_struct *work)
551 {
552         struct eeepc_wmi *eeepc;
553
554         eeepc = container_of(work, struct eeepc_wmi, hotplug_work);
555         eeepc_rfkill_hotplug(eeepc);
556 }
557
558 static int eeepc_setup_pci_hotplug(struct eeepc_wmi *eeepc)
559 {
560         int ret = -ENOMEM;
561         struct pci_bus *bus = pci_find_bus(0, 1);
562
563         if (!bus) {
564                 pr_err("Unable to find wifi PCI bus\n");
565                 return -ENODEV;
566         }
567
568         eeepc->hotplug_workqueue =
569                 create_singlethread_workqueue("hotplug_workqueue");
570         if (!eeepc->hotplug_workqueue)
571                 goto error_workqueue;
572
573         INIT_WORK(&eeepc->hotplug_work, eeepc_hotplug_work);
574
575         eeepc->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
576         if (!eeepc->hotplug_slot)
577                 goto error_slot;
578
579         eeepc->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
580                                             GFP_KERNEL);
581         if (!eeepc->hotplug_slot->info)
582                 goto error_info;
583
584         eeepc->hotplug_slot->private = eeepc;
585         eeepc->hotplug_slot->release = &eeepc_cleanup_pci_hotplug;
586         eeepc->hotplug_slot->ops = &eeepc_hotplug_slot_ops;
587         eeepc_get_adapter_status(eeepc->hotplug_slot,
588                                  &eeepc->hotplug_slot->info->adapter_status);
589
590         ret = pci_hp_register(eeepc->hotplug_slot, bus, 0, "eeepc-wifi");
591         if (ret) {
592                 pr_err("Unable to register hotplug slot - %d\n", ret);
593                 goto error_register;
594         }
595
596         return 0;
597
598 error_register:
599         kfree(eeepc->hotplug_slot->info);
600 error_info:
601         kfree(eeepc->hotplug_slot);
602         eeepc->hotplug_slot = NULL;
603 error_slot:
604         destroy_workqueue(eeepc->hotplug_workqueue);
605 error_workqueue:
606         return ret;
607 }
608
609 /*
610  * Rfkill devices
611  */
612 static int eeepc_rfkill_set(void *data, bool blocked)
613 {
614         int dev_id = (unsigned long)data;
615         u32 ctrl_param = !blocked;
616         acpi_status status;
617
618         status = eeepc_wmi_set_devstate(dev_id, ctrl_param, NULL);
619
620         if (ACPI_FAILURE(status))
621                 return -EIO;
622
623         return 0;
624 }
625
626 static void eeepc_rfkill_query(struct rfkill *rfkill, void *data)
627 {
628         int dev_id = (unsigned long)data;
629         int result;
630
631         result = eeepc_wmi_get_devstate_simple(dev_id);
632
633         if (result < 0)
634                 return ;
635
636         rfkill_set_sw_state(rfkill, !result);
637 }
638
639 static int eeepc_rfkill_wlan_set(void *data, bool blocked)
640 {
641         struct eeepc_wmi *eeepc = data;
642         int ret;
643
644         /*
645          * This handler is enabled only if hotplug is enabled.
646          * In this case, the eeepc_wmi_set_devstate() will
647          * trigger a wmi notification and we need to wait
648          * this call to finish before being able to call
649          * any wmi method
650          */
651         mutex_lock(&eeepc->wmi_lock);
652         ret = eeepc_rfkill_set((void *)(long)EEEPC_WMI_DEVID_WLAN, blocked);
653         mutex_unlock(&eeepc->wmi_lock);
654         return ret;
655 }
656
657 static void eeepc_rfkill_wlan_query(struct rfkill *rfkill, void *data)
658 {
659         eeepc_rfkill_query(rfkill, (void *)(long)EEEPC_WMI_DEVID_WLAN);
660 }
661
662 static const struct rfkill_ops eeepc_rfkill_wlan_ops = {
663         .set_block = eeepc_rfkill_wlan_set,
664         .query = eeepc_rfkill_wlan_query,
665 };
666
667 static const struct rfkill_ops eeepc_rfkill_ops = {
668         .set_block = eeepc_rfkill_set,
669         .query = eeepc_rfkill_query,
670 };
671
672 static int eeepc_new_rfkill(struct eeepc_wmi *eeepc,
673                             struct rfkill **rfkill,
674                             const char *name,
675                             enum rfkill_type type, int dev_id)
676 {
677         int result = eeepc_wmi_get_devstate_simple(dev_id);
678
679         if (result < 0)
680                 return result;
681
682         if (dev_id == EEEPC_WMI_DEVID_WLAN && eeepc->hotplug_wireless)
683                 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
684                                        &eeepc_rfkill_wlan_ops, eeepc);
685         else
686                 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
687                                        &eeepc_rfkill_ops, (void *)(long)dev_id);
688
689         if (!*rfkill)
690                 return -EINVAL;
691
692         rfkill_init_sw_state(*rfkill, !result);
693         result = rfkill_register(*rfkill);
694         if (result) {
695                 rfkill_destroy(*rfkill);
696                 *rfkill = NULL;
697                 return result;
698         }
699         return 0;
700 }
701
702 static void eeepc_wmi_rfkill_exit(struct eeepc_wmi *eeepc)
703 {
704         eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
705         eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
706         eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
707         if (eeepc->wlan_rfkill) {
708                 rfkill_unregister(eeepc->wlan_rfkill);
709                 rfkill_destroy(eeepc->wlan_rfkill);
710                 eeepc->wlan_rfkill = NULL;
711         }
712         /*
713          * Refresh pci hotplug in case the rfkill state was changed after
714          * eeepc_unregister_rfkill_notifier()
715          */
716         eeepc_rfkill_hotplug(eeepc);
717         if (eeepc->hotplug_slot)
718                 pci_hp_deregister(eeepc->hotplug_slot);
719         if (eeepc->hotplug_workqueue)
720                 destroy_workqueue(eeepc->hotplug_workqueue);
721
722         if (eeepc->bluetooth_rfkill) {
723                 rfkill_unregister(eeepc->bluetooth_rfkill);
724                 rfkill_destroy(eeepc->bluetooth_rfkill);
725                 eeepc->bluetooth_rfkill = NULL;
726         }
727         if (eeepc->wimax_rfkill) {
728                 rfkill_unregister(eeepc->wimax_rfkill);
729                 rfkill_destroy(eeepc->wimax_rfkill);
730                 eeepc->wimax_rfkill = NULL;
731         }
732         if (eeepc->wwan3g_rfkill) {
733                 rfkill_unregister(eeepc->wwan3g_rfkill);
734                 rfkill_destroy(eeepc->wwan3g_rfkill);
735                 eeepc->wwan3g_rfkill = NULL;
736         }
737 }
738
739 static int eeepc_wmi_rfkill_init(struct eeepc_wmi *eeepc)
740 {
741         int result = 0;
742
743         mutex_init(&eeepc->hotplug_lock);
744         mutex_init(&eeepc->wmi_lock);
745
746         result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
747                                   "eeepc-wlan", RFKILL_TYPE_WLAN,
748                                   EEEPC_WMI_DEVID_WLAN);
749
750         if (result && result != -ENODEV)
751                 goto exit;
752
753         result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
754                                   "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
755                                   EEEPC_WMI_DEVID_BLUETOOTH);
756
757         if (result && result != -ENODEV)
758                 goto exit;
759
760         result = eeepc_new_rfkill(eeepc, &eeepc->wimax_rfkill,
761                                   "eeepc-wimax", RFKILL_TYPE_WIMAX,
762                                   EEEPC_WMI_DEVID_WIMAX);
763
764         if (result && result != -ENODEV)
765                 goto exit;
766
767         result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
768                                   "eeepc-wwan3g", RFKILL_TYPE_WWAN,
769                                   EEEPC_WMI_DEVID_WWAN3G);
770
771         if (result && result != -ENODEV)
772                 goto exit;
773
774         if (!eeepc->hotplug_wireless)
775                 goto exit;
776
777         result = eeepc_setup_pci_hotplug(eeepc);
778         /*
779          * If we get -EBUSY then something else is handling the PCI hotplug -
780          * don't fail in this case
781          */
782         if (result == -EBUSY)
783                 result = 0;
784
785         eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
786         eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
787         eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
788         /*
789          * Refresh pci hotplug in case the rfkill state was changed during
790          * setup.
791          */
792         eeepc_rfkill_hotplug(eeepc);
793
794 exit:
795         if (result && result != -ENODEV)
796                 eeepc_wmi_rfkill_exit(eeepc);
797
798         if (result == -ENODEV)
799                 result = 0;
800
801         return result;
802 }
803
804 /*
805  * Backlight
806  */
807 static int read_backlight_power(void)
808 {
809         int ret = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_BACKLIGHT);
810
811         if (ret < 0)
812                 return ret;
813
814         return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
815 }
816
817 static int read_brightness(struct backlight_device *bd)
818 {
819         u32 retval;
820         acpi_status status;
821
822         status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BRIGHTNESS, &retval);
823
824         if (ACPI_FAILURE(status))
825                 return -EIO;
826         else
827                 return retval & EEEPC_WMI_DSTS_BRIGHTNESS_MASK;
828 }
829
830 static int update_bl_status(struct backlight_device *bd)
831 {
832         u32 ctrl_param;
833         acpi_status status;
834         int power;
835
836         ctrl_param = bd->props.brightness;
837
838         status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BRIGHTNESS,
839                                         ctrl_param, NULL);
840
841         if (ACPI_FAILURE(status))
842                 return -EIO;
843
844         power = read_backlight_power();
845         if (power != -ENODEV && bd->props.power != power) {
846                 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
847                 status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT,
848                                                 ctrl_param, NULL);
849
850                 if (ACPI_FAILURE(status))
851                         return -EIO;
852         }
853         return 0;
854 }
855
856 static const struct backlight_ops eeepc_wmi_bl_ops = {
857         .get_brightness = read_brightness,
858         .update_status = update_bl_status,
859 };
860
861 static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
862 {
863         struct backlight_device *bd = eeepc->backlight_device;
864         int old = bd->props.brightness;
865         int new = old;
866
867         if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
868                 new = code - NOTIFY_BRNUP_MIN + 1;
869         else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
870                 new = code - NOTIFY_BRNDOWN_MIN;
871
872         bd->props.brightness = new;
873         backlight_update_status(bd);
874         backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
875
876         return old;
877 }
878
879 static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
880 {
881         struct backlight_device *bd;
882         struct backlight_properties props;
883         int max;
884         int power;
885
886         max = eeepc_wmi_get_devstate_bits(EEEPC_WMI_DEVID_BRIGHTNESS,
887                                           EEEPC_WMI_DSTS_MAX_BRIGTH_MASK);
888         power = read_backlight_power();
889
890         if (max < 0 && power < 0) {
891                 /* Try to keep the original error */
892                 if (max == -ENODEV && power == -ENODEV)
893                         return -ENODEV;
894                 if (max != -ENODEV)
895                         return max;
896                 else
897                         return power;
898         }
899         if (max == -ENODEV)
900                 max = 0;
901         if (power == -ENODEV)
902                 power = FB_BLANK_UNBLANK;
903
904         memset(&props, 0, sizeof(struct backlight_properties));
905         props.max_brightness = max;
906         bd = backlight_device_register(EEEPC_WMI_FILE,
907                                        &eeepc->platform_device->dev, eeepc,
908                                        &eeepc_wmi_bl_ops, &props);
909         if (IS_ERR(bd)) {
910                 pr_err("Could not register backlight device\n");
911                 return PTR_ERR(bd);
912         }
913
914         eeepc->backlight_device = bd;
915
916         bd->props.brightness = read_brightness(bd);
917         bd->props.power = power;
918         backlight_update_status(bd);
919
920         return 0;
921 }
922
923 static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
924 {
925         if (eeepc->backlight_device)
926                 backlight_device_unregister(eeepc->backlight_device);
927
928         eeepc->backlight_device = NULL;
929 }
930
931 static void eeepc_wmi_homekey_filter(struct eeepc_wmi *eeepc, int *code,
932                                      unsigned int *value, bool *autorelease)
933 {
934         switch (*code) {
935         case HOME_PRESS:
936                 *value = 1;
937                 *autorelease = 0;
938                 break;
939         case HOME_HOLD:
940                 *code = EEEPC_WMI_KEY_IGNORE;
941                 break;
942         case HOME_RELEASE:
943                 *code = HOME_PRESS;
944                 *value = 0;
945                 *autorelease = 0;
946                 break;
947         }
948 }
949
950 static void eeepc_wmi_notify(u32 value, void *context)
951 {
952         struct eeepc_wmi *eeepc = context;
953         struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
954         union acpi_object *obj;
955         acpi_status status;
956         int code;
957         int orig_code;
958         unsigned int key_value = 1;
959         bool autorelease = 1;
960
961         status = wmi_get_event_data(value, &response);
962         if (status != AE_OK) {
963                 pr_err("bad event status 0x%x\n", status);
964                 return;
965         }
966
967         obj = (union acpi_object *)response.pointer;
968
969         if (obj && obj->type == ACPI_TYPE_INTEGER) {
970                 code = obj->integer.value;
971                 orig_code = code;
972
973                 eeepc_wmi_homekey_filter(eeepc, &code, &key_value,
974                                          &autorelease);
975                 if (code == EEEPC_WMI_KEY_IGNORE)
976                         goto exit;
977
978                 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
979                         code = NOTIFY_BRNUP_MIN;
980                 else if (code >= NOTIFY_BRNDOWN_MIN &&
981                          code <= NOTIFY_BRNDOWN_MAX)
982                         code = NOTIFY_BRNDOWN_MIN;
983
984                 if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
985                         if (!acpi_video_backlight_support())
986                                 eeepc_wmi_backlight_notify(eeepc, orig_code);
987                 }
988
989                 if (!sparse_keymap_report_event(eeepc->inputdev, code,
990                                                 key_value, autorelease))
991                         pr_info("Unknown key %x pressed\n", code);
992         }
993
994 exit:
995         kfree(obj);
996 }
997
998 /*
999  * Sys helpers
1000  */
1001 static int parse_arg(const char *buf, unsigned long count, int *val)
1002 {
1003         if (!count)
1004                 return 0;
1005         if (sscanf(buf, "%i", val) != 1)
1006                 return -EINVAL;
1007         return count;
1008 }
1009
1010 static ssize_t store_sys_wmi(int devid, const char *buf, size_t count)
1011 {
1012         acpi_status status;
1013         u32 retval;
1014         int rv, value;
1015
1016         value = eeepc_wmi_get_devstate_simple(devid);
1017         if (value == -ENODEV) /* Check device presence */
1018                 return value;
1019
1020         rv = parse_arg(buf, count, &value);
1021         status = eeepc_wmi_set_devstate(devid, value, &retval);
1022
1023         if (ACPI_FAILURE(status))
1024                 return -EIO;
1025         return rv;
1026 }
1027
1028 static ssize_t show_sys_wmi(int devid, char *buf)
1029 {
1030         int value = eeepc_wmi_get_devstate_simple(devid);
1031
1032         if (value < 0)
1033                 return value;
1034
1035         return sprintf(buf, "%d\n", value);
1036 }
1037
1038 #define EEEPC_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm)                 \
1039         static ssize_t show_##_name(struct device *dev,                 \
1040                                     struct device_attribute *attr,      \
1041                                     char *buf)                          \
1042         {                                                               \
1043                 return show_sys_wmi(_cm, buf);                          \
1044         }                                                               \
1045         static ssize_t store_##_name(struct device *dev,                \
1046                                      struct device_attribute *attr,     \
1047                                      const char *buf, size_t count)     \
1048         {                                                               \
1049                 return store_sys_wmi(_cm, buf, count);                  \
1050         }                                                               \
1051         static struct device_attribute dev_attr_##_name = {             \
1052                 .attr = {                                               \
1053                         .name = __stringify(_name),                     \
1054                         .mode = _mode },                                \
1055                 .show   = show_##_name,                                 \
1056                 .store  = store_##_name,                                \
1057         }
1058
1059 EEEPC_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, EEEPC_WMI_DEVID_TOUCHPAD);
1060 EEEPC_WMI_CREATE_DEVICE_ATTR(camera, 0644, EEEPC_WMI_DEVID_CAMERA);
1061 EEEPC_WMI_CREATE_DEVICE_ATTR(cardr, 0644, EEEPC_WMI_DEVID_CARDREADER);
1062
1063 static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
1064                            const char *buf, size_t count)
1065 {
1066         int value;
1067         struct acpi_buffer input = { (acpi_size)sizeof(value), &value };
1068         acpi_status status;
1069
1070         if (!count || sscanf(buf, "%i", &value) != 1)
1071                 return -EINVAL;
1072         if (value < 0 || value > 2)
1073                 return -EINVAL;
1074
1075         status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
1076                                      1, EEEPC_WMI_METHODID_CFVS, &input, NULL);
1077
1078         if (ACPI_FAILURE(status))
1079                 return -EIO;
1080         else
1081                 return count;
1082 }
1083
1084 static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
1085
1086 static struct attribute *platform_attributes[] = {
1087         &dev_attr_cpufv.attr,
1088         &dev_attr_camera.attr,
1089         &dev_attr_cardr.attr,
1090         &dev_attr_touchpad.attr,
1091         NULL
1092 };
1093
1094 static mode_t eeepc_sysfs_is_visible(struct kobject *kobj,
1095                                      struct attribute *attr,
1096                                      int idx)
1097 {
1098         bool supported = true;
1099         int devid = -1;
1100
1101         if (attr == &dev_attr_camera.attr)
1102                 devid = EEEPC_WMI_DEVID_CAMERA;
1103         else if (attr == &dev_attr_cardr.attr)
1104                 devid = EEEPC_WMI_DEVID_CARDREADER;
1105         else if (attr == &dev_attr_touchpad.attr)
1106                 devid = EEEPC_WMI_DEVID_TOUCHPAD;
1107
1108         if (devid != -1)
1109                 supported = eeepc_wmi_get_devstate_simple(devid) != -ENODEV;
1110
1111         return supported ? attr->mode : 0;
1112 }
1113
1114 static struct attribute_group platform_attribute_group = {
1115         .is_visible     = eeepc_sysfs_is_visible,
1116         .attrs          = platform_attributes
1117 };
1118
1119 static void eeepc_wmi_sysfs_exit(struct platform_device *device)
1120 {
1121         sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
1122 }
1123
1124 static int eeepc_wmi_sysfs_init(struct platform_device *device)
1125 {
1126         return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
1127 }
1128
1129 /*
1130  * Platform device
1131  */
1132 static int __init eeepc_wmi_platform_init(struct eeepc_wmi *eeepc)
1133 {
1134         return eeepc_wmi_sysfs_init(eeepc->platform_device);
1135 }
1136
1137 static void eeepc_wmi_platform_exit(struct eeepc_wmi *eeepc)
1138 {
1139         eeepc_wmi_sysfs_exit(eeepc->platform_device);
1140 }
1141
1142 /*
1143  * debugfs
1144  */
1145 struct eeepc_wmi_debugfs_node {
1146         struct eeepc_wmi *eeepc;
1147         char *name;
1148         int (*show)(struct seq_file *m, void *data);
1149 };
1150
1151 static int show_dsts(struct seq_file *m, void *data)
1152 {
1153         struct eeepc_wmi *eeepc = m->private;
1154         acpi_status status;
1155         u32 retval = -1;
1156
1157         status = eeepc_wmi_get_devstate(eeepc->debug.dev_id, &retval);
1158
1159         if (ACPI_FAILURE(status))
1160                 return -EIO;
1161
1162         seq_printf(m, "DSTS(%x) = %x\n", eeepc->debug.dev_id, retval);
1163
1164         return 0;
1165 }
1166
1167 static int show_devs(struct seq_file *m, void *data)
1168 {
1169         struct eeepc_wmi *eeepc = m->private;
1170         acpi_status status;
1171         u32 retval = -1;
1172
1173         status = eeepc_wmi_set_devstate(eeepc->debug.dev_id,
1174                                         eeepc->debug.ctrl_param, &retval);
1175         if (ACPI_FAILURE(status))
1176                 return -EIO;
1177
1178         seq_printf(m, "DEVS(%x, %x) = %x\n", eeepc->debug.dev_id,
1179                    eeepc->debug.ctrl_param, retval);
1180
1181         return 0;
1182 }
1183
1184 static struct eeepc_wmi_debugfs_node eeepc_wmi_debug_files[] = {
1185         { NULL, "devs", show_devs },
1186         { NULL, "dsts", show_dsts },
1187 };
1188
1189 static int eeepc_wmi_debugfs_open(struct inode *inode, struct file *file)
1190 {
1191         struct eeepc_wmi_debugfs_node *node = inode->i_private;
1192
1193         return single_open(file, node->show, node->eeepc);
1194 }
1195
1196 static const struct file_operations eeepc_wmi_debugfs_io_ops = {
1197         .owner = THIS_MODULE,
1198         .open  = eeepc_wmi_debugfs_open,
1199         .read = seq_read,
1200         .llseek = seq_lseek,
1201         .release = single_release,
1202 };
1203
1204 static void eeepc_wmi_debugfs_exit(struct eeepc_wmi *eeepc)
1205 {
1206         debugfs_remove_recursive(eeepc->debug.root);
1207 }
1208
1209 static int eeepc_wmi_debugfs_init(struct eeepc_wmi *eeepc)
1210 {
1211         struct dentry *dent;
1212         int i;
1213
1214         eeepc->debug.root = debugfs_create_dir(EEEPC_WMI_FILE, NULL);
1215         if (!eeepc->debug.root) {
1216                 pr_err("failed to create debugfs directory");
1217                 goto error_debugfs;
1218         }
1219
1220         dent = debugfs_create_x32("dev_id", S_IRUGO|S_IWUSR,
1221                                   eeepc->debug.root, &eeepc->debug.dev_id);
1222         if (!dent)
1223                 goto error_debugfs;
1224
1225         dent = debugfs_create_x32("ctrl_param", S_IRUGO|S_IWUSR,
1226                                   eeepc->debug.root, &eeepc->debug.ctrl_param);
1227         if (!dent)
1228                 goto error_debugfs;
1229
1230         for (i = 0; i < ARRAY_SIZE(eeepc_wmi_debug_files); i++) {
1231                 struct eeepc_wmi_debugfs_node *node = &eeepc_wmi_debug_files[i];
1232
1233                 node->eeepc = eeepc;
1234                 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
1235                                            eeepc->debug.root, node,
1236                                            &eeepc_wmi_debugfs_io_ops);
1237                 if (!dent) {
1238                         pr_err("failed to create debug file: %s\n", node->name);
1239                         goto error_debugfs;
1240                 }
1241         }
1242
1243         return 0;
1244
1245 error_debugfs:
1246         eeepc_wmi_debugfs_exit(eeepc);
1247         return -ENOMEM;
1248 }
1249
1250 /*
1251  * WMI Driver
1252  */
1253 static void eeepc_dmi_check(struct eeepc_wmi *eeepc)
1254 {
1255         const char *model;
1256
1257         model = dmi_get_system_info(DMI_PRODUCT_NAME);
1258         if (!model)
1259                 return;
1260
1261         /*
1262          * Whitelist for wlan hotplug
1263          *
1264          * Eeepc 1000H needs the current hotplug code to handle
1265          * Fn+F2 correctly. We may add other Eeepc here later, but
1266          * it seems that most of the laptops supported by eeepc-wmi
1267          * don't need to be on this list
1268          */
1269         if (strcmp(model, "1000H") == 0) {
1270                 eeepc->hotplug_wireless = true;
1271                 pr_info("wlan hotplug enabled\n");
1272         }
1273 }
1274
1275 static int __init eeepc_wmi_add(struct platform_device *pdev)
1276 {
1277         struct eeepc_wmi *eeepc;
1278         acpi_status status;
1279         int err;
1280
1281         eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
1282         if (!eeepc)
1283                 return -ENOMEM;
1284
1285         eeepc->platform_device = pdev;
1286         platform_set_drvdata(eeepc->platform_device, eeepc);
1287
1288         eeepc->hotplug_wireless = hotplug_wireless;
1289         eeepc_dmi_check(eeepc);
1290
1291         err = eeepc_wmi_platform_init(eeepc);
1292         if (err)
1293                 goto fail_platform;
1294
1295         err = eeepc_wmi_input_init(eeepc);
1296         if (err)
1297                 goto fail_input;
1298
1299         err = eeepc_wmi_led_init(eeepc);
1300         if (err)
1301                 goto fail_leds;
1302
1303         err = eeepc_wmi_rfkill_init(eeepc);
1304         if (err)
1305                 goto fail_rfkill;
1306
1307         if (!acpi_video_backlight_support()) {
1308                 err = eeepc_wmi_backlight_init(eeepc);
1309                 if (err && err != -ENODEV)
1310                         goto fail_backlight;
1311         } else
1312                 pr_info("Backlight controlled by ACPI video driver\n");
1313
1314         status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
1315                                             eeepc_wmi_notify, eeepc);
1316         if (ACPI_FAILURE(status)) {
1317                 pr_err("Unable to register notify handler - %d\n",
1318                         status);
1319                 err = -ENODEV;
1320                 goto fail_wmi_handler;
1321         }
1322
1323         err = eeepc_wmi_debugfs_init(eeepc);
1324         if (err)
1325                 goto fail_debugfs;
1326
1327         return 0;
1328
1329 fail_debugfs:
1330         wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
1331 fail_wmi_handler:
1332         eeepc_wmi_backlight_exit(eeepc);
1333 fail_backlight:
1334         eeepc_wmi_rfkill_exit(eeepc);
1335 fail_rfkill:
1336         eeepc_wmi_led_exit(eeepc);
1337 fail_leds:
1338         eeepc_wmi_input_exit(eeepc);
1339 fail_input:
1340         eeepc_wmi_platform_exit(eeepc);
1341 fail_platform:
1342         kfree(eeepc);
1343         return err;
1344 }
1345
1346 static int __exit eeepc_wmi_remove(struct platform_device *device)
1347 {
1348         struct eeepc_wmi *eeepc;
1349
1350         eeepc = platform_get_drvdata(device);
1351         wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
1352         eeepc_wmi_backlight_exit(eeepc);
1353         eeepc_wmi_input_exit(eeepc);
1354         eeepc_wmi_led_exit(eeepc);
1355         eeepc_wmi_rfkill_exit(eeepc);
1356         eeepc_wmi_debugfs_exit(eeepc);
1357         eeepc_wmi_platform_exit(eeepc);
1358
1359         kfree(eeepc);
1360         return 0;
1361 }
1362
1363 /*
1364  * Platform driver - hibernate/resume callbacks
1365  */
1366 static int eeepc_hotk_thaw(struct device *device)
1367 {
1368         struct eeepc_wmi *eeepc = dev_get_drvdata(device);
1369
1370         if (eeepc->wlan_rfkill) {
1371                 bool wlan;
1372
1373                 /*
1374                  * Work around bios bug - acpi _PTS turns off the wireless led
1375                  * during suspend.  Normally it restores it on resume, but
1376                  * we should kick it ourselves in case hibernation is aborted.
1377                  */
1378                 wlan = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
1379                 eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_WLAN, wlan, NULL);
1380         }
1381
1382         return 0;
1383 }
1384
1385 static int eeepc_hotk_restore(struct device *device)
1386 {
1387         struct eeepc_wmi *eeepc = dev_get_drvdata(device);
1388         int bl;
1389
1390         /* Refresh both wlan rfkill state and pci hotplug */
1391         if (eeepc->wlan_rfkill)
1392                 eeepc_rfkill_hotplug(eeepc);
1393
1394         if (eeepc->bluetooth_rfkill) {
1395                 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_BLUETOOTH);
1396                 rfkill_set_sw_state(eeepc->bluetooth_rfkill, bl);
1397         }
1398         if (eeepc->wimax_rfkill) {
1399                 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WIMAX);
1400                 rfkill_set_sw_state(eeepc->wimax_rfkill, bl);
1401         }
1402         if (eeepc->wwan3g_rfkill) {
1403                 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WWAN3G);
1404                 rfkill_set_sw_state(eeepc->wwan3g_rfkill, bl);
1405         }
1406
1407         return 0;
1408 }
1409
1410 static const struct dev_pm_ops eeepc_pm_ops = {
1411         .thaw = eeepc_hotk_thaw,
1412         .restore = eeepc_hotk_restore,
1413 };
1414
1415 static struct platform_driver platform_driver = {
1416         .remove = __exit_p(eeepc_wmi_remove),
1417         .driver = {
1418                 .name = EEEPC_WMI_FILE,
1419                 .owner = THIS_MODULE,
1420                 .pm = &eeepc_pm_ops,
1421         },
1422 };
1423
1424 static acpi_status __init eeepc_wmi_parse_device(acpi_handle handle, u32 level,
1425                                                  void *context, void **retval)
1426 {
1427         pr_warning("Found legacy ATKD device (%s)", EEEPC_ACPI_HID);
1428         *(bool *)context = true;
1429         return AE_CTRL_TERMINATE;
1430 }
1431
1432 static int __init eeepc_wmi_check_atkd(void)
1433 {
1434         acpi_status status;
1435         bool found = false;
1436
1437         status = acpi_get_devices(EEEPC_ACPI_HID, eeepc_wmi_parse_device,
1438                                   &found, NULL);
1439
1440         if (ACPI_FAILURE(status) || !found)
1441                 return 0;
1442         return -1;
1443 }
1444
1445 static int __init eeepc_wmi_probe(struct platform_device *pdev)
1446 {
1447         if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
1448             !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
1449                 pr_warning("No known WMI GUID found\n");
1450                 return -ENODEV;
1451         }
1452
1453         if (eeepc_wmi_check_atkd()) {
1454                 pr_warning("WMI device present, but legacy ATKD device is also "
1455                            "present and enabled.");
1456                 pr_warning("You probably booted with acpi_osi=\"Linux\" or "
1457                            "acpi_osi=\"!Windows 2009\"");
1458                 pr_warning("Can't load eeepc-wmi, use default acpi_osi "
1459                            "(preferred) or eeepc-laptop");
1460                 return -ENODEV;
1461         }
1462
1463         return eeepc_wmi_add(pdev);
1464 }
1465
1466 static struct platform_device *platform_device;
1467
1468 static int __init eeepc_wmi_init(void)
1469 {
1470         platform_device = platform_create_bundle(&platform_driver,
1471                                                  eeepc_wmi_probe,
1472                                                  NULL, 0, NULL, 0);
1473         if (IS_ERR(platform_device))
1474                 return PTR_ERR(platform_device);
1475         return 0;
1476 }
1477
1478 static void __exit eeepc_wmi_exit(void)
1479 {
1480         platform_device_unregister(platform_device);
1481         platform_driver_unregister(&platform_driver);
1482 }
1483
1484 module_init(eeepc_wmi_init);
1485 module_exit(eeepc_wmi_exit);