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_WIMAX 0x00010017
75 #define EEEPC_WMI_DEVID_WWAN3G 0x00010019
76 #define EEEPC_WMI_DEVID_BACKLIGHT 0x00050012
77 #define EEEPC_WMI_DEVID_CAMERA 0x00060013
78 #define EEEPC_WMI_DEVID_CARDREADER 0x00080013
79 #define EEEPC_WMI_DEVID_TPDLED 0x00100011
81 #define EEEPC_WMI_DSTS_STATUS_BIT 0x00000001
82 #define EEEPC_WMI_DSTS_PRESENCE_BIT 0x00010000
84 static bool hotplug_wireless;
86 module_param(hotplug_wireless, bool, 0444);
87 MODULE_PARM_DESC(hotplug_wireless,
88 "Enable hotplug for wireless device. "
89 "If your laptop needs that, please report to "
90 "acpi4asus-user@lists.sourceforge.net.");
92 static const struct key_entry eeepc_wmi_keymap[] = {
93 /* Sleep already handled via generic ACPI code */
94 { KE_IGNORE, NOTIFY_BRNDOWN_MIN, { KEY_BRIGHTNESSDOWN } },
95 { KE_IGNORE, NOTIFY_BRNUP_MIN, { KEY_BRIGHTNESSUP } },
96 { KE_KEY, 0x30, { KEY_VOLUMEUP } },
97 { KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
98 { KE_KEY, 0x32, { KEY_MUTE } },
99 { KE_KEY, 0x5c, { KEY_F15 } },
100 { KE_KEY, 0x5d, { KEY_WLAN } },
101 { KE_KEY, 0x6b, { KEY_F13 } }, /* Disable Touchpad */
102 { KE_KEY, 0x88, { KEY_WLAN } },
103 { KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } },
104 { KE_KEY, 0xe0, { KEY_PROG1 } },
105 { KE_KEY, 0xe1, { KEY_F14 } },
106 { KE_KEY, 0xe9, { KEY_DISPLAY_OFF } },
116 * eeepc-wmi/ - debugfs root directory
117 * dev_id - current dev_id
118 * ctrl_param - current ctrl_param
119 * devs - call DEVS(dev_id, ctrl_param) and print result
120 * dsts - call DSTS(dev_id) and print result
122 struct eeepc_wmi_debug {
129 bool hotplug_wireless;
131 struct input_dev *inputdev;
132 struct backlight_device *backlight_device;
133 struct platform_device *platform_device;
135 struct led_classdev tpd_led;
137 struct workqueue_struct *led_workqueue;
138 struct work_struct tpd_led_work;
140 struct rfkill *wlan_rfkill;
141 struct rfkill *bluetooth_rfkill;
142 struct rfkill *wimax_rfkill;
143 struct rfkill *wwan3g_rfkill;
145 struct hotplug_slot *hotplug_slot;
146 struct mutex hotplug_lock;
147 struct mutex wmi_lock;
148 struct workqueue_struct *hotplug_workqueue;
149 struct work_struct hotplug_work;
151 struct eeepc_wmi_debug debug;
154 static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc)
158 eeepc->inputdev = input_allocate_device();
159 if (!eeepc->inputdev)
162 eeepc->inputdev->name = "Eee PC WMI hotkeys";
163 eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0";
164 eeepc->inputdev->id.bustype = BUS_HOST;
165 eeepc->inputdev->dev.parent = &eeepc->platform_device->dev;
167 err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL);
171 err = input_register_device(eeepc->inputdev);
173 goto err_free_keymap;
178 sparse_keymap_free(eeepc->inputdev);
180 input_free_device(eeepc->inputdev);
184 static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc)
186 if (eeepc->inputdev) {
187 sparse_keymap_free(eeepc->inputdev);
188 input_unregister_device(eeepc->inputdev);
191 eeepc->inputdev = NULL;
194 static acpi_status eeepc_wmi_get_devstate(u32 dev_id, u32 *retval)
196 struct acpi_buffer input = { (acpi_size)sizeof(u32), &dev_id };
197 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
198 union acpi_object *obj;
202 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
203 1, EEEPC_WMI_METHODID_DSTS,
206 if (ACPI_FAILURE(status))
209 obj = (union acpi_object *)output.pointer;
210 if (obj && obj->type == ACPI_TYPE_INTEGER)
211 tmp = (u32)obj->integer.value;
224 static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
227 struct bios_args args = {
229 .ctrl_param = ctrl_param,
231 struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
235 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
236 EEEPC_WMI_METHODID_DEVS,
239 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
240 union acpi_object *obj;
243 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
244 EEEPC_WMI_METHODID_DEVS,
247 if (ACPI_FAILURE(status))
250 obj = (union acpi_object *)output.pointer;
251 if (obj && obj->type == ACPI_TYPE_INTEGER)
252 tmp = (u32)obj->integer.value;
264 /* Helper for special devices with magic return codes */
265 static int eeepc_wmi_get_devstate_simple(u32 dev_id)
270 status = eeepc_wmi_get_devstate(dev_id, &retval);
272 if (ACPI_FAILURE(status))
275 if (!(retval & EEEPC_WMI_DSTS_PRESENCE_BIT))
278 return retval & EEEPC_WMI_DSTS_STATUS_BIT;
285 * These functions actually update the LED's, and are called from a
286 * workqueue. By doing this as separate work rather than when the LED
287 * subsystem asks, we avoid messing with the Eeepc ACPI stuff during a
288 * potentially bad time, such as a timer interrupt.
290 static void tpd_led_update(struct work_struct *work)
293 struct eeepc_wmi *eeepc;
295 eeepc = container_of(work, struct eeepc_wmi, tpd_led_work);
297 ctrl_param = eeepc->tpd_led_wk;
298 eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_TPDLED, ctrl_param, NULL);
301 static void tpd_led_set(struct led_classdev *led_cdev,
302 enum led_brightness value)
304 struct eeepc_wmi *eeepc;
306 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
308 eeepc->tpd_led_wk = !!value;
309 queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
312 static int read_tpd_state(struct eeepc_wmi *eeepc)
314 return eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_TPDLED);
317 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
319 struct eeepc_wmi *eeepc;
321 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
323 return read_tpd_state(eeepc);
326 static int eeepc_wmi_led_init(struct eeepc_wmi *eeepc)
330 if (read_tpd_state(eeepc) < 0)
333 eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
334 if (!eeepc->led_workqueue)
336 INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
338 eeepc->tpd_led.name = "eeepc::touchpad";
339 eeepc->tpd_led.brightness_set = tpd_led_set;
340 eeepc->tpd_led.brightness_get = tpd_led_get;
341 eeepc->tpd_led.max_brightness = 1;
343 rv = led_classdev_register(&eeepc->platform_device->dev,
346 destroy_workqueue(eeepc->led_workqueue);
353 static void eeepc_wmi_led_exit(struct eeepc_wmi *eeepc)
355 if (eeepc->tpd_led.dev)
356 led_classdev_unregister(&eeepc->tpd_led);
357 if (eeepc->led_workqueue)
358 destroy_workqueue(eeepc->led_workqueue);
362 * PCI hotplug (for wlan rfkill)
364 static bool eeepc_wlan_rfkill_blocked(struct eeepc_wmi *eeepc)
366 int result = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
373 static void eeepc_rfkill_hotplug(struct eeepc_wmi *eeepc)
381 mutex_lock(&eeepc->wmi_lock);
382 blocked = eeepc_wlan_rfkill_blocked(eeepc);
383 mutex_unlock(&eeepc->wmi_lock);
385 mutex_lock(&eeepc->hotplug_lock);
387 if (eeepc->wlan_rfkill)
388 rfkill_set_sw_state(eeepc->wlan_rfkill, blocked);
390 if (eeepc->hotplug_slot) {
391 bus = pci_find_bus(0, 1);
393 pr_warning("Unable to find PCI bus 1?\n");
397 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
398 pr_err("Unable to read PCI config space?\n");
401 absent = (l == 0xffffffff);
403 if (blocked != absent) {
404 pr_warning("BIOS says wireless lan is %s, "
405 "but the pci device is %s\n",
406 blocked ? "blocked" : "unblocked",
407 absent ? "absent" : "present");
408 pr_warning("skipped wireless hotplug as probably "
409 "inappropriate for this model\n");
414 dev = pci_get_slot(bus, 0);
416 /* Device already present */
420 dev = pci_scan_single_device(bus, 0);
422 pci_bus_assign_resources(bus);
423 if (pci_bus_add_device(dev))
424 pr_err("Unable to hotplug wifi\n");
427 dev = pci_get_slot(bus, 0);
429 pci_remove_bus_device(dev);
436 mutex_unlock(&eeepc->hotplug_lock);
439 static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
441 struct eeepc_wmi *eeepc = data;
443 if (event != ACPI_NOTIFY_BUS_CHECK)
447 * We can't call directly eeepc_rfkill_hotplug because most
448 * of the time WMBC is still being executed and not reetrant.
449 * There is currently no way to tell ACPICA that we want this
450 * method to be serialized, we schedule a eeepc_rfkill_hotplug
451 * call later, in a safer context.
453 queue_work(eeepc->hotplug_workqueue, &eeepc->hotplug_work);
456 static int eeepc_register_rfkill_notifier(struct eeepc_wmi *eeepc,
462 status = acpi_get_handle(NULL, node, &handle);
464 if (ACPI_SUCCESS(status)) {
465 status = acpi_install_notify_handler(handle,
469 if (ACPI_FAILURE(status))
470 pr_warning("Failed to register notify on %s\n", node);
477 static void eeepc_unregister_rfkill_notifier(struct eeepc_wmi *eeepc,
480 acpi_status status = AE_OK;
483 status = acpi_get_handle(NULL, node, &handle);
485 if (ACPI_SUCCESS(status)) {
486 status = acpi_remove_notify_handler(handle,
488 eeepc_rfkill_notify);
489 if (ACPI_FAILURE(status))
490 pr_err("Error removing rfkill notify handler %s\n",
495 static int eeepc_get_adapter_status(struct hotplug_slot *hotplug_slot,
498 int result = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
507 static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
509 kfree(hotplug_slot->info);
513 static struct hotplug_slot_ops eeepc_hotplug_slot_ops = {
514 .owner = THIS_MODULE,
515 .get_adapter_status = eeepc_get_adapter_status,
516 .get_power_status = eeepc_get_adapter_status,
519 static void eeepc_hotplug_work(struct work_struct *work)
521 struct eeepc_wmi *eeepc;
523 eeepc = container_of(work, struct eeepc_wmi, hotplug_work);
524 eeepc_rfkill_hotplug(eeepc);
527 static int eeepc_setup_pci_hotplug(struct eeepc_wmi *eeepc)
530 struct pci_bus *bus = pci_find_bus(0, 1);
533 pr_err("Unable to find wifi PCI bus\n");
537 eeepc->hotplug_workqueue =
538 create_singlethread_workqueue("hotplug_workqueue");
539 if (!eeepc->hotplug_workqueue)
540 goto error_workqueue;
542 INIT_WORK(&eeepc->hotplug_work, eeepc_hotplug_work);
544 eeepc->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
545 if (!eeepc->hotplug_slot)
548 eeepc->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
550 if (!eeepc->hotplug_slot->info)
553 eeepc->hotplug_slot->private = eeepc;
554 eeepc->hotplug_slot->release = &eeepc_cleanup_pci_hotplug;
555 eeepc->hotplug_slot->ops = &eeepc_hotplug_slot_ops;
556 eeepc_get_adapter_status(eeepc->hotplug_slot,
557 &eeepc->hotplug_slot->info->adapter_status);
559 ret = pci_hp_register(eeepc->hotplug_slot, bus, 0, "eeepc-wifi");
561 pr_err("Unable to register hotplug slot - %d\n", ret);
568 kfree(eeepc->hotplug_slot->info);
570 kfree(eeepc->hotplug_slot);
571 eeepc->hotplug_slot = NULL;
573 destroy_workqueue(eeepc->hotplug_workqueue);
581 static int eeepc_rfkill_set(void *data, bool blocked)
583 int dev_id = (unsigned long)data;
584 u32 ctrl_param = !blocked;
587 status = eeepc_wmi_set_devstate(dev_id, ctrl_param, NULL);
589 if (ACPI_FAILURE(status))
595 static void eeepc_rfkill_query(struct rfkill *rfkill, void *data)
597 int dev_id = (unsigned long)data;
600 result = eeepc_wmi_get_devstate_simple(dev_id);
605 rfkill_set_sw_state(rfkill, !result);
608 static int eeepc_rfkill_wlan_set(void *data, bool blocked)
610 struct eeepc_wmi *eeepc = data;
614 * This handler is enabled only if hotplug is enabled.
615 * In this case, the eeepc_wmi_set_devstate() will
616 * trigger a wmi notification and we need to wait
617 * this call to finish before being able to call
620 mutex_lock(&eeepc->wmi_lock);
621 ret = eeepc_rfkill_set((void *)(long)EEEPC_WMI_DEVID_WLAN, blocked);
622 mutex_unlock(&eeepc->wmi_lock);
626 static void eeepc_rfkill_wlan_query(struct rfkill *rfkill, void *data)
628 eeepc_rfkill_query(rfkill, (void *)(long)EEEPC_WMI_DEVID_WLAN);
631 static const struct rfkill_ops eeepc_rfkill_wlan_ops = {
632 .set_block = eeepc_rfkill_wlan_set,
633 .query = eeepc_rfkill_wlan_query,
636 static const struct rfkill_ops eeepc_rfkill_ops = {
637 .set_block = eeepc_rfkill_set,
638 .query = eeepc_rfkill_query,
641 static int eeepc_new_rfkill(struct eeepc_wmi *eeepc,
642 struct rfkill **rfkill,
644 enum rfkill_type type, int dev_id)
646 int result = eeepc_wmi_get_devstate_simple(dev_id);
651 if (dev_id == EEEPC_WMI_DEVID_WLAN && eeepc->hotplug_wireless)
652 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
653 &eeepc_rfkill_wlan_ops, eeepc);
655 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
656 &eeepc_rfkill_ops, (void *)(long)dev_id);
661 rfkill_init_sw_state(*rfkill, !result);
662 result = rfkill_register(*rfkill);
664 rfkill_destroy(*rfkill);
671 static void eeepc_wmi_rfkill_exit(struct eeepc_wmi *eeepc)
673 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
674 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
675 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
676 if (eeepc->wlan_rfkill) {
677 rfkill_unregister(eeepc->wlan_rfkill);
678 rfkill_destroy(eeepc->wlan_rfkill);
679 eeepc->wlan_rfkill = NULL;
682 * Refresh pci hotplug in case the rfkill state was changed after
683 * eeepc_unregister_rfkill_notifier()
685 eeepc_rfkill_hotplug(eeepc);
686 if (eeepc->hotplug_slot)
687 pci_hp_deregister(eeepc->hotplug_slot);
688 if (eeepc->hotplug_workqueue)
689 destroy_workqueue(eeepc->hotplug_workqueue);
691 if (eeepc->bluetooth_rfkill) {
692 rfkill_unregister(eeepc->bluetooth_rfkill);
693 rfkill_destroy(eeepc->bluetooth_rfkill);
694 eeepc->bluetooth_rfkill = NULL;
696 if (eeepc->wimax_rfkill) {
697 rfkill_unregister(eeepc->wimax_rfkill);
698 rfkill_destroy(eeepc->wimax_rfkill);
699 eeepc->wimax_rfkill = NULL;
701 if (eeepc->wwan3g_rfkill) {
702 rfkill_unregister(eeepc->wwan3g_rfkill);
703 rfkill_destroy(eeepc->wwan3g_rfkill);
704 eeepc->wwan3g_rfkill = NULL;
708 static int eeepc_wmi_rfkill_init(struct eeepc_wmi *eeepc)
712 mutex_init(&eeepc->hotplug_lock);
713 mutex_init(&eeepc->wmi_lock);
715 result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
716 "eeepc-wlan", RFKILL_TYPE_WLAN,
717 EEEPC_WMI_DEVID_WLAN);
719 if (result && result != -ENODEV)
722 result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
723 "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
724 EEEPC_WMI_DEVID_BLUETOOTH);
726 if (result && result != -ENODEV)
729 result = eeepc_new_rfkill(eeepc, &eeepc->wimax_rfkill,
730 "eeepc-wimax", RFKILL_TYPE_WIMAX,
731 EEEPC_WMI_DEVID_WIMAX);
733 if (result && result != -ENODEV)
736 result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
737 "eeepc-wwan3g", RFKILL_TYPE_WWAN,
738 EEEPC_WMI_DEVID_WWAN3G);
740 if (result && result != -ENODEV)
743 result = eeepc_setup_pci_hotplug(eeepc);
745 * If we get -EBUSY then something else is handling the PCI hotplug -
746 * don't fail in this case
748 if (result == -EBUSY)
751 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
752 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
753 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
755 * Refresh pci hotplug in case the rfkill state was changed during
758 eeepc_rfkill_hotplug(eeepc);
761 if (result && result != -ENODEV)
762 eeepc_wmi_rfkill_exit(eeepc);
764 if (result == -ENODEV)
773 static int read_brightness(struct backlight_device *bd)
778 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BACKLIGHT, &retval);
780 if (ACPI_FAILURE(status))
783 return retval & 0xFF;
786 static int update_bl_status(struct backlight_device *bd)
792 ctrl_param = bd->props.brightness;
794 status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT,
797 if (ACPI_FAILURE(status))
803 static const struct backlight_ops eeepc_wmi_bl_ops = {
804 .get_brightness = read_brightness,
805 .update_status = update_bl_status,
808 static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
810 struct backlight_device *bd = eeepc->backlight_device;
811 int old = bd->props.brightness;
814 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
815 new = code - NOTIFY_BRNUP_MIN + 1;
816 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
817 new = code - NOTIFY_BRNDOWN_MIN;
819 bd->props.brightness = new;
820 backlight_update_status(bd);
821 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
826 static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
828 struct backlight_device *bd;
829 struct backlight_properties props;
831 memset(&props, 0, sizeof(struct backlight_properties));
832 props.max_brightness = 15;
833 bd = backlight_device_register(EEEPC_WMI_FILE,
834 &eeepc->platform_device->dev, eeepc,
835 &eeepc_wmi_bl_ops, &props);
837 pr_err("Could not register backlight device\n");
841 eeepc->backlight_device = bd;
843 bd->props.brightness = read_brightness(bd);
844 bd->props.power = FB_BLANK_UNBLANK;
845 backlight_update_status(bd);
850 static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
852 if (eeepc->backlight_device)
853 backlight_device_unregister(eeepc->backlight_device);
855 eeepc->backlight_device = NULL;
858 static void eeepc_wmi_notify(u32 value, void *context)
860 struct eeepc_wmi *eeepc = context;
861 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
862 union acpi_object *obj;
867 status = wmi_get_event_data(value, &response);
868 if (status != AE_OK) {
869 pr_err("bad event status 0x%x\n", status);
873 obj = (union acpi_object *)response.pointer;
875 if (obj && obj->type == ACPI_TYPE_INTEGER) {
876 code = obj->integer.value;
879 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
880 code = NOTIFY_BRNUP_MIN;
881 else if (code >= NOTIFY_BRNDOWN_MIN &&
882 code <= NOTIFY_BRNDOWN_MAX)
883 code = NOTIFY_BRNDOWN_MIN;
885 if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
886 if (!acpi_video_backlight_support())
887 eeepc_wmi_backlight_notify(eeepc, orig_code);
890 if (!sparse_keymap_report_event(eeepc->inputdev,
892 pr_info("Unknown key %x pressed\n", code);
901 static int parse_arg(const char *buf, unsigned long count, int *val)
905 if (sscanf(buf, "%i", val) != 1)
910 static ssize_t store_sys_wmi(int devid, const char *buf, size_t count)
916 value = eeepc_wmi_get_devstate_simple(devid);
917 if (value == -ENODEV) /* Check device presence */
920 rv = parse_arg(buf, count, &value);
921 status = eeepc_wmi_set_devstate(devid, value, &retval);
923 if (ACPI_FAILURE(status))
928 static ssize_t show_sys_wmi(int devid, char *buf)
930 int value = eeepc_wmi_get_devstate_simple(devid);
935 return sprintf(buf, "%d\n", value);
938 #define EEEPC_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
939 static ssize_t show_##_name(struct device *dev, \
940 struct device_attribute *attr, \
943 return show_sys_wmi(_cm, buf); \
945 static ssize_t store_##_name(struct device *dev, \
946 struct device_attribute *attr, \
947 const char *buf, size_t count) \
949 return store_sys_wmi(_cm, buf, count); \
951 static struct device_attribute dev_attr_##_name = { \
953 .name = __stringify(_name), \
955 .show = show_##_name, \
956 .store = store_##_name, \
959 EEEPC_WMI_CREATE_DEVICE_ATTR(camera, 0644, EEEPC_WMI_DEVID_CAMERA);
960 EEEPC_WMI_CREATE_DEVICE_ATTR(cardr, 0644, EEEPC_WMI_DEVID_CARDREADER);
962 static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
963 const char *buf, size_t count)
966 struct acpi_buffer input = { (acpi_size)sizeof(value), &value };
969 if (!count || sscanf(buf, "%i", &value) != 1)
971 if (value < 0 || value > 2)
974 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
975 1, EEEPC_WMI_METHODID_CFVS, &input, NULL);
977 if (ACPI_FAILURE(status))
983 static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
985 static struct attribute *platform_attributes[] = {
986 &dev_attr_cpufv.attr,
987 &dev_attr_camera.attr,
988 &dev_attr_cardr.attr,
992 static mode_t eeepc_sysfs_is_visible(struct kobject *kobj,
993 struct attribute *attr,
996 bool supported = true;
999 if (attr == &dev_attr_camera.attr)
1000 devid = EEEPC_WMI_DEVID_CAMERA;
1001 else if (attr == &dev_attr_cardr.attr)
1002 devid = EEEPC_WMI_DEVID_CARDREADER;
1005 supported = eeepc_wmi_get_devstate_simple(devid) != -ENODEV;
1007 return supported ? attr->mode : 0;
1010 static struct attribute_group platform_attribute_group = {
1011 .is_visible = eeepc_sysfs_is_visible,
1012 .attrs = platform_attributes
1015 static void eeepc_wmi_sysfs_exit(struct platform_device *device)
1017 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
1020 static int eeepc_wmi_sysfs_init(struct platform_device *device)
1022 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
1028 static int __init eeepc_wmi_platform_init(struct eeepc_wmi *eeepc)
1030 return eeepc_wmi_sysfs_init(eeepc->platform_device);
1033 static void eeepc_wmi_platform_exit(struct eeepc_wmi *eeepc)
1035 eeepc_wmi_sysfs_exit(eeepc->platform_device);
1041 struct eeepc_wmi_debugfs_node {
1042 struct eeepc_wmi *eeepc;
1044 int (*show)(struct seq_file *m, void *data);
1047 static int show_dsts(struct seq_file *m, void *data)
1049 struct eeepc_wmi *eeepc = m->private;
1053 status = eeepc_wmi_get_devstate(eeepc->debug.dev_id, &retval);
1055 if (ACPI_FAILURE(status))
1058 seq_printf(m, "DSTS(%x) = %x\n", eeepc->debug.dev_id, retval);
1063 static int show_devs(struct seq_file *m, void *data)
1065 struct eeepc_wmi *eeepc = m->private;
1069 status = eeepc_wmi_set_devstate(eeepc->debug.dev_id,
1070 eeepc->debug.ctrl_param, &retval);
1071 if (ACPI_FAILURE(status))
1074 seq_printf(m, "DEVS(%x, %x) = %x\n", eeepc->debug.dev_id,
1075 eeepc->debug.ctrl_param, retval);
1080 static struct eeepc_wmi_debugfs_node eeepc_wmi_debug_files[] = {
1081 { NULL, "devs", show_devs },
1082 { NULL, "dsts", show_dsts },
1085 static int eeepc_wmi_debugfs_open(struct inode *inode, struct file *file)
1087 struct eeepc_wmi_debugfs_node *node = inode->i_private;
1089 return single_open(file, node->show, node->eeepc);
1092 static const struct file_operations eeepc_wmi_debugfs_io_ops = {
1093 .owner = THIS_MODULE,
1094 .open = eeepc_wmi_debugfs_open,
1096 .llseek = seq_lseek,
1097 .release = single_release,
1100 static void eeepc_wmi_debugfs_exit(struct eeepc_wmi *eeepc)
1102 debugfs_remove_recursive(eeepc->debug.root);
1105 static int eeepc_wmi_debugfs_init(struct eeepc_wmi *eeepc)
1107 struct dentry *dent;
1110 eeepc->debug.root = debugfs_create_dir(EEEPC_WMI_FILE, NULL);
1111 if (!eeepc->debug.root) {
1112 pr_err("failed to create debugfs directory");
1116 dent = debugfs_create_x32("dev_id", S_IRUGO|S_IWUSR,
1117 eeepc->debug.root, &eeepc->debug.dev_id);
1121 dent = debugfs_create_x32("ctrl_param", S_IRUGO|S_IWUSR,
1122 eeepc->debug.root, &eeepc->debug.ctrl_param);
1126 for (i = 0; i < ARRAY_SIZE(eeepc_wmi_debug_files); i++) {
1127 struct eeepc_wmi_debugfs_node *node = &eeepc_wmi_debug_files[i];
1129 node->eeepc = eeepc;
1130 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
1131 eeepc->debug.root, node,
1132 &eeepc_wmi_debugfs_io_ops);
1134 pr_err("failed to create debug file: %s\n", node->name);
1142 eeepc_wmi_debugfs_exit(eeepc);
1149 static void eeepc_dmi_check(struct eeepc_wmi *eeepc)
1153 model = dmi_get_system_info(DMI_PRODUCT_NAME);
1158 * Whitelist for wlan hotplug
1160 * Eeepc 1000H needs the current hotplug code to handle
1161 * Fn+F2 correctly. We may add other Eeepc here later, but
1162 * it seems that most of the laptops supported by eeepc-wmi
1163 * don't need to be on this list
1165 if (strcmp(model, "1000H") == 0) {
1166 eeepc->hotplug_wireless = true;
1167 pr_info("wlan hotplug enabled\n");
1171 static int __init eeepc_wmi_add(struct platform_device *pdev)
1173 struct eeepc_wmi *eeepc;
1177 eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
1181 eeepc->platform_device = pdev;
1182 platform_set_drvdata(eeepc->platform_device, eeepc);
1184 eeepc->hotplug_wireless = hotplug_wireless;
1185 eeepc_dmi_check(eeepc);
1187 err = eeepc_wmi_platform_init(eeepc);
1191 err = eeepc_wmi_input_init(eeepc);
1195 err = eeepc_wmi_led_init(eeepc);
1199 err = eeepc_wmi_rfkill_init(eeepc);
1203 if (!acpi_video_backlight_support()) {
1204 err = eeepc_wmi_backlight_init(eeepc);
1206 goto fail_backlight;
1208 pr_info("Backlight controlled by ACPI video driver\n");
1210 status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
1211 eeepc_wmi_notify, eeepc);
1212 if (ACPI_FAILURE(status)) {
1213 pr_err("Unable to register notify handler - %d\n",
1216 goto fail_wmi_handler;
1219 err = eeepc_wmi_debugfs_init(eeepc);
1226 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
1228 eeepc_wmi_backlight_exit(eeepc);
1230 eeepc_wmi_rfkill_exit(eeepc);
1232 eeepc_wmi_led_exit(eeepc);
1234 eeepc_wmi_input_exit(eeepc);
1236 eeepc_wmi_platform_exit(eeepc);
1242 static int __exit eeepc_wmi_remove(struct platform_device *device)
1244 struct eeepc_wmi *eeepc;
1246 eeepc = platform_get_drvdata(device);
1247 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
1248 eeepc_wmi_backlight_exit(eeepc);
1249 eeepc_wmi_input_exit(eeepc);
1250 eeepc_wmi_led_exit(eeepc);
1251 eeepc_wmi_rfkill_exit(eeepc);
1252 eeepc_wmi_debugfs_exit(eeepc);
1253 eeepc_wmi_platform_exit(eeepc);
1260 * Platform driver - hibernate/resume callbacks
1262 static int eeepc_hotk_thaw(struct device *device)
1264 struct eeepc_wmi *eeepc = dev_get_drvdata(device);
1266 if (eeepc->wlan_rfkill) {
1270 * Work around bios bug - acpi _PTS turns off the wireless led
1271 * during suspend. Normally it restores it on resume, but
1272 * we should kick it ourselves in case hibernation is aborted.
1274 wlan = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
1275 eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_WLAN, wlan, NULL);
1281 static int eeepc_hotk_restore(struct device *device)
1283 struct eeepc_wmi *eeepc = dev_get_drvdata(device);
1286 /* Refresh both wlan rfkill state and pci hotplug */
1287 if (eeepc->wlan_rfkill)
1288 eeepc_rfkill_hotplug(eeepc);
1290 if (eeepc->bluetooth_rfkill) {
1291 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_BLUETOOTH);
1292 rfkill_set_sw_state(eeepc->bluetooth_rfkill, bl);
1294 if (eeepc->wimax_rfkill) {
1295 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WIMAX);
1296 rfkill_set_sw_state(eeepc->wimax_rfkill, bl);
1298 if (eeepc->wwan3g_rfkill) {
1299 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WWAN3G);
1300 rfkill_set_sw_state(eeepc->wwan3g_rfkill, bl);
1306 static const struct dev_pm_ops eeepc_pm_ops = {
1307 .thaw = eeepc_hotk_thaw,
1308 .restore = eeepc_hotk_restore,
1311 static struct platform_driver platform_driver = {
1312 .remove = __exit_p(eeepc_wmi_remove),
1314 .name = EEEPC_WMI_FILE,
1315 .owner = THIS_MODULE,
1316 .pm = &eeepc_pm_ops,
1320 static acpi_status __init eeepc_wmi_parse_device(acpi_handle handle, u32 level,
1321 void *context, void **retval)
1323 pr_warning("Found legacy ATKD device (%s)", EEEPC_ACPI_HID);
1324 *(bool *)context = true;
1325 return AE_CTRL_TERMINATE;
1328 static int __init eeepc_wmi_check_atkd(void)
1333 status = acpi_get_devices(EEEPC_ACPI_HID, eeepc_wmi_parse_device,
1336 if (ACPI_FAILURE(status) || !found)
1341 static int __init eeepc_wmi_probe(struct platform_device *pdev)
1343 if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
1344 !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
1345 pr_warning("No known WMI GUID found\n");
1349 if (eeepc_wmi_check_atkd()) {
1350 pr_warning("WMI device present, but legacy ATKD device is also "
1351 "present and enabled.");
1352 pr_warning("You probably booted with acpi_osi=\"Linux\" or "
1353 "acpi_osi=\"!Windows 2009\"");
1354 pr_warning("Can't load eeepc-wmi, use default acpi_osi "
1355 "(preferred) or eeepc-laptop");
1359 return eeepc_wmi_add(pdev);
1362 static struct platform_device *platform_device;
1364 static int __init eeepc_wmi_init(void)
1366 platform_device = platform_create_bundle(&platform_driver,
1369 if (IS_ERR(platform_device))
1370 return PTR_ERR(platform_device);
1374 static void __exit eeepc_wmi_exit(void)
1376 platform_device_unregister(platform_device);
1377 platform_driver_unregister(&platform_driver);
1380 module_init(eeepc_wmi_init);
1381 module_exit(eeepc_wmi_exit);