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_DEVS 0x53564544
69 #define EEEPC_WMI_METHODID_DSTS 0x53544344
70 #define EEEPC_WMI_METHODID_CFVS 0x53564643
72 #define EEEPC_WMI_DEVID_BACKLIGHT 0x00050012
73 #define EEEPC_WMI_DEVID_TPDLED 0x00100011
74 #define EEEPC_WMI_DEVID_WLAN 0x00010011
75 #define EEEPC_WMI_DEVID_BLUETOOTH 0x00010013
76 #define EEEPC_WMI_DEVID_WWAN3G 0x00010019
78 static bool hotplug_wireless;
80 module_param(hotplug_wireless, bool, 0444);
81 MODULE_PARM_DESC(hotplug_wireless,
82 "Enable hotplug for wireless device. "
83 "If your laptop needs that, please report to "
84 "acpi4asus-user@lists.sourceforge.net.");
86 static const struct key_entry eeepc_wmi_keymap[] = {
87 /* Sleep already handled via generic ACPI code */
88 { KE_IGNORE, NOTIFY_BRNDOWN_MIN, { KEY_BRIGHTNESSDOWN } },
89 { KE_IGNORE, NOTIFY_BRNUP_MIN, { KEY_BRIGHTNESSUP } },
90 { KE_KEY, 0x30, { KEY_VOLUMEUP } },
91 { KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
92 { KE_KEY, 0x32, { KEY_MUTE } },
93 { KE_KEY, 0x5c, { KEY_F15 } },
94 { KE_KEY, 0x5d, { KEY_WLAN } },
95 { KE_KEY, 0x6b, { KEY_F13 } }, /* Disable Touchpad */
96 { KE_KEY, 0x88, { KEY_WLAN } },
97 { KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } },
98 { KE_KEY, 0xe0, { KEY_PROG1 } },
99 { KE_KEY, 0xe1, { KEY_F14 } },
100 { KE_KEY, 0xe9, { KEY_DISPLAY_OFF } },
110 * eeepc-wmi/ - debugfs root directory
111 * dev_id - current dev_id
112 * ctrl_param - current ctrl_param
113 * devs - call DEVS(dev_id, ctrl_param) and print result
114 * dsts - call DSTS(dev_id) and print result
116 struct eeepc_wmi_debug {
123 bool hotplug_wireless;
125 struct input_dev *inputdev;
126 struct backlight_device *backlight_device;
127 struct platform_device *platform_device;
129 struct led_classdev tpd_led;
131 struct workqueue_struct *led_workqueue;
132 struct work_struct tpd_led_work;
134 struct rfkill *wlan_rfkill;
135 struct rfkill *bluetooth_rfkill;
136 struct rfkill *wwan3g_rfkill;
138 struct hotplug_slot *hotplug_slot;
139 struct mutex hotplug_lock;
140 struct mutex wmi_lock;
141 struct workqueue_struct *hotplug_workqueue;
142 struct work_struct hotplug_work;
144 struct eeepc_wmi_debug debug;
147 /* Only used in eeepc_wmi_init() and eeepc_wmi_exit() */
148 static struct platform_device *platform_device;
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;
264 * These functions actually update the LED's, and are called from a
265 * workqueue. By doing this as separate work rather than when the LED
266 * subsystem asks, we avoid messing with the Eeepc ACPI stuff during a
267 * potentially bad time, such as a timer interrupt.
269 static void tpd_led_update(struct work_struct *work)
272 struct eeepc_wmi *eeepc;
274 eeepc = container_of(work, struct eeepc_wmi, tpd_led_work);
276 ctrl_param = eeepc->tpd_led_wk;
277 eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_TPDLED, ctrl_param, NULL);
280 static void tpd_led_set(struct led_classdev *led_cdev,
281 enum led_brightness value)
283 struct eeepc_wmi *eeepc;
285 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
287 eeepc->tpd_led_wk = !!value;
288 queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
291 static int read_tpd_state(struct eeepc_wmi *eeepc)
296 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_TPDLED, &retval);
298 if (ACPI_FAILURE(status))
300 else if (!retval || retval == 0x00060000)
302 * if touchpad led is present, DSTS will set some bits,
303 * usually 0x00020000.
304 * 0x00060000 means that the device is not supported
308 /* Status is stored in the first bit */
312 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
314 struct eeepc_wmi *eeepc;
316 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
318 return read_tpd_state(eeepc);
321 static int eeepc_wmi_led_init(struct eeepc_wmi *eeepc)
325 if (read_tpd_state(eeepc) < 0)
328 eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
329 if (!eeepc->led_workqueue)
331 INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
333 eeepc->tpd_led.name = "eeepc::touchpad";
334 eeepc->tpd_led.brightness_set = tpd_led_set;
335 eeepc->tpd_led.brightness_get = tpd_led_get;
336 eeepc->tpd_led.max_brightness = 1;
338 rv = led_classdev_register(&eeepc->platform_device->dev,
341 destroy_workqueue(eeepc->led_workqueue);
348 static void eeepc_wmi_led_exit(struct eeepc_wmi *eeepc)
350 if (eeepc->tpd_led.dev)
351 led_classdev_unregister(&eeepc->tpd_led);
352 if (eeepc->led_workqueue)
353 destroy_workqueue(eeepc->led_workqueue);
357 * PCI hotplug (for wlan rfkill)
359 static bool eeepc_wlan_rfkill_blocked(struct eeepc_wmi *eeepc)
364 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_WLAN, &retval);
366 if (ACPI_FAILURE(status))
369 return !(retval & 0x1);
372 static void eeepc_rfkill_hotplug(struct eeepc_wmi *eeepc)
380 mutex_lock(&eeepc->wmi_lock);
381 blocked = eeepc_wlan_rfkill_blocked(eeepc);
382 mutex_unlock(&eeepc->wmi_lock);
384 mutex_lock(&eeepc->hotplug_lock);
386 if (eeepc->wlan_rfkill)
387 rfkill_set_sw_state(eeepc->wlan_rfkill, blocked);
389 if (eeepc->hotplug_slot) {
390 bus = pci_find_bus(0, 1);
392 pr_warning("Unable to find PCI bus 1?\n");
396 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
397 pr_err("Unable to read PCI config space?\n");
400 absent = (l == 0xffffffff);
402 if (blocked != absent) {
403 pr_warning("BIOS says wireless lan is %s, "
404 "but the pci device is %s\n",
405 blocked ? "blocked" : "unblocked",
406 absent ? "absent" : "present");
407 pr_warning("skipped wireless hotplug as probably "
408 "inappropriate for this model\n");
413 dev = pci_get_slot(bus, 0);
415 /* Device already present */
419 dev = pci_scan_single_device(bus, 0);
421 pci_bus_assign_resources(bus);
422 if (pci_bus_add_device(dev))
423 pr_err("Unable to hotplug wifi\n");
426 dev = pci_get_slot(bus, 0);
428 pci_remove_bus_device(dev);
435 mutex_unlock(&eeepc->hotplug_lock);
438 static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
440 struct eeepc_wmi *eeepc = data;
442 if (event != ACPI_NOTIFY_BUS_CHECK)
446 * We can't call directly eeepc_rfkill_hotplug because most
447 * of the time WMBC is still being executed and not reetrant.
448 * There is currently no way to tell ACPICA that we want this
449 * method to be serialized, we schedule a eeepc_rfkill_hotplug
450 * call later, in a safer context.
452 queue_work(eeepc->hotplug_workqueue, &eeepc->hotplug_work);
455 static int eeepc_register_rfkill_notifier(struct eeepc_wmi *eeepc,
461 status = acpi_get_handle(NULL, node, &handle);
463 if (ACPI_SUCCESS(status)) {
464 status = acpi_install_notify_handler(handle,
468 if (ACPI_FAILURE(status))
469 pr_warning("Failed to register notify on %s\n", node);
476 static void eeepc_unregister_rfkill_notifier(struct eeepc_wmi *eeepc,
479 acpi_status status = AE_OK;
482 status = acpi_get_handle(NULL, node, &handle);
484 if (ACPI_SUCCESS(status)) {
485 status = acpi_remove_notify_handler(handle,
487 eeepc_rfkill_notify);
488 if (ACPI_FAILURE(status))
489 pr_err("Error removing rfkill notify handler %s\n",
494 static int eeepc_get_adapter_status(struct hotplug_slot *hotplug_slot,
500 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_WLAN, &retval);
502 if (ACPI_FAILURE(status))
505 if (!retval || retval == 0x00060000)
508 *value = (retval & 0x1);
513 static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
515 kfree(hotplug_slot->info);
519 static struct hotplug_slot_ops eeepc_hotplug_slot_ops = {
520 .owner = THIS_MODULE,
521 .get_adapter_status = eeepc_get_adapter_status,
522 .get_power_status = eeepc_get_adapter_status,
525 static void eeepc_hotplug_work(struct work_struct *work)
527 struct eeepc_wmi *eeepc;
529 eeepc = container_of(work, struct eeepc_wmi, hotplug_work);
530 eeepc_rfkill_hotplug(eeepc);
533 static int eeepc_setup_pci_hotplug(struct eeepc_wmi *eeepc)
536 struct pci_bus *bus = pci_find_bus(0, 1);
539 pr_err("Unable to find wifi PCI bus\n");
543 eeepc->hotplug_workqueue =
544 create_singlethread_workqueue("hotplug_workqueue");
545 if (!eeepc->hotplug_workqueue)
546 goto error_workqueue;
548 INIT_WORK(&eeepc->hotplug_work, eeepc_hotplug_work);
550 eeepc->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
551 if (!eeepc->hotplug_slot)
554 eeepc->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
556 if (!eeepc->hotplug_slot->info)
559 eeepc->hotplug_slot->private = eeepc;
560 eeepc->hotplug_slot->release = &eeepc_cleanup_pci_hotplug;
561 eeepc->hotplug_slot->ops = &eeepc_hotplug_slot_ops;
562 eeepc_get_adapter_status(eeepc->hotplug_slot,
563 &eeepc->hotplug_slot->info->adapter_status);
565 ret = pci_hp_register(eeepc->hotplug_slot, bus, 0, "eeepc-wifi");
567 pr_err("Unable to register hotplug slot - %d\n", ret);
574 kfree(eeepc->hotplug_slot->info);
576 kfree(eeepc->hotplug_slot);
577 eeepc->hotplug_slot = NULL;
579 destroy_workqueue(eeepc->hotplug_workqueue);
587 static int eeepc_rfkill_set(void *data, bool blocked)
589 int dev_id = (unsigned long)data;
590 u32 ctrl_param = !blocked;
592 return eeepc_wmi_set_devstate(dev_id, ctrl_param, NULL);
595 static void eeepc_rfkill_query(struct rfkill *rfkill, void *data)
597 int dev_id = (unsigned long)data;
601 status = eeepc_wmi_get_devstate(dev_id, &retval);
603 if (ACPI_FAILURE(status))
606 rfkill_set_sw_state(rfkill, !(retval & 0x1));
609 static int eeepc_rfkill_wlan_set(void *data, bool blocked)
611 struct eeepc_wmi *eeepc = data;
615 * This handler is enabled only if hotplug is enabled.
616 * In this case, the eeepc_wmi_set_devstate() will
617 * trigger a wmi notification and we need to wait
618 * this call to finish before being able to call
621 mutex_lock(&eeepc->wmi_lock);
622 ret = eeepc_rfkill_set((void *)(long)EEEPC_WMI_DEVID_WLAN, blocked);
623 mutex_unlock(&eeepc->wmi_lock);
627 static void eeepc_rfkill_wlan_query(struct rfkill *rfkill, void *data)
629 eeepc_rfkill_query(rfkill, (void *)(long)EEEPC_WMI_DEVID_WLAN);
632 static const struct rfkill_ops eeepc_rfkill_wlan_ops = {
633 .set_block = eeepc_rfkill_wlan_set,
634 .query = eeepc_rfkill_wlan_query,
637 static const struct rfkill_ops eeepc_rfkill_ops = {
638 .set_block = eeepc_rfkill_set,
639 .query = eeepc_rfkill_query,
642 static int eeepc_new_rfkill(struct eeepc_wmi *eeepc,
643 struct rfkill **rfkill,
645 enum rfkill_type type, int dev_id)
651 status = eeepc_wmi_get_devstate(dev_id, &retval);
653 if (ACPI_FAILURE(status))
656 /* If the device is present, DSTS will always set some bits
657 * 0x00070000 - 1110000000000000000 - device supported
658 * 0x00060000 - 1100000000000000000 - not supported
659 * 0x00020000 - 0100000000000000000 - device supported
660 * 0x00010000 - 0010000000000000000 - not supported / special mode ?
662 if (!retval || retval == 0x00060000)
665 if (dev_id == EEEPC_WMI_DEVID_WLAN && eeepc->hotplug_wireless)
666 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
667 &eeepc_rfkill_wlan_ops, eeepc);
669 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
670 &eeepc_rfkill_ops, (void *)(long)dev_id);
675 rfkill_init_sw_state(*rfkill, !(retval & 0x1));
676 result = rfkill_register(*rfkill);
678 rfkill_destroy(*rfkill);
685 static void eeepc_wmi_rfkill_exit(struct eeepc_wmi *eeepc)
687 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
688 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
689 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
690 if (eeepc->wlan_rfkill) {
691 rfkill_unregister(eeepc->wlan_rfkill);
692 rfkill_destroy(eeepc->wlan_rfkill);
693 eeepc->wlan_rfkill = NULL;
696 * Refresh pci hotplug in case the rfkill state was changed after
697 * eeepc_unregister_rfkill_notifier()
699 eeepc_rfkill_hotplug(eeepc);
700 if (eeepc->hotplug_slot)
701 pci_hp_deregister(eeepc->hotplug_slot);
702 if (eeepc->hotplug_workqueue)
703 destroy_workqueue(eeepc->hotplug_workqueue);
705 if (eeepc->bluetooth_rfkill) {
706 rfkill_unregister(eeepc->bluetooth_rfkill);
707 rfkill_destroy(eeepc->bluetooth_rfkill);
708 eeepc->bluetooth_rfkill = NULL;
710 if (eeepc->wwan3g_rfkill) {
711 rfkill_unregister(eeepc->wwan3g_rfkill);
712 rfkill_destroy(eeepc->wwan3g_rfkill);
713 eeepc->wwan3g_rfkill = NULL;
717 static int eeepc_wmi_rfkill_init(struct eeepc_wmi *eeepc)
721 mutex_init(&eeepc->hotplug_lock);
722 mutex_init(&eeepc->wmi_lock);
724 result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
725 "eeepc-wlan", RFKILL_TYPE_WLAN,
726 EEEPC_WMI_DEVID_WLAN);
728 if (result && result != -ENODEV)
731 result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
732 "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
733 EEEPC_WMI_DEVID_BLUETOOTH);
735 if (result && result != -ENODEV)
738 result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
739 "eeepc-wwan3g", RFKILL_TYPE_WWAN,
740 EEEPC_WMI_DEVID_WWAN3G);
742 if (result && result != -ENODEV)
745 result = eeepc_setup_pci_hotplug(eeepc);
747 * If we get -EBUSY then something else is handling the PCI hotplug -
748 * don't fail in this case
750 if (result == -EBUSY)
753 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
754 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
755 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
757 * Refresh pci hotplug in case the rfkill state was changed during
760 eeepc_rfkill_hotplug(eeepc);
763 if (result && result != -ENODEV)
764 eeepc_wmi_rfkill_exit(eeepc);
766 if (result == -ENODEV)
775 static int read_brightness(struct backlight_device *bd)
780 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BACKLIGHT, &retval);
782 if (ACPI_FAILURE(status))
785 return retval & 0xFF;
788 static int update_bl_status(struct backlight_device *bd)
794 ctrl_param = bd->props.brightness;
796 status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT,
799 if (ACPI_FAILURE(status))
805 static const struct backlight_ops eeepc_wmi_bl_ops = {
806 .get_brightness = read_brightness,
807 .update_status = update_bl_status,
810 static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
812 struct backlight_device *bd = eeepc->backlight_device;
813 int old = bd->props.brightness;
816 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
817 new = code - NOTIFY_BRNUP_MIN + 1;
818 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
819 new = code - NOTIFY_BRNDOWN_MIN;
821 bd->props.brightness = new;
822 backlight_update_status(bd);
823 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
828 static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
830 struct backlight_device *bd;
831 struct backlight_properties props;
833 memset(&props, 0, sizeof(struct backlight_properties));
834 props.max_brightness = 15;
835 bd = backlight_device_register(EEEPC_WMI_FILE,
836 &eeepc->platform_device->dev, eeepc,
837 &eeepc_wmi_bl_ops, &props);
839 pr_err("Could not register backlight device\n");
843 eeepc->backlight_device = bd;
845 bd->props.brightness = read_brightness(bd);
846 bd->props.power = FB_BLANK_UNBLANK;
847 backlight_update_status(bd);
852 static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
854 if (eeepc->backlight_device)
855 backlight_device_unregister(eeepc->backlight_device);
857 eeepc->backlight_device = NULL;
860 static void eeepc_wmi_notify(u32 value, void *context)
862 struct eeepc_wmi *eeepc = context;
863 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
864 union acpi_object *obj;
869 status = wmi_get_event_data(value, &response);
870 if (status != AE_OK) {
871 pr_err("bad event status 0x%x\n", status);
875 obj = (union acpi_object *)response.pointer;
877 if (obj && obj->type == ACPI_TYPE_INTEGER) {
878 code = obj->integer.value;
881 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
882 code = NOTIFY_BRNUP_MIN;
883 else if (code >= NOTIFY_BRNDOWN_MIN &&
884 code <= NOTIFY_BRNDOWN_MAX)
885 code = NOTIFY_BRNDOWN_MIN;
887 if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
888 if (!acpi_video_backlight_support())
889 eeepc_wmi_backlight_notify(eeepc, orig_code);
892 if (!sparse_keymap_report_event(eeepc->inputdev,
894 pr_info("Unknown key %x pressed\n", code);
900 static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
901 const char *buf, size_t count)
904 struct acpi_buffer input = { (acpi_size)sizeof(value), &value };
907 if (!count || sscanf(buf, "%i", &value) != 1)
909 if (value < 0 || value > 2)
912 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
913 1, EEEPC_WMI_METHODID_CFVS, &input, NULL);
915 if (ACPI_FAILURE(status))
921 static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
923 static struct attribute *platform_attributes[] = {
924 &dev_attr_cpufv.attr,
928 static struct attribute_group platform_attribute_group = {
929 .attrs = platform_attributes
932 static void eeepc_wmi_sysfs_exit(struct platform_device *device)
934 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
937 static int eeepc_wmi_sysfs_init(struct platform_device *device)
939 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
945 static int __init eeepc_wmi_platform_init(struct eeepc_wmi *eeepc)
949 eeepc->platform_device = platform_device_alloc(EEEPC_WMI_FILE, -1);
950 if (!eeepc->platform_device)
952 platform_set_drvdata(eeepc->platform_device, eeepc);
954 err = platform_device_add(eeepc->platform_device);
956 goto fail_platform_device;
958 err = eeepc_wmi_sysfs_init(eeepc->platform_device);
964 platform_device_del(eeepc->platform_device);
965 fail_platform_device:
966 platform_device_put(eeepc->platform_device);
970 static void eeepc_wmi_platform_exit(struct eeepc_wmi *eeepc)
972 eeepc_wmi_sysfs_exit(eeepc->platform_device);
973 platform_device_unregister(eeepc->platform_device);
979 struct eeepc_wmi_debugfs_node {
980 struct eeepc_wmi *eeepc;
982 int (*show)(struct seq_file *m, void *data);
985 static int show_dsts(struct seq_file *m, void *data)
987 struct eeepc_wmi *eeepc = m->private;
991 status = eeepc_wmi_get_devstate(eeepc->debug.dev_id, &retval);
993 if (ACPI_FAILURE(status))
996 seq_printf(m, "DSTS(%x) = %x\n", eeepc->debug.dev_id, retval);
1001 static int show_devs(struct seq_file *m, void *data)
1003 struct eeepc_wmi *eeepc = m->private;
1007 status = eeepc_wmi_set_devstate(eeepc->debug.dev_id,
1008 eeepc->debug.ctrl_param, &retval);
1009 if (ACPI_FAILURE(status))
1012 seq_printf(m, "DEVS(%x, %x) = %x\n", eeepc->debug.dev_id,
1013 eeepc->debug.ctrl_param, retval);
1018 static struct eeepc_wmi_debugfs_node eeepc_wmi_debug_files[] = {
1019 { NULL, "devs", show_devs },
1020 { NULL, "dsts", show_dsts },
1023 static int eeepc_wmi_debugfs_open(struct inode *inode, struct file *file)
1025 struct eeepc_wmi_debugfs_node *node = inode->i_private;
1027 return single_open(file, node->show, node->eeepc);
1030 static const struct file_operations eeepc_wmi_debugfs_io_ops = {
1031 .owner = THIS_MODULE,
1032 .open = eeepc_wmi_debugfs_open,
1034 .llseek = seq_lseek,
1035 .release = single_release,
1038 static void eeepc_wmi_debugfs_exit(struct eeepc_wmi *eeepc)
1040 debugfs_remove_recursive(eeepc->debug.root);
1043 static int eeepc_wmi_debugfs_init(struct eeepc_wmi *eeepc)
1045 struct dentry *dent;
1048 eeepc->debug.root = debugfs_create_dir(EEEPC_WMI_FILE, NULL);
1049 if (!eeepc->debug.root) {
1050 pr_err("failed to create debugfs directory");
1054 dent = debugfs_create_x32("dev_id", S_IRUGO|S_IWUSR,
1055 eeepc->debug.root, &eeepc->debug.dev_id);
1059 dent = debugfs_create_x32("ctrl_param", S_IRUGO|S_IWUSR,
1060 eeepc->debug.root, &eeepc->debug.ctrl_param);
1064 for (i = 0; i < ARRAY_SIZE(eeepc_wmi_debug_files); i++) {
1065 struct eeepc_wmi_debugfs_node *node = &eeepc_wmi_debug_files[i];
1067 node->eeepc = eeepc;
1068 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
1069 eeepc->debug.root, node,
1070 &eeepc_wmi_debugfs_io_ops);
1072 pr_err("failed to create debug file: %s\n", node->name);
1080 eeepc_wmi_debugfs_exit(eeepc);
1087 static void eeepc_dmi_check(struct eeepc_wmi *eeepc)
1091 model = dmi_get_system_info(DMI_PRODUCT_NAME);
1096 * Whitelist for wlan hotplug
1098 * Eeepc 1000H needs the current hotplug code to handle
1099 * Fn+F2 correctly. We may add other Eeepc here later, but
1100 * it seems that most of the laptops supported by eeepc-wmi
1101 * don't need to be on this list
1103 if (strcmp(model, "1000H") == 0) {
1104 eeepc->hotplug_wireless = true;
1105 pr_info("wlan hotplug enabled\n");
1109 static struct platform_device * __init eeepc_wmi_add(void)
1111 struct eeepc_wmi *eeepc;
1115 eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
1117 return ERR_PTR(-ENOMEM);
1119 eeepc->hotplug_wireless = hotplug_wireless;
1120 eeepc_dmi_check(eeepc);
1123 * Register the platform device first. It is used as a parent for the
1124 * sub-devices below.
1126 err = eeepc_wmi_platform_init(eeepc);
1130 err = eeepc_wmi_input_init(eeepc);
1134 err = eeepc_wmi_led_init(eeepc);
1138 err = eeepc_wmi_rfkill_init(eeepc);
1142 if (!acpi_video_backlight_support()) {
1143 err = eeepc_wmi_backlight_init(eeepc);
1145 goto fail_backlight;
1147 pr_info("Backlight controlled by ACPI video driver\n");
1149 status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
1150 eeepc_wmi_notify, eeepc);
1151 if (ACPI_FAILURE(status)) {
1152 pr_err("Unable to register notify handler - %d\n",
1155 goto fail_wmi_handler;
1158 err = eeepc_wmi_debugfs_init(eeepc);
1162 return eeepc->platform_device;
1165 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
1167 eeepc_wmi_backlight_exit(eeepc);
1169 eeepc_wmi_rfkill_exit(eeepc);
1171 eeepc_wmi_led_exit(eeepc);
1173 eeepc_wmi_input_exit(eeepc);
1175 eeepc_wmi_platform_exit(eeepc);
1178 return ERR_PTR(err);
1181 static int eeepc_wmi_remove(struct platform_device *device)
1183 struct eeepc_wmi *eeepc;
1185 eeepc = platform_get_drvdata(device);
1186 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
1187 eeepc_wmi_backlight_exit(eeepc);
1188 eeepc_wmi_input_exit(eeepc);
1189 eeepc_wmi_led_exit(eeepc);
1190 eeepc_wmi_rfkill_exit(eeepc);
1191 eeepc_wmi_debugfs_exit(eeepc);
1192 eeepc_wmi_platform_exit(eeepc);
1198 static struct platform_driver platform_driver = {
1200 .name = EEEPC_WMI_FILE,
1201 .owner = THIS_MODULE,
1205 static acpi_status __init eeepc_wmi_parse_device(acpi_handle handle, u32 level,
1206 void *context, void **retval)
1208 pr_warning("Found legacy ATKD device (%s)", EEEPC_ACPI_HID);
1209 *(bool *)context = true;
1210 return AE_CTRL_TERMINATE;
1213 static int __init eeepc_wmi_check_atkd(void)
1218 status = acpi_get_devices(EEEPC_ACPI_HID, eeepc_wmi_parse_device,
1221 if (ACPI_FAILURE(status) || !found)
1226 static int __init eeepc_wmi_init(void)
1230 if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
1231 !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
1232 pr_warning("No known WMI GUID found\n");
1236 if (eeepc_wmi_check_atkd()) {
1237 pr_warning("WMI device present, but legacy ATKD device is also "
1238 "present and enabled.");
1239 pr_warning("You probably booted with acpi_osi=\"Linux\" or "
1240 "acpi_osi=\"!Windows 2009\"");
1241 pr_warning("Can't load eeepc-wmi, use default acpi_osi "
1242 "(preferred) or eeepc-laptop");
1246 platform_device = eeepc_wmi_add();
1247 if (IS_ERR(platform_device)) {
1248 err = PTR_ERR(platform_device);
1249 goto fail_eeepc_wmi;
1252 err = platform_driver_register(&platform_driver);
1254 pr_warning("Unable to register platform driver\n");
1255 goto fail_platform_driver;
1260 fail_platform_driver:
1261 eeepc_wmi_remove(platform_device);
1266 static void __exit eeepc_wmi_exit(void)
1268 eeepc_wmi_remove(platform_device);
1269 platform_driver_unregister(&platform_driver);
1272 module_init(eeepc_wmi_init);
1273 module_exit(eeepc_wmi_exit);