blob: e60385df4af22fef9ff9b961c02b77eb252434f1 [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 Chary79ec1172011-07-01 11:34:35 +020098#define ASUS_WMI_DEVID_CWAP 0x00010003
Corentin Charye12e6d92011-02-26 10:20:31 +010099#define ASUS_WMI_DEVID_WLAN 0x00010011
100#define ASUS_WMI_DEVID_BLUETOOTH 0x00010013
Corentin Chary2f686b52011-02-26 10:20:41 +0100101#define ASUS_WMI_DEVID_GPS 0x00010015
Corentin Charye12e6d92011-02-26 10:20:31 +0100102#define ASUS_WMI_DEVID_WIMAX 0x00010017
103#define ASUS_WMI_DEVID_WWAN3G 0x00010019
Corentin Chary2f686b52011-02-26 10:20:41 +0100104#define ASUS_WMI_DEVID_UWB 0x00010021
105
106/* Leds */
107/* 0x000200XX and 0x000400XX */
Corentin Chary79ec1172011-07-01 11:34:35 +0200108#define ASUS_WMI_DEVID_LED1 0x00020011
109#define ASUS_WMI_DEVID_LED2 0x00020012
110#define ASUS_WMI_DEVID_LED3 0x00020013
111#define ASUS_WMI_DEVID_LED4 0x00020014
112#define ASUS_WMI_DEVID_LED5 0x00020015
113#define ASUS_WMI_DEVID_LED6 0x00020016
Corentin Chary43815942011-02-06 13:28:43 +0100114
115/* Backlight and Brightness */
Corentin Charye12e6d92011-02-26 10:20:31 +0100116#define ASUS_WMI_DEVID_BACKLIGHT 0x00050011
117#define ASUS_WMI_DEVID_BRIGHTNESS 0x00050012
Corentin Chary2f686b52011-02-26 10:20:41 +0100118#define ASUS_WMI_DEVID_KBD_BACKLIGHT 0x00050021
119#define ASUS_WMI_DEVID_LIGHT_SENSOR 0x00050022 /* ?? */
Corentin Chary43815942011-02-06 13:28:43 +0100120
121/* Misc */
Corentin Charye12e6d92011-02-26 10:20:31 +0100122#define ASUS_WMI_DEVID_CAMERA 0x00060013
Corentin Chary43815942011-02-06 13:28:43 +0100123
124/* Storage */
Corentin Charye12e6d92011-02-26 10:20:31 +0100125#define ASUS_WMI_DEVID_CARDREADER 0x00080013
Corentin Chary43815942011-02-06 13:28:43 +0100126
127/* Input */
Corentin Chary57ab7da2011-02-26 10:20:32 +0100128#define ASUS_WMI_DEVID_TOUCHPAD 0x00100011
Corentin Charye12e6d92011-02-26 10:20:31 +0100129#define ASUS_WMI_DEVID_TOUCHPAD_LED 0x00100012
Yong Wang3d7b1652010-04-11 09:27:54 +0800130
Corentin Chary2f686b52011-02-26 10:20:41 +0100131/* Fan, Thermal */
132#define ASUS_WMI_DEVID_THERMAL_CTRL 0x00110011
133#define ASUS_WMI_DEVID_FAN_CTRL 0x00110012
134
135/* Power */
136#define ASUS_WMI_DEVID_PROCESSOR_STATE 0x00120012
137
Corentin Chary43815942011-02-06 13:28:43 +0100138/* DSTS masks */
Corentin Charye12e6d92011-02-26 10:20:31 +0100139#define ASUS_WMI_DSTS_STATUS_BIT 0x00000001
Corentin Charya75fe0d2011-02-26 10:20:34 +0100140#define ASUS_WMI_DSTS_UNKNOWN_BIT 0x00000002
Corentin Charye12e6d92011-02-26 10:20:31 +0100141#define ASUS_WMI_DSTS_PRESENCE_BIT 0x00010000
Corentin Chary2f686b52011-02-26 10:20:41 +0100142#define ASUS_WMI_DSTS_USER_BIT 0x00020000
143#define ASUS_WMI_DSTS_BIOS_BIT 0x00040000
Corentin Charye12e6d92011-02-26 10:20:31 +0100144#define ASUS_WMI_DSTS_BRIGHTNESS_MASK 0x000000FF
145#define ASUS_WMI_DSTS_MAX_BRIGTH_MASK 0x0000FF00
Yong Wangee027e42010-03-21 10:26:34 +0800146
Yong Wang3d7b1652010-04-11 09:27:54 +0800147struct bios_args {
Corentin Charyd33da3b2011-02-26 10:20:35 +0100148 u32 arg0;
149 u32 arg1;
150} __packed;
Yong Wang3d7b1652010-04-11 09:27:54 +0800151
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100152/*
Corentin Charye12e6d92011-02-26 10:20:31 +0100153 * <platform>/ - debugfs root directory
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100154 * dev_id - current dev_id
155 * ctrl_param - current ctrl_param
Corentin Charyef343492011-02-26 10:20:39 +0100156 * method_id - current method_id
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100157 * devs - call DEVS(dev_id, ctrl_param) and print result
158 * dsts - call DSTS(dev_id) and print result
Corentin Charyef343492011-02-26 10:20:39 +0100159 * call - call method_id(dev_id, ctrl_param) and print result
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100160 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100161struct asus_wmi_debug {
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100162 struct dentry *root;
Corentin Charyef343492011-02-26 10:20:39 +0100163 u32 method_id;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100164 u32 dev_id;
165 u32 ctrl_param;
166};
167
Corentin Charya7ce3f02011-02-26 10:20:33 +0100168struct asus_rfkill {
169 struct asus_wmi *asus;
170 struct rfkill *rfkill;
171 u32 dev_id;
172};
173
Corentin Charye12e6d92011-02-26 10:20:31 +0100174struct asus_wmi {
Corentin Chary1d070f82011-02-26 10:20:36 +0100175 int dsts_id;
Corentin Chary46dbca82011-02-26 10:20:38 +0100176 int spec;
177 int sfun;
Corentin Chary1d070f82011-02-26 10:20:36 +0100178
Yong Wang81248882010-04-11 09:26:33 +0800179 struct input_dev *inputdev;
Yong Wang3d7b1652010-04-11 09:27:54 +0800180 struct backlight_device *backlight_device;
Corentin Charye07babd2011-02-26 10:20:42 +0100181 struct device *hwmon_device;
Corentin Chary27c136c2010-11-29 08:14:05 +0100182 struct platform_device *platform_device;
Corentin Chary084fca62010-11-29 08:14:06 +0100183
184 struct led_classdev tpd_led;
185 int tpd_led_wk;
Corentin Charye9809c02011-07-01 11:34:31 +0200186 struct led_classdev kbd_led;
187 int kbd_led_wk;
Corentin Chary084fca62010-11-29 08:14:06 +0100188 struct workqueue_struct *led_workqueue;
189 struct work_struct tpd_led_work;
Corentin Charye9809c02011-07-01 11:34:31 +0200190 struct work_struct kbd_led_work;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100191
Corentin Charya7ce3f02011-02-26 10:20:33 +0100192 struct asus_rfkill wlan;
193 struct asus_rfkill bluetooth;
194 struct asus_rfkill wimax;
195 struct asus_rfkill wwan3g;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100196
Corentin Charyafa7c882011-02-06 13:28:28 +0100197 struct hotplug_slot *hotplug_slot;
198 struct mutex hotplug_lock;
Corentin Chary279f8f92011-02-06 13:28:29 +0100199 struct mutex wmi_lock;
200 struct workqueue_struct *hotplug_workqueue;
201 struct work_struct hotplug_work;
Corentin Charyafa7c882011-02-06 13:28:28 +0100202
Corentin Charye12e6d92011-02-26 10:20:31 +0100203 struct asus_wmi_debug debug;
204
205 struct asus_wmi_driver *driver;
Yong Wang81248882010-04-11 09:26:33 +0800206};
207
Corentin Charye12e6d92011-02-26 10:20:31 +0100208static int asus_wmi_input_init(struct asus_wmi *asus)
Yong Wangee027e42010-03-21 10:26:34 +0800209{
210 int err;
211
Corentin Charye12e6d92011-02-26 10:20:31 +0100212 asus->inputdev = input_allocate_device();
213 if (!asus->inputdev)
Yong Wangee027e42010-03-21 10:26:34 +0800214 return -ENOMEM;
215
Corentin Chary58a9f392011-03-30 16:32:32 +0200216 asus->inputdev->name = asus->driver->input_name;
217 asus->inputdev->phys = asus->driver->input_phys;
Corentin Charye12e6d92011-02-26 10:20:31 +0100218 asus->inputdev->id.bustype = BUS_HOST;
219 asus->inputdev->dev.parent = &asus->platform_device->dev;
Seth Forshee39bbde02011-07-04 09:49:20 +0200220 set_bit(EV_REP, asus->inputdev->evbit);
Yong Wangee027e42010-03-21 10:26:34 +0800221
Corentin Charye12e6d92011-02-26 10:20:31 +0100222 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
Yong Wangee027e42010-03-21 10:26:34 +0800223 if (err)
224 goto err_free_dev;
225
Corentin Charye12e6d92011-02-26 10:20:31 +0100226 err = input_register_device(asus->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800227 if (err)
228 goto err_free_keymap;
229
230 return 0;
231
232err_free_keymap:
Corentin Charye12e6d92011-02-26 10:20:31 +0100233 sparse_keymap_free(asus->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800234err_free_dev:
Corentin Charye12e6d92011-02-26 10:20:31 +0100235 input_free_device(asus->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800236 return err;
237}
238
Corentin Charye12e6d92011-02-26 10:20:31 +0100239static void asus_wmi_input_exit(struct asus_wmi *asus)
Yong Wang81248882010-04-11 09:26:33 +0800240{
Corentin Charye12e6d92011-02-26 10:20:31 +0100241 if (asus->inputdev) {
242 sparse_keymap_free(asus->inputdev);
243 input_unregister_device(asus->inputdev);
Yong Wang81248882010-04-11 09:26:33 +0800244 }
245
Corentin Charye12e6d92011-02-26 10:20:31 +0100246 asus->inputdev = NULL;
Yong Wang81248882010-04-11 09:26:33 +0800247}
248
Corentin Charyd33da3b2011-02-26 10:20:35 +0100249static int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1,
250 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800251{
Corentin Charyd33da3b2011-02-26 10:20:35 +0100252 struct bios_args args = {
253 .arg0 = arg0,
254 .arg1 = arg1,
255 };
256 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
Yong Wang3d7b1652010-04-11 09:27:54 +0800257 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Yong Wang3d7b1652010-04-11 09:27:54 +0800258 acpi_status status;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100259 union acpi_object *obj;
Yong Wang3d7b1652010-04-11 09:27:54 +0800260 u32 tmp;
261
Corentin Charyd33da3b2011-02-26 10:20:35 +0100262 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 1, method_id,
Corentin Charyafa7c882011-02-06 13:28:28 +0100263 &input, &output);
Yong Wang3d7b1652010-04-11 09:27:54 +0800264
265 if (ACPI_FAILURE(status))
Corentin Charyd33da3b2011-02-26 10:20:35 +0100266 goto exit;
Yong Wang3d7b1652010-04-11 09:27:54 +0800267
268 obj = (union acpi_object *)output.pointer;
269 if (obj && obj->type == ACPI_TYPE_INTEGER)
Corentin Charye12e6d92011-02-26 10:20:31 +0100270 tmp = (u32) obj->integer.value;
Yong Wang3d7b1652010-04-11 09:27:54 +0800271 else
272 tmp = 0;
273
Corentin Chary2a3f0062010-11-29 08:14:10 +0100274 if (retval)
275 *retval = tmp;
Yong Wang3d7b1652010-04-11 09:27:54 +0800276
277 kfree(obj);
278
Corentin Charyd33da3b2011-02-26 10:20:35 +0100279exit:
280 if (ACPI_FAILURE(status))
281 return -EIO;
Yong Wang3d7b1652010-04-11 09:27:54 +0800282
Corentin Charyd33da3b2011-02-26 10:20:35 +0100283 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
284 return -ENODEV;
285
286 return 0;
Yong Wang3d7b1652010-04-11 09:27:54 +0800287}
288
Corentin Chary1d070f82011-02-26 10:20:36 +0100289static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
Corentin Charyd33da3b2011-02-26 10:20:35 +0100290{
Corentin Chary1d070f82011-02-26 10:20:36 +0100291 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
Corentin Charyd33da3b2011-02-26 10:20:35 +0100292}
293
294static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
Corentin Chary1d070f82011-02-26 10:20:36 +0100295 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800296{
Corentin Charyd33da3b2011-02-26 10:20:35 +0100297 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
298 ctrl_param, retval);
Yong Wang3d7b1652010-04-11 09:27:54 +0800299}
300
Corentin Chary5c956382011-02-06 13:28:31 +0100301/* Helper for special devices with magic return codes */
Corentin Chary1d070f82011-02-26 10:20:36 +0100302static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
303 u32 dev_id, u32 mask)
Corentin Chary5c956382011-02-06 13:28:31 +0100304{
305 u32 retval = 0;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100306 int err;
Corentin Chary5c956382011-02-06 13:28:31 +0100307
Corentin Chary1d070f82011-02-26 10:20:36 +0100308 err = asus_wmi_get_devstate(asus, dev_id, &retval);
Corentin Chary5c956382011-02-06 13:28:31 +0100309
Corentin Charyd33da3b2011-02-26 10:20:35 +0100310 if (err < 0)
311 return err;
Corentin Chary5c956382011-02-06 13:28:31 +0100312
Corentin Charye12e6d92011-02-26 10:20:31 +0100313 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
Corentin Chary5c956382011-02-06 13:28:31 +0100314 return -ENODEV;
315
Corentin Charya75fe0d2011-02-26 10:20:34 +0100316 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
317 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
318 return -ENODEV;
319 }
320
Corentin Charyb7187262011-02-06 13:28:39 +0100321 return retval & mask;
322}
323
Corentin Chary1d070f82011-02-26 10:20:36 +0100324static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
Corentin Charyb7187262011-02-06 13:28:39 +0100325{
Corentin Chary1d070f82011-02-26 10:20:36 +0100326 return asus_wmi_get_devstate_bits(asus, dev_id,
327 ASUS_WMI_DSTS_STATUS_BIT);
Corentin Chary5c956382011-02-06 13:28:31 +0100328}
329
Corentin Chary084fca62010-11-29 08:14:06 +0100330/*
331 * LEDs
332 */
333/*
334 * These functions actually update the LED's, and are called from a
335 * workqueue. By doing this as separate work rather than when the LED
Corentin Charye12e6d92011-02-26 10:20:31 +0100336 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
Corentin Chary084fca62010-11-29 08:14:06 +0100337 * potentially bad time, such as a timer interrupt.
338 */
339static void tpd_led_update(struct work_struct *work)
340{
341 int ctrl_param;
Corentin Charye12e6d92011-02-26 10:20:31 +0100342 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100343
Corentin Charye12e6d92011-02-26 10:20:31 +0100344 asus = container_of(work, struct asus_wmi, tpd_led_work);
Corentin Chary084fca62010-11-29 08:14:06 +0100345
Corentin Charye12e6d92011-02-26 10:20:31 +0100346 ctrl_param = asus->tpd_led_wk;
347 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
Corentin Chary084fca62010-11-29 08:14:06 +0100348}
349
350static void tpd_led_set(struct led_classdev *led_cdev,
351 enum led_brightness value)
352{
Corentin Charye12e6d92011-02-26 10:20:31 +0100353 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100354
Corentin Charye12e6d92011-02-26 10:20:31 +0100355 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
Corentin Chary084fca62010-11-29 08:14:06 +0100356
Corentin Charye12e6d92011-02-26 10:20:31 +0100357 asus->tpd_led_wk = !!value;
358 queue_work(asus->led_workqueue, &asus->tpd_led_work);
Corentin Chary084fca62010-11-29 08:14:06 +0100359}
360
Corentin Charye12e6d92011-02-26 10:20:31 +0100361static int read_tpd_led_state(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100362{
Corentin Chary1d070f82011-02-26 10:20:36 +0100363 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
Corentin Chary084fca62010-11-29 08:14:06 +0100364}
365
366static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
367{
Corentin Charye12e6d92011-02-26 10:20:31 +0100368 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100369
Corentin Charye12e6d92011-02-26 10:20:31 +0100370 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
Corentin Chary084fca62010-11-29 08:14:06 +0100371
Corentin Charye12e6d92011-02-26 10:20:31 +0100372 return read_tpd_led_state(asus);
Corentin Chary084fca62010-11-29 08:14:06 +0100373}
374
Corentin Charye9809c02011-07-01 11:34:31 +0200375static void kbd_led_update(struct work_struct *work)
Corentin Chary084fca62010-11-29 08:14:06 +0100376{
Corentin Charye9809c02011-07-01 11:34:31 +0200377 int ctrl_param = 0;
378 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100379
Corentin Charye9809c02011-07-01 11:34:31 +0200380 asus = container_of(work, struct asus_wmi, kbd_led_work);
Corentin Chary084fca62010-11-29 08:14:06 +0100381
Corentin Charye9809c02011-07-01 11:34:31 +0200382 /*
383 * bits 0-2: level
384 * bit 7: light on/off
385 */
386 if (asus->kbd_led_wk > 0)
387 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
Corentin Chary084fca62010-11-29 08:14:06 +0100388
Corentin Charye9809c02011-07-01 11:34:31 +0200389 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
390}
Corentin Chary084fca62010-11-29 08:14:06 +0100391
Corentin Charye9809c02011-07-01 11:34:31 +0200392static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
393{
394 int retval;
395
396 /*
397 * bits 0-2: level
398 * bit 7: light on/off
399 * bit 8-10: environment (0: dark, 1: normal, 2: light)
400 * bit 17: status unknown
401 */
402 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
403 0xFFFF);
404
Corentin Charyaf965e92011-07-01 11:34:34 +0200405 /* Unknown status is considered as off */
Corentin Charye9809c02011-07-01 11:34:31 +0200406 if (retval == 0x8000)
Corentin Charyaf965e92011-07-01 11:34:34 +0200407 retval = 0;
Corentin Charye9809c02011-07-01 11:34:31 +0200408
409 if (retval >= 0) {
410 if (level)
411 *level = retval & 0x80 ? retval & 0x7F : 0;
412 if (env)
413 *env = (retval >> 8) & 0x7F;
414 retval = 0;
Corentin Chary084fca62010-11-29 08:14:06 +0100415 }
416
Corentin Charye9809c02011-07-01 11:34:31 +0200417 return retval;
418}
419
420static void kbd_led_set(struct led_classdev *led_cdev,
421 enum led_brightness value)
422{
423 struct asus_wmi *asus;
424
425 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
426
427 if (value > asus->kbd_led.max_brightness)
428 value = asus->kbd_led.max_brightness;
429 else if (value < 0)
430 value = 0;
431
432 asus->kbd_led_wk = value;
433 queue_work(asus->led_workqueue, &asus->kbd_led_work);
434}
435
436static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
437{
438 struct asus_wmi *asus;
439 int retval, value;
440
441 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
442
443 retval = kbd_led_read(asus, &value, NULL);
444
445 if (retval < 0)
446 return retval;
447
448 return value;
Corentin Chary084fca62010-11-29 08:14:06 +0100449}
450
Corentin Charye12e6d92011-02-26 10:20:31 +0100451static void asus_wmi_led_exit(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100452{
Corentin Charye12e6d92011-02-26 10:20:31 +0100453 if (asus->tpd_led.dev)
454 led_classdev_unregister(&asus->tpd_led);
455 if (asus->led_workqueue)
456 destroy_workqueue(asus->led_workqueue);
Corentin Chary084fca62010-11-29 08:14:06 +0100457}
458
Corentin Charye9809c02011-07-01 11:34:31 +0200459static int asus_wmi_led_init(struct asus_wmi *asus)
460{
461 int rv = 0;
462
463 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
464 if (!asus->led_workqueue)
465 return -ENOMEM;
466
467 if (read_tpd_led_state(asus) >= 0) {
468 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
469
470 asus->tpd_led.name = "asus::touchpad";
471 asus->tpd_led.brightness_set = tpd_led_set;
472 asus->tpd_led.brightness_get = tpd_led_get;
473 asus->tpd_led.max_brightness = 1;
474
475 rv = led_classdev_register(&asus->platform_device->dev,
476 &asus->tpd_led);
477 if (rv)
478 goto error;
479 }
480
481 if (kbd_led_read(asus, NULL, NULL) >= 0) {
482 INIT_WORK(&asus->kbd_led_work, kbd_led_update);
483
484 asus->kbd_led.name = "asus::kbd_backlight";
485 asus->kbd_led.brightness_set = kbd_led_set;
486 asus->kbd_led.brightness_get = kbd_led_get;
487 asus->kbd_led.max_brightness = 3;
488
489 rv = led_classdev_register(&asus->platform_device->dev,
490 &asus->kbd_led);
491 }
492
493error:
494 if (rv)
495 asus_wmi_led_exit(asus);
496
497 return rv;
498}
499
500
Corentin Chary084fca62010-11-29 08:14:06 +0100501/*
Corentin Charyafa7c882011-02-06 13:28:28 +0100502 * PCI hotplug (for wlan rfkill)
503 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100504static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100505{
Corentin Chary1d070f82011-02-26 10:20:36 +0100506 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100507
Corentin Chary5c956382011-02-06 13:28:31 +0100508 if (result < 0)
Corentin Charyafa7c882011-02-06 13:28:28 +0100509 return false;
Corentin Chary5c956382011-02-06 13:28:31 +0100510 return !result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100511}
512
Corentin Charye12e6d92011-02-26 10:20:31 +0100513static void asus_rfkill_hotplug(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100514{
515 struct pci_dev *dev;
516 struct pci_bus *bus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100517 bool blocked;
Corentin Charyafa7c882011-02-06 13:28:28 +0100518 bool absent;
519 u32 l;
520
Corentin Charye12e6d92011-02-26 10:20:31 +0100521 mutex_lock(&asus->wmi_lock);
522 blocked = asus_wlan_rfkill_blocked(asus);
523 mutex_unlock(&asus->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100524
Corentin Charye12e6d92011-02-26 10:20:31 +0100525 mutex_lock(&asus->hotplug_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100526
Corentin Charya7ce3f02011-02-26 10:20:33 +0100527 if (asus->wlan.rfkill)
528 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
Corentin Chary279f8f92011-02-06 13:28:29 +0100529
Corentin Charye12e6d92011-02-26 10:20:31 +0100530 if (asus->hotplug_slot) {
Corentin Charyafa7c882011-02-06 13:28:28 +0100531 bus = pci_find_bus(0, 1);
532 if (!bus) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700533 pr_warn("Unable to find PCI bus 1?\n");
Corentin Charyafa7c882011-02-06 13:28:28 +0100534 goto out_unlock;
535 }
536
537 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
538 pr_err("Unable to read PCI config space?\n");
539 goto out_unlock;
540 }
541 absent = (l == 0xffffffff);
542
543 if (blocked != absent) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700544 pr_warn("BIOS says wireless lan is %s, "
545 "but the pci device is %s\n",
546 blocked ? "blocked" : "unblocked",
547 absent ? "absent" : "present");
548 pr_warn("skipped wireless hotplug as probably "
549 "inappropriate for this model\n");
Corentin Charyafa7c882011-02-06 13:28:28 +0100550 goto out_unlock;
551 }
552
553 if (!blocked) {
554 dev = pci_get_slot(bus, 0);
555 if (dev) {
556 /* Device already present */
557 pci_dev_put(dev);
558 goto out_unlock;
559 }
560 dev = pci_scan_single_device(bus, 0);
561 if (dev) {
562 pci_bus_assign_resources(bus);
563 if (pci_bus_add_device(dev))
564 pr_err("Unable to hotplug wifi\n");
565 }
566 } else {
567 dev = pci_get_slot(bus, 0);
568 if (dev) {
569 pci_remove_bus_device(dev);
570 pci_dev_put(dev);
571 }
572 }
573 }
574
575out_unlock:
Corentin Charye12e6d92011-02-26 10:20:31 +0100576 mutex_unlock(&asus->hotplug_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100577}
578
Corentin Charye12e6d92011-02-26 10:20:31 +0100579static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
Corentin Charyafa7c882011-02-06 13:28:28 +0100580{
Corentin Charye12e6d92011-02-26 10:20:31 +0100581 struct asus_wmi *asus = data;
Corentin Charyafa7c882011-02-06 13:28:28 +0100582
583 if (event != ACPI_NOTIFY_BUS_CHECK)
584 return;
585
Corentin Chary279f8f92011-02-06 13:28:29 +0100586 /*
Corentin Charye12e6d92011-02-26 10:20:31 +0100587 * We can't call directly asus_rfkill_hotplug because most
Corentin Chary279f8f92011-02-06 13:28:29 +0100588 * of the time WMBC is still being executed and not reetrant.
589 * There is currently no way to tell ACPICA that we want this
Corentin Charye12e6d92011-02-26 10:20:31 +0100590 * method to be serialized, we schedule a asus_rfkill_hotplug
Corentin Chary279f8f92011-02-06 13:28:29 +0100591 * call later, in a safer context.
592 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100593 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
Corentin Charyafa7c882011-02-06 13:28:28 +0100594}
595
Corentin Charye12e6d92011-02-26 10:20:31 +0100596static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
Corentin Charyafa7c882011-02-06 13:28:28 +0100597{
598 acpi_status status;
599 acpi_handle handle;
600
601 status = acpi_get_handle(NULL, node, &handle);
602
603 if (ACPI_SUCCESS(status)) {
604 status = acpi_install_notify_handler(handle,
605 ACPI_SYSTEM_NOTIFY,
Corentin Charye12e6d92011-02-26 10:20:31 +0100606 asus_rfkill_notify, asus);
Corentin Charyafa7c882011-02-06 13:28:28 +0100607 if (ACPI_FAILURE(status))
Joe Perches5ad77dc2011-03-29 15:21:35 -0700608 pr_warn("Failed to register notify on %s\n", node);
Corentin Charyafa7c882011-02-06 13:28:28 +0100609 } else
610 return -ENODEV;
611
612 return 0;
613}
614
Corentin Charye12e6d92011-02-26 10:20:31 +0100615static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
Corentin Charyafa7c882011-02-06 13:28:28 +0100616{
617 acpi_status status = AE_OK;
618 acpi_handle handle;
619
620 status = acpi_get_handle(NULL, node, &handle);
621
622 if (ACPI_SUCCESS(status)) {
623 status = acpi_remove_notify_handler(handle,
Corentin Charye12e6d92011-02-26 10:20:31 +0100624 ACPI_SYSTEM_NOTIFY,
625 asus_rfkill_notify);
Corentin Charyafa7c882011-02-06 13:28:28 +0100626 if (ACPI_FAILURE(status))
627 pr_err("Error removing rfkill notify handler %s\n",
Corentin Charye12e6d92011-02-26 10:20:31 +0100628 node);
Corentin Charyafa7c882011-02-06 13:28:28 +0100629 }
630}
631
Corentin Charye12e6d92011-02-26 10:20:31 +0100632static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
633 u8 *value)
Corentin Charyafa7c882011-02-06 13:28:28 +0100634{
Corentin Chary1d070f82011-02-26 10:20:36 +0100635 struct asus_wmi *asus = hotplug_slot->private;
636 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100637
Corentin Chary5c956382011-02-06 13:28:31 +0100638 if (result < 0)
639 return result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100640
Corentin Chary5c956382011-02-06 13:28:31 +0100641 *value = !!result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100642 return 0;
643}
644
Corentin Charye12e6d92011-02-26 10:20:31 +0100645static void asus_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
Corentin Charyafa7c882011-02-06 13:28:28 +0100646{
647 kfree(hotplug_slot->info);
648 kfree(hotplug_slot);
649}
650
Corentin Charye12e6d92011-02-26 10:20:31 +0100651static struct hotplug_slot_ops asus_hotplug_slot_ops = {
Corentin Charyafa7c882011-02-06 13:28:28 +0100652 .owner = THIS_MODULE,
Corentin Charye12e6d92011-02-26 10:20:31 +0100653 .get_adapter_status = asus_get_adapter_status,
654 .get_power_status = asus_get_adapter_status,
Corentin Charyafa7c882011-02-06 13:28:28 +0100655};
656
Corentin Charye12e6d92011-02-26 10:20:31 +0100657static void asus_hotplug_work(struct work_struct *work)
Corentin Chary279f8f92011-02-06 13:28:29 +0100658{
Corentin Charye12e6d92011-02-26 10:20:31 +0100659 struct asus_wmi *asus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100660
Corentin Charye12e6d92011-02-26 10:20:31 +0100661 asus = container_of(work, struct asus_wmi, hotplug_work);
662 asus_rfkill_hotplug(asus);
Corentin Chary279f8f92011-02-06 13:28:29 +0100663}
664
Corentin Charye12e6d92011-02-26 10:20:31 +0100665static int asus_setup_pci_hotplug(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100666{
667 int ret = -ENOMEM;
668 struct pci_bus *bus = pci_find_bus(0, 1);
669
670 if (!bus) {
671 pr_err("Unable to find wifi PCI bus\n");
672 return -ENODEV;
673 }
674
Corentin Charye12e6d92011-02-26 10:20:31 +0100675 asus->hotplug_workqueue =
676 create_singlethread_workqueue("hotplug_workqueue");
677 if (!asus->hotplug_workqueue)
Corentin Chary279f8f92011-02-06 13:28:29 +0100678 goto error_workqueue;
679
Corentin Charye12e6d92011-02-26 10:20:31 +0100680 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
Corentin Chary279f8f92011-02-06 13:28:29 +0100681
Corentin Charye12e6d92011-02-26 10:20:31 +0100682 asus->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
683 if (!asus->hotplug_slot)
Corentin Charyafa7c882011-02-06 13:28:28 +0100684 goto error_slot;
685
Corentin Charye12e6d92011-02-26 10:20:31 +0100686 asus->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
687 GFP_KERNEL);
688 if (!asus->hotplug_slot->info)
Corentin Charyafa7c882011-02-06 13:28:28 +0100689 goto error_info;
690
Corentin Charye12e6d92011-02-26 10:20:31 +0100691 asus->hotplug_slot->private = asus;
692 asus->hotplug_slot->release = &asus_cleanup_pci_hotplug;
693 asus->hotplug_slot->ops = &asus_hotplug_slot_ops;
694 asus_get_adapter_status(asus->hotplug_slot,
695 &asus->hotplug_slot->info->adapter_status);
Corentin Charyafa7c882011-02-06 13:28:28 +0100696
Corentin Charye12e6d92011-02-26 10:20:31 +0100697 ret = pci_hp_register(asus->hotplug_slot, bus, 0, "asus-wifi");
Corentin Charyafa7c882011-02-06 13:28:28 +0100698 if (ret) {
699 pr_err("Unable to register hotplug slot - %d\n", ret);
700 goto error_register;
701 }
702
703 return 0;
704
705error_register:
Corentin Charye12e6d92011-02-26 10:20:31 +0100706 kfree(asus->hotplug_slot->info);
Corentin Charyafa7c882011-02-06 13:28:28 +0100707error_info:
Corentin Charye12e6d92011-02-26 10:20:31 +0100708 kfree(asus->hotplug_slot);
709 asus->hotplug_slot = NULL;
Corentin Charyafa7c882011-02-06 13:28:28 +0100710error_slot:
Corentin Charye12e6d92011-02-26 10:20:31 +0100711 destroy_workqueue(asus->hotplug_workqueue);
Corentin Chary279f8f92011-02-06 13:28:29 +0100712error_workqueue:
Corentin Charyafa7c882011-02-06 13:28:28 +0100713 return ret;
714}
715
716/*
Corentin Charyba48fdb2010-11-29 08:14:07 +0100717 * Rfkill devices
718 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100719static int asus_rfkill_set(void *data, bool blocked)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100720{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100721 struct asus_rfkill *priv = data;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100722 u32 ctrl_param = !blocked;
723
Corentin Charyd33da3b2011-02-26 10:20:35 +0100724 return asus_wmi_set_devstate(priv->dev_id, ctrl_param, NULL);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100725}
726
Corentin Charye12e6d92011-02-26 10:20:31 +0100727static void asus_rfkill_query(struct rfkill *rfkill, void *data)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100728{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100729 struct asus_rfkill *priv = data;
Corentin Chary5c956382011-02-06 13:28:31 +0100730 int result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100731
Corentin Chary1d070f82011-02-26 10:20:36 +0100732 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100733
Corentin Chary5c956382011-02-06 13:28:31 +0100734 if (result < 0)
Corentin Charye12e6d92011-02-26 10:20:31 +0100735 return;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100736
Corentin Charya7ce3f02011-02-26 10:20:33 +0100737 rfkill_set_sw_state(priv->rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100738}
739
Corentin Charye12e6d92011-02-26 10:20:31 +0100740static int asus_rfkill_wlan_set(void *data, bool blocked)
Corentin Chary279f8f92011-02-06 13:28:29 +0100741{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100742 struct asus_rfkill *priv = data;
743 struct asus_wmi *asus = priv->asus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100744 int ret;
745
746 /*
747 * This handler is enabled only if hotplug is enabled.
Corentin Charye12e6d92011-02-26 10:20:31 +0100748 * In this case, the asus_wmi_set_devstate() will
Corentin Chary279f8f92011-02-06 13:28:29 +0100749 * trigger a wmi notification and we need to wait
750 * this call to finish before being able to call
751 * any wmi method
752 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100753 mutex_lock(&asus->wmi_lock);
Corentin Charya7ce3f02011-02-26 10:20:33 +0100754 ret = asus_rfkill_set(data, blocked);
Corentin Charye12e6d92011-02-26 10:20:31 +0100755 mutex_unlock(&asus->wmi_lock);
Corentin Chary279f8f92011-02-06 13:28:29 +0100756 return ret;
757}
758
Corentin Charye12e6d92011-02-26 10:20:31 +0100759static const struct rfkill_ops asus_rfkill_wlan_ops = {
760 .set_block = asus_rfkill_wlan_set,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100761 .query = asus_rfkill_query,
Corentin Chary279f8f92011-02-06 13:28:29 +0100762};
763
Corentin Charye12e6d92011-02-26 10:20:31 +0100764static const struct rfkill_ops asus_rfkill_ops = {
765 .set_block = asus_rfkill_set,
766 .query = asus_rfkill_query,
Corentin Charyba48fdb2010-11-29 08:14:07 +0100767};
768
Corentin Charye12e6d92011-02-26 10:20:31 +0100769static int asus_new_rfkill(struct asus_wmi *asus,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100770 struct asus_rfkill *arfkill,
Corentin Charye12e6d92011-02-26 10:20:31 +0100771 const char *name, enum rfkill_type type, int dev_id)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100772{
Corentin Chary1d070f82011-02-26 10:20:36 +0100773 int result = asus_wmi_get_devstate_simple(asus, dev_id);
Corentin Charya7ce3f02011-02-26 10:20:33 +0100774 struct rfkill **rfkill = &arfkill->rfkill;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100775
Corentin Chary5c956382011-02-06 13:28:31 +0100776 if (result < 0)
777 return result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100778
Corentin Charya7ce3f02011-02-26 10:20:33 +0100779 arfkill->dev_id = dev_id;
780 arfkill->asus = asus;
781
Corentin Charye12e6d92011-02-26 10:20:31 +0100782 if (dev_id == ASUS_WMI_DEVID_WLAN && asus->driver->hotplug_wireless)
783 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100784 &asus_rfkill_wlan_ops, arfkill);
Corentin Chary279f8f92011-02-06 13:28:29 +0100785 else
Corentin Charye12e6d92011-02-26 10:20:31 +0100786 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100787 &asus_rfkill_ops, arfkill);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100788
789 if (!*rfkill)
790 return -EINVAL;
791
Corentin Chary5c956382011-02-06 13:28:31 +0100792 rfkill_init_sw_state(*rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100793 result = rfkill_register(*rfkill);
794 if (result) {
795 rfkill_destroy(*rfkill);
796 *rfkill = NULL;
797 return result;
798 }
799 return 0;
800}
801
Corentin Charye12e6d92011-02-26 10:20:31 +0100802static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100803{
Corentin Charye12e6d92011-02-26 10:20:31 +0100804 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
805 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
806 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
Corentin Charya7ce3f02011-02-26 10:20:33 +0100807 if (asus->wlan.rfkill) {
808 rfkill_unregister(asus->wlan.rfkill);
809 rfkill_destroy(asus->wlan.rfkill);
810 asus->wlan.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100811 }
Corentin Charyafa7c882011-02-06 13:28:28 +0100812 /*
813 * Refresh pci hotplug in case the rfkill state was changed after
Corentin Charye12e6d92011-02-26 10:20:31 +0100814 * asus_unregister_rfkill_notifier()
Corentin Charyafa7c882011-02-06 13:28:28 +0100815 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100816 asus_rfkill_hotplug(asus);
817 if (asus->hotplug_slot)
818 pci_hp_deregister(asus->hotplug_slot);
819 if (asus->hotplug_workqueue)
820 destroy_workqueue(asus->hotplug_workqueue);
Corentin Charyafa7c882011-02-06 13:28:28 +0100821
Corentin Charya7ce3f02011-02-26 10:20:33 +0100822 if (asus->bluetooth.rfkill) {
823 rfkill_unregister(asus->bluetooth.rfkill);
824 rfkill_destroy(asus->bluetooth.rfkill);
825 asus->bluetooth.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100826 }
Corentin Charya7ce3f02011-02-26 10:20:33 +0100827 if (asus->wimax.rfkill) {
828 rfkill_unregister(asus->wimax.rfkill);
829 rfkill_destroy(asus->wimax.rfkill);
830 asus->wimax.rfkill = NULL;
Corentin Chary2e9e1592011-02-06 13:28:37 +0100831 }
Corentin Charya7ce3f02011-02-26 10:20:33 +0100832 if (asus->wwan3g.rfkill) {
833 rfkill_unregister(asus->wwan3g.rfkill);
834 rfkill_destroy(asus->wwan3g.rfkill);
835 asus->wwan3g.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100836 }
837}
838
Corentin Charye12e6d92011-02-26 10:20:31 +0100839static int asus_wmi_rfkill_init(struct asus_wmi *asus)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100840{
841 int result = 0;
842
Corentin Charye12e6d92011-02-26 10:20:31 +0100843 mutex_init(&asus->hotplug_lock);
844 mutex_init(&asus->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100845
Corentin Charya7ce3f02011-02-26 10:20:33 +0100846 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
847 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100848
849 if (result && result != -ENODEV)
850 goto exit;
851
Corentin Charya7ce3f02011-02-26 10:20:33 +0100852 result = asus_new_rfkill(asus, &asus->bluetooth,
Corentin Charye12e6d92011-02-26 10:20:31 +0100853 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
854 ASUS_WMI_DEVID_BLUETOOTH);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100855
856 if (result && result != -ENODEV)
857 goto exit;
858
Corentin Charya7ce3f02011-02-26 10:20:33 +0100859 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
860 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
Corentin Chary2e9e1592011-02-06 13:28:37 +0100861
862 if (result && result != -ENODEV)
863 goto exit;
864
Corentin Charya7ce3f02011-02-26 10:20:33 +0100865 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
866 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100867
868 if (result && result != -ENODEV)
869 goto exit;
870
Corentin Charye12e6d92011-02-26 10:20:31 +0100871 if (!asus->driver->hotplug_wireless)
Corentin Charyc14d4b82011-02-06 13:28:40 +0100872 goto exit;
873
Corentin Charye12e6d92011-02-26 10:20:31 +0100874 result = asus_setup_pci_hotplug(asus);
Corentin Charyafa7c882011-02-06 13:28:28 +0100875 /*
876 * If we get -EBUSY then something else is handling the PCI hotplug -
877 * don't fail in this case
878 */
879 if (result == -EBUSY)
880 result = 0;
881
Corentin Charye12e6d92011-02-26 10:20:31 +0100882 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
883 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
884 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
Corentin Charyafa7c882011-02-06 13:28:28 +0100885 /*
886 * Refresh pci hotplug in case the rfkill state was changed during
887 * setup.
888 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100889 asus_rfkill_hotplug(asus);
Corentin Charyafa7c882011-02-06 13:28:28 +0100890
Corentin Charyba48fdb2010-11-29 08:14:07 +0100891exit:
892 if (result && result != -ENODEV)
Corentin Charye12e6d92011-02-26 10:20:31 +0100893 asus_wmi_rfkill_exit(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100894
895 if (result == -ENODEV)
896 result = 0;
897
898 return result;
899}
900
901/*
Corentin Charye07babd2011-02-26 10:20:42 +0100902 * Hwmon device
903 */
904static ssize_t asus_hwmon_pwm1(struct device *dev,
Corentin Chary49979d02011-07-01 11:34:26 +0200905 struct device_attribute *attr,
906 char *buf)
Corentin Charye07babd2011-02-26 10:20:42 +0100907{
908 struct asus_wmi *asus = dev_get_drvdata(dev);
909 u32 value;
910 int err;
911
912 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
913
914 if (err < 0)
915 return err;
916
Corentin Chary49979d02011-07-01 11:34:26 +0200917 value &= 0xFF;
Corentin Charye07babd2011-02-26 10:20:42 +0100918
919 if (value == 1) /* Low Speed */
920 value = 85;
921 else if (value == 2)
922 value = 170;
923 else if (value == 3)
924 value = 255;
925 else if (value != 0) {
926 pr_err("Unknown fan speed %#x", value);
927 value = -1;
928 }
929
930 return sprintf(buf, "%d\n", value);
931}
932
933static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO, asus_hwmon_pwm1, NULL, 0);
934
935static ssize_t
936show_name(struct device *dev, struct device_attribute *attr, char *buf)
937{
938 return sprintf(buf, "asus\n");
939}
940static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
941
942static struct attribute *hwmon_attributes[] = {
943 &sensor_dev_attr_pwm1.dev_attr.attr,
944 &sensor_dev_attr_name.dev_attr.attr,
945 NULL
946};
947
948static mode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
949 struct attribute *attr, int idx)
950{
951 struct device *dev = container_of(kobj, struct device, kobj);
952 struct platform_device *pdev = to_platform_device(dev->parent);
953 struct asus_wmi *asus = platform_get_drvdata(pdev);
954 bool ok = true;
955 int dev_id = -1;
956 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
957
958 if (attr == &sensor_dev_attr_pwm1.dev_attr.attr)
959 dev_id = ASUS_WMI_DEVID_FAN_CTRL;
960
961 if (dev_id != -1) {
962 int err = asus_wmi_get_devstate(asus, dev_id, &value);
963
964 if (err < 0)
Al Viroe772aed2011-07-23 20:59:40 -0400965 return 0; /* can't return negative here */
Corentin Charye07babd2011-02-26 10:20:42 +0100966 }
967
968 if (dev_id == ASUS_WMI_DEVID_FAN_CTRL) {
969 /*
970 * We need to find a better way, probably using sfun,
971 * bits or spec ...
972 * Currently we disable it if:
973 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
974 * - reverved bits are non-zero
975 * - sfun and presence bit are not set
976 */
Corentin Chary49979d02011-07-01 11:34:26 +0200977 if (value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
Corentin Charye07babd2011-02-26 10:20:42 +0100978 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)))
979 ok = false;
980 }
981
982 return ok ? attr->mode : 0;
983}
984
985static struct attribute_group hwmon_attribute_group = {
986 .is_visible = asus_hwmon_sysfs_is_visible,
987 .attrs = hwmon_attributes
988};
989
990static void asus_wmi_hwmon_exit(struct asus_wmi *asus)
991{
992 struct device *hwmon;
993
994 hwmon = asus->hwmon_device;
995 if (!hwmon)
996 return;
997 sysfs_remove_group(&hwmon->kobj, &hwmon_attribute_group);
998 hwmon_device_unregister(hwmon);
999 asus->hwmon_device = NULL;
1000}
1001
1002static int asus_wmi_hwmon_init(struct asus_wmi *asus)
1003{
1004 struct device *hwmon;
1005 int result;
1006
1007 hwmon = hwmon_device_register(&asus->platform_device->dev);
1008 if (IS_ERR(hwmon)) {
1009 pr_err("Could not register asus hwmon device\n");
1010 return PTR_ERR(hwmon);
1011 }
Corentin Chary49979d02011-07-01 11:34:26 +02001012 dev_set_drvdata(hwmon, asus);
Corentin Charye07babd2011-02-26 10:20:42 +01001013 asus->hwmon_device = hwmon;
1014 result = sysfs_create_group(&hwmon->kobj, &hwmon_attribute_group);
1015 if (result)
1016 asus_wmi_hwmon_exit(asus);
1017 return result;
1018}
1019
1020/*
Corentin Chary084fca62010-11-29 08:14:06 +01001021 * Backlight
1022 */
Corentin Chary1d070f82011-02-26 10:20:36 +01001023static int read_backlight_power(struct asus_wmi *asus)
Corentin Charyb7187262011-02-06 13:28:39 +01001024{
Corentin Chary1d070f82011-02-26 10:20:36 +01001025 int ret = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_BACKLIGHT);
Corentin Charyb7187262011-02-06 13:28:39 +01001026
1027 if (ret < 0)
1028 return ret;
1029
1030 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1031}
1032
Corentin Chary8fbea012011-02-26 10:20:37 +01001033static int read_brightness_max(struct asus_wmi *asus)
1034{
1035 u32 retval;
1036 int err;
1037
1038 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1039
1040 if (err < 0)
1041 return err;
1042
1043 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
1044 retval >>= 8;
1045
1046 if (!retval)
1047 return -ENODEV;
1048
1049 return retval;
1050}
1051
Yong Wang3d7b1652010-04-11 09:27:54 +08001052static int read_brightness(struct backlight_device *bd)
1053{
Corentin Chary1d070f82011-02-26 10:20:36 +01001054 struct asus_wmi *asus = bl_get_data(bd);
Dan Carpenter0986f252011-03-15 10:06:23 +03001055 u32 retval;
1056 int err;
Yong Wang3d7b1652010-04-11 09:27:54 +08001057
Corentin Chary1d070f82011-02-26 10:20:36 +01001058 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
Yong Wang3d7b1652010-04-11 09:27:54 +08001059
Corentin Charyd33da3b2011-02-26 10:20:35 +01001060 if (err < 0)
1061 return err;
1062
1063 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
Yong Wang3d7b1652010-04-11 09:27:54 +08001064}
1065
1066static int update_bl_status(struct backlight_device *bd)
1067{
Corentin Chary1d070f82011-02-26 10:20:36 +01001068 struct asus_wmi *asus = bl_get_data(bd);
Corentin Charydfed65d2010-11-29 08:14:12 +01001069 u32 ctrl_param;
Corentin Charyd33da3b2011-02-26 10:20:35 +01001070 int power, err;
Yong Wang3d7b1652010-04-11 09:27:54 +08001071
1072 ctrl_param = bd->props.brightness;
1073
Corentin Charyd33da3b2011-02-26 10:20:35 +01001074 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
1075 ctrl_param, NULL);
Yong Wang3d7b1652010-04-11 09:27:54 +08001076
Corentin Charyd33da3b2011-02-26 10:20:35 +01001077 if (err < 0)
1078 return err;
Corentin Charyb7187262011-02-06 13:28:39 +01001079
Corentin Chary1d070f82011-02-26 10:20:36 +01001080 power = read_backlight_power(asus);
Corentin Charyb7187262011-02-06 13:28:39 +01001081 if (power != -ENODEV && bd->props.power != power) {
1082 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
Corentin Charyd33da3b2011-02-26 10:20:35 +01001083 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
1084 ctrl_param, NULL);
Corentin Charyb7187262011-02-06 13:28:39 +01001085 }
Corentin Chary8fbea012011-02-26 10:20:37 +01001086 return err;
Yong Wang3d7b1652010-04-11 09:27:54 +08001087}
1088
Corentin Charye12e6d92011-02-26 10:20:31 +01001089static const struct backlight_ops asus_wmi_bl_ops = {
Yong Wang3d7b1652010-04-11 09:27:54 +08001090 .get_brightness = read_brightness,
1091 .update_status = update_bl_status,
1092};
1093
Corentin Charye12e6d92011-02-26 10:20:31 +01001094static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
Yong Wang3d7b1652010-04-11 09:27:54 +08001095{
Corentin Charye12e6d92011-02-26 10:20:31 +01001096 struct backlight_device *bd = asus->backlight_device;
Yong Wang3d7b1652010-04-11 09:27:54 +08001097 int old = bd->props.brightness;
Daniel Mackb7670ed2010-05-19 12:37:01 +02001098 int new = old;
Yong Wang3d7b1652010-04-11 09:27:54 +08001099
1100 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1101 new = code - NOTIFY_BRNUP_MIN + 1;
1102 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
1103 new = code - NOTIFY_BRNDOWN_MIN;
1104
1105 bd->props.brightness = new;
1106 backlight_update_status(bd);
1107 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
1108
1109 return old;
1110}
1111
Corentin Charye12e6d92011-02-26 10:20:31 +01001112static int asus_wmi_backlight_init(struct asus_wmi *asus)
Yong Wang3d7b1652010-04-11 09:27:54 +08001113{
1114 struct backlight_device *bd;
1115 struct backlight_properties props;
Corentin Charyb7187262011-02-06 13:28:39 +01001116 int max;
1117 int power;
1118
Corentin Chary8fbea012011-02-26 10:20:37 +01001119 max = read_brightness_max(asus);
Corentin Charyb7187262011-02-06 13:28:39 +01001120
Corentin Charyb7187262011-02-06 13:28:39 +01001121 if (max == -ENODEV)
1122 max = 0;
Corentin Chary8fbea012011-02-26 10:20:37 +01001123 else if (max < 0)
1124 return max;
1125
1126 power = read_backlight_power(asus);
1127
Corentin Charyb7187262011-02-06 13:28:39 +01001128 if (power == -ENODEV)
1129 power = FB_BLANK_UNBLANK;
Corentin Chary8fbea012011-02-26 10:20:37 +01001130 else if (power < 0)
1131 return power;
Yong Wang3d7b1652010-04-11 09:27:54 +08001132
1133 memset(&props, 0, sizeof(struct backlight_properties));
Axel Lin60cfa092011-06-29 11:43:30 +08001134 props.type = BACKLIGHT_PLATFORM;
Corentin Charyb7187262011-02-06 13:28:39 +01001135 props.max_brightness = max;
Corentin Charye12e6d92011-02-26 10:20:31 +01001136 bd = backlight_device_register(asus->driver->name,
1137 &asus->platform_device->dev, asus,
1138 &asus_wmi_bl_ops, &props);
Yong Wang3d7b1652010-04-11 09:27:54 +08001139 if (IS_ERR(bd)) {
1140 pr_err("Could not register backlight device\n");
1141 return PTR_ERR(bd);
1142 }
1143
Corentin Charye12e6d92011-02-26 10:20:31 +01001144 asus->backlight_device = bd;
Yong Wang3d7b1652010-04-11 09:27:54 +08001145
1146 bd->props.brightness = read_brightness(bd);
Corentin Charyb7187262011-02-06 13:28:39 +01001147 bd->props.power = power;
Yong Wang3d7b1652010-04-11 09:27:54 +08001148 backlight_update_status(bd);
1149
1150 return 0;
1151}
1152
Corentin Charye12e6d92011-02-26 10:20:31 +01001153static void asus_wmi_backlight_exit(struct asus_wmi *asus)
Yong Wang3d7b1652010-04-11 09:27:54 +08001154{
Corentin Charye12e6d92011-02-26 10:20:31 +01001155 if (asus->backlight_device)
1156 backlight_device_unregister(asus->backlight_device);
Yong Wang3d7b1652010-04-11 09:27:54 +08001157
Corentin Charye12e6d92011-02-26 10:20:31 +01001158 asus->backlight_device = NULL;
Yong Wang3d7b1652010-04-11 09:27:54 +08001159}
1160
Corentin Charye12e6d92011-02-26 10:20:31 +01001161static void asus_wmi_notify(u32 value, void *context)
Yong Wang3d7b1652010-04-11 09:27:54 +08001162{
Corentin Charye12e6d92011-02-26 10:20:31 +01001163 struct asus_wmi *asus = context;
Yong Wang3d7b1652010-04-11 09:27:54 +08001164 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
1165 union acpi_object *obj;
1166 acpi_status status;
1167 int code;
1168 int orig_code;
Seth Forsheec4453f62011-07-01 11:34:27 +02001169 unsigned int key_value = 1;
1170 bool autorelease = 1;
Yong Wang3d7b1652010-04-11 09:27:54 +08001171
1172 status = wmi_get_event_data(value, &response);
1173 if (status != AE_OK) {
1174 pr_err("bad event status 0x%x\n", status);
1175 return;
1176 }
1177
1178 obj = (union acpi_object *)response.pointer;
1179
Corentin Chary57ab7da2011-02-26 10:20:32 +01001180 if (!obj || obj->type != ACPI_TYPE_INTEGER)
1181 goto exit;
Yong Wang3d7b1652010-04-11 09:27:54 +08001182
Corentin Chary57ab7da2011-02-26 10:20:32 +01001183 code = obj->integer.value;
1184 orig_code = code;
Yong Wang3d7b1652010-04-11 09:27:54 +08001185
Seth Forsheec4453f62011-07-01 11:34:27 +02001186 if (asus->driver->key_filter) {
1187 asus->driver->key_filter(asus->driver, &code, &key_value,
1188 &autorelease);
1189 if (code == ASUS_WMI_KEY_IGNORE)
1190 goto exit;
1191 }
1192
Corentin Chary57ab7da2011-02-26 10:20:32 +01001193 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1194 code = NOTIFY_BRNUP_MIN;
1195 else if (code >= NOTIFY_BRNDOWN_MIN &&
1196 code <= NOTIFY_BRNDOWN_MAX)
1197 code = NOTIFY_BRNDOWN_MIN;
Yong Wang3d7b1652010-04-11 09:27:54 +08001198
Corentin Chary57ab7da2011-02-26 10:20:32 +01001199 if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
1200 if (!acpi_video_backlight_support())
1201 asus_wmi_backlight_notify(asus, orig_code);
Seth Forsheec4453f62011-07-01 11:34:27 +02001202 } else if (!sparse_keymap_report_event(asus->inputdev, code,
1203 key_value, autorelease))
Corentin Chary57ab7da2011-02-26 10:20:32 +01001204 pr_info("Unknown key %x pressed\n", code);
Yong Wang3d7b1652010-04-11 09:27:54 +08001205
Corentin Chary57ab7da2011-02-26 10:20:32 +01001206exit:
Yong Wang3d7b1652010-04-11 09:27:54 +08001207 kfree(obj);
1208}
1209
Corentin Chary9e1565b2011-02-06 13:28:36 +01001210/*
1211 * Sys helpers
1212 */
1213static int parse_arg(const char *buf, unsigned long count, int *val)
1214{
1215 if (!count)
1216 return 0;
1217 if (sscanf(buf, "%i", val) != 1)
1218 return -EINVAL;
1219 return count;
1220}
1221
Corentin Chary1d070f82011-02-26 10:20:36 +01001222static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
1223 const char *buf, size_t count)
Corentin Chary9e1565b2011-02-06 13:28:36 +01001224{
Corentin Chary9e1565b2011-02-06 13:28:36 +01001225 u32 retval;
Corentin Charyd33da3b2011-02-26 10:20:35 +01001226 int rv, err, value;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001227
Corentin Chary1d070f82011-02-26 10:20:36 +01001228 value = asus_wmi_get_devstate_simple(asus, devid);
Corentin Charye12e6d92011-02-26 10:20:31 +01001229 if (value == -ENODEV) /* Check device presence */
Corentin Chary9e1565b2011-02-06 13:28:36 +01001230 return value;
1231
1232 rv = parse_arg(buf, count, &value);
Corentin Charyd33da3b2011-02-26 10:20:35 +01001233 err = asus_wmi_set_devstate(devid, value, &retval);
Corentin Chary9e1565b2011-02-06 13:28:36 +01001234
Corentin Charyd33da3b2011-02-26 10:20:35 +01001235 if (err < 0)
1236 return err;
1237
Corentin Chary9e1565b2011-02-06 13:28:36 +01001238 return rv;
1239}
1240
Corentin Chary1d070f82011-02-26 10:20:36 +01001241static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
Corentin Chary9e1565b2011-02-06 13:28:36 +01001242{
Corentin Chary1d070f82011-02-26 10:20:36 +01001243 int value = asus_wmi_get_devstate_simple(asus, devid);
Corentin Chary9e1565b2011-02-06 13:28:36 +01001244
1245 if (value < 0)
1246 return value;
1247
1248 return sprintf(buf, "%d\n", value);
1249}
1250
Corentin Charye12e6d92011-02-26 10:20:31 +01001251#define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
Corentin Chary9e1565b2011-02-06 13:28:36 +01001252 static ssize_t show_##_name(struct device *dev, \
1253 struct device_attribute *attr, \
1254 char *buf) \
1255 { \
Corentin Chary1d070f82011-02-26 10:20:36 +01001256 struct asus_wmi *asus = dev_get_drvdata(dev); \
1257 \
1258 return show_sys_wmi(asus, _cm, buf); \
Corentin Chary9e1565b2011-02-06 13:28:36 +01001259 } \
1260 static ssize_t store_##_name(struct device *dev, \
1261 struct device_attribute *attr, \
1262 const char *buf, size_t count) \
1263 { \
Corentin Chary1d070f82011-02-26 10:20:36 +01001264 struct asus_wmi *asus = dev_get_drvdata(dev); \
1265 \
1266 return store_sys_wmi(asus, _cm, buf, count); \
Corentin Chary9e1565b2011-02-06 13:28:36 +01001267 } \
1268 static struct device_attribute dev_attr_##_name = { \
1269 .attr = { \
1270 .name = __stringify(_name), \
1271 .mode = _mode }, \
1272 .show = show_##_name, \
1273 .store = store_##_name, \
1274 }
1275
Corentin Charye12e6d92011-02-26 10:20:31 +01001276ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
1277ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
1278ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
Corentin Chary9e1565b2011-02-06 13:28:36 +01001279
Dmitry Torokhov67fa38e2010-11-03 11:14:01 -07001280static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
1281 const char *buf, size_t count)
Chris Bagwell7f80d732010-10-11 18:47:18 -05001282{
1283 int value;
Chris Bagwell7f80d732010-10-11 18:47:18 -05001284
1285 if (!count || sscanf(buf, "%i", &value) != 1)
1286 return -EINVAL;
1287 if (value < 0 || value > 2)
1288 return -EINVAL;
1289
Corentin Charyd33da3b2011-02-26 10:20:35 +01001290 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001291}
1292
1293static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
1294
Corentin Chary4e37b422010-11-29 08:14:08 +01001295static struct attribute *platform_attributes[] = {
1296 &dev_attr_cpufv.attr,
Corentin Chary9e1565b2011-02-06 13:28:36 +01001297 &dev_attr_camera.attr,
1298 &dev_attr_cardr.attr,
Corentin Chary4615bb62011-02-06 13:28:42 +01001299 &dev_attr_touchpad.attr,
Corentin Chary4e37b422010-11-29 08:14:08 +01001300 NULL
1301};
1302
Corentin Charye12e6d92011-02-26 10:20:31 +01001303static mode_t asus_sysfs_is_visible(struct kobject *kobj,
1304 struct attribute *attr, int idx)
Corentin Chary9e1565b2011-02-06 13:28:36 +01001305{
Corentin Chary1d070f82011-02-26 10:20:36 +01001306 struct device *dev = container_of(kobj, struct device, kobj);
1307 struct platform_device *pdev = to_platform_device(dev);
1308 struct asus_wmi *asus = platform_get_drvdata(pdev);
1309 bool ok = true;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001310 int devid = -1;
1311
1312 if (attr == &dev_attr_camera.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01001313 devid = ASUS_WMI_DEVID_CAMERA;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001314 else if (attr == &dev_attr_cardr.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01001315 devid = ASUS_WMI_DEVID_CARDREADER;
Corentin Chary4615bb62011-02-06 13:28:42 +01001316 else if (attr == &dev_attr_touchpad.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01001317 devid = ASUS_WMI_DEVID_TOUCHPAD;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001318
1319 if (devid != -1)
Corentin Chary1d070f82011-02-26 10:20:36 +01001320 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
Corentin Chary9e1565b2011-02-06 13:28:36 +01001321
Corentin Chary1d070f82011-02-26 10:20:36 +01001322 return ok ? attr->mode : 0;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001323}
1324
Corentin Chary4e37b422010-11-29 08:14:08 +01001325static struct attribute_group platform_attribute_group = {
Corentin Charye12e6d92011-02-26 10:20:31 +01001326 .is_visible = asus_sysfs_is_visible,
1327 .attrs = platform_attributes
Corentin Chary4e37b422010-11-29 08:14:08 +01001328};
1329
Corentin Charye12e6d92011-02-26 10:20:31 +01001330static void asus_wmi_sysfs_exit(struct platform_device *device)
Chris Bagwell7f80d732010-10-11 18:47:18 -05001331{
Corentin Chary4e37b422010-11-29 08:14:08 +01001332 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001333}
1334
Corentin Charye12e6d92011-02-26 10:20:31 +01001335static int asus_wmi_sysfs_init(struct platform_device *device)
Chris Bagwell7f80d732010-10-11 18:47:18 -05001336{
Corentin Chary4e37b422010-11-29 08:14:08 +01001337 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001338}
1339
Corentin Chary27c136c2010-11-29 08:14:05 +01001340/*
1341 * Platform device
1342 */
Joe Perches39ddf3b2011-03-29 15:21:32 -07001343static int asus_wmi_platform_init(struct asus_wmi *asus)
Corentin Chary27c136c2010-11-29 08:14:05 +01001344{
Corentin Chary46dbca82011-02-26 10:20:38 +01001345 int rv;
1346
1347 /* INIT enable hotkeys on some models */
1348 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
1349 pr_info("Initialization: %#x", rv);
1350
1351 /* We don't know yet what to do with this version... */
1352 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
Corentin Chary57d5c8e2011-07-01 11:34:30 +02001353 pr_info("BIOS WMI version: %d.%d", rv >> 16, rv & 0xFF);
Corentin Chary46dbca82011-02-26 10:20:38 +01001354 asus->spec = rv;
1355 }
1356
1357 /*
1358 * The SFUN method probably allows the original driver to get the list
1359 * of features supported by a given model. For now, 0x0100 or 0x0800
1360 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
1361 * The significance of others is yet to be found.
1362 */
1363 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
1364 pr_info("SFUN value: %#x", rv);
1365 asus->sfun = rv;
1366 }
1367
Corentin Chary1d070f82011-02-26 10:20:36 +01001368 /*
1369 * Eee PC and Notebooks seems to have different method_id for DSTS,
1370 * but it may also be related to the BIOS's SPEC.
1371 * Note, on most Eeepc, there is no way to check if a method exist
1372 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
1373 * but once again, SPEC may probably be used for that kind of things.
1374 */
1375 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS, 0, 0, NULL))
1376 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
1377 else if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS2, 0, 0, NULL))
1378 asus->dsts_id = ASUS_WMI_METHODID_DSTS2;
1379
1380 if (!asus->dsts_id) {
1381 pr_err("Can't find DSTS");
1382 return -ENODEV;
1383 }
1384
Corentin Charye12e6d92011-02-26 10:20:31 +01001385 return asus_wmi_sysfs_init(asus->platform_device);
Corentin Chary27c136c2010-11-29 08:14:05 +01001386}
1387
Corentin Charye12e6d92011-02-26 10:20:31 +01001388static void asus_wmi_platform_exit(struct asus_wmi *asus)
Corentin Chary27c136c2010-11-29 08:14:05 +01001389{
Corentin Charye12e6d92011-02-26 10:20:31 +01001390 asus_wmi_sysfs_exit(asus->platform_device);
Corentin Chary27c136c2010-11-29 08:14:05 +01001391}
1392
1393/*
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001394 * debugfs
1395 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001396struct asus_wmi_debugfs_node {
1397 struct asus_wmi *asus;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001398 char *name;
Corentin Charye12e6d92011-02-26 10:20:31 +01001399 int (*show) (struct seq_file *m, void *data);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001400};
1401
1402static int show_dsts(struct seq_file *m, void *data)
1403{
Corentin Charye12e6d92011-02-26 10:20:31 +01001404 struct asus_wmi *asus = m->private;
Corentin Charyd33da3b2011-02-26 10:20:35 +01001405 int err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001406 u32 retval = -1;
1407
Corentin Chary1d070f82011-02-26 10:20:36 +01001408 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001409
Corentin Charyd33da3b2011-02-26 10:20:35 +01001410 if (err < 0)
1411 return err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001412
Corentin Charyef343492011-02-26 10:20:39 +01001413 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001414
1415 return 0;
1416}
1417
1418static int show_devs(struct seq_file *m, void *data)
1419{
Corentin Charye12e6d92011-02-26 10:20:31 +01001420 struct asus_wmi *asus = m->private;
Corentin Charyd33da3b2011-02-26 10:20:35 +01001421 int err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001422 u32 retval = -1;
1423
Corentin Charyd33da3b2011-02-26 10:20:35 +01001424 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
1425 &retval);
1426
1427 if (err < 0)
1428 return err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001429
Corentin Charyef343492011-02-26 10:20:39 +01001430 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
Corentin Charye12e6d92011-02-26 10:20:31 +01001431 asus->debug.ctrl_param, retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001432
1433 return 0;
1434}
1435
Corentin Charyef343492011-02-26 10:20:39 +01001436static int show_call(struct seq_file *m, void *data)
1437{
1438 struct asus_wmi *asus = m->private;
1439 struct bios_args args = {
1440 .arg0 = asus->debug.dev_id,
1441 .arg1 = asus->debug.ctrl_param,
1442 };
1443 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1444 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
1445 union acpi_object *obj;
1446 acpi_status status;
1447
1448 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
1449 1, asus->debug.method_id,
1450 &input, &output);
1451
1452 if (ACPI_FAILURE(status))
1453 return -EIO;
1454
1455 obj = (union acpi_object *)output.pointer;
1456 if (obj && obj->type == ACPI_TYPE_INTEGER)
1457 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
1458 asus->debug.dev_id, asus->debug.ctrl_param,
1459 (u32) obj->integer.value);
1460 else
1461 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
1462 asus->debug.dev_id, asus->debug.ctrl_param,
Dan Carpentera1d60862011-03-15 10:07:37 +03001463 obj ? obj->type : -1);
Corentin Charyef343492011-02-26 10:20:39 +01001464
1465 kfree(obj);
1466
1467 return 0;
1468}
1469
Corentin Charye12e6d92011-02-26 10:20:31 +01001470static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
1471 {NULL, "devs", show_devs},
1472 {NULL, "dsts", show_dsts},
Corentin Charyef343492011-02-26 10:20:39 +01001473 {NULL, "call", show_call},
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001474};
1475
Corentin Charye12e6d92011-02-26 10:20:31 +01001476static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001477{
Corentin Charye12e6d92011-02-26 10:20:31 +01001478 struct asus_wmi_debugfs_node *node = inode->i_private;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001479
Corentin Charye12e6d92011-02-26 10:20:31 +01001480 return single_open(file, node->show, node->asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001481}
1482
Corentin Charye12e6d92011-02-26 10:20:31 +01001483static const struct file_operations asus_wmi_debugfs_io_ops = {
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001484 .owner = THIS_MODULE,
Corentin Charye12e6d92011-02-26 10:20:31 +01001485 .open = asus_wmi_debugfs_open,
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001486 .read = seq_read,
1487 .llseek = seq_lseek,
1488 .release = single_release,
1489};
1490
Corentin Charye12e6d92011-02-26 10:20:31 +01001491static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001492{
Corentin Charye12e6d92011-02-26 10:20:31 +01001493 debugfs_remove_recursive(asus->debug.root);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001494}
1495
Corentin Charye12e6d92011-02-26 10:20:31 +01001496static int asus_wmi_debugfs_init(struct asus_wmi *asus)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001497{
1498 struct dentry *dent;
1499 int i;
1500
Corentin Charye12e6d92011-02-26 10:20:31 +01001501 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
1502 if (!asus->debug.root) {
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001503 pr_err("failed to create debugfs directory");
1504 goto error_debugfs;
1505 }
1506
Corentin Charyef343492011-02-26 10:20:39 +01001507 dent = debugfs_create_x32("method_id", S_IRUGO | S_IWUSR,
1508 asus->debug.root, &asus->debug.method_id);
1509 if (!dent)
1510 goto error_debugfs;
1511
Corentin Charye12e6d92011-02-26 10:20:31 +01001512 dent = debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR,
1513 asus->debug.root, &asus->debug.dev_id);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001514 if (!dent)
1515 goto error_debugfs;
1516
Corentin Charye12e6d92011-02-26 10:20:31 +01001517 dent = debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR,
1518 asus->debug.root, &asus->debug.ctrl_param);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001519 if (!dent)
1520 goto error_debugfs;
1521
Corentin Charye12e6d92011-02-26 10:20:31 +01001522 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
1523 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001524
Corentin Charye12e6d92011-02-26 10:20:31 +01001525 node->asus = asus;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001526 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
Corentin Charye12e6d92011-02-26 10:20:31 +01001527 asus->debug.root, node,
1528 &asus_wmi_debugfs_io_ops);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001529 if (!dent) {
1530 pr_err("failed to create debug file: %s\n", node->name);
1531 goto error_debugfs;
1532 }
1533 }
1534
1535 return 0;
1536
1537error_debugfs:
Corentin Charye12e6d92011-02-26 10:20:31 +01001538 asus_wmi_debugfs_exit(asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001539 return -ENOMEM;
1540}
1541
1542/*
Corentin Chary27c136c2010-11-29 08:14:05 +01001543 * WMI Driver
1544 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001545static int asus_wmi_add(struct platform_device *pdev)
Corentin Charyafa7c882011-02-06 13:28:28 +01001546{
Corentin Charye12e6d92011-02-26 10:20:31 +01001547 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
1548 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
1549 struct asus_wmi *asus;
Yong Wangee027e42010-03-21 10:26:34 +08001550 acpi_status status;
Corentin Chary27c136c2010-11-29 08:14:05 +01001551 int err;
Yong Wangee027e42010-03-21 10:26:34 +08001552
Corentin Charye12e6d92011-02-26 10:20:31 +01001553 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
1554 if (!asus)
Corentin Charya04ce292011-02-06 13:28:33 +01001555 return -ENOMEM;
1556
Corentin Charye12e6d92011-02-26 10:20:31 +01001557 asus->driver = wdrv;
1558 asus->platform_device = pdev;
1559 wdrv->platform_device = pdev;
1560 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001561
Corentin Charye12e6d92011-02-26 10:20:31 +01001562 if (wdrv->quirks)
1563 wdrv->quirks(asus->driver);
Corentin Charyafa7c882011-02-06 13:28:28 +01001564
Corentin Charye12e6d92011-02-26 10:20:31 +01001565 err = asus_wmi_platform_init(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001566 if (err)
1567 goto fail_platform;
Yong Wang45f2c692010-04-11 09:27:19 +08001568
Corentin Charye12e6d92011-02-26 10:20:31 +01001569 err = asus_wmi_input_init(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08001570 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +01001571 goto fail_input;
Yong Wang3d7b1652010-04-11 09:27:54 +08001572
Corentin Charye07babd2011-02-26 10:20:42 +01001573 err = asus_wmi_hwmon_init(asus);
1574 if (err)
1575 goto fail_hwmon;
1576
Corentin Charye12e6d92011-02-26 10:20:31 +01001577 err = asus_wmi_led_init(asus);
Corentin Chary084fca62010-11-29 08:14:06 +01001578 if (err)
1579 goto fail_leds;
1580
Corentin Charye12e6d92011-02-26 10:20:31 +01001581 err = asus_wmi_rfkill_init(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001582 if (err)
1583 goto fail_rfkill;
1584
Yong Wang3d7b1652010-04-11 09:27:54 +08001585 if (!acpi_video_backlight_support()) {
Corentin Charye12e6d92011-02-26 10:20:31 +01001586 err = asus_wmi_backlight_init(asus);
Corentin Charyb7187262011-02-06 13:28:39 +01001587 if (err && err != -ENODEV)
Corentin Chary27c136c2010-11-29 08:14:05 +01001588 goto fail_backlight;
Yong Wang3d7b1652010-04-11 09:27:54 +08001589 } else
1590 pr_info("Backlight controlled by ACPI video driver\n");
Yong Wang45f2c692010-04-11 09:27:19 +08001591
Corentin Charye12e6d92011-02-26 10:20:31 +01001592 status = wmi_install_notify_handler(asus->driver->event_guid,
1593 asus_wmi_notify, asus);
Yong Wang45f2c692010-04-11 09:27:19 +08001594 if (ACPI_FAILURE(status)) {
Corentin Charye12e6d92011-02-26 10:20:31 +01001595 pr_err("Unable to register notify handler - %d\n", status);
Yong Wang45f2c692010-04-11 09:27:19 +08001596 err = -ENODEV;
Corentin Chary27c136c2010-11-29 08:14:05 +01001597 goto fail_wmi_handler;
Yong Wang45f2c692010-04-11 09:27:19 +08001598 }
1599
Corentin Charye12e6d92011-02-26 10:20:31 +01001600 err = asus_wmi_debugfs_init(asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001601 if (err)
1602 goto fail_debugfs;
1603
Corentin Charya04ce292011-02-06 13:28:33 +01001604 return 0;
Yong Wang45f2c692010-04-11 09:27:19 +08001605
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001606fail_debugfs:
Corentin Charye12e6d92011-02-26 10:20:31 +01001607 wmi_remove_notify_handler(asus->driver->event_guid);
Corentin Chary27c136c2010-11-29 08:14:05 +01001608fail_wmi_handler:
Corentin Charye12e6d92011-02-26 10:20:31 +01001609 asus_wmi_backlight_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001610fail_backlight:
Corentin Charye12e6d92011-02-26 10:20:31 +01001611 asus_wmi_rfkill_exit(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001612fail_rfkill:
Corentin Charye12e6d92011-02-26 10:20:31 +01001613 asus_wmi_led_exit(asus);
Corentin Chary084fca62010-11-29 08:14:06 +01001614fail_leds:
Corentin Charye07babd2011-02-26 10:20:42 +01001615 asus_wmi_hwmon_exit(asus);
1616fail_hwmon:
Corentin Charye12e6d92011-02-26 10:20:31 +01001617 asus_wmi_input_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001618fail_input:
Corentin Charye12e6d92011-02-26 10:20:31 +01001619 asus_wmi_platform_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001620fail_platform:
Corentin Charye12e6d92011-02-26 10:20:31 +01001621 kfree(asus);
Corentin Charya04ce292011-02-06 13:28:33 +01001622 return err;
Yong Wang45f2c692010-04-11 09:27:19 +08001623}
1624
Corentin Charye12e6d92011-02-26 10:20:31 +01001625static int asus_wmi_remove(struct platform_device *device)
Yong Wang45f2c692010-04-11 09:27:19 +08001626{
Corentin Charye12e6d92011-02-26 10:20:31 +01001627 struct asus_wmi *asus;
Yong Wang45f2c692010-04-11 09:27:19 +08001628
Corentin Charye12e6d92011-02-26 10:20:31 +01001629 asus = platform_get_drvdata(device);
1630 wmi_remove_notify_handler(asus->driver->event_guid);
1631 asus_wmi_backlight_exit(asus);
1632 asus_wmi_input_exit(asus);
Corentin Charye07babd2011-02-26 10:20:42 +01001633 asus_wmi_hwmon_exit(asus);
Corentin Charye12e6d92011-02-26 10:20:31 +01001634 asus_wmi_led_exit(asus);
1635 asus_wmi_rfkill_exit(asus);
1636 asus_wmi_debugfs_exit(asus);
1637 asus_wmi_platform_exit(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08001638
Corentin Charye12e6d92011-02-26 10:20:31 +01001639 kfree(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08001640 return 0;
1641}
1642
Corentin Chary0773d7f2011-02-06 13:28:32 +01001643/*
1644 * Platform driver - hibernate/resume callbacks
1645 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001646static int asus_hotk_thaw(struct device *device)
Corentin Chary0773d7f2011-02-06 13:28:32 +01001647{
Corentin Charye12e6d92011-02-26 10:20:31 +01001648 struct asus_wmi *asus = dev_get_drvdata(device);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001649
Corentin Charya7ce3f02011-02-26 10:20:33 +01001650 if (asus->wlan.rfkill) {
Corentin Chary0773d7f2011-02-06 13:28:32 +01001651 bool wlan;
1652
1653 /*
1654 * Work around bios bug - acpi _PTS turns off the wireless led
1655 * during suspend. Normally it restores it on resume, but
1656 * we should kick it ourselves in case hibernation is aborted.
1657 */
Corentin Chary1d070f82011-02-26 10:20:36 +01001658 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charye12e6d92011-02-26 10:20:31 +01001659 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001660 }
1661
1662 return 0;
1663}
1664
Corentin Charye12e6d92011-02-26 10:20:31 +01001665static int asus_hotk_restore(struct device *device)
Corentin Chary0773d7f2011-02-06 13:28:32 +01001666{
Corentin Charye12e6d92011-02-26 10:20:31 +01001667 struct asus_wmi *asus = dev_get_drvdata(device);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001668 int bl;
1669
1670 /* Refresh both wlan rfkill state and pci hotplug */
Corentin Charya7ce3f02011-02-26 10:20:33 +01001671 if (asus->wlan.rfkill)
Corentin Charye12e6d92011-02-26 10:20:31 +01001672 asus_rfkill_hotplug(asus);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001673
Corentin Charya7ce3f02011-02-26 10:20:33 +01001674 if (asus->bluetooth.rfkill) {
Corentin Chary1d070f82011-02-26 10:20:36 +01001675 bl = !asus_wmi_get_devstate_simple(asus,
1676 ASUS_WMI_DEVID_BLUETOOTH);
Corentin Charya7ce3f02011-02-26 10:20:33 +01001677 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01001678 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01001679 if (asus->wimax.rfkill) {
Corentin Chary1d070f82011-02-26 10:20:36 +01001680 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
Corentin Charya7ce3f02011-02-26 10:20:33 +01001681 rfkill_set_sw_state(asus->wimax.rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01001682 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01001683 if (asus->wwan3g.rfkill) {
Corentin Chary1d070f82011-02-26 10:20:36 +01001684 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
Corentin Charya7ce3f02011-02-26 10:20:33 +01001685 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001686 }
1687
1688 return 0;
1689}
1690
Corentin Charye12e6d92011-02-26 10:20:31 +01001691static const struct dev_pm_ops asus_pm_ops = {
1692 .thaw = asus_hotk_thaw,
1693 .restore = asus_hotk_restore,
Corentin Chary0773d7f2011-02-06 13:28:32 +01001694};
1695
Corentin Charye12e6d92011-02-26 10:20:31 +01001696static int asus_wmi_probe(struct platform_device *pdev)
Corentin Charyd358cb52010-11-29 08:14:14 +01001697{
Corentin Charye12e6d92011-02-26 10:20:31 +01001698 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
1699 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
1700 int ret;
Corentin Charyd358cb52010-11-29 08:14:14 +01001701
Corentin Charye12e6d92011-02-26 10:20:31 +01001702 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -07001703 pr_warn("Management GUID not found\n");
Yong Wangee027e42010-03-21 10:26:34 +08001704 return -ENODEV;
1705 }
1706
Corentin Charye12e6d92011-02-26 10:20:31 +01001707 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -07001708 pr_warn("Event GUID not found\n");
Corentin Charyd358cb52010-11-29 08:14:14 +01001709 return -ENODEV;
1710 }
1711
Corentin Charye12e6d92011-02-26 10:20:31 +01001712 if (wdrv->probe) {
1713 ret = wdrv->probe(pdev);
1714 if (ret)
1715 return ret;
1716 }
1717
1718 return asus_wmi_add(pdev);
Corentin Charya04ce292011-02-06 13:28:33 +01001719}
Yong Wangee027e42010-03-21 10:26:34 +08001720
Corentin Charye12e6d92011-02-26 10:20:31 +01001721static bool used;
Yong Wangee027e42010-03-21 10:26:34 +08001722
Corentin Chary8fe8c252011-07-01 11:34:32 +02001723int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
Corentin Charya04ce292011-02-06 13:28:33 +01001724{
Corentin Charye12e6d92011-02-26 10:20:31 +01001725 struct platform_driver *platform_driver;
1726 struct platform_device *platform_device;
1727
1728 if (used)
1729 return -EBUSY;
1730
1731 platform_driver = &driver->platform_driver;
1732 platform_driver->remove = asus_wmi_remove;
1733 platform_driver->driver.owner = driver->owner;
1734 platform_driver->driver.name = driver->name;
1735 platform_driver->driver.pm = &asus_pm_ops;
1736
1737 platform_device = platform_create_bundle(platform_driver,
1738 asus_wmi_probe,
Corentin Charya04ce292011-02-06 13:28:33 +01001739 NULL, 0, NULL, 0);
1740 if (IS_ERR(platform_device))
1741 return PTR_ERR(platform_device);
Corentin Charye12e6d92011-02-26 10:20:31 +01001742
1743 used = true;
1744 return 0;
1745}
1746EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
1747
1748void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
1749{
1750 platform_device_unregister(driver->platform_device);
1751 platform_driver_unregister(&driver->platform_driver);
1752 used = false;
1753}
1754EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
1755
1756static int __init asus_wmi_init(void)
1757{
1758 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
1759 pr_info("Asus Management GUID not found");
1760 return -ENODEV;
1761 }
1762
1763 pr_info("ASUS WMI generic driver loaded");
Yong Wangee027e42010-03-21 10:26:34 +08001764 return 0;
1765}
1766
Corentin Charye12e6d92011-02-26 10:20:31 +01001767static void __exit asus_wmi_exit(void)
Yong Wangee027e42010-03-21 10:26:34 +08001768{
Corentin Charye12e6d92011-02-26 10:20:31 +01001769 pr_info("ASUS WMI generic driver unloaded");
Yong Wangee027e42010-03-21 10:26:34 +08001770}
1771
Corentin Charye12e6d92011-02-26 10:20:31 +01001772module_init(asus_wmi_init);
1773module_exit(asus_wmi_exit);