blob: 34e6b4d83a9300c4bbb1bf2de3069c3ad210f518 [file] [log] [blame]
Yong Wangee027e42010-03-21 10:26:34 +08001/*
Corentin Charye12e6d92011-02-26 10:20:31 +01002 * Asus PC WMI hotkey driver
Yong Wangee027e42010-03-21 10:26:34 +08003 *
4 * Copyright(C) 2010 Intel Corporation.
Corentin Chary57ab7da2011-02-26 10:20:32 +01005 * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
Yong Wangee027e42010-03-21 10:26:34 +08006 *
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
Yong Wang81248882010-04-11 09:26:33 +080027#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
Yong Wangee027e42010-03-21 10:26:34 +080029#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/types.h>
Tejun Heoa32f3922010-04-05 11:37:59 +090033#include <linux/slab.h>
Yong Wangee027e42010-03-21 10:26:34 +080034#include <linux/input.h>
35#include <linux/input/sparse-keymap.h>
Yong Wang3d7b1652010-04-11 09:27:54 +080036#include <linux/fb.h>
37#include <linux/backlight.h>
Corentin Chary084fca62010-11-29 08:14:06 +010038#include <linux/leds.h>
Corentin Charyba48fdb2010-11-29 08:14:07 +010039#include <linux/rfkill.h>
Corentin Charyafa7c882011-02-06 13:28:28 +010040#include <linux/pci.h>
41#include <linux/pci_hotplug.h>
Corentin Chary8c1b2d82010-11-29 08:14:09 +010042#include <linux/debugfs.h>
43#include <linux/seq_file.h>
Yong Wang45f2c692010-04-11 09:27:19 +080044#include <linux/platform_device.h>
Yong Wangee027e42010-03-21 10:26:34 +080045#include <acpi/acpi_bus.h>
46#include <acpi/acpi_drivers.h>
47
Corentin Charye12e6d92011-02-26 10:20:31 +010048#include "asus-wmi.h"
Yong Wang45f2c692010-04-11 09:27:19 +080049
Corentin Charye12e6d92011-02-26 10:20:31 +010050MODULE_AUTHOR("Corentin Chary <corentincj@iksaif.net>, "
51 "Yong Wang <yong.y.wang@intel.com>");
52MODULE_DESCRIPTION("Asus Generic WMI Driver");
Yong Wangee027e42010-03-21 10:26:34 +080053MODULE_LICENSE("GPL");
54
Corentin Charye12e6d92011-02-26 10:20:31 +010055#define to_platform_driver(drv) \
56 (container_of((drv), struct platform_driver, driver))
Corentin Charyd358cb52010-11-29 08:14:14 +010057
Corentin Charye12e6d92011-02-26 10:20:31 +010058#define to_asus_wmi_driver(pdrv) \
59 (container_of((pdrv), struct asus_wmi_driver, platform_driver))
Yong Wangee027e42010-03-21 10:26:34 +080060
Corentin Charye12e6d92011-02-26 10:20:31 +010061#define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
Yong Wangee027e42010-03-21 10:26:34 +080062
Corentin Chary33e0e6f2011-02-06 13:28:34 +010063#define NOTIFY_BRNUP_MIN 0x11
64#define NOTIFY_BRNUP_MAX 0x1f
65#define NOTIFY_BRNDOWN_MIN 0x20
66#define NOTIFY_BRNDOWN_MAX 0x2e
Yong Wangee027e42010-03-21 10:26:34 +080067
Corentin Chary43815942011-02-06 13:28:43 +010068/* WMI Methods */
Corentin Charye12e6d92011-02-26 10:20:31 +010069#define ASUS_WMI_METHODID_DSTS 0x53544344
70#define ASUS_WMI_METHODID_DEVS 0x53564544
71#define ASUS_WMI_METHODID_CFVS 0x53564643
Yong Wang3d7b1652010-04-11 09:27:54 +080072
Corentin Charyd33da3b2011-02-26 10:20:35 +010073#define ASUS_WMI_UNSUPPORTED_METHOD 0xFFFFFFFE
74
Corentin Chary43815942011-02-06 13:28:43 +010075/* Wireless */
Corentin Charye12e6d92011-02-26 10:20:31 +010076#define ASUS_WMI_DEVID_WLAN 0x00010011
77#define ASUS_WMI_DEVID_BLUETOOTH 0x00010013
78#define ASUS_WMI_DEVID_WIMAX 0x00010017
79#define ASUS_WMI_DEVID_WWAN3G 0x00010019
Corentin Chary43815942011-02-06 13:28:43 +010080
81/* Backlight and Brightness */
Corentin Charye12e6d92011-02-26 10:20:31 +010082#define ASUS_WMI_DEVID_BACKLIGHT 0x00050011
83#define ASUS_WMI_DEVID_BRIGHTNESS 0x00050012
Corentin Chary43815942011-02-06 13:28:43 +010084
85/* Misc */
Corentin Charye12e6d92011-02-26 10:20:31 +010086#define ASUS_WMI_DEVID_CAMERA 0x00060013
Corentin Chary43815942011-02-06 13:28:43 +010087
88/* Storage */
Corentin Charye12e6d92011-02-26 10:20:31 +010089#define ASUS_WMI_DEVID_CARDREADER 0x00080013
Corentin Chary43815942011-02-06 13:28:43 +010090
91/* Input */
Corentin Chary57ab7da2011-02-26 10:20:32 +010092#define ASUS_WMI_DEVID_TOUCHPAD 0x00100011
Corentin Charye12e6d92011-02-26 10:20:31 +010093#define ASUS_WMI_DEVID_TOUCHPAD_LED 0x00100012
Yong Wang3d7b1652010-04-11 09:27:54 +080094
Corentin Chary43815942011-02-06 13:28:43 +010095/* DSTS masks */
Corentin Charye12e6d92011-02-26 10:20:31 +010096#define ASUS_WMI_DSTS_STATUS_BIT 0x00000001
Corentin Charya75fe0d2011-02-26 10:20:34 +010097#define ASUS_WMI_DSTS_UNKNOWN_BIT 0x00000002
Corentin Charye12e6d92011-02-26 10:20:31 +010098#define ASUS_WMI_DSTS_PRESENCE_BIT 0x00010000
99#define ASUS_WMI_DSTS_BRIGHTNESS_MASK 0x000000FF
100#define ASUS_WMI_DSTS_MAX_BRIGTH_MASK 0x0000FF00
Yong Wangee027e42010-03-21 10:26:34 +0800101
Yong Wang3d7b1652010-04-11 09:27:54 +0800102struct bios_args {
Corentin Charyd33da3b2011-02-26 10:20:35 +0100103 u32 arg0;
104 u32 arg1;
105} __packed;
Yong Wang3d7b1652010-04-11 09:27:54 +0800106
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100107/*
Corentin Charye12e6d92011-02-26 10:20:31 +0100108 * <platform>/ - debugfs root directory
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100109 * dev_id - current dev_id
110 * ctrl_param - current ctrl_param
111 * devs - call DEVS(dev_id, ctrl_param) and print result
112 * dsts - call DSTS(dev_id) and print result
113 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100114struct asus_wmi_debug {
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100115 struct dentry *root;
116 u32 dev_id;
117 u32 ctrl_param;
118};
119
Corentin Charya7ce3f02011-02-26 10:20:33 +0100120struct asus_rfkill {
121 struct asus_wmi *asus;
122 struct rfkill *rfkill;
123 u32 dev_id;
124};
125
Corentin Charye12e6d92011-02-26 10:20:31 +0100126struct asus_wmi {
Yong Wang81248882010-04-11 09:26:33 +0800127 struct input_dev *inputdev;
Yong Wang3d7b1652010-04-11 09:27:54 +0800128 struct backlight_device *backlight_device;
Corentin Chary27c136c2010-11-29 08:14:05 +0100129 struct platform_device *platform_device;
Corentin Chary084fca62010-11-29 08:14:06 +0100130
131 struct led_classdev tpd_led;
132 int tpd_led_wk;
133 struct workqueue_struct *led_workqueue;
134 struct work_struct tpd_led_work;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100135
Corentin Charya7ce3f02011-02-26 10:20:33 +0100136 struct asus_rfkill wlan;
137 struct asus_rfkill bluetooth;
138 struct asus_rfkill wimax;
139 struct asus_rfkill wwan3g;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100140
Corentin Charyafa7c882011-02-06 13:28:28 +0100141 struct hotplug_slot *hotplug_slot;
142 struct mutex hotplug_lock;
Corentin Chary279f8f92011-02-06 13:28:29 +0100143 struct mutex wmi_lock;
144 struct workqueue_struct *hotplug_workqueue;
145 struct work_struct hotplug_work;
Corentin Charyafa7c882011-02-06 13:28:28 +0100146
Corentin Charye12e6d92011-02-26 10:20:31 +0100147 struct asus_wmi_debug debug;
148
149 struct asus_wmi_driver *driver;
Yong Wang81248882010-04-11 09:26:33 +0800150};
151
Corentin Charye12e6d92011-02-26 10:20:31 +0100152static int asus_wmi_input_init(struct asus_wmi *asus)
Yong Wangee027e42010-03-21 10:26:34 +0800153{
154 int err;
155
Corentin Charye12e6d92011-02-26 10:20:31 +0100156 asus->inputdev = input_allocate_device();
157 if (!asus->inputdev)
Yong Wangee027e42010-03-21 10:26:34 +0800158 return -ENOMEM;
159
Corentin Charye12e6d92011-02-26 10:20:31 +0100160 asus->inputdev->name = asus->driver->input_phys;
161 asus->inputdev->phys = asus->driver->input_name;
162 asus->inputdev->id.bustype = BUS_HOST;
163 asus->inputdev->dev.parent = &asus->platform_device->dev;
Yong Wangee027e42010-03-21 10:26:34 +0800164
Corentin Charye12e6d92011-02-26 10:20:31 +0100165 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
Yong Wangee027e42010-03-21 10:26:34 +0800166 if (err)
167 goto err_free_dev;
168
Corentin Charye12e6d92011-02-26 10:20:31 +0100169 err = input_register_device(asus->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800170 if (err)
171 goto err_free_keymap;
172
173 return 0;
174
175err_free_keymap:
Corentin Charye12e6d92011-02-26 10:20:31 +0100176 sparse_keymap_free(asus->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800177err_free_dev:
Corentin Charye12e6d92011-02-26 10:20:31 +0100178 input_free_device(asus->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800179 return err;
180}
181
Corentin Charye12e6d92011-02-26 10:20:31 +0100182static void asus_wmi_input_exit(struct asus_wmi *asus)
Yong Wang81248882010-04-11 09:26:33 +0800183{
Corentin Charye12e6d92011-02-26 10:20:31 +0100184 if (asus->inputdev) {
185 sparse_keymap_free(asus->inputdev);
186 input_unregister_device(asus->inputdev);
Yong Wang81248882010-04-11 09:26:33 +0800187 }
188
Corentin Charye12e6d92011-02-26 10:20:31 +0100189 asus->inputdev = NULL;
Yong Wang81248882010-04-11 09:26:33 +0800190}
191
Corentin Charyd33da3b2011-02-26 10:20:35 +0100192static int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1,
193 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800194{
Corentin Charyd33da3b2011-02-26 10:20:35 +0100195 struct bios_args args = {
196 .arg0 = arg0,
197 .arg1 = arg1,
198 };
199 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
Yong Wang3d7b1652010-04-11 09:27:54 +0800200 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Yong Wang3d7b1652010-04-11 09:27:54 +0800201 acpi_status status;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100202 union acpi_object *obj;
Yong Wang3d7b1652010-04-11 09:27:54 +0800203 u32 tmp;
204
Corentin Charyd33da3b2011-02-26 10:20:35 +0100205 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 1, method_id,
Corentin Charyafa7c882011-02-06 13:28:28 +0100206 &input, &output);
Yong Wang3d7b1652010-04-11 09:27:54 +0800207
208 if (ACPI_FAILURE(status))
Corentin Charyd33da3b2011-02-26 10:20:35 +0100209 goto exit;
Yong Wang3d7b1652010-04-11 09:27:54 +0800210
211 obj = (union acpi_object *)output.pointer;
212 if (obj && obj->type == ACPI_TYPE_INTEGER)
Corentin Charye12e6d92011-02-26 10:20:31 +0100213 tmp = (u32) obj->integer.value;
Yong Wang3d7b1652010-04-11 09:27:54 +0800214 else
215 tmp = 0;
216
Corentin Chary2a3f0062010-11-29 08:14:10 +0100217 if (retval)
218 *retval = tmp;
Yong Wang3d7b1652010-04-11 09:27:54 +0800219
220 kfree(obj);
221
Corentin Charyd33da3b2011-02-26 10:20:35 +0100222exit:
223 if (ACPI_FAILURE(status))
224 return -EIO;
Yong Wang3d7b1652010-04-11 09:27:54 +0800225
Corentin Charyd33da3b2011-02-26 10:20:35 +0100226 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
227 return -ENODEV;
228
229 return 0;
Yong Wang3d7b1652010-04-11 09:27:54 +0800230}
231
Corentin Charyd33da3b2011-02-26 10:20:35 +0100232static int asus_wmi_get_devstate(u32 dev_id, u32 *retval)
233{
234 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS, dev_id,
235 0, retval);
236}
237
238static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
Corentin Charye12e6d92011-02-26 10:20:31 +0100239 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800240{
Corentin Charyd33da3b2011-02-26 10:20:35 +0100241 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
242 ctrl_param, retval);
Yong Wang3d7b1652010-04-11 09:27:54 +0800243}
244
Corentin Chary5c956382011-02-06 13:28:31 +0100245/* Helper for special devices with magic return codes */
Corentin Charye12e6d92011-02-26 10:20:31 +0100246static int asus_wmi_get_devstate_bits(u32 dev_id, u32 mask)
Corentin Chary5c956382011-02-06 13:28:31 +0100247{
248 u32 retval = 0;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100249 int err;
Corentin Chary5c956382011-02-06 13:28:31 +0100250
Corentin Charyd33da3b2011-02-26 10:20:35 +0100251 err = asus_wmi_get_devstate(dev_id, &retval);
Corentin Chary5c956382011-02-06 13:28:31 +0100252
Corentin Charyd33da3b2011-02-26 10:20:35 +0100253 if (err < 0)
254 return err;
Corentin Chary5c956382011-02-06 13:28:31 +0100255
Corentin Charye12e6d92011-02-26 10:20:31 +0100256 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
Corentin Chary5c956382011-02-06 13:28:31 +0100257 return -ENODEV;
258
Corentin Charya75fe0d2011-02-26 10:20:34 +0100259 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
260 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
261 return -ENODEV;
262 }
263
Corentin Charyb7187262011-02-06 13:28:39 +0100264 return retval & mask;
265}
266
Corentin Charye12e6d92011-02-26 10:20:31 +0100267static int asus_wmi_get_devstate_simple(u32 dev_id)
Corentin Charyb7187262011-02-06 13:28:39 +0100268{
Corentin Charye12e6d92011-02-26 10:20:31 +0100269 return asus_wmi_get_devstate_bits(dev_id, ASUS_WMI_DSTS_STATUS_BIT);
Corentin Chary5c956382011-02-06 13:28:31 +0100270}
271
Corentin Chary084fca62010-11-29 08:14:06 +0100272/*
273 * LEDs
274 */
275/*
276 * These functions actually update the LED's, and are called from a
277 * workqueue. By doing this as separate work rather than when the LED
Corentin Charye12e6d92011-02-26 10:20:31 +0100278 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
Corentin Chary084fca62010-11-29 08:14:06 +0100279 * potentially bad time, such as a timer interrupt.
280 */
281static void tpd_led_update(struct work_struct *work)
282{
283 int ctrl_param;
Corentin Charye12e6d92011-02-26 10:20:31 +0100284 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100285
Corentin Charye12e6d92011-02-26 10:20:31 +0100286 asus = container_of(work, struct asus_wmi, tpd_led_work);
Corentin Chary084fca62010-11-29 08:14:06 +0100287
Corentin Charye12e6d92011-02-26 10:20:31 +0100288 ctrl_param = asus->tpd_led_wk;
289 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
Corentin Chary084fca62010-11-29 08:14:06 +0100290}
291
292static void tpd_led_set(struct led_classdev *led_cdev,
293 enum led_brightness value)
294{
Corentin Charye12e6d92011-02-26 10:20:31 +0100295 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100296
Corentin Charye12e6d92011-02-26 10:20:31 +0100297 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
Corentin Chary084fca62010-11-29 08:14:06 +0100298
Corentin Charye12e6d92011-02-26 10:20:31 +0100299 asus->tpd_led_wk = !!value;
300 queue_work(asus->led_workqueue, &asus->tpd_led_work);
Corentin Chary084fca62010-11-29 08:14:06 +0100301}
302
Corentin Charye12e6d92011-02-26 10:20:31 +0100303static int read_tpd_led_state(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100304{
Corentin Charye12e6d92011-02-26 10:20:31 +0100305 return asus_wmi_get_devstate_simple(ASUS_WMI_DEVID_TOUCHPAD_LED);
Corentin Chary084fca62010-11-29 08:14:06 +0100306}
307
308static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
309{
Corentin Charye12e6d92011-02-26 10:20:31 +0100310 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100311
Corentin Charye12e6d92011-02-26 10:20:31 +0100312 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
Corentin Chary084fca62010-11-29 08:14:06 +0100313
Corentin Charye12e6d92011-02-26 10:20:31 +0100314 return read_tpd_led_state(asus);
Corentin Chary084fca62010-11-29 08:14:06 +0100315}
316
Corentin Charye12e6d92011-02-26 10:20:31 +0100317static int asus_wmi_led_init(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100318{
319 int rv;
320
Corentin Charye12e6d92011-02-26 10:20:31 +0100321 if (read_tpd_led_state(asus) < 0)
Corentin Chary084fca62010-11-29 08:14:06 +0100322 return 0;
323
Corentin Charye12e6d92011-02-26 10:20:31 +0100324 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
325 if (!asus->led_workqueue)
Corentin Chary084fca62010-11-29 08:14:06 +0100326 return -ENOMEM;
Corentin Charye12e6d92011-02-26 10:20:31 +0100327 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
Corentin Chary084fca62010-11-29 08:14:06 +0100328
Corentin Charye12e6d92011-02-26 10:20:31 +0100329 asus->tpd_led.name = "asus::touchpad";
330 asus->tpd_led.brightness_set = tpd_led_set;
331 asus->tpd_led.brightness_get = tpd_led_get;
332 asus->tpd_led.max_brightness = 1;
Corentin Chary084fca62010-11-29 08:14:06 +0100333
Corentin Charye12e6d92011-02-26 10:20:31 +0100334 rv = led_classdev_register(&asus->platform_device->dev, &asus->tpd_led);
Corentin Chary084fca62010-11-29 08:14:06 +0100335 if (rv) {
Corentin Charye12e6d92011-02-26 10:20:31 +0100336 destroy_workqueue(asus->led_workqueue);
Corentin Chary084fca62010-11-29 08:14:06 +0100337 return rv;
338 }
339
340 return 0;
341}
342
Corentin Charye12e6d92011-02-26 10:20:31 +0100343static void asus_wmi_led_exit(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100344{
Corentin Charye12e6d92011-02-26 10:20:31 +0100345 if (asus->tpd_led.dev)
346 led_classdev_unregister(&asus->tpd_led);
347 if (asus->led_workqueue)
348 destroy_workqueue(asus->led_workqueue);
Corentin Chary084fca62010-11-29 08:14:06 +0100349}
350
351/*
Corentin Charyafa7c882011-02-06 13:28:28 +0100352 * PCI hotplug (for wlan rfkill)
353 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100354static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100355{
Corentin Charye12e6d92011-02-26 10:20:31 +0100356 int result = asus_wmi_get_devstate_simple(ASUS_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100357
Corentin Chary5c956382011-02-06 13:28:31 +0100358 if (result < 0)
Corentin Charyafa7c882011-02-06 13:28:28 +0100359 return false;
Corentin Chary5c956382011-02-06 13:28:31 +0100360 return !result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100361}
362
Corentin Charye12e6d92011-02-26 10:20:31 +0100363static void asus_rfkill_hotplug(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100364{
365 struct pci_dev *dev;
366 struct pci_bus *bus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100367 bool blocked;
Corentin Charyafa7c882011-02-06 13:28:28 +0100368 bool absent;
369 u32 l;
370
Corentin Charye12e6d92011-02-26 10:20:31 +0100371 mutex_lock(&asus->wmi_lock);
372 blocked = asus_wlan_rfkill_blocked(asus);
373 mutex_unlock(&asus->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100374
Corentin Charye12e6d92011-02-26 10:20:31 +0100375 mutex_lock(&asus->hotplug_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100376
Corentin Charya7ce3f02011-02-26 10:20:33 +0100377 if (asus->wlan.rfkill)
378 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
Corentin Chary279f8f92011-02-06 13:28:29 +0100379
Corentin Charye12e6d92011-02-26 10:20:31 +0100380 if (asus->hotplug_slot) {
Corentin Charyafa7c882011-02-06 13:28:28 +0100381 bus = pci_find_bus(0, 1);
382 if (!bus) {
383 pr_warning("Unable to find PCI bus 1?\n");
384 goto out_unlock;
385 }
386
387 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
388 pr_err("Unable to read PCI config space?\n");
389 goto out_unlock;
390 }
391 absent = (l == 0xffffffff);
392
393 if (blocked != absent) {
394 pr_warning("BIOS says wireless lan is %s, "
Corentin Charye12e6d92011-02-26 10:20:31 +0100395 "but the pci device is %s\n",
396 blocked ? "blocked" : "unblocked",
397 absent ? "absent" : "present");
Corentin Charyafa7c882011-02-06 13:28:28 +0100398 pr_warning("skipped wireless hotplug as probably "
Corentin Charye12e6d92011-02-26 10:20:31 +0100399 "inappropriate for this model\n");
Corentin Charyafa7c882011-02-06 13:28:28 +0100400 goto out_unlock;
401 }
402
403 if (!blocked) {
404 dev = pci_get_slot(bus, 0);
405 if (dev) {
406 /* Device already present */
407 pci_dev_put(dev);
408 goto out_unlock;
409 }
410 dev = pci_scan_single_device(bus, 0);
411 if (dev) {
412 pci_bus_assign_resources(bus);
413 if (pci_bus_add_device(dev))
414 pr_err("Unable to hotplug wifi\n");
415 }
416 } else {
417 dev = pci_get_slot(bus, 0);
418 if (dev) {
419 pci_remove_bus_device(dev);
420 pci_dev_put(dev);
421 }
422 }
423 }
424
425out_unlock:
Corentin Charye12e6d92011-02-26 10:20:31 +0100426 mutex_unlock(&asus->hotplug_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100427}
428
Corentin Charye12e6d92011-02-26 10:20:31 +0100429static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
Corentin Charyafa7c882011-02-06 13:28:28 +0100430{
Corentin Charye12e6d92011-02-26 10:20:31 +0100431 struct asus_wmi *asus = data;
Corentin Charyafa7c882011-02-06 13:28:28 +0100432
433 if (event != ACPI_NOTIFY_BUS_CHECK)
434 return;
435
Corentin Chary279f8f92011-02-06 13:28:29 +0100436 /*
Corentin Charye12e6d92011-02-26 10:20:31 +0100437 * We can't call directly asus_rfkill_hotplug because most
Corentin Chary279f8f92011-02-06 13:28:29 +0100438 * of the time WMBC is still being executed and not reetrant.
439 * There is currently no way to tell ACPICA that we want this
Corentin Charye12e6d92011-02-26 10:20:31 +0100440 * method to be serialized, we schedule a asus_rfkill_hotplug
Corentin Chary279f8f92011-02-06 13:28:29 +0100441 * call later, in a safer context.
442 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100443 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
Corentin Charyafa7c882011-02-06 13:28:28 +0100444}
445
Corentin Charye12e6d92011-02-26 10:20:31 +0100446static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
Corentin Charyafa7c882011-02-06 13:28:28 +0100447{
448 acpi_status status;
449 acpi_handle handle;
450
451 status = acpi_get_handle(NULL, node, &handle);
452
453 if (ACPI_SUCCESS(status)) {
454 status = acpi_install_notify_handler(handle,
455 ACPI_SYSTEM_NOTIFY,
Corentin Charye12e6d92011-02-26 10:20:31 +0100456 asus_rfkill_notify, asus);
Corentin Charyafa7c882011-02-06 13:28:28 +0100457 if (ACPI_FAILURE(status))
458 pr_warning("Failed to register notify on %s\n", node);
459 } else
460 return -ENODEV;
461
462 return 0;
463}
464
Corentin Charye12e6d92011-02-26 10:20:31 +0100465static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
Corentin Charyafa7c882011-02-06 13:28:28 +0100466{
467 acpi_status status = AE_OK;
468 acpi_handle handle;
469
470 status = acpi_get_handle(NULL, node, &handle);
471
472 if (ACPI_SUCCESS(status)) {
473 status = acpi_remove_notify_handler(handle,
Corentin Charye12e6d92011-02-26 10:20:31 +0100474 ACPI_SYSTEM_NOTIFY,
475 asus_rfkill_notify);
Corentin Charyafa7c882011-02-06 13:28:28 +0100476 if (ACPI_FAILURE(status))
477 pr_err("Error removing rfkill notify handler %s\n",
Corentin Charye12e6d92011-02-26 10:20:31 +0100478 node);
Corentin Charyafa7c882011-02-06 13:28:28 +0100479 }
480}
481
Corentin Charye12e6d92011-02-26 10:20:31 +0100482static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
483 u8 *value)
Corentin Charyafa7c882011-02-06 13:28:28 +0100484{
Corentin Charye12e6d92011-02-26 10:20:31 +0100485 int result = asus_wmi_get_devstate_simple(ASUS_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100486
Corentin Chary5c956382011-02-06 13:28:31 +0100487 if (result < 0)
488 return result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100489
Corentin Chary5c956382011-02-06 13:28:31 +0100490 *value = !!result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100491 return 0;
492}
493
Corentin Charye12e6d92011-02-26 10:20:31 +0100494static void asus_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
Corentin Charyafa7c882011-02-06 13:28:28 +0100495{
496 kfree(hotplug_slot->info);
497 kfree(hotplug_slot);
498}
499
Corentin Charye12e6d92011-02-26 10:20:31 +0100500static struct hotplug_slot_ops asus_hotplug_slot_ops = {
Corentin Charyafa7c882011-02-06 13:28:28 +0100501 .owner = THIS_MODULE,
Corentin Charye12e6d92011-02-26 10:20:31 +0100502 .get_adapter_status = asus_get_adapter_status,
503 .get_power_status = asus_get_adapter_status,
Corentin Charyafa7c882011-02-06 13:28:28 +0100504};
505
Corentin Charye12e6d92011-02-26 10:20:31 +0100506static void asus_hotplug_work(struct work_struct *work)
Corentin Chary279f8f92011-02-06 13:28:29 +0100507{
Corentin Charye12e6d92011-02-26 10:20:31 +0100508 struct asus_wmi *asus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100509
Corentin Charye12e6d92011-02-26 10:20:31 +0100510 asus = container_of(work, struct asus_wmi, hotplug_work);
511 asus_rfkill_hotplug(asus);
Corentin Chary279f8f92011-02-06 13:28:29 +0100512}
513
Corentin Charye12e6d92011-02-26 10:20:31 +0100514static int asus_setup_pci_hotplug(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100515{
516 int ret = -ENOMEM;
517 struct pci_bus *bus = pci_find_bus(0, 1);
518
519 if (!bus) {
520 pr_err("Unable to find wifi PCI bus\n");
521 return -ENODEV;
522 }
523
Corentin Charye12e6d92011-02-26 10:20:31 +0100524 asus->hotplug_workqueue =
525 create_singlethread_workqueue("hotplug_workqueue");
526 if (!asus->hotplug_workqueue)
Corentin Chary279f8f92011-02-06 13:28:29 +0100527 goto error_workqueue;
528
Corentin Charye12e6d92011-02-26 10:20:31 +0100529 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
Corentin Chary279f8f92011-02-06 13:28:29 +0100530
Corentin Charye12e6d92011-02-26 10:20:31 +0100531 asus->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
532 if (!asus->hotplug_slot)
Corentin Charyafa7c882011-02-06 13:28:28 +0100533 goto error_slot;
534
Corentin Charye12e6d92011-02-26 10:20:31 +0100535 asus->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
536 GFP_KERNEL);
537 if (!asus->hotplug_slot->info)
Corentin Charyafa7c882011-02-06 13:28:28 +0100538 goto error_info;
539
Corentin Charye12e6d92011-02-26 10:20:31 +0100540 asus->hotplug_slot->private = asus;
541 asus->hotplug_slot->release = &asus_cleanup_pci_hotplug;
542 asus->hotplug_slot->ops = &asus_hotplug_slot_ops;
543 asus_get_adapter_status(asus->hotplug_slot,
544 &asus->hotplug_slot->info->adapter_status);
Corentin Charyafa7c882011-02-06 13:28:28 +0100545
Corentin Charye12e6d92011-02-26 10:20:31 +0100546 ret = pci_hp_register(asus->hotplug_slot, bus, 0, "asus-wifi");
Corentin Charyafa7c882011-02-06 13:28:28 +0100547 if (ret) {
548 pr_err("Unable to register hotplug slot - %d\n", ret);
549 goto error_register;
550 }
551
552 return 0;
553
554error_register:
Corentin Charye12e6d92011-02-26 10:20:31 +0100555 kfree(asus->hotplug_slot->info);
Corentin Charyafa7c882011-02-06 13:28:28 +0100556error_info:
Corentin Charye12e6d92011-02-26 10:20:31 +0100557 kfree(asus->hotplug_slot);
558 asus->hotplug_slot = NULL;
Corentin Charyafa7c882011-02-06 13:28:28 +0100559error_slot:
Corentin Charye12e6d92011-02-26 10:20:31 +0100560 destroy_workqueue(asus->hotplug_workqueue);
Corentin Chary279f8f92011-02-06 13:28:29 +0100561error_workqueue:
Corentin Charyafa7c882011-02-06 13:28:28 +0100562 return ret;
563}
564
565/*
Corentin Charyba48fdb2010-11-29 08:14:07 +0100566 * Rfkill devices
567 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100568static int asus_rfkill_set(void *data, bool blocked)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100569{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100570 struct asus_rfkill *priv = data;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100571 u32 ctrl_param = !blocked;
572
Corentin Charyd33da3b2011-02-26 10:20:35 +0100573 return asus_wmi_set_devstate(priv->dev_id, ctrl_param, NULL);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100574}
575
Corentin Charye12e6d92011-02-26 10:20:31 +0100576static void asus_rfkill_query(struct rfkill *rfkill, void *data)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100577{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100578 struct asus_rfkill *priv = data;
Corentin Chary5c956382011-02-06 13:28:31 +0100579 int result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100580
Corentin Charya7ce3f02011-02-26 10:20:33 +0100581 result = asus_wmi_get_devstate_simple(priv->dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100582
Corentin Chary5c956382011-02-06 13:28:31 +0100583 if (result < 0)
Corentin Charye12e6d92011-02-26 10:20:31 +0100584 return;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100585
Corentin Charya7ce3f02011-02-26 10:20:33 +0100586 rfkill_set_sw_state(priv->rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100587}
588
Corentin Charye12e6d92011-02-26 10:20:31 +0100589static int asus_rfkill_wlan_set(void *data, bool blocked)
Corentin Chary279f8f92011-02-06 13:28:29 +0100590{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100591 struct asus_rfkill *priv = data;
592 struct asus_wmi *asus = priv->asus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100593 int ret;
594
595 /*
596 * This handler is enabled only if hotplug is enabled.
Corentin Charye12e6d92011-02-26 10:20:31 +0100597 * In this case, the asus_wmi_set_devstate() will
Corentin Chary279f8f92011-02-06 13:28:29 +0100598 * trigger a wmi notification and we need to wait
599 * this call to finish before being able to call
600 * any wmi method
601 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100602 mutex_lock(&asus->wmi_lock);
Corentin Charya7ce3f02011-02-26 10:20:33 +0100603 ret = asus_rfkill_set(data, blocked);
Corentin Charye12e6d92011-02-26 10:20:31 +0100604 mutex_unlock(&asus->wmi_lock);
Corentin Chary279f8f92011-02-06 13:28:29 +0100605 return ret;
606}
607
Corentin Charye12e6d92011-02-26 10:20:31 +0100608static const struct rfkill_ops asus_rfkill_wlan_ops = {
609 .set_block = asus_rfkill_wlan_set,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100610 .query = asus_rfkill_query,
Corentin Chary279f8f92011-02-06 13:28:29 +0100611};
612
Corentin Charye12e6d92011-02-26 10:20:31 +0100613static const struct rfkill_ops asus_rfkill_ops = {
614 .set_block = asus_rfkill_set,
615 .query = asus_rfkill_query,
Corentin Charyba48fdb2010-11-29 08:14:07 +0100616};
617
Corentin Charye12e6d92011-02-26 10:20:31 +0100618static int asus_new_rfkill(struct asus_wmi *asus,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100619 struct asus_rfkill *arfkill,
Corentin Charye12e6d92011-02-26 10:20:31 +0100620 const char *name, enum rfkill_type type, int dev_id)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100621{
Corentin Charye12e6d92011-02-26 10:20:31 +0100622 int result = asus_wmi_get_devstate_simple(dev_id);
Corentin Charya7ce3f02011-02-26 10:20:33 +0100623 struct rfkill **rfkill = &arfkill->rfkill;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100624
Corentin Chary5c956382011-02-06 13:28:31 +0100625 if (result < 0)
626 return result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100627
Corentin Charya7ce3f02011-02-26 10:20:33 +0100628 arfkill->dev_id = dev_id;
629 arfkill->asus = asus;
630
Corentin Charye12e6d92011-02-26 10:20:31 +0100631 if (dev_id == ASUS_WMI_DEVID_WLAN && asus->driver->hotplug_wireless)
632 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100633 &asus_rfkill_wlan_ops, arfkill);
Corentin Chary279f8f92011-02-06 13:28:29 +0100634 else
Corentin Charye12e6d92011-02-26 10:20:31 +0100635 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100636 &asus_rfkill_ops, arfkill);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100637
638 if (!*rfkill)
639 return -EINVAL;
640
Corentin Chary5c956382011-02-06 13:28:31 +0100641 rfkill_init_sw_state(*rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100642 result = rfkill_register(*rfkill);
643 if (result) {
644 rfkill_destroy(*rfkill);
645 *rfkill = NULL;
646 return result;
647 }
648 return 0;
649}
650
Corentin Charye12e6d92011-02-26 10:20:31 +0100651static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100652{
Corentin Charye12e6d92011-02-26 10:20:31 +0100653 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
654 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
655 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
Corentin Charya7ce3f02011-02-26 10:20:33 +0100656 if (asus->wlan.rfkill) {
657 rfkill_unregister(asus->wlan.rfkill);
658 rfkill_destroy(asus->wlan.rfkill);
659 asus->wlan.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100660 }
Corentin Charyafa7c882011-02-06 13:28:28 +0100661 /*
662 * Refresh pci hotplug in case the rfkill state was changed after
Corentin Charye12e6d92011-02-26 10:20:31 +0100663 * asus_unregister_rfkill_notifier()
Corentin Charyafa7c882011-02-06 13:28:28 +0100664 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100665 asus_rfkill_hotplug(asus);
666 if (asus->hotplug_slot)
667 pci_hp_deregister(asus->hotplug_slot);
668 if (asus->hotplug_workqueue)
669 destroy_workqueue(asus->hotplug_workqueue);
Corentin Charyafa7c882011-02-06 13:28:28 +0100670
Corentin Charya7ce3f02011-02-26 10:20:33 +0100671 if (asus->bluetooth.rfkill) {
672 rfkill_unregister(asus->bluetooth.rfkill);
673 rfkill_destroy(asus->bluetooth.rfkill);
674 asus->bluetooth.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100675 }
Corentin Charya7ce3f02011-02-26 10:20:33 +0100676 if (asus->wimax.rfkill) {
677 rfkill_unregister(asus->wimax.rfkill);
678 rfkill_destroy(asus->wimax.rfkill);
679 asus->wimax.rfkill = NULL;
Corentin Chary2e9e1592011-02-06 13:28:37 +0100680 }
Corentin Charya7ce3f02011-02-26 10:20:33 +0100681 if (asus->wwan3g.rfkill) {
682 rfkill_unregister(asus->wwan3g.rfkill);
683 rfkill_destroy(asus->wwan3g.rfkill);
684 asus->wwan3g.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100685 }
686}
687
Corentin Charye12e6d92011-02-26 10:20:31 +0100688static int asus_wmi_rfkill_init(struct asus_wmi *asus)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100689{
690 int result = 0;
691
Corentin Charye12e6d92011-02-26 10:20:31 +0100692 mutex_init(&asus->hotplug_lock);
693 mutex_init(&asus->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100694
Corentin Charya7ce3f02011-02-26 10:20:33 +0100695 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
696 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100697
698 if (result && result != -ENODEV)
699 goto exit;
700
Corentin Charya7ce3f02011-02-26 10:20:33 +0100701 result = asus_new_rfkill(asus, &asus->bluetooth,
Corentin Charye12e6d92011-02-26 10:20:31 +0100702 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
703 ASUS_WMI_DEVID_BLUETOOTH);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100704
705 if (result && result != -ENODEV)
706 goto exit;
707
Corentin Charya7ce3f02011-02-26 10:20:33 +0100708 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
709 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
Corentin Chary2e9e1592011-02-06 13:28:37 +0100710
711 if (result && result != -ENODEV)
712 goto exit;
713
Corentin Charya7ce3f02011-02-26 10:20:33 +0100714 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
715 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100716
717 if (result && result != -ENODEV)
718 goto exit;
719
Corentin Charye12e6d92011-02-26 10:20:31 +0100720 if (!asus->driver->hotplug_wireless)
Corentin Charyc14d4b82011-02-06 13:28:40 +0100721 goto exit;
722
Corentin Charye12e6d92011-02-26 10:20:31 +0100723 result = asus_setup_pci_hotplug(asus);
Corentin Charyafa7c882011-02-06 13:28:28 +0100724 /*
725 * If we get -EBUSY then something else is handling the PCI hotplug -
726 * don't fail in this case
727 */
728 if (result == -EBUSY)
729 result = 0;
730
Corentin Charye12e6d92011-02-26 10:20:31 +0100731 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
732 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
733 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
Corentin Charyafa7c882011-02-06 13:28:28 +0100734 /*
735 * Refresh pci hotplug in case the rfkill state was changed during
736 * setup.
737 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100738 asus_rfkill_hotplug(asus);
Corentin Charyafa7c882011-02-06 13:28:28 +0100739
Corentin Charyba48fdb2010-11-29 08:14:07 +0100740exit:
741 if (result && result != -ENODEV)
Corentin Charye12e6d92011-02-26 10:20:31 +0100742 asus_wmi_rfkill_exit(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100743
744 if (result == -ENODEV)
745 result = 0;
746
747 return result;
748}
749
750/*
Corentin Chary084fca62010-11-29 08:14:06 +0100751 * Backlight
752 */
Corentin Charyb7187262011-02-06 13:28:39 +0100753static int read_backlight_power(void)
754{
Corentin Charye12e6d92011-02-26 10:20:31 +0100755 int ret = asus_wmi_get_devstate_simple(ASUS_WMI_DEVID_BACKLIGHT);
Corentin Charyb7187262011-02-06 13:28:39 +0100756
757 if (ret < 0)
758 return ret;
759
760 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
761}
762
Yong Wang3d7b1652010-04-11 09:27:54 +0800763static int read_brightness(struct backlight_device *bd)
764{
Corentin Charydfed65d2010-11-29 08:14:12 +0100765 u32 retval;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100766 int err;
Yong Wang3d7b1652010-04-11 09:27:54 +0800767
Corentin Charyd33da3b2011-02-26 10:20:35 +0100768 err = asus_wmi_get_devstate(ASUS_WMI_DEVID_BRIGHTNESS, &retval);
Yong Wang3d7b1652010-04-11 09:27:54 +0800769
Corentin Charyd33da3b2011-02-26 10:20:35 +0100770 if (err < 0)
771 return err;
772
773 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
Yong Wang3d7b1652010-04-11 09:27:54 +0800774}
775
776static int update_bl_status(struct backlight_device *bd)
777{
Corentin Charydfed65d2010-11-29 08:14:12 +0100778 u32 ctrl_param;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100779 int power, err;
Yong Wang3d7b1652010-04-11 09:27:54 +0800780
781 ctrl_param = bd->props.brightness;
782
Corentin Charyd33da3b2011-02-26 10:20:35 +0100783 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
784 ctrl_param, NULL);
Yong Wang3d7b1652010-04-11 09:27:54 +0800785
Corentin Charyd33da3b2011-02-26 10:20:35 +0100786 if (err < 0)
787 return err;
Corentin Charyb7187262011-02-06 13:28:39 +0100788
789 power = read_backlight_power();
790 if (power != -ENODEV && bd->props.power != power) {
791 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
Corentin Charyd33da3b2011-02-26 10:20:35 +0100792 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
793 ctrl_param, NULL);
Corentin Charyb7187262011-02-06 13:28:39 +0100794 }
795 return 0;
Yong Wang3d7b1652010-04-11 09:27:54 +0800796}
797
Corentin Charye12e6d92011-02-26 10:20:31 +0100798static const struct backlight_ops asus_wmi_bl_ops = {
Yong Wang3d7b1652010-04-11 09:27:54 +0800799 .get_brightness = read_brightness,
800 .update_status = update_bl_status,
801};
802
Corentin Charye12e6d92011-02-26 10:20:31 +0100803static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
Yong Wang3d7b1652010-04-11 09:27:54 +0800804{
Corentin Charye12e6d92011-02-26 10:20:31 +0100805 struct backlight_device *bd = asus->backlight_device;
Yong Wang3d7b1652010-04-11 09:27:54 +0800806 int old = bd->props.brightness;
Daniel Mackb7670ed2010-05-19 12:37:01 +0200807 int new = old;
Yong Wang3d7b1652010-04-11 09:27:54 +0800808
809 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
810 new = code - NOTIFY_BRNUP_MIN + 1;
811 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
812 new = code - NOTIFY_BRNDOWN_MIN;
813
814 bd->props.brightness = new;
815 backlight_update_status(bd);
816 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
817
818 return old;
819}
820
Corentin Charye12e6d92011-02-26 10:20:31 +0100821static int asus_wmi_backlight_init(struct asus_wmi *asus)
Yong Wang3d7b1652010-04-11 09:27:54 +0800822{
823 struct backlight_device *bd;
824 struct backlight_properties props;
Corentin Charyb7187262011-02-06 13:28:39 +0100825 int max;
826 int power;
827
Corentin Charye12e6d92011-02-26 10:20:31 +0100828 max = asus_wmi_get_devstate_bits(ASUS_WMI_DEVID_BRIGHTNESS,
829 ASUS_WMI_DSTS_MAX_BRIGTH_MASK);
Corentin Charyb7187262011-02-06 13:28:39 +0100830 power = read_backlight_power();
831
832 if (max < 0 && power < 0) {
833 /* Try to keep the original error */
834 if (max == -ENODEV && power == -ENODEV)
835 return -ENODEV;
836 if (max != -ENODEV)
837 return max;
838 else
839 return power;
840 }
841 if (max == -ENODEV)
842 max = 0;
843 if (power == -ENODEV)
844 power = FB_BLANK_UNBLANK;
Yong Wang3d7b1652010-04-11 09:27:54 +0800845
846 memset(&props, 0, sizeof(struct backlight_properties));
Corentin Charyb7187262011-02-06 13:28:39 +0100847 props.max_brightness = max;
Corentin Charye12e6d92011-02-26 10:20:31 +0100848 bd = backlight_device_register(asus->driver->name,
849 &asus->platform_device->dev, asus,
850 &asus_wmi_bl_ops, &props);
Yong Wang3d7b1652010-04-11 09:27:54 +0800851 if (IS_ERR(bd)) {
852 pr_err("Could not register backlight device\n");
853 return PTR_ERR(bd);
854 }
855
Corentin Charye12e6d92011-02-26 10:20:31 +0100856 asus->backlight_device = bd;
Yong Wang3d7b1652010-04-11 09:27:54 +0800857
858 bd->props.brightness = read_brightness(bd);
Corentin Charyb7187262011-02-06 13:28:39 +0100859 bd->props.power = power;
Yong Wang3d7b1652010-04-11 09:27:54 +0800860 backlight_update_status(bd);
861
862 return 0;
863}
864
Corentin Charye12e6d92011-02-26 10:20:31 +0100865static void asus_wmi_backlight_exit(struct asus_wmi *asus)
Yong Wang3d7b1652010-04-11 09:27:54 +0800866{
Corentin Charye12e6d92011-02-26 10:20:31 +0100867 if (asus->backlight_device)
868 backlight_device_unregister(asus->backlight_device);
Yong Wang3d7b1652010-04-11 09:27:54 +0800869
Corentin Charye12e6d92011-02-26 10:20:31 +0100870 asus->backlight_device = NULL;
Yong Wang3d7b1652010-04-11 09:27:54 +0800871}
872
Corentin Charye12e6d92011-02-26 10:20:31 +0100873static void asus_wmi_notify(u32 value, void *context)
Yong Wang3d7b1652010-04-11 09:27:54 +0800874{
Corentin Charye12e6d92011-02-26 10:20:31 +0100875 struct asus_wmi *asus = context;
Yong Wang3d7b1652010-04-11 09:27:54 +0800876 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
877 union acpi_object *obj;
878 acpi_status status;
879 int code;
880 int orig_code;
881
882 status = wmi_get_event_data(value, &response);
883 if (status != AE_OK) {
884 pr_err("bad event status 0x%x\n", status);
885 return;
886 }
887
888 obj = (union acpi_object *)response.pointer;
889
Corentin Chary57ab7da2011-02-26 10:20:32 +0100890 if (!obj || obj->type != ACPI_TYPE_INTEGER)
891 goto exit;
Yong Wang3d7b1652010-04-11 09:27:54 +0800892
Corentin Chary57ab7da2011-02-26 10:20:32 +0100893 code = obj->integer.value;
894 orig_code = code;
Yong Wang3d7b1652010-04-11 09:27:54 +0800895
Corentin Chary57ab7da2011-02-26 10:20:32 +0100896 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
897 code = NOTIFY_BRNUP_MIN;
898 else if (code >= NOTIFY_BRNDOWN_MIN &&
899 code <= NOTIFY_BRNDOWN_MAX)
900 code = NOTIFY_BRNDOWN_MIN;
Yong Wang3d7b1652010-04-11 09:27:54 +0800901
Corentin Chary57ab7da2011-02-26 10:20:32 +0100902 if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
903 if (!acpi_video_backlight_support())
904 asus_wmi_backlight_notify(asus, orig_code);
905 } else if (!sparse_keymap_report_event(asus->inputdev, code, 1, true))
906 pr_info("Unknown key %x pressed\n", code);
Yong Wang3d7b1652010-04-11 09:27:54 +0800907
Corentin Chary57ab7da2011-02-26 10:20:32 +0100908exit:
Yong Wang3d7b1652010-04-11 09:27:54 +0800909 kfree(obj);
910}
911
Corentin Chary9e1565b2011-02-06 13:28:36 +0100912/*
913 * Sys helpers
914 */
915static int parse_arg(const char *buf, unsigned long count, int *val)
916{
917 if (!count)
918 return 0;
919 if (sscanf(buf, "%i", val) != 1)
920 return -EINVAL;
921 return count;
922}
923
924static ssize_t store_sys_wmi(int devid, const char *buf, size_t count)
925{
Corentin Chary9e1565b2011-02-06 13:28:36 +0100926 u32 retval;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100927 int rv, err, value;
Corentin Chary9e1565b2011-02-06 13:28:36 +0100928
Corentin Charye12e6d92011-02-26 10:20:31 +0100929 value = asus_wmi_get_devstate_simple(devid);
930 if (value == -ENODEV) /* Check device presence */
Corentin Chary9e1565b2011-02-06 13:28:36 +0100931 return value;
932
933 rv = parse_arg(buf, count, &value);
Corentin Charyd33da3b2011-02-26 10:20:35 +0100934 err = asus_wmi_set_devstate(devid, value, &retval);
Corentin Chary9e1565b2011-02-06 13:28:36 +0100935
Corentin Charyd33da3b2011-02-26 10:20:35 +0100936 if (err < 0)
937 return err;
938
Corentin Chary9e1565b2011-02-06 13:28:36 +0100939 return rv;
940}
941
942static ssize_t show_sys_wmi(int devid, char *buf)
943{
Corentin Charye12e6d92011-02-26 10:20:31 +0100944 int value = asus_wmi_get_devstate_simple(devid);
Corentin Chary9e1565b2011-02-06 13:28:36 +0100945
946 if (value < 0)
947 return value;
948
949 return sprintf(buf, "%d\n", value);
950}
951
Corentin Charye12e6d92011-02-26 10:20:31 +0100952#define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
Corentin Chary9e1565b2011-02-06 13:28:36 +0100953 static ssize_t show_##_name(struct device *dev, \
954 struct device_attribute *attr, \
955 char *buf) \
956 { \
957 return show_sys_wmi(_cm, buf); \
958 } \
959 static ssize_t store_##_name(struct device *dev, \
960 struct device_attribute *attr, \
961 const char *buf, size_t count) \
962 { \
963 return store_sys_wmi(_cm, buf, count); \
964 } \
965 static struct device_attribute dev_attr_##_name = { \
966 .attr = { \
967 .name = __stringify(_name), \
968 .mode = _mode }, \
969 .show = show_##_name, \
970 .store = store_##_name, \
971 }
972
Corentin Charye12e6d92011-02-26 10:20:31 +0100973ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
974ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
975ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
Corentin Chary9e1565b2011-02-06 13:28:36 +0100976
Dmitry Torokhov67fa38e2010-11-03 11:14:01 -0700977static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
978 const char *buf, size_t count)
Chris Bagwell7f80d732010-10-11 18:47:18 -0500979{
980 int value;
Chris Bagwell7f80d732010-10-11 18:47:18 -0500981
982 if (!count || sscanf(buf, "%i", &value) != 1)
983 return -EINVAL;
984 if (value < 0 || value > 2)
985 return -EINVAL;
986
Corentin Charyd33da3b2011-02-26 10:20:35 +0100987 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
Chris Bagwell7f80d732010-10-11 18:47:18 -0500988}
989
990static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
991
Corentin Chary4e37b422010-11-29 08:14:08 +0100992static struct attribute *platform_attributes[] = {
993 &dev_attr_cpufv.attr,
Corentin Chary9e1565b2011-02-06 13:28:36 +0100994 &dev_attr_camera.attr,
995 &dev_attr_cardr.attr,
Corentin Chary4615bb62011-02-06 13:28:42 +0100996 &dev_attr_touchpad.attr,
Corentin Chary4e37b422010-11-29 08:14:08 +0100997 NULL
998};
999
Corentin Charye12e6d92011-02-26 10:20:31 +01001000static mode_t asus_sysfs_is_visible(struct kobject *kobj,
1001 struct attribute *attr, int idx)
Corentin Chary9e1565b2011-02-06 13:28:36 +01001002{
1003 bool supported = true;
1004 int devid = -1;
1005
1006 if (attr == &dev_attr_camera.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01001007 devid = ASUS_WMI_DEVID_CAMERA;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001008 else if (attr == &dev_attr_cardr.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01001009 devid = ASUS_WMI_DEVID_CARDREADER;
Corentin Chary4615bb62011-02-06 13:28:42 +01001010 else if (attr == &dev_attr_touchpad.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01001011 devid = ASUS_WMI_DEVID_TOUCHPAD;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001012
1013 if (devid != -1)
Corentin Charye12e6d92011-02-26 10:20:31 +01001014 supported = asus_wmi_get_devstate_simple(devid) != -ENODEV;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001015
1016 return supported ? attr->mode : 0;
1017}
1018
Corentin Chary4e37b422010-11-29 08:14:08 +01001019static struct attribute_group platform_attribute_group = {
Corentin Charye12e6d92011-02-26 10:20:31 +01001020 .is_visible = asus_sysfs_is_visible,
1021 .attrs = platform_attributes
Corentin Chary4e37b422010-11-29 08:14:08 +01001022};
1023
Corentin Charye12e6d92011-02-26 10:20:31 +01001024static void asus_wmi_sysfs_exit(struct platform_device *device)
Chris Bagwell7f80d732010-10-11 18:47:18 -05001025{
Corentin Chary4e37b422010-11-29 08:14:08 +01001026 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001027}
1028
Corentin Charye12e6d92011-02-26 10:20:31 +01001029static int asus_wmi_sysfs_init(struct platform_device *device)
Chris Bagwell7f80d732010-10-11 18:47:18 -05001030{
Corentin Chary4e37b422010-11-29 08:14:08 +01001031 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001032}
1033
Corentin Chary27c136c2010-11-29 08:14:05 +01001034/*
1035 * Platform device
1036 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001037static int __init asus_wmi_platform_init(struct asus_wmi *asus)
Corentin Chary27c136c2010-11-29 08:14:05 +01001038{
Corentin Charye12e6d92011-02-26 10:20:31 +01001039 return asus_wmi_sysfs_init(asus->platform_device);
Corentin Chary27c136c2010-11-29 08:14:05 +01001040}
1041
Corentin Charye12e6d92011-02-26 10:20:31 +01001042static void asus_wmi_platform_exit(struct asus_wmi *asus)
Corentin Chary27c136c2010-11-29 08:14:05 +01001043{
Corentin Charye12e6d92011-02-26 10:20:31 +01001044 asus_wmi_sysfs_exit(asus->platform_device);
Corentin Chary27c136c2010-11-29 08:14:05 +01001045}
1046
1047/*
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001048 * debugfs
1049 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001050struct asus_wmi_debugfs_node {
1051 struct asus_wmi *asus;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001052 char *name;
Corentin Charye12e6d92011-02-26 10:20:31 +01001053 int (*show) (struct seq_file *m, void *data);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001054};
1055
1056static int show_dsts(struct seq_file *m, void *data)
1057{
Corentin Charye12e6d92011-02-26 10:20:31 +01001058 struct asus_wmi *asus = m->private;
Corentin Charyd33da3b2011-02-26 10:20:35 +01001059 int err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001060 u32 retval = -1;
1061
Corentin Charyd33da3b2011-02-26 10:20:35 +01001062 err = asus_wmi_get_devstate(asus->debug.dev_id, &retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001063
Corentin Charyd33da3b2011-02-26 10:20:35 +01001064 if (err < 0)
1065 return err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001066
Corentin Charye12e6d92011-02-26 10:20:31 +01001067 seq_printf(m, "DSTS(%x) = %x\n", asus->debug.dev_id, retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001068
1069 return 0;
1070}
1071
1072static int show_devs(struct seq_file *m, void *data)
1073{
Corentin Charye12e6d92011-02-26 10:20:31 +01001074 struct asus_wmi *asus = m->private;
Corentin Charyd33da3b2011-02-26 10:20:35 +01001075 int err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001076 u32 retval = -1;
1077
Corentin Charyd33da3b2011-02-26 10:20:35 +01001078 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
1079 &retval);
1080
1081 if (err < 0)
1082 return err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001083
Corentin Charye12e6d92011-02-26 10:20:31 +01001084 seq_printf(m, "DEVS(%x, %x) = %x\n", asus->debug.dev_id,
1085 asus->debug.ctrl_param, retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001086
1087 return 0;
1088}
1089
Corentin Charye12e6d92011-02-26 10:20:31 +01001090static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
1091 {NULL, "devs", show_devs},
1092 {NULL, "dsts", show_dsts},
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001093};
1094
Corentin Charye12e6d92011-02-26 10:20:31 +01001095static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001096{
Corentin Charye12e6d92011-02-26 10:20:31 +01001097 struct asus_wmi_debugfs_node *node = inode->i_private;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001098
Corentin Charye12e6d92011-02-26 10:20:31 +01001099 return single_open(file, node->show, node->asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001100}
1101
Corentin Charye12e6d92011-02-26 10:20:31 +01001102static const struct file_operations asus_wmi_debugfs_io_ops = {
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001103 .owner = THIS_MODULE,
Corentin Charye12e6d92011-02-26 10:20:31 +01001104 .open = asus_wmi_debugfs_open,
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001105 .read = seq_read,
1106 .llseek = seq_lseek,
1107 .release = single_release,
1108};
1109
Corentin Charye12e6d92011-02-26 10:20:31 +01001110static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001111{
Corentin Charye12e6d92011-02-26 10:20:31 +01001112 debugfs_remove_recursive(asus->debug.root);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001113}
1114
Corentin Charye12e6d92011-02-26 10:20:31 +01001115static int asus_wmi_debugfs_init(struct asus_wmi *asus)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001116{
1117 struct dentry *dent;
1118 int i;
1119
Corentin Charye12e6d92011-02-26 10:20:31 +01001120 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
1121 if (!asus->debug.root) {
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001122 pr_err("failed to create debugfs directory");
1123 goto error_debugfs;
1124 }
1125
Corentin Charye12e6d92011-02-26 10:20:31 +01001126 dent = debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR,
1127 asus->debug.root, &asus->debug.dev_id);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001128 if (!dent)
1129 goto error_debugfs;
1130
Corentin Charye12e6d92011-02-26 10:20:31 +01001131 dent = debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR,
1132 asus->debug.root, &asus->debug.ctrl_param);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001133 if (!dent)
1134 goto error_debugfs;
1135
Corentin Charye12e6d92011-02-26 10:20:31 +01001136 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
1137 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001138
Corentin Charye12e6d92011-02-26 10:20:31 +01001139 node->asus = asus;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001140 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
Corentin Charye12e6d92011-02-26 10:20:31 +01001141 asus->debug.root, node,
1142 &asus_wmi_debugfs_io_ops);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001143 if (!dent) {
1144 pr_err("failed to create debug file: %s\n", node->name);
1145 goto error_debugfs;
1146 }
1147 }
1148
1149 return 0;
1150
1151error_debugfs:
Corentin Charye12e6d92011-02-26 10:20:31 +01001152 asus_wmi_debugfs_exit(asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001153 return -ENOMEM;
1154}
1155
1156/*
Corentin Chary27c136c2010-11-29 08:14:05 +01001157 * WMI Driver
1158 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001159static int asus_wmi_add(struct platform_device *pdev)
Corentin Charyafa7c882011-02-06 13:28:28 +01001160{
Corentin Charye12e6d92011-02-26 10:20:31 +01001161 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
1162 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
1163 struct asus_wmi *asus;
Yong Wangee027e42010-03-21 10:26:34 +08001164 acpi_status status;
Corentin Chary27c136c2010-11-29 08:14:05 +01001165 int err;
Yong Wangee027e42010-03-21 10:26:34 +08001166
Corentin Charye12e6d92011-02-26 10:20:31 +01001167 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
1168 if (!asus)
Corentin Charya04ce292011-02-06 13:28:33 +01001169 return -ENOMEM;
1170
Corentin Charye12e6d92011-02-26 10:20:31 +01001171 asus->driver = wdrv;
1172 asus->platform_device = pdev;
1173 wdrv->platform_device = pdev;
1174 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001175
Corentin Charye12e6d92011-02-26 10:20:31 +01001176 if (wdrv->quirks)
1177 wdrv->quirks(asus->driver);
Corentin Charyafa7c882011-02-06 13:28:28 +01001178
Corentin Charye12e6d92011-02-26 10:20:31 +01001179 err = asus_wmi_platform_init(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001180 if (err)
1181 goto fail_platform;
Yong Wang45f2c692010-04-11 09:27:19 +08001182
Corentin Charye12e6d92011-02-26 10:20:31 +01001183 err = asus_wmi_input_init(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08001184 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +01001185 goto fail_input;
Yong Wang3d7b1652010-04-11 09:27:54 +08001186
Corentin Charye12e6d92011-02-26 10:20:31 +01001187 err = asus_wmi_led_init(asus);
Corentin Chary084fca62010-11-29 08:14:06 +01001188 if (err)
1189 goto fail_leds;
1190
Corentin Charye12e6d92011-02-26 10:20:31 +01001191 err = asus_wmi_rfkill_init(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001192 if (err)
1193 goto fail_rfkill;
1194
Yong Wang3d7b1652010-04-11 09:27:54 +08001195 if (!acpi_video_backlight_support()) {
Corentin Charye12e6d92011-02-26 10:20:31 +01001196 err = asus_wmi_backlight_init(asus);
Corentin Charyb7187262011-02-06 13:28:39 +01001197 if (err && err != -ENODEV)
Corentin Chary27c136c2010-11-29 08:14:05 +01001198 goto fail_backlight;
Yong Wang3d7b1652010-04-11 09:27:54 +08001199 } else
1200 pr_info("Backlight controlled by ACPI video driver\n");
Yong Wang45f2c692010-04-11 09:27:19 +08001201
Corentin Charye12e6d92011-02-26 10:20:31 +01001202 status = wmi_install_notify_handler(asus->driver->event_guid,
1203 asus_wmi_notify, asus);
Yong Wang45f2c692010-04-11 09:27:19 +08001204 if (ACPI_FAILURE(status)) {
Corentin Charye12e6d92011-02-26 10:20:31 +01001205 pr_err("Unable to register notify handler - %d\n", status);
Yong Wang45f2c692010-04-11 09:27:19 +08001206 err = -ENODEV;
Corentin Chary27c136c2010-11-29 08:14:05 +01001207 goto fail_wmi_handler;
Yong Wang45f2c692010-04-11 09:27:19 +08001208 }
1209
Corentin Charye12e6d92011-02-26 10:20:31 +01001210 err = asus_wmi_debugfs_init(asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001211 if (err)
1212 goto fail_debugfs;
1213
Corentin Charya04ce292011-02-06 13:28:33 +01001214 return 0;
Yong Wang45f2c692010-04-11 09:27:19 +08001215
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001216fail_debugfs:
Corentin Charye12e6d92011-02-26 10:20:31 +01001217 wmi_remove_notify_handler(asus->driver->event_guid);
Corentin Chary27c136c2010-11-29 08:14:05 +01001218fail_wmi_handler:
Corentin Charye12e6d92011-02-26 10:20:31 +01001219 asus_wmi_backlight_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001220fail_backlight:
Corentin Charye12e6d92011-02-26 10:20:31 +01001221 asus_wmi_rfkill_exit(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001222fail_rfkill:
Corentin Charye12e6d92011-02-26 10:20:31 +01001223 asus_wmi_led_exit(asus);
Corentin Chary084fca62010-11-29 08:14:06 +01001224fail_leds:
Corentin Charye12e6d92011-02-26 10:20:31 +01001225 asus_wmi_input_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001226fail_input:
Corentin Charye12e6d92011-02-26 10:20:31 +01001227 asus_wmi_platform_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001228fail_platform:
Corentin Charye12e6d92011-02-26 10:20:31 +01001229 kfree(asus);
Corentin Charya04ce292011-02-06 13:28:33 +01001230 return err;
Yong Wang45f2c692010-04-11 09:27:19 +08001231}
1232
Corentin Charye12e6d92011-02-26 10:20:31 +01001233static int asus_wmi_remove(struct platform_device *device)
Yong Wang45f2c692010-04-11 09:27:19 +08001234{
Corentin Charye12e6d92011-02-26 10:20:31 +01001235 struct asus_wmi *asus;
Yong Wang45f2c692010-04-11 09:27:19 +08001236
Corentin Charye12e6d92011-02-26 10:20:31 +01001237 asus = platform_get_drvdata(device);
1238 wmi_remove_notify_handler(asus->driver->event_guid);
1239 asus_wmi_backlight_exit(asus);
1240 asus_wmi_input_exit(asus);
1241 asus_wmi_led_exit(asus);
1242 asus_wmi_rfkill_exit(asus);
1243 asus_wmi_debugfs_exit(asus);
1244 asus_wmi_platform_exit(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08001245
Corentin Charye12e6d92011-02-26 10:20:31 +01001246 kfree(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08001247 return 0;
1248}
1249
Corentin Chary0773d7f2011-02-06 13:28:32 +01001250/*
1251 * Platform driver - hibernate/resume callbacks
1252 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001253static int asus_hotk_thaw(struct device *device)
Corentin Chary0773d7f2011-02-06 13:28:32 +01001254{
Corentin Charye12e6d92011-02-26 10:20:31 +01001255 struct asus_wmi *asus = dev_get_drvdata(device);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001256
Corentin Charya7ce3f02011-02-26 10:20:33 +01001257 if (asus->wlan.rfkill) {
Corentin Chary0773d7f2011-02-06 13:28:32 +01001258 bool wlan;
1259
1260 /*
1261 * Work around bios bug - acpi _PTS turns off the wireless led
1262 * during suspend. Normally it restores it on resume, but
1263 * we should kick it ourselves in case hibernation is aborted.
1264 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001265 wlan = asus_wmi_get_devstate_simple(ASUS_WMI_DEVID_WLAN);
1266 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001267 }
1268
1269 return 0;
1270}
1271
Corentin Charye12e6d92011-02-26 10:20:31 +01001272static int asus_hotk_restore(struct device *device)
Corentin Chary0773d7f2011-02-06 13:28:32 +01001273{
Corentin Charye12e6d92011-02-26 10:20:31 +01001274 struct asus_wmi *asus = dev_get_drvdata(device);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001275 int bl;
1276
1277 /* Refresh both wlan rfkill state and pci hotplug */
Corentin Charya7ce3f02011-02-26 10:20:33 +01001278 if (asus->wlan.rfkill)
Corentin Charye12e6d92011-02-26 10:20:31 +01001279 asus_rfkill_hotplug(asus);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001280
Corentin Charya7ce3f02011-02-26 10:20:33 +01001281 if (asus->bluetooth.rfkill) {
Corentin Charye12e6d92011-02-26 10:20:31 +01001282 bl = !asus_wmi_get_devstate_simple(ASUS_WMI_DEVID_BLUETOOTH);
Corentin Charya7ce3f02011-02-26 10:20:33 +01001283 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01001284 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01001285 if (asus->wimax.rfkill) {
Corentin Charye12e6d92011-02-26 10:20:31 +01001286 bl = !asus_wmi_get_devstate_simple(ASUS_WMI_DEVID_WIMAX);
Corentin Charya7ce3f02011-02-26 10:20:33 +01001287 rfkill_set_sw_state(asus->wimax.rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01001288 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01001289 if (asus->wwan3g.rfkill) {
Corentin Charye12e6d92011-02-26 10:20:31 +01001290 bl = !asus_wmi_get_devstate_simple(ASUS_WMI_DEVID_WWAN3G);
Corentin Charya7ce3f02011-02-26 10:20:33 +01001291 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001292 }
1293
1294 return 0;
1295}
1296
Corentin Charye12e6d92011-02-26 10:20:31 +01001297static const struct dev_pm_ops asus_pm_ops = {
1298 .thaw = asus_hotk_thaw,
1299 .restore = asus_hotk_restore,
Corentin Chary0773d7f2011-02-06 13:28:32 +01001300};
1301
Corentin Charye12e6d92011-02-26 10:20:31 +01001302static int asus_wmi_probe(struct platform_device *pdev)
Corentin Charyd358cb52010-11-29 08:14:14 +01001303{
Corentin Charye12e6d92011-02-26 10:20:31 +01001304 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
1305 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
1306 int ret;
Corentin Charyd358cb52010-11-29 08:14:14 +01001307
Corentin Charye12e6d92011-02-26 10:20:31 +01001308 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
1309 pr_warning("Management GUID not found\n");
Yong Wangee027e42010-03-21 10:26:34 +08001310 return -ENODEV;
1311 }
1312
Corentin Charye12e6d92011-02-26 10:20:31 +01001313 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
1314 pr_warning("Event GUID not found\n");
Corentin Charyd358cb52010-11-29 08:14:14 +01001315 return -ENODEV;
1316 }
1317
Corentin Charye12e6d92011-02-26 10:20:31 +01001318 if (wdrv->probe) {
1319 ret = wdrv->probe(pdev);
1320 if (ret)
1321 return ret;
1322 }
1323
1324 return asus_wmi_add(pdev);
Corentin Charya04ce292011-02-06 13:28:33 +01001325}
Yong Wangee027e42010-03-21 10:26:34 +08001326
Corentin Charye12e6d92011-02-26 10:20:31 +01001327static bool used;
Yong Wangee027e42010-03-21 10:26:34 +08001328
Corentin Charye12e6d92011-02-26 10:20:31 +01001329int asus_wmi_register_driver(struct asus_wmi_driver *driver)
Corentin Charya04ce292011-02-06 13:28:33 +01001330{
Corentin Charye12e6d92011-02-26 10:20:31 +01001331 struct platform_driver *platform_driver;
1332 struct platform_device *platform_device;
1333
1334 if (used)
1335 return -EBUSY;
1336
1337 platform_driver = &driver->platform_driver;
1338 platform_driver->remove = asus_wmi_remove;
1339 platform_driver->driver.owner = driver->owner;
1340 platform_driver->driver.name = driver->name;
1341 platform_driver->driver.pm = &asus_pm_ops;
1342
1343 platform_device = platform_create_bundle(platform_driver,
1344 asus_wmi_probe,
Corentin Charya04ce292011-02-06 13:28:33 +01001345 NULL, 0, NULL, 0);
1346 if (IS_ERR(platform_device))
1347 return PTR_ERR(platform_device);
Corentin Charye12e6d92011-02-26 10:20:31 +01001348
1349 used = true;
1350 return 0;
1351}
1352EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
1353
1354void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
1355{
1356 platform_device_unregister(driver->platform_device);
1357 platform_driver_unregister(&driver->platform_driver);
1358 used = false;
1359}
1360EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
1361
1362static int __init asus_wmi_init(void)
1363{
1364 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
1365 pr_info("Asus Management GUID not found");
1366 return -ENODEV;
1367 }
1368
1369 pr_info("ASUS WMI generic driver loaded");
Yong Wangee027e42010-03-21 10:26:34 +08001370 return 0;
1371}
1372
Corentin Charye12e6d92011-02-26 10:20:31 +01001373static void __exit asus_wmi_exit(void)
Yong Wangee027e42010-03-21 10:26:34 +08001374{
Corentin Charye12e6d92011-02-26 10:20:31 +01001375 pr_info("ASUS WMI generic driver unloaded");
Yong Wangee027e42010-03-21 10:26:34 +08001376}
1377
Corentin Charye12e6d92011-02-26 10:20:31 +01001378module_init(asus_wmi_init);
1379module_exit(asus_wmi_exit);