blob: 9095c28340cd6eac3277595ba731554b0560ec47 [file] [log] [blame]
Yong Wangee027e42010-03-21 10:26:34 +08001/*
Corentin Charye12e6d92011-02-26 10:20:31 +01002 * Asus PC WMI hotkey driver
Yong Wangee027e42010-03-21 10:26:34 +08003 *
4 * Copyright(C) 2010 Intel Corporation.
Corentin Chary57ab7da2011-02-26 10:20:32 +01005 * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
Yong Wangee027e42010-03-21 10:26:34 +08006 *
7 * Portions based on wistron_btns.c:
8 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
9 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
10 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
Yong Wang81248882010-04-11 09:26:33 +080027#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
Yong Wangee027e42010-03-21 10:26:34 +080029#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/types.h>
Tejun Heoa32f3922010-04-05 11:37:59 +090033#include <linux/slab.h>
Yong Wangee027e42010-03-21 10:26:34 +080034#include <linux/input.h>
35#include <linux/input/sparse-keymap.h>
Yong Wang3d7b1652010-04-11 09:27:54 +080036#include <linux/fb.h>
37#include <linux/backlight.h>
Corentin Chary084fca62010-11-29 08:14:06 +010038#include <linux/leds.h>
Corentin Charyba48fdb2010-11-29 08:14:07 +010039#include <linux/rfkill.h>
Corentin Charyafa7c882011-02-06 13:28:28 +010040#include <linux/pci.h>
41#include <linux/pci_hotplug.h>
Corentin Chary8c1b2d82010-11-29 08:14:09 +010042#include <linux/debugfs.h>
43#include <linux/seq_file.h>
Yong Wang45f2c692010-04-11 09:27:19 +080044#include <linux/platform_device.h>
Yong Wangee027e42010-03-21 10:26:34 +080045#include <acpi/acpi_bus.h>
46#include <acpi/acpi_drivers.h>
47
Corentin Charye12e6d92011-02-26 10:20:31 +010048#include "asus-wmi.h"
Yong Wang45f2c692010-04-11 09:27:19 +080049
Corentin Charye12e6d92011-02-26 10:20:31 +010050MODULE_AUTHOR("Corentin Chary <corentincj@iksaif.net>, "
51 "Yong Wang <yong.y.wang@intel.com>");
52MODULE_DESCRIPTION("Asus Generic WMI Driver");
Yong Wangee027e42010-03-21 10:26:34 +080053MODULE_LICENSE("GPL");
54
Corentin Charye12e6d92011-02-26 10:20:31 +010055#define to_platform_driver(drv) \
56 (container_of((drv), struct platform_driver, driver))
Corentin Charyd358cb52010-11-29 08:14:14 +010057
Corentin Charye12e6d92011-02-26 10:20:31 +010058#define to_asus_wmi_driver(pdrv) \
59 (container_of((pdrv), struct asus_wmi_driver, platform_driver))
Yong Wangee027e42010-03-21 10:26:34 +080060
Corentin Charye12e6d92011-02-26 10:20:31 +010061#define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
Yong Wangee027e42010-03-21 10:26:34 +080062
Corentin Chary33e0e6f2011-02-06 13:28:34 +010063#define NOTIFY_BRNUP_MIN 0x11
64#define NOTIFY_BRNUP_MAX 0x1f
65#define NOTIFY_BRNDOWN_MIN 0x20
66#define NOTIFY_BRNDOWN_MAX 0x2e
Yong Wangee027e42010-03-21 10:26:34 +080067
Corentin Chary43815942011-02-06 13:28:43 +010068/* WMI Methods */
Corentin Chary46dbca82011-02-26 10:20:38 +010069#define ASUS_WMI_METHODID_SPEC 0x43455053
70#define ASUS_WMI_METHODID_SFUN 0x4E554653
Corentin Charye12e6d92011-02-26 10:20:31 +010071#define ASUS_WMI_METHODID_DSTS 0x53544344
Corentin Chary1d070f82011-02-26 10:20:36 +010072#define ASUS_WMI_METHODID_DSTS2 0x53545344
Corentin Charye12e6d92011-02-26 10:20:31 +010073#define ASUS_WMI_METHODID_DEVS 0x53564544
74#define ASUS_WMI_METHODID_CFVS 0x53564643
Corentin Chary46dbca82011-02-26 10:20:38 +010075#define ASUS_WMI_METHODID_INIT 0x54494E49
Yong Wang3d7b1652010-04-11 09:27:54 +080076
Corentin Charyd33da3b2011-02-26 10:20:35 +010077#define ASUS_WMI_UNSUPPORTED_METHOD 0xFFFFFFFE
78
Corentin Chary43815942011-02-06 13:28:43 +010079/* Wireless */
Corentin Charye12e6d92011-02-26 10:20:31 +010080#define ASUS_WMI_DEVID_WLAN 0x00010011
81#define ASUS_WMI_DEVID_BLUETOOTH 0x00010013
82#define ASUS_WMI_DEVID_WIMAX 0x00010017
83#define ASUS_WMI_DEVID_WWAN3G 0x00010019
Corentin Chary43815942011-02-06 13:28:43 +010084
85/* Backlight and Brightness */
Corentin Charye12e6d92011-02-26 10:20:31 +010086#define ASUS_WMI_DEVID_BACKLIGHT 0x00050011
87#define ASUS_WMI_DEVID_BRIGHTNESS 0x00050012
Corentin Chary43815942011-02-06 13:28:43 +010088
89/* Misc */
Corentin Charye12e6d92011-02-26 10:20:31 +010090#define ASUS_WMI_DEVID_CAMERA 0x00060013
Corentin Chary43815942011-02-06 13:28:43 +010091
92/* Storage */
Corentin Charye12e6d92011-02-26 10:20:31 +010093#define ASUS_WMI_DEVID_CARDREADER 0x00080013
Corentin Chary43815942011-02-06 13:28:43 +010094
95/* Input */
Corentin Chary57ab7da2011-02-26 10:20:32 +010096#define ASUS_WMI_DEVID_TOUCHPAD 0x00100011
Corentin Charye12e6d92011-02-26 10:20:31 +010097#define ASUS_WMI_DEVID_TOUCHPAD_LED 0x00100012
Yong Wang3d7b1652010-04-11 09:27:54 +080098
Corentin Chary43815942011-02-06 13:28:43 +010099/* DSTS masks */
Corentin Charye12e6d92011-02-26 10:20:31 +0100100#define ASUS_WMI_DSTS_STATUS_BIT 0x00000001
Corentin Charya75fe0d2011-02-26 10:20:34 +0100101#define ASUS_WMI_DSTS_UNKNOWN_BIT 0x00000002
Corentin Charye12e6d92011-02-26 10:20:31 +0100102#define ASUS_WMI_DSTS_PRESENCE_BIT 0x00010000
103#define ASUS_WMI_DSTS_BRIGHTNESS_MASK 0x000000FF
104#define ASUS_WMI_DSTS_MAX_BRIGTH_MASK 0x0000FF00
Yong Wangee027e42010-03-21 10:26:34 +0800105
Yong Wang3d7b1652010-04-11 09:27:54 +0800106struct bios_args {
Corentin Charyd33da3b2011-02-26 10:20:35 +0100107 u32 arg0;
108 u32 arg1;
109} __packed;
Yong Wang3d7b1652010-04-11 09:27:54 +0800110
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100111/*
Corentin Charye12e6d92011-02-26 10:20:31 +0100112 * <platform>/ - debugfs root directory
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100113 * dev_id - current dev_id
114 * ctrl_param - current ctrl_param
Corentin Charyef343492011-02-26 10:20:39 +0100115 * method_id - current method_id
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100116 * devs - call DEVS(dev_id, ctrl_param) and print result
117 * dsts - call DSTS(dev_id) and print result
Corentin Charyef343492011-02-26 10:20:39 +0100118 * call - call method_id(dev_id, ctrl_param) and print result
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100119 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100120struct asus_wmi_debug {
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100121 struct dentry *root;
Corentin Charyef343492011-02-26 10:20:39 +0100122 u32 method_id;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100123 u32 dev_id;
124 u32 ctrl_param;
125};
126
Corentin Charya7ce3f02011-02-26 10:20:33 +0100127struct asus_rfkill {
128 struct asus_wmi *asus;
129 struct rfkill *rfkill;
130 u32 dev_id;
131};
132
Corentin Charye12e6d92011-02-26 10:20:31 +0100133struct asus_wmi {
Corentin Chary1d070f82011-02-26 10:20:36 +0100134 int dsts_id;
Corentin Chary46dbca82011-02-26 10:20:38 +0100135 int spec;
136 int sfun;
Corentin Chary1d070f82011-02-26 10:20:36 +0100137
Yong Wang81248882010-04-11 09:26:33 +0800138 struct input_dev *inputdev;
Yong Wang3d7b1652010-04-11 09:27:54 +0800139 struct backlight_device *backlight_device;
Corentin Chary27c136c2010-11-29 08:14:05 +0100140 struct platform_device *platform_device;
Corentin Chary084fca62010-11-29 08:14:06 +0100141
142 struct led_classdev tpd_led;
143 int tpd_led_wk;
144 struct workqueue_struct *led_workqueue;
145 struct work_struct tpd_led_work;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100146
Corentin Charya7ce3f02011-02-26 10:20:33 +0100147 struct asus_rfkill wlan;
148 struct asus_rfkill bluetooth;
149 struct asus_rfkill wimax;
150 struct asus_rfkill wwan3g;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100151
Corentin Charyafa7c882011-02-06 13:28:28 +0100152 struct hotplug_slot *hotplug_slot;
153 struct mutex hotplug_lock;
Corentin Chary279f8f92011-02-06 13:28:29 +0100154 struct mutex wmi_lock;
155 struct workqueue_struct *hotplug_workqueue;
156 struct work_struct hotplug_work;
Corentin Charyafa7c882011-02-06 13:28:28 +0100157
Corentin Charye12e6d92011-02-26 10:20:31 +0100158 struct asus_wmi_debug debug;
159
160 struct asus_wmi_driver *driver;
Yong Wang81248882010-04-11 09:26:33 +0800161};
162
Corentin Charye12e6d92011-02-26 10:20:31 +0100163static int asus_wmi_input_init(struct asus_wmi *asus)
Yong Wangee027e42010-03-21 10:26:34 +0800164{
165 int err;
166
Corentin Charye12e6d92011-02-26 10:20:31 +0100167 asus->inputdev = input_allocate_device();
168 if (!asus->inputdev)
Yong Wangee027e42010-03-21 10:26:34 +0800169 return -ENOMEM;
170
Corentin Charye12e6d92011-02-26 10:20:31 +0100171 asus->inputdev->name = asus->driver->input_phys;
172 asus->inputdev->phys = asus->driver->input_name;
173 asus->inputdev->id.bustype = BUS_HOST;
174 asus->inputdev->dev.parent = &asus->platform_device->dev;
Yong Wangee027e42010-03-21 10:26:34 +0800175
Corentin Charye12e6d92011-02-26 10:20:31 +0100176 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
Yong Wangee027e42010-03-21 10:26:34 +0800177 if (err)
178 goto err_free_dev;
179
Corentin Charye12e6d92011-02-26 10:20:31 +0100180 err = input_register_device(asus->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800181 if (err)
182 goto err_free_keymap;
183
184 return 0;
185
186err_free_keymap:
Corentin Charye12e6d92011-02-26 10:20:31 +0100187 sparse_keymap_free(asus->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800188err_free_dev:
Corentin Charye12e6d92011-02-26 10:20:31 +0100189 input_free_device(asus->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800190 return err;
191}
192
Corentin Charye12e6d92011-02-26 10:20:31 +0100193static void asus_wmi_input_exit(struct asus_wmi *asus)
Yong Wang81248882010-04-11 09:26:33 +0800194{
Corentin Charye12e6d92011-02-26 10:20:31 +0100195 if (asus->inputdev) {
196 sparse_keymap_free(asus->inputdev);
197 input_unregister_device(asus->inputdev);
Yong Wang81248882010-04-11 09:26:33 +0800198 }
199
Corentin Charye12e6d92011-02-26 10:20:31 +0100200 asus->inputdev = NULL;
Yong Wang81248882010-04-11 09:26:33 +0800201}
202
Corentin Charyd33da3b2011-02-26 10:20:35 +0100203static int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1,
204 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800205{
Corentin Charyd33da3b2011-02-26 10:20:35 +0100206 struct bios_args args = {
207 .arg0 = arg0,
208 .arg1 = arg1,
209 };
210 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
Yong Wang3d7b1652010-04-11 09:27:54 +0800211 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Yong Wang3d7b1652010-04-11 09:27:54 +0800212 acpi_status status;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100213 union acpi_object *obj;
Yong Wang3d7b1652010-04-11 09:27:54 +0800214 u32 tmp;
215
Corentin Charyd33da3b2011-02-26 10:20:35 +0100216 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 1, method_id,
Corentin Charyafa7c882011-02-06 13:28:28 +0100217 &input, &output);
Yong Wang3d7b1652010-04-11 09:27:54 +0800218
219 if (ACPI_FAILURE(status))
Corentin Charyd33da3b2011-02-26 10:20:35 +0100220 goto exit;
Yong Wang3d7b1652010-04-11 09:27:54 +0800221
222 obj = (union acpi_object *)output.pointer;
223 if (obj && obj->type == ACPI_TYPE_INTEGER)
Corentin Charye12e6d92011-02-26 10:20:31 +0100224 tmp = (u32) obj->integer.value;
Yong Wang3d7b1652010-04-11 09:27:54 +0800225 else
226 tmp = 0;
227
Corentin Chary2a3f0062010-11-29 08:14:10 +0100228 if (retval)
229 *retval = tmp;
Yong Wang3d7b1652010-04-11 09:27:54 +0800230
231 kfree(obj);
232
Corentin Charyd33da3b2011-02-26 10:20:35 +0100233exit:
234 if (ACPI_FAILURE(status))
235 return -EIO;
Yong Wang3d7b1652010-04-11 09:27:54 +0800236
Corentin Charyd33da3b2011-02-26 10:20:35 +0100237 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
238 return -ENODEV;
239
240 return 0;
Yong Wang3d7b1652010-04-11 09:27:54 +0800241}
242
Corentin Chary1d070f82011-02-26 10:20:36 +0100243static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
Corentin Charyd33da3b2011-02-26 10:20:35 +0100244{
Corentin Chary1d070f82011-02-26 10:20:36 +0100245 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
Corentin Charyd33da3b2011-02-26 10:20:35 +0100246}
247
248static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
Corentin Chary1d070f82011-02-26 10:20:36 +0100249 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800250{
Corentin Charyd33da3b2011-02-26 10:20:35 +0100251 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
252 ctrl_param, retval);
Yong Wang3d7b1652010-04-11 09:27:54 +0800253}
254
Corentin Chary5c956382011-02-06 13:28:31 +0100255/* Helper for special devices with magic return codes */
Corentin Chary1d070f82011-02-26 10:20:36 +0100256static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
257 u32 dev_id, u32 mask)
Corentin Chary5c956382011-02-06 13:28:31 +0100258{
259 u32 retval = 0;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100260 int err;
Corentin Chary5c956382011-02-06 13:28:31 +0100261
Corentin Chary1d070f82011-02-26 10:20:36 +0100262 err = asus_wmi_get_devstate(asus, dev_id, &retval);
Corentin Chary5c956382011-02-06 13:28:31 +0100263
Corentin Charyd33da3b2011-02-26 10:20:35 +0100264 if (err < 0)
265 return err;
Corentin Chary5c956382011-02-06 13:28:31 +0100266
Corentin Charye12e6d92011-02-26 10:20:31 +0100267 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
Corentin Chary5c956382011-02-06 13:28:31 +0100268 return -ENODEV;
269
Corentin Charya75fe0d2011-02-26 10:20:34 +0100270 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
271 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
272 return -ENODEV;
273 }
274
Corentin Charyb7187262011-02-06 13:28:39 +0100275 return retval & mask;
276}
277
Corentin Chary1d070f82011-02-26 10:20:36 +0100278static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
Corentin Charyb7187262011-02-06 13:28:39 +0100279{
Corentin Chary1d070f82011-02-26 10:20:36 +0100280 return asus_wmi_get_devstate_bits(asus, dev_id,
281 ASUS_WMI_DSTS_STATUS_BIT);
Corentin Chary5c956382011-02-06 13:28:31 +0100282}
283
Corentin Chary084fca62010-11-29 08:14:06 +0100284/*
285 * LEDs
286 */
287/*
288 * These functions actually update the LED's, and are called from a
289 * workqueue. By doing this as separate work rather than when the LED
Corentin Charye12e6d92011-02-26 10:20:31 +0100290 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
Corentin Chary084fca62010-11-29 08:14:06 +0100291 * potentially bad time, such as a timer interrupt.
292 */
293static void tpd_led_update(struct work_struct *work)
294{
295 int ctrl_param;
Corentin Charye12e6d92011-02-26 10:20:31 +0100296 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100297
Corentin Charye12e6d92011-02-26 10:20:31 +0100298 asus = container_of(work, struct asus_wmi, tpd_led_work);
Corentin Chary084fca62010-11-29 08:14:06 +0100299
Corentin Charye12e6d92011-02-26 10:20:31 +0100300 ctrl_param = asus->tpd_led_wk;
301 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
Corentin Chary084fca62010-11-29 08:14:06 +0100302}
303
304static void tpd_led_set(struct led_classdev *led_cdev,
305 enum led_brightness value)
306{
Corentin Charye12e6d92011-02-26 10:20:31 +0100307 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100308
Corentin Charye12e6d92011-02-26 10:20:31 +0100309 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
Corentin Chary084fca62010-11-29 08:14:06 +0100310
Corentin Charye12e6d92011-02-26 10:20:31 +0100311 asus->tpd_led_wk = !!value;
312 queue_work(asus->led_workqueue, &asus->tpd_led_work);
Corentin Chary084fca62010-11-29 08:14:06 +0100313}
314
Corentin Charye12e6d92011-02-26 10:20:31 +0100315static int read_tpd_led_state(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100316{
Corentin Chary1d070f82011-02-26 10:20:36 +0100317 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
Corentin Chary084fca62010-11-29 08:14:06 +0100318}
319
320static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
321{
Corentin Charye12e6d92011-02-26 10:20:31 +0100322 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100323
Corentin Charye12e6d92011-02-26 10:20:31 +0100324 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
Corentin Chary084fca62010-11-29 08:14:06 +0100325
Corentin Charye12e6d92011-02-26 10:20:31 +0100326 return read_tpd_led_state(asus);
Corentin Chary084fca62010-11-29 08:14:06 +0100327}
328
Corentin Charye12e6d92011-02-26 10:20:31 +0100329static int asus_wmi_led_init(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100330{
331 int rv;
332
Corentin Charye12e6d92011-02-26 10:20:31 +0100333 if (read_tpd_led_state(asus) < 0)
Corentin Chary084fca62010-11-29 08:14:06 +0100334 return 0;
335
Corentin Charye12e6d92011-02-26 10:20:31 +0100336 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
337 if (!asus->led_workqueue)
Corentin Chary084fca62010-11-29 08:14:06 +0100338 return -ENOMEM;
Corentin Charye12e6d92011-02-26 10:20:31 +0100339 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
Corentin Chary084fca62010-11-29 08:14:06 +0100340
Corentin Charye12e6d92011-02-26 10:20:31 +0100341 asus->tpd_led.name = "asus::touchpad";
342 asus->tpd_led.brightness_set = tpd_led_set;
343 asus->tpd_led.brightness_get = tpd_led_get;
344 asus->tpd_led.max_brightness = 1;
Corentin Chary084fca62010-11-29 08:14:06 +0100345
Corentin Charye12e6d92011-02-26 10:20:31 +0100346 rv = led_classdev_register(&asus->platform_device->dev, &asus->tpd_led);
Corentin Chary084fca62010-11-29 08:14:06 +0100347 if (rv) {
Corentin Charye12e6d92011-02-26 10:20:31 +0100348 destroy_workqueue(asus->led_workqueue);
Corentin Chary084fca62010-11-29 08:14:06 +0100349 return rv;
350 }
351
352 return 0;
353}
354
Corentin Charye12e6d92011-02-26 10:20:31 +0100355static void asus_wmi_led_exit(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100356{
Corentin Charye12e6d92011-02-26 10:20:31 +0100357 if (asus->tpd_led.dev)
358 led_classdev_unregister(&asus->tpd_led);
359 if (asus->led_workqueue)
360 destroy_workqueue(asus->led_workqueue);
Corentin Chary084fca62010-11-29 08:14:06 +0100361}
362
363/*
Corentin Charyafa7c882011-02-06 13:28:28 +0100364 * PCI hotplug (for wlan rfkill)
365 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100366static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100367{
Corentin Chary1d070f82011-02-26 10:20:36 +0100368 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100369
Corentin Chary5c956382011-02-06 13:28:31 +0100370 if (result < 0)
Corentin Charyafa7c882011-02-06 13:28:28 +0100371 return false;
Corentin Chary5c956382011-02-06 13:28:31 +0100372 return !result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100373}
374
Corentin Charye12e6d92011-02-26 10:20:31 +0100375static void asus_rfkill_hotplug(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100376{
377 struct pci_dev *dev;
378 struct pci_bus *bus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100379 bool blocked;
Corentin Charyafa7c882011-02-06 13:28:28 +0100380 bool absent;
381 u32 l;
382
Corentin Charye12e6d92011-02-26 10:20:31 +0100383 mutex_lock(&asus->wmi_lock);
384 blocked = asus_wlan_rfkill_blocked(asus);
385 mutex_unlock(&asus->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100386
Corentin Charye12e6d92011-02-26 10:20:31 +0100387 mutex_lock(&asus->hotplug_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100388
Corentin Charya7ce3f02011-02-26 10:20:33 +0100389 if (asus->wlan.rfkill)
390 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
Corentin Chary279f8f92011-02-06 13:28:29 +0100391
Corentin Charye12e6d92011-02-26 10:20:31 +0100392 if (asus->hotplug_slot) {
Corentin Charyafa7c882011-02-06 13:28:28 +0100393 bus = pci_find_bus(0, 1);
394 if (!bus) {
395 pr_warning("Unable to find PCI bus 1?\n");
396 goto out_unlock;
397 }
398
399 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
400 pr_err("Unable to read PCI config space?\n");
401 goto out_unlock;
402 }
403 absent = (l == 0xffffffff);
404
405 if (blocked != absent) {
406 pr_warning("BIOS says wireless lan is %s, "
Corentin Charye12e6d92011-02-26 10:20:31 +0100407 "but the pci device is %s\n",
408 blocked ? "blocked" : "unblocked",
409 absent ? "absent" : "present");
Corentin Charyafa7c882011-02-06 13:28:28 +0100410 pr_warning("skipped wireless hotplug as probably "
Corentin Charye12e6d92011-02-26 10:20:31 +0100411 "inappropriate for this model\n");
Corentin Charyafa7c882011-02-06 13:28:28 +0100412 goto out_unlock;
413 }
414
415 if (!blocked) {
416 dev = pci_get_slot(bus, 0);
417 if (dev) {
418 /* Device already present */
419 pci_dev_put(dev);
420 goto out_unlock;
421 }
422 dev = pci_scan_single_device(bus, 0);
423 if (dev) {
424 pci_bus_assign_resources(bus);
425 if (pci_bus_add_device(dev))
426 pr_err("Unable to hotplug wifi\n");
427 }
428 } else {
429 dev = pci_get_slot(bus, 0);
430 if (dev) {
431 pci_remove_bus_device(dev);
432 pci_dev_put(dev);
433 }
434 }
435 }
436
437out_unlock:
Corentin Charye12e6d92011-02-26 10:20:31 +0100438 mutex_unlock(&asus->hotplug_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100439}
440
Corentin Charye12e6d92011-02-26 10:20:31 +0100441static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
Corentin Charyafa7c882011-02-06 13:28:28 +0100442{
Corentin Charye12e6d92011-02-26 10:20:31 +0100443 struct asus_wmi *asus = data;
Corentin Charyafa7c882011-02-06 13:28:28 +0100444
445 if (event != ACPI_NOTIFY_BUS_CHECK)
446 return;
447
Corentin Chary279f8f92011-02-06 13:28:29 +0100448 /*
Corentin Charye12e6d92011-02-26 10:20:31 +0100449 * We can't call directly asus_rfkill_hotplug because most
Corentin Chary279f8f92011-02-06 13:28:29 +0100450 * of the time WMBC is still being executed and not reetrant.
451 * There is currently no way to tell ACPICA that we want this
Corentin Charye12e6d92011-02-26 10:20:31 +0100452 * method to be serialized, we schedule a asus_rfkill_hotplug
Corentin Chary279f8f92011-02-06 13:28:29 +0100453 * call later, in a safer context.
454 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100455 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
Corentin Charyafa7c882011-02-06 13:28:28 +0100456}
457
Corentin Charye12e6d92011-02-26 10:20:31 +0100458static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
Corentin Charyafa7c882011-02-06 13:28:28 +0100459{
460 acpi_status status;
461 acpi_handle handle;
462
463 status = acpi_get_handle(NULL, node, &handle);
464
465 if (ACPI_SUCCESS(status)) {
466 status = acpi_install_notify_handler(handle,
467 ACPI_SYSTEM_NOTIFY,
Corentin Charye12e6d92011-02-26 10:20:31 +0100468 asus_rfkill_notify, asus);
Corentin Charyafa7c882011-02-06 13:28:28 +0100469 if (ACPI_FAILURE(status))
470 pr_warning("Failed to register notify on %s\n", node);
471 } else
472 return -ENODEV;
473
474 return 0;
475}
476
Corentin Charye12e6d92011-02-26 10:20:31 +0100477static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
Corentin Charyafa7c882011-02-06 13:28:28 +0100478{
479 acpi_status status = AE_OK;
480 acpi_handle handle;
481
482 status = acpi_get_handle(NULL, node, &handle);
483
484 if (ACPI_SUCCESS(status)) {
485 status = acpi_remove_notify_handler(handle,
Corentin Charye12e6d92011-02-26 10:20:31 +0100486 ACPI_SYSTEM_NOTIFY,
487 asus_rfkill_notify);
Corentin Charyafa7c882011-02-06 13:28:28 +0100488 if (ACPI_FAILURE(status))
489 pr_err("Error removing rfkill notify handler %s\n",
Corentin Charye12e6d92011-02-26 10:20:31 +0100490 node);
Corentin Charyafa7c882011-02-06 13:28:28 +0100491 }
492}
493
Corentin Charye12e6d92011-02-26 10:20:31 +0100494static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
495 u8 *value)
Corentin Charyafa7c882011-02-06 13:28:28 +0100496{
Corentin Chary1d070f82011-02-26 10:20:36 +0100497 struct asus_wmi *asus = hotplug_slot->private;
498 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)
501 return result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100502
Corentin Chary5c956382011-02-06 13:28:31 +0100503 *value = !!result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100504 return 0;
505}
506
Corentin Charye12e6d92011-02-26 10:20:31 +0100507static void asus_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
Corentin Charyafa7c882011-02-06 13:28:28 +0100508{
509 kfree(hotplug_slot->info);
510 kfree(hotplug_slot);
511}
512
Corentin Charye12e6d92011-02-26 10:20:31 +0100513static struct hotplug_slot_ops asus_hotplug_slot_ops = {
Corentin Charyafa7c882011-02-06 13:28:28 +0100514 .owner = THIS_MODULE,
Corentin Charye12e6d92011-02-26 10:20:31 +0100515 .get_adapter_status = asus_get_adapter_status,
516 .get_power_status = asus_get_adapter_status,
Corentin Charyafa7c882011-02-06 13:28:28 +0100517};
518
Corentin Charye12e6d92011-02-26 10:20:31 +0100519static void asus_hotplug_work(struct work_struct *work)
Corentin Chary279f8f92011-02-06 13:28:29 +0100520{
Corentin Charye12e6d92011-02-26 10:20:31 +0100521 struct asus_wmi *asus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100522
Corentin Charye12e6d92011-02-26 10:20:31 +0100523 asus = container_of(work, struct asus_wmi, hotplug_work);
524 asus_rfkill_hotplug(asus);
Corentin Chary279f8f92011-02-06 13:28:29 +0100525}
526
Corentin Charye12e6d92011-02-26 10:20:31 +0100527static int asus_setup_pci_hotplug(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100528{
529 int ret = -ENOMEM;
530 struct pci_bus *bus = pci_find_bus(0, 1);
531
532 if (!bus) {
533 pr_err("Unable to find wifi PCI bus\n");
534 return -ENODEV;
535 }
536
Corentin Charye12e6d92011-02-26 10:20:31 +0100537 asus->hotplug_workqueue =
538 create_singlethread_workqueue("hotplug_workqueue");
539 if (!asus->hotplug_workqueue)
Corentin Chary279f8f92011-02-06 13:28:29 +0100540 goto error_workqueue;
541
Corentin Charye12e6d92011-02-26 10:20:31 +0100542 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
Corentin Chary279f8f92011-02-06 13:28:29 +0100543
Corentin Charye12e6d92011-02-26 10:20:31 +0100544 asus->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
545 if (!asus->hotplug_slot)
Corentin Charyafa7c882011-02-06 13:28:28 +0100546 goto error_slot;
547
Corentin Charye12e6d92011-02-26 10:20:31 +0100548 asus->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
549 GFP_KERNEL);
550 if (!asus->hotplug_slot->info)
Corentin Charyafa7c882011-02-06 13:28:28 +0100551 goto error_info;
552
Corentin Charye12e6d92011-02-26 10:20:31 +0100553 asus->hotplug_slot->private = asus;
554 asus->hotplug_slot->release = &asus_cleanup_pci_hotplug;
555 asus->hotplug_slot->ops = &asus_hotplug_slot_ops;
556 asus_get_adapter_status(asus->hotplug_slot,
557 &asus->hotplug_slot->info->adapter_status);
Corentin Charyafa7c882011-02-06 13:28:28 +0100558
Corentin Charye12e6d92011-02-26 10:20:31 +0100559 ret = pci_hp_register(asus->hotplug_slot, bus, 0, "asus-wifi");
Corentin Charyafa7c882011-02-06 13:28:28 +0100560 if (ret) {
561 pr_err("Unable to register hotplug slot - %d\n", ret);
562 goto error_register;
563 }
564
565 return 0;
566
567error_register:
Corentin Charye12e6d92011-02-26 10:20:31 +0100568 kfree(asus->hotplug_slot->info);
Corentin Charyafa7c882011-02-06 13:28:28 +0100569error_info:
Corentin Charye12e6d92011-02-26 10:20:31 +0100570 kfree(asus->hotplug_slot);
571 asus->hotplug_slot = NULL;
Corentin Charyafa7c882011-02-06 13:28:28 +0100572error_slot:
Corentin Charye12e6d92011-02-26 10:20:31 +0100573 destroy_workqueue(asus->hotplug_workqueue);
Corentin Chary279f8f92011-02-06 13:28:29 +0100574error_workqueue:
Corentin Charyafa7c882011-02-06 13:28:28 +0100575 return ret;
576}
577
578/*
Corentin Charyba48fdb2010-11-29 08:14:07 +0100579 * Rfkill devices
580 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100581static int asus_rfkill_set(void *data, bool blocked)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100582{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100583 struct asus_rfkill *priv = data;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100584 u32 ctrl_param = !blocked;
585
Corentin Charyd33da3b2011-02-26 10:20:35 +0100586 return asus_wmi_set_devstate(priv->dev_id, ctrl_param, NULL);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100587}
588
Corentin Charye12e6d92011-02-26 10:20:31 +0100589static void asus_rfkill_query(struct rfkill *rfkill, void *data)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100590{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100591 struct asus_rfkill *priv = data;
Corentin Chary5c956382011-02-06 13:28:31 +0100592 int result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100593
Corentin Chary1d070f82011-02-26 10:20:36 +0100594 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100595
Corentin Chary5c956382011-02-06 13:28:31 +0100596 if (result < 0)
Corentin Charye12e6d92011-02-26 10:20:31 +0100597 return;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100598
Corentin Charya7ce3f02011-02-26 10:20:33 +0100599 rfkill_set_sw_state(priv->rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100600}
601
Corentin Charye12e6d92011-02-26 10:20:31 +0100602static int asus_rfkill_wlan_set(void *data, bool blocked)
Corentin Chary279f8f92011-02-06 13:28:29 +0100603{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100604 struct asus_rfkill *priv = data;
605 struct asus_wmi *asus = priv->asus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100606 int ret;
607
608 /*
609 * This handler is enabled only if hotplug is enabled.
Corentin Charye12e6d92011-02-26 10:20:31 +0100610 * In this case, the asus_wmi_set_devstate() will
Corentin Chary279f8f92011-02-06 13:28:29 +0100611 * trigger a wmi notification and we need to wait
612 * this call to finish before being able to call
613 * any wmi method
614 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100615 mutex_lock(&asus->wmi_lock);
Corentin Charya7ce3f02011-02-26 10:20:33 +0100616 ret = asus_rfkill_set(data, blocked);
Corentin Charye12e6d92011-02-26 10:20:31 +0100617 mutex_unlock(&asus->wmi_lock);
Corentin Chary279f8f92011-02-06 13:28:29 +0100618 return ret;
619}
620
Corentin Charye12e6d92011-02-26 10:20:31 +0100621static const struct rfkill_ops asus_rfkill_wlan_ops = {
622 .set_block = asus_rfkill_wlan_set,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100623 .query = asus_rfkill_query,
Corentin Chary279f8f92011-02-06 13:28:29 +0100624};
625
Corentin Charye12e6d92011-02-26 10:20:31 +0100626static const struct rfkill_ops asus_rfkill_ops = {
627 .set_block = asus_rfkill_set,
628 .query = asus_rfkill_query,
Corentin Charyba48fdb2010-11-29 08:14:07 +0100629};
630
Corentin Charye12e6d92011-02-26 10:20:31 +0100631static int asus_new_rfkill(struct asus_wmi *asus,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100632 struct asus_rfkill *arfkill,
Corentin Charye12e6d92011-02-26 10:20:31 +0100633 const char *name, enum rfkill_type type, int dev_id)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100634{
Corentin Chary1d070f82011-02-26 10:20:36 +0100635 int result = asus_wmi_get_devstate_simple(asus, dev_id);
Corentin Charya7ce3f02011-02-26 10:20:33 +0100636 struct rfkill **rfkill = &arfkill->rfkill;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100637
Corentin Chary5c956382011-02-06 13:28:31 +0100638 if (result < 0)
639 return result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100640
Corentin Charya7ce3f02011-02-26 10:20:33 +0100641 arfkill->dev_id = dev_id;
642 arfkill->asus = asus;
643
Corentin Charye12e6d92011-02-26 10:20:31 +0100644 if (dev_id == ASUS_WMI_DEVID_WLAN && asus->driver->hotplug_wireless)
645 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100646 &asus_rfkill_wlan_ops, arfkill);
Corentin Chary279f8f92011-02-06 13:28:29 +0100647 else
Corentin Charye12e6d92011-02-26 10:20:31 +0100648 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100649 &asus_rfkill_ops, arfkill);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100650
651 if (!*rfkill)
652 return -EINVAL;
653
Corentin Chary5c956382011-02-06 13:28:31 +0100654 rfkill_init_sw_state(*rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100655 result = rfkill_register(*rfkill);
656 if (result) {
657 rfkill_destroy(*rfkill);
658 *rfkill = NULL;
659 return result;
660 }
661 return 0;
662}
663
Corentin Charye12e6d92011-02-26 10:20:31 +0100664static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100665{
Corentin Charye12e6d92011-02-26 10:20:31 +0100666 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
667 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
668 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
Corentin Charya7ce3f02011-02-26 10:20:33 +0100669 if (asus->wlan.rfkill) {
670 rfkill_unregister(asus->wlan.rfkill);
671 rfkill_destroy(asus->wlan.rfkill);
672 asus->wlan.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100673 }
Corentin Charyafa7c882011-02-06 13:28:28 +0100674 /*
675 * Refresh pci hotplug in case the rfkill state was changed after
Corentin Charye12e6d92011-02-26 10:20:31 +0100676 * asus_unregister_rfkill_notifier()
Corentin Charyafa7c882011-02-06 13:28:28 +0100677 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100678 asus_rfkill_hotplug(asus);
679 if (asus->hotplug_slot)
680 pci_hp_deregister(asus->hotplug_slot);
681 if (asus->hotplug_workqueue)
682 destroy_workqueue(asus->hotplug_workqueue);
Corentin Charyafa7c882011-02-06 13:28:28 +0100683
Corentin Charya7ce3f02011-02-26 10:20:33 +0100684 if (asus->bluetooth.rfkill) {
685 rfkill_unregister(asus->bluetooth.rfkill);
686 rfkill_destroy(asus->bluetooth.rfkill);
687 asus->bluetooth.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100688 }
Corentin Charya7ce3f02011-02-26 10:20:33 +0100689 if (asus->wimax.rfkill) {
690 rfkill_unregister(asus->wimax.rfkill);
691 rfkill_destroy(asus->wimax.rfkill);
692 asus->wimax.rfkill = NULL;
Corentin Chary2e9e1592011-02-06 13:28:37 +0100693 }
Corentin Charya7ce3f02011-02-26 10:20:33 +0100694 if (asus->wwan3g.rfkill) {
695 rfkill_unregister(asus->wwan3g.rfkill);
696 rfkill_destroy(asus->wwan3g.rfkill);
697 asus->wwan3g.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100698 }
699}
700
Corentin Charye12e6d92011-02-26 10:20:31 +0100701static int asus_wmi_rfkill_init(struct asus_wmi *asus)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100702{
703 int result = 0;
704
Corentin Charye12e6d92011-02-26 10:20:31 +0100705 mutex_init(&asus->hotplug_lock);
706 mutex_init(&asus->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100707
Corentin Charya7ce3f02011-02-26 10:20:33 +0100708 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
709 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100710
711 if (result && result != -ENODEV)
712 goto exit;
713
Corentin Charya7ce3f02011-02-26 10:20:33 +0100714 result = asus_new_rfkill(asus, &asus->bluetooth,
Corentin Charye12e6d92011-02-26 10:20:31 +0100715 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
716 ASUS_WMI_DEVID_BLUETOOTH);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100717
718 if (result && result != -ENODEV)
719 goto exit;
720
Corentin Charya7ce3f02011-02-26 10:20:33 +0100721 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
722 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
Corentin Chary2e9e1592011-02-06 13:28:37 +0100723
724 if (result && result != -ENODEV)
725 goto exit;
726
Corentin Charya7ce3f02011-02-26 10:20:33 +0100727 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
728 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100729
730 if (result && result != -ENODEV)
731 goto exit;
732
Corentin Charye12e6d92011-02-26 10:20:31 +0100733 if (!asus->driver->hotplug_wireless)
Corentin Charyc14d4b82011-02-06 13:28:40 +0100734 goto exit;
735
Corentin Charye12e6d92011-02-26 10:20:31 +0100736 result = asus_setup_pci_hotplug(asus);
Corentin Charyafa7c882011-02-06 13:28:28 +0100737 /*
738 * If we get -EBUSY then something else is handling the PCI hotplug -
739 * don't fail in this case
740 */
741 if (result == -EBUSY)
742 result = 0;
743
Corentin Charye12e6d92011-02-26 10:20:31 +0100744 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
745 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
746 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
Corentin Charyafa7c882011-02-06 13:28:28 +0100747 /*
748 * Refresh pci hotplug in case the rfkill state was changed during
749 * setup.
750 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100751 asus_rfkill_hotplug(asus);
Corentin Charyafa7c882011-02-06 13:28:28 +0100752
Corentin Charyba48fdb2010-11-29 08:14:07 +0100753exit:
754 if (result && result != -ENODEV)
Corentin Charye12e6d92011-02-26 10:20:31 +0100755 asus_wmi_rfkill_exit(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100756
757 if (result == -ENODEV)
758 result = 0;
759
760 return result;
761}
762
763/*
Corentin Chary084fca62010-11-29 08:14:06 +0100764 * Backlight
765 */
Corentin Chary1d070f82011-02-26 10:20:36 +0100766static int read_backlight_power(struct asus_wmi *asus)
Corentin Charyb7187262011-02-06 13:28:39 +0100767{
Corentin Chary1d070f82011-02-26 10:20:36 +0100768 int ret = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_BACKLIGHT);
Corentin Charyb7187262011-02-06 13:28:39 +0100769
770 if (ret < 0)
771 return ret;
772
773 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
774}
775
Corentin Chary8fbea012011-02-26 10:20:37 +0100776static int read_brightness_max(struct asus_wmi *asus)
777{
778 u32 retval;
779 int err;
780
781 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
782
783 if (err < 0)
784 return err;
785
786 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
787 retval >>= 8;
788
789 if (!retval)
790 return -ENODEV;
791
792 return retval;
793}
794
Yong Wang3d7b1652010-04-11 09:27:54 +0800795static int read_brightness(struct backlight_device *bd)
796{
Corentin Chary1d070f82011-02-26 10:20:36 +0100797 struct asus_wmi *asus = bl_get_data(bd);
Corentin Chary8fbea012011-02-26 10:20:37 +0100798 u32 retval, err;
Yong Wang3d7b1652010-04-11 09:27:54 +0800799
Corentin Chary1d070f82011-02-26 10:20:36 +0100800 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
Yong Wang3d7b1652010-04-11 09:27:54 +0800801
Corentin Charyd33da3b2011-02-26 10:20:35 +0100802 if (err < 0)
803 return err;
804
805 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
Yong Wang3d7b1652010-04-11 09:27:54 +0800806}
807
808static int update_bl_status(struct backlight_device *bd)
809{
Corentin Chary1d070f82011-02-26 10:20:36 +0100810 struct asus_wmi *asus = bl_get_data(bd);
Corentin Charydfed65d2010-11-29 08:14:12 +0100811 u32 ctrl_param;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100812 int power, err;
Yong Wang3d7b1652010-04-11 09:27:54 +0800813
814 ctrl_param = bd->props.brightness;
815
Corentin Charyd33da3b2011-02-26 10:20:35 +0100816 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
817 ctrl_param, NULL);
Yong Wang3d7b1652010-04-11 09:27:54 +0800818
Corentin Charyd33da3b2011-02-26 10:20:35 +0100819 if (err < 0)
820 return err;
Corentin Charyb7187262011-02-06 13:28:39 +0100821
Corentin Chary1d070f82011-02-26 10:20:36 +0100822 power = read_backlight_power(asus);
Corentin Charyb7187262011-02-06 13:28:39 +0100823 if (power != -ENODEV && bd->props.power != power) {
824 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
Corentin Charyd33da3b2011-02-26 10:20:35 +0100825 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
826 ctrl_param, NULL);
Corentin Charyb7187262011-02-06 13:28:39 +0100827 }
Corentin Chary8fbea012011-02-26 10:20:37 +0100828 return err;
Yong Wang3d7b1652010-04-11 09:27:54 +0800829}
830
Corentin Charye12e6d92011-02-26 10:20:31 +0100831static const struct backlight_ops asus_wmi_bl_ops = {
Yong Wang3d7b1652010-04-11 09:27:54 +0800832 .get_brightness = read_brightness,
833 .update_status = update_bl_status,
834};
835
Corentin Charye12e6d92011-02-26 10:20:31 +0100836static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
Yong Wang3d7b1652010-04-11 09:27:54 +0800837{
Corentin Charye12e6d92011-02-26 10:20:31 +0100838 struct backlight_device *bd = asus->backlight_device;
Yong Wang3d7b1652010-04-11 09:27:54 +0800839 int old = bd->props.brightness;
Daniel Mackb7670ed2010-05-19 12:37:01 +0200840 int new = old;
Yong Wang3d7b1652010-04-11 09:27:54 +0800841
842 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
843 new = code - NOTIFY_BRNUP_MIN + 1;
844 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
845 new = code - NOTIFY_BRNDOWN_MIN;
846
847 bd->props.brightness = new;
848 backlight_update_status(bd);
849 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
850
851 return old;
852}
853
Corentin Charye12e6d92011-02-26 10:20:31 +0100854static int asus_wmi_backlight_init(struct asus_wmi *asus)
Yong Wang3d7b1652010-04-11 09:27:54 +0800855{
856 struct backlight_device *bd;
857 struct backlight_properties props;
Corentin Charyb7187262011-02-06 13:28:39 +0100858 int max;
859 int power;
860
Corentin Chary8fbea012011-02-26 10:20:37 +0100861 max = read_brightness_max(asus);
Corentin Charyb7187262011-02-06 13:28:39 +0100862
Corentin Charyb7187262011-02-06 13:28:39 +0100863 if (max == -ENODEV)
864 max = 0;
Corentin Chary8fbea012011-02-26 10:20:37 +0100865 else if (max < 0)
866 return max;
867
868 power = read_backlight_power(asus);
869
Corentin Charyb7187262011-02-06 13:28:39 +0100870 if (power == -ENODEV)
871 power = FB_BLANK_UNBLANK;
Corentin Chary8fbea012011-02-26 10:20:37 +0100872 else if (power < 0)
873 return power;
Yong Wang3d7b1652010-04-11 09:27:54 +0800874
875 memset(&props, 0, sizeof(struct backlight_properties));
Corentin Charyb7187262011-02-06 13:28:39 +0100876 props.max_brightness = max;
Corentin Charye12e6d92011-02-26 10:20:31 +0100877 bd = backlight_device_register(asus->driver->name,
878 &asus->platform_device->dev, asus,
879 &asus_wmi_bl_ops, &props);
Yong Wang3d7b1652010-04-11 09:27:54 +0800880 if (IS_ERR(bd)) {
881 pr_err("Could not register backlight device\n");
882 return PTR_ERR(bd);
883 }
884
Corentin Charye12e6d92011-02-26 10:20:31 +0100885 asus->backlight_device = bd;
Yong Wang3d7b1652010-04-11 09:27:54 +0800886
887 bd->props.brightness = read_brightness(bd);
Corentin Charyb7187262011-02-06 13:28:39 +0100888 bd->props.power = power;
Yong Wang3d7b1652010-04-11 09:27:54 +0800889 backlight_update_status(bd);
890
891 return 0;
892}
893
Corentin Charye12e6d92011-02-26 10:20:31 +0100894static void asus_wmi_backlight_exit(struct asus_wmi *asus)
Yong Wang3d7b1652010-04-11 09:27:54 +0800895{
Corentin Charye12e6d92011-02-26 10:20:31 +0100896 if (asus->backlight_device)
897 backlight_device_unregister(asus->backlight_device);
Yong Wang3d7b1652010-04-11 09:27:54 +0800898
Corentin Charye12e6d92011-02-26 10:20:31 +0100899 asus->backlight_device = NULL;
Yong Wang3d7b1652010-04-11 09:27:54 +0800900}
901
Corentin Charye12e6d92011-02-26 10:20:31 +0100902static void asus_wmi_notify(u32 value, void *context)
Yong Wang3d7b1652010-04-11 09:27:54 +0800903{
Corentin Charye12e6d92011-02-26 10:20:31 +0100904 struct asus_wmi *asus = context;
Yong Wang3d7b1652010-04-11 09:27:54 +0800905 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
906 union acpi_object *obj;
907 acpi_status status;
908 int code;
909 int orig_code;
910
911 status = wmi_get_event_data(value, &response);
912 if (status != AE_OK) {
913 pr_err("bad event status 0x%x\n", status);
914 return;
915 }
916
917 obj = (union acpi_object *)response.pointer;
918
Corentin Chary57ab7da2011-02-26 10:20:32 +0100919 if (!obj || obj->type != ACPI_TYPE_INTEGER)
920 goto exit;
Yong Wang3d7b1652010-04-11 09:27:54 +0800921
Corentin Chary57ab7da2011-02-26 10:20:32 +0100922 code = obj->integer.value;
923 orig_code = code;
Yong Wang3d7b1652010-04-11 09:27:54 +0800924
Corentin Chary57ab7da2011-02-26 10:20:32 +0100925 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
926 code = NOTIFY_BRNUP_MIN;
927 else if (code >= NOTIFY_BRNDOWN_MIN &&
928 code <= NOTIFY_BRNDOWN_MAX)
929 code = NOTIFY_BRNDOWN_MIN;
Yong Wang3d7b1652010-04-11 09:27:54 +0800930
Corentin Chary57ab7da2011-02-26 10:20:32 +0100931 if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
932 if (!acpi_video_backlight_support())
933 asus_wmi_backlight_notify(asus, orig_code);
934 } else if (!sparse_keymap_report_event(asus->inputdev, code, 1, true))
935 pr_info("Unknown key %x pressed\n", code);
Yong Wang3d7b1652010-04-11 09:27:54 +0800936
Corentin Chary57ab7da2011-02-26 10:20:32 +0100937exit:
Yong Wang3d7b1652010-04-11 09:27:54 +0800938 kfree(obj);
939}
940
Corentin Chary9e1565b2011-02-06 13:28:36 +0100941/*
942 * Sys helpers
943 */
944static int parse_arg(const char *buf, unsigned long count, int *val)
945{
946 if (!count)
947 return 0;
948 if (sscanf(buf, "%i", val) != 1)
949 return -EINVAL;
950 return count;
951}
952
Corentin Chary1d070f82011-02-26 10:20:36 +0100953static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
954 const char *buf, size_t count)
Corentin Chary9e1565b2011-02-06 13:28:36 +0100955{
Corentin Chary9e1565b2011-02-06 13:28:36 +0100956 u32 retval;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100957 int rv, err, value;
Corentin Chary9e1565b2011-02-06 13:28:36 +0100958
Corentin Chary1d070f82011-02-26 10:20:36 +0100959 value = asus_wmi_get_devstate_simple(asus, devid);
Corentin Charye12e6d92011-02-26 10:20:31 +0100960 if (value == -ENODEV) /* Check device presence */
Corentin Chary9e1565b2011-02-06 13:28:36 +0100961 return value;
962
963 rv = parse_arg(buf, count, &value);
Corentin Charyd33da3b2011-02-26 10:20:35 +0100964 err = asus_wmi_set_devstate(devid, value, &retval);
Corentin Chary9e1565b2011-02-06 13:28:36 +0100965
Corentin Charyd33da3b2011-02-26 10:20:35 +0100966 if (err < 0)
967 return err;
968
Corentin Chary9e1565b2011-02-06 13:28:36 +0100969 return rv;
970}
971
Corentin Chary1d070f82011-02-26 10:20:36 +0100972static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
Corentin Chary9e1565b2011-02-06 13:28:36 +0100973{
Corentin Chary1d070f82011-02-26 10:20:36 +0100974 int value = asus_wmi_get_devstate_simple(asus, devid);
Corentin Chary9e1565b2011-02-06 13:28:36 +0100975
976 if (value < 0)
977 return value;
978
979 return sprintf(buf, "%d\n", value);
980}
981
Corentin Charye12e6d92011-02-26 10:20:31 +0100982#define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
Corentin Chary9e1565b2011-02-06 13:28:36 +0100983 static ssize_t show_##_name(struct device *dev, \
984 struct device_attribute *attr, \
985 char *buf) \
986 { \
Corentin Chary1d070f82011-02-26 10:20:36 +0100987 struct asus_wmi *asus = dev_get_drvdata(dev); \
988 \
989 return show_sys_wmi(asus, _cm, buf); \
Corentin Chary9e1565b2011-02-06 13:28:36 +0100990 } \
991 static ssize_t store_##_name(struct device *dev, \
992 struct device_attribute *attr, \
993 const char *buf, size_t count) \
994 { \
Corentin Chary1d070f82011-02-26 10:20:36 +0100995 struct asus_wmi *asus = dev_get_drvdata(dev); \
996 \
997 return store_sys_wmi(asus, _cm, buf, count); \
Corentin Chary9e1565b2011-02-06 13:28:36 +0100998 } \
999 static struct device_attribute dev_attr_##_name = { \
1000 .attr = { \
1001 .name = __stringify(_name), \
1002 .mode = _mode }, \
1003 .show = show_##_name, \
1004 .store = store_##_name, \
1005 }
1006
Corentin Charye12e6d92011-02-26 10:20:31 +01001007ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
1008ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
1009ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
Corentin Chary9e1565b2011-02-06 13:28:36 +01001010
Dmitry Torokhov67fa38e2010-11-03 11:14:01 -07001011static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
1012 const char *buf, size_t count)
Chris Bagwell7f80d732010-10-11 18:47:18 -05001013{
1014 int value;
Chris Bagwell7f80d732010-10-11 18:47:18 -05001015
1016 if (!count || sscanf(buf, "%i", &value) != 1)
1017 return -EINVAL;
1018 if (value < 0 || value > 2)
1019 return -EINVAL;
1020
Corentin Charyd33da3b2011-02-26 10:20:35 +01001021 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001022}
1023
1024static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
1025
Corentin Chary4e37b422010-11-29 08:14:08 +01001026static struct attribute *platform_attributes[] = {
1027 &dev_attr_cpufv.attr,
Corentin Chary9e1565b2011-02-06 13:28:36 +01001028 &dev_attr_camera.attr,
1029 &dev_attr_cardr.attr,
Corentin Chary4615bb62011-02-06 13:28:42 +01001030 &dev_attr_touchpad.attr,
Corentin Chary4e37b422010-11-29 08:14:08 +01001031 NULL
1032};
1033
Corentin Charye12e6d92011-02-26 10:20:31 +01001034static mode_t asus_sysfs_is_visible(struct kobject *kobj,
1035 struct attribute *attr, int idx)
Corentin Chary9e1565b2011-02-06 13:28:36 +01001036{
Corentin Chary1d070f82011-02-26 10:20:36 +01001037 struct device *dev = container_of(kobj, struct device, kobj);
1038 struct platform_device *pdev = to_platform_device(dev);
1039 struct asus_wmi *asus = platform_get_drvdata(pdev);
1040 bool ok = true;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001041 int devid = -1;
1042
1043 if (attr == &dev_attr_camera.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01001044 devid = ASUS_WMI_DEVID_CAMERA;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001045 else if (attr == &dev_attr_cardr.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01001046 devid = ASUS_WMI_DEVID_CARDREADER;
Corentin Chary4615bb62011-02-06 13:28:42 +01001047 else if (attr == &dev_attr_touchpad.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01001048 devid = ASUS_WMI_DEVID_TOUCHPAD;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001049
1050 if (devid != -1)
Corentin Chary1d070f82011-02-26 10:20:36 +01001051 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
Corentin Chary9e1565b2011-02-06 13:28:36 +01001052
Corentin Chary1d070f82011-02-26 10:20:36 +01001053 return ok ? attr->mode : 0;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001054}
1055
Corentin Chary4e37b422010-11-29 08:14:08 +01001056static struct attribute_group platform_attribute_group = {
Corentin Charye12e6d92011-02-26 10:20:31 +01001057 .is_visible = asus_sysfs_is_visible,
1058 .attrs = platform_attributes
Corentin Chary4e37b422010-11-29 08:14:08 +01001059};
1060
Corentin Charye12e6d92011-02-26 10:20:31 +01001061static void asus_wmi_sysfs_exit(struct platform_device *device)
Chris Bagwell7f80d732010-10-11 18:47:18 -05001062{
Corentin Chary4e37b422010-11-29 08:14:08 +01001063 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001064}
1065
Corentin Charye12e6d92011-02-26 10:20:31 +01001066static int asus_wmi_sysfs_init(struct platform_device *device)
Chris Bagwell7f80d732010-10-11 18:47:18 -05001067{
Corentin Chary4e37b422010-11-29 08:14:08 +01001068 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001069}
1070
Corentin Chary27c136c2010-11-29 08:14:05 +01001071/*
1072 * Platform device
1073 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001074static int __init asus_wmi_platform_init(struct asus_wmi *asus)
Corentin Chary27c136c2010-11-29 08:14:05 +01001075{
Corentin Chary46dbca82011-02-26 10:20:38 +01001076 int rv;
1077
1078 /* INIT enable hotkeys on some models */
1079 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
1080 pr_info("Initialization: %#x", rv);
1081
1082 /* We don't know yet what to do with this version... */
1083 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
1084 pr_info("BIOS WMI version: %d.%d", rv >> 8, rv & 0xFF);
1085 asus->spec = rv;
1086 }
1087
1088 /*
1089 * The SFUN method probably allows the original driver to get the list
1090 * of features supported by a given model. For now, 0x0100 or 0x0800
1091 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
1092 * The significance of others is yet to be found.
1093 */
1094 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
1095 pr_info("SFUN value: %#x", rv);
1096 asus->sfun = rv;
1097 }
1098
Corentin Chary1d070f82011-02-26 10:20:36 +01001099 /*
1100 * Eee PC and Notebooks seems to have different method_id for DSTS,
1101 * but it may also be related to the BIOS's SPEC.
1102 * Note, on most Eeepc, there is no way to check if a method exist
1103 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
1104 * but once again, SPEC may probably be used for that kind of things.
1105 */
1106 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS, 0, 0, NULL))
1107 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
1108 else if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS2, 0, 0, NULL))
1109 asus->dsts_id = ASUS_WMI_METHODID_DSTS2;
1110
1111 if (!asus->dsts_id) {
1112 pr_err("Can't find DSTS");
1113 return -ENODEV;
1114 }
1115
Corentin Charye12e6d92011-02-26 10:20:31 +01001116 return asus_wmi_sysfs_init(asus->platform_device);
Corentin Chary27c136c2010-11-29 08:14:05 +01001117}
1118
Corentin Charye12e6d92011-02-26 10:20:31 +01001119static void asus_wmi_platform_exit(struct asus_wmi *asus)
Corentin Chary27c136c2010-11-29 08:14:05 +01001120{
Corentin Charye12e6d92011-02-26 10:20:31 +01001121 asus_wmi_sysfs_exit(asus->platform_device);
Corentin Chary27c136c2010-11-29 08:14:05 +01001122}
1123
1124/*
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001125 * debugfs
1126 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001127struct asus_wmi_debugfs_node {
1128 struct asus_wmi *asus;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001129 char *name;
Corentin Charye12e6d92011-02-26 10:20:31 +01001130 int (*show) (struct seq_file *m, void *data);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001131};
1132
1133static int show_dsts(struct seq_file *m, void *data)
1134{
Corentin Charye12e6d92011-02-26 10:20:31 +01001135 struct asus_wmi *asus = m->private;
Corentin Charyd33da3b2011-02-26 10:20:35 +01001136 int err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001137 u32 retval = -1;
1138
Corentin Chary1d070f82011-02-26 10:20:36 +01001139 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001140
Corentin Charyd33da3b2011-02-26 10:20:35 +01001141 if (err < 0)
1142 return err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001143
Corentin Charyef343492011-02-26 10:20:39 +01001144 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001145
1146 return 0;
1147}
1148
1149static int show_devs(struct seq_file *m, void *data)
1150{
Corentin Charye12e6d92011-02-26 10:20:31 +01001151 struct asus_wmi *asus = m->private;
Corentin Charyd33da3b2011-02-26 10:20:35 +01001152 int err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001153 u32 retval = -1;
1154
Corentin Charyd33da3b2011-02-26 10:20:35 +01001155 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
1156 &retval);
1157
1158 if (err < 0)
1159 return err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001160
Corentin Charyef343492011-02-26 10:20:39 +01001161 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
Corentin Charye12e6d92011-02-26 10:20:31 +01001162 asus->debug.ctrl_param, retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001163
1164 return 0;
1165}
1166
Corentin Charyef343492011-02-26 10:20:39 +01001167static int show_call(struct seq_file *m, void *data)
1168{
1169 struct asus_wmi *asus = m->private;
1170 struct bios_args args = {
1171 .arg0 = asus->debug.dev_id,
1172 .arg1 = asus->debug.ctrl_param,
1173 };
1174 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1175 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
1176 union acpi_object *obj;
1177 acpi_status status;
1178
1179 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
1180 1, asus->debug.method_id,
1181 &input, &output);
1182
1183 if (ACPI_FAILURE(status))
1184 return -EIO;
1185
1186 obj = (union acpi_object *)output.pointer;
1187 if (obj && obj->type == ACPI_TYPE_INTEGER)
1188 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
1189 asus->debug.dev_id, asus->debug.ctrl_param,
1190 (u32) obj->integer.value);
1191 else
1192 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
1193 asus->debug.dev_id, asus->debug.ctrl_param,
1194 obj->type);
1195
1196 kfree(obj);
1197
1198 return 0;
1199}
1200
Corentin Charye12e6d92011-02-26 10:20:31 +01001201static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
1202 {NULL, "devs", show_devs},
1203 {NULL, "dsts", show_dsts},
Corentin Charyef343492011-02-26 10:20:39 +01001204 {NULL, "call", show_call},
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001205};
1206
Corentin Charye12e6d92011-02-26 10:20:31 +01001207static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001208{
Corentin Charye12e6d92011-02-26 10:20:31 +01001209 struct asus_wmi_debugfs_node *node = inode->i_private;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001210
Corentin Charye12e6d92011-02-26 10:20:31 +01001211 return single_open(file, node->show, node->asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001212}
1213
Corentin Charye12e6d92011-02-26 10:20:31 +01001214static const struct file_operations asus_wmi_debugfs_io_ops = {
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001215 .owner = THIS_MODULE,
Corentin Charye12e6d92011-02-26 10:20:31 +01001216 .open = asus_wmi_debugfs_open,
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001217 .read = seq_read,
1218 .llseek = seq_lseek,
1219 .release = single_release,
1220};
1221
Corentin Charye12e6d92011-02-26 10:20:31 +01001222static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001223{
Corentin Charye12e6d92011-02-26 10:20:31 +01001224 debugfs_remove_recursive(asus->debug.root);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001225}
1226
Corentin Charye12e6d92011-02-26 10:20:31 +01001227static int asus_wmi_debugfs_init(struct asus_wmi *asus)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001228{
1229 struct dentry *dent;
1230 int i;
1231
Corentin Charye12e6d92011-02-26 10:20:31 +01001232 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
1233 if (!asus->debug.root) {
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001234 pr_err("failed to create debugfs directory");
1235 goto error_debugfs;
1236 }
1237
Corentin Charyef343492011-02-26 10:20:39 +01001238 dent = debugfs_create_x32("method_id", S_IRUGO | S_IWUSR,
1239 asus->debug.root, &asus->debug.method_id);
1240 if (!dent)
1241 goto error_debugfs;
1242
Corentin Charye12e6d92011-02-26 10:20:31 +01001243 dent = debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR,
1244 asus->debug.root, &asus->debug.dev_id);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001245 if (!dent)
1246 goto error_debugfs;
1247
Corentin Charye12e6d92011-02-26 10:20:31 +01001248 dent = debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR,
1249 asus->debug.root, &asus->debug.ctrl_param);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001250 if (!dent)
1251 goto error_debugfs;
1252
Corentin Charye12e6d92011-02-26 10:20:31 +01001253 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
1254 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001255
Corentin Charye12e6d92011-02-26 10:20:31 +01001256 node->asus = asus;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001257 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
Corentin Charye12e6d92011-02-26 10:20:31 +01001258 asus->debug.root, node,
1259 &asus_wmi_debugfs_io_ops);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001260 if (!dent) {
1261 pr_err("failed to create debug file: %s\n", node->name);
1262 goto error_debugfs;
1263 }
1264 }
1265
1266 return 0;
1267
1268error_debugfs:
Corentin Charye12e6d92011-02-26 10:20:31 +01001269 asus_wmi_debugfs_exit(asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001270 return -ENOMEM;
1271}
1272
1273/*
Corentin Chary27c136c2010-11-29 08:14:05 +01001274 * WMI Driver
1275 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001276static int asus_wmi_add(struct platform_device *pdev)
Corentin Charyafa7c882011-02-06 13:28:28 +01001277{
Corentin Charye12e6d92011-02-26 10:20:31 +01001278 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
1279 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
1280 struct asus_wmi *asus;
Yong Wangee027e42010-03-21 10:26:34 +08001281 acpi_status status;
Corentin Chary27c136c2010-11-29 08:14:05 +01001282 int err;
Yong Wangee027e42010-03-21 10:26:34 +08001283
Corentin Charye12e6d92011-02-26 10:20:31 +01001284 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
1285 if (!asus)
Corentin Charya04ce292011-02-06 13:28:33 +01001286 return -ENOMEM;
1287
Corentin Charye12e6d92011-02-26 10:20:31 +01001288 asus->driver = wdrv;
1289 asus->platform_device = pdev;
1290 wdrv->platform_device = pdev;
1291 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001292
Corentin Charye12e6d92011-02-26 10:20:31 +01001293 if (wdrv->quirks)
1294 wdrv->quirks(asus->driver);
Corentin Charyafa7c882011-02-06 13:28:28 +01001295
Corentin Charye12e6d92011-02-26 10:20:31 +01001296 err = asus_wmi_platform_init(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001297 if (err)
1298 goto fail_platform;
Yong Wang45f2c692010-04-11 09:27:19 +08001299
Corentin Charye12e6d92011-02-26 10:20:31 +01001300 err = asus_wmi_input_init(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08001301 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +01001302 goto fail_input;
Yong Wang3d7b1652010-04-11 09:27:54 +08001303
Corentin Charye12e6d92011-02-26 10:20:31 +01001304 err = asus_wmi_led_init(asus);
Corentin Chary084fca62010-11-29 08:14:06 +01001305 if (err)
1306 goto fail_leds;
1307
Corentin Charye12e6d92011-02-26 10:20:31 +01001308 err = asus_wmi_rfkill_init(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001309 if (err)
1310 goto fail_rfkill;
1311
Yong Wang3d7b1652010-04-11 09:27:54 +08001312 if (!acpi_video_backlight_support()) {
Corentin Charye12e6d92011-02-26 10:20:31 +01001313 err = asus_wmi_backlight_init(asus);
Corentin Charyb7187262011-02-06 13:28:39 +01001314 if (err && err != -ENODEV)
Corentin Chary27c136c2010-11-29 08:14:05 +01001315 goto fail_backlight;
Yong Wang3d7b1652010-04-11 09:27:54 +08001316 } else
1317 pr_info("Backlight controlled by ACPI video driver\n");
Yong Wang45f2c692010-04-11 09:27:19 +08001318
Corentin Charye12e6d92011-02-26 10:20:31 +01001319 status = wmi_install_notify_handler(asus->driver->event_guid,
1320 asus_wmi_notify, asus);
Yong Wang45f2c692010-04-11 09:27:19 +08001321 if (ACPI_FAILURE(status)) {
Corentin Charye12e6d92011-02-26 10:20:31 +01001322 pr_err("Unable to register notify handler - %d\n", status);
Yong Wang45f2c692010-04-11 09:27:19 +08001323 err = -ENODEV;
Corentin Chary27c136c2010-11-29 08:14:05 +01001324 goto fail_wmi_handler;
Yong Wang45f2c692010-04-11 09:27:19 +08001325 }
1326
Corentin Charye12e6d92011-02-26 10:20:31 +01001327 err = asus_wmi_debugfs_init(asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001328 if (err)
1329 goto fail_debugfs;
1330
Corentin Charya04ce292011-02-06 13:28:33 +01001331 return 0;
Yong Wang45f2c692010-04-11 09:27:19 +08001332
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001333fail_debugfs:
Corentin Charye12e6d92011-02-26 10:20:31 +01001334 wmi_remove_notify_handler(asus->driver->event_guid);
Corentin Chary27c136c2010-11-29 08:14:05 +01001335fail_wmi_handler:
Corentin Charye12e6d92011-02-26 10:20:31 +01001336 asus_wmi_backlight_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001337fail_backlight:
Corentin Charye12e6d92011-02-26 10:20:31 +01001338 asus_wmi_rfkill_exit(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001339fail_rfkill:
Corentin Charye12e6d92011-02-26 10:20:31 +01001340 asus_wmi_led_exit(asus);
Corentin Chary084fca62010-11-29 08:14:06 +01001341fail_leds:
Corentin Charye12e6d92011-02-26 10:20:31 +01001342 asus_wmi_input_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001343fail_input:
Corentin Charye12e6d92011-02-26 10:20:31 +01001344 asus_wmi_platform_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01001345fail_platform:
Corentin Charye12e6d92011-02-26 10:20:31 +01001346 kfree(asus);
Corentin Charya04ce292011-02-06 13:28:33 +01001347 return err;
Yong Wang45f2c692010-04-11 09:27:19 +08001348}
1349
Corentin Charye12e6d92011-02-26 10:20:31 +01001350static int asus_wmi_remove(struct platform_device *device)
Yong Wang45f2c692010-04-11 09:27:19 +08001351{
Corentin Charye12e6d92011-02-26 10:20:31 +01001352 struct asus_wmi *asus;
Yong Wang45f2c692010-04-11 09:27:19 +08001353
Corentin Charye12e6d92011-02-26 10:20:31 +01001354 asus = platform_get_drvdata(device);
1355 wmi_remove_notify_handler(asus->driver->event_guid);
1356 asus_wmi_backlight_exit(asus);
1357 asus_wmi_input_exit(asus);
1358 asus_wmi_led_exit(asus);
1359 asus_wmi_rfkill_exit(asus);
1360 asus_wmi_debugfs_exit(asus);
1361 asus_wmi_platform_exit(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08001362
Corentin Charye12e6d92011-02-26 10:20:31 +01001363 kfree(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08001364 return 0;
1365}
1366
Corentin Chary0773d7f2011-02-06 13:28:32 +01001367/*
1368 * Platform driver - hibernate/resume callbacks
1369 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001370static int asus_hotk_thaw(struct device *device)
Corentin Chary0773d7f2011-02-06 13:28:32 +01001371{
Corentin Charye12e6d92011-02-26 10:20:31 +01001372 struct asus_wmi *asus = dev_get_drvdata(device);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001373
Corentin Charya7ce3f02011-02-26 10:20:33 +01001374 if (asus->wlan.rfkill) {
Corentin Chary0773d7f2011-02-06 13:28:32 +01001375 bool wlan;
1376
1377 /*
1378 * Work around bios bug - acpi _PTS turns off the wireless led
1379 * during suspend. Normally it restores it on resume, but
1380 * we should kick it ourselves in case hibernation is aborted.
1381 */
Corentin Chary1d070f82011-02-26 10:20:36 +01001382 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charye12e6d92011-02-26 10:20:31 +01001383 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001384 }
1385
1386 return 0;
1387}
1388
Corentin Charye12e6d92011-02-26 10:20:31 +01001389static int asus_hotk_restore(struct device *device)
Corentin Chary0773d7f2011-02-06 13:28:32 +01001390{
Corentin Charye12e6d92011-02-26 10:20:31 +01001391 struct asus_wmi *asus = dev_get_drvdata(device);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001392 int bl;
1393
1394 /* Refresh both wlan rfkill state and pci hotplug */
Corentin Charya7ce3f02011-02-26 10:20:33 +01001395 if (asus->wlan.rfkill)
Corentin Charye12e6d92011-02-26 10:20:31 +01001396 asus_rfkill_hotplug(asus);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001397
Corentin Charya7ce3f02011-02-26 10:20:33 +01001398 if (asus->bluetooth.rfkill) {
Corentin Chary1d070f82011-02-26 10:20:36 +01001399 bl = !asus_wmi_get_devstate_simple(asus,
1400 ASUS_WMI_DEVID_BLUETOOTH);
Corentin Charya7ce3f02011-02-26 10:20:33 +01001401 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01001402 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01001403 if (asus->wimax.rfkill) {
Corentin Chary1d070f82011-02-26 10:20:36 +01001404 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
Corentin Charya7ce3f02011-02-26 10:20:33 +01001405 rfkill_set_sw_state(asus->wimax.rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01001406 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01001407 if (asus->wwan3g.rfkill) {
Corentin Chary1d070f82011-02-26 10:20:36 +01001408 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
Corentin Charya7ce3f02011-02-26 10:20:33 +01001409 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
Corentin Chary0773d7f2011-02-06 13:28:32 +01001410 }
1411
1412 return 0;
1413}
1414
Corentin Charye12e6d92011-02-26 10:20:31 +01001415static const struct dev_pm_ops asus_pm_ops = {
1416 .thaw = asus_hotk_thaw,
1417 .restore = asus_hotk_restore,
Corentin Chary0773d7f2011-02-06 13:28:32 +01001418};
1419
Corentin Charye12e6d92011-02-26 10:20:31 +01001420static int asus_wmi_probe(struct platform_device *pdev)
Corentin Charyd358cb52010-11-29 08:14:14 +01001421{
Corentin Charye12e6d92011-02-26 10:20:31 +01001422 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
1423 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
1424 int ret;
Corentin Charyd358cb52010-11-29 08:14:14 +01001425
Corentin Charye12e6d92011-02-26 10:20:31 +01001426 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
1427 pr_warning("Management GUID not found\n");
Yong Wangee027e42010-03-21 10:26:34 +08001428 return -ENODEV;
1429 }
1430
Corentin Charye12e6d92011-02-26 10:20:31 +01001431 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
1432 pr_warning("Event GUID not found\n");
Corentin Charyd358cb52010-11-29 08:14:14 +01001433 return -ENODEV;
1434 }
1435
Corentin Charye12e6d92011-02-26 10:20:31 +01001436 if (wdrv->probe) {
1437 ret = wdrv->probe(pdev);
1438 if (ret)
1439 return ret;
1440 }
1441
1442 return asus_wmi_add(pdev);
Corentin Charya04ce292011-02-06 13:28:33 +01001443}
Yong Wangee027e42010-03-21 10:26:34 +08001444
Corentin Charye12e6d92011-02-26 10:20:31 +01001445static bool used;
Yong Wangee027e42010-03-21 10:26:34 +08001446
Corentin Charye12e6d92011-02-26 10:20:31 +01001447int asus_wmi_register_driver(struct asus_wmi_driver *driver)
Corentin Charya04ce292011-02-06 13:28:33 +01001448{
Corentin Charye12e6d92011-02-26 10:20:31 +01001449 struct platform_driver *platform_driver;
1450 struct platform_device *platform_device;
1451
1452 if (used)
1453 return -EBUSY;
1454
1455 platform_driver = &driver->platform_driver;
1456 platform_driver->remove = asus_wmi_remove;
1457 platform_driver->driver.owner = driver->owner;
1458 platform_driver->driver.name = driver->name;
1459 platform_driver->driver.pm = &asus_pm_ops;
1460
1461 platform_device = platform_create_bundle(platform_driver,
1462 asus_wmi_probe,
Corentin Charya04ce292011-02-06 13:28:33 +01001463 NULL, 0, NULL, 0);
1464 if (IS_ERR(platform_device))
1465 return PTR_ERR(platform_device);
Corentin Charye12e6d92011-02-26 10:20:31 +01001466
1467 used = true;
1468 return 0;
1469}
1470EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
1471
1472void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
1473{
1474 platform_device_unregister(driver->platform_device);
1475 platform_driver_unregister(&driver->platform_driver);
1476 used = false;
1477}
1478EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
1479
1480static int __init asus_wmi_init(void)
1481{
1482 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
1483 pr_info("Asus Management GUID not found");
1484 return -ENODEV;
1485 }
1486
1487 pr_info("ASUS WMI generic driver loaded");
Yong Wangee027e42010-03-21 10:26:34 +08001488 return 0;
1489}
1490
Corentin Charye12e6d92011-02-26 10:20:31 +01001491static void __exit asus_wmi_exit(void)
Yong Wangee027e42010-03-21 10:26:34 +08001492{
Corentin Charye12e6d92011-02-26 10:20:31 +01001493 pr_info("ASUS WMI generic driver unloaded");
Yong Wangee027e42010-03-21 10:26:34 +08001494}
1495
Corentin Charye12e6d92011-02-26 10:20:31 +01001496module_init(asus_wmi_init);
1497module_exit(asus_wmi_exit);