blob: 75dd692d31ee27725e9e93a40c87c5913d13595c [file] [log] [blame]
Yong Wangee027e42010-03-21 10:26:34 +08001/*
2 * Eee PC WMI hotkey driver
3 *
4 * Copyright(C) 2010 Intel Corporation.
Corentin Chary4c4edfa2010-11-29 08:14:11 +01005 * Copyright(C) 2010 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>
Corentin Charyafa7c882011-02-06 13:28:28 +010045#include <linux/dmi.h>
Yong Wangee027e42010-03-21 10:26:34 +080046#include <acpi/acpi_bus.h>
47#include <acpi/acpi_drivers.h>
48
Yong Wang45f2c692010-04-11 09:27:19 +080049#define EEEPC_WMI_FILE "eeepc-wmi"
50
Yong Wangee027e42010-03-21 10:26:34 +080051MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
52MODULE_DESCRIPTION("Eee PC WMI Hotkey Driver");
53MODULE_LICENSE("GPL");
54
Corentin Charyd358cb52010-11-29 08:14:14 +010055#define EEEPC_ACPI_HID "ASUS010" /* old _HID used in eeepc-laptop */
56
Yong Wangee027e42010-03-21 10:26:34 +080057#define EEEPC_WMI_EVENT_GUID "ABBC0F72-8EA1-11D1-00A0-C90629100000"
Yong Wang3d7b1652010-04-11 09:27:54 +080058#define EEEPC_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
Yong Wangee027e42010-03-21 10:26:34 +080059
60MODULE_ALIAS("wmi:"EEEPC_WMI_EVENT_GUID);
Yong Wang3d7b1652010-04-11 09:27:54 +080061MODULE_ALIAS("wmi:"EEEPC_WMI_MGMT_GUID);
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 Chary33e0e6f2011-02-06 13:28:34 +010068#define EEEPC_WMI_METHODID_DSTS 0x53544344
69#define EEEPC_WMI_METHODID_DEVS 0x53564544
70#define EEEPC_WMI_METHODID_CFVS 0x53564643
Yong Wang3d7b1652010-04-11 09:27:54 +080071
Corentin Charyba48fdb2010-11-29 08:14:07 +010072#define EEEPC_WMI_DEVID_WLAN 0x00010011
73#define EEEPC_WMI_DEVID_BLUETOOTH 0x00010013
Corentin Chary2e9e1592011-02-06 13:28:37 +010074#define EEEPC_WMI_DEVID_WIMAX 0x00010017
Corentin Charyba48fdb2010-11-29 08:14:07 +010075#define EEEPC_WMI_DEVID_WWAN3G 0x00010019
Corentin Chary33e0e6f2011-02-06 13:28:34 +010076#define EEEPC_WMI_DEVID_BACKLIGHT 0x00050012
Corentin Chary9e1565b2011-02-06 13:28:36 +010077#define EEEPC_WMI_DEVID_CAMERA 0x00060013
78#define EEEPC_WMI_DEVID_CARDREADER 0x00080013
Corentin Chary33e0e6f2011-02-06 13:28:34 +010079#define EEEPC_WMI_DEVID_TPDLED 0x00100011
Yong Wang3d7b1652010-04-11 09:27:54 +080080
Corentin Charyaafa7192011-02-06 13:28:35 +010081#define EEEPC_WMI_DSTS_STATUS_BIT 0x00000001
82#define EEEPC_WMI_DSTS_PRESENCE_BIT 0x00010000
83
Corentin Charyafa7c882011-02-06 13:28:28 +010084static bool hotplug_wireless;
85
86module_param(hotplug_wireless, bool, 0444);
87MODULE_PARM_DESC(hotplug_wireless,
88 "Enable hotplug for wireless device. "
89 "If your laptop needs that, please report to "
90 "acpi4asus-user@lists.sourceforge.net.");
91
Yong Wangee027e42010-03-21 10:26:34 +080092static const struct key_entry eeepc_wmi_keymap[] = {
93 /* Sleep already handled via generic ACPI code */
Yong Wangee027e42010-03-21 10:26:34 +080094 { KE_IGNORE, NOTIFY_BRNDOWN_MIN, { KEY_BRIGHTNESSDOWN } },
95 { KE_IGNORE, NOTIFY_BRNUP_MIN, { KEY_BRIGHTNESSUP } },
Corentin Chary5628e5a2011-02-06 13:28:26 +010096 { KE_KEY, 0x30, { KEY_VOLUMEUP } },
97 { KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
98 { KE_KEY, 0x32, { KEY_MUTE } },
99 { KE_KEY, 0x5c, { KEY_F15 } },
100 { KE_KEY, 0x5d, { KEY_WLAN } },
Chris Bagwelleda17482010-10-11 18:47:17 -0500101 { KE_KEY, 0x6b, { KEY_F13 } }, /* Disable Touchpad */
Corentin Charybc40cce2011-02-06 13:28:27 +0100102 { KE_KEY, 0x88, { KEY_WLAN } },
Corentin Chary5628e5a2011-02-06 13:28:26 +0100103 { KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } },
104 { KE_KEY, 0xe0, { KEY_PROG1 } },
Chris Bagwelleda17482010-10-11 18:47:17 -0500105 { KE_KEY, 0xe1, { KEY_F14 } },
Corentin Chary54c799a2011-02-06 13:28:38 +0100106 { KE_KEY, 0xe9, { KEY_BRIGHTNESS_ZERO } },
Yong Wangee027e42010-03-21 10:26:34 +0800107 { KE_END, 0},
108};
109
Yong Wang3d7b1652010-04-11 09:27:54 +0800110struct bios_args {
111 u32 dev_id;
112 u32 ctrl_param;
113};
114
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100115/*
116 * eeepc-wmi/ - debugfs root directory
117 * dev_id - current dev_id
118 * ctrl_param - current ctrl_param
119 * devs - call DEVS(dev_id, ctrl_param) and print result
120 * dsts - call DSTS(dev_id) and print result
121 */
122struct eeepc_wmi_debug {
123 struct dentry *root;
124 u32 dev_id;
125 u32 ctrl_param;
126};
127
Yong Wang81248882010-04-11 09:26:33 +0800128struct eeepc_wmi {
Corentin Charyafa7c882011-02-06 13:28:28 +0100129 bool hotplug_wireless;
130
Yong Wang81248882010-04-11 09:26:33 +0800131 struct input_dev *inputdev;
Yong Wang3d7b1652010-04-11 09:27:54 +0800132 struct backlight_device *backlight_device;
Corentin Chary27c136c2010-11-29 08:14:05 +0100133 struct platform_device *platform_device;
Corentin Chary084fca62010-11-29 08:14:06 +0100134
135 struct led_classdev tpd_led;
136 int tpd_led_wk;
137 struct workqueue_struct *led_workqueue;
138 struct work_struct tpd_led_work;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100139
140 struct rfkill *wlan_rfkill;
141 struct rfkill *bluetooth_rfkill;
Corentin Chary2e9e1592011-02-06 13:28:37 +0100142 struct rfkill *wimax_rfkill;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100143 struct rfkill *wwan3g_rfkill;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100144
Corentin Charyafa7c882011-02-06 13:28:28 +0100145 struct hotplug_slot *hotplug_slot;
146 struct mutex hotplug_lock;
Corentin Chary279f8f92011-02-06 13:28:29 +0100147 struct mutex wmi_lock;
148 struct workqueue_struct *hotplug_workqueue;
149 struct work_struct hotplug_work;
Corentin Charyafa7c882011-02-06 13:28:28 +0100150
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100151 struct eeepc_wmi_debug debug;
Yong Wang81248882010-04-11 09:26:33 +0800152};
153
Yong Wang81248882010-04-11 09:26:33 +0800154static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc)
Yong Wangee027e42010-03-21 10:26:34 +0800155{
156 int err;
157
Yong Wang81248882010-04-11 09:26:33 +0800158 eeepc->inputdev = input_allocate_device();
159 if (!eeepc->inputdev)
Yong Wangee027e42010-03-21 10:26:34 +0800160 return -ENOMEM;
161
Yong Wang81248882010-04-11 09:26:33 +0800162 eeepc->inputdev->name = "Eee PC WMI hotkeys";
Yong Wang45f2c692010-04-11 09:27:19 +0800163 eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0";
Yong Wang81248882010-04-11 09:26:33 +0800164 eeepc->inputdev->id.bustype = BUS_HOST;
Corentin Chary27c136c2010-11-29 08:14:05 +0100165 eeepc->inputdev->dev.parent = &eeepc->platform_device->dev;
Yong Wangee027e42010-03-21 10:26:34 +0800166
Yong Wang81248882010-04-11 09:26:33 +0800167 err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL);
Yong Wangee027e42010-03-21 10:26:34 +0800168 if (err)
169 goto err_free_dev;
170
Yong Wang81248882010-04-11 09:26:33 +0800171 err = input_register_device(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800172 if (err)
173 goto err_free_keymap;
174
175 return 0;
176
177err_free_keymap:
Yong Wang81248882010-04-11 09:26:33 +0800178 sparse_keymap_free(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800179err_free_dev:
Yong Wang81248882010-04-11 09:26:33 +0800180 input_free_device(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800181 return err;
182}
183
Yong Wang81248882010-04-11 09:26:33 +0800184static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc)
185{
186 if (eeepc->inputdev) {
187 sparse_keymap_free(eeepc->inputdev);
188 input_unregister_device(eeepc->inputdev);
189 }
190
191 eeepc->inputdev = NULL;
192}
193
Corentin Chary2a3f0062010-11-29 08:14:10 +0100194static acpi_status eeepc_wmi_get_devstate(u32 dev_id, u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800195{
196 struct acpi_buffer input = { (acpi_size)sizeof(u32), &dev_id };
197 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
198 union acpi_object *obj;
199 acpi_status status;
200 u32 tmp;
201
202 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
Corentin Charyafa7c882011-02-06 13:28:28 +0100203 1, EEEPC_WMI_METHODID_DSTS,
204 &input, &output);
Yong Wang3d7b1652010-04-11 09:27:54 +0800205
206 if (ACPI_FAILURE(status))
207 return status;
208
209 obj = (union acpi_object *)output.pointer;
210 if (obj && obj->type == ACPI_TYPE_INTEGER)
211 tmp = (u32)obj->integer.value;
212 else
213 tmp = 0;
214
Corentin Chary2a3f0062010-11-29 08:14:10 +0100215 if (retval)
216 *retval = tmp;
Yong Wang3d7b1652010-04-11 09:27:54 +0800217
218 kfree(obj);
219
220 return status;
221
222}
223
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100224static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
225 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800226{
227 struct bios_args args = {
228 .dev_id = dev_id,
229 .ctrl_param = ctrl_param,
230 };
231 struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
232 acpi_status status;
233
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100234 if (!retval) {
235 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
236 EEEPC_WMI_METHODID_DEVS,
237 &input, NULL);
238 } else {
239 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
240 union acpi_object *obj;
241 u32 tmp;
242
243 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
244 EEEPC_WMI_METHODID_DEVS,
245 &input, &output);
246
247 if (ACPI_FAILURE(status))
248 return status;
249
250 obj = (union acpi_object *)output.pointer;
251 if (obj && obj->type == ACPI_TYPE_INTEGER)
252 tmp = (u32)obj->integer.value;
253 else
254 tmp = 0;
255
256 *retval = tmp;
257
258 kfree(obj);
259 }
Yong Wang3d7b1652010-04-11 09:27:54 +0800260
261 return status;
262}
263
Corentin Chary5c956382011-02-06 13:28:31 +0100264/* Helper for special devices with magic return codes */
265static int eeepc_wmi_get_devstate_simple(u32 dev_id)
266{
267 u32 retval = 0;
268 acpi_status status;
269
270 status = eeepc_wmi_get_devstate(dev_id, &retval);
271
272 if (ACPI_FAILURE(status))
273 return -EINVAL;
274
Corentin Charyaafa7192011-02-06 13:28:35 +0100275 if (!(retval & EEEPC_WMI_DSTS_PRESENCE_BIT))
Corentin Chary5c956382011-02-06 13:28:31 +0100276 return -ENODEV;
277
Corentin Charyaafa7192011-02-06 13:28:35 +0100278 return retval & EEEPC_WMI_DSTS_STATUS_BIT;
Corentin Chary5c956382011-02-06 13:28:31 +0100279}
280
Corentin Chary084fca62010-11-29 08:14:06 +0100281/*
282 * LEDs
283 */
284/*
285 * These functions actually update the LED's, and are called from a
286 * workqueue. By doing this as separate work rather than when the LED
287 * subsystem asks, we avoid messing with the Eeepc ACPI stuff during a
288 * potentially bad time, such as a timer interrupt.
289 */
290static void tpd_led_update(struct work_struct *work)
291{
292 int ctrl_param;
293 struct eeepc_wmi *eeepc;
294
295 eeepc = container_of(work, struct eeepc_wmi, tpd_led_work);
296
297 ctrl_param = eeepc->tpd_led_wk;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100298 eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_TPDLED, ctrl_param, NULL);
Corentin Chary084fca62010-11-29 08:14:06 +0100299}
300
301static void tpd_led_set(struct led_classdev *led_cdev,
302 enum led_brightness value)
303{
304 struct eeepc_wmi *eeepc;
305
306 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
307
308 eeepc->tpd_led_wk = !!value;
309 queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
310}
311
312static int read_tpd_state(struct eeepc_wmi *eeepc)
313{
Corentin Chary5c956382011-02-06 13:28:31 +0100314 return eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_TPDLED);
Corentin Chary084fca62010-11-29 08:14:06 +0100315}
316
317static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
318{
319 struct eeepc_wmi *eeepc;
320
321 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
322
323 return read_tpd_state(eeepc);
324}
325
326static int eeepc_wmi_led_init(struct eeepc_wmi *eeepc)
327{
328 int rv;
329
330 if (read_tpd_state(eeepc) < 0)
331 return 0;
332
333 eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
334 if (!eeepc->led_workqueue)
335 return -ENOMEM;
336 INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
337
338 eeepc->tpd_led.name = "eeepc::touchpad";
339 eeepc->tpd_led.brightness_set = tpd_led_set;
340 eeepc->tpd_led.brightness_get = tpd_led_get;
341 eeepc->tpd_led.max_brightness = 1;
342
343 rv = led_classdev_register(&eeepc->platform_device->dev,
344 &eeepc->tpd_led);
345 if (rv) {
346 destroy_workqueue(eeepc->led_workqueue);
347 return rv;
348 }
349
350 return 0;
351}
352
353static void eeepc_wmi_led_exit(struct eeepc_wmi *eeepc)
354{
355 if (eeepc->tpd_led.dev)
356 led_classdev_unregister(&eeepc->tpd_led);
357 if (eeepc->led_workqueue)
358 destroy_workqueue(eeepc->led_workqueue);
359}
360
361/*
Corentin Charyafa7c882011-02-06 13:28:28 +0100362 * PCI hotplug (for wlan rfkill)
363 */
364static bool eeepc_wlan_rfkill_blocked(struct eeepc_wmi *eeepc)
365{
Corentin Chary5c956382011-02-06 13:28:31 +0100366 int result = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100367
Corentin Chary5c956382011-02-06 13:28:31 +0100368 if (result < 0)
Corentin Charyafa7c882011-02-06 13:28:28 +0100369 return false;
Corentin Chary5c956382011-02-06 13:28:31 +0100370 return !result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100371}
372
373static void eeepc_rfkill_hotplug(struct eeepc_wmi *eeepc)
374{
375 struct pci_dev *dev;
376 struct pci_bus *bus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100377 bool blocked;
Corentin Charyafa7c882011-02-06 13:28:28 +0100378 bool absent;
379 u32 l;
380
Corentin Chary279f8f92011-02-06 13:28:29 +0100381 mutex_lock(&eeepc->wmi_lock);
382 blocked = eeepc_wlan_rfkill_blocked(eeepc);
383 mutex_unlock(&eeepc->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100384
385 mutex_lock(&eeepc->hotplug_lock);
386
Corentin Chary279f8f92011-02-06 13:28:29 +0100387 if (eeepc->wlan_rfkill)
388 rfkill_set_sw_state(eeepc->wlan_rfkill, blocked);
389
Corentin Charyafa7c882011-02-06 13:28:28 +0100390 if (eeepc->hotplug_slot) {
391 bus = pci_find_bus(0, 1);
392 if (!bus) {
393 pr_warning("Unable to find PCI bus 1?\n");
394 goto out_unlock;
395 }
396
397 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
398 pr_err("Unable to read PCI config space?\n");
399 goto out_unlock;
400 }
401 absent = (l == 0xffffffff);
402
403 if (blocked != absent) {
404 pr_warning("BIOS says wireless lan is %s, "
405 "but the pci device is %s\n",
406 blocked ? "blocked" : "unblocked",
407 absent ? "absent" : "present");
408 pr_warning("skipped wireless hotplug as probably "
409 "inappropriate for this model\n");
410 goto out_unlock;
411 }
412
413 if (!blocked) {
414 dev = pci_get_slot(bus, 0);
415 if (dev) {
416 /* Device already present */
417 pci_dev_put(dev);
418 goto out_unlock;
419 }
420 dev = pci_scan_single_device(bus, 0);
421 if (dev) {
422 pci_bus_assign_resources(bus);
423 if (pci_bus_add_device(dev))
424 pr_err("Unable to hotplug wifi\n");
425 }
426 } else {
427 dev = pci_get_slot(bus, 0);
428 if (dev) {
429 pci_remove_bus_device(dev);
430 pci_dev_put(dev);
431 }
432 }
433 }
434
435out_unlock:
436 mutex_unlock(&eeepc->hotplug_lock);
437}
438
439static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
440{
441 struct eeepc_wmi *eeepc = data;
442
443 if (event != ACPI_NOTIFY_BUS_CHECK)
444 return;
445
Corentin Chary279f8f92011-02-06 13:28:29 +0100446 /*
447 * We can't call directly eeepc_rfkill_hotplug because most
448 * of the time WMBC is still being executed and not reetrant.
449 * There is currently no way to tell ACPICA that we want this
450 * method to be serialized, we schedule a eeepc_rfkill_hotplug
451 * call later, in a safer context.
452 */
453 queue_work(eeepc->hotplug_workqueue, &eeepc->hotplug_work);
Corentin Charyafa7c882011-02-06 13:28:28 +0100454}
455
456static int eeepc_register_rfkill_notifier(struct eeepc_wmi *eeepc,
457 char *node)
458{
459 acpi_status status;
460 acpi_handle handle;
461
462 status = acpi_get_handle(NULL, node, &handle);
463
464 if (ACPI_SUCCESS(status)) {
465 status = acpi_install_notify_handler(handle,
466 ACPI_SYSTEM_NOTIFY,
467 eeepc_rfkill_notify,
468 eeepc);
469 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
477static void eeepc_unregister_rfkill_notifier(struct eeepc_wmi *eeepc,
478 char *node)
479{
480 acpi_status status = AE_OK;
481 acpi_handle handle;
482
483 status = acpi_get_handle(NULL, node, &handle);
484
485 if (ACPI_SUCCESS(status)) {
486 status = acpi_remove_notify_handler(handle,
487 ACPI_SYSTEM_NOTIFY,
488 eeepc_rfkill_notify);
489 if (ACPI_FAILURE(status))
490 pr_err("Error removing rfkill notify handler %s\n",
491 node);
492 }
493}
494
495static int eeepc_get_adapter_status(struct hotplug_slot *hotplug_slot,
496 u8 *value)
497{
Corentin Chary5c956382011-02-06 13:28:31 +0100498 int result = eeepc_wmi_get_devstate_simple(EEEPC_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
507static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
508{
509 kfree(hotplug_slot->info);
510 kfree(hotplug_slot);
511}
512
513static struct hotplug_slot_ops eeepc_hotplug_slot_ops = {
514 .owner = THIS_MODULE,
515 .get_adapter_status = eeepc_get_adapter_status,
516 .get_power_status = eeepc_get_adapter_status,
517};
518
Corentin Chary279f8f92011-02-06 13:28:29 +0100519static void eeepc_hotplug_work(struct work_struct *work)
520{
521 struct eeepc_wmi *eeepc;
522
523 eeepc = container_of(work, struct eeepc_wmi, hotplug_work);
524 eeepc_rfkill_hotplug(eeepc);
525}
526
Corentin Charyafa7c882011-02-06 13:28:28 +0100527static int eeepc_setup_pci_hotplug(struct eeepc_wmi *eeepc)
528{
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 Chary279f8f92011-02-06 13:28:29 +0100537 eeepc->hotplug_workqueue =
538 create_singlethread_workqueue("hotplug_workqueue");
539 if (!eeepc->hotplug_workqueue)
540 goto error_workqueue;
541
542 INIT_WORK(&eeepc->hotplug_work, eeepc_hotplug_work);
543
Corentin Charyafa7c882011-02-06 13:28:28 +0100544 eeepc->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
545 if (!eeepc->hotplug_slot)
546 goto error_slot;
547
548 eeepc->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
549 GFP_KERNEL);
550 if (!eeepc->hotplug_slot->info)
551 goto error_info;
552
553 eeepc->hotplug_slot->private = eeepc;
554 eeepc->hotplug_slot->release = &eeepc_cleanup_pci_hotplug;
555 eeepc->hotplug_slot->ops = &eeepc_hotplug_slot_ops;
556 eeepc_get_adapter_status(eeepc->hotplug_slot,
557 &eeepc->hotplug_slot->info->adapter_status);
558
559 ret = pci_hp_register(eeepc->hotplug_slot, bus, 0, "eeepc-wifi");
560 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:
568 kfree(eeepc->hotplug_slot->info);
569error_info:
570 kfree(eeepc->hotplug_slot);
571 eeepc->hotplug_slot = NULL;
572error_slot:
Corentin Chary279f8f92011-02-06 13:28:29 +0100573 destroy_workqueue(eeepc->hotplug_workqueue);
574error_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 */
581static int eeepc_rfkill_set(void *data, bool blocked)
582{
583 int dev_id = (unsigned long)data;
584 u32 ctrl_param = !blocked;
Corentin Chary7898cf12011-02-06 13:28:30 +0100585 acpi_status status;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100586
Corentin Chary7898cf12011-02-06 13:28:30 +0100587 status = eeepc_wmi_set_devstate(dev_id, ctrl_param, NULL);
588
589 if (ACPI_FAILURE(status))
590 return -EIO;
591
592 return 0;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100593}
594
595static void eeepc_rfkill_query(struct rfkill *rfkill, void *data)
596{
597 int dev_id = (unsigned long)data;
Corentin Chary5c956382011-02-06 13:28:31 +0100598 int result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100599
Corentin Chary5c956382011-02-06 13:28:31 +0100600 result = eeepc_wmi_get_devstate_simple(dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100601
Corentin Chary5c956382011-02-06 13:28:31 +0100602 if (result < 0)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100603 return ;
604
Corentin Chary5c956382011-02-06 13:28:31 +0100605 rfkill_set_sw_state(rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100606}
607
Corentin Chary279f8f92011-02-06 13:28:29 +0100608static int eeepc_rfkill_wlan_set(void *data, bool blocked)
609{
610 struct eeepc_wmi *eeepc = data;
611 int ret;
612
613 /*
614 * This handler is enabled only if hotplug is enabled.
615 * In this case, the eeepc_wmi_set_devstate() will
616 * trigger a wmi notification and we need to wait
617 * this call to finish before being able to call
618 * any wmi method
619 */
620 mutex_lock(&eeepc->wmi_lock);
621 ret = eeepc_rfkill_set((void *)(long)EEEPC_WMI_DEVID_WLAN, blocked);
622 mutex_unlock(&eeepc->wmi_lock);
623 return ret;
624}
625
626static void eeepc_rfkill_wlan_query(struct rfkill *rfkill, void *data)
627{
628 eeepc_rfkill_query(rfkill, (void *)(long)EEEPC_WMI_DEVID_WLAN);
629}
630
631static const struct rfkill_ops eeepc_rfkill_wlan_ops = {
632 .set_block = eeepc_rfkill_wlan_set,
633 .query = eeepc_rfkill_wlan_query,
634};
635
Corentin Charyba48fdb2010-11-29 08:14:07 +0100636static const struct rfkill_ops eeepc_rfkill_ops = {
637 .set_block = eeepc_rfkill_set,
638 .query = eeepc_rfkill_query,
639};
640
641static int eeepc_new_rfkill(struct eeepc_wmi *eeepc,
642 struct rfkill **rfkill,
643 const char *name,
644 enum rfkill_type type, int dev_id)
645{
Corentin Chary5c956382011-02-06 13:28:31 +0100646 int result = eeepc_wmi_get_devstate_simple(dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100647
Corentin Chary5c956382011-02-06 13:28:31 +0100648 if (result < 0)
649 return result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100650
Corentin Chary279f8f92011-02-06 13:28:29 +0100651 if (dev_id == EEEPC_WMI_DEVID_WLAN && eeepc->hotplug_wireless)
652 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
653 &eeepc_rfkill_wlan_ops, eeepc);
654 else
655 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
656 &eeepc_rfkill_ops, (void *)(long)dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100657
658 if (!*rfkill)
659 return -EINVAL;
660
Corentin Chary5c956382011-02-06 13:28:31 +0100661 rfkill_init_sw_state(*rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100662 result = rfkill_register(*rfkill);
663 if (result) {
664 rfkill_destroy(*rfkill);
665 *rfkill = NULL;
666 return result;
667 }
668 return 0;
669}
670
671static void eeepc_wmi_rfkill_exit(struct eeepc_wmi *eeepc)
672{
Corentin Charyafa7c882011-02-06 13:28:28 +0100673 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
674 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
675 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
Corentin Charyba48fdb2010-11-29 08:14:07 +0100676 if (eeepc->wlan_rfkill) {
677 rfkill_unregister(eeepc->wlan_rfkill);
678 rfkill_destroy(eeepc->wlan_rfkill);
679 eeepc->wlan_rfkill = NULL;
680 }
Corentin Charyafa7c882011-02-06 13:28:28 +0100681 /*
682 * Refresh pci hotplug in case the rfkill state was changed after
683 * eeepc_unregister_rfkill_notifier()
684 */
685 eeepc_rfkill_hotplug(eeepc);
686 if (eeepc->hotplug_slot)
687 pci_hp_deregister(eeepc->hotplug_slot);
Corentin Chary279f8f92011-02-06 13:28:29 +0100688 if (eeepc->hotplug_workqueue)
689 destroy_workqueue(eeepc->hotplug_workqueue);
Corentin Charyafa7c882011-02-06 13:28:28 +0100690
Corentin Charyba48fdb2010-11-29 08:14:07 +0100691 if (eeepc->bluetooth_rfkill) {
692 rfkill_unregister(eeepc->bluetooth_rfkill);
693 rfkill_destroy(eeepc->bluetooth_rfkill);
694 eeepc->bluetooth_rfkill = NULL;
695 }
Corentin Chary2e9e1592011-02-06 13:28:37 +0100696 if (eeepc->wimax_rfkill) {
697 rfkill_unregister(eeepc->wimax_rfkill);
698 rfkill_destroy(eeepc->wimax_rfkill);
699 eeepc->wimax_rfkill = NULL;
700 }
Corentin Charyba48fdb2010-11-29 08:14:07 +0100701 if (eeepc->wwan3g_rfkill) {
702 rfkill_unregister(eeepc->wwan3g_rfkill);
703 rfkill_destroy(eeepc->wwan3g_rfkill);
704 eeepc->wwan3g_rfkill = NULL;
705 }
706}
707
708static int eeepc_wmi_rfkill_init(struct eeepc_wmi *eeepc)
709{
710 int result = 0;
711
Corentin Charyafa7c882011-02-06 13:28:28 +0100712 mutex_init(&eeepc->hotplug_lock);
Corentin Chary279f8f92011-02-06 13:28:29 +0100713 mutex_init(&eeepc->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100714
Corentin Charyba48fdb2010-11-29 08:14:07 +0100715 result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
716 "eeepc-wlan", RFKILL_TYPE_WLAN,
717 EEEPC_WMI_DEVID_WLAN);
718
719 if (result && result != -ENODEV)
720 goto exit;
721
722 result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
723 "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
724 EEEPC_WMI_DEVID_BLUETOOTH);
725
726 if (result && result != -ENODEV)
727 goto exit;
728
Corentin Chary2e9e1592011-02-06 13:28:37 +0100729 result = eeepc_new_rfkill(eeepc, &eeepc->wimax_rfkill,
730 "eeepc-wimax", RFKILL_TYPE_WIMAX,
731 EEEPC_WMI_DEVID_WIMAX);
732
733 if (result && result != -ENODEV)
734 goto exit;
735
Corentin Charyba48fdb2010-11-29 08:14:07 +0100736 result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
737 "eeepc-wwan3g", RFKILL_TYPE_WWAN,
738 EEEPC_WMI_DEVID_WWAN3G);
739
740 if (result && result != -ENODEV)
741 goto exit;
742
Corentin Charyafa7c882011-02-06 13:28:28 +0100743 result = eeepc_setup_pci_hotplug(eeepc);
744 /*
745 * If we get -EBUSY then something else is handling the PCI hotplug -
746 * don't fail in this case
747 */
748 if (result == -EBUSY)
749 result = 0;
750
751 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
752 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
753 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
754 /*
755 * Refresh pci hotplug in case the rfkill state was changed during
756 * setup.
757 */
758 eeepc_rfkill_hotplug(eeepc);
759
Corentin Charyba48fdb2010-11-29 08:14:07 +0100760exit:
761 if (result && result != -ENODEV)
762 eeepc_wmi_rfkill_exit(eeepc);
763
764 if (result == -ENODEV)
765 result = 0;
766
767 return result;
768}
769
770/*
Corentin Chary084fca62010-11-29 08:14:06 +0100771 * Backlight
772 */
Yong Wang3d7b1652010-04-11 09:27:54 +0800773static int read_brightness(struct backlight_device *bd)
774{
Corentin Charydfed65d2010-11-29 08:14:12 +0100775 u32 retval;
Yong Wang3d7b1652010-04-11 09:27:54 +0800776 acpi_status status;
777
Corentin Chary2a3f0062010-11-29 08:14:10 +0100778 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BACKLIGHT, &retval);
Yong Wang3d7b1652010-04-11 09:27:54 +0800779
780 if (ACPI_FAILURE(status))
781 return -1;
782 else
Corentin Chary2a3f0062010-11-29 08:14:10 +0100783 return retval & 0xFF;
Yong Wang3d7b1652010-04-11 09:27:54 +0800784}
785
786static int update_bl_status(struct backlight_device *bd)
787{
788
Corentin Charydfed65d2010-11-29 08:14:12 +0100789 u32 ctrl_param;
Yong Wang3d7b1652010-04-11 09:27:54 +0800790 acpi_status status;
791
792 ctrl_param = bd->props.brightness;
793
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100794 status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT,
795 ctrl_param, NULL);
Yong Wang3d7b1652010-04-11 09:27:54 +0800796
797 if (ACPI_FAILURE(status))
798 return -1;
799 else
800 return 0;
801}
802
803static const struct backlight_ops eeepc_wmi_bl_ops = {
804 .get_brightness = read_brightness,
805 .update_status = update_bl_status,
806};
807
808static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
809{
810 struct backlight_device *bd = eeepc->backlight_device;
811 int old = bd->props.brightness;
Daniel Mackb7670ed2010-05-19 12:37:01 +0200812 int new = old;
Yong Wang3d7b1652010-04-11 09:27:54 +0800813
814 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
815 new = code - NOTIFY_BRNUP_MIN + 1;
816 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
817 new = code - NOTIFY_BRNDOWN_MIN;
818
819 bd->props.brightness = new;
820 backlight_update_status(bd);
821 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
822
823 return old;
824}
825
826static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
827{
828 struct backlight_device *bd;
829 struct backlight_properties props;
830
831 memset(&props, 0, sizeof(struct backlight_properties));
832 props.max_brightness = 15;
833 bd = backlight_device_register(EEEPC_WMI_FILE,
Corentin Chary27c136c2010-11-29 08:14:05 +0100834 &eeepc->platform_device->dev, eeepc,
Yong Wang3d7b1652010-04-11 09:27:54 +0800835 &eeepc_wmi_bl_ops, &props);
836 if (IS_ERR(bd)) {
837 pr_err("Could not register backlight device\n");
838 return PTR_ERR(bd);
839 }
840
841 eeepc->backlight_device = bd;
842
843 bd->props.brightness = read_brightness(bd);
844 bd->props.power = FB_BLANK_UNBLANK;
845 backlight_update_status(bd);
846
847 return 0;
848}
849
850static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
851{
852 if (eeepc->backlight_device)
853 backlight_device_unregister(eeepc->backlight_device);
854
855 eeepc->backlight_device = NULL;
856}
857
858static void eeepc_wmi_notify(u32 value, void *context)
859{
860 struct eeepc_wmi *eeepc = context;
861 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
862 union acpi_object *obj;
863 acpi_status status;
864 int code;
865 int orig_code;
866
867 status = wmi_get_event_data(value, &response);
868 if (status != AE_OK) {
869 pr_err("bad event status 0x%x\n", status);
870 return;
871 }
872
873 obj = (union acpi_object *)response.pointer;
874
875 if (obj && obj->type == ACPI_TYPE_INTEGER) {
876 code = obj->integer.value;
877 orig_code = code;
878
879 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
880 code = NOTIFY_BRNUP_MIN;
881 else if (code >= NOTIFY_BRNDOWN_MIN &&
882 code <= NOTIFY_BRNDOWN_MAX)
883 code = NOTIFY_BRNDOWN_MIN;
884
885 if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
886 if (!acpi_video_backlight_support())
887 eeepc_wmi_backlight_notify(eeepc, orig_code);
888 }
889
890 if (!sparse_keymap_report_event(eeepc->inputdev,
891 code, 1, true))
892 pr_info("Unknown key %x pressed\n", code);
893 }
894
895 kfree(obj);
896}
897
Corentin Chary9e1565b2011-02-06 13:28:36 +0100898/*
899 * Sys helpers
900 */
901static int parse_arg(const char *buf, unsigned long count, int *val)
902{
903 if (!count)
904 return 0;
905 if (sscanf(buf, "%i", val) != 1)
906 return -EINVAL;
907 return count;
908}
909
910static ssize_t store_sys_wmi(int devid, const char *buf, size_t count)
911{
912 acpi_status status;
913 u32 retval;
914 int rv, value;
915
916 value = eeepc_wmi_get_devstate_simple(devid);
917 if (value == -ENODEV) /* Check device presence */
918 return value;
919
920 rv = parse_arg(buf, count, &value);
921 status = eeepc_wmi_set_devstate(devid, value, &retval);
922
923 if (ACPI_FAILURE(status))
924 return -EIO;
925 return rv;
926}
927
928static ssize_t show_sys_wmi(int devid, char *buf)
929{
930 int value = eeepc_wmi_get_devstate_simple(devid);
931
932 if (value < 0)
933 return value;
934
935 return sprintf(buf, "%d\n", value);
936}
937
938#define EEEPC_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
939 static ssize_t show_##_name(struct device *dev, \
940 struct device_attribute *attr, \
941 char *buf) \
942 { \
943 return show_sys_wmi(_cm, buf); \
944 } \
945 static ssize_t store_##_name(struct device *dev, \
946 struct device_attribute *attr, \
947 const char *buf, size_t count) \
948 { \
949 return store_sys_wmi(_cm, buf, count); \
950 } \
951 static struct device_attribute dev_attr_##_name = { \
952 .attr = { \
953 .name = __stringify(_name), \
954 .mode = _mode }, \
955 .show = show_##_name, \
956 .store = store_##_name, \
957 }
958
959EEEPC_WMI_CREATE_DEVICE_ATTR(camera, 0644, EEEPC_WMI_DEVID_CAMERA);
960EEEPC_WMI_CREATE_DEVICE_ATTR(cardr, 0644, EEEPC_WMI_DEVID_CARDREADER);
961
Dmitry Torokhov67fa38e2010-11-03 11:14:01 -0700962static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
963 const char *buf, size_t count)
Chris Bagwell7f80d732010-10-11 18:47:18 -0500964{
965 int value;
966 struct acpi_buffer input = { (acpi_size)sizeof(value), &value };
967 acpi_status status;
968
969 if (!count || sscanf(buf, "%i", &value) != 1)
970 return -EINVAL;
971 if (value < 0 || value > 2)
972 return -EINVAL;
973
974 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
975 1, EEEPC_WMI_METHODID_CFVS, &input, NULL);
976
977 if (ACPI_FAILURE(status))
978 return -EIO;
979 else
980 return count;
981}
982
983static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
984
Corentin Chary4e37b422010-11-29 08:14:08 +0100985static struct attribute *platform_attributes[] = {
986 &dev_attr_cpufv.attr,
Corentin Chary9e1565b2011-02-06 13:28:36 +0100987 &dev_attr_camera.attr,
988 &dev_attr_cardr.attr,
Corentin Chary4e37b422010-11-29 08:14:08 +0100989 NULL
990};
991
Corentin Chary9e1565b2011-02-06 13:28:36 +0100992static mode_t eeepc_sysfs_is_visible(struct kobject *kobj,
993 struct attribute *attr,
994 int idx)
995{
996 bool supported = true;
997 int devid = -1;
998
999 if (attr == &dev_attr_camera.attr)
1000 devid = EEEPC_WMI_DEVID_CAMERA;
1001 else if (attr == &dev_attr_cardr.attr)
1002 devid = EEEPC_WMI_DEVID_CARDREADER;
1003
1004 if (devid != -1)
1005 supported = eeepc_wmi_get_devstate_simple(devid) != -ENODEV;
1006
1007 return supported ? attr->mode : 0;
1008}
1009
Corentin Chary4e37b422010-11-29 08:14:08 +01001010static struct attribute_group platform_attribute_group = {
Corentin Chary9e1565b2011-02-06 13:28:36 +01001011 .is_visible = eeepc_sysfs_is_visible,
1012 .attrs = platform_attributes
Corentin Chary4e37b422010-11-29 08:14:08 +01001013};
1014
Chris Bagwell7f80d732010-10-11 18:47:18 -05001015static void eeepc_wmi_sysfs_exit(struct platform_device *device)
1016{
Corentin Chary4e37b422010-11-29 08:14:08 +01001017 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001018}
1019
1020static int eeepc_wmi_sysfs_init(struct platform_device *device)
1021{
Corentin Chary4e37b422010-11-29 08:14:08 +01001022 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001023}
1024
Corentin Chary27c136c2010-11-29 08:14:05 +01001025/*
1026 * Platform device
1027 */
1028static int __init eeepc_wmi_platform_init(struct eeepc_wmi *eeepc)
1029{
Corentin Charya04ce292011-02-06 13:28:33 +01001030 return eeepc_wmi_sysfs_init(eeepc->platform_device);
Corentin Chary27c136c2010-11-29 08:14:05 +01001031}
1032
1033static void eeepc_wmi_platform_exit(struct eeepc_wmi *eeepc)
1034{
1035 eeepc_wmi_sysfs_exit(eeepc->platform_device);
Corentin Chary27c136c2010-11-29 08:14:05 +01001036}
1037
1038/*
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001039 * debugfs
1040 */
1041struct eeepc_wmi_debugfs_node {
1042 struct eeepc_wmi *eeepc;
1043 char *name;
1044 int (*show)(struct seq_file *m, void *data);
1045};
1046
1047static int show_dsts(struct seq_file *m, void *data)
1048{
1049 struct eeepc_wmi *eeepc = m->private;
1050 acpi_status status;
1051 u32 retval = -1;
1052
1053 status = eeepc_wmi_get_devstate(eeepc->debug.dev_id, &retval);
1054
1055 if (ACPI_FAILURE(status))
1056 return -EIO;
1057
1058 seq_printf(m, "DSTS(%x) = %x\n", eeepc->debug.dev_id, retval);
1059
1060 return 0;
1061}
1062
1063static int show_devs(struct seq_file *m, void *data)
1064{
1065 struct eeepc_wmi *eeepc = m->private;
1066 acpi_status status;
1067 u32 retval = -1;
1068
1069 status = eeepc_wmi_set_devstate(eeepc->debug.dev_id,
1070 eeepc->debug.ctrl_param, &retval);
1071 if (ACPI_FAILURE(status))
1072 return -EIO;
1073
1074 seq_printf(m, "DEVS(%x, %x) = %x\n", eeepc->debug.dev_id,
1075 eeepc->debug.ctrl_param, retval);
1076
1077 return 0;
1078}
1079
1080static struct eeepc_wmi_debugfs_node eeepc_wmi_debug_files[] = {
1081 { NULL, "devs", show_devs },
1082 { NULL, "dsts", show_dsts },
1083};
1084
1085static int eeepc_wmi_debugfs_open(struct inode *inode, struct file *file)
1086{
1087 struct eeepc_wmi_debugfs_node *node = inode->i_private;
1088
1089 return single_open(file, node->show, node->eeepc);
1090}
1091
1092static const struct file_operations eeepc_wmi_debugfs_io_ops = {
1093 .owner = THIS_MODULE,
1094 .open = eeepc_wmi_debugfs_open,
1095 .read = seq_read,
1096 .llseek = seq_lseek,
1097 .release = single_release,
1098};
1099
1100static void eeepc_wmi_debugfs_exit(struct eeepc_wmi *eeepc)
1101{
1102 debugfs_remove_recursive(eeepc->debug.root);
1103}
1104
1105static int eeepc_wmi_debugfs_init(struct eeepc_wmi *eeepc)
1106{
1107 struct dentry *dent;
1108 int i;
1109
1110 eeepc->debug.root = debugfs_create_dir(EEEPC_WMI_FILE, NULL);
1111 if (!eeepc->debug.root) {
1112 pr_err("failed to create debugfs directory");
1113 goto error_debugfs;
1114 }
1115
1116 dent = debugfs_create_x32("dev_id", S_IRUGO|S_IWUSR,
1117 eeepc->debug.root, &eeepc->debug.dev_id);
1118 if (!dent)
1119 goto error_debugfs;
1120
1121 dent = debugfs_create_x32("ctrl_param", S_IRUGO|S_IWUSR,
1122 eeepc->debug.root, &eeepc->debug.ctrl_param);
1123 if (!dent)
1124 goto error_debugfs;
1125
1126 for (i = 0; i < ARRAY_SIZE(eeepc_wmi_debug_files); i++) {
1127 struct eeepc_wmi_debugfs_node *node = &eeepc_wmi_debug_files[i];
1128
1129 node->eeepc = eeepc;
1130 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
1131 eeepc->debug.root, node,
1132 &eeepc_wmi_debugfs_io_ops);
1133 if (!dent) {
1134 pr_err("failed to create debug file: %s\n", node->name);
1135 goto error_debugfs;
1136 }
1137 }
1138
1139 return 0;
1140
1141error_debugfs:
1142 eeepc_wmi_debugfs_exit(eeepc);
1143 return -ENOMEM;
1144}
1145
1146/*
Corentin Chary27c136c2010-11-29 08:14:05 +01001147 * WMI Driver
1148 */
Corentin Charyafa7c882011-02-06 13:28:28 +01001149static void eeepc_dmi_check(struct eeepc_wmi *eeepc)
1150{
1151 const char *model;
1152
1153 model = dmi_get_system_info(DMI_PRODUCT_NAME);
1154 if (!model)
1155 return;
1156
1157 /*
1158 * Whitelist for wlan hotplug
1159 *
1160 * Eeepc 1000H needs the current hotplug code to handle
1161 * Fn+F2 correctly. We may add other Eeepc here later, but
1162 * it seems that most of the laptops supported by eeepc-wmi
1163 * don't need to be on this list
1164 */
1165 if (strcmp(model, "1000H") == 0) {
1166 eeepc->hotplug_wireless = true;
1167 pr_info("wlan hotplug enabled\n");
1168 }
1169}
1170
Corentin Charya04ce292011-02-06 13:28:33 +01001171static int __init eeepc_wmi_add(struct platform_device *pdev)
Yong Wangee027e42010-03-21 10:26:34 +08001172{
Yong Wang45f2c692010-04-11 09:27:19 +08001173 struct eeepc_wmi *eeepc;
Yong Wangee027e42010-03-21 10:26:34 +08001174 acpi_status status;
Corentin Chary27c136c2010-11-29 08:14:05 +01001175 int err;
Yong Wangee027e42010-03-21 10:26:34 +08001176
Corentin Chary27c136c2010-11-29 08:14:05 +01001177 eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
1178 if (!eeepc)
Corentin Charya04ce292011-02-06 13:28:33 +01001179 return -ENOMEM;
1180
1181 eeepc->platform_device = pdev;
1182 platform_set_drvdata(eeepc->platform_device, eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +01001183
Corentin Charyafa7c882011-02-06 13:28:28 +01001184 eeepc->hotplug_wireless = hotplug_wireless;
1185 eeepc_dmi_check(eeepc);
1186
Corentin Chary27c136c2010-11-29 08:14:05 +01001187 err = eeepc_wmi_platform_init(eeepc);
1188 if (err)
1189 goto fail_platform;
Yong Wang45f2c692010-04-11 09:27:19 +08001190
1191 err = eeepc_wmi_input_init(eeepc);
1192 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +01001193 goto fail_input;
Yong Wang3d7b1652010-04-11 09:27:54 +08001194
Corentin Chary084fca62010-11-29 08:14:06 +01001195 err = eeepc_wmi_led_init(eeepc);
1196 if (err)
1197 goto fail_leds;
1198
Corentin Charyba48fdb2010-11-29 08:14:07 +01001199 err = eeepc_wmi_rfkill_init(eeepc);
1200 if (err)
1201 goto fail_rfkill;
1202
Yong Wang3d7b1652010-04-11 09:27:54 +08001203 if (!acpi_video_backlight_support()) {
1204 err = eeepc_wmi_backlight_init(eeepc);
1205 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +01001206 goto fail_backlight;
Yong Wang3d7b1652010-04-11 09:27:54 +08001207 } else
1208 pr_info("Backlight controlled by ACPI video driver\n");
Yong Wang45f2c692010-04-11 09:27:19 +08001209
1210 status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
Corentin Chary27c136c2010-11-29 08:14:05 +01001211 eeepc_wmi_notify, eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +08001212 if (ACPI_FAILURE(status)) {
1213 pr_err("Unable to register notify handler - %d\n",
1214 status);
1215 err = -ENODEV;
Corentin Chary27c136c2010-11-29 08:14:05 +01001216 goto fail_wmi_handler;
Yong Wang45f2c692010-04-11 09:27:19 +08001217 }
1218
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001219 err = eeepc_wmi_debugfs_init(eeepc);
1220 if (err)
1221 goto fail_debugfs;
1222
Corentin Charya04ce292011-02-06 13:28:33 +01001223 return 0;
Yong Wang45f2c692010-04-11 09:27:19 +08001224
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001225fail_debugfs:
1226 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
Corentin Chary27c136c2010-11-29 08:14:05 +01001227fail_wmi_handler:
Yong Wang3d7b1652010-04-11 09:27:54 +08001228 eeepc_wmi_backlight_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +01001229fail_backlight:
Corentin Charyba48fdb2010-11-29 08:14:07 +01001230 eeepc_wmi_rfkill_exit(eeepc);
1231fail_rfkill:
Corentin Chary084fca62010-11-29 08:14:06 +01001232 eeepc_wmi_led_exit(eeepc);
1233fail_leds:
Yong Wang45f2c692010-04-11 09:27:19 +08001234 eeepc_wmi_input_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +01001235fail_input:
1236 eeepc_wmi_platform_exit(eeepc);
1237fail_platform:
1238 kfree(eeepc);
Corentin Charya04ce292011-02-06 13:28:33 +01001239 return err;
Yong Wang45f2c692010-04-11 09:27:19 +08001240}
1241
Corentin Charya04ce292011-02-06 13:28:33 +01001242static int __exit eeepc_wmi_remove(struct platform_device *device)
Yong Wang45f2c692010-04-11 09:27:19 +08001243{
1244 struct eeepc_wmi *eeepc;
1245
1246 eeepc = platform_get_drvdata(device);
1247 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
Yong Wang3d7b1652010-04-11 09:27:54 +08001248 eeepc_wmi_backlight_exit(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +08001249 eeepc_wmi_input_exit(eeepc);
Corentin Chary084fca62010-11-29 08:14:06 +01001250 eeepc_wmi_led_exit(eeepc);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001251 eeepc_wmi_rfkill_exit(eeepc);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001252 eeepc_wmi_debugfs_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +01001253 eeepc_wmi_platform_exit(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +08001254
Corentin Chary27c136c2010-11-29 08:14:05 +01001255 kfree(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +08001256 return 0;
1257}
1258
Corentin Chary0773d7f2011-02-06 13:28:32 +01001259/*
1260 * Platform driver - hibernate/resume callbacks
1261 */
1262static int eeepc_hotk_thaw(struct device *device)
1263{
1264 struct eeepc_wmi *eeepc = dev_get_drvdata(device);
1265
1266 if (eeepc->wlan_rfkill) {
1267 bool wlan;
1268
1269 /*
1270 * Work around bios bug - acpi _PTS turns off the wireless led
1271 * during suspend. Normally it restores it on resume, but
1272 * we should kick it ourselves in case hibernation is aborted.
1273 */
1274 wlan = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
1275 eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_WLAN, wlan, NULL);
1276 }
1277
1278 return 0;
1279}
1280
1281static int eeepc_hotk_restore(struct device *device)
1282{
1283 struct eeepc_wmi *eeepc = dev_get_drvdata(device);
1284 int bl;
1285
1286 /* Refresh both wlan rfkill state and pci hotplug */
1287 if (eeepc->wlan_rfkill)
1288 eeepc_rfkill_hotplug(eeepc);
1289
1290 if (eeepc->bluetooth_rfkill) {
1291 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_BLUETOOTH);
1292 rfkill_set_sw_state(eeepc->bluetooth_rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01001293 }
1294 if (eeepc->wimax_rfkill) {
1295 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WIMAX);
1296 rfkill_set_sw_state(eeepc->wimax_rfkill, bl);
1297 }
Corentin Chary0773d7f2011-02-06 13:28:32 +01001298 if (eeepc->wwan3g_rfkill) {
1299 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WWAN3G);
1300 rfkill_set_sw_state(eeepc->wwan3g_rfkill, bl);
1301 }
1302
1303 return 0;
1304}
1305
1306static const struct dev_pm_ops eeepc_pm_ops = {
1307 .thaw = eeepc_hotk_thaw,
1308 .restore = eeepc_hotk_restore,
1309};
1310
Yong Wang45f2c692010-04-11 09:27:19 +08001311static struct platform_driver platform_driver = {
Corentin Charya04ce292011-02-06 13:28:33 +01001312 .remove = __exit_p(eeepc_wmi_remove),
Yong Wang45f2c692010-04-11 09:27:19 +08001313 .driver = {
1314 .name = EEEPC_WMI_FILE,
1315 .owner = THIS_MODULE,
Corentin Chary0773d7f2011-02-06 13:28:32 +01001316 .pm = &eeepc_pm_ops,
Yong Wang45f2c692010-04-11 09:27:19 +08001317 },
Yong Wang45f2c692010-04-11 09:27:19 +08001318};
1319
Corentin Charyd358cb52010-11-29 08:14:14 +01001320static acpi_status __init eeepc_wmi_parse_device(acpi_handle handle, u32 level,
1321 void *context, void **retval)
1322{
1323 pr_warning("Found legacy ATKD device (%s)", EEEPC_ACPI_HID);
1324 *(bool *)context = true;
1325 return AE_CTRL_TERMINATE;
1326}
1327
1328static int __init eeepc_wmi_check_atkd(void)
1329{
1330 acpi_status status;
1331 bool found = false;
1332
1333 status = acpi_get_devices(EEEPC_ACPI_HID, eeepc_wmi_parse_device,
1334 &found, NULL);
1335
1336 if (ACPI_FAILURE(status) || !found)
1337 return 0;
1338 return -1;
1339}
1340
Corentin Charya04ce292011-02-06 13:28:33 +01001341static int __init eeepc_wmi_probe(struct platform_device *pdev)
Yong Wang45f2c692010-04-11 09:27:19 +08001342{
Yong Wang3d7b1652010-04-11 09:27:54 +08001343 if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
1344 !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
Yong Wang81248882010-04-11 09:26:33 +08001345 pr_warning("No known WMI GUID found\n");
Yong Wangee027e42010-03-21 10:26:34 +08001346 return -ENODEV;
1347 }
1348
Corentin Charyd358cb52010-11-29 08:14:14 +01001349 if (eeepc_wmi_check_atkd()) {
1350 pr_warning("WMI device present, but legacy ATKD device is also "
1351 "present and enabled.");
1352 pr_warning("You probably booted with acpi_osi=\"Linux\" or "
1353 "acpi_osi=\"!Windows 2009\"");
1354 pr_warning("Can't load eeepc-wmi, use default acpi_osi "
1355 "(preferred) or eeepc-laptop");
1356 return -ENODEV;
1357 }
1358
Corentin Charya04ce292011-02-06 13:28:33 +01001359 return eeepc_wmi_add(pdev);
1360}
Yong Wangee027e42010-03-21 10:26:34 +08001361
Corentin Charya04ce292011-02-06 13:28:33 +01001362static struct platform_device *platform_device;
Yong Wangee027e42010-03-21 10:26:34 +08001363
Corentin Charya04ce292011-02-06 13:28:33 +01001364static int __init eeepc_wmi_init(void)
1365{
1366 platform_device = platform_create_bundle(&platform_driver,
1367 eeepc_wmi_probe,
1368 NULL, 0, NULL, 0);
1369 if (IS_ERR(platform_device))
1370 return PTR_ERR(platform_device);
Yong Wangee027e42010-03-21 10:26:34 +08001371 return 0;
1372}
1373
1374static void __exit eeepc_wmi_exit(void)
1375{
Corentin Charya04ce292011-02-06 13:28:33 +01001376 platform_device_unregister(platform_device);
Yong Wang45f2c692010-04-11 09:27:19 +08001377 platform_driver_unregister(&platform_driver);
Yong Wangee027e42010-03-21 10:26:34 +08001378}
1379
1380module_init(eeepc_wmi_init);
1381module_exit(eeepc_wmi_exit);