2 * Eee PC WMI hotkey driver
4 * Copyright(C) 2010 Intel Corporation.
5 * Copyright(C) 2010 Corentin Chary <corentin.chary@gmail.com>
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>
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.
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.
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
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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>
49 #define EEEPC_WMI_FILE "eeepc-wmi"
51 MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
52 MODULE_DESCRIPTION("Eee PC WMI Hotkey Driver");
53 MODULE_LICENSE("GPL");
55 #define EEEPC_ACPI_HID "ASUS010" /* old _HID used in eeepc-laptop */
57 #define EEEPC_WMI_EVENT_GUID "ABBC0F72-8EA1-11D1-00A0-C90629100000"
58 #define EEEPC_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
60 MODULE_ALIAS("wmi:"EEEPC_WMI_EVENT_GUID);
61 MODULE_ALIAS("wmi:"EEEPC_WMI_MGMT_GUID);
63 #define NOTIFY_BRNUP_MIN 0x11
64 #define NOTIFY_BRNUP_MAX 0x1f
65 #define NOTIFY_BRNDOWN_MIN 0x20
66 #define NOTIFY_BRNDOWN_MAX 0x2e
68 #define EEEPC_WMI_METHODID_DSTS 0x53544344
69 #define EEEPC_WMI_METHODID_DEVS 0x53564544
70 #define EEEPC_WMI_METHODID_CFVS 0x53564643
72 #define EEEPC_WMI_DEVID_WLAN 0x00010011
73 #define EEEPC_WMI_DEVID_BLUETOOTH 0x00010013
74 #define EEEPC_WMI_DEVID_WWAN3G 0x00010019
75 #define EEEPC_WMI_DEVID_BACKLIGHT 0x00050012
76 #define EEEPC_WMI_DEVID_TPDLED 0x00100011
78 #define EEEPC_WMI_DSTS_STATUS_BIT 0x00000001
79 #define EEEPC_WMI_DSTS_PRESENCE_BIT 0x00010000
81 static bool hotplug_wireless;
83 module_param(hotplug_wireless, bool, 0444);
84 MODULE_PARM_DESC(hotplug_wireless,
85 "Enable hotplug for wireless device. "
86 "If your laptop needs that, please report to "
87 "acpi4asus-user@lists.sourceforge.net.");
89 static const struct key_entry eeepc_wmi_keymap[] = {
90 /* Sleep already handled via generic ACPI code */
91 { KE_IGNORE, NOTIFY_BRNDOWN_MIN, { KEY_BRIGHTNESSDOWN } },
92 { KE_IGNORE, NOTIFY_BRNUP_MIN, { KEY_BRIGHTNESSUP } },
93 { KE_KEY, 0x30, { KEY_VOLUMEUP } },
94 { KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
95 { KE_KEY, 0x32, { KEY_MUTE } },
96 { KE_KEY, 0x5c, { KEY_F15 } },
97 { KE_KEY, 0x5d, { KEY_WLAN } },
98 { KE_KEY, 0x6b, { KEY_F13 } }, /* Disable Touchpad */
99 { KE_KEY, 0x88, { KEY_WLAN } },
100 { KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } },
101 { KE_KEY, 0xe0, { KEY_PROG1 } },
102 { KE_KEY, 0xe1, { KEY_F14 } },
103 { KE_KEY, 0xe9, { KEY_DISPLAY_OFF } },
113 * eeepc-wmi/ - debugfs root directory
114 * dev_id - current dev_id
115 * ctrl_param - current ctrl_param
116 * devs - call DEVS(dev_id, ctrl_param) and print result
117 * dsts - call DSTS(dev_id) and print result
119 struct eeepc_wmi_debug {
126 bool hotplug_wireless;
128 struct input_dev *inputdev;
129 struct backlight_device *backlight_device;
130 struct platform_device *platform_device;
132 struct led_classdev tpd_led;
134 struct workqueue_struct *led_workqueue;
135 struct work_struct tpd_led_work;
137 struct rfkill *wlan_rfkill;
138 struct rfkill *bluetooth_rfkill;
139 struct rfkill *wwan3g_rfkill;
141 struct hotplug_slot *hotplug_slot;
142 struct mutex hotplug_lock;
143 struct mutex wmi_lock;
144 struct workqueue_struct *hotplug_workqueue;
145 struct work_struct hotplug_work;
147 struct eeepc_wmi_debug debug;
150 static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc)
154 eeepc->inputdev = input_allocate_device();
155 if (!eeepc->inputdev)
158 eeepc->inputdev->name = "Eee PC WMI hotkeys";
159 eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0";
160 eeepc->inputdev->id.bustype = BUS_HOST;
161 eeepc->inputdev->dev.parent = &eeepc->platform_device->dev;
163 err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL);
167 err = input_register_device(eeepc->inputdev);
169 goto err_free_keymap;
174 sparse_keymap_free(eeepc->inputdev);
176 input_free_device(eeepc->inputdev);
180 static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc)
182 if (eeepc->inputdev) {
183 sparse_keymap_free(eeepc->inputdev);
184 input_unregister_device(eeepc->inputdev);
187 eeepc->inputdev = NULL;
190 static acpi_status eeepc_wmi_get_devstate(u32 dev_id, u32 *retval)
192 struct acpi_buffer input = { (acpi_size)sizeof(u32), &dev_id };
193 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
194 union acpi_object *obj;
198 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
199 1, EEEPC_WMI_METHODID_DSTS,
202 if (ACPI_FAILURE(status))
205 obj = (union acpi_object *)output.pointer;
206 if (obj && obj->type == ACPI_TYPE_INTEGER)
207 tmp = (u32)obj->integer.value;
220 static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
223 struct bios_args args = {
225 .ctrl_param = ctrl_param,
227 struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
231 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
232 EEEPC_WMI_METHODID_DEVS,
235 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
236 union acpi_object *obj;
239 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
240 EEEPC_WMI_METHODID_DEVS,
243 if (ACPI_FAILURE(status))
246 obj = (union acpi_object *)output.pointer;
247 if (obj && obj->type == ACPI_TYPE_INTEGER)
248 tmp = (u32)obj->integer.value;
260 /* Helper for special devices with magic return codes */
261 static int eeepc_wmi_get_devstate_simple(u32 dev_id)
266 status = eeepc_wmi_get_devstate(dev_id, &retval);
268 if (ACPI_FAILURE(status))
271 if (!(retval & EEEPC_WMI_DSTS_PRESENCE_BIT))
274 return retval & EEEPC_WMI_DSTS_STATUS_BIT;
281 * These functions actually update the LED's, and are called from a
282 * workqueue. By doing this as separate work rather than when the LED
283 * subsystem asks, we avoid messing with the Eeepc ACPI stuff during a
284 * potentially bad time, such as a timer interrupt.
286 static void tpd_led_update(struct work_struct *work)
289 struct eeepc_wmi *eeepc;
291 eeepc = container_of(work, struct eeepc_wmi, tpd_led_work);
293 ctrl_param = eeepc->tpd_led_wk;
294 eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_TPDLED, ctrl_param, NULL);
297 static void tpd_led_set(struct led_classdev *led_cdev,
298 enum led_brightness value)
300 struct eeepc_wmi *eeepc;
302 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
304 eeepc->tpd_led_wk = !!value;
305 queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
308 static int read_tpd_state(struct eeepc_wmi *eeepc)
310 return eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_TPDLED);
313 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
315 struct eeepc_wmi *eeepc;
317 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
319 return read_tpd_state(eeepc);
322 static int eeepc_wmi_led_init(struct eeepc_wmi *eeepc)
326 if (read_tpd_state(eeepc) < 0)
329 eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
330 if (!eeepc->led_workqueue)
332 INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
334 eeepc->tpd_led.name = "eeepc::touchpad";
335 eeepc->tpd_led.brightness_set = tpd_led_set;
336 eeepc->tpd_led.brightness_get = tpd_led_get;
337 eeepc->tpd_led.max_brightness = 1;
339 rv = led_classdev_register(&eeepc->platform_device->dev,
342 destroy_workqueue(eeepc->led_workqueue);
349 static void eeepc_wmi_led_exit(struct eeepc_wmi *eeepc)
351 if (eeepc->tpd_led.dev)
352 led_classdev_unregister(&eeepc->tpd_led);
353 if (eeepc->led_workqueue)
354 destroy_workqueue(eeepc->led_workqueue);
358 * PCI hotplug (for wlan rfkill)
360 static bool eeepc_wlan_rfkill_blocked(struct eeepc_wmi *eeepc)
362 int result = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
369 static void eeepc_rfkill_hotplug(struct eeepc_wmi *eeepc)
377 mutex_lock(&eeepc->wmi_lock);
378 blocked = eeepc_wlan_rfkill_blocked(eeepc);
379 mutex_unlock(&eeepc->wmi_lock);
381 mutex_lock(&eeepc->hotplug_lock);
383 if (eeepc->wlan_rfkill)
384 rfkill_set_sw_state(eeepc->wlan_rfkill, blocked);
386 if (eeepc->hotplug_slot) {
387 bus = pci_find_bus(0, 1);
389 pr_warning("Unable to find PCI bus 1?\n");
393 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
394 pr_err("Unable to read PCI config space?\n");
397 absent = (l == 0xffffffff);
399 if (blocked != absent) {
400 pr_warning("BIOS says wireless lan is %s, "
401 "but the pci device is %s\n",
402 blocked ? "blocked" : "unblocked",
403 absent ? "absent" : "present");
404 pr_warning("skipped wireless hotplug as probably "
405 "inappropriate for this model\n");
410 dev = pci_get_slot(bus, 0);
412 /* Device already present */
416 dev = pci_scan_single_device(bus, 0);
418 pci_bus_assign_resources(bus);
419 if (pci_bus_add_device(dev))
420 pr_err("Unable to hotplug wifi\n");
423 dev = pci_get_slot(bus, 0);
425 pci_remove_bus_device(dev);
432 mutex_unlock(&eeepc->hotplug_lock);
435 static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
437 struct eeepc_wmi *eeepc = data;
439 if (event != ACPI_NOTIFY_BUS_CHECK)
443 * We can't call directly eeepc_rfkill_hotplug because most
444 * of the time WMBC is still being executed and not reetrant.
445 * There is currently no way to tell ACPICA that we want this
446 * method to be serialized, we schedule a eeepc_rfkill_hotplug
447 * call later, in a safer context.
449 queue_work(eeepc->hotplug_workqueue, &eeepc->hotplug_work);
452 static int eeepc_register_rfkill_notifier(struct eeepc_wmi *eeepc,
458 status = acpi_get_handle(NULL, node, &handle);
460 if (ACPI_SUCCESS(status)) {
461 status = acpi_install_notify_handler(handle,
465 if (ACPI_FAILURE(status))
466 pr_warning("Failed to register notify on %s\n", node);
473 static void eeepc_unregister_rfkill_notifier(struct eeepc_wmi *eeepc,
476 acpi_status status = AE_OK;
479 status = acpi_get_handle(NULL, node, &handle);
481 if (ACPI_SUCCESS(status)) {
482 status = acpi_remove_notify_handler(handle,
484 eeepc_rfkill_notify);
485 if (ACPI_FAILURE(status))
486 pr_err("Error removing rfkill notify handler %s\n",
491 static int eeepc_get_adapter_status(struct hotplug_slot *hotplug_slot,
494 int result = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
503 static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
505 kfree(hotplug_slot->info);
509 static struct hotplug_slot_ops eeepc_hotplug_slot_ops = {
510 .owner = THIS_MODULE,
511 .get_adapter_status = eeepc_get_adapter_status,
512 .get_power_status = eeepc_get_adapter_status,
515 static void eeepc_hotplug_work(struct work_struct *work)
517 struct eeepc_wmi *eeepc;
519 eeepc = container_of(work, struct eeepc_wmi, hotplug_work);
520 eeepc_rfkill_hotplug(eeepc);
523 static int eeepc_setup_pci_hotplug(struct eeepc_wmi *eeepc)
526 struct pci_bus *bus = pci_find_bus(0, 1);
529 pr_err("Unable to find wifi PCI bus\n");
533 eeepc->hotplug_workqueue =
534 create_singlethread_workqueue("hotplug_workqueue");
535 if (!eeepc->hotplug_workqueue)
536 goto error_workqueue;
538 INIT_WORK(&eeepc->hotplug_work, eeepc_hotplug_work);
540 eeepc->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
541 if (!eeepc->hotplug_slot)
544 eeepc->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
546 if (!eeepc->hotplug_slot->info)
549 eeepc->hotplug_slot->private = eeepc;
550 eeepc->hotplug_slot->release = &eeepc_cleanup_pci_hotplug;
551 eeepc->hotplug_slot->ops = &eeepc_hotplug_slot_ops;
552 eeepc_get_adapter_status(eeepc->hotplug_slot,
553 &eeepc->hotplug_slot->info->adapter_status);
555 ret = pci_hp_register(eeepc->hotplug_slot, bus, 0, "eeepc-wifi");
557 pr_err("Unable to register hotplug slot - %d\n", ret);
564 kfree(eeepc->hotplug_slot->info);
566 kfree(eeepc->hotplug_slot);
567 eeepc->hotplug_slot = NULL;
569 destroy_workqueue(eeepc->hotplug_workqueue);
577 static int eeepc_rfkill_set(void *data, bool blocked)
579 int dev_id = (unsigned long)data;
580 u32 ctrl_param = !blocked;
583 status = eeepc_wmi_set_devstate(dev_id, ctrl_param, NULL);
585 if (ACPI_FAILURE(status))
591 static void eeepc_rfkill_query(struct rfkill *rfkill, void *data)
593 int dev_id = (unsigned long)data;
596 result = eeepc_wmi_get_devstate_simple(dev_id);
601 rfkill_set_sw_state(rfkill, !result);
604 static int eeepc_rfkill_wlan_set(void *data, bool blocked)
606 struct eeepc_wmi *eeepc = data;
610 * This handler is enabled only if hotplug is enabled.
611 * In this case, the eeepc_wmi_set_devstate() will
612 * trigger a wmi notification and we need to wait
613 * this call to finish before being able to call
616 mutex_lock(&eeepc->wmi_lock);
617 ret = eeepc_rfkill_set((void *)(long)EEEPC_WMI_DEVID_WLAN, blocked);
618 mutex_unlock(&eeepc->wmi_lock);
622 static void eeepc_rfkill_wlan_query(struct rfkill *rfkill, void *data)
624 eeepc_rfkill_query(rfkill, (void *)(long)EEEPC_WMI_DEVID_WLAN);
627 static const struct rfkill_ops eeepc_rfkill_wlan_ops = {
628 .set_block = eeepc_rfkill_wlan_set,
629 .query = eeepc_rfkill_wlan_query,
632 static const struct rfkill_ops eeepc_rfkill_ops = {
633 .set_block = eeepc_rfkill_set,
634 .query = eeepc_rfkill_query,
637 static int eeepc_new_rfkill(struct eeepc_wmi *eeepc,
638 struct rfkill **rfkill,
640 enum rfkill_type type, int dev_id)
642 int result = eeepc_wmi_get_devstate_simple(dev_id);
647 if (dev_id == EEEPC_WMI_DEVID_WLAN && eeepc->hotplug_wireless)
648 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
649 &eeepc_rfkill_wlan_ops, eeepc);
651 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
652 &eeepc_rfkill_ops, (void *)(long)dev_id);
657 rfkill_init_sw_state(*rfkill, !result);
658 result = rfkill_register(*rfkill);
660 rfkill_destroy(*rfkill);
667 static void eeepc_wmi_rfkill_exit(struct eeepc_wmi *eeepc)
669 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
670 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
671 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
672 if (eeepc->wlan_rfkill) {
673 rfkill_unregister(eeepc->wlan_rfkill);
674 rfkill_destroy(eeepc->wlan_rfkill);
675 eeepc->wlan_rfkill = NULL;
678 * Refresh pci hotplug in case the rfkill state was changed after
679 * eeepc_unregister_rfkill_notifier()
681 eeepc_rfkill_hotplug(eeepc);
682 if (eeepc->hotplug_slot)
683 pci_hp_deregister(eeepc->hotplug_slot);
684 if (eeepc->hotplug_workqueue)
685 destroy_workqueue(eeepc->hotplug_workqueue);
687 if (eeepc->bluetooth_rfkill) {
688 rfkill_unregister(eeepc->bluetooth_rfkill);
689 rfkill_destroy(eeepc->bluetooth_rfkill);
690 eeepc->bluetooth_rfkill = NULL;
692 if (eeepc->wwan3g_rfkill) {
693 rfkill_unregister(eeepc->wwan3g_rfkill);
694 rfkill_destroy(eeepc->wwan3g_rfkill);
695 eeepc->wwan3g_rfkill = NULL;
699 static int eeepc_wmi_rfkill_init(struct eeepc_wmi *eeepc)
703 mutex_init(&eeepc->hotplug_lock);
704 mutex_init(&eeepc->wmi_lock);
706 result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
707 "eeepc-wlan", RFKILL_TYPE_WLAN,
708 EEEPC_WMI_DEVID_WLAN);
710 if (result && result != -ENODEV)
713 result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
714 "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
715 EEEPC_WMI_DEVID_BLUETOOTH);
717 if (result && result != -ENODEV)
720 result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
721 "eeepc-wwan3g", RFKILL_TYPE_WWAN,
722 EEEPC_WMI_DEVID_WWAN3G);
724 if (result && result != -ENODEV)
727 result = eeepc_setup_pci_hotplug(eeepc);
729 * If we get -EBUSY then something else is handling the PCI hotplug -
730 * don't fail in this case
732 if (result == -EBUSY)
735 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
736 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
737 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
739 * Refresh pci hotplug in case the rfkill state was changed during
742 eeepc_rfkill_hotplug(eeepc);
745 if (result && result != -ENODEV)
746 eeepc_wmi_rfkill_exit(eeepc);
748 if (result == -ENODEV)
757 static int read_brightness(struct backlight_device *bd)
762 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BACKLIGHT, &retval);
764 if (ACPI_FAILURE(status))
767 return retval & 0xFF;
770 static int update_bl_status(struct backlight_device *bd)
776 ctrl_param = bd->props.brightness;
778 status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT,
781 if (ACPI_FAILURE(status))
787 static const struct backlight_ops eeepc_wmi_bl_ops = {
788 .get_brightness = read_brightness,
789 .update_status = update_bl_status,
792 static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
794 struct backlight_device *bd = eeepc->backlight_device;
795 int old = bd->props.brightness;
798 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
799 new = code - NOTIFY_BRNUP_MIN + 1;
800 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
801 new = code - NOTIFY_BRNDOWN_MIN;
803 bd->props.brightness = new;
804 backlight_update_status(bd);
805 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
810 static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
812 struct backlight_device *bd;
813 struct backlight_properties props;
815 memset(&props, 0, sizeof(struct backlight_properties));
816 props.max_brightness = 15;
817 bd = backlight_device_register(EEEPC_WMI_FILE,
818 &eeepc->platform_device->dev, eeepc,
819 &eeepc_wmi_bl_ops, &props);
821 pr_err("Could not register backlight device\n");
825 eeepc->backlight_device = bd;
827 bd->props.brightness = read_brightness(bd);
828 bd->props.power = FB_BLANK_UNBLANK;
829 backlight_update_status(bd);
834 static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
836 if (eeepc->backlight_device)
837 backlight_device_unregister(eeepc->backlight_device);
839 eeepc->backlight_device = NULL;
842 static void eeepc_wmi_notify(u32 value, void *context)
844 struct eeepc_wmi *eeepc = context;
845 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
846 union acpi_object *obj;
851 status = wmi_get_event_data(value, &response);
852 if (status != AE_OK) {
853 pr_err("bad event status 0x%x\n", status);
857 obj = (union acpi_object *)response.pointer;
859 if (obj && obj->type == ACPI_TYPE_INTEGER) {
860 code = obj->integer.value;
863 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
864 code = NOTIFY_BRNUP_MIN;
865 else if (code >= NOTIFY_BRNDOWN_MIN &&
866 code <= NOTIFY_BRNDOWN_MAX)
867 code = NOTIFY_BRNDOWN_MIN;
869 if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
870 if (!acpi_video_backlight_support())
871 eeepc_wmi_backlight_notify(eeepc, orig_code);
874 if (!sparse_keymap_report_event(eeepc->inputdev,
876 pr_info("Unknown key %x pressed\n", code);
882 static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
883 const char *buf, size_t count)
886 struct acpi_buffer input = { (acpi_size)sizeof(value), &value };
889 if (!count || sscanf(buf, "%i", &value) != 1)
891 if (value < 0 || value > 2)
894 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
895 1, EEEPC_WMI_METHODID_CFVS, &input, NULL);
897 if (ACPI_FAILURE(status))
903 static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
905 static struct attribute *platform_attributes[] = {
906 &dev_attr_cpufv.attr,
910 static struct attribute_group platform_attribute_group = {
911 .attrs = platform_attributes
914 static void eeepc_wmi_sysfs_exit(struct platform_device *device)
916 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
919 static int eeepc_wmi_sysfs_init(struct platform_device *device)
921 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
927 static int __init eeepc_wmi_platform_init(struct eeepc_wmi *eeepc)
929 return eeepc_wmi_sysfs_init(eeepc->platform_device);
932 static void eeepc_wmi_platform_exit(struct eeepc_wmi *eeepc)
934 eeepc_wmi_sysfs_exit(eeepc->platform_device);
940 struct eeepc_wmi_debugfs_node {
941 struct eeepc_wmi *eeepc;
943 int (*show)(struct seq_file *m, void *data);
946 static int show_dsts(struct seq_file *m, void *data)
948 struct eeepc_wmi *eeepc = m->private;
952 status = eeepc_wmi_get_devstate(eeepc->debug.dev_id, &retval);
954 if (ACPI_FAILURE(status))
957 seq_printf(m, "DSTS(%x) = %x\n", eeepc->debug.dev_id, retval);
962 static int show_devs(struct seq_file *m, void *data)
964 struct eeepc_wmi *eeepc = m->private;
968 status = eeepc_wmi_set_devstate(eeepc->debug.dev_id,
969 eeepc->debug.ctrl_param, &retval);
970 if (ACPI_FAILURE(status))
973 seq_printf(m, "DEVS(%x, %x) = %x\n", eeepc->debug.dev_id,
974 eeepc->debug.ctrl_param, retval);
979 static struct eeepc_wmi_debugfs_node eeepc_wmi_debug_files[] = {
980 { NULL, "devs", show_devs },
981 { NULL, "dsts", show_dsts },
984 static int eeepc_wmi_debugfs_open(struct inode *inode, struct file *file)
986 struct eeepc_wmi_debugfs_node *node = inode->i_private;
988 return single_open(file, node->show, node->eeepc);
991 static const struct file_operations eeepc_wmi_debugfs_io_ops = {
992 .owner = THIS_MODULE,
993 .open = eeepc_wmi_debugfs_open,
996 .release = single_release,
999 static void eeepc_wmi_debugfs_exit(struct eeepc_wmi *eeepc)
1001 debugfs_remove_recursive(eeepc->debug.root);
1004 static int eeepc_wmi_debugfs_init(struct eeepc_wmi *eeepc)
1006 struct dentry *dent;
1009 eeepc->debug.root = debugfs_create_dir(EEEPC_WMI_FILE, NULL);
1010 if (!eeepc->debug.root) {
1011 pr_err("failed to create debugfs directory");
1015 dent = debugfs_create_x32("dev_id", S_IRUGO|S_IWUSR,
1016 eeepc->debug.root, &eeepc->debug.dev_id);
1020 dent = debugfs_create_x32("ctrl_param", S_IRUGO|S_IWUSR,
1021 eeepc->debug.root, &eeepc->debug.ctrl_param);
1025 for (i = 0; i < ARRAY_SIZE(eeepc_wmi_debug_files); i++) {
1026 struct eeepc_wmi_debugfs_node *node = &eeepc_wmi_debug_files[i];
1028 node->eeepc = eeepc;
1029 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
1030 eeepc->debug.root, node,
1031 &eeepc_wmi_debugfs_io_ops);
1033 pr_err("failed to create debug file: %s\n", node->name);
1041 eeepc_wmi_debugfs_exit(eeepc);
1048 static void eeepc_dmi_check(struct eeepc_wmi *eeepc)
1052 model = dmi_get_system_info(DMI_PRODUCT_NAME);
1057 * Whitelist for wlan hotplug
1059 * Eeepc 1000H needs the current hotplug code to handle
1060 * Fn+F2 correctly. We may add other Eeepc here later, but
1061 * it seems that most of the laptops supported by eeepc-wmi
1062 * don't need to be on this list
1064 if (strcmp(model, "1000H") == 0) {
1065 eeepc->hotplug_wireless = true;
1066 pr_info("wlan hotplug enabled\n");
1070 static int __init eeepc_wmi_add(struct platform_device *pdev)
1072 struct eeepc_wmi *eeepc;
1076 eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
1080 eeepc->platform_device = pdev;
1081 platform_set_drvdata(eeepc->platform_device, eeepc);
1083 eeepc->hotplug_wireless = hotplug_wireless;
1084 eeepc_dmi_check(eeepc);
1086 err = eeepc_wmi_platform_init(eeepc);
1090 err = eeepc_wmi_input_init(eeepc);
1094 err = eeepc_wmi_led_init(eeepc);
1098 err = eeepc_wmi_rfkill_init(eeepc);
1102 if (!acpi_video_backlight_support()) {
1103 err = eeepc_wmi_backlight_init(eeepc);
1105 goto fail_backlight;
1107 pr_info("Backlight controlled by ACPI video driver\n");
1109 status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
1110 eeepc_wmi_notify, eeepc);
1111 if (ACPI_FAILURE(status)) {
1112 pr_err("Unable to register notify handler - %d\n",
1115 goto fail_wmi_handler;
1118 err = eeepc_wmi_debugfs_init(eeepc);
1125 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
1127 eeepc_wmi_backlight_exit(eeepc);
1129 eeepc_wmi_rfkill_exit(eeepc);
1131 eeepc_wmi_led_exit(eeepc);
1133 eeepc_wmi_input_exit(eeepc);
1135 eeepc_wmi_platform_exit(eeepc);
1141 static int __exit eeepc_wmi_remove(struct platform_device *device)
1143 struct eeepc_wmi *eeepc;
1145 eeepc = platform_get_drvdata(device);
1146 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
1147 eeepc_wmi_backlight_exit(eeepc);
1148 eeepc_wmi_input_exit(eeepc);
1149 eeepc_wmi_led_exit(eeepc);
1150 eeepc_wmi_rfkill_exit(eeepc);
1151 eeepc_wmi_debugfs_exit(eeepc);
1152 eeepc_wmi_platform_exit(eeepc);
1159 * Platform driver - hibernate/resume callbacks
1161 static int eeepc_hotk_thaw(struct device *device)
1163 struct eeepc_wmi *eeepc = dev_get_drvdata(device);
1165 if (eeepc->wlan_rfkill) {
1169 * Work around bios bug - acpi _PTS turns off the wireless led
1170 * during suspend. Normally it restores it on resume, but
1171 * we should kick it ourselves in case hibernation is aborted.
1173 wlan = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
1174 eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_WLAN, wlan, NULL);
1180 static int eeepc_hotk_restore(struct device *device)
1182 struct eeepc_wmi *eeepc = dev_get_drvdata(device);
1185 /* Refresh both wlan rfkill state and pci hotplug */
1186 if (eeepc->wlan_rfkill)
1187 eeepc_rfkill_hotplug(eeepc);
1189 if (eeepc->bluetooth_rfkill) {
1190 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_BLUETOOTH);
1191 rfkill_set_sw_state(eeepc->bluetooth_rfkill, bl);
1193 if (eeepc->wwan3g_rfkill) {
1194 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WWAN3G);
1195 rfkill_set_sw_state(eeepc->wwan3g_rfkill, bl);
1201 static const struct dev_pm_ops eeepc_pm_ops = {
1202 .thaw = eeepc_hotk_thaw,
1203 .restore = eeepc_hotk_restore,
1206 static struct platform_driver platform_driver = {
1207 .remove = __exit_p(eeepc_wmi_remove),
1209 .name = EEEPC_WMI_FILE,
1210 .owner = THIS_MODULE,
1211 .pm = &eeepc_pm_ops,
1215 static acpi_status __init eeepc_wmi_parse_device(acpi_handle handle, u32 level,
1216 void *context, void **retval)
1218 pr_warning("Found legacy ATKD device (%s)", EEEPC_ACPI_HID);
1219 *(bool *)context = true;
1220 return AE_CTRL_TERMINATE;
1223 static int __init eeepc_wmi_check_atkd(void)
1228 status = acpi_get_devices(EEEPC_ACPI_HID, eeepc_wmi_parse_device,
1231 if (ACPI_FAILURE(status) || !found)
1236 static int __init eeepc_wmi_probe(struct platform_device *pdev)
1238 if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
1239 !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
1240 pr_warning("No known WMI GUID found\n");
1244 if (eeepc_wmi_check_atkd()) {
1245 pr_warning("WMI device present, but legacy ATKD device is also "
1246 "present and enabled.");
1247 pr_warning("You probably booted with acpi_osi=\"Linux\" or "
1248 "acpi_osi=\"!Windows 2009\"");
1249 pr_warning("Can't load eeepc-wmi, use default acpi_osi "
1250 "(preferred) or eeepc-laptop");
1254 return eeepc_wmi_add(pdev);
1257 static struct platform_device *platform_device;
1259 static int __init eeepc_wmi_init(void)
1261 platform_device = platform_create_bundle(&platform_driver,
1264 if (IS_ERR(platform_device))
1265 return PTR_ERR(platform_device);
1269 static void __exit eeepc_wmi_exit(void)
1271 platform_device_unregister(platform_device);
1272 platform_driver_unregister(&platform_driver);
1275 module_init(eeepc_wmi_init);
1276 module_exit(eeepc_wmi_exit);