blob: 83a7a8101fd6095b0f79a85baabf70f844e8e3d9 [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 Charye07babd2011-02-26 10:20:42 +010042#include <linux/hwmon.h>
43#include <linux/hwmon-sysfs.h>
Corentin Chary8c1b2d82010-11-29 08:14:09 +010044#include <linux/debugfs.h>
45#include <linux/seq_file.h>
Yong Wang45f2c692010-04-11 09:27:19 +080046#include <linux/platform_device.h>
Yong Wangee027e42010-03-21 10:26:34 +080047#include <acpi/acpi_bus.h>
48#include <acpi/acpi_drivers.h>
49
Corentin Charye12e6d92011-02-26 10:20:31 +010050#include "asus-wmi.h"
Yong Wang45f2c692010-04-11 09:27:19 +080051
Corentin Charye12e6d92011-02-26 10:20:31 +010052MODULE_AUTHOR("Corentin Chary <corentincj@iksaif.net>, "
53 "Yong Wang <yong.y.wang@intel.com>");
54MODULE_DESCRIPTION("Asus Generic WMI Driver");
Yong Wangee027e42010-03-21 10:26:34 +080055MODULE_LICENSE("GPL");
56
Corentin Charye12e6d92011-02-26 10:20:31 +010057#define to_platform_driver(drv) \
58 (container_of((drv), struct platform_driver, driver))
Corentin Charyd358cb52010-11-29 08:14:14 +010059
Corentin Charye12e6d92011-02-26 10:20:31 +010060#define to_asus_wmi_driver(pdrv) \
61 (container_of((pdrv), struct asus_wmi_driver, platform_driver))
Yong Wangee027e42010-03-21 10:26:34 +080062
Corentin Charye12e6d92011-02-26 10:20:31 +010063#define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
Yong Wangee027e42010-03-21 10:26:34 +080064
Corentin Chary33e0e6f2011-02-06 13:28:34 +010065#define NOTIFY_BRNUP_MIN 0x11
66#define NOTIFY_BRNUP_MAX 0x1f
67#define NOTIFY_BRNDOWN_MIN 0x20
68#define NOTIFY_BRNDOWN_MAX 0x2e
Corentin Charye9809c02011-07-01 11:34:31 +020069#define NOTIFY_KBD_BRTUP 0xc4
70#define NOTIFY_KBD_BRTDWN 0xc5
Yong Wangee027e42010-03-21 10:26:34 +080071
Corentin Chary43815942011-02-06 13:28:43 +010072/* WMI Methods */
Corentin Chary2f686b52011-02-26 10:20:41 +010073#define ASUS_WMI_METHODID_SPEC 0x43455053 /* BIOS SPECification */
74#define ASUS_WMI_METHODID_SFBD 0x44424653 /* Set First Boot Device */
75#define ASUS_WMI_METHODID_GLCD 0x44434C47 /* Get LCD status */
76#define ASUS_WMI_METHODID_GPID 0x44495047 /* Get Panel ID?? (Resol) */
77#define ASUS_WMI_METHODID_QMOD 0x444F4D51 /* Quiet MODe */
78#define ASUS_WMI_METHODID_SPLV 0x4C425053 /* Set Panel Light Value */
79#define ASUS_WMI_METHODID_SFUN 0x4E554653 /* FUNCtionalities */
80#define ASUS_WMI_METHODID_SDSP 0x50534453 /* Set DiSPlay output */
81#define ASUS_WMI_METHODID_GDSP 0x50534447 /* Get DiSPlay output */
82#define ASUS_WMI_METHODID_DEVP 0x50564544 /* DEVice Policy */
83#define ASUS_WMI_METHODID_OSVR 0x5256534F /* OS VeRsion */
84#define ASUS_WMI_METHODID_DSTS 0x53544344 /* Device STatuS */
85#define ASUS_WMI_METHODID_DSTS2 0x53545344 /* Device STatuS #2*/
86#define ASUS_WMI_METHODID_BSTS 0x53545342 /* Bios STatuS ? */
87#define ASUS_WMI_METHODID_DEVS 0x53564544 /* DEVice Set */
88#define ASUS_WMI_METHODID_CFVS 0x53564643 /* CPU Frequency Volt Set */
89#define ASUS_WMI_METHODID_KBFT 0x5446424B /* KeyBoard FilTer */
90#define ASUS_WMI_METHODID_INIT 0x54494E49 /* INITialize */
91#define ASUS_WMI_METHODID_HKEY 0x59454B48 /* Hot KEY ?? */
Yong Wang3d7b1652010-04-11 09:27:54 +080092
Corentin Charyd33da3b2011-02-26 10:20:35 +010093#define ASUS_WMI_UNSUPPORTED_METHOD 0xFFFFFFFE
94
Corentin Chary43815942011-02-06 13:28:43 +010095/* Wireless */
Corentin Chary2f686b52011-02-26 10:20:41 +010096#define ASUS_WMI_DEVID_HW_SWITCH 0x00010001
97#define ASUS_WMI_DEVID_WIRELESS_LED 0x00010002
Corentin Charye12e6d92011-02-26 10:20:31 +010098#define ASUS_WMI_DEVID_WLAN 0x00010011
99#define ASUS_WMI_DEVID_BLUETOOTH 0x00010013
Corentin Chary2f686b52011-02-26 10:20:41 +0100100#define ASUS_WMI_DEVID_GPS 0x00010015
Corentin Charye12e6d92011-02-26 10:20:31 +0100101#define ASUS_WMI_DEVID_WIMAX 0x00010017
102#define ASUS_WMI_DEVID_WWAN3G 0x00010019
Corentin Chary2f686b52011-02-26 10:20:41 +0100103#define ASUS_WMI_DEVID_UWB 0x00010021
104
105/* Leds */
106/* 0x000200XX and 0x000400XX */
Corentin Chary43815942011-02-06 13:28:43 +0100107
108/* Backlight and Brightness */
Corentin Charye12e6d92011-02-26 10:20:31 +0100109#define ASUS_WMI_DEVID_BACKLIGHT 0x00050011
110#define ASUS_WMI_DEVID_BRIGHTNESS 0x00050012
Corentin Chary2f686b52011-02-26 10:20:41 +0100111#define ASUS_WMI_DEVID_KBD_BACKLIGHT 0x00050021
112#define ASUS_WMI_DEVID_LIGHT_SENSOR 0x00050022 /* ?? */
Corentin Chary43815942011-02-06 13:28:43 +0100113
114/* Misc */
Corentin Charye12e6d92011-02-26 10:20:31 +0100115#define ASUS_WMI_DEVID_CAMERA 0x00060013
Corentin Chary43815942011-02-06 13:28:43 +0100116
117/* Storage */
Corentin Charye12e6d92011-02-26 10:20:31 +0100118#define ASUS_WMI_DEVID_CARDREADER 0x00080013
Corentin Chary43815942011-02-06 13:28:43 +0100119
120/* Input */
Corentin Chary57ab7da2011-02-26 10:20:32 +0100121#define ASUS_WMI_DEVID_TOUCHPAD 0x00100011
Corentin Charye12e6d92011-02-26 10:20:31 +0100122#define ASUS_WMI_DEVID_TOUCHPAD_LED 0x00100012
Yong Wang3d7b1652010-04-11 09:27:54 +0800123
Corentin Chary2f686b52011-02-26 10:20:41 +0100124/* Fan, Thermal */
125#define ASUS_WMI_DEVID_THERMAL_CTRL 0x00110011
126#define ASUS_WMI_DEVID_FAN_CTRL 0x00110012
127
128/* Power */
129#define ASUS_WMI_DEVID_PROCESSOR_STATE 0x00120012
130
Corentin Chary43815942011-02-06 13:28:43 +0100131/* DSTS masks */
Corentin Charye12e6d92011-02-26 10:20:31 +0100132#define ASUS_WMI_DSTS_STATUS_BIT 0x00000001
Corentin Charya75fe0d2011-02-26 10:20:34 +0100133#define ASUS_WMI_DSTS_UNKNOWN_BIT 0x00000002
Corentin Charye12e6d92011-02-26 10:20:31 +0100134#define ASUS_WMI_DSTS_PRESENCE_BIT 0x00010000
Corentin Chary2f686b52011-02-26 10:20:41 +0100135#define ASUS_WMI_DSTS_USER_BIT 0x00020000
136#define ASUS_WMI_DSTS_BIOS_BIT 0x00040000
Corentin Charye12e6d92011-02-26 10:20:31 +0100137#define ASUS_WMI_DSTS_BRIGHTNESS_MASK 0x000000FF
138#define ASUS_WMI_DSTS_MAX_BRIGTH_MASK 0x0000FF00
Yong Wangee027e42010-03-21 10:26:34 +0800139
Yong Wang3d7b1652010-04-11 09:27:54 +0800140struct bios_args {
Corentin Charyd33da3b2011-02-26 10:20:35 +0100141 u32 arg0;
142 u32 arg1;
143} __packed;
Yong Wang3d7b1652010-04-11 09:27:54 +0800144
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100145/*
Corentin Charye12e6d92011-02-26 10:20:31 +0100146 * <platform>/ - debugfs root directory
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100147 * dev_id - current dev_id
148 * ctrl_param - current ctrl_param
Corentin Charyef343492011-02-26 10:20:39 +0100149 * method_id - current method_id
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100150 * devs - call DEVS(dev_id, ctrl_param) and print result
151 * dsts - call DSTS(dev_id) and print result
Corentin Charyef343492011-02-26 10:20:39 +0100152 * call - call method_id(dev_id, ctrl_param) and print result
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100153 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100154struct asus_wmi_debug {
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100155 struct dentry *root;
Corentin Charyef343492011-02-26 10:20:39 +0100156 u32 method_id;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100157 u32 dev_id;
158 u32 ctrl_param;
159};
160
Corentin Charya7ce3f02011-02-26 10:20:33 +0100161struct asus_rfkill {
162 struct asus_wmi *asus;
163 struct rfkill *rfkill;
164 u32 dev_id;
165};
166
Corentin Charye12e6d92011-02-26 10:20:31 +0100167struct asus_wmi {
Corentin Chary1d070f892011-02-26 10:20:36 +0100168 int dsts_id;
Corentin Chary46dbca82011-02-26 10:20:38 +0100169 int spec;
170 int sfun;
Corentin Chary1d070f892011-02-26 10:20:36 +0100171
Yong Wang81248882010-04-11 09:26:33 +0800172 struct input_dev *inputdev;
Yong Wang3d7b1652010-04-11 09:27:54 +0800173 struct backlight_device *backlight_device;
Corentin Charye07babd2011-02-26 10:20:42 +0100174 struct device *hwmon_device;
Corentin Chary27c136c2010-11-29 08:14:05 +0100175 struct platform_device *platform_device;
Corentin Chary084fca62010-11-29 08:14:06 +0100176
177 struct led_classdev tpd_led;
178 int tpd_led_wk;
Corentin Charye9809c02011-07-01 11:34:31 +0200179 struct led_classdev kbd_led;
180 int kbd_led_wk;
Corentin Chary084fca62010-11-29 08:14:06 +0100181 struct workqueue_struct *led_workqueue;
182 struct work_struct tpd_led_work;
Corentin Charye9809c02011-07-01 11:34:31 +0200183 struct work_struct kbd_led_work;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100184
Corentin Charya7ce3f02011-02-26 10:20:33 +0100185 struct asus_rfkill wlan;
186 struct asus_rfkill bluetooth;
187 struct asus_rfkill wimax;
188 struct asus_rfkill wwan3g;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100189
Corentin Charyafa7c882011-02-06 13:28:28 +0100190 struct hotplug_slot *hotplug_slot;
191 struct mutex hotplug_lock;
Corentin Chary279f8f92011-02-06 13:28:29 +0100192 struct mutex wmi_lock;
193 struct workqueue_struct *hotplug_workqueue;
194 struct work_struct hotplug_work;
Corentin Charyafa7c882011-02-06 13:28:28 +0100195
Corentin Charye12e6d92011-02-26 10:20:31 +0100196 struct asus_wmi_debug debug;
197
198 struct asus_wmi_driver *driver;
Yong Wang81248882010-04-11 09:26:33 +0800199};
200
Corentin Charye12e6d92011-02-26 10:20:31 +0100201static int asus_wmi_input_init(struct asus_wmi *asus)
Yong Wangee027e42010-03-21 10:26:34 +0800202{
203 int err;
204
Corentin Charye12e6d92011-02-26 10:20:31 +0100205 asus->inputdev = input_allocate_device();
206 if (!asus->inputdev)
Yong Wangee027e42010-03-21 10:26:34 +0800207 return -ENOMEM;
208
Corentin Chary58a9f392011-03-30 16:32:32 +0200209 asus->inputdev->name = asus->driver->input_name;
210 asus->inputdev->phys = asus->driver->input_phys;
Corentin Charye12e6d92011-02-26 10:20:31 +0100211 asus->inputdev->id.bustype = BUS_HOST;
212 asus->inputdev->dev.parent = &asus->platform_device->dev;
Seth Forshee39bbde02011-07-04 09:49:20 +0200213 set_bit(EV_REP, asus->inputdev->evbit);
Yong Wangee027e42010-03-21 10:26:34 +0800214
Corentin Charye12e6d92011-02-26 10:20:31 +0100215 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
Yong Wangee027e42010-03-21 10:26:34 +0800216 if (err)
217 goto err_free_dev;
218
Corentin Charye12e6d92011-02-26 10:20:31 +0100219 err = input_register_device(asus->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800220 if (err)
221 goto err_free_keymap;
222
223 return 0;
224
225err_free_keymap:
Corentin Charye12e6d92011-02-26 10:20:31 +0100226 sparse_keymap_free(asus->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800227err_free_dev:
Corentin Charye12e6d92011-02-26 10:20:31 +0100228 input_free_device(asus->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800229 return err;
230}
231
Corentin Charye12e6d92011-02-26 10:20:31 +0100232static void asus_wmi_input_exit(struct asus_wmi *asus)
Yong Wang81248882010-04-11 09:26:33 +0800233{
Corentin Charye12e6d92011-02-26 10:20:31 +0100234 if (asus->inputdev) {
235 sparse_keymap_free(asus->inputdev);
236 input_unregister_device(asus->inputdev);
Yong Wang81248882010-04-11 09:26:33 +0800237 }
238
Corentin Charye12e6d92011-02-26 10:20:31 +0100239 asus->inputdev = NULL;
Yong Wang81248882010-04-11 09:26:33 +0800240}
241
Corentin Charyd33da3b2011-02-26 10:20:35 +0100242static int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1,
243 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800244{
Corentin Charyd33da3b2011-02-26 10:20:35 +0100245 struct bios_args args = {
246 .arg0 = arg0,
247 .arg1 = arg1,
248 };
249 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
Yong Wang3d7b1652010-04-11 09:27:54 +0800250 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Yong Wang3d7b1652010-04-11 09:27:54 +0800251 acpi_status status;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100252 union acpi_object *obj;
Yong Wang3d7b1652010-04-11 09:27:54 +0800253 u32 tmp;
254
Corentin Charyd33da3b2011-02-26 10:20:35 +0100255 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 1, method_id,
Corentin Charyafa7c882011-02-06 13:28:28 +0100256 &input, &output);
Yong Wang3d7b1652010-04-11 09:27:54 +0800257
258 if (ACPI_FAILURE(status))
Corentin Charyd33da3b2011-02-26 10:20:35 +0100259 goto exit;
Yong Wang3d7b1652010-04-11 09:27:54 +0800260
261 obj = (union acpi_object *)output.pointer;
262 if (obj && obj->type == ACPI_TYPE_INTEGER)
Corentin Charye12e6d92011-02-26 10:20:31 +0100263 tmp = (u32) obj->integer.value;
Yong Wang3d7b1652010-04-11 09:27:54 +0800264 else
265 tmp = 0;
266
Corentin Chary2a3f0062010-11-29 08:14:10 +0100267 if (retval)
268 *retval = tmp;
Yong Wang3d7b1652010-04-11 09:27:54 +0800269
270 kfree(obj);
271
Corentin Charyd33da3b2011-02-26 10:20:35 +0100272exit:
273 if (ACPI_FAILURE(status))
274 return -EIO;
Yong Wang3d7b1652010-04-11 09:27:54 +0800275
Corentin Charyd33da3b2011-02-26 10:20:35 +0100276 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
277 return -ENODEV;
278
279 return 0;
Yong Wang3d7b1652010-04-11 09:27:54 +0800280}
281
Corentin Chary1d070f892011-02-26 10:20:36 +0100282static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
Corentin Charyd33da3b2011-02-26 10:20:35 +0100283{
Corentin Chary1d070f892011-02-26 10:20:36 +0100284 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
Corentin Charyd33da3b2011-02-26 10:20:35 +0100285}
286
287static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
Corentin Chary1d070f892011-02-26 10:20:36 +0100288 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800289{
Corentin Charyd33da3b2011-02-26 10:20:35 +0100290 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
291 ctrl_param, retval);
Yong Wang3d7b1652010-04-11 09:27:54 +0800292}
293
Corentin Chary5c956382011-02-06 13:28:31 +0100294/* Helper for special devices with magic return codes */
Corentin Chary1d070f892011-02-26 10:20:36 +0100295static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
296 u32 dev_id, u32 mask)
Corentin Chary5c956382011-02-06 13:28:31 +0100297{
298 u32 retval = 0;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100299 int err;
Corentin Chary5c956382011-02-06 13:28:31 +0100300
Corentin Chary1d070f892011-02-26 10:20:36 +0100301 err = asus_wmi_get_devstate(asus, dev_id, &retval);
Corentin Chary5c956382011-02-06 13:28:31 +0100302
Corentin Charyd33da3b2011-02-26 10:20:35 +0100303 if (err < 0)
304 return err;
Corentin Chary5c956382011-02-06 13:28:31 +0100305
Corentin Charye12e6d92011-02-26 10:20:31 +0100306 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
Corentin Chary5c956382011-02-06 13:28:31 +0100307 return -ENODEV;
308
Corentin Charya75fe0d2011-02-26 10:20:34 +0100309 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
310 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
311 return -ENODEV;
312 }
313
Corentin Charyb7187262011-02-06 13:28:39 +0100314 return retval & mask;
315}
316
Corentin Chary1d070f892011-02-26 10:20:36 +0100317static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
Corentin Charyb7187262011-02-06 13:28:39 +0100318{
Corentin Chary1d070f892011-02-26 10:20:36 +0100319 return asus_wmi_get_devstate_bits(asus, dev_id,
320 ASUS_WMI_DSTS_STATUS_BIT);
Corentin Chary5c956382011-02-06 13:28:31 +0100321}
322
Corentin Chary084fca62010-11-29 08:14:06 +0100323/*
324 * LEDs
325 */
326/*
327 * These functions actually update the LED's, and are called from a
328 * workqueue. By doing this as separate work rather than when the LED
Corentin Charye12e6d92011-02-26 10:20:31 +0100329 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
Corentin Chary084fca62010-11-29 08:14:06 +0100330 * potentially bad time, such as a timer interrupt.
331 */
332static void tpd_led_update(struct work_struct *work)
333{
334 int ctrl_param;
Corentin Charye12e6d92011-02-26 10:20:31 +0100335 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100336
Corentin Charye12e6d92011-02-26 10:20:31 +0100337 asus = container_of(work, struct asus_wmi, tpd_led_work);
Corentin Chary084fca62010-11-29 08:14:06 +0100338
Corentin Charye12e6d92011-02-26 10:20:31 +0100339 ctrl_param = asus->tpd_led_wk;
340 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
Corentin Chary084fca62010-11-29 08:14:06 +0100341}
342
343static void tpd_led_set(struct led_classdev *led_cdev,
344 enum led_brightness value)
345{
Corentin Charye12e6d92011-02-26 10:20:31 +0100346 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100347
Corentin Charye12e6d92011-02-26 10:20:31 +0100348 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
Corentin Chary084fca62010-11-29 08:14:06 +0100349
Corentin Charye12e6d92011-02-26 10:20:31 +0100350 asus->tpd_led_wk = !!value;
351 queue_work(asus->led_workqueue, &asus->tpd_led_work);
Corentin Chary084fca62010-11-29 08:14:06 +0100352}
353
Corentin Charye12e6d92011-02-26 10:20:31 +0100354static int read_tpd_led_state(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100355{
Corentin Chary1d070f892011-02-26 10:20:36 +0100356 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
Corentin Chary084fca62010-11-29 08:14:06 +0100357}
358
359static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
360{
Corentin Charye12e6d92011-02-26 10:20:31 +0100361 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100362
Corentin Charye12e6d92011-02-26 10:20:31 +0100363 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
Corentin Chary084fca62010-11-29 08:14:06 +0100364
Corentin Charye12e6d92011-02-26 10:20:31 +0100365 return read_tpd_led_state(asus);
Corentin Chary084fca62010-11-29 08:14:06 +0100366}
367
Corentin Charye9809c02011-07-01 11:34:31 +0200368static void kbd_led_update(struct work_struct *work)
Corentin Chary084fca62010-11-29 08:14:06 +0100369{
Corentin Charye9809c02011-07-01 11:34:31 +0200370 int ctrl_param = 0;
371 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100372
Corentin Charye9809c02011-07-01 11:34:31 +0200373 asus = container_of(work, struct asus_wmi, kbd_led_work);
Corentin Chary084fca62010-11-29 08:14:06 +0100374
Corentin Charye9809c02011-07-01 11:34:31 +0200375 /*
376 * bits 0-2: level
377 * bit 7: light on/off
378 */
379 if (asus->kbd_led_wk > 0)
380 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
Corentin Chary084fca62010-11-29 08:14:06 +0100381
Corentin Charye9809c02011-07-01 11:34:31 +0200382 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
383}
Corentin Chary084fca62010-11-29 08:14:06 +0100384
Corentin Charye9809c02011-07-01 11:34:31 +0200385static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
386{
387 int retval;
388
389 /*
390 * bits 0-2: level
391 * bit 7: light on/off
392 * bit 8-10: environment (0: dark, 1: normal, 2: light)
393 * bit 17: status unknown
394 */
395 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
396 0xFFFF);
397
398 if (retval == 0x8000)
399 retval = -ENODEV;
400
401 if (retval >= 0) {
402 if (level)
403 *level = retval & 0x80 ? retval & 0x7F : 0;
404 if (env)
405 *env = (retval >> 8) & 0x7F;
406 retval = 0;
Corentin Chary084fca62010-11-29 08:14:06 +0100407 }
408
Corentin Charye9809c02011-07-01 11:34:31 +0200409 return retval;
410}
411
412static void kbd_led_set(struct led_classdev *led_cdev,
413 enum led_brightness value)
414{
415 struct asus_wmi *asus;
416
417 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
418
419 if (value > asus->kbd_led.max_brightness)
420 value = asus->kbd_led.max_brightness;
421 else if (value < 0)
422 value = 0;
423
424 asus->kbd_led_wk = value;
425 queue_work(asus->led_workqueue, &asus->kbd_led_work);
426}
427
428static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
429{
430 struct asus_wmi *asus;
431 int retval, value;
432
433 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
434
435 retval = kbd_led_read(asus, &value, NULL);
436
437 if (retval < 0)
438 return retval;
439
440 return value;
Corentin Chary084fca62010-11-29 08:14:06 +0100441}
442
Corentin Charye12e6d92011-02-26 10:20:31 +0100443static void asus_wmi_led_exit(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100444{
Corentin Charye12e6d92011-02-26 10:20:31 +0100445 if (asus->tpd_led.dev)
446 led_classdev_unregister(&asus->tpd_led);
447 if (asus->led_workqueue)
448 destroy_workqueue(asus->led_workqueue);
Corentin Chary084fca62010-11-29 08:14:06 +0100449}
450
Corentin Charye9809c02011-07-01 11:34:31 +0200451static int asus_wmi_led_init(struct asus_wmi *asus)
452{
453 int rv = 0;
454
455 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
456 if (!asus->led_workqueue)
457 return -ENOMEM;
458
459 if (read_tpd_led_state(asus) >= 0) {
460 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
461
462 asus->tpd_led.name = "asus::touchpad";
463 asus->tpd_led.brightness_set = tpd_led_set;
464 asus->tpd_led.brightness_get = tpd_led_get;
465 asus->tpd_led.max_brightness = 1;
466
467 rv = led_classdev_register(&asus->platform_device->dev,
468 &asus->tpd_led);
469 if (rv)
470 goto error;
471 }
472
473 if (kbd_led_read(asus, NULL, NULL) >= 0) {
474 INIT_WORK(&asus->kbd_led_work, kbd_led_update);
475
476 asus->kbd_led.name = "asus::kbd_backlight";
477 asus->kbd_led.brightness_set = kbd_led_set;
478 asus->kbd_led.brightness_get = kbd_led_get;
479 asus->kbd_led.max_brightness = 3;
480
481 rv = led_classdev_register(&asus->platform_device->dev,
482 &asus->kbd_led);
483 }
484
485error:
486 if (rv)
487 asus_wmi_led_exit(asus);
488
489 return rv;
490}
491
492
Corentin Chary084fca62010-11-29 08:14:06 +0100493/*
Corentin Charyafa7c882011-02-06 13:28:28 +0100494 * PCI hotplug (for wlan rfkill)
495 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100496static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100497{
Corentin Chary1d070f892011-02-26 10:20:36 +0100498 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100499
Corentin Chary5c956382011-02-06 13:28:31 +0100500 if (result < 0)
Corentin Charyafa7c882011-02-06 13:28:28 +0100501 return false;
Corentin Chary5c956382011-02-06 13:28:31 +0100502 return !result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100503}
504
Corentin Charye12e6d92011-02-26 10:20:31 +0100505static void asus_rfkill_hotplug(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100506{
507 struct pci_dev *dev;
508 struct pci_bus *bus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100509 bool blocked;
Corentin Charyafa7c882011-02-06 13:28:28 +0100510 bool absent;
511 u32 l;
512
Corentin Charye12e6d92011-02-26 10:20:31 +0100513 mutex_lock(&asus->wmi_lock);
514 blocked = asus_wlan_rfkill_blocked(asus);
515 mutex_unlock(&asus->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100516
Corentin Charye12e6d92011-02-26 10:20:31 +0100517 mutex_lock(&asus->hotplug_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100518
Corentin Charya7ce3f02011-02-26 10:20:33 +0100519 if (asus->wlan.rfkill)
520 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
Corentin Chary279f8f92011-02-06 13:28:29 +0100521
Corentin Charye12e6d92011-02-26 10:20:31 +0100522 if (asus->hotplug_slot) {
Corentin Charyafa7c882011-02-06 13:28:28 +0100523 bus = pci_find_bus(0, 1);
524 if (!bus) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700525 pr_warn("Unable to find PCI bus 1?\n");
Corentin Charyafa7c882011-02-06 13:28:28 +0100526 goto out_unlock;
527 }
528
529 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
530 pr_err("Unable to read PCI config space?\n");
531 goto out_unlock;
532 }
533 absent = (l == 0xffffffff);
534
535 if (blocked != absent) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700536 pr_warn("BIOS says wireless lan is %s, "
537 "but the pci device is %s\n",
538 blocked ? "blocked" : "unblocked",
539 absent ? "absent" : "present");
540 pr_warn("skipped wireless hotplug as probably "
541 "inappropriate for this model\n");
Corentin Charyafa7c882011-02-06 13:28:28 +0100542 goto out_unlock;
543 }
544
545 if (!blocked) {
546 dev = pci_get_slot(bus, 0);
547 if (dev) {
548 /* Device already present */
549 pci_dev_put(dev);
550 goto out_unlock;
551 }
552 dev = pci_scan_single_device(bus, 0);
553 if (dev) {
554 pci_bus_assign_resources(bus);
555 if (pci_bus_add_device(dev))
556 pr_err("Unable to hotplug wifi\n");
557 }
558 } else {
559 dev = pci_get_slot(bus, 0);
560 if (dev) {
561 pci_remove_bus_device(dev);
562 pci_dev_put(dev);
563 }
564 }
565 }
566
567out_unlock:
Corentin Charye12e6d92011-02-26 10:20:31 +0100568 mutex_unlock(&asus->hotplug_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100569}
570
Corentin Charye12e6d92011-02-26 10:20:31 +0100571static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
Corentin Charyafa7c882011-02-06 13:28:28 +0100572{
Corentin Charye12e6d92011-02-26 10:20:31 +0100573 struct asus_wmi *asus = data;
Corentin Charyafa7c882011-02-06 13:28:28 +0100574
575 if (event != ACPI_NOTIFY_BUS_CHECK)
576 return;
577
Corentin Chary279f8f92011-02-06 13:28:29 +0100578 /*
Corentin Charye12e6d92011-02-26 10:20:31 +0100579 * We can't call directly asus_rfkill_hotplug because most
Corentin Chary279f8f92011-02-06 13:28:29 +0100580 * of the time WMBC is still being executed and not reetrant.
581 * There is currently no way to tell ACPICA that we want this
Corentin Charye12e6d92011-02-26 10:20:31 +0100582 * method to be serialized, we schedule a asus_rfkill_hotplug
Corentin Chary279f8f92011-02-06 13:28:29 +0100583 * call later, in a safer context.
584 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100585 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
Corentin Charyafa7c882011-02-06 13:28:28 +0100586}
587
Corentin Charye12e6d92011-02-26 10:20:31 +0100588static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
Corentin Charyafa7c882011-02-06 13:28:28 +0100589{
590 acpi_status status;
591 acpi_handle handle;
592
593 status = acpi_get_handle(NULL, node, &handle);
594
595 if (ACPI_SUCCESS(status)) {
596 status = acpi_install_notify_handler(handle,
597 ACPI_SYSTEM_NOTIFY,
Corentin Charye12e6d92011-02-26 10:20:31 +0100598 asus_rfkill_notify, asus);
Corentin Charyafa7c882011-02-06 13:28:28 +0100599 if (ACPI_FAILURE(status))
Joe Perches5ad77dc2011-03-29 15:21:35 -0700600 pr_warn("Failed to register notify on %s\n", node);
Corentin Charyafa7c882011-02-06 13:28:28 +0100601 } else
602 return -ENODEV;
603
604 return 0;
605}
606
Corentin Charye12e6d92011-02-26 10:20:31 +0100607static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
Corentin Charyafa7c882011-02-06 13:28:28 +0100608{
609 acpi_status status = AE_OK;
610 acpi_handle handle;
611
612 status = acpi_get_handle(NULL, node, &handle);
613
614 if (ACPI_SUCCESS(status)) {
615 status = acpi_remove_notify_handler(handle,
Corentin Charye12e6d92011-02-26 10:20:31 +0100616 ACPI_SYSTEM_NOTIFY,
617 asus_rfkill_notify);
Corentin Charyafa7c882011-02-06 13:28:28 +0100618 if (ACPI_FAILURE(status))
619 pr_err("Error removing rfkill notify handler %s\n",
Corentin Charye12e6d92011-02-26 10:20:31 +0100620 node);
Corentin Charyafa7c882011-02-06 13:28:28 +0100621 }
622}
623
Corentin Charye12e6d92011-02-26 10:20:31 +0100624static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
625 u8 *value)
Corentin Charyafa7c882011-02-06 13:28:28 +0100626{
Corentin Chary1d070f892011-02-26 10:20:36 +0100627 struct asus_wmi *asus = hotplug_slot->private;
628 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100629
Corentin Chary5c956382011-02-06 13:28:31 +0100630 if (result < 0)
631 return result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100632
Corentin Chary5c956382011-02-06 13:28:31 +0100633 *value = !!result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100634 return 0;
635}
636
Corentin Charye12e6d92011-02-26 10:20:31 +0100637static void asus_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
Corentin Charyafa7c882011-02-06 13:28:28 +0100638{
639 kfree(hotplug_slot->info);
640 kfree(hotplug_slot);
641}
642
Corentin Charye12e6d92011-02-26 10:20:31 +0100643static struct hotplug_slot_ops asus_hotplug_slot_ops = {
Corentin Charyafa7c882011-02-06 13:28:28 +0100644 .owner = THIS_MODULE,
Corentin Charye12e6d92011-02-26 10:20:31 +0100645 .get_adapter_status = asus_get_adapter_status,
646 .get_power_status = asus_get_adapter_status,
Corentin Charyafa7c882011-02-06 13:28:28 +0100647};
648
Corentin Charye12e6d92011-02-26 10:20:31 +0100649static void asus_hotplug_work(struct work_struct *work)
Corentin Chary279f8f92011-02-06 13:28:29 +0100650{
Corentin Charye12e6d92011-02-26 10:20:31 +0100651 struct asus_wmi *asus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100652
Corentin Charye12e6d92011-02-26 10:20:31 +0100653 asus = container_of(work, struct asus_wmi, hotplug_work);
654 asus_rfkill_hotplug(asus);
Corentin Chary279f8f92011-02-06 13:28:29 +0100655}
656
Corentin Charye12e6d92011-02-26 10:20:31 +0100657static int asus_setup_pci_hotplug(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100658{
659 int ret = -ENOMEM;
660 struct pci_bus *bus = pci_find_bus(0, 1);
661
662 if (!bus) {
663 pr_err("Unable to find wifi PCI bus\n");
664 return -ENODEV;
665 }
666
Corentin Charye12e6d92011-02-26 10:20:31 +0100667 asus->hotplug_workqueue =
668 create_singlethread_workqueue("hotplug_workqueue");
669 if (!asus->hotplug_workqueue)
Corentin Chary279f8f92011-02-06 13:28:29 +0100670 goto error_workqueue;
671
Corentin Charye12e6d92011-02-26 10:20:31 +0100672 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
Corentin Chary279f8f92011-02-06 13:28:29 +0100673
Corentin Charye12e6d92011-02-26 10:20:31 +0100674 asus->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
675 if (!asus->hotplug_slot)
Corentin Charyafa7c882011-02-06 13:28:28 +0100676 goto error_slot;
677
Corentin Charye12e6d92011-02-26 10:20:31 +0100678 asus->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
679 GFP_KERNEL);
680 if (!asus->hotplug_slot->info)
Corentin Charyafa7c882011-02-06 13:28:28 +0100681 goto error_info;
682
Corentin Charye12e6d92011-02-26 10:20:31 +0100683 asus->hotplug_slot->private = asus;
684 asus->hotplug_slot->release = &asus_cleanup_pci_hotplug;
685 asus->hotplug_slot->ops = &asus_hotplug_slot_ops;
686 asus_get_adapter_status(asus->hotplug_slot,
687 &asus->hotplug_slot->info->adapter_status);
Corentin Charyafa7c882011-02-06 13:28:28 +0100688
Corentin Charye12e6d92011-02-26 10:20:31 +0100689 ret = pci_hp_register(asus->hotplug_slot, bus, 0, "asus-wifi");
Corentin Charyafa7c882011-02-06 13:28:28 +0100690 if (ret) {
691 pr_err("Unable to register hotplug slot - %d\n", ret);
692 goto error_register;
693 }
694
695 return 0;
696
697error_register:
Corentin Charye12e6d92011-02-26 10:20:31 +0100698 kfree(asus->hotplug_slot->info);
Corentin Charyafa7c882011-02-06 13:28:28 +0100699error_info:
Corentin Charye12e6d92011-02-26 10:20:31 +0100700 kfree(asus->hotplug_slot);
701 asus->hotplug_slot = NULL;
Corentin Charyafa7c882011-02-06 13:28:28 +0100702error_slot:
Corentin Charye12e6d92011-02-26 10:20:31 +0100703 destroy_workqueue(asus->hotplug_workqueue);
Corentin Chary279f8f92011-02-06 13:28:29 +0100704error_workqueue:
Corentin Charyafa7c882011-02-06 13:28:28 +0100705 return ret;
706}
707
708/*
Corentin Charyba48fdb2010-11-29 08:14:07 +0100709 * Rfkill devices
710 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100711static int asus_rfkill_set(void *data, bool blocked)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100712{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100713 struct asus_rfkill *priv = data;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100714 u32 ctrl_param = !blocked;
715
Corentin Charyd33da3b2011-02-26 10:20:35 +0100716 return asus_wmi_set_devstate(priv->dev_id, ctrl_param, NULL);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100717}
718
Corentin Charye12e6d92011-02-26 10:20:31 +0100719static void asus_rfkill_query(struct rfkill *rfkill, void *data)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100720{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100721 struct asus_rfkill *priv = data;
Corentin Chary5c956382011-02-06 13:28:31 +0100722 int result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100723
Corentin Chary1d070f892011-02-26 10:20:36 +0100724 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100725
Corentin Chary5c956382011-02-06 13:28:31 +0100726 if (result < 0)
Corentin Charye12e6d92011-02-26 10:20:31 +0100727 return;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100728
Corentin Charya7ce3f02011-02-26 10:20:33 +0100729 rfkill_set_sw_state(priv->rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100730}
731
Corentin Charye12e6d92011-02-26 10:20:31 +0100732static int asus_rfkill_wlan_set(void *data, bool blocked)
Corentin Chary279f8f92011-02-06 13:28:29 +0100733{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100734 struct asus_rfkill *priv = data;
735 struct asus_wmi *asus = priv->asus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100736 int ret;
737
738 /*
739 * This handler is enabled only if hotplug is enabled.
Corentin Charye12e6d92011-02-26 10:20:31 +0100740 * In this case, the asus_wmi_set_devstate() will
Corentin Chary279f8f92011-02-06 13:28:29 +0100741 * trigger a wmi notification and we need to wait
742 * this call to finish before being able to call
743 * any wmi method
744 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100745 mutex_lock(&asus->wmi_lock);
Corentin Charya7ce3f02011-02-26 10:20:33 +0100746 ret = asus_rfkill_set(data, blocked);
Corentin Charye12e6d92011-02-26 10:20:31 +0100747 mutex_unlock(&asus->wmi_lock);
Corentin Chary279f8f92011-02-06 13:28:29 +0100748 return ret;
749}
750
Corentin Charye12e6d92011-02-26 10:20:31 +0100751static const struct rfkill_ops asus_rfkill_wlan_ops = {
752 .set_block = asus_rfkill_wlan_set,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100753 .query = asus_rfkill_query,
Corentin Chary279f8f92011-02-06 13:28:29 +0100754};
755
Corentin Charye12e6d92011-02-26 10:20:31 +0100756static const struct rfkill_ops asus_rfkill_ops = {
757 .set_block = asus_rfkill_set,
758 .query = asus_rfkill_query,
Corentin Charyba48fdb2010-11-29 08:14:07 +0100759};
760
Corentin Charye12e6d92011-02-26 10:20:31 +0100761static int asus_new_rfkill(struct asus_wmi *asus,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100762 struct asus_rfkill *arfkill,
Corentin Charye12e6d92011-02-26 10:20:31 +0100763 const char *name, enum rfkill_type type, int dev_id)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100764{
Corentin Chary1d070f892011-02-26 10:20:36 +0100765 int result = asus_wmi_get_devstate_simple(asus, dev_id);
Corentin Charya7ce3f02011-02-26 10:20:33 +0100766 struct rfkill **rfkill = &arfkill->rfkill;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100767
Corentin Chary5c956382011-02-06 13:28:31 +0100768 if (result < 0)
769 return result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100770
Corentin Charya7ce3f02011-02-26 10:20:33 +0100771 arfkill->dev_id = dev_id;
772 arfkill->asus = asus;
773
Corentin Charye12e6d92011-02-26 10:20:31 +0100774 if (dev_id == ASUS_WMI_DEVID_WLAN && asus->driver->hotplug_wireless)
775 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100776 &asus_rfkill_wlan_ops, arfkill);
Corentin Chary279f8f92011-02-06 13:28:29 +0100777 else
Corentin Charye12e6d92011-02-26 10:20:31 +0100778 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100779 &asus_rfkill_ops, arfkill);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100780
781 if (!*rfkill)
782 return -EINVAL;
783
Corentin Chary5c956382011-02-06 13:28:31 +0100784 rfkill_init_sw_state(*rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100785 result = rfkill_register(*rfkill);
786 if (result) {
787 rfkill_destroy(*rfkill);
788 *rfkill = NULL;
789 return result;
790 }
791 return 0;
792}
793
Corentin Charye12e6d92011-02-26 10:20:31 +0100794static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100795{
Corentin Charye12e6d92011-02-26 10:20:31 +0100796 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
797 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
798 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
Corentin Charya7ce3f02011-02-26 10:20:33 +0100799 if (asus->wlan.rfkill) {
800 rfkill_unregister(asus->wlan.rfkill);
801 rfkill_destroy(asus->wlan.rfkill);
802 asus->wlan.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100803 }
Corentin Charyafa7c882011-02-06 13:28:28 +0100804 /*
805 * Refresh pci hotplug in case the rfkill state was changed after
Corentin Charye12e6d92011-02-26 10:20:31 +0100806 * asus_unregister_rfkill_notifier()
Corentin Charyafa7c882011-02-06 13:28:28 +0100807 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100808 asus_rfkill_hotplug(asus);
809 if (asus->hotplug_slot)
810 pci_hp_deregister(asus->hotplug_slot);
811 if (asus->hotplug_workqueue)
812 destroy_workqueue(asus->hotplug_workqueue);
Corentin Charyafa7c882011-02-06 13:28:28 +0100813
Corentin Charya7ce3f02011-02-26 10:20:33 +0100814 if (asus->bluetooth.rfkill) {
815 rfkill_unregister(asus->bluetooth.rfkill);
816 rfkill_destroy(asus->bluetooth.rfkill);
817 asus->bluetooth.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100818 }
Corentin Charya7ce3f02011-02-26 10:20:33 +0100819 if (asus->wimax.rfkill) {
820 rfkill_unregister(asus->wimax.rfkill);
821 rfkill_destroy(asus->wimax.rfkill);
822 asus->wimax.rfkill = NULL;
Corentin Chary2e9e1592011-02-06 13:28:37 +0100823 }
Corentin Charya7ce3f02011-02-26 10:20:33 +0100824 if (asus->wwan3g.rfkill) {
825 rfkill_unregister(asus->wwan3g.rfkill);
826 rfkill_destroy(asus->wwan3g.rfkill);
827 asus->wwan3g.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100828 }
829}
830
Corentin Charye12e6d92011-02-26 10:20:31 +0100831static int asus_wmi_rfkill_init(struct asus_wmi *asus)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100832{
833 int result = 0;
834
Corentin Charye12e6d92011-02-26 10:20:31 +0100835 mutex_init(&asus->hotplug_lock);
836 mutex_init(&asus->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100837
Corentin Charya7ce3f02011-02-26 10:20:33 +0100838 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
839 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100840
841 if (result && result != -ENODEV)
842 goto exit;
843
Corentin Charya7ce3f02011-02-26 10:20:33 +0100844 result = asus_new_rfkill(asus, &asus->bluetooth,
Corentin Charye12e6d92011-02-26 10:20:31 +0100845 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
846 ASUS_WMI_DEVID_BLUETOOTH);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100847
848 if (result && result != -ENODEV)
849 goto exit;
850
Corentin Charya7ce3f02011-02-26 10:20:33 +0100851 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
852 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
Corentin Chary2e9e1592011-02-06 13:28:37 +0100853
854 if (result && result != -ENODEV)
855 goto exit;
856
Corentin Charya7ce3f02011-02-26 10:20:33 +0100857 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
858 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100859
860 if (result && result != -ENODEV)
861 goto exit;
862
Corentin Charye12e6d92011-02-26 10:20:31 +0100863 if (!asus->driver->hotplug_wireless)
Corentin Charyc14d4b82011-02-06 13:28:40 +0100864 goto exit;
865
Corentin Charye12e6d92011-02-26 10:20:31 +0100866 result = asus_setup_pci_hotplug(asus);
Corentin Charyafa7c882011-02-06 13:28:28 +0100867 /*
868 * If we get -EBUSY then something else is handling the PCI hotplug -
869 * don't fail in this case
870 */
871 if (result == -EBUSY)
872 result = 0;
873
Corentin Charye12e6d92011-02-26 10:20:31 +0100874 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
875 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
876 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
Corentin Charyafa7c882011-02-06 13:28:28 +0100877 /*
878 * Refresh pci hotplug in case the rfkill state was changed during
879 * setup.
880 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100881 asus_rfkill_hotplug(asus);
Corentin Charyafa7c882011-02-06 13:28:28 +0100882
Corentin Charyba48fdb2010-11-29 08:14:07 +0100883exit:
884 if (result && result != -ENODEV)
Corentin Charye12e6d92011-02-26 10:20:31 +0100885 asus_wmi_rfkill_exit(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100886
887 if (result == -ENODEV)
888 result = 0;
889
890 return result;
891}
892
893/*
Corentin Charye07babd2011-02-26 10:20:42 +0100894 * Hwmon device
895 */
896static ssize_t asus_hwmon_pwm1(struct device *dev,
Corentin Chary49979d02011-07-01 11:34:26 +0200897 struct device_attribute *attr,
898 char *buf)
Corentin Charye07babd2011-02-26 10:20:42 +0100899{
900 struct asus_wmi *asus = dev_get_drvdata(dev);
901 u32 value;
902 int err;
903
904 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
905
906 if (err < 0)
907 return err;
908
Corentin Chary49979d02011-07-01 11:34:26 +0200909 value &= 0xFF;
Corentin Charye07babd2011-02-26 10:20:42 +0100910
911 if (value == 1) /* Low Speed */
912 value = 85;
913 else if (value == 2)
914 value = 170;
915 else if (value == 3)
916 value = 255;
917 else if (value != 0) {
918 pr_err("Unknown fan speed %#x", value);
919 value = -1;
920 }
921
922 return sprintf(buf, "%d\n", value);
923}
924
925static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO, asus_hwmon_pwm1, NULL, 0);
926
927static ssize_t
928show_name(struct device *dev, struct device_attribute *attr, char *buf)
929{
930 return sprintf(buf, "asus\n");
931}
932static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
933
934static struct attribute *hwmon_attributes[] = {
935 &sensor_dev_attr_pwm1.dev_attr.attr,
936 &sensor_dev_attr_name.dev_attr.attr,
937 NULL
938};
939
940static mode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
941 struct attribute *attr, int idx)
942{
943 struct device *dev = container_of(kobj, struct device, kobj);
944 struct platform_device *pdev = to_platform_device(dev->parent);
945 struct asus_wmi *asus = platform_get_drvdata(pdev);
946 bool ok = true;
947 int dev_id = -1;
948 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
949
950 if (attr == &sensor_dev_attr_pwm1.dev_attr.attr)
951 dev_id = ASUS_WMI_DEVID_FAN_CTRL;
952
953 if (dev_id != -1) {
954 int err = asus_wmi_get_devstate(asus, dev_id, &value);
955
956 if (err < 0)
Al Viroe772aed2011-07-23 20:59:40 -0400957 return 0; /* can't return negative here */
Corentin Charye07babd2011-02-26 10:20:42 +0100958 }
959
960 if (dev_id == ASUS_WMI_DEVID_FAN_CTRL) {
961 /*
962 * We need to find a better way, probably using sfun,
963 * bits or spec ...
964 * Currently we disable it if:
965 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
966 * - reverved bits are non-zero
967 * - sfun and presence bit are not set
968 */
Corentin Chary49979d02011-07-01 11:34:26 +0200969 if (value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
Corentin Charye07babd2011-02-26 10:20:42 +0100970 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)))
971 ok = false;
972 }
973
974 return ok ? attr->mode : 0;
975}
976
977static struct attribute_group hwmon_attribute_group = {
978 .is_visible = asus_hwmon_sysfs_is_visible,
979 .attrs = hwmon_attributes
980};
981
982static void asus_wmi_hwmon_exit(struct asus_wmi *asus)
983{
984 struct device *hwmon;
985
986 hwmon = asus->hwmon_device;
987 if (!hwmon)
988 return;
989 sysfs_remove_group(&hwmon->kobj, &hwmon_attribute_group);
990 hwmon_device_unregister(hwmon);
991 asus->hwmon_device = NULL;
992}
993
994static int asus_wmi_hwmon_init(struct asus_wmi *asus)
995{
996 struct device *hwmon;
997 int result;
998
999 hwmon = hwmon_device_register(&asus->platform_device->dev);
1000 if (IS_ERR(hwmon)) {
1001 pr_err("Could not register asus hwmon device\n");
1002 return PTR_ERR(hwmon);
1003 }
Corentin Chary49979d02011-07-01 11:34:26 +02001004 dev_set_drvdata(hwmon, asus);
Corentin Charye07babd2011-02-26 10:20:42 +01001005 asus->hwmon_device = hwmon;
1006 result = sysfs_create_group(&hwmon->kobj, &hwmon_attribute_group);
1007 if (result)
1008 asus_wmi_hwmon_exit(asus);
1009 return result;
1010}
1011
1012/*
Corentin Chary084fca62010-11-29 08:14:06 +01001013 * Backlight
1014 */
Corentin Chary1d070f892011-02-26 10:20:36 +01001015static int read_backlight_power(struct asus_wmi *asus)
Corentin Charyb7187262011-02-06 13:28:39 +01001016{
Corentin Chary1d070f892011-02-26 10:20:36 +01001017 int ret = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_BACKLIGHT);
Corentin Charyb7187262011-02-06 13:28:39 +01001018
1019 if (ret < 0)
1020 return ret;
1021
1022 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1023}
1024
Corentin Chary8fbea012011-02-26 10:20:37 +01001025static int read_brightness_max(struct asus_wmi *asus)
1026{
1027 u32 retval;
1028 int err;
1029
1030 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1031
1032 if (err < 0)
1033 return err;
1034
1035 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
1036 retval >>= 8;
1037
1038 if (!retval)
1039 return -ENODEV;
1040
1041 return retval;
1042}
1043
Yong Wang3d7b1652010-04-11 09:27:54 +08001044static int read_brightness(struct backlight_device *bd)
1045{
Corentin Chary1d070f892011-02-26 10:20:36 +01001046 struct asus_wmi *asus = bl_get_data(bd);
Dan Carpenter0986f252011-03-15 10:06:23 +03001047 u32 retval;
1048 int err;
Yong Wang3d7b1652010-04-11 09:27:54 +08001049
Corentin Chary1d070f892011-02-26 10:20:36 +01001050 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
Yong Wang3d7b1652010-04-11 09:27:54 +08001051
Corentin Charyd33da3b2011-02-26 10:20:35 +01001052 if (err < 0)
1053 return err;
1054
1055 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
Yong Wang3d7b1652010-04-11 09:27:54 +08001056}
1057
1058static int update_bl_status(struct backlight_device *bd)
1059{
Corentin Chary1d070f892011-02-26 10:20:36 +01001060 struct asus_wmi *asus = bl_get_data(bd);
Corentin Charydfed65d2010-11-29 08:14:12 +01001061 u32 ctrl_param;
Corentin Charyd33da3b2011-02-26 10:20:35 +01001062 int power, err;
Yong Wang3d7b1652010-04-11 09:27:54 +08001063
1064 ctrl_param = bd->props.brightness;
1065
Corentin Charyd33da3b2011-02-26 10:20:35 +01001066 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
1067 ctrl_param, NULL);
Yong Wang3d7b1652010-04-11 09:27:54 +08001068
Corentin Charyd33da3b2011-02-26 10:20:35 +01001069 if (err < 0)
1070 return err;
Corentin Charyb7187262011-02-06 13:28:39 +01001071
Corentin Chary1d070f892011-02-26 10:20:36 +01001072 power = read_backlight_power(asus);
Corentin Charyb7187262011-02-06 13:28:39 +01001073 if (power != -ENODEV && bd->props.power != power) {
1074 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
Corentin Charyd33da3b2011-02-26 10:20:35 +01001075 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
1076 ctrl_param, NULL);
Corentin Charyb7187262011-02-06 13:28:39 +01001077 }
Corentin Chary8fbea012011-02-26 10:20:37 +01001078 return err;
Yong Wang3d7b1652010-04-11 09:27:54 +08001079}
1080
Corentin Charye12e6d92011-02-26 10:20:31 +01001081static const struct backlight_ops asus_wmi_bl_ops = {
Yong Wang3d7b1652010-04-11 09:27:54 +08001082 .get_brightness = read_brightness,
1083 .update_status = update_bl_status,
1084};
1085
Corentin Charye12e6d92011-02-26 10:20:31 +01001086static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
Yong Wang3d7b1652010-04-11 09:27:54 +08001087{
Corentin Charye12e6d92011-02-26 10:20:31 +01001088 struct backlight_device *bd = asus->backlight_device;
Yong Wang3d7b1652010-04-11 09:27:54 +08001089 int old = bd->props.brightness;
Daniel Mackb7670ed2010-05-19 12:37:01 +02001090 int new = old;
Yong Wang3d7b1652010-04-11 09:27:54 +08001091
1092 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1093 new = code - NOTIFY_BRNUP_MIN + 1;
1094 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
1095 new = code - NOTIFY_BRNDOWN_MIN;
1096
1097 bd->props.brightness = new;
1098 backlight_update_status(bd);
1099 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
1100
1101 return old;
1102}
1103
Corentin Charye12e6d92011-02-26 10:20:31 +01001104static int asus_wmi_backlight_init(struct asus_wmi *asus)
Yong Wang3d7b1652010-04-11 09:27:54 +08001105{
1106 struct backlight_device *bd;
1107 struct backlight_properties props;
Corentin Charyb7187262011-02-06 13:28:39 +01001108 int max;
1109 int power;
1110
Corentin Chary8fbea012011-02-26 10:20:37 +01001111 max = read_brightness_max(asus);
Corentin Charyb7187262011-02-06 13:28:39 +01001112
Corentin Charyb7187262011-02-06 13:28:39 +01001113 if (max == -ENODEV)
1114 max = 0;
Corentin Chary8fbea012011-02-26 10:20:37 +01001115 else if (max < 0)
1116 return max;
1117
1118 power = read_backlight_power(asus);
1119
Corentin Charyb7187262011-02-06 13:28:39 +01001120 if (power == -ENODEV)
1121 power = FB_BLANK_UNBLANK;
Corentin Chary8fbea012011-02-26 10:20:37 +01001122 else if (power < 0)
1123 return power;
Yong Wang3d7b1652010-04-11 09:27:54 +08001124
1125 memset(&props, 0, sizeof(struct backlight_properties));
Axel Lin60cfa092011-06-29 11:43:30 +08001126 props.type = BACKLIGHT_PLATFORM;
Corentin Charyb7187262011-02-06 13:28:39 +01001127 props.max_brightness = max;
Corentin Charye12e6d92011-02-26 10:20:31 +01001128 bd = backlight_device_register(asus->driver->name,
1129 &asus->platform_device->dev, asus,
1130 &asus_wmi_bl_ops, &props);
Yong Wang3d7b1652010-04-11 09:27:54 +08001131 if (IS_ERR(bd)) {
1132 pr_err("Could not register backlight device\n");
1133 return PTR_ERR(bd);
1134 }
1135
Corentin Charye12e6d92011-02-26 10:20:31 +01001136 asus->backlight_device = bd;
Yong Wang3d7b1652010-04-11 09:27:54 +08001137
1138 bd->props.brightness = read_brightness(bd);
Corentin Charyb7187262011-02-06 13:28:39 +01001139 bd->props.power = power;
Yong Wang3d7b1652010-04-11 09:27:54 +08001140 backlight_update_status(bd);
1141
1142 return 0;
1143}
1144
Corentin Charye12e6d92011-02-26 10:20:31 +01001145static void asus_wmi_backlight_exit(struct asus_wmi *asus)
Yong Wang3d7b1652010-04-11 09:27:54 +08001146{
Corentin Charye12e6d92011-02-26 10:20:31 +01001147 if (asus->backlight_device)
1148 backlight_device_unregister(asus->backlight_device);
Yong Wang3d7b1652010-04-11 09:27:54 +08001149
Corentin Charye12e6d92011-02-26 10:20:31 +01001150 asus->backlight_device = NULL;
Yong Wang3d7b1652010-04-11 09:27:54 +08001151}
1152
Corentin Charye12e6d92011-02-26 10:20:31 +01001153static void asus_wmi_notify(u32 value, void *context)
Yong Wang3d7b1652010-04-11 09:27:54 +08001154{
Corentin Charye12e6d92011-02-26 10:20:31 +01001155 struct asus_wmi *asus = context;
Yong Wang3d7b1652010-04-11 09:27:54 +08001156 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
1157 union acpi_object *obj;
1158 acpi_status status;
1159 int code;
1160 int orig_code;
Seth Forsheec4453f62011-07-01 11:34:27 +02001161 unsigned int key_value = 1;
1162 bool autorelease = 1;
Yong Wang3d7b1652010-04-11 09:27:54 +08001163
1164 status = wmi_get_event_data(value, &response);
1165 if (status != AE_OK) {
1166 pr_err("bad event status 0x%x\n", status);
1167 return;
1168 }
1169
1170 obj = (union acpi_object *)response.pointer;
1171
Corentin Chary57ab7da2011-02-26 10:20:32 +01001172 if (!obj || obj->type != ACPI_TYPE_INTEGER)
1173 goto exit;
Yong Wang3d7b1652010-04-11 09:27:54 +08001174
Corentin Chary57ab7da2011-02-26 10:20:32 +01001175 code = obj->integer.value;
1176 orig_code = code;
Yong Wang3d7b1652010-04-11 09:27:54 +08001177
Seth Forsheec4453f62011-07-01 11:34:27 +02001178 if (asus->driver->key_filter) {
1179 asus->driver->key_filter(asus->driver, &code, &key_value,
1180 &autorelease);
1181 if (code == ASUS_WMI_KEY_IGNORE)
1182 goto exit;
1183 }
1184
Corentin Chary57ab7da2011-02-26 10:20:32 +01001185 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1186 code = NOTIFY_BRNUP_MIN;
1187 else if (code >= NOTIFY_BRNDOWN_MIN &&
1188 code <= NOTIFY_BRNDOWN_MAX)
1189 code = NOTIFY_BRNDOWN_MIN;
Yong Wang3d7b1652010-04-11 09:27:54 +08001190
Corentin Chary57ab7da2011-02-26 10:20:32 +01001191 if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
1192 if (!acpi_video_backlight_support())
1193 asus_wmi_backlight_notify(asus, orig_code);
Seth Forsheec4453f62011-07-01 11:34:27 +02001194 } else if (!sparse_keymap_report_event(asus->inputdev, code,
1195 key_value, autorelease))
Corentin Chary57ab7da2011-02-26 10:20:32 +01001196 pr_info("Unknown key %x pressed\n", code);
Yong Wang3d7b1652010-04-11 09:27:54 +08001197
Corentin Chary57ab7da2011-02-26 10:20:32 +01001198exit:
Yong Wang3d7b1652010-04-11 09:27:54 +08001199 kfree(obj);
1200}
1201
Corentin Chary9e1565b2011-02-06 13:28:36 +01001202/*
1203 * Sys helpers
1204 */
1205static int parse_arg(const char *buf, unsigned long count, int *val)
1206{
1207 if (!count)
1208 return 0;
1209 if (sscanf(buf, "%i", val) != 1)
1210 return -EINVAL;
1211 return count;
1212}
1213
Corentin Chary1d070f892011-02-26 10:20:36 +01001214static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
1215 const char *buf, size_t count)
Corentin Chary9e1565b2011-02-06 13:28:36 +01001216{
Corentin Chary9e1565b2011-02-06 13:28:36 +01001217 u32 retval;
Corentin Charyd33da3b2011-02-26 10:20:35 +01001218 int rv, err, value;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001219
Corentin Chary1d070f892011-02-26 10:20:36 +01001220 value = asus_wmi_get_devstate_simple(asus, devid);
Corentin Charye12e6d92011-02-26 10:20:31 +01001221 if (value == -ENODEV) /* Check device presence */
Corentin Chary9e1565b2011-02-06 13:28:36 +01001222 return value;
1223
1224 rv = parse_arg(buf, count, &value);
Corentin Charyd33da3b2011-02-26 10:20:35 +01001225 err = asus_wmi_set_devstate(devid, value, &retval);
Corentin Chary9e1565b2011-02-06 13:28:36 +01001226
Corentin Charyd33da3b2011-02-26 10:20:35 +01001227 if (err < 0)
1228 return err;
1229
Corentin Chary9e1565b2011-02-06 13:28:36 +01001230 return rv;
1231}
1232
Corentin Chary1d070f892011-02-26 10:20:36 +01001233static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
Corentin Chary9e1565b2011-02-06 13:28:36 +01001234{
Corentin Chary1d070f892011-02-26 10:20:36 +01001235 int value = asus_wmi_get_devstate_simple(asus, devid);
Corentin Chary9e1565b2011-02-06 13:28:36 +01001236
1237 if (value < 0)
1238 return value;
1239
1240 return sprintf(buf, "%d\n", value);
1241}
1242
Corentin Charye12e6d92011-02-26 10:20:31 +01001243#define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
Corentin Chary9e1565b2011-02-06 13:28:36 +01001244 static ssize_t show_##_name(struct device *dev, \
1245 struct device_attribute *attr, \
1246 char *buf) \
1247 { \
Corentin Chary1d070f892011-02-26 10:20:36 +01001248 struct asus_wmi *asus = dev_get_drvdata(dev); \
1249 \
1250 return show_sys_wmi(asus, _cm, buf); \
Corentin Chary9e1565b2011-02-06 13:28:36 +01001251 } \
1252 static ssize_t store_##_name(struct device *dev, \
1253 struct device_attribute *attr, \
1254 const char *buf, size_t count) \
1255 { \
Corentin Chary1d070f892011-02-26 10:20:36 +01001256 struct asus_wmi *asus = dev_get_drvdata(dev); \
1257 \
1258 return store_sys_wmi(asus, _cm, buf, count); \
Corentin Chary9e1565b2011-02-06 13:28:36 +01001259 } \
1260 static struct device_attribute dev_attr_##_name = { \
1261 .attr = { \
1262 .name = __stringify(_name), \
1263 .mode = _mode }, \
1264 .show = show_##_name, \
1265 .store = store_##_name, \
1266 }
1267
Corentin Charye12e6d92011-02-26 10:20:31 +01001268ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
1269ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
1270ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
Corentin Chary9e1565b2011-02-06 13:28:36 +01001271
Dmitry Torokhov67fa38e2010-11-03 11:14:01 -07001272static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
1273 const char *buf, size_t count)
Chris Bagwell7f80d732010-10-11 18:47:18 -05001274{
1275 int value;
Chris Bagwell7f80d732010-10-11 18:47:18 -05001276
1277 if (!count || sscanf(buf, "%i", &value) != 1)
1278 return -EINVAL;
1279 if (value < 0 || value > 2)
1280 return -EINVAL;
1281
Corentin Charyd33da3b2011-02-26 10:20:35 +01001282 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001283}
1284
1285static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
1286
Corentin Chary4e37b422010-11-29 08:14:08 +01001287static struct attribute *platform_attributes[] = {
1288 &dev_attr_cpufv.attr,
Corentin Chary9e1565b2011-02-06 13:28:36 +01001289 &dev_attr_camera.attr,
1290 &dev_attr_cardr.attr,
Corentin Chary4615bb62011-02-06 13:28:42 +01001291 &dev_attr_touchpad.attr,
Corentin Chary4e37b422010-11-29 08:14:08 +01001292 NULL
1293};
1294
Corentin Charye12e6d92011-02-26 10:20:31 +01001295static mode_t asus_sysfs_is_visible(struct kobject *kobj,
1296 struct attribute *attr, int idx)
Corentin Chary9e1565b2011-02-06 13:28:36 +01001297{
Corentin Chary1d070f892011-02-26 10:20:36 +01001298 struct device *dev = container_of(kobj, struct device, kobj);
1299 struct platform_device *pdev = to_platform_device(dev);
1300 struct asus_wmi *asus = platform_get_drvdata(pdev);
1301 bool ok = true;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001302 int devid = -1;
1303
1304 if (attr == &dev_attr_camera.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01001305 devid = ASUS_WMI_DEVID_CAMERA;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001306 else if (attr == &dev_attr_cardr.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01001307 devid = ASUS_WMI_DEVID_CARDREADER;
Corentin Chary4615bb62011-02-06 13:28:42 +01001308 else if (attr == &dev_attr_touchpad.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01001309 devid = ASUS_WMI_DEVID_TOUCHPAD;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001310
1311 if (devid != -1)
Corentin Chary1d070f892011-02-26 10:20:36 +01001312 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
Corentin Chary9e1565b2011-02-06 13:28:36 +01001313
Corentin Chary1d070f892011-02-26 10:20:36 +01001314 return ok ? attr->mode : 0;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001315}
1316
Corentin Chary4e37b422010-11-29 08:14:08 +01001317static struct attribute_group platform_attribute_group = {
Corentin Charye12e6d92011-02-26 10:20:31 +01001318 .is_visible = asus_sysfs_is_visible,
1319 .attrs = platform_attributes
Corentin Chary4e37b422010-11-29 08:14:08 +01001320};
1321
Corentin Charye12e6d92011-02-26 10:20:31 +01001322static void asus_wmi_sysfs_exit(struct platform_device *device)
Chris Bagwell7f80d732010-10-11 18:47:18 -05001323{
Corentin Chary4e37b422010-11-29 08:14:08 +01001324 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001325}
1326
Corentin Charye12e6d92011-02-26 10:20:31 +01001327static int asus_wmi_sysfs_init(struct platform_device *device)
Chris Bagwell7f80d732010-10-11 18:47:18 -05001328{
Corentin Chary4e37b422010-11-29 08:14:08 +01001329 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001330}
1331
Corentin Chary27c136c2010-11-29 08:14:05 +01001332/*
1333 * Platform device
1334 */
Joe Perches39ddf3b2011-03-29 15:21:32 -07001335static int asus_wmi_platform_init(struct asus_wmi *asus)
Corentin Chary27c136c2010-11-29 08:14:05 +01001336{
Corentin Chary46dbca82011-02-26 10:20:38 +01001337 int rv;
1338
1339 /* INIT enable hotkeys on some models */
1340 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
1341 pr_info("Initialization: %#x", rv);
1342
1343 /* We don't know yet what to do with this version... */
1344 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
Corentin Chary57d5c8e2011-07-01 11:34:30 +02001345 pr_info("BIOS WMI version: %d.%d", rv >> 16, rv & 0xFF);
Corentin Chary46dbca82011-02-26 10:20:38 +01001346 asus->spec = rv;
1347 }
1348
1349 /*
1350 * The SFUN method probably allows the original driver to get the list
1351 * of features supported by a given model. For now, 0x0100 or 0x0800
1352 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
1353 * The significance of others is yet to be found.
1354 */
1355 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
1356 pr_info("SFUN value: %#x", rv);
1357 asus->sfun = rv;
1358 }
1359
Corentin Chary1d070f892011-02-26 10:20:36 +01001360 /*
1361 * Eee PC and Notebooks seems to have different method_id for DSTS,
1362 * but it may also be related to the BIOS's SPEC.
1363 * Note, on most Eeepc, there is no way to check if a method exist
1364 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
1365 * but once again, SPEC may probably be used for that kind of things.
1366 */
1367 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS, 0, 0, NULL))
1368 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
1369 else if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS2, 0, 0, NULL))
1370 asus->dsts_id = ASUS_WMI_METHODID_DSTS2;
1371
1372 if (!asus->dsts_id) {
1373 pr_err("Can't find DSTS");
1374 return -ENODEV;
1375 }
1376
Corentin Charye12e6d92011-02-26 10:20:31 +01001377 return asus_wmi_sysfs_init(asus->platform_device);
Corentin Chary27c136c2010-11-29 08:14:05 +01001378}
1379
Corentin Charye12e6d92011-02-26 10:20:31 +01001380static void asus_wmi_platform_exit(struct asus_wmi *asus)
Corentin Chary27c136c2010-11-29 08:14:05 +01001381{
Corentin Charye12e6d92011-02-26 10:20:31 +01001382 asus_wmi_sysfs_exit(asus->platform_device);
Corentin Chary27c136c2010-11-29 08:14:05 +01001383}
1384
1385/*
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001386 * debugfs
1387 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001388struct asus_wmi_debugfs_node {
1389 struct asus_wmi *asus;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001390 char *name;
Corentin Charye12e6d92011-02-26 10:20:31 +01001391 int (*show) (struct seq_file *m, void *data);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001392};
1393
1394static int show_dsts(struct seq_file *m, void *data)
1395{
Corentin Charye12e6d92011-02-26 10:20:31 +01001396 struct asus_wmi *asus = m->private;
Corentin Charyd33da3b2011-02-26 10:20:35 +01001397 int err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001398 u32 retval = -1;
1399
Corentin Chary1d070f892011-02-26 10:20:36 +01001400 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001401
Corentin Charyd33da3b2011-02-26 10:20:35 +01001402 if (err < 0)
1403 return err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001404
Corentin Charyef343492011-02-26 10:20:39 +01001405 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001406
1407 return 0;
1408}
1409
1410static int show_devs(struct seq_file *m, void *data)
1411{
Corentin Charye12e6d92011-02-26 10:20:31 +01001412 struct asus_wmi *asus = m->private;
Corentin Charyd33da3b2011-02-26 10:20:35 +01001413 int err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001414 u32 retval = -1;
1415
Corentin Charyd33da3b2011-02-26 10:20:35 +01001416 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
1417 &retval);
1418
1419 if (err < 0)
1420 return err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001421
Corentin Charyef343492011-02-26 10:20:39 +01001422 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
Corentin Charye12e6d92011-02-26 10:20:31 +01001423 asus->debug.ctrl_param, retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001424
1425 return 0;
1426}
1427
Corentin Charyef343492011-02-26 10:20:39 +01001428static int show_call(struct seq_file *m, void *data)
1429{
1430 struct asus_wmi *asus = m->private;
1431 struct bios_args args = {
1432 .arg0 = asus->debug.dev_id,
1433 .arg1 = asus->debug.ctrl_param,
1434 };
1435 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1436 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
1437 union acpi_object *obj;
1438 acpi_status status;
1439
1440 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
1441 1, asus->debug.method_id,
1442 &input, &output);
1443
1444 if (ACPI_FAILURE(status))
1445 return -EIO;
1446
1447 obj = (union acpi_object *)output.pointer;
1448 if (obj && obj->type == ACPI_TYPE_INTEGER)
1449 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
1450 asus->debug.dev_id, asus->debug.ctrl_param,
1451 (u32) obj->integer.value);
1452 else
1453 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
1454 asus->debug.dev_id, asus->debug.ctrl_param,
Dan Carpentera1d60862011-03-15 10:07:37 +03001455 obj ? obj->type : -1);
Corentin Charyef343492011-02-26 10:20:39 +01001456
1457 kfree(obj);
1458
1459 return 0;
1460}
1461
Corentin Charye12e6d92011-02-26 10:20:31 +01001462static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
1463 {NULL, "devs", show_devs},
1464 {NULL, "dsts", show_dsts},
Corentin Charyef343492011-02-26 10:20:39 +01001465 {NULL, "call", show_call},
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001466};
1467
Corentin Charye12e6d92011-02-26 10:20:31 +01001468static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001469{
Corentin Charye12e6d92011-02-26 10:20:31 +01001470 struct asus_wmi_debugfs_node *node = inode->i_private;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001471
Corentin Charye12e6d92011-02-26 10:20:31 +01001472 return single_open(file, node->show, node->asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001473}
1474
Corentin Charye12e6d92011-02-26 10:20:31 +01001475static const struct file_operations asus_wmi_debugfs_io_ops = {
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001476 .owner = THIS_MODULE,
Corentin Charye12e6d92011-02-26 10:20:31 +01001477 .open = asus_wmi_debugfs_open,
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001478 .read = seq_read,
1479 .llseek = seq_lseek,
1480 .release = single_release,
1481};
1482
Corentin Charye12e6d92011-02-26 10:20:31 +01001483static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001484{
Corentin Charye12e6d92011-02-26 10:20:31 +01001485 debugfs_remove_recursive(asus->debug.root);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001486}
1487
Corentin Charye12e6d92011-02-26 10:20:31 +01001488static int asus_wmi_debugfs_init(struct asus_wmi *asus)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001489{
1490 struct dentry *dent;
1491 int i;
1492
Corentin Charye12e6d92011-02-26 10:20:31 +01001493 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
1494 if (!asus->debug.root) {
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001495 pr_err("failed to create debugfs directory");
1496 goto error_debugfs;
1497 }
1498
Corentin Charyef343492011-02-26 10:20:39 +01001499 dent = debugfs_create_x32("method_id", S_IRUGO | S_IWUSR,
1500 asus->debug.root, &asus->debug.method_id);
1501 if (!dent)
1502 goto error_debugfs;
1503
Corentin Charye12e6d92011-02-26 10:20:31 +01001504 dent = debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR,
1505 asus->debug.root, &asus->debug.dev_id);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001506 if (!dent)
1507 goto error_debugfs;
1508
Corentin Charye12e6d92011-02-26 10:20:31 +01001509 dent = debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR,
1510 asus->debug.root, &asus->debug.ctrl_param);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001511 if (!dent)
1512 goto error_debugfs;
1513
Corentin Charye12e6d92011-02-26 10:20:31 +01001514 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
1515 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001516
Corentin Charye12e6d92011-02-26 10:20:31 +01001517 node->asus = asus;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001518 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
Corentin Charye12e6d92011-02-26 10:20:31 +01001519 asus->debug.root, node,
1520 &asus_wmi_debugfs_io_ops);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001521 if (!dent) {
1522 pr_err("failed to create debug file: %s\n", node->name);
1523 goto error_debugfs;
1524 }
1525 }
1526
1527 return 0;
1528
1529error_debugfs:
Corentin Charye12e6d92011-02-26 10:20:31 +01001530 asus_wmi_debugfs_exit(asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001531 return -ENOMEM;
1532}
1533
1534/*
Corentin Chary27c136c2010-11-29 08:14:05 +01001535 * WMI Driver
1536 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001537static int asus_wmi_add(struct platform_device *pdev)
Corentin Charyafa7c882011-02-06 13:28:28 +01001538{
Corentin Charye12e6d92011-02-26 10:20:31 +01001539 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
1540 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
1541 struct asus_wmi *asus;
Yong Wangee027e42010-03-21 10:26:34 +08001542 acpi_status status;
Corentin Chary27c136c2010-11-29 08:14:05 +01001543 int err;
Yong Wangee027e42010-03-21 10:26:34 +08001544
Corentin Charye12e6d92011-02-26 10:20:31 +01001545 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
1546 if (!asus)
Corentin Charya04ce292011-02-06 13:28:33 +01001547 return -ENOMEM;
1548
Corentin Charye12e6d92011-02-26 10:20:31 +01001549 asus->driver = wdrv;
1550 asus->platform_device = pdev;
1551 wdrv->platform_device = pdev;
1552 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001553
Corentin Charye12e6d92011-02-26 10:20:31 +01001554 if (wdrv->quirks)
1555 wdrv->quirks(asus->driver);
Corentin Charyafa7c882011-02-06 13:28:28 +01001556
Corentin Charye12e6d92011-02-26 10:20:31 +01001557 err = asus_wmi_platform_init(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001558 if (err)
1559 goto fail_platform;
Yong Wang45f2c692010-04-11 09:27:19 +08001560
Corentin Charye12e6d92011-02-26 10:20:31 +01001561 err = asus_wmi_input_init(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08001562 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +01001563 goto fail_input;
Yong Wang3d7b1652010-04-11 09:27:54 +08001564
Corentin Charye07babd2011-02-26 10:20:42 +01001565 err = asus_wmi_hwmon_init(asus);
1566 if (err)
1567 goto fail_hwmon;
1568
Corentin Charye12e6d92011-02-26 10:20:31 +01001569 err = asus_wmi_led_init(asus);
Corentin Chary084fca62010-11-29 08:14:06 +01001570 if (err)
1571 goto fail_leds;
1572
Corentin Charye12e6d92011-02-26 10:20:31 +01001573 err = asus_wmi_rfkill_init(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001574 if (err)
1575 goto fail_rfkill;
1576
Yong Wang3d7b1652010-04-11 09:27:54 +08001577 if (!acpi_video_backlight_support()) {
Corentin Charye12e6d92011-02-26 10:20:31 +01001578 err = asus_wmi_backlight_init(asus);
Corentin Charyb7187262011-02-06 13:28:39 +01001579 if (err && err != -ENODEV)
Corentin Chary27c136c2010-11-29 08:14:05 +01001580 goto fail_backlight;
Yong Wang3d7b1652010-04-11 09:27:54 +08001581 } else
1582 pr_info("Backlight controlled by ACPI video driver\n");
Yong Wang45f2c692010-04-11 09:27:19 +08001583
Corentin Charye12e6d92011-02-26 10:20:31 +01001584 status = wmi_install_notify_handler(asus->driver->event_guid,
1585 asus_wmi_notify, asus);
Yong Wang45f2c692010-04-11 09:27:19 +08001586 if (ACPI_FAILURE(status)) {
Corentin Charye12e6d92011-02-26 10:20:31 +01001587 pr_err("Unable to register notify handler - %d\n", status);
Yong Wang45f2c692010-04-11 09:27:19 +08001588 err = -ENODEV;
Corentin Chary27c136c2010-11-29 08:14:05 +01001589 goto fail_wmi_handler;
Yong Wang45f2c692010-04-11 09:27:19 +08001590 }
1591
Corentin Charye12e6d92011-02-26 10:20:31 +01001592 err = asus_wmi_debugfs_init(asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001593 if (err)
1594 goto fail_debugfs;
1595
Corentin Charya04ce292011-02-06 13:28:33 +01001596 return 0;
Yong Wang45f2c692010-04-11 09:27:19 +08001597
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001598fail_debugfs:
Corentin Charye12e6d92011-02-26 10:20:31 +01001599 wmi_remove_notify_handler(asus->driver->event_guid);
Corentin Chary27c136c2010-11-29 08:14:05 +01001600fail_wmi_handler:
Corentin Charye12e6d92011-02-26 10:20:31 +01001601 asus_wmi_backlight_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001602fail_backlight:
Corentin Charye12e6d92011-02-26 10:20:31 +01001603 asus_wmi_rfkill_exit(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001604fail_rfkill:
Corentin Charye12e6d92011-02-26 10:20:31 +01001605 asus_wmi_led_exit(asus);
Corentin Chary084fca62010-11-29 08:14:06 +01001606fail_leds:
Corentin Charye07babd2011-02-26 10:20:42 +01001607 asus_wmi_hwmon_exit(asus);
1608fail_hwmon:
Corentin Charye12e6d92011-02-26 10:20:31 +01001609 asus_wmi_input_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001610fail_input:
Corentin Charye12e6d92011-02-26 10:20:31 +01001611 asus_wmi_platform_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001612fail_platform:
Corentin Charye12e6d92011-02-26 10:20:31 +01001613 kfree(asus);
Corentin Charya04ce292011-02-06 13:28:33 +01001614 return err;
Yong Wang45f2c692010-04-11 09:27:19 +08001615}
1616
Corentin Charye12e6d92011-02-26 10:20:31 +01001617static int asus_wmi_remove(struct platform_device *device)
Yong Wang45f2c692010-04-11 09:27:19 +08001618{
Corentin Charye12e6d92011-02-26 10:20:31 +01001619 struct asus_wmi *asus;
Yong Wang45f2c692010-04-11 09:27:19 +08001620
Corentin Charye12e6d92011-02-26 10:20:31 +01001621 asus = platform_get_drvdata(device);
1622 wmi_remove_notify_handler(asus->driver->event_guid);
1623 asus_wmi_backlight_exit(asus);
1624 asus_wmi_input_exit(asus);
Corentin Charye07babd2011-02-26 10:20:42 +01001625 asus_wmi_hwmon_exit(asus);
Corentin Charye12e6d92011-02-26 10:20:31 +01001626 asus_wmi_led_exit(asus);
1627 asus_wmi_rfkill_exit(asus);
1628 asus_wmi_debugfs_exit(asus);
1629 asus_wmi_platform_exit(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08001630
Corentin Charye12e6d92011-02-26 10:20:31 +01001631 kfree(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08001632 return 0;
1633}
1634
Corentin Chary0773d7f2011-02-06 13:28:32 +01001635/*
1636 * Platform driver - hibernate/resume callbacks
1637 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001638static int asus_hotk_thaw(struct device *device)
Corentin Chary0773d7f2011-02-06 13:28:32 +01001639{
Corentin Charye12e6d92011-02-26 10:20:31 +01001640 struct asus_wmi *asus = dev_get_drvdata(device);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001641
Corentin Charya7ce3f02011-02-26 10:20:33 +01001642 if (asus->wlan.rfkill) {
Corentin Chary0773d7f2011-02-06 13:28:32 +01001643 bool wlan;
1644
1645 /*
1646 * Work around bios bug - acpi _PTS turns off the wireless led
1647 * during suspend. Normally it restores it on resume, but
1648 * we should kick it ourselves in case hibernation is aborted.
1649 */
Corentin Chary1d070f892011-02-26 10:20:36 +01001650 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charye12e6d92011-02-26 10:20:31 +01001651 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001652 }
1653
1654 return 0;
1655}
1656
Corentin Charye12e6d92011-02-26 10:20:31 +01001657static int asus_hotk_restore(struct device *device)
Corentin Chary0773d7f2011-02-06 13:28:32 +01001658{
Corentin Charye12e6d92011-02-26 10:20:31 +01001659 struct asus_wmi *asus = dev_get_drvdata(device);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001660 int bl;
1661
1662 /* Refresh both wlan rfkill state and pci hotplug */
Corentin Charya7ce3f02011-02-26 10:20:33 +01001663 if (asus->wlan.rfkill)
Corentin Charye12e6d92011-02-26 10:20:31 +01001664 asus_rfkill_hotplug(asus);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001665
Corentin Charya7ce3f02011-02-26 10:20:33 +01001666 if (asus->bluetooth.rfkill) {
Corentin Chary1d070f892011-02-26 10:20:36 +01001667 bl = !asus_wmi_get_devstate_simple(asus,
1668 ASUS_WMI_DEVID_BLUETOOTH);
Corentin Charya7ce3f02011-02-26 10:20:33 +01001669 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01001670 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01001671 if (asus->wimax.rfkill) {
Corentin Chary1d070f892011-02-26 10:20:36 +01001672 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
Corentin Charya7ce3f02011-02-26 10:20:33 +01001673 rfkill_set_sw_state(asus->wimax.rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01001674 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01001675 if (asus->wwan3g.rfkill) {
Corentin Chary1d070f892011-02-26 10:20:36 +01001676 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
Corentin Charya7ce3f02011-02-26 10:20:33 +01001677 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001678 }
1679
1680 return 0;
1681}
1682
Corentin Charye12e6d92011-02-26 10:20:31 +01001683static const struct dev_pm_ops asus_pm_ops = {
1684 .thaw = asus_hotk_thaw,
1685 .restore = asus_hotk_restore,
Corentin Chary0773d7f2011-02-06 13:28:32 +01001686};
1687
Corentin Charye12e6d92011-02-26 10:20:31 +01001688static int asus_wmi_probe(struct platform_device *pdev)
Corentin Charyd358cb52010-11-29 08:14:14 +01001689{
Corentin Charye12e6d92011-02-26 10:20:31 +01001690 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
1691 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
1692 int ret;
Corentin Charyd358cb52010-11-29 08:14:14 +01001693
Corentin Charye12e6d92011-02-26 10:20:31 +01001694 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -07001695 pr_warn("Management GUID not found\n");
Yong Wangee027e42010-03-21 10:26:34 +08001696 return -ENODEV;
1697 }
1698
Corentin Charye12e6d92011-02-26 10:20:31 +01001699 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -07001700 pr_warn("Event GUID not found\n");
Corentin Charyd358cb52010-11-29 08:14:14 +01001701 return -ENODEV;
1702 }
1703
Corentin Charye12e6d92011-02-26 10:20:31 +01001704 if (wdrv->probe) {
1705 ret = wdrv->probe(pdev);
1706 if (ret)
1707 return ret;
1708 }
1709
1710 return asus_wmi_add(pdev);
Corentin Charya04ce292011-02-06 13:28:33 +01001711}
Yong Wangee027e42010-03-21 10:26:34 +08001712
Corentin Charye12e6d92011-02-26 10:20:31 +01001713static bool used;
Yong Wangee027e42010-03-21 10:26:34 +08001714
Corentin Charye12e6d92011-02-26 10:20:31 +01001715int asus_wmi_register_driver(struct asus_wmi_driver *driver)
Corentin Charya04ce292011-02-06 13:28:33 +01001716{
Corentin Charye12e6d92011-02-26 10:20:31 +01001717 struct platform_driver *platform_driver;
1718 struct platform_device *platform_device;
1719
1720 if (used)
1721 return -EBUSY;
1722
1723 platform_driver = &driver->platform_driver;
1724 platform_driver->remove = asus_wmi_remove;
1725 platform_driver->driver.owner = driver->owner;
1726 platform_driver->driver.name = driver->name;
1727 platform_driver->driver.pm = &asus_pm_ops;
1728
1729 platform_device = platform_create_bundle(platform_driver,
1730 asus_wmi_probe,
Corentin Charya04ce292011-02-06 13:28:33 +01001731 NULL, 0, NULL, 0);
1732 if (IS_ERR(platform_device))
1733 return PTR_ERR(platform_device);
Corentin Charye12e6d92011-02-26 10:20:31 +01001734
1735 used = true;
1736 return 0;
1737}
1738EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
1739
1740void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
1741{
1742 platform_device_unregister(driver->platform_device);
1743 platform_driver_unregister(&driver->platform_driver);
1744 used = false;
1745}
1746EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
1747
1748static int __init asus_wmi_init(void)
1749{
1750 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
1751 pr_info("Asus Management GUID not found");
1752 return -ENODEV;
1753 }
1754
1755 pr_info("ASUS WMI generic driver loaded");
Yong Wangee027e42010-03-21 10:26:34 +08001756 return 0;
1757}
1758
Corentin Charye12e6d92011-02-26 10:20:31 +01001759static void __exit asus_wmi_exit(void)
Yong Wangee027e42010-03-21 10:26:34 +08001760{
Corentin Charye12e6d92011-02-26 10:20:31 +01001761 pr_info("ASUS WMI generic driver unloaded");
Yong Wangee027e42010-03-21 10:26:34 +08001762}
1763
Corentin Charye12e6d92011-02-26 10:20:31 +01001764module_init(asus_wmi_init);
1765module_exit(asus_wmi_exit);